Code generation is the last phase in the compilation process. Being a machine-dependent phase, it is not possible to generate good code without considering the details of the particular machine for which the compiler is expected to generate code. Even so, a carefully selected code-generation algorithm can produce code that is twice as fast as code generated by an ill- considered code-generation algorithm.
In this chapter, we first discuss straightforward code generation from a sequence of three-address statements. This is followed by a discussion of the code-generation algorithm that takes into account the flow of control structures in the program when assigning registers to names . Then we will look at a code-generation algorithm that is capable of generating reasonably good code from a basic block. Finally, various machine-dependent optimizations that are capable of improving the efficiency of object code are discussed. Throughout our discussion, we assume that the input to the code-generation algorithm is a sequence of three-address statements partitioned into basic blocks.