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 624 - (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 :     * -- Allen
26 : monnier 247 *)
27 : george 545 local
28 :     val rewriteMemReg = true (* should we rewrite memRegs *)
29 :     in
30 :    
31 : monnier 247 functor X86
32 :     (structure X86Instr : X86INSTR
33 :     structure X86MLTree : MLTREE
34 : george 555 structure ExtensionComp : MLTREE_EXTENSION_COMP
35 :     where I = X86Instr and T = X86MLTree
36 : monnier 475 sharing X86MLTree.Region = X86Instr.Region
37 : george 545 sharing X86MLTree.LabelExp = X86Instr.LabelExp
38 :     datatype arch = Pentium | PentiumPro | PentiumII | PentiumIII
39 :     val arch : arch ref
40 : leunga 593 val cvti2f :
41 :     (* source operand, guaranteed to be non-memory! *)
42 :     {ty: X86MLTree.ty, src: X86Instr.operand} ->
43 :     {instrs : X86Instr.instruction list,(* the instructions *)
44 :     tempMem: X86Instr.operand, (* temporary for CVTI2F *)
45 :     cleanup: X86Instr.instruction list (* cleanup code *)
46 :     }
47 : george 545 ) : sig include MLTREECOMP
48 :     val rewriteMemReg : bool
49 :     end =
50 : monnier 247 struct
51 :     structure T = X86MLTree
52 : monnier 429 structure S = T.Stream
53 : monnier 247 structure I = X86Instr
54 : george 545 structure C = I.C
55 :     structure Shuffle = Shuffle(I)
56 : monnier 247 structure W32 = Word32
57 : george 545 structure LE = I.LabelExp
58 :     structure A = MLRiscAnnotations
59 : monnier 247
60 : george 545 type instrStream = (I.instruction,C.regmap,C.cellset) T.stream
61 : george 555 type mltreeStream = (T.stm,C.regmap,T.mlrisc list) T.stream
62 : leunga 565
63 :     datatype kind = REAL | INTEGER
64 : george 545
65 :     structure Gen = MLTreeGen
66 :     (structure T = T
67 :     val intTy = 32
68 :     val naturalWidths = [32]
69 :     datatype rep = SE | ZE | NEITHER
70 :     val rep = NEITHER
71 :     )
72 :    
73 : monnier 411 fun error msg = MLRiscErrorMsg.error("X86",msg)
74 : monnier 247
75 : george 545 (* Should we perform automatic MemReg translation?
76 :     * If this is on, we can avoid doing RewritePseudo phase entirely.
77 :     *)
78 :     val rewriteMemReg = rewriteMemReg
79 :     fun isMemReg r = rewriteMemReg andalso r >= 8 andalso r < 32
80 : monnier 247
81 : george 555 val ST0 = C.ST 0
82 :     val ST7 = C.ST 7
83 :    
84 : george 545 (*
85 :     * The code generator
86 :     *)
87 : monnier 411 fun selectInstructions
88 : george 545 (instrStream as
89 :     S.STREAM{emit,defineLabel,entryLabel,pseudoOp,annotation,
90 : monnier 429 beginCluster,endCluster,exitBlock,alias,phi,comment,...}) =
91 : george 545 let exception EA
92 : monnier 411
93 : george 545 (* label where a trap is generated -- one per cluster *)
94 :     val trapLabel = ref (NONE: (I.instruction * Label.label) option)
95 : monnier 247
96 : george 545 (* effective address of an integer register *)
97 :     fun IntReg r = if isMemReg r then MemReg r else I.Direct r
98 :     and MemReg r =
99 :     ((* memRegsUsed := Word.orb(!memRegsUsed,
100 :     Word.<<(0w1, Word.fromInt r-0w8)); *)
101 :     I.MemReg r
102 :     )
103 : monnier 411
104 : george 545 (* Add an overflow trap *)
105 :     fun trap() =
106 :     let val jmp =
107 :     case !trapLabel of
108 :     NONE => let val label = Label.newLabel "trap"
109 :     val jmp = I.JCC{cond=I.O,
110 :     opnd=I.ImmedLabel(LE.LABEL label)}
111 :     in trapLabel := SOME(jmp, label); jmp end
112 :     | SOME(jmp, _) => jmp
113 :     in emit jmp end
114 : monnier 411
115 : george 545 val newReg = C.newReg
116 :     val newFreg = C.newFreg
117 : monnier 247
118 : george 545 (* mark an expression with a list of annotations *)
119 :     fun mark'(i,[]) = i
120 :     | mark'(i,a::an) = mark'(I.ANNOTATION{i=i,a=a},an)
121 : monnier 247
122 : george 545 (* annotate an expression and emit it *)
123 :     fun mark(i,an) = emit(mark'(i,an))
124 : monnier 247
125 : george 545 (* emit parallel copies for integers
126 :     * Translates parallel copies that involve memregs into
127 :     * individual copies.
128 :     *)
129 :     fun copy([], [], an) = ()
130 :     | copy(dst, src, an) =
131 :     let fun mvInstr{dst as I.MemReg rd, src as I.MemReg rs} =
132 :     if rd = rs then [] else
133 :     let val tmpR = I.Direct(newReg())
134 :     in [I.MOVE{mvOp=I.MOVL, src=src, dst=tmpR},
135 :     I.MOVE{mvOp=I.MOVL, src=tmpR, dst=dst}]
136 :     end
137 :     | mvInstr{dst=I.Direct rd, src=I.Direct rs} =
138 :     if rd = rs then []
139 :     else [I.COPY{dst=[rd], src=[rs], tmp=NONE}]
140 :     | mvInstr{dst, src} = [I.MOVE{mvOp=I.MOVL, src=src, dst=dst}]
141 :     in
142 :     app emit (Shuffle.shuffle{mvInstr=mvInstr, ea=IntReg}
143 :     {regmap=fn r => r, tmp=SOME(I.Direct(newReg())),
144 :     dst=dst, src=src})
145 :     end
146 :    
147 :     (* conversions *)
148 :     val itow = Word.fromInt
149 :     val wtoi = Word.toInt
150 :     fun toInt32 i = Int32.fromLarge(Int.toLarge i)
151 :     val w32toi32 = Word32.toLargeIntX
152 :     val i32tow32 = Word32.fromLargeInt
153 : monnier 247
154 : george 545 (* One day, this is going to bite us when precision(LargeInt)>32 *)
155 :     fun wToInt32 w = Int32.fromLarge(Word32.toLargeIntX w)
156 : monnier 247
157 : george 545 (* some useful registers *)
158 :     val eax = I.Direct(C.eax)
159 :     val ecx = I.Direct(C.ecx)
160 :     val edx = I.Direct(C.edx)
161 : monnier 247
162 : george 545 fun immedLabel lab = I.ImmedLabel(LE.LABEL lab)
163 :    
164 :     (* Is the expression zero? *)
165 :     fun isZero(T.LI 0) = true
166 :     | isZero(T.LI32 0w0) = true
167 :     | isZero(T.MARK(e,a)) = isZero e
168 :     | isZero _ = false
169 :     (* Does the expression set the zero bit?
170 :     * WARNING: we assume these things are not optimized out!
171 :     *)
172 :     fun setZeroBit(T.ANDB _) = true
173 :     | setZeroBit(T.ORB _) = true
174 :     | setZeroBit(T.XORB _) = true
175 :     | setZeroBit(T.SRA _) = true
176 :     | setZeroBit(T.SRL _) = true
177 :     | setZeroBit(T.SLL _) = true
178 :     | setZeroBit(T.MARK(e, _)) = setZeroBit e
179 :     | setZeroBit _ = false
180 : monnier 247
181 : george 545 (* emit parallel copies for floating point *)
182 :     fun fcopy(fty, [], [], _) = ()
183 :     | fcopy(fty, dst as [_], src as [_], an) =
184 :     mark(I.FCOPY{dst=dst,src=src,tmp=NONE}, an)
185 :     | fcopy(fty, dst, src, an) =
186 :     mark(I.FCOPY{dst=dst,src=src,tmp=SOME(I.FDirect(newFreg()))}, an)
187 : monnier 247
188 : george 545 (* Translates MLTREE condition code to x86 condition code *)
189 :     fun cond T.LT = I.LT | cond T.LTU = I.B
190 :     | cond T.LE = I.LE | cond T.LEU = I.BE
191 :     | cond T.EQ = I.EQ | cond T.NE = I.NE
192 :     | cond T.GE = I.GE | cond T.GEU = I.AE
193 :     | cond T.GT = I.GT | cond T.GTU = I.A
194 : monnier 247
195 : george 545 (* Move and annotate *)
196 :     fun move'(src as I.Direct s, dst as I.Direct d, an) =
197 :     if s=d then ()
198 :     else mark(I.COPY{dst=[d], src=[s], tmp=NONE}, an)
199 :     | move'(src, dst, an) = mark(I.MOVE{mvOp=I.MOVL, src=src, dst=dst}, an)
200 : monnier 247
201 : george 545 (* Move only! *)
202 :     fun move(src, dst) = move'(src, dst, [])
203 : monnier 247
204 : george 545 fun zero dst = emit(I.BINARY{binOp=I.XORL, src=dst, dst=dst})
205 : monnier 247
206 : george 545 val readonly = I.Region.readonly
207 : monnier 247
208 : george 545 (*
209 :     * Compute an effective address. This is a new version
210 :     *)
211 :     fun address(ea, mem) =
212 :     let (* tricky way to negate without overflow! *)
213 :     fun neg32 w = Word32.notb w + 0w1
214 : monnier 247
215 : george 545 (* Keep building a bigger and bigger effective address expressions
216 :     * The input is a list of trees
217 :     * b -- base
218 :     * i -- index
219 :     * s -- scale
220 :     * d -- immed displacement
221 :     *)
222 :     fun doEA([], b, i, s, d) = makeAddressingMode(b, i, s, d)
223 :     | doEA(t::trees, b, i, s, d) =
224 :     (case t of
225 :     T.LI n => doEAImmed(trees, n, b, i, s, d)
226 :     | T.LI32 n => doEAImmedw(trees, n, b, i, s, d)
227 :     | T.CONST c => doEALabel(trees, LE.CONST c, b, i, s, d)
228 :     | T.LABEL le => doEALabel(trees, le, b, i, s, d)
229 :     | T.ADD(32, t1, t2 as T.REG(_,r)) =>
230 :     if isMemReg r then doEA(t2::t1::trees, b, i, s, d)
231 :     else doEA(t1::t2::trees, b, i, s, d)
232 :     | T.ADD(32, t1, t2) => doEA(t1::t2::trees, b, i, s, d)
233 :     | T.SUB(32, t1, T.LI n) =>
234 :     (* can't overflow here *)
235 :     doEA(t1::T.LI32(neg32(Word32.fromInt n))::trees, b, i, s, d)
236 :     | T.SUB(32, t1, T.LI32 n) =>
237 :     doEA(t1::T.LI32(neg32 n)::trees, b, i, s, d)
238 :     | T.SLL(32, t1, T.LI 0) => displace(trees, t1, b, i, s, d)
239 :     | T.SLL(32, t1, T.LI 1) => indexed(trees, t1, t, 1, b, i, s, d)
240 :     | T.SLL(32, t1, T.LI 2) => indexed(trees, t1, t, 2, b, i, s, d)
241 :     | T.SLL(32, t1, T.LI 3) => indexed(trees, t1, t, 3, b, i, s, d)
242 :     | T.SLL(32, t1, T.LI32 0w0) => displace(trees, t1, b, i, s, d)
243 :     | T.SLL(32, t1, T.LI32 0w1) => indexed(trees,t1,t,1,b,i,s,d)
244 :     | T.SLL(32, t1, T.LI32 0w2) => indexed(trees,t1,t,2,b,i,s,d)
245 :     | T.SLL(32, t1, T.LI32 0w3) => indexed(trees,t1,t,3,b,i,s,d)
246 :     | t => displace(trees, t, b, i, s, d)
247 :     )
248 : monnier 247
249 : george 545 (* Add an immed constant *)
250 :     and doEAImmed(trees, 0, b, i, s, d) = doEA(trees, b, i, s, d)
251 :     | doEAImmed(trees, n, b, i, s, I.Immed m) =
252 :     doEA(trees, b, i, s, (* no overflow! *)
253 :     I.Immed(w32toi32(Word32.fromInt n + i32tow32 m)))
254 :     | doEAImmed(trees, n, b, i, s, I.ImmedLabel le) =
255 :     doEA(trees, b, i, s, I.ImmedLabel(LE.PLUS(le,LE.INT n)))
256 :     | doEAImmed(trees, n, b, i, s, _) = error "doEAImmed"
257 : monnier 247
258 : george 545 (* Add an immed32 constant *)
259 :     and doEAImmedw(trees, 0w0, b, i, s, d) = doEA(trees, b, i, s, d)
260 :     | doEAImmedw(trees, n, b, i, s, I.Immed m) =
261 :     (* no overflow! *)
262 :     doEA(trees, b, i, s, I.Immed(w32toi32(i32tow32 m + n)))
263 :     | doEAImmedw(trees, n, b, i, s, I.ImmedLabel le) =
264 :     doEA(trees, b, i, s,
265 :     I.ImmedLabel(LE.PLUS(le,LE.INT(Word32.toIntX n)))
266 :     handle Overflow => error "doEAImmedw: constant too large")
267 :     | doEAImmedw(trees, n, b, i, s, _) = error "doEAImmedw"
268 : monnier 247
269 : george 545 (* Add a label expression *)
270 :     and doEALabel(trees, le, b, i, s, I.Immed 0) =
271 :     doEA(trees, b, i, s, I.ImmedLabel le)
272 :     | doEALabel(trees, le, b, i, s, I.Immed m) =
273 :     doEA(trees, b, i, s,
274 :     I.ImmedLabel(LE.PLUS(le,LE.INT(Int32.toInt m)))
275 :     handle Overflow => error "doEALabel: constant too large")
276 :     | doEALabel(trees, le, b, i, s, I.ImmedLabel le') =
277 :     doEA(trees, b, i, s, I.ImmedLabel(LE.PLUS(le,le')))
278 :     | doEALabel(trees, le, b, i, s, _) = error "doEALabel"
279 : monnier 247
280 : george 545 and makeAddressingMode(NONE, NONE, _, disp) = disp
281 :     | makeAddressingMode(SOME base, NONE, _, disp) =
282 :     I.Displace{base=base, disp=disp, mem=mem}
283 :     | makeAddressingMode(base, SOME index, scale, disp) =
284 :     I.Indexed{base=base, index=index, scale=scale,
285 :     disp=disp, mem=mem}
286 : monnier 247
287 : george 545 (* generate code for tree and ensure that it is not in %esp *)
288 :     and exprNotEsp tree =
289 :     let val r = expr tree
290 :     in if r = C.esp then
291 :     let val tmp = newReg()
292 :     in move(I.Direct r, I.Direct tmp); tmp end
293 :     else r
294 :     end
295 : monnier 247
296 : george 545 (* Add a base register *)
297 :     and displace(trees, t, NONE, i, s, d) = (* no base yet *)
298 :     doEA(trees, SOME(expr t), i, s, d)
299 :     | displace(trees, t, b as SOME base, NONE, _, d) = (* no index *)
300 :     (* make t the index, but make sure that it is not %esp! *)
301 :     let val i = expr t
302 :     in if i = C.esp then
303 :     (* swap base and index *)
304 :     if base <> C.esp then
305 :     doEA(trees, SOME i, b, 0, d)
306 :     else (* base and index = %esp! *)
307 :     let val index = newReg()
308 :     in move(I.Direct i, I.Direct index);
309 :     doEA(trees, b, SOME index, 0, d)
310 :     end
311 :     else
312 :     doEA(trees, b, SOME i, 0, d)
313 :     end
314 :     | displace(trees, t, SOME base, i, s, d) = (* base and index *)
315 :     let val b = expr(T.ADD(32,T.REG(32,base),t))
316 :     in doEA(trees, SOME b, i, s, d) end
317 : monnier 247
318 : george 545 (* Add an indexed register *)
319 :     and indexed(trees, t, t0, scale, b, NONE, _, d) = (* no index yet *)
320 :     doEA(trees, b, SOME(exprNotEsp t), scale, d)
321 :     | indexed(trees, _, t0, _, NONE, i, s, d) = (* no base *)
322 :     doEA(trees, SOME(expr t0), i, s, d)
323 :     | indexed(trees, _, t0, _, SOME base, i, s, d) = (*base and index*)
324 :     let val b = expr(T.ADD(32, t0, T.REG(32, base)))
325 :     in doEA(trees, SOME b, i, s, d) end
326 :    
327 :     in case doEA([ea], NONE, NONE, 0, I.Immed 0) of
328 :     I.Immed _ => raise EA
329 :     | I.ImmedLabel le => I.LabelEA le
330 :     | ea => ea
331 :     end (* address *)
332 : monnier 247
333 : george 545 (* reduce an expression into an operand *)
334 :     and operand(T.LI i) = I.Immed(toInt32 i)
335 :     | operand(T.LI32 w) = I.Immed(wToInt32 w)
336 :     | operand(T.CONST c) = I.ImmedLabel(LE.CONST c)
337 :     | operand(T.LABEL lab) = I.ImmedLabel lab
338 :     | operand(T.REG(_,r)) = IntReg r
339 :     | operand(T.LOAD(32,ea,mem)) = address(ea, mem)
340 :     | operand(t) = I.Direct(expr t)
341 : monnier 247
342 : george 545 and moveToReg(opnd) =
343 :     let val dst = I.Direct(newReg())
344 :     in move(opnd, dst); dst
345 :     end
346 : monnier 247
347 : george 545 and reduceOpnd(I.Direct r) = r
348 :     | reduceOpnd opnd =
349 :     let val dst = newReg()
350 :     in move(opnd, I.Direct dst); dst
351 :     end
352 : monnier 247
353 : george 545 (* ensure that the operand is either an immed or register *)
354 :     and immedOrReg(opnd as I.Displace _) = moveToReg opnd
355 :     | immedOrReg(opnd as I.Indexed _) = moveToReg opnd
356 :     | immedOrReg(opnd as I.MemReg _) = moveToReg opnd
357 :     | immedOrReg(opnd as I.LabelEA _) = moveToReg opnd
358 :     | immedOrReg opnd = opnd
359 : monnier 247
360 : george 545 and isImmediate(I.Immed _) = true
361 :     | isImmediate(I.ImmedLabel _) = true
362 :     | isImmediate _ = false
363 : monnier 247
364 : george 545 and regOrMem opnd = if isImmediate opnd then moveToReg opnd else opnd
365 :    
366 :     and isMemOpnd opnd =
367 :     (case opnd of
368 :     I.Displace _ => true
369 :     | I.Indexed _ => true
370 :     | I.MemReg _ => true
371 :     | I.LabelEA _ => true
372 : george 555 | I.FDirect f => true
373 : george 545 | _ => false
374 :     )
375 :    
376 :     (*
377 :     * Compute an integer expression and put the result in
378 :     * the destination register rd.
379 :     *)
380 :     and doExpr(exp, rd : I.C.cell, an) =
381 :     let val rdOpnd = IntReg rd
382 : monnier 247
383 : george 545 fun equalRd(I.Direct r) = r = rd
384 :     | equalRd(I.MemReg r) = r = rd
385 :     | equalRd _ = false
386 : monnier 247
387 : george 545 (* Emit a binary operator. If the destination is
388 :     * a memReg, do something smarter.
389 :     *)
390 :     fun genBinary(binOp, opnd1, opnd2) =
391 :     if isMemReg rd andalso
392 :     (isMemOpnd opnd1 orelse isMemOpnd opnd2) orelse
393 :     equalRd(opnd2)
394 :     then
395 :     let val tmpR = newReg()
396 :     val tmp = I.Direct tmpR
397 :     in move(opnd1, tmp);
398 :     mark(I.BINARY{binOp=binOp, src=opnd2, dst=tmp}, an);
399 :     move(tmp, rdOpnd)
400 :     end
401 :     else
402 :     (move(opnd1, rdOpnd);
403 :     mark(I.BINARY{binOp=binOp, src=opnd2, dst=rdOpnd}, an)
404 :     )
405 : monnier 247
406 : george 545 (* Generate a binary operator; it may commute *)
407 :     fun binaryComm(binOp, e1, e2) =
408 :     let val (opnd1, opnd2) =
409 :     case (operand e1, operand e2) of
410 :     (x as I.Immed _, y) => (y, x)
411 :     | (x as I.ImmedLabel _, y) => (y, x)
412 :     | (x, y as I.Direct _) => (y, x)
413 :     | (x, y) => (x, y)
414 :     in genBinary(binOp, opnd1, opnd2)
415 :     end
416 :    
417 :     (* Generate a binary operator; non-commutative *)
418 :     fun binary(binOp, e1, e2) =
419 :     genBinary(binOp, operand e1, operand e2)
420 :    
421 :     (* Generate a unary operator *)
422 :     fun unary(unOp, e) =
423 :     let val opnd = operand e
424 :     in if isMemReg rd andalso isMemOpnd opnd then
425 :     let val tmp = I.Direct(newReg())
426 :     in move(opnd, tmp); move(tmp, rdOpnd)
427 :     end
428 :     else move(opnd, rdOpnd);
429 :     mark(I.UNARY{unOp=unOp, opnd=rdOpnd}, an)
430 :     end
431 :    
432 :     (* Generate shifts; the shift
433 :     * amount must be a constant or in %ecx *)
434 :     fun shift(opcode, e1, e2) =
435 :     let val (opnd1, opnd2) = (operand e1, operand e2)
436 :     in case opnd2 of
437 :     I.Immed _ => genBinary(opcode, opnd1, opnd2)
438 :     | _ =>
439 :     if equalRd(opnd2) then
440 :     let val tmpR = newReg()
441 :     val tmp = I.Direct tmpR
442 :     in move(opnd1, tmp);
443 :     move(opnd2, ecx);
444 :     mark(I.BINARY{binOp=opcode, src=ecx, dst=tmp},an);
445 :     move(tmp, rdOpnd)
446 :     end
447 :     else
448 :     (move(opnd1, rdOpnd);
449 :     move(opnd2, ecx);
450 :     mark(I.BINARY{binOp=opcode, src=ecx, dst=rdOpnd},an)
451 :     )
452 :     end
453 :    
454 :     (* Division or remainder: divisor must be in %edx:%eax pair *)
455 :     fun divrem(signed, overflow, e1, e2, resultReg) =
456 :     let val (opnd1, opnd2) = (operand e1, operand e2)
457 :     val _ = move(opnd1, eax)
458 : leunga 606 val oper = if signed then (emit(I.CDQ); I.IDIVL)
459 :     else (zero edx; I.DIVL)
460 : george 545 in mark(I.MULTDIV{multDivOp=oper, src=regOrMem opnd2},an);
461 :     move(resultReg, rdOpnd);
462 :     if overflow then trap() else ()
463 :     end
464 :    
465 :     (* Optimize the special case for division *)
466 :     fun divide(signed, overflow, e1, e2 as T.LI n) =
467 :     let fun isPowerOf2 w = Word.andb((w - 0w1), w) = 0w0
468 :     fun log2 n = (* n must be > 0!!! *)
469 :     let fun loop(0w1,pow) = pow
470 :     | loop(w,pow) = loop(Word.>>(w, 0w1),pow+1)
471 :     in loop(n,0) end
472 :     val w = Word.fromInt n
473 :     in if n > 1 andalso isPowerOf2 w then
474 :     let val pow = T.LI(log2 w)
475 :     in if signed then
476 :     (* signed; simulate round towards zero *)
477 :     let val label = Label.newLabel ""
478 :     val reg1 = expr e1
479 :     val opnd1 = I.Direct reg1
480 :     in if setZeroBit e1 then ()
481 :     else emit(I.CMPL{lsrc=opnd1, rsrc=I.Immed 0});
482 :     emit(I.JCC{cond=I.GE, opnd=immedLabel label});
483 :     emit(if n = 2 then
484 :     I.UNARY{unOp=I.INCL, opnd=opnd1}
485 :     else
486 :     I.BINARY{binOp=I.ADDL,
487 :     src=I.Immed(toInt32 n - 1),
488 :     dst=opnd1});
489 :     defineLabel label;
490 :     shift(I.SARL, T.REG(32, reg1), pow)
491 :     end
492 :     else (* unsigned *)
493 :     shift(I.SHRL, e1, pow)
494 :     end
495 :     else
496 :     (* note the only way we can overflow is if
497 :     * n = 0 or n = -1
498 :     *)
499 :     divrem(signed, overflow andalso (n = ~1 orelse n = 0),
500 :     e1, e2, eax)
501 :     end
502 :     | divide(signed, overflow, e1, e2) =
503 :     divrem(signed, overflow, e1, e2, eax)
504 : monnier 247
505 : george 545 fun rem(signed, overflow, e1, e2) =
506 :     divrem(signed, overflow, e1, e2, edx)
507 :    
508 :     (* unsigned integer multiplication *)
509 :     fun uMultiply(e1, e2) =
510 :     (* note e2 can never be (I.Direct edx) *)
511 :     (move(operand e1, eax);
512 : leunga 606 mark(I.MULTDIV{multDivOp=I.MULL,
513 : george 545 src=regOrMem(operand e2)},an);
514 :     move(eax, rdOpnd)
515 :     )
516 :    
517 :     (* signed integer multiplication:
518 :     * The only forms that are allowed that also sets the
519 :     * OF and CF flags are:
520 :     *
521 :     * imul r32, r32/m32, imm8
522 :     * imul r32, imm8
523 :     * imul r32, imm32
524 :     *)
525 :     fun multiply(e1, e2) =
526 :     let fun doit(i1 as I.Immed _, i2 as I.Immed _, dstR, dst) =
527 :     (move(i1, dst);
528 :     mark(I.MUL3{dst=dstR, src1=i2, src2=NONE},an))
529 :     | doit(rm, i2 as I.Immed _, dstR, dst) =
530 :     doit(i2, rm, dstR, dst)
531 :     | doit(imm as I.Immed(i), rm, dstR, dst) =
532 :     mark(I.MUL3{dst=dstR, src1=rm, src2=SOME i},an)
533 :     | doit(r1 as I.Direct _, r2 as I.Direct _, dstR, dst) =
534 :     (move(r1, dst);
535 :     mark(I.MUL3{dst=dstR, src1=r2, src2=NONE},an))
536 :     | doit(r1 as I.Direct _, rm, dstR, dst) =
537 :     (move(r1, dst);
538 :     mark(I.MUL3{dst=dstR, src1=rm, src2=NONE},an))
539 :     | doit(rm, r as I.Direct _, dstR, dst) =
540 :     doit(r, rm, dstR, dst)
541 :     | doit(rm1, rm2, dstR, dst) =
542 :     if equalRd rm2 then
543 :     let val tmpR = newReg()
544 :     val tmp = I.Direct tmpR
545 :     in move(rm1, tmp);
546 :     mark(I.MUL3{dst=tmpR, src1=rm2, src2=NONE},an);
547 :     move(tmp, dst)
548 :     end
549 :     else
550 :     (move(rm1, dst);
551 :     mark(I.MUL3{dst=dstR, src1=rm2, src2=NONE},an)
552 :     )
553 :     val (opnd1, opnd2) = (operand e1, operand e2)
554 :     in if isMemReg rd then (* destination must be a real reg *)
555 :     let val tmpR = newReg()
556 :     val tmp = I.Direct tmpR
557 :     in doit(opnd1, opnd2, tmpR, tmp);
558 :     move(tmp, rdOpnd)
559 :     end
560 :     else
561 :     doit(opnd1, opnd2, rd, rdOpnd)
562 :     end
563 : monnier 247
564 : george 545 (* Makes sure the destination must be a register *)
565 :     fun dstMustBeReg f =
566 :     if isMemReg rd then
567 :     let val tmpR = newReg()
568 :     val tmp = I.Direct(tmpR)
569 :     in f(tmpR, tmp); move(tmp, rdOpnd) end
570 :     else f(rd, rdOpnd)
571 : monnier 247
572 : george 545 (* Emit a load instruction; makes sure that the destination
573 :     * is a register
574 :     *)
575 :     fun genLoad(mvOp, ea, mem) =
576 :     dstMustBeReg(fn (_, dst) =>
577 :     mark(I.MOVE{mvOp=mvOp, src=address(ea, mem), dst=dst},an))
578 :    
579 :     (* Generate a zero extended loads *)
580 :     fun load8(ea, mem) = genLoad(I.MOVZBL, ea, mem)
581 :     fun load16(ea, mem) = genLoad(I.MOVZWL, ea, mem)
582 :     fun load8s(ea, mem) = genLoad(I.MOVSBL, ea, mem)
583 :     fun load16s(ea, mem) = genLoad(I.MOVSWL, ea, mem)
584 :     fun load32(ea, mem) = genLoad(I.MOVL, ea, mem)
585 :    
586 :     (* Generate a sign extended loads *)
587 :    
588 :     (* Generate setcc instruction:
589 :     * semantics: MV(rd, COND(_, T.CMP(ty, cc, t1, t2), yes, no))
590 : leunga 583 * Bug, if eax is either t1 or t2 then problem will occur!!!
591 :     * Note that we have to use eax as the destination of the
592 :     * setcc because it only works on the registers
593 :     * %al, %bl, %cl, %dl and %[abcd]h. The last four registers
594 :     * are inaccessible in 32 bit mode.
595 : george 545 *)
596 :     fun setcc(ty, cc, t1, t2, yes, no) =
597 : leunga 583 let val (cc, yes, no) =
598 :     if yes > no then (cc, yes, no)
599 :     else (T.Basis.negateCond cc, no, yes)
600 : george 545 in (* Clear the destination first.
601 :     * This this because stupid SETcc
602 :     * only writes to the low order
603 :     * byte. That's Intel architecture, folks.
604 :     *)
605 : leunga 583 zero eax;
606 : george 545 case (yes, no) of
607 :     (1, 0) => (* normal case *)
608 :     let val cc = cmp(true, ty, cc, t1, t2, [])
609 : leunga 583 in mark(I.SET{cond=cond cc, opnd=eax}, an);
610 :     move(eax, rdOpnd)
611 :     end
612 : george 545 | (C1, C2) =>
613 :     (* general case;
614 : leunga 583 * from the Intel optimization guide p3-5
615 :     *)
616 :     let val cc = cmp(true, ty, cc, t1, t2, [])
617 :     in case C1-C2 of
618 :     D as (1 | 2 | 3 | 4 | 5 | 8 | 9) =>
619 :     let val (base,scale) =
620 :     case D of
621 :     1 => (NONE, 0)
622 :     | 2 => (NONE, 1)
623 :     | 3 => (SOME C.eax, 1)
624 :     | 4 => (NONE, 2)
625 :     | 5 => (SOME C.eax, 2)
626 :     | 8 => (NONE, 3)
627 :     | 9 => (SOME C.eax, 3)
628 :     val addr = I.Indexed{base=base,
629 :     index=C.eax,
630 :     scale=scale,
631 :     disp=I.Immed C2,
632 : george 545 mem=readonly}
633 : leunga 583 val tmpR = newReg()
634 :     val tmp = I.Direct tmpR
635 :     in emit(I.SET{cond=cond cc, opnd=eax});
636 :     mark(I.LEA{r32=tmpR, addr=addr}, an);
637 :     move(tmp, rdOpnd)
638 :     end
639 :     | D =>
640 :     (emit(I.SET{cond=cond(T.Basis.negateCond cc),
641 :     opnd=eax});
642 :     emit(I.UNARY{unOp=I.DECL, opnd=eax});
643 :     emit(I.BINARY{binOp=I.ANDL,
644 :     src=I.Immed D, dst=eax});
645 :     if C2 = 0 then
646 :     move(eax, rdOpnd)
647 :     else
648 :     let val tmpR = newReg()
649 :     val tmp = I.Direct tmpR
650 :     in mark(I.LEA{addr=
651 :     I.Displace{
652 :     base=C.eax,
653 :     disp=I.Immed C2,
654 :     mem=readonly},
655 :     r32=tmpR}, an);
656 :     move(tmp, rdOpnd)
657 :     end
658 :     )
659 :     end
660 : george 545 end (* setcc *)
661 :    
662 :     (* Generate cmovcc instruction.
663 :     * on Pentium Pro and Pentium II only
664 :     *)
665 :     fun cmovcc(ty, cc, t1, t2, yes, no) =
666 :     let fun genCmov(dstR, _) =
667 :     let val _ = doExpr(no, dstR, []) (* false branch *)
668 :     val cc = cmp(true, ty, cc, t1, t2, []) (* compare *)
669 :     in mark(I.CMOV{cond=cond cc, src=operand yes, dst=dstR}, an)
670 :     end
671 :     in dstMustBeReg genCmov
672 :     end
673 :    
674 :     fun unknownExp exp = doExpr(Gen.compileRexp exp, rd, an)
675 : monnier 247
676 : leunga 606 (* Add n to rd *)
677 :     fun addN n =
678 :     let val n = operand n
679 :     val src = if isMemReg rd then immedOrReg n else n
680 :     in mark(I.BINARY{binOp=I.ADDL, src=src, dst=rdOpnd}, an) end
681 :    
682 : george 545 (* Generate addition *)
683 :     fun addition(e1, e2) =
684 : leunga 606 case e1 of
685 :     T.REG(_,rs) => if rs = rd then addN e2 else addition1(e1,e2)
686 :     | _ => addition1(e1,e2)
687 :     and addition1(e1, e2) =
688 :     case e2 of
689 :     T.REG(_,rs) => if rs = rd then addN e1 else addition2(e1,e2)
690 :     | _ => addition2(e1,e2)
691 :     and addition2(e1,e2) =
692 : george 545 (dstMustBeReg(fn (dstR, _) =>
693 :     mark(I.LEA{r32=dstR, addr=address(exp, readonly)}, an))
694 :     handle EA => binaryComm(I.ADDL, e1, e2))
695 : monnier 247
696 :    
697 : george 545 in case exp of
698 :     T.REG(_,rs) =>
699 :     if isMemReg rs andalso isMemReg rd then
700 :     let val tmp = I.Direct(newReg())
701 :     in move'(MemReg rs, tmp, an);
702 :     move'(tmp, rdOpnd, [])
703 :     end
704 :     else move'(IntReg rs, rdOpnd, an)
705 :     | (T.LI 0 | T.LI32 0w0) =>
706 :     (* As per Fermin's request, special optimization for rd := 0.
707 :     * Currently we don't bother with the size.
708 :     *)
709 :     if isMemReg rd then move'(I.Immed 0, rdOpnd, an)
710 :     else mark(I.BINARY{binOp=I.XORL, src=rdOpnd, dst=rdOpnd}, an)
711 :     | T.LI n => move'(I.Immed(toInt32 n), rdOpnd, an)
712 :     | T.LI32 w => move'(I.Immed(wToInt32 w), rdOpnd, an)
713 :     | T.CONST c => move'(I.ImmedLabel(LE.CONST c), rdOpnd, an)
714 :     | T.LABEL lab => move'(I.ImmedLabel lab, rdOpnd, an)
715 : monnier 247
716 : george 545 (* 32-bit addition *)
717 :     | T.ADD(32, e, (T.LI 1|T.LI32 0w1)) => unary(I.INCL, e)
718 :     | T.ADD(32, (T.LI 1|T.LI32 0w1), e) => unary(I.INCL, e)
719 :     | T.ADD(32, e, T.LI ~1) => unary(I.DECL, e)
720 :     | T.ADD(32, T.LI ~1, e) => unary(I.DECL, e)
721 :     | T.ADD(32, e1, e2) => addition(e1, e2)
722 : monnier 247
723 : george 545 (* 32-bit subtraction *)
724 :     | T.SUB(32, e, (T.LI 1 | T.LI32 0w1)) => unary(I.DECL, e)
725 :     | T.SUB(32, e, T.LI ~1) => unary(I.INCL, e)
726 :     | T.SUB(32, (T.LI 0 | T.LI32 0w0), e) => unary(I.NEGL, e)
727 : monnier 247
728 : george 545 (* Never mind:
729 :     | T.SUB(32, e1, e2 as T.LI n) =>
730 :     (mark(I.LEA{r32=rd, addr=address(T.ADD(32, e1, T.LI(~n)),
731 :     I.Region.readonly)}, an)
732 :     handle (Overflow|EA) => binary(I.SUBL, e1, e2))
733 :     *)
734 :     | T.SUB(32, e1, e2) => binary(I.SUBL, e1, e2)
735 : monnier 247
736 : george 545 | T.MULU(32, x, y) => uMultiply(x, y)
737 :     | T.DIVU(32, x, y) => divide(false, false, x, y)
738 :     | T.REMU(32, x, y) => rem(false, false, x, y)
739 : monnier 247
740 : george 545 | T.MULS(32, x, y) => multiply(x, y)
741 :     | T.DIVS(32, x, y) => divide(true, false, x, y)
742 :     | T.REMS(32, x, y) => rem(true, false, x, y)
743 : monnier 247
744 : george 545 | T.ADDT(32, x, y) => (binaryComm(I.ADDL, x, y); trap())
745 :     | T.SUBT(32, x, y) => (binary(I.SUBL, x, y); trap())
746 :     | T.MULT(32, x, y) => (multiply(x, y); trap())
747 :     | T.DIVT(32, x, y) => divide(true, true, x, y)
748 :     | T.REMT(32, x, y) => rem(true, true, x, y)
749 : monnier 247
750 : george 545 | T.ANDB(32, x, y) => binaryComm(I.ANDL, x, y)
751 :     | T.ORB(32, x, y) => binaryComm(I.ORL, x, y)
752 :     | T.XORB(32, x, y) => binaryComm(I.XORL, x, y)
753 :     | T.NOTB(32, x) => unary(I.NOTL, x)
754 : monnier 247
755 : george 545 | T.SRA(32, x, y) => shift(I.SARL, x, y)
756 :     | T.SRL(32, x, y) => shift(I.SHRL, x, y)
757 :     | T.SLL(32, x, y) => shift(I.SHLL, x, y)
758 : monnier 247
759 : george 545 | T.LOAD(8, ea, mem) => load8(ea, mem)
760 :     | T.LOAD(16, ea, mem) => load16(ea, mem)
761 :     | T.LOAD(32, ea, mem) => load32(ea, mem)
762 :     | T.CVTI2I(_,T.SIGN_EXTEND,_,T.LOAD(8,ea,mem)) => load8s(ea, mem)
763 :     | T.CVTI2I(_,T.SIGN_EXTEND,_,T.LOAD(16,ea,mem)) => load16s(ea, mem)
764 : monnier 498
765 : george 545 | T.COND(32, T.CMP(ty, cc, t1, t2), T.LI yes, T.LI no) =>
766 : leunga 583 setcc(ty, cc, t1, t2, toInt32 yes, toInt32 no)
767 :     | T.COND(32, T.CMP(ty, cc, t1, t2), T.LI32 yes, T.LI32 no) =>
768 :     setcc(ty, cc, t1, t2, Word32.toLargeIntX yes,
769 :     Word32.toLargeIntX no)
770 : george 545 | T.COND(32, T.CMP(ty, cc, t1, t2), yes, no) =>
771 :     (case !arch of (* PentiumPro and higher has CMOVcc *)
772 :     Pentium => unknownExp exp
773 :     | _ => cmovcc(ty, cc, t1, t2, yes, no)
774 :     )
775 :     | T.LET(s,e) => (doStmt s; doExpr(e, rd, an))
776 :     | T.MARK(e, A.MARKREG f) => (f rd; doExpr(e, rd, an))
777 :     | T.MARK(e, a) => doExpr(e, rd, a::an)
778 :     | T.PRED(e,c) => doExpr(e, rd, A.CTRLUSE c::an)
779 : george 555 | T.REXT e =>
780 :     ExtensionComp.compileRext (reducer()) {e=e, rd=rd, an=an}
781 : george 545 (* simplify and try again *)
782 :     | exp => unknownExp exp
783 :     end (* doExpr *)
784 : monnier 247
785 : george 545 (* generate an expression and return its result register
786 :     * If rewritePseudo is on, the result is guaranteed to be in a
787 :     * non memReg register
788 :     *)
789 :     and expr(exp as T.REG(_, rd)) =
790 :     if isMemReg rd then genExpr exp else rd
791 :     | expr exp = genExpr exp
792 : monnier 247
793 : george 545 and genExpr exp =
794 :     let val rd = newReg() in doExpr(exp, rd, []); rd end
795 : monnier 247
796 : george 545 (* Compare an expression with zero.
797 :     * On the x86, TEST is superior to AND for doing the same thing,
798 :     * since it doesn't need to write out the result in a register.
799 :     *)
800 :     and cmpWithZero(cc as (T.EQ | T.NE), e as T.ANDB(ty, a, b)) =
801 :     (case ty of
802 :     8 => test(I.TESTB, a, b)
803 :     | 16 => test(I.TESTW, a, b)
804 :     | 32 => test(I.TESTL, a, b)
805 :     | _ => (expr e; ())
806 :     ; cc)
807 :     | cmpWithZero(cc, e) = (expr e; cc)
808 : monnier 247
809 : george 545 (* Emit a test.
810 :     * The available modes are
811 :     * r/m, r
812 :     * r/m, imm
813 :     * On selecting the right instruction: TESTL/TESTW/TESTB.
814 :     * When anding an operand with a constant
815 :     * that fits within 8 (or 16) bits, it is possible to use TESTB,
816 :     * (or TESTW) instead of TESTL. Because x86 is little endian,
817 :     * this works for memory operands too. However, with TESTB, it is
818 :     * not possible to use registers other than
819 :     * AL, CL, BL, DL, and AH, CH, BH, DH. So, the best way is to
820 :     * perform register allocation first, and if the operand registers
821 :     * are one of EAX, ECX, EBX, or EDX, replace the TESTL instruction
822 :     * by TESTB.
823 :     *)
824 :     and test(testopcode, a, b) =
825 :     let val (_, opnd1, opnd2) = commuteComparison(T.EQ, true, a, b)
826 :     (* translate r, r/m => r/m, r *)
827 :     val (opnd1, opnd2) =
828 :     if isMemOpnd opnd2 then (opnd2, opnd1) else (opnd1, opnd2)
829 :     in emit(testopcode{lsrc=opnd1, rsrc=opnd2})
830 :     end
831 : monnier 247
832 : george 545 (* generate a condition code expression
833 :     * The zero is for setting the condition code!
834 :     * I have no idea why this is used.
835 :     *)
836 :     and doCCexpr(T.CMP(ty, cc, t1, t2), 0, an) =
837 :     (cmp(false, ty, cc, t1, t2, an); ())
838 :     | doCCexpr(T.CCMARK(e,A.MARKREG f),rd,an) = (f rd; doCCexpr(e,rd,an))
839 :     | doCCexpr(T.CCMARK(e,a), rd, an) = doCCexpr(e,rd,a::an)
840 :     | doCCexpr(T.CCEXT e, cd, an) =
841 : george 555 ExtensionComp.compileCCext (reducer()) {e=e, ccd=cd, an=an}
842 : george 545 | doCCexpr _ = error "doCCexpr"
843 : monnier 247
844 : george 545 and ccExpr e = error "ccExpr"
845 : monnier 247
846 : george 545 (* generate a comparison and sets the condition code;
847 :     * return the actual cc used. If the flag swapable is true,
848 :     * we can also reorder the operands.
849 :     *)
850 :     and cmp(swapable, ty, cc, t1, t2, an) =
851 :     (case cc of
852 :     (T.EQ | T.NE) =>
853 :     (* Sometimes the comparison is not necessary because
854 :     * the bits are already set!
855 :     *)
856 :     if isZero t1 andalso setZeroBit t2 then cmpWithZero(cc, t2)
857 :     else if isZero t2 andalso setZeroBit t1 then cmpWithZero(cc, t1)
858 :     (* == and <> can be reordered *)
859 :     else genCmp(ty, true, cc, t1, t2, an)
860 :     | _ => genCmp(ty, swapable, cc, t1, t2, an)
861 :     )
862 : monnier 247
863 : george 545 (* Give a and b which are the operands to a comparison (or test)
864 :     * Return the appropriate condition code and operands.
865 :     * The available modes are:
866 :     * r/m, imm
867 :     * r/m, r
868 :     * r, r/m
869 :     *)
870 :     and commuteComparison(cc, swapable, a, b) =
871 :     let val (opnd1, opnd2) = (operand a, operand b)
872 :     in (* Try to fold in the operands whenever possible *)
873 :     case (isImmediate opnd1, isImmediate opnd2) of
874 :     (true, true) => (cc, moveToReg opnd1, opnd2)
875 :     | (true, false) =>
876 :     if swapable then (T.Basis.swapCond cc, opnd2, opnd1)
877 :     else (cc, moveToReg opnd1, opnd2)
878 :     | (false, true) => (cc, opnd1, opnd2)
879 :     | (false, false) =>
880 :     (case (opnd1, opnd2) of
881 :     (_, I.Direct _) => (cc, opnd1, opnd2)
882 :     | (I.Direct _, _) => (cc, opnd1, opnd2)
883 :     | (_, _) => (cc, moveToReg opnd1, opnd2)
884 :     )
885 :     end
886 :    
887 :     (* generate a real comparison; return the real cc used *)
888 :     and genCmp(ty, swapable, cc, a, b, an) =
889 :     let val (cc, opnd1, opnd2) = commuteComparison(cc, swapable, a, b)
890 :     in mark(I.CMPL{lsrc=opnd1, rsrc=opnd2}, an); cc
891 :     end
892 : monnier 247
893 : george 545 (* generate code for jumps *)
894 :     and jmp(T.LABEL(lexp as LE.LABEL lab), labs, an) =
895 :     mark(I.JMP(I.ImmedLabel lexp, [lab]), an)
896 :     | jmp(T.LABEL lexp, labs, an) = mark(I.JMP(I.ImmedLabel lexp, labs), an)
897 :     | jmp(ea, labs, an) = mark(I.JMP(operand ea, labs), an)
898 :    
899 :     (* convert mlrisc to cellset:
900 :     *)
901 :     and cellset mlrisc =
902 :     let val addCCReg = C.addCell C.CC
903 :     fun g([],acc) = acc
904 :     | g(T.GPR(T.REG(_,r))::regs,acc) = g(regs,C.addReg(r,acc))
905 :     | g(T.FPR(T.FREG(_,f))::regs,acc) = g(regs,C.addFreg(f,acc))
906 :     | g(T.CCR(T.CC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc))
907 :     | g(T.CCR(T.FCC(_,cc))::regs,acc) = g(regs,addCCReg(cc,acc))
908 :     | g(_::regs, acc) = g(regs, acc)
909 :     in g(mlrisc, C.empty) end
910 :    
911 :     (* generate code for calls *)
912 :     and call(ea, flow, def, use, mem, an) =
913 :     mark(I.CALL(operand ea,cellset(def),cellset(use),mem),an)
914 :    
915 :     (* generate code for integer stores *)
916 :     and store8(ea, d, mem, an) =
917 :     let val src = (* movb has to use %eax as source. Stupid x86! *)
918 :     case immedOrReg(operand d) of
919 :     src as I.Direct r =>
920 :     if r = C.eax then src else (move(src, eax); eax)
921 :     | src => src
922 :     in mark(I.MOVE{mvOp=I.MOVB, src=src, dst=address(ea,mem)},an)
923 :     end
924 :     and store16(ea, d, mem, an) = error "store16"
925 :     and store32(ea, d, mem, an) =
926 :     move'(immedOrReg(operand d), address(ea, mem), an)
927 :    
928 :     (* generate code for branching *)
929 :     and branch(T.CMP(ty, cc, t1, t2), lab, an) =
930 :     (* allow reordering of operands *)
931 :     let val cc = cmp(true, ty, cc, t1, t2, [])
932 :     in mark(I.JCC{cond=cond cc, opnd=immedLabel lab}, an) end
933 :     | branch(T.FCMP(fty, fcc, t1, t2), lab, an) =
934 :     fbranch(fty, fcc, t1, t2, lab, an)
935 :     | branch(ccexp, lab, an) =
936 :     (doCCexpr(ccexp, 0, []);
937 :     mark(I.JCC{cond=cond(Gen.condOf ccexp), opnd=immedLabel lab}, an)
938 :     )
939 :    
940 :     (* generate code for floating point compare and branch *)
941 :     and fbranch(fty, fcc, t1, t2, lab, an) =
942 :     let fun compare() =
943 :     let fun ignoreOrder (T.FREG _) = true
944 :     | ignoreOrder (T.FLOAD _) = true
945 :     | ignoreOrder (T.FMARK(e,_)) = ignoreOrder e
946 :     | ignoreOrder _ = false
947 :     in if ignoreOrder t1 orelse ignoreOrder t2 then
948 :     (reduceFexp(fty, t2, []); reduceFexp(fty, t1, []))
949 :     else (reduceFexp(fty, t1, []); reduceFexp(fty, t2, []);
950 :     emit(I.FXCH{opnd=C.ST(1)}));
951 :     emit(I.FUCOMPP)
952 : monnier 411 end
953 : george 545 fun andil i = emit(I.BINARY{binOp=I.ANDL,src=I.Immed(i),dst=eax})
954 : leunga 585 fun testil i = emit(I.TESTL{lsrc=eax,rsrc=I.Immed(i)})
955 : george 545 fun xoril i = emit(I.BINARY{binOp=I.XORL,src=I.Immed(i),dst=eax})
956 :     fun cmpil i = emit(I.CMPL{rsrc=I.Immed(i), lsrc=eax})
957 :     fun j(cc, lab) = mark(I.JCC{cond=cc, opnd=immedLabel lab},an)
958 :     fun sahf() = emit(I.SAHF)
959 :     fun branch() =
960 :     case fcc
961 :     of T.== => (andil 0x4400; xoril 0x4000; j(I.EQ, lab))
962 :     | T.?<> => (andil 0x4400; xoril 0x4000; j(I.NE, lab))
963 :     | T.? => (sahf(); j(I.P,lab))
964 :     | T.<=> => (sahf(); j(I.NP,lab))
965 : leunga 585 | T.> => (testil 0x4500; j(I.EQ,lab))
966 :     | T.?<= => (testil 0x4500; j(I.NE,lab))
967 :     | T.>= => (testil 0x500; j(I.EQ,lab))
968 :     | T.?< => (testil 0x500; j(I.NE,lab))
969 : george 545 | T.< => (andil 0x4500; cmpil 0x100; j(I.EQ,lab))
970 :     | T.?>= => (andil 0x4500; cmpil 0x100; j(I.NE,lab))
971 :     | T.<= => (andil 0x4100; cmpil 0x100; j(I.EQ,lab);
972 :     cmpil 0x4000; j(I.EQ,lab))
973 : leunga 585 | T.?> => (sahf(); j(I.P,lab); testil 0x4100; j(I.EQ,lab))
974 :     | T.<> => (testil 0x4400; j(I.EQ,lab))
975 :     | T.?= => (testil 0x4400; j(I.NE,lab))
976 : george 545 | _ => error "fbranch"
977 :     (*esac*)
978 :     in compare(); emit I.FNSTSW; branch()
979 : monnier 411 end
980 : monnier 247
981 : george 545 and fld(32, opnd) = I.FLDS opnd
982 :     | fld(64, opnd) = I.FLDL opnd
983 : george 555 | fld(80, opnd) = I.FLDT opnd
984 : george 545 | fld _ = error "fld"
985 :    
986 : leunga 565 and fild(16, opnd) = I.FILD opnd
987 :     | fild(32, opnd) = I.FILDL opnd
988 :     | fild(64, opnd) = I.FILDLL opnd
989 :     | fild _ = error "fild"
990 :    
991 :     and fxld(INTEGER, ty, opnd) = fild(ty, opnd)
992 :     | fxld(REAL, fty, opnd) = fld(fty, opnd)
993 :    
994 : george 545 and fstp(32, opnd) = I.FSTPS opnd
995 :     | fstp(64, opnd) = I.FSTPL opnd
996 : george 555 | fstp(80, opnd) = I.FSTPT opnd
997 : george 545 | fstp _ = error "fstp"
998 :    
999 :     (* generate code for floating point stores *)
1000 :     and fstore(fty, ea, d, mem, an) =
1001 :     (case d of
1002 :     T.FREG(fty, fs) => emit(fld(fty, I.FDirect fs))
1003 :     | _ => reduceFexp(fty, d, []);
1004 :     mark(fstp(fty, address(ea, mem)), an)
1005 :     )
1006 :    
1007 : leunga 593 and fexpr e = (reduceFexp(64, e, []); C.ST(0))
1008 : george 545
1009 :     (* generate floating point expression and put the result in fd *)
1010 :     and doFexpr(fty, T.FREG(_, fs), fd, an) =
1011 :     (if fs = fd then ()
1012 :     else mark(I.FCOPY{dst=[fd], src=[fs], tmp=NONE}, an)
1013 :     )
1014 :     | doFexpr(fty, T.FLOAD(fty', ea, mem), fd, an) =
1015 :     let val ea = address(ea, mem)
1016 :     in mark(fld(fty', ea), an);
1017 : leunga 624 if fd = ST0 then () else emit(fstp(fty, I.FDirect fd))
1018 : george 545 end
1019 :     | doFexpr(fty, e, fd, an) =
1020 :     (reduceFexp(fty, e, []);
1021 : leunga 624 if fd = ST0 then () else mark(fstp(fty, I.FDirect fd), an)
1022 : george 545 )
1023 :    
1024 :     (*
1025 :     * Generate floating point expression using Sethi-Ullman's scheme:
1026 :     * This function evaluates a floating point expression,
1027 :     * and put result in %ST(0).
1028 :     *)
1029 :     and reduceFexp(fty, fexp, an) =
1030 : george 555 let val ST = I.ST(C.ST 0)
1031 :     val ST1 = I.ST(C.ST 1)
1032 : leunga 593 val cleanupCode = ref [] : I.instruction list ref
1033 : george 545
1034 : leunga 565 datatype su_tree =
1035 :     LEAF of int * T.fexp * ans
1036 :     | BINARY of int * T.fty * fbinop * su_tree * su_tree * ans
1037 :     | UNARY of int * T.fty * I.funOp * su_tree * ans
1038 :     and fbinop = FADD | FSUB | FMUL | FDIV
1039 :     | FIADD | FISUB | FIMUL | FIDIV
1040 :     withtype ans = Annotations.annotations
1041 : monnier 247
1042 : leunga 565 fun label(LEAF(n, _, _)) = n
1043 :     | label(BINARY(n, _, _, _, _, _)) = n
1044 :     | label(UNARY(n, _, _, _, _)) = n
1045 : george 545
1046 : leunga 565 fun annotate(LEAF(n, x, an), a) = LEAF(n,x,a::an)
1047 :     | annotate(BINARY(n,t,b,x,y,an), a) = BINARY(n,t,b,x,y,a::an)
1048 :     | annotate(UNARY(n,t,u,x,an), a) = UNARY(n,t,u,x,a::an)
1049 : george 545
1050 : leunga 565 (* Generate expression tree with sethi-ullman numbers *)
1051 :     fun su(e as T.FREG _) = LEAF(1, e, [])
1052 :     | su(e as T.FLOAD _) = LEAF(1, e, [])
1053 :     | su(e as T.CVTI2F _) = LEAF(1, e, [])
1054 :     | su(T.CVTF2F(_, _, t)) = su t
1055 :     | su(T.FMARK(t, a)) = annotate(su t, a)
1056 :     | su(T.FABS(fty, t)) = suUnary(fty, I.FABS, t)
1057 :     | su(T.FNEG(fty, t)) = suUnary(fty, I.FCHS, t)
1058 :     | su(T.FSQRT(fty, t)) = suUnary(fty, I.FSQRT, t)
1059 :     | su(T.FADD(fty, t1, t2)) = suComBinary(fty,FADD,FIADD,t1,t2)
1060 :     | su(T.FMUL(fty, t1, t2)) = suComBinary(fty,FMUL,FIMUL,t1,t2)
1061 :     | su(T.FSUB(fty, t1, t2)) = suBinary(fty,FSUB,FISUB,t1,t2)
1062 :     | su(T.FDIV(fty, t1, t2)) = suBinary(fty,FDIV,FIDIV,t1,t2)
1063 :     | su _ = error "su"
1064 :    
1065 :     (* Try to fold the the memory operand or integer conversion *)
1066 :     and suFold(e as T.FREG _) = (LEAF(0, e, []), false)
1067 :     | suFold(e as T.FLOAD _) = (LEAF(0, e, []), false)
1068 :     | suFold(e as T.CVTI2F(_,(16 | 32),_)) = (LEAF(0, e, []), true)
1069 :     | suFold(T.CVTF2F(_, _, t)) = suFold t
1070 :     | suFold(T.FMARK(t, a)) =
1071 :     let val (t, integer) = suFold t
1072 :     in (annotate(t, a), integer) end
1073 :     | suFold e = (su e, false)
1074 :    
1075 :     (* Can the tree be folded into the src operand? *)
1076 :     and foldable(T.FREG _) = true
1077 :     | foldable(T.FLOAD _) = true
1078 :     | foldable(T.CVTI2F(_, (16 | 32), _)) = true
1079 :     | foldable(T.CVTF2F(_, _, t)) = foldable t
1080 :     | foldable(T.FMARK(t, _)) = foldable t
1081 :     | foldable _ = false
1082 :    
1083 :     (* Form unary tree *)
1084 :     and suUnary(fty, funary, t) =
1085 :     let val t = su t
1086 :     in UNARY(label t, fty, funary, t, [])
1087 : george 545 end
1088 : leunga 565
1089 :     (* Form binary tree *)
1090 :     and suBinary(fty, binop, ibinop, t1, t2) =
1091 :     let val t1 = su t1
1092 :     val (t2, integer) = suFold t2
1093 :     val n1 = label t1
1094 :     val n2 = label t2
1095 :     val n = if n1=n2 then n1+1 else Int.max(n1,n2)
1096 :     val myOp = if integer then ibinop else binop
1097 :     in BINARY(n, fty, myOp, t1, t2, [])
1098 : george 545 end
1099 : george 555
1100 : leunga 565 (* Try to fold in the operand if possible.
1101 :     * This only applies to commutative operations.
1102 :     *)
1103 :     and suComBinary(fty, binop, ibinop, t1, t2) =
1104 :     let val (t1, t2) = if foldable t2 then (t1, t2) else (t2, t1)
1105 :     in suBinary(fty, binop, ibinop, t1, t2) end
1106 :    
1107 :     and sameTree(LEAF(_, T.FREG(t1,f1), []),
1108 :     LEAF(_, T.FREG(t2,f2), [])) = t1=t2 andalso f1=f2
1109 :     | sameTree _ = false
1110 :    
1111 :     (* Traverse tree and generate code *)
1112 :     fun gencode(LEAF(_, t, an)) = mark(fxld(leafEA t), an)
1113 :     | gencode(BINARY(_, _, binop, x, t2 as LEAF(0, y, a1), a2)) =
1114 :     let val _ = gencode x
1115 :     val (_, fty, src) = leafEA y
1116 :     fun gen(code) = mark(code, a1 @ a2)
1117 :     fun binary(oper32, oper64) =
1118 :     if sameTree(x, t2) then
1119 :     gen(I.FBINARY{binOp=oper64, src=ST, dst=ST})
1120 : george 555 else
1121 :     let val oper =
1122 : leunga 565 if isMemOpnd src then
1123 :     case fty of
1124 :     32 => oper32
1125 :     | 64 => oper64
1126 :     | _ => error "gencode: BINARY"
1127 :     else oper64
1128 :     in gen(I.FBINARY{binOp=oper, src=src, dst=ST}) end
1129 :     fun ibinary(oper16, oper32) =
1130 :     let val oper = case fty of
1131 :     16 => oper16
1132 :     | 32 => oper32
1133 :     | _ => error "gencode: IBINARY"
1134 :     in gen(I.FIBINARY{binOp=oper, src=src}) end
1135 :     in case binop of
1136 :     FADD => binary(I.FADDS, I.FADDL)
1137 :     | FSUB => binary(I.FDIVS, I.FSUBL)
1138 :     | FMUL => binary(I.FMULS, I.FMULL)
1139 :     | FDIV => binary(I.FDIVS, I.FDIVL)
1140 :     | FIADD => ibinary(I.FIADDS, I.FIADDL)
1141 :     | FISUB => ibinary(I.FIDIVS, I.FISUBL)
1142 :     | FIMUL => ibinary(I.FIMULS, I.FIMULL)
1143 :     | FIDIV => ibinary(I.FIDIVS, I.FIDIVL)
1144 :     end
1145 :     | gencode(BINARY(_, fty, binop, t1, t2, an)) =
1146 :     let fun doit(t1, t2, oper, operP, operRP) =
1147 :     let (* oper[P] => ST(1) := ST oper ST(1); [pop]
1148 :     * operR[P] => ST(1) := ST(1) oper ST; [pop]
1149 :     *)
1150 :     val n1 = label t1
1151 :     val n2 = label t2
1152 :     in if n1 < n2 andalso n1 <= 7 then
1153 :     (gencode t2;
1154 :     gencode t1;
1155 :     mark(I.FBINARY{binOp=operP, src=ST, dst=ST1}, an))
1156 :     else if n2 <= n1 andalso n2 <= 7 then
1157 :     (gencode t1;
1158 :     gencode t2;
1159 :     mark(I.FBINARY{binOp=operRP, src=ST, dst=ST1}, an))
1160 :     else
1161 :     let (* both labels > 7 *)
1162 :     val fs = I.FDirect(newFreg())
1163 :     in gencode t2;
1164 :     emit(fstp(fty, fs));
1165 :     gencode t1;
1166 :     mark(I.FBINARY{binOp=oper, src=fs, dst=ST}, an)
1167 :     end
1168 :     end
1169 :     in case binop of
1170 :     FADD => doit(t1,t2,I.FADDL,I.FADDP,I.FADDP)
1171 :     | FMUL => doit(t1,t2,I.FMULL,I.FMULP,I.FMULP)
1172 :     | FSUB => doit(t1,t2,I.FSUBL,I.FSUBP,I.FSUBRP)
1173 :     | FDIV => doit(t1,t2,I.FDIVL,I.FDIVP,I.FDIVRP)
1174 : george 545 | _ => error "gencode.BINARY"
1175 :     end
1176 : leunga 565 | gencode(UNARY(_, _, unaryOp, su, an)) =
1177 :     (gencode(su); mark(I.FUNARY(unaryOp),an))
1178 :    
1179 :     (* Generate code for a leaf.
1180 :     * Returns the type and an effective address
1181 :     *)
1182 :     and leafEA(T.FREG(fty, f)) = (REAL, fty, I.FDirect f)
1183 :     | leafEA(T.FLOAD(fty, ea, mem)) = (REAL, fty, address(ea, mem))
1184 : leunga 593 | leafEA(T.CVTI2F(_, 32, t)) = int2real(32, t)
1185 :     | leafEA(T.CVTI2F(_, 16, t)) = int2real(16, t)
1186 :     | leafEA(T.CVTI2F(_, 8, t)) = int2real(8, t)
1187 : leunga 565 | leafEA _ = error "leafEA"
1188 :    
1189 :     (* Move integer t of size ty into a memory location *)
1190 : leunga 593 and int2real(ty, t) =
1191 : leunga 565 let val opnd = operand t
1192 :     in if isMemOpnd opnd andalso (ty = 16 orelse ty = 32)
1193 :     then (INTEGER, ty, opnd)
1194 : leunga 593 else
1195 :     let val {instrs, tempMem, cleanup} =
1196 :     cvti2f{ty=ty, src=opnd}
1197 :     in app emit instrs;
1198 :     cleanupCode := !cleanupCode @ cleanup;
1199 :     (INTEGER, 32, tempMem)
1200 :     end
1201 : george 545 end
1202 : leunga 593 in gencode(su fexp);
1203 :     app emit(!cleanupCode)
1204 : george 545 end (*reduceFexp*)
1205 :    
1206 :     (* generate code for a statement *)
1207 :     and stmt(T.MV(_, rd, e), an) = doExpr(e, rd, an)
1208 :     | stmt(T.FMV(fty, fd, e), an) = doFexpr(fty, e, fd, an)
1209 :     | stmt(T.CCMV(ccd, e), an) = doCCexpr(e, ccd, an)
1210 :     | stmt(T.COPY(_, dst, src), an) = copy(dst, src, an)
1211 :     | stmt(T.FCOPY(fty, dst, src), an) = fcopy(fty, dst, src, an)
1212 :     | stmt(T.JMP(ctrl, e, labs), an) = jmp(e, labs, an)
1213 : leunga 591 | stmt(T.CALL{funct, targets, defs, uses, cdefs, cuses, region}, an) =
1214 :     call(funct,targets,defs,uses,region,an)
1215 : george 545 | stmt(T.RET _, an) = mark(I.RET NONE, an)
1216 :     | stmt(T.STORE(8, ea, d, mem), an) = store8(ea, d, mem, an)
1217 :     | stmt(T.STORE(16, ea, d, mem), an) = store16(ea, d, mem, an)
1218 :     | stmt(T.STORE(32, ea, d, mem), an) = store32(ea, d, mem, an)
1219 :     | stmt(T.FSTORE(fty, ea, d, mem), an) = fstore(fty, ea, d, mem, an)
1220 :     | stmt(T.BCC(ctrl, cc, lab), an) = branch(cc, lab, an)
1221 :     | stmt(T.DEFINE l, _) = defineLabel l
1222 :     | stmt(T.ANNOTATION(s, a), an) = stmt(s, a::an)
1223 : george 555 | stmt(T.EXT s, an) =
1224 :     ExtensionComp.compileSext (reducer()) {stm=s, an=an}
1225 : george 545 | stmt(s, _) = doStmts(Gen.compileStm s)
1226 :    
1227 :     and doStmt s = stmt(s, [])
1228 :     and doStmts ss = app doStmt ss
1229 :    
1230 :     and beginCluster' _ =
1231 :     ((* Must be cleared by the client.
1232 :     * if rewriteMemReg then memRegsUsed := 0w0 else ();
1233 :     *)
1234 :     trapLabel := NONE; beginCluster 0)
1235 :     and endCluster' a =
1236 : monnier 247 (case !trapLabel
1237 : monnier 411 of NONE => ()
1238 : george 545 | SOME(_, lab) => (defineLabel lab; emit(I.INTO))
1239 : monnier 411 (*esac*);
1240 : george 545 endCluster(a)
1241 :     )
1242 :    
1243 :     and reducer() =
1244 :     T.REDUCER{reduceRexp = expr,
1245 :     reduceFexp = fexpr,
1246 :     reduceCCexp = ccExpr,
1247 :     reduceStm = stmt,
1248 :     operand = operand,
1249 :     reduceOperand = reduceOpnd,
1250 :     addressOf = fn e => address(e, I.Region.memory), (*XXX*)
1251 :     emit = mark,
1252 :     instrStream = instrStream,
1253 :     mltreeStream = self()
1254 :     }
1255 :    
1256 :     and self() =
1257 :     S.STREAM
1258 :     { beginCluster= beginCluster',
1259 :     endCluster = endCluster',
1260 :     emit = doStmt,
1261 :     pseudoOp = pseudoOp,
1262 :     defineLabel = defineLabel,
1263 :     entryLabel = entryLabel,
1264 :     comment = comment,
1265 :     annotation = annotation,
1266 :     exitBlock = fn mlrisc => exitBlock(cellset mlrisc),
1267 :     alias = alias,
1268 :     phi = phi
1269 :     }
1270 :    
1271 :     in self()
1272 : monnier 247 end
1273 :    
1274 : george 545 end (* functor *)
1275 :    
1276 :     end (* local *)

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