CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Similar documents
CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

High-level View of a Compiler

The View from 35,000 Feet

UG3 Compiling Techniques Overview of the Course

Compiling Techniques

Overview of a Compiler

Overview of the Course

Overview of the Class

Overview of the Class

Introduction to Compiler

Compilers and Interpreters

Compiling Techniques

Overview of a Compiler

Overview of a Compiler

CS 132 Compiler Construction

Goals for course What is a compiler and briefly how does it work? Review everything you should know from 330 and before

Compiler Construction

Compiler Design. Dr. Chengwei Lei CEECS California State University, Bakersfield

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

Compiler Construction LECTURE # 1

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

CS 352: Compilers: Principles and Practice

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

Front End. Hwansoo Han

Compilers. Lecture 2 Overview. (original slides by Sam

Introduction to Parsing

CS426 Compiler Construction Fall 2006

CS415 Compilers. Lexical Analysis

CSE 401/M501 Compilers

CS 406/534 Compiler Construction Parsing Part I

Compiler Construction 1. Introduction. Oscar Nierstrasz

CS 406/534 Compiler Construction Putting It All Together

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

CSCI 565 Compiler Design and Implementation Spring 2014

CS 432 Fall Mike Lam, Professor. Compilers. Advanced Systems Elective

CSEP 501 Compilers. Overview and Administrivia Hal Perkins Winter /8/ Hal Perkins & UW CSE A-1

Intermediate Representations

Compilers. Computer Science 431

Compiler Design (40-414)

Intermediate Representations

Compiler Construction 1. Introduction. Oscar Nierstrasz

CS 526 Advanced Topics in Compiler Construction. 1 of 12

EECS 6083 Intro to Parsing Context Free Grammars

CS415 Compilers. Intermediate Represeation & Code Generation

Parsing II Top-down parsing. Comp 412

CS Compiler Construction West Virginia fall semester 2014 August 18, 2014 syllabus 1.0

Academic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers

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

PLAGIARISM. Administrivia. Compilers. CS143 11:00-12:15TT B03 Gates. Text. Staff. Instructor. TAs. Office hours, contact info on 143 web site

Introduction to Parsing. Comp 412

Administrivia. Compilers. CS143 10:30-11:50TT Gates B01. Text. Staff. Instructor. TAs. The Purple Dragon Book. Alex Aiken. Aho, Lam, Sethi & Ullman

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS 314 Principles of Programming Languages. Lecture 3

CSE 582 Autumn 2002 Exam 11/26/02

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

CS 4201 Compilers 2014/2015 Handout: Lab 1

Compilers for Modern Architectures Course Syllabus, Spring 2015

Part 5 Program Analysis Principles and Techniques

Compilers and Code Optimization EDOARDO FUSELLA

CA Compiler Construction

Compiler Theory Introduction and Course Outline Sandro Spina Department of Computer Science

Lexical Scanning COMP360

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

ECE573 Introduction to Compilers & Translators

General Course Information. Catalogue Description. Objectives

Compiling Regular Expressions COMP360

CS 432 Fall Mike Lam, Professor. Compilers. Advanced Systems Elective

Formats of Translated Programs

CS/SE 153 Concepts of Compiler Design

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division

What do Compilers Produce?

Principles of Compiler Construction ( )

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

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

CST-402(T): Language Processors

Academic Formalities. CS Language Translators. Compilers A Sangam. What, When and Why of Compilers

CS131: Programming Languages and Compilers. Spring 2017

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

CS 314 Principles of Programming Languages

CS 415 Midterm Exam Spring 2002

Formal Languages and Compilers Lecture I: Introduction to Compilers

Lexical Analysis. Introduction

Instruction Selection: Preliminaries. Comp 412

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

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

Theory and Compiling COMP360

Advanced Compiler Construction

Principles of Compiler Construction ( )

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

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Optimizing Finite Automata

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Introduction to Programming Languages and Compilers. CS164 11:00-12:30 TT 10 Evans. UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164)

Compiler Optimisation

15-411/ Compiler Design

CS606- compiler instruction Solved MCQS From Midterm Papers

Compilers Crash Course

Transcription:

CS415 Compilers Overview of the Course These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Welcome to CS415 - Compilers Topics in the design of programming language translators, including parsing, run-time storage management, error recovery, code generation, and optimization Instructor: Ulrich Kremer (uli@cs.rutgers.edu) My Office Hours: TBD. Teaching Assistants and Grader TA: Jeff Ames, TA: Chen Cong, Grader: Rajanya Dhar Office hours: TBD. Recitations / office hours will start next week Textbooks: Required: Engineering a Compiler by Cooper/Torcson Recommended: New (or old) Dragon Book Web Site: http://www.cs.rutgers.edu/courses/415/classes/spring_2018_kremer/ Project descriptions, handouts, homeworks, lecture slides Sakai: sakai.rutgers.edu and piazza Homework and project questions, grades, homework solutions cs415, spring 18 Lecture 1 2

Basis for Grading Exams Midterm 25% Final 35% Homework 5% Recitation Attendance 5% Projects (tentative!) Register allocation 10% Front End 10% Code generator/optimizer 10% Notice: This grading scheme and projects are tentative and subject to change. cs415, spring 18 Lecture 1 3

Basis for Grading Exams Midterm Final Homework Closed-notes, closed-book final is cumulative Reinforce concepts, provide practice Number of assignments t.b.d. Projects Register allocation Front End Code generator/opt. High ratio of thought to programming single student labs (note academic integrity information) cs415, spring 18 Lecture 1 4

Rough Syllabus Overview 1 Local Register Allocation 13 Instruction Scheduling 12 Scanning 2 Parsing 3 Context Sensitive Analysis 4 Inner Workings of Compiled Code 6, 7 Introduction to Optimization 8 Code Selection 11 More Optimization (time permitting) Advanced topics in language design/compilation: automatic parallelization, GPUs, power & energy cs415, spring 18 Lecture 1 5

Class-taking technique for CS 415 Get an ilab account NOW, if you not have one already. I will use projected material extensively I will moderate my speed, you sometimes need to say STOP You should read the book Not all material will be covered in class Book complements the lectures You are responsible for material from class The tests will cover both lecture and reading I will probably hint at good test questions in class CS 415 is not a programming course Projects are graded on functionality, documentation, and project reports more than style. However, things should be reasonable Use the resources provided to you See me or the TAs in office hours if you have questions Post questions regarding homework and projects on sakai forums Email personal issues to me with subject line starting cs415:" cs415, spring 18 Lecture 1 6

About Textbooks Check out the SERC Library or online sources Consult them for alternate / more detailed views Other recommended textbook is the Dragon Book Aho, Lam, Sethi, Ullman: Compilers - Principles, Techniques, and Tools (2 nd edition) Older version (Dragon book) also fine cs415, spring 18 Lecture 1 7

Compilers What is a compiler? A program that translates an executable program in one language into an executable program in another language A good compiler should improve the program, in some way What is an interpreter? A program that reads an executable program and produces the results of executing that program C is typically compiled, Scheme is typically interpreted Java is compiled to bytecode (code for the Java VM) which is then interpreted Or a hybrid strategy is used Just-in-time compilation Dynamic optimization (hot paths) cs415, spring 18 Lecture 1 8

Why Study Compilation? Compilers are important system software components They are intimately interconnected with architecture, systems, programming methodology, and language design Compilers include many applications of theory to practice Scanning, parsing, static analysis, instruction selection Many practical applications have embedded languages Commands, macros, Many applications have input formats that look like languages, Matlab, Mathematica Writing a compiler exposes practical algorithmic & engineering issues Approximating hard problems; efficiency & scalability No free lunch, i.e., there are multi-dimensional tradeoffs cs415, spring 18 Lecture 1 9

Intrinsic interest Ø Compiler construction involves ideas from many different parts of computer science Artificial intelligence Greedy algorithms Heuristic search techniques Algorithms Theory Systems Architecture Graph algorithms, union-find, dynamic programming DFAs & PDAs, pattern matching, fixed-point algorithms Allocation & naming, synchronization, data locality Pipeline & hierarchy management, instruction set use, parallelism cs415, spring 18 Lecture 1 10

Intrinsic merit Ø Compiler construction poses challenging and interesting problems: Compilers must do a lot but also run fast Compilers have primary responsibility for run-time performance Compilers are responsible for making it acceptable to use the full power of the programming language Computer architects perpetually create new challenges for the compiler by building more complex machines (e.g.: multi-core, GPUs) Compilers must/should hide that complexity from the programmer Success requires mastery of complex interactions cs415, spring 18 Lecture 1 11

Making Languages Usable It was our belief that if FORTRAN, during its first months, were to translate any reasonable scientific source program into an object program only half as fast as its hand-coded counterpart, then acceptance of our system would be in serious danger... I believe that had we failed to produce efficient programs, the widespread use of languages like FORTRAN would have been seriously delayed. John Backus cs415, spring 18 Lecture 1 12

About the instructor My own research Compiling for homogeneous parallel architectures Compiler-directed power and energy management Programming models and languages for dynamic networks of mobile devices (SpatialViews, Sarana) Resource-aware security enforcement (establishing trust) Programming models and languages for Autonomous Underwater Vehicles (AUVs) Programming models for program quality/energy tradeoffs Thus, my interests lie in Interplay between compiler, OS, and architecture Static analysis to discern program behavior Run-time performance analysis Physical measurements (power/energy, performance, memory) Putting Gliders (AUVs) into the water off the coast of New Jersey and make them behave smarter Make smart phone run longer on the same battery charge cs415, spring 18 Lecture 1 13

High-level View of a Compiler Source code Compiler Machine code Errors Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code) Must agree with OS & linker on format for object code Big step up from assembly language use higher level notations cs415, spring 18 Lecture 1 14

Traditional Two-pass Compiler Source code Front End Back End Machine code Errors Implications Use an intermediate representation () Front end maps legal source code into Back end maps into target machine code Extension: multiple front ends & multiple passes (better code) Typically, front end is O(n) or O(n log n), while back end is NPC cs415, spring 18 Lecture 1 15

A Common Fallacy Fortran Front end Back end Target 1 Scheme Java Front end Front end Back end Target 2 Smalltalk Front end Back end Target 3 Can we build n x m compilers with n+m components? Must encode all language specific knowledge in each front end Must encode all features in a single Must encode all target specific knowledge in each back end Limited success in systems with very low-level s cs415, spring 18 Lecture 1 16

The Front End Source code Scanner tokens Parser Errors Responsibilities Recognize legal (& illegal) programs Report errors in a useful way Produce & preliminary storage map Shape the code for the back end Much of front end construction can be automated cs415, spring 18 Lecture 1 17

The Front End Source code Scanner tokens Parser Errors Scanner Maps character stream into words the basic unit of syntax Produces pairs a word & its part of speech x = x + y ; becomes <id,x> = <id,x> + <id,y> ; word @ lexeme, part of speech @ token type In casual speech, we call the pair a token Typical tokens include number, identifier, +,, new, while, if Scanner eliminates white space (including comments) Speed is important cs415, spring 18 Lecture 1 18

The Front End Source code Scanner tokens Parser Parser Recognizes context-free syntax & reports errors Errors Guides context-sensitive ( semantic ) analysis (type checking) Builds for source program Hand-coded parsers are fairly easy to build Most books advocate using automatic parser generators cs415, spring 18 Lecture 1 19

The Front End Context-free syntax is specified with a grammar SheepNoise SheepNoise baa baa This grammar defines the set of noises that a sheep makes under normal circumstances It is written in a variant of Backus Naur Form (BNF) Formally, a grammar G = (S,N,T,P) S is the start symbol N is a set of non-terminal symbols T is a set of terminal symbols or words P is a set of productions or rewrite rules (P : N N ÈT ) cs415, spring 18 Lecture 1 20

The Front End Context-free syntax can be put to better use 1. goal expr 2. expr expr op term 3. term 4. term number 5. id 6. op + 7. - S = goal T = { number, id, +, - } N = { goal, expr, term, op } P = { 1, 2, 3, 4, 5, 6, 7} This grammar defines simple expressions with addition & subtraction over number and id This grammar, like many, falls in a class called context-free grammars, abbreviated CFG cs415, spring 18 Lecture 1 21

The Front End Given a CFG, we can derive sentences by repeated substitution Production Result goal => 1 expr => 2 expr op term => 5 expr op y => 7 expr - y => 2 expr op term - y => 4 expr op 2 - y => 6 expr + 2 - y => 3 term + 2 - y => 5 x + 2 - y To recognize a valid sentence in some CFG, we will need to construct a derivation automatically (forwards or backwards) cs415, spring 18 Lecture 1 22

The Front End A parse can be represented by a tree (parse tree or syntax tree) x + 2 - y goal expr expr op term expr op term - <id,y> term <id,x> + <number,2> This contains a lot of unneeded information. 1. goal expr 2. expr expr op term 3. term 4. term number 5. id 6. op + 7. - cs415, spring 18 Lecture 1 23

The Front End Compilers often use an abstract syntax tree (AST) - <id,x> + <number,2> <id,y> The AST summarizes grammatical structure, without including detail about the derivation This is much more concise ASTs are one kind of intermediate representation () cs415, spring 18 Lecture 1 24

The Back End Instruction Selection Register Allocation Instruction Scheduling Machine code Responsibilities Translate into target machine code Choose instructions to implement each operation Decide which value to keep in registers Ensure conformance with system interfaces Automation has been less successful in the back end Errors cs415, spring 18 Lecture 1 25

The Back End Instruction Selection Register Allocation Instruction Scheduling Machine code Errors Instruction Selection Produce fast, compact code Take advantage of target features such as addressing modes Usually viewed as a pattern matching problem ad hoc methods, pattern matching, dynamic programming This was the problem of the future in 1978 Spurred by transition from PDP-11 to VAX-11 Orthogonality of RISC simplified this problem cs415, spring 18 Lecture 1 26

The Back End Instruction Selection Register Allocation Instruction Scheduling Machine code Errors Register Allocation Have each value in a register when it is used Manage a limited set of resources Select appropriate LOADs & STOREs Optimal allocation is NP-Complete (1 or k registers) Typically, compilers approximate solutions to NP-Complete problems cs415, spring 18 Lecture 1 27

The Back End Instruction Selection Register Allocation Instruction Scheduling Machine code Errors Instruction Scheduling Avoid hardware stalls and interlocks Use all functional units productively Can increase lifetime of variables (changing the allocation) Optimal scheduling is NP-Complete in nearly all cases Heuristic techniques are well developed cs415, spring 18 Lecture 1 28

Traditional Three-pass Compiler Source Code Front End Middle End Back End Machine code Errors Code Improvement (or Optimization) Analyzes and rewrites (or transforms) Primary goal is to reduce running time of the compiled code May also improve space, power dissipation, energy consumption, Must preserve meaning of the code (may include approximations) Measured by values of named variables or produced output Subject of cs515, and cs516, maybe final weeks of cs415 cs415, spring 18 Lecture 1 29

The Optimizer (or Middle End) Opt Opt Opt... Opt 1 2 3 n Errors Modern optimizers are structured as a series of passes Typical Transformations Discover & propagate some constant value Move a computation to a less frequently executed place Specialize some computation based on context Discover a redundant computation & remove it Remove useless or unreachable code Encode an idiom in some particularly efficient form cs415, spring 18 Lecture 1 30

Modern Restructuring Compiler Source Code Front End HL AST Restructurer HL AST Gen Opt + Back End Machine code Typical Restructuring (source-to-source) Transformations: Blocking for memory hierarchy and register reuse Vectorization Parallelization All based on dependence Also full and partial inlining Errors cs415, spring 18 Lecture 1 31

Role of the Run-time System Memory management services Allocate In the heap or in an activation record (stack frame) Deallocate Collect garbage Run-time type checking Error processing (exception handling) Interface to the operating system Input and output Support of parallelism Parallel thread initiation Communication and synchronization cs415, spring 18 Lecture 1 32

Next class Register Allocation Read EaC: Chapters 13.1 13.3 No recitations or office hours this week cs415, spring 18 Lecture 1 33