Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Lecture 3. Implementing a Compiler
Wei Le
2015.8
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
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
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
Pointer Analysis and Code Optimization
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
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
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
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++)
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++)
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
Language Evaluation Criteria
Compilers
I More needed and more complex
I Driven by increasing gap between new languages and newarchitectures
I Venerable and healthy area
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
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)
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
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
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
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
Marple Scanner and Parser