25
CSC3315 (Spring 2008) 1 CSC 3315 CSC 3315 Subprograms Subprograms Hamid Harroud Hamid Harroud School of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn University University http://www.aui.ma/~H.Harroud/csc3315/

CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315

Embed Size (px)

Citation preview

CSC3315 (Spring 2008) 1

CSC 3315CSC 3315SubprogramsSubprograms

Hamid HarroudHamid HarroudSchool of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn

UniversityUniversityhttp://www.aui.ma/~H.Harroud/csc3315/

1-2

Fundamentals of Fundamentals of SubprogramsSubprograms

Each subprogram has a single entry point The calling program is suspended during

execution of the called subprogram Control always returns to the caller when the

called subprogram’s execution terminates

1-3

Basic DefinitionsBasic Definitions A subprogram definition describes the interface to and the

actions of the subprogram abstraction A subprogram call is an explicit request that the subprogram

be executed A subprogram header is the first part of the definition,

including the name, the kind of subprogram, and the formal parameters

The parameter profile (aka signature) of a subprogram is the number, order, and types of its parameters

The protocol is a subprogram’s parameter profile and, if it is a function, its return type

1-4

Basic Definitions Basic Definitions (continued)(continued)

Function declarations in C and C++ are often called prototypes

A subprogram declaration provides the protocol, but not the body, of the subprogram

A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram

An actual parameter represents a value or address used in the subprogram call statement

1-5

Local Referencing Local Referencing EnvironmentsEnvironments

Local variables can be stack-dynamic (bound to storage) Advantages

Support for recursion Storage for locals is shared among some subprograms

Disadvantages Allocation/de-allocation, initialization time Indirect addressing Subprograms cannot be history sensitive

Local variables can be static More efficient (no indirection) No run-time overhead Cannot support recursion

1-6

Type Checking ParametersType Checking Parameters Considered very important for reliability FORTRAN 77 and original C: none Pascal, FORTRAN 90, Java, and Ada: it is

always required Relatively new languages Perl, JavaScript,

and PHP do not require type checking

1-7

The General Semantics of The General Semantics of Calls and ReturnsCalls and Returns The subprogram call and return operations of a

language are together called its subprogram linkage

A subprogram call has numerous actions associated with it Parameter passing methods Static local variables Execution status of calling program Transfer of control Subprogram nesting

1-8

Implementing “Simple” Implementing “Simple” Subprograms: Call Subprograms: Call SemanticsSemantics

Save the execution status of the caller Carry out the parameter-passing process Pass the return address to the callee Transfer control to the callee

1-9

Implementing “Simple” Implementing “Simple” Subprograms: Return SemanticsSubprograms: Return Semantics

If pass-by-value-result parameters are used, move the current values of those parameters to their corresponding actual parameters

If it is a function, move the functional value to a place the caller can get it

Restore the execution status of the caller Transfer control back to the caller

1-10

Implementing “Simple” Implementing “Simple” Subprograms: PartsSubprograms: Parts

Two separate parts: the actual code and the noncode part (local variables and data that can change)

The format, or layout, of the noncode part of an executing subprogram is called an activation record

An activation record instance is a concrete example of an activation record (the collection of data for a particular subprogram activation)

1-11

An Activation Record for An Activation Record for “Simple” Subprograms“Simple” Subprograms

1-12

Code and Code and Activation Records Activation Records of a Program with of a Program with “Simple” “Simple” SubprogramsSubprograms

1-13

Implementing Subprograms Implementing Subprograms with Stack-Dynamic Local with Stack-Dynamic Local VariablesVariables

More complex activation record The compiler must generate code to cause implicit

allocation and de-allocation of local variables Recursion must be supported (adds the possibility

of multiple simultaneous activations of a subprogram)

1-14

Typical Activation Record for a Typical Activation Record for a Language with Stack-Dynamic Local Language with Stack-Dynamic Local VariablesVariables

1-15

Implementing Subprograms with Stack-Implementing Subprograms with Stack-Dynamic Local Variables: Activation Dynamic Local Variables: Activation RecordRecord

The activation record format is static, but its size may be dynamic

The dynamic link points to the top of an instance of the activation record of the caller

An activation record instance is dynamically created when a subprogram is called

Run-time stack

1-16

An Example Without An Example Without RecursionRecursion

void A(int x) {int y;...C(y);...

}void B(float r) {

int s, t;...A(s);...

}void C(int q) {

...}void main() {

float p;...B(p);...

}

main calls BB calls AA calls C

1-17

An Example Without An Example Without RecursionRecursion

1-18

Dynamic Chain and Local Dynamic Chain and Local OffsetOffset

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

1-19

Static ScopingStatic Scoping A static chain is a chain of static links that

connects certain activation record instances The static link in an activation record instance

for subprogram A points to one of the activation record instances of A's static parent

The static chain from an activation record instance connects it to all of its static ancestors

1-20

Example Pascal ProgramExample Pascal Programprogram MAIN_2; var X : integer; procedure BIGSUB; var A, B, C : integer; procedure SUB1; var A, D : integer; begin { SUB1 } A := B + C; <-----------------------1 end; { SUB1 } procedure SUB2(X : integer); var B, E : integer; procedure SUB3; var C, E : integer; begin { SUB3 } SUB1; E := B + A: <--------------------2 end; { SUB3 } begin { SUB2 } SUB3; A := D + E; <-----------------------3 end; { SUB2 } begin { BIGSUB } SUB2(7); end; { BIGSUB } begin BIGSUB; end; { MAIN_2 }

1-21

Example Pascal Program Example Pascal Program (continued)(continued)

Call sequence for MAIN_2

MAIN_2 calls BIGSUB BIGSUB calls SUB2 SUB2 calls SUB3 SUB3 calls SUB1

1-22

Stack Stack Contents at Contents at Position 1Position 1

1-23

DisplaysDisplays An alternative to static chains Static links are stored in a single array called

a display The contents of the display at any given time

is a list of addresses of the accessible activation record instances

1-24

SummarySummary 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

1-25

Summary (continued)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