11/12/2015© 2002-08 Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008.

Slides:



Advertisements
Similar presentations
Instruction Set Design
Advertisements

Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
CSE 5317/4305 L7: Run-Time Storage Organization1 Run-Time Storage Organization Leonidas Fegaras.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
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:
CS 536 Spring Run-time organization Lecture 19.
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
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
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.
8/9/2015© Hal Perkins & UW CSEK-1 CSE P 501 – Compilers Code Shape I – Basic Constructs Hal Perkins Winter 2008.
Chapter 8 :: Subroutines and Control Abstraction
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Runtime Environments Compiler Construction Chapter 7.
Compiler Construction
CSc 453 Runtime Environments Saumya Debray The University of Arizona Tucson.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
CPSC 388 – Compiler Design and Construction Runtime Environments.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Activation Records (in Tiger) CS 471 October 24, 2007.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
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,
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Functions/Methods in Assembly
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
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
Modern Programming Tools And Techniques-I
Section 11: Comparing Java and C
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Inheritance, Polymorphism and the Object Memory Model
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Code Shape II – Objects & Classes Hal Perkins Autumn 2011
The Procedure Abstraction Part V: Run-time Structures for OOLs
Running MiniJava Basic Code Generation and Bootstrapping
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
Code Shape II – Objects & Classes Hal Perkins Summer 2004
Runtime Environments What is in the memory?.
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
Presentation transcript:

11/12/2015© Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008

11/12/2015© Hal Perkins & UW CSEL-2 Agenda Object representation and layout Field access What is this ? Object creation - new Method calls Dynamic dispatch Method tables Super Runtime type information

11/12/2015© Hal Perkins & UW CSEL-3 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 { int it; 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(); System.out.println(two.getIt()); System.out.println(two.getThat()); }

11/12/2015© Hal Perkins & UW CSEL-4 Your Answer Here

11/12/2015© Hal Perkins & UW CSEL-5 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

11/12/2015© Hal Perkins & UW CSEL-6 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

11/12/2015© Hal Perkins & UW CSEL-7 Method Dispatch Tables Often known as “vtables” One pointer per method – points to first instruction in method code Dispatch table offsets fixed at compile time One instance of this per class, not per object

11/12/2015© Hal Perkins & UW CSEL-8 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, etc.)

11/12/2015© Hal Perkins & UW CSEL-9 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

11/12/2015© Hal Perkins & UW CSEL-10 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

11/12/2015© Hal Perkins & UW CSEL-11 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 { int it; 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(); System.out.println(two.getIt()); System.out.println(two.getThat()); }

11/12/2015© Hal Perkins & UW CSEL-12 Implementation

11/12/2015© Hal Perkins & UW CSEL-13 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

11/12/2015© Hal Perkins & UW CSEL-14 Object Layout Typically, allocate fields sequentially Follow processor/OS alignment conventions when appropriate 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

11/12/2015© Hal Perkins & UW CSEL-15 Local Variable Field Access Source int n = obj.fld; X86 Assuming that obj is a local variable in the current method mov eax,[ebp+offset obj ]; load obj mov eax,[eax+offset fld ]; load fld mov [ebp+offset n ],eax; store n

11/12/2015© Hal Perkins & UW CSEL-16 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

11/12/2015© Hal Perkins & UW CSEL-17 Source Level View When you write 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);

11/12/2015© Hal Perkins & UW CSEL-18 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

11/12/2015© Hal Perkins & UW CSEL-19 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

11/12/2015© Hal Perkins & UW CSEL-20 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

11/12/2015© Hal Perkins & UW CSEL-21 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 One$getTag dd One$setIt dd One$getIt dd Two$getThat dd Two$resetIt

11/12/2015© Hal Perkins & UW CSEL-22 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

11/12/2015© Hal Perkins & UW CSEL-23 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

11/12/2015© Hal Perkins & UW CSEL-24 Object Creation Source One one = new One(…); X86 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) ; arguments (if needed) call One$One; call constructor (no vtab lookup needed) ; (if needed) pop eax; recover ptr to object mov [ebp+offset one ],eax; store object reference in variable one

11/12/2015© Hal Perkins & UW CSEL-25 Constructor Only special issue here is generating call to superclass constructor Same issues as super.method(…) calls – we’ll defer for now

11/12/2015© Hal Perkins & UW CSEL-26 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

11/12/2015© Hal Perkins & UW CSEL-27 Method Call Source obj.meth(…); X86 ; (as needed) mov ecx,[ebp+offset obj ]; get pointer to object mov eax,[ecx]; get pointer to method table call dword ptr [eax+offset meth ] ; call indirect via method tbl ; (if needed) mov ecx,[ebp+offset ecxtemp ] ; (if needed)

11/12/2015© Hal Perkins & UW CSEL-28 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)

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 11/12/2015© Hal Perkins & UW CSEL-29

11/12/2015© Hal Perkins & UW CSEL-30 Coming Attractions Code generation: register allocation, instruction selection & scheduling Industrial-strength versions plus a simpler “get it to work” scheme for the project Code optimization