Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 388 – Compiler Design and Construction Parameter Passing.

Similar presentations

Presentation on theme: "CPSC 388 – Compiler Design and Construction Parameter Passing."— Presentation transcript:

1 CPSC 388 – Compiler Design and Construction Parameter Passing

2 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

3 Terminology  r-value refers to the value of an expression. If x has the value of 2 and y has the value of 3 then expressionr-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.

4 Parameter Passing Modes  Pass by Value  Pass by Reference  Pass by Value-Restore  Pass by Name Programmer Compiler Writer

5 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);

6 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!

7 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(); = "Joe"; } public static void main(String[] args) { Person P = new Person(); P.age = 2; = "Ann"; changePerson(P); System.out.println(P.age); System.out.println(; }

8 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

9 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

10 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

11 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.

12 You Try It static 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

13 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

14 Assembly Code Example void 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 t0 sub t0,t0,5 #t0 = t0 – 5 sw t0,(FP) #store result into f's AR passed by reference ________________ lw t0,(FP) #load a's l-value into t0 lw t1,(t0) #load a's r-value into t1 sub t1,t1,5 #t1 = t1 - 5 sw t1,(t0) #store result into main's AR

15 Assembly Code Example  Calls generate different assembly code as well int 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?

16 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.

17 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

18 Aliases Via Pointers class Person { int age; String name; } Person p, q; p = new Person(); q = p;  and are aliases (same l-value, refer to same location in memory)  p and q are not aliases!

19 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)

20 Different Results with Aliasing int 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 reference Or pass by value-result?

21 Different Results with Aliasing void 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 reference Or pass by value-result? The order values are copied back matters!

22 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);

23 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.

24 Example Pass by Name Parameter void 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]); }

25 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

Download ppt "CPSC 388 – Compiler Design and Construction Parameter Passing."

Similar presentations

Ads by Google