Chapter 9: Subprogram Control

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
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.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
Semantics of Calls and Returns
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
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 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
ISBN Chapter 10 Implementing Subprograms.
1 Programming Languages Implementation of Control Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
Names and Binding In procedural programming, you write instructions the manipulate the “state” of the process where the “state” is the collection of variables.
CS 2104 Prog. Lang. Concepts Subprograms
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
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.
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.
Basic Semantics Associating meaning with language entities.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
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.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
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.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
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
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
ISBN Chapter 10 Implementing Subprograms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
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?
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Names and Attributes Names are a key programming language feature
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Midterm Review In Text: Chapters 1-3, 5-10, 15.
Implementing Subprograms
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Languages and Compilers (SProg og Oversættere)
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Chapter 9: Subprogram Control interaction among subprograms how subprograms pass data among themselves Subprogram Sequence Control Attributes of Data Control Parameter passing Explicit Common Environments

Subprogram Sequence Control Simple subprogram call return Copy rule view of subprograms: the effect of a call statement is the same as if the subprogram were copied and inserted into the main program.

Assumptions Subprograms cannot be recursive Explicit call statements are required Subprograms must execute completely at each call Immediate transfer of control at point of call Single execution sequence

Simple flow of execution CALL RETURN

Simple call-return subprograms Execution of subprograms Subprogram definition. Subprogram activation.

Subprogram definition The definition is translated into a template, used to create an activation each time a subprogram is called.

Subprogram activation a code segment (the invariant part) - executable code and constants, an activation record (the dynamic part) - local data, parameters. created anew each time the subprogram is called, destroyed when the subprogram returns.

System-defined pointers Current-instruction pointer – CIP address of the next statement to be executed Current-environment pointer – CEP pointer to the activation record.

On call instruction An activation record is created Current CIP and CEP are saved in the created activation record as return point CEP is assigned the address of the activation record. CIP gets the address of the first instruction in the code segment The execution continues from the address in CIP

On return The old values of CIP and CEP are retrieved. The execution continues from the address in CIP Restrictions of the model: at most one activation of any subprogram

The simplest implementation Allocate storage for a single activation record statically as an extension of the code segment. Used in FORTRAN and COBOL. The activation record is not destroyed - only reinitialized for each subprogram execution. Hardware support - CIP is the program counter, CEP is not used, simple jump executed on return.

Stack-based implementation The simplest run-time storage management technique call statements : push CIP and CEP return statements : pop CIP and CEP off of the stack. Used in most C implementations LISP: uses the stack as an environment.

Recursive Subprograms Specification Syntactically - no difference Semantically - multiple activations of the same subprogram exist simultaneously at some point in the execution. E.G. the first recursive call creates a second activation within the lifetime of the first activation.

Implementation Stack-based - CIP and CEP are stored in stack, forming a dynamic chain of links. A new activation record is created for each call and destroyed on return. The lifetimes of the activation records cannot overlap - they are nested.

Attributes of Data Control Data control features determine the accessibility of data at different points during program execution. Central problem: the meaning of variable names, i.e. the correspondence between names and memory locations.

Names and Referencing Environments Two ways to make a data object available as an operand for an operation Direct transmission Referencing through a named data object

Direct transmission A data object computed at one point as the result of an operation may be directly transmitted to another operation as an operand Example: x = y + 2*z; The result of multiplication is transmitted directly as an operand of the addition operation

Referencing through a named data object A data object may be given a name when it is created, the name may then be used to designate it as an operand of an operation.

Program elements that may be named To be discussed next Variables Formal parameters Subprograms resolved at translation time: Defined types Defined constants Labels Exception names Primitive operations Literal constants

Associations and Referencing Environments Association: binding identifiers to particular data objects and subprograms Referencing environment: the set of identifier associations for a given subprogram. Referencing operations during program execution: determine the particular data object or subprogram associated with an identifier

Local referencing environment The set of associations created on entry to a subprogram formal parameters, local variables, and subprograms defined only within that subprogram

Non-local referencing environment The set of associations for identifiers used within a subprogram not created on entry to it Global referencing environment: associations created at the start of execution of the main program, available to be used in a subprogram Predefined referencing environments: predefined associations in the language definition

Associations Visibility of associations Associations are visible if they are part of the referencing environment. Otherwise associations are hidden Dynamic Scope of associations The set of subprogram activations within which the association is visible

Aliases for Data Objects Multiple names of a data object separate environments - no problem in a single referencing environment - called aliases. Problems with aliasing Can make code difficult to understand Implementation difficulties at the optimization step - difficult to spot interdependent statements - not to reorder them

Example of aliasing Program main; var I: integer; procedure Sub1 ( var J: integer); begin ……… (* I and J refer to same data object *) end; …. Sub1(I); …. end.

Static and Dynamic Scope The dynamic scope of an association for an identifier: the set of subprogram activations in which the association is visible during execution. tied to the dynamic chain of subprogram activations. The static scope of a declaration the part of the program text where the declared identifier is used.

Dynamic scope rules Static scope rules Relate references with associations for names during program execution Static scope rules : relate references with declarations of names in the program text.

Block structure Block-structured languages : Each program or subprogram is organized as a set of nested blocks. Each block introduces a new local referencing environment.

Static scope rules for block-structured programs Subprogram A Declaration of X Declaration of Y Subprogram B Declaration of Y Declaration of Z Use of Y Use of X Use of Z Static scope rules for block-structured programs Hidden to A

Local Data and Local Referencing Environments Local environment of a subprogram: various identifiers declared in the subprogram : variables, parameters, subprogram names. Static scope rules: implemented by means of a table of the local declarations Dynamic scope rules: Retention - Associations and the bound values are retained after execution Deletion - Associations are deleted

Implementation of dynamic scope rules By means of a local environment table to associate names, types and values. Retention: the table is kept as part of the code segment Deletion: the table is kept as part of the activation record, destroyed after each execution.