Unified · Split
| @@ -42,7 +42,7 @@ · 41 unmodified lines | |||
| 42 | /* preprocessor debug */ | 42 | /* preprocessor debug */ |
| 43 | //#define PP_DEBUG | 43 | //#define PP_DEBUG |
| 44 | 44 | ||
| 45 | /* amount of virtual memory associate to a section (currently, we do | 45 | /* amount of virtual memory associated to a section (currently, we do |
| 46 | not realloc them) */ | 46 | not realloc them) */ |
| 47 | #define SECTION_VSIZE (1024 * 1024) | 47 | #define SECTION_VSIZE (1024 * 1024) |
| 48 | 48 | ||
| @@ -1306,57 +1306,66 @@ void tok_print(int *str) · 1257 unmodified lines | |||
| 1306 | } | 1306 | } |
| 1307 | #endif | 1307 | #endif |
| 1308 | 1308 | ||
| 1309 | /* parse after #define */ | ||
| 1310 | void parse_define(void) | ||
| 1311 | { | ||
| 1312 | Sym *s, *first, **ps; | ||
| 1313 | int v, t, *str, len; | ||
| 1314 | |||
| 1315 | v = tok; | ||
| 1316 | /* XXX: should check if same macro (ANSI) */ | ||
| 1317 | first = NULL; | ||
| 1318 | t = MACRO_OBJ; | ||
| 1319 | /* '(' must be just after macro definition for MACRO_FUNC */ | ||
| 1320 | if (ch == '(') { | ||
| 1321 | next_nomacro(); | ||
| 1322 | next_nomacro(); | ||
| 1323 | ps = &first; | ||
| 1324 | while (tok != ')') { | ||
| 1325 | if (tok == TOK_DOTS) | ||
| 1326 | tok = TOK___VA_ARGS__; | ||
| 1327 | s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0); | ||
| 1328 | *ps = s; | ||
| 1329 | ps = &s->next; | ||
| 1330 | next_nomacro(); | ||
| 1331 | if (tok != ',') | ||
| 1332 | break; | ||
| 1333 | next_nomacro(); | ||
| 1334 | } | ||
| 1335 | t = MACRO_FUNC; | ||
| 1336 | } | ||
| 1337 | str = NULL; | ||
| 1338 | len = 0; | ||
| 1339 | while (1) { | ||
| 1340 | skip_spaces(); | ||
| 1341 | if (ch == '\n' || ch == -1) | ||
| 1342 | break; | ||
| 1343 | next_nomacro(); | ||
| 1344 | tok_add2(&str, &len, tok, &tokc); | ||
| 1345 | } | ||
| 1346 | tok_add(&str, &len, 0); | ||
| 1347 | #ifdef PP_DEBUG | ||
| 1348 | printf("define %s %d: ", get_tok_str(v, NULL), t); | ||
| 1349 | tok_print(str); | ||
| 1350 | #endif | ||
| 1351 | s = sym_push1(&define_stack, v, t, (int)str); | ||
| 1352 | s->next = first; | ||
| 1353 | } | ||
| 1354 | |||
| 1309 | void preprocess(void) | 1355 | void preprocess(void) |
| 1310 | { | 1356 | { |
| 1311 | int size, i, c, v, t, *str, len; | 1357 | int size, i, c; |
| 1312 | char buf[1024], *q, *p; | 1358 | char buf[1024], *q, *p; |
| 1313 | char buf1[1024]; | 1359 | char buf1[1024]; |
| 1314 | BufferedFile *f; | 1360 | BufferedFile *f; |
| 1315 | Sym **ps, *first, *s; | 1361 | Sym *s; |
| 1316 | 1362 | ||
| 1317 | cinp(); | 1363 | cinp(); |
| 1318 | next_nomacro(); | 1364 | next_nomacro(); |
| 1319 | redo: | 1365 | redo: |
| 1320 | if (tok == TOK_DEFINE) { | 1366 | if (tok == TOK_DEFINE) { |
| 1321 | next_nomacro(); | 1367 | next_nomacro(); |
| 1322 | v = tok; | 1368 | parse_define(); |
| 1323 | /* XXX: should check if same macro (ANSI) */ | ||
| 1324 | first = NULL; | ||
| 1325 | t = MACRO_OBJ; | ||
| 1326 | /* '(' must be just after macro definition for MACRO_FUNC */ | ||
| 1327 | if (ch == '(') { | ||
| 1328 | next_nomacro(); | ||
| 1329 | next_nomacro(); | ||
| 1330 | ps = &first; | ||
| 1331 | while (tok != ')') { | ||
| 1332 | if (tok == TOK_DOTS) | ||
| 1333 | tok = TOK___VA_ARGS__; | ||
| 1334 | s = sym_push1(&define_stack, tok | SYM_FIELD, 0, 0); | ||
| 1335 | *ps = s; | ||
| 1336 | ps = &s->next; | ||
| 1337 | next_nomacro(); | ||
| 1338 | if (tok != ',') | ||
| 1339 | break; | ||
| 1340 | next_nomacro(); | ||
| 1341 | } | ||
| 1342 | t = MACRO_FUNC; | ||
| 1343 | } | ||
| 1344 | str = NULL; | ||
| 1345 | len = 0; | ||
| 1346 | while (1) { | ||
| 1347 | skip_spaces(); | ||
| 1348 | if (ch == '\n' || ch == -1) | ||
| 1349 | break; | ||
| 1350 | next_nomacro(); | ||
| 1351 | tok_add2(&str, &len, tok, &tokc); | ||
| 1352 | } | ||
| 1353 | tok_add(&str, &len, 0); | ||
| 1354 | #ifdef PP_DEBUG | ||
| 1355 | printf("define %s %d: ", get_tok_str(v, NULL), t); | ||
| 1356 | tok_print(str); | ||
| 1357 | #endif | ||
| 1358 | s = sym_push1(&define_stack, v, t, (int)str); | ||
| 1359 | s->next = first; | ||
| 1360 | } else if (tok == TOK_UNDEF) { | 1369 | } else if (tok == TOK_UNDEF) { |
| 1361 | next_nomacro(); | 1370 | next_nomacro(); |
| 1362 | s = sym_find1(&define_stack, tok); | 1371 | s = sym_find1(&define_stack, tok); |
| @@ -5675,46 +5684,40 @@ int tcc_compile_file(const char *filename1) · 4312 unmodified lines | |||
| 5675 | tcc parser, but would need a custom 'FILE *' */ | 5684 | tcc parser, but would need a custom 'FILE *' */ |
| 5676 | void define_symbol(const char *sym) | 5685 | void define_symbol(const char *sym) |
| 5677 | { | 5686 | { |
| 5678 | TokenSym *ts; | 5687 | char *p; |
| 5679 | int *str, len; | 5688 | BufferedFile bf1, *bf = &bf1; |
| 5680 | CValue cval; | ||
| 5681 | const char *p; | ||
| 5682 | char buf[256]; | ||
| 5683 | 5689 | ||
| 5684 | p = strchr(sym, '='); | 5690 | pstrcpy(bf->buffer, IO_BUF_SIZE, sym); |
| 5691 | p = strchr(bf->buffer, '='); | ||
| 5685 | if (!p) { | 5692 | if (!p) { |
| 5686 | pstrcpy(buf, sizeof(buf), sym); | 5693 | /* default value */ |
| 5687 | p = "1"; | 5694 | pstrcat(bf->buffer, IO_BUF_SIZE, " 1"); |
| 5688 | } else { | 5695 | } else { |
| 5689 | len = p - sym; | 5696 | *p = ' '; |
| 5690 | if (len > sizeof(buf) - 1) | ||
| 5691 | len = sizeof(buf) - 1; | ||
| 5692 | memcpy(buf, sym, len); | ||
| 5693 | buf[len] = '\0'; | ||
| 5694 | p++; | ||
| 5695 | } | 5697 | } |
| 5696 | 5698 | ||
| 5697 | ts = tok_alloc(buf, 0); | 5699 | /* init file structure */ |
| 5698 | str = NULL; | 5700 | bf->fd = -1; |
| 5699 | len = 0; | 5701 | bf->buf_ptr = bf->buffer; |
| 5700 | if (isnum(*p)) { | 5702 | bf->buf_end = bf->buffer + strlen(bf->buffer); |
| 5701 | /* integer case */ | 5703 | bf->filename[0] = '\0'; |
| 5702 | cval.i = atoi(p); | 5704 | bf->line_num = 1; |
| 5703 | tok_add2(&str, &len, TOK_CINT, &cval); | 5705 | file = bf; |
| 5704 | } else { | 5706 | |
| 5705 | /* string case */ | 5707 | include_stack_ptr = include_stack; |
| 5706 | cval.ts = tok_alloc(p, 0); | 5708 | |
| 5707 | tok_add2(&str, &len, TOK_STR, &cval); | 5709 | /* parse with define parser */ |
| 5708 | } | 5710 | inp(); |
| 5709 | tok_add(&str, &len, 0); | 5711 | ch = '\n'; /* needed to parse correctly first preprocessor command */ |
| 5710 | sym_push1(&define_stack, ts->tok, MACRO_OBJ, (int)str); | 5712 | next_nomacro(); |
| 5713 | parse_define(); | ||
| 5714 | file = NULL; | ||
| 5711 | } | 5715 | } |
| 5712 | 5716 | ||
| 5713 | void undef_symbol(const char *sym) | 5717 | void undef_symbol(const char *sym) |
| 5714 | { | 5718 | { |
| 5715 | TokenSym *ts; | 5719 | TokenSym *ts; |
| 5716 | Sym *s; | 5720 | Sym *s; |
| 5717 | printf("undef %s\n", sym); | ||
| 5718 | ts = tok_alloc(sym, 0); | 5721 | ts = tok_alloc(sym, 0); |
| 5719 | s = sym_find1(&define_stack, tok); | 5722 | s = sym_find1(&define_stack, tok); |
| 5720 | /* undefine symbol by putting an invalid name */ | 5723 | /* undefine symbol by putting an invalid name */ |