-- Abstract expression trees as an intermediate representation language: module IRTree(Exp(..),Stm(..),BinOp(..),RelOp(..), newlabel,extendlabel,newtemp,extendtemp) where data Exp = CONST Int -- could be extended to other constants, like floats, chars | NAME Label -- target of a jump | TEMP Temp -- temporary value (e.g., register) | BINOP BinOp Exp Exp -- apply binary operator | MEM Exp -- memory access | CALL Exp [Exp] -- procedure call | ESEQ Stm Exp -- expression with side effect deriving Show data Stm = MOVE Exp Exp -- assignment | EXP Exp -- expression as statement | JUMP Exp -- unconditional jump | CJUMP RelOp Exp Exp Label Label -- conditional jump | SEQ Stm Stm -- sequence of statements | LABEL Label -- definition of a label deriving Show data BinOp = PLUS| MINUS | MUL | DIV | MOD | AND | OR | LSHIFT | RSHIFT -- etc deriving Show data RelOp = EQ | NE | LT | GT | LTE | GTE deriving Show ------------------------------------------------------------------ -- Labels: data Label = Label String deriving Show newlabel :: Label -> Int -> Label newlabel (Label l) i = Label (l ++ '$' : show i) extendlabel :: Label -> Int -> Label extendlabel (Label l) i = Label (l ++ '_' : show i) ------------------------------------------------------------------ -- Temporaries: data Temp = Temp String deriving Show newtemp :: Temp -> Int -> Temp newtemp (Temp l) i = Temp (l ++ '$' : show i) extendtemp :: Temp -> Int -> Temp extendtemp (Temp l) i = Temp (l ++ '_' : show i)