Top Banner
Formal Languages and Compilers Lecture X—Intermediate Code Generation Alessandro Artale Free University of Bozen-Bolzano Faculty of Computer Science – POS Building, Room: 2.03 [email protected] http://www.inf.unibz.it/artale/ Formal Languages and Compilers — BSc course 2019/20 – Second Semester Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation
23

Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Jun 25, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Formal Languages and CompilersLecture X—Intermediate Code Generation

Alessandro Artale

Free University of Bozen-BolzanoFaculty of Computer Science – POS Building, Room: 2.03

[email protected]

http://www.inf.unibz.it/∼artale/

Formal Languages and Compilers — BSc course

2019/20 – Second Semester

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 2: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Summary of Lecture X

Three-Address Code

Code for Assignments

Boolean Expressions and Flow-of-Control Statements

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 3: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Intermediate Code Generation

An intermediate code is generated as a program for an abstract machine.1 The intermediate code should be easy to translate into the target program.2 A machine-independent Code Optimizer can be applied before generating the

target code.

As intermediate code we consider the three-address code, similar toassembly: sequence of instructions with at most three operands such that:

1 There is at most one operator, in addition to the assignment (we make explicitthe operators precedence).

2 The general form is: x := y op z

where x,y,z are called addresses, i.e., either identifiers, constants orcompiler-generated temporary names.

Temporary names must be generated to compute intermediate operations.Addresses are implemented as pointers to their symbol-table entries.

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 4: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Types of Three-Address Statements

Three-Address statements are akin to assembly code: Statements can have labelsand there are statements for flow-of-control.

1 Assignment Statements: x := y op z.2 Unary Assignment Statements: x := op y.3 Copy Statements: x := y.4 Unconditional Jump: goto L, with L a label of a statement.5 Conditional Jump: if x relop y goto L.

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 5: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Types of Three-Address Statements (Cont.)

6 Procedure Call: param x, and call p,n for calling a procedure, p, with n

parameters. With return y the returned value of the procedure is indicated.param x1

param x2

. . .

param xn

call p, n

7 Indexed assignments: x := y[i] or x[i] := y.Note: x[i] denotes the value in the location i memory units beyond thelocation x.

8 Pointer Assignmets: x := &y, x := *y, or *x := y; where &y stands for theaddress of y, and *y for the value stored at y.

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 6: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Summary

Three-Address Code

Code for Assignments

Boolean Expressions and Flow-of-Control Statements

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 7: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Assignments and Symbol Tables: The TranslationThe following S-attributed definition generates three-address code for assignments.

Production Semantic RulesS → id := E p := lookup(id.name);

if p ̸= nil then emit(p ′ :=′ E .addr)else error

E → E1 + E2 E .addr := newtemp();emit(E .addr ′ :=′ E1.addr ′+′ E2.addr)

E → E1 ∗ E2 E .addr := newtemp();emit(E .addr ′ :=′ E1.addr ′∗′ E2.addr)

E → −E1 E .addr := newtemp();emit(E .addr ′ :=′ ′uminus′ E1.addr)

E → (E1) E .addr := E1.addrE → id p := lookup(id.name);

if p ̸= nil then E .addr := pelse error

E → num E .addr := newtemp();E .addr := num.val

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 8: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Assignments and Symbol Tables: Notes

The function emit() output to a file a three-address code such that:1 Everything quoted is taken literally;2 The rest is evaluated.

Temporary names are generated for intermediate computations.The function newtemp() generates distinct temporary names t1, t2, . . ..

Expressions have a synthesized attribute:1 E .addr : Temporary name holding the value of E ;

Names/addresses stand for pointers to their symbol table entries: other infoare needed for the final code generation (in particular, the storage address).

Note. Under this assumption Temporary Names must be also entered into thesymbol table as they are created by the newtemp() function.

The function lookup(id.name) return nil if the entry is not found in thesymbol table, otherwise a pointer to the entry is returned.

The lookup(id.name) can be easily modified to account for scope: If namedoes not appear in the current symbol table the enclosing symbol table ischecked (see the Lecture on “Symbol Table”).

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 9: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Code for Assignments: An Example

Given the assignment a := b ∗ −c + d the code generated by the above grammar is:

S

:=id

a

E t3

E+E t2

id* E t1E

did E-

b id

c

t1 := uminus ct2 := b ∗ t1t3 := t2 + da := t3

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 10: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Summary

Three-Address Code

Code for Assignments

Boolean Expressions and Flow-of-Control Statements

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 11: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Boolean Expressions

Boolean Expressions are used to either compute logical values or asconditional expressions in flow-of-control statements.

We consider Boolean Expressions with the following grammar:

E → E or E | E and E | not E | (E ) | E relop E | true | false

There are two methods to evaluate Boolean Expressions1 Numerical Representation. Encode true with ’1’ and false with ’0’ and we

proceed analogously to arithmetic expressions.2 Jumping Code. We represent the value of a Boolean Expression by a position

reached in a program.

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 12: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Numerical Representation of Boolean Expressions

Expressions will be evaluated from left to right assuming that: or and and areleft-associative, and that or has lowest precedence, then and, and finally not.Example 1. The translation for “a or (b and (not c))” is:t1 := not ct2 := b and t1t3 := a or t2

Example 2. A relational expression such as a<b is equivalent to theconditional statement if a<b then 1 else 0. Its translation involves jumps tolabeled statements:100: if a<b goto 103

101: t := 0

102: goto 104

103: t := 1

104:

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 13: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Numerical Representation: The Translation

The following S-Attributed Definition makes use of the global variablenextstat that gives the index of the next three-address code statement andis incremented by emit.Production Semantic RulesE → E1 or E2 E .addr := newtemp();

emit(E .addr ′ :=′ E1.addr ′or′ E2.addr)E → E1 and E2 E .addr := newtemp();

emit(E .addr ′ :=′ E1.addr ′and′ E2.addr)E → not E1 E .addr := newtemp();

emit(E .addr ′ :=′ ′not′ E1.addr)E → (E1) E .addr := E1.addrE → E1 relop E2 E .addr := newtemp();

emit(′if ′ E1.addr relop.op E2.addr ′goto′

nextstat+ 3);emit(E .addr ′ :=′ ′0′);emit(′goto′ nextstat+ 2);emit(E .addr ′ :=′ ′1′)

E → true E .addr := newtemp(); emit(E .addr ′ :=′ ′1′)E → false E .addr := newtemp(); emit(E .addr ′ :=′ ′0′)

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 14: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Jumping Code for Boolean Expressions

The value of a Boolean Expression is represented by a position in the code.

Consider Example 2: We can tell what value t will have by whether we reachstatement 101 or statement 103.

Jumping code is extremely useful when Boolean Expressions are in the contextof flow-of-control statements.

We start by presenting the translation for flow-of-control statementsgenerated by the following grammar:S → if E then S

| if E then S1 else S2

| while E do S

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 15: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Flow-of-Control Statements

In the translation, we assume that a three-address code statement can have asymbolic label, and that the function newlabel() generates such labels.We associate with E two labels using inherited attributes:

1 E.true, the label to which control flows if E is true;2 E.false, the label to which control flows if E is false.

We associate to S the inherited attribute S.next that represents the labelattached to the first statement after the code for S .

Note 1. This method of generating symbolic labels can lead to a proliferationof label: The backpatching method (see the Book) creates labels only whenneeded and emits directly the code.

Note 2. To substitute symbolic labels with actual addresses a second pass isneeded: backpatching will avoid also the two-pass translation.

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 16: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Flow-of-Control Statements (Cont.)

The following figures show how the flow-of-control statements are translated.

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 17: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Flow-of-Control Statements: The Translation

Production Semantic RulesP → S S .next = newlabel();

P.code := S .code || gen(S .next ′ :′)S → if E then S1 E .true := newlabel(); E .false := S .next;

S1.next := S .next;S .code := E .code || gen(E .true ′ :′) || S1.code

S → if E then S1 else S2 E .true := newlabel(); E .false := newlabel();S1.next := S .next; S2.next := S .next;S .code := E .code || gen(E .true ′ :′) || S1.code ||

gen(′goto′ S .next) ||gen(E .false ′ :′) || S2.code

S → while E do S1 E .begin := newlabel();E .true := newlabel(); E .false := S .next;S1.next := E .begin;S .code := gen(E .begin ′ :′) || E .code ||

gen(E .true ′ :′) || S1.code ||gen(′goto′ E .begin)

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 18: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Flow-of-Control Statements with Translation Schemes

Translation SchemeP → {S .next = newlabel(); }{P.code := S .code || gen(S .next ′ :′)}S → if {E .true := newlabel(); E .false := S .next; } E then

{S1.next := S .next; } S1{S .code := E .code || gen(E .true ′ :′) ||S1.code}

S → if {E .true := newlabel(); E .false := newlabel(); } E then{S1.next := S .next; } S1 else { S2.next := S .next; } S2

{S .code := E .code || gen(E .true ′ :′) || S1.code ||gen(′goto′ S .next) || gen(E .false ′ :′) || S2.code}

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 19: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Jumping Code for Boolean Expressions (Cont.)

Boolean Expressions are translated in a sequence of conditional andunconditional jumps to either E.true or E.false.

a < b. The code is of the form:if a < b goto E.truegoto E.false

E1orE2. If E1 is true then E is true, so E1.true = E .true. Otherwise, E2 mustbe evaluated, so E1.false is set to the label of the first statement in the codefor E2.

E1andE2. Analogous considerations apply.

not E1. We just interchange the true and false with that for E .

Note. Both the true and false attributes are inherited and the translation isan L-attributed grammar.

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 20: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Jumping Code for Boolean Expressions: The Translation

Production Semantic RulesE → E1 or E2 E1.true := E .true; E1.false := newlabel();

E2.true := E .true; E2.false := E .false;E .code := E1.code || gen(E1.false ′ :′) || E2.code

E → E1 and E2 E1.true := newlabel(); E1.false := E .false;E2.true := E .true; E2.false := E .false;E .code := E1.code || gen(E1.true ′ :′) || E2.code

E → not E1 E1.true := E .false; E1.false := E .true;E .code := E1.code

E → (E1) E1.true := E .true; E1.false := E .false;E .code := E1.code

(Follows →)

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 21: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Jumping Code for Boolean Expressions: The Translation

Production Semantic RulesE → E1 relop E2 E .code := E1.code || E2.code ||

gen(′if ′ E1.addr relop.op E2.addr ′goto′ E .true) ||gen(′goto′ E .false)

E → id p = lookup(id.name);if(p.type = bool) then

E .code := gen(′if ′ p = true ′goto′ E .true)||gen(′goto′ E .false)

else if(p ̸= nil) thenE .addr = p;E .code =′ ′

else errorE → true E .code := gen(′goto′ E .true)E → false E .code := gen(′goto′ E .false)

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 22: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Flow-of-Control and Boolean Expressions: An Example

Example. Translate the following statement:while a < b do

if c or d thenx := y + z

elsex := y - z

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation

Page 23: Formal Languages and Compilers Lecture X—Intermediate Code ...artale/Compiler/Lectures/slide10-InterCode... · Intermediate Code Generation An intermediate code is generated as

Summary of Lecture X

Three-Address Code

Code for Assignments

Boolean Expressions and Flow-of-Control Statements

Alessandro Artale Formal Languages and Compilers Lecture X—Intermediate Code Generation