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

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

CS415 Compilers. Intermediate Represeation & Code Generation

Intermediate Representations

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

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

Intermediate Representations

Intermediate Representations & Symbol Tables

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

Intermediate Representations

Compiler Design. Code Shaping. Hwansoo Han

Intermediate Representation (IR)

Alternatives for semantic processing

Intermediate Representations Part II

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

Front End. Hwansoo Han

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

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

CSE 401/M501 Compilers

6. Intermediate Representation

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

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

Intermediate Code Generation

Code Shape II Expressions & Assignment

Intermediate representation

Compilers and computer architecture: A realistic compiler to MIPS

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

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

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

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

Code Generation. Lecture 30

Intermediate Representations

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

6. Intermediate Representation!

Principles of Compiler Design

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

CS 406/534 Compiler Construction Putting It All Together

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

Module 27 Switch-case statements and Run-time storage management

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

Prelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science

Compiler Optimization Intermediate Representation

Chapter 2A Instructions: Language of the Computer

CODE GENERATION Monday, May 31, 2010

Lecture #31: Code Generation

Semantic Analysis. Lecture 9. February 7, 2018

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

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Lecture #19: Code Generation

Intermediate Languages and Machine Languages. Lecture #31: Code Generation. Stack Machine with Accumulator. Stack Machines as Virtual Machines

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

An Overview of Compilation

ECE232: Hardware Organization and Design

Register allocation. TDT4205 Lecture 31

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

CMPSC 160 Translation of Programming Languages. Three-Address Code

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

CS2210: Compiler Construction. Code Generation

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

Intermediate Code Generation

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Intermediate Code Generation

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Programming Languages

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

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

G Programming Languages - Fall 2012

CS 4110 Programming Languages & Logics

Code Generation Super Lectures

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Compilers. Lecture 2 Overview. (original slides by Sam

1 Lexical Considerations

Code Generation. Lecture 12

CSc 453 Intermediate Code Generation

UNIT IV INTERMEDIATE CODE GENERATION

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

Intermediate Representations

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Naming in OOLs and Storage Layout Comp 412

CS 432 Fall Mike Lam, Professor. Code Generation

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

The Procedure Abstraction

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

CSE 401/M501 Compilers

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

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

Digital Forensics Lecture 3 - Reverse Engineering

CMPT 379 Compilers. Parse trees

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Writing Evaluators MIF08. Laure Gonnord

Implementing Control Flow Constructs Comp 412

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Transcription:

Prelude COMP 181 Lecture 14 Intermediate representations and code generation October 19, 2006 Who is Seth Lloyd? Professor of mechanical engineering at MIT, pioneer in quantum computing Article in Nature: Ultimate physical limits to computation Idea: the ultimate laptop Start with 1 kilogram of matter Use speed of light c, quantum scale h, gravity constant G System stats: Speed: 2mc 2 /πh = 5.4 10 50 ops/sec Memory: about 10 31 bits 10 21 GB Bad news: operating temperature is 10 9 Kelvin Tufts University Computer Science 2 Today Intermediate representations and code generation Scanner Parser Intermediate code generation Semantic checker Back end Low-level IR High-level IR Intermediate representations Decisions in IR design affect the speed and capabilities of the compiler Some important IR properties Ease of generation, manipulation, optimization Size of the representation Level of abstraction: level of detail in the IR How close is IR to source code? To the machine? What kinds of operations are represented? Often, different IRs for different jobs Typically: High-level IR: close to the source language Low-level IR: close to the machine assembly code Tufts University Computer Science 3 Tufts University Computer Science 4 Types of IRs Three major categories Structural Eamples: Graph oriented Trees, DAGs Heavily used in source-to-source translators Tend to be large Linear Pseudo-code for an abstract machine Level of abstraction varies Simple, compact data structures Easier to rearrange Hybrid Combination of graphs and linear code Eamples: 3 address code Stack machine code Eample: Control-flow graph Tufts University Computer Science 5 High-level IR High-level language constructs Array accesses, field accesses Comple control flow Loops, conditionals, switch, break, continue Procedures: callers and callees Arithmetic and logic operators Including things like short-circuit && and Often: tree structured Arbitrary nesting of epressions and statements Tufts University Computer Science 6 1

Abstract Synta Tree AST: parse tree with some intermediate nodes removed 2 * y What is this representation good for? We can reconstruct original source Source-to-source translators Program understanding tools Tufts University Computer Science 7 - * 2 y Directed Acyclic Graph A directed acyclic graph (DAG) AST with a unique node for each value z - w / 2 y Why do this? More compact (sharing) Encodes redundancy * Tufts University Computer Science 8 z - 2 * y w / 2 Same epression twice means that the compiler might arrange to evaluate it just once! Low-level IR Linear stream of abstract instructions Instruction: single operation and assignment = y op z y op z op, y, z Must break down high-level contructs: lowering Eample: z = 2 * y t 2 * y z - t Introduce temps as necessary: called virtual registers What are advantages? Resembles many machine instruction formats Introduces a new set of names we ll need these later Fairly compact form Low-level IR Eplicit load and store t1 = *p t1 = load p move t1, [p] *p = t2 store [p] = t2 move [p], t2 Note: we must build addresses using arithmetic Simple control-flow label1: goto label1 if_goto, label1 Jump to label1 if has nonzero value Tufts University Computer Science 9 Tufts University Computer Science 10 Memory model What kind of storage do variables represent? y op z Possibilities: Values on the stack Values in global variables Temporaries introduced during lowering Typically: Temporaries: treated as registers (unlimited #) Other variables: must be eplicitly loaded, stored Tufts University Computer Science 11 Memory model z = 2 * y Memory operations Often added later, by back-end Eplicitly represent variables t1 2 * y z t1 IA32 (Intel 86) %name a register $val a constant %esp stack pointer 4(%esp) indirect offset move 4(%esp), %ea // load y mul $2, %ea move 8(%esp), %ed // load sub %ed, %ea, %eb // t1 is eb move %ea, 12(%esp) // store in z Tufts University Computer Science 12 2

Stack Machine Code Originally for stack-based computers 2 * y What are advantages? push push 2 push y multiply subtract Introduced names are implicit, not eplicit Simple to generate and eecute code Compact form who cares about code size? Embedded systems Systems where code is transmitted (the Net) Now, Java VM Post-fi notation Tufts University Computer Science 13 Control-flow Graph (CFG) Models the transfer of control in the procedure Nodes in the graph are basic blocks Within basic block: some linear, low-level linear form Edges in the graph represent control flow Eample: if ( = y) a 2 b 5 c a * b a 3 b 4 Basic blocks Maimal length sequences of straight-line code Tufts University Computer Science 14 Static Single Assignment Idea: Each variable assigned only once A variable represents a specific value Add on to other forms, mostly CFG = z y = 1 y = 2 = y - 1 0 = z0 y0 1 = 0 1 y1 = 1 2 2 = y1-1 Turns imperative code into functional code Static Single Assignment Problem: what about control flow? a 2 b 5 if ( = y) c a * b a 3 b 4 Φ functions are selectors Works on loops as well a1 2 b1 5 if ( = y) a3 = Φ(a1,a2) b3 = Φ(b1,b2)? c a3 * b3 a2 3 b2 4 Tufts University Computer Science 15 Tufts University Computer Science 16 Static Single Assignment IR Trade-offs Advantages Speeds up some program analysis Functional semantics easier to reason about Breaks up live ranges Disadvantages Makes some optimizations more comple Doesn t work for C why? Pointers to local variables: p = &; *p = 7; for (i=0; i<n; i) A[i] = i; Loop iterations are independent A = [ ] i i for i=0;i<n;i Loop invariant Strength reduce to temp2 = 4 loop: temp1 = &A temp2 = i * 4 temp3 = temp1 temp2 store [temp3] = i... goto loop Tufts University Computer Science 17 Tufts University Computer Science 18 3

The Rest of the Story Representing the code is only part of an IR Other necessary components Symbol table (already discussed) Constant table Representation, type Storage class, offset Storage map Overall storage layout Overlap information Virtual register assignments Overview Kinds of representations Tree or graph Linear Level of abstraction High-level: close to the language Low-level: close to machine Note: can represent low-level IR as a tree In the compiler: Start with high-level IR, move lower Why? Tufts University Computer Science 19 Tufts University Computer Science 20 Towards code generation if (c == 0) { while (c < 20) { c = c 2; } } else c = n * n 2; t1 = c == 0 if_goto t1, lab1 t2 = n * n c = t2 2 goto end lab1: t3 = c >= 20 if_goto t3, end c = c 2 goto lab1 end: Lowering How do we translate from high-level IR to low-level IR? HIR is comple, with nested structures LIR is low-level, with everything eplicit Need a systematic algorithm Idea: Define translation for each AST node, assuming we have code for children Come up with a scheme to stitch them together Recursively descend the AST Tufts University Computer Science 21 Tufts University Computer Science 22 Lowering scheme Define a generate function For each kind of AST node Produces code for that kind of node May call generate for children nodes How to stitch code together? Generate function returns a temporary (or a register) holding the result Emit code that combines the results Tufts University Computer Science 23 Lowering epressions Arithmetic operations epr1 op epr2 epr1 t1 = generate(epr1) t2 = generate(epr2) emit( r = t1 op t2 ) Return the register to generate call above Tufts University Computer Science 24 op epr2 Generate code for left and right children, get the registers holding the results Obtain a fresh register name Emit code for this operation 4

Lowering epressions Short-circuiting Scheme works for: Binary arithmetic Unary operations Logic operations What about && and? In C and Java, they are short-circuiting Need control flow If epr1 is true, don t eval epr2 epr1 epr2 t1 = generate(epr1) emit( r = t1 ) emit( if_goto t1, E ) t2 = generate(epr2) emit( r = t2 ) epr1 epr2 Tufts University Computer Science 25 Tufts University Computer Science 26 Details Helper functions t1 = generate(epr1) emit( r = t1 ) emit( if_goto t1, E ) t2 = generate(epr2) emit( r = t2 ) t1 = epr1 r = t1 if_goto t1, E t2 = epr2 r = t2 E:... t3 = L: ifgoto t1 = emit() The only function that generates instructions Adds instructions to end of buffer At the end, buffer contains code new_label() Generate a unique label name Does not update code new_temp() Generate a unique temporary name May require type information Order of calls to emit is significant! Tufts University Computer Science 27 Tufts University Computer Science 28 Short-circuiting && Short-circuiting && epr1 && epr2 && epr1 epr2 N = new_label() t1 = generate(epr1) emit( r = t1 ) emit( if_goto t1, N ) emit( goto E ) emit( N: ) t2 = generate(epr2) emit( r = t2 ) Can we do better? epr1 && epr2 epr1 && epr2 t1 = generate(epr1) emit( r = t1 ) emit( ifnot_goto t1, E ) t2 = generate(epr2) emit( r = t2 ) Tufts University Computer Science 29 Tufts University Computer Science 30 5

Array access Depends on abstraction OR: epr1 [ epr2 ] Emit array op Lower later a = generate(epr1) o = generate(epr2) emit( o = o * size ) emit( a = a o ) emit( r = load a ) Type information from the symbol table Tufts University Computer Science 31 Statements Simple sequences statement1; statement2;... statementn; Conditionals if (epr) statement; generate(statement1) generate(statement2)... generate(statementn) t = generate(epr) emit( ifnot_goto t, E ) generate(statement) Tufts University Computer Science 32 Loops Function call Emit label for top of loop Generate condition and loop body Different calling conventions Why call generate here? while (epr) statement; T = new_label() emit( T: ) t = generate(epr) emit( ifnot_goto t, E ) generate(statement) emit( goto T ) = f(epr1, epr2,...); a = generate(f) foreach epr-i ti = generate(epri) emit( push ti ) emit( call_jump a ) emit( = get_result ) Tufts University Computer Science 33 Tufts University Computer Science 34 For loop How does for work? for (epr1; epr2; epr3) statement Assignment Problem Difference between right-side and left-side Right-side: a value r-value Left-side: a location l-value Eample: array assignment A[i] = B[j] Store to this location Load from this location Tufts University Computer Science 35 Tufts University Computer Science 36 6

Special generate Define generate for l-values lgenerate returns register contains address Simple case: also applies to variables Assignment Use lgenerate for left-side Return r-value for nested assignment a = generate(epr1) o = generate(epr2) emit( o = o * size ) emit( a = a o ) emit( r = load a ) a = generate(epr1) o = generate(epr2) emit( o = o * size ) emit( a = a o ) return a epr1 = epr2; r = generate(epr2) l = lgenerate(epr2) emit( store *l = r ) r-value case l-value case Tufts University Computer Science 37 Tufts University Computer Science 38 At leaves Depends on level of abstraction generate(v) for variables All virtual registers: return v Strict register machine: emit( r = load v ) Lower level: emit( r = load base offset ) base is stack pointer, offset from symbol table Note: may introduces many temporaries generate(c) for constants May return special object to avoid r = # Tufts University Computer Science 39 Generation: Big picture Reg generate(astnode node) { Reg r; switch (node.getkind()) { case BIN: t1 = generate(node.getleft()); t2 = generate(node.getright()); ; emit( r = t1 op t2 ); break; case NUM: ; emit( r = node.getvalue() ); break; case ID: ; o = symtab.getoffset(node.getid()); emit( r = load sp o ); break; } } Tufts University Computer Science 40 Code Shape Definition All those nebulous properties of the code that impact performance & code quality Includes: Code for different constructs Cost, storage requirements & mapping Choice of operations Code shape is the end product of many decisions Impact Code shape influences algorithm choice & results Code shape can encode important facts, or hide them Tufts University Computer Science 41 Code Shape An eample: y z y z y t1 t1 z t2 z t1 t1 y t2 What if is 2 and z is 3? What if yz is evaluated earlier? y z y z t1 t1 z t2 The best shape for yz depends on contet There may be several conflicting options Tufts University Computer Science 42 z y y z Addition is commutative & associative for integers 7

Code Shape Another eample the switch statement Implement it as a jump table Lookup address in a table & jump to it Uniform (constant) cost Implement it as cascaded if-then-else statements Cost depends on where your case actually occurs O(number of cases) Implement it as a binary search Uniform (log n) cost Compiler must choose best implementation strategy No way to convert one into another Tufts University Computer Science 43 Order of evaluation Ordering for performance Using associativity and commutativity Very hard problem Operands op1 must be preserved while op2 is computed Emit code for more intensive one first Language requirements Sequence points: Places where side effects must be visible to other operations C eamples: f() g() may be eecuted in any order f() g() f must be eecuted first f(i) argument to f must be i1 Tufts University Computer Science 44 Code Generation Tree-walk algorithm Notice: generates code for children first Effectively, a bottom up algorithm So that means. Right! Use synta directed translation Can emit LIR code in productions Pass registers in $$, $1, $2, etc. Tricky part: assignment Tufts University Computer Science 45 One-pass code generation Goal::= Epr:e {: RES = e :} Epr::= Epr:e Term:t {: ; emit( r = e t ); Epr:e Term:t {: ; emit( r = e - t ); Term::= Term:t * Fact:f {: ; emit( r = t * f ); Term:t / Fact:f {: ; emit( r = t - f ); Fact::= ID:i {: ; o = symtab.getoffset(i); emit( r = load sp o ); NUM:n {: ; emit( r = $n ); Tufts University Computer Science 46 Net time Midterm Midterm course evaluations Then: More code generation Conference report Don t forget about PA2 Tufts University Computer Science 47 8