Compiler Code Generation COMP360

Similar documents
Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

Compiling Regular Expressions COMP360

Lexical Scanning COMP360

Theory and Compiling COMP360

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

A programming language requires two major definitions A simple one pass compiler

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A simple syntax-directed

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

A Simple Syntax-Directed Translator

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Snobol. Snowflake. and COMP360

Building Compilers with Phoenix

Compiler Design (40-414)

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Syntax-Directed Translation. Lecture 14

CS 415 Midterm Exam Spring SOLUTION

CS2383 Programming Assignment 3

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Class Information ANNOUCEMENTS

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

Compiler Optimization

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

RAID 0 (non-redundant) RAID Types 4/25/2011

Question Bank. 10CS63:Compiler Design

List of Figures. About the Authors. Acknowledgments

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Compiler Construction Using

Specifying Syntax COMP360

VIVA QUESTIONS WITH ANSWERS

Compilers and Interpreters

CSE 401 Midterm Exam 11/5/10

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Compiler Optimisation

Syntax and Grammars 1 / 21

Introduction to Compiler

Intermediate Representations

2068 (I) Attempt all questions.

CST-402(T): Language Processors

Formal Languages and Compilers Lecture I: Introduction to Compilers

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Introduction to Compiler Design

CSCI312 Principles of Programming Languages!

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Introduction to Compilers

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

Principles of Programming Languages COMP251: Syntax and Grammars

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Introduction to Compiler Construction

Tour of common optimizations

COMPILERS BASIC COMPILER FUNCTIONS

Computer Architecture COMP360

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Compilers. Compiler Construction Tutorial The Front-end

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Compiler Design Aug 1996

IR Optimization. May 15th, Tuesday, May 14, 13

Review. Pat Morin COMP 3002

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

TDDD55 - Compilers and Interpreters Lesson 3

Crafting a Compiler with C (II) Compiler V. S. Interpreter

CS5363 Final Review. cs5363 1

CS606- compiler instruction Solved MCQS From Midterm Papers

Compiler Construction

CS153: Compilers Lecture 15: Local Optimization

SOFTWARE ARCHITECTURE 5. COMPILER

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

Introduction to Compiler Construction

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

LECTURE NOTES ON COMPILER DESIGN P a g e 2

More Code Generation and Optimization. Pat Morin COMP 3002

Syntax. In Text: Chapter 3

Introduction to Compiler Construction

PRINCIPLES OF COMPILER DESIGN

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

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Appendix A The DL Language

VALLIAMMAI ENGINEERING COLLEGE

UNIVERSITY OF CALIFORNIA

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

COP 3402 Systems Software Syntax Analysis (Parser)

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Question 1. Notes on the Exam. Today. Comp 104: Operating Systems Concepts 11/05/2015. Revision Lectures

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

The View from 35,000 Feet

COMPILER CONSTRUCTION Seminar 03 TDDB

Transcription:

Compiler Code Generation COMP360

Students who acquire large debts putting themselves through school are unlikely to think about changing society. When you trap people in a system of debt, they can t afford the time to think. Noam Chomsky

Snowflake Parser A recursive descent parser for the Snowflake language is due by noon on Friday, February 17, 2017 This parser should work with your Snowflake scanner The parser must determine if the input Snowflake program has proper syntax An appropriate error message (showing line and column) must be displayed if there is a syntax error

Exam 1 The first COMP360 exam will be on Friday, February 17, 2017 You are allowed to have one 8½ by 11 page of notes

Stages of a Compiler Source preprocessing Lexical Analysis (scanning) Syntactic Analysis (parsing) Semantic Analysis Optimization Code Generation Link to libraries

Lexical Analysis Lexical Analysis or scanning reads the source code (or expanded source code) It removes all comments and white space The output of the scanner is a stream of tokens Tokens can be words, symbols or character strings A scanner can be a finite state automata

Parsing Syntactic Analysis or parsing reads the stream of tokens created by the scanner It checks that the language syntax is correct The output of the Syntactic Analyzer is a parse tree The parser can be implemented by a context free grammar stack machine

Semantic Analysis The Semantic Analysis inputs the parse tree from the parser Semantic Analysis checks that the operations are valid for the given operands (i.e. cannot divide a String) This stage determines what the program is to do The output of the Semantic Analysis is an intermediate code. This is similar to assembler language, but may include higher level operations

Creating a Parse Tree The goal of the parser is to create a parse tree whose leaves are the tokens from the lexical scanner when traversed left to right In addition to proving that a tree does or does not exist, the parser should actually build the tree The semantic analyzer will use the tree to create executable code

Parsing statement variable = expression ; variable string of characters expression variable + expression variable

Semantic Analysis Semantic analysis will check Intermediate code defines a series of simple steps that will execute the program Operation First Operand Second Operand Destination add Boy Cat temp1 add temp1 Dog temp2 copy temp2 A

Simple Machine Language Load register with B Add C to register Store register in Temp1 Load register with Temp1 Add D to register Store register in Temp2 Load register with Temp2 Store register in A

Optimized Machine Language Load register with B Add C to register Store register in Temp1 Load register with Temp1 Add D to register Store register in Temp2 Load register with Temp2 Store register in A

Action Rules Action rules are applied to the BNF productions to indicate what action needs to be taken to execute that portion of the program Action rules can either copy a value from a lower production in the tree or compute a function on the lower tree values Action rules can only involve lower items on the tree

Example Grammar with Actions 1. expr term expr = term 2. expr term + expr 2 expr = sum( term, expr 2 ) 3. expr term - expr 2 expr = diff( term, expr 2 ) 4. term factor term = factor 5. term factor * factor 2 term = mult(factor,factor 2 ) 6. term factor / factor 2 term = div(factor,factor 2 ) 7. factor variable factor = variable 8. factor ( expr ) factor = expr

dog * ( cat + cow ) Annotated Parse Tree Example

Postfix Traversal Traversing the parse tree in a postfix order, taking the specified actions, will correctly execute the program After the parser has built a parse tree, the semantic analyzer can traverse the tree in postfix order Executable code can be generated in the order found by a postfix traversal

Data Structures Review Assume we have nodes that have a left operand, operator and right operand Some nodes may only have one operand and no operator (copy action) An infix traversal visits the left node, the operation and then the right node A prefix traversal visits the operation, the left node and then the right node A postfix traversal visits the left node, the right node and then the operation

Example Tree

Postfix Traversal Example dog * ( cat + cow ) Get dog Get cat Get cow temp1 = cat + cow temp2 = dog * temp1 copies omitted

Loop Invariants Calculations inside a loop that do not change in the loop, can be moved before the loop. This can make the program much more efficient if the calculation moved out of the loop require a lot of CPU time for (int i=0; i<n; i++) { x = y+z; a[i] = 6*i + x*x; } x = y+z; temp = x*x; for (int i=0; i<n; i++) { a[i] = 6*i + temp; } from Wikipedia

Peephole Optimization Peephole optimization is a kind of optimization performed over a very small set of instructions in a segment of generated code This contrasts with loop optimizations that have to loop at the entire loop

Common peephole optimization Constant folding Evaluate constant subexpressions in advance Strength reduction Replace slow operations with faster equivalents Null sequences Delete useless operations Combine operations Replace several operations with one equivalent Algebraic laws Use algebraic laws to simplify or reorder instructions from Wikipedia

Constant Simplification A statement may include arithmetic with constants that can be computed at compile time volume = 4.0 / 3.0 * 3.14159 * r * r; Can be simplified to volume = 4.18879 * r * r;

Constant Propagation Constants can be carried to the next statements int x = 14; int y = 7 - x / 2; return y * (28 / x + 2); Propagating x yields: int x = 14; int y = 7-14 / 2; return y * (28 / 14 + 2); Further simplifying to return 0; from Wikipedia

Strength Reduction strength reduction is where expensive operations are replaced with equivalent but less expensive operations There are multiple ways to divide by 8 divide R3, 8 shiftright R3, 3 // if integer multiply R3, 0.125 // if double

Assembly Line Pipelining is like an assembly line. Each stage of the execution cycle performs its function and passes the instruction to the next cycle Each stage can be working on a different instruction at the same time Several instructions can be in the process of execution simultaneously There is no reason to keep the ALU idle when you are fetching or storing an operand

Pipelining

Hazards A hazard is a situation that reduces the processors ability to pipeline instructions Resource When different instructions want to use the same CPU resource Data When the data used in an instruction is modified by the previous instruction Control When a jump is taken or anything changes the sequential flow

Avoiding Data Hazards The compiler can generate code that avoids data hazards Interleaving different parts of an expression or the program can space access to data values to avoid data hazards Using multiple registers instead of always the same register helps reduce data hazards

Pipeline Efficient Code Inefficient Load R1, W Add R1, 7 Store R1, W Load R1, X Add R1, 8 Store R1, X Load R1, Y Add R1, 9 Store R1, Y Load R1, Z Add R1, 10 Store R1, X Pipeline Efficient (4 stage pipeline) Load R1, W Load R2, X Load R3, Y Load R4, Z Add R1, 7 Add R2, 8 Add R3, 9 Add R4, 10 Store R1, W Store R2, X Store R3, Y Store R4, Z

Register Allocation Keeping values in the registers instead of memory makes the program run much faster Many computers have 16 or 32 general purpose registers allowing several values to be kept in a register Finding the optimal register allocation is equivalent to the NP-Complete graph coloring problem

Memory Hierarchy

Exam 1 The first COMP360 exam will be on Friday, February 17, 2017 You are allowed to have one 8½ by 11 page of notes