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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 933 - (view) (download)

1 : leunga 583 (*
2 : monnier 247 *
3 :     * COPYRIGHT (c) 1998 Bell Laboratories.
4 : george 545 *
5 :     * This is a revised version that takes into account of
6 :     * the extended x86 instruction set, and has better handling of
7 :     * non-standard types. I've factored out the integer/floating point
8 :     * comparison code, added optimizations for conditional moves.
9 :     * The latter generates SETcc and CMOVcc (Pentium Pro only) instructions.
10 :     * To avoid problems, I have tried to incorporate as much of
11 :     * Lal's original magic incantations as possible.
12 : monnier 247 *
13 : george 545 * Some changes:
14 :     *
15 :     * 1. REMU/REMS/REMT are now supported
16 :     * 2. COND is supported by generating SETcc and/or CMOVcc; this
17 :     * may require at least a Pentium II to work.
18 :     * 3. Division by a constant has been optimized. Division by
19 :     * a power of 2 generates SHRL or SARL.
20 :     * 4. Better addressing mode selection has been implemented. This should
21 :     * improve array indexing on SML/NJ.
22 :     * 5. Generate testl/testb instead of andl whenever appropriate. This
23 :     * is recommended by the Intel Optimization Guide and seems to improve
24 :     * boxity tests on SML/NJ.
25 : leunga 731 *
26 :     * More changes for floating point:
27 :     * A new mode is implemented which generates pseudo 3-address instructions
28 :     * for floating point. These instructions are register allocated the
29 :     * normal way, with the virtual registers mapped onto a set of pseudo
30 :     * %fp registers. These registers are then mapped onto the %st registers
31 :     * with a new postprocessing phase.
32 :     *
33 : george 545 * -- Allen
34 : monnier 247 *)
35 : george 545 local
36 :     val rewriteMemReg = true (* should we rewrite memRegs *)
37 : leunga 731 val enableFastFPMode = true (* set this to false to disable the mode *)
38 : george 545 in
39 :    
40 : monnier 247 functor X86
41 :     (structure X86Instr : X86INSTR
42 : leunga 797 structure MLTreeUtils : MLTREE_UTILS
43 : george 933 where T = X86Instr.T
44 : george 555 structure ExtensionComp : MLTREE_EXTENSION_COMP
45 : george 933 where I = X86Instr and T = X86Instr.T
46 : george 545 datatype arch = Pentium | PentiumPro | PentiumII | PentiumIII
47 :     val arch : arch ref
48 : leunga 593 val cvti2f :
49 : leunga 815 {ty: X86Instr.T.ty,
50 :     src: X86Instr.operand,
51 :     (* source operand, guaranteed to be non-memory! *)
52 :     an: Annotations.annotations ref (* cluster annotations *)
53 :     } ->
54 : leunga 593 {instrs : X86Instr.instruction list,(* the instructions *)
55 :     tempMem: X86Instr.operand, (* temporary for CVTI2F *)
56 :     cleanup: X86Instr.instruction list (* cleanup code *)
57 :     }
58 : leunga 731 (* When the following flag is set, we allocate floating point registers
59 :     * directly on the floating point stack
60 :     *)
61 :     val fast_floating_point : bool ref
62 : george 545 ) : sig include MLTREECOMP
63 :     val rewriteMemReg : bool
64 :     end =
65 : monnier 247 struct
66 : leunga 775 structure I = X86Instr
67 :     structure T = I.T
68 : monnier 429 structure S = T.Stream
69 : george 545 structure C = I.C
70 :     structure Shuffle = Shuffle(I)
71 : monnier 247 structure W32 = Word32
72 : george 545 structure LE = I.LabelExp
73 :     structure A = MLRiscAnnotations
74 : george 909 structure CFG = ExtensionComp.CFG
75 : george 889 structure CB = CellsBasis
76 : monnier 247
77 : george 909 type instrStream = (I.instruction,C.cellset,CFG.cfg) T.stream
78 :     type mltreeStream = (T.stm,T.mlrisc list,CFG.cfg) T.stream
79 : leunga 565
80 :     datatype kind = REAL | INTEGER
81 : george 545
82 :     structure Gen = MLTreeGen
83 :     (structure T = T
84 :     val intTy = 32
85 :     val naturalWidths = [32]
86 :     datatype rep = SE | ZE | NEITHER
87 :     val rep = NEITHER
88 :     )
89 :    
90 : monnier 411 fun error msg = MLRiscErrorMsg.error("X86",msg)
91 : monnier 247
92 : george 545 (* Should we perform automatic MemReg translation?
93 :     * If this is on, we can avoid doing RewritePseudo phase entirely.
94 :     *)
95 :     val rewriteMemReg = rewriteMemReg
96 : leunga 731
97 :     (* The following hardcoded *)
98 : leunga 744 fun isMemReg r = rewriteMemReg andalso
99 : george 889 let val r = CB.registerNum r
100 : leunga 744 in r >= 8 andalso r < 32
101 :     end
102 : leunga 731 fun isFMemReg r = if enableFastFPMode andalso !fast_floating_point
103 : george 889 then let val r = CB.registerNum r
104 : leunga 744 in r >= 8 andalso r < 32 end
105 : leunga 731 else true
106 : leunga 744 val isAnyFMemReg = List.exists (fn r =>
107 : george 889 let val r = CB.registerNum r
108 : leunga 744 in r >= 8 andalso r < 32 end
109 :     )
110 : monnier 247
111 : george 555 val ST0 = C.ST 0
112 :     val ST7 = C.ST 7
113 : leunga 797 val one = T.I.int_1
114 : george 555
115 : leunga 797 val opcodes8 = {INC=I.INCB,DEC=I.DECB,ADD=I.ADDB,SUB=I.SUBB,
116 :     NOT=I.NOTB,NEG=I.NEGB,
117 :     SHL=I.SHLB,SHR=I.SHRB,SAR=I.SARB,
118 :     OR=I.ORB,AND=I.ANDB,XOR=I.XORB}
119 :     val opcodes16 = {INC=I.INCW,DEC=I.DECW,ADD=I.ADDW,SUB=I.SUBW,
120 :     NOT=I.NOTW,NEG=I.NEGW,
121 :     SHL=I.SHLW,SHR=I.SHRW,SAR=I.SARW,
122 :     OR=I.ORW,AND=I.ANDW,XOR=I.XORW}
123 :     val opcodes32 = {INC=I.INCL,DEC=I.DECL,ADD=I.ADDL,SUB=I.SUBL,
124 :     NOT=I.NOTL,NEG=I.NEGL,
125 :     SHL=I.SHLL,SHR=I.SHRL,SAR=I.SARL,
126 :     OR=I.ORL,AND=I.ANDL,XOR=I.XORL}
127 :    
128 : george 545 (*
129 :     * The code generator
130 :     *)
131 : monnier 411 fun selectInstructions
132 : george 545 (instrStream as
133 : leunga 815 S.STREAM{emit,defineLabel,entryLabel,pseudoOp,annotation,getAnnotations,
134 : leunga 744 beginCluster,endCluster,exitBlock,comment,...}) =
135 : george 545 let exception EA
136 : monnier 411
137 : george 545 (* label where a trap is generated -- one per cluster *)
138 :     val trapLabel = ref (NONE: (I.instruction * Label.label) option)
139 : monnier 247
140 : leunga 731 (* flag floating point generation *)
141 :     val floatingPointUsed = ref false
142 :    
143 : george 545 (* effective address of an integer register *)
144 : leunga 731 fun IntReg r = if isMemReg r then I.MemReg r else I.Direct r
145 :     and RealReg r = if isFMemReg r then I.FDirect r else I.FPR r
146 : monnier 411
147 : george 545 (* Add an overflow trap *)
148 :     fun trap() =
149 :     let val jmp =
150 :     case !trapLabel of
151 : george 909 NONE => let val label = Label.label "trap" ()
152 : george 545 val jmp = I.JCC{cond=I.O,
153 : leunga 775 opnd=I.ImmedLabel(T.LABEL label)}
154 : george 545 in trapLabel := SOME(jmp, label); jmp end
155 :     | SOME(jmp, _) => jmp
156 :     in emit jmp end
157 : monnier 411
158 : george 545 val newReg = C.newReg
159 :     val newFreg = C.newFreg
160 : monnier 247
161 : leunga 731 fun fsize 32 = I.FP32
162 :     | fsize 64 = I.FP64
163 :     | fsize 80 = I.FP80
164 :     | fsize _ = error "fsize"
165 :    
166 : george 545 (* mark an expression with a list of annotations *)
167 :     fun mark'(i,[]) = i
168 :     | mark'(i,a::an) = mark'(I.ANNOTATION{i=i,a=a},an)
169 : monnier 247
170 : george 545 (* annotate an expression and emit it *)
171 :     fun mark(i,an) = emit(mark'(i,an))
172 : monnier 247
173 : leunga 731 val emits = app emit
174 :    
175 : george 545 (* emit parallel copies for integers
176 :     * Translates parallel copies that involve memregs into
177 :     * individual copies.
178 :     *)
179 :     fun copy([], [], an) = ()
180 :     | copy(dst, src, an) =
181 :     let fun mvInstr{dst as I.MemReg rd, src as I.MemReg rs} =
182 : george 889 if CB.sameColor(rd,rs) then [] else
183 : george 545 let val tmpR = I.Direct(newReg())
184 :     in [I.MOVE{mvOp=I.MOVL, src=src, dst=tmpR},
185 :     I.MOVE{mvOp=I.MOVL, src=tmpR, dst=dst}]
186 :     end
187 :     | mvInstr{dst=I.Direct rd, src=I.Direct rs} =
188 : george 889 if CB.sameColor(rd,rs) then []
189 : george 545 else [I.COPY{dst=[rd], src=[rs], tmp=NONE}]
190 :     | mvInstr{dst, src} = [I.MOVE{mvOp=I.MOVL, src=src, dst=dst}]
191 :     in
192 : leunga 731 emits (Shuffle.shuffle{mvInstr=mvInstr, ea=IntReg}
193 : leunga 744 {tmp=SOME(I.Direct(newReg())),
194 : george 545 dst=dst, src=src})
195 :     end
196 :    
197 :     (* conversions *)
198 :     val itow = Word.fromInt
199 :     val wtoi = Word.toInt
200 : george 761 fun toInt32 i = T.I.toInt32(32, i)
201 : george 545 val w32toi32 = Word32.toLargeIntX
202 :     val i32tow32 = Word32.fromLargeInt
203 : monnier 247
204 : george 545 (* One day, this is going to bite us when precision(LargeInt)>32 *)
205 :     fun wToInt32 w = Int32.fromLarge(Word32.toLargeIntX w)
206 : monnier 247
207 : george 545 (* some useful registers *)
208 :     val eax = I.Direct(C.eax)
209 :     val ecx = I.Direct(C.ecx)
210 :     val edx = I.Direct(C.edx)
211 : monnier 247
212 : leunga 775 fun immedLabel lab = I.ImmedLabel(T.LABEL lab)
213 : george 545
214 :     (* Is the expression zero? *)
215 : george 761 fun isZero(T.LI z) = T.I.isZero z
216 : george 545 | isZero(T.MARK(e,a)) = isZero e
217 :     | isZero _ = false
218 :     (* Does the expression set the zero bit?
219 :     * WARNING: we assume these things are not optimized out!
220 :     *)
221 :     fun setZeroBit(T.ANDB _) = true
222 :     | setZeroBit(T.ORB _) = true
223 :     | setZeroBit(T.XORB _) = true
224 :     | setZeroBit(T.SRA _) = true
225 :     | setZeroBit(T.SRL _) = true
226 :     | setZeroBit(T.SLL _) = true
227 : leunga 695 | setZeroBit(T.SUB _) = true
228 :     | setZeroBit(T.ADDT _) = true
229 :     | setZeroBit(T.SUBT _) = true
230 : george 545 | setZeroBit(T.MARK(e, _)) = setZeroBit e
231 :     | setZeroBit _ = false
232 : monnier 247
233 : leunga 695 fun setZeroBit2(T.ANDB _) = true
234 :     | setZeroBit2(T.ORB _) = true
235 :     | setZeroBit2(T.XORB _) = true
236 :     | setZeroBit2(T.SRA _) = true
237 :     | setZeroBit2(T.SRL _) = true
238 :     | setZeroBit2(T.SLL _) = true
239 :     | setZeroBit2(T.ADD(32, _, _)) = true (* can't use leal! *)
240 :     | setZeroBit2(T.SUB _) = true
241 :     | setZeroBit2(T.ADDT _) = true
242 :     | setZeroBit2(T.SUBT _) = true
243 :     | setZeroBit2(T.MARK(e, _)) = setZeroBit2 e
244 :     | setZeroBit2 _ = false
245 :    
246 : leunga 731 (* emit parallel copies for floating point
247 :     * Normal version.
248 :     *)
249 :     fun fcopy'(fty, [], [], _) = ()
250 :     | fcopy'(fty, dst as [_], src as [_], an) =
251 : george 545 mark(I.FCOPY{dst=dst,src=src,tmp=NONE}, an)
252 : leunga 731 | fcopy'(fty, dst, src, an) =
253 : george 545 mark(I.FCOPY{dst=dst,src=src,tmp=SOME(I.FDirect(newFreg()))}, an)
254 : monnier 247
255 : leunga 731 (* emit parallel copies for floating point.
256 :     * Fast version.
257 :     * Translates parallel copies that involve memregs into
258 :     * individual copies.
259 :     *)
260 :    
261 :     fun fcopy''(fty, [], [], _) = ()
262 :     | fcopy''(fty, dst, src, an) =
263 :     if true orelse isAnyFMemReg dst orelse isAnyFMemReg src then
264 :     let val fsize = fsize fty
265 :     fun mvInstr{dst, src} = [I.FMOVE{fsize=fsize, src=src, dst=dst}]
266 :     in
267 :     emits (Shuffle.shuffle{mvInstr=mvInstr, ea=RealReg}
268 : leunga 744 {tmp=case dst of
269 : leunga 731 [_] => NONE
270 :     | _ => SOME(I.FPR(newReg())),
271 :     dst=dst, src=src})
272 :     end
273 :     else
274 :     mark(I.FCOPY{dst=dst,src=src,tmp=
275 :     case dst of
276 :     [_] => NONE
277 :     | _ => SOME(I.FPR(newFreg()))}, an)
278 :    
279 :     fun fcopy x = if enableFastFPMode andalso !fast_floating_point
280 :     then fcopy'' x else fcopy' x
281 :    
282 : george 545 (* Translates MLTREE condition code to x86 condition code *)
283 :     fun cond T.LT = I.LT | cond T.LTU = I.B
284 :     | cond T.LE = I.LE | cond T.LEU = I.BE
285 :     | cond T.EQ = I.EQ | cond T.NE = I.NE
286 :     | cond T.GE = I.GE | cond T.GEU = I.AE
287 :     | cond T.GT = I.GT | cond T.GTU = I.A
288 : monnier 247
289 : leunga 815 fun zero dst = emit(I.BINARY{binOp=I.XORL, src=dst, dst=dst})
290 :    
291 : george 545 (* Move and annotate *)
292 :     fun move'(src as I.Direct s, dst as I.Direct d, an) =
293 : george 889 if CB.sameColor(s,d) then ()
294 : george 545 else mark(I.COPY{dst=[d], src=[s], tmp=NONE}, an)
295 : leunga 815 | move'(I.Immed 0, dst as I.Direct d, an) =
296 :     mark(I.BINARY{binOp=I.XORL, src=dst, dst=dst}, an)
297 : george 545 | move'(src, dst, an) = mark(I.MOVE{mvOp=I.MOVL, src=src, dst=dst}, an)
298 : monnier 247
299 : george 545 (* Move only! *)
300 :     fun move(src, dst) = move'(src, dst, [])
301 : monnier 247
302 : george 545 val readonly = I.Region.readonly
303 : monnier 247
304 : george 545 (*
305 : george 761 * Compute an effective address.
306 : george 545 *)
307 : george 761 fun address(ea, mem) = let
308 : george 545 (* Keep building a bigger and bigger effective address expressions
309 :     * The input is a list of trees
310 :     * b -- base
311 :     * i -- index
312 :     * s -- scale
313 :     * d -- immed displacement
314 :     *)
315 :     fun doEA([], b, i, s, d) = makeAddressingMode(b, i, s, d)
316 :     | doEA(t::trees, b, i, s, d) =
317 :     (case t of
318 : george 761 T.LI n => doEAImmed(trees, toInt32 n, b, i, s, d)
319 : leunga 775 | T.CONST _ => doEALabel(trees, t, b, i, s, d)
320 :     | T.LABEL _ => doEALabel(trees, t, b, i, s, d)
321 :     | T.LABEXP le => doEALabel(trees, le, b, i, s, d)
322 : george 545 | T.ADD(32, t1, t2 as T.REG(_,r)) =>
323 :     if isMemReg r then doEA(t2::t1::trees, b, i, s, d)
324 :     else doEA(t1::t2::trees, b, i, s, d)
325 :     | T.ADD(32, t1, t2) => doEA(t1::t2::trees, b, i, s, d)
326 :     | T.SUB(32, t1, T.LI n) =>
327 : george 761 doEA(t1::T.LI(T.I.NEG(32,n))::trees, b, i, s, d)
328 :     | T.SLL(32, t1, T.LI n) => let
329 :     val n = T.I.toInt(32, n)
330 :     in
331 :     case n
332 :     of 0 => displace(trees, t1, b, i, s, d)
333 :     | 1 => indexed(trees, t1, t, 1, b, i, s, d)
334 :     | 2 => indexed(trees, t1, t, 2, b, i, s, d)
335 :     | 3 => indexed(trees, t1, t, 3, b, i, s, d)
336 :     | _ => displace(trees, t, b, i, s, d)
337 :     end
338 : george 545 | t => displace(trees, t, b, i, s, d)
339 :     )
340 : monnier 247
341 : george 545 (* Add an immed constant *)
342 :     and doEAImmed(trees, 0, b, i, s, d) = doEA(trees, b, i, s, d)
343 :     | doEAImmed(trees, n, b, i, s, I.Immed m) =
344 : george 761 doEA(trees, b, i, s, I.Immed(n+m))
345 : george 545 | doEAImmed(trees, n, b, i, s, I.ImmedLabel le) =
346 : leunga 775 doEA(trees, b, i, s,
347 :     I.ImmedLabel(T.ADD(32,le,T.LI(T.I.fromInt32(32, n)))))
348 : george 545 | doEAImmed(trees, n, b, i, s, _) = error "doEAImmed"
349 : monnier 247
350 : george 545 (* Add a label expression *)
351 :     and doEALabel(trees, le, b, i, s, I.Immed 0) =
352 :     doEA(trees, b, i, s, I.ImmedLabel le)
353 :     | doEALabel(trees, le, b, i, s, I.Immed m) =
354 :     doEA(trees, b, i, s,
355 : leunga 775 I.ImmedLabel(T.ADD(32,le,T.LI(T.I.fromInt32(32, m))))
356 : george 545 handle Overflow => error "doEALabel: constant too large")
357 :     | doEALabel(trees, le, b, i, s, I.ImmedLabel le') =
358 : leunga 775 doEA(trees, b, i, s, I.ImmedLabel(T.ADD(32,le,le')))
359 : george 545 | doEALabel(trees, le, b, i, s, _) = error "doEALabel"
360 : monnier 247
361 : george 545 and makeAddressingMode(NONE, NONE, _, disp) = disp
362 :     | makeAddressingMode(SOME base, NONE, _, disp) =
363 :     I.Displace{base=base, disp=disp, mem=mem}
364 :     | makeAddressingMode(base, SOME index, scale, disp) =
365 : george 761 I.Indexed{base=base, index=index, scale=scale,
366 : george 545 disp=disp, mem=mem}
367 : monnier 247
368 : george 545 (* generate code for tree and ensure that it is not in %esp *)
369 :     and exprNotEsp tree =
370 :     let val r = expr tree
371 : george 889 in if CB.sameColor(r, C.esp) then
372 : george 545 let val tmp = newReg()
373 :     in move(I.Direct r, I.Direct tmp); tmp end
374 :     else r
375 :     end
376 : monnier 247
377 : george 545 (* Add a base register *)
378 :     and displace(trees, t, NONE, i, s, d) = (* no base yet *)
379 :     doEA(trees, SOME(expr t), i, s, d)
380 :     | displace(trees, t, b as SOME base, NONE, _, d) = (* no index *)
381 :     (* make t the index, but make sure that it is not %esp! *)
382 :     let val i = expr t
383 : george 889 in if CB.sameColor(i, C.esp) then
384 : george 545 (* swap base and index *)
385 : george 889 if CB.sameColor(base, C.esp) then
386 : george 545 doEA(trees, SOME i, b, 0, d)
387 :     else (* base and index = %esp! *)
388 :     let val index = newReg()
389 :     in move(I.Direct i, I.Direct index);
390 :     doEA(trees, b, SOME index, 0, d)
391 :     end
392 :     else
393 :     doEA(trees, b, SOME i, 0, d)
394 :     end
395 :     | displace(trees, t, SOME base, i, s, d) = (* base and index *)
396 :     let val b = expr(T.ADD(32,T.REG(32,base),t))
397 :     in doEA(trees, SOME b, i, s, d) end
398 : monnier 247
399 : george 545 (* Add an indexed register *)
400 :     and indexed(trees, t, t0, scale, b, NONE, _, d) = (* no index yet *)
401 :     doEA(trees, b, SOME(exprNotEsp t), scale, d)
402 :     | indexed(trees, _, t0, _, NONE, i, s, d) = (* no base *)
403 :     doEA(trees, SOME(expr t0), i, s, d)
404 :     | indexed(trees, _, t0, _, SOME base, i, s, d) = (*base and index*)
405 :     let val b = expr(T.ADD(32, t0, T.REG(32, base)))
406 :     in doEA(trees, SOME b, i, s, d) end
407 :    
408 :     in case doEA([ea], NONE, NONE, 0, I.Immed 0) of
409 :     I.Immed _ => raise EA
410 :     | I.ImmedLabel le => I.LabelEA le
411 :     | ea => ea
412 :     end (* address *)
413 : monnier 247
414 : george 545 (* reduce an expression into an operand *)
415 : george 761 and operand(T.LI i) = I.Immed(toInt32(i))
416 : leunga 775 | operand(x as (T.CONST _ | T.LABEL _)) = I.ImmedLabel x
417 :     | operand(T.LABEXP le) = I.ImmedLabel le
418 : george 545 | operand(T.REG(_,r)) = IntReg r
419 :     | operand(T.LOAD(32,ea,mem)) = address(ea, mem)
420 :     | operand(t) = I.Direct(expr t)
421 : monnier 247
422 : george 545 and moveToReg(opnd) =
423 :     let val dst = I.Direct(newReg())
424 :     in move(opnd, dst); dst
425 :     end
426 : monnier 247
427 : george 545 and reduceOpnd(I.Direct r) = r
428 :     | reduceOpnd opnd =
429 :     let val dst = newReg()
430 :     in move(opnd, I.Direct dst); dst
431 :     end
432 : monnier 247
433 : george 545 (* ensure that the operand is either an immed or register *)
434 :     and immedOrReg(opnd as I.Displace _) = moveToReg opnd
435 :     | immedOrReg(opnd as I.Indexed _) = moveToReg opnd
436 :     | immedOrReg(opnd as I.MemReg _) = moveToReg opnd
437 :     | immedOrReg(opnd as I.LabelEA _) = moveToReg opnd
438 :     | immedOrReg opnd = opnd
439 : monnier 247
440 : george 545 and isImmediate(I.Immed _) = true
441 :     | isImmediate(I.ImmedLabel _) = true
442 :     | isImmediate _ = false
443 : monnier 247
444 : george 545 and regOrMem opnd = if isImmediate opnd then moveToReg opnd else opnd
445 :    
446 :     and isMemOpnd opnd =
447 :     (case opnd of
448 :     I.Displace _ => true
449 :     | I.Indexed _ => true
450 :     | I.MemReg _ => true
451 :     | I.LabelEA _ => true
452 : george 555 | I.FDirect f => true
453 : george 545 | _ => false
454 :     )
455 :    
456 :     (*
457 :     * Compute an integer expression and put the result in
458 :     * the destination register rd.
459 :     *)
460 : george 889 and doExpr(exp, rd : CB.cell, an) =
461 : george 545 let val rdOpnd = IntReg rd
462 : monnier 247
463 : george 889 fun equalRd(I.Direct r) = CB.sameColor(r, rd)
464 :     | equalRd(I.MemReg r) = CB.sameColor(r, rd)
465 : george 545 | equalRd _ = false
466 : monnier 247
467 : george 545 (* Emit a binary operator. If the destination is
468 :     * a memReg, do something smarter.
469 :     *)
470 :     fun genBinary(binOp, opnd1, opnd2) =
471 :     if isMemReg rd andalso
472 :     (isMemOpnd opnd1 orelse isMemOpnd opnd2) orelse
473 :     equalRd(opnd2)
474 :     then
475 :     let val tmpR = newReg()
476 :     val tmp = I.Direct tmpR
477 :     in move(opnd1, tmp);
478 :     mark(I.BINARY{binOp=binOp, src=opnd2, dst=tmp}, an);
479 :     move(tmp, rdOpnd)
480 :     end
481 :     else
482 :     (move(opnd1, rdOpnd);
483 :     mark(I.BINARY{binOp=binOp, src=opnd2, dst=rdOpnd}, an)
484 :     )
485 : monnier 247
486 : george 545 (* Generate a binary operator; it may commute *)
487 :     fun binaryComm(binOp, e1, e2) =
488 :     let val (opnd1, opnd2) =
489 :     case (operand e1, operand e2) of
490 :     (x as I.Immed _, y) => (y, x)
491 :     | (x as I.ImmedLabel _, y) => (y, x)
492 :     | (x, y as I.Direct _) => (y, x)
493 :     | (x, y) => (x, y)
494 :     in genBinary(binOp, opnd1, opnd2)
495 :     end
496 :    
497 :     (* Generate a binary operator; non-commutative *)
498 :     fun binary(binOp, e1, e2) =
499 :     genBinary(binOp, operand e1, operand e2)
500 :    
501 :     (* Generate a unary operator *)
502 :     fun unary(unOp, e) =
503 :     let val opnd = operand e
504 :     in if isMemReg rd andalso isMemOpnd opnd then
505 :     let val tmp = I.Direct(newReg())
506 :     in move(opnd, tmp); move(tmp, rdOpnd)
507 :     end
508 :     else move(opnd, rdOpnd);
509 :     mark(I.UNARY{unOp=unOp, opnd=rdOpnd}, an)
510 :     end
511 :    
512 :     (* Generate shifts; the shift
513 :     * amount must be a constant or in %ecx *)
514 :     fun shift(opcode, e1, e2) =
515 :     let val (opnd1, opnd2) = (operand e1, operand e2)
516 :     in case opnd2 of
517 :     I.Immed _ => genBinary(opcode, opnd1, opnd2)
518 :     | _ =>
519 :     if equalRd(opnd2) then
520 :     let val tmpR = newReg()
521 :     val tmp = I.Direct tmpR
522 :     in move(opnd1, tmp);
523 :     move(opnd2, ecx);
524 :     mark(I.BINARY{binOp=opcode, src=ecx, dst=tmp},an);
525 :     move(tmp, rdOpnd)
526 :     end
527 :     else
528 :     (move(opnd1, rdOpnd);
529 :     move(opnd2, ecx);
530 :     mark(I.BINARY{binOp=opcode, src=ecx, dst=rdOpnd},an)
531 :     )
532 :     end
533 :    
534 :     (* Division or remainder: divisor must be in %edx:%eax pair *)
535 :     fun divrem(signed, overflow, e1, e2, resultReg) =
536 :     let val (opnd1, opnd2) = (operand e1, operand e2)
537 :     val _ = move(opnd1, eax)
538 : leunga 815 val oper = if signed then (emit(I.CDQ); I.IDIVL1)
539 :     else (zero edx; I.DIVL1)
540 : george 545 in mark(I.MULTDIV{multDivOp=oper, src=regOrMem opnd2},an);
541 :     move(resultReg, rdOpnd);
542 :     if overflow then trap() else ()
543 :     end
544 :    
545 :     (* Optimize the special case for division *)
546 : george 761 fun divide(signed, overflow, e1, e2 as T.LI n') = let
547 :     val n = toInt32 n'
548 :     val w = T.I.toWord32(32, n')
549 :     fun isPowerOf2 w = W32.andb((w - 0w1), w) = 0w0
550 : george 545 fun log2 n = (* n must be > 0!!! *)
551 :     let fun loop(0w1,pow) = pow
552 : george 761 | loop(w,pow) = loop(W32.>>(w, 0w1),pow+1)
553 : george 545 in loop(n,0) end
554 :     in if n > 1 andalso isPowerOf2 w then
555 : george 761 let val pow = T.LI(T.I.fromInt(32,log2 w))
556 : george 545 in if signed then
557 :     (* signed; simulate round towards zero *)
558 : george 909 let val label = Label.anon()
559 : george 545 val reg1 = expr e1
560 :     val opnd1 = I.Direct reg1
561 :     in if setZeroBit e1 then ()
562 :     else emit(I.CMPL{lsrc=opnd1, rsrc=I.Immed 0});
563 :     emit(I.JCC{cond=I.GE, opnd=immedLabel label});
564 :     emit(if n = 2 then
565 :     I.UNARY{unOp=I.INCL, opnd=opnd1}
566 :     else
567 :     I.BINARY{binOp=I.ADDL,
568 : george 761 src=I.Immed(n - 1),
569 : george 545 dst=opnd1});
570 :     defineLabel label;
571 :     shift(I.SARL, T.REG(32, reg1), pow)
572 :     end
573 :     else (* unsigned *)
574 :     shift(I.SHRL, e1, pow)
575 :     end
576 :     else
577 :     (* note the only way we can overflow is if
578 :     * n = 0 or n = -1
579 :     *)
580 :     divrem(signed, overflow andalso (n = ~1 orelse n = 0),
581 :     e1, e2, eax)
582 :     end
583 :     | divide(signed, overflow, e1, e2) =
584 :     divrem(signed, overflow, e1, e2, eax)
585 : monnier 247
586 : george 545 fun rem(signed, overflow, e1, e2) =
587 :     divrem(signed, overflow, e1, e2, edx)
588 : leunga 815
589 :     (* Makes sure the destination must be a register *)
590 :     fun dstMustBeReg f =
591 :     if isMemReg rd then
592 :     let val tmpR = newReg()
593 :     val tmp = I.Direct(tmpR)
594 :     in f(tmpR, tmp); move(tmp, rdOpnd) end
595 :     else f(rd, rdOpnd)
596 :    
597 : george 545 (* unsigned integer multiplication *)
598 :     fun uMultiply(e1, e2) =
599 :     (* note e2 can never be (I.Direct edx) *)
600 :     (move(operand e1, eax);
601 : leunga 815 mark(I.MULTDIV{multDivOp=I.MULL1,
602 : george 545 src=regOrMem(operand e2)},an);
603 :     move(eax, rdOpnd)
604 :     )
605 :    
606 :     (* signed integer multiplication:
607 :     * The only forms that are allowed that also sets the
608 :     * OF and CF flags are:
609 :     *
610 : leunga 815 * (dst) (src1) (src2)
611 : george 545 * imul r32, r32/m32, imm8
612 : leunga 815 * (dst) (src)
613 : george 545 * imul r32, imm8
614 :     * imul r32, imm32
615 : leunga 815 * imul r32, r32/m32
616 :     * Note: destination must be a register!
617 : george 545 *)
618 :     fun multiply(e1, e2) =
619 : leunga 815 dstMustBeReg(fn (rd, rdOpnd) =>
620 :     let fun doit(i1 as I.Immed _, i2 as I.Immed _) =
621 :     (move(i1, rdOpnd);
622 :     mark(I.BINARY{binOp=I.IMULL, dst=rdOpnd, src=i2},an))
623 :     | doit(rm, i2 as I.Immed _) = doit(i2, rm)
624 :     | doit(imm as I.Immed(i), rm) =
625 :     mark(I.MUL3{dst=rd, src1=rm, src2=i},an)
626 :     | doit(r1 as I.Direct _, r2 as I.Direct _) =
627 :     (move(r1, rdOpnd);
628 :     mark(I.BINARY{binOp=I.IMULL, dst=rdOpnd, src=r2},an))
629 :     | doit(r1 as I.Direct _, rm) =
630 :     (move(r1, rdOpnd);
631 :     mark(I.BINARY{binOp=I.IMULL, dst=rdOpnd, src=rm},an))
632 :     | doit(rm, r as I.Direct _) = doit(r, rm)
633 :     | doit(rm1, rm2) =
634 : george 545 if equalRd rm2 then
635 :     let val tmpR = newReg()
636 :     val tmp = I.Direct tmpR
637 :     in move(rm1, tmp);
638 : leunga 815 mark(I.BINARY{binOp=I.IMULL, dst=tmp, src=rm2},an);
639 :     move(tmp, rdOpnd)
640 : george 545 end
641 :     else
642 : leunga 815 (move(rm1, rdOpnd);
643 :     mark(I.BINARY{binOp=I.IMULL, dst=rdOpnd, src=rm2},an)
644 : george 545 )
645 :     val (opnd1, opnd2) = (operand e1, operand e2)
646 : leunga 815 in doit(opnd1, opnd2)
647 : george 545 end
648 : leunga 815 )
649 : monnier 247
650 : george 545 (* Emit a load instruction; makes sure that the destination
651 :     * is a register
652 :     *)
653 :     fun genLoad(mvOp, ea, mem) =
654 :     dstMustBeReg(fn (_, dst) =>
655 :     mark(I.MOVE{mvOp=mvOp, src=address(ea, mem), dst=dst},an))
656 :    
657 :     (* Generate a zero extended loads *)
658 :     fun load8(ea, mem) = genLoad(I.MOVZBL, ea, mem)
659 :     fun load16(ea, mem) = genLoad(I.MOVZWL, ea, mem)
660 :     fun load8s(ea, mem) = genLoad(I.MOVSBL, ea, mem)
661 :     fun load16s(ea, mem) = genLoad(I.MOVSWL, ea, mem)
662 :     fun load32(ea, mem) = genLoad(I.MOVL, ea, mem)
663 :    
664 :     (* Generate a sign extended loads *)
665 :    
666 :     (* Generate setcc instruction:
667 :     * semantics: MV(rd, COND(_, T.CMP(ty, cc, t1, t2), yes, no))
668 : leunga 583 * Bug, if eax is either t1 or t2 then problem will occur!!!
669 :     * Note that we have to use eax as the destination of the
670 :     * setcc because it only works on the registers
671 :     * %al, %bl, %cl, %dl and %[abcd]h. The last four registers
672 :     * are inaccessible in 32 bit mode.
673 : george 545 *)
674 :     fun setcc(ty, cc, t1, t2, yes, no) =
675 : leunga 583 let val (cc, yes, no) =
676 :     if yes > no then (cc, yes, no)
677 :     else (T.Basis.negateCond cc, no, yes)
678 : george 545 in (* Clear the destination first.
679 :     * This this because stupid SETcc
680 :     * only writes to the low order
681 :     * byte. That's Intel architecture, folks.
682 :     *)
683 : leunga 695 case (yes, no, cc) of
684 :     (1, 0, T.LT) =>
685 :     let val tmp = I.Direct(expr(T.SUB(32,t1,t2)))
686 :     in move(tmp, rdOpnd);
687 :     emit(I.BINARY{binOp=I.SHRL,src=I.Immed 31,dst=rdOpnd})
688 :     end
689 :     | (1, 0, T.GT) =>
690 :     let val tmp = I.Direct(expr(T.SUB(32,t1,t2)))
691 :     in emit(I.UNARY{unOp=I.NOTL,opnd=tmp});
692 :     move(tmp, rdOpnd);
693 :     emit(I.BINARY{binOp=I.SHRL,src=I.Immed 31,dst=rdOpnd})
694 :     end
695 :     | (1, 0, _) => (* normal case *)
696 : george 545 let val cc = cmp(true, ty, cc, t1, t2, [])
697 : leunga 583 in mark(I.SET{cond=cond cc, opnd=eax}, an);
698 : leunga 695 emit(I.BINARY{binOp=I.ANDL,src=I.Immed 255, dst=eax});
699 : leunga 583 move(eax, rdOpnd)
700 :     end
701 : leunga 695 | (C1, C2, _) =>
702 : george 545 (* general case;
703 : leunga 583 * from the Intel optimization guide p3-5
704 :     *)
705 : leunga 695 let val _ = zero eax;
706 :     val cc = cmp(true, ty, cc, t1, t2, [])
707 : leunga 583 in case C1-C2 of
708 :     D as (1 | 2 | 3 | 4 | 5 | 8 | 9) =>
709 :     let val (base,scale) =
710 :     case D of
711 :     1 => (NONE, 0)
712 :     | 2 => (NONE, 1)
713 :     | 3 => (SOME C.eax, 1)
714 :     | 4 => (NONE, 2)
715 :     | 5 => (SOME C.eax, 2)
716 :     | 8 => (NONE, 3)
717 :     | 9 => (SOME C.eax, 3)
718 :     val addr = I.Indexed{base=base,
719 :     index=C.eax,
720 :     scale=scale,
721 :     disp=I.Immed C2,
722 : george 545 mem=readonly}
723 : leunga 583 val tmpR = newReg()
724 :     val tmp = I.Direct tmpR
725 :     in emit(I.SET{cond=cond cc, opnd=eax});
726 :     mark(I.LEA{r32=tmpR, addr=addr}, an);
727 :     move(tmp, rdOpnd)
728 :     end
729 :     | D =>
730 :     (emit(I.SET{cond=cond(T.Basis.negateCond cc),
731 :     opnd=eax});
732 :     emit(I.UNARY{unOp=I.DECL, opnd=eax});
733 :     emit(I.BINARY{binOp=I.ANDL,
734 :     src=I.Immed D, dst=eax});
735 :     if C2 = 0 then
736 :     move(eax, rdOpnd)
737 :     else
738 :     let val tmpR = newReg()
739 :     val tmp = I.Direct tmpR
740 :     in mark(I.LEA{addr=
741 :     I.Displace{
742 :     base=C.eax,
743 :     disp=I.Immed C2,
744 :     mem=readonly},
745 :     r32=tmpR}, an);
746 :     move(tmp, rdOpnd)
747 :     end
748 :     )
749 :     end
750 : george 545 end (* setcc *)
751 :    
752 :     (* Generate cmovcc instruction.
753 :     * on Pentium Pro and Pentium II only
754 :     *)
755 :     fun cmovcc(ty, cc, t1, t2, yes, no) =
756 :     let fun genCmov(dstR, _) =
757 :     let val _ = doExpr(no, dstR, []) (* false branch *)
758 :     val cc = cmp(true, ty, cc, t1, t2, []) (* compare *)
759 :     in mark(I.CMOV{cond=cond cc, src=operand yes, dst=dstR}, an)
760 :     end
761 :     in dstMustBeReg genCmov
762 :     end
763 :    
764 :     fun unknownExp exp = doExpr(Gen.compileRexp exp, rd, an)
765 : monnier 247
766 : leunga 606 (* Add n to rd *)
767 :     fun addN n =
768 :     let val n = operand n
769 :     val src = if isMemReg rd then immedOrReg n else n
770 :     in mark(I.BINARY{binOp=I.ADDL, src=src, dst=rdOpnd}, an) end
771 :    
772 : george 545 (* Generate addition *)
773 :     fun addition(e1, e2) =
774 : leunga 606 case e1 of
775 : george 889 T.REG(_,rs) => if CB.sameColor(rs,rd) then addN e2
776 : leunga 744 else addition1(e1,e2)
777 : leunga 606 | _ => addition1(e1,e2)
778 :     and addition1(e1, e2) =
779 :     case e2 of
780 : george 889 T.REG(_,rs) => if CB.sameColor(rs,rd) then addN e1
781 : leunga 744 else addition2(e1,e2)
782 : leunga 606 | _ => addition2(e1,e2)
783 :     and addition2(e1,e2) =
784 : george 545 (dstMustBeReg(fn (dstR, _) =>
785 :     mark(I.LEA{r32=dstR, addr=address(exp, readonly)}, an))
786 :     handle EA => binaryComm(I.ADDL, e1, e2))
787 : monnier 247
788 :    
789 : george 545 in case exp of
790 :     T.REG(_,rs) =>
791 :     if isMemReg rs andalso isMemReg rd then
792 :     let val tmp = I.Direct(newReg())
793 : leunga 731 in move'(I.MemReg rs, tmp, an);
794 : george 545 move'(tmp, rdOpnd, [])
795 :     end
796 :     else move'(IntReg rs, rdOpnd, an)
797 : george 761 | T.LI z => let
798 :     val n = toInt32 z
799 :     in
800 :     if n=0 then
801 :     (* As per Fermin's request, special optimization for rd := 0.
802 :     * Currently we don't bother with the size.
803 :     *)
804 :     if isMemReg rd then move'(I.Immed 0, rdOpnd, an)
805 :     else mark(I.BINARY{binOp=I.XORL, src=rdOpnd, dst=rdOpnd}, an)
806 :     else
807 :     move'(I.Immed(n), rdOpnd, an)
808 :     end
809 : leunga 775 | (T.CONST _ | T.LABEL _) =>
810 :     move'(I.ImmedLabel exp, rdOpnd, an)
811 :     | T.LABEXP le => move'(I.ImmedLabel le, rdOpnd, an)
812 : monnier 247
813 : george 545 (* 32-bit addition *)
814 : george 761 | T.ADD(32, e1, e2 as T.LI n) => let
815 :     val n = toInt32 n
816 :     in
817 :     case n
818 :     of 1 => unary(I.INCL, e1)
819 :     | ~1 => unary(I.DECL, e1)
820 :     | _ => addition(e1, e2)
821 :     end
822 :     | T.ADD(32, e1 as T.LI n, e2) => let
823 :     val n = toInt32 n
824 :     in
825 :     case n
826 :     of 1 => unary(I.INCL, e2)
827 :     | ~1 => unary(I.DECL, e2)
828 :     | _ => addition(e1, e2)
829 :     end
830 : george 545 | T.ADD(32, e1, e2) => addition(e1, e2)
831 : monnier 247
832 : leunga 695 (* 32-bit addition but set the flag!
833 :     * This is a stupid hack for now.
834 :     *)
835 : george 761 | T.ADD(0, e, e1 as T.LI n) => let
836 :     val n = T.I.toInt(32, n)
837 :     in
838 :     if n=1 then unary(I.INCL, e)
839 :     else if n = ~1 then unary(I.DECL, e)
840 :     else binaryComm(I.ADDL, e, e1)
841 :     end
842 :     | T.ADD(0, e1 as T.LI n, e) => let
843 :     val n = T.I.toInt(32, n)
844 :     in
845 :     if n=1 then unary(I.INCL, e)
846 :     else if n = ~1 then unary(I.DECL, e)
847 :     else binaryComm(I.ADDL, e1, e)
848 :     end
849 :     | T.ADD(0, e1, e2) => binaryComm(I.ADDL, e1, e2)
850 :    
851 : george 545 (* 32-bit subtraction *)
852 : george 761 | T.SUB(32, e1, e2 as T.LI n) => let
853 :     val n = toInt32 n
854 :     in
855 :     case n
856 :     of 0 => doExpr(e1, rd, an)
857 :     | 1 => unary(I.DECL, e1)
858 :     | ~1 => unary(I.INCL, e1)
859 :     | _ => binary(I.SUBL, e1, e2)
860 :     end
861 :     | T.SUB(32, e1 as T.LI n, e2) =>
862 :     if T.I.isZero n then unary(I.NEGL, e2)
863 :     else binary(I.SUBL, e1, e2)
864 : george 545 | T.SUB(32, e1, e2) => binary(I.SUBL, e1, e2)
865 : monnier 247
866 : george 545 | T.MULU(32, x, y) => uMultiply(x, y)
867 :     | T.DIVU(32, x, y) => divide(false, false, x, y)
868 :     | T.REMU(32, x, y) => rem(false, false, x, y)
869 : monnier 247
870 : george 545 | T.MULS(32, x, y) => multiply(x, y)
871 :     | T.DIVS(32, x, y) => divide(true, false, x, y)
872 :     | T.REMS(32, x, y) => rem(true, false, x, y)
873 : monnier 247
874 : george 545 | T.ADDT(32, x, y) => (binaryComm(I.ADDL, x, y); trap())
875 :     | T.SUBT(32, x, y) => (binary(I.SUBL, x, y); trap())
876 :     | T.MULT(32, x, y) => (multiply(x, y); trap())
877 :     | T.DIVT(32, x, y) => divide(true, true, x, y)
878 :     | T.REMT(32, x, y) => rem(true, true, x, y)
879 : monnier 247
880 : george 545 | T.ANDB(32, x, y) => binaryComm(I.ANDL, x, y)
881 :     | T.ORB(32, x, y) => binaryComm(I.ORL, x, y)
882 :     | T.XORB(32, x, y) => binaryComm(I.XORL, x, y)
883 :     | T.NOTB(32, x) => unary(I.NOTL, x)
884 : monnier 247
885 : george 545 | T.SRA(32, x, y) => shift(I.SARL, x, y)
886 :     | T.SRL(32, x, y) => shift(I.SHRL, x, y)
887 :     | T.SLL(32, x, y) => shift(I.SHLL, x, y)
888 : monnier 247
889 : george 545 | T.LOAD(8, ea, mem) => load8(ea, mem)
890 :     | T.LOAD(16, ea, mem) => load16(ea, mem)
891 :     | T.LOAD(32, ea, mem) => load32(ea, mem)
892 : monnier 498
893 : leunga 776 | T.SX(32,8,T.LOAD(8,ea,mem)) => load8s(ea, mem)
894 :     | T.SX(32,16,T.LOAD(16,ea,mem)) => load16s(ea, mem)
895 :     | T.ZX(32,8,T.LOAD(8,ea,mem)) => load8(ea, mem)
896 : leunga 779 | T.ZX(32,16,T.LOAD(16,ea,mem)) => load16(ea, mem)
897 : leunga 776
898 : george 545 | T.COND(32, T.CMP(ty, cc, t1, t2), T.LI yes, T.LI no) =>
899 : leunga 583 setcc(ty, cc, t1, t2, toInt32 yes, toInt32 no)
900 : george 545 | T.COND(32, T.CMP(ty, cc, t1, t2), yes, no) =>
901 :     (case !arch of (* PentiumPro and higher has CMOVcc *)
902 :     Pentium => unknownExp exp
903 :     | _ => cmovcc(ty, cc, t1, t2, yes, no)
904 :     )
905 :     | T.LET(s,e) => (doStmt s; doExpr(e, rd, an))
906 :     | T.MARK(e, A.MARKREG f) => (f rd; doExpr(e, rd, an))
907 :     | T.MARK(e, a) => doExpr(e, rd, a::an)
908 :     | T.PRED(e,c) => doExpr(e, rd, A.CTRLUSE c::an)
909 : george 555 | T.REXT e =>
910 :     ExtensionComp.compileRext (reducer()) {e=e, rd=rd, an=an}
911 : george 545 (* simplify and try again *)
912 :     | exp => unknownExp exp
913 :     end (* doExpr *)
914 : monnier 247
915 : george 545 (* generate an expression and return its result register
916 :     * If rewritePseudo is on, the result is guaranteed to be in a
917 :     * non memReg register
918 :     *)
919 :     and expr(exp as T.REG(_, rd)) =
920 :     if isMemReg rd then genExpr exp else rd
921 :     | expr exp = genExpr exp
922 : monnier 247
923 : george 545 and genExpr exp =
924 :     let val rd = newReg() in doExpr(exp, rd, []); rd end
925 : monnier 247
926 : george 545 (* Compare an expression with zero.
927 :     * On the x86, TEST is superior to AND for doing the same thing,
928 :     * since it doesn't need to write out the result in a register.
929 :     *)
930 : leunga 695 and cmpWithZero(cc as (T.EQ | T.NE), e as T.ANDB(ty, a, b), an) =
931 : george 545 (case ty of
932 : leunga 695 8 => test(I.TESTB, a, b, an)
933 :     | 16 => test(I.TESTW, a, b, an)
934 :     | 32 => test(I.TESTL, a, b, an)
935 :     | _ => doExpr(e, newReg(), an);
936 :     cc)
937 :     | cmpWithZero(cc, e, an) =
938 :     let val e =
939 :     case e of (* hack to disable the lea optimization XXX *)
940 :     T.ADD(_, a, b) => T.ADD(0, a, b)
941 :     | e => e
942 :     in doExpr(e, newReg(), an); cc end
943 : monnier 247
944 : george 545 (* Emit a test.
945 :     * The available modes are
946 :     * r/m, r
947 :     * r/m, imm
948 :     * On selecting the right instruction: TESTL/TESTW/TESTB.
949 :     * When anding an operand with a constant
950 :     * that fits within 8 (or 16) bits, it is possible to use TESTB,
951 :     * (or TESTW) instead of TESTL. Because x86 is little endian,
952 :     * this works for memory operands too. However, with TESTB, it is
953 :     * not possible to use registers other than
954 :     * AL, CL, BL, DL, and AH, CH, BH, DH. So, the best way is to
955 :     * perform register allocation first, and if the operand registers
956 :     * are one of EAX, ECX, EBX, or EDX, replace the TESTL instruction
957 :     * by TESTB.
958 :     *)
959 : leunga 695 and test(testopcode, a, b, an) =
960 : george 545 let val (_, opnd1, opnd2) = commuteComparison(T.EQ, true, a, b)
961 :     (* translate r, r/m => r/m, r *)
962 :     val (opnd1, opnd2) =
963 :     if isMemOpnd opnd2 then (opnd2, opnd1) else (opnd1, opnd2)
964 : leunga 695 in mark(testopcode{lsrc=opnd1, rsrc=opnd2}, an)
965 : george 545 end
966 : monnier 247
967 : leunga 815 (* %eflags <- src *)
968 :     and moveToEflags src =
969 : george 889 if CB.sameColor(src, C.eflags) then ()
970 : leunga 815 else (move(I.Direct src, eax); emit(I.LAHF))
971 :    
972 :     (* dst <- %eflags *)
973 :     and moveFromEflags dst =
974 : george 889 if CB.sameColor(dst, C.eflags) then ()
975 : leunga 815 else (emit(I.SAHF); move(eax, I.Direct dst))
976 :    
977 : george 545 (* generate a condition code expression
978 : leunga 744 * The zero is for setting the condition code!
979 :     * I have no idea why this is used.
980 :     *)
981 :     and doCCexpr(T.CMP(ty, cc, t1, t2), rd, an) =
982 : leunga 815 (cmp(false, ty, cc, t1, t2, an);
983 :     moveFromEflags rd
984 :     )
985 :     | doCCexpr(T.CC(cond,rs), rd, an) =
986 : george 889 if CB.sameColor(rs,C.eflags) orelse CB.sameColor(rd,C.eflags) then
987 : leunga 815 (moveToEflags rs; moveFromEflags rd)
988 : leunga 744 else
989 : leunga 815 move'(I.Direct rs, I.Direct rd, an)
990 : george 545 | doCCexpr(T.CCMARK(e,A.MARKREG f),rd,an) = (f rd; doCCexpr(e,rd,an))
991 :     | doCCexpr(T.CCMARK(e,a), rd, an) = doCCexpr(e,rd,a::an)
992 :     | doCCexpr(T.CCEXT e, cd, an) =
993 : george 555 ExtensionComp.compileCCext (reducer()) {e=e, ccd=cd, an=an}
994 : george 545 | doCCexpr _ = error "doCCexpr"
995 : monnier 247
996 : george 545 and ccExpr e = error "ccExpr"
997 : monnier 247
998 : george 545 (* generate a comparison and sets the condition code;
999 :     * return the actual cc used. If the flag swapable is true,
1000 :     * we can also reorder the operands.
1001 :     *)
1002 :     and cmp(swapable, ty, cc, t1, t2, an) =
1003 : leunga 695 (* == and <> can be always be reordered *)
1004 :     let val swapable = swapable orelse cc = T.EQ orelse cc = T.NE
1005 :     in (* Sometimes the comparison is not necessary because
1006 :     * the bits are already set!
1007 :     *)
1008 :     if isZero t1 andalso setZeroBit2 t2 then
1009 :     if swapable then
1010 :     cmpWithZero(T.Basis.swapCond cc, t2, an)
1011 :     else (* can't reorder the comparison! *)
1012 :     genCmp(ty, false, cc, t1, t2, an)
1013 :     else if isZero t2 andalso setZeroBit2 t1 then
1014 :     cmpWithZero(cc, t1, an)
1015 :     else genCmp(ty, swapable, cc, t1, t2, an)
1016 :     end
1017 : monnier 247
1018 : george 545 (* Give a and b which are the operands to a comparison (or test)
1019 :     * Return the appropriate condition code and operands.
1020 :     * The available modes are:
1021 :     * r/m, imm
1022 :     * r/m, r
1023 :     * r, r/m
1024 :     *)
1025 :     and commuteComparison(cc, swapable, a, b) =
1026 :     let val (opnd1, opnd2) = (operand a, operand b)
1027 :     in (* Try to fold in the operands whenever possible *)
1028 :     case (isImmediate opnd1, isImmediate opnd2) of
1029 :     (true, true) => (cc, moveToReg opnd1, opnd2)
1030 :     | (true, false) =>
1031 :     if swapable then (T.Basis.swapCond cc, opnd2, opnd1)
1032 :     else (cc, moveToReg opnd1, opnd2)
1033 :     | (false, true) => (cc, opnd1, opnd2)
1034 :     | (false, false) =>
1035 :     (case (opnd1, opnd2) of
1036 :     (_, I.Direct _) => (cc, opnd1, opnd2)
1037 :     | (I.Direct _, _) => (cc, opnd1, opnd2)
1038 :     | (_, _) => (cc, moveToReg opnd1, opnd2)
1039 :     )
1040 :     end
1041 :    
1042 :     (* generate a real comparison; return the real cc used *)
1043 :     and genCmp(ty, swapable, cc, a, b, an) =
1044 :     let val (cc, opnd1, opnd2) = commuteComparison(cc, swapable, a, b)
1045 :     in mark(I.CMPL{lsrc=opnd1, rsrc=opnd2}, an); cc
1046 :     end
1047 : monnier 247
1048 : george 545 (* generate code for jumps *)
1049 : leunga 775 and jmp(lexp as T.LABEL lab, labs, an) =
1050 : george 545 mark(I.JMP(I.ImmedLabel lexp, [lab]), an)
1051 : leunga 775 | jmp(T.LABEXP le, labs, an) = mark(I.JMP(I.ImmedLabel le, labs), an)
1052 :     | jmp(ea, labs, an) = mark(I.JMP(operand ea, labs), an)
1053 : george 545
1054 :     (* convert mlrisc to cellset:
1055 :     *)
1056 :     and cellset mlrisc =
1057 : jhr 900 let val addCCReg = CB.CellSet.add
1058 : george 545 fun g([],acc) = acc
1059 :     | g(T.GPR(T.REG(_,r))::regs,acc) = g(regs,C.addReg(r,acc))
1060 :     | g(T.FPR(T.FREG(_,f))::regs,acc) = g(regs,C.addFreg(f,acc))
1061 :     | g(T.CCR(T.CC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc))
1062 :     | g(T.CCR(T.FCC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc))
1063 :     | g(_::regs, acc) = g(regs, acc)
1064 :     in g(mlrisc, C.empty) end
1065 :    
1066 :     (* generate code for calls *)
1067 : blume 839 and call(ea, flow, def, use, mem, cutsTo, an, pops) =
1068 : leunga 815 let fun return(set, []) = set
1069 :     | return(set, a::an) =
1070 :     case #peek A.RETURN_ARG a of
1071 : jhr 900 SOME r => return(CB.CellSet.add(r, set), an)
1072 : leunga 815 | NONE => return(set, an)
1073 : blume 839 in
1074 :     mark(I.CALL{opnd=operand ea,defs=cellset(def),uses=cellset(use),
1075 :     return=return(C.empty,an),cutsTo=cutsTo,mem=mem,
1076 :     pops=pops},an)
1077 : leunga 815 end
1078 : george 545
1079 : leunga 815 (* generate code for integer stores; first move data to %eax
1080 :     * This is mainly because we can't allocate to registers like
1081 :     * ah, dl, dx etc.
1082 :     *)
1083 :     and genStore(mvOp, ea, d, mem, an) =
1084 :     let val src =
1085 : george 545 case immedOrReg(operand d) of
1086 :     src as I.Direct r =>
1087 : george 889 if CB.sameColor(r,C.eax)
1088 : leunga 744 then src else (move(src, eax); eax)
1089 : george 545 | src => src
1090 : leunga 815 in mark(I.MOVE{mvOp=mvOp, src=src, dst=address(ea,mem)},an)
1091 : george 545 end
1092 : leunga 815
1093 :     (* generate code for 8-bit integer stores *)
1094 :     (* movb has to use %eax as source. Stupid x86! *)
1095 :     and store8(ea, d, mem, an) = genStore(I.MOVB, ea, d, mem, an)
1096 : blume 818 and store16(ea, d, mem, an) =
1097 :     mark(I.MOVE{mvOp=I.MOVW, src=immedOrReg(operand d), dst=address(ea, mem)}, an)
1098 : george 545 and store32(ea, d, mem, an) =
1099 :     move'(immedOrReg(operand d), address(ea, mem), an)
1100 :    
1101 :     (* generate code for branching *)
1102 :     and branch(T.CMP(ty, cc, t1, t2), lab, an) =
1103 :     (* allow reordering of operands *)
1104 :     let val cc = cmp(true, ty, cc, t1, t2, [])
1105 :     in mark(I.JCC{cond=cond cc, opnd=immedLabel lab}, an) end
1106 :     | branch(T.FCMP(fty, fcc, t1, t2), lab, an) =
1107 :     fbranch(fty, fcc, t1, t2, lab, an)
1108 :     | branch(ccexp, lab, an) =
1109 : leunga 744 (doCCexpr(ccexp, C.eflags, []);
1110 : george 545 mark(I.JCC{cond=cond(Gen.condOf ccexp), opnd=immedLabel lab}, an)
1111 :     )
1112 :    
1113 :     (* generate code for floating point compare and branch *)
1114 :     and fbranch(fty, fcc, t1, t2, lab, an) =
1115 : leunga 731 let fun ignoreOrder (T.FREG _) = true
1116 :     | ignoreOrder (T.FLOAD _) = true
1117 :     | ignoreOrder (T.FMARK(e,_)) = ignoreOrder e
1118 :     | ignoreOrder _ = false
1119 :    
1120 :     fun compare'() = (* Sethi-Ullman style *)
1121 :     (if ignoreOrder t1 orelse ignoreOrder t2 then
1122 :     (reduceFexp(fty, t2, []); reduceFexp(fty, t1, []))
1123 :     else (reduceFexp(fty, t1, []); reduceFexp(fty, t2, []);
1124 :     emit(I.FXCH{opnd=C.ST(1)}));
1125 :     emit(I.FUCOMPP);
1126 :     fcc
1127 :     )
1128 :    
1129 :     fun compare''() =
1130 :     (* direct style *)
1131 :     (* Try to make lsrc the memory operand *)
1132 :     let val lsrc = foperand(fty, t1)
1133 :     val rsrc = foperand(fty, t2)
1134 :     val fsize = fsize fty
1135 :     fun cmp(lsrc, rsrc, fcc) =
1136 :     (emit(I.FCMP{fsize=fsize,lsrc=lsrc,rsrc=rsrc}); fcc)
1137 :     in case (lsrc, rsrc) of
1138 :     (I.FPR _, I.FPR _) => cmp(lsrc, rsrc, fcc)
1139 :     | (I.FPR _, mem) => cmp(mem,lsrc,T.Basis.swapFcond fcc)
1140 :     | (mem, I.FPR _) => cmp(lsrc, rsrc, fcc)
1141 :     | (lsrc, rsrc) => (* can't be both memory! *)
1142 :     let val ftmpR = newFreg()
1143 :     val ftmp = I.FPR ftmpR
1144 :     in emit(I.FMOVE{fsize=fsize,src=rsrc,dst=ftmp});
1145 :     cmp(lsrc, ftmp, fcc)
1146 :     end
1147 :     end
1148 :    
1149 :     fun compare() =
1150 :     if enableFastFPMode andalso !fast_floating_point
1151 :     then compare''() else compare'()
1152 :    
1153 : george 545 fun andil i = emit(I.BINARY{binOp=I.ANDL,src=I.Immed(i),dst=eax})
1154 : leunga 585 fun testil i = emit(I.TESTL{lsrc=eax,rsrc=I.Immed(i)})
1155 : george 545 fun xoril i = emit(I.BINARY{binOp=I.XORL,src=I.Immed(i),dst=eax})
1156 :     fun cmpil i = emit(I.CMPL{rsrc=I.Immed(i), lsrc=eax})
1157 :     fun j(cc, lab) = mark(I.JCC{cond=cc, opnd=immedLabel lab},an)
1158 :     fun sahf() = emit(I.SAHF)
1159 : leunga 731 fun branch(fcc) =
1160 : george 545 case fcc
1161 :     of T.== => (andil 0x4400; xoril 0x4000; j(I.EQ, lab))
1162 :     | T.?<> => (andil 0x4400; xoril 0x4000; j(I.NE, lab))
1163 :     | T.? => (sahf(); j(I.P,lab))
1164 :     | T.<=> => (sahf(); j(I.NP,lab))
1165 : leunga 585 | T.> => (testil 0x4500; j(I.EQ,lab))
1166 :     | T.?<= => (testil 0x4500; j(I.NE,lab))
1167 :     | T.>= => (testil 0x500; j(I.EQ,lab))
1168 :     | T.?< => (testil 0x500; j(I.NE,lab))
1169 : george 545 | T.< => (andil 0x4500; cmpil 0x100; j(I.EQ,lab))
1170 :     | T.?>= => (andil 0x4500; cmpil 0x100; j(I.NE,lab))
1171 :     | T.<= => (andil 0x4100; cmpil 0x100; j(I.EQ,lab);
1172 :     cmpil 0x4000; j(I.EQ,lab))
1173 : leunga 585 | T.?> => (sahf(); j(I.P,lab); testil 0x4100; j(I.EQ,lab))
1174 :     | T.<> => (testil 0x4400; j(I.EQ,lab))
1175 :     | T.?= => (testil 0x4400; j(I.NE,lab))
1176 : george 545 | _ => error "fbranch"
1177 :     (*esac*)
1178 : leunga 731 val fcc = compare()
1179 :     in emit I.FNSTSW;
1180 :     branch(fcc)
1181 : monnier 411 end
1182 : monnier 247
1183 : leunga 731 (*========================================================
1184 :     * Floating point code generation starts here.
1185 :     * Some generic fp routines first.
1186 :     *========================================================*)
1187 :    
1188 :     (* Can this tree be folded into the src operand of a floating point
1189 :     * operations?
1190 :     *)
1191 :     and foldableFexp(T.FREG _) = true
1192 :     | foldableFexp(T.FLOAD _) = true
1193 :     | foldableFexp(T.CVTI2F(_, (16 | 32), _)) = true
1194 :     | foldableFexp(T.CVTF2F(_, _, t)) = foldableFexp t
1195 :     | foldableFexp(T.FMARK(t, _)) = foldableFexp t
1196 :     | foldableFexp _ = false
1197 :    
1198 :     (* Move integer e of size ty into a memory location.
1199 :     * Returns a quadruple:
1200 :     * (INTEGER,return ty,effect address of memory location,cleanup code)
1201 :     *)
1202 :     and convertIntToFloat(ty, e) =
1203 :     let val opnd = operand e
1204 :     in if isMemOpnd opnd andalso (ty = 16 orelse ty = 32)
1205 :     then (INTEGER, ty, opnd, [])
1206 :     else
1207 : leunga 815 let val {instrs, tempMem, cleanup} =
1208 :     cvti2f{ty=ty, src=opnd, an=getAnnotations()}
1209 : leunga 731 in emits instrs;
1210 :     (INTEGER, 32, tempMem, cleanup)
1211 :     end
1212 :     end
1213 :    
1214 :     (*========================================================
1215 :     * Sethi-Ullman based floating point code generation as
1216 :     * implemented by Lal
1217 :     *========================================================*)
1218 :    
1219 : george 545 and fld(32, opnd) = I.FLDS opnd
1220 :     | fld(64, opnd) = I.FLDL opnd
1221 : george 555 | fld(80, opnd) = I.FLDT opnd
1222 : george 545 | fld _ = error "fld"
1223 :    
1224 : leunga 565 and fild(16, opnd) = I.FILD opnd
1225 :     | fild(32, opnd) = I.FILDL opnd
1226 :     | fild(64, opnd) = I.FILDLL opnd
1227 :     | fild _ = error "fild"
1228 :    
1229 :     and fxld(INTEGER, ty, opnd) = fild(ty, opnd)
1230 :     | fxld(REAL, fty, opnd) = fld(fty, opnd)
1231 :    
1232 : george 545 and fstp(32, opnd) = I.FSTPS opnd
1233 :     | fstp(64, opnd) = I.FSTPL opnd
1234 : george 555 | fstp(80, opnd) = I.FSTPT opnd
1235 : george 545 | fstp _ = error "fstp"
1236 :    
1237 :     (* generate code for floating point stores *)
1238 : leunga 731 and fstore'(fty, ea, d, mem, an) =
1239 : george 545 (case d of
1240 :     T.FREG(fty, fs) => emit(fld(fty, I.FDirect fs))
1241 :     | _ => reduceFexp(fty, d, []);
1242 :     mark(fstp(fty, address(ea, mem)), an)
1243 :     )
1244 :    
1245 : leunga 731 (* generate code for floating point loads *)
1246 :     and fload'(fty, ea, mem, fd, an) =
1247 :     let val ea = address(ea, mem)
1248 :     in mark(fld(fty, ea), an);
1249 : george 889 if CB.sameColor(fd,ST0) then ()
1250 : leunga 744 else emit(fstp(fty, I.FDirect fd))
1251 : leunga 731 end
1252 :    
1253 :     and fexpr' e = (reduceFexp(64, e, []); C.ST(0))
1254 : george 545
1255 :     (* generate floating point expression and put the result in fd *)
1256 : leunga 731 and doFexpr'(fty, T.FREG(_, fs), fd, an) =
1257 : george 889 (if CB.sameColor(fs,fd) then ()
1258 : george 545 else mark(I.FCOPY{dst=[fd], src=[fs], tmp=NONE}, an)
1259 :     )
1260 : leunga 731 | doFexpr'(_, T.FLOAD(fty, ea, mem), fd, an) =
1261 :     fload'(fty, ea, mem, fd, an)
1262 :     | doFexpr'(fty, T.FEXT fexp, fd, an) =
1263 :     (ExtensionComp.compileFext (reducer()) {e=fexp, fd=fd, an=an};
1264 : george 889 if CB.sameColor(fd,ST0) then () else emit(fstp(fty, I.FDirect fd))
1265 : leunga 731 )
1266 :     | doFexpr'(fty, e, fd, an) =
1267 : george 545 (reduceFexp(fty, e, []);
1268 : george 889 if CB.sameColor(fd,ST0) then ()
1269 : leunga 744 else mark(fstp(fty, I.FDirect fd), an)
1270 : george 545 )
1271 :    
1272 :     (*
1273 :     * Generate floating point expression using Sethi-Ullman's scheme:
1274 :     * This function evaluates a floating point expression,
1275 :     * and put result in %ST(0).
1276 :     *)
1277 :     and reduceFexp(fty, fexp, an) =
1278 : george 555 let val ST = I.ST(C.ST 0)
1279 :     val ST1 = I.ST(C.ST 1)
1280 : leunga 593 val cleanupCode = ref [] : I.instruction list ref
1281 : george 545
1282 : leunga 565 datatype su_tree =
1283 :     LEAF of int * T.fexp * ans
1284 :     | BINARY of int * T.fty * fbinop * su_tree * su_tree * ans
1285 :     | UNARY of int * T.fty * I.funOp * su_tree * ans
1286 :     and fbinop = FADD | FSUB | FMUL | FDIV
1287 :     | FIADD | FISUB | FIMUL | FIDIV
1288 :     withtype ans = Annotations.annotations
1289 : monnier 247
1290 : leunga 565 fun label(LEAF(n, _, _)) = n
1291 :     | label(BINARY(n, _, _, _, _, _)) = n
1292 :     | label(UNARY(n, _, _, _, _)) = n
1293 : george 545
1294 : leunga 565 fun annotate(LEAF(n, x, an), a) = LEAF(n,x,a::an)
1295 :     | annotate(BINARY(n,t,b,x,y,an), a) = BINARY(n,t,b,x,y,a::an)
1296 :     | annotate(UNARY(n,t,u,x,an), a) = UNARY(n,t,u,x,a::an)
1297 : george 545
1298 : leunga 565 (* Generate expression tree with sethi-ullman numbers *)
1299 :     fun su(e as T.FREG _) = LEAF(1, e, [])
1300 :     | su(e as T.FLOAD _) = LEAF(1, e, [])
1301 :     | su(e as T.CVTI2F _) = LEAF(1, e, [])
1302 :     | su(T.CVTF2F(_, _, t)) = su t
1303 :     | su(T.FMARK(t, a)) = annotate(su t, a)
1304 :     | su(T.FABS(fty, t)) = suUnary(fty, I.FABS, t)
1305 :     | su(T.FNEG(fty, t)) = suUnary(fty, I.FCHS, t)
1306 :     | su(T.FSQRT(fty, t)) = suUnary(fty, I.FSQRT, t)
1307 :     | su(T.FADD(fty, t1, t2)) = suComBinary(fty,FADD,FIADD,t1,t2)
1308 :     | su(T.FMUL(fty, t1, t2)) = suComBinary(fty,FMUL,FIMUL,t1,t2)
1309 :     | su(T.FSUB(fty, t1, t2)) = suBinary(fty,FSUB,FISUB,t1,t2)
1310 :     | su(T.FDIV(fty, t1, t2)) = suBinary(fty,FDIV,FIDIV,t1,t2)
1311 :     | su _ = error "su"
1312 :    
1313 :     (* Try to fold the the memory operand or integer conversion *)
1314 :     and suFold(e as T.FREG _) = (LEAF(0, e, []), false)
1315 :     | suFold(e as T.FLOAD _) = (LEAF(0, e, []), false)
1316 :     | suFold(e as T.CVTI2F(_,(16 | 32),_)) = (LEAF(0, e, []), true)
1317 :     | suFold(T.CVTF2F(_, _, t)) = suFold t
1318 :     | suFold(T.FMARK(t, a)) =
1319 :     let val (t, integer) = suFold t
1320 :     in (annotate(t, a), integer) end
1321 :     | suFold e = (su e, false)
1322 :    
1323 :     (* Form unary tree *)
1324 :     and suUnary(fty, funary, t) =
1325 :     let val t = su t
1326 :     in UNARY(label t, fty, funary, t, [])
1327 : george 545 end
1328 : leunga 565
1329 :     (* Form binary tree *)
1330 :     and suBinary(fty, binop, ibinop, t1, t2) =
1331 :     let val t1 = su t1
1332 :     val (t2, integer) = suFold t2
1333 :     val n1 = label t1
1334 :     val n2 = label t2
1335 :     val n = if n1=n2 then n1+1 else Int.max(n1,n2)
1336 :     val myOp = if integer then ibinop else binop
1337 :     in BINARY(n, fty, myOp, t1, t2, [])
1338 : george 545 end
1339 : george 555
1340 : leunga 565 (* Try to fold in the operand if possible.
1341 :     * This only applies to commutative operations.
1342 :     *)
1343 :     and suComBinary(fty, binop, ibinop, t1, t2) =
1344 : leunga 731 let val (t1, t2) = if foldableFexp t2
1345 :     then (t1, t2) else (t2, t1)
1346 : leunga 565 in suBinary(fty, binop, ibinop, t1, t2) end
1347 :    
1348 :     and sameTree(LEAF(_, T.FREG(t1,f1), []),
1349 : leunga 744 LEAF(_, T.FREG(t2,f2), [])) =
1350 : george 889 t1 = t2 andalso CB.sameColor(f1,f2)
1351 : leunga 565 | sameTree _ = false
1352 :    
1353 :     (* Traverse tree and generate code *)
1354 :     fun gencode(LEAF(_, t, an)) = mark(fxld(leafEA t), an)
1355 :     | gencode(BINARY(_, _, binop, x, t2 as LEAF(0, y, a1), a2)) =
1356 :     let val _ = gencode x
1357 :     val (_, fty, src) = leafEA y
1358 :     fun gen(code) = mark(code, a1 @ a2)
1359 :     fun binary(oper32, oper64) =
1360 :     if sameTree(x, t2) then
1361 :     gen(I.FBINARY{binOp=oper64, src=ST, dst=ST})
1362 : george 555 else
1363 :     let val oper =
1364 : leunga 565 if isMemOpnd src then
1365 :     case fty of
1366 :     32 => oper32
1367 :     | 64 => oper64
1368 :     | _ => error "gencode: BINARY"
1369 :     else oper64
1370 :     in gen(I.FBINARY{binOp=oper, src=src, dst=ST}) end
1371 :     fun ibinary(oper16, oper32) =
1372 :     let val oper = case fty of
1373 :     16 => oper16
1374 :     | 32 => oper32
1375 :     | _ => error "gencode: IBINARY"
1376 :     in gen(I.FIBINARY{binOp=oper, src=src}) end
1377 :     in case binop of
1378 :     FADD => binary(I.FADDS, I.FADDL)
1379 :     | FSUB => binary(I.FDIVS, I.FSUBL)
1380 :     | FMUL => binary(I.FMULS, I.FMULL)
1381 :     | FDIV => binary(I.FDIVS, I.FDIVL)
1382 :     | FIADD => ibinary(I.FIADDS, I.FIADDL)
1383 :     | FISUB => ibinary(I.FIDIVS, I.FISUBL)
1384 :     | FIMUL => ibinary(I.FIMULS, I.FIMULL)
1385 :     | FIDIV => ibinary(I.FIDIVS, I.FIDIVL)
1386 :     end
1387 :     | gencode(BINARY(_, fty, binop, t1, t2, an)) =
1388 :     let fun doit(t1, t2, oper, operP, operRP) =
1389 :     let (* oper[P] => ST(1) := ST oper ST(1); [pop]
1390 :     * operR[P] => ST(1) := ST(1) oper ST; [pop]
1391 :     *)
1392 :     val n1 = label t1
1393 :     val n2 = label t2
1394 :     in if n1 < n2 andalso n1 <= 7 then
1395 :     (gencode t2;
1396 :     gencode t1;
1397 :     mark(I.FBINARY{binOp=operP, src=ST, dst=ST1}, an))
1398 :     else if n2 <= n1 andalso n2 <= 7 then
1399 :     (gencode t1;
1400 :     gencode t2;
1401 :     mark(I.FBINARY{binOp=operRP, src=ST, dst=ST1}, an))
1402 :     else
1403 :     let (* both labels > 7 *)
1404 :     val fs = I.FDirect(newFreg())
1405 :     in gencode t2;
1406 :     emit(fstp(fty, fs));
1407 :     gencode t1;
1408 :     mark(I.FBINARY{binOp=oper, src=fs, dst=ST}, an)
1409 :     end
1410 :     end
1411 :     in case binop of
1412 :     FADD => doit(t1,t2,I.FADDL,I.FADDP,I.FADDP)
1413 :     | FMUL => doit(t1,t2,I.FMULL,I.FMULP,I.FMULP)
1414 :     | FSUB => doit(t1,t2,I.FSUBL,I.FSUBP,I.FSUBRP)
1415 :     | FDIV => doit(t1,t2,I.FDIVL,I.FDIVP,I.FDIVRP)
1416 : george 545 | _ => error "gencode.BINARY"
1417 :     end
1418 : leunga 565 | gencode(UNARY(_, _, unaryOp, su, an)) =
1419 :     (gencode(su); mark(I.FUNARY(unaryOp),an))
1420 :    
1421 :     (* Generate code for a leaf.
1422 :     * Returns the type and an effective address
1423 :     *)
1424 :     and leafEA(T.FREG(fty, f)) = (REAL, fty, I.FDirect f)
1425 :     | leafEA(T.FLOAD(fty, ea, mem)) = (REAL, fty, address(ea, mem))
1426 : leunga 593 | leafEA(T.CVTI2F(_, 32, t)) = int2real(32, t)
1427 :     | leafEA(T.CVTI2F(_, 16, t)) = int2real(16, t)
1428 :     | leafEA(T.CVTI2F(_, 8, t)) = int2real(8, t)
1429 : leunga 565 | leafEA _ = error "leafEA"
1430 :    
1431 : leunga 731 and int2real(ty, e) =
1432 :     let val (_, ty, ea, cleanup) = convertIntToFloat(ty, e)
1433 :     in cleanupCode := !cleanupCode @ cleanup;
1434 :     (INTEGER, ty, ea)
1435 : george 545 end
1436 : leunga 731
1437 :     in gencode(su fexp);
1438 :     emits(!cleanupCode)
1439 : george 545 end (*reduceFexp*)
1440 : leunga 731
1441 :     (*========================================================
1442 :     * This section generates 3-address style floating
1443 :     * point code.
1444 :     *========================================================*)
1445 :    
1446 :     and isize 16 = I.I16
1447 :     | isize 32 = I.I32
1448 :     | isize _ = error "isize"
1449 :    
1450 :     and fstore''(fty, ea, d, mem, an) =
1451 :     (floatingPointUsed := true;
1452 :     mark(I.FMOVE{fsize=fsize fty, dst=address(ea,mem),
1453 :     src=foperand(fty, d)},
1454 :     an)
1455 :     )
1456 :    
1457 :     and fload''(fty, ea, mem, d, an) =
1458 :     (floatingPointUsed := true;
1459 :     mark(I.FMOVE{fsize=fsize fty, src=address(ea,mem),
1460 :     dst=RealReg d}, an)
1461 :     )
1462 :    
1463 :     and fiload''(ity, ea, d, an) =
1464 :     (floatingPointUsed := true;
1465 :     mark(I.FILOAD{isize=isize ity, ea=ea, dst=RealReg d}, an)
1466 :     )
1467 :    
1468 :     and fexpr''(e as T.FREG(_,f)) =
1469 :     if isFMemReg f then transFexpr e else f
1470 :     | fexpr'' e = transFexpr e
1471 :    
1472 :     and transFexpr e =
1473 :     let val fd = newFreg() in doFexpr''(64, e, fd, []); fd end
1474 :    
1475 :     (*
1476 :     * Process a floating point operand. Put operand in register
1477 :     * when possible. The operand should match the given fty.
1478 :     *)
1479 :     and foperand(fty, e as T.FREG(fty', f)) =
1480 :     if fty = fty' then RealReg f else I.FPR(fexpr'' e)
1481 :     | foperand(fty, T.CVTF2F(_, _, e)) =
1482 :     foperand(fty, e) (* nop on the x86 *)
1483 :     | foperand(fty, e as T.FLOAD(fty', ea, mem)) =
1484 :     (* fold operand when the precison matches *)
1485 :     if fty = fty' then address(ea, mem) else I.FPR(fexpr'' e)
1486 :     | foperand(fty, e) = I.FPR(fexpr'' e)
1487 :    
1488 :     (*
1489 :     * Process a floating point operand.
1490 :     * Try to fold in a memory operand or conversion from an integer.
1491 :     *)
1492 :     and fioperand(T.FREG(fty,f)) = (REAL, fty, RealReg f, [])
1493 :     | fioperand(T.FLOAD(fty, ea, mem)) =
1494 :     (REAL, fty, address(ea, mem), [])
1495 :     | fioperand(T.CVTF2F(_, _, e)) = fioperand(e) (* nop on the x86 *)
1496 :     | fioperand(T.CVTI2F(_, ty, e)) = convertIntToFloat(ty, e)
1497 :     | fioperand(T.FMARK(e,an)) = fioperand(e) (* XXX *)
1498 :     | fioperand(e) = (REAL, 64, I.FPR(fexpr'' e), [])
1499 :    
1500 :     (* Generate binary operator. Since the real binary operators
1501 :     * does not take memory as destination, we also ensure this
1502 :     * does not happen.
1503 :     *)
1504 :     and fbinop(targetFty,
1505 :     binOp, binOpR, ibinOp, ibinOpR, lsrc, rsrc, fd, an) =
1506 :     (* Put the mem operand in rsrc *)
1507 :     let val _ = floatingPointUsed := true;
1508 :     fun isMemOpnd(T.FREG(_, f)) = isFMemReg f
1509 :     | isMemOpnd(T.FLOAD _) = true
1510 :     | isMemOpnd(T.CVTI2F(_, (16 | 32), _)) = true
1511 :     | isMemOpnd(T.CVTF2F(_, _, t)) = isMemOpnd t
1512 :     | isMemOpnd(T.FMARK(t, _)) = isMemOpnd t
1513 :     | isMemOpnd _ = false
1514 :     val (binOp, ibinOp, lsrc, rsrc) =
1515 :     if isMemOpnd lsrc then (binOpR, ibinOpR, rsrc, lsrc)
1516 :     else (binOp, ibinOp, lsrc, rsrc)
1517 :     val lsrc = foperand(targetFty, lsrc)
1518 :     val (kind, fty, rsrc, code) = fioperand(rsrc)
1519 :     fun dstMustBeFreg f =
1520 :     if targetFty <> 64 then
1521 :     let val tmpR = newFreg()
1522 :     val tmp = I.FPR tmpR
1523 :     in mark(f tmp, an);
1524 :     emit(I.FMOVE{fsize=fsize targetFty,
1525 :     src=tmp, dst=RealReg fd})
1526 :     end
1527 :     else mark(f(RealReg fd), an)
1528 :     in case kind of
1529 :     REAL =>
1530 :     dstMustBeFreg(fn dst =>
1531 :     I.FBINOP{fsize=fsize fty, binOp=binOp,
1532 :     lsrc=lsrc, rsrc=rsrc, dst=dst})
1533 :     | INTEGER =>
1534 :     (dstMustBeFreg(fn dst =>
1535 :     I.FIBINOP{isize=isize fty, binOp=ibinOp,
1536 :     lsrc=lsrc, rsrc=rsrc, dst=dst});
1537 :     emits code
1538 :     )
1539 :     end
1540 : george 545
1541 : leunga 731 and funop(fty, unOp, src, fd, an) =
1542 :     let val src = foperand(fty, src)
1543 :     in mark(I.FUNOP{fsize=fsize fty,
1544 :     unOp=unOp, src=src, dst=RealReg fd},an)
1545 :     end
1546 :    
1547 :     and doFexpr''(fty, e, fd, an) =
1548 :     case e of
1549 : george 889 T.FREG(_,fs) => if CB.sameColor(fs,fd) then ()
1550 : leunga 731 else fcopy''(fty, [fd], [fs], an)
1551 :     (* Stupid x86 does everything as 80-bits internally. *)
1552 :    
1553 :     (* Binary operators *)
1554 :     | T.FADD(_, a, b) => fbinop(fty,
1555 :     I.FADDL, I.FADDL, I.FIADDL, I.FIADDL,
1556 :     a, b, fd, an)
1557 :     | T.FSUB(_, a, b) => fbinop(fty,
1558 :     I.FSUBL, I.FSUBRL, I.FISUBL, I.FISUBRL,
1559 :     a, b, fd, an)
1560 :     | T.FMUL(_, a, b) => fbinop(fty,
1561 :     I.FMULL, I.FMULL, I.FIMULL, I.FIMULL,
1562 :     a, b, fd, an)
1563 :     | T.FDIV(_, a, b) => fbinop(fty,
1564 :     I.FDIVL, I.FDIVRL, I.FIDIVL, I.FIDIVRL,
1565 :     a, b, fd, an)
1566 :    
1567 :     (* Unary operators *)
1568 :     | T.FNEG(_, a) => funop(fty, I.FCHS, a, fd, an)
1569 :     | T.FABS(_, a) => funop(fty, I.FABS, a, fd, an)
1570 :     | T.FSQRT(_, a) => funop(fty, I.FSQRT, a, fd, an)
1571 :    
1572 :     (* Load *)
1573 :     | T.FLOAD(fty,ea,mem) => fload''(fty, ea, mem, fd, an)
1574 :    
1575 :     (* Type conversions *)
1576 :     | T.CVTF2F(_, _, e) => doFexpr''(fty, e, fd, an)
1577 :     | T.CVTI2F(_, ty, e) =>
1578 :     let val (_, ty, ea, cleanup) = convertIntToFloat(ty, e)
1579 :     in fiload''(ty, ea, fd, an);
1580 :     emits cleanup
1581 :     end
1582 :    
1583 :     | T.FMARK(e,A.MARKREG f) => (f fd; doFexpr''(fty, e, fd, an))
1584 :     | T.FMARK(e, a) => doFexpr''(fty, e, fd, a::an)
1585 :     | T.FPRED(e, c) => doFexpr''(fty, e, fd, A.CTRLUSE c::an)
1586 :     | T.FEXT fexp =>
1587 :     ExtensionComp.compileFext (reducer()) {e=fexp, fd=fd, an=an}
1588 :     | _ => error("doFexpr''")
1589 :    
1590 :     (*========================================================
1591 :     * Tie the two styles of fp code generation together
1592 :     *========================================================*)
1593 :     and fstore(fty, ea, d, mem, an) =
1594 :     if enableFastFPMode andalso !fast_floating_point
1595 :     then fstore''(fty, ea, d, mem, an)
1596 :     else fstore'(fty, ea, d, mem, an)
1597 :     and fload(fty, ea, d, mem, an) =
1598 :     if enableFastFPMode andalso !fast_floating_point
1599 :     then fload''(fty, ea, d, mem, an)
1600 :     else fload'(fty, ea, d, mem, an)
1601 :     and fexpr e =
1602 :     if enableFastFPMode andalso !fast_floating_point
1603 :     then fexpr'' e else fexpr' e
1604 :     and doFexpr(fty, e, fd, an) =
1605 :     if enableFastFPMode andalso !fast_floating_point
1606 :     then doFexpr''(fty, e, fd, an)
1607 :     else doFexpr'(fty, e, fd, an)
1608 :    
1609 : leunga 797 (*================================================================
1610 :     * Optimizations for x := x op y
1611 :     * Special optimizations:
1612 :     * Generate a binary operator, result must in memory.
1613 :     * The source must not be in memory
1614 :     *================================================================*)
1615 :     and binaryMem(binOp, src, dst, mem, an) =
1616 :     mark(I.BINARY{binOp=binOp, src=immedOrReg(operand src),
1617 :     dst=address(dst,mem)}, an)
1618 :     and unaryMem(unOp, opnd, mem, an) =
1619 :     mark(I.UNARY{unOp=unOp, opnd=address(opnd,mem)}, an)
1620 :    
1621 :     and isOne(T.LI n) = n = one
1622 :     | isOne _ = false
1623 :    
1624 :     (*
1625 :     * Perform optimizations based on recognizing
1626 :     * x := x op y or
1627 :     * x := y op x
1628 :     * first.
1629 :     *)
1630 :     and store(ty, ea, d, mem, an,
1631 :     {INC,DEC,ADD,SUB,NOT,NEG,SHL,SHR,SAR,OR,AND,XOR},
1632 :     doStore
1633 :     ) =
1634 :     let fun default() = doStore(ea, d, mem, an)
1635 :     fun binary1(t, t', unary, binary, ea', x) =
1636 :     if t = ty andalso t' = ty then
1637 :     if MLTreeUtils.eqRexp(ea, ea') then
1638 :     if isOne x then unaryMem(unary, ea, mem, an)
1639 :     else binaryMem(binary, x, ea, mem, an)
1640 :     else default()
1641 :     else default()
1642 :     fun unary(t,unOp, ea') =
1643 :     if t = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1644 :     unaryMem(unOp, ea, mem, an)
1645 :     else default()
1646 :     fun binary(t,t',binOp,ea',x) =
1647 :     if t = ty andalso t' = ty andalso
1648 :     MLTreeUtils.eqRexp(ea, ea') then
1649 :     binaryMem(binOp, x, ea, mem, an)
1650 :     else default()
1651 :    
1652 :     fun binaryCom1(t,unOp,binOp,x,y) =
1653 :     if t = ty then
1654 :     let fun again() =
1655 :     case y of
1656 :     T.LOAD(ty',ea',_) =>
1657 :     if ty' = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1658 :     if isOne x then unaryMem(unOp, ea, mem, an)
1659 :     else binaryMem(binOp,x,ea,mem,an)
1660 :     else default()
1661 :     | _ => default()
1662 :     in case x of
1663 :     T.LOAD(ty',ea',_) =>
1664 :     if ty' = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1665 :     if isOne y then unaryMem(unOp, ea, mem, an)
1666 :     else binaryMem(binOp,y,ea,mem,an)
1667 :     else again()
1668 :     | _ => again()
1669 :     end
1670 :     else default()
1671 :    
1672 :     fun binaryCom(t,binOp,x,y) =
1673 :     if t = ty then
1674 :     let fun again() =
1675 :     case y of
1676 :     T.LOAD(ty',ea',_) =>
1677 :     if ty' = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1678 :     binaryMem(binOp,x,ea,mem,an)
1679 :     else default()
1680 :     | _ => default()
1681 :     in case x of
1682 :     T.LOAD(ty',ea',_) =>
1683 :     if ty' = ty andalso MLTreeUtils.eqRexp(ea, ea') then
1684 :     binaryMem(binOp,y,ea,mem,an)
1685 :     else again()
1686 :     | _ => again()
1687 :     end
1688 :     else default()
1689 :    
1690 :     in case d of
1691 :     T.ADD(t,x,y) => binaryCom1(t,INC,ADD,x,y)
1692 :     | T.SUB(t,T.LOAD(t',ea',_),x) => binary1(t,t',DEC,SUB,ea',x)
1693 :     | T.ORB(t,x,y) => binaryCom(t,OR,x,y)
1694 :     | T.ANDB(t,x,y) => binaryCom(t,AND,x,y)
1695 :     | T.XORB(t,x,y) => binaryCom(t,XOR,x,y)
1696 :     | T.SLL(t,T.LOAD(t',ea',_),x) => binary(t,t',SHL,ea',x)
1697 :     | T.SRL(t,T.LOAD(t',ea',_),x) => binary(t,t',SHR,ea',x)
1698 :     | T.SRA(t,T.LOAD(t',ea',_),x) => binary(t,t',SAR,ea',x)
1699 :     | T.NEG(t,T.LOAD(t',ea',_)) => unary(t,NEG,ea')
1700 :     | T.NOTB(t,T.LOAD(t',ea',_)) => unary(t,NOT,ea')
1701 :     | _ => default()
1702 :     end (* store *)
1703 :    
1704 : george 545 (* generate code for a statement *)
1705 :     and stmt(T.MV(_, rd, e), an) = doExpr(e, rd, an)
1706 :     | stmt(T.FMV(fty, fd, e), an) = doFexpr(fty, e, fd, an)
1707 :     | stmt(T.CCMV(ccd, e), an) = doCCexpr(e, ccd, an)
1708 :     | stmt(T.COPY(_, dst, src), an) = copy(dst, src, an)
1709 :     | stmt(T.FCOPY(fty, dst, src), an) = fcopy(fty, dst, src, an)
1710 : leunga 744 | stmt(T.JMP(e, labs), an) = jmp(e, labs, an)
1711 : blume 839 | stmt(T.CALL{funct, targets, defs, uses, region, pops, ...}, an) =
1712 :     call(funct,targets,defs,uses,region,[],an, pops)
1713 :     | stmt(T.FLOW_TO(T.CALL{funct, targets, defs, uses, region, pops, ...},
1714 : leunga 796 cutTo), an) =
1715 : blume 839 call(funct,targets,defs,uses,region,cutTo,an, pops)
1716 : george 545 | stmt(T.RET _, an) = mark(I.RET NONE, an)
1717 : leunga 797 | stmt(T.STORE(8, ea, d, mem), an) =
1718 :     store(8, ea, d, mem, an, opcodes8, store8)
1719 :     | stmt(T.STORE(16, ea, d, mem), an) =
1720 :     store(16, ea, d, mem, an, opcodes16, store16)
1721 :     | stmt(T.STORE(32, ea, d, mem), an) =
1722 :     store(32, ea, d, mem, an, opcodes32, store32)
1723 :    
1724 : george 545 | stmt(T.FSTORE(fty, ea, d, mem), an) = fstore(fty, ea, d, mem, an)
1725 : leunga 744 | stmt(T.BCC(cc, lab), an) = branch(cc, lab, an)
1726 : george 545 | stmt(T.DEFINE l, _) = defineLabel l
1727 :     | stmt(T.ANNOTATION(s, a), an) = stmt(s, a::an)
1728 : george 555 | stmt(T.EXT s, an) =
1729 :     ExtensionComp.compileSext (reducer()) {stm=s, an=an}
1730 : george 545 | stmt(s, _) = doStmts(Gen.compileStm s)
1731 :    
1732 :     and doStmt s = stmt(s, [])
1733 :     and doStmts ss = app doStmt ss
1734 :    
1735 :     and beginCluster' _ =
1736 :     ((* Must be cleared by the client.
1737 :     * if rewriteMemReg then memRegsUsed := 0w0 else ();
1738 :     *)
1739 : leunga 731 floatingPointUsed := false;
1740 :     trapLabel := NONE;
1741 :     beginCluster 0
1742 :     )
1743 : george 545 and endCluster' a =
1744 : monnier 247 (case !trapLabel
1745 : monnier 411 of NONE => ()
1746 : george 545 | SOME(_, lab) => (defineLabel lab; emit(I.INTO))
1747 : monnier 411 (*esac*);
1748 : leunga 731 (* If floating point has been used allocate an extra
1749 :     * register just in case we didn't use any explicit register
1750 :     *)
1751 :     if !floatingPointUsed then (newFreg(); ())
1752 :     else ();
1753 : george 545 endCluster(a)
1754 :     )
1755 :    
1756 :     and reducer() =
1757 :     T.REDUCER{reduceRexp = expr,
1758 :     reduceFexp = fexpr,
1759 :     reduceCCexp = ccExpr,
1760 :     reduceStm = stmt,
1761 :     operand = operand,
1762 :     reduceOperand = reduceOpnd,
1763 :     addressOf = fn e => address(e, I.Region.memory), (*XXX*)
1764 :     emit = mark,
1765 :     instrStream = instrStream,
1766 :     mltreeStream = self()
1767 :     }
1768 :    
1769 :     and self() =
1770 :     S.STREAM
1771 : leunga 815 { beginCluster = beginCluster',
1772 :     endCluster = endCluster',
1773 :     emit = doStmt,
1774 :     pseudoOp = pseudoOp,
1775 :     defineLabel = defineLabel,
1776 :     entryLabel = entryLabel,
1777 :     comment = comment,
1778 :     annotation = annotation,
1779 :     getAnnotations = getAnnotations,
1780 :     exitBlock = fn mlrisc => exitBlock(cellset mlrisc)
1781 : george 545 }
1782 :    
1783 :     in self()
1784 : monnier 247 end
1785 :    
1786 : george 545 end (* functor *)
1787 :    
1788 :     end (* local *)

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