Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.

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.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
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 :
1 Chapter 8 – Subroutines and Control Abstractions.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
(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:
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Run-Time Storage Organization
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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
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
Week 8-9b spring 2002CSCI337 Organization of Prog. Lang0 A Brief Recap Subprogram benefits, basic characteristics from names to their values  names can.
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.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
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.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
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.
The Procedure Abstraction Part I: Basics Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS 2104 Prog. Lang. Concepts Subprograms
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 9 Functions It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures. A. Perlis.
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.
Basic Semantics Associating meaning with language entities.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
ISBN Chapter 10 Implementing Subprograms.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
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.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
ISBN Chapter 10 Implementing Subprograms.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
Procedure Activations Programming Language. Exploration Name ocurrenceDeclarationLocationValue scopeactivationstate a.From names to their declarations.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Functions.
Implementing Subprograms
Principles of programming languages 4: Parameter passing, Scope rules
Run-Time Environments
Run-Time Environments
UNIT V Run Time Environments.
Presentation transcript:

Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined by two elements: –A specification (protocol, interface, declaration) –A body (definition) The specification usually identifies the parameters (Perl uses a default parameter and usually declares their types (not in weakly typed languages). In some cases it is critical to be able to separate a procedure’s declaration from its invocation. This happens, for example, when one must construct mutually recursive procedures. The body usually defines a block of code that is to be executed when the procedure is called (invoked, activated, executed). A procedure is called by specifying its name and a list of arguments (actual parameters) to be bound to the (formal) parameters of the procedure. The procedure body is then executed. In the old-old days, functions returned values and procedures didn’t. It is common nowadays to blur the line of distinction between these and let any procedure return a value. Control returns from a procedure when its execution is completed either by leaving its block or by executing a return statement. The value returned can either be implicit (the value returned by the last executed statement in the block) or explicit (the value associate with the executed return statement.

Procedure Semantics Most current language implementations follow the tenets of block structure in procedure call semantics. Each procedure definition is a block. When a procedure is invoked, an activation record (frame) is created to contain name bindings for that procedure during its lifetime. This activation record contains two critical references: –The dynamic chain pointer (this points to the caller’s activation record in the stack. This is remembered so that the caller’s activation can be restored upon return) –The static chain pointer (this points to the most recent activation record of the block in which this procedure was declared. This is remembered so that references to non-local variables can be found from the lexical (or static) nesting structure of the program.

Parameters Nonlocals and Closures Procedures are useless unless they can compute different values in different contexts. To do this requires information from outside the procedure body. Two methods exist to get such external values: –Parameters –Nonlocal references If a procedure has no nonlocal references (if it uses only its parameters) it is said to be closed. In languages that can create procedures at runtime, the nonlocal referencing environment of a procedure must be stored along with it at runtime. The object that represents a procedure together with its nonlocal referencing environment is known as a closure.

Parameter Passing Methods Pass by Value This method is common and useful. The values of the arguments are copied into the activation record of the called procedure. As C demonstrates, aliases can occur if pointers are passed by value. Pass by Reference This method allocates a hidden or buried pointer to the argument in the called procedure’s activation record. An implicit dereference is performed each time the parameter is used. This allows the call to be made without copying the argument (which could be a large structure or array). Problems can occur if expressions are allowed to be passed by reference. Pass by Value-Result This method is quite rare. It involves pass by value (copy-in), followed by execution of the body, followed by a copy-out of the parameter values to the original arguments. Pass by Name This method (from Algol 60) was created when parameter passing was poorly understood. It involves implementing the same behavior as would occur if the arguments were textually substituted (with appropriate renamings) in place of the parameters in the body, and the body were then evaluated. The concept of a thunk (a piece of code to evaluate the name parameter) was invented to implement this complex method. The complexity is clear when one realizes it’s impossible to write a swap procedure using name parameter passing and also looks at Jensen’s device.