# Intermediate Code Generation Intermediate Code Generation Intermediate languages

• Slides: 32

Intermediate Code Generation

Intermediate Code Generation Intermediate languages l Declarations l Expressions l Statements l 2

Intermediate Languages l Syntax tree a : = b * - c + b * - c : = a + * b * - b c l Postfix notation a b c - * + : = 3 l Three-address code c

Three-Address Code x : = y op z where x, y, z are names, constants, or temporaries x+y*z t 1 : = y * z t 2 : = x + t 1 4 a : = b * -c + b * -c t 1 : = -c t 2 : = b * t 1 t 3 : = -c t 4 : = b * t 3 t 5 : = t 2 + t 4 a : = t 5

Types of Three-Address Code l l l 5 Assignment statement Copy statement Unconditional jump Conditional jump Procedural call x : = y op z x : = op y x : = y goto L if x relop y goto L param x call p return y

Types of Three-Address Code l l 6 Indexed assignment x : = y[i] x[i] : = y Address and pointer assignment x : = &y x : = *y *x : = y

Implementation of Three-Address Code l 7 Quadruples op (0) (1) * (2) (3) * (4) + (5) : = arg 1 c b t 2 t 5 arg 2 t 1 t 3 t 4 result t 1 t 2 t 3 t 4 t 5 a

Implementation of Three-Address Code l Triples (0) (1) (2) (3) (4) (5) 8 op * * + : = arg 1 c b (1) a arg 2 (0) (2) (3) (4)

Implementation of Three-Address Code l 9 Indirect Triples statement (0) (14) (15) (2) (16) (3) (17) (4) (18) (5) (19) op * * + : = arg 1 c b (15) a arg 2 (14) (16) (17) (18)

Comparison l Qualdruples – – l Triples – l space efficiency Indirect Triples – – 10 direct access of the location for temporaries easier for optimization space efficiency

New Names and Labels l l 11 Function newtemp returns a new name for each call Function newlabel returns a new label for each call

Assignments S id “: =” E {emit(id. name ‘: =’ E. place); } E E 1 “+” E 2 {E. place : = newtemp; emit(E. place ‘: =’ E 1. place ‘+’ E 2. place); } E E 1 “*” E 2 {E. place : = newtemp; emit(E. place ‘: =’ E 1. place ‘*’ E 2. place); } E “-” E 1 {E. place : = newtemp; emit(E. place ‘: =’ ‘-’ E 1. place); } E “(” E 1 “)” {E. place : = E 1. place} E id {E. place : = id. name} 12

Array Accesses A[i]: base + (i - low) w (i w) + (base - low w) A[i 1, i 2]: base + ((i 1 - low 1) n 2 + i 2 - low 2) w (((i 1 n 2) + i 2) w) + (base - ((low 1 n 2) + low 2) w) 13 c(id. place), width(id. place), limit(id. place, i)

Array Accesses 14 l Use inherited attributes L id “[” Elist “]” | id Elist “, ” E | E l Use synthesized attributes L Elist “]” | id Elist “, ” E | id “[” E

Array Accesses Elist id “[” E { Elist. place : = E. place; Elist. ndim : = 1; Elist. array : = id. place; } Elist 1 “, ” E { t : = newtemp; m : = Elist 1. ndim + 1; emit(t ‘: =’ Elist 1. place ‘*’ limit(Elist 1. array, m)); emit(t ‘: =’ t ‘+’ E. place); Elist. array : = Elist 1. array; Elist. place : = t; Elist. ndim : = m; } 15

Array Accesses L Elist “]” { L. place : = newtemp; L. offset : = newtemp; emit(L. place ‘: =’ c(Elist. array)); emit(L. offset ‘: =’ Elist. place ‘*’ width(Elist. array)) } L id { L. place : = id. place; L. offset : = null } 16

Array Accesses 17 E L { if L. offset = null then E. place : = L. place; else begin E. place : = newtemp; emit(E. place ‘: =’ L. place ‘[’ L. offset ‘]’); end } S L “: =” E { if L. offset = null then emit(L. place ‘: =’ E. place); else emit(L. place ‘[’ L. offset ‘]’ ‘: =’ E. place); }

An Example x : = A[y, z] n 1 = 10, n 2 = 20, w = 4 c = base. A - ((1 20) + 1) 4 = base. A - 84 18 t 1 t 2 t 3 t 4 x : = : = : = y * 20 t 1 + z c t 1 * 4 t 2[t 3] t 4

Type Conversion E E 1 + E 2 {E. place : = newtemp; if E 1. type = int and E 2. type = int then begin emit(E. place ‘: =’ E 1. place ‘int+’ E 2. place); E. type : = int; end else if E 1. type = real and E 2. type = real then begin emit(E. place ‘: =’ E 1. place ‘real+’ E 2. place); E. type : = real; end else if E 1. type = int and E 2. type = real then begin u : = newtemp; emit(u ‘: =’ ‘inttoreal’ E 1. place); emit(E. place ‘: =’ u ‘real+’ E 2. place); E. type : = real; end else if … 19 }

Flow-of-Control Statements S | | | 20 if E then S 1 else S 2 while E do S 1 switch E begin case V 1: S 1 case V 2: S 2 … case Vn-1: Sn-1 default: Sn end

Conditional Statements E. code E. true: E. false: 21 S 1. code E. true E. false S if E then S 1 { E. true : = newlabel; E. false : = S. next; S 1. next : = S. next; S. code : = E. code || gen(E. true ‘: ’) || S 1. code }

Conditional Statements E. code E. true: S 1. code goto S. next E. false: 22 S. next: S 2. code S if E then S 1 else S 2 E. true { E. true : = newlabel; E. false S 1. next : = S. next; S 2. next : = S. next; S. code : = E. code || gen(E. true ‘: ’) || S 1. code || gen(‘goto’ S. next) || gen(E. false ‘: ’) || S 2. code }

Loop Statements S. begin: E. true: E. code S 1. code goto S. begin E. false: 23 S while E do S 1 { S. begin : = newlabel; E. true E. false : = S. next; E. false S 1. next : = S. begin; S. code : = gen(S. begin ‘: ’) || E. code || gen(E. true ‘: ’) || S 1. code || gen(‘goto’ S. begin) }

Boolean Expressions 24 E E 1 or E 2 {E 1. true : = E. true; E 1. false : = newlabel; E 2. true : = E. true; E 2. false : = E. false; E. code : = E 1. code || gen(E 1. false ‘: ’) || E 2. code; } E E 1 and E 2 {E 1. true : = newlabel; E 1. false : = E. false; E 2. true : = E. true; E 2. false : = E. false; E. code : = E 1. code || gen(E 1. true ‘: ’) || E 2. code; } E not E 1 {E 1. true : = E. false; E 1. false : = E. true; E. code : = E 1. code; }

Boolean Expressions 25 E “(” E 1 “)” { E 1. true : = E. true; E 1. false : = E. false; E. code : = E 1. code; } E id 1 relop id 2 { E. code : = gen(‘if’ id 1. place relop. op id 2. place ‘goto’ E. true) || gen(‘goto’ E. false); } E true { E. code : = gen(‘goto’ E. true); } E false { E. code : = gen(‘goto’ E. false); }

An Example a < b or c < d and e < f 26 if a < b goto Ltrue goto L 1: if c < d goto L 2 goto Lfalse L 2: if e < f goto Ltrue goto Lfalse

An Example while a < b do if c < d then x : = y + z else x : = y - z 27 Lbegin: if a < b goto L 1 goto Lnext L 1: if c < d goto L 2 goto L 3 L 2: t 1 : = y + z x : = t 1 goto Lbegin L 3: t 2 : = y - z x : = t 2 goto Lbegin Lnext:

Case Statements l l l 28 Conditional goto’s – less than 10 cases Jump table – more than 10 cases – dense value range Hash table – more than 10 cases – sparse value range

Conditional Goto’s code to evaluate E into t goto test L 1: code for S 1 goto next … Ln-1: code for Sn-1 goto next Ln: code for Sn goto next 29 test: if t = V 1 goto L 1 … if t = Vn-1 goto Ln next:

Jump Table code to evaluate E into t if t < Vmin goto Ldefault if t > Vmax goto Ldefault i : = t - Vmin L : = jump. Table[i] goto L 30

Hash Table code to evaluate E into t i : = hash(t) L : = hash. Table[i] goto L 31

Procedure Calls S call id “(” Elist “)” { for each item p on queue do emit(‘param’ p); emit(‘call’ id. place); } 32 Elist “, ” E { append E. place to the end of queue; } Elist E { initialize queue to contain only E. place; }