Course contentsShow
← Computer Science
Lesson 2051 of 2,87246. Compilation and InterpretationFree lesson

Three-Address Code Translation

Converting three-address intermediate code into assembly involves mapping virtual operations to real instructions.

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:

  1. Match operations to instructions: A TAC operation like t1 = a + b becomes an ADD instruction in the target architecture
  2. Assign registers: Variables like a, b, and t1 need physical CPU registers or memory locations
  3. Handle addressing modes: Different architectures access memory differently
  4. 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.