The code generator should take the following things into consideration to generate the code: Code GeneratorĪ code generator is expected to have an understanding of the target machine’s runtime environment and its instruction set. If the target code can accommodate those instructions directly, that will not only improve the quality of code, but also yield more efficient results.
The target machine can deploy more sophisticated instructions, which can have the capability to perform specific operations much efficiently. Though the output of a * a and a 2 is same, a 2 is much more efficient to implement. Their ‘strength’ can be reduced by replacing them with other operations that consume less time and space, but produce the same result.įor example, x * 2 can be replaced by x << 1, which involves only one left shift. There are operations that consume more time and space. For example, the expression a = a + 0 can be replaced by a itself and the expression a = a + 1 can simply be replaced by INC a. There are occasions where algebraic expressions can be made simple. So instead of jumping to L1 and then to L2, the control can directly reach L2, as shown below: In this code,label L1 can be removed as it passes the control to L2. There are instances in a code where the program control jumps back and forth without performing any significant task. In this code segment, the printf statement will never be executed as the program control returns back before it can execute, hence printf can be removed. Programmers may have accidently written a piece of code that can never be reached. Unreachable code is a part of the program code that is never accessed because of programming constructs. We can delete the first instruction and re-write the sentence as: Multiple loading and storing of instructions may carry the same meaning even if some of them are removed. A bunch of statements is analyzed and are checked for the following possible optimization: Redundant instruction eliminationĪt source code level, the following can be done by the user:Īt compilation level, the compiler searches for instructions redundant in nature. These methods can be applied on intermediate codes as well as on target codes. By locally, we mean a small portion of the code block at hand. This optimization technique works locally on the source code to transform it into an optimized code. Interior nodes also represent the results of expressions or the identifiers/name where the values are to be stored or assigned. Leaf nodes represent identifiers, names or constants. DAG provides easy transformation on basic blocks. Directed Acyclic Graphĭirected Acyclic Graph (DAG) is a tool that depicts the structure of basic blocks, helps to see the flow of values flowing among the basic blocks, and offers optimization too. We will now see how the intermediate code is transformed into target object code (assembly code, in this case).