Backpatching
description
Transcript of Backpatching
![Page 1: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/1.jpg)
Backpatching
Lecture 23
Wed, Apr 13, 2005
![Page 2: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/2.jpg)
The “next” Destination of a Statement
Every statement will be represented as a LinkedList object in the grammar.
The linked list will be a list of all backpatch labels occurring within the statement that must be resolved to the “next” destination of that statement.
In the grammar, we must writenonterminal LinkedList stmts, stmt, n;
![Page 3: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/3.jpg)
Labels and Jumps in the Grammar
The productions that will involve jumps are stmts stmts m stmtstmt IF ( cexpr ) m stmtstmt IF ( cexpr ) m stmt n ELSE m stmtfunc fbeg stmts m RBRACE
Remember, m represents a destination. n represents a jump.
![Page 4: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/4.jpg)
Sequences of Statements
Consider the first production.
stmts stmts1 m stmt Every statement has a “next” destination. Normally, but not always, this is the next
statement. This production will insert labels between
consecutive statements, even though most of them will not be used.
![Page 5: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/5.jpg)
Sequences of Statments
The label at m serves as the “next” destination of stmts1.
![Page 6: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/6.jpg)
Sequences of Statments
The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute).
stmts stmts1 m stmt
![Page 7: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/7.jpg)
Sequences of Statments
The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute).
stmts stmts1 m stmt
next
![Page 8: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/8.jpg)
Sequences of Statments
The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute).
stmts stmts1 m stmt
next next
![Page 9: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/9.jpg)
Action
We must take the following actions. Backpatch stmts1.nextList to m. Return stmt.nextList.
Then stmt.nextList becomes nextList of stmts, i.e., a synthesized attribute.
This resolves the “next” destination from stmts1 and leaves the “next” destination from stmt unresolved.
![Page 10: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/10.jpg)
The One-Way if Statement
Now consider the one-way if statement.stmt IF ( cexpr ) m stmt1
The “true” list of the backpatch node associated with cexpr will be resolved to m.
The “false” list of cexpr will later be resolved to the label following stmt1.
stmt IF ( cexpr ) m stmt1
![Page 11: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/11.jpg)
The One-Way if Statement
Now consider the one-way if statement.stmt IF ( cexpr ) m stmt1
The “true” list of the backpatch node associated with cexpr will be resolved to m.
The “false” list of cexpr will later be resolved to the label following stmt1.
stmt IF ( cexpr ) m stmt1
T
![Page 12: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/12.jpg)
The One-Way if Statement
Now consider the one-way if statement.stmt IF ( cexpr ) m stmt1
The “true” list of the backpatch node associated with cexpr will be resolved to m.
The “false” list of cexpr will later be resolved to the label following stmt1.
stmt IF ( cexpr ) m stmt1
T
F
![Page 13: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/13.jpg)
The One-Way if Statement
Now consider the one-way if statement.stmt IF ( cexpr ) m stmt1
The “true” list of the backpatch node associated with cexpr will be resolved to m.
The “false” list of cexpr will later be resolved to the label following stmt1.
stmt IF ( cexpr ) m stmt1
T
F
next
![Page 14: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/14.jpg)
Action
We must take the following actions. Backpatch cexpr.trueList to m. Merge cexpr.falseList and stmt1.nextList. Return the merged list.
The merged list becomes the nextList of stmt.
![Page 15: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/15.jpg)
Example
Consider the following segment of code.
if (a) b = 900;a = 200;
![Page 16: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/16.jpg)
Example
JUMPT INT BLABEL blabel=3 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
EQU BLABEL blabel=3 LABEL label=5
LABEL label=6
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=4 LABEL label=6
![Page 17: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/17.jpg)
Example
JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
EQU BLABEL blabel=3 LABEL label=5
LABEL label=6
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=4 LABEL label=6
![Page 18: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/18.jpg)
Example
JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
EQU BLABEL blabel=3 LABEL label=5
LABEL label=6
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=4 LABEL label=6
![Page 19: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/19.jpg)
Example
JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4 = 6
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
EQU BLABEL blabel=3 LABEL label=5
LABEL label=6
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=4 LABEL label=6
![Page 20: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/20.jpg)
Example
JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4 = 6
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
EQU BLABEL blabel=3 LABEL label=5
LABEL label=6
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=4 LABEL label=6
![Page 21: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/21.jpg)
Two-Way if Statements
The two-way if production is more interesting.stmt IF ( cexpr ) m1 stmt1 n ELSE m2 stmt2
The “true” destination from cexpr is m1 and the “false” destination is m2.
The “next” destination of stmt1 is the same as the “next destination of n.
n represents a jump to the “next” destination of stmt2.
The “next” destination of stmt2 becomes the “next” destination of stmt.
![Page 22: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/22.jpg)
Two-Way if Statements
Consider the two-way if statement.
stmt IF ( cexpr ) m1 stmt1 n ELSE m2 stmt2
![Page 23: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/23.jpg)
Two-Way if Statements
If cexpr is true, then execution jumps to the label m1.
stmt IF ( cexpr ) m1 stmt1 n ELSE m2 stmt2
T
![Page 24: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/24.jpg)
Two-Way if Statements
If cexpr is false, then execution jumps to the label m2.
stmt IF ( cexpr ) m1 stmt1 n ELSE m2 stmt2
T
F
![Page 25: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/25.jpg)
Two-Way if Statements
When stmt1 is completed, execution jumps to the statement following the if statement, i.e., it jumps to stmt.nextList.
stmt IF ( cexpr ) m1 stmt1 n ELSE m2 stmt2
T
F
next
![Page 26: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/26.jpg)
Two-Way if Statements
If execution reaches n, then it jumps to stmt.nextList.
stmt IF ( cexpr ) m1 stmt1 n ELSE m2 stmt2
T
F
next
![Page 27: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/27.jpg)
Two-Way if Statements
When stmt2 is completed, execution jumps to stmt.nextList.
stmt IF ( cexpr ) m1 stmt1 n ELSE m2 stmt2
T
F
next
next
![Page 28: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/28.jpg)
Action
We must take the following actions. Backpatch cexpr.trueList to m1.
Backpatch cexpr.falseList to m2.
Merge stmt1.nextList, n.nextList, and stmt2.nextList.
The merged list becomes the nextList of stmt.
![Page 29: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/29.jpg)
Example
Consider the following segment of code.
if (a) b = 900;else b = 500;a = 200;
![Page 30: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/30.jpg)
Example
JUMPT INT BLABEL blabel=3 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
JUMP INT BLABEL blabel=6
LABEL label=7
ASSIGN INT NAME PTR|INT value="b" NUM INT value=500
EQU BLABEL blabel=3 LABEL label=5
EQU BLABEL blabel=4 LABEL label=7
LABEL label=8
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=6 LABEL label=8
![Page 31: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/31.jpg)
Example
JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
JUMP INT BLABEL blabel=6
LABEL label=7
ASSIGN INT NAME PTR|INT value="b" NUM INT value=500
EQU BLABEL blabel=3 LABEL label=5
EQU BLABEL blabel=4 LABEL label=7
LABEL label=8
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=6 LABEL label=8
![Page 32: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/32.jpg)
Example
JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4 = 7
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
JUMP INT BLABEL blabel=6
LABEL label=7
ASSIGN INT NAME PTR|INT value="b" NUM INT value=500
EQU BLABEL blabel=3 LABEL label=5
EQU BLABEL blabel=4 LABEL label=7
LABEL label=8
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=6 LABEL label=8
![Page 33: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/33.jpg)
Example
JUMPT INT BLABEL blabel=3 = 5 CMPNE INT NUM INT value=0 DEREF INT NAME PTR|INT value="a"
JUMP INT BLABEL blabel=4 = 7
LABEL label=5
ASSIGN INT NAME PTR|INT value="b" NUM INT value=900
JUMP INT BLABEL blabel=6 = 8
LABEL label=7
ASSIGN INT NAME PTR|INT value="b" NUM INT value=500
EQU BLABEL blabel=3 LABEL label=5
EQU BLABEL blabel=4 LABEL label=7
LABEL label=8
ASSIGN INT NAME PTR|INT value="a" NUM INT value=200
EQU BLABEL blabel=6 LABEL label=8
![Page 34: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/34.jpg)
Backpatching at a Function End
Consider the productionfunc fbeg stmts m RBRACE
stmts is a linked list of backpatch labels that must be resolved.
If we reach the end of the function without yet resolving them, then they must be resolved to the return that occurs at the end of the function.
![Page 35: Backpatching](https://reader035.fdocuments.net/reader035/viewer/2022081603/56813a0e550346895da1e16f/html5/thumbnails/35.jpg)
Backpatching at a Function End
When the code is generated for this production, it is important that the backpatching occur first, before the return.