Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/amd64.mdl

raw
1 (* amd64.mdl
2 *
3 * COPYRIGHT (c) 2019 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *
6 * MLRISC machine description for AMD64 (aka x86-64) architecture.
7 *)
8
9 architecture AMD64 =
10 struct
11
12 superscalar (* superscalar machine *)
13
14 little endian (* little endian architecture *)
15
16 lowercase assembly (* print assembly in lower case *)
17
18 storage
19 GP = $r[16] of 64 bits
20 asm: (fn (0,8) => "%al" | (4, 8) => "%ah"
21 | (1,8) => "%cl" | (5, 8) => "%ch"
22 | (2,8) => "%dl" | (6, 8) => "%dh"
23 | (3,8) => "%bl" | (7, 8) => "%bh"
24 | (r,8) => "%r"^Int.toString r^"b"
25
26 | (0,16) => "%ax" | (4,16) => "%sp"
27 | (1,16) => "%cx" | (5,16) => "%bp"
28 | (2,16) => "%dx" | (6,16) => "%si"
29 | (3,16) => "%bx" | (7,16) => "%di"
30 | (r,16) => "%r"^Int.toString r^"w"
31
32 | (0,32) => "%eax" | (4,32) => "%esp"
33 | (1,32) => "%ecx" | (5,32) => "%ebp"
34 | (2,32) => "%edx" | (6,32) => "%esi"
35 | (3,32) => "%ebx" | (7,32) => "%edi"
36 | (r,32) => "%r"^Int.toString r^"d"
37
38 | (0,64) => "%rax" | (4,64) => "%rsp"
39 | (1,64) => "%rcx" | (5,64) => "%rbp"
40 | (2,64) => "%rdx" | (6,64) => "%rsi"
41 | (3,64) => "%rbx" | (7,64) => "%rdi"
42 | (r,64) => "%r"^Int.toString r
43
44 | (r,_) => "%"^Int.toString r
45 )
46 | FP = $f[16] of 64 bits
47 asm: (fn (f, _) =>
48 if f < 16
49 then "%xmm"^Int.toString f
50 else "%f"^Int.toString f )
51 | CC = $cc[] of 32 bits aliasing GP asm: "cc"
52 | EFLAGS = $eflags[1] of 32 bits asm: "$eflags"
53 | FFLAGS = $fflags[1] of 32 bits asm: "$fflags"
54 | MEM = $m[] of 8 aggregable bits asm: "mem"
55 | CTRL = $ctrl[] asm: "ctrl"
56
57 locations
58 rax = $r[0]
59 and rcx = $r[1]
60 and rdx = $r[2]
61 and rbx = $r[3]
62 and rsp = $r[4]
63 and rbp = $r[5]
64 and rsi = $r[6]
65 and rdi = $r[7]
66 and r8 = $r[8]
67 and r9 = $r[9]
68 and r10 = $r[10]
69 and r11 = $r[11]
70 and r12 = $r[12]
71 and r13 = $r[13]
72 and r14 = $r[14]
73 and r15 = $r[15]
74
75 and xmm0 = $f[0]
76 and xmm1 = $f[1]
77 and xmm2 = $f[2]
78 and xmm3 = $f[3]
79 and xmm4 = $f[4]
80 and xmm5 = $f[5]
81 and xmm6 = $f[6]
82 and xmm7 = $f[7]
83 and xmm8 = $f[8]
84 and xmm9 = $f[9]
85 and xmm10 = $f[10]
86 and xmm11 = $f[11]
87 and xmm12 = $f[12]
88 and xmm13 = $f[13]
89 and xmm14 = $f[14]
90 and xmm15 = $f[15]
91
92 and stackptrR = $r[4]
93 and asmTmpR = $r[0] (* not used *)
94 and fasmTmp = $f[0] (* not used *)
95 and eflags = $eflags[0]
96
97 (*------------------------------------------------------------------------
98 *
99 * Representation for various opcodes.
100 *
101 *------------------------------------------------------------------------*)
102 structure Instruction =
103 struct
104 (* An effective address can be any combination of
105 * base + index*scale + disp
106 * or
107 * B + I*SCALE + DISP
108 *
109 * where any component is optional. The operand datatype captures
110 * all these combinations.
111 *
112 * DISP == Immed | ImmedLabel | Const
113 * B == Displace{base=B, disp=0}
114 * B+DISP == Displace{base=B, disp=DISP}
115 * I*2^SCALE+DISP == Indexed{base=NONE,index=I,scale=SCALE,disp=D}
116 * B+I*2^SCALE+DISP == Indexed{base=SOME B,index=I,scale=SCALE,disp=DISP}
117 * Note1: The index register cannot be rbp.
118 * The disp field must be one of Immed, ImmedLabel, or Const.
119 *)
120
121 (* Note: Relative is only generated after sdi resolution *)
122 datatype operand =
123 Immed of Int32.int rtl: int
124 | Immed64 of Int64.int rtl: int
125 | ImmedLabel of T.labexp rtl: labexp
126 | Relative of int (* no semantics given *)
127 | LabelEA of T.labexp rtl: labexp (* XXX *)
128 | Direct of (int * $GP) rtl: $r[GP]
129 | FDirect of $FP rtl: $f[FP]
130 | Displace of {base: $GP, disp:operand, mem:Region.region}
131 rtl: $m[$r[base] + disp : mem]
132 | Indexed of {base: $GP option, index: $GP, scale:int,
133 disp:operand, mem:Region.region}
134 rtl: $m[$r[base] + $r[index] << scale + disp : mem]
135
136 type addressing_mode = operand
137
138 type ea = operand
139
140 type byte = Word8.word
141
142 datatype cond! =
143 EQ "e" 0w4 | NE 0w5 | LT "l" 0w12 | LE 0w14 | GT "g" 0w15 | GE 0w13
144 | B 0w2 | BE (* below *) 0w6 | A 0w7 | AE (* above *) 0w3
145 | C 0w2 | NC (* if carry *) 0w3 | P 0wxa | NP (* if parity *) 0wxb
146 | O 0w0 | NO (* overflow *) 0w1
147
148 (* LOCK can only be used in front of
149 * (Intel ordering, not gasm ordering)
150 * ADC, ADD, AND, BT mem, reg/imm
151 * BTS, BTR, BTC, OR mem, reg/imm
152 * SBB, SUB, XOR mem, reg/imm
153 * XCHG reg, mem
154 * XCHG mem, reg
155 * DEC, INC, NEG, NOT mem
156 *)
157
158 datatype binaryOp! =
159 ADDQ | SUBQ | ANDQ | ORQ | XORQ | SHLQ | SARQ | SHRQ | IMULQ
160 | ADCQ | SBBQ
161 | ADDL | SUBL | ANDL | ORL | XORL | SHLL | SARL | SHRL | IMULL
162 | ADCL | SBBL
163 | ADDW | SUBW | ANDW | ORW | XORW | SHLW | SARW | SHRW | IMULW
164 | ADDB | SUBB | ANDB | ORB | XORB | SHLB | SARB | SHRB | IMULB
165 | BTSW | BTCW | BTRW | BTSL | BTCL | BTRL
166 | ROLW | RORW | ROLL | RORL
167 | XCHGB | XCHGW | XCHGL
168 | LOCK_ADCW "lock\n\tadcw"
169 | LOCK_ADCL "lock\n\tadcl"
170 | LOCK_ADDW "lock\n\taddw"
171 | LOCK_ADDL "lock\n\taddl"
172 | LOCK_ANDW "lock\n\tandw"
173 | LOCK_ANDL "lock\n\tandl"
174 | LOCK_BTSW "lock\n\tbtsw"
175 | LOCK_BTSL "lock\n\tbtsl"
176 | LOCK_BTRW "lock\n\tbtrw"
177 | LOCK_BTRL "lock\n\tbtrl"
178 | LOCK_BTCW "lock\n\tbtcw"
179 | LOCK_BTCL "lock\n\tbtcl"
180 | LOCK_ORW "lock\n\torw"
181 | LOCK_ORL "lock\n\torl"
182 | LOCK_SBBW "lock\n\tsbbw"
183 | LOCK_SBBL "lock\n\tsbbl"
184 | LOCK_SUBW "lock\n\tsubw"
185 | LOCK_SUBL "lock\n\tsubl"
186 | LOCK_XORW "lock\n\txorw"
187 | LOCK_XORL "lock\n\txorl"
188 | LOCK_XADDB "lock\n\txaddb"
189 | LOCK_XADDW "lock\n\txaddw"
190 | LOCK_XADDL "lock\n\txaddl"
191
192 (* One operand multiplication/division operations *)
193 datatype multDivOp! =
194 IMULL1 "imull" | MULL1 "mull" | IDIVL1 "idivl" | DIVL1 "divl"
195 | IMULQ1 "imulq" | MULQ1 "mulq" | IDIVQ1 "idivq" | DIVQ1 "divq"
196
197 datatype unaryOp! = DECQ | INCQ | NEGQ | NOTQ
198 | DECL | INCL | NEGL | NOTL
199 | DECW | INCW | NEGW | NOTW
200 | DECB | INCB | NEGB | NOTB
201 | LOCK_DECQ "lock\n\tdecq"
202 | LOCK_INCQ "lock\n\tincq"
203 | LOCK_NEGQ "lock\n\tnegq"
204 | LOCK_NOTQ "lock\n\tnotq"
205
206 datatype shiftOp! = SHLDL | SHRDL
207
208 datatype bitOp! = BTW
209 | BTL
210 | BTQ
211 | LOCK_BTW "lock\n\tbtw"
212 | LOCK_BTL "lock\n\tbtl"
213
214 datatype move! = MOVQ
215 | MOVL (* zx(long) -> qword *)
216 | MOVB
217 | MOVW
218 | MOVABSQ
219 | MOVSWQ (* sx(word) -> qword *)
220 | MOVZWQ (* zx(word) -> qword *)
221 | MOVSWL (* sx(word) -> long *)
222 | MOVZWL (* zx(word) -> long *)
223 | MOVSBQ (* sx(byte) -> qword *)
224 | MOVZBQ (* zx(byte) -> qword *)
225 | MOVSBL (* sx(byte) -> long *)
226 | MOVZBL (* zx(byte) -> long *)
227 | MOVSLQ (* sx(long) -> qword *)
228 | CVTSD2SI (* double -> long *)
229 | CVTSS2SI (* float -> long *)
230 | CVTSD2SIQ (* double -> qword *)
231 | CVTSS2SIQ (* float -> qword *)
232
233 datatype fbin_op! =
234 ADDSS | ADDSD
235 | SUBSS | SUBSD
236 | MULSS | MULSD
237 | DIVSS | DIVSD
238 | XORPS | XORPD
239 | ANDPS | ANDPD
240 | ORPS | ORPD
241
242 datatype fcom_op! =
243 COMISS | COMISD (* ordered *)
244 | UCOMISS | UCOMISD (* unordered *)
245
246 datatype fmove_op! =
247 MOVSS | MOVSD
248 (* conversion *)
249 | CVTSS2SD | CVTSD2SS
250 | CVTSI2SS | CVTSI2SSQ | CVTSI2SD | CVTSI2SDQ
251
252 (* Intel floating point precision *)
253 datatype fsize = FP32 "s" | FP64 "l"
254
255 (* Intel integer precision *)
256 datatype isize = I8 "8" | I16 "16" | I32 "32" | I64 "64"
257
258 end (* Instruction *)
259
260 (*------------------------------------------------------------------------
261 *
262 * Here, I'm going to define the semantics of the instructions
263 *
264 *------------------------------------------------------------------------*)
265 structure RTL =
266 struct
267
268 (* Get the basis *)
269 include "Tools/basis.mdl"
270 open Basis
271 infix 1 || (* parallel effects *)
272 infix 2 := (* assignment *)
273
274 (* Some type abbreviations *)
275 fun byte x = (x : #8 bits)
276 fun word x = (x : #16 bits)
277 fun long x = (x : #32 bits)
278 fun qword x = (x: #64 bits)
279 fun float x = (x : #32 bits)
280 fun double x = (x : #64 bits)
281
282 (* Intel register abbreviations *)
283 val rax = $r[0] and rcx = $r[1] and rdx = $r[2] and rbx = $r[3]
284 and rsp = $r[4] and rbp = $r[5] and rsi = $r[6] and rdi = $r[7]
285
286 (* Condition codes bits in eflag.
287 * Let's give symbolic name for each bit as per the Intel doc.
288 *)
289 rtl setFlag : #n bits -> #n bits
290 fun flag b = andb($eflags[0] >> b, 1)
291 val CF = flag 0 and PF = flag 2
292 and ZF = flag 6 and SF = flag 7 and OF = flag 11
293
294 (* Now gets use the bits to express the conditions. Again from Intel. *)
295 (* conditions *) (* aliases *)
296 val B = CF == 1 val C = B and NAE = B
297 val BE = CF == 1 orelse ZF == 1 val NA = BE
298 val E = ZF == 1 val Z = E
299 val L = SF <> OF val NGE = L
300 val LE = SF <> OF orelse ZF == 1 val NG = LE
301 val NB = CF == 0 val AE = NB and NC = NB
302 val NBE = CF == 0 andalso ZF == 0 val A = NBE
303 val NE = ZF == 0 val NZ = NE
304 val NL = SF == OF val GE = NL
305 val NLE = ZF == 0 andalso SF == OF val G = NLE
306 val NO = OF == 0
307 val NP = PF == 0 val PO = NP
308 val NS = SF == 0
309 val O = OF == 1
310 val P = PF == 1 val PE = P
311 val S = SF == 1
312
313 rtl NOP{} = () (* duh! *)
314 rtl LEAL{addr, r32} = $r[r32] := addr (* this is completely wrong! XXX *)
315 rtl LEAQ{addr, r64} = $r[r32] := addr (* this is completely wrong! XXX *)
316
317 (* moves with type conversion *)
318 rtl MOVQ{src,dst} = dst := qword src
319 rtl MOVL{src,dst} = dst := long src
320 rtl MOVW{src,dst} = dst := word src
321 rtl MOVB{src,dst} = dst := byte src
322 rtl MOVSWL{src,dst} = dst := long(sx(word src))
323 rtl MOVZWL{src,dst} = dst := long(zx(word src))
324 rtl MOVSBL{src,dst} = dst := long(sx(byte src))
325 rtl MOVZBL{src,dst} = dst := long(zx(byte src))
326 rtl MOVSWQ{src,dst} = dst := qword(sx(word src))
327 rtl MOVZWQ{src,dst} = dst := qword(zx(word src))
328 rtl MOVSBQ{src,dst} = dst := qword(sx(byte src))
329 rtl MOVZBQ{src,dst} = dst := qword(zx(byte src))
330 rtl MOVZLQ{src,dst} = dst := qword(zx(long src))
331
332 (* semantics of integer arithmetic;
333 * all instructions sets the condition code
334 *)
335 fun binop typ oper {dst,src} = dst := typ(oper(dst,src))
336 fun arith typ oper {dst,src} = dst := typ(oper(dst,src))
337 || $eflags[0] := ??? (* XXX *)
338 fun unary typ oper {opnd} = opnd := typ(oper opnd)
339
340 fun inc x = x + 1
341 fun dec x = x - 1
342
343 (* I'm too lazy to specify the semantics of these for now *)
344 rtl adc sbb bts btc btr rol ror xchg xadd cmpxchg rdtsc rdtscp
345 : #n bits * #n bits -> #n bits
346
347 rtl [ADD,SUB,AND,OR,XOR]^^B = map (arith byte) [(+),(-),andb,orb,xorb]
348 rtl [ADD,SUB,AND,OR,XOR]^^W = map (arith word) [(+),(-),andb,orb,xorb]
349 rtl [ADD,SUB,AND,OR,XOR]^^L = map (arith long) [(+),(-),andb,orb,xorb]
350 rtl [ADD,SUB,AND,OR,XOR]^^Q = map (arith qword) [(+),(-),andb,orb,xorb]
351 rtl [SHR,SHL,SAR]^^B = map (binop byte) [(>>),(<<),(~>>)]
352 rtl [SHR,SHL,SAR]^^W = map (binop word) [(>>),(<<),(~>>)]
353 rtl [SHR,SHL,SAR]^^L = map (binop long) [(>>),(<<),(~>>)]
354 rtl [SHR,SHL,SAR]^^Q = map (binop qword) [(>>),(<<),(~>>)]
355 rtl [NEG,NOT,INC,DEC]^^B = map (unary byte) [(~),notb,inc,dec]
356 rtl [NEG,NOT,INC,DEC]^^W = map (unary word) [(~),notb,inc,dec]
357 rtl [NEG,NOT,INC,DEC]^^L = map (unary long) [(~),notb,inc,dec]
358 rtl [NEG,NOT,INC,DEC]^^Q = map (unary qword) [(~),notb,inc,dec]
359
360
361 rtl [ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^B =
362 map (arith byte) [adc,sbb,bts,btc,btr,rol,ror,xchg]
363 rtl [ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^W =
364 map (arith word) [adc,sbb,bts,btc,btr,rol,ror,xchg]
365 rtl [ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^L =
366 map (arith long) [adc,sbb,bts,btc,btr,rol,ror,xchg]
367 rtl [ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^Q =
368 map (arith qword) [adc,sbb,bts,btc,btr,rol,ror,xchg]
369
370 fun lockarith typ oper {src,dst}=
371 dst := typ(oper(dst,src))
372 || Kill $eflags[0] (* XXX *)
373 fun lockunary typ oper {opnd} =
374 opnd := typ(oper(opnd))
375 || Kill $eflags[0] (* XXX *)
376
377 rtl LOCK_^^[ADD,SUB,AND,OR,XOR,XADD]^^B =
378 map (lockarith byte) [(+),(-),andb,orb,xorb,xadd]
379 rtl LOCK_^^[ADD,SUB,AND,OR,XOR,XADD]^^W =
380 map (lockarith word) [(+),(-),andb,orb,xorb,xadd]
381 rtl LOCK_^^[ADD,SUB,AND,OR,XOR,XADD]^^L =
382 map (lockarith long) [(+),(-),andb,orb,xorb,xadd]
383 rtl LOCK_^^[ADD,SUB,AND,OR,XOR,XADD]^^Q =
384 map (lockarith qword) [(+),(-),andb,orb,xorb,xadd]
385 rtl LOCK_^^[ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^B =
386 map (lockarith byte) [adc,sbb,bts,btc,btr,rol,ror,xchg]
387 rtl LOCK_^^[ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^W =
388 map (lockarith word) [adc,sbb,bts,btc,btr,rol,ror,xchg]
389 rtl LOCK_^^[ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^L =
390 map (lockarith long) [adc,sbb,bts,btc,btr,rol,ror,xchg]
391 rtl LOCK_^^[ADC,SBB,BTS,BTC,BTR,ROL,ROR,XCHG]^^Q =
392 map (lockarith qword) [adc,sbb,bts,btc,btr,rol,ror,xchg]
393 rtl LOCK_^^[DEC,INC,NEG,NOT]^^L =
394 map (lockunary long) [dec,inc,(~),notb]
395 rtl LOCK_^^[DEC,INC,NEG,NOT]^^Q =
396 map (lockunary qword) [dec,inc,(~),notb]
397 rtl LOCK_^^[CMPXCHG]^^B = map (lockarith byte) [cmpxchg]
398 rtl LOCK_^^[CMPXCHG]^^W = map (lockarith word) [cmpxchg]
399 rtl LOCK_^^[CMPXCHG]^^L = map (lockarith long) [cmpxchg]
400 rtl LOCK_^^[CMPXCHG]^^Q = map (lockarith qword) [cmpxchg]
401
402 (* Multiplication/division *)
403 rtl upperMultiply : #n bits * #n bits -> #n bits
404 rtl MULL1{src} = eax := muls(eax, src) ||
405 edx := upperMultiply(eax, src) ||
406 $eflags[0] := ???
407 rtl IDIVL1{src} = eax := divs(eax, src) ||
408 edx := rems(eax, src) ||
409 $eflags[0] := ???
410 rtl DIVL1{src} = edx := divu(eax, src) ||
411 edx := remu(eax, src) ||
412 $eflags[0] := ???
413
414 (* test[b,w,l] *)
415 rtl TESTB {lsrc,rsrc} = $eflags[0] := setFlag(andb(byte lsrc, rsrc))
416 rtl TESTW {lsrc,rsrc} = $eflags[0] := setFlag(andb(word lsrc, rsrc))
417 rtl TESTL {lsrc,rsrc} = $eflags[0] := setFlag(andb(long lsrc, rsrc))
418 rtl TESTQ {lsrc,rsrc} = $eflags[0] := setFlag(andb(qword lsrc, rsrc))
419
420 (* setcc *)
421 fun set cc {opnd} = opnd := byte(cond(cc, 0xff, 0x0))
422 rtl SET^^ [EQ,NE,LT,LE,GT,GE,B,BE,A,AE,C,NC,P,NP,O,NO] =
423 map set [E ,NE,L, LE,G ,GE,B,BE,A,AE,C,NC,P,NP,O,NO]
424
425 (* conditional move *)
426 fun cmov cc {src,dst} = if cc then $r[dst] := long src else ()
427 rtl CMOV^^ [EQ,NE,LT,LE,GT,GE,B,BE,A,AE,C,NC,P,NP,O,NO] =
428 map cmov [E ,NE,L, LE,G ,GE,B,BE,A,AE,C,NC,P,NP,O,NO]
429
430 (* push and pops *)
431 rtl PUSHQ {operand} = $m[rsp - 8] := qword(operand) || rsp := rsp - 8
432 rtl POP {operand} = operand := long($m[esp]) || esp := esp + 4
433
434 (* semantics of branches and jumps *)
435 rtl JMP{operand} = Jmp(long operand)
436 fun jcc cc {opnd} = if cc then Jmp(long opnd) else ()
437 rtl J^^ [EQ,NE,LT,LE,GT,GE,B,BE,A,AE,C,NC,P,NP,O,NO] =
438 map jcc [E ,NE,L, LE,G ,GE,B,BE,A,AE,C,NC,P,NP,O,NO]
439 rtl CALL{opnd,defs,uses} =
440 Call(long opnd) ||
441 Kill $cellset[defs] ||
442 Use $cellset[uses]
443
444
445 end (* RTL *)
446
447 (*------------------------------------------------------------------------
448 * Machine Instruction encoding on the x86-64
449 * Because of variable width instructions.
450 * We decompose each byte field into a seperate format first, then combine
451 * then to form the real instructions
452 *------------------------------------------------------------------------*)
453 instruction formats 8 bits
454 modrm{mod:2, reg:3, rm:3}
455 | reg{opc:5, reg:3}
456 | sib{ss:2, index:3, base:3}
457 | immed8{imm:8}
458
459 instruction formats 32 bits
460 immed32{imm:32}
461
462 (*
463 * Variable format instructions
464 *)
465 instruction formats
466 immedOpnd{opnd} =
467 (case opnd of
468 I.Immed i32 => i32
469 | I.Immed i64 => i64
470 | I.ImmedLabel le => lexp le
471 | I.LabelEA le => lexp le
472 | _ => error "immedOpnd"
473 )
474 | extension{opc, opnd} = (* generate an extension *)
475 (case opnd of
476 I.Direct (_, r) => modrm{mod=3, reg=opc, rm=r}
477 | I.FDirect _ => raise Fail "todo"
478 | I.Displace{base, disp, ...} =>
479 let val immed = immedOpnd{opnd=disp}
480 in () (* XXX *)
481 end
482 | I.Indexed{base=NONE, index, scale, disp, ...} => ()
483 | I.Indexed{base=SOME b, index, scale, disp, ...} => ()
484 | _ => error "immedExt"
485 )
486
487 instruction formats 16 bits
488 encodeST{prefix:8, opc:5, st: $FP 3}
489
490 instruction formats
491 encodeReg{prefix:8, reg: $GP 3, opnd} =
492 (emit prefix; immedExt{opc=reg, opnd=opnd})
493 | arith{opc1,opc2,src,dst} =
494 (case (src, dst) of
495 (I.ImmedLabel le, dst) => arith{opc1,opc2,src=I.Immed(lexp le),dst}
496 | (I.LabelEA le, dst) => arith{opc1,opc2,src=I.Immed(lexp le),dst}
497 | (I.Immed i,dst) => ()
498 | (I.Immed64 i,dst) => ()
499 | (src, I.Direct (_,r)) => encodeReg{prefix=opc1+op3,reg,opnd=src}
500 | (I.Direct (_,r),dst) => encodeReg{prefix=opc1+0w1,reg,opnd=dst}
501 | _ => error "arith"
502 )
503
504 structure Assembly =
505 struct
506 fun emitInt32 i = let
507 val s = Int32.toString i
508 val s = if i >= 0 then s else "-"^String.substring(s,1,size s-1)
509 in
510 emit s
511 end
512 fun emitInt64 i = let
513 val s = Int64.toString i
514 val s = if (i >= 0) then s else "-"^String.substring(s,1,size s-1)
515 in
516 emit s
517 end
518
519 val {low=SToffset, ...} = C.cellRange CellsBasis.FP
520
521 fun emitScale 0 = emit "1"
522 | emitScale 1 = emit "2"
523 | emitScale 2 = emit "4"
524 | emitScale 3 = emit "8"
525 | emitScale _ = error "emitScale"
526
527 and eImmed(I.Immed i) = emitInt32 i
528 | eImmed(I.Immed64 i) = emitInt64 i
529 | eImmed(I.ImmedLabel lexp) = emit_labexp lexp
530 | eImmed _ = error "eImmed"
531
532 and emit_operand opn = (case opn
533 of I.Immed i => (emit "$"; emitInt32 i)
534 | I.Immed64 i => (emit "$"; emitInt64 i)
535 | I.ImmedLabel lexp => (emit "$"; emit_labexp lexp)
536 | I.LabelEA le => emit_labexp le
537 | I.Relative _ => error "emit_operand"
538 | I.Direct (ty, r) => emit(CellsBasis.toStringWithSize(r,ty))
539 | I.FDirect f => emit(CellsBasis.toString f)
540 | I.Displace{base,disp,mem,...} =>
541 (emit_disp disp; emit "("; emitCell base; emit ")";
542 emit_region mem)
543 | I.Indexed{base,index,scale,disp,mem,...} =>
544 (emit_disp disp; emit "(";
545 case base of
546 NONE => ()
547 | SOME base => emitCell base;
548 comma();
549 emitCell index; comma();
550 emitScale scale; emit ")"; emit_region mem)
551 (* end case *))
552
553 and emit_operand8(I.Direct (_, r)) =
554 emit(CellsBasis.toStringWithSize(r,8))
555 | emit_operand8 opn = emit_operand opn
556
557 and emit_cell (r, sz) = emit (CellsBasis.toStringWithSize(r,sz))
558
559 and emit_disp(I.Immed 0) = ()
560 | emit_disp(I.Immed i) = emitInt32 i
561 | emit_disp(I.Immed64 0) = ()
562 | emit_disp(I.Immed64 i) = emitInt64 i
563 | emit_disp(I.ImmedLabel lexp) = emit_labexp lexp
564 | emit_disp _ = error "emit_disp"
565
566 (* The gas assembler does not like the "$" prefix for immediate
567 * labels in certain instructions.
568 *)
569 fun stupidGas(I.ImmedLabel lexp) = emit_labexp lexp
570 | stupidGas opnd = (emit "*"; emit_operand opnd)
571
572 (* Display the floating point binary opcode *)
573 fun isMemOpnd(I.FDirect f) = true
574 | isMemOpnd(I.LabelEA _) = true
575 | isMemOpnd(I.Displace _) = true
576 | isMemOpnd(I.Indexed _) = true
577 | isMemOpnd _ = false
578 fun chop fbinOp =
579 let val n = size fbinOp
580 in case Char.toLower(String.sub(fbinOp,n-1)) of
581 (#"s" | #"l") => String.substring(fbinOp,0,n-1)
582 | _ => fbinOp
583 end
584
585 val emit_dst = emit_operand
586 val emit_src = emit_operand
587 val emit_opnd = emit_operand
588 val emit_opnd8 = emit_operand8
589 val emit_rsrc = emit_operand
590 val emit_lsrc = emit_operand
591 val emit_addr = emit_operand
592 val emit_src1 = emit_operand
593 val emit_ea = emit_operand
594 val emit_count = emit_operand
595 fun emit_byte b = (emit "$0x"; emit(Word8.toString b))
596 end (* Assembly *)
597
598
599 (*------------------------------------------------------------------------
600 *
601 * Reservation tables and pipeline definitions for scheduling.
602 * Faked for now as I don't have to time to look up the definitions
603 * from the Intel doc.
604 *
605 *------------------------------------------------------------------------*)
606
607 (* Function units *)
608 resource issue and mem and alu and falu and fmul and fdiv and branch
609
610 (* Different implementations of cpus *)
611 cpu default 2 [2 issue, 2 mem, 1 alu, 1 falu, 1 fmul] (* 2 issue machine *)
612
613 (* Definitions of various reservation tables *)
614 pipeline NOP _ = [issue]
615 and ARITH _ = [issue^^alu]
616 and LOAD _ = [issue^^mem]
617 and STORE _ = [issue^^mem,mem,mem]
618 and BRANCH _ = [issue^^branch]
619
620 (*------------------------------------------------------------------------
621 *
622 * Compiler representation of the instruction set.
623 *
624 *------------------------------------------------------------------------*)
625 instruction
626 NOP
627 asm: ``nop''
628 rtl: ``NOP''
629
630 | JMP of operand * Label.label list
631 asm: ``jmp\t<stupidGas operand>''
632 rtl: ``JMP''
633
634 | JCC of {cond:cond, opnd:operand}
635 asm: ``j<cond>\t<stupidGas opnd>''
636 rtl: ``J<cond>''
637
638 | CALL of {opnd: operand, defs: $cellset, uses: $cellset,
639 return: $cellset, cutsTo: Label.label list, mem: Region.region,
640 pops:Int32.int}
641 asm: ``call\t<stupidGas opnd><mem><
642 emit_defs(defs)><
643 emit_uses(uses)><
644 emit_cellset("return",return)><
645 emit_cutsTo cutsTo>''
646 rtl: ``CALL''
647
648 | ENTER of {src1:operand, src2:operand}
649 asm: ``enter\t<emit_operand src1>, <emit_operand src2>''
650
651 | LEAVE
652 asm: ``leave''
653
654 | RET of operand option
655 asm: ``ret<case option of NONE => ()
656 | SOME e => (emit "\t"; emit_operand e)>''
657
658 | MOVE of {mvOp:move, src:operand, dst:operand}
659 asm: ``<mvOp>\t<src>, <dst>''
660 rtl: ``<mvOp>''
661
662 | LEAL of {r32: $GP, addr: operand}
663 asm: ``leal\t<addr>, <emit_cell (r32, 32)>''
664 rtl: ``LEAL''
665
666 | LEAQ of {r64: $GP, addr: operand}
667 asm: ``leaq\t<addr>, <emit_cell (r64, 64)>''
668 rtl: ``LEAQ''
669
670 | CMPQ of {lsrc: operand, rsrc: operand}
671 asm: ``cmpq\t<rsrc>, <lsrc>''
672
673 | CMPL of {lsrc: operand, rsrc: operand}
674 asm: ``cmpl\t<rsrc>, <lsrc>''
675
676 | CMPW of {lsrc: operand, rsrc: operand}
677 ``cmpb\t<rsrc>, <lsrc>''
678
679 | CMPB of {lsrc: operand, rsrc: operand}
680 ``cmpb\t<rsrc>, <lsrc>''
681
682 | TESTQ of {lsrc: operand, rsrc: operand}
683 asm: ``testq\t<rsrc>, <lsrc>''
684 rtl: ``TESTQ''
685
686 | TESTL of {lsrc: operand, rsrc: operand}
687 asm: ``testl\t<rsrc>, <lsrc>''
688 rtl: ``TESTL''
689
690 | TESTW of {lsrc: operand, rsrc: operand}
691 asm: ``testw\t<rsrc>, <lsrc>''
692 rtl: ``TESTW''
693
694 | TESTB of {lsrc: operand, rsrc: operand}
695 asm: ``testb\t<rsrc>, <lsrc>''
696 rtl: ``TESTB''
697
698 | BITOP of {bitOp:bitOp, lsrc: operand, rsrc: operand}
699 ``<bitOp>\t<rsrc>, <lsrc>''
700
701 | BINARY of {binOp:binaryOp, src:operand, dst:operand}
702 asm: (case (src,binOp) of
703 (I.Direct _, (* tricky business here for shifts *)
704 (I.SARQ | I.SHRQ | I.SHLQ |
705 I.SARL | I.SHRL | I.SHLL |
706 I.SARW | I.SHRW | I.SHLW |
707 I.SARB | I.SHRB | I.SHLB)) => ``<binOp>\t%cl, <dst>''
708 | _ => ``<binOp>\t<src>, <dst>''
709 )
710 (*rtl: ``<binOp>''*)
711 | SHIFT of {shiftOp:shiftOp, src:operand, dst:operand, count:operand}
712 asm: (case count of (* must be %ecx if it is a register *)
713 I.Direct (ty, ecx) => ``<shiftOp>\t<src>, <dst>''
714 | _ => ``<shiftOp>\t<src>, <count>, <dst>''
715 )
716
717 | MULTDIV of {multDivOp:multDivOp, src:operand}
718 asm: ``<multDivOp>\t<src>''
719
720 | MUL3 of {dst: $GP, src2: Int32.int, src1:operand}
721 (* Fermin: constant operand must go first *)
722 asm: ``imull\t$<emitInt32 src2>, <src1>, <emit_cell (dst, 32)>''
723
724 | MULQ3 of {dst: $GP, src2: Int32.int, src1:operand}
725 (* Fermin: constant operand must go first *)
726 asm: ``imulq\t$<emitInt32 src2>, <src1>, <emit_cell (dst, 64)>''
727
728 | UNARY of {unOp:unaryOp, opnd:operand}
729 asm: ``<unOp>\t<opnd>''
730 rtl: ``<unOp>''
731
732 (* set byte on condition code; note that
733 * this only sets the low order byte, so it also
734 * uses its operand.
735 *)
736 | SET of {cond:cond, opnd:operand}
737 asm: ``set<cond>\t<emit_opnd8 opnd>''
738 rtl: ``SET<cond>''
739
740 | CMOV of {cond:cond, src:operand, dst: $GP}
741 asm: ``cmov<cond>\t<src>, <dst>''
742 rtl: ``CMOV<cond>''
743
744 | PUSH of operand
745 asm: ``pushq\t<operand>''
746 rtl: ``PUSHQ''
747
748 | PUSHFQ (* push $rflags onto stack *)
749 ``pushfq''
750
751 | POPFQ (* pop $rflags onto stack *)
752 ``popfq''
753
754 | POP of operand
755 asm: ``popq\t<operand>''
756 rtl: ``POP''
757
758 (* also known as CLTD *)
759 | CDQ
760 ``cdq''
761
762 (* also known as CQTO *)
763 | CDO
764 ``cdo''
765
766 (* interrupt; use 0w4 to signal overflow *)
767 | INT of byte
768 ``int\t<byte>''
769
770 (* floating-point operations (SSE scalar instructions) *)
771 | FMOVE of {fmvOp : fmove_op, dst : operand, src : operand}
772 ``<fmvOp>\t <src>, <dst>''
773
774 | FBINOP of {binOp : fbin_op, dst : $FP, src : operand}
775 ``<binOp>\t <src>, <dst>''
776
777 | FCOM of {comOp : fcom_op, dst : $FP, src : operand}
778 ``<comOp>\t <src>, <dst>''
779
780 | FSQRTS of {dst : operand, src : operand}
781 ``sqrtss\t <src>, <dst>''
782
783 | FSQRTD of {dst : operand, src : operand}
784 ``sqrtsd\t <src>, <dst>''
785
786 (* misc *)
787 | SAHF (* %flags -> %ah *)
788 ``sahf''
789
790 (*** concurrency operations ****)
791
792 (* performs a serializing operation on all load-to-memory operations issued prior to
793 * the lfence instruction.
794 *)
795 | LFENCE
796 asm: ``lfence''
797 rtl: ``LFENCE''
798
799 (* performs a serializing operation on all load-from-memory and store-to-memory
800 * operations issued prior to the mfence instruction.
801 *)
802 | MFENCE
803 asm: ``mfence''
804 rtl: ``MFENCE''
805
806 (* performs a serializing operation on all store-to-memory operations issued prior to
807 * the sfence instruction.
808 *)
809 | SFENCE
810 asm: ``sfence''
811 rtl: ``SFENCE''
812
813 (* improves performance of spin-wait loops *)
814 | PAUSE
815 asm: ``pause''
816 rtl: ``PAUSE''
817
818 | XCHG of {lock:bool, sz:isize, src: operand, dst:operand}
819 asm: (if lock then ``lock\n\t'' else ();
820 ``xchg'';
821 case sz of
822 I.I8 => ``b''
823 | I.I16 => ``w''
824 | I.I32 => ``l''
825 | I.I64 => ``q'';
826 ``\t<src>, <dst>''
827 )
828
829 | CMPXCHG of {lock:bool, sz:isize, src: operand, dst:operand}
830 asm: (if lock then ``lock\n\t'' else ();
831 ``cmpxchg'';
832 case sz of
833 I.I8 => ``b''
834 | I.I16 => ``w''
835 | I.I32 => ``l''
836 | I.I64 => ``q'';
837 ``\t<src>, <dst>''
838 )
839
840 | XADD of {lock:bool, sz:isize, src:operand, dst:operand}
841 asm: (if lock then ``lock\n\t'' else ();
842 ``xadd'';
843 case sz of
844 I.I8 => ``b''
845 | I.I16 => ``w''
846 | I.I32 => ``l''
847 | I.I64 => ``q'';
848 ``\t<src>, <dst>''
849 )
850
851 | RDTSC
852 asm: ``rdtsc''
853 | RDTSCP
854 asm: ``rdtscp''
855
856 | LAHF (* %ah -> %flags *)
857 ``lahf''
858
859 | SOURCE of {}
860 asm: ``source''
861 mc: ()
862
863 | SINK of {}
864 asm: ``sink''
865 mc: ()
866
867 | PHI of {}
868 asm: ``phi''
869 mc: ()
870
871 end
872