0% found this document useful (0 votes)
9 views

Intermediate Code Generation

Uploaded by

shvdo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Intermediate Code Generation

Uploaded by

shvdo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 59

Intermediate Code

generation
Intermediate Representations
Many ways to represent intermediate code
Three-Address Code (TAC)
• Each instruction can have at most three operands
• Large statements are broken into little operations that use temporary
variables
X=(2+3)+4
turns into to
T1=2+3;
X=T1+4;
• Exact instruction set might differ depending on
• Source language being compiled
• Target machine language
TAC instruction examples
• Assignment statements of the form x: = y op z, where op is a
binary arithmetic or logical operation
• . Assignment instructions of the form x:= op y, where op is a
unary operation
• Copy statements of the form x: = y
• The unconditional jump goto L
• Conditional jumps ifz value goto label
• Indexed assignments of the form x: = y[ i ] and x [ i ]: = y.
• Address and pointer assignments of the form x:= &y, x:= *y,
*x: = y
TAC instruction for Function calls
• A call of the function p(x1,, x2,..., xn) is implanted using the instructions
param x
call p, n //p is function name and n is # of parameters
return y
BeginFunc N // instruction reserving N bytes of space for locals and temporaries
EndFunc // When reached, cleans up stack frame and returns
• Afunction call is often translated into the sequence
param x1
param x2

param xn
call p, n
TAC examples
TAC examples
TAC examples
TAC examples
Compiling Functions
Consists of four pieces:
• A label identifying the start of the function
• A BeginFunc N; instruction reserving N bytes of
space for locals and temporaries
• The body of the function.
• An EndFunc; instruction marking the end of the
function. – When reached, cleans up stack frame
and returns.
TAC examples
TAC examples
TAC examples
TAC examples
TAC examples
TAC generation
TAC generation for Arithmetic Expressions
TAC generation for Basic Expressions Cont.
E  constant E.place  newtemp();
E.code  gen(E.place,':=' , constant.value);
Example
TAC Generation for Boolean Expressions

– Use 1 to represent true, use 0 to represent false


– store this result in a temporary
TAC for Boolean Expressions
TAC for While Statement
TAC for while statement
S → while E do S1
S.begin := newlabel()
S.after := newlabel()
S.code := gen(S.begin ‘:’) ||
E.code ||
gen(ifz E.place goto S.after) ||
S1.code ||
gen(‘goto’ S.begin) ||
gen(S.after ‘:’)
TAC for if else statement
S → if E then S1 else S2

S.false_branch := newlabel()
S.after := newlabel()
S.code :=E.code ||
gen(ifz E.place goto S.false_branch) ||
S1.code ||
gen('goto' S.after) ||
gen(S.false_branch ':') ||
S2.code ||
gen(S.after ':')
TAC for statement sequence
S  S1; S2

S.code := S1.code || S2.code


Translation Scheme for Memory Allocation
P → D {offset := 0;}
D → D; D
D → id : T {enter(id.name,T.type, offset); offset := offset + T.width; }
T → char { T.type := char; T.width := 4; }
| int { T.type := integer; T.width := 4; }
| float { T.type := float; T.width := 8; }
| array[num] of T1 {T.type := array(num.val, T1.type); T.width:=num.val* T1.width; }
| pointer T { T.type ← pointer(T1.type); T.width ← 8; }
Computing an Array Address
TAC for Function Calls
Logical Stack Frame
(Stack grows downwards)
Stack Management in TAC
Compiling Functions
Suppose f calls g.
The caller f pushes the parameters on the stack one by one into g frame.
Compiling Functions Cont.
Then, the callee, g, reserves room for its local variables
and temporaries using BeginFunc N; instruction:
Compiling Functions Cont.
When the callee, g, ends, it pops its local variables and temporaries using
EndFunc; instruction which frees all memory allocated previously using
BeginFunc N;
Compiling Functions Cont.
Then, space of parameters of the callee, g, is deallocated
by the caller f using PopParams N; instruction:
Compiling Function calls Example
Compiling Function calls Example Cont.
Storage Allocation

● As described so far, TAC does not


specify where variables and temporaries
are stored
● For code generation, we will need to
tell the code generator where each
variable should be stored
The Frame Pointer
The frame pointer contains the address of the frame of the function
being currently executed:
The Frame Pointer
When calling another function, the frame pointer is modified and it points to the
frame of the callee:
The Frame Pointer
After the callee finishes execution, the frame of the callee is popped and the
frame pointer return to point to the frame of the caller:
Logical vs Physical Stack Frames

• A frame contains also a pointer to the frame of the caller.


• This pointer and is used to update the frame pointer once the current function
completes execution and its frame is popped. This is necessary to execute a
sequence of function calls
Logical vs Physical Stack Frames
(Mostly) Physical Stack Frames
(Mostly) Physical Stack Frames
On a function call
(Mostly) Physical Stack Frames
When the callee finishes execution:
The Stored Return Address
• Internally, the processor has a special register called the
program counter (PC) that stores the address of the next
instruction to execute.
• Whenever a function returns, it needs to restore the PC so
that the calling function resumes execution where it left off.
• The address of where to return is stored in MIPS in a special
register called ra (“return address.”)
• To allow MIPS functions to call one another, each function
needs to store the ra inside the caller
• Upon function completion, the ra in the frame of the function
is copied into PC
Physical Stack Frames
On a function call:
Assigning Locations to Variables
• In the code generator, we must assign
each local variable, parameter, and
temporary variable its own location.
• These locations occur in a particular
stack frame and are called fp-relative.
• Parameters begin at address fp + 4
and grow upward.
• Locals and temporaries begin at
address fp – 8 and grow downward
Global Variables
The Global Pointer
Summary of Memory Layout
•Most details abstracted away by IR format.
•Parameters start at fp + 4 and grow upward.
•Locals start at fp – 8 and grow downward.
•Globals start at gp + 0 and grow upward.
•We need to write code to assign variables
to these locations.
Access Link
• Stored in the AR
• Contains the address of the activation record of the enclosing procedure
• Used to access non-local variables during execution and is also used to map these
variables to logical addresses
Variable Addressability
Local variables
– ARP(or FP) becomes the base address
– Convert to static distance coordinate and use
ARP + offset
Non-local Variables (from other procedures)
– Find the right AR by following Access Link(s)
– Use that ARP + offset
Introduction to Translation into Stack Machine Code
Stack Machine Code
Intermediate Code
Stack Based Code Generation
Example

You might also like