Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/branches/SMLNJ/src/MLRISC/x86/emit/x86Asm.sml
ViewVC logotype

Annotation of /sml/branches/SMLNJ/src/MLRISC/x86/emit/x86Asm.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 469 - (view) (download)

1 : monnier 411 (*
2 :     * This file was automatically generated by MDGen
3 :     * from the machine description file "x86/x86.md".
4 : monnier 247 *)
5 :    
6 :    
7 : monnier 411 functor X86AsmEmitter(structure Instr : X86INSTR
8 :     structure Stream : INSTRUCTION_STREAM
9 :     structure Shuffle : X86SHUFFLE
10 :     where I = Instr
11 :    
12 :     (*#line 140.1 "x86/x86.md"*)
13 :     structure MemRegs : MEMORY_REGISTERS where I=Instr
14 :     ) : INSTRUCTION_EMITTER =
15 : monnier 247 struct
16 : monnier 411 structure I = Instr
17 :     structure C = I.C
18 :     structure S = Stream
19 :     structure P = S.P
20 :     structure LE = LabelExp
21 :     structure Constant = I.Constant
22 :    
23 :     fun error msg = MLRiscErrorMsg.error("X86Asm",msg)
24 :    
25 : monnier 429 fun makeStream formatAnnotations =
26 : monnier 411 let val stream = !AsmStream.asmOutStream
27 : monnier 429 fun emit' s = TextIO.output(stream,s)
28 :     val newline = ref true
29 :     val tabs = ref 0
30 :     fun tabbing 0 = ()
31 :     | tabbing n = (emit' "\t"; tabbing(n-1))
32 :     fun emit s = (tabbing(!tabs); tabs := 0; newline := false; emit' s)
33 :     fun nl() = (tabs := 0; if !newline then () else (newline := true; emit' "\n"))
34 : monnier 411 fun comma() = emit ","
35 : monnier 429 fun tab() = tabs := !tabs + 1
36 :     fun ms n = let val s = Int.toString n
37 :     in if n<0 then "-"^String.substring(s,1,size s-1)
38 :     else s
39 :     end
40 : monnier 411 fun emit_label lab = emit(Label.nameOf lab)
41 :     fun emit_labexp le = emit(LabelExp.toString le)
42 :     fun emit_const c = emit(Constant.toString c)
43 :     fun emit_int i = emit(ms i)
44 :     fun paren f = (emit "("; f(); emit ")")
45 :     fun defineLabel lab = emit(Label.nameOf lab^":\n")
46 :     fun entryLabel lab = defineLabel lab
47 :     fun comment msg = emit("\t/* " ^ msg ^ " */")
48 :     fun annotation a = (comment(Annotations.toString a); nl())
49 :     fun emit_region mem = comment(I.Region.toString mem)
50 :     fun pseudoOp pOp = emit(P.toString pOp)
51 :     fun init size = (comment("Code Size = " ^ ms size); nl())
52 :     fun doNothing _ = ()
53 : monnier 429 val emitRegInfo = AsmFormatUtil.reginfo(emit,formatAnnotations)
54 : monnier 411 fun emitter regmap =
55 :     let
56 : monnier 247
57 : monnier 429 fun emit_CC r =
58 :     ((emit (C.showCC (regmap r)));
59 :     (emitRegInfo r))
60 :     and emit_FP r =
61 :     ((emit (C.showFP (regmap r)));
62 :     (emitRegInfo r))
63 :     and emit_GP r =
64 :     ((emit (C.showGP (regmap r)));
65 :     (emitRegInfo r))
66 : monnier 247
67 : monnier 429 fun asm_unaryOp (I.DEC) = "dec"
68 :     | asm_unaryOp (I.INC) = "inc"
69 :     | asm_unaryOp (I.NEG) = "neg"
70 :     | asm_unaryOp (I.NOT) = "not"
71 :     and emit_unaryOp x = (emit (asm_unaryOp x))
72 :     and asm_fbinOp (I.FADDP) = "faddp"
73 : monnier 411 | asm_fbinOp (I.FADD) = "fadd"
74 :     | asm_fbinOp (I.FMULP) = "fmulp"
75 :     | asm_fbinOp (I.FMUL) = "fmul"
76 :     | asm_fbinOp (I.FSUBP) = "fsubp"
77 :     | asm_fbinOp (I.FSUB) = "fsub"
78 :     | asm_fbinOp (I.FSUBRP) = "fsubrp"
79 :     | asm_fbinOp (I.FSUBR) = "fsubr"
80 :     | asm_fbinOp (I.FDIVP) = "fdivp"
81 :     | asm_fbinOp (I.FDIV) = "fdiv"
82 :     | asm_fbinOp (I.FDIVRP) = "fdivrp"
83 :     | asm_fbinOp (I.FDIVR) = "fdivr"
84 :     and emit_fbinOp x = (emit (asm_fbinOp x))
85 : monnier 429 and asm_funOp (I.FABS) = "fabs"
86 :     | asm_funOp (I.FCHS) = "fchs"
87 :     and emit_funOp x = (emit (asm_funOp x))
88 :     and asm_move (I.MOVL) = "movl"
89 :     | asm_move (I.MOVZX) = "movzx"
90 :     | asm_move (I.MOVB) = "movb"
91 :     and emit_move x = (emit (asm_move x))
92 :     and asm_multDivOp (I.UMUL) = "umul"
93 :     | asm_multDivOp (I.IDIV) = "idiv"
94 :     | asm_multDivOp (I.UDIV) = "udiv"
95 :     and emit_multDivOp x = (emit (asm_multDivOp x))
96 :     and asm_binaryOp (I.ADD) = "add"
97 :     | asm_binaryOp (I.SUB) = "sub"
98 :     | asm_binaryOp (I.AND) = "and"
99 :     | asm_binaryOp (I.OR) = "or"
100 :     | asm_binaryOp (I.XOR) = "xor"
101 :     | asm_binaryOp (I.SHL) = "shl"
102 :     | asm_binaryOp (I.SAR) = "sar"
103 :     | asm_binaryOp (I.SHR) = "shr"
104 :     and emit_binaryOp x = (emit (asm_binaryOp x))
105 : monnier 411 and asm_cond (I.EQ) = "eq"
106 :     | asm_cond (I.NE) = "ne"
107 :     | asm_cond (I.LT) = "lt"
108 :     | asm_cond (I.LE) = "le"
109 :     | asm_cond (I.GT) = "gt"
110 :     | asm_cond (I.GE) = "ge"
111 :     | asm_cond (I.B) = "b"
112 :     | asm_cond (I.BE) = "be"
113 :     | asm_cond (I.A) = "a"
114 :     | asm_cond (I.AE) = "ae"
115 :     | asm_cond (I.C) = "c"
116 :     | asm_cond (I.NC) = "nc"
117 :     | asm_cond (I.P) = "p"
118 :     | asm_cond (I.NP) = "np"
119 :     | asm_cond (I.O) = "o"
120 :     | asm_cond (I.NO) = "no"
121 :     and emit_cond x = (emit (asm_cond x))
122 :     val memReg = (MemRegs.memReg regmap)
123 : monnier 247
124 : monnier 429 fun emitInt32 i = let
125 : monnier 247
126 : monnier 429 (*#line 144.1 "x86/x86.md"*)
127 :     val s = (Int32.toString i)
128 :    
129 :     (*#line 145.1 "x86/x86.md"*)
130 :     val s = (if (i >= 0)
131 :     then s
132 :     else ("-" ^ (String.substring (s, 1, ((size s) - 1)))))
133 :     in (emit s)
134 :     end
135 :    
136 :    
137 : monnier 411 fun emit_src2 NONE = ()
138 :     | emit_src2 (SOME i) =
139 :     ((emit "$");
140 :     (emitInt32 i);
141 :     (emit ", "))
142 : monnier 247
143 : monnier 411 fun emitScale 0 = (emit "1")
144 :     | emitScale 1 = (emit "2")
145 :     | emitScale 2 = (emit "4")
146 :     | emitScale 3 = (emit "8")
147 :     | emitScale _ = (error "emitScale")
148 :     and eImmed (I.Immed i) = (emitInt32 i)
149 :     | eImmed (I.Const c) = (emit_const c)
150 :     | eImmed (I.ImmedLabel lexp) = (emit_labexp lexp)
151 :     | eImmed _ = (error "eImmed")
152 :     and emit_operand opn =
153 :     (
154 :     case opn of
155 :     I.Immed i =>
156 :     ((emit "$");
157 :     (emitInt32 i))
158 :     | I.Const c => (emit_const c)
159 :     | I.ImmedLabel lexp =>
160 :     ((emit "$");
161 :     (emit_labexp lexp))
162 :     | I.LabelEA le => (emit_labexp le)
163 :     | I.Relative _ => (error "emit_operand")
164 :     | I.Direct r => (emit_GP r)
165 :     | I.FDirect f => let
166 : monnier 247
167 : monnier 429 (*#line 171.1 "x86/x86.md"*)
168 : monnier 411 val f' = (regmap f)
169 :     in (if (f' < (32 + 8))
170 :     then (emit_FP f')
171 :     else (emit_operand (memReg opn)))
172 :     end
173 : monnier 247
174 : monnier 411 | I.Displace{base, disp=I.Immed 0, mem, ...} =>
175 :     ((emit "(");
176 :     (emit_GP base);
177 :     (emit ")");
178 :     (emit_region mem))
179 :     | I.Displace{base, disp, mem, ...} =>
180 :     ((eImmed disp);
181 :     (emit "(");
182 :     (emit_GP base);
183 :     (emit ")");
184 :     (emit_region mem))
185 :     | I.Indexed{base=NONE, index, scale, disp, mem, ...} =>
186 :     ((emit "(");
187 :     (emit_GP index);
188 :     (comma ());
189 :     (emitScale scale);
190 :     (emit ")");
191 :     (emit_region mem))
192 :     | I.Indexed{base=SOME base, index, scale, disp, mem, ...} =>
193 :     ((eOptionalDisp disp);
194 :     (emit "(");
195 :     (emit_GP base);
196 :     (comma ());
197 :     (emit_GP index);
198 : monnier 429 (comma ());
199 : monnier 411 (emitScale scale);
200 :     (emit ")");
201 :     (emit_region mem))
202 :     )
203 :     and eOptionalDisp (I.Immed 0) = ()
204 :     | eOptionalDisp (I.Const c) = (emit (Constant.toString c))
205 :     | eOptionalDisp (I.Immed i) = (emitInt32 i)
206 :     | eOptionalDisp _ = (error "eOptionalDisp")
207 : monnier 247
208 : monnier 411 fun stupidGas (I.ImmedLabel lexp) = (emit_labexp lexp)
209 :     | stupidGas (I.LabelEA _) = (error "stupidGas")
210 :     | stupidGas opnd = (emit_operand opnd)
211 :     val emit_dst = emit_operand
212 :     val emit_src = emit_operand
213 :     val emit_opnd = emit_operand
214 :     val emit_src1 = emit_operand
215 :     val emit_rsrc = emit_operand
216 :     val emit_lsrc = emit_operand
217 :     val emit_addr = emit_operand
218 : monnier 247
219 : monnier 411 fun emitInstr instr =
220 :     ((tab ());
221 :    
222 :     (
223 :     case instr of
224 :     I.NOP => (emit "nop")
225 :     | I.JMP(operand, label) =>
226 :     ((emit "jmp\t");
227 :     (stupidGas operand))
228 : monnier 247 | I.JCC{cond, opnd} =>
229 : monnier 411 ((emit "j");
230 :     (emit_cond cond);
231 :     (emit ", ");
232 :     (stupidGas opnd))
233 :     | I.CALL(operand, cellset1, cellset2, region) =>
234 :     ((emit "call\t");
235 :     (stupidGas operand);
236 :     (emit_region region))
237 : monnier 429 | I.RET operand =>
238 :     ((emit "ret");
239 :    
240 :     (
241 :     case operand of
242 :     NONE => ()
243 :     | SOME e =>
244 :     ((emit "\t");
245 :     (emit_operand e))
246 :     ))
247 : monnier 247 | I.MOVE{mvOp, src, dst} =>
248 : monnier 411 ((emit_move mvOp);
249 :     (emit "\t");
250 :     (emit_src src);
251 :     (emit ", ");
252 :     (emit_dst dst))
253 : monnier 247 | I.LEA{r32, addr} =>
254 : monnier 411 ((emit "leal\t");
255 :     (emit_addr addr);
256 :     (emit ", ");
257 :     (emit_GP r32))
258 : monnier 247 | I.CMP{lsrc, rsrc} =>
259 : monnier 411 ((emit "cmpl\t");
260 :     (emit_rsrc rsrc);
261 :     (emit ", ");
262 :     (emit_lsrc lsrc))
263 :     | I.BINARY{binOp, src, dst} =>
264 :     ((emit_binaryOp binOp);
265 :     (emit "l\t");
266 :     (emit_src src);
267 :     (emit ", ");
268 :     (emit_dst dst))
269 :     | I.MULTDIV{multDivOp, src} =>
270 :     ((emit_multDivOp multDivOp);
271 :     (emit "l\t");
272 :     (emit_src src))
273 :     | I.MUL3{dst, src1, src2} =>
274 :     ((emit "imul\t");
275 :     (emit_src1 src1);
276 :     (emit ", ");
277 :     (emit_src2 src2);
278 :     (emit_int dst))
279 : monnier 247 | I.UNARY{unOp, opnd} =>
280 : monnier 411 ((emit_unaryOp unOp);
281 :     (emit "l\t");
282 :     (emit_opnd opnd))
283 :     | I.PUSH operand =>
284 :     ((emit "pushl\t");
285 :     (emit_operand operand))
286 :     | I.POP operand =>
287 :     ((emit "popl\t");
288 :     (emit_operand operand))
289 :     | I.CDQ => (emit "cdq")
290 :     | I.INTO => (emit "into")
291 :     | I.COPY{dst, src, tmp} => (emitInstrs (Shuffle.shuffle {regmap=regmap, tmp=tmp, dst=dst, src=src}))
292 :     | I.FCOPY{dst, src, tmp} => (emitInstrs (Shuffle.shufflefp {regmap=regmap, tmp=tmp, dst=dst, src=src}))
293 : monnier 247 | I.FBINARY{binOp, src, dst} =>
294 : monnier 411 ((emit_fbinOp binOp);
295 :     (emit "\t");
296 :     (emit_src src);
297 :     (emit ", ");
298 :     (emit_dst dst))
299 :     | I.FUNARY funOp => (emit_funOp funOp)
300 :     | I.FUCOMPP => (emit "fucompp")
301 :     | I.FXCH => (emit "fxch")
302 :     | I.FSTP operand =>
303 :     ((emit "fstp\t");
304 :     (emit_operand operand))
305 :     | I.FLD operand =>
306 :     ((emit "fld\t");
307 :     (emit_operand operand))
308 :     | I.FILD operand =>
309 :     ((emit "fild\t");
310 :     (emit_operand operand))
311 :     | I.FNSTSW => (emit "fnstsw")
312 :     | I.SAHF => (emit "sahf")
313 :     | I.ANNOTATION{i, a} =>
314 :     ((emitInstr i);
315 :     (comment (Annotations.toString a)))
316 :     | I.GROUP annotation => (comment (Annotations.toString annotation))
317 :     );
318 :     (nl ()))
319 :     and emitInstrs [] = ()
320 :     | emitInstrs (i::is) =
321 :     (emitInstr i; app (fn i => (tab(); emitInstr i)) is)
322 :     in emitInstr end
323 :    
324 : monnier 429 in S.STREAM{beginCluster=init,
325 : monnier 411 pseudoOp=pseudoOp,
326 :     emit=emitter,
327 : monnier 429 endCluster=doNothing,
328 : monnier 411 defineLabel=defineLabel,
329 :     entryLabel=entryLabel,
330 :     comment=comment,
331 :     exitBlock=doNothing,
332 : monnier 429 annotation=annotation,
333 :     phi=doNothing,
334 :     alias=doNothing
335 : monnier 411 }
336 :     end
337 : monnier 247 end
338 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0