GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 10

Evaluation order with one accumulator

Compiler Design · Code Generation & Register Allocation · Full GATE CSE 2004 paper

GATE 2004, Q10 1 mark · MCQ
Not in GATE 2027 syllabusReport issue

Code Generation & Register Allocation

Question 10, GATE 2004: For E→E1−E2, generate code for a CPU with only one user register. Subtraction requires its first operand in that register; E1 and E2 have no common subexpression. Which evaluation order gives the shortest code?
Options:
(A) Evaluate E1 first
(B) Evaluate E2 first
(C) Necessarily interleave E1 and E2 evaluation
(D) Evaluation order is of no consequence
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Evaluate E2 first and save its value in a temporary memory location. Then evaluate E1, leaving the required first operand in the sole register, and subtract the saved E2.

2. Evaluating E1 first would require saving it, evaluating/storing E2, then reloading E1 before subtraction. Thus E2-first, B, avoids an extra reload in the stated accumulator-style model, assuming both expressions require evaluation and their order is free of side-effect constraints.

3. Teaching check: The final subtraction needs register=E1 and memory temporary=E2. E2-first naturally reaches that arrangement after one save. E1-first must preserve E1 across E2’s evaluation, then reload it, adding a memory operation. This argument assumes unrestricted evaluation order and nontrivial expression evaluations.

Original paper, page 2