Ia3-1
Ia3-1
4. Instruction selection:
o Nature of instruction set of the target machine should be complete and
uniform.
o When you consider the efficiency of target machine then the instruction
speed and machine idioms are important factors.
o The quality of the generated code can be determined by its speed and
size.
5. Register allocation
Register can be accessed faster than memory. The instructions involving
operands in register are shorter and faster than those involving in memory
operand.
The following sub problems arise when we use registers:
Register allocation: In register allocation, we select the set of variables that
will reside in register.
Register assignment: In Register assignment, we pick the register that contains
variable.
6. Evaluation order
The efficiency of the target code can be affected by the order in which the
computations are performed. Some computation orders need fewer registers
to hold results of intermediate than others.
3. Basic blocks and flow graphs:
Basic Blocks in Compiler Design:
Basic Block is a straight line code sequence that has no branches in and out
branches except to the entry and at the end respectively. Basic Block is a set of
statements that always executes one after other, in a sequence.
Algorithm: Partitioning three-address code into basic blocks.
Input: A sequence of three address instructions.
Process: Instructions from intermediate code which are leaders are
determined. The following are the rules used for finding a leader:
1. The first three-address instruction of the intermediate code is a leader.
2. Instructions that are targets of unconditional or conditional jump/goto
statements are leaders.
3. Instructions that immediately follow unconditional or conditional
jump/goto statements are considered leaders.
Basic blocks are sequences of instructions in a program that have no branches
except at the entry and exit. Compiler design is one of the core areas of
computer science, and if you want to gain expertise in it, the GATE CS Self-
Paced Course provides a detailed look into basic blocks, optimization, and
other compiler design concepts
Example 1:
The following sequence of three-address statements forms a basic block:
FLOW GRAPH:
Flow graph is a directed graph. It contains the flow of control information for
the set of basic block.
A control flow graph is used to depict that how the program control is being
parsed among the blocks. It is useful in the loop optimization.
Flow graph for the vector dot product is given as follows:
1. Pass by Value
Characteristics:
Use Cases: Suitable for primitive data types where the overhead of
copying is minimal.
2. Pass by Reference
Characteristics:
Mechanism: A copy of the actual parameter is passed in, and at the end of
the function, the final value is copied back to the original variable.
Characteristics:
Characteristics:
Use Cases: Commonly found in languages like Algol; less used in modern
languages.
5. Pass by Pointer
Characteristics:
Use Cases: Frequently used in C and C++ for dynamic data structures.
6. Pass by Closure
Characteristics:
Use Cases: Useful for callbacks, event handling, and maintaining state.
5.STORAGE ALLOCATION IN CD:
Storage allocation in compiler design involves managing memory for variables, data
structures, and code during the compilation process and runtime. The methods of storage
allocation can significantly affect performance and memory usage. Here are the primary
storage allocation methods:
1. Static Allocation
Description: Memory is allocated at compile time for variables whose sizes are known
and fixed. This includes global variables and constants.
Characteristics:
Use Cases: Best for global constants, fixed-size arrays, and other data whose lifetime
and size are known at compile time.
2. Stack Allocation
Characteristics:
Use Cases: Suitable for local variables and parameters in functions, especially in
languages with block scope.
3. Heap Allocation
Characteristics:
o Requires manual deallocation (e.g., free in C), which can lead to memory leaks
if not handled properly.
Use Cases: Useful for data structures like linked lists, trees, and arrays whose sizes are
not known at compile time.
4. Global Allocation
Description: Memory is allocated for global variables and is accessible throughout the
program.
Characteristics:
o The lifetime of global variables lasts for the duration of the program.
Use Cases: Best for data that needs to be accessed across multiple functions or
modules.
5. Memory Pooling
Characteristics:
Use Cases: Common in game development and real-time systems where performance
is critical.
6. Garbage Collection
Characteristics:
Use Cases: Common in languages like Java, Python, and others that prioritize
developer productivity and safety.