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,223 @@
1
+ #ifndef PRISM_INTERNAL_ARENA_H
2
+ #define PRISM_INTERNAL_ARENA_H
3
+
4
+ #include "prism/compiler/align.h"
5
+ #include "prism/compiler/branch.h"
6
+ #include "prism/compiler/flex_array.h"
7
+ #include "prism/compiler/force_inline.h"
8
+ #include "prism/compiler/inline.h"
9
+ #include "prism/compiler/noreturn.h"
10
+
11
+ #include "prism/arena.h"
12
+
13
+ #include <stddef.h>
14
+ #include <stdint.h>
15
+ #include <string.h>
16
+
17
+ /*
18
+ * A single block of memory in the arena. Blocks are linked via prev pointers so
19
+ * they can be freed by walking the chain.
20
+ */
21
+ typedef struct pm_arena_block {
22
+ /* The previous block in the chain (for freeing). */
23
+ struct pm_arena_block *prev;
24
+
25
+ /* The total usable bytes in data[]. */
26
+ size_t capacity;
27
+
28
+ /* The number of bytes consumed so far. */
29
+ size_t used;
30
+
31
+ /* The block's data. */
32
+ char data[PM_FLEX_ARRAY_LENGTH];
33
+ } pm_arena_block_t;
34
+
35
+ /*
36
+ * A bump allocator. Allocations are made by bumping a pointer within the
37
+ * current block. When a block is full, a new block is allocated and linked to
38
+ * the previous one. An allocation too large to share a block gets one of its
39
+ * own on a separate chain, so it does not displace the current block. All
40
+ * blocks are freed at once by walking both chains.
41
+ */
42
+ struct pm_arena_t {
43
+ /* The active block (allocate from here). */
44
+ pm_arena_block_t *current;
45
+
46
+ /* Blocks that each hold a single large allocation, never bumped into. */
47
+ pm_arena_block_t *large;
48
+
49
+ /*
50
+ * The number of blocks allocated on the current chain, which sets the size
51
+ * of the next one.
52
+ */
53
+ size_t block_count;
54
+ };
55
+
56
+ /*
57
+ * Free all blocks in the arena. After this call, all pointers returned by
58
+ * pm_arena_alloc and pm_arena_zalloc are invalid.
59
+ */
60
+ void pm_arena_cleanup(pm_arena_t *arena);
61
+
62
+ /*
63
+ * Ensure the arena has at least `capacity` bytes available in its current
64
+ * block, allocating a new block if necessary. This allows callers to
65
+ * pre-size the arena to avoid repeated small block allocations.
66
+ */
67
+ void pm_arena_reserve(pm_arena_t *arena, size_t capacity);
68
+
69
+ /*
70
+ * Slow path for pm_arena_alloc: allocate a new block, either the next current
71
+ * block or a block of its own for a large allocation, and return a pointer to
72
+ * the first `size` bytes. Do not call directly — use pm_arena_alloc instead.
73
+ */
74
+ void * pm_arena_alloc_slow(pm_arena_t *arena, size_t size);
75
+
76
+ /*
77
+ * Report an allocation whose total size does not fit in a size_t. This
78
+ * goes through PRISM_OOM like any other allocation failure, and lives out of
79
+ * line so this header does not need the headers PRISM_OOM depends on.
80
+ */
81
+ PRISM_NORETURN void pm_arena_alloc_overflow(void);
82
+
83
+ /*
84
+ * Allocate memory from the arena. The returned memory is NOT zeroed. This
85
+ * function is infallible — it aborts on allocation failure.
86
+ *
87
+ * The fast path (bump pointer within the current block) is inlined at each
88
+ * call site. The slow path (new block allocation) is out-of-line.
89
+ */
90
+ static PRISM_FORCE_INLINE void *
91
+ pm_arena_alloc(pm_arena_t *arena, size_t size, size_t alignment) {
92
+ if (arena->current != NULL) {
93
+ size_t used_aligned = (arena->current->used + alignment - 1) & ~(alignment - 1);
94
+ size_t needed = used_aligned + size;
95
+
96
+ if (used_aligned >= arena->current->used && needed >= used_aligned && needed <= arena->current->capacity) {
97
+ arena->current->used = needed;
98
+ return arena->current->data + used_aligned;
99
+ }
100
+ }
101
+
102
+ return pm_arena_alloc_slow(arena, size);
103
+ }
104
+
105
+ /*
106
+ * Allocate memory from the arena whose address is aligned to the given power of
107
+ * two. Unlike pm_arena_alloc, the alignment may exceed the alignment of the
108
+ * arena's blocks, at the cost of a slower path that is not inlined.
109
+ */
110
+ void * pm_arena_alloc_aligned(pm_arena_t *arena, size_t size, size_t alignment);
111
+
112
+ /*
113
+ * Allocate memory for a single element of type T_ from the arena.
114
+ */
115
+ #define PM_ARENA_ALLOC(arena_, T_) \
116
+ ((T_ *) pm_arena_alloc(arena_, sizeof(T_), PRISM_ALIGNOF(T_)))
117
+
118
+ /*
119
+ * Allocate an array of elements from the arena. The returned memory is NOT
120
+ * zeroed. This function is infallible — it aborts on allocation failure.
121
+ */
122
+ static PRISM_INLINE void *
123
+ pm_arena_alloc_array(pm_arena_t *arena, size_t count, size_t size, size_t alignment) {
124
+ if (PRISM_UNLIKELY(count > SIZE_MAX / size)) pm_arena_alloc_overflow();
125
+ return pm_arena_alloc(arena, count * size, alignment);
126
+ }
127
+
128
+ /*
129
+ * Allocate an array of elements of type T_ from the arena.
130
+ */
131
+ #define PM_ARENA_ALLOC_ARRAY(arena_, T_, count_) \
132
+ ((T_ *) pm_arena_alloc_array(arena_, count_, sizeof(T_), PRISM_ALIGNOF(T_)))
133
+
134
+ /*
135
+ * Allocate zero-initialized memory from the arena. This function is infallible
136
+ * — it aborts on allocation failure.
137
+ */
138
+ static PRISM_INLINE void *
139
+ pm_arena_zalloc(pm_arena_t *arena, size_t size, size_t alignment) {
140
+ void *ptr = pm_arena_alloc(arena, size, alignment);
141
+ memset(ptr, 0, size);
142
+ return ptr;
143
+ }
144
+
145
+ /*
146
+ * Allocate a zero-initialized element of type T_ from the arena.
147
+ */
148
+ #define PM_ARENA_ZALLOC(arena_, T_) \
149
+ ((T_ *) pm_arena_zalloc(arena_, sizeof(T_), PRISM_ALIGNOF(T_)))
150
+
151
+ /*
152
+ * Allocate a zero-initialized array of elements from the arena. This function
153
+ * is infallible — it aborts on allocation failure.
154
+ */
155
+ static PRISM_INLINE void *
156
+ pm_arena_zalloc_array(pm_arena_t *arena, size_t count, size_t size, size_t alignment) {
157
+ void *ptr = pm_arena_alloc_array(arena, count, size, alignment);
158
+ memset(ptr, 0, count * size);
159
+ return ptr;
160
+ }
161
+
162
+ /*
163
+ * Allocate a zero-initialized array of elements of type T_ from the arena.
164
+ */
165
+ #define PM_ARENA_ZALLOC_ARRAY(arena_, T_, count_) \
166
+ ((T_ *) pm_arena_zalloc_array(arena_, count_, sizeof(T_), PRISM_ALIGNOF(T_)))
167
+
168
+ /*
169
+ * Allocate memory from the arena and copy the given data into it. This is a
170
+ * convenience wrapper around pm_arena_alloc + memcpy.
171
+ */
172
+ static PRISM_INLINE void *
173
+ pm_arena_memdup(pm_arena_t *arena, const void *src, size_t size, size_t alignment) {
174
+ void *dst = pm_arena_alloc(arena, size, alignment);
175
+ memcpy(dst, src, size);
176
+ return dst;
177
+ }
178
+
179
+ /*
180
+ * Resize an allocation from the arena from old_size to new_size bytes and
181
+ * return where it now lives. The sizes are in the same order as xrealloc_sized.
182
+ * An old_size of 0 means there is no allocation yet, so ptr may be NULL and a
183
+ * new allocation is returned.
184
+ *
185
+ * The most recent allocation in the current block ends exactly where the
186
+ * block's used bytes do, so it is resized in place when the block has room. Any
187
+ * other allocation stays where it is when it shrinks, and is copied into a new
188
+ * allocation when it grows, leaving the old one behind in the arena.
189
+ */
190
+ static PRISM_INLINE void *
191
+ pm_arena_realloc(pm_arena_t *arena, void *ptr, size_t new_size, size_t old_size, size_t alignment) {
192
+ void *result;
193
+
194
+ if (old_size == 0) {
195
+ result = pm_arena_alloc(arena, new_size, alignment);
196
+ } else {
197
+ pm_arena_block_t *block = arena->current;
198
+
199
+ if (block != NULL && (char *) ptr + old_size == block->data + block->used) {
200
+ size_t start = block->used - old_size;
201
+
202
+ if (new_size <= block->capacity - start) {
203
+ block->used = start + new_size;
204
+ return ptr;
205
+ }
206
+ }
207
+
208
+ if (new_size <= old_size) return ptr;
209
+
210
+ result = pm_arena_alloc(arena, new_size, alignment);
211
+ memcpy(result, ptr, old_size);
212
+ }
213
+
214
+ return result;
215
+ }
216
+
217
+ /*
218
+ * Reallocate an element of type T_ from the arena.
219
+ */
220
+ #define PM_ARENA_REALLOC(arena_, ptr_, new_size_, old_size_, T_) \
221
+ ((T_ *) pm_arena_realloc(arena_, ptr_, new_size_, old_size_, PRISM_ALIGNOF(T_)))
222
+
223
+ #endif
@@ -0,0 +1,14 @@
1
+ #ifndef PRISM_INTERNAL_ASSERT_H
2
+ #define PRISM_INTERNAL_ASSERT_H
3
+
4
+ /**
5
+ * Assertion macro for prism. By default, it uses the standard C assert. A
6
+ * consumer can define PRISM_ASSERT before building prism to handle assertions
7
+ * differently.
8
+ */
9
+ #ifndef PRISM_ASSERT
10
+ #include <assert.h>
11
+ #define PRISM_ASSERT(expr_) assert(expr_)
12
+ #endif
13
+
14
+ #endif
@@ -0,0 +1,239 @@
1
+ /*
2
+ * Arithmetic on arbitrary-precision unsigned integers stored as arrays of
3
+ * 32-bit words.
4
+ */
5
+ #ifndef PRISM_INTERNAL_BIGNUM_H
6
+ #define PRISM_INTERNAL_BIGNUM_H
7
+
8
+ #include "prism/compiler/inline.h"
9
+
10
+ #include "prism/internal/assert.h"
11
+ #include "prism/internal/bit.h"
12
+
13
+ #include <stdbool.h>
14
+ #include <stddef.h>
15
+ #include <stdint.h>
16
+ #include <string.h>
17
+
18
+ /*
19
+ * The base of the words of a number.
20
+ */
21
+ #define PM_BIGNUM_BINARY_BASE ((uint64_t) 1 << 32)
22
+
23
+ /*
24
+ * The base of the words of a decimal number, each of which is nine digits.
25
+ */
26
+ #define PM_BIGNUM_DECIMAL_BASE 1000000000
27
+
28
+ /*
29
+ * An arbitrary-precision unsigned integer.
30
+ */
31
+ typedef struct {
32
+ /* The number of words in use. */
33
+ uint32_t length;
34
+
35
+ /* The words, least significant first. */
36
+ uint32_t *words;
37
+ } pm_bignum_t;
38
+
39
+ /*
40
+ * Multiply the number by multiplier and add addend, in place. A word times a
41
+ * word plus a word is at most 2**64 - 2**32, so each intermediate product fits
42
+ * in a uint64_t. A nonzero carry out of the top word becomes a new top word, so
43
+ * the storage must have room for it.
44
+ */
45
+ static PRISM_INLINE void
46
+ pm_bignum_mul_add(pm_bignum_t *bignum, uint32_t multiplier, uint32_t addend) {
47
+ PRISM_ASSERT(multiplier != 0);
48
+ uint64_t carry = addend;
49
+
50
+ for (uint32_t index = 0; index < bignum->length; index++) {
51
+ uint64_t product = (uint64_t) bignum->words[index] * multiplier + carry;
52
+ bignum->words[index] = (uint32_t) product;
53
+ carry = product >> 32;
54
+ }
55
+
56
+ if (carry != 0) bignum->words[bignum->length++] = (uint32_t) carry;
57
+ }
58
+
59
+ /*
60
+ * Divide the number by divisor, in place, working down from the top word.
61
+ * Returns the remainder. The remainder is always below the divisor, so shifting
62
+ * it above the next word fits in a uint64_t, and since the divisor is a single
63
+ * word the quotient is at most one word shorter. When inlined with a constant
64
+ * divisor, the compiler replaces the division with a multiplication.
65
+ */
66
+ static PRISM_INLINE uint32_t
67
+ pm_bignum_div(pm_bignum_t *bignum, uint32_t divisor) {
68
+ PRISM_ASSERT(divisor != 0);
69
+ uint64_t remainder = 0;
70
+
71
+ for (uint32_t index = bignum->length; index > 0; index--) {
72
+ uint64_t current = (remainder << 32) | bignum->words[index - 1];
73
+ bignum->words[index - 1] = (uint32_t) (current / divisor);
74
+ remainder = current % divisor;
75
+ }
76
+
77
+ if (bignum->length > 0 && bignum->words[bignum->length - 1] == 0) bignum->length--;
78
+ return (uint32_t) remainder;
79
+ }
80
+
81
+ /*
82
+ * Divide the number by divisor if it divides evenly, and return whether it did.
83
+ * Otherwise the number is left unchanged: multiplying the quotient back and
84
+ * adding the remainder restores it exactly.
85
+ */
86
+ static PRISM_INLINE bool
87
+ pm_bignum_div_exact(pm_bignum_t *bignum, uint32_t divisor) {
88
+ uint32_t remainder = pm_bignum_div(bignum, divisor);
89
+ if (remainder == 0) return true;
90
+
91
+ pm_bignum_mul_add(bignum, divisor, remainder);
92
+ return false;
93
+ }
94
+
95
+ /*
96
+ * Shift the number left by count bits, in place. The words grow by count / 32
97
+ * plus possibly one more, so the storage must have room for them.
98
+ */
99
+ static PRISM_INLINE void
100
+ pm_bignum_shift_left(pm_bignum_t *bignum, size_t count) {
101
+ if (bignum->length == 0) return;
102
+
103
+ uint32_t *words = bignum->words;
104
+ uint32_t length = bignum->length;
105
+ uint32_t word_shift = (uint32_t) (count / 32);
106
+ uint32_t bit_shift = (uint32_t) (count % 32);
107
+
108
+ /* Work down from the top word, so each word is read before the word that
109
+ * replaces it is written. */
110
+ uint32_t carry = bit_shift == 0 ? 0 : words[length - 1] >> (32 - bit_shift);
111
+ for (uint32_t index = length; index > 0; index--) {
112
+ uint32_t below = (bit_shift == 0 || index == 1) ? 0 : words[index - 2] >> (32 - bit_shift);
113
+ words[index - 1 + word_shift] = (words[index - 1] << bit_shift) | below;
114
+ }
115
+
116
+ memset(words, 0, sizeof(uint32_t) * word_shift);
117
+ bignum->length = length + word_shift;
118
+ if (carry != 0) words[bignum->length++] = carry;
119
+ }
120
+
121
+ /*
122
+ * Shift the number right by count bits, in place, dropping the bits that are
123
+ * shifted out.
124
+ */
125
+ static PRISM_INLINE void
126
+ pm_bignum_shift_right(pm_bignum_t *bignum, size_t count) {
127
+ uint32_t *words = bignum->words;
128
+ uint32_t word_shift = (uint32_t) (count / 32);
129
+ uint32_t bit_shift = (uint32_t) (count % 32);
130
+
131
+ if (word_shift >= bignum->length) {
132
+ bignum->length = 0;
133
+ return;
134
+ }
135
+
136
+ /* Work up from the bottom word, so each word is read before the word that
137
+ * replaces it is written. */
138
+ uint32_t length = bignum->length - word_shift;
139
+ for (uint32_t index = 0; index < length; index++) {
140
+ uint32_t above = (bit_shift == 0 || index + 1 == length) ? 0 : words[index + word_shift + 1] << (32 - bit_shift);
141
+ words[index] = (words[index + word_shift] >> bit_shift) | above;
142
+ }
143
+
144
+ bignum->length = length;
145
+ if (words[length - 1] == 0) bignum->length--;
146
+ }
147
+
148
+ /*
149
+ * Return the number of trailing zero bits in a nonzero number. Its top word is
150
+ * nonzero, so the scan for the first nonzero word stops within its length.
151
+ */
152
+ static PRISM_INLINE uint64_t
153
+ pm_bignum_trailing_zeros(const pm_bignum_t *bignum) {
154
+ PRISM_ASSERT(bignum->length > 0 && bignum->words[bignum->length - 1] != 0);
155
+
156
+ uint32_t index = 0;
157
+ while (bignum->words[index] == 0) index++;
158
+ return (uint64_t) index * 32 + pm_ctzll(bignum->words[index]);
159
+ }
160
+
161
+ /*
162
+ * Compare two numbers. Returns -1 if left is less than right, 0 if they are
163
+ * equal, and 1 if left is greater than right.
164
+ */
165
+ static PRISM_INLINE int
166
+ pm_bignum_compare(const pm_bignum_t *left, const pm_bignum_t *right) {
167
+ if (left->length != right->length) return left->length < right->length ? -1 : 1;
168
+
169
+ for (uint32_t index = left->length; index > 0; index--) {
170
+ uint32_t left_word = left->words[index - 1];
171
+ uint32_t right_word = right->words[index - 1];
172
+ if (left_word != right_word) return left_word < right_word ? -1 : 1;
173
+ }
174
+
175
+ return 0;
176
+ }
177
+
178
+ /*
179
+ * Return the value of a number of at most two words. Words past the length may
180
+ * hold stale values after an in-place operation, so only the words in use are
181
+ * read.
182
+ */
183
+ static PRISM_INLINE uint64_t
184
+ pm_bignum_uint64(const pm_bignum_t *bignum) {
185
+ PRISM_ASSERT(bignum->length <= 2);
186
+ return (bignum->length > 0 ? (uint64_t) bignum->words[0] : 0) | (bignum->length > 1 ? (uint64_t) bignum->words[1] << 32 : 0);
187
+ }
188
+
189
+ /*
190
+ * Temporary words for the operations that need them, taken and given back in
191
+ * stack order: an operation saves the cursor, takes what it needs, and
192
+ * restores the cursor before it returns.
193
+ */
194
+ typedef struct {
195
+ /* The next free word. */
196
+ uint32_t *cursor;
197
+
198
+ /* One past the last word. */
199
+ uint32_t *end;
200
+ } pm_bignum_scratch_t;
201
+
202
+ /*
203
+ * Take count words from the scratch space.
204
+ */
205
+ static PRISM_INLINE uint32_t *
206
+ pm_bignum_scratch_take(pm_bignum_scratch_t *scratch, uint32_t count) {
207
+ PRISM_ASSERT((size_t) (scratch->end - scratch->cursor) >= count);
208
+ uint32_t *words = scratch->cursor;
209
+ scratch->cursor += count;
210
+ return words;
211
+ }
212
+
213
+ /*
214
+ * Add right into left, in place. The storage must have room for the sum, which
215
+ * is at most one word longer than the longer of the two.
216
+ */
217
+ void pm_bignum_add(pm_bignum_t *left, const pm_bignum_t *right);
218
+
219
+ /*
220
+ * Multiply left by right into result, whose storage has room for
221
+ * left->length + right->length words and overlaps neither. Long numbers use
222
+ * Karatsuba's method, which takes its temporaries from scratch.
223
+ */
224
+ void pm_bignum_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch);
225
+
226
+ /*
227
+ * Add right into left, in place, in base 10**9. The storage must have room for
228
+ * the sum, which is at most one word longer than the longer of the two.
229
+ */
230
+ void pm_bignum_decimal_add(pm_bignum_t *left, const pm_bignum_t *right);
231
+
232
+ /*
233
+ * Multiply left by right into result, in base 10**9, whose storage has room
234
+ * for left->length + right->length words and overlaps neither. Long numbers
235
+ * use Karatsuba's method, which takes its temporaries from scratch.
236
+ */
237
+ void pm_bignum_decimal_multiply(pm_bignum_t *result, const pm_bignum_t *left, const pm_bignum_t *right, pm_bignum_scratch_t *scratch);
238
+
239
+ #endif
@@ -0,0 +1,66 @@
1
+ #ifndef PRISM_INTERNAL_BIT_H
2
+ #define PRISM_INTERNAL_BIT_H
3
+
4
+ #include "prism/compiler/inline.h"
5
+
6
+ /*
7
+ * Count trailing zero bits in a 64-bit value, and find the index of the
8
+ * highest set bit in one (the result of the x86 bsr instruction). pm_ctzll is
9
+ * used by SWAR identifier scanning to find the first non-matching byte in a
10
+ * word, and pm_bsrll by integer parsing and printing to find the largest power
11
+ * of two below a count.
12
+ *
13
+ * Precondition: v must be nonzero. The results are undefined when v == 0
14
+ * (matching the behavior of __builtin_ctzll, __builtin_clzll,
15
+ * _BitScanForward64, and _BitScanReverse64).
16
+ */
17
+ #if defined(__GNUC__) || defined(__clang__)
18
+ #define pm_bsrll(v) (63 - (unsigned) __builtin_clzll(v))
19
+ #define pm_ctzll(v) ((unsigned) __builtin_ctzll(v))
20
+ #elif defined(_MSC_VER) && defined(_WIN64)
21
+ #include <intrin.h>
22
+ #include <stdint.h>
23
+
24
+ static PRISM_INLINE unsigned
25
+ pm_bsrll(uint64_t v) {
26
+ unsigned long index;
27
+ _BitScanReverse64(&index, v);
28
+ return (unsigned) index;
29
+ }
30
+
31
+ static PRISM_INLINE unsigned
32
+ pm_ctzll(uint64_t v) {
33
+ unsigned long index;
34
+ _BitScanForward64(&index, v);
35
+ return (unsigned) index;
36
+ }
37
+ #else
38
+ #include <stdint.h>
39
+
40
+ static PRISM_INLINE unsigned
41
+ pm_bsrll(uint64_t v) {
42
+ unsigned c = 0;
43
+ if (v & 0xFFFFFFFF00000000ULL) { c += 32; v >>= 32; }
44
+ if (v & 0x00000000FFFF0000ULL) { c += 16; v >>= 16; }
45
+ if (v & 0x000000000000FF00ULL) { c += 8; v >>= 8; }
46
+ if (v & 0x00000000000000F0ULL) { c += 4; v >>= 4; }
47
+ if (v & 0x000000000000000CULL) { c += 2; v >>= 2; }
48
+ if (v & 0x0000000000000002ULL) { c += 1; }
49
+ return c;
50
+ }
51
+
52
+ static PRISM_INLINE unsigned
53
+ pm_ctzll(uint64_t v) {
54
+ unsigned c = 0;
55
+ v &= (uint64_t) (-(int64_t) v);
56
+ if (!(v & 0x00000000FFFFFFFFULL)) c += 32;
57
+ if (!(v & 0x0000FFFF0000FFFFULL)) c += 16;
58
+ if (!(v & 0x00FF00FF00FF00FFULL)) c += 8;
59
+ if (!(v & 0x0F0F0F0F0F0F0F0FULL)) c += 4;
60
+ if (!(v & 0x3333333333333333ULL)) c += 2;
61
+ if (!(v & 0x5555555555555555ULL)) c += 1;
62
+ return c;
63
+ }
64
+ #endif
65
+
66
+ #endif
@@ -0,0 +1,104 @@
1
+ #ifndef PRISM_INTERNAL_BUFFER_H
2
+ #define PRISM_INTERNAL_BUFFER_H
3
+
4
+ #include "prism/compiler/format.h"
5
+
6
+ #include "prism/buffer.h"
7
+
8
+ #include <stdbool.h>
9
+ #include <stdint.h>
10
+
11
+ /*
12
+ * A simple memory buffer that stores data in a contiguous block of memory.
13
+ */
14
+ struct pm_buffer_t {
15
+ /* The length of the buffer in bytes. */
16
+ size_t length;
17
+
18
+ /* The capacity of the buffer in bytes that has been allocated. */
19
+ size_t capacity;
20
+
21
+ /* A pointer to the start of the buffer. */
22
+ char *value;
23
+ };
24
+
25
+ /* Initialize a pm_buffer_t with the given capacity. */
26
+ void pm_buffer_init(pm_buffer_t *buffer, size_t capacity);
27
+
28
+ /* Free the memory held by the buffer. */
29
+ void pm_buffer_cleanup(pm_buffer_t *buffer);
30
+
31
+ /* Append the given amount of space as zeroes to the buffer. */
32
+ void pm_buffer_append_zeroes(pm_buffer_t *buffer, size_t length);
33
+
34
+ /* Append the given byte to the buffer the given number of times. */
35
+ void pm_buffer_append_repeated(pm_buffer_t *buffer, uint8_t value, size_t length);
36
+
37
+ /* Append a formatted string to the buffer. */
38
+ void pm_buffer_append_format(pm_buffer_t *buffer, const char *format, ...) PRISM_ATTRIBUTE_FORMAT(2, 3);
39
+
40
+ /* Append a string to the buffer. */
41
+ void pm_buffer_append_string(pm_buffer_t *buffer, const char *value, size_t length);
42
+
43
+ /*
44
+ * Append a string literal to the buffer, with its length computed at compile
45
+ * time. Concatenating with "" means anything other than a string literal fails
46
+ * to compile, instead of having its pointer size taken as its length.
47
+ */
48
+ #define PM_BUFFER_APPEND_LITERAL(buffer, literal) pm_buffer_append_string((buffer), "" literal, sizeof(literal) - 1)
49
+
50
+ /* Append a list of bytes to the buffer. */
51
+ void pm_buffer_append_bytes(pm_buffer_t *buffer, const uint8_t *value, size_t length);
52
+
53
+ /* Append a single byte to the buffer. */
54
+ void pm_buffer_append_byte(pm_buffer_t *buffer, uint8_t value);
55
+
56
+ /* Append a 32-bit unsigned integer to the buffer as a variable-length integer. */
57
+ void pm_buffer_append_varuint(pm_buffer_t *buffer, uint32_t value);
58
+
59
+ /* Append a 32-bit signed integer to the buffer as a variable-length integer. */
60
+ void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
61
+
62
+ /* Append a double to the buffer. */
63
+ void pm_buffer_append_double(pm_buffer_t *buffer, double value);
64
+
65
+ /* Append a 64-bit unsigned integer to the buffer in native endian order. */
66
+ void pm_buffer_append_uint64(pm_buffer_t *buffer, uint64_t value);
67
+
68
+ /* Append a unicode codepoint to the buffer. */
69
+ bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
70
+
71
+ /*
72
+ * The different types of escaping that can be performed by the buffer when
73
+ * appending a slice of Ruby source code.
74
+ */
75
+ typedef enum {
76
+ PM_BUFFER_ESCAPING_RUBY,
77
+ PM_BUFFER_ESCAPING_JSON
78
+ } pm_buffer_escaping_t;
79
+
80
+ /* Append a slice of source code to the buffer. */
81
+ void pm_buffer_append_source(pm_buffer_t *buffer, const uint8_t *source, size_t length, pm_buffer_escaping_t escaping);
82
+
83
+ /* Prepend the given string to the buffer. */
84
+ void pm_buffer_prepend_string(pm_buffer_t *buffer, const char *value, size_t length);
85
+
86
+ /* Concatenate one buffer onto another. */
87
+ void pm_buffer_concat(pm_buffer_t *destination, const pm_buffer_t *source);
88
+
89
+ /*
90
+ * Clear the buffer by reducing its size to 0. This does not free the allocated
91
+ * memory, but it does allow the buffer to be reused.
92
+ */
93
+ void pm_buffer_clear(pm_buffer_t *buffer);
94
+
95
+ /* Strip the whitespace from the end of the buffer. */
96
+ void pm_buffer_rstrip(pm_buffer_t *buffer);
97
+
98
+ /* Checks if the buffer includes the given value. */
99
+ size_t pm_buffer_index(const pm_buffer_t *buffer, char value);
100
+
101
+ /* Insert the given string into the buffer at the given index. */
102
+ void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size_t length);
103
+
104
+ #endif