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,675 @@
1
+ #include "prism/internal/integer.h"
2
+
3
+ #include "prism/compiler/align.h"
4
+ #include "prism/compiler/inline.h"
5
+
6
+ #include "prism/internal/allocator.h"
7
+ #include "prism/internal/arena.h"
8
+ #include "prism/internal/assert.h"
9
+ #include "prism/internal/bignum.h"
10
+ #include "prism/internal/bit.h"
11
+ #include "prism/internal/buffer.h"
12
+ #include "prism/internal/oom.h"
13
+
14
+ /* Include sys/types.h before inttypes.h to work around an issue with certain
15
+ * versions of GCC and newlib which causes omission of PRIu64 and the other
16
+ * 64-bit macros. */
17
+ #include <sys/types.h>
18
+ #include <inttypes.h>
19
+
20
+ #include <stdbool.h>
21
+ #include <stddef.h>
22
+ #include <stdlib.h>
23
+ #include <string.h>
24
+
25
+ /**
26
+ * When parsing long decimal digits, we switch to a divide-and-conquer approach
27
+ * once it gets really long because we start to see the impact of an O(n^1.58)
28
+ * vs an O(n^2) algorithm.
29
+ */
30
+ #define PM_INTEGER_DIVIDE_THRESHOLD 224
31
+
32
+ /**
33
+ * When printing integers of many words, we switch to a divide-and-conquer
34
+ * approach for the same reason as parsing: the repeated division by 10**9 is
35
+ * O(n^2), while the divide and conquer is O(n^1.58). Below this many words, the
36
+ * scratch space and the powers of two that the divide and conquer needs cost
37
+ * more than it saves.
38
+ */
39
+ #define PM_INTEGER_STRING_THRESHOLD 48
40
+
41
+ /**
42
+ * Within the divide and conquer, pieces of at most this many words are
43
+ * converted by repeated division by 10**9. This is lower than
44
+ * PM_INTEGER_STRING_THRESHOLD because the pieces share the scratch space and
45
+ * the powers of two, so they do not pay for them again.
46
+ */
47
+ #define PM_INTEGER_STRING_LEAF 8
48
+
49
+ /**
50
+ * The values of a hexadecimal digit, where the index is the ASCII character.
51
+ */
52
+ static const int8_t pm_integer_parse_digit_values[256] = {
53
+ /* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
54
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x */
55
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 1x */
56
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 2x */
57
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 3x */
58
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 4x */
59
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 5x */
60
+ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 6x */
61
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 7x */
62
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 8x */
63
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 9x */
64
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Ax */
65
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Bx */
66
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Cx */
67
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Dx */
68
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Ex */
69
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* Fx */
70
+ };
71
+
72
+ /**
73
+ * Return the value of a hexadecimal digit in a uint8_t.
74
+ */
75
+ static uint8_t
76
+ pm_integer_parse_digit(const uint8_t character) {
77
+ int8_t value = pm_integer_parse_digit_values[character];
78
+ PRISM_ASSERT(value != -1 && "invalid digit");
79
+
80
+ return (uint8_t) value;
81
+ }
82
+
83
+ /**
84
+ * Whether a character between the digits of a number is skipped. Underscores
85
+ * separate digits, and the decimal point of a float rational like 1.5r does not
86
+ * change its numerator, which is all of its digits.
87
+ */
88
+ static PRISM_INLINE bool
89
+ pm_integer_separator(uint8_t character) {
90
+ return character == '_' || character == '.';
91
+ }
92
+
93
+ /**
94
+ * Store a bignum whose words are allocated in the arena, with room for capacity
95
+ * words, into an integer. An integer with at most two words stores them inline,
96
+ * so all of the arena words are given back; otherwise only the unused ones are.
97
+ */
98
+ static void
99
+ pm_integer_from_bignum(pm_arena_t *arena, pm_integer_t *integer, const pm_bignum_t *bignum, uint32_t capacity) {
100
+ if (bignum->length <= 2) {
101
+ uint64_t value = pm_bignum_uint64(bignum);
102
+ PM_ARENA_REALLOC(arena, bignum->words, 0, sizeof(uint32_t) * capacity, uint32_t);
103
+ *integer = pm_integer_uint64(value);
104
+ } else {
105
+ uint32_t *words = PM_ARENA_REALLOC(arena, bignum->words, sizeof(uint32_t) * bignum->length, sizeof(uint32_t) * capacity, uint32_t);
106
+ *integer = (pm_integer_t) { .length = bignum->length, .words = { .values = words } };
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Parse the digits of an integer in a power-of-two base that does not fit in
112
+ * two words. Every digit but the leading one contributes exactly the same number
113
+ * of bits, so the exact number of words is known before parsing, and the digits
114
+ * are packed directly into words allocated from the arena.
115
+ */
116
+ static void
117
+ pm_integer_parse_powof2(pm_arena_t *arena, pm_integer_t *integer, uint32_t base, uint32_t digits, const uint8_t *start, const uint8_t *end) {
118
+ static const uint8_t digit_bits[16] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 };
119
+ uint32_t leading_bits = digit_bits[pm_integer_parse_digit(*start)];
120
+
121
+ uint32_t bit = (uint32_t) pm_ctzll(base);
122
+ PRISM_ASSERT(bit >= 1 && bit <= 4);
123
+
124
+ uint32_t length = (uint32_t) (((uint64_t) (digits - 1) * bit + leading_bits + 31) / 32);
125
+ PRISM_ASSERT(length > 2);
126
+
127
+ uint32_t *words = PM_ARENA_ZALLOC_ARRAY(arena, uint32_t, length);
128
+ uint64_t bit_position = 0;
129
+
130
+ for (const uint8_t *cursor = end; cursor > start;) {
131
+ if (pm_integer_separator(*--cursor)) continue;
132
+ uint32_t value = pm_integer_parse_digit(*cursor);
133
+
134
+ uint32_t index = (uint32_t) (bit_position / 32);
135
+ uint32_t shift = (uint32_t) (bit_position % 32);
136
+
137
+ words[index] |= value << shift;
138
+ if (32 - shift < bit && index + 1 < length) words[index + 1] |= value >> (32 - shift);
139
+
140
+ bit_position += bit;
141
+ }
142
+
143
+ *integer = (pm_integer_t) { .length = length, .words = { .values = words } };
144
+ }
145
+
146
+ /**
147
+ * The number of words needed to hold a decimal integer with the given number
148
+ * of digits. A number with d digits is below 10**d, so it needs at most:
149
+ *
150
+ * d * log2(10) + 1
151
+ *
152
+ * bits, which rounds up to at most one word more than the whole words below
153
+ * that count. To find log2(10), we approximate it as 3402/1024, which is
154
+ * slightly greater than the actual value so it does not undercount.
155
+ */
156
+ static PRISM_INLINE uint32_t
157
+ pm_integer_decimal_words(uint64_t digits) {
158
+ return (uint32_t) ((digits * 3402 / 1024) / 32 + 1);
159
+ }
160
+
161
+ /**
162
+ * The number of words of storage needed to convert nchunks chunks of nine
163
+ * decimal digits, which is one word more than the value needs. The conversion
164
+ * multiplies the high half by a power of ten into this storage, and
165
+ * pm_bignum_multiply writes the sum of its factors' lengths before it trims a
166
+ * zero top word. Each factor's length rounds its bits up to a whole word, so
167
+ * that sum can be one word more than the product needs.
168
+ */
169
+ static PRISM_INLINE uint32_t
170
+ pm_integer_chunks_words(uint32_t nchunks) {
171
+ return pm_integer_decimal_words((uint64_t) nchunks * 9) + 1;
172
+ }
173
+
174
+ /**
175
+ * Convert nchunks of nine decimal digits into a bignum. Do this by effectively
176
+ * converting the base of the number from 10**9 to 2**32. The result's storage
177
+ * must have room for pm_integer_chunks_words(nchunks) words.
178
+ */
179
+ static void
180
+ pm_integer_parse_decimal_divide_chunks(pm_bignum_t *result, uint32_t nchunks, const uint32_t *chunks, const pm_bignum_t *powers, pm_bignum_scratch_t *scratch) {
181
+ if (nchunks <= PM_INTEGER_DIVIDE_THRESHOLD) {
182
+ result->length = 0;
183
+ for (uint32_t index = nchunks; index > 0; index--) pm_bignum_mul_add(result, PM_BIGNUM_DECIMAL_BASE, chunks[index - 1]);
184
+ } else {
185
+ uint32_t level = pm_bsrll((uint64_t) nchunks - 1);
186
+ uint32_t split = (uint32_t) 1 << level;
187
+
188
+ uint32_t *saved = scratch->cursor;
189
+ pm_bignum_t high = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_chunks_words(nchunks - split)) };
190
+ pm_integer_parse_decimal_divide_chunks(&high, nchunks - split, chunks + split, powers, scratch);
191
+
192
+ PRISM_ASSERT(high.length + powers[level].length <= pm_integer_chunks_words(nchunks));
193
+ pm_bignum_multiply(result, &high, &powers[level], scratch);
194
+ scratch->cursor = saved;
195
+
196
+ pm_bignum_t low = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_chunks_words(split)) };
197
+ pm_integer_parse_decimal_divide_chunks(&low, split, chunks, powers, scratch);
198
+ pm_bignum_add(result, &low);
199
+ scratch->cursor = saved;
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Parse the digits of a decimal integer of more than
205
+ * PM_INTEGER_DIVIDE_THRESHOLD chunks of nine digits by divide and conquer. The
206
+ * digits are packed into chunks, the powers of ten that the splits need are
207
+ * computed by repeated squaring, and the result is converted straight into
208
+ * words allocated from the arena. Everything else lives in one block of
209
+ * scratch space from the heap, sized for the worst case: the chunks, the
210
+ * powers, and the halves and products of the deepest chain of splits, with
211
+ * Karatsuba's temporaries bounded by 12 times the longer factor's length.
212
+ */
213
+ static void
214
+ pm_integer_parse_decimal_divide(pm_arena_t *arena, pm_integer_t *integer, uint32_t digits, const uint8_t *start, const uint8_t *end) {
215
+ uint32_t nchunks = (uint32_t) (((uint64_t) digits + 8) / 9);
216
+ uint32_t capacity = pm_integer_chunks_words(nchunks);
217
+
218
+ pm_bignum_t result = {
219
+ .length = 0,
220
+ .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
221
+ };
222
+
223
+ /* The scratch space is about 12 times the number of chunks, which can
224
+ * exceed 32 bits, and on a 32-bit platform its size in bytes can exceed
225
+ * size_t. */
226
+ uint64_t scratch_size = (uint64_t) nchunks + 12 * (uint64_t) capacity + 1024;
227
+ if (scratch_size > SIZE_MAX / sizeof(uint32_t)) PRISM_OOM();
228
+
229
+ uint32_t *allocation = (uint32_t *) xmalloc(sizeof(uint32_t) * (size_t) scratch_size);
230
+ if (allocation == NULL) PRISM_OOM();
231
+ pm_bignum_scratch_t scratch = { .cursor = allocation, .end = allocation + (size_t) scratch_size };
232
+
233
+ /* Pack the digits into chunks of nine, least significant first. The most
234
+ * significant chunk takes the leftover digits so the rest are exactly
235
+ * nine. */
236
+ uint32_t *chunks = pm_bignum_scratch_take(&scratch, nchunks);
237
+ uint32_t chunk_size = digits % 9 == 0 ? 9 : digits % 9;
238
+ uint32_t chunk_digits = 0;
239
+ uint32_t chunk_index = nchunks;
240
+ uint32_t chunk = 0;
241
+
242
+ for (const uint8_t *cursor = start; cursor < end; cursor++) {
243
+ if (pm_integer_separator(*cursor)) continue;
244
+ chunk = chunk * 10 + pm_integer_parse_digit(*cursor);
245
+
246
+ if (++chunk_digits == chunk_size) {
247
+ chunks[--chunk_index] = chunk;
248
+ chunk_size = 9;
249
+ chunk_digits = 0;
250
+ chunk = 0;
251
+ }
252
+ }
253
+ PRISM_ASSERT(chunk_index == 0);
254
+
255
+ /* powers[level] is 10**(9 * 2**level), for every level a split uses. */
256
+ pm_bignum_t powers[64];
257
+ uint32_t level = 0;
258
+ powers[level].words = pm_bignum_scratch_take(&scratch, 1);
259
+ powers[level].words[0] = PM_BIGNUM_DECIMAL_BASE;
260
+ powers[level].length = 1;
261
+
262
+ for (uint32_t split = 2; split < nchunks; split *= 2) {
263
+ const pm_bignum_t *previous = &powers[level++];
264
+ powers[level].words = pm_bignum_scratch_take(&scratch, 2 * previous->length);
265
+ pm_bignum_multiply(&powers[level], previous, previous, &scratch);
266
+ }
267
+
268
+ pm_integer_parse_decimal_divide_chunks(&result, nchunks, chunks, powers, &scratch);
269
+ xfree_sized(allocation, sizeof(uint32_t) * (size_t) scratch_size);
270
+
271
+ PRISM_ASSERT(result.length > 2 && result.length <= capacity);
272
+ pm_integer_from_bignum(arena, integer, &result, capacity);
273
+ }
274
+
275
+ /**
276
+ * Parse the digits of a decimal integer that does not fit in two words. The
277
+ * words are built in place in the arena: starting from zero, the number is
278
+ * multiplied by 10**9 and the next nine digits are added, until every digit is
279
+ * consumed. The first chunk takes the leftover digits so the rest are exactly
280
+ * nine.
281
+ */
282
+ static void
283
+ pm_integer_parse_decimal_schoolbook(pm_arena_t *arena, pm_integer_t *integer, uint32_t digits, const uint8_t *start, const uint8_t *end) {
284
+ static const uint32_t powof10[10] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
285
+
286
+ /* The words that turn out to be unneeded are given back to the arena
287
+ * below. */
288
+ uint32_t capacity = pm_integer_decimal_words(digits);
289
+ pm_bignum_t bignum = {
290
+ .length = 0,
291
+ .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
292
+ };
293
+
294
+ uint32_t chunk_size = digits % 9 == 0 ? 9 : digits % 9;
295
+ uint32_t chunk_digits = 0;
296
+ uint32_t chunk = 0;
297
+
298
+ for (const uint8_t *cursor = start; cursor < end; cursor++) {
299
+ if (pm_integer_separator(*cursor)) continue;
300
+ chunk = chunk * 10 + pm_integer_parse_digit(*cursor);
301
+
302
+ if (++chunk_digits == chunk_size) {
303
+ pm_bignum_mul_add(&bignum, powof10[chunk_size], chunk);
304
+
305
+ chunk_size = 9;
306
+ chunk_digits = 0;
307
+ chunk = 0;
308
+ }
309
+ }
310
+
311
+ PRISM_ASSERT(bignum.length > 2 && bignum.length <= capacity);
312
+ pm_integer_from_bignum(arena, integer, &bignum, capacity);
313
+ }
314
+
315
+ /**
316
+ * Parse the digits of an integer in the given base, after any prefix.
317
+ */
318
+ static void
319
+ pm_integer_parse_digits(pm_arena_t *arena, pm_integer_t *integer, uint32_t base, const uint8_t *start, const uint8_t *end) {
320
+ /* Leading zeros do not change the value, so skip them here and the paths
321
+ * below always start at a nonzero digit. */
322
+ while (start < end && (*start == '0' || pm_integer_separator(*start))) start++;
323
+
324
+ /* Everything has been consumed if the integer is zero, or if it is invalid
325
+ * and has no digits. Either way the result is zero. */
326
+ if (start >= end) {
327
+ *integer = (pm_integer_t) { 0 };
328
+ return;
329
+ }
330
+
331
+ /* Accumulate into a uint64_t, which covers every integer whose words are
332
+ * stored inline. value * base + digit overflows exactly when value
333
+ * is past limit, or equal to it with digit past limit_digit. */
334
+ const uint64_t limit = UINT64_MAX / base;
335
+ const uint64_t limit_digit = UINT64_MAX % base;
336
+
337
+ const uint8_t *cursor = start;
338
+ uint64_t value = (uint64_t) pm_integer_parse_digit(*cursor++);
339
+
340
+ for (; cursor < end; cursor++) {
341
+ if (pm_integer_separator(*cursor)) continue;
342
+ uint64_t digit = (uint64_t) pm_integer_parse_digit(*cursor);
343
+
344
+ if (value > limit || (value == limit && digit > limit_digit)) {
345
+ uint32_t digits = 0;
346
+ for (const uint8_t *scan = start; scan < end; scan++) {
347
+ if (!pm_integer_separator(*scan)) digits++;
348
+ }
349
+
350
+ if (base != 10) {
351
+ pm_integer_parse_powof2(arena, integer, base, digits, start, end);
352
+ } else if (((uint64_t) digits + 8) / 9 > PM_INTEGER_DIVIDE_THRESHOLD) {
353
+ pm_integer_parse_decimal_divide(arena, integer, digits, start, end);
354
+ } else {
355
+ pm_integer_parse_decimal_schoolbook(arena, integer, digits, start, end);
356
+ }
357
+
358
+ return;
359
+ }
360
+
361
+ value = value * base + digit;
362
+ }
363
+
364
+ *integer = pm_integer_uint64(value);
365
+ }
366
+
367
+ /**
368
+ * Parse an integer from a string. This assumes that the format of the integer
369
+ * has already been validated, as internal validation checks are not performed
370
+ * here.
371
+ */
372
+ void
373
+ pm_integer_parse(pm_arena_t *arena, pm_integer_t *integer, pm_integer_base_t base, const uint8_t *start, const uint8_t *end) {
374
+ /* Ignore unary +. Unary - is parsed differently and will not end up here.
375
+ * Instead, it will modify the parsed integer later. */
376
+ if (*start == '+') start++;
377
+
378
+ /* Skip past any prefixes. */
379
+ switch (base) {
380
+ case PM_INTEGER_BASE_BINARY:
381
+ start += 2; /* 0b */
382
+ break;
383
+ case PM_INTEGER_BASE_OCTAL:
384
+ start++; /* 0 */
385
+ if (*start == '_' || *start == 'o' || *start == 'O') start++; /* o */
386
+ break;
387
+ case PM_INTEGER_BASE_DECIMAL:
388
+ if (*start == '0' && (end - start) > 1) start += 2; /* 0d */
389
+ break;
390
+ case PM_INTEGER_BASE_HEXADECIMAL:
391
+ start += 2; /* 0x */
392
+ break;
393
+ }
394
+
395
+ pm_integer_parse_digits(arena, integer, (uint32_t) base, start, end);
396
+ }
397
+
398
+ /**
399
+ * Parse a float rational literal like 1.5r, given its digits and decimal point
400
+ * without the r suffix, into a numerator and denominator in lowest terms. Its
401
+ * value is its digits read as an integer over 10**k, where k is the number of
402
+ * digits after the decimal point. Since 10**k is 2**k * 5**k, the greatest
403
+ * common divisor of the two is a power of two times a power of five, so the
404
+ * fraction is reduced by dividing those out of the numerator and leaving them
405
+ * out of the denominator.
406
+ */
407
+ void
408
+ pm_integer_parse_rational(pm_arena_t *arena, pm_integer_t *numerator, pm_integer_t *denominator, const uint8_t *start, const uint8_t *end) {
409
+ static const uint32_t powof5[14] = { 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125 };
410
+
411
+ if (*start == '+') start++;
412
+
413
+ const uint8_t *point = memchr(start, '.', (size_t) (end - start));
414
+ PRISM_ASSERT(point != NULL);
415
+
416
+ /* Each trailing zero after the decimal point divides both sides by 10, so
417
+ * drop them before parsing. */
418
+ while (end > point + 1 && (end[-1] == '0' || end[-1] == '_')) end--;
419
+
420
+ uint32_t exponent = 0;
421
+ for (const uint8_t *cursor = point + 1; cursor < end; cursor++) {
422
+ if (*cursor != '_') exponent++;
423
+ }
424
+
425
+ pm_integer_parse_digits(arena, numerator, 10, start, end);
426
+ uint32_t twos = 0;
427
+ uint32_t fives = 0;
428
+
429
+ if (numerator->length > 0 && exponent > 0) {
430
+ uint32_t length = numerator->length;
431
+ pm_bignum_t bignum = {
432
+ .length = length,
433
+ .words = length <= 2 ? numerator->words.value : numerator->words.values
434
+ };
435
+
436
+ uint64_t trailing = pm_bignum_trailing_zeros(&bignum);
437
+ twos = trailing > exponent ? exponent : (uint32_t) trailing;
438
+ pm_bignum_shift_right(&bignum, twos);
439
+
440
+ /* 5**13 is the largest power of five that fits in a word, so divide by
441
+ * it while it divides evenly before dividing by five alone. */
442
+ while (exponent - fives >= 13 && pm_bignum_div_exact(&bignum, powof5[13])) fives += 13;
443
+ while (fives < exponent && pm_bignum_div_exact(&bignum, 5)) fives++;
444
+
445
+ if (length <= 2) {
446
+ *numerator = pm_integer_uint64(pm_bignum_uint64(&bignum));
447
+ } else {
448
+ pm_integer_from_bignum(arena, numerator, &bignum, length);
449
+ }
450
+ }
451
+
452
+ /* The denominator is what is left of 10**exponent. 2378/1024 is just above
453
+ * log2(5), so this capacity never undercounts. */
454
+ uint32_t remaining_fives = exponent - fives;
455
+ uint32_t remaining_twos = exponent - twos;
456
+ uint32_t capacity = (uint32_t) (((uint64_t) remaining_fives * 2378 / 1024 + remaining_twos + 32) / 32);
457
+
458
+ pm_bignum_t bignum = {
459
+ .length = 1,
460
+ .words = PM_ARENA_ALLOC_ARRAY(arena, uint32_t, capacity)
461
+ };
462
+ bignum.words[0] = 1;
463
+
464
+ for (; remaining_fives >= 13; remaining_fives -= 13) pm_bignum_mul_add(&bignum, powof5[13], 0);
465
+ if (remaining_fives > 0) pm_bignum_mul_add(&bignum, powof5[remaining_fives], 0);
466
+ pm_bignum_shift_left(&bignum, remaining_twos);
467
+
468
+ PRISM_ASSERT(bignum.length <= capacity);
469
+ pm_integer_from_bignum(arena, denominator, &bignum, capacity);
470
+ }
471
+
472
+ /**
473
+ * Return a view of the magnitude of an integer as a bignum. The view's words
474
+ * point into the integer, so it must only be read.
475
+ */
476
+ static pm_bignum_t
477
+ pm_integer_bignum(const pm_integer_t *integer) {
478
+ uint32_t length;
479
+ const uint32_t *words = pm_integer_words(integer, &length);
480
+ return (pm_bignum_t) { .length = length, .words = (uint32_t *) words };
481
+ }
482
+
483
+ /**
484
+ * Compare two integers. This function returns -1 if the left integer is less
485
+ * than the right integer, 0 if they are equal, and 1 if the left integer is
486
+ * greater than the right integer.
487
+ */
488
+ int
489
+ pm_integer_compare(const pm_integer_t *left, const pm_integer_t *right) {
490
+ if (left->negative != right->negative) return left->negative ? -1 : 1;
491
+ int negative = left->negative ? -1 : 1;
492
+
493
+ pm_bignum_t left_bignum = pm_integer_bignum(left);
494
+ pm_bignum_t right_bignum = pm_integer_bignum(right);
495
+ return pm_bignum_compare(&left_bignum, &right_bignum) * negative;
496
+ }
497
+
498
+ /**
499
+ * The number of words of storage needed to convert an integer of length words
500
+ * into base 10**9. The integer is below 2**(32 * length), so it needs at most:
501
+ *
502
+ * 32 * length * log10(2) / 9
503
+ *
504
+ * words in base 10**9, rounded up. To find 32 * log10(2) / 9, we approximate it
505
+ * as 1097/1024, which is slightly greater than the actual value so it does not
506
+ * undercount. As in pm_integer_chunks_words, the conversion multiplies the
507
+ * high half by a power of two into this storage, which writes the sum of its
508
+ * factors' lengths, and each factor's length rounds up on its own, so this is
509
+ * one word more than the value needs.
510
+ */
511
+ static PRISM_INLINE uint32_t
512
+ pm_integer_string_words(uint32_t length) {
513
+ return (uint32_t) ((uint64_t) length * 1097 / 1024 + 2);
514
+ }
515
+
516
+ /**
517
+ * Convert length words of a bignum into base 10**9. The result's storage must
518
+ * have room for pm_integer_string_words(length) words. Up to
519
+ * PM_INTEGER_STRING_LEAF words, a copy of the words is divided by 10**9
520
+ * until nothing is left, and each remainder is the next word of the result.
521
+ * Past it, the words split below the largest power of two less than length, at
522
+ * 2**level words, so the value is high * 2**(32 * 2**level) + low with that
523
+ * power of two already in base 10**9 in powers[level], and both halves convert
524
+ * the same way.
525
+ */
526
+ static void
527
+ pm_integer_string_convert(pm_bignum_t *result, const uint32_t *words, uint32_t length, const pm_bignum_t *powers, pm_bignum_scratch_t *scratch) {
528
+ /* The low half of a split can have leading zero words. */
529
+ while (length > 0 && words[length - 1] == 0) length--;
530
+ uint32_t *saved = scratch->cursor;
531
+
532
+ if (length <= PM_INTEGER_STRING_LEAF) {
533
+ pm_bignum_t bignum = { .length = length, .words = pm_bignum_scratch_take(scratch, length) };
534
+ memcpy(bignum.words, words, sizeof(uint32_t) * length);
535
+
536
+ result->length = 0;
537
+ while (bignum.length > 0) result->words[result->length++] = pm_bignum_div(&bignum, PM_BIGNUM_DECIMAL_BASE);
538
+ } else {
539
+ uint32_t level = pm_bsrll((uint64_t) length - 1);
540
+ uint32_t split = (uint32_t) 1 << level;
541
+
542
+ pm_bignum_t high = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_string_words(length - split)) };
543
+ pm_integer_string_convert(&high, words + split, length - split, powers, scratch);
544
+
545
+ PRISM_ASSERT(high.length + powers[level].length <= pm_integer_string_words(length));
546
+ pm_bignum_decimal_multiply(result, &high, &powers[level], scratch);
547
+ scratch->cursor = saved;
548
+
549
+ pm_bignum_t low = { .length = 0, .words = pm_bignum_scratch_take(scratch, pm_integer_string_words(split)) };
550
+ pm_integer_string_convert(&low, words, split, powers, scratch);
551
+ pm_bignum_decimal_add(result, &low);
552
+ }
553
+
554
+ scratch->cursor = saved;
555
+ }
556
+
557
+ /**
558
+ * Convert the words of an integer of more than PM_INTEGER_STRING_THRESHOLD
559
+ * words to a decimal string by divide and conquer. The powers of two that the
560
+ * splits need are computed in base 10**9 by repeated squaring, and the integer
561
+ * is converted into base 10**9, each word of which is nine digits. Everything
562
+ * lives in one block of scratch space from the heap, sized for the worst case:
563
+ * the result, the powers, and the halves and products of the deepest chain of
564
+ * splits, with Karatsuba's temporaries bounded by 12 times the longer factor's
565
+ * length.
566
+ */
567
+ static void
568
+ pm_integer_string_divide(pm_buffer_t *buffer, const uint32_t *words, uint32_t length) {
569
+ uint32_t capacity = pm_integer_string_words(length);
570
+
571
+ /* The scratch space is about 13 times the number of words, which can
572
+ * exceed 32 bits, and on a 32-bit platform its size in bytes can exceed
573
+ * size_t. */
574
+ uint64_t scratch_size = 12 * (uint64_t) capacity + 1024;
575
+ if (scratch_size > SIZE_MAX / sizeof(uint32_t)) PRISM_OOM();
576
+
577
+ uint32_t *allocation = (uint32_t *) xmalloc(sizeof(uint32_t) * (size_t) scratch_size);
578
+ if (allocation == NULL) PRISM_OOM();
579
+ pm_bignum_scratch_t scratch = { .cursor = allocation, .end = allocation + (size_t) scratch_size };
580
+
581
+ pm_bignum_t result = { .length = 0, .words = pm_bignum_scratch_take(&scratch, capacity) };
582
+
583
+ /* powers[level] is 2**(32 * 2**level) in base 10**9, for every level a
584
+ * split uses. */
585
+ pm_bignum_t powers[64];
586
+ uint32_t level = 0;
587
+ powers[level].words = pm_bignum_scratch_take(&scratch, 2);
588
+ powers[level].words[0] = (uint32_t) (PM_BIGNUM_BINARY_BASE % PM_BIGNUM_DECIMAL_BASE);
589
+ powers[level].words[1] = (uint32_t) (PM_BIGNUM_BINARY_BASE / PM_BIGNUM_DECIMAL_BASE);
590
+ powers[level].length = 2;
591
+
592
+ for (uint32_t split = 2; split < length; split *= 2) {
593
+ const pm_bignum_t *previous = &powers[level++];
594
+ powers[level] = (pm_bignum_t) { .length = 0, .words = pm_bignum_scratch_take(&scratch, 2 * previous->length) };
595
+ pm_bignum_decimal_multiply(&powers[level], previous, previous, &scratch);
596
+ }
597
+
598
+ pm_integer_string_convert(&result, words, length, powers, &scratch);
599
+ PRISM_ASSERT(result.length > 0 && result.length < capacity);
600
+
601
+ /* Every word but the most significant is exactly nine digits, padded with
602
+ * zeros. */
603
+ pm_buffer_append_format(buffer, "%" PRIu32, result.words[result.length - 1]);
604
+
605
+ for (uint32_t index = result.length - 1; index > 0; index--) {
606
+ uint32_t word = result.words[index - 1];
607
+ char digits[9];
608
+
609
+ for (size_t digit = 9; digit > 0; digit--) {
610
+ digits[digit - 1] = (char) ('0' + word % 10);
611
+ word /= 10;
612
+ }
613
+
614
+ pm_buffer_append_string(buffer, digits, 9);
615
+ }
616
+
617
+ xfree_sized(allocation, sizeof(uint32_t) * (size_t) scratch_size);
618
+ }
619
+
620
+ /**
621
+ * Convert an integer to a decimal string.
622
+ */
623
+ void
624
+ pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer) {
625
+ if (integer->negative) {
626
+ pm_buffer_append_byte(buffer, '-');
627
+ }
628
+
629
+ /* An integer whose words are inline fits in a uint64_t, so it can be
630
+ * appended directly. */
631
+ if (integer->length <= 2) {
632
+ pm_buffer_append_format(buffer, "%" PRIu64, pm_integer_uint64_value(integer));
633
+ return;
634
+ }
635
+
636
+ if (integer->length > PM_INTEGER_STRING_THRESHOLD) {
637
+ pm_integer_string_divide(buffer, integer->words.values, integer->length);
638
+ return;
639
+ }
640
+
641
+ /* Dividing by 10**9 yields the next nine digits from the remainder, least
642
+ * significant first, so the digits are written from the end of the buffer
643
+ * toward the front. The division is destructive, so it works on a copy. */
644
+ const size_t words_size = sizeof(uint32_t) * integer->length;
645
+
646
+ /* A number of n words has at most n * 32 * log10(2) + 1 decimal digits,
647
+ * which is at most n * 10 for the three or more words that reach here. */
648
+ const size_t capacity = (size_t) integer->length * 10;
649
+
650
+ /* The copy of the words and the digits share one allocation, with the
651
+ * words first so that they are aligned. */
652
+ void *allocation = xmalloc(words_size + capacity);
653
+ if (allocation == NULL) PRISM_OOM();
654
+
655
+ pm_bignum_t bignum = { .length = integer->length, .words = (uint32_t *) allocation };
656
+ memcpy(bignum.words, integer->words.values, words_size);
657
+
658
+ char *digits = (char *) allocation + words_size;
659
+ size_t offset = capacity;
660
+
661
+ while (bignum.length > 0) {
662
+ uint32_t remainder = pm_bignum_div(&bignum, PM_BIGNUM_DECIMAL_BASE);
663
+
664
+ /* Every chunk but the most significant is exactly nine digits, padded
665
+ * with zeros. The most significant chunk is nonzero and stops at its
666
+ * leading digit. */
667
+ for (size_t index = 0; index < 9 && (bignum.length > 0 || remainder > 0); index++) {
668
+ digits[--offset] = (char) ('0' + remainder % 10);
669
+ remainder /= 10;
670
+ }
671
+ }
672
+
673
+ pm_buffer_append_string(buffer, digits + offset, capacity - offset);
674
+ xfree_sized(allocation, words_size + capacity);
675
+ }