CSc 453 Interpreters & Interpretation

Similar documents
Compiling Techniques

High-Level Language VMs

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

Java: framework overview and in-the-small features

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

Compiler construction 2009

Intermediate Representations

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

CSc 520 Principles of Programming Languages

Just-In-Time Compilation

CSc 453. Compilers and Systems Software. 18 : Interpreters. Department of Computer Science University of Arizona. Kinds of Interpreters

Optimization Techniques

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

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

Part VII : Code Generation

Dispatch techniques and closure representations

CSc 553. Principles of Compilation. 2 : Interpreters. Department of Computer Science University of Arizona

CS577 Modern Language Processors. Spring 2018 Lecture Interpreters

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

Context Threading: A flexible and efficient dispatch technique for virtual machine interpreters

Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM

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

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

Interaction of JVM with x86, Sparc and MIPS

Intermediate Representations

Translating JVM Code to MIPS Code 1 / 43

JAM 16: The Instruction Set & Sample Programs

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Just In Time Compilation

Oak Intermediate Bytecodes

Running class Timing on Java HotSpot VM, 1

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

Static Program Analysis

We can emit stack-machine-style code for expressions via recursion

Principles of Compiler Design

Playing with bird guts. Jonathan Worthington YAPC::EU::2007

Compiler-guaranteed Safety in Code-copying Virtual Machines

Compiler construction 2009

EDAN65: Compilers, Lecture 13 Run;me systems for object- oriented languages. Görel Hedin Revised:

H.-S. Oh, B.-J. Kim, H.-K. Choi, S.-M. Moon. School of Electrical Engineering and Computer Science Seoul National University, Korea

Intermediate Representations

A Trace-based Java JIT Compiler Retrofitted from a Method-based Compiler

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Compiler Construction I

Recap: Printing Trees into Bytecodes

Virtual Machine Showdown: Stack Versus Registers

Just-In-Time Compilers & Runtime Optimizers

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

Course Overview. Levels of Programming Languages. Compilers and other translators. Tombstone Diagrams. Syntax Specification

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

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

02 B The Java Virtual Machine

CSE 504: Compiler Design. Code Generation

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

CS 2210 Programming Project (Part IV)

The Java Language Implementation

Chapter 5. A Closer Look at Instruction Set Architectures

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Run-time Program Management. Hwansoo Han

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Intermediate representation

Hardware Emulation and Virtual Machines

On the Design of the Local Variable Cache in a Hardware Translation-Based Java Virtual Machine

CSE 401/M501 Compilers

CSCE 314 Programming Languages

CSc 453 Intermediate Code Generation

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

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

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

SSA Based Mobile Code: Construction and Empirical Evaluation

A Tour of Language Implementation

Implementing Higher-Level Languages. Quick tour of programming language implementation techniques. From the Java level to the C level.

Instruction Set Principles and Examples. Appendix B

BEAMJIT: An LLVM based just-in-time compiler for Erlang. Frej Drejhammar

Mixed Mode Execution with Context Threading

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

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

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

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

YETI. GraduallY Extensible Trace Interpreter VEE Mathew Zaleski, Angela Demke Brown (University of Toronto) Kevin Stoodley (IBM Toronto)

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

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Course Administration

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

What are compilers? A Compiler

Jazelle ARM. By: Adrian Cretzu & Sabine Loebner

Chapter 2A Instructions: Language of the Computer

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Intermediate Code Generation

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

Delft-Java Link Translation Buffer

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

Three-Address Code IR

Emulation. Michael Jantz

Java On Steroids: Sun s High-Performance Java Implementation. History

<Insert Picture Here> Maxine: A JVM Written in Java

Intermediate Code Generation

CODE GENERATION Monday, May 31, 2010

Evolution of a Java just-in-time compiler for IA-32 platforms

Transcription:

CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson Interpreters An interpreter is a program that executes another program. An interpreter implements a virtual machine, which may be different from the underlying hardware platform. Examples: Java Virtual Machine; VMs for high-level languages such as Scheme, Prolog, Icon, Smalltalk, Perl, Tcl. The virtual machine is often at a higher level of semantic abstraction than the native hardware. This can help portability, but affects performance. CSc 453: Interpreters & Interpretation 2 1

Interpretation vs. Compilation CSc 453: Interpreters & Interpretation 3 Interpreter Operation ip = start of program; while ( done ) { op = current operation at ip; execute code for op on current operands; advance ip to next operation; CSc 453: Interpreters & Interpretation 4 2

Interpreter Design Issues Encoding the operation I.e., getting from the opcode to the code for that operation ( dispatch ): byte code (e.g., JVM) indirect threaded code direct threaded code. Representing operands register machines: operations are performed on a fixed finite set of global locations ( registers ) (e.g.: SPIM); stack machines: operations are performed on the top of a stack of operands (e.g.: JVM). CSc 453: Interpreters & Interpretation 5 Byte Code Operations encoded as small integers (~1 byte). The interpreter uses the opcode to index into a table of code addresses: while ( TRUE ) { byte op = ip; switch ( op ) { case ADD: perform addition; break; case SUB: perform subtraction; break; Advantages: simple, portable. Disadvantages: inefficient. CSc 453: Interpreters & Interpretation 6 3

Direct Threaded Code Indexing through a jump table (as with byte code) is expensive. Idea: Use the address of the code for an operation as the opcode for that operation. The opcode may no longer fit in a byte. while ( TRUE ) { addr op = *ip; goto *op; /* jump to code for the operation */ More efficient, but the interpreted code is less portable. [James R. Bell. Threaded Code. Communications of the ACM, vol. 16 no. 6, June 1973, pp. 370 372] CSc 453: Interpreters & Interpretation 7 Byte Code vs. Threaded Code Control flow behavior: [based on: James R. Bell. Threaded Code. Communications of the ACM, vol. 16 no. 6, June 1973, pp. 370 372] byte code: threaded code: CSc 453: Interpreters & Interpretation 8 4

Indirect Threaded Code Intermediate in portability, efficiency between byte code and direct threaded code. Each opcode is the address of a word that contains the address of the code for the operation. Avoids some of the costs associated with translating a jump table index into a code address. while ( TRUE ) { addr op = *ip; goto **op; /* jump to code for the operation */ [R. B. K. Dewar. Indirect Threaded Code. Communications of the ACM, vol. 18 no. 6, June 1975, pp. 330 331] CSc 453: Interpreters & Interpretation 9 Example program operations add mul sub mul add memory address 10000 11000 12000 13000 operation add: sub: mul: div: byte code 17 23 18 23 17 Op Table index contents 17 10000 18 11000 23 12000 27 13000 direct threaded 10000 12000 11000 12000 10000 indirect threaded 6000 6008 6004 6008 6000 Op Table addr contents 6000 10000 6004 11000 6008 12000 6012 13000 CSc 453: Interpreters & Interpretation 10 5

Handling Operands 1: Stack Machines Used by Pascal interpreters ( 70s and 80s); resurrected in the Java Virtual Machine. Basic idea: operands and results are maintained on a stack; operations pop operands off this stack, push the result back on the stack. Advantages: simplicity compact code. Disadvantages: code optimization (e.g., utilizing hardware registers effectively) not easy. CSc 453: Interpreters & Interpretation 11 Stack Machine Code The code for an operation op x 1,, x n is: push x n push x 1 op Example: JVM code for x = 2 y 1 : iconst 1 /* push the integer constant 1 */ iload y /* push the value of the integer variable y */ iconst 2 imul /* after this, stack contains: (2*y), 1 */ isub istore x /* pop stack top, store to integer variable x */ CSc 453: Interpreters & Interpretation 12 6

Generating Stack Machine Code Essentially just a post-order traversal of the syntax tree: void gencode(struct syntaxtreenode tnode ) { if ( IsLeaf( tnode ) ) { else { n = tnode n_operands; for (i = n; i > 0; i--) { gencode( tnode operand[i] ); /* traverse children first */ /* for */ gen_instr( opcode_table[tnode op] ); /* then generate code for the node */ /* if [else] */ CSc 453: Interpreters & Interpretation 13 Handling Operands 2: Register Machines Basic idea: Have a fixed set of virtual machine registers; Some of these get mapped to hardware registers. Advantages: potentially better optimization opportunities. Disadvantages: code is less compact; interpreter becomes more complex (e.g., to decode VM register names). CSc 453: Interpreters & Interpretation 14 7

Just-in in-time Compilers (JITs( JITs) Basic idea: compile byte code to native code during execution. Advantages: original (interpreted) program remains portable, compact; the executing program runs faster. Disadvantages: more complex runtime system; performance may degrade if runtime compilation cost exceeds savings. CSc 453: Interpreters & Interpretation 15 Improving JIT Effectiveness Reducing Costs: incur compilation cost only when justifiable; invoke JIT compiler on a per-method basis, at the point when a method is invoked. Improving benefits: some systems monitor the executing code; methods that are executed repeatedly get optimized further. CSc 453: Interpreters & Interpretation 16 8

Method Dispatch: vtables vtables ( virtual tables ) are a common implementation mechanism for virtual methods in OO languages. The implementation of each class contains a vtable for its methods: each virtual method f in the class has an entry in the vtable that gives f s address. each instance of an object gets a pointer to the corresponding vtable. to invoke a (virtual) method, get its address from the vtable. CSc 453: Interpreters & Interpretation 17 VMs with JITs Each method has vtable entries for: byte code address; native code address. Initially, the native code address field points to the JIT compiler. When a method is first invoked, this automatically calls the JIT compiler. The JIT compiler: generates native code from the byte code for the method; patches the native code address of the method to point to the newly generated native code (so subsequent calls go directly to native code); jumps to the native code. CSc 453: Interpreters & Interpretation 18 9

JITs: : Deciding what to Compile For a JIT to improve performance, the benefit of compiling to native code must offset the cost of doing so. E.g., JIT compiling infrequently called straight-line code methods can lead to a slowdown! We want to JIT-compile only those methods that contain frequently executed ( hot ) code: methods that are called a large number of times; or methods containing loops with large iteration counts. CSc 453: Interpreters & Interpretation 19 JITs: : Deciding what to Compile Identifying frequently called methods: count the number of times each method is invoked; if the count exceeds a threshold, invoke JIT compiler. (In practice, start the count at the threshold value and count down to 0: this is slightly more efficient.) Identifying hot loops: modify the interpreter to snoop the loop iteration count when it finds a loop, using simple bytecode pattern matching. use the iteration count to adjust the amount by which the invocation count for the method is decremented on each call. CSc 453: Interpreters & Interpretation 20 10

Typical JIT Optimizations Choose optimizations that are cheap to perform and likely to improve performance, e.g.: inlining frequently called methods: consider both code size and call frequency in inlining decision. exception check elimination: eliminate redundant null pointer checks, array bounds checks. common subexpression elimination: avoid address recomputation, reduce the overhead of array and instance variable access. simple, fast register allocation. CSc 453: Interpreters & Interpretation 21 11