120 likes | 274 Views
Compilation by Program Transformation. 600.426: Programming Languages Spring 2004. OCaml. OCaml. DSR Full. DSR No Nested Functions. DSR Primitive Operations Only. OCaml. gcc. DSR C-like Program Structure. OCaml. Machine Language. Primitive C. Goal.
E N D
Compilation by Program Transformation 600.426: Programming Languages Spring 2004
OCaml OCaml DSR Full DSR No Nested Functions DSR Primitive Operations Only OCaml gcc DSR C-like Program Structure OCaml Machine Language Primitive C 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 • To appreciate the gap between high- and low-level code, and understanding how the gaps may be bridged
Why Study Compilation? • Speed • Speed • 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
A–Trans- lation Closure Conversion DSR Full DSR No Nested Functions DSR Primitive Operations Only Function Hoisting gcc DSR C-like Program Structure Translation into C Machine Language Primitive C 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
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; loop_entry: if (i>=10) GOTO loop_exit; a = a+i; i++; GOTO loop_entry; loop_exit: [next_statement]… 1 i=0; while (i<10) { a = a+i; i++; } 2
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