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

The concept of memory locations for variables in modern programming languages, focusing on activation records, static and dynamic allocation, and nesting links. It covers topics such as activation-specific variables, return addresses, and link to caller's activation record.

Typology: Slides

2012/2013

Uploaded on 04/18/2013

palvani
palvani 🇮🇳

4.5

(2)

83 documents

1 / 53

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Memory Locations For Variables
Chapter Twelve Modern Programming Languages, 2nd ed. 1
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35

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