SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Similar documents
CSCE 314 Programming Languages

Compiling for Different Platforms. Problem: Too Many Platforms! Dream: Platform Independence. Java Platform 5/3/2011

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

Reverse Engineering of Managed Languages

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Let s make some Marc R. Hoffmann Eclipse Summit Europe

Compiling Techniques

301AA - Advanced Programming [AP-2017]

CSC 4181 Handout : JVM

SOFTWARE ARCHITECTURE 6. LISP

Run-time Program Management. Hwansoo Han

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

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

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

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

Compiler construction 2009

JAM 16: The Instruction Set & Sample Programs

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

CSc 453 Interpreters & Interpretation

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

301AA - Advanced Programming

The Java Virtual Machine

Improving Java Performance

Virtual Machine Design

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

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

Java Class Loading and Bytecode Verification

A Tour of Language Implementation

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

CSE 431S Final Review. Washington University Spring 2013

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac.

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

COMP3131/9102: Programming Languages and Compilers

Chapter 5. A Closer Look at Instruction Set Architectures

Improving Java Code Performance. Make your Java/Dalvik VM happier

Joeq Analysis Framework. CS 243, Winter

Compiler construction 2009

Java: framework overview and in-the-small features

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

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

The Java Virtual Machine

Problem: Too Many Platforms!

Recap: Printing Trees into Bytecodes

02 B The Java Virtual Machine

Tutorial 3: Code Generation

Static Program Analysis

High-Level Language VMs

Topics. Structured Computer Organization. Assembly language. IJVM instruction set. Mic-1 simulator programming

Code Generation Introduction

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Chapter 5. A Closer Look at Instruction Set Architectures

JoeQ Framework CS243, Winter 20156

Bytecode Support for the Universe Type System. Alex Suzuki

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

Java byte code verification

The Java Language Implementation

An Introduction to Multicodes. Ben Stephenson Department of Computer Science University of Western Ontario

Portable Resource Control in Java The J-SEAL2 Approach

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

The Hardware/Software Interface CSE351 Spring 2015

Introduction to Programming Using Java (98-388)

Translating JVM Code to MIPS Code 1 / 43

Design and Implementation of Pep, a Java Just-In-Time Translator

Compilation 2012 The What and Why of Compilers

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

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Plan for Today. Safe Programming Languages. What is a secure programming language?

Concepts of Programming Languages

Shared Mutable State SWEN-220

Program Dynamic Analysis. Overview

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3]

CMPSC 497: Java Security

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Course Overview. Levels of Programming Languages. Compilers and other translators. Tombstone Diagrams. Syntax Specification

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

Computer Components. Software{ User Programs. Operating System. Hardware

Portable Resource Control in Java: Application to Mobile Agent Security

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

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Intermediate Representations

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Context Threading: A flexible and efficient dispatch technique for virtual machine interpreters

Programming Languages, Summary CSC419; Odelia Schwartz

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Classes, subclasses, subtyping

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

COMP 321: Introduction to Computer Systems

Java Primer 1: Types, Classes and Operators

Taming the Java Virtual Machine. Li Haoyi, Chicago Scala Meetup, 19 Apr 2017

LECTURE 18. Control Flow

C++ Inheritance and Encapsulation

Java Internals. Frank Yellin Tim Lindholm JavaSoft

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

STRUCTURING OF PROGRAM

Code Profiling. CSE260, Computer Science B: Honors Stony Brook University

Concepts Introduced in Chapter 7

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

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

Transcription:

1 SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/

Java Programming Language Java Introduced in 1995 Object-oriented programming language Developed for embedding into small devices C like syntax no goto statement C object-oriented 1950 1960 1970 1980 1990 2000 1957 FORTRAN 1960 COBOL 1964 BASIC 1966 PL/I 2 1958 LISP, ALGOL 1969 PASCAL 1970 B 1972 C, Prolog 1974 ML 1975 Scheme 1977 SQL 1979 Ada 1980 Smalltalk 1983 C++, Objective-C 1984 Common Lisp, Postscript 1987 Perl 1990 Python, Haskell 1991 Microsoft Visual Basic 1993 Ruby, Lua 1995 Java, PHP 1997 Javascript 2000 C# 2003 Scala C++ Objective-C Java 2010 2009 Go 2011 Dart 2014 Swift 2020

3 Object-oriented Programming Language Software = a set of objects Objects send messages to other objects for communication. Set of procedures Set of objects data object data object data function call procedure call message message function procedure object object data data non object-oriented Procedures and data are independent A procedure is a set of instructions. Procedure centered object-oriented Each object has data Objects receives messages to manipulate data. Data centered

4 Features of object-oriented programming Encapsulation Data are encapsulated in a object. Use methods to access data. Abstract data type vs concrete data type An object consist of data and methods. Abstract data type array use array to implement queue method array Encapsulation method method object data data data are hidden use public methods to manipulate data put get LIFO = Last In First Out put get FIFO = First In First Out

Features of object-oriented programming 5 Class and Inheritance Each object belongs to a class. The object is an instance of the class. Classes have parent-child relation. Child class inherits parent class. Parent methods are child methods. Parent data are child data. Class hierarchy Parent-child relation creates class hierarchy. car parent Super Class child Sub Class inheritance 4 wheel 3 wheel 2 wheel 1 wheel large small motorcycle bicycle truck bus compact car

6 Java Program Example class name local data public class Human { private String name; constructor for objects public Human(String name) { this.name = name; refer itself public String getname() { return name; method parent class public class Student extends Human { private int id; public Student(int id, String name) { this.id = id; Human(name); public int getid() { return id; public static void main(string[] args) { Student s = new Student(12345, "Hagino"); System.out.println(s.getName()); invoke a method Human Class for human Holds a name as its data Student Class for students Subclass of Human Holds a student id number as its data. Create a Student object. Use getname to get the name and print it out. create a new object

7 Java Class Class declaration specify parent class specify implementing interface declare data declare constructors declare methods Class variables shared data among objects in the class declare data with static Class methods cannot refer objects methods for the class public class class declare data constructors methods object object variable x class name extends parent class implements interface { class variable c share data for this class use to create new objects manipulate data of the object object object variable x

8 extends vs implements Can a class has more than one parent? inherit properties from two or more parent classes. 4 wheel 2 wheel car gas auto electric non auto man power Multiple inheritance diamond problem Which methods to inherit gaspowered vehicle electric vehicle motorcycle electric bicycle bicycle inherit class A method m inherit class B override m inherit diamond problem class D which m? class C override m inherit Solution in Java Only one parent (use extends) Use interface to have multiple features. interface = class without implementation

9 Java Virtual Machine Speed vs Portability Compile Java programs to JVM (Java Virtual Machine) machine programs JVM can be implemented on various architectures. Compiler + Interpreter Java translate JVM machine language execute various CPU source program Java compiler object program Java virtual machine Java Virtual Machine Virtual (i.e. not physically exists) ideally thought CPU Designed for Java compilation Implements Java objects Use GC to manage memory Java JVM javac JVM M

Implementation of Virtual Machine 10 Virtual Machine Virtual CPU Simpler than real CPU Model ideal CPU Usually byte machine Implement virtual machine Emulate the virtual CPU Get an instruction pointed by PC execute the instruction for (;;) { op = *pc++; switch (op) { case ILOAD: index = *pc++; *--sp = fp[index]; break; case IADD: v1 = *sp++; v2 = *sp; *sp = v1 + v2; break;... PC: Program Counter advance PC to the next

11 Structure of Java Virtual Machine JVM codes are stored in a class file. Actual codes are stored in methods. ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; abc.java class abc {... compile javac abc.class

12 JVM Data Type Primitive data type integer floating point boolean returnaddress Reference data type Objects primitive data integer int x = 1; x.add(3); object integer Integer x = new Integer(1); x.add(3); No add is actually implemented. Java is not pure object-oriented. efficiency vs pure Primitive data types are not objects. Objects for primitive data

13 Structure of JVM PC register Points to code of the currently executing method. returnaddress data type Virtual machine Holds frames which are created by method invokation Pushed on to Discarded when the method finishes. Heap Shared by all the threads. Holds objects and arrays. PC Method area VM value frame value frame Constant pool Heap object object native method Method area Shared by all the threads. Holds codes for methods. Constant pool Classes and interfaces have constant pool. Constants and information of methods and fields are stored. Native method Used when executing native codes. created when a method is invoked point to operand local variables value for intermediate computation point to constant pool

14 CPU Register CPU Calculation on registers Accumulator is the most important register for calculation. register CPU calculate CPU register register register register auxiliary load store memory data data Data are loaded to registers, calculated, and stored to memory. data Stack may be used for complicated calculation. CPU CPU Stack CPU load memory データ CPU has no register. Calculation on calculate data data store データ

15 Register vs Stack z = (x + y) * (u + w); register CPU CPU 1 load x, A 2 load y, B 3 add B, A 4 push A 5 load u, A 1 2 A register B register x y 3 1 load x 2 load y 3 add 4 load u 5 load w 1 2 x y 6 load w, B 7 add B, A 8 pop B 4 6 add 7 mult 8 store z 3 9 mult A, B 10 store B, z

16 JVM Instruction (1) JVM instruction Op code is one byte. May have several operands. There are 202 op codes defined (one is no used). Three op codes are reserved. Integer addition: iadd op code: iadd = 0x60 operand: none before iadd after operation: iadd :..., v 1, v 2..., r (r = v 1 + v 2 ) v 2 v 1 op r (r = v 1 + v 2 )

17 JVM Instruction (2) i local v Load value from a local variable iload = 0x15 operand: 1 byte, index スタック 参照 スタック iload, i :......, local[i] iload v local: array of local variables For small indexes, 0, 1, 2, and 3, there are special one byte op code: iload_0, iload_1, iload_2 and iload_3 i local v Store value to a local variable istore = 0x36 スタック 格納 スタック istore, i :..., v... (local[i] v) v istore

18 JVM Instruction (3) Conditional if_icmpeq = 0x9f operand: 2 bytes, offset to the code to jump if_icmpeq, b 1, b 2 :..., v 1, v 2... Pop v 1 and v 2 from the, compare them and if they are equal, add (b 1 256+b 2 ) to PC It they are not equal, just add 3 to PC to execute the next op code. PC PC+3 コード if_icmpeq, b 1, b 2 next op code PC+ (b 1 256+b 2 ) when v 1 = v 2 if_icmpeq v 2 v 1 compare

19 JVM Instruction (4) Method invovation invokevirtual = 0xb6 invokevirtual, i 1, i 2 :..., o, [a 1, [a 2...]]... o : object a 1, a 2,...: arguments to the method i 1 256+ i 2 : index of constant pool class name, method name, descriptor (number of arguments and their type) Find a method from the class name, method name and descriptor. Create a new frame with object o and arguments a 1, a 2,... Set PC to the first op code of the method. If synchronized is set, obtain the monitor for o. augments object a 2 a 1 o invokevirtual o method frame

20 JVM Instruction (5) Return from method ireturn = 0xac etc. ireturn :..., v Returns an integer value. Pop v from the current frame. Discard the current frame. Push v to the caller frame. Put back the previous PC. v current method frame ireturn current method frame v

21 Compilation Example (1) Use ``javap -c'' to see the result of compilation. Compilation of a simple loop void spin() { int i; for (i = 0; i < 100; i++) { ; // Loop body is empty Method void spin() 0 iconst_0 1 istore_1 2 goto 8 5 iinc 1,1 8 iload_1 9 bipush 100 11 if_icmplt 5 14 return 1 local i

22 Compilation Example (2) Method with arguments Method invocation int addtwo(int i, int j) { return i + j; int add12and13() { return addtwo(12, 13); Method int addtwo(int,int) 0 iload_1 1 iload_2 2 iadd 3 ireturn Method int add12and13() 0 aload_0 1 bipush 12 3 bipush 13 5 invokevirtual #4 // Example.addTow(II)I 8 ireturn aload_0: push itself (this) to the Use invokestatic for class methods.

23 Compilation Example (3) Create an instance Object create() { return new Object(); Method java.lang.object create() 0 new #1 // Class java.lang.object 3 dup 4 invokespecial #4 // Method java.lang.object.<init>()v 5 areturn new to create a new object. Invoke the parent constructor. areturn returns the created object.

24 JIT Compiler Merit and demerit of virtual machines High portability Slower than native machine codes JIT compiler Just In Time Java JVM Translate into native machine codes when executed. The first translation may take time, but the translated codes run at native speed. javac JVM JIT M M M Java Compile JVM machine code JIT machine code execute CPU source program Java compiler object program JIT compiler

25 Summary Java Object-oriented Programming Languages Virtual machine Reference The Java Virtual Machine Specification, Java SE 8 Edition Tim Lindholm, Frank Yellin, Gilad Bracha, Alex Buckley Addison-Wesley http://java.sun.com/docs/books/jvms/