Chap. 8 :: Subroutines and Control Abstraction

Similar documents
Programming Languages

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

Chapter 9 :: Subroutines and Control Abstraction

Control Abstraction. Hwansoo Han

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

CS A331 Programming Language Concepts

LECTURE 19. Subroutines and Parameter Passing

Chapter 3:: Names, Scopes, and Bindings

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

Principles of Programming Languages

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

CA Compiler Construction

G Programming Languages - Fall 2012

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

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

CS 314 Principles of Programming Languages. Lecture 13

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

Lecture 7: Binding Time and Storage

Implementing Procedure Calls

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

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

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

Concepts Introduced in Chapter 7

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

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

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

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

Memory Management and Run-Time Systems

Programming Languages

Chapter 10. Implementing Subprograms

SPIM Procedure Calls

System Software Assignment 1 Runtime Support for Procedures

CS 314 Principles of Programming Languages

CSc 520 Principles of Programming Languages

Code Generation & Parameter Passing

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

Functions and Procedures

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

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

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

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

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

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

MIPS Procedure Calls - Review

Language of the Machine Recursive functions

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

CSc 520 Principles of Programming Languages

Run-time Environment

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

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Chapter 8 :: Composite Types

CS 314 Principles of Programming Languages. Lecture 11

Implementing Subprograms

CIT Week13 Lecture

Programmiersprachen (Programming Languages)

ECE232: Hardware Organization and Design

Procedure and Object- Oriented Abstraction

Compilation 2014 Activation Records (Part 1)

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

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

6. Names, Scopes, and Bindings

Code Generation. Lecture 19

COP4020 Fall 2006 Final Exam

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

Implementing Subroutines. Outline [1]

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

CS153: Compilers Lecture 8: Compiling Calls

Statement Basics. Assignment Statements

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

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

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

Names and Abstractions: What s in a Name?

CS558 Programming Languages

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

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

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

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

Code Generation. Lecture 12

Run-Time Environments

Run-time Environments

Run-time Environments

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

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

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

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

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

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Lecture 7: Procedures and Program Execution Preview

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

Course Administration

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Imperative Programming

ECE260: Fundamentals of Computer Engineering

Short Notes of CS201

Chapter 10. Implementing Subprograms ISBN

Transcription:

Chap. 8 :: Subroutines and Control Abstraction Michael L. Scott Programming Language Theory 2015, kkman@sangji.ac.kr 1

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 Programming Language Theory 2015, kkman@sangji.ac.kr 2

Programming Language Theory 2015, kkman@sangji.ac.kr 3

Allocation strategies (2) Stack parameters local variables temporaries bookkeeping information Heap dynamic allocation Programming Language Theory 2015, kkman@sangji.ac.kr 4

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 Programming Language Theory 2015, kkman@sangji.ac.kr 5

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 Programming Language Theory 2015, kkman@sangji.ac.kr 6

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 Programming Language Theory 2015, kkman@sangji.ac.kr 7

Programming Language Theory 2015, kkman@sangji.ac.kr 8

Caller saves into the temporaries and locals area any callersaves registers whose values will be needed after the call puts up to 4 small arguments into registers $4-$7 (a0- a3) it depends on the types of the parameters and the order in which they appear in the argument list puts the rest of the arguments into the arg build area at the top of the stack frame does jal, which puts return address into register ra and branches note that jal, like all branches, has a delay slot Programming Language Theory 2015, kkman@sangji.ac.kr 9

In prolog, Callee subtracts framesize from sp saves callee-saves registers used anywhere inside callee copies sp to fp In epilog, Callee puts return value into registers (mem if large) copies fp into sp (see below for rationale) restores saved registers using sp as base adds to sp to deallocate frame does jra Programming Language Theory 2015, kkman@sangji.ac.kr 10

After call, Caller moves return value from register to wherever it's needed (if appropriate) 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 Programming Language Theory 2015, kkman@sangji.ac.kr 11

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 Programming Language Theory 2015, kkman@sangji.ac.kr 12

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 Programming Language Theory 2015, kkman@sangji.ac.kr 13

Parameter Passing Parameter passing mechanisms have three basic implementations value value/result (copying) reference (aliasing) closure/name Many languages (e.g., Pascal) provide value and reference directly Programming Language Theory 2015, kkman@sangji.ac.kr 14

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); Programming Language Theory 2015, kkman@sangji.ac.kr 15

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 Programming Language Theory 2015, kkman@sangji.ac.kr 16

In a language with a reference model of variables (Lisp, Clu), 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 Programming Language Theory 2015, kkman@sangji.ac.kr 17

Programming Language Theory 2015, kkman@sangji.ac.kr 18

Generic Subroutines and Modules Generic modules or classes are particularly valuable for creating containers: data abstractions that hold a collection of objects Generic subroutines (methods) are needed in generic modules (classes), and may also be useful in their own right Programming Language Theory 2015, kkman@sangji.ac.kr 19

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 Programming Language Theory 2015, kkman@sangji.ac.kr 20

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 Programming Language Theory 2015, kkman@sangji.ac.kr 21

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 Programming Language Theory 2015, kkman@sangji.ac.kr 22

Coroutines Programming Language Theory 2015, kkman@sangji.ac.kr 23