Three-Address Code Translation
What you'll learn: How compilers convert three-address code intermediate representation into real assembly instructions for a target processor.
What Is Three-Address Code Translation?
You've already learned that three-address code (TAC) is a simple intermediate representation where each instruction has at most one operator and three operands. Now comes the crucial step: translating those abstract operations into actual machine instructions that a CPU can execute.
Think of TAC as a recipe written in universal cooking language, and assembly as instructions for a specific brand of oven. Translation is the process of adapting those universal steps to work with your particular hardware.
The Translation Process
Each TAC instruction maps to one or more assembly instructions. The compiler must:
- Match operations to instructions: A TAC operation like
t1 = a + bbecomes an ADD instruction in the target architecture - Assign registers: Variables like
a,b, andt1need physical CPU registers or memory locations - Handle addressing modes: Different architectures access memory differently
- Respect instruction constraints: Some CPUs require operands in specific registers or formats
For example, a simple TAC sequence:
t1 = x + 5
y = t1
Might translate to x86 assembly as:
MOV eax, [x] ; load x into register eax
ADD eax, 5 ; add 5 to eax
MOV [y], eax ; store result in y
Architecture Matters
Different target architectures have different instruction sets, register counts, and addressing modes. The translator must understand these constraints and generate appropriate code for each platform.
Key Takeaway: Three-address code translation bridges the gap between architecture-independent intermediate code and architecture-specific assembly by mapping abstract operations to real machine instructions while respecting hardware constraints.