CS263: Runtime Systems Lecture: High-level language virtual machines

Similar documents
CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

CS 32. Lecture 1: oops

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

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

Java Object Oriented Design. CSC207 Fall 2014

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Overview. Elements of Programming Languages. Objects. Self-Reference

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

Lecture 02, Fall 2018 Friday September 7

CS-202 Introduction to Object Oriented Programming

Overview. Elements of Programming Languages. Objects. Self-Reference

What is Inheritance?

Dynamically-typed Languages. David Miller

Data Abstraction. Hwansoo Han

Chapter 2: Java OOP I

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

CS558 Programming Languages

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

CSE 401/M501 Compilers

CS 251 Intermediate Programming Inheritance

CSE 431S Type Checking. Washington University Spring 2013

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

Rules and syntax for inheritance. The boring stuff

C++ Programming: Polymorphism

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

Arrays Classes & Methods, Inheritance

Object-Oriented Concepts

Object Model Comparisons

Java: introduction to object-oriented features

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

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

Object Model. Object Oriented Programming Spring 2015

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Topic 9: Type Checking

Topic 9: Type Checking

ECE 2400 / ENGRD 2140 Computer Systems Programming Course Overview

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

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

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Chapter 5 Object-Oriented Programming

Practice for Chapter 11

Principles of Object Oriented Programming. Lecture 4

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Chapter 14 Abstract Classes and Interfaces

Lecture 4: Extending Classes. Concept

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

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Polymorphism. Agenda

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

The Java Language Implementation

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

Chapter 9 Subprograms

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

COMP322 - Introduction to C++

CS153: Compilers Lecture 11: Compiling Objects

Introduction to Java

CMSC 330: Organization of Programming Languages

Exercise: Singleton 1

301AA - Advanced Programming [AP-2017]

CS558 Programming Languages

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Lecture 1: Overview of Java

Semantic Analysis. Lecture 9. February 7, 2018

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

The Pyth Language. Administrivia

Programming overview

CSE 307: Principles of Programming Languages

invokedynamic under the hood

CSE 421 Course Overview and Introduction to Java

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

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

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Chapter 9. Subprograms

Binghamton University. CS-140 Fall Dynamic Types

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

CS 231 Data Structures and Algorithms, Fall 2016

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

CS260 Intro to Java & Android 03.Java Language Basics

CSC 1214: Object-Oriented Programming

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

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

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Chapter 9 Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

Lecture 8: JavaScript

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Introduction to Java. Handout-1d. cs402 - Spring

Delft-Java Link Translation Buffer

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

G Programming Languages - Fall 2012

Subtyping (Dynamic Polymorphism)

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses:

Transcription:

CS263: Runtime Systems Lecture: High-level language virtual machines Today: A Review of Object-oriented features Chandra Krintz UCSB Computer Science Department

Virtual machines (VMs) Terminology Aka managed runtime environments (MREs) Aka [high-level] language runtime (runtime) Vernacular Java: JVMs,.Net: Common language runtime (CLR), Python/others: runtime

Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java bytecode cls2.class

Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent static compiler (Ex: g++) executable code linkable code linker executable code Java bytecode cls2.class

Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Translation from bytecode to native machine code executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization

Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Interpreter and/or dynamic compiler executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization

Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Interpreter and/or dynamic compiler executable code static compiler (Ex: g++) executable code linkable code linker executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization

C# Architecture-independent Format called bytecode Dynamically loaded / executed by a common language runtime (e.g. mono) File.cs class cls1 { } class cls2 { } source compiler (mcs File.cs) cls1.exe MSIL bytecode architecture-independent architecture-dependent Common Language Runtime Translation from bytecode to native machine code executable code MSIL bytecode cls2.exe Runtime environment program loading/verif. memory management thread/synchronization optimization

Python Architecture-independent Format called bytecode Dynamically loaded / executed by a python runtime (e.g. cpython) File.py Non-class code class cls1: source compiler (pyc File.py) cls1.pyc Python bytecode Or source code! architecture-independent architecture-dependent Python Runtime Translation from bytecode to native machine code executable code Runtime environment program loading/verif. memory management thread/synchronization optimization

Python Architecture-independent Format called bytecode Dynamically loaded / executed by a python runtime (e.g. cpython) File.py Non-class code class cls1: source compiler (pyc File.py) cls1.pyc Python bytecode architecture-independent architecture-dependent High-Level Lang. Runtime Translation from bytecode to native machine code executable code Runtime environment program loading/verif. memory management thread/synchronization optimization

OO Review Things you should already know (study/review if not) Static vs dynamic typing Type-safety, memory safety; vs type-inference Class Object Class/object members (aka attributes) Fields (data) Methods (code) Static fields vs instance fields Static methods vs instances methods Inheritance Dynamic dispatch of virtual instance methods Dynamically typed OO language features

Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time (C, C++, Java, C#, all below) -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name Object is of Type Type Object is of Type Type is associated with value and not known until runtime when the code executes

Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name runtime Object is of Type Type runtime Object is of Type Type is associated with value and not known until runtime when the code executes

Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name runtime In the code, known ahead of runtime runtime Object Type is of Type Object is of Type Type is associated with value and not known until runtime when the code executes

Static vs Dynamic Typing Variable Name STATIC Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name Object is of Type Type Must match (be compatible) Type Safe if this and operations on types are checked (statically if possible, dynamically required for some operations) and legal according to the type system. A type system generally tries to guarantee that operations are not used with values for which that operation does not make sense. Object is of Type Type is associated with value and not known until runtime when the code executes Checked at runtime

Example Languages Statically typed, not type safe or memory safe C, C++ Type-safe, memory safe, statically typed Java, C# & the.net languages, Scala, ML, Go, Swift If you don t specify the type, the compiler can infer it in many cases Memory safe = there is no way to access a memory location except through an object reference Cannot walk through memory arbitrarily Cannot dereference an object that is NULL Dynamically typed (most are sandboxed) Python, Ruby, Lua Javascript, PHP Perl, OS shell scripts

Java Classes/Objects class MyMainClass { public static void main(string argv[]) { MyClass A = new MyClass(); } } Object = An instance of a class def. Class members Methods and fields Same for all objects of that class Declared using the static modifier class MyClass { int field1; int field2; static int field3; } void meth1 (int arg1) { } static void meth2(int arg1){ } MyClass() { } 18

Java Classes/Objects class MyMainClass { public static void main(string argv[]) { MyClass A = new MyClass(); } } Object = An instance of a class def. Class members Methods and fields Same for all objects of that class Declared using the static modifier class MyClass { int field1; int field2; static int field3; } void meth1 (int arg1) { } static void meth2(int arg1){ } MyClass() { } Instance members = non-static members (each object has own copy) Methods and fields Created & allocated when new is called Initialized when the constructor is called (MyClass() { } above) 19

Type-safe Object Conversions (Java/C#) A variable with a class type Can hold Objects of its own type or of subtypes of its own type Source compiler checks this statically (at compile time) Child c = new Parent(); // is not allowed class Parent { int field1; } class Child extends Parent { int field2; } Child c = new Child(); ((Parent)c).field1 = 2;// OK Child has Parent parts via inheritance c.field1 = 2; //also works (is the same as above in this case) 20

Inheritance and Static Fields & Methods Most languages (Java, C++, Python (fields only), others) Class/static fields and methods (Should be) accessed using the Class Name Parent.staticMethod(); Parent.staticField = 2; If accessed via an object, compiler uses the static (declared) or casted type of variable, & replaces the access with one like above. Parent obj = new Parent(); obj.staticfield = 2; //is same as above Members are inherited by subclasses: Child extends Parent System.err.println(Child.staticField); // output: 2

Inheritance and Static Fields & Methods Most languages (Java, C++, Python (fields only), others) Class/static fields and methods If Child has a field/method with the same name (e.g. staticfield) Both are available (Child hides parent s field): Child obj=new Child(); obj.staticfield = 2; Accessible via a cast ((Parent)obj).staticField = 4; or directly thru the parent s class name: Parent.staticField = 7; Hidden NOT overridden

Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Use casts or assign into a variable that has the type of the parent Child inherits from Parent Child obj = new Child(); obj.field1 = 7; //field1 is in both Parent/Child ((Parent)obj).field1 = 4; //field1 inher from Parent

Type-safe Object Conversions (Java/C#) A variable with a class type Can hold Objects of its own type or of subtypes of its own type Source compiler checks this statically (at compile time) Child c = new Parent(); // is not allowed Can be casted To a supertype The compiler checks compatibility at compile time To a subtype The compiler inserts runtime check to check the underlying object type when this code executes Checks that the underlying object type is 24 the one to which the cast is converting class Parent { int field1; } class Child extends Parent { int field1; } Parent p = Child c = ((Child)p).field1 = 7; ((Parent)c).field1 = 2;

Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Use casts or assign into a variable that has the type of the parent Child inherits from Parent Child obj = new Child(); obj.field1 = 7; //field1 is in both Parent/Child ((Parent)obj).field1 = 4; //field1 inher from Parent

Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Cast or assign into a variable that has the type of the parent Child inherits from Parent Parent pobj = getit(); Child obj = new Child(); pobj.field1 = 2; //field1 inher. from Parent obj.field1 = 7; //field1 is in both Parent/Child ((Child)pobj).field1 = 1; //error or which field1? ((Parent)obj).field1 = 4; //field1 inher from Parent //how does this work in Java? pobj = new Parent(); Hidden NOT overridden ((Child)pobj).field1 = 1; //what about this?

Inheritance and Instance Methods Most languages (Java, C++, Python, Others) Instance methods An object instance is required for access Accessible from object (lookup table referred to by object) Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch Dynamic dispatch

Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch - Make them like fields and have the child hide them (making them still accessible) we use the static/casted type determine which method to call C++,C# use this Parent obj = new Child(); obj.instancemethod(); //instancemethod is defined in both //calls which one? ((Child)obj).instanceMethod(); //calls which one?

Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch - Make them like fields and have the child hide them (making them still accessible) we use the static/casted type determine which method to call C++,C# use this Parent obj = new Child(); obj.instancemethod(); //instancemethod is defined in both //calls Parent s ((Child)obj).instanceMethod(); //calls Child s

Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Dynamic dispatch Look up the underlying object type at runtime to determine which method to call All instance methods in Java and Python use this Instance methods marked virtual in C++ and C# use this Parent obj = new Child(); obj.instancemethod(); ((Parent)obj).instanceMethod(); //instancemethod is defined in both //calls which one? //calls which one?

Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Dynamic dispatch Look up the underlying object type at runtime to determine which method to call All instance methods in Java and Python use this Instance methods marked virtual in C++ and C# use this Parent obj = new Child(); obj.instancemethod(); ((Parent)obj).instanceMethod(); //instancemethod is defined in both //calls Child s //calls Child s

Dynamically Typed OO Languages Python, Ruby, Javascript, Lua, Attributes/properties (fields and methods) are defined on the fly upon first write/definition Object == unordered set of key-value pairs Lookup of attributes is by name for every access Most OO languages employ class-based inheritance Javascript and Self use prototype inheritance Prototypes define a set of properties; prototypes are objects Objects can inherit their properties from a prototype (ie other objects) And then change them (diverge from prototype) Hidden classes (or maps) are used by the runtime to track which properties an object has any (control) point in the executing program