Code generation (compiler)
This Code generation intro needs additional citations for verification. (November 2006) |
In computing, code generation is part of the process chain of a compiler and converts intermediate representation of source code into a form (e.g., machine code) that can be readily executed by the target system.
Sophisticated compilers typically perform
The input to the code generator typically consists of a
Major tasks
In addition to the basic conversion from an intermediate representation into a linear sequence of machine instructions, a typical code generator tries to optimize the generated code in some way.
Tasks which are typically part of a sophisticated compiler's "code generation" phase include:
- Instruction selection: which instructions to use.
- pipelinedmachines.
- variables to processor registers[2]
- Debug data generation if required so the code can be debugged.
Instruction selection is typically carried out by doing a
W := ADD(X,MUL(Y,Z))
might be transformed into a linear sequence of instructions by recursively generating the sequences for t1 := X
and t2 := MUL(Y,Z)
, and then emitting the instruction ADD W, t1, t2
.
In a compiler that uses an intermediate language, there may be two instruction selection stages—one to convert the parse tree into intermediate code, and a second phase much later to convert the intermediate code into instructions from the
), then the second code-generation phase may involve building a tree from the linear intermediate code.Runtime code generation
When code generation occurs at
Related concepts
The fundamental task of taking input in one language and producing output in a non-trivially different language can be understood in terms of the core
Many
Reflection
In general, a syntax and semantic analyzer tries to retrieve the structure of the program from the source code, while a code generator uses this structural information (e.g.,
See also
- Automatic programming
- Comparison of code generation tools
- Source-to-source compilation: automatic translation of a computer program from one programming language to another
References
- ISBN 978-1-55860-320-2.
code generation.
- ISBN 0-201-10088-6.
- ^ Code Generation: The Real Lesson of Rails. Artima.com (2006-03-16). Retrieved on 2013-08-10.