0% found this document useful (0 votes)
419 views

Back Patching CC Presentation

Backpatching is a technique used in one-pass code generation to fill in missing labels by patching instructions after parsing is complete. It involves creating lists of incomplete jump instructions and later inserting the correct label. For boolean expressions, true and false lists are used to track incomplete jumps. Flow of control statements are translated by generating code, recording incomplete jumps in marker nonterminals, and later backpatching the jumps. Break and continue statements can also be implemented using backpatching.

Uploaded by

Sarmad Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
419 views

Back Patching CC Presentation

Backpatching is a technique used in one-pass code generation to fill in missing labels by patching instructions after parsing is complete. It involves creating lists of incomplete jump instructions and later inserting the correct label. For boolean expressions, true and false lists are used to track incomplete jumps. Flow of control statements are translated by generating code, recording incomplete jumps in marker nonterminals, and later backpatching the jumps. Break and continue statements can also be implemented using backpatching.

Uploaded by

Sarmad Mehmood
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Back Patching

Presented by Sarmad Mahmood Rana(2183090)


Tooba Amir Butt (2183102)
BACKPATCHING

Back patching is the activity of filling up the unspecified information of labels by using the appropriate
semantic expression in during the code generation process. It is done by Boolean expression.
It is putting the address instead of labels when the proper label is
determined.
For Example.
1. if a < b goto _
2. goto _
3. if c < d goto _
4. goto _
5. if e < f goto _
6. goto _
Back Patching Operations

Back patching Algorithms perform three types of operations.

1) make list (i) – creates a new list containing only i, an index into the array of quadruples and
returns a pointer to the list it has made.
2) Merge (i, j) – concatenates the lists pointed to by i and j, and returns a pointer to the
concatenated list.
3) Back patch (p, i) – inserts i as the target label for each of the statements on the list pointed
to by p.
One-Pass Code Generation Using
Backpatching
Backpatching can be used to generate code for boolean expressions and flow-of-control
statements in one pass. The translations we generate will be of the same form as those , except
for how we manage labels.

Its synthesized attributes truelist and falselist of nonterminal B are used to manage labels in


jumping code for boolean expressions.

we generate instructions into an instruction array, and labels will be indices into this array. To
manipulate lists of jumps,
Truelist, Falselist

In particular, B. truelist will be a list of jump or conditional jump instructions into which we


must insert the label to which control goes if B is true. B. falselist likewise is the list of
instructions that eventually get the label to which control goes when B is false. As code is
generated for B, jumps to the true and false exits are left incomplete,  with the label field
unfilled. These incomplete jumps are placed on  lists  pointed  to  by  B. truelist and  B.
falselist, as appropriate.
For Example.
 if b<c goto _ B True
list
 goto _
False
Back patching for Boolean Expressions

 Boolean expressions are usually translated using the jump method since this is convenient
for optimization.
 However, more than a single pass may be needed in order to generate code for boolean
expressions and flow of control during bottom-up parsing!
 Indeed, when translating forward jumps, at the time we generate the code we do not know
the (numerical) address of the label we want to branch to
Semantic Rules for Incorporating Backpatching

B->B1 or M B2 { backpatch(B1.falselist,M.quad); B.truelist := merge(B1.truelist,B2.truelist);


E.falselist := B2.falselist }
B->B1 and M B2 { backpatch (B1.treulist,M.quad); B.truelist := B2.truelist; B.falselist :=
merge(B1.false, B2falselist)}
B -> not B1 { B.truelist := B1.falselist; B.falselist := B1.truelist}
B -> (B1) {B.truelist := B1.truelist; B.falselist := B1.falselist}
B -> true {B.truelist := makelist(nextquad); emit(‘goto_’)}
B -> false {B. false list := makelist(nextquad); emit(‘goto_’)}
M -> e { M.quad := nextquad }
B -> id1 relop id2 { B.truelist := makelist(nextquad); B.falselist := makelist(nextquad) +1
emit(‘if’ id1.place relop.op id2.place ‘goto’_) emit(‘goto_’)}
Parse Tree
Consider the same example Boolean expression “x < 100 || x > 200 && x ! = y”. The
corresponding derivation tree .
Continue

100: if x < 100 goto


101: goto
The two instructions are generated. (We arbitrarily start instruction numbers at 100.) The marker
nonterminal M in the production
B -> B1 ||M B2
records the value of nextinstr, which at this time is 102. The reduction of
x > 200 to B by production (5) generates the instructions
102: if x > 200 goto
103: goto
The subexpression x > 200 corresponds to B1 in the production
B -> B1 && M B2
The marker nonterminal M records the current value of nextinstr,
which is now
104. Reducing x ! = y into B by production (5) generates

104: if x != y goto
105: goto-
Flow-of-Control Statements

We now use backpatching to translate flow-of-control statements in one pass.

Consider statements generated by the following grammar:


S -> if ( B ) S | if ( B ) S else S | while ( B ) S | { L } j A ;
L|LSjS
Here S denotes a statement, L a statement list, A an assignment-statement,
and B a boolean expression. Note that there must be other productions,
Flow-of-Control Statements

Translation of statements

1) S -> if ( B )M S1 {backpatch(B :truelist; M :instr);


S:nextlist = merge(B :falselist; S1:nextlist); g
2) S -> if ( B )M1 S1 N else M2 S2
{ backpatch(B :truelist; M1:instr);
backpatch(B :falselist; M2:instr);
temp = merge(S1:nextlist; N :nextlist);
S:nextlist = merge(temp; S2:nextlist); }
3) S -> while M1 ( B ) M2 S1
{ backpatch(S1:nextlist; M1:instr);
backpatch(B :truelist; M2:instr);
S:nextlist = B :falselist;
gen(goto0 M1:instr); }
Flow-of-Control Statements

Translation of statements

4) S -> { L } { S:nextlist = L:nextlist; }


5) S -> A ; { S:nextlist = null; }
6) M -> e { M :instr = nextinstr; }
7) N -> {N :nextlist = makelist(nextinstr);
gen(goto --); }
8) L -> L1 M S { backpatch(L1:nextlist; M :instr);
L:nextlist = S:nextlist; }
9) L -> S { L:nextlist = S:nextlist; }
Three Address Code

100: if x < 100 goto


101: goto
102: if x > 200 goto 104
103: goto
104: if x != y goto
105: goto
 (a) After backpatching 104 into instruction 102.
100: if x < 100 goto
101: goto 102
102: if x > 200 goto 104
103: goto
104: if x != y goto
105: goto
 (b) After backpatching 102 into instruction 101.
Break-, Continue-, Goto Statement

Goto-statements can be implemented by maintaining a list of unfilled jumps for each label
and then backpatching the target when it is known.

Java does away with goto-statements. However, Java does permit disciplined jumps called
break-statements, which send control out of an enclosing construct, and continue-
statements, which trigger the next iteration of an en-closing loop.
Code

The following excerpt from a lexical analyzer illustrates simple break- and continue-statements:
1) for ( ; ; readch() ) {
2) if( peek == ' ' || peek == '\t' ) continue;
3) else if( peek == '\n' ) line = line + 1;
4) else break;
5) }
Control jumps from the break-statement on line 4 to the next statement after
the enclosing for-loop. Control jumps from the continue-statement on line 2 to
code to evaluate readch() and then to the if-statement on line 2.

You might also like