X-Git-Url: http://git.joshuawise.com/snipe.git/blobdiff_plain/0a24e44d4e9f82f8d3d83de8e58c83c8cf2868b6..6ade8b0a3251e44b34c6bdbbd9403e36d6fd6231:/trans/tree.sml diff --git a/trans/tree.sml b/trans/tree.sml index f69cefb..f5a92b5 100644 --- a/trans/tree.sml +++ b/trans/tree.sml @@ -1,8 +1,10 @@ -(* L2 Compiler +(* L3 Compiler * IR Trees * Author: Kaustuv Chaudhuri * Modified: Alex Vaynberg * Modified: Frank Pfenning + * Modified: Joshua Wise + * Modified: Chris Lu *) signature TREE = @@ -11,19 +13,25 @@ sig datatype binop = ADD | SUB | MUL | DIV | MOD | LSH | RSH | LOGOR | LOGAND | BITOR | BITAND | BITXOR | NEQ | EQ | LT | GT | LE | GE datatype unop = NEG | BITNOT | BANG + type Blarg = int + datatype exp = CONST of Word32.word | TEMP of Temp.temp + | ARG of Blarg (* I am j4cbo *) | BINOP of binop * exp * exp | UNOP of unop * exp + | CALL of Ast.ident * exp list and stm = MOVE of exp * exp | RETURN of exp | LABEL of Label.label | JUMPIFN of exp * Label.label | JUMP of Label.label + and func = + FUNCTION of Ast.ident * stm list - type program = stm list + type program = func list structure Print : sig @@ -39,23 +47,31 @@ struct datatype binop = ADD | SUB | MUL | DIV | MOD | LSH | RSH | LOGOR | LOGAND | BITOR | BITAND | BITXOR | NEQ | EQ | LT | GT | LE | GE datatype unop = NEG | BITNOT | BANG + type Blarg = int + datatype exp = CONST of Word32.word | TEMP of Temp.temp + | ARG of Blarg | BINOP of binop * exp * exp | UNOP of unop * exp + | CALL of Ast.ident * exp list and stm = MOVE of exp * exp | RETURN of exp | LABEL of Label.label | JUMPIFN of exp * Label.label | JUMP of Label.label + and func = + FUNCTION of Ast.ident * stm list - type program = stm list + type program = func list structure Print = struct + exception Aaaasssssss + fun pp_binop ADD = "+" | pp_binop SUB = "-" | pp_binop MUL = "*" @@ -81,10 +97,13 @@ struct fun pp_exp (CONST(x)) = Word32Signed.toString x | pp_exp (TEMP(t)) = Temp.name t + | pp_exp (ARG(n)) = "arg#"^Int.toString n | pp_exp (BINOP (binop, e1, e2)) = "(" ^ pp_exp e1 ^ " " ^ pp_binop binop ^ " " ^ pp_exp e2 ^ ")" | pp_exp (UNOP (unop, e1)) = pp_unop unop ^ "(" ^ pp_exp e1 ^ ")" + | pp_exp (CALL (f, l)) = + Symbol.name f ^ "(" ^ (String.concatWith ", " (List.map pp_exp l)) ^ ")" fun pp_stm (MOVE (e1,e2)) = pp_exp e1 ^ " <-- " ^ pp_exp e2 @@ -98,6 +117,11 @@ struct "jump "^Label.name l^" if! "^pp_exp e fun pp_program (nil) = "" - | pp_program (stm::stms) = pp_stm stm ^ "\n" ^ pp_program stms + | pp_program (FUNCTION(id, stms)::funcs) = + (Symbol.name id) ^ + "\n{\n" ^ + (foldr (fn (a,b) => (pp_stm a) ^ "\n" ^ b) "" stms) ^ + "}\n" ^ + pp_program funcs end end