opisdfjsfdajk Flashcards
Definition of a Subprogram
Interface to and the actions of the subprogram abstraction
Subprograms in Major Languages
Executable: Python
Nonexecutable: Most other Languages
Ruby: Can be called without an object, like a function
Lua: anonymous
Subprogram call
Explicit request that the subprogram be executed
Parts of a subprogram
Header: First Part, includes name, kind of subprogram, and formal parameters
Profile (Signature): Number, order, types of its parameter
Protocol: Parameter profile and its functions, return type
Formal Parameter
Dummy variable listed in subprogram
Formal Parameters in Major Languages
C++, Python, Ruby PHP: can have default values
Categories of Subprograms
Procedure: Collection of statements that define parameterized computations
Functions: Resemble procedure but are modeled on math functions
Dynamic Stack Local Vars
ADV: Support for recursion; Storage for locals is shared among some subprograms
DIS: Allocation/Deallocation, initialization time, indirect addressing, Subprograms cannot be history-sensitive
Static Local Vars
ADV: Opposite of Stack Dynamic
DIS:
Static/Stack Dynamic in major languages
Stack Dynamic: Locals of most languages, Methods in Java, C++, Python, C#
Static: can be declared in C based languages
Semantic Models
In mode: actual –> formal
Out mode: formal –>actual
Inout Mode: actual <–> formal
Implementation Models
(Pass-by- )
value (In mode)
result (out mode)
value-result (inout mode)
reference (inout mode)
name (inout mode)
Parameter passing methods of major Languages
C: Pass by Value/reference
C++: Pass-by-reference
Java: (Non-object) Pass by value (objects) pass by reference
Fortran 95+: in, out, or inout mode
C#: Pass-by-value, reference can be called with ref
Python and Ruby: Pass by assignment
Type Checking
Non in Fortran and OG C
Required in Java and Pascal
Ansi C, C++ is a choice
Not required in Perl, Javascript, PHP
Not possible: Python Ruby
Shallow Binding
Environment of the call statement that enacts the passed subprogram
Dynamic-Scoping
Deep Binding
Enviorment of the definition of the passed subprogram
Static-Scoped
Types of Return Values in Major languages
C: allows any type except arrays and functions
C++: allows user-defined types
Java and C#: Can return any type
Python and Ruby: Can be returned
Coroutines
Subprogram that has multiple entries and controls them itself
Coroutine call named resume, repeated resume eachother
Overloaded subprogram
One that has the same name as another subprogram in same referencing enviroment
Overloaded Subprograms in Major Languages
C++, Java, C#, ada: Predefined overloaded subprograms
Overload in Java
Must differ in # of parameters, Data types, Sequence
Type Promotion:
Byte -> short->int->long->float->double
Overload in Python
Every operator can be overloaded
Subprogram Linkage
Subprogram call and return operations of a language are together
Semantics of calls to a subprogram
Parameter passing methods
Stack-Dynamic allocation of local vars
Save the execution status of calling programs
Transfer of control and arrange for the return
If subprogram nesting is supported, access to nonlocal vars must be arranged
General Semantics of subprogram returns
In, inout mode parameters must have their values returned
Deallocation of stack-dynamic locals
Restore the execution status
Return control to the caller
Call Semantics
Save the execution status of caller
Pass the parameters
pass the return address to the called
Transfer control to the called
Return Semantics
Pass-by-value/out mode parameters, move the value of those to their corresponding actual parameters
If function, move the functional value to a place the caller can reach
Restore the execution status of caller
Transfer control back to caller
Activation Record
Format, or layout, of the non-code part
Activation Record Instance
Concrete example of an activation record
Revised Semantics Call
Caller Actions:
Create an ARI
Save the execution status of current program unit
Copute and pass the parameters
Pass the return address to be called
Transfer control to the called
Prologue actions of the called:
Save the old EP in the stack as the dynamic link and create the new value
Allocate local variables
Dynamic Chain
Collection of dynamic links in the stack at a given time
Local_offset
Local variable can be accessed by their offset from the beginning of the activiation record
Nested Subprograms in Major Languages
Non-C-Based static-scoped languages use stack dynamic local variables and allow subprograms to be nested
Locating a non local reference
Finding offset is easy
Finding the correct ARI
Static chain
A chain of static links that connects certain ARI
Static Link
an ARI for Subprogram A points to one of the ARIs of A static parents
Static Depth
an integer associated with a static scope whose value is the depth of nesting of that scope
Chain_offset or nesting_depth
Difference between the static_depth of the reference and what of the scope when it is decalred
Deep access
Non local references are found by searching the ARIs on the dynamic chain
Shallow Access
Put locals in a central place