Three-Address Code

Three-Address Code

If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted. It is a kind of intermediate code that can be easily generated and converted into machine code.

The expression a+b*c can be translated into three-address instructions:        

t1 = b * c
t2 = a + t1

Here, t1 and t2 are the temporary names, which are generated by the compiler.

Example:

Consider the following expression:

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

Three-address code for the given example is shown below:

t1 = b – c

t2 = a * t1

t3 = a + t2

t4 = t1 * d

t5 = t3 + t4

For the construction of a three-address code, two concepts are required: addresses and instructions. Three-address code can be performed using records called quadruples and triples, which are described below in detail.

The address can be any of the following: a name, a constant, or a compiler-generated temporary name.

Quadruples

A quadruples consists of four fields such as op, arg1, arg2, and result. The internal code for the operator is stored in the op field. For instance, the three-address instruction a = b + c is depicted by putting + in the op, b in arg1, c in arg2, and a in the result.

This rule has some anomaly, which is given below:

  • Operations with unary operators do not use arg2.
  • Arg2 and result don’t use by the PARAM operator.
  • Conditional and unconditional operators put the target label in the result.

Example

See the below expression:

a = b * - c + b * - c;

Here, we have used the special operator minus to distinguish the unary minus operator from the binary minus operator.

Three address code for the given statements are:

t1 = minus c

t2 = b * t1

t3 = minus c

t4 = b * t3

t5 = t2 + t1

a = t5

Quadruple representation for given expression:

 oparg1arg2result
0minusc t1
1*bt1t2
2minusc t3
3*bt3t4
4+t2t4t5
5=t5 a

Triples

A triple has only three fields, which are known as op, arg1, and arg2. Result fields in quadruple are used for temporary names. Triples using the position of an operation to store results rather than temporary names. So triple using position (0) rather than temporary t1.

Example

Consider the following expression:

a = b * - c + b * - c ;

The three-address code and quadruple of this expression are shown above. The syntax tree and triple of this expression are shown below. 

 oparg1arg2
0minusc 
1*b(0)
2minusc 
3*b(2)
4+(1)(3)
5=a(4)

Here, forthecopy statement, a=t5 is replaced by a in agr1 and by 4 in arg2.


Related Topics

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Symbol Table Compiler Design

Symbol Table It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc.  The...

3 minutes read.

Parsing in Compiler Design

Parsing is the term used to describe the process of converting data between different formats. The parser can carry out this operation. The parser is a part of the translator...

4 minutes read.

LEX

LEX Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows...

3 minutes read.

Run-Time Environments

Run-Time Environments Storage Organization Every target program has its own logical address, and an executable program runs in it. The logical address space has the location for each program value. The...

2 minutes read.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

2 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Target Machine

Target Machine A target machine is a byte-addressable machine. This machine has n general-purpose registers, R0, R1,.....Rn-1. A Simple Target Machine Model has three-address instruction. A full-edged assembly language would have...

3 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.

Phases of Compiler

Phases of Compiler The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input. Here we will see all...

2 minutes read.

Compiler Design Tutorial

Our compiler design tutorial will provide all the information about compiler from basic to advanced level. This compiler tutorial will help the student for their semester as well as for...

3 minutes read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 minutes read.

Lexical Analysis in Compiler Design

Lexical Analysis It is the first phase of the compiler. As we know, it is also known as a scanner. The input for lexical analysis is source code. After taking source...

2 minutes read.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 minutes read.

Optimization of Basic Blocks in Compiler Design

Optimization of Basic Blocks We can apply the optimization process on a basic block. While optimization, there is no need to change the set of expressions computed by the block. The basic...

3 minutes read.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

6 minutes read.

Stack Allocation of Space

Stack Allocation of Space Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space...

2 minutes read.

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

4 minutes read.