Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
language:types [2021/11/21 11:25] rajit |
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. | ||
- | < | + | < |
bool a, | bool a, | ||
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. | ||
- | < | + | < |
bool a; | bool a; | ||
pint a; | pint a; | ||
+ | </ | ||
+ | < | ||
-[ERROR]-> | -[ERROR]-> | ||
</ | </ | ||
Line 68: | Line 70: | ||
A parameter instantiation can be accompanied by a single // | A parameter instantiation can be accompanied by a single // | ||
which initializes the value of a variable. | which initializes the value of a variable. | ||
- | < | + | < |
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 | ||
- | < | + | < |
pint a=c, c=5; | pint a=c, c=5; | ||
+ | </ | ||
+ | < | ||
-[ERROR]-> | -[ERROR]-> | ||
</ | </ | ||
Line 92: | Line 96: | ||
examples of creating arrays are shown below: | examples of creating arrays are shown below: | ||
- | < | + | < |
int ar1[4] | int ar1[4] | ||
preal ar2[7] | preal ar2[7] | ||
Line 107: | Line 111: | ||
specifications, | specifications, | ||
- | < | + | < |
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 '' | type. Variables used must always be parameter types (typically '' | ||
- | < | + | < |
preal a = 4.3; | preal a = 4.3; | ||
bool ar6[7*a+5]; | bool ar6[7*a+5]; | ||
+ | </ | ||
+ | < | ||
-[ERROR]-> | -[ERROR]-> | ||
</ | </ | ||
Line 125: | Line 131: | ||
the example below. | the example below. | ||
- | < | + | < |
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. | ||
- | < | + | < |
bool n[4..4], n[6..6]; | bool n[4..4], n[6..6]; | ||
</ | </ | ||
Line 144: | Line 150: | ||
dynamically extended in ACT. | dynamically extended in 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: | ||
'' | '' | ||
- | < | + | < |
bool m[6..6][5..10] | bool m[6..6][5..10] | ||
</ | </ | ||
Line 158: | Line 164: | ||
Note that this is quite different from the statement | Note that this is quite different from the statement | ||
- | < | + | < |
bool m[6][5..10]; | bool m[6][5..10]; | ||
</ | </ | ||
Line 168: | Line 174: | ||
initializers. | initializers. | ||
- | < | + | < |
bool x[10]; | bool x[10]; | ||
bool y[10] = x; | bool y[10] = x; | ||
+ | </ | ||
+ | < | ||
-[ERROR]-> | -[ERROR]-> | ||
</ | </ | ||
Line 179: | Line 187: | ||
+ | |||
+ | |||
+ | ===== Parameterized types ===== | ||
+ | |||
+ | Parameterized types give ACT considerable flexibility in type | ||
+ | definitions. Parameterized types come in two flavors: built-in types, | ||
+ | and user-defined types. For user-defined types, ACT guarantees | ||
+ | that the order in which parameters are created and initialized is from | ||
+ | left to right. Therefore, one can use the value of one parameter in | ||
+ | the definition of another one. | ||
+ | |||
+ | |||
+ | Although we have been describing the types '' | ||
+ | as simple types, they are in fact parameterized. Omitting the | ||
+ | parameters makes ACT use implicit default parameters for both of | ||
+ | them. | ||
+ | |||
+ | The '' | ||
+ | specify the integer. This bit-width can be specified using angle | ||
+ | brackets, as shown below: | ||
+ | |||
+ | <code act> | ||
+ | int< | ||
+ | int< | ||
+ | </ | ||
+ | |||
+ | When interpreting these bits as integers, ACT assumes an unsigned | ||
+ | binary representation. The default bit-width is thirty-two. | ||
+ | |||
+ | The channel type '' | ||
+ | being sent and received on the channel. | ||
+ | |||
+ | <code act> | ||
+ | chan(bool) x; // x is a Boolean channel | ||
+ | chan(int< | ||
+ | </ | ||
+ | |||
+ | The default data type for a channel is assumed to be the default | ||
+ | '' | ||
+ | |||
+ | Channels are almost always unidirectional, | ||
+ | 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, | ||
+ | </ | ||
+ | These are sometimes called //exchange channels//, since data is exchanged between the sender and receiver. | ||
+ | |||
+ | |||
+ | Another built-in data type is the // | ||
+ | enumeration type corresponds to integer-valued variables with a | ||
+ | restricted range. | ||
+ | |||
+ | <code act> | ||
+ | enum< | ||
+ | </ | ||
+ | |||
+ | For convenience, | ||
+ | of expressions. Also, enumerations that have power-of-two ranges are | ||
+ | type-equivalent to the approprate '' | ||
+ | '' | ||
+ | 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 '' | ||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | 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 '' | ||
+ | output on '' | ||
+ | |||
+ | <code act> | ||
+ | defcell nand2 (bool? a, b; bool! c) { ... } | ||
+ | </ | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | 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. | ||