
Overview ♦ In this second part of the course we will talk Implementation of about how to implement: High Level Languages ♦ Objects and inheritance. ♦ FPLs: higher order functions, laziness. ♦ Advanced Compiler Techniques Concurrency: processes, message passing. ♦ 2005 Automatic memory management. (GC) Erik Stenman ♦ Virtual Machines. EPFL ♦ Just in time compilation. Advanced Compiler Techniques 5/6/2005 2 http://lamp.epfl.ch/teaching/advancedCompiler/ Implementation of High Level Implementation of Languages Object Oriented Languages ♦We will look at some simple ways to ♦In class based OO languages each object implement concepts in HLL. belongs to a class that defines the fields, ♦We will look at some more complex and methods, and the type of the object. more efficient implementations of these class A { A a = new A; concepts. Implementation of OO int x = 42; a.foo (); int y = 17; VMT Code: foo ♦We will also look at some general Stack A a: VMT Code: foo foo : return x optimization techniques that can be used Heap int foo () { header : with great advantage in HLL. return x; x: 42 } y: 17 } Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 3 http://lamp.epfl.ch/teaching/advancedCompiler/ 4 http://lamp.epfl.ch/teaching/advancedCompiler/ Implementation of Implementation of Object Oriented Languages Object Oriented Languages ♦In class based OO languages each object ♦In class based OO languages each object belongs to a class that defines the fields, belongs to a class that defines the fields, methods, and the type of the object. methods, and the type of the object. class A { A a = new A; Reference to object: class A { A a = new A; Representation of object: Implementation of OO int x = 42; a.foo (); many/object. Implementation of OO int x = 42; a.foo (); 1/object. int y = 17; VMT Code:foo int y = 17; VMT Code:foo Stack A a: VMT Code:foo Stack A a: VMT Code:foo foo: return x foo: return x Heap Heap int foo () { header: int foo () { header : return x; x: 42 return x; x: 42 } y: 17 } y: 17 } } Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 5 http://lamp.epfl.ch/teaching/advancedCompiler/ 6 http://lamp.epfl.ch/teaching/advancedCompiler/ 1 Implementation of Implementation of Object Oriented Languages Object Oriented Languages ♦In class based OO languages each object ♦In class based OO languages each object belongs to a class that defines the fields, belongs to a class that defines the fields, methods, and the type of the object. methods, and the type of the object. class A { A a = new A; Virtual Method Table: class A { A a = new A; Code for functions (foo): Implementation of OO a.foo (); Implementation of OO a.foo (); int x = 42; 1/class. int x = 42; max 1/class. int y = 17; VMT Code:foo int y = 17; VMT Code: foo Stack A a: VMT Code:foo Stack A a: VMT Code: foo foo : return x foo: return x Heap Heap int foo () { header: int foo () { header: return x; x: 42 return x; x: 42 } y: 17 } y: 17 } } Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 7 http://lamp.epfl.ch/teaching/advancedCompiler/ 8 http://lamp.epfl.ch/teaching/advancedCompiler/ Implementation of Single Inheritance: Object Oriented Languages Fields ♦Object Oriented languages support ♦With single inheritance we can order the inheritance. fields in such a way that all fields of a class ♦Inheritance complicates the answer to some are stored after fields of the superclass. questions: ♦This way we know at compile time the Implementation of OO ♦ Where is the value of a field stored? offset of each field. ♦ Where is the code for a certain method? Implementation of OO: Single inheritance ♦ What type will a value have at runtime? Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 9 http://lamp.epfl.ch/teaching/advancedCompiler/ 10 http://lamp.epfl.ch/teaching/advancedCompiler/ Single Inheritance: Single Inheritance: Fields Fields Heap header: ♦Example: x: 0 class A {int x = 0;} header: class A { int x = 0; } class B extends A {int y = 0; x: 0 int z = 0;} y: 0 class C extends A {int r = 0;} header: class B extends A { int y = 0; class D extends B {int s = 0;} x: 0 z: 0 y: 0 s: 0 int z = 0; } z: 0 Offsets: class C extends A { int r = 0; } Stack A a: (A,B,C,D). x: 1 class D extends A { int s = 0; } B b: Implementation of OO: Single inheritance Implementation of OO: Single inheritance (B,D). y: 2 C c: header: (B,D). z: 3 x: 0 D d: r: 0 (C). r: 2 (D). s: 4 Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 11 http://lamp.epfl.ch/teaching/advancedCompiler/ 12 http://lamp.epfl.ch/teaching/advancedCompiler/ 2 Single Inheritance: Single Inheritance: Methods Methods ♦ If we only have single inheritance we can handle ♦Example: methods in much the same way as fields. ♦ We store addresses to methods in the VMT class A { int f {…}; } instead of in the object. class B extends A { int g {…}; } ♦ We copy all the addresses of the super classes to class C extends B { int f {…}; } the VMT of the subclasses. ♦ If a method is overridden we use the address of the new definition instead of the definition in the Implementation of OO: Single inheritance superclass. Implementation of OO: Single inheritance Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 13 http://lamp.epfl.ch/teaching/advancedCompiler/ 14 http://lamp.epfl.ch/teaching/advancedCompiler/ Single Inheritance: Single Inheritance: Methods Testing Class Membership class A {int f {…}; } VMT ( AAA))) class B extends A { int g {…}; } f: class C extends B { ♦Many OO languages allow you to test class Heap int f {…}; } VMT ( BBB))) Code: A_f header: f: membership of an object. header: g: Code: B_g ♦In Java there is “o instanceof C”. header: VMT ( CCC))) Code: C_f ♦An object is a member of all its A a = new A; f: B b = new B; g: superclasses. C c = new C; b.g(); ♦ c.f(); We need to be able to find the superclass of Implementation of OO: Single inheritance Implementation of OO: Single Inheritance a class. Let us extend our implementation LD r1,SP(8) ; Get b LD r1,SP(4) ; Get c LD r2,r1(0) ; Get &VMT( B) LD r2,r1(0) ; Get &VMT( C) with class descriptors. LD r3,r2(4) ; Get & B_g LD r3,r2(0) ; Get & C_f call r3 ; Call B_g call r3 ; Call C_f Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 15 http://lamp.epfl.ch/teaching/advancedCompiler/ 16 http://lamp.epfl.ch/teaching/advancedCompiler/ Single Inheritance: Single Inheritance: Class Membership Testing Class Membership class A {int f {…}; } Class AAA class B extends A { super: int g {…}; } VMT class C extends B { f: ♦Searching through the class hierarchy is int f {…}; } Code: A_f Heap Class BBB inefficient. A a = new A; Heap B b = new B; header: super: Code: B_g C c = new C; VMT f: ♦We can trade space for speed. header: c instanceof A; g: Code: C_f ♦Let each class descriptor have a display of Now we can do header: c instanceof A as: Class CCC all superclasses. I.E., a direct link to each t = c.header super: Implementation of OO: Single inheritance Implementation of OO: Single Inheritance LLL: if t == A goto True VMT f: superclass. t = t.super g: if t != nil goto LLL res = false goto End True : res = true End : Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 17 http://lamp.epfl.ch/teaching/advancedCompiler/ 18 http://lamp.epfl.ch/teaching/advancedCompiler/ 3 Single Inheritance: Multiple Inheritance Class Membership Class A class A {} class B extends A { } level: 1 class C extends B { } s ♦In languages with multiple inheritance, i.e., Class B VMT where it is possible to extend several parent A a = new A; Heap B b = new B; header: level: 2 classes with a class, all the operations we C c = new C; ss c instance of A; header: s have seen become more difficult. header: Now we can do VMT c instance of A as: ♦Java’s hybrid approach with interfaces t1 = c.header Class C complicates these issues in the same way as Implementation of OO: Single Inheritance res = t1[0] >= 1 \\\\\\A_level level: 3 if !res goto End sss Implementation of OO: Multiple Inheritance multiple inheritance. t2 = t1[2222] \\ 2<-A_level+1 ss res = (t2 == A)A AA s End: VMT Advanced Compiler Techniques 5/6/2005 Advanced Compiler Techniques 5/6/2005 19 http://lamp.epfl.ch/teaching/advancedCompiler/ 20 http://lamp.epfl.ch/teaching/advancedCompiler/ Multiple Inheritance: Multiple Inheritance: Graph Coloring Graph Coloring Heap header: ♦ One way to handle the layout of fields would be x: 0 class A {int x = 0;} to use graph coloring. (This can also be used for class B {int y = 0; methods.) int z = 0;} class C extends A,B {int r = 0;} header: ----- ♦ All identical fields would have to occupy the y: 0 z: 0 same offset in the object. Offsets: Stack A a: ♦ For some objects there would be holes in the array (A,C). x: 1 B b: of fields. To reduce the wasted space the fields (B,C). y: 2 Implementation of OO: Multiple inheritance Implementation of OO: Multiple Inheritance C c: header: can be compacted in the object by storing the (B,C). z: 3 x: 0 y: 0 offsets in the class descriptor.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages12 Page
-
File Size-