The SSM runtime supports user-defined algebraic data types (ADTs), sometimes known as "tagged unions." An ADT is inhabited by one of several variants; those variants may also contain ssm_value_t fields. For more information, see languages Haskell or OCaml, which also have ADTs.
Each variant is identified by a per-ADT tag. When an ADT variant does not have any fields, the tag is stored as a packed_val in an ssm_value_t. When an ADT variant does contain fields, it is allocated on the heap with a variant-flavored ssm_mm header, where the tag is stored in the tag field.
The count field records the number of fields that are in the object, which are stored in an ssm_value_t array following the header. The "template" for each ADT object's memory layout is defined by ssm_adt1; ADT objects with more fields look like ssm_adt1 with longer fields.
|
struct | ssm_adt1 |
| The struct template of a heap-allocated ADT object. More...
|
|
◆ ssm_new_adt
#define ssm_new_adt |
( |
|
fc, |
|
|
|
tag |
|
) |
| |
Value: (fprintf(stderr, "%s:%d:ssm_new_adt(%d, %d)\n", __FILE__, __LINE__, (fc), \
(tag)), \
ssm_new_adt_int((fc), (tag)))
Definition at line 511 of file ssm.h.
◆ ssm_adt_field
Access the field of an ADT object.
The result of this macro can also be used as an l-value, i.e., it may be assigned to, e.g.,:
#define ssm_adt_field(v, i)
Access the field of an ADT object.
#define ssm_marshal(v)
Construct an ssm_value_t from a 31-bit integral value.
- Note
- The behavior of using this macro on an ssm_value_t that does not point to an ADT object of sufficient size (val_count greater than i) is undefined.
- Parameters
-
v | the ssm_value_t pointing to a heap-allocated ADT object. |
i | the 0-base index of the field in v to be accessed. |
- Returns
- the i'th field of v.
Definition at line 536 of file ssm.h.
◆ ssm_tag
Retrieve the tag of an ADT object.
- Note
- The behavior of using this macro on an ssm_value_t that points to anything other than an ADT object is undefined.
- Parameters
-
- Returns
- the tag of v.
Definition at line 547 of file ssm.h.
◆ ssm_adt_field_count
#define ssm_adt_field_count |
( |
|
v | ) |
((v).heap_ptr->info.variant.count) |
Obtain number of fields in the ADT pointed by v.
Definition at line 551 of file ssm.h.
◆ ssm_adt_size
#define ssm_adt_size |
( |
|
val_count | ) |
(sizeof(struct ssm_adt1) + sizeof(ssm_value_t) * ((val_count)-1)) |
Compute the size of a heap-allocated ADT.
- Parameters
-
val_count | the val_count field of the ADT object's ssm_mm header. |
- Returns
- the size of the ADT object in the heap.
Definition at line 558 of file ssm.h.
◆ ssm_adt_heap_size
Compute the size of an ADT already allocated on the heap.
Definition at line 562 of file ssm.h.
◆ ssm_new_adt_int()
ssm_value_t ssm_new_adt_int |
( |
uint8_t |
field_count, |
|
|
uint8_t |
tag |
|
) |
| |
Allocate a new ADT object on the heap.
- Note
- This function fully initializes the ssm_mm header of the ADT object, but leaves its fields uninitialized. It is the responsibility of the caller to properly all val_count fields.
- Parameters
-
field_count | the number of fields in the ADT object. |
tag | the tag of the ADT object, stored in the ssm_mm header. |
- Returns
- ssm_value_t poining to the ADT object on the heap.
Definition at line 274 of file ssm-mem.c.