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

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

What do Compilers Produce?

The Structure of a Syntax-Directed Compiler

Compilers and Interpreters

Formats of Translated Programs

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

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

Compilers. History of Compilers. A compiler allows programmers to ignore the machine-dependent details of programming.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The Structure of a Compiler

The role of semantic analysis in a compiler

CST-402(T): Language Processors

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Semantic Analysis. Lecture 9. February 7, 2018

Introduction. CS 2210 Compiler Design Wonsun Ahn

Advances in Compilers

Appendix A The DL Language

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Undergraduate Compilers in a Day

Compilers and Code Optimization EDOARDO FUSELLA

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

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

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

Compiler Design (40-414)

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 415 Midterm Exam Spring 2002

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

When do We Run a Compiler?

Working of the Compilers

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Yacc: A Syntactic Analysers Generator

CS 321 IV. Overview of Compilation

The Structure of a Syntax-Directed Compiler

4. An interpreter is a program that

Compilers & Translation Systems Engineering

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Introduction to Compiler Design

History of Compilers The term

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

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

TDDD55- Compilers and Interpreters Lesson 3

Front End. Hwansoo Han

Pioneering Compiler Design

Compilers. Prerequisites

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

TDDD55 - Compilers and Interpreters Lesson 3

Compiler Structure. Lexical. Scanning/ Screening. Analysis. Syntax. Parsing. Analysis. Semantic. Context Analysis. Analysis.

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

An Overview of Compilation

COMPILER DESIGN LECTURE NOTES

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

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.


CS A331 Programming Language Concepts

Alternatives for semantic processing

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

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

CS321 Languages and Compiler Design I. Winter 2012 Lecture 1

Compilers. Lecture 2 Overview. (original slides by Sam

Time : 1 Hour Max Marks : 30

Intermediate Code Generation

CSE 504: Compiler Design. Code Generation

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Compiler Construction

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Compilers Crash Course

Intermediate Code & Local Optimizations

Syntax Errors; Static Semantics

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

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

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

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

COMPILER CONSTRUCTION Seminar 02 TDDB44

CS101 Introduction to Programming Languages and Compilers

Introduction to Programming Using Java (98-388)

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

SLIDE 2. At the beginning of the lecture, we answer question: On what platform the system will work when discussing this subject?

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

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

CS 360 Programming Languages Interpreters

Compiler Design. Subject Code: 6CS63/06IS662. Part A UNIT 1. Chapter Introduction. 1.1 Language Processors

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

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Reversing. Time to get with the program

Intermediate Representations

LECTURE NOTES ON COMPILER DESIGN P a g e 2

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Semantic Analysis and Type Checking

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Chapter 2 A Quick Tour

LECTURE 3. Compiler Phases

The View from 35,000 Feet

Project Compiler. CS031 TA Help Session November 28, 2011

Compilers and interpreters

The Design and Implementation of Gnu Compiler Collection

Transcription:

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

What is a compiler?

What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g., Fortran, C++) to lowlevel assembly language that can be executed by hardware int a, b; a = 3; if (a < 4) { b = 2; } else { b = 3; } 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

Compilers are translators Fortran C C++ Java Text processing language HTML/XML Verilog Command & Scripting Languages Natural language Domain specific languages translate Machine code Virtual machine code Augmented machine code Low-level commands VLSI circuits Another language

Compilers are optimizers Can perform optimizations to make a program more efficient int a, b, c; b = a + 3; c = a + 3; var a var b var c mov a r1 addi 3 r1 mov r1 b mov a r2 addi 3 r2 mov r2 c var a var b var c mov a r1 addi 3 r1 mov r1 b mov r1 c

Why do we need compilers? Compilers provide portability Old days: whenever a new machine was built, programs had to be rewritten to support new instruction sets IBM System/360 (1964): Common Instruction Set Architecture (ISA) programs could be run on any machine which supported ISA Common ISA is a huge deal (note continued existence of x86) But still a problem: when new ISA is introduced (EPIC) or new extensions added (x86-64), compilers would have to be rewritten Compile to machine-independent bytecode!

Why do we need compilers? (II) Compilers enable high performance and productivity Old: programmers wrote in assembly language, architectures were simple (no pipelines, caches, etc.) Close match between programs and machines easier to achieve performance New: programmers write in high level languages (Ruby, Python), architectures are complex (superscalar, out-of-order execution, multicore) Compilers are needed to bridge this semantic gap Compilers let programmers write in high level languages and still get good performance on complex architectures

Semantic Gap def index @posts = Post.find(:all) respond_to do format format.html # index.html.erb format.xml { render :xml => @posts } end end Snippet of Ruby-on-rails code AMD Barcelona architecture

Semantic Gap def index @posts = Post.find(:all) respond_to do format format.html # index.html.erb format.xml { render :xml => @posts } end end Snippet of Ruby-on-rails code AMD Barcelona architecture

Why do I need to learn compilers? Better understanding of your program s behavior, performance, etc. For your own DSL, experimental language, etc. For your own architecture, programming model, etc. For your own optimization

Some common compiler types 1.High level language assembly language (e.g., gcc) 2.High level language machine independent bytecode (e.g., javac) 3.Bytecode native machine code (e.g., java s JIT compiler) 4.High level language high level language (e.g., domain specific languages, many research languages, e.g., ImpSynt from my group)

HLL to Assembly Compiler converts program into assembly Assembler is machine-specific translator which converts assembly into machine code add $7 $8 $9 ($7 = $8 + $9) 000000 00111 01000 01001 00000 100000 Conversion is usually one-to-one with some exceptions Program locations Variable names

HLL to Bytecode Compiler converts program into machine independent bytecode e.g., javac generates Java bytecode, C# compiler generates CIL Interpreter then executes bytecode on-the-fly Bytecode instructions are executed by invoking methods of the interpreter, rather than directly executing on the machine Aside: what are the pros and cons of this approach?

Bytecode to Assembly Just-in-time compiler compiles code while program executes to produce machine code Is this better or worse than a compiler which generates machine code directly from the program? Dynamically mixing interpreter and JIT compilation Method-based Trace compilation and On-stack replacement

Structure of a Compiler

Scanner Compiler starts by seeing only program text if (a < 4) { b = 5 }

Scanner Compiler starts by seeing only program text i f ( a < 4 ) { \n \t b = 5 \n }

Scanner Compiler starts by seeing only program text Scanner converts program text into string of tokens i f ( a < 4 ) { \n \t b = 5 \n }

Scanner Compiler starts by seeing only program text Scanner converts program text into string of tokens But we still don t know what the syntactic structure of the program is

Parser Converts string of tokens into a parse tree or an abstract syntax tree. Captures syntactic structure of code (i.e., this is an if statement, with a then-block )

Parser Converts string of tokens into a parse tree or an abstract syntax tree. Captures syntactic structure of code (i.e., this is an if statement, with a then-block )

Semantic actions Interpret the semantics of syntactic constructs Note that up until now we have only been concerned with what the syntax of the code is What s the difference?

Syntax vs. Semantics Syntax: grammatical structure of language What symbols, in what order, is a legal part of the language? But something that is syntactically correct may mean nothing! colorless green ideas sleep furiously Semantics: meaning of language What does a particular set of symbols, in a particular order, mean? What does it mean to be an if statement? evaluate the conditional, if the conditional is true, execute the then clause, otherwise execute the else clause

A note on semantics How do you define semantics? Static semantics: properties of programs All variables must have a type Expressions must use consistent types Can define using attribute grammars Execution semantics: how does a program execute? Can define an operational or denotational semantics for a language Will be discussed in the last few weeks of the class For many languages, the compiler is the specification

Semantic actions Actions taken by compiler based on the semantics of program statements Building a symbol table Generating intermediate representations

Symbol tables A list of every declaration in the program, along with other information Variable declarations: types, scope Function declarations: return types, # and type of arguments Program Example Integer ii;... ii = 3.5;... print ii; Symbol Table Name Type Scope ii int global...

Intermediate representation Also called IR A (relatively) low level representation of the program But not machine-specific! One example: three address code bge a, 4, done mov 5, b done: //done! Each instruction can take at most three operands (variables, literals, or labels) Note: no registers!

Optimizer Transforms code to make it more efficient Different kinds, operating at different levels High-level optimizations Loop interchange, parallelization Operates at level of AST, or even source code Scalar optimizations Dead code elimination, common sub-expression elimination Operates on IR Local optimizations Strength reduction, constant folding Operates on small sequences of instructions

Code generation Generate assembly from intermediate representation Select which instructions to use Schedule instructions Decide which registers to use bge a, 4, done mov 5, b done: //done! ld a, r1 mov 4, r2 cmp r1, r2 bge done mov 5, r3 st r3, b done:

Code generation Generate assembly from intermediate representation Select which instructions to use Schedule instructions Decide which registers to use bge a, 4, done mov 5, b done: //done! mov 4, r1 ld a, r2 cmp r1, r2 blt done mov 5, r1 st r1, b done:

Overall structure of a compiler Use regular expressions to define tokens. Can then use scanner generators such as lex or flex. Define language using context free grammars. Can then use parser generators such as yacc or bison. Semantic routines done by hand. But can be formalized. Written manually. Automation is an active research area (e.g., dataflow analysis frameworks) Written manually

Overall structure of a compiler Use regular expressions to define tokens. Can then use scanner generators such as lex or flex. Define language using context free grammars. Can then use parser generators such as yacc or bison. Semantic routines done by hand. But can be formalized. Written manually. Automation is an active research area (e.g., dataflow analysis frameworks) Written manually

Front-end vs. Back-end Scanner + Parser + Semantic actions + (high level) optimizations called the frontend of a compiler IR-level optimizations and code generation (instruction selection, scheduling, register allocation) called the back-end of a compiler Can build multiple front-ends for a particular back-end e.g., gcc & g++, or many front-ends which generate CIL Can build multiple back-ends for a particular front-end e.g., gcc allows targeting different architectures front end back end

Design considerations (I) Compiler and language designs influence each other Higher level languages are harder to compile Domain-specific languages (computational genomics, machine learning, etc.) and operations More work to bridge the gap between language and assembly Flexible languages are often harder to compile Dynamic typing (Ruby, Python) makes a language very flexible, but it is hard for a compiler to catch errors (in fact, many simply won t)

Design considerations (II) Compiler design is influenced by architectures CISC vs. RISC CISC designed for days when programmers wrote in assembly RISC has a much simpler instruction model Accelerators CUDA, OpenCL, etc. Different programming models Performance portability