Implement Subprograms Flashcards
Subroutine linkage
Subroutine call and return operations
Implementation of subroutines must be based on ….
Semantics of the subroutine linkage
Semantics of a subroutine call (4)
(1) save the execution status of the current Program unit
(2) pass the Parameters
(3) pass the return address to the callee
(4) transfer control to the callee
Subroutine return:
If there are pass-by-value-result or out-mode parameters, the current values of those parameters are move to …
The corresponding actual parameters
Subroutine return:
Move the return value to…
A place accessible to the caller
Subroutine return:
Execution status of the caller is …
Returned
Subroutine return:
Control is transferred …
Back to the caller
Storage:
Call and return actions require storage for … (5)
Status information about the caller, parameters, return address, return value for functions, and local variables
“Simple” subroutines are
Those that cannot be nested and all local variables are static.
A simple subroutine consists of two parts: (give def for each to/size)
Code and data
Code: constant (instruction space)
Data: can change when the subroutine is executed (data space)
Both have fixed size
Activation record is
The format or layout of the data part because the data is relevant to an activation, execution, of the subroutine
The formation of an activation record is
Static
An activation record instance is a
Concrete example of an activation record corresponding to one execution
An activation record for a simple subroutine
What does the stack look like, size, allocation type, side note
Local var
Params
Return Add
Fixed size (statically allocated)
Could be attached to the code part of the sub Program
When each programming unit is compiling is it
Machine code along with a list of references to external sub programs are written to a file
A linker is called for …
MAIN to create an executable program
What does a linker do? (4)
Is a part of the OS
Also called loader, linker/loader, loader, link editor
Finds and loads all referenced subroutines including code and activation records into memory
Sets the target addresses of calls to those subroutines entry addresses
Call stack is
A stack data structure that stores information about the active subroutines of a Program
Also known as execution stack, control stack, runtime stack, machine stack.
Frowns downward in memory towards lower addresses and shrinks upwards
The activation record on stack is also called
Stack frame
stack pointer (sp) points to
The frame top
Frame pointer (to) points
To the frame base
Important advantage of stack dynamic local variables
Support for recursion
Comple activation record stack contents
Local variables Parameters Dynamic link Return address Stacktop
Dynamic link (control links) =
Previous sp
What is dynamic link used for?
In destruction of the current activation record instance when the procedure completes its execution
What does the dynamic link restore?
The sp in the previous frame (caller)
What is a collection of dynamic links in the stack at a given time called? And what do the represent?
Called dynamic chain (call chain)
Represents the dynamic history of how execution got to its current position
What is the purpose of a dynamic link?
Required when there are allocations from the stack by a subroutine by one it’s activation record (temporaries)
Access biblical variables in dynamic scoped languages
Local scalar variables are bound to
Storage within an activation record instance
Local variables that are structures are sometimes allocate elsewhere and only leave their …
Descriptors and a pointer to the storage as a part of their activation record
Recursion adds the possibility of multiple …
Simultaneous activations of a subroutine at a given time with atleast one call from outside the subroutine and one or more recursive calls.
Each recursion activation requires
It’s own activation record instance
What static scoped languages use stack dynamic local variables and allow subroutine to be nested?
Fortran, ada, Python, JavaScript
Finding access to non local variables in nested subroutines of static scoped Lang but stack dynamic local variables
(1) find activation record instance on the stack where variable is allocation
(2) local-offset of the variables to access it
Local offset describes the …
Offset from the beginning/bottom of an activation record
Finding activation record instance:
Static chaining (3)
(1) new ptr (static link aka static scope pointer/access links) point to bottom of activation record instance of the static parent
(2) used to access non local varicela
(3) static link appears below parameters on activation record
Activation record instance with static link stack
Local variables Parameters Dynamic link Static link Return address
A static chain is a chain of
Static links that connect the activation record instances of all static ancestors for an executing subroutine
Static chain can be used to implement
No local variable access
Static_depth and nesting_depth difference
Static depth: how deeply it is nested in the outermost scope for a non local reference
Static depth of a variable definition is the nesting_depth/chain_depth of the reference