COMP 181 Compilers

COMP 181 Compilers

COMP 181 Compilers Lecture 1 The view from 35000 feet Monday, Sept. 12, 2005 Introduction What is this artifact? The Rosetta Stone Significance? Same document in Greek and Egyptian hieroglyphics Am I in the wrong class? No, compilers are translators Tufts University Computer Science 2 1 Language translation How does translation work? Meaning Semantics Semantics Syntax Syntax High-level language Assembly/machine code Tufts University Computer Science 3 Compiler overview Responsibilities : Recognize legal programs Generate correct code Manage hardware resources Registers Memory Cooperate with OS System calls Object code, virtual memory layout Tufts University Computer Science 4 2 Traditional Two-pass Compiler Source Front IR Back Machine code End End code Errors 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 (better code ) Typically, front end is O(n) or O(n log n), while back end is NPC Tufts University Computer Science 5 A Common Fallacy Front Back Fortran Target 1 end end Front Scheme end Back Target 2 end Front Java end Front Back Target 3 Smalltalk end end Can we build n*m compilers with n+m components? Must encode all language knowledge in each front end Must encode all features in a single IR Must encode all target knowledge in each back end Limited success in systems with very low-level IR s Tufts University Computer Science 6 3 The Front End Source tokens IR Scanner Parser code Errors Responsibilities Recognize legal (& illegal) programs Report errors in a useful way Produce IR & preliminary storage map Shape the code for the back end Much of front end construction can be automated Tufts University Computer Science 7 The Front End Source tokens IR Scanner Parser code Errors Scanner Maps character stream into words—the basic unit of syntax Produces pairs — a word & its part of speech x = x + y; becomes <id ,x> = < id ,x> + < id ,y> ; word ≅ lexeme, part of speech ≅ token type In casual speech, we call the pair a token Typical tokens include number , identifier , +, –, while , if Scanner eliminates white space ( including comments ) Speed is important Tufts University Computer Science 8 4 The Front End Source tokens IR Scanner Parser code Errors Parser Recognizes context-free syntax & reports errors Guides context-sensitive (“semantic”) analysis (type checking ) Builds IR for source program Most parsers built using automated tools Hand-coded parsers not too difficult to build Tufts University Computer Science 9 The Front End Context-free syntax is specified with a grammar sentence → subject verb object subject → noun | proper-noun It is written in a variant of Backus–Naur Form (BNF) Formally, a grammar G = (S,N,T,P) N is a set of non-terminal symbols T is a set of terminal symbols or words S is the start symbol – chosen from N P is a set of productions or rewrite rules (P : N → N ∪T ) Tufts University Computer Science 10 5 The Front End Application to programming languages: 1. goal → expr 2. expr → expr op term S = goal 3. | term T = { number , id , +, - } 4. term → number N = { goal , expr , term , op } 5. | id 6. op → + P = { 1, 2, 3, 4, 5, 6, 7 } 7. | - This grammar defines simple expressions with addition & subtraction over “number” and “id” This grammar, like many, falls in a class called “context- free grammars” Tufts University Computer Science 11 The Front End Given a grammar, we Production Result can derive sentences goal 1 expr by repeated 2 expr op term substitution 5 expr op y 7 expr - y 2 expr op term - y 4 expr op 2 - y To recognize a valid 6 expr + 2 - y sentence in some 3 term + 2 - y CFG, we reverse this 5 x + 2 -y process and build up a parse Tufts University Computer Science 12 6 The Front End A parse can be goal represented by a tree (parse tree or syntax tree ) expr x + 2 - y expr op term expr op term - <id, y> + <number, 2> term 1. goal → expr 2. expr → expr op term <id, x> 3. | term 4. term → number Contains a lot of 5. | id 6. op → + unneeded information. 7. | - Tufts University Computer Science 13 The Front End Compilers often use an abstract syntax tree - The AST summarizes + <id, y> grammatical structure, without including detail about the derivation <id, x> <number, 2> This is much more concise AST s are one kind of intermediate representation ( IR ) Tufts University Computer Science 14 7 Front end to back end Code shape and lowering Start with high-level AST Dismantle complex structures into simple ones if if (x > 0) { t0 = x > 0 br t0 label1 y = a + b * c; > goto label2 z = d + b * c; x 0 label1: } t1 = b * c = = y = a + t1 y + z = d + t1 z + label2: a * d * b c b c Tufts University Computer Science 15 The Back End IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code Errors 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 back end Tufts University Computer Science 16 8 The Back End IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code Errors Instruction Selection Produce fast, compact code Take advantage of target features Addressing modes, special instructions (e.g., multadd ) Often viewed as a pattern matching problem ad hoc and automated methods Used to be a bigger problem on CISC machines – VAX-11 Orthogonality of RISC simplified this problem Tufts University Computer Science 17 The Back End Example: RISC instructions load @b => r1 ... load @c => r2 label1: mult r1, r2 => r3 t1 = b * c load @a => r4 y = a + t1 add r3, r4 => r5 z = d + t1 store r5 => @y ... load @d => r6 add r3, r6 => r7 store r7 => @z Notice: Explicit loads and stores Lots of registers – “virtual registers” Tufts University Computer Science 18 9 The Back End IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code Errors Register Allocation PentiumPentium 4 4 Have each value in a register when it is used Registers 1 cycle Manage a limited set of resourcesRegisters 1 cycle Cache 3-8 cycles Can change instruction choicesCache & insert LOAD 3-8s & cycles STORE s Memory 30-150 cycles Optimal allocation is NP-CompleteMemory (1 or k 30-150registers) cycles Compilers approximate solutions to NP-Complete problems Tufts University Computer Science 19 The Back End IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code Errors 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 Tufts University Computer Science 20 10 The Back End Conflicting goals: Move loads early to avoid waiting Move operations together to reduce registers load @b => r1 load @b => r1 load @c => r2 load @c => r2 mult r1, r2 => r3 load @a => r4 load @a => r1 load @d => r5 add r3, r1 => r1 mult r1, r2 => r3 store r1 => @y add r3, r4 => r4 load @d => r1 store r4 => @y add r3, r1 => r1 add r3, r5 => r5 store r1 => @z store r5 => @z Uses only 3 registers, Start loads early, hide but may stall on loads latency, but need 5 registers Tufts University Computer Science 21 Traditional 3-pass compiler Source Front IR Middle IR Back Machine Code End End End code ErrorsUnderstood misnomer Code Improvement (or optimization ) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time May also improve space, power consumption, … Must preserve “meaning” of the code Next semester... Tufts University Computer Science 22 11 The Optimizer (or Middle End) IROpt IROpt IROpt ... IROpt IR 1 2 3 n Errors Modern optimizers are structured as a series of passes 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 Tufts University Computer Science 23 Optimization Example: Array accesses for (i = 0; i < N; i++) for (i = 0; i < N; i++) for (j = 0; j < M; j++) for (j = 0; j < M; j++){ A[i][j] = A[i][j] + C; t0 = &A + (i * M) + j (*t0) += C; } for (i = 0; i < N; i++) { t1 = 0; t1 = ii ** M;M for (i = 0; i < N; i++) { for (j = 0; j < M; j++){ t1 = t1t1 + M;M; t0 = &A + t1 + j for (j = 0; j < M; j++){ (*t0) += C; t0 = &A + t1 + j } (*t0) += C; } } } Tufts University Computer Science 24 12 Role of the Run-time System Memory management services Allocate In the heap or in an activation record ( stack frame ) Deallocate Collect garbage Run-time type checking Error processing (exception handling) Interface to the operating system Input and output Support of parallelism Parallel thread initiation Communication and synchronization Tufts University Computer Science 25 Related systems Interpreters Scripting languages XML processing Just-in-time compilers Java JVM Microsoft CLR Binary instrumentation PIN valgrind Tufts University Computer Science 26 13 A bit about my research Getting more out of compilers: High-level optimizations Library-level optimization Checking for errors and security vulnerabilities Approximating vulnerability Cooperation with run-time system Compiler-assisted garbage collection

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us