Code Shape II – Objects & Classes Hal Perkins Autumn 2009

Slides:



Advertisements
Similar presentations
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Advertisements

CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
10/14/2002© 2002 Hal Perkins & UW CSEM-1 CSE 582 – Compilers Running JFlat Basic Code Generation and Bootstrapping Hal Perkins Autumn 2002.
Run time vs. Compile time
Semantics of Calls and Returns
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Chapter 8 :: Subroutines and Control Abstraction
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Activation Records (in Tiger) CS 471 October 24, 2007.
11/12/2015© Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008.
Spring 2014Jim Hogg - UW - CSE - P501L-1 CSE P501 – Compiler Construction Object layout Field access What is this ? Object creation - new Method calls.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CSC 8505 Compiler Construction Runtime Environments.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
CSE 333 Systems Programming Hal Perkins Winter 2016 Bonus Lecture– Function Pointers and Objects in C.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Design issues for Object-Oriented Languages
Object Lifetime and Pointers
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Modern Programming Tools And Techniques-I
Section 11: Comparing Java and C
Implementing Subprograms Chapter 10
Names and Attributes Names are a key programming language feature
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE 374 Programming Concepts & Tools
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Inheritance and Polymorphism
COMPILERS Activation Records
143A: Principles of Operating Systems Lecture 4: Calling conventions
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
Introduction to Compilers Tim Teitelbaum
Inheritance, Polymorphism and the Object Memory Model
Chapter 9 :: Subroutines and Control Abstraction
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Java Programming Language
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
Code Shape II – Objects & Classes Hal Perkins Autumn 2011
The Procedure Abstraction Part V: Run-time Structures for OOLs
The University of Adelaide, School of Computer Science
Code Shape I – Basic Constructs Hal Perkins Autumn 2011
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
EECE.3170 Microprocessor Systems Design I
Running MiniJava Basic Code Generation and Bootstrapping
EECE.3170 Microprocessor Systems Design I
PZ09A - Activation records
Running MiniJava Basic Code Generation and Bootstrapping
Code Shape II – Objects & Classes Hal Perkins Autumn 2005
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Code Shape II – Objects & Classes Hal Perkins Summer 2004
Runtime Environments What is in the memory?.
Computer Organization and Assembly Language
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

Code Shape II – Objects & Classes Hal Perkins Autumn 2009 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Autumn 2009 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Agenda Object representation and layout Field access What is this? Object creation - new Method calls Dynamic dispatch Method tables Super Runtime type information 7/20/2019 © 2002-09 Hal Perkins & UW CSE

What does this program print? class One { int tag; int it; void setTag() { tag = 1; } int getTag() { return tag; } void setIt(int it) {this.it = it;} int getIt() { return it; } } class Two extends One { void setTag() { tag = 2; it = 3; int getThat() { return it; } void resetIt() { super.setIt(42); } public static void main(String[] args) { Two two = new Two(); One one = two; one.setTag(); System.out.println(one.getTag()); one.setIt(17); two.setTag(); System.out.println(two.getIt()); System.out.println(two.getThat()); two.resetIt(); } Au02: output is (on separate lines) 2 17 3 42 3 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Your Answer Here 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Object Representation The naïve explanation is that an object contains Fields declared in its class and in all superclasses Redeclaration of a field hides superclass instance Methods declared in its class and in all superclasses Redeclaration of a method overrides (replaces) But overridden methods can still be accessed by super.… When a method is called, the method “inside” that particular object is called But we don’t want to really implement it this way – we only want one copy of each method’s code 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Actual representation Each object contains An entry for each field (variable) A pointer to a runtime data structure describing the class Key component: method dispatch table Basically a C struct Fields hidden by declarations in extended classes are still allocated in the object and are accessible from superclass methods 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Method Dispatch Tables Often known as “vtables” One pointer per method – points to beginning of method code Dispatch table offsets fixed at compile time One instance of this per class, not per object 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Method Tables and Inheritance Simple implementation Method table for extended class has pointers to methods declared in it Method table also contains a pointer to parent class method table Method dispatch Look in current table and use it if method declared locally Look in parent class table if not local Repeat Actually used in some dynamic systems (e.g. SmallTalk, Ruby, etc.) 7/20/2019 © 2002-09 Hal Perkins & UW CSE

O(1) Method Dispatch Idea: First part of method table for extended class has pointers for same methods in same order as parent class BUT pointers actually refer to overriding methods if these exist  Method dispatch is indirect using fixed offsets known at compile time – O(1) In C: *(object->vtbl[offset])(parameters) Pointers to additional methods in extended class are included in the table following inherited/overridden ones 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Method Dispatch Footnotes Still want pointer to parent class method table for other purposes Casts and instanceof Multiple inheritance requires more complex mechanisms Also true for multiple interfaces 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Perverse Example Revisited class One { int tag; int it; void setTag() { tag = 1; } int getTag() { return tag; } void setIt(int it) {this.it = it;} int getIt() { return it; } } class Two extends One { void setTag() { tag = 2; it = 3; int getThat() { return it; } void resetIt() { super.setIt(42); } public static void main(String[] args) { Two two = new Two(); One one = two; one.setTag(); System.out.println(one.getTag()); one.setIt(17); two.setTag(); System.out.println(two.getIt()); System.out.println(two.getThat()); two.resetIt(); } 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Implementation 7/20/2019 © 2002-09 Hal Perkins & UW CSE

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 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Object Layout Typically, allocate fields sequentially Follow processor/OS alignment conventions when appropriate / available Use first word of object for pointer to method table/class information Objects are allocated on the heap No actual bits in the generated code 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Local Variable Field Access Source int n = obj.fld; X86 Assuming that obj is a local variable in the current method mov eax,[ebp+offsetobj] ; load obj ptr mov eax,[eax+offsetfld] ; load fld mov [ebp+offsetn],eax ; store n 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Local Fields A method can refer to fields in the receiving object either explicitly as “this.f” or implicitly as “f” Both compile to the same code – an implicit “this.” is assumed if not present explicitly Mechanism: a reference to the current object is an implicit parameter to every method Can be in a register or on the stack 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Source Level View When you write You really get void setIt(int it) { this.it = it; } … obj.setIt(42); You really get void setIt(ObjType this, int it) { this.it = it; } … setIt(obj,42); 7/20/2019 © 2002-09 Hal Perkins & UW CSE

x86 Conventions (C++) ecx is traditionally used as “this” Add to method call mov ecx,receivingObject ; ptr to 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 One possibility: add to prologue Following examples aren’t careful about this 7/20/2019 © 2002-09 Hal Perkins & UW CSE

x86 Local Field Access Source X86 int n = fld; or int n = this.fld; mov eax,[ecx+offsetfld] ; load fld mov [ebp+offsetn],eax ; store n 7/20/2019 © 2002-09 Hal Perkins & UW CSE

x86 Method Tables (vtbls) We’ll generate these in the assembly language source program Need to pick a naming convention for method labels; suggestion: For methods, classname$methodname Would need something more sophisticated for overloading For the vtables themselves, classname$$ First method table entry points to superclass table Also useful: second entry points to default (0-argument) constructor (if you have constructors) Makes implementation of super() particularly simple 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Method Tables For Perverse Example class One { void setTag() { … } int getTag() { … } void setIt(int it) {…} int getIt() { … } } class Two extends One { void setTag() { … } int getThat() { … } void resetIt() { … } .data One$$ dd 0 ; no superclass dd One$One dd One$setTag dd One$getTag dd One$setIt dd One$getIt Two$$ dd One$$ ; parent dd Two$Two dd Two$setTag dd Two$getThat dd Two$resetIt 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Method Table Footnotes Key point: First four non-constructor method entries in Two’s method table are pointers to methods declared in One in exactly the same order  Compiler knows correct offset for a particular method regardless of whether that method is overridden 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Object Creation – new Steps needed Call storage manager (malloc or similar) to get the raw bits Store pointer to method table in the first 4 bytes of the object Call a constructor (with pointer to the new object, this, in ecx) Result of new is pointer to the constructed object 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Object Creation Source X86 One one = new One(…); push nBytesNeeded ; obj size + 4 call mallocEquiv ; addr of bits returned in eax add esp,4 ; pop nBytesNeeded lea edx,One$$ ; get method table address mov [eax],edx ; store vtab ptr at beginning of object mov ecx,eax ; set up “this” for constructor push ecx ; save ecx (constructor might clobber it) <push constructor arguments> ; arguments (if needed) call One$One ; call constructor (no vtab lookup needed) <pop constructor arguments> ; (if needed) pop eax ; recover ptr to object mov [ebp+offsetone],eax ; store object reference in variable one 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Constructor Only special issue here is generating call to superclass constructor Same issues as super.method(…) calls – we’ll defer for now 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Method Calls Steps needed Push arguments as usual Put pointer to object in ecx (new this) Get pointer to method table from first 4 bytes of object Jump indirectly through method table Restore ecx to point to current object (if needed) Useful hack: push it in the function prologue so it is always in the stack frame at a known location 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Method Call Source X86 obj.meth(…); <push arguments from right to left> ; (as needed) mov ecx,[ebp+offsetobj] ; get pointer to object mov eax,[ecx] ; get pointer to method table call dword ptr [eax+offsetmeth] ; call indirect via method tbl <pop arguments> ; (if needed) mov ecx,[ebp+offsetecxtemp] ; (if needed) Au02: It should be possible to jump indirectly, but I’m not sure about the syntax 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Handling super Almost the same as a regular method call with one extra level of indirection Source super.meth(…); X86 <push arguments from right to left> ; (if needed) mov ecx,[ebp+offsetobj] ; get pointer to object mov eax,[ecx] ; get method tbl pointer mov eax,[eax] ; get parent’s method tbl pointer call dword ptr [eax+offsetmeth] ; indirect call <pop arguments> ; (if needed) 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Runtime Type Checking Use the method table for the class as a “runtime representation” of the class The test for “o instanceof C” is Is o’s method table pointer == &C$$? If so, result is “true” Recursively, get the superclass’s method table pointer from the method table and check that Stop when you reach Object (or a null pointer, depending on how you represent things) If no match when you reach the top of the chain, result is “false” Same test as part of check for legal downcast 7/20/2019 © 2002-09 Hal Perkins & UW CSE

Coming Attractions Code generation: register allocation, instruction selection & scheduling Industrial-strength versions plus a simpler “get it to work” scheme for our project Code optimization 7/20/2019 © 2002-09 Hal Perkins & UW CSE