Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compilation by Program Transformation 600.426: Programming Languages Spring 2004.

Similar presentations


Presentation on theme: "Compilation by Program Transformation 600.426: Programming Languages Spring 2004."— Presentation transcript:

1 Compilation by Program Transformation 600.426: Programming Languages Spring 2004

2 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 To appreciate the gap between high- and low-level code, and understanding how the gaps may be bridged Goal DSR Full DSR No Nested Functions DSR Primitive Operations Only Primitive C Machine Language OCaml gcc DSR C-like Program Structure OCaml

3 Why Study Compilation? 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 1.Speed 2.Speed 3.Speed

4 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 Full DSR No Nested Functions DSR Primitive Operations Only Primitive C Machine Language Closure Conversion A–Trans- lation gcc DSR C-like Program Structure Translation into C Function Hoisting

5 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

6 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

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

8 Example of PT-Approach MOV, $0 loop_entry: CMP, $10 JGE loop_exit ADD, INC JMP loop_entry loop_exit: [next_statement]…

9 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

10 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!

11 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


Download ppt "Compilation by Program Transformation 600.426: Programming Languages Spring 2004."

Similar presentations


Ads by Google