0% found this document useful (0 votes)
21 views6 pages

Intermediate Code Generation Concepts

NA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

Intermediate Code Generation Concepts

NA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT 3 – Intermediate Code Generation &

Syntax Directed Translation (SDT)


1. Define Inherited Attribute.
An inherited attribute is one whose value at a node in a parse tree is computed
using information passed from its parent node, the node itself, or its left siblings. It
is used to pass information downward or sideways in syntax-directed translation.
2. State the Rules of Type Checking.
Type checking ensures that the operands and operators in an expression are
compatible. The basic rules are:

 The operand types of an operator must be compatible.


 The result type of an expression must be properly defined.
 If needed, type coercion is applied to convert one type to another.

3. What is Meant by Type Checking?


Type checking is the process by which the compiler verifies that the program
follows the type rules of the language. It ensures data type consistency and prevents
invalid operations (e.g., adding an integer to a string).
4. What is Meant by Backpatching?
Backpatching is a code generation technique in which jump or branch instructions
are generated with unspecified targets. Later, when the correct target labels are
known, those addresses are patched or filled in.
5. What is the Use of a Three Address Code (TAC)?
Three Address Code (TAC) is an intermediate representation where each
instruction has at most three operands. It makes the execution order explicit,
simplifies code generation, and facilitates optimization.
6. What is Meant by Coercion?
Coercion refers to the implicit type conversion performed by the compiler to make
operand types compatible with each other, such as automatically converting an
integer to a float when required.
7. What are Synthesized and Inherited Attributes?

 Synthesized attributes compute information upward from child nodes to parent


nodes.
 Inherited attributes compute information downward from parent to children or
sideways from left siblings.
These attributes help in defining syntax-directed definitions (SDDs).

8. Explain Syntax Tree with an Example.


A Syntax Tree (or Abstract Syntax Tree – AST) represents the hierarchical
structure of a source program.
Each internal node represents an operator, and each leaf node represents an
operand.
Example: For the expression a = b * -c + b * -c, the root node is the assignment
operator (:=), with ‘a’ as the left child and the ‘+’ operator as the root of the right
subtree.
9. Write the Semantic Action for the Production Rule E → E1 or M E2.
This production uses backpatching for handling Boolean expressions.
Semantic Action:

 Merge true lists and false lists of E1 and E2.


 Use marker (M) to store the next instruction address for later backpatching.
 Generate conditional and unconditional jump code accordingly.

10. Convert the Statement x = a + (b * c) + (d * e) into Three Address Code and


Triples.
Three Address Code:
t1 = b * c
t2 = d * e
t3 = a + t1
t4 = t3 + t2
x = t4

Triples Representation:
(1) *, b, c
(2) *, d, e
(3) +, a, (1)
(4) +, (3), (2)
(5) =, x, (4)

11. Translate x = (a + b) * -c / d into Quadruples and Triples.


Quadruples:
(1) +, a, b, t1
(2) -, c, , t2
(3) /, t2, d, t3
(4) *, t1, t3, t4
(5) =, t4, , x

Triples:
(1) +, a, b
(2) -, c,
(3) /, (2), d
(4) *, (1), (3)
(5) =, (4), x

12. Draw the Syntax Tree for the Expression a = b * -c + b * -c.


The root node is the assignment operator (:=).
Left child: a
Right child: + operator
Both operands of ‘+’ are subtrees representing b * -c.
13. What is an L-Attribute? Give Example.
An L-attributed definition allows inherited attributes of a symbol Xi on the right-
hand side to depend only on:

 The attributes of the parent, and


 The attributes of symbols to its left (X1, X2, ..., Xi−1).
Example: Attribute grammars used in declaration propagation and type checking
are L-attributed.

14. Convert x = a * b + c * e into Three Address Code, Quadruples, and Indirect


Triples.
Three Address Code:
t1 = a * b
t2 = c * e
t3 = t1 + t2
x = t3

Quadruples:
(1) *, a, b, t1
(2) *, c, e, t2
(3) +, t1, t2, t3
(4) =, t3, , x

Indirect Triples:
P1 → (1) *, a, b
P2 → (2) *, c, e
P3 → (3) +, (1), (2)
P4 → (4) =, (3), x

⚙️UNIT 4 – Run-Time Environment &


Code Generation
1. What are Two Strategies for Dynamic Storage Allocation?
The two main strategies are Stack Allocation and Heap Allocation.
2. Write the Role of Activation Record.
An Activation Record (also called a stack frame) stores information required for
procedure execution, such as:

 Local variables
 Temporary values
 Return address
 Parameters and saved machine state

3. Define Static Allocation and Stack Allocation.

 Static Allocation: Memory is assigned at compile time, and variable addresses


remain fixed.
 Stack Allocation: Memory is managed dynamically at runtime using a stack
structure; activation records are pushed and popped during function calls.
4. List the Different Types of Storage Allocation Strategies.
The major strategies are:

 Static Allocation
 Stack Allocation
 Heap Allocation

5. When is a Variable Syntactically Live at a Point?


A variable is said to be live at a point if its current value will be used in the future
before being redefined.
6. How is the Liveness of a Variable Calculated?
Liveness is found using data flow analysis. A variable is live if there exists a path
from its definition to a use, and it is not redefined along that path.
7. What are Dangling References?
A dangling reference occurs when a memory location is freed but pointers still
refer to that invalid location, leading to undefined behavior.
8. What is the Use of Heap Memory? When is it Created and Used?
Heap memory is used for dynamic allocation during runtime. It is created when the
program requests memory dynamically (e.g., using malloc() in C) and is freed when
no longer needed.
9. Explain the Issues in Code Generator Design.
Main issues in code generation include:

 Instruction Selection – choosing efficient machine instructions


 Register Allocation – assigning variables to limited registers
 Memory Management – managing spills and reloads
 Evaluation Order – determining efficient order for expression computation

10. Explain Heap Management.


Heap Management handles allocation and deallocation of memory blocks in the
heap at runtime. It also deals with fragmentation, garbage collection, and efficient
space utilization.
11. How to Perform Register Assignment for Outer Loops?
For outer loops, registers are assigned to loop-invariant and frequently used
variables to minimize memory access. This helps in improving the execution
efficiency of loops.

🚀 UNIT 5 – Code Optimization


1. What are the Causes of Redundancy?
Redundancy arises due to:

 Common subexpressions
 Loop-invariant computations
 Partially redundant code
These can be removed to improve performance.
2. What is Copy Propagation?
Copy Propagation replaces occurrences of a variable that holds a copy of another
variable’s value (e.g., x = y) with the original variable itself (x → y), enabling dead
code elimination.
3. What is DAG? Give Example.
A Directed Acyclic Graph (DAG) represents expressions within a basic block.
It helps identify common subexpressions and eliminate redundancy.
Example: For a = b + c; d = b + c;, both expressions share the same node for b
+ c.
4. What is a Flow Graph? Give Example.
A Flow Graph (or Control Flow Graph) is a directed graph where:

 Nodes represent basic blocks.


 Edges represent possible control flow paths.
Example: A loop forms a backward edge in the flow graph.

5. What is a Basic Block?


A basic block is a sequence of consecutive statements with:

 One entry point (first statement).


 One exit point (last statement).
There are no jumps or branches in between.

6. Define Constant Folding.


Constant Folding is a compile-time optimization that evaluates constant
expressions and replaces them with their computed values, reducing runtime
computation.
7. Identify Optimizations Performed on a Peephole.
Peephole optimizations include:

 Redundant load/store elimination


 Constant folding
 Strength reduction (e.g., replacing x * 2 with x + x)
 Null sequence removal (e.g., a = a + 0)

8. Define Peephole.
A peephole is a small window of consecutive machine instructions used for localized
optimization.
9. Explain Peephole Optimization.
Peephole Optimization examines a small group of instructions and replaces them
with shorter or faster equivalents, improving execution efficiency without changing
semantics.
10. Difference Between Peephole Optimization and Normal Optimization.

 Peephole Optimization: Local, machine-dependent, limited to a few instructions.


 Normal Optimization: Global, machine-independent, and applied at intermediate
levels (e.g., loops, functions).

11. What are Global Common Subexpressions?


A global common subexpression is an expression computed in one basic block and
recomputed in another without operand redefinition in between. It can be computed
once and reused.
12. What are Control-Flow Constraints?
Control-flow constraints restrict optimization by preserving the execution order of
program statements as represented in the control flow graph.
13. Define DAG.
A Directed Acyclic Graph (DAG) is used to represent expressions in a basic block
and helps detect common subexpressions to reduce redundant computations.
14. Can DAG be Used for Optimization? Give Example.
Yes, DAGs are used for code optimization. For example, in the expression x = a +
b; y = a + b;, both computations are represented by a single DAG node for a + b.
15. Why are Quadruples Preferred Over Triples in an Optimizing Compiler?
Quadruples explicitly name temporary results, making it easier to reorder
statements during optimization. In triples, references depend on statement positions,
making reordering difficult.

You might also like