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 545 - (view) (download)

1 : monnier 411 (*
2 : george 545 * This file was automatically generated by MDGen (v2.0)
3 : monnier 411 * 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 : george 545 (*#line 164.7 "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 : george 545 structure LabelExp = I.LabelExp
21 : monnier 411 structure Constant = I.Constant
22 :    
23 : monnier 498 val show_cellset = MLRiscControl.getFlag "asm-show-cellset"
24 : george 545 val show_region = MLRiscControl.getFlag "asm-show-region"
25 : monnier 498
26 : george 545 fun error msg = MLRiscErrorMsg.error("X86AsmEmitter",msg)
27 : monnier 411
28 : monnier 429 fun makeStream formatAnnotations =
29 : monnier 411 let val stream = !AsmStream.asmOutStream
30 : monnier 429 fun emit' s = TextIO.output(stream,s)
31 :     val newline = ref true
32 :     val tabs = ref 0
33 :     fun tabbing 0 = ()
34 :     | tabbing n = (emit' "\t"; tabbing(n-1))
35 :     fun emit s = (tabbing(!tabs); tabs := 0; newline := false; emit' s)
36 :     fun nl() = (tabs := 0; if !newline then () else (newline := true; emit' "\n"))
37 : monnier 411 fun comma() = emit ","
38 : monnier 429 fun tab() = tabs := !tabs + 1
39 :     fun ms n = let val s = Int.toString n
40 :     in if n<0 then "-"^String.substring(s,1,size s-1)
41 :     else s
42 :     end
43 : monnier 411 fun emit_label lab = emit(Label.nameOf lab)
44 :     fun emit_labexp le = emit(LabelExp.toString le)
45 :     fun emit_const c = emit(Constant.toString c)
46 :     fun emit_int i = emit(ms i)
47 :     fun paren f = (emit "("; f(); emit ")")
48 :     fun defineLabel lab = emit(Label.nameOf lab^":\n")
49 :     fun entryLabel lab = defineLabel lab
50 :     fun comment msg = emit("\t/* " ^ msg ^ " */")
51 :     fun annotation a = (comment(Annotations.toString a); nl())
52 : george 545 fun doNothing _ = ()
53 : monnier 411 fun emit_region mem = comment(I.Region.toString mem)
54 : george 545 val emit_region =
55 :     if !show_region then emit_region else doNothing
56 : monnier 411 fun pseudoOp pOp = emit(P.toString pOp)
57 :     fun init size = (comment("Code Size = " ^ ms size); nl())
58 :     fun emitter regmap =
59 :     let
60 : george 545 val emitRegInfo = AsmFormatUtil.reginfo
61 :     (emit,regmap,formatAnnotations)
62 :     fun emit_GP r =
63 :     ( emit (C.showGP (regmap r));
64 :     emitRegInfo r )
65 : monnier 429 and emit_FP r =
66 : george 545 ( emit (C.showFP (regmap r));
67 :     emitRegInfo r )
68 :     and emit_CC r =
69 :     ( emit (C.showCC (regmap r));
70 :     emitRegInfo r )
71 :     and emit_MEM r =
72 :     ( emit (C.showMEM (regmap r));
73 :     emitRegInfo r )
74 :     and emit_CTRL r =
75 :     ( emit (C.showCTRL (regmap r));
76 :     emitRegInfo r )
77 : monnier 498
78 :     fun emit_cellset(title,cellset) =
79 :     (nl(); comment(title^C.cellsetToString' regmap cellset))
80 : george 545 val emit_cellset =
81 :     if !show_cellset then emit_cellset else doNothing
82 : monnier 498 fun emit_defs cellset = emit_cellset("defs: ",cellset)
83 :     fun emit_uses cellset = emit_cellset("uses: ",cellset)
84 : george 545 fun asm_cond (I.EQ) = "e"
85 :     | asm_cond (I.NE) = "ne"
86 :     | asm_cond (I.LT) = "l"
87 :     | asm_cond (I.LE) = "le"
88 :     | asm_cond (I.GT) = "g"
89 :     | asm_cond (I.GE) = "ge"
90 :     | asm_cond (I.B) = "b"
91 :     | asm_cond (I.BE) = "be"
92 :     | asm_cond (I.A) = "a"
93 :     | asm_cond (I.AE) = "ae"
94 :     | asm_cond (I.C) = "c"
95 :     | asm_cond (I.NC) = "nc"
96 :     | asm_cond (I.P) = "p"
97 :     | asm_cond (I.NP) = "np"
98 :     | asm_cond (I.O) = "o"
99 :     | asm_cond (I.NO) = "no"
100 :     and emit_cond x = emit (asm_cond x)
101 :     and asm_binaryOp (I.ADDL) = "addl"
102 :     | asm_binaryOp (I.SUBL) = "subl"
103 :     | asm_binaryOp (I.ANDL) = "andl"
104 :     | asm_binaryOp (I.ORL) = "orl"
105 :     | asm_binaryOp (I.XORL) = "xorl"
106 :     | asm_binaryOp (I.SHLL) = "shll"
107 :     | asm_binaryOp (I.SARL) = "sarl"
108 :     | asm_binaryOp (I.SHRL) = "shrl"
109 :     | asm_binaryOp (I.ADCL) = "adcl"
110 :     | asm_binaryOp (I.SBBL) = "sbbl"
111 :     | asm_binaryOp (I.ADDW) = "addw"
112 :     | asm_binaryOp (I.SUBW) = "subw"
113 :     | asm_binaryOp (I.ANDW) = "andw"
114 :     | asm_binaryOp (I.ORW) = "orw"
115 :     | asm_binaryOp (I.XORW) = "xorw"
116 :     | asm_binaryOp (I.SHLW) = "shlw"
117 :     | asm_binaryOp (I.SARW) = "sarw"
118 :     | asm_binaryOp (I.SHRW) = "shrw"
119 :     | asm_binaryOp (I.ADDB) = "addb"
120 :     | asm_binaryOp (I.SUBB) = "subb"
121 :     | asm_binaryOp (I.ANDB) = "andb"
122 :     | asm_binaryOp (I.ORB) = "orb"
123 :     | asm_binaryOp (I.XORB) = "xorb"
124 :     | asm_binaryOp (I.SHLB) = "shlb"
125 :     | asm_binaryOp (I.SARB) = "sarb"
126 :     | asm_binaryOp (I.SHRB) = "shrb"
127 :     and emit_binaryOp x = emit (asm_binaryOp x)
128 :     and asm_multDivOp (I.UMUL) = "umul"
129 :     | asm_multDivOp (I.IDIV) = "idiv"
130 :     | asm_multDivOp (I.UDIV) = "udiv"
131 :     and emit_multDivOp x = emit (asm_multDivOp x)
132 :     and asm_unaryOp (I.DECL) = "decl"
133 :     | asm_unaryOp (I.INCL) = "incl"
134 :     | asm_unaryOp (I.NEGL) = "negl"
135 :     | asm_unaryOp (I.NOTL) = "notl"
136 :     | asm_unaryOp (I.NOTW) = "notw"
137 :     | asm_unaryOp (I.NOTB) = "notb"
138 :     and emit_unaryOp x = emit (asm_unaryOp x)
139 :     and asm_move (I.MOVL) = "movl"
140 :     | asm_move (I.MOVB) = "movb"
141 :     | asm_move (I.MOVW) = "movw"
142 :     | asm_move (I.MOVSWL) = "movswl"
143 :     | asm_move (I.MOVZWL) = "movzwl"
144 :     | asm_move (I.MOVSBL) = "movsbl"
145 :     | asm_move (I.MOVZBL) = "movzbl"
146 :     and emit_move x = emit (asm_move x)
147 : monnier 429 and asm_fbinOp (I.FADDP) = "faddp"
148 : monnier 411 | asm_fbinOp (I.FADD) = "fadd"
149 : george 545 | asm_fbinOp (I.FIADD) = "fiadd"
150 : monnier 411 | asm_fbinOp (I.FMULP) = "fmulp"
151 :     | asm_fbinOp (I.FMUL) = "fmul"
152 : george 545 | asm_fbinOp (I.FIMUL) = "fimul"
153 : monnier 411 | asm_fbinOp (I.FSUBP) = "fsubp"
154 :     | asm_fbinOp (I.FSUB) = "fsub"
155 : george 545 | asm_fbinOp (I.FISUB) = "fisub"
156 : monnier 411 | asm_fbinOp (I.FSUBRP) = "fsubrp"
157 :     | asm_fbinOp (I.FSUBR) = "fsubr"
158 : george 545 | asm_fbinOp (I.FISUBR) = "fisubr"
159 : monnier 411 | asm_fbinOp (I.FDIVP) = "fdivp"
160 :     | asm_fbinOp (I.FDIV) = "fdiv"
161 : george 545 | asm_fbinOp (I.FIDIV) = "fidiv"
162 : monnier 411 | asm_fbinOp (I.FDIVRP) = "fdivrp"
163 :     | asm_fbinOp (I.FDIVR) = "fdivr"
164 : george 545 | asm_fbinOp (I.FIDIVR) = "fidivr"
165 :     and emit_fbinOp x = emit (asm_fbinOp x)
166 : monnier 429 and asm_funOp (I.FABS) = "fabs"
167 :     | asm_funOp (I.FCHS) = "fchs"
168 : george 545 | asm_funOp (I.FSIN) = "fsin"
169 :     | asm_funOp (I.FCOS) = "fcos"
170 :     | asm_funOp (I.FTAN) = "ftan"
171 :     | asm_funOp (I.FSCALE) = "fscale"
172 :     | asm_funOp (I.FRNDINT) = "frndint"
173 :     | asm_funOp (I.FSQRT) = "fsqrt"
174 :     | asm_funOp (I.FTST) = "ftst"
175 :     | asm_funOp (I.FXAM) = "fxam"
176 :     | asm_funOp (I.FINCSTP) = "fincstp"
177 :     | asm_funOp (I.FDECSTP) = "fdecstp"
178 :     and emit_funOp x = emit (asm_funOp x)
179 :     and asm_fenvOp (I.FLDENV) = "fldenv"
180 :     | asm_fenvOp (I.FNLDENV) = "fnldenv"
181 :     | asm_fenvOp (I.FSTENV) = "fstenv"
182 :     | asm_fenvOp (I.FNSTENV) = "fnstenv"
183 :     and emit_fenvOp x = emit (asm_fenvOp x)
184 : monnier 247
185 : george 545 (*#line 166.6 "x86/x86.md"*)
186 :     val memReg = MemRegs.memReg regmap
187 :    
188 :     (*#line 167.6 "x86/x86.md"*)
189 : monnier 429 fun emitInt32 i = let
190 : monnier 247
191 : george 545 (*#line 168.10 "x86/x86.md"*)
192 :     val s = Int32.toString i
193 : monnier 429
194 : george 545 (*#line 169.10 "x86/x86.md"*)
195 : monnier 429 val s = (if (i >= 0)
196 : george 545 then s
197 :     else ("-" ^ (String.substring (s, 1, (size s) - 1))))
198 :     in emit s
199 : monnier 429 end
200 :    
201 :    
202 : george 545 (*#line 172.6 "x86/x86.md"*)
203 :     fun emitScale 0 = emit "1"
204 :     | emitScale 1 = emit "2"
205 :     | emitScale 2 = emit "4"
206 :     | emitScale 3 = emit "8"
207 :     | emitScale _ = error "emitScale"
208 :     and eImmed (I.Immed i) = emitInt32 i
209 :     | eImmed (I.ImmedLabel lexp) = emit_labexp lexp
210 :     | eImmed _ = error "eImmed"
211 : monnier 411 and emit_operand opn =
212 :     (
213 :     case opn of
214 :     I.Immed i =>
215 : george 545 ( emit "$";
216 :     emitInt32 i )
217 : monnier 411 | I.ImmedLabel lexp =>
218 : george 545 ( emit "$";
219 :     emit_labexp lexp )
220 :     | I.LabelEA le => emit_labexp le
221 :     | I.Relative _ => error "emit_operand"
222 :     | I.Direct r => emit_GP r
223 :     | I.MemReg r => emit_operand (memReg opn)
224 : monnier 411 | I.FDirect f => let
225 : monnier 247
226 : george 545 (*#line 191.17 "x86/x86.md"*)
227 :     val f' = regmap f
228 : monnier 411 in (if (f' < (32 + 8))
229 :     then (emit_FP f')
230 :     else (emit_operand (memReg opn)))
231 :     end
232 : monnier 247
233 : monnier 411 | I.Displace{base, disp, mem, ...} =>
234 : george 545 ( emit_disp disp;
235 :     emit "(";
236 :     emit_GP base;
237 :     emit ")";
238 :     emit_region mem )
239 : monnier 411 | I.Indexed{base=NONE, index, scale, disp, mem, ...} =>
240 : george 545 ( emit_disp disp;
241 :     emit "(";
242 :     emit_GP index;
243 :     comma ();
244 :     emitScale scale;
245 :     emit ")";
246 :     emit_region mem )
247 : monnier 411 | I.Indexed{base=SOME base, index, scale, disp, mem, ...} =>
248 : george 545 ( emit_disp disp;
249 :     emit "(";
250 :     emit_GP base;
251 :     comma ();
252 :     emit_GP index;
253 :     comma ();
254 :     emitScale scale;
255 :     emit ")";
256 :     emit_region mem )
257 : monnier 411 )
258 : george 545 and emit_disp (I.Immed 0) = ()
259 :     | emit_disp (I.Immed i) = emitInt32 i
260 :     | emit_disp (I.ImmedLabel lexp) = emit_labexp lexp
261 :     | emit_disp _ = error "emit_disp"
262 : monnier 247
263 : george 545 (*#line 212.7 "x86/x86.md"*)
264 :     fun stupidGas (I.ImmedLabel lexp) = emit_labexp lexp
265 :     | stupidGas (I.LabelEA _) = error "stupidGas"
266 :     | stupidGas opnd = emit_operand opnd
267 :    
268 :     (*#line 216.7 "x86/x86.md"*)
269 : monnier 411 val emit_dst = emit_operand
270 : george 545
271 :     (*#line 217.7 "x86/x86.md"*)
272 : monnier 411 val emit_src = emit_operand
273 : george 545
274 :     (*#line 218.7 "x86/x86.md"*)
275 : monnier 411 val emit_opnd = emit_operand
276 : george 545
277 :     (*#line 219.7 "x86/x86.md"*)
278 : monnier 411 val emit_rsrc = emit_operand
279 : george 545
280 :     (*#line 220.7 "x86/x86.md"*)
281 : monnier 411 val emit_lsrc = emit_operand
282 : george 545
283 :     (*#line 221.7 "x86/x86.md"*)
284 : monnier 411 val emit_addr = emit_operand
285 : monnier 247
286 : george 545 (*#line 222.7 "x86/x86.md"*)
287 :     val emit_src1 = emit_operand
288 : monnier 411 fun emitInstr instr =
289 : george 545 ( tab ();
290 : monnier 411
291 :     (
292 :     case instr of
293 : george 545 I.NOP => emit "nop"
294 : monnier 411 | I.JMP(operand, label) =>
295 : george 545 ( emit "jmp\t";
296 :     stupidGas operand )
297 : monnier 247 | I.JCC{cond, opnd} =>
298 : george 545 ( emit "j";
299 :     emit_cond cond;
300 :     emit "\t";
301 :     stupidGas opnd )
302 : monnier 411 | I.CALL(operand, cellset1, cellset2, region) =>
303 : george 545 ( emit "call\t";
304 :     stupidGas operand;
305 :     emit_region region;
306 :     emit_defs cellset1;
307 :     emit_uses cellset2 )
308 :     | I.LEAVE => emit "leave"
309 : monnier 429 | I.RET operand =>
310 : george 545 ( emit "ret";
311 : monnier 429
312 :     (
313 :     case operand of
314 :     NONE => ()
315 :     | SOME e =>
316 : george 545 ( emit "\t";
317 :     emit_operand e )
318 : monnier 429 ))
319 : monnier 247 | I.MOVE{mvOp, src, dst} =>
320 : george 545 ( emit_move mvOp;
321 :     emit "\t";
322 :     emit_src src;
323 :     emit ", ";
324 :     emit_dst dst )
325 : monnier 247 | I.LEA{r32, addr} =>
326 : george 545 ( emit "leal\t";
327 :     emit_addr addr;
328 :     emit ", ";
329 :     emit_GP r32 )
330 :     | I.CMPL{lsrc, rsrc} =>
331 :     ( emit "cmpl\t";
332 :     emit_rsrc rsrc;
333 :     emit ", ";
334 :     emit_lsrc lsrc )
335 :     | I.CMPW{lsrc, rsrc} =>
336 :     ( emit "cmpb\t";
337 :     emit_rsrc rsrc;
338 :     emit ", ";
339 :     emit_lsrc lsrc )
340 :     | I.CMPB{lsrc, rsrc} =>
341 :     ( emit "cmpb\t";
342 :     emit_rsrc rsrc;
343 :     emit ", ";
344 :     emit_lsrc lsrc )
345 :     | I.TESTL{lsrc, rsrc} =>
346 :     ( emit "testl\t";
347 :     emit_rsrc rsrc;
348 :     emit ", ";
349 :     emit_lsrc lsrc )
350 :     | I.TESTW{lsrc, rsrc} =>
351 :     ( emit "testw\t";
352 :     emit_rsrc rsrc;
353 :     emit ", ";
354 :     emit_lsrc lsrc )
355 :     | I.TESTB{lsrc, rsrc} =>
356 :     ( emit "testb\t";
357 :     emit_rsrc rsrc;
358 :     emit ", ";
359 :     emit_lsrc lsrc )
360 : monnier 411 | I.BINARY{binOp, src, dst} =>
361 : george 545 (
362 :     case (src, binOp) of
363 :     (I.Direct _, (I.SARL | I.SHRL | I.SHLL | I.SARW | I.SHRW | I.SHLW | I.SARB | I.SHRB | I.SHLB)) =>
364 :     ( emit_binaryOp binOp;
365 :     emit "\t%cl, ";
366 :     emit_dst dst )
367 :     | _ =>
368 :     ( emit_binaryOp binOp;
369 :     emit "\t";
370 :     emit_src src;
371 :     emit ", ";
372 :     emit_dst dst )
373 :     )
374 : monnier 411 | I.MULTDIV{multDivOp, src} =>
375 : george 545 ( emit_multDivOp multDivOp;
376 :     emit "l\t";
377 :     emit_src src )
378 :     | I.MUL3{dst, src2, src1} =>
379 :     (
380 :     case src2 of
381 :     NONE =>
382 :     ( emit "imul\t";
383 :     emit_src1 src1;
384 :     emit ", ";
385 :     emit_GP dst )
386 :     | SOME i =>
387 :     ( emit "imul\t$";
388 :     emitInt32 i;
389 :     emit ", ";
390 :     emit_src1 src1;
391 :     emit ", ";
392 :     emit_GP dst )
393 :     )
394 : monnier 247 | I.UNARY{unOp, opnd} =>
395 : george 545 ( emit_unaryOp unOp;
396 :     emit "\t";
397 :     emit_opnd opnd )
398 :     | I.SET{cond, opnd} =>
399 :     ( emit "set";
400 :     emit_cond cond;
401 :     emit "\t";
402 :     emit_opnd opnd )
403 :     | I.CMOV{cond, src, dst} =>
404 :     ( emit "cmov";
405 :     emit_cond cond;
406 :     emit "\t";
407 :     emit_src src;
408 :     emit ", ";
409 :     emit_GP dst )
410 :     | I.PUSHL operand =>
411 :     ( emit "pushl\t";
412 :     emit_operand operand )
413 :     | I.PUSHW operand =>
414 :     ( emit "pushw\t";
415 :     emit_operand operand )
416 :     | I.PUSHB operand =>
417 :     ( emit "pushb\t";
418 :     emit_operand operand )
419 : monnier 411 | I.POP operand =>
420 : george 545 ( emit "popl\t";
421 :     emit_operand operand )
422 :     | I.CDQ => emit "cdq"
423 :     | I.INTO => emit "into"
424 :     | I.COPY{dst, src, tmp} => emitInstrs (Shuffle.shuffle {regmap=regmap, tmp=tmp, dst=dst, src=src})
425 :     | I.FCOPY{dst, src, tmp} => emitInstrs (Shuffle.shufflefp {regmap=regmap, tmp=tmp, dst=dst, src=src})
426 : monnier 247 | I.FBINARY{binOp, src, dst} =>
427 : george 545 ( emit_fbinOp binOp;
428 :     emit "\t";
429 :     emit_src src;
430 :     emit ", ";
431 :     emit_dst dst )
432 :     | I.FUNARY funOp => emit_funOp funOp
433 :     | I.FUCOMPP => emit "fucompp"
434 :     | I.FCOM => emit "fcom"
435 :     | I.FCOMPP => emit "fcompp"
436 :     | I.FXCH{opnd} =>
437 :     ( emit "fxch\t";
438 :     (if (opnd = (C.ST 1))
439 :     then ()
440 :     else (emit_FP opnd)))
441 :     | I.FSTPL operand =>
442 :     ( emit "fstpl\t";
443 :     emit_operand operand )
444 :     | I.FSTPS operand =>
445 :     ( emit "fstps\t";
446 :     emit_operand operand )
447 :     | I.FLDL operand =>
448 :     ( emit "fldl\t";
449 :     emit_operand operand )
450 :     | I.FLDS operand =>
451 :     ( emit "flds\t";
452 :     emit_operand operand )
453 : monnier 411 | I.FILD operand =>
454 : george 545 ( emit "fild\t";
455 :     emit_operand operand )
456 :     | I.FNSTSW => emit "fnstsw"
457 :     | I.FENV{fenvOp, opnd} =>
458 :     ( emit_fenvOp fenvOp;
459 :     emit "\t";
460 :     emit_opnd opnd )
461 :     | I.SAHF => emit "sahf"
462 : monnier 411 | I.ANNOTATION{i, a} =>
463 : george 545 ( emitInstr i;
464 :     comment (Annotations.toString a))
465 : monnier 411 );
466 : george 545 nl ())
467 : monnier 411 and emitInstrs [] = ()
468 :     | emitInstrs (i::is) =
469 :     (emitInstr i; app (fn i => (tab(); emitInstr i)) is)
470 :     in emitInstr end
471 :    
472 : monnier 429 in S.STREAM{beginCluster=init,
473 : monnier 411 pseudoOp=pseudoOp,
474 :     emit=emitter,
475 : monnier 429 endCluster=doNothing,
476 : monnier 411 defineLabel=defineLabel,
477 :     entryLabel=entryLabel,
478 :     comment=comment,
479 :     exitBlock=doNothing,
480 : monnier 429 annotation=annotation,
481 :     phi=doNothing,
482 :     alias=doNothing
483 : monnier 411 }
484 :     end
485 : monnier 247 end
486 :    

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