When statements are translated using the functions in Fig. 6.5, it will
often be the case that the statement immediately following a label is a GOTO statement, i.e., we have the following situation:
LABEL label1
GOTO label2
It is clear that any jump to label1 can be replaced by a jump to label2, and that this
will result in faster code. Hence, it is desirable to do so. This is called jump-to-jump
optimisation, and can be done after code-generation by a post-process that looks for
these situations. However, it is also possible to avoid most of these situations by
modifying the translation function.
This can be done by adding an extra inherited attribute endlabel, which holds the
name of a label that can be used as the target of a jump to the end of the code that is
being translated. If the code is immediately followed by a GOTO statement, endlabel
will hold the target of this GOTO rather than a label immediately preceding this.
a) Add the endlabel attribute to TransStat from Fig. 6.5 and modify the rules so
endlabel is exploited for jump-to-jump optimisation. Remember to set endlabel
correctly in recursive calls to TransStat.
b) Use the modified TransStat to translate the following statement:
while x>0 do
x := x-1;
if x>10 then x := x/2
The extent of the while loop is indicated by indentation.
Use the same vtable as 6.1 and use endlab as the endlabel for the
whole statement.