Handling Assignment Comp 412

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

Arrays and Functions

Code Shape II Expressions & Assignment

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

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

The Processor Memory Hierarchy

Parsing II Top-down parsing. Comp 412

Compiler Design. Code Shaping. Hwansoo Han

Implementing Control Flow Constructs Comp 412

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

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

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

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

Naming in OOLs and Storage Layout Comp 412

Instruction Selection: Preliminaries. Comp 412

The Software Stack: From Assembly Language to Machine Code

CS415 Compilers. Intermediate Represeation & Code Generation

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Intermediate Representations

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

Runtime Support for Algol-Like Languages Comp 412

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

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

Syntax Analysis, III Comp 412

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

Intermediate Code Generation

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

Syntax Analysis, III Comp 412

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

Building a Parser Part III

Intermediate Code Generation

DEMO A Language for Practice Implementation Comp 506, Spring 2018

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

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

CPSC 3740 Programming Languages University of Lethbridge. Data Types

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

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

Optimizer. Defining and preserving the meaning of the program

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

Introduction to Optimization Local Value Numbering

Intermediate Representations

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

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

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

Types. What is a type?

Instruction Selection, II Tree-pattern matching

Combining Optimizations: Sparse Conditional Constant Propagation

Parsing II Top-down parsing. Comp 412

Lecture 12: Compiler Backend

Run-time Environments

Run-time Environments

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

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Introduction to Parsing. Comp 412

Just-In-Time Compilers & Runtime Optimizers

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

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

Intermediate Code Generation (ICG)

CS 406/534 Compiler Construction Putting It All Together

The View from 35,000 Feet

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

What about Object-Oriented Languages?

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

1 Lexical Considerations

Intermediate Representations & Symbol Tables

The So'ware Stack: From Assembly Language to Machine Code

Multi-dimensional Arrays

Chapter 3:: Names, Scopes, and Bindings

The Procedure Abstraction

Instruction Selection: Peephole Matching. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Intermediate Representation Prof. James L. Frankel Harvard University

Program construction in C++ for Scientific Computing

The SPL Programming Language Reference Manual

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

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

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Language Reference Manual

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Chapter 8 :: Composite Types

Motivation for typed languages

Intermediate Code Generation

IR Generation. May 13, Monday, May 13, 13

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

Code Merge. Flow Analysis. bookkeeping

Programming Languages

CS558 Programming Languages

Intermediate Representations

Three-Address Code IR

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

Programming Languages, Summary CSC419; Odelia Schwartz

Review of the C Programming Language

Instruction Scheduling Beyond Basic Blocks Extended Basic Blocks, Superblock Cloning, & Traces, with a quick introduction to Dominators.

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

CS415 Compilers. Lexical Analysis

Compiler Code Generation COMP360

CS 406/534 Compiler Construction Parsing Part I

The PCAT Programming Language Reference Manual

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Data Types. Outline. In Text: Chapter 6. What is a type? Primitives Strings Ordinals Arrays Records Sets Pointers 5-1. Chapter 6: Data Types 2

Transcription:

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 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. Chapter 7 in EaC2e

This material is likely to be on the final exam COMP 412, Fall 2018 1

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 We hide a lot of detail inside EmitLoad(). COMP 412, Fall 2018 2

What is Left in Translation? We have talked about the mechanism of translation. Next, we need to focus on the content, or product, of translation. Expressions More operators in expressions Boolean expressions, relational expressions, string operations Type-related issues in expressions Mixed-type operations and value conversions. Assignment (including addressability) Today Control Flow Code shape and SDT strategies Procedure and Function Calls Functionality: what, precisely, does a procedure call do? Implementation: how does the compiler emit code to do that? COMP 412, Fall 2018 3

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) We will continue to assume that addressability can be handled. Ensuing lectures will fill in the pieces of this somewhat complex story. COMP 412, Fall 2018 4

From Figure 7.7 on p 351 in EaC2e Boolean & Relational Expressions This grammar allows w < x < y < z with left associativity We need to add more operators to the expression grammar. Here is one way to do that. 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 2018 a string reference, a function call, 5

Boolean and Relational Expressions Translation can be pretty simple and formulaic Assign values to true and false Use logical operators and comparison operators Significant optimization is possible, either in translation or afterwards Represent values implicitly with the program counter Optimize evaluation Short-circuit optimization, Boolean simplification, propagate inequalities and inequalities x is zero true (x = 0) false x is nonzero COMP 412, Fall 2018 6

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

Handling Assignment Some languages treat assignment as a distinct statement. Some languages treat assignment as an expression operator. lhs rhs 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 (an rvalue) (an lvalue) Some values go into registers, others go into memory Storage allocation makes that decision, based on available knowledge Lifetime, storage class, knowledge all factor into that decision COMP 412, Fall 2018 8

Handling Assignment Type information factors into the implementation of assignment lhs rhs What if lhs and rhs are distinct types? The programming language must specify the correct behavior. Typical rule is the same as an arithmetic operator Evaluate rhs to its natural type Convert rhs to the type of lhs Alternative: The PL might disallow automatic or implicit conversions Require the programmer to get the type information correct Programmer inserts a cast, which is an explicit conversion In many ways, this alternative is safer than an implicit conversion COMP 412, Fall 2018 9

Handling Assignment Type information factors into the implementation of assignment lhs rhs What if the compiler cannot determine the type of lhs and/or rhs? Must store a runtime type tag with the value Must emit code to compute types alongside operations Must emit code to check compatibility and to perform conversions Wow. That sounds expensive. It is. That is why PLs should have sound & complete type systems Alternative is to allow unsafe programs to execute. (Bad idea ) 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 2018 10

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 and should design the language so all checks are static COMP 412, Fall 2018 11

Handling Assignment The compiler must emit code to compute an address for any lhs that is not kept in a register. Many interesting values cannot be kept in a register Strings, arrays, structures, objects Global and static values Known values that are too large Factor ( Expr ) number Reference All those cases lumped together as Reference Aggregate Value (arrays, structures/records, strings, objects) Compiler needs to know a starting address Compiler needs to compute an internal layout to derive an offset Compiler needs to understand size & type From this information, it emits code to compute the runtime address COMP 412, Fall 2018 12

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 that @A 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 2018 13

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 2018 14

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 2018 15