Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.

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

Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
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.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
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.
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)
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.
Encapsulation by Subprograms and Type Definitions
Run-Time Storage Organization
Run time vs. Compile time
Semantics of Calls and Returns
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.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Chapter 8 :: Subroutines and Control Abstraction
CS 2104 Prog. Lang. Concepts Subprograms
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.
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
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Angela Guercio.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
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
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
ISBN Chapter 10 Implementing Subprograms.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing 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
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Chapter 9 Subprograms Fundamentals of Subprograms
Implementing Subprograms
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters  non-local references Results - data object or values delivered by the subprogram. Returned through  parameters,  assignments to non-local variables, or  explicit function values

Formal Parameters A formal parameter is a particular kind of local data object within a subprogram. It has a name, the declaration specifies its attributes.

Actual Parameters An actual parameter is a data object that is shared with the caller subprogram. a local data object belonging to the caller, a formal parameter of the caller, a non-local data object visible to the caller, a result returned by a function invoked by the caller and immediately transmitted to the called subprogram.

Establishing a Correspondence Positional correspondence – pairing actual and formal parameters based on their respective positions in the actual- and formal- parameter lists Correspondence by explicit name – The name is paired explicitly by the caller.

Methods for transmitting parameters Call by name - the actual parameter is substituted in the subprogram Call by reference – a pointer to the location of the data object is made available to the subprogram. The data object does not change position in memory. Call by value – the value of the actual parameter is copied in the location of the formal parameter.

Methods for transmitting parameters Call by value-result – same as call by values, however at the end of execution the result is copied into the actual parameter. Call by result – A parameter transmitted by result is used only to transmit a result back from a subprogram. The initial value of the actual- parameter data object makes no difference and cannot be used by the subprogram.

Methods for transmitting parameters Call by constant value – no change in the value of the formal parameter is allowed during program execution. The formal parameter acts as a local constant during the execution of the subprogram.

Examples: Pass by name in AlgolPass by reference in FORTRAN procedure S (el, k);SUNROUTINE S(EL, K) integer el, k;K = 2 beginEL = 0 k:=2;RETURN el := 0END end; A[1] := A[2] := 1;A(1) = A(2) = 1 i := 1;I = 1 S(A[i],i);CALL S (A(I), I)

Pass by name example: After calling S(A[i], i), the effect is as if the procedure were i := 2; A[i] := 0; As a result A[2] becomes 0. On exit we have i = 2, A[1] = 1, A[2] = 0.

Pass by reference example: Since at the time of call i is 1, the formal parameter el is linked to the address of A(1). Thus it is A(1) that becomes 0. On exit we have: i = 2, A(1) = 0, A(2) = 1

Transmission Semantics Types of parameters: input information output information (the result) both input and output The three types can be accomplished by copying or using pass-by-reference Return results: Using parameters Using functions with a return value

Implementation of Parameter Transmission Implementing formal parameters: Storage - in the activation record Type: Local data object of type T in case of pass by value, pass by value-result, pass by result Local data object of type pointer to T in case of pass by reference Call by name implementation: the formal parameters are subprograms that evaluate the actual parameters.

Actions for parameter transmission Associated with the point of call of the subprogram each actual parameter is evaluated in the referencing environment of the calling program, and a list of pointers is set up.

Actions for parameter transmission Associated with the entry and exit in the subprogram on entry: copying the entire contents of the actual parameter in the formal parameter, or copying the pointer to the actual parameter on exit: copying result values into actual parameters or copying function values into registers

Compiler tasks 1. Generate prologue and epilogue code to perform the actions on entry and exit. This code is stored in the segment code part of the activation record of the subprogram 2. Perform the necessary static type checking

Explicit Common Environment Specification: A common environment not local to any single subprogram. It may contain: definitions of variables, constants, types. It cannot contain: subprograms, formal parameters. Implementation: as a separate block of memory storage.