Lecture 1: Introduction

Similar documents
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

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

Compilers Crash Course

Machine Program: Procedure. Zhaoguo Wang

Credits and Disclaimers

CSCI 565 Compiler Design and Implementation Spring 2014

Compiler Construction LECTURE # 1

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

How Software Executes

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

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

Setting & Examining Condition Codes. int gt(long x, long y) { return x > y; } int has_nonzero_masked(long x, long mask) { return!!

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

Generation. representation to the machine

Function Calls and Stack

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

Compiler Construction D7011E

How Software Executes

CA Compiler Construction

Machine Programming 3: Procedures

How Software Executes

The Hardware/Software Interface CSE351 Spring 2013

Compilers for Modern Architectures Course Syllabus, Spring 2015

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

The Hardware/Software Interface CSE351 Spring 2015

void twiddle1(int *xp, int *yp) { void twiddle2(int *xp, int *yp) {

CSE351 Spring 2018, Midterm Exam April 27, 2018

Do not turn the page until 5:10.

CS241 Computer Organization Spring 2015 IA

6.1. CS356 Unit 6. x86 Procedures Basic Stack Frames

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

CSE 401/M501 Compilers

Compiler Construction D7011E

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

understanding recursive data types, recursive functions to compute over them, and structural induction to prove things about them

Credits and Disclaimers

Assembly I: Basic Operations. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Machine Programming 1: Introduction

Machine/Assembler Language Putting It All Together

Credits and Disclaimers

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

Data Representa/ons: IA32 + x86-64

CS558 Programming Languages

Second Part of the Course

Machine Level Programming II: Arithmetic &Control

CS241 Computer Organization Spring Loops & Arrays

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Process Layout and Function Calls

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

Compilers and computer architecture: introduction

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

University of Washington

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

Compiler Internals. Reminders. Course infrastructure. Registering for the course

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS153: Compilers Lecture 2: Assembly

CPS104 Recitation: Assembly Programming

Sungkyunkwan University

CS 33. Architecture and Optimization (2) CS33 Intro to Computer Systems XV 1 Copyright 2016 Thomas W. Doeppner. All rights reserved.

Credits to Randy Bryant & Dave O Hallaron

CS356: Discussion #15 Review for Final Exam. Marco Paolieri Illustrations from CS:APP3e textbook

The von Neumann Machine

Program Optimization. Today. DAC 2001 Tutorial. Overview Generally Useful Optimizations. Optimization Blockers

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies) When : TODAY!! 5-6 pm Where : 3rd Floor Atrium, CIT

CS 261 Fall Mike Lam, Professor. x86-64 Control Flow

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

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

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

General Course Information. Catalogue Description. Objectives

Compiler Construction 2010 (6 credits)

Controlling Program Flow

Machine- Level Programming II: Arithme6c & Control

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

Many of the following slides are taken with permission from. The book is used explicitly in CS 2505 and CS 3214 and as a reference in CS 2506.

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Credits and Disclaimers

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Credits and Disclaimers

CS 33. Architecture and Optimization (1) CS33 Intro to Computer Systems XV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Machine-Level Programming II: Control and Arithmetic

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

The von Neumann Machine

x86 Programming I CSE 351 Winter

Princeton University Computer Science 217: Introduction to Programming Systems. Context of this Lecture. Assembly Language: Part 1

Systems Programming and Computer Architecture ( )

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

Instruction Set Architectures

The Hardware/Software Interface CSE351 Spring 2015

CS 33. Architecture and Optimization (2) CS33 Intro to Computer Systems XVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Von Neumann Architecture. Machine Languages. Context of this Lecture. High-Level Languages. Assembly Language: Part 1. Princeton University.

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

CS 314 Principles of Programming Languages

Principles. Performance Tuning. Examples. Amdahl s Law: Only Bottlenecks Matter. Original Enhanced = Speedup. Original Enhanced.

Introduction to Compilers

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

Transcription:

Lecture 1: Introduction

Staff Lecturer Prof. Michael Carbin mcarbin@mit.edu 253-5881 32-G782 Prof. Martin Rinard rinard@mit.edu 258-6922 32-G828 Rooms MWF 3-370 TH 4-149 Course Secretary Cree Bruins cbruins@csail.mit.edu 32-G784a Teaching Assistants David Vargas dvargas@mit.edu Website http://6.035.scripts.mit.edu

Reference Textbooks Modern Compiler Implementation in Java (Tiger book) A.W. Appel Cambridge University Press, 1998 ISBN 0-52158-388-8 A textbook tutorial on compiler implementation, including techniques for many language features Advanced Compiler Design and Implementation (Whale book) Steven Muchnick Morgan Kaufman Publishers, 1997 ISBN 1-55860-320-4 Compilers: Principles, Techniques and Tools (Dragon book) Aho, Lam, Sethi and Ullman Addison-Wesley, 2006 ISBN 0321486811 Engineering a Compiler (Ark book) Keith D. Cooper, Linda Torczon Morgan Kaufman Publishers, 2003 ISBN 1-55860-698-X Optimizing Compilers for Modern Architectures Randy Allen and Ken Kennedy Morgan Kaufman Publishers, 2001 ISBN 1-55860-286-0 Essentially a recipe book of optimizations; very complete and suited for industrial practitioners and researchers. The classic compilers textbook, although its front-end emphasis reflects its age. New edition has more optimization material. A modern classroom textbook, with increased emphasis on the back-end and implementation techniques. A modern textbook that focuses on optimizations including parallelization and memory hierarchy optimization

The Project: The Five Segments Lexical and Syntax Analysis Semantic Analysis Code Generation Dataflow Analysis Optimizations

Each Segment... Segment Start Project Description Lectures 2 to 5 lectures Project Time (Design Document) (Project Checkpoint) Project Due

Project Groups 1 st project is an individual project Projects 2 to 5 are group projects Each group consists of 3 to 4 students Projects are designed to produce a compiler by the end of class Grading All group members (mostly) get the same grade Scanner/parser ungraded (you can use this to evaluate potential group members) Semantic Checker/Code Generator graded together Dataflow Analyzer/Optimizer graded together 5 turnins total, 2 turnins are graded

Quizzes Two Quizzes In-Class Quiz 50 Minutes (be on time!) Open book, open notes

Mini Quizzes You already got one Given at the beginning of the class Collected at the end Collaboration is OK This is in lieu of time consuming problem sets

Grading Breakdown Project = 70% of grade 25% Semantic Checker/Code Generator 45% Dataflow Analyzer/Optimizer Quizzes 24%, 12% each Miniquizzes/class participation 6% Relectures

More Course Stuff Blank page project all the rope you what! Challenging project You are on your own! Collaboration policy Talk all you want Write all of your code yourself Accepted Languages Java Scala (people have done well with this language) Haskell

Why Study Compilers? Compilers enable programming at a high level language instead of machine instructions. Malleability, Portability, Modularity, Simplicity, Programmer Productivity Also Efficiency and Performance Indispensible programmer productivity tool One of most complex software systems to build

Compilers Construction touches many topics in Computer Science Theory Finite State Automata, Grammars and Parsing, data-flow Algorithms Graph manipulation, dynamic programming Data structures Symbol tables, abstract syntax trees Systems Allocation and naming, multi-pass systems, compiler construction Computer Architecture Memory hierarchy, instruction selection, interlocks and latencies, parallelism Security Detection of and Protection against vulnerabilities Software Engineering Software development environments, debugging Artificial Intelligence Heuristic based search for best optimizations

What a Compiler Does Input: High-level programming language Output: Low-level assembly instructions Compiler does the translation: Read and understand the program Precisely determine what actions it requires Figure-out how to faithfully carry out those actions Instruct the computer to carry out those actions

Input to the Compiler Standard imperative language (Java, C, C++) State Variables, Structures, Arrays Computation Expressions (arithmetic, logical, etc.) Assignment statements Control flow (conditionals, loops) Procedures

Output of the Compiler State Registers Memory with Flat Address Space Machine code load/store architecture Load, store instructions Arithmetic, logical operations on registers Branch instructions

Example (input program) int sumcalc(int a, int b, int N) { int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y;

Example (Output assembly code) sumcalc: pushq %rbp movq %rsp, %rbp movl %edi, -4(%rbp) movl %esi, -8(%rbp) movl %edx, -12(%rbp) movl $0, -20(%rbp) movl $0, -24(%rbp) movl $0, -16(%rbp).L2: movl -16(%rbp), %eax cmpl -12(%rbp), %eax jg.l3 movl -4(%rbp), %eax leal 0(,%rax,4), %edx leaq -8(%rbp), %rax movq %rax, -40(%rbp) movl %edx, %eax movq -40(%rbp), %rcx cltd idivl (%rcx) movl %eax, -28(%rbp) movl -28(%rbp), %edx imull -16(%rbp), %edx movl -16(%rbp), %eax incl %eax imull %eax, %eax addl %eax, %edx leaq -20(%rbp), %rax addl %edx, (%rax) movl -8(%rbp), %eax movl %eax, %edx imull -24(%rbp), %edx leaq -20(%rbp), %rax addl %edx, (%rax) leaq -16(%rbp), %rax incl (%rax) jmp.l2.l3: movl -20(%rbp), %eax leave ret.size sumcalc,.-sumcalc.section.lframe1:.long.lecie1-.lscie1.lscie1:.long 0x0.byte 0x1.string "".uleb128 0x1.sleb128-8.byte 0x10.byte 0xc.uleb128 0x7.uleb128 0x8.byte 0x90.uleb128 0x1.align 8.LECIE1:.long.LEFDE1-.LASFDE1.long.LASFDE1-.Lframe1.quad.LFB2.quad.LFE2-.LFB2.byte 0x4.long.LCFI0-.LFB2.byte 0xe.uleb128 0x10.byte 0x86.uleb128 0x2.byte 0x4.long.LCFI1-.LCFI0.byte 0xd.uleb128 0x6.align 8

Optimization Example int sumcalc(int a, int b, int N) { int i; int x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y;

pushq %rbp movq %rsp, %rbp movl %edi, -4(%rbp) movl %esi, -8(%rbp) movl %edx, -12(%rbp) movl $0, -20(%rbp) movl $0, -24(%rbp) movl $0, -16(%rbp).L2: movl -16(%rbp), %eax cmpl -12(%rbp), %eax jg.l3 movl -4(%rbp), %eax leal 0(,%rax,4), %edx leaq -8(%rbp), %rax movq %rax, -40(%rbp) movl %edx, %eax movq -40(%rbp), %rcx cltd idivl (%rcx) movl %eax, -28(%rbp) movl -28(%rbp), %edx imull -16(%rbp), %edx movl -16(%rbp), %eax incl %eax imull %eax, %eax addl %eax, %edx leaq -20(%rbp), %rax addl %edx, (%rax) movl -8(%rbp), %eax movl %eax, %edx imull -24(%rbp), %edx leaq -20(%rbp), %rax addl %edx, (%rax) leaq -16(%rbp), %rax incl (%rax) jmp.l2.l3: movl -20(%rbp), %eax leave ret 45

Lets Optimize... int sumcalc(int a, int b, int N) { int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y;

Constant Propagation int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y;

Constant Propagation int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*y;

Constant Propagation int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*0;

Algebraic Simplification int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*0;

Algebraic Simplification int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x + b*0;

Algebraic Simplification int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x;

Copy Propagation int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x;

Copy Propagation int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1); x = x;

Copy Propagation int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1);

Common Subexpression Elimination int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1);

Common Subexpression Elimination int i, x, y; y = 0; x = x + (4*a/b)*i + (i+1)*(i+1);

Common Subexpression Elimination int i, x, y, t; y = 0; t = i+1; x = x + (4*a/b)*i + t*t;

Dead Code Elimination int i, x, y, t; y = 0; t = i+1; x = x + (4*a/b)*i + t*t;

Dead Code Elimination int i, x, y, t; y = 0; t = i+1; x = x + (4*a/b)*i + t*t;

Dead Code Elimination int i, x, t; t = i+1; x = x + (4*a/b)*i + t*t;

Loop Invariant Removal int i, x, t; t = i+1; x = x + (4*a/b)*i + t*t;

Loop Invariant Removal int i, x, t; t = i+1; x = x + (4*a/b)*i + t*t;

Loop Invariant Removal int i, x, t, u; u = (4*a/b); t = i+1; x = x + u*i + t*t;

Strength Reduction int i, x, t, u; u = (4*a/b); t = i+1; x = x + u*i + t*t;

Strength Reduction int i, x, t, u; u = (4*a/b); t = i+1; x = x + u*i + t*t;

Strength Reduction int i, x, t, u, v; u = ((a<<2)/b); v = 0; t = i+1; x = x + v + t*t; v = v + u;

Register Allocation Local variable X Local variable Y Local variable I fp

Register Allocation Local variable X Local variable Y Local variable I fp $r8d = X $r9d = t $r10d = u $ebx = v $ecx = i

Optimized Example int sumcalc(int a, int b, int N) { int i, x, t, u, v; u = ((a<<2)/b); v = 0; t = i+1; x = x + v + t*t; v = v + u;

Unoptimized Code pushq %rbp movq %rsp, %rbp movl %edi, -4(%rbp) movl %esi, -8(%rbp) movl %edx, -12(%rbp) movl $0, -20(%rbp) movl $0, -24(%rbp) movl $0, -16(%rbp).L2: movl -16(%rbp), %eax cmpl -12(%rbp), %eax jg.l3 movl -4(%rbp), %eax leal 0(,%rax,4), %edx leaq -8(%rbp), %rax movq %rax, -40(%rbp) movl %edx, %eax movq -40(%rbp), %rcx cltd idivl (%rcx) movl %eax, -28(%rbp) movl -28(%rbp), %edx imull -16(%rbp), %edx movl -16(%rbp), %eax incl %eax imull %eax, %eax addl %eax, %edx leaq -20(%rbp), %rax addl %edx, (%rax) movl -8(%rbp), %eax movl %eax, %edx imull -24(%rbp), %edx leaq -20(%rbp), %rax addl %edx, (%rax) leaq -16(%rbp), %rax incl (%rax) jmp.l2.l3: movl -20(%rbp), %eax leave ret Inner Loop: 10*mov + 5*lea + 5*add/inc + 4*div/mul + 5*cmp/br/jmp = 29 instructions Execution time = 43 sec Optimized Code xorl %r8d, %r8d xorl %ecx, %ecx movl %edx, %r9d cmpl %edx, %r8d jg.l7 sall $2, %edi.l5: movl %edi, %eax cltd idivl %esi leal 1(%rcx), %edx movl %eax, %r10d imull %ecx, %r10d movl %edx, %ecx imull %edx, %ecx leal (%r10,%rcx), %eax movl %edx, %ecx addl %eax, %r8d cmpl %r9d, %edx jle.l5.l7: movl %r8d, %eax ret 4*mov + 2*lea + 1*add/inc+ 3*div/mul + 2*cmp/br/jmp = 12 instructions Execution time = 17 sec

Compilers Optimize Programs for Performance/Speed Code Size Power Consumption Fast/Efficient Compilation Security/Reliability Debugging