-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreproc.c
3549 lines (2959 loc) · 110 KB
/
preproc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "preproc.h"
#include "alloc.h"
#include "compiler.h"
#include "diagnostics.h"
#include "fs.h"
#include "hashtbl.h"
#include "io.h"
#include "log.h"
#include "profile.h"
#include "program.h"
#include "thrd.h"
#include "util.h"
#include "vector.h"
#include <ctype.h>
#include <stddef.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#if ARCH_AARCH64 && __has_include(<arm_neon.h>)
#define USE_NEON 1
#include <arm_neon.h>
#include <stdint.h>
#endif
struct preproc_text {
struct text_pos pos;
const char *text;
size_t len;
// the values that can be set by the preprocessor
size_t line;
const char *file;
struct path_components path;
// bool
struct vector *enabled;
};
struct preproc {
struct fs *fs;
struct arena_allocator *arena;
// struct preproc_text
struct vector *texts;
struct vector *unexpanded_buffer_tokens;
struct vector *buffer_tokens;
// ustr_t, struct preproc_define
struct hashtbl *defines;
// stores current macros we are expanding in to prevent recursion
struct hashtbl *parents;
struct compiler_diagnostics *diagnostics;
struct preproc_create_args args;
// if the current line has seen a token that is not whitespace
bool line_has_nontrivial_token;
// whether we are in an include/embed that accepts <foo> style strings
bool in_angle_string_context;
// if a ## was just seen, we concat the next token
bool concat_next_token;
// if a defined/__has_feature etc was just seen, we don't expand until end
ustr_t query_ident;
bool keep_next_token;
struct vector *events;
// after a `defined(SYM` symbol, we need to know to look to strip the close
// bracket
bool waiting_for_close;
};
static const char *preproc_token_name(enum preproc_token_ty ty) {
#define CASE_RET(name) \
case name: \
return #name;
switch (ty) {
CASE_RET(PREPROC_TOKEN_TY_UNKNOWN)
CASE_RET(PREPROC_TOKEN_TY_EOF)
CASE_RET(PREPROC_TOKEN_TY_DIRECTIVE)
CASE_RET(PREPROC_TOKEN_TY_IDENTIFIER)
CASE_RET(PREPROC_TOKEN_TY_PREPROC_NUMBER)
CASE_RET(PREPROC_TOKEN_TY_STRING_LITERAL)
CASE_RET(PREPROC_TOKEN_TY_PUNCTUATOR)
CASE_RET(PREPROC_TOKEN_TY_NEWLINE)
CASE_RET(PREPROC_TOKEN_TY_WHITESPACE)
CASE_RET(PREPROC_TOKEN_TY_COMMENT)
CASE_RET(PREPROC_TOKEN_TY_OTHER)
}
#undef CASE_RET
}
enum preproc_define_value_ty {
PREPROC_DEFINE_VALUE_TY_TOKEN,
PREPROC_DEFINE_VALUE_TY_TOKEN_VEC,
PREPROC_DEFINE_VALUE_TY_MACRO_FN
};
enum FLAG_ENUM preproc_macro_fn_flags {
PREPROC_MACRO_FN_FLAG_NONE = 0,
PREPROC_MACRO_FN_FLAG_VARIADIC = 1,
};
enum preproc_macro_fn_token_ty {
PREPROC_MACRO_FN_TOKEN_TY_TOKEN,
PREPROC_MACRO_FN_TOKEN_TY_PARAM,
PREPROC_MACRO_FN_TOKEN_TY_STRINGIFIED_PARAM,
PREPROC_MACRO_FN_TOKEN_TY_VA_ARGS,
PREPROC_MACRO_FN_TOKEN_TY_STRINGIFIED_VA_ARGS,
PREPROC_MACRO_FN_TOKEN_TY_VA_OPT,
};
struct preproc_macro_fn_token {
enum preproc_macro_fn_token_ty ty;
union {
struct preproc_token token;
struct preproc_token opt;
size_t param;
};
};
struct preproc_macro_fn {
size_t num_params;
struct vector *tokens;
enum preproc_macro_fn_flags flags;
};
struct preproc_define_value {
enum preproc_define_value_ty ty;
union {
struct preproc_token token;
struct vector *vec;
struct preproc_macro_fn macro_fn;
};
};
enum FLAG_ENUM preproc_define_flags {
PREPROC_DEFINE_FLAG_NONE = 0,
// e.g `defined`, `__has_include`
PREPROC_DEFINE_FLAG_WELL_KNOWN = 1 << 0,
// can be overriden without error
// primarily used for macros that fix bugs in system headers
PREPROC_DEFINE_FLAG_WEAK = 1 << 1,
};
struct preproc_define {
struct preproc_token name;
struct preproc_define_value value;
enum preproc_define_flags flags;
};
static void preproc_create_builtin_macros(struct preproc *preproc,
enum compile_target target) {
// FIXME: vectors leak, vector should probably be arena-based
#define DEF_BUILTIN(tok_ty, n, v, f) \
do { \
size_t name_len = strlen((n)); \
ustr_t ident = {.str = (n), .len = name_len}; \
\
struct preproc_define define = { \
.name = {.ty = PREPROC_TOKEN_TY_IDENTIFIER, \
.span = MK_INVALID_TEXT_SPAN(0, name_len), \
.text = MK_USTR((n))}, \
.value = \
{ \
.ty = PREPROC_DEFINE_VALUE_TY_TOKEN, \
.token = {.ty = tok_ty, \
.span = MK_INVALID_TEXT_SPAN(0, strlen((v))), \
.text = MK_USTR((v))}, \
}, \
.flags = f}; \
\
hashtbl_insert(preproc->defines, &ident, &define); \
} while (0)
#define DEF_BUILTIN_NUM(n, v) \
DEF_BUILTIN(PREPROC_TOKEN_TY_PREPROC_NUMBER, (n), (v), \
PREPROC_DEFINE_FLAG_NONE)
#define DEF_BUILTIN_IDENT(n, v) \
DEF_BUILTIN(PREPROC_TOKEN_TY_IDENTIFIER, (n), (v), PREPROC_DEFINE_FLAG_NONE)
#define DEF_BUILTIN_WELL_KNOWN(n) \
DEF_BUILTIN(PREPROC_TOKEN_TY_IDENTIFIER, (n), (n), \
PREPROC_DEFINE_FLAG_WELL_KNOWN)
// HACK: musl doesn't include `stdarg.h` so until we support
// `__builtin_va_list`, just typedef it away
// DEF_BUILTIN_IDENT("__builtin_va_list", "void *");
DEF_BUILTIN_WELL_KNOWN("defined");
DEF_BUILTIN_WELL_KNOWN("has_include");
DEF_BUILTIN_WELL_KNOWN("__has_include");
// TODO: implement these
DEF_BUILTIN_WELL_KNOWN("has_embed");
DEF_BUILTIN_WELL_KNOWN("__has_embed");
DEF_BUILTIN_WELL_KNOWN("__has_feature");
DEF_BUILTIN_WELL_KNOWN("__has_builtin");
DEF_BUILTIN_WELL_KNOWN("__has_attribute");
DEF_BUILTIN_WELL_KNOWN("__has_c_attribute");
DEF_BUILTIN_WELL_KNOWN("__has_extension");
// needed because clang headers do not properly avoid it
// TODO: have more flexible solution to this (so we can ignore arbitary
// __foo type macros)
DEF_BUILTIN_WELL_KNOWN("__building_module");
DEF_BUILTIN_IDENT("__extension__", "");
DEF_BUILTIN_NUM("__FLT_MAX__", "3.40282347E+38");
DEF_BUILTIN_NUM("__DBL_MAX__", "1.7976931348623157E+308");
DEF_BUILTIN_NUM("__JCC__", "1");
DEF_BUILTIN_NUM("__jcc__", "1");
DEF_BUILTIN_NUM("__STDC__", "1");
switch (preproc->args.c_standard) {
case COMPILE_C_STANDARD_C11:
DEF_BUILTIN_NUM("__STDC_VERSION__", "201112L");
break;
case COMPILE_C_STANDARD_C17:
DEF_BUILTIN_NUM("__STDC_VERSION__", "201710L");
break;
case COMPILE_C_STANDARD_C23:
DEF_BUILTIN_NUM("__STDC_VERSION__", "202311L");
break;
case COMPILE_C_STANDARD_C2Y:
DEF_BUILTIN_NUM("__STDC_VERSION__", "202400L");
break;
}
// TODO: support different version targets. This is C11
DEF_BUILTIN_NUM("__STDC_HOSTED__", "1");
DEF_BUILTIN_NUM("__STDC_UTF_16__", "1");
DEF_BUILTIN_NUM("__STDC_UTF_32__", "1");
// C23 only
DEF_BUILTIN_NUM("__STDC_EMBED_NOT_FOUND__", "0");
DEF_BUILTIN_NUM("__STDC_EMBED_FOUND__", "1");
DEF_BUILTIN_NUM("__STDC_EMBED_EMPTY__", "2");
// C11
DEF_BUILTIN_NUM("__STDC_NO_ATOMICS__", "1");
DEF_BUILTIN_NUM("__STDC_NO_COMPLEX__", "1");
DEF_BUILTIN_NUM("__STDC_NO_THREADS__", "1");
DEF_BUILTIN_NUM("__STDC_NO_VLA__", "1");
// currently all backends are little endian
DEF_BUILTIN_NUM("__LITTLE_ENDIAN__", "1");
if (target == COMPILE_TARGET_MACOS_ARM64 ||
target == COMPILE_TARGET_MACOS_X86_64) {
// on older SDKs `__DARWIN_OS_INLINE` is not properly defined
struct preproc_define define = {
.name = {.ty = PREPROC_TOKEN_TY_IDENTIFIER,
.span = MK_INVALID_TEXT_SPAN(0, strlen("__DARWIN_OS_INLINE")),
.text = MK_USTR("__DARWIN_OS_INLINE")},
.value =
{
.ty = PREPROC_DEFINE_VALUE_TY_TOKEN_VEC,
.vec = vector_create_in_arena(sizeof(struct preproc_token),
preproc->arena),
},
.flags = PREPROC_DEFINE_FLAG_WEAK};
vector_push_back(define.value.vec,
&(struct preproc_token){
.ty = PREPROC_TOKEN_TY_IDENTIFIER,
.span = MK_INVALID_TEXT_SPAN(0, strlen("static")),
.text = MK_USTR("static")});
vector_push_back(define.value.vec,
&(struct preproc_token){
.ty = PREPROC_TOKEN_TY_IDENTIFIER,
.span = MK_INVALID_TEXT_SPAN(0, strlen("inline")),
.text = MK_USTR("inline")});
hashtbl_insert(preproc->defines, &MK_USTR("__DARWIN_OS_INLINE"), &define);
}
switch (target) {
case COMPILE_TARGET_MACOS_ARM64:
// needed for <TargetConditionals.h>
DEF_BUILTIN_NUM("TARGET_CPU_ARM64", "1");
DEF_BUILTIN_NUM("TARGET_OS_MAC", "1");
DEF_BUILTIN_NUM("__APPLE__", "1");
DEF_BUILTIN_NUM("__aarch64__", "1");
DEF_BUILTIN_NUM("__arm64__", "1");
DEF_BUILTIN_NUM("__LP64__", "1");
DEF_BUILTIN_NUM("_LP64", "1");
break;
case COMPILE_TARGET_MACOS_X86_64:
// needed for <TargetConditionals.h>
DEF_BUILTIN_NUM("TARGET_CPU_X86_64", "1");
DEF_BUILTIN_NUM("TARGET_OS_MAC", "1");
DEF_BUILTIN_NUM("__APPLE__", "1");
DEF_BUILTIN_NUM("__x86_64__", "1");
DEF_BUILTIN_NUM("__LP64__", "1");
DEF_BUILTIN_NUM("_LP64", "1");
break;
case COMPILE_TARGET_LINUX_ARM64:
DEF_BUILTIN_NUM("__linux__", "1");
DEF_BUILTIN_NUM("__aarch64__", "1");
DEF_BUILTIN_NUM("__arm64__", "1");
DEF_BUILTIN_NUM("__LP64__", "1");
DEF_BUILTIN_NUM("_LP64", "1");
break;
case COMPILE_TARGET_LINUX_X86_64:
DEF_BUILTIN_NUM("__linux__", "1");
DEF_BUILTIN_NUM("__x86_64__", "1");
DEF_BUILTIN_NUM("__LP64__", "1");
DEF_BUILTIN_NUM("_LP64", "1");
break;
case COMPILE_TARGET_LINUX_RV32I:
DEF_BUILTIN_NUM("__linux__", "1");
DEF_BUILTIN_NUM("__riscv", "1");
DEF_BUILTIN_NUM("__riscv32", "1");
DEF_BUILTIN_NUM("__riscv__", "1");
DEF_BUILTIN_NUM("__ILP32__", "1");
DEF_BUILTIN_NUM("_ILP32", "1");
DEF_BUILTIN_IDENT("__INT32_TYPE__", "int");
DEF_BUILTIN_IDENT("__INTPTR_TYPE__", "int");
break;
case COMPILE_TARGET_EEP:
break;
}
// magic macros such as __TIME__ are handled in the processor
#undef DEF_BUILTIN
}
struct preproc_events preproc_get_events(struct preproc *preproc) {
DEBUG_ASSERT(
preproc->events,
"events was NULL (was PREPROC_EXPAND_TOKEN_FLAG_EMIT_EVENTS set?)");
return (struct preproc_events){.events = vector_head(preproc->events),
.num_events = vector_length(preproc->events)};
}
static struct preproc_text create_preproc_text(struct preproc *preproc,
const char *text,
const char *path) {
struct path_components components =
path ? path_components(preproc->arena, path)
: (struct path_components){NULL, NULL, NULL};
// FIXME: should have this in temp arena
struct vector *enabled = vector_create_in_arena(sizeof(bool), preproc->arena);
bool is_enabled = true;
vector_push_back(enabled, &is_enabled);
// FIXME: spans are entirely broken at the moment
return (struct preproc_text){
.text = text,
.len = strlen(text),
.pos = {.file = path, .col = 0, .line = 0, .idx = 0},
.line = 0,
.file = components.file,
.path = components,
.enabled = enabled};
}
enum preproc_special_macro {
PREPROC_SPECIAL_MACRO_FILE,
PREPROC_SPECIAL_MACRO_LINE,
PREPROC_SPECIAL_MACRO_TIME,
PREPROC_SPECIAL_MACRO_DATE,
};
enum preproc_unexpanded_token_ty {
PREPROC_UNEXPANDED_TOKEN_TY_BEGIN_EXPAND,
PREPROC_UNEXPANDED_TOKEN_TY_END_EXPAND,
PREPROC_UNEXPANDED_TOKEN_TY_TOKEN,
};
struct preproc_unexpanded_token {
enum preproc_unexpanded_token_ty ty;
union {
ustr_t ident;
struct preproc_token token;
};
};
static bool is_newline(char c) { return c == '\n'; }
static bool is_whitespace(char c) { return isspace(c) && !is_newline(c); }
static bool is_identifier_char(char c) {
return isalpha(c) || isdigit(c) || c == '_';
}
static bool is_first_identifier_char(char c) {
return is_identifier_char(c) && !isdigit(c);
}
static bool is_preproc_number_char(char c) {
// legal chars are letters, digits, underscores, periods, and exponents (not
// handled here)
return isalpha(c) || isdigit(c) || c == '_' || c == '.';
}
static once_flag SPECIAL_MACROS_ONCE = ONCE_FLAG_INIT;
static struct hashtbl *SPECIAL_MACROS = NULL;
static void build_special_macros(void) {
debug("building special macro table");
SPECIAL_MACROS =
hashtbl_create_ustr_keyed(sizeof(enum preproc_special_macro));
#define SPECIAL_MACRO(kw, ty) \
do { \
ustr_t k = { \
.str = kw, \
.len = strlen(kw), \
}; \
enum preproc_special_macro v = ty; \
hashtbl_insert(SPECIAL_MACROS, &k, &v); \
} while (0)
SPECIAL_MACRO("__FILE__", PREPROC_SPECIAL_MACRO_FILE);
SPECIAL_MACRO("__LINE__", PREPROC_SPECIAL_MACRO_LINE);
SPECIAL_MACRO("__TIME__", PREPROC_SPECIAL_MACRO_TIME);
SPECIAL_MACRO("__DATE__", PREPROC_SPECIAL_MACRO_DATE);
#undef SPECIAL_MACRO
debug("built special macro table (len=%zu)", hashtbl_size(SPECIAL_MACROS));
}
enum preproc_create_result
preproc_create(struct program program, struct fs *fs,
struct preproc_create_args args,
struct compiler_diagnostics *diagnostics,
enum compile_preproc_mode mode, struct preproc **preproc) {
call_once(&SPECIAL_MACROS_ONCE, build_special_macros);
if (args.fixed_timestamp) {
DEBUG_ASSERT(strlen(args.fixed_timestamp) >= 19,
"`fixed_timestamp` must be at least 19");
}
info("beginning preproc stage");
struct arena_allocator *arena;
arena_allocator_create("preproc", &arena);
struct preproc *p = nonnull_malloc(sizeof(*p));
p->arena = arena;
p->fs = fs;
p->args = args;
p->diagnostics = diagnostics;
p->events = NULL;
switch (mode) {
case COMPILE_PREPROC_MODE_PREPROC:
break;
case COMPILE_PREPROC_MODE_EMIT_EVENTS:
p->events = vector_create_in_arena(sizeof(struct preproc_event), arena);
break;
case COMPILE_PREPROC_MODE_NO_PREPROC:
BUG("preproc with COMPILE_PREPROC_MODE_NO_PREPROC makes no sense (preproc "
"should be skipped)");
}
if (args.verbose) {
fprintf(stderr, "sys_include_paths: \n");
for (size_t i = 0; i < args.num_sys_include_paths; i++) {
fprintf(stderr, " %s\n", args.sys_include_paths[i]);
}
fprintf(stderr, "\ninclude_paths: \n");
for (size_t i = 0; i < args.num_include_paths; i++) {
fprintf(stderr, " %s\n", args.include_paths[i]);
}
}
p->texts = vector_create_in_arena(sizeof(struct preproc_text), arena);
struct preproc_text text = create_preproc_text(p, program.text, args.path);
vector_push_back(p->texts, &text);
p->line_has_nontrivial_token = false;
p->in_angle_string_context = false;
p->concat_next_token = false;
p->keep_next_token = false;
p->waiting_for_close = false;
p->defines = hashtbl_create_ustr_keyed_in_arena(
p->arena, sizeof(struct preproc_define));
// tokens that have appeared (e.g from a macro) and need to be processed next
p->buffer_tokens =
vector_create_in_arena(sizeof(struct preproc_token), arena);
p->unexpanded_buffer_tokens =
vector_create_in_arena(sizeof(struct preproc_unexpanded_token), arena);
p->parents = hashtbl_create_ustr_keyed_in_arena(p->arena, 0);
*preproc = p;
preproc_create_builtin_macros(p, args.target);
for (size_t i = 0; i < args.num_defines; i++) {
struct preproc_define_macro *define = &args.defines[i];
// FIXME: properly tokenise
if (!define->value.str || is_first_identifier_char(define->value.str[0])) {
struct preproc_define def = {
.name = {.ty = PREPROC_TOKEN_TY_IDENTIFIER,
.span = MK_INVALID_TEXT_SPAN(0, define->name.len),
.text = define->name},
.value = {
.ty = PREPROC_DEFINE_VALUE_TY_TOKEN,
.token = {.ty = PREPROC_TOKEN_TY_IDENTIFIER,
.span = MK_INVALID_TEXT_SPAN(0, define->value.len),
.text = define->value},
}};
hashtbl_insert(p->defines, &define->name, &def);
} else if (isdigit(define->value.str[0])) {
struct preproc_define def = {
.name = {.ty = PREPROC_TOKEN_TY_IDENTIFIER,
.span = MK_INVALID_TEXT_SPAN(0, define->name.len),
.text = define->name},
.value = {
.ty = PREPROC_DEFINE_VALUE_TY_TOKEN,
.token = {.ty = PREPROC_TOKEN_TY_PREPROC_NUMBER,
.span = MK_INVALID_TEXT_SPAN(0, define->value.len),
.text = define->value},
}};
hashtbl_insert(p->defines, &define->name, &def);
} else if (define->value.str[0] == '\"') {
struct preproc_define def = {
.name = {.ty = PREPROC_TOKEN_TY_IDENTIFIER,
.span = MK_INVALID_TEXT_SPAN(0, define->name.len),
.text = define->name},
.value = {
.ty = PREPROC_DEFINE_VALUE_TY_TOKEN,
.token = {.ty = PREPROC_TOKEN_TY_STRING_LITERAL,
.span = MK_INVALID_TEXT_SPAN(0, define->value.len),
.text = define->value},
}};
hashtbl_insert(p->defines, &define->name, &def);
} else {
TODO("other -D tok types");
}
}
return PREPROC_CREATE_RESULT_SUCCESS;
}
void preproc_free(struct preproc **preproc) {
arena_allocator_free(&(*preproc)->arena);
(*preproc)->arena = NULL;
free(*preproc);
*preproc = NULL;
}
static void find_multiline_comment_end_scalar(struct preproc_text *preproc_text,
struct text_pos *cur_pos) {
while (/* token must be at least 2 chars */ cur_pos->idx + 1 <
preproc_text->len) {
if (preproc_text->text[cur_pos->idx] == '\n') {
next_line(cur_pos);
} else if (preproc_text->text[cur_pos->idx] == '*' &&
preproc_text->text[cur_pos->idx + 1] == '/') {
// found it!
next_col(cur_pos);
next_col(cur_pos);
return;
} else {
next_col(cur_pos);
}
}
// if not found, it will just push to end of file and next token will be EOF
}
// disabled as does not correctly handle newlines
#if USE_NEON && 0
void find_multiline_comment_end(struct preproc_text *preproc_text,
struct text_pos *cur_pos);
void find_multiline_comment_end(struct preproc_text *preproc_text,
struct text_pos *cur_pos) {
const unsigned char *text =
(const unsigned char *)preproc_text->text + cur_pos->idx;
size_t len = preproc_text->len;
uint8x16_t indices0 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
uint8x16_t indices1 = {16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31};
#define BLOCK_SZ 32
size_t rem = len - cur_pos->idx;
size_t nb = rem / BLOCK_SZ;
uint8_t trail = 0;
for (size_t i = 0; i < nb; i++) {
uint8x16_t v0 = vld1q_u8(text);
uint8x16_t v1 = vld1q_u8(text + 16);
uint8x16_t eq_nl0 = vceqq_u8(v0, vdupq_n_u8('\n'));
uint8x16_t eq_nl1 = vceqq_u8(v1, vdupq_n_u8('\n'));
uint16x8_t widen0_0 = vpaddlq_u8(eq_nl0);
uint32x4_t widen0_1 = vpaddlq_u16(widen0_0);
uint64x2_t widen0_2 = vpaddlq_u32(widen0_1);
uint16x8_t widen1_0 = vpaddlq_u8(eq_nl1);
uint32x4_t widen1_1 = vpaddlq_u16(widen1_0);
uint64x2_t widen1_2 = vpaddlq_u32(widen1_1);
uint8x16_t eq_star0 = vceqq_u8(v0, vdupq_n_u8('*'));
uint8x16_t eq_star1 = vceqq_u8(v1, vdupq_n_u8('*'));
if (trail && vgetq_lane_u8(v0, 0) == '/') {
cur_pos->idx += 1;
return;
}
cur_pos->line += vgetq_lane_u64(widen0_2, 0) + vgetq_lane_u64(widen0_2, 1);
if (vgetq_lane_u8(eq_star0, 15) && vgetq_lane_u8(v1, 0) == '/') {
cur_pos->idx += 16 + 1;
return;
}
cur_pos->line += vgetq_lane_u64(widen1_2, 0) + vgetq_lane_u64(widen1_2, 1);
trail = vgetq_lane_u8(eq_star1, 15);
uint8x16_t s0 = vextq_u8(v0, vdupq_n_u8(0), 1);
uint8x16_t s1 = vextq_u8(v1, vdupq_n_u8(0), 1);
uint8x16_t eq_slash0 = vceqq_u8(s0, vdupq_n_u8('/'));
uint8x16_t eq_slash1 = vceqq_u8(s1, vdupq_n_u8('/'));
uint8x16_t match0 = vandq_u8(eq_star0, eq_slash0);
uint8x16_t match1 = vandq_u8(eq_star1, eq_slash1);
uint8x16_t masked0 = vbslq_u8(match0, indices0, vdupq_n_u8(0xFF));
uint8x16_t masked1 = vbslq_u8(match1, indices1, vdupq_n_u8(0xFF));
uint8_t min0 = vminvq_u8(masked0);
uint8_t min1 = vminvq_u8(masked1);
if (min0 != 0xFF) {
cur_pos->idx += min0 + 2;
return;
}
if (min1 != 0xFF) {
cur_pos->idx += min1 + 2;
return;
}
text += BLOCK_SZ;
cur_pos->idx += BLOCK_SZ;
}
#undef BLOCK_SZ
if (trail) {
cur_pos->idx--;
}
find_multiline_comment_end_scalar(preproc_text, cur_pos);
}
#else
static void find_multiline_comment_end(struct preproc_text *preproc_text,
struct text_pos *cur_pos) {
find_multiline_comment_end_scalar(preproc_text, cur_pos);
}
#endif
static bool try_consume(struct preproc_text *preproc_text, struct text_pos *pos,
char c) {
DEBUG_ASSERT(
preproc_text->pos.idx != pos->idx,
"calling `try_consume` with `pos` the same as preproc makes no sense");
DEBUG_ASSERT(c != '\n', "can't use on newlines");
if (pos->idx < preproc_text->len && preproc_text->text[pos->idx] == c) {
next_col(pos);
return true;
}
return false;
}
// tries to consume two tokens (you can infer this from the function name)
static bool try_consume2(struct preproc_text *preproc_text,
struct text_pos *pos, char c0, char c1) {
DEBUG_ASSERT(
preproc_text->pos.idx != pos->idx,
"calling `try_consume` with `pos` the same as preproc makes no sense");
DEBUG_ASSERT(c0 != '\n' && c1 != '\n', "can't use on newlines");
if (pos->idx + 1 < preproc_text->len && preproc_text->text[pos->idx] == c0 &&
preproc_text->text[pos->idx + 1] == c1) {
next_col(pos);
next_col(pos);
return true;
}
return false;
}
void preproc_next_raw_token(struct preproc *preproc,
struct preproc_token *token) {
struct preproc_text *preproc_text;
while (vector_length(preproc->texts)) {
preproc_text = vector_tail(preproc->texts);
if (preproc_text->pos.idx < preproc_text->len ||
vector_length(preproc->texts) == 1) {
break;
}
// FIXME: why does this sometimes get hit?
// DEBUG_ASSERT(vector_length(preproc_text->enabled) == 1,
// "text %s ended with enabled depth of %zu (should have been
// 1)", preproc_text->file,
// vector_length(preproc_text->enabled));
vector_pop(preproc->texts);
}
const char *text = preproc_text->text;
while (vector_length(preproc->unexpanded_buffer_tokens)) {
struct preproc_unexpanded_token *unexp =
vector_pop(preproc->unexpanded_buffer_tokens);
switch (unexp->ty) {
case PREPROC_UNEXPANDED_TOKEN_TY_BEGIN_EXPAND:
hashtbl_insert(preproc->parents, &unexp->ident, NULL);
break;
case PREPROC_UNEXPANDED_TOKEN_TY_END_EXPAND:
hashtbl_remove(preproc->parents, &unexp->ident);
break;
case PREPROC_UNEXPANDED_TOKEN_TY_TOKEN:
*token = unexp->token;
return;
}
}
struct text_pos start = preproc_text->pos;
struct text_pos end = start;
size_t len = preproc_text->len;
if (start.idx >= preproc_text->len) {
token->ty = PREPROC_TOKEN_TY_EOF;
token->text = MK_EMPTY_USTR();
token->span.start = start;
token->span.end = end;
return;
}
while (end.idx + 1 < len && text[end.idx] == '\\' &&
is_newline(text[end.idx + 1])) {
// literally just skip this, don't even generate a token
next_col(&end);
next_line(&end);
preproc_text->pos = end;
preproc->line_has_nontrivial_token = false;
start = preproc_text->pos;
end = start;
}
if (end.idx < len && is_newline(text[end.idx])) {
next_line(&end);
preproc->line_has_nontrivial_token = false;
preproc->in_angle_string_context = false;
token->ty = PREPROC_TOKEN_TY_NEWLINE;
token->span = (struct text_span){.start = start, .end = end};
token->text =
(ustr_t){.str = &text[start.idx], .len = text_span_len(&token->span)};
preproc_text->pos = end;
return;
}
while (end.idx < len && is_whitespace(text[end.idx])) {
next_col(&end);
}
if (start.idx != end.idx) {
// we have processed whitespace
token->ty = PREPROC_TOKEN_TY_WHITESPACE;
token->span = (struct text_span){.start = start, .end = end};
token->text =
(ustr_t){.str = &text[start.idx], .len = text_span_len(&token->span)};
preproc_text->pos = end;
return;
}
// we will find a token here that is not whitespace
// save old value as it is needed for determining if a directive is valid
bool line_has_nontrivial_token = preproc->line_has_nontrivial_token;
preproc->line_has_nontrivial_token = true;
char c = text[end.idx];
char c2 = text[end.idx + 1];
if (c == '/' && c2 == '/') {
while (end.idx < len && !is_newline(text[end.idx])) {
next_col(&end);
}
token->ty = PREPROC_TOKEN_TY_COMMENT;
token->span = (struct text_span){.start = start, .end = end};
token->text =
(ustr_t){.str = &text[start.idx], .len = text_span_len(&token->span)};
preproc_text->pos = end;
return;
}
if (c == '/' && c2 == '*') {
next_col(&end);
next_col(&end);
find_multiline_comment_end(preproc_text, &end);
token->ty = PREPROC_TOKEN_TY_COMMENT;
token->span = (struct text_span){.start = start, .end = end};
token->text =
(ustr_t){.str = &text[start.idx], .len = text_span_len(&token->span)};
preproc_text->pos = end;
return;
}
// FIXME: make sure there are excess null chars at end of string
switch (c) {
case 'L': {
char next = text[end.idx + 1];
if (next == '<' || next == '"' || next == '\'') {
c = next;
next_col(&end);
goto string_literal;
}
// BUG: doesn't work on JCC
// goto not_punctuator;
while (end.idx < len && is_identifier_char(text[end.idx])) {
next_col(&end);
}
token->ty = PREPROC_TOKEN_TY_IDENTIFIER;
token->span = (struct text_span){.start = start, .end = end};
token->text =
(ustr_t){.str = &text[start.idx], .len = text_span_len(&token->span)};
preproc_text->pos = end;
return;
}
case '<':
case '"':
case '\'':
string_literal: {
if (c == '<' && !preproc->in_angle_string_context) {
break;
}
// string/char literal
// skip first single-quote
next_col(&end);
char end_char = c == '<' ? '>' : c;
// move forward while
bool char_escaped = false;
// BUG: doesn't work on JCC
// for (size_t i = end.idx;
// i < preproc_text->len &&
// !(!char_escaped && text[i] == end_char);
// i++) {
for (size_t i = end.idx;; i++) {
if (!(i < len && !(!char_escaped && text[i] == end_char))) {
break;
}
// next char is escaped if this char is a non-escaped backslash
char_escaped = !char_escaped && text[i] == '\\';
next_col(&end);
}
// skip final single-quote
next_col(&end);
token->ty = PREPROC_TOKEN_TY_STRING_LITERAL;
token->span = (struct text_span){.start = start, .end = end};
token->text =
(ustr_t){.str = &text[start.idx], .len = text_span_len(&token->span)};
preproc_text->pos = end;
return;
}
case '#':
next_col(&end);
if (!line_has_nontrivial_token) {
token->ty = PREPROC_TOKEN_TY_DIRECTIVE;
} else if (try_consume(preproc_text, &end, '#')) {
token->ty = PREPROC_TOKEN_TY_PUNCTUATOR;
token->punctuator = (struct preproc_token_punctuator){
.ty = PREPROC_TOKEN_PUNCTUATOR_TY_CONCAT};
} else {
token->ty = PREPROC_TOKEN_TY_PUNCTUATOR;
token->punctuator = (struct preproc_token_punctuator){
.ty = PREPROC_TOKEN_PUNCTUATOR_TY_STRINGIFY};
}
token->span = (struct text_span){.start = start, .end = end};
token->text =
(ustr_t){.str = &text[start.idx], .len = text_span_len(&token->span)};
preproc_text->pos = end;
return;
default:
break;
}
// we need to check for preproccessing number first as they can begin with `.`
// and would be wrongly classed as punctuators
if (isdigit(c) || (end.idx + 1 < len && c == '.' && isdigit(text[end.idx]))) {
next_col(&end);
while (end.idx < len) {
char nc = text[end.idx];
char sgn = text[end.idx + 1];
if ((tolower(nc) == 'e' || tolower(nc) == 'p') &&
(sgn == '+' || sgn == '-')) {
// need to check if it is an exponent
next_col(&end);
next_col(&end);
} else if (is_preproc_number_char(nc)) {
next_col(&end);
} else {
token->ty = PREPROC_TOKEN_TY_PREPROC_NUMBER;
token->span = (struct text_span){.start = start, .end = end};
token->text = (ustr_t){.str = &text[start.idx],
.len = text_span_len(&token->span)};
preproc_text->pos = end;
return;