Intermediate Code Generation · Compiler Design
32 questions · asked in 16 of 27 years (2000–2026)
MCQ 26 · MSQ 1 · NAT 51-mark 13 · 2-mark 19
2000Questions per year2026
Questions 11–15 of 32
Consider the following ANSI C code segment:
z=x + 3 + y-> f1 + y-> f2;
for (i = 0; i < 200; i = i + 2)
{
if (z > i)
{
p = p + x + 3;
q = q + y-> f1;
} else
{
p = p + y-> f2;
q = q + x + 3;
}
}Assume that the variable y points to a struct (allocated on the heap) containing two fields f1 and f2, and the local variables x, y, z, p, q, and i are allotted registers. Common sub-expression elimination (CSE) optimization is applied on the code. The number of addition and the dereference operations (of the form y -> f1 or y -> f2) in the optimized code, respectively, are:Answer: (D)
CSE finds x + 3, yf1 and yf2 one time before the loop and reuses them.
- Before the loop: t = x + 3, then z = t + f1 + f2 3 additions, 2 dereferences.
- Loop: 100 passes (i = 0, 2, , 198). Each pass has i + 2, one addition to p, one to q 3 additions 100 = 300.
Total: 303 additions, 2 dereferences.
Answer: 303 and 2
- Before the loop: t = x + 3, then z = t + f1 + f2 3 additions, 2 dereferences.
- Loop: 100 passes (i = 0, 2, , 198). Each pass has i + 2, one addition to p, one to q 3 additions 100 = 300.
Total: 303 additions, 2 dereferences.
Answer: 303 and 2
For a statement S in a program, in the context of liveness analysis, the following sets are defined:
USE(S) : the set of variables used in S
IN(S) : the set of variables that are live at the entry of S
OUT(S) : the set of variables that are live at the exit of S
Consider a basic block that consists of two statements, S1 followed by S2. Which one of the following statements is correct?
USE(S) : the set of variables used in S
IN(S) : the set of variables that are live at the entry of S
OUT(S) : the set of variables that are live at the exit of S
Consider a basic block that consists of two statements, S1 followed by S2. Which one of the following statements is correct?
Answer: (A)
Inside a basic block, control moves directly from S1 to S2. So the variables live at the exit of S1 are the same as the variables live at the entry of S2, that is, OUT(S1) = IN(S2).
Answer: OUT(S1) = IN(S2)
Answer: OUT(S1) = IN(S2)
Consider the productions and . Each of the five non-terminals A,P,Q,X, and Y has two attributes: s is a synthesized attribute, and i is an inherited attribute. Consider the following rules.
Rule 1: P.i=A.i+2, Q.i=P.i+A.i, and A.s=P.s+Q.s
Rule 2: X.i=A.i+Y.s and Y.i=X.s+A.i
Which one of the following is TRUE?
Rule 1: P.i=A.i+2, Q.i=P.i+A.i, and A.s=P.s+Q.s
Rule 2: X.i=A.i+Y.s and Y.i=X.s+A.i
Which one of the following is TRUE?
Answer: (B)
A rule is L-attributed when each inherited attribute depends only on the inherited attributes of the parent and on attributes of siblings to its left.
- Rule 1: P.i depends on A.i. Q.i depends on P.i (a left sibling) and A.i. So Rule 1 is L-attributed.
- Rule 2: X.i depends on Y.s, but Y sits to the right of X. So Rule 2 is not L-attributed.
Answer: Only Rule 1 is L-attributed
- Rule 1: P.i depends on A.i. Q.i depends on P.i (a left sibling) and A.i. So Rule 1 is L-attributed.
- Rule 2: X.i depends on Y.s, but Y sits to the right of X. So Rule 2 is not L-attributed.
Answer: Only Rule 1 is L-attributed
Consider the following intermediate program in three address code
p= a -b
q= p *c
p= u * v
q= p + q
Which one of the following corresponds to a static single assignment form of the above code?
p= a -b
q= p *c
p= u * v
q= p + q
Which one of the following corresponds to a static single assignment form of the above code?
Answer: (B)
The original code is , , , . In SSA, each name is assigned exactly once, and every use refers to the latest version of that variable (the subscript numbers themselves do not matter).
(B) , , , . Every name is assigned once. uses the first (). The last statement uses the second () and the first (). So it is correct.
(A) assigns twice and twice, so it is not SSA.
(C) uses , and , which are never assigned.
(D) uses plain and (in and in the last statement) instead of subscripted names.
Answer: (B)
(B) , , , . Every name is assigned once. uses the first (). The last statement uses the second () and the first (). So it is correct.
(A) assigns twice and twice, so it is not SSA.
(C) uses , and , which are never assigned.
(D) uses plain and (in and in the last statement) instead of subscripted names.
Answer: (B)
Consider the expression ( a-1)* (((b + c) / 3) + d)) . Let X be the minimum number of registers required by an optimal code generation (without any register spill) algorithm for a load/store architecture in which (i) only loads and store instructions can have memory operands and (ii) arithmetic instructions can have only register or immediate operands. The value of X is _________.
Answer: 2
On a load/store machine an arithmetic instruction works only on registers or immediate values. So needs and in two registers at the same time. This means 1 register is not enough.
Two registers are enough if the larger subtree is evaluated first:
R1 = b, R2 = c, R1 = R1 + R2 (R2 is free again), R1 = R1 / 3 (3 is an immediate), R2 = d, R1 = R1 + R2
R2 = a, R2 = R2 1, R1 = R2 * R1
At no point are more than 2 registers in use.
Answer: 2
Two registers are enough if the larger subtree is evaluated first:
R1 = b, R2 = c, R1 = R1 + R2 (R2 is free again), R1 = R1 / 3 (3 is an immediate), R2 = d, R1 = R1 + R2
R2 = a, R2 = R2 1, R1 = R2 * R1
At no point are more than 2 registers in use.
Answer: 2
Keep practising
Topics in Compiler Design
Lexical AnalysisParsingSyntax-directed TranslationRuntime EnvironmentAssemblers, Linkers and Loaders
Intermediate Code GenerationYou are here
Matching