Professional Documents
Culture Documents
Intermediate Code
Intermediate Code
Syntax Tree
- Directed
- Three-Address
- Code
Code
- High-level
Three-Address Code
v Generalized assembly code for a virtual 3-address machine
v 3-address code represents a linearization of the syntax tree
v 3-address code can be:
- High level:
- Low level:
addresses for the two operands and one address for the result
- General form x := y op z
- op is an operator code
- x, y, and z are typically implemented as pointers to symbols
- x is either an identifier or a temporary symbol
- y and z can be an identifier, a literal, or a temporary symbol
Intermediate Code Generation 2
Unary Operator
(2+a*(bc/d))/e
t1 := c / d
t2 := b t1
t3 := a * t2
t4 := 2 + t3
t5 := t4 / e
v Compiler generates temporaries when translating into 3AC
v t1, t2, t3, t4, and t5 are generated temporaries
v Temporaries are identified by number
v Temporaries are stored in symbols, like identifiers
v Type information is also added to temporary symbols
Intermediate Code Generation 4
operator code
- Two
- A pointer to
dynamically as required
result
target
label
first
second
link
operator+(Code
operator+(Code
operator+(Inst*
operator+(Inst*
a,
c,
i,
i,
Code
Inst*
Code
Inst*
b);
i);
c);
j);
Compiler Design Muhammed Mudawwar
Grammar Rules
Semantic Rules
E E1 addop E2
E.sym := newtemp();
AddInst := new Inst(addop.op, E.sym, E1.sym, E2.sym);
E.code := E1.code + E2.code + AddInst;
E E1 mulop E2
E.sym := newtemp();
MulInst := new Inst(mulop.op, E.sym, E1.sym, E2.sym);
E.code := E1.code + E2.code + MulInst;
E UnaryOp E1
E.sym := newtemp();
UnaryInst := new Inst(UnaryOp.op, E.sym, E1.sym);
E.code := E1.code + UnaryInst;
Grammar Rules
Semantic Rules
E ( E1 )
E.sym := E1.sym;
E.code := E1.code;
E id
E.sym := idTable.lookup(id.name);
E.code.first := E.code.last = 0;
E num
E.sym := num.sym;
E.code.first := E.code.last = 0;
UnaryOp addop
UnaryOp not
UnaryOp.op := NOT
S.code
E.code
IFNOT E.sym goto Next
SList.code
LABEL Next
E.code
IFNOT E.sym goto Else
SList 1.code
GOTO Next
LABEL Else
SList 2.code
LABEL Next
Intermediate Code Generation 14
Grammar Rules
Semantic Rules
Next := newlabel();
IfNotNext := new Inst(IFNOT, Next, E.sym);
S.code := E.code + IfNotNext + Slist.code + Next;
Else := newlabel();
Next := newlabel();
IfNotElse := new Inst(IFNOT, Else, E.sym);
GotoNext := new Inst(GOTO, Next);
S.code := E.code + IfNotElse + Slist 1.code +
GotoNext + Else + Slist 2.code + Next;
LABEL Expr
E.code
IFNOT E.sym goto Next
SList.code
GOTO Expr
LABEL Next
Intermediate Code Generation 16
Grammar Rules
Semantic Rules