Unit 4 CD
Unit 4 CD
intermediate representation (IR) that is easier to analyze and optimize before generating the
target code. Declarations and assignment statements are essential components of this process:
1. Declarations:
o Purpose: Declarations allocate memory space and define attributes such as type
and name in the symbol table.
o Process:
1. Identify declaration statements like variable declarations or function
prototypes.
2. Allocate memory space based on the data type and size specified in the
declaration.
3. Update the symbol table with the type, name, and memory location of the
declared entity.
2. Assignment Statements:
o Purpose: Assignment statements assign values to variables or attributes.
o Process:
1. Identify assignment statements where a value is assigned to a variable or
an attribute.
2. Generate intermediate code that represents the assignment operation.
3. Ensure type compatibility between the assigned value and the variable
being assigned.
4. Update the symbol table with the assigned value if necessary.
3. Example:
o Source Code:
c
Copy code
int a, b;
a = 10;
b = a + 5;
o Intermediate Code:
css
Copy code
allocate a, 4
allocate b, 4
a = 10
t1 = a + 5
b = t1
Both Boolean expressions and case statements play crucial roles in the control flow and decision-
making processes within compilers, ensuring efficient program execution and behavior.
Before:
assembly
Copy code
MOV R1, A
MOV A, R1
After:
assembly
Copy code
; eliminated redundant move
Before:
assembly
Copy code
MOV R1, #2
MOV R2, #3
ADD R3, R1, R2
After:
assembly
Copy code
MOV R3, #5
3. Strength Reduction: Replacing a costly operation with a less expensive one. For
example, replacing multiplication by a power of two with a left shift.
Before:
assembly
Copy code
MUL R1, R2, #4
After:
assembly
Copy code
SHL R1, R2, #2
Before:
assembly
Copy code
JMP L1
L1: NOP
After:
assembly
Copy code
; eliminated unnecessary jump
Before:
assembly
Copy code
MOV R1, #1
ADD R1, R1, #1
After:
assembly
Copy code
MOV R1, #2
Example of Peephole Optimization
Before Optimization:
assembly
Copy code
LOAD R1, A ; Load value of A into R1
LOAD R2, B ; Load value of B into R2
ADD R1, R1, R2; Add R2 to R1
STORE A, R1 ; Store result back into A
LOAD R1, A ; Load value of A again into R1
After Optimization:
assembly
Copy code
LOAD R1, A ; Load value of A into R1
LOAD R2, B ; Load value of B into R2
ADD R1, R1, R2; Add R2 to R1
STORE A, R1 ; Store result back into A
; Removed redundant LOAD R1, A
Benefits of Peephole Optimization
1. Improved Performance: By reducing the number of instructions and optimizing for faster
execution, peephole optimization can significantly enhance runtime performance.
2. Reduced Code Size: Eliminating redundant or unnecessary instructions can lead to a smaller
code footprint, which is especially beneficial in memory-constrained environments.
3. Simplicity: Peephole optimization algorithms are relatively simple to implement and can provide
immediate improvements without extensive analysis.
1. Local Scope: Peephole optimization only looks at small sections of code, so it might miss
opportunities for optimization that require a broader context.
2. Diminishing Returns: There is a limit to how much can be achieved through peephole
optimization alone. More significant improvements often require more sophisticated, global
optimization techniques.
3. Architecture Dependence: Optimizations that are beneficial on one architecture may not apply
to another, requiring different sets of patterns and rules for different targets.
Conclusion
When generating code for control flow statements, the compiler often encounters situations
where it needs to emit a jump instruction to a label or address that has not yet been determined.
This typically happens in the case of:
Backpatching allows the compiler to generate the jump instructions with a placeholder and later
fill in the correct target addresses once they are known.
Characteristics of DAG
1. Directed: Each edge has a direction, indicating a one-way relationship from one node to another.
2. Acyclic: There are no cycles, meaning no path leads back to its starting point.
3. Vertices and Edges: Consists of vertices (or nodes) and edges (or arcs) that connect the vertices.
Applications of DAG
DAGs have numerous applications across various fields, particularly in computer science, data
processing, and optimization problems. Here are some key applications in detail:
1. Compiler Design
Task Scheduling: DAGs are used to model tasks in a project where some tasks depend on the
completion of others. Each node represents a task, and edges represent dependencies.
Topological Sorting: DAGs allow for topological sorting, which is crucial in scheduling tasks such
that all dependencies are respected. This ensures that a task is only performed after all its
prerequisite tasks are completed.
Example: In project management, tasks can be represented as a DAG where tasks must be
completed in a certain order. A topological sort of the DAG provides a feasible schedule.
Workflow Management: In data processing pipelines and workflow systems, DAGs are used to
define the sequence of processing steps.
Data Dependencies: Each node represents a data processing step, and edges represent the flow
of data from one step to another. This ensures that each step receives the data it needs from
preceding steps.
Example: Apache Airflow uses DAGs to manage and schedule complex workflows. Each task in
the workflow is a node, and dependencies between tasks are directed edges.
Versioning and Merging: In version control systems like Git, DAGs are used to represent the
history of changes.
Commit History: Each commit is a node, and edges represent the parent-child relationship
between commits. This allows for efficient management of branching and merging.
Example: In Git, the commit history forms a DAG, where branches and merges are handled
efficiently without creating cycles.
5. Dependency Resolution
Example: Package managers like npm (for Node.js) use DAGs to resolve and install package
dependencies in the correct order.
Network Routing: In computer networks, DAGs can be used to model and optimize routing
paths.
Shortest Path: Algorithms like Dijkstra’s or Bellman-Ford can be used on DAGs to find the
shortest path from one node to another efficiently.
Example: In communication networks, routing protocols may use DAGs to ensure data packets
follow the optimal path without creating routing loops.
7. Game Development
Game State Management: DAGs are used to manage game states and transitions.
State Transitions: Each node represents a game state, and edges represent transitions between
states based on player actions or game events.
Example: In a game, the progression from one level to another or different game modes can be
represented as a DAG, ensuring a logical flow of states.
Conclusion
DAGs are a fundamental data structure with wide-ranging applications in computer science and
related fields. Their acyclic nature and directed edges make them suitable for modeling
dependencies, scheduling tasks, optimizing expressions, managing versions, and resolving
dependencies. By ensuring no cycles, DAGs facilitate efficient and logical progressions through
nodes, making them invaluable in both theoretical and practical contexts.