]> Joshua Wise's Git repositories - snipe.git/blob - trans/treeutils.sml
Initial import of l5c
[snipe.git] / trans / treeutils.sml
1 signature TREEUTILS =
2 sig
3   val effect : Tree.exp -> bool
4   val effect_stm : Tree.stm -> bool
5
6   structure Print :
7   sig
8     val pp_exp : Tree.exp -> string
9     val pp_stm : Tree.stm -> string
10     val pp_program : Tree.program -> string
11   end
12 end
13
14 structure TreeUtils :> TREEUTILS =
15 struct
16   structure T = Tree
17
18   (* effect : T.exp -> bool
19    * true iff the given expression has an effect.
20    *)
21   fun effect (T.CONST _) = false
22     | effect (T.TEMP _) = false
23     | effect (T.ARG _) = false
24     | effect (T.BINOP(T.DIV, _, _)) = true
25     | effect (T.BINOP(T.MOD, _, _)) = true
26     | effect (T.CALL _) = true
27     | effect (T.BINOP(_, a, b)) = (effect a) orelse (effect b)
28     | effect (T.UNOP (_, a)) = effect a
29     | effect (T.MEMORY _) = true
30     | effect (T.ALLOC _) = true
31     | effect (T.COND (a, b, c)) = (effect a) orelse (effect b) orelse (effect c)
32     | effect (T.STMVAR (sl, e)) = true (* Has to be, to be safe <--- jwise is an assclown, he was too lazy to write a effect_stm *)
33     | effect (T.NULLPTR) = false
34
35   fun effect_stm (T.MOVE (e1,e2)) = effect e1 orelse effect e2
36     | effect_stm (T.RETURN (e1,e2)) = effect e1 orelse effect e1
37     | effect_stm (T.EFFECT e) = effect e
38     | effect_stm (T.JUMPIFN (e,_)) = effect e
39     | effect_stm _ = false
40
41   structure Print = 
42   struct
43     exception Aaaasssssss
44
45     fun pp_binop T.ADD = "+"
46       | pp_binop T.SUB = "-"
47       | pp_binop T.MUL = "*"
48       | pp_binop T.DIV = "/"
49       | pp_binop T.MOD = "%"
50       | pp_binop T.LSH = "<<"
51       | pp_binop T.RSH = ">>"
52       | pp_binop T.LOGOR = "||"
53       | pp_binop T.LOGAND = "&&"
54       | pp_binop T.BITOR = "|"
55       | pp_binop T.BITAND = "&"
56       | pp_binop T.BITXOR = "^"
57       | pp_binop T.NEQ = "!="
58       | pp_binop T.EQ = "=="
59       | pp_binop T.LE = "<="
60       | pp_binop T.LT = "<"
61       | pp_binop T.GE = ">="
62       | pp_binop T.GT = ">"
63       | pp_binop T.BE = "[BE]"
64     
65     fun pp_unop T.NEG = "-"
66       | pp_unop T.BITNOT = "~"
67       | pp_unop T.BANG = "!"
68
69     fun pp_exp (T.CONST(x)) = Word32Signed.toString x
70       | pp_exp (T.TEMP(t)) = Temp.name t
71       | pp_exp (T.ARG(n, sz)) = "arg#"^Int.toString n
72       | pp_exp (T.BINOP (binop, e1, e2)) =
73           "(" ^ pp_exp e1 ^ " " ^ pp_binop binop ^ " " ^ pp_exp e2 ^ ")"
74       | pp_exp (T.UNOP (unop, e1)) =
75           pp_unop unop ^ "(" ^ pp_exp e1 ^ ")"
76       | pp_exp (T.CALL (f, l, sz)) =
77           Symbol.name f ^ "(" ^ (String.concatWith ", " (List.map (fn (e, _) => pp_exp e) l)) ^ ")"
78       | pp_exp (T.MEMORY (exp, sz)) = "M(" ^ Temp.sfx sz ^ ")[" ^ pp_exp exp ^ "]"
79       | pp_exp (T.ALLOC(e)) = "NEW(" ^ pp_exp e ^ ")"
80       | pp_exp (T.COND(c,e1,e2)) = "(" ^ pp_exp c ^ ") ? (" ^ pp_exp e1 ^ ") : (" ^ pp_exp e2 ^ ")"
81       | pp_exp (T.STMVAR(sl,v)) = "({" ^ (foldr (fn (st,s) => (pp_stm st) ^ "; " ^ s) "" sl) ^ (pp_exp v) ^ "})"
82       | pp_exp (T.NULLPTR) = "NULL"
83
84     and pp_stm (T.MOVE (e1,e2)) =
85           pp_exp e1 ^ "  <--  " ^ pp_exp e2
86       | pp_stm (T.RETURN (e, sz)) =
87           "return " ^ pp_exp e
88       | pp_stm (T.EFFECT e) = pp_exp e
89       | pp_stm (T.LABEL l) =
90           Label.name l ^ ":"
91       | pp_stm (T.JUMP l) = 
92           "jump "^Label.name l
93       | pp_stm (T.JUMPIFN (e, l)) =
94           "jump "^Label.name l^" if! "^pp_exp e
95
96     fun pp_program (nil) = ""
97       | pp_program (T.FUNCTION(id, stms)::funcs) =
98           (Symbol.name id) ^
99           "\n{\n" ^
100           (foldr (fn (a,b) => (pp_stm a) ^ "\n" ^ b) "" stms) ^
101           "}\n" ^
102           pp_program funcs
103   end
104 end
This page took 0.029046 seconds and 4 git commands to generate.