1 / 11

Compilation by Program Transformation

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.

brook
Download Presentation

Compilation by Program Transformation

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. Compilation by Program Transformation 600.426: Programming Languages Spring 2004

  2. 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

  3. 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

  4. 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

  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; 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

  8. 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]…

  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

More Related