# Compiler Construction

Size: px
Start display at page:

## Transcription

1 Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University

2 Recap: Static Data Structures Outline of Lecture 18 Recap: Static Data Structures Pseudo-Dynamic Data Structures Heap Management Memory Deallocation Garbage Collection Reference-Counting Garbage Collection Mark-and-Sweep Garbage Collection 2 of 24 Compiler Construction

3 Recap: Static Data Structures Modified Syntax of EPL Definition (Modified syntax of EPL) The modified syntax of EPL is defined as follows (where n 1): Z : z (* z is an integer *) B : b ::= true false (* b is a Boolean *) R : r (* r is a real number *) Con : c ::= z b r (* c is a constant *) Ide : I, J (* I, J are identifiers *) Type : T ::= bool int real I array[z 1..z 2 ] of T record I 1 :T 1 ;... ;I n :T n end Var : V ::= I V[E] V.I Exp : E ::= c V E 1 + E 2 E 1 < E 2 E 1 and E 2... Cmd : Dcl : Pgm : C ::= V:=E C 1 ;C 2 if E then C 1 else C 2 while E do C D ::= D C D T D V D C ::= ε const I 1 := c 1 ;... ;I n := c n ; D T ::= ε type I 1 := T 1 ;... ;I n := T n ; D V ::= ε var I 1 : T 1 ;... ;I n : T n ; P ::= D C 3 of 24 Compiler Construction

4 Pseudo-Dynamic Data Structures Outline of Lecture 18 Recap: Static Data Structures Pseudo-Dynamic Data Structures Heap Management Memory Deallocation Garbage Collection Reference-Counting Garbage Collection Mark-and-Sweep Garbage Collection 4 of 24 Compiler Construction

5 Pseudo-Dynamic Data Structures Variant Records Example 18.1 (Variant records in Pascal) TYPE Coordinate = RECORD nr: INTEGER; CASE type: (cartesian, polar) OF cartesian: (x, y: REAL); polar: (r : REAL; phi: INTEGER ) END END; VAR pt: Coordinate; pt.type := cartesian; pt.x := 0.5; pt.y := 1.2; 5 of 24 Compiler Construction

6 Pseudo-Dynamic Data Structures Variant Records Example 18.1 (Variant records in Pascal) TYPE Coordinate = RECORD nr: INTEGER; CASE type: (cartesian, polar) OF cartesian: (x, y: REAL); polar: (r : REAL; phi: INTEGER ) END END; VAR pt: Coordinate; pt.type := cartesian; pt.x := 0.5; pt.y := 1.2; Implementation: Allocate memory for biggest variant Share memory between variant fields 5 of 24 Compiler Construction

7 Pseudo-Dynamic Data Structures Dynamic Arrays Example 18.2 (Dynamic arrays in Pascal) FUNCTION Sum(VAR a: ARRAY OF REAL): REAL; VAR i: INTEGER; s: REAL; BEGIN s := 0.0; FOR i := 0 to HIGH(a) do s := s + a[i] END; Sum := s END 6 of 24 Compiler Construction

8 Pseudo-Dynamic Data Structures Dynamic Arrays Example 18.2 (Dynamic arrays in Pascal) FUNCTION Sum(VAR a: ARRAY OF REAL): REAL; VAR i: INTEGER; s: REAL; BEGIN s := 0.0; FOR i := 0 to HIGH(a) do s := s + a[i] END; Sum := s END Implementation: Memory requirements unknown at compile time but determined by actual function/procedure parameters = no heap required Use array descriptor with following fields as parameter value: starting memory address of array size of array lower index of array (possibly fixed by 0) upper index of array (actually redundant) Use data stack or index register to access array elements 6 of 24 Compiler Construction

9 Heap Management Outline of Lecture 18 Recap: Static Data Structures Pseudo-Dynamic Data Structures Heap Management Memory Deallocation Garbage Collection Reference-Counting Garbage Collection Mark-and-Sweep Garbage Collection 7 of 24 Compiler Construction

10 Heap Management Dynamic Memory Allocation I Dynamically manipulated data structures (lists, trees, graphs,...) So far: creation of (static) objects by declaration Now: creation of (dynamic) objects by explicit memory allocation Access by (implicit or explicit) pointers Deletion by explicit deallocation or garbage collection (= automatic deallocation of unreachable objects) 8 of 24 Compiler Construction

11 Heap Management Dynamic Memory Allocation I Dynamically manipulated data structures (lists, trees, graphs,...) So far: creation of (static) objects by declaration Now: creation of (dynamic) objects by explicit memory allocation Access by (implicit or explicit) pointers Deletion by explicit deallocation or garbage collection (= automatic deallocation of unreachable objects) Implementation: runtime stack not sufficient (lifetime of objects generally exceeds lifetime of procedure calls) new data structure: heap Simplest form of organisation: Runtime stack Heap 0 SP (stack pointer) HP max (heap pointer) 8 of 24 Compiler Construction

12 Heap Management Dynamic Memory Allocation II Runtime stack Heap New instruction: NEW ( malloc,...) allocates n memory cells 0 SP (where n = topmost value of runtime stack) (stack pointer) returns address of first cell formal semantics (SP = stack pointer, HP = heap pointer, <.> = dereferencing): if HP - <SP> > SP then HP := HP - <SP>; <SP> := HP else error("memory overflow") HP max (heap pointer) 9 of 24 Compiler Construction

13 Heap Management Dynamic Memory Allocation II Runtime stack Heap New instruction: NEW ( malloc,...) allocates n memory cells 0 SP (where n = topmost value of runtime stack) (stack pointer) returns address of first cell formal semantics (SP = stack pointer, HP = heap pointer, <.> = dereferencing): if HP - <SP> > SP then HP := HP - <SP>; <SP> := HP else error("memory overflow") But: collision check required for every operation which increases SP (e.g., expression evaluations) Efficient solution: add extreme stack pointer EP points to topmost SP which will be used in the computation of current procedure statically computable at compile time set by procedure entry code modified semantics of NEW: if HP - <SP> > EP then HP := HP - <SP>; <SP> := HP else error("memory overflow") HP max (heap pointer) 9 of 24 Compiler Construction

14 Memory Deallocation Outline of Lecture 18 Recap: Static Data Structures Pseudo-Dynamic Data Structures Heap Management Memory Deallocation Garbage Collection Reference-Counting Garbage Collection Mark-and-Sweep Garbage Collection 10 of 24 Compiler Construction

15 Memory Deallocation Memory Deallocation Releasing memory areas that have become unused explicitly by programmer automatically by runtime system (garbage collection) 11 of 24 Compiler Construction

16 Memory Deallocation Memory Deallocation Releasing memory areas that have become unused explicitly by programmer automatically by runtime system (garbage collection) Management of deallocated memory areas by free list (usually doubly-linked list) goal: reduction of fragmentation (= heap memory split in large number of non-contiguous free areas) coalescing of contiguous areas allocation strategies: first-fit vs. best-fit 11 of 24 Compiler Construction

17 Memory Deallocation Explicit Deallocation Manually releasing memory areas that have become unused Pascal: dispose C: free 12 of 24 Compiler Construction

18 Memory Deallocation Explicit Deallocation Manually releasing memory areas that have become unused Pascal: dispose C: free Problems with manual deallocation: memory leaks: failing to eventually delete data that cannot be referenced anymore critical for long-running/reactive programs (operating systems, server code,...) dangling pointer dereference ( use after free ): referencing of deleted data may lead to runtime error (if deallocated pointer reset to nil) or produce side effects (if deallocated pointer keeps value and storage reallocated) 12 of 24 Compiler Construction

19 Memory Deallocation Explicit Deallocation Manually releasing memory areas that have become unused Pascal: dispose C: free Problems with manual deallocation: memory leaks: failing to eventually delete data that cannot be referenced anymore critical for long-running/reactive programs (operating systems, server code,...) dangling pointer dereference ( use after free ): referencing of deleted data may lead to runtime error (if deallocated pointer reset to nil) or produce side effects (if deallocated pointer keeps value and storage reallocated) Adopt programming conventions (object ownership) or use automatic deallocation 12 of 24 Compiler Construction

20 Garbage Collection Outline of Lecture 18 Recap: Static Data Structures Pseudo-Dynamic Data Structures Heap Management Memory Deallocation Garbage Collection Reference-Counting Garbage Collection Mark-and-Sweep Garbage Collection 13 of 24 Compiler Construction

21 Garbage Collection Garbage Collection Garbage = data that cannot be referenced (anymore) Garbage collection = automatic deallocation of unreachable data 14 of 24 Compiler Construction

22 Garbage Collection Garbage Collection Garbage = data that cannot be referenced (anymore) Garbage collection = automatic deallocation of unreachable data Supported by many programming languages: object-oriented: Java, Smalltalk functional: Lisp (first GC), ML, Haskell logic: Prolog scripting: Perl 14 of 24 Compiler Construction

23 Garbage Collection Garbage Collection Garbage = data that cannot be referenced (anymore) Garbage collection = automatic deallocation of unreachable data Supported by many programming languages: object-oriented: Java, Smalltalk functional: Lisp (first GC), ML, Haskell logic: Prolog scripting: Perl Design goals for garbage collectors: execution time: no significant increase of application runtime space usage: avoid memory fragmentation pause time: minimise maximal pause time of application program caused by garbage collection (especially in real-time applications) 14 of 24 Compiler Construction

24 Garbage Collection Preliminaries Object = allocated entity Object has type known at runtime, defining size of object references to other objects = excludes type-unsafe languages that allow manipulation of pointers (C, C++) 15 of 24 Compiler Construction

25 Garbage Collection Preliminaries Object = allocated entity Object has type known at runtime, defining size of object references to other objects = excludes type-unsafe languages that allow manipulation of pointers (C, C++) Reference always to address at beginning of object ( = all references to an object have same value) 15 of 24 Compiler Construction

26 Garbage Collection Preliminaries Object = allocated entity Object has type known at runtime, defining size of object references to other objects = excludes type-unsafe languages that allow manipulation of pointers (C, C++) Reference always to address at beginning of object ( = all references to an object have same value) Mutator = application program modifying objects in heap creation of objects by acquiring storage introduce/drop references to existing objects Objects become garbage when not (indirectly) reachable by mutator 15 of 24 Compiler Construction

27 Garbage Collection Reachability of Objects Root set = heap data that is directly accessible by mutator for Java: static field members and variables on stack yields directly reachable objects Every object with a reference that is stored in a reachable object is indirectly reachable 16 of 24 Compiler Construction

28 Garbage Collection Reachability of Objects Root set = heap data that is directly accessible by mutator for Java: static field members and variables on stack yields directly reachable objects Every object with a reference that is stored in a reachable object is indirectly reachable Mutator operations that affect reachability: object allocation: memory manager returns reference to new object creates new reachable object parameter passing and return values: passing of object references from calling site to called procedure or vice versa propagates reachability of objects reference assignment: assignments p := q with references p and q creates second reference to object referred to by q, propagating reachability destroys orginal reference in p, potentially causing unreachability procedure return: removes local variables potentially causes unreachability of objects 16 of 24 Compiler Construction

29 Garbage Collection Reachability of Objects Root set = heap data that is directly accessible by mutator for Java: static field members and variables on stack yields directly reachable objects Every object with a reference that is stored in a reachable object is indirectly reachable Mutator operations that affect reachability: object allocation: memory manager returns reference to new object creates new reachable object parameter passing and return values: passing of object references from calling site to called procedure or vice versa propagates reachability of objects reference assignment: assignments p := q with references p and q creates second reference to object referred to by q, propagating reachability destroys orginal reference in p, potentially causing unreachability procedure return: removes local variables potentially causes unreachability of objects Objects becoming unreachable can cause more objects to become unreachable 16 of 24 Compiler Construction

30 Garbage Collection Identifying Unreachable Objects Principal approaches: Catch program steps that turn reachable into unreachable objects = reference counting Periodically locate all reachable objects; others then unreachable = mark-and-sweep 17 of 24 Compiler Construction

31 Reference-Counting Garbage Collection Outline of Lecture 18 Recap: Static Data Structures Pseudo-Dynamic Data Structures Heap Management Memory Deallocation Garbage Collection Reference-Counting Garbage Collection Mark-and-Sweep Garbage Collection 18 of 24 Compiler Construction

32 Reference-Counting Garbage Collection Reference-Counting Garbage Collectors I Working principle Add reference count field to each heap object (= number of references to that object) 19 of 24 Compiler Construction

33 Reference-Counting Garbage Collection Reference-Counting Garbage Collectors I Working principle Add reference count field to each heap object (= number of references to that object) Mutator operations maintain reference count: object allocation: set reference count of new object to 1 parameter passing: increment reference count of each object passed to procedure reference assignment p := q: decrement/increment reference count of object referred to by p/q procedure return: decrement reference count of each object that a local variable refers to (multiple decrement if sharing) 19 of 24 Compiler Construction

34 Reference-Counting Garbage Collection Reference-Counting Garbage Collectors I Working principle Add reference count field to each heap object (= number of references to that object) Mutator operations maintain reference count: object allocation: set reference count of new object to 1 parameter passing: increment reference count of each object passed to procedure reference assignment p := q: decrement/increment reference count of object referred to by p/q procedure return: decrement reference count of each object that a local variable refers to (multiple decrement if sharing) Moreover: transitive loss of reachability when reference count of object becomes zero = decrement reference count of each object pointed to (and add object storage to free list) 19 of 24 Compiler Construction

35 Reference-Counting Garbage Collection Reference-Counting Garbage Collectors I Working principle Add reference count field to each heap object (= number of references to that object) Mutator operations maintain reference count: object allocation: set reference count of new object to 1 parameter passing: increment reference count of each object passed to procedure reference assignment p := q: decrement/increment reference count of object referred to by p/q procedure return: decrement reference count of each object that a local variable refers to (multiple decrement if sharing) Moreover: transitive loss of reachability when reference count of object becomes zero = decrement reference count of each object pointed to (and add object storage to free list) Example 18.3 (on the board) 19 of 24 Compiler Construction

36 Reference-Counting Garbage Collection Reference-Counting Garbage Collectors II Advantage: Incrementality collector operations spread over mutator s computation short pause times (good for real-time/interactive applications) immediate collection of garbage (low space usage) exception: transitive loss of reachability (reference removal may produce further garbage) but: recursive modification can be deferred 20 of 24 Compiler Construction

37 Reference-Counting Garbage Collection Reference-Counting Garbage Collectors II Advantage: Incrementality collector operations spread over mutator s computation short pause times (good for real-time/interactive applications) immediate collection of garbage (low space usage) exception: transitive loss of reachability (reference removal may produce further garbage) but: recursive modification can be deferred Disadvantages Incompleteness: cannot collect unreachable cyclic data structures (cf. Example 18.3) High overhead: additional operations for assignments and procedure calls/exits proportional to number of mutator steps (and not to number of heap objects) 20 of 24 Compiler Construction

38 Reference-Counting Garbage Collection Reference-Counting Garbage Collectors II Advantage: Incrementality collector operations spread over mutator s computation short pause times (good for real-time/interactive applications) immediate collection of garbage (low space usage) exception: transitive loss of reachability (reference removal may produce further garbage) but: recursive modification can be deferred Disadvantages Incompleteness: cannot collect unreachable cyclic data structures (cf. Example 18.3) High overhead: additional operations for assignments and procedure calls/exits proportional to number of mutator steps (and not to number of heap objects) Conclusion Use for real-time/interactive applications 20 of 24 Compiler Construction

39 Mark-and-Sweep Garbage Collection Outline of Lecture 18 Recap: Static Data Structures Pseudo-Dynamic Data Structures Heap Management Memory Deallocation Garbage Collection Reference-Counting Garbage Collection Mark-and-Sweep Garbage Collection 21 of 24 Compiler Construction

40 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors I Working principle Mutator runs and makes allocation requests Collector runs periodically (typically when space exhausted/below critical threshold) computes set of reachable objects reclaims storage for objects in complement set 22 of 24 Compiler Construction

41 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors II Algorithm 18.4 (Mark-and-sweep garbage collection) Input: heap Heap, root set Root, free list Free 23 of 24 Compiler Construction

42 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors II Algorithm 18.4 (Mark-and-sweep garbage collection) Input: heap Heap, root set Root, free list Free Procedure: 1. (* Marking phase *) for each o in Heap, let r o := true iff o referenced by Root (* initialise r flags *) 2. let W := {o r o = true} (* working set *) 3. while o W do i. let W := W \ {o} ii. for each o referenced by o with r o = false, let r o = true; W := W {o } 4. (* Sweeping phase *) for each o in Heap with r o = false, add o to Free 23 of 24 Compiler Construction

43 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors II Algorithm 18.4 (Mark-and-sweep garbage collection) Input: heap Heap, root set Root, free list Free Procedure: 1. (* Marking phase *) for each o in Heap, let r o := true iff o referenced by Root (* initialise r flags *) 2. let W := {o r o = true} (* working set *) 3. while o W do i. let W := W \ {o} ii. for each o referenced by o with r o = false, let r o = true; W := W {o } 4. (* Sweeping phase *) for each o in Heap with r o = false, add o to Free Output: modified free list 23 of 24 Compiler Construction

44 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors II Algorithm 18.4 (Mark-and-sweep garbage collection) Input: heap Heap, root set Root, free list Free Procedure: 1. (* Marking phase *) for each o in Heap, let r o := true iff o referenced by Root (* initialise r flags *) 2. let W := {o r o = true} (* working set *) 3. while o W do i. let W := W \ {o} ii. for each o referenced by o with r o = false, let r o = true; W := W {o } 4. (* Sweeping phase *) for each o in Heap with r o = false, add o to Free Output: modified free list Example 18.5 (on the board) 23 of 24 Compiler Construction

45 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors III Advantages Completeness: identifies all unreachable objects Time complexity proportional to number of objects in heap 24 of 24 Compiler Construction

46 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors III Advantages Completeness: identifies all unreachable objects Time complexity proportional to number of objects in heap Disadvantage: stop-the-world style May introduce long pauses into mutator execution (sweeping inspects complete heap) 24 of 24 Compiler Construction

47 Mark-and-Sweep Garbage Collection Mark-and-Sweep Garbage Collectors III Advantages Completeness: identifies all unreachable objects Time complexity proportional to number of objects in heap Disadvantage: stop-the-world style May introduce long pauses into mutator execution (sweeping inspects complete heap) Conclusion: refine to short-pause garbage collection Incremental collection: divide work in time by interleaving mutation and collection Partial collection: divide work in space by collecting subset of garbage at a time see Chapter 7 of A.V. Aho, M.S. Lam, R. Sethi, J.D. Ullman: Compilers Principles, Techniques, and Tools; 2nd ed., Addison-Wesley, of 24 Compiler Construction

### Compiler Construction

Compiler Construction Lecture 18: Code Generation V (Implementation of Dynamic Data Structures) Thomas Noll Lehrstuhl für Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/

### Run-time Environments -Part 3

Run-time Environments -Part 3 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Outline of the Lecture Part 3 What is run-time support?

### G Programming Languages - Fall 2012

G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

### Run-Time Environments/Garbage Collection

Run-Time Environments/Garbage Collection Department of Computer Science, Faculty of ICT January 5, 2014 Introduction Compilers need to be aware of the run-time environment in which their compiled programs

### Garbage Collection. Akim D le, Etienne Renault, Roland Levillain. May 15, CCMP2 Garbage Collection May 15, / 35

Garbage Collection Akim Demaille, Etienne Renault, Roland Levillain May 15, 2017 CCMP2 Garbage Collection May 15, 2017 1 / 35 Table of contents 1 Motivations and Definitions 2 Reference Counting Garbage

### Garbage Collection Algorithms. Ganesh Bikshandi

Garbage Collection Algorithms Ganesh Bikshandi Announcement MP4 posted Term paper posted Introduction Garbage : discarded or useless material Collection : the act or process of collecting Garbage collection

### CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can

### Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Runtime The optimized program is ready to run What sorts of facilities are available at runtime Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis token stream Syntactic

### Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Programming Languages Third Edition Chapter 10 Control II Procedures and Environments Objectives Understand the nature of procedure definition and activation Understand procedure semantics Learn parameter-passing

### CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages Variables and Constants R. Sekar 1 / 22 Topics 2 / 22 Variables and Constants Variables are stored in memory, whereas constants need not be. Value of variables

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Seminar Analysis and Verification of Pointer Programs (WS

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Generation of Intermediate Code Outline of Lecture 15 Generation

### Run-time Environments - 3

Run-time Environments - 3 Y.N. Srikant Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Outline of the Lecture n What is run-time

### Storage. Outline. Variables and updating. Copy vs. Ref semantics Lifetime. Dangling References Garbage collection

Storage 1 Variables and updating Outline Copy vs. Ref semantics Lifetime Local and global variables Heap variables Persistent variables Dangling References Garbage collection 2 Variables and Updating Variable:

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Seminar Analysis and Verification of Pointer Programs (WS

### CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

CS 480 Fall 2015 Mike Lam, Professor Runtime Environments (a.k.a. procedure calls and heap management) Subprograms General characteristics Single entry point Caller is suspended while subprogram is executing

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ws-1819/cc/ Generation of Intermediate Code Outline of Lecture 15

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Generation of Intermediate Code Conceptual Structure of

### Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Compilers 8. Run-time Support Laszlo Böszörmenyi Compilers Run-time - 1 Run-Time Environment A compiler needs an abstract model of the runtime environment of the compiled code It must generate code for

### Lecture 13: Garbage Collection

Lecture 13: Garbage Collection COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer/Mikkel Kringelbach 1 Garbage Collection Every modern programming language allows programmers

### Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Data Types In Text: Chapter 6 1 Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers Chapter 6: Data Types 2 5-1 Data Types Two components: Set of objects in the type (domain

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #6: Memory Management CS 61C L06 Memory Management (1) 2006-07-05 Andy Carle Memory Management (1/2) Variable declaration allocates

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC 330 - Spring 2013 1 Memory Attributes! Memory to store data in programming languages has the following lifecycle

### Name, Scope, and Binding. Outline [1]

Name, Scope, and Binding In Text: Chapter 3 Outline [1] Variable Binding Storage bindings and lifetime Type bindings Type Checking Scope Lifetime vs. Scope Referencing Environments N. Meng, S. Arthur 2

### Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Chapter 5 Basic Semantics Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, and the Environment Variables

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC 330 Spring 2017 1 Memory Attributes Memory to store data in programming languages has the following lifecycle

### Lecture 15 Garbage Collection

Lecture 15 Garbage Collection I. Introduction to GC -- Reference Counting -- Basic Trace-Based GC II. Copying Collectors III. Break Up GC in Time (Incremental) IV. Break Up GC in Space (Partial) Readings:

### Data Types In Text: Ch C apter 6 1

Data Types In Text: Chapter 6 1 Outline What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 2 Data Types Two components: Set of objects in the type (domain of values) Set of applicable

### Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008. Outline. Dynamic Allocation. Variables and Constants. Aliases and Problems. Garbage. Introduction. On Wednesday, we were talking

### Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Runtime Environment Relationship between names and data objects (of target machine) Allocation & de-allocation is managed by run time support package Each execution of a procedure is an activation of the

### Lecture 13: Complex Types and Garbage Collection

Lecture 13: Complex Types and Garbage Collection COMP 524 Programming Language Concepts Stephen Olivier March 17, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

### Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Project there are a couple of 3 person teams regroup or see me or forever hold your peace a new drop with new type checking is coming using it is optional 1 Compiler Architecture source code Now we jump

### Programming Languages Third Edition. Chapter 7 Basic Semantics

Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol

### CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

CS 345 Garbage Collection Vitaly Shmatikov slide 1 Major Areas of Memory Static area Fixed size, fixed content, allocated at compile time Run-time stack Variable size, variable content (activation records)

### Chapter 5. Names, Bindings, and Scopes

Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative

### Lecture Notes on Garbage Collection

Lecture Notes on Garbage Collection 15-411: Compiler Design André Platzer Lecture 20 1 Introduction In the previous lectures we have considered a programming language C0 with pointers and memory and array

### CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC330 Fall 2018 1 Memory Attributes Memory to store data in programming languages has the following lifecycle

### Garbage Collection. Weiyuan Li

Garbage Collection Weiyuan Li Why GC exactly? - Laziness - Performance - free is not free - combats memory fragmentation - More flame wars Basic concepts - Type Safety - Safe: ML, Java (not really) - Unsafe:

### Programming Language Implementation

A Practical Introduction to Programming Language Implementation 2014: Week 10 Garbage Collection College of Information Science and Engineering Ritsumeikan University 1 review of last week s topics dynamic

### Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

COMP 412 FALL 2017 Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java) Copyright 2017, Keith D. Cooper & Zoran Budimlić, all rights reserved. Students enrolled in Comp 412 at Rice

### Storage. Outline. Variables and Updating. Composite Variables. Storables Lifetime : Programming Languages. Course slides - Storage

Storage 1 Variables and Updating Outline Composite Variables Total and selective updating Array variables Storables Lifetime Local and global variables Heap variables Persistent variables Garbage collection

### CS558 Programming Languages

CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

### Names, Bindings, Scopes

Names, Bindings, Scopes Variables In imperative l Language: abstractions of von Neumann machine Variables: abstraction of memory cell or cells Sometimes close to machine (e.g., integers), sometimes not

### CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS 430 Spring 2015 Mike Lam, Professor Data Types and Type Checking Type Systems Type system Rules about valid types, type compatibility, and how data values can be used Benefits of a robust type system

### CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

CS577 Modern Language Processors Spring 2018 Lecture Garbage Collection 1 BASIC GARBAGE COLLECTION Garbage Collection (GC) is the automatic reclamation of heap records that will never again be accessed

### Types II. Hwansoo Han

Types II Hwansoo Han Arrays Most common and important composite data types Homogeneous elements, unlike records Fortran77 requires element type be scalar Elements can be any type (Fortran90, etc.) A mapping

### Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

CSc 453 Compilers and Systems Software 24 : Garbage Collection Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Dynamic Memory Management

### Compiler Construction Lecture 1: Introduction Winter Semester 2018/19 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ws-1819/cc/ Preliminaries Outline of Lecture 1 Preliminaries What

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 More Memory Management CS 61C L07 More Memory Management (1) 2004-09-15 Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia Star Wars

### Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Manual llocation Dynamic memory allocation is an obvious necessity in a programming environment. S 1622: Garbage ollection Many programming languages expose some functions or keywords to manage runtime

### Compiler Construction D7011E

Compiler Construction D7011E Lecture 14: Memory Management Viktor Leijon Slides largely by Johan Nordlander with material generously provided by Mark P. Jones. 1 First: Run-time Systems 2 The Final Component:

### Motivation for Dynamic Memory. Dynamic Memory Allocation. Stack Organization. Stack Discussion. Questions answered in this lecture:

CS 537 Introduction to Operating Systems UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department Dynamic Memory Allocation Questions answered in this lecture: When is a stack appropriate? When is

### 6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

6.172 Performance Engineering of Software Systems Spring 2009 Lecture 9 MIT OpenCourseWare Dynamic Storage Allocation Stack allocation: LIFO (last-in-first-out) Array and pointer A used unused P before

### Automatic Memory Management

Automatic Memory Management Why Automatic Memory Management? Storage management is still a hard problem in modern programming Why Automatic Memory Management? Storage management is still a hard problem

### CS 241 Honors Memory

CS 241 Honors Memory Ben Kurtovic Atul Sandur Bhuvan Venkatesh Brian Zhou Kevin Hong University of Illinois Urbana Champaign February 20, 2018 CS 241 Course Staff (UIUC) Memory February 20, 2018 1 / 35

### G Programming Languages - Fall 2012

G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

### Garbage Collection. Lecture Compilers SS Dr.-Ing. Ina Schaefer. Software Technology Group TU Kaiserslautern. Ina Schaefer Garbage Collection 1

Garbage Collection Lecture Compilers SS 2009 Dr.-Ing. Ina Schaefer Software Technology Group TU Kaiserslautern Ina Schaefer Garbage Collection 1 Content of Lecture 1. Introduction: Overview and Motivation

### One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Automatic Memory Management #1 One-Slide Summary An automatic memory management system deallocates objects when they are no longer used and reclaims their storage space. We must be conservative and only

### CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table

### Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

### Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

### Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Chapter 6 Topics WEEK E FOUR Data Types Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer and Reference

### Topic IV. Parameters. Chapter 5 of Programming languages: Concepts & constructs by R. Sethi (2ND EDITION). Addison-Wesley, 1996.

References: Topic IV Block-structured procedural languages Algol and Pascal Chapters 5 and 7, of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapter 5 of Programming languages: Concepts

### Run-time Environments

Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

### Run-time Environments

Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

### Compiler Construction Lecture 1: Introduction Summer Semester 2017 Thomas Noll Software Modeling and Verification Group RWTH Aachen University

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Preliminaries People Lectures: Thomas Noll (noll@cs.rwth-aachen.de)

### 6. Names, Scopes, and Bindings

Copyright (C) R.A. van Engelen, FSU Department of Computer Science, 2000-2004 6. Names, Scopes, and Bindings Overview Names Binding time Object lifetime Object storage management Static allocation Stack

### Topic IV. Block-structured procedural languages Algol and Pascal. References:

References: Topic IV Block-structured procedural languages Algol and Pascal Chapters 5 and 7, of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapters 10( 2) and 11( 1) of Programming

### Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

### CS321 Languages and Compiler Design I Winter 2012 Lecture 13

STATIC SEMANTICS Static Semantics are those aspects of a program s meaning that can be studied at at compile time (i.e., without running the program). Contrasts with Dynamic Semantics, which describe how

### Programmin Languages/Variables and Storage

Programmin Languages/Variables and Storage Onur Tolga Şehitoğlu Computer Engineering 4 Mart 2007 Outline 1 Storage Array Variables 2 Semantics of Assignment 3 Variable Lifetime Global Lifetime Local Lifetime

### Garbage Collection Techniques

Garbage Collection Techniques Michael Jantz COSC 340: Software Engineering 1 Memory Management Memory Management Recognizing when allocated objects are no longer needed Deallocating (freeing) the memory

### Garbage Collection. Steven R. Bagley

Garbage Collection Steven R. Bagley Reference Counting Counts number of pointers to an Object deleted when the count hits zero Eager deleted as soon as it is finished with Problem: Circular references

### CS558 Programming Languages

CS558 Programming Languages Fall 2017 Lecture 3a Andrew Tolmach Portland State University 1994-2017 Binding, Scope, Storage Part of being a high-level language is letting the programmer name things: variables

### Dynamic Memory Allocation

Dynamic Memory Allocation CS61, Lecture 11 Prof. Stephen Chong October 6, 2011 Announcements 1/2 Reminder: No section on Monday Monday sections have been rescheduled See website for details Please attend

SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms

### Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Fifth Generation CS 4100 LISP From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition, by Bruce J. MacLennan, Chapters 9, 10, 11, and based on slides by Istvan Jonyer

### Garbage Collection. Hwansoo Han

Garbage Collection Hwansoo Han Heap Memory Garbage collection Automatically reclaim the space that the running program can never access again Performed by the runtime system Two parts of a garbage collector

### TYPES, VALUES AND DECLARATIONS

COSC 2P90 TYPES, VALUES AND DECLARATIONS (c) S. Thompson, M. Winters 1 Names, References, Values & Types data items have a value and a type type determines set of operations variables Have an identifier

### Types and Type Inference

CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

### Advances in Programming Languages: Regions

Advances in Programming Languages: Regions Allan Clark and Stephen Gilmore The University of Edinburgh February 22, 2007 Introduction The design decision that memory will be managed on a per-language basis

### Compiler Construction

Compiler Construction Lecture 15: Code Generation I (Intermediate Code) Thomas Noll Lehrstuhl für Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/

### CS558 Programming Languages

CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a

### Qualifying Exam in Programming Languages and Compilers

Qualifying Exam in Programming Languages and Compilers University of Wisconsin Fall 1991 Instructions This exam contains nine questions, divided into two parts. All students taking the exam should answer

### High-Level Language VMs

High-Level Language VMs Outline Motivation What is the need for HLL VMs? How are these different from System or Process VMs? Approach to HLL VMs Evolutionary history Pascal P-code Object oriented HLL VMs

### Structure of Programming Languages Lecture 10

Structure of Programming Languages Lecture 10 CS 6636 4536 Spring 2017 CS 6636 4536 Lecture 10: Classes... 1/23 Spring 2017 1 / 23 Outline 1 1. Types Type Coercion and Conversion Type Classes, Generics,

### Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation II CSE 351 Autumn 2016 Instructor: Justin Hsia Teaching Assistants: Chris Ma Hunter Zahn John Kaltenbach Kevin Bi Sachin Mehta Suraj Bhat Thomas Neuman Waylon Huang Xi Liu Yufang Sun

### Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

Memory Management Chapter Fourteen Modern Programming Languages, 2nd ed. 1 Dynamic Memory Allocation Lots of things need memory at runtime: Activation records Objects Explicit allocations: new, malloc,

### Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

### Compiler Construction

Compiler Construction Lecture 15: Code Generation I (Intermediate Code) Thomas Noll Lehrstuhl für Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/

### Programming Languages

Programming Languages Tevfik Koşar Lecture - VIII February 9 th, 2006 1 Roadmap Allocation techniques Static Allocation Stack-based Allocation Heap-based Allocation Scope Rules Static Scopes Dynamic Scopes

### Heap Management. Heap Allocation

Heap Management Heap Allocation A very flexible storage allocation mechanism is heap allocation. Any number of data objects can be allocated and freed in a memory pool, called a heap. Heap allocation is

### CS558 Programming Languages

CS558 Programming Languages Fall 2016 Lecture 3a Andrew Tolmach Portland State University 1994-2016 Formal Semantics Goal: rigorous and unambiguous definition in terms of a wellunderstood formalism (e.g.

### Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 7 C Memory Management 2007-02-06 Hello to Said S. from Columbus, OH CS61C L07 More Memory Management (1) Lecturer SOE Dan Garcia www.cs.berkeley.edu/~ddgarcia

### CS558 Programming Languages

CS558 Programming Languages Winter 2017 Lecture 4a Andrew Tolmach Portland State University 1994-2017 Semantics and Erroneous Programs Important part of language specification is distinguishing valid from