Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Similar documents
CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

High-Level Language VMs

Compiling Techniques

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

Run-time Program Management. Hwansoo Han

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Java Internals. Frank Yellin Tim Lindholm JavaSoft

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

Java: framework overview and in-the-small features

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

The Java Language Implementation

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Managed runtimes & garbage collection

Field Analysis. Last time Exploit encapsulation to improve memory system performance

Compiler construction 2009

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

CSCE 314 Programming Languages

SABLEJIT: A Retargetable Just-In-Time Compiler for a Portable Virtual Machine p. 1

Java TM. Multi-Dispatch in the. Virtual Machine: Design and Implementation. Computing Science University of Saskatchewan

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Introduction to Programming Using Java (98-388)

JAM 16: The Instruction Set & Sample Programs

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Delft-Java Dynamic Translation

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Short Notes of CS201

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Running class Timing on Java HotSpot VM, 1

CS201 - Introduction to Programming Glossary By

CMSC 430 Introduction to Compilers. Spring Intermediate Representations and Bytecode Formats

CSc 453 Interpreters & Interpretation

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Chapter 5. A Closer Look at Instruction Set Architectures

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Java Primer 1: Types, Classes and Operators

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Compiler construction 2009

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

Structure of Programming Languages Lecture 10

Computer Components. Software{ User Programs. Operating System. Hardware

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Page 1. Structure of von Nuemann machine. Instruction Set - the type of Instructions

CMSC 430 Introduction to Compilers. Fall Language Virtual Machines

Run-time Environments

Run-time Environments

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

JAVA PERFORMANCE. PR SW2 S18 Dr. Prähofer DI Leopoldseder

CSE 401/M501 Compilers

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Run-Time Environments/Garbage Collection

MIPS Instruction Set Architecture (2)

Plan for Today. Safe Programming Languages. What is a secure programming language?

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Program Dynamic Analysis. Overview

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3]

Jaos - Java on Aos. Oberon Event 03 Patrik Reali

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

UNIT 3

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CMSC 330: Organization of Programming Languages

Chapter 5. A Closer Look at Instruction Set Architectures

Chapter 4 Java Language Fundamentals

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Java Overview An introduction to the Java Programming Language

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Module 27 Switch-case statements and Run-time storage management

Last class: OS and Architecture. OS and Computer Architecture

Last class: OS and Architecture. Chapter 3: Operating-System Structures. OS and Computer Architecture. Common System Components

THEORY OF COMPILATION

StackVsHeap SPL/2010 SPL/20

1 Shyam sir JAVA Notes

Translating JVM Code to MIPS Code 1 / 43

G Programming Languages - Fall 2012

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

Chapter 6 Introduction to Defining Classes

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Hardware Emulation and Virtual Machines

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Bytecode Manipulation Techniques for Dynamic Applications for the Java Virtual Machine

Procedure and Object- Oriented Abstraction

CS 231 Data Structures and Algorithms, Fall 2016

Control Abstraction. Hwansoo Han

Delft-Java Dynamic Translation

C#: framework overview and in-the-small features

Garbage Collection. Hwansoo Han

Intermediate Representations

Transcription:

Agenda CSE P 501 Compilers Java Implementation JVMs, JITs &c Hal Perkins Summer 2004 Java virtual machine architecture.class files Class loading Execution engines Interpreters & JITs various strategies Garbage Collection Exception Handling Managed code execution in C#/CLR is similar 8/17/2004 2002-04 Hal Perkins & UW CSE T-1 8/17/2004 2002-04 Hal Perkins & UW CSE T-2 Java Implementation Overview Java compiler (javac, jikes) produces machine-independent.class file Target architecture is Java Virtual Machine (JVM) simple stack machine Java execution engine (java) Loads.class files Executes code Either interprets stack machine code or compiles to native code (JIT) JVM Architecture Abstract stack machine Implementation not required to mirror JVM specification Only requirement is that execution of.class files has defined effect Multiple implementation strategies depending on goals Compilers vs interpreters Optimizing for servers vs workstations 8/17/2004 2002-04 Hal Perkins & UW CSE T-3 8/17/2004 2002-04 Hal Perkins & UW CSE T-4 JVM Data Types Basic data types found in Java byte, short, int, long, char, float, double, boolean Reference Types 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-5 8/17/2004 2002-04 Hal Perkins & UW CSE T-6 CSE P 501 Su04 T-1

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-stacks or equivalent 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-7 8/17/2004 2002-04 Hal Perkins & UW CSE T-8 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 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-9 8/17/2004 2002-04 Hal Perkins & UW CSE T-10 Instruction Sampler (1) Load/store Transfer values between local variables and operand stack Different opcodes for int, float, double, address Load, store, load immediate Special encodings for load0, load1, load2, load3 to get compact code for first few local vars 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-11 8/17/2004 2002-04 Hal Perkins & UW CSE T-12 CSE P 501 Su04 T-2

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. Instruction Sampler (4) Object creation & manipulation New class instance New array Static field access Array element access Array length Instanceof, checkcast 8/17/2004 2002-04 Hal Perkins & UW CSE T-13 8/17/2004 2002-04 Hal Perkins & UW CSE T-14 Instruction Sampler (5) Control transfer Unconditional branch goto, jsr (used to implement finally blocks) Conditional branch ifeq, iflt, ifnull, etc. Compound conditional branches - switch Instruction Sampler (6) Method invocation invokevirtual invokeinterface invokespecial (constructors, superclass, private) invokestatic Method return Typed value-returning instructions Return for void methods 8/17/2004 2002-04 Hal Perkins & UW CSE T-15 8/17/2004 2002-04 Hal Perkins & UW CSE T-16 Instruction Sampler (7) Exceptions: athrow Synchronication Model is monitors (cf any standard operating system textbook) monitorenter, monitorexit 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, lots of metadata much of the symbol table/type/other attributes data found in a compiler Supports dynamic class loading Allows runtime compilation (JITs), etc. 8/17/2004 2002-04 Hal Perkins & UW CSE T-17 8/17/2004 2002-04 Hal Perkins & UW CSE T-18 CSE P 501 Su04 T-3

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 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-19 8/17/2004 2002-04 Hal Perkins & UW CSE T-20 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. 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. 8/17/2004 2002-04 Hal Perkins & UW CSE T-21 8/17/2004 2002-04 Hal Perkins & UW CSE T-22 Class Loaders One or more class loaders (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 initial built-in bootstrap class loader Readying.class Files for Execution Several distinct steps Loading Linking Verification Preparation Resolution of symbolic references Initialization 8/17/2004 2002-04 Hal Perkins & UW CSE T-23 8/17/2004 2002-04 Hal Perkins & UW CSE T-24 CSE P 501 Su04 T-4

Loading Class loader locates binary representation of the class (normally a.class file) and reads it 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 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-25 8/17/2004 2002-04 Hal Perkins & UW CSE T-26 Linking: Verification Checks that binary representation is structurally correct Verifies static and structural constraints (see above) 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 Linking: Preparation Creation of static fields & initialization to default values Implementations can optionally precompute additional information Method tables, for example 8/17/2004 2002-04 Hal Perkins & UW CSE T-27 8/17/2004 2002-04 Hal Perkins & UW CSE T-28 Linking: Resolution Check symbolic references and, usually, replace with direct references that can be executed more efficiently 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. 8/17/2004 2002-04 Hal Perkins & UW CSE T-29 8/17/2004 2002-04 Hal Perkins & UW CSE T-30 CSE P 501 Su04 T-5

Virtual Machine Startup Initial class specified in implementationdefined 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 in initial class is executed to drive all further execution 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) 8/17/2004 2002-04 Hal Perkins & UW CSE T-31 8/17/2004 2002-04 Hal Perkins & UW CSE T-32 Hybrid Implementations Interpret or use very dumb compiler most of the time Identify hot spots by dynamic profiling 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 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, ) GC must prove not needed before reclaiming 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 Can t do this for C/C++ because of incomplete type info & weak type system; best you can hope for is a conservative GC 8/17/2004 2002-04 Hal Perkins & UW CSE T-33 8/17/2004 2002-04 Hal Perkins & UW CSE T-34 Garbage Collection Basic idea Identify root set of references Registers Active stack frames Static fields in classes Trace closure of root set references Reclaim any allocated objects that are not reachable Garbage Collection Variations Compacting collectors Move active objects so they are adjacent in new heap space Advantage: better locality Need bookkeeping during move/compact sweep to handle pointers between old space and new space 8/17/2004 2002-04 Hal Perkins & UW CSE T-35 8/17/2004 2002-04 Hal Perkins & UW CSE T-36 CSE P 501 Su04 T-6

Generation Garbage Collection Observation: Programs written in most O-O languages create many short-lived objects Implication: Scanning entire heap on each GC is mostly wasted effort Strategy Allocate new objects in a small part of the heap Routine GC just collects in this nursery Objects that survive some number of GCs are moved to more permanent part of heap Still need to GC full heap occasionally Escape Analysis Another idea 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-37 8/17/2004 2002-04 Hal Perkins & UW CSE T-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. 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 8/17/2004 2002-04 Hal Perkins & UW CSE T-39 8/17/2004 2002-04 Hal Perkins & UW CSE T-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, and the 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 Object-oriented languages introduce new implementation issues, and different tradeoffs for classical compiler techniques Wide interest in the compiler research community, particularly since Java burst onto the scene Still an active area of research/development 8/17/2004 2002-04 Hal Perkins & UW CSE T-41 8/17/2004 2002-04 Hal Perkins & UW CSE T-42 CSE P 501 Su04 T-7