backburn 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/Makefile +50 -0
  4. data/README.md +520 -0
  5. data/docs/cli-and-outputs.md +226 -0
  6. data/docs/technical-spec.md +656 -0
  7. data/exe/backburn +8 -0
  8. data/ext/backburn/extconf.rb +31 -0
  9. data/ext/backburn/prism/COMMIT +1 -0
  10. data/ext/backburn/prism/LICENSE.md +7 -0
  11. data/ext/backburn/prism/Makefile +213 -0
  12. data/ext/backburn/prism/include/prism/arena.h +45 -0
  13. data/ext/backburn/prism/include/prism/arena.hpp +42 -0
  14. data/ext/backburn/prism/include/prism/ast.h +10395 -0
  15. data/ext/backburn/prism/include/prism/ast.hpp +641 -0
  16. data/ext/backburn/prism/include/prism/buffer.h +60 -0
  17. data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
  18. data/ext/backburn/prism/include/prism/comments.h +72 -0
  19. data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
  20. data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
  21. data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
  22. data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
  23. data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
  24. data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
  25. data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
  26. data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
  27. data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
  28. data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
  29. data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
  30. data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
  31. data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
  32. data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
  33. data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
  34. data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
  35. data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
  36. data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
  37. data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
  38. data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
  39. data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
  40. data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
  41. data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
  42. data/ext/backburn/prism/include/prism/errors_format.h +63 -0
  43. data/ext/backburn/prism/include/prism/excludes.h +29 -0
  44. data/ext/backburn/prism/include/prism/integer.h +79 -0
  45. data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
  46. data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
  47. data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
  48. data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
  49. data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
  50. data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
  51. data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
  52. data/ext/backburn/prism/include/prism/internal/char.h +145 -0
  53. data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
  54. data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
  55. data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
  56. data/ext/backburn/prism/include/prism/internal/double.h +19 -0
  57. data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
  58. data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
  59. data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
  60. data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
  61. data/ext/backburn/prism/include/prism/internal/list.h +62 -0
  62. data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
  63. data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
  64. data/ext/backburn/prism/include/prism/internal/node.h +197 -0
  65. data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
  66. data/ext/backburn/prism/include/prism/internal/options.h +248 -0
  67. data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
  68. data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
  69. data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
  70. data/ext/backburn/prism/include/prism/internal/source.h +72 -0
  71. data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
  72. data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
  73. data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
  74. data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
  75. data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
  76. data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
  77. data/ext/backburn/prism/include/prism/json.h +40 -0
  78. data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
  79. data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
  80. data/ext/backburn/prism/include/prism/node.h +139 -0
  81. data/ext/backburn/prism/include/prism/node.hpp +272 -0
  82. data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
  83. data/ext/backburn/prism/include/prism/options.h +393 -0
  84. data/ext/backburn/prism/include/prism/options.hpp +43 -0
  85. data/ext/backburn/prism/include/prism/parser.h +433 -0
  86. data/ext/backburn/prism/include/prism/parser.hpp +274 -0
  87. data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
  88. data/ext/backburn/prism/include/prism/serialize.h +123 -0
  89. data/ext/backburn/prism/include/prism/source.h +163 -0
  90. data/ext/backburn/prism/include/prism/stream.h +36 -0
  91. data/ext/backburn/prism/include/prism/string_query.h +97 -0
  92. data/ext/backburn/prism/include/prism/stringy.h +61 -0
  93. data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
  94. data/ext/backburn/prism/include/prism/version.h +46 -0
  95. data/ext/backburn/prism/include/prism.h +123 -0
  96. data/ext/backburn/prism/include/prism.hpp +24 -0
  97. data/ext/backburn/prism/src/arena.c +200 -0
  98. data/ext/backburn/prism/src/bignum.c +399 -0
  99. data/ext/backburn/prism/src/buffer.c +405 -0
  100. data/ext/backburn/prism/src/char.c +289 -0
  101. data/ext/backburn/prism/src/comments.c +455 -0
  102. data/ext/backburn/prism/src/constant_pool.c +410 -0
  103. data/ext/backburn/prism/src/diagnostic.c +918 -0
  104. data/ext/backburn/prism/src/double.c +394 -0
  105. data/ext/backburn/prism/src/encoding.c +1701 -0
  106. data/ext/backburn/prism/src/errors_format.c +650 -0
  107. data/ext/backburn/prism/src/integer.c +675 -0
  108. data/ext/backburn/prism/src/json.c +5745 -0
  109. data/ext/backburn/prism/src/line_offset_list.c +99 -0
  110. data/ext/backburn/prism/src/list.c +24 -0
  111. data/ext/backburn/prism/src/memchr.c +37 -0
  112. data/ext/backburn/prism/src/node.c +4464 -0
  113. data/ext/backburn/prism/src/options.c +475 -0
  114. data/ext/backburn/prism/src/parser.c +371 -0
  115. data/ext/backburn/prism/src/prettyprint.c +9038 -0
  116. data/ext/backburn/prism/src/prism.c +23615 -0
  117. data/ext/backburn/prism/src/regexp.c +3833 -0
  118. data/ext/backburn/prism/src/serialize.c +2656 -0
  119. data/ext/backburn/prism/src/source.c +525 -0
  120. data/ext/backburn/prism/src/static_literals.c +562 -0
  121. data/ext/backburn/prism/src/string_query.c +195 -0
  122. data/ext/backburn/prism/src/stringy.c +77 -0
  123. data/ext/backburn/prism/src/strncasecmp.c +37 -0
  124. data/ext/backburn/prism/src/strpbrk.c +465 -0
  125. data/ext/backburn/prism/src/tokens.c +724 -0
  126. data/ext/backburn/prism/src/xxh3.c +292 -0
  127. data/lib/backburn/version.rb +7 -0
  128. data/presets/devise.roots +11 -0
  129. data/presets/fast-mcp.roots +5 -0
  130. data/presets/graphql.roots +6 -0
  131. data/presets/phlex.roots +7 -0
  132. data/presets/pundit.roots +3 -0
  133. data/presets/rails-framework.roots +10 -0
  134. data/presets/rails.roots +83 -0
  135. data/presets/sidekiq.roots +4 -0
  136. data/presets/thor.roots +3 -0
  137. data/rails.roots +99 -0
  138. data/scripts/embed-presets.sh +13 -0
  139. data/scripts/version-h.sh +7 -0
  140. data/src/analysis/analysis.c +95 -0
  141. data/src/analysis/analysis.h +24 -0
  142. data/src/analysis/batches.c +127 -0
  143. data/src/analysis/batches.h +17 -0
  144. data/src/analysis/graph.c +238 -0
  145. data/src/analysis/graph.h +34 -0
  146. data/src/analysis/mark.c +300 -0
  147. data/src/analysis/mark.h +21 -0
  148. data/src/analysis/roots.c +229 -0
  149. data/src/analysis/roots.h +29 -0
  150. data/src/cli/cli.c +237 -0
  151. data/src/cli/cli.h +9 -0
  152. data/src/cli/config.c +112 -0
  153. data/src/cli/config.h +14 -0
  154. data/src/cli/options.c +17 -0
  155. data/src/cli/options.h +35 -0
  156. data/src/commands/commands.h +11 -0
  157. data/src/commands/explain.c +255 -0
  158. data/src/commands/gemfile.c +126 -0
  159. data/src/commands/gemfile.h +27 -0
  160. data/src/commands/gems.c +408 -0
  161. data/src/commands/init.c +92 -0
  162. data/src/commands/roots.c +82 -0
  163. data/src/commands/scan.c +91 -0
  164. data/src/core/intern.c +94 -0
  165. data/src/core/intern.h +40 -0
  166. data/src/core/json.c +17 -0
  167. data/src/core/json.h +9 -0
  168. data/src/core/model.c +25 -0
  169. data/src/core/model.h +85 -0
  170. data/src/core/util.c +116 -0
  171. data/src/core/util.h +48 -0
  172. data/src/index/erb.c +40 -0
  173. data/src/index/erb.h +9 -0
  174. data/src/index/haml.c +334 -0
  175. data/src/index/haml.h +10 -0
  176. data/src/index/indexer.c +702 -0
  177. data/src/index/indexer.h +19 -0
  178. data/src/index/walk.c +144 -0
  179. data/src/index/walk.h +14 -0
  180. data/src/main.c +81 -0
  181. data/src/output/baseline.c +101 -0
  182. data/src/output/baseline.h +15 -0
  183. data/src/output/report.c +440 -0
  184. data/src/output/report.h +28 -0
  185. metadata +231 -0
@@ -0,0 +1,410 @@
1
+ #include "prism/internal/constant_pool.h"
2
+
3
+ #include "prism/compiler/align.h"
4
+ #include "prism/compiler/inline.h"
5
+ #include "prism/internal/arena.h"
6
+ #include "prism/internal/assert.h"
7
+
8
+ #include <stdbool.h>
9
+
10
+ /**
11
+ * Initialize a list of constant ids.
12
+ */
13
+ void
14
+ pm_constant_id_list_init(pm_arena_t *arena, pm_constant_id_list_t *list, uint32_t size) {
15
+ if (size) {
16
+ list->ids = PM_ARENA_ZALLOC_ARRAY(arena, pm_constant_id_t, size);
17
+ } else {
18
+ list->ids = NULL;
19
+ }
20
+
21
+ list->size = size;
22
+ }
23
+
24
+ /**
25
+ * Append a constant id to a list of constant ids. A list's array is always
26
+ * exactly its size, so it grows to exactly one more id, in place when it is the
27
+ * most recent allocation in the arena and by copying it otherwise.
28
+ */
29
+ void
30
+ pm_constant_id_list_append(pm_arena_t *arena, pm_constant_id_list_t *list, pm_constant_id_t id) {
31
+ if (list->size == 0) {
32
+ list->ids = PM_ARENA_ALLOC(arena, pm_constant_id_t);
33
+ list->ids[list->size++] = id;
34
+ } else {
35
+ list->ids = PM_ARENA_REALLOC(arena, list->ids, sizeof(pm_constant_id_t) * (list->size + 1), sizeof(pm_constant_id_t) * list->size, pm_constant_id_t);
36
+ list->ids[list->size++] = id;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Set the constant id at the specified index of a list of constant ids. The
42
+ * slot must still be unset.
43
+ */
44
+ void
45
+ pm_constant_id_list_insert(pm_constant_id_list_t *list, uint32_t index, pm_constant_id_t id) {
46
+ PRISM_ASSERT(index < list->size);
47
+ PRISM_ASSERT(list->ids[index] == PM_CONSTANT_ID_UNSET);
48
+
49
+ list->ids[index] = id;
50
+ }
51
+
52
+ /**
53
+ * The slot that holds the given id, or the empty slot it would occupy. The
54
+ * table is never full, so this terminates.
55
+ */
56
+ static PRISM_INLINE size_t
57
+ pm_constant_id_set_slot(const pm_constant_id_set_t *set, pm_constant_id_t id) {
58
+ PRISM_ASSERT(set->capacity != 0);
59
+
60
+ size_t mask = set->capacity - 1;
61
+ size_t index = ((size_t) pm_constant_id_hash(id)) & mask;
62
+
63
+ while (set->ids[index] != PM_CONSTANT_ID_UNSET && set->ids[index] != id) {
64
+ index = (index + 1) & mask;
65
+ }
66
+
67
+ return index;
68
+ }
69
+
70
+ /**
71
+ * Add a constant id to the set, reporting whether it was not already there.
72
+ */
73
+ bool
74
+ pm_constant_id_set_insert(pm_arena_t *arena, pm_constant_id_set_t *set, pm_constant_id_t id) {
75
+ /* PM_CONSTANT_ID_UNSET marks an empty slot, so it cannot also be a member. */
76
+ PRISM_ASSERT(id != PM_CONSTANT_ID_UNSET);
77
+
78
+ /*
79
+ * Grow at the same load factor the constant pool uses.
80
+ * An empty set has no slots at all, so this also builds the first table.
81
+ */
82
+ if (set->size >= (set->capacity / 4 * 3)) {
83
+ size_t capacity = set->capacity == 0 ? 8 : set->capacity * 2;
84
+ pm_constant_id_set_t grown = {
85
+ .size = set->size,
86
+ .capacity = capacity,
87
+ .ids = PM_ARENA_ZALLOC_ARRAY(arena, pm_constant_id_t, capacity)
88
+ };
89
+
90
+ for (size_t index = 0; index < set->capacity; index++) {
91
+ pm_constant_id_t moved = set->ids[index];
92
+ if (moved != PM_CONSTANT_ID_UNSET) grown.ids[pm_constant_id_set_slot(&grown, moved)] = moved;
93
+ }
94
+
95
+ *set = grown;
96
+ }
97
+
98
+ size_t index = pm_constant_id_set_slot(set, id);
99
+ if (set->ids[index] == id) return false;
100
+
101
+ set->ids[index] = id;
102
+ set->size++;
103
+ return true;
104
+ }
105
+
106
+ /**
107
+ * A multiply-xorshift hash that processes input a word at a time. This is
108
+ * significantly faster than the byte-at-a-time djb2 hash for the short strings
109
+ * typical in Ruby source (~15 bytes average). Each word is mixed into the hash
110
+ * by XOR followed by multiplication by a large odd constant, which spreads
111
+ * entropy across all bits. A final xorshift fold produces the 32-bit result.
112
+ */
113
+ static PRISM_INLINE uint32_t
114
+ pm_constant_pool_hash(const uint8_t *start, size_t length) {
115
+ // This constant is borrowed from wyhash. It is a 64-bit odd integer with
116
+ // roughly equal 0/1 bits, chosen for good avalanche behavior when used in
117
+ // multiply-xorshift sequences.
118
+ static const uint64_t secret = 0x517cc1b727220a95ULL;
119
+ uint64_t hash = (uint64_t) length;
120
+
121
+ if (length <= 8) {
122
+ // Short strings: read first and last 4 bytes (overlapping for len < 8).
123
+ // This covers the majority of Ruby identifiers with a single multiply.
124
+ if (length >= 4) {
125
+ uint32_t a, b;
126
+ memcpy(&a, start, 4);
127
+ memcpy(&b, start + length - 4, 4);
128
+ hash ^= (uint64_t) a | ((uint64_t) b << 32);
129
+ } else if (length > 0) {
130
+ hash ^= (uint64_t) start[0] | ((uint64_t) start[length >> 1] << 8) | ((uint64_t) start[length - 1] << 16);
131
+ }
132
+ hash *= secret;
133
+ } else if (length <= 16) {
134
+ // Medium strings: read first and last 8 bytes (overlapping).
135
+ // Two multiplies instead of the three the loop-based approach needs.
136
+ uint64_t word;
137
+ memcpy(&word, start, 8);
138
+ hash ^= word;
139
+ hash *= secret;
140
+ memcpy(&word, start + length - 8, 8);
141
+ hash ^= word;
142
+ hash *= secret;
143
+ } else {
144
+ const uint8_t *ptr = start;
145
+ size_t remaining = length;
146
+
147
+ while (remaining >= 8) {
148
+ uint64_t word;
149
+ memcpy(&word, ptr, 8);
150
+ hash ^= word;
151
+ hash *= secret;
152
+ ptr += 8;
153
+ remaining -= 8;
154
+ }
155
+
156
+ if (remaining > 0) {
157
+ // Read the last 8 bytes (overlapping with already-processed data).
158
+ uint64_t word;
159
+ memcpy(&word, start + length - 8, 8);
160
+ hash ^= word;
161
+ hash *= secret;
162
+ }
163
+ }
164
+
165
+ hash ^= hash >> 32;
166
+ return (uint32_t) hash;
167
+ }
168
+
169
+ /**
170
+ * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
171
+ */
172
+ static uint32_t
173
+ next_power_of_two(uint32_t v) {
174
+ // Avoid underflow in subtraction on next line.
175
+ if (v == 0) {
176
+ // 1 is the nearest power of 2 to 0 (2^0)
177
+ return 1;
178
+ }
179
+ v--;
180
+ v |= v >> 1;
181
+ v |= v >> 2;
182
+ v |= v >> 4;
183
+ v |= v >> 8;
184
+ v |= v >> 16;
185
+ v++;
186
+ return v;
187
+ }
188
+
189
+ /**
190
+ * Return the number of constants that a pool with the given capacity has room
191
+ * for. The pool grows before an insert once it is three quarters full, so at
192
+ * most three quarters of its capacity, rounded up, ever holds a constant.
193
+ */
194
+ static PRISM_INLINE size_t
195
+ pm_constant_pool_constants_capacity(uint32_t capacity) {
196
+ return (size_t) (capacity - capacity / 4);
197
+ }
198
+
199
+ /**
200
+ * Return whether the given bucket holds the given string. The stored hash is
201
+ * compared first, so that probe collisions are rejected without reading the
202
+ * constant or calling memcmp.
203
+ */
204
+ static PRISM_INLINE bool
205
+ pm_constant_pool_bucket_match(const pm_constant_pool_t *pool, const pm_constant_pool_bucket_t *bucket, uint32_t hash, const uint8_t *start, size_t length) {
206
+ if (bucket->hash != hash) return false;
207
+
208
+ const pm_constant_t *constant = &pool->constants[bucket->id - 1];
209
+ return constant->length == length && memcmp(constant->start, start, length) == 0;
210
+ }
211
+
212
+ /**
213
+ * Resize a constant pool to a given capacity.
214
+ */
215
+ static PRISM_INLINE void
216
+ pm_constant_pool_resize(pm_arena_t *arena, pm_constant_pool_t *pool) {
217
+ uint32_t next_capacity = pool->capacity * 2;
218
+ const uint32_t mask = next_capacity - 1;
219
+
220
+ /* The constants are indexed by id, which a resize does not change, so
221
+ * they only need more room. They grow before the buckets are allocated,
222
+ * which lets them grow in place when they are the most recent allocation
223
+ * in the arena. */
224
+ size_t constants_size = pm_constant_pool_constants_capacity(pool->capacity) * sizeof(pm_constant_t);
225
+ size_t next_constants_size = pm_constant_pool_constants_capacity(next_capacity) * sizeof(pm_constant_t);
226
+ pool->constants = PM_ARENA_REALLOC(arena, pool->constants, next_constants_size, constants_size, pm_constant_t);
227
+
228
+ pm_constant_pool_bucket_t *next_buckets = PM_ARENA_ZALLOC_ARRAY(arena, pm_constant_pool_bucket_t, next_capacity);
229
+
230
+ // For each bucket in the current constant pool, find the index in the
231
+ // next constant pool, and insert it.
232
+ for (uint32_t index = 0; index < pool->capacity; index++) {
233
+ pm_constant_pool_bucket_t *bucket = &pool->buckets[index];
234
+
235
+ // If an id is set on this constant, then we know we have content here.
236
+ // In this case we need to insert it into the next constant pool.
237
+ if (bucket->id != PM_CONSTANT_ID_UNSET) {
238
+ uint32_t next_index = bucket->hash & mask;
239
+
240
+ // This implements linear scanning to find the next available slot
241
+ // in case this index is already taken. We don't need to bother
242
+ // comparing the values since we know that the hash is unique.
243
+ while (next_buckets[next_index].id != PM_CONSTANT_ID_UNSET) {
244
+ next_index = (next_index + 1) & mask;
245
+ }
246
+
247
+ // Here we copy over the entire bucket, which includes the id so
248
+ // that they are consistent between resizes.
249
+ next_buckets[next_index] = *bucket;
250
+ }
251
+ }
252
+
253
+ pool->buckets = next_buckets;
254
+ pool->capacity = next_capacity;
255
+ }
256
+
257
+ /**
258
+ * Initialize a new constant pool with a given capacity.
259
+ */
260
+ void
261
+ pm_constant_pool_init(pm_arena_t *arena, pm_constant_pool_t *pool, uint32_t capacity) {
262
+ capacity = next_power_of_two(capacity);
263
+
264
+ pool->buckets = PM_ARENA_ZALLOC_ARRAY(arena, pm_constant_pool_bucket_t, capacity);
265
+ pool->constants = PM_ARENA_ALLOC_ARRAY(arena, pm_constant_t, pm_constant_pool_constants_capacity(capacity));
266
+ pool->size = 0;
267
+ pool->capacity = capacity;
268
+ }
269
+
270
+ /**
271
+ * Return the number of bytes that pm_constant_pool_init allocates for the given
272
+ * capacity.
273
+ */
274
+ size_t
275
+ pm_constant_pool_init_size(uint32_t capacity) {
276
+ capacity = next_power_of_two(capacity);
277
+ return capacity * sizeof(pm_constant_pool_bucket_t) + pm_constant_pool_constants_capacity(capacity) * sizeof(pm_constant_t);
278
+ }
279
+
280
+ /**
281
+ * Return a pointer to the constant indicated by the given constant id.
282
+ */
283
+ pm_constant_t *
284
+ pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id) {
285
+ PRISM_ASSERT(constant_id != PM_CONSTANT_ID_UNSET && constant_id <= pool->size);
286
+ return &pool->constants[constant_id - 1];
287
+ }
288
+
289
+ /**
290
+ * Find a constant in a constant pool. Returns the id of the constant, or 0 if
291
+ * the constant is not found.
292
+ */
293
+ pm_constant_id_t
294
+ pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
295
+ const uint32_t mask = pool->capacity - 1;
296
+
297
+ uint32_t hash = pm_constant_pool_hash(start, length);
298
+ uint32_t index = hash & mask;
299
+ pm_constant_pool_bucket_t *bucket;
300
+
301
+ while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
302
+ if (pm_constant_pool_bucket_match(pool, bucket, hash, start, length)) {
303
+ return bucket->id;
304
+ }
305
+
306
+ index = (index + 1) & mask;
307
+ }
308
+
309
+ return PM_CONSTANT_ID_UNSET;
310
+ }
311
+
312
+ /**
313
+ * Insert a constant into a constant pool and return its index in the pool.
314
+ */
315
+ static PRISM_INLINE pm_constant_id_t
316
+ pm_constant_pool_insert(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length, pm_constant_pool_bucket_type_t type) {
317
+ if (pool->size >= (pool->capacity / 4 * 3)) {
318
+ pm_constant_pool_resize(arena, pool);
319
+ }
320
+
321
+ const uint32_t mask = pool->capacity - 1;
322
+
323
+ uint32_t hash = pm_constant_pool_hash(start, length);
324
+ uint32_t index = hash & mask;
325
+ pm_constant_pool_bucket_t *bucket;
326
+
327
+ while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
328
+ // If there is a collision, then we need to check if the content is the
329
+ // same as the content we are trying to insert. If it is, then we can
330
+ // return the id of the existing constant.
331
+ if (pm_constant_pool_bucket_match(pool, bucket, hash, start, length)) {
332
+ // Since we have found a match, we need to check if this is
333
+ // attempting to insert a shared or an owned constant. We want to
334
+ // prefer shared constants since they don't require allocations.
335
+ if (type != PM_CONSTANT_POOL_BUCKET_OWNED && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
336
+ // If we're attempting to insert a shared constant and the
337
+ // existing constant is owned, then we can replace it with the
338
+ // shared constant to prefer non-owned references.
339
+ bucket->type = (unsigned int) (type & 0x3);
340
+ pool->constants[bucket->id - 1].start = start;
341
+ }
342
+
343
+ return bucket->id;
344
+ }
345
+
346
+ index = (index + 1) & mask;
347
+ }
348
+
349
+ // IDs are allocated starting at 1, since the value 0 denotes a non-existent
350
+ // constant.
351
+ uint32_t id = ++pool->size;
352
+ PRISM_ASSERT(pool->size < ((uint32_t) (1 << 30)));
353
+
354
+ *bucket = (pm_constant_pool_bucket_t) {
355
+ .id = (unsigned int) (id & 0x3fffffff),
356
+ .type = (unsigned int) (type & 0x3),
357
+ .hash = hash
358
+ };
359
+
360
+ pool->constants[id - 1] = (pm_constant_t) {
361
+ .start = start,
362
+ .length = length,
363
+ };
364
+
365
+ return id;
366
+ }
367
+
368
+ /**
369
+ * Insert a constant into a constant pool. Returns the id of the constant, or
370
+ * PM_CONSTANT_ID_UNSET if any potential calls to resize fail.
371
+ */
372
+ pm_constant_id_t
373
+ pm_constant_pool_insert_shared(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
374
+ return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT);
375
+ }
376
+
377
+ /**
378
+ * Insert a constant into a constant pool from memory that is now owned by the
379
+ * constant pool. Returns the id of the constant, or PM_CONSTANT_ID_UNSET if any
380
+ * potential calls to resize fail.
381
+ */
382
+ pm_constant_id_t
383
+ pm_constant_pool_insert_owned(pm_arena_t *arena, pm_constant_pool_t *pool, uint8_t *start, size_t length) {
384
+ return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED);
385
+ }
386
+
387
+ /**
388
+ * Insert a constant into a constant pool from memory that is constant. Returns
389
+ * the id of the constant, or PM_CONSTANT_ID_UNSET if any potential calls to
390
+ * resize fail.
391
+ */
392
+ pm_constant_id_t
393
+ pm_constant_pool_insert_constant(pm_arena_t *arena, pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
394
+ return pm_constant_pool_insert(arena, pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT);
395
+ }
396
+
397
+ /**
398
+ * Return a raw pointer to the start of a constant.
399
+ */
400
+ const uint8_t *
401
+ pm_constant_start(const pm_constant_t *constant) {
402
+ return constant->start;
403
+ }
404
+
405
+ /**
406
+ * Return the length of a constant.
407
+ */
408
+ size_t pm_constant_length(const pm_constant_t *constant) {
409
+ return constant->length;
410
+ }