Chapter 9 :: Subroutines and Control Abstraction

Similar documents
Chap. 8 :: Subroutines and Control Abstraction

Programming Languages

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

Control Abstraction. Hwansoo Han

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

CS A331 Programming Language Concepts

Chapter 3:: Names, Scopes, and Bindings

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

LECTURE 19. Subroutines and Parameter Passing

G Programming Languages - Fall 2012

Topic 3-a. Calling Convention 2/29/2008 1

Principles of Programming Languages

CA Compiler Construction

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

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

Concepts Introduced in Chapter 7

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Chapter 10. Implementing Subprograms

Implementing Procedure Calls

Implementing Subroutines. Outline [1]

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

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

CS 314 Principles of Programming Languages. Lecture 13

Chapter 8 :: Composite Types

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

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

Procedure and Object- Oriented Abstraction

Lecture 7: Binding Time and Storage

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

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

Module 27 Switch-case statements and Run-time storage management

Scope, Functions, and Storage Management

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

Stacks and Function Calls

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Code Generation & Parameter Passing

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

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

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Run-time Environments

Run-time Environments

CS 314 Principles of Programming Languages

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

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

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

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

CIT Week13 Lecture

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

CSc 520 Principles of Programming Languages

Run-time Environment

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

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

System Software Assignment 1 Runtime Support for Procedures

Topic 7: Activation Records

CS 2210 Programming Project (Part IV)

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

MIPS Procedure Calls - Review

Memory Management and Run-Time Systems

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

CS558 Programming Languages

CSc 520 Principles of Programming Languages

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

COP4020 Fall 2006 Final Exam

Chapter 11 :: Functional Languages

Lecture 9: Procedures & Functions. CS 540 George Mason University

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

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

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

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

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; :

Compiling Code, Procedures and Stacks

SRC Assembly Language Programming - III

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

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Summer 2003 Lecture 14 07/02/03

Implementing Subprograms

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Programmiersprachen (Programming Languages)

Chapter 10 :: Data Abstraction and Object Orientation

Course Administration

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

SPIM Procedure Calls

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

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

Compilers and computer architecture: A realistic compiler to MIPS

ECE232: Hardware Organization and Design

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

The Procedure Abstraction

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Functions and Procedures

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

Computer Systems Lecture 9

Short Notes of CS201

Transcription:

Chapter 9 :: Subroutines and Control Abstraction Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Review Of Stack Layout Allocation strategies Static Code Globals Own variables Explicit constants (including strings, sets, other aggregates) Small scalars may be stored in the instructions themselves 2 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Review Of Stack Layout 3 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Review Of Stack Layout Allocation strategies (2) Stack parameters local variables temporaries bookkeeping information Heap dynamic allocation 4 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Review Of Stack Layout Contents of a stack frame bookkeeping return PC (dynamic link) saved registers line number saved display entries static link arguments and returns local variables temporaries 5 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Calling Sequences Maintenance of stack is responsibility of calling sequence and subroutine prolog and epilog discussed in Chapter 3 space is saved by putting as much in the prolog and epilog as possible time may be saved by putting stuff in the caller instead, where more information may be known e.g., there may be fewer registers IN USE at the point of call than are used SOMEWHERE in the callee 6 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Calling Sequences Common strategy is to divide registers into caller-saves and callee-saves sets caller uses the "callee-saves" registers first "caller-saves" registers if necessary Local variables and arguments are assigned fixed OFFSETS from the stack pointer or frame pointer at compile time some storage layouts use a separate arguments pointer the VAX architecture encouraged this 7 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Calling Sequences 8 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Calling Sequences (LLVM on ARM) Caller saves into the local variable and temporaries area any caller-saves registers whose values are still needed puts up to 4 small arguments into registers r0-r3 puts the rest of the arguments into the argument build area at the top of the current frame does b1 or b1x, which puts return address into register lr, jumps to target address, and (optionally) changes instruction set coding 9 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Calling Sequences (LLVM on ARM) In prolog, Callee pushes necessary registers onto stack initializes frame pointer by adding small constant to the sp placing result in r7 subtracts from sp to make space for local variables, temporaries, and arg build area at top of stack In epilog, Callee puts return value into r0-r3 or memory (as appropriate) subtracts small constant from r7, puts result in sp (effectively deallocates most of frame) pops saved registers from stack, pc takes place of lr from prologue (branches to caller as side effect) 10 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Calling Sequences (LLVM on ARM) After call, Caller moves return value to wherever it's needed restores caller-saves registers lazily over time, as their values are needed All arguments have space in the stack, whether passed in registers or not The subroutine just begins with some of the arguments already cached in registers, and 'stale' values in memory 11 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Calling Sequences (LLVM on ARM) This is a normal state of affairs; optimizing compilers keep things in registers whenever possible, flushing to memory only when they run out of registers, or when code may attempt to access the data through a pointer or from an inner scope 12 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Calling Sequences (LLVM on ARM) Many parts of the calling sequence, prologue, and/or epilogue can be omitted in common cases particularly LEAF routines (those that don't call other routines) leaving things out saves time simple leaf routines don't use the stack - don't even use memory and are exceptionally fast 13 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Parameter Passing Parameter passing mechanisms have four basic implementations value value/result (copying) reference (aliasing) closure/name Many languages (e.g. Ada, C++, Swift) provide value and reference directly 14 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Parameter Passing C/C++: functions parameters passed by value (C) parameters passed by reference can be simulated with pointers (C) void proc(int* x,int y){*x = *x+y } proc(&a,b); or directly passed by reference (C++) void proc(int& x, int y) {x = x + y } proc(a,b); 15 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Parameter Passing Ada goes for semantics: who can do what In: callee reads only Out: callee writes and can then read (formal not initialized); actual modified In out: callee reads and writes; actual modified Ada in/out is always implemented as value/result for scalars, and either value/result or reference for structured objects 16 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Parameter Passing In a language with a reference model of variables (ML, Ruby), pass by reference (sharing) is the obvious approach It's also the only option in Fortran If you pass a constant, the compiler creates a temporary location to hold it If you modify the temporary, who cares? Call-by name is an old Algol technique Think of it as call by textual substitution (procedure with all name parameters works like macro) - what you pass are hidden procedures called THUNKS 17 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Parameter Passing 18 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Exception Handling What is an exception? a hardware-detected run-time error or unusual condition detected by software Examples arithmetic overflow end-of-file on input wrong type for input data user-defined conditions, not necessarily errors 19 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Exception Handling What is an exception handler? code executed when exception occurs may need a different handler for each type of exception Why design in exception handling facilities? allow user to explicitly handle errors in a uniform manner allow user to handle errors without having to check these conditions explicitly in the program everywhere they might occur 20 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017

Coroutines Coroutines are execution contexts that exist concurrently, but that execute one at a time, and that transfer control to each other explicitly, by name Coroutines can be used to implement iterators (Section 6.5.3) threads (to be discussed in Chapter 12) Because they are concurrent (i.e., simultaneously started but not completed), coroutines cannot share a single stack 21 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017 Coroutines 22 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November 21, 2017