CSE 401/M501 Compilers

Similar documents
CSE 401/M501 Compilers

CSE 401/M501 Compilers

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Java and C CSE 351 Spring

CSE 401 Final Exam. December 16, 2010

Java and C. CSE 351 Autumn 2018

CSE 401/M501 Compilers

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

CSE 401 Compilers. Code Shape I Basic Constructs Hal Perkins Winter UW CSE 401 Winter 2015 K-1

Rules and syntax for inheritance. The boring stuff

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

CSE P 501 Exam Sample Solution 12/1/11

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CS558 Programming Languages Winter 2013 Lecture 8

CS-202 Introduction to Object Oriented Programming

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

COP 3330 Final Exam Review

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

What is Inheritance?

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

Data Abstraction. Hwansoo Han

CSE 401/M501 Compilers

Subtyping (Dynamic Polymorphism)

CSE P 501 Exam 12/1/11

Final CSE 131B Spring 2004

CPS 506 Comparative Programming Languages. Programming Language

Exercise: Singleton 1

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

The Stack & Procedures

G52CPP C++ Programming Lecture 13

Java: introduction to object-oriented features

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture Notes on Programming Languages

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

Machine-Level Programming III: Procedures

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Assembly Language: Function Calls

The Java Programming Language

C++ Important Questions with Answers

CSE 303: Concepts and Tools for Software Development

Chapter 5 Object-Oriented Programming

Class, Variable, Constructor, Object, Method Questions

Inheritance, Polymorphism and the Object Memory Model

Structs and Alignment CSE 351 Spring

The Stack & Procedures

What about Object-Oriented Languages?

Arrays Classes & Methods, Inheritance

Run-Time Data Structures

C++ Programming: Polymorphism

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

OBJECT ORİENTATİON ENCAPSULATİON

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

History C++ Design Goals. How successful? Significant constraints. Overview of C++

C++ Yanyan SHEN. slide 1

Object Oriented Programming. Java-Lecture 11 Polymorphism

Java Fundamentals (II)

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Programming Languages and Techniques (CIS120)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Inheritance and Interfaces

Code Generation & Parameter Passing

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

University of Washington

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Implementing Higher-Level Languages. Quick tour of programming language implementation techniques. From the Java level to the C level.

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Overriding Variables: Shadowing

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Machine-level Programs Procedure

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Assembly Programming IV

CS429: Computer Organization and Architecture

CS453 CLASSES, VARIABLES, ASSIGNMENTS

Object typing and subtypes

More Relationships Between Classes

Inheritance and Polymorphism

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Areas for growth: I love feedback

Objects, Encapsulation, Inheritance (2)

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

CSE 504: Compiler Design. Runtime Environments

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

Java Object Oriented Design. CSC207 Fall 2014

JAVA: A Primer. By: Amrita Rajagopal

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

CSE 401/M501 Compilers

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Practice for Chapter 11

Transcription:

CSE 401/M501 Compilers Code Shape II Objects & Classes Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 L-1

Administrivia Semantics/type check due next Thur. 11/15 How s it going? Reminder: if you want to use late days (max 2 per assignment, max 4 overall), both partners need to have them available and both are charged if used Sections this week: AST semantics checks & debugging thereof, interpreters Codegen part of the project out late next week Project-specific details in sections next week UW CSE 401/M501 Autumn 2018 K-2

Agenda Object representation and layout Field access What is this? Object creation - new Method calls Dynamic dispatch Method tables Super Runtime type information (As before, more generality than we actually need for the project) UW CSE 401/M501 Autumn 2018 L-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()); UW CSE 401/M501 Autumn 2018 L-4

Your Answer Here UW CSE 401/M501 Autumn 2018 L-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 (shadows) superclass instance but the superclass field is still there Methods declared in its class and 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 really don t want to copy all those methods, do we?) UW CSE 401/M501 Autumn 2018 L-6

Actual representation Each object contains: Storage for every field (instance variable) Including all inherited fields (public or private or ) A pointer to a runtime data structure for its class Key component: method dispatch table (next slide) An object is basically a C struct Fields hidden (shadowed) by declarations in subclasses are still allocated in the object and are accessible from superclass methods (using offsets assigned as part of superclass object layout) Subclass methods access new fields using offsets assigned when subclass fields appended to superclass struct layout UW CSE 401/M501 Autumn 2018 L-7

Method Dispatch Tables One of these per class, not per object Often called vtable, vtbl, or vtab (virtual function table term from C++, but standard in all languages with dynamic dispatch) One pointer per method points to beginning of method code Dispatch table (vtable) offsets fixed at compile time UW CSE 401/M501 Autumn 2018 L-8

Method Tables and Inheritance An initial, really simple implementation Method table for each class containing names of all methods declared in the class and pointers from each name to compiled code for the method Method table also contains a pointer to parent class method table Method dispatch Look in current table and use if method declared locally Look in parent class table if not local Repeat Message not understood if you can t find it after search Actually used in typical implementations of some dynamic languages (e.g. SmallTalk, Ruby, etc.) UW CSE 401/M501 Autumn 2018 L-9

O(1) Method Dispatch Idea: First part of method table for extended class has pointers for the same methods in the same order as the parent class BUT pointers actually refer to overriding methods if these exist \ Method dispatch can be done with indirect jump using fixed offsets known at compile time O(1) In C: *(object->vtbl[offset])(parameters) Pointers to additional methods defined (added) in subclass are included in the vtable after pointers to superclass methods UW CSE 401/M501 Autumn 2018 L-10

Method Dispatch Footnotes Don t need a vtable pointer to parent class vtable for method calls, but often useful for other purposes Casts and instanceof Multiple inheritance requires more complex mechanisms Also true for multiple interfaces UW CSE 401/M501 Autumn 2018 L-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()); UW CSE 401/M501 Autumn 2018 L-12

Implementation UW CSE 401/M501 Autumn 2018 L-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 Runtime type information instanceof and casts UW CSE 401/M501 Autumn 2018 L-14

Object Layout Typically, allocate fields sequentially Follow processor/os alignment conventions for structs/objects when appropriate/available Include padding bytes for alignment as needed Use first word of object for pointer to method table/class information Objects are allocated on the heap (in Java) No actual storage bits in the generated code UW CSE 401/M501 Autumn 2018 L-15

Object Field Access Source int n = obj.fld; x86-64 Assuming that obj is a local variable in the current method s stack frame movq offset obj (%rbp),%rax movq offset fld (%rax),%rax movq %rax,offset n (%rbp) # load obj ptr # load fld # store n (assignment stmt) Same idea used to reference fields of this Use implicit this parameter passed to method instead of a local variable to get object address UW CSE 401/M501 Autumn 2018 L-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 explicitly A pointer to the object (i.e., this ) is an implicit, hidden parameter to all methods UW CSE 401/M501 Autumn 2018 L-17

Source Level View What you write: int getit() { return it; } void setit(int it) { this.it = it; } obj.setit(42); k = obj.getit(); What you really get: int getit(objtype this) { return this.it; } void setit(objtype this, int it) { this.it = it; } setit(obj, 42); k = getit(obj); UW CSE 401/M501 Autumn 2018 L-18

x86-64 this Convention (C++) this is an implicit first parameter to every non-static method Address of object ( this ) placed in %rdi for every non-static method call Remaining parameters (if any) in %rsi, etc. We ll use this convention in our project UW CSE 401/M501 Autumn 2018 L-19

MiniJava Method Tables (vtbls) Generate these as initialized data in the assembly language source program Need to pick a naming convention for assembly language labels; suggest: For methods, classname$methodname Would need something more sophisticated for overloading For the vtables themselves, classname$$ First method table entry points to superclass table (we might not use it in our project, but is helpful if you add instanceof or type cast checks) UW CSE 401/M501 Autumn 2018 L-20

Method Tables For Perverse Example (gcc/as syntax) class One { void settag() { } int gettag() { } void setit(int it) { } int getit() { } } class Two extends One { void settag() { } int getthat() { } void resetit() { } }.data One$$:.quad 0 # no superclass.quad One$setTag.quad One$getTag.quad One$setIt.quad One$getIt Two$$:.quad One$$ # superclass.quad Two$setTag.quad One$getTag.quad One$setIt.quad One$getIt.quad Two$getThat.quad Two$resetIt UW CSE 401/M501 Autumn 2018 L-21

Method Table Layout Key point: First entries in Two s method table are pointers to methods in exactly the same order as in One s method table Actual pointers reference code appropriate for objects of each class (inherited or overridden) \ Compiler knows correct offset for a particular method pointer regardless of whether that method is overridden and regardless of the actual (dynamic) type of the object UW CSE 401/M501 Autumn 2018 L-22

Object Creation new Steps needed Call storage manager (malloc or equivalent) to get the raw bits Initialize bytes to 0 (for Java, not in e.g., C++) Store pointer to method table (vtbl) in the first 8 bytes of the object Call a constructor with this pointer to the new object in %rdi and other parameters as needed (Not in MiniJava since we don t have constructors) Result of new is a pointer to the new object UW CSE 401/M501 Autumn 2018 L-23

Object Creation Source One one = new One( ); x86-64 movq $nbytesneeded,%rdi # obj size + 8 (include space for vtbl ptr) call mallocequiv # addr of allocated bytes returned in %rax <zero out allocated object, or use calloc instead of malloc to get bytes> leaq One$$,%rdx # get method table address movq %rdx,0(%rax) # store vtbl ptr at beginning of object movq %rax,%rdi # set up this for constructor movq %rax,offset temp (%rbp) # save this for later (or maybe pushq) <load constructor arguments> # arguments (if needed) call One$One # call ctor if we have one (no vtbl lookup) movq offset temp (%rbp),%rax # recover ptr to object movq %rax,offset one (%rbp) # store object reference in variable one UW CSE 401/M501 Autumn 2018 L-24

Constructor Why don t we need a vtable lookup to find the right constructor to call? Because at compile time we know the actual class (it says so right after new ), so we can generate a call instruction to a known label Same with super.method( ) or superclass constructor calls at compile time we know all of the superclasses (need this to compile subclass and construct method tables), so we know statically which class super.method belongs to UW CSE 401/M501 Autumn 2018 L-25

Method Calls Steps needed Parameter passing: just like an ordinary C function, except load a pointer to the object in %rdi as the first ( this ) argument Get a pointer to the object s method table from the first 8 bytes of the object Jump indirectly through the method table UW CSE 401/M501 Autumn 2018 L-26

Method Call Source obj.method( ); x86-64 <load arguments in registers as usual> # as needed movq offset obj (%rbp),%rdi # first argument is obj ptr ( this ) movq 0(%rdi),%rax # load vtable address into %rax call *offset method (%rax) # call function whose address is at # the specified offset in the vtable * *Can get same effect with: or with: addq $offset method,%rax call *(%rax) movq $offset method (%rax),%rax call *%rax UW CSE 401/M501 Autumn 2018 L-27

Runtime Type Checking We can use the method table for the class as a runtime representation of the class Each class has one vtable at a unique address The test for o instanceof C is Is o s method table pointer == &C$$? If so, result is true Recursively, get pointer to superclass method table from the method table and check that Stop when you reach Object (or a null pointer, depending on whether there is a ultimate superclass of everything) If no match by the top of the chain, result is false Same test as part of check for legal downcast (e.g., how to check for ClassCastException on (type)obj cast) UW CSE 401/M501 Autumn 2018 L-28

Coming (& past) Attractions Simple code generation for the project (sections & [if needed] lectures next week) Then more compiler topics: Other IRs besides ASTs Industrial-strength register allocation, instruction selection, and scheduling Survey of code optimization Dynamic languages? JVM? What else? UW CSE 401/M501 Autumn 2018 L-29