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

Similar documents
Compilers Crash Course

Compiler Construction LECTURE # 1

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

Outline CS4120/4121. Compilation in a Nutshell 1. Administration. Introduction to Compilers Andrew Myers. HW1 out later today due next Monday.

CA Compiler Construction

Lecture 1: Introduction

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

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

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

General Course Information. Catalogue Description. Objectives

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

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

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

CSE 504: Compiler Design

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

ECE573 Introduction to Compilers & Translators

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

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Compilers. Pierre Geurts

You may work with a partner on this quiz; both of you must submit your answers.

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

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

CSE 401/M501 Compilers

Compiler Design (40-414)

CPSC 411: Introduction to Compiler Construction

CS 314 Principles of Programming Languages

CSE 582 Autumn 2002 Exam 11/26/02

CS426 Compiler Construction Fall 2006

Formal Languages and Compilers Lecture I: Introduction to Compilers

Compilers and Interpreters

Philadelphia University Faculty of Information Technology Department of Computer Science --- Semester, 2007/2008. Course Syllabus

Compiling Regular Expressions COMP360

Introduction to Compilers

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

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

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

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

15-411/ Compiler Design

CSCI 565 Compiler Design and Implementation Spring 2014

Compilers for Modern Architectures Course Syllabus, Spring 2015

Lecture 1 CIS 341: COMPILERS

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

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Overview. Overview. Programming problems are easier to solve in high-level languages

Evaluation Scheme L T P Total Credit Theory Mid Sem Exam

Who are we? Andre Platzer Out of town the first week GHC TAs Alex Crichton, senior in CS and ECE Ian Gillis, senior in CS

CS 536. Class Meets. Introduction to Programming Languages and Compilers. Instructor. Key Dates. Teaching Assistant. Charles N. Fischer.

Machine Programming 1: Introduction

Compilers. Introduction. Yannis Smaragdakis, U. Athens (original slides by Sam

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

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

CS 415 Midterm Exam Spring 2002

Introduction to Compiler Construction

Compiler Construction D7011E

Compiler I (dt. Übersetzer I) Prof. Dr. Uwe Kastens Winter 2001/2002

2001 bei Prof. Dr. Uwe Kastens

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

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

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

Course introduction. Advanced Compiler Construction Michel Schinz

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN

Compiler construction

SOFTWARE ARCHITECTURE 5. COMPILER

W4118: PC Hardware and x86. Junfeng Yang

Introduction to Compiler Construction

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

Software Lesson 2 Outline

Finding People and Information (1) G53CMP: Lecture 1. Aims and Motivation (1) Finding People and Information (2)

CS 415 Midterm Exam Spring SOLUTION

Compiler Construction

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

INF5110 Compiler Construction

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Compiler construction in4303 lecture 9

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY

Introduction to Compiler Design

Overview of Compiler. A. Introduction

CS202 Compiler Construction. Christian Skalka. Course prerequisites. Solid programming skills a must.

Translator Design CRN Course Administration CMSC 4173 Spring 2017

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Compilers. Computer Science 431

x86 assembly CS449 Fall 2017

Chapter 2 A Quick Tour

Undergraduate Compilers in a Day

Practical Malware Analysis

Languages and Automata

COMP 210 Example Question Exam 2 (Solutions at the bottom)

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTER SCIENCE AND ENGINEERING COURSE PLAN

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

CS131: Programming Languages and Compilers. Spring 2017

Important Project Dates

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

CST-402(T): Language Processors

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

Transcription:

CS 4120 / 4121 CS 5120/5121 Introduction to Compilers Fall 2011 Andrew Myers Lecture 1: Overview Outline About this course Introduction to compilers What are compilers? Why should we learn about them? Anatomy of a compiler Introduction to lexical analysis Text stream to tokens 2 Course Information MWF 1:25-2:15PM in Hollister 306 Instructor: Andrew Myers Teaching Assistants: Wenzel Jakob, Gregor Stocks E-mail: cs4120-l@cs.cornell.edu Web page: http://www.cs.cornell.edu/courses/cs4120 Newsgroup: cornell.class.cs4120 4 = 5 & 0 = 1 CS 4120 and 5120 are really the same course same lectures same assignments or nearly so 5120 is for MEng students, 4120 for others CS 4121 (5121) is required! most coursework is in the project 3 4

Textbooks Work Lecture notes provided; no required textbook On reserve in Uris Library: Compilers Principles, Techniques and Tools. Aho, Lam, Sethi and Ullman (The Dragon Book) (strength: parsing) Modern Compiler Implementation in Java. Andrew Appel. (strength: translation) Advanced Compiler Design and Implementation. Steve Muchnick. (strength: optimization) Homeworks: 4, 20% total 5/5/5/5 Programming Assignments: 7, 50% 5-10% each Exams: 2 prelims, 30% 15/15 No final exam 5 6 Academic integrity Homeworks Taken seriously. Do your own (or your group s) work. Report who you discussed homework with (whether student in class or not). Three assignments in first half of course; one homework in second half Not done in groups you may discuss with others but do your own work Report who you discussed homework with 7 8

Projects Seven programming assignments Implementation language: Java talk to us if your group wants to use something else (e.g., OCaml) Groups of 3-4 students same group for entire class (ordinarily) same grade for all (ordinarily) workload and success in this class depend on working and planning well with your group. Be a good citizen. tell us early if you are having problems. End of this class: some time to form groups create your group on CMS for PA1. contact us if you are having trouble finding a group. Assignments Due at midnight on due date Late homeworks, programming assignments increasingly penalized 1 day: 5%, 2 days: 15%, 3 days: 30%, 4 days: 50% weekend = 1 day Extensions often granted, but must be approved 2 days in advance Projects submitted via CMS Solutions available via CMS 9 10 Why take this course? What are Compilers? CS 4120 is an elective course Expect to learn: practical applications of theory, algorithms, data structures parsing deeper understanding of what code is how high-level languages are implemented a little programming language semantics Intel x86 architecture, Java how programs really execute on computers how to be a better programmer (esp. in groups) Translators from one representation of program code to another Typically: high-level source code to machine language (object code) Not always: Java compiler: Java to interpretable bytecodes Java JIT: bytecode to executable image 11 12

Source Code : optimized for human readability expressive: matches human notions of grammar redundant to help avoid programming errors computation possibly not fully determined by code int expr(int n) { int d; d = 4 * n * n * (n + 1) * (n + 1); return d; } 13 Machine code Optimized for hardware expr: Redundancy, ambiguity reduced Information about intent and reasoning lost Assembly code! machine code pushl %ebp 55 %esp, %ebp 89 e5 subl $4, %esp 83 ec 04 8(%ebp), %eax 8b 45 08 %eax, %edx 89 c2 8(%ebp), %edx 0f af 55 08 8(%ebp), %eax 8b 45 08 incl %eax 40 %eax, %edx 0f af d0 8(%ebp), %eax 8b 45 08 incl %eax 40 0f af c2 sall $2, %eax c1 e0 02 %eax, -4(%ebp) 89 45 fc -4(%ebp), %eax 8b 45 fc leave c9 ret c3 14 Example (Output assembly code) Unoptimized Code expr: pushl subl incl incl sall leave ret %ebp %esp, %ebp $4, %esp 8(%ebp), %eax %eax, %edx 8(%ebp), %edx 8(%ebp), %eax %eax %eax, %edx 8(%ebp), %eax %eax $2, %eax %eax, -4(%ebp) -4(%ebp), %eax Optimized Code expr: pushl incl sall leave ret %ebp %esp, %ebp 8(%ebp), %edx %edx $2, %eax How to translate? and machine code mismatch Goals: source-level expressiveness for task best performance for concrete computation reasonable translation efficiency (< O(n 3 )) maintainable compiler code 15 16

How to translate correctly? How to translate effectively? Programming languages describe computation precisely Therefore: translation can be precisely described (a compiler can be correct) Correctness is very important! hard to debug programs with broken compiler non-trivial: programming languages are expressive implications for development cost, security this course: techniques for building correct compilers some compilers have been proven correct! [X. Leroy, Formal Certification of a Compiler Back End, POPL '06] High-level source code? Low-level machine code 17 18 Idea: translate in steps Compilation in a Nutshell 1 Compiler uses a series of different program representations. Intermediate representations that are good for program manipulations of various kinds (analysis, optimization, code generation). (character stream) Token stream Abstract syntax tree (AST) if (b == 0) a = b; if ( b == 0 ) a = b ; == b 0 if boolean == Decorated AST int b int 0 if a = b int = ; int a int b lvalue ; Lexical analysis Parsing Semantic Analysis 19 20

Compilation in a Nutshell 2 Simplified Compiler Structure boolean == if int b int 0 int = int a int b lvalue if b == 0 goto L1 else L2 L1: a = b L2: cmp r b, 0 jnz L2 L1: mov r a, r b L2: ; Intermediate Code Generation Optimization, Code Generation Register allocation, optimization cmp ecx, 0 cmovz [ebp+8],ecx 21 (character stream) if (b == 0) a = b; Program analysis & Optimization Assembly code cmp 0, %rcx cmovz %rcx, %rdx Token stream Lexical analysis Parsing Abstract syntax tree Intermediate Code Generation Intermediate code Control flow graphs Code generation Front end (machine-independent) Back end (machine-dependent) 22 Even bigger picture Schedule Assembly code Fully-resolved object code (machine code + symbol tables, relocation info) Compiler Assembler Linker Loader Executable image in memory Object code (machine code + symbol tables) 23 Detailed schedule on web page, with links Lexical analysis and parsing: 6 Semantic analysis: 5 Intermediate code: 4 Prelim #1 Code generation: 3 Separate compilation and objects: 4 Optimization: 8 Prelim #2 Run-time, link-time support: 2 Advanced topics: 7 24

First step: Lexical Analysis (character stream) Assembly code Lexical analysis Token stream Abstract syntax tree Parsing Intermediate Code Generation Intermediate code Code generation 25 What is Lexical Analysis? Aka tokenizing, scanning, lexing Converts character stream to token stream of pairs!token type, attribute" if (x1 * x2<1.0) { } y = x1; i f ( x 1 * x 2 < 1. 0 ) { \n if ( Id: x1 * Id: x2 < Num: 1.0 ) { Id: y 26 Token stream Gets rid of whitespace, comments Only! Token type, attribute ":! Id, x ",! Float, 1.0e0 " Token location preserved for debugging, run-time/ compile-time error messages (source file, line number, character posn...)! Id, x, Main.java, 542" Issues: how to specify tokens how to implement tokenizer/lexer 27