Code Generation (compiler) - Major Tasks in Code Generation

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:

    Inanimate objects are classified scientifically into three major categories—those that don’t work, those that break down and those that get lost.
    Russell Baker (b. 1925)

    More than ten million women march to work every morning side by side with the men. Steadily the importance of women is gaining not only in the routine tasks of industry but in executive responsibility. I include also the woman who stays at home as the guardian of the welfare of the family. She is a partner in the job and wages. Women constitute a part of our industrial achievement.
    Herbert Hoover (1874–1964)

    Acknowledge your will and speak to us all, “This alone is what I will to be!” Hang your own penal code up above you: we want to be its enforcers!
    Friedrich Nietzsche (1844–1900)

    What makes this Generation of Vermin so very Prolifick, is the indefatigable Diligence with which they apply themselves to their Business. A Man does not undergo more watchings and fatigues in a Campaign, than in the Course of a vicious Amour. As it is said of some Men, that they make their Business their Pleasure, these Sons of Darkness may be said to make their Pleasure their Business. They might conquer their corrupt Inclinations with half the Pains they are at in gratifying them.
    Joseph Addison (1672–1719)