Code Generation and Optimization Techniques
Code generation
Code generation is the process of transforming a representation of code into a, in most cases binary, output that can be executed. The most common form of code generation is the transformation of an AST or intermediate representation into assembly and subsequently into binary code. Generating code for language runtimes (e.g. Java's JVM) does also count as code generation. Optimization techniques are often performed before, while and sometimes even after the code generation has taken place. Code generators are often also referred to as the "back end" of a Compiler, because it is usually the last stage that runs. Code generation also often has to function as a sort of polyfill, like in the case of "libgcc" and its counterparts. Some processors, especially in the embedded space, may not possess certain functionality, like an FPU or even division for that matter, which then may have to be implemented at software-level if used in the program and some features may not be used in order to maintain a wide availability of the program across different processors of the same architecture, in which case Just In Time (JIT) compilers have an advantage in terms of native optimization, because their compiled code only needs to run on the current processor. This is also a reason why source-based systems like Gentoo exist.
Optimization in Code generation
Optimizations in code generation are often platform-specific, as different processors and architectures have different strengths and weaknesses. One system may have a fast processor, but really slow memory (e.g. Nintendo 64), which may lead to specific optimization for size and the disabling of inlining functionality. Other processors may be slow, but be able to communicate with very fast memory, in which case caching results in a lookup-table may be preferable. This is why most optimizing compilers have options to optimize for different metrics such as size, speed and even energy-efficiency. Some may also offer options to disable specific optimizations either from the command line or in the backend's API itself.