, 1 Using ParentheC to Transform Scheme Programs to C or How to Write Interesting Recursive Programs in a Spartan Host (Program Counter) Ronald Garcia, Jeremy G. Siek, Ruj Akavipat, William Byrd, Samuel Chun, David W. Mack, Alex Platte, Heather Roinestad, Kyle Blocher, Joseph P. Near, and Daniel P. Friedman Computer Science Department, Indiana University Bloomington, IN 47405, USA 2 Garcia et al. 1 Introduction Many interesting Scheme programs can be transformed into equivalent C programs. The process involves applying provably correct transformations to a correct Scheme program, converting it step-by-step from one form to another. At each stage the program runs exactly as it did at the beginning. After applying these transformations, the result is the same Scheme program but in registerized and trampolined form. The final step of the transformation to C is a relatively trivial rewrite of Scheme syntax into C. Conversion to C is simple-minded, but unfortunately it is not easy. The resulting C code is generally quite verbose and relatively complex. The translation is mechanical, but mistakes in translation are easy to make and quite difficult to locate. Experience has shown in the presence of a single mistake, it is better to start the process from scratch. The ParentheC/PC language, which contains only seven simple forms, is one approach to avoiding the tedious translation from trampolined Scheme to C. Manually converting Scheme to ParentheC/PC is easy and the rest of the work has been automated. The problem, of course, would be much simpler if C did not blowup when cascading many recursive function calls. This is a situation that occurs when running an interpreter, for example. More importantly, even if the code is in first-order tail form, we still have to contend with cascading tail calls. Of course, these tail calls can be eliminated by either using trampolining, by using gotos with global register assignments, or by using a special program-counter register along with the trampolining strategy. Here, we choose the program-counter approach. While transforming the Scheme program to trampolined form, we methodically introduce places where we can use ParentheC/PC. When we have reached tail recursive, registerized, and trampolined forms, the entire program will have been transformed to ParentheC/PC, which is completely testable in Scheme. Finally, we can run the pc2c translator, which produces a valid C program that yields the same results as the original Scheme program. The structure of the paper is in four sections. First, we introduce the language ParentheC/PC, which is a subset of Scheme plus seven simple macros. Second, we work through a simple example starting with a recursive definition and taking it all the way to C. This process entails making the code be in first-order tail form. That means that all calls are tail calls to global functions and there are no lambda expressions. Once we have reached this stage, we registerize and then trampoline the program. Then we feed the ParentheC/PC file to pc2c, which creates a C program (in two files). We end this section by showing how the C code coresponds to the ParentheC/PC code. Next, we describe the operation of the seven ParentheC/PC macros. In the last section, we offer some conclusions. Tutorial 3 2 The Language ParentheC/PC ParentheC/PC is a subset of Scheme with seven special forms: define-registers, define-program-counter, define-label, define-union, union-case, mount-trampoline, and dismount-trampoline. Here is the grammar: <1-Prim> <2-Prim> → → | | | → | | | | | | | | | | → | | | | | | | → → * (define-registers *) (define-program-counter ) (define-label