CSc 520 Principles of Programming Languages

Similar documents
CSc 520 Principles of Programming Languages

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

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

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

CSc 520 Principles of Programming Languages

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

Memory Management and Run-Time Systems

Principles of Programming Languages

G Programming Languages - Fall 2012

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

Chap. 8 :: Subroutines and Control Abstraction

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

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

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

Chapter 10. Implementing Subprograms ISBN

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

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

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

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

Control Abstraction. Hwansoo Han

Run-time Environments -Part 1

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

CS 314 Principles of Programming Languages. Lecture 13

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

CSc 520 Principles of Programming Languages

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

1 Lexical Considerations

System Software Assignment 1 Runtime Support for Procedures

Lexical Considerations

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

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

Wednesday, October 15, 14. Functions

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

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

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

Weeks 6&7: Procedures and Parameter Passing

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

CSc 520 Principles of Programming Languages

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

CSc 520 Principles of Programming Languages

CS 314 Principles of Programming Languages

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

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

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

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

CS558 Programming Languages

Informatica 3 Syntax and Semantics

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

Compiler Construction

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

References and pointers

Lexical Considerations

Compiler Construction

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

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

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

CSc 453 Compilers and Systems Software

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

CSc 520 Principles of Programming Languages

Course Administration

CSCI565 Compiler Design

CS 314 Principles of Programming Languages

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.

CS 415 Midterm Exam Spring SOLUTION

Systems I. Machine-Level Programming V: Procedures

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

IR Generation. May 13, Monday, May 13, 13

Run-Time Environments

9. Subprograms. 9.2 Fundamentals of Subprograms

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

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

CSE 582 Autumn 2002 Exam 11/26/02

Run-time Environments

Run-time Environments

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

COP4020 Fall 2006 Final Exam

Goals of this Lecture

CSc 520. Principles of Programming Languages 25: Types Introduction

Programmiersprachen (Programming Languages)

Lecture08: Scope and Lexical Address

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

CS 415 Midterm Exam Spring 2002

Special Topics: Programming Languages

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

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CIT Week13 Lecture

Transcription:

CSc 520 Principles of Programming Languages 31: Procedures Parameters Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

Parameter Passing Value Parameters PROG M; PROC P(X:INT); X:=5 END P; VAR S:INT; S:=6; P(S); END. X=5 S=6 } } P(6) M Value parameters are (usually) copied by the caller into the callee s activation record. Changes to a formal won t affect the actual. [2]

arameter Passing Reference Parameter PROG M; PROC P(VAR X:INT); X:=5 END P; VAR S:INT; S:=6; P(S); END. 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. [3]

Call-by-Value Parameters 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" END P; VAR r : INTEGER; X : T; P(r, X) END [4]

Call-by-Reference 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. 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" END P; VAR r : INTEGER; X : T; P(5 + r, X) END [5]

Call-by-Name Parameters (Un-)popularized by Algol 60. A name parameter is (re-)evaluated every time it is referenced, in the callers environment. Algorithm: 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... 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. [7]

Call-by-Name Parameters... VAR i : INTEGER; VAR a : ARRAY 2 OF INTEGER; PROCEDURE P (NAME x:integer); END; i := i + 1; x := x + 1; END 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. [8]

Call-by-Name Implementation PROCEDURE P (thunk : PROC()); i := i + 1; thunk() := thunk() + 1; END; PROCEDURE thunk1 () : ADDRESS; RETURN ADDR(a[i]) END; i := 1; a[1] := 1; a[2] := 2; P(thunk1); WRITE a[1], a[2]; END [9]

Call-by-Name Jensen s Device 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; ENDFOR; RETURN Result; END; VAR i : END 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 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"; END P; VAR L : T; P(L); END. [11]

Large Value Parameters... Algorithm 1: Callee Copy OCEDURE P (VAR x : T); R xt : T; GIN copy(xt,x,1000); xt[5]:="f"; D P; R L : T; GIN P(L); D Algorithm 2: Caller Copy PROCEDURE P (VAR x : T); x[5] := "f"; END P; VAR L : T; VAR LT : T; copy(lt, L, 1000); P(LT); END [12]

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. [13]

Parameter Passing... 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. [14]

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). [15]

Parameter Passing... 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. [16]

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. [17]

Parameter Passing in Ada... 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). [18]

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. [19]

Parameter Passing in Ada... type t is record a, b : r : t; integer; end record; procedure foo (s : begin r.a := r.a + 1; s.a := s.a + 1; end foo; in out t) is r.a := 3; foo(r); if r.a = 4 then put("implementation uses pass-by-value") else put("implementation uses pass-by-address") [20]

Exam Problem 415.330/96 (A) 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. [21]

[22] 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); END R; N := W; Z := Z+1; R(Z); END Q; W := X + 1; Q(W); END P; P(15); END M. Activ ation Record Stack grows up! Procedure Name: Procedure Name: Procedure Name: Procedure Name: Procedure Name: Local Vars: Actual Params: Control Link Local Vars: Actual Params: Control Link Local Vars: Actual Params: Control Link Local Vars: Actual Params: Control Link Local Vars: Actual Params: Control Link Static Link Static Link Static Link Static Link Static Link Procedure Name: M This is the activation record for the main program. Exam Problem 415.330/96 (B)

Homework Draw the stack when control reaches point for the third time. Include all actual parameters, local variables, return addresses, and static and dynamic links. PROGRAM M; PROCEDURE P(X:INTEGER); VAR A : INTEGER; PROCEDURE Q(Y : INTEGER); VAR B : INTEGER; B := Y + 1; A := B + 2; P(B); END Q; A:= X + 1; Q(A); END P; P(0); END M. [23]

Readings and References Read Scott, pp. 441 448, 450 464 Read the Dragon Book: Parameter Passing 424 427 [24]

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]