Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Implementing Subprograms
(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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
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:
Chapter 9: Subprogram Control
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 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Chapter 8 :: Subroutines and Control Abstraction
ISBN Chapter 10 Implementing Subprograms.
CS 2104 Prog. Lang. Concepts Subprograms
Chapter 9 Subprograms Fundamentals of Subprograms
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Basic Semantics Associating meaning with language entities.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
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.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 :: Subroutines and Control Abstraction
Implementing Subprograms
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Implementing Subprograms
Implementing Subprograms
Subprograms In Text: Chapter 9.
Implementing Subprograms
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Implementing Subprograms
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule – consider child code as copied into parent Subprogram used to –Separate program into logical units –Execute functions with different parameters

Recursion Must maintain storage for multiple copies of data Code is reentrant – stored separately Storage for return address and data Indirect recursion We do not know the number of times that our code or data will be copied PL/1 – specific statement – is recursive Non-recursive subprogram does not need stack implementation

Execution structure Child is explicitly called (exceptions are not) Entrance at top or elsewhere –FORTRAN, coroutines (resume) Scheduled subprogram call can delay transfer of control Coroutines have multiple activation records active at one time but single thread of control – their execution can be traced Tasks have multiple activation records active at one time and conceptually allow multiple threads of control

Implementation of non-recursive routines For execution efficiency –Code and data may be joined, with code directly referencing data address –Return address stored in no-op at top of subroutine code –Last statement of subroutine is branch to first address, indirectly For reliability, may require that code be stored separately (non self modifying code) Cost in storage of all (but smaller) activation records during program’s lifetime

Implementation for recursive routines Activation records (frames) on stack contain –Caller’s return address (next CIP) –Parameters –Local variables –Return value for functions and temps –Ptr to parent activation record (static chain ptr) –Ptr to caller’s activation record (dynamic chain ptr) Register contains PC (Current Instruction Ptr) Register contains base address of activation record (current environment pointer) Register contains stack pointer

Storage map in general System and program code Static variables Activation records – grow dynamically --- available storage ---- Heap storage Note deallocation of stack storage when module completes

Pascal forward declaration Module can generally call each other (indirect recursion) if signature (prototype) is known to them Pascal uses forward declaration –P. 356 – 10 out of 13 compilers handled a conforming program incorrectly

Binding of name to data object Reference environment for a module includes local, nonlocal and global declarations Static or dynamic binding of name to data object Scope of a declaration is the reference environment in which that declaration is valid Scope is static if compiler can determine the declaration’s reference environment (containing module) Static scope associated with block structured programming

Aliases Two names for the same data object in the (at least overlapping) same referencing environment –Ex: global variable passed as reference parameter with different name in subroutine

Implementation Static scope- compiler stores in code segment the address of the name’s data object, if known, else the offset from the appropriate module’s activation record plus the degree of nesting of that module to follow the static chain) Activation record will store values of local data objects but also a static chain ptr Dynamic scope – either search for the name through the dynamic chain (implies that names are stored on activation record) or update a central name table

Retention and deletion of environments Are modules history sensitive. Should they be? Static variables are retained Automatic variables are deleted Heap data objects are retained until explicitly destroyed or garbage is collected, but external access paths may be lost from the stack Parameters association should be deleted between calls Initial assignment of values to static variables

Parameter transmission Association of actual and formal parameter –Positional correspondence –Named correspondence Type checking Methods for transmission –Does program specify implementation ? Call by reference (var; &I) Call by value or copy (C default) Call by value-result (copy in/copy out) Call by name (text substitution) Call by result (function return) –Does program specify semantics Ada’s in/out compared with C’s const

Example (p. 383) Q (int i, int *j) { i = i + 10; *j = *j + 10; printf (…) } P() {int a = 2; int b = 3; Q( a, &b); printf (…) try Q(&a, &a); also for value/result; Discuss Q( a+b, &b) if reference or value/result is used Call by name

Example (p. 387) R( int *i, int *j) {*i = *i + 1; *j = *j + 1; } P() {int c[4] = {6,7,8, 0}; int m = 2; R (&m, & c[m]); Print all of c}

Exception handling (from chapt 11) User defined Bad_Data_Value : exception; exception when Bad_Data_Value => … when others => … end; //block p.438 Exception propagation – dynamic chain