C Ompiler Theory: (Intermediate C Ode Generation - Abstract S Yntax + 3 Address C Ode)
C Ompiler Theory: (Intermediate C Ode Generation - Abstract S Yntax + 3 Address C Ode)
006
Why intermediate code ?
Three-Address Code
Made up of instructions of the general form x=y op z
X, y and z are the three addresses.
C
Often used as an intermediate representation. e.g.
LOTOS language
Directed Acyclic Graphs (i)
An address can be
A name : source program names
In an implementation we would have these
as pointers pointing towards the symbol
table.
A constant
A compiler-generated temporary
To store temporary results
Addresses and Instructions (i)
Assignment instructions
x = y op z where op is binary
x = op y where op is unary (-, !, type casting)
Copy Instructions
x=y
Jumps
Unconditional : goto L
Conditional :
if x goto L
ifFalse x goto L
if x relop y goto L
Addresses and Instructions (ii)
Consists of
Intermediate Code produced by front end
Symbol Table to determine the runtime addresses
of the data objects denoted by the names in the
intermediate representation
LD dst, addr
Loads the value in location addr into
location dst.
dst = addr
LD r, x loads the value at addr x into r
LD r1, r2 loads the contents of register r2
into register r1
Store operations
ST x, r
Stored the value in register r into the
location x.
This instruction denotes the assignment
x = r.
Note difference from LD
Computation Operators
BR L
Causes control to branch to the machine
instruction with label L
BR stands for branch
Conditional Jumps
Bcond r, L
Where r is a register and L is a label
Cond stands for any of the of the common
tests on values eg. LT, GT, etc
BLTZ r, L causes a jump to label L if the
value in register r is less than zero, and
allows control to pass to the next machine
instruction if not.
3AC to machine code (x=y-z)
LD R1, y // R1 = y
LD R2, z // R2 = z
SUB R1, R1, R2 // R1 = R1 - R2
ST x, R1 // x = R1
3AC to machine code (if x<y goto L)
LD R1, x // R1 = x
LD R2, y // R2 = y
SUB R1, R1, R2 // R1 = R1 - R2
BLTZ R1, M // if R1<0 jump M
Instruction Selection
Loop optimisation
Loops are an important place where optimisations may
occur. Clearly we try to reduce the number of
instructions inside the loop ! One option is to use code
motion ( and maintain semantics )
This transformation takes out of the loop any
expressions that have the same evaluation
independent of the number of times the loop executes
(loop-invariant computation) and places it before the
loop.
e.g. while (i <= (limit-1)) .... limit – 1 can be
computed before the loop !! i <= t where t = limit-1