10/12/2015© 2002-08 Hal Perkins & UW CSEV-1 CSE P 501 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Winter 2008.

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

1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Names and Bindings.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
Java security (in a nutshell)
Memory Management Case study (JVM & CLR) KIRAN KUMAR V KIRAN KUMAR V LENIN THUMMALAPALLI LENIN THUMMALAPALLI.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS 536 Spring Run-time organization Lecture 19.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
Run time vs. Compile time
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.
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Java Security Updated May Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security.
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
OOP Languages: Java vs C++
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
1 The Java Virtual Machine Yearly Programming Project.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Lecture 10 : Introduction to Java Virtual Machine
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
1 Introduction to JVM Based on material produced by Bill Venners.
Compiler Construction
Java 2 security model Valentina Casola. Components of Java the development environment –development lifecycle –Java language features –class files and.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
12/10/2002© 2002 Hal Perkins & UW CSET-1 CSE 582 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Autumn 2002.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Lecture 04: Instruction Set Principles Kai Bu
Processes and Virtual Memory
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented Software Development 4. C# data types, objects and references.
Programming Languages and Paradigms Activation Records in Java.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Hello world !!! ASCII representation of hello.c.
RealTimeSystems Lab Jong-Koo, Lim
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Design issues for Object-Oriented Languages
JAVA MULTIPLE CHOICE QUESTION.
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Java Primer 1: Types, Classes and Operators
Java Programming Language
Java security (in a nutshell)
Topic: JAVA .Class File Structure
Java Virtual Machine Complete subject details are available at:
Java Virtual Machine (JVM)
Byte Code Verification
Java Implementation – JVMs, JITs &c Hal Perkins Summer 2004
Presentation transcript:

10/12/2015© Hal Perkins & UW CSEV-1 CSE P 501 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Winter 2008

10/12/2015© Hal Perkins & UW CSEV-2 Agenda Java virtual machine architecture.class files Class loading Execution engines Interpreters & JITs – various strategies Exception Handling

10/12/2015© Hal Perkins & UW CSEV-3 Java Implementation Overview Java compiler (javac et al) produces machine-independent.class files Target architecture is Java Virtual Machine (JVM) – simple stack machine Java execution engine (java) Loads.class files (often from libraries) Executes code Either interprets stack machine code or compiles to native code (JIT)

10/12/2015© Hal Perkins & UW CSEV-4 JVM Architecture Abstract stack machine Implementation not required to use JVM specification literally Only requirement is that execution of.class files has specified effect Multiple implementation strategies depending on goals Compilers vs interpreters Optimizing for servers vs workstations

10/12/2015© Hal Perkins & UW CSEV-5 JVM Data Types Primitive types byte, short, int, long, char, float, double, boolean Reference types Non-generic only (more on this later)

10/12/2015© Hal Perkins & UW CSEV-6 JVM Runtime Data Areas (1) Semantics defined by the JVM Specification Implementer may do anything that preserves these semantics Per-thread data pc register Stack Holds frames (details below) May be a real stack or may be heap allocated

10/12/2015© Hal Perkins & UW CSEV-7 JVM Runtime Data Areas (2) Per-VM data – shared by all threads Heap – objects allocated here Method area – per-class data Runtime constant pool Field and method data Code for methods and constructors Native method stacks Regular C-like stacks or equivalent

10/12/2015© Hal Perkins & UW CSEV-8 Frames Created when method invoked; destroyed when method completes Allocated on stack of creating thread Contents Local variables Operand stack for JVM instructions Reference to runtime constant pool Symbolic data that supports dynamic linking Anything else the implementer wants

10/12/2015© Hal Perkins & UW CSEV-9 Representation of Objects Implementer's choice JVM spec 3.7: “The Java virtual machine does not mandate any particular internal structure for objects” Likely possibilities Data + pointer to Class object Pair of pointers: one to heap-allocated data, one to Class object

10/12/2015© Hal Perkins & UW CSEV-10 JVM Instruction Set Stack machine Byte stream Instruction format 1 byte opcode 0 or more bytes of operands Instructions encode type information Verified when class loaded

10/12/2015© Hal Perkins & UW CSEV-11 Instruction Sampler (1) Load/store Transfer values between local variables and operand stack Different opcodes for int, float, double, addresses Load, store, load immediate Special encodings for load0, load1, load2, load3 to get compact code for first few local vars

10/12/2015© Hal Perkins & UW CSEV-12 Instruction Sampler (2) Arithmetic Again, different opcodes for different types byte, short, char & boolean use int instructions Pop operands from operand stack, push result onto operand stack Add, subtract, multiply, divide, remainder, negate, shift, and, or, increment, compare Stack management Pop, dup, swap

10/12/2015© Hal Perkins & UW CSEV-13 Instruction Sampler (3) Type conversion Widening – int to long, float, double; long to float, double, float to double Narrowing – int to byte, short, char; double to int, long, float, etc.

10/12/2015© Hal Perkins & UW CSEV-14 Instruction Sampler (4) Object creation & manipulation New class instance New array Static field access Array element access Array length Instanceof, checkcast

10/12/2015© Hal Perkins & UW CSEV-15 Instruction Sampler (5) Control transfer Unconditional branch – goto, jsr (originally used to implement finally blocks) Conditional branch – ifeq, iflt, ifnull, etc. Compound conditional branches - switch

10/12/2015© Hal Perkins & UW CSEV-16 Instruction Sampler (6) Method invocation invokevirtual invokeinterface invokespecial (constructors, superclass, private) invokestatic Method return Typed value-returning instructions Return for void methods

10/12/2015© Hal Perkins & UW CSEV-17 Instruction Sampler (7) Exceptions: athrow Synchronication Model is monitors (cf any standard operating system textbook) monitorenter, monitorexit Memory model greatly cleaned up in Java 5

JVM and Generics Surprisingly, JVM has no knowledge of generic types Not checked at runtime, not available for reflection, etc. Compiler erases all generic type info Resulting code is pre-generics Java Objects are class Object in resulting code & appropriate casts are added Only one instance of each type-erased class – no code expansion/duplication (as in C++ templates) 10/12/2015© Hal Perkins & UW CSEV-18

Generics and Type Erasure Why did they do that? Compatibility: need to interop with existing code that doesn’t use generics Existing non-generic code and new generic libraries, or Newly written code and older non-generic classes Tradeoffs: only reasonable way to add generics given existing world, but Generic type information unavailable at runtime (casts, instanceof, reflection) Can’t create new instance or array of generic type C#/CLR is different – generics reflected in CLR 10/12/2015© Hal Perkins & UW CSEV-19

10/12/2015© Hal Perkins & UW CSEV-20 Class File Format Basic requirements are tightly specified Implementations can extend Examples: data to support debugging or profiling JVMs must ignore extensions they don’t recognize Very high-level, symbolic, lots of metadata – much of the symbol table/type/other attribute data produced by a compiler front end Supports dynamic class loading Allows runtime compilation (JITs), etc.

10/12/2015© Hal Perkins & UW CSEV-21 Contents of Class Files (1) Starts with magic number (0xCAFEBABE) Constant pool - symbolic information String constants Class and interface names Field names All other operands and references in the class file are referenced via a constant pool offset Constant pool is essentially a “symbol table” for the class

10/12/2015© Hal Perkins & UW CSEV-22 Contents of Class Files (2) Class and superclass info Index into constant pool Interface information Index into constant pool for every interface this class implements Fields declared in this class proper, but not inherited ones (includes type info) Methods (includes type info) Includes byte code instructions for methods that are not native or abstract

10/12/2015© Hal Perkins & UW CSEV-23 Constraints on Class Files (1) Long list; verified at class load time  execution engine can assume valid, safe code Some examples of static constraints Target of each jump must be an opcode No jumps to the middle of an instruction or out of bounds Operands of load/store instructions must be valid index into constant pool new is only used to create objects; anewarray is only used to create arrays Only invokespecial can call a constructor Index value in load/store must be in bounds Etc. etc. etc.

10/12/2015© Hal Perkins & UW CSEV-24 Constraints on Class Files (2) Some examples of structural constraints Instructions must have appropriate type and number of arguments If instruction can be executed along several paths, operand stack must have same depth at that point along all paths No local variable access before being assigned a value Operand stack never exceeds limit on size No pop from empty operand stack Execution cannot fall off the end of a method Method invocation arguments must be compatible with method descriptor Etc. etc. etc. etc.

10/12/2015© Hal Perkins & UW CSEV-25 Class Loaders One or more class loader (instances of ClassLoader or its derived classes) is associated with each JVM Responsible for loading the bits and preparing them Different class loaders may have different policies Eager vs lazy class loading, cache binary representations, etc. May be user-defined, or the initial built-in bootstrap class loader

10/12/2015© Hal Perkins & UW CSEV-26 Readying.class Files for Execution Several distinct steps Loading Linking Verification Preparation Resolution of symbolic references Initialization

10/12/2015© Hal Perkins & UW CSEV-27 Loading Class loader locates binary representation of the class and reads it (normally a.class file, either in the local file system, or in a.jar file, or on the net) Once loaded, a class is identified in the JVM by its fully qualified name + class loader id A good class loader should always return the same class object given the same name Different class loaders generally create different class objects even given the same class name

10/12/2015© Hal Perkins & UW CSEV-28 Linking Combines binary form of a class or interface type with the runtime state of the JVM Always occurs after loading Implementation has flexibility on timing Example: can resolve references to other classes during verification (static) or only when actually used (lazy) Requirement is that verification must precede initialization, and semantics of language must be respected No exceptions thrown at unexpected places, for example

10/12/2015© Hal Perkins & UW CSEV-29 Linking: Verification Checks that binary representation is structurally correct Verifies static and structural constraints (see above for examples) Goal is to prevent any subversion of the Java type system May causes additional classes and interfaces to be loaded, but not necessarily prepared or verified

10/12/2015© Hal Perkins & UW CSEV-30 Linking: Preparation Creation of static fields & initialization to default values Implementations can optionally precompute additional information Method tables, for example

10/12/2015© Hal Perkins & UW CSEV-31 Linking: Resolution Check symbolic references and, usually, replace with direct references that can be executed more efficiently

10/12/2015© Hal Perkins & UW CSEV-32 Initialization Execute static initializers and initializers for static fields Direct superclass must be initialized first Constructor(s) not executed here Done by a separate instruction as part of new, etc.

10/12/2015© Hal Perkins & UW CSEV-33 Virtual Machine Startup Initial class specified in implementation- defined manner Command line, IDE option panel, etc. JVM uses bootstrap class loader to load, link, and initialize that class public static void main(String[]) method of initial class is executed to drive all further execution

10/12/2015© Hal Perkins & UW CSEV-34 Execution Engines Basic Choices Interpret JVM bytecodes directly Compile bytecodes to native code, which then executes on the native processor Just-In-Time compiler (JIT)

10/12/2015© Hal Perkins & UW CSEV-35 Hybrid Implementations Interpret or use very simple compiler most of the time Identify “hot spots” by dynamic profiling Often per-method counter incremented on each call Timer-based sampling, etc. Run optimizing JIT on hot code Data-flow analysis, standard compiler middle-end optimizations, back-end instruction selection/ scheduling & register allocation Need to balance compilation cost against responsiveness, expected benefits Different tradeoffs for desktop vs server JVMs

Memory Management JVM includes instructions for creating objects and arrays, but not deleting Garbage collection used to reclaim no-longer needed storage (objects, arrays, classes, …) Strong type system means GC can have exact information.class file includes type information GC can have exact knowledge of layouts since these are internal to the JVM More details next hour 10/12/2015© Hal Perkins & UW CSEV-36

10/12/2015© Hal Perkins & UW CSEV-37 Escape Analysis Another optimization based on observation that many methods allocate local objects as temporaries Idea: Compiler tries to prove that no reference to a locally allocated object can “escape” Not stored in a global variable or object Not passed as a parameter

10/12/2015© Hal Perkins & UW CSEV-38 Using Escape Analysis If all references to an object are local, it doesn’t need to be allocated on the heap in the usual manner Can allocate storage for it in local stack frame Essentially zero cost Still need to preserve the semantics of new, constructor, etc.

10/12/2015© Hal Perkins & UW CSEV-39 Exception Handling Goal: should have zero cost if no exceptions are thrown Otherwise programmers will subvert exception handling with the excuse of “performance” Corollary: cannot execute any exception handling code on entry/exit from individual methods or try blocks

10/12/2015© Hal Perkins & UW CSEV-40 Implementing Exception Handling Idea: Original compiler generates table of exception handler information in the.class file Entries include start and end of section of code array protected by this handler; argument type Order of entries is significant When exception is thrown, JVM searches exception table for first matching argument type that has a pc range that includes the current execution location

Summary That’s the overview – many more details, obviously, if you want to implement a JVM Primary reference: Java Virtual Machine Specification, 2nd ed, A-W, Available online: Many additional research papers & studies all over the web and in conference proceedings 10/12/2015© Hal Perkins & UW CSEV-41