Mantis 1041

P1800-2008/D4, 12.5

Clarify case item evaluation order

In Section 12.5

In Syntax 12-3 (in 12.5) and in A.6.7

CHANGE                                                                                                                                                                         

case_statement ::=

  [ unique_priority ] case_keyword ( expression ) case_item { case_item } endcase

| [ unique_priority ] case_keyword ( expression ) matches case_pattern_item { case_pattern_item } endcase

| [ unique_priority ] case ( expression ) inside case_inside_item { case_inside_item } endcase

case_keyword ::= case | casez | casex

 

case_item ::=

  expression { , expression } : statement_or_null

| default [ : ] statement_or_null

 

case_pattern_item ::=

  pattern [ &&& expression ] : statement_or_null

| default [ : ] statement_or_null

 

case_inside_item ::=

  open_range_list : statement_or_null

| default [ : ] statement_or_null

 

randcase_statement ::=

randcase randcase_item { randcase_item } endcase

randcase_item ::= expression : statement_or_null

TO

case_statement ::=

  [ unique_priority ] case_keyword ( case_expression ) case_item { case_item } endcase

| [ unique_priority ] case_keyword ( case_expression ) matches case_pattern_item { case_pattern_item } endcase

| [ unique_priority ] case ( case_expression ) inside case_inside_item { case_inside_item } endcase

case_keyword ::= case | casez | casex

case_expression ::= expression

 

case_item ::=

  case_item_expression { , case_item_expression } : statement_or_null

| default [ : ] statement_or_null

 

case_pattern_item ::=

  pattern [ &&& expression ] : statement_or_null

| default [ : ] statement_or_null

 

case_inside_item ::

  open_range_list : statement_or_null

| default [ : ] statement_or_null

case_item_expression ::= expression

 

randcase_statement ::=

randcase randcase_item { randcase_item } endcase

randcase_item ::= expression : statement_or_null

 

In 12.5

CHANGE

The case expression and the case item expression can be computed at run time; neither expression is required to be a constant expression.

 

...

 

The case expression given in parentheses shall be evaluated exactly once and before any of the case item expressions. The case item expressions shall be evaluated and compared in the exact order in which they are given. If there is a default case item, it is ignored during this linear search. During the linear search, if one of the case item expressions matches the case expression given in parentheses, then the statement associated with that case item shall be executed, and the linear search shall terminate. If all comparisons fail and the default item is given, then the default item statement shall be executed. If the default statement is not given and all of the comparisons fail, then none of the case item statements shall be executed.

 

Apart from syntax, the case statement differs from the multiway if-else-if construct in two important ways:

a) The conditional expressions in the if–else–if construct are more general than comparing one expression with several others, as in the case statement.

b) The case statement provides a definitive result when there are x and z values in an expression.

 

In a case expression comparison, the comparison only succeeds when each bit matches exactly with respect to the values 0, 1, x, and z. As a consequence, care is needed in specifying the expressions in the case statement. The bit length of all the expressions shall be equal so that exact bitwise matching can be performed. The length of all the case item expressions, as well as the case expression in the parentheses, shall be made equal to the length of the longest case expression and case item expression. If any of these expressions is unsigned, then all of them shall be treated as unsigned. If all of these expressions are signed, then they shall be treated as signed.

 

The reason for providing a case expression comparison that handles the x and z values is that it provides a mechanism for detecting such values and reducing the pessimism that can be generated by their presence.

TO

The case expression and the case item expression can be computed at run time; neither expression is required to be a constant expression.
The case_expression and case_item expressions are not required to be constant expressions.

 

...

 

The case_expression  case expression given in parentheses shall be evaluated exactly once and before any of the case_item expressions case item expressions. The case_item expressions case item expressions shall be evaluated and then compared, in the exact order in which they are given appear. If there is a default case_item case item, it is ignored during this linear search. During the linear search, if one of the case_item expressions case item expressions matches the case_expression  case expression given in parentheses, then the statement associated with that case_item case item shall be executed, and the linear search shall terminate. If all comparisons fail and the default item is given, then the default item statement shall be executed. If the default statement is not given and all of the comparisons fail, then none of the case_item case item statements shall be executed.

 

Apart from syntax, the case statement differs from the multiway if-else-if construct in two important ways:

a) The conditional expressions in the if–else–if construct are more general than comparing one expression with several others, as in the case statement.

b) The case statement provides a definitive result when there are x and z values in an expression.

 

In a case_expression  case expression comparison, the comparison only succeeds when each bit matches exactly with respect to the values 0, 1, x, and z. As a consequence, care is needed in specifying the expressions in the case statement. The bit length of all the expressions shall be equal so that exact bitwise matching can be performed. The length of all the case_item expressions case item expressions, as well as the case_expression  case expression in the parentheses, shall be made equal to the length of the longest case_expression  case expression and case_item expressions case item expression. If any of these expressions is unsigned, then all of them shall be treated as unsigned. If all of these expressions are signed, then they shall be treated as signed.

 

The reason for providing a case_expression  case expression comparison that handles the x and z values is that it provides a mechanism for detecting such values and reducing the pessimism that can be generated by their presence.

 

 

In Section 12.5.1,

CHANGE

 

Do-not-care values (z values for casez, z and x values for casex) in any bit of either the case expression or the case items shall be treated as do-not-care conditions during the comparison, and that bit position shall not be considered. The do-not-care conditions in case expression can be used to control dynamically which bits should be compared at any time.

 

TO

Do-not-care values (z values for casez, z and x values for casex) in any bit of either the case expression case_expression or the case items case_items shall be treated as do-not-care conditions during the comparison, and that bit position shall not be considered. The do-not-care conditions in case expression can be used to control dynamically which bits should be compared at any time.

 

 

In Section 12.5.2,

CHANGE

12.5.2 Constant expression in case statement

A constant expression can be used for case expression. The value of the constant expression shall be compared against case item expressions.

...

In this example, the case expression is a constant expression (1). The case items are expressions (bit-selects) and are compared against the constant expression for a match.

 

TO

12.5.2 Constant expression in case statement

A constant expression can be used for the case_expression case expression. The value of the constant expression shall be compared against the case_item expressions case item expressions.

...

In this example, the case_expression case expression is a constant expression (1). The case items case_items are expressions (bit-selects) and are compared against the constant expression for a match.

 

 

In Section 12.5.3,

CHANGE

12.5.3 unique-case and priority-case

 

The case, casez, and casex keywords can be qualified by priority or unique keywords, collectively referred to as a priority-case or unique-case. A priority-case shall act on the first match only. A unique-case asserts that there are no overlapping case items and hence that it is safe for the case items to be evaluated in parallel.

 

In a unique-case, it shall be legal to evaluate a case item expression at any time after the evaluation of the case expression and before the evaluation of the corresponding comparison. A unique-case shall be illegal if, for any such interleaving of evaluations and comparisons, more than one case item matches the case expression. For an illegal unique-case, an implementation shall be required to issue a warning message, unless it can demonstrate a legal interleaving of evaluations and comparisons so that no more than one case item matches the case expression. To implement this requirement, an implementation can continue the evaluations and comparisons after the termination of the usual linear search and before the execution of the statement associated with the first matching case item. However, the statements associated with any additional matching case items shall not be executed.

 

If the case is qualified as priority or unique, the simulator shall issue a warning message if no case item matches. These warnings can be issued at either compile time or run time, as soon as it is possible to determine the illegal condition.

 

TO

12.5.3 unique-case and priority-case

 

The case, casez, and casex keywords can be qualified by priority or unique keywords, collectively referred to as a priority-case or unique-case. A priority-case shall act on the first match only. A unique-case asserts that there are no overlapping case_items case items and hence that it is safe for the case_items case items to be evaluated in parallel.

 

In a unique-case, it shall be legal to evaluate a case_item expression case item expression at any time after the evaluation of the case_expression case expression and before the evaluation of the corresponding comparison. A unique-case shall be illegal if, for any such interleaving of evaluations and comparisons, more than one case_item case item matches the case_expression case expression. For an illegal unique-case, an implementation shall be required to issue a warning message, unless it can demonstrate a legal interleaving of evaluations and comparisons so that no more than one case_item case item matches the case_expression case expression. To implement this requirement, an implementation can continue the evaluations and comparisons after the termination of the usual linear search and before the execution of the statement associated with the first matching case_item case item. However, the statements associated with any additional matching case_items case items shall not be executed.

 

If the case is qualified as priority or unique, the simulator shall issue a warning message if no case_item case item matches. These warnings can be issued at either compile time or run time, as soon as it is possible to determine the illegal condition.

 

 

In Section 12.5.4,

CHANGE

12.5.4 Set membership case statement

The keyword inside can be used after the parenthesized expression to indicate a set membership (see 11.4.13). In a case-inside statement, the case expression shall be compared with each case item expression (open_range_list) using the set membership inside operator. The inside operator uses asymmetric wildcard matching (see 11.4.6). Accordingly, the case expression shall be the left operand, and each case item expression shall be the right operand. The case expression given in parentheses and each case item expression in braces shall be evaluated in the order specified by a normal case, unique-case, or priority-case statement. A case item shall be matched when the inside operation compares the case expression to the case item

expression and returns 1’b1 and no match when the operation returns 1’b0 or 1’bx. If all comparisons do not match and the default item is given, the default item statement shall be executed.

 

TO

12.5.4 Set membership case statement

The keyword inside can be used after the parenthesized expression to indicate a set membership (see 11.4.13). In a case-inside statement, the case_expression case expression shall be compared with each case_item expression case item expression (open_range_list) using the set membership inside operator. The inside operator uses asymmetric wildcard matching (see 11.4.6). Accordingly, the case_expression case expression shall be the left operand, and each case_item expression case item expression shall be the right operand. The case_expression case expression given in parentheses and each case_item expression case item expression in braces shall be evaluated in the order specified by a normal case, unique-case, or priority-case statement. A case_item case item shall be matched when the inside operation compares the case_expression case expression to the case_item expression case item expression and returns 1’b1 and no match when the operation returns 1’b0 or 1’bx. If all comparisons do not match and the default item is given, the default item statement shall be executed.

 

 

In Section 17.16,

CHANGE

The keyword randcase introduces a case statement that randomly selects one of its branches. The randcase item expressions are non-negative integral values that constitute the branch weights. An item’s weight divided by the sum of all weights gives the probability of taking that branch.

...

If a branch specifies a zero weight, then that branch is not taken. If all randcase items specify zero weights, then no branch is taken and a warning can be issued.

 

TO

The keyword randcase introduces a case statement that randomly selects one of its branches. The randcase item expressions randcase_item expressions are non-negative integral values that constitute the branch weights. An item’s weight divided by the sum of all weights gives the probability of taking that branch.

...

If a branch specifies a zero weight, then that branch is not taken. If all randcase items randcase_items specify zero weights, then no branch is taken and a warning can be issued.

 

 

In Syntax 17-12 (17.17), Syntax 17-15 (17.17.3) and A.6.12

CHANGE

rs_case ::= case ( expression ) rs_case_item { rs_case_item } endcase

 

rs_case_item ::=

  expression { , expression } : production_item ;

| default [ : ] production_item ;

TO

rs_case ::= case ( case_expression ) rs_case_item { rs_case_item } endcase

 

rs_case_item ::=

  case_item_expression { , case_item_expression } : production_item ;

| default [ : ] production_item ;

 

In Syntax 17-12 (17.17) and Syntax 17-15 (17.17.3)

ADD at end

case_expression ::= expression  // from A.6.7

case_item_expression ::= expression

 

In Section 17.17.3,
CHANGE

This example defines the production SELECT with a case statement. The case expression (device & 7) is evaluated and compared against the two case_item expressions. If the expression matches 0, the production NETWORK is generated; and if it matches 1 or 2, the production DISK is generated. Otherwise, the production MEMORY is generated.

 

TO

This example defines the production SELECT with a case statement. The case_expression case expression (device & 7) is evaluated and compared against the two case_item expressions case item expressions. If the expression matches 0, the production NETWORK is generated; and if it matches 1 or 2, the production DISK is generated. Otherwise, the production MEMORY is generated.