430 likes | 995 Views
Compiler. Chang Chi-Chung 2008.02.26. Textbook. Compilers: Principles, Techniques, and Tools, 2/E. Alfred V. Aho , Columbia University Monica S. Lam , Stanford University Ravi Sethi , Avaya Labs Jeffrey D. Ullman , Stanford University Free books http://freecomputerbooks.com. Dragon.
E N D
Compiler Chang Chi-Chung 2008.02.26
Textbook • Compilers: Principles, Techniques, and Tools, 2/E. • Alfred V. Aho, Columbia University • Monica S. Lam, Stanford University • Ravi Sethi, Avaya Labs • Jeffrey D. Ullman, Stanford University • Free books • http://freecomputerbooks.com Dragon
Score • Midterm Examination 20% • Final Examination 25% • Quizz(2) 30% • Project 10% • Homework 10% • Participation 5%
Objectives • 瞭解編譯器的構建原理 • Know how to use compiler construction tools, such as generators of scanners and parsers • Be able to define LL(1), LR(1), and LALR(1) grammars • Be familiar with compiler analysis and optimization techniques • 建立屬於自己的編譯器
History • Early 1950’s • Mnemonic assembly languages. • Macro instructions were added later. • In the latter half of the 1950 • Fortran: scientific computation. • To take 18 man-years • Cobol: business data processing. • Lisp: symbolic computation.
Programming Languages 低階語言 machine dependent 高階語言 machine independent 自然語言 none 機器語言 組合語言 C、C++、Java … Chinese English German
Introduction to Compilers(1) Compiler Source Program Target Program • Source program must be equivalent to target program. • Definitions • Recognizer • Translator
Introduction to Compilers(2) • Translator • from one format to another • query interpreter • text formatter • silicon compiler • infix notation postfix notation: 3 + 5 - 6 * 6 ====> 3 5 + 6 6 * - • pretty printers • Computational theory • power of certain machines • the set of languages that can be recognized by this machine • Grammar • definition of this machine
Impacts on Compilers • Programming languages • Machine architecture • Language theory • Algorithms and data structures • Software engineering
Use the Compiler Techniques • Programming Languages (C、C++…) • Scripts (Javascript、bash) • Editors (syntax highlighting) • Pretty printers (e.g. Doxygen) • Static checkers (e.g. Lint and Splint) • Interpreters • Text formatters (e.g. TeX and LaTeX) • Silicon compilers (e.g. VHDL) • Query interpreters/compilers (Databases)
Input Compiler Target Program Source Program Error messages Output Compilers 編譯器 • Compilation • Translation of a program written in a source language into a semantically equivalent program written in a target language
Source Program Interpreter Output Input Error messages Interpreters 直譯器 • Interpretation • Performing the operations implied by the source program
Hybrid Source Program Translator Virtual Machine intermediate program input output
Compiler vs. Interpreter • 請自己蒐集資料比較
A Language-Processing System source program Preprocessor Try for example: gcc -v myprog.c modified source program Compiler target assembly program Assembler relocatable machine code library files relocatable object files Linker/Loader target machine code
character stream Phases of a Compiler Lexical Analyzer token stream Syntax Analyzer syntax tree Semantic Analyzer syntax tree Symbol Table Intermediate Code Generator intermediate representation Machine-Independent Code Optimizer intermediate representation Code Generator target-machine code Machine-Dependent Code Optimizer target-machine code target-machine code
= = + <id,1> + <id,1> * <id,2> * <id,2> <id,3> 60 <id,3> inttofloat 60 character stream position = initial + rate * 60 Lexical Analyzer <id,1> <=> <id,2> <+> <id,3> <*> <60> Syntax Analyzer Semantic Analyzer
Intermediate Code Generator t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 Machine-Independent Code Optimizer t1 = id3 * 60.0 id1 = id2 + t1 Code Generator LDF R2, id3 MULF R2, R2, #60.0 LDF R1, id2 ADDF R1, R1, R2 STF id1, R1 SYMBOL TABLE
The Grouping of Phases • Compiler front and back ends: • Front end: analysis (machine independent) • Back end: synthesis (machine dependent) • Compiler passes: • A collection of phases is done only once (single pass) or multiple times (multi pass) • Single pass: usually requires everything to be defined before being used in source program • Multi pass: compiler may have to keep entire program representation in memory
The Analysis-Synthesis Model of Compilation • There are two parts to compilation: • Analysis determines the operations implied by the source program which are recorded in a tree structure • Synthesis takes the tree structure and translates the operations therein into the target program
Compiler-Construction Tools • Software development tools are available to implement one or more compiler phases • Scanner generators • Parser generators • Syntax-directed translation engines • Automatic code generators • Data-flow engines
Programming Language Basic(1) • Static vs. Dynamic • Compile Time vs. Run Time • Examples • scope of declarations • memory location of variables • Environments and States environment state locations (variables) names values According to the scope rules of a language
Programming Language Basic(2) • Static Scope and Block Structure • C/C++ uses { } • Pascal uses begin, end • Explicit Access Control • C++, C#, Java provide public, protected, private • Dynamic Scope • Parameter Passing Mechanisms • caller and callee • actual parameters and formal parameters. • call by value • call by reference (call by address) • call by name • Aliasing
program TEST; beginx=1;y=2;SUB(x,x+y);print(x); end; An Example procedure SUB(i,j) begin i=i+1; print(x); i=i+j; print(i); end;
Results • Answer • Call-by-reference 2,5,5 • Call-by-value 1,5,1 • Call-by-name 2,6,6 • Call-by-value and copy-restore 1,5,5