Chapter 8 :: Subroutines and Control Abstraction

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

10/6: Lecture Topics Procedure call Calling conventions The stack
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
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 :
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
CS 536 Spring Run-time organization Lecture 19.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run-Time Storage Organization
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Overview Parameter passing modes.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
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 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
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.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
CSC 8505 Compiler Construction Runtime Environments.
Lecture 19: Control Abstraction (Section )
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
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?
Computer Architecture & Operations I
Storage Allocation Mechanisms
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 8 :: Subroutines and Control Abstraction
Implementing Subprograms Chapter 10
Functions.
Run-time organization
Procedures (Functions)
Functions and Procedures
CS 3304 Comparative Languages
Chapter 9 :: Subroutines and Control Abstraction
CS 3304 Comparative Languages
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Frame Linkage.
The University of Adelaide, School of Computer Science
Topic 3-a Calling Convention 1/10/2019.
UNIT V Run Time Environments.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott Adapted from Scott, 2006

Subroutines/Methods/Functions Subroutines are the primary control abstraction c.f., data abstraction Subroutine performs a well defined operation Formal Parameters Specified in the subroutine signature Actual Parameters == Arguments Values passed to the subroutine Function is a subroutine that returns something Procedure is a subroutine that returns nothing Adapted from Scott, 2006

Allocation strategies -- static 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 Adapted from Scott, 2006

Allocation strategies (2) Review Of Stack Layout Allocation strategies (2) Stack arguments local variables temporaries bookkeeping information like the return address Heap dynamic allocation Adapted from Scott, 2006

Review Of Stack Layout frame pointer Static links point to lexically surrounding routine Dynamic links point to calling context (saved value of the frame pointer) Adapted from Scott, 2006

Contents of a stack frame Review Of Stack Layout Contents of a stack frame bookkeeping return PC (dynamic link) saved registers static link arguments and returns local variables Adapted from Scott, 2006

Calling Sequences Maintenance of stack is responsibility of calling sequence and 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 e.g., there may be fewer registers IN USE at the point of call than are used SOMEWHERE in the callee Goal is to save only those registers needed by both caller and callee Adapted from Scott, 2006

Calling Sequences Common strategy is to divide registers into caller-saves and callee-saves sets callee uses the "callee-saves" registers first callee uses "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 Adapted from Scott, 2006

Calling Sequences Adapted from Scott, 2006

Calling Sequences (C on MIPS) Caller saves into the temporaries and locals area any caller-saves 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 Adapted from Scott, 2006

Calling Sequences (C on MIPS) In prolog, Callee allocates a frame by subtracting framesize from sp saves callee-saves registers used anywhere inside callee In epilog, Callee puts return value into registers (memory if large) restores saved registers using sp as base adds to sp to deallocate frame does jra Adapted from Scott, 2006

Calling Sequences (C on MIPS) 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 Adapted from Scott, 2006

Calling Sequences (C on MIPS) 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 Adapted from Scott, 2006

Calling Sequences (C on MIPS) 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 Adapted from Scott, 2006

Parameter passing mechanisms value (copying in) reference (aliasing) value/result (copying in and copying out) closure/name (reference to subroutine & context) Many languages (e.g., Pascal) provide value and reference directly Adapted from Scott, 2006

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 ); Adapted from Scott, 2006

Ada and PL/SQL go for semantics: who can do what Parameter Passing Ada and PL/SQL go for semantics: who can do what In: callee reads only call by value Out: callee writes and can then read formal not initialized) actual modified call by result In out: callee reads and writes call by value/result Ada in/out is always implemented as value/result for scalars, and either value/result or reference for structured objects Adapted from Scott, 2006

Pass by reference is the only option in Fortran Parameter Passing In a language with a reference model of variables (Lisp, Smalltalk), pass by reference (sharing) is the obvious approach Pass by reference is 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 Adapted from Scott, 2006

Parameter Passing Adapted from Scott, 2006