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

Similar documents
Concepts Introduced in Chapter 7

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

System Software Assignment 1 Runtime Support for Procedures

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

Run-time Environments

Run-time Environments

G Programming Languages - Fall 2012

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

CS558 Programming Languages

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

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

Names, Scope, and Bindings

Short Notes of CS201

Programming Language Implementation

Names, Scope, and Bindings

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CS201 - Introduction to Programming Glossary By

CMSC 330: Organization of Programming Languages

G Programming Languages - Fall 2012

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

Types and Type Inference

Lecture 13: Garbage Collection

Chapter 5 Names, Binding, Type Checking and Scopes

CMSC 330: Organization of Programming Languages

Chapter 5. Names, Bindings, and Scopes

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Imperative Programming Languages (IPL)

6. Names, Scopes, and Bindings

Names, Scope, and Bindings

Functional Languages and Higher-Order Functions

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

Programming Languages

CS 345. Functions. Vitaly Shmatikov. slide 1

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

Principles of Programming Languages

Run-time Environments - 3

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

Names, Scopes, and Bindings II. Hwansoo Han

COP4020 Programming Assignment 1 - Spring 2011

CS558 Programming Languages

Implementing Subprograms

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

Names, Scope, and Bindings

Names, Bindings, Scopes

CS 314 Principles of Programming Languages

Topic 7: Activation Records

Lecture 16: Static Semantics Overview 1

Weeks 6&7: Procedures and Parameter Passing

Code Generation & Parameter Passing

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Introduction to Programming Using Java (98-388)

CSE 3302 Programming Languages Lecture 5: Control

Control Abstraction. Hwansoo Han

Introduction. A. Bellaachia Page: 1

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Names and Abstractions: What s in a Name?

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

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

G Programming Languages - Fall 2012

Garbage Collection. CS 351: Systems Programming Michael Saelee

CS558 Programming Languages

Chapter 9 Subprograms

Garbage Collection. Steven R. Bagley

Wednesday, October 15, 14. Functions

Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

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

Types and Type Inference

Scope, Functions, and Storage Management

Imperative Programming

Runtime Environments I. Basilio B. Fraguela

Principles of Computer Science

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

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

CSC 533: Organization of Programming Languages. Spring 2005

Principles of Programming Languages

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Memory Management and Run-Time Systems

Programming Languages, Summary CSC419; Odelia Schwartz

Run-time Environments - 2

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

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

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

The role of semantic analysis in a compiler

Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

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

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

10. Abstract Data Types

Lecture 13: Complex Types and Garbage Collection

Memory Management: The Details

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

CSE 303: Concepts and Tools for Software Development

Programming Languages

CMSC 330: Organization of Programming Languages

CSE 504: Compilers. Expressions. R. Sekar

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Implementing Subprograms

Transcription:

Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns to caller 2 Design Issues Syntax, type checking Parameter passing mechanism Static or dynamic allocation of locals Static or dynamic scope Environment of parameter functions Overloading Generics Separate compilation 3 CSC 4101: Programming Languages 1

Syntax FORTRAN SUBROUTINE ADDER (parameters) Ada procedure ADDER (parameters) C void ADDER (parameters) 4 More Syntax Positional or keyword parameters Default values Ada example: function F(X : FLOAT; Y : INTEGER := 1; Z : FLOAT) RETURN FLOAT; I := F(3.14, Z => 2.71); 5 Type Checking Are argument types checked against parameter types? If functions are passed as arguments, is the function type checked? Kernighan & Richie C: int foo(int (*f)()) {... int bar(int i, int j) {... foo(bar); 6 CSC 4101: Programming Languages 2

Fixed-point Version of Factorial In K&R C, can t be typed in ANSI C int f(int (*g)(), int n) { if (n == 0) return 1; else return n * g(g, n-1); int fac(int n) { return f(f, n); 7 Parameter-Passing Methods Call-by-Value (Copy-In, Eager Eval.) Call-by-Result (Copy-Out) Copy-In-Copy-Out Call-by-Reference Call-by-Name Call-by-Need (Lazy Evaluation) 8 Call-by-Value Allocate memory for parameter Evaluate argument Initialize parameter with value Call procedure Nothing happens on return Used in C, C++, Pascal, Lisp, ML, etc. 9 CSC 4101: Programming Languages 3

Call-by-Value Example int a = 1; void foo(int x) { // a and x have same value // changes to a or x don t // affect other variable // argument can be expression foo(a + a); foo(a); // no modifications to a 10 Call-by-Result Argument must be variable Allocate memory for parameter Don t initialize parameter Call procedure... Copy parameter into argument var. Return from procedure 11 Call-by-Result Example int a = 2; void foo(int x) { // x is not initialized // changes to a or x don t // affect other variable // argument must be variable foo(a); // a was modified 12 CSC 4101: Programming Languages 4

Call-by-Value-Result (Copy-In-Copy-Out) Combination of previous two Copy argument value on call Copy result on return Used by Ada for parameters of primitive type in in-out mode 13 Call-by-Value-Result Example int a = 3; void foo(int x) { // a and x have same value // changes to a or x don t // affect each other // argument must be variable foo(a); // a might be modified 14 Call-by-Reference Evaluate argument into temporary Parameter is alias for location of tmp Call procedure Nothing happens on return Used by FORTRAN before 1977, Pascal var parameter 15 CSC 4101: Programming Languages 5

Call-by-Reference Example int a = 4; void foo(int x) { // a and x reference same location // changes to a or x // affect each other // argument can be an expression foo(a + a); foo(a); // a might be modified 16 Call-by-Ref in FORTRAN IV Implementations SUBROUTINE FOO(I) I = 5 RETURN FOO(10) J = 10 J gets value 5! 17 Call-by-Name Don t evaluate argument Create closure to evaluate argument Call procedure Eval arg by calling parameter closure Nothing happens on return Used by ALGOL-60, Simula-67 Similar to macro expansion (e.g, TeX) 18 CSC 4101: Programming Languages 6

Call-by-Name Example int a = 5; void foo(int x) { // x is a function // to get value of argument, // evaluate x() when value is needed // argument can be an expression foo(a + a); foo(a); // no modifications to a 19 Call-by-Need (Lazy Evaluation) Similar to Call-by-Name Argument evaluated only once Result kept in temporary Behavior differs with side effects Used by Haskell, Miranda 20 Example Lazy Evaluation int diverge() { return diverge(); int if_then_else(int c, int t, int e) { return c? t : e; int i = if_then_else(1, 42, diverge()); 21 CSC 4101: Programming Languages 7

Static vs. Dynamic Locals Static efficient no time for (de)allocation required retain value across function calls Dynamic (on the stack) required for recursive functions on modern hardware cheap access let compiler do the optimizations 22 Implementation of Parameter Passing Allocate activation record on heap, in registers, or on the stack Copy values into activation record Copy pointers for Call-by-Reference Encapsulate lazy args. in closure Branch to function code Standard calling convention: stack 23 Environment of Function Parameters Function is passed down int x = 1; // static scoping int foo() { return x; int bar(int (*f) ()) { int x = 2; // dynamic scoping return f(); int i = bar(foo); 24 CSC 4101: Programming Languages 8

Static vs. Dynamic Scope Static scope non-local variables are looked up in statically enclosing scope Need static link for nested functions i gets value 1 Dynamic scope non-locals are looked up along dynamic call chain i gets value 2 25 Environment of Function Parameters Function is passed up (define (add n) (lambda (m) (+ n m))) (define add1 (add 1)) (define add5 (add 5)) (define i (add1 10)) (define j (add5 10)) 26 Implementation of Closures Functions are represented as pair pointer to code pointer to environment Activation records might be on heap Needs garbage collection Programmer doesn t know when/how to reclaim the activation record of add 27 CSC 4101: Programming Languages 9

Garbage Collection No explicit free or delete When memory runs out, GC traverses life data structures starting with global variables and stack marks reachable data scans heap to find unreachable data collects unreachable data in free list may compact heap 28 Heap Data Structure For manual allocation, copying GC Blocks of storage with free list Each storage block contains length Free blocks are linked into free list First Fit: allocate object in first block that s large enough, keep rest on free list Combine blocks when deleting objects 29 Heap Data Structure (cont d) For Copying GC Only needs top of heap pointer Other possibilities (less efficient) Linked list with Best Fit Binary tree 30 CSC 4101: Programming Languages 10

GC Algorithms Mark & Sweep style Traverse and mark all live data structures Sweep heap, unmarked data on free list For C/C++: conservative GC Copying GC Allocate new heap (one page at a time) Copy objects to new heap, compact Update pointers 31 GC Algorithms (cont d) Generational GC New objects become garbage quicker Frequently collect new generation Rarely collect old generation Modern GC, e.g., in Java VM Generational GC Mark & sweep for young generation Copying for old generation 32 Conservative GC for C/C++ Pointers and integers look alike Copying collection doesn t work Integers are treated as potential pointers May not find all garbage but is safe May not work with pointer arithmetic free() is no-op, malloc() runs GC Boehm-Demers-Weiser GC http://www.hboehm.info/gc/ 33 CSC 4101: Programming Languages 11

Overloading vs. Multimethods Multiple methods/functions with same name, different arg. types int foo(int); int foo(float); Overloading method selection at compile time Multimethods method selection at run time 34 Overloaded Operators Allow redefining operators Ada function *... C++ operator * allows redefinition of [], ->, (), etc. 35 Generic Functions Functions with type parameter template <class Type> Type max (Type n, Type m) { return n > m? n : m; Instantiated at compile time Better than using preprocessor 36 CSC 4101: Programming Languages 12

Separate Compilation Compiler needs type information of other compilation units C++ Use #include Split code into header and source file Java Search in current directory Compile other classes if needed 37 CSC 4101: Programming Languages 13