Скачать презентацию Lecture 02 Compiler Overview Kwangman Man http compiler Скачать презентацию Lecture 02 Compiler Overview Kwangman Man http compiler

fa3b68fe1a74d3204ca1412926118b8d.ppt

  • Количество слайдов: 28

Lecture 02: Compiler Overview Kwangman Man (http: //compiler. sj. ac. kr/, kkman@sangji. ac. kr) Lecture 02: Compiler Overview Kwangman Man (http: //compiler. sj. ac. kr/, [email protected] ac. kr) Sang. Ji University Fall 2015

1. 1 Translation l What is a compiler? – A program that translates an 1. 1 Translation l What is a compiler? – A program that translates an executable program in one language into an executable program in another language. – The compiler should improve the program, in some way. • Optimizations, … Source code Compiler Machine code Errors Lecture 02: Compiler Overview, 2015. 2

l What is an interpreter? – A program that reads an executable program and l What is an interpreter? – A program that reads an executable program and produces the results of executing that program. l Java is compiled to bytecodes(code for the Java VM) – which are then interpreted or a hybrid strategy is used – Just-in-time. I(JIT) compilation l Perl, Scheme, APL, … Lecture 02: Compiler Overview, 2015 3

Taking a Broader View l Compiler Technology = Off-Line Processing – Goals: improved performance Taking a Broader View l Compiler Technology = Off-Line Processing – Goals: improved performance and language usability. • Making it practical to use the full power of the language – Trade-off • preprocessing time versus execution time (or space) – Rule • performance of both compiler and application must be acceptable to the end user Lecture 02: Compiler Overview, 2015 4

1. 2 Why Study Compilation? l Compilers are important system software components – intimately 1. 2 Why Study Compilation? l Compilers are important system software components – intimately interconnected with architecture, systems, programming methodology, and language design. l Compilers include many applications of theory to practice – Scanning, parsing, static analysis, instruction selection l Writing a compiler exposes practical algorithmic & engineering issues – Approximating hard problems; efficiency & scalability Lecture 02: Compiler Overview, 2015 5

Intrinsic interest l Compiler construction involves ideas from many different parts of computer science Intrinsic interest l Compiler construction involves ideas from many different parts of computer science Artificial intelligence Algorithms Theory Systems Architecture Lecture 02: Compiler Overview, 2015. Greedy algorithms Heuristic search techniques Graph algorithms, union-find Dynamic programming DFAs & PDAs, pattern matching Fixed-point algorithms Allocation & naming, Synchronization, locality Pipeline & hierarchy management Instruction set use 6

Intrinsic merit Ø Compiler construction poses challenging and interesting problems: – Compilers must do Intrinsic merit Ø Compiler construction poses challenging and interesting problems: – Compilers must do a lot but also run fast – Compilers have primary responsibility for run-time performance – Compilers are responsible for making it acceptable to use the full power of the programming language – Computer architects perpetually create new challenges for the compiler by building more complex machines – Compilers must hide that complexity from the programmer – Success requires mastery of complex interactions Lecture 02: Compiler Overview, 2015 7

The View from 35, 000 Feet Lecture 02: Compiler Overview, 2015 The View from 35, 000 Feet Lecture 02: Compiler Overview, 2015

1. 3 High-level View of a Compiler l Implications – – Must recognize legal 1. 3 High-level View of a Compiler l Implications – – Must recognize legal (and illegal) programs generate correct code manage storage of all variables (and code) agree with OS & linker on format for object code Source code Compiler Machine code Errors Lecture 02: Compiler Overview, 2015 9

Traditional Two-pass Compiler l Implications – – Use an Intermediate Representation (IR) Front end Traditional Two-pass Compiler l Implications – – Use an Intermediate Representation (IR) Front end maps legal source code into IR Back end maps IR into target machine code Admits multiple front ends & multiple passes Source code Front End IR Back End Machine code Errors Lecture 02: Compiler Overview, 2015 10

A Common Fallacy l Can we build n x m compilers with n+m components A Common Fallacy l Can we build n x m compilers with n+m components ? – Must encode all language specific knowledge in each front end – Must encode all features in a single IR – Must encode all target specific knowledge in each back end Fortran Front end Scheme Front end Java Front end Smalltalk Front end Lecture 02: Compiler Overview, 2015 Back end Target 1 Back end Target 2 Back end Target 3 11

Fortran Front end Scheme Front end Java Front end Smalltalk Front end Lecture 02: Fortran Front end Scheme Front end Java Front end Smalltalk Front end Lecture 02: Compiler Overview, 2015 Back end IR Target 1 Back end Target 2 Back end Target 3 12

The Front End l Responsibilities – – – Recognize legal (& illegal) programs Report The Front End l Responsibilities – – – Recognize legal (& illegal) programs Report errors in a useful way Produce IR & preliminary storage map Shape the code for the back end, IR Much of front end construction can be automated Source code Scanner tokens IR Parser Errors Lecture 02: Compiler Overview, 2015 13

Scanner, Lexical analyser l Scanner – Maps character stream into tokens—the basic unit of Scanner, Lexical analyser l Scanner – Maps character stream into tokens—the basic unit of syntax – Produces pairs • x = x + y ; becomes = + ; • word lexeme, part of speech token • In casual speech, we call the pair a token – Typical tokens include number, identifier, +, –, new, while, if – eliminates white space including comments – Speed is important Operators Constants: 0, 1, 2, 3, … Variables, Names Keywords Lecture 02: Compiler Overview, 2015 14

l Parser – Recognizes context-free syntax & reports errors – Guides context-sensitive (“semantic”) analysis, l Parser – Recognizes context-free syntax & reports errors – Guides context-sensitive (“semantic”) analysis, type checking – Builds IR for source program Hand-coded parsers are fairly easy to build Most books advocate using automatic parser generators Lecture 02: Compiler Overview, 2015 15

l Context-free syntax is specified with a grammar Sheep. Noise baa | baa – l Context-free syntax is specified with a grammar Sheep. Noise baa | baa – written in a variant of Backus–Naur Form(BNF) l Formally, a grammar G = (S, N, T, P) – – S N T P is the start symbol is a set of non-terminal symbols is a set of terminal symbols or words is a set of productions or rewrite rules(P : N N T ) (Example due to Dr. Scott K. Warren) Lecture 02: Compiler Overview, 2015 16

l Context-free syntax can be put to better use – defines simple expressions with l Context-free syntax can be put to better use – defines simple expressions with addition & subtraction over “number” and “id” – called “context-free grammars”, abbreviated CFG. 1. goal expr 2. expr op term 3. | term 4. term number 5. | id 6. op 7. + | S = goal T = { number, id, +, - } N = { goal, expr, term, op } P = { 1, 2, 3, 4, 5, 6, 7} - Lecture 02: Compiler Overview, 2015 17

l Given a CFG, – can derive sentences by repeated substitution – To recognize l Given a CFG, – can derive sentences by repeated substitution – To recognize a valid sentence in some CFG, we reverse this process and build up a parse Production 1 2 5 7 2 4 6 3 5 Result goal expr op term expr op y expr - y expr op term - y expr op 2 - y expr + 2 - y term + 2 - y x + 2 - y Lecture 02: Compiler Overview, 2015 18

l A parse can be represented by a tree, parse tree or syntax tree l A parse can be represented by a tree, parse tree or syntax tree – x + 2 - y – contains a lot of unneeded informations goal expr op term + term - Lecture 02: Compiler Overview, 2015 19

l Compilers often use an abstract syntax tree, AST – is much more concise l Compilers often use an abstract syntax tree, AST – is much more concise ASTs are one kind of intermediate representation(IR). – The AST summarizes grammatical structure, without including detail about the derivation. - + Lecture 02: Compiler Overview, 2015 20

The Back End l Responsibilities – – Translate IR into target machine code Choose The Back End l Responsibilities – – Translate IR into target machine code Choose instructions to implement each IR operation Decide which value to keep in registers Ensure conformance with system interfaces Automation has been less successful in the back end IR Instruction Selection IR Register Allocation IR Machine code Instruction Scheduling Errors Lecture 02: Compiler Overview, 2015 21

l Instruction Selection – Produce fast, compact code – Take advantage of target features l Instruction Selection – Produce fast, compact code – Take advantage of target features such as addressing modes – Usually viewed as a pattern matching problem • ad hoc methods, pattern matching, dynamic programming l This was the problem of the future in 1978 – Spurred by transition from PDP-11 to VAX-11 – Orthogonality of RISC simplified this problem Lecture 02: Compiler Overview, 2015 22

l Register Allocation – – Have each value in a register when it is l Register Allocation – – Have each value in a register when it is used Manage a limited set of resources Can change instruction choices & insert LOADs & STOREs Optimal allocation is NP-Complete(1 or k registers) Compilers approximate solutions to NP-Complete problems Lecture 02: Compiler Overview, 2015 23

l Instruction Scheduling – Avoid hardware stalls and interlocks – Use all functional units l Instruction Scheduling – Avoid hardware stalls and interlocks – Use all functional units productively – Can increase lifetime of variables(changing the allocation) Optimal scheduling is NP-Complete in nearly all cases Heuristic techniques are well developed Lecture 02: Compiler Overview, 2015 24

Traditional Three-pass Compiler l Code Improvement (or Optimization) – Analyzes IR and rewrites (or Traditional Three-pass Compiler l Code Improvement (or Optimization) – Analyzes IR and rewrites (or transforms) IR – Primary goal is to reduce running time of the compiled code – May also improve space, power consumption, … l Must preserve “meaning” of the code – Measured by values of named variables Source Code Front End IR Middle End IR Back End Machine code Errors Lecture 02: Compiler Overview, 2015 25

The Optimizer (or Middle End) l Typical Transformations – – – Discover & propagate The Optimizer (or Middle End) l Typical Transformations – – – Discover & propagate some constant value Move a computation to a less frequently executed place Specialize some computation based on context Discover a redundant computation & remove it Remove useless or unreachable code Encode an idiom in some particularly efficient form IR O pt 1 IR O pt 2 IR O pt 3 IR. . . O pt n IR Errors Lecture 02: Compiler Overview, 2015 26

Modern Restructuring Compiler l Typical Restructuring Transformations: – – – Blocking for memory hierarchy Modern Restructuring Compiler l Typical Restructuring Transformations: – – – Blocking for memory hierarchy and register reuse Vectorization Parallelization All based on dependence Also full and partial inlining Source Code Front End HL HL AST Restructure AST r IR Gen IR Opt + Back End Machine code Errors Lecture 02: Compiler Overview, 2015 27

Role of the Run-time System l Memory management services – Allocate • In the Role of the Run-time System l Memory management services – Allocate • In the heap or in an activation record (stack frame) – Deallocate – Collect garbage l Run-time type checking l Error processing l Interface to the operating system – Input and output l Support of parallelism – Parallel thread initiation – Communication and synchronization Lecture 02: Compiler Overview, 2015 28