]> Joshua Wise's Git repositories - snipe.git/blame - trans/tree.sml
Initial import of l2c
[snipe.git] / trans / tree.sml
CommitLineData
0a24e44d 1(* L2 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>
6 *)
7
8signature TREE =
9sig
10
0a24e44d
JW
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
12aa4087
JW
13
14 datatype exp =
15 CONST of Word32.word
16 | TEMP of Temp.temp
17 | BINOP of binop * exp * exp
0a24e44d 18 | UNOP of unop * exp
12aa4087
JW
19 and stm =
20 MOVE of exp * exp
21 | RETURN of exp
0a24e44d
JW
22 | LABEL of Label.label
23 | JUMPIFN of exp * Label.label
24 | JUMP of Label.label
12aa4087
JW
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
34end
35
36structure Tree :> TREE =
37struct
38
0a24e44d
JW
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
12aa4087
JW
41
42 datatype exp =
43 CONST of Word32.word
44 | TEMP of Temp.temp
45 | BINOP of binop * exp * exp
0a24e44d 46 | UNOP of unop * exp
12aa4087
JW
47 and stm =
48 MOVE of exp * exp
49 | RETURN of exp
0a24e44d
JW
50 | LABEL of Label.label
51 | JUMPIFN of exp * Label.label
52 | JUMP of Label.label
12aa4087
JW
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 = "%"
0a24e44d
JW
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 = "!"
12aa4087
JW
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 ^ ")"
0a24e44d
JW
86 | pp_exp (UNOP (unop, e1)) =
87 pp_unop unop ^ "(" ^ pp_exp e1 ^ ")"
12aa4087
JW
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
0a24e44d
JW
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
12aa4087
JW
99
100 fun pp_program (nil) = ""
101 | pp_program (stm::stms) = pp_stm stm ^ "\n" ^ pp_program stms
102 end
103end
This page took 0.144208 seconds and 4 git commands to generate.