Debian Codegraph @

lib/mlrisc-lib/MLRISC/x86/x86MC.sml

raw
1 (*
2 *
3 * COPYRIGHT (c) 1996 Bell Laboratories.
4 *
5 * IMPORTANT NOTE:
6 * Integer registers are numbered from 0 - 31 (0-7 are physical)
7 * Floating point registers are numbered from 32-63 (32-39 are physical)
8 *)
9 functor X86MCEmitter
10 (structure Instr : X86INSTR
11 structure Shuffle : X86SHUFFLE (* where I = Instr *)
12 where type I.Constant.const = Instr.Constant.const
13 and type I.Region.region = Instr.Region.region
14 and type I.T.Basis.cond = Instr.T.Basis.cond
15 and type I.T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
16 and type I.T.Basis.ext = Instr.T.Basis.ext
17 and type I.T.Basis.fcond = Instr.T.Basis.fcond
18 and type I.T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
19 and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
20 and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
21 and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
22 and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
23 and type I.T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
24 and type I.T.ccexp = Instr.T.ccexp
25 and type I.T.fexp = Instr.T.fexp
26 (* and type I.T.labexp = Instr.T.labexp *)
27 and type I.T.mlrisc = Instr.T.mlrisc
28 and type I.T.oper = Instr.T.oper
29 and type I.T.rep = Instr.T.rep
30 and type I.T.rexp = Instr.T.rexp
31 and type I.T.stm = Instr.T.stm
32 (* and type I.addressing_mode = Instr.addressing_mode *)
33 and type I.binaryOp = Instr.binaryOp
34 and type I.bitOp = Instr.bitOp
35 and type I.cond = Instr.cond
36 and type I.fbinOp = Instr.fbinOp
37 and type I.fenvOp = Instr.fenvOp
38 and type I.fibinOp = Instr.fibinOp
39 and type I.fsize = Instr.fsize
40 and type I.funOp = Instr.funOp
41 and type I.instr = Instr.instr
42 and type I.instruction = Instr.instruction
43 and type I.isize = Instr.isize
44 and type I.move = Instr.move
45 and type I.multDivOp = Instr.multDivOp
46 and type I.operand = Instr.operand
47 and type I.shiftOp = Instr.shiftOp
48 and type I.unaryOp = Instr.unaryOp
49 structure MLTreeEval : MLTREE_EVAL (* where T = Instr.T *)
50 where type T.Basis.cond = Instr.T.Basis.cond
51 and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
52 and type T.Basis.ext = Instr.T.Basis.ext
53 and type T.Basis.fcond = Instr.T.Basis.fcond
54 and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
55 and type T.Constant.const = Instr.T.Constant.const
56 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
57 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
58 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
59 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
60 and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
61 and type T.Region.region = Instr.T.Region.region
62 and type T.ccexp = Instr.T.ccexp
63 and type T.fexp = Instr.T.fexp
64 (* and type T.labexp = Instr.T.labexp *)
65 and type T.mlrisc = Instr.T.mlrisc
66 and type T.oper = Instr.T.oper
67 and type T.rep = Instr.T.rep
68 and type T.rexp = Instr.T.rexp
69 and type T.stm = Instr.T.stm
70 structure MemRegs : MEMORY_REGISTERS (* where I = Instr *)
71 where type I.Constant.const = Instr.Constant.const
72 and type I.Region.region = Instr.Region.region
73 and type I.T.Basis.cond = Instr.T.Basis.cond
74 and type I.T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
75 and type I.T.Basis.ext = Instr.T.Basis.ext
76 and type I.T.Basis.fcond = Instr.T.Basis.fcond
77 and type I.T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
78 and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
79 and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
80 and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
81 and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
82 and type I.T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
83 and type I.T.ccexp = Instr.T.ccexp
84 and type I.T.fexp = Instr.T.fexp
85 (* and type I.T.labexp = Instr.T.labexp *)
86 and type I.T.mlrisc = Instr.T.mlrisc
87 and type I.T.oper = Instr.T.oper
88 and type I.T.rep = Instr.T.rep
89 and type I.T.rexp = Instr.T.rexp
90 and type I.T.stm = Instr.T.stm
91 (* and type I.addressing_mode = Instr.addressing_mode *)
92 and type I.binaryOp = Instr.binaryOp
93 and type I.bitOp = Instr.bitOp
94 and type I.cond = Instr.cond
95 and type I.fbinOp = Instr.fbinOp
96 and type I.fenvOp = Instr.fenvOp
97 and type I.fibinOp = Instr.fibinOp
98 and type I.fsize = Instr.fsize
99 and type I.funOp = Instr.funOp
100 and type I.instr = Instr.instr
101 and type I.instruction = Instr.instruction
102 and type I.isize = Instr.isize
103 and type I.move = Instr.move
104 and type I.multDivOp = Instr.multDivOp
105 and type I.operand = Instr.operand
106 and type I.shiftOp = Instr.shiftOp
107 and type I.unaryOp = Instr.unaryOp
108 val memRegBase : CellsBasis.cell option
109 structure AsmEmitter : INSTRUCTION_EMITTER (* where I = Instr *)
110 where type I.addressing_mode = Instr.addressing_mode
111 and type I.ea = Instr.ea
112 and type I.instr = Instr.instr
113 and type I.instruction = Instr.instruction
114 and type I.operand = Instr.operand
115 ) : MC_EMIT =
116 struct
117 structure I = Instr
118 structure C = I.C
119 structure Const = I.Constant
120 structure W32 = Word32
121 structure W8 = Word8
122 structure W = LargeWord
123 structure CB = CellsBasis
124 structure LE = MLTreeEval
125
126 val itow = Word.fromInt
127 val wtoi = Word.toInt
128
129 fun error msg = MLRiscErrorMsg.impossible ("X86MCEmitter." ^ msg)
130
131 (*
132 * Sanity check!
133 *)
134
135 val eax = 0 val esp = 4
136 val ecx = 1 val ebp = 5
137 val edx = 2 val esi = 6
138 val ebx = 3 val edi = 7
139
140 val opnd16Prefix = 0x66
141
142 fun const c = Int32.fromInt(Const.valueOf c)
143 fun lexp le = Int32.fromInt(LE.valueOf le)
144
145 val toWord8 = Word8.fromLargeWord o LargeWord.fromLargeInt o Int32.toLarge
146 val eBytes = Word8Vector.fromList
147 fun eByte i = eBytes [W8.fromInt i]
148 local
149 val toLWord = (W.fromLargeInt o Int32.toLarge)
150 fun shift (w,cnt) = W8.fromLargeWord(W.>>(w, cnt))
151 in
152 fun eShort i16 = let
153 val w = toLWord i16
154 in [shift(w, 0w0), shift(w,0w8)]
155 end
156 fun eLong i32 = let
157 val w = toLWord i32
158 in [shift(w, 0w0), shift(w,0w8), shift(w,0w16), shift(w,0w24)]
159 end
160 end
161
162 fun emitInstrs(instrs) = Word8Vector.concat(map emitInstr instrs)
163
164 and emitX86Instr(instr: I.instr) = let
165 val error =
166 fn msg =>
167 let val AsmEmitter.S.STREAM{emit,...} = AsmEmitter.makeStream []
168 in emit (I.INSTR instr); error msg end
169
170 val rNum = CB.physicalRegisterNum
171 val fNum = CB.physicalRegisterNum
172
173 fun memReg r = MemRegs.memReg{reg=r, base=Option.valOf memRegBase}
174
175 datatype size = Zero | Bits8 | Bits32
176 fun size i =
177 if i = 0 then Zero
178 else if Int32.<(i, 128) andalso Int32.<=(~128, i) then Bits8
179 else Bits32
180
181 fun immedOpnd(I.Immed(i32)) = i32
182 | immedOpnd(I.ImmedLabel le) = lexp le
183 | immedOpnd(I.LabelEA le) = lexp le
184 | immedOpnd _ = error "immedOpnd"
185
186 nonfix mod
187
188 fun scale(n, m) = Word.toIntX(Word.<<(Word.fromInt n, Word.fromInt m))
189 fun modrm{mod, reg, rm} = W8.fromInt(scale(mod,6) + scale(reg,3) + rm)
190 fun sib{ss, index, base} = W8.fromInt(scale(ss,6) + scale(index,3) + base)
191 fun reg{opc, reg} = W8.fromInt(scale(opc,3) + reg)
192
193 fun eImmedExt(opc, I.Direct r) = [modrm{mod=3, reg=opc, rm=rNum r}]
194 | eImmedExt(opc, opn as I.MemReg _) = eImmedExt(opc, memReg opn)
195 | eImmedExt(opc, I.Displace{base, disp, ...}) = let
196 val base = rNum base (* XXX rNum may be done twice *)
197 val immed = immedOpnd disp
198 fun displace(mod, eDisp) =
199 if base=esp then
200 modrm{mod=mod, reg=opc, rm=4}::
201 sib{ss=0, index=4, base=esp}::eDisp immed
202 else
203 modrm{mod=mod, reg=opc, rm=base} :: eDisp immed
204 in
205 case size immed
206 of Zero =>
207 if base=esp then
208 [modrm{mod=0, reg=opc, rm=4}, sib{ss=0,index=4,base=esp}]
209 else if base=ebp then
210 [modrm{mod=1, reg=opc, rm=ebp}, 0w0]
211 else
212 [modrm{mod=0, reg=opc, rm=base}]
213 | Bits8 => displace(1, fn i => [toWord8 i])
214 | Bits32 => displace(2, eLong)
215 (*esac*)
216 end
217 | eImmedExt(opc, I.Indexed{base=NONE, index, scale, disp, ...}) =
218 (modrm{mod=0, reg=opc, rm=4} ::
219 sib{base=5, ss=scale, index=rNum index} ::
220 eLong(immedOpnd disp))
221 | eImmedExt(opc, I.Indexed{base=SOME b, index, scale, disp, ...}) = let
222 val index = rNum index
223 val base = rNum b
224 val immed = immedOpnd disp
225 fun indexed(mod, eDisp) =
226 modrm{mod=mod, reg=opc, rm=4} ::
227 sib{ss=scale, index=index, base=base} :: eDisp immed
228 in
229 case size immed
230 of Zero =>
231 if base=ebp then
232 [modrm{mod=1, reg=opc, rm=4},
233 sib{ss=scale, index=index, base=5}, 0w0]
234 else
235 [modrm{mod=0, reg=opc, rm=4},
236 sib{ss=scale, index=index, base=base}]
237 | Bits8 => indexed(1, fn i => [toWord8 i])
238 | Bits32 => indexed(2, eLong)
239 (*esac*)
240 end
241 | eImmedExt(opc, opnd as I.FDirect f) = eImmedExt(opc, memReg opnd)
242 | eImmedExt(_, I.Immed _) = error "eImmedExt: Immed"
243 | eImmedExt(_, I.ImmedLabel _) = error "eImmedExt: ImmedLabel"
244 | eImmedExt(_, I.Relative _) = error "eImmedExt: Relative"
245 | eImmedExt(_, I.LabelEA _) = error "eImmedExt: LabelEA"
246 | eImmedExt(_, I.FPR _) = error "eImmedExt: FPR"
247 | eImmedExt(_, I.ST _) = error "eImmedExt: ST"
248
249 (* Short hands for various encodings *)
250 fun encode(byte1, opc, opnd) = eBytes(byte1 :: eImmedExt(opc, opnd))
251 fun encodeST(byte1, opc, STn) = eBytes[byte1, reg{opc=opc,reg=fNum STn}]
252 fun encode2(byte1, byte2, opc, opnd) =
253 eBytes(byte1 :: byte2 :: eImmedExt(opc, opnd))
254 fun encodeReg(byte1, reg, opnd) = encode(byte1, rNum reg, opnd)
255 fun encodeLongImm(byte1, opc, opnd, i) =
256 eBytes(byte1 :: (eImmedExt(opc, opnd) @ eLong i))
257 fun encodeShortImm(byte1, opc, opnd, w) =
258 eBytes(byte1 :: (eImmedExt(opc, opnd) @ eShort w))
259 fun encodeByteImm(byte1, opc, opnd, b) =
260 eBytes(byte1 :: (eImmedExt(opc, opnd) @ [toWord8 b]))
261
262 fun condCode cond =
263 (case cond
264 of I.EQ => 0w4 | I.NE => 0w5
265 | I.LT => 0w12 | I.LE => 0w14
266 | I.GT => 0w15 | I.GE => 0w13
267 | I.A => 0w7 | I.AE => 0w3
268 | I.B => 0w2 | I.BE => 0w6
269 | I.C => 0w2 | I.NC => 0w3
270 | I.P => 0wxa | I.NP => 0wxb
271 | I.O => 0w0 | I.NO => 0w1
272 (*esac*))
273
274 (* arith: only 5 cases need be considered:
275 * dst, src
276 * -----------
277 * EAX, imm32
278 * r/m32, imm32
279 * r/m32, imm8
280 * r/m32, r32
281 * r32, r/m32
282 *)
283 fun arith(opc1, opc2) = let
284 fun f(I.ImmedLabel le, dst) = f(I.Immed(lexp le), dst)
285 | f(I.LabelEA le, dst) = f(I.Immed(lexp le), dst)
286 | f(I.Immed(i), dst) =
287 (case size i
288 of Bits32 =>
289 (case dst
290 of I.Direct r =>
291 if CB.physicalRegisterNum r = eax then
292 eBytes(W8.fromInt(8*opc2 + 5) :: eLong(i))
293 else
294 encodeLongImm(0wx81, opc2, dst, i)
295 | _ => encodeLongImm(0wx81, opc2, dst, i)
296 (*esac*))
297 | _ => encodeByteImm(0wx83, opc2, dst, i) (* 83 /digit ib *)
298 (*esac*))
299 | f(src, I.Direct r) = encodeReg(opc1+0w3, r, src)
300 | f(I.Direct r, dst) = encodeReg(opc1+0w1, r, dst)
301 | f _ = error "arith.f"
302 in f
303 end
304
305 (* test: the following cases need be considered:
306 * lsrc, rsrc
307 * -----------
308 * AL, imm8 opc1 A8
309 * EAX, imm32 opc1 A9
310 * r/m8, imm8 opc2 F6/0 ib
311 * r/m32, imm32 opc2 F7/0 id
312 * r/m8, r8 opc3 84/r
313 * r/m32, r32 opc3 85/r
314 *)
315 fun test(bits, I.ImmedLabel le, lsrc) = test(bits, I.Immed(lexp le), lsrc)
316 | test(bits, I.LabelEA le, lsrc) = test(bits, I.Immed(lexp le), lsrc)
317 | test(bits, I.Immed(i), lsrc) =
318 (case (lsrc, i >= 0 andalso i < 255) of
319 (I.Direct r, false) =>
320 if CB.physicalRegisterNum r = eax then eBytes(0wxA9 :: eLong i)
321 else encodeLongImm(0wxF7, 0, lsrc, i)
322 | (_, false) => encodeLongImm(0wxF7, 0, lsrc, i)
323 | (I.Direct r, true) => (* 8 bit *)
324 let val r = CB.physicalRegisterNum r
325 in if r = eax then eBytes[0wxA8, toWord8 i]
326 else if r < 4 then
327 (* unfortunately, only CL, DL, BL can be encoded *)
328 encodeByteImm(0wxF6, 0, lsrc, i)
329 else if bits = 8 then error "test.8"
330 else encodeLongImm(0wxF7, 0, lsrc, i)
331 end
332 | (_, true) => encodeByteImm(0wxF6, 0, lsrc, i)
333 )
334 | test(8, rsrc as I.Direct r, lsrc) =
335 if rNum r < 4 then encodeReg(0wx84, r, lsrc)
336 else error "test.8"
337 | test(32, I.Direct r, lsrc) = encodeReg(0wx85, r, lsrc)
338 | test _ = error "test"
339
340 in
341 case instr
342 of I.NOP => eByte 0x90
343 | I.JMP(I.Relative i, _) => ((let
344 fun shortJmp() = eBytes[0wxeb, Word8.fromInt(i-2)]
345 in
346 case size(Int32.fromInt (i-2))
347 of Bits32 => eBytes(0wxe9 :: eLong(Int32.fromInt(i-5)))
348 | _ => shortJmp()
349 (*esac*)
350 end
351 ) handle e => (print "JMP\n"; raise e))
352 | I.JMP(opnd, _) => encode(0wxff, 4, opnd)
353 | I.JCC{cond, opnd=I.Relative i} =>
354 let val code = condCode cond
355 in case size (Int32.fromInt(i-2))
356 of Bits32 =>
357 eBytes(0wx0f :: Word8.+(0wx80,code) :: eLong(Int32.fromInt(i-6)))
358 | _ =>
359 eBytes[Word8.+(0wx70,code), Word8.fromInt(i-2)]
360 end
361 | I.CALL{opnd=I.Relative i,...} => eBytes(0wxe8::eLong(Int32.fromInt(i-5)))
362 | I.CALL{opnd, ...} => encode(0wxff, 2, opnd)
363 | I.RET NONE => eByte 0xc3
364 (* integer *)
365 | I.MOVE{mvOp=I.MOVL, src, dst} =>
366 let fun mv(I.Immed(i), I.Direct r) =
367 eBytes(Word8.+(0wxb8, Word8.fromInt(rNum r))::eLong(i))
368 | mv(I.Immed(i), _) = encodeLongImm(0wxc7, 0, dst, i)
369 | mv(I.ImmedLabel le,dst) = mv(I.Immed(lexp le),dst)
370 | mv(I.LabelEA le,dst) = error "MOVL: LabelEA"
371 | mv(src as I.MemReg _, dst) = mv(memReg src, dst)
372 | mv(src, dst as I.MemReg _) = mv(src, memReg dst)
373 | mv(src,dst) = arith(0wx88, 0) (src, dst)
374 in mv(src,dst) end
375 | I.MOVE{mvOp=I.MOVW, src, dst} => let
376 fun immed16 i = Int32.<(i, 32768) andalso Int32.<=(~32768, i)
377 fun prefix v = Word8Vector.concat[eByte(opnd16Prefix), v]
378 fun mv(I.Immed(i), _) =
379 (case dst
380 of I.Direct r =>
381 if immed16 i then
382 prefix(eBytes(W8.+(0wxb8, W8.fromInt(rNum r)):: eShort(i)))
383 else error "MOVW: Immediate too large"
384 | _ => prefix(encodeShortImm(0wxc7, 0, dst, i))
385 (*esac*))
386 | mv(src as I.MemReg _, dst) = mv(memReg src, dst)
387 | mv(src, dst as I.MemReg _) = mv(src, memReg dst)
388 | mv(src, dst) = prefix(arith(0wx88, 0) (src, dst))
389 in mv(src, dst)
390 end
391 | I.MOVE{mvOp=I.MOVB, dst, src=I.Immed(i)} =>
392 encodeByteImm(0wxc6, 0, dst, i)
393 (* 2007/02/20 AKL: just store the low order 8 bits.
394 Forget about checking for the range. This is because of
395 sign-extension issues in sml/nj's code generator.
396 (case size i
397 of Bits32 => error "MOVE: MOVB: imm8"
398 | _ => encodeByteImm(0wxc6, 0, dst, i)
399 (*esac*))
400 *)
401 | I.MOVE{mvOp=I.MOVB, dst, src=I.Direct r} => encodeReg(0wx88, r, dst)
402 | I.MOVE{mvOp=I.MOVB, dst=I.Direct r, src} => encodeReg(0wx8a, r, src)
403 | I.MOVE{mvOp, src=I.Immed _, ...} => error "MOVE: Immed"
404 | I.MOVE{mvOp, src, dst=I.Direct r} =>
405 let val byte2 =
406 case mvOp of
407 I.MOVZBL => 0wxb6
408 | I.MOVZWL => 0wxb7
409 | I.MOVSBL => 0wxbe
410 | I.MOVSWL => 0wxbf
411 | _ => error "MOV[SZ]X"
412 in eBytes(0wx0f :: byte2 :: eImmedExt(rNum r, src)) end
413 | I.MOVE _ => error "MOVE"
414 | I.CMOV{cond,src,dst} =>
415 let val cond = condCode cond
416 in eBytes(0wx0f :: Word8.+(cond,0wx40) :: eImmedExt(rNum dst, src))
417 end
418 | I.LEA{r32, addr} => encodeReg(0wx8d, r32, addr)
419 | I.CMPL{lsrc, rsrc} => arith(0wx38, 7) (rsrc, lsrc)
420 | (I.CMPW _ | I.CMPB _) => error "CMP"
421 | I.TESTL{lsrc, rsrc} => test(32, rsrc, lsrc)
422 | I.TESTB{lsrc, rsrc} => test(8, rsrc, lsrc)
423 | I.TESTW _ => error "TEST"
424 | I.BINARY{binOp, src, dst} => let
425 fun shift(code, src) =
426 (case src
427 of I.Immed (1) => encode(0wxd1, code, dst)
428 | I.Immed (n) => encodeByteImm(0wxc1, code, dst, n)
429 | I.Direct r =>
430 if rNum r <> ecx then error "shift: Direct"
431 else encode(0wxd3, code, dst)
432 | I.MemReg _ => shift(code, memReg src)
433 | _ => error "shift"
434 (*esac*))
435 in
436 case binOp
437 of I.ADDL => arith(0w0, 0) (src, dst)
438 | I.SUBL => arith(0wx28, 5) (src, dst)
439 | I.ANDL => arith(0wx20, 4) (src, dst)
440 | I.ORL => arith(0w8, 1) (src, dst)
441 | I.XORL => arith(0wx30, 6) (src, dst)
442 | I.SHLL => shift(4,src)
443 | I.SARL => shift(7,src)
444 | I.SHRL => shift(5,src)
445 | I.IMULL =>
446 (case (src, dst)
447 of (I.Immed(i), I.Direct dstR) =>
448 (case size i
449 of Bits32 => encodeLongImm(0wx69, rNum dstR, dst, i)
450 | _ => encodeByteImm(0wx6b, rNum dstR, dst, i)
451 )
452 | (_, I.Direct dstR) =>
453 eBytes(0wx0f::0wxaf::(eImmedExt(rNum dstR, src)))
454 | _ => error "imull"
455 )
456 | _ => error "binary"
457 end
458 | I.MULTDIV{multDivOp, src} => let
459 val mulOp =
460 (case multDivOp of
461 I.MULL1 => 4 | I.IDIVL1 => 7 | I.DIVL1 => 6
462 | I.IMULL1 => error "imull1")
463 in encode(0wxf7, mulOp, src)
464 end
465 | I.MUL3{dst, src1, src2=i} =>
466 (case src1
467 of I.Immed _ => error "mul3: Immed"
468 | I.ImmedLabel _ => error "mul3: ImmedLabel"
469 | _ =>
470 (case size i
471 of Bits32 => encodeLongImm(0wx69, rNum dst, src1, i)
472 | _ => encodeByteImm(0wx6b, rNum dst, src1, i)
473 (*esac*))
474 (*esac*))
475 | I.UNARY{unOp, opnd} =>
476 (case unOp
477 of I.DECL =>
478 (case opnd
479 of I.Direct d => eByte(0x48 + rNum d)
480 | _ => encode(0wxff, 1, opnd)
481 (*esac*))
482 | I.INCL =>
483 (case opnd
484 of I.Direct d => eByte(0x40 + rNum d)
485 | _ => encode(0wxff, 0, opnd)
486 (*esac*))
487 | I.NEGL => encode(0wxf7, 3, opnd)
488 | I.NOTL => encode(0wxf7, 2, opnd)
489 | _ => error "UNARY is not in DECL/INCL/NEGL,NOTL"
490 (*esac*))
491 | I.SET{cond,opnd} =>
492 eBytes(0wx0f :: Word8.+(0wx90,condCode cond) :: eImmedExt(0, opnd))
493 | I.PUSHL(I.Immed(i)) =>
494 (case size i
495 of Bits32 => eBytes(0wx68 :: eLong(i))
496 | _ => eBytes[0wx6a, toWord8 i]
497 (*esac*))
498 | I.PUSHL(I.Direct r) => eByte(0x50+rNum r)
499 | I.PUSHL opnd => encode(0wxff, 6, opnd)
500 | I.POP(I.Direct r) => eByte(0x58+rNum r)
501 | I.POP(opnd) => encode(0wx8f, 0, opnd)
502 | I.CDQ => eByte(0x99)
503 | I.INTO => eByte(0xce)
504
505 (* floating *)
506 | I.FBINARY{binOp, src=I.ST src, dst=I.ST dst} =>
507 let val src = W8.fromInt(fNum src)
508 val dst = W8.fromInt(fNum dst)
509 val (opc1, opc2) =
510 case (src, dst) of
511 (_, 0w0) =>
512 (case binOp
513 of I.FADDL => (0wxd8, 0wxc0 + src)
514 | I.FMULL => (0wxd8, 0wxc8 + src)
515 | I.FSUBRL => (0wxd8, 0wxe8 + src)
516 | I.FSUBL => (0wxd8, 0wxe0 + src) (* gas XXX *)
517 | I.FDIVRL => (0wxd8, 0wxf8 + src)
518 | I.FDIVL => (0wxd8, 0wxf0 + src) (* gas XXX *)
519 | _ => error "FBINARY:pop:src=%st(n),dst=%st"
520 )
521 | (0w0, _) =>
522 (case binOp
523 of I.FADDP => (0wxde, 0wxc0 + dst)
524 | I.FMULP => (0wxde, 0wxc8 + dst)
525 | I.FSUBRP => (0wxde, 0wxe8 + dst) (* gas XXX *)
526 | I.FSUBP => (0wxde, 0wxe0 + dst)
527 | I.FDIVRP => (0wxde, 0wxf8 + dst) (* gas XXX *)
528 | I.FDIVP => (0wxde, 0wxf0 + dst)
529
530 | I.FADDL => (0wxdc, 0wxc0 + dst)
531 | I.FMULL => (0wxdc, 0wxc8 + dst)
532 | I.FSUBRL => (0wxdc, 0wxe8 + dst) (* gas XXX *)
533 | I.FSUBL => (0wxdc, 0wxe0 + dst)
534 | I.FDIVRL => (0wxdc, 0wxf8 + dst) (* gas XXX *)
535 | I.FDIVL => (0wxdc, 0wxf0 + dst)
536
537 | _ => error "FBINARY (0w0,_)"
538 )
539 | (_, _) => error "FBINARY (src, dst) non %st(0)"
540 in eBytes[opc1, opc2]
541 end
542 | I.FBINARY{binOp, src, dst=I.ST dst} =>
543 if CB.physicalRegisterNum dst = 0 then
544 let
545 val (opc, code) =
546 (case binOp of
547 I.FADDL => (0wxdc, 0)
548 | I.FMULL => (0wxdc, 1)
549 | I.FCOML => (0wxdc, 2)
550 | I.FCOMPL => (0wxdc, 3)
551 | I.FSUBL => (0wxdc, 4)
552 | I.FSUBRL => (0wxdc, 5)
553 | I.FDIVL => (0wxdc, 6)
554 | I.FDIVRL => (0wxdc, 7)
555 | I.FADDS => (0wxd8, 0)
556 | I.FMULS => (0wxd8, 1)
557 | I.FCOMS => (0wxd8, 2)
558 | I.FCOMPS => (0wxd8, 3)
559 | I.FSUBS => (0wxd8, 4)
560 | I.FSUBRS => (0wxd8, 5)
561 | I.FDIVS => (0wxd8, 6)
562 | I.FDIVRS => (0wxd8, 7)
563 | _ => error "FBINARY:pop:dst=%st"
564 (*esac*))
565 in encode(opc, code, src)
566 end
567 else error "FBINARY"
568 | I.FIBINARY{binOp, src} =>
569 let val (opc, code) =
570 case binOp of
571 I.FIADDL => (0wxda, 0)
572 | I.FIMULL => (0wxda, 1)
573 | I.FICOML => (0wxda, 2)
574 | I.FICOMPL => (0wxda, 3)
575 | I.FISUBL => (0wxda, 4)
576 | I.FISUBRL => (0wxda, 5)
577 | I.FIDIVL => (0wxda, 6)
578 | I.FIDIVRL => (0wxda, 7)
579 | I.FIADDS => (0wxde, 0)
580 | I.FIMULS => (0wxde, 1)
581 | I.FICOMS => (0wxde, 2)
582 | I.FICOMPS => (0wxde, 3)
583 | I.FISUBS => (0wxde, 4)
584 | I.FISUBRS => (0wxde, 5)
585 | I.FIDIVS => (0wxde, 6)
586 | I.FIDIVRS => (0wxde, 7)
587 in encode(opc, code, src) end
588 | I.FUNARY unOp =>
589 eBytes[0wxd9,
590 case unOp
591 of I.FABS => 0wxe1
592 | I.FCHS => 0wxe0
593 | I.FSQRT => 0wxfa
594 | I.FSIN => 0wxfe
595 | I.FCOS => 0wxff
596 | I.FPTAN => 0wxf2
597 | I.FPATAN => 0wxf3
598 | I.FDECSTP => 0wxf6
599 | I.FINCSTP => 0wxf7
600 | _ => error "FUNARY"
601 ]
602 | I.FXCH{opnd} => encodeST(0wxd9, 25, opnd)
603
604 | I.FUCOM(I.ST n) => encodeST(0wxdd, 28, n)
605 | I.FUCOMP(I.ST n) => encodeST(0wxdd, 29, n)
606 | I.FUCOMPP => eBytes[0wxda, 0wxe9]
607 | I.FCOMI(I.ST n) => encodeST(0wxdb, 0x1e, n)
608 | I.FCOMIP(I.ST n) => encodeST(0wxdf, 0x1e, n)
609 | I.FUCOMI(I.ST n) => encodeST(0wxdb, 0x1d, n)
610 | I.FUCOMIP(I.ST n) => encodeST(0wxdf, 0x1d, n)
611
612 | I.FSTS opnd => encode(0wxd9, 2, opnd)
613 | I.FSTL(I.ST n) => encodeST(0wxdd, 26, n)
614 | I.FSTL opnd => encode(0wxdd, 2, opnd)
615
616 | I.FSTPS opnd => encode(0wxd9, 3, opnd)
617 | I.FSTPL(I.ST n) => encodeST(0wxdd, 27, n)
618 | I.FSTPL opnd => encode(0wxdd, 3, opnd)
619 | I.FSTPT opnd => encode(0wxdb, 7, opnd)
620
621 | I.FLD1 => eBytes[0wxd9,0wxe8]
622 | I.FLDL2T => eBytes[0wxd9,0wxe9]
623 | I.FLDL2E => eBytes[0wxd9,0wxea]
624 | I.FLDPI => eBytes[0wxd9,0wxeb]
625 | I.FLDLG2 => eBytes[0wxd9,0wxec]
626 | I.FLDLN2 => eBytes[0wxd9,0wxed]
627 | I.FLDZ => eBytes[0wxd9,0wxee]
628 | I.FLDS opnd => encode(0wxd9, 0, opnd)
629
630 | I.FLDL(I.ST n) => encodeST(0wxd9, 24, n)
631 | I.FLDL opnd => encode(0wxdd, 0, opnd)
632
633 | I.FILD opnd => encode(0wxdf, 0, opnd)
634 | I.FILDL opnd => encode(0wxdb, 0, opnd)
635 | I.FILDLL opnd => encode(0wxdf, 5, opnd)
636
637 | I.FNSTSW => eBytes[0wxdf, 0wxe0]
638
639 (* misc *)
640 | I.SAHF => eByte(0x9e)
641 | _ => error "emitInstr"
642 end
643 and emitInstr (I.LIVE _) = Word8Vector.fromList []
644 | emitInstr (I.KILL _) = Word8Vector.fromList []
645 | emitInstr(I.COPY{k, dst, src, tmp, ...}) =
646 (case k
647 of CB.GP => emitInstrs(Shuffle.shuffle {tmp=tmp, dst=dst, src=src})
648 | CB.FP => emitInstrs(Shuffle.shufflefp {tmp=tmp, dst=dst, src=src})
649 | _ => error "COPY"
650 (*esac*))
651 | emitInstr (I.INSTR instr) = emitX86Instr instr
652 | emitInstr (I.ANNOTATION{i,...}) = emitInstr i
653
654 end