CSCE 314 Lecture 37

From Notes
Jump to navigation Jump to search

« previous | Friday, December 2, 2011 | next »


Java Virtual Machine

Designed for Java, but has potential to translate from any programming language.

JIT compilation: if a particular piece of code is run several times, JVM might compile it into native code to make it run faster.

Instructions stored in class files as platform-independent bytecode

Very simple stack-based machine: a software abstraction of a computer

Instruction Set

Bytecode is in binary format, so unintelligible to humans. Mnemonics are similar to what assembly does for machine code.

Views memory as an array of bytes:

  • Constructing an object: asks for a contiguous set of memory locations
  • Accessing a field: jump to bytes at a specific offset
  • Call a function: go to that location in memory

JVM does not allow byte-level access.

Sample program

getstatic java/lang/System/out Ljava/io/PrintStream;                    # load necessary libraries
ldc "Hello, World"                                                      # push "Hello, World" onto stack
invokevirtual java/io/PrintStream/println (Ljava/lang/String;)V         # invoke virtual function that takes a reference to a string and return void

Bytecode Forrmat

Operation: mnemonic, operand1, operand2, ...

Forms: mnemonic = opcode


Class File format

Do not have to be stored in files:

  • Database
  • Network
  • JAR file

ClassLoader fetches classes to load


Verifier

Protects machine by rejecting programs that might subvert JVM security