Code generator with limited registers

def codegenExp( e : Expr, target : Register ) = 
  if e is of the form
    Ident( x ) then 
      List ( I_Load ( target, x ) )
    Const( n ) then  
      List ( I_LoadImm ( target, n ) ) 
    Binop( lhs, op, rhs ) then 
      if ( target < maxRegs-1 ) 
        codegenExp ( rhs, target ) ++
        codegenExp ( lhs, target+1 ) ++
        codegenBinop ( op, target, target+1 ) 
      else 
        codegenExp ( rhs, target ) ++
        List ( I_Push ( target ) ) ++
        codegenExp ( lhs, target ) ++
        codegenBinopStack ( op, target ) 
    ...
def codegenBinop( op : Op, r1 : Register, r2 : Register ) = 
  if op is of the form
   Plus () then List ( I_Plus ( r1, r2 ) )
   Minus () then List ( I_Minus ( r1, r2 ) )
   ...

def codegenBinopStack( op : Op, r : Register ) =
  if op is of the form 
   Plus () then List ( I_PlusStack ( r ) )
   Minus () then List ( I_MinusStack ( r ) )
    ...

Note that this 'merges' the machine language of the register machine with the language of the accumulator machine. In the tutorials, you will be asked to make this precise.

Semantics of the new commands (example)

For example the new command

PlusStack r 

simply does

r := r + mem ( SP );
SP := ( SP + 1 ) % maxMem