Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
intro_example:pure_struct [2025/05/01 19:54] – rajit | intro_example:pure_struct [2025/05/18 15:07] (current) – rajit | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Using pure structures ====== | ====== Using pure structures ====== | ||
- | A pure structure is a user-defined data type that contains only '' | + | A [[language: |
+ | make CHP more human-readable. An example of a pure structure is: | ||
<code act> | <code act> | ||
Line 24: | Line 25: | ||
[] else -> log (" | [] else -> log (" | ||
] | ] | ||
- | []~cmd -> // write command | + | |
| | ||
| | ||
Line 36: | Line 37: | ||
(Note: this is not meant to be a working cache!) | (Note: this is not meant to be a working cache!) | ||
+ | |||
+ | ===== Methods ===== | ||
We can abstract away the matching function as follows: | We can abstract away the matching function as follows: | ||
Line 47: | Line 50: | ||
{ | { | ||
chp { | chp { | ||
- | self := valid & tag = addr{31..8}; | + | self := valid & tag = addr{31..8} |
} | } | ||
} | } | ||
Line 54: | Line 57: | ||
- | defproc cache(chan? | + | defproc cache(chan? |
{ | { | ||
| | ||
Line 68: | Line 71: | ||
[] else -> log (" | [] else -> log (" | ||
] | ] | ||
- | []~cmd -> // write command | + | []~cmd -> // write command |
| | ||
| | ||
Line 78: | Line 81: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Note that method functions, like other functions in ACT, are pure functions. In other words, they cannot change the fields of the structure. If you need to modify the fields of the structure, special '' | ||
+ | |||
+ | <code act> | ||
+ | deftype cache_entry (bool valid; int< | ||
+ | { | ||
+ | methods { | ||
+ | function match (int< | ||
+ | { | ||
+ | chp { | ||
+ | self := valid & tag = addr{31..8} | ||
+ | } | ||
+ | } | ||
+ | macro setval (int< | ||
+ | valid+; | ||
+ | data := dv; | ||
+ | tag := addr{31..8} | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | defproc cache(chan? | ||
+ | { | ||
+ | | ||
+ | | ||
+ | bool cmd; | ||
+ | | ||
+ | |||
+ | chp { | ||
+ | *[ A? | ||
+ | [cmd -> // read command | ||
+ | ce := C[addr{7..0}]; | ||
+ | [ ce.match(addr) -> log (" | ||
+ | [] else -> log (" | ||
+ | ] | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | ] | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Converting to raw integers ===== | ||
+ | |||
+ | A common requirement is to convert a pure structure data type into a collection of bits, corresponding to an integer with a bit-width that is the sum of the widths of all the elements within the pure structure. | ||
+ | |||
+ | In the example above, this can be done as follows: | ||
+ | |||
+ | <code act> | ||
+ | int< | ||
+ | ... | ||
+ | chp { | ||
+ | ...; | ||
+ | x := int(ce); | ||
+ | ... | ||
+ | |||
+ | ce := cache_type(x); | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The order in which the bits are packed is from left to right in the structure definition, with the left-most field corresponding to the most significant bits of the integer. |