1 Java Virtual Machine: Instruction Set Cheng-Chia Chen.

Slides:



Advertisements
Similar presentations
The Java Virtual Machine Vishnuvardhan.M Dept. of Computer Science - SSBN.
Advertisements

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.
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.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CS 153: Concepts of Compiler Design November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
The Java Virtual Machine
Advice Weaving in AspectJ Alex Gontmakher. Outline Possible implementation approaches Quick JVM primer AJC implementation Performance Evaluation.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Lab 9 Java Bytecode & The Jasmin Assembler
1 Java Virtual Machine Professor Yihjia Tsai Tamkang University.
Other Computer Organizations RISC. What is RISC Pronounced risk, acronym for reduced instruction set computer, a type of microprocessor that recognizes.
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
Oop The JVM. oop What’s a JVM uA computing machine (just like 8086, but not produced by Intel) lAbstract: machine is specified in a book. lConcrete: anyone.
Oop The JVM. oop What’s a JVM uA computing machine (just like 8086, but not produced by Intel) lAbstract: machine is specified in a book. lConcrete: anyone.
Mic-1: Microarchitecture University of Fribourg, Switzerland System I: Introduction to Computer Architecture WS January 2006
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.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
Oop The JVM. oop What’s a JVM uA computing machine (just like 8086, but not produced by Intel) lAbstract: machine is specified in a book. lConcrete: anyone.
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)
1 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
1 The Java Virtual Machine Yearly Programming Project.
The Java Virtual Machine 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Specification and Design of Quasi- Delay-Insensitive Java Card Microprocessor Fu-Chiung Cheng & Chuin-Ren Wang Dept. of Computer Science and Engineering.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
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.
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
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.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
CSCE 314 Programming Languages JVM Dr. Hyunyoung Lee 1.
MicroJava-701 Philipp Baecker · Johannes Bungert · Andreas Gladisch · Christian Titze.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
Programming Languages and Paradigms Activation Records in Java.
Building a Java Interpreter CS 142 (b) 01/14. Lab Sessions Monday, Wednesday – 10am – noon – ICS 189 Send an or make an appointment with the TA.
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 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 4 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
Recap: Printing Trees into Bytecodes To evaluate e 1 *e 2 interpreter –evaluates e 1 –evaluates e 2 –combines the result using * Compiler for e 1 *e 2.
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 153: Concepts of Compiler Design November 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 432: Compiler Construction Lecture 15
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
CS 153: Concepts of Compiler Design November 2 Class Meeting
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
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
Chapter 4 Control Structures.
CS 432: Compiler Construction Lecture 16
Building a Java Interpreter
CMPE 152: Compiler Design April 11 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design April 18 Class Meeting
Presentation transcript:

1 Java Virtual Machine: Instruction Set Cheng-Chia Chen

2 2 Java Program class SumI { public static void main (String[] args) { int count=10; int sum =0; for (int index=1;index<count;index++) sum=sum+index; } // method main } // class SumI

3 3 Java ByteCode Method void main(java.lang.String[]) 0 bipush 10// byte push 10 to stack (0x10) 2 istore_1// load 10 (top of stack) to count 3 iconst_0// push 0 to stack 4 istore_2// load 0 (top of stack to sum 5 iconst_1// push 1 to stack 6 istore_3// load 1 (top of stack) to index 7 goto 17// go to 17

4 3 Java ByteCode 10 iload_2// load sum to stack 11 iload_3// load index to stack 12 iadd// add 13 istore_2// store “top of stack” to sum 14 iinc 3 1// index iload_3// load index to stack 18 iload_1// load count to stack 19 if_icmplt 10// if index < count goto return

5 import java.io.*; class ReadFile { public static void main (String[] args) { try { FileInputStream in = new FileInputStream(args[0]); int c; while((c = in.read()) != -1) System.out.println(c); in.close(); } catch(Exception e) { e.printStackTrace(); } } // method main } // class ReadFile

6 JVM Instruction Sets l Stack and Local Variable Operations l Type Conversion l Integer Arithmetic l Logic and Bit manipulation l Floating Point Arithmetic l Objects and Arrays l Control Flow l Exceptions l Finally Clauses l Method Invocation and Return l Thread Synchronization

7 Menmonics for instruction type i for an int operation, l for long, s for short, b for byte, c for char, f for float, d for double, and a for reference/array. l EX: iload, lload, fload, dload, aload,…

8 Load and Store Instructions l transfer values between the local variables and the operand stack l load: localVar  Stack l store: Stack  localVar l [i | l | f | d | a]load u8,// or wide load u16 l [i | l | f | d | a]load_, // n=0..3 l [i | l | f | d | a]store u8, // or wide load u16 l [i | l | f | d | a]store_, // n=0..3

9 Push Constants onto stack iconst_ x »x =m1..5; push int x onto stack. fconst_ x »x =0..2; push float x onto stack. l lconst_x ; dconst_x : »x =0 or 1; push long (or double) x onto stack l aconst_null: push null onto stack. l bipush s8; sipush s16, »push (int) s8 [or (int) s16] onto stack

10 Push Constants onto stack l ldc u8; ldc_w u16, ldc2_w u16, »push single word (or double worlds for ldc2) from constant_pool entry u8 (or u16) »ldc/ldc_w can be used to push byte/short/char/int/float/String constants.

11 Generic Stack Operations l nop // do nothing l pop, pop2, // pop top (two) words l dup, dup2, // duplicate top (two) words l swap. // swap top two words l dup_x1, dup_x2 »duplicate top words and put 1 ( 2 ) down »w3, w2, w1 => w3,w1,w2,w1 or (w1,w3,w2,w1) l dup2_x1, dup2_x2, »w4, w3, w2, w1 =>w4, w2,w1, w3,w2,w1 or (w2,w1,w4,w3,w2,w1)

12 Integer and floating point Arithmetic Instructions l Add: iadd, ladd, fadd, dadd. l Subtract: isub, lsub, fsub, dsub. l Multiply: imul, lmul, fmul, dmul. l Divide: idiv, ldiv, fdiv, ddiv. l Remainder: irem, lrem, frem, drem. l Negate: ineg, lneg, fneg, dneg. l Local variable increment: »iinc u8 s8 // add s8 to lcalvar[u8]. »wide iinc u16 s16 // add s16 to local var at u16

13 Shift and Bitwise operations l Shift: ishl, ishr, iushr, lshl, lshr, lushr. »ishl : … int2, int1 => …, int2<<(int1 & 0x001f) »lushr: … long2, int1=> …, long2 >>> (int1 & 0x003f) l Bitwise OR: ior, lor. l Bitwise AND: iand, land. l Bitwise exclusive OR: ixor, lxor.

14 Comparison operations l dcmpg, dcmpl, »…d1,d2=>… int; (d1 -1; d2=d1=> 0; d1>d2 =>1 ) or NaN=>(g =>1; l=>-1) l fcmpg, fcmpl, // like above l lcmp. »… long1, long2 => … int k where » k = -1, 0 or 1 depending on long1 long2.

15 Type Conversion Instructions l widening conversions: »int  long  float  double : »i2l, i2f, i2d, l2f, l2d, f2d. »// sign extension+round-to-nearest mode l Narrowing conversions: »double  float  long  int : »d2f, d2l, d2i, f2l, f2i, l2i // truncate+ sign extension+ rtn mode »int  short, byte, char: »i2s, i2c, i2b // truncate+ sign extension l Note: byte, short, char  int are done automaticaly.

16 Object Creation and Field Access l new »Create a new class instance on the heap, »and push a reference: l Field access: »getfield fieldRefIndex; putfield fieldRefIndex, »… objRef  … value; …objRef, value  … » getstatic fieldRefIndex putstatic fieldRefIndex » …  value; …value  … l Type Checking: »instanceof class_index : … objRef  … RltVal »checkcast class_index: …obj1  … obj1 if obj1 can be casted to class type, o/w throw CastException

17 Array Creation l newarray aType: »pop length, allocate new primitive array of type given by atype, push the reference. »aType : (z,c,f,d,b,s,i,l) ->(4,5,6,7,8,9,10,11) »… u16 -> arrayRef l anewarray Class_index, »pop length, allocate new array of type given by class_index, push the reference l multianewarray type_index dim:u8. »pop dim number of array lengths, allocate multi- dimensional array of type class_index, pop reference. »new int[10][20] ==> p10, p20, multianewarray [[i 2.

18 Array Component Access l array component  stack : »baload, caload, saload, iaload, »… arrayRef, index:s32  … (int) array[index] »laload, faload, daload, aaload. l stack  array component »bastore, castore, sastore, iastore, »… arrayRef, index:s32, val  … »lastore, fastore, dastore, aastore. l Get the length of array: arraylength »… arrayRef  … (int)array.length

19 Control flow Instructions 1. Conditional Branches l Conditional branch: »unary comparsions –ifeq, iflt, ifle, ifne, ifgt, ifge, // =0?, <0?,… –ifnull, ifnonnull, // null or not null »binary comparisons –if_icmpeq, if_icmpne, if_icmplt, if_icmpgt, if_icmple, if_icmpge, –if_acmpeq, if_acmpne. // compare reference l For comparison of long, double and float »use dcmpg, dcmpl, fcmpg, fcmpl, lcmp and »unary comparisons on int.

20 Control flow Instructions : 2.Compound Conditional Branches l lookupswitch defaultOffset #pairs {case offset} #pairs : »all (blue) fields are int32 values »case must be in ascending order »e.g.: lookupswitch lab0 3 2 lab1 10 lab2 13 lab3 l tableswitch defOffset lowCase highCase offset high-low+1. »goto offset[top-low+1] iff low ≤ top ≤ high »else goto defOffset. »e.g.: tableswitch lab lab11 lab12 lab13 lab14.

21 Control flow Instructions : Unconditional branches l goto s16; goto_w s32 »pc = pc + s16 (or s32). // for implementing finally clauses // intraMethod subroutine call l jsr s16, jsr_w s32, »push return addr and jumpToSubroutine pc+s16 for a finally block »…  … returnAddr l ret u8; widen ret u16, »return to addr given by localVar u8 or u16.

22 Method Invocation and Return Instructions l invokevirtual : for normal methods l invokeInterface: »for interface methods // search of implementation is needed l invokespecial: »for private method, superclass method and instance initialization (constructors) methods l invokestatic : for static methods l format: opcode u16 // methodRefIndex l return : »ireturn, lreturn, freturn, dreturn, areturn »return : for void, instance, class (or interface) initialization methods

23 Exceptions related Instructions l Throw Exception: »athrow »can also be thrown by other instructions if abnormal condition detected. »… ExcObj  … Implementing finally l jsr s16, jsr_w s32, »jumpToSubroutine pc+s16 for a finally block l ret u8; widen ret u16, »return to addr given by localVar u8 or u16.

24 Synchronization l via monitor construct: »monitorenter, monitorexit l monitorenter »pop objectref, acquire the lock associated with the object. »… ObjRef  … l monitorexit »pop objectref, release the lock associate with objectref » … ObjRef  …

25 Classes needing special support of JVM l Reflection: » java.lang.reflect.* and java.lang. Class. l Loading and creation of a class or interface. »ClassLoader. l Linking and initialization of a class or interface. l Security: »java.security.** and other classes such as SecurityManage r. Multithreading, such as the class Thread. Weak references: java.lang.ref.* l …