intermediate code generation o(n) o(n log n) o(n) very fast...

8
1 Spring 2010 CSCI 565 - Compiler Design Pedro Diniz [email protected] Intermediate Code Generation Basic Approach and Application to Assignment and Expressions Array Expressions Boolean Expressions Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use. Spring 2010 CSCI 565 - Compiler Design Pedro Diniz [email protected] Structure of a Compiler A compiler is a lot of fast stuff followed by some hard problems The hard stuff is mostly in Code Generation and Optimization For super-scalars, its Allocation & Scheduling that counts Intermediate Code Generation Code Generation Scanner Parser Analysis & Optimization O(n log n) O(n) O(n) O(n) words IR IR IR asm regs k regs Very Fast or Very Hard NP Complete Spring 2010 CSCI 565 - Compiler Design Pedro Diniz [email protected] Intermediate Code Generation Direct Translation Using SDT scheme Parse tree to Three-Address Instructions Can be done while parsing in a single pass Needs to be able to deal with Syntactic Errors and Recovery Indirect Translation First validate parsing constructing of AST Uses SDT scheme to build AST Traverse the AST and generate Three Address Instructions Intermediate Code Generation O(n) IR IR Three-Address Instructions regs Parse tree AST This Lecture Same but Easier Spring 2010 CSCI 565 - Compiler Design Pedro Diniz [email protected] Three-Address Instructions IR High-level Constructs mapped to Three-Address Instructions Register-based IR for Expression Evaluation Infinite Number of Virtual Registers Still Independent of Target Architecture Parameter Passing Discipline either on Stack or via Registers Addresses and Instructions Symbolic Names are addresses of the corresponding source-level variable. Various constants, such as numeric and offsets (known at compile time) Generic Instruction Format: Label: x = y op z or if exp goto L Statements can have Symbolic Labels Compiler inserts Temporary Variables (any variable with t prefix) Type and Conversions dealt in other Phases of the Code Generation Spring 2010 CSCI 565 - Compiler Design Pedro Diniz [email protected] Three-Address Instructions Assignments: x = y op z (binary operator) x = op y (unary) x = y (copy) x = y[i] and x[i] = y (array indexing assignments) x = phi y z (Static Single Assignment instruction) Memory Operations: x = &y; x = *y and *x = y; for assignments via pointer variables. Spring 2010 CSCI 565 - Compiler Design Pedro Diniz [email protected] Control Transfer and Function Calls: goto L (unconditional); if (a relop b) goto L (conditional) where relop is a relational operator consistent with the type of the variables a and b; y = call p, n for a function or procedure call instruction to the name or variable p p might be a variable holding a set of possible symbolic names (a function pointer) the value n specifies that before this call there were n putparam instructions to load the values of the arguments. • the param x instruction specifies a specific value in reverse order (i.e, the param instruction closest to the call is the first argument value. Later we will talk about parameter passing disciplines (Run-Time Env.) Three-Address Instructions

Upload: vannhan

Post on 19-May-2018

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

1

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Intermediate Code Generation

Basic Approach and Application to Assignment and Expressions

Array ExpressionsBoolean Expressions

Copyright 2010, Pedro C. Diniz, all rights reserved.Students enrolled in the Compilers class at the University of Southern Californiahave explicit permission to make copies of these materials for their personal use.

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Structure of a Compiler

A compiler is a lot of fast stuff followed by some hard problems– The hard stuff is mostly in Code Generation and Optimization– For super-scalars, its Allocation & Scheduling that counts

IntermediateCode Generation

CodeGeneration

Scanner ParserAnalysis

&Optimization

O(n log n)O(n)O(n)

O(n)

words IR

IR IRasm

∞regs

kregs

Very Fast or VeryHard NP Complete

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Intermediate Code Generation

• Direct Translation– Using SDT scheme– Parse tree to Three-Address Instructions– Can be done while parsing in a single pass– Needs to be able to deal with Syntactic Errors and Recovery

• Indirect Translation– First validate parsing constructing of AST– Uses SDT scheme to build AST– Traverse the AST and generate Three Address Instructions

IntermediateCode Generation

O(n)

IR IR

Three-AddressInstructions∞ regs

Parse tree

AST

This Lecture

Same but Easier

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Three-Address Instructions IR• High-level Constructs mapped to Three-Address Instructions

– Register-based IR for Expression Evaluation– Infinite Number of Virtual Registers– Still Independent of Target Architecture– Parameter Passing Discipline either on Stack or via Registers

• Addresses and Instructions– Symbolic Names are addresses of the corresponding source-level variable.– Various constants, such as numeric and offsets (known at compile time)

• Generic Instruction Format:Label: x = y op z or if exp goto L– Statements can have Symbolic Labels– Compiler inserts Temporary Variables (any variable with t prefix)– Type and Conversions dealt in other Phases of the Code Generation

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Three-Address Instructions• Assignments:

– x = y op z (binary operator)– x = op y (unary)– x = y (copy)– x = y[i] and x[i] = y (array indexing assignments)– x = phi y z (Static Single Assignment instruction)

• Memory Operations:– x = &y; x = *y and *x = y; for assignments via pointer variables.

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

• Control Transfer and Function Calls:– goto L (unconditional);– if (a relop b) goto L (conditional) where relop is a relational

operator consistent with the type of the variables a and b;– y = call p, n for a function or procedure call instruction to the name

or variable p• p might be a variable holding a set of possible symbolic names (a function

pointer)• the value n specifies that before this call there were n putparam instructions

to load the values of the arguments.• the param x instruction specifies a specific value in reverse order (i.e, theparam instruction closest to the call is the first argument value.

• Later we will talk about parameter passing disciplines (Run-Time Env.)

Three-Address Instructions

Page 2: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

2

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Function Call Example

t1 = a

t2 = b + 1

putparam t1

putparam t2

y = call p, 2

p: getparam z

getparam x

t3 = x + z

return t3

return

y = p(a, b+1)

int p(x,z){

return x+z;

}

Source Code Three Address Instructions

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Function Call Example

t1 = a

t2 = b + 1

putparam t1

putparam t2

y = call p, 2

p: getparam z

getparam x

t3 = x + z

return t3

return

y = p(a, b+1)

int p(x,z){

return x+z;

}

Source Code Three Address Instructions

argumentevaluation

passing argson the stack

getting valuesfrom the stack

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Loop Example

L: t1 = i + 1

i = t1

t2 = i * 8

t3 = a[t2]

if t3 < v goto L

do

i = i + 1;

while (a[i] < v);

Source Code Three Address Instructions

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Loop Example

L: t1 = i + 1

i = t1

t2 = i * 8

t3 = a[t2]

if t3 < v goto L

do

i = i + 1;

while (a[i] < v);

Source Code Three Address Instructions

Where did thiscome from ?

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

SDT to Three Address Code

• Attributes for the Non-Terminals, E and S– Location (in terms of temporary variable) of the value of an expression: E.place– The Code that Evaluates the Expressions or Statement: E.code– Markers for beginning and end of sections of the code S.begin, S.end

• Semantic Actions in Productions of the Grammar– Functions to create temporaries newtemp, and labels newlabel– Auxiliary functions to enter symbols and consult types corresponding to

declarations in a symbol table.– Generate the code we use the emit function gen which creates a list of

instructions to be emitted later and can generate symbolic labels correspondingto next instruction of a list.

– Use of append function on lists of instructions.– Synthesized and Inherited Attributes

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Assignment StatementsS → id = E { p = lookup(id.name);

if (p != NULL)

S.code = gen(p ‘=‘ E.place);

else

error;

S.code = nulllist;

}

E → E1 + E2 { E.place = newtemp();

E.code = append(E1.code,E2.code,gen(E.place ‘=‘ E1.place ‘+’ E2.place); }

E → E1 * E2 { E.place = newtemp();

E.code = append(E1.code,E2.code,gen(E.place ‘=‘ E1.place ‘*’ E2.place); }

E → - E1 { E.place = newtemp();

E.code = append(E1.code,gen(E.place ‘=‘ ‘-’ E1.place)); }

E → (E1) { E.place = E1.place; E.code = E1.code; }

E → id { p = lookup(id.name);

if (p != NULL)

E.place = p;

else

error;

E.code = nulllist;

}

Page 3: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

3

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

E E*

id

c

id

d

E

E E*

id

e

id

f

E

E+

E

-

Assignment: Example

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

E E*

id

c

id

d

E

E E*

id

e

id

f

E

E+

E

-

E → id { p = lookup(id.name);

if (p != NULL)

E.place = p;

else

error;

E.code = null list;

}

Production:

Assignment: Example

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

E E*

id

c

id

d

E

E E*

id

e

id

f

E

E+

E

-

E → id { p = lookup(id.name);

if (p != NULL)

E.place = p;

else

error;

E.code = null list;

}

Production:

place = loc(f)

code = null

place = loc(e)

code = null

Assignment: Example

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

E E*

id

c

id

d

E

E E*

id

e

id

f

E

E+

E

-

E → E1 * E2 {E.place = newtemp();

E.code = gen(E.place ‘=‘ E1.place ‘*’ E2.place);}

Production:

place = loc(t1)

code = {t1 = e * f;}

place = loc(f)

code = null

place = loc(e)

code = null

Assignment: Example

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

E E*

id

c

id

d

E

E E*

id

e

id

f

E

E+

E

-

place = loc(f)

code = null

place = loc(e)

code = null

place = loc(t1)

code = {t1 = e * f;}

place = loc(t2)

code = {t2 = c + d;}

place = loc(d)

code = null

place = loc(c)

code = null

E → E1 * E2 {E.place = newtemp();

E.code = gen(E.place ‘=‘ E1.place ‘*’ E2.place);}

Production:

Assignment: Example

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

E E*

id

c

id

d

E

E E*

id

e

id

f

E

E+

E

-

Production:S → id = E { p = lookup(id.name);

if (p != NULL)

E.code = append(E.code,

gen(p ‘=‘ E.place));

else

error;

}

place = loc(t3)

code = {t1 = e * f; t2 = c * d; t3 = t2 - t1; }

place = loc(t5)

code = {t1 = e * f; t2 = c * d; t3 = t2 - t1; t4 = a * b; t5 = t4 + t3; }

code = {t1 = e * f; t2 = c * d; t3 = t2 - t1; t4 = a * b; t5 = t4 + t3; x = t5; }

place = loc(x)

code = null

place = loc(f)

code = null

place = loc(e)

code = null

place = loc(t1)

code = {t1 = e + f;}

place = loc(t2)

code = {t2 = c + d;}

place = loc(t4)

code = {t4 = a * b;}

place = loc(b)

code = null

place = loc(a)

code = null

place = loc(d)

code = null

place = loc(c)

code = null

Assignment: Example

Page 4: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

4

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

EE

*

id

c

id

d

E

E E*

id

e

id

f

E

E + E

-

t1 = e * f;

t2 = c * d;

t3 = t2 - t1;

t4 = a * b;

t5 = t4 + t3;

x = t5;

Assignment: Example

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Reusing Temporary Variables• Temporary Variables

– Short lived– Used for Evaluation of Expressions– Clutter the Symbol Table

• Change the newtemp Function– Keep track of when a value created in a temporary is used– Use a counter to keep track of the number of active temporaries– When a temporary is used in an expression decrement counter– When a temporary is generated by newtemp increment counter– Initialize counter to zero (0)

• Alternatively, can be done as a post-processing pass…

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

• Only 2 Temporary Variables andhence only 2 registers are Needed

x = a * b + c * d - e * f;

S

id =

E E*

id

a

id

b

xE

EE

*

id

c

id

d

E

E E*

id

e

id

f

E

E + E

-

// c = 0

t1 = e * f; // c = 1

t2 = c * d; // c = 2

t1 = t2 - t1; // c = 1

t2 = a * b; // c = 2

t1 = t2 + t1; // c = 1

x = t1; // c = 0

Assignment: Example

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Code Generation for Array Accesses• Questions:

– What is the Base Type of A?– What are the Dimensions of A?

• Answers:– Use Symbol Table– Check Array Layout

t1 = y * 20t1 = t1 * z

t2 = baseA - 84t3 = 4 * t1t4 = t2[t3]

X = t4

S

L = E

id L

id

Elist

E

]

Elist

L

id

,

[ E

L

id

y

z

A

x = A[y,z];…

x

?

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

How does the Compiler Handle A[i,j] ?First, must agree on a storage schemeRow-major order (most languages)

Lay out as a sequence of consecutive rowsRightmost subscript varies fastestA[1,1], A[1,2], A[1,3], A[2,1], A[2,2], A[2,3]

Column-major order (Fortran)Lay out as a sequence of columnsLeftmost subscript varies fastestA[1,1], A[2,1], A[1,2], A[2,2], A[1,3], A[2,3]

Indirection vectors (Java)Vector of pointers to pointers to … to valuesTakes much more space, trades indirection for arithmeticNot amenable to analysis

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Laying Out ArraysThe Concept

Row-major order

Column-major order

Indirection vectors

1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4A

1,1 2,1 1,2 2,2 1,3 2,3 1,4 2,4A

1,1 1,2 1,3 1,4

2,1 2,2 2,3 2,4A

1,1 1,2 1,3 1,4

2,1 2,2 2,3 2,4A

These have distinct &different cache behavior

Page 5: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

5

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Computing an Array Address

A[ i ]

• baseA + ( i – low ) x sizeof(baseType(A))• In general: base(A) + ( i – low ) x sizeof(baseType(A))

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

A[ i ]

• baseA + ( i – low ) x sizeof(baseType(A))• In general: base(A) + ( i – low ) x sizeof(baseType(A))

Almost always a power of 2,known at compile-time anduse a shift for speed

int A[1:10] where low is 1; Make low 0for faster access (saves a – ) as in the Clanguage

Computing an Array Address

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

A[ i ]• baseA + ( i – low ) x sizeof(baseType(A))• In general: base(A) + ( i – low ) x sizeof(baseType(A))

What about A[i1,i2] ?

Row-major order, two dimensions baseA + (( i1 – low1 ) x (high2 – low2 + 1) + i2 – low2) x sizeof(baseType(A))

Column-major order, two dimensions baseA + (( i2 – low2 ) x (high1 – low1 + 1) + i1 – low1) x sizeof(baseType(A))

Indirection vectors, two dimensions*(A[i1])[i2] — where A[i1] is, itself, a 1-d array reference

This stuff looks expensive!Lots of implicit +, -, x ops

Computing an Array Address

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

where w = sizeof(baseType(A))

Optimizing Address Calculation for A[i,j]

In row-major orderbaseA + (i–low1)(high2–low2+1) x w + (j – low2) x w

Which can be factored intobaseA + i x (high2–low2+1) x w + j x w – (low1 x (high2–low2+1) x w) + (low2 x w)

If lowi, highi, and w are known, the last term is a constantDefine baseA0 as baseA – (low1 x (high2–low2+1)) x w + low2 x w

and len2 as (high2-low2+1)

Then, the address expression becomes baseA0 + (i x len2 + j ) x w

Compile-time constants

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

• A[i1,i2,…,ik]

Addressing generalizes to((…((i1n2+i2)n3+i3)…)nk+ik) x w + +baseA - ((…((low1n2+low2)n3+low3)…)mk+lowk) x w

where nj = highj - lowj + 1 and w = sizeof(baseType(A))

First term can be computed using the recurrence e1 = i1 em = em-1 x nm + im at the end multiply by w and add compile-time constant term

Address Calculation for A[i1,i2,…,ik]

Compile-time constants

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

SDT for Addressing Arrays Elements

• Three Attributes– place: just the name or base address of the array– offset: the index value into the array– ndim: the number of dimensions

• Use the Recurrence to Compute Offsetoffset1 = i1

offsetm = offsetm-1 x nm + im– At the end multiply by w = sizeof(baseType(A))– Add the compile-time constant term– Keep track of which dimension at each level– Use the auxiliary function nm = numElem(A,m) as the number of

elements along the mth dimension of A

Page 6: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

6

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

L → Elist ] {

L.place = newtemp();

L.offset = newtemp();

code1 = gen(L.place ‘=‘ constTerm(Elist.array));

code2 = gen(L.offset ‘=‘ Elist.place * sizeof(baseType(Elist.array)));

L.code = append(Elist.code,code1,code2);

}

Elist → Elist1 , E {

t = newtemp();

m = Elist1.ndim + 1;

code1 = gen(t = Elist1.place * numElem(Elist1.array,m));

code2 = gen(t = t + E.place);

Elist.array = Elist1.array;

Elist.place = t;

Elist.ndim = m;

Elist.code = append(Elist1.code,E.code,code1,code2);

}

Elist → id [ E {

Elist.array = id.place;

Elist.place = E.place;

Elist.ndim = 1;

EList.code = E.code;

}

SDT for Addressing Arrays Elements

S

L

x

= E

L

Elist

Elist E

LE

L

A [

,

]

y

z

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

E → L { if (L.offset = NULL) then

E.place = L.place;

else

E.place = newtemp;

E.code = gen(E.place = L.place[L.offset]);

}

S → L = E { if L.offset = NULL then

E.code = gen(L.place = E.place);

else

S.code = append(E.code,gen(L.place[L.offset] = E.place);

}

L → id { L.place = id.place;

L.offset = null;

}

SDT for Addressing Arrays Elements

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = A[y,z]; A is 10 x 20 array with low1 = low2 = 1

sizeof(baseType(A)) = sizeof(int) = 4 bytesS

L

x

= E

L

Elist

Elist E

LE

L

A [

,

]

y

z

SDT for Addressing Arrays Elements

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

x = A[y,z];

S

x

=

A [

,

]

y

z

Elist.place = y

Elist.ndim = 1

Elist.array = A

Elist.place = t1

Elist.ndim = 2

Elist.array = A

E.place = z

L.place = z

L.offset = null

L.place = y

L.offset = null

E.place = y

L.place = t2

L.offset = t3

E.place = t4L.place = xL.offset = null

t1 = y * 20 // numElem(A,2) = 20

t1 = t1 + z // E.place is z

t2 = c // c = base(A) - (20+1)*4

t3 = t1 * 4 // sizeof(int) = 4

t4 = t2[t3]

x = t4

SDT for Addressing Arrays Elements

A is 10 x 20 array with low1 = low2 = 1

sizeof(baseType(A)) = sizeof(int) = 4 bytes

See page 383-384 of the textbook for an alternative approach

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Array ReferencesWhat about arrays as actual parameters?

Whole arrays, as call-by-reference parameters• Need dimension information ⇒ build a dope vector• Store the values in the calling sequence• Pass the address of the dope vector in the parameter slot• Generate complete address polynomial at each reference

Some improvement is possible• Save leni and lowi rather than lowi and highi

• Pre-compute the fixed terms in prologue sequence

What about call-by-value?• Most call-by-value languages pass arrays by reference• This is a language design issue

baseA

low1

high1

low2

high2

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

What about A[12] as an actual parameter?

If corresponding parameter is a scalar, it’s easy• Pass the address or value, as needed• Must know about both formal & actual parameter• Language definition must force this interpretation

What is corresponding parameter is an array?• Must know about both formal & actual parameter• Meaning must be well-defined and understood• Cross-procedural checking of conformability

⇒ Again, we’re treading on language design issues

Array References

Page 7: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

7

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

What about Variable-Sized Arrays?

Local arrays dimensioned by actual parameters• Same set of problems as parameter arrays• Requires dope vectors (or equivalent)

– dope vector at fixed offset in activation record

⇒ Different access costs for textually similar references

This presents a lot of opportunity for a good optimizer• Common sub-expressions in the address polynomial• Contents of dope vector are fixed during each activation• Should be able to recover much of the lost ground

⇒ Handle them like parameter arrays

Array References

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Array Address Calculations in a Loop

DO J = 1, NA[I,J] = A[I,J] + B[I,J]

END DO

• Naïve: Perform the address calculation twice

DO J = 1, NR1 = baseA0 + (J x len1 + I ) x floatsizeR2 = baseB0 + (J x len1 + I ) x floatsizeMEM(R1) = MEM(R1) + MEM(R2)

END DO

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

DO J = 1, NA[I,J] = A[I,J] + B[I,J]

END DO

• Sophisticated: Move common calculations out of loop

R1 = I x floatsizec = len1 x floatsize ! Compile-time constantR2 = baseA0 + R1R3 = baseB0 + R1DO J = 1, N

a = J x cR4 = R2 + aR5 = R3 + aMEM(R4) = MEM(R4) + MEM(R5)

END DO

Array Address Calculations in a Loop

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

DO J = 1, NA[I,J] = A[I,J] + B[I,J]

END DO

• Very sophisticated: Convert multiply to add (Operator Strength Reduction)

R1 = I x floatsizec = len1 x floatsize ! Compile-time constantR2 = baseA0 + R1 ; R3 = baseB0 + R1DO J = 1, N

R2 = R2 + cR3 = R3 + cMEM(R2) = MEM(R2) + MEM(R3)

END DO

Array Address Calculations in a Loop

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

• Two Basic Code Generation Flavors– Use boolean and, or and not instructions (like arithmetic)– Control-flow (or positional code) defines true or false of predicate

• Arithmetic Evaluation– Simpler to generate code as just eagerly evaluate the expression– Associate ‘1’ or ‘0’ with outcome of predicates and combine with logic instr.– Use the same SDT scheme explained for arithmetic operations.

• Control Flow Evaluation (short circuit evaluation)– More efficient in many cases– Complications

• Need to Know Address to Jump To in Some Cases• Solution: Two Additional Attributes

– nextstat (Inherited) Indicates the next location to be generated– laststat (Synthesized) Indicates the last location filled– As code is generated the attributes are filled with the correct value

SDT Scheme for Boolean Expressions

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Arithmetic Scheme: Grammar and Actions

E → false ‖ E.place = newtemp()

E.code = {gen(E.place = 0)}

E.laststat = E.nextstat + 1

E → true ‖ E.place = newtemp()

E.code = {gen(E.place = 1)}

E.laststat = E.nextstat + 1

E → (E1) ‖ E.place = E1.place; E.code = E1.code; E1.nextstat = E.nextstat E.laststat = E1.laststat

E → not E1 ‖ E.place = newtemp() E.code = append(E1.code,gen(E.place = not E1.place)) E1.nextstat = E.nextstat E.laststat = E1.laststat + 1

Page 8: Intermediate Code Generation O(n) O(n log n) O(n) Very Fast …pedro/Teaching/CSCI565-Spring10/Lecture… ·  · 2010-02-17Assignment: Example CSCI 565 - Compiler Design Spring 2010

8

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

E → E1 or E2 ‖ E.place = newtemp()

E.code = append(E1.code,E2.code,gen(E.place = E1.place or E2.place)

E1.nextstat = E.nexstat

E2.nextstat = E1.laststat

E.laststat = E2.laststat + 1

E → E1 and E2 ‖ E.place = newtemp()

E.code = append(E1.code,E2.code,gen(E.place = E1.place and E2.place)

E1.nextstat = E.nexstat

E2.nextstat = E1.laststat

E.laststat = E2.laststat + 1

E → id1 relop id2 ‖ E.place = newtemp()

E.code = gen(if id1.place relop id2.place goto E.nextstat+3)

E.code = append(E.code,gen(E.place = 0))

E.code = append(E.code,gen(goto E.nextstat+2))

E.code = append(E.code,gen(E.place = 1))

E.laststat = E.nextstat + 4

Arithmetic Scheme: Grammar and Actions

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Boolean Expressions: Examplea < b or c < d and e < f

00: if a < b goto 03

01: t1 = 0

02: goto 04

03: t1 = 1

04: if c < d goto 07

05: t2 = 0

06: goto 08

07: t2 = 1

08: if e < f goto 11

09: t3 = 0

10: goto 12

11: t3 = 1

12: t4 = t2 and t3

13: t5 = t1 or t4

id relop id

E

E

E

id relop id

E

id relop id

E

a b

c d e f<<

<

or

and

Spring 2010CSCI 565 - Compiler Design

Pedro [email protected]

Summary

• Intermediate Code Generation– Using Syntax-Directed Translation Schemes– Expressions and Assignments– Array Expressions and Array References

– Boolean Expressions (Arithmetic Scheme)