Unified · Split
| @@ -53,19 +53,24 @@ · 52 unmodified lines | |||
| 53 | 53 | ||
| 54 | //#define MEM_DEBUG | 54 | //#define MEM_DEBUG |
| 55 | 55 | ||
| 56 | /* assembler debug */ | ||
| 57 | //#define ASM_DEBUG | ||
| 58 | |||
| 56 | /* target selection */ | 59 | /* target selection */ |
| 57 | //#define TCC_TARGET_I386 /* i386 code generator */ | 60 | //#define TCC_TARGET_I386 /* i386 code generator */ |
| 58 | //#define TCC_TARGET_IL /* .NET CLI generator */ | ||
| 59 | 61 | ||
| 60 | /* default target is I386 */ | 62 | /* default target is I386 */ |
| 61 | #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_IL) | 63 | #if !defined(TCC_TARGET_I386) |
| 62 | #define TCC_TARGET_I386 | 64 | #define TCC_TARGET_I386 |
| 63 | #endif | 65 | #endif |
| 64 | 66 | ||
| 65 | #if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL) | 67 | #if !defined(WIN32) && !defined(TCC_UCLIBC) |
| 66 | #define CONFIG_TCC_BCHECK /* enable bound checking code */ | 68 | #define CONFIG_TCC_BCHECK /* enable bound checking code */ |
| 67 | #endif | 69 | #endif |
| 68 | 70 | ||
| 71 | /* define it to include assembler support */ | ||
| 72 | #define CONFIG_TCC_ASM | ||
| 73 | |||
| 69 | #ifndef CONFIG_TCC_PREFIX | 74 | #ifndef CONFIG_TCC_PREFIX |
| 70 | #define CONFIG_TCC_PREFIX "/usr/local" | 75 | #define CONFIG_TCC_PREFIX "/usr/local" |
| 71 | #endif | 76 | #endif |
| @@ -203,8 +208,10 @@ typedef struct AttributeDef { · 131 unmodified lines | |||
| 203 | #define MACRO_OBJ 0 /* object like macro */ | 208 | #define MACRO_OBJ 0 /* object like macro */ |
| 204 | #define MACRO_FUNC 1 /* function like macro */ | 209 | #define MACRO_FUNC 1 /* function like macro */ |
| 205 | 210 | ||
| 206 | /* field 'Sym.r' for labels */ | 211 | /* field 'Sym.r' for C labels */ |
| 212 | #define LABEL_DEFINED 0 /* label is defined */ | ||
| 207 | #define LABEL_FORWARD 1 /* label is forward defined */ | 213 | #define LABEL_FORWARD 1 /* label is forward defined */ |
| 214 | #define LABEL_DECLARED 2 /* label is declared but never used */ | ||
| 208 | 215 | ||
| 209 | /* type_decl() types */ | 216 | /* type_decl() types */ |
| 210 | #define TYPE_ABSTRACT 1 /* type without variable */ | 217 | #define TYPE_ABSTRACT 1 /* type without variable */ |
| @@ -256,8 +263,8 @@ typedef struct CachedInclude { · 45 unmodified lines | |||
| 256 | 263 | ||
| 257 | /* parser */ | 264 | /* parser */ |
| 258 | static struct BufferedFile *file; | 265 | static struct BufferedFile *file; |
| 259 | static int ch, tok, tok1; | 266 | static int ch, tok; |
| 260 | static CValue tokc, tok1c; | 267 | static CValue tokc; |
| 261 | static CString tokcstr; /* current parsed string, if any */ | 268 | static CString tokcstr; /* current parsed string, if any */ |
| 262 | /* additionnal informations about token */ | 269 | /* additionnal informations about token */ |
| 263 | static int tok_flags; | 270 | static int tok_flags; |
| @@ -265,9 +272,17 @@ static int tok_flags; · 1 unmodified line | |||
| 265 | #define TOK_FLAG_BOF 0x0002 /* beginning of file before */ | 272 | #define TOK_FLAG_BOF 0x0002 /* beginning of file before */ |
| 266 | #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */ | 273 | #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */ |
| 267 | 274 | ||
| 268 | /* if true, line feed is returned as a token. line feed is also | 275 | static int *macro_ptr, *macro_ptr_allocated; |
| 269 | returned at eof */ | 276 | static int *unget_saved_macro_ptr; |
| 270 | static int return_linefeed; | 277 | static int unget_saved_buffer[TOK_MAX_SIZE + 1]; |
| 278 | static int unget_buffer_enabled; | ||
| 279 | static int parse_flags; | ||
| 280 | #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */ | ||
| 281 | #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */ | ||
| 282 | #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a | ||
| 283 | token. line feed is also | ||
| 284 | returned at eof */ | ||
| 285 | |||
| 271 | static Section *text_section, *data_section, *bss_section; /* predefined sections */ | 286 | static Section *text_section, *data_section, *bss_section; /* predefined sections */ |
| 272 | static Section *cur_text_section; /* current section where function code is | 287 | static Section *cur_text_section; /* current section where function code is |
| 273 | generated */ | 288 | generated */ |
| @@ -302,12 +317,13 @@ static char token_buf[STRING_MAX_SIZE + 1]; · 28 unmodified lines | |||
| 302 | static char *funcname; | 317 | static char *funcname; |
| 303 | static Sym *global_stack, *local_stack; | 318 | static Sym *global_stack, *local_stack; |
| 304 | static Sym *define_stack; | 319 | static Sym *define_stack; |
| 305 | static Sym *label_stack; | 320 | static Sym *global_label_stack, *local_label_stack; |
| 306 | 321 | ||
| 307 | static SValue vstack[VSTACK_SIZE], *vtop; | 322 | static SValue vstack[VSTACK_SIZE], *vtop; |
| 308 | static int *macro_ptr, *macro_ptr_allocated; | ||
| 309 | /* some predefined types */ | 323 | /* some predefined types */ |
| 310 | static CType char_pointer_type, func_old_type, int_type; | 324 | static CType char_pointer_type, func_old_type, int_type; |
| 325 | /* true if isid(c) || isnum(c) */ | ||
| 326 | static unsigned char isidnum_table[256]; | ||
| 311 | 327 | ||
| 312 | /* compile with debug symbol (and use them if error during execution) */ | 328 | /* compile with debug symbol (and use them if error during execution) */ |
| 313 | static int do_debug = 0; | 329 | static int do_debug = 0; |
| @@ -364,9 +380,9 @@ struct TCCState { · 50 unmodified lines | |||
| 364 | 380 | ||
| 365 | /* got handling */ | 381 | /* got handling */ |
| 366 | Section *got; | 382 | Section *got; |
| 383 | Section *plt; | ||
| 367 | unsigned long *got_offsets; | 384 | unsigned long *got_offsets; |
| 368 | int nb_got_offsets; | 385 | int nb_got_offsets; |
| 369 | int nb_plt_entries; | ||
| 370 | /* give the correspondance from symtab indexes to dynsym indexes */ | 386 | /* give the correspondance from symtab indexes to dynsym indexes */ |
| 371 | int *symtab_to_dynsym; | 387 | int *symtab_to_dynsym; |
| 372 | 388 | ||
| @@ -375,6 +391,9 @@ struct TCCState { · 2 unmodified lines | |||
| 375 | /* exported dynamic symbol section */ | 391 | /* exported dynamic symbol section */ |
| 376 | Section *dynsym; | 392 | Section *dynsym; |
| 377 | 393 | ||
| 394 | /* if true, no standard headers are added */ | ||
| 395 | int nostdinc; | ||
| 396 | |||
| 378 | /* if true, static linking is performed */ | 397 | /* if true, static linking is performed */ |
| 379 | int static_link; | 398 | int static_link; |
| 380 | 399 | ||
| @@ -385,6 +404,9 @@ struct TCCState { · 4 unmodified lines | |||
| 385 | jmp_buf error_jmp_buf; | 404 | jmp_buf error_jmp_buf; |
| 386 | int nb_errors; | 405 | int nb_errors; |
| 387 | 406 | ||
| 407 | /* tiny assembler state */ | ||
| 408 | Sym *asm_labels; | ||
| 409 | |||
| 388 | /* see include_stack_ptr */ | 410 | /* see include_stack_ptr */ |
| 389 | BufferedFile *include_stack[INCLUDE_STACK_SIZE]; | 411 | BufferedFile *include_stack[INCLUDE_STACK_SIZE]; |
| 390 | 412 | ||
| @@ -441,9 +463,11 @@ struct TCCState { · 50 unmodified lines | |||
| 441 | #define VT_EXTERN 0x00000080 /* extern definition */ | 463 | #define VT_EXTERN 0x00000080 /* extern definition */ |
| 442 | #define VT_STATIC 0x00000100 /* static variable */ | 464 | #define VT_STATIC 0x00000100 /* static variable */ |
| 443 | #define VT_TYPEDEF 0x00000200 /* typedef definition */ | 465 | #define VT_TYPEDEF 0x00000200 /* typedef definition */ |
| 466 | #define VT_INLINE 0x00000400 /* inline definition */ | ||
| 444 | 467 | ||
| 445 | /* type mask (except storage) */ | 468 | /* type mask (except storage) */ |
| 446 | #define VT_TYPE (~(VT_EXTERN | VT_STATIC | VT_TYPEDEF)) | 469 | #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE) |
| 470 | #define VT_TYPE (~(VT_STORAGE)) | ||
| 447 | 471 | ||
| 448 | /* token values */ | 472 | /* token values */ |
| 449 | 473 | ||
| @@ -515,6 +539,65 @@ static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224< · 65 unmodified lines | |||
| 515 | /* all identificators and strings have token above that */ | 539 | /* all identificators and strings have token above that */ |
| 516 | #define TOK_IDENT 256 | 540 | #define TOK_IDENT 256 |
| 517 | 541 | ||
| 542 | /* only used for i386 asm opcodes definitions */ | ||
| 543 | #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x) | ||
| 544 | |||
| 545 | #define DEF_BWL(x) \ | ||
| 546 | DEF(TOK_ASM_ ## x ## b, #x "b") \ | ||
| 547 | DEF(TOK_ASM_ ## x ## w, #x "w") \ | ||
| 548 | DEF(TOK_ASM_ ## x ## l, #x "l") \ | ||
| 549 | DEF(TOK_ASM_ ## x, #x) | ||
| 550 | |||
| 551 | #define DEF_WL(x) \ | ||
| 552 | DEF(TOK_ASM_ ## x ## w, #x "w") \ | ||
| 553 | DEF(TOK_ASM_ ## x ## l, #x "l") \ | ||
| 554 | DEF(TOK_ASM_ ## x, #x) | ||
| 555 | |||
| 556 | #define DEF_FP1(x) \ | ||
| 557 | DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \ | ||
| 558 | DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \ | ||
| 559 | DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \ | ||
| 560 | DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s") | ||
| 561 | |||
| 562 | #define DEF_FP(x) \ | ||
| 563 | DEF(TOK_ASM_ ## f ## x, "f" #x ) \ | ||
| 564 | DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \ | ||
| 565 | DEF_FP1(x) | ||
| 566 | |||
| 567 | #define DEF_ASMTEST(x) \ | ||
| 568 | DEF_ASM(x ## o) \ | ||
| 569 | DEF_ASM(x ## no) \ | ||
| 570 | DEF_ASM(x ## b) \ | ||
| 571 | DEF_ASM(x ## c) \ | ||
| 572 | DEF_ASM(x ## nae) \ | ||
| 573 | DEF_ASM(x ## nb) \ | ||
| 574 | DEF_ASM(x ## nc) \ | ||
| 575 | DEF_ASM(x ## ae) \ | ||
| 576 | DEF_ASM(x ## e) \ | ||
| 577 | DEF_ASM(x ## z) \ | ||
| 578 | DEF_ASM(x ## ne) \ | ||
| 579 | DEF_ASM(x ## nz) \ | ||
| 580 | DEF_ASM(x ## be) \ | ||
| 581 | DEF_ASM(x ## na) \ | ||
| 582 | DEF_ASM(x ## nbe) \ | ||
| 583 | DEF_ASM(x ## a) \ | ||
| 584 | DEF_ASM(x ## s) \ | ||
| 585 | DEF_ASM(x ## ns) \ | ||
| 586 | DEF_ASM(x ## p) \ | ||
| 587 | DEF_ASM(x ## pe) \ | ||
| 588 | DEF_ASM(x ## np) \ | ||
| 589 | DEF_ASM(x ## po) \ | ||
| 590 | DEF_ASM(x ## l) \ | ||
| 591 | DEF_ASM(x ## nge) \ | ||
| 592 | DEF_ASM(x ## nl) \ | ||
| 593 | DEF_ASM(x ## ge) \ | ||
| 594 | DEF_ASM(x ## le) \ | ||
| 595 | DEF_ASM(x ## ng) \ | ||
| 596 | DEF_ASM(x ## nle) \ | ||
| 597 | DEF_ASM(x ## g) | ||
| 598 | |||
| 599 | #define TOK_ASM_int TOK_INT | ||
| 600 | |||
| 518 | enum { | 601 | enum { |
| 519 | TOK_LAST = TOK_IDENT - 1, | 602 | TOK_LAST = TOK_IDENT - 1, |
| 520 | #define DEF(id, str) id, | 603 | #define DEF(id, str) id, |
| @@ -522,7 +605,7 @@ enum { · 1 unmodified line | |||
| 522 | #undef DEF | 605 | #undef DEF |
| 523 | }; | 606 | }; |
| 524 | 607 | ||
| 525 | static const char *tcc_keywords = | 608 | static const char tcc_keywords[] = |
| 526 | #define DEF(id, str) str "\0" | 609 | #define DEF(id, str) str "\0" |
| 527 | #include "tcctok.h" | 610 | #include "tcctok.h" |
| 528 | #undef DEF | 611 | #undef DEF |
| @@ -559,6 +642,8 @@ static void next_nomacro(void); · 30 unmodified lines | |||
| 559 | static void parse_expr_type(CType *type); | 642 | static void parse_expr_type(CType *type); |
| 560 | static void expr_type(CType *type); | 643 | static void expr_type(CType *type); |
| 561 | static void unary_type(CType *type); | 644 | static void unary_type(CType *type); |
| 645 | static void block(int *bsym, int *csym, int *case_sym, int *def_sym, | ||
| 646 | int case_reg, int is_expr); | ||
| 562 | static int expr_const(void); | 647 | static int expr_const(void); |
| 563 | static void expr_eq(void); | 648 | static void expr_eq(void); |
| 564 | static void gexpr(void); | 649 | static void gexpr(void); |
| @@ -578,7 +663,7 @@ void vdup(void); · 13 unmodified lines | |||
| 578 | int get_reg(int rc); | 663 | int get_reg(int rc); |
| 579 | 664 | ||
| 580 | static void macro_subst(TokenString *tok_str, | 665 | static void macro_subst(TokenString *tok_str, |
| 581 | Sym **nested_list, int *macro_str); | 666 | Sym **nested_list, const int *macro_str); |
| 582 | int save_reg_forced(int r); | 667 | int save_reg_forced(int r); |
| 583 | void gen_op(int op); | 668 | void gen_op(int op); |
| 584 | void force_charshort_cast(int t); | 669 | void force_charshort_cast(int t); |
| @@ -632,6 +717,38 @@ static int tcc_add_dll(TCCState *s, const char *filename, int flags); · 47 unmodified lines | |||
| 632 | #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */ | 717 | #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */ |
| 633 | static int tcc_add_file_internal(TCCState *s, const char *filename, int flags); | 718 | static int tcc_add_file_internal(TCCState *s, const char *filename, int flags); |
| 634 | 719 | ||
| 720 | /* tccasm.c */ | ||
| 721 | |||
| 722 | #ifdef CONFIG_TCC_ASM | ||
| 723 | |||
| 724 | typedef struct ExprValue { | ||
| 725 | uint32_t v; | ||
| 726 | Sym *sym; | ||
| 727 | } ExprValue; | ||
| 728 | |||
| 729 | #define MAX_ASM_OPERANDS 30 | ||
| 730 | |||
| 731 | typedef struct ASMOperand { | ||
| 732 | int id; /* GCC 3 optionnal identifier (0 if number only supported */ | ||
| 733 | char *constraint; | ||
| 734 | char asm_str[16]; /* computed asm string for operand */ | ||
| 735 | SValue *vt; /* C value of the expression */ | ||
| 736 | int ref_index; /* if >= 0, gives reference to a output constraint */ | ||
| 737 | int priority; /* priority, used to assign registers */ | ||
| 738 | int reg; /* if >= 0, register number used for this operand */ | ||
| 739 | } ASMOperand; | ||
| 740 | |||
| 741 | static void asm_expr(TCCState *s1, ExprValue *pe); | ||
| 742 | static int asm_int_expr(TCCState *s1); | ||
| 743 | static int find_constraint(ASMOperand *operands, int nb_operands, | ||
| 744 | const char *name, const char **pp); | ||
| 745 | |||
| 746 | static int tcc_assemble(TCCState *s1, int do_preprocess); | ||
| 747 | |||
| 748 | #endif | ||
| 749 | |||
| 750 | static void asm_instr(void); | ||
| 751 | |||
| 635 | /* true if float/double/long double type */ | 752 | /* true if float/double/long double type */ |
| 636 | static inline int is_float(int t) | 753 | static inline int is_float(int t) |
| 637 | { | 754 | { |
| @@ -643,9 +760,6 @@ static inline int is_float(int t) · 5 unmodified lines | |||
| 643 | #ifdef TCC_TARGET_I386 | 760 | #ifdef TCC_TARGET_I386 |
| 644 | #include "i386-gen.c" | 761 | #include "i386-gen.c" |
| 645 | #endif | 762 | #endif |
| 646 | #ifdef TCC_TARGET_IL | ||
| 647 | #include "il-gen.c" | ||
| 648 | #endif | ||
| 649 | 763 | ||
| 650 | #ifdef CONFIG_TCC_STATIC | 764 | #ifdef CONFIG_TCC_STATIC |
| 651 | 765 | ||
| @@ -1113,7 +1227,7 @@ void skip(int c) · 461 unmodified lines | |||
| 1113 | next(); | 1227 | next(); |
| 1114 | } | 1228 | } |
| 1115 | 1229 | ||
| 1116 | void test_lvalue(void) | 1230 | static void test_lvalue(void) |
| 1117 | { | 1231 | { |
| 1118 | if (!(vtop->r & VT_LVAL)) | 1232 | if (!(vtop->r & VT_LVAL)) |
| 1119 | expect("lvalue"); | 1233 | expect("lvalue"); |
| @@ -2157,15 +2271,49 @@ static Sym *label_find(int v) · 1037 unmodified lines | |||
| 2157 | return table_ident[v]->sym_label; | 2271 | return table_ident[v]->sym_label; |
| 2158 | } | 2272 | } |
| 2159 | 2273 | ||
| 2160 | static Sym *label_push(int v, int flags) | 2274 | static Sym *label_push(Sym **ptop, int v, int flags) |
| 2161 | { | 2275 | { |
| 2162 | Sym *s; | 2276 | Sym *s, **ps; |
| 2163 | s = sym_push2(&label_stack, v, 0, 0); | 2277 | s = sym_push2(ptop, v, 0, 0); |
| 2164 | s->r = flags; | 2278 | s->r = flags; |
| 2165 | table_ident[v - TOK_IDENT]->sym_label = s; | 2279 | ps = &table_ident[v - TOK_IDENT]->sym_label; |
| 2280 | if (ptop == &global_label_stack) { | ||
| 2281 | /* modify the top most local identifier, so that | ||
| 2282 | sym_identifier will point to 's' when popped */ | ||
| 2283 | while (*ps != NULL) | ||
| 2284 | ps = &(*ps)->prev_tok; | ||
| 2285 | } | ||
| 2286 | s->prev_tok = *ps; | ||
| 2287 | *ps = s; | ||
| 2166 | return s; | 2288 | return s; |
| 2167 | } | 2289 | } |
| 2168 | 2290 | ||
| 2291 | /* pop labels until element last is reached. Look if any labels are | ||
| 2292 | undefined. Define symbols if '&&label' was used. */ | ||
| 2293 | static void label_pop(Sym **ptop, Sym *slast) | ||
| 2294 | { | ||
| 2295 | Sym *s, *s1; | ||
| 2296 | for(s = *ptop; s != slast; s = s1) { | ||
| 2297 | s1 = s->prev; | ||
| 2298 | if (s->r == LABEL_DECLARED) { | ||
| 2299 | warning("label '%s' declared but not used", get_tok_str(s->v, NULL)); | ||
| 2300 | } else if (s->r == LABEL_FORWARD) { | ||
| 2301 | error("label '%s' used but not defined", | ||
| 2302 | get_tok_str(s->v, NULL)); | ||
| 2303 | } else { | ||
| 2304 | if (s->c) { | ||
| 2305 | /* define corresponding symbol. A size of | ||
| 2306 | 1 is put. */ | ||
| 2307 | put_extern_sym(s, cur_text_section, (long)s->next, 1); | ||
| 2308 | } | ||
| 2309 | } | ||
| 2310 | /* remove label */ | ||
| 2311 | table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok; | ||
| 2312 | tcc_free(s); | ||
| 2313 | } | ||
| 2314 | *ptop = slast; | ||
| 2315 | } | ||
| 2316 | |||
| 2169 | /* eval an expression for #if/#elif */ | 2317 | /* eval an expression for #if/#elif */ |
| 2170 | static int expr_preprocess(void) | 2318 | static int expr_preprocess(void) |
| 2171 | { | 2319 | { |
| @@ -2315,15 +2463,16 @@ static inline void add_cached_include(TCCState *s1, int type, · 143 unmodified lines | |||
| 2315 | static void preprocess(int is_bof) | 2463 | static void preprocess(int is_bof) |
| 2316 | { | 2464 | { |
| 2317 | TCCState *s1 = tcc_state; | 2465 | TCCState *s1 = tcc_state; |
| 2318 | int size, i, c, n; | 2466 | int size, i, c, n, saved_parse_flags; |
| 2319 | char buf[1024], *q, *p; | 2467 | char buf[1024], *q, *p; |
| 2320 | char buf1[1024]; | 2468 | char buf1[1024]; |
| 2321 | BufferedFile *f; | 2469 | BufferedFile *f; |
| 2322 | Sym *s; | 2470 | Sym *s; |
| 2323 | CachedInclude *e; | 2471 | CachedInclude *e; |
| 2324 | 2472 | ||
| 2325 | return_linefeed = 1; /* linefeed will be returned as a | 2473 | saved_parse_flags = parse_flags; |
| 2326 | token. EOF is also returned as line feed */ | 2474 | parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM | |
| 2475 | PARSE_FLAG_LINEFEED; | ||
| 2327 | next_nomacro(); | 2476 | next_nomacro(); |
| 2328 | redo: | 2477 | redo: |
| 2329 | switch(tok) { | 2478 | switch(tok) { |
| @@ -2572,7 +2721,7 @@ static void preprocess(int is_bof) · 242 unmodified lines | |||
| 2572 | while (tok != TOK_LINEFEED) | 2721 | while (tok != TOK_LINEFEED) |
| 2573 | next_nomacro(); | 2722 | next_nomacro(); |
| 2574 | the_end: | 2723 | the_end: |
| 2575 | return_linefeed = 0; | 2724 | parse_flags = saved_parse_flags; |
| 2576 | } | 2725 | } |
| 2577 | 2726 | ||
| 2578 | /* read a number in base b */ | 2727 | /* read a number in base b */ |
| @@ -3020,11 +3169,11 @@ static inline void next_nomacro1(void) · 441 unmodified lines | |||
| 3020 | parse_eof: | 3169 | parse_eof: |
| 3021 | { | 3170 | { |
| 3022 | TCCState *s1 = tcc_state; | 3171 | TCCState *s1 = tcc_state; |
| 3023 | 3172 | if (parse_flags & PARSE_FLAG_LINEFEED) { | |
| 3024 | if (return_linefeed) { | ||
| 3025 | tok = TOK_LINEFEED; | 3173 | tok = TOK_LINEFEED; |
| 3026 | } else if (s1->include_stack_ptr == s1->include_stack) { | 3174 | } else if (s1->include_stack_ptr == s1->include_stack || |
| 3027 | /* no include left : end of file */ | 3175 | !(parse_flags & PARSE_FLAG_PREPROCESS)) { |
| 3176 | /* no include left : end of file. */ | ||
| 3028 | tok = TOK_EOF; | 3177 | tok = TOK_EOF; |
| 3029 | } else { | 3178 | } else { |
| 3030 | /* pop include file */ | 3179 | /* pop include file */ |
| @@ -3054,7 +3203,7 @@ static inline void next_nomacro1(void) · 23 unmodified lines | |||
| 3054 | break; | 3203 | break; |
| 3055 | 3204 | ||
| 3056 | case '\n': | 3205 | case '\n': |
| 3057 | if (return_linefeed) { | 3206 | if (parse_flags & PARSE_FLAG_LINEFEED) { |
| 3058 | tok = TOK_LINEFEED; | 3207 | tok = TOK_LINEFEED; |
| 3059 | } else { | 3208 | } else { |
| 3060 | file->line_num++; | 3209 | file->line_num++; |
| @@ -3067,7 +3216,8 @@ static inline void next_nomacro1(void) · 6 unmodified lines | |||
| 3067 | case '#': | 3216 | case '#': |
| 3068 | /* XXX: simplify */ | 3217 | /* XXX: simplify */ |
| 3069 | PEEKC(c, p); | 3218 | PEEKC(c, p); |
| 3070 | if (tok_flags & TOK_FLAG_BOL) { | 3219 | if ((tok_flags & TOK_FLAG_BOL) && |
| 3220 | (parse_flags & PARSE_FLAG_PREPROCESS)) { | ||
| 3071 | file->buf_ptr = p; | 3221 | file->buf_ptr = p; |
| 3072 | preprocess(tok_flags & TOK_FLAG_BOF); | 3222 | preprocess(tok_flags & TOK_FLAG_BOF); |
| 3073 | p = file->buf_ptr; | 3223 | p = file->buf_ptr; |
| @@ -3104,7 +3254,7 @@ static inline void next_nomacro1(void) · 30 unmodified lines | |||
| 3104 | p++; | 3254 | p++; |
| 3105 | for(;;) { | 3255 | for(;;) { |
| 3106 | c = *p; | 3256 | c = *p; |
| 3107 | if (!isid(c) && !isnum(c)) | 3257 | if (!isidnum_table[c]) |
| 3108 | break; | 3258 | break; |
| 3109 | h = TOK_HASH_FUNC(h, c); | 3259 | h = TOK_HASH_FUNC(h, c); |
| 3110 | p++; | 3260 | p++; |
| @@ -3139,7 +3289,7 @@ static inline void next_nomacro1(void) · 28 unmodified lines | |||
| 3139 | p--; | 3289 | p--; |
| 3140 | PEEKC(c, p); | 3290 | PEEKC(c, p); |
| 3141 | parse_ident_slow: | 3291 | parse_ident_slow: |
| 3142 | while (isid(c) || isnum(c)) { | 3292 | while (isidnum_table[c]) { |
| 3143 | cstr_ccat(&tokcstr, c); | 3293 | cstr_ccat(&tokcstr, c); |
| 3144 | PEEKC(c, p); | 3294 | PEEKC(c, p); |
| 3145 | } | 3295 | } |
| @@ -3375,6 +3525,7 @@ static inline void next_nomacro1(void) · 229 unmodified lines | |||
| 3375 | case ':': | 3525 | case ':': |
| 3376 | case '?': | 3526 | case '?': |
| 3377 | case '~': | 3527 | case '~': |
| 3528 | case '$': /* only used in assembler */ | ||
| 3378 | tok = c; | 3529 | tok = c; |
| 3379 | p++; | 3530 | p++; |
| 3380 | break; | 3531 | break; |
| @@ -3498,115 +3649,6 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 117 unmodified lines | |||
| 3498 | return str.str; | 3649 | return str.str; |
| 3499 | } | 3650 | } |
| 3500 | 3651 | ||
| 3501 | /* handle the '##' operator */ | ||
| 3502 | static int *macro_twosharps(void) | ||
| 3503 | { | ||
| 3504 | TokenSym *ts; | ||
| 3505 | int *macro_ptr1; | ||
| 3506 | int t; | ||
| 3507 | const char *p1, *p2; | ||
| 3508 | CValue cval; | ||
| 3509 | TokenString macro_str1; | ||
| 3510 | CString cstr; | ||
| 3511 | |||
| 3512 | cstr_new(&cstr); | ||
| 3513 | tok_str_new(¯o_str1); | ||
| 3514 | tok = 0; | ||
| 3515 | while (1) { | ||
| 3516 | next_nomacro(); | ||
| 3517 | if (tok == 0) | ||
| 3518 | break; | ||
| 3519 | while (*macro_ptr == TOK_TWOSHARPS) { | ||
| 3520 | macro_ptr++; | ||
| 3521 | macro_ptr1 = macro_ptr; | ||
| 3522 | t = *macro_ptr; | ||
| 3523 | if (t) { | ||
| 3524 | TOK_GET(t, macro_ptr, cval); | ||
| 3525 | |||
| 3526 | /* We concatenate the two tokens if we have an | ||
| 3527 | identifier or a preprocessing number */ | ||
| 3528 | cstr_reset(&cstr); | ||
| 3529 | p1 = get_tok_str(tok, &tokc); | ||
| 3530 | cstr_cat(&cstr, p1); | ||
| 3531 | p2 = get_tok_str(t, &cval); | ||
| 3532 | cstr_cat(&cstr, p2); | ||
| 3533 | cstr_ccat(&cstr, '\0'); | ||
| 3534 | |||
| 3535 | if ((tok >= TOK_IDENT || tok == TOK_PPNUM) && | ||
| 3536 | (t >= TOK_IDENT || t == TOK_PPNUM)) { | ||
| 3537 | if (tok == TOK_PPNUM) { | ||
| 3538 | /* if number, then create a number token */ | ||
| 3539 | /* NOTE: no need to allocate because | ||
| 3540 | tok_str_add2() does it */ | ||
| 3541 | tokc.cstr = &cstr; | ||
| 3542 | } else { | ||
| 3543 | /* if identifier, we must do a test to | ||
| 3544 | validate we have a correct identifier */ | ||
| 3545 | if (t == TOK_PPNUM) { | ||
| 3546 | const char *p; | ||
| 3547 | int c; | ||
| 3548 | |||
| 3549 | p = p2; | ||
| 3550 | for(;;) { | ||
| 3551 | c = *p; | ||
| 3552 | if (c == '\0') | ||
| 3553 | break; | ||
| 3554 | p++; | ||
| 3555 | if (!isnum(c) && !isid(c)) | ||
| 3556 | goto error_pasting; | ||
| 3557 | } | ||
| 3558 | } | ||
| 3559 | ts = tok_alloc(cstr.data, strlen(cstr.data)); | ||
| 3560 | tok = ts->tok; /* modify current token */ | ||
| 3561 | } | ||
| 3562 | } else { | ||
| 3563 | const char *str = cstr.data; | ||
| 3564 | const unsigned char *q; | ||
| 3565 | |||
| 3566 | /* we look for a valid token */ | ||
| 3567 | /* XXX: do more extensive checks */ | ||
| 3568 | if (!strcmp(str, ">>=")) { | ||
| 3569 | tok = TOK_A_SAR; | ||
| 3570 | } else if (!strcmp(str, "<<=")) { | ||
| 3571 | tok = TOK_A_SHL; | ||
| 3572 | } else if (strlen(str) == 2) { | ||
| 3573 | /* search in two bytes table */ | ||
| 3574 | q = tok_two_chars; | ||
| 3575 | for(;;) { | ||
| 3576 | if (!*q) | ||
| 3577 | goto error_pasting; | ||
| 3578 | if (q[0] == str[0] && q[1] == str[1]) | ||
| 3579 | break; | ||
| 3580 | q += 3; | ||
| 3581 | } | ||
| 3582 | tok = q[2]; | ||
| 3583 | } else { | ||
| 3584 | error_pasting: | ||
| 3585 | /* NOTE: because get_tok_str use a static buffer, | ||
| 3586 | we must save it */ | ||
| 3587 | cstr_reset(&cstr); | ||
| 3588 | p1 = get_tok_str(tok, &tokc); | ||
| 3589 | cstr_cat(&cstr, p1); | ||
| 3590 | cstr_ccat(&cstr, '\0'); | ||
| 3591 | p2 = get_tok_str(t, &cval); | ||
| 3592 | warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2); | ||
| 3593 | /* cannot merge tokens: just add them separately */ | ||
| 3594 | tok_str_add2(¯o_str1, tok, &tokc); | ||
| 3595 | /* XXX: free associated memory ? */ | ||
| 3596 | tok = t; | ||
| 3597 | tokc = cval; | ||
| 3598 | } | ||
| 3599 | } | ||
| 3600 | } | ||
| 3601 | } | ||
| 3602 | tok_str_add2(¯o_str1, tok, &tokc); | ||
| 3603 | } | ||
| 3604 | cstr_free(&cstr); | ||
| 3605 | tok_str_add(¯o_str1, 0); | ||
| 3606 | return macro_str1.str; | ||
| 3607 | } | ||
| 3608 | |||
| 3609 | |||
| 3610 | /* do macro substitution of current token with macro 's' and add | 3652 | /* do macro substitution of current token with macro 's' and add |
| 3611 | result to (tok_str,tok_len). 'nested_list' is the list of all | 3653 | result to (tok_str,tok_len). 'nested_list' is the list of all |
| 3612 | macros we got inside to avoid recursing. Return non zero if no | 3654 | macros we got inside to avoid recursing. Return non zero if no |
| @@ -3622,9 +3664,6 @@ static int macro_subst_tok(TokenString *tok_str, · 9 unmodified lines | |||
| 3622 | CString cstr; | 3664 | CString cstr; |
| 3623 | 3665 | ||
| 3624 | /* if symbol is a macro, prepare substitution */ | 3666 | /* if symbol is a macro, prepare substitution */ |
| 3625 | /* if nested substitution, do nothing */ | ||
| 3626 | if (sym_find2(*nested_list, tok)) | ||
| 3627 | return -1; | ||
| 3628 | 3667 | ||
| 3629 | /* special macros */ | 3668 | /* special macros */ |
| 3630 | if (tok == TOK___LINE__) { | 3669 | if (tok == TOK___LINE__) { |
| @@ -3735,37 +3774,180 @@ static int macro_subst_tok(TokenString *tok_str, · 104 unmodified lines | |||
| 3735 | return 0; | 3774 | return 0; |
| 3736 | } | 3775 | } |
| 3737 | 3776 | ||
| 3777 | /* handle the '##' operator. Return NULL if no '##' seen. Otherwise | ||
| 3778 | return the resulting string (which must be freed). */ | ||
| 3779 | static inline int *macro_twosharps(const int *macro_str) | ||
| 3780 | { | ||
| 3781 | TokenSym *ts; | ||
| 3782 | const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr; | ||
| 3783 | int t; | ||
| 3784 | const char *p1, *p2; | ||
| 3785 | CValue cval; | ||
| 3786 | TokenString macro_str1; | ||
| 3787 | CString cstr; | ||
| 3788 | |||
| 3789 | start_macro_ptr = macro_str; | ||
| 3790 | /* we search the first '##' */ | ||
| 3791 | for(;;) { | ||
| 3792 | macro_ptr1 = macro_str; | ||
| 3793 | TOK_GET(t, macro_str, cval); | ||
| 3794 | /* nothing more to do if end of string */ | ||
| 3795 | if (t == 0) | ||
| 3796 | return NULL; | ||
| 3797 | if (*macro_str == TOK_TWOSHARPS) | ||
| 3798 | break; | ||
| 3799 | } | ||
| 3800 | |||
| 3801 | /* we saw '##', so we need more processing to handle it */ | ||
| 3802 | cstr_new(&cstr); | ||
| 3803 | tok_str_new(¯o_str1); | ||
| 3804 | tok = t; | ||
| 3805 | tokc = cval; | ||
| 3806 | |||
| 3807 | /* add all tokens seen so far */ | ||
| 3808 | for(ptr = start_macro_ptr; ptr < macro_ptr1;) { | ||
| 3809 | TOK_GET(t, ptr, cval); | ||
| 3810 | tok_str_add2(¯o_str1, t, &cval); | ||
| 3811 | } | ||
| 3812 | saved_macro_ptr = macro_ptr; | ||
| 3813 | /* XXX: suppress the use of macro_ptr here */ | ||
| 3814 | macro_ptr = (int *)macro_str; | ||
| 3815 | for(;;) { | ||
| 3816 | while (*macro_ptr == TOK_TWOSHARPS) { | ||
| 3817 | macro_ptr++; | ||
| 3818 | macro_ptr1 = macro_ptr; | ||
| 3819 | t = *macro_ptr; | ||
| 3820 | if (t) { | ||
| 3821 | TOK_GET(t, macro_ptr, cval); | ||
| 3822 | /* We concatenate the two tokens if we have an | ||
| 3823 | identifier or a preprocessing number */ | ||
| 3824 | cstr_reset(&cstr); | ||
| 3825 | p1 = get_tok_str(tok, &tokc); | ||
| 3826 | cstr_cat(&cstr, p1); | ||
| 3827 | p2 = get_tok_str(t, &cval); | ||
| 3828 | cstr_cat(&cstr, p2); | ||
| 3829 | cstr_ccat(&cstr, '\0'); | ||
| 3830 | |||
| 3831 | if ((tok >= TOK_IDENT || tok == TOK_PPNUM) && | ||
| 3832 | (t >= TOK_IDENT || t == TOK_PPNUM)) { | ||
| 3833 | if (tok == TOK_PPNUM) { | ||
| 3834 | /* if number, then create a number token */ | ||
| 3835 | /* NOTE: no need to allocate because | ||
| 3836 | tok_str_add2() does it */ | ||
| 3837 | tokc.cstr = &cstr; | ||
| 3838 | } else { | ||
| 3839 | /* if identifier, we must do a test to | ||
| 3840 | validate we have a correct identifier */ | ||
| 3841 | if (t == TOK_PPNUM) { | ||
| 3842 | const char *p; | ||
| 3843 | int c; | ||
| 3844 | |||
| 3845 | p = p2; | ||
| 3846 | for(;;) { | ||
| 3847 | c = *p; | ||
| 3848 | if (c == '\0') | ||
| 3849 | break; | ||
| 3850 | p++; | ||
| 3851 | if (!isnum(c) && !isid(c)) | ||
| 3852 | goto error_pasting; | ||
| 3853 | } | ||
| 3854 | } | ||
| 3855 | ts = tok_alloc(cstr.data, strlen(cstr.data)); | ||
| 3856 | tok = ts->tok; /* modify current token */ | ||
| 3857 | } | ||
| 3858 | } else { | ||
| 3859 | const char *str = cstr.data; | ||
| 3860 | const unsigned char *q; | ||
| 3861 | |||
| 3862 | /* we look for a valid token */ | ||
| 3863 | /* XXX: do more extensive checks */ | ||
| 3864 | if (!strcmp(str, ">>=")) { | ||
| 3865 | tok = TOK_A_SAR; | ||
| 3866 | } else if (!strcmp(str, "<<=")) { | ||
| 3867 | tok = TOK_A_SHL; | ||
| 3868 | } else if (strlen(str) == 2) { | ||
| 3869 | /* search in two bytes table */ | ||
| 3870 | q = tok_two_chars; | ||
| 3871 | for(;;) { | ||
| 3872 | if (!*q) | ||
| 3873 | goto error_pasting; | ||
| 3874 | if (q[0] == str[0] && q[1] == str[1]) | ||
| 3875 | break; | ||
| 3876 | q += 3; | ||
| 3877 | } | ||
| 3878 | tok = q[2]; | ||
| 3879 | } else { | ||
| 3880 | error_pasting: | ||
| 3881 | /* NOTE: because get_tok_str use a static buffer, | ||
| 3882 | we must save it */ | ||
| 3883 | cstr_reset(&cstr); | ||
| 3884 | p1 = get_tok_str(tok, &tokc); | ||
| 3885 | cstr_cat(&cstr, p1); | ||
| 3886 | cstr_ccat(&cstr, '\0'); | ||
| 3887 | p2 = get_tok_str(t, &cval); | ||
| 3888 | warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2); | ||
| 3889 | /* cannot merge tokens: just add them separately */ | ||
| 3890 | tok_str_add2(¯o_str1, tok, &tokc); | ||
| 3891 | /* XXX: free associated memory ? */ | ||
| 3892 | tok = t; | ||
| 3893 | tokc = cval; | ||
| 3894 | } | ||
| 3895 | } | ||
| 3896 | } | ||
| 3897 | } | ||
| 3898 | tok_str_add2(¯o_str1, tok, &tokc); | ||
| 3899 | next_nomacro(); | ||
| 3900 | if (tok == 0) | ||
| 3901 | break; | ||
| 3902 | } | ||
| 3903 | macro_ptr = (int *)saved_macro_ptr; | ||
| 3904 | cstr_free(&cstr); | ||
| 3905 | tok_str_add(¯o_str1, 0); | ||
| 3906 | return macro_str1.str; | ||
| 3907 | } | ||
| 3908 | |||
| 3909 | |||
| 3738 | /* do macro substitution of macro_str and add result to | 3910 | /* do macro substitution of macro_str and add result to |
| 3739 | (tok_str,tok_len). 'nested_list' is the list of all macros we got | 3911 | (tok_str,tok_len). 'nested_list' is the list of all macros we got |
| 3740 | inside to avoid recursing. */ | 3912 | inside to avoid recursing. */ |
| 3741 | static void macro_subst(TokenString *tok_str, | 3913 | static void macro_subst(TokenString *tok_str, |
| 3742 | Sym **nested_list, int *macro_str) | 3914 | Sym **nested_list, const int *macro_str) |
| 3743 | { | 3915 | { |
| 3744 | Sym *s; | 3916 | Sym *s; |
| 3745 | int *saved_macro_ptr; | 3917 | int *saved_macro_ptr, *macro_str1; |
| 3746 | int *macro_str1; | 3918 | const int *ptr; |
| 3919 | int t, ret; | ||
| 3920 | CValue cval; | ||
| 3747 | 3921 | ||
| 3748 | saved_macro_ptr = macro_ptr; | ||
| 3749 | macro_ptr = macro_str; | ||
| 3750 | /* first scan for '##' operator handling */ | 3922 | /* first scan for '##' operator handling */ |
| 3751 | macro_str1 = macro_twosharps(); | 3923 | ptr = macro_str; |
| 3752 | macro_ptr = macro_str1; | 3924 | macro_str1 = macro_twosharps(ptr); |
| 3753 | 3925 | if (macro_str1) | |
| 3926 | ptr = macro_str1; | ||
| 3754 | while (1) { | 3927 | while (1) { |
| 3755 | next_nomacro(); | 3928 | TOK_GET(t, ptr, cval); |
| 3756 | if (tok == 0) | 3929 | if (t == 0) |
| 3757 | break; | 3930 | break; |
| 3758 | s = define_find(tok); | 3931 | s = define_find(t); |
| 3759 | if (s != NULL) { | 3932 | if (s != NULL) { |
| 3760 | if (macro_subst_tok(tok_str, nested_list, s) != 0) | 3933 | /* if nested substitution, do nothing */ |
| 3934 | if (sym_find2(*nested_list, t)) | ||
| 3935 | goto no_subst; | ||
| 3936 | saved_macro_ptr = macro_ptr; | ||
| 3937 | macro_ptr = (int *)ptr; | ||
| 3938 | tok = t; | ||
| 3939 | ret = macro_subst_tok(tok_str, nested_list, s); | ||
| 3940 | ptr = (int *)macro_ptr; | ||
| 3941 | macro_ptr = saved_macro_ptr; | ||
| 3942 | if (ret != 0) | ||
| 3761 | goto no_subst; | 3943 | goto no_subst; |
| 3762 | } else { | 3944 | } else { |
| 3763 | no_subst: | 3945 | no_subst: |
| 3764 | tok_str_add2(tok_str, tok, &tokc); | 3946 | tok_str_add2(tok_str, t, &cval); |
| 3765 | } | 3947 | } |
| 3766 | } | 3948 | } |
| 3767 | macro_ptr = saved_macro_ptr; | 3949 | if (macro_str1) |
| 3768 | tok_str_free(macro_str1); | 3950 | tok_str_free(macro_str1); |
| 3769 | } | 3951 | } |
| 3770 | 3952 | ||
| 3771 | /* return next token with macro substitution */ | 3953 | /* return next token with macro substitution */ |
| @@ -3774,48 +3956,68 @@ static void next(void) · 2 unmodified lines | |||
| 3774 | Sym *nested_list, *s; | 3956 | Sym *nested_list, *s; |
| 3775 | TokenString str; | 3957 | TokenString str; |
| 3776 | 3958 | ||
| 3777 | /* special 'ungettok' case for label parsing */ | 3959 | redo: |
| 3778 | if (tok1) { | 3960 | next_nomacro(); |
| 3779 | tok = tok1; | 3961 | if (!macro_ptr) { |
| 3780 | tokc = tok1c; | 3962 | /* if not reading from macro substituted string, then try |
| 3781 | tok1 = 0; | 3963 | to substitute macros */ |
| 3782 | } else { | 3964 | if (tok >= TOK_IDENT && |
| 3783 | redo: | 3965 | (parse_flags & PARSE_FLAG_PREPROCESS)) { |
| 3784 | next_nomacro(); | 3966 | s = define_find(tok); |
| 3785 | if (!macro_ptr) { | 3967 | if (s) { |
| 3786 | /* if not reading from macro substituted string, then try | 3968 | /* we have a macro: we try to substitute */ |
| 3787 | to substitute macros */ | 3969 | tok_str_new(&str); |
| 3788 | if (tok >= TOK_IDENT) { | 3970 | nested_list = NULL; |
| 3789 | s = define_find(tok); | 3971 | if (macro_subst_tok(&str, &nested_list, s) == 0) { |
| 3790 | if (s) { | 3972 | /* substitution done, NOTE: maybe empty */ |
| 3791 | /* we have a macro: we try to substitute */ | 3973 | tok_str_add(&str, 0); |
| 3792 | tok_str_new(&str); | 3974 | macro_ptr = str.str; |
| 3793 | nested_list = NULL; | 3975 | macro_ptr_allocated = str.str; |
| 3794 | if (macro_subst_tok(&str, &nested_list, s) == 0) { | 3976 | goto redo; |
| 3795 | /* substitution done, NOTE: maybe empty */ | ||
| 3796 | tok_str_add(&str, 0); | ||
| 3797 | macro_ptr = str.str; | ||
| 3798 | macro_ptr_allocated = str.str; | ||
| 3799 | goto redo; | ||
| 3800 | } | ||
| 3801 | } | 3977 | } |
| 3802 | } | 3978 | } |
| 3803 | } else { | 3979 | } |
| 3804 | if (tok == 0) { | 3980 | } else { |
| 3981 | if (tok == 0) { | ||
| 3982 | /* end of macro or end of unget buffer */ | ||
| 3983 | if (unget_buffer_enabled) { | ||
| 3984 | macro_ptr = unget_saved_macro_ptr; | ||
| 3985 | unget_buffer_enabled = 0; | ||
| 3986 | } else { | ||
| 3805 | /* end of macro string: free it */ | 3987 | /* end of macro string: free it */ |
| 3806 | tok_str_free(macro_ptr_allocated); | 3988 | tok_str_free(macro_ptr_allocated); |
| 3807 | macro_ptr = NULL; | 3989 | macro_ptr = NULL; |
| 3808 | goto redo; | ||
| 3809 | } | 3990 | } |
| 3991 | goto redo; | ||
| 3810 | } | 3992 | } |
| 3811 | 3993 | } | |
| 3812 | /* convert preprocessor tokens into C tokens */ | 3994 | |
| 3813 | if (tok == TOK_PPNUM) { | 3995 | /* convert preprocessor tokens into C tokens */ |
| 3814 | parse_number((char *)tokc.cstr->data); | 3996 | if (tok == TOK_PPNUM && |
| 3815 | } | 3997 | (parse_flags & PARSE_FLAG_TOK_NUM)) { |
| 3998 | parse_number((char *)tokc.cstr->data); | ||
| 3816 | } | 3999 | } |
| 3817 | } | 4000 | } |
| 3818 | 4001 | ||
| 4002 | /* push back current token and set current token to 'last_tok'. Only | ||
| 4003 | identifier case handled for labels. */ | ||
| 4004 | static inline void unget_tok(int last_tok) | ||
| 4005 | { | ||
| 4006 | int i, n; | ||
| 4007 | int *q; | ||
| 4008 | unget_saved_macro_ptr = macro_ptr; | ||
| 4009 | unget_buffer_enabled = 1; | ||
| 4010 | q = unget_saved_buffer; | ||
| 4011 | macro_ptr = q; | ||
| 4012 | *q++ = tok; | ||
| 4013 | n = tok_ext_size(tok) - 1; | ||
| 4014 | for(i=0;i<n;i++) | ||
| 4015 | *q++ = tokc.tab[i]; | ||
| 4016 | *q = 0; /* end of token string */ | ||
| 4017 | tok = last_tok; | ||
| 4018 | } | ||
| 4019 | |||
| 4020 | |||
| 3819 | void swap(int *p, int *q) | 4021 | void swap(int *p, int *q) |
| 3820 | { | 4022 | { |
| 3821 | int t; | 4023 | int t; |
| @@ -3903,6 +4105,10 @@ static Sym *external_sym(int v, CType *type, int r) · 81 unmodified lines | |||
| 3903 | /* push forward reference */ | 4105 | /* push forward reference */ |
| 3904 | s = sym_push(v, type, r | VT_CONST | VT_SYM, 0); | 4106 | s = sym_push(v, type, r | VT_CONST | VT_SYM, 0); |
| 3905 | s->type.t |= VT_EXTERN; | 4107 | s->type.t |= VT_EXTERN; |
| 4108 | } else { | ||
| 4109 | if (!is_compatible_types(&s->type, type)) | ||
| 4110 | error("incompatible types for redefinition of '%s'", | ||
| 4111 | get_tok_str(v, NULL)); | ||
| 3906 | } | 4112 | } |
| 3907 | return s; | 4113 | return s; |
| 3908 | } | 4114 | } |
| @@ -5545,10 +5751,11 @@ void inc(int post, int c) · 1636 unmodified lines | |||
| 5545 | - section(x) : generate data/code in this section. | 5751 | - section(x) : generate data/code in this section. |
| 5546 | - unused : currently ignored, but may be used someday. | 5752 | - unused : currently ignored, but may be used someday. |
| 5547 | */ | 5753 | */ |
| 5548 | void parse_attribute(AttributeDef *ad) | 5754 | static void parse_attribute(AttributeDef *ad) |
| 5549 | { | 5755 | { |
| 5550 | int t, n; | 5756 | int t, n; |
| 5551 | 5757 | ||
| 5758 | while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) { | ||
| 5552 | next(); | 5759 | next(); |
| 5553 | skip('('); | 5760 | skip('('); |
| 5554 | skip('('); | 5761 | skip('('); |
| @@ -5558,8 +5765,8 @@ void parse_attribute(AttributeDef *ad) · 3 unmodified lines | |||
| 5558 | t = tok; | 5765 | t = tok; |
| 5559 | next(); | 5766 | next(); |
| 5560 | switch(t) { | 5767 | switch(t) { |
| 5561 | case TOK_SECTION: | 5768 | case TOK_SECTION1: |
| 5562 | case TOK___SECTION__: | 5769 | case TOK_SECTION2: |
| 5563 | skip('('); | 5770 | skip('('); |
| 5564 | if (tok != TOK_STR) | 5771 | if (tok != TOK_STR) |
| 5565 | expect("section name"); | 5772 | expect("section name"); |
| @@ -5567,8 +5774,8 @@ void parse_attribute(AttributeDef *ad) · 1 unmodified line | |||
| 5567 | next(); | 5774 | next(); |
| 5568 | skip(')'); | 5775 | skip(')'); |
| 5569 | break; | 5776 | break; |
| 5570 | case TOK_ALIGNED: | 5777 | case TOK_ALIGNED1: |
| 5571 | case TOK___ALIGNED__: | 5778 | case TOK_ALIGNED2: |
| 5572 | if (tok == '(') { | 5779 | if (tok == '(') { |
| 5573 | next(); | 5780 | next(); |
| 5574 | n = expr_const(); | 5781 | n = expr_const(); |
| @@ -5580,28 +5787,28 @@ void parse_attribute(AttributeDef *ad) · 5 unmodified lines | |||
| 5580 | } | 5787 | } |
| 5581 | ad->aligned = n; | 5788 | ad->aligned = n; |
| 5582 | break; | 5789 | break; |
| 5583 | case TOK_UNUSED: | 5790 | case TOK_UNUSED1: |
| 5584 | case TOK___UNUSED__: | 5791 | case TOK_UNUSED2: |
| 5585 | /* currently, no need to handle it because tcc does not | 5792 | /* currently, no need to handle it because tcc does not |
| 5586 | track unused objects */ | 5793 | track unused objects */ |
| 5587 | break; | 5794 | break; |
| 5588 | case TOK_NORETURN: | 5795 | case TOK_NORETURN1: |
| 5589 | case TOK___NORETURN__: | 5796 | case TOK_NORETURN2: |
| 5590 | /* currently, no need to handle it because tcc does not | 5797 | /* currently, no need to handle it because tcc does not |
| 5591 | track unused objects */ | 5798 | track unused objects */ |
| 5592 | break; | 5799 | break; |
| 5593 | case TOK_CDECL: | 5800 | case TOK_CDECL1: |
| 5594 | case TOK___CDECL: | 5801 | case TOK_CDECL2: |
| 5595 | case TOK___CDECL__: | 5802 | case TOK_CDECL3: |
| 5596 | ad->func_call = FUNC_CDECL; | 5803 | ad->func_call = FUNC_CDECL; |
| 5597 | break; | 5804 | break; |
| 5598 | case TOK_STDCALL: | 5805 | case TOK_STDCALL1: |
| 5599 | case TOK___STDCALL: | 5806 | case TOK_STDCALL2: |
| 5600 | case TOK___STDCALL__: | 5807 | case TOK_STDCALL3: |
| 5601 | ad->func_call = FUNC_STDCALL; | 5808 | ad->func_call = FUNC_STDCALL; |
| 5602 | break; | 5809 | break; |
| 5603 | default: | 5810 | default: |
| 5604 | warning("'%s' attribute ignored", get_tok_str(t, NULL)); | 5811 | // warning("'%s' attribute ignored", get_tok_str(t, NULL)); |
| 5605 | /* skip parameters */ | 5812 | /* skip parameters */ |
| 5606 | /* XXX: skip parenthesis too */ | 5813 | /* XXX: skip parenthesis too */ |
| 5607 | if (tok == '(') { | 5814 | if (tok == '(') { |
| @@ -5618,6 +5825,7 @@ void parse_attribute(AttributeDef *ad) · 10 unmodified lines | |||
| 5618 | } | 5825 | } |
| 5619 | skip(')'); | 5826 | skip(')'); |
| 5620 | skip(')'); | 5827 | skip(')'); |
| 5828 | } | ||
| 5621 | } | 5829 | } |
| 5622 | 5830 | ||
| 5623 | /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */ | 5831 | /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */ |
| @@ -5695,7 +5903,7 @@ static void struct_decl(CType *type, int u) · 71 unmodified lines | |||
| 5695 | if (tok != ':') { | 5903 | if (tok != ':') { |
| 5696 | type_decl(&type1, &ad, &v, TYPE_DIRECT); | 5904 | type_decl(&type1, &ad, &v, TYPE_DIRECT); |
| 5697 | if ((type1.t & VT_BTYPE) == VT_FUNC || | 5905 | if ((type1.t & VT_BTYPE) == VT_FUNC || |
| 5698 | (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN))) | 5906 | (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE))) |
| 5699 | error("invalid type for '%s'", | 5907 | error("invalid type for '%s'", |
| 5700 | get_tok_str(v, NULL)); | 5908 | get_tok_str(v, NULL)); |
| 5701 | } | 5909 | } |
| @@ -5804,6 +6012,11 @@ static int parse_btype(CType *type, AttributeDef *ad) · 102 unmodified lines | |||
| 5804 | t = 0; | 6012 | t = 0; |
| 5805 | while(1) { | 6013 | while(1) { |
| 5806 | switch(tok) { | 6014 | switch(tok) { |
| 6015 | case TOK_EXTENSION: | ||
| 6016 | /* currently, we really ignore extension */ | ||
| 6017 | next(); | ||
| 6018 | continue; | ||
| 6019 | |||
| 5807 | /* basic types */ | 6020 | /* basic types */ |
| 5808 | case TOK_CHAR: | 6021 | case TOK_CHAR: |
| 5809 | u = VT_BYTE; | 6022 | u = VT_BYTE; |
| @@ -5861,15 +6074,20 @@ static int parse_btype(CType *type, AttributeDef *ad) · 51 unmodified lines | |||
| 5861 | goto basic_type2; | 6074 | goto basic_type2; |
| 5862 | 6075 | ||
| 5863 | /* type modifiers */ | 6076 | /* type modifiers */ |
| 5864 | case TOK_CONST: | 6077 | case TOK_CONST1: |
| 5865 | case TOK_VOLATILE: | 6078 | case TOK_CONST2: |
| 6079 | case TOK_CONST3: | ||
| 6080 | case TOK_VOLATILE1: | ||
| 6081 | case TOK_VOLATILE2: | ||
| 6082 | case TOK_VOLATILE3: | ||
| 5866 | case TOK_REGISTER: | 6083 | case TOK_REGISTER: |
| 5867 | case TOK_SIGNED: | 6084 | case TOK_SIGNED1: |
| 5868 | case TOK___SIGNED__: | 6085 | case TOK_SIGNED2: |
| 6086 | case TOK_SIGNED3: | ||
| 5869 | case TOK_AUTO: | 6087 | case TOK_AUTO: |
| 5870 | case TOK_INLINE: | 6088 | case TOK_RESTRICT1: |
| 5871 | case TOK___INLINE__: | 6089 | case TOK_RESTRICT2: |
| 5872 | case TOK_RESTRICT: | 6090 | case TOK_RESTRICT3: |
| 5873 | next(); | 6091 | next(); |
| 5874 | break; | 6092 | break; |
| 5875 | case TOK_UNSIGNED: | 6093 | case TOK_UNSIGNED: |
| @@ -5890,12 +6108,22 @@ static int parse_btype(CType *type, AttributeDef *ad) · 14 unmodified lines | |||
| 5890 | t |= VT_TYPEDEF; | 6108 | t |= VT_TYPEDEF; |
| 5891 | next(); | 6109 | next(); |
| 5892 | break; | 6110 | break; |
| 6111 | case TOK_INLINE1: | ||
| 6112 | case TOK_INLINE2: | ||
| 6113 | case TOK_INLINE3: | ||
| 6114 | t |= VT_INLINE; | ||
| 6115 | next(); | ||
| 6116 | break; | ||
| 6117 | |||
| 5893 | /* GNUC attribute */ | 6118 | /* GNUC attribute */ |
| 5894 | case TOK___ATTRIBUTE__: | 6119 | case TOK_ATTRIBUTE1: |
| 6120 | case TOK_ATTRIBUTE2: | ||
| 5895 | parse_attribute(ad); | 6121 | parse_attribute(ad); |
| 5896 | break; | 6122 | break; |
| 5897 | /* GNUC typeof */ | 6123 | /* GNUC typeof */ |
| 5898 | case TOK_TYPEOF: | 6124 | case TOK_TYPEOF1: |
| 6125 | case TOK_TYPEOF2: | ||
| 6126 | case TOK_TYPEOF3: | ||
| 5899 | next(); | 6127 | next(); |
| 5900 | parse_expr_type(&type1); | 6128 | parse_expr_type(&type1); |
| 5901 | goto basic_type2; | 6129 | goto basic_type2; |
| @@ -5972,8 +6200,8 @@ static void post_type(CType *type, AttributeDef *ad) · 70 unmodified lines | |||
| 5972 | if (l == 0) | 6200 | if (l == 0) |
| 5973 | l = FUNC_OLD; | 6201 | l = FUNC_OLD; |
| 5974 | skip(')'); | 6202 | skip(')'); |
| 5975 | t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN); | 6203 | t1 = type->t & VT_STORAGE; |
| 5976 | type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN); | 6204 | type->t &= ~VT_STORAGE; |
| 5977 | post_type(type, ad); | 6205 | post_type(type, ad); |
| 5978 | /* we push a anonymous symbol which will contain the function prototype */ | 6206 | /* we push a anonymous symbol which will contain the function prototype */ |
| 5979 | s = sym_push(SYM_FIELD, type, ad->func_call, l); | 6207 | s = sym_push(SYM_FIELD, type, ad->func_call, l); |
| @@ -5991,8 +6219,8 @@ static void post_type(CType *type, AttributeDef *ad) · 11 unmodified lines | |||
| 5991 | } | 6219 | } |
| 5992 | skip(']'); | 6220 | skip(']'); |
| 5993 | /* parse next post type */ | 6221 | /* parse next post type */ |
| 5994 | t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN); | 6222 | t1 = type->t & VT_STORAGE; |
| 5995 | type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN); | 6223 | type->t &= ~VT_STORAGE; |
| 5996 | post_type(type, ad); | 6224 | post_type(type, ad); |
| 5997 | 6225 | ||
| 5998 | /* we push a anonymous symbol which will contain the array | 6226 | /* we push a anonymous symbol which will contain the array |
| @@ -6016,11 +6244,27 @@ static void type_decl(CType *type, AttributeDef *ad, int *v, int td) · 17 unmodified lines | |||
| 6016 | 6244 | ||
| 6017 | while (tok == '*') { | 6245 | while (tok == '*') { |
| 6018 | next(); | 6246 | next(); |
| 6019 | while (tok == TOK_CONST || tok == TOK_VOLATILE || tok == TOK_RESTRICT) | 6247 | redo: |
| 6248 | switch(tok) { | ||
| 6249 | case TOK_CONST1: | ||
| 6250 | case TOK_CONST2: | ||
| 6251 | case TOK_CONST3: | ||
| 6252 | case TOK_VOLATILE1: | ||
| 6253 | case TOK_VOLATILE2: | ||
| 6254 | case TOK_VOLATILE3: | ||
| 6255 | case TOK_RESTRICT1: | ||
| 6256 | case TOK_RESTRICT2: | ||
| 6257 | case TOK_RESTRICT3: | ||
| 6020 | next(); | 6258 | next(); |
| 6259 | goto redo; | ||
| 6260 | } | ||
| 6021 | mk_pointer(type); | 6261 | mk_pointer(type); |
| 6022 | } | 6262 | } |
| 6023 | 6263 | ||
| 6264 | /* XXX: clarify attribute handling */ | ||
| 6265 | if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) | ||
| 6266 | parse_attribute(ad); | ||
| 6267 | |||
| 6024 | /* recursive type */ | 6268 | /* recursive type */ |
| 6025 | /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */ | 6269 | /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */ |
| 6026 | type1.t = 0; /* XXX: same as int */ | 6270 | type1.t = 0; /* XXX: same as int */ |
| @@ -6028,7 +6272,7 @@ static void type_decl(CType *type, AttributeDef *ad, int *v, int td) · 1 unmodified line | |||
| 6028 | next(); | 6272 | next(); |
| 6029 | /* XXX: this is not correct to modify 'ad' at this point, but | 6273 | /* XXX: this is not correct to modify 'ad' at this point, but |
| 6030 | the syntax is not clear */ | 6274 | the syntax is not clear */ |
| 6031 | if (tok == TOK___ATTRIBUTE__) | 6275 | if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) |
| 6032 | parse_attribute(ad); | 6276 | parse_attribute(ad); |
| 6033 | type_decl(&type1, ad, v, td); | 6277 | type_decl(&type1, ad, v, td); |
| 6034 | skip(')'); | 6278 | skip(')'); |
| @@ -6044,7 +6288,7 @@ static void type_decl(CType *type, AttributeDef *ad, int *v, int td) · 9 unmodified lines | |||
| 6044 | } | 6288 | } |
| 6045 | } | 6289 | } |
| 6046 | post_type(type, ad); | 6290 | post_type(type, ad); |
| 6047 | if (tok == TOK___ATTRIBUTE__) | 6291 | if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) |
| 6048 | parse_attribute(ad); | 6292 | parse_attribute(ad); |
| 6049 | if (!type1.t) | 6293 | if (!type1.t) |
| 6050 | return; | 6294 | return; |
| @@ -6154,7 +6398,11 @@ static void unary(void) · 103 unmodified lines | |||
| 6154 | 6398 | ||
| 6155 | /* XXX: GCC 2.95.3 does not generate a table although it should be | 6399 | /* XXX: GCC 2.95.3 does not generate a table although it should be |
| 6156 | better here */ | 6400 | better here */ |
| 6401 | tok_next: | ||
| 6157 | switch(tok) { | 6402 | switch(tok) { |
| 6403 | case TOK_EXTENSION: | ||
| 6404 | next(); | ||
| 6405 | goto tok_next; | ||
| 6158 | case TOK_CINT: | 6406 | case TOK_CINT: |
| 6159 | case TOK_CCHAR: | 6407 | case TOK_CCHAR: |
| 6160 | case TOK_LCHAR: | 6408 | case TOK_LCHAR: |
| @@ -6241,6 +6489,13 @@ static void unary(void) · 80 unmodified lines | |||
| 6241 | unary(); | 6489 | unary(); |
| 6242 | gen_cast(&type); | 6490 | gen_cast(&type); |
| 6243 | } | 6491 | } |
| 6492 | } else if (tok == '{') { | ||
| 6493 | /* save all registers */ | ||
| 6494 | save_regs(0); | ||
| 6495 | /* statement expression : we do not accept break/continue | ||
| 6496 | inside as GCC does */ | ||
| 6497 | block(NULL, NULL, NULL, NULL, 0, 1); | ||
| 6498 | skip(')'); | ||
| 6244 | } else { | 6499 | } else { |
| 6245 | gexpr(); | 6500 | gexpr(); |
| 6246 | skip(')'); | 6501 | skip(')'); |
| @@ -6291,7 +6546,8 @@ static void unary(void) · 44 unmodified lines | |||
| 6291 | gen_op('+'); | 6546 | gen_op('+'); |
| 6292 | break; | 6547 | break; |
| 6293 | case TOK_SIZEOF: | 6548 | case TOK_SIZEOF: |
| 6294 | case TOK_ALIGNOF: | 6549 | case TOK_ALIGNOF1: |
| 6550 | case TOK_ALIGNOF2: | ||
| 6295 | t = tok; | 6551 | t = tok; |
| 6296 | next(); | 6552 | next(); |
| 6297 | if (tok == '(') { | 6553 | if (tok == '(') { |
| @@ -6328,7 +6584,10 @@ static void unary(void) · 30 unmodified lines | |||
| 6328 | expect("label identifier"); | 6584 | expect("label identifier"); |
| 6329 | s = label_find(tok); | 6585 | s = label_find(tok); |
| 6330 | if (!s) { | 6586 | if (!s) { |
| 6331 | s = label_push(tok, LABEL_FORWARD); | 6587 | s = label_push(&global_label_stack, tok, LABEL_FORWARD); |
| 6588 | } else { | ||
| 6589 | if (s->r == LABEL_DECLARED) | ||
| 6590 | s->r = LABEL_FORWARD; | ||
| 6332 | } | 6591 | } |
| 6333 | if (!s->type.t) { | 6592 | if (!s->type.t) { |
| 6334 | s->type.t = VT_VOID; | 6593 | s->type.t = VT_VOID; |
| @@ -6734,9 +6993,13 @@ static void expr_eq(void) · 399 unmodified lines | |||
| 6734 | c = vtop->c.i; | 6993 | c = vtop->c.i; |
| 6735 | vpop(); | 6994 | vpop(); |
| 6736 | next(); | 6995 | next(); |
| 6737 | gexpr(); | 6996 | if (tok == ':' && gnu_ext) { |
| 6738 | c1 = vtop->c.i; | 6997 | c1 = c; |
| 6739 | vpop(); | 6998 | } else { |
| 6999 | gexpr(); | ||
| 7000 | c1 = vtop->c.i; | ||
| 7001 | vpop(); | ||
| 7002 | } | ||
| 6740 | skip(':'); | 7003 | skip(':'); |
| 6741 | expr_eq(); | 7004 | expr_eq(); |
| 6742 | if (c) | 7005 | if (c) |
| @@ -6756,8 +7019,13 @@ static void expr_eq(void) · 13 unmodified lines | |||
| 6756 | gv(rc); | 7019 | gv(rc); |
| 6757 | save_regs(1); | 7020 | save_regs(1); |
| 6758 | } | 7021 | } |
| 6759 | tt = gtst(1, 0); | 7022 | if (tok == ':' && gnu_ext) { |
| 6760 | gexpr(); | 7023 | gv_dup(); |
| 7024 | tt = gtst(1, 0); | ||
| 7025 | } else { | ||
| 7026 | tt = gtst(1, 0); | ||
| 7027 | gexpr(); | ||
| 7028 | } | ||
| 6761 | type1 = vtop->type; | 7029 | type1 = vtop->type; |
| 6762 | sv = *vtop; /* save value to handle it later */ | 7030 | sv = *vtop; /* save value to handle it later */ |
| 6763 | vtop--; /* no vpop so that FP stack is not flushed */ | 7031 | vtop--; /* no vpop so that FP stack is not flushed */ |
| @@ -6896,30 +7164,25 @@ static int expr_const(void) · 132 unmodified lines | |||
| 6896 | return zero */ | 7164 | return zero */ |
| 6897 | static int is_label(void) | 7165 | static int is_label(void) |
| 6898 | { | 7166 | { |
| 6899 | int t; | 7167 | int last_tok; |
| 6900 | CValue c; | ||
| 6901 | 7168 | ||
| 6902 | /* fast test first */ | 7169 | /* fast test first */ |
| 6903 | if (tok < TOK_UIDENT) | 7170 | if (tok < TOK_UIDENT) |
| 6904 | return 0; | 7171 | return 0; |
| 6905 | /* no need to save tokc since we expect an identifier */ | 7172 | /* no need to save tokc because tok is an identifier */ |
| 6906 | t = tok; | 7173 | last_tok = tok; |
| 6907 | c = tokc; | ||
| 6908 | next(); | 7174 | next(); |
| 6909 | if (tok == ':') { | 7175 | if (tok == ':') { |
| 6910 | next(); | 7176 | next(); |
| 6911 | return t; | 7177 | return last_tok; |
| 6912 | } else { | 7178 | } else { |
| 6913 | /* XXX: may not work in all cases (macros ?) */ | 7179 | unget_tok(last_tok); |
| 6914 | tok1 = tok; | ||
| 6915 | tok1c = tokc; | ||
| 6916 | tok = t; | ||
| 6917 | tokc = c; | ||
| 6918 | return 0; | 7180 | return 0; |
| 6919 | } | 7181 | } |
| 6920 | } | 7182 | } |
| 6921 | 7183 | ||
| 6922 | static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg) | 7184 | static void block(int *bsym, int *csym, int *case_sym, int *def_sym, |
| 7185 | int case_reg, int is_expr) | ||
| 6923 | { | 7186 | { |
| 6924 | int a, b, c, d; | 7187 | int a, b, c, d; |
| 6925 | Sym *s; | 7188 | Sym *s; |
| @@ -6932,6 +7195,12 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 6 unmodified lines | |||
| 6932 | last_line_num = file->line_num; | 7195 | last_line_num = file->line_num; |
| 6933 | } | 7196 | } |
| 6934 | 7197 | ||
| 7198 | if (is_expr) { | ||
| 7199 | /* default return value is (void) */ | ||
| 7200 | vpushi(0); | ||
| 7201 | vtop->type.t = VT_VOID; | ||
| 7202 | } | ||
| 7203 | |||
| 6935 | if (tok == TOK_IF) { | 7204 | if (tok == TOK_IF) { |
| 6936 | /* if test */ | 7205 | /* if test */ |
| 6937 | next(); | 7206 | next(); |
| @@ -6939,13 +7208,13 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 1 unmodified line | |||
| 6939 | gexpr(); | 7208 | gexpr(); |
| 6940 | skip(')'); | 7209 | skip(')'); |
| 6941 | a = gtst(1, 0); | 7210 | a = gtst(1, 0); |
| 6942 | block(bsym, csym, case_sym, def_sym, case_reg); | 7211 | block(bsym, csym, case_sym, def_sym, case_reg, 0); |
| 6943 | c = tok; | 7212 | c = tok; |
| 6944 | if (c == TOK_ELSE) { | 7213 | if (c == TOK_ELSE) { |
| 6945 | next(); | 7214 | next(); |
| 6946 | d = gjmp(0); | 7215 | d = gjmp(0); |
| 6947 | gsym(a); | 7216 | gsym(a); |
| 6948 | block(bsym, csym, case_sym, def_sym, case_reg); | 7217 | block(bsym, csym, case_sym, def_sym, case_reg, 0); |
| 6949 | gsym(d); /* patch else jmp */ | 7218 | gsym(d); /* patch else jmp */ |
| 6950 | } else | 7219 | } else |
| 6951 | gsym(a); | 7220 | gsym(a); |
| @@ -6957,19 +7226,43 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 5 unmodified lines | |||
| 6957 | skip(')'); | 7226 | skip(')'); |
| 6958 | a = gtst(1, 0); | 7227 | a = gtst(1, 0); |
| 6959 | b = 0; | 7228 | b = 0; |
| 6960 | block(&a, &b, case_sym, def_sym, case_reg); | 7229 | block(&a, &b, case_sym, def_sym, case_reg, 0); |
| 6961 | gjmp_addr(d); | 7230 | gjmp_addr(d); |
| 6962 | gsym(a); | 7231 | gsym(a); |
| 6963 | gsym_addr(b, d); | 7232 | gsym_addr(b, d); |
| 6964 | } else if (tok == '{') { | 7233 | } else if (tok == '{') { |
| 7234 | Sym *llabel; | ||
| 7235 | |||
| 6965 | next(); | 7236 | next(); |
| 6966 | /* declarations */ | 7237 | /* record local declaration stack position */ |
| 6967 | s = local_stack; | 7238 | s = local_stack; |
| 7239 | llabel = local_label_stack; | ||
| 7240 | /* handle local labels declarations */ | ||
| 7241 | if (tok == TOK_LABEL) { | ||
| 7242 | next(); | ||
| 7243 | for(;;) { | ||
| 7244 | if (tok < TOK_UIDENT) | ||
| 7245 | expect("label identifier"); | ||
| 7246 | label_push(&local_label_stack, tok, LABEL_DECLARED); | ||
| 7247 | next(); | ||
| 7248 | if (tok == ',') { | ||
| 7249 | next(); | ||
| 7250 | } else { | ||
| 7251 | skip(';'); | ||
| 7252 | break; | ||
| 7253 | } | ||
| 7254 | } | ||
| 7255 | } | ||
| 6968 | while (tok != '}') { | 7256 | while (tok != '}') { |
| 6969 | decl(VT_LOCAL); | 7257 | decl(VT_LOCAL); |
| 6970 | if (tok != '}') | 7258 | if (tok != '}') { |
| 6971 | block(bsym, csym, case_sym, def_sym, case_reg); | 7259 | if (is_expr) |
| 7260 | vpop(); | ||
| 7261 | block(bsym, csym, case_sym, def_sym, case_reg, is_expr); | ||
| 7262 | } | ||
| 6972 | } | 7263 | } |
| 7264 | /* pop locally defined labels */ | ||
| 7265 | label_pop(&local_label_stack, llabel); | ||
| 6973 | /* pop locally defined symbols */ | 7266 | /* pop locally defined symbols */ |
| 6974 | sym_pop(&local_stack, s); | 7267 | sym_pop(&local_stack, s); |
| 6975 | next(); | 7268 | next(); |
| @@ -7039,7 +7332,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 63 unmodified lines | |||
| 7039 | gsym(e); | 7332 | gsym(e); |
| 7040 | } | 7333 | } |
| 7041 | skip(')'); | 7334 | skip(')'); |
| 7042 | block(&a, &b, case_sym, def_sym, case_reg); | 7335 | block(&a, &b, case_sym, def_sym, case_reg, 0); |
| 7043 | gjmp_addr(c); | 7336 | gjmp_addr(c); |
| 7044 | gsym(a); | 7337 | gsym(a); |
| 7045 | gsym_addr(b, c); | 7338 | gsym_addr(b, c); |
| @@ -7049,7 +7342,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 3 unmodified lines | |||
| 7049 | a = 0; | 7342 | a = 0; |
| 7050 | b = 0; | 7343 | b = 0; |
| 7051 | d = ind; | 7344 | d = ind; |
| 7052 | block(&a, &b, case_sym, def_sym, case_reg); | 7345 | block(&a, &b, case_sym, def_sym, case_reg, 0); |
| 7053 | skip(TOK_WHILE); | 7346 | skip(TOK_WHILE); |
| 7054 | skip('('); | 7347 | skip('('); |
| 7055 | gsym(b); | 7348 | gsym(b); |
| @@ -7071,7 +7364,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 15 unmodified lines | |||
| 7071 | a = 0; | 7364 | a = 0; |
| 7072 | b = gjmp(0); /* jump to first case */ | 7365 | b = gjmp(0); /* jump to first case */ |
| 7073 | c = 0; | 7366 | c = 0; |
| 7074 | block(&a, csym, &b, &c, case_reg); | 7367 | block(&a, csym, &b, &c, case_reg, 0); |
| 7075 | /* if no default, jmp after switch */ | 7368 | /* if no default, jmp after switch */ |
| 7076 | if (c == 0) | 7369 | if (c == 0) |
| 7077 | c = ind; | 7370 | c = ind; |
| @@ -7111,7 +7404,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 33 unmodified lines | |||
| 7111 | } | 7404 | } |
| 7112 | gsym(b); | 7405 | gsym(b); |
| 7113 | skip(':'); | 7406 | skip(':'); |
| 7114 | block(bsym, csym, case_sym, def_sym, case_reg); | 7407 | block(bsym, csym, case_sym, def_sym, case_reg, 0); |
| 7115 | } else | 7408 | } else |
| 7116 | if (tok == TOK_DEFAULT) { | 7409 | if (tok == TOK_DEFAULT) { |
| 7117 | next(); | 7410 | next(); |
| @@ -7121,7 +7414,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 3 unmodified lines | |||
| 7121 | if (*def_sym) | 7414 | if (*def_sym) |
| 7122 | error("too many 'default'"); | 7415 | error("too many 'default'"); |
| 7123 | *def_sym = ind; | 7416 | *def_sym = ind; |
| 7124 | block(bsym, csym, case_sym, def_sym, case_reg); | 7417 | block(bsym, csym, case_sym, def_sym, case_reg, 0); |
| 7125 | } else | 7418 | } else |
| 7126 | if (tok == TOK_GOTO) { | 7419 | if (tok == TOK_GOTO) { |
| 7127 | next(); | 7420 | next(); |
| @@ -7136,7 +7429,10 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 8 unmodified lines | |||
| 7136 | s = label_find(tok); | 7429 | s = label_find(tok); |
| 7137 | /* put forward definition if needed */ | 7430 | /* put forward definition if needed */ |
| 7138 | if (!s) { | 7431 | if (!s) { |
| 7139 | s = label_push(tok, LABEL_FORWARD); | 7432 | s = label_push(&global_label_stack, tok, LABEL_FORWARD); |
| 7433 | } else { | ||
| 7434 | if (s->r == LABEL_DECLARED) | ||
| 7435 | s->r = LABEL_FORWARD; | ||
| 7140 | } | 7436 | } |
| 7141 | /* label already defined */ | 7437 | /* label already defined */ |
| 7142 | if (s->r & LABEL_FORWARD) | 7438 | if (s->r & LABEL_FORWARD) |
| @@ -7148,30 +7444,40 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 5 unmodified lines | |||
| 7148 | expect("label identifier"); | 7444 | expect("label identifier"); |
| 7149 | } | 7445 | } |
| 7150 | skip(';'); | 7446 | skip(';'); |
| 7447 | } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) { | ||
| 7448 | asm_instr(); | ||
| 7151 | } else { | 7449 | } else { |
| 7152 | b = is_label(); | 7450 | b = is_label(); |
| 7153 | if (b) { | 7451 | if (b) { |
| 7154 | /* label case */ | 7452 | /* label case */ |
| 7155 | s = label_find(b); | 7453 | s = label_find(b); |
| 7156 | if (s) { | 7454 | if (s) { |
| 7157 | if (!(s->r & LABEL_FORWARD)) | 7455 | if (s->r == LABEL_DEFINED) |
| 7158 | error("multiple defined label"); | 7456 | error("duplicate label '%s'", get_tok_str(s->v, NULL)); |
| 7159 | gsym((long)s->next); | 7457 | gsym((long)s->next); |
| 7458 | s->r = LABEL_DEFINED; | ||
| 7160 | } else { | 7459 | } else { |
| 7161 | s = label_push(b, 0); | 7460 | s = label_push(&global_label_stack, b, LABEL_DEFINED); |
| 7162 | } | 7461 | } |
| 7163 | s->next = (void *)ind; | 7462 | s->next = (void *)ind; |
| 7164 | s->r = 0; | ||
| 7165 | /* we accept this, but it is a mistake */ | 7463 | /* we accept this, but it is a mistake */ |
| 7166 | if (tok == '}') | 7464 | if (tok == '}') { |
| 7167 | warning("deprecated use of label at end of compound statement"); | 7465 | warning("deprecated use of label at end of compound statement"); |
| 7168 | else | 7466 | } else { |
| 7169 | block(bsym, csym, case_sym, def_sym, case_reg); | 7467 | if (is_expr) |
| 7468 | vpop(); | ||
| 7469 | block(bsym, csym, case_sym, def_sym, case_reg, is_expr); | ||
| 7470 | } | ||
| 7170 | } else { | 7471 | } else { |
| 7171 | /* expression case */ | 7472 | /* expression case */ |
| 7172 | if (tok != ';') { | 7473 | if (tok != ';') { |
| 7173 | gexpr(); | 7474 | if (is_expr) { |
| 7174 | vpop(); | 7475 | vpop(); |
| 7476 | gexpr(); | ||
| 7477 | } else { | ||
| 7478 | gexpr(); | ||
| 7479 | vpop(); | ||
| 7480 | } | ||
| 7175 | } | 7481 | } |
| 7176 | skip(';'); | 7482 | skip(';'); |
| 7177 | } | 7483 | } |
| @@ -7187,13 +7493,14 @@ static void decl_designator(CType *type, Section *sec, unsigned long c, · 9 unmodified lines | |||
| 7187 | int size_only) | 7493 | int size_only) |
| 7188 | { | 7494 | { |
| 7189 | Sym *s, *f; | 7495 | Sym *s, *f; |
| 7190 | int notfirst, index, align, l; | 7496 | int notfirst, index, index_last, align, l, nb_elems, elem_size; |
| 7191 | CType type1; | 7497 | CType type1; |
| 7192 | 7498 | ||
| 7193 | notfirst = 0; | 7499 | notfirst = 0; |
| 7500 | elem_size = 0; | ||
| 7501 | nb_elems = 1; | ||
| 7194 | if (gnu_ext && (l = is_label()) != 0) | 7502 | if (gnu_ext && (l = is_label()) != 0) |
| 7195 | goto struct_field; | 7503 | goto struct_field; |
| 7196 | |||
| 7197 | while (tok == '[' || tok == '.') { | 7504 | while (tok == '[' || tok == '.') { |
| 7198 | if (tok == '[') { | 7505 | if (tok == '[') { |
| 7199 | if (!(type->t & VT_ARRAY)) | 7506 | if (!(type->t & VT_ARRAY)) |
| @@ -7203,11 +7510,28 @@ static void decl_designator(CType *type, Section *sec, unsigned long c, · 3 unmodified lines | |||
| 7203 | index = expr_const(); | 7510 | index = expr_const(); |
| 7204 | if (index < 0 || (s->c >= 0 && index >= s->c)) | 7511 | if (index < 0 || (s->c >= 0 && index >= s->c)) |
| 7205 | expect("invalid index"); | 7512 | expect("invalid index"); |
| 7513 | if (tok == TOK_DOTS && gnu_ext) { | ||
| 7514 | next(); | ||
| 7515 | index_last = expr_const(); | ||
| 7516 | if (index_last < 0 || | ||
| 7517 | (s->c >= 0 && index_last >= s->c) || | ||
| 7518 | index_last < index) | ||
| 7519 | expect("invalid index"); | ||
| 7520 | } else { | ||
| 7521 | index_last = index; | ||
| 7522 | } | ||
| 7206 | skip(']'); | 7523 | skip(']'); |
| 7207 | if (!notfirst) | 7524 | if (!notfirst) |
| 7208 | *cur_index = index; | 7525 | *cur_index = index_last; |
| 7209 | type = pointed_type(type); | 7526 | type = pointed_type(type); |
| 7210 | c += index * type_size(type, &align); | 7527 | elem_size = type_size(type, &align); |
| 7528 | c += index * elem_size; | ||
| 7529 | /* NOTE: we only support ranges for last designator */ | ||
| 7530 | nb_elems = index_last - index + 1; | ||
| 7531 | if (nb_elems != 1) { | ||
| 7532 | notfirst = 1; | ||
| 7533 | break; | ||
| 7534 | } | ||
| 7211 | } else { | 7535 | } else { |
| 7212 | next(); | 7536 | next(); |
| 7213 | l = tok; | 7537 | l = tok; |
| @@ -7259,6 +7583,25 @@ static void decl_designator(CType *type, Section *sec, unsigned long c, · 45 unmodified lines | |||
| 7259 | } | 7583 | } |
| 7260 | } | 7584 | } |
| 7261 | decl_initializer(type, sec, c, 0, size_only); | 7585 | decl_initializer(type, sec, c, 0, size_only); |
| 7586 | |||
| 7587 | /* XXX: make it more general */ | ||
| 7588 | if (!size_only && nb_elems > 1) { | ||
| 7589 | unsigned long c_end; | ||
| 7590 | uint8_t *src, *dst; | ||
| 7591 | int i; | ||
| 7592 | |||
| 7593 | if (!sec) | ||
| 7594 | error("range init not supported yet for dynamic storage"); | ||
| 7595 | c_end = c + nb_elems * elem_size; | ||
| 7596 | if (c_end > sec->data_allocated) | ||
| 7597 | section_realloc(sec, c_end); | ||
| 7598 | src = sec->data + c; | ||
| 7599 | dst = src; | ||
| 7600 | for(i = 1; i < nb_elems; i++) { | ||
| 7601 | dst += elem_size; | ||
| 7602 | memcpy(dst, src, elem_size); | ||
| 7603 | } | ||
| 7604 | } | ||
| 7262 | } | 7605 | } |
| 7263 | 7606 | ||
| 7264 | #define EXPR_VAL 0 | 7607 | #define EXPR_VAL 0 |
| @@ -7482,9 +7825,32 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 217 unmodified lines | |||
| 7482 | s->c = array_length; | 7825 | s->c = array_length; |
| 7483 | } else if ((type->t & VT_BTYPE) == VT_STRUCT && | 7826 | } else if ((type->t & VT_BTYPE) == VT_STRUCT && |
| 7484 | (sec || !first || tok == '{')) { | 7827 | (sec || !first || tok == '{')) { |
| 7828 | int par_count; | ||
| 7829 | |||
| 7485 | /* NOTE: the previous test is a specific case for automatic | 7830 | /* NOTE: the previous test is a specific case for automatic |
| 7486 | struct/union init */ | 7831 | struct/union init */ |
| 7487 | /* XXX: union needs only one init */ | 7832 | /* XXX: union needs only one init */ |
| 7833 | |||
| 7834 | /* XXX: this test is incorrect for local initializers | ||
| 7835 | beginning with ( without {. It would be much more difficult | ||
| 7836 | to do it correctly (ideally, the expression parser should | ||
| 7837 | be used in all cases) */ | ||
| 7838 | par_count = 0; | ||
| 7839 | if (tok == '(') { | ||
| 7840 | AttributeDef ad1; | ||
| 7841 | CType type1; | ||
| 7842 | next(); | ||
| 7843 | while (tok == '(') { | ||
| 7844 | par_count++; | ||
| 7845 | next(); | ||
| 7846 | } | ||
| 7847 | if (!parse_btype(&type1, &ad1)) | ||
| 7848 | expect("cast"); | ||
| 7849 | type_decl(&type1, &ad1, &n, TYPE_ABSTRACT); | ||
| 7850 | if (!is_compatible_types(type, &type1)) | ||
| 7851 | error("invalid type for cast"); | ||
| 7852 | skip(')'); | ||
| 7853 | } | ||
| 7488 | no_oblock = 1; | 7854 | no_oblock = 1; |
| 7489 | if (first || tok == '{') { | 7855 | if (first || tok == '{') { |
| 7490 | skip('{'); | 7856 | skip('{'); |
| @@ -7519,6 +7885,10 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 28 unmodified lines | |||
| 7519 | } | 7885 | } |
| 7520 | if (!no_oblock) | 7886 | if (!no_oblock) |
| 7521 | skip('}'); | 7887 | skip('}'); |
| 7888 | while (par_count) { | ||
| 7889 | skip(')'); | ||
| 7890 | par_count--; | ||
| 7891 | } | ||
| 7522 | } else if (tok == '{') { | 7892 | } else if (tok == '{') { |
| 7523 | next(); | 7893 | next(); |
| 7524 | decl_initializer(type, sec, c, first, size_only); | 7894 | decl_initializer(type, sec, c, first, size_only); |
| @@ -7619,15 +7989,8 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 94 unmodified lines | |||
| 7619 | sec = NULL; | 7989 | sec = NULL; |
| 7620 | if (do_bounds_check && (type->t & VT_ARRAY)) | 7990 | if (do_bounds_check && (type->t & VT_ARRAY)) |
| 7621 | loc--; | 7991 | loc--; |
| 7622 | #ifdef TCC_TARGET_IL | ||
| 7623 | /* XXX: ugly patch to allocate local variables for IL, just | ||
| 7624 | for testing */ | ||
| 7625 | addr = loc; | ||
| 7626 | loc++; | ||
| 7627 | #else | ||
| 7628 | loc = (loc - size) & -align; | 7992 | loc = (loc - size) & -align; |
| 7629 | addr = loc; | 7993 | addr = loc; |
| 7630 | #endif | ||
| 7631 | /* handles bounds */ | 7994 | /* handles bounds */ |
| 7632 | /* XXX: currently, since we do only one pass, we cannot track | 7995 | /* XXX: currently, since we do only one pass, we cannot track |
| 7633 | '&' operators, so we add only arrays */ | 7996 | '&' operators, so we add only arrays */ |
| @@ -7640,31 +8003,6 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 6 unmodified lines | |||
| 7640 | bounds_ptr[0] = addr; | 8003 | bounds_ptr[0] = addr; |
| 7641 | bounds_ptr[1] = size; | 8004 | bounds_ptr[1] = size; |
| 7642 | } | 8005 | } |
| 7643 | } else { | ||
| 7644 | /* compute section */ | ||
| 7645 | sec = ad->section; | ||
| 7646 | if (!sec) { | ||
| 7647 | if (has_init) | ||
| 7648 | sec = data_section; | ||
| 7649 | else | ||
| 7650 | sec = bss_section; | ||
| 7651 | } | ||
| 7652 | data_offset = sec->data_offset; | ||
| 7653 | data_offset = (data_offset + align - 1) & -align; | ||
| 7654 | addr = data_offset; | ||
| 7655 | /* very important to increment global pointer at this time | ||
| 7656 | because initializers themselves can create new initializers */ | ||
| 7657 | data_offset += size; | ||
| 7658 | /* add padding if bound check */ | ||
| 7659 | if (do_bounds_check) | ||
| 7660 | data_offset++; | ||
| 7661 | sec->data_offset = data_offset; | ||
| 7662 | /* allocate section space to put the data */ | ||
| 7663 | if (sec->sh_type != SHT_NOBITS && | ||
| 7664 | data_offset > sec->data_allocated) | ||
| 7665 | section_realloc(sec, data_offset); | ||
| 7666 | } | ||
| 7667 | if (!sec) { | ||
| 7668 | if (v) { | 8006 | if (v) { |
| 7669 | /* local variable */ | 8007 | /* local variable */ |
| 7670 | sym_push(v, type, r, addr); | 8008 | sym_push(v, type, r, addr); |
| @@ -7674,24 +8012,76 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 3 unmodified lines | |||
| 7674 | } | 8012 | } |
| 7675 | } else { | 8013 | } else { |
| 7676 | Sym *sym; | 8014 | Sym *sym; |
| 7677 | 8015 | ||
| 8016 | sym = NULL; | ||
| 8017 | if (v && scope == VT_CONST) { | ||
| 8018 | /* see if the symbol was already defined */ | ||
| 8019 | sym = sym_find(v); | ||
| 8020 | if (sym) { | ||
| 8021 | if (!is_compatible_types(&sym->type, type)) | ||
| 8022 | error("incompatible types for redefinition of '%s'", | ||
| 8023 | get_tok_str(v, NULL)); | ||
| 8024 | if (sym->type.t & VT_EXTERN) { | ||
| 8025 | /* if the variable is extern, it was not allocated */ | ||
| 8026 | sym->type.t &= ~VT_EXTERN; | ||
| 8027 | } else { | ||
| 8028 | /* we accept several definitions of the same | ||
| 8029 | global variable. this is tricky, because we | ||
| 8030 | must play with the SHN_COMMON type of the symbol */ | ||
| 8031 | /* XXX: should check if the variable was already | ||
| 8032 | initialized. It is incorrect to initialized it | ||
| 8033 | twice */ | ||
| 8034 | /* no init data, we won't add more to the symbol */ | ||
| 8035 | if (!has_init) | ||
| 8036 | goto no_alloc; | ||
| 8037 | } | ||
| 8038 | } | ||
| 8039 | } | ||
| 8040 | |||
| 8041 | /* allocate symbol in corresponding section */ | ||
| 8042 | sec = ad->section; | ||
| 8043 | if (!sec) { | ||
| 8044 | if (has_init) | ||
| 8045 | sec = data_section; | ||
| 8046 | } | ||
| 8047 | if (sec) { | ||
| 8048 | data_offset = sec->data_offset; | ||
| 8049 | data_offset = (data_offset + align - 1) & -align; | ||
| 8050 | addr = data_offset; | ||
| 8051 | /* very important to increment global pointer at this time | ||
| 8052 | because initializers themselves can create new initializers */ | ||
| 8053 | data_offset += size; | ||
| 8054 | /* add padding if bound check */ | ||
| 8055 | if (do_bounds_check) | ||
| 8056 | data_offset++; | ||
| 8057 | sec->data_offset = data_offset; | ||
| 8058 | /* allocate section space to put the data */ | ||
| 8059 | if (sec->sh_type != SHT_NOBITS && | ||
| 8060 | data_offset > sec->data_allocated) | ||
| 8061 | section_realloc(sec, data_offset); | ||
| 8062 | } else { | ||
| 8063 | addr = 0; /* avoid warning */ | ||
| 8064 | } | ||
| 8065 | |||
| 7678 | if (v) { | 8066 | if (v) { |
| 7679 | if (scope == VT_CONST) { | 8067 | if (scope == VT_CONST) { |
| 7680 | /* global scope: see if already defined */ | ||
| 7681 | sym = sym_find(v); | ||
| 7682 | if (!sym) | 8068 | if (!sym) |
| 7683 | goto do_def; | 8069 | goto do_def; |
| 7684 | if (!is_compatible_types(&sym->type, type)) | ||
| 7685 | error("incompatible types for redefinition of '%s'", | ||
| 7686 | get_tok_str(v, NULL)); | ||
| 7687 | if (!(sym->type.t & VT_EXTERN)) | ||
| 7688 | error("redefinition of '%s'", get_tok_str(v, NULL)); | ||
| 7689 | sym->type.t &= ~VT_EXTERN; | ||
| 7690 | } else { | 8070 | } else { |
| 7691 | do_def: | 8071 | do_def: |
| 7692 | sym = sym_push(v, type, r | VT_SYM, 0); | 8072 | sym = sym_push(v, type, r | VT_SYM, 0); |
| 7693 | } | 8073 | } |
| 7694 | put_extern_sym(sym, sec, addr, size); | 8074 | /* update symbol definition */ |
| 8075 | if (sec) { | ||
| 8076 | put_extern_sym(sym, sec, addr, size); | ||
| 8077 | } else { | ||
| 8078 | Elf32_Sym *esym; | ||
| 8079 | /* put a common area */ | ||
| 8080 | put_extern_sym(sym, NULL, align, size); | ||
| 8081 | /* XXX: find a nicer way */ | ||
| 8082 | esym = &((Elf32_Sym *)symtab_section->data)[sym->c]; | ||
| 8083 | esym->st_shndx = SHN_COMMON; | ||
| 8084 | } | ||
| 7695 | } else { | 8085 | } else { |
| 7696 | CValue cval; | 8086 | CValue cval; |
| 7697 | 8087 | ||
| @@ -7722,6 +8112,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 24 unmodified lines | |||
| 7722 | restore_parse_state(&saved_parse_state); | 8112 | restore_parse_state(&saved_parse_state); |
| 7723 | } | 8113 | } |
| 7724 | } | 8114 | } |
| 8115 | no_alloc: ; | ||
| 7725 | } | 8116 | } |
| 7726 | 8117 | ||
| 7727 | void put_func_debug(Sym *sym) | 8118 | void put_func_debug(Sym *sym) |
| @@ -7829,8 +8220,10 @@ static void func_decl_list(Sym *func_sym) · 101 unmodified lines | |||
| 7829 | get_tok_str(v, NULL)); | 8220 | get_tok_str(v, NULL)); |
| 7830 | found: | 8221 | found: |
| 7831 | /* check that no storage specifier except 'register' was given */ | 8222 | /* check that no storage specifier except 'register' was given */ |
| 7832 | if (type.t & (VT_EXTERN | VT_STATIC | VT_TYPEDEF)) | 8223 | if (type.t & VT_STORAGE) |
| 7833 | error("storage class specified for '%s'", get_tok_str(v, NULL)); | 8224 | error("storage class specified for '%s'", get_tok_str(v, NULL)); |
| 8225 | /* array must be transformed to pointer according to ANSI C */ | ||
| 8226 | type.t &= ~VT_ARRAY; | ||
| 7834 | /* we can add the type (NOTE: it could be local to the function) */ | 8227 | /* we can add the type (NOTE: it could be local to the function) */ |
| 7835 | s->type = type; | 8228 | s->type = type; |
| 7836 | /* accept other parameters */ | 8229 | /* accept other parameters */ |
| @@ -7902,6 +8295,9 @@ static void decl(int l) · 65 unmodified lines | |||
| 7902 | error("cannot use local functions"); | 8295 | error("cannot use local functions"); |
| 7903 | if (!(type.t & VT_FUNC)) | 8296 | if (!(type.t & VT_FUNC)) |
| 7904 | expect("function definition"); | 8297 | expect("function definition"); |
| 8298 | /* XXX: cannot do better now: convert extern line to static inline */ | ||
| 8299 | if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE)) | ||
| 8300 | type.t = (type.t & ~VT_EXTERN) | VT_STATIC; | ||
| 7905 | 8301 | ||
| 7906 | #ifdef CONFIG_REG_VARS | 8302 | #ifdef CONFIG_REG_VARS |
| 7907 | /* parse all function code and record it */ | 8303 | /* parse all function code and record it */ |
| @@ -7965,31 +8361,11 @@ static void decl(int l) · 57 unmodified lines | |||
| 7965 | macro_ptr = func_str.str; | 8361 | macro_ptr = func_str.str; |
| 7966 | next(); | 8362 | next(); |
| 7967 | #endif | 8363 | #endif |
| 7968 | block(NULL, NULL, NULL, NULL, 0); | 8364 | block(NULL, NULL, NULL, NULL, 0, 0); |
| 7969 | gsym(rsym); | 8365 | gsym(rsym); |
| 7970 | gfunc_epilog(); | 8366 | gfunc_epilog(); |
| 7971 | cur_text_section->data_offset = ind; | 8367 | cur_text_section->data_offset = ind; |
| 7972 | /* look if any labels are undefined. Define symbols if | 8368 | label_pop(&global_label_stack, NULL); |
| 7973 | '&&label' was used. */ | ||
| 7974 | { | ||
| 7975 | Sym *s, *s1; | ||
| 7976 | for(s = label_stack; s != NULL; s = s1) { | ||
| 7977 | s1 = s->prev; | ||
| 7978 | if (s->r & LABEL_FORWARD) { | ||
| 7979 | error("label '%s' used but not defined", | ||
| 7980 | get_tok_str(s->v, NULL)); | ||
| 7981 | } | ||
| 7982 | if (s->c) { | ||
| 7983 | /* define corresponding symbol. A size of | ||
| 7984 | 1 is put. */ | ||
| 7985 | put_extern_sym(s, cur_text_section, (long)s->next, 1); | ||
| 7986 | } | ||
| 7987 | /* remove label */ | ||
| 7988 | table_ident[s->v - TOK_IDENT]->sym_label = NULL; | ||
| 7989 | tcc_free(s); | ||
| 7990 | } | ||
| 7991 | label_stack = NULL; | ||
| 7992 | } | ||
| 7993 | sym_pop(&local_stack, NULL); /* reset local stack */ | 8369 | sym_pop(&local_stack, NULL); /* reset local stack */ |
| 7994 | /* end of function */ | 8370 | /* end of function */ |
| 7995 | /* patch symbol size */ | 8371 | /* patch symbol size */ |
| @@ -8021,15 +8397,20 @@ static void decl(int l) · 25 unmodified lines | |||
| 8021 | r = 0; | 8397 | r = 0; |
| 8022 | if (!(type.t & VT_ARRAY)) | 8398 | if (!(type.t & VT_ARRAY)) |
| 8023 | r |= lvalue_type(type.t); | 8399 | r |= lvalue_type(type.t); |
| 8024 | if (btype.t & VT_EXTERN) { | 8400 | has_init = (tok == '='); |
| 8401 | if ((btype.t & VT_EXTERN) || | ||
| 8402 | ((type.t & VT_ARRAY) && (type.t & VT_STATIC) && | ||
| 8403 | !has_init && l == VT_CONST && type.ref->c < 0)) { | ||
| 8025 | /* external variable */ | 8404 | /* external variable */ |
| 8405 | /* NOTE: as GCC, uninitialized global static | ||
| 8406 | arrays of null size are considered as | ||
| 8407 | extern */ | ||
| 8026 | external_sym(v, &type, r); | 8408 | external_sym(v, &type, r); |
| 8027 | } else { | 8409 | } else { |
| 8028 | if (type.t & VT_STATIC) | 8410 | if (type.t & VT_STATIC) |
| 8029 | r |= VT_CONST; | 8411 | r |= VT_CONST; |
| 8030 | else | 8412 | else |
| 8031 | r |= l; | 8413 | r |= l; |
| 8032 | has_init = (tok == '='); | ||
| 8033 | if (has_init) | 8414 | if (has_init) |
| 8034 | next(); | 8415 | next(); |
| 8035 | decl_initializer_alloc(&type, &ad, r, | 8416 | decl_initializer_alloc(&type, &ad, r, |
| @@ -8046,6 +8427,20 @@ static void decl(int l) · 10 unmodified lines | |||
| 8046 | } | 8427 | } |
| 8047 | } | 8428 | } |
| 8048 | 8429 | ||
| 8430 | /* better than nothing, but needs extension to handle '-E' option | ||
| 8431 | correctly too */ | ||
| 8432 | static void preprocess_init(TCCState *s1) | ||
| 8433 | { | ||
| 8434 | s1->include_stack_ptr = s1->include_stack; | ||
| 8435 | /* XXX: move that before to avoid having to initialize | ||
| 8436 | file->ifdef_stack_ptr ? */ | ||
| 8437 | s1->ifdef_stack_ptr = s1->ifdef_stack; | ||
| 8438 | file->ifdef_stack_ptr = s1->ifdef_stack_ptr; | ||
| 8439 | |||
| 8440 | /* XXX: not ANSI compliant: bound checking says error */ | ||
| 8441 | vtop = vstack - 1; | ||
| 8442 | } | ||
| 8443 | |||
| 8049 | /* compile the C file opened in 'file'. Return non zero if errors. */ | 8444 | /* compile the C file opened in 'file'. Return non zero if errors. */ |
| 8050 | static int tcc_compile(TCCState *s1) | 8445 | static int tcc_compile(TCCState *s1) |
| 8051 | { | 8446 | { |
| @@ -8056,15 +8451,9 @@ static int tcc_compile(TCCState *s1) · 4 unmodified lines | |||
| 8056 | #ifdef INC_DEBUG | 8451 | #ifdef INC_DEBUG |
| 8057 | printf("%s: **** new file\n", file->filename); | 8452 | printf("%s: **** new file\n", file->filename); |
| 8058 | #endif | 8453 | #endif |
| 8059 | funcname = ""; | 8454 | preprocess_init(s1); |
| 8060 | s1->include_stack_ptr = s1->include_stack; | ||
| 8061 | /* XXX: move that before to avoid having to initialize | ||
| 8062 | file->ifdef_stack_ptr ? */ | ||
| 8063 | s1->ifdef_stack_ptr = s1->ifdef_stack; | ||
| 8064 | file->ifdef_stack_ptr = s1->ifdef_stack_ptr; | ||
| 8065 | 8455 | ||
| 8066 | /* XXX: not ANSI compliant: bound checking says error */ | 8456 | funcname = ""; |
| 8067 | vtop = vstack - 1; | ||
| 8068 | anon_sym = SYM_FIRST_ANOM; | 8457 | anon_sym = SYM_FIRST_ANOM; |
| 8069 | 8458 | ||
| 8070 | /* file info: full path + filename */ | 8459 | /* file info: full path + filename */ |
| @@ -8117,6 +8506,7 @@ static int tcc_compile(TCCState *s1) · 46 unmodified lines | |||
| 8117 | 8506 | ||
| 8118 | ch = file->buf_ptr[0]; | 8507 | ch = file->buf_ptr[0]; |
| 8119 | tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; | 8508 | tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF; |
| 8509 | parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM; | ||
| 8120 | next(); | 8510 | next(); |
| 8121 | decl(VT_CONST); | 8511 | decl(VT_CONST); |
| 8122 | if (tok != -1) | 8512 | if (tok != -1) |
| @@ -8133,9 +8523,9 @@ static int tcc_compile(TCCState *s1) · 10 unmodified lines | |||
| 8133 | /* reset define stack, but leave -Dsymbols (may be incorrect if | 8523 | /* reset define stack, but leave -Dsymbols (may be incorrect if |
| 8134 | they are undefined) */ | 8524 | they are undefined) */ |
| 8135 | free_defines(define_start); | 8525 | free_defines(define_start); |
| 8136 | 8526 | ||
| 8137 | sym_pop(&global_stack, NULL); | 8527 | sym_pop(&global_stack, NULL); |
| 8138 | 8528 | ||
| 8139 | return s1->nb_errors != 0 ? -1 : 0; | 8529 | return s1->nb_errors != 0 ? -1 : 0; |
| 8140 | } | 8530 | } |
| 8141 | 8531 | ||
| @@ -8212,6 +8602,18 @@ void tcc_undefine_symbol(TCCState *s1, const char *sym) · 70 unmodified lines | |||
| 8212 | define_undef(s); | 8602 | define_undef(s); |
| 8213 | } | 8603 | } |
| 8214 | 8604 | ||
| 8605 | #ifdef CONFIG_TCC_ASM | ||
| 8606 | |||
| 8607 | #include "i386-asm.c" | ||
| 8608 | #include "tccasm.c" | ||
| 8609 | |||
| 8610 | #else | ||
| 8611 | static void asm_instr(void) | ||
| 8612 | { | ||
| 8613 | error("inline asm() not supported"); | ||
| 8614 | } | ||
| 8615 | #endif | ||
| 8616 | |||
| 8215 | #include "tccelf.c" | 8617 | #include "tccelf.c" |
| 8216 | 8618 | ||
| 8217 | /* print the position in the source file of PC value 'pc' by reading | 8619 | /* print the position in the source file of PC value 'pc' by reading |
| @@ -8526,6 +8928,8 @@ TCCState *tcc_new(void) · 308 unmodified lines | |||
| 8526 | { | 8928 | { |
| 8527 | const char *p, *r; | 8929 | const char *p, *r; |
| 8528 | TCCState *s; | 8930 | TCCState *s; |
| 8931 | TokenSym *ts; | ||
| 8932 | int i, c; | ||
| 8529 | 8933 | ||
| 8530 | s = tcc_mallocz(sizeof(TCCState)); | 8934 | s = tcc_mallocz(sizeof(TCCState)); |
| 8531 | if (!s) | 8935 | if (!s) |
| @@ -8533,10 +8937,9 @@ TCCState *tcc_new(void) · 1 unmodified line | |||
| 8533 | tcc_state = s; | 8937 | tcc_state = s; |
| 8534 | s->output_type = TCC_OUTPUT_MEMORY; | 8938 | s->output_type = TCC_OUTPUT_MEMORY; |
| 8535 | 8939 | ||
| 8536 | /* default include paths */ | 8940 | /* init isid table */ |
| 8537 | tcc_add_sysinclude_path(s, "/usr/local/include"); | 8941 | for(i=0;i<256;i++) |
| 8538 | tcc_add_sysinclude_path(s, "/usr/include"); | 8942 | isidnum_table[i] = isid(i) || isnum(i); |
| 8539 | tcc_add_sysinclude_path(s, CONFIG_TCC_PREFIX "/lib/tcc/include"); | ||
| 8540 | 8943 | ||
| 8541 | /* add all tokens */ | 8944 | /* add all tokens */ |
| 8542 | table_ident = NULL; | 8945 | table_ident = NULL; |
| @@ -8546,8 +8949,12 @@ TCCState *tcc_new(void) · 3 unmodified lines | |||
| 8546 | p = tcc_keywords; | 8949 | p = tcc_keywords; |
| 8547 | while (*p) { | 8950 | while (*p) { |
| 8548 | r = p; | 8951 | r = p; |
| 8549 | while (*r++); | 8952 | for(;;) { |
| 8550 | tok_alloc(p, r - p - 1); | 8953 | c = *r++; |
| 8954 | if (c == '\0') | ||
| 8955 | break; | ||
| 8956 | } | ||
| 8957 | ts = tok_alloc(p, r - p - 1); | ||
| 8551 | p = r; | 8958 | p = r; |
| 8552 | } | 8959 | } |
| 8553 | 8960 | ||
| @@ -8564,6 +8971,7 @@ TCCState *tcc_new(void) · 10 unmodified lines | |||
| 8564 | tcc_define_symbol(s, "__i386__", NULL); | 8971 | tcc_define_symbol(s, "__i386__", NULL); |
| 8565 | #endif | 8972 | #endif |
| 8566 | #if defined(linux) | 8973 | #if defined(linux) |
| 8974 | tcc_define_symbol(s, "__linux__", NULL); | ||
| 8567 | tcc_define_symbol(s, "linux", NULL); | 8975 | tcc_define_symbol(s, "linux", NULL); |
| 8568 | #endif | 8976 | #endif |
| 8569 | /* tiny C specific defines */ | 8977 | /* tiny C specific defines */ |
| @@ -8700,7 +9108,17 @@ static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 130 unmodified lines | |||
| 8700 | if (!ext || !strcmp(ext, "c")) { | 9108 | if (!ext || !strcmp(ext, "c")) { |
| 8701 | /* C file assumed */ | 9109 | /* C file assumed */ |
| 8702 | ret = tcc_compile(s1); | 9110 | ret = tcc_compile(s1); |
| 8703 | } else { | 9111 | } else |
| 9112 | #ifdef CONFIG_TCC_ASM | ||
| 9113 | if (!strcmp(ext, "S")) { | ||
| 9114 | /* preprocessed assembler */ | ||
| 9115 | ret = tcc_assemble(s1, 1); | ||
| 9116 | } else if (!strcmp(ext, "s")) { | ||
| 9117 | /* non preprocessed assembler */ | ||
| 9118 | ret = tcc_assemble(s1, 0); | ||
| 9119 | } else | ||
| 9120 | #endif | ||
| 9121 | { | ||
| 8704 | fd = file->fd; | 9122 | fd = file->fd; |
| 8705 | /* assume executable format: auto guess file type */ | 9123 | /* assume executable format: auto guess file type */ |
| 8706 | if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) { | 9124 | if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) { |
| @@ -8819,8 +9237,19 @@ int tcc_add_symbol(TCCState *s, const char *name, unsigned long val) · 112 unmodified lines | |||
| 8819 | 9237 | ||
| 8820 | int tcc_set_output_type(TCCState *s, int output_type) | 9238 | int tcc_set_output_type(TCCState *s, int output_type) |
| 8821 | { | 9239 | { |
| 9240 | char buf[1024]; | ||
| 9241 | |||
| 8822 | s->output_type = output_type; | 9242 | s->output_type = output_type; |
| 8823 | 9243 | ||
| 9244 | if (!s->nostdinc) { | ||
| 9245 | /* default include paths */ | ||
| 9246 | /* XXX: reverse order needed if -isystem support */ | ||
| 9247 | tcc_add_sysinclude_path(s, "/usr/local/include"); | ||
| 9248 | tcc_add_sysinclude_path(s, "/usr/include"); | ||
| 9249 | snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path); | ||
| 9250 | tcc_add_sysinclude_path(s, buf); | ||
| 9251 | } | ||
| 9252 | |||
| 8824 | /* if bound checking, then add corresponding sections */ | 9253 | /* if bound checking, then add corresponding sections */ |
| 8825 | #ifdef CONFIG_TCC_BCHECK | 9254 | #ifdef CONFIG_TCC_BCHECK |
| 8826 | if (do_bounds_check) { | 9255 | if (do_bounds_check) { |
| @@ -8873,7 +9302,7 @@ static int64_t getclock_us(void) · 46 unmodified lines | |||
| 8873 | 9302 | ||
| 8874 | void help(void) | 9303 | void help(void) |
| 8875 | { | 9304 | { |
| 8876 | printf("tcc version 0.9.15 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" | 9305 | printf("tcc version 0.9.16 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n" |
| 8877 | "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" | 9306 | "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n" |
| 8878 | " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n" | 9307 | " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n" |
| 8879 | " [--] infile1 [infile2... --] [infile_args...]\n" | 9308 | " [--] infile1 [infile2... --] [infile_args...]\n" |
| @@ -8904,16 +9333,86 @@ void help(void) · 24 unmodified lines | |||
| 8904 | ); | 9333 | ); |
| 8905 | } | 9334 | } |
| 8906 | 9335 | ||
| 9336 | #define TCC_OPTION_HAS_ARG 0x0001 | ||
| 9337 | #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */ | ||
| 9338 | |||
| 9339 | typedef struct TCCOption { | ||
| 9340 | const char *name; | ||
| 9341 | uint16_t index; | ||
| 9342 | uint16_t flags; | ||
| 9343 | } TCCOption; | ||
| 9344 | |||
| 9345 | enum { | ||
| 9346 | TCC_OPTION_HELP, | ||
| 9347 | TCC_OPTION_MARKER, | ||
| 9348 | TCC_OPTION_I, | ||
| 9349 | TCC_OPTION_D, | ||
| 9350 | TCC_OPTION_U, | ||
| 9351 | TCC_OPTION_L, | ||
| 9352 | TCC_OPTION_B, | ||
| 9353 | TCC_OPTION_l, | ||
| 9354 | TCC_OPTION_bench, | ||
| 9355 | TCC_OPTION_bt, | ||
| 9356 | TCC_OPTION_b, | ||
| 9357 | TCC_OPTION_g, | ||
| 9358 | TCC_OPTION_c, | ||
| 9359 | TCC_OPTION_static, | ||
| 9360 | TCC_OPTION_shared, | ||
| 9361 | TCC_OPTION_o, | ||
| 9362 | TCC_OPTION_r, | ||
| 9363 | TCC_OPTION_W, | ||
| 9364 | TCC_OPTION_O, | ||
| 9365 | TCC_OPTION_m, | ||
| 9366 | TCC_OPTION_f, | ||
| 9367 | TCC_OPTION_nostdinc, | ||
| 9368 | TCC_OPTION_print_search_dirs, | ||
| 9369 | TCC_OPTION_rdynamic, | ||
| 9370 | }; | ||
| 9371 | |||
| 9372 | static const TCCOption tcc_options[] = { | ||
| 9373 | { "h", TCC_OPTION_HELP, 0 }, | ||
| 9374 | { "?", TCC_OPTION_HELP, 0 }, | ||
| 9375 | { "-", TCC_OPTION_MARKER, 0 }, | ||
| 9376 | { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG }, | ||
| 9377 | { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG }, | ||
| 9378 | { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG }, | ||
| 9379 | { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG }, | ||
| 9380 | { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG }, | ||
| 9381 | { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, | ||
| 9382 | { "bench", TCC_OPTION_bench, 0 }, | ||
| 9383 | { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG }, | ||
| 9384 | #ifdef CONFIG_TCC_BCHECK | ||
| 9385 | { "b", TCC_OPTION_b, 0 }, | ||
| 9386 | #endif | ||
| 9387 | { "g", TCC_OPTION_g, 0 }, | ||
| 9388 | { "c", TCC_OPTION_c, 0 }, | ||
| 9389 | { "static", TCC_OPTION_static, 0 }, | ||
| 9390 | { "shared", TCC_OPTION_shared, 0 }, | ||
| 9391 | { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG }, | ||
| 9392 | { "rdynamic", TCC_OPTION_rdynamic, 0 }, /* currently ignored */ | ||
| 9393 | { "r", TCC_OPTION_r, 0 }, | ||
| 9394 | { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, | ||
| 9395 | { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, | ||
| 9396 | { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG }, | ||
| 9397 | { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP }, | ||
| 9398 | { "nostdinc", TCC_OPTION_nostdinc, 0 }, | ||
| 9399 | { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 }, | ||
| 9400 | { NULL }, | ||
| 9401 | }; | ||
| 9402 | |||
| 8907 | int main(int argc, char **argv) | 9403 | int main(int argc, char **argv) |
| 8908 | { | 9404 | { |
| 8909 | char *r, *outfile; | 9405 | char *r; |
| 8910 | int optind, output_type, multiple_files, i, reloc_output; | 9406 | int optind, output_type, multiple_files, i, reloc_output; |
| 8911 | TCCState *s; | 9407 | TCCState *s; |
| 8912 | char **files; | 9408 | char **files; |
| 8913 | int nb_files, nb_libraries, nb_objfiles, dminus, ret; | 9409 | int nb_files, nb_libraries, nb_objfiles, dminus, ret; |
| 8914 | char objfilename[1024]; | 9410 | char objfilename[1024]; |
| 8915 | int64_t start_time = 0; | 9411 | int64_t start_time = 0; |
| 8916 | 9412 | const TCCOption *popt; | |
| 9413 | const char *optarg, *p1, *r1, *outfile; | ||
| 9414 | int print_search_dirs; | ||
| 9415 | |||
| 8917 | s = tcc_new(); | 9416 | s = tcc_new(); |
| 8918 | output_type = TCC_OUTPUT_MEMORY; | 9417 | output_type = TCC_OUTPUT_MEMORY; |
| 8919 | 9418 | ||
| @@ -8925,9 +9424,10 @@ int main(int argc, char **argv) · 5 unmodified lines | |||
| 8925 | nb_files = 0; | 9424 | nb_files = 0; |
| 8926 | nb_libraries = 0; | 9425 | nb_libraries = 0; |
| 8927 | reloc_output = 0; | 9426 | reloc_output = 0; |
| 9427 | print_search_dirs = 0; | ||
| 8928 | while (1) { | 9428 | while (1) { |
| 8929 | if (optind >= argc) { | 9429 | if (optind >= argc) { |
| 8930 | if (nb_files == 0) | 9430 | if (nb_files == 0 && !print_search_dirs) |
| 8931 | goto show_help; | 9431 | goto show_help; |
| 8932 | else | 9432 | else |
| 8933 | break; | 9433 | break; |
| @@ -8941,75 +9441,134 @@ int main(int argc, char **argv) · 7 unmodified lines | |||
| 8941 | /* argv[0] will be this file */ | 9441 | /* argv[0] will be this file */ |
| 8942 | break; | 9442 | break; |
| 8943 | } | 9443 | } |
| 8944 | } else if (r[1] == '-') { | 9444 | } else { |
| 8945 | /* '--' enables multiple files input and also ends several file input */ | 9445 | /* find option in table (match only the first chars */ |
| 8946 | if (dminus && multiple_files) { | 9446 | popt = tcc_options; |
| 8947 | optind--; /* argv[0] will be '--' */ | 9447 | for(;;) { |
| 8948 | break; | 9448 | p1 = popt->name; |
| 9449 | if (p1 == NULL) | ||
| 9450 | error("invalid option -- '%s'", r); | ||
| 9451 | r1 = r + 1; | ||
| 9452 | for(;;) { | ||
| 9453 | if (*p1 == '\0') | ||
| 9454 | goto option_found; | ||
| 9455 | if (*r1 != *p1) | ||
| 9456 | break; | ||
| 9457 | p1++; | ||
| 9458 | r1++; | ||
| 9459 | } | ||
| 9460 | popt++; | ||
| 8949 | } | 9461 | } |
| 8950 | dminus = 1; | 9462 | option_found: |
| 8951 | multiple_files = 1; | 9463 | if (popt->flags & TCC_OPTION_HAS_ARG) { |
| 8952 | } else if (r[1] == 'h' || r[1] == '?') { | 9464 | if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) { |
| 8953 | show_help: | 9465 | optarg = r1; |
| 8954 | help(); | 9466 | } else { |
| 8955 | return 1; | 9467 | if (optind >= argc) |
| 8956 | } else if (r[1] == 'I') { | 9468 | error("argument to '%s' is missing", r); |
| 8957 | if (tcc_add_include_path(s, r + 2) < 0) | 9469 | optarg = argv[optind++]; |
| 8958 | error("too many include paths"); | 9470 | } |
| 8959 | } else if (r[1] == 'D') { | 9471 | } else { |
| 8960 | char *sym, *value; | 9472 | if (*r1 != '\0') |
| 8961 | sym = r + 2; | 9473 | goto show_help; |
| 8962 | value = strchr(sym, '='); | 9474 | optarg = NULL; |
| 8963 | if (value) { | ||
| 8964 | *value = '\0'; | ||
| 8965 | value++; | ||
| 8966 | } | 9475 | } |
| 8967 | tcc_define_symbol(s, sym, value); | 9476 | |
| 8968 | } else if (r[1] == 'U') { | 9477 | switch(popt->index) { |
| 8969 | tcc_undefine_symbol(s, r + 2); | 9478 | case TCC_OPTION_HELP: |
| 8970 | } else if (r[1] == 'L') { | 9479 | show_help: |
| 8971 | tcc_add_library_path(s, r + 2); | 9480 | help(); |
| 8972 | } else if (r[1] == 'B') { | 9481 | return 1; |
| 8973 | /* set tcc utilities path (mainly for tcc development) */ | 9482 | case TCC_OPTION_MARKER: |
| 8974 | tcc_lib_path = r + 2; | 9483 | /* '--' enables multiple files input and also ends several |
| 8975 | } else if (r[1] == 'l') { | 9484 | file input */ |
| 8976 | dynarray_add((void ***)&files, &nb_files, r); | 9485 | if (dminus && multiple_files) { |
| 8977 | nb_libraries++; | 9486 | optind--; /* argv[0] will be '--' */ |
| 8978 | } else if (!strcmp(r + 1, "bench")) { | 9487 | goto end_parse; |
| 8979 | do_bench = 1; | 9488 | } |
| 8980 | } else if (!strcmp(r + 1, "bt")) { | 9489 | dminus = 1; |
| 8981 | num_callers = atoi(argv[optind++]); | 9490 | multiple_files = 1; |
| 8982 | } else | 9491 | case TCC_OPTION_I: |
| 9492 | if (tcc_add_include_path(s, optarg) < 0) | ||
| 9493 | error("too many include paths"); | ||
| 9494 | break; | ||
| 9495 | case TCC_OPTION_D: | ||
| 9496 | { | ||
| 9497 | char *sym, *value; | ||
| 9498 | sym = (char *)optarg; | ||
| 9499 | value = strchr(sym, '='); | ||
| 9500 | if (value) { | ||
| 9501 | *value = '\0'; | ||
| 9502 | value++; | ||
| 9503 | } | ||
| 9504 | tcc_define_symbol(s, sym, value); | ||
| 9505 | } | ||
| 9506 | break; | ||
| 9507 | case TCC_OPTION_U: | ||
| 9508 | tcc_undefine_symbol(s, optarg); | ||
| 9509 | break; | ||
| 9510 | case TCC_OPTION_L: | ||
| 9511 | tcc_add_library_path(s, optarg); | ||
| 9512 | break; | ||
| 9513 | case TCC_OPTION_B: | ||
| 9514 | /* set tcc utilities path (mainly for tcc development) */ | ||
| 9515 | tcc_lib_path = optarg; | ||
| 9516 | break; | ||
| 9517 | case TCC_OPTION_l: | ||
| 9518 | dynarray_add((void ***)&files, &nb_files, r); | ||
| 9519 | nb_libraries++; | ||
| 9520 | break; | ||
| 9521 | case TCC_OPTION_bench: | ||
| 9522 | do_bench = 1; | ||
| 9523 | break; | ||
| 9524 | case TCC_OPTION_bt: | ||
| 9525 | num_callers = atoi(optarg); | ||
| 9526 | break; | ||
| 8983 | #ifdef CONFIG_TCC_BCHECK | 9527 | #ifdef CONFIG_TCC_BCHECK |
| 8984 | if (r[1] == 'b') { | 9528 | case TCC_OPTION_b: |
| 8985 | do_bounds_check = 1; | 9529 | do_bounds_check = 1; |
| 8986 | do_debug = 1; | 9530 | do_debug = 1; |
| 8987 | } else | 9531 | break; |
| 8988 | #endif | 9532 | #endif |
| 8989 | if (r[1] == 'g') { | 9533 | case TCC_OPTION_g: |
| 8990 | do_debug = 1; | 9534 | do_debug = 1; |
| 8991 | } else if (r[1] == 'c') { | 9535 | break; |
| 8992 | multiple_files = 1; | 9536 | case TCC_OPTION_c: |
| 8993 | output_type = TCC_OUTPUT_OBJ; | 9537 | multiple_files = 1; |
| 8994 | } else if (!strcmp(r + 1, "static")) { | 9538 | output_type = TCC_OUTPUT_OBJ; |
| 8995 | s->static_link = 1; | 9539 | break; |
| 8996 | } else if (!strcmp(r + 1, "shared")) { | 9540 | case TCC_OPTION_static: |
| 8997 | output_type = TCC_OUTPUT_DLL; | 9541 | s->static_link = 1; |
| 8998 | } else if (r[1] == 'o') { | 9542 | break; |
| 8999 | if (optind >= argc) | 9543 | case TCC_OPTION_shared: |
| 9000 | goto show_help; | 9544 | output_type = TCC_OUTPUT_DLL; |
| 9001 | multiple_files = 1; | 9545 | break; |
| 9002 | outfile = argv[optind++]; | 9546 | case TCC_OPTION_o: |
| 9003 | } else if (r[1] == 'r') { | 9547 | multiple_files = 1; |
| 9004 | /* generate a .o merging several output files */ | 9548 | outfile = optarg; |
| 9005 | reloc_output = 1; | 9549 | break; |
| 9006 | output_type = TCC_OUTPUT_OBJ; | 9550 | case TCC_OPTION_r: |
| 9007 | } else if (r[1] == 'W' || r[1] == 'O' || r[1] == 'm' || r[1] == 'f') { | 9551 | /* generate a .o merging several output files */ |
| 9008 | /* ignore those options to be a drop-in replacement for gcc */ | 9552 | reloc_output = 1; |
| 9009 | } else { | 9553 | output_type = TCC_OUTPUT_OBJ; |
| 9010 | error("invalid option -- '%s'", r); | 9554 | break; |
| 9555 | case TCC_OPTION_nostdinc: | ||
| 9556 | s->nostdinc = 1; | ||
| 9557 | break; | ||
| 9558 | case TCC_OPTION_print_search_dirs: | ||
| 9559 | print_search_dirs = 1; | ||
| 9560 | break; | ||
| 9561 | default: | ||
| 9562 | break; | ||
| 9563 | } | ||
| 9011 | } | 9564 | } |
| 9012 | } | 9565 | } |
| 9566 | end_parse: | ||
| 9567 | if (print_search_dirs) { | ||
| 9568 | /* enough for Linux kernel */ | ||
| 9569 | printf("install: %s/\n", tcc_lib_path); | ||
| 9570 | return 0; | ||
| 9571 | } | ||
| 9013 | 9572 | ||
| 9014 | nb_objfiles = nb_files - nb_libraries; | 9573 | nb_objfiles = nb_files - nb_libraries; |
| 9015 | 9574 | ||