1 / 29

Theory of Compilation

Lecture 13 – Compiling Object-Oriented Programs. Theory of Compilation. Eran Yahav. www.cs.technion.ac.il/~ yahave/tocs2011/compilers-lec13.pptx. Reference: MCD 6.2.9. Source text . txt. Executable code. exe. You are here. Compiler. Lexical Analysis. Syntax Analysis Parsing.

magnar
Télécharger la présentation

Theory of Compilation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Source text txt Executable code exe You are here Compiler LexicalAnalysis Syntax Analysis Parsing SemanticAnalysis 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 DispacthVectorPtr class Foo { int x; int y; void rise() {…} void shine() {…} } x y Runtime memory layoutfor object of class Foo rise shine Compile timeinformation

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

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

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

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

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

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

  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

  15. Multiple Inheritance 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() {…} } Pointer to E DVPtr m1_C_C Pointer to C inside E c1 m2_C_E c2 m3_D_D DVPtr m4_D_E Pointer to D inside E d1 m5_E_E e1 Dispatch vector E-Object layout 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)

  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 Single generated handler for entire program labelNPE: push $strNPE # error message call __println push $1 # error code call __exit

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

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

  20. 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

  21. 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)

  22. 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 updatesupdate(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.

  23. 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

  24. 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

  25. GC Using Mark & Sweep mark(Obj) { if mark_bit(Obj) == unmarked { mark_bit(Obj)=marked for C in Children(Obj) mark(C) } } mark_sweep() { for Ptr in Roots mark(Ptr) sweep() } 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) }

  26. 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

  27. Example old new B A Roots C D E

  28. Example old new B A A Roots C C D E

  29. The End

More Related