Intermediate Code Generation
Intermediate Code Generation
By Bishnu Gautam 1
Compiler Construction Intermediate-Code-Generation
Intermediate Target
Front end code Back end machine code
Intermediate codes are machine independent codes, but they are close
to machine instructions.
By Bishnu Gautam 2
Compiler Construction Intermediate-Code-Generation
Intermediate Representations
By Bishnu Gautam 3
Compiler Construction Intermediate-Code-Generation
Three-Address Statements
• Assignment statements: x = y op z, op is binary
• Assignment statements: x = op y, op is unary
• Indexed assignments: x = y[i], x[i] = y
• Pointer assignments: x = &y, x = *y, *x = y
• Copy statements: x = y
• Unconditional jumps: goto label
• Conditional jumps: if x relop y goto label
• Function calls: param x… call p, n return y
By Bishnu Gautam 5
Compiler Construction Intermediate-Code-Generation
Code generation
t3 = t1 + t2
By Bishnu Gautam 6
Compiler Construction Intermediate-Code-Generation
Syntax-Directed Translation into
Three-Address Code
Productions Semantic rules
S → id = E S.code = E.code || gen(id.place ‘=’ E.place); S.begin = S.after = nil
S → while E (see next slide)
do S1 Returns a new temporary name
E → E1 + E2 E.place = newtemp();
E.code = E1.code || E2.code || gen(E.place ‘=’ E1.place ‘+’ E2.place)
E → E1 * E2 E.place = newtemp();
E.code = E1.code || E2.code || gen(E.place ‘=’ E1.place ‘*’ E2.place)
E → - E1 E.place = newtemp();
E.code = E1.code || gen(E.place ‘=’ ‘uminus’ E1.place)
E → ( E1 ) E.place = E1.place
E.code = E1.code
E → id E.place = id.name
E.code = ‘’
E → num E.place = newtemp();
E.code = gen(E.place ‘=’ num.value)
By Bishnu Gautam 7
Compiler Construction Intermediate-Code-Generation
t1 = 2
t2 = t1 * n
t3 = t2 + k
i = t3
L1: if i = 0 goto L2
t4 = i - k
i = t4
goto L1
L2: By Bishnu Gautam 9
Compiler Construction Intermediate-Code-Generation
By Bishnu Gautam 14
Compiler Construction Intermediate-Code-Generation
Translation Schemes for
Assignments Statements
S→S;S
S → id := E
{ p := lookup(top(tblptr), id.name);
if p = nil then
error()
else if p.level = 0 then // global variable
emit(id.place ‘:=’ E.place)
else // local variable in subroutine frame
emit(fp[p.offset] ‘:=’ E.place) }
By Bishnu Gautam 15
Compiler Construction Intermediate-Code-Generation
Translation Schemes for
Expressions
E → E1 + E2 { E.place := newtemp();
emit(E.place ‘:=’ E1.place ‘+’ E2.place) }
E → E1 * E2 { E.place := newtemp();
emit(E.place ‘:=’ E1.place ‘*’ E2.place) }
E → - E1 { E.place := newtemp();
emit(E.place ‘:=’ ‘uminus’ E1.place) }
E → ( E1 ) { E.place := E1.place }
E → id { p := lookup(top(tblptr), id.name);
if p = nil then error()
else if p.level = 0 then // global variable
E.place := id.place
else // local variable in frame
E.place := fp[p.offset] } … …->
By Bishnu Gautam 16
Compiler Construction Intermediate-Code-Generation
Translation Schemes for
…… Expressions
E → E1 ^ { E.place := newtemp();
emit(E.place ‘:=’ ‘*’ E1.place) }
E → & E1 { E.place := newtemp();
emit(E.place ‘:=’ ‘&’ E1.place) }
E → id1 . id2 { p := lookup(top(tblptr), id1.name);
if p = nil or p.type != Trec then error()
else
q := lookup(p.type.table, id2.name);
if q = nil then error()
else if p.level = 0 then // global variable
E.place := id1.place[q.offset]
else // local variable in frame
E.place := fp[p.offset+q.offset] }
By Bishnu Gautam 17
Compiler Construction Intermediate-Code-Generation
t1 := c // c = baseA - 10 * 4
t2 := i * 4
t3 := t1[t2] By Bishnu Gautam 18
… := t3
Compiler Construction Intermediate-Code-Generation
if a < b goto L1
a<b t1 := 0
goto L2
L1: t1 := 1
L2:
call foo(a+1, b, 7) t1 := a + 1
t2 := 7
param t1
param b
param t2
By Bishnu Gautam 21
call foo 3
Compiler Construction Intermediate-Code-Generation
Exercise
8.1(c), 8.2 (a), 8.3(c), 8.7, 8.12 & 8.14 from book
By Bishnu Gautam 22