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

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