Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/mltree/amd64-gen.sml

raw
1 (* amd64-gen.sml
2 *
3 * COPYRIGHT (c) 2016 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *
6 * Translate MLRISC trees into AMD64 instructions.
7 *)
8
9 functor AMD64Gen (
10
11 structure I : AMD64INSTR
12 structure MLTreeUtils : MLTREE_UTILS (* where T = I.T *)
13 where type T.Basis.cond = I.T.Basis.cond
14 and type T.Basis.div_rounding_mode = I.T.Basis.div_rounding_mode
15 and type T.Basis.ext = I.T.Basis.ext
16 and type T.Basis.fcond = I.T.Basis.fcond
17 and type T.Basis.rounding_mode = I.T.Basis.rounding_mode
18 and type T.Constant.const = I.T.Constant.const
19 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) I.T.Extension.ccx
20 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) I.T.Extension.fx
21 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) I.T.Extension.rx
22 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) I.T.Extension.sx
23 and type T.I.div_rounding_mode = I.T.I.div_rounding_mode
24 and type T.Region.region = I.T.Region.region
25 and type T.ccexp = I.T.ccexp
26 and type T.fexp = I.T.fexp
27 (* and type T.labexp = I.T.labexp *)
28 and type T.mlrisc = I.T.mlrisc
29 and type T.oper = I.T.oper
30 and type T.rep = I.T.rep
31 and type T.rexp = I.T.rexp
32 and type T.stm = I.T.stm
33 structure ExtensionComp : MLTREE_EXTENSION_COMP (* where I = I and T = I.T *)
34 where type I.addressing_mode = I.addressing_mode
35 and type I.ea = I.ea
36 and type I.instr = I.instr
37 and type I.instruction = I.instruction
38 and type I.operand = I.operand
39 where type T.Basis.cond = I.T.Basis.cond
40 and type T.Basis.div_rounding_mode = I.T.Basis.div_rounding_mode
41 and type T.Basis.ext = I.T.Basis.ext
42 and type T.Basis.fcond = I.T.Basis.fcond
43 and type T.Basis.rounding_mode = I.T.Basis.rounding_mode
44 and type T.Constant.const = I.T.Constant.const
45 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) I.T.Extension.ccx
46 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) I.T.Extension.fx
47 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) I.T.Extension.rx
48 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) I.T.Extension.sx
49 and type T.I.div_rounding_mode = I.T.I.div_rounding_mode
50 and type T.Region.region = I.T.Region.region
51 and type T.ccexp = I.T.ccexp
52 and type T.fexp = I.T.fexp
53 (* and type T.labexp = I.T.labexp *)
54 and type T.mlrisc = I.T.mlrisc
55 and type T.oper = I.T.oper
56 and type T.rep = I.T.rep
57 and type T.rexp = I.T.rexp
58 and type T.stm = I.T.stm
59
60 (* Take a number of bits and returns an rexp that points to a literal with the high bit set.
61 * We need this literal value for floating-point negation and absolute value (at least
62 * until SSE4).
63 *)
64 val signBit : int -> MLTreeUtils.T.rexp
65 (* Same as signBit, except the high bit is zero and the low bits are 1s. *)
66 val negateSignBit : int -> MLTreeUtils.T.rexp
67
68 (* guaranteeing that floats are stored at 16-byte aligned addresses reduces the number of instructions *)
69 val floats16ByteAligned : bool
70
71 ) : MLTREECOMP = struct
72
73 structure TS = ExtensionComp.TS
74 structure T = I.T
75 structure I = I
76 structure CFG = ExtensionComp.CFG
77 structure C = I.C
78 structure CB = CellsBasis
79 structure A = MLRiscAnnotations
80 structure TRS = MLTreeSize (structure T = T
81 val intTy = 64)
82 structure Shuffle = Shuffle (I)
83 structure O = AMD64Opcodes (structure I = I)
84 structure Gen = MLTreeGen (
85 structure T = T
86 structure Cells = C
87 val intTy = 64
88 val naturalWidths = [32, 64]
89 datatype rep = SE | ZE | NEITHER
90 val rep = NEITHER)
91
92 structure W32 = Word32
93
94 fun error msg = MLRiscErrorMsg.error ("AMD64Gen", msg)
95
96 type instrStream = (I.instruction,C.cellset,CFG.cfg) TS.stream
97 type mltreeStream = (T.stm,T.mlrisc list,CFG.cfg) TS.stream
98
99 (* label where the shared trap is generated -- one per cluster *)
100 val trapLabel = ref (NONE: (I.instruction * Label.label) option)
101
102 (* flag to control merging of overflow traps *)
103 val mergeTraps = ref true
104
105 fun gpr (ty, r) = I.Direct (ty, r)
106 val fpr = I.FDirect
107
108 fun rax ty = I.Direct(ty,C.rax)
109 fun rcx ty = I.Direct(ty,C.rcx)
110 fun rdx ty = I.Direct(ty,C.rdx)
111
112 fun signExtend ty = if ty = 64 then I.CDO else I.CDQ
113
114 val readonly = I.Region.readonly
115
116 val newReg = C.newReg
117 val newFreg = C.newFreg
118
119 (* convert mlrisc to cellset: *)
120 fun cellset mlrisc = let
121 val addCCReg = CB.CellSet.add
122 fun g([],acc) = acc
123 | g(T.GPR(T.REG(_,r))::regs,acc) = g(regs,C.addReg(r,acc))
124 | g(T.FPR(T.FREG(_,f))::regs,acc) = g(regs,C.addFreg(f,acc))
125 | g(T.CCR(T.CC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc))
126 | g(T.CCR(T.FCC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc))
127 | g(_::regs, acc) = g(regs, acc)
128 in
129 g(mlrisc, C.empty)
130 end
131
132 (* conversions to fixed-precision integers*)
133 fun toInt32 i = T.I.toInt32(32, i)
134 fun toInt64 i = T.I.toInt64(64, i)
135
136 (* 64BIT: workaround for compiler bug *)
137 val minInt32 = Word64.toLargeIntX 0wxFFFFFFFF80000000
138 val maxInt32 = Word64.toLargeInt 0wx80000000
139
140 (* is an immediate operand representable as a signed 32-bit 2's complement value?
141 * Note that values get sign extended when loaded into a 64-bit register.
142 *)
143 fun fitsIn32Bits (z : IntInf.int) = (minInt32 <= z) andalso (z < maxInt32)
144 (*
145 fun fitsIn32Bits (z : IntInf.int) = (~0x80000000 <= z) andalso (z < 0x80000000)
146 *)
147
148 fun move64 (src, dst) = I.move {mvOp=I.MOVABSQ, src=src, dst=dst}
149
150 (* analyze for power-of-two-ness; this does not handle values that do not fit
151 * in 32 bits.
152 *)
153 fun analyze i' = if fitsIn32Bits i'
154 then let
155 val i = toInt32 i'
156 in
157 let
158 val (isneg, a, w) = if i >= 0
159 then (false, i, T.I.toWord32 (32, i'))
160 else (true, ~i, T.I.toWord32 (32, T.I.NEG (32, i')))
161 fun log2 (0w1, p) = p
162 | log2 (w, p) = log2 (W32.>> (w, 0w1), p + 1)
163 in
164 if w > 0w1 andalso W32.andb (w - 0w1, w) = 0w0
165 then SOME(i, isneg, a, T.LI (T.I.fromInt32 (32, log2 (w, 0))))
166 else NONE
167 end handle _ => NONE
168 end
169 else NONE
170 (* a 64-bit version that we might want to use in the future
171 fun analyze i' = let
172 val i = toInt64 i'
173 val (isneg, a, w) = if i >= 0
174 then (false, i, T.I.toWord64 (64, i'))
175 else (true, ~i, T.I.toWord64 (64, T.I.NEG (64, i')))
176 fun log2 (0w1, p) = p
177 | log2 (w, p) = log2 (W64.>> (w, 0w1), p + 1)
178 in
179 if w > 0w1 andalso W64.andb (w - 0w1, w) = 0w0
180 then (i, SOME (isneg, a, T.LI (T.I.fromInt64 (64, log2 (w, 0)))))
181 else (i, NONE)
182 end
183 handle _ => (toInt64 i', NONE)
184 *)
185
186 (* translate MLTREE condition codes to amd64 condition codes *)
187 fun cond T.LT = I.LT | cond T.LTU = I.B
188 | cond T.LE = I.LE | cond T.LEU = I.BE
189 | cond T.EQ = I.EQ | cond T.NE = I.NE
190 | cond T.GE = I.GE | cond T.GEU = I.AE
191 | cond T.GT = I.GT | cond T.GTU = I.A
192 | cond cc = error(concat["cond(", T.Basis.condToString cc, ")"])
193
194 (* Is the expression zero? *)
195 fun isZero (T.LI 0) = true
196 | isZero (T.MARK(e, _)) = isZero e
197 | isZero _ = false
198
199 fun setZeroBit(T.ANDB _) = true
200 | setZeroBit(T.ORB _) = true
201 | setZeroBit(T.XORB _) = true
202 | setZeroBit(T.SRA _) = true
203 | setZeroBit(T.SRL _) = true
204 | setZeroBit(T.SLL _) = true
205 | setZeroBit(T.SUB _) = true
206 | setZeroBit(T.ADDT _) = true
207 | setZeroBit(T.SUBT _) = true
208 | setZeroBit(T.MARK(e, _)) = setZeroBit e
209 | setZeroBit _ = false
210
211 fun setZeroBit2(T.ANDB _) = true
212 | setZeroBit2(T.ORB _) = true
213 | setZeroBit2(T.XORB _) = true
214 | setZeroBit2(T.SRA _) = true
215 | setZeroBit2(T.SRL _) = true
216 | setZeroBit2(T.SLL _) = true
217 | setZeroBit2(T.ADD(_, _, _)) = true (* can't use leal! *)
218 | setZeroBit2(T.SUB _) = true
219 | setZeroBit2(T.ADDT _) = true
220 | setZeroBit2(T.SUBT _) = true
221 | setZeroBit2(T.MARK(e, _)) = setZeroBit2 e
222 | setZeroBit2 _ = false
223
224 and isMemOpnd opnd = (case opnd
225 of I.Displace _ => true
226 | I.Indexed _ => true
227 | I.LabelEA _ => true
228 | I.FDirect f => true
229 | _ => false
230 (* end case *))
231
232 fun selectInstructions (instrStream as TS.S.STREAM{
233 emit=emitInstr, defineLabel, entryLabel, pseudoOp, annotation,
234 getAnnotations, beginCluster, endCluster, exitBlock,
235 comment, ...
236 }) = let
237 val emit = emitInstr o I.INSTR
238 val emits = app emitInstr
239 (* mark an expression with a list of annotations *)
240 fun mark' (i, []) = emitInstr i
241 | mark' (i, a::an) = mark'(I.ANNOTATION{i=i,a=a},an)
242 (* annotate an expression and emit it *)
243 fun mark (i, an) = mark' (I.INSTR i, an)
244 (* annotated 64-bit move *)
245 fun move64' (src, dst, an) = mark' (move64(src, dst), an)
246 (* move with annotation *)
247 fun move' (ty, dst as I.Direct (_, s), src as I.Direct (_, d), an) =
248 if CB.sameColor (s, d)
249 then ()
250 else mark' (I.COPY {k=CB.GP, sz=ty, src=[s], dst=[d], tmp=NONE}, an)
251 | move' (ty, I.Immed 0, dst as I.Direct _, an) =
252 mark' (I.binary {binOp=O.xorOp ty, src=dst, dst=dst}, an)
253 | move' (ty, src as I.Immed64 n, dst, an) = move64' (src, dst, an)
254 | move' (ty, src as I.ImmedLabel _ , dst as I.Direct _, an) = move64' (src, dst, an)
255 | move' (ty, src as I.ImmedLabel _ , dst, an) = let
256 val tmp = newReg ()
257 val tmpR = I.Direct (64, tmp)
258 in
259 move64' (src, tmpR, an);
260 emitInstr (I.move {mvOp=I.MOVQ, src=tmpR, dst=dst})
261 end
262 | move' (ty, src, dst, an) =
263 mark' (I.move {mvOp=O.movOp ty, src=src, dst=dst}, an)
264 (* move without annotation *)
265 fun move (ty, src, dst) = move' (ty, src, dst, [])
266
267 fun zero (ty, dst) = emit (I.BINARY{binOp=O.xorOp ty, src=dst, dst=dst})
268
269 fun copy (ty, [], [], an) = ()
270 | copy (ty, dst, src, an) = let
271 fun mvInstr {dst=I.Direct (_,rd), src=I.Direct (_,rs)} =
272 if CB.sameColor (rd, rs)
273 then []
274 else [I.COPY {k=CB.GP, sz=ty, dst=[rd], src=[rs], tmp=NONE}]
275 | mvInstr {dst, src} = [I.move {mvOp=O.movOp ty, src=src, dst=dst}]
276 val stms = Shuffle.shuffle {mvInstr=mvInstr, ea=fn r => gpr (ty, r)}
277 {tmp=SOME (I.Direct (ty, newReg ())), dst=dst, src=src}
278 in
279 emits stms
280 end (* copy *)
281
282 (* Add an overflow trap *)
283 fun trap () = if !mergeTraps
284 then let
285 val jmp = (case !trapLabel
286 of NONE => let
287 val label = Label.label "trap" ()
288 val jmp = I.ANNOTATION{
289 i = I.jcc{cond=I.O, opnd=I.ImmedLabel(T.LABEL label)},
290 a = MLRiscAnnotations.BRANCHPROB Probability.unlikely
291 }
292 in
293 trapLabel := SOME(jmp, label);
294 jmp
295 end
296 | SOME(jmp, _) => jmp
297 (* end case *))
298 in
299 emitInstr jmp
300 end (* trap *)
301 else let
302 val label = Label.anon()
303 val jmp = I.ANNOTATION{
304 i = I.jcc{cond=I.NO, opnd = I.ImmedLabel(T.LABEL label)},
305 a = MLRiscAnnotations.BRANCHPROB Probability.likely
306 }
307 in
308 emitInstr jmp;
309 (* signal an overflow exception (code 4) *)
310 emit(I.INT 0w4);
311 defineLabel label
312 end
313
314 exception EA
315
316 fun address' ty (ea : T.rexp, mem) = let
317 fun makeAddressingMode (NONE, NONE, _, disp) = disp
318 | makeAddressingMode (SOME base, NONE, _, disp) =
319 I.Displace{base=base, disp=disp, mem=mem}
320 | makeAddressingMode (base, SOME index, scale, disp) =
321 I.Indexed{base=base, index=index, scale=scale, disp=disp, mem=mem}
322
323 (* Keep building a bigger and bigger effective address expressions
324 * The input is a list of trees
325 * b -- base
326 * i -- index
327 * s -- scale
328 * d -- immed displacement
329 *)
330 fun doEA ([], b, i, s, d) = makeAddressingMode(b, i, s, d)
331 | doEA (t::trees, b, i, s, d) = (case t
332 of T.LI n => if fitsIn32Bits n
333 then doEAImmed (trees, toInt32 n, b, i, s, d)
334 else error "doEA: immediate too large"
335 | T.CONST _ => doEALabel(trees, t, b, i, s, d)
336 | T.LABEL _ => doEALabel(trees, t, b, i, s, d)
337 | T.LABEXP le => doEALabel(trees, le, b, i, s, d)
338 | T.ADD(ty, t1, t2 as T.REG(_,r)) =>
339 doEA(t1::t2::trees, b, i, s, d)
340 | T.ADD(ty, t1, t2) => doEA(t1::t2::trees, b, i, s, d)
341 | T.SUB(ty, t1, T.LI n) =>
342 doEA(t1::T.LI(T.I.NEG(ty,n))::trees, b, i, s, d)
343 | T.SLL(ty, t1, T.LI n) => let
344 val n = T.I.toInt(ty, n)
345 in
346 case n
347 of 0 => displace(trees, t1, b, i, s, d)
348 | 1 => indexed(trees, t1, t, 1, b, i, s, d)
349 | 2 => indexed(trees, t1, t, 2, b, i, s, d)
350 | 3 => indexed(trees, t1, t, 3, b, i, s, d)
351 | _ => displace(trees, t, b, i, s, d)
352 end
353 | t => displace(trees, t, b, i, s, d)
354 (* esac *))
355
356 (* Add a 32-bit immed constant *)
357 and doEAImmed (trees, 0, b, i, s, d) = doEA(trees, b, i, s, d)
358 | doEAImmed (trees, n, b, i, s, I.Immed m) =
359 doEA(trees, b, i, s, I.Immed(n+m))
360 | doEAImmed (trees, n, b, i, s, I.ImmedLabel le) =
361 doEA(trees, b, i, s,
362 I.ImmedLabel(T.ADD(ty,le,T.LI(T.I.fromInt32(ty, n)))))
363 | doEAImmed (trees, n, b, i, s, _) = error "doEAImmed"
364
365 (* Add a label expression.
366 * NOTE: Labels in the AMD64 can be 64 bits, but operands can only handle 32-bit constants.
367 * We have to spill label expressions to temporaries to be correct.
368 * TODO: eliminate the extra register-register move from genExpr
369 *)
370 (* Added below pattern to stop infinite loop where (genExpr le)
371 below called addition and addition called address again. *)
372 and doEALabel (trees, le, SOME base, i, s, I.Immed 0) = let
373 val r = newReg ()
374 val _ = expr (le, r, [])
375 val _ = mark (I.BINARY{binOp=(O.addOp ty), src=operand ty (T.REG(ty,base)), dst=operand ty (T.REG(ty,r))}, [])
376 in
377 doEA(trees, SOME r, i, s, I.Immed 0)
378 end
379 | doEALabel (trees, le, b, i, s, d) = let
380 val le = (case b
381 of NONE => le
382 | SOME base => T.ADD (ty, T.REG (ty, base), le)
383 (* end case *))
384 in
385 (case d
386 of I.Immed 0 => doEA(trees, SOME (genExpr le), i, s, I.Immed 0)
387 | I.Immed m => (doEA (trees,
388 SOME (genExpr (T.ADD (ty, le, T.LI(T.I.fromInt32(ty, m))))),
389 i, s, I.Immed 0)
390 handle Overflow => error "doEALabel: constant too large")
391 | I.ImmedLabel le' => doEA (trees,
392 SOME (genExpr (T.ADD (ty, le', le))),
393 i, s, I.Immed 0)
394 | _ => error "doEALabel"
395 (* end case *))
396 end
397
398 (* generate code for tree and ensure that it is not in %rsp *)
399 and exprNotRsp tree = let
400 val r = genExpr tree
401 in
402 if CB.sameColor(r, C.rsp) then let
403 val tmp = newReg()
404 in
405 move(ty, I.Direct (ty,r), I.Direct (ty,tmp));
406 tmp
407 end
408 else r
409 end
410
411 (* Add a base register *)
412 and displace(trees, t, NONE, i, s, d) = (* no base yet *)
413 doEA(trees, SOME(genExpr t), i, s, d)
414 | displace(trees, t, b as SOME base, NONE, _, d) = (* no index *)
415 (* make t the index, but make sure that it is not %rsp! *)
416 let val i = genExpr t
417 in if CB.sameColor(i, C.rsp) then
418 (* swap base and index *)
419 if CB.sameColor(base, C.rsp) then
420 doEA(trees, SOME i, b, 0, d)
421 else (* base and index = %rsp! *)
422 let val index = newReg()
423 in
424 move(ty, I.Direct (ty,i), I.Direct (ty,index));
425 doEA(trees, b, SOME index, 0, d)
426 end
427 else doEA(trees, b, SOME i, 0, d)
428 end
429 | displace(trees, t, SOME base, i, s, d) = (* base and index *)
430 let val b = genExpr (T.ADD(ty,T.REG(ty,base),t))
431 in
432 doEA(trees, SOME b, i, s, d)
433 end
434
435 (* Add an indexed register *)
436 and indexed(trees, t, t0, scale, b, NONE, _, d) = (* no index yet *)
437 doEA(trees, b, SOME(exprNotRsp t), scale, d)
438 | indexed(trees, _, t0, _, NONE, i, s, d) = (* no base *)
439 doEA(trees, SOME(genExpr t0), i, s, d)
440 | indexed(trees, _, t0, _, SOME base, i, s, d) = (*base and index*)
441 let val b = genExpr (T.ADD(ty, t0, T.REG(ty, base)))
442 in
443 doEA(trees, SOME b, i, s, d)
444 end
445 in
446 case doEA ([ea], NONE, NONE, 0, I.Immed 0)
447 of I.Immed _ => raise EA
448 | I.ImmedLabel le => I.Displace {base=genExpr le, disp=I.Immed 0, mem=mem} (* I.LabelEA le*)
449 | ea => ea
450 end (* address' *)
451
452 and address (ea, mem) = address' 64 (ea, mem)
453
454 (* load the label into a temp register. this operation is necessary, since most instructions
455 * can only handle 32-bit immediates
456 *)
457 and loadLabel src = let
458 val tmp = newReg ()
459 val tmpR = I.Direct (64, tmp)
460 in
461 emitInstr (move64 (src, tmpR));
462 tmpR
463 end
464
465 (* reduce an expression into an operand *)
466 and operand ty (T.LI i) = if (fitsIn32Bits i)
467 then I.Immed (toInt32 i)
468 else let
469 (* i is a 64-bit operand *)
470 val dstR = newReg ()
471 val dst = I.Direct (ty, dstR)
472 val i' = T.I.signed (64, i)
473 in
474 move (ty, I.Immed64 (Int64.fromLarge i'), dst);
475 dst
476 end
477 | operand _ (x as (T.CONST _ | T.LABEL _)) = loadLabel (I.ImmedLabel x)
478 | operand _ (T.LABEXP le) = loadLabel (I.ImmedLabel le)
479 | operand _ (T.REG(ty,r)) = gpr (ty, r)
480 | operand _ (T.LOAD(ty,ea,mem)) = address (ea, mem)
481 | operand ty t = I.Direct(ty, genExpr t)
482
483 and immedLabel lab = I.ImmedLabel(T.LABEL lab)
484
485 (* ensure that the operand is either an immed or register *)
486 and immedOrReg(ty, opnd as I.Displace _) = moveToReg (ty, opnd)
487 | immedOrReg(ty, opnd as I.Indexed _) = moveToReg (ty, opnd)
488 | immedOrReg(ty, opnd as I.LabelEA _) = moveToReg (ty, opnd)
489 | immedOrReg (ty, opnd) = opnd
490
491 and regOrMem (ty, opnd as (I.Immed _ | I.ImmedLabel _)) = moveToReg (ty, opnd)
492 | regOrMem (ty, opnd) = opnd
493
494 and getExpr (exp as T.REG(_, rd)) = rd
495 | getExpr exp = genExpr exp
496
497 and moveToReg (ty, opnd) = let
498 val dst = I.Direct (ty, newReg ())
499 in
500 move (ty, opnd, dst);
501 dst
502 end
503
504 and isImmediate(I.Immed _) = true
505 (* | isImmediate(I.ImmedLabel _) = true *)
506 | isImmediate _ = false
507
508 (* generate an expression, and return its result in a register *)
509 and genExpr (T.REG (_, r)) = r
510 | genExpr e = let
511 val r = newReg ()
512 in
513 expr (e, r, []);
514 r
515 end
516
517 and expr' (ty, e, dst, an) = let
518 val dstOpnd = gpr (ty, dst)
519 fun equalDst (I.Direct (_,r)) = CB.sameColor(r, dst)
520 | equalDst _ = false
521 fun dstMustBeReg f = f (dst, dstOpnd)
522 fun genLoad (mvOp, ea, mem) = dstMustBeReg (fn (_, dst) =>
523 mark (I.MOVE {mvOp=mvOp, src=address (ea, mem), dst=dst},an))
524 fun unknownExp exp = expr (Gen.compileRexp exp, dst, an)
525
526 (* Generate a unary operator *)
527 fun unary(ty, unOp, e) = let
528 val opnd = operand ty e
529 in
530 if isMemOpnd opnd then let
531 val tmp = I.Direct(ty, newReg())
532 in
533 move(ty, opnd, tmp);
534 move(ty, tmp, dstOpnd)
535 end
536 else move(ty, opnd, dstOpnd);
537 mark(I.UNARY{unOp=unOp ty, opnd=dstOpnd}, an)
538 end
539
540 fun genBinary (ty, binOp, opnd1, opnd2) =
541 if (isMemOpnd opnd1 orelse isMemOpnd opnd2) orelse
542 equalDst(opnd2) then let
543 val tmpR = newReg()
544 val tmp = I.Direct (ty,tmpR)
545 in
546 move (ty, opnd1, tmp);
547 mark (I.BINARY{binOp=binOp ty, src=opnd2, dst=tmp}, an);
548 move (ty, tmp, dstOpnd)
549 end
550 else (move (ty, opnd1, dstOpnd);
551 mark(I.BINARY{binOp=binOp ty, src=opnd2, dst=dstOpnd}, an))
552 (* generate a binary operator that may commute *)
553 fun binaryComm (ty, binOp, e1, e2) = let
554 val (opnd1, opnd2) = (case (operand ty e1, operand ty e2)
555 of (x as I.Immed _, y) => (y, x)
556 | (x as I.ImmedLabel _, y) => (y, x)
557 | (x, y as I.Direct _) => (y, x)
558 | (x, y) => (x, y)
559 (* end case *))
560 in
561 genBinary(ty, binOp, opnd1, opnd2)
562 end
563 (* Generate a binary operator; non-commutative *)
564 fun binary(ty, binOp, e1, e2) =
565 genBinary(ty, binOp, operand ty e1, operand ty e2)
566 (* Add n to dst *)
567 fun addN (addOp, n) = let
568 val n = operand ty n
569 in
570 mark (I.BINARY{binOp=addOp, src=n, dst=dstOpnd}, an)
571 end
572 (* Generate addition *)
573 fun addition (ty, e1, e2) = (case e1
574 of T.REG(_,rs) =>
575 if CB.sameColor(rs,dst)
576 then addN (O.addOp ty, e2)
577 else addition1 (ty, e1,e2)
578 | _ => addition1(ty, e1,e2)
579 (* end case *))
580 and addition1 (ty, e1, e2) = (case e2
581 of T.REG(_,rs) =>
582 if CB.sameColor(rs,dst)
583 then addN (O.addOp ty, e1)
584 else addition2(ty, e1,e2)
585 | _ => addition2 (ty, e1,e2)
586 (* end case *))
587 and addition2 (32, e1, e2) = (
588 (* try *)
589 dstMustBeReg(fn (dstR, _) =>
590 mark(I.LEAL{r32=dstR, addr=address' 32 (e, readonly)}, an))
591 (* catch *)
592 handle EA => binaryComm(ty, O.addOp, e1, e2))
593 | addition2 (64, e1, e2) = let
594 fun isBigImmed (T.LI n) = not(fitsIn32Bits n)
595 | isBigImmed _ = false
596 fun lea (dstR, _) = mark(I.LEAQ{r64=dstR, addr=address' 64 (e, readonly)}, an)
597 in
598 if isBigImmed e1 orelse isBigImmed e2
599 then binaryComm(ty, O.addOp, e1, e2)
600 else (dstMustBeReg lea) handle EA => binaryComm(ty, O.addOp, e1, e2)
601 end
602 (* the shift amount must be a constant or in %rcx *)
603 fun shift(ty, opcode, e1, e2) = let
604 val (opnd1, opnd2) = (operand ty e1, operand ty e2)
605 in
606 (case opnd2
607 of I.Immed _ => genBinary(ty, opcode, opnd1, opnd2)
608 | _ =>
609 if equalDst(opnd2) then
610 let val tmpR = newReg()
611 val tmp = I.Direct (ty,tmpR)
612 in
613 move(ty, opnd1, tmp);
614 move(ty, opnd2, rcx ty);
615 mark(I.BINARY{binOp=opcode ty, src=rcx ty,
616 dst=tmp},an);
617 move(ty, tmp, dstOpnd)
618 end
619 else
620 (move(ty, opnd1, dstOpnd);
621 move(ty, opnd2, rcx ty);
622 mark(I.BINARY{binOp=opcode ty, src=rcx ty,
623 dst=dstOpnd},an))
624 (* end case *))
625 end
626
627 (* division with rounding towards negative infinity *)
628 fun divinf0 (ty, e1, e2) = let
629 val o1 = operand ty e1
630 val o2 = operand ty e2
631 val l = Label.anon ()
632 in
633 move (ty, o1, rax ty);
634 emit (signExtend ty);
635 mark (I.MULTDIV { multDivOp = O.idivOp ty,
636 src = regOrMem (ty, o2) },
637 an);
638 (* NOTE: on the x86-64, the IDIV instruction traps on overflow
639 if overflow then trap() else ();
640 *)
641 app emit [(O.cmpOp ty) { lsrc = rdx ty, rsrc = I.Immed 0 },
642 I.JCC { cond = I.EQ, opnd = immedLabel l },
643 I.BINARY { binOp = O.xorOp ty,
644 src = regOrMem (ty, o2),
645 dst = rdx ty },
646 I.JCC { cond = I.GE, opnd = immedLabel l },
647 I.UNARY { unOp = O.decOp ty, opnd = rax ty }];
648 defineLabel l;
649 move (ty, rax ty, dstOpnd)
650 end
651
652 (* Division by a power of two when rounding to neginf is the
653 * same as an arithmetic right shift. *)
654 fun divinf (ty, e1, e2 as T.LI n') = (case analyze n'
655 of NONE => divinf0 (ty, e1, e2)
656 | SOME(_, false, _, p) =>
657 shift (ty, O.sarOp, T.REG (ty, getExpr e1), p)
658 | SOME(_, true, _, p) => let
659 val reg = getExpr e1
660 in
661 emit(I.UNARY { unOp = O.negOp ty, opnd = I.Direct (ty,reg) });
662 shift (ty, O.sarOp, T.REG (ty, reg), p)
663 end
664 (* eed case *))
665 | divinf (ty, e1, e2) = divinf0 (ty, e1, e2)
666
667 fun reminf0 (ty, e1, e2) = let
668 val o1 = operand ty e1
669 val o2 = operand ty e2
670 val l = Label.anon ()
671 in
672 move (ty, o1, rax ty);
673 emit (signExtend ty);
674 mark (I.MULTDIV { multDivOp = O.idiv1Op ty,
675 src = regOrMem (ty, o2) },
676 an);
677 app emit [(O.cmpOp ty) { lsrc = rdx ty, rsrc = I.Immed 0 },
678 I.JCC { cond = I.EQ, opnd = immedLabel l }];
679 move (ty, rdx ty, rax ty);
680 app emit [I.BINARY { binOp = O.xorOp ty,
681 src = regOrMem (ty, o2), dst = rax ty },
682 I.JCC { cond = I.GE, opnd = immedLabel l },
683 I.BINARY { binOp = O.addOp ty,
684 src = regOrMem (ty, o2), dst = rdx ty }];
685 defineLabel l;
686 move (ty, rdx ty, dstOpnd)
687 end
688
689 (* n mod (power-of-2) corrrsponds to a bitmask (AND).
690 * If the power is negative, then we must first negate
691 * the argument and then again negate the result. *)
692 fun reminf (ty, e1, e2 as T.LI n') = (case analyze n'
693 of NONE => reminf0 (ty, e1, e2)
694 | SOME(_, false, a, _) =>
695 binaryComm (ty, O.andOp, e1, T.LI (T.I.fromInt32 (ty, a - 1)))
696 | SOME(_, true, a, _) => let
697 val r1 = getExpr e1
698 val o1 = I.Direct (ty,r1)
699 in
700 emit (I.UNARY { unOp = O.negOp ty, opnd = o1 });
701 emit (I.BINARY { binOp = O.andOp ty,
702 src = I.Immed (a - 1),
703 dst = o1 });
704 unary (ty, O.negOp, T.REG (ty, r1))
705 end
706 (* end case *))
707 | reminf (ty, e1, e2) = reminf0 (ty, e1, e2)
708
709 (* Division or remainder: divisor must be in %rdx:%rax pair *)
710 fun divrem (ty, signed, e1, e2, resultReg) = let
711 val (opnd1, opnd2) = (operand ty e1, operand ty e2)
712 val _ = move(ty, opnd1, rax ty)
713 val oper = if signed
714 then (emit(signExtend ty); O.idiv1Op ty)
715 else (zero (ty, rdx ty); O.div1Op ty)
716 in
717 mark(I.MULTDIV{multDivOp=oper, src=regOrMem (ty, opnd2)},an);
718 move(ty, resultReg, dstOpnd)
719 (* NOTE: on the x86-64, the IDIV instruction traps on overflow
720 if overflow then trap() else ()
721 *)
722 end
723
724 (* Optimize the special case for division *)
725 fun divide (ty, signed, e1, e2 as T.LI n') = (case analyze n'
726 of SOME(n, isneg, a, p) =>
727 if signed then
728 let val label = Label.anon ()
729 val reg1 = getExpr e1
730 val opnd1 = I.Direct (ty,reg1)
731 in
732 if isneg then
733 emit (I.UNARY { unOp = O.negOp ty,
734 opnd = opnd1 })
735 else if setZeroBit e1 then ()
736 else emit (O.cmpOp ty { lsrc = opnd1,
737 rsrc = I.Immed 0 });
738 emit (I.JCC { cond = I.GE,
739 opnd = immedLabel label });
740 emit (if a = 2 then
741 I.UNARY { unOp = O.incOp ty,
742 opnd = opnd1 }
743 else
744 I.BINARY { binOp = O.addOp ty,
745 src = I.Immed (a - 1),
746 dst = opnd1 });
747 defineLabel label;
748 shift (ty, O.sarOp, T.REG (ty, reg1), p)
749 end
750 else shift (ty, O.shrOp, e1, p)
751 | NONE =>
752 divrem(ty, signed, e1, e2, rax ty))
753 | divide (ty, signed, e1, e2) =
754 divrem (ty, signed, e1, e2, rax ty)
755
756 (* rem never causes overflow *)
757 fun rem (ty, signed, e1, e2 as T.LI n') = (case analyze n'
758 of SOME(n, isneg, a, _) =>
759 if signed then
760 (* The following logic should work uniformely
761 * for both isneg and not isneg. It only uses
762 * the absolute value (a) of the divisor.
763 * Here is the formula:
764 * let p be a power of two and a = abs(p):
765 *
766 * x % p = x - ((x < 0 ? x + a - 1 : x) & (-a))
767 *
768 * (That's what GCC seems to do.)
769 *)
770 let val r1 = getExpr e1
771 val o1 = I.Direct (ty,r1)
772 val rt = newReg ()
773 val tmp = I.Direct (ty,rt)
774 val l = Label.anon ()
775 in
776 move (ty, o1, tmp);
777 if setZeroBit e1 then ()
778 else emit ((O.cmpOp ty) { lsrc = o1,
779 rsrc = I.Immed 0 });
780 emit (I.JCC { cond = I.GE,
781 opnd = immedLabel l });
782 emit (I.BINARY { binOp = O.addOp ty,
783 src = I.Immed (a - 1),
784 dst = tmp });
785 defineLabel l;
786 emit (I.BINARY { binOp = O.andOp ty,
787 src = I.Immed (~a),
788 dst = tmp });
789 binary (ty, O.subOp, T.REG (ty, r1), T.REG (ty, rt))
790 end
791 else
792 if isneg then
793 (* this is really strange... *)
794 divrem (ty, false, e1, e2, rdx ty)
795 else
796 (* 64BIT: FIXME what if n-1 does not fit in 32 bits? *)
797 binaryComm (ty, O.andOp, e1,
798 T.LI (T.I.fromInt32 (ty, n - 1)))
799 | NONE => divrem (ty, signed, e1, e2, rdx ty))
800 | rem(ty, signed, e1, e2) =
801 divrem(ty, signed, e1, e2, rdx ty)
802
803
804 (* unsigned integer multiplication *)
805 fun uMultiply0 (ty, e1, e2) =
806 (* note e2 can never be (I.Direct rdx) *)
807 (move(ty, operand ty e1, rax ty);
808 mark(I.MULTDIV{multDivOp=O.mul1Op ty,
809 src=regOrMem(ty, operand ty e2)},an);
810 move(ty, rax ty, dstOpnd)
811 )
812
813 fun uMultiply (ty, e1, e2 as T.LI n') = (case analyze n'
814 of SOME(_, false, _, p) => shift (ty, O.shlOp, e1, p)
815 | NONE => uMultiply0 (ty, e1, e2))
816 | uMultiply (ty, e1 as T.LI _, e2) = uMultiply (ty, e2, e1)
817 | uMultiply (ty, e1, e2) = uMultiply0 (ty, e1, e2)
818
819 (* signed integer multiplication:
820 * The only forms that are allowed that also sets the
821 * OF and CF flags are:
822 *
823 * (dst) (src1) (src2)
824 * imul r64, r64/m64, imm8
825 * (dst) (src)
826 * imul r64, imm8
827 * imul r64, imm32
828 * imul r64, r32/m64
829 * Note: destination must be a register!
830 *)
831 fun multiply (ty, e1, e2) =
832 dstMustBeReg(fn (dst, dstOpnd) =>
833 let fun doit(i1 as I.Immed _, i2 as I.Immed _) =
834 (move(ty, i1, dstOpnd);
835 mark(I.BINARY{binOp=O.imulOp ty, dst=dstOpnd, src=i2},an))
836 | doit(rm, i2 as I.Immed _) = doit(i2, rm)
837 | doit(imm as I.Immed i, rm) =
838 (case ty
839 of 32 => mark(I.MUL3{dst=dst, src1=rm, src2=i},an)
840 | 64 => mark(I.MULQ3{dst=dst, src1=rm, src2=i},an)
841 (* esac *))
842 | doit(r1 as I.Direct _, r2 as I.Direct _) =
843 (move(ty, r1, dstOpnd);
844 mark(I.BINARY{binOp=O.imulOp ty, dst=dstOpnd, src=r2},an))
845 | doit(r1 as I.Direct _, rm) =
846 (move(ty, r1, dstOpnd);
847 mark(I.BINARY{binOp=O.imulOp ty, dst=dstOpnd, src=rm},an))
848 | doit(rm, r as I.Direct _) = doit(r, rm)
849 | doit(rm1, rm2) =
850 if equalDst rm2 then
851 let val tmpR = newReg()
852 val tmp = I.Direct (ty,tmpR)
853 in move(ty, rm1, tmp);
854 mark(I.BINARY{binOp=O.imulOp ty, dst=tmp, src=rm2},an);
855 move(ty, tmp, dstOpnd)
856 end
857 else
858 (move(ty, rm1, dstOpnd);
859 mark(I.BINARY{binOp=O.imulOp ty, dst=dstOpnd, src=rm2},an)
860 )
861 val (opnd1, opnd2) = (operand ty e1, operand ty e2)
862 in doit(opnd1, opnd2)
863 end
864 )
865
866 fun multiply_notrap (ty, e1, e2 as T.LI n') = (case analyze n'
867 of SOME(_, isneg, _, p) => let
868 val r1 = getExpr e1
869 val o1 = I.Direct (ty,r1)
870 in
871 if isneg
872 then emit (I.UNARY { unOp = O.negOp ty, opnd = o1 })
873 else ();
874 shift (ty, O.shlOp, T.REG (ty, r1), p)
875 end
876 | NONE => multiply (ty, e1, e2))
877 | multiply_notrap (ty, e1 as T.LI _, e2) = multiply_notrap (ty, e2, e1)
878 | multiply_notrap (ty, e1, e2) = multiply (ty, e1, e2)
879
880 (* NOTE: cmovcc encodes its operand lengths implicitly in the operand names: i.e.,
881 * cmove %rax, %rbx
882 * *)
883 fun cmovcc (tyCond, tyCmp, cc, t1, t2, y, n) = let
884 fun gen (dstR, _) = let
885 val _ = expr' (tyCond, n, dstR, []) (* false branch *)
886 val src = regOrMem (tyCond, operand tyCond y) (* yes branch (note the ordering ) *)
887 val cc = cmp (true, tyCmp, cc, t1, t2, []) (* compare *)
888 in
889 mark (I.CMOV {cond=cond cc, src=src, dst=dstR}, an)
890 end
891 in
892 dstMustBeReg gen
893 end
894
895 (* conditional move for float comparisons *)
896 fun fcmovcc (tyCond, fty, cc, t1, t2, y, n) = dstMustBeReg (fn (dstR, _) => let
897 val _ = expr' (tyCond, n, dstR, []) (* false branch *)
898 val src = regOrMem (tyCond, operand tyCond y) (* true branch *)
899 fun j cc = mark (I.CMOV {cond=cc, src=src, dst=dstR}, an)
900 in
901 fbranch' (fty, cc, t1, t2, j)
902 end)
903
904 in
905 (case e
906 of T.REG (ty, r) => move' (ty, gpr (ty, r), dstOpnd, an)
907 | T.LI z => if (fitsIn32Bits z)
908 then move' (ty, I.Immed(toInt32 z), dstOpnd, an)
909 else move64' (I.Immed64(toInt64 z), dstOpnd, an)
910 | (T.CONST _ | T.LABEL _) =>
911 move' (ty, I.ImmedLabel e, dstOpnd, an)
912 | T.LABEXP le => move' (ty, I.ImmedLabel le, dstOpnd, an)
913 (* arithmetic operations *)
914 | T.ADD(ty, e1, e2 as T.LI n) => (case n
915 of 1 => unary(ty, O.incOp, e1)
916 | ~1 => unary(ty, O.decOp, e1)
917 | _ => addition (ty, e1, e2)
918 (* end case *))
919 | T.ADD(ty, e1 as T.LI n, e2) => (case n
920 of 1 => unary(ty, O.incOp, e2)
921 | ~1 => unary(ty, O.decOp, e2)
922 | _ => addition (ty, e1, e2)
923 (* end case *))
924 | T.ADD(ty, e1, e2) => addition (ty, e1, e2)
925 | T.SUB(ty, e1, e2 as T.LI n) => (case n
926 of 0 => expr' (ty, e1, dst, an)
927 | 1 => unary(ty, O.decOp, e1)
928 | ~1 => unary(ty, O.incOp, e1)
929 | _ => binary(ty, O.subOp, e1, e2)
930 (* end case *))
931 | T.SUB(ty, e1 as T.LI n, e2) => if n = 0
932 then unary(ty, O.negOp, e2)
933 else binary(ty, O.subOp, e1, e2)
934 | T.SUB(ty, e1, e2) => binary(ty, O.subOp, e1, e2)
935 (* unsigned *)
936 | T.MULU(ty, x, y) => uMultiply(ty, x, y)
937 | T.DIVU(ty, x, y) => divide(ty, false, x, y)
938 | T.REMU(ty, x, y) => rem(ty, false, x, y)
939 (* signed *)
940 | T.MULS(ty, x, y) => multiply_notrap (ty, x, y)
941 | T.DIVS(T.DIV_TO_ZERO, ty, x, y) => divide(ty, true, x, y)
942 | T.DIVS(T.DIV_TO_NEGINF, ty, x, y) => divinf (ty, x, y)
943 | T.REMS(T.DIV_TO_ZERO, ty, x, y) => rem(ty, true, x, y)
944 | T.REMS(T.DIV_TO_NEGINF, ty, x, y) => reminf (ty, x, y)
945 (* trapping *)
946 | T.ADDT(ty, x, y) => (binaryComm(ty, O.addOp, x, y); trap())
947 | T.SUBT(ty, T.LI 0, y) => (unary(ty, O.negOp, y); trap())
948 | T.SUBT(ty, x, y) => (binary(ty, O.subOp, x, y); trap())
949 | T.MULT(ty, x, y) => (multiply (ty, x, y); trap ())
950 | T.DIVT(T.DIV_TO_ZERO, ty, x, y) => divide(ty, true, x, y)
951 | T.DIVT(T.DIV_TO_NEGINF, ty, x, y) => divinf (ty, x, y)
952 (* bitwise operations *)
953 | T.ANDB(ty, x, y) => binaryComm(ty, O.andOp, x, y)
954 | T.ORB(ty, x, y) => binaryComm(ty, O.orOp, x, y)
955 | T.XORB(ty, x, y) => binaryComm(ty, O.xorOp, x, y)
956 | T.NOTB(ty, x) => unary(ty, O.notOp, x)
957 | T.SRA(ty, x, y) => shift(ty, O.sarOp, x, y)
958 | T.SRL(ty, x, y) => shift(ty, O.shrOp, x, y)
959 | T.SLL(ty, x, y) => shift(ty, O.shlOp, x, y)
960 (* loads *)
961 | T.LOAD (8, ea, mem) => genLoad (O.loadZXOp (8, 64), ea, mem)
962 | T.LOAD (16, ea, mem) => genLoad (O.loadZXOp (16, 64), ea, mem)
963 | T.LOAD (32, ea, mem) => genLoad (I.MOVL, ea, mem)
964 | T.LOAD (64, ea, mem) => genLoad (I.MOVQ, ea, mem)
965 (* sign-extended loads *)
966 | T.SX (tTy, fTy, x) =>
967 mark (I.MOVE {mvOp=O.loadSXOp (fTy, tTy), src=regOrMem(fTy, operand fTy x), dst=I.Direct(tTy, dst)},an)
968 (* there is no movslq instruction, but movl suffices. *)
969 | T.ZX(64, 32, e) =>
970 mark (I.MOVE {mvOp=I.MOVL, src=operand 32 e, dst=I.Direct(32, dst)},an)
971 (* zero-extended loads *)
972 | T.ZX(tTy, fTy, e) =>
973 mark (I.MOVE {mvOp=O.loadZXOp (fTy, tTy), src=regOrMem(fTy, operand fTy e), dst=I.Direct(tTy, dst)},an)
974 | T.CVTF2I (ty, roundingMd, fty, fExp) => let
975 (* FIXME: handle the rounding mode *)
976 val mvOp = (case (fty, ty)
977 of (64, 32) => I.CVTSD2SI
978 | (32, 32) => I.CVTSS2SI
979 | (64, 64) => I.CVTSD2SIQ
980 | (32, 64) => I.CVTSS2SIQ
981 (* end case *))
982 in
983 mark' (I.move {mvOp=mvOp, src=foperand(fty, fExp), dst=dstOpnd}, an)
984 end
985 | T.COND (tyCond, T.CMP (tyCmp, cc, t1, t2), y, n) =>
986 cmovcc (tyCond, tyCmp, cc, t1, t2, y, n)
987 | T.COND (tyCond, T.FCMP (fty, cc, t1, t2), y, n) =>
988 fcmovcc (tyCond, fty, cc, t1, t2, y, n)
989 | T.NEG (ty, x) => unary (ty, O.negOp, x)
990 | T.LET (s, e) => (stmt s; expr (e, dst, an))
991 | T.MARK (e, A.MARKREG f) => (f dst; expr' (ty, e, dst, an))
992 | T.MARK (e, a) => expr' (ty, e, dst, a :: an)
993 | T.PRED (e, c) => expr' (ty, e, dst, A.CTRLUSE c :: an)
994 | _ => raise Fail("Unsupported rexp: " ^ MLTreeUtils.rexpToString e)
995 (* end case *))
996 end (* expr' *)
997
998 and expr (e, dst, an) = expr' (TRS.size e, e, dst, an)
999
1000 and fcopy (fty, ds, rs, an) = let
1001 fun mvInstr {dst, src} =
1002 [I.fmove {fmvOp=O.fmovOp fty, dst=dst, src=src}]
1003 (* eliminate unnecessary copies *)
1004 val (ds, rs) = ListPair.unzip (List.filter (not o CB.sameColor) (ListPair.zip (ds, rs)))
1005 val stms = Shuffle.shuffle {mvInstr=mvInstr, ea=fpr} {dst=ds, src=rs, tmp=NONE}
1006 in
1007 emits stms
1008 end
1009
1010 (* put a floating-point expression into a register *)
1011 and fexpToReg (fty, e) = (case e
1012 of T.FREG (fty', r) => r
1013 | e => let
1014 val r = newFreg ()
1015 in
1016 fexpr (fty, r, e, []);
1017 r
1018 end
1019 (* end case *))
1020
1021 (* put a floating-point expression into an operand *)
1022 and foperand (fty, e) = (case e
1023 of T.FLOAD (fty, ea, mem) => address (ea, mem)
1024 | T.FREG (fty, r) => I.FDirect r
1025 | e => I.FDirect (fexpToReg (fty, e))
1026 (* end case *))
1027
1028 and falignedOperand (fty, e) = if floats16ByteAligned
1029 then foperand (fty, e)
1030 else I.FDirect (fexpToReg (fty, e))
1031
1032 (* SSE binary ops
1033 * (src) (dst)
1034 * binOp freg/m64, freg
1035 *)
1036 and fbinop (fty, binOp, a, b, d, an) = let
1037 (* try to move memory operands to src rather than dst if binOp is commutative *)
1038 val (a, b) = (case (binOp, a, b)
1039 of ( (I.ADDSS | I.ADDSD | I.MULSS | I.MULSD | I.XORPS | I.XORPD | I.ANDPS | I.ANDPD | I.ORPS | I.ORPD),
1040 T.FLOAD _,
1041 T.FREG _) => (b, a)
1042 | _ => (a, b)
1043 (* end case *))
1044 val src = foperand (fty, b)
1045 in
1046 fexpr (fty, d, a, []);
1047 mark (I.FBINOP {binOp=binOp, dst=d, src=src}, an)
1048 end
1049
1050 and fsqrt (fty, d, a, an) = let
1051 val s = falignedOperand (fty, a)
1052 (* TODO: allow the source operand to be a memory location
1053 * val aOpnd = foperand (fty, a)
1054 *)
1055 val oper = (case fty
1056 of 32 => I.FSQRTS
1057 | 64 => I.FSQRTD
1058 | _ => error "fsqrt"
1059 (* end case *))
1060 in
1061 mark (oper {src=s, dst=I.FDirect d}, an)
1062 end
1063
1064 and convertf2f (fromTy, toTy, e, d, an) = let
1065 val fmvOp = (case (fromTy, toTy)
1066 of (32, 64) => I.CVTSS2SD
1067 | (64, 32) => I.CVTSD2SS
1068 | _ => error "convertf2f"
1069 (* end case *))
1070 in
1071 mark (I.FMOVE {fmvOp=fmvOp, dst=I.FDirect d, src=foperand (fromTy, e)}, an)
1072 end (* convertf2f *)
1073
1074 and converti2f (fty, ity, e, d, an) = let
1075 val fmvOp = (case (ity, fty)
1076 of (32, 32) => I.CVTSI2SS
1077 | (32, 64) => I.CVTSI2SD
1078 | (64, 32) => I.CVTSI2SSQ
1079 | (64, 64) => I.CVTSI2SDQ
1080 (* end case *))
1081 val src = regOrMem (ity, operand ity e)
1082 in
1083 mark (I.FMOVE {fmvOp=fmvOp, dst=I.FDirect d, src=src}, an)
1084 end (* converti2f *)
1085
1086 and fexpr (fty, d, e, an) = ( (*floatingPointUsed := true;*)
1087 case e
1088 of T.FREG (_, r) => fcopy (fty, [d], [r], an)
1089 (* binary operators *)
1090 | T.FADD (_, a, b) => fbinop (fty, O.faddOp fty, a, b, d, an)
1091 | T.FSUB (_, a, b) => fbinop (fty, O.fsubOp fty, a, b, d, an)
1092 | T.FMUL (_, a, b) => fbinop (fty, O.fmulOp fty, a, b, d, an)
1093 | T.FDIV (_, a, b) => fbinop (fty, O.fdivOp fty, a, b, d, an)
1094 (* unary operators *)
1095 | T.FNEG (_, a) => let
1096 val fop = (case fty
1097 of 32 => I.XORPS
1098 | 64 => I.XORPD
1099 (* end case *))
1100 val r = newFreg ()
1101 val s = falignedOperand (fty, a)
1102 in
1103 (* Two old versions of this code: pre-r3081 and r3081+
1104 fload (fty, T.LABEL l, I.Region.memory, r, an);
1105 fload (fty, T.ADD (64, T.REG (64, C.rsp), T.LI 208), I.Region.memory, r, an);
1106 *)
1107 fload (fty, signBit fty, I.Region.memory, r, an);
1108 mark (I.FBINOP {binOp=fop, dst=r, src=s}, an);
1109 fcopy (fty, [d], [r], an)
1110 end
1111 | T.FABS (_, a) => let
1112 val fop = (case fty
1113 of 32 => I.ANDPS
1114 | 64 => I.ANDPD
1115 (* end case *))
1116 val r = newFreg ()
1117 val s = falignedOperand (fty, a)
1118 in
1119 (* Two old versions of this code: pre-r3081 and r3081+
1120 fload (fty, T.LABEL l, I.Region.memory, r, an);
1121 fload (fty, T.ADD (64, T.REG (64, C.rsp), T.LI 216), I.Region.memory, r, an);
1122 *)
1123 fload (fty, negateSignBit fty, I.Region.memory, r, an);
1124 mark (I.FBINOP {binOp=fop, dst=r, src=s}, an);
1125 fcopy (fty, [d], [r], an)
1126 end
1127 | T.FSQRT (fty, a) => fsqrt (fty, d, a, an)
1128 (* conversions *)
1129 | T.CVTF2F (fTy, tTy, e) => convertf2f (fTy, tTy, e, d, an)
1130 | T.CVTI2F (fty, ty, e) => converti2f (fty, ty, e, d, an)
1131 (* load *)
1132 | T.FLOAD (fty, ea, mem) => fload (fty, ea, mem, d, an)
1133 (* misc *)
1134 | T.FMARK (e, A.MARKREG f) => (f d; fexpr (fty, d, e, an))
1135 | T.FMARK (e, a) => fexpr (fty, d, e, a::an)
1136 | T.FPRED (e, c) => fexpr (fty, d, e, A.CTRLUSE c::an)
1137 | T.FEXT fexp => ExtensionComp.compileFext (reducer()) {e=fexp, fd=d, an=an}
1138 | _ => error "fexpr"
1139 (* end case *))
1140
1141 and fload (fty, ea, mem, d, an) = mark (
1142 I.FMOVE {fmvOp=O.fmovOp fty, dst=I.FDirect d,
1143 src=address (ea, mem)}, an)
1144
1145 and fstore (fty, ea, mem, e, an) = mark (
1146 I.FMOVE {fmvOp=O.fmovOp fty, dst=address (ea, mem),
1147 src=I.FDirect (fexpToReg (fty, e))}, an)
1148
1149 and call (ea, flow, def, use, mem, cutsTo, an, pops) = let
1150 fun return(set, []) = set
1151 | return(set, a::an) = (case #peek A.RETURN_ARG a
1152 of SOME r => return(CB.CellSet.add(r, set), an)
1153 | NONE => return(set, an)
1154 (* end case *))
1155 val dst = (case ea
1156 of T.LABEL lab => I.ImmedLabel ea
1157 | T.LABEXP le => I.ImmedLabel le
1158 | _ => operand 64 ea
1159 (* end case *))
1160 in
1161 mark(I.CALL{
1162 opnd=dst, defs=cellset def, uses=cellset use,
1163 return=return (C.empty, an), cutsTo=cutsTo, mem=mem,
1164 pops=pops
1165 }, an)
1166 end (* call *)
1167
1168 and jmp (lexp as T.LABEL lab, labs, an) =
1169 mark (I.JMP (I.ImmedLabel lexp, [lab]), an)
1170 | jmp (T.LABEXP le, labs, an) = mark (I.JMP (I.ImmedLabel le, labs), an)
1171 | jmp (ea, labs, an) = mark (I.JMP (operand 64 ea, labs), an)
1172
1173 (* 8-bit store operations must use one of %al, %bl, %cl, or %dl as the source;
1174 * we chose %al (aka %rax).
1175 *)
1176 and doStore 8 (ea, d, mem, an) = let
1177 val src = (case immedOrReg (8, operand 8 d)
1178 of src as I.Direct(_, r) => if CB.sameColor(r, C.rax)
1179 then src
1180 else (move(64, src, rax 64); rax 8)
1181 | src => src
1182 (* end case *))
1183 in
1184 move' (8, src, address (ea, mem), an)
1185 end
1186 | doStore ty (ea, d, mem, an) =
1187 move' (ty, immedOrReg (ty, operand ty d), address (ea, mem), an)
1188
1189 and binaryMem(ty, binOp, src, dst, mem, an) =
1190 mark(I.BINARY{binOp=binOp, src=immedOrReg(ty, operand ty src),
1191 dst=address (dst,mem)}, an)
1192 and unaryMem(ty, unOp, opnd, mem, an) =
1193 mark(I.UNARY{unOp=unOp, opnd=address (opnd,mem)}, an)
1194 and isOne (T.LI 1) = true
1195 | isOne _ = false
1196
1197 and store (ty, ea, d, mem, opcodes : O.opcodes, doStore, an) = let
1198 val {INC,DEC,ADD,SUB,NOT,NEG,SHL,SHR,SAR,OR,AND,XOR, ...} = opcodes
1199 fun default () = doStore (ea, d, mem, an)
1200 fun binary1 (t, t', unary, binary, ea', x) =
1201 if t = ty andalso t' = ty then
1202 if MLTreeUtils.eqRexp(ea, ea') then
1203 if isOne x then unaryMem(ty, unary, ea, mem, an)
1204 else binaryMem(ty, binary, x, ea, mem, an)
1205 else default()
1206 else default()
1207 fun unary(t,unOp, ea') =
1208 if t = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1209 unaryMem(ty, unOp, ea, mem, an)
1210 else default()
1211 fun binary(t,t',binOp,ea',x) =
1212 if t = ty andalso t' = ty andalso
1213 MLTreeUtils.eqRexp(ea, ea') then
1214 binaryMem(ty, binOp, x, ea, mem, an)
1215 else default()
1216 fun binaryCom1(t,unOp,binOp,x,y) =
1217 if t = ty then let
1218 fun again() = (case y
1219 of T.LOAD(ty',ea',_) =>
1220 if ty' = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1221 if isOne x then unaryMem(ty, unOp, ea, mem, an)
1222 else binaryMem(ty, binOp,x,ea,mem,an)
1223 else default()
1224 | _ => default()
1225 (* end case *))
1226 in
1227 (case x
1228 of T.LOAD(ty',ea',_) => if ty' = ty
1229 andalso MLTreeUtils.eqRexp(ea, ea') then
1230 if isOne y then unaryMem(ty, unOp, ea, mem, an)
1231 else binaryMem(ty, binOp,y,ea,mem,an)
1232 else again()
1233 | _ => again()
1234 (* end case *))
1235 end
1236 else default()
1237 fun binaryCom(t,binOp,x,y) = if t = ty then
1238 let fun again() = (case y
1239 of T.LOAD(ty',ea',_) =>
1240 if ty' = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1241 binaryMem(ty, binOp,x,ea,mem,an)
1242 else default()
1243 | _ => default()
1244 (* end case *))
1245 in
1246 (case x
1247 of T.LOAD(ty',ea',_) =>
1248 if ty' = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1249 binaryMem(ty, binOp,y,ea,mem,an)
1250 else again()
1251 | _ => again()
1252 (* end case *))
1253 end
1254 else default()
1255 in
1256 case d
1257 of T.ADD(t,x,y) => binaryCom1(t,INC,ADD,x,y)
1258 | T.SUB(t,T.LOAD(t',ea',_),x) => binary1(t,t',DEC,SUB,ea',x)
1259 | T.ORB(t,x,y) => binaryCom(t,OR,x,y)
1260 | T.ANDB(t,x,y) => binaryCom(t,AND,x,y)
1261 | T.XORB(t,x,y) => binaryCom(t,XOR,x,y)
1262 | T.SLL(t,T.LOAD(t',ea',_),x) => binary(t,t',SHL,ea',x)
1263 | T.SRL(t,T.LOAD(t',ea',_),x) => binary(t,t',SHR,ea',x)
1264 | T.SRA(t,T.LOAD(t',ea',_),x) => binary(t,t',SAR,ea',x)
1265 | T.NEG(t,T.LOAD(t',ea',_)) => unary(t,NEG,ea')
1266 | T.NOTB (t, T.LOAD(t',ea',_)) => unary(t,NOT,ea')
1267 | _ => default ()
1268 (* end case *)
1269 end (* store *)
1270
1271 (* floating-point branch for
1272 * if (t1 fcc t2)
1273 * goto lab
1274 *)
1275 and fbranch (fty, fcc, t1, t2, lab, an) = let
1276 fun j cc = mark (I.JCC {cond=cc, opnd=immedLabel lab}, an)
1277 in
1278 fbranch' (fty, fcc, t1, t2, j)
1279 end
1280
1281 and fbranch' (fty, fcc, x, y, j) = let
1282 (* the condition code bits are set as follows:
1283 *
1284 * P Z C
1285 * x < y 0 0 1
1286 * x > y 0 0 0
1287 * x = y 0 1 0
1288 * unordered 1 1 1
1289 *
1290 * and the AMD64 condition tests are as follows:
1291 *
1292 * A - 0 0 -- implies ordered
1293 * AE - - 0
1294 * B - - 1
1295 * BE - 1 -
1296 * or - - 1
1297 * EQ - 1 -
1298 * NE - 0 -
1299 * NP 0 - -
1300 * P 1 - -
1301 *)
1302 fun branch fcc = (case fcc
1303 of T.== => orderedOnly I.EQ
1304 | T.?<> => (j I.P; j I.NE)
1305 | T.? => j I.P
1306 | T.<=> => j I.NP
1307 | T.> => j I.A
1308 | T.?<= => (j I.P; j I.BE)
1309 | T.>= => orderedOnly I.AE
1310 | T.?< => (j I.P; j I.BE)
1311 | T.< => orderedOnly I.B
1312 | T.?>= => (j I.P; j I.AE)
1313 | T.<= => orderedOnly I.BE
1314 | T.?> => (j I.P; j I.A)
1315 | T.<> => orderedOnly I.NE
1316 | T.?= => j I.EQ
1317 | _ => error(concat["fbranch(", T.Basis.fcondToString fcc, ")"])
1318 (* end case *))
1319 (* branch on condition if ordered *)
1320 (* FIXME: the emitted jump does not have annotations! *)
1321 and orderedOnly fcc = let
1322 val lab = Label.anon()
1323 in
1324 emit (I.JCC{cond=I.P, opnd=immedLabel lab});
1325 j fcc;
1326 defineLabel lab
1327 end
1328 (* compare for condition (x op y)
1329 *
1330 * (y) (x)
1331 * ucomiss/d xmm1/m32, xmm2
1332 *)
1333 fun compare () = let
1334 val x = foperand (fty, x)
1335 val y = foperand (fty, y)
1336 fun cmp (x, y, fcc) = (
1337 emit (I.FCOM {comOp=O.ucomOp fty, src=y, dst=x});
1338 fcc)
1339 in
1340 (case (x, y)
1341 of (I.FDirect xReg, I.FDirect _) => cmp (xReg, y, fcc)
1342 | (mem, I.FDirect yReg) =>
1343 cmp (yReg, x, T.Basis.swapFcond fcc)
1344 | (I.FDirect xReg, mem) => cmp (xReg, y, fcc)
1345 | _ => let
1346 val xReg = newFreg ()
1347 val xTmp = I.FDirect xReg
1348 in
1349 emit (I.FMOVE {fmvOp=O.fmovOp fty, src=x, dst=xTmp});
1350 cmp (xReg, y, fcc)
1351 end
1352 (* end case *))
1353 end (* compare *)
1354 in
1355 branch (compare ())
1356 end (* fbranch' *)
1357
1358 (* %eflags <- src *)
1359 and moveToEflags src =
1360 if CB.sameColor(src, C.eflags) then ()
1361 else (move(32, I.Direct (32,src), rax 32); emit(I.LAHF))
1362
1363 (* dst <- %eflags *)
1364 and moveFromEflags dst =
1365 if CB.sameColor(dst, C.eflags) then ()
1366 else (emit(I.SAHF); move(32, rax 32, I.Direct (32,dst)))
1367
1368 (* Emit a test.
1369 * The available modes are
1370 * r/m, r
1371 * r/m, imm
1372 * On selecting the right instruction: TESTQ/TESTL/TESTW/TESTB.
1373 * When anding an operand with a constant
1374 * that fits within 8 (or 16) bits, it is possible to use TESTB,
1375 * (or TESTW) instead of TESTL. Because amd64 is little endian,
1376 * this works for memory operands too. However, with TESTB, it is
1377 * not possible to use registers other than
1378 * AL, CL, BL, DL, and AH, CH, BH, DH. So, the best way is to
1379 * perform register allocation first, and if the operand registers
1380 * are one of RAX, RCX, RBX, or RDX, replace the TESTL instruction
1381 * by TESTB.
1382 *)
1383 and test(ty, testopcode, a, b, an) = let
1384 val (_, opnd1, opnd2) = commuteComparison(ty, T.EQ, true, a, b)
1385 (* translate r, r/m => r/m, r *)
1386 val (opnd1, opnd2) =
1387 if isMemOpnd opnd2 then (opnd2, opnd1) else (opnd1, opnd2)
1388 in
1389 mark (testopcode {lsrc=opnd1, rsrc=opnd2}, an)
1390 end
1391
1392 (* generate a real comparison; return the real cc used *)
1393 and genCmp (ty, swapable, cc, a, b, an) = let
1394 val (cc, opnd1, opnd2) = commuteComparison(ty, cc, swapable, a, b)
1395 in
1396 (case ty
1397 of 8 => mark(I.CMPB{lsrc=opnd1, rsrc=opnd2}, an)
1398 | 16 => mark(I.CMPW{lsrc=opnd1, rsrc=opnd2}, an)
1399 | 32 => mark(I.CMPL{lsrc=opnd1, rsrc=opnd2}, an)
1400 | 64 => mark(I.CMPQ{lsrc=opnd1, rsrc=opnd2}, an)
1401 (* esac *));
1402 cc
1403 end
1404
1405 (* Give a and b which are the operands to a comparison (or test)
1406 * Return the appropriate condition code and operands.
1407 * The available modes are:
1408 * r/m, imm
1409 * r/m, r
1410 * r, r/m
1411 *)
1412 and commuteComparison (ty, cc, swapable, a, b) = let
1413 val (opnd1, opnd2) = (operand ty a, operand ty b)
1414 in (* Try to fold in the operands whenever possible *)
1415 case (isImmediate opnd1, isImmediate opnd2)
1416 of (true, true) => (cc, moveToReg (ty, opnd1), opnd2)
1417 | (true, false) => if swapable
1418 then (T.Basis.swapCond cc, opnd2, opnd1)
1419 else (cc, moveToReg (ty, opnd1), opnd2)
1420 | (false, true) => (cc, opnd1, opnd2)
1421 | (false, false) => (case (opnd1, opnd2)
1422 of (_, I.Direct _) => (cc, opnd1, opnd2)
1423 | (I.Direct _, _) => (cc, opnd1, opnd2)
1424 | (_, _) => (cc, moveToReg (ty, opnd1), opnd2)
1425 (* end case *))
1426 (* end case *)
1427 end (* commuteComparison *)
1428
1429 (* generate a condition code expression
1430 * The zero is for setting the condition code!
1431 * I have no idea why this is used.
1432 *)
1433 and doCCexpr(T.CMP(ty, cc, t1, t2), dst, an) = (
1434 cmp(false, ty, cc, t1, t2, an);
1435 moveFromEflags dst)
1436 | doCCexpr(T.CC(cond,rs), dst, an) =
1437 if CB.sameColor(rs,C.eflags) orelse CB.sameColor(dst,C.eflags)
1438 then (moveToEflags rs; moveFromEflags dst)
1439 else move'(64, I.Direct (64,rs), I.Direct (64,dst), an)
1440 | doCCexpr(T.CCMARK(e,A.MARKREG f),dst,an) = (f dst; doCCexpr(e,dst,an))
1441 | doCCexpr(T.CCMARK(e,a), dst, an) = doCCexpr(e,dst,a::an)
1442 | doCCexpr(T.CCEXT e, cd, an) =
1443 ExtensionComp.compileCCext (reducer()) {e=e, ccd=cd, an=an}
1444 | doCCexpr _ = error "doCCexpr"
1445
1446 (* Compare an expression with zero.
1447 * On the amd64, TEST is superior to AND for doing the same thing,
1448 * since it doesn't need to write out the result in a register.
1449 *)
1450 and cmpWithZero(cc as (T.EQ | T.NE), e as T.ANDB(ty, a, b), an) =
1451 (case ty
1452 of 8 => test(ty, I.TESTB, a, b, an)
1453 | 16 => test(ty, I.TESTW, a, b, an)
1454 | 32 => test(ty, I.TESTL, a, b, an)
1455 | 64 => test(ty, I.TESTQ, a, b, an)
1456 | _ => expr (e, newReg(), an);
1457 cc)
1458 | cmpWithZero(cc, e, an) = (expr (e, newReg(), an); cc)
1459
1460 (* generate a comparison and sets the condition code;
1461 * return the actual cc used. If the flag swapable is true,
1462 * we can also reorder the operands.
1463 *)
1464 and cmp (swapable, ty, cc, t1, t2, an) = let
1465 (* == and <> can be always be reordered *)
1466 val swapable = swapable orelse cc = T.EQ orelse cc = T.NE
1467 in (* Sometimes the comparison is not necessary because
1468 * the bits are already set!
1469 *)
1470 if isZero t1 andalso setZeroBit2 t2 then
1471 if swapable then
1472 cmpWithZero(T.Basis.swapCond cc, t2, an)
1473 else (* can't reorder the comparison! *)
1474 genCmp(ty, false, cc, t1, t2, an)
1475 else if isZero t2 andalso setZeroBit2 t1 then
1476 cmpWithZero(cc, t1, an)
1477 else genCmp(ty, swapable, cc, t1, t2, an)
1478 end
1479
1480 and branch (T.CMP(ty, cc, t1, t2), lab, an) = let
1481 val cc = cmp (true, ty, cc, t1, t2, [])
1482 in
1483 mark (I.JCC{cond=cond cc, opnd=immedLabel lab}, an)
1484 end
1485 | branch (T.FCMP(fty, fcc, t1, t2), lab, an) =
1486 fbranch (fty, fcc, t1, t2, lab, an)
1487 | branch (ccexp, lab, an) = (
1488 doCCexpr(ccexp, C.eflags, []);
1489 mark(I.JCC{cond=cond(Gen.condOf ccexp), opnd=immedLabel lab}, an))
1490
1491 and stmt' (s, an) = (case s
1492 of T.MV (ty, d, e) => expr' (ty, e, d, an)
1493 | T.FMV (fty, d, e) => fexpr (fty, d, e, an)
1494 | T.CCMV (ccd, e) => doCCexpr (e, ccd, an)
1495 | T.COPY (ty, dst, src) => copy (ty, dst, src, an)
1496 | T.FCOPY (fty, dst, src) => fcopy (fty, dst, src, an)
1497 | T.JMP (e, labs) => jmp (e, labs, an)
1498 | T.CALL {funct, targets, defs, uses, region, pops, ...} =>
1499 call (funct, targets, defs, uses, region, [], an, pops)
1500 | T.FLOW_TO(T.CALL{funct, targets, defs, uses, region, pops, ...},
1501 cutTo) =>
1502 call (funct, targets, defs, uses, region, cutTo, an, pops)
1503 | T.RET _ => mark (I.RET NONE, an)
1504 | T.STORE (ty, ea, d, mem) =>
1505 store (ty, ea, d, mem, O.opcodes ty, doStore ty, an)
1506 | T.FSTORE (fty, ea, e, mem) => fstore (fty, ea, mem, e, an)
1507 | T.BCC(cc, lab) => branch (cc, lab, an)
1508 | T.DEFINE l => defineLabel l
1509 | T.LIVE s => mark' (I.LIVE{regs=cellset s,spilled=C.empty},an)
1510 | T.KILL s => mark' (I.KILL{regs=cellset s,spilled=C.empty},an)
1511 | T.ANNOTATION(s, a) => stmt' (s, a::an)
1512 | T.EXT s =>
1513 ExtensionComp.compileSext (reducer ()) {stm=s, an=an}
1514 | s => app stmt (Gen.compileStm s)
1515 (* end case *))
1516 and stmt s = stmt' (s, [])
1517
1518 and beginCluster' _ = (
1519 trapLabel := NONE;
1520 beginCluster 0)
1521
1522 and endCluster' a = (
1523 case !trapLabel
1524 of NONE => ()
1525 | SOME(_, lab) => (
1526 defineLabel lab;
1527 (* signal an overflow exception (code 4) *)
1528 emit(I.INT 0w4))
1529 (* end case *);
1530 (* If floating point has been used allocate an extra
1531 * register just in case we didn't use any explicit register *)
1532 ignore (newFreg ());
1533 endCluster a)
1534
1535 and ccExpr e = error "ccExpr"
1536
1537 and reduceOpnd (I.Direct(ty, r)) = r
1538 | reduceOpnd opnd = let
1539 val dst = newReg()
1540 in
1541 move(64, opnd, I.Direct(64, dst)); dst
1542 end
1543
1544 and reducer () = TS.REDUCER{
1545 reduceRexp = genExpr,
1546 reduceFexp = fn e => fexpToReg (64, e),
1547 reduceCCexp = ccExpr,
1548 reduceStm = stmt',
1549 operand = operand 64,
1550 reduceOperand = reduceOpnd,
1551 addressOf = fn e => address(e, I.Region.memory), (*XXX*)
1552 emit = mark',
1553 instrStream = instrStream,
1554 mltreeStream = self()
1555 }
1556
1557 and self () = TS.S.STREAM {
1558 beginCluster=beginCluster',
1559 endCluster=endCluster',
1560 emit=stmt,
1561 pseudoOp=pseudoOp,
1562 defineLabel=defineLabel,
1563 entryLabel=entryLabel,
1564 comment=comment,
1565 annotation=annotation,
1566 getAnnotations=getAnnotations,
1567 exitBlock=exitBlock o cellset}
1568 in
1569 self ()
1570 end (* selectInstructions *)
1571
1572 end (* AMD64Gen *)