Operator Overloading

Definition:

Operator overloading is a declaration of a function whose designator is an operator symbol.

Simplified Syntax

function "operator" (parameters) return type is

function "operator" (parameters) return type is

declarations

  begin

    sequential statements

  end function;

Description

The operator is called overloaded if there is more than one function specifying it for different data and result types. VHDL allows defining operators of the same names as predefined operators, but for different operand types. Both can exist together in one specification, offering greater versatility to the user.

Such functions can be invoked both with prefix notation (Example 1) and usual infix notation (Example 2).

In the case when the operator is called and the operand values belong to different types, it is necessary to use the type conversion or the qualified expression in order to select appropriate operator (Example 3).

Examples

Example 1

type Log4 is ('0', '1', 'Z', 'X');
function "nand" (Left, Right: Log4) return Log4;
function "or" (Left, Right: Log4) return Log4;
signal S1, S2: Log4;
S1 <= "or" ('1', 'Z');
S2 <= "nand"(S1, 'X');

Functions or and nand implement basic logic operations for operands of type Log4 overloading the predefined operations of the same names. In the above example these functions are called using the standard call syntax function (prefix).

Example 2

signal S3, S4: Log4;
S3 <= (S1 nand S2) or 'X';

The operators or and nand are overloaded through the declarations as in the Example 1. Here the overloaded operators are used in the infix form, i.e. with the operator name between the operands.

Example 3

function "or" (Left, Right: Log4) return Bit;
signal S4: Bit;
S4 <= log4('1') or Log4('0');

The or operator is used here in a 4-value logical expression by connecting '1' and '0' operands with the or symbol. The qualified expression was used here to indicate the type of operands (which otherwise would be considered to be of the type BIT).

Important Notes

· Operators "+" and "-" can be defined both as binary operators (with two operands) or unary operators (with one operand).

· Invoking a user-defined overloaded operator always requires evaluation of both operands before the operation is executed (some predefined operators do not evaluate the right operand if the result can be decided from the left operand only).

No comments:

Post a Comment

Please provide valuable comments and suggestions for our motivation...

Popular Posts