Docsity

Understanding Activation Records in Modern Programming: Memory Locations for Variables, Slides of Advanced Computer Programming

Partial preview of the text

Download Understanding Activation Records in Modern Programming: Memory Locations for Variables and more Slides Advanced Computer Programming in PDF only on Docsity!

Memory Locations For Variables

A Binding Question

 Variables are bound (dynamically) to values

 Those values must be stored somewhere

 Therefore, variables must somehow be bound to memory locations

 How?

Outline

 Activation records

 Static allocation of activation records

 Stacks of activation records

 Handling nested function definitions

 Functions as parameters

 Long-lived activation records

Function Activations

 The lifetime of one execution of a function, from call to corresponding return, is called an activation of the function

 When each activation has its own binding of a variable to a memory locations, it is an activation-specific variable

 (Also called dynamic or automatic )

Block Activations

 For block constructs that contain code, we can speak of an activation of the block  The lifetime of one execution of the block  A variable might be specific to an activation of a particular block within a function:

Chapter Twelve Modern Programming Languages, 2nd ed. 7

fun fact n = if (n=0) then 1 else let val b = fact (n-1) in nb end;*

Docsity.com

Other Lifetimes For Variables

 Most imperative languages have a way to declare a variable that is bound to a single memory location for the entire runtime  Obvious binding solution: static allocation (classically, the loader allocates these)

Chapter Twelve Modern Programming Languages, 2nd ed. 8

int count = 0; int nextcount() { count = count + 1; return count; } Docsity.com

Other Lifetimes For Variables

 Object-oriented languages use variables whose lifetimes are associated with object lifetimes

 Some languages have variables whose values are persistent: they last across multiple executions of the program

 Today, we will focus on activation-specific variables

Activation Records

 Language implementations usually allocate all the activation-specific variables of a function together as an activation record

 The activation record also contains other activation-specific data, such as

  • Return address: where to go in the program when this activation returns
  • Link to caller’s activation record: more about this in a moment

Outline

 Activation-specific variables

 Static allocation of activation records

 Stacks of activation records

 Handling nested function definitions

 Functions as parameters

 Long-lived activation records

Static Allocation

 The simplest approach: allocate one activation record for every function, statically

 Older dialects of Fortran and Cobol used this system

 Simple and fast

Drawback

 Each function has one activation record

 There can be only one activation alive at a time

 Modern languages (including modern dialects of Cobol and Fortran) do not obey this restriction:

  • Recursion
  • Multithreading

Outline

 Activation-specific variables

 Static allocation of activation records

 Stacks of activation records

 Handling nested function definitions

 Functions as parameters

 Long-lived activation records

Current Activation Record

 Before, static: location of activation record was determined before runtime

 Now, dynamic: location of the current activation record is not known until runtime

 A function must know how to find the address of its current activation record

 Often, a machine register is reserved to hold this

C Example

Chapter Twelve Modern Programming Languages, 2nd ed. 20

int fact(int n) { int result; if (n<2) result = 1; else result = n * fact(n-1); return result; }

We are evaluating fact(3). This shows the contents of memory just before the recursive call that creates a second activation.

previous activation record result:?

return address

n: 3

current activation record

Docsity.com