CS412/413 Introduction to Compilers and Translators May 3, 1999 Lecture 34: Compiler-like Systems JIT bytecode interpreter src-to-src translator bytecode.

Slides:



Advertisements
Similar presentations
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Advertisements

1 Compiler Construction Intermediate Code Generation.
Program Representations. Representing programs Goals.
Compiler Construction Sohail Aslam Lecture IR Taxonomy IRs fall into three organizational categories 1.Graphical IRs encode the compiler’s knowledge.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
CPSC Compiler Tutorial 9 Review of Compiler.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
CS 536 Spring Code generation I Lecture 20.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Intermediate Code. Local Optimizations
1 Programming Languages Translation  Lecture Objectives:  Be able to list and explain five features of the Java programming language.  Be able to explain.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
3-1 3 Compilers and interpreters  Compilers and other translators  Interpreters  Tombstone diagrams  Real vs virtual machines  Interpretive compilers.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Precision Going back to constant prop, in what cases would we lose precision?
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CS 355 – Programming Languages
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Lecture 10 : Introduction to Java Virtual Machine
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Compiler Chapter# 5 Intermediate code generation.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Chapter 7 Syntax-Directed Compilation (AST & Target Code) 1.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Joey Paquet, 2000, Lecture 10 Introduction to Code Generation and Intermediate Representations.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Overview of Compilers and JikesRVM John.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction to Code Generation and Intermediate Representations
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Code Generation CPSC 388 Ellen Walker Hiram College.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
INTERMEDIATE LANGUAGES SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compiler Chapter 9. Intermediate Languages Sung-Dong Kim Dept. of Computer Engineering, Hansung University.
CS 404 Introduction to Compiler Design
Component 1.6.
Compiler Design (40-414) Main Text Book:
CS 3304 Comparative Languages
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Chapter 9. Intermediate Languages
Overview of Compilation The Compiler BACK End
课程名 编译原理 Compiling Techniques
CSc 453 Interpreters & Interpretation
Lecture 30 (based on slides by R. Bodik)
Overview of Compilation The Compiler BACK End
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
CSc 453 Interpreters & Interpretation
Presentation transcript:

CS412/413 Introduction to Compilers and Translators May 3, 1999 Lecture 34: Compiler-like Systems JIT bytecode interpreter src-to-src translator bytecode compiler

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 2 Administration Project code must be turned in Friday even if you haven’t finished the programming assignment -- at minimum, turn in something that can compile programs. If parts of program are incomplete, include explanation of design, and components that are working

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 3 A Compiler Lexical analysis Parsing Intermediate Code Generation MIR Optimization Code generation LIR optimization HIR Optimization Linking & Loading “Compiler Classic” bytecode compiler JIT compiler front end back end source-to- source translator interpreter

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 4 Other options Compile from source to another high-level language (source-to-source translator), let other compiler deal with code gen, etc. Compile from source to an intermediate code format for which a back end already exists (ucode, RTF, LCC,...) Compile from source to an intermediate code format executable by an interpreter –abstract syntax tree –bytecodes –threaded code (call- or pointer-threaded) Advantage: Architecture independence

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 5 Source-to-source translator Idea: choose well-supported high-level language (e.g. C, C++, Java) as target Translate AST to high-level language constructs instead of to IR, pass translated code off to underlying compiler Advantage: easy, can leverage good underlying compiler technology. Examples: C++ (to C), PolyJ (to Java), Toba (JVM to C) Disadvantages: target language won’t support all features, optimization harder in target language, language may impose extra checks

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 6 Compiling to C C doesn’t impose extra checks, is reasonably close to assembly (but can’t support static exception tables!), widely available Mismatch: doesn’t allow statements underneath expressions; need to translate and canonicalize in one step Translation of an expression into C is: –a sequence of statements to be executed –an expression to be evaluated afterward E  S 1 ;…;S n, E’

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 7 Translation rules Translation still can be performed by recursive traversal of AST Iota  C rules: E  S, E’ id = E;  S, id = E’; S i  S i ’, E i ’ { S 1 ;…; S n }  S 1 ’;…; S n ’, E n ’ block assignment

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 8 Translating to Java Same problems as C, plus: Java is type- safe (good in a HLL, not so good in an intermediate language!) May need to use cast and instanceof expressions in generated code: slow in Java

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 9 Back Ends Several standard intermediate code formats exist with back ends for various architectures -- can reuse back ends –p-code: very old stack machine format –UCODE : old Stanford/MIPS stack machine format –Java bytecode: new stack machine format –RTL: GNU gcc, etc. –SUIF: Stanford format for optimization –LCC: Lightweight C compiler

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 10 Intermediate code formats Quadruples –compact, similar to machine code, good for standard optimization techniques Stack-machine –easy to generate code for –not compact (contrary to popular opinion) –can be converted back into quadruples –used by some (sort of) high-level languages: FORTH, PostScript, HP calculators

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 11 Stack machine format Code is a sequence of stack operations (not necessarily the same stack as the call stack) push CONST : add CONST to the top of stack pop : discard top of stack store : in memory location specified by top of stack store element just below. load : replace top of stack with memory location it points to +, *, /, -, … : replace top two elems w/ result of operation

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 12 Generating code Stack operations mostly don’t name operands (implicit): can code in 1 byte Expression is translated to code that leaves its value on the top of stack Translation of E 1 + E 2 :  E 1 + E 2    E 1  ;  E 2  ; + Translation of id = E :  id = E    E  ; push addr(id); store Good for very simple code generation

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 13 Verbosity Values get “trapped” down low on stack especially with subexpression elim. Need instruction that re-pushes element at known stack index on top; instruction is used often Might as well have abstract register operands! Result: less compact than a register- based format; extra copies of data too

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 14 Stack machine  register machine At each point in code, keep track of stack depth (if possible) Assign temporaries according to depth Replace stack operands with quadruples using these temporaries abc a + b*c push a ; 0 push b ; 1 push c ; 2 * ; 1 + ; 0 t0 = a t1 = b t2 = c t1 = t1 * t2 t0 = t0 + t1 012 ab*c a+b*c

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 15 JIT compilers Particularly widely available back end with well-defined intermediate code (JVM bytecode) Generate code by reconstructing registers as discussed Compilation is done on-the-fly: generating code quickly is essential  generated code quality is usually low HotSpot: new Sun JIT. High-quality optimization (esp. inlining and specialization), but used sparingly

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 16 Interpreters “Why generate machine code at all? Just run it. Processors are really fast” Options: –token interpreters (parsing on the fly) -- reallly slow (>1000x) –AST interpreters x –threaded interpreters x –bytecode interpreters x

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 17 AST interpreters “Yet another recursive traversal” For every node type in AST, add method Object evaluate(RunTimeContext r) Evaluate method is implemented recursively Object PlusNode.evaluate(r) { return left.evaluate(r).plus(right.evaluate(r)); } Variables, etc. looked up in r ; some help from AST yields big speed-ups (e.g. pre- computed variable locations)

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 18 Threaded interpreters Code is represented as a bunch of blocks of memory connected by pointers. Blocks are either code blocks or pointer blocks. Execution is (mostly) depth-first traversal Each block starts with a single jump instruction JMP next JMP machine code RET

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 19 Threaded interpreters Can call blocks directly regardless of kind Conditional branches, other control structure embedded in special machine code blocks that munge stack Interpreter proper is ~10 instructions of code: very space-efficient! Long favored by FORTH fans: whole language runtime fits in 4K memory

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 20 Call-threaded interpreters Instead of pointers in pointer blocks, store CALL instructions Depth-first traversal done by processor automatically Faster, trivial code generation required CALL machine code RET CALL RET CALL RET

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 21 Bytecode interpreters Problem with threaded interpreters: operations with immediate arguments require multiple words (code bloat) Observation: most interpreter instructions can be encoded in less than a word, many even in just a byte (esp. with stack-machine model) Tradeoff: interpreter size vs. executable code size

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 22 Implementing a bytecode interpreter Bytecode interpreter simulates a simple architecture (either stack or register machine) Interpreter state: –current code pointer –current simulated return stack –current registers or stack & stack pointer Interpreter code is a big loop containing a switch over kinds of bytecode instructions –one big function: optimization techniques work well. Ideally: no recursion on function calls Result: about 10x slowdown if done right

CS 412/413 Introduction to Compilers and Translators -- Spring '99 Andrew Myers 23 Summary Building a new system for executing code doesn’t require construction of a full compiler Cost-effective strategies: source-to-source translation or translation to an existing intermediate code format Most material covered in this course is still helpful for these approaches High performance: translate to C Portability, extensibility: translate to Java or JVM