SlideShare a Scribd company logo
CS4200 Compiler Construction
Eelco Visser
TU Delft
September 2019
Lecture 1: What is a Compiler?
!1
Course organization
- Course team 

- Study material 

- Project

Introduction
- What is a compiler?

- Why do we need compilers?

- What do we study in this course?
This Lecture
!2
Course Organization
3
Eelco Visser
!4
http://eelcovisser.org
!5
No use of electronic devices during lectures
!6
Slack #spoofax-users channel
for questions about Spoofax
=> send me email
Brightspace: Announcements
!7
Course Website
!8
https://tudelft-cs4200-2019.github.io/
CS4200-A: Compiler Construction (5 ECTS)
- Study concepts and techniques

- Lectures 

- Papers

- Homework assignments: apply to small problems

- Assessment: exams in November and January

CS4200-B: Compiler Construction Project (5 ECTS)
- Build a compiler and programming environment for a subset of Java

- Assessment: code submitted for lab assignments
CS4200: Two Courses
!9
CS4200-A: Compiler Construction
Concepts and Techniques
10
!11
CS4200-A: Homework Assignments
!12
WebLab for Homework, Exams, Grade Registration
!13
https://weblab.tudelft.nl/cs4200/2019-2020/
Sign in to WebLab using “Single Sign On for TU Delft”
!14
Enroll for Course CS4200 in WebLab
!15
https://weblab.tudelft.nl/cs4200/2019-2020/
Academic Misconduct
!16
DON’T!
CS4200-B: Compiler Construction Project
Building a compiler and IDE for MiniJava
17
Project Assignments
!18
https://tudelft-cs4200-2019.github.io
Download Special Spoofax Version for the Course!
!19
https://tudelft-cs4200-2019.github.io/documentation/spoofax.html
!20
!21
Spoofax Documentation
!22
http://www.metaborg.org
Private GitLab repository per student
- https://gitlab.ewi.tudelft.nl/CS4200-B/2019-2020/

Submit by Merge Request
Multiple submissions possible
Limited early feedback provided
Details online:
- https://tudelft-cs4200-2019.github.io/project/#submission
Assignment Submission
!23
Assignment Deadlines
!24
To pass you need to meet all of these criteria:
- Each assignment grade >= 4

- Each milestone average >= 5

- Overall average >= 6
Project grades
!25
Project Grades and Registration
!26
https://weblab.tudelft.nl/cs4200/2019-2020/assignment/20207/info
Lab on Fridays. 13:45-17:45 DW-PC 2
Walk-in hours on Wednesdays. 9:30-11:00 E4.420 (Building 28)
Contact hours
!27
https://tudelft-cs4200-2018.github.io/team/
What is a Compiler?
28
Etymology
!29
Latin
Etymology
From con- (“with, together”) + pÄ«lƍ (“ram down”).

Pronunciation
	‱	(Classical) IPA(key): /komˈpiː.loː/, [kɔmˈpiː.É«oː]

Verb
compÄ«lƍ (present inïŹnitive compÄ«lāre, perfect active compÄ«lāvÄ«, supine
compÄ«lātum); ïŹrst conjugation

	1.	I snatch together and carry oïŹ€; plunder, pillage, rob, steal.

https://en.wiktionary.org/wiki/compilo#Latin
Dictionary
!30
English
Verb
compile (third-person singular simple present compiles, present participle compiling, simple past and
past participle compiled)

	1.	 (transitive) To put together; to assemble; to make by gathering things from various sources. Samuel
Johnson compiled one of the most inïŹ‚uential dictionaries of the English language.

	2.	 (obsolete) To construct, build. quotations 

	3.	 (transitive, programming) To use a compiler to process source code and produce executable code.
After I compile this program I'll run it and see if it works.

	4.	 (intransitive, programming) To be successfully processed by a compiler into executable code. There
must be an error in my source code because it won't compile.

	5.	 (obsolete, transitive) To contain or comprise. quotations 

	6.	 (obsolete) To write; to compose.
https://en.wiktionary.org/wiki/compile
Etymology
!31
The ïŹrst compiler was written by Grace Hopper, in 1952, for the A-0
System language. The term compiler was coined by Hopper.[1][2] The A-0
functioned more as a loader or linker than the modern notion of a compiler.
https://en.wikipedia.org/wiki/History_of_compiler_construction
Compiling = Translating
!32
High-Level
Language
compiler
Low-Level
Language
A compiler translates high-level programs to low-level programs
Compiling = Translating
!33
C gcc X86
GCC translates C programs to object code for X86 (and other architectures)
Compiling = Translating
!34
Java javac
JVM
bytecode
A Java compiler translates Java programs to bytecode instructions for Java Virtual Machine
Architecture: Multi-Pass Compiler
!35
Java Type Check
JVM
bytecode
A modern compiler typically consists of sequence of stages or passes
Parse CodeGenOptimize
Intermediate Representations
!36
Java Type Check
JVM
bytecode
A compiler is a composition of a series of translations between intermediate languages
Parse CodeGenOptimize
Abstract
Syntax
Tree
Annotated
AST
Transformed
AST
Compiler Components
!37
Java Type Check
JVM
bytecode
Parse CodeGenOptimize
Abstract
Syntax
Tree
Annotated
AST
Transformed
AST
Parser
‱Reads in program text

‱Checks that it complies with the syntactic rules of the language

‱Produces an abstract syntax tree

‱Represents the underlying (syntactic) structure of the program.
Compiler Components
!38
Java Type Check
JVM
bytecode
Parse CodeGenOptimize
Abstract
Syntax
Tree
Annotated
AST
Transformed
AST
Type checker
‱Consumes an abstract syntax tree

‱Checks that the program complies with the static semantic rules of the language

‱Performs name analysis, relating uses of names to declarations of names

‱Checks that the types of arguments of operations are consistent with their speciïŹcation
Compiler Components
!39
Java Type Check
JVM
bytecode
Parse CodeGenOptimize
Abstract
Syntax
Tree
Annotated
AST
Transformed
AST
Optimizer
‱Consumes a (typed) abstract syntax tree

‱Applies transformations that improve the program in various dimensions 

‣ execution time

‣ memory consumption

‣ energy consumption.
Compiler Components
!40
Java Type Check
JVM
bytecode
Parse CodeGenOptimize
Abstract
Syntax
Tree
Annotated
AST
Transformed
AST
Code generator
‱Transforms abstract syntax tree to instructions for a particular computer architecture

‱aka instruction selection

Register allocator
‱Assigns physical registers to symbolic registers in the generated instructions
Back-EndFront-End
Compiler = Front-end + Back-End
!41
Java Type Check
JVM
bytecode
A compiler can typically be divided in a front-end (analysis) and a back-end (synthesis)
Parse CodeGenOptimize
Annotated
AST
Back-EndFront-End
Compiler = Front-end + Back-End
!42
C Type Check X86Parse CodeGenOptimizeLLVM
A compiler can typically be divided in a front-end (analysis) and a back-end (synthesis)
Back-End
Front-End
Repurposing Back-End
!43
C Type Check
X86
Repurposing: reuse a back-end for a diïŹ€erent source language
Parse
CodeGenOptimizeLLVM
Front-End
C++ Type CheckParse
Back-EndFront-End
Retargeting Compiler
!44
C Type Check X86
Retargeting: compile to diïŹ€erent hardware architecture
Parse CodeGenOptimize
LLVM
Back-End
ArmCodeGenOptimize
Front-End
C++ Type CheckParse
Compiler
- translates high-level programs to machine code for a computer

Bytecode compiler
- generates code for a virtual machine

Just-in-time compiler
- defers (some aspects of) compilation to run time

Source-to-source compiler (transpiler)
- translate between high-level languages

Cross-compiler
- runs on diïŹ€erent architecture than target architecture
Types of Compilers (1)
!45
Interpreter
- directly executes a program (although prior to execution program is
typically transformed)

Hardware compiler
- generate conïŹguration for FPGA or integrated circuit

De-compiler
- translates from low-level language to high-level language
Types of Compilers (2)
!46
What is a Compiler?
!47
Java Type Check
JVM
bytecode
Parse CodeGenOptimize
Compiler Construction = Building Variants of Java?
A bunch of components for translating programs
Why Compilers?
48
- fetch data from memory

- store data in register

- perform basic operation on data in register

- fetch instruction from memory

- update the program counter

- etc.
Programming = Instructing Computer
!49
!50
"Computational thinking is the thought processes
involved in formulating a problem and expressing its
solution(s) in such a way that a computer—human or
machine—can eïŹ€ectively carry out."
Jeanette M. Wing. Computational Thinking BeneïŹts Society. 

In Social Issues in Computing. January 10, 2014. 

http://socialissues.cs.toronto.edu/index.html
!51
Problem
Domain
Solution
Domain
Programming is expressing intent
!52
Intermediate
Language
linguistic abstraction | liNGˈgwistik abˈstrakSHən |
noun
1. a programming language construct that captures a programming design pattern
the linguistic abstraction saved a lot of programming eïŹ€ort
he introduced a linguistic abstraction for page navigation in web programming
2. the process of introducing linguistic abstractions
linguistic abstraction for name binding removed the algorithmic encoding of name resolution
Problem
Domain
Solution
Domain
From Instructions to Expressions
!53
mov &a, &c
add &b, &c
mov &a, &t1
sub &b, &t1
and &t1,&c
Source: http://sites.google.com/site/arch1utep/home/course_outline/translating-complex-expressions-into-assembly-language-using-expression-trees
c = a
c += b
t1 = a
t1 -= b
c &= t1
c = (a + b) & (a - b)
From Calling Conventions to Procedures
!54
f(e1)
calc:
push eBP ; save old frame pointer
mov eBP,eSP ; get new frame pointer
sub eSP,localsize ; reserve place for locals
.
. ; perform calculations, leave result in AX
.
mov eSP,eBP ; free space for locals
pop eBP ; restore old frame pointer
ret paramsize ; free parameter space and return
push eAX ; pass some register result
push byte[eBP+20] ; pass some memory variable (FASM/TASM syntax)
push 3 ; pass some constant
call calc ; the returned result is now in eAX
def f(x)={ ... }
http://en.wikipedia.org/wiki/Calling_convention
function deïŹnition and call in Scala
From Malloc to Garbage Collection
!55
/* Allocate space for an array with ten elements of type int. */
int *ptr = (int*)malloc(10 * sizeof (int));
if (ptr == NULL) {
/* Memory could not be allocated, the program
should handle the error here as appropriate. */
} else {
/* Allocation succeeded. Do something. */
free(ptr); /* We are done with the int objects,
and free the associated pointer. */
ptr = NULL; /* The pointer must not be used again,
unless re-assigned to using malloc again. */
}
http://en.wikipedia.org/wiki/Malloc
int [] = new int[10];
/* use it; gc will clean up (hopefully) */
Linguistic Abstraction
!56
identify pattern
use new abstraction
language A language B
design abstraction
Compiler Automates Work of Programmer
!57
Problem
Domain
Solution
Domain
General-
Purpose
Language
CompilerProgrammer
Compilers for modern high-level languages

- Reduce the gap between problem domain and program

- Support programming in terms of computational
concepts instead of machine concepts

- Abstract from hardware architecture (portability)

- Protect against a range of common programming errors
Domain-SpeciïŹc Languages
58
- Systems programming

- Embedded software

- Web programming

- Enterprise software

- Database programming

- Distributed programming

- Data analytics

- ...
Domains of Computation
!59
Problem
Domain
Solution
Domain
General-
Purpose
Language
!60
Problem
Domain
Solution
Domain
General-
Purpose
Language
“A programming language is low level when its
programs require attention to the irrelevant”
Alan J. Perlis. Epigrams on Programming.
SIGPLAN Notices, 17(9):7-13, 1982.
!61
Solution
Domain
Problem
Domain
Domain-speciïŹc language (DSL)
noun
1. a programming language that provides notation, analysis,
veriïŹcation, and optimization specialized to an application
domain
2. result of linguistic abstraction beyond general-purpose
computation
General-
Purpose
Language
Domain-
Specific
Language
Domain Analysis
- What are the features of the domain?

Language Design
- What are adequate linguistic abstractions?

- Coverage: can language express everything in the domain?

‣ often the domain is unbounded; language design is making choice what to cover

- Minimality: but not more

‣ allowing too much interferes with multi-purpose goal

Semantics
- What is the semantics of such deïŹnitions?

- How can we verify the correctness / consistency of language deïŹnitions?

Implementation
- How do we derive eïŹƒcient language implementations from such deïŹnitions?

Evaluation
- Apply to new and existing languages to determine adequacy
Language Design Methodology
!62
!63
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
!64
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
Making programming languages
is probably very expensive?
!65
General-
Purpose
Language
Making programming languages
is probably very expensive?
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
Language
Design
Compiler +
Editor (IDE)
!66
Compiler +
Editor (IDE)
Meta-Linguistic Abstraction
Language
Design
General-
Purpose
Language
Declarative
Meta
Languages
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
Language
Design
Applying compiler construction to the domain of compiler construction
!67
Compiler +
Editor (IDE)
Language
Design
General-
Purpose
Language
Declarative
Meta
Languages
Solution
Domain
Problem
Domain
General-
Purpose
Language
Language
Design
That also applies to the deïŹnition of (compilers for) general purpose languages
!68
Compiler +
Editor (IDE)
Language
Design
Declarative
Meta
Languages
!69
Language Workbench
Language Design
Syntax
DeïŹnition
Static
Semantics
Dynamic
Semantics
Transforms
Meta-DSLs
Compiler +
Editor (IDE)
!70
A Language Designer’s Workbench
Language Design
SDF3 Stratego
Consistency
Proof
NaBL2 DynSem
Responsive
Editor (IDE)
Tests
Incremental
Compiler
Syntax
DeïŹnition
Static
Semantics
Dynamic
Semantics
Transforms
Objective
- A workbench supporting design and implementation of programming languages

Approach
- Declarative multi-purpose domain-speciïŹc meta-languages

Meta-Languages
- Languages for deïŹning languages

Domain-SpeciïŹc
- Linguistic abstractions for domain of language deïŹnition (syntax, names, types, 
)

Multi-Purpose
- Derivation of interpreters, compilers, rich editors, documentation, and veriïŹcation from single
source

Declarative
- Focus on what not how; avoid bias to particular purpose in language deïŹnition
Declarative Language DeïŹnition
!71
Representation
- Standardized representation for <aspect> of programs

- Independent of speciïŹc object language

SpeciïŹcation Formalism
- Language-speciïŹc declarative rules

- Abstract from implementation concerns

Language-Independent Interpretation
- Formalism interpreted by language-independent algorithm

- Multiple interpretations for diïŹ€erent purposes

- Reuse between implementations of diïŹ€erent languages
Separation of Concerns
!72
SDF3: Syntax deïŹnition
- context-free grammars + disambiguation + constructors + templates

- derivation of parser, formatter, syntax highlighting, 


NaBL2 or Statix: Names & Types
- name resolution with scope graphs

- type checking/inference with constraints

- derivation of name & type resolution algorithm

Stratego: Program Transformation
- term rewrite rules with programmable rewriting strategies

- derivation of program transformation system

FlowSpec: Data-Flow Analysis
- extraction of control-ïŹ‚ow graph and speciïŹcation of data-ïŹ‚ow rules

- derivation of data-ïŹ‚ow analysis engine

DynSem: Dynamic Semantics
- speciïŹcation of operational (natural) semantics 

- derivation of interpreter
Meta-Languages in Spoofax Language Workbench
!73
The Spoofax Language Workbench
- Lennart C. L. Kats, Eelco Visser

- OOPSLA 2010 

A Language Designer's Workbench
- A one-stop-shop for implementation and veriïŹcation of language designs 

- Eelco Visser, Guido Wachsmuth, Andrew P. Tolmach, Pierre Neron, Vlad A. Vergu, Augusto
Passalaqua, Gabriël D. P. Konat

- Onward 2014
Literature
!74
A Taste of Compiler
Construction
75
Language DeïŹnition in Spoofax Language Workbench
!76
SDF3: Syntax
DeïŹnition
NaBL2: Static
Semantics
DynSem: Dynamic
Semantics
Programming
Environment+ + Stratego: Program
Transformation+
Calc: A Little Calculator Language
!77
rY = 0.017; // yearly interest rate
Y = 30; // number of years
P = 379,000; // principal
N = Y * 12; // number of months
c = if(rY == 0) // no interest
P / N
else
let r = rY / 12 in
let f = (1 + r) ^ N in
(r * P * f) / (f - 1);
c; // payment per month
https://github.com/MetaBorgCube/metaborg-calc
http://www.metaborg.org/en/latest/source/langdev/meta/lang/tour/index.html
Calc: Syntax DeïŹnition
!78
context-free syntax // numbers
Exp = <(<Exp>)> {bracket}
Exp.Num = NUM
Exp.Min = <-<Exp>>
Exp.Pow = <<Exp> ^ <Exp>> {right}
Exp.Mul = <<Exp> * <Exp>> {left}
Exp.Div = <<Exp> / <Exp>> {left}
Exp.Sub = <<Exp> - <Exp>> {left, prefer}
Exp.Add = <<Exp> + <Exp>> {left}
Exp.Eq = <<Exp> == <Exp>> {non-assoc}
Exp.Neq = <<Exp> != <Exp>> {non-assoc}
Exp.Gt = [[Exp] > [Exp]] {non-assoc}
Exp.Lt = [[Exp] < [Exp]] {non-assoc}
context-free syntax // variables and functions
Exp.Var = ID
Exp.Let = <
let <ID> = <Exp> in
<Exp>
>
Exp.Fun = < <ID+> . <Exp>>
Exp.App = <<Exp> <Exp>> {left}
Calc: Type System
!79
rules // numbers
[[ Num(x) ^ (s) : NumT() ]].
[[ Pow(e1, e2) ^ (s) : NumT() ]] :=
[[ e1 ^ (s) : NumT() ]],
[[ e2 ^ (s) : NumT() ]].
[[ Mul(e1, e2) ^ (s) : NumT() ]] :=
[[ e1 ^ (s) : NumT() ]],
[[ e2 ^ (s) : NumT() ]].
[[ Add(e1, e2) ^ (s) : NumT() ]] :=
[[ e1 ^ (s) : NumT() ]],
[[ e2 ^ (s) : NumT() ]].
rules // variables and functions
[[ Var(x) ^ (s) : ty ]] :=
{x} -> s, {x} |-> d, d : ty.
[[ Let(x, e1, e2) ^ (s) : ty2 ]] :=
new s_let, {x} <- s_let, {x} : ty, s_let -P-> s,
[[ e1 ^ (s) : ty ]],
[[ e2 ^ (s_let) : ty2 ]].
[[ Fun([x], e) ^ (s) : FunT(ty1, ty2) ]] :=
new s_fun, {x} <- s_fun, {x} : ty1, s_fun -P-> s,
[[ e ^ (s_fun) : ty2 ]].
[[ App(e1, e2) ^ (s) : ty_res ]] :=
[[ e1 ^ (s) : ty_fun ]],
[[ e2 ^ (s) : ty_arg ]],
FunT(ty_arg, ty_res) instOf ty_fun.
Calc: Dynamic Semantics
!80
rules // numbers
Num(n) --> NumV(parseB(n))
Pow(NumV(i), NumV(j)) --> NumV(powB(i, j))
Mul(NumV(i), NumV(j)) --> NumV(mulB(i, j))
Div(NumV(i), NumV(j)) --> NumV(divB(i, j))
Sub(NumV(i), NumV(j)) --> NumV(subB(i, j))
Add(NumV(i), NumV(j)) --> NumV(addB(i, j))
Lt(NumV(i), NumV(j)) --> BoolV(ltB(i, j))
Eq(NumV(i), NumV(j)) --> BoolV(eqB(i, j))
rules // variables and functions
E |- Var(x) --> E[x]
E |- Fun([x], e) --> ClosV(x, e, E)
E |- Let(x, v1, e2) --> v
where E {x |--> v1, E} |- e2 --> v
App(ClosV(x, e, E), v_arg) --> v
where E {x |--> v_arg, E} |- e --> v
Calc: Code Generation
!81
rules // numbers
exp-to-java : Num(v) -> $[BigDecimal.valueOf([v])]
exp-to-java :
Add(e1, e2) -> $[[je1].add([je2])]
with
<exp-to-java> e1 => je1
; <exp-to-java> e2 => je2
exp-to-java :
Sub(e1, e2) -> $[[je1].subtract([je2])]
with
<exp-to-java> e1 => je1
; <exp-to-java> e2 => je2
rules // variables and functions
exp-to-java : Var(x) -> $[[x]]
exp-to-java :
Let(x, e1, e2) -> $[(([jty]) [x] -> [je2]).apply([je1])]
with
<nabl2-get-ast-type> e1 => ty1
; <nabl2-get-ast-type> e2 => ty2
; <type-to-java> FunT(ty1, ty2) => jty
; <exp-to-java> e1 => je1
; <exp-to-java> e2 => je2
exp-to-java :
f@Fun([x], e) -> $[(([jty]) [x] -> [je])]
with
<nabl2-get-ast-type> f => ty
; <type-to-java> ty => jty
; <exp-to-java> e => je
exp-to-java:
App(e1, e2) -> $[[e1].apply([e2])]
with
<exp-to-java> e1 => je1
; <exp-to-java> e2 => je2
Lecture Language: Tiger
!82
https://github.com/MetaBorgCube/metaborg-tiger
Studying Compiler
Construction
83
The Basis
!84
Java Type Check
JVM
bytecode
Parse CodeGenOptimize
!85
Compiler construction techniques
are applicable in a wide range of
software (development) applications
SpeciïŹc
‱ Understanding a speciïŹc compiler

‱ Understanding a programming language (MiniJava)

‱ Understanding a target machine (Java Virtual Machine)

‱ Understanding a compilation scheme (MiniJava to Byte Code)

Architecture
‱ Understanding architecture of compilers

‱ Understanding (concepts of) programming languages

‱ Understanding compilation techniques

Domains
‱ Understanding (principles of) syntax deïŹnition and parsing

‱ Understanding (principles of) static semantics and type checking

‱ Understanding (principles of) dynamic semantics and interpretation/code generation

Meta
‱ Understanding meta-languages and their compilation
Levels of Understanding Compilers
!86
Syntax
‱ concrete syntax, abstract syntax

‱ context-free grammars

‱ derivations, ambiguity, disambiguation,
associativity, priority

‱ parsing, parse trees, abstract syntax trees,
terms

‱ pretty-printing

‱ parser generation

‱ syntactic editor services

Transformation
‱ rewrite rules, rewrite strategies

‱ simpliïŹcation, desugaring
Course Topics
!87
Statics
‱ static semantics and type checking

‣ name binding, name resolution, scope graphs

‣ types, type checking, type inference, subtyping

‣ uniïŹcation, constraints

‱ semantic editor services

‱ data-ïŹ‚ow analysis

‣ control-ïŹ‚ow, data-ïŹ‚ow

‣ monotone frameworks, worklist algorithm

Dynamics
‱ dynamic semantics and interpreters

‱ operational semantics, program execution

‱ virtual machines, assembly code, byte code

‱ code generation

‱ memory management, garbage collection
Q1
‱ What is a compiler? (Introduction)

‱ Syntax DeïŹnition

‱ Parsing

‱ Syntactic Editor Services

‱ Transformation

‱ Static Semantics & Name Resolution

‱ Type Checking

‱ SpeciïŹcation with Statix (or NaBL2)

‱ Constraint Resolution
Lectures (Tentative)
!88
Lectures: Thursday, 10:45 in
Lecture Hall Boole at EWI
Lectures: Wednesday, 15:45
in Lecture Hall Boole at EWI
Q2
‱ Data-Flow Analysis

‱ Monotone Frameworks

‱ Virtual Machines

‱ Code Generation

‱ Interpreters

‱ Memory Management

‱ Applications

‱ Reserve

‱ Overview
Homework Assignments
89
This award winning paper describes the design of the
Spoofax Language Workbench.
It provides an alternative architecture for
programming languages tooling from the compiler
pipeline discussed in this lecture.
Read the paper and make the homework assignments on
WebLab.
Note that the details of some of the technologies in
Spoofax have changed since the publication. The
https://doi.org/10.1145/1932682.1869497
Assignment
Read this paper in preparation for Lecture 2
http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2010-019.pdf
https://doi.org/10.1145/1932682.1869535
Next Lecture
92
Next: Syntax DeïŹnition
!93
Lecture: Friday, 13:45 in
Lecture Hall Pi at EWI
Java Type Check
JVM
bytecode
Parse CodeGenOptimize
Specification of syntax definition from which we can derive parsers

More Related Content

PDF
Compiler Construction | Lecture 17 | Beyond Compiler Construction
PDF
Compiler Construction | Lecture 6 | Introduction to Static Analysis
PDF
Compiler Construction | Lecture 1 | What is a compiler?
PDF
Declare Your Language: What is a Compiler?
PPT
Compiler Design Tutorial
PPTX
7 compiler lab
PDF
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
PPT
Introduction to programming with c,
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 1 | What is a compiler?
Declare Your Language: What is a Compiler?
Compiler Design Tutorial
7 compiler lab
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Introduction to programming with c,

What's hot (20)

PPTX
PPTX
System Programming Unit III
DOC
Lex tool manual
PPTX
4 compiler lab - Syntax Ana
PPTX
COM1407: Introduction to C Programming
PPTX
Intermediate code- generation
PPT
Overview of c++
PPTX
System Programming Unit IV
KEY
Unit 1 cd
PPTX
Introduction to c programming
PDF
Lecture 1 Compiler design , computation
PPTX
Presentation c++
PPTX
A brief introduction to C Language
PPTX
Structure of the compiler
PPTX
Compiler Engineering Lab#3
PPTX
1 cc
PPT
Introduction to Procedural Programming in C++
PDF
Introduction to c++ ppt 1
PPTX
Introduction to C programming
PPTX
OOP Poster Presentation
System Programming Unit III
Lex tool manual
4 compiler lab - Syntax Ana
COM1407: Introduction to C Programming
Intermediate code- generation
Overview of c++
System Programming Unit IV
Unit 1 cd
Introduction to c programming
Lecture 1 Compiler design , computation
Presentation c++
A brief introduction to C Language
Structure of the compiler
Compiler Engineering Lab#3
1 cc
Introduction to Procedural Programming in C++
Introduction to c++ ppt 1
Introduction to C programming
OOP Poster Presentation
Ad

Similar to CS4200 2019 Lecture 1: Introduction (20)

PPT
Chapter One
 
PPTX
Compilers.pptx
PDF
Chapter1.pdf
PPT
Compiler Design Basics
PPTX
Introduction to Compilers
PPTX
unit1_cd unit1_cd unit1_cd unit1_cd unit1_cd (1).pptx
PPT
Compiler design lessons notes from Semester
PPT
Compiler Design Basics
PDF
Compiler design
PPTX
Lecture 1 introduction to language processors
PPTX
Compiler
PDF
CS-321 Compiler Design computer engineering PPT.pdf
PPT
A basic introduction to compiler design.ppt
PPT
A basic introduction to compiler design.ppt
PPT
Chap01-Intro.ppt
PPT
compiler construvtion aaaaaaaaaaaaaaaaaads
PDF
Lecture # 1
PPTX
Compiler Construction Lecture One .pptx
PPTX
Chapter 2 Program language translation.pptx
DOC
Chapter 1 1
 
Chapter One
 
Compilers.pptx
Chapter1.pdf
Compiler Design Basics
Introduction to Compilers
unit1_cd unit1_cd unit1_cd unit1_cd unit1_cd (1).pptx
Compiler design lessons notes from Semester
Compiler Design Basics
Compiler design
Lecture 1 introduction to language processors
Compiler
CS-321 Compiler Design computer engineering PPT.pdf
A basic introduction to compiler design.ppt
A basic introduction to compiler design.ppt
Chap01-Intro.ppt
compiler construvtion aaaaaaaaaaaaaaaaaads
Lecture # 1
Compiler Construction Lecture One .pptx
Chapter 2 Program language translation.pptx
Chapter 1 1
 
Ad

More from Eelco Visser (20)

PDF
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
PDF
CS4200 2019 | Lecture 4 | Syntactic Services
PDF
CS4200 2019 | Lecture 3 | Parsing
PDF
CS4200 2019 | Lecture 2 | syntax-definition
PDF
A Direct Semantics of Declarative Disambiguation Rules
PDF
Declarative Type System Specification with Statix
PDF
Compiler Construction | Lecture 15 | Memory Management
PDF
Compiler Construction | Lecture 14 | Interpreters
PDF
Compiler Construction | Lecture 13 | Code Generation
PDF
Compiler Construction | Lecture 12 | Virtual Machines
PDF
Compiler Construction | Lecture 11 | Monotone Frameworks
PDF
Compiler Construction | Lecture 10 | Data-Flow Analysis
PDF
Compiler Construction | Lecture 9 | Constraint Resolution
PDF
Compiler Construction | Lecture 8 | Type Constraints
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
PDF
Compiler Construction | Lecture 4 | Parsing
PDF
Compiler Construction | Lecture 3 | Syntactic Editor Services
PDF
Compiler Construction | Lecture 2 | Declarative Syntax Definition
PDF
Declare Your Language: Virtual Machines & Code Generation
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 2 | syntax-definition
A Direct Semantics of Declarative Disambiguation Rules
Declarative Type System Specification with Statix
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Declare Your Language: Virtual Machines & Code Generation

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Understanding Forklifts - TECH EHS Solution
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
AI in Product Development-omnex systems
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Essential Infomation Tech presentation.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Odoo Companies in India – Driving Business Transformation.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Understanding Forklifts - TECH EHS Solution
wealthsignaloriginal-com-DS-text-... (1).pdf
L1 - Introduction to python Backend.pptx
How to Migrate SBCGlobal Email to Yahoo Easily
AI in Product Development-omnex systems
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03
Softaken Excel to vCard Converter Software.pdf
Essential Infomation Tech presentation.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Which alternative to Crystal Reports is best for small or large businesses.pdf
top salesforce developer skills in 2025.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
How to Choose the Right IT Partner for Your Business in Malaysia
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Digital Strategies for Manufacturing Companies

CS4200 2019 Lecture 1: Introduction

  • 1. CS4200 Compiler Construction Eelco Visser TU Delft September 2019 Lecture 1: What is a Compiler? !1
  • 2. Course organization - Course team - Study material - Project Introduction - What is a compiler? - Why do we need compilers? - What do we study in this course? This Lecture !2
  • 5. !5 No use of electronic devices during lectures
  • 6. !6 Slack #spoofax-users channel for questions about Spoofax => send me email
  • 9. CS4200-A: Compiler Construction (5 ECTS) - Study concepts and techniques - Lectures - Papers - Homework assignments: apply to small problems - Assessment: exams in November and January CS4200-B: Compiler Construction Project (5 ECTS) - Build a compiler and programming environment for a subset of Java - Assessment: code submitted for lab assignments CS4200: Two Courses !9
  • 11. !11
  • 13. WebLab for Homework, Exams, Grade Registration !13 https://weblab.tudelft.nl/cs4200/2019-2020/
  • 14. Sign in to WebLab using “Single Sign On for TU Delft” !14
  • 15. Enroll for Course CS4200 in WebLab !15 https://weblab.tudelft.nl/cs4200/2019-2020/
  • 17. CS4200-B: Compiler Construction Project Building a compiler and IDE for MiniJava 17
  • 19. Download Special Spoofax Version for the Course! !19 https://tudelft-cs4200-2019.github.io/documentation/spoofax.html
  • 20. !20
  • 21. !21
  • 23. Private GitLab repository per student - https://gitlab.ewi.tudelft.nl/CS4200-B/2019-2020/ Submit by Merge Request Multiple submissions possible Limited early feedback provided Details online: - https://tudelft-cs4200-2019.github.io/project/#submission Assignment Submission !23
  • 25. To pass you need to meet all of these criteria: - Each assignment grade >= 4 - Each milestone average >= 5 - Overall average >= 6 Project grades !25
  • 26. Project Grades and Registration !26 https://weblab.tudelft.nl/cs4200/2019-2020/assignment/20207/info
  • 27. Lab on Fridays. 13:45-17:45 DW-PC 2 Walk-in hours on Wednesdays. 9:30-11:00 E4.420 (Building 28) Contact hours !27 https://tudelft-cs4200-2018.github.io/team/
  • 28. What is a Compiler? 28
  • 29. Etymology !29 Latin Etymology From con- (“with, together”) + pÄ«lƍ (“ram down”). Pronunciation ‱ (Classical) IPA(key): /komˈpiː.loː/, [kɔmˈpiː.É«oː] Verb compÄ«lƍ (present inïŹnitive compÄ«lāre, perfect active compÄ«lāvÄ«, supine compÄ«lātum); ïŹrst conjugation 1. I snatch together and carry oïŹ€; plunder, pillage, rob, steal. https://en.wiktionary.org/wiki/compilo#Latin
  • 30. Dictionary !30 English Verb compile (third-person singular simple present compiles, present participle compiling, simple past and past participle compiled) 1. (transitive) To put together; to assemble; to make by gathering things from various sources. Samuel Johnson compiled one of the most inïŹ‚uential dictionaries of the English language. 2. (obsolete) To construct, build. quotations 3. (transitive, programming) To use a compiler to process source code and produce executable code. After I compile this program I'll run it and see if it works. 4. (intransitive, programming) To be successfully processed by a compiler into executable code. There must be an error in my source code because it won't compile. 5. (obsolete, transitive) To contain or comprise. quotations 6. (obsolete) To write; to compose. https://en.wiktionary.org/wiki/compile
  • 31. Etymology !31 The ïŹrst compiler was written by Grace Hopper, in 1952, for the A-0 System language. The term compiler was coined by Hopper.[1][2] The A-0 functioned more as a loader or linker than the modern notion of a compiler. https://en.wikipedia.org/wiki/History_of_compiler_construction
  • 32. Compiling = Translating !32 High-Level Language compiler Low-Level Language A compiler translates high-level programs to low-level programs
  • 33. Compiling = Translating !33 C gcc X86 GCC translates C programs to object code for X86 (and other architectures)
  • 34. Compiling = Translating !34 Java javac JVM bytecode A Java compiler translates Java programs to bytecode instructions for Java Virtual Machine
  • 35. Architecture: Multi-Pass Compiler !35 Java Type Check JVM bytecode A modern compiler typically consists of sequence of stages or passes Parse CodeGenOptimize
  • 36. Intermediate Representations !36 Java Type Check JVM bytecode A compiler is a composition of a series of translations between intermediate languages Parse CodeGenOptimize Abstract Syntax Tree Annotated AST Transformed AST
  • 37. Compiler Components !37 Java Type Check JVM bytecode Parse CodeGenOptimize Abstract Syntax Tree Annotated AST Transformed AST Parser ‱Reads in program text ‱Checks that it complies with the syntactic rules of the language ‱Produces an abstract syntax tree ‱Represents the underlying (syntactic) structure of the program.
  • 38. Compiler Components !38 Java Type Check JVM bytecode Parse CodeGenOptimize Abstract Syntax Tree Annotated AST Transformed AST Type checker ‱Consumes an abstract syntax tree ‱Checks that the program complies with the static semantic rules of the language ‱Performs name analysis, relating uses of names to declarations of names ‱Checks that the types of arguments of operations are consistent with their speciïŹcation
  • 39. Compiler Components !39 Java Type Check JVM bytecode Parse CodeGenOptimize Abstract Syntax Tree Annotated AST Transformed AST Optimizer ‱Consumes a (typed) abstract syntax tree ‱Applies transformations that improve the program in various dimensions ‣ execution time ‣ memory consumption ‣ energy consumption.
  • 40. Compiler Components !40 Java Type Check JVM bytecode Parse CodeGenOptimize Abstract Syntax Tree Annotated AST Transformed AST Code generator ‱Transforms abstract syntax tree to instructions for a particular computer architecture ‱aka instruction selection Register allocator ‱Assigns physical registers to symbolic registers in the generated instructions
  • 41. Back-EndFront-End Compiler = Front-end + Back-End !41 Java Type Check JVM bytecode A compiler can typically be divided in a front-end (analysis) and a back-end (synthesis) Parse CodeGenOptimize Annotated AST
  • 42. Back-EndFront-End Compiler = Front-end + Back-End !42 C Type Check X86Parse CodeGenOptimizeLLVM A compiler can typically be divided in a front-end (analysis) and a back-end (synthesis)
  • 43. Back-End Front-End Repurposing Back-End !43 C Type Check X86 Repurposing: reuse a back-end for a diïŹ€erent source language Parse CodeGenOptimizeLLVM Front-End C++ Type CheckParse
  • 44. Back-EndFront-End Retargeting Compiler !44 C Type Check X86 Retargeting: compile to diïŹ€erent hardware architecture Parse CodeGenOptimize LLVM Back-End ArmCodeGenOptimize Front-End C++ Type CheckParse
  • 45. Compiler - translates high-level programs to machine code for a computer Bytecode compiler - generates code for a virtual machine Just-in-time compiler - defers (some aspects of) compilation to run time Source-to-source compiler (transpiler) - translate between high-level languages Cross-compiler - runs on diïŹ€erent architecture than target architecture Types of Compilers (1) !45
  • 46. Interpreter - directly executes a program (although prior to execution program is typically transformed) Hardware compiler - generate conïŹguration for FPGA or integrated circuit De-compiler - translates from low-level language to high-level language Types of Compilers (2) !46
  • 47. What is a Compiler? !47 Java Type Check JVM bytecode Parse CodeGenOptimize Compiler Construction = Building Variants of Java? A bunch of components for translating programs
  • 49. - fetch data from memory - store data in register - perform basic operation on data in register - fetch instruction from memory - update the program counter - etc. Programming = Instructing Computer !49
  • 50. !50 "Computational thinking is the thought processes involved in formulating a problem and expressing its solution(s) in such a way that a computer—human or machine—can eïŹ€ectively carry out." Jeanette M. Wing. Computational Thinking BeneïŹts Society. In Social Issues in Computing. January 10, 2014. http://socialissues.cs.toronto.edu/index.html
  • 52. !52 Intermediate Language linguistic abstraction | liNGˈgwistik abˈstrakSHən | noun 1. a programming language construct that captures a programming design pattern the linguistic abstraction saved a lot of programming eïŹ€ort he introduced a linguistic abstraction for page navigation in web programming 2. the process of introducing linguistic abstractions linguistic abstraction for name binding removed the algorithmic encoding of name resolution Problem Domain Solution Domain
  • 53. From Instructions to Expressions !53 mov &a, &c add &b, &c mov &a, &t1 sub &b, &t1 and &t1,&c Source: http://sites.google.com/site/arch1utep/home/course_outline/translating-complex-expressions-into-assembly-language-using-expression-trees c = a c += b t1 = a t1 -= b c &= t1 c = (a + b) & (a - b)
  • 54. From Calling Conventions to Procedures !54 f(e1) calc: push eBP ; save old frame pointer mov eBP,eSP ; get new frame pointer sub eSP,localsize ; reserve place for locals . . ; perform calculations, leave result in AX . mov eSP,eBP ; free space for locals pop eBP ; restore old frame pointer ret paramsize ; free parameter space and return push eAX ; pass some register result push byte[eBP+20] ; pass some memory variable (FASM/TASM syntax) push 3 ; pass some constant call calc ; the returned result is now in eAX def f(x)={ ... } http://en.wikipedia.org/wiki/Calling_convention function deïŹnition and call in Scala
  • 55. From Malloc to Garbage Collection !55 /* Allocate space for an array with ten elements of type int. */ int *ptr = (int*)malloc(10 * sizeof (int)); if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */ } else { /* Allocation succeeded. Do something. */ free(ptr); /* We are done with the int objects, and free the associated pointer. */ ptr = NULL; /* The pointer must not be used again, unless re-assigned to using malloc again. */ } http://en.wikipedia.org/wiki/Malloc int [] = new int[10]; /* use it; gc will clean up (hopefully) */
  • 56. Linguistic Abstraction !56 identify pattern use new abstraction language A language B design abstraction
  • 57. Compiler Automates Work of Programmer !57 Problem Domain Solution Domain General- Purpose Language CompilerProgrammer Compilers for modern high-level languages - Reduce the gap between problem domain and program - Support programming in terms of computational concepts instead of machine concepts - Abstract from hardware architecture (portability) - Protect against a range of common programming errors
  • 59. - Systems programming - Embedded software - Web programming - Enterprise software - Database programming - Distributed programming - Data analytics - ... Domains of Computation !59 Problem Domain Solution Domain General- Purpose Language
  • 60. !60 Problem Domain Solution Domain General- Purpose Language “A programming language is low level when its programs require attention to the irrelevant” Alan J. Perlis. Epigrams on Programming. SIGPLAN Notices, 17(9):7-13, 1982.
  • 61. !61 Solution Domain Problem Domain Domain-speciïŹc language (DSL) noun 1. a programming language that provides notation, analysis, veriïŹcation, and optimization specialized to an application domain 2. result of linguistic abstraction beyond general-purpose computation General- Purpose Language Domain- Specific Language
  • 62. Domain Analysis - What are the features of the domain? Language Design - What are adequate linguistic abstractions? - Coverage: can language express everything in the domain? ‣ often the domain is unbounded; language design is making choice what to cover - Minimality: but not more ‣ allowing too much interferes with multi-purpose goal Semantics - What is the semantics of such deïŹnitions? - How can we verify the correctness / consistency of language deïŹnitions? Implementation - How do we derive eïŹƒcient language implementations from such deïŹnitions? Evaluation - Apply to new and existing languages to determine adequacy Language Design Methodology !62
  • 65. !65 General- Purpose Language Making programming languages is probably very expensive? Solution Domain Problem Domain General- Purpose Language Domain- Specific Language Language Design Compiler + Editor (IDE)
  • 66. !66 Compiler + Editor (IDE) Meta-Linguistic Abstraction Language Design General- Purpose Language Declarative Meta Languages Solution Domain Problem Domain General- Purpose Language Domain- Specific Language Language Design Applying compiler construction to the domain of compiler construction
  • 70. !70 A Language Designer’s Workbench Language Design SDF3 Stratego Consistency Proof NaBL2 DynSem Responsive Editor (IDE) Tests Incremental Compiler Syntax DeïŹnition Static Semantics Dynamic Semantics Transforms
  • 71. Objective - A workbench supporting design and implementation of programming languages Approach - Declarative multi-purpose domain-speciïŹc meta-languages Meta-Languages - Languages for deïŹning languages Domain-SpeciïŹc - Linguistic abstractions for domain of language deïŹnition (syntax, names, types, 
) Multi-Purpose - Derivation of interpreters, compilers, rich editors, documentation, and veriïŹcation from single source Declarative - Focus on what not how; avoid bias to particular purpose in language deïŹnition Declarative Language DeïŹnition !71
  • 72. Representation - Standardized representation for <aspect> of programs - Independent of speciïŹc object language SpeciïŹcation Formalism - Language-speciïŹc declarative rules - Abstract from implementation concerns Language-Independent Interpretation - Formalism interpreted by language-independent algorithm - Multiple interpretations for diïŹ€erent purposes - Reuse between implementations of diïŹ€erent languages Separation of Concerns !72
  • 73. SDF3: Syntax deïŹnition - context-free grammars + disambiguation + constructors + templates - derivation of parser, formatter, syntax highlighting, 
 NaBL2 or Statix: Names & Types - name resolution with scope graphs - type checking/inference with constraints - derivation of name & type resolution algorithm Stratego: Program Transformation - term rewrite rules with programmable rewriting strategies - derivation of program transformation system FlowSpec: Data-Flow Analysis - extraction of control-ïŹ‚ow graph and speciïŹcation of data-ïŹ‚ow rules - derivation of data-ïŹ‚ow analysis engine DynSem: Dynamic Semantics - speciïŹcation of operational (natural) semantics - derivation of interpreter Meta-Languages in Spoofax Language Workbench !73
  • 74. The Spoofax Language Workbench - Lennart C. L. Kats, Eelco Visser - OOPSLA 2010 A Language Designer's Workbench - A one-stop-shop for implementation and veriïŹcation of language designs - Eelco Visser, Guido Wachsmuth, Andrew P. Tolmach, Pierre Neron, Vlad A. Vergu, Augusto Passalaqua, GabriĂ«l D. P. Konat - Onward 2014 Literature !74
  • 75. A Taste of Compiler Construction 75
  • 76. Language DeïŹnition in Spoofax Language Workbench !76 SDF3: Syntax DeïŹnition NaBL2: Static Semantics DynSem: Dynamic Semantics Programming Environment+ + Stratego: Program Transformation+
  • 77. Calc: A Little Calculator Language !77 rY = 0.017; // yearly interest rate Y = 30; // number of years P = 379,000; // principal N = Y * 12; // number of months c = if(rY == 0) // no interest P / N else let r = rY / 12 in let f = (1 + r) ^ N in (r * P * f) / (f - 1); c; // payment per month https://github.com/MetaBorgCube/metaborg-calc http://www.metaborg.org/en/latest/source/langdev/meta/lang/tour/index.html
  • 78. Calc: Syntax DeïŹnition !78 context-free syntax // numbers Exp = <(<Exp>)> {bracket} Exp.Num = NUM Exp.Min = <-<Exp>> Exp.Pow = <<Exp> ^ <Exp>> {right} Exp.Mul = <<Exp> * <Exp>> {left} Exp.Div = <<Exp> / <Exp>> {left} Exp.Sub = <<Exp> - <Exp>> {left, prefer} Exp.Add = <<Exp> + <Exp>> {left} Exp.Eq = <<Exp> == <Exp>> {non-assoc} Exp.Neq = <<Exp> != <Exp>> {non-assoc} Exp.Gt = [[Exp] > [Exp]] {non-assoc} Exp.Lt = [[Exp] < [Exp]] {non-assoc} context-free syntax // variables and functions Exp.Var = ID Exp.Let = < let <ID> = <Exp> in <Exp> > Exp.Fun = < <ID+> . <Exp>> Exp.App = <<Exp> <Exp>> {left}
  • 79. Calc: Type System !79 rules // numbers [[ Num(x) ^ (s) : NumT() ]]. [[ Pow(e1, e2) ^ (s) : NumT() ]] := [[ e1 ^ (s) : NumT() ]], [[ e2 ^ (s) : NumT() ]]. [[ Mul(e1, e2) ^ (s) : NumT() ]] := [[ e1 ^ (s) : NumT() ]], [[ e2 ^ (s) : NumT() ]]. [[ Add(e1, e2) ^ (s) : NumT() ]] := [[ e1 ^ (s) : NumT() ]], [[ e2 ^ (s) : NumT() ]]. rules // variables and functions [[ Var(x) ^ (s) : ty ]] := {x} -> s, {x} |-> d, d : ty. [[ Let(x, e1, e2) ^ (s) : ty2 ]] := new s_let, {x} <- s_let, {x} : ty, s_let -P-> s, [[ e1 ^ (s) : ty ]], [[ e2 ^ (s_let) : ty2 ]]. [[ Fun([x], e) ^ (s) : FunT(ty1, ty2) ]] := new s_fun, {x} <- s_fun, {x} : ty1, s_fun -P-> s, [[ e ^ (s_fun) : ty2 ]]. [[ App(e1, e2) ^ (s) : ty_res ]] := [[ e1 ^ (s) : ty_fun ]], [[ e2 ^ (s) : ty_arg ]], FunT(ty_arg, ty_res) instOf ty_fun.
  • 80. Calc: Dynamic Semantics !80 rules // numbers Num(n) --> NumV(parseB(n)) Pow(NumV(i), NumV(j)) --> NumV(powB(i, j)) Mul(NumV(i), NumV(j)) --> NumV(mulB(i, j)) Div(NumV(i), NumV(j)) --> NumV(divB(i, j)) Sub(NumV(i), NumV(j)) --> NumV(subB(i, j)) Add(NumV(i), NumV(j)) --> NumV(addB(i, j)) Lt(NumV(i), NumV(j)) --> BoolV(ltB(i, j)) Eq(NumV(i), NumV(j)) --> BoolV(eqB(i, j)) rules // variables and functions E |- Var(x) --> E[x] E |- Fun([x], e) --> ClosV(x, e, E) E |- Let(x, v1, e2) --> v where E {x |--> v1, E} |- e2 --> v App(ClosV(x, e, E), v_arg) --> v where E {x |--> v_arg, E} |- e --> v
  • 81. Calc: Code Generation !81 rules // numbers exp-to-java : Num(v) -> $[BigDecimal.valueOf([v])] exp-to-java : Add(e1, e2) -> $[[je1].add([je2])] with <exp-to-java> e1 => je1 ; <exp-to-java> e2 => je2 exp-to-java : Sub(e1, e2) -> $[[je1].subtract([je2])] with <exp-to-java> e1 => je1 ; <exp-to-java> e2 => je2 rules // variables and functions exp-to-java : Var(x) -> $[[x]] exp-to-java : Let(x, e1, e2) -> $[(([jty]) [x] -> [je2]).apply([je1])] with <nabl2-get-ast-type> e1 => ty1 ; <nabl2-get-ast-type> e2 => ty2 ; <type-to-java> FunT(ty1, ty2) => jty ; <exp-to-java> e1 => je1 ; <exp-to-java> e2 => je2 exp-to-java : f@Fun([x], e) -> $[(([jty]) [x] -> [je])] with <nabl2-get-ast-type> f => ty ; <type-to-java> ty => jty ; <exp-to-java> e => je exp-to-java: App(e1, e2) -> $[[e1].apply([e2])] with <exp-to-java> e1 => je1 ; <exp-to-java> e2 => je2
  • 84. The Basis !84 Java Type Check JVM bytecode Parse CodeGenOptimize
  • 85. !85 Compiler construction techniques are applicable in a wide range of software (development) applications
  • 86. SpeciïŹc ‱ Understanding a speciïŹc compiler ‱ Understanding a programming language (MiniJava) ‱ Understanding a target machine (Java Virtual Machine) ‱ Understanding a compilation scheme (MiniJava to Byte Code) Architecture ‱ Understanding architecture of compilers ‱ Understanding (concepts of) programming languages ‱ Understanding compilation techniques Domains ‱ Understanding (principles of) syntax deïŹnition and parsing ‱ Understanding (principles of) static semantics and type checking ‱ Understanding (principles of) dynamic semantics and interpretation/code generation Meta ‱ Understanding meta-languages and their compilation Levels of Understanding Compilers !86
  • 87. Syntax ‱ concrete syntax, abstract syntax ‱ context-free grammars ‱ derivations, ambiguity, disambiguation, associativity, priority ‱ parsing, parse trees, abstract syntax trees, terms ‱ pretty-printing ‱ parser generation ‱ syntactic editor services Transformation ‱ rewrite rules, rewrite strategies ‱ simpliïŹcation, desugaring Course Topics !87 Statics ‱ static semantics and type checking ‣ name binding, name resolution, scope graphs ‣ types, type checking, type inference, subtyping ‣ uniïŹcation, constraints ‱ semantic editor services ‱ data-ïŹ‚ow analysis ‣ control-ïŹ‚ow, data-ïŹ‚ow ‣ monotone frameworks, worklist algorithm Dynamics ‱ dynamic semantics and interpreters ‱ operational semantics, program execution ‱ virtual machines, assembly code, byte code ‱ code generation ‱ memory management, garbage collection
  • 88. Q1 ‱ What is a compiler? (Introduction) ‱ Syntax DeïŹnition ‱ Parsing ‱ Syntactic Editor Services ‱ Transformation ‱ Static Semantics & Name Resolution ‱ Type Checking ‱ SpeciïŹcation with Statix (or NaBL2) ‱ Constraint Resolution Lectures (Tentative) !88 Lectures: Thursday, 10:45 in Lecture Hall Boole at EWI Lectures: Wednesday, 15:45 in Lecture Hall Boole at EWI Q2 ‱ Data-Flow Analysis ‱ Monotone Frameworks ‱ Virtual Machines ‱ Code Generation ‱ Interpreters ‱ Memory Management ‱ Applications ‱ Reserve ‱ Overview
  • 90. This award winning paper describes the design of the Spoofax Language Workbench. It provides an alternative architecture for programming languages tooling from the compiler pipeline discussed in this lecture. Read the paper and make the homework assignments on WebLab. Note that the details of some of the technologies in Spoofax have changed since the publication. The https://doi.org/10.1145/1932682.1869497
  • 91. Assignment Read this paper in preparation for Lecture 2 http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2010-019.pdf https://doi.org/10.1145/1932682.1869535
  • 93. Next: Syntax DeïŹnition !93 Lecture: Friday, 13:45 in Lecture Hall Pi at EWI Java Type Check JVM bytecode Parse CodeGenOptimize Specification of syntax definition from which we can derive parsers