-
Notifications
You must be signed in to change notification settings - Fork 0
/
tccpp.c
4040 lines (3770 loc) · 117 KB
/
tccpp.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
/*
* TCC - Tiny C Compiler
*
* Copyright (c) 2001-2004 Fabrice Bellard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define USING_GLOBALS
#include "tcc.h"
/********************************************************/
/* global variables */
/* ------------------------------------------------------------------------- */
static void tok_print(TCCState *S, const char *msg, const int *str);
static const char tcc_keywords[] =
#define DEF(id, str) str "\0"
#include "tcctok.h"
#undef DEF
;
/* WARNING: the content of this string encodes token numbers */
static const unsigned char tok_two_chars[] =
/* outdated -- gr
"<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
"-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
*/{
'<','=', TOK_LE,
'>','=', TOK_GE,
'!','=', TOK_NE,
'&','&', TOK_LAND,
'|','|', TOK_LOR,
'+','+', TOK_INC,
'-','-', TOK_DEC,
'=','=', TOK_EQ,
'<','<', TOK_SHL,
'>','>', TOK_SAR,
'+','=', TOK_A_ADD,
'-','=', TOK_A_SUB,
'*','=', TOK_A_MUL,
'/','=', TOK_A_DIV,
'%','=', TOK_A_MOD,
'&','=', TOK_A_AND,
'^','=', TOK_A_XOR,
'|','=', TOK_A_OR,
'-','>', TOK_ARROW,
'.','.', TOK_TWODOTS,
'#','#', TOK_TWOSHARPS,
'#','#', TOK_PPJOIN,
0
};
static void next_nomacro(TCCState *S);
ST_FUNC void skip(TCCState *S, int c)
{
if (S->tok != c)
tcc_error(S, "'%c' expected (got \"%s\")", c, get_tok_str(S, S->tok, &S->tokc));
next(S);
}
ST_FUNC void expect(TCCState *S, const char *msg)
{
tcc_error(S, "%s expected", msg);
}
/* ------------------------------------------------------------------------- */
/* Custom allocator for tiny objects */
#define USE_TAL
#ifndef USE_TAL
#define tal_free(S, al, p) tcc_free(S, p)
#define tal_realloc(S, al, p, size) tcc_realloc(S, p, size)
#define tal_new(S, a, b, c)
#define tal_delete(S, a)
#else
#if !defined(MEM_DEBUG)
#define tal_free(S, al, p) tal_free_impl(S, al, p)
#define tal_realloc(S, al, p, size) tal_realloc_impl(S, &al, p, size)
#define TAL_DEBUG_PARAMS
#else
#define TAL_DEBUG 1
//#define TAL_INFO 1 /* collect and dump allocators stats */
#define tal_free(S, al, p) tal_free_impl(S, al, p, __FILE__, __LINE__)
#define tal_realloc(S, al, p, size) tal_realloc_impl(S, &al, p, size, __FILE__, __LINE__)
#define TAL_DEBUG_PARAMS , const char *file, int line
#define TAL_DEBUG_FILE_LEN 40
#endif
#define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */
#define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */
#define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */
#define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */
#define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */
#define CSTR_TAL_LIMIT 1024
typedef struct tal_header_t {
unsigned size;
#ifdef TAL_DEBUG
int line_num; /* negative line_num used for double free check */
char file_name[TAL_DEBUG_FILE_LEN + 1];
#endif
} tal_header_t;
/* ------------------------------------------------------------------------- */
static TinyAlloc *tal_new(TCCState *S, TinyAlloc **pal, unsigned limit, unsigned size)
{
TinyAlloc *al = tcc_mallocz(S, sizeof(TinyAlloc));
al->p = al->buffer = tcc_malloc(S, size);
al->limit = limit;
al->size = size;
if (pal) *pal = al;
return al;
}
static void tal_delete(TCCState *S, TinyAlloc *al)
{
TinyAlloc *next;
tail_call:
if (!al)
return;
#ifdef TAL_INFO
fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
(al->peak_p - al->buffer) * 100.0 / al->size);
#endif
#ifdef TAL_DEBUG
if (al->nb_allocs > 0) {
uint8_t *p;
fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",
al->nb_allocs, al->limit);
p = al->buffer;
while (p < al->p) {
tal_header_t *header = (tal_header_t *)p;
if (header->line_num > 0) {
fprintf(stderr, "%s:%d: chunk of %d bytes leaked\n",
header->file_name, header->line_num, header->size);
}
p += header->size + sizeof(tal_header_t);
}
#if MEM_DEBUG-0 == 2
exit(2);
#endif
}
#endif
next = al->next;
tcc_free(S, al->buffer);
tcc_free(S, al);
al = next;
goto tail_call;
}
static void tal_free_impl(TCCState *S, TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
{
if (!p)
return;
tail_call:
if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
#ifdef TAL_DEBUG
tal_header_t *header = (((tal_header_t *)p) - 1);
if (header->line_num < 0) {
fprintf(stderr, "%s:%d: TAL_DEBUG: double frees chunk from\n",
file, line);
fprintf(stderr, "%s:%d: %d bytes\n",
header->file_name, (int)-header->line_num, (int)header->size);
} else
header->line_num = -header->line_num;
#endif
al->nb_allocs--;
if (!al->nb_allocs)
al->p = al->buffer;
} else if (al->next) {
al = al->next;
goto tail_call;
}
else
tcc_free(S, p);
}
static void *tal_realloc_impl(TCCState *S, TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_PARAMS)
{
tal_header_t *header;
void *ret;
int is_own;
unsigned adj_size = (size + 3) & -4;
TinyAlloc *al = *pal;
tail_call:
is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
if ((!p || is_own) && size <= al->limit) {
if (al->p - al->buffer + adj_size + sizeof(tal_header_t) < al->size) {
header = (tal_header_t *)al->p;
header->size = adj_size;
#ifdef TAL_DEBUG
{ int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
header->file_name[TAL_DEBUG_FILE_LEN] = 0;
header->line_num = line; }
#endif
ret = al->p + sizeof(tal_header_t);
al->p += adj_size + sizeof(tal_header_t);
if (is_own) {
header = (((tal_header_t *)p) - 1);
if (p) memcpy(ret, p, header->size);
#ifdef TAL_DEBUG
header->line_num = -header->line_num;
#endif
} else {
al->nb_allocs++;
}
#ifdef TAL_INFO
if (al->nb_peak < al->nb_allocs)
al->nb_peak = al->nb_allocs;
if (al->peak_p < al->p)
al->peak_p = al->p;
al->nb_total++;
#endif
return ret;
} else if (is_own) {
al->nb_allocs--;
ret = tal_realloc(S, *pal, 0, size);
header = (((tal_header_t *)p) - 1);
if (p) memcpy(ret, p, header->size);
#ifdef TAL_DEBUG
header->line_num = -header->line_num;
#endif
return ret;
}
if (al->next) {
al = al->next;
} else {
TinyAlloc *bottom = al, *next = al->top ? al->top : al;
al = tal_new(S, pal, next->limit, next->size * 2);
al->next = next;
bottom->top = al;
}
goto tail_call;
}
if (is_own) {
al->nb_allocs--;
ret = tcc_malloc(S, size);
header = (((tal_header_t *)p) - 1);
if (p) memcpy(ret, p, header->size);
#ifdef TAL_DEBUG
header->line_num = -header->line_num;
#endif
} else if (al->next) {
al = al->next;
goto tail_call;
} else
ret = tcc_realloc(S, p, size);
#ifdef TAL_INFO
al->nb_missed++;
#endif
return ret;
}
#endif /* USE_TAL */
/* ------------------------------------------------------------------------- */
/* CString handling */
static void cstr_realloc(TCCState *S, CString *cstr, int new_size)
{
int size;
size = cstr->size_allocated;
if (size < 8)
size = 8; /* no need to allocate a too small first string */
while (size < new_size)
size = size * 2;
cstr->data = tcc_realloc(S, cstr->data, size);
cstr->size_allocated = size;
}
/* add a byte */
ST_INLN void cstr_ccat(TCCState *S, CString *cstr, int ch)
{
int size;
size = cstr->size + 1;
if (size > cstr->size_allocated)
cstr_realloc(S, cstr, size);
((unsigned char *)cstr->data)[size - 1] = ch;
cstr->size = size;
}
ST_INLN char *unicode_to_utf8 (char *b, uint32_t Uc)
{
if (Uc<0x80) *b++=Uc;
else if (Uc<0x800) *b++=192+Uc/64, *b++=128+Uc%64;
else if (Uc-0xd800u<0x800) return b;
else if (Uc<0x10000) *b++=224+Uc/4096, *b++=128+Uc/64%64, *b++=128+Uc%64;
else if (Uc<0x110000) *b++=240+Uc/262144, *b++=128+Uc/4096%64, *b++=128+Uc/64%64, *b++=128+Uc%64;
return b;
}
/* add a unicode character expanded into utf8 */
ST_INLN void cstr_u8cat(TCCState *S, CString *cstr, int ch)
{
char buf[4], *e;
e = unicode_to_utf8(buf, (uint32_t)ch);
cstr_cat(S, cstr, buf, e - buf);
}
ST_FUNC void cstr_cat(TCCState *S, CString *cstr, const char *str, int len)
{
int size;
if (len <= 0)
len = strlen(str) + 1 + len;
size = cstr->size + len;
if (size > cstr->size_allocated)
cstr_realloc(S, cstr, size);
memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
cstr->size = size;
}
/* add a wide char */
ST_FUNC void cstr_wccat(TCCState *S, CString *cstr, int ch)
{
int size;
size = cstr->size + sizeof(nwchar_t);
if (size > cstr->size_allocated)
cstr_realloc(S, cstr, size);
*(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
cstr->size = size;
}
ST_FUNC void cstr_new(TCCState *S, CString *cstr)
{
memset(cstr, 0, sizeof(CString));
}
/* free string and reset it to NULL */
ST_FUNC void cstr_free(TCCState *S, CString *cstr)
{
tcc_free(S, cstr->data);
cstr_new(S, cstr);
}
/* reset string to empty */
ST_FUNC void cstr_reset(CString *cstr)
{
cstr->size = 0;
}
ST_FUNC int cstr_vprintf(TCCState *S, CString *cstr, const char *fmt, va_list ap)
{
va_list v;
int len, size = 80;
for (;;) {
size += cstr->size;
if (size > cstr->size_allocated)
cstr_realloc(S, cstr, size);
size = cstr->size_allocated - cstr->size;
va_copy(v, ap);
len = vsnprintf((char*)cstr->data + cstr->size, size, fmt, v);
va_end(v);
if (len > 0 && len < size)
break;
size *= 2;
}
cstr->size += len;
return len;
}
ST_FUNC int cstr_printf(TCCState *S, CString *cstr, const char *fmt, ...)
{
va_list ap; int len;
va_start(ap, fmt);
len = cstr_vprintf(S, cstr, fmt, ap);
va_end(ap);
return len;
}
/* XXX: unicode ? */
static void add_char(TCCState *S, CString *cstr, int c)
{
if (c == '\'' || c == '\"' || c == '\\') {
/* XXX: could be more precise if char or string */
cstr_ccat(S, cstr, '\\');
}
if (c >= 32 && c <= 126) {
cstr_ccat(S, cstr, c);
} else {
cstr_ccat(S, cstr, '\\');
if (c == '\n') {
cstr_ccat(S, cstr, 'n');
} else {
cstr_ccat(S, cstr, '0' + ((c >> 6) & 7));
cstr_ccat(S, cstr, '0' + ((c >> 3) & 7));
cstr_ccat(S, cstr, '0' + (c & 7));
}
}
}
/* ------------------------------------------------------------------------- */
/* allocate a new token */
static TokenSym *tok_alloc_new(TCCState *S, TokenSym **pts, const char *str, int len)
{
TokenSym *ts, **ptable;
int i;
if (S->tok_ident >= SYM_FIRST_ANOM)
tcc_error(S, "memory full (symbols)");
/* expand token table if needed */
i = S->tok_ident - TOK_IDENT;
if ((i % TOK_ALLOC_INCR) == 0) {
ptable = tcc_realloc(S, S->tccpp_table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
S->tccpp_table_ident = ptable;
}
ts = tal_realloc(S, S->toksym_alloc, 0, sizeof(TokenSym) + len);
S->tccpp_table_ident[i] = ts;
ts->tok = S->tok_ident++;
ts->sym_define = NULL;
ts->sym_label = NULL;
ts->sym_struct = NULL;
ts->sym_identifier = NULL;
ts->len = len;
ts->hash_next = NULL;
memcpy(ts->str, str, len);
ts->str[len] = '\0';
*pts = ts;
return ts;
}
#define TOK_HASH_INIT 1
#define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
/* find a token and add it if not found */
ST_FUNC TokenSym *tok_alloc(TCCState *S, const char *str, int len)
{
TokenSym *ts, **pts;
int i;
unsigned int h;
h = TOK_HASH_INIT;
for(i=0;i<len;i++)
h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
h &= (TOK_HASH_SIZE - 1);
pts = &S->tccpp_hash_ident[h];
for(;;) {
ts = *pts;
if (!ts)
break;
if (ts->len == len && !memcmp(ts->str, str, len))
return ts;
pts = &(ts->hash_next);
}
return tok_alloc_new(S, pts, str, len);
}
ST_FUNC int tok_alloc_const(TCCState *S, const char *str)
{
return tok_alloc(S, str, strlen(str))->tok;
}
/* XXX: buffer overflow */
/* XXX: float tokens */
ST_FUNC const char *get_tok_str(TCCState *S, int v, CValue *cv)
{
char *p;
int i, len;
cstr_reset(&S->tccpp_cstr_buf);
p = S->tccpp_cstr_buf.data;
switch(v) {
case TOK_CINT:
case TOK_CUINT:
case TOK_CLONG:
case TOK_CULONG:
case TOK_CLLONG:
case TOK_CULLONG:
/* XXX: not quite exact, but only useful for testing */
#ifdef _WIN32
sprintf(p, "%u", (unsigned)cv->i);
#else
sprintf(p, "%llu", (unsigned long long)cv->i);
#endif
break;
case TOK_LCHAR:
cstr_ccat(S, &S->tccpp_cstr_buf, 'L');
case TOK_CCHAR:
cstr_ccat(S, &S->tccpp_cstr_buf, '\'');
add_char(S, &S->tccpp_cstr_buf, cv->i);
cstr_ccat(S, &S->tccpp_cstr_buf, '\'');
cstr_ccat(S, &S->tccpp_cstr_buf, '\0');
break;
case TOK_PPNUM:
case TOK_PPSTR:
return (char*)cv->str.data;
case TOK_LSTR:
cstr_ccat(S, &S->tccpp_cstr_buf, 'L');
case TOK_STR:
cstr_ccat(S, &S->tccpp_cstr_buf, '\"');
if (v == TOK_STR) {
len = cv->str.size - 1;
for(i=0;i<len;i++)
add_char(S, &S->tccpp_cstr_buf, ((unsigned char *)cv->str.data)[i]);
} else {
len = (cv->str.size / sizeof(nwchar_t)) - 1;
for(i=0;i<len;i++)
add_char(S, &S->tccpp_cstr_buf, ((nwchar_t *)cv->str.data)[i]);
}
cstr_ccat(S, &S->tccpp_cstr_buf, '\"');
cstr_ccat(S, &S->tccpp_cstr_buf, '\0');
break;
case TOK_CFLOAT:
cstr_cat(S, &S->tccpp_cstr_buf, "<float>", 0);
break;
case TOK_CDOUBLE:
cstr_cat(S, &S->tccpp_cstr_buf, "<double>", 0);
break;
case TOK_CLDOUBLE:
cstr_cat(S, &S->tccpp_cstr_buf, "<long double>", 0);
break;
case TOK_LINENUM:
cstr_cat(S, &S->tccpp_cstr_buf, "<linenumber>", 0);
break;
/* above tokens have value, the ones below don't */
case TOK_LT:
v = '<';
goto addv;
case TOK_GT:
v = '>';
goto addv;
case TOK_DOTS:
return strcpy(p, "...");
case TOK_A_SHL:
return strcpy(p, "<<=");
case TOK_A_SAR:
return strcpy(p, ">>=");
case TOK_EOF:
return strcpy(p, "<eof>");
default:
if (v < TOK_IDENT) {
/* search in two bytes table */
const unsigned char *q = tok_two_chars;
while (*q) {
if (q[2] == v) {
*p++ = q[0];
*p++ = q[1];
*p = '\0';
return S->tccpp_cstr_buf.data;
}
q += 3;
}
if (v >= 127) {
sprintf(S->tccpp_cstr_buf.data, "<%02x>", v);
return S->tccpp_cstr_buf.data;
}
addv:
*p++ = v;
case 0: /* nameless anonymous symbol */
*p = '\0';
} else if (v < S->tok_ident) {
return S->tccpp_table_ident[v - TOK_IDENT]->str;
} else if (v >= SYM_FIRST_ANOM) {
/* special name for anonymous symbol */
sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
} else {
/* should never happen */
return NULL;
}
break;
}
return S->tccpp_cstr_buf.data;
}
/* return the current character, handling end of block if necessary
(but not stray) */
static int handle_eob(TCCState *S)
{
BufferedFile *bf = S->tccpp_file;
int len;
/* only tries to read if really end of buffer */
if (bf->buf_ptr >= bf->buf_end) {
if (bf->fd >= 0) {
#if defined(PARSE_DEBUG)
len = 1;
#else
len = IO_BUF_SIZE;
#endif
len = read(bf->fd, bf->buffer, len);
if (len < 0)
len = 0;
} else {
len = 0;
}
total_bytes += len;
bf->buf_ptr = bf->buffer;
bf->buf_end = bf->buffer + len;
*bf->buf_end = CH_EOB;
}
if (bf->buf_ptr < bf->buf_end) {
return bf->buf_ptr[0];
} else {
bf->buf_ptr = bf->buf_end;
return CH_EOF;
}
}
/* read next char from current input file and handle end of input buffer */
static inline void inp(TCCState *S)
{
S->tccpp_ch = *(++(S->tccpp_file->buf_ptr));
/* end of buffer/file handling */
if (S->tccpp_ch == CH_EOB)
S->tccpp_ch = handle_eob(S);
}
/* handle '\[\r]\n' */
static int handle_stray_noerror(TCCState *S)
{
while (S->tccpp_ch == '\\') {
inp(S);
if (S->tccpp_ch == '\n') {
S->tccpp_file->line_num++;
inp(S);
} else if (S->tccpp_ch == '\r') {
inp(S);
if (S->tccpp_ch != '\n')
goto fail;
S->tccpp_file->line_num++;
inp(S);
} else {
fail:
return 1;
}
}
return 0;
}
static void handle_stray(TCCState *S)
{
if (handle_stray_noerror(S))
tcc_error(S, "stray '\\' in program");
}
/* skip the stray and handle the \\n case. Output an error if
incorrect char after the stray */
static int handle_stray1(TCCState *S, uint8_t *p)
{
int c;
S->tccpp_file->buf_ptr = p;
if (p >= S->tccpp_file->buf_end) {
c = handle_eob(S);
if (c != '\\')
return c;
p = S->tccpp_file->buf_ptr;
}
S->tccpp_ch = *p;
if (handle_stray_noerror(S)) {
if (!(S->tccpp_parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
tcc_error(S, "stray '\\' in program");
*--S->tccpp_file->buf_ptr = '\\';
}
p = S->tccpp_file->buf_ptr;
c = *p;
return c;
}
/* handle just the EOB case, but not stray */
#define PEEKC_EOB(c, p)\
{\
p++;\
c = *p;\
if (c == '\\') {\
S->tccpp_file->buf_ptr = p;\
c = handle_eob(S);\
p = S->tccpp_file->buf_ptr;\
}\
}
/* handle the complicated stray case */
#define PEEKC(c, p)\
{\
p++;\
c = *p;\
if (c == '\\') {\
c = handle_stray1(S, p);\
p = S->tccpp_file->buf_ptr;\
}\
}
/* input with '\[\r]\n' handling. Note that this function cannot
handle other characters after '\', so you cannot call it inside
strings or comments */
static void minp(TCCState *S)
{
inp(S);
if (S->tccpp_ch == '\\')
handle_stray(S);
}
/* single line C++ comments */
static uint8_t *parse_line_comment(TCCState *S, uint8_t *p)
{
int c;
p++;
for(;;) {
c = *p;
redo:
if (c == '\n' || c == CH_EOF) {
break;
} else if (c == '\\') {
S->tccpp_file->buf_ptr = p;
c = handle_eob(S);
p = S->tccpp_file->buf_ptr;
if (c == '\\') {
PEEKC_EOB(c, p);
if (c == '\n') {
S->tccpp_file->line_num++;
PEEKC_EOB(c, p);
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c == '\n') {
S->tccpp_file->line_num++;
PEEKC_EOB(c, p);
}
}
} else {
goto redo;
}
} else {
p++;
}
}
return p;
}
/* C comments */
static uint8_t *parse_comment(TCCState *S, uint8_t *p)
{
int c;
p++;
for(;;) {
/* fast skip loop */
for(;;) {
c = *p;
if (c == '\n' || c == '*' || c == '\\')
break;
p++;
c = *p;
if (c == '\n' || c == '*' || c == '\\')
break;
p++;
}
/* now we can handle all the cases */
if (c == '\n') {
S->tccpp_file->line_num++;
p++;
} else if (c == '*') {
p++;
for(;;) {
c = *p;
if (c == '*') {
p++;
} else if (c == '/') {
goto end_of_comment;
} else if (c == '\\') {
S->tccpp_file->buf_ptr = p;
c = handle_eob(S);
p = S->tccpp_file->buf_ptr;
if (c == CH_EOF)
tcc_error(S, "unexpected end of file in comment");
if (c == '\\') {
/* skip '\[\r]\n', otherwise just skip the stray */
while (c == '\\') {
PEEKC_EOB(c, p);
if (c == '\n') {
S->tccpp_file->line_num++;
PEEKC_EOB(c, p);
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c == '\n') {
S->tccpp_file->line_num++;
PEEKC_EOB(c, p);
}
} else {
goto after_star;
}
}
}
} else {
break;
}
}
after_star: ;
} else {
/* stray, eob or eof */
S->tccpp_file->buf_ptr = p;
c = handle_eob(S);
p = S->tccpp_file->buf_ptr;
if (c == CH_EOF) {
tcc_error(S, "unexpected end of file in comment");
} else if (c == '\\') {
p++;
}
}
}
end_of_comment:
p++;
return p;
}
ST_FUNC int set_idnum(TCCState *S, int c, int val)
{
int prev = S->tccpp_isidnum_table[c - CH_EOF];
S->tccpp_isidnum_table[c - CH_EOF] = val;
return prev;
}
#define cinp minp
static inline void skip_spaces(TCCState *S)
{
while (S->tccpp_isidnum_table[S->tccpp_ch - CH_EOF] & IS_SPC)
cinp(S);
}
static inline int check_space(TCCState *S, int t, int *spc)
{
if (t < 256 && (S->tccpp_isidnum_table[t - CH_EOF] & IS_SPC)) {
if (*spc)
return 1;
*spc = 1;
} else
*spc = 0;
return 0;
}
/* parse a string without interpreting escapes */
static uint8_t *parse_pp_string(TCCState *S, uint8_t *p,
int sep, CString *str)
{
int c;
p++;
for(;;) {
c = *p;
if (c == sep) {
break;
} else if (c == '\\') {
S->tccpp_file->buf_ptr = p;
c = handle_eob(S);
p = S->tccpp_file->buf_ptr;
if (c == CH_EOF) {
unterminated_string:
/* XXX: indicate line number of start of string */
tcc_error(S, "missing terminating %c character", sep);
} else if (c == '\\') {
/* escape : just skip \[\r]\n */
PEEKC_EOB(c, p);
if (c == '\n') {
S->tccpp_file->line_num++;
p++;
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c != '\n')
expect(S, "'\n' after '\r'");
S->tccpp_file->line_num++;
p++;
} else if (c == CH_EOF) {
goto unterminated_string;
} else {
if (str) {
cstr_ccat(S, str, '\\');
cstr_ccat(S, str, c);
}
p++;
}
}
} else if (c == '\n') {
S->tccpp_file->line_num++;
goto add_char;
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c != '\n') {
if (str)
cstr_ccat(S, str, '\r');
} else {
S->tccpp_file->line_num++;
goto add_char;
}
} else {
add_char:
if (str)
cstr_ccat(S, str, c);
p++;
}
}
p++;
return p;
}
/* skip block of text until #else, #elif or #endif. skip also pairs of
#if/#endif */
static void preprocess_skip(TCCState *S)
{
int a, start_of_line, c, in_warn_or_error;
uint8_t *p;
p = S->tccpp_file->buf_ptr;
a = 0;
redo_start:
start_of_line = 1;
in_warn_or_error = 0;
for(;;) {
redo_no_start:
c = *p;
switch(c) {
case ' ':
case '\t':
case '\f':
case '\v':
case '\r':
p++;
goto redo_no_start;
case '\n':
S->tccpp_file->line_num++;
p++;
goto redo_start;
case '\\':
S->tccpp_file->buf_ptr = p;
c = handle_eob(S);
if (c == CH_EOF) {
expect(S, "#endif");
} else if (c == '\\') {
S->tccpp_ch = S->tccpp_file->buf_ptr[0];
handle_stray_noerror(S);
}
p = S->tccpp_file->buf_ptr;
goto redo_no_start;
/* skip strings */
case '\"':
case '\'':
if (in_warn_or_error)
goto _default;
p = parse_pp_string(S, p, c, NULL);
break;
/* skip comments */
case '/':
if (in_warn_or_error)
goto _default;
S->tccpp_file->buf_ptr = p;
S->tccpp_ch = *p;
minp(S);
p = S->tccpp_file->buf_ptr;
if (S->tccpp_ch == '*') {
p = parse_comment(S, p);
} else if (S->tccpp_ch == '/') {
p = parse_line_comment(S, p);
}
break;
case '#':
p++;
if (start_of_line) {
S->tccpp_file->buf_ptr = p;
next_nomacro(S);
p = S->tccpp_file->buf_ptr;
if (a == 0 &&
(S->tok == TOK_ELSE || S->tok == TOK_ELIF || S->tok == TOK_ENDIF))
goto the_end;
if (S->tok == TOK_IF || S->tok == TOK_IFDEF || S->tok == TOK_IFNDEF)
a++;
else if (S->tok == TOK_ENDIF)
a--;
else if( S->tok == TOK_ERROR || S->tok == TOK_WARNING)
in_warn_or_error = 1;
else if (S->tok == TOK_LINEFEED)
goto redo_start;