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

Slides:



Advertisements
Similar presentations
Intermediate Representations CS 671 February 12, 2008.
Advertisements

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.
Virtual Machines Matthew Dwyer 324E Nichols Hall
Intermediate Code Generation
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
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.
IPT Readings on Instrumentation, Profiling, and Tracing Seminar presentation by Alessandra Gorla University of Lugano December 7, 2006.
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.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
CSc 453 Interpreters & Interpretation Saumya Debray The University of Arizona Tucson.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Compiler Construction Lecture 17 Mapping Variables to Memory.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Programmer's view on Computer Architecture by Istvan Haller.
Lecture 10 : Introduction to Java Virtual Machine
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
1 Introduction to JVM Based on material produced by Bill Venners.
CS412/413 Introduction to Compilers and Translators May 3, 1999 Lecture 34: Compiler-like Systems JIT bytecode interpreter src-to-src translator bytecode.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Virtual Machines, Interpretation Techniques, and Just-In-Time Compilers Kostis Sagonas
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.
Intermediate Code Representations
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate Language  Compiler Model Front-End− language dependant part Back-End− machine dependant part [1/34]
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
RealTimeSystems Lab Jong-Koo, Lim
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
Compiler Design (40-414) Main Text Book:
Computer Architecture Instruction Set Architecture
The Java Virtual Machine (JVM)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS216: Program and Data Representation
Compiler Chapter 9. Intermediate Languages
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
Intermediate Code Generation Part I
Intermediate Representations Hal Perkins Autumn 2011
Java Virtual Machine (JVM)
Intermediate Representations
CSc 453 Interpreters & Interpretation
Intermediate Code Generation Part I
Intermediate Representations
Compiler Design 21. Intermediate Code Generation
Intermediate Representations Hal Perkins Autumn 2005
Intermediate Code Generation Part I
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation Part I
Lecture 4: Instruction Set Design/Pipelining
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generating machine-independent intermediate form.
CSc 453 Interpreters & Interpretation
Presentation transcript:

1 Lecture 10 Intermediate Representations

2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in IR form into an equivalent program that may run more eciently. back end »transforms the code in IR form into native code for the target machine The IR encodes knowledge that the compiler has derived about the source program.

3 Intermediate Representations l Advantages »compiler can make multiple passes over program »break the compiler into manageable pieces »support multiple languages and architectures using multiple front & back ends »enables machine-independent optimization l Desirable properties »easy & inexpensive to generate and manipulate »contains sufficient information l Examples »abstract syntax tree (AST) »directed acyclic graph (DAG) » control flow graph (CFG) » three address code » stack code

4 Classification of IRs l Structural »structural IRs are graphically oriented »examples: trees, directed acyclic graphs (DAGs) »heavily used in source to source translators »nodes, edges tend to be large l Linear »pseudo-code for some abstract machine »large variation in level of abstraction »simple, compact data structures »easier to rearrange

5 Classification of IRs l Hybrids »combination of graphs and linear code »attempt to take best of each »examples: control-flow graph (CFG)

6 Abstract Syntax Tree (AST) l is a parse tree with the nodes for most nonterminals removed. EX: x - 2 * y. l For ease of manipulation, can use a linear (operator) form of the tree. »postfix form: x 2 y * -

7 Directed Acyclic Graph (DAG) is an AST with a unique node for each value. Ex: x  2 * y + sin(2*x) z  x / 2

8 Control Flow Graph (CFG) l models the transfers of control in the procedure. »nodes in the graph are basic blocks (maximal- length straight-line blocks of code ) »edges in the graph represent control flow loops, if-then-else, case, goto etc. l Example »if (x=y) then s1 else s2 »s3

9 Three Address Code (TAC) l are statements of the form: x  y op z with a single operator and, at most, three names. l Complex expressions like x - y * 3 are simplified to t1  y * 3 t2  x - t1 l Advantages »compact form (direct naming) »names for intermediate values

10 Register transfer language ( RTL) l only load/store instructions access memory l all other operands are registers l version of three address code for RISC

11 Typical TAC Statements l Assignments: »x  y op z x  op y x  y »x  y[i] x[i]  y l Branches : goto L l Conditional branches: if x relop y goto L l Procedure calls: »param x call p, n return y

12 TAC »Address and pointer assignments: »load temp var // (temp  var) »store temp var // (var  temp) »load temp temp2 // temp  *temp2 »store temp temp2 // *temp2  temp »mv temp temp2 // temp  temp2 »la temp var // (temp  &var) »loadi temp const; // temp  const

13 Representation of TAC l can use quadruples (op x y z) to represent the TAC: x  y op z. l Ex: x – y * 3 can be represented as : 1. load t1 y // t1  y 2. loadi t2 2 // t2  3 3. mult t3 t1 t2 // t3  t1 * t2 4. load t4 x // t4  x 5. sub t5 t4 t2 // t5  t4 – t2

14 Stack Machine Code l Simplify IR by assuming implicit stack : »(i.e all temps come from a stack) l Example: z = x - 2 * y becomes »push addr(z) // or la z »push x »push 2 »push y »multiply »subtract »store z l cf: postfix form: addr(z) x 2 y * - =

15 Stack Machine Code l Advantages » compact form »introduced names are implicit, not explicit »simple to generate and execute code l Disadvantages »processors operate on registers, not stacks »difficult to reuse values on stack

16 Stack is not enough l Symbol table: »identifiers, procedures »size, type, location »lexical nesting depth l Constant table: »representation, type »storage class, offset(s) l Storage map: »storage layout »overlap information »(virtual) register assignments

17 Virtual Machine l Interpret IR using virtual machine" l Examples »P-code for Pascal »postscript for display devices »Java byte code for Java l Result »easy & portable »much slower l Just-in-time compilation (JIT) »begin interpreting IR »find performance critical section(s); compile section(s) to native code...or just compile entire program »compilation time becomes execution time

18 Java Virtual Machine (JVM) Consist of four parts : l Memory »stack (for function call frames) »heap (for dynamically allocated memory) »constant pool (shared constant data) »code segment (instructions of class files) l Registers »stack pointer (SP), local stack pointer (LSP), »program counter (PC) l Condition codes »stores result of last conditional instruction l Execution unit »1. reads current JVM instruction »2. change state of virtual machine »3. increment PC (modify if call, branch)

19 Java Byte Codes l Arithmetic instructions »ineg [...:i]  [...:-i] »iadd [...:i1,i2]  [...:i1+i2] »isub [...:i1,i2]  [...:i1-i2] »imul [...:i1,i2]  [...:i1*i2] »idiv [...:i1,i2]  [...:i1/i2] l Direct instructions »iinc k a [...]  [...] local[k]  local[k]+a

20 Java Byte Codes (continued) l Constant loading »iconst_0 [...]  [...:0] »iconst_1 [...]  [...:1] »aconst_null [...]  [...:null] »ldc_int i [...]  [...:i] »ldc_string s [...]  [...:str(s)] l Locals [vars + arguments] operations »iload k [...]  [...:local[k]] »aload k[...]  [...:local[k]] »istore k [...:i]  [...] local[k]  i »astore k [...:o]  [...] local[k]  o

21 Java Byte Codes (continued) l Branch instructions »goto L [...]  [...] branch to L »ifeq L [...:i]  [...] branch if i = 0 »ifne L [...:i]  [...] branch if i != 0 »ifnull L [...:o]  [...] branch if o = null »ifnonnull L [...:o]  [...] branch if o != null »if_icmpeq L [...:i1:i2]  [...] branch if i1 = i2 »if_icmpne L [...:i1:i2]  [...] branch if i1 != i2 »if_icmpgt L [...:i1:i2]  [...] branch if i1 > i2 »if_icmplt L [...:i1:i2]  [...] branch if i1 < i2 »if_acmpeq L [...:o1:o2]  [...] branch if o1 = o2 »if_acmpne L [...:o1:o2]  [...] branch if o1 != o2

22 Java Byte Codes (continued) l Stack operations »dup [...:v]  [...:v,v] »pop [...:v]  [...] »swap [...:v1,v2]  [...:v2,v1] l Functions »invoke [...:args]  [...] push stack frame,... »ireturn [...:i]  [...] ret i, pop stack frame »areturn [...:o]  [...] ret o, pop stack frame »return [...]  [...] pop stack frame l Listed are just those relating to our simple language; for more details see JavaByteCode.ppt

23 Java Byte Code Interpreter pc = code.start while (true) { newpc = pc + code[pc].length; switch ( code[pc].opcode) { case iconst_1: push(1); break; case iload: push(local[code[pc+1]]); break; case istore: t  pop(); local[code[pc+1]]  t; break; case iadd: t1  pop(); t2  pop(); push(t1 + t2); break; case ifeq: t  pop(); if (t = 0) newpc = code[pc+1]; break;...} pc  newpc; }