]> Joshua Wise's Git repositories - snipe.git/blame - trans/tree.sml
Initial import of l3c
[snipe.git] / trans / tree.sml
CommitLineData
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
10signature TREE =
11sig
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
6ade8b0a 21 | ARG of Blarg (* I am j4cbo *)
12aa4087 22 | BINOP of binop * exp * exp
0a24e44d 23 | UNOP of unop * exp
6ade8b0a 24 | CALL of Ast.ident * exp list
12aa4087
JW
25 and stm =
26 MOVE of exp * exp
27 | RETURN of exp
0a24e44d
JW
28 | LABEL of Label.label
29 | JUMPIFN of exp * Label.label
30 | JUMP of Label.label
6ade8b0a
JW
31 and func =
32 FUNCTION of Ast.ident * stm list
12aa4087 33
6ade8b0a 34 type program = func list
12aa4087
JW
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
42end
43
44structure Tree :> TREE =
45struct
46
0a24e44d
JW
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
12aa4087 49
6ade8b0a
JW
50 type Blarg = int
51
12aa4087
JW
52 datatype exp =
53 CONST of Word32.word
54 | TEMP of Temp.temp
6ade8b0a 55 | ARG of Blarg
12aa4087 56 | BINOP of binop * exp * exp
0a24e44d 57 | UNOP of unop * exp
6ade8b0a 58 | CALL of Ast.ident * exp list
12aa4087
JW
59 and stm =
60 MOVE of exp * exp
61 | RETURN of exp
0a24e44d
JW
62 | LABEL of Label.label
63 | JUMPIFN of exp * Label.label
64 | JUMP of Label.label
6ade8b0a
JW
65 and func =
66 FUNCTION of Ast.ident * stm list
12aa4087 67
6ade8b0a 68 type program = func list
12aa4087
JW
69
70 structure Print =
71 struct
72
6ade8b0a
JW
73 exception Aaaasssssss
74
12aa4087
JW
75 fun pp_binop ADD = "+"
76 | pp_binop SUB = "-"
77 | pp_binop MUL = "*"
78 | pp_binop DIV = "/"
79 | pp_binop MOD = "%"
0a24e44d
JW
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 = "!"
12aa4087
JW
97
98 fun pp_exp (CONST(x)) = Word32Signed.toString x
99 | pp_exp (TEMP(t)) = Temp.name t
6ade8b0a 100 | pp_exp (ARG(n)) = "arg#"^Int.toString n
12aa4087
JW
101 | pp_exp (BINOP (binop, e1, e2)) =
102 "(" ^ pp_exp e1 ^ " " ^ pp_binop binop ^ " " ^ pp_exp e2 ^ ")"
0a24e44d
JW
103 | pp_exp (UNOP (unop, e1)) =
104 pp_unop unop ^ "(" ^ pp_exp e1 ^ ")"
6ade8b0a
JW
105 | pp_exp (CALL (f, l)) =
106 Symbol.name f ^ "(" ^ (String.concatWith ", " (List.map pp_exp l)) ^ ")"
12aa4087
JW
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
0a24e44d
JW
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
12aa4087
JW
118
119 fun pp_program (nil) = ""
6ade8b0a
JW
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
12aa4087
JW
126 end
127end
This page took 0.027155 seconds and 4 git commands to generate.