CD Unit 5
CD Unit 5
For example, the respective three-address statements would be translated into latter
code sequence as shown below:
P: =Q+R
S: =P+T
MOV Q, R0
ADD R, R0
MOV R0, P
MOV P, R0
ADD T, R0
MOV R0, S
Here the fourth statement is redundant as the value of the P is loaded again in that
statement that just has been stored in the previous statement. It leads to an inefficient
code sequence. A given intermediate representation can be translated into many code
sequences, with significant cost differences between the different implementations. A
prior knowledge of instruction cost is needed in order to design good sequences, but
accurate cost information is difficult to predict.
1. During Register allocation – we select only those set of variables that will
reside in the registers at each point in the program.
2. During a subsequent Register assignment phase, the specific register is
picked to access the variable.
5. Evaluation order –
The code generator decides the order in which the instruction will be executed. The
order of computations affects the efficiency of the target code.
6.Approaches to code generation issues: Code generator must always generate the
correct code. It is essential because of the number of special cases that a code generator
might face. Some of the design goals of code generator are:
o Correct
o Easily maintainable
o Testable
o Maintainable
Code Generation Algorithm
d := (a - b) + (a - c) + (a - c)
Three address Target code Register ddress
code sequence descriptor descriptor
empty
t 1 := a - b R1 contains a t 1 is in R2
R2 contains t1
t 2:= a - c R3 contains c,
R1 contains t 2 t 2 is in R1
∑ use(x, B) + 2 * live(x, B)
B in L