Lecture 19: Control Abstraction (Section )

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

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.
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
Procedure call frame: Hold values passed to a procedure as arguments
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha
Procedures and Control Flow CS351 – Programming Paradigms.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
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)
Runtime Environments Source language issues 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:
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
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.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
1 Exceptions (Section 8.5) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
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,
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Compiler Construction
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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.
1 Run-Time Environments. 2 Procedure Activation and Lifetime A procedure is activated when called The lifetime of an activation of a procedure is the.
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.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
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,
CSC 8505 Compiler Construction Runtime Environments.
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Implementing Subprograms
© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 3.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
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
Lecture 7 Macro Review Stack Frames
Run-Time Environments Chapter 7
Implementing Subprograms Chapter 10
Computer Science 210 Computer Organization
COMPILERS Activation Records
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Run-Time Environments
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Run-Time Environments
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

Lecture 19: Control Abstraction (Section 8.1-8.2) CSCI 431 Programming Languages Fall 2002 Lecture 19: Control Abstraction (Section 8.1-8.2) A compilation of material developed by Felix Hernandez-Campos and Michael Scott

Abstraction Programming languages support the binding of names with potentially complex program fragments that can be used through an interface Programmers only need to know about the purpose of the fragment rather than its implementation  Abstraction A control abstraction performs a well-defined operation Subroutines A data abstraction represents information Data structures Most data structures include some number of control abstractions

Subroutines Execute an operation on behalf of a calling program unit Subroutines can be parameterized The parameters in the definition of the function are known as formal parameters The parameters passed in the subroutine call are known as actual parameters or arguments At the time of the call, actual parameters are mapped to formal parameters Functions are subroutines that return a value, while procedures are subroutines that do not return a value

Subroutine Frames 1001: A(3) … 2001: int A(int n) { int m = n * n; return m + A(n-1); } Actual Parameters Formal Parameters Each subroutines requires a subroutine frame (a.k.a activation record) to keep track of Arguments and return values Local variables and temporaries Bookkeeping information When a subroutine returns, its frame is removed

Call Stack Recursive Subroutine

C Example Stack pointer sp Frame pointer fp Top of the frame stack Frame pointer fp Access to arguments and locals via offset of fp They differ if temporary space is allocated in the stack Why do we need a stack?

C Example Stack pointer sp Frame pointer fp Top of the frame stack Frame pointer fp Access to arguments and locals via offset of fp They differ if temporary space is allocated in the stack

Stack Maintenance Calling sequence (by caller) and prologue (by callee) are executed in the way into the subroutine: Pass parameters Save return address Change program counter Change stack pointer to allocate stack space Save registers (including frame pointer) Change frame pointer to new frame Initialization code Separation of tasks? As much as possible in the callee (only once in the program)

Stack Maintenance Epilogue (by callee) is executed in the way out of the subroutine: Pass return value Execute finalization code Deallocate stack frame (restore stack pointer) Restore registers

C Example Calling sequence by the caller: Caller saves registers in local variables and temporaries space 4 scalar arguments in registers Rest of the arguments at the top of the current frame Return address in register ra and jump to target address

C Example Prologue by the callee: Subtract the frame size from the sp Save registers at the beginning of the new frame using sp as the base for displacement Copy the sp into the fp

C Example Callee epilogue: Set the function return value Copy fp to sp to deallocate any dynamically allocated space Restore registers including ra (based on sp) Add frame size to sp Return (jump to ra)

Pascal Example

Parameter Passing Pass-by-value Pass-by-result Pass-by-value-result Input parameter Pass-by-result Output parameter Pass-by-value-result Input/output parameter Pass-by-reference Input/Output parameter, no copy Pass-by-name Textual substitution

Closure Closure are subroutines that maintain all their referencing environment This mechanism is also known as deep binding This is significant when subroutines are passed as arguments

Exception Handling An exception is an unexpected or unusual condition that arises during program execution Raised by the program or detected by the language implementation Example: read a value after EOF reached Alternatives: Invent the value (e.g. –1) Always return the value and a status code (must be checked every time) Pass a closure (if available) to handle errors

Exception Handling Exception move error-checking out of the normal flow of the program No special values to be returned No error checking after each call Exceptions in Java http://java.sun.com/docs/books/tutorial/essential/exceptions/