Compilers Crash Course

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

CA Compiler Construction

Compiler Construction LECTURE # 1

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

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

Lecture 1: Introduction

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

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

Compiler Design (40-414)

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

CSE 582 Autumn 2002 Exam 11/26/02

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

Introduction to Compilers

Compiling Regular Expressions COMP360

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

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

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

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

CS 314 Principles of Programming Languages

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

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

Practical Malware Analysis

Formal Languages and Compilers Lecture I: Introduction to Compilers

LECTURE NOTES ON COMPILER DESIGN P a g e 2

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

SOFTWARE ARCHITECTURE 5. COMPILER

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

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

Lecture 1 CIS 341: COMPILERS

Chapter 2 A Quick Tour

Compilers. Pierre Geurts

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

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

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

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

Overview of Compiler. A. Introduction

General Course Information. Catalogue Description. Objectives

Compiler construction

INF5110 Compiler Construction

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

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

2001 bei Prof. Dr. Uwe Kastens

CSE P 501 Exam 11/17/05 Sample Solution

Introduction to Compiler Construction

Application Specific Signal Processors S

Compiler Construction D7011E

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

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

CSE 401/M501 Compilers

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

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Compiler construction in4303 lecture 9

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

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

CSE 582 Autumn 2002 Exam Sample Solution

Software Lesson 2 Outline

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

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Week 3: Compilers and Interpreters

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II


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

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

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Programming Language Processor Theory

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

Working of the Compilers

Compilers and Interpreters

Compilers and Code Optimization EDOARDO FUSELLA

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Compiler Construction 2010 (6 credits)

The Hardware/Software Interface CSE351 Spring 2013

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

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

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

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

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

Introduction to Compilers Professor Jeremy Siek

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

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

Principles of Compiler Design

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Compiler Construction

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

CPS104 Recitation: Assembly Programming

Introduction to Compiler Construction

Generation. representation to the machine

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

CS426 Compiler Construction Fall 2006

Compilers. Lecture 2 Overview. (original slides by Sam

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

CPEG421/621 Tutorial

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

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

CS131: Programming Languages and Compilers. Spring 2017

CST-402(T): Language Processors

Transcription:

Compilers Crash Course Prof. Michael Clarkson CSci 6907.85 Spring 2014 Slides Acknowledgment: Prof. Andrew Myers (Cornell) What are Compilers? 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 2

Source Code 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; } 3 Machine Code Optimized for hardware expr: Redundancy, ambiguity reduced Information about intent and reasoning lost Assembly code machine code pushl subl sall leave ret %ebp %esp, %ebp $4, %esp %eax, %edx 8(%ebp), %edx %eax %eax, %edx %eax $2, %eax %eax, -4(%ebp) -4(%ebp), %eax 55 89 e5 83 ec 04 8b 45 08 89 c2 0f af 55 08 8b 45 08 40 0f af d0 8b 45 08 40 0f af c2 c1 e0 02 89 45 fc 8b 45 fc c9 c3 4

Example (Output assembly code) Unoptimized Code Optimized Code expr: pushl subl sall leave ret %ebp %esp, %ebp $4, %esp %eax, %edx 8(%ebp), %edx %eax %eax, %edx %eax $2, %eax %eax, -4(%ebp) -4(%ebp), %eax expr: pushl sall leave ret %ebp %esp, %ebp 8(%ebp), %edx %edx $2, %eax 5 How to translate? Source code and machine code mismatch Goals: source-level expressiveness for task best performance for concrete computation reasonable translation efficiency (< O(n 3 )) maintainable compiler code 6

How to translate correctly? Programming languages describe computation precisely erefore: 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 some compilers have been proven correct! [X. Leroy, Formal Verification of a Realistic Compiler, CACM 52(7), 2009] 7 How to translate effectively? High-level source code? Low-level machine code 8

Idea: translate in steps Compiler uses a series of different intermediate representations (IRs) of programs. Different IRs are good for different phases of compilation 9 Compilation in a Nutshell 1 Source code (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 10

Compilation in a Nutshell 2 if boolean == int b int 0 int = int a int b lvalue Intermediate Code Generation if b == 0 goto L1 else L2 L1: a = b L2: cmp r b, 0 jnz L2 L1: mov r a, r b L2: Assembly Code Generation Register allocation, Optimization cmp ecx, 0 cmovz [ebp+8],ecx 11 Compilation in a Nutshell 2 if boolean == int b int 0 int = int a int b lvalue Intermediate Code Generation if b == 0 goto L1 else L2 L1: a = b L2: cmp r b, 0 jnz L2 L1: mov r a, r b L2: Assembly Code Generation Register allocation, Optimization cmp ecx, 0 cmovz [ebp+8],ecx 12

Simplified Compiler Structure Source code (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 Assembly Code generation Front end (machine-independent) Back end (machine-dependent) 13 Even Bigger Picture Assembly code Source code Compiler Fully-resolved object code (machine code + symbol tables, relocation info) Assembler Linker Loader Executable image in memory Object code (machine code + symbol tables) 14

Where to Learn More Compilers Principles, Techniques and Tools. Aho, Lam, Sethi and Ullman ( e Dragon Book) (strength: parsing) Modern Compiler Implementation in Java. Andrew Appel. (strength: translation) Advanced Compiler Design and Implementation. Steve Muchnick. (strength: optimization) 15