next up previous
Next: Boolean Expressions and Control Flow Up: Compiler Theory: Intermediate Code Generation Previous: Abstract Stack Machines

Three-Address Code

THREE-ADDRESS CODES are a form of IR similar to assembler for an imaginary machine. Each three address code instruction has the form

x := y op z
Observe that


THREE-ADDRESS STATEMENTS are similar to assembly code. We will consider the following statements.

Assignments.
Two possible forms:
Copy statements.
They have the form x := y.
Inconditional jumps.
They have the form
goto L
where L is a symbolic label of a statement.
inconditional jumps.
They have the form
if x relop y goto L
where statement L is executed if x and y are in relation relop.
Procedure calls.
They have the form
param x1 
param x2

param xn 
call p, n
corresponding to the procdure call p(x1, x2, ..., xn)
Return statement.
They have the form return y where y representing a returned value is optional.
Indexed assignments.
They have the form x := y[i] or x[i] := y.
Address assignments.
They have the form x := &y which sets x to the location of y.
Pointer assignments.
They have the form


SYNTAX-DIRECTED TRANSLATION INTO 3-ADDRESS CODE. We give below a S-definition generating 3-address code from assignments. We use the following attributes

The function newtemp returns a sequence of distinct names and | | is used to denote the concatenation of strings.
Production Semantic Rule
S $ \longmapsto$ $ \bf id$ : = E S.code := E.code | | generate( $ \bf id$.place, ':=', E.place)
E $ \longmapsto$ E1 + E2 E.place := newtemp
  code := generate(E.place, ':=', E1.place, '+', E2.place)
  E.code := E1.code | | E2.code | | code
E $ \longmapsto$ E1*E2 E.place := newtemp
  code := generate(E.place, ':=', E1.place, '*', E2.place)
  E.code := E1.code | | E2.code | | code
E $ \longmapsto$ - E1 E.place := newtemp
  code := generate(E.place, ':=', ' $ \bf unimus$', E1.place)
  E.code := E1.code | | code
E $ \longmapsto$ (E1) E.place := E1.place
  E.code := E1.code
E $ \longmapsto$ $ \bf id$ E.place := $ \bf id$.place
  E.code := ''
Observe that


next up previous
Next: Boolean Expressions and Control Flow Up: Compiler Theory: Intermediate Code Generation Previous: Abstract Stack Machines
Marc Moreno Maza
2004-12-02