Вы находитесь на странице: 1из 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation
Compiler Design

CSE 504
1 2 3 4 5

Syntax-Directed Code Generation Machines Expressions Statements Short-Circuit Code


Last modied: Mon Jan 28 2013 at 17:19:57 EST Version: 1.5 22:21:13 2013/01/28 Compiled at 22:19 on 2013/04/08 Compiler Design Code Generation CSE 504 1 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation

Intermediate code generation: Abstract (machine independent) code. Code optimization: Transformations to the code to improve time/space performance. Final code generation: Emitting machine instructions.

Compiler Design

Code Generation

CSE 504

2 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Syntax Directed Translation

Interpretation: E E 1 + E 2 Type Checking: E E 1 + E 2

{ E .val := E 1 .val + E 2 .val ; } { if E 1 .type E 2 .type int E .type = int ; else E .type = oat ; }

Compiler Design

Code Generation

CSE 504

3 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation via Syntax Directed Translation

Code Generation: E E 1 + E 2

{ E .code = E 1 .code || E 2 .code || add }

Compiler Design

Code Generation

CSE 504

4 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Stack Machines

Simplied translation, but fewer opportunities for optimization. Machine Conguration:


Contents of stack; each element of the stack is a cell of some standard size (e.g. 32 bits). Registers: Program counter, Stack pointer, (more later)

Stack representation:
[ ] to represent empty stack v1 ::S to represent a stack whose top element is v1 and the remainder of the stack is S . S [i ] represents the value at the i -th element of stack S (counting from the base, not top, of the stack).

Compiler Design

Code Generation

CSE 504

5 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Stack Machine Instructions


load immed v : Push v on stack. S load immed v a :: S load v :: S load: Load value from given address to top of stack. v :: S where v = S [a]. store: Store a given value to a given address. v :: a :: S store v :: T where T is same as S except T [a] = v . add: Add top two elements. v2 :: v1 :: S v :: S
Compiler Design

add pop
Code Generation

v1 + v2 :: S S
CSE 504 6 / 28

pop: Remove top-most element.

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Extended Stack Machine


Add Heap space. newarray n: Create a new array of n elements on Heap. Push the base address of the array on top of stack. aload Stack before: i :: a :: S . a is the base address of array (on heap); i is the index in that array. Push that value of the i -th element of array at a to top of stack. astore Stack before: v :: i :: a :: S . v is the value to be stored. a is the base address of array (on heap); i is the index in that array. Place v in the i -th element of array at a. Leave v alone on top of stack.
Compiler Design Code Generation CSE 504 7 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Register Machines

Machines with a (possibly unbounded) number of registers. Lets call them t1 , t2 , . . . Separate Heap space for dynamically allocated objects. Instructions:
move t1 , t2 : Move value from register t2 to t1 . move immed t1 , i : move literal constant i to a register t1 . add t1 , t2 : Add values of t1 and t2 , store it back in t1 . load t1 , t2 : Value in t2 is a heap address; move the value at that address to t1 . store t1 , t2 : Value in t1 is a heap address; move the value of t2 to that address.

Compiler Design

Code Generation

CSE 504

8 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation and Attributes (Stack Machine)


E E1 + E2 { E .code = E 1 .code || E 2 .code || add } { E .code = load immed int.val } { E .code = load id.addr } { E .code = load immed id.addr || E 1 .code || store }

E E E

int id id = E 1

Compiler Design

Code Generation

CSE 504

9 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation and Attributes (Register Machine)


E E1 + E2 { E .t = generate new temporary (); E .code = E 1 .code || E 2 .code || add E .t , E 1 .t , E 2 .t } { E .addr = generate new temporary (); E .code = mov immed E .t , int.val } { E .t = id.addr ; E .code = [ ] } { E .t = E 1 .t ; E .code = E 1 .code || move id.addr , E .t }
Code Generation CSE 504 10 / 28

int

id

id = E

Compiler Design

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

l - and r -Values

i = i + 1; l -value: location where the value of the expression is stored. r -value: actual value of the expression Some expressions (e.g. i) have both l - and r -values. Other expressions (e.g. 5) have only an r -value. Some expressions values may be (at least temporarily) stored in locations, but those locations may not have a meaning in terms of the program, and we consider them also to have only r -values (e.g. i+1).

Compiler Design

Code Generation

CSE 504

11 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation for L-expressions


Let L be an expression which has an l -value. Well use the following attributes: L.t : Register holding Ls value L.at : Register holding Ls address. L.lcode : Code for evaluating Ls address. L.rcode : Code for evaluating Ls value (a.k.a. L.code ) Using these, we can generate code for assignment expression as follows:
E { L = E1 E .t = E 1 .t ; E .code = L.lcode || E 1 .code || move L.at , E 1 .t

}
Compiler Design Code Generation CSE 504 12 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Arrays
Consider expression grammar changed as follows:
L represents simple identiers as well as array expressions.

E E E E

E +E L=E int L

The index of an array expression can be any arbitrary expression (including an array expression itself) Example: x[y[i]] The base of an array expression is an identier or another array expression. Example: (x[i])[j] LHS of an assignment can be an array expression.

L id L L[E ]

Compiler Design

Code Generation

CSE 504

13 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Addresses and Allocation

Well consider the scenario where


variables are allocated on stack/registers, arrays (and other objects) are allocated on heap

This means there will be two kinds of addresses: stack addresses (or register names), and heap addresses.
move instruction assigns value to a stack location/register. store instruction assigns value to a heap cell.

So well use another attribute for L-expressions, L.mem, to keep track of the kind of memory address they have.

Compiler Design

Code Generation

CSE 504

14 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Generating code for arrays (Register Machine):


L id { L.t = id.addr ; L.lcode = L.rcode = [ ]; L.mem = reg ; } { L.at = generate new temporary (); L.lcode = L1 .rcode || E .code || mul L.at , E .t , 4 || add L.at , L.at , L1 .at ; L.t = generate new temporary (); L.rcode = L.lcode || load L.t , L.at ; L.mem = heap ; }

Part A

L.t : Register holding Ls value. L.at : Register holding Ls address. L.lcode : Code for evaluating Ls address. L.rcode : Code for evaluating Ls value.

L1 [ E ]

Compiler Design

Code Generation

CSE 504

15 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Generating code for arrays (Register Machine):

Part B

{ E .t = L.t ; E .code = L.rcode }

Example expression: (i + a[i]) + b[i][j] With i.addr = t1 , j.addr = t2 , a.addr = t3 , b.addr = t4 .

// is rcode (empty) // a[i]s rcode mul t5, t1, 4 add t5, t5, t3 load t6, t5 // i+a[i]s code add t7, t1, t6 // b[i][j]s rcode: // b[i]s rcode mul t8, t1, 4 add t8, t8, t4 load t9, t8 // use b[i] as base: mul t10, t2, 4 add t10, t10, t8 load t11, t10 // add b[i][j] to prev result add t12, t7, t11
CSE 504 16 / 28

Compiler Design

Code Generation

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Generating code for arrays (Register Machine):

Part C

L = E1 E .t = E 1 .t ; if L.mem == reg assigncode = move L.at , E 1 .t ; else assigncode = store L.at , E 1 .t E .code = L.lcode || E 1 .code || assigncode ;

Compiler Design

Code Generation

CSE 504

17 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation for Statements

Ss

S Ss 1

Ss S

E ;

{ Ss .code = S .code || Ss 1 .code ; } { Ss .code = [ ]} { S .code = E .code ; }

Compiler Design

Code Generation

CSE 504

18 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Conditional Statements

if E , S 1 , S 2

{ elselabel = get new label (); endlabel = get new label (); S .code = E .code || beq E .t , 0, elselabel || S 1 .code ; || jmp endlabel ||elselabel : || S 2 .code ; ||endlabel : }

Compiler Design

Code Generation

CSE 504

19 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Conditional Statements and Continuations

S .end : label to jump after S is executed completely.


S if E , S 1 , S 2 { S .begin = get new label (); S 1 .end = S 2 .end = S .end ; S .code = S .begin: || E .code || beq E .t , 0, S 2 .begin || S 1 .code || S 2 .code ; }

Compiler Design

Code Generation

CSE 504

20 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Continuations

Attributes of a statement that specify where control will ow to after the statement is executed. Analogous to the follow sets of grammar symbols. In deterministic languages, there is only one continuation for each statement. Can be generalized to include local variables whose values are needed to execute the following statements: Uniformly captures call, return and exceptions.

Compiler Design

Code Generation

CSE 504

21 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Sequence and Continuation


Most frequently, the continuation of a statement will simply be its succeeding statement. We will use a special label fallthrough to denote this. Ss S Ss 1 { Ss 1 .end = Ss .end ; S .end = fallthrough; Ss .code = . . . } S E ; { if S .end == fallthrough next = [ ] else next = jmp S .end S .code = . . . || next ; }
Compiler Design Code Generation CSE 504 22 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Code Generation for Boolean Expressions


E E 1 && E 2 { E .t = generate new temporary (); E .code = E 1 .code || E 2 .code || and E .t , E1 .t , E2 .t ; }

The above code evaluates E2 regardless of the value of E1 . Short circuit code: evaluate E2 only if needed. E E 1 && E 2 { E .t = generate new temporary (); skip = generate new label (); E .code = E 1 .code || move E .t , E1 .t || beq E .t , 0, skip || E 2 .code || move E .t , E2 .t || skip :; }
Code Generation CSE 504 23 / 28

Compiler Design

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Generating Shortcircuit Code

Use two continuations for each boolean expression: E .success : where control will go when expression in E evaluates to true . E .fail : where control will go when expression in E evaluates to false . Both continuations are inherited attributes.

Compiler Design

Code Generation

CSE 504

24 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Shortcircuit Code for Boolean Expressions

E 1 && E 2

! E1

true

E 1 .fail = E .fail ; E 2 .fail = E .fail ; E 1 .success = get new label (); E 2 .success = E .success ; E .code = E 1 .code || E 1 .success :|| E 2 .code } { E 1 .fail = E .success ; E 1 .success = E .fail ; E .code = E 1 .code } { E .code = jmp, E .success

Compiler Design

Code Generation

CSE 504

25 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Short-circuit code for Conditional Statements

if E , S 1 , S 2

{ S .begin = get new label (); S 1 .end = S 2 .end = S .end ; E .success = S 1 .begin; E .fail = S 2 .begin; S .code = S .begin: || E .code || S 1 .code || S 2 .code ; }

Compiler Design

Code Generation

CSE 504

26 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Continuations and Code Generation

Continuation of a statement is an inherited attribute. It is not an L-inherited attribute! Code of statement is a synthesized attribute, but is dependent on its continuation. Backpatching: Make two passes to generate code.
1 2

Generate code, leaving holes where continuation values are needed. Fill these holes on the next pass.

Compiler Design

Code Generation

CSE 504

27 / 28

Syntax-Directed Code Generation

Machines

Expressions

Statements

Short-Circuit Code

Whats left?

After intermediate code is generated, Optimize intermediate code using target machine-independent techniques. Examples:
constant propagation loop-invariant code motion dead-code elimination strength reduction

Generate nal machine code Perform target machine-specic optimizations.

Compiler Design

Code Generation

CSE 504

28 / 28

Вам также может понравиться