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 14:34] – [Data methods] rajitlanguage:types2:data [2025/05/02 10:13] (current) rajit
Line 5: Line 5:
    * //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 60: Line 62:
 ==== Special data methods ==== ==== Special data methods ====
  
-There are two methods that can be specified for a data type:+There are two special methods that can be specified for a data type:
    - a //set method//, used to write a value to the type;    - a //set method//, used to write a value to the type;
    - a //get method//, used to read the value of the type.    - a //get method//, used to read the value of the type.
Line 127: Line 129:
 to what is normally viewed as a record/struct in a normal software programming language. to what is normally viewed as a record/struct in a normal software programming language.
  
 +Pure structures can be used with special function methods, providing a degree of object-oriented
 +programming capability within ACT.
 +
 +The following is an example of a pure structure:
 +<code act>
 +deftype purestruct (int<4> a; int<8> b; bool q) { }
 +</code>
 +
 +Pure structures can be converted to and from an ''int'' type of the appropriate bit-width. 
 +
 +<code act>
 +purestruct p;
 +int<13> x;
 +...
 +chp {
 +   ...
 +   x := int(p);
 +   ...
 +   p := purestruct(x);
 +   ...
 +}
 +</code>
 +
 +In the example above, the 'int()' operator can be used to convert the pure structure into a single integer with a bit-width that is large enough to hold all the elements of the structure. In this example, we require 13 bits to hold the 'int<4>', 'int<8>', and 'bool' member of the structure. The order of packing is left to right, so the assignment is the same as:
 +<code act>
 +  ...
 +  x := {p.a, p.b, p.q};
 +  ...
 +</code>
 +
 +Similarly, the ''purestruct()'' constructor can be used to unpack an integer of the appropriate bit-width assuming that the integer is of the format generated by ''int()''.
 +===== Parameter structures =====
 +
 +It can be convenient to group a collection of parameters for readability/organization. To support this, ACT includes parameter structures. The following is an example of a parameter structure:
 +<code act>
 + defptype myps (pint a, b; pbool c) { }
 +</code>
 +
 +This defines ''myps'' as a parameter type, which means it can be used as a template parameter in process definitions. For example, the following would be valid:
 +<code act>
 +template <myps p> defproc myprocess (bool x[10]) 
 +{   
 +   // use p.a, p.b etc.
 +}
 +</code>
 +
 +Parameter structures can be initialized element-by-element in the usual way, or by using a built-in constructor for them:
 +<code act>
 +myps p;
 +p.a = 5;  // element initialization
 +
 +myps q;
 +
 +q = myps (4, 8, false);  // constructor
 +</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.