Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Parameter Passing. Variables: lvalues and rvalues In the assignment statement “X=Y”, variables X and Y are being used in different ways. Y is being used.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 Chapter 8 – Subroutines and Control Abstractions.
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 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 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
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
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
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.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
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,
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Chapter 9 Subprograms Fundamentals of 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 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.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
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.
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.
ISBN Chapter 9 Subprograms Sections
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 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
ISBN Chapter 9 Subprograms. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
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.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Chapter 9 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
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Subprograms In Text: Chapter 9.
Languages and Compilers (SProg og Oversættere)
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book calls it protocol

Subprograms Actions: direct function relating input values to output values; side effects on global state and subprogram internal state May depend on implicit arguments in form of non-local variables

Subprogram As Abstraction Subprograms encapsulate local variables, specifics of algorithm applied –Once compiled, programmer cannot access these details in other programs

Subprogram As Abstraction Application of subprogram does not require user to know details of input data layout (just its type) –Form of information hiding

Subprogram Parameters Formal parameters: names (and types) of arguments to the subprogram used in defining the subprogram body Are types checked? Is number of arguments fixed? Actual parameters: arguments supplied for formal parameters when subprogram is called

Establishing the correspondence positional - good for small lists Keyword parameters Example: (Ada) SUB(Y=>ACT_Y, MAX=>100) Disadvantage is that the caller must know names is callee May be default values (Ada, C++) if no parameter is supplied

Subprogram Parameters Parameters may be used to: –Deliver a value to subprogram – in mode –Return a result from subprogram – out mode –Both – in out mode Most languages use only in mode Ada uses all

Parameter Passing Aliases may be created a formal parameter is a nonlocal variable the same data object passed for two parameters CALL S(X,X) With aliasing, interesting problems in optimizations occur.

Parameter Passing (In Mode) Pass-by-value: Subprogram makes local copy of value (r-value) given to input parameter Assignments to parameter not visible outside program Most common in current popular languages

If pass array by value, entire array does not get copied C is not consistent here transmission by constant value - as in C++ (make it by reference for speed, but don’t allow changes) no assignment to param is allowed OR is allowed, but only changes local copy For protection, then the formal parameter may not be transmitted to another subprogram except as a constant value parameter May be implemented as transmission by value or reference costly to pass by value if parameter is large - storage and time

Parameter Passing (In Mode) Pass-by-name: text for argument is passed to subprogram and expanded in each place parameter is used –Roughly same as using macros –Note, you can’t evaluate late without having “code” to execute –You also need to know a context for evaluating non-local variables Achieves late binding

Pass-by-name Example integer INDEX= 1; integer array ARRAY[1:2] procedure UPDATE (PARAM); integer PARAM begin PARAM := 3; INDEX := INDEX + 1; PARAM := 5; end UPDATE(ARRAY[INDEX]);

Pass-by-name Example Previous code puts 3 in ARRAY[1] and 5 in ARRAY[2] How is this accomplished if the compiled code must work for ANY argument that is passed? PARAM can be x, x+y, 2*t[6*x]+7 How can you generate code for UPDATE when you don’t know what is passed?

New interest in functional languages means more interest in delayed evaluation. Very flexible, but inefficient. Difficult to implement. Confusing to read/write. Some simple operations are not possible with pass by name. Lazy evaluation is another form of late binding. Only evaluate when it becomes necessary. Substitute name or expression (in calling environment) for formal parameter The name location binding is delayed until (and established fresh each time) the formal parameter is encountered. Implemented by passing parameter-less subprograms (thunks) rather than variable name. An expression needs to be evaluated IN the proper environment. Don't have mechanism to do that other than thru procedure call. Whenever formal parameter is referenced, a call is made to thunk, which evaluates the parameter in the proper (caller) environment and returns proper resulting value (or location)

Example: procedure R(var i,j: integer); begin var m:boolean; m := true; i := i + 1; j := j + 1; write(i,j); end; m := 2; for(i=0;i<10;i++) c[i]=10*i; R(m,c[m]); pass by reference: adds 1 to m and c[2] Pass by name: adds 1 to m and c[3]

Example for Call by Name b1: begin real x,y; y := 0.0; procedure G(t): name t; begin integer w; integer x; w := 10; y := 20; x := 50 print t x:= 0; print t end G; b2: begin real y; y := 0.5; x := 1.0; call G(y-x) end end thunk() return(y-x) end;

If name parameter is on left hand side, thunk would have to return the address of the element (rather than the value). Not a problem for in–only parameters

IN OUT parameters transmission by reference formal parameter is local object of type pointer If expression is passed as an in/out parameter: a temporary location may be passed (and then the copy is changed, not the original) Disadvantages: –access slower as is indirect (always follow a pointer to access), but passing is fast (only copy a pointer, not a whole structure) –may make inadvertent changes to parameters (if out only was desired)

Parameter Passing (In Out Mode) Same effect can be had in pass-by- value by passing a pointer

IN OUT parameters –Value-restore. Copy value in on call Copy changed value back on return. –Used to save cost of indirect access. –aliases may be problematic - especially likely if pass same parameter twice. Then if arguments are changed, original values may be changed differently depending on order of change (if value-restore)

Value-restore value copied at time of call and copied back at time of return has the same as call by reference if –the subprogram terminates normally, and –the called subprogram cannot also access the actual parameter through an alias –parameters are names for same argument doit(X,X). Need to know order arguments are copied back. Need to know whether address is computed again before copying back. XX(i,a[i])

OUT only parameters formal parameter is a local variable with no initial value copied back at termination of subprogram Pass by result Explicit function Values: may be considered an extra OUT parameter return(expr) value to be returned by assignment to function name if return is an address (e.g., list[index]), when is it evaluated? time of call? time of return?

Subprogram Implementation Subprogram definition gives template for its execution –May be executed many times with different arguments

Subprogram Implementation Template holds code to create storage for program data, code to execute program statements, code to delete program storage when through, and storage for program constants Layout of all but code for program statements called activation record

Actions to pass parameters: Point of call: parameters evaluated and list of parms is set up Perform type checking and promotion (if needed) point of entry: prologue of called routine completes association by copying pointers into locations or copying entire contents point of exit: epilogue of called routine must copy result values into actual parameters transmitted by result or value-result

Subprogram Implementation Code segment Activation Record Code to create activation record inst Program code Code to delete activation record inst const 1 const n Return point Static link Dynamic link Result data Input parameters Local parameters

Activation Records Code segment generated once at compile time Activation record instances created at run time Fresh activation record instance created for each call of subprogram –Usually put in a stack (runtime stack) –“return point” inserted first

Activation Records Static link – pointer to bottom of activation record instance of static parent –Used to find non-local variables Dynamic link – pointer to end of activation record instance of the caller –Used to delete subprogram activation record instance from runtime stack at completion (by resetting stack pointer)

Subprograms as parameters: Corresponding formal parameter is of type subprogram name Problems static type checking: cannot determine if number of arguments is correct Needs not just name, but full procedure specification: type returned, number order and type of args. nonlocal references (free variables) –variables without bindings assume same nonlocal environment as if inline expansion were used usually not what was intended –nonlocal reference means the same thing during execution of the subprogram passed as a parameter as it would if the subprogram were invoked at the point where it appears as an actual parameter in the parameter list Need to create the correct nonlocal environment fairly straightforward with static chain method determine correct static chain pointer for the subprogram parameter and pass that along as part of the information transmitted with a subprogram parameter As in passing labels as parameters, need an (ip, ep) (instruction pointer, environment pointer) pair

Are three choices of environment for evaluating unbound variables of function passed as parameter 1.the environment of the subprogram to which it is passed (shallow binding) Not appropriate for block structures languages because of static binding of variables 2.the environment of the subprogram which is passed (deep binding) 3.the environment of the subprogram which passes the subprogram (ad hoc) not used