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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
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.
CPSC 388 – Compiler Design and Construction Parameter Passing.
1 Parameter Passing (Section 8.3) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
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.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
PSUCS322 HM 1 Languages and Compiler Design II Runtime System Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Run time vs. Compile time
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.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
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,
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Subprograms subroutines, procedures, functions, methods.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
1 CS 201 Computer Systems Programming Chapter 18 “Parameter Passing” Herbert G. Mayer, PSU CS Status 1/23/2913.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
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.
ISBN Chapter 9 Subprograms Sections
CSC 8505 Compiler Construction Runtime Environments.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
7. Runtime Environments Zhang Zhizheng
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
CSE 3302 Programming Languages
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Chapter 9 Chapter 9 Subprograms. The Structure of Run-Time Memory.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Chapter 9 Subprograms.
Principles of programming languages 4: Parameter passing, Scope rules
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
CSC 533: Programming Languages Spring 2015
Topic 3-a Calling Convention 1/10/2019.
CSE 3302 Programming Languages
Parameter transmission
PZ09B - Parameter transmission
Parameter transmission
CSC 533: Programming Languages Spring 2018
CSC 533: Programming Languages Spring 2019
Parameter transmission
Presentation transcript:

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 Spring 2010 rev.: 4/19/2010

PSUCS322 HM 2 Agenda Parameter Classification Call by Value Call by Reference Call by Copy-Restore Call by Name

PSUCS322 HM 3 Parameter Classification Basic question concerning parameter passing: For each parameter, what exactly is passed from the caller to callée — the parameter’s address? its content? or something else? Various parameter passing methods: call by value: passing r-values call by reference: passing l-values call by copy-restore: hybrid between by-value and by-reference; AKA Call by Value-Result call by name: passing via name substitution; is rarely used; first designed for Algol-60

PSUCS322 HM 4 Call by Value Each actual argument is evaluated before call. On entry, the resulting value is copied and bound to the formal parameter; which behaves just like a local variable Advantages: –Simple; easy to understand! Formal parameters can be used as local variables, Updating them doesn’t affect actuals in calling procedure: double hyp( double a, double b ) { a = a * a; b = b * b; return sqrt( a + b ); // use built-in sqrt() } //end hyp

PSUCS322 HM 5 Problem with Call by Value Can be inefficient if value is large: typedef struct { double a1, a2,..., a10; } vector; double dotp( vector v, vector w ) { return v.a1 * w.a1 + v.a2 * w.a v.a10 * w.a10; } //end dotp vector v1, v2; double d = dotp( v1, v2 ); // copy 20 doubles Cannot affect calling environment directly: void swap( int i, int j ) { int temp; temp = i; i = j; j = temp; } //end swap swap( a[p], a[q] ); // has no effect! Can return result only through (the single) return value

PSUCS322 HM 6 Call by Reference Implemented by passing address of actual parameter: On entry, the formal is bound to the address, providing a reference to actual parameter from within the subroutine If actual argument doesn’t have an l-value (e.g., “2 + 3”), then either: –Forbid in language, i.e. treat as an error; compiler catches this –evaluate it into a temporary location and pass its address; but what will be the modified value of actual after the call? Advantages: –No more large copying –Actual parameter can be updated — Now swap, etc., work fine!

PSUCS322 HM 7 Problems with Call by Reference Accesses are slower: the formal parameter is an address that must be dereferenced to get at the value Opportunity for aliasing problems, e.g., procedure MatrixMult( a, b, c: matrix )... sets c := a * b; MatrixMult( a, b, a ) // oops! Question: Can we combine the simplicity of call by value with the efficiency for large values of call by reference?

PSUCS322 HM 8 C and C++ Solution C uses call by value, except for arrays. Structs (records in other languages) pass actual contents, while arrays are passed by address (pointer). Arrays do not get copied when passed as parameters Separately, programmers can take the address of a variable explicitly, and pass that to obtain call by ref-like behavior. swap1( int *a, int *b ) { // pointer value params, NOT ref int t; t = *a; *a = *b; *b = t; } //end swap1 swap1( &a[p], &a[q] ); C++ inherits the above properties of C. Furthermore, it supports call by reference parameters through the use of reference variables: swap2( int &a, int &b ) { // genuine reference parameter int t; t = a; a = b; b = t; } //end swap2 swap2( a[p], a[q] ); Swap2( i, j );

PSUCS322 HM 9 Pascal and Ada Solution In Pascal, Ada, and similar languages, structures and arrays can be passed by value (copying the whole contents) or by reference (passing only the address): –in parameter: is for passing value of the actual into the subroutine –out parameter: is for passing value of the formal parameter back to the actual –in out parameter: is for passing value into as well as out from the subroutine The idea is to let the programmer decide, which parameter passing method to use for each parameter Later versions of Ada, fix the implementation choice: –Scalar types are passed by value, if an in parameter, or copy- restore, if an out or in-out parameter; AKA value-result parameter –Composite data types are passed by reference

PSUCS322 HM 10 Java Solution Java has only call by value mode. However, in Java, a variable representing an object is a reference to the object, instead of its content. Note that a variable always points to actual heap space When object is passed as a parameter, only its reference is copied This reference model of variables also provides some call by reference-like benefits — when an object is passed as an actual parameter, its content can be updated in the subroutine: class A { int i = 0;... } class B { void sub( A a ) { a.i = 5; } void test() { A x = new A(); x.i = 4; sub( x ); System.out.println( x.i ); // prints 4? Or 5? } //end test } //end B

PSUCS322 HM 11 Difference to Call by Reference? Consider: class A { int i = 0;... } class B { void sub( A a ) { a = new A(); // parameter a gets modified here a.i = 5; } //end sub void test() { // <- start here <- A x = new A(); x.i = 4; sub( x ); System.out.println( x.i ); // prints 4? Or 5? Or?? } //end test } //end B Updating formal parameter affects only the local copy

PSUCS322 HM 12 Call by Copy-Restore Each actual argument is evaluated to a value before call On entry, value is bound to formal parameter just like a local Updating formal parameters doesn’t affect actuals in calling procedure during execution Upon exit, the final contents of formals are copied into the actuals Thus, behaves like call by reference in most “normal” situations, but may give different results when concurrency or aliasing are involved: type t is record a, b: integer; end record; r : t; procedure foo( s : in out t ) is begin r.a := 2; s.a := s.a + 3; end foo; r.a := 1; foo( r ); print( r.a ); -- what’s the value of r.a?

PSUCS322 HM 13 Test: Reference or Copy-Restore? var a : integer := 12;... procedure trick( var x : integer ) begin –- trick x := 13; if x = 13 and a = 13 then writeln( “pass by reference.” ); elsif x = 13 and a = 12 then writeln( “pass by copy-restore.” ); else writeln( “mysterious new method.” ); end if; end trick;... trick( a );

PSUCS322 HM 14 Call by Name First introduced in Algol-60, first implemented via “thunk” by Ingerman Idea derived from substitution or macro-expansion. In C and some other languages, macros are used to define simple functions. The user interface of a macro resembles that of a function. #define max( a, b ) ((a)>(b) ? (a) : (b)) v = max( x, y ); When a macro is invoked, each formal parameter is literally replaced by corresponding actual parameter; string substitution: max( x+1, y-2 ) => ( (x+1) > (y-2) ? (x+1) : (y-2)) However, macros and functions are fundamentally different: –Macros are invoked at compile-time (or pre-compile-time), functions are called at runtime –Macros can not contain recursions

PSUCS322 HM 15 Call by Name, Cont’d But blind substitution is dangerous because of possible “variable capture” #define swap( x, y ) { int t; t = x; x = y; y = t; }... swap( a[ t ], a[ q ] ); the call expands to { int t; t = a[ t ]; a[ t ] = a[ q ]; a[ q ] = t; } Here “t is captured” by the declaration in the macro, and is undefined at its first use Call by name is actually “substitution with renaming where necessary” –Flexible, but potentially very confusing, and inefficient to implement; requires small “.dll” or piece of code to evaluate actual parameter –If language has no updatable variables (as in “pure” functional languages), call by name renders beautifully simple semantics for function calls