COMPILER Design Unit-6
COMPILER Design Unit-6
COMPILER Design Unit-6
1)
When three-address code is generated, temporary names are made up for the interior nodes of a
syntax tree. The value of non-terminal E on the left side of E E1 + E will be computed into a new
temporary t. In general, the three- address code for id: = E consists of code to evaluate E into some
temporary t, followed by the assignment id.place: = t. If an expression is a single identifier, say y,
then y itself holds the value of the expression. For the moment, we create a new name every time a
temporary is needed; techniques for reusing temporaries are given in Section S.3. The S-attributed
definition in Fig. 8.6 generates three-address code for assignment statements. Given input a: = b+ c
+ b+ c, it produces the code in Fig. 8.5(a). The synthesized attribute S.code represents the threeaddress code for the assignment S. The non-terminal E has two attributes:
1. E.place, the name that will hold the value of E, and
2. E.code, the sequence of three-address statements evaluating E.
The function newtemp returns a sequence of distinct names t1, t2,... in response to successive calls.
For convenience, we use the notation gen(x : = y + z) in Fig. 8.6 to represent the three-address
statement x: = y + z. Expressions appearing instead of variables like x, y, and z are evaluated when
passed to gen, and quoted operators or operands, like +, are taken literally. In practice, threeaddress statements might be sent to an output file, rather than built up into the code attributes. Flowof-control statements can be added to the language of assignments in Fig. 8.6 by productions and
semantic rules )like the ones for while statements in Fig. 8.7. In the figure, the code for S - while E do
S, is generated using new attributes S.begin and S.after to mark the first statement in the code for E
and the statement following the code for S, respectively.
3
We can use a tree,called an activation tree to depict the way control enters and leaves activations.
The LIFETIME of an activation of procedure P is the sequence of steps between the first and last
steps of Ps body, including any procedures called while P is running.
Normally, when control flows from one activation to another, it must (eventually) return to the same
activation.
When activations are thusly nested, we can represent control flow with ACTIVATION TREES.
In an activation tree ,
1. Eac
Space is pushed and popped on a run-time stack during program execution such as procedure calls
and returns.
Heap allocation
Allow space to be allocated and freed at any time.
Code
Static data
Stack
Code
Static Data
Heap
Heap
Virtual
address
Stack
5
Limitations:
No recursive procedures
No dynamic data structures
12) What is stack allocation?
Stack Allocation
.Recursive procedure require stack allocation
Activation records (AR) are pushed and popped as activations begin and end.
The offset of each local data relative to the beginning of AR is stored in the symbol
table.
float f(int k)
{
float c[10],b;
b = c[k]*3.14;
return b;
}
Return value
offset = 0
Parameter k
offset = 4
Local c[10]
offset = 8
6
real a;
procedure B
real b;
reference a; non-local
end B
end A;
Example: Non-local names in C
7
an old implementation bug in Fortran
func(a,b) { a = b};
call func(3,4); print(3);
Copy-Restore
A hybrid between call-by-value and call-by reference.
The actual parameters are evaluated and their r-values are passed as in call-by-value. In addition, lvalues are determined before the call.
When control returns, the current r-values of the formal parameters are copied back into the l-values
of the actual parameters.
Call-by-Name
The actual parameters literally substituted for the formals. This is like a macro-expansion or in-line
.expansion
Call-by-name is not used in practice. However, the conceptually related technique of in-line
expansion is commonly used.
In-lining may be one of the most effective optimization transformations if they are guided by
execution profiles.