Search code examples
operatorsvhdloperator-precedenceunary-operator

Are parentheses really necessary in expressions with unary logical operators?


VHDL-2008 introduced reduction operators that take in a 1D array of logic elements and output a single bit value. Quoting from Verification Horizons Vol. 8 Issue 3 Oct. 2012:

VHDL-2008 creates unary versions of AND, OR, NOR, NAND, XOR, and XNOR for logic array types (bit_vector, std_logic_vector, …). The operators are applied to each element of the array argument (a reduction operation) and produce an element result. Unary operators have the same precedence as the miscellaneous operators (**, ABS, and NOT).

The mechanics are explained in the LRM [9.2.2], but what motivates this question is Note 2 from [9.1]:

NOTE 2—The syntax for an expression involving a unary condition operator or unary logical operator in combination with any other operator requires that the unary operator and its operand be a parenthesized expression. For example, the expressions “(and A) and B” and “A and (and B)” are legal, whereas the expression “and A and B” and “A and and B” are not.

However, ModelSim and ActiveHDL happily accept this:

variable B, Y: bit;
variable A: bit_vector(3 downto 0);
...
Y := and A and B;  -- Should be illegal according to Note 2 [9.1]

And this:

variable A, Y: bit;
variable B: bit_vector(3 downto 0);
...
Y := A and and B;  -- Should be illegal according to Note 2 [9.1]

Now, if we keep reading the LRM, another note in [9.2.2] provides some clues into why this works:

NOTE—All of the binary logical operators belong to the class of operators with the lowest precedence. The unary logical operators belong to the class of operators with the highest precedence

According to this note, the tool should have no problem understanding the expressions in the examples above.

So, the question is: do we really need to write the parentheses, as Note 2 in [9.1] suggests, or can we rely on operator precedence as the note in [9.2.2] suggests?


Solution

  • See Precedence of Unary Logical Operators Dated 2013-11-14.

    Actions

    Change the grammar production for miscellaneous operator to (note the extra space after unary should not be there - it is a twiki issue)

    miscellaneous_operator ::= ** | abs | not | unary _logical_operator

    Delete note 2 at the top of 118 that reads:

    "NOTE 2—The syntax for an expression involving a unary condition operator or unary logical operator in combination with any other operator requires that the unary operator and its operand be a parenthesized expression. For example, the expressions “(and A) and B” and “A and (and B)” are legal, whereas the expression “and A and B” and “A and and B” are not. Similarly, “and (and A)” is legal, whereas “and and A” is not. An expression consisting only of a unary condition oprator or unary logical operator and its operand need not be parenthesized."


    After going through the issue de novo I'll support it. The reason why is this eliminates a lexical ambiguity, requiring the original NOTE 2 compliance be caught at elaboration time instead of being reflected in the EBNF. (If parentheses were to be required they should appear in the EBNF, like those of mixing binary logical operators of the same priority).

    There's no telling when and if this will get approved.