1 - Introduction
1 - Introduction
Radu Prodan
INTRODUCTION TO COMPILER
CONSTRUCTION
05/03/2024 R. Prodan, Compiler Construction, Summer Semester 2024 1
Overview
▪ Content: introduction to compiler construction
▪ Lecture (621.400)
– Wednesday, 13:30 − 15:00, N.1.44
– https://campus.aau.at/studium/course/113655
– Slides and handouts available in Moodle before lecture
▪ Exercises (621.401)
– Wednesday, 15:15 – 16:45, S.2.69, Mario Taschwer
– https://campus.aau.at/studium/course/113656
▪ Exams
– Friday, July 12, 13:30 – 15:10, HS A
– Tuesday, September 24, 13:30 – 15:10, HS A
– Friday, November 15, 13:30 – 15:10, HS A
– Friday, January 31, 13:30 – 15:10, HS A
– Written, pen and paper, open book
▪ Target language
– Object code (machine code) Compiler
▪ Assembly language
– MOV AL, 010B
– Translates symbolic code into memory locations
▪ Lexical level
– Defined by a dictionary
– C++: list of keywords, number formats, variable names, etc.
– Regular expressions
▪ Syntactical level
– Defined by a grammar
– C++: syntax rules for loops
▪ Semantic level
– Meaning of well-defined sentences (e.g. true, false)
– Defined in prose as part of language specification
Source Code
Target Code
Generator
Optimiser
Optimiser
Semantic
Syntactic
Analyser
Analyser
Analyser
Lexical
Code
Literal Symbol Error
Table Table Handler
▪ Output
subscript-expression additive-expression
– Parse tree or
syntax tree expression [ expression ] expression + expression
– Syntax errors
identifier identifier number number
a index 4 2
▪ a[index] = 4+2
▪ Attributes
– Annotate or decorate abstract syntax tree
– E.g. declarations and data type checking
• Is variable a scalar or array ?
• Is an expression type-consistent ?
• Does dimension of an array match declaration ?
• Is an array reference in bounds ?
t = 4 + 2 Machine
a[index] = t Code
Code for
Pascal Front-end Back-end
Platform C
▪ M languages M front-ends
▪ N platforms N back-ends
▪ Examples
– Dead code elimination
– Constant folding, constant propagation
– Loop fusion, loop fission
– Code motion (move invariant code out of loops)
05/03/2024 R. Prodan, Compiler Construction, Summer Semester 2024 18
Source Code Optimiser: Example
t = 4 + 2
a[index] = t
assign-expression
subscript-expression number
▪ Constant folding integer 6
integer
t = 6
identifier identifier
a[index] = t a index
array of integer integer
▪ Constant propagation
a[index] = 6
05/03/2024 R. Prodan, Compiler Construction, Summer Semester 2024 19
Code Generator
▪ Input MOV R0, index
– Intermediate ;; store value of index in R0
representation MUL R0, 2
;; double value in R0
▪ Output MOV R1, &a
– Assembly code ;; store address of a in R1
– Machine code ADD R1, R0
;; add R0 to R1
▪ Challenges MOV *R1, 6
– Memory management ;; store 6 at address in R1
– Register allocation
– Instruction scheduling
▪ a[2*index] = 6
05/03/2024 R. Prodan, Compiler Construction, Summer Semester 2024 20
Target Code Optimiser
▪ Examples
– Replace slow instructions with faster ones
• Replace multiplication with shift instruction
• Use indexed addressing mode to perform array store
– Data pre-fetching
– Code parallelisation
▪ a[2*index] = 6
▪ Syntax Tree
▪ Symbol Table
▪ Literal Table
▪ Intermediate Code
▪ Temporary Files
Semantic Analyser
Code Generator
▪ Phases
– Lexical analysis
– Syntactic analysis
– Semantic analysis
– Intermediate code generation
– Source code optimisation
– Code generation
– Target code optimisation