Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing.

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

Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational 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.
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:
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Semantics of Calls and Returns
Chapter 9: Subprogram Control
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.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
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.
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.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Subprograms - implementation
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
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.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
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.
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
Implementing Subprograms
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Runtime Environments What is in the memory?.
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
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Chapter Ten: Implementing Subprograms Lesson 10

Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing arrays  Passing functions  This lesson: parameter passing revisited  The how 3/1/20162Implementing Subprograms

Early programming languages  No stack  Two separate parts  Code  Non-code part (local variables and data that can change)  In concept: activation record  Actual allocated space: an instance of an activation record 3/1/2016Implementing Subprograms3 Local VariablesParametersReturn Address

Example  No stack: Main and 3 subprograms  Local variables  Parameters  Return Address  What were these called again?  Could be proximal to procedures 3/1/2016Implementing Subprograms4

Semantics of a call  Linking 3/1/2016Implementing Subprograms5 gcc -c A.c gcc -c B.c gcc -c C.c gcc MAIN.c gcc -o mainProg MAIN.o A.o B.o C.o gcc -c A.c gcc -c B.c gcc -c C.c gcc MAIN.c gcc -o mainProg MAIN.o A.o B.o C.o Embeds code that: Prologue (function call) Execution status of caller saved Loads parameters Loads return address Initializes local variables Transfers control to called procedure Epilogue (return) Transfer out variables (if pass by value) Load return value of function in an accessible location Execution status of caller restored Transfers control back to calling procedure Activation Records

Stack supported  Why a stack implementation?  What’s different?  Dynamic link points to the base of the ARI of the caller  Why would we need such a thing?  Dynamic chain (or call chain) 3/1/2016Implementing Subprograms6 Local variablesParametersDynamic linkReturn address Stack Environment Pointer (EP) Pointer to base of active ARI When a subprogram is called the current EP is stored in the dynamic link of the new ARI Local offset is a local variable’s location relative to the EP Environment Pointer (EP) Pointer to base of active ARI When a subprogram is called the current EP is stored in the dynamic link of the new ARI Local offset is a local variable’s location relative to the EP

What if statically scoped?  Need a pointer to the containing subprogram, not just caller  Static link  Why would we need such a thing?  Static chain 3/1/2016Implementing Subprograms7 Local variablesParametersDynamic linkStatic linkReturn address Stack

An Example: C Function void sub(float total, int part) { int list[5]; float sum; … } [4] [3] [2] [1] [0] 3/1/20168Implementing Subprograms Where does dynamic link point? Static link? Return address? Where does dynamic link point? Static link? Return address?

An Example Without Recursion void fun1(int x) { int y;... C(y);... } void fun2(float r) { int s, t;... A(s);... } void fun3(int q) {... } void main() { float p;... B(p);... } 3/1/20169Implementing Subprograms MAIN calls fun1 fun1 calls fun2 fun2 calls fun3 MAIN calls fun1 fun1 calls fun2 fun2 calls fun3

An Example With Recursion  Factorial  Drilling-down 3/1/2016Implementing Subprograms10

Unwinding Factorial 3/1/2016Implementing Subprograms11  Unwinding

Blocks (loops and conditionals)  Blocks are user-specified local scopes for variables  The lifetime of temp in the above example begins when control enters the block  An advantage of using a local variable like temp is that it cannot interfere with any other variable with the same name 3/1/2016Implementing Subprograms12 if(condition){ int temp; ….. } if(condition){ int temp; ….. } for(int I = 0; I < thresh; i++){ int temp; ….. } for(int I = 0; I < thresh; i++){ int temp; ….. }

Implementing Blocks  Two Methods:  First  Treat blocks as parameter-less subprograms  Use stack as per usual  Second  Allocate in same ARI  Maximum storage required for a block can be statically determined  When two variables are in different blocks they can occupy the same storage 3/1/2016Implementing Subprograms13 void main(){ int x, y, z; while(…){ int a, b, c; … while(…){ int d, e; } while( …){ int f, g; … } … } void main(){ int x, y, z; while(…){ int a, b, c; … while(…){ int d, e; } while( …){ int f, g; … } … }

Implementing Dynamic Scoping  Deep Access: use dynamic chain (already discussed)  Shallow Access: put locals in a central place  One stack for each variable name  Central table with an entry for each variable name 3/1/201614Implementing Subprograms void sub3() { int x, z; x = u + v; … } void sub2() { int w, x; … } void sub1() { int v, w; … } void main() { int v, u; … } void sub3() { int x, z; x = u + v; … } void sub2() { int w, x; … } void sub1() { int v, w; … } void main() { int v, u; … } sub1sub2 sub1sub3sub1 main sub2sub3sub1 uvxzw main calls sub1 sub1 calls sub1 sub1 calls sub2 sub2 calls sub3 main calls sub1 sub1 calls sub1 sub1 calls sub2 sub2 calls sub3

The end 3/1/201615Implementing Subprograms