Back-patching

We generate a series of branching statements with the targets of the jumps temporarily left unspecified. Use a to-be-determined label table, each entry of which contains a list of places that need to be back-patched. The same table can also be used to implement labels and goto’s.

There are two possible ways to translate switch statements.
Scheme 1:
code to evaluate E into t
goto test
L[1]: code for S[1]
goto next
...
L[k]: code for S[k]
goto next
L[d]: code for S[d]
goto next
test:
if t = V[1] goto L[1]
...
if t = V[k] goto L[k]
goto L[d]
next:
...
Scheme 2:
code to evaluate E into t
if t <> V[1] goto L[1]
code for S[1]
goto next
L[1]: if t <> V[2] goto L[2]
code for S[2]
goto next
...
L[k-1]: if t <> V[k] goto
L[k]
code for S[k]
goto next
L[k]: code for S[d]
next:
The first scheme is simpler to implement as it creates all the necessary labels first and then goes for test and branching.

Case three-address-code instructions used to translate a switch statement are as below.
case t V1 L1
case t V2 L2
...
case t Vn-1 Ln-1
case t t Ln

label next

0 comments