Code generation is the compiler's phase in which the meaning of the source text (the compiler's input) is unparsed and output in the target language.
Sophisticated compilers may use several cascaded code generation stages to fully compile code; this is due to the fact that algorithms for code optimization are more readily applicable in an intermediate code form, and also facilitates a single compiler that can target multiple architectures as only the final code generation stage (the backend) would need to change from target to target.
The input to the code generator stage typically consists of a parse tree, abstract syntax tree, or intermediate language code. Since the target machine may be a physical machine such as a microprocessor, or an abstract machine such as a virtual machine or an intermediate language (human-readable code), the output of code generator could be machine code, assembly code, code for an abstract machine (like JVM), or anything between.
In a more general sense, code generation is used to produce programs in some automatic manner, reducing the need for human programmers to write code manually. Code generations can be done either at runtime, including load time, or compiler time. Just-in-time compilers are an example of a code generator that produce native or nearly native code from byte-code or the like when programs are loaded onto the compilers. On the other hand, a compiler-compiler, (yacc, for example) almost always generates code at compiler time. A preprocessor is an example of the simplest code generator, which produces target code from the source code by replacing predefined keywords.