-
Notifications
You must be signed in to change notification settings - Fork 1
/
tccpp.c
3128 lines (2948 loc) · 85.6 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
*/
#include "tcc.h"
/********************************************************/
/* global variables */
ST_DATA int tok_flags;
/* additional informations about token */
#define TOK_FLAG_BOL 0x0001 /* beginning of line before */
#define TOK_FLAG_BOF 0x0002 /* beginning of file before */
#define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
#define TOK_FLAG_EOF 0x0008 /* end of file */
ST_DATA int parse_flags;
#define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
#define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
#define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
token. line feed is also
returned at eof */
#define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
#define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
ST_DATA struct BufferedFile *file;
ST_DATA int ch, tok;
ST_DATA CValue tokc;
ST_DATA const int *macro_ptr;
ST_DATA CString tokcstr; /* current parsed string, if any */
/* display benchmark infos */
ST_DATA int total_lines;
ST_DATA int total_bytes;
ST_DATA int tok_ident;
ST_DATA TokenSym **table_ident;
/* ------------------------------------------------------------------------- */
static int *macro_ptr_allocated;
static const int *unget_saved_macro_ptr;
static int unget_saved_buffer[TOK_MAX_SIZE + 1];
static int unget_buffer_enabled;
static TokenSym *hash_ident[TOK_HASH_SIZE];
static char token_buf[STRING_MAX_SIZE + 1];
/* true if isid(c) || isnum(c) */
static unsigned char isidnum_table[256-CH_EOF];
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[] =
"<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
"-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
struct macro_level {
struct macro_level *prev;
const int *p;
};
ST_FUNC void next_nomacro(void);
static void next_nomacro_spc(void);
static void macro_subst(
TokenString *tok_str,
Sym **nested_list,
const int *macro_str,
struct macro_level **can_read_stream
);
ST_FUNC void skip(int c)
{
if (tok != c)
tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
next();
}
ST_FUNC void expect(const char *msg)
{
tcc_error("%s expected", msg);
}
/* ------------------------------------------------------------------------- */
/* CString handling */
static void cstr_realloc(CString *cstr, int new_size)
{
int size;
void *data;
size = cstr->size_allocated;
if (size == 0)
size = 8; /* no need to allocate a too small first string */
while (size < new_size)
size = size * 2;
data = tcc_realloc(cstr->data_allocated, size);
cstr->data_allocated = data;
cstr->size_allocated = size;
cstr->data = data;
}
/* add a byte */
ST_FUNC void cstr_ccat(CString *cstr, int ch)
{
int size;
size = cstr->size + 1;
if (size > cstr->size_allocated)
cstr_realloc(cstr, size);
((unsigned char *)cstr->data)[size - 1] = ch;
cstr->size = size;
}
ST_FUNC void cstr_cat(CString *cstr, const char *str)
{
int c;
for(;;) {
c = *str;
if (c == '\0')
break;
cstr_ccat(cstr, c);
str++;
}
}
/* add a wide char */
ST_FUNC void cstr_wccat(CString *cstr, int ch)
{
int size;
size = cstr->size + sizeof(nwchar_t);
if (size > cstr->size_allocated)
cstr_realloc(cstr, size);
*(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
cstr->size = size;
}
ST_FUNC void cstr_new(CString *cstr)
{
memset(cstr, 0, sizeof(CString));
}
/* free string and reset it to NULL */
ST_FUNC void cstr_free(CString *cstr)
{
tcc_free(cstr->data_allocated);
cstr_new(cstr);
}
/* reset string to empty */
ST_FUNC void cstr_reset(CString *cstr)
{
cstr->size = 0;
}
/* XXX: unicode ? */
static void add_char(CString *cstr, int c)
{
if (c == '\'' || c == '\"' || c == '\\') {
/* XXX: could be more precise if char or string */
cstr_ccat(cstr, '\\');
}
if (c >= 32 && c <= 126) {
cstr_ccat(cstr, c);
} else {
cstr_ccat(cstr, '\\');
if (c == '\n') {
cstr_ccat(cstr, 'n');
} else {
cstr_ccat(cstr, '0' + ((c >> 6) & 7));
cstr_ccat(cstr, '0' + ((c >> 3) & 7));
cstr_ccat(cstr, '0' + (c & 7));
}
}
}
/* ------------------------------------------------------------------------- */
/* allocate a new token */
static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
{
TokenSym *ts, **ptable;
int i;
if (tok_ident >= SYM_FIRST_ANOM)
tcc_error("memory full");
/* expand token table if needed */
i = tok_ident - TOK_IDENT;
if ((i % TOK_ALLOC_INCR) == 0) {
ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
table_ident = ptable;
}
ts = tcc_malloc(sizeof(TokenSym) + len);
table_ident[i] = ts;
ts->tok = 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) * 263 + (c))
/* find a token and add it if not found */
ST_FUNC TokenSym *tok_alloc(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 = &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(pts, str, len);
}
/* XXX: buffer overflow */
/* XXX: float tokens */
ST_FUNC char *get_tok_str(int v, CValue *cv)
{
static char buf[STRING_MAX_SIZE + 1];
static CString cstr_buf;
CString *cstr;
char *p;
int i, len;
/* NOTE: to go faster, we give a fixed buffer for small strings */
cstr_reset(&cstr_buf);
cstr_buf.data = buf;
cstr_buf.size_allocated = sizeof(buf);
p = buf;
switch(v) {
case TOK_CINT:
case TOK_CUINT:
/* XXX: not quite exact, but only useful for testing */
sprintf(p, "%u", cv->ui);
break;
case TOK_CLLONG:
case TOK_CULLONG:
/* XXX: not quite exact, but only useful for testing */
#ifdef _WIN32
sprintf(p, "%u", (unsigned)cv->ull);
#else
sprintf(p, "%Lu", cv->ull);
#endif
break;
case TOK_LCHAR:
cstr_ccat(&cstr_buf, 'L');
case TOK_CCHAR:
cstr_ccat(&cstr_buf, '\'');
add_char(&cstr_buf, cv->i);
cstr_ccat(&cstr_buf, '\'');
cstr_ccat(&cstr_buf, '\0');
break;
case TOK_PPNUM:
cstr = cv->cstr;
len = cstr->size - 1;
for(i=0;i<len;i++)
add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
cstr_ccat(&cstr_buf, '\0');
break;
case TOK_LSTR:
cstr_ccat(&cstr_buf, 'L');
case TOK_STR:
cstr = cv->cstr;
cstr_ccat(&cstr_buf, '\"');
if (v == TOK_STR) {
len = cstr->size - 1;
for(i=0;i<len;i++)
add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
} else {
len = (cstr->size / sizeof(nwchar_t)) - 1;
for(i=0;i<len;i++)
add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
}
cstr_ccat(&cstr_buf, '\"');
cstr_ccat(&cstr_buf, '\0');
break;
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, ">>=");
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 buf;
}
q += 3;
}
addv:
*p++ = v;
*p = '\0';
} else if (v < tok_ident) {
return 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 cstr_buf.data;
}
/* fill input buffer and peek next char */
static int tcc_peekc_slow(BufferedFile *bf)
{
int len;
/* only tries to read if really end of buffer */
if (bf->buf_ptr >= bf->buf_end) {
if (bf->fd != -1) {
#if defined(PARSE_DEBUG)
len = 8;
#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;
}
}
/* return the current character, handling end of block if necessary
(but not stray) */
ST_FUNC int handle_eob(void)
{
return tcc_peekc_slow(file);
}
/* read next char from current input file and handle end of input buffer */
ST_INLN void inp(void)
{
ch = *(++(file->buf_ptr));
/* end of buffer/file handling */
if (ch == CH_EOB)
ch = handle_eob();
}
/* handle '\[\r]\n' */
static int handle_stray_noerror(void)
{
while (ch == '\\') {
inp();
if (ch == '\n') {
file->line_num++;
inp();
} else if (ch == '\r') {
inp();
if (ch != '\n')
goto fail;
file->line_num++;
inp();
} else {
fail:
return 1;
}
}
return 0;
}
static void handle_stray(void)
{
if (handle_stray_noerror())
tcc_error("stray '\\' in program");
}
/* skip the stray and handle the \\n case. Output an error if
incorrect char after the stray */
static int handle_stray1(uint8_t *p)
{
int c;
if (p >= file->buf_end) {
file->buf_ptr = p;
c = handle_eob();
p = file->buf_ptr;
if (c == '\\')
goto parse_stray;
} else {
parse_stray:
file->buf_ptr = p;
ch = *p;
handle_stray();
p = 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 == '\\') {\
file->buf_ptr = p;\
c = handle_eob();\
p = file->buf_ptr;\
}\
}
/* handle the complicated stray case */
#define PEEKC(c, p)\
{\
p++;\
c = *p;\
if (c == '\\') {\
c = handle_stray1(p);\
p = 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 */
ST_FUNC void minp(void)
{
inp();
if (ch == '\\')
handle_stray();
}
/* single line C++ comments */
static uint8_t *parse_line_comment(uint8_t *p)
{
int c;
p++;
for(;;) {
c = *p;
redo:
if (c == '\n' || c == CH_EOF) {
break;
} else if (c == '\\') {
file->buf_ptr = p;
c = handle_eob();
p = file->buf_ptr;
if (c == '\\') {
PEEKC_EOB(c, p);
if (c == '\n') {
file->line_num++;
PEEKC_EOB(c, p);
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c == '\n') {
file->line_num++;
PEEKC_EOB(c, p);
}
}
} else {
goto redo;
}
} else {
p++;
}
}
return p;
}
/* C comments */
ST_FUNC uint8_t *parse_comment(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') {
file->line_num++;
p++;
} else if (c == '*') {
p++;
for(;;) {
c = *p;
if (c == '*') {
p++;
} else if (c == '/') {
goto end_of_comment;
} else if (c == '\\') {
file->buf_ptr = p;
c = handle_eob();
p = file->buf_ptr;
if (c == '\\') {
/* skip '\[\r]\n', otherwise just skip the stray */
while (c == '\\') {
PEEKC_EOB(c, p);
if (c == '\n') {
file->line_num++;
PEEKC_EOB(c, p);
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c == '\n') {
file->line_num++;
PEEKC_EOB(c, p);
}
} else {
goto after_star;
}
}
}
} else {
break;
}
}
after_star: ;
} else {
/* stray, eob or eof */
file->buf_ptr = p;
c = handle_eob();
p = file->buf_ptr;
if (c == CH_EOF) {
tcc_error("unexpected end of file in comment");
} else if (c == '\\') {
p++;
}
}
}
end_of_comment:
p++;
return p;
}
#define cinp minp
static inline void skip_spaces(void)
{
while (is_space(ch))
cinp();
}
static inline int check_space(int t, int *spc)
{
if (is_space(t)) {
if (*spc)
return 1;
*spc = 1;
} else
*spc = 0;
return 0;
}
/* parse a string without interpreting escapes */
static uint8_t *parse_pp_string(uint8_t *p,
int sep, CString *str)
{
int c;
p++;
for(;;) {
c = *p;
if (c == sep) {
break;
} else if (c == '\\') {
file->buf_ptr = p;
c = handle_eob();
p = file->buf_ptr;
if (c == CH_EOF) {
unterminated_string:
/* XXX: indicate line number of start of string */
tcc_error("missing terminating %c character", sep);
} else if (c == '\\') {
/* escape : just skip \[\r]\n */
PEEKC_EOB(c, p);
if (c == '\n') {
file->line_num++;
p++;
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c != '\n')
expect("'\n' after '\r'");
file->line_num++;
p++;
} else if (c == CH_EOF) {
goto unterminated_string;
} else {
if (str) {
cstr_ccat(str, '\\');
cstr_ccat(str, c);
}
p++;
}
}
} else if (c == '\n') {
file->line_num++;
goto add_char;
} else if (c == '\r') {
PEEKC_EOB(c, p);
if (c != '\n') {
if (str)
cstr_ccat(str, '\r');
} else {
file->line_num++;
goto add_char;
}
} else {
add_char:
if (str)
cstr_ccat(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(void)
{
int a, start_of_line, c, in_warn_or_error;
uint8_t *p;
p = 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':
file->line_num++;
p++;
goto redo_start;
case '\\':
file->buf_ptr = p;
c = handle_eob();
if (c == CH_EOF) {
expect("#endif");
} else if (c == '\\') {
ch = file->buf_ptr[0];
handle_stray_noerror();
}
p = file->buf_ptr;
goto redo_no_start;
/* skip strings */
case '\"':
case '\'':
if (in_warn_or_error)
goto _default;
p = parse_pp_string(p, c, NULL);
break;
/* skip comments */
case '/':
if (in_warn_or_error)
goto _default;
file->buf_ptr = p;
ch = *p;
minp();
p = file->buf_ptr;
if (ch == '*') {
p = parse_comment(p);
} else if (ch == '/') {
p = parse_line_comment(p);
}
break;
case '#':
p++;
if (start_of_line) {
file->buf_ptr = p;
next_nomacro();
p = file->buf_ptr;
if (a == 0 &&
(tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
goto the_end;
if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
a++;
else if (tok == TOK_ENDIF)
a--;
else if( tok == TOK_ERROR || tok == TOK_WARNING)
in_warn_or_error = 1;
else if (tok == TOK_LINEFEED)
goto redo_start;
}
break;
_default:
default:
p++;
break;
}
start_of_line = 0;
}
the_end: ;
file->buf_ptr = p;
}
/* ParseState handling */
/* XXX: currently, no include file info is stored. Thus, we cannot display
accurate messages if the function or data definition spans multiple
files */
/* save current parse state in 's' */
ST_FUNC void save_parse_state(ParseState *s)
{
s->line_num = file->line_num;
s->macro_ptr = macro_ptr;
s->tok = tok;
s->tokc = tokc;
}
/* restore parse state from 's' */
ST_FUNC void restore_parse_state(ParseState *s)
{
file->line_num = s->line_num;
macro_ptr = s->macro_ptr;
tok = s->tok;
tokc = s->tokc;
}
/* return the number of additional 'ints' necessary to store the
token */
static inline int tok_ext_size(int t)
{
switch(t) {
/* 4 bytes */
case TOK_CINT:
case TOK_CUINT:
case TOK_CCHAR:
case TOK_LCHAR:
case TOK_CFLOAT:
case TOK_LINENUM:
return 1;
case TOK_STR:
case TOK_LSTR:
case TOK_PPNUM:
tcc_error("unsupported token");
return 1;
case TOK_CDOUBLE:
case TOK_CLLONG:
case TOK_CULLONG:
return 2;
case TOK_CLDOUBLE:
return LDOUBLE_SIZE / 4;
default:
return 0;
}
}
/* token string handling */
ST_INLN void tok_str_new(TokenString *s)
{
s->str = NULL;
s->len = 0;
s->allocated_len = 0;
s->last_line_num = -1;
}
ST_FUNC void tok_str_free(int *str)
{
tcc_free(str);
}
static int *tok_str_realloc(TokenString *s)
{
int *str, len;
if (s->allocated_len == 0) {
len = 8;
} else {
len = s->allocated_len * 2;
}
str = tcc_realloc(s->str, len * sizeof(int));
s->allocated_len = len;
s->str = str;
return str;
}
ST_FUNC void tok_str_add(TokenString *s, int t)
{
int len, *str;
len = s->len;
str = s->str;
if (len >= s->allocated_len)
str = tok_str_realloc(s);
str[len++] = t;
s->len = len;
}
static void tok_str_add2(TokenString *s, int t, CValue *cv)
{
int len, *str;
len = s->len;
str = s->str;
/* allocate space for worst case */
if (len + TOK_MAX_SIZE > s->allocated_len)
str = tok_str_realloc(s);
str[len++] = t;
switch(t) {
case TOK_CINT:
case TOK_CUINT:
case TOK_CCHAR:
case TOK_LCHAR:
case TOK_CFLOAT:
case TOK_LINENUM:
str[len++] = cv->tab[0];
break;
case TOK_PPNUM:
case TOK_STR:
case TOK_LSTR:
{
int nb_words;
CString *cstr;
nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
while ((len + nb_words) > s->allocated_len)
str = tok_str_realloc(s);
cstr = (CString *)(str + len);
cstr->data = NULL;
cstr->size = cv->cstr->size;
cstr->data_allocated = NULL;
cstr->size_allocated = cstr->size;
memcpy((char *)cstr + sizeof(CString),
cv->cstr->data, cstr->size);
len += nb_words;
}
break;
case TOK_CDOUBLE:
case TOK_CLLONG:
case TOK_CULLONG:
#if LDOUBLE_SIZE == 8
case TOK_CLDOUBLE:
#endif
str[len++] = cv->tab[0];
str[len++] = cv->tab[1];
break;
#if LDOUBLE_SIZE == 12
case TOK_CLDOUBLE:
str[len++] = cv->tab[0];
str[len++] = cv->tab[1];
str[len++] = cv->tab[2];
#elif LDOUBLE_SIZE == 16
case TOK_CLDOUBLE:
str[len++] = cv->tab[0];
str[len++] = cv->tab[1];
str[len++] = cv->tab[2];
str[len++] = cv->tab[3];
#elif LDOUBLE_SIZE != 8
#error add long double size support
#endif
break;
default:
break;
}
s->len = len;
}
/* add the current parse token in token string 's' */
ST_FUNC void tok_str_add_tok(TokenString *s)
{
CValue cval;
/* save line number info */
if (file->line_num != s->last_line_num) {
s->last_line_num = file->line_num;
cval.i = s->last_line_num;
tok_str_add2(s, TOK_LINENUM, &cval);
}
tok_str_add2(s, tok, &tokc);
}
/* get a token from an integer array and increment pointer
accordingly. we code it as a macro to avoid pointer aliasing. */
static inline void TOK_GET(int *t, const int **pp, CValue *cv)
{
const int *p = *pp;
int n, *tab;
tab = cv->tab;
switch(*t = *p++) {
case TOK_CINT:
case TOK_CUINT:
case TOK_CCHAR:
case TOK_LCHAR:
case TOK_CFLOAT:
case TOK_LINENUM:
tab[0] = *p++;
break;
case TOK_STR:
case TOK_LSTR:
case TOK_PPNUM:
cv->cstr = (CString *)p;
cv->cstr->data = (char *)p + sizeof(CString);
p += (sizeof(CString) + cv->cstr->size + 3) >> 2;
break;
case TOK_CDOUBLE:
case TOK_CLLONG:
case TOK_CULLONG:
n = 2;
goto copy;
case TOK_CLDOUBLE:
#if LDOUBLE_SIZE == 16
n = 4;
#elif LDOUBLE_SIZE == 12
n = 3;
#elif LDOUBLE_SIZE == 8
n = 2;
#else
# error add long double size support
#endif
copy:
do
*tab++ = *p++;
while (--n);
break;
default:
break;
}
*pp = p;
}
static int macro_is_equal(const int *a, const int *b)