open Globals

type bin = 
  Parsetree.bin

type llvm_type =
  | Tint of int
  | Tarray of int * llvm_type
  | Tpointer of llvm_type
  | Tstruct of llvm_type array
  | Tnamedstruct of string

type signature = {
  sig_args : llvm_type array;
  sig_rtyp : llvm_type option
}

type value =
  | VUNDEF
  | VINT of int * int
  | VNULL of llvm_type
  | VVAR of Id.t
  | VLOAD of Id.t

type cexp =
  | VAL of value
  | ALLOCA of bool * llvm_type
  | MALLOC of llvm_type
  | ARRAY_MALLOC of value * value
  | LOAD of value
  | STORE of value * value
  | BINOP of value * bin * value
  | CALL of Id.t * value list
  | EXT_CALL of string * value list
  | GEP of value * value list

type exp =
  | DIE of string
  | LET of Id.t * llvm_type * cexp * exp
  | IF of value * exp * exp
  | RETURN of cexp
  | LET_BLOCK of Id.t * (Id.t * llvm_type) list * exp * exp
  | GOTO of Id.t * value list
  | LET_REC of (Id.t, llvm_type, exp) fundef list * exp

type prog = {
  prog_named : (string * llvm_type list) list;
  prog_strings : (Id.t * string) list;
  prog_body : exp
}

Introduce ANF as intermediate language.
Introduce ANF as intermediate language.

This module Anf will take the place of Typedtree2 later on.

The reasons to introduce a new intermediate language are
varied. The main one is aesthetic. The current intermediate
language is a very inelegant statement oriented language.
In particular the current intermediate language does not get
me access to all the features of the SSA language that underlies
LLVM (such as PHI-instructions). We could have used a language
that was closer to LLVM's, but then it is very hard to mantain
the scoping information for inner functions that we need to
perform Lambda Lifting (that we do after translation into the
intermediate language). The solutions is ANF, Administrative
Normal Form. It is the functional equivalent to SSA. We could have
also used CPS (Continuation Passing Style), but it is simpler to
generate LLVM code from ANF. The key feature is that it provides
for local blocks (like local function definitions, but that can
only be called in tail position). They correspond to basic blocks
in SSA. Its arguments correspond to PHI-nodes. All of this is
nicely explained in A. Appel's paper 'SSA is Functional Programming'.
