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

Size: px
Start display at page:

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

Transcription

1 COMP 412 FALL 2017 Generating Code for Assignment Statements back to work Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. Chapters 4, 6 & 7 in EaC2e

2 Review ( from Lec. 18) Example Building an Abstract Syntax Tree 1 Goal Expr \$\$ = \$1; 2 Expr Expr + Term \$\$ = MakeAddNode(\$1,\$3); 3 Expr - Term \$\$ = MakeSubNode(\$1,\$3); 4 Term \$\$ = \$1; 5 Term Term * Factor \$\$ = MakeMulNode(\$1,\$3); 6 Term / Factor \$\$ = MakeDivNode(\$1,\$3); 7 Factor \$\$ = \$1; 8 Factor ( Expr ) \$\$ = \$2; 9 number \$\$ = MakeNumNode(token); 10 ident \$\$ = MakeIdNode(token); Assumptions: constructors for each node stack holds pointers to nodes COMP We call 412, \$\$ Fall = 2017 \$1; a copy rule. 1

3 Review ( from Lec. 18) Example Building an Abstract Syntax Tree Stack Input Action \$ id num * id shift \$ id num * id reduce 10 \$ Factor num * id reduce 7 \$ Term num * id reduce 4 \$ Expr num * id shift \$ Expr num * id shift \$ Expr num * id reduce 9 \$ Expr Factor * id reduce 7 \$ Expr Term * id shift \$ Expr Term * id shift \$ Expr Term * id reduce 10 \$ Expr Term * Factor reduce 5 \$ Expr Term reduce 3 \$ Expr accept AHSDT Works! Built the AST Some reduce actions just copied values; others called constructors. Same tree as earlier slide - id num x 2 * id y COMP 412, Fall

4 Review ( from Lec. 18) Example Emitting ILOC 1 Goal Expr 2 Expr Expr +Term \$\$ = NextRegister(); Emit(add, \$1, \$3, \$\$); 3 Expr - Term \$\$ = NextRegister(); Emit(sub, \$1, \$3, \$\$); 4 Term \$\$ = \$1; 5 Term Term * Factor \$\$ = NextRegister(); Emit(mult, \$1, \$3, \$\$) 6 Term / Factor \$\$ = NextRegister(); Emit(div, \$1, \$3, \$\$); 7 Factor \$\$ = \$1; 8 Factor ( Expr ) \$\$ = \$2; 9 number \$\$ = NextRegister(); Emit(loadI,Value(lexeme),\$\$); 10 ident \$\$ = NextRegister(); EmitLoad(ident,\$\$); Assumptions NextRegister() returns a virtual register name Emit() can format assembly code EmitLoad() handles addressability & gets a value into a register COMP Copy 412, rules Fall on 2017 the same productions as in the last example 3

5 Review ( from Lec. 18) Example Emitting ILOC Stack Input Action \$ id num * id shift \$ id num * id reduce 10 \$ Factor num * id reduce 7 \$ Term num * id reduce 4 \$ Expr num * id shift \$ Expr num * id shift \$ Expr num * id reduce 9 \$ Expr Factor * id reduce 7 \$ Expr Term * id shift \$ Expr Term * id shift \$ Expr Term * id reduce 10 \$ Expr Term * Factor reduce 5 \$ Expr Term reduce 3 \$ Expr accept Emitting ILOC Simple, but clean, code EmitLoad() hides all of the messy details of naming, addressability, and address modes loadai vr1 loadi 2 vr2 loadai vr3 mult vr2, vr3 vr4 sub vr1,vr4 vr5 COMP 412, Fall

6 What s Left in Expressions and Assignments? Assume that we all understand symbol tables & storage mapping We hid a lot of detail in EmitLoad() How does all of that work? What about type information? Every variable & value has a type Operators have a range of types over which they are defined What about more operators? Assignment? Arrays, strings, structures If the language does not have a declarations before executables rule, then the compiler must either: (1) derive its knowledge of variables, types, and locations incrementally; or (2) make two passes over the code (e.g., build an AST and re-traverse it). To accomplish (1), the compiler use a higher level of abstraction in the initial IR to avoid the need for specific addresses (e.g., represent variables with names and expand address computations COMP 412, Fall at 2017 a later stage in compilation.) 5

7 Handling Identifiers Identifiers have some subtlety, which we hid in EmitLoad() The compiler needs to decide which values can legally live in registers A value is ambiguous if the compiler can access it via more than one name Some pointer based values Some call-by-reference parameters Most compilers treat array elements (e.g., A[i,j]) as ambiguous Ambiguous values must live in memory Any unambiguous value is a candidate for a register See pages in EaC2e for discussion of ambiguity (load or store at each access) The compiler needs to assign offsets to each variable kept in RAM Order them by alignment constraints and assign most constrained objects first (e.g., double-word, then word, then half-word, then byte, ) Reduce padding needed to maintain alignment See pages in EaC2e for discussion COMP 412, Fall 2017 of alignment within a data area 6

8 Handling Identifiers Identifiers have some subtlety, which we hid in EmitLoad() For a scalar identifier x at some point p: The compiler needs to know Which declaration of x pertains at p? And what properties does it declare? The various symbol tables resolve this issue After storage assignment x has either A virtual register number, or A base + offset pair to compute an address static int x; int foo( int y ) { int x; x = y * 9; if(x > 1017) x = foo(x) + 17; return x; } x = foo(12) Emitload() can either return the VR or emit code to compute the address into a register, perform the load, and return the resulting VR For aggregates, the address computation is more complex, as we shall see COMP 412, Fall

9 Extending the Grammar Adding other operators +, -,, and are not enough Need to add operators to grammar and generate code for them Defining the syntax Additional productions Additional levels of precedence See, for example, Fig. 7.7 on p. 351 of EaC2e. Code generation follows the scheme for existing operators Evaluate the operands, then perform the operation Complex operations (sin, log 10, 2 x ) might turn into library calls Handle assignment as an operator COMP 412, Fall

10 From Figure 7.7 on p 351 in EaC2e Boolean & Relational Expressions This grammar allows w < x < y < z with left associativity For example, booleans, relationals, and unary operators Boolean Boolean AndTerm AndTerm AndTerm AndTerm RelExpr RelExpr RelExpr RelExpr < Expr RelExpr Expr RelExpr = Expr RelExpr Expr RelExpr Expr RelExpr > Expr Expr Expr Expr + Term Expr - Term Term Term Term Value Term Value Value Value! Factor Factor Factor ( Expr ) number Reference where Reference derives a name, a subscripted name, a structure COMP reference, 412, Fall 2017 a string reference, a function call, 9

11 Mixed-Type Expressions So far, expressions have had one consistent type What if the operands to an operation have different types? Key observation: the language must define the behavior Might simply be an error, detected during semantic elaboration Require the programmer to fix it (insert a cast?) Might require the compiler to insert an implicit conversion Implicit conversions Compiler uses a conversion table to determine type of result Convert arguments to result type & perform operation Most languages have symmetric & rational conversion tables Typical Conversion Table for Addition (note the symmetry) + Integer Real Double Complex Integer Integer Real Double Complex Real Real Real Double Complex Double Double Double Double Complex Complex Complex Complex Complex Complex COMP 412, Fall

12 Assignment as an Operator lhs rhs Strategy Evaluate rhs to a value Evaluate lhs to a location lvalue is a register Þ move rhs into the register lvalue is an address Þ store rhs into the memory location If rvalue & lvalue have different types Evaluate rvalue to its natural type Convert that value to the type of *lvalue (an rvalue) (an lvalue) Unambiguous scalars go into registers Ambiguous scalars or aggregates go into memory Keeping ambiguous values in memory COMP 412, Fall 2017 lets the hardware sort out the addresses. 11

13 Handling Assignment What if the compiler cannot determine the type of the rhs? Issue is a property of the language & the specific program For type-safety, compiler must insert a run-time check Some languages & implementations ignore safety (a truly bad idea) Add a tag field to the data items to hold type information Explicitly check tags at runtime Code for assignment becomes more complex evaluate rhs if type(lhs) ¹ rhs.tag then convert rhs to type(lhs) or throw an exception lhs rhs Choice between conversion & a runtime exception depends on details of language & type system Much more complex than static checking, plus costs occur at runtime rather than compile time COMP 412, Fall

14 Handling Assignment Compile-time type-checking Goal is to eliminate the need for both tags & runtime checks Determine, at compile time, the type of each subexpression Use runtime check only if compiler cannot determine types Optimization strategy If compiler knows the type, move the check to compile-time Unless tags are needed for garbage collection, eliminate them If check is needed, try to overlap it with other computation Can design the language so all checks are static COMP 412, Fall

15 Extending the Schemes More complex cases for IDENTIFIER What about references to aggregate values? (array or structure elements) Need a layout for aggregate Need a formula to find the specified element Will (often) generate runtime arithmetic to compute element address What about function calls in expressions? (future lecture) Generate the calling sequence & load the return value Severely limits compiler s ability to reorder operations What about a reference to a value passed in as a parameter? Many linkages pass the first several values in registers Call-by-value Þ just a local variable with a funny offset Call-by-reference Þ funny offset, extra indirection COMP 412, Fall Parameters are typically stored at a negative offset from the local data area. Code will have a pointer to that data area.

16 Expanding the Expression Grammar The Classic, Left-Recursive, Expression Grammar 1 Goal Expr 2 Expr Expr +Term 3 Expr - Term 4 Term 5 Term Term * Factor 6 Term / Factor 7 Factor 8 Factor ( Expr ) 9 number 10 ident The grammar is great for x - 2 * y Not all programs operate over the domain of ident and number More complex programs require more complex expressions No mention of array references, structure references, or function calls COMP 412, Fall

17 Expanding the Expression Grammar The Classic, Left-Recursive, Expression Grammar 1 Goal Expr 2 Expr Expr +Term 3 Expr - Term 4 Term 5 Term Term * Factor 6 Term / Factor 7 Factor 8 Factor ( Expr ) 9 number 10 ident 11 ident [ Exprs ] 12 ident ( Exprs ) References to aggregates & functions First, the compiler must recognize expressions that refer to arrays, structures, and function calls Second, the compiler must have schemes to generate code to implement those abstractions Array reference Function invocation COMP 412, Fall

18 Scheme to Generate Code For A[i,j]? Compiler must generate the runtime address of the element A[i,j] The compiler needs to know where A begins Tie between compile-time knowledge and runtime behavior We will defer this discussion until we discuss procedure calls assume is the address of A s first element The compiler must have a plan for the internal layout of A Programming language usually dictates array element layout Three common choices Row-major order Column-major order Indirection vectors And a formula for calculating the address of an array element General scheme: compute address, then issue load (rvalue) or store (lvalue) A The Concept 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 COMP 412, Fall

19 Array Layout Row-Major Order Lay out as a sequence of consecutive rows Rightmost subscript varies fastest A[1,1], A[1,2], A[1,3], A[2,1], A[2,2], A[2,3] Storage Layout Stride one access: successive references in the loop are to successive locations in the level one (or L1) data cache. Stride one access maximizes spatial reuse & the effectiveness of hardware prefetch units. A 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 Stride One Access for ( i = 0; i < n; i++) for ( j = 0; j < n; j++) A[i][j] = 0; Declared arrays in C (and most languages) A The Concept 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 COMP 412, Fall

20 Array Layout Column-Major Order Lay out as a sequence of columns Leftmost subscript varies fastest A[1,1], A[2,1], A[1,2], A[2,2], A[1,3], A[2,3] Storage Layout A 1,1 2,1 1,2 2,2 1,3 2,3 1,4 2,4 Stride One Access for ( j = 0; j < n; j++) for ( i =0; i < n; i++) A[i][j] = 0; All arrays in FORTRAN A The Concept 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 COMP 412, Fall

21 Array Layout Indirection Vectors Vector of pointers to pointers to to values Takes much more space, trades indirection for arithmetic Not amenable to analysis Storage Layout A 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 Stride One Access No reference pattern guarantees stride one access in cache, unless rows are contiguous Arrays in Java int **array; in C 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 COMP 412, Fall A The Concept

22 Return to high-school algebra Computing an Address for an Array Element The General Scheme for an Array Reference Compute an address Issue a load Color Code: Invariant Varying A[ i + ( i low ) x sizeof(a[1]) In general: base(a) + ( i low ) x is the base address of A. Depending on how A is may be an offset from the pointer to the procedure s data area (its ARP), an offset from some global label, or an arbitrary address. The first two are compile time constants. low Lower bound on index COMP ARP 412, Activation Fall 2017 Record Pointer (see next lecture) 22

23 Computing an Address for an Array Element A[ i + ( i low ) x sizeof(a[1]) In general: base(a) + ( i low ) x sizeof(a[1]) int A[1:10] Þ low is 1 Make low be 0 for faster access (saves a ) Almost always a power of 2, known at compile-time Þ use a shift for speed Note, for the record, that a naïve vector reference turns into a subtract, a shift, and an addressoffset load (e.g., loadao) three operations before optimization. This sequence is amazingly cheap. low Lower bound on index COMP 412, Fall

24 Assume sizeof(a[1]) is 1, for simplicity. Computing an Address for an Array Element A[ i + ( i low ) x sizeof(elt) In general: base(a) + ( i low ) x sizeof(a[1]) What about A[i 1,i 2 ]? low 1 Lower bound on row index 1 high 1 Upper bound on row index 5 low 2 Lower bound on column index 1 high 2 Upper bound on column index 4 Row-major order, two + (( i 1 low 1 ) x (high 2 low 2 + 1) + i 2 low 2 ) x sizeof(elt) A[4,3] 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 3,1 3,2 3,3 3,4 4,1 4,2 4,3 4,4 5,1 5,2 5,3 5,4 (i-low 1 ) is 3 (high 2 low 2 + 1) is 4 3 x 4 is 12 i 2 low 2 is 2 Combined, the 2 terms take us to the start of A[4,3] Address computation took 3 adds, 3 subtracts, 1 multiply and 1 shift COMP 412, Fall Cheap in comparison to the register-save costs of a function call.

25 Array Layout Row-Major Order Lay out as a sequence of consecutive rows Rightmost subscript varies fastest A[1,1], A[1,2], A[1,3], A[2,1], A[2,2], A[2,3] Storage Layout A 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 Address Polynomial for + (( i 1 low 1 ) x (high 2 low 2 + 1) + i 2 low 2 ) x sizeof(elt) Why does C start arrays indices at zero? To avoid those subtractions COMP This polynomial 412, Fall 2017 follows Horner s rule for evaluation. 25

26 Array Layout Column-Major Order Lay out as a sequence of columns Leftmost subscript varies fastest A[1,1], A[2,1], A[1,2], A[2,2], A[1,3], A[2,3] Storage Layout A 1,1 2,1 1,2 2,2 1,3 2,3 1,4 2,4 Address Polynomial for + (( i 2 low 2 ) x (high 1 low 1 + 1) + i 1 low 1 ) x sizeof(elt) Change in layout order swaps the subscripts COMP This polynomial 412, Fall 2017 follows Horner s rule for evaluation. 26

27 Array Layout Indirection Vectors Vector of pointers to pointers to to values Takes much more space, trades indirection for arithmetic Not amenable to analysis Storage Layout 1,1 1,2 1,3 1,4 A Address Polynomial for A[i,j] 2,1 2,2 2,3 2,4 *(A[i 1 ])[i 2 ] where each of the [i] s is, itself, a 1-d array reference Back when systems supported 1 or 2 cycle indirect load operations, this scheme was efficient. It replaces a multiply & an add with an indirect load. A[i 1 ] *[i 2 ] sub i 1, low => r 1 r 1 => r 2 load r 2 => r 3 sub i 2, low => r 4 add r 3, r 4 => r 5 load r 5 => r 6 Sketch of the code COMP 412, Fall

28 Array Address Calculations In scientific codes, array address calculations are a major cost Each additional dimension adds more arithmetic Efficiency in address calculation is a critical issue for performance A[i+1,j], A[i,j], A[i,j+1] should all have some common terms Horner s rule evaluation hides the common terms Improving these calculations is a matter of algebra (including distributivity!) Improving the efficiency of array address calculations has been a major focus of code optimization (& hardware design) for the last 40 years Generate better code Transform it to fit the context Design memory systems that support common array access patterns Optimize hardware for stride one access Include sophisticated prefetch engines that recognize access patterns COMP 412, Fall

### Handling Assignment Comp 412

COMP 412 FALL 2018 Handling Assignment Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp

### Arrays and Functions

COMP 506 Rice University Spring 2018 Arrays and Functions source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

### Code Shape II Expressions & Assignment

Code Shape II Expressions & Assignment Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make

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

COMP 412 FALL 20167 Computing Inside The Parser Syntax-Directed Translation, II Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all

### Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

COMP 412 FALL 2018 Computing Inside The Parser Syntax-Directed Translation, II Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights

### Compiler Design. Code Shaping. Hwansoo Han

Compiler Design Code Shaping Hwansoo Han Code Shape Definition All those nebulous properties of the code that impact performance & code quality Includes code, approach for different constructs, cost, storage

### (just another operator) Ambiguous scalars or aggregates go into memory

Handling Assignment (just another operator) lhs rhs Strategy Evaluate rhs to a value (an rvalue) Evaluate lhs to a location (an lvalue) > lvalue is a register move rhs > lvalue is an address store rhs

### The Processor Memory Hierarchy

Corrected COMP 506 Rice University Spring 2018 The Processor Memory Hierarchy source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved.

### CS415 Compilers. Intermediate Represeation & Code Generation

CS415 Compilers Intermediate Represeation & Code Generation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review - Types of Intermediate Representations

### Parsing II Top-down parsing. Comp 412

COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

### Instruction Selection: Preliminaries. Comp 412

COMP 412 FALL 2017 Instruction Selection: Preliminaries Comp 412 source code Front End Optimizer Back End target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

### Naming in OOLs and Storage Layout Comp 412

COMP 412 FALL 2018 Naming in OOLs and Storage Layout Comp 412 source IR IR target Front End Optimizer Back End Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in

### Intermediate Representations

Most of the material in this lecture comes from Chapter 5 of EaC2 Intermediate Representations Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP

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

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

### CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation

CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on

### 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

COMP 412 FALL 2017 Code Shape Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at

### The Software Stack: From Assembly Language to Machine Code

COMP 506 Rice University Spring 2018 The Software Stack: From Assembly Language to Machine Code source code IR Front End Optimizer Back End IR target code Somewhere Out Here Copyright 2018, Keith D. Cooper

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

COMP 412 FALL 2017 Procedure and Function Calls, Part II Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students

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

COMP 412 FALL 2017 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

### Implementing Control Flow Constructs Comp 412

COMP 412 FALL 2018 Implementing Control Flow Constructs Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

### Intermediate Representations

Intermediate Representations Where In The Course Are We? Rest of the course: compiler writer needs to choose among alternatives Choices affect the quality of compiled code time to compile There may be

### Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students

### Intermediate Representations

COMP 506 Rice University Spring 2018 Intermediate Representations source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

### Intermediate Code Generation

Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in the

### Computing Inside The Parser Syntax-Directed Translation. Comp 412

COMP 412 FALL 2018 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights

### Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

COMP 412 FALL 2017 Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412 source IR Front End Optimizer Back End IR target Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

### Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412 COMP 412 FALL 2018 source code IR Front End Optimizer Back End IR target

### Syntax Analysis, III Comp 412

COMP 412 FALL 2017 Syntax Analysis, III Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp

### Intermediate Code Generation

Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the

### Runtime Support for Algol-Like Languages Comp 412

COMP 412 FALL 2018 Runtime Support for Algol-Like Languages Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

### Syntax Analysis, III Comp 412

Updated algorithm for removal of indirect left recursion to match EaC3e (3/2018) COMP 412 FALL 2018 Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, III Comp 412 source code

### Instruction Selection, II Tree-pattern matching

Instruction Selection, II Tree-pattern matching Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 4 at Rice University have explicit permission

### Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

### Intermediate Representations & Symbol Tables

Intermediate Representations & Symbol Tables Copyright 2014, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

### Building a Parser Part III

COMP 506 Rice University Spring 2018 Building a Parser Part III With Practical Application To Lab One source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda

### Intermediate Representations Part II

Intermediate Representations Part II Types of Intermediate Representations Three major categories Structural Linear Hybrid Directed Acyclic Graph A directed acyclic graph (DAG) is an AST with a unique

### Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

COMP 412 FALL 2017 Local Optimization: Value Numbering The Desert Island Optimization Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon,

### Lecture 12: Compiler Backend

CS 515 Programming Language and Compilers I Lecture 1: Compiler Backend (The lectures are based on the slides copyrighted by Keith Cooper and Linda Torczon from Rice University.) Zheng (Eddy) Zhang Rutgers

### Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

COMP 412 FALL 2017 Syntax Analysis, VII One more LR(1) example, plus some more stuff Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon,

### Parsing II Top-down parsing. Comp 412

COMP 412 FALL 2017 Parsing II Top-down parsing Comp 412 source code IR Front End OpMmizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

### DEMO A Language for Practice Implementation Comp 506, Spring 2018

DEMO A Language for Practice Implementation Comp 506, Spring 2018 1 Purpose This document describes the Demo programming language. Demo was invented for instructional purposes; it has no real use aside

### 1 Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

### Intermediate Code Generation (ICG)

Intermediate Code Generation (ICG) Transform AST to lower-level intermediate representation Basic Goals: Separation of Concerns Generate efficient code sequences for individual operations Keep it fast

### Introduction to Parsing. Comp 412

COMP 412 FALL 2010 Introduction to Parsing Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make

### Intermediate Representations

Intermediate Representations Intermediate Representations (EaC Chaper 5) Source Code Front End IR Middle End IR Back End Target Code Front end - produces an intermediate representation (IR) Middle end

### Local Register Allocation (critical content for Lab 2) Comp 412

Updated After Tutorial COMP 412 FALL 2018 Local Register Allocation (critical content for Lab 2) Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda

### Optimizer. Defining and preserving the meaning of the program

Where are we? Well understood Engineering Source Code Front End IR Optimizer IR Back End Machine code Errors The latter half of a compiler contains more open problems, more challenges, and more gray areas

### 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

COMP 412 FALL 2018 Code Shape Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at

### CS415 Compilers Register Allocation. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers Register Allocation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review: The Back End IR Instruction Selection IR Register

### CSE 504: Compiler Design. Runtime Environments

Runtime Environments Pradipta De pradipta.de@sunykorea.ac.kr Current Topic Procedure Abstractions Mechanisms to manage procedures and procedure calls from compiler s perspective Runtime Environment Choices

### Syntax Analysis, VI Examples from LR Parsing. Comp 412

Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, VI Examples from LR Parsing Comp 412 COMP 412 FALL 2018 source code IR IR target Front End Optimizer Back End code Copyright

### CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers Syntax Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Limits of Regular Languages Advantages of Regular Expressions

### Lexical Analysis. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Lexical Analysis Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]

### Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Register Allocation Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP at Rice. Copyright 00, Keith D. Cooper & Linda Torczon, all rights reserved.

### Compiler Optimisation

Compiler Optimisation 1 Introductory Lecture Hugh Leather IF 1.18a hleather@inf.ed.ac.uk Institute for Computing Systems Architecture School of Informatics University of Edinburgh 2018 Textbooks Engineering

### CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers Procedure Abstractions These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Where are we? Well understood Engineering Source Code

### Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Runtime Environment The Procedure Abstraction and Separate Compilation Topics we will cover The procedure abstraction and linkage conventions Runtime storage convention Non-local data access (brief) These

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

CSE P 501 Compilers Intermediate Representations Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 G-1 Administrivia Semantics/types/symbol table project due ~2 weeks how goes it? Should be caught up on

### The structure of a compiler

The structure of a compiler O(n) A compiler is a lot of fast stuff followed by hard problems scanning parsing intermediate code generation Polynomial time Code optimization: local dataflow, global dataflow,

### EECS 6083 Intro to Parsing Context Free Grammars

EECS 6083 Intro to Parsing Context Free Grammars Based on slides from text web site: Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. 1 Parsing sequence of tokens parser

### Program construction in C++ for Scientific Computing

1 (26) School of Engineering Sciences Program construction in C++ for Scientific Computing 2 (26) Outline 1 2 3 4 5 6 3 (26) Our Point class is a model for the vector space R 2. In this space, operations

### CS5363 Final Review. cs5363 1

CS5363 Final Review cs5363 1 Programming language implementation Programming languages Tools for describing data and algorithms Instructing machines what to do Communicate between computers and programmers

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

### The ILOC Simulator User Documentation

The ILOC Simulator User Documentation Spring 2015 Semester The ILOC instruction set is taken from the book, Engineering A Compiler, published by the Morgan- Kaufmann imprint of Elsevier [1]. The simulator

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

### CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

### CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CS 91.406/534 Compiler Construction University of Massachusetts Lowell Professor Li Xu Fall 2004 NOTHING: A Language for Practice Implementation 1 Introduction NOTHING is a programming language designed

### CPSC 3740 Programming Languages University of Lethbridge. Data Types

Data Types A data type defines a collection of data values and a set of predefined operations on those values Some languages allow user to define additional types Useful for error detection through type

### Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Runtime Environment Relationship between names and data objects (of target machine) Allocation & de-allocation is managed by run time support package Each execution of a procedure is an activation of the

### 3.5 Practical Issues PRACTICAL ISSUES Error Recovery

3.5 Practical Issues 141 3.5 PRACTICAL ISSUES Even with automatic parser generators, the compiler writer must manage several issues to produce a robust, efficient parser for a real programming language.

### Chapter 3: Operators, Expressions and Type Conversion

101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

### UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. M.Sc. in Computational Science & Engineering

COMP60081 Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE M.Sc. in Computational Science & Engineering Fundamentals of High Performance Execution Wednesday 16 th January 2008 Time: 09:45

### CS 406/534 Compiler Construction Parsing Part I

CS 406/534 Compiler Construction Parsing Part I Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy and Dr.

### Language Reference Manual simplicity

Language Reference Manual simplicity Course: COMS S4115 Professor: Dr. Stephen Edwards TA: Graham Gobieski Date: July 20, 2016 Group members Rui Gu rg2970 Adam Hadar anh2130 Zachary Moffitt znm2104 Suzanna

### The ILOC Simulator User Documentation

The ILOC Simulator User Documentation Comp 506, Spring 2017 The ILOC instruction set is taken from the book, Engineering A Compiler, published by the Morgan- Kaufmann imprint of Elsevier [1]. The simulator

### CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith

### The ILOC Simulator User Documentation

The ILOC Simulator User Documentation COMP 412, Fall 2015 Documentation for Lab 1 The ILOC instruction set is taken from the book, Engineering A Compiler, published by the Elsevier Morgan-Kaufmann [1].

### CSE 401/M501 Compilers

CSE 401/M501 Compilers Intermediate Representations Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 G-1 Agenda Survey of Intermediate Representations Graphical Concrete/Abstract Syntax Trees (ASTs)

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

Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

### Run-time Environments

Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

### Run-time Environments

Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

### CS415 Compilers. Lexical Analysis

CS415 Compilers Lexical Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Lecture 7 1 Announcements First project and second homework

### Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

COMP 412 FALL 2017 Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java) Copyright 2017, Keith D. Cooper & Zoran Budimlić, all rights reserved. Students enrolled in Comp 412 at Rice

SE352b Software Engineering Design Tools W3: Programming Paradigms Feb. 3, 2005 SE352b, ECE,UWO, Hamada Ghenniwa SE352b: Roadmap CASE Tools: Introduction System Programming Tools Programming Paradigms

### CS 2210 Programming Project (Part IV)

CS 2210 Programming Project (Part IV) April 25, 2018 Code Generation This project is intended to give you experience in writing a code generator as well as bring together the various issues of code generation

### Review of the C Programming Language

Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

### Introduction to Optimization Local Value Numbering

COMP 506 Rice University Spring 2018 Introduction to Optimization Local Value Numbering source IR IR target code Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights

### Intermediate Code Generation

Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

### Alternatives for semantic processing

Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

### COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

### Lab 3, Tutorial 1 Comp 412

COMP 412 FALL 2018 Lab 3, Tutorial 1 Comp 412 source code IR IR Front End Optimizer Back End target code Copyright 2018, Keith D. Cooper, Linda Torczon & Zoran Budimlić, all rights reserved. Students enrolled

### Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Course Name: Advanced Java Lecture 2 Topics to be covered Variables Operators Variables -Introduction A variables can be considered as a name given to the location in memory where values are stored. One

### Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers and Arrays CS 201 This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book. Pointers Powerful but difficult to master Used to simulate pass-by-reference

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

Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

### Semantic actions for declarations and expressions

Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate

### MIDTERM EXAMINATION - CS130 - Spring 2003

MIDTERM EXAMINATION - CS130 - Spring 2003 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 120 + 10 extra credit This exam counts for