Debian Codegraph @

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

raw
1 (*
2 * WARNING: This file was automatically generated by MDLGen (v3.1)
3 * from the machine description file "x86/x86.mdl".
4 * DO NOT EDIT this file directly
5 *)
6
7
8 functor X86MCEmitter(structure Instr : X86INSTR
9 structure MLTreeEval : MLTREE_EVAL where T = Instr.T
10 structure Stream : INSTRUCTION_STREAM
11 structure CodeString : CODE_STRING
12 ) : INSTRUCTION_EMITTER =
13 struct
14 structure I = Instr
15 structure C = I.C
16 structure Constant = I.Constant
17 structure T = I.T
18 structure S = Stream
19 structure P = S.P
20 structure W = Word32
21
22 (* X86 is little endian *)
23
24 fun error msg = MLRiscErrorMsg.error("X86MC",msg)
25 fun makeStream _ =
26 let infix && || << >> ~>>
27 val op << = W.<<
28 val op >> = W.>>
29 val op ~>> = W.~>>
30 val op || = W.orb
31 val op && = W.andb
32 val itow = W.fromInt
33 fun emit_bool false = 0w0 : W.word
34 | emit_bool true = 0w1 : W.word
35 val emit_int = itow
36 fun emit_word w = w
37 fun emit_label l = itow(Label.addrOf l)
38 fun emit_labexp le = itow(MLTreeEval.valueOf le)
39 fun emit_const c = itow(Constant.valueOf c)
40 val w32ToByte = Word8.fromLarge o Word32.toLarge
41 val loc = ref 0
42
43 (* emit a byte *)
44 fun eByte b =
45 let val i = !loc in loc := i + 1; CodeString.update(i,b) end
46
47 (* emit the low order byte of a word *)
48 (* note: fromLargeWord strips the high order bits! *)
49 fun eByteW w =
50 let val i = !loc
51 in loc := i + 1; CodeString.update(i, w32ToByte w) end
52
53 fun doNothing _ = ()
54 fun fail _ = raise Fail "MCEmitter"
55 fun getAnnotations () = error "getAnnotations"
56
57 fun pseudoOp pOp = P.emitValue{pOp=pOp, loc= !loc,emit=eByte}
58
59 fun init n = (CodeString.init n; loc := 0)
60
61
62 fun eWord8 w =
63 let val b8 = w
64 in eByteW b8
65 end
66 and eWord16 w =
67 let val b8 = w
68 val w = w >> 0wx8
69 val b16 = w
70 in
71 ( eByteW b8;
72 eByteW b16 )
73 end
74 and eWord32 w =
75 let val b8 = w
76 val w = w >> 0wx8
77 val b16 = w
78 val w = w >> 0wx8
79 val b24 = w
80 val w = w >> 0wx8
81 val b32 = w
82 in
83 ( eByteW b8;
84 eByteW b16;
85 eByteW b24;
86 eByteW b32 )
87 end
88 fun emit_GP r = itow (CellsBasis.physicalRegisterNum r)
89 and emit_FP r = itow (CellsBasis.physicalRegisterNum r)
90 and emit_CC r = itow (CellsBasis.physicalRegisterNum r)
91 and emit_EFLAGS r = itow (CellsBasis.physicalRegisterNum r)
92 and emit_FFLAGS r = itow (CellsBasis.physicalRegisterNum r)
93 and emit_MEM r = itow (CellsBasis.physicalRegisterNum r)
94 and emit_CTRL r = itow (CellsBasis.physicalRegisterNum r)
95 and emit_CELLSET r = itow (CellsBasis.physicalRegisterNum r)
96 fun emit_cond (I.EQ) = (0wx4 : Word32.word)
97 | emit_cond (I.NE) = (0wx5 : Word32.word)
98 | emit_cond (I.LT) = (0wxC : Word32.word)
99 | emit_cond (I.LE) = (0wxE : Word32.word)
100 | emit_cond (I.GT) = (0wxF : Word32.word)
101 | emit_cond (I.GE) = (0wxD : Word32.word)
102 | emit_cond (I.B) = (0wx2 : Word32.word)
103 | emit_cond (I.BE) = (0wx6 : Word32.word)
104 | emit_cond (I.A) = (0wx7 : Word32.word)
105 | emit_cond (I.AE) = (0wx3 : Word32.word)
106 | emit_cond (I.C) = (0wx2 : Word32.word)
107 | emit_cond (I.NC) = (0wx3 : Word32.word)
108 | emit_cond (I.P) = (0wxA : Word32.word)
109 | emit_cond (I.NP) = (0wxB : Word32.word)
110 | emit_cond (I.O) = (0wx0 : Word32.word)
111 | emit_cond (I.NO) = (0wx1 : Word32.word)
112 and emit_fibinOp (I.FIADDS) = (0wxDE, 0)
113 | emit_fibinOp (I.FIMULS) = (0wxDE, 1)
114 | emit_fibinOp (I.FICOMS) = (0wxDE, 2)
115 | emit_fibinOp (I.FICOMPS) = (0wxDE, 3)
116 | emit_fibinOp (I.FISUBS) = (0wxDE, 4)
117 | emit_fibinOp (I.FISUBRS) = (0wxDE, 5)
118 | emit_fibinOp (I.FIDIVS) = (0wxDE, 6)
119 | emit_fibinOp (I.FIDIVRS) = (0wxDE, 7)
120 | emit_fibinOp (I.FIADDL) = (0wxDA, 0)
121 | emit_fibinOp (I.FIMULL) = (0wxDA, 1)
122 | emit_fibinOp (I.FICOML) = (0wxDA, 2)
123 | emit_fibinOp (I.FICOMPL) = (0wxDA, 3)
124 | emit_fibinOp (I.FISUBL) = (0wxDA, 4)
125 | emit_fibinOp (I.FISUBRL) = (0wxDA, 5)
126 | emit_fibinOp (I.FIDIVL) = (0wxDA, 6)
127 | emit_fibinOp (I.FIDIVRL) = (0wxDA, 7)
128 and emit_funOp (I.FCHS) = (0wxE0 : Word32.word)
129 | emit_funOp (I.FABS) = (0wxE1 : Word32.word)
130 | emit_funOp (I.FTST) = (0wxE4 : Word32.word)
131 | emit_funOp (I.FXAM) = (0wxE5 : Word32.word)
132 | emit_funOp (I.FPTAN) = (0wxF2 : Word32.word)
133 | emit_funOp (I.FPATAN) = (0wxF3 : Word32.word)
134 | emit_funOp (I.FXTRACT) = (0wxF4 : Word32.word)
135 | emit_funOp (I.FPREM1) = (0wxF5 : Word32.word)
136 | emit_funOp (I.FDECSTP) = (0wxF6 : Word32.word)
137 | emit_funOp (I.FINCSTP) = (0wxF7 : Word32.word)
138 | emit_funOp (I.FPREM) = (0wxF8 : Word32.word)
139 | emit_funOp (I.FYL2XP1) = (0wxF9 : Word32.word)
140 | emit_funOp (I.FSQRT) = (0wxFA : Word32.word)
141 | emit_funOp (I.FSINCOS) = (0wxFB : Word32.word)
142 | emit_funOp (I.FRNDINT) = (0wxFC : Word32.word)
143 | emit_funOp (I.FSCALE) = (0wxFD : Word32.word)
144 | emit_funOp (I.FSIN) = (0wxFE : Word32.word)
145 | emit_funOp (I.FCOS) = (0wxFF : Word32.word)
146 fun modrm {mod, reg, rm} = eWord8 ((op mod << 0wx6) + ((reg << 0wx3) + rm))
147 and reg {opc, reg} = eWord8 ((opc << 0wx3) + reg)
148 and sib {ss, index, base} = eWord8 ((ss << 0wx6) + ((index << 0wx3) + base))
149 and immed8 {imm} = eWord8 imm
150 and immed32 {imm} = eWord32 imm
151 and immedOpnd {opnd} =
152 (case opnd of
153 I.Immed i32 => i32
154 | I.ImmedLabel le => lexp le
155 | I.LabelEA le => lexp le
156 | _ => error "immedOpnd"
157 )
158 and extension {opc, opnd} =
159 (case opnd of
160 I.Direct r => modrm {mod=3, reg=opc, rm=r}
161 | I.MemReg _ => extension {opc=opc, opnd=memReg opnd}
162 | I.FDirect _ => extension {opc=opc, opnd=memReg opnd}
163 | I.Displace{base, disp, ...} =>
164 let
165 (*#line 475.13 "x86/x86.mdl"*)
166 val immed = immedOpnd {opnd=disp}
167 in ()
168 end
169 | I.Indexed{base=NONE, index, scale, disp, ...} => ()
170 | I.Indexed{base=SOME b, index, scale, disp, ...} => ()
171 | _ => error "immedExt"
172 )
173 and encodeST {prefix, opc, st} =
174 let val st = emit_FP st
175 in eWord16 ((prefix << 0wx8) + ((opc << 0wx3) + st))
176 end
177 and encodeReg {prefix, reg, opnd} =
178 let val reg = emit_GP reg
179 in
180 ( emit prefix;
181 immedExt {opc=reg, opnd=opnd})
182 end
183 and arith {opc1, opc2, src, dst} =
184 (case (src, dst) of
185 (I.ImmedLabel le, dst) => arith {opc1=opc1, opc2=opc2, src=I.Immed (lexp le),
186 dst=dst}
187 | (I.LabelEA le, dst) => arith {opc1=opc1, opc2=opc2, src=I.Immed (lexp le),
188 dst=dst}
189 | (I.Immed i, dst) => ()
190 | (src, I.Direct r) => encodeReg {prefix=opc1 + op3, reg=reg, opnd=src}
191 | (I.Direct r, dst) => encodeReg {prefix=opc1 + 0wx1, reg=reg, opnd=dst}
192 | _ => error "arith"
193 )
194 fun emitter instr =
195 let
196 fun emitInstr (I.NOP) = error "NOP"
197 | emitInstr (I.JMP(operand, list)) = error "JMP"
198 | emitInstr (I.JCC{cond, opnd}) = error "JCC"
199 | emitInstr (I.CALL{opnd, defs, uses, return, cutsTo, mem, pops}) = error "CALL"
200 | emitInstr (I.ENTER{src1, src2}) = error "ENTER"
201 | emitInstr (I.LEAVE) = error "LEAVE"
202 | emitInstr (I.RET option) = error "RET"
203 | emitInstr (I.MOVE{mvOp, src, dst}) = error "MOVE"
204 | emitInstr (I.LEA{r32, addr}) = error "LEA"
205 | emitInstr (I.CMPL{lsrc, rsrc}) = error "CMPL"
206 | emitInstr (I.CMPW{lsrc, rsrc}) = error "CMPW"
207 | emitInstr (I.CMPB{lsrc, rsrc}) = error "CMPB"
208 | emitInstr (I.TESTL{lsrc, rsrc}) = error "TESTL"
209 | emitInstr (I.TESTW{lsrc, rsrc}) = error "TESTW"
210 | emitInstr (I.TESTB{lsrc, rsrc}) = error "TESTB"
211 | emitInstr (I.BITOP{bitOp, lsrc, rsrc}) = error "BITOP"
212 | emitInstr (I.BINARY{binOp, src, dst}) = error "BINARY"
213 | emitInstr (I.SHIFT{shiftOp, src, dst, count}) = error "SHIFT"
214 | emitInstr (I.CMPXCHG{lock, sz, src, dst}) = error "CMPXCHG"
215 | emitInstr (I.MULTDIV{multDivOp, src}) = error "MULTDIV"
216 | emitInstr (I.MUL3{dst, src2, src1}) = error "MUL3"
217 | emitInstr (I.UNARY{unOp, opnd}) = error "UNARY"
218 | emitInstr (I.SET{cond, opnd}) = error "SET"
219 | emitInstr (I.CMOV{cond, src, dst}) = error "CMOV"
220 | emitInstr (I.PUSHL operand) = error "PUSHL"
221 | emitInstr (I.PUSHW operand) = error "PUSHW"
222 | emitInstr (I.PUSHB operand) = error "PUSHB"
223 | emitInstr (I.PUSHFD) = error "PUSHFD"
224 | emitInstr (I.POPFD) = error "POPFD"
225 | emitInstr (I.POP operand) = error "POP"
226 | emitInstr (I.CDQ) = error "CDQ"
227 | emitInstr (I.INTO) = error "INTO"
228 | emitInstr (I.FBINARY{binOp, src, dst}) = error "FBINARY"
229 | emitInstr (I.FIBINARY{binOp, src}) = error "FIBINARY"
230 | emitInstr (I.FUNARY funOp) = error "FUNARY"
231 | emitInstr (I.FUCOM operand) = error "FUCOM"
232 | emitInstr (I.FUCOMP operand) = error "FUCOMP"
233 | emitInstr (I.FUCOMPP) = error "FUCOMPP"
234 | emitInstr (I.FCOMPP) = error "FCOMPP"
235 | emitInstr (I.FCOMI operand) = error "FCOMI"
236 | emitInstr (I.FCOMIP operand) = error "FCOMIP"
237 | emitInstr (I.FUCOMI operand) = error "FUCOMI"
238 | emitInstr (I.FUCOMIP operand) = error "FUCOMIP"
239 | emitInstr (I.FXCH{opnd}) = error "FXCH"
240 | emitInstr (I.FSTPL operand) = error "FSTPL"
241 | emitInstr (I.FSTPS operand) = error "FSTPS"
242 | emitInstr (I.FSTPT operand) = error "FSTPT"
243 | emitInstr (I.FSTL operand) = error "FSTL"
244 | emitInstr (I.FSTS operand) = error "FSTS"
245 | emitInstr (I.FLD1) = error "FLD1"
246 | emitInstr (I.FLDL2E) = error "FLDL2E"
247 | emitInstr (I.FLDL2T) = error "FLDL2T"
248 | emitInstr (I.FLDLG2) = error "FLDLG2"
249 | emitInstr (I.FLDLN2) = error "FLDLN2"
250 | emitInstr (I.FLDPI) = error "FLDPI"
251 | emitInstr (I.FLDZ) = error "FLDZ"
252 | emitInstr (I.FLDL operand) = error "FLDL"
253 | emitInstr (I.FLDS operand) = error "FLDS"
254 | emitInstr (I.FLDT operand) = error "FLDT"
255 | emitInstr (I.FILD operand) = error "FILD"
256 | emitInstr (I.FILDL operand) = error "FILDL"
257 | emitInstr (I.FILDLL operand) = error "FILDLL"
258 | emitInstr (I.FNSTSW) = error "FNSTSW"
259 | emitInstr (I.FENV{fenvOp, opnd}) = error "FENV"
260 | emitInstr (I.FMOVE{fsize, src, dst}) = error "FMOVE"
261 | emitInstr (I.FILOAD{isize, ea, dst}) = error "FILOAD"
262 | emitInstr (I.FBINOP{fsize, binOp, lsrc, rsrc, dst}) = error "FBINOP"
263 | emitInstr (I.FIBINOP{isize, binOp, lsrc, rsrc, dst}) = error "FIBINOP"
264 | emitInstr (I.FUNOP{fsize, unOp, src, dst}) = error "FUNOP"
265 | emitInstr (I.FCMP{i, fsize, lsrc, rsrc}) = error "FCMP"
266 | emitInstr (I.SAHF) = error "SAHF"
267 | emitInstr (I.LFENCE) = error "LFENCE"
268 | emitInstr (I.MFENCE) = error "MFENCE"
269 | emitInstr (I.SFENCE) = error "SFENCE"
270 | emitInstr (I.PAUSE) = error "PAUSE"
271 | emitInstr (I.LAHF) = error "LAHF"
272 | emitInstr (I.SOURCE{}) = ()
273 | emitInstr (I.SINK{}) = ()
274 | emitInstr (I.PHI{}) = ()
275 in
276 emitInstr instr
277 end
278
279 fun emitInstruction(I.ANNOTATION{i, ...}) = emitInstruction(i)
280 | emitInstruction(I.INSTR(i)) = emitter(i)
281 | emitInstruction(I.LIVE _) = ()
282 | emitInstruction(I.KILL _) = ()
283 | emitInstruction _ = error "emitInstruction"
284
285 in S.STREAM{beginCluster=init,
286 pseudoOp=pseudoOp,
287 emit=emitInstruction,
288 endCluster=fail,
289 defineLabel=doNothing,
290 entryLabel=doNothing,
291 comment=doNothing,
292 exitBlock=doNothing,
293 annotation=doNothing,
294 getAnnotations=getAnnotations
295 }
296 end
297 end
298