Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

Similar documents
CSE P 501 Compilers. Dynamic Languages Hal Perkins Winter UW CSE P 501 Winter 2016 X2-1

Dynamically-typed Languages. David Miller

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

Intermediate Code, Object Representation, Type-Based Optimization

Simula 67. Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula

Objects. Deian Stefan (Adopted from my & Edward Yang s CS242 slides)

Object Model. Object Oriented Programming Spring 2015

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

Outline. Central concepts in OO languages Objects as activation records (Simula) Dynamically-typed object-oriented languages

Principles of Programming Languages

Inheritance, Polymorphism and the Object Memory Model

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

CS558 Programming Languages Winter 2013 Lecture 8

Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula. John Mitchell

Object Model. Object Oriented Programming Winter

Data Abstraction. Hwansoo Han

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Compiler Optimization Intermediate Representation

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

CS558 Programming Languages

Subtyping (Dynamic Polymorphism)

LATENT METHODS. Richard CS

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

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

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

2. The object-oriented paradigm!

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CSE Compilers. Reminders/ Announcements. Lecture 15: Seman9c Analysis, Part III Michael Ringenburg Winter 2013

Some instance messages and methods

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Programming Languages and Techniques (CIS120)

Object Oriented Programming

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

Java and C CSE 351 Spring

Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

JSR 292 Cookbook: Fresh Recipes with New Ingredients

Chapter 9 :: Data Abstraction and Object Orientation

High-Level Synthesis Creating Custom Circuits from High-Level Code

Design Pa*erns. + Anima/on Undo/Redo Graphics and Hints

CSE 504: Compiler Design. Runtime Environments

Baggy bounds checking. Periklis Akri5dis, Manuel Costa, Miguel Castro, Steven Hand

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Object Oriented Design (OOD): The Concept

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

ShortCut: Architectural Support for Fast Object Access in Scripting Languages

Ways to implement a language

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

Principles of Programming Languages

10/7/15. MediaItem tostring Method. Objec,ves. Using booleans in if statements. Review. Javadoc Guidelines

2. The object-oriented paradigm

Konzepte von Programmiersprachen

Computer Organization & Assembly Language Programming

Efficient Separate Compilation of Object-Oriented Languages

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

CS 32. Lecture 1: oops

Lecturer: William W.Y. Hsu. Programming Languages

JOVE. An Optimizing Compiler for Java. Allen Wirfs-Brock Instantiations Inc.

W1005 Intro to CS and Programming in MATLAB. Brief History of Compu?ng. Fall 2014 Instructor: Ilia Vovsha. hip://

RV: A Run'me Verifica'on Framework for Monitoring, Predic'on and Mining

Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

Java's Memory Management

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

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

CSc 453 Interpreters & Interpretation

Case Study: Meta Classes

Runtime Environments, Part II

CSE P 501 Compilers. Memory Management and Garbage Collec<on Hal Perkins Winter UW CSE P 501 Winter 2016 W-1

Concepts of Programming Languages

Polymorphism. Contents. Assignment to Derived Class Object. Assignment to Base Class Object

CS153: Compilers Lecture 11: Compiling Objects

Dynamic Languages Strike Back. Steve Yegge Stanford EE Dept Computer Systems Colloquium May 7, 2008

RaceMob: Crowdsourced Data Race Detec,on

The Procedure Abstraction

Chapter 10 :: Data Abstraction and Object Orientation

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Lecture Notes on Programming Languages

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Lecture 32: Pipeline Parallelism 3

Interprocedural Analysis with Data-Dependent Calls. Circularity dilemma. A solution: optimistic iterative analysis. Example

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

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

Java. Package, Interface & Excep2on

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

How to live with IP forever

Lecture 16: Object Programming Languages

Type Feedback for Bytecode Interpreters

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

An Introduction to Object Orientation

Computer Architecture: Mul1ple Issue. Berk Sunar and Thomas Eisenbarth ECE 505

Efficient Separate Compilation of Object-Oriented Languages

Ø Interface methods are public by default

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

What about Object-Oriented Languages?

Java: Past, Present, and Future. Brian Goetz Java Language Architect Oracle Corpora>on

Chapter 13. Object Oriented Programming

C++ (Non for C Programmer) (BT307) 40 Hours

Libraries are wri4en in C/C++ and compiled for the par>cular hardware.

New Programming Paradigms

Transcription:

Dynamic Languages CSE 501 Spring 15 With materials adopted from John Mitchell

Dynamic Programming Languages Languages where program behavior, broadly construed, cannot be determined during compila@on Types Code to be executed (eval in Javascript) Loading external libraries Language examples Javascript Python PHP Smalltalk Matlab

History of Self Prototype- based pure object- oriented language. Designed by Randall Smith (Xerox PARC) and David Ungar (Stanford) in 1987. Successor to Smalltalk- 80 Vehicle for implementa@on research Later implementa@on by Craig Chambers and others at Stanford This is the one we are studying

History of SELF Lisp Simula Smalltalk- 80 Self Java (VM) Javascript Lua

Design Goals Conceptual economy Everything is an object Everything done using messages No classes No variables Concreteness Objects should seem real GUI to manipulate objects directly

Language Overview Dynamically typed Users do not declare types All computa@on via message passing Objects are organized into slots Opera@ons on objects: send messages add new slots replace old slots remove slots

Objects and Slots Object consists of named slots. Data Such slots return contents upon evalua@on; so act like instance variables Assignment Set the value of associated slot Method Slot contains Self code Parent Point to exis@ng object to inherit slots

Messages and Methods When message is sent, object searched for slot with name. If none found, all parents are searched. Run@me error if more than one parent has a slot with the same name. If slot is found, its contents evaluated and returned. Run@me error otherwise clone parent* print parent* x 3 x:

Messages and Methods obj x 3 clone obj print print point object parent* obj x: 4 obj a-er se/ng x to 4. print parent* x 3 x:

Mixing State and Behavior parent* + add points parent* x 4 y 17 x: parent* x y random number generator o y: y:

Crea@ng Objects To create an object, we copy an old one We can add new methods, override exis@ng ones, or even remove methods as the program executes These opera@ons also apply to parent slots as well

Changing Parent Pointers frog jump prince dance eatfly eatcake p parent* p jump. p eatfly. p parent: prince. p dance. parent*: name name: Charles

Changing Parent Pointers frog jump prince dance eatfly eatcake p parent* p jump. p eatfly. p parent: prince. p dance. parent*: name name: Charles

Why no classes? Classes require programmers to understand a more complex model. To make a new kind of object, we have to create a new class first. To change an object, we have to change the class. Infinite meta- class regression. But: Does Self require programmer to reinvent structure? Common to structure Self programs with traits: objects that simply collect behavior for sharing.

Contrast with C++ C++ Restricts expressiveness to ensure efficient implementa@on Class hierarchy is fixed during development Self Provides high- level abstrac@on of underlying machine Compiler does fancy op@miza@ons to obtain acceptable performance

Implementation Challenges I Many, many slow function calls: Function calls generally expensive. Dynamic dispatch makes message invocation even slower than typical procedure calls. OO programs tend to have lots of small methods. Everything is a message: even variable access! The resulting call density of pure objectoriented programs is staggering, and brings naïve implementations to their knees [Chambers & Ungar, PLDI 89]

C++ Object Layout Point object Point class Template 2 3 x y Virtual method table ctor draw move... ColorPoint object 4 5 red ColorPoint class Template x y color Virtual method table ctor color draw parent ctor

Naive Self Object Layout ColorPoint object Point object x y color ctor draw 4 5 red x y ctor move 4 5 move

Implementa@on Challenges II No sta@c type system Each reference could point to any object, making it hard to find methods sta@cally. No class structure to enforce sharing Each object having a copy of its methods leads to space overheads. Op@mized Smalltalk- 80 roughly 10 @mes slower than op@mized C

Op@miza@on Strategies Avoid per object space requirements Avoid interpre@ng Compile code instead Avoid method lookup Inline methods wherever possible Saves method call overhead Enables further op@miza@ons

Clone Families Avoid per object data Model Implementa@on prototype Mutable Fixed map Fixed Info clone family Mutable Mutable Mutable Mutable Fixed Fixed Fixed Fixed Mutable Mutable Mutable Map Mutable Map Map Map

Dynamic Compila@on Avoid interpre@ng Source Byte Code Machine Code obj x: 4! obj y: 10 Method is entered LOAD R0! MOV R1 2! ADD R1 R2!,,, First method execu@on 010010100 100110001 001011010 00110 Method is converted to byte codes when entered Compiled to machine code when first executed Code stored in cache if cache fills, previously compiled method flushed Requires en@re source (byte) code to be available

Lookup Cache Avoid method lookup Cache of recently used methods, indexed by (receiver type, message name) pairs. When a message is sent, compiler first consults cache if found: invokes associated code if absent: performs general lookup and poten@ally updates cache

Sta@c Type Predic@on Avoid method lookup Compiler predicts types that are unknown but likely: Arithme@c opera@ons (+, -, <, etc.) have small integers as their receivers 95% of @me in Smalltalk- 80. iftrue had Boolean receiver 100% of the @me. Compiler inlines code (and test to confirm guess): if type = smallint jump to method_smallint! else call general_lookup!

Inline Caches Avoid method lookup First message send from a call site : general lookup rou@ne invoked call site back- patched is previous method s@ll correct? yes: invoke code directly no: proceed with general lookup & backpatch Successful about 95% of the @me All compiled implementa@ons of Smalltalk and Self use inline caches

Polymorphic Inline Caches Avoid method lookup Typical call site has <10 dis@nct receiver types So olen can cache all receivers At each call site, for each new receiver, extend patch code: if type = rectangle jump to method_rect! if type = circle jump to method_circle! call general_lookup! Aler some threshold, revert to simple inline cache (megamorphic site) Order clauses by frequency Inline short methods

Customized Compila@on Inline methods Compile several copies of each method, one for each receiver type Within each copy: Compiler knows the type of self Calls through self can be sta@cally selected and inlined Enables downstream op@miza@ons Increases code size

Type Analysis Inline methods Constructed by compiler by flow analysis. Type: set of possible maps for object Singleton: know map sta@cally Union/Merge: know expression has one of a fixed collec@on of maps. Unknown: know nothing about expression. If singleton, we can inline method. If type is small, we can insert type test and create branch for each possible receiver (type casing)

Performance Improvements Ini@al version of Self was 4-5 @mes slower than op@mized C. Aler op@miza@ons, implementa@on described in paper is within a factor of 2 of op@mized C.

How successful? Few users: not a popular success No compelling applica@on, un@l JavaScript Influenced development of object calculi w/o classes However, many research innova@ons Very simple computa@onal model Enormous advances in compila@on techniques Influenced the design of Java compilers Direct influence on design of Javascript

Lessons Pochoir / Halide (DSL) Design specific constructs for domain Constructs need to easily map to underlying target language Otherwise implementa@on might be a nightmare Expose high- level structure allows domain- specific op@miza@ons Self / Javascript (Dynamic Languages) Power of simplicity Everything is an object No classes, no variables Implementa@on specific to program constructs Uses various op@miza@on tricks to recover performance