Presentation is loading. Please wait.

Presentation is loading. Please wait.

Backpatching Lecture 24 Fri, Apr 16, 2004. Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java.

Similar presentations


Presentation on theme: "Backpatching Lecture 24 Fri, Apr 16, 2004. Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java."— Presentation transcript:

1 Backpatching Lecture 24 Fri, Apr 16, 2004

2 Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java 1.2 and we are using Java 1.1.8. To have access to the LinkedList class, you will have to download Java 1.4.2 from Sun Microsystems. Go to http://java.sun.com/j2se/1.4.2/download.html

3 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 write nonterminal LinkedList stmts, stmt, n;

4 Labels and Jumps in the Grammar The productions that will involve jumps are stmts  stmts m stmt stmt  IF ( cexpr ) m stmt stmt  IF ( cexpr ) m stmt n ELSE m stmt func  fbeg stmts m RBRACE Remember, m represents a destination. n represents a jump.

5 Sequences of Statements Consider the first production. stmts  stmts 1 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.

6 Sequences of Statments The label at m serves as the “next” destination of stmts 1.

7 Sequences of Statments The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute). stmts  stmts 1 m stmt

8 Sequences of Statments The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute). stmts  stmts 1 m stmt next

9 Sequences of Statments The “next” destination from stmt becomes the “next” destination from stmts (a synthesized attribute). stmts  stmts 1 m stmt next

10 Action We must take the following actions. Backpatch stmts 1.nextList to m. Return stmt.nextList (to be the nextList of stmts). This resolves the “next” destination from stmts 1 and leaves the “next” destination from stmt unresolved.

11 The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 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 stmt 1. stmt  IF ( cexpr ) m stmt 1

12 The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 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 stmt 1. stmt  IF ( cexpr ) m stmt 1 T

13 The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 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 stmt 1. stmt  IF ( cexpr ) m stmt 1 T F

14 The One-Way if Statement Now consider the one-way if statement. stmt  IF ( cexpr ) m stmt 1 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 stmt 1. stmt  IF ( cexpr ) m stmt 1 T F next

15 Action We must take the following actions. Backpatch cexpr.trueList to m. Merge cexpr.falseList and stmt 1.nextList. Return the merged list (to be the nextList of stmt).

16 Example Consider the following segment of code. if (a) b = 900; a = 200;

17 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

18 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

19 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

20 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

21 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

22 Two-Way if Statements The two-way if production is more interesting. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 The “true” destination from cexpr is m 1 and the “false” destination is m 2. The “next” destination of stmt 1 is the same as the “next destination of n. n represents a jump to the “next” destination of stmt 2. The “next” destination of stmt 2 becomes the “next” destination of stmt.

23 Two-Way if Statements Consider the two-way if statement. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2

24 Two-Way if Statements If cexpr is true, then execution jumps to the label m 1. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T

25 Two-Way if Statements If cexpr is false, then execution jumps to the label m 2. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F

26 Two-Way if Statements When stmt 1 is completed, execution jumps to the statement following the if statement, i.e., it jumps to stmt.nextList. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F next

27 Two-Way if Statements If execution reaches n, then it jumps to stmt.nextList. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F next

28 Two-Way if Statements When stmt 2 is completed, execution jumps to stmt.nextList. stmt  IF ( cexpr ) m 1 stmt 1 n ELSE m 2 stmt 2 T F next

29 Action We must take the following actions. Backpatch cexpr.trueList to m 1. Backpatch cexpr.falseList to m 2. Merge stmt 1.nextList, n.nextList, and stmt 2.nextList.

30 Example Consider the following segment of code. if (a) b = 900; else b = 500; a = 200;

31 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

32 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

33 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

34 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

35 Backpatching at a Function End Consider the production func  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.

36 Backpatching at a Function End When the code is generated for this production, it is important that the backpatching occur first, before the return.


Download ppt "Backpatching Lecture 24 Fri, Apr 16, 2004. Linked Lists in Java The Java Platform includes a LinkedList class. Unfortunately, it was introduced in Java."

Similar presentations


Ads by Google