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/trunk/src/MLRISC/x86/emit/x86Asm.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/x86/emit/x86Asm.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 498 - (view) (download)
Original Path: sml/branches/SMLNJ/src/MLRISC/x86/emit/x86Asm.sml

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

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