Run-time Program Management Hwansoo Han
Run-time System Run-time system refers to Set of libraries needed for correct operation of language implementation Some parts obtain all the information from subroutine arguments (e.g., heap management) Others require more extensive knowledge of the compiler or the generated program (e.g., garbage collector) 2
Compiler/Runtime Integration Examples Garbage collection Variable numbers of arguments Exception handling Event handling Coroutine & thread implementation Remote procedure call Transactional memory Dynamic linking 3
Virtual Machines A complete set of programming environment Application programming interfaces (APIs) Instruction set architecture (ISA) I/O Scheduling Other services provided by libraries and OS 4
System VM vs. Process VM System VMs (virtual machine monitors) Emulates all the hardware needed to run a standard OS Privileged and unprivileged instructions Memory mapped I/O, Virtual memory Interrupt IBM CP/CMS (1967) run multiple single-user CMS s on CP VMware, Parallels run multiple OSes Process VMs Provide an environment for a single user-level process Subset of unprivileged instructions Library-level I/O interface Java Virtual Machine (JVM) runs Java bytecode Common Language Infrastructure (CLI) runs Common Intermediate Language (CIL) 5
Java Virtual Machine (JVM) Sun Microsystems developed the Java Language (1990-1) Code in the JVM was entirely interpreted Just-in-time (JIT) compiler added in 1998 (with Java2) JVM architecture Interfaces are designed as target for a Java compiler Direct support for all the built-in and reference types Enforce definite assignment (i.e., no uninitialized variables) and type safety Built-in support for exceptions, threads, garbage collection, reflection, dynamic loading, and security Compilers targeting JVM exist for many languages other than Java Ruby, JavaScript, Python, Scheme, C, Ada, Cobol,... 6
JVM Storage Management Storage allocations in the JVM mirror those of Java language Global constant pool Set of registers Stack for each thread Method area to hold executable bytecode Heap for dynamically allocated objects 7
Constant Pool Global data Constant pool contains both program constants and a variety of symbol table information Example class Hello { public static void main(string args[]) { System.out.println( Hello, world! ); } } 8
Content of Constant Pool Asciz: zero-terminated ASCII string Textual names Files (14) Classes (21, 22, 23, 26) Methods (7, 11, 27) Fields (24) Others (9, 10, 13) Type signatures Field & method V: void Lname: class name (8, 12, 25, 28) [ output of Sun s javap ] 9
Class Files Class file is stored as a stream of byte javac Hello.java produces a class file, Hello.class Begins with a magic number (0x_cafe_babe) Multiple class files can be combined into a Java archive jar cf jar-file input-files creates a Java archive, jar-file from multiple input-files jar tf jar-file views the content of a Java archive, jar-file jar xf jar-file extracts the content of a Java archive, jar-file 10
Java Bytecode Instructions are operated on a stack based machine An entry of Class file s method table holds Bytecode for a method Other information Number of local variables, including parameters Maximum depth required in the operand stack Table of exception handler information Bytecode range covered by this handler Address (index in the code) of the handler itself Type of exception caught (an index into the constant pool) 11
Bytecode Instruction Set Format Opcode of a single byte Arguments, if any, occupying subsequent bytes (big-endian) Most instructions do not need arguments Version 2 (current as of early 2008) Load/store move values between operand stack and local variable array Arithmetic integer and floating-point operation on operand stack values Type conversion widen or narrow values among built-in types Object management create/query/access the objects & arrays Operand stack management push, pop, duplicate, swap Control transfer conditional, unconditional, multiway branches (switch) Method calls call/return of methods (including constructor/initializers) Exceptions throw Monitors enter and exit (wait, notify, notifyall are invoked) 12
Java Program - example List insert 13
Java Bytecode - example Special load/store 14 First 4 local var array aload_[0123] : reference load iload_[0123] : integer load astore_[0123] : reference store istore_[0123] : integer store Access fields Object reference in the stack Field argument (index of constant pool) getfield, putfield Local var array Operand stack 0: 1: 2: 3: this v n top public void insert(int v) { node n = head; while (n.next!= null && n.next.val < v) {... }... Code: Stack=3, Locals=4, Args_size=2 0: aload_0 // this 1: getfield #4 // head 4: astore_2 5: aload_2 // n 6: getfield #5 // n.next 9: ifnull 31 12: aload_2 // n 13: getfield #5 // n.next 16: getfield #6 // n.next.val 19: iload_1 // v 20: if_icmpge 31
Java Bytecode example (cont d) Control transfer ifnull, if_icmpge, goto,... Object management new, newarray Operand stack management dup Method call/return invokespecial, invokeinterface, invokestatic, invokevirtual, return, areturn, ireturn [ output of Sun s javap ] 15
Common Language Infrastructure (CLI) History of CLI Began at Microsoft in the late 1990s Need for interoperability among programming languages running on Windows platforms For.NET, a specification for CLI VM was standardized by ECMA in 2001 Contributions of CLI Common Type System (CTS) for all supported languages Virtual Execution System (VES) virtual machine architecture Common Intermediate Language (CIL) instruction set for VES Strong resemblance to Java bytecode Portable Executable (PE) a portable file format for code and metadata Based on Common Object File Format (COFF) from AT&T System V Unix Languages supported 16 C#, Visual Basic, C++, Jscript, F# (an ML descendant)
Architecture of CLI Similarities between CLI and JVM Multi-threaded, stack-based virtual machine Garbage collection, exceptions, Virtual method dispatch, mix-in inheritance Platform-independent, self-descriptive, bytecode notation Safety features as in JVMs Definite assignment, strong typing, Protection against overflow/underflow of the operand stack 17
Architecture of CLI (cont d) Contrasts between CLI and JVM 18 Richer type system for CLI Both value and reference variable of structured types Contiguous multidimensional arrays, function pointers,... Richer calling mechanisms in CLI Explicit tail-recursion Value and reference parameters, variable number of parameters Multiple return values, non-virtual methods Unsafe code made explicit in CLI (for unverifiable code) Non-converting type cast Dynamic allocation of non-garbage-collected memory Pointer-to non-heap data, pointer arithmetic Miscellaneous CLI support Global data and functions, Local variables with elaboration-time binding, Optional detection of arithmetic overflow
Late Binding of Machine Code Just-in-time (JIT) compilation JIT system compiles programs immediately prior to execution source/intermediate code to machine code Can add significant delay to program start-up time Cannot apply expensive optimizations Not global, interprocedural, but local optimizations Source-to-bytecode compiler (javac) handles heavy machineindependent optimizations instead, but still heavy machine-code level optimizations cannot be done Use a combination of interpreter and JIT compilation to reduce the overhead of start-up time and latency Incremental JIT compilation Executing with interpreter, compiling in parallel for future calls Caching the compiled code for later use 19
Dynamic Compilation Useful cases for delayed compilation Source or bytecode was not created or discovered until run time Wish to perform optimizations that depend on information gathered during execution Hot paths discovery from profiling 20 Most programs spend most of time in a small fraction of code Aggressive optimizations on hot paths can yield large improvement Making hot paths contiguous in memory Unrolling loops Assigning frequently used expressions to registers Scheduling instructions to minimize the pipeline stalls Allow statically unsafe optimizations, which is OK dynamically In-lining functions which is proved not to be extended SUN s HotSpot JVM and JIT (first released in 1999) Microsoft s CIL-to-machine-code compiler Common Lisp compiler
Binary Translation Recompilation of object code Already compiled code run on a machine with different ISA Apple s Rosetta system (run PowerPC Mac code on x86 Mac) Mac 68K emulator (interpret 68K code for PowerPC) DEC FX!32 for MS Windows (interpret and hot-path translation) HP Aries (PA-RISC to Itanium), Intel IA-32 EL (x86 to Itanium) Dynamic optimizations for long-running programs Fast compilation first, aggressive profile-based dynamic optimizations Partial execution trace (hot path) optimization removes branch overhead HP Dynamo for PA-RISC, DynamoRIO for x86 Challenges from the loss of information in the original code 21 Object code typically lacks type information and control-flow constructs