]>
Commit | Line | Data |
---|---|---|
1 | (* L2 compiler | |
2 | * stringifier | |
3 | * turns a list of x86 insns into the assembly code to generate them | |
4 | * Author: Chris Lu <czl@andrew.cmu.edu> | |
5 | *) | |
6 | ||
7 | signature STRINGIFY = | |
8 | sig | |
9 | type asm = x86.insn list | |
10 | val stringify : asm -> string | |
11 | end | |
12 | ||
13 | structure Stringify :> STRINGIFY = | |
14 | struct | |
15 | type asm = x86.insn list | |
16 | structure X = x86 | |
17 | ||
18 | (* val stringify : asm -> string | |
19 | * turns a x86 instruction list into a string of assembly code for these instructions *) | |
20 | ||
21 | fun stringify' (X.MOVL (r1, r2)) = "\tmovl " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
22 | | stringify' (X.SUBL (r1, r2)) = "\tsubl " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
23 | | stringify' (X.IMUL (r1, r2)) = "\timul " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
24 | | stringify' (X.IMUL3 (r1, r2, k)) = "\timul " ^ X.prettyprint_oper (X.CONST k) ^ ", " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
25 | | stringify' (X.ADDL (r1, r2)) = "\taddl " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
26 | | stringify' (X.IDIVL (r1)) = "\tidivl " ^ X.prettyprint_oper r1 ^ "\n" | |
27 | | stringify' (X.NEG (r1)) = "\tnegl " ^ X.prettyprint_oper r1 ^ "\n" | |
28 | | stringify' (X.NOTL (r1)) = "\tnotl " ^ X.prettyprint_oper r1 ^ "\n" | |
29 | | stringify' (X.RET) = "\tret\n" | |
30 | | stringify' (X.CLTD) = "\tcltd\n" | |
31 | | stringify' (X.SALL (r1, X.REG X.ECX)) = "\tsall %cl, " ^ X.prettyprint_oper r1 ^ "\n" | |
32 | | stringify' (X.SALL (r1, X.CONST k)) = "\tsall " ^ X.prettyprint_operb (X.CONST k) ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
33 | | stringify' (X.SALL _) = raise ErrorMsg.InternalError "Invalid operand generated for SALL" | |
34 | | stringify' (X.SARL (r1, X.REG X.ECX)) = "\tsarl %cl, " ^ X.prettyprint_oper r1 ^ "\n" | |
35 | | stringify' (X.SARL (r1, X.CONST k)) = "\tsarl " ^ X.prettyprint_operb (X.CONST k) ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
36 | | stringify' (X.SARL _) = raise ErrorMsg.InternalError "Invalid operand generated for SARL" | |
37 | | stringify' (X.ANDL (r1, r2)) = "\tandl " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
38 | | stringify' (X.ORL (r1, r2)) = "\torl " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
39 | | stringify' (X.XORL (r1, r2)) = "\txorl " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
40 | | stringify' (X.CMPL (r1, r2)) = "\tcmpl " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
41 | | stringify' (X.TEST (r1, r2)) = "\ttest " ^ X.prettyprint_oper r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
42 | | stringify' (X.SETNE (r1)) = "\tsetne " ^ X.prettyprint_operb r1 ^ "\n" | |
43 | | stringify' (X.SETE (r1)) = "\tsete " ^ X.prettyprint_operb r1 ^ "\n" | |
44 | | stringify' (X.SETLE (r1)) = "\tsetle " ^ X.prettyprint_operb r1 ^ "\n" | |
45 | | stringify' (X.SETL (r1)) = "\tsetl " ^ X.prettyprint_operb r1 ^ "\n" | |
46 | | stringify' (X.SETGE (r1)) = "\tsetge " ^ X.prettyprint_operb r1 ^ "\n" | |
47 | | stringify' (X.SETG (r1)) = "\tsetg " ^ X.prettyprint_operb r1 ^ "\n" | |
48 | | stringify' (X.MOVZBL (r1, r2)) = "\tmovzbl " ^ X.prettyprint_operb r2 ^ ", " ^ X.prettyprint_oper r1 ^ "\n" | |
49 | | stringify' (X.DIRECTIVE(s)) = s ^ "\n" | |
50 | | stringify' (X.COMMENT(s)) = "\t// " ^ s ^ "\n" | |
51 | | stringify' (X.LABEL l) = Label.name l ^ ":\n" | |
52 | | stringify' (X.JMP l) = "\tjmp " ^ Label.name l ^ "\n" | |
53 | | stringify' (X.JE l) = "\tje " ^ Label.name l ^ "\n" | |
54 | | stringify' (X.JNE l) = "\tjne " ^ Label.name l ^ "\n" | |
55 | ||
56 | (* val stringify : asm -> string *) | |
57 | fun stringify l = foldr (fn (a,b) => (stringify' a) ^ b) ("") l | |
58 | ||
59 | end |