1. Consider this "basic block" of 3-address code statements. Note that this basic block came from a loop with variable i.
Assume variables a, i, x are programmer-defined (type int[], or int), C is a programmer-defined constant (also int), and that all ti are compiler-generated temporaries, not used outside this basic block.
Line #
1) t1 := i - 1
2) t2 := 4*t1
3) t3 := a[t2]
4) x := t3
5) t4 := 4*t1
6) t5 := a[t4]
7) t6 := C - 1
8) t7 := 4*t6
9) t8 := t7
10) a[t8] := x
List at least FOUR significant types of optimization that can be done to the block. For each, BRIEFLY:
a) define the optimization
b) list line number(s) where this optimization can be used
c) explain how the optimization improves this line (or lines)
2. Show the output of the program (in a language that is not C++ but has similar syntax) listed below if the language uses:
a) dynamic scope and call by reference
b) static scope and call by value-result
int a, b;
int f(int x) {
a++;
x++;
return (a+x);
}
void g(int y){
int a = 2;
int c = f(a);
b = 9;
y = y+a;
}
void main(){
a = 7;
b = 5;
g(b);
print (a, b);
}
3. Problem: Which of the following language features CAN and which CANNOT be described using a context free grammar (or Backus-Naur Form grammar). Explain your responses.
a) Multiplication has higher precedence than addition.
b) Boolean expressions are evaluated using short-circuit evaluation.
c) The index used in an array reference must be in the range with which the array was declared.
d) In a nested IF statement, an ELSE corresponds to the nearest IF.
4. "The C language uses structural type equivalence for arrays and name type equivalence for structs."
a) Clearly and concisely, explain the meaning of this sentence.
Give (if possible) type and/or variable definitions in C or C++ for:
b) Two arrays a1, a2 that are of equivalent types
c) Two arrays b1, b2 that are not of equivalent types
d) Two structs c1, c2 that are of equivalent types
e) Two structs d1, d2 that are not of equivalent types