compiler construction - cs.uu.nl · pdf filei software: toy compilers, utility libraries,...

26
[Faculty of Science Information and Computing Sciences] Compiler Construction WWW: http://www.cs.uu.nl/wiki/Cco Contact: [email protected] Edition 2016/2017

Upload: doantu

Post on 05-Mar-2018

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

Compiler ConstructionWWW: http://www.cs.uu.nl/wiki/Cco

Contact: [email protected]

Edition 2016/2017

Page 2: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

2

Course overview

Page 3: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

3

What is compiler construction about?

Programs are usually written in a high-level programminglanguage, such as Haskell:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)main = print (fibs !! 42)

To run them on hardware, they need to be translated intomachine-executable code:

ce fa ed fe 07 00 00 00 03 00 00 00 02 00 00 00

0c 00 00 00 00 06 00 00 85 00 00 00 01 00 00 00

38 00 00 00 5f 5f 50 41 47 45 5a 45 52 4f 00 ...

This translation is typically carried out by a piece of softwareknown as a compiler.

Page 4: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

4

Why study compiler construction?

Most computer scientists will never have to write a compiler fora full-scale programming language such as Haskell, Java, or C.

However, many problems studied in compiler construction showup in the implementation of other sorts of software as well;most prominently, consuming, validating, manipulating, andproducing structured data.

Moreover, compilers are typically excellent examples of welldesigned software and the utilisation of formal methods insoftware development.

A popular trend is the use of so-called domain-specificlanguages: small languages dedicated to a specific problemdomain. The implementation of such languages may involveconstructing a compiler or interpreter.

Page 5: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

4

Why study compiler construction?

Most computer scientists will never have to write a compiler fora full-scale programming language such as Haskell, Java, or C.

However, many problems studied in compiler construction showup in the implementation of other sorts of software as well;most prominently, consuming, validating, manipulating, andproducing structured data.

Moreover, compilers are typically excellent examples of welldesigned software and the utilisation of formal methods insoftware development.

A popular trend is the use of so-called domain-specificlanguages: small languages dedicated to a specific problemdomain. The implementation of such languages may involveconstructing a compiler or interpreter.

Page 6: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

4

Why study compiler construction?

Most computer scientists will never have to write a compiler fora full-scale programming language such as Haskell, Java, or C.

However, many problems studied in compiler construction showup in the implementation of other sorts of software as well;most prominently, consuming, validating, manipulating, andproducing structured data.

Moreover, compilers are typically excellent examples of welldesigned software and the utilisation of formal methods insoftware development.

A popular trend is the use of so-called domain-specificlanguages: small languages dedicated to a specific problemdomain. The implementation of such languages may involveconstructing a compiler or interpreter.

Page 7: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

4

Why study compiler construction?

Most computer scientists will never have to write a compiler fora full-scale programming language such as Haskell, Java, or C.

However, many problems studied in compiler construction showup in the implementation of other sorts of software as well;most prominently, consuming, validating, manipulating, andproducing structured data.

Moreover, compilers are typically excellent examples of welldesigned software and the utilisation of formal methods insoftware development.

A popular trend is the use of so-called domain-specificlanguages: small languages dedicated to a specific problemdomain. The implementation of such languages may involveconstructing a compiler or interpreter.

Page 8: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

5

Themes

I Principles of programming languages.

I Formal semantics.

I Code generation.

I Run-time systems.

I Type systems.

I Metaprogramming.

I Generative programming.

I Syntax-driven/tree-oriented programming (attributegrammars).

I Theory into practice: everything implemented.

Page 9: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

6

What you can expect to get out of this course

I A basic understanding of the design and implementation ofcompilers and interpreters.

I A closer look at typical programming-language constructs.

I An introduction to the specification and implementation oftype systems for programming languages.

I The analysis of first-order and higher-order languages

I Some more advanced topics (tbd).

Page 10: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

7

What this course is not

I A course on functional programming.

I A course on parsing and formal language theory.

I A course on combinator-language design.

I A course on assembly programming.

I A course on computer architecture.

I A course on logic and proof theory.

I An in-depth course on type theory.

Page 11: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

8

Administratrivia

Page 12: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

9

Course form

I Lectures: 2 × 2 hours per week.I First: focus on lab exercisesI Later: capita selecta

I Lab exercises: 3x, each 3 weeks, includingI Static analysis of first-order languagesI Static analysis of higher-order languagesI Code generation: functional language implementation

I Lab sessions: 2 hours per week, tuesdayI Lab exercises train the theoryI Organisation: peer reviewed, pairwise cooperation

I Early on in the course more lecture, less lab.

Page 13: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

10

Prerequisites

Participants are assumed to be familiar with the basic conceptsof imperative and functional programming.During the course, we will implement compilers, analyzersand/or interpreters in Haskell.

Furthermore, experience with combinator-based parsing isassumed.

Page 14: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

11

Course material

I Slides/handouts: made available on the course website

I Software: toy compilers, utility libraries,attribute-grammar system, and virtual machines.

I Reading material: a book and a few papers

I Exercises and assignments.

Page 15: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

12

Further reading: Dragon book

Alfred V. Aho, Monica S. Lam, RaviSethi, and Jeffrey D. Ullman.Compilers. Principles, Techniques, &Tools. Pearson Education, Boston,Massachusetts, 2nd edition, 2007.

Page 16: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

13

Further reading: Tiger books

Andrew W. Appel. Modern CompilerImplementation in C. CambridgeUniversity Press, Cambridge, 1998.

Andrew W. Appel. Modern CompilerImplementation in Java. CambridgeUniversity Press, Cambridge, 1998.

Andrew W. Appel. Modern CompilerImplementation in ML. CambridgeUniversity Press, Cambridge, 1998.

Page 17: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

14

Further reading: Grune et al.

Dick Grune, Henri E. Bal, Ceriel J. H.Jacobs, and Koen G. Langedoen.Modern Compiler Design. John Wiley &Sons, Chichester, 2000.

Page 18: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

15

Further reading: Mitchell

John C. Mitchell. Foundations forProgramming Languages. The MITPress, Cambridge, Massachusetts, 1996.

John C. Mitchell. Concepts inProgramming Languages. CambridgeUniversity Press, Cambridge, 2003.

Page 19: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

16

Further reading: TAPL

Benjamin C. Pierce. Types andProgramming Languages. The MITPress, Cambridge, Massachusetts, 2002.

Benjamin C. Pierce, editor. AdvancedTopics in Types and ProgrammingLanguages. The MIT Press, Cambridge,Massachusetts, 2005.

Page 20: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

17

(A Little Bit of) History

Page 21: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

18

A-0 system

The first electronic computers were programmed in machinelanguage and, later, in assembly language.

Compilation was introduced by Grace Hopper in the A-0 system(1952):

I A-0 programs were subroutines identified by numeric codes.

I Calls to routines were denoted by juxtaposing the numericcode and call arguments.

I Today, A-0 would be considered a linker.

I Eventually led to Flow-Matic, influencing the design ofCOBOL

Page 22: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

19

FORTRAN

The first compiler for a higher language was the FORTRANcompiler by John Backus and his team at IBM (1957).

Initially, the attitude towards higher languages was sceptical:they were not expected to compete, performancewise, withassembly languages.

However, the FORTRAN compiler carried out heavyoptimizations, resulting in impressively efficient code.

Moreover: a typical FORTRAN program was about 20 timessmaller than the corresponding assembly program.

Page 23: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

20

1960s and 1970s

COBOL was the first language that could be compiled tomultiple platforms (1960).

In 1962, Timothy Hart and Michael Levin created the Lisp 1.5compiler, which was the first bootstrapping compiler.

During the 1960s and 1970s, the number of proposedprogramming languages increased rapidly; focus shifted fromgeneration of fast code towards tools and techniques forimplementing compilers and interpreters.

Page 24: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

21

Compilerbau

In 1977, Niklaus Wirth wrote Compilerbau, an influentialtextbook on compiler construction, in which he presented thestepwise implementation of a compiler for PL/0.

Notable features were

I the use of a recursive descent parser for syntactic analysis,

I portable P-code as a target of code generation,

I use of T-diagrams as a means for describing thebootstrapping problem.

Page 25: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

22

Since then

Recent decades are characterized by the emergence of “new”programming paradigms (OO, functional programming).

These rely on run-time facilities that exceed the capabilities oftypical hardware architectures.

Challenge for implementors: mapping advanced high-levellanguage concepts onto native machine languages.

Page 26: Compiler Construction - cs.uu.nl · PDF fileI Software: toy compilers, utility libraries, attribute-grammar system, ... textbook on compiler construction,

[Faculty of ScienceInformation and Computing Sciences]

23

Current challenges

Major challenges include:

I domain-specific optimisation and error diagnosis

I programming support for heterogenuous systems(multicore, FPGA, GPU)

I making dependently typed languages usable