CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

Similar documents
CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

UNIT V Sub u P b ro r g o r g a r m a s

Memory Management and Run-Time Systems

G Programming Languages - Fall 2012

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Programming Languages

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Principles of Programming Languages

Chap. 8 :: Subroutines and Control Abstraction

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

Chapter 9 :: Subroutines and Control Abstraction

CS 345. Functions. Vitaly Shmatikov. slide 1

Implementing Subprograms

Programming Languages: Lecture 12

Object-Oriented Languages. CSc 453. Compilers and Systems Software. 23 : OO Languages. Department of Computer Science University of Arizona

Chapter 10. Implementing Subprograms ISBN

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CSc 453. Semantic Analysis III. Basic Types... Basic Types. Structured Types: Arrays. Compilers and Systems Software. Christian Collberg

Control Abstraction. Hwansoo Han

Run-time Environments -Part 1

CSc 453. OO Languages. Object-Oriented Languages... Object-Oriented Languages. Compiling OO Languages. Compilers and Systems Software

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

CSC 2400: Computer Systems. Using the Stack for Function Calls

CSc 520 Principles of Programming Languages

1 Lexical Considerations

System Software Assignment 1 Runtime Support for Procedures

CS 314 Principles of Programming Languages. Lecture 13

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

Lexical Considerations

CSc 520 Principles of Programming Languages

Parameter passing. Parameter: A variable in a procedure that represents some other data from the procedure that invoked the given procedure.

Wednesday, October 15, 14. Functions

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Concepts Introduced in Chapter 7

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Introduction. Inline Expansion. CSc 553. Principles of Compilation. 29 : Optimization IV. Department of Computer Science University of Arizona

CS 314 Principles of Programming Languages

procedure definition (or declaration - some make a distinction, we generally won t), e.g., void foo(int x) {... }

Parameter Binding. Value: The formal parameter represents a local variable initialized to the value of the corresponding actual parameter.

CSC 8400: Computer Systems. Using the Stack for Function Calls

Informatica 3 Syntax and Semantics

CS558 Programming Languages

Compiler Construction

References and pointers

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

Lexical Considerations

Compiler Construction

Weeks 6&7: Procedures and Parameter Passing

MIPS Procedure Calls. Lecture 6 CS301

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Topic IV. Block-structured procedural languages Algol and Pascal. References:

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Chapter 8. Fundamental Characteristics of Subprograms. 1. A subprogram has a single entry point

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Course Administration

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

CSc 453 Compilers and Systems Software

Systems I. Machine-Level Programming V: Procedures

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Run-Time Environments

9. Subprograms. 9.2 Fundamentals of Subprograms

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

PROCEDURES, METHODS AND FUNCTIONS

ECE232: Hardware Organization and Design

Run-time Environments

CSc 520 Principles of Programming Languages

Run-time Environments

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

COP4020 Fall 2006 Final Exam

Goals of this Lecture

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Lecture08: Scope and Lexical Address

Special Topics: Programming Languages

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

CS 314 Principles of Programming Languages

CIT Week13 Lecture

CS 415 Midterm Exam Spring SOLUTION

Programming Languages: Lecture 11

22c:111 Programming Language Concepts. Fall Functions

Functions and Procedures

NOTE: Answer ANY FOUR of the following 6 sections:

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

Lab 4 Prelab: MIPS Function Calls

Informatica 3 Marcello Restelli

Lecture 5: Procedure Calls

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

MIPS Functions and the Runtime Stack

Transcription:

Parameter Passing Value Parameters CSc 520 Principles of Programming Languages 28 : Control Structures Parameters Christian Collberg collberg+520@gmail.com PROG M; PROC P(X:INT); X:=5 VAR S:INT; S:=6; P(S);. X=5 S=6 P(6) M Department of Computer Science University of Arizona Copyright c 2008 Christian Collberg Value parameters are (usually) copied by the caller into the callee s activation record. Changes to a formal won t affect the actual. [1] 520 [2] ameter Passing Reference Parameter Call-by-Value Parameters PROG M; PROC P(VAR X:INT); X:=5 VAR S:INT; S:=6; P(S);. X=5 S=5 P(6) M Reference parameters are passed by passing the address (location, l-value) fo the parameter. Changes to a formal affects the actual also. 1. The caller computes the arguments r-value. 2. The caller places the r-values in the callee s activation record. The caller s actuals are never affected by the call. Copies may have to be made of large structures. TYPE T = ARRAY 10000 OF CHAR; PROC P (a:integer; b:t); a:=10; b[5]:="4" VAR r : INTEGER; X : T; P(r, X)

Call-by-Reference Parameters Call-by-Name Parameters 1. The caller computes the arguments l-value. 2. Expression actuals (like a + b) are stored in a new location. 3. The caller places the l-values in the callee s activation record. (Un-)popularized by Algol 60. A name parameter is (re-)evaluated every time it is referenced, in the callers environment. Algorithm: The caller s actuals may be affected by the call. TYPE T = ARRAY 10000 OF CHAR; PROC P (VAR a:int; VAR b:t); a:=10; b[5]:="4" VAR r : INTEGER; X : T; P(5 + r, X) [5] 520 1. The caller passes a thunk, a function which computes the argument s l-value and/or r-value, to the callee. 2. The caller also passes a static link to its environment. 3. Every time the callee references the name parameter, the thunk is called to evaluate it. The static link is passed to the thunk. [6] Call-by-Name Parameters... Call-by-Name Parameters... Algorithm: 4. If the parameter is used as an l-value, the thunk should return an l-value, otherwise an r-value. 5. If the parameter is used as an l-value, but the actual parameter has no l-value (it s a constant), the thunk should produce an error. Consequences: Every time a callee references a name parameter, it may produce a different result. VAR i : INTEGER; VAR a : ARRAY 2 OF INTEGER; PROCEDURE P (NAME x:integer); i := i + 1; x := x + 1; ; i := 1; a[1] := 1; a[2] := 2; P(a[i]); WRITE a[1], a[2]; x := x + 1 becomes a[i] := a[i] + 1. Since i is incremented before x, we get a[2] := a[2] + 1. Print 1,3.

Call-by-Name Implementation Call-by-Name Jensen s Device PROCEDURE P (thunk : PROC()); i := i + 1; thunk() := thunk() + 1; ; PROCEDURE thunk1 () : ADDRESS; RETURN ADDR(a[i]) ; i := 1; a[1] := 1; a[2] := 2; P(thunk1); WRITE a[1], a[2]; [9] 520 PROC Sum (NAME Expr:REAL; NAME Idx:INTEGER; Max:INTEGER):INTEGER; VAR Result : REAL := 0; FOR k := 1 TO Max DO; Idx := k; Result := Result + Expr; FOR; RETURN Result; ; VAR i : INTEGER; WRITE Sum(i, i, 5); (* 5 i=1 i *) WRITE Sum(i*i, i, 10); (* 10 [10] i=1 i2 *) Large Value Parameters Large Value Parameters... Large value parameters have to be treated specially, so that a change to the formal won t affect the actual. Example: TYPE T = ARRAY [1..1000] OF CHAR; PROCEDURE P (x : T); x[5] := "f"; VAR L : T; P(L);. Algorithm 1: Callee Copy PROCEDURE P (VAR x : VAR xt : T; copy(xt,x,1000); xt[5]:="f"; VAR L : T; P(L); T); Algorithm 2: Caller Copy PROCEDURE P (VAR x : T); x[5] := "f"; VAR L : T; VAR LT : T; copy(lt, L, 1000); P(LT);

Parameter Passing Parameter Passing... In Pascal, parameters are passed either by value or by reference (if the formal is preceeded by the keyword var). In C, all parameters are passed by value. Pass by reference can be simulated by explicitly passing the address of a variable: swap(&x,&y). In FORTRAN, all parameters are passed by reference. A programmer can simulate pass-by-value by explicitly making a local copy of an argument. Unlike most languages, FORTRAN allows r-values to be passed by reference: swap(3+4,7*x). The compiler creates a temporary variable to hold the value. In Java, object references are transfered using pass-by-sharing. This means that the actual and formal will refer to the same object. The compiler simply passes the address of the object. In Java, primitive types are passed by value. [13] 520 [14] Parameter Passing... Parameter Passing... In Pascal and Modula-2 a programmer would use call-by-value to ensure that the callee cannot modify the actual argument. In Pascal and Modula-2 a programmer would use call-by-reference to ensure that the callee can modify the actual argument, or to make sure that a large parameter (which semantically should be passed by value) is not copied. (This is done for efficiency reasons). Modula-3 provides a READONLY parameter mode. A READONLY formal parameter cannot be changed by the callee. The formal 1. cannot be on the left-hand-side of an assignment statement, and 2. cannot be passed by reference to another routine. Small READONLY parameters are passed by value. Large READONLY parameters are passed by reference.

Parameter Passing in Ada Parameter Passing in Ada... Ada has three modes: 1. in-parameters pass information from the caller to the callee. The callee cannot write to them. 2. out-parameters pass information to the callee from the caller. The callee can read and write them. They start out being uninitialized. 3. in out-parameters pass information from the caller to the callee and back. For scalars and pointers, all modes should be implemented by copying values. Thus 1. in-parameters are passed-by-value. 2. out-parameters are passed-by-result (the formal is copied into the actual when the procedure returns). 3. in out-parameters are passed-by-value/result (On entry, the actual is copied into the formal. On return, the formal is copied back into the actual). [17] 520 [18] Parameter Passing in Ada... Parameter Passing in Ada... For constructed types (records, arrays) an implementation is allowed to pass either values or addresses. If an in out parameter is passed by address an assignment to the formal changes the actual immediately. If an in out parameter is passed by value an assignment to the formal will not affect the actual until the procedure returns (and the formal is copied back into the actual). Ada disallows programs that can tell which implementation a compiler uses. type t is record a, b : integer; end record; r : t; procedure foo (s : in out t) is begin r.a := r.a + 1; s.a := s.a + 1; end foo; r.a := 3; foo(r); if r.a = 4 then put("implementation uses pass-by-value") else put("implementation uses pass-by-address")

Exam Problem 415.330/96 (A) Exam Problem 415.330/96 (B) Show the status of the run-time stack when execution has reached point for the second time in the program on the next slide. Fill in the name of each procedure invocation in the correct activation record. Also fill in the values of local variables and actual parameters, and show where the static links and control links are pointing. Assume that all actual parameters are passed on the stack rather than in registers. PROGRAM M; PROC P ( X:INT); VAR W:INT; PROC Q ( VAR Z:INT); VAR N:INT; PROC R ( V:INT); VAR L:INT; L := W; P(23); R; N := W; Z := Z+1; R(Z); Q; W := X + 1; Q(W); P(15); M. Activ ation Record Stack grows up! This is the activation Name: M record for the main program. [21] 520 [22] Homework Readings and References Draw the stack when control reaches point for the third time. Include all actual parameters, local variables, return addresses, and static and dynamic links. Read Scott, pp. 417 431 PROGRAM M; PROCEDURE P(X:INTEGER); VAR A : INTEGER; PROCEDURE Q(Y : INTEGER); VAR B : INTEGER; B := Y + 1; A := B + 2; P(B); Q; A:= X + 1; Q(A); P(0); M.

Summary A parameter is often passed by the caller copying it (or its address, in case of VAR parameters) into the callees activation record. On the MIPS, the caller has an area in its own activation record in which it puts actual parameters before it jumps to the callee. For each procedure P the compiler figures out the maximum number of arguments P passes to any procedure it calls. The corresponding amount of memory has to be allocated in P s activation record. [25]