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

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