Consider the grammar rule for arithmetic expressions. The code generated is targeted to a CPU having a single user register. The subtraction operation requires the first operand to be in the register. If and do not have any common sub expression, in order to get the shortest possible code
View worked solution
Answer: B
To find the evaluation order that produces the shortest code, let's analyze the instructions generated under a single-register () constraint. The subtraction operation requires the first operand () to be in the register at the time of subtraction (i.e., ).
Case 1: is evaluated first
- Evaluate into .
- Since we need to evaluate , we must spill to a temporary memory location:
Store R → T1 - Evaluate into .
- Since the subtraction requires (the first operand) to be in , we cannot subtract directly. We must:
- Spill :
Store R → T2 - Load back:
Load T1 → R - Perform subtraction:
Sub T2 from R()
- Spill :
This sequence requires 2 Store and 1 Load extra instructions.
Case 2: is evaluated first
- Evaluate into .
- Spill to a temporary memory location to free :
Store R → T1 - Evaluate into . Now, the first operand is already in .
- Perform subtraction directly using the spilled value of :
Sub T1 from R()
This sequence requires only 1 Store extra instruction.
Thus, evaluating first yields the shortest possible code.
Free practice · Your answers on this page are not saved.
