compiler construction - utrecht · pdf filei software: toy compilers, utility libraries,...

32
[Faculty of Science Information and Computing Sciences] Compiler Construction Stefan Holdermans Dept. of Information and Computing Sciences, Utrecht University P.O. Box 80.089, 3508 TB Utrecht, The Netherlands E-mail: [email protected] Web pages: http://people.cs.uu.nl/stefan/ November 10, 2008

Upload: phunganh

Post on 05-Mar-2018

255 views

Category:

Documents


7 download

TRANSCRIPT

[Faculty of ScienceInformation and Computing Sciences]

Compiler Construction

Stefan Holdermans

Dept. of Information and Computing Sciences, Utrecht UniversityP.O. Box 80.089, 3508 TB Utrecht, The Netherlands

E-mail: [email protected]

Web pages: http://people.cs.uu.nl/stefan/

November 10, 2008

[Faculty of ScienceInformation and Computing Sciences]

2

Agenda

I Course overview

I Administratrivia

I History

[Faculty of ScienceInformation and Computing Sciences]

3

Course overview

[Faculty of ScienceInformation and Computing Sciences]

4

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 000c 00 00 00 00 06 00 00 85 00 00 00 01 00 00 0038 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.

[Faculty of ScienceInformation and Computing Sciences]

5

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 in software development is the use of so-calleddomain-specific languages: small languages dedicated to aspecific problem domain. The implementation of suchlanguages involves constructing a compiler or interpreter.

[Faculty of ScienceInformation and Computing Sciences]

5

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 in software development is the use of so-calleddomain-specific languages: small languages dedicated to aspecific problem domain. The implementation of suchlanguages involves constructing a compiler or interpreter.

[Faculty of ScienceInformation and Computing Sciences]

5

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 in software development is the use of so-calleddomain-specific languages: small languages dedicated to aspecific problem domain. The implementation of suchlanguages involves constructing a compiler or interpreter.

[Faculty of ScienceInformation and Computing Sciences]

5

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 in software development is the use of so-calleddomain-specific languages: small languages dedicated to aspecific problem domain. The implementation of suchlanguages involves constructing a compiler or interpreter.

[Faculty of ScienceInformation and Computing Sciences]

6

Themes

I Principles of programming languages.

I Formal semantics.

I Code generation.

I Run-time systems.

I Type systems.

I Metaprogramming.

I Language-oriented programming.

I Generative programming.

I Tree-oriented programming (attribute grammars).

I Theory into practice: “let the code do the talking”.

[Faculty of ScienceInformation and Computing Sciences]

7

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 Hands-on experience in writing and extending compilersand interpreters for small languages.

I An introduction to formal semantics of programminglanguages.

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

I Some deeper delving into more advanced topics such asregister allocation and garbage collection.

[Faculty of ScienceInformation and Computing Sciences]

8

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.

[Faculty of ScienceInformation and Computing Sciences]

9

Administratrivia

[Faculty of ScienceInformation and Computing Sciences]

10

Instructor

Stefan Holdermans

Centrumgebouw Noord (CGN), room B109Office hours: Wednesdays, 1pm–3pmE-mail: [email protected]

[Faculty of ScienceInformation and Computing Sciences]

11

Registration

21 students have registered for the course.

If you have not registered yet, please do so as soon as possible.

[Faculty of ScienceInformation and Computing Sciences]

12

Web pages

“Officical” course information:

I http://www.cs.uu.nl/education/vak.

php?vak=INFOMCCO&jaar=2008

Planning, announcements, etc.:

I http://www.cs.uu.nl/docs/vakken/mcco

I http://www.cs.uu.nl/wiki/Cco

[Faculty of ScienceInformation and Computing Sciences]

13

Course form

I Lectures: 2 × 2 hours per week.

I Lab sessions: 2 × 2 hours per week, for working on bothpractical and theoretical exercises and assignments.

[Faculty of ScienceInformation and Computing Sciences]

14

Schedule

I Period runs from weeks 46 to 4.

I No classes in weeks 52 and 1 (exams andholidays).

I Lectures: Mondays, 9am–11am(BBL-420), Wednesdays, 9–11am(BBL-471).

I Lab sessions: Mondays and Wednesdays,11am-13pm (BBL-461, BBL-468).

I Exam: Monday, January 26, 2009,9am–12pm (BBL-471).

I Re-exam: Monday, March 16, 2009,9am–12pm (BBL-420).

[Faculty of ScienceInformation and Computing Sciences]

15

Grading

I Credits: 7.5 ECTS.

I Written exam (80 %).

I Assignments (20 %).

I Both components should be at least 5.

I To qualify for a second chance, the overall grade should beat least 4.

[Faculty of ScienceInformation and Computing Sciences]

16

Implementation of programming langauges

In previous years, this course was known as Implementatie vanprogrammeertalen/Implementation of programming languages(INFOIPT).

If you already obtained credits for INFOIPT, you cannot obtaincredits for the present course.

The main themes of the course roughly remained the same, butthe focus has shifted somewhat.

[Faculty of ScienceInformation and Computing Sciences]

17

Prerequisites

Participants are assumed to be familiar with the basic conceptsof imperative and functional programming.During the course, we will implement compilers and interpretersin Haskell.

Furthermore, experience with combinator-based parsing isassumed.

[Faculty of ScienceInformation and Computing Sciences]

18

Course material

I Slides: made available from the web immedidately afterthe lectures.

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

I Handouts: made available from the web.

I Exercises and assignments.

[Faculty of ScienceInformation and Computing Sciences]

19

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.

[Faculty of ScienceInformation and Computing Sciences]

20

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.

[Faculty of ScienceInformation and Computing Sciences]

21

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.

[Faculty of ScienceInformation and Computing Sciences]

22

Further reading: Mitchell

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

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

[Faculty of ScienceInformation and Computing Sciences]

23

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.

[Faculty of ScienceInformation and Computing Sciences]

24

History

[Faculty of ScienceInformation and Computing Sciences]

25

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.

[Faculty of ScienceInformation and Computing Sciences]

26

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.

[Faculty of ScienceInformation and Computing Sciences]

27

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.

[Faculty of ScienceInformation and Computing Sciences]

28

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.

[Faculty of ScienceInformation and Computing Sciences]

29

Recent years

Recent years are characterized by the emergence of newprogramming 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.