Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD 6.2.9 www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec13.pptx.

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Intermediate Representation III. 2 PAs PA2 deadline is
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
Garbage Collection What is garbage and how can we deal with it?
Garbage Collection  records not reachable  reclaim to allow reuse  performed by runtime system (support programs linked with the compiled code) (support.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
Run-time organization  Data representation  Storage organization: –stack –heap –garbage collection Programming Languages 3 © 2012 David A Watt,
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
CPSC 388 – Compiler Design and Construction
Mark and Sweep Algorithm Reference Counting Memory Related PitFalls
CS 536 Spring Automatic Memory Management Lecture 24.
CSC321: Programming Languages 11-1 Programming Languages Tucker and Noonan Chapter 11: Memory Management 11.1 The Heap 11.2 Implementation of Dynamic Arrays.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Compiler Construction Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Theory of Compilation Erez Petrank Lecture 9: Runtime (part 2); object oriented issues 1.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Memory Management Professor Yihjia Tsai Tamkang University.
Compiler Construction Code Generation II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Memory Allocation. Three kinds of memory Fixed memory Stack memory Heap memory.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Run time vs. Compile time
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
1 An Efficient On-the-Fly Cycle Collection Harel Paz, Erez Petrank - Technion, Israel David F. Bacon, V. T. Rajan - IBM T.J. Watson Research Center Elliot.
CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal.
Assembler, Linker and OO Paradigm Professor Yihjia Tsai Tamkang University.
Compiler Construction Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Theory of Compilation Erez Petrank Lecture 8: Runtime. 1.
Lecture 10 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
Code Generation III. PAs PA4 5.1 – 7.2 PA5 (bonus) 24.1 –
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
OOPLs /FEN March 2004 Object-Oriented Languages1 Object-Oriented Languages - Design and Implementation Java: Behind the Scenes Finn E. Nordbjerg,
Theory of Compilation Erez Petrank Lecture 9: Runtime Part II. 1.
Compiler Construction Dr. Noam Rinetzky and Orr Tamir School of Computer Science Tel Aviv University
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
Winter Compiler Construction T9 – IR part 2 + Runtime organization Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
University of Washington Wouldn’t it be nice… If we never had to free memory? Do you free objects in Java? 1.
Theory of Compilation Erez Petrank Lecture 8: Runtime. 1.
Compilation /16a Lecture 10 Compiling Object-Oriented Programs Noam Rinetzky 1.
Winter Compiler Construction Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University.
Compilation Lecture 13: Course summary: Advanced Topics Noam Rinetzky 1.
Code Generation II. 2 Compiler IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation.
Runtime Environments Chapter 7. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Design issues for Object-Oriented Languages
Dynamic Allocation in C
CSC 533: Programming Languages Spring 2016
Garbage Collection What is garbage and how can we deal with it?
CSC 533: Programming Languages Spring 2015
Dynamic Memory Allocation
CS 153: Concepts of Compiler Design November 28 Class Meeting
Concepts of programming languages
Introduction to Compilers Tim Teitelbaum
Lecture 6 C++ Programming
Strategies for automatic memory management
Mooly Sagiv and Roman Manevich School of Computer Science
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
CSC 533: Programming Languages Spring 2019
Garbage Collection What is garbage and how can we deal with it?
Presentation transcript:

Lecture 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD

2 You are here Executable code exe Source text txt Compiler Lexical Analysis Syntax Analysis Parsing Semantic Analysis Inter. Rep. (IR) Code Gen.

3 Representing Data at Runtime  Source language types  int, boolean, string, object types  Target language types  Single bytes, integers, address representation  Compiler should map source types to some combination of target types  Implement source types using target types

4 Basic Types  int, boolean, string, void  Arithmetic operations  Addition, subtraction, multiplication, division, remainder  Could be mapped directly to target language types and operations

5 Pointer Types  Represent addresses of source language data structures  Usually implemented as an unsigned integer  Pointer dereferencing – retrieves pointed value  May produce an error  Null pointer dereference  when is this error triggered?  how is this error produced?

6 Object Types  Basic operations  Field selection  computing address of field, dereferencing address  Copying  copy block or field-by-field copying  Method invocation  Identifying method to be called, calling it  How does it look at runtime?

7 Object Types class Foo { int x; int y; void rise() {…} void shine() {…} } x y rise shine Compile time information Runtime memory layout for object of class Foo DispacthVectorPtr

8 Field Selection x y rise shine Compile time information Runtime memory layout for object of class Foo DispacthVectorPtr Foo f; int q; q = f.x; MOV f, %EBX MOV 4(%EBX), %EAX MOV %EAX, q base pointer field offset from base pointer

9 Object Types - Inheritance x y rise shine Compile time information Runtime memory layout for object of class Bar twinkle z DispacthVectorPtr class Foo { int x; int y; void rise() {…} void shine() {…} } class Bar extends Foo{ int z; void twinkle() {…} }

10 Object Types - Polymorphism class Foo { … void rise() {…} void shine() {…} } x y Runtime memory layout for object of class Bar class Bar extends Foo{ … } z class Main { void main() { Foo f = new Bar(); f.rise(); } f Pointer to Bar Pointer to Foo inside Bar DVPtr

11 Dynamic Binding  Finding the right method implementation  Done at runtime according to object type  Using the Dispatch Vector (a.k.a. Dispatch Table) class Foo { … void rise() {…} void shine() {…} } class Bar extends Foo{ void rise() {…} } class Main { void main() { Foo f = new Bar(); f.rise(); }

12 Dispatch Vectors in Depth  Vector contains addresses of methods  Indexed by method-id number  A method signature has the same id number for all subclasses class Main { void main() { Foo f = new Bar(); f.rise(); } class Foo { … void rise() {…} void shine() {…} } 0 1 class Bar extends Foo{ void rise() {…} } 0 x y z f Pointer to Bar Pointer to Foo inside Bar DVPtr shine rise shine rise Object layout Dispatch vector Method code using Bar’s dispatch table

13 Dispatch Vectors in Depth class Main { void main() { Foo f = new Foo(); f.rise(); } class Foo { … void rise() {…} void shine() {…} } 0 1 class Bar extends Foo{ void rise() {…} } 0 x y f Pointer to Foo DVPtr shine rise shine rise Object layout Dispatch vector Method code using Foo’s dispatch table

14 Representing dispatch tables class A { void rise() {…} void shine() {…} static void foo() {…} } class B extends A { void rise() {…} void shine() {…} void twinkle() {…} } # data section.data.align 4 _DV_A:.long _A_rise.long _A_shine _DV_B:.long _B_rise.long _B_shine.long _B_twinkle

Multiple Inheritance 15 supertyping convert_ptr_to_E_to_ptr_to_C(e) = e convert_ptr_to_E_to_ptr_to_D(e) = e + sizeof (class C) subtyping convert_ptr_to_C_to_ptr_to_E(e) = c convert_ptr_to_D_to_ptr_to_E(e) = e - sizeof (class C) class C { field c1; field c2; void m1() {…} void m2() {…} } class D { field d1; void m3() {…} void m4() {…} } class E extends C,D{ field e1; void m2() {…} void m4() {…} void m5() {…} } c1 c2 DVPtr Pointer to E Pointer to C inside E DVPtr m2_C_E m1_C_C E-Object layout Dispatch vector d1 e1 m4_D_E m3_D_D m5_E_E Pointer to D inside E

16 Runtime checks  generate code for checking attempted illegal operations  Null pointer check  MoveField, MoveArray, ArrayLength, VirtualCall  Reference arguments to library functions should not be null  Array bounds check  Array allocation size check  Division by zero  …  If check fails jump to error handler code that prints a message and gracefully exists program

17 Null pointer check # null pointer check cmp $0,%eax je labelNPE labelNPE: push $strNPE# error message call __println push $1# error code call __exit Single generated handler for entire program

18 Array bounds check # array bounds check mov -4(%eax),%ebx # ebx = length mov $0,%ecx # ecx = index cmp %ecx,%ebx jle labelABE # ebx <= ecx ? cmp $0,%ecx jl labelABE # ecx < 0 ? labelABE: push $strABE # error message call __println push $1 # error code call __exit Single generated handler for entire program

19 Array allocation size check # array size check cmp $0,%eax# eax == array size jle labelASE # eax <= 0 ? labelASE: push $strASE # error message call __println push $1 # error code call __exit Single generated handler for entire program

Automatic Memory Management  automatically free memory when it is no longer needed  not limited to OO programs, we show it here because it is prevalent in OO languages such as Java  also in functional languages  approximate reasoning about object liveness  use reachability to approximate liveness  assume reachable objects are live  non-reachable objects are dead  Three classical garbage collection techniques  reference counting  mark and sweep  copying 20

GC using Reference Counting  add a reference-count field to every object  how many references point to it  when (rc==0) the object is non reachable  non reachable => dead  can be collected (deallocated) 21

Managing Reference Counts  Each object has a reference count o.RC  A newly allocated object o gets o.RC = 1  why?  write-barrier for reference updates update(x,old,new) { old.RC--; new.RC++; if (old.RC == 0) collect(old); }  collect(old) will decrement RC for all children and recursively collect objects whose RC reached 0. 22

Cycles!  cannot identify non-reachable cycles  reference counts for nodes on the cycle will never decrement to 0  several approaches for dealing with cycles  ignore  periodically invoke a tracing algorithm to collect cycles  specialized algorithms for collecting cycles 23

GC Using Mark & Sweep  Marking phase  mark roots  trace all objects transitively reachable from roots  mark every traversed object  Sweep phase  scan all objects in the heap  collect all unmarked objects 24

25 mark_sweep() { for Ptr in Roots mark(Ptr) sweep() } mark(Obj) { if mark_bit(Obj) == unmarked { mark_bit(Obj)=marked for C in Children(Obj) mark(C) } Sweep() { p = Heap_bottom while (p < Heap_top) if (mark_bit(p) == unmarked) then free(p) else mark_bit(p) = unmarked; p=p+size(p) } GC Using Mark & Sweep

Copying GC  partition the heap into two parts: old space, new space  GC  copy all reachable objects from old space to new space  swap roles of old/new space 26

Example 27 oldnew Roots A D C B E

Example 28 oldnew Roots A D C B E A C

The End 29