What is Code Generation in Compiler Construction
Firstly, After generating tokens, parsing them; constructing the syntax tree, performing the semantic phase; and generating an intermediate representation the last phase or step is to generate code. Secondly, The simplest approach to code generation is in which we consider each component of the program in isolation that is that one the program has no relation to the other program and is unaware of them. Moreover, Each expression; statement is going to be generated; as a separate unit and has nothing to do with its neighboring units.
Each expression and statement; are going to be generated as a standalone unit; without regard to its neighbors. This is easy and easy; it’s conservative and can cause an outsized amount of non-optimal code. Furthermore, But it will work, and give you a starting point for thinking about more sophisticated techniques. A target tree is formed or produced from the structure tree; by the code generator and it has three main principles or subtasks;
Resource allocation:
Firstly, We distinguish global register allocation, which holds over a whole procedure; from local register allocation, which controls the utilization of registers within expressions and influences the execution order. Register assignment takes place during code selection.
Execution order determination:
Secondly, Specify the sequence in which the descendants of a node will be evaluated. In the simplest case, we cancel the execution order without reference to target machine register allocation. The code selector performs a depth-first, left-to-right traversal of the structure tree that corresponds directly to the posting form of the expressions. It does not alter the left-to-right evaluation of the operands of computing the value of an expression.
Code selection:
Lastly, Select the final instruction sequence corresponding to the operations appearing in the structure tree under the mapping. Memory mapping determines the dimensions and (relative) address of every object. In the process, it yields the sizes and alignments for all target types; therefore the relative addresses the components of composite objects. It also constitutes most of the information needed to construct the type templates.