Types of 3 address code and example

Here we have 3 address code which have below types as:

1) x=y Op z

2) x=Op z

3) x=y

4) if x (relational Op) y goto L

5) goto L

6) A[i] = x

7) y=A[i]

8) x=*P

8) y=&x

 

 

Example:

-(a+b)*(c+d)+(a+b+c)

given instruction , you should change it into 3 address code 

sol:-

t1=a+b

t2=-t1

t3=c+d

t4=t2*t3

t5=a+b

t6=t5+c

t7=t4+t6

 

Contributor's Info

Created:
0Comment
Introduction to intermediate code generation

Intermediate code generator receives input from its predecessor phase, semantic analyzer, in the form of an annotated syntax tree. That syntax tree then can be converted into a linear representation, e.g., postfix notation. Intermediate code tends to be machine independent code.

1)If a compiler translates the source language to its target machine language without having the option for generating intermediate code, then for each new machine, a full native compiler is required.

2)Intermediate code eliminates the need of a new full compiler for every unique machine by keeping the analysis portion same for all the compilers.

3)The second part of compiler, synthesis, is changed according to the target machine.

4)It becomes easier to apply the source code modifications to improve code performance by applying code optimization techniques on the intermediate code. intermediate code can be either language specific (e.g., Byte Code for Java) or language independent (three-address code).

 

Three-Address Code

Intermediate code generator receives input from its predecessor phase, semantic analyzer, in the form of an annotated syntax tree. That syntax tree then can be converted into a linear representation, e.g., postfix notation. Intermediate code tends to be machine independent code. Therefore, code generator assumes to have unlimited number of memory storage (register) to generate code.

For example:

a = b + c * d;

The intermediate code generator will try to divide this expression into sub-expressions and then generate the corresponding code.

r1 = c * d;

r2 = b + r1;

a = r2

r being used as registers in the target program.

A three-address code has at most three address locations to calculate the expression. A three-address code can be represented in three forms : quadruples , triples, indirect triple.

Quadruples

 

Each instruction in quadruples presentation is divided into four fields: operator, arg1, arg2, and result. The above example is represented below in quadruples format:

Op arg1 arg2 result
* c d r1
+ b r1 r2
+ r2 r1 r3
= r3   a

 

 

Triples

Each instruction in triples presentation has three fields : op, arg1, and arg2.The results of respective sub-expressions are denoted by the position of expression. Triples represent similarity with DAG and syntax tree. They are equivalent to DAG while representing expressions.

Op arg1 arg2
* c d
+ b (0)
+ (1) (0)
= (2)  

 

Triples face the problem of code immovability while optimization, as the results are positional and changing the order or position of an expression may cause problems.

 

 

Indirect Triples

This representation is an enhancement over triples representation. It uses pointers instead of position to store results. This enables the optimizers to freely re-position the sub-expression to produce an optimized code.

disadvantages of using indirect triples is we have to access two memory due to pointer.

 

for more details one can refer to below link:

https://www.slideshare.net/rawan_z/intermediate-code-generation

Contributor's Info

Created:
0Comment
Intermediate Code Generator

Intermediate code generator:

This phase is also called as the translator because it will translate the syntax tree into the intermediate representation so that it is easy to convert into the machine understandable language. This process allows the separation of dependencies among the source and the target.

Representation of intermediate code is done in two different ways.

High level representation.

Abstract syntax tree

Direct acyclic graphs

Flow graph

Low level representation

Postfix notation

Three address code

Contributor's Info

Created:
0Comment