Docsity

Implementing Functions - Compilers and Interpreters - Lecture Slides | CS 4240, Assignments of Computer Science

Partial preview of the text

Download Implementing Functions - Compilers and Interpreters - Lecture Slides | CS 4240 and more Assignments Computer Science in PDF only on Docsity!

Implementing functions

Theory

First-class

Functions

Programming

Methodology Implementation

Memory Management

In the beginning, there was FORTRAN: no dynamically allocated memory everything in static arrays with fi xed limits a signi fi cant style as recently as 1983 (TEX)

Dynamic allocation, explicit memory management: Pascal (new/dispose) C (malloc/free) C++ (new/delete)

Still an area for some research: ef fi ciency locality (important for memory hierarchy) avoiding fragmentation

many schemes (Knuth, vol. 1; also Grunwald & Zorn)

Automatic Memory Management

Why not have malloc without free? let memory be freed automatically why not call it cons

Can be required in the language de fi nition: No Scheme object is ever destroyed. The reason that implementations of Scheme do not (usually!) run out of storange is that they are permitted to reclaim the storage occupied by an object if they can prove that the object cannot possibly matter to any future computation.

Plan of study: see how garbage is created see how we identify it discuss ways of reclaiming it

Garbage Creation

Association list for instructor

(set nr ’((Course 152) (Office (Maxwell-Dworkin 231)) (Email nr@eecs))) nr

Course 152 Email nr@eecs

Office

231 Maxwell-Dworkin

To change of fi ce from list to symbol:

(set nr (mkassoc ’Office ’Maxwell-Dworkin- nr)) nr

Course 152 Email nr@eecs

Office

231 Maxwell-Dworkin

First recursive call:

(mkassoc x y (cdr alist)) alist nr

Course 152 Email nr@eecs

Office

231 Maxwell-Dworkin

Now return, back to fi rst call:

(cons (car alist) (mkassoc ...))

alist nr

152 Email nr@eecs

Office

231 Maxwell-Dworkin

Course

Office Maxwell-Dworkin-

Return from mkassoc : Just before set , everything is live value to be returned from mkassoc

nr

nr@eecs

Office

231 Maxwell-Dworkin

Course

Office Maxwell-Dworkin-

152 Email

What is garbage?

A cell is garbage if its contents can’t affect any future (legal) computation

As an approximation, we call a cell garbage if it is not reachable from a root set

Any cell that is not garbage is live data live data might affect a future computation

What values can affect a computation? We divide these values into two parts. Roots: local variables of procedures parameters of procedures global variables Other values reachable from the roots: what if a formal parameter is a cons cell?

Identifying roots

Unless you planned ahead, fi nding roots is nontrivial we have done for you on homework

Roots in compiled code: global variables local variables on the call stack machine registers

Collector has to be told where to look (but wait for “conservative” collection)

Roots in a bytecode interpreter (e.g., Kamin chap 9) part of the de fi nition of the “virtual machine”

Roots in an AST-based interpreter any active C functio that could lead to Sx global variables that could lead to Sx s ( globalEnv , nilValue , trueValue )

Tracing pointers

Confusing pointer & integer could cause leaks suppose integer n is address of an object?

Two major approaches Type tagging: each type of heap object gets unique tag tag leads to descriptive info compiler puts “which fi elds are pointers” in desc Example: Modula- Pointer tagging: use special bits to distinguish pointers, integers “LISP machines” had special “tag bits” on stock hardware, loses a bit from integer space e.g., all integers have low bit set heap object descriptors only show size some objects are ‘non-pointer-containing’ e.g., strings Example: Standard ML of New Jersey

Two families of garbage collectors

Using “start at roots, follow pointers” approach: mark-and-sweep collection

  1. unmark all objects
  2. trace pointers, marking all live data
  3. “sweep away” unmarked objects unmarked objects moved to “free list”, reallocated copying collection: works with two “semi-spaces” all objects, some free space in “from-space” “to-space” is empty from-space full? copy live data to to-space to-space is not full (not all objects copied) now objects, free space are in to-space “ fl ip”

Garbage collection and heap growth

Allocate

OK Empty

gc

Allocate

OK

k > 0

Empty

k = 0

Grow Heap

Allocate

OK Empty (FATAL)

n live cells k free cells

Mark and Sweep Collection

Return to association list:

nr

Email nr@eecs

Office

231 Maxwell-Dworkin

Office Maxwell-Dworkin-

Course 152