vile / lexer / block_scan.h
331 lines · 308 sloc · 15.09 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1#ifndef VILE_BLOCK_SCAN_H
2#define VILE_BLOCK_SCAN_H
3
4#include <stdint.h>
5
6#if !defined(__TINYC__) && (defined(__GNUC__) || defined(__clang__))
7#define VILE_BLOCK_GNU_INTRINSICS 1
8#endif
9
10/* Each block caller has proved that 16 bytes are available. Loads are unaligned,
11 * read-only, and never inspect padding past the source. Returning a scalar
12 * avoids copying SIMD structs through the generated V wrappers. */
13#if defined(__SSE2__) && defined(VILE_BLOCK_GNU_INTRINSICS)
14#include <emmintrin.h>
15
16static inline int vile_block_non_ascii(const unsigned char *source) {
17 return _mm_movemask_epi8(_mm_loadu_si128((const __m128i *)source)) != 0;
18}
19
20static inline int vile_block_alphanumeric_prefix(const unsigned char *source) {
21 __m128i bytes = _mm_loadu_si128((const __m128i *)source);
22 __m128i folded = _mm_or_si128(bytes, _mm_set1_epi8(0x20));
23 __m128i letters = _mm_and_si128(_mm_cmpgt_epi8(folded, _mm_set1_epi8('a' - 1)),
24 _mm_cmpgt_epi8(_mm_set1_epi8('z' + 1), folded));
25 __m128i digits = _mm_and_si128(_mm_cmpgt_epi8(bytes, _mm_set1_epi8('0' - 1)),
26 _mm_cmpgt_epi8(_mm_set1_epi8('9' + 1), bytes));
27 unsigned int stop = (unsigned int)~_mm_movemask_epi8(_mm_or_si128(letters, digits)) & 0xffff;
28 return stop == 0 ? 16 : __builtin_ctz(stop);
29}
30
31static inline int vile_block_string_special(const unsigned char *source) {
32 __m128i bytes = _mm_loadu_si128((const __m128i *)source);
33 __m128i quotes = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('"')),
34 _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\\')));
35 __m128i lines = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('\r')),
36 _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\n')));
37 return _mm_movemask_epi8(_mm_or_si128(quotes, lines)) != 0;
38}
39#elif defined(__aarch64__) && defined(__ARM_NEON) && defined(VILE_BLOCK_GNU_INTRINSICS)
40#include <arm_neon.h>
41
42static inline int vile_block_non_ascii(const unsigned char *source) {
43 return vmaxvq_u8(vld1q_u8(source)) >= 0x80;
44}
45
46static inline int vile_block_alphanumeric_prefix(const unsigned char *source) {
47 uint8x16_t bytes = vld1q_u8(source);
48 uint8x16_t folded = vorrq_u8(bytes, vdupq_n_u8(0x20));
49 uint8x16_t letters = vandq_u8(vcgeq_u8(folded, vdupq_n_u8('a')),
50 vcleq_u8(folded, vdupq_n_u8('z')));
51 uint8x16_t digits = vandq_u8(vcgeq_u8(bytes, vdupq_n_u8('0')),
52 vcleq_u8(bytes, vdupq_n_u8('9')));
53 static const unsigned char lanes[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
54 return vminvq_u8(vbslq_u8(vorrq_u8(letters, digits), vdupq_n_u8(16), vld1q_u8(lanes)));
55}
56
57static inline int vile_block_string_special(const unsigned char *source) {
58 uint8x16_t bytes = vld1q_u8(source);
59 uint8x16_t quotes = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('"')),
60 vceqq_u8(bytes, vdupq_n_u8('\\')));
61 uint8x16_t lines = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('\r')),
62 vceqq_u8(bytes, vdupq_n_u8('\n')));
63 return vmaxvq_u8(vorrq_u8(quotes, lines)) != 0;
64}
65#else
66static inline int vile_block_non_ascii(const unsigned char *source) {
67 unsigned int high = 0;
68 for (int i = 0; i < 16; ++i) high |= source[i];
69 return (high & 0x80) != 0;
70}
71
72static inline int vile_block_alphanumeric_prefix(const unsigned char *source) {
73 for (int i = 0; i < 16; ++i) {
74 unsigned char c = source[i];
75 if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
76 (c >= '0' && c <= '9'))) return i;
77 }
78 return 16;
79}
80
81static inline int vile_block_string_special(const unsigned char *source) {
82 for (int i = 0; i < 16; ++i) {
83 unsigned char c = source[i];
84 if (c == '"' || c == '\\' || c == '\r' || c == '\n') return 1;
85 }
86 return 0;
87}
88#endif
89
90/* Used only after the V scanner has encountered a legacy string escape.
91 * Consume ordinary bytes and validated quote/backslash pairs in one bounded
92 * run. Extended escapes, line endings and real closing quotes return to V.
93 * A mask is reused for all escapes within a block; escaped quote/backslash
94 * bits are discarded together, including pairs crossing the last lane. */
95#if defined(VILE_BLOCK_GNU_INTRINSICS)
96__attribute__((noinline))
97#endif
98static int64_t vile_string_legacy_run(const unsigned char *source, int64_t length) {
99 int64_t i = 0;
100#if defined(VILE_BLOCK_GNU_INTRINSICS) && (defined(__SSE2__) || (defined(__aarch64__) && defined(__ARM_NEON)))
101 while (i <= length - 16) {
102#if defined(__SSE2__)
103 __m128i bytes = _mm_loadu_si128((const __m128i *)(source + i));
104 __m128i quotes = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('"')),
105 _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\\')));
106 __m128i lines = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('\r')),
107 _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\n')));
108 unsigned int mask = (unsigned int)_mm_movemask_epi8(_mm_or_si128(quotes, lines));
109#else
110 uint8x16_t bytes = vld1q_u8(source + i);
111 uint8x16_t quotes = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('"')),
112 vceqq_u8(bytes, vdupq_n_u8('\\')));
113 uint8x16_t lines = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('\r')),
114 vceqq_u8(bytes, vdupq_n_u8('\n')));
115 uint8x16_t stop = vorrq_u8(quotes, lines);
116 static const unsigned char bit_values[8] = {1,2,4,8,16,32,64,128};
117 uint8x8_t bits = vld1_u8(bit_values);
118 unsigned int mask = (unsigned int)vaddv_u8(vand_u8(vget_low_u8(stop), bits)) |
119 ((unsigned int)vaddv_u8(vand_u8(vget_high_u8(stop), bits)) << 8);
120#endif
121 int64_t consumed = i + 16;
122 while (mask) {
123 int lane = __builtin_ctz(mask);
124 int64_t at = i + lane;
125 if (source[at] != '\\' || at + 1 >= length ||
126 (source[at + 1] != '"' && source[at + 1] != '\\')) return at;
127 if (at + 2 > consumed) consumed = at + 2;
128 mask &= ~((1u << (lane + 2)) - 1u);
129 }
130 i = consumed;
131 }
132#endif
133 while (i < length) {
134 unsigned char c = source[i];
135 if (c == '\\') {
136 if (i + 1 >= length || (source[i + 1] != '"' && source[i + 1] != '\\')) return i;
137 i += 2;
138 } else {
139 if (c == '"' || c == '\r' || c == '\n') return i;
140 ++i;
141 }
142 }
143 return i;
144}
145
146/* The short scalar prefix uses an exact byte table: one membership load and
147 * one termination branch, independent of letter/digit/punctuation category. */
148static const unsigned char vile_subsequent[256] = {
149 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
150 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1,
152 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
153 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
154 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
155 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
156 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
162 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165};
166static inline int vile_identifier_subsequent(unsigned char c) {
167 return vile_subsequent[c];
168}
169
170/* A byte belongs to the R5RS alphabet when its high-nibble bit is set
171 * in the mask selected by its low nibble. Two byte shuffles classify every
172 * subsequent character, including punctuation, without a scalar rescan. */
173#if defined(VILE_BLOCK_GNU_INTRINSICS) && (defined(__x86_64__) || defined(__i386__))
174#include <immintrin.h>
175static const unsigned char vile_subsequent_low[16] = {184, 252, 248, 248, 252, 252, 252, 248, 248, 248, 252, 84, 88, 92, 252, 124};
176static const unsigned char vile_subsequent_high[16] = {1,2,4,8,16,32,64,128,0,0,0,0,0,0,0,0};
177
178__attribute__((target("avx2"), noinline))
179static int64_t vile_identifier_avx2(const unsigned char *source, int64_t length) {
180 __m256i low = _mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i *)vile_subsequent_low));
181 __m256i high = _mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i *)vile_subsequent_high));
182 __m256i nibble = _mm256_set1_epi8(15);
183 int64_t i = 0;
184 for (; i <= length - 32; i += 32) {
185 __m256i c = _mm256_loadu_si256((const __m256i *)(source + i));
186 __m256i allowed = _mm256_and_si256(_mm256_shuffle_epi8(low, _mm256_and_si256(c, nibble)),
187 _mm256_shuffle_epi8(high, _mm256_and_si256(_mm256_srli_epi16(c, 4), nibble)));
188 unsigned int stop = (unsigned int)_mm256_movemask_epi8(_mm256_cmpeq_epi8(allowed, _mm256_setzero_si256()));
189 if (stop) return i + __builtin_ctz(stop);
190 }
191 while (i < length && vile_identifier_subsequent(source[i])) ++i;
192 return i;
193}
194
195__attribute__((target("ssse3"), noinline))
196static int64_t vile_identifier_ssse3(const unsigned char *source, int64_t length) {
197 __m128i low = _mm_loadu_si128((const __m128i *)vile_subsequent_low);
198 __m128i high = _mm_loadu_si128((const __m128i *)vile_subsequent_high);
199 __m128i nibble = _mm_set1_epi8(15);
200 int64_t i = 0;
201 for (; i <= length - 16; i += 16) {
202 __m128i c = _mm_loadu_si128((const __m128i *)(source + i));
203 __m128i allowed = _mm_and_si128(_mm_shuffle_epi8(low, _mm_and_si128(c, nibble)),
204 _mm_shuffle_epi8(high, _mm_and_si128(_mm_srli_epi16(c, 4), nibble)));
205 unsigned int stop = (unsigned int)_mm_movemask_epi8(_mm_cmpeq_epi8(allowed, _mm_setzero_si128()));
206 if (stop) return i + __builtin_ctz(stop);
207 }
208 while (i < length && vile_identifier_subsequent(source[i])) ++i;
209 return i;
210}
211#endif
212
213/* Long-run setup stays out of the short-token leaf function. */
214#if defined(VILE_BLOCK_GNU_INTRINSICS)
215__attribute__((noinline))
216#endif
217static int64_t vile_identifier_long(const unsigned char *source, int64_t length) {
218 int64_t i = 16;
219#if defined(VILE_BLOCK_GNU_INTRINSICS) && (defined(__x86_64__) || defined(__i386__))
220 if (length - i >= 64) {
221 if (__builtin_cpu_supports("avx2")) return i + vile_identifier_avx2(source + i, length - i);
222 if (__builtin_cpu_supports("ssse3")) return i + vile_identifier_ssse3(source + i, length - i);
223 }
224#endif
225 while (i <= length - 16) {
226 int count = vile_block_alphanumeric_prefix(source + i);
227 i += count;
228 if (count != 16) break;
229 }
230 while (i < length && vile_identifier_subsequent(source[i])) ++i;
231 return i;
232}
233
234/* Borrow exactly length bytes. Short identifiers need no stack frame, SIMD
235 * setup or CPU dispatch. Finishing 16 valid bytes tail-calls the long kernel. */
236#if defined(VILE_BLOCK_GNU_INTRINSICS)
237__attribute__((noinline))
238#endif
239static int64_t vile_identifier_run(const unsigned char *source, int64_t length) {
240 int64_t prefix = length < 16 ? length : 16;
241 for (int64_t i = 0; i < prefix; ++i) {
242 if (!vile_identifier_subsequent(source[i])) return i;
243 }
244 if (length <= 16) return length;
245 return vile_identifier_long(source, length);
246}
247
248/* Source UTF8 is validated once by the scanner. Ordinary high bytes cannot
249 * contain ASCII specials; only C2 85 and E2 80 A8/A9 need extra line handling.
250 * Candidate masks avoid decoding ordinary Unicode scalars, while inspecting
251 * each candidate against the complete bounded source also handles block edges. */
252static inline int vile_unicode_line_at(const unsigned char *source, int64_t length,
253 int64_t at, int paragraph) {
254 unsigned char c = source[at];
255 if (c == 0xc2) return at + 1 < length && source[at + 1] == 0x85;
256 return c == 0xe2 && at + 2 < length && source[at + 1] == 0x80 &&
257 (source[at + 2] == 0xa8 || (paragraph && source[at + 2] == 0xa9));
258}
259
260static inline int64_t vile_unicode_run(const unsigned char *source, int64_t length,
261 int string_mode, int unicode_mode) {
262 int64_t i = 0;
263#if defined(__SSE2__) && defined(VILE_BLOCK_GNU_INTRINSICS)
264 for (; i <= length - 16; i += 16) {
265 __m128i bytes = _mm_loadu_si128((const __m128i *)(source + i));
266 __m128i stop = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('\r')),
267 _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\n')));
268 if (string_mode) stop = _mm_or_si128(stop,
269 _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8('"')),
270 _mm_cmpeq_epi8(bytes, _mm_set1_epi8('\\'))));
271 unsigned int ascii = (unsigned int)_mm_movemask_epi8(stop);
272 unsigned int mask = ascii;
273 if (unicode_mode) {
274 __m128i candidates = _mm_or_si128(_mm_cmpeq_epi8(bytes, _mm_set1_epi8((char)0xc2)),
275 _mm_cmpeq_epi8(bytes, _mm_set1_epi8((char)0xe2)));
276 mask |= (unsigned int)_mm_movemask_epi8(candidates);
277 }
278 while (mask) {
279 int lane = __builtin_ctz(mask);
280 if ((ascii & (1u << lane)) || (unicode_mode && vile_unicode_line_at(source, length, i + lane, !string_mode)))
281 return i + lane;
282 mask &= mask - 1;
283 }
284 }
285#elif defined(__aarch64__) && defined(__ARM_NEON) && defined(VILE_BLOCK_GNU_INTRINSICS)
286 for (; i <= length - 16; i += 16) {
287 uint8x16_t bytes = vld1q_u8(source + i);
288 uint8x16_t stop = vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('\r')),
289 vceqq_u8(bytes, vdupq_n_u8('\n')));
290 if (string_mode) stop = vorrq_u8(stop,
291 vorrq_u8(vceqq_u8(bytes, vdupq_n_u8('"')),vceqq_u8(bytes, vdupq_n_u8('\\'))));
292 uint8x16_t candidates = vdupq_n_u8(0);
293 if (unicode_mode) candidates = vorrq_u8(vceqq_u8(bytes,vdupq_n_u8(0xc2)),vceqq_u8(bytes,vdupq_n_u8(0xe2)));
294 if (vmaxvq_u8(vorrq_u8(stop,candidates)) != 0) {
295 for (int lane = 0; lane < 16; ++lane) {
296 unsigned char c = source[i + lane];
297 if (c == '\r' || c == '\n' || (string_mode && (c == '"' || c == '\\')) ||
298 (unicode_mode && vile_unicode_line_at(source,length,i + lane,!string_mode))) return i + lane;
299 }
300 }
301 }
302#endif
303 for (; i < length; ++i) {
304 unsigned char c = source[i];
305 if (c == '\r' || c == '\n' || (string_mode && (c == '"' || c == '\\')) ||
306 (unicode_mode && vile_unicode_line_at(source,length,i,!string_mode))) return i;
307 }
308 return length;
309}
310
311#if defined(VILE_BLOCK_GNU_INTRINSICS)
312__attribute__((noinline))
313#endif
314static int64_t vile_string_unicode_run(const unsigned char *source, int64_t length) {
315 return vile_unicode_run(source,length,1,1);
316}
317#if defined(VILE_BLOCK_GNU_INTRINSICS)
318__attribute__((noinline))
319#endif
320static int64_t vile_comment_unicode_run(const unsigned char *source, int64_t length) {
321 return vile_unicode_run(source,length,0,1);
322}
323
324#if defined(VILE_BLOCK_GNU_INTRINSICS)
325__attribute__((noinline))
326#endif
327static int64_t vile_comment_ascii_run(const unsigned char *source, int64_t length) {
328 return vile_unicode_run(source,length,0,0);
329}
330
331#endif
332