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

Similar documents
G Programming Languages - Fall 2012

LECTURE 19. Subroutines and Parameter Passing

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

G Programming Languages - Fall 2012

Programming Languages

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

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

Run-time Environments - 2

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

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

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

CS558 Programming Languages

Principles of Programming Languages

Programming Languages Third Edition. Chapter 7 Basic Semantics

THEORY OF COMPILATION

Weeks 6&7: Procedures and Parameter Passing

Control Abstraction. Hwansoo Han

Chapter 9 :: Subroutines and Control Abstraction

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

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

Chap. 8 :: Subroutines and Control Abstraction

Programming Languages: Lecture 12

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

Run-Time Environments

The role of semantic analysis in a compiler

Wednesday, October 15, 14. Functions

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

5. Semantic Analysis!

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

Memory Management and Run-Time Systems

Scope, Functions, and Storage Management

System Software Assignment 1 Runtime Support for Procedures

Concepts Introduced in Chapter 7

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

Implementing Subprograms

Run-time Environment

The compilation process is driven by the syntactic structure of the program as discovered by the parser

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

Statement Basics. Assignment Statements

CS 314 Principles of Programming Languages

G Programming Languages - Fall 2012

Course Administration

Semantic Analysis and Type Checking

CIT Week13 Lecture

CS 314 Principles of Programming Languages. Lecture 13

Code Generation & Parameter Passing

Special Topics: Programming Languages

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

Programming Languages: Lecture 11

Names, Bindings, Scopes

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

References and pointers

Run Time Environments

Chapter 10. Implementing Subprograms

Programming Languages

Chapter 10. Implementing Subprograms ISBN

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

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Procedure and Object- Oriented Abstraction

CA Compiler Construction

Implementing Procedure Calls

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

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

6. Names, Scopes, and Bindings

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

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

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

ECE232: Hardware Organization and Design

CSC 533: Organization of Programming Languages. Spring 2005

CS153: Compilers Lecture 8: Compiling Calls

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

ECS 142 Project: Code generation hints

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Compiler Construction

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

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

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

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

Semantic Analysis. Lecture 9. February 7, 2018

Chapter 5. Names, Bindings, and Scopes

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

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

Chapter 9 Subprograms

Concepts of Programming Languages

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

Lecture 7: Binding Time and Storage

Run-Time Data Structures

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

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

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

CSc 520 Principles of Programming Languages

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

The Procedure Abstraction

CS 415 Midterm Exam Spring 2002

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

Transcription:

G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1

Review Last week Control Structures Selection Loops 2

Outline Subprograms Calling Sequences Parameter Passing Recursion Sources: PLP, 3.5, 6.6, 7.4, 8.1-8.3 Osinski. Lecture notes, Summer 2008. Barrett. Lecture notes, Fall 2009. 3

Subprograms The basic abstraction mechanism Functions correspond to the mathematical notion of computation: input output Procedures affect the environment, and are called for their side-effects Pure functional model possible but rare (HASKELL) Hybrid model most common: functions can have (limited) side effects 4

Activation Records Recall that subroutine calls rely heavily on use of the stack. Each time a subroutine is called, space on the stack is allocated for the objects needed by the subroutine. This space is called a stack frame or activation record. The stack pointer contains the address of either the last used location or the next unused location on the stack. The frame pointer points into the activation record of a subroutine so that any objects allocated on the stack can be referenced with a static offset from the frame pointer. 5

Activation Records Why not use an offset from the stack pointer to reference subroutine objects? The stack pointer changes, with nested scopes and function/procedure calls. Also, there may be objects that are allocated on the stack whose size is unknown at compile time. These objects get allocated above the frame pointer so that objects whose size is known at compile time can still be accessed quickly. 6

Activation Records Why not use an offset from the stack pointer to reference subroutine objects? There may be objects that are allocated on the stack whose size is unknown at compile time. These objects get allocated last so that objects whose size is known at compile time can still be accessed quickly via a known offset from the frame pointer. Example procedure foo (size : integer) is M : array (1..size, 1..size) of real;... begin... end 7

Typical Activation Record Stack pointer Frame pointer Variable-length Objects Temporaries Local Variables Saved Values of Registers etc. Return address 8

Managing Activation Records When a subroutine is called, a new activation record is created and populated with data. The management of this task involves both the caller and the callee. The calling sequence refers to code executed by the caller just before and just after a subroutine call. The prologue refers to activation record management code executed at the beginning of a subroutine. The epilogue refers to activation record management code executed at the end of a subroutine. Sometimes the term calling sequence is used to refer to the combined operations of the caller, prologue, and epilogue. 9

Calling Sequence Calling a subroutine In the caller Store any caller-saved registers Place arguments in registers and/or stack Compute static link and pass as extra argument (Save return address on stack) Jump to subroutine In the callee Allocate frame by changing stack pointer Save old frame pointer and update with new value Save any callee-saved registers Initialize objects 10

Calling Sequence Finishing a subroutine In the callee Move return values (if any) into registers and/or stack Restore callee-saved registers Restore frame and stack pointers Jump back to return address In the caller Save return values Restore caller-saved registers 11

Calling a Subroutine Calling Sequence (before) Stack pointer Frame pointer Caller Activation Record 12

Typical Calling Sequence Calling Sequence (before) 1. Save caller-save registers Stack pointer Frame pointer Caller-Saved Registers Caller Activation Record 13

Typical Calling Sequence Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack Stack pointer Frame pointer Arguments Caller-Saved Registers Caller Activation Record 14

Typical Calling Sequence Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue Stack pointer Return address Arguments Frame pointer Caller-Saved Registers Caller Activation Record 15

Typical Calling Sequence Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp Stack pointer Frame pointer Saved fp Return address Arguments Caller-Saved Registers Caller Activation Record 16

Typical Calling Sequence Stack pointer Frame pointer Callee-Saved Registers Saved fp Return address Arguments Caller-Saved Registers Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers 17

Typical Calling Sequence Stack pointer Frame pointer Variable-length Objects Temporaries Local Variables Callee-Saved Registers Saved fp Return address Arguments Caller-Saved Registers Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers 18

Typical Calling Sequence Stack pointer Frame pointer Callee-Saved Registers Saved fp Return address Arguments Caller-Saved Registers Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers Epilogue 1. Restore callee-save registers 19

Typical Calling Sequence Stack pointer Frame pointer Saved fp Return address Arguments Caller-Saved Registers Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers Epilogue 1. Restore callee-save registers 2. Restore frame pointer 20

Typical Calling Sequence Stack pointer Frame pointer Return address Arguments Caller-Saved Registers Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers Epilogue 1. Restore callee-save registers 2. Restore frame pointer 3. Jump to return address 21

Typical Calling Sequence Stack pointer Frame pointer Arguments Caller-Saved Registers Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers Epilogue 1. Restore callee-save registers 2. Restore frame pointer 3. Jump to return address Calling Sequence (after) 22

Typical Calling Sequence Stack pointer Frame pointer Caller-Saved Registers Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers Epilogue 1. Restore callee-save registers 2. Restore frame pointer 3. Jump to return address Calling Sequence (after) 1. Restore caller-save registers 23

Typical Calling Sequence Stack pointer Frame pointer Caller Activation Record Calling Sequence (before) 1. Save caller-save registers 2. Push arguments on stack 3. Jump to subroutine, saving return address on stack Prologue 1. Save old fp, set new fp 2. Save callee-save registers Epilogue 1. Restore callee-save registers 2. Restore frame pointer 3. Jump to return address Calling Sequence (after) 1. Restore caller-save registers 24

Calling Sequence Are there advantages to having the caller or callee perform various tasks? If possible, have the callee perform tasks: task code needs to occur only once, rather than at every call site. However, some tasks (e.g. parameter passing) must be performed by the caller. 25

Saving Registers One difficult question is whether the caller or callee should be in charge of saving registers. What would the caller have to do to ensure proper saving of registers? Save all registers currently being used by caller What would the callee have to do to ensure proper saving of registers? Save all registers that will be used by callee Which is better? Could be either one no clear answer. In practice, many processors (including MIPS and x86) compromise: half the registers are caller-save and half are callee-save. Register Windows offer an alternative: each routine has access only to a small window of a large number of registers; when a subroutine is called, the window moves, overlapping a bit to allow parameter passing. 26

Optimizations Leaf routines A leaf routine is one which does not call any subroutines. Leaf routines can avoid pushing the return address on the stack: it can just be left in a register. If a leaf routine is sufficiently simple (no local variables), it may not even need a stack frame at all. Inlining Another optimization is to inline a function: inserting the code for the function at every call site. What are advantages and disadvantages of inlining? Advantages: avoid overhead, enable more compiler optimizations Disadvantages: increases code size, can t always do it (i.e. recursive procedures) 27

Parameter Passing Definitions Formal parameters are the names that appear in the declaration of the subroutine. Actual parameters or arguments refer to the expressions passed to a subroutine at a particular call site. // formal parameters: a, b, c function f (int a, int b, int c)... // arguments: i, 2/i, g(i,j) f(i, 2/i, g(i,j)); 28

Parameter passing Modes What does a reference to a formal parameter in the execution of a subroutine mean in terms of the actual parameters? It depends on the parameter mode. by value: formal is bound to value of actual by reference: formal is bound to location of actual by copy-return: formal is bound to value of actual; upon return from routine, actual gets copy of formal by name: formal is bound to expression for actual; expression evaluated whenever needed; writes to parameter are allowed (and can affect other parameters!) by need: formal is bound to expression for actual; expression evaluated the first time its value is needed; cannot write to parameters What are the advantages of passing by need? 29

Parameter passing Modes What does a reference to a formal parameter in the execution of a subroutine mean in terms of the actual parameters? It depends on the parameter mode. by value: formal is bound to value of actual by reference: formal is bound to location of actual by copy-return: formal is bound to value of actual; upon return from routine, actual gets copy of formal by name: formal is bound to expression for actual; expression evaluated whenever needed; writes to parameter are allowed (and can affect other parameters!) by need: formal is bound to expression for actual; expression evaluated the first time its value is needed; cannot write to parameters How does reference differ from copy-return? 30

Reference vs Copy-Return Consider the following PASCAL program: var global: integer := 10; another: integer := 2; procedure confuse (var first, second: integer); begin first := first + global; second := first * global; end; begin confuse(global, another); /* first and global */ /* are aliased */ end different results if by reference or by copy-return such programs are considered erroneous in ADA passing by value with copy-return is less error-prone 31

Parameter Passing in C C: parameter passing is always by value: assignment to formal is assignment to local copy passing by reference can be simulated by using pointers void incr (int *x) { (*x)++; } incr(&counter); /* pointer to counter */ no need to distinguish between functions and procedures: void indicates side-effects only 32

Parameter Passing in C++ default is by-value (same semantics as C) explicit reference parameters also allowed: void incr (int& y) { y++; } // compiler knows declaration of incr, // builds reference incr(counter); semantic intent can be indicated by qualifier: // passed by reference, but call cannot // modify it void f (const double& val); 33

Parameter Passing in JAVA semantics of assignment to parameter differs for primitive types and for classes: primitive types have value semantics objects have reference semantics consequence: methods can modify objects for formals of primitive types: assignment allowed, only affects local copy for objects: final means that formal is read-only 34

Parameter Passing in ADA goal: separate semantic intent from implementation parameter modes: in : read-only in subprogram out : write in subprogram in out : read-write in subprogram independent of whether binding by value, by reference, or by copy-return in : bind by value or reference out : bind by reference or copy-return in out : bind by reference or by value/copy-return functions can only have in parameters 35

Passing Subroutines as Parameters C and C++ allow parameters which are pointers to subroutines: void (*pf) (double); // pf is a pointer to a function that takes // a double argument and returns void typedef void (*PROC)(int); // type abbreviation clarifies syntax void do_it (double d) {... } void use_it (PROC); PROC ptr = &do_it; use_it(ptr); use_it(&do_it); Are there any implementation challenges for this kind of subroutine call? 36

Passing Subroutines as Parameters Not really: can be implemented in the same way as a usual subroutine call: in particular the referencing environment can stay the same. What if a nested subroutine is passed as a parameter? A closure must be created and passed in place of the subroutine. A closure is a reference to a subroutine together with its referencing environment. When a subroutine is called through a closure, the referencing environment from when the closure was created is restored as part of the calling sequence. 37

Passing Subroutines as Parameters Remember your homework? # Compose several parsers in sequence def make_seq(*ps): def parse(s, i): for p in ps: i = p(s, i) if -1 == i: return -1 return i return parse The make_seq function takes zero or more parser functions as its only argument. It defines a nested parse function, which applies these functions on some string and at some starting index in order. It returns the closure of nested function and environment, because parse needs to access ps to work. (Using parser functions to compose other parser functions is a well-known technique, called parser combinators.) 38

Syntactic sugar Default values for in-parameters (ADA) function Incr (Base: Integer; Inc: Integer := 1) return Integer; Incr(A(J)) equivalent to Incr(A(J), 1) also available in C++ int f (int first, int second = 0, char *handle = 0); named associations (Ada): Incr(Inc => 17, Base => A(I)); 39

Variable number of parameters printf("this is %d a format %d string", x, y); within body of printf, need to locate as many actuals as placeholders in the format string solution: place parameters on stack in reverse order return address actual 1 (format string)... actual n-1 actual n 40

First-class functions: implementation implications Allowing functions as first-class values forces heap allocation of activation records. environment of function definition must be preserved until the point of call: activation record cannot be reclaimed if it creates functions functional languages require more complex run-time management higher-order functions: functions that take (other) functions as arguments and/or return functions powerful complex to implement efficiently imperative languages restrict their use (a function that takes/returns pointers to functions can be considered a higher-order function) 41

Recursion In order to understand recursion, you must first understand recursion. 42

Recursion In order to understand recursion, you must first understand recursion. Recursion is when a subroutine is called from within itself. Example int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } 43

Recursion What are some advantages and disadvantages of using recursion? Advantages: often conceptually easier, and easier to understand code Disadvantages: usually slower, can lead to stack overflow There is one case when recursion can be implemented without using a stack frame for every call: A tail recursive subroutine is one in which no additional computation ever follows a recursive call. For tail recursive subroutines, the compiler can reuse the current activation record at the time of the recursive call, eliminating the need to allocate a new one. 44