SlideShare a Scribd company logo
1 of 31
Download to read offline
Chapter 9: Implementing Subprograms 
Principles of Programming Languages
Contents 
•Implementing “Simple” Subprograms 
•Implementing Subprograms with Stack- Dynamic Local Variables 
•Nested Subprograms 
•Blocks 
•Implementing Dynamic Scoping
“Simple” Subprograms 
•No nested subprograms 
•All local variables are static 
–No recursion
•Subprogram linkage: The subprogram call and return operations of a language 
•Actions associated with a subprogram call 
1.Save the execution status of current program unit 
2.Pass the parameters 
3.Pass the return address to the callee 
4.Transfer control to the callee 
“Simple” Subprograms: Calls and Returns
“Simple” Subprograms: Calls and Returns 
•Actions of a subprogram return: 
1.If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters 
2.If it is a function, functional value is moved to a place accessible to the caller 
3.Restore the execution status of the caller 
4.Transfer control back to the caller 
•What storage does a subprogram call and return need?
Activation Record 
•Activation record: the layout of noncode part of a subprogram 
–Can change when the subprogram is executed 
–Its layout is static
Code and Activation Records 
•Can be statically allocated 
•Activation records sometimes are attached to their code segment
Subprograms with Stack-Dynamic Local Variables 
•More complex 
–The compiler must generate code to cause implicit allocation and de-allocation of local variables 
–Recursion must be supported 
•There can be more than one instance of a subprogram at a given time, one from outside call, and one or more recursive calls 
•Each activation requires its own copy of the formal parameters and the dynamically allocated local variables, along with the return address.
•The activation record format is static, but its size may be dynamic (Ada’s array) 
New Activation Record 
•An activation record instance is dynamically created when a subprogram is called 
•Some fields are placed by the caller
New Activation Record 
•Return address usually consists of a pointer to the instruction after subprogram call statements 
•Dynamic link is the pointer to the top of the activation record instance of the caller 
–Static-scoped languages use this link in destruction of the current activation record 
–The stack top is set to the value of the old dynamic link
New Activation Record 
•Parameters are the values or addresses provided by the caller 
•Local variables 
–Scalar variables are bound to storage within an activation record instance 
–Structured variables are sometimes allocated elsewhere, only their descriptors and a pointer to that storage are part of activation record
An Example: C Function 
void sub(float total, int part) 
{ 
int list[4]; 
float sum; 
. . . 
}
Run-Time Stack 
•Subprogram last called is the first to complete 
•Create instances of these activation records on a stack: run-time stack 
•Environment pointer (EP) is required to access parameters and local variables during the execution of a subprogram
Sub2 
Sub1 
Main 
Environment Pointer Illustration 
•Only saved versions of EP are stored in the activation record instances 
•Saved versions are stored with execution status information 
EP 
EP 
EP
Return (to fun2) 
Dynamic link 
Parameter 
Return (to fun1) 
Dynamic link 
Parameter 
Local 
An Example Without Recursion 
void fun1(float r) { 
int s, t; 
... 
fun2(s); 
... 
} 
void fun2(int x) { 
int y; 
... 
fun3(y); 
... 
} 
void fun3(int q) { 
... 
} 
void main() { 
float p; 
... 
fun1(p); 
... 
} 
Return (to main) 
Dynamic link 
Parameter 
Local 
Local 
Local 
p 
s 
t 
r 
1 
2 
3 
x 
y 
4 
q 
Top 
Top 
Top 
Top 
main 
fun1 
fun2 
fun3 
Dynamic chain
Dynamic Chain and Local Offset 
•The collection of dynamic links in the stack at a given time is called the dynamic chain, or call chain 
•Local variables can be accessed by their offset from the beginning of the activation record. This offset is called the local_offset 
•The local_offset of a local variable can be determined by the compiler at compile time
Return (to factorial) 
Dynamic link 
Parameter 
Functional value 
An Example With Recursion 
int factorial (int n) { 
if (n <= 1) return 1; 
else return 
(n * factorial(n - 1)); 
} 
void main() { 
int value; 
value = factorial(3); 
} 
Return (to factorial) 
Dynamic link 
Parameter 
Functional value 
Return (to main) 
Dynamic link 
Parameter 
Functional value 
Local 
value 
n 
Top 
Top 
Top 
Top 
main 
1st factorial 
2nd factorial 
3rd factorial 
? 
? 
3 
? 
2 
? 
1 
n 
n 
1 
2 
6 
6
Nested Subprograms 
•Some non-C-based static-scoped languages use stack-dynamic local variables and allow subprograms to be nested 
–Fortran 95, Ada, Python, JavaScript 
•All variables that can be non-locally accessed reside in some activation record instance in the stack 
•The process of locating a non-local reference: 
1.Find the correct activation record instance 
2.Determine the correct offset within that activation record instance
Locating a Non-local Reference 
•Finding the offset is easy 
•Finding the correct activation record instance 
–Static semantic rules guarantee that all non-local variables that can be referenced have been allocated in some activation record instance that is on the stack when the reference is made
Static Chains 
•A new pointer, static link, is added to the activation record 
•Static link point from an activation record to the activation record of its static parent 
•Used for accesses to nonlocal variables 
•A static chain is a chain of static links that connects certain activation record instances 
•The static chain from an activation record instance connects it to all of its static ancestors 
•(chain_offset, local_offset)
program Main_2; 
var X : integer; 
procedure Bigsub; 
var A, B, C : integer; 
procedure Sub1; 
var A, D : integer; 
begin 
A := B + C; <-----------1 
end; 
procedure Sub2(X : integer); 
var B, E : integer; 
procedure Sub3; 
var C, E : integer; 
begin 
Sub1; 
E := B + A: <--------2 
end; 
begin 
Sub3; 
A := D + E; <-----------3 
end; 
begin 
Sub2(7); 
end; 
begin 
Bigsub; 
end;
Static Chain Maintenance 
•Subprogram return: no problem 
•Subprogram call: 
–Consider subprogram declaration as variable declaration so that “who declares who” is known statically 
–Calculate the nested_depth from caller to callee’s “father” 
–Trace nested_depth from caller, then connect with callee
Blocks 
•Blocks are user-specified local scopes for variables 
•C: 
{ int temp; 
temp = list[upper]; 
list[upper] = list[lower]; 
list[lower] = temp; 
} 
•The lifetime of temp in the above example begins when control enters the block and ends when exits 
•An advantage of using a local variable is that it cannot interfere with any other variable with the same name
Implementing Blocks 
1.Treat blocks as parameter-less subprograms that are always called from the same location 
–Every block has an activation record; an instance is created every time the block is executed 
–Then, static-chain process is used 
2.Since the maximum storage required for a block can be statically determined, this amount of space can be allocated after the local variables in the activation record
Implementing Blocks 
void main() { 
int x, y, z; 
while (...) { 
int a, b, c; 
... 
while (...) { 
int d, e; 
... 
} 
} 
while (...) { 
int f, g; 
... 
} 
... 
} 
d 
e 
Activation 
record instance 
for 
main 
x 
y 
z 
b 
c 
a 
f 
g 
Block 
variables 
Local 
variables
Implementing Dynamic Scoping 
•There are two distinct ways: deep access and shallow access 
•Don’t be confused with deep and shallow binding in Chapter 8 
•Deep Access: non-local references are found by searching the activation record instances on the dynamic chain
void sub3() { 
int x, z; 
x = u + v; 
... 
} 
void sub2() { 
int w, x; 
... 
} 
void sub1() { 
int v, w; 
... 
} 
void main() { 
int v, u; 
... 
} 
main  sub2  sub1  sub2  sub3
Shallow Access 
•Shallow Access: First option 
–Local variables are not stored in the activation records of those program 
–There is a central table with many stacks 
–One stack for each variable name 
•Other options for central table: 
1.One stack stores all saved objects, the top one will be accessible 
2.Variables are stored in activation records, but the latest value is stored in a single cells central table
void sub3() { 
int x, z; 
x = u + v; 
... 
} 
void sub2() { 
int w, x; 
... 
} 
void sub1() { 
int v, w; 
... 
} 
void main() { 
int v, u; 
... 
} 
main  sub2  sub1  sub2  sub3
Summary 
•Subprogram linkage semantics requires many action by the implementation 
•Simple subprograms have relatively basic actions 
•Stack-dynamic languages are more complex 
•Subprograms with stack-dynamic local variables and nested subprograms have two components 
–actual code 
–activation record
Summary (continued) 
•Activation record instances contain formal parameters and local variables among other things 
•Static chains are the primary method of implementing accesses to non-local variables in static-scoped languages with nested subprograms 
•Access to non-local variables in dynamic-scoped languages can be implemented by use of the dynamic chain or thru some central variable table method

More Related Content

What's hot

Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)Om Ganesh
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresSelvaraj Seerangan
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
UART Communication
UART CommunicationUART Communication
UART Communicationdattatraya1
 
Structure in c language
Structure in c languageStructure in c language
Structure in c languagesangrampatil81
 
Pointers in c
Pointers in cPointers in c
Pointers in cMohd Arif
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in CColin
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Python programming ppt.pptx
Python programming ppt.pptxPython programming ppt.pptx
Python programming ppt.pptxnagendrasai12
 
Finalize() method
Finalize() methodFinalize() method
Finalize() methodJadavsejal
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in CUri Dekel
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in Crgnikate
 

What's hot (20)

Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and StructuresDynamic Memory Allocation, Pointers and Functions, Pointers and Structures
Dynamic Memory Allocation, Pointers and Functions, Pointers and Structures
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
UART Communication
UART CommunicationUART Communication
UART Communication
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Packages and Datastructures - Python
Packages and Datastructures - PythonPackages and Datastructures - Python
Packages and Datastructures - Python
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Moving Average Filter in C
Moving Average Filter in CMoving Average Filter in C
Moving Average Filter in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Basics of python
Basics of pythonBasics of python
Basics of python
 
Python programming ppt.pptx
Python programming ppt.pptxPython programming ppt.pptx
Python programming ppt.pptx
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 

Viewers also liked

Memory allocation
Memory allocationMemory allocation
Memory allocationsanya6900
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and DynamicSneh Pahilwani
 
How to build a news website use CMS wordpress
How to build a news website use CMS wordpressHow to build a news website use CMS wordpress
How to build a news website use CMS wordpressbaran19901990
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentationbaran19901990
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sưbaran19901990
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apachebaran19901990
 
Chapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsChapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsSaeed Iqbal
 
Computer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 clComputer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 clSaumya Sahu
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prologbaran19901990
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programsharman kaur
 
Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notesSiva Ayyakutti
 

Viewers also liked (20)

08 subprograms
08 subprograms08 subprograms
08 subprograms
 
Memory allocation
Memory allocationMemory allocation
Memory allocation
 
Scope - Static and Dynamic
Scope - Static and DynamicScope - Static and Dynamic
Scope - Static and Dynamic
 
Hadoop
HadoopHadoop
Hadoop
 
How to build a news website use CMS wordpress
How to build a news website use CMS wordpressHow to build a news website use CMS wordpress
How to build a news website use CMS wordpress
 
Introduction to HBase
Introduction to HBaseIntroduction to HBase
Introduction to HBase
 
Untitled Presentation
Untitled PresentationUntitled Presentation
Untitled Presentation
 
Datatype
DatatypeDatatype
Datatype
 
Chapter2
Chapter2Chapter2
Chapter2
 
Nhập môn công tác kỹ sư
Nhập môn công tác kỹ sưNhập môn công tác kỹ sư
Nhập môn công tác kỹ sư
 
Config websocket on apache
Config websocket on apacheConfig websocket on apache
Config websocket on apache
 
Control structure
Control structureControl structure
Control structure
 
Chapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutionsChapter 9 & chapter 10 solutions
Chapter 9 & chapter 10 solutions
 
Chapter 17 dccn
Chapter 17 dccnChapter 17 dccn
Chapter 17 dccn
 
Computer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 clComputer Fundamentals Chapter 12 cl
Computer Fundamentals Chapter 12 cl
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
Software engineering lecture notes
Software engineering lecture notesSoftware engineering lecture notes
Software engineering lecture notes
 

Similar to 09 implementing+subprograms

-ImplementSubprogram-theory of programming
-ImplementSubprogram-theory of programming-ImplementSubprogram-theory of programming
-ImplementSubprogram-theory of programmingjavariagull777
 
10 implementing subprograms
10 implementing subprograms10 implementing subprograms
10 implementing subprogramsMunawar Ahmed
 
Runtimeenvironment
RuntimeenvironmentRuntimeenvironment
RuntimeenvironmentAnusuya123
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Iffat Anjum
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local namesVarsha Kumar
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfHimanshu883663
 
Journey into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka StreamsJourney into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka StreamsKevin Webber
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxsangeeta borde
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++Mohammad Shaker
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,Ahmad55ali
 

Similar to 09 implementing+subprograms (20)

-ImplementSubprogram-theory of programming
-ImplementSubprogram-theory of programming-ImplementSubprogram-theory of programming
-ImplementSubprogram-theory of programming
 
10 implementing subprograms
10 implementing subprograms10 implementing subprograms
10 implementing subprograms
 
Runtimeenvironment
RuntimeenvironmentRuntimeenvironment
Runtimeenvironment
 
C language
C languageC language
C language
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Functions
FunctionsFunctions
Functions
 
2.0 Stacks.pptx
2.0 Stacks.pptx2.0 Stacks.pptx
2.0 Stacks.pptx
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local names
 
Intermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdfIntermediate code optimization Unit-4.pdf
Intermediate code optimization Unit-4.pdf
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
 
Journey into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka StreamsJourney into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka Streams
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
Functions
FunctionsFunctions
Functions
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Unit 4
Unit 4Unit 4
Unit 4
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,Global variables, sorting static variables,function and arrays,
Global variables, sorting static variables,function and arrays,
 

More from baran19901990

More from baran19901990 (15)

Tìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google MapTìm đường đi xe buýt trong TPHCM bằng Google Map
Tìm đường đi xe buýt trong TPHCM bằng Google Map
 
How to install nginx vs unicorn
How to install nginx vs unicornHow to install nginx vs unicorn
How to install nginx vs unicorn
 
Subprogram
SubprogramSubprogram
Subprogram
 
Lexical
LexicalLexical
Lexical
 
Introduction
IntroductionIntroduction
Introduction
 
07 control+structures
07 control+structures07 control+structures
07 control+structures
 
How to install git on ubuntu
How to install git on ubuntuHow to install git on ubuntu
How to install git on ubuntu
 
Ruby notification
Ruby notificationRuby notification
Ruby notification
 
Rails notification
Rails notificationRails notification
Rails notification
 
Linux notification
Linux notificationLinux notification
Linux notification
 
Lab4
Lab4Lab4
Lab4
 
Lab5
Lab5Lab5
Lab5
 
09
0909
09
 
Báo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạnBáo cáo mô hình quản lý khách sạn
Báo cáo mô hình quản lý khách sạn
 
MDA Framework
MDA FrameworkMDA Framework
MDA Framework
 

Recently uploaded

Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Servicegwenoracqe6
 
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersMoving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersDamian Radcliffe
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Delhi Call girls
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLimonikaupta
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...SUHANI PANDEY
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...tanu pandey
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...SUHANI PANDEY
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...Escorts Call Girls
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...singhpriety023
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.soniya singh
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...SUHANI PANDEY
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge GraphsEleniIlkou
 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...SUHANI PANDEY
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtrahman018755
 
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...SUHANI PANDEY
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.soniya singh
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...SUHANI PANDEY
 

Recently uploaded (20)

Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersMoving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRLLucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
Lucknow ❤CALL GIRL 88759*99948 ❤CALL GIRLS IN Lucknow ESCORT SERVICE❤CALL GIRL
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...Pune Airport ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready...
Pune Airport ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready...
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
Sarola * Female Escorts Service in Pune | 8005736733 Independent Escorts & Da...
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
 

09 implementing+subprograms

  • 1. Chapter 9: Implementing Subprograms Principles of Programming Languages
  • 2. Contents •Implementing “Simple” Subprograms •Implementing Subprograms with Stack- Dynamic Local Variables •Nested Subprograms •Blocks •Implementing Dynamic Scoping
  • 3. “Simple” Subprograms •No nested subprograms •All local variables are static –No recursion
  • 4. •Subprogram linkage: The subprogram call and return operations of a language •Actions associated with a subprogram call 1.Save the execution status of current program unit 2.Pass the parameters 3.Pass the return address to the callee 4.Transfer control to the callee “Simple” Subprograms: Calls and Returns
  • 5. “Simple” Subprograms: Calls and Returns •Actions of a subprogram return: 1.If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters 2.If it is a function, functional value is moved to a place accessible to the caller 3.Restore the execution status of the caller 4.Transfer control back to the caller •What storage does a subprogram call and return need?
  • 6. Activation Record •Activation record: the layout of noncode part of a subprogram –Can change when the subprogram is executed –Its layout is static
  • 7. Code and Activation Records •Can be statically allocated •Activation records sometimes are attached to their code segment
  • 8. Subprograms with Stack-Dynamic Local Variables •More complex –The compiler must generate code to cause implicit allocation and de-allocation of local variables –Recursion must be supported •There can be more than one instance of a subprogram at a given time, one from outside call, and one or more recursive calls •Each activation requires its own copy of the formal parameters and the dynamically allocated local variables, along with the return address.
  • 9. •The activation record format is static, but its size may be dynamic (Ada’s array) New Activation Record •An activation record instance is dynamically created when a subprogram is called •Some fields are placed by the caller
  • 10. New Activation Record •Return address usually consists of a pointer to the instruction after subprogram call statements •Dynamic link is the pointer to the top of the activation record instance of the caller –Static-scoped languages use this link in destruction of the current activation record –The stack top is set to the value of the old dynamic link
  • 11. New Activation Record •Parameters are the values or addresses provided by the caller •Local variables –Scalar variables are bound to storage within an activation record instance –Structured variables are sometimes allocated elsewhere, only their descriptors and a pointer to that storage are part of activation record
  • 12. An Example: C Function void sub(float total, int part) { int list[4]; float sum; . . . }
  • 13. Run-Time Stack •Subprogram last called is the first to complete •Create instances of these activation records on a stack: run-time stack •Environment pointer (EP) is required to access parameters and local variables during the execution of a subprogram
  • 14. Sub2 Sub1 Main Environment Pointer Illustration •Only saved versions of EP are stored in the activation record instances •Saved versions are stored with execution status information EP EP EP
  • 15. Return (to fun2) Dynamic link Parameter Return (to fun1) Dynamic link Parameter Local An Example Without Recursion void fun1(float r) { int s, t; ... fun2(s); ... } void fun2(int x) { int y; ... fun3(y); ... } void fun3(int q) { ... } void main() { float p; ... fun1(p); ... } Return (to main) Dynamic link Parameter Local Local Local p s t r 1 2 3 x y 4 q Top Top Top Top main fun1 fun2 fun3 Dynamic chain
  • 16. Dynamic Chain and Local Offset •The collection of dynamic links in the stack at a given time is called the dynamic chain, or call chain •Local variables can be accessed by their offset from the beginning of the activation record. This offset is called the local_offset •The local_offset of a local variable can be determined by the compiler at compile time
  • 17. Return (to factorial) Dynamic link Parameter Functional value An Example With Recursion int factorial (int n) { if (n <= 1) return 1; else return (n * factorial(n - 1)); } void main() { int value; value = factorial(3); } Return (to factorial) Dynamic link Parameter Functional value Return (to main) Dynamic link Parameter Functional value Local value n Top Top Top Top main 1st factorial 2nd factorial 3rd factorial ? ? 3 ? 2 ? 1 n n 1 2 6 6
  • 18. Nested Subprograms •Some non-C-based static-scoped languages use stack-dynamic local variables and allow subprograms to be nested –Fortran 95, Ada, Python, JavaScript •All variables that can be non-locally accessed reside in some activation record instance in the stack •The process of locating a non-local reference: 1.Find the correct activation record instance 2.Determine the correct offset within that activation record instance
  • 19. Locating a Non-local Reference •Finding the offset is easy •Finding the correct activation record instance –Static semantic rules guarantee that all non-local variables that can be referenced have been allocated in some activation record instance that is on the stack when the reference is made
  • 20. Static Chains •A new pointer, static link, is added to the activation record •Static link point from an activation record to the activation record of its static parent •Used for accesses to nonlocal variables •A static chain is a chain of static links that connects certain activation record instances •The static chain from an activation record instance connects it to all of its static ancestors •(chain_offset, local_offset)
  • 21. program Main_2; var X : integer; procedure Bigsub; var A, B, C : integer; procedure Sub1; var A, D : integer; begin A := B + C; <-----------1 end; procedure Sub2(X : integer); var B, E : integer; procedure Sub3; var C, E : integer; begin Sub1; E := B + A: <--------2 end; begin Sub3; A := D + E; <-----------3 end; begin Sub2(7); end; begin Bigsub; end;
  • 22. Static Chain Maintenance •Subprogram return: no problem •Subprogram call: –Consider subprogram declaration as variable declaration so that “who declares who” is known statically –Calculate the nested_depth from caller to callee’s “father” –Trace nested_depth from caller, then connect with callee
  • 23. Blocks •Blocks are user-specified local scopes for variables •C: { int temp; temp = list[upper]; list[upper] = list[lower]; list[lower] = temp; } •The lifetime of temp in the above example begins when control enters the block and ends when exits •An advantage of using a local variable is that it cannot interfere with any other variable with the same name
  • 24. Implementing Blocks 1.Treat blocks as parameter-less subprograms that are always called from the same location –Every block has an activation record; an instance is created every time the block is executed –Then, static-chain process is used 2.Since the maximum storage required for a block can be statically determined, this amount of space can be allocated after the local variables in the activation record
  • 25. Implementing Blocks void main() { int x, y, z; while (...) { int a, b, c; ... while (...) { int d, e; ... } } while (...) { int f, g; ... } ... } d e Activation record instance for main x y z b c a f g Block variables Local variables
  • 26. Implementing Dynamic Scoping •There are two distinct ways: deep access and shallow access •Don’t be confused with deep and shallow binding in Chapter 8 •Deep Access: non-local references are found by searching the activation record instances on the dynamic chain
  • 27. void sub3() { int x, z; x = u + v; ... } void sub2() { int w, x; ... } void sub1() { int v, w; ... } void main() { int v, u; ... } main  sub2  sub1  sub2  sub3
  • 28. Shallow Access •Shallow Access: First option –Local variables are not stored in the activation records of those program –There is a central table with many stacks –One stack for each variable name •Other options for central table: 1.One stack stores all saved objects, the top one will be accessible 2.Variables are stored in activation records, but the latest value is stored in a single cells central table
  • 29. void sub3() { int x, z; x = u + v; ... } void sub2() { int w, x; ... } void sub1() { int v, w; ... } void main() { int v, u; ... } main  sub2  sub1  sub2  sub3
  • 30. Summary •Subprogram linkage semantics requires many action by the implementation •Simple subprograms have relatively basic actions •Stack-dynamic languages are more complex •Subprograms with stack-dynamic local variables and nested subprograms have two components –actual code –activation record
  • 31. Summary (continued) •Activation record instances contain formal parameters and local variables among other things •Static chains are the primary method of implementing accesses to non-local variables in static-scoped languages with nested subprograms •Access to non-local variables in dynamic-scoped languages can be implemented by use of the dynamic chain or thru some central variable table method