Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/amd64MC.sml

raw
1 (* amd64MC.sml
2 *
3 * COPYRIGHT (c) 2019 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *
6 * Machine code emitter for AMD64 architecture.
7 *)
8
9 functor AMD64MCEmitter (
10
11 structure Instr : AMD64INSTR
12 structure Shuffle : AMD64SHUFFLE (* where I = Instr *)
13 where type I.Constant.const = Instr.Constant.const
14 and type I.Region.region = Instr.Region.region
15 and type I.T.Basis.cond = Instr.T.Basis.cond
16 and type I.T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
17 and type I.T.Basis.ext = Instr.T.Basis.ext
18 and type I.T.Basis.fcond = Instr.T.Basis.fcond
19 and type I.T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
20 and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
21 and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
22 and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
23 and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
24 and type I.T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
25 and type I.T.ccexp = Instr.T.ccexp
26 and type I.T.fexp = Instr.T.fexp
27 (* and type I.T.labexp = Instr.T.labexp *)
28 and type I.T.mlrisc = Instr.T.mlrisc
29 and type I.T.oper = Instr.T.oper
30 and type I.T.rep = Instr.T.rep
31 and type I.T.rexp = Instr.T.rexp
32 and type I.T.stm = Instr.T.stm
33 (* and type I.addressing_mode = Instr.addressing_mode *)
34 and type I.binaryOp = Instr.binaryOp
35 and type I.bitOp = Instr.bitOp
36 and type I.cond = Instr.cond
37 and type I.fbin_op = Instr.fbin_op
38 and type I.fcom_op = Instr.fcom_op
39 and type I.fmove_op = Instr.fmove_op
40 and type I.fsize = Instr.fsize
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 AsmEmitter : INSTRUCTION_EMITTER (* where I = Instr *)
71 where type I.addressing_mode = Instr.addressing_mode
72 and type I.ea = Instr.ea
73 and type I.instr = Instr.instr
74 and type I.instruction = Instr.instruction
75 and type I.operand = Instr.operand
76
77 ) : MC_EMIT = struct
78 structure I = Instr
79 structure C = I.C
80 structure W32 = Word32
81 structure W8 = Word8
82 structure W = LargeWord
83 structure CB = CellsBasis
84
85 val println = print o (fn s => s^"\n")
86 val i2s = Int.toString
87 val print_int = println o i2s
88
89 val itow = Word.fromInt
90 val wtoi = Word.toInt
91
92 fun error msg = MLRiscErrorMsg.impossible ("AMD64MCEmitter: " ^ msg)
93 fun unimplemented instr = MLRiscErrorMsg.impossible (concat[
94 "AMD64MCEmitter: ", instr, " unimplemented"
95 ])
96
97 (*
98 * Sanity check!
99 *)
100 val eax = 0 val esp = 4
101 val ecx = 1 val ebp = 5
102 val edx = 2 val esi = 6
103 val ebx = 3 val edi = 7
104
105 val lockPrefix : Word8.word = 0wxF0
106
107 fun lexp le = Int32.fromInt (MLTreeEval.valueOf le)
108
109 val toWord8 = Word8.fromLargeWord o LargeWord.fromLargeInt o Int32.toLarge
110 val eBytes = Word8Vector.fromList
111 fun eByte i = eBytes [W8.fromInt i]
112
113 local
114 val toLWord = (W.fromLargeInt o Int32.toLarge)
115 fun shift (w,cnt) = W8.fromLargeWord(W.>>(w, cnt))
116 in
117 fun eShort i16 = let
118 val w = toLWord i16
119 in [
120 shift(w, 0w0), shift(w,0w8)
121 ] end
122 fun eLong i32 = let
123 val w = toLWord i32
124 in [
125 shift(w, 0w0), shift(w,0w8), shift(w,0w16), shift(w,0w24)
126 ] end
127 end (* local *)
128
129 fun eLongLong i64 = let
130 val toLLWord = (Word64.fromLargeInt o Int64.toLarge)
131 val w = toLLWord i64
132 fun shift64 (w,cnt) = let
133 val shifted = Word64.>>(w, cnt)
134 val masked = Word64.andb(0w255, shifted)
135 in Word8.fromInt (Word64.toInt masked) end
136 in [shift64(w, 0w0),
137 shift64(w, 0w8),
138 shift64(w, 0w16),
139 shift64(w, 0w24),
140 shift64(w, 0w32),
141 shift64(w, 0w40),
142 shift64(w, 0w48),
143 shift64(w, 0w56)]
144 end
145
146 fun eLongLongCut i64 = let
147 val toLLWord = (Word64.fromLargeInt o Int64.toLarge)
148 val w = toLLWord i64
149 fun shift64 (w,cnt) = let
150 val shifted = Word64.>>(w, cnt)
151 val masked = Word64.andb(0w255, shifted)
152 in Word8.fromInt (Word64.toInt masked) end
153 in [
154 shift64(w, 0w0), shift64(w, 0w8), shift64(w, 0w16), shift64(w, 0w24)
155 ] end
156
157 fun emitAMD64Instr instr = let
158 val error = fn msg => let
159 val AsmEmitter.S.STREAM{emit,...} = AsmEmitter.makeStream []
160 in
161 emit (I.INSTR instr); error msg
162 end
163
164 datatype reg_or_opc = REG of int | OPCODE of int
165 fun rMask r = r mod 8
166 fun getRO (REG r) = rMask r
167 | getRO (OPCODE oc) = oc
168 val rNum' = rMask o CB.physicalRegisterNum
169 val rNum = CB.physicalRegisterNum
170 val fNum = CB.physicalRegisterNum
171 val isExtReg = (fn x => x > 7) o rNum
172 fun isExtReg' (REG r) = r > 7
173 | isExtReg' _ = false
174
175 (* sizes of immediate operands *)
176 datatype size = Zero | Bits8 | Bits32
177 fun size i =
178 if i = 0 then Zero
179 else if Int32.<(i, 128) andalso Int32.<=(~128, i) then Bits8
180 else Bits32
181
182 fun immedOpnd (I.Immed i32) = i32
183 | immedOpnd (I.ImmedLabel le) = lexp le
184 | immedOpnd (I.LabelEA le) = lexp le
185 | immedOpnd _ = error "immedOpnd"
186
187 nonfix mod
188
189 fun scale (n, m) = Word.toIntX(Word.<<(Word.fromInt n, Word.fromInt m))
190 fun modrm {mod, reg, rm} = W8.fromInt(scale(mod,6) + scale(reg,3) + rm)
191 fun sib {ss, index, base} = W8.fromInt(scale(ss,6) + scale(index,3) + base)
192 fun eREXRegs (r, x, b) = let
193 val rb1 = if r then 0wx4 else 0wx0
194 val rb2 = if x then rb1 + 0wx2 else rb1
195 val rb3 = if b then rb2 + 0wx1 else rb2
196 in
197 rb3
198 end (* rex *)
199 fun eREX rb = 0wx40 + rb
200 fun eREX64 rb = eREX rb + 0wx8 (* sets REX.W *)
201
202 fun eImmedExt (_, I.Immed _) = error "eImmedExt: Immed"
203 | eImmedExt (_, I.Immed64 _) = error "eImmedExt: Immed64"
204 | eImmedExt (_, I.ImmedLabel _) = error "eImmedExt: ImmedLabel"
205 | eImmedExt (_, I.Relative _) = error "eImmedExt: Relative"
206 | eImmedExt (_, I.LabelEA _) = error "eImmedExt: LabelEA"
207 | eImmedExt (r', I.Direct (_, r)) =
208 ( (isExtReg' r', false, isExtReg r),
209 [modrm{mod=3, reg=getRO r', rm=rNum' r}] )
210 | eImmedExt (r', I.FDirect r) =
211 ( (isExtReg' r', false, isExtReg r),
212 [modrm{mod=3, reg=getRO r', rm=rNum' r}] )
213 | eImmedExt (r', I.Displace{base=base', disp, ...}) = let
214 val base = rNum' base'
215 val immed = immedOpnd disp
216 val rex = (isExtReg' r', false, isExtReg base')
217 val r' = getRO r'
218 fun displace(mod, eDisp) = if base = esp
219 then modrm{mod=mod, reg=r', rm=4}::
220 sib{ss=0, index=4, base=esp}::eDisp immed
221 else modrm{mod=mod, reg=r', rm=base} :: eDisp immed
222 in
223 (rex,
224 (case size immed
225 of Zero => if base = esp
226 then [modrm{mod=0, reg=r', rm=4}, sib{ss=0,index=4,base=esp}]
227 else if base = ebp
228 then [modrm{mod=1, reg=r', rm=ebp}, 0w0]
229 else [modrm{mod=0, reg=r', rm=base}]
230 | Bits8 => displace (1, fn i => [toWord8 i])
231 | Bits32 => displace (2, eLong)
232 (*esac*)) )
233 end
234 | eImmedExt (r', I.Indexed{base=NONE, index, scale, disp, ...}) = let
235 val rex = (isExtReg' r', isExtReg index, false)
236 val r' = getRO r'
237 in
238 (rex,
239 (modrm{mod=0, reg=r', rm=4} ::
240 sib{base=5, ss=scale, index=rNum' index} ::
241 eLong (immedOpnd disp)) )
242 end
243 | eImmedExt (r', I.Indexed{base=SOME b, index, scale, disp, ...}) = let
244 val rex = (isExtReg' r', isExtReg index, isExtReg b)
245 val r' = getRO r'
246 val index = rNum' index
247 val base = rNum' b
248 val immed = immedOpnd disp
249 fun indexed (mod, eDisp) =
250 modrm{mod=mod, reg=r', rm=4} ::
251 sib{ss=scale, index=index, base=base} :: eDisp immed
252 in
253 (rex,
254 (case size immed
255 of Zero =>
256 if base=ebp then
257 [modrm{mod=1, reg=r', rm=4},
258 sib{ss=scale, index=index, base=5}, 0w0]
259 else
260 [modrm{mod=0, reg=r', rm=4},
261 sib{ss=scale, index=index, base=base}]
262 | Bits8 => indexed(1, fn i => [toWord8 i])
263 | Bits32 => indexed(2, eLong)
264 (*esac*)) )
265 end
266
267 fun encode32' (bytes, r', opnd) = let
268 val (rex, e) = eImmedExt (r', opnd)
269 in
270 case eREXRegs rex
271 of 0w0 => bytes @ e
272 | rexByte => (eREX rexByte) :: bytes @ e
273 (* esac *)
274 end (* encode32' *)
275 fun encode64' (bytes, r', opnd) = let
276 val (rex, e) = eImmedExt (r', opnd)
277 in
278 (eREX64 (eREXRegs rex)) :: bytes @ e
279 end (* encode64' *)
280 fun encode' 32 = encode32'
281 | encode' _ = encode64'
282 fun encode32 (byte1, r', opnd) = eBytes (encode32' ([byte1], r', opnd))
283 fun encode64 (byte1, r', opnd) = eBytes (encode64' ([byte1], r', opnd))
284 fun encode sz = if sz = 64 then encode64 else encode32
285 fun encodeReg32 (byte1, r, opnd) = encode32 (byte1, REG(rNum r), opnd)
286 fun encodeReg64 (byte1, r, opnd) = encode64 (byte1, REG(rNum r), opnd)
287 fun encodeReg sz = if sz = 64 then encodeReg64 else encodeReg32
288 fun encodeLongImm32 (byte1, r', opnd, i) =
289 eBytes ((encode32' ([byte1], r', opnd)) @ eLong i)
290 fun encodeLongImm64 (byte1, r', opnd, i) =
291 eBytes ((encode64' ([byte1], r', opnd)) @ eLong i)
292 fun encodeLongImm sz = if sz = 64 then encodeLongImm64 else encodeLongImm32
293 fun encodeShortImm32 (byte1, r', opnd, w) =
294 eBytes ((encode32' ([byte1], r', opnd)) @ eShort w)
295 fun encodeShortImm64 (byte1, r', opnd, w) =
296 eBytes ((encode64' ([byte1], r', opnd)) @ eShort w)
297 fun encodeShortImm sz = if sz = 64 then encodeShortImm64 else encodeShortImm32
298 fun encodeByteImm32 (byte1, r', opnd, b) =
299 eBytes ((encode32' ([byte1], r', opnd)) @ [toWord8 b])
300 fun encodeByteImm64 (byte1, r', opnd, b) =
301 eBytes ((encode64' ([byte1], r', opnd)) @ [toWord8 b])
302 fun encodeByteImm sz = if sz = 64
303 then encodeByteImm64
304 else encodeByteImm32
305 fun encodeST (byte1, opc, STn) = let
306 fun reg {opc, reg} = W8.fromInt (scale (opc,3) + reg)
307 in
308 eBytes [byte1, reg{opc=opc,reg=fNum STn}]
309 end
310
311 (* arith: only 5 cases need be considered for each size:
312 * dst, src op/en
313 * --------------------------
314 * EAX, imm32 I
315 * r/m32, imm32 MI
316 * r/m32, imm8 MI
317 * r/m32, r32 MR
318 * r32, r/m32 RM
319 *)
320 fun arith (sz : int, opc1 : Word8.word, opc2 : reg_or_opc) = let
321 fun f (I.ImmedLabel le, dst) = f(I.Immed(lexp le), dst)
322 | f (I.LabelEA le, dst) = f(I.Immed(lexp le), dst)
323 | f (I.Immed i, dst) = (case size i
324 of Bits32 => (case dst
325 of I.Direct (_, r) => if CB.physicalRegisterNum r = eax
326 then if sz = 32
327 then eBytes (W8.fromInt (8 * (getRO opc2) + 5) :: eLong i)
328 else eBytes (eREX64 0w0 :: W8.fromInt(8 * (getRO opc2) + 5)
329 :: eLong i)
330 else encodeLongImm sz (0wx81, opc2, dst, i)
331 | _ => encodeLongImm sz (0wx81, opc2, dst, i)
332 (*esac*))
333 | _ => encodeByteImm sz (0wx83, opc2, dst, i) (* 83 /digit ib *)
334 (*esac*))
335 | f(src, I.Direct(_, r)) = encodeReg sz (opc1+0w3, r, src)
336 | f(I.Direct(_, r), dst) = encodeReg sz (opc1+0w1, r, dst)
337 | f _ = error "arith"
338 in
339 f
340 end (* arith *)
341
342 fun condCode cond = (case cond
343 of I.EQ => 0w4 | I.NE => 0w5
344 | I.LT => 0w12 | I.LE => 0w14
345 | I.GT => 0w15 | I.GE => 0w13
346 | I.A => 0w7 | I.AE => 0w3
347 | I.B => 0w2 | I.BE => 0w6
348 | I.C => 0w2 | I.NC => 0w3
349 | I.P => 0wxa | I.NP => 0wxb
350 | I.O => 0w0 | I.NO => 0w1
351 (*esac*))
352
353 (* test: the following cases need be considered:
354 * lsrc, rsrc
355 * -----------
356 * AL, imm8 opc1 A8
357 * EAX, imm32 opc1 A9
358 * r/m8, imm8 opc2 F6/0 ib
359 * r/m32, imm32 opc2 F7/0 id
360 * r/m8, r8 opc3 84/r
361 * r/m32, r32 opc3 85/r
362 *)
363 fun test (sz, I.ImmedLabel le, lsrc) = test(sz, I.Immed(lexp le), lsrc)
364 | test (sz, I.LabelEA le, lsrc) = test(sz, I.Immed(lexp le), lsrc)
365 | test (sz, I.Immed i, lsrc) = (case (lsrc, i >= 0 andalso i < 255)
366 of (I.Direct (_, r), false) => if CB.physicalRegisterNum r = eax
367 then eBytes(0wxA9 :: eLong i)
368 else encodeLongImm sz (0wxF7, OPCODE 0, lsrc, i)
369 | (_, false) => encodeLongImm sz (0wxF7, OPCODE 0, lsrc, i)
370 | (I.Direct (_, r), true) => let (* 8 bit *)
371 val r = CB.physicalRegisterNum r
372 in
373 if r = eax
374 then eBytes[0wxA8, toWord8 i]
375 else if r < 4
376 (* unfortunately, only CL, DL, BL can be encoded *)
377 then encodeByteImm sz (0wxF6, OPCODE 0, lsrc, i)
378 else if sz = 8
379 then error "test.8"
380 else encodeLongImm sz (0wxF7, OPCODE 0, lsrc, i)
381 end
382 | (_, true) => encodeByteImm sz (0wxF6, OPCODE 0, lsrc, i)
383 (* end case *))
384 | test (8, rsrc as I.Direct (_, r), lsrc) = if rNum r < 4
385 then encodeReg32 (0wx84, r, lsrc)
386 else error "test.8"
387 | test (sz, I.Direct (_, r), lsrc) = encodeReg sz (0wx85, r, lsrc)
388 | test _ = error "test"
389
390 fun movsd (byte3, r, opnd) =
391 eBytes (0wxf2::encode32'([0wxf, byte3], REG (rNum r), opnd))
392
393 (* DEBUG print instructions in stdout
394 fun makestream s = let
395 fun write f slice = let
396 val t = f slice
397 val _ = s := (!s)^t
398 in
399 String.size t
400 end
401 val writer =
402 TextPrimIO.WR
403 {
404 name = "stringout",
405 chunkSize = 512,
406 writeVec = SOME (write CharVectorSlice.vector),
407 writeArr = SOME (write CharArraySlice.vector),
408 writeVecNB = NONE,
409 writeArrNB = NONE,
410 block = NONE,
411 canOutput = NONE,
412 getPos = NONE,
413 setPos = NONE,
414 endPos = NONE,
415 verifyPos = NONE,
416 close = (fn () => ()),
417 ioDesc = NONE
418 }
419 val stream =
420 TextIO.StreamIO.mkOutstream (writer, IO.NO_BUF)
421 in
422 TextIO.mkOutstream stream
423 end
424
425 fun instrstring () =
426 let
427 val s = ref ""
428 val stream = makestream s
429 val _ =
430 AsmStream.withStream stream
431 (fn _ => let
432 val AsmEmitter.S.STREAM{emit,...} =
433 AsmEmitter.makeStream []
434 in
435 emit (I.INSTR instr) end) ()
436 val _ = TextIO.closeOut stream
437 in !s end
438
439 val _ = print (instrstring ())
440 *)
441
442 in
443 case instr
444 of I.NOP => eByte 0x90
445 | I.JMP(I.Relative i, _) => (
446 case size (Int32.fromInt(i-2))
447 of Bits32 => eBytes (0wxe9 :: eLong (Int32.fromInt (i-5)))
448 | _ => eBytes [0wxeb, Word8.fromInt(i-2)]
449 (*esac*))
450 | I.JMP(opnd, _) => let
451 val ty = (case opnd of I.Direct(ty,_) => ty | _ => ~1)
452 in
453 if ty = 64
454 then let
455 fun encodejmp (bytes, r', opnd) = let
456 val (rex, e) = eImmedExt (r', opnd)
457 in
458 case eREXRegs rex
459 of 0w0 => bytes @ e
460 | rexByte => (eREX rexByte) :: bytes @ e
461 (* end case *)
462 end
463 in
464 eBytes(encodejmp([0wxff], OPCODE 4, opnd))
465 end
466 else encode32(0wxff, OPCODE 4, opnd)
467 end
468 | I.JCC{cond, opnd=I.Relative i} => let
469 val code = condCode cond
470 val i' = Int32.fromInt i
471 in
472 case size (i'-2)
473 of Bits32 => eBytes(0wx0f :: Word8.+(0wx80, code) :: eLong(i'-6))
474 | _ => eBytes[Word8.+(0wx70,code), Word8.fromInt(i-2)]
475 (* end case *)
476 end
477 | I.CALL{opnd=I.Relative i,...} =>
478 eBytes (0wxe8 :: eLong (Int32.fromInt (i-5)))
479 | I.CALL{opnd, ...} => encode32 (0wxff, OPCODE 2, opnd)
480 | I.ENTER{src1, src2} => unimplemented "ENTER"
481 | I.LEAVE => eByte 0xc9
482 | I.RET NONE => eByte 0xc3
483 | I.MOVE{mvOp, src, dst} => let
484 (* utility to compute rex and adjusted register *)
485 fun rexReg r = if r < 8
486 then (0wx48, r)
487 else (0wx49, r - 8)
488 (* emit basic MOV operation *)
489 fun mov sz = (case (src, dst)
490 of (I.Immed i, I.Direct (_, r)) => (case sz
491 of 32 => eBytes (Word8.+(0wxb8, Word8.fromInt(rNum r))::eLong i)
492 | 64 => let
493 val (rex, reg) = rexReg(rNum r)
494 in
495 eBytes(rex::0wxc7::Word8.+(0wxc0, Word8.fromInt reg)::eLong i)
496 end
497 | _ => raise Fail "impossible"
498 (* end case *))
499 | (I.Immed i, _) => encodeLongImm sz (0wxc7, OPCODE 0, dst, i)
500 | (I.Immed64 i, I.Direct (_, r)) => if sz = 32
501 then let
502 val (start, reg) = if rNum r < 8
503 then ([], rNum r)
504 else ([0wx41], rNum r - 8)
505 in
506 eBytes(start@Word8.+(0wxb8, Word8.fromInt reg)::eLongLongCut i)
507 end
508 else let
509 val (rex, reg) = rexReg(rNum r)
510 in
511 eBytes(rex::Word8.+(0wxb8, Word8.fromInt reg)::eLongLong i)
512 end
513 | (I.Immed64 i, _) => error " Immed64 _"
514 | (I.ImmedLabel le, dst) =>
515 encodeLongImm sz (0wxc7, OPCODE 0, dst, lexp le)
516 | (I.LabelEA le, dst) => error "MOVL: LabelEA"
517 | (src, dst) => arith(sz, 0wx88, OPCODE 0) (src, dst)
518 (* end case *))
519 (* zero and sign-extension moves for 16/32-bit results *)
520 fun extend (opc, r) = eBytes (encode32' ([0wx0f, opc], REG(rNum r), src))
521 (* zero and sign-extension moves for 64-bit results *)
522 fun extend64 (opc, r) = eBytes (encode64' ([0wx0f, opc], REG(rNum r), src))
523 in
524 case (mvOp, src, dst)
525 of (I.MOVQ, _, _) => mov 64
526 | (I.MOVL, _, _) => mov 32
527 (* NOTE: there is a comment by Allen in the x86 code generator stating that there is a
528 * bug in the ML code generator related to sign extension, thus we ignore the range
529 * check and just store the low 8 bits.
530 *)
531 | (I.MOVB, I.Immed i, _) => encodeByteImm32 (0wxc6, OPCODE 0, dst, i)
532 (*
533 | (I.MOVB, I.Immed i, _) => (case size i
534 of Bits32 => error "MOVE: MOVB: imm8"
535 | _ => encodeByteImm32 (0wxc6, OPCODE 0, dst, i)
536 (* end case *))
537 *)
538 | (I.MOVB, I.Direct(_, r), _) => encodeReg32 (0wx88, r, dst)
539 | (I.MOVB, _, I.Direct(_, r)) => encodeReg32 (0wx8a, r, src)
540 | (I.MOVW, _, I.Direct(_, r)) =>
541 eBytes (0wx66 :: encode32' ([0wx89], REG (rNum r), dst))
542 | (I.MOVW, _, _) => unimplemented "MOVW"
543 | (I.MOVABSQ, I.Immed64 i, I.Direct(_, r)) => let
544 val (rex, reg) = rexReg (rNum r)
545 in
546 eBytes(rex :: (0wxb8+Word8.fromInt reg) :: eLongLong i)
547 end
548 | (I.MOVABSQ, I.ImmedLabel labexp, I.Direct(_ ,r)) => let
549 val (byte1, p) = rexReg(rNum r)
550 val byte2 = 0wxb8 + Word8.fromInt p
551 val byten = eLong (lexp labexp) (* FIXME: should be 64 bits *)
552 val hilong = if (lexp labexp) < 0
553 then [0wxff, 0wxff, 0wxff, 0wxff]
554 else eLong 0
555 in
556 eBytes ([byte1, byte2] @ byten @ hilong)
557 end
558 | (I.MOVABSQ, _, _) => unimplemented "MOVABSQ"
559 | (I.MOVSWQ, _, I.Direct(_, r)) => extend64 (0wxbf, r)
560 | (I.MOVZWQ, _, I.Direct(_, r)) => extend64 (0wxb7, r)
561 | (I.MOVSWL, _, I.Direct(_, r)) => extend (0wxbf, r)
562 | (I.MOVZWL, _, I.Direct(_, r)) => extend (0wxb7, r)
563 | (I.MOVSBQ, _, I.Direct(_, r)) => extend64 (0wxbe, r)
564 | (I.MOVZBQ, _, I.Direct(_, r)) => extend64 (0wxb6, r)
565 | (I.MOVSBL, _, I.Direct(_, r)) => extend (0wxbe, r)
566 | (I.MOVZBL, _, I.Direct(_, r)) => extend (0wxb6, r)
567 | (I.MOVSLQ, _, I.Direct(_, r)) => extend64 (0wx63, r)
568 | (I.CVTSD2SI, _, I.Direct(_, r)) => unimplemented "CVTSD2SI"
569 | (I.CVTSS2SI, _, I.Direct(_, r)) => unimplemented "CVTSS2SI"
570 | (I.CVTSD2SIQ, _, I.Direct(_, r)) => unimplemented "CVTSD2SIQ"
571 | (I.CVTSS2SIQ, _, I.Direct(_, r)) => unimplemented "CVTSS2SIQ"
572 | _ => error "MOVE"
573 (* end case *)
574 end
575 | I.LEAL{r32, addr} => encodeReg32(0wx8d, r32, addr)
576 | I.LEAQ{r64, addr} => encodeReg64(0wx8d, r64, addr)
577 | I.CMPQ{lsrc, rsrc} => arith(64, 0wx38, OPCODE 7) (rsrc, lsrc)
578 | I.CMPL{lsrc, rsrc} => arith(32, 0wx38, OPCODE 7) (rsrc, lsrc)
579 | I.CMPW _ => unimplemented "CMPW"
580 | I.CMPB _ => unimplemented "CMP"
581 | I.TESTQ{lsrc, rsrc} => test(64, rsrc, lsrc)
582 | I.TESTL{lsrc, rsrc} => test(32, rsrc, lsrc)
583 | I.TESTW _ => unimplemented "TESTW"
584 | I.TESTB{lsrc, rsrc} => test(8, rsrc, lsrc)
585 | I.BITOP{bitOp, lsrc, rsrc} => let
586 fun encode sz = (case lsrc
587 of I.Immed n =>
588 if ((0 <= n) andalso (n < sz))
589 then let
590 val (rex, [modRM]) = eImmedExt (OPCODE 4, rsrc)
591 val ib = Word8.fromLargeInt(Int32.toLarge n)
592 in
593 if (sz = 64)
594 then [eREX64 (eREXRegs rex), 0wx0f, 0wxba, modRM, ib]
595 else [0wx0f, 0wxba, modRM, ib]
596 end
597 else error "BITOP: invalid bit position"
598 | I.Direct(_, r) => let
599 val (rex, suffix) = eImmedExt (REG(rNum r), rsrc)
600 in
601 if (sz = 64)
602 then eREX64 (eREXRegs rex) :: 0wx0f :: 0wxba :: suffix
603 else 0wx0f :: 0wxba :: suffix
604 end
605 (* end case *))
606 in
607 case bitOp
608 of I.BTW => unimplemented "BTW"
609 | I.BTL => eBytes (encode 32)
610 | I.BTQ => eBytes (encode 64)
611 | I.LOCK_BTW => eBytes (lockPrefix :: encode 32)
612 | I.LOCK_BTL => eBytes (lockPrefix :: encode 64)
613 (* end case *)
614 end
615 | I.BINARY{binOp, src, dst} => let
616 fun shift (sz, code) = (case src
617 of I.Immed 1 => encode sz (0wxd1, OPCODE code, dst)
618 | I.Immed n => encodeByteImm sz (0wxc1, OPCODE code, dst, n)
619 | I.Direct(_, r) => if rNum r <> ecx
620 then error "shift: Direct"
621 else encode sz (0wxd3, OPCODE code, dst)
622 (* | I.MemReg _ => shift(code, memReg src) *)
623 | _ => error "shift"
624 (*esac*))
625 (* signed integer multiplication
626 * dst <- src * dst [REX.W] 0F AF /r
627 * dst <- src * immed8 [REX.W] 6B /r ib
628 * dst <- src * immed32 [REX.W] 69 /r id
629 *)
630 fun imul sz = (case (src, dst)
631 of (I.Immed i, I.Direct(_, dstR)) => (case size i
632 of Bits32 => encodeLongImm sz (0wx69, REG (rNum dstR), dst, i)
633 | _ => encodeByteImm sz (0wx6b, REG (rNum dstR), dst, i)
634 (* end case *))
635 | (_, I.Direct(_, dstR)) =>
636 eBytes (encode' sz ([0wx0f, 0wxaf], REG (rNum dstR), src))
637 | _ => error "imul"
638 (* end case *))
639 in
640 case binOp
641 of I.ADDQ => arith(64, 0w0, OPCODE 0) (src, dst)
642 | I.SUBQ => arith(64, 0wx28, OPCODE 5) (src, dst)
643 | I.ANDQ => arith(64, 0wx20, OPCODE 4) (src, dst)
644 | I.ORQ => arith(64, 0w8, OPCODE 1) (src, dst)
645 | I.XORQ => arith(64, 0wx30, OPCODE 6) (src, dst)
646 | I.SHLQ => shift(64, 4)
647 | I.SARQ => shift(64, 7)
648 | I.SHRQ => shift(64, 5)
649 | I.IMULQ => imul 64
650 | I.ADCQ => unimplemented "ADCQ"
651 | I.SBBQ => unimplemented "SBBQ"
652 | I.ADDL => arith(32, 0w0, OPCODE 0) (src, dst)
653 | I.SUBL => arith(32, 0wx28, OPCODE 5) (src, dst)
654 | I.ANDL => arith(32, 0wx20, OPCODE 4) (src, dst)
655 | I.ORL => arith(32, 0w8, OPCODE 1) (src, dst)
656 | I.XORL => arith(32, 0wx30, OPCODE 6) (src, dst)
657 | I.SHLL => shift(32, 4)
658 | I.SARL => shift(32, 7)
659 | I.SHRL => shift(32, 5)
660 | I.IMULL => imul 32
661 | I.ADCL => unimplemented "ADCL"
662 | I.SBBL => unimplemented "SBBL"
663 | I.ADDW => unimplemented "ADDW"
664 | I.SUBW => unimplemented "SUBW"
665 | I.ANDW => unimplemented "ANDW"
666 | I.ORW => unimplemented "ORW"
667 | I.XORW => unimplemented "XORW"
668 | I.SHLW => unimplemented "SHLW"
669 | I.SARW => unimplemented "SARW"
670 | I.SHRW => unimplemented "SHRW"
671 | I.IMULW => unimplemented "IMULW"
672 | I.ADDB => unimplemented "ADDB"
673 | I.SUBB => unimplemented "SUBB"
674 | I.ANDB => unimplemented "ANDB"
675 | I.ORB => unimplemented "ORB"
676 | I.XORB => unimplemented "XORB"
677 | I.SHLB => unimplemented "SHLB"
678 | I.SARB => unimplemented "SARB"
679 | I.SHRB => unimplemented "SHRB"
680 | I.IMULB => unimplemented "IMULB"
681 | I.BTSW => unimplemented "BTSW"
682 | I.BTCW => unimplemented "BTCW"
683 | I.BTRW => unimplemented "BTRW"
684 | I.BTSL => unimplemented "BTSL"
685 | I.BTCL => unimplemented "BTCL"
686 | I.BTRL => unimplemented "BTRL"
687 | I.ROLW => unimplemented "ROLW"
688 | I.RORW => unimplemented "RORW"
689 | I.ROLL => unimplemented "ROLL"
690 | I.RORL => unimplemented "RORL"
691 | I.XCHGB => unimplemented "XCHGB"
692 | I.XCHGW => unimplemented "XCHGW"
693 | I.XCHGL => unimplemented "XCHGL"
694 | I.LOCK_ADCW => unimplemented "LOCK_ADCW"
695 | I.LOCK_ADCL => unimplemented "LOCK_ADCL"
696 | I.LOCK_ADDW => unimplemented "LOCK_ADDW"
697 | I.LOCK_ADDL => unimplemented "LOCK_ADDL"
698 | I.LOCK_ANDW => unimplemented "LOCK_ANDW"
699 | I.LOCK_ANDL => unimplemented "LOCK_ANDL"
700 | I.LOCK_BTSW => unimplemented "LOCK_BTSW"
701 | I.LOCK_BTSL => unimplemented "LOCK_BTSL"
702 | I.LOCK_BTRW => unimplemented "LOCK_BTRW"
703 | I.LOCK_BTRL => unimplemented "LOCK_BTRL"
704 | I.LOCK_BTCW => unimplemented "LOCK_BTCW"
705 | I.LOCK_BTCL => unimplemented "LOCK_BTCL"
706 | I.LOCK_ORW => unimplemented "LOCK_ORW"
707 | I.LOCK_ORL => unimplemented "LOCK_ORL"
708 | I.LOCK_SBBW => unimplemented "LOCK_SBBW"
709 | I.LOCK_SBBL => unimplemented "LOCK_SBBL"
710 | I.LOCK_SUBW => unimplemented "LOCK_SUBW"
711 | I.LOCK_SUBL => unimplemented "LOCK_SUBL"
712 | I.LOCK_XORW => unimplemented "LOCK_XORW"
713 | I.LOCK_XORL => unimplemented "LOCK_XORL"
714 | I.LOCK_XADDB => unimplemented "LOCK_XADDB"
715 | I.LOCK_XADDW => unimplemented "LOCK_XADDW"
716 | I.LOCK_XADDL => unimplemented "LOCK_XADDL"
717 (* end case *)
718 end
719 | I.SHIFT{shiftOp, src, dst, count} => (case shiftOp
720 of I.SHLDL => unimplemented "SHLDL"
721 | I.SHRDL => unimplemented "SHRDL"
722 (* end case *))
723 | I.MULTDIV{multDivOp, src} => let
724 val (mulOp, sz) = (case multDivOp
725 of I.MULL1 => (4, 32) | I.IDIVL1 => (7, 32) | I.DIVL1 => (6, 32)
726 | I.MULQ1 => (4, 64) | I.IDIVQ1 => (7, 64) | I.DIVQ1 => (6, 64)
727 | I.IMULL1 => error "imull1"
728 | I.IMULQ1 => error "imulq1"
729 (* esac *))
730 in
731 encode sz (0wxf7, OPCODE mulOp, src)
732 end
733 | I.MUL3{dst, src1, src2=i} => (case src1
734 of I.Immed _ => error "mul3: Immed"
735 | I.ImmedLabel _ => error "mul3: ImmedLabel"
736 | _ => (case size i
737 of Bits32 => encodeLongImm32(0wx69, REG (rNum dst), src1, i)
738 | _ => encodeByteImm32(0wx6b, REG (rNum dst), src1, i)
739 (*esac*))
740 (*esac*))
741 | I.MULQ3{dst, src1, src2=i} => (case src1
742 of I.Immed _ => error "mul3: Immed"
743 | I.ImmedLabel _ => error "mul3: ImmedLabel"
744 | _ => (case size i
745 of Bits32 => encodeLongImm64(0wx69, REG (rNum dst), src1, i)
746 | _ => encodeByteImm64(0wx6b, REG (rNum dst), src1, i)
747 (*esac*))
748 (*esac*))
749 | I.UNARY{unOp, opnd} => let
750 fun lock code = eBytes(lockPrefix :: code)
751 in
752 case unOp
753 of I.DECQ => encode64 (0wxff, OPCODE 1, opnd)
754 | I.INCQ => encode64 (0wxff, OPCODE 0, opnd)
755 | I.NEGQ => encode64 (0wxf7, OPCODE 3, opnd)
756 | I.NOTQ => encode64 (0wxff, OPCODE 2, opnd)
757 | I.DECL => encode32 (0wxff, OPCODE 1, opnd)
758 | I.NEGL => encode32 (0wxff, OPCODE 3, opnd)
759 | I.INCL => encode32 (0wxff, OPCODE 0, opnd)
760 | I.NOTL => encode32 (0wxff, OPCODE 2, opnd)
761 | I.DECW => unimplemented "DECW"
762 | I.NEGW => unimplemented "NEGW"
763 | I.INCW => unimplemented "INCW"
764 | I.NOTW => unimplemented "NOTW"
765 | I.DECB => unimplemented "DECB"
766 | I.NEGB => unimplemented "NEGB"
767 | I.INCB => unimplemented "INCB"
768 | I.NOTB => unimplemented "NOTB"
769 | I.LOCK_DECQ => lock (encode64' ([0wxff], OPCODE 1, opnd))
770 | I.LOCK_INCQ => lock (encode64' ([0wxff], OPCODE 0, opnd))
771 | I.LOCK_NEGQ => lock (encode64' ([0wxf7], OPCODE 3, opnd))
772 | I.LOCK_NOTQ => lock (encode64' ([0wxff], OPCODE 2, opnd))
773 (* esac *)
774 end
775 | I.SET{cond,opnd} =>
776 eBytes (encode32' ([0wx0f, Word8.+(0wx90,condCode cond)], REG 0, opnd))
777 | I.CMOV{cond,src,dst} =>
778 eBytes (encode32' ([0wx0f, Word8.+(condCode cond,0wx40)], REG (rNum dst), src))
779 | I.PUSH(I.Immed i) => (case size i
780 of Bits32 => eBytes(0wx68 :: eLong i)
781 | _ => eBytes [0wx6a, toWord8 i]
782 (* esac *))
783 | I.PUSH(I.Direct(_, r)) => let
784 val r = rNum r
785 in
786 if (r < 8)
787 then eByte (0x50 + r)
788 else eBytes [0wx41, 0wx50 + Word8.fromInt(r - 8)]
789 end
790 (* TODO: check that PUSH is correct for other operands *)
791 | I.PUSH opnd => encode32 (0wxff, OPCODE 6, opnd)
792 | I.PUSHFQ => eByte 0x9c
793 | I.POPFQ => eByte 0x9d
794 | I.POP(I.Direct(_, r)) => let
795 val r = rNum r
796 in
797 if (r < 8)
798 then eByte (0x58 + r)
799 else eBytes [0wx41, 0wx58 + Word8.fromInt(r - 8)]
800 end
801 (* TODO: check that POP is correct for other operands *)
802 | I.POP opnd => encode32 (0wx8f, OPCODE 0, opnd)
803 | I.CDQ => eByte 0x99
804 | I.CDO => eBytes [0wx48, 0wx99]
805 | I.INT b => eBytes [0wxcd, b]
806 | I.FMOVE {fmvOp=I.MOVSD, dst=I.FDirect r, src=src} => movsd(0wx10, r, src)
807 | I.FMOVE {fmvOp=I.MOVSD, dst=dst, src=I.FDirect r} => movsd(0wx11, r, dst)
808 | I.FMOVE {fmvOp, dst, src} => (case fmvOp
809 of I.CVTSI2SD => error "CVTSI2SD not implemented"
810 | I.CVTSI2SDQ => let
811 val I.FDirect r = dst
812 in
813 eBytes([0wxf2] @ encode64'([0wxf,0wx2a],REG (fNum r),src))
814 end
815 | I.CVTSD2SS => (case dst
816 of (I.FDirect r) =>
817 eBytes([0wxf2] @ encode32'([0wxf,0wx5a],REG (fNum r),src))
818 | _ => error "CVTSD2SS"
819 (* end case *))
820 | I.CVTSS2SD => (case dst
821 of (I.FDirect r) =>
822 eBytes([0wxf3] @ encode32'([0wxf,0wx5a],REG (fNum r),src))
823 | _ => error "CVTSD2SD"
824 (* end case *))
825 | I.MOVSS => (case src
826 of (I.FDirect r) =>
827 eBytes([0wxf3] @ encode32'([0wxf,0wx11],REG (fNum r),dst))
828 | _ => error "MOVSS"
829 (* end case *))
830 | _ => error "FMOVE"
831 (* end case *))
832 | I.FBINOP {binOp, src, dst} => let
833 fun encode2 (op1, op2) = eBytes(encode32'([op1, op2], REG(fNum dst), src))
834 fun encode (op1, op2, op3) =
835 eBytes(op1 :: encode32'([op2, op3], REG(fNum dst), src))
836 in
837 case binOp
838 of I.ADDSS => encode (0wxf3, 0wx0f, 0wx58)
839 | I.ADDSD => encode (0wxf2, 0wx0f, 0wx58)
840 | I.SUBSS => encode (0wxf3, 0wx0f, 0wx5c)
841 | I.SUBSD => encode (0wxf2, 0wx0f, 0wx5c)
842 | I.MULSS => encode (0wxf3, 0wx0f, 0wx59)
843 | I.MULSD => encode (0wxf2, 0wx0f, 0wx59)
844 | I.DIVSS => encode (0wxf3, 0wx0f, 0wx5e)
845 | I.DIVSD => encode (0wxf2, 0wx0f, 0wx5e)
846 | I.XORPS => encode2 (0wx0f, 0wx57)
847 | I.XORPD => encode (0wx66, 0wx0f, 0wx57)
848 | I.ANDPS => encode2 (0wx0f, 0wx54)
849 | I.ANDPD => encode (0wx66, 0wx0f, 0wx54)
850 | I.ORPS => encode2 (0wx0f, 0wx56)
851 | I.ORPD => encode (0wx66, 0wx0f, 0wx56)
852 (* end case *)
853 end
854 | I.FCOM {comOp, src, dst} => (case comOp
855 of I.COMISS => unimplemented "COMISS"
856 | I.COMISD => unimplemented "COMISD"
857 | I.UCOMISS => unimplemented "UCOMISS"
858 | I.UCOMISD => eBytes([0wx66] @ encode32'([0wxf,0wx2e],REG (fNum dst),src))
859 (* end case *))
860 | I.FSQRTS {dst, src} => let
861 val I.FDirect r = dst
862 in
863 eBytes([0wxf3] @ encode32'([0wxf,0wx51],REG (fNum r),src))
864 end
865 | I.FSQRTD {dst, src} => let
866 val I.FDirect r = dst
867 in
868 eBytes([0wxf2] @ encode32'([0wxf,0wx51],REG (fNum r),src))
869 end
870 | I.SAHF => eByte 0x9e
871 | I.LFENCE => eBytes [0wx0f, 0wxae, 0wxe8]
872 | I.MFENCE => eBytes [0wx0f, 0wxae, 0wxf0]
873 | I.SFENCE => eBytes [0wx0f, 0wxae, 0wxf8]
874 | I.PAUSE => eBytes [0wxf3, 0wx90]
875 | I.XCHG _ => unimplemented "XCHG"
876 | I.CMPXCHG _ => unimplemented "CMPXCHG"
877 | I.XADD _ => unimplemented "XADD"
878 | I.RDTSC => unimplemented "RDTSC"
879 | I.RDTSCP => unimplemented "RDTSCP"
880 | I.LAHF => eByte 0x9f
881 | _ => error "emitInstr"
882 (* esac *)
883 end (* emitAMD64Instr *)
884
885 fun emitInstr (I.LIVE _) = Word8Vector.fromList []
886 | emitInstr (I.KILL _) = Word8Vector.fromList []
887 | emitInstr (I.COPY{k, dst, src, tmp, ...}) = (case k
888 of CB.GP => emitInstrs (Shuffle.shuffle {tmp=tmp, dst=dst, src=src})
889 | CB.FP => emitInstrs (Shuffle.shufflefp {tmp=tmp, dst=dst, src=src})
890 | _ => error "COPY"
891 (*esac*))
892 | emitInstr (I.INSTR instr) = emitAMD64Instr instr
893 (* NOTE: the general consensus on the internet is that Intel hardware ignores the
894 * branch hint prefixes and just relies on dynamic techniques. Therefore, we
895 * comment this code out for now.
896 | emitInstr (I.ANNOTATION{i, a}) = (case #peek MLRiscAnnotations.BRANCH_PROB a
897 of SOME prob => let
898 val prob = Probability.toReal prob
899 fun emit (I.ANNOTATION{i, ...}) = emit i
900 | emit (I.INSTR(instr as I.JCC _)) = let
901 val code = emitInstr i
902 in
903 if prob < 0.5
904 then Word8Vector.prepend (0wx2e, code) (* add not-taken hint *)
905 else if prob > 0.5
906 then Word8Vector.prepend (0wx3e, code) (* add taken hint *)
907 else code
908 end
909 | emit _ = error "bogus BRANCH_PROB annotation"
910 in
911 emit i
912 end
913 | NONE => emitInstr i
914 (* end case *))
915 *)
916 | emitInstr (I.ANNOTATION{i, a}) = emitInstr i
917
918 and emitInstrs instrs = Word8Vector.concat(map emitInstr instrs)
919
920 end (* AMD64MCEmitter *)