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 26–30 of 32
The following code segment is executed on a processor which allows only register operands in its instructions. Each instruction can have atmost two source operands and one destination operand. Assume that all variables are dead after this code segment.
c = a + b;
d = c * a;
e = c + a;
x = c * c;
if (x > a) {
y = a * a;
}
else {
d = d * d;
e = e * e;
} What is the minimum number of registers needed in the instruction set architecture of the processor to compile this code segment without any spill to memory? Do not apply any optimization other than optimizing register allocationAnswer: (B)
Do not move any code. Look at the point just after e = c + a. Here a, c, d and e are all live. c is still needed for x = c * c. d and e are still needed in the else part. So 4 values are live at the same time.
After this, x takes the place of c, so at most 4 values stay live. So 4 registers are enough, and fewer will not work.
Answer: 4
After this, x takes the place of c, so at most 4 values stay live. So 4 registers are enough, and fewer will not work.
Answer: 4
The following code segment is executed on a processor which allows only register operands in its instructions. Each instruction can have atmost two source operands and one destination operand. Assume that all variables are dead after this code segment.
c = a + b;
d = c * a;
e = c + a;
x = c * c;
if (x > a) {
y = a * a;
}
else {
d = d * d;
e = e * e;
} Suppose the instruction set architecture of the processor has only two registers. The only allowed compiler optimization is code motion, which moves statements from one place to another while preserving correctness. What is the minimum number of spills to memory in the compiled code?Answer: (B)
After c = a + b, the value b is not needed again. The values a and c are still needed. To test x > a, the processor needs x and a in the two registers. But c is also still needed if the else branch runs.
Statements d = c * a and e = c + a are used only in the else branch. Code motion lets us move them into that branch. Then the code is:
c = a + b; x = c * c; if (x > a) { y = a * a; } else { d = c * a; d = d * d; e = c + a; e = e * e; }
At the test, three values are live (a, x and c) and there are only 2 registers. So c is stored to memory once (one spill). Then:
if branch: c is dead, and y = a * a needs only a and y. No more spills.
else branch: c is loaded back, d = c * a and d * d are computed. Then c is loaded again for e = c + a and e * e. Reloading the stored c is not a new spill.
Zero spills is not possible. At the test, a and x must be in registers, but the else branch still needs a value derived from c (c itself, or d and e if they were computed earlier). That value has to be kept in memory, so there is at least one spill.
Answer: (B)
Statements d = c * a and e = c + a are used only in the else branch. Code motion lets us move them into that branch. Then the code is:
c = a + b; x = c * c; if (x > a) { y = a * a; } else { d = c * a; d = d * d; e = c + a; e = e * e; }
At the test, three values are live (a, x and c) and there are only 2 registers. So c is stored to memory once (one spill). Then:
if branch: c is dead, and y = a * a needs only a and y. No more spills.
else branch: c is loaded back, d = c * a and d * d are computed. Then c is loaded again for e = c + a and e * e. Reloading the stored c is not a new spill.
Zero spills is not possible. At the test, a and x must be in registers, but the else branch still needs a value derived from c (c itself, or d and e if they were computed earlier). That value has to be kept in memory, so there is at least one spill.
Answer: (B)
Consider evaluating the following expression tree on a machine with load-store architecture in which memory can be accessed only through load and store instructions. The variables a, b, c, d and e are initially stored in memory. The binary operators used in this expression tree can be evaluated by the machine only when the operands are in registers. The instructions produce result only in a register. If no intermediate results can be stored in memory, what is the minimum number of registers needed to evaluate this expression?
Answer: (D)
A binary operator needs both operands in registers, and a leaf must be loaded into a register first. So needs 2 registers, and needs 2 registers. For : evaluate first (2 registers, result in 1), then load into a second register. So it also needs 2 registers.
The root has two subtrees, and , and each needs 2 registers. Whichever is done first leaves its result in 1 register while the other subtree still needs 2 registers of its own. So 3 registers are needed at that point, and 2 are not enough.
Schedule:
- Load c and d (2 registers). c + d R1.
- Load e R2. e R1 R1 (1 register still in use).
- Load a and b into R2 and R3 (3 registers in use). a b R2. Then R2 + R1.
Answer: (D)
The root has two subtrees, and , and each needs 2 registers. Whichever is done first leaves its result in 1 register while the other subtree still needs 2 registers of its own. So 3 registers are needed at that point, and 2 are not enough.
Schedule:
- Load c and d (2 registers). c + d R1.
- Load e R2. e R1 R1 (1 register still in use).
- Load a and b into R2 and R3 (3 registers in use). a b R2. Then R2 + R1.
Answer: (D)
The program below uses six temporary variables a, b, c, d, e, f.
a =1
b= 10
c =20
d= a +b
e= c +d
f =c +e
b= c+ e
e =b +f
d =5 +e
return d +f
Assuming that all operations take their operands from registers, what is the minimum number of registers needed to execute this program without spilling?
a =1
b= 10
c =20
d= a +b
e= c +d
f =c +e
b= c+ e
e =b +f
d =5 +e
return d +f
Assuming that all operations take their operands from registers, what is the minimum number of registers needed to execute this program without spilling?
Answer: (B)
A variable needs a register only while it is live (its value is used later). Live variables after each statement (counting the values that are held):
- before : are live (3). and are not used later, so they die here.
- after :
- after : (the old is never used again, because is set again later)
- after : (3 live together)
- after : ( and die, because the later is set again before it is used)
- after : ( dies)
- after : ( dies), then is returned.
At most 3 values are live at the same time, and 3 are live at some points, so 3 registers are needed.
Answer: (B)
- before : are live (3). and are not used later, so they die here.
- after :
- after : (the old is never used again, because is set again later)
- after : (3 live together)
- after : ( and die, because the later is set again before it is used)
- after : ( dies)
- after : ( dies), then is returned.
At most 3 values are live at the same time, and 3 are live at some points, so 3 registers are needed.
Answer: (B)
Some code optimizations are carried out on the intermediate code because
Answer: (A)
Intermediate code does not depend on the machine. If the optimizations are done on it, the same optimizer can be reused for many target processors, and only the code generator has to be rewritten for each new target. If the optimization were done on target code, it would have to be redone for every architecture. So doing some optimization on intermediate code improves the portability of the compiler.
(B) is at most a side benefit: intermediate code is simpler to analyse because it has no machine details, but that is not the main reason.
(C) is wrong: dataflow information can also be used on target code, so it is not true that it cannot be used otherwise.
(D) is wrong: information from the front end (such as types and the symbol table) can also be passed on to later phases, so it is not true that it cannot be used otherwise.
Answer: (A)
(B) is at most a side benefit: intermediate code is simpler to analyse because it has no machine details, but that is not the main reason.
(C) is wrong: dataflow information can also be used on target code, so it is not true that it cannot be used otherwise.
(D) is wrong: information from the front end (such as types and the symbol table) can also be passed on to later phases, so it is not true that it cannot be used otherwise.
Answer: (A)
Keep practising
Topics in Compiler Design
Lexical AnalysisParsingSyntax-directed TranslationRuntime EnvironmentAssemblers, Linkers and Loaders
Intermediate Code GenerationYou are here
Matching