CS 334
|
Avoid second-class types.
Ex. in Pascal: Restrictions on return values of functions, lack of procedure variables, etc.
ML comes much closer to satisfying.
Modern tendency to strengthen static typing and avoid implicit holes in types system.
- usually explicit (dangerous ) means for bypassing types system, if desired
Try to push as many errors to compile time as possible by:
Important direction of current research in computer science:
Provide type safety, but increase flexibility.
Important progress over last 20 years:
Polymorphism, ADT's, Subtyping & other aspects of object-oriented languages.
Varies between languages.
Pascal: primitive (integer, real, char, boolean), sets, pointers
ML: primitive, records, tuples, lists, function abstractions, ref's to vbles.
Examine how variables allocated and lifetime.
E.g. Procedures, functions, and blocks (from ALGOL 60 & C, like parameterless procedures located in-line.)
Program unit represented during execution by unit instance, composed of code segment and activation record (gives info on parameters and local variables, and where to return after execution).
Activation Record Structure:
How is procedure call made?
All storage (local and global) known at translation time (hence static).
Activation records can be associated with each code segment.
Structure:
Global info shared via common statement:
COMMON/NAME1/A,B,S(25)Statement must occur in all units wishing to share information. Name of the block must be identical, though can give different names to variables. (Gives rise to holes in typing) Identifiers are matched in order w/ no checking of types across unit boundaries.
Space for all common blocks allocated and available globally.
Procedure call and return straightforward
Activation record pushed onto stack each time there is a procedure call.
Popped off after return.
Ex.
Program main; type array_type = array [1..10] of real; var a : integer; b : array_type; Procedure x (var c : integer; d : array_type); var e : array_type; procedure y (f : array_type); var g : integer; begin : z(a+c); : end; {y} begin {x} : ..... := b[6]....... y(e); : end; {x} Procedure z (h : integer); var a : array_type; begin : x (h,a); : end; begin {main} : x (a,b); : end. {main}
Draw static scopes.
Dynamic calling sequences:
Main => x => y => z => x => y ....Look at picture of run-time stack after x calls y 2nd time.
How do we get reference to b in x? to a in y? Where can these variables be held?
Dynamic link (called control link in text) provides pointer to beginning (usually) of caller's activation record.
Static link provides pointer to beginning of activation record of statically containing program unit.
How do find location of variable? Easy in FORTRAN! Not here!
Must keep track of the static nesting level of each variable and procedure.
When access vble or procedure, subtract static nesting level of definition from static nesting level of the definition.
Tells how far down static chain to environment of definition.
Example:
Name Level Name Level main 0 y 2 a 1 f 3 b 1 g 3 x 1 z 1 c 2 h 2 d 2 a 2 e 2Notes:
Sol'n: Piece of cake - each activation record for a given procedure is identical in size and location of info. Pascal.
E.g. x : Array [m..n] of integer;
often called semi-dynamic
Sol'n: Space for vble descriptors allocated at fixed offset.
Location of local vbles and parameters may have to be calculated from this info at each invocation (e.g., store starting location on stack and use indirection).
Sol'n: Separate data area called heap is required.
Lifetime of data independent of lifetime of calling unit.
Allocates and deallocates new space when necessary.
Complicated as size of blocks needed varies.
Requires careful handling to preserve space.
Stack and heap space do not overlap during execution.
Come back and talk about management of heap later!
(Notice difference btn 1, 2, & 3 is binding time!)