greg morrisettfall 2013. compilers. (duh) translating one programming language into another. ...

18
CS153: Compilers Greg Morrisett Fall 2013

Post on 19-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

CS153: CompilersGreg Morrisett Fall 2013

Page 2: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

What is the course about?

Compilers. (duh) Translating one programming language

into another. Also interpreters.

Translating and running a language from within another language.

Page 3: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Basic Architecture

ParsingElaboratio

nLowering

Code Generatio

n

Optimization

Source Code

Target Code

Page 4: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Front End

Lexing & Parsing From strings to data structures Usually split into two phases:

Strings/Files

TokensAbstract Syntax Trees

Lexing Parsing

Page 5: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Parsing Tools

Parsing is something that happens in essentially all applications. E.g., Google search bar, calendar, etc. First step in going from raw data to information

Lots of CS theory (121) to help out Regular expressions (finite state automata) Context-free grammars (push-down automata) These abstractions show up in optimization

too! Lots of tool support

E.g., Lex and Yacc; parsing combinators; etc.

Page 6: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Elaboration

Type-checking. Resolve variables, modules, etc. Check that operations are given values of the

right types. Infer types for sub-expressions. Check for other safety/security problems.

UntypedAbstract Syntax Trees

Typed Abstract Syntax Trees

Page 7: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Lowering

Translate high-level features into a small number of target-like constructs.

e.g., while, for, do-loops all compiled to code using goto’s.

e.g., objects, closures to records and function pointers.

e.g., make type-tests, array-bounds checks, etc. explicit. Typed

Abstract Syntax Trees

Intermediate Code ASTs

Page 8: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Optimization

Rewrite expensive sequences of operations into less expensive.

e.g., constant folding: 3+4 7 e.g., lift an invariant computation out of a loop e.g., parallelize a loop

optimization

Intermediate Code ASTs

Intermediate Code ASTs

Page 9: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Code Generation

Translate intermediate code into target code.

Register assignment Instruction selection Instruction scheduling Machine-specific optimization

Machine Code

Intermediate Code ASTs

Page 10: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Who Should Take 153?

People fascinated by languages & compilers Why does[n’t] this language include this feature?

Systems programmers Know the one tool that you use every day. What can[’t] a compiler do well?

Architecture designers See 432, itanium disasters; register windows These days, architecture folks use compilers too!

API designers A language is the ultimate API c.f., Facebook

Page 11: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

What background?

Ideally CS51 & CS61 But 51 is probably good enough. 61 is okay, but you will have to pick up

Ocaml. Some assumptions:

Assume you know a little bit about how machines work (e.g., 2’s compliment arithmetic).▪ If you don’t know something, ask me!

Assume you can write functional programs. Assume you are willing to work your tail off.

Page 12: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Administrivia

Web site: http://people.fas.harvard.edu/~lib153/

TF: Lucas Waye 9 programming assignments.

Bulk of your grade. Work with a partner. No late days. Ever.

Mid-term (Oct 9) and Final exams.

Page 13: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Programming Environment

Ocaml Not familiar? Pick up along the way or

take 51. Ideal for writing compilers.

SPIM MIPS simulator. Ideal target for compilers.

Page 14: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Book

Modern Compiler Implementation in ML, by Andrew Appel.

Good for background reading, but we won’t follow closely.

Page 15: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Projects

MIPS simulator Understand the machine we are targeting

Fortran-ish -> MIPS Simple front-end Simple lowering & code generation

C-ish -> MIPS 1st-order procedures, structs, arrays

Scheme-ish -> C-ish Higher-order procedures, type-tests

ML-ish -> Scheme-ish Type inference

Page 16: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Projects, continued

Algebraic optimization Tree-based rewriting

Control-flow graphs Rip out naïve C-ish backend and replace

with one based on control-flow graphs. Implement liveness analysis Implement graph-coloring register

allocation

Page 17: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Key Outcomes

Understand how compilers work Parsing, type-checking & inference, lowering,

analysis, optimization, code generation. How to rewrite your code so that the compiler can do

its job better. Significant engineering experience on a big project.

Understand language and architecture design tradeoffs. e.g., why are error messages in compilers so bad? e.g., why do advanced languages essentially require

garbage collection? e.g., what prevents us from parallelizing C/C++?

Page 18: Greg MorrisettFall 2013.  Compilers.  (duh)  Translating one programming language into another.  Also interpreters.  Translating and running a language

Along the way…

What’s happening with architecture? Multi-core Heterogeneous units

What’s happening with languages? Security may matter more than

performance Productivity may matter more than

performance Cluster-level performance (e.g.,

Map/Reduce) Feedback-driven optimization