code generation, intermediate representation, target language, instruction set, registers, data structures, control constructs, procedures and functions, activation record, code optimization, machine-dependent optimization, register allocation
The last phase of compilation is Code Generation. This phase takes the Intermediate Representation of the original source program as input and produces a semantically equivalent program in the target language as output. Depending upon the nature of the target language, there may be subsequent steps necessary to make the program ultimately executable on a particular platform. In a typical case of Linux/Unix-like platform, the target language is generally a (simplified) assembly code, and further steps of assembly through the system assembler linking with language library and loading are required to get an executable program. Further, while generating the target code, it will be highly desirable to generate a “good” code, one which satisfies the basic requirement of any good algorithm – it should be correct and efficient.
The basic requirements of the Code Generation phase are:
In order to satisfy these requirements, several things need to be done:
Fig. 9.1 Code generator in the compiler chain. The dashed arrows show the order in which we discuss the phases, from pedagogical necessity
We first discuss, after detailing our concerns in the task of code generation, a naïve code generator, without any worry of inefficiency of the generated code. Of course, correctness of the generated code must be ensured even in presence of special cases and idiosyncrasy of the target language. Then we point out some typical inefficiencies and see how the code generator can deal with them and generate better target code, by what is called machine-dependent optimization. We shall also discuss some concepts like Basic Blocks which are used in a real-life compiler, viz., GNU gcc.
18.191.235.62