Crafting a Compiler with C Chapter 1 Introduction Teacher : Dr' Lawrence Y' Deng contact: Lawrencema - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Crafting a Compiler with C Chapter 1 Introduction Teacher : Dr' Lawrence Y' Deng contact: Lawrencema

Description:

Peephole optimization. 19 3 Semantic routines ... peephole optimization: simple and effective. Ex 1. STORE R1, A. LOAD A, R1. Ex 2. MUL R1, #1 ... – PowerPoint PPT presentation

Number of Views:233
Avg rating:3.0/5.0
Slides: 25
Provided by: 163246
Category:

less

Transcript and Presenter's Notes

Title: Crafting a Compiler with C Chapter 1 Introduction Teacher : Dr' Lawrence Y' Deng contact: Lawrencema


1
Crafting a Compiler with CChapter 1
IntroductionTeacher Dr. Lawrence Y.
Dengcontact Lawrence_at_mail.sju.edu.twGrading
Attendance 10, Midterm exam. 30, Homework 20,
Final exam. Team project 40
  • Textbook
  • Crafting a compiler with C C. N. Fischer and R.
    J. LeBlanc, Jr.1991, The Benjamin/Cummings
    Publishing Company
  • Reference
  • Compilers Principles, Techniques, and Tools,A.
    V. Aho, R. Sethi, J. D. Ullman, 2006 2th, Addison
    Weslay

2
Outlines
  • 1.1 Overview and History
  • 1.2 What Do Compilers Do?
  • 1.3 The Structure of a Compiler
  • 1.4 The Syntax and Semantics of Programming
    Languages
  • 1.5 Compiler Design and Programming Language
    Design
  • 1.6 Compiler Classifications
  • 1.7 Influences on Computer Design

3
Overview and History
  • Compilers are fundamental to modern computing.
  • They act as translators, transforming
    human-oriented programming languages into
    computer-oriented machine languages.

Programming Language (Source)
Machine Language (Target)
Compiler
4
Compilers are fundamental to modern computing
  • Natural Language Processing
  • ???????????????(????)??????????????OCR
  • ??????????????????????
  • ?????????????????
  • ????

5
Reference Technologies
  • Finite State Machine Network
  • Data Classification
  • Web Content Structure/Content/Usage Mining

6
Overview and History (Contd)
  • The first real compiler
  • FORTRAN compilers of the late 1950s, IBM
  • 18 person-years to build
  • Compiler technology is more broadly applicable
    and has been employed in rather unexpected areas.
  • Text-formatting languages, like nroff and troff
    preprocessor packages like eqn, tbl, pic
  • Silicon compiler for the creation of VLSI
    circuits
  • Command languages of OS
  • Query languages of Database systems
  • efficient? (compared with assembly program)
  • not bad, but much easier to write
  • high-level languages are feasible.
  • 18 man-year, ad hoc structure
  • Today, we can build a simple compiler in a few
    month.
  • Crafting an efficient and reliable compiler is
    still challenging.

7
1.2 What Do Compilers Do?
  • Compilers may be distinguished according to the
    kind of target code they generate
  • Pure Machine Code
  • Augmented Machine Code
  • Virtual Machine Code
  • JVM, P-code

8
What Do Compilers Do? (Contd)
  • Another way that compilers differ from one
    another is in the format of the target machine
    code they generate
  • Assembly Language Format
  • Relocatable Binary Format
  • A linkage step is required
  • Memory-Image (Load-and-Go) Format

9
What Do Compilers Do? (Contd)
  • Another kind of language processor, called an
    interpreter, differs from a compiler in that it
    executes programs without explicitly performing a
    translation

Output
Interpreter
Source Program Encoding
Data
10
1.3 The Structure of a Compiler
  • Any compiler must perform two major tasks
  • Analysis of the source program
  • Synthesis of a machine-language program

11
(No Transcript)
12
The Structure of a Compiler (Contd)
Source Program
Tokens
Syntactic
Scanner
Parser
Semantic Routines
Structure
(Character Stream)
Intermediate Representation
Optimizer
Symbol and Attribute Tables
(Used by all Phases of The Compiler)
Code Generator
The structure of a Syntax-Directed Compiler
Target Machine Code
13
(No Transcript)
14
the structure of a compiler
source program
ab20
scanner
ididintint
parser
syntax trees
semantic routine
symbol table
1. type are correct load b mul 2 add 0 sta a
optimizer
load b shL 1 sta a
code generator
oo1F 2b4A 309F
target code
15
The Structure of a Compiler (Contd)
  • Scanner
  • The scanner begins the analysis of the source
    program by reading the input, character by
    character, and grouping characters into
    individual words and symbols (tokens)
  • The tokens are encoded and then are fed to the
    parser for syntactic analysis
  • For details, see the bottom of page 8.
  • Scanner generators

16
The Structure of a Compiler (Contd)
  • Parser
  • Given a formal syntax specification (typically as
    a context-free CFG grammar), the parse reads
    tokens and groups them into units as specified by
    the productions of the CFG being used.
  • While parsing, the parser verifies correct
    syntax, and if a syntax error is found, it issues
    a suitable diagnostic.
  • As syntactic structure is recognized, the parser
    either calls corresponding semantic routines
    directly or builds a syntax tree.

17
  • lt2gt Parser identify syntactic structure
  • - Syntax is specified by grammars,e.g.,
  • IfStmt if ( Exp ) then Stmt
  • Exp
  • - Parsers can be generated from grammars.

yacc or llgen
grammar
parser
- Error-repair, e.g., ")" is missing if ( a gt 3
then - Parsers may build a syntax tree or it may
call semantic routines directly.
18
The Structure of a Compiler (Contd)
  • Semantic Routines
  • Perform two functions
  • Check the static semantics of each construct
  • Do the actual translation
  • The heart of a compiler
  • Optimizer
  • The IR code generated by the semantic routines is
    analyzed and transformed into functionally
    equivalent but improved IR code.
  • This phase can be very complex and slow
  • Peephole optimization

19
  • lt3gt Semantic routines
  • - Check that variables are defined, that types
    are correct, , etc. Ex.
  • x x true
  • - Generate IR (3-addr code), e.g.,
  • ADD A, B, C
  • - hand-coded
  • - associated with grammar rules
  • - formalized as attribute grammars (AG)

20
  • peephole optimization
  • simple and effective
  • Ex 1. STORE R1, A
  • LOAD A, R1
  • Ex 2. MUL R1, 1
  • Ex 3. ADD R1, 0
  • These innocent code can be actually generated by
    a naive compiler.

21
The Structure of a Compiler (Contd)
  • One-pass compiler
  • No optimization is required
  • To merge code generation with semantic routines
    and eliminate the use of an IR
  • Compiler writing tools
  • Compiler generators or compiler-compilers
  • E.g. scanner and parser generators

22
1.4 Compiler Design and Programming Language
Design
  • An interesting aspect is how programming language
    design and compiler design influence one another.
  • Programming languages that are easy to compiler
    have many advantages
  • See the 2nd paragraph of page 16.

23
1.5 Compiler Design and Programming Language
Design (Contd)
  • Languages such as Snobol and APL are usually
    considered noncompilable
  • What attributes must be found in a programming
    language to allow compilation?
  • Can the scope and binding of each identifier
    reference be determined before execution begins
  • Can the type of object be determined before
    execution begins?
  • Can existing program text be changed or added to
    during execution?

24
1.6 Compiler Classifications
  • Diagnostic compilers
  • Report and repair compile-time errors.
  • - Add run-time checks, e.g., array subscripts.
  • - should be used in real world.
  • - vs. production compiler
  • Optimizing compilers
  • - for production use
  • - 'optimal' is suspicious because
  • 1. theoretically undecidable
  • 2. practically infeasible
  • - Perform a lot of transformations with unknown
    effects.
  • Ex. place data in registers
  • fast access
  • slow procedure call
  • Retargetable compiler
  • Localize machine dependence.
  • - difficult to implement
  • - less efficient object code
Write a Comment
User Comments (0)
About PowerShow.com