Project Overview
COP-5621, Spring 2024
Table of Contents
Review
Questions about the last class?
Compiler Overview
The WHILE language
- Very simple, imperative language
- Assignment
- Conditionals
- While loops
- Arithmetic expressions
- Boolean expressions
- Relational operators
Example
x := 3; while x > 0 do x := x - 1
What's the final state of this program?
Example: factorial of x, \(x!\)
How would we write this?
fact := 1 while x > 0 do fact := fact * x x := x - 1
What about I/O?
x := inparam outparam := x
Features missing?
Are there any language features that cannot be written using while?
Grammars
Grammar overview
- Terminals
- Non-terminals
- Productions
- Starting symbol
What the terminals of WHILE?
Nonterminals?
Proving a string is in a language
(Diagram)
- Grammar deriviations
- Parse trees
Parsing WHILE's grammar
What's the value of x?
x = 1; if x > 10 then y = 1 else y = 2; x = 3
Ambiguous syntax
Compound statements
Replace statement sequence \(S; S\) with
\begin{align*} S ::= \texttt{begin} \, S \, (; \, S)* \, \texttt{end} \end{align*}Here, parentheses means grouping and asterisk means repetition, as in regular expressions.
Semantics
Proof rules
Using proof rules for semantics
Simple example, artihmetic
Environments and variable assignment/usage
How control-flow constructs would work, conditionals
Define semantics for a while loop
Derive the value of a few example programs
Proof rules effectively define an interpreter, a set of recrusive functions for each grammar construct
Syntax-directed interpreter
Homework Review
Environments and variable assignment
While loop semantics
Compiler project
Write an optimizing compiler that turns WHILE into machine code.
Overview
(Diagram)
Show each phase, Parser (given), While2IR, ControlFlow, Optimizer, CodeGen
Parser (Setup)
- We'll use ANTLR to generate a parser
We won't implement any type-checking
Intermediate code generation (IR)
- Translates WHILE into an
intermediate representation
(IR)
Why use an IR?
Simple example of our IR
Control-flow analysis (CFG)
- Models branching using a directed graph
Example of the graph for the simple IR example
Optimization (Local and Global)
- Peephole (in-class)
- Local
- Global
Additional optimizations include interprocedural (across functions).
Example of complex program having operations removed by optimization
dce.while
Machine code generation (CodeGen)
- Turns IR into machine code
There are more optimization opportunities at this point. We will only look at register allocation.
Example of machine code output
Illustration of the compiler
Live demo of the compiler, each phase, and their inputs and outputs
README.md illustrates commands
Support utilities (Interpreter, GraphView, CFG2IR)
Compiler infrastructure
Code architecture for each phase (Compiler.py driver, grammar/, While2IR, ControlFlow, Optimizer, CodeGen)
Development environment
Virtual machine (optional, recommended)
Compiler project grading will be done on this VM setup
One-time project setup
README.md shows the setup
git
for submission
All projects will be submitted via git to the github classroom