CPSC 388 – Compiler Design and Construction Parameter Passing.

Slides:



Advertisements
Similar presentations
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Advertisements

The University of Adelaide, School of Computer Science
Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Advanced Programming Parameter Passing Giuseppe Attardi Università di Pisa.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Procedures and Control Flow CS351 – Programming Paradigms.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Chapter EighteenModern Programming Languages1 Parameters.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Overview Parameter passing modes.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CS 2104 Prog. Lang. Concepts Subprograms
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
Chapter 7 Runtime Environments. Relationships between names and data objects As execution proceeds, the same name can denote different data objects Procedures,
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
CS 536 Parameter Passing 1. Roadmap Last Time – Storing variables Locals, non-locals, globals This Time – Propagating values from one function to another.
Pointers *, &, array similarities, functions, sizeof.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
CSC 8505 Compiler Construction Runtime Environments.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
CSE 3302 Programming Languages
Week 12 Methods for passing actual parameters to formal parameters.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Functions.
Principles of programming languages 4: Parameter passing, Scope rules
Pointers and References
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Subroutines – parameter passing
CSE 3302 Programming Languages
UNIT V Run Time Environments.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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

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.

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

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

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!

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

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

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

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

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.

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

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

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

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?

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.

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

Aliases Via Pointers class 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!

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)

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?

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!

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

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.

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]); }

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