Lecture 1 - CSC 303
Lecture 1 - CSC 303
CONSTRUCTIO
N
(CSC 303)
LECTURE 1
1
Course Objectives
This course introduces students to the
fundamental principles and techniques
involved in designing and implementing
compilers.
Topics include lexical analysis, syntax
analysis, semantic analysis, intermediate
code generation, optimization, and code
generation.
Grading:
Assignment – 15%
Mid-semester exam – 15%
Exam – 70%
A.O. AGBEYANGI - CHRISLAND
UNIVERSITY 4
Compilers,
Interpreters …
PS — INTRODUCTION 5 1.5
Compilers and
Interpreters
“Compilation”
◦ Translation of a program written in a source language into a semantically
equivalent program written in a target language
◦ Oversimplified view:
Input
Source Target
Compiler
Program Program
“Interpretation”
◦ Performing the operations implied by the source program
◦ Oversimplified view:
Source
Program
Interpreter Output
Input
Error messages
Preprocessor
Source Program
Compiler
Target Assembly Program
Assembler
Relocatable Object Code
Linker Libraries and
Relocatable Object Files
Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
And symbol table with names
Parser (performs syntax analysis based Parse tree or abstract syntax tree ;
|
on the grammar of the programming =
language) / \
A +
/ \
B C
13
Compiler-Construction
Tools
14
What qualities are
important in a compiler?
1. Correct code
2. Output runs fast
3. Compiler runs fast
4. Compile time proportional to program size
5. Support for separate compilation
6. Good diagnostics for syntax errors
7. Works well with the debugger
8. Good diagnostics for flow anomalies
9. Cross language calls
10. Consistent, predictable optimization
15
A bit of history
1957: First complete compiler for FORTRAN by John Backus and team
16
A compiler was originally a program that
“compiled” subroutines [a link-loader].
When in 1954 the combination “algebraic
compiler” came into use, or rather into
misuse, the meaning of the term had already
shifted into the present one.
— Bauer and Eickel [1975]
17
Abstract view
18
Traditional two pass
compiler
19
Front end
21
Parser
24
Parse trees
© Oscar Nierstrasz 25
Abstract syntax trees
26
Roadmap
Overview
Front end
Back end
Multi-pass compilers
27
Back end
28
Instruction selection
29
Register allocation
Overview
Front end
Back end
Multi-pass compilers
31
Traditional three-pass
compiler
32
Optimizer (middle
end)
Modern optimizers are usually built as a set of passes
34
Compiler phases
Lex Break source file into individual words, or tokens
Parse Analyse the phrase structure of program
Parsing Actions Build a piece of abstract syntax tree for each phrase
Determine what each phrase means, relate uses of variables to their
Semantic Analysis
definitions, check types of expressions, request translation of each phrase
Place variables, function parameters, etc., into activation records (stack
Frame Layout
frames) in a machine-dependent way
Produce intermediate representation trees (IR trees), a notation that is not tied
Translate
to any particular source language or target machine
Hoist side effects out of expressions, and clean up conditional branches, for
Canonicalize
convenience of later phases
Group IR-tree nodes into clumps that correspond to actions of target-machine
Instruction Selection
instructions
Analyse sequence of instructions into control flow graph showing all possible
Control Flow Analysis
flows of control program might follow when it runs
Gather information about flow of data through variables of program; e.g.,
Data Flow Analysis liveness analysis calculates places where each variable holds a still-needed
(live) value
Choose registers for variables and temporary values; variables not
Register Allocation
simultaneously live can share same register
Code Emission Replace temporary names in each machine instruction with registers
35
A straight-line programming language
(no loops or conditionals):
Stm Stm ; Stm CompoundStm
Stm id := Exp AssignStm
Stm print ( ExpList ) PrintStm
Exp id IdExp
Exp num NumExp
Exp Exp Binop Exp OpExp
Exp ( Stm , Exp ) EseqExp
ExpList Exp , ExpList PairExpList
ExpList Exp LastExpList
Binop + Plus
Binop Minus
Binop Times
Binop / Div
a := 5 + 3; b := (print(a,a—1),10a); print(b) 8 7
prints 80
36
Tree representation
a := 5 + 3; b := (print(a,a—1),10a); print(b)
37
SUMMARY: What you should know!
What is the difference between a compiler and an interpreter?
What are the important qualities of compilers?
Why are compilers commonly split into multiple passes?
What are the typical responsibilities of the different parts of a
modern compiler?
How are context-free grammars specified?
What is “abstract” about an abstract syntax tree?
What is an intermediate representation and what is it for?
Why is optimisation a separate activity?
Is Java compiled or interpreted? What about Python? Ruby? PHP? Are
you sure?
What are the key differences between modern compilers and
compilers written in the 1970s?
Why is it hard for compilers to generate good error messages?
Discuss in detail, the differences between one-pass and a multi-pass
compiler
38
Assignment 1
39
Questions
40