Code Generation
How to produce intermediate or
target code
2301373 Code Generation 2
Outline
 Intermediate code
 Three-address code
 P-code
 Code generation techniques
 Using attribute grammar
 Tree traversal
 Macro expansion
 Address Calculation
 Code generation for control statements
 Code generation for logical expressions
2301373 Code Generation 3
Intermediate Code
 Intermediate representation for programs
 Why intermediate code
 Reduce amount of work if optimization is
done for intermediate code
 Easy for retargeting compilers
 Forms of intermediate code
 Abstract syntax tree
 Linearization of abstract syntax tree
2301373 Code Generation 4
Three-address code: Form of Instructions
 x = y op z
 x, y, and z are addresses of
 Variables (perhaps temporaries)
 Locations in programs
 y and z must be differed from x
 Operators can be:
 Arithmetic operators (3-address)
 Relational operators (3-address)
 Conditional operators (2-address)
 If_false … goto …
 Jump (1-address)
 Input/Output
 Halt
 Labels can be assigned to a location
2301373 Code Generation 5
Example of 3-address Code
read (x);
if x>0 then
{ fact:=1;
repeat
{ fact:=fact*x;
x:=x-1;
}
until x==0;
write(fact);
}
read x
t1=x>0
if_false t1 goto L1
fact=1
label L2
t2=fact*x
fact=t2
t3=x-1
x=t3
t4=x==0
if_false t4 goto L2
write fact
label L1
halt
2301373 Code Generation 6
P-Code
 Code for a hypothetical stack machine
 For Pascal compilers
 No variable name is required
 Instructions
 Load stack
 Arithmetic and relational operators
 Jumps
 Operations are performed on topmost
values on stack
 0-address or 1-address instructions
2301373 Code Generation 7
P-code Instruction
 Load: push stack
 Load value
 Load address
 Load constant
 Store: save top of
stack in memory
 Destructive store
 Nondestructive store
 Arithmetic operations
 Add
 Subtract
 Multiply
 Compare
 Greater
 Less
 equal
 Label
 Jump
 Unconditional jump
 Conditional jump
 I/O
 Read
 write
 Stop
2301373 Code Generation 8
Example of P-code
read (x);
if x>0 then
{ fact:=1;
repeat
{
fact:=fact*x;
x:=x-1;
}
until x==0;
write(fact);
}
loada x
read
loadv x
loadc 0
greater
jumpONfalse L1
loada fact
loadc 1
store
label L2
loada fact
loadv fact
loadv x
mult
store
loada x
loadv x
loadc 1
sub
store
loadv x
loadc 0
equ
jumpF L2
loadv fact
write
label L1
stop
2301373 Code Generation 9
Code Generation Using Synthesizes Attributes
 An attribute is created for the sequence
of characters representing generated
code.
 An attribute grammar is written to
generate the intermediate/target code.
 The value of the attribute is passed from
child nodes upto their parent node to
construct a larger chunk of code
2301373 Code Generation 10
Attribute Grammar for P-code
Grammar Rules
exp1 -> id = exp2
exp -> aexp
aexp1 ->aexp2 + f
actor
aexp -> factor
factor -> ( exp )
factor -> num
factor -> id
Semantic Rules
exp1.code =“loada” || id.strval +
+ exp2.code ++ ”stn”
exp.code = aexp.code
aexp1 .code = aexp2 .code ++ fac
tor.code ++ “add”
aexp.code = factor.code
factor.code = exp.code
factor.code = “loadc ” || num.strv
al
factor.code = “loadv ” || id.strval
2301373 Code Generation 11
Generating P-Code:Example
exp
=
id
exp
exp
aexp
aexp factor
+
factor
( )
aexp
aexp factor
+
factor
num
id
num
loada x
loadv x
loadc 3
adi
stn
loadc 4
adi
loadc 4
loada x
loadv x
loadc 3
adi
stn
loadv x
loadc 3
adi
loadv x loadc 3
loadv x
loadv x
loadc 3
adi
loada x
loadv x
loadc 3
adi
stn
loada x
loadv x
loadc 3
adi
stn
loada x
loadv x
loadc 3
adi
stn
loadc 4
adi
2301373 Code Generation 12
Attribute Grammar for 3-address Code
Grammar Rules
exp1-> id = exp2
exp -> aexp
aexp1->aexp2+fac
tor
aexp -> factor
factor -> ( exp )
factor -> num
factor -> id
Semantic Rules
exp1.name = exp2.name;
exp1.code=exp2.code++id.strval||”=“||exp2.name
exp.name = aexp.name; exp.code = aexp.code
exp1.name = newtemp();
aexp1.code=aexp2.code++factor.code++ aexp1.
name||”=“||aexp2 .name||”+“||factor.name
aexp.name=factor.name;aexp.code=factor.cod
e
factor.name =exp.name; factor.code = exp.cod
e
factor.name = num.strval; factor.code = “”
factor.name = id.strval; factor.code = “”
2301373 Code Generation 13
Generating 3-address Code:Example
exp
=
id
exp
exp
aexp
aexp factor
+
factor
( )
aexp
aexp factor
+
factor
num
id
num
x
4
x
t1
t1=x+3
3
t1
t1=x+3
t1
t1=x+3
x=t1
t1
t1=x+3
x=t1
t1
t1=x+3
x=t1
t2
t1=x+3
x=t1
t2=t1+4
t2
t1=x+3
x=t1
t2=t1+4
4
2301373 Code Generation 14
Practical Code Generation: Tree Traversal
procedure genCode (t:node)
{ if (t is not null) then
{ generate code to prepare for code of left child;
genCode(left child of t);
generate code to prepare for code of right child;
genCode(right child of t);
generate code to implement the action of t;
}
}
2301373 Code Generation 15
Practical Code Generation for P-code
gencode(T:Tree)
{ if (T is not null)
{switch (T.type)
case plusnode:
{ gencode(T.lchild);
gencode(T.rchild);
emitCode(“add”); }
case asgnnode:
{ emitcode(“loada”,T.strval);
gencode(T.rchild);
emitcode(“stn”); }
case constnode:
{ emitcode(“loadc”,t.strval); }
case idnode:
{ emitcode(“loadv”,t.strval); }
default:
{ emitcode(“error”);
}
}
}
2301373 Code Generation 16
Macaro Expansion
 From intermediate code to target code
 Example:
 3-address code:
x=y+n
 P code:
loada x
loadv y
loadc n
adi
sto
2301373 Code Generation 17
Address Calculation
 Addressing operations
 Array references
 Record structure references
2301373 Code Generation 18
Addressing Operations
3-address code
 address of x
 &x
 indirect address
(content pointed
by x)
 *x
P-code
 address of x
 loada x
 indirect load
 ind x
 (load *(top+ x))
 indexed address
 ixa x
 (load top*x+
(top-1))
2301373 Code Generation 19
Array References
3-address code
 x=a[i]
t1=i*elesize(a)
t2=&a+t1
x=*t2
 a[i]=x
t1=i*elesize(a)
t2=&a+t1
*t2=x
P-code
 x=a[i]
loada x
loada a
loadv i
ixa elesize(a)
ind 0
sto
 a[i]=x
loada a
loadv i
ixa elesize(a)
loadv x
sto
Find offset
Find address
Find address
Find offset
Find address
Find address
Find content
2301373 Code Generation 20
More Complex Array References
3-address code
 a[i+1]=a[j+2]+3
t1=j+2
t2=t1*elesize(a)
t3=&a+t2
t4=*t3
t5=t4+3
t6=i+1
t7=t6*elesize(a)
t8=&a+t7
*t8=t5
P code
 a[i+1]=a[j+2]+3
loada a
loadv i
loadc 1
adi
ixa elesize(a)
loada a
loadv j
loadc 2
adi
ixa elesize(a)
ind 0
loadc 3
adi
sto
t4=a[t1]
a[t6]=t5 t1=a[j+2]
t2=&a[i+1]
a[t6]=t5
t4=a[t1]
t2=&a[i+1]
t1=a[j+2]
2301373 Code Generation 21
Structure References
typedef struct rec
{ int i; char c; int j; }
Entry;
Entry x;
3-address code
 Address of a field
t1=&x+offset(x,j)
 Content of a field
t1=&x+offset(x,j)
t2=*t1
P code
 Address of a field
loada x
loadc offset(x.j)
ixa 1
 Content of a field
loada x
ind offset(x.j)
x.i
x.j
x.c
base address of x
Offset of x.j
Offset of x.c
2301373 Code Generation 22
Code Generation for Control Statements
 If Statements
 While Statements
 Logical Expressions
2301373 Code Generation 23
Code Generation for If-Statements
IF ( E ) S1 ELSE S2
 3-address code
<code evaluating E
and assigning to t1>
if_false t1 goto L1
<code for S1>
goto L2
label L1
<code for S2>
label L2
 P code
<code evaluating E>
jumpF L1
<code for S1>
jump L2
label L1
<code for S2>
label L2
2301373 Code Generation 24
Code Generation for While-Statements
WHILE ( E ) S
 3-address code
label L1
<code evaluating E
and assigning to t1>
if_false t1 goto L2
<code for S>
goto L1
label L2
 P code
label L1
<code evaluating E>
jumpF L2
<code for S>
jump L1
label L2
2301373 Code Generation 25
Generating Labels
 Forward jump
 Label must be generated before
defining the label
 For intermediate code
 Generate label at the jump instruction
 Store the label until the actual
location is found
 For target code (assembly)
 Leave the destination address in the
jump instruction
 When the destination address is
found, go back and fill the address
(backpatching)
 Short jump or long jump?
 Leave space enough for long jump
 If only short jump is required, the
extra space is filled with nop.
label L1
<code for E>
jumpF L2
<code for S>
jump L1
label L2
2301373 Code Generation 26
Code Generation for Logical Expressions
 Data types and operators
 Use Boolean data type and
operators if included in the
target/intermediate language
 Use integer 0/1 and bitwise
opeartors
 Short-circuit evaluation
 IF a AND b THEN S
 If a is false,
 the whole exp is false
 there is no need to evaluate b
 IF a OR b THEN S
 If a is true,
 the whole exp is true
 there is no need to evaluate b
 Intermediate code for
IF a AND b THEN S
<code for a>
if_false a goto L1
<code for b>
if_false b goto L1
<code for S>
Label L1
 Intermediate code for
IF a OR b THEN S
<code for a>
if_false a goto L1
goto L2
Label L1
<code for b>
if_false b goto L3
Label L2
<code for S>
Label L3

CODE GENERATION PHASE COMPILER DESIGN.ppt

  • 1.
    Code Generation How toproduce intermediate or target code
  • 2.
    2301373 Code Generation2 Outline  Intermediate code  Three-address code  P-code  Code generation techniques  Using attribute grammar  Tree traversal  Macro expansion  Address Calculation  Code generation for control statements  Code generation for logical expressions
  • 3.
    2301373 Code Generation3 Intermediate Code  Intermediate representation for programs  Why intermediate code  Reduce amount of work if optimization is done for intermediate code  Easy for retargeting compilers  Forms of intermediate code  Abstract syntax tree  Linearization of abstract syntax tree
  • 4.
    2301373 Code Generation4 Three-address code: Form of Instructions  x = y op z  x, y, and z are addresses of  Variables (perhaps temporaries)  Locations in programs  y and z must be differed from x  Operators can be:  Arithmetic operators (3-address)  Relational operators (3-address)  Conditional operators (2-address)  If_false … goto …  Jump (1-address)  Input/Output  Halt  Labels can be assigned to a location
  • 5.
    2301373 Code Generation5 Example of 3-address Code read (x); if x>0 then { fact:=1; repeat { fact:=fact*x; x:=x-1; } until x==0; write(fact); } read x t1=x>0 if_false t1 goto L1 fact=1 label L2 t2=fact*x fact=t2 t3=x-1 x=t3 t4=x==0 if_false t4 goto L2 write fact label L1 halt
  • 6.
    2301373 Code Generation6 P-Code  Code for a hypothetical stack machine  For Pascal compilers  No variable name is required  Instructions  Load stack  Arithmetic and relational operators  Jumps  Operations are performed on topmost values on stack  0-address or 1-address instructions
  • 7.
    2301373 Code Generation7 P-code Instruction  Load: push stack  Load value  Load address  Load constant  Store: save top of stack in memory  Destructive store  Nondestructive store  Arithmetic operations  Add  Subtract  Multiply  Compare  Greater  Less  equal  Label  Jump  Unconditional jump  Conditional jump  I/O  Read  write  Stop
  • 8.
    2301373 Code Generation8 Example of P-code read (x); if x>0 then { fact:=1; repeat { fact:=fact*x; x:=x-1; } until x==0; write(fact); } loada x read loadv x loadc 0 greater jumpONfalse L1 loada fact loadc 1 store label L2 loada fact loadv fact loadv x mult store loada x loadv x loadc 1 sub store loadv x loadc 0 equ jumpF L2 loadv fact write label L1 stop
  • 9.
    2301373 Code Generation9 Code Generation Using Synthesizes Attributes  An attribute is created for the sequence of characters representing generated code.  An attribute grammar is written to generate the intermediate/target code.  The value of the attribute is passed from child nodes upto their parent node to construct a larger chunk of code
  • 10.
    2301373 Code Generation10 Attribute Grammar for P-code Grammar Rules exp1 -> id = exp2 exp -> aexp aexp1 ->aexp2 + f actor aexp -> factor factor -> ( exp ) factor -> num factor -> id Semantic Rules exp1.code =“loada” || id.strval + + exp2.code ++ ”stn” exp.code = aexp.code aexp1 .code = aexp2 .code ++ fac tor.code ++ “add” aexp.code = factor.code factor.code = exp.code factor.code = “loadc ” || num.strv al factor.code = “loadv ” || id.strval
  • 11.
    2301373 Code Generation11 Generating P-Code:Example exp = id exp exp aexp aexp factor + factor ( ) aexp aexp factor + factor num id num loada x loadv x loadc 3 adi stn loadc 4 adi loadc 4 loada x loadv x loadc 3 adi stn loadv x loadc 3 adi loadv x loadc 3 loadv x loadv x loadc 3 adi loada x loadv x loadc 3 adi stn loada x loadv x loadc 3 adi stn loada x loadv x loadc 3 adi stn loadc 4 adi
  • 12.
    2301373 Code Generation12 Attribute Grammar for 3-address Code Grammar Rules exp1-> id = exp2 exp -> aexp aexp1->aexp2+fac tor aexp -> factor factor -> ( exp ) factor -> num factor -> id Semantic Rules exp1.name = exp2.name; exp1.code=exp2.code++id.strval||”=“||exp2.name exp.name = aexp.name; exp.code = aexp.code exp1.name = newtemp(); aexp1.code=aexp2.code++factor.code++ aexp1. name||”=“||aexp2 .name||”+“||factor.name aexp.name=factor.name;aexp.code=factor.cod e factor.name =exp.name; factor.code = exp.cod e factor.name = num.strval; factor.code = “” factor.name = id.strval; factor.code = “”
  • 13.
    2301373 Code Generation13 Generating 3-address Code:Example exp = id exp exp aexp aexp factor + factor ( ) aexp aexp factor + factor num id num x 4 x t1 t1=x+3 3 t1 t1=x+3 t1 t1=x+3 x=t1 t1 t1=x+3 x=t1 t1 t1=x+3 x=t1 t2 t1=x+3 x=t1 t2=t1+4 t2 t1=x+3 x=t1 t2=t1+4 4
  • 14.
    2301373 Code Generation14 Practical Code Generation: Tree Traversal procedure genCode (t:node) { if (t is not null) then { generate code to prepare for code of left child; genCode(left child of t); generate code to prepare for code of right child; genCode(right child of t); generate code to implement the action of t; } }
  • 15.
    2301373 Code Generation15 Practical Code Generation for P-code gencode(T:Tree) { if (T is not null) {switch (T.type) case plusnode: { gencode(T.lchild); gencode(T.rchild); emitCode(“add”); } case asgnnode: { emitcode(“loada”,T.strval); gencode(T.rchild); emitcode(“stn”); } case constnode: { emitcode(“loadc”,t.strval); } case idnode: { emitcode(“loadv”,t.strval); } default: { emitcode(“error”); } } }
  • 16.
    2301373 Code Generation16 Macaro Expansion  From intermediate code to target code  Example:  3-address code: x=y+n  P code: loada x loadv y loadc n adi sto
  • 17.
    2301373 Code Generation17 Address Calculation  Addressing operations  Array references  Record structure references
  • 18.
    2301373 Code Generation18 Addressing Operations 3-address code  address of x  &x  indirect address (content pointed by x)  *x P-code  address of x  loada x  indirect load  ind x  (load *(top+ x))  indexed address  ixa x  (load top*x+ (top-1))
  • 19.
    2301373 Code Generation19 Array References 3-address code  x=a[i] t1=i*elesize(a) t2=&a+t1 x=*t2  a[i]=x t1=i*elesize(a) t2=&a+t1 *t2=x P-code  x=a[i] loada x loada a loadv i ixa elesize(a) ind 0 sto  a[i]=x loada a loadv i ixa elesize(a) loadv x sto Find offset Find address Find address Find offset Find address Find address Find content
  • 20.
    2301373 Code Generation20 More Complex Array References 3-address code  a[i+1]=a[j+2]+3 t1=j+2 t2=t1*elesize(a) t3=&a+t2 t4=*t3 t5=t4+3 t6=i+1 t7=t6*elesize(a) t8=&a+t7 *t8=t5 P code  a[i+1]=a[j+2]+3 loada a loadv i loadc 1 adi ixa elesize(a) loada a loadv j loadc 2 adi ixa elesize(a) ind 0 loadc 3 adi sto t4=a[t1] a[t6]=t5 t1=a[j+2] t2=&a[i+1] a[t6]=t5 t4=a[t1] t2=&a[i+1] t1=a[j+2]
  • 21.
    2301373 Code Generation21 Structure References typedef struct rec { int i; char c; int j; } Entry; Entry x; 3-address code  Address of a field t1=&x+offset(x,j)  Content of a field t1=&x+offset(x,j) t2=*t1 P code  Address of a field loada x loadc offset(x.j) ixa 1  Content of a field loada x ind offset(x.j) x.i x.j x.c base address of x Offset of x.j Offset of x.c
  • 22.
    2301373 Code Generation22 Code Generation for Control Statements  If Statements  While Statements  Logical Expressions
  • 23.
    2301373 Code Generation23 Code Generation for If-Statements IF ( E ) S1 ELSE S2  3-address code <code evaluating E and assigning to t1> if_false t1 goto L1 <code for S1> goto L2 label L1 <code for S2> label L2  P code <code evaluating E> jumpF L1 <code for S1> jump L2 label L1 <code for S2> label L2
  • 24.
    2301373 Code Generation24 Code Generation for While-Statements WHILE ( E ) S  3-address code label L1 <code evaluating E and assigning to t1> if_false t1 goto L2 <code for S> goto L1 label L2  P code label L1 <code evaluating E> jumpF L2 <code for S> jump L1 label L2
  • 25.
    2301373 Code Generation25 Generating Labels  Forward jump  Label must be generated before defining the label  For intermediate code  Generate label at the jump instruction  Store the label until the actual location is found  For target code (assembly)  Leave the destination address in the jump instruction  When the destination address is found, go back and fill the address (backpatching)  Short jump or long jump?  Leave space enough for long jump  If only short jump is required, the extra space is filled with nop. label L1 <code for E> jumpF L2 <code for S> jump L1 label L2
  • 26.
    2301373 Code Generation26 Code Generation for Logical Expressions  Data types and operators  Use Boolean data type and operators if included in the target/intermediate language  Use integer 0/1 and bitwise opeartors  Short-circuit evaluation  IF a AND b THEN S  If a is false,  the whole exp is false  there is no need to evaluate b  IF a OR b THEN S  If a is true,  the whole exp is true  there is no need to evaluate b  Intermediate code for IF a AND b THEN S <code for a> if_false a goto L1 <code for b> if_false b goto L1 <code for S> Label L1  Intermediate code for IF a OR b THEN S <code for a> if_false a goto L1 goto L2 Label L1 <code for b> if_false b goto L3 Label L2 <code for S> Label L3