Code Shape II – Objects & Classes Hal Perkins Autumn 2005

Slides:



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

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.
Run time vs. Compile time
Run-time Environment and Program Organization
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
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
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.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
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
The Object-Oriented Thought Process Chapter 03
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
CSE 374 Programming Concepts & Tools
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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
Discussion Section – 11/3/2012
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
Implementing Subprograms
Code Shape I – Basic Constructs Hal Perkins Autumn 2011
Running MiniJava Basic Code Generation and Bootstrapping
PZ09A - Activation records
Running MiniJava Basic Code Generation and Bootstrapping
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
CSC 497/583 Advanced Topics in Computer Security
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Code Shape II – Objects & Classes Hal Perkins Autumn 2009
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 2005 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Autumn 2005 2/22/2019 © 2002-05 Hal Perkins & UW CSE

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

OverLoading and Hiding 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 { int it; void setTag() { tag = 2; it = 3; } int getThat() { return it; } void resetIt() { super.setIt(42); } 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 Hal Perkins & UW CSE

Your Answer Here 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 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/C++ struct Fields hidden by declarations in extended classes are still allocated in the object and are accessible from superclass methods 2/22/2019 © 2002-05 Hal Perkins & UW CSE

Method Dispatch Tables Often known as “vtables” One pointer per method Offsets fixed at compile time One instance of this per class, not per object 2/22/2019 © 2002-05 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 local Look in parent class table if not local Repeat Actually used in some dynamic systems (e.g. SmallTalk, etc.) 2/22/2019 © 2002-05 Hal Perkins & UW CSE

O(1) Method Dispatch Idea: First part of method table for extended class has pointers 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 2/22/2019 © 2002-05 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 multiple interfaces in perverse situations(!) 2/22/2019 © 2002-05 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(); } 2/22/2019 © 2002-05 Hal Perkins & UW CSE

Implementation & Trace 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 Hal Perkins & UW CSE

Object Layout Typically, allocate fields sequentially Follow processor/OS alignment conventions when appropriate Use first 32 bits of object for pointer to method table/class information Objects are allocated on the heap No actual representation in the generated code 2/22/2019 © 2002-05 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 mov eax,[eax+offsetfld] ; load fld mov [ebp+offsetn],eax ; store n 2/22/2019 © 2002-05 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 Mechanism: a reference to the current object is an implicit parameter to every method Can be in a register or on the stack 2/22/2019 © 2002-05 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); 2/22/2019 © 2002-05 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 (more about that to come) 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 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 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 Need something more sophisticated to implement 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 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 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 (pointer to new object, this, in ecx) Result of new is pointer to the constructed object 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 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 2/22/2019 © 2002-05 Hal Perkins & UW CSE

Method Call Source X86 obj.meth(…); <push arguments from right to left> ; (if 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 2/22/2019 © 2002-05 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) Au02: It should be possible to jump indirectly, but I’m not sure about the syntax 2/22/2019 © 2002-05 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” 2/22/2019 © 2002-05 Hal Perkins & UW CSE

Coming Attractions Code Generation Two models Simple tree walk, which is adequate to complete the project More standard instruction selection/ scheduling/register allocation regime Rest of the quarter – survey of optimization plus special topics 2/22/2019 © 2002-05 Hal Perkins & UW CSE