the java virtual machine the java virtual machine
play

The Java Virtual Machine The Java Virtual Machine interpret - PowerPoint PPT Presentation

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


  1. Virtual Machines in Compilation Virtual Machines in Compilation Abstract Syntax Tree compile Compilation 2007 Compilation 2007 Virtual Machine Code The Java Virtual Machine The Java Virtual Machine interpret compile Native Binary Code Michael I. Schwartzbach BRICS, University of Aarhus Java Virtual Machine 2 Virtual Machines in Compilation Compiling Virtual Machine Code Virtual Machines in Compilation Compiling Virtual Machine Code � Example: Abstract Syntax Tree • gcc translates into RTL, optimizes RTL, and then compile compiles RTL into native code Virtual Machine Code � Advantages: interpret compile • exposes many details of the underlying architecture Virtual Machine Code • facilitates code generators for many targets � Disadvantage: interpret compile • a code generator must be built for each target Virtual Machine Code interpret compile Native Binary Code Java Virtual Machine 3 Java Virtual Machine 4 1

  2. Interpreting Virtual Machine Code Designing A Virtual Machine Interpreting Virtual Machine Code Designing A Virtual Machine � Examples: � The instruction set may be more or less high-level • P-code for Pascal interpreters � A balance must be found between: • Postscript code for display devices • the work of the compiler • Java bytecode for the Java Virtual Machine • the work of the interpreter � Advantages: � In the extreme case, there is only one instruction: • easy to generate code • compiler guy: execute "program " • the code is architecture independent • interpreter guy: print "result" • bytecode can be more compact � The resulting sweet spot involves: � Disadvantage: • doing as much as possible at compile time • poor performance (naively 5-100 times slower) • exposing the program structure to the interpreter • minimizing the size of the generated code Java Virtual Machine 5 Java Virtual Machine 6 Java Virtual Machine The Java Stack Java Virtual Machine The Java Stack � Components of the JVM: � The stack consists of frames : • stack (per thread) sp • heap lsp local stack • constant pool • code segment locals The number of local slots and the size of the local stack are • program counter (per thread) arguments fixed at compile-time this (we ignore multiple threads in this presentation) Java Virtual Machine 7 Java Virtual Machine 8 2

  3. The Java Heap The Java Constant Pool The Java Heap The Java Constant Pool � The heap consists of objects : � The constant pool consists of all constant data : • numbers • strings • symbolic names of classes, interfaces, and fields fields runtime type Java Virtual Machine 9 Java Virtual Machine 10 The Java Code Segment Java Bytecodes The Java Code Segment Java Bytecodes � The code segment consists of bytecodes of � A bytecode instruction consists of: variable sizes: • a one-byte opcode • a variable number of arguments (offsets or pointers to the constant pool) � It consumes and produces some stack elements pc � Constants, locals, and stack elements are typed: • addresses ( a ) • primitive types ( i , c , b , s , f , d , l ) Java Virtual Machine 11 Java Virtual Machine 12 3

  4. Class Files Class Loading Class Files Class Loading � Java compilers generate class files: � Classes are loaded lazily when first accessed • magic number ( 0xCAFEBABE ) � Class name must match file name • minor version/major version � Super classes are loaded first (transitively) • constant pool � The bytecode is verified • access flags � Static fields are allocated and given default values • this class � Static initializers are executed • super class • interfaces • fields • methods • attributes (extra hints for the JVM) Java Virtual Machine 13 Java Virtual Machine 14 From Methods to Bytecode A Sketch of an Interpreter From Methods to Bytecode A Sketch of an Interpreter � pc = code.start; A simple Java method: while(true) { npc = pc + instruction_length(code[pc]); switch (opcode(code[pc])) { case ILOAD_1: push(locals[1]); public int Abs(int i) .method public Abs(I)I // int argument, int result break; .limit stack 2 // stack with 2 locations { if (i < 0) case ILOAD: push(locals[code[pc+1]]); .limit locals 2 // space for 2 locals return(i * -1); break; else case ISTORE: t = pop(); // --locals-- --stack--- locals[code[pc+1]] = t; return(i); iload_1 // [ x -3 ] [ -3 * ] break; } ifge Label1 // [ x -3 ] [ * * ] case IADD: t1 = pop(); t2 = pop(); iload_1 // [ x -3 ] [ -3 * ] push(t1 + t2); iconst_m1 // [ x -3 ] [ -3 -1 ] break; imul // [ x -3 ] [ 3 * ] case IFEQ: t = pop(); ireturn // [ x -3 ] [ * * ] if (t==0) npc = code[pc+1]; Label1: break; iload_1 ... ireturn } .end method pc = npc; } � Comments show trace of: x.Abs(-3) Java Virtual Machine 15 Java Virtual Machine 16 4

  5. Instructions Arithmetic Operations Instructions Arithmetic Operations ineg [...:i] → [...:-i] � The JVM has 256 instructions for: • arithmetic operations i2c [...:i] → [...:i%65536] • branch operations iadd [...:i:j] → [...:i+j] • constant loading operations isub [...:i:j] → [...:i-j] • locals operations imul [...:i:j] → [...:i*j] • stack operations • class operations idiv [...:i:j] → [...:i/j] • method operations irem [...:i:j] → [...:i%j] � See the JVM specification for the full list iinc k i [...] → [...] locals[k]=locals[k]+i Java Virtual Machine 17 Java Virtual Machine 18 Branch Operations Branch Operations Branch Operations Branch Operations goto L [...] → [...] if_icmpeq L [...:i:j] → [...] branch always branch if i==j ifeq L [...:i] → [...] if_icmpne L if_icmpgt L branch if i==0 if_icmplt L if_icmpge L ifne L [...:i] → [...] if_icmple L branch if i!=0 if_acmpeq L [...:a:b] → [...] inull L [...:a] → [...] branch if a==b branch if a==null if_acpmne L ifnonnull L [...:a] → [...] branch if a!=null Java Virtual Machine 19 Java Virtual Machine 20 5

  6. Constant Loading Operations Locals Operations Constant Loading Operations Locals Operations iconst_0 [...] → [...:0] iload k [...] → [...:locals[k]] iconst_1 [...] → [...:1] istore k [...:i] → [...] locals[k]=i ... aload k [...] → [...:locals[k]] iconst_5 [...] → [...:5] astore k [...:a] → [...] aconst_null [...] → [...:null] locals[k]=a ldc i [...] → [...:i] ldc s [...] → [...:String(s)] Java Virtual Machine 21 Java Virtual Machine 22 Field Operations Stack Operations Field Operations Stack Operations getfield f sig [...:a] → [...:a.f] dup [...:v] → [...:v:v] putfield f sig [...:a:v] → [...] pop [...:v] → [...] a.f=v swap [...v:w] → [...:w:v] getstatic f sig [...] → [...:C.f] nop [...] → [...] putstatic f sig [...:v] → [...] dup_x1 [...:v:w] → [...:w:v:w] C.f=v dup_x2 [...:u:v:w] → [...:w:u:v:w] Java Virtual Machine 23 Java Virtual Machine 24 6

  7. Class Operations Method Operations Class Operations Method Operations new C [...] → [...:a] invokevirtual name sig [...:a:v 1 :...:v n ] → [...(:v)] m=lookup(type(a),name,sig) instance_of C [...:a] → [...:i] push frame of size m.locals+m.stacksize if (a==null) i==0 locals[0]=a else i==(type(a)<=C) locals[1]=v 1 checkcast C [...:a] → [...:a] ... locals[n]=v n if (a!=null) && !type(a)<=C) pc=m.code throw ClassCastException invokestatic invokespecial invokeinterface Java Virtual Machine 25 Java Virtual Machine 26 Method Operations A Java Method Method Operations A Java Method ireturn [...:i] → [...] public boolean member(Object item) { if (first.equals(item)) return i and pop stack frame return true; else if (rest == null) areturn [...:a] → [...] return false; return a and pop stack frame else return rest.member(item); return [...] → [...] } pop stack frame Java Virtual Machine 27 Java Virtual Machine 28 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend