0% found this document useful (0 votes)
28 views42 pages

CSC 453 Intermediate Code Generation: Saumya Debray

The document discusses intermediate code generation. It describes how intermediate representations bridge source and target languages by being closer to the target language while still being machine-independent, allowing optimizations. Different types of intermediate languages are presented, including high-level representations like syntax trees and low-level ones like 3-address code. Syntax trees are discussed as an intermediate representation, showing an example and how they are constructed. Three-address code is also described as a low-level intermediate representation, demonstrating an example expression translated to 3-address code. The overall approach of code generation from an intermediate representation is outlined, including traversing the syntax tree and generating code recursively at each node.

Uploaded by

Jitendra Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views42 pages

CSC 453 Intermediate Code Generation: Saumya Debray

The document discusses intermediate code generation. It describes how intermediate representations bridge source and target languages by being closer to the target language while still being machine-independent, allowing optimizations. Different types of intermediate languages are presented, including high-level representations like syntax trees and low-level ones like 3-address code. Syntax trees are discussed as an intermediate representation, showing an example and how they are constructed. Three-address code is also described as a low-level intermediate representation, demonstrating an example expression translated to 3-address code. The overall approach of code generation from an intermediate representation is outlined, including traversing the syntax tree and generating code recursively at each node.

Uploaded by

Jitendra Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

CSc 453

Intermediate Code
Generation
Saumya Debray
The University of Arizona
Tucson
Overview

 Intermediate representations span the gap between


the source and target languages:
 closer to target language;
 (more or less) machine independent;
 allows many optimizations to be done in a machine-independent way.
 Implementable via syntax directed translation, so
can be folded into the parsing process.

CSc 453: Intermediate Code Generation 2


Types of Intermediate Languages
 High Level Representations (e.g., syntax trees):
 closer to the source language
 easy to generate from an input program
 code optimizations may not be straightforward.
 Low Level Representations (e.g., 3-address
code, RTL):
 closer to the target machine;

 easier for optimizations, final code generation;

CSc 453: Intermediate Code Generation 3


Syntax Trees

A syntax tree shows the structure of a program by abstracting


away irrelevant details from a parse tree.
 Each node represents a computation to be performed;

 The children of the node represents what that computation is


performed on.
Syntax trees decouple parsing from subsequent processing.
CSc 453: Intermediate Code Generation 4
Syntax Trees: Example
Parse tree:
Grammar :
EE+T | T
TT*F | F
F  ( E ) | id

Input: id + id * id
Syntax tree:

CSc 453: Intermediate Code Generation 5


Syntax Trees: Structure
 Expressions:
 leaves: identifiers or constants;
 internal nodes are labeled with operators;
 the children of a node are its operands.

 Statements:
 a node’s label indicates what kind of
statement it is;
 the children correspond to the components
of the statement.

CSc 453: Intermediate Code Generation 6


Constructing Syntax Trees
General Idea: construct bottom-up using
synthesized attributes.
E→E+E { $$ = mkTree(PLUS, $1, $3); }

S → if ‘(‘ E ‘)’ S OptElse { $$ = mkTree(IF, $3, $5, $6); }


OptElse → else S { $$ = $2; }
| /* epsilon */ { $$ = NULL; }

S → while ‘(‘ E ‘)’ S { $$ = mkTree(WHILE, $3, $5); }

mkTree(NodeType, Child1, Child2, …) allocates space for the tree node and fills in its node
type as well as its children.
CSc 453: Intermediate Code Generation 7
Three Address Code
 Low-level IR
 instructions are of the form ‘x = y op z,’ where x,
y, z are variables, constants, or “temporaries”.

 At most one operator allowed on RHS, so no


‘built-up” expressions.
Instead, expressions are computed using temporaries
(compiler-generated variables).

CSc 453: Intermediate Code Generation 8


Three Address Code: Example
 Source:
if ( x + y*z > x*y + z)
a = 0;

 Three Address Code:


tmp1 = y*z
tmp2 = x+tmp1 // x + y*z
tmp3 = x*y
tmp4 = tmp3+z // x*y + z
if (tmp2 <= tmp4) goto L
a = 0
L:
CSc 453: Intermediate Code Generation 9
An Intermediate Instruction Set
 Assignment:  Procedure call/return:
 x = y op z (op binary)  param x, k (x is the kth param)
 x = op y (op unary);  retval x
 x=y  call p
 Jumps:  enter p
 if ( x op y ) goto L (L a label);  leave p
 goto L  return
 Pointer and indexed  retrieve x
assignments:  Type Conversion:
 x = y[ z ]  x = cvt_A_to_B y (A, B base types)
 y[ z ] = x e.g.: cvt_int_to_float
 x = &y  Miscellaneous
 x = *y  label L
 *y = x.
CSc 453: Intermediate Code Generation 10
Three Address Code: Representation
 Each instruction represented as a structure called a
quadruple (or “quad”):
 contains info about the operation, up to 3 operands.
 for operands: use a bit to indicate whether constant or ST pointer.
E.g.:
x=y+z if ( x  y ) goto L

CSc 453: Intermediate Code Generation 11


Code Generation: Approach
 function prototypes, global declarations:
 save information in the global symbol table.
 function definitions:
 function name, return type, argument type and number saved
in global table (if not already there);
 process formals, local declarations into local symbol table;
 process body:
 construct syntax tree;
 traverse syntax tree and generate code for the function;
 deallocate syntax tree and local symbol table.

CSc 453: Intermediate Code Generation 12


Code Generation: Approach
Recursively traverse syntax tree:
 Node type determines action at each node;
 Code for each node is a (doubly linked) list of three-address instructions;
 Generate code for each node after processing its children

codeGen_stmt(synTree_node S) codeGen_expr(synTree_node E)
{ {
switch (S.nodetype) { switch (E.nodetype) {
case FOR: … ; break; case ‘+’: … ; break;
case WHILE : … ; break; case ‘*’ : … ; break;
case IF: … ; break; case ‘–’: … ; break;
case ‘=‘ : … ; break; case ‘/’ : … ; break;
… …
} } recursively process the children,
then generate code for this node
and glue it all together.

CSc 453: Intermediate Code Generation 13


Intermediate Code Generation
Auxiliary Routines:
 struct symtab_entry *newtemp(typename t)
creates a symbol table entry for new temporary variable each
time it is called, and returns a pointer to this ST entry.
 struct instr *newlabel()
returns a new label instruction each time it is called.
 struct instr *newinstr(arg1, arg2, …)
creates a new instruction, fills it in with the arguments supplied,
and returns a pointer to the result.

CSc 453: Intermediate Code Generation 14


Intermediate Code Generation…
 struct symtab_entry *newtemp( t )
{
struct symtab_entry *ntmp = malloc( … ); /* check: ntmp == NULL? */
ntmp->name = …create a new name that doesn’t conflict…
ntmp->type = t;
ntmp->scope = LOCAL;
return ntmp;
}
 struct instr *newinstr(opType, src1, src2, dest)
{
struct instr *ninstr = malloc( … ); /* check: ninstr == NULL? */
ninstr->op = opType;
ninstr->src1 = src1; ninstr->src2 = src2; ninstr->dest = dest;
return ninstr;
}

CSc 453: Intermediate Code Generation 15


Intermediate Code for a Function
Code generated for a function f:
 begin with ‘enter f ’, where f is a pointer to the function’s
symbol table entry:
 this allocates the function’s activation record;
 activation record size obtained from f ’s symbol table information;
 this is followed by code for the function body;
 generated using codeGen_stmt(…) [to be discussed soon]
 each return in the body (incl. any implicit return at the end of
the function body) are translated to the code
leave f /* clean up: f a pointer to the function’s symbol table entry */
return /* + associated return value, if any */
CSc 453: Intermediate Code Generation 16
Simple Expressions
Syntax tree node for expressions augmented with the
following fields:
 type: the type of the expression (or “error”);
 code: a list of intermediate code instructions for evaluating the expression.
 place: the location where the value of the expression will be kept at runtime:

CSc 453: Intermediate Code Generation 17


Simple Expressions
Syntax tree node for expressions augmented with the
following fields:
 type: the type of the expression (or “error”);
 code: a list of intermediate code instructions for evaluating the expression.
 place: the location where the value of the expression will be kept at runtime:

 When generating intermediate code, this just refers to a symbol table entry for a
variable or temporary that will hold that value;

 The variable/temporary is mapped to an actual memory location when going from


intermediate to final code.

CSc 453: Intermediate Code Generation 18


Simple Expressions 1

Syntax tree node E Action during intermediate code generation

codeGen_expr(E)
E intcon { /* E.nodetype == INTCON; */
E.place = newtemp(E.type);
E.code = ‘E.place = intcon.val’;
}

codeGen_expr(E)
E id { /* E.nodetype == ID; */
/* E.place is just the location of id (nothing more to do) */
E.code = NULL;
}

CSc 453: Intermediate Code Generation 19


Simple Expressions 2
Syntax tree node E Action during intermediate code generation

codeGen_expr(E)
E –
{
/* E.nodetype == UNARY_MINUS */
codeGen_expr(E1); /* recursively traverse E1, generate code for it */
E.place = newtemp( E.type ); /* allocate space to hold E’s value */
E.code = E1.code  newinstr(UMINUS, E1.place, NULL, E.place);
E1
}
codeGen_expr(E)
E + {
/* E.nodetype == ‘+’ … other binary operators are similar */
codeGen_expr(E1);
codeGen_expr(E2); /* generate code for E1 and E2 */
E.place = newtemp( E.type ); /* allocate space to hold E’s value */
E1 E2 E.code = E1.code  E2.code  newinstr(PLUS, E1.place, E2.place, E.place );
}

CSc 453: Intermediate Code Generation 20


Accessing Array Elements 1
 Given:
 an array A[lo…hi] that starts at address b;
 suppose we want to access A[ i ].
 We can use indexed addressing in the
intermediate code for this:
 A[ i ] is the (i + lo)th array element starting from address b.
 Code generated for A[ i ] is:
t1 = i + lo
t2 = A[ t1 ] /* A being treated as a 0-based array at this level. */

CSc 453: Intermediate Code Generation 21


Accessing Array Elements 2
 In general, address computations can’t be
avoided, due to pointer and record types.
 Accessing A[ i ] for an array A[lo…hi] starting at
address b, where each element is w bytes wide:
Address of A[ i ] is b + ( i – lo )  w
= (b – lo  w) + i  w
= kA + i  w.
kA depends only on A, and is known at compile time.
 Code generated:
t1 = i  w
t2 = kA + t1 /* address of A[ i ] */
t3 = t2
CSc 453: Intermediate Code Generation 22
Accessing Structure Fields
 Use the symbol table to store information about the
order and type of each field within the structure.
 Hence determine the distance from the start of a struct to each field.
 For code generation, add the displacement to the base address of the
structure to get the address of the field.
 Example: Given
struct s { … } *p;

x = pa; /* a is at displacement a within struct s */
The generated code has the form:
t1 = p + a /* address of pa */
x = t1

CSc 453: Intermediate Code Generation 23


Assignments
codeGen_stmt(S):
S: =
/* base case: S.nodetype = ‘S’ */
codeGen_expr(LHS);
codeGen_expr(RHS);
LHS RHS S.code = LHS.code
 RHS.code
 newinstr(ASSG,
Code structure: LHS.place,
evaluate LHS RHS.place) ;

evaluate RHS
copy value of RHS into LHS

CSc 453: Intermediate Code Generation 24


Logical Expressions 1
 Syntax tree node: relop

E1 E2

 Naïve but Simple Code (TRUE=1, FALSE=0):


t1 = { evaluate E1
t2 = { evaluate E2
t3 = 1 /* TRUE */
if ( t1 relop t2 ) goto L
t3 = 0 /* FALSE */
L: …
 Disadvantage: lots of unnecessary memory references.

CSc 453: Intermediate Code Generation 25


Logical Expressions 2
 Observation: Logical expressions are used
mainly to direct flow of control.
 Intuition: “tell” the logical expression where to
branch based on its truth value.
 When generating code for B, use two inherited attributes,
trueDst and falseDst. Each is (a pointer to) a label
instruction.
E.g.: for a statement if ( B ) S1 else S2 :

B.trueDst = start of S1
B.falseDst = start of S2
 The code generated for B jumps to the appropriate label.
CSc 453: Intermediate Code Generation 26
Logical Expressions 2: cont’d
Syntax tree:
codeGen_bool(B, trueDst, falseDst):
relop /* base case: B.nodetype == relop */
B.code = E1.code
 E2.code
 newinstr(relop, E1.place, E2.place, trueDst)
E1 E2
 newinstr(GOTO, falseDst, NULL, NULL);

Example: B  x+y > 2*z.


Suppose trueDst = Lbl1, falseDst = Lbl2.

E1  x+y, E1.place = tmp1, E1.code   ‘tmp1 = x + y’ 


E2  2*z, E2.place = tmp2, E2.code   ‘tmp2 = 2 * z’ 
B.code = E1.code  E2.code  ‘if (tmp1 > tmp2) goto Lbl1’  goto Lbl2
=  ‘tmp1 = x + y’ , ‘tmp2 = 2 * z’, ‘if (tmp1 > tmp2) goto Lbl1’ , goto Lbl2 
CSc 453: Intermediate Code Generation 27
Short Circuit Evaluation
&& codeGen_bool (B, trueDst, falseDst):
/* recursive case 1: B.nodetype == ‘&&’ */
L1 = newlabel( );
codeGen_bool(B1, L1, falseDst);
B1 B2
codeGen_bool(B2, trueDst, falseDst);
B.code = B1.code  L1  B2.code;

codeGen_bool (B, trueDst, falseDst):


|| /* recursive case 2: B.nodetype == ‘||’ */
L1 = newlabel( );
codeGen_bool(B1, trueDst, L1);
B1 B2 codeGen_bool(B2, trueDst, falseDst);
B.code = B1.code  L1  B2.code;

CSc 453: Intermediate Code Generation 28


Conditionals
Syntax Tree: codeGen_stmt(S):
S: if /* S.nodetype == ‘IF’ */
Lthen = newlabel();
Lelse = newlabel();
Lafter = newlabel();
B S1 S2 codeGen_bool(B, Lthen , Lelse);
codeGen_stmt(S1);
 Code Structure: codeGen_stmt(S2);
S.code = B.code
code to evaluate B
 Lthen
Lthen: code for S1
 S1.code
goto Lafter
 newinstr(GOTO, Lafter)
Lelse: code for S2  Lelse
Lafter : …  S2.code
 Lafter ;

CSc 453: Intermediate Code Generation 29


Loops 1
S: while codeGen_stmt(S):
/* S.nodetype == ‘WHILE’ */
Ltop = newlabel();
B S1 Lbody = newlabel();
Lafter = newlabel();
codeGen_bool(B, Lbody, Lafter);
Code Structure: codeGen_stmt(S1);
Ltop : code to evaluate B S.code = Ltop
if ( !B ) goto Lafter  B.code
Lbody: code for S1  Lbody
goto Ltop  S1.code
 newinstr(GOTO, Ltop)
Lafter: …
 Lafter ;

CSc 453: Intermediate Code Generation 30


Loops 2
S: while codeGen_stmt(S):
/* S.nodetype = ‘WHILE’ */
Ltop = newlabel();
B S1 Leval = newlabel();
Lafter = newlabel();
codeGen_bool(B, Ltop, Lafter);
Code Structure: codeGen_stmt(S1);
goto Leval S.code =
Ltop : newinstr(GOTO, Leval)
code for S1  Ltop
Leval: code to evaluate B  S1.code
if ( B ) goto Ltop  Leval
Lafter:  B.code
This code executes fewer branch ops.  Lafter ;

CSc 453: Intermediate Code Generation 31


Multi-way Branches: switch statements
 Goal:
generate code to (efficiently) choose amongst a fixed set of alternatives
based on the value of an expression.
 Implementation Choices:
 linear search
 best for a small number of case labels ( 3 or 4)
 cost increases with no. of case labels; later cases more expensive.
 binary search
 best for a moderate number of case labels ( 4 – 8)
 cost increases with no. of case labels.
 jump tables
 best for large no. of case labels ( 8)
 may take a large amount of space if the labels are not well-clustered.
CSc 453: Intermediate Code Generation 32
Background: Jump Tables
 A jump table is an array of code addresses:
 Tbl[ i ] is the address of the code to execute if the expression evaluates
to i.
 if the set of case labels have “holes”, the correspond jump table entries
point to the default case.
 Bounds checks:
 Before indexing into a jump table, we must check that the expression
value is within the proper bounds (if not, jump to the default case).
 The check
lower_bound  exp_value  upper bound
can be implemented using a single unsigned comparison.

CSc 453: Intermediate Code Generation 33


Jump Tables: cont’d
 Given a switch with max. and min. case labels cmax
and cmin, the jump table is accessed as follows:

Instruction Cost (cycles)


t0  value of expression …
t0 = t0 – cmin 1
if (t0 u cmax – cmin) goto DefaultCase 4 to 6
t1 = JmpTbl_BaseAddr 1
t1 += 4*t0 1
jmp *t1 3 to 5
: 10 to 14
CSc 453: Intermediate Code Generation 34
Jump Tables: Space Costs
 A jump table with max. and min. case labels
cmax and cmin needs  cmax – cmin entries.
This can be wasteful if the entries aren’t “dense enough”, e.g.:
switch (x) {
case 1: …
case 1000: …
case 1000000: …
}

 Define the density of a set of case labels as


density = no. of case labels / (cmax – cmin )
 Compilers will not generate a jump table if
density below some threshold (typically, 0.5).
CSc 453: Intermediate Code Generation 35
Switch Statements: Overall Algorithm
 if no. of case labels is small ( ~ 8), use
linear or binary search.
 use no. of case labels to decide between the two.
 if density  threshold (~ 0.5) :
 generate a jump table;
else :
 divide the set of case labels into sub-ranges s.t. each sub-range
has density  threshold;
 generate code to use binary search to choose amongst the sub-
ranges;
 handle each sub-range recursively.

CSc 453: Intermediate Code Generation 36


Function Calls
 Caller:
 evaluate actual parameters, place them where the callee
expects them:
 param x, k /* x is the kth actual parameter of the call */
 save appropriate machine state (e.g., return address) and
transfer control to the callee:
 call p
 Callee:
 allocate space for activation record, save callee-saved
registers as needed, update stack/frame pointers:
 enter p

CSc 453: Intermediate Code Generation 37


Function Returns
 Callee:
 restore callee-saved registers; place return value (if any)
where caller can find it; update stack/frame pointers:
 retval x;
 leave p
 transfer control back to caller:
 return
 Caller:
 save value returned by callee (if any) into x:
 retrieve x

CSc 453: Intermediate Code Generation 38


Function Call/Return: Example
 Source: x = f(0, y+1) + 1;
 Intermediate Code: Caller:
t1 = y+1
param t1, 2
param 0, 1
call f
retrieve t2
x = t2+1
 Intermediate Code: Callee:
enter f /* set up activation record */
… /* code for f’s body */
retval t27 /* return the value of t27 */
leave f /* clean up activation record */
return
CSc 453: Intermediate Code Generation 39
Intermediate Code for Function Calls
 non-void return type: codeGen_expr(E):
/* E.nodetype = FUNCALL */
E call codeGen_expr_list(arguments);
E.place = newtemp( f.returnType );
arguments E.code = …code to evaluate the arguments…
f (sym. tbl. ptr)
(list of expressions)  param xk

Code Structure:  param x1
 call f, k
… evaluate actuals …
 retrieve E.place;
param xk
… R-to-L
param x1
call f
retrieve t0 /* t0 a temporary var */

CSc 453: Intermediate Code Generation 40


Intermediate Code for Function Calls
 void return type: codeGen_stmt(S):
/* S.nodetype = FUNCALL */
S call codeGen_expr_list(arguments);
E.place = newtemp( f.returnType );
arguments S.code = …code to evaluate the arguments…
f (sym. tbl. ptr)
(list of expressions)  param xk

Code Structure:  param x1
 call f, k
… evaluate actuals …
 retrieve E.place;
param xk
… R-to-L
param x1 void return type  f has no return value
call f  no need to allocate space for one, or
retrieve t0 /* t0 a temporary var */ to retrieve any return value.

CSc 453: Intermediate Code Generation 41


Reusing Temporaries
Storage usage can be reduced considerably by
reusing space for temporaries:
 For each type T, keep a “free list” of temporaries of type T;
 newtemp(T) first checks the appropriate free list to see if it
can reuse any temps; allocates new storage if not.
 putting temps on the free list:
 distinguish between user variables (not freed) and compiler-
generated temps (freed);
 free a temp after the point of its last use (i.e., when its value is no
longer needed).

CSc 453: Intermediate Code Generation 42

You might also like