Assignment # 5
Assignment # 5
Q 1. a) Describe all the High Level(Source Code) & Low Level (Target Code) Optimization
Techniques with example in each case.
Answer:
Case 1:
Remove I = 0;
Case 2:
x = y + z; // Moved out of the loop since it's invariant.
for (int j = 0; j < n; j++) {
a[j] = 6 * j;
}
Case 3:
S1 = 4 * i;
S2 = a[S1];
S3 = 4 * j;
S4 = S1; // S4 is the same as S1, so reuse S1.
S5 = n;
S6 = b[S1] + S5; // Use S1 instead of recalculating 4 * i.
Case 4:
Case 5:
Case 6:
Case 7:
Case 8:
Case 9:
Case 10:
Case 11:
a) E → E + T | E – T | T
T→ T * F | F
F → ( E ) | id | num
ii. Draw Directed Acyclic Graph for the following expression with the help of semantic
rules given in part (i).
a + a * (b – c) + (b – c) * d
iii. Show the steps for the construction of DAG of Fig. in part(ii) provided Node and Leaf
return an existing node, if possible, as discussed above. We assume that entry-a points
to the symbol table entry a, and similarly for the other identifiers.
int a[10];
char * s= “hello”;
Q 10.
Consider the following C- language code fragment.
void f (int x, char c)
{
int a[10];
double y;
……………..
}
a) Show (i.e. draw) the complete activation record for a call to f in the stack-based runtime
environment.
b) Assuming two bytes for integers, four bytes for addresses, one byte for characters, and eight
bytes for double-precision floating point. Calculate the off-set values for all the four
variables.
-------------------------------------------------------------------------------------------------------
Q 11. Consider the C code of the following figure:
int x = 2;
void g(int); /*prototype */
void f(int n)
{
static int x=1;
g(n);
x--; }
void g(int m)
{ int y= m-1;
if (y > 0)
{
f(y);
x --;
g(y);
}}
main( )
{
g(x);
return 0;
}
Draw the stack based run-time environment for the above C program during first,second
and third call to g.
------------------------------------------------------------------------------------------------------------------
Q. 12
Consider the simple recursive implementation of Euclid’s algorithm to compute the greatest
common divisor of two non-negative integers whose code(in C) is given as follows:
# include <stdio.h>
int x,y;
main ( )
{
scanf( “%d%d”, &x, &y);
Printf(“%d\n”,gcd(x,y));
return 0;
}
Suppose the user inputs the values 15 and 10 to this program, so that main initially makes the
call gcd(15,10). This call results in a second, recursive call gcd(10, 5) (since 15 % 10 =5), and
this results in a third call gcd(5,0) (since 10%5=0), which then returns the value 5.
Note also that the fp points to the control link of the current activation record, so on the next call
the current fp becomes the control link of the next activation record.
Draw the stack-based runtime environment for the above program.
---------------------------------------------------------------------------------------
Q.13
Consider the following procedure in C syntax:
void f ( char c, char s[10], double r )
{
int * x;
int y[5];
------
}
Using the standard C parameter passing conventions, and assuming the data sizes:
integer = 2 bytes, char = 1 byte , double = 8 bytes , address = 4 bytes, determine the offsets
from the fp of the following, using the activation record structure of the runtime environment:
(1) c (2) s[7] (3) y[2] .
--------------------------------------THE END ---------------------------------------------------