]>
Commit | Line | Data |
---|---|---|
1 | (* L2 Compiler | |
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> | |
6 | *) | |
7 | ||
8 | signature TREE = | |
9 | sig | |
10 | ||
11 | datatype binop = ADD | SUB | MUL | DIV | MOD | LSH | RSH | LOGOR | LOGAND | BITOR | BITAND | BITXOR | NEQ | EQ | LT | GT | LE | GE | |
12 | datatype unop = NEG | BITNOT | BANG | |
13 | ||
14 | datatype exp = | |
15 | CONST of Word32.word | |
16 | | TEMP of Temp.temp | |
17 | | BINOP of binop * exp * exp | |
18 | | UNOP of unop * exp | |
19 | and stm = | |
20 | MOVE of exp * exp | |
21 | | RETURN of exp | |
22 | | LABEL of Label.label | |
23 | | JUMPIFN of exp * Label.label | |
24 | | JUMP of Label.label | |
25 | ||
26 | type program = stm list | |
27 | ||
28 | structure Print : | |
29 | sig | |
30 | val pp_exp : exp -> string | |
31 | val pp_stm : stm -> string | |
32 | val pp_program : program -> string | |
33 | end | |
34 | end | |
35 | ||
36 | structure Tree :> TREE = | |
37 | struct | |
38 | ||
39 | datatype binop = ADD | SUB | MUL | DIV | MOD | LSH | RSH | LOGOR | LOGAND | BITOR | BITAND | BITXOR | NEQ | EQ | LT | GT | LE | GE | |
40 | datatype unop = NEG | BITNOT | BANG | |
41 | ||
42 | datatype exp = | |
43 | CONST of Word32.word | |
44 | | TEMP of Temp.temp | |
45 | | BINOP of binop * exp * exp | |
46 | | UNOP of unop * exp | |
47 | and stm = | |
48 | MOVE of exp * exp | |
49 | | RETURN of exp | |
50 | | LABEL of Label.label | |
51 | | JUMPIFN of exp * Label.label | |
52 | | JUMP of Label.label | |
53 | ||
54 | type program = stm list | |
55 | ||
56 | structure Print = | |
57 | struct | |
58 | ||
59 | fun pp_binop ADD = "+" | |
60 | | pp_binop SUB = "-" | |
61 | | pp_binop MUL = "*" | |
62 | | pp_binop DIV = "/" | |
63 | | pp_binop MOD = "%" | |
64 | | pp_binop LSH = "<<" | |
65 | | pp_binop RSH = ">>" | |
66 | | pp_binop LOGOR = "||" | |
67 | | pp_binop LOGAND = "&&" | |
68 | | pp_binop BITOR = "|" | |
69 | | pp_binop BITAND = "&" | |
70 | | pp_binop BITXOR = "^" | |
71 | | pp_binop NEQ = "!=" | |
72 | | pp_binop EQ = "==" | |
73 | | pp_binop LE = "<=" | |
74 | | pp_binop LT = "<" | |
75 | | pp_binop GE = ">=" | |
76 | | pp_binop GT = ">" | |
77 | ||
78 | fun pp_unop NEG = "-" | |
79 | | pp_unop BITNOT = "~" | |
80 | | pp_unop BANG = "!" | |
81 | ||
82 | fun pp_exp (CONST(x)) = Word32Signed.toString x | |
83 | | pp_exp (TEMP(t)) = Temp.name t | |
84 | | pp_exp (BINOP (binop, e1, e2)) = | |
85 | "(" ^ pp_exp e1 ^ " " ^ pp_binop binop ^ " " ^ pp_exp e2 ^ ")" | |
86 | | pp_exp (UNOP (unop, e1)) = | |
87 | pp_unop unop ^ "(" ^ pp_exp e1 ^ ")" | |
88 | ||
89 | fun pp_stm (MOVE (e1,e2)) = | |
90 | pp_exp e1 ^ " <-- " ^ pp_exp e2 | |
91 | | pp_stm (RETURN e) = | |
92 | "return " ^ pp_exp e | |
93 | | pp_stm (LABEL l) = | |
94 | Label.name l ^ ":" | |
95 | | pp_stm (JUMP l) = | |
96 | "jump "^Label.name l | |
97 | | pp_stm (JUMPIFN (e, l)) = | |
98 | "jump "^Label.name l^" if! "^pp_exp e | |
99 | ||
100 | fun pp_program (nil) = "" | |
101 | | pp_program (stm::stms) = pp_stm stm ^ "\n" ^ pp_program stms | |
102 | end | |
103 | end |