Chapter 9 :: Subroutines and Control Abstraction

Slides:



Advertisements
Similar presentations
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Advertisements

The University of Adelaide, School of Computer Science
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
1 Subroutines and Control Abstraction. 2 Control Abstraction Abstraction Abstraction associate a name N to a program part P associate a name N to a program.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Run-Time Storage Organization
Run time vs. Compile time
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Chapter 8 :: Subroutines and Control Abstraction
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Activation Records CS 671 February 7, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens Syntactic analysis.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CSC 8505 Compiler Construction Runtime Environments.
Lecture 19: Control Abstraction (Section )
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Copyright © 2009 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Chapter 14 Functions.
Computer Architecture & Operations I
Storage Allocation Mechanisms
Lecture 7 Macro Review Stack Frames
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Chapter 10 : Implementing Subprograms
Chapter 8 :: Subroutines and Control Abstraction
Implementing Subprograms Chapter 10
Functions.
Implementing Subprograms
COMPILERS Activation Records
Run-time organization
Introduction to Compilers Tim Teitelbaum
Functions and Procedures
Chapter 7 Subroutines Dr. A.P. Preethy
CS 3304 Comparative Languages
Chapter 10: Implementing Subprograms Sangho Ha
CS 3304 Comparative Languages
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
Implementing Subprograms
by Richard P. Paul, 2nd edition, 2000.
Topic 3-a Calling Convention 1/10/2019.
Code Shape IV Procedure Calls & Dispatch
UNIT V Run Time Environments.
Run Time Environments 薛智文
Runtime Environments What is in the memory?.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Chapter 9 :: Subroutines and Control Abstraction Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright © 2016 Elsevier

Allocation strategies 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

Review Of Stack Layout

Allocation strategies (2) Review Of Stack Layout Allocation strategies (2) Stack parameters local variables temporaries bookkeeping information Heap dynamic allocation

Contents of a stack frame 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

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

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

Calling Sequences

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

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)

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

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

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

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

C/C++: functions Parameter Passing 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);

Ada goes for semantics: who can do what 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

It's also the only option in Fortran 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

Parameter Passing

What is an exception? Examples 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

What is an exception handler? 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

Coroutines can be used to implement 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

Coroutines