Introduction to Code Generation in Compiler

Estudies4you
Memory Management
Both the front end and code generator perform the task of mapping the names in the source program to addresses to the data objects in runtime memory
The names used in the three address code refer to the entries in the symbol table
The type in a declaration statement determines the amount of storage (memory) needed to store the declared identifier
Using the symbol table information about memory requirements code generator determines the addresses in the target code
Similarly, if the three address code contains the labels then those labels can be converted into equivalent memory address
Instruction Selection:
The uniformity and completeness of instruction set is an important factor for the code generator
The selection of instruction depends upon the instruction set of target machine
The speed of instruction and machine idioms are two important factors in selection of instructions
If we do not consider the efficiency of target code then the instruction selection becomes a straightforward task
For each type of three address code, the code skeleton can be prepared which ultimately gives the target code for the corresponding construct
Register Allocation
The usage of operands becomes shorter and faster if the instruction contains “Register Operands” when compared to the usage of operands in memory. The effective utilization of register becomes an important factor while generating a good code
There are two important activities done while using registers:
                Register Allocation: During register allocation, select appropriate set of variable that will reside in registers
                Register Assignment: During register assignment, pick up the specific register in which corresponding variable will reside
Choice of Evaluation Order
The evaluation order is an important factor in generating an efficient target code
Some orders require less number of registers to hold the intermediate results than others. Picking up the best order is one difficulty in code generation
Mostly, avoid this problem by referring the order in which the three address code is generated by semantic actions
Approaches to Code Generation
The most important factor for a code generation is that it produces the correct code
With this approach of code generation, various algorithms for generating code are designed
Object Code Forms
The output generated by code generator is an object code or machine code. This code typically appears in the below forms
Absolute code
Relocatable Machine Code
Assemble Code
object code generation, phases of compilation, last phase of compilation, code generation in compilers, what is code generation, role of code generation,estudies4you, machine code in compilers,Introduction to Code Generation in Compiler,Instruction Selection in code generation,what is relocatable machine code in compiler, assemble code in compilers,absolute code in compilers,memory management in compilers, jntuh compiler design lecture notes,jntu compiler design notes pdf,

Figure 1
Absolute code:
Absolute code is a machine code that consists of reference to actual addresses within programs address space
The generated code can be put directly in the memory, and execution begins quickly
With the help of absolute code generation, the compilation and execution of small program is done rapidly
Example:       
A number of “student-job” compilers like WATFIV (WATerlodo FORTRAM IV) and PL/C generate absolute code.
Relocatable Machine Code:
The compilation of subprograms can be carried out separately whenever relocatable machine language program is generated as output
A set of relocatable object modules can be linked together and can be loaded for execution with the aid of a linking loader
The prime advantage of producing relocatable machine code is that one gains more flexibility in the process of compiling subroutines independently and calling other previously compiled projects from an object module
In the case when the target machine does not handle relocation automatically, the compiler must give explicit relocation data to the loader to link the independently compiled program segments
Assemble code:
The code generation process becomes little easier when an assembly language program is produced as output.
In the process of generation of code, the symbolic instructions can be generated, and assembler’s macro facilities can be used.
But the generation of assembler code as an output slows down the code generation process, since assembling, linking and loading are required.



To Top