Compiler Designing W07L1205

Temporaries (Improvement)

  • 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:

ar

  • The explanation is necessary to understand the concept.

[II]

  • 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.
Advertisements

Help to improve or comment as you wish

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s