1 | /*
|
---|
2 | * TCC - Tiny C Compiler
|
---|
3 | *
|
---|
4 | * Copyright (c) 2001-2004 Fabrice Bellard
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "tcc.h"
|
---|
22 |
|
---|
23 | /********************************************************/
|
---|
24 | /* global variables */
|
---|
25 |
|
---|
26 | ST_DATA int tok_flags;
|
---|
27 | ST_DATA int parse_flags;
|
---|
28 |
|
---|
29 | ST_DATA struct BufferedFile *file;
|
---|
30 | ST_DATA int ch, tok;
|
---|
31 | ST_DATA CValue tokc;
|
---|
32 | ST_DATA const int *macro_ptr;
|
---|
33 | ST_DATA CString tokcstr; /* current parsed string, if any */
|
---|
34 |
|
---|
35 | /* display benchmark infos */
|
---|
36 | ST_DATA int total_lines;
|
---|
37 | ST_DATA int total_bytes;
|
---|
38 | ST_DATA int tok_ident;
|
---|
39 | ST_DATA TokenSym **table_ident;
|
---|
40 |
|
---|
41 | /* ------------------------------------------------------------------------- */
|
---|
42 |
|
---|
43 | static TokenSym *hash_ident[TOK_HASH_SIZE];
|
---|
44 | static char token_buf[STRING_MAX_SIZE + 1];
|
---|
45 | static CString cstr_buf;
|
---|
46 | static CString macro_equal_buf;
|
---|
47 | static TokenString tokstr_buf;
|
---|
48 | static unsigned char isidnum_table[256 - CH_EOF];
|
---|
49 | static int pp_debug_tok, pp_debug_symv;
|
---|
50 | static int pp_once;
|
---|
51 | static int pp_expr;
|
---|
52 | static int pp_counter;
|
---|
53 | static void tok_print(const char *msg, const int *str);
|
---|
54 |
|
---|
55 | static struct TinyAlloc *toksym_alloc;
|
---|
56 | static struct TinyAlloc *tokstr_alloc;
|
---|
57 | static struct TinyAlloc *cstr_alloc;
|
---|
58 |
|
---|
59 | static TokenString *macro_stack;
|
---|
60 |
|
---|
61 | static const char tcc_keywords[] =
|
---|
62 | #define DEF(id, str) str "\0"
|
---|
63 | #include "tcctok.h"
|
---|
64 | #undef DEF
|
---|
65 | ;
|
---|
66 |
|
---|
67 | /* WARNING: the content of this string encodes token numbers */
|
---|
68 | static const unsigned char tok_two_chars[] =
|
---|
69 | /* outdated -- gr
|
---|
70 | "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253"
|
---|
71 | "-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
|
---|
72 | */{
|
---|
73 | '<','=', TOK_LE,
|
---|
74 | '>','=', TOK_GE,
|
---|
75 | '!','=', TOK_NE,
|
---|
76 | '&','&', TOK_LAND,
|
---|
77 | '|','|', TOK_LOR,
|
---|
78 | '+','+', TOK_INC,
|
---|
79 | '-','-', TOK_DEC,
|
---|
80 | '=','=', TOK_EQ,
|
---|
81 | '<','<', TOK_SHL,
|
---|
82 | '>','>', TOK_SAR,
|
---|
83 | '+','=', TOK_A_ADD,
|
---|
84 | '-','=', TOK_A_SUB,
|
---|
85 | '*','=', TOK_A_MUL,
|
---|
86 | '/','=', TOK_A_DIV,
|
---|
87 | '%','=', TOK_A_MOD,
|
---|
88 | '&','=', TOK_A_AND,
|
---|
89 | '^','=', TOK_A_XOR,
|
---|
90 | '|','=', TOK_A_OR,
|
---|
91 | '-','>', TOK_ARROW,
|
---|
92 | '.','.', TOK_TWODOTS,
|
---|
93 | '#','#', TOK_TWOSHARPS,
|
---|
94 | 0
|
---|
95 | };
|
---|
96 |
|
---|
97 | static void next_nomacro_spc(void);
|
---|
98 |
|
---|
99 | ST_FUNC void skip(int c)
|
---|
100 | {
|
---|
101 | if (tok != c)
|
---|
102 | tcc_error("'%c' expected (got \"%s\")", c, get_tok_str(tok, &tokc));
|
---|
103 | next();
|
---|
104 | }
|
---|
105 |
|
---|
106 | ST_FUNC void expect(const char *msg)
|
---|
107 | {
|
---|
108 | tcc_error("%s expected", msg);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* ------------------------------------------------------------------------- */
|
---|
112 | /* Custom allocator for tiny objects */
|
---|
113 |
|
---|
114 | //#define USE_TAL
|
---|
115 |
|
---|
116 | #ifndef USE_TAL
|
---|
117 | #define tal_free(al, p) tcc_free(p)
|
---|
118 | #define tal_realloc(al, p, size) tcc_realloc(p, size)
|
---|
119 | #define tal_new(a,b,c)
|
---|
120 | #define tal_delete(a)
|
---|
121 | #else
|
---|
122 | #if !defined(MEM_DEBUG)
|
---|
123 | #define tal_free(al, p) tal_free_impl(al, p)
|
---|
124 | #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
|
---|
125 | #define TAL_DEBUG_PARAMS
|
---|
126 | #else
|
---|
127 | #define TAL_DEBUG 1
|
---|
128 | //#define TAL_INFO 1 /* collect and dump allocators stats */
|
---|
129 | #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
|
---|
130 | #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
|
---|
131 | #define TAL_DEBUG_PARAMS , const char *file, int line
|
---|
132 | #define TAL_DEBUG_FILE_LEN 40
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | #define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */
|
---|
136 | #define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */
|
---|
137 | #define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */
|
---|
138 | #define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */
|
---|
139 | #define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */
|
---|
140 | #define CSTR_TAL_LIMIT 1024
|
---|
141 |
|
---|
142 | typedef struct TinyAlloc {
|
---|
143 | unsigned limit;
|
---|
144 | unsigned size;
|
---|
145 | uint8_t *buffer;
|
---|
146 | uint8_t *p;
|
---|
147 | unsigned nb_allocs;
|
---|
148 | struct TinyAlloc *next, *top;
|
---|
149 | #ifdef TAL_INFO
|
---|
150 | unsigned nb_peak;
|
---|
151 | unsigned nb_total;
|
---|
152 | unsigned nb_missed;
|
---|
153 | uint8_t *peak_p;
|
---|
154 | #endif
|
---|
155 | } TinyAlloc;
|
---|
156 |
|
---|
157 | typedef struct tal_header_t {
|
---|
158 | unsigned size;
|
---|
159 | #ifdef TAL_DEBUG
|
---|
160 | int line_num; /* negative line_num used for double free check */
|
---|
161 | char file_name[TAL_DEBUG_FILE_LEN + 1];
|
---|
162 | #endif
|
---|
163 | } tal_header_t;
|
---|
164 |
|
---|
165 | /* ------------------------------------------------------------------------- */
|
---|
166 |
|
---|
167 | static TinyAlloc *tal_new(TinyAlloc **pal, unsigned limit, unsigned size)
|
---|
168 | {
|
---|
169 | TinyAlloc *al = tcc_mallocz(sizeof(TinyAlloc));
|
---|
170 | al->p = al->buffer = tcc_malloc(size);
|
---|
171 | al->limit = limit;
|
---|
172 | al->size = size;
|
---|
173 | if (pal) *pal = al;
|
---|
174 | return al;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static void tal_delete(TinyAlloc *al)
|
---|
178 | {
|
---|
179 | TinyAlloc *next;
|
---|
180 |
|
---|
181 | tail_call:
|
---|
182 | if (!al)
|
---|
183 | return;
|
---|
184 | #ifdef TAL_INFO
|
---|
185 | fprintf(stderr, "limit=%5d, size=%5g MB, nb_peak=%6d, nb_total=%8d, nb_missed=%6d, usage=%5.1f%%\n",
|
---|
186 | al->limit, al->size / 1024.0 / 1024.0, al->nb_peak, al->nb_total, al->nb_missed,
|
---|
187 | (al->peak_p - al->buffer) * 100.0 / al->size);
|
---|
188 | #endif
|
---|
189 | #ifdef TAL_DEBUG
|
---|
190 | if (al->nb_allocs > 0) {
|
---|
191 | uint8_t *p;
|
---|
192 | fprintf(stderr, "TAL_DEBUG: memory leak %d chunk(s) (limit= %d)\n",
|
---|
193 | al->nb_allocs, al->limit);
|
---|
194 | p = al->buffer;
|
---|
195 | while (p < al->p) {
|
---|
196 | tal_header_t *header = (tal_header_t *)p;
|
---|
197 | if (header->line_num > 0) {
|
---|
198 | fprintf(stderr, "%s:%d: chunk of %d bytes leaked\n",
|
---|
199 | header->file_name, header->line_num, header->size);
|
---|
200 | }
|
---|
201 | p += header->size + sizeof(tal_header_t);
|
---|
202 | }
|
---|
203 | #if MEM_DEBUG-0 == 2
|
---|
204 | exit(2);
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 | #endif
|
---|
208 | next = al->next;
|
---|
209 | tcc_free(al->buffer);
|
---|
210 | tcc_free(al);
|
---|
211 | al = next;
|
---|
212 | goto tail_call;
|
---|
213 | }
|
---|
214 |
|
---|
215 | static void tal_free_impl(TinyAlloc *al, void *p TAL_DEBUG_PARAMS)
|
---|
216 | {
|
---|
217 | if (!p)
|
---|
218 | return;
|
---|
219 | tail_call:
|
---|
220 | if (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size) {
|
---|
221 | #ifdef TAL_DEBUG
|
---|
222 | tal_header_t *header = (((tal_header_t *)p) - 1);
|
---|
223 | if (header->line_num < 0) {
|
---|
224 | fprintf(stderr, "%s:%d: TAL_DEBUG: double frees chunk from\n",
|
---|
225 | file, line);
|
---|
226 | fprintf(stderr, "%s:%d: %d bytes\n",
|
---|
227 | header->file_name, (int)-header->line_num, (int)header->size);
|
---|
228 | } else
|
---|
229 | header->line_num = -header->line_num;
|
---|
230 | #endif
|
---|
231 | al->nb_allocs--;
|
---|
232 | if (!al->nb_allocs)
|
---|
233 | al->p = al->buffer;
|
---|
234 | } else if (al->next) {
|
---|
235 | al = al->next;
|
---|
236 | goto tail_call;
|
---|
237 | }
|
---|
238 | else
|
---|
239 | tcc_free(p);
|
---|
240 | }
|
---|
241 |
|
---|
242 | static void *tal_realloc_impl(TinyAlloc **pal, void *p, unsigned size TAL_DEBUG_PARAMS)
|
---|
243 | {
|
---|
244 | tal_header_t *header;
|
---|
245 | void *ret;
|
---|
246 | int is_own;
|
---|
247 | unsigned adj_size = (size + 3) & -4;
|
---|
248 | TinyAlloc *al = *pal;
|
---|
249 |
|
---|
250 | tail_call:
|
---|
251 | is_own = (al->buffer <= (uint8_t *)p && (uint8_t *)p < al->buffer + al->size);
|
---|
252 | if ((!p || is_own) && size <= al->limit) {
|
---|
253 | if (al->p + adj_size + sizeof(tal_header_t) < al->buffer + al->size) {
|
---|
254 | header = (tal_header_t *)al->p;
|
---|
255 | header->size = adj_size;
|
---|
256 | #ifdef TAL_DEBUG
|
---|
257 | { int ofs = strlen(file) - TAL_DEBUG_FILE_LEN;
|
---|
258 | strncpy(header->file_name, file + (ofs > 0 ? ofs : 0), TAL_DEBUG_FILE_LEN);
|
---|
259 | header->file_name[TAL_DEBUG_FILE_LEN] = 0;
|
---|
260 | header->line_num = line; }
|
---|
261 | #endif
|
---|
262 | ret = al->p + sizeof(tal_header_t);
|
---|
263 | al->p += adj_size + sizeof(tal_header_t);
|
---|
264 | if (is_own) {
|
---|
265 | header = (((tal_header_t *)p) - 1);
|
---|
266 | memcpy(ret, p, header->size);
|
---|
267 | #ifdef TAL_DEBUG
|
---|
268 | header->line_num = -header->line_num;
|
---|
269 | #endif
|
---|
270 | } else {
|
---|
271 | al->nb_allocs++;
|
---|
272 | }
|
---|
273 | #ifdef TAL_INFO
|
---|
274 | if (al->nb_peak < al->nb_allocs)
|
---|
275 | al->nb_peak = al->nb_allocs;
|
---|
276 | if (al->peak_p < al->p)
|
---|
277 | al->peak_p = al->p;
|
---|
278 | al->nb_total++;
|
---|
279 | #endif
|
---|
280 | return ret;
|
---|
281 | } else if (is_own) {
|
---|
282 | al->nb_allocs--;
|
---|
283 | ret = tal_realloc(*pal, 0, size);
|
---|
284 | header = (((tal_header_t *)p) - 1);
|
---|
285 | memcpy(ret, p, header->size);
|
---|
286 | #ifdef TAL_DEBUG
|
---|
287 | header->line_num = -header->line_num;
|
---|
288 | #endif
|
---|
289 | return ret;
|
---|
290 | }
|
---|
291 | if (al->next) {
|
---|
292 | al = al->next;
|
---|
293 | } else {
|
---|
294 | TinyAlloc *bottom = al, *next = al->top ? al->top : al;
|
---|
295 |
|
---|
296 | al = tal_new(pal, next->limit, next->size * 2);
|
---|
297 | al->next = next;
|
---|
298 | bottom->top = al;
|
---|
299 | }
|
---|
300 | goto tail_call;
|
---|
301 | }
|
---|
302 | if (is_own) {
|
---|
303 | al->nb_allocs--;
|
---|
304 | ret = tcc_malloc(size);
|
---|
305 | header = (((tal_header_t *)p) - 1);
|
---|
306 | memcpy(ret, p, header->size);
|
---|
307 | #ifdef TAL_DEBUG
|
---|
308 | header->line_num = -header->line_num;
|
---|
309 | #endif
|
---|
310 | } else if (al->next) {
|
---|
311 | al = al->next;
|
---|
312 | goto tail_call;
|
---|
313 | } else
|
---|
314 | ret = tcc_realloc(p, size);
|
---|
315 | #ifdef TAL_INFO
|
---|
316 | al->nb_missed++;
|
---|
317 | #endif
|
---|
318 | return ret;
|
---|
319 | }
|
---|
320 |
|
---|
321 | #endif /* USE_TAL */
|
---|
322 |
|
---|
323 | /* ------------------------------------------------------------------------- */
|
---|
324 | /* CString handling */
|
---|
325 | static void cstr_realloc(CString *cstr, int new_size)
|
---|
326 | {
|
---|
327 | int size;
|
---|
328 |
|
---|
329 | size = cstr->size_allocated;
|
---|
330 | if (size < 8)
|
---|
331 | size = 8; /* no need to allocate a too small first string */
|
---|
332 | while (size < new_size)
|
---|
333 | size = size * 2;
|
---|
334 | cstr->data = tal_realloc(cstr_alloc, cstr->data, size);
|
---|
335 | cstr->size_allocated = size;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /* add a byte */
|
---|
339 | ST_INLN void cstr_ccat(CString *cstr, int ch)
|
---|
340 | {
|
---|
341 | int size;
|
---|
342 | size = cstr->size + 1;
|
---|
343 | if (size > cstr->size_allocated)
|
---|
344 | cstr_realloc(cstr, size);
|
---|
345 | ((unsigned char *)cstr->data)[size - 1] = ch;
|
---|
346 | cstr->size = size;
|
---|
347 | }
|
---|
348 |
|
---|
349 | ST_FUNC void cstr_cat(CString *cstr, const char *str, int len)
|
---|
350 | {
|
---|
351 | int size;
|
---|
352 | if (len <= 0)
|
---|
353 | len = strlen(str) + 1 + len;
|
---|
354 | size = cstr->size + len;
|
---|
355 | if (size > cstr->size_allocated)
|
---|
356 | cstr_realloc(cstr, size);
|
---|
357 | memmove(((unsigned char *)cstr->data) + cstr->size, str, len);
|
---|
358 | cstr->size = size;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /* add a wide char */
|
---|
362 | ST_FUNC void cstr_wccat(CString *cstr, int ch)
|
---|
363 | {
|
---|
364 | int size;
|
---|
365 | size = cstr->size + sizeof(nwchar_t);
|
---|
366 | if (size > cstr->size_allocated)
|
---|
367 | cstr_realloc(cstr, size);
|
---|
368 | *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
|
---|
369 | cstr->size = size;
|
---|
370 | }
|
---|
371 |
|
---|
372 | ST_FUNC void cstr_new(CString *cstr)
|
---|
373 | {
|
---|
374 | memset(cstr, 0, sizeof(CString));
|
---|
375 | }
|
---|
376 |
|
---|
377 | /* free string and reset it to NULL */
|
---|
378 | ST_FUNC void cstr_free(CString *cstr)
|
---|
379 | {
|
---|
380 | tal_free(cstr_alloc, cstr->data);
|
---|
381 | cstr_new(cstr);
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* reset string to empty */
|
---|
385 | ST_FUNC void cstr_reset(CString *cstr)
|
---|
386 | {
|
---|
387 | cstr->size = 0;
|
---|
388 | }
|
---|
389 |
|
---|
390 | /* XXX: unicode ? */
|
---|
391 | static void add_char(CString *cstr, int c)
|
---|
392 | {
|
---|
393 | if (c == '\'' || c == '\"' || c == '\\') {
|
---|
394 | /* XXX: could be more precise if char or string */
|
---|
395 | cstr_ccat(cstr, '\\');
|
---|
396 | }
|
---|
397 | if (c >= 32 && c <= 126) {
|
---|
398 | cstr_ccat(cstr, c);
|
---|
399 | } else {
|
---|
400 | cstr_ccat(cstr, '\\');
|
---|
401 | if (c == '\n') {
|
---|
402 | cstr_ccat(cstr, 'n');
|
---|
403 | } else {
|
---|
404 | cstr_ccat(cstr, '0' + ((c >> 6) & 7));
|
---|
405 | cstr_ccat(cstr, '0' + ((c >> 3) & 7));
|
---|
406 | cstr_ccat(cstr, '0' + (c & 7));
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | /* ------------------------------------------------------------------------- */
|
---|
412 | /* allocate a new token */
|
---|
413 | static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
|
---|
414 | {
|
---|
415 | TokenSym *ts, **ptable;
|
---|
416 | int i;
|
---|
417 |
|
---|
418 | if (tok_ident >= SYM_FIRST_ANOM)
|
---|
419 | tcc_error("memory full (symbols)");
|
---|
420 |
|
---|
421 | /* expand token table if needed */
|
---|
422 | i = tok_ident - TOK_IDENT;
|
---|
423 | if ((i % TOK_ALLOC_INCR) == 0) {
|
---|
424 | ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
|
---|
425 | table_ident = ptable;
|
---|
426 | }
|
---|
427 |
|
---|
428 | ts = tal_realloc(toksym_alloc, 0, sizeof(TokenSym) + len);
|
---|
429 | table_ident[i] = ts;
|
---|
430 | ts->tok = tok_ident++;
|
---|
431 | ts->sym_define = NULL;
|
---|
432 | ts->sym_label = NULL;
|
---|
433 | ts->sym_struct = NULL;
|
---|
434 | ts->sym_identifier = NULL;
|
---|
435 | ts->len = len;
|
---|
436 | ts->hash_next = NULL;
|
---|
437 | memcpy(ts->str, str, len);
|
---|
438 | ts->str[len] = '\0';
|
---|
439 | *pts = ts;
|
---|
440 | return ts;
|
---|
441 | }
|
---|
442 |
|
---|
443 | #define TOK_HASH_INIT 1
|
---|
444 | #define TOK_HASH_FUNC(h, c) ((h) + ((h) << 5) + ((h) >> 27) + (c))
|
---|
445 |
|
---|
446 |
|
---|
447 | /* find a token and add it if not found */
|
---|
448 | ST_FUNC TokenSym *tok_alloc(const char *str, int len)
|
---|
449 | {
|
---|
450 | TokenSym *ts, **pts;
|
---|
451 | int i;
|
---|
452 | unsigned int h;
|
---|
453 |
|
---|
454 | h = TOK_HASH_INIT;
|
---|
455 | for(i=0;i<len;i++)
|
---|
456 | h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
|
---|
457 | h &= (TOK_HASH_SIZE - 1);
|
---|
458 |
|
---|
459 | pts = &hash_ident[h];
|
---|
460 | for(;;) {
|
---|
461 | ts = *pts;
|
---|
462 | if (!ts)
|
---|
463 | break;
|
---|
464 | if (ts->len == len && !memcmp(ts->str, str, len))
|
---|
465 | return ts;
|
---|
466 | pts = &(ts->hash_next);
|
---|
467 | }
|
---|
468 | return tok_alloc_new(pts, str, len);
|
---|
469 | }
|
---|
470 |
|
---|
471 | /* XXX: buffer overflow */
|
---|
472 | /* XXX: float tokens */
|
---|
473 | ST_FUNC const char *get_tok_str(int v, CValue *cv)
|
---|
474 | {
|
---|
475 | char *p;
|
---|
476 | int i, len;
|
---|
477 |
|
---|
478 | cstr_reset(&cstr_buf);
|
---|
479 | p = cstr_buf.data;
|
---|
480 |
|
---|
481 | switch(v) {
|
---|
482 | case TOK_CINT:
|
---|
483 | case TOK_CUINT:
|
---|
484 | case TOK_CLONG:
|
---|
485 | case TOK_CULONG:
|
---|
486 | case TOK_CLLONG:
|
---|
487 | case TOK_CULLONG:
|
---|
488 | /* XXX: not quite exact, but only useful for testing */
|
---|
489 | #ifdef _WIN32
|
---|
490 | sprintf(p, "%u", (unsigned)cv->i);
|
---|
491 | #else
|
---|
492 | sprintf(p, "%llu", (unsigned long long)cv->i);
|
---|
493 | #endif
|
---|
494 | break;
|
---|
495 | case TOK_LCHAR:
|
---|
496 | cstr_ccat(&cstr_buf, 'L');
|
---|
497 | case TOK_CCHAR:
|
---|
498 | cstr_ccat(&cstr_buf, '\'');
|
---|
499 | add_char(&cstr_buf, cv->i);
|
---|
500 | cstr_ccat(&cstr_buf, '\'');
|
---|
501 | cstr_ccat(&cstr_buf, '\0');
|
---|
502 | break;
|
---|
503 | case TOK_PPNUM:
|
---|
504 | case TOK_PPSTR:
|
---|
505 | return (char*)cv->str.data;
|
---|
506 | case TOK_LSTR:
|
---|
507 | cstr_ccat(&cstr_buf, 'L');
|
---|
508 | case TOK_STR:
|
---|
509 | cstr_ccat(&cstr_buf, '\"');
|
---|
510 | if (v == TOK_STR) {
|
---|
511 | len = cv->str.size - 1;
|
---|
512 | for(i=0;i<len;i++)
|
---|
513 | add_char(&cstr_buf, ((unsigned char *)cv->str.data)[i]);
|
---|
514 | } else {
|
---|
515 | len = (cv->str.size / sizeof(nwchar_t)) - 1;
|
---|
516 | for(i=0;i<len;i++)
|
---|
517 | add_char(&cstr_buf, ((nwchar_t *)cv->str.data)[i]);
|
---|
518 | }
|
---|
519 | cstr_ccat(&cstr_buf, '\"');
|
---|
520 | cstr_ccat(&cstr_buf, '\0');
|
---|
521 | break;
|
---|
522 |
|
---|
523 | case TOK_CFLOAT:
|
---|
524 | cstr_cat(&cstr_buf, "<float>", 0);
|
---|
525 | break;
|
---|
526 | case TOK_CDOUBLE:
|
---|
527 | cstr_cat(&cstr_buf, "<double>", 0);
|
---|
528 | break;
|
---|
529 | case TOK_CLDOUBLE:
|
---|
530 | cstr_cat(&cstr_buf, "<long double>", 0);
|
---|
531 | break;
|
---|
532 | case TOK_LINENUM:
|
---|
533 | cstr_cat(&cstr_buf, "<linenumber>", 0);
|
---|
534 | break;
|
---|
535 |
|
---|
536 | /* above tokens have value, the ones below don't */
|
---|
537 | case TOK_LT:
|
---|
538 | v = '<';
|
---|
539 | goto addv;
|
---|
540 | case TOK_GT:
|
---|
541 | v = '>';
|
---|
542 | goto addv;
|
---|
543 | case TOK_DOTS:
|
---|
544 | return strcpy(p, "...");
|
---|
545 | case TOK_A_SHL:
|
---|
546 | return strcpy(p, "<<=");
|
---|
547 | case TOK_A_SAR:
|
---|
548 | return strcpy(p, ">>=");
|
---|
549 | case TOK_EOF:
|
---|
550 | return strcpy(p, "<eof>");
|
---|
551 | default:
|
---|
552 | if (v < TOK_IDENT) {
|
---|
553 | /* search in two bytes table */
|
---|
554 | const unsigned char *q = tok_two_chars;
|
---|
555 | while (*q) {
|
---|
556 | if (q[2] == v) {
|
---|
557 | *p++ = q[0];
|
---|
558 | *p++ = q[1];
|
---|
559 | *p = '\0';
|
---|
560 | return cstr_buf.data;
|
---|
561 | }
|
---|
562 | q += 3;
|
---|
563 | }
|
---|
564 | if (v >= 127) {
|
---|
565 | sprintf(cstr_buf.data, "<%02x>", v);
|
---|
566 | return cstr_buf.data;
|
---|
567 | }
|
---|
568 | addv:
|
---|
569 | *p++ = v;
|
---|
570 | *p = '\0';
|
---|
571 | } else if (v < tok_ident) {
|
---|
572 | return table_ident[v - TOK_IDENT]->str;
|
---|
573 | } else if (v >= SYM_FIRST_ANOM) {
|
---|
574 | /* special name for anonymous symbol */
|
---|
575 | sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
|
---|
576 | } else {
|
---|
577 | /* should never happen */
|
---|
578 | return NULL;
|
---|
579 | }
|
---|
580 | break;
|
---|
581 | }
|
---|
582 | return cstr_buf.data;
|
---|
583 | }
|
---|
584 |
|
---|
585 | /* return the current character, handling end of block if necessary
|
---|
586 | (but not stray) */
|
---|
587 | ST_FUNC int handle_eob(void)
|
---|
588 | {
|
---|
589 | BufferedFile *bf = file;
|
---|
590 | int len;
|
---|
591 |
|
---|
592 | /* only tries to read if really end of buffer */
|
---|
593 | if (bf->buf_ptr >= bf->buf_end) {
|
---|
594 | if (bf->fd >= 0) {
|
---|
595 | #if defined(PARSE_DEBUG)
|
---|
596 | len = 1;
|
---|
597 | #else
|
---|
598 | len = IO_BUF_SIZE;
|
---|
599 | #endif
|
---|
600 | len = read(bf->fd, bf->buffer, len);
|
---|
601 | if (len < 0)
|
---|
602 | len = 0;
|
---|
603 | } else {
|
---|
604 | len = 0;
|
---|
605 | }
|
---|
606 | total_bytes += len;
|
---|
607 | bf->buf_ptr = bf->buffer;
|
---|
608 | bf->buf_end = bf->buffer + len;
|
---|
609 | *bf->buf_end = CH_EOB;
|
---|
610 | }
|
---|
611 | if (bf->buf_ptr < bf->buf_end) {
|
---|
612 | return bf->buf_ptr[0];
|
---|
613 | } else {
|
---|
614 | bf->buf_ptr = bf->buf_end;
|
---|
615 | return CH_EOF;
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | /* read next char from current input file and handle end of input buffer */
|
---|
620 | ST_INLN void inp(void)
|
---|
621 | {
|
---|
622 | ch = *(++(file->buf_ptr));
|
---|
623 | /* end of buffer/file handling */
|
---|
624 | if (ch == CH_EOB)
|
---|
625 | ch = handle_eob();
|
---|
626 | }
|
---|
627 |
|
---|
628 | /* handle '\[\r]\n' */
|
---|
629 | static int handle_stray_noerror(void)
|
---|
630 | {
|
---|
631 | while (ch == '\\') {
|
---|
632 | inp();
|
---|
633 | if (ch == '\n') {
|
---|
634 | file->line_num++;
|
---|
635 | inp();
|
---|
636 | } else if (ch == '\r') {
|
---|
637 | inp();
|
---|
638 | if (ch != '\n')
|
---|
639 | goto fail;
|
---|
640 | file->line_num++;
|
---|
641 | inp();
|
---|
642 | } else {
|
---|
643 | fail:
|
---|
644 | return 1;
|
---|
645 | }
|
---|
646 | }
|
---|
647 | return 0;
|
---|
648 | }
|
---|
649 |
|
---|
650 | static void handle_stray(void)
|
---|
651 | {
|
---|
652 | if (handle_stray_noerror())
|
---|
653 | tcc_error("stray '\\' in program");
|
---|
654 | }
|
---|
655 |
|
---|
656 | /* skip the stray and handle the \\n case. Output an error if
|
---|
657 | incorrect char after the stray */
|
---|
658 | static int handle_stray1(uint8_t *p)
|
---|
659 | {
|
---|
660 | int c;
|
---|
661 |
|
---|
662 | file->buf_ptr = p;
|
---|
663 | if (p >= file->buf_end) {
|
---|
664 | c = handle_eob();
|
---|
665 | if (c != '\\')
|
---|
666 | return c;
|
---|
667 | p = file->buf_ptr;
|
---|
668 | }
|
---|
669 | ch = *p;
|
---|
670 | if (handle_stray_noerror()) {
|
---|
671 | if (!(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
|
---|
672 | tcc_error("stray '\\' in program");
|
---|
673 | *--file->buf_ptr = '\\';
|
---|
674 | }
|
---|
675 | p = file->buf_ptr;
|
---|
676 | c = *p;
|
---|
677 | return c;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /* handle just the EOB case, but not stray */
|
---|
681 | #define PEEKC_EOB(c, p)\
|
---|
682 | {\
|
---|
683 | p++;\
|
---|
684 | c = *p;\
|
---|
685 | if (c == '\\') {\
|
---|
686 | file->buf_ptr = p;\
|
---|
687 | c = handle_eob();\
|
---|
688 | p = file->buf_ptr;\
|
---|
689 | }\
|
---|
690 | }
|
---|
691 |
|
---|
692 | /* handle the complicated stray case */
|
---|
693 | #define PEEKC(c, p)\
|
---|
694 | {\
|
---|
695 | p++;\
|
---|
696 | c = *p;\
|
---|
697 | if (c == '\\') {\
|
---|
698 | c = handle_stray1(p);\
|
---|
699 | p = file->buf_ptr;\
|
---|
700 | }\
|
---|
701 | }
|
---|
702 |
|
---|
703 | /* input with '\[\r]\n' handling. Note that this function cannot
|
---|
704 | handle other characters after '\', so you cannot call it inside
|
---|
705 | strings or comments */
|
---|
706 | ST_FUNC void minp(void)
|
---|
707 | {
|
---|
708 | inp();
|
---|
709 | if (ch == '\\')
|
---|
710 | handle_stray();
|
---|
711 | }
|
---|
712 |
|
---|
713 | /* single line C++ comments */
|
---|
714 | static uint8_t *parse_line_comment(uint8_t *p)
|
---|
715 | {
|
---|
716 | int c;
|
---|
717 |
|
---|
718 | p++;
|
---|
719 | for(;;) {
|
---|
720 | c = *p;
|
---|
721 | redo:
|
---|
722 | if (c == '\n' || c == CH_EOF) {
|
---|
723 | break;
|
---|
724 | } else if (c == '\\') {
|
---|
725 | file->buf_ptr = p;
|
---|
726 | c = handle_eob();
|
---|
727 | p = file->buf_ptr;
|
---|
728 | if (c == '\\') {
|
---|
729 | PEEKC_EOB(c, p);
|
---|
730 | if (c == '\n') {
|
---|
731 | file->line_num++;
|
---|
732 | PEEKC_EOB(c, p);
|
---|
733 | } else if (c == '\r') {
|
---|
734 | PEEKC_EOB(c, p);
|
---|
735 | if (c == '\n') {
|
---|
736 | file->line_num++;
|
---|
737 | PEEKC_EOB(c, p);
|
---|
738 | }
|
---|
739 | }
|
---|
740 | } else {
|
---|
741 | goto redo;
|
---|
742 | }
|
---|
743 | } else {
|
---|
744 | p++;
|
---|
745 | }
|
---|
746 | }
|
---|
747 | return p;
|
---|
748 | }
|
---|
749 |
|
---|
750 | /* C comments */
|
---|
751 | ST_FUNC uint8_t *parse_comment(uint8_t *p)
|
---|
752 | {
|
---|
753 | int c;
|
---|
754 |
|
---|
755 | p++;
|
---|
756 | for(;;) {
|
---|
757 | /* fast skip loop */
|
---|
758 | for(;;) {
|
---|
759 | c = *p;
|
---|
760 | if (c == '\n' || c == '*' || c == '\\')
|
---|
761 | break;
|
---|
762 | p++;
|
---|
763 | c = *p;
|
---|
764 | if (c == '\n' || c == '*' || c == '\\')
|
---|
765 | break;
|
---|
766 | p++;
|
---|
767 | }
|
---|
768 | /* now we can handle all the cases */
|
---|
769 | if (c == '\n') {
|
---|
770 | file->line_num++;
|
---|
771 | p++;
|
---|
772 | } else if (c == '*') {
|
---|
773 | p++;
|
---|
774 | for(;;) {
|
---|
775 | c = *p;
|
---|
776 | if (c == '*') {
|
---|
777 | p++;
|
---|
778 | } else if (c == '/') {
|
---|
779 | goto end_of_comment;
|
---|
780 | } else if (c == '\\') {
|
---|
781 | file->buf_ptr = p;
|
---|
782 | c = handle_eob();
|
---|
783 | p = file->buf_ptr;
|
---|
784 | if (c == CH_EOF)
|
---|
785 | tcc_error("unexpected end of file in comment");
|
---|
786 | if (c == '\\') {
|
---|
787 | /* skip '\[\r]\n', otherwise just skip the stray */
|
---|
788 | while (c == '\\') {
|
---|
789 | PEEKC_EOB(c, p);
|
---|
790 | if (c == '\n') {
|
---|
791 | file->line_num++;
|
---|
792 | PEEKC_EOB(c, p);
|
---|
793 | } else if (c == '\r') {
|
---|
794 | PEEKC_EOB(c, p);
|
---|
795 | if (c == '\n') {
|
---|
796 | file->line_num++;
|
---|
797 | PEEKC_EOB(c, p);
|
---|
798 | }
|
---|
799 | } else {
|
---|
800 | goto after_star;
|
---|
801 | }
|
---|
802 | }
|
---|
803 | }
|
---|
804 | } else {
|
---|
805 | break;
|
---|
806 | }
|
---|
807 | }
|
---|
808 | after_star: ;
|
---|
809 | } else {
|
---|
810 | /* stray, eob or eof */
|
---|
811 | file->buf_ptr = p;
|
---|
812 | c = handle_eob();
|
---|
813 | p = file->buf_ptr;
|
---|
814 | if (c == CH_EOF) {
|
---|
815 | tcc_error("unexpected end of file in comment");
|
---|
816 | } else if (c == '\\') {
|
---|
817 | p++;
|
---|
818 | }
|
---|
819 | }
|
---|
820 | }
|
---|
821 | end_of_comment:
|
---|
822 | p++;
|
---|
823 | return p;
|
---|
824 | }
|
---|
825 |
|
---|
826 | ST_FUNC int set_idnum(int c, int val)
|
---|
827 | {
|
---|
828 | int prev = isidnum_table[c - CH_EOF];
|
---|
829 | isidnum_table[c - CH_EOF] = val;
|
---|
830 | return prev;
|
---|
831 | }
|
---|
832 |
|
---|
833 | #define cinp minp
|
---|
834 |
|
---|
835 | static inline void skip_spaces(void)
|
---|
836 | {
|
---|
837 | while (isidnum_table[ch - CH_EOF] & IS_SPC)
|
---|
838 | cinp();
|
---|
839 | }
|
---|
840 |
|
---|
841 | static inline int check_space(int t, int *spc)
|
---|
842 | {
|
---|
843 | if (t < 256 && (isidnum_table[t - CH_EOF] & IS_SPC)) {
|
---|
844 | if (*spc)
|
---|
845 | return 1;
|
---|
846 | *spc = 1;
|
---|
847 | } else
|
---|
848 | *spc = 0;
|
---|
849 | return 0;
|
---|
850 | }
|
---|
851 |
|
---|
852 | /* parse a string without interpreting escapes */
|
---|
853 | static uint8_t *parse_pp_string(uint8_t *p,
|
---|
854 | int sep, CString *str)
|
---|
855 | {
|
---|
856 | int c;
|
---|
857 | p++;
|
---|
858 | for(;;) {
|
---|
859 | c = *p;
|
---|
860 | if (c == sep) {
|
---|
861 | break;
|
---|
862 | } else if (c == '\\') {
|
---|
863 | file->buf_ptr = p;
|
---|
864 | c = handle_eob();
|
---|
865 | p = file->buf_ptr;
|
---|
866 | if (c == CH_EOF) {
|
---|
867 | unterminated_string:
|
---|
868 | /* XXX: indicate line number of start of string */
|
---|
869 | tcc_error("missing terminating %c character", sep);
|
---|
870 | } else if (c == '\\') {
|
---|
871 | /* escape : just skip \[\r]\n */
|
---|
872 | PEEKC_EOB(c, p);
|
---|
873 | if (c == '\n') {
|
---|
874 | file->line_num++;
|
---|
875 | p++;
|
---|
876 | } else if (c == '\r') {
|
---|
877 | PEEKC_EOB(c, p);
|
---|
878 | if (c != '\n')
|
---|
879 | expect("'\n' after '\r'");
|
---|
880 | file->line_num++;
|
---|
881 | p++;
|
---|
882 | } else if (c == CH_EOF) {
|
---|
883 | goto unterminated_string;
|
---|
884 | } else {
|
---|
885 | if (str) {
|
---|
886 | cstr_ccat(str, '\\');
|
---|
887 | cstr_ccat(str, c);
|
---|
888 | }
|
---|
889 | p++;
|
---|
890 | }
|
---|
891 | }
|
---|
892 | } else if (c == '\n') {
|
---|
893 | file->line_num++;
|
---|
894 | goto add_char;
|
---|
895 | } else if (c == '\r') {
|
---|
896 | PEEKC_EOB(c, p);
|
---|
897 | if (c != '\n') {
|
---|
898 | if (str)
|
---|
899 | cstr_ccat(str, '\r');
|
---|
900 | } else {
|
---|
901 | file->line_num++;
|
---|
902 | goto add_char;
|
---|
903 | }
|
---|
904 | } else {
|
---|
905 | add_char:
|
---|
906 | if (str)
|
---|
907 | cstr_ccat(str, c);
|
---|
908 | p++;
|
---|
909 | }
|
---|
910 | }
|
---|
911 | p++;
|
---|
912 | return p;
|
---|
913 | }
|
---|
914 |
|
---|
915 | /* skip block of text until #else, #elif or #endif. skip also pairs of
|
---|
916 | #if/#endif */
|
---|
917 | static void preprocess_skip(void)
|
---|
918 | {
|
---|
919 | int a, start_of_line, c, in_warn_or_error;
|
---|
920 | uint8_t *p;
|
---|
921 |
|
---|
922 | p = file->buf_ptr;
|
---|
923 | a = 0;
|
---|
924 | redo_start:
|
---|
925 | start_of_line = 1;
|
---|
926 | in_warn_or_error = 0;
|
---|
927 | for(;;) {
|
---|
928 | redo_no_start:
|
---|
929 | c = *p;
|
---|
930 | switch(c) {
|
---|
931 | case ' ':
|
---|
932 | case '\t':
|
---|
933 | case '\f':
|
---|
934 | case '\v':
|
---|
935 | case '\r':
|
---|
936 | p++;
|
---|
937 | goto redo_no_start;
|
---|
938 | case '\n':
|
---|
939 | file->line_num++;
|
---|
940 | p++;
|
---|
941 | goto redo_start;
|
---|
942 | case '\\':
|
---|
943 | file->buf_ptr = p;
|
---|
944 | c = handle_eob();
|
---|
945 | if (c == CH_EOF) {
|
---|
946 | expect("#endif");
|
---|
947 | } else if (c == '\\') {
|
---|
948 | ch = file->buf_ptr[0];
|
---|
949 | handle_stray_noerror();
|
---|
950 | }
|
---|
951 | p = file->buf_ptr;
|
---|
952 | goto redo_no_start;
|
---|
953 | /* skip strings */
|
---|
954 | case '\"':
|
---|
955 | case '\'':
|
---|
956 | if (in_warn_or_error)
|
---|
957 | goto _default;
|
---|
958 | p = parse_pp_string(p, c, NULL);
|
---|
959 | break;
|
---|
960 | /* skip comments */
|
---|
961 | case '/':
|
---|
962 | if (in_warn_or_error)
|
---|
963 | goto _default;
|
---|
964 | file->buf_ptr = p;
|
---|
965 | ch = *p;
|
---|
966 | minp();
|
---|
967 | p = file->buf_ptr;
|
---|
968 | if (ch == '*') {
|
---|
969 | p = parse_comment(p);
|
---|
970 | } else if (ch == '/') {
|
---|
971 | p = parse_line_comment(p);
|
---|
972 | }
|
---|
973 | break;
|
---|
974 | case '#':
|
---|
975 | p++;
|
---|
976 | if (start_of_line) {
|
---|
977 | file->buf_ptr = p;
|
---|
978 | next_nomacro();
|
---|
979 | p = file->buf_ptr;
|
---|
980 | if (a == 0 &&
|
---|
981 | (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
|
---|
982 | goto the_end;
|
---|
983 | if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
|
---|
984 | a++;
|
---|
985 | else if (tok == TOK_ENDIF)
|
---|
986 | a--;
|
---|
987 | else if( tok == TOK_ERROR || tok == TOK_WARNING)
|
---|
988 | in_warn_or_error = 1;
|
---|
989 | else if (tok == TOK_LINEFEED)
|
---|
990 | goto redo_start;
|
---|
991 | else if (parse_flags & PARSE_FLAG_ASM_FILE)
|
---|
992 | p = parse_line_comment(p - 1);
|
---|
993 | } else if (parse_flags & PARSE_FLAG_ASM_FILE)
|
---|
994 | p = parse_line_comment(p - 1);
|
---|
995 | break;
|
---|
996 | _default:
|
---|
997 | default:
|
---|
998 | p++;
|
---|
999 | break;
|
---|
1000 | }
|
---|
1001 | start_of_line = 0;
|
---|
1002 | }
|
---|
1003 | the_end: ;
|
---|
1004 | file->buf_ptr = p;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | #if 0
|
---|
1008 | /* return the number of additional 'ints' necessary to store the
|
---|
1009 | token */
|
---|
1010 | static inline int tok_size(const int *p)
|
---|
1011 | {
|
---|
1012 | switch(*p) {
|
---|
1013 | /* 4 bytes */
|
---|
1014 | case TOK_CINT:
|
---|
1015 | case TOK_CUINT:
|
---|
1016 | case TOK_CCHAR:
|
---|
1017 | case TOK_LCHAR:
|
---|
1018 | case TOK_CFLOAT:
|
---|
1019 | case TOK_LINENUM:
|
---|
1020 | return 1 + 1;
|
---|
1021 | case TOK_STR:
|
---|
1022 | case TOK_LSTR:
|
---|
1023 | case TOK_PPNUM:
|
---|
1024 | case TOK_PPSTR:
|
---|
1025 | return 1 + ((sizeof(CString) + ((CString *)(p+1))->size + 3) >> 2);
|
---|
1026 | case TOK_CLONG:
|
---|
1027 | case TOK_CULONG:
|
---|
1028 | return 1 + LONG_SIZE / 4;
|
---|
1029 | case TOK_CDOUBLE:
|
---|
1030 | case TOK_CLLONG:
|
---|
1031 | case TOK_CULLONG:
|
---|
1032 | return 1 + 2;
|
---|
1033 | case TOK_CLDOUBLE:
|
---|
1034 | return 1 + LDOUBLE_SIZE / 4;
|
---|
1035 | default:
|
---|
1036 | return 1 + 0;
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 | #endif
|
---|
1040 |
|
---|
1041 | /* token string handling */
|
---|
1042 | ST_INLN void tok_str_new(TokenString *s)
|
---|
1043 | {
|
---|
1044 | s->str = NULL;
|
---|
1045 | s->len = s->lastlen = 0;
|
---|
1046 | s->allocated_len = 0;
|
---|
1047 | s->last_line_num = -1;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | ST_FUNC TokenString *tok_str_alloc(void)
|
---|
1051 | {
|
---|
1052 | TokenString *str = tal_realloc(tokstr_alloc, 0, sizeof *str);
|
---|
1053 | tok_str_new(str);
|
---|
1054 | return str;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | ST_FUNC int *tok_str_dup(TokenString *s)
|
---|
1058 | {
|
---|
1059 | int *str;
|
---|
1060 |
|
---|
1061 | str = tal_realloc(tokstr_alloc, 0, s->len * sizeof(int));
|
---|
1062 | memcpy(str, s->str, s->len * sizeof(int));
|
---|
1063 | return str;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | ST_FUNC void tok_str_free_str(int *str)
|
---|
1067 | {
|
---|
1068 | tal_free(tokstr_alloc, str);
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | ST_FUNC void tok_str_free(TokenString *str)
|
---|
1072 | {
|
---|
1073 | tok_str_free_str(str->str);
|
---|
1074 | tal_free(tokstr_alloc, str);
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | ST_FUNC int *tok_str_realloc(TokenString *s, int new_size)
|
---|
1078 | {
|
---|
1079 | int *str, size;
|
---|
1080 |
|
---|
1081 | size = s->allocated_len;
|
---|
1082 | if (size < 16)
|
---|
1083 | size = 16;
|
---|
1084 | while (size < new_size)
|
---|
1085 | size = size * 2;
|
---|
1086 | if (size > s->allocated_len) {
|
---|
1087 | str = tal_realloc(tokstr_alloc, s->str, size * sizeof(int));
|
---|
1088 | s->allocated_len = size;
|
---|
1089 | s->str = str;
|
---|
1090 | }
|
---|
1091 | return s->str;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | ST_FUNC void tok_str_add(TokenString *s, int t)
|
---|
1095 | {
|
---|
1096 | int len, *str;
|
---|
1097 |
|
---|
1098 | len = s->len;
|
---|
1099 | str = s->str;
|
---|
1100 | if (len >= s->allocated_len)
|
---|
1101 | str = tok_str_realloc(s, len + 1);
|
---|
1102 | str[len++] = t;
|
---|
1103 | s->len = len;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | ST_FUNC void begin_macro(TokenString *str, int alloc)
|
---|
1107 | {
|
---|
1108 | str->alloc = alloc;
|
---|
1109 | str->prev = macro_stack;
|
---|
1110 | str->prev_ptr = macro_ptr;
|
---|
1111 | str->save_line_num = file->line_num;
|
---|
1112 | macro_ptr = str->str;
|
---|
1113 | macro_stack = str;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | ST_FUNC void end_macro(void)
|
---|
1117 | {
|
---|
1118 | TokenString *str = macro_stack;
|
---|
1119 | macro_stack = str->prev;
|
---|
1120 | macro_ptr = str->prev_ptr;
|
---|
1121 | file->line_num = str->save_line_num;
|
---|
1122 | if (str->alloc == 2) {
|
---|
1123 | str->alloc = 3; /* just mark as finished */
|
---|
1124 | } else {
|
---|
1125 | tok_str_free(str);
|
---|
1126 | }
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | static void tok_str_add2(TokenString *s, int t, CValue *cv)
|
---|
1130 | {
|
---|
1131 | int len, *str;
|
---|
1132 |
|
---|
1133 | len = s->lastlen = s->len;
|
---|
1134 | str = s->str;
|
---|
1135 |
|
---|
1136 | /* allocate space for worst case */
|
---|
1137 | if (len + TOK_MAX_SIZE >= s->allocated_len)
|
---|
1138 | str = tok_str_realloc(s, len + TOK_MAX_SIZE + 1);
|
---|
1139 | str[len++] = t;
|
---|
1140 | switch(t) {
|
---|
1141 | case TOK_CINT:
|
---|
1142 | case TOK_CUINT:
|
---|
1143 | case TOK_CCHAR:
|
---|
1144 | case TOK_LCHAR:
|
---|
1145 | case TOK_CFLOAT:
|
---|
1146 | case TOK_LINENUM:
|
---|
1147 | #if LONG_SIZE == 4
|
---|
1148 | case TOK_CLONG:
|
---|
1149 | case TOK_CULONG:
|
---|
1150 | #endif
|
---|
1151 | str[len++] = cv->tab[0];
|
---|
1152 | break;
|
---|
1153 | case TOK_PPNUM:
|
---|
1154 | case TOK_PPSTR:
|
---|
1155 | case TOK_STR:
|
---|
1156 | case TOK_LSTR:
|
---|
1157 | {
|
---|
1158 | /* Insert the string into the int array. */
|
---|
1159 | size_t nb_words =
|
---|
1160 | 1 + (cv->str.size + sizeof(int) - 1) / sizeof(int);
|
---|
1161 | if (len + nb_words >= s->allocated_len)
|
---|
1162 | str = tok_str_realloc(s, len + nb_words + 1);
|
---|
1163 | str[len] = cv->str.size;
|
---|
1164 | memcpy(&str[len + 1], cv->str.data, cv->str.size);
|
---|
1165 | len += nb_words;
|
---|
1166 | }
|
---|
1167 | break;
|
---|
1168 | case TOK_CDOUBLE:
|
---|
1169 | case TOK_CLLONG:
|
---|
1170 | case TOK_CULLONG:
|
---|
1171 | #if LONG_SIZE == 8
|
---|
1172 | case TOK_CLONG:
|
---|
1173 | case TOK_CULONG:
|
---|
1174 | #endif
|
---|
1175 | #if LDOUBLE_SIZE == 8
|
---|
1176 | case TOK_CLDOUBLE:
|
---|
1177 | #endif
|
---|
1178 | str[len++] = cv->tab[0];
|
---|
1179 | str[len++] = cv->tab[1];
|
---|
1180 | break;
|
---|
1181 | #if LDOUBLE_SIZE == 12
|
---|
1182 | case TOK_CLDOUBLE:
|
---|
1183 | str[len++] = cv->tab[0];
|
---|
1184 | str[len++] = cv->tab[1];
|
---|
1185 | str[len++] = cv->tab[2];
|
---|
1186 | #elif LDOUBLE_SIZE == 16
|
---|
1187 | case TOK_CLDOUBLE:
|
---|
1188 | str[len++] = cv->tab[0];
|
---|
1189 | str[len++] = cv->tab[1];
|
---|
1190 | str[len++] = cv->tab[2];
|
---|
1191 | str[len++] = cv->tab[3];
|
---|
1192 | #elif LDOUBLE_SIZE != 8
|
---|
1193 | #error add long double size support
|
---|
1194 | #endif
|
---|
1195 | break;
|
---|
1196 | default:
|
---|
1197 | break;
|
---|
1198 | }
|
---|
1199 | s->len = len;
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | /* add the current parse token in token string 's' */
|
---|
1203 | ST_FUNC void tok_str_add_tok(TokenString *s)
|
---|
1204 | {
|
---|
1205 | CValue cval;
|
---|
1206 |
|
---|
1207 | /* save line number info */
|
---|
1208 | if (file->line_num != s->last_line_num) {
|
---|
1209 | s->last_line_num = file->line_num;
|
---|
1210 | cval.i = s->last_line_num;
|
---|
1211 | tok_str_add2(s, TOK_LINENUM, &cval);
|
---|
1212 | }
|
---|
1213 | tok_str_add2(s, tok, &tokc);
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | /* get a token from an integer array and increment pointer
|
---|
1217 | accordingly. we code it as a macro to avoid pointer aliasing. */
|
---|
1218 | static inline void TOK_GET(int *t, const int **pp, CValue *cv)
|
---|
1219 | {
|
---|
1220 | const int *p = *pp;
|
---|
1221 | int n, *tab;
|
---|
1222 |
|
---|
1223 | tab = cv->tab;
|
---|
1224 | switch(*t = *p++) {
|
---|
1225 | #if LONG_SIZE == 4
|
---|
1226 | case TOK_CLONG:
|
---|
1227 | #endif
|
---|
1228 | case TOK_CINT:
|
---|
1229 | case TOK_CCHAR:
|
---|
1230 | case TOK_LCHAR:
|
---|
1231 | case TOK_LINENUM:
|
---|
1232 | cv->i = *p++;
|
---|
1233 | break;
|
---|
1234 | #if LONG_SIZE == 4
|
---|
1235 | case TOK_CULONG:
|
---|
1236 | #endif
|
---|
1237 | case TOK_CUINT:
|
---|
1238 | cv->i = (unsigned)*p++;
|
---|
1239 | break;
|
---|
1240 | case TOK_CFLOAT:
|
---|
1241 | tab[0] = *p++;
|
---|
1242 | break;
|
---|
1243 | case TOK_STR:
|
---|
1244 | case TOK_LSTR:
|
---|
1245 | case TOK_PPNUM:
|
---|
1246 | case TOK_PPSTR:
|
---|
1247 | cv->str.size = *p++;
|
---|
1248 | cv->str.data = p;
|
---|
1249 | p += (cv->str.size + sizeof(int) - 1) / sizeof(int);
|
---|
1250 | break;
|
---|
1251 | case TOK_CDOUBLE:
|
---|
1252 | case TOK_CLLONG:
|
---|
1253 | case TOK_CULLONG:
|
---|
1254 | #if LONG_SIZE == 8
|
---|
1255 | case TOK_CLONG:
|
---|
1256 | case TOK_CULONG:
|
---|
1257 | #endif
|
---|
1258 | n = 2;
|
---|
1259 | goto copy;
|
---|
1260 | case TOK_CLDOUBLE:
|
---|
1261 | #if LDOUBLE_SIZE == 16
|
---|
1262 | n = 4;
|
---|
1263 | #elif LDOUBLE_SIZE == 12
|
---|
1264 | n = 3;
|
---|
1265 | #elif LDOUBLE_SIZE == 8
|
---|
1266 | n = 2;
|
---|
1267 | #else
|
---|
1268 | # error add long double size support
|
---|
1269 | #endif
|
---|
1270 | copy:
|
---|
1271 | do
|
---|
1272 | *tab++ = *p++;
|
---|
1273 | while (--n);
|
---|
1274 | break;
|
---|
1275 | default:
|
---|
1276 | break;
|
---|
1277 | }
|
---|
1278 | *pp = p;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | static int macro_is_equal(const int *a, const int *b)
|
---|
1282 | {
|
---|
1283 | CValue cv;
|
---|
1284 | int t;
|
---|
1285 |
|
---|
1286 | if (!a || !b)
|
---|
1287 | return 1;
|
---|
1288 |
|
---|
1289 | while (*a && *b) {
|
---|
1290 | /* first time preallocate macro_equal_buf, next time only reset position to start */
|
---|
1291 | cstr_reset(¯o_equal_buf);
|
---|
1292 | TOK_GET(&t, &a, &cv);
|
---|
1293 | cstr_cat(¯o_equal_buf, get_tok_str(t, &cv), 0);
|
---|
1294 | TOK_GET(&t, &b, &cv);
|
---|
1295 | if (strcmp(macro_equal_buf.data, get_tok_str(t, &cv)))
|
---|
1296 | return 0;
|
---|
1297 | }
|
---|
1298 | return !(*a || *b);
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /* defines handling */
|
---|
1302 | ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg)
|
---|
1303 | {
|
---|
1304 | Sym *s, *o;
|
---|
1305 |
|
---|
1306 | o = define_find(v);
|
---|
1307 | s = sym_push2(&define_stack, v, macro_type, 0);
|
---|
1308 | s->d = str;
|
---|
1309 | s->next = first_arg;
|
---|
1310 | table_ident[v - TOK_IDENT]->sym_define = s;
|
---|
1311 |
|
---|
1312 | if (o && !macro_is_equal(o->d, s->d))
|
---|
1313 | tcc_warning("%s redefined", get_tok_str(v, NULL));
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | /* undefined a define symbol. Its name is just set to zero */
|
---|
1317 | ST_FUNC void define_undef(Sym *s)
|
---|
1318 | {
|
---|
1319 | int v = s->v;
|
---|
1320 | if (v >= TOK_IDENT && v < tok_ident)
|
---|
1321 | table_ident[v - TOK_IDENT]->sym_define = NULL;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | ST_INLN Sym *define_find(int v)
|
---|
1325 | {
|
---|
1326 | v -= TOK_IDENT;
|
---|
1327 | if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
---|
1328 | return NULL;
|
---|
1329 | return table_ident[v]->sym_define;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | /* free define stack until top reaches 'b' */
|
---|
1333 | ST_FUNC void free_defines(Sym *b)
|
---|
1334 | {
|
---|
1335 | while (define_stack != b) {
|
---|
1336 | Sym *top = define_stack;
|
---|
1337 | define_stack = top->prev;
|
---|
1338 | tok_str_free_str(top->d);
|
---|
1339 | define_undef(top);
|
---|
1340 | sym_free(top);
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | /* restore remaining (-D or predefined) symbols if they were
|
---|
1344 | #undef'd in the file */
|
---|
1345 | while (b) {
|
---|
1346 | int v = b->v;
|
---|
1347 | if (v >= TOK_IDENT && v < tok_ident) {
|
---|
1348 | Sym **d = &table_ident[v - TOK_IDENT]->sym_define;
|
---|
1349 | if (!*d)
|
---|
1350 | *d = b;
|
---|
1351 | }
|
---|
1352 | b = b->prev;
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | /* label lookup */
|
---|
1357 | ST_FUNC Sym *label_find(int v)
|
---|
1358 | {
|
---|
1359 | v -= TOK_IDENT;
|
---|
1360 | if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
|
---|
1361 | return NULL;
|
---|
1362 | return table_ident[v]->sym_label;
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | ST_FUNC Sym *label_push(Sym **ptop, int v, int flags)
|
---|
1366 | {
|
---|
1367 | Sym *s, **ps;
|
---|
1368 | s = sym_push2(ptop, v, 0, 0);
|
---|
1369 | s->r = flags;
|
---|
1370 | ps = &table_ident[v - TOK_IDENT]->sym_label;
|
---|
1371 | if (ptop == &global_label_stack) {
|
---|
1372 | /* modify the top most local identifier, so that
|
---|
1373 | sym_identifier will point to 's' when popped */
|
---|
1374 | while (*ps != NULL)
|
---|
1375 | ps = &(*ps)->prev_tok;
|
---|
1376 | }
|
---|
1377 | s->prev_tok = *ps;
|
---|
1378 | *ps = s;
|
---|
1379 | return s;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /* pop labels until element last is reached. Look if any labels are
|
---|
1383 | undefined. Define symbols if '&&label' was used. */
|
---|
1384 | ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep)
|
---|
1385 | {
|
---|
1386 | Sym *s, *s1;
|
---|
1387 | for(s = *ptop; s != slast; s = s1) {
|
---|
1388 | s1 = s->prev;
|
---|
1389 | if (s->r == LABEL_DECLARED) {
|
---|
1390 | tcc_warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
|
---|
1391 | } else if (s->r == LABEL_FORWARD) {
|
---|
1392 | tcc_error("label '%s' used but not defined",
|
---|
1393 | get_tok_str(s->v, NULL));
|
---|
1394 | } else {
|
---|
1395 | if (s->c) {
|
---|
1396 | /* define corresponding symbol. A size of
|
---|
1397 | 1 is put. */
|
---|
1398 | put_extern_sym(s, cur_text_section, s->jnext, 1);
|
---|
1399 | }
|
---|
1400 | }
|
---|
1401 | /* remove label */
|
---|
1402 | table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
|
---|
1403 | if (!keep)
|
---|
1404 | sym_free(s);
|
---|
1405 | }
|
---|
1406 | if (!keep)
|
---|
1407 | *ptop = slast;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | /* fake the nth "#if defined test_..." for tcc -dt -run */
|
---|
1411 | static void maybe_run_test(TCCState *s)
|
---|
1412 | {
|
---|
1413 | const char *p;
|
---|
1414 | if (s->include_stack_ptr != s->include_stack)
|
---|
1415 | return;
|
---|
1416 | p = get_tok_str(tok, NULL);
|
---|
1417 | if (0 != memcmp(p, "test_", 5))
|
---|
1418 | return;
|
---|
1419 | if (0 != --s->run_test)
|
---|
1420 | return;
|
---|
1421 | fprintf(s->ppfp, "\n[%s]\n" + !(s->dflag & 32), p), fflush(s->ppfp);
|
---|
1422 | define_push(tok, MACRO_OBJ, NULL, NULL);
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | /* eval an expression for #if/#elif */
|
---|
1426 | static int expr_preprocess(void)
|
---|
1427 | {
|
---|
1428 | int c, t;
|
---|
1429 | TokenString *str;
|
---|
1430 |
|
---|
1431 | str = tok_str_alloc();
|
---|
1432 | pp_expr = 1;
|
---|
1433 | while (tok != TOK_LINEFEED && tok != TOK_EOF) {
|
---|
1434 | next(); /* do macro subst */
|
---|
1435 | if (tok == TOK_DEFINED) {
|
---|
1436 | next_nomacro();
|
---|
1437 | t = tok;
|
---|
1438 | if (t == '(')
|
---|
1439 | next_nomacro();
|
---|
1440 | if (tok < TOK_IDENT)
|
---|
1441 | expect("identifier");
|
---|
1442 | if (tcc_state->run_test)
|
---|
1443 | maybe_run_test(tcc_state);
|
---|
1444 | c = define_find(tok) != 0;
|
---|
1445 | if (t == '(') {
|
---|
1446 | next_nomacro();
|
---|
1447 | if (tok != ')')
|
---|
1448 | expect("')'");
|
---|
1449 | }
|
---|
1450 | tok = TOK_CINT;
|
---|
1451 | tokc.i = c;
|
---|
1452 | } else if (tok >= TOK_IDENT) {
|
---|
1453 | /* if undefined macro */
|
---|
1454 | tok = TOK_CINT;
|
---|
1455 | tokc.i = 0;
|
---|
1456 | }
|
---|
1457 | tok_str_add_tok(str);
|
---|
1458 | }
|
---|
1459 | pp_expr = 0;
|
---|
1460 | tok_str_add(str, -1); /* simulate end of file */
|
---|
1461 | tok_str_add(str, 0);
|
---|
1462 | /* now evaluate C constant expression */
|
---|
1463 | begin_macro(str, 1);
|
---|
1464 | next();
|
---|
1465 | c = expr_const();
|
---|
1466 | end_macro();
|
---|
1467 | return c != 0;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 |
|
---|
1471 | /* parse after #define */
|
---|
1472 | ST_FUNC void parse_define(void)
|
---|
1473 | {
|
---|
1474 | Sym *s, *first, **ps;
|
---|
1475 | int v, t, varg, is_vaargs, spc;
|
---|
1476 | int saved_parse_flags = parse_flags;
|
---|
1477 |
|
---|
1478 | v = tok;
|
---|
1479 | if (v < TOK_IDENT || v == TOK_DEFINED)
|
---|
1480 | tcc_error("invalid macro name '%s'", get_tok_str(tok, &tokc));
|
---|
1481 | /* XXX: should check if same macro (ANSI) */
|
---|
1482 | first = NULL;
|
---|
1483 | t = MACRO_OBJ;
|
---|
1484 | /* We have to parse the whole define as if not in asm mode, in particular
|
---|
1485 | no line comment with '#' must be ignored. Also for function
|
---|
1486 | macros the argument list must be parsed without '.' being an ID
|
---|
1487 | character. */
|
---|
1488 | parse_flags = ((parse_flags & ~PARSE_FLAG_ASM_FILE) | PARSE_FLAG_SPACES);
|
---|
1489 | /* '(' must be just after macro definition for MACRO_FUNC */
|
---|
1490 | next_nomacro_spc();
|
---|
1491 | if (tok == '(') {
|
---|
1492 | int dotid = set_idnum('.', 0);
|
---|
1493 | next_nomacro();
|
---|
1494 | ps = &first;
|
---|
1495 | if (tok != ')') for (;;) {
|
---|
1496 | varg = tok;
|
---|
1497 | next_nomacro();
|
---|
1498 | is_vaargs = 0;
|
---|
1499 | if (varg == TOK_DOTS) {
|
---|
1500 | varg = TOK___VA_ARGS__;
|
---|
1501 | is_vaargs = 1;
|
---|
1502 | } else if (tok == TOK_DOTS && gnu_ext) {
|
---|
1503 | is_vaargs = 1;
|
---|
1504 | next_nomacro();
|
---|
1505 | }
|
---|
1506 | if (varg < TOK_IDENT)
|
---|
1507 | bad_list:
|
---|
1508 | tcc_error("bad macro parameter list");
|
---|
1509 | s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
|
---|
1510 | *ps = s;
|
---|
1511 | ps = &s->next;
|
---|
1512 | if (tok == ')')
|
---|
1513 | break;
|
---|
1514 | if (tok != ',' || is_vaargs)
|
---|
1515 | goto bad_list;
|
---|
1516 | next_nomacro();
|
---|
1517 | }
|
---|
1518 | next_nomacro_spc();
|
---|
1519 | t = MACRO_FUNC;
|
---|
1520 | set_idnum('.', dotid);
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | tokstr_buf.len = 0;
|
---|
1524 | spc = 2;
|
---|
1525 | parse_flags |= PARSE_FLAG_ACCEPT_STRAYS | PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED;
|
---|
1526 | /* The body of a macro definition should be parsed such that identifiers
|
---|
1527 | are parsed like the file mode determines (i.e. with '.' being an
|
---|
1528 | ID character in asm mode). But '#' should be retained instead of
|
---|
1529 | regarded as line comment leader, so still don't set ASM_FILE
|
---|
1530 | in parse_flags. */
|
---|
1531 | while (tok != TOK_LINEFEED && tok != TOK_EOF) {
|
---|
1532 | /* remove spaces around ## and after '#' */
|
---|
1533 | if (TOK_TWOSHARPS == tok) {
|
---|
1534 | if (2 == spc)
|
---|
1535 | goto bad_twosharp;
|
---|
1536 | if (1 == spc)
|
---|
1537 | --tokstr_buf.len;
|
---|
1538 | spc = 3;
|
---|
1539 | tok = TOK_PPJOIN;
|
---|
1540 | } else if ('#' == tok) {
|
---|
1541 | spc = 4;
|
---|
1542 | } else if (check_space(tok, &spc)) {
|
---|
1543 | goto skip;
|
---|
1544 | }
|
---|
1545 | tok_str_add2(&tokstr_buf, tok, &tokc);
|
---|
1546 | skip:
|
---|
1547 | next_nomacro_spc();
|
---|
1548 | }
|
---|
1549 |
|
---|
1550 | parse_flags = saved_parse_flags;
|
---|
1551 | if (spc == 1)
|
---|
1552 | --tokstr_buf.len; /* remove trailing space */
|
---|
1553 | tok_str_add(&tokstr_buf, 0);
|
---|
1554 | if (3 == spc)
|
---|
1555 | bad_twosharp:
|
---|
1556 | tcc_error("'##' cannot appear at either end of macro");
|
---|
1557 | define_push(v, t, tok_str_dup(&tokstr_buf), first);
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 | static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
|
---|
1561 | {
|
---|
1562 | const unsigned char *s;
|
---|
1563 | unsigned int h;
|
---|
1564 | CachedInclude *e;
|
---|
1565 | int i;
|
---|
1566 |
|
---|
1567 | h = TOK_HASH_INIT;
|
---|
1568 | s = (unsigned char *) filename;
|
---|
1569 | while (*s) {
|
---|
1570 | #ifdef _WIN32
|
---|
1571 | h = TOK_HASH_FUNC(h, toup(*s));
|
---|
1572 | #else
|
---|
1573 | h = TOK_HASH_FUNC(h, *s);
|
---|
1574 | #endif
|
---|
1575 | s++;
|
---|
1576 | }
|
---|
1577 | h &= (CACHED_INCLUDES_HASH_SIZE - 1);
|
---|
1578 |
|
---|
1579 | i = s1->cached_includes_hash[h];
|
---|
1580 | for(;;) {
|
---|
1581 | if (i == 0)
|
---|
1582 | break;
|
---|
1583 | e = s1->cached_includes[i - 1];
|
---|
1584 | if (0 == PATHCMP(e->filename, filename))
|
---|
1585 | return e;
|
---|
1586 | i = e->hash_next;
|
---|
1587 | }
|
---|
1588 | if (!add)
|
---|
1589 | return NULL;
|
---|
1590 |
|
---|
1591 | e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
|
---|
1592 | strcpy(e->filename, filename);
|
---|
1593 | e->ifndef_macro = e->once = 0;
|
---|
1594 | dynarray_add(&s1->cached_includes, &s1->nb_cached_includes, e);
|
---|
1595 | /* add in hash table */
|
---|
1596 | e->hash_next = s1->cached_includes_hash[h];
|
---|
1597 | s1->cached_includes_hash[h] = s1->nb_cached_includes;
|
---|
1598 | #ifdef INC_DEBUG
|
---|
1599 | printf("adding cached '%s'\n", filename);
|
---|
1600 | #endif
|
---|
1601 | return e;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | static void pragma_parse(TCCState *s1)
|
---|
1605 | {
|
---|
1606 | next_nomacro();
|
---|
1607 | if (tok == TOK_push_macro || tok == TOK_pop_macro) {
|
---|
1608 | int t = tok, v;
|
---|
1609 | Sym *s;
|
---|
1610 |
|
---|
1611 | if (next(), tok != '(')
|
---|
1612 | goto pragma_err;
|
---|
1613 | if (next(), tok != TOK_STR)
|
---|
1614 | goto pragma_err;
|
---|
1615 | v = tok_alloc(tokc.str.data, tokc.str.size - 1)->tok;
|
---|
1616 | if (next(), tok != ')')
|
---|
1617 | goto pragma_err;
|
---|
1618 | if (t == TOK_push_macro) {
|
---|
1619 | while (NULL == (s = define_find(v)))
|
---|
1620 | define_push(v, 0, NULL, NULL);
|
---|
1621 | s->type.ref = s; /* set push boundary */
|
---|
1622 | } else {
|
---|
1623 | for (s = define_stack; s; s = s->prev)
|
---|
1624 | if (s->v == v && s->type.ref == s) {
|
---|
1625 | s->type.ref = NULL;
|
---|
1626 | break;
|
---|
1627 | }
|
---|
1628 | }
|
---|
1629 | if (s)
|
---|
1630 | table_ident[v - TOK_IDENT]->sym_define = s->d ? s : NULL;
|
---|
1631 | else
|
---|
1632 | tcc_warning("unbalanced #pragma pop_macro");
|
---|
1633 | pp_debug_tok = t, pp_debug_symv = v;
|
---|
1634 |
|
---|
1635 | } else if (tok == TOK_once) {
|
---|
1636 | search_cached_include(s1, file->filename, 1)->once = pp_once;
|
---|
1637 |
|
---|
1638 | } else if (s1->output_type == TCC_OUTPUT_PREPROCESS) {
|
---|
1639 | /* tcc -E: keep pragmas below unchanged */
|
---|
1640 | unget_tok(' ');
|
---|
1641 | unget_tok(TOK_PRAGMA);
|
---|
1642 | unget_tok('#');
|
---|
1643 | unget_tok(TOK_LINEFEED);
|
---|
1644 |
|
---|
1645 | } else if (tok == TOK_pack) {
|
---|
1646 | /* This may be:
|
---|
1647 | #pragma pack(1) // set
|
---|
1648 | #pragma pack() // reset to default
|
---|
1649 | #pragma pack(push,1) // push & set
|
---|
1650 | #pragma pack(pop) // restore previous */
|
---|
1651 | next();
|
---|
1652 | skip('(');
|
---|
1653 | if (tok == TOK_ASM_pop) {
|
---|
1654 | next();
|
---|
1655 | if (s1->pack_stack_ptr <= s1->pack_stack) {
|
---|
1656 | stk_error:
|
---|
1657 | tcc_error("out of pack stack");
|
---|
1658 | }
|
---|
1659 | s1->pack_stack_ptr--;
|
---|
1660 | } else {
|
---|
1661 | int val = 0;
|
---|
1662 | if (tok != ')') {
|
---|
1663 | if (tok == TOK_ASM_push) {
|
---|
1664 | next();
|
---|
1665 | if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
|
---|
1666 | goto stk_error;
|
---|
1667 | s1->pack_stack_ptr++;
|
---|
1668 | skip(',');
|
---|
1669 | }
|
---|
1670 | if (tok != TOK_CINT)
|
---|
1671 | goto pragma_err;
|
---|
1672 | val = tokc.i;
|
---|
1673 | if (val < 1 || val > 16 || (val & (val - 1)) != 0)
|
---|
1674 | goto pragma_err;
|
---|
1675 | next();
|
---|
1676 | }
|
---|
1677 | *s1->pack_stack_ptr = val;
|
---|
1678 | }
|
---|
1679 | if (tok != ')')
|
---|
1680 | goto pragma_err;
|
---|
1681 |
|
---|
1682 | } else if (tok == TOK_comment) {
|
---|
1683 | char *p; int t;
|
---|
1684 | next();
|
---|
1685 | skip('(');
|
---|
1686 | t = tok;
|
---|
1687 | next();
|
---|
1688 | skip(',');
|
---|
1689 | if (tok != TOK_STR)
|
---|
1690 | goto pragma_err;
|
---|
1691 | p = tcc_strdup((char *)tokc.str.data);
|
---|
1692 | next();
|
---|
1693 | if (tok != ')')
|
---|
1694 | goto pragma_err;
|
---|
1695 | if (t == TOK_lib) {
|
---|
1696 | dynarray_add(&s1->pragma_libs, &s1->nb_pragma_libs, p);
|
---|
1697 | } else {
|
---|
1698 | if (t == TOK_option)
|
---|
1699 | tcc_set_options(s1, p);
|
---|
1700 | tcc_free(p);
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | } else if (s1->warn_unsupported) {
|
---|
1704 | tcc_warning("#pragma %s is ignored", get_tok_str(tok, &tokc));
|
---|
1705 | }
|
---|
1706 | return;
|
---|
1707 |
|
---|
1708 | pragma_err:
|
---|
1709 | tcc_error("malformed #pragma directive");
|
---|
1710 | return;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 | /* is_bof is true if first non space token at beginning of file */
|
---|
1714 | ST_FUNC void preprocess(int is_bof)
|
---|
1715 | {
|
---|
1716 | TCCState *s1 = tcc_state;
|
---|
1717 | int i, c, n, saved_parse_flags;
|
---|
1718 | char buf[1024], *q;
|
---|
1719 | Sym *s;
|
---|
1720 |
|
---|
1721 | saved_parse_flags = parse_flags;
|
---|
1722 | parse_flags = PARSE_FLAG_PREPROCESS
|
---|
1723 | | PARSE_FLAG_TOK_NUM
|
---|
1724 | | PARSE_FLAG_TOK_STR
|
---|
1725 | | PARSE_FLAG_LINEFEED
|
---|
1726 | | (parse_flags & PARSE_FLAG_ASM_FILE)
|
---|
1727 | ;
|
---|
1728 |
|
---|
1729 | next_nomacro();
|
---|
1730 | redo:
|
---|
1731 | switch(tok) {
|
---|
1732 | case TOK_DEFINE:
|
---|
1733 | pp_debug_tok = tok;
|
---|
1734 | next_nomacro();
|
---|
1735 | pp_debug_symv = tok;
|
---|
1736 | parse_define();
|
---|
1737 | break;
|
---|
1738 | case TOK_UNDEF:
|
---|
1739 | pp_debug_tok = tok;
|
---|
1740 | next_nomacro();
|
---|
1741 | pp_debug_symv = tok;
|
---|
1742 | s = define_find(tok);
|
---|
1743 | /* undefine symbol by putting an invalid name */
|
---|
1744 | if (s)
|
---|
1745 | define_undef(s);
|
---|
1746 | break;
|
---|
1747 | case TOK_INCLUDE:
|
---|
1748 | case TOK_INCLUDE_NEXT:
|
---|
1749 | ch = file->buf_ptr[0];
|
---|
1750 | /* XXX: incorrect if comments : use next_nomacro with a special mode */
|
---|
1751 | skip_spaces();
|
---|
1752 | if (ch == '<') {
|
---|
1753 | c = '>';
|
---|
1754 | goto read_name;
|
---|
1755 | } else if (ch == '\"') {
|
---|
1756 | c = ch;
|
---|
1757 | read_name:
|
---|
1758 | inp();
|
---|
1759 | q = buf;
|
---|
1760 | while (ch != c && ch != '\n' && ch != CH_EOF) {
|
---|
1761 | if ((q - buf) < sizeof(buf) - 1)
|
---|
1762 | *q++ = ch;
|
---|
1763 | if (ch == '\\') {
|
---|
1764 | if (handle_stray_noerror() == 0)
|
---|
1765 | --q;
|
---|
1766 | } else
|
---|
1767 | inp();
|
---|
1768 | }
|
---|
1769 | *q = '\0';
|
---|
1770 | minp();
|
---|
1771 | #if 0
|
---|
1772 | /* eat all spaces and comments after include */
|
---|
1773 | /* XXX: slightly incorrect */
|
---|
1774 | while (ch1 != '\n' && ch1 != CH_EOF)
|
---|
1775 | inp();
|
---|
1776 | #endif
|
---|
1777 | } else {
|
---|
1778 | int len;
|
---|
1779 | /* computed #include : concatenate everything up to linefeed,
|
---|
1780 | the result must be one of the two accepted forms.
|
---|
1781 | Don't convert pp-tokens to tokens here. */
|
---|
1782 | parse_flags = (PARSE_FLAG_PREPROCESS
|
---|
1783 | | PARSE_FLAG_LINEFEED
|
---|
1784 | | (parse_flags & PARSE_FLAG_ASM_FILE));
|
---|
1785 | next();
|
---|
1786 | buf[0] = '\0';
|
---|
1787 | while (tok != TOK_LINEFEED) {
|
---|
1788 | pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
|
---|
1789 | next();
|
---|
1790 | }
|
---|
1791 | len = strlen(buf);
|
---|
1792 | /* check syntax and remove '<>|""' */
|
---|
1793 | if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') &&
|
---|
1794 | (buf[0] != '<' || buf[len-1] != '>'))))
|
---|
1795 | tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
|
---|
1796 | c = buf[len-1];
|
---|
1797 | memmove(buf, buf + 1, len - 2);
|
---|
1798 | buf[len - 2] = '\0';
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
|
---|
1802 | tcc_error("#include recursion too deep");
|
---|
1803 | /* store current file in stack, but increment stack later below */
|
---|
1804 | *s1->include_stack_ptr = file;
|
---|
1805 | i = tok == TOK_INCLUDE_NEXT ? file->include_next_index : 0;
|
---|
1806 | n = 2 + s1->nb_include_paths + s1->nb_sysinclude_paths;
|
---|
1807 | for (; i < n; ++i) {
|
---|
1808 | char buf1[sizeof file->filename];
|
---|
1809 | CachedInclude *e;
|
---|
1810 | const char *path;
|
---|
1811 |
|
---|
1812 | if (i == 0) {
|
---|
1813 | /* check absolute include path */
|
---|
1814 | if (!IS_ABSPATH(buf))
|
---|
1815 | continue;
|
---|
1816 | buf1[0] = 0;
|
---|
1817 |
|
---|
1818 | } else if (i == 1) {
|
---|
1819 | /* search in file's dir if "header.h" */
|
---|
1820 | if (c != '\"')
|
---|
1821 | continue;
|
---|
1822 | /* https://savannah.nongnu.org/bugs/index.php?50847 */
|
---|
1823 | path = file->true_filename;
|
---|
1824 | pstrncpy(buf1, path, tcc_basename(path) - path);
|
---|
1825 |
|
---|
1826 | } else {
|
---|
1827 | /* search in all the include paths */
|
---|
1828 | int j = i - 2, k = j - s1->nb_include_paths;
|
---|
1829 | path = k < 0 ? s1->include_paths[j] : s1->sysinclude_paths[k];
|
---|
1830 | pstrcpy(buf1, sizeof(buf1), path);
|
---|
1831 | pstrcat(buf1, sizeof(buf1), "/");
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | pstrcat(buf1, sizeof(buf1), buf);
|
---|
1835 | e = search_cached_include(s1, buf1, 0);
|
---|
1836 | if (e && (define_find(e->ifndef_macro) || e->once == pp_once)) {
|
---|
1837 | /* no need to parse the include because the 'ifndef macro'
|
---|
1838 | is defined (or had #pragma once) */
|
---|
1839 | #ifdef INC_DEBUG
|
---|
1840 | printf("%s: skipping cached %s\n", file->filename, buf1);
|
---|
1841 | #endif
|
---|
1842 | goto include_done;
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | if (tcc_open(s1, buf1) < 0)
|
---|
1846 | continue;
|
---|
1847 |
|
---|
1848 | file->include_next_index = i + 1;
|
---|
1849 | #ifdef INC_DEBUG
|
---|
1850 | printf("%s: including %s\n", file->prev->filename, file->filename);
|
---|
1851 | #endif
|
---|
1852 | /* update target deps */
|
---|
1853 | dynarray_add(&s1->target_deps, &s1->nb_target_deps,
|
---|
1854 | tcc_strdup(buf1));
|
---|
1855 | /* push current file in stack */
|
---|
1856 | ++s1->include_stack_ptr;
|
---|
1857 | /* add include file debug info */
|
---|
1858 | if (s1->do_debug)
|
---|
1859 | put_stabs(file->filename, N_BINCL, 0, 0, 0);
|
---|
1860 | tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
|
---|
1861 | ch = file->buf_ptr[0];
|
---|
1862 | goto the_end;
|
---|
1863 | }
|
---|
1864 | tcc_error("include file '%s' not found", buf);
|
---|
1865 | include_done:
|
---|
1866 | break;
|
---|
1867 | case TOK_IFNDEF:
|
---|
1868 | c = 1;
|
---|
1869 | goto do_ifdef;
|
---|
1870 | case TOK_IF:
|
---|
1871 | c = expr_preprocess();
|
---|
1872 | goto do_if;
|
---|
1873 | case TOK_IFDEF:
|
---|
1874 | c = 0;
|
---|
1875 | do_ifdef:
|
---|
1876 | next_nomacro();
|
---|
1877 | if (tok < TOK_IDENT)
|
---|
1878 | tcc_error("invalid argument for '#if%sdef'", c ? "n" : "");
|
---|
1879 | if (is_bof) {
|
---|
1880 | if (c) {
|
---|
1881 | #ifdef INC_DEBUG
|
---|
1882 | printf("#ifndef %s\n", get_tok_str(tok, NULL));
|
---|
1883 | #endif
|
---|
1884 | file->ifndef_macro = tok;
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 | c = (define_find(tok) != 0) ^ c;
|
---|
1888 | do_if:
|
---|
1889 | if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
|
---|
1890 | tcc_error("memory full (ifdef)");
|
---|
1891 | *s1->ifdef_stack_ptr++ = c;
|
---|
1892 | goto test_skip;
|
---|
1893 | case TOK_ELSE:
|
---|
1894 | if (s1->ifdef_stack_ptr == s1->ifdef_stack)
|
---|
1895 | tcc_error("#else without matching #if");
|
---|
1896 | if (s1->ifdef_stack_ptr[-1] & 2)
|
---|
1897 | tcc_error("#else after #else");
|
---|
1898 | c = (s1->ifdef_stack_ptr[-1] ^= 3);
|
---|
1899 | goto test_else;
|
---|
1900 | case TOK_ELIF:
|
---|
1901 | if (s1->ifdef_stack_ptr == s1->ifdef_stack)
|
---|
1902 | tcc_error("#elif without matching #if");
|
---|
1903 | c = s1->ifdef_stack_ptr[-1];
|
---|
1904 | if (c > 1)
|
---|
1905 | tcc_error("#elif after #else");
|
---|
1906 | /* last #if/#elif expression was true: we skip */
|
---|
1907 | if (c == 1) {
|
---|
1908 | c = 0;
|
---|
1909 | } else {
|
---|
1910 | c = expr_preprocess();
|
---|
1911 | s1->ifdef_stack_ptr[-1] = c;
|
---|
1912 | }
|
---|
1913 | test_else:
|
---|
1914 | if (s1->ifdef_stack_ptr == file->ifdef_stack_ptr + 1)
|
---|
1915 | file->ifndef_macro = 0;
|
---|
1916 | test_skip:
|
---|
1917 | if (!(c & 1)) {
|
---|
1918 | preprocess_skip();
|
---|
1919 | is_bof = 0;
|
---|
1920 | goto redo;
|
---|
1921 | }
|
---|
1922 | break;
|
---|
1923 | case TOK_ENDIF:
|
---|
1924 | if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
|
---|
1925 | tcc_error("#endif without matching #if");
|
---|
1926 | s1->ifdef_stack_ptr--;
|
---|
1927 | /* '#ifndef macro' was at the start of file. Now we check if
|
---|
1928 | an '#endif' is exactly at the end of file */
|
---|
1929 | if (file->ifndef_macro &&
|
---|
1930 | s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
|
---|
1931 | file->ifndef_macro_saved = file->ifndef_macro;
|
---|
1932 | /* need to set to zero to avoid false matches if another
|
---|
1933 | #ifndef at middle of file */
|
---|
1934 | file->ifndef_macro = 0;
|
---|
1935 | while (tok != TOK_LINEFEED)
|
---|
1936 | next_nomacro();
|
---|
1937 | tok_flags |= TOK_FLAG_ENDIF;
|
---|
1938 | goto the_end;
|
---|
1939 | }
|
---|
1940 | break;
|
---|
1941 | case TOK_PPNUM:
|
---|
1942 | n = strtoul((char*)tokc.str.data, &q, 10);
|
---|
1943 | goto _line_num;
|
---|
1944 | case TOK_LINE:
|
---|
1945 | next();
|
---|
1946 | if (tok != TOK_CINT)
|
---|
1947 | _line_err:
|
---|
1948 | tcc_error("wrong #line format");
|
---|
1949 | n = tokc.i;
|
---|
1950 | _line_num:
|
---|
1951 | next();
|
---|
1952 | if (tok != TOK_LINEFEED) {
|
---|
1953 | if (tok == TOK_STR) {
|
---|
1954 | if (file->true_filename == file->filename)
|
---|
1955 | file->true_filename = tcc_strdup(file->filename);
|
---|
1956 | pstrcpy(file->filename, sizeof(file->filename), (char *)tokc.str.data);
|
---|
1957 | } else if (parse_flags & PARSE_FLAG_ASM_FILE)
|
---|
1958 | break;
|
---|
1959 | else
|
---|
1960 | goto _line_err;
|
---|
1961 | --n;
|
---|
1962 | }
|
---|
1963 | if (file->fd > 0)
|
---|
1964 | total_lines += file->line_num - n;
|
---|
1965 | file->line_num = n;
|
---|
1966 | if (s1->do_debug)
|
---|
1967 | put_stabs(file->filename, N_BINCL, 0, 0, 0);
|
---|
1968 | break;
|
---|
1969 | case TOK_ERROR:
|
---|
1970 | case TOK_WARNING:
|
---|
1971 | c = tok;
|
---|
1972 | ch = file->buf_ptr[0];
|
---|
1973 | skip_spaces();
|
---|
1974 | q = buf;
|
---|
1975 | while (ch != '\n' && ch != CH_EOF) {
|
---|
1976 | if ((q - buf) < sizeof(buf) - 1)
|
---|
1977 | *q++ = ch;
|
---|
1978 | if (ch == '\\') {
|
---|
1979 | if (handle_stray_noerror() == 0)
|
---|
1980 | --q;
|
---|
1981 | } else
|
---|
1982 | inp();
|
---|
1983 | }
|
---|
1984 | *q = '\0';
|
---|
1985 | if (c == TOK_ERROR)
|
---|
1986 | tcc_error("#error %s", buf);
|
---|
1987 | else
|
---|
1988 | tcc_warning("#warning %s", buf);
|
---|
1989 | break;
|
---|
1990 | case TOK_PRAGMA:
|
---|
1991 | pragma_parse(s1);
|
---|
1992 | break;
|
---|
1993 | case TOK_LINEFEED:
|
---|
1994 | goto the_end;
|
---|
1995 | default:
|
---|
1996 | /* ignore gas line comment in an 'S' file. */
|
---|
1997 | if (saved_parse_flags & PARSE_FLAG_ASM_FILE)
|
---|
1998 | goto ignore;
|
---|
1999 | if (tok == '!' && is_bof)
|
---|
2000 | /* '!' is ignored at beginning to allow C scripts. */
|
---|
2001 | goto ignore;
|
---|
2002 | tcc_warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
|
---|
2003 | ignore:
|
---|
2004 | file->buf_ptr = parse_line_comment(file->buf_ptr - 1);
|
---|
2005 | goto the_end;
|
---|
2006 | }
|
---|
2007 | /* ignore other preprocess commands or #! for C scripts */
|
---|
2008 | while (tok != TOK_LINEFEED)
|
---|
2009 | next_nomacro();
|
---|
2010 | the_end:
|
---|
2011 | parse_flags = saved_parse_flags;
|
---|
2012 | }
|
---|
2013 |
|
---|
2014 | /* evaluate escape codes in a string. */
|
---|
2015 | static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
|
---|
2016 | {
|
---|
2017 | int c, n;
|
---|
2018 | const uint8_t *p;
|
---|
2019 |
|
---|
2020 | p = buf;
|
---|
2021 | for(;;) {
|
---|
2022 | c = *p;
|
---|
2023 | if (c == '\0')
|
---|
2024 | break;
|
---|
2025 | if (c == '\\') {
|
---|
2026 | p++;
|
---|
2027 | /* escape */
|
---|
2028 | c = *p;
|
---|
2029 | switch(c) {
|
---|
2030 | case '0': case '1': case '2': case '3':
|
---|
2031 | case '4': case '5': case '6': case '7':
|
---|
2032 | /* at most three octal digits */
|
---|
2033 | n = c - '0';
|
---|
2034 | p++;
|
---|
2035 | c = *p;
|
---|
2036 | if (isoct(c)) {
|
---|
2037 | n = n * 8 + c - '0';
|
---|
2038 | p++;
|
---|
2039 | c = *p;
|
---|
2040 | if (isoct(c)) {
|
---|
2041 | n = n * 8 + c - '0';
|
---|
2042 | p++;
|
---|
2043 | }
|
---|
2044 | }
|
---|
2045 | c = n;
|
---|
2046 | goto add_char_nonext;
|
---|
2047 | case 'x':
|
---|
2048 | case 'u':
|
---|
2049 | case 'U':
|
---|
2050 | p++;
|
---|
2051 | n = 0;
|
---|
2052 | for(;;) {
|
---|
2053 | c = *p;
|
---|
2054 | if (c >= 'a' && c <= 'f')
|
---|
2055 | c = c - 'a' + 10;
|
---|
2056 | else if (c >= 'A' && c <= 'F')
|
---|
2057 | c = c - 'A' + 10;
|
---|
2058 | else if (isnum(c))
|
---|
2059 | c = c - '0';
|
---|
2060 | else
|
---|
2061 | break;
|
---|
2062 | n = n * 16 + c;
|
---|
2063 | p++;
|
---|
2064 | }
|
---|
2065 | c = n;
|
---|
2066 | goto add_char_nonext;
|
---|
2067 | case 'a':
|
---|
2068 | c = '\a';
|
---|
2069 | break;
|
---|
2070 | case 'b':
|
---|
2071 | c = '\b';
|
---|
2072 | break;
|
---|
2073 | case 'f':
|
---|
2074 | c = '\f';
|
---|
2075 | break;
|
---|
2076 | case 'n':
|
---|
2077 | c = '\n';
|
---|
2078 | break;
|
---|
2079 | case 'r':
|
---|
2080 | c = '\r';
|
---|
2081 | break;
|
---|
2082 | case 't':
|
---|
2083 | c = '\t';
|
---|
2084 | break;
|
---|
2085 | case 'v':
|
---|
2086 | c = '\v';
|
---|
2087 | break;
|
---|
2088 | case 'e':
|
---|
2089 | if (!gnu_ext)
|
---|
2090 | goto invalid_escape;
|
---|
2091 | c = 27;
|
---|
2092 | break;
|
---|
2093 | case '\'':
|
---|
2094 | case '\"':
|
---|
2095 | case '\\':
|
---|
2096 | case '?':
|
---|
2097 | break;
|
---|
2098 | default:
|
---|
2099 | invalid_escape:
|
---|
2100 | if (c >= '!' && c <= '~')
|
---|
2101 | tcc_warning("unknown escape sequence: \'\\%c\'", c);
|
---|
2102 | else
|
---|
2103 | tcc_warning("unknown escape sequence: \'\\x%x\'", c);
|
---|
2104 | break;
|
---|
2105 | }
|
---|
2106 | } else if (is_long && c >= 0x80) {
|
---|
2107 | /* assume we are processing UTF-8 sequence */
|
---|
2108 | /* reference: The Unicode Standard, Version 10.0, ch3.9 */
|
---|
2109 |
|
---|
2110 | int cont; /* count of continuation bytes */
|
---|
2111 | int skip; /* how many bytes should skip when error occurred */
|
---|
2112 | int i;
|
---|
2113 |
|
---|
2114 | /* decode leading byte */
|
---|
2115 | if (c < 0xC2) {
|
---|
2116 | skip = 1; goto invalid_utf8_sequence;
|
---|
2117 | } else if (c <= 0xDF) {
|
---|
2118 | cont = 1; n = c & 0x1f;
|
---|
2119 | } else if (c <= 0xEF) {
|
---|
2120 | cont = 2; n = c & 0xf;
|
---|
2121 | } else if (c <= 0xF4) {
|
---|
2122 | cont = 3; n = c & 0x7;
|
---|
2123 | } else {
|
---|
2124 | skip = 1; goto invalid_utf8_sequence;
|
---|
2125 | }
|
---|
2126 |
|
---|
2127 | /* decode continuation bytes */
|
---|
2128 | for (i = 1; i <= cont; i++) {
|
---|
2129 | int l = 0x80, h = 0xBF;
|
---|
2130 |
|
---|
2131 | /* adjust limit for second byte */
|
---|
2132 | if (i == 1) {
|
---|
2133 | switch (c) {
|
---|
2134 | case 0xE0: l = 0xA0; break;
|
---|
2135 | case 0xED: h = 0x9F; break;
|
---|
2136 | case 0xF0: l = 0x90; break;
|
---|
2137 | case 0xF4: h = 0x8F; break;
|
---|
2138 | }
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 | if (p[i] < l || p[i] > h) {
|
---|
2142 | skip = i; goto invalid_utf8_sequence;
|
---|
2143 | }
|
---|
2144 |
|
---|
2145 | n = (n << 6) | (p[i] & 0x3f);
|
---|
2146 | }
|
---|
2147 |
|
---|
2148 | /* advance pointer */
|
---|
2149 | p += 1 + cont;
|
---|
2150 | c = n;
|
---|
2151 | goto add_char_nonext;
|
---|
2152 |
|
---|
2153 | /* error handling */
|
---|
2154 | invalid_utf8_sequence:
|
---|
2155 | tcc_warning("ill-formed UTF-8 subsequence starting with: \'\\x%x\'", c);
|
---|
2156 | c = 0xFFFD;
|
---|
2157 | p += skip;
|
---|
2158 | goto add_char_nonext;
|
---|
2159 |
|
---|
2160 | }
|
---|
2161 | p++;
|
---|
2162 | add_char_nonext:
|
---|
2163 | if (!is_long)
|
---|
2164 | cstr_ccat(outstr, c);
|
---|
2165 | else {
|
---|
2166 | #ifdef TCC_TARGET_PE
|
---|
2167 | /* store as UTF-16 */
|
---|
2168 | if (c < 0x10000) {
|
---|
2169 | cstr_wccat(outstr, c);
|
---|
2170 | } else {
|
---|
2171 | c -= 0x10000;
|
---|
2172 | cstr_wccat(outstr, (c >> 10) + 0xD800);
|
---|
2173 | cstr_wccat(outstr, (c & 0x3FF) + 0xDC00);
|
---|
2174 | }
|
---|
2175 | #else
|
---|
2176 | cstr_wccat(outstr, c);
|
---|
2177 | #endif
|
---|
2178 | }
|
---|
2179 | }
|
---|
2180 | /* add a trailing '\0' */
|
---|
2181 | if (!is_long)
|
---|
2182 | cstr_ccat(outstr, '\0');
|
---|
2183 | else
|
---|
2184 | cstr_wccat(outstr, '\0');
|
---|
2185 | }
|
---|
2186 |
|
---|
2187 | static void parse_string(const char *s, int len)
|
---|
2188 | {
|
---|
2189 | uint8_t buf[1000], *p = buf;
|
---|
2190 | int is_long, sep;
|
---|
2191 |
|
---|
2192 | if ((is_long = *s == 'L'))
|
---|
2193 | ++s, --len;
|
---|
2194 | sep = *s++;
|
---|
2195 | len -= 2;
|
---|
2196 | if (len >= sizeof buf)
|
---|
2197 | p = tcc_malloc(len + 1);
|
---|
2198 | memcpy(p, s, len);
|
---|
2199 | p[len] = 0;
|
---|
2200 |
|
---|
2201 | cstr_reset(&tokcstr);
|
---|
2202 | parse_escape_string(&tokcstr, p, is_long);
|
---|
2203 | if (p != buf)
|
---|
2204 | tcc_free(p);
|
---|
2205 |
|
---|
2206 | if (sep == '\'') {
|
---|
2207 | int char_size, i, n, c;
|
---|
2208 | /* XXX: make it portable */
|
---|
2209 | if (!is_long)
|
---|
2210 | tok = TOK_CCHAR, char_size = 1;
|
---|
2211 | else
|
---|
2212 | tok = TOK_LCHAR, char_size = sizeof(nwchar_t);
|
---|
2213 | n = tokcstr.size / char_size - 1;
|
---|
2214 | if (n < 1)
|
---|
2215 | tcc_error("empty character constant");
|
---|
2216 | if (n > 1)
|
---|
2217 | tcc_warning("multi-character character constant");
|
---|
2218 | for (c = i = 0; i < n; ++i) {
|
---|
2219 | if (is_long)
|
---|
2220 | c = ((nwchar_t *)tokcstr.data)[i];
|
---|
2221 | else
|
---|
2222 | c = (c << 8) | ((char *)tokcstr.data)[i];
|
---|
2223 | }
|
---|
2224 | tokc.i = c;
|
---|
2225 | } else {
|
---|
2226 | tokc.str.size = tokcstr.size;
|
---|
2227 | tokc.str.data = tokcstr.data;
|
---|
2228 | if (!is_long)
|
---|
2229 | tok = TOK_STR;
|
---|
2230 | else
|
---|
2231 | tok = TOK_LSTR;
|
---|
2232 | }
|
---|
2233 | }
|
---|
2234 |
|
---|
2235 | /* we use 64 bit numbers */
|
---|
2236 | #define BN_SIZE 2
|
---|
2237 |
|
---|
2238 | /* bn = (bn << shift) | or_val */
|
---|
2239 | static void bn_lshift(unsigned int *bn, int shift, int or_val)
|
---|
2240 | {
|
---|
2241 | int i;
|
---|
2242 | unsigned int v;
|
---|
2243 | for(i=0;i<BN_SIZE;i++) {
|
---|
2244 | v = bn[i];
|
---|
2245 | bn[i] = (v << shift) | or_val;
|
---|
2246 | or_val = v >> (32 - shift);
|
---|
2247 | }
|
---|
2248 | }
|
---|
2249 |
|
---|
2250 | static void bn_zero(unsigned int *bn)
|
---|
2251 | {
|
---|
2252 | int i;
|
---|
2253 | for(i=0;i<BN_SIZE;i++) {
|
---|
2254 | bn[i] = 0;
|
---|
2255 | }
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | /* parse number in null terminated string 'p' and return it in the
|
---|
2259 | current token */
|
---|
2260 | static void parse_number(const char *p)
|
---|
2261 | {
|
---|
2262 | int b, t, shift, frac_bits, s, exp_val, ch;
|
---|
2263 | char *q;
|
---|
2264 | unsigned int bn[BN_SIZE];
|
---|
2265 | double d;
|
---|
2266 |
|
---|
2267 | /* number */
|
---|
2268 | q = token_buf;
|
---|
2269 | ch = *p++;
|
---|
2270 | t = ch;
|
---|
2271 | ch = *p++;
|
---|
2272 | *q++ = t;
|
---|
2273 | b = 10;
|
---|
2274 | if (t == '.') {
|
---|
2275 | goto float_frac_parse;
|
---|
2276 | } else if (t == '0') {
|
---|
2277 | if (ch == 'x' || ch == 'X') {
|
---|
2278 | q--;
|
---|
2279 | ch = *p++;
|
---|
2280 | b = 16;
|
---|
2281 | } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
|
---|
2282 | q--;
|
---|
2283 | ch = *p++;
|
---|
2284 | b = 2;
|
---|
2285 | }
|
---|
2286 | }
|
---|
2287 | /* parse all digits. cannot check octal numbers at this stage
|
---|
2288 | because of floating point constants */
|
---|
2289 | while (1) {
|
---|
2290 | if (ch >= 'a' && ch <= 'f')
|
---|
2291 | t = ch - 'a' + 10;
|
---|
2292 | else if (ch >= 'A' && ch <= 'F')
|
---|
2293 | t = ch - 'A' + 10;
|
---|
2294 | else if (isnum(ch))
|
---|
2295 | t = ch - '0';
|
---|
2296 | else
|
---|
2297 | break;
|
---|
2298 | if (t >= b)
|
---|
2299 | break;
|
---|
2300 | if (q >= token_buf + STRING_MAX_SIZE) {
|
---|
2301 | num_too_long:
|
---|
2302 | tcc_error("number too long");
|
---|
2303 | }
|
---|
2304 | *q++ = ch;
|
---|
2305 | ch = *p++;
|
---|
2306 | }
|
---|
2307 | if (ch == '.' ||
|
---|
2308 | ((ch == 'e' || ch == 'E') && b == 10) ||
|
---|
2309 | ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
|
---|
2310 | if (b != 10) {
|
---|
2311 | /* NOTE: strtox should support that for hexa numbers, but
|
---|
2312 | non ISOC99 libcs do not support it, so we prefer to do
|
---|
2313 | it by hand */
|
---|
2314 | /* hexadecimal or binary floats */
|
---|
2315 | /* XXX: handle overflows */
|
---|
2316 | *q = '\0';
|
---|
2317 | if (b == 16)
|
---|
2318 | shift = 4;
|
---|
2319 | else
|
---|
2320 | shift = 1;
|
---|
2321 | bn_zero(bn);
|
---|
2322 | q = token_buf;
|
---|
2323 | while (1) {
|
---|
2324 | t = *q++;
|
---|
2325 | if (t == '\0') {
|
---|
2326 | break;
|
---|
2327 | } else if (t >= 'a') {
|
---|
2328 | t = t - 'a' + 10;
|
---|
2329 | } else if (t >= 'A') {
|
---|
2330 | t = t - 'A' + 10;
|
---|
2331 | } else {
|
---|
2332 | t = t - '0';
|
---|
2333 | }
|
---|
2334 | bn_lshift(bn, shift, t);
|
---|
2335 | }
|
---|
2336 | frac_bits = 0;
|
---|
2337 | if (ch == '.') {
|
---|
2338 | ch = *p++;
|
---|
2339 | while (1) {
|
---|
2340 | t = ch;
|
---|
2341 | if (t >= 'a' && t <= 'f') {
|
---|
2342 | t = t - 'a' + 10;
|
---|
2343 | } else if (t >= 'A' && t <= 'F') {
|
---|
2344 | t = t - 'A' + 10;
|
---|
2345 | } else if (t >= '0' && t <= '9') {
|
---|
2346 | t = t - '0';
|
---|
2347 | } else {
|
---|
2348 | break;
|
---|
2349 | }
|
---|
2350 | if (t >= b)
|
---|
2351 | tcc_error("invalid digit");
|
---|
2352 | bn_lshift(bn, shift, t);
|
---|
2353 | frac_bits += shift;
|
---|
2354 | ch = *p++;
|
---|
2355 | }
|
---|
2356 | }
|
---|
2357 | if (ch != 'p' && ch != 'P')
|
---|
2358 | expect("exponent");
|
---|
2359 | ch = *p++;
|
---|
2360 | s = 1;
|
---|
2361 | exp_val = 0;
|
---|
2362 | if (ch == '+') {
|
---|
2363 | ch = *p++;
|
---|
2364 | } else if (ch == '-') {
|
---|
2365 | s = -1;
|
---|
2366 | ch = *p++;
|
---|
2367 | }
|
---|
2368 | if (ch < '0' || ch > '9')
|
---|
2369 | expect("exponent digits");
|
---|
2370 | while (ch >= '0' && ch <= '9') {
|
---|
2371 | exp_val = exp_val * 10 + ch - '0';
|
---|
2372 | ch = *p++;
|
---|
2373 | }
|
---|
2374 | exp_val = exp_val * s;
|
---|
2375 |
|
---|
2376 | /* now we can generate the number */
|
---|
2377 | /* XXX: should patch directly float number */
|
---|
2378 | d = (double)bn[1] * 4294967296.0 + (double)bn[0];
|
---|
2379 | d = ldexp(d, exp_val - frac_bits);
|
---|
2380 | t = toup(ch);
|
---|
2381 | if (t == 'F') {
|
---|
2382 | ch = *p++;
|
---|
2383 | tok = TOK_CFLOAT;
|
---|
2384 | /* float : should handle overflow */
|
---|
2385 | tokc.f = (float)d;
|
---|
2386 | } else if (t == 'L') {
|
---|
2387 | ch = *p++;
|
---|
2388 | #ifdef TCC_TARGET_PE
|
---|
2389 | tok = TOK_CDOUBLE;
|
---|
2390 | tokc.d = d;
|
---|
2391 | #else
|
---|
2392 | tok = TOK_CLDOUBLE;
|
---|
2393 | /* XXX: not large enough */
|
---|
2394 | tokc.ld = (long double)d;
|
---|
2395 | #endif
|
---|
2396 | } else {
|
---|
2397 | tok = TOK_CDOUBLE;
|
---|
2398 | tokc.d = d;
|
---|
2399 | }
|
---|
2400 | } else {
|
---|
2401 | /* decimal floats */
|
---|
2402 | if (ch == '.') {
|
---|
2403 | if (q >= token_buf + STRING_MAX_SIZE)
|
---|
2404 | goto num_too_long;
|
---|
2405 | *q++ = ch;
|
---|
2406 | ch = *p++;
|
---|
2407 | float_frac_parse:
|
---|
2408 | while (ch >= '0' && ch <= '9') {
|
---|
2409 | if (q >= token_buf + STRING_MAX_SIZE)
|
---|
2410 | goto num_too_long;
|
---|
2411 | *q++ = ch;
|
---|
2412 | ch = *p++;
|
---|
2413 | }
|
---|
2414 | }
|
---|
2415 | if (ch == 'e' || ch == 'E') {
|
---|
2416 | if (q >= token_buf + STRING_MAX_SIZE)
|
---|
2417 | goto num_too_long;
|
---|
2418 | *q++ = ch;
|
---|
2419 | ch = *p++;
|
---|
2420 | if (ch == '-' || ch == '+') {
|
---|
2421 | if (q >= token_buf + STRING_MAX_SIZE)
|
---|
2422 | goto num_too_long;
|
---|
2423 | *q++ = ch;
|
---|
2424 | ch = *p++;
|
---|
2425 | }
|
---|
2426 | if (ch < '0' || ch > '9')
|
---|
2427 | expect("exponent digits");
|
---|
2428 | while (ch >= '0' && ch <= '9') {
|
---|
2429 | if (q >= token_buf + STRING_MAX_SIZE)
|
---|
2430 | goto num_too_long;
|
---|
2431 | *q++ = ch;
|
---|
2432 | ch = *p++;
|
---|
2433 | }
|
---|
2434 | }
|
---|
2435 | *q = '\0';
|
---|
2436 | t = toup(ch);
|
---|
2437 | errno = 0;
|
---|
2438 | if (t == 'F') {
|
---|
2439 | ch = *p++;
|
---|
2440 | tok = TOK_CFLOAT;
|
---|
2441 | tokc.f = strtof(token_buf, NULL);
|
---|
2442 | } else if (t == 'L') {
|
---|
2443 | ch = *p++;
|
---|
2444 | #ifdef TCC_TARGET_PE
|
---|
2445 | tok = TOK_CDOUBLE;
|
---|
2446 | tokc.d = strtod(token_buf, NULL);
|
---|
2447 | #else
|
---|
2448 | tok = TOK_CLDOUBLE;
|
---|
2449 | tokc.ld = strtold(token_buf, NULL);
|
---|
2450 | #endif
|
---|
2451 | } else {
|
---|
2452 | tok = TOK_CDOUBLE;
|
---|
2453 | tokc.d = strtod(token_buf, NULL);
|
---|
2454 | }
|
---|
2455 | }
|
---|
2456 | } else {
|
---|
2457 | unsigned long long n, n1;
|
---|
2458 | int lcount, ucount, ov = 0;
|
---|
2459 | const char *p1;
|
---|
2460 |
|
---|
2461 | /* integer number */
|
---|
2462 | *q = '\0';
|
---|
2463 | q = token_buf;
|
---|
2464 | if (b == 10 && *q == '0') {
|
---|
2465 | b = 8;
|
---|
2466 | q++;
|
---|
2467 | }
|
---|
2468 | n = 0;
|
---|
2469 | while(1) {
|
---|
2470 | t = *q++;
|
---|
2471 | /* no need for checks except for base 10 / 8 errors */
|
---|
2472 | if (t == '\0')
|
---|
2473 | break;
|
---|
2474 | else if (t >= 'a')
|
---|
2475 | t = t - 'a' + 10;
|
---|
2476 | else if (t >= 'A')
|
---|
2477 | t = t - 'A' + 10;
|
---|
2478 | else
|
---|
2479 | t = t - '0';
|
---|
2480 | if (t >= b)
|
---|
2481 | tcc_error("invalid digit");
|
---|
2482 | n1 = n;
|
---|
2483 | n = n * b + t;
|
---|
2484 | /* detect overflow */
|
---|
2485 | if (n1 >= 0x1000000000000000ULL && n / b != n1)
|
---|
2486 | ov = 1;
|
---|
2487 | }
|
---|
2488 |
|
---|
2489 | /* Determine the characteristics (unsigned and/or 64bit) the type of
|
---|
2490 | the constant must have according to the constant suffix(es) */
|
---|
2491 | lcount = ucount = 0;
|
---|
2492 | p1 = p;
|
---|
2493 | for(;;) {
|
---|
2494 | t = toup(ch);
|
---|
2495 | if (t == 'L') {
|
---|
2496 | if (lcount >= 2)
|
---|
2497 | tcc_error("three 'l's in integer constant");
|
---|
2498 | if (lcount && *(p - 1) != ch)
|
---|
2499 | tcc_error("incorrect integer suffix: %s", p1);
|
---|
2500 | lcount++;
|
---|
2501 | ch = *p++;
|
---|
2502 | } else if (t == 'U') {
|
---|
2503 | if (ucount >= 1)
|
---|
2504 | tcc_error("two 'u's in integer constant");
|
---|
2505 | ucount++;
|
---|
2506 | ch = *p++;
|
---|
2507 | } else {
|
---|
2508 | break;
|
---|
2509 | }
|
---|
2510 | }
|
---|
2511 |
|
---|
2512 | /* Determine if it needs 64 bits and/or unsigned in order to fit */
|
---|
2513 | if (ucount == 0 && b == 10) {
|
---|
2514 | if (lcount <= (LONG_SIZE == 4)) {
|
---|
2515 | if (n >= 0x80000000U)
|
---|
2516 | lcount = (LONG_SIZE == 4) + 1;
|
---|
2517 | }
|
---|
2518 | if (n >= 0x8000000000000000ULL)
|
---|
2519 | ov = 1, ucount = 1;
|
---|
2520 | } else {
|
---|
2521 | if (lcount <= (LONG_SIZE == 4)) {
|
---|
2522 | if (n >= 0x100000000ULL)
|
---|
2523 | lcount = (LONG_SIZE == 4) + 1;
|
---|
2524 | else if (n >= 0x80000000U)
|
---|
2525 | ucount = 1;
|
---|
2526 | }
|
---|
2527 | if (n >= 0x8000000000000000ULL)
|
---|
2528 | ucount = 1;
|
---|
2529 | }
|
---|
2530 |
|
---|
2531 | if (ov)
|
---|
2532 | tcc_warning("integer constant overflow");
|
---|
2533 |
|
---|
2534 | tok = TOK_CINT;
|
---|
2535 | if (lcount) {
|
---|
2536 | tok = TOK_CLONG;
|
---|
2537 | if (lcount == 2)
|
---|
2538 | tok = TOK_CLLONG;
|
---|
2539 | }
|
---|
2540 | if (ucount)
|
---|
2541 | ++tok; /* TOK_CU... */
|
---|
2542 | tokc.i = n;
|
---|
2543 | }
|
---|
2544 | if (ch)
|
---|
2545 | tcc_error("invalid number\n");
|
---|
2546 | }
|
---|
2547 |
|
---|
2548 |
|
---|
2549 | #define PARSE2(c1, tok1, c2, tok2) \
|
---|
2550 | case c1: \
|
---|
2551 | PEEKC(c, p); \
|
---|
2552 | if (c == c2) { \
|
---|
2553 | p++; \
|
---|
2554 | tok = tok2; \
|
---|
2555 | } else { \
|
---|
2556 | tok = tok1; \
|
---|
2557 | } \
|
---|
2558 | break;
|
---|
2559 |
|
---|
2560 | /* return next token without macro substitution */
|
---|
2561 | static inline void next_nomacro1(void)
|
---|
2562 | {
|
---|
2563 | int t, c, is_long, len;
|
---|
2564 | TokenSym *ts;
|
---|
2565 | uint8_t *p, *p1;
|
---|
2566 | unsigned int h;
|
---|
2567 |
|
---|
2568 | p = file->buf_ptr;
|
---|
2569 | redo_no_start:
|
---|
2570 | c = *p;
|
---|
2571 | switch(c) {
|
---|
2572 | case ' ':
|
---|
2573 | case '\t':
|
---|
2574 | tok = c;
|
---|
2575 | p++;
|
---|
2576 | if (parse_flags & PARSE_FLAG_SPACES)
|
---|
2577 | goto keep_tok_flags;
|
---|
2578 | while (isidnum_table[*p - CH_EOF] & IS_SPC)
|
---|
2579 | ++p;
|
---|
2580 | goto redo_no_start;
|
---|
2581 | case '\f':
|
---|
2582 | case '\v':
|
---|
2583 | case '\r':
|
---|
2584 | p++;
|
---|
2585 | goto redo_no_start;
|
---|
2586 | case '\\':
|
---|
2587 | /* first look if it is in fact an end of buffer */
|
---|
2588 | c = handle_stray1(p);
|
---|
2589 | p = file->buf_ptr;
|
---|
2590 | if (c == '\\')
|
---|
2591 | goto parse_simple;
|
---|
2592 | if (c != CH_EOF)
|
---|
2593 | goto redo_no_start;
|
---|
2594 | {
|
---|
2595 | TCCState *s1 = tcc_state;
|
---|
2596 | if ((parse_flags & PARSE_FLAG_LINEFEED)
|
---|
2597 | && !(tok_flags & TOK_FLAG_EOF)) {
|
---|
2598 | tok_flags |= TOK_FLAG_EOF;
|
---|
2599 | tok = TOK_LINEFEED;
|
---|
2600 | goto keep_tok_flags;
|
---|
2601 | } else if (!(parse_flags & PARSE_FLAG_PREPROCESS)) {
|
---|
2602 | tok = TOK_EOF;
|
---|
2603 | } else if (s1->ifdef_stack_ptr != file->ifdef_stack_ptr) {
|
---|
2604 | tcc_error("missing #endif");
|
---|
2605 | } else if (s1->include_stack_ptr == s1->include_stack) {
|
---|
2606 | /* no include left : end of file. */
|
---|
2607 | tok = TOK_EOF;
|
---|
2608 | } else {
|
---|
2609 | tok_flags &= ~TOK_FLAG_EOF;
|
---|
2610 | /* pop include file */
|
---|
2611 |
|
---|
2612 | /* test if previous '#endif' was after a #ifdef at
|
---|
2613 | start of file */
|
---|
2614 | if (tok_flags & TOK_FLAG_ENDIF) {
|
---|
2615 | #ifdef INC_DEBUG
|
---|
2616 | printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
|
---|
2617 | #endif
|
---|
2618 | search_cached_include(s1, file->filename, 1)
|
---|
2619 | ->ifndef_macro = file->ifndef_macro_saved;
|
---|
2620 | tok_flags &= ~TOK_FLAG_ENDIF;
|
---|
2621 | }
|
---|
2622 |
|
---|
2623 | /* add end of include file debug info */
|
---|
2624 | if (tcc_state->do_debug) {
|
---|
2625 | put_stabd(N_EINCL, 0, 0);
|
---|
2626 | }
|
---|
2627 | /* pop include stack */
|
---|
2628 | tcc_close();
|
---|
2629 | s1->include_stack_ptr--;
|
---|
2630 | p = file->buf_ptr;
|
---|
2631 | if (p == file->buffer)
|
---|
2632 | tok_flags = TOK_FLAG_BOF|TOK_FLAG_BOL;
|
---|
2633 | goto redo_no_start;
|
---|
2634 | }
|
---|
2635 | }
|
---|
2636 | break;
|
---|
2637 |
|
---|
2638 | case '\n':
|
---|
2639 | file->line_num++;
|
---|
2640 | tok_flags |= TOK_FLAG_BOL;
|
---|
2641 | p++;
|
---|
2642 | maybe_newline:
|
---|
2643 | if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
|
---|
2644 | goto redo_no_start;
|
---|
2645 | tok = TOK_LINEFEED;
|
---|
2646 | goto keep_tok_flags;
|
---|
2647 |
|
---|
2648 | case '#':
|
---|
2649 | /* XXX: simplify */
|
---|
2650 | PEEKC(c, p);
|
---|
2651 | if ((tok_flags & TOK_FLAG_BOL) &&
|
---|
2652 | (parse_flags & PARSE_FLAG_PREPROCESS)) {
|
---|
2653 | file->buf_ptr = p;
|
---|
2654 | preprocess(tok_flags & TOK_FLAG_BOF);
|
---|
2655 | p = file->buf_ptr;
|
---|
2656 | goto maybe_newline;
|
---|
2657 | } else {
|
---|
2658 | if (c == '#') {
|
---|
2659 | p++;
|
---|
2660 | tok = TOK_TWOSHARPS;
|
---|
2661 | } else {
|
---|
2662 | if (parse_flags & PARSE_FLAG_ASM_FILE) {
|
---|
2663 | p = parse_line_comment(p - 1);
|
---|
2664 | goto redo_no_start;
|
---|
2665 | } else {
|
---|
2666 | tok = '#';
|
---|
2667 | }
|
---|
2668 | }
|
---|
2669 | }
|
---|
2670 | break;
|
---|
2671 |
|
---|
2672 | /* dollar is allowed to start identifiers when not parsing asm */
|
---|
2673 | case '$':
|
---|
2674 | if (!(isidnum_table[c - CH_EOF] & IS_ID)
|
---|
2675 | || (parse_flags & PARSE_FLAG_ASM_FILE))
|
---|
2676 | goto parse_simple;
|
---|
2677 |
|
---|
2678 | case 'a': case 'b': case 'c': case 'd':
|
---|
2679 | case 'e': case 'f': case 'g': case 'h':
|
---|
2680 | case 'i': case 'j': case 'k': case 'l':
|
---|
2681 | case 'm': case 'n': case 'o': case 'p':
|
---|
2682 | case 'q': case 'r': case 's': case 't':
|
---|
2683 | case 'u': case 'v': case 'w': case 'x':
|
---|
2684 | case 'y': case 'z':
|
---|
2685 | case 'A': case 'B': case 'C': case 'D':
|
---|
2686 | case 'E': case 'F': case 'G': case 'H':
|
---|
2687 | case 'I': case 'J': case 'K':
|
---|
2688 | case 'M': case 'N': case 'O': case 'P':
|
---|
2689 | case 'Q': case 'R': case 'S': case 'T':
|
---|
2690 | case 'U': case 'V': case 'W': case 'X':
|
---|
2691 | case 'Y': case 'Z':
|
---|
2692 | case '_':
|
---|
2693 | parse_ident_fast:
|
---|
2694 | p1 = p;
|
---|
2695 | h = TOK_HASH_INIT;
|
---|
2696 | h = TOK_HASH_FUNC(h, c);
|
---|
2697 | while (c = *++p, isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
|
---|
2698 | h = TOK_HASH_FUNC(h, c);
|
---|
2699 | len = p - p1;
|
---|
2700 | if (c != '\\') {
|
---|
2701 | TokenSym **pts;
|
---|
2702 |
|
---|
2703 | /* fast case : no stray found, so we have the full token
|
---|
2704 | and we have already hashed it */
|
---|
2705 | h &= (TOK_HASH_SIZE - 1);
|
---|
2706 | pts = &hash_ident[h];
|
---|
2707 | for(;;) {
|
---|
2708 | ts = *pts;
|
---|
2709 | if (!ts)
|
---|
2710 | break;
|
---|
2711 | if (ts->len == len && !memcmp(ts->str, p1, len))
|
---|
2712 | goto token_found;
|
---|
2713 | pts = &(ts->hash_next);
|
---|
2714 | }
|
---|
2715 | ts = tok_alloc_new(pts, (char *) p1, len);
|
---|
2716 | token_found: ;
|
---|
2717 | } else {
|
---|
2718 | /* slower case */
|
---|
2719 | cstr_reset(&tokcstr);
|
---|
2720 | cstr_cat(&tokcstr, (char *) p1, len);
|
---|
2721 | p--;
|
---|
2722 | PEEKC(c, p);
|
---|
2723 | parse_ident_slow:
|
---|
2724 | while (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
|
---|
2725 | {
|
---|
2726 | cstr_ccat(&tokcstr, c);
|
---|
2727 | PEEKC(c, p);
|
---|
2728 | }
|
---|
2729 | ts = tok_alloc(tokcstr.data, tokcstr.size);
|
---|
2730 | }
|
---|
2731 | tok = ts->tok;
|
---|
2732 | break;
|
---|
2733 | case 'L':
|
---|
2734 | t = p[1];
|
---|
2735 | if (t != '\\' && t != '\'' && t != '\"') {
|
---|
2736 | /* fast case */
|
---|
2737 | goto parse_ident_fast;
|
---|
2738 | } else {
|
---|
2739 | PEEKC(c, p);
|
---|
2740 | if (c == '\'' || c == '\"') {
|
---|
2741 | is_long = 1;
|
---|
2742 | goto str_const;
|
---|
2743 | } else {
|
---|
2744 | cstr_reset(&tokcstr);
|
---|
2745 | cstr_ccat(&tokcstr, 'L');
|
---|
2746 | goto parse_ident_slow;
|
---|
2747 | }
|
---|
2748 | }
|
---|
2749 | break;
|
---|
2750 |
|
---|
2751 | case '0': case '1': case '2': case '3':
|
---|
2752 | case '4': case '5': case '6': case '7':
|
---|
2753 | case '8': case '9':
|
---|
2754 | t = c;
|
---|
2755 | PEEKC(c, p);
|
---|
2756 | /* after the first digit, accept digits, alpha, '.' or sign if
|
---|
2757 | prefixed by 'eEpP' */
|
---|
2758 | parse_num:
|
---|
2759 | cstr_reset(&tokcstr);
|
---|
2760 | for(;;) {
|
---|
2761 | cstr_ccat(&tokcstr, t);
|
---|
2762 | if (!((isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))
|
---|
2763 | || c == '.'
|
---|
2764 | || ((c == '+' || c == '-')
|
---|
2765 | && (((t == 'e' || t == 'E')
|
---|
2766 | && !(parse_flags & PARSE_FLAG_ASM_FILE
|
---|
2767 | /* 0xe+1 is 3 tokens in asm */
|
---|
2768 | && ((char*)tokcstr.data)[0] == '0'
|
---|
2769 | && toup(((char*)tokcstr.data)[1]) == 'X'))
|
---|
2770 | || t == 'p' || t == 'P'))))
|
---|
2771 | break;
|
---|
2772 | t = c;
|
---|
2773 | PEEKC(c, p);
|
---|
2774 | }
|
---|
2775 | /* We add a trailing '\0' to ease parsing */
|
---|
2776 | cstr_ccat(&tokcstr, '\0');
|
---|
2777 | tokc.str.size = tokcstr.size;
|
---|
2778 | tokc.str.data = tokcstr.data;
|
---|
2779 | tok = TOK_PPNUM;
|
---|
2780 | break;
|
---|
2781 |
|
---|
2782 | case '.':
|
---|
2783 | /* special dot handling because it can also start a number */
|
---|
2784 | PEEKC(c, p);
|
---|
2785 | if (isnum(c)) {
|
---|
2786 | t = '.';
|
---|
2787 | goto parse_num;
|
---|
2788 | } else if ((isidnum_table['.' - CH_EOF] & IS_ID)
|
---|
2789 | && (isidnum_table[c - CH_EOF] & (IS_ID|IS_NUM))) {
|
---|
2790 | *--p = c = '.';
|
---|
2791 | goto parse_ident_fast;
|
---|
2792 | } else if (c == '.') {
|
---|
2793 | PEEKC(c, p);
|
---|
2794 | if (c == '.') {
|
---|
2795 | p++;
|
---|
2796 | tok = TOK_DOTS;
|
---|
2797 | } else {
|
---|
2798 | *--p = '.'; /* may underflow into file->unget[] */
|
---|
2799 | tok = '.';
|
---|
2800 | }
|
---|
2801 | } else {
|
---|
2802 | tok = '.';
|
---|
2803 | }
|
---|
2804 | break;
|
---|
2805 | case '\'':
|
---|
2806 | case '\"':
|
---|
2807 | is_long = 0;
|
---|
2808 | str_const:
|
---|
2809 | cstr_reset(&tokcstr);
|
---|
2810 | if (is_long)
|
---|
2811 | cstr_ccat(&tokcstr, 'L');
|
---|
2812 | cstr_ccat(&tokcstr, c);
|
---|
2813 | p = parse_pp_string(p, c, &tokcstr);
|
---|
2814 | cstr_ccat(&tokcstr, c);
|
---|
2815 | cstr_ccat(&tokcstr, '\0');
|
---|
2816 | tokc.str.size = tokcstr.size;
|
---|
2817 | tokc.str.data = tokcstr.data;
|
---|
2818 | tok = TOK_PPSTR;
|
---|
2819 | break;
|
---|
2820 |
|
---|
2821 | case '<':
|
---|
2822 | PEEKC(c, p);
|
---|
2823 | if (c == '=') {
|
---|
2824 | p++;
|
---|
2825 | tok = TOK_LE;
|
---|
2826 | } else if (c == '<') {
|
---|
2827 | PEEKC(c, p);
|
---|
2828 | if (c == '=') {
|
---|
2829 | p++;
|
---|
2830 | tok = TOK_A_SHL;
|
---|
2831 | } else {
|
---|
2832 | tok = TOK_SHL;
|
---|
2833 | }
|
---|
2834 | } else {
|
---|
2835 | tok = TOK_LT;
|
---|
2836 | }
|
---|
2837 | break;
|
---|
2838 | case '>':
|
---|
2839 | PEEKC(c, p);
|
---|
2840 | if (c == '=') {
|
---|
2841 | p++;
|
---|
2842 | tok = TOK_GE;
|
---|
2843 | } else if (c == '>') {
|
---|
2844 | PEEKC(c, p);
|
---|
2845 | if (c == '=') {
|
---|
2846 | p++;
|
---|
2847 | tok = TOK_A_SAR;
|
---|
2848 | } else {
|
---|
2849 | tok = TOK_SAR;
|
---|
2850 | }
|
---|
2851 | } else {
|
---|
2852 | tok = TOK_GT;
|
---|
2853 | }
|
---|
2854 | break;
|
---|
2855 |
|
---|
2856 | case '&':
|
---|
2857 | PEEKC(c, p);
|
---|
2858 | if (c == '&') {
|
---|
2859 | p++;
|
---|
2860 | tok = TOK_LAND;
|
---|
2861 | } else if (c == '=') {
|
---|
2862 | p++;
|
---|
2863 | tok = TOK_A_AND;
|
---|
2864 | } else {
|
---|
2865 | tok = '&';
|
---|
2866 | }
|
---|
2867 | break;
|
---|
2868 |
|
---|
2869 | case '|':
|
---|
2870 | PEEKC(c, p);
|
---|
2871 | if (c == '|') {
|
---|
2872 | p++;
|
---|
2873 | tok = TOK_LOR;
|
---|
2874 | } else if (c == '=') {
|
---|
2875 | p++;
|
---|
2876 | tok = TOK_A_OR;
|
---|
2877 | } else {
|
---|
2878 | tok = '|';
|
---|
2879 | }
|
---|
2880 | break;
|
---|
2881 |
|
---|
2882 | case '+':
|
---|
2883 | PEEKC(c, p);
|
---|
2884 | if (c == '+') {
|
---|
2885 | p++;
|
---|
2886 | tok = TOK_INC;
|
---|
2887 | } else if (c == '=') {
|
---|
2888 | p++;
|
---|
2889 | tok = TOK_A_ADD;
|
---|
2890 | } else {
|
---|
2891 | tok = '+';
|
---|
2892 | }
|
---|
2893 | break;
|
---|
2894 |
|
---|
2895 | case '-':
|
---|
2896 | PEEKC(c, p);
|
---|
2897 | if (c == '-') {
|
---|
2898 | p++;
|
---|
2899 | tok = TOK_DEC;
|
---|
2900 | } else if (c == '=') {
|
---|
2901 | p++;
|
---|
2902 | tok = TOK_A_SUB;
|
---|
2903 | } else if (c == '>') {
|
---|
2904 | p++;
|
---|
2905 | tok = TOK_ARROW;
|
---|
2906 | } else {
|
---|
2907 | tok = '-';
|
---|
2908 | }
|
---|
2909 | break;
|
---|
2910 |
|
---|
2911 | PARSE2('!', '!', '=', TOK_NE)
|
---|
2912 | PARSE2('=', '=', '=', TOK_EQ)
|
---|
2913 | PARSE2('*', '*', '=', TOK_A_MUL)
|
---|
2914 | PARSE2('%', '%', '=', TOK_A_MOD)
|
---|
2915 | PARSE2('^', '^', '=', TOK_A_XOR)
|
---|
2916 |
|
---|
2917 | /* comments or operator */
|
---|
2918 | case '/':
|
---|
2919 | PEEKC(c, p);
|
---|
2920 | if (c == '*') {
|
---|
2921 | p = parse_comment(p);
|
---|
2922 | /* comments replaced by a blank */
|
---|
2923 | tok = ' ';
|
---|
2924 | goto keep_tok_flags;
|
---|
2925 | } else if (c == '/') {
|
---|
2926 | p = parse_line_comment(p);
|
---|
2927 | tok = ' ';
|
---|
2928 | goto keep_tok_flags;
|
---|
2929 | } else if (c == '=') {
|
---|
2930 | p++;
|
---|
2931 | tok = TOK_A_DIV;
|
---|
2932 | } else {
|
---|
2933 | tok = '/';
|
---|
2934 | }
|
---|
2935 | break;
|
---|
2936 |
|
---|
2937 | /* simple tokens */
|
---|
2938 | case '(':
|
---|
2939 | case ')':
|
---|
2940 | case '[':
|
---|
2941 | case ']':
|
---|
2942 | case '{':
|
---|
2943 | case '}':
|
---|
2944 | case ',':
|
---|
2945 | case ';':
|
---|
2946 | case ':':
|
---|
2947 | case '?':
|
---|
2948 | case '~':
|
---|
2949 | case '@': /* only used in assembler */
|
---|
2950 | parse_simple:
|
---|
2951 | tok = c;
|
---|
2952 | p++;
|
---|
2953 | break;
|
---|
2954 | default:
|
---|
2955 | if (c >= 0x80 && c <= 0xFF) /* utf8 identifiers */
|
---|
2956 | goto parse_ident_fast;
|
---|
2957 | if (parse_flags & PARSE_FLAG_ASM_FILE)
|
---|
2958 | goto parse_simple;
|
---|
2959 | tcc_error("unrecognized character \\x%02x", c);
|
---|
2960 | break;
|
---|
2961 | }
|
---|
2962 | tok_flags = 0;
|
---|
2963 | keep_tok_flags:
|
---|
2964 | file->buf_ptr = p;
|
---|
2965 | #if defined(PARSE_DEBUG)
|
---|
2966 | printf("token = %d %s\n", tok, get_tok_str(tok, &tokc));
|
---|
2967 | #endif
|
---|
2968 | }
|
---|
2969 |
|
---|
2970 | /* return next token without macro substitution. Can read input from
|
---|
2971 | macro_ptr buffer */
|
---|
2972 | static void next_nomacro_spc(void)
|
---|
2973 | {
|
---|
2974 | if (macro_ptr) {
|
---|
2975 | redo:
|
---|
2976 | tok = *macro_ptr;
|
---|
2977 | if (tok) {
|
---|
2978 | TOK_GET(&tok, ¯o_ptr, &tokc);
|
---|
2979 | if (tok == TOK_LINENUM) {
|
---|
2980 | file->line_num = tokc.i;
|
---|
2981 | goto redo;
|
---|
2982 | }
|
---|
2983 | }
|
---|
2984 | } else {
|
---|
2985 | next_nomacro1();
|
---|
2986 | }
|
---|
2987 | //printf("token = %s\n", get_tok_str(tok, &tokc));
|
---|
2988 | }
|
---|
2989 |
|
---|
2990 | ST_FUNC void next_nomacro(void)
|
---|
2991 | {
|
---|
2992 | do {
|
---|
2993 | next_nomacro_spc();
|
---|
2994 | } while (tok < 256 && (isidnum_table[tok - CH_EOF] & IS_SPC));
|
---|
2995 | }
|
---|
2996 |
|
---|
2997 |
|
---|
2998 | static void macro_subst(
|
---|
2999 | TokenString *tok_str,
|
---|
3000 | Sym **nested_list,
|
---|
3001 | const int *macro_str
|
---|
3002 | );
|
---|
3003 |
|
---|
3004 | /* substitute arguments in replacement lists in macro_str by the values in
|
---|
3005 | args (field d) and return allocated string */
|
---|
3006 | static int *macro_arg_subst(Sym **nested_list, const int *macro_str, Sym *args)
|
---|
3007 | {
|
---|
3008 | int t, t0, t1, spc;
|
---|
3009 | const int *st;
|
---|
3010 | Sym *s;
|
---|
3011 | CValue cval;
|
---|
3012 | TokenString str;
|
---|
3013 | CString cstr;
|
---|
3014 |
|
---|
3015 | tok_str_new(&str);
|
---|
3016 | t0 = t1 = 0;
|
---|
3017 | while(1) {
|
---|
3018 | TOK_GET(&t, ¯o_str, &cval);
|
---|
3019 | if (!t)
|
---|
3020 | break;
|
---|
3021 | if (t == '#') {
|
---|
3022 | /* stringize */
|
---|
3023 | TOK_GET(&t, ¯o_str, &cval);
|
---|
3024 | if (!t)
|
---|
3025 | goto bad_stringy;
|
---|
3026 | s = sym_find2(args, t);
|
---|
3027 | if (s) {
|
---|
3028 | cstr_new(&cstr);
|
---|
3029 | cstr_ccat(&cstr, '\"');
|
---|
3030 | st = s->d;
|
---|
3031 | spc = 0;
|
---|
3032 | while (*st >= 0) {
|
---|
3033 | TOK_GET(&t, &st, &cval);
|
---|
3034 | if (t != TOK_PLCHLDR
|
---|
3035 | && t != TOK_NOSUBST
|
---|
3036 | && 0 == check_space(t, &spc)) {
|
---|
3037 | const char *s = get_tok_str(t, &cval);
|
---|
3038 | while (*s) {
|
---|
3039 | if (t == TOK_PPSTR && *s != '\'')
|
---|
3040 | add_char(&cstr, *s);
|
---|
3041 | else
|
---|
3042 | cstr_ccat(&cstr, *s);
|
---|
3043 | ++s;
|
---|
3044 | }
|
---|
3045 | }
|
---|
3046 | }
|
---|
3047 | cstr.size -= spc;
|
---|
3048 | cstr_ccat(&cstr, '\"');
|
---|
3049 | cstr_ccat(&cstr, '\0');
|
---|
3050 | #ifdef PP_DEBUG
|
---|
3051 | printf("\nstringize: <%s>\n", (char *)cstr.data);
|
---|
3052 | #endif
|
---|
3053 | /* add string */
|
---|
3054 | cval.str.size = cstr.size;
|
---|
3055 | cval.str.data = cstr.data;
|
---|
3056 | tok_str_add2(&str, TOK_PPSTR, &cval);
|
---|
3057 | cstr_free(&cstr);
|
---|
3058 | } else {
|
---|
3059 | bad_stringy:
|
---|
3060 | expect("macro parameter after '#'");
|
---|
3061 | }
|
---|
3062 | } else if (t >= TOK_IDENT) {
|
---|
3063 | s = sym_find2(args, t);
|
---|
3064 | if (s) {
|
---|
3065 | int l0 = str.len;
|
---|
3066 | st = s->d;
|
---|
3067 | /* if '##' is present before or after, no arg substitution */
|
---|
3068 | if (*macro_str == TOK_PPJOIN || t1 == TOK_PPJOIN) {
|
---|
3069 | /* special case for var arg macros : ## eats the ','
|
---|
3070 | if empty VA_ARGS variable. */
|
---|
3071 | if (t1 == TOK_PPJOIN && t0 == ',' && gnu_ext && s->type.t) {
|
---|
3072 | if (*st <= 0) {
|
---|
3073 | /* suppress ',' '##' */
|
---|
3074 | str.len -= 2;
|
---|
3075 | } else {
|
---|
3076 | /* suppress '##' and add variable */
|
---|
3077 | str.len--;
|
---|
3078 | goto add_var;
|
---|
3079 | }
|
---|
3080 | }
|
---|
3081 | } else {
|
---|
3082 | add_var:
|
---|
3083 | if (!s->next) {
|
---|
3084 | /* Expand arguments tokens and store them. In most
|
---|
3085 | cases we could also re-expand each argument if
|
---|
3086 | used multiple times, but not if the argument
|
---|
3087 | contains the __COUNTER__ macro. */
|
---|
3088 | TokenString str2;
|
---|
3089 | sym_push2(&s->next, s->v, s->type.t, 0);
|
---|
3090 | tok_str_new(&str2);
|
---|
3091 | macro_subst(&str2, nested_list, st);
|
---|
3092 | tok_str_add(&str2, 0);
|
---|
3093 | s->next->d = str2.str;
|
---|
3094 | }
|
---|
3095 | st = s->next->d;
|
---|
3096 | }
|
---|
3097 | for(;;) {
|
---|
3098 | int t2;
|
---|
3099 | TOK_GET(&t2, &st, &cval);
|
---|
3100 | if (t2 <= 0)
|
---|
3101 | break;
|
---|
3102 | tok_str_add2(&str, t2, &cval);
|
---|
3103 | }
|
---|
3104 | if (str.len == l0) /* expanded to empty string */
|
---|
3105 | tok_str_add(&str, TOK_PLCHLDR);
|
---|
3106 | } else {
|
---|
3107 | tok_str_add(&str, t);
|
---|
3108 | }
|
---|
3109 | } else {
|
---|
3110 | tok_str_add2(&str, t, &cval);
|
---|
3111 | }
|
---|
3112 | t0 = t1, t1 = t;
|
---|
3113 | }
|
---|
3114 | tok_str_add(&str, 0);
|
---|
3115 | return str.str;
|
---|
3116 | }
|
---|
3117 |
|
---|
3118 | static char const ab_month_name[12][4] =
|
---|
3119 | {
|
---|
3120 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
3121 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
3122 | };
|
---|
3123 |
|
---|
3124 | static int paste_tokens(int t1, CValue *v1, int t2, CValue *v2)
|
---|
3125 | {
|
---|
3126 | CString cstr;
|
---|
3127 | int n, ret = 1;
|
---|
3128 |
|
---|
3129 | cstr_new(&cstr);
|
---|
3130 | if (t1 != TOK_PLCHLDR)
|
---|
3131 | cstr_cat(&cstr, get_tok_str(t1, v1), -1);
|
---|
3132 | n = cstr.size;
|
---|
3133 | if (t2 != TOK_PLCHLDR)
|
---|
3134 | cstr_cat(&cstr, get_tok_str(t2, v2), -1);
|
---|
3135 | cstr_ccat(&cstr, '\0');
|
---|
3136 |
|
---|
3137 | tcc_open_bf(tcc_state, ":paste:", cstr.size);
|
---|
3138 | memcpy(file->buffer, cstr.data, cstr.size);
|
---|
3139 | tok_flags = 0;
|
---|
3140 | for (;;) {
|
---|
3141 | next_nomacro1();
|
---|
3142 | if (0 == *file->buf_ptr)
|
---|
3143 | break;
|
---|
3144 | if (is_space(tok))
|
---|
3145 | continue;
|
---|
3146 | tcc_warning("pasting \"%.*s\" and \"%s\" does not give a valid"
|
---|
3147 | " preprocessing token", n, cstr.data, (char*)cstr.data + n);
|
---|
3148 | ret = 0;
|
---|
3149 | break;
|
---|
3150 | }
|
---|
3151 | tcc_close();
|
---|
3152 | //printf("paste <%s>\n", (char*)cstr.data);
|
---|
3153 | cstr_free(&cstr);
|
---|
3154 | return ret;
|
---|
3155 | }
|
---|
3156 |
|
---|
3157 | /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
|
---|
3158 | return the resulting string (which must be freed). */
|
---|
3159 | static inline int *macro_twosharps(const int *ptr0)
|
---|
3160 | {
|
---|
3161 | int t;
|
---|
3162 | CValue cval;
|
---|
3163 | TokenString macro_str1;
|
---|
3164 | int start_of_nosubsts = -1;
|
---|
3165 | const int *ptr;
|
---|
3166 |
|
---|
3167 | /* we search the first '##' */
|
---|
3168 | for (ptr = ptr0;;) {
|
---|
3169 | TOK_GET(&t, &ptr, &cval);
|
---|
3170 | if (t == TOK_PPJOIN)
|
---|
3171 | break;
|
---|
3172 | if (t == 0)
|
---|
3173 | return NULL;
|
---|
3174 | }
|
---|
3175 |
|
---|
3176 | tok_str_new(¯o_str1);
|
---|
3177 |
|
---|
3178 | //tok_print(" $$$", ptr0);
|
---|
3179 | for (ptr = ptr0;;) {
|
---|
3180 | TOK_GET(&t, &ptr, &cval);
|
---|
3181 | if (t == 0)
|
---|
3182 | break;
|
---|
3183 | if (t == TOK_PPJOIN)
|
---|
3184 | continue;
|
---|
3185 | while (*ptr == TOK_PPJOIN) {
|
---|
3186 | int t1; CValue cv1;
|
---|
3187 | /* given 'a##b', remove nosubsts preceding 'a' */
|
---|
3188 | if (start_of_nosubsts >= 0)
|
---|
3189 | macro_str1.len = start_of_nosubsts;
|
---|
3190 | /* given 'a##b', remove nosubsts preceding 'b' */
|
---|
3191 | while ((t1 = *++ptr) == TOK_NOSUBST)
|
---|
3192 | ;
|
---|
3193 | if (t1 && t1 != TOK_PPJOIN) {
|
---|
3194 | TOK_GET(&t1, &ptr, &cv1);
|
---|
3195 | if (t != TOK_PLCHLDR || t1 != TOK_PLCHLDR) {
|
---|
3196 | if (paste_tokens(t, &cval, t1, &cv1)) {
|
---|
3197 | t = tok, cval = tokc;
|
---|
3198 | } else {
|
---|
3199 | tok_str_add2(¯o_str1, t, &cval);
|
---|
3200 | t = t1, cval = cv1;
|
---|
3201 | }
|
---|
3202 | }
|
---|
3203 | }
|
---|
3204 | }
|
---|
3205 | if (t == TOK_NOSUBST) {
|
---|
3206 | if (start_of_nosubsts < 0)
|
---|
3207 | start_of_nosubsts = macro_str1.len;
|
---|
3208 | } else {
|
---|
3209 | start_of_nosubsts = -1;
|
---|
3210 | }
|
---|
3211 | tok_str_add2(¯o_str1, t, &cval);
|
---|
3212 | }
|
---|
3213 | tok_str_add(¯o_str1, 0);
|
---|
3214 | //tok_print(" ###", macro_str1.str);
|
---|
3215 | return macro_str1.str;
|
---|
3216 | }
|
---|
3217 |
|
---|
3218 | /* peek or read [ws_str == NULL] next token from function macro call,
|
---|
3219 | walking up macro levels up to the file if necessary */
|
---|
3220 | static int next_argstream(Sym **nested_list, TokenString *ws_str)
|
---|
3221 | {
|
---|
3222 | int t;
|
---|
3223 | const int *p;
|
---|
3224 | Sym *sa;
|
---|
3225 |
|
---|
3226 | for (;;) {
|
---|
3227 | if (macro_ptr) {
|
---|
3228 | p = macro_ptr, t = *p;
|
---|
3229 | if (ws_str) {
|
---|
3230 | while (is_space(t) || TOK_LINEFEED == t || TOK_PLCHLDR == t)
|
---|
3231 | tok_str_add(ws_str, t), t = *++p;
|
---|
3232 | }
|
---|
3233 | if (t == 0) {
|
---|
3234 | end_macro();
|
---|
3235 | /* also, end of scope for nested defined symbol */
|
---|
3236 | sa = *nested_list;
|
---|
3237 | while (sa && sa->v == 0)
|
---|
3238 | sa = sa->prev;
|
---|
3239 | if (sa)
|
---|
3240 | sa->v = 0;
|
---|
3241 | continue;
|
---|
3242 | }
|
---|
3243 | } else {
|
---|
3244 | ch = handle_eob();
|
---|
3245 | if (ws_str) {
|
---|
3246 | while (is_space(ch) || ch == '\n' || ch == '/') {
|
---|
3247 | if (ch == '/') {
|
---|
3248 | int c;
|
---|
3249 | uint8_t *p = file->buf_ptr;
|
---|
3250 | PEEKC(c, p);
|
---|
3251 | if (c == '*') {
|
---|
3252 | p = parse_comment(p);
|
---|
3253 | file->buf_ptr = p - 1;
|
---|
3254 | } else if (c == '/') {
|
---|
3255 | p = parse_line_comment(p);
|
---|
3256 | file->buf_ptr = p - 1;
|
---|
3257 | } else
|
---|
3258 | break;
|
---|
3259 | ch = ' ';
|
---|
3260 | }
|
---|
3261 | if (ch == '\n')
|
---|
3262 | file->line_num++;
|
---|
3263 | if (!(ch == '\f' || ch == '\v' || ch == '\r'))
|
---|
3264 | tok_str_add(ws_str, ch);
|
---|
3265 | cinp();
|
---|
3266 | }
|
---|
3267 | }
|
---|
3268 | t = ch;
|
---|
3269 | }
|
---|
3270 |
|
---|
3271 | if (ws_str)
|
---|
3272 | return t;
|
---|
3273 | next_nomacro_spc();
|
---|
3274 | return tok;
|
---|
3275 | }
|
---|
3276 | }
|
---|
3277 |
|
---|
3278 | /* do macro substitution of current token with macro 's' and add
|
---|
3279 | result to (tok_str,tok_len). 'nested_list' is the list of all
|
---|
3280 | macros we got inside to avoid recursing. Return non zero if no
|
---|
3281 | substitution needs to be done */
|
---|
3282 | static int macro_subst_tok(
|
---|
3283 | TokenString *tok_str,
|
---|
3284 | Sym **nested_list,
|
---|
3285 | Sym *s)
|
---|
3286 | {
|
---|
3287 | Sym *args, *sa, *sa1;
|
---|
3288 | int parlevel, t, t1, spc;
|
---|
3289 | TokenString str;
|
---|
3290 | char *cstrval;
|
---|
3291 | CValue cval;
|
---|
3292 | CString cstr;
|
---|
3293 | char buf[32];
|
---|
3294 |
|
---|
3295 | /* if symbol is a macro, prepare substitution */
|
---|
3296 | /* special macros */
|
---|
3297 | if (tok == TOK___LINE__ || tok == TOK___COUNTER__) {
|
---|
3298 | t = tok == TOK___LINE__ ? file->line_num : pp_counter++;
|
---|
3299 | snprintf(buf, sizeof(buf), "%d", t);
|
---|
3300 | cstrval = buf;
|
---|
3301 | t1 = TOK_PPNUM;
|
---|
3302 | goto add_cstr1;
|
---|
3303 | } else if (tok == TOK___FILE__) {
|
---|
3304 | cstrval = file->filename;
|
---|
3305 | goto add_cstr;
|
---|
3306 | } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
|
---|
3307 | time_t ti;
|
---|
3308 | struct tm *tm;
|
---|
3309 |
|
---|
3310 | time(&ti);
|
---|
3311 | tm = localtime(&ti);
|
---|
3312 | if (tok == TOK___DATE__) {
|
---|
3313 | snprintf(buf, sizeof(buf), "%s %2d %d",
|
---|
3314 | ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
|
---|
3315 | } else {
|
---|
3316 | snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
|
---|
3317 | tm->tm_hour, tm->tm_min, tm->tm_sec);
|
---|
3318 | }
|
---|
3319 | cstrval = buf;
|
---|
3320 | add_cstr:
|
---|
3321 | t1 = TOK_STR;
|
---|
3322 | add_cstr1:
|
---|
3323 | cstr_new(&cstr);
|
---|
3324 | cstr_cat(&cstr, cstrval, 0);
|
---|
3325 | cval.str.size = cstr.size;
|
---|
3326 | cval.str.data = cstr.data;
|
---|
3327 | tok_str_add2(tok_str, t1, &cval);
|
---|
3328 | cstr_free(&cstr);
|
---|
3329 | } else if (s->d) {
|
---|
3330 | int saved_parse_flags = parse_flags;
|
---|
3331 | int *joined_str = NULL;
|
---|
3332 | int *mstr = s->d;
|
---|
3333 |
|
---|
3334 | if (s->type.t == MACRO_FUNC) {
|
---|
3335 | /* whitespace between macro name and argument list */
|
---|
3336 | TokenString ws_str;
|
---|
3337 | tok_str_new(&ws_str);
|
---|
3338 |
|
---|
3339 | spc = 0;
|
---|
3340 | parse_flags |= PARSE_FLAG_SPACES | PARSE_FLAG_LINEFEED
|
---|
3341 | | PARSE_FLAG_ACCEPT_STRAYS;
|
---|
3342 |
|
---|
3343 | /* get next token from argument stream */
|
---|
3344 | t = next_argstream(nested_list, &ws_str);
|
---|
3345 | if (t != '(') {
|
---|
3346 | /* not a macro substitution after all, restore the
|
---|
3347 | * macro token plus all whitespace we've read.
|
---|
3348 | * whitespace is intentionally not merged to preserve
|
---|
3349 | * newlines. */
|
---|
3350 | parse_flags = saved_parse_flags;
|
---|
3351 | tok_str_add(tok_str, tok);
|
---|
3352 | if (parse_flags & PARSE_FLAG_SPACES) {
|
---|
3353 | int i;
|
---|
3354 | for (i = 0; i < ws_str.len; i++)
|
---|
3355 | tok_str_add(tok_str, ws_str.str[i]);
|
---|
3356 | }
|
---|
3357 | tok_str_free_str(ws_str.str);
|
---|
3358 | return 0;
|
---|
3359 | } else {
|
---|
3360 | tok_str_free_str(ws_str.str);
|
---|
3361 | }
|
---|
3362 | do {
|
---|
3363 | next_nomacro(); /* eat '(' */
|
---|
3364 | } while (tok == TOK_PLCHLDR);
|
---|
3365 |
|
---|
3366 | /* argument macro */
|
---|
3367 | args = NULL;
|
---|
3368 | sa = s->next;
|
---|
3369 | /* NOTE: empty args are allowed, except if no args */
|
---|
3370 | for(;;) {
|
---|
3371 | do {
|
---|
3372 | next_argstream(nested_list, NULL);
|
---|
3373 | } while (is_space(tok) || TOK_LINEFEED == tok);
|
---|
3374 | empty_arg:
|
---|
3375 | /* handle '()' case */
|
---|
3376 | if (!args && !sa && tok == ')')
|
---|
3377 | break;
|
---|
3378 | if (!sa)
|
---|
3379 | tcc_error("macro '%s' used with too many args",
|
---|
3380 | get_tok_str(s->v, 0));
|
---|
3381 | tok_str_new(&str);
|
---|
3382 | parlevel = spc = 0;
|
---|
3383 | /* NOTE: non zero sa->t indicates VA_ARGS */
|
---|
3384 | while ((parlevel > 0 ||
|
---|
3385 | (tok != ')' &&
|
---|
3386 | (tok != ',' || sa->type.t)))) {
|
---|
3387 | if (tok == TOK_EOF || tok == 0)
|
---|
3388 | break;
|
---|
3389 | if (tok == '(')
|
---|
3390 | parlevel++;
|
---|
3391 | else if (tok == ')')
|
---|
3392 | parlevel--;
|
---|
3393 | if (tok == TOK_LINEFEED)
|
---|
3394 | tok = ' ';
|
---|
3395 | if (!check_space(tok, &spc))
|
---|
3396 | tok_str_add2(&str, tok, &tokc);
|
---|
3397 | next_argstream(nested_list, NULL);
|
---|
3398 | }
|
---|
3399 | if (parlevel)
|
---|
3400 | expect(")");
|
---|
3401 | str.len -= spc;
|
---|
3402 | tok_str_add(&str, -1);
|
---|
3403 | tok_str_add(&str, 0);
|
---|
3404 | sa1 = sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, 0);
|
---|
3405 | sa1->d = str.str;
|
---|
3406 | sa = sa->next;
|
---|
3407 | if (tok == ')') {
|
---|
3408 | /* special case for gcc var args: add an empty
|
---|
3409 | var arg argument if it is omitted */
|
---|
3410 | if (sa && sa->type.t && gnu_ext)
|
---|
3411 | goto empty_arg;
|
---|
3412 | break;
|
---|
3413 | }
|
---|
3414 | if (tok != ',')
|
---|
3415 | expect(",");
|
---|
3416 | }
|
---|
3417 | if (sa) {
|
---|
3418 | tcc_error("macro '%s' used with too few args",
|
---|
3419 | get_tok_str(s->v, 0));
|
---|
3420 | }
|
---|
3421 |
|
---|
3422 | parse_flags = saved_parse_flags;
|
---|
3423 |
|
---|
3424 | /* now subst each arg */
|
---|
3425 | mstr = macro_arg_subst(nested_list, mstr, args);
|
---|
3426 | /* free memory */
|
---|
3427 | sa = args;
|
---|
3428 | while (sa) {
|
---|
3429 | sa1 = sa->prev;
|
---|
3430 | tok_str_free_str(sa->d);
|
---|
3431 | if (sa->next) {
|
---|
3432 | tok_str_free_str(sa->next->d);
|
---|
3433 | sym_free(sa->next);
|
---|
3434 | }
|
---|
3435 | sym_free(sa);
|
---|
3436 | sa = sa1;
|
---|
3437 | }
|
---|
3438 | }
|
---|
3439 |
|
---|
3440 | sym_push2(nested_list, s->v, 0, 0);
|
---|
3441 | parse_flags = saved_parse_flags;
|
---|
3442 | joined_str = macro_twosharps(mstr);
|
---|
3443 | macro_subst(tok_str, nested_list, joined_str ? joined_str : mstr);
|
---|
3444 |
|
---|
3445 | /* pop nested defined symbol */
|
---|
3446 | sa1 = *nested_list;
|
---|
3447 | *nested_list = sa1->prev;
|
---|
3448 | sym_free(sa1);
|
---|
3449 | if (joined_str)
|
---|
3450 | tok_str_free_str(joined_str);
|
---|
3451 | if (mstr != s->d)
|
---|
3452 | tok_str_free_str(mstr);
|
---|
3453 | }
|
---|
3454 | return 0;
|
---|
3455 | }
|
---|
3456 |
|
---|
3457 | /* do macro substitution of macro_str and add result to
|
---|
3458 | (tok_str,tok_len). 'nested_list' is the list of all macros we got
|
---|
3459 | inside to avoid recursing. */
|
---|
3460 | static void macro_subst(
|
---|
3461 | TokenString *tok_str,
|
---|
3462 | Sym **nested_list,
|
---|
3463 | const int *macro_str
|
---|
3464 | )
|
---|
3465 | {
|
---|
3466 | Sym *s;
|
---|
3467 | int t, spc, nosubst;
|
---|
3468 | CValue cval;
|
---|
3469 |
|
---|
3470 | spc = nosubst = 0;
|
---|
3471 |
|
---|
3472 | while (1) {
|
---|
3473 | TOK_GET(&t, ¯o_str, &cval);
|
---|
3474 | if (t <= 0)
|
---|
3475 | break;
|
---|
3476 |
|
---|
3477 | if (t >= TOK_IDENT && 0 == nosubst) {
|
---|
3478 | s = define_find(t);
|
---|
3479 | if (s == NULL)
|
---|
3480 | goto no_subst;
|
---|
3481 |
|
---|
3482 | /* if nested substitution, do nothing */
|
---|
3483 | if (sym_find2(*nested_list, t)) {
|
---|
3484 | /* and mark it as TOK_NOSUBST, so it doesn't get subst'd again */
|
---|
3485 | tok_str_add2(tok_str, TOK_NOSUBST, NULL);
|
---|
3486 | goto no_subst;
|
---|
3487 | }
|
---|
3488 |
|
---|
3489 | {
|
---|
3490 | TokenString str;
|
---|
3491 | str.str = (int*)macro_str;
|
---|
3492 | begin_macro(&str, 2);
|
---|
3493 |
|
---|
3494 | tok = t;
|
---|
3495 | macro_subst_tok(tok_str, nested_list, s);
|
---|
3496 |
|
---|
3497 | if (str.alloc == 3) {
|
---|
3498 | /* already finished by reading function macro arguments */
|
---|
3499 | break;
|
---|
3500 | }
|
---|
3501 |
|
---|
3502 | macro_str = macro_ptr;
|
---|
3503 | end_macro ();
|
---|
3504 | }
|
---|
3505 | if (tok_str->len)
|
---|
3506 | spc = is_space(t = tok_str->str[tok_str->lastlen]);
|
---|
3507 | } else {
|
---|
3508 | if (t == '\\' && !(parse_flags & PARSE_FLAG_ACCEPT_STRAYS))
|
---|
3509 | tcc_error("stray '\\' in program");
|
---|
3510 | no_subst:
|
---|
3511 | if (!check_space(t, &spc))
|
---|
3512 | tok_str_add2(tok_str, t, &cval);
|
---|
3513 |
|
---|
3514 | if (nosubst) {
|
---|
3515 | if (nosubst > 1 && (spc || (++nosubst == 3 && t == '(')))
|
---|
3516 | continue;
|
---|
3517 | nosubst = 0;
|
---|
3518 | }
|
---|
3519 | if (t == TOK_NOSUBST)
|
---|
3520 | nosubst = 1;
|
---|
3521 | }
|
---|
3522 | /* GCC supports 'defined' as result of a macro substitution */
|
---|
3523 | if (t == TOK_DEFINED && pp_expr)
|
---|
3524 | nosubst = 2;
|
---|
3525 | }
|
---|
3526 | }
|
---|
3527 |
|
---|
3528 | /* return next token with macro substitution */
|
---|
3529 | ST_FUNC void next(void)
|
---|
3530 | {
|
---|
3531 | redo:
|
---|
3532 | if (parse_flags & PARSE_FLAG_SPACES)
|
---|
3533 | next_nomacro_spc();
|
---|
3534 | else
|
---|
3535 | next_nomacro();
|
---|
3536 |
|
---|
3537 | if (macro_ptr) {
|
---|
3538 | if (tok == TOK_NOSUBST || tok == TOK_PLCHLDR) {
|
---|
3539 | /* discard preprocessor markers */
|
---|
3540 | goto redo;
|
---|
3541 | } else if (tok == 0) {
|
---|
3542 | /* end of macro or unget token string */
|
---|
3543 | end_macro();
|
---|
3544 | goto redo;
|
---|
3545 | }
|
---|
3546 | } else if (tok >= TOK_IDENT && (parse_flags & PARSE_FLAG_PREPROCESS)) {
|
---|
3547 | Sym *s;
|
---|
3548 | /* if reading from file, try to substitute macros */
|
---|
3549 | s = define_find(tok);
|
---|
3550 | if (s) {
|
---|
3551 | Sym *nested_list = NULL;
|
---|
3552 | tokstr_buf.len = 0;
|
---|
3553 | macro_subst_tok(&tokstr_buf, &nested_list, s);
|
---|
3554 | tok_str_add(&tokstr_buf, 0);
|
---|
3555 | begin_macro(&tokstr_buf, 2);
|
---|
3556 | goto redo;
|
---|
3557 | }
|
---|
3558 | }
|
---|
3559 | /* convert preprocessor tokens into C tokens */
|
---|
3560 | if (tok == TOK_PPNUM) {
|
---|
3561 | if (parse_flags & PARSE_FLAG_TOK_NUM)
|
---|
3562 | parse_number((char *)tokc.str.data);
|
---|
3563 | } else if (tok == TOK_PPSTR) {
|
---|
3564 | if (parse_flags & PARSE_FLAG_TOK_STR)
|
---|
3565 | parse_string((char *)tokc.str.data, tokc.str.size - 1);
|
---|
3566 | }
|
---|
3567 | }
|
---|
3568 |
|
---|
3569 | /* push back current token and set current token to 'last_tok'. Only
|
---|
3570 | identifier case handled for labels. */
|
---|
3571 | ST_INLN void unget_tok(int last_tok)
|
---|
3572 | {
|
---|
3573 |
|
---|
3574 | TokenString *str = tok_str_alloc();
|
---|
3575 | tok_str_add2(str, tok, &tokc);
|
---|
3576 | tok_str_add(str, 0);
|
---|
3577 | begin_macro(str, 1);
|
---|
3578 | tok = last_tok;
|
---|
3579 | }
|
---|
3580 |
|
---|
3581 | ST_FUNC void preprocess_start(TCCState *s1, int is_asm)
|
---|
3582 | {
|
---|
3583 | CString cstr;
|
---|
3584 | int i;
|
---|
3585 |
|
---|
3586 | s1->include_stack_ptr = s1->include_stack;
|
---|
3587 | s1->ifdef_stack_ptr = s1->ifdef_stack;
|
---|
3588 | file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
|
---|
3589 | pp_expr = 0;
|
---|
3590 | pp_counter = 0;
|
---|
3591 | pp_debug_tok = pp_debug_symv = 0;
|
---|
3592 | pp_once++;
|
---|
3593 | pvtop = vtop = vstack - 1;
|
---|
3594 | s1->pack_stack[0] = 0;
|
---|
3595 | s1->pack_stack_ptr = s1->pack_stack;
|
---|
3596 |
|
---|
3597 | set_idnum('$', s1->dollars_in_identifiers ? IS_ID : 0);
|
---|
3598 | set_idnum('.', is_asm ? IS_ID : 0);
|
---|
3599 |
|
---|
3600 | cstr_new(&cstr);
|
---|
3601 | cstr_cat(&cstr, "\"", -1);
|
---|
3602 | cstr_cat(&cstr, file->filename, -1);
|
---|
3603 | cstr_cat(&cstr, "\"", 0);
|
---|
3604 | tcc_define_symbol(s1, "__BASE_FILE__", cstr.data);
|
---|
3605 |
|
---|
3606 | cstr_reset(&cstr);
|
---|
3607 | for (i = 0; i < s1->nb_cmd_include_files; i++) {
|
---|
3608 | cstr_cat(&cstr, "#include \"", -1);
|
---|
3609 | cstr_cat(&cstr, s1->cmd_include_files[i], -1);
|
---|
3610 | cstr_cat(&cstr, "\"\n", -1);
|
---|
3611 | }
|
---|
3612 | if (cstr.size) {
|
---|
3613 | *s1->include_stack_ptr++ = file;
|
---|
3614 | tcc_open_bf(s1, "<command line>", cstr.size);
|
---|
3615 | memcpy(file->buffer, cstr.data, cstr.size);
|
---|
3616 | }
|
---|
3617 | cstr_free(&cstr);
|
---|
3618 |
|
---|
3619 | if (is_asm)
|
---|
3620 | tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
|
---|
3621 |
|
---|
3622 | parse_flags = is_asm ? PARSE_FLAG_ASM_FILE : 0;
|
---|
3623 | tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
|
---|
3624 | }
|
---|
3625 |
|
---|
3626 | /* cleanup from error/setjmp */
|
---|
3627 | ST_FUNC void preprocess_end(TCCState *s1)
|
---|
3628 | {
|
---|
3629 | while (macro_stack)
|
---|
3630 | end_macro();
|
---|
3631 | macro_ptr = NULL;
|
---|
3632 | }
|
---|
3633 |
|
---|
3634 | ST_FUNC void tccpp_new(TCCState *s)
|
---|
3635 | {
|
---|
3636 | int i, c;
|
---|
3637 | const char *p, *r;
|
---|
3638 |
|
---|
3639 | /* might be used in error() before preprocess_start() */
|
---|
3640 | s->include_stack_ptr = s->include_stack;
|
---|
3641 | s->ppfp = stdout;
|
---|
3642 |
|
---|
3643 | /* init isid table */
|
---|
3644 | for(i = CH_EOF; i<128; i++)
|
---|
3645 | set_idnum(i,
|
---|
3646 | is_space(i) ? IS_SPC
|
---|
3647 | : isid(i) ? IS_ID
|
---|
3648 | : isnum(i) ? IS_NUM
|
---|
3649 | : 0);
|
---|
3650 |
|
---|
3651 | for(i = 128; i<256; i++)
|
---|
3652 | set_idnum(i, IS_ID);
|
---|
3653 |
|
---|
3654 | /* init allocators */
|
---|
3655 | tal_new(&toksym_alloc, TOKSYM_TAL_LIMIT, TOKSYM_TAL_SIZE);
|
---|
3656 | tal_new(&tokstr_alloc, TOKSTR_TAL_LIMIT, TOKSTR_TAL_SIZE);
|
---|
3657 | tal_new(&cstr_alloc, CSTR_TAL_LIMIT, CSTR_TAL_SIZE);
|
---|
3658 |
|
---|
3659 | memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
|
---|
3660 | cstr_new(&cstr_buf);
|
---|
3661 | cstr_realloc(&cstr_buf, STRING_MAX_SIZE);
|
---|
3662 | tok_str_new(&tokstr_buf);
|
---|
3663 | tok_str_realloc(&tokstr_buf, TOKSTR_MAX_SIZE);
|
---|
3664 |
|
---|
3665 | tok_ident = TOK_IDENT;
|
---|
3666 | p = tcc_keywords;
|
---|
3667 | while (*p) {
|
---|
3668 | r = p;
|
---|
3669 | for(;;) {
|
---|
3670 | c = *r++;
|
---|
3671 | if (c == '\0')
|
---|
3672 | break;
|
---|
3673 | }
|
---|
3674 | tok_alloc(p, r - p - 1);
|
---|
3675 | p = r;
|
---|
3676 | }
|
---|
3677 | }
|
---|
3678 |
|
---|
3679 | ST_FUNC void tccpp_delete(TCCState *s)
|
---|
3680 | {
|
---|
3681 | int i, n;
|
---|
3682 |
|
---|
3683 | /* free -D and compiler defines */
|
---|
3684 | free_defines(NULL);
|
---|
3685 |
|
---|
3686 | /* free tokens */
|
---|
3687 | n = tok_ident - TOK_IDENT;
|
---|
3688 | for(i = 0; i < n; i++)
|
---|
3689 | tal_free(toksym_alloc, table_ident[i]);
|
---|
3690 | tcc_free(table_ident);
|
---|
3691 | table_ident = NULL;
|
---|
3692 |
|
---|
3693 | /* free static buffers */
|
---|
3694 | cstr_free(&tokcstr);
|
---|
3695 | cstr_free(&cstr_buf);
|
---|
3696 | cstr_free(¯o_equal_buf);
|
---|
3697 | tok_str_free_str(tokstr_buf.str);
|
---|
3698 |
|
---|
3699 | /* free allocators */
|
---|
3700 | tal_delete(toksym_alloc);
|
---|
3701 | toksym_alloc = NULL;
|
---|
3702 | tal_delete(tokstr_alloc);
|
---|
3703 | tokstr_alloc = NULL;
|
---|
3704 | tal_delete(cstr_alloc);
|
---|
3705 | cstr_alloc = NULL;
|
---|
3706 | }
|
---|
3707 |
|
---|
3708 | /* ------------------------------------------------------------------------- */
|
---|
3709 | /* tcc -E [-P[1]] [-dD} support */
|
---|
3710 |
|
---|
3711 | static void tok_print(const char *msg, const int *str)
|
---|
3712 | {
|
---|
3713 | FILE *fp;
|
---|
3714 | int t, s = 0;
|
---|
3715 | CValue cval;
|
---|
3716 |
|
---|
3717 | fp = tcc_state->ppfp;
|
---|
3718 | fprintf(fp, "%s", msg);
|
---|
3719 | while (str) {
|
---|
3720 | TOK_GET(&t, &str, &cval);
|
---|
3721 | if (!t)
|
---|
3722 | break;
|
---|
3723 | fprintf(fp, " %s" + s, get_tok_str(t, &cval)), s = 1;
|
---|
3724 | }
|
---|
3725 | fprintf(fp, "\n");
|
---|
3726 | }
|
---|
3727 |
|
---|
3728 | static void pp_line(TCCState *s1, BufferedFile *f, int level)
|
---|
3729 | {
|
---|
3730 | int d = f->line_num - f->line_ref;
|
---|
3731 |
|
---|
3732 | if (s1->dflag & 4)
|
---|
3733 | return;
|
---|
3734 |
|
---|
3735 | if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_NONE) {
|
---|
3736 | ;
|
---|
3737 | } else if (level == 0 && f->line_ref && d < 8) {
|
---|
3738 | while (d > 0)
|
---|
3739 | fputs("\n", s1->ppfp), --d;
|
---|
3740 | } else if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_STD) {
|
---|
3741 | fprintf(s1->ppfp, "#line %d \"%s\"\n", f->line_num, f->filename);
|
---|
3742 | } else {
|
---|
3743 | fprintf(s1->ppfp, "# %d \"%s\"%s\n", f->line_num, f->filename,
|
---|
3744 | level > 0 ? " 1" : level < 0 ? " 2" : "");
|
---|
3745 | }
|
---|
3746 | f->line_ref = f->line_num;
|
---|
3747 | }
|
---|
3748 |
|
---|
3749 | static void define_print(TCCState *s1, int v)
|
---|
3750 | {
|
---|
3751 | FILE *fp;
|
---|
3752 | Sym *s;
|
---|
3753 |
|
---|
3754 | s = define_find(v);
|
---|
3755 | if (NULL == s || NULL == s->d)
|
---|
3756 | return;
|
---|
3757 |
|
---|
3758 | fp = s1->ppfp;
|
---|
3759 | fprintf(fp, "#define %s", get_tok_str(v, NULL));
|
---|
3760 | if (s->type.t == MACRO_FUNC) {
|
---|
3761 | Sym *a = s->next;
|
---|
3762 | fprintf(fp,"(");
|
---|
3763 | if (a)
|
---|
3764 | for (;;) {
|
---|
3765 | fprintf(fp,"%s", get_tok_str(a->v & ~SYM_FIELD, NULL));
|
---|
3766 | if (!(a = a->next))
|
---|
3767 | break;
|
---|
3768 | fprintf(fp,",");
|
---|
3769 | }
|
---|
3770 | fprintf(fp,")");
|
---|
3771 | }
|
---|
3772 | tok_print("", s->d);
|
---|
3773 | }
|
---|
3774 |
|
---|
3775 | static void pp_debug_defines(TCCState *s1)
|
---|
3776 | {
|
---|
3777 | int v, t;
|
---|
3778 | const char *vs;
|
---|
3779 | FILE *fp;
|
---|
3780 |
|
---|
3781 | t = pp_debug_tok;
|
---|
3782 | if (t == 0)
|
---|
3783 | return;
|
---|
3784 |
|
---|
3785 | file->line_num--;
|
---|
3786 | pp_line(s1, file, 0);
|
---|
3787 | file->line_ref = ++file->line_num;
|
---|
3788 |
|
---|
3789 | fp = s1->ppfp;
|
---|
3790 | v = pp_debug_symv;
|
---|
3791 | vs = get_tok_str(v, NULL);
|
---|
3792 | if (t == TOK_DEFINE) {
|
---|
3793 | define_print(s1, v);
|
---|
3794 | } else if (t == TOK_UNDEF) {
|
---|
3795 | fprintf(fp, "#undef %s\n", vs);
|
---|
3796 | } else if (t == TOK_push_macro) {
|
---|
3797 | fprintf(fp, "#pragma push_macro(\"%s\")\n", vs);
|
---|
3798 | } else if (t == TOK_pop_macro) {
|
---|
3799 | fprintf(fp, "#pragma pop_macro(\"%s\")\n", vs);
|
---|
3800 | }
|
---|
3801 | pp_debug_tok = 0;
|
---|
3802 | }
|
---|
3803 |
|
---|
3804 | static void pp_debug_builtins(TCCState *s1)
|
---|
3805 | {
|
---|
3806 | int v;
|
---|
3807 | for (v = TOK_IDENT; v < tok_ident; ++v)
|
---|
3808 | define_print(s1, v);
|
---|
3809 | }
|
---|
3810 |
|
---|
3811 | /* Add a space between tokens a and b to avoid unwanted textual pasting */
|
---|
3812 | static int pp_need_space(int a, int b)
|
---|
3813 | {
|
---|
3814 | return 'E' == a ? '+' == b || '-' == b
|
---|
3815 | : '+' == a ? TOK_INC == b || '+' == b
|
---|
3816 | : '-' == a ? TOK_DEC == b || '-' == b
|
---|
3817 | : a >= TOK_IDENT ? b >= TOK_IDENT
|
---|
3818 | : a == TOK_PPNUM ? b >= TOK_IDENT
|
---|
3819 | : 0;
|
---|
3820 | }
|
---|
3821 |
|
---|
3822 | /* maybe hex like 0x1e */
|
---|
3823 | static int pp_check_he0xE(int t, const char *p)
|
---|
3824 | {
|
---|
3825 | if (t == TOK_PPNUM && toup(strchr(p, 0)[-1]) == 'E')
|
---|
3826 | return 'E';
|
---|
3827 | return t;
|
---|
3828 | }
|
---|
3829 |
|
---|
3830 | /* Preprocess the current file */
|
---|
3831 | ST_FUNC int tcc_preprocess(TCCState *s1)
|
---|
3832 | {
|
---|
3833 | BufferedFile **iptr;
|
---|
3834 | int token_seen, spcs, level;
|
---|
3835 | const char *p;
|
---|
3836 | char white[400];
|
---|
3837 |
|
---|
3838 | parse_flags = PARSE_FLAG_PREPROCESS
|
---|
3839 | | (parse_flags & PARSE_FLAG_ASM_FILE)
|
---|
3840 | | PARSE_FLAG_LINEFEED
|
---|
3841 | | PARSE_FLAG_SPACES
|
---|
3842 | | PARSE_FLAG_ACCEPT_STRAYS
|
---|
3843 | ;
|
---|
3844 | /* Credits to Fabrice Bellard's initial revision to demonstrate its
|
---|
3845 | capability to compile and run itself, provided all numbers are
|
---|
3846 | given as decimals. tcc -E -P10 will do. */
|
---|
3847 | if (s1->Pflag == LINE_MACRO_OUTPUT_FORMAT_P10)
|
---|
3848 | parse_flags |= PARSE_FLAG_TOK_NUM, s1->Pflag = 1;
|
---|
3849 |
|
---|
3850 | #ifdef PP_BENCH
|
---|
3851 | /* for PP benchmarks */
|
---|
3852 | do next(); while (tok != TOK_EOF);
|
---|
3853 | return 0;
|
---|
3854 | #endif
|
---|
3855 |
|
---|
3856 | if (s1->dflag & 1) {
|
---|
3857 | pp_debug_builtins(s1);
|
---|
3858 | s1->dflag &= ~1;
|
---|
3859 | }
|
---|
3860 |
|
---|
3861 | token_seen = TOK_LINEFEED, spcs = 0;
|
---|
3862 | pp_line(s1, file, 0);
|
---|
3863 | for (;;) {
|
---|
3864 | iptr = s1->include_stack_ptr;
|
---|
3865 | next();
|
---|
3866 | if (tok == TOK_EOF)
|
---|
3867 | break;
|
---|
3868 |
|
---|
3869 | level = s1->include_stack_ptr - iptr;
|
---|
3870 | if (level) {
|
---|
3871 | if (level > 0)
|
---|
3872 | pp_line(s1, *iptr, 0);
|
---|
3873 | pp_line(s1, file, level);
|
---|
3874 | }
|
---|
3875 | if (s1->dflag & 7) {
|
---|
3876 | pp_debug_defines(s1);
|
---|
3877 | if (s1->dflag & 4)
|
---|
3878 | continue;
|
---|
3879 | }
|
---|
3880 |
|
---|
3881 | if (is_space(tok)) {
|
---|
3882 | if (spcs < sizeof white - 1)
|
---|
3883 | white[spcs++] = tok;
|
---|
3884 | continue;
|
---|
3885 | } else if (tok == TOK_LINEFEED) {
|
---|
3886 | spcs = 0;
|
---|
3887 | if (token_seen == TOK_LINEFEED)
|
---|
3888 | continue;
|
---|
3889 | ++file->line_ref;
|
---|
3890 | } else if (token_seen == TOK_LINEFEED) {
|
---|
3891 | pp_line(s1, file, 0);
|
---|
3892 | } else if (spcs == 0 && pp_need_space(token_seen, tok)) {
|
---|
3893 | white[spcs++] = ' ';
|
---|
3894 | }
|
---|
3895 |
|
---|
3896 | white[spcs] = 0, fputs(white, s1->ppfp), spcs = 0;
|
---|
3897 | fputs(p = get_tok_str(tok, &tokc), s1->ppfp);
|
---|
3898 | token_seen = pp_check_he0xE(tok, p);
|
---|
3899 | }
|
---|
3900 | return 0;
|
---|
3901 | }
|
---|
3902 |
|
---|
3903 | /* ------------------------------------------------------------------------- */
|
---|