Assignment For Compiler Construction
Assignment For Compiler Construction
Q 1. a) Describe all the High Level(Source Code) & Low Level (Target Code) Optimization
Techniques with example in each case.
b) From the following Code Segments, Write the name of the Optimization Technique that
can be applied in each case. And also write the modified code after applying the
optimization technique.
Case 1. Case 2. Case 3. Case 4.
i=0; for ( int j = 0 ; j < n ; j ++) S1 = 4 x i DO I = 1, 100
if (i = = 1) { S2 = a[S1] ARRAY(I) = 2.0 * PI * I
{ x=y+z; S3 = 4 x j
a=x+5; a[j] = 6 x j; S4 = 4 x i ENDDO
} } S5 = n
S6 = b[S4] +
S5
Case 5. Case 6. Case 7.
Auto-decrement MOV R0, index ;; value of index → R0 for(int i=0; i<3; i++)
and auto-increment MUL R0, 2 ;; double value in R0 {
addressing modes. MOV R1, &a ;; address of a → R1 color_map[n+i] = i;
ADD R1, R0 ;; add R0 to R1 }
MOV *R1, 6 ;; constant 6 → address
in R1
Case 8. Case 9. Case 10. Case 11.
X= X+ 0 while (i <= limit-2) int cube(int s)
Or do { return s*s*s; } B=Ax2
X= X*1 {loop code} int main( ) (X)2 = ??
{cout << "The cube
X / 1=?? of 3 is: " << cube(3)
<< "\n";
return 0; }
Q. 2 What are the optimization Pitfalls..
------------------------------------------------------------------------------------------------------------
II. Intermediate Code Generation.
a) E → E + T | E – T | T
T→ T * F | F
F → ( E ) | id | num
int a[10];
char * s= “hello”;
int f(int i, int b[ ]) void g (char * s) main ( )
{ { char c = s[0]; {
int j=i; B: { int a [5]; int x=1;
A: { int i=j; -------- x= f(x, a);
char c = b[i]; -------- g(s);
---------- } return 0;
---------- } } }
return 0
}
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] .