Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
language:types2:data [2025/04/21 17:29] – [Enumerations] rajitlanguage:types2:data [2025/05/02 10:13] (current) rajit
Line 3: Line 3:
 A user-defined data type is defined using ''deftype''. These are classified into three categories: A user-defined data type is defined using ''deftype''. These are classified into three categories:
    * //data types//, which correspond to representations of the built-in ''int'' and ''bool'' types.    * //data types//, which correspond to representations of the built-in ''int'' and ''bool'' types.
-   * //enumerations//, which are data types with symbolic  values. 
    * //structures//, which are a collection of data types.    * //structures//, which are a collection of data types.
    * //parameter structures//, which are a collection of parameter types.    * //parameter structures//, which are a collection of parameter types.
 +
 +In addition, data types can also be   //enumerations//, which are data types with symbolic  values.
  
 ===== Data types ===== ===== Data types =====
Line 104: Line 105:
 semantics of the channel permit waiting.) semantics of the channel permit waiting.)
  
-===== Enumerations ===== 
- 
-A user-defined enumeration can be specified as follows: 
- 
-<code act> 
-defenum myenum {  
-   ADD, SUB, MULT 
- }; 
-</code> 
- 
-This defines an enumeration called ''myenum'', and three elements.  Internally, the symbolic names are replaced with integers starting with ''0'', and the circuits generated for the enumeration are similar to normal integer types but with a bitwidth that is large enough to represent all the enumeration values. 
- 
-The enumeration constants can be accessed using the enumeration type name as follows: 
- 
-<code act> 
-... 
-myenum x; 
-... 
-chp { 
-   ... 
-   x := myenum.SUB; 
-   ... 
-} 
-</code> 
  
 ===== Structures ===== ===== Structures =====
Line 207: Line 184:
 q = myps (4, 8, false);  // constructor q = myps (4, 8, false);  // constructor
 </code> </code>
 +
 +===== Enumerations =====
 +
 +A user-defined enumeration can be specified as follows:
 +
 +<code act>
 +defenum myenum { 
 +   ADD, SUB, MULT
 + };
 +</code>
 +
 +This defines an enumeration called ''myenum'', and three elements.  Internally, the symbolic names are replaced with integers starting with ''0'', and the circuits generated for the enumeration are similar to normal integer types but with a bitwidth that is large enough to represent all the enumeration values.
 +
 +The enumeration constants can be accessed using the enumeration type name as follows:
 +
 +<code act>
 +...
 +myenum x;
 +...
 +chp {
 +   ...
 +   x := myenum.SUB;
 +   ...
 +}
 +</code>
 +
 +Trying to set an enumeration to an integer will result in a type error:
 +
 +<code act>
 +chp {
 +   ...
 +   x := 1;
 +   ...
 +}   
 +</code>
 +
 +will lead to:
 +
 +<code>
 +-[ERROR]-> Typechecking failed on CHP assignment
 +  stmt: x := 1
 + Enumeration/non-enumeration types are incompatible
 +</code>
 +
 +However, sometimes a user may want to use user-defined enumerations as integers. In order to do so,
 +the enumeration can be declared to be a valid integer as follows:
 +
 +<code act>
 +defenum myenum : int { 
 +   ADD, SUB, MULT
 + };
 +</code>
 +
 +This version will permit integer assignments. Note that it is up to the user to ensure that integer assignments are within range. The ACT simulator ''actsim'' will check, but in principle any assignment that fits within the smallest number of bits required to represent the enumeration might be generated by circuit synthesis. Hence, using the '':int'' decorator to allow integer assignments is not recommended.