CPSC 388 – Compiler Design and Construction Parameter Passing.

25
CPSC 388 – Compiler Design and Construction Parameter Passing

Transcript of CPSC 388 – Compiler Design and Construction Parameter Passing.

Page 1: CPSC 388 – Compiler Design and Construction Parameter Passing.

CPSC 388 – Compiler Design and Construction

Parameter Passing

Page 2: CPSC 388 – Compiler Design and Construction Parameter Passing.

Terminology

int method(int I, int J){}

I and J are formal parameters

int a,b;

method(a,b);

a and b are actual parameters

Page 3: CPSC 388 – Compiler Design and Construction Parameter Passing.

Terminology r-value refers to the value of an expression.

If x has the value of 2 and y has the value of 3 thenexpression r-value

x 2 y 3 x+y 5 x==y false

l-value refers to the location or address of an expression.x if it is global x if it is local x+y

All expressions have an r-value. Only expressions that can appear on the left side of an assignment have an l-value.

Page 4: CPSC 388 – Compiler Design and Construction Parameter Passing.

Parameter Passing Modes

Pass by Value

Pass by Reference

Pass by Value-Restore

Pass by Name

Programmer

Compiler Writer

Page 5: CPSC 388 – Compiler Design and Construction Parameter Passing.

Pass by Value In java and C you only have pass by value In pascal it is pass by value unless the

formal declaration starts with “var”, in C++ it is by value unless the formal contains a “&”

//Pascal procedure header

Procedure f(x: integer; var y: integer)

// C++ function header

void f(int x; int & y);

Page 6: CPSC 388 – Compiler Design and Construction Parameter Passing.

Pass by Value (continued) The calling method copies the r-value of the actual into the

called method’s activation record.

Changes to a formal have no effect on the actual.

void f( int[] A ) { A[0] = 10; // change an element of parameter A A = null; // change A itself (but not the

actual)}

void g() { int[] B = new int [3]; B[0] = 5; f(B);

System.out.println(B[0]);}

You can change what is pointed to by formal!

Page 7: CPSC 388 – Compiler Design and Construction Parameter Passing.

What happens when the following code executes?

class Person { int age; String name;}

class Test { static void changePerson(Person P) { P.age = 10;

P = new Person();P.name = "Joe";

}

public static void main(String[] args) { Person P = new Person();

P.age = 2;P.name = "Ann";changePerson(P);System.out.println(P.age);System.out.println(P.name);

}}

Page 8: CPSC 388 – Compiler Design and Construction Parameter Passing.

Reference Parameters

calling method copies the l-value of the actual into the called method's activation record

What if the actual has no l-value? e.g. f(x+y) Compiler can give an error Compiler can create a temporary to hold

the value and pass the address of this temporary

Page 9: CPSC 388 – Compiler Design and Construction Parameter Passing.

Bad Compiler Design with Reference Parameters

void mistake(int x) { // x is a reference parameter x = x+1;

}

void main() { int a;

mistake(1); a = 1;

print(a);} Output was 2!

Created a temporary for the INT_LIT of 1.All uses of 1 used this temporary

Page 10: CPSC 388 – Compiler Design and Construction Parameter Passing.

Reference Parameters in C++class Person { public: String name; int age;};

void birthday(Person per) { per.age++;}

void main() { Person P; P.age = 0; birthday(P); print(P.age);}

P is a Person, not a pointer to a Person

The parameter is passed by value (no &)So a copy of P is created in birthday’s AR

Page 11: CPSC 388 – Compiler Design and Construction Parameter Passing.

Reasons for using reference parameters

When the job of the called method is to modify the parameter

When the called method will not modify the parameter, but the parameter is very large.

Page 12: CPSC 388 – Compiler Design and Construction Parameter Passing.

You Try Itstatic void swap(int x, int y) { int tmp = x; x = y; y = tmp;} Assume that A is an array of integers, and that j and

k are (different) array indexes. Draw the AR to illustrate what happens for the call:

swap(A[j], A[k]); With pass by value With pass by reference

Page 13: CPSC 388 – Compiler Design and Construction Parameter Passing.

Code Generator with Parameters Assembly code generator creates different

code for pass by value and pass by reference

In pass by value activation record holds value of variable. Access variable by offset from the frame pointer, FP.

In pass by reference activation record holds address of the value of the variable

Page 14: CPSC 388 – Compiler Design and Construction Parameter Passing.

Assembly Code Examplevoid f(int a) { a = a - 5;}

void main() { int x = 10; f(x);}

passed by value_____________ lw t0,(FP) #load a's r-value into t0sub t0,t0,5 #t0 = t0 – 5sw t0,(FP) #store result into f's AR

passed by reference________________ lw t0,(FP) #load a's l-value into t0lw t1,(t0) #load a's r-value into t1sub t1,t1,5 #t1 = t1 - 5sw t1,(t0) #store result into main's AR

Page 15: CPSC 388 – Compiler Design and Construction Parameter Passing.

Assembly Code Example Calls generate different assembly

code as wellint x,y;x=y=3;f(x,y);

What assembly code would be generated to populate the AR for function f if the first param is by reference and the second is by value?

Page 16: CPSC 388 – Compiler Design and Construction Parameter Passing.

Pass by Value-Restore Used in Fortran IV and Ada

Values are copied into method’s AR At end of method call values are

copied back to calling method

Pass by value-restore has same effect as pass by reference EXCEPT when there are aliases.

Page 17: CPSC 388 – Compiler Design and Construction Parameter Passing.

Aliasing Two expressions that have the same

l-value

Occurs by: Pointer manipulation Parameter is pass by reference and

global 2 parameters passed by reference using

same expression

Page 18: CPSC 388 – Compiler Design and Construction Parameter Passing.

Aliases Via Pointersclass Person {

int age;

String name;

}

Person p, q;

p = new Person();

q = p;

p.name and q.name are aliases (same l-value, refer to same location in memory) p and q are not aliases!

Page 19: CPSC 388 – Compiler Design and Construction Parameter Passing.

You Try It

Create aliases by passing a global variable as a parameter (pass by reference)

Create aliases by passing the same variable as different parameters (pass by reference)

Page 20: CPSC 388 – Compiler Design and Construction Parameter Passing.

Different Results with Aliasingint x = 1; // a global variable

void f(int & a){ a = 2; // a and x are aliases x = 0;}

main(){ f(x); cout << x;}What is the difference if the parameter is pass by referenceOr pass by value-result?

Page 21: CPSC 388 – Compiler Design and Construction Parameter Passing.

Different Results with Aliasingvoid f(int &a, &b) { a = 2; b = 4; } main() { int x; f(x, x); cout << x; }

What is the difference if the parameter is pass by referenceOr pass by value-result?

The order values are copied back matters!

Page 22: CPSC 388 – Compiler Design and Construction Parameter Passing.

You Try It Assume all parameters are pass by value-

result. Question 1: Give a high-level description

of what the code generator must do for a method call.

Question 2: Give the specific code that would be generated for the call shown below, assuming that variables x and y are stored at offsets -8 and -12 in the calling method's AR. int x, y;f(x, y);

Page 23: CPSC 388 – Compiler Design and Construction Parameter Passing.

Pass by Name Parameters

Used by Algol Think of it like this:

Every call statement is replaced by the body of the called method.

Each occurrence of a formal parameter in the called method is replaced with the corresponding argument -- the actual text of the argument, not its value.

Page 24: CPSC 388 – Compiler Design and Construction Parameter Passing.

Example Pass by Name Parametervoid Init(int x, int y){ for (int k = 0; k < 10; k++) { y = 0; x++; }}

main(){ int j; int A[10]; j = 0; Init(j, A[j]);}

Page 25: CPSC 388 – Compiler Design and Construction Parameter Passing.

Comparison of Methods of Parameter Passing

Call by Value

Call by Reference

Call by Value Result

Call by Name

Does not cause Aliasing

Efficient for large objects

Faster than dereferencing in call by ref.

Faster for params that are not used