Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

Similar documents
We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR.

Administration. Where we are. Canonical form. Canonical form. One SEQ node. CS 412 Introduction to Compilers

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

7 Translation to Intermediate Code

Where we are. Instruction selection. Abstract Assembly. CS 4120 Introduction to Compilers

Intermediate Code Generation

CPSC 411, Fall 2010 Midterm Examination

Agenda. CSE P 501 Compilers. Big Picture. Compiler Organization. Intermediate Representations. IR for Code Generation. CSE P 501 Au05 N-1

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Principles of Compiler Design

CSE 401/M501 Compilers

Intermediate representation

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

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

Translation to Intermediate Code. Chapter 7

Programming Language Processor Theory

Announcements. Project 2: released due this sunday! Midterm date is now set: check newsgroup / website. Chapter 7: Translation to IR

Intermediate Representations

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

CA Compiler Construction

Topic 7: Intermediate Representations

Time : 1 Hour Max Marks : 30

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilation 2013 Translation to IR

Intermediate Representation (IR)

A Simple Syntax-Directed Translator

A simple syntax-directed

Alternatives for semantic processing

Languages and Compiler Design II IR Code Generation I

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

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

Advanced Topics in MNIT. Lecture 1 (27 Aug 2015) CADSL

CS453 Compiler Construction

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

Intermediate Representations

Lecture Notes on Intermediate Representation

CS 6353 Compiler Construction Project Assignments

Compiler Internals. Reminders. Course infrastructure. Registering for the course

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Lecture Notes on Intermediate Representation

Introduction to Compiler Design

An Overview of Compilation

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

IR trees: Statements. IR trees: Expressions. Translating MiniJava. Kinds of expressions. Local variables: Allocate as a temporary t

Compilers Crash Course

Project Compiler. CS031 TA Help Session November 28, 2011

Undergraduate Compilers in a Day

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

Translation. From ASTs to IR trees

CS 6353 Compiler Construction Project Assignments

Itree Stmts and Exprs. Back-End Code Generation. Summary: IR -> Machine Code. Side-Effects

Administration. Today

Lecture Notes on Intermediate Representation

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

CSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compilers. Compiler Construction Tutorial The Front-end

Compiling Regular Expressions COMP360

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

Compiler Construction

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

COMPILERS BASIC COMPILER FUNCTIONS

Compilers and Code Optimization EDOARDO FUSELLA

Syntax and Grammars 1 / 21

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Compiler Construction 2009/2010: Intermediate Representation

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

5. Semantic Analysis!

Lecture Notes on Intermediate Representation

Code Generation. Lecture 30

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Implementing Algorithms in MIPS Assembly

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

S3.0 : A multicore 32-bit Processor

Compilers. Lecture 2 Overview. (original slides by Sam

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Fundamentals of Compilation

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Summary: Semantic Analysis

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Syntax-Directed Translation. Lecture 14

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

Intermediate Representations

Intermediate Representations

CS 360 Programming Languages Interpreters

Transcription:

Where we are CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 13: Intermediate Code 25 Sep 09 Source code (character stream) Token stream Abstract syntax tree Abstract syntax tree + type objects, symbol tables Lexical analysis Syntactic Analysis Semantic Analysis Intermediate Code Generation regular expressions grammars static semantics Intermediate Code 1 CS 4120 Introduction to Compilers 2 Intermediate Code Abstract machine code - simpler Allows machine-independent code generation, optimization AST Pentium Java bytecode Alpha What makes a good IR? Easy to translate from AST Easy to translate to assembly Narrow interface: small number of node types (instructions) Easy to AST (>40 node types) Easy to retarget IR (13 node types) Pentium (>200 opcodes) CS 4120 Introduction to Compilers 3 CS 4120 Introduction to Compilers 4

Intermediate Code Abstract machine code (Intermediate Representation) Allows machine-independent code generation, optimization Pentium Optimizing compilers Goal: get program closer to machine code without losing information needed to do useful optimizations Need multiple IR stages Pentium (LIR) AST IR Java bytecode AST HIR MIR Java bytecode (LIR) Alpha Itanium (LIR) CS 4120 Introduction to Compilers 5 CS 4120 Introduction to Compilers 6 High-level IR (HIR) AST + new node types not generated by parser Preserves high-level language constructs structured flow, variables, methods Allows high-level optimizations based on properties of source language (e.g. inlining, reuse of constant variables) More passes: ideal for visitors CS 4120 Introduction to Compilers 7 Medium-level IR (MIR) Intermediate between AST and assembly Appel s IR: tree structured IR (triples) Unstructured jumps, registers, memory loc ns Convenient for translation to high-quality machine code Other MIRs: quadruples: a = b OP c ( a is explicit, not arc) UCODE: stack machine based (like Java bytecode) advantage of tree IR: easier instruction selection advantage of quadruples: easier dataflow analysis, optimization advantage of UCODE: slightly easier to generate CS 4120 Introduction to Compilers 8

Low-level IR (LIR) Assembly code + extra pseudoinstructions Machine-dependent Translation to assembly code is trivial Allows optimization of code for low-level considerations: scheduling, memory layout CS 4120 Introduction to Compilers 9 MIR tree Intermediate Representation is a tree of nodes representing abstract machine instructions: can be interpreted IR almost the same as Appel s (except CJUMP) Statement nodes return no value, are executed in a particular order e.g., SEQ, CJUMP Iota statement! IR statement! Expression nodes return a value, children are executed in no particular order e.g. ADD, SUB non-determinism gives flexibility for optimization CS 4120 Introduction to Compilers 10 IR expressions CONST(i) : the integer constant i TEMP(t) : a temporary register t. The abstract machine has an infinite number of registers OP(e 1, e 2 ) : one of the following operations arithmetic: ADD, SUB, MUL, DIV, MOD bit logic: AND, OR, XOR, LSHIFT, RSHIFT, ARSHIFT comparisons: EQ, NEQ, LT, GT, LEQ, GEQ MEM(e) : contents of memory locn w/ address e CALL( f, a 0, a 1, ) : result of fcn f applied to arguments a i NAME(n) : address of the statement or global data location labeled n (TBD) ESEQ(s, e) : result of e after stmt s is executed CS 4120 Introduction to Compilers 11 CONST CONST node represents an integer constant i CONST(i) Value of node is i CS 4120 Introduction to Compilers 12

TEMP TEMP node is one of the infinite number of registers (temporaries) For brevity, FP = TEMP(FP) Used for local variables and temporaries Value of node is the current content of the named register at the time of evaluation TEMP(t) CS 4120 Introduction to Compilers 13 OP Abstract machine supports a variety of different operations OP OP(e 1, e 2 ) e 1 e 2 Evaluates e 1 and e 2 and then applies operation to their results e 1 and e 2 must be expression nodes Any order of evaluation of e 1 and e 2 is allowed CS 4120 Introduction to Compilers 14 MEM MEM node is a memory location MEM(e) MEM e Computes value of e and looks up contents of memory at that address CS 4120 Introduction to Compilers 15 CALL CALL node represents a function call CALL(e f, e 0, e 1, e 2, ) function code address arguments CALL No explicit representation of argument passing, stack frame setup, etc. Value of node is result of call e f e 0 e 1 e 2 CS 4120 Introduction to Compilers 16

NAME Address of memory location named n Two kinds of named locations labeled statements in program (from LABEL statement) global data definitions (not represented in IR) NAME(n) NAME(n) ESEQ Evaluates an expression e after completion of a statement s that might affect result of e Result of node is result of e ESEQ(s, e) s ESEQ e CS 4120 Introduction to Compilers 17 CS 4120 Introduction to Compilers 18 IR statements (dest, e) : move result of e into dest dest = TEMP(t) : assign to temporary t dest = MEM(e) : assign to memory locn e EXP(e) : evaluate e for side-effects, discard result SEQ(s 1,, s n ) : execute each stmt s i in order JUMP(e) : jump to address e CJUMP(e, l 1, l 2 ) : jump to statement named l 1 or l 2 depending on whether e is true or false LABEL(n) : labels a statement (for use in NAME) CS 4120 Introduction to Compilers 19 TEMP(n) n = 0; while (n < 10) ( n = n + 1 ) CONST(0) LABEL(HEAD) CJUMP LT TEMP(n) TEMP(10) Example SEQ((TEMP(n), CONST(0)), LABEL(HEAD), CJUMP(LT(TEMP(n), CONST(10)), BODY, END), LABEL(BODY), (TEMP(n), ADD(TEMP(n), CONST(1))), JUMP(NAME(HEAD)), LABEL(END)) SEQ BODY END LABEL(BODY) JUMP NAME(HEAD) ADD TEMP(n) TEMP(n) LABEL(END) CONST(1) CS 4120 Introduction to Compilers 20

Structure of IR tree Top of tree is a statement Expressions are under some statements Statements under expressions only if there is an ESEQ node S S S S S S S E E E E E ESEQ E E E S E S E E E CS 4120 Introduction to Compilers 21 Executing the IR IR tree is a program representation; can be executed directly by an interpreter Execution is tree traversal (exc. jumps) TEMP(n)CONST(0) LABEL(HEAD) CJUMP LT BODY TEMP(n) TEMP(10) SEQ LABEL(BODY) END JUMP NAME(HEAD) ADD TEMP(n) TEMP(n) LABEL(END) CONST(1) CS 4120 Introduction to Compilers 22 How to translate? How do we translate an AST/High-level IR into this IR representation? Next: syntax-directed translation CS 4120 Introduction to Compilers 23