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, Im not saying he didnt 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)
“I am of course confident that I will fulfil my tasks as a writer in all circumstancesfrom my grave even more successfully and more irrefutably than in my lifetime. No one can bar the road to truth, and to advance its cause I am prepared to accept even death. But may it be that repeated lessons will finally teach us not to stop the writers pen during his lifetime? At no time has this ennobled our history.”
—Alexander Solzhenitsyn (b. 1918)
“Motion or change, and identity or rest, are the first and second secrets of nature: Motion and Rest. The whole code of her laws may be written on the thumbnail, or the signet of a ring.”
—Ralph Waldo Emerson (18031882)
“I call it our collective inheritance of isolation. We inherit isolation in the bones of our lives. It is passed on to us as sure as the shape of our noses and the length of our legs. When we are young, we are taught to keep to ourselves for reasons we may not yet understand. As we grow up we become the men who never cry and the women who never complain. We become another generation of people expected not to bother others with our problems.”
—Paula C. Lowe (20th century)