Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 oop The JVM

2 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 can implement uInput: lA “class” file a binary file with lots of numbers and instructions. lA search path (to find more class files) uOutput lThe “execution” of the class file. uHow? lIt is all in The Java Virtual Machine Specification, by Tim Lindholm and Frank Yellin uStart with? lA method named “main” in a given class file. The method must have certain properties lContinue execution in other methods as necessary.

3 oop Class File? uThe binary form for Java programs uRepresents a complete description of one Java class or interface uPlatform independent – bytecodes are the machine language of the JVM uNot necessarily linked to the java language: Compiler Program in Java Lang. Program in Java Lang. Program in other Lang. Program in other Lang. Program in Java Lang. Program in Java Lang. Java class files Java class files Other Binary format Other Binary format

4 oop Class File Structure lPart I: Pool Maps strings to integers. Mostly symbolic names. lPart II: instructions for execution Organized in “methods” Each reference to another method, or another class file is through a “small integer” (index to the pool)

5 oop Basic JVM Components The Java Virtual Machine Class loader Execution engine Host operating system Program Class files The Java API’s class files Native methods invocation

6 oop Semantics of the Abstract Machine uLow level, Assembly Like: lno expression such as (a + b) *c uNo ordinary memory addressing (cannot access address 1000) lUse symbolic names, as defined in the pool. lGarbage collected! uNo registers (stack semantics) lTo do (a+b)*c: Push a Push b Add Push c Mult lScratch variables (used for storing arguments and local variables) Each method defines how many it needs

7 oop Typed Assembly uHigh Level Language uClass f(int a, int b, int c, int d) { return (a + b) *c - d; } 0: iload_1 1: iload_2 2: iadd 3: iload_3 4: imul 5: iload 4 6: isub 7: ireturn

8 oop JVM Types uSimilar (but not identical) to Java uPrimitive data types. lbyte: one byte lshort: two bytes lint: four bytes llong: eight bytes (2 entries on stack) lfloat: four bytes ldouble: eight bytes (2 entries on stack) lchar: two bytes uReference types: lClass reference lInterface reference lArray reference

9 oop Object Oriented Assembly uNo ordinary memory model lAll memory references are through fields. uA class file defines a class, just like any other object oriented language: lClass has: Fields (also static) Methods (also static) Constructors,... lMulti-threaded language lException support So that constructors can fail

10 oop Memory Model uAreas: lStacks (no single stack, since we have threads) Usually organized as a linked list Elements: method frames which include Local Variables Array (LVA) Operand stack (OS) Accessible by push/pop instructions. Garbage collected lHeap All objects and all arrays No object is allocated in the stack Each object is associated with a class stored in the method area Garbage collected. lMethod area Information about types, constant pool, fields and method i Inaccessible to programmer Garbage collected

11 oop Typed Instructions uMost JVM instructions are typed ! lEnables bytecode verification at load time  “xload v” (x ∈ {a, i, l, f, d}) lLoads (i.e. pushes) a variable v on the stack lThe prefix specifies the type lIf x = l (long) or x = d (double) then two words are pushed lOtherwise, the type annotation is only for type checking u“xastore” lStores in an array (the array, the index and the stored value are popped from the operand stack) uThis is the first successful attempt to bring type safety to a lower level language

12 oop JVM Instruction Set 1.Arithmetic 2.Stack Manipulation 3.Variables and Constants 4.Conversions 5.Arrays 6.Objects (methods and fields) 7.Control

13 oop Some Instructions uArithmetic: liadd, isub, imul, idiv, ineg, irem lBitwise (ints & longs) : iand, ior, ixor, ishl, ishr, iushr uStack Manipulation: lSwap, pop, dup lVersions that work on two words at a time: pop2, dup2 uLoad and Store: lLocals -> Stack: [i/f/l/d/a]load n l Stack ->Locals: [i/f/l/d/a]store n l Specialized load and store instructions: iload_1 pushes int from local variable position one onto stack

14 oop Some More Instructions uType Conversions (casts) lThe JVM pops the value at the top of the stack, converts it, and pushes the result back onto the stack. li2f converts int to float uInstructions for Arrays lnewarrray : Allocates an array of primitives lanewarrray : Allocates an array of references uInstructions for Objects lnew Followed (separately) by call to constructor, lgetfield linvokevirtual Stack:... object-reference params ->... returned-value

15 oop Some More Instructions uControl Instructions lAll control structures (if, switch, while, for, break, continue) are translated to labels and branches to labels Labels have method scope lLabels are translated into offsets from the beginning of the branch instruction to the beginning of the labeled instruction lgoto, if_icmpeq, if_acmpeq, ifnull…

16 oop Type Checking Strategies uNone (e.g., PDP11 assembly) uCompile time only (e.g., C++) uRuntime only (e.g., Smalltalk) uCompile time and runtime (e.g., C#) uLoad time: JVM lRationale: No compilation process Any hacker can mess with the bytecodes.

17 oop Type Checking in the JVM uAt each code location (byte offset of the method code) lThe number of cells used in LVA and OS is known. lEach cell has one, and only one type Primitive/reference. lOnly instructions that treat the cells “as they should” are allowed: No arithmetical operations on characters. Cannot push two integers and then pop a long. Only apply methods if the object knows about them.

18 oop Verification uWhen? lMainly during the load and link process uWhy? lNo guarantee that the class file was generated by a Java compiler lEnhance runtime performance uExamples lThere are no operand stack overflows or underflows. lAll local variable uses and stores are valid. lThe arguments to all the Java Virtual Machine instructions are of valid types.

19 oop Verification Process uPass 1 – when the class file is loaded lThe file is properly formatted, and all its data is recognized by the JVM uPass 2 – when the class file is linked lAll checks that do not involve instructions final classes are not subclassed, final methods are not overridden. Every class (except Object ) has a superclass. All field references and method references in the constant pool have valid names, valid classes, and a valid type descriptor.

20 oop Verification Process – cont. uPass 3 – still during linking lData-flow analysis on each method. Ensure that at any given point in the program, no matter what code path is taken to reach that point: The operand stack is always the same size and contains the same types of objects. No local variable is accessed unless it is known to contain a value of an appropriate type. Methods are invoked with the appropriate arguments. Fields are assigned only using values of appropriate types. All opcodes have appropriate type arguments on the operand stack and in the local variables uPass 4 - the first time a method is actually invoked la virtual pass whose checking is done by JVM instructions The referenced method or field exists in the given class. The currently executing method has access to the referenced method or field.

21 oop JVM in Java Architecture uJava’s architecture main technologies: lThe Java programming language Source files lThe Java class file format Compiled files lThe Java API Provide access to system resources lThe Java virtual machine Runs the class files

22 oop The Java Programming Environment Compile time environmentrun time environment A.Java B.Java C.Java Java Compiler A.class B.class C.class A.class B.class C.class Java Virtual Machine Object class String class

23 oop The Java Programming Environment Compile time environmentrun time environment A.Java B.Java C.Java Java Compiler A.class B.class C.class A.class B.class C.class Java Virtual Machine Object class String class


Download ppt "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."

Similar presentations


Ads by Google