Program Generation (PG) is about writing programs that write programs. A programgenerator composes various pieces of code to construct a new program. When employedat runtime, PG can produce an efficient version of a program by specializing it according toinputs that become available at runtime. PG has been used in a wide range of applicationsto improve program efficiency and modularity as well as programmer productivity.There are two major problems associated with PG: (1) Program generation has its owncost, which may cause a performance loss even though PG is intended for performancegain. This is especially important for runtime program generation. (2) Compilability guaranteesabout the generated program are poor; the generator may produce a type-incorrectprogram. In this dissertation we focus on these two problems. We provide three techniquesthat address the first problem. First, we show that just-in-time generation can successfullyreduce the cost of generation by avoiding unnecessary program generation. Wedo this by means of an experiment in the context of marshalling in Java, where we generatespecialized object marshallers based on object types. Just-in-time generation improvedthe speedup from 1.22 to 3.16. Second, we apply source-level transformations to optimizethe execution of program generators. Up to 15% speedup has been achieved in runtimegeneration time for Jumbo, a PG system for Java. Third, we provide a technique to stageanalysis of generated programs to perform a portion of the analysis at compile time ratherthan completing the entire analysis at runtime. We also give experimental evidence viaseveral examples that this technique reduces runtime generation cost. To address the secondproblem of PG, we first show that operational semantics of record calculus and programgeneration are equivalent, and that a record type system can be used to type-checkprogram generators. We also show that this is true in the presence of expressions withside-effects. We then make use of an already-existing record calculus feature, subtyping,to extend the program generation type system with subtyping constraints. As a result,we obtain a very expressive type system to statically guarantee that a generator will producetype-safe code. We state and prove the theorems based on an ML-like language withprogram generation constructs. |