16
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Embed Size (px)

Citation preview

Page 1: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Chapter 5:Programming Languages

and Constructs by Ravi Sethi

Activation Records

Dolores Zage

Page 2: 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

Page 3: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Mapping or Binding Times

Compile Activation Run

Page 4: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

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);

}

Page 5: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Activation Time

Binding of declarations to locations is done at activation time - this is important in recursive procedures

LocationName occurrence

Declaration Value

scope activation state

Page 6: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Run Time

The binding of locations to values is done dynamically at run time and can be changed by assignments

Page 7: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

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

Page 8: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Activation trees

Nodes in the tree represent activations activation trees and the structure chart are

closely related.

Page 9: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Elements of an Activation Record

Control link

Access link

Saved state

Parameters

Function result

Local variables

Points to the activation record of the caller

Static link, used to implement lexically scoped languages

Page 10: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

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

Page 11: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

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

Page 12: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

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

Page 13: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Memory Layout

Code

Static global data

Stack local data

Heap dynamic data

Page 14: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Dangling Pointers

A pointer that refers to storage that is being used for another purpose

(see page 186 for example)

Page 15: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

Displays

Optimization technique for obtaining faster access to nonlocals

array of pointers to activation records, indexed by lexical nesting depth

Page 16: Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage

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