Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.

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

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.
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.
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:
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
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.
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.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
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.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
April 23, ICE 1341 – Programming Languages (Lecture #16) In-Young Ko Programming Languages (ICE 1341) Lecture #16 Programming Languages (ICE 1341)
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
國立台灣大學 資訊工程學系 薛智文 98 Spring Run Time Environments (textbook ch# 7.1–7.3 )
Subprograms - implementation
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
7. Runtime Environments Zhang Zhizheng
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
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing.
ISBN Chapter 10 Implementing Subprograms.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
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
COMPILERS Activation Records
Implementing Subprograms
Functions and Procedures
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
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
CSC 533: Programming Languages Spring 2018
Implementing Subprograms
CSC 533: Programming Languages Spring 2019
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:

Subprograms - implementation

Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate local variables establish links to non-local variables begin execution of subprogram

Returning from a subprogram  returning control to a calling program: pass back parameters (and function value) deallocate local variables restore caller’s links to non-local variables restore conditions in calling program restart execution of calling program

General design calling program call sub subprogram return address parameters local variables... activation record

Using a compiled subprogram  compiled code +  activation record parameter space other local data space (function value) return address (in calling program) … more later

Simple Example - FORTRAN 77 (Sebesta)  linker puts executable program together: executable code for main program executable code for subprograms data space for main program data space for subprograms (activation records)

Simple Example - FORTRAN 77 (Sebesta)  linked program with one subprogram A data for main activation record for sub A code for main code for sub A execution starts here call A at call  save status of main  put parameters in activation record  save ‘return address’  start sub A (*) at return  put parameters (and function value) in main  restore status of main  restart main at ‘return address’ *

Sebesta’s first simple example: FORTRAN 77  FORTRAN 77 is simple no nested subprograms no recursion parameter passing by value - result static memory allocation in subprograms non-local references outside of call- return process (COMMON)

FORTRAN 77 to standard imperative language (Algol)  FORTRAN 77 Algol no nested subprograms nested no recursion recursion parameter passing by value - result and by reference static memory allocation dynamic non-local references (COMMON) SCOPING

Standard imperative language  activation record created dynamically in statically scoped language return address in caller static link (scope) dynamic link (caller AR) parameters local variables activation record provided by caller

Standard imperative language  page example - no non-local scope and no recursion  page example - recursion

Scoping – non-local references  all references are SOMEWHERE on the run-time stack find the proper activation record instance find the reference (offset) in the ARI  two implementation strategies static chains displays

Non-local references by static chaining  uses static link field in activation record  static link must refer to static parent of subprogram (not to caller)  nested static scopes are found by following the chain of static link fields  static depth – depth of nesting of a procedure: main program == 0

Non-local references by static chaining: static_depth, nesting_depth program main var x,y,w procedure sub1 var z,y procedure sub11 var z,w begin z = x + y * w end begin sub11() end begin sub1() end static depth0 1 2 nesting depth, offset: z: 0, 3 x: 2, 3 y: 1, 4 w: 0, 4

Scope by static chaining  page – Pascal example with static link chains MAIN_2 BIGSUB SUB_1 SUB_2 SUB_3

Setting the static link at call time  how to find the activation record of the static parent? search along dynamic links (slow, especially with recursion) – depends on nesting of calls use static links (faster – depends on static nesting only) but HOW?

Setting the static link at call time: illustration program main var x,y,w procedure sub1 var y,z begin sub2() end procedure sub2 var x,z begin sub1() end begin sub1() end return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables main sub1 sub2 sub1 sub2 sub1

Setting static link using caller static links, p  At call: SUB3 calls SUB1 static depth of caller SUB3 - 3 static depth of parent of SUB1 (BIGSUB) - 1 nesting depth (difference) 3-1 = 2 follow caller’s (SUB3) static chain 2 links to parent (BIGSUB) of called subprogram (SUB1) put address of parent (BIGSUB) in static link field of subprogram (SUB1)

Scoping – non-local references  all references are SOMEWHERE on the run-time stack find the proper activation record instance find the reference (offset) in the ARI  two implementation strategies static chains displays

Non-local references by display  uses a display -array of references to activation records (no static links)  gives constant time access to non- local identifiers even with deeply nested subprogram scopes  static depth is index into display array

Non-local references by display (1)  to access non-local variable use static depth of scope (procedure) of variable to find reference in display follow reference to activation record of procedure use offset to find variable

Non-local references by display (2)  to update display when calling procedure A (static depth a)  make activation record for A on stack  store current contents of display[a] in activation record  put pointer to activation record of A in display[a]

Non-local references by display (3)  to update display when terminating procedure A  restore contents of display[a] as saved in activation record  remove activation record of A

Blocks  block scopes are like procedures but their order of activation is fixed at compile time  consecutive blocks can share space in AR of procedure  re-used identifier names distinguished by offset

Procedure Parameters – binding to an environment (eg p.379) return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables return address static link dynamic link parameters local variables SUB1 SUB3 SUB4 SUB2 SUB1 SUB2 SUB3 SUB4 shallow binding – for dynamic scope deep binding – appropriate for static scope

Procedure Parameters – binding to an environment  problem for deep binding: receiving procedure (SUB4) may not be in path of parent (SUB3) of actual parameter procedure (SUB2) SUB1 SUB3 SUB4 SUB2  solution: caller (SUB3) passes pointer to parent (SUB3) with actual parameter (SUB2)

Procedure parameters  with static chaining – only need reference to parent of actual parameter to set static link  with displays – may force more than one change to display array -> activation record must save multiple changes (or save entire display)

Dynamic scoping  ‘deep access’ - follow dynamic chain can be slo-o-o-o-o-ow  ‘shallow access’

Dynamic scoping  ‘deep access’  ‘shallow access’ – local variables are not in activation records; each has its own stack

Shallow access by variable stacks proc A var x=2, y=3, z=4 begin call B end proc B var x=22, w=55, t=66 begin call C end proc C var x=222, y=333, w=555 begin call B end main var r=0,t=0 begin call A end rtwxyzrtwxyz while in C: