Compilation by Program Transformation

Document Sample
scope of work template
							    Compilation by
Program Transformation




        600.426: Programming Languages
                            Spring 2004
Goal
    To understand the most basic, fundamental concepts behind
     compilation: how a high-level program can be mapped to
     machine code
        By writing a DSR compiler
    DSR      OCaml           DSR            OCaml              DSR
    Full              No Nested Functions           Primitive Operations Only
                                                                 OCaml

            Machine   gcc                   OCaml             DSR
                            Primitive C
           Language                                 C-like Program Structure




    To appreciate the gap between high- and low-level code, and
     understanding how the gaps may be bridged
                                            1. Speed

Why Study Compilation?                      2. Speed
                                            3. Speed


   Compilers are an important technology because
    code produced by a compiler is faster than
    interpreted code by several orders of magnitude

   Atleast 95% of the production software running is
    compiled code

   Compiled code is sometimes even better than
    hand-crafted assembly code because of the
    complexity of modern processors
Outline
     We will outline a compiler of DSR to a very limited subset of C
      (“pseudo-assembly”)
     You will later implement a DSR compiler in Caml by filling in
      the holes left out
     Compiling: Series of Program Transformations
          Closure Conversion
          A-Translation
          Function Hoisting
      & finally, Translation into C
    DSR     Closure            DSR              A–Trans-               DSR
    Full   Conversion   No Nested Functions      lation     Primitive Operations Only
                                                                         Function
                                                                         Hoisting

             Machine    gcc                   Translation             DSR
                                Primitive C     into C
            Language                                        C-like Program Structure
Program Transformation Approach

   Map programs to equivalent programs, removing
    high-level features one at a time

   The idea is:
       To make the program’s expressiveness more and more
        primitive
       Making it closer and closer to the target (machine)
        language, and
       Hence bridging gaps gradually
Program Transformation Approach

   Real production compilers such as gcc and Sun’s javac do
    not use a trandformation process
       Primarily because the speed of the compilation itself is too slow

   It is in fact possible to produce very good code by
    transformation.
       E.g., The SML/NJ ML compiler uses a transformational approach

   Most production compilers transform the program to an
    intermediate form which is neither source nor target language
    (“intermediate language”) and do numerous optimizing
    transformations on this intermediate code
Example of PT-Approach

for (i=0 ; i<10 ; i++)
    a = a+i;
                                        i=0;
            1                loop_entry: if (i>=10)
                                               GOTO loop_exit;
i=0;                     2              a = a+i;
while (i<10)                            i++;
{                                       GOTO loop_entry;
     a = a+i;                loop_exit: [next_statement]…
     i++;
}
Example of PT-Approach

           MOV <i>, $0
loop_entry: CMP <i>, $10
           JGE loop_exit
           ADD <a>, <i>
           INC <i>
           JMP loop_entry
loop_exit: [next_statement]…
Soundness Property
   Programs before and after translation have the
    same execution behavior
       In our case, termination and same numerical output
       But in general, the same I/O behavior


   Note that the programs that are output by the
    translation are not necessarily operationally
    equivalent to the originals
       Context
What we will not be covering
   Lexical Analysis and Parsing

   Optimizations

   Compilation of low-level languages such as C/C++
       Our focus is on the compilation of higher-order languages


   Our executables will not
       Try to catch run-time type errors
       Garbage collect unused memory

                   We are not being Realistic!
Closure Conversion: Why?
   Transformation which eliminates nonlocal variables
    in functions
       To obtain an equivalent program where all variables used
        in functions are parameters of the function


   The problematic nonlocals which must be removed
    are those that are parameters of other functions,
    where function definitions have been nested

						
Related docs