Java Virtual Machine - Kirkwood Community virtual machine Source code (java or other HLL) compiler/linker java compiler class file ... –Compiled Java program makes similar call,

  • View
    216

  • Download
    3

Embed Size (px)

Transcript

  • Java Virtual Machine

    part 1

  • One more architecture: the Java Virtual Machine

    JVM is a virtual machine that executes Java Byte Code (JBC)

    start with Java source code (.java text file)

    Java compiler (javac) creates JBC file with .class extension

    standardized binary format

    single program consists of one or more class files

    multiple class files may be packaged together as a .jar file for program distribution

  • JVM execution environment

    JVM is represented by an executable program called java

    emulates JVM instruction set

    interpreter

    JBC is stack based, so JVM uses stack architecture

  • Why a virtual machine?

    Idea didnt originate with Java idea comes from time-sharing systems (circa 1960s)

    VM advantages:

    platform independence

    transcends physical limits

    ease of updates

    security safeguards

  • Platform independence

    Java compiler is platform-independent: makes no assumptions about characteristics of underlying hardware

    JVM required to run Java byte code

    Works as a wrapper around a real machines architecture so the JVM itself is extremely platform dependent

  • Java environment vs. traditional HLL environment

    hardware platform

    machine language

    java virtual machine

    Source code (java or other HLL)

    compiler/linker

    java compiler

    class file (JBC)

  • How it works

    Java compiler translates source code into JBC

    JVM acts as interpreter - translates specific byte codes into machine instructions specific to the harbor platform its running on

    Acts like giant switch/case structure: each bytecode instruction triggers jump to a specific block of code that implements the instruction in the architectures native machine language

  • JVMs superpower: transcends physical limits

    No hardware costs (both $ and resource tradeoffs)

    Because of multithreading, can have (seemingly) unlimited processor power

    No backward compatibility issues

    Can be adapted to optimize hardware resources of specific platform

    Designed from scratch in mid-90s: several generations of engineering experience led to design superior to most physical chips

  • JVM and security issues

    A virtual machine can be (and JVM is) configured to run in secure environment

    VM can intervene if a program tries to do something it shouldnt can enforce stricter security policies than those of OS

    JVM bytecode is verifiable

    most security flaws happen by accident

    byte code is checked by both compiler and JVM

    result: improved software quality & reliability

  • Downside of virtual machines

    Before Java, virtual machines relatively uncommon because of performance issues

    Takes about 1000 times longer to do an operation in software instead of hardware

    hardware advances & compiler improvements mitigate this

    in practical terms, speed difference is anywhere from 2x slower to more than about 6% slower

    VM doesnt provide direct control over hardware available with native low-level language

  • Characteristics of JVM

    Not just a virtual machine; something like a virtual operating system

    Case in point: output statement printf()

    Compiled C program calls the operating systems write() function

    Compiled Java program makes similar call, but to a JVM routine which then calls the real write() function

  • Java & threading

    Because Java is a virtual machine, it is free of some of the constraints of a real machine architecture

    Java threads exemplify this

    separate processes running in parallel

    simulates a multi-processor environment independent of actual platform

  • Characteristics of JVM

    Stack-based language & machine

    Each thread within a program has its own stack

    32-bit word size

    Relatively small instruction set (about 200 instructions)

  • Characteristics of JVM: registers

    4 registers (sort of):

    program counter (PC)

    optop: points to top of operand stack for currently-active method

    frame: points to stack frame for current method

    vars: points to start of local variables for current method

    Each program (or thread) has these, as well as its own stack

  • Characteristics of JVM: registers

    No general-purpose registers

    means more memory fetches, detrimental to performance

    tradeoff is high degree of portability

    Most instructions access stack

  • Characteristics of JVM: stack memory

    Each method call produces its own stack frame, which is pushed on the threads stack; a return instruction pops the stack

    Stack frame includes:

    local variables section

    operand stack section

  • Local variables section of stack frame

    Consists of set of word-size slots, each of which holds a single variable; includes

    parameters & locally-declared data, in order of declaration;

    if method is non-static, first slot (slot 0) contains pointer to this

  • Operand stack section of stack frame

    Operand stack section is where methods instructions operate - the stack referred to when talking about instructions operating on the stack

    Maximum depth of operand stack is determined at compile time

    Current stack depth is determined by number & type of operands on stack:

    double and long values take up two slots

    all other data types take one slot

  • JVM Method area

    Stores classes used by executing program; includes:

    bytecode & access types of methods

    values & access types of static variables

    PC points to this area location of next instruction

    Method area also includes constant pool storage for literal values used in program

  • JVM Heap

    Memory allocated for objects from this area

    Holds objects instance values and pointer to objects class in the Method area

  • JVM instruction set

    Instructions consist of one-byte opcode followed by 0 or more operands

    Instruction types include:

    load/store of local variables & object fields

    array

    arithmetic and logical

    type conversion

    control

    method call/return

  • Java Byte Code

    As assembly language code is to most HLLs, JBC is to Java

    Although most programmers work at the high level, a thorough understanding of the lower level helps us achieve better performing, lower cost software

  • JVM instructions*

    A JVM instruction consists of a one-byte opcode specifying the operation to be performed, followed by zero or more operands supplying arguments or data that are used by the operation

    Many instructions have no operands and consist only of an opcode

    * This and the next several slides are almost verbatim from the official

    reference on all things JVM; the quoted parts are in purple:

    http://java.sun.com/docs/books/jvms/second_edition/html/

    http://java.sun.com/docs/books/jvms/second_edition/html/

  • The JVM Loop

    Ignoring exceptions, the inner loop of a Java

    virtual machine interpreter is effectively

    do {

    fetch an opcode;

    if (operands)

    fetch operands;

    execute the action for the opcode;

    } while (there is more to do);

  • Opcodes and operands

    The number and size of the operands are determined by the opcode

    If an operand is more than one byte in size, then it is stored in big-endian order

    The bytecode instruction stream is only single-byte aligned

    Not assuming data alignment means that immediate data larger than a byte must be constructed from bytes at run time on many machines

  • JBC Data Types

    Correspond closely to Java types; conspicuous for its absence is boolean, which in JBC is stored as an int

    This is because it is no more (and is likely to be less) efficient in most real architectures to access a single bit as opposed to a single (32-bit) word so boolean values are stored as 1 or 0

    Other sub-word storage types (byte, short and char) are promoted to word type for arithmetic operations (implicit promotion, to us) but thats in the stack, not in memory

    Operations on these types are, effectively, int operations

  • JBC data types Data type JBC Code Explanation

    int i 32-bit signed integer

    float f 32-bit IEEE 754 floating point number

    long l 64-bit integer takes 2 stack frames

    double d 64-bit IEEE 754 floating point number (2 stack frames)

    byte b 8-bit signed integer

    short s 16-bit signed integer

    char c 16-bit unsigned integer or Unicode (UTF-16) character

    address a Objects

  • JVM stack frames

    Recall that each thread or program has its own JVM stack to store frames

    Frames are created when methods are invoked

    Frame consists of:

    operand stack

    local variable table (array)

    pointer to the runtime constant pool of the current methods class

  • JVM stack frames

    Size of both operand stack and local variable table are determined at compile time

    Operand stack stores:

    operands for opcode instructions

    operation results

    return values from methods

  • JVM instructions

    Data typing in Java requires type-specific instructions; thus for example, the add instruction comes