-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSDL_shader_preprocessor.c
2368 lines (2012 loc) · 72.6 KB
/
SDL_shader_preprocessor.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
/**
* SDL_shader_tools; tools for SDL GPU shader support.
*
* Please see the file LICENSE.txt in the source's root directory.
*/
#define __SDL_SHADER_INTERNAL__ 1
#include "SDL_shader_internal.h"
/* !!! FIXME: replace printf debugging with SDL_Log? */
#if DEBUG_PREPROCESSOR
#define print_debug_token(token, len, val) \
SDL_SHADER_print_debug_token("PREPROCESSOR", token, len, val)
#else
#define print_debug_token(token, len, val)
#endif
#if DEBUG_LEXER
static Token debug_preprocessor_lexer(IncludeState *s)
{
const Token retval = preprocessor_lexer(s);
SDL_SHADER_print_debug_token("LEXER", s->token, s->tokenlen, retval);
return retval;
}
#define preprocessor_lexer(s) debug_preprocessor_lexer(s)
#endif
#if DEBUG_TOKENIZER
static void print_debug_lexing_position(const Context *ctx)
{
printf("NOW LEXING %s:%d ...\n", ctx->filename, (int) ctx->position);
}
#else
#define print_debug_lexing_position(s)
#endif
#if DEBUG_TOKENIZER
void SDL_SHADER_print_debug_token(const char *subsystem, const char *token,
const size_t tokenlen, const Token tokenval)
{
printf("%s TOKEN: \"", subsystem);
size_t i;
for (i = 0; i < tokenlen; i++) {
if (token[i] == '\n') {
printf("\\n");
} else if (token[i] == '\\') {
printf("\\\\");
} else {
printf("%c", token[i]);
}
}
printf("\" (");
switch (tokenval) {
#define TOKENCASE(x) case x: printf("%s", #x); break
TOKENCASE(TOKEN_UNKNOWN);
TOKENCASE(TOKEN_IDENTIFIER);
TOKENCASE(TOKEN_INT_LITERAL);
TOKENCASE(TOKEN_FLOAT_LITERAL);
TOKENCASE(TOKEN_STRING_LITERAL);
TOKENCASE(TOKEN_ADDASSIGN);
TOKENCASE(TOKEN_SUBASSIGN);
TOKENCASE(TOKEN_MULTASSIGN);
TOKENCASE(TOKEN_DIVASSIGN);
TOKENCASE(TOKEN_MODASSIGN);
TOKENCASE(TOKEN_XORASSIGN);
TOKENCASE(TOKEN_ANDASSIGN);
TOKENCASE(TOKEN_ORASSIGN);
TOKENCASE(TOKEN_INCREMENT);
TOKENCASE(TOKEN_DECREMENT);
TOKENCASE(TOKEN_RSHIFT);
TOKENCASE(TOKEN_LSHIFT);
TOKENCASE(TOKEN_ANDAND);
TOKENCASE(TOKEN_OROR);
TOKENCASE(TOKEN_LEQ);
TOKENCASE(TOKEN_GEQ);
TOKENCASE(TOKEN_EQL);
TOKENCASE(TOKEN_NEQ);
TOKENCASE(TOKEN_HASH);
TOKENCASE(TOKEN_HASHHASH);
TOKENCASE(TOKEN_PP_INCLUDE);
TOKENCASE(TOKEN_PP_LINE);
TOKENCASE(TOKEN_PP_DEFINE);
TOKENCASE(TOKEN_PP_UNDEF);
TOKENCASE(TOKEN_PP_IF);
TOKENCASE(TOKEN_PP_IFDEF);
TOKENCASE(TOKEN_PP_IFNDEF);
TOKENCASE(TOKEN_PP_ELSE);
TOKENCASE(TOKEN_PP_ELIF);
TOKENCASE(TOKEN_PP_ENDIF);
TOKENCASE(TOKEN_PP_ERROR);
TOKENCASE(TOKEN_PP_PRAGMA);
TOKENCASE(TOKEN_PP_BAD);
TOKENCASE(TOKEN_INCOMPLETE_STRING_LITERAL);
TOKENCASE(TOKEN_INCOMPLETE_COMMENT);
TOKENCASE(TOKEN_BAD_CHARS);
TOKENCASE(TOKEN_SINGLE_COMMENT);
TOKENCASE(TOKEN_MULTI_COMMENT);
TOKENCASE(TOKEN_EOI);
#undef TOKENCASE
case ((Token) '\n'):
printf("'\\n'");
break;
case ((Token) '\\'):
printf("'\\\\'");
break;
default:
SDL_assert(((int)tokenval) < 256);
printf("'%c'", (char) tokenval);
break;
}
printf(")\n");
}
#endif
static const char *attempt_include_open(const char *path, const char *fname,
const char **outdata, size_t *outbytes,
char *failstr, size_t failstrlen,
SDL_SHADER_Malloc m, SDL_SHADER_Free f, void *d)
{
char *ptr;
Sint64 flen;
SDL_RWops *io = NULL;
const size_t len = SDL_strlen(path) + SDL_strlen(fname) + 3;
char *fullpath = (char *) m(len, d);
if (fullpath == NULL) {
SDL_snprintf(failstr, failstrlen, "Out of memory");
return NULL;
}
*failstr = '\0';
SDL_snprintf(fullpath, len, "%s/%s", path, fname);
ptr = fullpath;
#if defined(__WINDOWS__) || defined(__OS2__)
while (SDL_TRUE) {
const char ch = *ptr;
if (ch == '\0') {
break;
} else if (ch == '/') {
*ptr = '\\';
}
ptr++;
}
#endif
io = SDL_RWFromFile(fullpath, "rb");
if (!io) {
f(fullpath, d);
return NULL; /* !!! FIXME: fill in failstr if permission denied, etc. Leave alone for not found. */
}
flen = SDL_RWsize(io);
if (flen < 0) {
SDL_snprintf(failstr, failstrlen, "Failed to get file length of '%s': %s", fullpath, SDL_GetError());
SDL_RWclose(io);
f(fullpath, d);
return NULL;
}
*outdata = m((size_t) flen, d);
if (!*outdata) {
SDL_snprintf(failstr, failstrlen, "Out of memory");
SDL_RWclose(io);
f(fullpath, d);
return NULL;
}
if (SDL_RWread(io, (char *) *outdata, (size_t) flen, 1) != 1) {
SDL_snprintf(failstr, failstrlen, "Failed to read '%s': %s", fullpath, SDL_GetError());
SDL_RWclose(io);
f((void *) *outdata, d);
*outdata = NULL;
f(fullpath, d);
return NULL;
}
SDL_RWclose(io);
*outbytes = (size_t) flen;
return fullpath;
}
static const char *internal_include_open(SDL_SHADER_IncludeType inctype,
const char *fname, const char *parent_fname,
const char *parent_data,
const char **outdata, size_t *outbytes,
const char **include_paths, size_t include_path_count,
char *failstr, size_t failstrlen,
SDL_SHADER_Malloc m, SDL_SHADER_Free f, void *d)
{
const char *rc = NULL;
size_t i;
if (parent_fname) {
const size_t slen = SDL_strlen(parent_fname) + 1;
char *ptr;
char *ptr2;
char *parent_dir = m(slen, d);
if (!parent_dir) {
SDL_snprintf(failstr, failstrlen, "Out of memory");
return NULL;
}
SDL_snprintf(parent_dir, slen, "%s", parent_fname);
ptr = SDL_strrchr(parent_dir, '/');
ptr2 = SDL_strrchr(ptr ? (ptr + 1) : parent_dir, '\\');
if (ptr2) {
ptr = ptr2;
}
if (!ptr) {
f(parent_dir, d);
} else {
if (ptr == parent_dir) {
ptr++; /* if this was going to turn "/absolute_path" into "", make it "/" instead. */
}
*ptr = '\0'; /* will open "parent_dir/fname" */
rc = attempt_include_open(parent_dir, fname, outdata, outbytes, failstr, failstrlen, m, f, d);
f(parent_dir, d);
if (rc != NULL) {
return rc;
} else if (*failstr != '\0') {
return NULL;
}
}
}
for (i = 0; i < include_path_count; i++) {
rc = attempt_include_open(include_paths[i], fname, outdata, outbytes, failstr, failstrlen, m, f, d);
if (rc != NULL) {
return rc;
} else if (*failstr != '\0') {
return NULL;
}
}
if (*failstr == '\0') {
SDL_snprintf(failstr, failstrlen, "%s: no such file or directory", fname);
}
return NULL;
}
static void internal_include_close(const char *data, SDL_SHADER_Malloc m, SDL_SHADER_Free f, void *d)
{
f((void *) data, d);
}
/* !!! FIXME: maybe use these pool magic elsewhere? */
/* !!! FIXME: maybe just get rid of this? (maybe the fragmentation isn't a big deal?) */
/* Pool stuff... */
/* ugh, I hate this macro salsa. */
#define FREE_POOL(type, poolname) \
static void free_##poolname##_pool(Context *ctx) { \
type *item = ctx->poolname##_pool; \
while (item != NULL) { \
type *next = item->next; \
Free(ctx, item); \
item = next; \
} \
}
#define GET_POOL(type, poolname) \
static type *get_##poolname(Context *ctx) { \
type *retval = ctx->poolname##_pool; \
if (retval != NULL) { \
ctx->poolname##_pool = retval->next; \
} else { \
retval = (type *) Malloc(ctx, sizeof (type)); \
} \
if (retval != NULL) { \
SDL_zerop(retval); \
} \
return retval; \
}
#define PUT_POOL(type, poolname) \
static void put_##poolname(Context *ctx, type *item) { \
item->next = ctx->poolname##_pool; \
ctx->poolname##_pool = item; \
}
#define IMPLEMENT_POOL(type, poolname) \
FREE_POOL(type, poolname) \
GET_POOL(type, poolname) \
PUT_POOL(type, poolname)
IMPLEMENT_POOL(Conditional, conditional)
IMPLEMENT_POOL(IncludeState, include)
IMPLEMENT_POOL(Define, define)
/* Preprocessor define hashtable stuff... */
/* !!! FIXME: why isn't this using SDL_shader_common.c's code? */
/* this is djb's xor hashing function. */
static inline Uint32 hash_string_djbxor(const char *sym)
{
Uint32 hash = 5381;
while (*sym) {
hash = ((hash << 5) + hash) ^ *(sym++);
}
return hash;
}
static inline Uint8 hash_define(const char *sym)
{
return (Uint8) hash_string_djbxor(sym);
}
static int add_define(Context *ctx, const char *sym, const char *val,
char **parameters, int paramcount)
{
const Uint8 hash = hash_define(sym);
Define *bucket = ctx->define_hashtable[hash];
while (bucket) {
if (SDL_strcmp(bucket->identifier, sym) == 0) {
warnf(ctx, "'%s' already defined", sym);
/* !!! FIXME: gcc reports the location of previous #define here. */
return 0;
}
bucket = bucket->next;
}
bucket = get_define(ctx);
if (bucket == NULL) {
return 0;
}
bucket->definition = val;
bucket->original = NULL;
bucket->identifier = sym;
bucket->parameters = (const char **) parameters;
bucket->paramcount = paramcount;
bucket->next = ctx->define_hashtable[hash];
ctx->define_hashtable[hash] = bucket;
return 1;
}
static void free_define(Context *ctx, Define *def)
{
if (def != NULL) {
int i;
for (i = 0; i < def->paramcount; i++) {
Free(ctx, (void *) def->parameters[i]);
}
Free(ctx, (void *) def->parameters);
Free(ctx, (void *) def->identifier);
Free(ctx, (void *) def->definition);
Free(ctx, (void *) def->original);
put_define(ctx, def);
}
}
static int remove_define(Context *ctx, const char *sym)
{
const Uint8 hash = hash_define(sym);
Define *bucket = ctx->define_hashtable[hash];
Define *prev = NULL;
while (bucket) {
if (SDL_strcmp(bucket->identifier, sym) == 0) {
if (prev == NULL) {
ctx->define_hashtable[hash] = bucket->next;
} else {
prev->next = bucket->next;
}
free_define(ctx, bucket);
return 1;
}
prev = bucket;
bucket = bucket->next;
}
return 0;
}
static const Define *find_define(Context *ctx, const char *sym)
{
const Uint8 filestrhash = 67;
const Uint8 linestrhash = 75;
const Uint8 hash = hash_define(sym);
Define *bucket = ctx->define_hashtable[hash];
while (bucket) {
if (SDL_strcmp(bucket->identifier, sym) == 0) {
return bucket;
}
bucket = bucket->next;
}
SDL_assert(hash_define("__FILE__") == filestrhash);
SDL_assert(hash_define("__LINE__") == linestrhash);
if ( (hash == filestrhash) && (ctx->file_macro) && (SDL_strcmp(sym, "__FILE__") == 0) ) {
const IncludeState *state = ctx->include_stack;
const char *fname = state ? state->filename : "";
const size_t len = SDL_strlen(fname) + 3;
char *str;
Free(ctx, (char *) ctx->file_macro->definition);
str = (char *) Malloc(ctx, len);
if (!str) {
return NULL;
}
str[0] = '\"';
SDL_memcpy(str + 1, fname, len - 3);
str[len - 2] = '\"';
str[len - 1] = '\0';
ctx->file_macro->definition = str;
return ctx->file_macro;
} else if ( (hash == linestrhash) && (ctx->line_macro) && (SDL_strcmp(sym, "__LINE__") == 0) ) {
const IncludeState *state = ctx->include_stack;
const size_t bufsize = 32;
char *str;
Free(ctx, (char *) ctx->line_macro->definition);
str = (char *) Malloc(ctx, bufsize);
if (!str) {
return 0;
}
const size_t len = SDL_snprintf(str, bufsize, "%u", state->line);
SDL_assert(len < bufsize);
ctx->line_macro->definition = str;
return ctx->line_macro;
}
return NULL;
}
static const Define *find_define_by_token(Context *ctx)
{
IncludeState *state = ctx->include_stack;
char *sym;
SDL_assert(state->tokenval == TOKEN_IDENTIFIER);
sym = (char *) alloca(state->tokenlen+1);
SDL_memcpy(sym, state->token, state->tokenlen);
sym[state->tokenlen] = '\0';
return find_define(ctx, sym);
}
static const Define *find_macro_arg(const IncludeState *state,
const Define *defines)
{
const Define *def = NULL;
char *sym = (char *) alloca(state->tokenlen + 1);
SDL_memcpy(sym, state->token, state->tokenlen);
sym[state->tokenlen] = '\0';
for (def = defines; def != NULL; def = def->next) {
SDL_assert(def->parameters == NULL); /* args can't have args! */
SDL_assert(def->paramcount == 0); /* args can't have args! */
if (SDL_strcmp(def->identifier, sym) == 0) {
break;
}
}
return def;
}
static void put_all_defines(Context *ctx)
{
size_t i;
for (i = 0; i < SDL_arraysize(ctx->define_hashtable); i++) {
Define *bucket = ctx->define_hashtable[i];
ctx->define_hashtable[i] = NULL;
while (bucket) {
Define *next = bucket->next;
free_define(ctx, bucket);
bucket = next;
}
}
}
static SDL_bool push_source(Context *ctx, const char *fname, const char *source, size_t srclen, Sint32 linenum, SDL_SHADER_IncludeClose close_callback)
{
IncludeState *state = get_include(ctx);
if (state == NULL) {
return SDL_FALSE;
}
/*printf("PUSH SOURCE [fname='%s' linenum=%u, source='%s']\n", fname, (uint) linenum, source);*/
if (fname != NULL) {
state->filename = stringcache(ctx->filename_cache, fname);
if (state->filename == NULL) {
put_include(ctx, state);
return SDL_FALSE;
}
}
state->close_callback = close_callback;
state->source_base = source;
state->source = source;
state->token = source;
state->tokenval = ((Token) '\n');
state->orig_length = srclen;
state->bytes_left = srclen;
state->line = linenum;
state->next = ctx->include_stack;
state->asm_comments = ctx->asm_comments;
if (ctx->include_stack) {
state->current_define = ctx->include_stack->current_define;
}
ctx->include_stack = state;
ctx->filename = state->filename;
ctx->position = linenum;
print_debug_lexing_position(ctx);
return SDL_TRUE;
}
static SDL_bool push_source_define(Context *ctx, const char *fname, const Define *def, Uint32 linenum)
{
const SDL_bool retval = push_source(ctx, fname, def->definition, SDL_strlen(def->definition), linenum, NULL);
if (retval) {
ctx->include_stack->current_define = def;
}
return retval;
}
static void pop_source(Context *ctx)
{
IncludeState *state = ctx->include_stack;
Conditional *cond;
/*printf("POP SOURCE\n");*/
SDL_assert(state != NULL); /* more pops than pushes! */
if (state == NULL) {
return;
}
if (state->close_callback) {
state->close_callback(state->source_base, ctx->malloc,
ctx->free, ctx->malloc_data);
}
/* state->filename is a pointer to the filename cache; don't free it here! */
cond = state->conditional_stack;
while (cond) {
Conditional *next = cond->next;
put_conditional(ctx, cond);
cond = next;
}
ctx->include_stack = state->next;
ctx->filename = ctx->include_stack ? ctx->include_stack->filename : NULL;
ctx->position = ctx->include_stack ? ctx->include_stack->line : 0;
print_debug_lexing_position(ctx);
put_include(ctx, state);
}
static void close_define_include(const char *data, SDL_SHADER_Malloc m,
SDL_SHADER_Free f, void *d)
{
f((void *) data, d);
}
SDL_bool preprocessor_start(Context *ctx, const SDL_SHADER_CompilerParams *params, SDL_bool asm_comments)
{
char *define_include = NULL;
size_t define_include_len = 0;
int okay = 1;
size_t i = 0;
if ((params->include_open == NULL) != (params->include_close == NULL)) {
return SDL_FALSE;
}
/* we don't own the *_include_paths arrays, don't free them. */
ctx->uses_preprocessor = SDL_TRUE;
ctx->allow_dotdot_includes = params->allow_dotdot_includes;
ctx->allow_absolute_includes = params->allow_absolute_includes;
ctx->system_include_paths = params->system_include_paths;
ctx->system_include_path_count = params->system_include_path_count;
ctx->local_include_paths = params->local_include_paths;
ctx->local_include_path_count = params->local_include_path_count;
ctx->open_callback = params->include_open ? params->include_open : internal_include_open;
ctx->close_callback = params->include_close ? params->include_close : internal_include_close;
ctx->asm_comments = asm_comments;
ctx->filename_cache = stringcache_create(MallocContextBridge, FreeContextBridge, ctx);
okay = ((okay) && (ctx->filename_cache != NULL));
ctx->file_macro = get_define(ctx);
okay = ((okay) && (ctx->file_macro != NULL));
if ((okay) && (ctx->file_macro)) {
okay = ((ctx->file_macro->identifier = StrDup(ctx, "__FILE__")) != 0);
}
ctx->line_macro = get_define(ctx);
okay = ((okay) && (ctx->line_macro != NULL));
if ((okay) && (ctx->line_macro)) {
okay = ((ctx->line_macro->identifier = StrDup(ctx, "__LINE__")) != 0);
}
/* let the usual preprocessor parser sort these out. */
if ((okay) && (params->define_count > 0)) {
Buffer *predefbuf = buffer_create(256, MallocContextBridge, FreeContextBridge, ctx);
okay = okay && (predefbuf != NULL);
for (i = 0; okay && (i < params->define_count); i++) {
okay = okay && buffer_append_fmt(predefbuf, "#define %s %s\n", params->defines[i].identifier, params->defines[i].definition);
}
define_include_len = buffer_size(predefbuf);
if (define_include_len > 0) {
define_include = buffer_flatten(predefbuf);
okay = okay && (define_include != NULL);
}
buffer_destroy(predefbuf);
}
if ((okay) && (!push_source(ctx, params->filename, params->source, params->sourcelen, 1, NULL))) {
okay = 0;
}
if ((okay) && (define_include_len > 0)) {
SDL_assert(define_include != NULL);
okay = push_source(ctx, "<predefined macros>", define_include, define_include_len, SDL_SHADER_POSITION_BEFORE, close_define_include);
}
if (!okay) {
return SDL_FALSE;
}
return SDL_TRUE;
}
void preprocessor_end(Context *ctx)
{
if (!ctx || !ctx->uses_preprocessor) {
return;
}
while (ctx->include_stack != NULL) {
pop_source(ctx);
}
put_all_defines(ctx);
if (ctx->filename_cache != NULL) {
stringcache_destroy(ctx->filename_cache);
}
free_define(ctx, ctx->file_macro);
free_define(ctx, ctx->line_macro);
free_define_pool(ctx);
free_conditional_pool(ctx);
free_include_pool(ctx);
ctx->uses_preprocessor = SDL_FALSE;
}
static inline void pushback(IncludeState *state)
{
#if DEBUG_PREPROCESSOR
printf("PREPROCESSOR PUSHBACK\n");
#endif
SDL_assert(!state->pushedback);
state->pushedback = SDL_TRUE;
}
static Token lexer(IncludeState *state)
{
if (state->pushedback) {
state->pushedback = SDL_FALSE;
return state->tokenval;
}
return preprocessor_lexer(state);
}
/* !!! FIXME: parsing fails on preprocessor directives should skip rest of line. */
static int require_newline(IncludeState *state)
{
Token token;
while (SDL_TRUE) {
token = lexer(state);
if (token == ((Token) ' ')) {
continue; /* whitespace, keep looking */
} else if (token == TOKEN_MULTI_COMMENT) {
if (MemChr(state->token, '\n', state->tokenlen) != NULL) { /* there was a newline in the comment, it counts. */
pushback(state);
return 1;
}
continue;
} else if (token == TOKEN_SINGLE_COMMENT) { /* should drop a newline next call. */
continue;
} else {
break;
}
}
pushback(state); /* rewind no matter what. */
return ( (token == TOKEN_INCOMPLETE_STRING_LITERAL) || /* call it an eol. */
(token == TOKEN_INCOMPLETE_COMMENT) || /* call it an eol. */
(token == ((Token) '\n')) || (token == TOKEN_EOI) );
}
/* !!! FIXME: didn't we implement this by hand elsewhere? */
static int token_to_int(IncludeState *state)
{
char *buf;
SDL_assert(state->tokenval == TOKEN_INT_LITERAL);
buf = (char *) alloca(state->tokenlen+1);
SDL_memcpy(buf, state->token, state->tokenlen);
buf[state->tokenlen] = '\0';
return SDL_atoi(buf);
}
static void handle_pp_include(Context *ctx)
{
char failstr[128];
IncludeState *state = ctx->include_stack;
Token token = lexer(state);
SDL_SHADER_IncludeType incltype;
const char *newdata = NULL;
size_t newbytes = 0;
char *filename = NULL;
const char *updated_filename = NULL;
int bogus = 0;
const char **include_paths = NULL;
size_t include_path_count = 0;
char *ptr = NULL;
if (token == TOKEN_STRING_LITERAL) {
incltype = SDL_SHADER_INCLUDETYPE_LOCAL;
include_paths = ctx->local_include_paths;
include_path_count = ctx->local_include_path_count;
} else if (token == ((Token) '<')) {
incltype = SDL_SHADER_INCLUDETYPE_SYSTEM;
include_paths = ctx->system_include_paths;
include_path_count = ctx->system_include_path_count;
/* can't use lexer, since every byte between the < > pair is considered part of the filename. :/ */
while (!bogus) {
if ( !(bogus = (state->bytes_left == 0)) ) {
const char ch = *state->source;
if ( !(bogus = ((ch == '\r') || (ch == '\n'))) ) {
state->source++;
state->bytes_left--;
if (ch == '>') {
break;
}
}
}
}
} else {
bogus = 1;
}
if (!bogus) {
size_t len;
state->token++; /* skip '<' or '\"'... */
len = (size_t) (state->source - state->token);
filename = (char *) alloca(len); /* !!! FIXME: maybe don't alloca this. */
SDL_memcpy(filename, state->token, len-1);
filename[len-1] = '\0';
bogus = !require_newline(state);
}
if (bogus) {
fail(ctx, "Invalid #include directive");
return;
}
ptr = filename;
while (SDL_TRUE) {
const char ch = *(ptr++);
if (ch == '\0') {
break;
} else if (ch == '\\') {
fail(ctx, "'\\' characters in #include directives are forbidden (use '/' instead)"); /* sorry, Windows. */
return;
}
}
if (!ctx->allow_absolute_includes) {
if (*filename == '/') {
fail(ctx, "Absolute paths in #include directives are forbidden");
return;
}
}
if (!ctx->allow_dotdot_includes) {
ptr = filename;
while (SDL_TRUE) {
const char ch = *(ptr++);
if (ch == '\0') {
break;
} else if ((ch == '/') && ((ptr[0] == '.') && (ptr[1] == '.') && ((ptr[2] == '/') || (ptr[2] == '\0')))) {
fail(ctx, "'..' paths in #include directives are forbidden");
return;
}
}
}
/* We _should_ have provided internal implementations in this case. */
SDL_assert(ctx->open_callback != NULL);
SDL_assert(ctx->close_callback != NULL);
failstr[0] = '\0';
updated_filename = ctx->open_callback(incltype, filename, state->filename, state->source_base,
&newdata, &newbytes, include_paths, include_path_count,
failstr, sizeof (failstr), ctx->malloc, ctx->free, ctx->malloc_data);
if (!updated_filename) {
fail(ctx, failstr[0] ? failstr : "Include callback failed");
return;
}
if (!push_source(ctx, updated_filename, newdata, newbytes, 1, ctx->close_callback)) {
SDL_assert(ctx->out_of_memory);
ctx->close_callback(newdata, ctx->malloc, ctx->free, ctx->malloc_data);
}
if (updated_filename != filename) {
ctx->free((void *) updated_filename, ctx->malloc_data);
}
}
static void handle_pp_line(Context *ctx)
{
IncludeState *state = ctx->include_stack;
const char *filename = NULL;
int linenum = 0;
int bogus = 0;
if (lexer(state) != TOKEN_INT_LITERAL) {
bogus = 1;
} else {
linenum = token_to_int(state);
}
if (!bogus) {
Token t = lexer(state);
if (t == ((Token) '\n')) {
state->line = linenum;
return;
}
bogus = (t != TOKEN_STRING_LITERAL);
}
if (!bogus) {
state->token++; /* skip '\"'... */
filename = stringcache_len(ctx->filename_cache, state->token, state->tokenlen-1); /* may be NULL if stringcache() failed. */
if (!filename) {
return;
}
bogus = !require_newline(state);
}
if (bogus) {
fail(ctx, "Invalid #line directive");
} else {
ctx->filename = state->filename = filename; /* already stringcache'd. */
ctx->position = state->line = linenum;
}
}
static void handle_pp_error(Context *ctx)
{
IncludeState *state = ctx->include_stack;
const char *prefix = "#error";
const size_t prefixlen = SDL_strlen(prefix);
char failstr[256];
size_t avail = sizeof (failstr);
size_t cpy = 0;
SDL_bool done = SDL_FALSE;
char *ptr;
SDL_strlcpy(failstr, prefix, avail);
avail -= prefixlen + 1; /* +1 for the null terminator */
ptr = failstr + prefixlen;
state->report_whitespace = SDL_TRUE;
while (!done) {
const Token token = lexer(state);
switch (token) {
case ((Token) '\n'):
state->line--; /* make sure error is on the right line. */
/* fall through! */
case TOKEN_INCOMPLETE_STRING_LITERAL:
case TOKEN_INCOMPLETE_COMMENT:
case TOKEN_EOI:
pushback(state); /* move back so we catch this later. */
done = SDL_TRUE;
break;
case ((Token) ' '):
if (!avail) {
break;
}
*(ptr++) = ' ';
avail--;
break;
default:
cpy = SDL_min(avail, state->tokenlen);
if (cpy) {
SDL_memcpy(ptr, state->token, cpy);
}
ptr += cpy;
avail -= cpy;
break;
}
}
*ptr = '\0';
state->report_whitespace = SDL_FALSE;
fail(ctx, failstr);
}
static void handle_pp_bad(Context *ctx)
{
IncludeState *state = ctx->include_stack;
failf(ctx, "unknown directive \"%.*s\"", state->tokenlen, state->token);
}
static void handle_pp_define(Context *ctx)
{
static const char space = ' ';
Buffer *buffer = NULL;
IncludeState *state = ctx->include_stack;
SDL_bool done = SDL_FALSE;
SDL_bool hashhash_error = SDL_FALSE;
size_t buflen;
int params = 0;
char **idents = NULL;
char *definition = NULL;
char *sym;
if (lexer(state) != TOKEN_IDENTIFIER) {
fail(ctx, "Macro names must be identifiers");
return;
}
sym = (char *) Malloc(ctx, state->tokenlen+1);
if (sym == NULL) {
return;
}
SDL_memcpy(sym, state->token, state->tokenlen);
sym[state->tokenlen] = '\0';
if (SDL_strcmp(sym, "defined") == 0) {
Free(ctx, sym);
fail(ctx, "'defined' cannot be used as a macro name");
return;
}
/* Don't treat these symbols as special anymore if they get (re)#defined. */
if (SDL_strcmp(sym, "__FILE__") == 0) {
if (ctx->file_macro) {
warnf(ctx, "'%s' already defined", sym);
free_define(ctx, ctx->file_macro);
ctx->file_macro = NULL;
}
} else if (SDL_strcmp(sym, "__LINE__") == 0) {
if (ctx->line_macro) {
warnf(ctx, "'%s' already defined", sym);
free_define(ctx, ctx->line_macro);
ctx->line_macro = NULL;
}
}
/* #define a(b) is different than #define a (b) :( */
state->report_whitespace = SDL_TRUE;
lexer(state);
state->report_whitespace = SDL_FALSE;
if (state->tokenval == ((Token) ' ')) {
lexer(state); /* skip it. */
} else if (state->tokenval == ((Token) '(')) {
IncludeState saved;