ssm  0.0.2
Runtime Library for the Sparse Synchronous Model
Algebraic data types

Discussion

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.

Data Structures

struct  ssm_adt1
 The struct template of a heap-allocated ADT object. More...
 

Macros

#define ssm_new_adt(fc, tag)
 
#define ssm_adt_field(v, i)    (&*container_of((v).heap_ptr, struct ssm_adt1, mm)->fields)[i]
 Access the field of an ADT object. More...
 
#define ssm_tag(v)    (ssm_on_heap(v) ? (v).heap_ptr->info.variant.tag : ssm_unmarshal(v))
 Retrieve the tag of an ADT object. More...
 
#define ssm_adt_field_count(v)   ((v).heap_ptr->info.variant.count)
 Obtain number of fields in the ADT pointed by v. More...
 
#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. More...
 
#define ssm_adt_heap_size(v)   ssm_adt_size(ssm_adt_field_count(v))
 Compute the size of an ADT already allocated on the heap. More...
 

Functions

ssm_value_t ssm_new_adt_int (uint8_t field_count, uint8_t tag)
 Allocate a new ADT object on the heap. More...
 

Macro Definition Documentation

◆ 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

#define ssm_adt_field (   v,
 
)     (&*container_of((v).heap_ptr, struct ssm_adt1, mm)->fields)[i]

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.
Definition: ssm.h:536
#define ssm_marshal(v)
Construct an ssm_value_t from a 31-bit integral value.
Definition: ssm.h:291
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
vthe ssm_value_t pointing to a heap-allocated ADT object.
ithe 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

#define ssm_tag (   v)     (ssm_on_heap(v) ? (v).heap_ptr->info.variant.tag : ssm_unmarshal(v))

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
vthe ssm_value_t whose tag is being retrieved.
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_countthe 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

#define ssm_adt_heap_size (   v)    ssm_adt_size(ssm_adt_field_count(v))

Compute the size of an ADT already allocated on the heap.

Definition at line 562 of file ssm.h.

Function Documentation

◆ 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_countthe number of fields in the ADT object.
tagthe 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.