220 likes | 704 Views
Chapter 5: Programming Languages and Constructs by Ravi Sethi. Activation Records Dolores Zage. Activation Records. Each execution of the body is called an activation of the body
E N D
Chapter 5:Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage
Activation Records • Each execution of the body is called an activation of the body • associated with each activation of a body is storage for the variables declared in the body called an activation record
Mapping or Binding Times • Compile • Activation • Run
Compile Time • Binding of name occurrences to declarations is defined in terms of lexical context #include <stdio.h> int i; void main() { int i; for(i=0; i<4; i++) printf(“I is %d\n”, i); }
Activation Time • Binding of declarations to locations is done at activation time - this is important in recursive procedures Location scope activation state Name occurrence Declaration Value
Run Time • The binding of locations to values is done dynamically at run time and can be changed by assignments
Control Flow Between Activations • In a sequential language, one procedure at at a time • P calls Q : P is put on hold, Q gets activated and when finishes execution resumes with P • coroutines - suspend execution, return back to caller, and then resume execution later from where they were suspended example the classic producer-consumer application
Activation trees • Nodes in the tree represent activations • activation trees and the structure chart are closely related.
Elements of an Activation Record Points to the activation record of the caller Control link Static link, used to implement lexically scoped languages Access link Saved state Parameters Function result Local variables
Results can be different under lexical and dynamic scope • Lexical - pointer to the block that contains declaration • dynamic - follow the control links for the nearest binding • See program 15.4 again • produces ‘LL’ under lexical • produces ‘LD’ under dynamic
Heap • Storage spot for activation records • the records stay here as long as they are needed • pieces are allocated and freed in some relatively unstructured manner • problems of storage allocation, recovery, compaction and reuse may be severe • garbage collection - technique to reclaim storage that is no longer needed
Stack • Activation records held in a stack • storage reused efficiently • storage is allocated when activation begins and released when ends • stack imposes restrictions on language design - functions as parameters
Memory Layout Static global data Stack local data Code Heap dynamic data
Dangling Pointers • A pointer that refers to storage that is being used for another purpose • (see page 186 for example)
Displays • Optimization technique for obtaining faster access to nonlocals • array of pointers to activation records, indexed by lexical nesting depth
Summary of Procedures • Before there were programming languages there were procedures • giving a name to a piece of code • when called->activated • when activated where does it get its information • from names-> declarations->storage locations-> values