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

Similar documents
CS 406/534 Compiler Construction Putting It All Together

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

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Working of the Compilers

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

BSCS Fall Mid Term Examination December 2012

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

Compilers and Interpreters

CS606- compiler instruction Solved MCQS From Midterm Papers

Compiling Regular Expressions COMP360

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Important Project Dates

Introduction to Compiler Design

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

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

CST-402(T): Language Processors

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

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

Downloaded from Page 1. LR Parsing

Question Bank. 10CS63:Compiler Design

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Compiler Optimisation

Compilers Crash Course

The Structure of a Syntax-Directed Compiler

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

When do We Run a Compiler?

Compiler Code Generation COMP360

Compiler Design Overview. Compiler Design 1

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

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

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

ECE 468/573 Midterm 1 September 30, 2015

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

CS415 Compilers. Lexical Analysis

2068 (I) Attempt all questions.

VALLIAMMAI ENGINEERING COLLEGE

LECTURE NOTES ON COMPILER DESIGN P a g e 2

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Formal Languages and Compilers Lecture VI: Lexical Analysis

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

CS 132 Compiler Construction

CS5363 Final Review. cs5363 1

CMSC 350: COMPILER DESIGN

CMSC 330: Organization of Programming Languages. Context Free Grammars

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

The Structure of a Syntax-Directed Compiler

INSTITUTE OF AERONAUTICAL ENGINEERING

What do Compilers Produce?

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

List of Figures. About the Authors. Acknowledgments

Undergraduate Compilers in a Day

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Compiling Techniques

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

COMPILER DESIGN. For COMPUTER SCIENCE

LECTURE 3. Compiler Phases

Lecture 7: Deterministic Bottom-Up Parsing

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

ECE 468/573 Midterm 1 October 1, 2014

Lexical Scanning COMP360

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

Lecture 8: Deterministic Bottom-Up Parsing

Intermediate Code & Local Optimizations

CS 314 Principles of Programming Languages. Lecture 3

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

CJT^jL rafting Cm ompiler

Compilers. Bottom-up Parsing. (original slides by Sam

Compiler Construction

ECE251 Midterm practice questions, Fall 2010

Compiler Design Aug 1996

CS 314 Principles of Programming Languages

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

UNIT III & IV. Bottom up parsing

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

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

Name of chapter & details

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

CSE 582 Autumn 2002 Exam 11/26/02

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

CS 4120 Introduction to Compilers

Appendix Set Notation and Concepts

Theory and Compiling COMP360

The Structure of a Compiler

4. An interpreter is a program that

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 2: Lexical Analysis 23 Jan 08

Validating LR(1) parsers

Welcome to CSE131b: Compiler Construction

GUJARAT TECHNOLOGICAL UNIVERSITY

HOLY ANGEL UNIVERSITY COLLEGE OF INFORMATION AND COMMUNICATIONS TECHNOLOGY COMPILER THEORY COURSE SYLLABUS

QUESTIONS RELATED TO UNIT I, II And III

VIVA QUESTIONS WITH ANSWERS

Chapter 3: Lexing and Parsing

LR Parsing - The Items

Front End. Hwansoo Han

Semantic Analysis. Lecture 9. February 7, 2018

Some Thoughts on Grad School. Undergraduate Compilers Review

Transcription:

Outline 5 Fall 2002 Lecture 17: Putting it all together From parsing to code generation Saman Amarasinghe 2 6.035 MIT Fall 1998 How to make the computer understand? Write a program using a programming language Microprocessors talk in assembly language Program written in a Programming Languages Compiler Assembly Language Translation Example (input program) int expr(int n) { int d; d = 4 * n * n * (n + 1) * (n + 1); return d; } Saman Amarasinghe 3 6.035 MIT Fall 1998 Saman Amarasinghe 4 6.035 MIT Fall 1998 Example (Output assembly code) Unoptimized Code Optimized Code lda $30,-32($30) stq $26,0($30) stq $15,8($30) bis $30,$30,$15 bis $16,$16,$1 stl $1,16($15) lds $f1,16($15) sts $f1,24($15) ldl $5,24($15) bis $5,$5,$2 s4addq $2,0,$3 ldl $4,16($15) mull $4,$3,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 ldl $3,16($15) addq $3,1,$4 mull $2,$4,$2 stl $2,20($15) ldl $0,20($15) br $31,$33 $33: bis $15,$15,$30 ldq $26,0($30) ldq $15,8($30) addq $30,32,$30 ret $31,($26),1 s4addq $16,0,$0 mull $16,$0,$0 addq $16,1,$16 mull $0,$16,$0 mull $0,$16,$0 ret $31,($26),1 Program written in a Programming Languages Compiler Assembly Language Translation Saman Amarasinghe 5 6.035 MIT Fall 1998 Saman Amarasinghe 6 6.035 MIT Fall 1998 1

Anatomy of a Computer Program (character stream) Lexical Analyzer (Scanner) Token Stream Syntax Analyzer (Parser) Parse Tree Semantic Analyzer Intermediate Representation Code Optimizer Optimized Intermediate Representation Code Generator Assembly code Lexical Analysis Lexical analyzer create tokens out of a text stream Tokens are defined using regular expressions Saman Amarasinghe 7 6.035 MIT Fall 1998 Saman Amarasinghe 8 6.035 MIT Fall 1998 Examples of Regular Expressions Regular Expression Strings matched a a a b ab a b a b å a* a aa aaa (a å) b ab b num = 0 1 2 3 4 5 6 7 8 9 0 1 2 3 posint = num num* 8 6035 int = (å -) posint -42 1024 real = int (å (. posint)) -12.56 12 1.414... Saman Amarasinghe 9 6.035 MIT Fall 1998 Lexical Analysis Lexical analyzer create tokens out of a text stream Tokens are defined using regular expressions Regular expressions can be mapped to Nondeterministic Finite Automatons (NFA) by simple construction NFA is transformed to a DFA Transformation algorithm Executing a DFA is straightforward Saman Amarasinghe 10 6.035 MIT Fall 1998 Syntax Analysis (parsing) Defining a language using context-free grammars Example: A CFG for expressions fi <op> fi ( ) fi - fi num <op> fi + <op> fi * Saman Amarasinghe 11 6.035 MIT Fall 1998 Saman Amarasinghe 12 6.035 MIT Fall 1998 2

Parse Tree Example Syntax Analysis (parsing) num <op> * num * ( num + num ) ( ) <op> Defining a language using context-free grammars Classification of Grammars Context free unambiguous LR(k) LR(1) LALR(1) SLR(1) LR(0) regular num + num Saman Amarasinghe 13 6.035 MIT Fall 1998 Saman Amarasinghe 14 6.035 MIT Fall 1998 State Stack Symbol Stack LR(k) Parser Engine LR(k) Parser Engine ACTION Goto State ( ) $ X s0 shift to s2 error error goto s1 s1 error error accept s2 shift to shift to error s2 s5 goto s3 s3 error shift to s4 error s4 reduce reduce (2) reduce (2) (2) s5 reduce (3) reduce (3) reduce (3) Current Symbol Parser Action Saman Amarasinghe 15 6.035 MIT Fall 1998 ACTION Goto State ( ) $ X Y s0 shift to s1 reduce (5) reduce (5) goto s5 goto s6 s1 shift to s2 reduce (5) reduce(3 or 5) goto s3 s2 shift to s2 reduce (5) reduce (5) goto s3 s3 error shift to s4 error s4 error reduce (4) reduce (4) s5 error error accept s6 error error reduce (2) s0 <S> fi <X> $ <X> fi Y <X> fi ( <Y> fi ( <Y> ) <Y> fi X s5 <S> fi <X> $ Y ( s1 <X> fi ( <Y> fi ( <Y> ) <Y> fi ( <Y> ) <Y> fi s6 <X> fi <Y> Saman Amarasinghe 16 6.035 MIT Fall 1998 ( Y s2 <Y> fi ( <Y> ) <Y> fi ( <Y> ) <Y> fi Y 31 s3 <Y> fi ( <Y> ) ) s4 <Y> fi ( <Y> ) ( Semantic Analysis Building a symbol table Static Checking Flow-of-control checks Uniqueness checks Type checking Dynamic Checking Array bounds check Null pointer dereference check Translation to Intermediate Format Goal: Remain Largely Machine Independent But Move Closer to Standard Machine Model From high-level IR to a low-level IR Eliminate Structured Flow of Control Convert to a Flat Address Space Saman Amarasinghe 17 6.035 MIT Fall 1998 Saman Amarasinghe 18 6.035 MIT Fall 1998 3

Code Optimizations Generate code as good as hand-crafted by a good assembly programmer Have stable, robust performance Abstract the architecture away from the programmer Exploit architectural strengths Hide architectural weaknesses Code Optimizations Algebraic simplification Common subexpression elimination Copy propagation Constant propagation Dead-code elimination Register allocation Instruction Scheduling Saman Amarasinghe 19 6.035 MIT Fall 1998 Saman Amarasinghe 20 6.035 MIT Fall 1998 Outline 17 Compiler Project! Selected current research You guys build a full-blown compiler from the ground up!!! From espresso or decaf to working code Saman Amarasinghe 21 6.035 MIT Fall 1998 Saman Amarasinghe 22 6.035 MIT Fall 1998 Compiler Derby Who has the fastest compiler in the east??? Get your compiler to generate MIPS assembly and you can participate Test case is the Mandelbrot generator program Week 15 / Day 3 in the morning location TBA refreshments provided Outline 22 Saman Amarasinghe 23 6.035 MIT Fall 1998 Saman Amarasinghe 24 6.035 MIT Fall 1998 4

How will you use 6.035 knowledge? As an informed programmer As a designer of simple languages to aid other programming tasks As an engineer dealing with new computer architectures As a true compiler hacker 1. Informed Programmer Now you know what the compiler is doing don t treat it as a black box don t trust it to do the right thing! Implications performance debugging correctness Saman Amarasinghe 25 6.035 MIT Fall 1998 Saman Amarasinghe 26 6.035 MIT Fall 1998 1. Informed Programmer What did you learned in 6.035? How optimizations work or why they did not work How to read and understand optimized code 2. Language Extensions In many applications and systems, you may need to: implement a simple language handle input define an interface command and control extend a language add new functionality modify semantics help with optimizations 25 Saman Amarasinghe 27 6.035 MIT Fall 1998 Saman Amarasinghe 28 6.035 MIT Fall 1998 2. Language Extensions What you learned in 6.035 define tokens and languages using regular expressions and CFGs use tools such as jlex, lex, javacup, yacc build intermediate representations perform simple transformations on the IR 3. Computer Architectures Many special purpose processors in your cell phone, car engine, watch, etc. etc. Designing new architectures Adapting compiler back-ends for new architectures 29 Saman Amarasinghe 29 6.035 MIT Fall 1998 Saman Amarasinghe 30 6.035 MIT Fall 1998 5

3. Designing New Architectures 3. Designing New Architectures Great advances in VLSI technology What did you learned in 6.035 very fast and small transistors scaling up to billion transistors but, slow and limited wires and I/O Capabilities of a compiler: what is simple and what is hard to do How to think like a compiler writer A computer architecture is a combination of hardware and compiler need to know what a compiler can do and what hardware need to do If compiler can do it don t waste hardware resources. Saman Amarasinghe 31 6.035 MIT Fall 1998 Saman Amarasinghe 32 6.035 MIT Fall 1998 3. Back-end support 3. Back-end support Every new architecture need a new backend What do you learned in 6.035 Instruction scheduling Even if the ISA is the same, different resource constrains How to handle new features Intermediate representations Transforming/optimizing the IR Process of generating assembly from a high-level IR Assembly interface issues (eg: calling conventions) Register allocation issues Code scheduling issues Saman Amarasinghe 33 6.035 MIT Fall 1998 Saman Amarasinghe 34 6.035 MIT Fall 1998 36 36 Theory Theory: Algorithms Implementation Develop general, abstract concepts Prove correctness, optimality etc. Examples parse theory lattices and data-flow abstract interpretation The language ML Saman Amarasinghe 35 6.035 MIT Fall 1998 Saman Amarasinghe 36 6.035 MIT Fall 1998 6

Algorithms: Design a solution to a given problem (Mostly new optimizations) Use many techniques such as graph theory, number theory, etc. May have to limit the scope and find good heuristics Examples partial redundancy elimination register allocation by graph coloring using multi-granular (MMX) operations Saman Amarasinghe 37 6.035 MIT Fall 1998 Implementation: Develop a new compiler Issues of designing a very complex software Putting theory and algorithms into practice Examples A JIT for Java A query optimization engine for SQL A rasterizer for postscript Saman Amarasinghe 38 6.035 MIT Fall 1998 What did you learned in 6.035? Outline 42 Saman Amarasinghe 39 6.035 MIT Fall 1998 Saman Amarasinghe 40 6.035 MIT Fall 1998 Saman Amarasinghe 41 6.035 MIT Fall 1998 Saman Amarasinghe 42 6.035 MIT Fall 1998 7