staload TL = "templab.sats"
staload "irtree.sats"
fn fprint_binop (out: FILEref, binop: binop) = case+ binop of
| PLUS _ => fprint_string (out, "+")
| MINUS _ => fprint_string (out, "-")
| MUL _ => fprint_string (out, "*")
| DIV _ => fprint_string (out, "/")
fn fprint_relop (out: FILEref, relop: relop) = case+ relop of
| EQ _ => fprint_string (out, "=")
| NEQ _ => fprint_string (out, "<>")
| GT _ => fprint_string (out, "GT")
| GE _ => fprint_string (out, "GTE")
| LT _ => fprint_string (out, "LT")
| LE _ => fprint_string (out, "LE")
fn fprint_explst
(out: FILEref, es: explst) = let
fun loop
(out: FILEref, es: explst, i: int): void =
case+ es of
| list_cons (e, es) => let
val () = if i > 0 then fprint_string (out, ", ")
in
fprint_exp (out, e); loop (out, es, i+1)
end | list_nil () => ()
in
loop (out, es, 0)
end
implement fprint_exp (out, exp) = let
macdef prstr (str) = fprint_string (out, ,(str))
in
case+ exp of
| EXPconst i => begin
prstr "EXPconst("; fprint_int (out, i); prstr ")"
end | EXPname (lab) => begin
prstr "EXPname("; $TL.fprint_label (out, lab); prstr ")"
end | EXPtemp (tmp) => begin
prstr "EXPtemp("; $TL.fprint_temp (out, tmp); prstr ")"
end | EXPbinop (binop, e1, e2) => begin
prstr "EXPbinop(";
fprint_binop (out, binop);
prstr "; ";
fprint_exp (out, e1); prstr ", "; fprint_exp (out, e2);
prstr ")"
end | EXPmem e => begin
prstr "EXPmem("; fprint_exp (out, e); prstr ")"
end | EXPcall (e_fun, es_arg) => begin
prstr "EXPcall(";
fprint_exp (out, e_fun);
prstr "; ";
fprint_explst (out, es_arg);
prstr ")"
end | EXPeseq (s, e) => begin
prstr "EXPeseq(";
fprint_stm (out, s); prstr "; "; fprint_exp (out, e);
prstr ")"
end end
implement print_exp (exp) = fprint_exp (stdout_ref, exp)
implement prerr_exp (exp) = fprint_exp (stderr_ref, exp)
implement fprint_stm (out, stm) = let
macdef prstr (str) = fprint_string (out, ,(str))
in
case+ stm of
| STMmove (e1, e2) => begin
prstr "STMmove(";
fprint_exp (out, e1); prstr ", "; fprint_exp (out, e2);
prstr ")"
end | STMexp e => begin
prstr "STMexp("; fprint_exp (out, e); prstr ")"
end | STMjump (e, labs) => begin
prstr "STMjump(";
fprint_exp (out, e); prstr "; "; prstr "...";
prstr ")"
end | STMcjump (relop, e1, e2, tlab, flab) => begin
prstr "STMcjump(";
fprint_relop (out, relop);
prstr "; ";
fprint_exp (out, e1);
prstr ", ";
fprint_exp (out, e2);
prstr "; ";
$TL.fprint_label (out, tlab);
prstr " : ";
$TL.fprint_label (out, flab);
prstr ")"
end | STMseq (s1, s2) => begin
prstr "STMseq(";
fprint_stm (out, s1); prstr "; "; fprint_stm (out, s2);
prstr ")"
end | STMlabel lab => begin
prstr "STMlabel("; $TL.fprint_label (out, lab); prstr ")"
end | STMusedef _ => begin
prstr "STMusedef("; prstr "..."; prstr ")"
end end
implement print_stm (stm) = fprint_stm (stdout_ref, stm)
implement prerr_stm (stm) = fprint_stm (stderr_ref, stm)
implement exp_const_0 = EXPconst 0
implement exp_const_1 = EXPconst 1
implement stm_nop = STMexp (exp_const_0)
implement binop_is_additive (binop) =
case+ binop of
| PLUS _ => true | MINUS _ => true | _ => false
implement binop_is_multiplicative (binop) =
case+ binop of
| MUL _ => true | DIV _ => true | _ => false
implement relop_negate (relop) = case+ relop of
| EQ () => NEQ ()
| NEQ () => EQ ()
| GT () => LE ()
| GE () => LT ()
| LT () => GE ()
| LE () => GT ()