COMP 412 FALL 2017 Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412 source IR Front End Optimizer Back End IR target Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have eplicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. Chapter 6 & 7 in EaC2e
Support for Name Spaces In an ALL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit that builds & maintains the runtime structures for addressability In an OOL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit that builds & maintains the runtime structures for addressability This lecture focuses on runtime support for OOLs. COMP 412, Fall 2017 1
Runtime Support for OOLs Where are we? We have seen: Symbol tables for leically-scoped ALLs Symbol tables & object tables for OOLs Linkage conventions for procedure calls (in ALLs and OOLs) Access links & displays to provide runtime support for leical scopes ALL addressability relies on the ARP and name mangled labels An OOL adds addresses that are relative to the receiving object Today: runtime structures to support addressability in an OOL In essence, given an object fee, how do we find fee.method() and fee.member COMP 412, Fall 2017 2
From the lecture on naming Compile-time Structures for OOLs Java To compile method M of object O in C, the compiler needs: 1. Leically scoped symbol table for the current block and its surrounding scopes in M Just like an ALL inner declarations hide outer declarations 2. Chain of symbol tables for inheritance Class C and all of its superes Need to find methods and instance variables in any super 3. Symbol tables for all global es (package scope) Entries for all members with visibility Need to construct symbol tables for imported packages and link them into the structure in appropriate places Three sets of tables are needed for name resolution. In an ALL, we could combine 1 & 3 for a single unified set of tables. In Java, we need to split them so that the compiler can check the inheritance hierarchy between the leical hierarchy & the global name space. COMP 412, Fall 2017 3
From the lecture on naming Compile-time Structures for OOLs Java Conceptually Leical Hierarchy Class Hierarchy Global Scope Search Order: leical hieraracy, hierarchy, global scope COMP 412, Fall 2017 Note the sheaf of tables model. 4
The Java Name Space Class Point { public int, y; public void draw(); } Class ColorPoint etends Point { Color c; public void draw() { } public void test() { y = ; draw(); } } Class C { int, y; public void m() { Point p = new ColorPoint(); y = p.; p.draw(); } } The ic eample for discussing inheritance. We will use & etend it. // inherits, y, & draw() from Point // local data // override (hide) Point s draw // local // independent of Point & ColorPoint // local data // local // uses ColorPoint, and, by inheritance // the definitions from Point This slide appeared gratuitously & COMP 412, Fall 2017 pointlessly in an earlier lecture. 5
Runtime Structures for OOLs This discussion is generic rather than Java-specific. Object lifetimes are independent Each object needs an object record (OR) to hold its state Independent allocation and deallocation Think heap We will talk of OR pointers, much like AR pointers We won t call them ORPs Classes are objects, too ORs of es instantiate the hierarchy Object Records Distinct static storage for data members Need fast, consistent access Known constant offsets from OR pointer Linguistic provision for initialization of method lifetimes, of lifetimes of other objects The Concept an object s data area fee() fie() foe() count 0 4 8 12 The Heap address polynomial for a vector, similar COMP 412, Fall 2017 to fields in an activation record. 6
Review What is an Object? The Concept: An object is an abstract data type that encapsulates data, operations and internal state behind a simple, consistent interface. Data Code Data Code Elaborating the concepts: Each object has internal state Data members are static (lifetime of object ) Eternal access is through members Each object has a set of associated procedures, or methods Some methods are public, others are private Locating a procedure by name is more comple than in an ALL Comple behavior arises from objects internal states y z Data Code To avoid ecess copies, keep in es Ideas COMP go 412, back Fall to 2017 Simula 67, & data-abstraction languages such as CLU & Alphard 7
Runtime Structures for OOLs From Concept to Implementation Members of a should share members Replication would waste space Replication would be a nightmare This discussion is generic rather than Java-specific. fee() fie() foe() count 0 4 8 12 The Concept y count data 1 data count 2 The Implementation fee() fie() foe() The Implementation Single copy of the for each method Class has a vector of pointers Place vector @ fied offset in OR COMP 412, Fall 2017 8
Runtime Structures for OOLs From Concept to Implementation Members of a should share members Replication would waste space Replication would be a nightmare This discussion is generic rather than Java-specific. fee() fie() foe() count 0 4 8 12 The Concept y count data 1 data 2 count The Implementation fee() fie() foe() The Implementation Single copy of the for each method Class has a vector of pointers Place vector @ fied offset in OR At offset 0 for efficiency (?) COMP 412, Fall 2017 9
Runtime Structures for OOLs From Concept to Implementation Members of a should share members Replication would waste space Replication would be a nightmare This discussion is generic rather than Java-specific. fee() fie() foe() count 0 4 8 12 The Concept y count data 1 data 2 fee() fie() The Implementation Single copy of the for each method Move pointer into object for faster access One more slot in the OR count Reality foe() One less indirection in each call COMP 412, Fall 2017 10
OR Layout Assume single inheritance Inheritance has a strong impact on OR Layout OR needs slots for each declared member, all the way up the hierarchy (, super, super-super, ) Each object needs an instance variable that points to its Each needs an instance variable that points to its super Can use prefiing of storage to lay out the OR Back to Our Java Eample Class Point Class Point { public int, y; } Class ColorPoint etends Point { Color c; } OR for a Point What happens if we cast a ColorPoint to a Point? COMP A Point 412, to Fall a ColorPoint? 2017 Take the word etends literally. 11 self self OR for a ColorPoint y y c
Open World versus Closed World Compile-time prefiing assumes that the structure is known when layout is performed. Two common cases occur in OO language design. Closed-World Assumption Class structure is known and closed prior to runtime Can lay out ORs in the compiler and/or the linker (Compile time) Open-World Assumption Class structure can change at runtime Cannot lay out ORs until they are allocated Walk hierarchy at allocation Need the compile-time data structures at runtime (Interpreter or JIT) C++ has a closed structure. Java as an open structure. STOP COMP 412, Fall 2017 12