Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2014Jim Hogg - UW - CSE - P501L-1 CSE P501 – Compiler Construction Object layout Field access What is this ? Object creation - new Method calls.

Similar presentations


Presentation on theme: "Spring 2014Jim Hogg - UW - CSE - P501L-1 CSE P501 – Compiler Construction Object layout Field access What is this ? Object creation - new Method calls."— Presentation transcript:

1 Spring 2014Jim Hogg - UW - CSE - P501L-1 CSE P501 – Compiler Construction Object layout Field access What is this ? Object creation - new Method calls Dynamic dispatch Method tables (vtables) super Runtime Type Info

2 Spring 2014Jim Hogg - UW - CSE - P501L-2 What does this program print? class B { // Base int x, y; void setx(int i) {x=i;} int getx() {return x;} void sety(int i) {y=i;} int gety() {return y;} } class D extends B { // Derived int y; void sety(int i) {y = 2*i;} int gety() {return 2*y;} void supery(int i) {super.sety(i);} } public class Main { public static void main(String[] args) { D d = new D(); B b = d; b.setx(1); System.out.println(b.getx()); b.sety(2); System.out.println(b.gety()); d.supery(42); System.out.println(d.gety()); } main will print 3 values when run. What are these values?

3 Object Layout - Conceptual (NOT actual) Spring 2014Jim Hogg - UW - CSE - P501L-3 x y setx... getx... sety... gety... class B x y setx... getx... sety... gety... class D y sety... gety... supery... x = ? y = ? setx... getx... sety... gety... y = ? sety... gety... supery... db

4 setx - getx Spring 2014Jim Hogg - UW - CSE - P501L-4 x = 1 y setx(i) {x=i;} getx... sety... gety... y = ? sety... gety... supery... d b b.setx(1) b.getx() => 1 x = 1 y setx... getx() {return x;} sety... gety... y = ? sety... gety... supery... d b

5 sety - gety Spring 2014Jim Hogg - UW - CSE - P501L-5 b.sety(2) x = 1 y setx... getx... sety... gety... y = 4 sety(i) {y=2*i;} gety... supery... d b b.gety() => 8 x = 1 y setx... getx... sety... gety... y = 4 sety... gety() {return 2*y;} supery... d b

6 supery - gety Spring 2014Jim Hogg - UW - CSE - P501L-6 x = 1 y = 42 setx... getx... sety... gety... y = 4 sety... gety... supery(i) {super.sety(i);} db d.supery(42) x = 1 y = 42 setx... getx... sety... gety... y = 4 sety... gety() {return 2*y;} supery... db d.gety() => 8

7 Object Representation in MiniJava Conceptually, each object contains: Fields - declared in its class or in its super-classes Redeclaration of a field hides (occludes) super-class instance; but still reachable via super. Methods declared in its class or in its super-classes Redeclaration of a method overrides (replaces); but still reachable via super. When a method is called, the most-derived ("furthest down the page") method is called. (Compile-time type of variable doesn't matter) But we don’t want to keep a copy of every method in every object. Just one, shared copy of each will be fine Spring 2014Jim Hogg - UW - CSE - P501L-7

8 Spring 2014Jim Hogg - UW - CSE - P501L-8 Actual Representation Each object contains A slot for each field declared in its class or in its super-classes A pointer to a runtime structure describing the class, and its method dispatch table, or "vtable" Note that sub-classes may not even exist when base class is defined. And yet, these future sub-classes must be substitutable for objects of today's base class

9 Method Dispatch Tables ("vtable") One vtable per class (not per object!) One slot for each (virtual) method slot points to beginning of method code slot matches method name + number of parameters + types of parameters vtable offsets are fixed at compile time In full Java, every class inherits hashCode, toString, etc from Object Spring 2014 Jim Hogg - UW - CSE - P501 L-9 setx getx sety gety supery xyxy y objects of class D vtable for class D xyxy y toString hashcode inserted by compiler inserted by user inherited from java.lang.Object Note that field x must be stored at the same offset in objects of class B as in objects of class D (because a D is substitutable for a B}

10 vtables : laid out at compiletime Spring 2014Jim Hogg - UW - CSE - P501L-10 xyxy object of class B vtable for class D toString hashcode xyxy y objects of class D toString hashcode B.setx B.getx B.sety B.gety D.sety D.gety D.supery setx getx sety gety setx getx sety gety supery Shared by B and D Note that a method, called abc, say occupies the same offset in the vtable for all classes in an inheritance chain

11 Spring 2014Jim Hogg - UW - CSE - P501L-11 Method Dispatch Footnotes Want vtable to point to parent vtable, to support instanceof and super. Implementing multiple Interfaces is tractable Implementing multiple inheritance, with fields and method bodies, is more complex

12 Spring 2014Jim Hogg - UW - CSE - P501L-12 Now What? Need to explore Object layout in memory Compiling field references Implicit and explicit use of “this” Representation of vtables Object creation: new Code for dynamic dispatch Including implementing “super.f” Runtime type information – instanceof and casts

13 Spring 2014Jim Hogg - UW - CSE - P501L-13 In C/C++ allocate fields sequentially, in same lexical order as they were defined, but compiler may insert padding to maintain field alignment eg: char, int => 1-byte char, 3-byte pad, 4-byte int In Java and C#, fields will be packed (largest-to-smallest) within each sub-class slice to minimize space, but to also maintain field alignment eg: doubles then ints then shorts and (Unicode) chars First word of each object points to vtable Java objects are allocated on the heap (C# structs are allocated on the stack, and follow value semantics) Object Layout

14 Spring 2014Jim Hogg - UW - CSE - P501L-14 Source int n = obj.fld; x86 Assuming that obj is a local variable in the current method mov eax, [ebp+offset obj ]; load obj ptr mov eax, [eax+offset fld ]; load fld mov [ebp+offset n ], eax; store n Field Access

15 Spring 2014Jim Hogg - UW - CSE - P501L-15 Local Fields A method can refer to fields in the receiving object either explicitly as this.fld or implicitly as fld Both compile to the same code – an implicit this. is inserted by the compiler if not present explicitly Mechanism: a reference to the current object is an implicit, first parameter to every method Can be in a register or on the stack

16 Spring 2014Jim Hogg - UW - CSE - P501L-16 class C { int x; void setx(C this, int i) { this.x = i; }... C c = new C(); setx(c, 42); class C { int x; void setx(int i) { x = i; }... C c = new C(); c.setx(42); You get this:You write this: What is this ?

17 Spring 2014Jim Hogg - UW - CSE - P501L-17 x86 Conventions (C++) ecx is traditionally used as this Add to method call mov ecx, c; c points to receiving object Do this after arguments are evaluated and pushed, right before dynamic dispatch code that actually calls the method Need to save ecx in a temporary or on the stack in methods that call other non-static methods Following examples aren’t always careful about this

18 Spring 2014Jim Hogg - UW - CSE - P501L-18 x86 Local Field Access Source int n = fld; or int n = this.fld; x86 mov eax, [ecx+offset fld ]; load fld mov [ebp+offset n ], eax; store n

19 Spring 2014Jim Hogg - UW - CSE - P501L-19 x86 Method Tables (vtables) We’ll generate vtables in assembly language program Need to pick a naming convention for method labels to avoid collisions For methods, classname$methodname Would need something more sophisticated for overloading For the vtables themselves, classname$$ Examples of "name mangling" First slot in vtable points to super-class vtable Second slot in vtable points to default (0-argument) constructor Makes implementation of super() simple

20 Example vtable Definitions Spring 2014Jim Hogg - UW - CSE - P501L-20 class B { // Base int x, y; void setx(int i) {x=i;} int getx() {return x;} void sety(int i) {y=i;} int gety() {return y;} } class D extends B { // Derived int y; void sety(int i) {y = 2*i;} int gety() {return 2*y;} void supery(int i) {super.sety(i);} }.data B$$: dd 0; no superclass dd B$B; default ctor dd B$setx dd B$getx dd B$sety dd B$gety D$$: dd B$$; parent dd D$D; default ctor dd B$setx ; parent dd B$getx; parent dd D$sety dd D$gety dd D$supery vtable definitions dd is a synonym is dword. Its operand is the value for that field, not its name. In fact, slots in vtables are anonymous - they have no name. class definitions

21 Spring 2014Jim Hogg - UW - CSE - P501L-21 vtable Footnotes Key point: First 4 non-ctor method entries in D’s vtable point to methods declared in B in exactly the same order So compiler knows correct offset for a particular method regardless of whether that method is overridden

22 Spring 2014Jim Hogg - UW - CSE - P501L-22 Steps Call storage manager (malloc or similar) to get raw bits Store pointer to vtable in first 4 bytes of object Call a constructor (with pointer to the new object, this, in ecx) Result of new is pointer to constructed object Object Creation

23 Spring 2014Jim Hogg - UW - CSE - P501L-23 Object Creation Source B b = new B(); x86 push numBytes; size-of-C + 4 call malloc; addr of bits returned in eax add esp, 4; pop numBytes lea edx, B$$; get vtable address mov [eax], edx; store vtable pointer into 1st object slot mov ecx, eax; set up this for constructor push ecx; save ecx (constructor might clobber it) ; arguments (if needed) call B$B; call constructor (no vtable lookup needed) ; (if needed) pop eax; recover pointer to object mov [ebp+offset b ],eax; store object reference in variable b

24 Spring 2014Jim Hogg - UW - CSE - P501L-24 Constructor Only special issue here is generating call to superclass constructor Same issues as super.method(…) calls – we’ll defer for now

25 Spring 2014Jim Hogg - UW - CSE - P501L-25 Steps Push arguments as usual Put pointer to object in ecx (this) Get pointer to vtable from first 4 bytes of object Jump indirect thru vtable Restore ecx to point to current object (if needed) Useful hack: push it in the function prolog so always in the stack frame at a known location Method Calls

26 Spring 2014Jim Hogg - UW - CSE - P501L-26 Method Call Source obj.meth(…); x86 ; (as needed) mov ecx, [ebp+offset obj ] ; get pointer to object mov eax, [ecx] ; get pointer to vtable call dword ptr [eax+offset meth ] ; call indirect via vtable ; (if needed) mov ecx, [ebp+offset ecxtemp ] ; (restore if needed)

27 Spring 2014Jim Hogg - UW - CSE - P501L-27 Handling super Almost the same as a regular method call with one extra level of indirection Source super.meth(…); x86 ; (if needed) mov ecx,[ebp+offset obj ]; get pointer to object mov eax,[ecx]; get method tbl pointer mov eax,[eax]; get parent’s method tbl pointer call dword ptr [eax+offset meth ] ; indirect call ; (if needed)

28 Use vtable as a “runtime representation” of the class The test for (o instanceof C) is Is o’s vtable pointer == &C$$ ? If yes, result is true Recursively, get the superclass’s vtable pointer (from current vtable) and check Stop when you reach Object, or null pointer, depending on how you represent things If no match when you reach the top of the chain, result is false Same test is part of check for legal downcast Spring 2014Jim Hogg - UW - CSE - P501L-28 Runtime Type Info

29 Spring 2014Jim Hogg - UW - CSE - P501L-29 Code Generation for Objects Representation Method calls Inheritance and overriding Strategies for implementing code generators Code improvement – optimization Next


Download ppt "Spring 2014Jim Hogg - UW - CSE - P501L-1 CSE P501 – Compiler Construction Object layout Field access What is this ? Object creation - new Method calls."

Similar presentations


Ads by Google