Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
language:types [2021/11/21 11:56]
rajit [Parameterized types]
language:types [2023/04/09 19:55] (current)
rajit [Parameterized types]
Line 50: Line 50:
 name followed by a comma-separated list of identifier names. name followed by a comma-separated list of identifier names.
  
-<code>+<code act>
 bool a,b,c,n1,n1x2; bool a,b,c,n1,n1x2;
 pint x,y,z; pint x,y,z;
Line 60: Line 60:
 to have more than one instantiation of a variable in the same scope. to have more than one instantiation of a variable in the same scope.
  
-<code>+<code act>
 bool a; bool a;
 pint a; pint a;
 +</code>
 +<code>
 -[ERROR]-> Duplicate instance for name `a' -[ERROR]-> Duplicate instance for name `a'
 </code> </code>
Line 68: Line 70:
 A parameter instantiation can be accompanied by a single //initializer// A parameter instantiation can be accompanied by a single //initializer//
 which initializes the value of a variable. which initializes the value of a variable.
-<code>+<code act>
 pint a=5, c=8; pint a=5, c=8;
 preal b=8.9; preal b=8.9;
Line 76: Line 78:
 Using constructs such as Using constructs such as
  
-<code>+<code act>
 pint a=c, c=5; pint a=c, c=5;
 +</code>
 +<code>
 -[ERROR]-> The identifier `c' does not exist in the current scope -[ERROR]-> The identifier `c' does not exist in the current scope
 </code> </code>
Line 92: Line 96:
 examples of creating arrays are shown below: examples of creating arrays are shown below:
  
-<code>+<code act>
 int ar1[4] int ar1[4]
 preal ar2[7] preal ar2[7]
Line 107: Line 111:
 specifications, as shown below. specifications, as shown below.
  
-<code>+<code act>
 int ar4[5*3] int ar4[5*3]
 preal ar5[7*x+(y%2)-p] // here x, y, and p must be integer parameter types preal ar5[7*x+(y%2)-p] // here x, y, and p must be integer parameter types
Line 115: Line 119:
 type. Variables used must always be parameter types (typically ''pint''). type. Variables used must always be parameter types (typically ''pint'').
  
-<code>+<code act>
 preal a = 4.3; preal a = 4.3;
 bool ar6[7*a+5]; bool ar6[7*a+5];
 +</code>
 +<code>
 -[ERROR]-> Expression must be of type int -[ERROR]-> Expression must be of type int
 </code> </code>
Line 125: Line 131:
 the example below. the example below.
  
-<code>+<code act>
 bool x[5,3]; bool x[5,3];
 bool y[1..6][9][2..10]; bool y[1..6][9][2..10];
Line 136: Line 142:
 aforementioned array is shown below. aforementioned array is shown below.
  
-<code>+<code act>
 bool n[4..4], n[6..6]; bool n[4..4], n[6..6];
 </code> </code>
Line 144: Line 150:
 dynamically extended in ACT. dynamically extended in ACT.
  
-<code>+<code act>
 bool n[5]; bool n[5];
 bool n[10..12]; // n is now defined at positions 0 to 4, 10 to 12 bool n[10..12]; // n is now defined at positions 0 to 4, 10 to 12
Line 152: Line 158:
 ''m'' at positions ''[6][5]'', ''[6][6]'', ..., ''[6][10]''. ''m'' at positions ''[6][5]'', ''[6][6]'', ..., ''[6][10]''.
  
-<code>+<code act>
 bool m[6..6][5..10] bool m[6..6][5..10]
 </code> </code>
Line 158: Line 164:
 Note that this is quite different from the statement Note that this is quite different from the statement
  
-<code>+<code act>
 bool m[6][5..10]; bool m[6][5..10];
 </code> </code>
Line 168: Line 174:
 initializers. initializers.
  
-<code>+<code act>
 bool x[10]; bool x[10];
 bool y[10] = x; bool y[10] = x;
 +</code>
 +<code>
 -[ERROR]-> Connection can only be specified for non-array instances -[ERROR]-> Connection can only be specified for non-array instances
 </code> </code>
Line 200: Line 208:
 brackets, as shown below: brackets, as shown below:
  
-<code>+<code act>
 int<1> x; // x is a one bit integer int<1> x; // x is a one bit integer
 int<37> y; // y is a thirty-seven bit integer int<37> y; // y is a thirty-seven bit integer
Line 211: Line 219:
 being sent and received on the channel. being sent and received on the channel.
  
-<code>+<code act>
 chan(bool) x; // x is a Boolean channel chan(bool) x; // x is a Boolean channel
 chan(int<16>) y; // y is a 16-bit integer channel chan(int<16>) y; // y is a 16-bit integer channel
Line 218: Line 226:
 The default data type for a channel is assumed to be the default The default data type for a channel is assumed to be the default
 ''int'', namely ''int<32>''. ''int'', namely ''int<32>''.
 +
 +Channels are almost always unidirectional, with data being transferred from sender to receiver.
 +In a few cases, it is useful to be able to transfer data from the sender to the receiver, and from the
 +receiver to the sender in one channel action. To declare a channel where data are transferred in
 +both directions, use:
 +<code act>
 +// a bool is transferred from sender to receiver, and
 +// an int is transferred from the receiver to the sender
 +chan(bool,int) x;
 +</code>
 +These are sometimes called //exchange channels//, since data is exchanged between the sender and receiver.
 +
  
 Another built-in data type is the //enumeration// type. An Another built-in data type is the //enumeration// type. An
Line 223: Line 243:
 restricted range. restricted range.
  
-<code>+<code act>
 enum<5> x; // x can take on values 0, 1, 2, 3, 4 enum<5> x; // x can take on values 0, 1, 2, 3, 4
 </code> </code>
Line 233: Line 253:
 useful when specifying a data value that is a one-hot code. useful when specifying a data value that is a one-hot code.
  
 +===== Directional types =====
 +
 +Data and channel types also support access permissions in terms of valid
 +operations on the types. To illustrate this, consider the simplest data
 +type, namely a ''bool''. There are three different ways a ''bool''
 +type can be defined, and they are shown below:
 +
 +<code act>
 +bool x;  // Boolean that may be read or written
 +bool! y; // Boolean that must be written, and may be read
 +bool? z; // Boolean that must be read, and cannot be written
 +</code>
 +
 +The ''!'' and ''?'' suffixes constrain the way in which the type
 +can be accessed. The primary use of this is in port lists, where one can
 +specify what variables are read and written by a process. The same
 +syntax can be used (with the same meaning) for user-defined data types.
 +
 +The following example shows a possible definition for a two-input nand
 +gate that takes two inputs ''a'' and ''b'', and produces its
 +output on ''c''.
 +
 +<code act>
 +defcell nand2 (bool? a, b; bool! c) { ... }
 +</code>
 +
 +Channels support a similar syntax, but the meaning is slightly
 +different.
 +
 +<code act>
 +chan(int) x;  // Sends or receives are permitted
 +chan!(int) y; // Only sends permitted
 +chan?(int) z; // Only receives permitted
 +</code>
 +
 +Again, the same syntax is valid for user-defined channels. These
 +constructs are useful in libraries for additional error checking, and
 +conveying more information to the user of the library.