Compilation 2007 The Java Virtual Machine Michael I. Schwartzbach BRICS, University of Aarhus.

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.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
Java security (in a nutshell)
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
Advice Weaving in AspectJ Alex Gontmakher. Outline Possible implementation approaches Quick JVM primer AJC implementation Performance Evaluation.
Lab 9 Java Bytecode & The Jasmin Assembler
The Java Programming Language
IPT Readings on Instrumentation, Profiling, and Tracing Seminar presentation by Alessandra Gorla University of Lugano December 7, 2006.
Outline Java program structure Basic program elements
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
Compilation 2007 The What and Why of Compilers Michael I. Schwartzbach BRICS, University of Aarhus.
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.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
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.
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)
CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.
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.
Implement High-level Program Language on JVM CSCE 531 ZHONGHAO LIU ZHONGHAO LIU XIAO LIN.
Java Language and SW Dev’t
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.
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
Java Virtual Machine Case Study on the Design of JikesRVM.
12/10/2002© 2002 Hal Perkins & UW CSET-1 CSE 582 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Autumn 2002.
Virtual Machines, Interpretation Techniques, and Just-In-Time Compilers Kostis Sagonas
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
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 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.
RealTimeSystems Lab Jong-Koo, Lim
CS 432: Compiler Construction Lecture 15
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Java security (in a nutshell)
Topic: JAVA .Class File Structure
CS 153: Concepts of Compiler Design November 2 Class Meeting
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
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
Units with – James tedder
Units with – James tedder
Java Implementation – JVMs, JITs &c Hal Perkins Summer 2004
CS 153: Concepts of Compiler Design November 6 Class Meeting
Building a Java Interpreter
CMPE 152: Compiler Design April 11 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CSc 453 Interpreters & Interpretation
Presentation transcript:

Compilation 2007 The Java Virtual Machine Michael I. Schwartzbach BRICS, University of Aarhus

2 Java Virtual Machine Virtual Machines in Compilation Abstract Syntax Tree Virtual Machine Code Native Binary Code compile interpret

3 Java Virtual Machine Virtual Machines in Compilation Abstract Syntax Tree Virtual Machine Code Native Binary Code compile interpret Virtual Machine Code compile interpret

4 Java Virtual Machine Compiling Virtual Machine Code  Example: gcc translates into RTL, optimizes RTL, and then compiles RTL into native code  Advantages: exposes many details of the underlying architecture facilitates code generators for many targets  Disadvantage: a code generator must be built for each target

5 Java Virtual Machine Interpreting Virtual Machine Code  Examples: P-code for Pascal interpreters Postscript code for display devices Java bytecode for the Java Virtual Machine  Advantages: easy to generate code the code is architecture independent bytecode can be more compact  Disadvantage: poor performance (naively times slower)

6 Java Virtual Machine Designing A Virtual Machine  The instruction set may be more or less high-level  A balance must be found between: the work of the compiler the work of the interpreter  In the extreme case, there is only one instruction: compiler guy: execute "program " interpreter guy: print "result"  The resulting sweet spot involves: doing as much as possible at compile time exposing the program structure to the interpreter minimizing the size of the generated code

7 Java Virtual Machine  Components of the JVM: stack (per thread) heap constant pool code segment program counter (per thread) (we ignore multiple threads in this presentation)

8 Java Virtual Machine The Java Stack  The stack consists of frames: this arguments locals local stack sp lsp The number of local slots and the size of the local stack are fixed at compile-time

9 Java Virtual Machine The Java Heap  The heap consists of objects: runtime type fields

10 Java Virtual Machine The Java Constant Pool  The constant pool consists of all constant data: numbers strings symbolic names of classes, interfaces, and fields

11 Java Virtual Machine The Java Code Segment  The code segment consists of bytecodes of variable sizes: pc

12 Java Virtual Machine Java Bytecodes Java Bytecodes  A bytecode instruction consists of: a one-byte opcode a variable number of arguments (offsets or pointers to the constant pool)  It consumes and produces some stack elements  Constants, locals, and stack elements are typed: addresses ( a ) primitive types ( i, c, b, s, f, d, l )

13 Java Virtual Machine Class Files  Java compilers generate class files: magic number ( 0xCAFEBABE ) minor version/major version constant pool access flags this class super class interfaces fields methods attributes (extra hints for the JVM)

14 Java Virtual Machine Class Loading  Classes are loaded lazily when first accessed  Class name must match file name  Super classes are loaded first (transitively)  The bytecode is verified  Static fields are allocated and given default values  Static initializers are executed

15 Java Virtual Machine From Methods to Bytecode  A simple Java method: public int Abs(int i) { if (i < 0) return(i * -1); else return(i); }  Comments show trace of: x.Abs(-3).method public Abs(I)I // int argument, int result.limit stack 2 // stack with 2 locations.limit locals 2 // space for 2 locals // --locals-- --stack--- iload_1 // [ x -3 ] [ -3 * ] ifge Label1 // [ x -3 ] [ * * ] iload_1 // [ x -3 ] [ -3 * ] iconst_m1 // [ x -3 ] [ ] imul // [ x -3 ] [ 3 * ] ireturn // [ x -3 ] [ * * ] Label1: iload_1 ireturn.end method

16 Java Virtual Machine A Sketch of an Interpreter pc = code.start; while(true) { npc = pc + instruction_length(code[pc]); switch (opcode(code[pc])) { case ILOAD_1: push(locals[1]); break; case ILOAD: push(locals[code[pc+1]]); break; case ISTORE: t = pop(); locals[code[pc+1]] = t; break; case IADD: t1 = pop(); t2 = pop(); push(t1 + t2); break; case IFEQ: t = pop(); if (t==0) npc = code[pc+1]; break;... } pc = npc; }

17 Java Virtual Machine Instructions  The JVM has 256 instructions for: arithmetic operations branch operations constant loading operations locals operations stack operations class operations method operations  See the JVM specification for the full list

18 Java Virtual Machine Arithmetic Operations ineg [...:i] → [...:-i] i2c [...:i] → [...:i%65536] iadd [...:i:j] → [...:i+j] isub [...:i:j] → [...:i-j] imul [...:i:j] → [...:i*j] idiv [...:i:j] → [...:i/j] irem [...:i:j] → [...:i%j] iinc k i [...] → [...] locals[k]=locals[k]+i

19 Java Virtual Machine Branch Operations goto L [...] → [...] branch always ifeq L [...:i] → [...] branch if i==0 ifne L [...:i] → [...] branch if i!=0 inull L [...:a] → [...] branch if a==null ifnonnull L [...:a] → [...] branch if a!=null

20 Java Virtual Machine Branch Operations if_icmpeq L [...:i:j] → [...] branch if i==j if_icmpne L if_acmpeq L [...:a:b] → [...] branch if a==b if_acpmne L if_icmpgt L if_icmple L if_icmplt Lif_icmpge L

21 Java Virtual Machine Constant Loading Operations iconst_0 [...] → [...:0] iconst_1 [...] → [...:1] iconst_5 [...] → [...:5] aconst_null [...] → [...:null] ldc i [...] → [...:i] ldc s [...] → [...:String(s)]...

22 Java Virtual Machine Locals Operations iload k [...] → [...:locals[k]] istore k [...:i] → [...] locals[k]=i aload k [...] → [...:locals[k]] astore k [...:a] → [...] locals[k]=a

23 Java Virtual Machine Field Operations getfield f sig [...:a] → [...:a.f] putfield f sig [...:a:v] → [...] a.f=v getstatic f sig [...] → [...:C.f] putstatic f sig [...:v] → [...] C.f=v

24 Java Virtual Machine Stack Operations dup [...:v] → [...:v:v] pop [...:v] → [...] swap [...v:w] → [...:w:v] nop [...] → [...] dup_x1 [...:v:w] → [...:w:v:w] dup_x2 [...:u:v:w] → [...:w:u:v:w]

25 Java Virtual Machine Class Operations new C [...] → [...:a] instance_of C [...:a] → [...:i] if (a==null) i==0 else i==(type(a)<=C) checkcast C [...:a] → [...:a] if (a!=null) && !type(a)<=C) throw ClassCastException

26 Java Virtual Machine Method Operations invokevirtual name sig [...:a:v 1 :...:v n ] → [...(:v)] m=lookup(type(a),name,sig) push frame of size m.locals+m.stacksize locals[0]=a locals[1]=v 1... locals[n]=v n pc=m.code invokestatic invokeinterface invokespecial

27 Java Virtual Machine Method Operations ireturn [...:i] → [...] return i and pop stack frame areturn [...:a] → [...] return a and pop stack frame return [...] → [...] pop stack frame

28 Java Virtual Machine A Java Method public boolean member(Object item) { if (first.equals(item)) return true; else if (rest == null) return false; else return rest.member(item); }

29 Java Virtual Machine Generated Bytecode.method public member(Ljava/lang/Object;)Z.limit locals 2 // locals[0] = x // locals[1] = item.limit stack 2 // initial stack [ * * ] aload_0 // [ x * ] getfield Cons/first Ljava/lang/Object; // [ x.first *] aload_1 // [ x.first item] invokevirtual java/lang/Object/equals(Ljava/lang/Object;)Z // [bool *] ifeq else_1 // [ * * ] iconst_1 // [ 1 * ] ireturn // [ * * ] else_1: aload_0 // [ x * ] getfield Cons/rest LCons; // [ x.rest * ] aconst_null // [ x.rest null] if_acmpne else_2 // [ * * ] iconst_0 // [ 0 * ] ireturn // [ * * ] else_2: aload_0 // [ x * ] getfield Cons/rest LCons; // [ x.rest * ] aload_1 // [ x.rest item ] invokevirtual Cons/member(Ljava/lang/Object;)Z // [ bool * ] ireturn // [ * * ].end method

30 Java Virtual Machine Bytecode Verification  Bytecode cannot be trusted to be well-behaved  Before execution, it must be verified  Verification is performed: at class loading time at runtime  A Java compiler must generate verifiable code

31 Java Virtual Machine Verification: Syntax  The first 4 bytes of a class file must contain the magic number 0xCAFEBABE  The bytecodes must be syntactically correct

32 Java Virtual Machine Verification: Constants and Headers  Final classes are not subclassed  Final methods are not overridden  Every class except Object has a superclass  All constants are legal  Field and method references have valid signatures

33 Java Virtual Machine Verification: Instructions  Branch targets are within the code segment  Only legal offsets are referenced  Constants have appropriate types  All instructions are complete  Execution cannot fall of the end of the code

34 Java Virtual Machine Verification: Dataflow Analysis  At each program point, the stack always has the same size and types of objects  No uninitialized locals are referenced  Methods are invoked with appropriate arguments  Fields are assigned appropriate values  All instructions have appropriate types of arguments on the stack and in the locals

35 Java Virtual Machine Verification: Gotcha.method public static main([Ljava/lang/String;)V.throws java/lang/Exception.limit stack 2.limit locals 1 ldc invokevirtual java/io/InputStream/read()I return java Fake Exception in thread "main" java.lang.VerifyError: (class: Fake, method: main signature: ([Ljava/lang/String;)V) Expecting to find object/array on stack

36 Java Virtual Machine Verification: Gotcha Again.method public static main([Ljava/lang/String;)V.throws java/lang/Exception.limit stack 2.limit locals 2 iload_1 return java Fake Exception in thread "main" java.lang.VerifyError: (class: Fake, method: main signature: ([Ljava/lang/String;)V) Accessing value from uninitialized register 1

37 Java Virtual Machine Verification: Gotcha Once More ifeq A ldc 42 goto B A: ldc "fortytwo" B: java Fake Exception in thread "main" java.lang.VerifyError: (class: Fake, method: main signature: ([Ljava/lang/String;)V Mismatched stack types

38 Java Virtual Machine Verification: Gonna Getcha Every Time A: iconst_5 goto A java Fake Exception in thread "main" java.lang.VerifyError: (class: Fake, method: main signature: ([Ljava/lang/String;)V Inconsistent stack height 1 != 0

39 Java Virtual Machine JVM Implementation  A naive bytecode interpreter is slow  State-of-the-art JVM implementations are not:

40 Java Virtual Machine Just-In-Time Compilation  Exemplified by SUN’s HotSpot JVM  Bytecode fragments are compiled at runtime targeted at the native platform based on runtime profiling customization is possible  Offers more opportunities than a static compiler

41 Java Virtual Machine Other Java Bytecode Tools  assembler ( jasmin )  deassembler ( javap )  decompiler ( cavaj, mocha, jad )  obfuscator (dozens of these...)  analyzer ( soot )