Major Tasks in Code Generation
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.
- Instruction scheduling: in which order to put those instructions. Scheduling is a speed optimization that can have a critical effect on pipelined machines.
- Register allocation: the allocation of variables to processor registers
- Debug data generation if required so the code can be debugged.
Instruction selection is typically carried out by doing a recursive postorder traversal on the abstract syntax tree, matching particular tree configurations against templates; for example, the tree 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 instruction set of the target machine. This second phase does not require a tree traversal; it can be done linearly, and typically involves a simple replacement of intermediate-language operations with their corresponding opcodes. However, if the compiler is actually a language translator (for example, one that converts Eiffel to C), then the second code-generation phase may involve building a tree from the linear intermediate code.
Read more about this topic: Code Generation (compiler)
Famous quotes containing the words major, tasks, code and/or generation:
“Look, I’m not saying he didn’t make some major mistakes. When it comes to value judgments, Rob is right up there with Custer and Nixon.”
—Jonathan Reynolds, screenwriter. Leo (Richard Mulligan)
“Everything I do is done within sight of the Führer, so that my faults or mistakes are never hidden from him. I do my very utmost to live and act in such a manner that the Führer should remain satisfied with me; I am hard-working; but whether I shall always be able to cope with the tasks entrusted to me in the future as well, is an open question.”
—Martin Bormann (1900–1945)
“Hollywood keeps before its child audiences a string of glorified young heroes, everyone of whom is an unhesitating and violent Anarchist. His one answer to everything that annoys him or disparages his country or his parents or his young lady or his personal code of manly conduct is to give the offender a “sock” in the jaw.... My observation leads me to believe that it is not the virtuous people who are good at socking jaws.”
—George Bernard Shaw (1856–1950)
“What an infernal set of fools those schoolmarms must be! Well, if in order to please men they wish to live on air, let them. The sooner the present generation of women dies out, the better. We have idiots enough in the world now without such women propagating any more.”
—Elizabeth Cady Stanton (1815–1902)