Add the tiger source code bundle from the book site

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2024-12-18 15:18:45 -08:00
parent 915660c8a7
commit 33d8bac511
87 changed files with 3252 additions and 0 deletions

64
tiger/chap7/printtree.sml Normal file
View file

@ -0,0 +1,64 @@
structure Printtree :
sig val printtree : TextIO.outstream * Tree.stm -> unit end =
struct
structure T = Tree
fun printtree (outstream, s0) =
let fun say s = TextIO.output(outstream,s)
fun sayln s= (say s; say "\n")
fun indent 0 = ()
| indent i = (say " "; indent(i-1))
fun stm(T.SEQ(a,b),d) =
(indent d; sayln "SEQ("; stm(a,d+1); sayln ","; stm(b,d+1); say ")")
| stm(T.LABEL lab, d) = (indent d; say "LABEL "; say (Symbol.name lab))
| stm(T.JUMP (e,_), d) = (indent d; sayln "JUMP("; exp(e,d+1); say ")")
| stm(T.CJUMP(r,a,b,t,f),d) = (indent d; say "CJUMP(";
relop r; sayln ",";
exp(a,d+1); sayln ","; exp(b,d+1); sayln ",";
indent(d+1); say(Symbol.name t);
say ","; say (Symbol.name f); say ")")
| stm(T.MOVE(a,b),d) = (indent d; sayln "MOVE("; exp(a,d+1); sayln ",";
exp(b,d+1); say ")")
| stm(T.EXP e, d) = (indent d; sayln "EXP("; exp(e,d+1); say ")")
and exp(T.BINOP(p,a,b),d) = (indent d; say "BINOP("; binop p; sayln ",";
exp(a,d+1); sayln ","; exp(b,d+1); say ")")
| exp(T.MEM(e),d) = (indent d; sayln "MEM("; exp(e,d+1); say ")")
| exp(T.TEMP t, d) = (indent d; say "TEMP t"; say(Int.toString t))
| exp(T.ESEQ(s,e),d) = (indent d; sayln "ESEQ("; stm(s,d+1); sayln ",";
exp(e,d+1); say ")")
| exp(T.NAME lab, d) = (indent d; say "NAME "; say (Symbol.name lab))
| exp(T.CONST i, d) = (indent d; say "CONST "; say(Int.toString i))
| exp(T.CALL(e,el),d) = (indent d; sayln "CALL("; exp(e,d+1);
app (fn a => (sayln ","; exp(a,d+2))) el;
say ")")
and binop T.PLUS = say "PLUS"
| binop T.MINUS = say "MINUS"
| binop T.MUL = say "MUL"
| binop T.DIV = say "DIV"
| binop T.AND = say "AND"
| binop T.OR = say "OR"
| binop T.LSHIFT = say "LSHIFT"
| binop T.RSHIFT = say "RSHIFT"
| binop T.ARSHIFT = say "ARSHIFT"
| binop T.XOR = say "XOR"
and relop T.EQ = say "EQ"
| relop T.NE = say "NE"
| relop T.LT = say "LT"
| relop T.GT = say "GT"
| relop T.LE = say "LE"
| relop T.GE = say "GE"
| relop T.ULT = say "ULT"
| relop T.ULE = say "ULE"
| relop T.UGT = say "UGT"
| relop T.UGE = say "UGE"
in stm(s0,0); sayln ""; TextIO.flushOut outstream
end
end

11
tiger/chap7/temp.sig Normal file
View file

@ -0,0 +1,11 @@
signature TEMP =
sig
eqtype temp
val newtemp : unit -> temp
structure Table : TABLE sharing type Table.key = temp
val makestring: temp -> string
type label = Symbol.symbol
val newlabel : unit -> label
val namedlabel : string -> label
end

24
tiger/chap7/temp.sml Normal file
View file

@ -0,0 +1,24 @@
(* make this an abstraction sometime *)
structure Temp : TEMP =
struct
type temp = int
val temps = ref 100
fun newtemp() = let val t = !temps in temps := t+1; t end
structure Table = IntMapTable(type key = int
fun getInt n = n)
fun makestring t = "t" ^ Int.toString t
type label = Symbol.symbol
local structure F = Format
fun postinc x = let val i = !x in x := i+1; i end
val labs = ref 0
in
fun newlabel() = Symbol.symbol(F.format "L%d" [F.INT(postinc labs)])
val namedlabel = Symbol.symbol
end
end

58
tiger/chap7/tree.sml Normal file
View file

@ -0,0 +1,58 @@
signature TREE =
sig
type label = Temp.label
type size
datatype stm = SEQ of stm * stm
| LABEL of label
| JUMP of exp * label list
| CJUMP of relop * exp * exp * label * label
| MOVE of exp * exp
| EXP of exp
and exp = BINOP of binop * exp * exp
| MEM of exp
| TEMP of Temp.temp
| ESEQ of stm * exp
| NAME of label
| CONST of int
| CALL of exp * exp list
and binop = PLUS | MINUS | MUL | DIV
| AND | OR | LSHIFT | RSHIFT | ARSHIFT | XOR
and relop = EQ | NE | LT | GT | LE | GE
| ULT | ULE | UGT | UGE
val notRel : relop -> relop
val commute: relop -> relop
end
structure Tree : TREE =
struct
type label=Temp.label
type size = int
datatype stm = SEQ of stm * stm
| LABEL of label
| JUMP of exp * label list
| CJUMP of relop * exp * exp * label * label
| MOVE of exp * exp
| EXP of exp
and exp = BINOP of binop * exp * exp
| MEM of exp
| TEMP of Temp.temp
| ESEQ of stm * exp
| NAME of label
| CONST of int
| CALL of exp * exp list
and binop = PLUS | MINUS | MUL | DIV
| AND | OR | LSHIFT | RSHIFT | ARSHIFT | XOR
and relop = EQ | NE | LT | GT | LE | GE
| ULT | ULE | UGT | UGE
end