32
Lecture 3. Implementing a Compiler Wei Le 2015.8

Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Lecture 3. Implementing a Compiler

Wei Le

2015.8

Page 2: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Phases and PassesI Passes: whether the compilers visit the same code (e.g. statement)

more than one time

I Single pass compiler:I Limited local optimizationI Simplicity and efficiencyI PascalI All phases of compiler interleavedI Compilation driven by parserI Scanner acts as subroutine of parser – ”give me next token”I As each phrase recognised by parser, it calls semantic routines to

process declarations, check for semantic errors and generate code

I Multi-pass compiler:I Modern compiler requires multiple passI Multi-pass allows complete separation of phases, more modular,

easier to develop, more portableI Main forms of IR: building AST, IR implies multiple passI pass one: collect information, pass two – code transformationI example points-to analysis and constant propagation

I Phases - conceptual stages, Symbol table coordinating informationbetween phases, e.g., lexical analysis, syntactical analysis

Page 3: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Phases and PassesI Passes: whether the compilers visit the same code (e.g. statement)

more than one timeI Single pass compiler:

I Limited local optimizationI Simplicity and efficiencyI PascalI All phases of compiler interleavedI Compilation driven by parserI Scanner acts as subroutine of parser – ”give me next token”I As each phrase recognised by parser, it calls semantic routines to

process declarations, check for semantic errors and generate code

I Multi-pass compiler:I Modern compiler requires multiple passI Multi-pass allows complete separation of phases, more modular,

easier to develop, more portableI Main forms of IR: building AST, IR implies multiple passI pass one: collect information, pass two – code transformationI example points-to analysis and constant propagation

I Phases - conceptual stages, Symbol table coordinating informationbetween phases, e.g., lexical analysis, syntactical analysis

Page 4: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Phases and PassesI Passes: whether the compilers visit the same code (e.g. statement)

more than one timeI Single pass compiler:

I Limited local optimizationI Simplicity and efficiencyI PascalI All phases of compiler interleavedI Compilation driven by parserI Scanner acts as subroutine of parser – ”give me next token”I As each phrase recognised by parser, it calls semantic routines to

process declarations, check for semantic errors and generate code

I Multi-pass compiler:I Modern compiler requires multiple passI Multi-pass allows complete separation of phases, more modular,

easier to develop, more portableI Main forms of IR: building AST, IR implies multiple passI pass one: collect information, pass two – code transformationI example points-to analysis and constant propagation

I Phases - conceptual stages, Symbol table coordinating informationbetween phases, e.g., lexical analysis, syntactical analysis

Page 5: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Pointer Analysis and Code Optimization

Page 6: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Current Compiler Research

Conferences:

I (PLDI) Programming Language Design and Implementation.

I (CC) Compiler construction

I (CGO) Code Generation and Optimization

Topics:

I Finding bugs in compilers

I Automatic parallelization

I Approximate computation

I Loop analysis

I Machine learning for code optimizations

I Call graph construction

Page 7: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Programming Languages

I Why so many programming languages?

I Applications, people think differentlyI Scientific computation: float pointers, arrays and operations,

parallelism (Fortune–formula translations, Julia)I Business applications: security, persistence, (SQL)I System programming: manage resources, real-time constraints

(reasoning about time) (C)

I Requirements: expressiveness, controlling the resources

Page 8: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Programming Languages

I Why so many programming languages?

I Applications, people think differentlyI Scientific computation: float pointers, arrays and operations,

parallelism (Fortune–formula translations, Julia)I Business applications: security, persistence, (SQL)I System programming: manage resources, real-time constraints

(reasoning about time) (C)

I Requirements: expressiveness, controlling the resources

Page 9: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Programming Languages

I Why new programming languages?

I CostI design the languageI implement the language: compilerI train the developers to use the language (hours, text books)

I PredictionI wildly used languages are slow to changeI easy to start a new languageI if the productivity exceed the time of training (easy to use, largely

improve the productivity – adopted to fill a void, e.g., Mobilecomputing, Internet languages)

I new languages tend to look like an old language (economic reasons,Java v.s C++)

Page 10: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Programming Languages

I Why new programming languages?

I CostI design the languageI implement the language: compilerI train the developers to use the language (hours, text books)

I PredictionI wildly used languages are slow to changeI easy to start a new languageI if the productivity exceed the time of training (easy to use, largely

improve the productivity – adopted to fill a void, e.g., Mobilecomputing, Internet languages)

I new languages tend to look like an old language (economic reasons,Java v.s C++)

Page 11: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Language Design

What makes a good programming language? (research question?)

I No universally accepted metrics for design

I A good language is the one people use? NO. Visual Basic andCOBOL are the best languages?

I Good language design is hard

Page 12: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Language Evaluation Criteria

Page 13: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Compilers

I More needed and more complex

I Driven by increasing gap between new languages and newarchitectures

I Venerable and healthy area

Page 14: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Implementing a Compiler

I Algorithms written and tuned by developers: efficient, clean code,flexibility

I Tool assisted compiler building: easy to build

I GCC/Clang: hand-written recursive descendant parser (gcc was onceused Bison), 1.5% speedup, further readinghttps://gcc.gnu.org/wiki/New_C_Parser

Page 15: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Compiler Tools

Compiler-compiler or compiler generator is a programming tool thatcreates a parser, interpreter, or compiler from some form of formaldescription of a language and machine:

Scanner generator: Lex, JLex, Flex

I Input: a declarative specification (tokens, regular expressions)

I Output: lexical analyzers

Parser generator: YACC, Bison

I Input: BNF grammars – specification of syntactic structures

I Output: Parsing programs (parser)

Page 16: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

COOL

Cool: Classroom Object Oriented Language

I Designed to be implementable in a short time

I Give a taste of implementation of modern languagesI AbstractionI Static typingI Reuse (inheritance)I Memory management ...

I But many things are left out

Page 17: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Abstraction

I Abstraction: detached from concrete details

I Abstraction necessary to build software systemsI languages and compilers: Higher-level code, few machine

dependenciesI subroutines: Abstract interface to behaviorI modules: Export interfaces; hide implementationI abstract data types: Bundle data with its operations

Page 18: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Types

I Originally, few typesI FORTRAN: scalars, arraysI LISP: every thing is dynamically typed

I Realization: Types helpI Allow the programmer to express abstractionI Allow the compiler to check against many frequent errorsI Sometimes to the point that programs are guaranteed: ”safe”

I More recentlyI Lots of interest in typesI Experiments with various forms of parameterizationI Best developed in functional programming

Page 19: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Reuse

I Reuse = exploit common patterns in software systems

I Goal: mass-produced software components

I Two popular approachesI Type parameterization: (List(int), List(double))I Classes and inheritance: C++ derived classes

I Inheritance allows:I Specialization of existing abstractionI Extension, modification, hiding behavior

Page 20: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 21: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 22: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 23: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 24: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 25: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 26: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 27: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 28: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 29: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 30: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 31: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed
Page 32: Lecture 3. Implementing a Compiler - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/3.ImplementingACompiler.pdf · COOL Cool: Classroom Object Oriented Language I Designed

Marple Scanner and Parser