What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y...

9
What is Three Address What is Three Address Code? Code? A statement of the form x = y op A statement of the form x = y op z is a three address statement. z is a three address statement. x, y and z here are the three x, y and z here are the three operands and op is any logical or operands and op is any logical or arithmetic operator. arithmetic operator. Here three address refers to the Here three address refers to the three addresses in the statement three addresses in the statement viz addresses of x , y and z. viz addresses of x , y and z.

Transcript of What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y...

Page 1: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

What is Three Address What is Three Address Code?Code?

A statement of the form x = y op z is A statement of the form x = y op z is a three address statement. x, y and z a three address statement. x, y and z here are the three operands and op here are the three operands and op is any logical or arithmetic operator.is any logical or arithmetic operator.

Here three address refers to the Here three address refers to the three addresses in the statement viz three addresses in the statement viz addresses of x , y and z.addresses of x , y and z.

Page 2: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

Say for example we have a Say for example we have a statement statement

a = b + c * d a = b + c * d

Then three address code for this Then three address code for this statement is :-statement is :-

t1 = c * dt1 = c * d

a = b + t1a = b + t1

Page 3: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

Types :-Types :-

There are different types of three There are different types of three address statements. Some of them address statements. Some of them are are

Assignment statements. They are of the Assignment statements. They are of the form x := y op z where op is a binary form x := y op z where op is a binary arithmetic or logical operation.arithmetic or logical operation.

Assignment Instructions. They are of the Assignment Instructions. They are of the form x := op y where op is an unary form x := op y where op is an unary operation like unary plus, unary minus operation like unary plus, unary minus shift etcshift etc

Page 4: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

Copy statements. They are of the form x := y Copy statements. They are of the form x := y where the value of y is assigned to xwhere the value of y is assigned to x..

Unconditional Jump goto L. The three Unconditional Jump goto L. The three address statement with label L is the next to address statement with label L is the next to be executed. be executed.

Conditional Jumps such as if x relop y goto L. Conditional Jumps such as if x relop y goto L. This instruction applies a relational operator This instruction applies a relational operator (<,>,<=,>=) to x and y and executes the (<,>,<=,>=) to x and y and executes the statement with label L if the conditional statement with label L if the conditional statement is satisfied. Else the statement statement is satisfied. Else the statement following if x relop y goto L is executedfollowing if x relop y goto L is executed

Page 5: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

param x and call p,n for procedure calls param x and call p,n for procedure calls and return y where y representing a and return y where y representing a returned value (optional).Three Address returned value (optional).Three Address statements for it are as follows. statements for it are as follows.

param x1 param x1 param x2 param x2 param x3 param x3 . . param xn param xn call p,n call p,n generated as a part of the three address generated as a part of the three address

code for call of the procedure code for call of the procedure p(x1,x2,x3,....xn) where n are the number p(x1,x2,x3,....xn) where n are the number of variables being sent to the procedure of variables being sent to the procedure

Page 6: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

Structure for Three Structure for Three Address CodeAddress Code

It is a quadruple of an operator, arg1 , It is a quadruple of an operator, arg1 , arg2 and a result or it is a triple of an arg2 and a result or it is a triple of an operator , arg1 and arg2. In triple form operator , arg1 and arg2. In triple form arg2 is generally a pointer to the triple arg2 is generally a pointer to the triple structure.structure.

Page 7: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

Three Address Code Three Address Code GenerationGeneration

Suppose we have the grammar Suppose we have the grammar S ->id := E S ->id := E E -> E+E E -> E+E E -> E*E E -> E*E E -> - E E -> - E E -> (E) E -> (E) E -> id E -> id

Page 8: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

Then the three address code for this Then the three address code for this grammar can be written as:-grammar can be written as:-

Page 9: What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.

E1 refers to the first E that comes in the E1 refers to the first E that comes in the production. production.

E2 refers to the second E that comes in the E2 refers to the second E that comes in the production. production.

Here E.code stands for the three address code Here E.code stands for the three address code generated by E and E.place stands for the generated by E and E.place stands for the variable name variable name