Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter Ten: Implementing Subprograms Lesson 10. Implementing?  Previous lesson: parameter passing  In, out, inout  By value  By reference  Passing."— Presentation transcript:

1 Chapter Ten: Implementing Subprograms Lesson 10

2 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

3 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

4 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

5 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

6 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

7 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

8 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?

9 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

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

11 Unwinding Factorial 3/1/2016Implementing Subprograms11  Unwinding

12 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; ….. }

13 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; … } … }

14 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

15 The end 3/1/201615Implementing Subprograms


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

Similar presentations


Ads by Google