Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/MLRISC/alpha/mltree/alpha.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/alpha/mltree/alpha.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 889 - (view) (download)

1 : monnier 409 (*
2 :     * This is a revamping of the Alpha32 instruction selection module
3 :     * using the new MLTREE and instruction representation. I've dropped
4 :     * the suffix 32 since we now support 64 bit datatypes.
5 :     *
6 :     * -- Allen
7 :     *
8 :     * Notes: places with optimizations are marked ***OPT***
9 :     *)
10 :    
11 :     functor Alpha
12 : leunga 744 (structure AlphaInstr : ALPHAINSTR
13 : monnier 409 structure PseudoInstrs : ALPHA_PSEUDO_INSTR
14 : george 555 structure ExtensionComp : MLTREE_EXTENSION_COMP
15 : leunga 775 where I = AlphaInstr
16 : monnier 475 sharing PseudoInstrs.I = AlphaInstr
17 : monnier 409
18 :     (* Cost of multiplication in cycles *)
19 :     val multCost : int ref
20 :    
21 :     (* Should we just use the native multiply by a constant? *)
22 :     val useMultByConst : bool ref
23 : george 545
24 :     (* Should we use SUD flags for floating point and generate DEFFREG?
25 :     * This should be set to false for C-like clients but true for SML/NJ.
26 :     *)
27 :     val SMLNJfloatingPoint : bool
28 : leunga 583
29 :     (* Should we use generate special byte/word load instructions
30 :     * like LDBU, LDWU, STB, STW.
31 :     *)
32 :     val byteWordLoadStores : bool ref
33 : monnier 409 ) : MLTREECOMP =
34 :     struct
35 :    
36 : leunga 775 structure I = AlphaInstr
37 :     structure C = I.C
38 :     structure T = I.T
39 : monnier 429 structure S = T.Stream
40 : leunga 775 structure R = T.Region
41 : monnier 409 structure W32 = Word32
42 : monnier 429 structure P = PseudoInstrs
43 : george 545 structure A = MLRiscAnnotations
44 : george 889 structure CB = CellsBasis
45 : monnier 409
46 :     (*********************************************************
47 :    
48 :     Trap Shadows, Floating Exceptions, and Denormalized
49 :     Numbers on the DEC Alpha
50 :    
51 :     Andrew W. Appel and Lal George
52 :     Nov 28, 1995
53 :    
54 :     See section 4.7.5.1 of the Alpha Architecture Reference Manual.
55 :    
56 :     The Alpha has imprecise exceptions, meaning that if a floating
57 :     point instruction raises an IEEE exception, the exception may
58 :     not interrupt the processor until several successive instructions have
59 :     completed. ML, on the other hand, may want a "precise" model
60 :     of floating point exceptions.
61 :    
62 :     Furthermore, the Alpha hardware does not support denormalized numbers
63 :     (for "gradual underflow"). Instead, underflow always rounds to zero.
64 :     However, each floating operation (add, mult, etc.) has a trapping
65 :     variant that will raise an exception (imprecisely, of course) on
66 :     underflow; in that case, the instruction will produce a zero result
67 :     AND an exception will occur. In fact, there are several variants
68 :     of each instruction; three variants of MULT are:
69 :    
70 :     MULT s1,s2,d truncate denormalized result to zero; no exception
71 :     MULT/U s1,s2,d truncate denormalized result to zero; raise UNDERFLOW
72 :     MULT/SU s1,s2,d software completion, producing denormalized result
73 :    
74 :     The hardware treats the MULT/U and MULT/SU instructions identically,
75 :     truncating a denormalized result to zero and raising the UNDERFLOW
76 :     exception. But the operating system, on an UNDERFLOW exception,
77 :     examines the faulting instruction to see if it's an /SU form, and if so,
78 :     recalculates s1*s2, puts the right answer in d, and continues,
79 :     all without invoking the user's signal handler.
80 :    
81 :     Because most machines compute with denormalized numbers in hardware,
82 :     to maximize portability of SML programs, we use the MULT/SU form.
83 :     (and ADD/SU, SUB/SU, etc.) But to use this form successfully,
84 :     certain rules have to be followed. Basically, d cannot be the same
85 :     register as s1 or s2, because the opsys needs to be able to
86 :     recalculate the operation using the original contents of s1 and s2,
87 :     and the MULT/SU instruction will overwrite d even if it traps.
88 :    
89 :     More generally, we may want to have a sequence of floating-point
90 :     instructions. The rules for such a sequence are:
91 :    
92 :     1. The sequence should end with a TRAPB (trap barrier) instruction.
93 :     (This could be relaxed somewhat, but certainly a TRAPB would
94 :     be a good idea sometime before the next branch instruction or
95 :     update of an ML reference variable, or any other ML side effect.)
96 :     2. No instruction in the sequence should destroy any operand of itself
97 :     or of any previous instruction in the sequence.
98 :     3. No two instructions in the sequence should write the same destination
99 :     register.
100 :    
101 :     We can achieve these conditions by the following trick in the
102 :     Alpha code generator. Each instruction in the sequence will write
103 :     to a different temporary; this is guaranteed by the translation from
104 :     ML-RISC. At the beginning of the sequence, we will put a special
105 :     pseudo-instruction (we call it DEFFREG) that "defines" the destination
106 :     register of the arithmetic instruction. If there are K arithmetic
107 :     instructions in the sequence, then we'll insert K DEFFREG instructions
108 :     all at the beginning of the sequence.
109 :     Then, each arithop will not only "define" its destination temporary
110 :     but will "use" it as well. When all these instructions are fed to
111 :     the liveness analyzer, the resulting interference graph will then
112 :     have inteference edges satisfying conditions 2 and 3 above.
113 :    
114 :     Of course, DEFFREG doesn't actually generate any code. In our model
115 :     of the Alpha, every instruction generates exactly 4 bytes of code
116 :     except the "span-dependent" ones. Therefore, we'll specify DEFFREG
117 :     as a span-dependent instruction whose minimum and maximum sizes are zero.
118 :    
119 :     At the moment, we do not group arithmetic operations into sequences;
120 :     that is, each arithop will be preceded by a single DEFFREG and
121 :     followed by a TRAPB. To avoid the cost of all those TRAPB's, we
122 :     should improve this when we have time. Warning: Don't put more
123 :     than 31 instructions in the sequence, because they're all required
124 :     to write to different destination registers!
125 :    
126 :     What about multiple traps? For example, suppose a sequence of
127 :     instructions produces an Overflow and a Divide-by-Zero exception?
128 :     ML would like to know only about the earliest trap, but the hardware
129 :     will report BOTH traps to the operating system. However, as long
130 :     as the rules above are followed (and the software-completion versions
131 :     of the arithmetic instructions are used), the operating system will
132 :     have enough information to know which instruction produced the
133 :     trap. It is very probable that the operating system will report ONLY
134 :     the earlier trap to the user process, but I'm not sure.
135 :    
136 :     For a hint about what the operating system is doing in its own
137 :     trap-handler (with software completion), see section 6.3.2 of
138 :     "OpenVMS Alpha Software" (Part II of the Alpha Architecture
139 :     Manual). This stuff should apply to Unix (OSF1) as well as VMS.
140 :    
141 :     ****************************************************************)
142 :    
143 :     fun error msg = MLRiscErrorMsg.error("Alpha",msg)
144 :    
145 : leunga 744 type instrStream = (I.instruction,C.cellset) T.stream
146 :     type mltreeStream = (T.stm,T.mlrisc list) T.stream
147 : monnier 409
148 :     (*
149 :     * This module is used to simulate operations of non-standard widths.
150 :     *)
151 :     structure Gen = MLTreeGen(structure T = T
152 :     val intTy = 64
153 :     val naturalWidths = [32,64]
154 : monnier 429 datatype rep = SE | ZE | NEITHER
155 :     val rep = SE
156 : monnier 409 )
157 :    
158 : leunga 744 val zeroR = C.r31
159 : monnier 409 val zeroOpn = I.REGop zeroR
160 : george 761 fun LI i = T.LI(T.I.fromInt(32, i))
161 :     fun toInt i = T.I.toInt(32, i)
162 :     val int_0 = T.I.int_0
163 :     val int_1 = T.I.int_1
164 : leunga 775 fun EQ(x:IntInf.int,y) = x=y
165 : monnier 409
166 :     (*
167 :     * Specialize the modules for multiplication/division
168 :     * by constant optimizations.
169 :     *)
170 :     functor Multiply32 = MLTreeMult
171 :     (structure I = I
172 :     structure T = T
173 : george 889 structure CB = CellsBasis
174 : monnier 409
175 :     val intTy = 32
176 :    
177 : george 889 type arg = {r1:CB.cell,r2:CB.cell,d:CB.cell}
178 :     type argi = {r:CB.cell,i:int,d:CB.cell}
179 : monnier 409
180 :     fun mov{r,d} = I.COPY{dst=[d],src=[r],tmp=NONE,impl=ref NONE}
181 :     fun add{r1,r2,d} = I.OPERATE{oper=I.ADDL,ra=r1,rb=I.REGop r2,rc=d}
182 :     (*
183 :     * How to left shift by a constant (32bits)
184 :     *)
185 :     fun slli{r,i=1,d} = [I.OPERATE{oper=I.ADDL,ra=r,rb=I.REGop r,rc=d}]
186 :     | slli{r,i=2,d} = [I.OPERATE{oper=I.S4ADDL,ra=r,rb=zeroOpn,rc=d}]
187 :     | slli{r,i=3,d} = [I.OPERATE{oper=I.S8ADDL,ra=r,rb=zeroOpn,rc=d}]
188 :     | slli{r,i,d} =
189 :     let val tmp = C.newReg()
190 :     in [I.OPERATE{oper=I.SLL,ra=r,rb=I.IMMop i,rc=tmp},
191 : leunga 646 I.OPERATE{oper=I.ADDL,ra=tmp,rb=zeroOpn,rc=d}]
192 : monnier 409 end
193 :    
194 :     (*
195 :     * How to right shift (unsigned) by a constant (32bits)
196 :     *)
197 :     fun srli{r,i,d} =
198 :     let val tmp = C.newReg()
199 :     in [I.OPERATE{oper=I.ZAP,ra=r,rb=I.IMMop 0xf0,rc=tmp},
200 :     I.OPERATE{oper=I.SRL,ra=tmp,rb=I.IMMop i,rc=d}]
201 :     end
202 :    
203 :     (*
204 :     * How to right shift (signed) by a constant (32bits)
205 :     *)
206 :     fun srai{r,i,d} =
207 :     let val tmp = C.newReg()
208 : leunga 646 in [I.OPERATE{oper=I.ADDL,ra=r,rb=zeroOpn,rc=tmp},
209 : monnier 409 I.OPERATE{oper=I.SRA,ra=tmp,rb=I.IMMop i,rc=d}]
210 :     end
211 :     )
212 :    
213 :     functor Multiply64 = MLTreeMult
214 :     (structure I = I
215 :     structure T = T
216 : george 889 structure CB = CellsBasis
217 : monnier 409
218 :     val intTy = 64
219 :    
220 : george 889 type arg = {r1:CB.cell, r2:CB.cell, d:CB.cell}
221 :     type argi = {r:CB.cell, i:int, d:CB.cell}
222 : monnier 409
223 :     fun mov{r,d} = I.COPY{dst=[d],src=[r],tmp=NONE,impl=ref NONE}
224 :     fun add{r1,r2,d}= I.OPERATE{oper=I.ADDQ,ra=r1,rb=I.REGop r2,rc=d}
225 :     fun slli{r,i,d} = [I.OPERATE{oper=I.SLL,ra=r,rb=I.IMMop i,rc=d}]
226 :     fun srli{r,i,d} = [I.OPERATE{oper=I.SRL,ra=r,rb=I.IMMop i,rc=d}]
227 :     fun srai{r,i,d} = [I.OPERATE{oper=I.SRA,ra=r,rb=I.IMMop i,rc=d}]
228 :     )
229 :    
230 :     (* signed, trapping version of multiply and divide *)
231 :     structure Mult32 = Multiply32
232 :     (val trapping = true
233 :     val multCost = multCost
234 :     fun addv{r1,r2,d} = [I.OPERATEV{oper=I.ADDLV,ra=r1,rb=I.REGop r2,rc=d}]
235 :     fun subv{r1,r2,d} = [I.OPERATEV{oper=I.SUBLV,ra=r1,rb=I.REGop r2,rc=d}]
236 :     val sh1addv = NONE
237 :     val sh2addv = NONE
238 :     val sh3addv = NONE
239 :     )
240 : monnier 429 (val signed = true)
241 : monnier 409
242 : monnier 429 (* non-trapping version of multiply and divide *)
243 :     functor Mul32 = Multiply32
244 : monnier 409 (val trapping = false
245 :     val multCost = multCost
246 :     fun addv{r1,r2,d} = [I.OPERATE{oper=I.ADDL,ra=r1,rb=I.REGop r2,rc=d}]
247 :     fun subv{r1,r2,d} = [I.OPERATE{oper=I.SUBL,ra=r1,rb=I.REGop r2,rc=d}]
248 :     val sh1addv = NONE
249 :     val sh2addv = SOME(fn {r1,r2,d} =>
250 :     [I.OPERATE{oper=I.S4ADDL,ra=r1,rb=I.REGop r2,rc=d}])
251 :     val sh3addv = SOME(fn {r1,r2,d} =>
252 :     [I.OPERATE{oper=I.S8ADDL,ra=r1,rb=I.REGop r2,rc=d}])
253 :     )
254 : monnier 429 structure Mulu32 = Mul32(val signed = false)
255 :     structure Muls32 = Mul32(val signed = true)
256 : monnier 409
257 :     (* signed, trapping version of multiply and divide *)
258 :     structure Mult64 = Multiply64
259 :     (val trapping = true
260 :     val multCost = multCost
261 :     fun addv{r1,r2,d} = [I.OPERATEV{oper=I.ADDQV,ra=r1,rb=I.REGop r2,rc=d}]
262 :     fun subv{r1,r2,d} = [I.OPERATEV{oper=I.SUBQV,ra=r1,rb=I.REGop r2,rc=d}]
263 :     val sh1addv = NONE
264 :     val sh2addv = NONE
265 :     val sh3addv = NONE
266 :     )
267 : monnier 429 (val signed = true)
268 : monnier 409
269 :     (* unsigned, non-trapping version of multiply and divide *)
270 : monnier 429 functor Mul64 = Multiply64
271 : monnier 409 (val trapping = false
272 :     val multCost = multCost
273 :     fun addv{r1,r2,d} = [I.OPERATE{oper=I.ADDQ,ra=r1,rb=I.REGop r2,rc=d}]
274 :     fun subv{r1,r2,d} = [I.OPERATE{oper=I.SUBQ,ra=r1,rb=I.REGop r2,rc=d}]
275 :     val sh1addv = NONE
276 :     val sh2addv = SOME(fn {r1,r2,d} =>
277 :     [I.OPERATE{oper=I.S4ADDQ,ra=r1,rb=I.REGop r2,rc=d}])
278 :     val sh3addv = SOME(fn {r1,r2,d} =>
279 :     [I.OPERATE{oper=I.S8ADDQ,ra=r1,rb=I.REGop r2,rc=d}])
280 :     )
281 : monnier 429 structure Mulu64 = Mul64(val signed = false)
282 :     structure Muls64 = Mul64(val signed = true)
283 : monnier 409
284 :     (*
285 :     * The main stuff
286 :     *)
287 :    
288 : george 761 datatype times4or8 = TIMES1 | TIMES4 | TIMES8
289 : monnier 409 datatype zeroOne = ZERO | ONE | OTHER
290 :     datatype commutative = COMMUTE | NOCOMMUTE
291 :    
292 : leunga 744 val zeroFR = C.f31
293 : leunga 583 val zeroEA = I.Direct zeroR
294 : george 761 val zeroT = T.LI int_0
295 : leunga 583 val trapb = [I.TRAPB]
296 :     val zeroImm = I.IMMop 0
297 :    
298 : monnier 409 fun selectInstructions
299 : george 545 (instrStream as
300 : leunga 815 S.STREAM{emit,beginCluster,endCluster,getAnnotations,
301 : monnier 429 defineLabel,entryLabel,pseudoOp,annotation,
302 : leunga 744 exitBlock,comment,...}) =
303 : monnier 409 let
304 :     infix || && << >> ~>>
305 :    
306 :     val op || = W32.orb
307 :     val op && = W32.andb
308 :     val op << = W32.<<
309 :     val op >> = W32.>>
310 :     val op ~>> = W32.~>>
311 :    
312 :     val itow = Word.fromInt
313 :     val wtoi = Word.toIntX
314 :    
315 :     val newReg = C.newReg
316 :     val newFreg = C.newFreg
317 :    
318 :     (* Choose the appropriate rounding mode to generate.
319 :     * This stuff is used to support the alpha32x SML/NJ backend.
320 : monnier 429 *
321 :     *
322 :     * Floating point rounding mode.
323 :     * When this is set to true, we use the /SU rounding mode
324 :     * (chopped towards zero) for floating point arithmetic.
325 :     * This flag is only used to support the old alpha32x backend.
326 :     *
327 :     * Otherwise, we use /SUD. This is the default for SML/NJ.
328 :     *
329 : monnier 409 *)
330 : george 545 val (ADDTX,SUBTX,MULTX,DIVTX) =
331 :     (I.ADDTSUD,I.SUBTSUD,I.MULTSUD,I.DIVTSUD)
332 :     val (ADDSX,SUBSX,MULSX,DIVSX) =
333 :     (I.ADDSSUD,I.SUBSSUD,I.MULSSUD,I.DIVSSUD)
334 : monnier 409
335 :     fun mark'(i,[]) = i
336 :     | mark'(i,a::an) = mark'(I.ANNOTATION{i=i,a=a},an)
337 :     fun mark(i,an) = emit(mark'(i,an))
338 :    
339 :     (* Fit within 16 bits? *)
340 :     fun literal16 n = ~32768 <= n andalso n < 32768
341 :     fun literal16w w =
342 :     let val hi = W32.~>>(w,0wx16)
343 :     in hi = 0w0 orelse (W32.notb hi) = 0w0 end
344 :    
345 :     (* emit an LDA instruction; return the register that holds the value *)
346 :     fun lda(base,I.IMMop 0) = base
347 :     | lda(base,offset) =
348 :     let val r = newReg()
349 :     in emit(I.LDA{r=r, b=base, d=offset}); r end
350 :    
351 :     (* emit load immed *)
352 : george 761 fun loadImmed(n, base, rd, an) = let
353 :     val n = T.I.toInt32(32, n)
354 :     in
355 :     if n = 0 then move(base, rd, an)
356 :     else if ~32768 <= n andalso n < 32768 then
357 :     mark(I.LDA{r=rd, b=base, d=I.IMMop(Int32.toInt n)}, an)
358 :     else loadImmed32(n, base, rd, an)
359 : monnier 409 end
360 :    
361 :     (* loadImmed32 is used to load int32 and word32 constants.
362 :     * In either case we sign extend the 32-bit value. This is compatible
363 :     * with LDL which sign extends a 32-bit valued memory location.
364 :     *)
365 : george 761 (* TODO:
366 :     * Should handle 64 bits if immediate is not in the 32 bit range.
367 :     *)
368 :     and loadImmed32(n, base, rd, an) = let
369 :     fun immed(0, high) =
370 :     mark(I.LDAH{r=rd, b=base, d=I.IMMop(high)}, an)
371 :     | immed(low, 0) =
372 :     mark(I.LDA{r=rd, b=base, d=I.IMMop(low)}, an)
373 :     | immed(low, high) =
374 :     (emit(I.LDA{r=rd, b=base, d=I.IMMop(low)});
375 :     mark(I.LDAH{r=rd, b=rd, d=I.IMMop(high)}, an)
376 :     )
377 :     val w = Word32.fromLargeInt(Int32.toLarge n)
378 :     val low = W32.andb(w, 0wxffff)
379 :     val high = W32.~>>(w, 0w16)
380 :     in
381 :     if W32.<(low, 0wx8000) then
382 :     immed(W32.toInt low, W32.toIntX high)
383 :     else let
384 :     val low = W32.toIntX(W32.-(low, 0wx10000))
385 :     val high = W32.toIntX(W32.+(high, 0w1))
386 : monnier 409 in
387 : george 761 if high <> 0x8000 then immed(low, high)
388 :     else let (* transition of high from pos to neg *)
389 :     val tmpR1 = newReg()
390 :     val tmpR2 = newReg()
391 :     val tmpR3=newReg()
392 :     in
393 :     (* you just gotta do, what you gotta do! *)
394 :     emit(I.LDA{r=tmpR3, b=base, d=I.IMMop(low)});
395 :     emit(I.OPERATE{oper=I.ADDQ, ra=zeroR, rb=I.IMMop 1, rc=tmpR1});
396 :     emit(I.OPERATE{oper=I.SLL, ra=tmpR1, rb=I.IMMop 31, rc=tmpR2});
397 :     mark(I.OPERATE{oper=I.ADDQ, ra=tmpR2, rb=I.REGop tmpR3, rc=rd},an)
398 :     end
399 :     end
400 :     end
401 : monnier 409
402 : george 761
403 : leunga 775 (* emit load label expression *)
404 :     and loadLabexp(le,d,an) = mark(I.LDA{r=d,b=zeroR,d=I.LABop le},an)
405 : monnier 409
406 :     (* emit a copy *)
407 : monnier 469 and copy(dst,src,an) =
408 : monnier 409 mark(I.COPY{dst=dst,src=src,impl=ref NONE,
409 :     tmp=case dst of
410 :     [_] => NONE | _ => SOME(I.Direct(newReg()))},an)
411 :    
412 :     (* emit a floating point copy *)
413 : monnier 469 and fcopy(dst,src,an) =
414 : monnier 409 mark(I.FCOPY{dst=dst,src=src,impl=ref NONE,
415 :     tmp=case dst of
416 :     [_] => NONE | _ => SOME(I.FDirect(newFreg()))},an)
417 :    
418 : monnier 469 and move(s,d,an) =
419 : george 889 if CB.sameCell(s,d) orelse CB.sameCell(d,zeroR) then () else
420 : monnier 409 mark(I.COPY{dst=[d],src=[s],impl=ref NONE,tmp=NONE},an)
421 :    
422 : monnier 469 and fmove(s,d,an) =
423 : george 889 if CB.sameCell(s,d) orelse CB.sameCell(d,zeroFR) then () else
424 : monnier 409 mark(I.FCOPY{dst=[d],src=[s],impl=ref NONE,tmp=NONE},an)
425 :    
426 :     (* emit an sign extension op *)
427 : monnier 469 and signExt32(r,d) =
428 : leunga 646 emit(I.OPERATE{oper=I.ADDL,ra=r,rb=zeroOpn,rc=d})
429 : monnier 409
430 :     (* emit an commutative arithmetic op *)
431 : monnier 469 and commArith(opcode,a,b,d,an) =
432 : monnier 409 case (opn a,opn b) of
433 :     (I.REGop r,i) => mark(I.OPERATE{oper=opcode,ra=r,rb=i,rc=d},an)
434 :     | (i,I.REGop r) => mark(I.OPERATE{oper=opcode,ra=r,rb=i,rc=d},an)
435 :     | (r,i) => mark(I.OPERATE{oper=opcode,ra=reduceOpn r,rb=i,rc=d},an)
436 :    
437 :     (* emit an arithmetic op *)
438 :     and arith(opcode,a,b,d,an) =
439 :     mark(I.OPERATE{oper=opcode,ra=expr a,rb=opn b,rc=d},an)
440 :     and arith'(opcode,a,b,d,an) =
441 :     let val rb = opn b
442 :     val ra = expr a
443 :     in mark(I.OPERATE{oper=opcode,ra=ra,rb=rb,rc=d},an) end
444 :    
445 :     (* emit a trapping commutative arithmetic op *)
446 :     and commArithTrap(opcode,a,b,d,an) =
447 :     (case (opn a,opn b) of
448 :     (I.REGop r,i) => mark(I.OPERATEV{oper=opcode,ra=r,rb=i,rc=d},an)
449 :     | (i,I.REGop r) => mark(I.OPERATEV{oper=opcode,ra=r,rb=i,rc=d},an)
450 :     | (r,i) => mark(I.OPERATEV{oper=opcode,ra=reduceOpn r,rb=i,rc=d},an);
451 :     emit(I.TRAPB)
452 :     )
453 :    
454 :     (* emit a trapping arithmetic op *)
455 :     and arithTrap(opcode,a,b,d,an) =
456 :     (mark(I.OPERATEV{oper=opcode,ra=expr a,rb=opn b,rc=d},an);
457 :     emit(I.TRAPB)
458 :     )
459 :    
460 :     (* convert an operand into a register *)
461 :     and reduceOpn(I.REGop r) = r
462 :     | reduceOpn(I.IMMop 0) = zeroR
463 :     | reduceOpn opn =
464 :     let val d = newReg()
465 :     in emit(I.OPERATE{oper=I.BIS,ra=zeroR,rb=opn,rc=d}); d end
466 :    
467 :     (* convert an expression into an operand *)
468 :     and opn(T.REG(_,r)) = I.REGop r
469 :     | opn(e as T.LI n) =
470 : leunga 775 if IntInf.<=(n, T.I.int_0xff) andalso IntInf.>=(n, T.I.int_0) then
471 : george 761 I.IMMop(toInt(n))
472 : monnier 409 else let val tmpR = newReg()
473 :     in loadImmed(n,zeroR,tmpR,[]); I.REGop tmpR end
474 : leunga 815 | opn(e as T.CONST _) = I.LABop e
475 : leunga 775 | opn(T.LABEXP x) = I.LABop x
476 : monnier 409 | opn e = I.REGop(expr e)
477 :    
478 : leunga 583 (* compute base+displacement from an expression
479 :     *)
480 : monnier 409 and addr exp =
481 : leunga 775 let fun toLexp(I.IMMop i) = T.LI(IntInf.fromInt i)
482 : leunga 583 | toLexp(I.LABop le) = le
483 :     | toLexp _ = error "addr.toLexp"
484 : monnier 409
485 : leunga 775 fun add(t,n,I.IMMop m) =
486 :     I.IMMop(toInt(T.I.ADD(t,n,IntInf.fromInt m)))
487 :     | add(t,n,I.LABop le) = I.LABop(T.ADD(t,T.LI n,le))
488 :     | add(t,n,_) = error "addr.add"
489 :    
490 :     fun addLe(ty,le,I.IMMop 0) = I.LABop le
491 :     | addLe(ty,le,disp) = I.LABop(T.ADD(ty,le,toLexp disp))
492 :    
493 :     fun sub(t,n,I.IMMop m) =
494 :     I.IMMop(toInt(T.I.SUB(t,IntInf.fromInt m,n)))
495 :     | sub(t,n,I.LABop le) = I.LABop(T.SUB(t,le,T.LI n))
496 :     | sub(t,n,_) = error "addr.sub"
497 :    
498 :     fun subLe(ty,le,I.IMMop 0) = I.LABop le
499 :     | subLe(ty,le,disp) = I.LABop(T.SUB(ty,le,toLexp disp))
500 : leunga 583
501 :     (* Should really take into account of the address width XXX *)
502 : leunga 775 fun fold(T.ADD(t,e,T.LI n),disp) = fold(e,add(t,n,disp))
503 :     | fold(T.ADD(t,e,x as T.CONST _),disp) = fold(e,addLe(t,x,disp))
504 :     | fold(T.ADD(t,e,x as T.LABEL _),disp) = fold(e,addLe(t,x,disp))
505 :     | fold(T.ADD(t,e,T.LABEXP l),disp) = fold(e,addLe(t,l,disp))
506 :     | fold(T.ADD(t,T.LI n,e),disp) = fold(e, add(t,n,disp))
507 :     | fold(T.ADD(t,x as T.CONST _,e),disp) = fold(e,addLe(t,x,disp))
508 :     | fold(T.ADD(t,x as T.LABEL _,e),disp) = fold(e,addLe(t,x,disp))
509 :     | fold(T.ADD(t,T.LABEXP l,e),disp) = fold(e,addLe(t,l,disp))
510 :     | fold(T.SUB(t,e,T.LI n),disp) = fold(e,sub(t,n,disp))
511 :     | fold(T.SUB(t,e,x as T.CONST _),disp) = fold(e,subLe(t,x,disp))
512 :     | fold(T.SUB(t,e,x as T.LABEL _),disp) = fold(e,subLe(t,x,disp))
513 :     | fold(T.SUB(t,e,T.LABEXP l),disp) = fold(e,subLe(t,l,disp))
514 : leunga 583 | fold(e,disp) = (expr e,disp)
515 :    
516 :     in makeEA(fold(exp, zeroImm))
517 :     end
518 :    
519 : monnier 409 (* compute base+displacement+small offset *)
520 :     and offset(base,disp as I.IMMop n,off) =
521 :     let val n' = n+off
522 :     in if literal16 n' then (base,I.IMMop n')
523 :     else
524 :     let val tmp = newReg()
525 :     in emit(I.OPERATE{oper=I.ADDQ,ra=base,rb=disp,rc=tmp});
526 :     (tmp,I.IMMop off)
527 :     end
528 :     end
529 : leunga 583 | offset(base,disp as I.LABop le,off) =
530 : leunga 775 (base, I.LABop(T.ADD(64,le,T.LI(IntInf.fromInt off))))
531 : monnier 409 | offset(base,disp,off) =
532 :     let val tmp = newReg()
533 :     in emit(I.OPERATE{oper=I.ADDQ,ra=base,rb=disp,rc=tmp});
534 :     (tmp,I.IMMop off)
535 :     end
536 :    
537 : leunga 583 (* check if base offset fits within the field *)
538 :     and makeEA(base, off as I.IMMop offset) =
539 :     if ~32768 <= offset andalso offset <= 32767
540 :     then (base, off)
541 : monnier 409 else
542 :     let val tmpR = newReg()
543 :     (* unsigned low 16 bits *)
544 :     val low = wtoi(Word.andb(itow offset, 0wxffff))
545 :     val high = offset div 65536
546 :     val (lowsgn, highsgn) = (* Sign-extend *)
547 :     if low <= 32767 then (low, high) else (low -65536, high+1)
548 :     in
549 :     (emit(I.LDAH{r=tmpR, b=base, d=I.IMMop highsgn});
550 :     (tmpR, I.IMMop lowsgn))
551 :     end
552 : leunga 583 | makeEA(base, offset) = (base, offset)
553 : monnier 409
554 :     (* look for multiply by 4 and 8 of the given type *)
555 :     and times4or8(ty,e) =
556 : george 761 let
557 :     fun f(t,a,n) = if t = ty then
558 :     if EQ(n, T.I.int_4) then (TIMES4,a)
559 :     else if EQ(n, T.I.int_8) then (TIMES8,a)
560 : monnier 409 else (TIMES1,e)
561 :     else (TIMES1,e)
562 : george 761
563 : monnier 409 fun u(t,a,n) = if t = ty then
564 : george 761 if EQ(n, T.I.int_2) then (TIMES4,a)
565 :     else if EQ(n, T.I.int_3) then (TIMES8,a)
566 : monnier 409 else (TIMES1,e)
567 :     else (TIMES1,e)
568 :     in case e of
569 :     T.MULU(t,a,T.LI n) => f(t,a,n)
570 :     | T.MULS(t,T.LI n,a) => f(t,a,n)
571 :     | T.SLL(t,a,T.LI n) => u(t,a,n)
572 :     | _ => (TIMES1,e)
573 :     end
574 :    
575 :     (* generate an add instruction
576 :     * ***OPT*** look for multiply by 4 and 8 and use the S4ADD and S8ADD
577 :     * forms.
578 :     *)
579 :     and plus(ty,add,s4add,s8add,a,b,d,an) =
580 :     (case times4or8(ty,a) of
581 :     (TIMES4,a) => arith(s4add,a,b,d,an)
582 :     | (TIMES8,a) => arith(s8add,a,b,d,an)
583 :     | _ =>
584 :     case times4or8(ty,b) of
585 :     (TIMES4,b) => arith'(s4add,b,a,d,an)
586 :     | (TIMES8,b) => arith'(s8add,b,a,d,an)
587 :     | _ => commArith(add,a,b,d,an)
588 :     )
589 :    
590 :     (* generate a subtract instruction
591 :     * ***OPT*** look for multiply by 4 and 8
592 :     *)
593 :     and minus(ty,sub,s4sub,s8sub,a,b,d,an) =
594 :     (case times4or8(ty,a) of
595 :     (TIMES4,a) => arith(s4sub,a,b,d,an)
596 :     | (TIMES8,a) => arith(s8sub,a,b,d,an)
597 :     | _ =>
598 : monnier 429 if ty = 64 then
599 : monnier 409 (case b of
600 :     (* use LDA to handle subtraction when possible
601 :     * Note: this may have sign extension problems later.
602 :     *)
603 : george 761 T.LI i => (loadImmed(T.I.NEGT(32,i),expr a,d,an) handle _ =>
604 : monnier 409 arith(sub,a,b,d,an))
605 :     | _ => arith(sub,a,b,d,an)
606 : monnier 429 ) else arith(sub,a,b,d,an)
607 : monnier 409 )
608 :    
609 :     (* look for special constants *)
610 : george 761 and wordOpn(T.LI n) = SOME(T.I.toWord32(32, n))
611 : monnier 409 | wordOpn e = NONE
612 :    
613 : monnier 429 (* look for special byte mask constants
614 :     * IMPORTANT: we must ALWAYS keep the sign bit!
615 :     *)
616 :     and byteMask(_,SOME 0wx00000000) = 0xff
617 :     | byteMask(_,SOME 0wx000000ff) = 0xfe
618 :     | byteMask(_,SOME 0wx0000ff00) = 0xfd
619 :     | byteMask(_,SOME 0wx0000ffff) = 0xfc
620 :     | byteMask(_,SOME 0wx00ff0000) = 0xfb
621 :     | byteMask(_,SOME 0wx00ff00ff) = 0xfa
622 :     | byteMask(_,SOME 0wx00ffff00) = 0xf9
623 :     | byteMask(_,SOME 0wx00ffffff) = 0xf8
624 :     | byteMask(ty,SOME 0wxff000000) = if ty = 64 then 0xf7 else 0x07
625 :     | byteMask(ty,SOME 0wxff0000ff) = if ty = 64 then 0xf6 else 0x06
626 :     | byteMask(ty,SOME 0wxff00ff00) = if ty = 64 then 0xf5 else 0x05
627 :     | byteMask(ty,SOME 0wxff00ffff) = if ty = 64 then 0xf4 else 0x04
628 :     | byteMask(ty,SOME 0wxffff0000) = if ty = 64 then 0xf3 else 0x03
629 :     | byteMask(ty,SOME 0wxffff00ff) = if ty = 64 then 0xf2 else 0x02
630 :     | byteMask(ty,SOME 0wxffffff00) = if ty = 64 then 0xf1 else 0x01
631 :     | byteMask(ty,SOME 0wxffffffff) = if ty = 64 then 0xf0 else 0x00
632 :     | byteMask _ = ~1
633 : monnier 409
634 :     (* generate an and instruction
635 :     * look for special masks.
636 :     *)
637 :     and andb(ty,a,b,d,an) =
638 :     case byteMask(ty,wordOpn a) of
639 : monnier 429 ~1 => (case byteMask(ty,wordOpn b) of
640 :     ~1 => commArith(I.AND,a,b,d,an)
641 : george 761 | mask => arith(I.ZAP,a,LI mask,d,an)
642 : monnier 429 )
643 : george 761 | mask => arith(I.ZAP,b,LI mask,d,an)
644 : monnier 409
645 :     (* generate sll/sra/srl *)
646 :     and sll32(a,b,d,an) =
647 :     case wordOpn b of
648 :     SOME 0w0 => doExpr(a,d,an)
649 :     | SOME 0w1 =>
650 :     let val r = T.REG(32,expr a) in arith(I.ADDL,r,r,d,an) end
651 :     | SOME 0w2 => arith(I.S4ADDL,a,zeroT,d,an)
652 :     | SOME 0w3 => arith(I.S8ADDL,a,zeroT,d,an)
653 :     | _ => let val t = newReg()
654 :     in arith(I.SLL,a,b,t,an);
655 :     signExt32(t,d)
656 :     end
657 :    
658 :     and sll64(a,b,d,an) =
659 :     case wordOpn b of
660 :     SOME 0w0 => doExpr(a,d,an)
661 :     | SOME 0w1 =>
662 :     let val r = T.REG(64,expr a) in arith(I.ADDQ,r,r,d,an) end
663 :     | SOME 0w2 => arith(I.S4ADDQ,a,zeroT,d,an)
664 :     | SOME 0w3 => arith(I.S8ADDQ,a,zeroT,d,an)
665 :     | _ => arith(I.SLL,a,b,d,an)
666 :    
667 : leunga 796 (* On the alpha, all 32 bit values are already sign extended.
668 :     * So no sign extension is necessary. We do the same for
669 :     * sra32 and sra64
670 :     *)
671 :     and sra(a,b,d,an) =
672 : monnier 409 mark(I.OPERATE{oper=I.SRA,ra=expr a,rb=opn b,rc=d},an)
673 :    
674 :     and srl32(a,b,d,an) =
675 :     let val ra = expr a
676 :     val rb = opn b
677 :     val t = newReg()
678 :     in emit(I.OPERATE{oper=I.ZAP,ra=ra,rb=I.IMMop 0xf0,rc=t});
679 :     mark(I.OPERATE{oper=I.SRL,ra=t,rb=rb,rc=d},an)
680 :     end
681 :    
682 :     and srl64(a,b,d,an) =
683 :     mark(I.OPERATE{oper=I.SRL,ra=expr a,rb=opn b,rc=d},an)
684 :    
685 :     (*
686 :     * Generic multiply.
687 :     * We first try to use the multiply by constant heuristic
688 :     *)
689 :     and multiply(ty,gen,genConst,e1,e2,rd,trapb,an) =
690 :     let fun nonconst(e1,e2) =
691 :     let val instr =
692 :     case (opn e1,opn e2) of
693 :     (i,I.REGop r) => gen{ra=r,rb=i,rc=rd}
694 :     | (I.REGop r,i) => gen{ra=r,rb=i,rc=rd}
695 :     | (r,i) => gen{ra=reduceOpn r,rb=i,rc=rd}
696 :     in mark'(instr,an)::trapb end
697 :     fun const(e,i) =
698 :     let val r = expr e
699 : george 761 in if !useMultByConst andalso
700 : leunga 775 IntInf.>=(i, T.I.int_0) andalso
701 :     IntInf.<(i, T.I.int_0x100) then
702 : george 761 mark'(gen{ra=r,rb=I.IMMop(toInt i),rc=rd},an)::trapb
703 : monnier 409 else
704 : george 761 (genConst{r=r,i=toInt i,d=rd}@trapb
705 : monnier 409 handle _ => nonconst(T.REG(ty,r),T.LI i))
706 :     end
707 :     val instrs =
708 : george 761 case (e1, e2)
709 :     of (_, T.LI i) => const(e1, i)
710 :     | (T.LI i, _) => const(e2, i)
711 :     | _ => nonconst(e1, e2)
712 : monnier 409 in app emit instrs
713 :     end
714 :    
715 :     (* Round r towards zero.
716 :     * I generate the following sequence of code, which should get
717 :     * mapped into conditional moves.
718 :     *
719 :     * d <- r + i;
720 :     * d <- if (r > 0) then r else d
721 :     *)
722 : george 545 (*
723 : monnier 409 and roundToZero{ty,r,i,d} =
724 :     (doStmt(T.MV(ty,d,T.ADD(ty,T.REG(ty,r),T.LI i)));
725 :     doStmt(T.MV(ty,d,T.COND(ty,T.CMP(ty,T.GE,T.REG(ty,r),T.LI 0),
726 :     T.REG(ty,r),T.REG(ty,d))))
727 :     )
728 : george 545 *)
729 : monnier 409
730 :     (*
731 :     * Generic division.
732 :     * We first try to use the division by constant heuristic
733 :     *)
734 :     and divide(ty,pseudo,genDiv,e1,e2,rd,an) =
735 :     let fun nonconst(e1,e2) =
736 :     pseudo({ra=expr e1,rb=opn e2,rc=rd},reduceOpn)
737 :    
738 :     fun const(e,i) =
739 :     let val r = expr e
740 : george 545 in genDiv{mode=T.TO_ZERO,stm=doStmt}
741 : george 761 {r=r,i=toInt i,d=rd}
742 : monnier 409 handle _ => nonconst(T.REG(ty,r),T.LI i)
743 :     end
744 :     val instrs =
745 :     case e2 of
746 :     T.LI i => const(e1,i)
747 :     | _ => nonconst(e1,e2)
748 :     in app emit instrs
749 :     end
750 :    
751 :    
752 :     (*
753 :     and multTrap(MULV,ADD,ADDV,e1,e2,rd,an) = (* signed multiply and trap *)
754 :     let val ADD = fn {ra,rb,rc} => I.OPERATE{oper=ADD,ra=ra,rb=rb,rc=rc}
755 :     val ADDV = fn {ra,rb,rc} => I.OPERATEV{oper=ADDV,ra=ra,rb=rb,rc=rc}
756 :     val MULV = fn {ra,rb,rc} => I.OPERATEV{oper=MULV,ra=ra,rb=rb,rc=rc}
757 :     in multiply(MULV,ADD,ADDV,e1,e2,rd,an);
758 :     emit(I.TRAPB)
759 :     end
760 :    
761 :     and mulu(MUL,ADD,e1,e2,rd,an) = (* unsigned multiply *)
762 :     let val ADD = fn {ra,rb,rc} => I.OPERATE{oper=ADD,ra=ra,rb=rb,rc=rc}
763 :     val MUL = fn {ra,rb,rc} => I.OPERATE{oper=MUL,ra=ra,rb=rb,rc=rc}
764 :     in multiply(MUL,ADD,ADD,e1,e2,rd,an)
765 :     end
766 :    
767 :     (* Multiplication *)
768 :     and multiply(MULV, ADD, ADDV, e1, e2, rd, an) =
769 :     let val reg = expr e1
770 :     val opn = opn e2
771 :     fun emitMulvImmed (reg, 0, rd) =
772 :     emit(I.LDA{r=rd, b=zeroR, d=I.IMMop 0})
773 :     | emitMulvImmed (reg, 1, rd) =
774 :     emit(ADD{ra=reg, rb=zeroOpn, rc=rd})
775 :     | emitMulvImmed (reg, multiplier, rd) =
776 :     let fun log2 0w1 = 0 | log2 n = 1 + (log2 (Word.>> (n, 0w1)))
777 :     fun exp2 n = Word.<<(0w1, n)
778 :     fun bitIsSet (x,n) = Word.andb(x,exp2 n) <> 0w0
779 :     fun loop (~1) = ()
780 :     | loop n =
781 :     (if bitIsSet(itow multiplier, itow n) then
782 :     emit(ADDV{ra=reg,rb=I.REGop rd,rc=rd})
783 :     else ();
784 :     if n>0 then
785 :     emit(ADDV{ra=rd,rb=I.REGop rd,rc=rd})
786 :     else ();
787 :     loop (n-1))
788 :     in emit(ADDV{ra=reg, rb=I.REGop reg, rc=rd});
789 :     loop ((log2 (itow multiplier)) - 1)
790 :     end
791 :     in case opn of
792 :     (I.IMMop multiplier) => emitMulvImmed (reg, multiplier, rd)
793 :     | _ => mark(MULV{ra=reg, rb=opn, rc=rd},an)
794 :     (*esac*)
795 :     end
796 :     *)
797 :    
798 :     (* generate pseudo instruction *)
799 :     and pseudo(instr,e1,e2,rc) =
800 :     app emit (instr({ra=expr e1,rb=opn e2,rc=rc}, reduceOpn))
801 :    
802 :     (* generate a load *)
803 :     and load(ldOp,ea,d,mem,an) =
804 :     let val (base,disp) = addr ea
805 :     in mark(I.LOAD{ldOp=ldOp,r=d,b=base,d=disp,mem=mem},an) end
806 :    
807 :     (* generate a load with zero extension *)
808 :     and loadZext(ea,rd,mem,EXT,an) =
809 :     let val (b, d) = addr ea
810 :     val t1 = newReg()
811 :     val _ = mark(I.LOAD{ldOp=I.LDQ_U, r=t1, b=b, d=d, mem=mem},an);
812 :     val t2 = lda(b,d)
813 :     in emit(I.OPERATE{oper=EXT, ra=t1, rb=I.REGop t2, rc=rd}) end
814 :    
815 :     (* generate a load with sign extension *)
816 :     and loadSext(ea,rd,mem,off,EXT,shift,an) =
817 :     let val (b, d) = addr ea
818 :     val (b',d') = offset(b,d,off)
819 :     val t1 = newReg()
820 :     val t2 = newReg()
821 :     val t3 = newReg()
822 :     in mark(I.LOAD{ldOp=I.LDQ_U, r=t1, b=b, d=d, mem=mem},an);
823 :     emit(I.LDA{r=t2, b=b', d=d'});
824 :     emit(I.OPERATE{oper=EXT, ra=t1, rb=I.REGop t2, rc=t3});
825 :     emit(I.OPERATE{oper=I.SRA, ra=t3, rb=I.IMMop shift, rc=rd})
826 :     end
827 :    
828 :     (* generate a load byte with zero extension (page 4-48) *)
829 : leunga 583 and load8(ea,rd,mem,an) =
830 :     if !byteWordLoadStores then load(I.LDBU,ea,rd,mem,an)
831 :     else loadZext(ea,rd,mem,I.EXTBL,an)
832 : monnier 409
833 :     (* generate a load byte with sign extension (page 4-48) *)
834 : leunga 583 and load8s(ea,rd,mem,an) =
835 : leunga 585 if !byteWordLoadStores then load(I.LDB,ea,rd,mem,an)
836 : leunga 583 else loadSext(ea,rd,mem,1,I.EXTQH,56,an)
837 : monnier 409
838 :     (* generate a load 16 bit *)
839 : leunga 583 and load16(ea,rd,mem,an) =
840 :     if !byteWordLoadStores then load(I.LDWU,ea,rd,mem,an)
841 :     else loadZext(ea,rd,mem,I.EXTWL,an)
842 : monnier 409
843 :     (* generate a load 16 bit with sign extension *)
844 : leunga 583 and load16s(ea,rd,mem,an) =
845 : leunga 585 if !byteWordLoadStores then load(I.LDW,ea,rd,mem,an)
846 : leunga 583 else loadSext(ea,rd,mem,2,I.EXTQH,48,an)
847 : monnier 409
848 :     (* generate a load 32 bit with sign extension *)
849 :     and load32s(ea,rd,mem,an) = load(I.LDL,ea,rd,mem,an)
850 :    
851 :     (* generate a floating point load *)
852 :     and fload(ldOp,ea,d,mem,an) =
853 :     let val (base,disp) = addr ea
854 :     in mark(I.FLOAD{ldOp=ldOp,r=d,b=base,d=disp,mem=mem},an) end
855 :    
856 :     (* generate a store *)
857 :     and store(stOp,ea,data,mem,an) =
858 :     let val (base,disp) = addr ea
859 :     in mark(I.STORE{stOp=stOp,r=expr data,b=base,d=disp,mem=mem},an) end
860 :    
861 :     (* generate an store8 or store16 *)
862 :     and storeUnaligned(ea,data,mem,INS,MSK,an) =
863 :     let val (base,disp) = addr ea
864 :     val data = expr data
865 :     val t1 = newReg()
866 :     val t3 = newReg()
867 :     val t4 = newReg()
868 :     val t5 = newReg()
869 :     val _ = emit(I.LOAD{ldOp=I.LDQ_U, r=t1, b=base, d=disp, mem=mem})
870 :     val t2 = lda(base,disp)
871 :     in emit(I.OPERATE{oper=INS, ra=data, rb=I.REGop(t2), rc=t3});
872 :     emit(I.OPERATE{oper=MSK, ra=t1, rb=I.REGop(t2), rc=t4});
873 :     emit(I.OPERATE{oper=I.BIS, ra=t4, rb=I.REGop(t3), rc=t5});
874 :     mark(I.STORE{stOp=I.STQ_U, r=t5, b=base, d=disp, mem=mem},an)
875 :     end
876 :    
877 :     (* generate a store byte *)
878 :     and store8(ea,data,mem,an) =
879 : leunga 585 if !byteWordLoadStores then store(I.STB, ea, data, mem, an)
880 :     else storeUnaligned(ea,data,mem,I.INSBL,I.MSKBL,an)
881 : monnier 409
882 :     (* generate a store16 *)
883 :     and store16(ea,data,mem,an) =
884 : leunga 585 if !byteWordLoadStores then store(I.STW, ea, data, mem, an)
885 :     else storeUnaligned(ea,data,mem,I.INSWL,I.MSKWL,an)
886 : monnier 409
887 : monnier 429 (* generate conversion from floating point to integer *)
888 :     and cvtf2i(pseudo,rounding,e,rd,an) =
889 :     app emit (pseudo{mode=rounding, fs=fexpr e, rd=rd})
890 :    
891 : monnier 409 (* generate an expression and return the register that holds the result *)
892 : george 761 and expr(e) = let
893 :     fun comp() = let
894 :     val r = newReg()
895 :     in doExpr(e, r, []); r
896 :     end
897 :     in
898 :     case e
899 :     of T.REG(_, r) => r
900 :     | T.LI z => if T.I.isZero(z) then zeroR else comp()
901 : leunga 624 (* On the alpha: all 32 bit values are already sign extended.
902 :     * So no sign extension is necessary
903 :     *)
904 : george 761 | T.SX(64, 32, e) => expr e
905 :     | T.ZX(64, 32, e) => expr e
906 :     | _ => comp()
907 :     end
908 : leunga 624
909 : monnier 409 (* generate an expression that targets register d *)
910 : monnier 429 and doExpr(exp,d,an) =
911 :     case exp of
912 : monnier 409 T.REG(_,r) => move(r,d,an)
913 :     | T.LI n => loadImmed(n,zeroR,d,an)
914 : leunga 775 | T.LABEL l => loadLabexp(exp,d,an)
915 :     | T.CONST c => loadLabexp(exp,d,an)
916 :     | T.LABEXP le => loadLabexp(le,d,an)
917 : monnier 409
918 :     (* special optimizations for additions and subtraction
919 :     * Question: using LDA for all widths is not really correct
920 :     * since the result may not fit into the sign extension scheme.
921 :     *)
922 : leunga 775 | T.ADD(64,e,T.LABEXP le) => mark(I.LDA{r=d,b=expr e,d=I.LABop le},an)
923 :     | T.ADD(64,T.LABEXP le,e) => mark(I.LDA{r=d,b=expr e,d=I.LABop le},an)
924 :     | T.ADD(64,e,x as (T.CONST _ | T.LABEL _)) =>
925 :     mark(I.LDA{r=d,b=expr e,d=I.LABop x},an)
926 :     | T.ADD(64,x as (T.CONST _ | T.LABEL _),e) =>
927 :     mark(I.LDA{r=d,b=expr e,d=I.LABop x},an)
928 : monnier 429 | T.ADD(64,e,T.LI i) => loadImmed(i, expr e, d, an)
929 :     | T.ADD(64,T.LI i,e) => loadImmed(i, expr e, d, an)
930 : george 761 | T.SUB(sz, a, b as T.LI z) =>
931 :     if T.I.isZero(z) then
932 :     doExpr(a,d,an)
933 :     else (case sz
934 :     of 32 => minus(32,I.SUBL,I.S4SUBL,I.S8SUBL,a,b,d,an)
935 :     | 64 => minus(64,I.SUBQ,I.S4SUBQ,I.S8SUBQ,a,b,d,an)
936 :     | _ => doExpr(Gen.compileRexp exp,d,an)
937 :     (*esac*))
938 : monnier 409
939 :     (* 32-bit support *)
940 :     | T.ADD(32,a,b) => plus(32,I.ADDL,I.S4ADDL,I.S8ADDL,a,b,d,an)
941 :     | T.SUB(32,a,b) => minus(32,I.SUBL,I.S4SUBL,I.S8SUBL,a,b,d,an)
942 :     | T.ADDT(32,a,b) => commArithTrap(I.ADDLV,a,b,d,an)
943 :     | T.SUBT(32,a,b) => arithTrap(I.SUBLV,a,b,d,an)
944 : monnier 429 | T.MULT(32,a,b) =>
945 : monnier 409 multiply(32,
946 :     fn{ra,rb,rc} => I.OPERATEV{oper=I.MULLV,ra=ra,rb=rb,rc=rc},
947 :     Mult32.multiply,a,b,d,trapb,an)
948 : monnier 429 | T.MULU(32,a,b) =>
949 : monnier 409 multiply(32,
950 :     fn{ra,rb,rc} => I.OPERATE{oper=I.MULL,ra=ra,rb=rb,rc=rc},
951 :     Mulu32.multiply,a,b,d,[],an)
952 : monnier 429 | T.MULS(32,a,b) =>
953 :     multiply(32,
954 :     fn{ra,rb,rc} => I.OPERATE{oper=I.MULL,ra=ra,rb=rb,rc=rc},
955 :     Muls32.multiply,a,b,d,[],an)
956 :     | T.DIVT(32,a,b) => divide(32,P.divlv,Mult32.divide,a,b,d,an)
957 :     | T.DIVU(32,a,b) => divide(32,P.divlu,Mulu32.divide,a,b,d,an)
958 :     | T.DIVS(32,a,b) => divide(32,P.divl,Muls32.divide,a,b,d,an)
959 :     | T.REMT(32,a,b) => pseudo(P.remlv,a,b,d)
960 :     | T.REMU(32,a,b) => pseudo(P.remlu,a,b,d)
961 :     | T.REMS(32,a,b) => pseudo(P.reml,a,b,d)
962 :    
963 : monnier 409 | T.SLL(32,a,b) => sll32(a,b,d,an)
964 : leunga 796 | T.SRA(32,a,b) => sra(a,b,d,an)
965 : monnier 409 | T.SRL(32,a,b) => srl32(a,b,d,an)
966 :    
967 :     (* 64 bit support *)
968 :     | T.ADD(64,a,b) => plus(64,I.ADDQ,I.S4ADDQ,I.S8ADDQ,a,b,d,an)
969 :     | T.SUB(64,a,b) => minus(64,I.SUBQ,I.S4SUBQ,I.S8SUBQ,a,b,d,an)
970 :     | T.ADDT(64,a,b) => commArithTrap(I.ADDQV,a,b,d,an)
971 :     | T.SUBT(64,a,b) => arithTrap(I.SUBQV,a,b,d,an)
972 : monnier 429 | T.MULT(64,a,b) =>
973 : monnier 409 multiply(64,
974 :     fn{ra,rb,rc} => I.OPERATEV{oper=I.MULQV,ra=ra,rb=rb,rc=rc},
975 :     Mult64.multiply,a,b,d,trapb,an)
976 : monnier 429 | T.MULU(64,a,b) =>
977 : monnier 409 multiply(64,
978 :     fn{ra,rb,rc} => I.OPERATE{oper=I.MULQ,ra=ra,rb=rb,rc=rc},
979 :     Mulu64.multiply,a,b,d,[],an)
980 : monnier 429 | T.MULS(64,a,b) =>
981 :     multiply(64,
982 :     fn{ra,rb,rc} => I.OPERATE{oper=I.MULQ,ra=ra,rb=rb,rc=rc},
983 :     Muls64.multiply,a,b,d,[],an)
984 :     | T.DIVT(64,a,b) => divide(64,P.divqv,Mult64.divide,a,b,d,an)
985 :     | T.DIVU(64,a,b) => divide(64,P.divqu,Mulu64.divide,a,b,d,an)
986 :     | T.DIVS(64,a,b) => divide(64,P.divq,Muls64.divide,a,b,d,an)
987 :     | T.REMT(64,a,b) => pseudo(P.remqv,a,b,d)
988 :     | T.REMU(64,a,b) => pseudo(P.remqu,a,b,d)
989 :     | T.REMS(64,a,b) => pseudo(P.remq,a,b,d)
990 :    
991 : monnier 409 | T.SLL(64,a,b) => sll64(a,b,d,an)
992 : leunga 796 | T.SRA(64,a,b) => sra(a,b,d,an)
993 : monnier 409 | T.SRL(64,a,b) => srl64(a,b,d,an)
994 :    
995 :     (* special bit operations with complement *)
996 :     | T.ANDB(_,a,T.NOTB(_,b)) => arith(I.BIC,a,b,d,an)
997 :     | T.ORB(_,a,T.NOTB(_,b)) => arith(I.ORNOT,a,b,d,an)
998 :     | T.XORB(_,a,T.NOTB(_,b)) => commArith(I.EQV,a,b,d,an)
999 :     | T.ANDB(_,T.NOTB(_,a),b) => arith(I.BIC,b,a,d,an)
1000 :     | T.ORB(_,T.NOTB(_,a),b) => arith(I.ORNOT,b,a,d,an)
1001 :     | T.XORB(_,T.NOTB(_,a),b) => commArith(I.EQV,b,a,d,an)
1002 :     | T.NOTB(_,T.XORB(_,a,b)) => commArith(I.EQV,b,a,d,an)
1003 :    
1004 :     (* bit operations *)
1005 :     | T.ANDB(ty,a,b) => andb(ty,a,b,d,an)
1006 :     | T.XORB(_,a,b) => commArith(I.XOR,a,b,d,an)
1007 :     | T.ORB(_,a,b) => commArith(I.BIS,a,b,d,an)
1008 :     | T.NOTB(_,e) => arith(I.ORNOT,zeroT,e,d,an)
1009 :    
1010 :     (* loads *)
1011 : leunga 744 | T.SX(_,_,T.LOAD(8,ea,mem)) => load8s(ea,d,mem,an)
1012 :     | T.SX(_,_,T.LOAD(16,ea,mem))=> load16s(ea,d,mem,an)
1013 :     | T.SX(_,_,T.LOAD(32,ea,mem))=> load32s(ea,d,mem,an)
1014 :     | T.ZX((8|16|32|64),_,T.LOAD(8,ea,mem)) => load8(ea,d,mem,an)
1015 :     | T.ZX((16|32|64),_,T.LOAD(16,ea,mem))=> load16(ea,d,mem,an)
1016 :     | T.ZX(64,_,T.LOAD(64,ea,mem)) => load(I.LDQ,ea,d,mem,an)
1017 : monnier 409 | T.LOAD(8,ea,mem) => load8(ea,d,mem,an)
1018 :     | T.LOAD(16,ea,mem) => load16(ea,d,mem,an)
1019 : monnier 429 | T.LOAD(32,ea,mem) => load32s(ea,d,mem,an)
1020 : monnier 409 | T.LOAD(64,ea,mem) => load(I.LDQ,ea,d,mem,an)
1021 :    
1022 : monnier 429 (* floating -> int conversion *)
1023 : monnier 475 | T.CVTF2I(ty,rounding,fty,e) =>
1024 :     (case (fty,ty) of
1025 : monnier 429 (32,32) => cvtf2i(P.cvtsl,rounding,e,d,an)
1026 :     | (32,64) => cvtf2i(P.cvtsq,rounding,e,d,an)
1027 :     | (64,32) => cvtf2i(P.cvttl,rounding,e,d,an)
1028 :     | (64,64) => cvtf2i(P.cvttq,rounding,e,d,an)
1029 : george 545 | _ => doExpr(Gen.compileRexp exp,d,an) (* other cases *)
1030 : monnier 429 )
1031 :    
1032 : monnier 409 (* conversion to boolean *)
1033 : george 761 | T.COND(_, T.CMP(ty,cond,e1,e2), x, y) =>
1034 :     (case (x, y)
1035 :     of (T.LI n, T.LI m) =>
1036 :     if EQ(n, int_1) andalso EQ(m, int_0) then
1037 :     compare(ty,cond,e1,e2,d,an)
1038 :     else if EQ(n, int_0) andalso EQ(m, int_1) then
1039 :     compare(ty,T.Basis.negateCond cond,e1,e2,d,an)
1040 :     else
1041 :     cmove(ty,cond,e1,e2,x,y,d,an)
1042 :     | _ => cmove(ty,cond,e1,e2,x,y,d,an)
1043 :     (*esac*))
1044 : monnier 409
1045 : george 545 | T.LET(s,e) => (doStmt s; doExpr(e, d, an))
1046 :     | T.MARK(e,A.MARKREG f) => (f d; doExpr(e,d,an))
1047 :     | T.MARK(e,a) => doExpr(e,d,a::an)
1048 : monnier 475 (* On the alpha: all 32 bit values are already sign extended.
1049 :     * So no sign extension is necessary
1050 :     *)
1051 : leunga 744 | T.SX(64, 32, e) => doExpr(e, d, an)
1052 :     | T.ZX(64, 32, e) => doExpr(e, d, an)
1053 : george 545
1054 :     | T.PRED(e, c) => doExpr(e, d, A.CTRLUSE c::an)
1055 : george 555 | T.REXT e => ExtensionComp.compileRext (reducer()) {e=e, an=an, rd=d}
1056 : monnier 429
1057 :     (* Defaults *)
1058 : george 545 | e => doExpr(Gen.compileRexp e,d,an)
1059 : monnier 409
1060 :     (* Hmmm... this is the funky thing described in the comments
1061 :     * in at the top of the file. This should be made parametrizable
1062 :     * for other backends.
1063 :     *)
1064 : george 545 and farith(opcode,opcodeSMLNJ,a,b,d,an) =
1065 : monnier 409 let val fa = fexpr a
1066 :     val fb = fexpr b
1067 : george 545 in if SMLNJfloatingPoint then
1068 :     (emit(I.DEFFREG d);
1069 :     mark(I.FOPERATEV{oper=opcodeSMLNJ,fa=fa,fb=fb,fc=d},an);
1070 :     emit(I.TRAPB)
1071 :     )
1072 :     else mark(I.FOPERATE{oper=opcode,fa=fa,fb=fb,fc=d},an)
1073 : monnier 409 end
1074 :    
1075 : george 545 and farith'(opcode,a,b,d,an) =
1076 :     mark(I.FOPERATE{oper=opcode,fa=fexpr a,fb=fexpr b,fc=d},an)
1077 :    
1078 : monnier 429 and funary(opcode,e,d,an) = mark(I.FUNARY{oper=opcode,fb=fexpr e,fc=d},an)
1079 :    
1080 :    
1081 : monnier 409 (* generate an floating point expression
1082 :     * return the register that holds the result
1083 :     *)
1084 :     and fexpr(T.FREG(_,r)) = r
1085 :     | fexpr e = let val d = newFreg() in doFexpr(e,d,[]); d end
1086 :    
1087 :     (* generate an external floating point operation *)
1088 : monnier 429 and fcvti2f(pseudo,e,fd,an) =
1089 : monnier 409 let val opnd = opn e
1090 : monnier 429 in app emit (pseudo({opnd=opnd, fd=fd}, reduceOpn))
1091 : monnier 409 end
1092 :    
1093 :     (* generate a floating point store *)
1094 :     and fstore(stOp,ea,data,mem,an) =
1095 :     let val (base,disp) = addr ea
1096 :     in mark(I.FSTORE{stOp=stOp,r=fexpr data,b=base,d=disp,mem=mem},an)
1097 :     end
1098 :    
1099 :     (* generate a floating point expression that targets register d *)
1100 :     and doFexpr(e,d,an) =
1101 :     case e of
1102 :     T.FREG(_,f) => fmove(f,d,an)
1103 :    
1104 :     (* single precision support *)
1105 : george 545 | T.FADD(32,a,b) => farith(I.ADDS,ADDSX,a,b,d,an)
1106 :     | T.FSUB(32,a,b) => farith(I.SUBS,SUBSX,a,b,d,an)
1107 :     | T.FMUL(32,a,b) => farith(I.MULS,MULSX,a,b,d,an)
1108 :     | T.FDIV(32,a,b) => farith(I.DIVS,DIVSX,a,b,d,an)
1109 : monnier 409
1110 :     (* double precision support *)
1111 : george 545 | T.FADD(64,a,b) => farith(I.ADDT,ADDTX,a,b,d,an)
1112 :     | T.FSUB(64,a,b) => farith(I.SUBT,SUBTX,a,b,d,an)
1113 :     | T.FMUL(64,a,b) => farith(I.MULT,MULTX,a,b,d,an)
1114 :     | T.FDIV(64,a,b) => farith(I.DIVT,DIVTX,a,b,d,an)
1115 : monnier 409
1116 : george 545 (* copy sign (correct?) XXX *)
1117 :     | T.FCOPYSIGN(_,T.FNEG(_,a),b) => farith'(I.CPYSN,a,b,d,an)
1118 :     | T.FCOPYSIGN(_,a,T.FNEG(_,b)) => farith'(I.CPYSN,a,b,d,an)
1119 :     | T.FNEG(_,T.FCOPYSIGN(_,a,b)) => farith'(I.CPYSN,a,b,d,an)
1120 :     | T.FCOPYSIGN(_,a,b) => farith'(I.CPYS,a,b,d,an)
1121 : monnier 429
1122 : monnier 409 (* generic *)
1123 :     | T.FABS(_,a) =>
1124 :     mark(I.FOPERATE{oper=I.CPYS,fa=zeroFR,fb=fexpr a,fc=d},an)
1125 :     | T.FNEG(_,a) =>
1126 :     let val fs = fexpr a
1127 :     in mark(I.FOPERATE{oper=I.CPYSN,fa=fs,fb=fs,fc=d},an) end
1128 :     | T.FSQRT(_,a) => error "fsqrt"
1129 :    
1130 :     (* loads *)
1131 :     | T.FLOAD(32,ea,mem) => fload(I.LDS,ea,d,mem,an)
1132 :     | T.FLOAD(64,ea,mem) => fload(I.LDT,ea,d,mem,an)
1133 : monnier 429
1134 :     (* floating/floating conversion
1135 :     * Note: it is not necessary to convert single precision
1136 :     * to double on the alpha.
1137 :     *)
1138 : george 545 | T.CVTF2F(fty,fty',e) => (* ignore rounding mode for now *)
1139 : monnier 475 (case (fty,fty') of
1140 : monnier 429 (64,64) => doFexpr(e,d,an)
1141 :     | (64,32) => doFexpr(e,d,an)
1142 :     | (32,32) => doFexpr(e,d,an)
1143 :     | (32,64) => funary(I.CVTTS,e,d,an) (* use normal rounding *)
1144 :     | _ => error "CVTF2F"
1145 :     )
1146 : monnier 409
1147 : monnier 429 (* integer -> floating point conversion *)
1148 : george 545 | T.CVTI2F(fty,ty,e) =>
1149 : monnier 429 let val pseudo =
1150 : monnier 475 case (ty,fty) of
1151 : monnier 429 (ty,32) => if ty <= 32 then P.cvtls else P.cvtqs
1152 :     | (ty,64) => if ty <= 32 then P.cvtlt else P.cvtqt
1153 :     | _ => error "CVTI2F"
1154 :     in fcvti2f(pseudo,e,d,an) end
1155 :    
1156 : george 545 | T.FMARK(e,A.MARKREG f) => (f d; doFexpr(e,d,an))
1157 :     | T.FMARK(e,a) => doFexpr(e,d,a::an)
1158 :     | T.FPRED(e,c) => doFexpr(e, d, A.CTRLUSE c::an)
1159 : george 555 | T.FEXT e => ExtensionComp.compileFext (reducer()) {e=e, fd=d, an=an}
1160 : monnier 409 | _ => error "doFexpr"
1161 :    
1162 :     (* check whether an expression is andb(e,1) *)
1163 : george 761 and isAndb1(e as T.ANDB(_, e1, e2)) = let
1164 :     fun isOne(n, ei) =
1165 :     if EQ(n, int_1) then (true, ei) else (false, e)
1166 :     in
1167 :     case(e1, e2)
1168 :     of (T.LI n, _) => isOne(n, e2)
1169 :     | (_, T.LI n) => isOne(n, e1)
1170 :     | _ => (false, e)
1171 :     end
1172 :     | isAndb1 e = (false, e)
1173 : monnier 409
1174 : george 761 and zeroOrOne(T.LI n) =
1175 :     if T.I.isZero n then ZERO
1176 :     else if EQ(n, int_1) then ONE
1177 :     else OTHER
1178 :     | zeroOrOne _ = OTHER
1179 : monnier 409
1180 :     (* compile a branch *)
1181 : george 545 and branch(e,lab,an) =
1182 : monnier 409 case e of
1183 :     T.CMP(ty,cc,e1 as T.LI _,e2) =>
1184 : george 545 branchBS(ty,T.Basis.swapCond cc,e2,e1,lab,an)
1185 : monnier 409 | T.CMP(ty,cc,e1,e2) => branchBS(ty,cc,e1,e2,lab,an)
1186 : george 545 (* generate an floating point branch *)
1187 :     | T.FCMP(fty,cc,e1,e2) =>
1188 :     let val f1 = fexpr e1
1189 :     val f2 = fexpr e2
1190 :     fun bcc(cmp,br) =
1191 :     let val tmpR = C.newFreg()
1192 :     in emit(I.DEFFREG(tmpR));
1193 :     emit(I.FOPERATE{oper=cmp,fa=f1,fb=f2,fc=tmpR});
1194 :     emit(I.TRAPB);
1195 :     mark(I.FBRANCH{b=br,f=tmpR,lab=lab},an)
1196 :     end
1197 :     fun fall(cmp1, br1, cmp2, br2) =
1198 :     let val tmpR1 = newFreg()
1199 :     val tmpR2 = newFreg()
1200 :     val fallLab = Label.newLabel ""
1201 :     in emit(I.DEFFREG(tmpR1));
1202 :     emit(I.FOPERATE{oper=cmp1, fa=f1, fb=f2, fc=tmpR1});
1203 :     emit(I.TRAPB);
1204 :     mark(I.FBRANCH{b=br1, f=tmpR1, lab=fallLab},an);
1205 :     emit(I.DEFFREG(tmpR2));
1206 :     emit(I.FOPERATE{oper=cmp2, fa=f1, fb=f2, fc=tmpR2});
1207 :     emit(I.TRAPB);
1208 :     mark(I.FBRANCH{b=br2, f=tmpR2, lab=lab},an);
1209 :     defineLabel fallLab
1210 :     end
1211 :     fun bcc2(cmp1, br1, cmp2, br2) =
1212 :     (bcc(cmp1, br1); bcc(cmp2, br2))
1213 :     in case cc of
1214 :     T.== => bcc(I.CMPTEQSU, I.FBNE)
1215 :     | T.?<> => bcc(I.CMPTEQSU, I.FBEQ)
1216 :     | T.? => bcc(I.CMPTUNSU, I.FBNE)
1217 :     | T.<=> => bcc(I.CMPTUNSU, I.FBEQ)
1218 :     | T.> => fall(I.CMPTLESU, I.FBNE, I.CMPTUNSU, I.FBEQ)
1219 :     | T.>= => fall(I.CMPTLTSU, I.FBNE, I.CMPTUNSU, I.FBEQ)
1220 :     | T.?> => bcc(I.CMPTLESU, I.FBEQ)
1221 :     | T.?>= => bcc(I.CMPTLTSU, I.FBEQ)
1222 :     | T.< => bcc(I.CMPTLTSU, I.FBNE)
1223 :     | T.<= => bcc(I.CMPTLESU, I.FBNE)
1224 :     | T.?< => bcc2(I.CMPTLTSU, I.FBNE, I.CMPTUNSU, I.FBNE)
1225 : leunga 744 | T.?<= => bcc2(I.CMPTLESU, I.FBNE, I.CMPTUNSU, I.FBNE)
1226 :     | T.<> => fall(I.CMPTEQSU, I.FBNE, I.CMPTUNSU, I.FBEQ)
1227 :     | T.?= => bcc2(I.CMPTEQSU, I.FBNE, I.CMPTUNSU, I.FBNE)
1228 :     | _ => error "branch"
1229 : george 545 end
1230 :     | e => mark(I.BRANCH{b=I.BNE,r=ccExpr e,lab=lab},an)
1231 : monnier 409
1232 : george 545 and br(opcode,exp,lab,an) = mark(I.BRANCH{b=opcode,r=expr exp,lab=lab},an)
1233 : monnier 409
1234 :     (* Use the branch on bit set/clear instruction when possible *)
1235 :     and branchBS(ty,cc,a,b,lab,an) =
1236 :     (case (cc,isAndb1 a,zeroOrOne b) of
1237 :     (T.EQ,(true,e),ONE) => br(I.BLBS,e,lab,an)
1238 :     | (T.EQ,(true,e),ZERO) => br(I.BLBC,e,lab,an)
1239 :     | (T.NE,(true,e),ZERO) => br(I.BLBS,e,lab,an)
1240 :     | (T.NE,(true,e),ONE) => br(I.BLBC,e,lab,an)
1241 :     | (cc,_,_) => branchIt(ty,cc,a,b,lab,an)
1242 :     )
1243 :    
1244 :     (* generate a branch instruction.
1245 :     * Check for branch on zero as a special case
1246 :     *)
1247 : george 761
1248 :     and branchIt(ty,cc,e1,e2 as T.LI z,lab,an) =
1249 :     if T.I.isZero z then branchIt0(cc,e1,lab,an)
1250 :     else branchItOther(ty,cc,e1,e2,lab,an)
1251 : monnier 409 | branchIt(ty,cc,e1,e2,lab,an) = branchItOther(ty,cc,e1,e2,lab,an)
1252 :    
1253 :     (* generate a branch instruction.
1254 :     * This function optimizes the special case of comparison with zero.
1255 :     *)
1256 :     and branchIt0(T.EQ,e,lab,an) = br(I.BEQ,e,lab,an)
1257 :     | branchIt0(T.NE,e,lab,an) = br(I.BNE,e,lab,an)
1258 :     | branchIt0(T.GT,e,lab,an) = br(I.BGT,e,lab,an)
1259 :     | branchIt0(T.GE,e,lab,an) = br(I.BGE,e,lab,an)
1260 :     | branchIt0(T.LE,e,lab,an) = br(I.BLE,e,lab,an)
1261 :     | branchIt0(T.LT,e,lab,an) = br(I.BLT,e,lab,an)
1262 :     | branchIt0(T.GTU,e,lab,an) = br(I.BNE,e,lab,an) (* always > 0! *)
1263 :     | branchIt0(T.GEU,e,lab,an) = (* always true! *) goto(lab,an)
1264 :     | branchIt0(T.LTU,e,lab,an) = (* always false! *) ()
1265 :     | branchIt0(T.LEU,e,lab,an) = br(I.BEQ,e,lab,an) (* never < 0! *)
1266 : leunga 744 | branchIt0 _ = error "brnachIt0"
1267 : monnier 409
1268 :     (* Generate the operands for unsigned comparisons
1269 :     * Mask out high order bits whenever necessary.
1270 :     *)
1271 :     and unsignedCmpOpnds(ty,e1,e2) =
1272 :     let fun zapHi(r,mask) =
1273 :     let val d = newReg()
1274 :     in emit(I.OPERATE{oper=I.ZAP, ra=r, rb=I.IMMop mask,rc=d});
1275 :     I.REGop d
1276 :     end
1277 :    
1278 :     fun zap(opn as I.REGop r) =
1279 :     (case ty of
1280 :     8 => zapHi(r,0xfd)
1281 :     | 16 => zapHi(r,0xfc)
1282 :     | 32 => zapHi(r,0xf0)
1283 :     | 64 => opn
1284 :     | _ => error "unsignedCmpOpnds"
1285 :     )
1286 :     | zap opn = opn
1287 :     val opn1 = opn e1
1288 :     val opn2 = opn e2
1289 :     in (zap opn1,zap opn2) end
1290 :    
1291 :     (* Generate a branch *)
1292 :     and branchItOther(ty,cond,e1,e2,lab,an) =
1293 :     let val tmpR = newReg()
1294 :     fun signedCmp(cmp,br) =
1295 :     (emit(I.OPERATE{oper=cmp, ra=expr e1, rb=opn e2, rc=tmpR});
1296 : george 545 mark(I.BRANCH{b=br, r=tmpR, lab=lab},an)
1297 : monnier 409 )
1298 :     fun unsignedCmp(ty,cmp,br) =
1299 :     let val (x,y) = unsignedCmpOpnds(ty,e1,e2)
1300 :     in emit(I.OPERATE{oper=cmp,ra=reduceOpn x,rb=y,rc=tmpR});
1301 : george 545 mark(I.BRANCH{b=br, r=tmpR, lab=lab},an)
1302 : monnier 409 end
1303 :     in case cond of
1304 :     T.LT => signedCmp(I.CMPLT,I.BNE)
1305 :     | T.LE => signedCmp(I.CMPLE,I.BNE)
1306 :     | T.GT => signedCmp(I.CMPLE,I.BEQ)
1307 :     | T.GE => signedCmp(I.CMPLT,I.BEQ)
1308 :     | T.EQ => signedCmp(I.CMPEQ,I.BNE)
1309 :     | T.NE => signedCmp(I.CMPEQ,I.BEQ)
1310 :     | T.LTU => unsignedCmp(ty,I.CMPULT,I.BNE)
1311 :     | T.LEU => unsignedCmp(ty,I.CMPULE,I.BNE)
1312 :     | T.GTU => unsignedCmp(ty,I.CMPULE,I.BEQ)
1313 :     | T.GEU => unsignedCmp(ty,I.CMPULT,I.BEQ)
1314 : leunga 744 | _ => error "branchItOther"
1315 : monnier 409 end
1316 :    
1317 :     (* This function generates a conditional move:
1318 :     * d = if cond(a,b) then x else y
1319 :     * Apparently, only signed comparisons conditional moves
1320 :     * are supported on the alpha.
1321 :     *)
1322 :     and cmove(ty,cond,a,b,x,y,d,an) =
1323 : george 545 let val tmp = newReg()
1324 :     val _ = doExpr(y,tmp,[]) (* evaluate false case *)
1325 : monnier 409
1326 :     val (cond,a,b) =
1327 :     (* move the immed operand to b *)
1328 :     case a of
1329 : leunga 775 (T.LI _ | T.CONST _ | T.LABEL _ | T.LABEXP _) =>
1330 :     (T.Basis.swapCond cond,b,a)
1331 : monnier 409 | _ => (cond,a,b)
1332 :    
1333 : george 761 fun sub(a, T.LI z) =
1334 :     if T.I.isZero z then expr a else expr(T.SUB(ty,a,b))
1335 :     | sub(a,b) = expr(T.SUB(ty,a,b))
1336 : monnier 409
1337 :     fun cmp(cond,e1,e2) =
1338 : george 545 let val flag = newReg()
1339 :     in compare(ty,cond,e1,e2,flag,[]); flag end
1340 : monnier 409
1341 :     val (oper,ra,x,y) =
1342 :     case (cond,isAndb1 a,zeroOrOne b) of
1343 :     (* low bit set/clear? *)
1344 :     (T.EQ,(true,e),ONE) => (I.CMOVLBS,expr e,x,y)
1345 :     | (T.EQ,(true,e),ZERO) => (I.CMOVLBC,expr e,x,y)
1346 :     | (T.NE,(true,e),ZERO) => (I.CMOVLBS,expr e,x,y)
1347 :     | (T.NE,(true,e),ONE) => (I.CMOVLBC,expr e,x,y)
1348 :     (* signed *)
1349 :     | (T.EQ,_,_) => (I.CMOVEQ,sub(a,b),x,y)
1350 : leunga 788 | (T.NE,_,_) => (I.CMOVNE,sub(a,b),x,y)
1351 : monnier 409 | (T.GT,_,_) => (I.CMOVGT,sub(a,b),x,y)
1352 :     | (T.GE,_,_) => (I.CMOVGE,sub(a,b),x,y)
1353 :     | (T.LT,_,_) => (I.CMOVLT,sub(a,b),x,y)
1354 :     | (T.LE,_,_) => (I.CMOVLE,sub(a,b),x,y)
1355 :    
1356 :     (* unsigned: do compare then use the condition code *)
1357 :     | (T.LTU,_,_) => (I.CMOVEQ,cmp(T.GEU,a,b),x,y)
1358 :     | (T.LEU,_,_) => (I.CMOVEQ,cmp(T.GTU,a,b),x,y)
1359 :     | (T.GTU,_,_) => (I.CMOVEQ,cmp(T.LEU,a,b),x,y)
1360 :     | (T.GEU,_,_) => (I.CMOVEQ,cmp(T.LTU,a,b),x,y)
1361 : leunga 744 | _ => error "cmove"
1362 : george 545 in mark(I.CMOVE{oper=oper,ra=ra,rb=opn x,rc=tmp},an); (* true case *)
1363 :     move(tmp, d, [])
1364 : monnier 409 end
1365 :    
1366 :    
1367 :     (* This function generates a comparion between e1 and e2 and writes
1368 :     * the result to register d.
1369 :     * It'll mask out the high order 32-bits when performing
1370 :     * unsigned 32-bit integer comparisons.
1371 :     *)
1372 :     and compare(ty,cond,e1,e2,d,an) =
1373 :     let fun signedCmp(oper,a,b,d) =
1374 :     mark(I.OPERATE{oper=oper,ra=expr a,rb=opn b,rc=d},an)
1375 :     fun unsignedCmp(ty,oper,a,b,d) =
1376 :     let val (x,y) = unsignedCmpOpnds(ty,a,b)
1377 :     in mark(I.OPERATE{oper=oper,ra=reduceOpn x,rb=y,rc=d},an)
1378 :     end
1379 :     fun eq(a,b,d) =
1380 :     (case (opn a,opn b) of
1381 :     (a,I.REGop r) =>
1382 :     mark(I.OPERATE{oper=I.CMPEQ,ra=r,rb=a,rc=d},an)
1383 :     | (a,b) =>
1384 :     mark(I.OPERATE{oper=I.CMPEQ,ra=reduceOpn a,rb=b,rc=d},an)
1385 :     )
1386 :     fun neq(a,b,d) =
1387 :     let val tmp = newReg()
1388 :     in eq(a,b,tmp);
1389 :     emit(I.OPERATE{oper=I.CMPEQ,ra=tmp,rb=zeroOpn,rc=d})
1390 :     end
1391 :     val (cond,e1,e2) =
1392 :     case e1 of
1393 : leunga 775 (T.LI _ | T.CONST _ | T.LABEL _ | T.LABEXP _) =>
1394 : george 545 (T.Basis.swapCond cond,e2,e1)
1395 : monnier 409 | _ => (cond,e1,e2)
1396 :     in case cond of
1397 :     T.EQ => eq(e1,e2,d)
1398 :     | T.NE => neq(e1,e2,d)
1399 :     | T.GT => signedCmp(I.CMPLT,e2,e1,d)
1400 :     | T.GE => signedCmp(I.CMPLE,e2,e1,d)
1401 :     | T.LT => signedCmp(I.CMPLT,e1,e2,d)
1402 :     | T.LE => signedCmp(I.CMPLE,e1,e2,d)
1403 :     | T.GTU => unsignedCmp(ty,I.CMPULT,e2,e1,d)
1404 :     | T.GEU => unsignedCmp(ty,I.CMPULE,e2,e1,d)
1405 :     | T.LTU => unsignedCmp(ty,I.CMPULT,e1,e2,d)
1406 :     | T.LEU => unsignedCmp(ty,I.CMPULE,e1,e2,d)
1407 : leunga 744 | _ => error "compare"
1408 : monnier 409 end
1409 :    
1410 :     (* generate an unconditional branch *)
1411 : george 545 and goto(lab,an) = mark(I.BRANCH{b=I.BR,r=zeroR,lab=lab},an)
1412 : monnier 409
1413 :     (* generate an call instruction *)
1414 : blume 839 and call(ea,flow,defs,uses,mem,cutTo,an,0) =
1415 :     let val defs=cellset defs
1416 :     val uses=cellset uses
1417 :     val instr =
1418 :     case (ea, flow) of
1419 :     (T.LABEL lab, [_]) =>
1420 :     I.BSR{lab=lab,r=C.returnAddr,defs=defs,uses=uses,
1421 :     cutsTo=cutTo,mem=mem}
1422 :     | _ => I.JSR{r=C.returnAddr,b=expr ea,
1423 :     d=0,defs=defs,uses=uses,cutsTo=cutTo,mem=mem}
1424 :     in mark(instr,an)
1425 :     end
1426 :     | call _ = error "pops<>0 not implemented"
1427 : monnier 409
1428 : george 545 and doCCexpr(T.CC(_,r),d,an) = move(r,d,an)
1429 :     | doCCexpr(T.FCC(_,r),d,an) = fmove(r,d,an)
1430 : monnier 409 | doCCexpr(T.CMP(ty,cond,e1,e2),d,an) = compare(ty,cond,e1,e2,d,an)
1431 :     | doCCexpr(T.FCMP(fty,cond,e1,e2),d,an) = error "doCCexpr"
1432 : george 545 | doCCexpr(T.CCMARK(e,A.MARKREG f),d,an) = (f d; doCCexpr(e,d,an))
1433 :     | doCCexpr(T.CCMARK(e,a),d,an) = doCCexpr(e,d,a::an)
1434 : george 555 | doCCexpr(T.CCEXT e,d,an) =
1435 :     ExtensionComp.compileCCext (reducer()) {e=e, ccd=d, an=an}
1436 : george 545 | doCCexpr _ = error "doCCexpr"
1437 : monnier 409
1438 : george 545 and ccExpr(T.CC(_,r)) = r
1439 :     | ccExpr(T.FCC(_,r)) = r
1440 : monnier 409 | ccExpr e = let val d = newReg()
1441 :     in doCCexpr(e,d,[]); d end
1442 :    
1443 :     (* compile a statement *)
1444 :     and stmt(s,an) =
1445 :     case s of
1446 :     T.MV(ty,r,e) => doExpr(e,r,an)
1447 :     | T.FMV(ty,r,e) => doFexpr(e,r,an)
1448 :     | T.CCMV(r,e) => doCCexpr(e,r,an)
1449 :     | T.COPY(ty,dst,src) => copy(dst,src,an)
1450 :     | T.FCOPY(ty,dst,src) => fcopy(dst,src,an)
1451 : leunga 775 | T.JMP(T.LABEL lab,_) => goto(lab,an)
1452 : leunga 744 | T.JMP(e,labs) => mark(I.JMPL({r=zeroR,b=expr e,d=0},labs),an)
1453 :     | T.BCC(cc,lab) => branch(cc,lab,an)
1454 : blume 839 | T.CALL{funct,targets,defs,uses,region,pops,...} =>
1455 :     call(funct,targets,defs,uses,region,[],an,pops)
1456 :     | T.FLOW_TO(T.CALL{funct,targets,defs,uses,region,pops,...},cutTo) =>
1457 :     call(funct,targets,defs,uses,region,cutTo,an,pops)
1458 : leunga 628 | T.RET _ => mark(I.RET{r=zeroR,b=C.returnAddr,d=0},an)
1459 : monnier 409 | T.STORE(8,ea,data,mem) => store8(ea,data,mem,an)
1460 :     | T.STORE(16,ea,data,mem) => store16(ea,data,mem,an)
1461 :     | T.STORE(32,ea,data,mem) => store(I.STL,ea,data,mem,an)
1462 :     | T.STORE(64,ea,data,mem) => store(I.STQ,ea,data,mem,an)
1463 :     | T.FSTORE(32,ea,data,mem) => fstore(I.STS,ea,data,mem,an)
1464 :     | T.FSTORE(64,ea,data,mem) => fstore(I.STT,ea,data,mem,an)
1465 : george 545 | T.DEFINE l => defineLabel l
1466 : monnier 409 | T.ANNOTATION(s,a) => stmt(s,a::an)
1467 : george 555 | T.EXT s => ExtensionComp.compileSext (reducer()) {stm=s,an=an}
1468 : george 545 | s => doStmts (Gen.compileStm s)
1469 : monnier 409
1470 : george 545 and reducer() =
1471 :     T.REDUCER{reduceRexp = expr,
1472 :     reduceFexp = fexpr,
1473 :     reduceCCexp = ccExpr,
1474 :     reduceStm = stmt,
1475 :     operand = opn,
1476 :     reduceOperand = reduceOpn,
1477 :     addressOf = addr,
1478 :     emit = mark,
1479 :     instrStream = instrStream,
1480 :     mltreeStream = self()
1481 :     }
1482 :    
1483 : monnier 409 and doStmt s = stmt(s,[])
1484 : george 545 and doStmts ss = app doStmt ss
1485 : monnier 409
1486 : george 545 (* convert mlrisc to cellset:
1487 :     * condition code registers are mapped onto general registers
1488 :     *)
1489 :     and cellset mlrisc =
1490 :     let fun g([],acc) = acc
1491 :     | g(T.GPR(T.REG(_,r))::regs,acc) = g(regs,C.addReg(r,acc))
1492 :     | g(T.FPR(T.FREG(_,f))::regs,acc) = g(regs,C.addFreg(f,acc))
1493 :     | g(T.CCR(T.CC(_,cc))::regs,acc) = g(regs,C.addReg(cc,acc))
1494 :     | g(T.CCR(T.FCC(_,cc))::regs,acc) = g(regs,C.addReg(cc,acc))
1495 :     | g(_::regs, acc) = g(regs, acc)
1496 :     in g(mlrisc, C.empty) end
1497 : monnier 409
1498 : george 545 and self() =
1499 :     S.STREAM
1500 : leunga 815 { beginCluster = beginCluster,
1501 :     endCluster = endCluster,
1502 :     emit = doStmt,
1503 :     pseudoOp = pseudoOp,
1504 :     defineLabel = defineLabel,
1505 :     entryLabel = entryLabel,
1506 :     comment = comment,
1507 :     annotation = annotation,
1508 :     getAnnotations = getAnnotations,
1509 :     exitBlock = fn regs => exitBlock(cellset regs)
1510 : george 545 }
1511 :     in self()
1512 : monnier 409 end
1513 :    
1514 :     end
1515 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0