Académique Documents
Professionnel Documents
Culture Documents
v A variety of intermediate representations are used in compilers v Most common intermediate representations are:
- Abstract - Directed
- Three-Address - Code
it is not linear and does not resemble target code constructs, such as if and while, should be translated into jumps
- 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:
representing all operations as abstractly as a syntax tree closely resembling target code
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 Compiler Design Muhammed Mudawwar
v Unary op instructions are of the form: x := op y op can be PLUS, MINUS, or NOT operator
Unary Operator
(2+a*(bc/d))/e
operator code pointers to operand symbols result symbol, goto target, or label number
- Two
- A pointer to
v A code sequence can be implemented as an array or a linked list v A linked list is preferable because it
- Facilitates - Grows
dynamically as required
+ operators will concatenate code sequences and instructions - The result of concatenation is a code sequence
Code Code Code Code 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
E E1 addop E2
Semantic Rules
E.sym := newtemp(); AddInst := new Inst(addop.op, E.sym, E1.sym, E2.sym); E.code := E1.code + E2.code + AddInst ; E.sym := newtemp(); MulInst := new Inst(mulop.op, E.sym, E1.sym, E2.sym); E.code := E1.code + E2.code + MulInst; E.sym := newtemp(); UnaryInst := new Inst(UnaryOp.op, E.sym, E1.sym); E.code := E1.code + UnaryInst ;
Compiler Design Muhammed Mudawwar
E E1 mulop E2
E UnaryOp E1
Grammar Rules
E ( E1 ) E id E num UnaryOp addop UnaryOp not
Intermediate Code Generation 13
Semantic Rules
E.sym := E1.sym; E.code := E1.code; E.sym := idTable.lookup(id.name ); E.code.first := E.code.last = 0; E.sym := num.sym; E.code.first := E.code.last = 0; if addop.op = ADD then UnaryOp.op := PLUS else UnaryOp.op := MINUS UnaryOp.op := NOT
Compiler Design Muhammed Mudawwar
IFNOT E.sym goto Else SList 1.code GOTO Next LABEL Else SList 2.code LABEL Next
LABEL Next
Grammar Rules
S if E then Slist end ;
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 ;
Compiler Design Muhammed Mudawwar
E.code IFNOT E.sym goto Next SList.code GOTO Expr LABEL Next
Grammar Rules
Semantic Rules
S while E do Slist end ; next := newlabel(); Expr := newlabel(); GotoExpr := new Inst(GOTO, Expr); IfNotNext := new Inst(IFNot, next, E.sym); S.code := Expr + E.code + IfNotNext+ Slist.code+ GotoExpr + next; Slist Slist1 S Slist.code := Slist 1.code + S.code; Slist Slist.code.first := 0; Slist.code.last := 0;
Intermediate Code Generation 17 Compiler Design Muhammed Mudawwar