compilers
DESCRIPTION
Compilers. Book: Crafting a Compiler with C Author: Charles N. Fischer and Richard J. LeBlanc, Jr. The Benjamin/Cumming Publishing Company, Inc. Gain Score. Homework: 10% Project: 40% (Two members in a team) Lexical analysis: 10% Syntax analysis: 20% Code generation: 10% Mid Exam: 25% - PowerPoint PPT PresentationTRANSCRIPT
CompilersCompilers
Book: Crafting a Compiler with CBook: Crafting a Compiler with CAuthor: Charles N. Fischer and Richard J. LeBlanc, Jr.Author: Charles N. Fischer and Richard J. LeBlanc, Jr.
The Benjamin/Cumming Publishing Company, IncThe Benjamin/Cumming Publishing Company, Inc
Gain ScoreGain Score
Homework: 10%Homework: 10% Project: 40% (Two members in a team)Project: 40% (Two members in a team)
Lexical analysis: 10%Lexical analysis: 10% Syntax analysis: 20%Syntax analysis: 20% Code generation: 10%Code generation: 10%
Mid Exam: 25%Mid Exam: 25% Final Exam: 25%Final Exam: 25%
ContentsContents
IntroductionIntroduction A Simple CompilerA Simple Compiler Scanning – Theory and PracticeScanning – Theory and Practice Grammars and ParsingGrammars and Parsing LL(1) ParsingLL(1) Parsing LR ParsingLR Parsing Semantic ProcessingSemantic Processing Symbol TablesSymbol Tables Run-time Storage OrganizationRun-time Storage Organization
Contents (Cont’d.)Contents (Cont’d.) Processing DeclarationsProcessing Declarations Processing Expressions and Data Structure Processing Expressions and Data Structure
ReferencesReferences Translating Control StructureTranslating Control Structure Translating Procedures and FunctionsTranslating Procedures and Functions Attribute Grammars and Multipass TranslationAttribute Grammars and Multipass Translation Code Generation and Local Code Code Generation and Local Code
OptimizationOptimization Global OptimizationGlobal Optimization Parsing in the Real WorldParsing in the Real World
Chapter 1 IntroductionChapter 1 Introduction
ContentsContents Overview and HistoryOverview and History What Do Compilers Do?What Do Compilers Do? The Structure of a CompilerThe Structure of a Compiler The Syntax and Semantics of Programming The Syntax and Semantics of Programming
LanguagesLanguages Compiler Design and Programming Compiler Design and Programming
Language DesignLanguage Design Compiler ClassificationsCompiler Classifications Influences on Computer DesignInfluences on Computer Design
Overview and HistoryOverview and History
Compilers are fundamental to modern Compilers are fundamental to modern computing.computing.
They act as They act as translatorstranslators, transforming human-, transforming human-oriented oriented programming languagesprogramming languages into into computer-oriented computer-oriented machine languagesmachine languages..
Programming Programming LanguageLanguage(Source)(Source)
CompilerCompilerMachineMachineLanguageLanguage(Target)(Target)
Overview and History (Cont’d.)Overview and History (Cont’d.)
The first real compilerThe first real compiler FORTRAN compilers of the late 1950sFORTRAN compilers of the late 1950s 18 person-years to build18 person-years to build
Today, we can build a simple compiler in a Today, we can build a simple compiler in a few month.few month.
Crafting an efficient and reliable compiler is Crafting an efficient and reliable compiler is still challenging.still challenging.
Overview and History (Cont’d.)Overview and History (Cont’d.)
Compiler technology is more broadly Compiler technology is more broadly applicable and has been employed in rather applicable and has been employed in rather unexpected areas.unexpected areas. Text-formatting languages, like nroff and troff; Text-formatting languages, like nroff and troff;
preprocessor packages like eqn, tbl, picpreprocessor packages like eqn, tbl, pic Silicon compiler for the creation of VLSI circuitsSilicon compiler for the creation of VLSI circuits Command languages of OSCommand languages of OS Query languages of Database systemsQuery languages of Database systems
What Do Compilers Do?What Do Compilers Do? Compilers may be distinguished according to Compilers may be distinguished according to
the kind of target code they generate:the kind of target code they generate: Pure Machine CodePure Machine Code
Assume there is no run-time OS support.Assume there is no run-time OS support. For systems implementation or embedded systemsFor systems implementation or embedded systems Run on bare machinesRun on bare machines
Augmented Machine CodeAugmented Machine Code For hardware + OS + language-specific support routines, For hardware + OS + language-specific support routines,
e.g., I/O, math functions, storage allocation, and data e.g., I/O, math functions, storage allocation, and data transfer.transfer.
Virtual Machine CodeVirtual Machine Code JVM, P-codeJVM, P-code PortablePortable 4-times slower4-times slower Code is interpreted.Code is interpreted.
What Do Compilers Do? (Cont’d.)What Do Compilers Do? (Cont’d.)
Another way that compilers differ from one Another way that compilers differ from one another is in the format of the target machine another is in the format of the target machine code they generatecode they generate Assembly Language FormatAssembly Language Format
Simplify compilationSimplify compilation Use symbolic labels rather than calculating addressUse symbolic labels rather than calculating address Pro: good for smaller machinesPro: good for smaller machines Con: need an additional passCon: need an additional pass
What Do Compilers Do? (Cont’d.)What Do Compilers Do? (Cont’d.) Relocatable Binary FormatRelocatable Binary Format
A linkage step is requiredA linkage step is required Similar to the output of assemblerSimilar to the output of assembler Need a linking step before executionNeed a linking step before execution Good for modular compilation, cross-language Good for modular compilation, cross-language
references, and librariesreferences, and libraries Memory-Image (Load-and-Go) FormatMemory-Image (Load-and-Go) Format
FastFast Very limited linking capabilitiesVery limited linking capabilities Good for debugging (frequent changes)Good for debugging (frequent changes)
Another kind of language processor, called Another kind of language processor, called an an interpreterinterpreter, differs from a compiler in that , differs from a compiler in that it executes programs without explicitly it executes programs without explicitly performing a translationperforming a translation
Advantages and Disadvantages of an Advantages and Disadvantages of an interpreterinterpreter See page 6 & 7See page 6 & 7
What Do Compilers Do? (Cont’d.)What Do Compilers Do? (Cont’d.)
Source Program Encoding
OutputInterpreter
Data
What Do Compilers Do? (Cont’d.)What Do Compilers Do? (Cont’d.)
AdvantageAdvantage Modification to program during executionModification to program during execution
Interactive debuggingInteractive debugging Not for every language, e.g., Basic, PascalNot for every language, e.g., Basic, Pascal
Dynamic-typed languagesDynamic-typed languages Variable types may change at run time, e.g., LISP.Variable types may change at run time, e.g., LISP. Difficult to compileDifficult to compile
Better diagnosticsBetter diagnostics Source code is available.Source code is available.
Machine independenceMachine independence However, the interpreter itself must be portable.However, the interpreter itself must be portable.
What Do Compilers Do? (Cont’d.)What Do Compilers Do? (Cont’d.)
DisadvantageDisadvantage Slower execution due to repeated examinationSlower execution due to repeated examination
Dynamic (LISP): 100:1Dynamic (LISP): 100:1 Static (BASIC): 10:1Static (BASIC): 10:1
Substantial space overheadSubstantial space overhead
The Structure of a CompilerThe Structure of a Compiler
Modern compilers are syntax-directedModern compilers are syntax-directed Compilation is driven the syntactic structure of Compilation is driven the syntactic structure of
programs; i.e., actions are associated with the programs; i.e., actions are associated with the structures.structures.
Any compiler must perform two major tasksAny compiler must perform two major tasks AnalysisAnalysis of the source program of the source program SynthesisSynthesis of a machine-language program of a machine-language program
The Structure of a Compiler The Structure of a Compiler (Cont’d.)(Cont’d.)
ScannerScanner ParserParser SemanticSemanticRoutinesRoutines
CodeCodeGeneratorGenerator
OptimizerOptimizer
SourceSourceProgramProgram
(Character(CharacterStream)Stream)
TokensTokens SyntacticSyntactic
StructureStructure
IntermediateIntermediateRepresentationRepresentation
Target MachineTarget MachineCodeCode
Symbol andSymbol andAttributeAttributeTablesTables
(Used by all Phases (Used by all Phases of The Compiler)of The Compiler)
The structure of a Syntax-Directed CompilerThe structure of a Syntax-Directed Compiler
The Structure of a Compiler (Cont’d.)The Structure of a Compiler (Cont’d.) ScannerScanner
The scanner begins the analysis of the source The scanner begins the analysis of the source program by reading the input, character by program by reading the input, character by character, and grouping characters into individual character, and grouping characters into individual words and symbols (words and symbols (tokenstokens))
The The tokenstokens are encoded and then are fed to the are encoded and then are fed to the parser for syntactic analysisparser for syntactic analysis
For details, see the bottom of page 8.For details, see the bottom of page 8. Scanner generatorsScanner generators
regularexp fortokens
finite automata
as programs
lex or scangen
The Structure of a Compiler (Cont’d.)The Structure of a Compiler (Cont’d.)
ParserParser Given a formal Given a formal syntax specificationsyntax specification (typically as a (typically as a
context-freecontext-free grammar [CFG]grammar [CFG]), the parse reads ), the parse reads tokens and groups them into units as specified by tokens and groups them into units as specified by the productions of the CFG being used.the productions of the CFG being used.
While parsing, the parser verifies correct syntax, While parsing, the parser verifies correct syntax, and if a syntax error is found, it issues a suitable and if a syntax error is found, it issues a suitable diagnosticdiagnostic..
As syntactic structure is recognized, the parser As syntactic structure is recognized, the parser either calls corresponding either calls corresponding semantic routinessemantic routines directly or builds a directly or builds a syntax treesyntax tree..
grammaryacc or llgen
parser
The Structure of a Compiler (Cont’d.)The Structure of a Compiler (Cont’d.) Semantic RoutinesSemantic Routines
Perform two functionsPerform two functions Check the static semantics of each constructCheck the static semantics of each construct Do the actual translation for generating IRDo the actual translation for generating IR
The heart of a compilerThe heart of a compiler OptimizerOptimizer
The IR code generated by the semantic routines is The IR code generated by the semantic routines is analyzed and transformed into functionally analyzed and transformed into functionally equivalent but improved IR code.equivalent but improved IR code.
This phase can be very complex and slowThis phase can be very complex and slow Peephole optimizationPeephole optimization
The Structure of a Compiler (Cont’d.)The Structure of a Compiler (Cont’d.)
One-pass compilerOne-pass compiler No optimization is requiredNo optimization is required To merge code generation with semantic routines To merge code generation with semantic routines
and eliminate the use of an IRand eliminate the use of an IR Retargetable compilerRetargetable compiler
Many machine description files, e.g., gccMany machine description files, e.g., gcc Match IR against target machine patterns.Match IR against target machine patterns.
Compiler writing toolsCompiler writing tools Compiler generators or compiler-compilersCompiler generators or compiler-compilers
Lex and YaccLex and Yacc E.g., scanner and parser generatorsE.g., scanner and parser generators
Compiler Design and Programming Compiler Design and Programming Language DesignLanguage Design An interesting aspect is how programming An interesting aspect is how programming
language design and compiler design language design and compiler design influence one another.influence one another.
Programming languages that are easy to Programming languages that are easy to compiler have many advantagescompiler have many advantages See the 2See the 2ndnd paragraph of page 16. paragraph of page 16.
Compiler Design and Programming Compiler Design and Programming Language Design (Cont’d.)Language Design (Cont’d.) Languages such as Snobol and APL are Languages such as Snobol and APL are
usually considered noncompilableusually considered noncompilable What attributes must be found in a What attributes must be found in a
programming language to allow compilation?programming language to allow compilation? Can the scope and binding of each identifier Can the scope and binding of each identifier
reference be determined before execution beginsreference be determined before execution begins Can the type of object be determined before Can the type of object be determined before
execution begins?execution begins? Can existing program text be changed or added to Can existing program text be changed or added to
during execution?during execution?
Compiler ClassificationsCompiler Classifications Diagnostic compilersDiagnostic compilers
Report and repair compile-time errors.Report and repair compile-time errors. Add run-time checks, e.g., array subscripts.Add run-time checks, e.g., array subscripts. should be used in real world.should be used in real world. vs. production compilervs. production compiler
Optimizing compilersOptimizing compilers Re-targetable compilerRe-targetable compiler
Localize machine dependence.Localize machine dependence. difficult to implementdifficult to implement less efficient object codeless efficient object code
Integrated programming environmentsIntegrated programming environments integrated E-C-Dintegrated E-C-D