Subprograms - implementation

Slides:



Advertisements
Similar presentations
1 Chapter 8 – Subroutines and Control Abstractions.
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.
PLLab, NTHU,Cs2403 Programming Languages Subprogram and its implementation.
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.
Semantics of Calls and Returns
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
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.
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.
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.
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
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
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
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
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.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
COMPILERS Activation Records
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
CSC 533: Programming Languages Spring 2015
Implementing Subprograms
PZ09A - Activation records
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 flow of control activation record calling program subprogram return address parameters local variables ... call sub flow of control

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 linked program with one subprogram A data for main at call save status of main put parameters in activation record save ‘return address’ start sub A (*) activation record for sub A execution starts here call A code for main call A at return put parameters (and function value) in main restore status of main restart main at ‘return address’ * code for sub A

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) example 1. set up memory main data sub activation record main code with line numbers sub code 2. e.g. main make arrays: sub to sort and return max as fn value MAIN x = array 6 2 4 1 call maxx = sortmax(x) y = array 6 2 4 1 call maxy = sortmax(y) SUB sort (arr)

FORTRAN 77 <--> Algol, imperative language no nested subprograms no recursion parameters value-result static memory allocation non-local refs (COMMON) Algol nested recursion and reference dynamic SCOPING

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

p.445 void sub(float total, int part) { int list[5]; float sum; … }

Standard imperative language page 446-447 example - no non-local scope and no recursion page 448-450 example - recursion

p.446-7 1 2 3 void fun1(float r){ int s, t; … <------1 fun2(s); … } … <------1 fun2(s); … } void fun2(int x){ int y; … <------2 fun3(y); void fun3(int q){ … <------3 void main(){ float p; … fun1(p); } p.446-7 1 2 3

Standard imperative language page 446-447 example - no non-local scope and no recursion page 448-450 example - recursion

p.448-50 int factorial;(int n){ <------1 if (n<=1) return 1; else return <------2 } void main(){ int value; value = fact(3); <------3 p.448-50

ending recursive calls

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 (no longer popular)

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 sub11() sub1() static depth 1 2 nesting depth, offset: z: 0, 3 x: 2, 3 y: 1, 4 w: 0, 4

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: how to find it? return address static link dynamic link parameters local variables program main var x,y,w procedure sub1 var y,z begin sub2() end procedure sub2 var x,z sub1() sub1 return address static link dynamic link parameters local variables sub2 return address static link dynamic link parameters local variables sub1 return address static link dynamic link parameters local variables sub2 return address static link dynamic link parameters local variables sub1 return address static link dynamic link parameters local variables main

Scope by static chaining procedure Main_2 is X: Integer; procedure BigSub is A,B,C: Integer; procedure Sub1 is A,D: Integer; begin -- of Sub1 A := B + C; <----1 end; -- Sub1 procedure Sub2(X: Integer) is B,E: Integer; procedure Sub3 is C,E: Integer; begin -- Sub3 Sub1; E := B + A; <----2 end; -- Sub3 begin -- Sub2 Sub3; A := D + E; <----3 end; -- Sub2 begin -- Bigsub Sub2(7); end; -- Bigsub begin -- Main_2 BigSub; end; -- Main_2 Scope by static chaining page 455-457 – Ada Main_2 Bigsub Sub1 Sub2 Sub3

Setting static link using caller static links 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)

procedure Main_2 is X: Integer; procedure BigSub is A,B,C: Integer; procedure Sub1 is A,D: Integer; begin -- of Sub1 A := B + C; <----1 end; -- Sub1 procedure Sub2(X: Integer) is B,E: Integer; procedure Sub3 is C,E: Integer; begin -- Sub3 Sub1; E := B + A; <----2 end; -- Sub3 begin -- Sub2 Sub3; A := D + E; <----3 end; -- Sub2 begin -- Bigsub Sub2(7); end; -- Bigsub begin -- Main_2 BigSub; end; -- Main_2

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

Dynamic scoping ‘deep access’ - follow dynamic chain ‘shallow access’ 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 call C proc C var x=222, y=333, w=555 main var r=0,t=0 call A while in C: r t w x y z 66 555 55 222 22 2 333 3 4