Java Virtual Machine (JVM)

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
BA1 JVM arkitektur BA2 Specifikation I The Java Virtual Machine Specification specificeres bl.a.: –De grundlæggende datatyper.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
The Java Virtual Machine
Advice Weaving in AspectJ Alex Gontmakher. Outline Possible implementation approaches Quick JVM primer AJC implementation Performance Evaluation.
Lab 9 Java Bytecode & The Jasmin Assembler
1 Java Virtual Machine Professor Yihjia Tsai Tamkang University.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
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.
Compilation 2007 The Java Virtual Machine Michael I. Schwartzbach BRICS, University of Aarhus.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Compiler Construction Lecture 17 Mapping Variables to Memory.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.
1 The Java Virtual Machine Yearly Programming Project.
Types and Inheritances Compiler Design Lecture (03/04//98) Computer Science Rensselaer Polytechnic.
1 Java.class File Format 陳正佳. 2 Java Virtual Machine the cornerstone of Sun's Java programming language. a component of the Java technology responsible.
The Java Virtual Machine 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Lecture 10 : Introduction to Java Virtual Machine
10/12/2015© Hal Perkins & UW CSEV-1 CSE P 501 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Winter 2008.
1 Introduction to JVM Based on material produced by Bill Venners.
1 Java Bytecode Optimization Optimizing Java Bytecode for Embedded Systems Stefan Hepp.
Code Compiled with javac static int k = 0; static boolean action(int si, boolean ob, int sm, int pr) { if (sm + 2*pr > 10 && !(si
Roopa.T PESIT, Bangalore. Source and Credits Dalvik VM, Dan Bornstein Google IO 2008 The Dalvik virtual machine Architecture by David Ehringer.
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.
12/10/2002© 2002 Hal Perkins & UW CSET-1 CSE 582 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Autumn 2002.
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
CSCE 314 Programming Languages JVM Dr. Hyunyoung Lee 1.
1 Lecture 12 Java Virtual Machine:.class file Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
Programming Languages and Paradigms Activation Records in Java.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.
Review on Program Challenge CSc3210 Yuan Long.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.
RealTimeSystems Lab Jong-Koo, Lim
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
(Not too) Real-Time JVM (Progress Report)
CS 432: Compiler Construction Lecture 15
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Java Primer 1: Types, Classes and Operators
CS 153: Concepts of Compiler Design November 2 Class Meeting
Java Virtual Machine Complete subject details are available at:
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Starting JavaProgramming
CSc 453 Interpreters & Interpretation
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Course Overview PART I: overview material PART II: inside a compiler
Java Implementation – JVMs, JITs &c Hal Perkins Summer 2004
CS 153: Concepts of Compiler Design November 6 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CSc 453 Interpreters & Interpretation
Presentation transcript:

Java Virtual Machine (JVM) Reasons for learning JVM Resource The Java TM Virtual Machine Specification (2 nd Ed), by Tim Lindholm & Frank Yellin, Addison-Wesley,1999 http://java.sun.com/docs/books/vmspec/ by Neng-Fa Zhou

JVM Types and Words byte, char, short, int, float, reference, and returnAddress in one word boolean, byte, char, and short are converted to int Special representations for byte, char, and short arrays are possible long and double in two words by Neng-Fa Zhou

JVM Architecture PC Method area OPTOP EP Heap LVARS ….. Registers Operand stack EP Environment Heap Local vars LVARS ….. Registers Stack by Neng-Fa Zhou

Data Areas of JVM Method area (shared by all threads) One record for each loaded class that contains: Constant pool Code for methods and constructors Heap (shared by all threads) One record for each class instance or array Stack (one per thread) Hold frames associated with method invocations by Neng-Fa Zhou

Stack Frame Structure Operand stack: Evaluate expressions Pass method arguments and receive results Local variables: Addressed as word offsets Constant pool: Dynamic linking Operand stack Environment Constant pool PC EP LVARS Local variables by Neng-Fa Zhou

The class File Format 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];     } by Neng-Fa Zhou

Names and Descriptors Class, method, and field names are all stored symbolically as strings A descriptor is a string representing the type of a field or method Class names Class names are always fully qualified Use forward slash rather than dot as the delimeter Ex: Java.util.Vector => java/util/Vector by Neng-Fa Zhou

Descriptors Primitive types Arrays Classes Ex: Use [ Lclassname B, C, D, F, I,J(long), S, Z(boolean), V(void) Arrays Use [ Classes Lclassname Ex: Type: String valueOf(char[] , int offset, int count) Descriptor:([CII)Ljava/lang/String; by Neng-Fa Zhou

Constants CONSTANT_Class CONSTANT_Fieldref CONSTANT_Methodref CONSTANT_InterfaceMthodref CONSTANT_String, CONSTANT_Integer CONSTANT_Float CONSTANT_Long CONSTANT_Double CONSTANT_NameAndType CONSTANT_Utf8 by Neng-Fa Zhou

field_info and method_info Access flags Name Descriptor Code field_info Access flags Name Descriptor Static values by Neng-Fa Zhou

Instruction Set Load and store (e.g., iload, istore ldc, iconst_<i>) Arithmetic (e.g., iadd, isub, imul, idiv, irem) Type conversion (e.g., i2b, i2f, i2d) Object creation and manipulation (e.g, new, newarray, iaload, iastore , getfield, putfield) Operand stack manipulation (e.g., pop, dup, dup_x1, swap) Control transfer (e.g., goto, ifeq, tableswitch) Method invocation and return (invokevirtual, invokeinterface, invokespecial, invokestatic, ireturn) Exception handling and synchronization (e.g.,athrow) by Neng-Fa Zhou

Compiling for JVM Constants, Local Variables, and Control constructs void spin() { int i; for (i = 0;i<100;i++) ; } Method void spin() 0 iconst_0 // Push int constant 0 1 istore_1 // Store into local variable 1 (i=0) 2 goto 8 // First time through don't increment 5 iinc 1 1 // Increment local variable 1 by 1 (i++) 8 iload_1 // Push local variable 1 (i) 9 bipush 100 // Push int constant 100 11 if_icmplt 5 // Compare and loop if less than (i < 100) 14 return // Return void when done by Neng-Fa Zhou

Compiling for JVM Receiving Arguments and Invoking Methods int m1(int a1, int a2) { return m2(2,3,a1,a2); } Method int m1() 0 aload_0 // Push local variable 0 (this) 1 bipush 2 // Push int constant 2 3 bipush 3 // Push int constant 3 5 iload_1 // Push local var a1 6 iload_2 // Push local var a2 7 invokevirtual #4 // Method Example.m(IIII)I 10 ireturn // Return int on top of operand stack by Neng-Fa Zhou

Compiling for JVM Working with Class Instances MyObj example() { MyObj o = new MyObj(); return silly(o); } Method MyObj example() 0 new #2 // Class MyObj 3 dup 4 invokespecial #5 // Method MyObj.<init>()V 7 astore_1 8 aload_0 9 aload_1 10 invokevirtual #4 // Method Example.silly(LMyObj;)LMyObj; 13 areturn by Neng-Fa Zhou

Compiling for JVM Arrays Creating arrays Manupulating arrays int x[] = new int[3]; 0 iconst_3 1 newarray int 3 astore_1 x[2] = 0; 4 aload_1 5 iconst_2 6 iconst_0 7 iastore x[0] = x[2]; 8 aload_1 9 iconst_0 10 aload_1 11 iconst_2 12 iaload 13 iastore by Neng-Fa Zhou

Compiling for JVM Switches Method int chooseNear(int) 0 iload_1 1 tableswitch 0 to 2: 0: 28 1: 30 2: 32 default:34 28 iconst_0 29 ireturn 30 iconst_1 31 ireturn 32 iconst_2 33 ireturn 34 iconst_m1 35 ireturn int chooseNear(int i) { switch (i) { case 0: return 0; case 1: return 1; case 2: return 2; default: return -1; } by Neng-Fa Zhou

Compiling for JVM Manipulation of the Operand Stack public long nextIndex(){ return index++; } private long index = 0; Method long nextIndex() 0 aload_0 // Push this 1 dup // Make a copy of it 2 getfield #4 // One of the copies of this is consumed // pushing long field index, // above the original this 5 dup2_x1 // The long on top of the operand stack is // inserted into the operand stack below the // original this 6 lconst_1 // Push long constant 1 7 ladd // The index value is incremented... 8 putfield #4 // ...and the result stored back in the field 11 lreturn // The original value of index is left on // top of the operand stack, ready to be returned by Neng-Fa Zhou

Compiling for JVM Exception Handling void catchOne() { try { tryItOut(); } catch (TestExc e) { handleExc(e); } Method void catchOne() 0 aload_0 // Beginning of try block 1 invokevirtual #6 // Method Example.tryItOut()V 4 return // End of try block; normal return 5 astore_1 // Store thrown value in local var 1 6 aload_0 // Push this 7 aload_1 // Push thrown value 8 invokevirtual #5 // Invoke handler method: // Example.handleExc(LTestExc;)V 11 return // Return after handling TestExc Exception table: From To Target Type 0 4 5 Class TestExc by Neng-Fa Zhou

Review Questions Does the efficiency of a program change after an int variable is changed to byte? When does dynamic loading take place? When does dynamic linking take place? Why are run-time byte-code verification and type checking necessary? How are exceptions handled in JVM? How different is the JVM from a stack machine for Pascal? by Neng-Fa Zhou