Code GenerationThe term for the last phase of a compiler that emits code in the target language.
Once the operating system has read an object and understood what to do with it, it has to produce the "code" that the hardware will run to evaluate the object. We at the TUNES project are convinced that code generation should be based on meta-programmable partial evaluation.
The simplest solutions for code generation are to write an interpreter that directly evaluates the internal encoding of objects, or an interpreter for an intermediate machine that is a compromise between the low-level hardware computing model and the high-level computing model. These solutions should be used as the default one in TUNES. But as some objects get used frequently (or are intended to be used frequently), the partial evaluator triggers and tries to optimize these objects, spending no more time optimizing than is expected to be saved by the optimizations.
The user, as always, can give more or less explicit and thorough hints about where to focus code generation efforts, and how to try to generate good code; they will associate code to be optimized and tactics to optimize code with, or give hints about such association for the system to look for automatically. Such tactics could look like:
- "try to implement these two functions as arrays, to interleave those two arrays (with possibly other arrays, too), to achieve power-of-two element size in the merged array, while wasting as few bytes as possible, to align the beginning of the array, properly offset, to a 64KB boundary (or better, put it at address 0)", so that access to the array will be as fast as possible.
- "try to use a barrel-shift register instead of an arithmetical counter for this bit-mask loop"
- "transform incremental counting into decremental counting to skip a compare instruction"
- "try these megafast implementations under various known contexts (CPU, calling convention, or any restrictive predicate)", or even
- "send that informal code specification down the usual trail to the company that offers commercial coding services and support, so they make great computer code out of this stuff".
Such tactics would prove much easier to use, much more portable, much surer as for efficiency (if tactic fails to yield better code, it can be semi-automatically ignored) and much faster to tweak (when a proper library is available) than any low-level coding, while bringing the same expressive power as this low-level coding; more importantly, they wouldn't tie code to old hardware or software hacks, and could be managed dynamically.
See also Run-Time Code Generation.