theory of compilation 236360 erez petrank
DESCRIPTION
Theory of Compilation 236360 Erez Petrank. Lecture 6: Intermediate Representation and Attribute Grammars. Source text . txt. Executable code. exe. You are here. Compiler. Lexical Analysis. Syntax Analysis. Semantic Analysis. Inter. Rep . ( IR). Code Gen. characters. tokens. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/1.jpg)
1
Theory of Compilation 236360
Erez PetrankLecture 6:
Intermediate Representation and Attribute Grammars
![Page 2: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/2.jpg)
You are here
2
Executable code
exeSourcetext
txt
Compiler
LexicalAnalysis
Syntax Analysis
Semantic
Analysis
Inter.Rep. (IR)
CodeGen.
characters
tokens AST(Abstract Syntax Tree)
Annotated AST
![Page 3: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/3.jpg)
3
GENERIC
Intermediate Representation• “neutral” representation between the front-end and the back-
end– Abstracts away details of the source language– Abstract away details of the target language
• Allows combination of various back-ends with various front-ends.
Java C Objective CAda
arm x86 ia64 …
…
![Page 4: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/4.jpg)
4
Intermediate Representation(s)• Annotated abstract syntax tree• Three address code• Postfix notation• …
• Sometimes we move between representations. E.g., – Syntax directed translation produces an annotated AST,– AST translated to three address code.
![Page 5: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/5.jpg)
Examples: Tree, DAG and Postfix
• Input:a := b * –c + b * –c
• Tree: DAG representation: assign assign
+ a + a
* * *
uminus b uminus b uminus b
c c c
postfix representation: a b c uminus * b c uminus * + assign
![Page 6: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/6.jpg)
6
Example: Annotated AST
• makeNode – creates new node for unary/binary operator
• makeLeaf – creates a leaf• id.place – pointer to symbol table
production semantic rule
S id := E➞ S.nptr = makeNode(‘assign’, makeLeaf(id,id.place), E.nptr)
E E1 + E2 ➞ E.nptr = makeNode(‘+’,E1.nptr,E2.nptr)
E E1 * E2➞ E.nptr = makeNode(‘*’,E1.nptr,E2.nptr)
E -E1➞ E.nptr = makeNode(‘uminus’,E1.nptr)
E (E1) ➞ E.nptr = E1.nptr
E id➞ E.nptr = makeLeaf(id,id.place)
![Page 7: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/7.jpg)
7
Memory Representation of an Annotated Tree
* *
uminus uminus
cid cid
bid bid
assign
+
aid
b id 0c id 1
1 uminus 22 0 * 3
b id 4c id 5
5 uminus 66 4 * 77 3 + 8
a id 98 9 assign 10
· · · 11
a = b * -c + b* -c
![Page 8: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/8.jpg)
8
Three Address Code (3AC)• Every instruction operates on three addresses
– result = operand1 operator operand2• Close to low-level operations in the machine
language – Operator is a basic operation
• Statements in the source language may be mapped to multiple instructions in three address code
![Page 9: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/9.jpg)
9
Three address code: example
assign
a +
*
b uminus
c
*
b uminus
c
t5:=at2 + t4:=t5
b * t3:=t4
– c:=t3
b * t1:=t2
– c:=t1
![Page 10: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/10.jpg)
10
Three address code: example instructionsinstruction meaning
x := y op z assignment with binary operator
x := op y assignment unary operator
x:= y assignment
x := &y assign address of y
x:=*y assignment from deref y
*x := y assignment to deref x
instruction meaning
goto L unconditional jump
if x relop y goto L conditional jump
![Page 11: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/11.jpg)
11
Array operations• Are these 3AC operations?
t1 := &y ; t1 = address-of yt2 := t1 + i ; t2 = address of y[i]X := *t2 ; value stored at y[i]
t1 := &x ; t1 = address-of xt2 := t1 + i ; t2 = address of x[i]*t2:= y ; store through pointer
x := y[i]
x[i] := y
![Page 12: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/12.jpg)
12
Three address code: exampleint main(void) { int i; int b[10]; for (i = 0; i < 10; ++i) b[i] = i*i; }
i := 0 ; assignmentL1: if i >= 10 goto L2 ; conditional jump t0 := i*i t1 := &b ; address-of operation t2 := t1 + i ; t2 holds the address of b[i] *t2 := t0 ; store through pointer i := i + 1 goto L1L2:
(example source: wikipedia)
![Page 13: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/13.jpg)
13
Three address code• Choice of instructions and operators affects code
generation and optimization
• Small set of instructions– Easy to generate machine code– Harder to optimize
• Large set of instructions– Harder to generate machine code
• Typically prefer small set and smart optimizer
![Page 14: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/14.jpg)
14
Creating 3AC• Assume bottom up parser
– Covers a wider range of grammars– LALR sufficient to cover most programming languages
• Creating 3AC via syntax directed translation• Attributes examples:
– code – code generated for a nonterminal– var – name of variable that stores result of nonterminal
• freshVar() – helper function that returns the name of a fresh variable
![Page 15: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/15.jpg)
15
Last Week: Attribute Grammars• Adding attributes + actions to a grammar• Evaluating attributes
– Build AST– Build dependency graph– Evaluation based on topological order– (works as long as there are no cycles)
• L-attributes, S-attributed grammars– Pre-determined evaluation order– Can be integrated into parsing
![Page 16: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/16.jpg)
16
Creating 3AC: expressions
production semantic rule
S id := E➞ S.code := E. code || gen(id.var ‘:=‘ E.var)
E E1 + E2➞ E.var := freshVar(); E.code = E1.code || E2.code || gen(E.var ‘:=‘ E1.var ‘+’ E2.var)
E E1 * E2➞ E.var := freshVar(); E.code = E1.code || E2.code || gen(E.var ‘:=‘ E1.var ‘*’ E2.var)
E - E1➞ E.var := freshVar(); E.code = E1.code || gen(E.var ‘:=‘ ‘-’ E1.var)
E (E1)➞ E.var := E1.varE.code = ‘(‘ || E1.code || ‘)’
E id➞ E.var := id.var; E.code = ‘’
(we use || to denote concatenation of intermediate code fragments)
![Page 17: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/17.jpg)
17
exampleassign
a+
*
b uminus
c
*
b uminus
c
E.var = cE.code =‘’
E.var = bE.code =‘’
E.var = t2E.code =‘t1 = -c t2 = b*t1’
E.var = t1E.code =‘t1 = -c’
E.var = bE.code =‘’
E.var = cE.code =‘’
E.var = t3E.code =‘t3 = -c’
E.var = t4E.code =‘t3 = -c t4 = b*t3’
E.var = t5E.code =‘t1 = -c t2 = b*t1 t3 = -c t4 = b*t3 t5 = t2+t4’
a = b * -c + b* -c
E.var = aE.code =‘’
E.code =‘t1 = -c t2 = b*t1 t3 = -c t4 = b*t3 t5 = t2+t4
a = t5’
![Page 18: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/18.jpg)
18
Creating 3AC: control statements
• 3AC only supports conditional/unconditional jumps
• Add labels • Attributes
– begin – label marks beginning of code– after – label marks end of code
• Helper function freshLabel() allocates a new fresh label
![Page 19: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/19.jpg)
19
Creating 3AC: control statements
S while E do S1
production semantic rule
S while E do S1 S.begin := freshLabel();S.after = freshLabel();S.code := gen(S.begin ‘:’) || E.code || gen(‘if’ E.var ‘=‘ ‘0’ ‘goto’ S.after) || S1.code || gen(‘goto’ S.begin) || gen(S.after ‘:’)
E.code S.begin:
if E.var = 0 goto S.after
S1.code
goto S.begin· · · S.after:
![Page 20: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/20.jpg)
22
Allocating Memory• Type checking helped us guarantee correctness• Also tells us
– How much memory allocate on the heap/stack for varaibles
– Where to find variables (based on offsets)– Compute address of an element inside array (size of
stride based on type of element)
![Page 21: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/21.jpg)
23
Declarations and Memory Allocation• Global variable “offset” with memory allocated
so far
production semantic rule
P D➞ { offset := 0}
D D D➞D T id;➞ { enter(id.name, T.type, offset); offset += T.width }
T integer➞ { T.type := int; T.width = 4 }
T float ➞ { T.type := float; T.width = 8}
T T1[num]➞ { T.type = array (num.val,T1.Type); T.width = num.val * T1.width; }
T *T1➞ { T.type := pointer(T1.type); T.width = 4}
![Page 22: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/22.jpg)
Allocating MemoryP
D2D1
D4
T1 id
int count
T2 id
float money
D5
T3 id
balancesT4 [ num ]
int 42T2.type = floatT2.width = 4
id.name = money
T1.type = intT1.width = 4
id.name = count
enter(count, int, 0) offset = offset + 4
enter(money, float, 4) offset = offset + 4
D6
![Page 23: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/23.jpg)
25
Adjusting to bottom-up
production semantic rule
P ➞ M D
M ➞ { offset := 0}
D D D➞D T id;➞ { enter(id.name, T.type, offset); offset += T.width }
T integer➞ { T.type := int; T.width = 4 }
T float ➞ { T.type := float; T.width = 8}
T T1[num]➞ { T.type = array (num.val,T1.Type); T.width = num.val * T1.width; }
T *T1➞ { T.type := pointer(T1.type); T.width = 4}
• On a top-down parsing, we can zero the offset initially. But for bottom-up, we don’t know what the first instruction is…
• A standard trick: add a marker and a rule that will always be first.
P
M D
Є
![Page 24: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/24.jpg)
Allocating MemoryP
D2D1
D4
T1 id
int count
T2 id
float money
D5
T3 id
balancesT4 [ num ]
int 42T1.type = intT1.width = 4
id.name = count
enter(count, int, 0) offset = offset + 4
D6M
offset = 0
![Page 25: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/25.jpg)
27
Generating IR code• Option 1
accumulate code in AST attributes
• Option 2emit IR code to a file during compilation– If for every production rule the code of the left-hand-side
is constructed from a concatenation of the code of the RHS in some fixed order
– Does not allow code manipulations after emitting.
![Page 26: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/26.jpg)
28
Expressions and assignments
production semantic action
S id := E➞ { p:= lookup(id.name); if p ≠ null then emit(p ‘:=‘ E.var) else error }
E E1 op E2➞ { E.var := freshVar(); emit(E.var ‘:=‘ E1.var op E2.var) }
E - E1➞ { E.var := freshVar(); emit(E.var ‘:=‘ ‘uminus’ E1.var) }
E ( E1)➞ { E.var := E1.var }
E id➞ { p:= lookup(id.name); if p ≠ null then E.var :=p else error }
• Lookup returns the variable location in memory. • Allocate space for a variable at first sight.
![Page 27: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/27.jpg)
29
Boolean Expressions
production semantic action
E E1 op E2➞ { E.var := freshVar(); emit(E.var ‘:=‘ E1.var op E2.var) }
E not E1➞ { E.var := freshVar(); emit(E.var ‘:=‘ ‘not’ E1.var) }
E ( E1)➞ { E.var := E1.var }
E true➞ { E.var := freshVar(); emit(E.var ‘:=‘ ‘1’) }
E false➞ { E.var := freshVar(); emit(E.var ‘:=‘ ‘0’) }
• Represent true as 1, false as 0• Wasteful representation, creating variables for true/false
![Page 28: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/28.jpg)
30
Boolean expressions via jumps
production semantic action
E id1 op id2➞ { E.var := freshVar(); emit(‘if’ id1.var relop id2.var ‘goto’ nextStmt+2);emit( E.var ‘:=‘ ‘0’);emit(‘goto ‘ nextStmt + 1);emit(E.var ‘:=‘ ‘1’)}
This is useful for short circuit evaluation. Let us start with an example.
![Page 29: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/29.jpg)
31
Boolean Expressions: an ExampleE
E E
a < b
or
E
c < d
E
e < f
and
![Page 30: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/30.jpg)
32
Boolean Expressions: an ExampleE
E E
a < b
or
E
c < d
E
e < f
and
if a < b goto 103 100:T1 := 0 101:goto 104 102:T1 := 1 103:
![Page 31: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/31.jpg)
33
Boolean Expressions: an ExampleE
E E
a < b
or
E
c < d
E
e < f
and
if a < b goto 103 100:T1 := 0 101:goto 104 102:T1 := 1 103:
if c < d goto 107 104:T2 := 0 105:goto 108 106:T2 := 1 107:
![Page 32: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/32.jpg)
34
Boolean Expressions: an ExampleE
E E
a < b
or
E
c < d
E
e < f
and
if a < b goto 103 100:T1 := 0 101:goto 104 102:T1 := 1 103:
if c < d goto 107 104:T2 := 0 105:goto 108 106:T2 := 1 107:
if e < f goto 111 108:T3 := 0 109:goto 112 110:T3 := 1 111:
112:
![Page 33: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/33.jpg)
35
Boolean Expressions: an ExampleE
E E
a < b
or
E
c < d
E
e < f
and
if a < b goto 103 100:T1 := 0 101:goto 104 102:T1 := 1 103:
if c < d goto 107 104:T2 := 0 105:goto 108 106:T2 := 1 107:
if e < f goto 111 108:T3 := 0 109:goto 112 110:T3 := 1 111:
112:113:T4 := T2 and T3
![Page 34: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/34.jpg)
36
Boolean Expressions: an ExampleE
E E
a < b
or
E
c < d
E
e < f
and
if a < b goto 103 100:T1 := 0 101:goto 104 102:T1 := 1 103:
if c < d goto 107 104:T2 := 0 105:goto 108 106:T2 := 1 107:
if e < f goto 111 108:T3 := 0 109:goto 112 110:T3 := 1 111:
112:113:T4 := T2 and T3
T5 := T1 or T4
![Page 35: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/35.jpg)
37
Short circuit evaluation• Second argument of a boolean operator is only evaluated if
the first argument does not already determine the outcome
• (x and y) is equivalent to: “if x then y else false”;• (x or y) is equivalent to: “if x then true else y”;
• Is short circuit evaluation equivalent to standard evaluation?
• We use it if the language definition dictates its use.
![Page 36: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/36.jpg)
38
Note Differenceint denom = 0;if (denom && nom/denom) { oops_i_just_divided_by_zero();}
if ( (file=open(“c:\grades”) or (die) ) printfile(file,…);
![Page 37: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/37.jpg)
39
Our previous examplea < b or (c<d and e<f)
100: if a < b goto 103101: T1 := 0102: goto 104103: T1 := 1104: if c < d goto 107105: T2 := 0106: goto 108107: T2 := 1108: if e < f goto 111109: T3 := 0110: goto 112111: T3 := 1112: T4 := T2 and T3
113: T5 := T1 and T4
100: if a < b goto 105101: if !(c < d) goto 103102: if e < f goto 105103: T := 0104: goto 106105: T := 1106:
naive Short circuit evaluation
![Page 38: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/38.jpg)
Control Structure: if, else, while.
• Consider the conditional jumps:
• Option 1: create code for B, create code for S, create a jump: beginning of S or end of S according to B’s value.
• More efficient: create code that jumps to the correct location immediately when the value of B is discovered.
if B then S1
if B then S1 else S2
while B do S1
S →||
![Page 39: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/39.jpg)
Control Structure: if, else, while. • The problem: we do not know where to jump to…• While parsing B’s tree for “if B then S”, we don’t know S’s
code and where it starts or ends. • Solution: for each expression B keep labels: B.trueLabel and
B.falseLabel. Jump to these labels when according to B’s value.
• Also, for each statement S, keep a label S.nextLabel with the address of the code that follows the statement S.
S
if B then S
•The semantic equation:B.falseLabel = S.next .
•For B.true, we create a new label between B’s code and S’s code .
![Page 40: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/40.jpg)
next התכונה• While parsing statement S, generate the code with its
next label.
• Is S.next inherited or synthesized? • Is S.code inherited or synthesized? • The label S.next is symbolic. The address will only be
known after we parse S.
production semantic action
P S S.next = freshLabel();P.code = S.code || label(S.next)
S S1 S2 S1.next = freshLabel();S2.next = S.next;S.code = S1.code || label(S1.next) || S2.code
![Page 41: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/41.jpg)
43
Control Structures: conditionalproduction semantic actionS if B then S1 B.trueLabel = freshLabel();
B.falseLabel = S.next;S1.next = S.next;S.code = B.code || gen (B.trueLabel ‘:’) || S1.code
“gen” creates the instruction and returns it as a string. (Unlike emit.)
Are S1.next, B.falseLabel inherited or synthesized?
Is S.code inherited or synthesized?. . .B.false:Code for S
B.true:
Code for B with jumps
→ to B.true
→ to B.false
![Page 42: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/42.jpg)
44
Control Structures: conditionalproduction semantic action
S if B then S1 else S2
B.trueLabel = freshLabel();B.falseLabel = freshLabel();S1.next = S.next;S2.next = S.next;S.code = B.code || gen(B.trueLabel ‘:’) || S1.code || gen(‘goto’ S.next) || gen(B.falseLabel ‘:’) || S2.code
B.trueLabel and B.falseLabel considered inherited
→ to B.trueLabel→ to B.falseLabel
B.code
S1.code B.trueLabel:
goto S.next
S2.code B.falseLabel:
. . . S.next:
![Page 43: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/43.jpg)
45
Boolean expressionsproduction semantic action
B B1 or B2 B1.trueLabel = B.trueLabel; B1.falseLabel = freshLabel(); B2.trueLabel = B.trueLabel; B2.falseLabel = B.falseLabel;B.code = B1.code || gen (B1.falseLabel ‘:’) || B2.code
B B1 and B2 B1.trueLabel = freshLabel(); B1.falseLabel = B.falseLabel;B2.trueLabel = B.trueLabel; B2.falseLabel = B.falseLabel;B.code = B1.code || gen (B1.trueLabel ‘:’) || B2.code
B not B1 B1.trueLabel = B.falseLabel; B1.falseLabel = B.trueLabel; B.code = B1.code;
B (B1) B1.trueLabel = B.trueLabel; B1.falseLabel = B.falseLabel; B.code = B1.code;
B id1 relop id2
B.code=gen (‘if’ id1.var relop id2.var ‘goto’ B.trueLabel)||gen(‘goto’ B.falseLabel);
B true B.code = gen(‘goto’ B.trueLabel)
B false B.code = gen(‘goto’ B.falseLabel);
![Page 44: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/44.jpg)
46
Boolean expressions
• How can we determine the address of B1.falseLabel?
• Only possible after we know the code of B1 and all the code preceding B1
production semantic action
B B1 or B2 B1.trueLabel = B.trueLabel; B1.falseLabel = freshLabel();B2.trueLabel = B.trueLabel;B.falseLabel = B.falseLabel;B.code = B1.code || gen (B1.falseLabel ‘:’) || B2.code
![Page 45: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/45.jpg)
47
Example
S
if B then S1
B1 B2and
falsetrue
B1.trueLabel = freshLabel();B1.falseLabel = B.falseLabel;B2.trueLabel = B.trueLabel;B2.falseLabel = B.falseLabel;B.code = B1.code || gen (B1.trueLabel ‘:’) || B2.code
B.code = gen(‘goto’ B1.trueLabel)
B.code = gen(‘goto’ B2.falseLabel)
![Page 46: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/46.jpg)
48
Example
S
if B then S1
B1 B2and
falsetrue
B.trueLabel = freshLabel();B.falseLabel = S.next;S1.next = S.next;S.code = B.code || gen (B.trueLabel ‘:’) || S1.code
B1.trueLabel = freshLabel();B1.falseLabel = B.falseLabel;B2.trueLabel = B.trueLabel;B2.falseLabel = B.falseLabel;B.code = B1.code || gen (B1.trueLabel ‘:’) || B2.code
B.code = gen(‘goto’ B1.trueLabel)
B.code = gen(‘goto’ B2.falseLabel)
![Page 47: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/47.jpg)
49
Computing the labels• We can build the code while parsing the tree bottom-up,
leaving the jump targets vacant. • We can compute the values for the labels in a second
traversal of the AST
• Can we do it in a single pass?
![Page 48: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/48.jpg)
50
So far…• Three address code.• Intermediate code generation is executed with parsing (via
semantic actions). • Creating code for Boolean expressions and for control
statements is more involved. • We typically use short circuit evaluation, value of
expression is implicit in control location. • We need to compute the branching addresses.• Option 1: compute them in a second AST pass.
![Page 49: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/49.jpg)
51
Backpatching (תיקון לאחור)• Goal: generate code in a single pass
• Generate code as we did before, but manage labels differently
• Keep targets of jumps empty until values are known, and then back-patch them
• New synthesized attributes for B– B.truelist – list of jump instructions that eventually get the
label where B goes when B is true.– B.falselist – list of jump instructions that eventually get the
label where B goes when B is false.
![Page 50: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/50.jpg)
52
Backpatching• For every label, maintain a list of instructions that
jump to this label• When the address of the label is known, go over
the list and update the address of the label
• Previous solutions do not guarantee a single pass– The attribute grammar we had before is not S-attributed
(e.g., next is inherited), and is not L-attributed (because the inherited attributes are not necessarily from left siblings).
![Page 51: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/51.jpg)
Why is it difficult?
• Think of an if-then-else statement. • Computing S1.next requires the code of S1, B, and S2. • To compute the code of S1, we need to know S1.next, or
S.next (jump targets). • Solution: compute S1’s code up to “leaving space” for
jump targets (S1.next). • Backpatching: maintain a list of all empty spaces that
need to jump to S1.next. • When we know S1.next, we go over this list and update
the jump targets with S1.next’s value.
if B
S
then elseS1 S2
![Page 52: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/52.jpg)
54
Functions for list handling• makelist(addr) – create a list of instructions
containing addr• merge(p1,p2) – concatenate the lists pointed to
by p1 and p2, returns a pointer to the new list• backpatch(p,addr) – inserts addr as the target
label for each of the instructions in the list pointed to by p
![Page 53: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/53.jpg)
Accumulating the code• Bottom-up parsing, code created from left to right, bottom-
up. • Semantic analysis executed during parsing and the code is
accumulated.
• Without backpatching: B.trueLabel and B.falseLabel for each expression B: where we should jump if B turns out true or false.
• Backpatching: B.truelist and B.falselist: instructions with missing jump target address.
• Also, S.nextlist: all instructions that should jump to after S.
![Page 54: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/54.jpg)
56
NextInstr()• Emit:
– generates an instruction and puts it in CodeBuffer(nextInstr)
– ++nextInstr
• NextInstr() returns the address of the next instruction.
![Page 55: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/55.jpg)
Computing Boolean expressions • Recall the code jumps to B.true ot B.false according to B’s
value.• Previously:
• Now with backpatching:
{ B.code := gen ( ' if ' id1.var relop.op id2.var ' goto ' B.true ) || gen ( ' goto ' B.false ) }
B → id1 relop id2
{ B.code := gen ( ' goto ' B.true ) } B → true{ B.code := gen ( ' goto ' B.false ) } B → false
{ B.truelist := makelist ( nextinstr ) ; B.falselist := makelist ( nextinstr + 1 ) ; emit ( ' if ' id1.var relop.op id2.var ' goto_ ' ); emit ( ' goto_ ' ); }
B → id1 relop id2
{ B.truelist := makelist ( nextinstr ) ; emit ( ' goto_ ' ) } B → true{ B.falselist := makelist ( nextinstr ) ; emit ( ' goto_ ' ) } B → false
![Page 56: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/56.jpg)
Computing Boolean expressions• Recall the code jumps to B.true ot B.false according to B’s
value.• Previously:
• Now with backpatching:
{ B1.true := B.false ; B1.false := B.true ; B.code := B1.code }
B → not B1
{ B1.true := B.true ; B1.false := B.false ; B.code := B1.code } B → ( B1 )
{ B.truelist := B1.falselist ; B.falselist := B1.truelist }
B → not B1
{ B.truelist := B1.truelist ; B.falselist := B1.falselist }
B → ( B1 )
![Page 57: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/57.jpg)
Computing Boolean expressions• Recall the code jumps to B.true ot B.false according to B’s
value.
• Now with backpatching:
{ B1.true := B.true ; B1.false := newlable() ; B2.true := B.true ; B2.false := B.false ; B.code := B1.code || gen ( B1.false ' : ') || B2.code }
B → B1 or B2
{ B1.true := newlabel (); B1.false := B.false ; B2.true := B.true ; B2.false := B.false ; B.code := B1.code || gen ( B1.true ' : (' || B2.code }
B → B1 and B2
{ backpatch ( B1.falselist, M.instr ) ; B.truelist := merge ( B1.truelist, B2.truelist ) ; B.falselist := B2.falselist }
B → B1 or M B2
{ backpatch ( B1.truelist, M.instr ) ; B.truelist := B2.truelist ; B.falselist := merge ( B1.falselist, B2.falselist ) }
B → B1 and M B2
{ M.instr := nextinstr } M →
![Page 58: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/58.jpg)
60
Using a marker
• { M.instr = nextinstr;}• Use M to obtain the address just before B2 code starts
being generated
B1 or
B
M B2
Example:
![Page 59: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/59.jpg)
61
Backpatching Boolean expressionsproduction semantic action
B B1 or M B2 backpatch(B1.falseList,M.instr);B.trueList = merge(B1.trueList,B2.trueList);B.falseList = B2.falseList;
B B1 and M B2 backpatch(B1.trueList,M.instr);B.trueList = B2.trueList;B.falseList = merge(B1.falseList,B2.falseList);
B not B1 B.trueList = B1.falseList;B.falseList = B1.trueList;
B (B1) B.trueList = B1.trueList;B.falseList = B1.falseList;
B id1 relop id2 B.trueList = makeList(nextInstr);B.falseList = makeList(nextInstr+1);emit (‘if’ id1.var relop id2.var ‘goto _’) || emit(‘goto _’);
B true B.trueList = makeList(nextInstr);emit (‘goto _’);
B false B.falseList = makeList(nextInstr);emit (‘goto _’);
M M.instr = nextinstr;
![Page 60: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/60.jpg)
62
Using a marker
• { M.instr = nextinstr;}• Use M to obtain the address just before B2 code starts
being generated
B1 or
B
M B2
Example:
![Page 61: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/61.jpg)
63
ExampleX < 100 or x > 200 and x != y
B
B
x < 100
B
x > 200
B
x != y
B
and
or M
M
100: if x< 100 goto _101: goto _
B id1 relop id2 B.trueList = makeList(nextInstr);B.falseList = makeList(nextInstr+1);emit (‘if’ id1.var relop id2.var ‘goto _’) || emit(‘goto _’);
B.t = {100}B.f = {101}
![Page 62: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/62.jpg)
64
ExampleX < 100 or x > 200 and x != y
B
B
x < 100
B
x > 200
B
x != y
B
and
or M
M
100: if x< 100 goto _101: goto _
B.t = {100}B.f = {101}
M M.instr = nextinstr;
M.i = 102
![Page 63: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/63.jpg)
65
ExampleX < 100 or x > 200 and x != y
B
B
x < 100
B
x > 200
B
x != y
B
and
or M
M
100: if x< 100 goto _101: goto _
B.t = {100}B.f = {101} M.i = 102
B id1 relop id2 B.trueList = makeList(nextInstr);B.falseList = makeList(nextInstr+1);emit (‘if’ id1.var relop id2.var ‘goto _’) || emit(‘goto _’);
102: if x> 200 goto _103: goto _
B.t = {102}B.f = {103}
![Page 64: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/64.jpg)
66
ExampleX < 100 or x > 200 and x != y
B
B
x < 100
B
x > 200
B
x != y
B
and
or M
M
100: if x< 100 goto _101: goto _
B.t = {100}B.f = {101} M.i = 102
102: if x> 200 goto _103: goto _
B.t = {102}B.f = {103}
M M.instr = nextinstr;
M.i = 104
![Page 65: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/65.jpg)
67
ExampleX < 100 or x > 200 and x != y
B
B
x < 100
B
x > 200
B
x != y
B
and
or M
M
100: if x< 100 goto _101: goto _
B.t = {100}B.f = {101} M.i = 102
102: if x> 200 goto _103: goto _
B.t = {102}B.f = {103}
M.i = 104
B id1 relop id2 B.trueList = makeList(nextInstr);B.falseList = makeList(nextInstr+1);emit (‘if’ id1.var relop id2.var ‘goto _’) || emit(‘goto _’);
104: if x!=y goto _105: goto _
B.t = {104}B.f = {105}
![Page 66: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/66.jpg)
68
ExampleX < 100 or x > 200 and x != y
B
B
x < 100
B
x > 200
B
x != y
B
and
or M
M
100: if x< 100 goto _101: goto _
B.t = {100}B.f = {101} M.i = 102
102: if x> 200 goto 104103: goto _
B.t = {102}B.f = {103}
M.i = 104
104: if x!=y goto _105: goto _
B.t = {104}B.f = {105}
B B1 and M B2 backpatch(B1.trueList,M.instr);B.trueList = B2.trueList;B.falseList = merge(B1.falseList,B2.falseList);
B.t = {104}B.f = {103,105}
![Page 67: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/67.jpg)
69
ExampleX < 100 or x > 200 and x != y
B
B
x < 100
B
x > 200
B
x != y
B
and
or M
M
100: if x< 100 goto _101: goto 102
B.t = {100}B.f = {101} M.i = 102
102: if x> 200 goto 104103: goto _
B.t = {102}B.f = {103}
M.i = 104
104: if x!=y goto _105: goto _
B.t = {104}B.f = {105}
B.t = {104}B.f = {103,105}
B B1 or M B2 backpatch(B1.falseList,M.instr);B.trueList = merge(B1.trueList,B2.trueList);B.falseList = B2.falseList;
B.t = {100,104}B.f = {103,105}
![Page 68: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/68.jpg)
70
Example
100: if x<100 goto _101: goto _102: if x>200 goto _103: goto _104: if x!=y goto _105: goto _
100: if x<100 goto _101: goto _102: if x>200 goto 104103: goto _104: if x!=y goto _105: goto _
100: if x<100 goto _101: goto 102102: if x>200 goto 104103: goto _104: if x!=y goto _105: goto _
Before backpatching After backpatchingby the productionB B1 and M B2
After backpatchingby the productionB B1 or M B2
![Page 69: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/69.jpg)
71
Backpatching for statementsproduction semantic action
S if (B) M S1 backpatch(B.trueList,M.instr);S.nextList = merge(B.falseList,S1.nextList);
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);
S while M1 (B) M2 S1
backpatch(S1.nextList,M1.instr);backpatch(B.trueList,M2.instr);S.nextList = B.falseList;emit(‘goto’ M1.instr);
S { L } S.nextList = L.nextList;
S A S.nextList = null;
M M.instr = nextinstr;
N N.nextList = makeList(nextInstr); emit(‘goto _’);
L L1 M S backpatch(L1.nextList,M.instr); L.nextList = S.nextList;
L S L.nextList = S.nextList
![Page 70: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/70.jpg)
72
Generate code for procedures
• we will see handling of procedure calls in much more detail later
n = f(a[i]);
t1 = i * 4t2 = a[t1] // could have expanded this as well param t2t3 = call f, 1n = t3
![Page 71: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/71.jpg)
73
Extend grammar for procedures
• type checking– function type: return type, type of formal parameters– within an expression function treated like any other
operator• symbol table
– parameter names
D define T id (F) { S } F | T id, FS return E; | …E id (A) | … A | E, A expressions
statements
![Page 72: Theory of Compilation 236360 Erez Petrank](https://reader035.vdocuments.us/reader035/viewer/2022062305/568161e3550346895dd1fdd7/html5/thumbnails/72.jpg)
74
Summary• Three address code.• Intermediate code generation is executed with parsing (via
semantic actions). • Creating code for Boolean expressions and for control
statements is more involved. • We typically use short circuit evaluation, value of
expression is implicit in control location. • We need to compute the branching addresses.• Option 1: compute them in a second AST pass.• Option 2: backpatching (a single pass): maintain lists of
incomplete jumps, where all jumps in a list have the same target. When the target becomes known, all instructions on its list are “filled in”.