1 // Common datastructures (lists, tables, closures)
9 #define LIST_LENGTH_BITS 64
10 #define LIST_FREE_BITS 48
11 #define LIST_ATOMIC_BITS 1
12 #define LIST_REFCOUNT_BITS 3
13 #define LIST_STRIDE_BITS 12
15 #define MAX_FOR_N_BITS(N) ((1L << (N)) - 1L)
16 #define LIST_MAX_STRIDE MAX_FOR_N_BITS(LIST_STRIDE_BITS - 1)
17 #define LIST_MIN_STRIDE (~MAX_FOR_N_BITS(LIST_STRIDE_BITS - 1))
18 #define LIST_MAX_DATA_REFCOUNT MAX_FOR_N_BITS(LIST_REFCOUNT_BITS)
19 #define LIST_MAX_FREE_ENTRIES MAX_FOR_N_BITS(LIST_FREE_BITS)
24 #define Int64_t int64_t
25 #define Int32_t int32_t
26 #define Int16_t int16_t
28 #define Byte_t uint8_t
36 #define OptionalInt_t Int_t
40 // All of the following fields add up to 64 bits, which means that list
41 // structs can be passed in two 64-bit registers. C will handle doing the
42 // bit arithmetic to extract the necessary values, which is cheaper than
43 // spilling onto the stack and needing to retrieve data from the stack.
44 uint64_t length : LIST_LENGTH_BITS;
45 uint64_t free : LIST_FREE_BITS;
46 bool atomic : LIST_ATOMIC_BITS;
47 uint8_t data_refcount : LIST_REFCOUNT_BITS;
48 int16_t stride : LIST_STRIDE_BITS;
52 uint32_t occupied : 1, index : 31;
56 // Maximum bucket size is determined by the maximum value for `index` in the `bucket_t` struct
57 #define TABLE_MAX_BUCKETS 0x7fffffff
58 #define TABLE_MAX_DATA_REFCOUNT 3
61 uint32_t count : 31, last_free : 31;
62 uint8_t data_refcount : 2;
66 typedef struct table_s {
69 bucket_info_t *bucket_info;
70 struct table_s *fallback;
73 typedef struct Present$$struct {
76 #define PRESENT_STRUCT ((Present$$type){})
81 } $OptionalPresent$$type;
83 #define NONE_PRESENT_STRUCT (($OptionalPresent$$type){.has_value = false})
84 #define OPTIONAL_PRESENT_STRUCT (($OptionalPresent$$type){.has_value = true})
90 enum text_type { TEXT_NONE, TEXT_ASCII, TEXT_GRAPHEMES, TEXT_CONCAT, TEXT_BLOB };
92 typedef struct Text_s {
93 uint64_t length : 53; // Number of grapheme clusters
102 const int32_t *graphemes;
103 // int32_t grapheme_buf[2];
106 const struct Text_s *left, *right;
110 const uint8_t *bytes;
115 typedef const char *Path_t;
117 #define OptionalPath_t Path_t
119 typedef struct Result$Success$$struct {
120 } Result$Success$$type;
123 Result$Success$$type value;
125 } $OptionalResult$Success$$type;
127 typedef struct Result$Failure$$struct {
129 } Result$Failure$$type;
132 Result$Failure$$type value;
134 } $OptionalResult$Failure$$type;
136 #define Result$Success ((Result$$type){.$tag = Result$tag$Success})
137 #define SuccessResult Result$Success
138 #define Result$tagged$Failure(msg) ((Result$$type){.$tag = Result$tag$Failure, .Failure.reason = msg})
139 #define FailureResult(...) Result$tagged$Failure(Texts(__VA_ARGS__))
141 typedef struct Result$$struct {
142 enum { Result$tag$none, Result$tag$Success, Result$tag$Failure } $tag;
144 Result$Success$$type Success;
145 Result$Failure$$type Failure;
149 #define Result_t Result$$type
151 #define OptionalBool_t uint8_t
152 #define OptionalList_t List_t
153 #define OptionalTable_t Table_t
154 #define OptionalText_t Text_t
155 #define OptionalClosure_t Closure_t
162 #define NONE_BYTE ((OptionalByte_t){.has_value = false})