1 / 18

Compiler: High-level to Machine Code Translation

Learn how a compiler checks for syntax errors, translates high-level language programs to machine code, and executes them on any machine. Understand the importance of symbol tables and generating efficient assembly language code.

malindaj
Télécharger la présentation

Compiler: High-level to Machine Code Translation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 18 Compilers andLanguage Translation (S&G, ch. 9) CS 100 - Lecture 18

  2. Read S&G ch. 10(Models of Computation) CS 100 - Lecture 18

  3. Editor Not OK (error messages) Create high-level language program OK Compiler Check for syntax errors and translate to machine code Run-time system Execute the machine language program on any machine Program Development CS 100 - Lecture 18 slide < S. Levy

  4. .begin clear sum load one store count while: load count compare eleven jumpeq endwhile add sum store sum increment count jump while endwhile: out sum halt one: .data 1 eleven: .data 11 sum: .data 0 count: .data 0 .end Set sum to 0 Set count to 1 While count <= 10 do Set sum to sum + count Increment count Output sum Compiler: A Programming Language Translator Compile CS 100 - Lecture 18 slide < S. Levy

  5. Compiler: Requirements / Tasks • Recognize programming constructs (conditionals, loops) and translate them to assembly language (compare, jump) • Reject and report unrecognized programs • Keep track of symbols (sum, count) and declare each one using a .data directive • Generate efficient assembly language code CS 100 - Lecture 18 slide < S. Levy

  6. load x compare y jumpgt outx out y jump next outx: out x next: .... if x > y output x else output y Recognizing Constructs: Parsing • Could try to translate, e.g., from every possible if/else to its corresponding assembly code: Rule #18734 CS 100 - Lecture 18 slide < S. Levy

  7. load a compare b jumplt outa out b jump next outa: out a next: .... if a < b output a else output b Recognizing Constructs: Parsing (2) • Could try to translate, e.g., from every possible if/else to its corresponding assembly code: Rule #22102 • Impossible, because we’d need an infinite number of rules! CS 100 - Lecture 18 slide < S. Levy

  8. if/else output output < a b a b Recognizing Constructs: Parsing (3) • Instead, we recognize (abstract) structures, and work on their components one at a time: CS 100 - Lecture 18 slide < S. Levy

  9. Parse Generate < load a compare b a < b a b • When parsing fails, compiler reports a syntax error: Parse Unrecognized operator: << a << b Recognizing Constructs: Parsing (4) • Now we can work on smaller pieces: CS 100 - Lecture 18 slide < S. Levy

  10. BNF Grammar • A notation for expressing all the allowable statements in a language • A finite description of an infinite number of possibilities • Each distinct arrangement is shown in schematic form • Because languages are nested hierarchically, the definitions are recursive CS 100 - Lecture 18

  11. Example Grammar • assignment statement ::=variable=expression • expression ::= variable | expressionarith opvariable | (expression) • arith op ::= + | – | * | / • variable ::= x | y | z  Unrealistic! CS 100 - Lecture 18

  12. Additional Example • if statement ::=if(Boolean expression)assignment statement;else clause • Boolean expression ::= variable | expressionrelationexpression • relation ::= == | < | > • else clause ::= elseassignment statement |  CS 100 - Lecture 18

  13. Compile Set sum to 0 sum: .data 0 • Subsequent references to symbol don't need a new declaration: Compile load sum add count store sum Set sum to sum + count Keeping Track of Symbols: Symbol Tables • When a symbol is first set, compiler should declare it: CS 100 - Lecture 18 slide < S. Levy

  14. Set sum to 0 While count <= 10 do Keeping Track of Symbols: Symbol Tables (2) • Failure to set initial value should result in an error: “count” not declared • Compiler uses a symbol table (dictionary; phonebook) to keep track of values we’ve declared CS 100 - Lecture 18 slide < S. Levy

  15. a: int b: int s: String x: double • Then compiler can use symbol table to detect “type errors”: can't set int to String a = a + s; Symbol Tables • In a language like Java, symbol table would contain info about variable types: CS 100 - Lecture 18 slide < S. Levy

  16. Optimization: Generating Efficient Assembly Code • “Laundry metaphor”: Unoptimized • Load dirty clothes into washer • Run washer • Unload wet clothes from washer to basket • Load wet clothes into dryer from basket • Run dryer • Unoptimized version allows us to break down the task into simple parts • Requires less coordination between washer & dryer CS 100 - Lecture 18 slide < S. Levy

  17. Optimization: Generating Efficient Assembly Code (2) • “Laundry metaphor”: Optimized • Load dirty clothes into washer • Run washer • Load wet clothes from washer to dryer • Run dryer • Optimized version saves a step and doesn't require a basket • Requires more coordination between washer & dryer CS 100 - Lecture 18 slide < S. Levy

  18. unoptimized load a add b store a out a load a add c store a Set a to a + b Output a Set a to a + c optimized load a add b store a out a add c store a Optimization • Note coordination required between first and third steps of pseudocode CS 100 - Lecture 18 slide < S. Levy

More Related