Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/instructions/amd64Props.sml

raw
1 (* amd64Props.sml
2 *
3 * This functor encodes semantic information of instructions.
4 *
5 *)
6
7 signature AMD64INSN_PROPERTIES =
8 sig
9 include INSN_PROPERTIES
10 (* returns the bit width of an instruction's source operand *)
11 val szOfInstr : I.instr -> int
12 (* returns the bit width of an floating-point instruction's source operand *)
13 val szOfFinstr : I.instr -> int
14 end
15
16 functor AMD64Props (
17 structure Instr : AMD64INSTR
18 structure MLTreeHash : MLTREE_HASH (* where T = Instr.T *)
19 where type T.Basis.cond = Instr.T.Basis.cond
20 and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
21 and type T.Basis.ext = Instr.T.Basis.ext
22 and type T.Basis.fcond = Instr.T.Basis.fcond
23 and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
24 and type T.Constant.const = Instr.T.Constant.const
25 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
26 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
27 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
28 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
29 and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
30 and type T.Region.region = Instr.T.Region.region
31 and type T.ccexp = Instr.T.ccexp
32 and type T.fexp = Instr.T.fexp
33 (* and type T.labexp = Instr.T.labexp *)
34 and type T.mlrisc = Instr.T.mlrisc
35 and type T.oper = Instr.T.oper
36 and type T.rep = Instr.T.rep
37 and type T.rexp = Instr.T.rexp
38 and type T.stm = Instr.T.stm
39 structure MLTreeEval : MLTREE_EVAL (* where T = Instr.T *)
40 where type T.Basis.cond = Instr.T.Basis.cond
41 and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
42 and type T.Basis.ext = Instr.T.Basis.ext
43 and type T.Basis.fcond = Instr.T.Basis.fcond
44 and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
45 and type T.Constant.const = Instr.T.Constant.const
46 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
47 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
48 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
49 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
50 and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
51 and type T.Region.region = Instr.T.Region.region
52 and type T.ccexp = Instr.T.ccexp
53 and type T.fexp = Instr.T.fexp
54 (* and type T.labexp = Instr.T.labexp *)
55 and type T.mlrisc = Instr.T.mlrisc
56 and type T.oper = Instr.T.oper
57 and type T.rep = Instr.T.rep
58 and type T.rexp = Instr.T.rexp
59 and type T.stm = Instr.T.stm
60 ) : AMD64INSN_PROPERTIES =
61 struct
62
63 structure I = Instr
64 structure C = I.C
65 structure T = I.T
66 structure CB = CellsBasis
67
68 exception NegateConditional
69
70 fun error msg = MLRiscErrorMsg.error("AMD64Props",msg)
71
72 datatype kind = IK_JUMP | IK_NOP | IK_INSTR | IK_COPY | IK_CALL
73 | IK_CALL_WITH_CUTS | IK_PHI | IK_SOURCE | IK_SINK
74 datatype target = LABELLED of Label.label | FALLTHROUGH | ESCAPES
75
76 fun instrKind (I.ANNOTATION {i, ...}) = instrKind i
77 | instrKind (I.COPY _) = IK_COPY
78 | instrKind (I.INSTR instr) = (case instr
79 of I.NOP => IK_NOP
80 | I.CALL {cutsTo=_::_, ...} => IK_CALL_WITH_CUTS
81 | ( I.JMP _ | I.JCC _ | I.RET _ | I.INT _ ) => IK_JUMP
82 | I.CALL _ => IK_CALL
83 | I.PHI {} => IK_PHI
84 | I.SOURCE {} => IK_SOURCE
85 | I.SINK {} => IK_SINK
86 | _ => IK_INSTR
87 (* end case *))
88 | instrKind _ = IK_INSTR
89
90 fun moveInstr (I.ANNOTATION {i, ...}) = moveInstr i
91 | moveInstr (I.COPY _) = true
92 | moveInstr _ = false
93
94 fun moveTmpR (I.ANNOTATION {i, ...}) = moveTmpR i
95 | moveTmpR ( I.COPY {k=CB.GP, tmp=SOME (I.Direct (_, r)), ...}
96 | I.COPY {k=CB.FP, tmp=SOME (I.FDirect r), ...} ) =
97 SOME r
98 | moveTmpR _ = NONE
99
100 fun moveDstSrc (I.ANNOTATION {i, ...}) = moveDstSrc i
101 | moveDstSrc (I.COPY {dst, src, ...}) = (dst, src)
102 | moveDstSrc _ = error "AMD64Props.moveDstSrc"
103
104 fun nop () = I.nop
105
106 fun jump l = I.jmp (I.ImmedLabel (T.LABEL l), [l])
107
108 fun branchTargets(I.ANNOTATION{i,...}) = branchTargets i
109 | branchTargets(I.INSTR i) = (case i
110 of I.JMP(_, []) => [ESCAPES]
111 | I.JMP(_, labs) => List.map LABELLED labs
112 | I.RET _ => [ESCAPES]
113 | I.JCC{opnd=I.ImmedLabel(T.LABEL(lab)), ...} =>
114 [FALLTHROUGH, LABELLED lab]
115 | I.CALL{cutsTo, ...} => FALLTHROUGH :: List.map LABELLED cutsTo
116 | I.INT _ => [ESCAPES]
117 | _ => error "branchTargets")
118 | branchTargets _ = error "branchTargets"
119
120 fun jump label = I.jmp (I.ImmedLabel(T.LABEL label), [label])
121
122 fun setJumpTarget(I.ANNOTATION {a,i}, l) =
123 I.ANNOTATION {a=a, i=setJumpTarget (i, l)}
124 | setJumpTarget(I.INSTR (I.JMP (I.ImmedLabel _, _)), lab) = jump lab
125 | setJumpTarget _ = error "setJumpTarget"
126
127 fun setBranchTargets{i=I.ANNOTATION{a,i}, t, f} =
128 I.ANNOTATION{a=a, i=setBranchTargets{i=i, t=t, f=f}}
129 | setBranchTargets{i=I.INSTR(I.JCC{cond,opnd=I.ImmedLabel _}), t, ...} =
130 I.jcc{cond=cond,opnd=I.ImmedLabel(T.LABEL t)}
131 | setBranchTargets _ = error "setBranchTargets"
132
133 val immedRange={lo= ~1073741824, hi=1073741823}
134 val toInt32 = Int32.fromLarge o Int.toLarge
135 (* immediates are restricted to 32 bits *)
136 fun loadImmed {immed, t} =
137 I.move {mvOp=I.MOVL, src=I.Immed (toInt32 immed), dst=I.Direct (32, t)}
138 fun loadOperand {opn, t} = I.move {mvOp=I.MOVQ, src=opn, dst=I.Direct (64, t)}
139
140 fun hashOpn(I.Immed i) = Word.fromInt(Int32.toInt i)
141 | hashOpn(I.Immed64 i) = Word.fromInt(Int64.toInt i)
142 | hashOpn(I.ImmedLabel le) = MLTreeHash.hash le + 0w123
143 | hashOpn(I.Relative i) = Word.fromInt i + 0w1232
144 | hashOpn(I.LabelEA le) = MLTreeHash.hash le + 0w44444
145 | hashOpn(I.Direct (_, r)) = CB.hashCell r
146 | hashOpn(I.FDirect f) = CB.hashCell f + 0w31245
147 | hashOpn(I.Displace {base, disp, ...}) =
148 hashOpn disp + CB.hashCell base
149 | hashOpn(I.Indexed {base, index, scale, disp, ...}) =
150 CB.hashCell index + Word.fromInt scale + hashOpn disp
151 fun eqOpn(I.Immed a,I.Immed b) = a = b
152 | eqOpn(I.Immed64 a,I.Immed64 b) = a = b
153 | eqOpn(I.ImmedLabel a,I.ImmedLabel b) = MLTreeEval.==(a,b)
154 | eqOpn(I.Relative a,I.Relative b) = a = b
155 | eqOpn(I.LabelEA a,I.LabelEA b) = MLTreeEval.==(a,b)
156 | eqOpn(I.Direct (_,a),I.Direct (_,b)) = CB.sameColor(a,b)
157 | eqOpn(I.FDirect a,I.FDirect b) = CB.sameColor(a,b)
158 | eqOpn(I.Displace{base=a,disp=b,...},I.Displace{base=c,disp=d,...}) =
159 CB.sameColor(a,c) andalso eqOpn(b,d)
160 | eqOpn(I.Indexed{base=a,index=b,scale=c,disp=d,...},
161 I.Indexed{base=e,index=f,scale=g,disp=h,...}) =
162 CB.sameColor(b,f) andalso c = g
163 andalso sameCellOption(a,e) andalso eqOpn(d,h)
164 | eqOpn _ = false
165 and sameCellOption(NONE, NONE) = true
166 | sameCellOption(SOME x, SOME y) = CB.sameColor(x,y)
167 | sameCellOption _ = false
168
169 fun negateConditional (I.ANNOTATION{i,a}, lab) =
170 I.ANNOTATION{i=negateConditional(i,lab), a=a}
171 | negateConditional (I.INSTR(I.JCC{cond,
172 opnd=I.ImmedLabel(T.LABEL _)}), lab) = let
173 val cond' = (case cond
174 of I.EQ => I.NE
175 | I.NE => I.EQ
176 | I.LT => I.GE
177 | I.LE => I.GT
178 | I.GT => I.LE
179 | I.GE => I.LT
180 | I.B => I.AE
181 | I.BE => I.A
182 | I.A => I.BE
183 | I.AE => I.B
184 | I.C => I.NC
185 | I.NC => I.C
186 | I.P => I.NP
187 | I.NP => I.P
188 | I.O => I.NO
189 | I.NO => I.O
190 (* end case *))
191 in
192 I.INSTR(I.JCC{cond=cond', opnd=I.ImmedLabel(T.LABEL lab)})
193 end
194 | negateConditional _ = error "AMD64Props.negateConditional"
195
196 val raxPair = [C.rdx, C.rax]
197
198 fun defUseR i = let
199 fun operandAcc (opnd, acc) = (case opnd
200 of I.Direct (_, r) => r :: acc
201 | I.Displace {base, ...} => base :: acc
202 | I.Indexed {base=NONE, index, ...} => index :: acc
203 | I.Indexed {base=SOME b, index, ...} => b :: index :: acc
204 | _ => acc
205 (* end case *))
206 fun operandUse opnd = operandAcc (opnd, [])
207 fun operandDef (I.Direct (_, r)) = [r]
208 | operandDef _ = []
209 fun cmpTest {lsrc, rsrc} = ([], operandAcc (lsrc, operandUse rsrc))
210 fun unary opnd = (operandDef opnd, operandUse opnd)
211 fun multDiv {src, multDivOp} = let
212 val uses = operandUse src
213 in
214 case multDivOp
215 of (I.IDIVL1 | I.DIVL1 | I.IDIVQ1 | I.DIVQ1) =>
216 (raxPair, C.rdx::C.rax::uses)
217 | (I.IMULL1 | I.MULL1 | I.IMULQ1 | I.MULQ1) =>
218 (raxPair, C.rax::uses)
219 (* end case *)
220 end
221 fun rspOnly () = let val s = [C.stackptrR] in (s, s) end
222 fun push opnd = ([C.stackptrR], operandAcc (opnd, [C.stackptrR]))
223 fun f i = (case i
224 of ( I.JMP (opnd, _) | I.JCC {opnd, ...} ) => ([], operandUse opnd)
225 | I.CALL {opnd, defs, uses, ...} =>
226 (C.getReg defs, operandAcc (opnd, C.getReg uses))
227 | I.MOVE {src, dst=I.Direct (_, r), ...} => ([r], operandUse src)
228 | I.MOVE {src, dst, ...} => ([], operandAcc (dst, operandUse src))
229 | ( I.LEAL {r32=r, addr} | I.LEAQ {r64=r, addr} ) =>
230 ([r], operandUse addr)
231 | ( I.CMPQ arg | I.CMPL arg | I.CMPW arg | I.CMPB arg
232 | I.TESTQ arg | I.TESTL arg | I.TESTW arg | I.TESTB arg ) =>
233 cmpTest arg
234 | I.BITOP{lsrc, rsrc, ...} => cmpTest {lsrc=lsrc,rsrc=rsrc}
235 | I.BINARY{binOp=I.XORL, src=I.Direct (_,rs),
236 dst=I.Direct (_,rd),...} =>
237 if CB.sameColor(rs,rd) then ([rd],[]) else ([rd],[rs,rd])
238 | I.BINARY{binOp=I.XORQ, src=I.Direct (_,rs),
239 dst=I.Direct (_,rd),...} =>
240 if CB.sameColor(rs,rd) then ([rd],[]) else ([rd],[rs,rd])
241 | I.BINARY {src, dst,...} =>
242 (operandDef dst, operandAcc (src, operandUse dst))
243 | I.SHIFT {src,dst,count,...} =>
244 (operandDef dst,
245 operandAcc(count, operandAcc (src, operandUse dst)))
246 | I.XADD {src, dst, ...} =>
247 (operandAcc (src, operandDef dst), operandAcc (src, operandUse dst))
248 | I.CMPXCHG {src, dst, ...} =>
249 (C.rax::operandDef dst, C.rax::operandAcc (src, operandUse dst))
250 | I.XCHG {src, dst, ...} =>
251 (operandDef dst, operandAcc (src, operandUse dst))
252 | ( I.ENTER _ | I.LEAVE ) => ([C.rsp, C.rbp], [C.rsp, C.rbp])
253 | I.MULTDIV arg => multDiv arg
254 | ( I.MUL3 {src1, dst, ...} | I.MULQ3 {src1, dst, ...} ) =>
255 ([dst], operandUse src1)
256 | ( I.UNARY{opnd, ...} | I.SET {opnd, ...} ) => unary opnd
257 | I.PUSH arg => push arg
258 | I.POP arg => (C.stackptrR::operandDef arg, [C.stackptrR])
259 | ( I.PUSHFQ | I.POPFQ )=> rspOnly ()
260 | I.CDQ => ([C.rdx], [C.rax]) (* really %edx and %eax *)
261 | I.CDO => ([C.rdx], [C.rax])
262 | I.FMOVE {dst, src, ...} => ([], operandAcc (dst, operandUse src))
263 | I.FCOM {src, ...} => ([], operandUse src)
264 | I.FBINOP {src, ...} => ([], operandUse src)
265 | I.SAHF => ([], [C.rax])
266 | I.LAHF => ([C.rax], [])
267 (* This sets the low order byte,
268 * do potentially it may define *and* use
269 *)
270 | I.CMOV {src, dst,...} => ([dst], operandAcc(src, [dst]))
271 | I.RDTSC => ([C.rax,C.rdx], [])
272 | I.RDTSCP => ([C.rax,C.rdx,C.rcx], [])
273 | _ => ([], [])
274 (* end case *))
275 in
276 case i
277 of I.ANNOTATION {i, ...} => defUseR i
278 | I.LIVE {regs, ...} => ([], C.getReg regs)
279 | I.KILL {regs, ...} => (C.getReg regs, [])
280 | I.COPY {k=CB.GP, src, dst, tmp, ...} => (case tmp
281 of SOME (I.Direct (_, t)) => (t :: dst, src)
282 | SOME ea => (dst, operandAcc (ea, src))
283 | NONE => (dst, src)
284 (* end case *))
285 | I.COPY _ => ([], [])
286 | I.INSTR i => f i
287 (* end case *)
288 end (* defUseR *)
289
290 fun defUseF i = let
291 fun operandAcc (I.FDirect r, acc) = r :: acc
292 | operandAcc (_, acc) = acc
293 fun operand opnd = operandAcc (opnd, [])
294 fun f i = (case i
295 of I.FMOVE {dst, src, ...} => (operand dst, operand src)
296 | I.FBINOP {dst, src, ...} => ([dst], dst :: operand src)
297 | I.FCOM {dst, src, ...} => ([], operandAcc (src, [dst]))
298 | ( I.FSQRTS {dst, src} | I.FSQRTD {dst, src} )=>
299 (operand dst, operand src)
300 | I.CALL {defs, uses, ...} =>
301 (C.getFreg defs, C.getFreg uses)
302 | _ => ([], [])
303 (* end case *))
304 in
305 case i
306 of I.ANNOTATION {i, ...} => defUseF i
307 | I.LIVE {regs, ...} => ([], C.getFreg regs)
308 | I.KILL {regs, ...} => (C.getFreg regs, [])
309 | I.COPY {k=CB.FP, src, dst, tmp, ...} => (case tmp
310 of SOME (I.FDirect t) => (t :: dst, src)
311 | NONE => (dst, src)
312 (* end case *))
313 | I.COPY _ => ([], [])
314 | I.INSTR i => f i
315 (* end case *)
316 end (* defUseF *)
317
318 fun defUse CB.GP = defUseR
319 | defUse CB.FP = defUseF
320 | defUse _ = error "defUse"
321
322 fun getAnnotations (I.ANNOTATION {i, a}) = let
323 val (i, an) = getAnnotations i
324 in
325 (i, a::an)
326 end
327 | getAnnotations i = (i,[])
328
329 fun annotate (i, a) = I.ANNOTATION {i=i, a=a}
330
331 fun szToInt I.I8 = 8
332 | szToInt I.I16 = 16
333 | szToInt I.I32 = 32
334 | szToInt I.I64 = 64
335
336 fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a}
337 (* | replicate(I.COPY{tmp=SOME _, dst, src}) =
338 I.COPY{tmp=SOME(I.Direct(C.newReg())), dst=dst, src=src}
339 | replicate(I.FCOPY{tmp=SOME _, dst, src}) =
340 I.FCOPY{tmp=SOME(I.FDirect(C.newFreg())), dst=dst, src=src} *)
341 | replicate i = i
342
343 (* determine the bit width of an instruction *)
344 fun szOfInstr instr = (case instr
345 of I.JCC _ => 32
346 (* NOTE: CMOV encodes operand length in its operands! *)
347 | I.CMOV {src=I.Direct (sz, _), ...} => sz
348 | I.MOVE {mvOp, ...} =>
349 (case mvOp
350 of ( I.MOVQ | I.MOVSWQ | I.MOVZWQ | I.MOVSBQ |
351 I.MOVZBQ | I.MOVSLQ |
352 I.CVTSD2SIQ | I.CVTSS2SIQ
353 ) => 64
354 | ( I.MOVL | I.MOVSWL | I.MOVZWL | I.MOVSBL |
355 I.CVTSD2SI | I.CVTSS2SI |
356 I.MOVZBL ) => 32
357 | I.MOVW => 16
358 | I.MOVB => 8
359 (* esac *))
360 | ( I.LEAL _ | I.CMPL _ | I.TESTL _ | I.MUL3 _ )
361 => 32
362 | ( I.CALL _ | I.LEAQ _ | I.CMPQ _ | I.TESTQ _ | I.MULQ3 _ | I.CMOV _)
363 => 64
364 | ( I.CMPW _ | I.TESTW _ ) => 16
365 | ( I.CMPB _ | I.TESTB _ ) => 8
366 | I.SHIFT {shiftOp, ...} => (case shiftOp
367 of ( I.SHLDL | I.SHRDL ) => 32
368 (* esac *))
369 | I.UNARY {unOp, ...} =>
370 (case unOp
371 of ( I.DECQ | I.INCQ | I.NEGQ | I.NOTQ |
372 I.LOCK_DECQ | I.LOCK_INCQ | I.LOCK_NEGQ | I.LOCK_NOTQ ) => 64
373 | ( I.DECL | I.INCL | I.NEGL | I.NOTL ) => 32
374 | ( I.DECW | I.INCW | I.NEGW | I.NOTW ) => 16
375 | ( I.DECB | I.INCB | I.NEGB | I.NOTB ) => 8
376 (* esac *))
377 | I.MULTDIV {multDivOp, ...} =>
378 (case multDivOp
379 of ( I.IMULL1 | I.MULL1 | I.IDIVL1 | I.DIVL1 ) => 32
380 | ( I.IMULQ1 | I.MULQ1 | I.IDIVQ1 | I.DIVQ1 ) => 64
381 (* esac *))
382 | I.BINARY {binOp, ...} =>
383 (case binOp
384 of ( I.ADDQ | I.SUBQ | I.ANDQ | I.ORQ | I.XORQ | I.SHLQ | I.SARQ
385 | I.SHRQ | I.IMULQ | I.ADCQ | I.SBBQ ) => 64
386 | ( I.ADDL | I.SUBL | I.ANDL | I.ORL | I.XORL | I.SHLL | I.SARL
387 | I.SHRL | I.IMULL | I.ADCL | I.SBBL | I.BTSL | I.BTCL
388 | I.BTRL | I.ROLL | I.RORL | I.XCHGL ) => 32
389 | ( I.ADDW | I.SUBW | I.ANDW | I.ORW | I.XORW | I.SHLW | I.SARW
390 | I.SHRW | I.IMULW | I.BTSW | I.BTCW | I.BTRW | I.ROLW
391 | I.RORW | I.XCHGW ) => 16
392 | ( I.ADDB | I.SUBB | I.ANDB | I.ORB | I.XORB | I.SHLB | I.SARB
393 | I.SHRB | I.IMULB | I.XCHGB ) => 8
394 | _ => raise Fail "" (* 64*)
395 (* esac *))
396 | I.XADD {sz, ...} => szToInt sz
397 | I.CMPXCHG {sz, ...} => szToInt sz
398 | I.PAUSE => 64
399 | I.RDTSC => 64
400 | I.RDTSCP => 64
401 | (I.MFENCE | I.SFENCE | I.LFENCE) => 64
402 | _ => raise Fail "" (*64*)
403 (* esac *))
404
405 fun szOfFinstr instr = (case instr
406 of I.FMOVE {fmvOp, ...} => (case fmvOp
407 of ( I.MOVSS | I.CVTSS2SD | I.CVTSI2SS | I.CVTSI2SSQ ) => 32
408 | ( I.MOVSD | I.CVTSD2SS | I.CVTSI2SD | I.CVTSI2SDQ ) => 64
409 (* end case *))
410 | I.FCOM {comOp, ...} => (case comOp
411 of ( I.COMISS | I.UCOMISS ) => 32
412 | ( I.COMISD | I.UCOMISD ) => 64
413 (* end case *))
414 | I.FBINOP {binOp, ...} => (case binOp
415 of ( I.ADDSS | I.SUBSS | I.MULSS | I.DIVSS | I.XORPS | I.ANDPS | I.ORPS ) => 32
416 | ( I.ADDSD | I.SUBSD | I.MULSD | I.DIVSD | I.XORPD | I.ANDPD | I.ORPD ) => 64
417 (* end case *))
418 | I.FSQRTS _ => 32
419 | I.FSQRTD _ => 64
420 (* end case *))
421
422 end (* AMD64Props *)