I am working on a hobby retargetable C compiler in OCaml and I'm building it bottom up. So far I have an annotated AST type, abridged:
type 'e expr =
| Int of 'e * int
| Var of 'e * var
| Neg of 'e * 'e expr
| Add of 'e * 'e expr * 'e expr
| Sub of 'e * 'e expr * 'e expr
and a three-address code type (again abridged):
type node = Copy of location * location
| Unary of location * unary_op * location
| Binary of location * location * binary_op * location
and location = Temp of int | Int of int | Var of string
and unary_op = Neg
and binary_op = Add | Sub
I have functions written that will transform an AST into a list of TAC nodes ignoring the annotations. Regarding this I have two questions:
What do I do differently when transforming a type-annotated AST to a list of TAC nodes? Should I add annotations to TAC nodes too? This would allow me to later transform high level
int
/char
types into lower-level ones likeI16
/I8
.How do I handle scoping? What if I have two
Var
s that have the same name in different scopes?