compiler construction · why study compiler construction? most computer scientists will never have...

26
[Faculty of Science Information and Computing Sciences] Compiler Construction WWW: http://www.cs.uu.nl/docs/vakken/mcco/ Contact: [email protected] Edition 2018/2019

Upload: others

Post on 21-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

  • [Faculty of ScienceInformation and Computing Sciences]

    Compiler ConstructionWWW: http://www.cs.uu.nl/docs/vakken/mcco/

    Contact: [email protected]

    Edition 2018/2019

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

  • [Faculty of ScienceInformation and Computing Sciences]

    2

    Course overview

  • [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.

  • [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.

  • [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.

  • [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.

  • [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.

  • [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.

  • [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).

  • [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.

  • [Faculty of ScienceInformation and Computing Sciences]

    8

    Administratrivia

  • [Faculty of ScienceInformation and Computing Sciences]

    9

    Course form

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

    I Lab exercises: 3x, includingI Attribute grammars for syntax-directed computing (20%)I Static analysis of first-order languages (40%)I Static analysis of higher-order languages (40%)

    I Lab sessions: (about) 2 hours per weekI Lab exercises train the theoryI Organisation: pairwise cooperation

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

  • [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.

  • [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.

  • [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.

  • [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.

  • [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.

  • [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.

  • [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.

  • [Faculty of ScienceInformation and Computing Sciences]

    17

    (A Little Bit of) History

  • [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

  • [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.

  • [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.

  • [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.

  • [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.

    Advent of wholesale concurrent and distributed programming

    Gradual typing, JIT, language workbenches, resource awareness,certified compilation, incremental analysis and compilation

  • [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