Debian Codegraph @

lib/mlrisc-lib/MLRISC/alpha/mltree/alpha.sml

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