Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Similar documents
G Programming Languages - Fall 2012

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

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

CSc 520 Principles of Programming Languages

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

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

CS 345. Functions. Vitaly Shmatikov. slide 1

CSc 520 Principles of Programming Languages

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

Principles of Programming Languages

LECTURE 18. Control Flow

Lecture 7: Binding Time and Storage

Control Abstraction. Hwansoo Han

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Programming Languages

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Chap. 8 :: Subroutines and Control Abstraction

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

Chapter 9 :: Subroutines and Control Abstraction

G Programming Languages - Fall 2012

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

CS 415 Midterm Exam Spring SOLUTION

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

Compiler Construction

COP4020 Fall 2006 Final Exam

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

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

9. Subprograms. 9.2 Fundamentals of Subprograms

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

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

Defensive Programming

Compiler Construction

Chapter 9 Subprograms

CS 314 Principles of Programming Languages. Lecture 13

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

14. Exception Handling

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS 314 Principles of Programming Languages

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Weeks 6&7: Procedures and Parameter Passing

Implementing Subprograms

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

6. Names, Scopes, and Bindings

Informatica 3 Syntax and Semantics

Programming Languages: Lecture 12

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Memory Management and Run-Time Systems

The role of semantic analysis in a compiler

Chapter 10. Implementing Subprograms ISBN

More Examples. Lecture 24: More Scala. Higher-Order Functions. Control Structures

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CS 314 Principles of Programming Languages

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

LECTURE 14. Names, Scopes, and Bindings: Scopes

Compiler Construction

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

Lecture 15: Object-Oriented Programming

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Scope. Chapter Ten Modern Programming Languages 1

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

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

Run-Time Environments

ECE 122. Engineering Problem Solving with Java

Programming Languages: Lecture 11

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

22c:111 Programming Language Concepts. Fall Functions

Compiler Construction

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

The Procedure Abstraction Part I: Basics

5. Semantic Analysis!

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Compiler Construction

NOTE: Answer ANY FOUR of the following 6 sections:

Subprograms. Akim D le Étienne Renault Roland Levillain EPITA École Pour l Informatique et les Techniques Avancées

low and not larger than high. 18 points

CS558 Programming Languages

CSE 3302 Notes 7: Control Abstraction

Run-time Environments -Part 1

G Programming Languages - Fall 2012

PROCEDURES, METHODS AND FUNCTIONS

Programming Languages

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

System Software Assignment 1 Runtime Support for Procedures

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Lecture08: Scope and Lexical Address

Advanced Exception Handling Mechanisms

Data Structures. Subodh Kumar. Dept of Computer Sc. & Engg. IIT Delhi

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

Today's Topics. Last Time Modelling Scopes and Visibility The Run Stack, the Dynamic Pointer Stack, the Display (LL,ON) addressing

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Special Topics: Programming Languages

COP4020 Programming Languages. Exception Handling Prof. Robert van Engelen

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Transcription:

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions COMP 524 Programming Language Concepts Stephen Olivier February 12, 2008 Based on notes by A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts

Parameter Passing Pass-by-value: Input parameter Pass-by-result: Output parameter Pass-by-value-result: Input/output parameter Pass-by-reference: Input/output parameter, no copy Pass-by-name: Effectively textual substitution 2

Pass-by-value int m=8, i=5; foo(m); print m; # print 8 proc foo(int b){ b = b+5; } 3

Pass-by-reference int m=8; foo(m); print m; # print 13 proc foo(int b){ b = b+5; } 4

Pass-by-value-result int m=8; foo(m); print m; # print 13 proc foo(int b){ b = b+5; } 5

Pass-by-name Arguments passed by name are re-evaluated in the caller s referencing environment every time they are used. They are implemented using a hidden-subroutine, known as a thunk. This is a costly parameter passing mechanism. Think of it as an in-line substitution (subroutine code put in-line at point of call, with parameters substituted). Or, actual parameters substituted textually in the subroutine body for the formulas. 6

Pass-by-name array A[1..100] of int; int i=5; foo(a[i], i); print A[i]; #print A[6]=7 #GOOD example proc foo(name B, name k){ k=6; B=7; } array A[1..100] of int; int i=5; foo(a[i]); print A[i]; #print A[5]=?? #BAD Example proc foo(name B){ int i=2; B=7; } #text sub does this proc foo{ i=6; A[i]=7; } } #text sub does this proc foo{ int i=2; A[i]=7; 7

Evaluate Pass-by-name In pass-by-name: real proc sum(expr, i, low, high); value low, high; real expr; integer i,low,high; begin real rtn; rtn := 0; for i:= low step 1 until high do rtn := rtn + expr; sum:=rtn end sum; 8

Ada in is call-by-value out is call-by-result in out is call-by-value/result Pass-by-value is expensive for complex types, so it can be implemented by passing either values or references However, programs can have different semantics with two solutions This is erroneous in Ada. 9

Ada Example 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); put(r.a); --does this print 4 or 5? 10

Summary Implementatio n mechanism Permissible Operations Changes to actual? Alias? value Value read, write no no in, const Val or ref read only no maybe out (Ada) Val or ref write only yes maybe value/result Val read, write yes no var, ref Ref Read, write yes yes sharing val or ref Read, write yes yes in out (Ada) val or ref Read, write yes maybe Name (Algo 60) Closure (thunk) Read, write yes yes 11

Other Parameter Passing Features Variable length parameter lists flexible in C using... C++, C#, Java require that all are the same type Named parameters Eliminates requirement for programmer to match the order of formal parameters to the order of actual parameters Default parameters Default value provided in the definition of the subroutine 12

Return Values Allow return values of complex types? Some restrict to primitive types and pointers Allow subroutine closures to be returned? A closure bundles a reference to a subroutine and a referencing environment Available in some imperative languages C instead allows function pointers to be returned 13

Return Values How to specify the return value? Fortran, Algol, Pascal: same name as the function name function add_five ( value1 : integer ) : integer; begin add_five := value1 + 5; end; C, C++, Java: return statement Other languages: name for function result provided in header procedure a () returns retvar : int retval := 5; end 14

Exception Handling An exception is an unexpected or unusual condition that arises during program execution. Raised by the program or detected by the language implementation Example: read a value after EOF reached Alternatives: Invent the value (e.g., -1) Always return the value and a status code (must be checked each time) Pass a closure (if available) to handle errors 15

Exception Handling Exceptions move error-checking out of the normal flow of the program No special values to be returned No error checking after each call 16

Exception Handlers Pioneered in PL/1 Syntax: ON condition statement The nested statement is not executed when the ON statement is encountered, but when the condition occurs e.g., overflow condition The binding of handlers depends on the flow of control. After the statement is executed, the program terminates if the condition is considered irrecoverable continues at the statement that followed the one in which the exception occurred. Dynamic binding of handlers and automatic resumption can potentially make programs confusing and error-prone. 17

Exception Handlers Modern languages make exception handler lexically bound, so they replace the portion of the code yet-tobe-completed In addition, exceptions that are not handled in the current block are propagated back up the dynamic chain. The dynamic chain is the sequence of dynamic links. Each activation record maintains a pointer to its caller, a.k.a., the dynamic link This is a restricted form of dynamic binding. 18

Exception Handlers Java uses lexically scoped exception handlers try{ int a[] = new int[2]; a[4]; } catch (ArrayIndexOutOfBoundsException e){ System.out.println( exception: + e.getmessage()); e.printstacktrace(); } 19

Exception Handlers Use of Exceptions Recover from an unexpected condition and continue e.g., request additional space to the OS after out-of-memory exception Graceful termination after an unrecoverable exception Printing some helpful error message e.g., dynamic Link and line number where the exception was raised in Java Local handling and propagation of exception Some exception have to be resolved at multiple level in the dynamic chain. e.g., Exceptions can be reraised in Java using the throw statement. 20

Returning Exceptions Propagation of exceptions effectively makes them return values. Consequently, programming languages include them in subroutine declarations Modula-3 requires all exceptions that are not caught internally to be declared in the subroutine header. C++ makes the list of exception optional Java divides them up into checked and unchecked exceptions 21

Hierarchy of Exceptions In PL/1, exceptions do not have a type. In Ada, all exceptions are of type exception Exception handler can handle one specific exception or all of them Since exceptions are classes in Java, exception handlers can capture an entire class of exceptions (parent classes and all its derived classes) Hierarchy of exceptions. 22

Implementation Linked-list of dynamically-bound handlers maintained at run-time Each subroutine has a default handler that takes care of the epilogue before propagating an exception This is slow, since the list must be updated for each block of code. Compile-time table of blocks and handlers Two fields: starting address of the block and address of the corresponding handler Exception handling using a binary search indexed by the program counter Logarithmic cost of the number handlers. 23

Java Each subroutine has a separate exception handling table. Thanks to independent compilation of code fragments. Each stack frame contains a pointer to the appropriate table. 24

C Exception can be simulated setjmp() can store a representation of the current program state in a buffer Returns 0 if normal return, 1 if return from long jump longjmp() can restore this state if(!setjmp(buffer)){ /* protected code */ } else { /* handler */ } 25

C The state is usually the set of registers longjmp() restores this set of registers Is this good enough? Changes to variables before the long jump are committed, but changes to registers are ignored If the handler needs to see changes to a variable that may be modified in the protected code, the programmer must include the volatile keyword in the variable s declaration. 26

Generics, Polymorphism Polymorphism is the property of code working for arguments/data of different types. Sort (list) works for list of int, list of string Many functional languages allow this but at cost... dynamic type checking Generics, templates allow static type checking but some measure of polymorphism. 27

Generics, Polymorphism generic compare (x,y: type T) returns bool{ return x<y; }... Creal = new compare(t=real); Cint = new compare(t=int); Cstr = new compare(t=string);.. generic inc(a: type T) returns T{ return a+1; } Cint = new compare(t=int); Cstr = new compare(t=string); #NO... Compiler reject 28