Three Adress Code
Generation
Presented by:
Rabin BK
CONTENTS
01
02
03
INTRODUCTION
TYPES
EXAMPLE
04 REFERENCES
INTRODUCTION
● An intermediate code (machine independent code) used by the optimizing
compilers
● A sequence of statements of the general form x = y op z
● where x, y, z are names, constants
● op stands for any operator (such as fixed or floating point arithmetic
operator or a logical operator on boolean valued data)
● Eliminates the need of a new full compiler for every unique machine by
keeping the analysis portion same for all the compilers.
● Assumes to have unlimited number of memory storage (register) to
generate code
TYPES OF 3 ADDRESS STATEMENTS
● Assignment statements
– Having the form x := y op z
– op --> binary arithmetic or logical operation
● Assignment Instructions
– Having the form x := op y
– op --> unary operation (unary plus, unary minus shift etc)
● Copy statements
● Having the form x := y
TYPES OF 3 ADDRESS STATEMENTS CONTD...
● Unconditional (Jump goto L)
– The three address statement with label L is the next to be executed
● Conditional Jumps (such as if x relop y goto L)
– applies a relational operator (<,>,<=,>=) to x and y
– if (condition satisfied) { execute the statement with label L } else {
execute the statement following, if x relop y goto L }
TYPES OF 3 ADDRESS STATEMENTS CONTD...
● param x and call p, n
– Used for procedure calls and return y(represents returned value)
– generated as a part of the three address code for call of the
procedure p(x1,x2,x3,....xn) where n are the number of variables
being sent to the procedure
param x1
param x2
.
.
param xn
call p,n
TYPES OF 3 ADDRESS STATEMENTS CONTD...
● Indexed copy instructions
x = y[i]
x[i] = y
● Address and pointer assignments
x = &y
x = *y
*x = y
EXAMPLE
if (a < b + c)
a = a - c;
c = b * c;
_t1 = b + c;
_t2 = a < _t1;
If Z _t2 Goto _L0;
_t3 = a - c;
a = _t3;
_L0: _t4 = b * c;
c = _t4;
int foo(int a, int b){
return a + b;
}
void main() {
int c;
int d;
foo(c, d);
}
foo:
BeginFunc 4;
_t0 = a + b;
Return _t0;
EndFunc;
main:
BeginFunc 12;
PushParam d;
PushParam c;
_t1 = LCall _foo;
PopParams 8;
EndFunc;
Number of bytes
for a function
Stack operation
REFERENCES
● https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/handouts/24
0%20TAC%20Examples.pdf
● https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/lectures/13/S
lides13.pdf
● https://www.tutorialspoint.com/compiler_design/compiler_design_interme
diate_code_generations.htm
● https://www.javatpoint.com/three-address-code
● http://cic.puj.edu.co/wiki/lib/exe/fetch.php?media=materias:compi:comp_
sesion21_aux1.pdf
QUERIES

Three address code generation

  • 1.
  • 2.
  • 3.
    INTRODUCTION ● An intermediatecode (machine independent code) used by the optimizing compilers ● A sequence of statements of the general form x = y op z ● where x, y, z are names, constants ● op stands for any operator (such as fixed or floating point arithmetic operator or a logical operator on boolean valued data) ● Eliminates the need of a new full compiler for every unique machine by keeping the analysis portion same for all the compilers. ● Assumes to have unlimited number of memory storage (register) to generate code
  • 4.
    TYPES OF 3ADDRESS STATEMENTS ● Assignment statements – Having the form x := y op z – op --> binary arithmetic or logical operation ● Assignment Instructions – Having the form x := op y – op --> unary operation (unary plus, unary minus shift etc) ● Copy statements ● Having the form x := y
  • 5.
    TYPES OF 3ADDRESS STATEMENTS CONTD... ● Unconditional (Jump goto L) – The three address statement with label L is the next to be executed ● Conditional Jumps (such as if x relop y goto L) – applies a relational operator (<,>,<=,>=) to x and y – if (condition satisfied) { execute the statement with label L } else { execute the statement following, if x relop y goto L }
  • 6.
    TYPES OF 3ADDRESS STATEMENTS CONTD... ● param x and call p, n – Used for procedure calls and return y(represents returned value) – generated as a part of the three address code for call of the procedure p(x1,x2,x3,....xn) where n are the number of variables being sent to the procedure param x1 param x2 . . param xn call p,n
  • 7.
    TYPES OF 3ADDRESS STATEMENTS CONTD... ● Indexed copy instructions x = y[i] x[i] = y ● Address and pointer assignments x = &y x = *y *x = y
  • 8.
    EXAMPLE if (a <b + c) a = a - c; c = b * c; _t1 = b + c; _t2 = a < _t1; If Z _t2 Goto _L0; _t3 = a - c; a = _t3; _L0: _t4 = b * c; c = _t4; int foo(int a, int b){ return a + b; } void main() { int c; int d; foo(c, d); } foo: BeginFunc 4; _t0 = a + b; Return _t0; EndFunc; main: BeginFunc 12; PushParam d; PushParam c; _t1 = LCall _foo; PopParams 8; EndFunc; Number of bytes for a function Stack operation
  • 9.
    REFERENCES ● https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/handouts/24 0%20TAC%20Examples.pdf ● https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/lectures/13/S lides13.pdf ●https://www.tutorialspoint.com/compiler_design/compiler_design_interme diate_code_generations.htm ● https://www.javatpoint.com/three-address-code ● http://cic.puj.edu.co/wiki/lib/exe/fetch.php?media=materias:compi:comp_ sesion21_aux1.pdf
  • 10.