Unified · Split
| @@ -34,14 +34,18 @@ · 33 unmodified lines | ||
| 34 | 34 | #define STRING_MAX_SIZE 1024 |
| 35 | 35 | #define INCLUDE_PATHS_MAX 32 |
| 36 | 36 | |
| 37 | +#define TOK_HASH_SIZE 521 | |
| 38 | +#define TOK_ALLOC_INCR 256 /* must be a power of two */ | |
| 39 | +#define SYM_HASH_SIZE 263 | |
| 40 | + | |
| 37 | 41 | /* number of available temporary registers */ |
| 38 | 42 | #define NB_REGS 3 |
| 39 | −/* defined if function parameters must be evaluated in revert order */ | |
| 43 | +/* defined if function parameters must be evaluated in reverse order */ | |
| 40 | 44 | #define INVERT_FUNC_PARAMS |
| 41 | 45 | |
| 42 | 46 | /* token symbol management */ |
| 43 | 47 | typedef struct TokenSym { |
| 44 | − struct TokenSym *next; | |
| 48 | + struct TokenSym *hash_next; | |
| 45 | 49 | int tok; /* token number */ |
| 46 | 50 | int len; |
| 47 | 51 | char str[1]; |
| @@ -54,8 +58,14 @@ typedef struct Sym { · 6 unmodified lines | ||
| 54 | 58 | int c; /* associated number */ |
| 55 | 59 | struct Sym *next; /* next related symbol */ |
| 56 | 60 | struct Sym *prev; /* prev symbol in stack */ |
| 61 | + struct Sym *hash_next; /* next symbol in hash table */ | |
| 57 | 62 | } Sym; |
| 58 | 63 | |
| 64 | +typedef struct SymStack { | |
| 65 | + struct Sym *top; | |
| 66 | + struct Sym *hash[SYM_HASH_SIZE]; | |
| 67 | +} SymStack; | |
| 68 | + | |
| 59 | 69 | #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */ |
| 60 | 70 | #define SYM_FIELD 0x20000000 /* struct/union field symbol space */ |
| 61 | 71 | |
| @@ -87,10 +97,12 @@ typedef struct { · 25 unmodified lines | ||
| 87 | 97 | FILE *file; |
| 88 | 98 | int tok, tok1, tokc, rsym, anon_sym, |
| 89 | 99 | prog, ind, loc, glo, vt, vc, const_wanted, line_num; |
| 90 | −TokenSym *first_ident; | |
| 100 | +int tok_ident; | |
| 101 | +TokenSym **table_ident; | |
| 102 | +TokenSym *hash_ident[521]; | |
| 91 | 103 | char token_buf[STRING_MAX_SIZE + 1]; |
| 92 | 104 | char *filename, *funcname; |
| 93 | −Sym *define_stack, *global_stack, *local_stack, *label_stack; | |
| 105 | +SymStack define_stack, global_stack, local_stack, label_stack; | |
| 94 | 106 | |
| 95 | 107 | int vstack[VSTACK_SIZE], *vstack_ptr; |
| 96 | 108 | int *macro_ptr, *macro_ptr_allocated; |
| @@ -283,14 +295,14 @@ void *dlsym(void *handle, char *symbol) · 186 unmodified lines | ||
| 283 | 295 | |
| 284 | 296 | #endif |
| 285 | 297 | |
| 286 | −int isid(c) | |
| 298 | +inline int isid(c) | |
| 287 | 299 | { |
| 288 | − return (c >= 'a' & c <= 'z') | | |
| 289 | − (c >= 'A' & c <= 'Z') | | |
| 300 | + return (c >= 'a' && c <= 'z') || | |
| 301 | + (c >= 'A' && c <= 'Z') || | |
| 290 | 302 | c == '_'; |
| 291 | 303 | } |
| 292 | 304 | |
| 293 | −int isnum(c) | |
| 305 | +inline int isnum(c) | |
| 294 | 306 | { |
| 295 | 307 | return c >= '0' & c <= '9'; |
| 296 | 308 | } |
| @@ -341,31 +353,40 @@ void test_lvalue() · 44 unmodified lines | ||
| 341 | 353 | |
| 342 | 354 | TokenSym *tok_alloc(char *str, int len) |
| 343 | 355 | { |
| 344 | − TokenSym *ts, **pts; | |
| 345 | − int t; | |
| 356 | + TokenSym *ts, **pts, **ptable; | |
| 357 | + int h, i; | |
| 346 | 358 | |
| 347 | 359 | if (len <= 0) |
| 348 | 360 | len = strlen(str); |
| 349 | − | |
| 350 | − t = TOK_IDENT; | |
| 351 | − pts = &first_ident; | |
| 361 | + h = 1; | |
| 362 | + for(i=0;i<len;i++) | |
| 363 | + h = ((h << 8) | (str[i] & 0xff)) % TOK_HASH_SIZE; | |
| 364 | + | |
| 365 | + pts = &hash_ident[h]; | |
| 352 | 366 | while (1) { |
| 353 | 367 | ts = *pts; |
| 354 | 368 | if (!ts) |
| 355 | 369 | break; |
| 356 | 370 | if (ts->len == len && !memcmp(ts->str, str, len)) |
| 357 | 371 | return ts; |
| 358 | − t++; | |
| 359 | − pts = &(ts->next); | |
| 372 | + pts = &(ts->hash_next); | |
| 373 | + } | |
| 374 | + /* expand token table if needed */ | |
| 375 | + i = tok_ident - TOK_IDENT; | |
| 376 | + if ((i % TOK_ALLOC_INCR) == 0) { | |
| 377 | + ptable = realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *)); | |
| 378 | + if (!ptable) | |
| 379 | + error("memory full"); | |
| 380 | + table_ident = ptable; | |
| 360 | 381 | } |
| 361 | 382 | ts = malloc(sizeof(TokenSym) + len); |
| 362 | 383 | if (!ts) |
| 363 | 384 | error("memory full"); |
| 364 | − ts->tok = t; | |
| 385 | + table_ident[i] = ts; | |
| 386 | + ts->tok = tok_ident++; | |
| 365 | 387 | ts->len = len; |
| 366 | − ts->next = NULL; | |
| 388 | + ts->hash_next = NULL; | |
| 367 | 389 | memcpy(ts->str, str, len + 1); |
| 368 | − | |
| 369 | 390 | *pts = ts; |
| 370 | 391 | return ts; |
| 371 | 392 | } |
| @@ -425,40 +446,65 @@ char *get_tok_str(int v, int c) · 53 unmodified lines | ||
| 425 | 446 | *p++ = v; |
| 426 | 447 | *p = '\0'; |
| 427 | 448 | return buf; |
| 449 | + } else if (v < tok_ident) { | |
| 450 | + return table_ident[v - TOK_IDENT]->str; | |
| 428 | 451 | } else { |
| 429 | − ts = first_ident; | |
| 430 | − while (ts != NULL) { | |
| 431 | − if (ts->tok == v) | |
| 432 | − return ts->str; | |
| 433 | − ts = ts->next; | |
| 434 | − } | |
| 452 | + /* should never happen */ | |
| 435 | 453 | return NULL; |
| 436 | 454 | } |
| 437 | 455 | } |
| 438 | 456 | |
| 457 | +/* push, without hashing */ | |
| 458 | +Sym *sym_push2(Sym **ps, int v, int t, int c) | |
| 459 | +{ | |
| 460 | + Sym *s; | |
| 461 | + s = malloc(sizeof(Sym)); | |
| 462 | + if (!s) | |
| 463 | + error("memory full"); | |
| 464 | + s->v = v; | |
| 465 | + s->t = t; | |
| 466 | + s->c = c; | |
| 467 | + s->next = NULL; | |
| 468 | + /* add in stack */ | |
| 469 | + s->prev = *ps; | |
| 470 | + *ps = s; | |
| 471 | + return s; | |
| 472 | +} | |
| 473 | + | |
| 439 | 474 | /* find a symbol and return its associated structure. 's' is the top |
| 440 | 475 | of the symbol stack */ |
| 441 | −Sym *sym_find1(Sym *s, int v) | |
| 476 | +Sym *sym_find2(Sym *s, int v) | |
| 442 | 477 | { |
| 443 | 478 | while (s) { |
| 444 | 479 | if (s->v == v) |
| 445 | 480 | return s; |
| 446 | 481 | s = s->prev; |
| 447 | 482 | } |
| 448 | − return 0; | |
| 483 | + return NULL; | |
| 449 | 484 | } |
| 450 | 485 | |
| 451 | −Sym *sym_push1(Sym **ps, int v, int t, int c) | |
| 486 | +/* find a symbol and return its associated structure. 'st' is the | |
| 487 | + symbol stack */ | |
| 488 | +Sym *sym_find1(SymStack *st, int v) | |
| 452 | 489 | { |
| 453 | 490 | Sym *s; |
| 454 | − s = malloc(sizeof(Sym)); | |
| 455 | − if (!s) | |
| 456 | − error("memory full"); | |
| 457 | − s->v = v; | |
| 458 | − s->t = t; | |
| 459 | − s->c = c; | |
| 460 | − s->next = NULL; | |
| 461 | − s->prev = *ps; | |
| 491 | + | |
| 492 | + s = st->hash[v % SYM_HASH_SIZE]; | |
| 493 | + while (s) { | |
| 494 | + if (s->v == v) | |
| 495 | + return s; | |
| 496 | + s = s->hash_next; | |
| 497 | + } | |
| 498 | + return 0; | |
| 499 | +} | |
| 500 | + | |
| 501 | +Sym *sym_push1(SymStack *st, int v, int t, int c) | |
| 502 | +{ | |
| 503 | + Sym *s, **ps; | |
| 504 | + s = sym_push2(&st->top, v, t, c); | |
| 505 | + /* add in hash table */ | |
| 506 | + ps = &st->hash[s->v % SYM_HASH_SIZE]; | |
| 507 | + s->hash_next = *ps; | |
| 462 | 508 | *ps = s; |
| 463 | 509 | return s; |
| 464 | 510 | } |
| @@ -467,33 +513,35 @@ Sym *sym_push1(Sym **ps, int v, int t, int c) · 2 unmodified lines | ||
| 467 | 513 | Sym *sym_find(int v) |
| 468 | 514 | { |
| 469 | 515 | Sym *s; |
| 470 | − s = sym_find1(local_stack, v); | |
| 516 | + s = sym_find1(&local_stack, v); | |
| 471 | 517 | if (!s) |
| 472 | − s = sym_find1(global_stack, v); | |
| 518 | + s = sym_find1(&global_stack, v); | |
| 473 | 519 | return s; |
| 474 | 520 | } |
| 475 | 521 | |
| 476 | 522 | /* push a given symbol on the symbol stack */ |
| 477 | 523 | Sym *sym_push(int v, int t, int c) |
| 478 | 524 | { |
| 479 | − if (local_stack) | |
| 525 | + if (local_stack.top) | |
| 480 | 526 | return sym_push1(&local_stack, v, t, c); |
| 481 | 527 | else |
| 482 | 528 | return sym_push1(&global_stack, v, t, c); |
| 483 | 529 | } |
| 484 | 530 | |
| 485 | 531 | /* pop symbols until top reaches 'b' */ |
| 486 | −void sym_pop(Sym **ps, Sym *b) | |
| 532 | +void sym_pop(SymStack *st, Sym *b) | |
| 487 | 533 | { |
| 488 | 534 | Sym *s, *ss; |
| 489 | 535 | |
| 490 | − s = *ps; | |
| 536 | + s = st->top; | |
| 491 | 537 | while(s != b) { |
| 492 | 538 | ss = s->prev; |
| 539 | + /* free hash table entry */ | |
| 540 | + st->hash[s->v % SYM_HASH_SIZE] = s->hash_next; | |
| 493 | 541 | free(s); |
| 494 | 542 | s = ss; |
| 495 | 543 | } |
| 496 | − *ps = b; | |
| 544 | + st->top = b; | |
| 497 | 545 | } |
| 498 | 546 | |
| 499 | 547 | int ch, ch1; |
| @@ -640,7 +688,7 @@ int expr_preprocess() · 140 unmodified lines | ||
| 640 | 688 | t = tok; |
| 641 | 689 | if (t == '(') |
| 642 | 690 | next_nomacro(); |
| 643 | − c = sym_find1(define_stack, tok) != 0; | |
| 691 | + c = sym_find1(&define_stack, tok) != 0; | |
| 644 | 692 | if (t == '(') |
| 645 | 693 | next_nomacro(); |
| 646 | 694 | tok = TOK_NUM; |
| @@ -748,7 +796,7 @@ void preprocess() · 101 unmodified lines | ||
| 748 | 796 | s->next = first; |
| 749 | 797 | } else if (tok == TOK_UNDEF) { |
| 750 | 798 | next_nomacro(); |
| 751 | − s = sym_find1(define_stack, tok); | |
| 799 | + s = sym_find1(&define_stack, tok); | |
| 752 | 800 | /* undefine symbol by putting an invalid name */ |
| 753 | 801 | if (s) |
| 754 | 802 | s->v = 0; |
| @@ -823,7 +871,7 @@ void preprocess() · 68 unmodified lines | ||
| 823 | 871 | c = 0; |
| 824 | 872 | do_ifdef: |
| 825 | 873 | next_nomacro(); |
| 826 | − c = (sym_find1(define_stack, tok) != 0) ^ c; | |
| 874 | + c = (sym_find1(&define_stack, tok) != 0) ^ c; | |
| 827 | 875 | do_if: |
| 828 | 876 | if (ifdef_stack_ptr >= ifdef_stack + IFDEF_STACK_SIZE) |
| 829 | 877 | error("memory full"); |
| @@ -1094,7 +1142,7 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 264 unmodified lines | ||
| 1094 | 1142 | t = *macro_str++; |
| 1095 | 1143 | if (!t) |
| 1096 | 1144 | break; |
| 1097 | − s = sym_find1(args, t); | |
| 1145 | + s = sym_find2(args, t); | |
| 1098 | 1146 | if (s) { |
| 1099 | 1147 | token_buf[0] = '\0'; |
| 1100 | 1148 | st = (int *)s->c; |
| @@ -1122,7 +1170,7 @@ int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 21 unmodified lines | ||
| 1122 | 1170 | } else if (t == TOK_NUM || t == TOK_CCHAR || t == TOK_STR) { |
| 1123 | 1171 | tok_add2(&str, &len, t, *macro_ptr++); |
| 1124 | 1172 | } else { |
| 1125 | − s = sym_find1(args, t); | |
| 1173 | + s = sym_find2(args, t); | |
| 1126 | 1174 | if (s) { |
| 1127 | 1175 | st = (int *)s->c; |
| 1128 | 1176 | /* if '##' is present before or after , no arg substitution */ |
| @@ -1199,7 +1247,7 @@ int *macro_twosharps(int *macro_str) · 70 unmodified lines | ||
| 1199 | 1247 | void macro_subst(int **tok_str, int *tok_len, |
| 1200 | 1248 | Sym **nested_list, int *macro_str) |
| 1201 | 1249 | { |
| 1202 | − Sym *s, *args, *sa; | |
| 1250 | + Sym *s, *args, *sa, *sa1; | |
| 1203 | 1251 | int *str, parlevel, len, *mstr, t, *saved_macro_ptr; |
| 1204 | 1252 | int mstr_allocated, *macro_str1; |
| 1205 | 1253 | |
| @@ -1228,10 +1276,10 @@ void macro_subst(int **tok_str, int *tok_len, · 22 unmodified lines | ||
| 1228 | 1276 | } else if (tok == TOK___TIME__) { |
| 1229 | 1277 | tok_add2(tok_str, tok_len, TOK_STR, |
| 1230 | 1278 | (int)tok_alloc("00:00:00", 0)); |
| 1231 | − } else if ((s = sym_find1(define_stack, tok)) != NULL) { | |
| 1279 | + } else if ((s = sym_find1(&define_stack, tok)) != NULL) { | |
| 1232 | 1280 | /* if symbol is a macro, prepare substitution */ |
| 1233 | 1281 | /* if nested substitution, do nothing */ |
| 1234 | − if (sym_find1(*nested_list, tok)) | |
| 1282 | + if (sym_find2(*nested_list, tok)) | |
| 1235 | 1283 | goto no_subst; |
| 1236 | 1284 | mstr = (int *)s->c; |
| 1237 | 1285 | mstr_allocated = 0; |
| @@ -1270,7 +1318,7 @@ void macro_subst(int **tok_str, int *tok_len, · 32 unmodified lines | ||
| 1270 | 1318 | next_nomacro(); |
| 1271 | 1319 | } |
| 1272 | 1320 | tok_add(&str, &len, 0); |
| 1273 | − sym_push1(&args, sa->v & ~SYM_FIELD, 0, (int)str); | |
| 1321 | + sym_push2(&args, sa->v & ~SYM_FIELD, 0, (int)str); | |
| 1274 | 1322 | if (tok != ',') |
| 1275 | 1323 | break; |
| 1276 | 1324 | next_nomacro(); |
| @@ -1283,15 +1331,19 @@ void macro_subst(int **tok_str, int *tok_len, · 6 unmodified lines | ||
| 1283 | 1331 | /* free memory */ |
| 1284 | 1332 | sa = args; |
| 1285 | 1333 | while (sa) { |
| 1334 | + sa1 = sa->prev; | |
| 1286 | 1335 | free((int *)sa->c); |
| 1287 | − sa = sa->prev; | |
| 1336 | + free(sa); | |
| 1337 | + sa = sa1; | |
| 1288 | 1338 | } |
| 1289 | − sym_pop(&args, NULL); | |
| 1290 | 1339 | mstr_allocated = 1; |
| 1291 | 1340 | } |
| 1292 | − sym_push1(nested_list, s->v, 0, 0); | |
| 1341 | + sym_push2(nested_list, s->v, 0, 0); | |
| 1293 | 1342 | macro_subst(tok_str, tok_len, nested_list, mstr); |
| 1294 | − sym_pop(nested_list, (*nested_list)->prev); | |
| 1343 | + /* pop nested defined symbol */ | |
| 1344 | + sa1 = *nested_list; | |
| 1345 | + *nested_list = sa1->prev; | |
| 1346 | + free(sa1); | |
| 1295 | 1347 | if (mstr_allocated) |
| 1296 | 1348 | free(mstr); |
| 1297 | 1349 | } else { |
| @@ -2425,7 +2477,7 @@ void unary() · 1127 unmodified lines | ||
| 2425 | 2477 | #ifdef INVERT_FUNC_PARAMS |
| 2426 | 2478 | { |
| 2427 | 2479 | int *str, len, parlevel, *saved_macro_ptr; |
| 2428 | − Sym *args; | |
| 2480 | + Sym *args, *s1; | |
| 2429 | 2481 | |
| 2430 | 2482 | /* read each argument and store it on a stack */ |
| 2431 | 2483 | /* XXX: merge it with macro args ? */ |
| @@ -2445,7 +2497,7 @@ void unary() · 13 unmodified lines | ||
| 2445 | 2497 | } |
| 2446 | 2498 | tok_add(&str, &len, -1); /* end of file added */ |
| 2447 | 2499 | tok_add(&str, &len, 0); |
| 2448 | − sym_push1(&args, 0, 0, (int)str); | |
| 2500 | + sym_push2(&args, 0, 0, (int)str); | |
| 2449 | 2501 | if (tok != ',') |
| 2450 | 2502 | break; |
| 2451 | 2503 | next(); |
| @@ -2462,8 +2514,10 @@ void unary() · 10 unmodified lines | ||
| 2462 | 2514 | next(); |
| 2463 | 2515 | expr_eq(); |
| 2464 | 2516 | gfunc_param(); |
| 2517 | + s1 = args->prev; | |
| 2465 | 2518 | free((int *)args->c); |
| 2466 | − sym_pop(&args, args->prev); | |
| 2519 | + free(args); | |
| 2520 | + args = s1; | |
| 2467 | 2521 | } |
| 2468 | 2522 | macro_ptr = saved_macro_ptr; |
| 2469 | 2523 | /* restore token */ |
| @@ -2692,7 +2746,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 222 unmodified lines | ||
| 2692 | 2746 | } else if (tok == '{') { |
| 2693 | 2747 | next(); |
| 2694 | 2748 | /* declarations */ |
| 2695 | − s = local_stack; | |
| 2749 | + s = local_stack.top; | |
| 2696 | 2750 | while (tok != '}') { |
| 2697 | 2751 | decl(VT_LOCAL); |
| 2698 | 2752 | block(bsym, csym, case_sym, def_sym, case_reg); |
| @@ -2810,7 +2864,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 111 unmodified lines | ||
| 2810 | 2864 | } else |
| 2811 | 2865 | if (tok == TOK_GOTO) { |
| 2812 | 2866 | next(); |
| 2813 | − s = sym_find1(label_stack, tok); | |
| 2867 | + s = sym_find1(&label_stack, tok); | |
| 2814 | 2868 | /* put forward definition if needed */ |
| 2815 | 2869 | if (!s) |
| 2816 | 2870 | s = sym_push1(&label_stack, tok, VT_FORWARD, 0); |
| @@ -2827,7 +2881,7 @@ void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) · 10 unmodified lines | ||
| 2827 | 2881 | if (tok == ':') { |
| 2828 | 2882 | next(); |
| 2829 | 2883 | /* label case */ |
| 2830 | − s = sym_find1(label_stack, b); | |
| 2884 | + s = sym_find1(&label_stack, b); | |
| 2831 | 2885 | if (s) { |
| 2832 | 2886 | if (!(s->t & VT_FORWARD)) |
| 2833 | 2887 | error("multiple defined label"); |
| @@ -2988,7 +3042,6 @@ void decl_assign(int t, int c, int first) · 154 unmodified lines | ||
| 2988 | 3042 | } else { |
| 2989 | 3043 | if ((t & VT_VALMASK) == VT_CONST) { |
| 2990 | 3044 | v = expr_const(); |
| 2991 | − printf("v=%d\n", v); | |
| 2992 | 3045 | if (t & VT_BYTE) |
| 2993 | 3046 | *(char *)c = v; |
| 2994 | 3047 | else if (t & VT_SHORT) |
| @@ -3135,11 +3188,20 @@ void open_dll(char *libname) · 140 unmodified lines | ||
| 3135 | 3188 | error((char *)dlerror()); |
| 3136 | 3189 | } |
| 3137 | 3190 | |
| 3191 | +/* output a binary file (for testing) */ | |
| 3192 | +void build_exe(char *filename) | |
| 3193 | +{ | |
| 3194 | + FILE *f; | |
| 3195 | + f = fopen(filename, "w"); | |
| 3196 | + fwrite((void *)prog, 1, ind - prog, f); | |
| 3197 | + fclose(f); | |
| 3198 | +} | |
| 3199 | + | |
| 3138 | 3200 | int main(int argc, char **argv) |
| 3139 | 3201 | { |
| 3140 | 3202 | Sym *s; |
| 3141 | 3203 | int (*t)(); |
| 3142 | − char *p, *r; | |
| 3204 | + char *p, *r, *outfile; | |
| 3143 | 3205 | int optind; |
| 3144 | 3206 | |
| 3145 | 3207 | include_paths[0] = "/usr/include"; |
| @@ -3148,6 +3210,7 @@ int main(int argc, char **argv) · 2 unmodified lines | ||
| 3148 | 3210 | nb_include_paths = 3; |
| 3149 | 3211 | |
| 3150 | 3212 | /* add all tokens */ |
| 3213 | + tok_ident = TOK_IDENT; | |
| 3151 | 3214 | p = "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0register\0signed\0auto\0inline\0restrict\0float\0double\0short\0struct\0union\0typedef\0default\0enum\0sizeof\0define\0include\0ifdef\0ifndef\0elif\0endif\0defined\0undef\0error\0line\0__LINE__\0__FILE__\0__DATE__\0__TIME__\0__VA_ARGS__\0__func__\0main\0"; |
| 3152 | 3215 | while (*p) { |
| 3153 | 3216 | r = p; |
| @@ -3163,8 +3226,10 @@ int main(int argc, char **argv) · 9 unmodified lines | ||
| 3163 | 3226 | #endif |
| 3164 | 3227 | |
| 3165 | 3228 | optind = 1; |
| 3229 | + outfile = NULL; | |
| 3166 | 3230 | while (1) { |
| 3167 | 3231 | if (optind >= argc) { |
| 3232 | + show_help: | |
| 3168 | 3233 | printf("tcc version 0.9 - Tiny C Compiler - Copyright (C) 2001 Fabrice Bellard\n" |
| 3169 | 3234 | "usage: tcc [-Idir] [-Dsym] [-llib] infile [infile_arg...]\n"); |
| 3170 | 3235 | return 1; |
| @@ -3172,6 +3237,7 @@ int main(int argc, char **argv) · 1 unmodified line | ||
| 3172 | 3237 | r = argv[optind]; |
| 3173 | 3238 | if (r[0] != '-') |
| 3174 | 3239 | break; |
| 3240 | + optind++; | |
| 3175 | 3241 | if (r[1] == 'I') { |
| 3176 | 3242 | if (nb_include_paths >= INCLUDE_PATHS_MAX) |
| 3177 | 3243 | error("too many include paths"); |
| @@ -3180,11 +3246,15 @@ int main(int argc, char **argv) · 2 unmodified lines | ||
| 3180 | 3246 | define_symbol(r + 2); |
| 3181 | 3247 | } else if (r[1] == 'l') { |
| 3182 | 3248 | open_dll(r + 2); |
| 3249 | + } else if (r[1] == 'o') { | |
| 3250 | + /* currently, only for testing, so not documented */ | |
| 3251 | + if (optind >= argc) | |
| 3252 | + goto show_help; | |
| 3253 | + outfile = argv[optind++]; | |
| 3183 | 3254 | } else { |
| 3184 | 3255 | fprintf(stderr, "invalid option -- '%s'\n", r); |
| 3185 | 3256 | exit(1); |
| 3186 | 3257 | } |
| 3187 | − optind++; | |
| 3188 | 3258 | } |
| 3189 | 3259 | |
| 3190 | 3260 | filename = argv[optind]; |
| @@ -3210,23 +3280,18 @@ int main(int argc, char **argv) · 19 unmodified lines | ||
| 3210 | 3280 | decl(VT_CONST); |
| 3211 | 3281 | if (tok != -1) |
| 3212 | 3282 | expect("declaration"); |
| 3213 | −#ifdef TEST | |
| 3214 | − { | |
| 3215 | − FILE *f; | |
| 3216 | − f = fopen(argv[optind + 1], "w"); | |
| 3217 | − fwrite((void *)prog, 1, ind - prog, f); | |
| 3218 | − fclose(f); | |
| 3283 | + if (outfile) { | |
| 3284 | + build_exe(outfile); | |
| 3219 | 3285 | return 0; |
| 3220 | − } | |
| 3221 | −#else | |
| 3222 | − s = sym_find(TOK_MAIN); | |
| 3223 | − if (!s) | |
| 3224 | − error("main() not defined"); | |
| 3225 | − t = (int (*)())s->c; | |
| 3286 | + } else { | |
| 3287 | + s = sym_find(TOK_MAIN); | |
| 3288 | + if (!s) | |
| 3289 | + error("main() not defined"); | |
| 3290 | + t = (int (*)())s->c; | |
| 3226 | 3291 | #ifdef PROFILE |
| 3227 | − return 1; | |
| 3292 | + return 1; | |
| 3228 | 3293 | #else |
| 3229 | − return (*t)(argc - optind, argv + optind); | |
| 3230 | −#endif | |
| 3294 | + return (*t)(argc - optind, argv + optind); | |
| 3231 | 3295 | #endif |
| 3296 | + } | |
| 3232 | 3297 | } |