]>
Commit | Line | Data |
---|---|---|
6ade8b0a | 1 | (* L3 Compiler |
12aa4087 JW |
2 | * IR Trees |
3 | * Author: Kaustuv Chaudhuri <kaustuv+@cs.cmu.edu> | |
4 | * Modified: Alex Vaynberg <alv@andrew.cmu.edu> | |
5 | * Modified: Frank Pfenning <fp@cs.cmu.edu> | |
6ade8b0a JW |
6 | * Modified: Joshua Wise <jwise> |
7 | * Modified: Chris Lu <czl> | |
12aa4087 JW |
8 | *) |
9 | ||
10 | signature TREE = | |
11 | sig | |
12 | ||
0a24e44d JW |
13 | datatype binop = ADD | SUB | MUL | DIV | MOD | LSH | RSH | LOGOR | LOGAND | BITOR | BITAND | BITXOR | NEQ | EQ | LT | GT | LE | GE |
14 | datatype unop = NEG | BITNOT | BANG | |
12aa4087 | 15 | |
6ade8b0a JW |
16 | type Blarg = int |
17 | ||
12aa4087 JW |
18 | datatype exp = |
19 | CONST of Word32.word | |
20 | | TEMP of Temp.temp | |
1144856b | 21 | | ARG of Blarg * int (* I am j4cbo *) |
12aa4087 | 22 | | BINOP of binop * exp * exp |
0a24e44d | 23 | | UNOP of unop * exp |
1144856b JW |
24 | | CALL of Ast.ident * (exp * int) list * int |
25 | | MEMORY of exp | |
26 | | ALLOC of exp | |
12aa4087 | 27 | and stm = |
1144856b JW |
28 | MOVE of exp * exp * int |
29 | | RETURN of exp * int | |
30 | | EFFECT of exp * int | |
0a24e44d JW |
31 | | LABEL of Label.label |
32 | | JUMPIFN of exp * Label.label | |
33 | | JUMP of Label.label | |
6ade8b0a JW |
34 | and func = |
35 | FUNCTION of Ast.ident * stm list | |
12aa4087 | 36 | |
6ade8b0a | 37 | type program = func list |
12aa4087 JW |
38 | |
39 | structure Print : | |
40 | sig | |
41 | val pp_exp : exp -> string | |
42 | val pp_stm : stm -> string | |
43 | val pp_program : program -> string | |
44 | end | |
45 | end | |
46 | ||
47 | structure Tree :> TREE = | |
48 | struct | |
49 | ||
0a24e44d JW |
50 | datatype binop = ADD | SUB | MUL | DIV | MOD | LSH | RSH | LOGOR | LOGAND | BITOR | BITAND | BITXOR | NEQ | EQ | LT | GT | LE | GE |
51 | datatype unop = NEG | BITNOT | BANG | |
12aa4087 | 52 | |
6ade8b0a JW |
53 | type Blarg = int |
54 | ||
12aa4087 JW |
55 | datatype exp = |
56 | CONST of Word32.word | |
57 | | TEMP of Temp.temp | |
1144856b | 58 | | ARG of Blarg * int |
12aa4087 | 59 | | BINOP of binop * exp * exp |
0a24e44d | 60 | | UNOP of unop * exp |
1144856b JW |
61 | | CALL of Ast.ident * (exp * int) list * int |
62 | | MEMORY of exp | |
63 | | ALLOC of exp | |
12aa4087 | 64 | and stm = |
1144856b JW |
65 | MOVE of exp * exp * int |
66 | | RETURN of exp * int | |
67 | | EFFECT of exp * int | |
0a24e44d JW |
68 | | LABEL of Label.label |
69 | | JUMPIFN of exp * Label.label | |
70 | | JUMP of Label.label | |
6ade8b0a JW |
71 | and func = |
72 | FUNCTION of Ast.ident * stm list | |
12aa4087 | 73 | |
6ade8b0a | 74 | type program = func list |
12aa4087 JW |
75 | |
76 | structure Print = | |
77 | struct | |
78 | ||
6ade8b0a JW |
79 | exception Aaaasssssss |
80 | ||
12aa4087 JW |
81 | fun pp_binop ADD = "+" |
82 | | pp_binop SUB = "-" | |
83 | | pp_binop MUL = "*" | |
84 | | pp_binop DIV = "/" | |
85 | | pp_binop MOD = "%" | |
0a24e44d JW |
86 | | pp_binop LSH = "<<" |
87 | | pp_binop RSH = ">>" | |
88 | | pp_binop LOGOR = "||" | |
89 | | pp_binop LOGAND = "&&" | |
90 | | pp_binop BITOR = "|" | |
91 | | pp_binop BITAND = "&" | |
92 | | pp_binop BITXOR = "^" | |
93 | | pp_binop NEQ = "!=" | |
94 | | pp_binop EQ = "==" | |
95 | | pp_binop LE = "<=" | |
96 | | pp_binop LT = "<" | |
97 | | pp_binop GE = ">=" | |
98 | | pp_binop GT = ">" | |
99 | ||
100 | fun pp_unop NEG = "-" | |
101 | | pp_unop BITNOT = "~" | |
102 | | pp_unop BANG = "!" | |
12aa4087 JW |
103 | |
104 | fun pp_exp (CONST(x)) = Word32Signed.toString x | |
105 | | pp_exp (TEMP(t)) = Temp.name t | |
1144856b | 106 | | pp_exp (ARG(n, sz)) = "arg#"^Int.toString n |
12aa4087 JW |
107 | | pp_exp (BINOP (binop, e1, e2)) = |
108 | "(" ^ pp_exp e1 ^ " " ^ pp_binop binop ^ " " ^ pp_exp e2 ^ ")" | |
0a24e44d JW |
109 | | pp_exp (UNOP (unop, e1)) = |
110 | pp_unop unop ^ "(" ^ pp_exp e1 ^ ")" | |
1144856b JW |
111 | | pp_exp (CALL (f, l, sz)) = |
112 | Symbol.name f ^ "(" ^ (String.concatWith ", " (List.map (fn (e, _) => pp_exp e) l)) ^ ")" | |
113 | | pp_exp (MEMORY exp) = "M[" ^ pp_exp exp ^ "]" | |
114 | | pp_exp (ALLOC(e)) = "NEW(" ^ pp_exp e ^ ")" | |
12aa4087 | 115 | |
1144856b | 116 | fun pp_stm (MOVE (e1,e2, sz)) = |
12aa4087 | 117 | pp_exp e1 ^ " <-- " ^ pp_exp e2 |
1144856b | 118 | | pp_stm (RETURN (e, sz)) = |
12aa4087 | 119 | "return " ^ pp_exp e |
1144856b | 120 | | pp_stm (EFFECT (e, sz)) = pp_exp e |
0a24e44d JW |
121 | | pp_stm (LABEL l) = |
122 | Label.name l ^ ":" | |
123 | | pp_stm (JUMP l) = | |
124 | "jump "^Label.name l | |
125 | | pp_stm (JUMPIFN (e, l)) = | |
126 | "jump "^Label.name l^" if! "^pp_exp e | |
12aa4087 JW |
127 | |
128 | fun pp_program (nil) = "" | |
6ade8b0a JW |
129 | | pp_program (FUNCTION(id, stms)::funcs) = |
130 | (Symbol.name id) ^ | |
131 | "\n{\n" ^ | |
132 | (foldr (fn (a,b) => (pp_stm a) ^ "\n" ^ b) "" stms) ^ | |
133 | "}\n" ^ | |
134 | pp_program funcs | |
12aa4087 JW |
135 | end |
136 | end |