1 Introduction to Java & Java Virtual Machine 5/12/2001 Hidehiko Masuhara.

Slides:



Advertisements
Similar presentations
Chapt.2 Machine Architecture Impact of languages –Support – faster, more secure Primitive Operations –e.g. nested subroutine calls »Subroutines implemented.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Virtual Machines Matthew Dwyer 324E Nichols Hall
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
JAVA Processors and JIT Scheduling. Overview & Literature n Formulation of the problem n JAVA introduction n Description of Caffeine * Literature: “Java.
Lightweight Abstraction for Mathematical Computation in Java 1 Pavel Bourdykine and Stephen M. Watt Department of Computer Science Western University London.
Method Manipulation in an Object-Oriented Processor.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Aarhus University, 2005Esmertec AG1 Implementing Object-Oriented Virtual Machines Lars Bak & Kasper Lund Esmertec AG
Java for High Performance Computing Jordi Garcia Almiñana 14 de Octubre de 1998 de la era post-internet.
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Consider With x = 10 we may proceed as (10-1) = 9 (10-7) = 3 (9*3) = 27 (10-11) = -1 27/(-1) = -27 Writing intermediates on paper.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
Session-02. Objective In this session you will learn : What is Class Loader ? What is Byte Code Verifier? JIT & JAVA API Features of Java Java Environment.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
Embedded Java Research Geoffrey Beers Peter Jantz December 18, 2001.
OOP Languages: Java vs C++
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Compiler Construction Lecture 17 Mapping Variables to Memory.
CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)
Programming Languages and Paradigms Object-Oriented Programming.
JVM And CLR Dan Agar April 16, Outline Java and.NET Design Philosophies Overview of Virtual Machines Technical Look at JVM and CLR Comparison of.
1 The Java Virtual Machine Yearly Programming Project.
Implement High-level Program Language on JVM CSCE 531 ZHONGHAO LIU ZHONGHAO LIU XIAO LIN.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
7. Just In Time Compilation Prof. O. Nierstrasz Jan Kurs.
Lecture 10 : Introduction to Java Virtual Machine
O VERVIEW OF THE IBM J AVA J UST - IN -T IME C OMPILER Presenters: Zhenhua Liu, Sanjeev Singh 1.
1 Introduction to JVM Based on material produced by Bill Venners.
1 Java Bytecode Optimization Optimizing Java Bytecode for Embedded Systems Stefan Hepp.
Roopa.T PESIT, Bangalore. Source and Credits Dalvik VM, Dan Bornstein Google IO 2008 The Dalvik virtual machine Architecture by David Ehringer.
CS412/413 Introduction to Compilers and Translators May 3, 1999 Lecture 34: Compiler-like Systems JIT bytecode interpreter src-to-src translator bytecode.
The Procedure Abstraction, Part VI: Inheritance in OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
An Efficient Stack Machine Martin Schöberl. JOP Stack Architecture2 Overview JVM stack machine Parameter passing Stack access patterns Common stack caches.
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
Java Virtual Machine Case Study on the Design of JikesRVM.
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Virtual Machines, Interpretation Techniques, and Just-In-Time Compilers Kostis Sagonas
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
CS 598 Scripting Languages Design and Implementation 14. Self Compilers.
Processes and Virtual Memory
CSE 598c – Virtual Machines Survey Proposal: Improving Performance for the JVM Sandra Rueda.
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
CS 598 Scripting Languages Design and Implementation 12. Interpreter implementation.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
RealTimeSystems Lab Jong-Koo, Lim
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Interpreted languages Jakub Yaghob
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Java Virtual Machine (JVM)
CSc 453 Interpreters & Interpretation
Adaptive Code Unloading for Resource-Constrained JVMs
Inlining and Devirtualization Hal Perkins Autumn 2011
Byte Code Verification
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
CSc 453 Interpreters & Interpretation
JIT Compiler Design Maxine Virtual Machine Dhwani Pandya
Presentation transcript:

1 Introduction to Java & Java Virtual Machine 5/12/2001 Hidehiko Masuhara

2 outline characteristics of Java language Java virtual machine –execution model JIT compilation –4 what it does’

3 characteristics of the Java language virtual machine (VM) based implementation object-oriented (OO) automatic memory management (GC) thread safe libraries dynamic class loading security mechanisms (incl. bytecode verification)

4 virtual machine based implementation not a new idea! advantages –compact object code (cf. embedded systems) –multi-platform execution (cf. write once, XXX everywhere) disadvantages –overheads (e.g., p-code (late 70’s), Smalltalk (80’s), Self (late 80’s),.Net (2000’s))

5 object-oriented encapsulation overriding reuse dynamic dispatch (virtual call) class Point { int x,y; draw(Graphics g) { g.putPixel(x,y);}} class Point { int x,y; draw(Graphics g) { g.putPixel(x,y);}} class ColorPoint extends Point { Color c; draw(Graphics g) { g.setColor(c); super.draw(g);}} class ColorPoint extends Point { Color c; draw(Graphics g) { g.setColor(c); super.draw(g);}} Point p; p.draw(g);

6 virtual calls are expensive object p object cp object red class Pointclass ColorPoint draw getX virtual method tables method body p.draw: vtable <- *p m <- *(vtable+0) call m p.draw: vtable <- *p m <- *(vtable+0) call m because they need “double dispatching”

7 automatic memory management (GC) memory for objects & classes are automatically reclaimed pros. easy and safe –no more worry about memory corruption cons. extra overheads –esp. in Java; tend to use fine-grained objects (you already know!)

8 thread safe libraries standard classes ensures serialized behavior how to ensure? lock the object around method execution problem: overuse of lock operations Thread1: ctr.inc(1) Thread1: ctr.inc(1) Thread2: ctr.inc(2) Thread2: ctr.inc(2) ctr.inc(1); ctr.inc(2) = or

9 dynamic class loading class is loaded at its first object creation also can manually load classes (eg.,DLL, SO) pros. –faster startup –smaller footprint cons. –make analysis difficult class Word { DocWin[] docs; help(Topic t) { Kairu k=new...; } class Word { DocWin[] docs; help(Topic t) { Kairu k=new...; } class Kairu is not loaded until help is called

10 class Point { int x; getX() { return this.x; } setX(int newX) { this.x = newX; } move(Point offset) { setX(this.getX() + offset.getX()); } } class Point { int x; getX() { return this.x; } setX(int newX) { this.x = newX; } move(Point offset) { setX(this.getX() + offset.getX()); } } dynamic loading makes analysis difficult because optimizations rely on the structure of class hierarchy move(Point offset) { this.x = this.x + offset.x; } move(Point offset) { this.x = this.x + offset.x; } can be optimized by inlinig

11 class Point { int x; getX() { return this.x; } setX(int newX) { this.x = newX; } move(Point offset) { setX(this.getX() + offset.getX()); } } class Point { int x; getX() { return this.x; } setX(int newX) { this.x = newX; } move(Point offset) { setX(this.getX() + offset.getX()); } } dynamic loading makes analysis difficult because optimizations rely on the structure of class hierarchy dynamic loading changes the structure move(Point offset) { this.x = this.x + offset.x; } move(Point offset) { this.x = this.x + offset.x; } class DPoint extends Point { getX() { return this.x +random(); } } class DPoint extends Point { getX() { return this.x +random(); } } DPoint can’t inherit move optimized move become incorrect when a subclass is loaded

12 virtual machine execution model of Java source (text) compiler CPU bytecode interpreter bytecode interpreter dynamic loading JIT compiler JIT compiler compiled code compiled code JVML verifier bytecode (aka. class file)

13 introduction to JVML a VML designed for Java language characteristics –rigidly defined (vs. previous counterparts) –typed: can check type safety –not a native machine code object manipulations are primitive infinite number of registers (local variables) operand stack

14 compilation sample (source) class Power { int base, unit; int compute(int n) { if (n==0) return this.unit; else return this.base * this.compute(n-1); } class Power { int base, unit; int compute(int n) { if (n==0) return this.unit; else return this.base * this.compute(n-1); }

15 compilation sample (assembly) > javap -c Power synchronized class Power extends java.lang.Object /* ACC_SUPER bit set */ { int base; int unit; int compute(int); Power(); } Method Power() 0 aload_0 1 invokespecial #3 4 return > javap -c Power synchronized class Power extends java.lang.Object /* ACC_SUPER bit set */ { int base; int unit; int compute(int); Power(); } Method Power() 0 aload_0 1 invokespecial #3 4 return Method int compute(int) 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn Method int compute(int) 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn type info. constructor the method

16 heap execution model of JVML p.setX(..) p.move(...) main(...) stack 123 Point X=10,y=3 Point X=10,y=3 local vars.op. stack ColorPoint X=10,y=3 c=Red ColorPoint X=10,y=3 c=Red a frame (activation record) array of Point fields are named op. stack: tentative, operands for VM inst. & for method invocation local vars.: mutable, valid through an invocation

17 VM instructions 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn when a method is invoked, –local var.#0: “this” –local var.#1..: arguments push int val. of lv#1 on op. stack pop int val., if it<>0 then jump to 9 Method int compute(int)

18 VM instructions 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn push addr. val. in lv#0 (this) on op. stack pop addr. val off op. stack, read field “unit”, push the result on op. stack return from the method

19 VM instructions 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn push “this” read “base” field push “this” push “n” push value 1 pop val. of “n” & 1 off stack, subtract, and push result

20 VM instructions 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn this n 1 base this n-1 base

21 VM instructions 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn method call –pop obj. & int off the op. stack, –call method “compute” of the obj. with int value –push return value pop 2 values, multiply, push result return from method

22 VM instructions 0 iload_1 1 ifne 9 4 aload_0 5 getfield #6 8 ireturn 9 aload_0 10 getfield #4 13 aload_0 14 iload_1 15 iconst_1 16 isub 17 invokevirtual #5 20 imul 21 ireturn this n-1 base r v

23 implementations of JVMs bytecode interpreter JIT compiler –as a traditional compiler –as an optimizing compiler –as a JIT compiler

24 bytecode interpreter simulates the machine it’s expensive VM core: –read bytecode –dispatch –compute while (true) { code = prog[pc++]; switch (code) { LOAD: n=prog[pc++]; v=local[n]; opstack[sp++]; STORE: } } while (true) { code = prog[pc++]; switch (code) { LOAD: n=prog[pc++]; v=local[n]; opstack[sp++]; STORE: } } alternative:“threaded execution” = a very light weight JIT compilation

25 JIT does what compilers do but does at run-time! (still not new; cf. Smalltalk & Self) register allocation (to local vars & op. stacks) translate VM instrs. to native instrs. instruction scheduling

26 JIT does optimizing compilers do method inlining (cf. Self) common subexpression elimination loop unrolling array boundary check deletion etc. but they must be quick enough

27 JIT does more than traditional compilers do stack allocation of temporary objects * up to programmer in C++ ** similar to region analysis in FPs eliminate lock/unlocks when accessing private objects optimistic type customization (cf. Self) (with revoke mechanism)

28 JIT does what only JIT does adaptive or mixed execution several execution modes –interpretive –quick compilation, no optimization –... –slow but highly optimizing compilation profile to find “hotspots” & more optimize them faster startup, smaller memory footprint

29 summary Java has many advanced language features –good for programmers –challenge for implementers JVM –key to Java’s portability –performance JIT compilers –has most features of modern optimizing compilers –do more than that!

30 参考文献 L. Peter Deutsch and Allan M. Schiffman. Efficient implementation of the Smalltalk-80 system. In Conference Record of the 11th Annual ACM Symposium on Principles of Programming Languages (POPL'84), pages , Salt Lake City, Jan J. Dolby and A. A. Chien. An evaluation of automatic object inline allocation techniques. In Proceedings of Conference on Object-Oriented Programming Systems, Languages, and Applications (OOPSLA), Tim Lindholm and Frank Yellin. The Java Virtual Machine Specification. Addison-Wesley, David Ungar and Randall B. Smith. Self: The power of simplicity. In Norman Meyrowitz, editor, Proceedings of Object-Oriented Programming Systems, Languages, and Applications, volume 22(12) of ACM SIGPLAN Notices, pages , Orlando, FL, October ACM. OOPSLA (Object-Oriented Programming Systems) / PLDI (Programming Language Design and Implementation) / POPL (Principles of Programming Languages) / Java Grande Workshop / Java Virtual Machine Conference