Course IntroductionLecture 1
Kevin Leach
January 4, 2018
Course IntroductionLecture 1
Compilers, what a concept
Kevin Leach
January 4, 2018
Cunning Plan
É AdministriviaÉ Compilers overviewÉ Compilation exampleÉ Course motivationÉ Course overview
Compiler Construction 2/40
Course Information
É Instructor: Kevin Leach ⟨[email protected]⟩É Please put EECS483 in the subject (or just use Piazza)
É Office Hours: TBA (see Piazza, prefer Thursday)É Meeting Time: MW 2:00–3:30, EECS 1311
É Bonus Discussion: F: 12:00–1:00, FXB 1012É Books (optional)
É Programming Language Pragmaticsby Michael Scott
É Compilers: Principles, Techniques, and Toolsby Alred Aho et al., a.k.a. the Dragon Book
Compiler Construction 3/40
What is a compiler?
The thing Eclipse runs when you hit ‘Play’?
Compiler Construction 5/40
What is a compiler?
...then you add try/catch to make the red go away?
Compiler Construction 6/40
What is a compiler?
A tool that turns your C code into segfaults?
Compiler Construction 7/40
What is a compiler?
A tool that turns your C code into segfaults?
Compiler Construction 7/40
What is a compiler?
A tool that turns your C code into segfaults?
Compiler Construction 7/40
What is a compiler?
A program that produces incomprehensible warnings?
Compiler Construction 8/40
What is a compiler?
É A language processing toolÉ Input in one language is transformed to an equivalent output
in another language
Structured Input Structured Output
Source Code
firefox.c firefox.exegcc -o firefox.exe firefox.c
Compiler Program
Compiler Construction 9/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)
É Polymorphism (Cat inherits Animal)É Variables (human-readable naming)É Functional paradigms (functions and immutable state)É Data types (int/string/char)É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)
É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)É Polymorphism (Cat inherits Animal)
É Variables (human-readable naming)É Functional paradigms (functions and immutable state)É Data types (int/string/char)É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)
É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)É Polymorphism (Cat inherits Animal)É Variables (human-readable naming)
É Functional paradigms (functions and immutable state)É Data types (int/string/char)É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)
É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)É Polymorphism (Cat inherits Animal)É Variables (human-readable naming)É Functional paradigms (functions and immutable state)
É Data types (int/string/char)É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)
É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)É Polymorphism (Cat inherits Animal)É Variables (human-readable naming)É Functional paradigms (functions and immutable state)É Data types (int/string/char)
É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)
É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)É Polymorphism (Cat inherits Animal)É Variables (human-readable naming)É Functional paradigms (functions and immutable state)É Data types (int/string/char)É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)
É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)É Polymorphism (Cat inherits Animal)É Variables (human-readable naming)É Functional paradigms (functions and immutable state)É Data types (int/string/char)É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)
É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Workflow
É Goal Developer can access language featuresÉ Object-oriented paradigms (classes)É Polymorphism (Cat inherits Animal)É Variables (human-readable naming)É Functional paradigms (functions and immutable state)É Data types (int/string/char)É Control flow (if/then/else)
É Constraint fast execution on native platform (e.g., x86 CPU)É Need to “remove” features so that a CPU circuit can understand
Compiler Construction 10/40
Compiler Input
É Input is structuredÉ Amenable to automated analysis and processingÉ Developer can write a specification for program behavior
É This class: Use COOL programming language
Compiler Construction 11/40
Compiler Input
É The compiler gives a promise to developer:If you supply a valid program according to the language’s rules,the compiler provides a valid assembly output
É Implication: Developer promises to provide valid programÉ If they don’t, compiler gets to complain!
Compiler Construction 12/40
Compiler Input
É The compiler gives a promise to developer:If you supply a valid program according to the language’s rules,the compiler provides a valid assembly outputÉ Implication: Developer promises to provide valid program
É If they don’t, compiler gets to complain!
Compiler Construction 12/40
Compiler Input
É The compiler gives a promise to developer:If you supply a valid program according to the language’s rules,the compiler provides a valid assembly outputÉ Implication: Developer promises to provide valid programÉ If they don’t, compiler gets to complain!
Compiler Construction 12/40
Compiler Output
É Output is also structuredÉ Windows PE, Linux ELF, etc...É Usually a lowered format
É This class: Produce assembly (either RISC or x86)
Compiler Construction 13/40
Compiler Output
Compiler performssemantics preserving
transformations
Compiler Construction 14/40
Issues Guiding Compiler Design
É CorrectnessÉ Compiler performs semantics-preserving transformations
É SpeedÉ Run timeÉ Compilation time
É SpaceÉ How large is the output?
É Feedback to userÉ On a scale of LaTex to Python, how good is your feedback?
ÉDebuggingÉ Can your output help the developer identify problems with
their desired program?
Compiler Construction 15/40
Issues Guiding Compiler Design
É CorrectnessÉ Compiler performs semantics-preserving transformations
É SpeedÉ Run timeÉ Compilation time
É SpaceÉ How large is the output?
É Feedback to userÉ On a scale of LaTex to Python, how good is your feedback?
ÉDebuggingÉ Can your output help the developer identify problems with
their desired program?
Compiler Construction 15/40
Issues Guiding Compiler Design
É CorrectnessÉ Compiler performs semantics-preserving transformations
É SpeedÉ Run timeÉ Compilation time
É SpaceÉ How large is the output?
É Feedback to userÉ On a scale of LaTex to Python, how good is your feedback?
ÉDebuggingÉ Can your output help the developer identify problems with
their desired program?
Compiler Construction 15/40
Issues Guiding Compiler Design
É CorrectnessÉ Compiler performs semantics-preserving transformations
É SpeedÉ Run timeÉ Compilation time
É SpaceÉ How large is the output?
É Feedback to userÉ On a scale of LaTex to Python, how good is your feedback?
ÉDebuggingÉ Can your output help the developer identify problems with
their desired program?
Compiler Construction 15/40
Issues Guiding Compiler Design
É CorrectnessÉ Compiler performs semantics-preserving transformations
É SpeedÉ Run timeÉ Compilation time
É SpaceÉ How large is the output?
É Feedback to userÉ On a scale of LaTex to Python, how good is your feedback?
ÉDebuggingÉ Can your output help the developer identify problems with
their desired program?
Compiler Construction 15/40
Compilers vs. Interpreters
É Compilers lower code to assemblyÉ A compiler produces a program that runs later (e.g., after
compilation)
É Interpreters directly execute the code providedÉ Common in scripting languages
É Just-in-time compilation?É Secretly, languages like Python, Lua, and Java are all compiled at
runtime!
É Why would you compile vs. interpret?
Compiler Construction 16/40
Compilers vs. Interpreters
É Compilers lower code to assemblyÉ A compiler produces a program that runs later (e.g., after
compilation)É Interpreters directly execute the code provided
É Common in scripting languages
É Just-in-time compilation?É Secretly, languages like Python, Lua, and Java are all compiled at
runtime!
É Why would you compile vs. interpret?
Compiler Construction 16/40
Compilers vs. Interpreters
É Compilers lower code to assemblyÉ A compiler produces a program that runs later (e.g., after
compilation)É Interpreters directly execute the code provided
É Common in scripting languages
É Just-in-time compilation?É Secretly, languages like Python, Lua, and Java are all compiled at
runtime!
É Why would you compile vs. interpret?
Compiler Construction 16/40
Compilers vs. Interpreters
É Compilers lower code to assemblyÉ A compiler produces a program that runs later (e.g., after
compilation)É Interpreters directly execute the code provided
É Common in scripting languages
É Just-in-time compilation?É Secretly, languages like Python, Lua, and Java are all compiled at
runtime!
É Why would you compile vs. interpret?
Compiler Construction 16/40
Stages of Compilation
É Compilers are well-established
Input Code (Cool Language)
Tokenization (Lexical Analysis)
Parser (Syntactic Analysis)
Semantic Analysis
Code Generation
Optimization
Output Code (assembly)
Compiler Construction 17/40
Front End vs. Back End
É Lexing, Parsing, and Type-checking are the front endÉ Code generation and optimization are the back end
É Modularity
Compiler Construction 18/40
Front End vs. Back End
É Lexing, Parsing, and Type-checking are the front endÉ Code generation and optimization are the back endÉ Modularity
Compiler Construction 18/40
Compiling by Analogy
É We’re going to make an English compiler.É It will turn English into Spanish.
É Example InputI enjoy compilers.É Example Output
Yo disfruto compiladores.
É Compiling is a 5 step processÉ Each step takes an input and creates an intermediate
representation
Compiler Construction 19/40
Compiling by Analogy
É We’re going to make an English compiler.É It will turn English into Spanish.
É Example InputI enjoy compilers.É Example Output
Yo disfruto compiladores.
É Compiling is a 5 step processÉ Each step takes an input and creates an intermediate
representation
Compiler Construction 19/40
1: Tokenization or Lexical Analysis
É Break input into tokens and lexemes that are part of thelanguage’s lexicon
keywords (if, while) operators (+, -)variable names constants (“hello, world”, 1, 0x40)
É English tokens = words
Valid Inputs Invalid Input
I am handsome. 我很帅。He is pony. Èl es poni.
Output is a list of lexemes and tokens.“I am handsome.” → “I”, “am”, “handsome”, “.”
Compiler Construction 20/40
1: Tokenization or Lexical Analysis
É Break input into tokens and lexemes that are part of thelanguage’s lexicon
keywords (if, while) operators (+, -)variable names constants (“hello, world”, 1, 0x40)
É English tokens = words
Valid Inputs Invalid Input
I am handsome. 我很帅。He is pony. Èl es poni.
Output is a list of lexemes and tokens.“I am handsome.” → “I”, “am”, “handsome”, “.”
Compiler Construction 20/40
2: Parsing or Syntactic Analysis
É Ensure tokens follow the rules of a grammarÉ + operator should have expressions on both sides...
2 + 3; Ø 2 + ; 7
É English parsing = is it grammatically correct?
Valid Input Invalid Input
I am handsome. I handsome am:I be short. Is he correct!!
Output is a parse tree or abstract syntax tree (on board).
Compiler Construction 21/40
3: Semantic Analysis (type checking)É Ensure input makes sense
É Does it make sense to add strings to integers?
É English = Does it make sense?Valid Input Invalid Input
I am handsome. I am short.College is expensive. I am rich.
Output is an annotated abstract syntax tree (AST, on board)
Compiler Construction 22/40
4: Code Generation
É Create code that exhibits the required behaviorÉ let x <- 2 + 3 becomes add t0 <- 2, 3
É English/Spanish example: Start generating Spanish
Input Output
I am handsome. Yo soy guapo.
Output is straight-line assembly code
Compiler Construction 23/40
5: Optimization
É Analyze generated code, change it to execute fasterÉ let x <- 2 + 3 becomes li t0 <- 5
É English/Spanish example: Remove superfluous words
Input Output
Yo soy guapo. Soy guapo.Output is optimized straight-line assembly code
Compiler Construction 24/40
Compilers Today
É Early on: Lexing, Parsing most complex and expensive
É Today: optimization dominates everything many times over,both in execution time and community interestÉ Scientific programsÉ Advanced processors (DSP, speculative execution)É Small devices (speed =more time idle = longer battery)
É We will not focus on lexing/parsing optimizations
É Support for languages with new featuresÉ MIMD architectures
Compiler Construction 25/40
Compilers Today
É Early on: Lexing, Parsing most complex and expensive
É Today: optimization dominates everything many times over,both in execution time and community interestÉ Scientific programsÉ Advanced processors (DSP, speculative execution)É Small devices (speed =more time idle = longer battery)
É We will not focus on lexing/parsing optimizations
É Support for languages with new featuresÉ MIMD architectures
Compiler Construction 25/40
Who cares?
Many upper-level electives correspond to a natural job choice
É Databases→OracleÉ Embedded Computing→ SamsungÉ E-commerce→ AmazonÉ Graphics→Nvidia
É What about compilers?
Compiler Construction 26/40
Who cares?Many upper-level electives correspond to a natural job choice
É Databases→OracleÉ Embedded Computing→ SamsungÉ E-commerce→ AmazonÉ Graphics→Nvidia
É What about compilers?
Compiler Construction 26/40
Microsoft
É Visual Studio!
Compiler Construction 27/40
Oracle
É Java!
Compiler Construction 28/40
Intel
É ICC!
Compiler Construction 29/40
É Go, Dart, etc.
Compiler Construction 30/40
Wind River, Green Hills
É Embedded!
Mars rover, cell phones, satellites, engine controllers, jets, cameras,game consoles...
Compiler Construction 31/40
Adobe
É Photoshop contains interpreters...
Compiler Construction 32/40
The list goes on
É Mozilla’s SpiderMonkey JS EngineÉ Unreal Engine Blueprints ScriptingÉ Cadence HSPICE Circuit simulation language
Compiler Construction 33/40
What About This Class?
É You will turn in pieces of a compiler
É Compilers are large software systemsÉ This is a challenging project that will require significant
planning and developing multiple integrated components
É Former students contend it is very worthwhile
Compiler Construction 34/40
What About This Class?
É You will turn in pieces of a compilerÉ Compilers are large software systems
É This is a challenging project that will require significantplanning and developing multiple integrated components
É Former students contend it is very worthwhile
Compiler Construction 34/40
What About This Class?
É You will turn in pieces of a compilerÉ Compilers are large software systems
É This is a challenging project that will require significantplanning and developing multiple integrated components
É Former students contend it is very worthwhile
Compiler Construction 34/40
Course Expectations
É Short version: Students will deliver a compilerÉ There are 6 assignments to help you progress towards this end
1. PA1 Toposort implementation2. PA2 Lexical Analyzer3. PA3 Syntactic Analyzer4. PA4 Semantic Analyzer5. PA5 Compiler6. PA6 Optimizing compiler (extra credit)
É N.B. Assignments are time-consuming. Start Early
Compiler Construction 35/40
Course Expectations
É Short version: Students will deliver a compilerÉ There are 6 assignments to help you progress towards this end
1. PA1 Toposort implementation2. PA2 Lexical Analyzer3. PA3 Syntactic Analyzer4. PA4 Semantic Analyzer5. PA5 Compiler6. PA6 Optimizing compiler (extra credit)
É N.B. Assignments are time-consuming. Start Early
Compiler Construction 35/40
Late policy
É Assignments turned in will be penalized by 1% per hourÉ 48 hours late = 48% off that assignment
Compiler Construction 36/40
Exams
É Two midterms, one final examÉ 10% each, 15% for final
É You can have one page of notes, front and backÉ Closed test otherwise
É Let me know as early as possible about conflicts
Compiler Construction 37/40
Academic Honesty
É Please don’t cheatÉ We often use plagiarism detection software
É Ample resources available!
Compiler Construction 38/40
Academic Honesty
É Please don’t cheatÉ We often use plagiarism detection software
É Ample resources available!
Compiler Construction 38/40
Course Resources
É This course is based on Stanford’s Compilers course
Compiler Construction 39/40
Homework 1É Write a short (50-100) line program in two languages: Cool and
OCamlÉ First implementation in your favorite language (PA1c) due
1/10!É Both implementations in your language+Cool (PA1) due 1/17!
Compiler Construction 40/40
Compiler Construction 40/40
Top Related