Nothing Special   »   [go: up one dir, main page]

Assignment # 5 - 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ASSIGNMENT # 05 (CLO-IV) 10 points.

Date of Submission: 30-06-2021 B.S.(C.S.)-7 A, B & C

I. Code Optimization Techniques


& Peephole(Target level) Optimizations.
Q 1.
From the following Code Segments, Write the name of the Optimization Technique (from the
names given below) that can be applied in each case. And also write the modified code after
applying the optimization technique.
1. Redundant-instruction Elimination.
2. Eliminating Unreachable Code.
3. Flow-of control optimizations.
4. Algebraic simplifications.
5. Use of machine-idioms.
6. Dead Code Elimination.
7. Loop unrolling (invariant looping ).
8. Use of Machine idioms.
9. Code hoisting.
10. Code movement.
11. Strength Reduction.
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 *
{ x=y+z; S3 = 4 x j PI * I
a=x+5; a[j] = 6 x j; S4 = 4 x i
} } S5 = n ENDDO
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.
X= X+ 0 while (i <= limit-2)
Or do B=Ax2
X= X*1 {loop code}

------------------------------------------------------------------------------------------------------------
II. Intermediate Code Generation.
Q.2 For the following grammar:

E → E+ T | E– T | T
T→ T * F | F
F → ( E ) | id | num

i. Write the attribute grammar/semantic rules/ syntax-directed definition.

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.

iv. Three-address code is a linearized representation of a syntax tree or a DAG in which


explicit names correspond to the interior nodes of the graph. Write down the
corresponding three-address code sequence for the DAG of the expression in part (ii).

Q.3 Give the sequence of Three-address code instructions as well as P-code insrtuctions
corresponding to each of the following arithmetic expressions.
a. 2+3+4+5
b. 2+ (3+(4+5))
c. a*b+ a*b*c

Q.4 Give the sequence of Three-address code instructions as well as P-code insrtuctions
corresponding to each of the following arithmetic expressions.
a. (x = y = 2) + 3*(x = 4)
b. a[a[i]] = b[i=2]

Q. 5 Consider the following assignment statements.


i) a = b * - c + b * - c ;
ii) a = - b * (c + d) /e
iii) x = - (a +b) * (c + d) + ( a + b + c)

a) Give Three-address codes for the above assignments.


b) Give Quadruples and triples to implement the above three address codes.
c) Give P-code sequence for the above assignments.

Q. 6:
Translate the assignment statements:
1. a = b[i] + c[j]
2. a[i] = b\*c - b\*d
3. x = f(y+1) + 2
4. x = \*p + &y
5. a = b* - (c – d) + b* - (c – d)
The post-fix notation is : a b c d - U * b c d – U * + =

into data structures for the implementation of three-address code. as well as p-code.
a) A syntax tree.
b) Three-address code.
b) Quadruples.
c) Triples.
---------------------------------------------------------------------------------------------------------
Q. 7 : Give the sequence of

a) Three address code and b) P code for factorial c code.

{ sample program in C language that computes factorial}


Read x; { input an integer}
if 0 < x then { do not compute if x < = 0 }
fact := 1;
repeat
fact := fact * x;
x := x-1
until x=0;
write fact { output factorial of x }
end.
Q 8. Give the sequence of

a) Three-address & (b) P-code


corresponding to the following piece of code.
Greatest Common Divisor program }
read u;
read v; { input two integers}
if v = 0 then v:= 0 { do nothing }
else
repeat
temp := v;
v := u - u/v*v; { computes u mod v }
u := temp
until v = 0
end;
write u { output gcd of original u & v}
----------------------------------------------------------------------
III. Run-time Environment
Q 9. Draw a possible organization for the run time environment of the following C program.
a) After entry into block A in function f.
b) After entry into block B in function g.

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. This code contains variables that has
its basic operation as follows:
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; }
------------------------------------------------------------------------------------------
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;

int gcd (int u, int v)


{ if (v = = 0) return u;
else return gcd(v, u % v);
}

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. During the
third call the runtime environment may be visualized as in figure. Note how each call to gcd adds
a new activation record of exactly the same size to the top of the stack and in each activation
record , the control link points to the control link of the previous activation record.
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.
------------------------------------------------------------------------------------------------------------
Q. 13 (Example 6.5) : Consider the statement

do i = i +1 ; while ( a[i] < v ) ;

Two possible translations of this statement is shown below. The translation in the figure uses a
symbolic label L, attached to the first instruction. The translation in (b) shows position numbers
for the instructions, starting arbitrarily at position 100. In both translations, the last instruction is
a conditional jump to the first instruction. The multiplication i*8 is appropriate for an array 0f
elements that each take 8 units of space.
From section 2.8.3, l- and r- values are appropriate on the left and right sides of assignment
respectively.

L: t1= i + 1 100: t1 = i +1
i = t1 101: i = t1
t2 = i * 8 102: t2 = i* 8
t3 = a [t2] 103: t3 = a [t2]
if t3 < v goto L 104: if t3 < v goto 100

(a) Symbolic Labels. (b) Position numbers

Figure : Two ways of assigning labels to three-address statements.

The choice of allowable operators is an important issue in the design of an intermediate form.
The operator set clearly must be rich enough to implement the operations in the source language.
Operators that are close to machine instructions make it easier to implement the intermediate
form on a target machine. However, if the front end must generate long sequence of instructions
for some source language operations, then the optimizer and code generator may have to work
harder to re-discover the structure and generate good code for these operations.

--------------------------------------THE END ---------------------------------------------------

You might also like