1 / 18

CS153: Compilers

Greg Morrisett Fall 2013. CS153: Compilers. What is the course about?. Compilers. (duh) Translating one programming language into another. Also interpreters. Translating and running a language from within another language. Basic Architecture. Source Code. Parsing. Elaboration.

adolfo
Download Presentation

CS153: Compilers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Greg Morrisett Fall 2013 CS153: Compilers

  2. What is the course about? • Compilers. • (duh) • Translating one programming language into another. • Also interpreters. • Translating and running a language from within another language.

  3. Basic Architecture Source Code Parsing Elaboration Lowering Code Generation Optimization Target Code

  4. Front End • Lexing & Parsing • From strings to data structures • Usually split into two phases: Strings/Files Tokens Abstract Syntax Trees Lexing Parsing

  5. 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.

  6. 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

  7. 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

  8. 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 Intermediate Code ASTs optimization Intermediate Code ASTs

  9. Code Generation Translate intermediate code into target code. • Register assignment • Instruction selection • Instruction scheduling • Machine-specific optimization Machine Code Intermediate Code ASTs

  10. 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, itaniumdisasters; register windows • These days, architecture folks use compilers too! • API designers • A language is the ultimate API • c.f., Facebook

  11. 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.

  12. 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.

  13. Programming Environment • Ocaml • Not familiar? Pick up along the way or take 51. • Ideal for writing compilers. • SPIM • MIPS simulator. • Ideal target for compilers.

  14. Book Modern Compiler Implementation in ML, by Andrew Appel. Good for background reading, but we won’t follow closely.

  15. 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

  16. 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

  17. 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++?

  18. 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

More Related