- Remember the case for adding two expressions e1 and e2 it used one temporary register to reload the value of e1. This temporary register can be reused by other expressions.
- The number of temporary for an expression can be calculated at the compil time, hence memory in AR is reserved for the number of temporaries. Some formula for counting number of temporaries are:(NT: number of temporaries)
NT(e + e)= max(NT(e), 1+ NT(e)) NT(e - e)= max(NT(e), 1+ NT(e)) NT(if a=b then c else d)= max(NT(a=b), NT(c), NT(d)) //NT(a=b)=max(NT(a), 1 + NT(b)) NT(id(arg1, ... argn))=max(NT(arg1), NT(arg2)...NT(argn)) NT(int)=0 NT(id)=0
- Including the temporary changes the structure of AR as:
- The explanation is necessary to understand the concept.
- The a + b assembly code with this structure becomes:
cgen(a + b, nt)= cgen(a, nt) sw $a0 nt($fp) cgen(b, nt+4) lw $t1 nt($fp) add $a0 $t1 $a0
- A code generator should know how many temporaries are in use so that the empty temporary can be used. Then temporary area is also a stack and it’s small and fixed-size.