RE: Expression evaluation order

From: Bresticker, Shalom <shalom.bresticker@intel.com>
Date: Thu Jan 27 2011 - 04:19:18 PST

Hi,

Exactly this change was made in SV-2009:

11.3.5 Operator expression short circuiting

The operators shall follow the associativity rules while evaluating an expression as described in 11.3.2.

Some operators (&&, ||, ->, and ?:) shall use short circuit evaluation; in other words, some of their operand expressions shall not be evaluated if their value is not required to determine the final value of the operation.

The detailed short circuiting behavior of each of these operators is described in its corresponding section (11.4.7 and 11.4.11). All other operators shall not use short circuit evaluation-all of their operand expressions are always evaluated. When short circuiting occurs, any side effects or runtime errors that would have occurred due to evaluation of the short circuited operand expression shall not occur.

For example:

logic regA, regB, regC, result ;
function logic myFunc(logic x);
...
endfunction
result = regA & (regB | myFunc(regC)) ;

Even if regA is known to be zero, the subexpression (regB | myFunc(regC)) will be evaluated and any side effects caused by calling myFunc(regC) will occur.

Note that implementations are free to optimize by omitting evaluation of subexpressions as long as the simulation
behavior (including side effects) is as if the standard rules were followed."

Regards,
Shalom

> -----Original Message-----
> From: owner-verilog-ams@eda.org [mailto:owner-verilog-ams@eda.org] On
> Behalf Of Paul Floyd
> Sent: Thursday, January 27, 2011 1:42 PM
> To: verilog-ams@eda.org
> Subject: Expression evaluation order
>
> Hi
>
> In 4.2.3, the example given is
>
> integer A, B, C, result;
> result = A & (B | C);
>
> I find this rather curious. In C like languages, evaluation short
> circuits only apply to logical and (&&) and logical or (||), not to
> bitwise and (&) or bitwise or (|). For instance, see sections 5.11 to
> 5.15 of ISO/IEC 14882: 2003 The C++ Standard.
>
> For bitwise operators, there are two cases that could be short
> circuited: bitwise and where the left operand is zero, and bitwise or
> where the left operand is all ones.
>
> My feeling is that this should be limited to logical operators, and
> that
> the text should be more prescriptive. Just saying that it doesn't need
> to be evaluated is not strong enough.
>
> I.e., state explicitly that for a logical and, if the left operand is
> zero then the right operand will not be evaluated. The same for a
> logical or, if the left operand is non-zero.
>
>
> Regards
> Paul Floyd
> --
> Dr Paul Floyd
> Mentor Graphics Corporation
>
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Thu Jan 27 04:19:52 2011

This archive was generated by hypermail 2.1.8 : Thu Jan 27 2011 - 04:19:55 PST