translation to intermediate code chapter 7 cheng-chia chen

58
Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

Post on 19-Dec-2015

240 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

Translation to Intermediate CodeChapter 7

Cheng-Chia Chen

Page 2: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

2

Intermediate Representation (IR)

• A kind of abstract machine language• Express target machine operations without machine

specific detail• independent of the details of the source language

• compiler front-end:– lexical analysis parsing semantic analysis

translation to IR

• back-end– optimization on IR code generation – (instruction selection ; register allocation)

Page 3: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

3

Compiler for several languages and machines

• Good IR – must be convenient for semantic analysis– must be convenient to translate into real machine code– has clear and simple meaning– ie., SIMPLE but GENERAL!

Java

ML

Pascal

C

C++

Sparc

MIPS

Pentinum

Itanium

Java

ML

Pascal

C

C++

IR

#Transformations:MxN M+N

Page 4: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

4

Intermediate Representation Tree

• package tree; // IR tree• abstract class Exp

– computation of some value (with side-Effect)

• CONST(int value) – integer constant value

• NAME(Label label) // prog loc– symbolic constant label ( corresponding to assembly language label,

i.e., a program loc ) reference.

• TEMP(temp.Temp temp) // data loc in register– a temporary (i.e., a loc for storing data); like registers in real machine

but the abstract machine has infinitely many temporaries.

• BINOP(int binop, Exp left, Exp right) – (left +/* right)

Page 5: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

5

tree.Exp (continued)

• MEM(Exp exp) // data loc in memory– the content of wordsize bytes of memory at address exp.– means “store” when at left child of MOVE and means “fetch”

otherwise.

• CALL(Exp func, List<Exp> args) // func is a NAME– procedure call func(args)– func is evaluated before args, which is evaluated left to right.– has side effect (caller-save, RV, argument registers etc

affected)

• ESEQ(Stm stm, Exp exp) //abbreviated as [stm : exp]– evaluate stm with side effect , then return exp.– has side effect

Page 6: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

6

abstract class tree.Stm • perform side effect and control flow.• MOVE(Exp dst, Exp src) // two forms

– MOVE(TEMP t, e) // t e– evaluate e and move it to temporary t

– MOVE(MEM(e1), e2) // M(e1) e2– evaluate e1 to get an address a, store the result of e2 into

wordsize– bytes of memory at address a.

• EXP(Exp exp) // ExpStm(Exp exp) or [ e :]– evaluate exp and discard the result – useful only if exp has side effect.

• LABEL(Label label) // declare the loc that label denotes– Define the constant label “label” to be the current machine code

address.– like a label definition in assembly.– can use NAME(label) as the target of jumps, calls,etc.– Why LABEL and NAME ? Avoid an object to be both an Exp and a Stm.

Page 7: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

7

abstract class tree.Stm• JUMP(Exp exp, List<Label> targets) // also for switch

– transfer control to address exp, where exp must be one value in targets.

– exp may be a label reference i.e., NAME(lb) or may be an address that can be calculated by other Exp.

– to jump to known Label lb, use JUMP(NAME lb, [ lb ] )

• CJUMP(int relop, Exp left, Exp right, Label iftrue, Label iffalse)– evaluate left, right; compare left and right using relop relation;– if result true goto iftrue else goto iffalse.– possible relop: EQ,NE for integer, signed integer: LT,GT,LE,GE.– unsigned: ULT,ULE,UGT,UGE.

• SEQ(Stm left, Stm right) // abbreviated as [left, right]– execute left and then execute right.

Page 8: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

8

Intermediate Representation Tree (cont’d)other classes• ExpList(Exp head, ExpList tail) //replaced by List<Exp>• StmList(Stm head, StmList tail) // replaced by List<Stm>Other constants:public class BINOP { final static int PLUS, MINUS, MUL, DIV, AND, OR, LSHIFT,

RSHIFT,ARSHIFT,XOR; … }pubilc class CJUMP { final static int EQ, NE, LT, GT, LE,GE, ULT, ULE, UGT,UGE;…}Notes: IR can specify only the body of each function; there is no

provision for procedure and function definitions.– procedure entry/exit will be added later as special glue that is different

for each target language.

Page 9: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

9

Translate into Tree

syntaxtree.Exp translate.Exp tree.Exp Ex(),Nx(),Cx() Exp(),Stm()

IdentifierExp() -> Ex.unEx() -> tree.MEM()

Assign() -> Nx.unNx() -> tree.MOVE()

Plus() -> Ex.unEx() -> tree.BINOP()

Plus() -> Ex.unCx() -> tree.CJUMP()

LessThan() -> Cx.unCx() -> tree.CJUMP()

LessThan() -> Cx.unEx() -> tree.ESEQ()

And() -> Cx.unCx() -> tree.CJUMP()

Page 10: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

10

The Idea• How might an expression be used ?

– Every expression will be used differently.– some return value, some return none, some will transfer control.

• Need to define methods for an Object-oriented interface to expressions.

package translate;

pubic abstract class Exp {

abstract tree.Exp unEx(); //Exp2Exp

abstract tree.Stm unNx(); // Exp2Non

abstract tree.Stm unCx(temp.Label t, temp.Label f);

//Exp2ConditionalJump

…}

Page 11: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

11

Some subclasses of translate.Exp

• Ex : Wrap a tree.Exp• Nx: Wrap a tree.Stm• Cx: Wrap a conditional or combination of Exps and

Stms.– e.g;: comparison operations < (e1, e2).– If you pass it two labels: say, t for ture and f for false, then it

will return you a tree.Stm that will jump to t if Exp is evaluted to true and jump to f if Exp is evaluted to false.

• e.g: a > b || c < d

Page 12: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

12

Example: a > b || c < dtree.Stm unCx(Label tt, Label ft) { Label zz = new Label();

return new SEQ ( new CJUMP(CJUMP.GT, a, b, tt, zz), new SEQ ( new LABEL(zz), new SEQ ( new CJUMP(CJUMP.GT, c, d, tt, ff )))

a>b

c<d

zz:

true

false

true

false

tt :ff:

CJUMP(>,a,b, tt, zz)

LABEL(zz:)

CJUMP(<,c,d, tt, ff)

Page 13: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

13

Conversion rules

Ex(Exp e)

Nx(Stm s )

CxRelCx(op,e1,e2)

tree.Exp

unEx()

e [ s : CONST(0) ]

// always return zero or never

happen

[ r1, c(op,e1,e2,t1,f1),

f1: r 0,

t1: : r ]

tree.Stm

unNx()

EXP(e)

(evaluate e, discard result)

s [ unCx(L,L), L: ]

// no branch, fall through

Stm

unCx(t,f)

CJUMP(NE, e, CONST(0), t,f)

e != 0 =>goto t

e==0 => goto f

never happen CJUMP(op, e1,e2,t,f)

Page 14: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

14

Ex: subclass of translate.Exp• an ordinary expression yielding a single value

class Ex extends Exp { tree.Exp exp; Ex(tree.Exp e) {exp=e;} tree.Exp unEx() {return exp;}

// if(exp!=0)goto t else goto f tree.Stm unCx(Label t, Label f) { if (exp instanceof CONST){ if (((CONST)exp).value!=0) return new JUMP(t); else return new JUMP(f);} else return new CJUMP(CJUMP.NE,exp, new CONST(0),t,f); } tree.Stm unNx() {return new tree.ExpStm(exp);}}

Page 15: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

15

• Expression that yields no value

Nx: subclass of Translate.Exp

class Nx extends Exp { tree.Stm stm; Nx(tree.Stm s) {stm=s;} tree.Exp unEx(){return new tree.ESEQ(stm, new CONST(0));} //-> never happen tree.Stm unCx(Label t, Label f) {

throw new Error("Nx.unCx"); //Never happen } tree.Stm unNx() {return stm;}}

Page 16: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

16

Cx: subclass of Translate.Exp

• A “conditional” expression that jumps to either t or f:

• eg: flag := (a<b && c<d)

new Cx(a<b && c<d) . unEx() = 1 if true, 0 if falseabstract class Cx extends Exp {

abstract tree.Stm unCx(Label t, Label f);

tree.Stm unNx() { Label tf = new Label(); return new Tree.SEQ( unCx(tf,tf), new Tree.LABEL(tf)); }

Page 17: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

17

tree.Exp unEx() {

Temp r = new Temp(); Label t1 = new Label();

Label f1 = new Label();

return new ESEQ(

new SEQ( MOVE(new TEMP(r), new CONST(1)), // r 1

new SEQ(unCx(t1,f1), // if(?) goto t1 else goto f1

new SEQ(new LABEL(f1), // f1:

new SEQ(new MOVE(new TEMP(r), new CONST(0)), //r 0

new LABEL(t1))))), // t1:

new TEMP(r)); // return r

}

}

Page 18: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

18

Translate Simple Variables

• A simple variable v declared in the current stack frame (i.e., formal parameter or local ) will be translated to :

MEM(BINOP(PLUS, TEMP fp, CONST k))

or abbreviated as MEM(+(TEMP fp, CONST k).

where – k is the offset of v within the frame – TEMP fp is the frame pointer register

Page 19: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

19

Frame class : all machine-dependent stuff

Package frame;public class Frame { … abstract public Temp FP(); abstract public int wordSize();}public abstract class Access{ public abstract tree.Exp exp(tree.Exp framePtr)}

• The exp() method of Frame.Access is used to turn a Frame.Access into a tree expression.

Page 20: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

20

Tree to access the local variable in Frame

The framePtr argument of exp(.) is the address of the stack frame that the Access lives in.

For miniJava:

1. If v is inFrame(k) then– v.exp(new TEMP(frame.FP())) yields

– MEM(+(TEMP fp, CONST k))

2. if v is InReg(Temp txxx) =>

– v.exp(…) yields TEMP(txxx).

• Note both inFrame(k) and InReg(t) are Frame.Access objects, Hence by calling their common exp() method, we can yield different results while translation without having to distinguish each other (simply call v.exp(.) ).

Page 21: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

21

Array Variables

• In MiniJava array variables behave like pointers (scalars )– new array values are created and initialized by the constructor new int[n], where n is the number of elements and 0 is the initial value of each element.

• MiniJava objects are also pointers (scalars)– object assignment is the pointer assignment.

• Cf: Array of C, Pascal (structure values)– int[20] a, b ; … – a = b // C address copy ; Pascal content copy

• Cf: Records of C, Pascal (structure values)– like above.

Page 22: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

22

l-value v.s. r-value• Not all Exps can appear at the left hand side of an assignment.

(or MOVE in IR)• examples:

– x+1 f(2) – 3; 5 x – 2 ; f(x) + 2 10 (x)– MOVE(5, e); MOVE(+(2, fp), e) (x)– f(x) x; (?)– x 5 ; a[j+2] 4; p.age 5 (0)– MOVE( MEM(…), e); MOVE(TEMP(t), e) (0)

• L-values are (results of) expressions that can appear at the left hand side of an assignment.– x, a[j+1], p.age

• R-values are expressions that are not L-values and hence can ONLY appear at the right hand side of an assignment.– 5, f(2) -3,

• Expression = L-Value U R-Value

Page 23: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

23

Structured L-values• Meaning of L-values is context-sensitive.

– x = x + 1 ;– left location ; right content

• l-value : left of assignment -> location– eg. x, p.y, a[i+2]

• r-value + l-value: right of assignment -> contents– eg. a+3, f(x) + all l-values

• All the variables and l-values in MiniJava are scalar– no case like A := B // copy B array into A array– if not, we need size of variables in Access – We need to modify MEM(Exp) to MEM(Exp, Size)– C struct, PASCAL array and record are structured l-value.

Page 24: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

24

Subscripting and field selection

• a[i] (i-low) x int_size + a // = size x i + (a – low x size)– a : array[low .. high] of Integer. // Pascal style

• a.f a+ offset(f)– a: record { f1: Type1, f2:Type2,…};

• if a is global constant addr,

a – low x int_size = W can be precomputed.

• if low = 0 and a = MEM(e) =>

a[i] = MEM(+(MEM(e), BONOP(MUL, i, CONST(int_size));

• Technically, l-value should be represented as an address.– I.e., as r-value ==> Content(addr). as l-value => ADDR(addr)

– In the book, l-value is represented as MEM[addr] with the knowing that

– MEM means both store(in lhs) and fetch(elsewhere).

Page 25: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

25

Array boundary check

• Compiler need to emit code to check array boundary.– but if it can guarantee the safety, it may omit the checking code.

• In MiniJava, we require to store the length of an array: – int len = … ;– int[ ] a = new int[ len ] ;

• We can use a[0] to store the length of a and store elements of a from a[1].– a.length a – a[0] a + int_size– a[k] a + (k+1) x int_size = (a + int_size) + k x int_size– and if k > MEM(a) array out of bound!

Page 26: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

26

Arithmetic• AST operator IR Tree operator

– Plus( e1, e2) BINOP(+, |[ e1 ]|, |[ e2]| ) – Times(e1, e2) ?

• no unary arithmetic operator– e.g. – 4 ==> 0 – 4.– ==> BINOP(MUNIS, CONST(0), CONST(4)).

– ~ n ==> 0 XOR n– ==> BINOP(XOR, CONST(0), |[ n ]| )– where |[ n ]| means the translation of n.

Page 27: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

27

Boolean operations on logical expressions• In miniJava

– true stored as 1– false stord as 0

AST to IR translations:• true new EX( CONST(1) )

– .unEx() = CONST(1); .unCx(tt, ff ) = Jump tt

• false new Ex ( CONST(0) )• op( b1, b2 ) BINOP( op, |[ b1 ]|, |[ b2 ]| ).

– op is AND, OR, XOR etc.

• not(b1) BINOP( XOR, CONST(111…11), |[ b1 ]| ).

• Bit operations : << , >>, >>>, &, |, ~– Analogous to boolean operations.

Page 28: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

28

Conditionals

• What is the result of a comparison like “ e1 < e2 “ ?– 1. store in a boolean temporary. // rlt = ( e1 < e2 ) ;– 2. Used to change control flow (will be a Cx expression ).– if( e1 < e2 ) then s1 else s2 ;– A statement that will JUMP to any true-or-false destination

• AST to IR :• Simple Cx expression from AST comparison operator

– CJUMP operator.

• x < 5 RelCX( <, |[ X ]|, CONST(5) ) = cx with– cx.unCx(tt,ff) returns CJUMP(LT, |[ X ]|, CONST(5), tt, ff)– cx.unEx() returns [ r 1, unCx(t,f), f: r 0, t: : r ]

Page 29: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

29

Relative Operator: RelCx()package translate;import temp.Label;

public class RelCx extends Cx { int op; tree.Exp left, right;

RelCx(int o, Tree.Exp l, Tree.Exp r) { op = o; left = l; right = r; } Tree.Stm unCx(Label t, Label f) { return new tree.CJUMP(op, left, right, t, f); }}

Page 30: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

30

• For relational operator, we have seen RelCx class, which has relop, left Exp and right Exp.

• &(and), |(or) and !(not) are translated into

If-Then-Else AST.– (A & B) A? B: false– (A | B ) A? : true:B– not A A? : false : true

• If e1 then e2 else e3, where– e1 is an Cx, e2 and e3 are Ex.

Page 31: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

31

IfThenElse(Exp e1, Exp e2, Exp e3).unEx()

1. Make two labels t and f to which conditional will branch. • tr(e1).unCx(t,f)

2. Allocate temp r, and after label t, move e2 to r. Jump to label join.

• SEQ(Label(t),Move(TEMP(r), tr(e2).unEx()), JUMP(join))

3. After label f, move e3 to r. Jump to label join. SEQ(Label(f), Move(TEMP(r), tr(e3).unEx(),JUMP(join))

4. join: TEMP(r)

5. If e2 and e3 are statements, replace unEx() by unNx().

Page 32: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

32

IfThenElse(Exp e1, Exp e2, Exp e3).unCx(tt, ff)

1. e1, e2 and e3 are (boolean) Exp

2. Make two labels t and f to which conditional will branch. • tr(e1).unCx(t,f)

3. Add a new Label t + … • [ t: ,tr(e2).unCx(tt, ff), f: tr(e3).unCx(ff, tt) ]

• Aa a result :

[ tr(e1).unCx(t,f) ,

t: , tr(e2).unCx(tt, ff),

f: , tr(e3).unCx(ff, tt) ]

Page 33: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

33

Ex : IFThenElse(e1, e2,e3).unCx(tt,ff)

• if ( x<5 ) then a > b else false

• SEQ( CJUMP(LT, x, CONST(5), t, f), • SEQ( LABEL(t), • CJUMP(GT, a, b, tt, ff)))

or• [ CJUMP(LT, x, 5, t, f ), • t: CJUMP(GT, a, b, tt, ff)• f: JUMP ff.• ]

note: tt and ft come from uncx(tt,ff) method call.

Page 34: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

34

If Then Else Expclass IfThenElseExp extends Exp { Exp cond, a, b; Label t = new Label(); Label f = new Label(); Label join = new Label(); IfThenElseExp(Exp cc, Exp aa, Exp bb) { cond=cc; a=aa; b=bb;}

Tree.Stm unCx(Label tt, Label ff) { return new Tree.SEQ(cond.unCx(t,f),

new Tree.SEQ(new Tree.LABEL(t), new Tree.SEQ(a.unCx(tt,ff), new Tree.SEQ(new Tree.LABEL(f), b.unCx(tt,ff))))); }

Page 35: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

35

Exp unEx() { Temp r = new Temp(); return new ESEQ(

new SEQ(cond.unCx(t,f), new SEQ(new Tree.LABEL(t), new SEQ(new MOVE(new TEMP(r), a.unEx()), new SEQ(new JUMP(join), new SEQ(new LABEL(f), new SEQ(new MOVE(new TEMP(r), b.unEx()), new LABEL(join))))))),

new TEMP(r)); } //[ cond? r a.unEx() : r b:unEx() ; return r]

Page 36: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

36

Stm unNx() { return new SEQ(cond.unCx(t,f),

new SEQ(new LABEL(t), new SEQ(a.unNx(), new SEQ(new JUMP(join), new SEQ(new LABEL(f), new SEQ(b.unNx(), new LABEL(join)))))));

}

}

// cond? a.unNx() : b.unEx()

Page 37: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

37

String

• For a string literal lit:• Translator needs

– make a new Label lab, and – return the tree tree.NAME(lab).

• should also put the assembly fragment– frame.string(lab,lit)

• onto a global list of data fragment to be handled by code emitter.

• All string operations are performed by runtime system function.– allocate space, return pointer.– compiler does’t need to know its representation

Page 38: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

38

Record and Array construction

• new a { f1 =e1,f2=e2,…,fn=en }– create and initialize n-element record.– records may outlive the procedure activation– => allocate space at heap area.– => no freeing action, delegating to GC.

• create n-word memory by calling external function and get a pointer TEMP(r) to the heap area. Then move n values e1,…en to location 0, 1w, 2w, …,(n-1)w relative to r.

• ESEQ (• TEMP(r) CALL(NAME(malloc), CONST(n*w))• M(TEMP(r) + 0W) e1;• …• M(TEMP(r) + (n-1)W) en;• TEMP(r) )

Page 39: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

39

Array Allocation : a = new int [e]

1. Determine how much space is needed for the array:-- = ((length of array +1) x (size of integer))-- keep the length of array with array itself.-- for bound checking( a[n] ? ); and for array length access (a.length ).

2. Call an external function to allocate on the heap.3. Generate code for saving the length of the array at offset 0.4. Generate the code for initializing each of the values in the

array to zero starting at the 1st element.ESEQ ( TEMP(r) CALL(NAME(Label(“malloc”), e*w+ w)) M(TEMP(r) + 0W) e; {for(int i = 1 to e) M(TEMP(r) + i*w) CONST(0); } TEMP(r) )

Page 40: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

40

Calling runtime-system function

• ex: call external fun named “InitArray” for array initialization with arguments a,b.

generate a general call such as static Label initArray = new Label(“initArray”);

new CALL( new NAME(initArray),

new List[] { a, b } );

• or target-machine specific details encapsulated into a fucntion provided by the frame:

public abstract class Frame{ …

abstract public tree.Exp externalCall(String func,List<tree.Exp> args);

}

aFrame.externalCall(“initArray”, args )

Page 41: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

41

Implementing externalCall

• depends on the relationship b/t MiniJava’s procedure call convention and that of external function.

• The simplest look like

tree.Exp externalCall(String s, tree.ExpList args) {

return new tree.CALL(

new tree.NAME(new temp.Label(s)), args));

}• may need to adjust static links, label underscores etc.• => new tree.CALL(

new tree.NAME(new temp.Label(“__”+s)), [slink | args]));

Page 42: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

42

while(cond, body).unNx()

• while (cond) do body• AST2IR translation:

JUMP(test)

top: <body> // tr(<body>).unNx()

test:

if <cond> then top else done // tr(<cond>).unCx(top,done)

done:• if break appear within the body(exp) , the translation is

JUMP(done).• if continue appear within the body(exp) , the translation is

JUMP(test).

Page 43: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

43

The translation

transStm(WhileStm w, Label done’ // break target ) {

Label done = new Label();

tree.Exp condition = transExp(w.cond, done’);

tree.Exp body = transStm(w.body, done);

return whileExp(cond, body, done);

}

transStm(BreakStm b, Label done) {

return new Nx( new JUMP( done ) );

}

Page 44: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

44

While Loop

public Exp whileExp(Exp cond, Exp body, Label done) {

Label test = new Label();

Label top = new Label();

return new Nx(new Tree.SEQ(new Tree.JUMP(test),

new Tree.SEQ(new Tree.LABEL(top),

new Tree.SEQ(body.unNx(),

new Tree.SEQ(new Tree.LABEL(test),

new Tree.SEQ(cond.unCx(top,done),

new Tree.LABEL(done)))))));

}

Page 45: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

45

Function Call

• Function call f(a1, …, an)

CALL(NAME lf, [e1,e2,…,en])

where lf : label for f– In general, add static link as an extra argument

CALL(NAME lf, [sl,e1,e2,…,en])

– In an OOP language, we need this (current object pointer) as an argument.

– For a private method p.m(a1, …, an):

CALL(NAME lc$m, [p,e1,e2,…,en])

– For dynamic methods, we need dipatch tables….

Page 46: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

46

Static Links

• When a variable x is declared at an outer level of static scope, the static link must be used.

MEM(+(CONST kn,

MEM(+(CONST kn-1, ….

MEM(+(CONST k1, TEMP FP))…))))

where k1, ….kn-1 are the static link offsets

kn is the offset of var x in local frame

• exp() method in Frame.Access need to calculate the chain of static links to dereference.– i.e., simply call env.get(“x”).getAccess().exp().

Page 47: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

47

Function call with Static Links

• function call f(a1,a2,…,an)– add static link as an extra parameter

– CALL(new NAME(labf), [sl, a1,a2,…,an]).

Page 48: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

48

Declarations

• For each variable declaration within function body (including parameters) – Allocate additional space in the current frame (of the

current level)

• For each function declaration– Keep a new “fragment” of Tree code for function’s body

Page 49: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

49

Variable Definition

• Translator should return an augmented type environment: – update symbol table

• Initializing variables:– translated into a Tree exp that must be put just before the

body of function => return a Translate.Exp containing assignment expressions.

• Translator will return “no-op” (eg. Ex(CONST(0)) if applied to function and type declarations.

Page 50: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

50

Function Definition

• function => prologue body epilogue• prologue

1. Pseudo-instruction to announce the beginning of the function. (Assembly dependent)

2. label definition for function name

3. instructions to adjust the stack pointer (allocate new frame)

4. instructions to save escaping arguments. including static link, moving non-escaping arguments into fresh temporary registers

5. save return address, callee-saved registers,…

Page 51: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

51

Epilogue of function

6. Instruction to move return value to proper place

7. Instructions to restore the saved registers

8. Instruction to restore the stack pointer(deallocate frame)

9. a return instruction (Jump to the return address)

10.pseudo-instruction to announce the end of function

note:

1. 3~5 : determined after stack frame size known to compiler.

I.e., known only after register allocation.

Page 52: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

52

2. For return value, Translate will generate the instruction

MOVE(TEMP(cframe.RV()), body)

3. RV location is specified by machine specific frame

package frame;

public abstract class Frame {

abstract public Temp RV();

}

4. Saving and restoring register arguments are part of “view shift”

-implemented as part of :

abstract public tree.Stm procedureEntryExit1( tree.Stm body)

// could simply t1,…tk a1,…,ak, where t1,…,tk are k new Temps.

Page 53: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

53

Fragments

• function definition : frame + body• need to produce a descriptor for function

– frame: machine specific information about low level variables and parameters

– body: The result returned from procEntryExit1().

package Translate;

public class Frag {

public Frag next;

}

Page 54: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

54

ProgFrag and DataFragpublic abstract class Frag {}

public class ProcFrag extends Frag {

public Tree.Stm body;

public Frame.Frame frame;

ProcFrag(Tree.Stm b, Frame.Frame fr) {

body=b; frame=fr;

}

}public class DataFrag extends Frag { public String data; DataFrag(String d) { data=d; } public String toString(){return data;}}

Page 55: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

55

Class Translatepublic class Translate extends DepthFirstVisitor { ………… private frame; private List<Frag> frags = new LinkedList<Frag>(); // linked list of accumulated fragments public void procEntryExit(Exp body) {

Frame.Frame myframe = frame; Tree.Exp bodyExp = body.unEx(); Tree.Stm bodyStm; if (bodyExp != null) bodyStm = MOVE(TEMP(myframe.RV()), bodyExp); else bodyStm = body.unNx(); ProcFrag frag = new ProcFrag(bodyStm, myframe); frags.add(frag);

} public List<Frag> getResults() { return frags;}

Page 56: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

56

Classes and Objects

• We can create multiple objects for a class.• Each object will have its own instance variables.• Two objects can have different instances of local variables.• Methods of an object can access the variables of that object.• We need “this” pointer in the method to access the local

variables : similar to accessing record fields -> need to lookup symbol table of the class.

Page 57: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

57

Creating new Objects

1. Generate code for allocating heap space for all the instance variables;

1. we need the number of variables declared in the class. (Object size?)

2. Iterate through the memory locations for those variables and initialize them:

1. (For MiniJava, all variables should be initialized to “0”. )

3. How about the nested object? - “extends”

Page 58: Translation to Intermediate Code Chapter 7 Cheng-Chia Chen

58

Example: Program 7.5

class Vehicle { int position; int gas; int move (int x) { position = position + x; return position; } int fill (int y) { gas = gas +y; return gas; }}

……Vehicle v = new Vehicle();v.move();

variable in the object

need to know its offset

variable in the stack

need initialization code

local v is a stack variable or a temp

translate to Vehical$move(v.exp())

position translated to MEM( +(f.This() , CONST( offset(‘position”) )). where f.This() return the Access of first argument.