Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three Address Code (TAC / 3AC)

While doing some reading, I came across the terms "Intermediate Language" and "3AC".

IL, as I understand, is the middle "step" in the source code compilation process. More specifically, I'm reading about bytecode (Java) and C.

The way I interpret it (correct me if am wrong) is;

Source Code 1 (ex. Lisp) -> Intermediate Language (C) -> Assembly Language -> Machine Code

Source Code 2 (ex. Java) -> Bytecode -> Java Virtual Machine

So based on that, am struggling to see where does The Three Address Code (TAC/3AC) comes into play, and what for its used.

like image 820
Carlos Avatar asked Oct 20 '10 20:10

Carlos


People also ask

How do I get a three address code?

Example-1: Convert the expression a * – (b + c) into three address code. It is structure with consist of 4 fields namely op, arg1, arg2 and result. op denotes the operator and arg1 and arg2 denotes the two operands and result is used to store the result of the expression. Easy to rearrange code for global optimization.

What is 3AC in compiler design?

The three-address code is a sequence of statements of the form A−=B op C, where A, B, C are either programmer-defined names, constants, or compiler-generated temporary names, the op represents an operator that can be constant or floatingpoint arithmetic operators or a Boolean valued data or a logical operator.

What are types of three address code?

The three address code can be represented in two forms: quadruples and triples.

Which one of the following is a three address code representation A quadruples B triples C indirect triples D All of the above?

Detailed Solution. The correct answer is option 3. Intermediate code can be represented in three forms, which are postfix notation, Syntax trees, Three address code. In a compiler, three address code can be implemented as records with fields for operator and operands.


1 Answers

Three-address code (TAC) is the intermediate representation used in most compilers. It is essentially a generic assembly language that falls in the lower-end of the mid-level IRs. Some variant of 2, 3 or 4 address code is fairly commonly used as an IR, since it maps well to most assembly languages.

A TAC instruction can have at most three operands. The operands could be two operands to a binary arithmetic operator and the third the result location, or an operand to compare to zero and a second location to branch to, and so on. For example, below on the top is an arithmetic expression and on the bottom, is a translation into TAC instructions:

//Expresion
        a = b * c + b * d;
//3AC
        _t1 = b * c;
        _t2 = b * d;
        _t3 = _t1 + _t2;
        a = _t3;

Source: http://web.archive.org/web/20151010192637/http://www.dound.com/courses/cs143/handouts/17-TAC-Examples.pdf

like image 55
Carlos Avatar answered Sep 28 '22 03:09

Carlos