Control Abstraction. Hwansoo Han

Similar documents
Chap. 8 :: Subroutines and Control Abstraction

Programming Languages

Chapter 9 :: Subroutines and Control Abstraction

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

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

LECTURE 19. Subroutines and Parameter Passing

CS A331 Programming Language Concepts

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

Principles of Programming Languages

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

G Programming Languages - Fall 2012

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

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

Chapter 3:: Names, Scopes, and Bindings

CA Compiler Construction

Implementing Procedure Calls

Lecture 7: Binding Time and Storage

Run-time Environments

Run-time Environments

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

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

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

Concepts Introduced in Chapter 7

The Procedure Abstraction

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

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

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

Run-time Environment

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

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

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

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

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

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

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Hardware OS & OS- Application interface

Chapter 10. Implementing Subprograms

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

Implementing Subroutines. Outline [1]

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

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

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Procedure and Object- Oriented Abstraction

ECE232: Hardware Organization and Design

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

Operating System. Operating System Overview. Structure of a Computer System. Structure of a Computer System. Structure of a Computer System

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

COP4020 Fall 2006 Final Exam

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

Code Generation & Parameter Passing

CIT Week13 Lecture

Functions and Procedures

Programming Languages

Memory Management and Run-Time Systems

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

G Programming Languages - Fall 2012

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

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

LECTURE 18. Control Flow

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

SPIM Procedure Calls

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

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

MIPS Procedure Calls - Review

EE 109 Unit 15 Subroutines and Stacks

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Scope, Functions, and Storage Management

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

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

CS 314 Principles of Programming Languages. Lecture 13

Language of the Machine Recursive functions

Naming in OOLs and Storage Layout Comp 412

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

COMPILER DESIGN - RUN-TIME ENVIRONMENT

CS153: Compilers Lecture 8: Compiling Calls

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

Compilers and computer architecture: A realistic compiler to MIPS

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

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

Anne Bracy CS 3410 Computer Science Cornell University

System Software Assignment 1 Runtime Support for Procedures

Lec 22: Interrupts. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Procedure Call. Procedure Call CS 217. Involves following actions

Types II. Hwansoo Han

Course Administration

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

SRC Assembly Language Programming - III

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

ECS 142 Project: Code generation hints

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

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 520 Principles of Programming Languages

Transcription:

Control Abstraction Hwansoo Han

Review of Static Allocation Static allocation strategies Code Global variables Own variables (live within an encapsulation - static in C) Explicit constants (including strings, sets, other aggregates) Small scalars may be stored in the instructions themselves 2

Review Of Stack Layout 3

Review of Allocation Strategies Stack allocation Parameters Local variables Temporaries Bookkeeping Heap allocation Dynamic allocation 4

Stack Frame Contents of a stack frame Bookkeeping Return PC (dynamic link) Saved registers Static link Arguments and returns Local variables Temporaries 5

Stack Frame Maintenance Maintenance of stack is the responsibility of Calling sequence Subroutine prolog and epilog 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 6

Register Save & Local Variables Common strategy is to divide registers into callersaves and callee-saves sets Caller uses the callee-saves registers first Callee-saves registers are for local variables which are more likely live across function calls Caller-saves registers if necessary Caller-saves registers are mostly for temporary values (less likely live across function calls) Local variables and arguments 7 Assigned at fixed offsets from the frame (or stack) pointer at compile time

Calling Sequences Before the call, Caller Saves into the temporaries any caller-saves registers whose values will be needed after the call (i.e. live registers) Puts small arguments into registers (or in the stack) 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 argument build area at the top of the stack frame Use a special call instruction to jump to the subroutine and store return address on the stack or in a register 8

Calling Sequences In the prolog, Callee Subtracts the frame size from sp (frame grows towards lower address) Saves callee-saves registers if used anywhere inside callee In the epilog, Callee Puts return value into registers (memory if large) Restores callee-saves registers Adds to sp to deallocate the frame (restore sp) Restore fp to old fp Return to caller 9

Calling Sequences After call, Caller Moves return value from register to wherever it's needed (if appropriate) Restores caller-saves registers if needed (lazily over time, as their values are needed) 10

Arguments in Registers All arguments have space in the stack, whether passed in registers or not The subroutine may begin with some of the arguments already cached in registers, and stale values in memory 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 11

Optimization for Calling Sequence Many parts of the calling sequence, prologue, and/or epilogue can be omitted in common cases Particularly, leaf routines don't call other routines If another routine is called, ra (which contains return address) should be saved, but leaf routines don t save it Simple leaf routines can be exceptionally fast No local variables and nothing to save and restore in the stack Don't even use memory, just compute with (caller-saves) registers 12

Parameter Passing Three basic implementations for parameter passing Call-by-value Call-by-reference Call-by-closure (subroutine + environment) Variations 13 Call-by-result copying back when return Call-by-value/result copying and copying back Call-by-sharing copying in reference model Call-by-name substitute as macros Read-only prevent modification Named parameter specify corresponding formal parameters

Parameter Passing in C/C++ 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); 14

Parameter Passing in Ada Ada goes for semantics: who can do what in: out: in out: callee reads only callee writes and can then read (formal not initialized); actual modified 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 15

Parameter Passing in Others Language with a reference model of variables (Lisp, Clu) Pass by reference (sharing) is the obvious approach Fortran always uses call-by-reference (only option) If you pass a constant or expression, the compiler creates a temporary location to hold the value and pass its reference 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) A way to mimic macros for assembly language programmers 16

Parameter Passing Modes 17

Exceptions 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 18

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 users to explicitly handle errors in a uniform manner Allow users to handle errors without having to check these conditions before Explicitly in the program everywhere they might occur 19

Events What is an event? Running programs need to respond to the events Events occurs outside the programs at unpredictable times Examples Inputs from users (keyboard, mouse motion/click) I/O from network, disk Event handlers A special callback function A dedicated thread to handle 20

Event Handlers Sequential handlers Works in an asynchronous way Register handler and return Use a hardware interrupt mechanism OS calls handler routines Return to the program Thread-based handlers 21 Separate thread control thread Works in a synchronous way A dedicated thread is called Makes a system call for the next event Waits for it to occur Signal delivery for sequential handler

Coroutines Concurrently calling one another 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 Threads Separate stack should be maintained 22 Because they are concurrent (i.e., simultaneously started but not completed), coroutines cannot share a single stack

Multiple Stacks for Coroutines B calls subroutine S and created coroutine D static link (main) 23 Cactus Stack

Summary Function Stack frame Passing parameters Exception Event Unexpected/unusual HW/SW condition caused by current program Try-catch (Java, C++) HW/SW condition required to respond by current program Event handling mechanism Coroutine Concurrent execution of multiple sequences, but execute one at a time Transfer: non-local goto to jump to other coroutine s last transfer (continuation) Cactus stack: Multiple concurrent stacks 24