Mr. Sachin Jain delivers AKGEC’s lecture 03 on Compiler Design, focusing on ”Three Address Code.”

In the lecture on "Three Address Code" by Mr. Sachin Jain, he emphasizes the simplicity and power of three address code in compiler design. Each instruction combines assignment and a binary operator, with at most three operands. The forms include assignments like X = Y operator Z, procedure calls, and array operations. Data structures like quadruples, triples, and indirect triples organize the code, each with its unique features. Quadruples include four fields: operator, argument1, argument2, and result. Triples lack a result field and use references to temporary values. Indirect triples further reference these temporary values. Understanding these structures is key to mastering compiler design.

This article provides an in-depth overview of "Three Address Code" in Compiler Design, as discussed in Lecture 03 by Mr. Sachin Jain at AKGEC. It delves into the concepts, various forms, and data structures associated with Three Address Code, elucidating with examples throughout.

πŸ“ Three Address Code Overview

In Compiler Design, Three Address Code (3AC) serves as an intermediate representation of code, facilitating optimization and translation processes. It comprises instructions with a maximum of three operands, typically involving assignment and binary operators.

Three Address Code Structure

Each 3AC instruction features an assignment statement and a binary operator, with at most one operator on the right-hand side. Examples include assignments, conditional statements, procedure calls, and array and pointer operations.

InstructionDescription
X = Y operator JAssignment with binary operator
X = operator YAssignment with unary operator (optional second operand)
X = to YSimple assignment
Go to LUnconditional jump
If X go to L, else go to MConditional jump
X reloop Y, go to LLoop statement
Procedure calls using parametersFunction calls with parameters
Y = call procedure P and NCalling a procedure with return value and parameters
X = to Y index IArray assignment with index
Pointer and storage operationsOperations involving pointers and storage locations

πŸ› οΈ Example Conversion

To illustrate the conversion process, consider the instruction a = -C * b + -C * D. Converting it to 3AC involves breaking it down into simpler operations using temporary variables.

Conversion Steps

  1. Assign -C to temporary variable T1.
  2. Compute B * T1 and store in T2.
  3. Assign -C to another temporary variable T3.
  4. Compute D * T3 and store in T4.
  5. Compute T2 + T4 and store in T5.
  6. Assign T5 to variable a.
StepThree Address Code
1T1 = -C
2T2 = B * T1
3T3 = -C
4T4 = D * T3
5T5 = T2 + T4
6a = T5

πŸ“Š Data Structures for Three Address Code

Three main data structures for organizing 3AC include Quadruple, Triple, and Indirect Triple. These structures vary in their representation of instructions and temporary variables.

Data Structure Comparison

StructureDescription
QuadrupleOperator, argument 1, argument 2, result
TripleOperator, argument 1, argument 2
Indirect TripleTriple with separate reference list

πŸ”„ Conversion Examples

Converting the example instruction B * -C + B * -C into Quadruple, Triple, and Indirect Triple structures demonstrates their differences in representation.

Quadruple

  1. -C is assigned to temporary T1.
  2. B * T1 is computed and stored in T2.
  3. -C is assigned to temporary T3.
  4. D * T3 is computed and stored in T4.
  5. T2 + T4 is computed and stored in T5.
  6. a is assigned T5.
Quadruple
1T1 = -C
2T2 = B * T1
3T3 = -C
4T4 = D * T3
5T5 = T2 + T4
6a = T5

Triple

  1. -C is assigned to temporary T1.
  2. B * T1 is computed and stored in T2.
  3. -C is assigned to temporary T3.
  4. D * T3 is computed and stored in T4.
  5. T2 + T4 is computed.

Reference Table:

0: T1, 1: T2, 2: T3, 3: T4

Indirect Triple

  1. -C is assigned to temporary T1.
  2. B * T1 is computed and stored in T2.
  3. -C is assigned to temporary T3.
  4. D * T3 is computed and stored in T4.
  5. T2 + T4 is computed.

Reference Table:

0: 1, 1: 2, 2: 3, 3: 4

Conclusion

Understanding Three Address Code and its associated data structures is fundamental in Compiler Design. Through proper conversion techniques, programmers can optimize code and enhance compiler efficiency.

πŸ“Œ Key Takeaways

  • Three Address Code serves as an intermediate representation in Compiler Design.
  • Conversion involves breaking down complex instructions into simpler operations.
  • Quadruple, Triple, and Indirect Triple are three main data structures for organizing Three Address Code.
  • Each structure has its unique representation and usage in compiler optimization.

πŸ€” FAQ

Q: How many operands can a Three Address Code instruction have?
A: A Three Address Code instruction can have at most three operands.

Q: What are the main data structures used for organizing Three Address Code?
A: The main data structures are Quadruple, Triple, and Indirect Triple.

Q: What is the purpose of using Three Address Code in Compiler Design?
A: Three Address Code serves as an intermediate representation for facilitating optimization and translation processes in compiler design.

Q: Can you explain the difference between Quadruple, Triple, and Indirect Triple?
A: Quadruple includes four fields: operator, argument 1, argument 2, and result. Triple lacks the result field and uses references to temporary variables. Indirect Triple further uses a separate reference list to access temporary variables indirectly.

This comprehensive guide elucidates the intricacies of Three Address Code, providing valuable insights into its structure, conversion techniques, and data structures. By mastering these concepts, developers can effectively optimize compilers and enhance code efficiency.

About the Author

Indian Inst of Management and Engg Soc
16.2K subscribers

About the Channel:

Ajay Kumar Garg Engineering College (AKGEC), Ghaziabad is affiliated to Dr. A.P.J. AKTU, Lucknow, and is approved by the AICTE. The college was established in 1998 and offers B.Tech courses in nine disciplines of Engineering. At the post graduate level, the College offers M.Tech and Master of Computer Applications course.HIGHLIGHTS: 1. Recipient of Academic Excellence Trophy for β€œBest Engineering College in UPTU” for two successive years from H.E., The Governor of U.P. 2. The only Institution in U.P. to have received approval from DST, Govt. of India, for establishment of Centre of Relevance and Excellence (CORE) in the field of Industrial Automation & Robotics. 3. Skill Development Centre established in partnership with National Skill Development Corporation (NSDC) 4. Best Robolab Setup Award 2020 by All India Council for Robotics & Automation (AICRA) 5. Best Private Engineering College in North India Award – 2018 during 4th Dialogue India Academia Conclave
Share the Post:
en_GBEN_GB