Typical Compiler. Ahead- of- time compiler. Compilers... that target interpreters. Interpreter 12/9/15. compile time. run time

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

Java and C CSE 351 Spring

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

Java and C. CSE 351 Autumn 2018

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

Classes. Compiling Methods. Code Generation for Objects. Implementing Objects. Methods. Fields

Object Oriented Languages. Hwansoo Han

Winter Compiler Construction T9 IR part 2 + Runtime organization. Announcements. Today. Know thy group s code

Implementing Object-Oriented Languages. Implementing instance variable access. Implementing dynamic dispatching (virtual functions)

Compiler construction

Data, memory. Pointers and Dynamic Variables. Example. Pointers Variables (or Pointers) Fall 2018, CS2

Programming Language Dilemma Fall 2002 Lecture 1 Introduction to Compilation. Compilation As Translation. Starting Point

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

Procedure and Object- Oriented Abstraction

Announcements. CSCI 334: Principles of Programming Languages. Lecture 19: C++

Object typing and subtypes

Introduction to Programming Using Java (98-388)

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

CS S-11 Memory Management 1

Classes. Code Generation for Objects. Compiling Methods. Dynamic Dispatch. The Need for Dispatching CS412/CS413

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

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

Semantic Analysis and Type Checking

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

The Java Programming Language

Bidirectional Object Layout for Separate Compilation

Module Mechanisms CS412/413. Modules + abstract types. Abstract types. Multiple Implementations. How to type-check?

Overriding Variables: Shadowing

Semantics. Names. Binding Time

Java Fundamentals (II)

UW CSE 351, Winter 2013 Final Exam

CS 403 Compiler Construction Lecture 8 Syntax Tree and Intermediate Code Generation [Based on Chapter 6 of Aho2] This Lecture

C++ Inheritance and Encapsulation

Simple example. Analysis of programs with pointers. Program model. Points-to relation

The Java Language Implementation

JAVA MOCK TEST JAVA MOCK TEST II

Compiler construction 2009

CSE 401/M501 Compilers

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

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

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

CSE351 Winter 2016, Final Examination March 16, 2016

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Java Object Oriented Design. CSC207 Fall 2014

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Short Notes of CS201

CS 231 Data Structures and Algorithms, Fall 2016

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

Solutions - Homework 2 (Due date: February 5 5:30 pm) Presentation and clarity are very important! Show your procedure!

Inheritance (IS A Relationship)

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

CS201 - Introduction to Programming Glossary By

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

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

Building Interpreters

Generic programming POLYMORPHISM 10/25/13

ECE 573 Midterm 1 September 29, 2009

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

Pointers CMPE-013/L. Pointers. Pointers What do they do? Pointers What are pointers? Gabriel Hugh Elkaim Winter 2014

Inheritance, Polymorphism and the Object Memory Model

CS 314H Honors Data Structures Fall 2017 Programming Assignment #6 Treaps Due November 12/15/17, 2017

Lectures 5-6: Introduction to C

COP 3330 Final Exam Review

Java: framework overview and in-the-small features

Derived and abstract data types. TDT4205 Lecture 15

Introducing C++ David Chisnall. March 15, 2011

C a; C b; C e; int c;

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

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

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

From C++ to Java. Duke CPS

Just-In-Time Compilation

CS 251 Intermediate Programming Java Basics

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

G Programming Languages - Fall 2012

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

CS 314H Algorithms and Data Structures Fall 2012 Programming Assignment #6 Treaps Due November 11/14/16, 2012

Java Primer 1: Types, Classes and Operators

15CS45 : OBJECT ORIENTED CONCEPTS

It s Not Complex Just Its Solutions Are Complex!

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

C++ Inheritance and Encapsulation

SSA Based Mobile Code: Construction and Empirical Evaluation

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS260 Intro to Java & Android 03.Java Language Basics

Advanced Systems Programming

CSCI 355 LAB #2 Spring 2004

1 Shyam sir JAVA Notes

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Programming Exercise 14: Inheritance and Polymorphism

Lectures 5-6: Introduction to C

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Optional: Building a processor from scratch

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance and object compatibility

C++ Part 2 <: <: C++ Run-Time Representation. Smalltalk vs. C++ Dynamic Dispatch. Smalltalk vs. C++ Dynamic Dispatch

Transcription:

Ahead- of- time Tpical Compiler compile time C source C 86 assembl 86 assembler 86 machine Source Leical Analzer Snta Analzer Semantic Analzer Analsis Intermediate Code Generator Snthesis run time 86 machine 86 computer Code Optimizer Code Generator Figures for s/runtime sstems adapted from slides b Steve Freund. 1 Target 2 Interpreter Compilers... that target interpreters Source source Compiler btecod e Interpreter = virtual machine bte Virtual Machine 3 5 1

Interpreters... that use s. JIT Compilers and Optimization Source Compiler Target Virtual Machine 7 source javac bte just-in-time bte Performance Monitor bte interpreter JVM 86 machine HotSpot JVM Jikes RVM SpiderMonke v8 Trans meta 8... Virtual Machine Model On translation, laout, and implementation We show natural, common, or conventional translations. Bte compile time run time High- Level Language Virtual Machine Language Ahead- of- time : No guarantee of this implementation/laout. Language is (mostl clean) abstraction. C: Much of implementation/laout guaranteed. Language eposes man machine details. Virtual machine (interpreter) JIT Native Machine Language 9 2

in Integers, floats, doubles, pointers same as C Null is tpicall represented as 0 Characters and strings Arras Objects pointers? called references much more constrained in Arras Ever element initialized to 0 or null Length specified in immutable field at start of arra (int 4 btes) arra.length returns value of this field Since it has this info, what can it do? int arra[5]: C?????????? 0 4 20 24 5 00 00 00 00 00 Representation in Representation in in Arras Ever element initialized to 0 or null Length specified in immutable field at start of arra (int 4 btes) arra.length returns value of this field Ever access triggers a bounds- check Code is added to ensure the inde is within bounds Eception if out- of- bounds int arra[5]: C?????????? 0 4 20 24 5 00 00 00 00 00 Bounds- checking sounds slow, but: 1. Length is likel in cache. 2. Compiler ma store length in register for loops. 3. Compiler ma prove that some checks are redundant. in Characters and strings Two- bte Uni instead of ASCII Represents most of the world s alphabets String not bounded b a \0 (null character) Bounded b hidden length field at beginning of string the string CS 240 : C: ASCII : Uni 43 53 20 32 34 30 \0 0 1 4 7 16 6 00 43 00 53 00 20 00 32 00 34 00 30 Representation in Representation in 3

structures (objects) in Objects are alwas stored b reference, never stored inline. Include comple data tpes (arras, other objects, etc.) using references Pointer/reference fields and variables In C, we have - > and. for field selection depending on whether we have a pointer to a struct or a struct (*r).a is so common it becomes r- >a C struct rec { int a[ 3]; struct rec *p; ; class Re c { int[] a = n ew int [3]; Rec p; In, all non- primitive variables are references to objects We alwas use r.a notation But reall follow reference to r with offset to a, just like C s r- >a struct rec *r = malloc(...); struct rec r2 ; r->i = val; r->a[2] = val ; r->p = & r2; r = new Rec() ; r2 = new Rec( ); r.i = va l; r.a[2] = val; r.p = r2 ; 0 4 16 20 Representation in 0 4 8 12 3 int[3] 0 4 16 Implementation Pointers/References Pointers in C can point to an memor address References in can onl point to [the starts of] objects And can onl be dereferenced to access a field or element of that object C struct rec { int a[ 3]; struct rec *p; ; struct rec* r = mal loc( ); some_fn(&(r.a[1])) //pt r r 0 4 16 20 class Re c { int[] a = n ew int [3]; Rec p; Rec r = new R ec(); some_fn( r.a, 1) // ref, inde r 0 4 8 12 Representation in 0 3 int[3] 4 16 Casting in C We can cast an pointer into an other pointer; just look at the same bits differentl struct BlockInfo { int size AndTa gs; struct BlockInfo* n et; struct BlockInfo* prev; ; tpedef struc t Bloc kinfo Block Info; int ; BlockInf o *b; BlockInf o *ne wblock ; newblock = (BlockInfo *) ( (ch ar *) b + ); s n p 0 4 8 12 s n Implementation p Cast b into char pointer so that ou can add bte offset without scaling Cast back into BlockInfo pointer so ou can use it as BlockInfo struct 4

Tpe- safe casting in objects Can onl cast compatible object references class Object { class Vehicle { int passengers; class Boat etends Vehicle { int propellers; class Car etends Vehicle { int wheels; // Vehicle is a super class of Boat and Car, which are siblings Vehicle v = new Vehicle(); Car c1 = new Car(); Boat b1 = new Boat(); Vehicle v1 = new Car(); // ok, everthing needed for Vehicle // is also in Car Vehicle v2 = v1; // ok, v1 is alread a Vehicle Car c2 = new Boat(); // incompatible tpe Boat and // Car are siblings Car c3 = new Vehicle(); // wrong direction; elements in Car // not in Vehicle (wheels) Boat b2 = (Boat) v; // run-time error; Vehicle does not contain // all elements in Boat (propellers) Car c4 = (Car) v2; // ok, v2 started out as Car Car c5 = (Car) b1; // incompatible tpes, b1 is Boat class Point { int ; int ; Point() { = 0; = 0; boolean sameplace(point p) { return ( == p.) && ( == p.); String tostring() { return "(" + + "," + + ")"; fields constructor methods How is this implemented / enforced? 20 objects : Poin t() Implementing dnamic dispatch : Poin t() p vtable pointer Point class vtable : Poin t.sa mepl ace() p vtable pointer Point class vtable : Poin t.sa mepl ace() : Poin t.to Stri ng() : Poin t.to Stri ng() vtable pointer q For each class, maps: field signature à offset (inde) vtable pointer : points to per- class virtual method table (vtable) For each class, maps: method signature à inde sameplace: 0 tostring: 1 22 vtable pointer q q what happens (pseudo ): : Point* p = calloc(1,sizeof(point)); Point p = new Point(); p->header =...; p->vtable = &Point_vtable; Point_constructor(p); return p.sameplace(q); return p.vtable[0](this=p, q); 5

Subclassing class ColorPoint etends Point{ String color; boolean getcolor() { return color; String tostring() { return super.tostring() + "[" + color + "]"; How do we access superclass pieces? fields inherited methods Where do we put etensions? new field new method overriding method dnamic (method) dispatch : Point p =???; return p.tostring(); vtable Color ColorPoint vtable vtable color what happens (pseudo ): return p.vtable[1](p); Point vtable : Poin t.sa mepl ace() : Poin t.to Stri ng() : Colo rpoi nt.t ostr ing() : Colo rpoi nt.g etco lor() 6