mod 4-5
mod 4-5
2. Target Program
The target program is the final output of the code generator, which can be in the form
of absolute machine language, relocatable machine language, or assembly language.
Each type of output has its own set of challenges:
Absolute Machine Language is easy to execute but lacks flexibility because it is
bound to specific memory locations.
Relocatable Machine Language allows parts of the program to be moved around in
memory, making it suitable for linking multiple modules, but it requires a linking loader
and has some overhead.
Assembly Language is symbolic and needs an additional step (an assembler) to
convert it into machine code, but it makes the code generation process easier.
3. Memory Management
Memory management in the code generation phase involves mapping variable names
to their corresponding memory locations. The code generator works closely with the
front-end to access the symbol table, where memory addresses for variables are stored.
A major challenge is ensuring that the code generator uses:
Memory efficiently
4. Instruction selection
Instruction selection is the process of choosing the most suitable machine instructions
to translate intermediate code into executable code. The goal is to optimize the
generated code by selecting instructions that are efficient and appropriate for the target
machine. If the right instructions are not selected, the resulting code can be inefficient
and slow. A code generator might need to decide between different ways of
implementing the same operation, such as using different addressing modes or
optimizing for processor-specific features.
5. Register Allocation
Efficient use of registers is important because registers are faster than memory, and
utilizing them effectively can significantly improve program performance. The
challenge lies in selecting the right variables to store in registers at different points in
the program.
Register allocation involves two stages:
1. Register Allocation: It is selecting which variables will reside in the registers at each
point in the program
2. Register Assignment: Assigning specific registers to those variables selected in
Register Allocation.
The difficulty arises in managing which variables are allocated to registers, especially
when the number of available registers is limited. Poor register allocation can lead to
spills, where data is temporarily stored in memory, causing slower performance.
Target Machine
Instruction Cost
RUN-TIME STORAGE MANAGEMENT
The information which required during an execution of a procedure is kept in a block of storage
called an activation record. The activation record includes storage for names local to the
procedure.
We can describe address in the target code using the following ways:
1. Static allocation
2. Stack allocation
In static allocation, the position of an activation record is fixed in memory at compile time.
In the stack allocation, for each execution of a procedure a new activation record is pushed
onto the stack. When the activation ends then the record is popped.
For the run-time allocation and deallocation of activation records the following three-address
statements are associated:
1. Call
2. Return
3. Halt
1. Code
2. Static data
3. Stack
Static allocation:
1. Implementation of call statement:
2. GOTO callee.code_area /* It transfers control to the target code for the called proc
edure*/
Where,
callee.code_area shows the address of the first instruction for called procedure.
#here + 20 literal are used to return address of the instruction following GOTO.
1. GOTO * callee.static_area
It is used to transfer the control to the address that is saved at the beginning of the activation
record.
The HALT statement is the final instruction that is used to return the control to the operating
system.
Stack allocation
Using the relative address, static allocation can become stack allocation for storage in
activation records.
In stack allocation, register is used to store the position of activation record so words in
activation records can be accessed as offsets from the value in this register.
The following code is needed to implement stack allocation:
1. Initialization of stack:
Where,
Easier to Implement: Intermediate code generation can simplify the code generation
process by reducing the complexity of the input code, making it easier to implement.
Facilitates Code Optimization: Intermediate code generation can enable the use of
various code optimization techniques, leading to improved performance and
efficiency of the generated code.