Intermediate Topic 27 of 50 Languages & Compilers
Three-Address Code
Simple IR with at most three operands per instruction.
How it works
Three-address code is an intermediate representation in which each instruction has at most three operands, typically a result and two sources, as in t1 = a + b. Complex expressions are broken into sequences of simple instructions using temporaries, and control flow is expressed with labels and jumps. It resembles assembly while staying machine independent, which makes optimization, analysis and instruction selection straightforward. Quadruples, triples and indirect triples are storage formats.
Step-by-step walkthrough
- Complex expressions are broken into simple steps.
- Each instruction has at most one operator and three addresses.
- t1 holds c times d.
- t2 adds b.
- It is easy to analyze and optimize.
Where it is used
Compilers translate expression trees into three-address code to apply constant folding, common subexpression elimination, dead code elimination and register allocation.
Common pitfall
Naively generated code has many redundant temporaries and copies that optimization must clean up. Handling short-circuit boolean expressions, array indexing and function calls needs care. Without SSA form, analyses must track variable redefinitions. Use consistent temporary naming and a control flow graph for global analysis.
Where it fits in the course
Three-Address Code is topic 27 of 50 in Languages & Compilers, rated Intermediate. It follows Intermediate Representations. The next topic is Static Single Assignment.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Languages & Compilers quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Languages & Compilers quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Three-Address Code?
Where do people use Three-Address Code?
What is the most common mistake with Three-Address Code?
What should I learn before this topic?
How can I practice it?
Is Three-Address Code hard to learn?
How long does it take to learn Three-Address Code?
Which topics are related to Three-Address Code?
What should I study after Three-Address Code?
Related topics
- Intermediate Representations (Languages & Compilers)
- Static Single Assignment (Languages & Compilers)
- Parser Generators (Languages & Compilers)
- Control Flow Graphs (Languages & Compilers)
- Code Style and Readability (Software Engineering)
- Code Reviews (Software Engineering)
- Three-Way Handshake (Computer Networks)