Intermediate Code Generation - Lecture Slides | CIS 631, Papers of Computer Science

Material Type: Paper; Class: Compiler Design; Subject: Computer & Info Science; University: Syracuse University; Term: Unknown 1989;

Typology: Papers

Pre 2010

Uploaded on 08/09/2009

koofers-user-t3e
koofers-user-t3e 🇺🇸

5

(2)

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Intermediate Code Generation
Forms of intermediate code vary from high level
Annotated abstract syntax trees
Directed acyclic graphs (common subexpressions are coalesced)
To the more low level Three Address Code
Each instruction has, at most, one binary operation
More abstract than machine instructions
No explicit memory allocation
No specific hardware architecture assumptions
Lower level than syntax trees
Control structures are spelled out in terms of instruction jumps
Suitable for many types of code optimization
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Intermediate Code Generation - Lecture Slides | CIS 631 and more Papers Computer Science in PDF only on Docsity!

Intermediate Code Generation

•^

Forms of intermediate code vary from high level^ –

Annotated abstract syntax trees

-^

Directed acyclic graphs (common subexpressions are coalesced)

•^

To the more low level Three Address Code^ –

Each instruction has, at most, one binary operation

-^

More abstract than machine instructions

  • No explicit memory allocation• No specific hardware architecture assumptions -^

Lower level than syntax trees

  • Control structures are spelled out in terms of instruction jumps -^

Suitable for many types of code optimization

Three Address Code •^

Consists of a sequence of instructions, each instruction mayhave up to three addresses, prototypically

t1 = t2 op t

•^

Addresses may be one of:^ –

A name. Each name is a symbol table index. For convenience, wewrite the names as the identifier.

-^

A constant.

-^

A compiler-generated temporary. Each time a temporary address isneeded, the compiler generates another name from the stream t1, t2,t3, etc.

  • Temporary names allow for code optimization to easily move

instructions

  • At target-code generation time, these names will be allocated to

registers or to memory.

Three Address Code Instructions •^

Unconditional jump: goto L^ –

L is a symbolic label of an instruction

•^

Conditional jumps:

if x goto L

and

ifFalse x goto L

–^

Left: If x is true, execute instruction L next

-^

Right: If x is false, execute instruction L next

•^

Conditional jumps:

if x relop y goto L

•^

Procedure calls. For a procedure call p(x1, …, xn)

param x1…param xncall p, n

Three Address Code Instructions •^

Indexed copy instructions:

x = y[i]

and x[i] = y

–^

Left: sets x to the value in the location i memory units beyond y

-^

Right: sets the contents of the location i memory units beyond y to x

•^

Address and pointer instructions:^ –

x = &y sets the r value of x to be the location (l value) of y.

-^

x = *y, presumably y is a pointer or temporary whose r value is alocation. The r value of x is set to the contents of that location.

-^

*x = y sets the r value of the object pointed to by x to the r value of y.

SDD of Intermediate Code •^

Incremental Translation^ –

Instead of using an attribute to keep the generated code, we assumethat we can generate instructions into a stream of instructions

  • gen(<three address instruction) generates an instruction• new Temp() generates a new temporary• lookup(top, id) returns the symbol table entry for id at the

topmost (innermost) lexical level

  • newlabel() generates a new abstract label name

Translation of Expressions •^

Uses the attribute addr to keep the addr of the instruction for thatnonterminal symbol.

E.addr = lookup(top, id.text)

| id

E.addr = E1.addr

| ( E1 )

E.addr = new Temp()Gen(E.addr = minus E1.addr)

| - E

E.addr = new Temp()Gen(E.addr = E1.addr plus E2.addr)

E

E1 + E

Gen(lookup(top, id.text) = E.addr)

S^

id = E ;

Short-circuit Boolean Expressions •^

Some language semantics decree that boolean expressionshave so-called short-circuit semantics.^ –

In this case, computing boolean operations may also have flow-of-controlExample:

if ( x < 100 || x > 200 && x != y ) x = 0; Translation:

if x < 100 goto L2ifFalse x >200 goto L1ifFalse x != y goto L L2: x = 0L1: …

Flow-of-Control Statements •^

Suppose that we have a grammar:

S

if ( B ) S1 | if ( B ) S1 else S

| while ( B ) S

and that we have defined an attribute for code

B.CodeS1.Code

B.trueB.false

to B.trueto B.false

B.CodeS1.Code Goto S.next

S2.code

B.true B.FalseS.Next

to B.trueto B.false

Control-Flow Boolean Expressions

B.Code = gen(goto B.false)

B

false

B.Code = gen(goto B.true)

B

true

B.Code = E1.code || E2.code

|| gen( if E1.addr relop E2.addr goto B.true)|| gen( goto B.false)

B

E1 rel E

B1.True = B.false; B1.false = B.true;B.Code = B1.code

B

! B

B1.true = newlabel(); B1.false = B.falseB2.true = B.true; B2.false = B.falseB.Code = B1.code || label(B1.true) || B2.code

B

B1 && B

B1.true = B.true; B1.false = newlabel();B2.true = B.true; B2.false = B.false;B.Code = B1.code || label(B1.false) || B2.code

B

B1 || B

Avoiding Redundant Gotos, Backpatching •^

Use ifFalse instructions where necessary

•^

Also use attribute value “fall” to mean to fall through wherepossible, instead of generating goto to the next expression

•^

The abstract labels require a two-pass scheme to later fill inthe addresses

•^

This can be avoided by instead passing a list of addressesthat need to be filled in, and filling them as it becomespossible. This is called backpatching.