COMPILER Design Unit-6

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

1

1)

Explain the process of syntax directed translation of three address code.

Syntax-Directed Translation into Three-Address Code

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.

2) What is an activation tree? Explain its functions.


Every execution of a procedure is called an ACTIVATION.

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

2. h node represents an activation of a procedure.


3. The root represents the activation of the main program.
4. The node for a is the parent of the node b if and only if control flows from activation a to b.
5. The node for 'a' is to the left of the node for 'b' if and only if the life time of 'a' occurs before
the life time of 'b'.
8) What are control stacks?
The flow of control in a program corresponds to a depth first traversal of the activation treethat
starts at the root,visits anode before its children,and recursively visits children at each node in a
left-to-right order.
We can use a stack,called a control stack to keep track of live procedure activations. The idea
is to push the node for an activation onto the control stack and to pop the node when the
activation ends.
When node 'n' is at the top of the control stack,the stack contains the nodes along the path from
'n' to the root.
9)

What are the storage allocation strategies used at Run time?


Storage Allocation
Static allocation
Storage allocation was fixed during the entire execution of a program
Stack allocation

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.

10) Explain with a diagram the run time storage organization.


Run Time Sorage Organization

Code
Static data
Stack

Code
Static Data

Heap
Heap

Virtual
address

Stack

11) What is static allocation?


Static Allocation
.Bindings between names and storage are fixed
The values of local names are retained across activations of a procedure.
Addressing is efficient

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

13) What are calling sequences?


Local b
offset = 48
Procedure calls are implemented by generating what are known as calling sequences in the target
code. A call sequence allocates an activation record and enters information into its fields. A return
sequence restores the state of the machine so the calling procedure can continue execution.
Calling Sequences
.A call sequence allocates an activation record and enters information into its fields
parameters, return address, old stack top, saving registers, local data initialization
A return sequence restores the state of the machine
return value, restore registers, restore old stack top, branch to return address
The code in calling sequence is often divided between the caller and the callee.
14) What are return sequences?
Possible return sequence

Callee places a return value next to the AR of the caller.

Callee restores top-sp and other registers

Callee branches to the return address

Caller can copy return value into its own AR

15) What are non-local names?


In a language with nested procedures (or blocks) and static scope (lexical scope), some names are
neither local nor global, they are non-local names.
procedure A

6
real a;
procedure B
real b;
reference a; non-local
end B
end A;
Example: Non-local names in C

16) Explain in detail


parameter passing.
Parameter Passing
Parameters
Names that appear in the declaration of a procedure are formal parameters.
Variables and expressions that are passed to a procedure are actual parameters (or arguments)
Parameter passing modes
Call by value
Call by reference
Copy-restore
Call by name
Call-by-Value
.The actual parameters are evaluated and their r-values are passed to the called procedure
A procedure called by value can affect its caller either through nonlocal names or through pointers.
Parameters in C are always passed by value. Array is unusual, what is passed by value is a pointer.
Pascal uses pass by value by default, but var parameters are passed by reference.
Call-by-Reference
Also known as call-by-address or call-by-location. The caller passes to the called procedure the lvalue of the parameter.
If the parameter is an expression, then the expression is evaluated in a new location, and the address
of the new location is passed.
Parameters in Fortran are passed by reference

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.

You might also like