]> Joshua Wise's Git repositories - snipe.git/blob - trans/tree.sml
Initial import of l3c
[snipe.git] / trans / tree.sml
1 (* L3 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  * Modified: Joshua Wise <jwise>
7  * Modified: Chris Lu <czl>
8  *)
9
10 signature TREE =
11 sig
12
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
15
16   type Blarg = int
17
18   datatype exp = 
19       CONST of Word32.word
20     | TEMP of Temp.temp
21     | ARG of Blarg (* I am j4cbo *)
22     | BINOP of binop * exp * exp
23     | UNOP of unop * exp
24     | CALL of Ast.ident * exp list
25   and stm =
26       MOVE of exp * exp
27     | RETURN of exp
28     | LABEL of Label.label
29     | JUMPIFN of exp * Label.label
30     | JUMP of Label.label
31   and func =
32       FUNCTION of Ast.ident * stm list
33
34   type program = func list
35
36   structure Print :
37   sig
38     val pp_exp : exp -> string
39     val pp_stm : stm -> string
40     val pp_program : program -> string
41   end
42 end
43
44 structure Tree :> TREE =
45 struct
46
47   datatype binop = ADD | SUB | MUL | DIV | MOD | LSH | RSH | LOGOR | LOGAND | BITOR | BITAND | BITXOR | NEQ | EQ | LT | GT | LE | GE
48   datatype unop = NEG | BITNOT | BANG
49
50   type Blarg = int
51
52   datatype exp = 
53       CONST of Word32.word
54     | TEMP of Temp.temp
55     | ARG of Blarg
56     | BINOP of binop * exp * exp
57     | UNOP of unop * exp
58     | CALL of Ast.ident * exp list
59   and stm =
60       MOVE of exp * exp
61     | RETURN of exp
62     | LABEL of Label.label
63     | JUMPIFN of exp * Label.label
64     | JUMP of Label.label
65   and func =
66       FUNCTION of Ast.ident * stm list
67
68   type program = func list
69
70   structure Print = 
71   struct
72
73     exception Aaaasssssss
74
75     fun pp_binop ADD = "+"
76       | pp_binop SUB = "-"
77       | pp_binop MUL = "*"
78       | pp_binop DIV = "/"
79       | pp_binop MOD = "%"
80       | pp_binop LSH = "<<"
81       | pp_binop RSH = ">>"
82       | pp_binop LOGOR = "||"
83       | pp_binop LOGAND = "&&"
84       | pp_binop BITOR = "|"
85       | pp_binop BITAND = "&"
86       | pp_binop BITXOR = "^"
87       | pp_binop NEQ = "!="
88       | pp_binop EQ = "=="
89       | pp_binop LE = "<="
90       | pp_binop LT = "<"
91       | pp_binop GE = ">="
92       | pp_binop GT = ">"
93     
94     fun pp_unop NEG = "-"
95       | pp_unop BITNOT = "~"
96       | pp_unop BANG = "!"
97
98     fun pp_exp (CONST(x)) = Word32Signed.toString x
99       | pp_exp (TEMP(t)) = Temp.name t
100       | pp_exp (ARG(n)) = "arg#"^Int.toString n
101       | pp_exp (BINOP (binop, e1, e2)) =
102           "(" ^ pp_exp e1 ^ " " ^ pp_binop binop ^ " " ^ pp_exp e2 ^ ")"
103       | pp_exp (UNOP (unop, e1)) =
104           pp_unop unop ^ "(" ^ pp_exp e1 ^ ")"
105       | pp_exp (CALL (f, l)) =
106           Symbol.name f ^ "(" ^ (String.concatWith ", " (List.map pp_exp l)) ^ ")"
107
108     fun pp_stm (MOVE (e1,e2)) =
109           pp_exp e1 ^ "  <--  " ^ pp_exp e2
110       | pp_stm (RETURN e) =
111           "return " ^ pp_exp e
112       | pp_stm (LABEL l) =
113           Label.name l ^ ":"
114       | pp_stm (JUMP l) = 
115           "jump "^Label.name l
116       | pp_stm (JUMPIFN (e, l)) =
117           "jump "^Label.name l^" if! "^pp_exp e
118
119     fun pp_program (nil) = ""
120       | pp_program (FUNCTION(id, stms)::funcs) =
121           (Symbol.name id) ^
122           "\n{\n" ^
123           (foldr (fn (a,b) => (pp_stm a) ^ "\n" ^ b) "" stms) ^
124           "}\n" ^
125           pp_program funcs
126   end
127 end
This page took 0.029617 seconds and 4 git commands to generate.