History

macro opt - asm support - new option parsing - supressed IL code

bellard committed 23 years ago

Commit 8e40dd91 parent 75e743d1 file · +982 −423Browse files

Unified · Split

tcc.c+982−423
@@ -53,19 +53,24 @@ · 52 unmodified lines
5353
5454 //#define MEM_DEBUG
5555
56+/* assembler debug */
57+//#define ASM_DEBUG
58+
5659 /* target selection */
5760 //#define TCC_TARGET_I386 /* i386 code generator */
58−//#define TCC_TARGET_IL /* .NET CLI generator */
5961
6062 /* default target is I386 */
61−#if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_IL)
63+#if !defined(TCC_TARGET_I386)
6264 #define TCC_TARGET_I386
6365 #endif
6466
65−#if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL)
67+#if !defined(WIN32) && !defined(TCC_UCLIBC)
6668 #define CONFIG_TCC_BCHECK /* enable bound checking code */
6769 #endif
6870
71+/* define it to include assembler support */
72+#define CONFIG_TCC_ASM
73+
6974 #ifndef CONFIG_TCC_PREFIX
7075 #define CONFIG_TCC_PREFIX "/usr/local"
7176 #endif
@@ -203,8 +208,10 @@ typedef struct AttributeDef { · 131 unmodified lines
203208 #define MACRO_OBJ 0 /* object like macro */
204209 #define MACRO_FUNC 1 /* function like macro */
205210
206−/* field 'Sym.r' for labels */
211+/* field 'Sym.r' for C labels */
212+#define LABEL_DEFINED 0 /* label is defined */
207213 #define LABEL_FORWARD 1 /* label is forward defined */
214+#define LABEL_DECLARED 2 /* label is declared but never used */
208215
209216 /* type_decl() types */
210217 #define TYPE_ABSTRACT 1 /* type without variable */
@@ -256,8 +263,8 @@ typedef struct CachedInclude { · 45 unmodified lines
256263
257264 /* parser */
258265 static struct BufferedFile *file;
259−static int ch, tok, tok1;
260−static CValue tokc, tok1c;
266+static int ch, tok;
267+static CValue tokc;
261268 static CString tokcstr; /* current parsed string, if any */
262269 /* additionnal informations about token */
263270 static int tok_flags;
@@ -265,9 +272,17 @@ static int tok_flags; · 1 unmodified line
265272 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
266273 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
267274
268−/* if true, line feed is returned as a token. line feed is also
269− returned at eof */
270−static int return_linefeed;
275+static int *macro_ptr, *macro_ptr_allocated;
276+static int *unget_saved_macro_ptr;
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+
271286 static Section *text_section, *data_section, *bss_section; /* predefined sections */
272287 static Section *cur_text_section; /* current section where function code is
273288 generated */
@@ -302,12 +317,13 @@ static char token_buf[STRING_MAX_SIZE + 1]; · 28 unmodified lines
302317 static char *funcname;
303318 static Sym *global_stack, *local_stack;
304319 static Sym *define_stack;
305−static Sym *label_stack;
320+static Sym *global_label_stack, *local_label_stack;
306321
307322 static SValue vstack[VSTACK_SIZE], *vtop;
308−static int *macro_ptr, *macro_ptr_allocated;
309323 /* some predefined types */
310324 static CType char_pointer_type, func_old_type, int_type;
325+/* true if isid(c) || isnum(c) */
326+static unsigned char isidnum_table[256];
311327
312328 /* compile with debug symbol (and use them if error during execution) */
313329 static int do_debug = 0;
@@ -364,9 +380,9 @@ struct TCCState { · 50 unmodified lines
364380
365381 /* got handling */
366382 Section *got;
383+ Section *plt;
367384 unsigned long *got_offsets;
368385 int nb_got_offsets;
369− int nb_plt_entries;
370386 /* give the correspondance from symtab indexes to dynsym indexes */
371387 int *symtab_to_dynsym;
372388
@@ -375,6 +391,9 @@ struct TCCState { · 2 unmodified lines
375391 /* exported dynamic symbol section */
376392 Section *dynsym;
377393
394+ /* if true, no standard headers are added */
395+ int nostdinc;
396+
378397 /* if true, static linking is performed */
379398 int static_link;
380399
@@ -385,6 +404,9 @@ struct TCCState { · 4 unmodified lines
385404 jmp_buf error_jmp_buf;
386405 int nb_errors;
387406
407+ /* tiny assembler state */
408+ Sym *asm_labels;
409+
388410 /* see include_stack_ptr */
389411 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
390412
@@ -441,9 +463,11 @@ struct TCCState { · 50 unmodified lines
441463 #define VT_EXTERN 0x00000080 /* extern definition */
442464 #define VT_STATIC 0x00000100 /* static variable */
443465 #define VT_TYPEDEF 0x00000200 /* typedef definition */
466+#define VT_INLINE 0x00000400 /* inline definition */
444467
445468 /* 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))
447471
448472 /* token values */
449473
@@ -515,6 +539,65 @@ static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224< · 65 unmodified lines
515539 /* all identificators and strings have token above that */
516540 #define TOK_IDENT 256
517541
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+
518601 enum {
519602 TOK_LAST = TOK_IDENT - 1,
520603 #define DEF(id, str) id,
@@ -522,7 +605,7 @@ enum { · 1 unmodified line
522605 #undef DEF
523606 };
524607
525−static const char *tcc_keywords =
608+static const char tcc_keywords[] =
526609 #define DEF(id, str) str "\0"
527610 #include "tcctok.h"
528611 #undef DEF
@@ -559,6 +642,8 @@ static void next_nomacro(void); · 30 unmodified lines
559642 static void parse_expr_type(CType *type);
560643 static void expr_type(CType *type);
561644 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);
562647 static int expr_const(void);
563648 static void expr_eq(void);
564649 static void gexpr(void);
@@ -578,7 +663,7 @@ void vdup(void); · 13 unmodified lines
578663 int get_reg(int rc);
579664
580665 static void macro_subst(TokenString *tok_str,
581− Sym **nested_list, int *macro_str);
666+ Sym **nested_list, const int *macro_str);
582667 int save_reg_forced(int r);
583668 void gen_op(int op);
584669 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
632717 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
633718 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
634719
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+
635752 /* true if float/double/long double type */
636753 static inline int is_float(int t)
637754 {
@@ -643,9 +760,6 @@ static inline int is_float(int t) · 5 unmodified lines
643760 #ifdef TCC_TARGET_I386
644761 #include "i386-gen.c"
645762 #endif
646−#ifdef TCC_TARGET_IL
647−#include "il-gen.c"
648−#endif
649763
650764 #ifdef CONFIG_TCC_STATIC
651765
@@ -1113,7 +1227,7 @@ void skip(int c) · 461 unmodified lines
11131227 next();
11141228 }
11151229
1116−void test_lvalue(void)
1230+static void test_lvalue(void)
11171231 {
11181232 if (!(vtop->r & VT_LVAL))
11191233 expect("lvalue");
@@ -2157,15 +2271,49 @@ static Sym *label_find(int v) · 1037 unmodified lines
21572271 return table_ident[v]->sym_label;
21582272 }
21592273
2160−static Sym *label_push(int v, int flags)
2274+static Sym *label_push(Sym **ptop, int v, int flags)
21612275 {
2162− Sym *s;
2163− s = sym_push2(&label_stack, v, 0, 0);
2276+ Sym *s, **ps;
2277+ s = sym_push2(ptop, v, 0, 0);
21642278 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;
21662288 return s;
21672289 }
21682290
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+
21692317 /* eval an expression for #if/#elif */
21702318 static int expr_preprocess(void)
21712319 {
@@ -2315,15 +2463,16 @@ static inline void add_cached_include(TCCState *s1, int type, · 143 unmodified lines
23152463 static void preprocess(int is_bof)
23162464 {
23172465 TCCState *s1 = tcc_state;
2318− int size, i, c, n;
2466+ int size, i, c, n, saved_parse_flags;
23192467 char buf[1024], *q, *p;
23202468 char buf1[1024];
23212469 BufferedFile *f;
23222470 Sym *s;
23232471 CachedInclude *e;
23242472
2325− return_linefeed = 1; /* linefeed will be returned as a
2326− token. EOF is also returned as line feed */
2473+ saved_parse_flags = parse_flags;
2474+ parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2475+ PARSE_FLAG_LINEFEED;
23272476 next_nomacro();
23282477 redo:
23292478 switch(tok) {
@@ -2572,7 +2721,7 @@ static void preprocess(int is_bof) · 242 unmodified lines
25722721 while (tok != TOK_LINEFEED)
25732722 next_nomacro();
25742723 the_end:
2575− return_linefeed = 0;
2724+ parse_flags = saved_parse_flags;
25762725 }
25772726
25782727 /* read a number in base b */
@@ -3020,11 +3169,11 @@ static inline void next_nomacro1(void) · 441 unmodified lines
30203169 parse_eof:
30213170 {
30223171 TCCState *s1 = tcc_state;
3023−
3024− if (return_linefeed) {
3172+ if (parse_flags & PARSE_FLAG_LINEFEED) {
30253173 tok = TOK_LINEFEED;
3026− } else if (s1->include_stack_ptr == s1->include_stack) {
3027− /* no include left : end of file */
3174+ } else if (s1->include_stack_ptr == s1->include_stack ||
3175+ !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3176+ /* no include left : end of file. */
30283177 tok = TOK_EOF;
30293178 } else {
30303179 /* pop include file */
@@ -3054,7 +3203,7 @@ static inline void next_nomacro1(void) · 23 unmodified lines
30543203 break;
30553204
30563205 case '\n':
3057− if (return_linefeed) {
3206+ if (parse_flags & PARSE_FLAG_LINEFEED) {
30583207 tok = TOK_LINEFEED;
30593208 } else {
30603209 file->line_num++;
@@ -3067,7 +3216,8 @@ static inline void next_nomacro1(void) · 6 unmodified lines
30673216 case '#':
30683217 /* XXX: simplify */
30693218 PEEKC(c, p);
3070− if (tok_flags & TOK_FLAG_BOL) {
3219+ if ((tok_flags & TOK_FLAG_BOL) &&
3220+ (parse_flags & PARSE_FLAG_PREPROCESS)) {
30713221 file->buf_ptr = p;
30723222 preprocess(tok_flags & TOK_FLAG_BOF);
30733223 p = file->buf_ptr;
@@ -3104,7 +3254,7 @@ static inline void next_nomacro1(void) · 30 unmodified lines
31043254 p++;
31053255 for(;;) {
31063256 c = *p;
3107− if (!isid(c) && !isnum(c))
3257+ if (!isidnum_table[c])
31083258 break;
31093259 h = TOK_HASH_FUNC(h, c);
31103260 p++;
@@ -3139,7 +3289,7 @@ static inline void next_nomacro1(void) · 28 unmodified lines
31393289 p--;
31403290 PEEKC(c, p);
31413291 parse_ident_slow:
3142− while (isid(c) || isnum(c)) {
3292+ while (isidnum_table[c]) {
31433293 cstr_ccat(&tokcstr, c);
31443294 PEEKC(c, p);
31453295 }
@@ -3375,6 +3525,7 @@ static inline void next_nomacro1(void) · 229 unmodified lines
33753525 case ':':
33763526 case '?':
33773527 case '~':
3528+ case '$': /* only used in assembler */
33783529 tok = c;
33793530 p++;
33803531 break;
@@ -3498,115 +3649,6 @@ static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args) · 117 unmodified lines
34983649 return str.str;
34993650 }
35003651
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(&macro_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(&macro_str1, tok, &tokc);
3595− /* XXX: free associated memory ? */
3596− tok = t;
3597− tokc = cval;
3598− }
3599− }
3600− }
3601− }
3602− tok_str_add2(&macro_str1, tok, &tokc);
3603− }
3604− cstr_free(&cstr);
3605− tok_str_add(&macro_str1, 0);
3606− return macro_str1.str;
3607−}
3608−
3609−
36103652 /* do macro substitution of current token with macro 's' and add
36113653 result to (tok_str,tok_len). 'nested_list' is the list of all
36123654 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
36223664 CString cstr;
36233665
36243666 /* if symbol is a macro, prepare substitution */
3625− /* if nested substitution, do nothing */
3626− if (sym_find2(*nested_list, tok))
3627− return -1;
36283667
36293668 /* special macros */
36303669 if (tok == TOK___LINE__) {
@@ -3735,37 +3774,180 @@ static int macro_subst_tok(TokenString *tok_str, · 104 unmodified lines
37353774 return 0;
37363775 }
37373776
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(&macro_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(&macro_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(&macro_str1, tok, &tokc);
3891+ /* XXX: free associated memory ? */
3892+ tok = t;
3893+ tokc = cval;
3894+ }
3895+ }
3896+ }
3897+ }
3898+ tok_str_add2(&macro_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(&macro_str1, 0);
3906+ return macro_str1.str;
3907+}
3908+
3909+
37383910 /* do macro substitution of macro_str and add result to
37393911 (tok_str,tok_len). 'nested_list' is the list of all macros we got
37403912 inside to avoid recursing. */
37413913 static void macro_subst(TokenString *tok_str,
3742− Sym **nested_list, int *macro_str)
3914+ Sym **nested_list, const int *macro_str)
37433915 {
37443916 Sym *s;
3745− int *saved_macro_ptr;
3746− int *macro_str1;
3917+ int *saved_macro_ptr, *macro_str1;
3918+ const int *ptr;
3919+ int t, ret;
3920+ CValue cval;
37473921
3748− saved_macro_ptr = macro_ptr;
3749− macro_ptr = macro_str;
37503922 /* first scan for '##' operator handling */
3751− macro_str1 = macro_twosharps();
3752− macro_ptr = macro_str1;
3753−
3923+ ptr = macro_str;
3924+ macro_str1 = macro_twosharps(ptr);
3925+ if (macro_str1)
3926+ ptr = macro_str1;
37543927 while (1) {
3755− next_nomacro();
3756− if (tok == 0)
3928+ TOK_GET(t, ptr, cval);
3929+ if (t == 0)
37573930 break;
3758− s = define_find(tok);
3931+ s = define_find(t);
37593932 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)
37613943 goto no_subst;
37623944 } else {
37633945 no_subst:
3764− tok_str_add2(tok_str, tok, &tokc);
3946+ tok_str_add2(tok_str, t, &cval);
37653947 }
37663948 }
3767− macro_ptr = saved_macro_ptr;
3768− tok_str_free(macro_str1);
3949+ if (macro_str1)
3950+ tok_str_free(macro_str1);
37693951 }
37703952
37713953 /* return next token with macro substitution */
@@ -3774,48 +3956,68 @@ static void next(void) · 2 unmodified lines
37743956 Sym *nested_list, *s;
37753957 TokenString str;
37763958
3777− /* special 'ungettok' case for label parsing */
3778− if (tok1) {
3779− tok = tok1;
3780− tokc = tok1c;
3781− tok1 = 0;
3782− } else {
3783− redo:
3784− next_nomacro();
3785− if (!macro_ptr) {
3786− /* if not reading from macro substituted string, then try
3787− to substitute macros */
3788− if (tok >= TOK_IDENT) {
3789− s = define_find(tok);
3790− if (s) {
3791− /* we have a macro: we try to substitute */
3792− tok_str_new(&str);
3793− nested_list = NULL;
3794− if (macro_subst_tok(&str, &nested_list, s) == 0) {
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− }
3959+ redo:
3960+ next_nomacro();
3961+ if (!macro_ptr) {
3962+ /* if not reading from macro substituted string, then try
3963+ to substitute macros */
3964+ if (tok >= TOK_IDENT &&
3965+ (parse_flags & PARSE_FLAG_PREPROCESS)) {
3966+ s = define_find(tok);
3967+ if (s) {
3968+ /* we have a macro: we try to substitute */
3969+ tok_str_new(&str);
3970+ nested_list = NULL;
3971+ if (macro_subst_tok(&str, &nested_list, s) == 0) {
3972+ /* substitution done, NOTE: maybe empty */
3973+ tok_str_add(&str, 0);
3974+ macro_ptr = str.str;
3975+ macro_ptr_allocated = str.str;
3976+ goto redo;
38013977 }
38023978 }
3803− } else {
3804− if (tok == 0) {
3979+ }
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 {
38053987 /* end of macro string: free it */
38063988 tok_str_free(macro_ptr_allocated);
38073989 macro_ptr = NULL;
3808− goto redo;
38093990 }
3991+ goto redo;
38103992 }
3811−
3812− /* convert preprocessor tokens into C tokens */
3813− if (tok == TOK_PPNUM) {
3814− parse_number((char *)tokc.cstr->data);
3815− }
3993+ }
3994+
3995+ /* convert preprocessor tokens into C tokens */
3996+ if (tok == TOK_PPNUM &&
3997+ (parse_flags & PARSE_FLAG_TOK_NUM)) {
3998+ parse_number((char *)tokc.cstr->data);
38163999 }
38174000 }
38184001
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+
38194021 void swap(int *p, int *q)
38204022 {
38214023 int t;
@@ -3903,6 +4105,10 @@ static Sym *external_sym(int v, CType *type, int r) · 81 unmodified lines
39034105 /* push forward reference */
39044106 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
39054107 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));
39064112 }
39074113 return s;
39084114 }
@@ -5545,10 +5751,11 @@ void inc(int post, int c) · 1636 unmodified lines
55455751 - section(x) : generate data/code in this section.
55465752 - unused : currently ignored, but may be used someday.
55475753 */
5548−void parse_attribute(AttributeDef *ad)
5754+static void parse_attribute(AttributeDef *ad)
55495755 {
55505756 int t, n;
5551−
5757+
5758+ while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
55525759 next();
55535760 skip('(');
55545761 skip('(');
@@ -5558,8 +5765,8 @@ void parse_attribute(AttributeDef *ad) · 3 unmodified lines
55585765 t = tok;
55595766 next();
55605767 switch(t) {
5561− case TOK_SECTION:
5562− case TOK___SECTION__:
5768+ case TOK_SECTION1:
5769+ case TOK_SECTION2:
55635770 skip('(');
55645771 if (tok != TOK_STR)
55655772 expect("section name");
@@ -5567,8 +5774,8 @@ void parse_attribute(AttributeDef *ad) · 1 unmodified line
55675774 next();
55685775 skip(')');
55695776 break;
5570− case TOK_ALIGNED:
5571− case TOK___ALIGNED__:
5777+ case TOK_ALIGNED1:
5778+ case TOK_ALIGNED2:
55725779 if (tok == '(') {
55735780 next();
55745781 n = expr_const();
@@ -5580,28 +5787,28 @@ void parse_attribute(AttributeDef *ad) · 5 unmodified lines
55805787 }
55815788 ad->aligned = n;
55825789 break;
5583− case TOK_UNUSED:
5584− case TOK___UNUSED__:
5790+ case TOK_UNUSED1:
5791+ case TOK_UNUSED2:
55855792 /* currently, no need to handle it because tcc does not
55865793 track unused objects */
55875794 break;
5588− case TOK_NORETURN:
5589− case TOK___NORETURN__:
5795+ case TOK_NORETURN1:
5796+ case TOK_NORETURN2:
55905797 /* currently, no need to handle it because tcc does not
55915798 track unused objects */
55925799 break;
5593− case TOK_CDECL:
5594− case TOK___CDECL:
5595− case TOK___CDECL__:
5800+ case TOK_CDECL1:
5801+ case TOK_CDECL2:
5802+ case TOK_CDECL3:
55965803 ad->func_call = FUNC_CDECL;
55975804 break;
5598− case TOK_STDCALL:
5599− case TOK___STDCALL:
5600− case TOK___STDCALL__:
5805+ case TOK_STDCALL1:
5806+ case TOK_STDCALL2:
5807+ case TOK_STDCALL3:
56015808 ad->func_call = FUNC_STDCALL;
56025809 break;
56035810 default:
5604− warning("'%s' attribute ignored", get_tok_str(t, NULL));
5811+ // warning("'%s' attribute ignored", get_tok_str(t, NULL));
56055812 /* skip parameters */
56065813 /* XXX: skip parenthesis too */
56075814 if (tok == '(') {
@@ -5618,6 +5825,7 @@ void parse_attribute(AttributeDef *ad) · 10 unmodified lines
56185825 }
56195826 skip(')');
56205827 skip(')');
5828+ }
56215829 }
56225830
56235831 /* 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
56955903 if (tok != ':') {
56965904 type_decl(&type1, &ad, &v, TYPE_DIRECT);
56975905 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)))
56995907 error("invalid type for '%s'",
57005908 get_tok_str(v, NULL));
57015909 }
@@ -5804,6 +6012,11 @@ static int parse_btype(CType *type, AttributeDef *ad) · 102 unmodified lines
58046012 t = 0;
58056013 while(1) {
58066014 switch(tok) {
6015+ case TOK_EXTENSION:
6016+ /* currently, we really ignore extension */
6017+ next();
6018+ continue;
6019+
58076020 /* basic types */
58086021 case TOK_CHAR:
58096022 u = VT_BYTE;
@@ -5861,15 +6074,20 @@ static int parse_btype(CType *type, AttributeDef *ad) · 51 unmodified lines
58616074 goto basic_type2;
58626075
58636076 /* type modifiers */
5864− case TOK_CONST:
5865− case TOK_VOLATILE:
6077+ case TOK_CONST1:
6078+ case TOK_CONST2:
6079+ case TOK_CONST3:
6080+ case TOK_VOLATILE1:
6081+ case TOK_VOLATILE2:
6082+ case TOK_VOLATILE3:
58666083 case TOK_REGISTER:
5867− case TOK_SIGNED:
5868− case TOK___SIGNED__:
6084+ case TOK_SIGNED1:
6085+ case TOK_SIGNED2:
6086+ case TOK_SIGNED3:
58696087 case TOK_AUTO:
5870− case TOK_INLINE:
5871− case TOK___INLINE__:
5872− case TOK_RESTRICT:
6088+ case TOK_RESTRICT1:
6089+ case TOK_RESTRICT2:
6090+ case TOK_RESTRICT3:
58736091 next();
58746092 break;
58756093 case TOK_UNSIGNED:
@@ -5890,12 +6108,22 @@ static int parse_btype(CType *type, AttributeDef *ad) · 14 unmodified lines
58906108 t |= VT_TYPEDEF;
58916109 next();
58926110 break;
6111+ case TOK_INLINE1:
6112+ case TOK_INLINE2:
6113+ case TOK_INLINE3:
6114+ t |= VT_INLINE;
6115+ next();
6116+ break;
6117+
58936118 /* GNUC attribute */
5894− case TOK___ATTRIBUTE__:
6119+ case TOK_ATTRIBUTE1:
6120+ case TOK_ATTRIBUTE2:
58956121 parse_attribute(ad);
58966122 break;
58976123 /* GNUC typeof */
5898− case TOK_TYPEOF:
6124+ case TOK_TYPEOF1:
6125+ case TOK_TYPEOF2:
6126+ case TOK_TYPEOF3:
58996127 next();
59006128 parse_expr_type(&type1);
59016129 goto basic_type2;
@@ -5972,8 +6200,8 @@ static void post_type(CType *type, AttributeDef *ad) · 70 unmodified lines
59726200 if (l == 0)
59736201 l = FUNC_OLD;
59746202 skip(')');
5975− t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5976− type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN);
6203+ t1 = type->t & VT_STORAGE;
6204+ type->t &= ~VT_STORAGE;
59776205 post_type(type, ad);
59786206 /* we push a anonymous symbol which will contain the function prototype */
59796207 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
59916219 }
59926220 skip(']');
59936221 /* parse next post type */
5994− t1 = type->t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN);
5995− type->t &= ~(VT_TYPEDEF | VT_STATIC | VT_EXTERN);
6222+ t1 = type->t & VT_STORAGE;
6223+ type->t &= ~VT_STORAGE;
59966224 post_type(type, ad);
59976225
59986226 /* 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
60166244
60176245 while (tok == '*') {
60186246 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:
60206258 next();
6259+ goto redo;
6260+ }
60216261 mk_pointer(type);
60226262 }
60236263
6264+ /* XXX: clarify attribute handling */
6265+ if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6266+ parse_attribute(ad);
6267+
60246268 /* recursive type */
60256269 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
60266270 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
60286272 next();
60296273 /* XXX: this is not correct to modify 'ad' at this point, but
60306274 the syntax is not clear */
6031− if (tok == TOK___ATTRIBUTE__)
6275+ if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
60326276 parse_attribute(ad);
60336277 type_decl(&type1, ad, v, td);
60346278 skip(')');
@@ -6044,7 +6288,7 @@ static void type_decl(CType *type, AttributeDef *ad, int *v, int td) · 9 unmodified lines
60446288 }
60456289 }
60466290 post_type(type, ad);
6047− if (tok == TOK___ATTRIBUTE__)
6291+ if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
60486292 parse_attribute(ad);
60496293 if (!type1.t)
60506294 return;
@@ -6154,7 +6398,11 @@ static void unary(void) · 103 unmodified lines
61546398
61556399 /* XXX: GCC 2.95.3 does not generate a table although it should be
61566400 better here */
6401+ tok_next:
61576402 switch(tok) {
6403+ case TOK_EXTENSION:
6404+ next();
6405+ goto tok_next;
61586406 case TOK_CINT:
61596407 case TOK_CCHAR:
61606408 case TOK_LCHAR:
@@ -6241,6 +6489,13 @@ static void unary(void) · 80 unmodified lines
62416489 unary();
62426490 gen_cast(&type);
62436491 }
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(')');
62446499 } else {
62456500 gexpr();
62466501 skip(')');
@@ -6291,7 +6546,8 @@ static void unary(void) · 44 unmodified lines
62916546 gen_op('+');
62926547 break;
62936548 case TOK_SIZEOF:
6294− case TOK_ALIGNOF:
6549+ case TOK_ALIGNOF1:
6550+ case TOK_ALIGNOF2:
62956551 t = tok;
62966552 next();
62976553 if (tok == '(') {
@@ -6328,7 +6584,10 @@ static void unary(void) · 30 unmodified lines
63286584 expect("label identifier");
63296585 s = label_find(tok);
63306586 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;
63326591 }
63336592 if (!s->type.t) {
63346593 s->type.t = VT_VOID;
@@ -6734,9 +6993,13 @@ static void expr_eq(void) · 399 unmodified lines
67346993 c = vtop->c.i;
67356994 vpop();
67366995 next();
6737− gexpr();
6738− c1 = vtop->c.i;
6739− vpop();
6996+ if (tok == ':' && gnu_ext) {
6997+ c1 = c;
6998+ } else {
6999+ gexpr();
7000+ c1 = vtop->c.i;
7001+ vpop();
7002+ }
67407003 skip(':');
67417004 expr_eq();
67427005 if (c)
@@ -6756,8 +7019,13 @@ static void expr_eq(void) · 13 unmodified lines
67567019 gv(rc);
67577020 save_regs(1);
67587021 }
6759− tt = gtst(1, 0);
6760− gexpr();
7022+ if (tok == ':' && gnu_ext) {
7023+ gv_dup();
7024+ tt = gtst(1, 0);
7025+ } else {
7026+ tt = gtst(1, 0);
7027+ gexpr();
7028+ }
67617029 type1 = vtop->type;
67627030 sv = *vtop; /* save value to handle it later */
67637031 vtop--; /* no vpop so that FP stack is not flushed */
@@ -6896,30 +7164,25 @@ static int expr_const(void) · 132 unmodified lines
68967164 return zero */
68977165 static int is_label(void)
68987166 {
6899− int t;
6900− CValue c;
7167+ int last_tok;
69017168
69027169 /* fast test first */
69037170 if (tok < TOK_UIDENT)
69047171 return 0;
6905− /* no need to save tokc since we expect an identifier */
6906− t = tok;
6907− c = tokc;
7172+ /* no need to save tokc because tok is an identifier */
7173+ last_tok = tok;
69087174 next();
69097175 if (tok == ':') {
69107176 next();
6911− return t;
7177+ return last_tok;
69127178 } else {
6913− /* XXX: may not work in all cases (macros ?) */
6914− tok1 = tok;
6915− tok1c = tokc;
6916− tok = t;
6917− tokc = c;
7179+ unget_tok(last_tok);
69187180 return 0;
69197181 }
69207182 }
69217183
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)
69237186 {
69247187 int a, b, c, d;
69257188 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
69327195 last_line_num = file->line_num;
69337196 }
69347197
7198+ if (is_expr) {
7199+ /* default return value is (void) */
7200+ vpushi(0);
7201+ vtop->type.t = VT_VOID;
7202+ }
7203+
69357204 if (tok == TOK_IF) {
69367205 /* if test */
69377206 next();
@@ -6939,13 +7208,13 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 1 unmodified line
69397208 gexpr();
69407209 skip(')');
69417210 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);
69437212 c = tok;
69447213 if (c == TOK_ELSE) {
69457214 next();
69467215 d = gjmp(0);
69477216 gsym(a);
6948− block(bsym, csym, case_sym, def_sym, case_reg);
7217+ block(bsym, csym, case_sym, def_sym, case_reg, 0);
69497218 gsym(d); /* patch else jmp */
69507219 } else
69517220 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
69577226 skip(')');
69587227 a = gtst(1, 0);
69597228 b = 0;
6960− block(&a, &b, case_sym, def_sym, case_reg);
7229+ block(&a, &b, case_sym, def_sym, case_reg, 0);
69617230 gjmp_addr(d);
69627231 gsym(a);
69637232 gsym_addr(b, d);
69647233 } else if (tok == '{') {
7234+ Sym *llabel;
7235+
69657236 next();
6966− /* declarations */
7237+ /* record local declaration stack position */
69677238 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+ }
69687256 while (tok != '}') {
69697257 decl(VT_LOCAL);
6970− if (tok != '}')
6971− block(bsym, csym, case_sym, def_sym, case_reg);
7258+ if (tok != '}') {
7259+ if (is_expr)
7260+ vpop();
7261+ block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7262+ }
69727263 }
7264+ /* pop locally defined labels */
7265+ label_pop(&local_label_stack, llabel);
69737266 /* pop locally defined symbols */
69747267 sym_pop(&local_stack, s);
69757268 next();
@@ -7039,7 +7332,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 63 unmodified lines
70397332 gsym(e);
70407333 }
70417334 skip(')');
7042− block(&a, &b, case_sym, def_sym, case_reg);
7335+ block(&a, &b, case_sym, def_sym, case_reg, 0);
70437336 gjmp_addr(c);
70447337 gsym(a);
70457338 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
70497342 a = 0;
70507343 b = 0;
70517344 d = ind;
7052− block(&a, &b, case_sym, def_sym, case_reg);
7345+ block(&a, &b, case_sym, def_sym, case_reg, 0);
70537346 skip(TOK_WHILE);
70547347 skip('(');
70557348 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
70717364 a = 0;
70727365 b = gjmp(0); /* jump to first case */
70737366 c = 0;
7074− block(&a, csym, &b, &c, case_reg);
7367+ block(&a, csym, &b, &c, case_reg, 0);
70757368 /* if no default, jmp after switch */
70767369 if (c == 0)
70777370 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
71117404 }
71127405 gsym(b);
71137406 skip(':');
7114− block(bsym, csym, case_sym, def_sym, case_reg);
7407+ block(bsym, csym, case_sym, def_sym, case_reg, 0);
71157408 } else
71167409 if (tok == TOK_DEFAULT) {
71177410 next();
@@ -7121,7 +7414,7 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 3 unmodified lines
71217414 if (*def_sym)
71227415 error("too many 'default'");
71237416 *def_sym = ind;
7124− block(bsym, csym, case_sym, def_sym, case_reg);
7417+ block(bsym, csym, case_sym, def_sym, case_reg, 0);
71257418 } else
71267419 if (tok == TOK_GOTO) {
71277420 next();
@@ -7136,7 +7429,10 @@ static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_re · 8 unmodified lines
71367429 s = label_find(tok);
71377430 /* put forward definition if needed */
71387431 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;
71407436 }
71417437 /* label already defined */
71427438 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
71487444 expect("label identifier");
71497445 }
71507446 skip(';');
7447+ } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7448+ asm_instr();
71517449 } else {
71527450 b = is_label();
71537451 if (b) {
71547452 /* label case */
71557453 s = label_find(b);
71567454 if (s) {
7157− if (!(s->r & LABEL_FORWARD))
7158− error("multiple defined label");
7455+ if (s->r == LABEL_DEFINED)
7456+ error("duplicate label '%s'", get_tok_str(s->v, NULL));
71597457 gsym((long)s->next);
7458+ s->r = LABEL_DEFINED;
71607459 } else {
7161− s = label_push(b, 0);
7460+ s = label_push(&global_label_stack, b, LABEL_DEFINED);
71627461 }
71637462 s->next = (void *)ind;
7164− s->r = 0;
71657463 /* we accept this, but it is a mistake */
7166− if (tok == '}')
7464+ if (tok == '}') {
71677465 warning("deprecated use of label at end of compound statement");
7168− else
7169− block(bsym, csym, case_sym, def_sym, case_reg);
7466+ } else {
7467+ if (is_expr)
7468+ vpop();
7469+ block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7470+ }
71707471 } else {
71717472 /* expression case */
71727473 if (tok != ';') {
7173− gexpr();
7174− vpop();
7474+ if (is_expr) {
7475+ vpop();
7476+ gexpr();
7477+ } else {
7478+ gexpr();
7479+ vpop();
7480+ }
71757481 }
71767482 skip(';');
71777483 }
@@ -7187,13 +7493,14 @@ static void decl_designator(CType *type, Section *sec, unsigned long c, · 9 unmodified lines
71877493 int size_only)
71887494 {
71897495 Sym *s, *f;
7190− int notfirst, index, align, l;
7496+ int notfirst, index, index_last, align, l, nb_elems, elem_size;
71917497 CType type1;
71927498
71937499 notfirst = 0;
7500+ elem_size = 0;
7501+ nb_elems = 1;
71947502 if (gnu_ext && (l = is_label()) != 0)
71957503 goto struct_field;
7196−
71977504 while (tok == '[' || tok == '.') {
71987505 if (tok == '[') {
71997506 if (!(type->t & VT_ARRAY))
@@ -7203,11 +7510,28 @@ static void decl_designator(CType *type, Section *sec, unsigned long c, · 3 unmodified lines
72037510 index = expr_const();
72047511 if (index < 0 || (s->c >= 0 && index >= s->c))
72057512 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+ }
72067523 skip(']');
72077524 if (!notfirst)
7208− *cur_index = index;
7525+ *cur_index = index_last;
72097526 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+ }
72117535 } else {
72127536 next();
72137537 l = tok;
@@ -7259,6 +7583,25 @@ static void decl_designator(CType *type, Section *sec, unsigned long c, · 45 unmodified lines
72597583 }
72607584 }
72617585 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+ }
72627605 }
72637606
72647607 #define EXPR_VAL 0
@@ -7482,9 +7825,32 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 217 unmodified lines
74827825 s->c = array_length;
74837826 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
74847827 (sec || !first || tok == '{')) {
7828+ int par_count;
7829+
74857830 /* NOTE: the previous test is a specific case for automatic
74867831 struct/union init */
74877832 /* 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+ }
74887854 no_oblock = 1;
74897855 if (first || tok == '{') {
74907856 skip('{');
@@ -7519,6 +7885,10 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 28 unmodified lines
75197885 }
75207886 if (!no_oblock)
75217887 skip('}');
7888+ while (par_count) {
7889+ skip(')');
7890+ par_count--;
7891+ }
75227892 } else if (tok == '{') {
75237893 next();
75247894 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
76197989 sec = NULL;
76207990 if (do_bounds_check && (type->t & VT_ARRAY))
76217991 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
76287992 loc = (loc - size) & -align;
76297993 addr = loc;
7630−#endif
76317994 /* handles bounds */
76327995 /* XXX: currently, since we do only one pass, we cannot track
76337996 '&' operators, so we add only arrays */
@@ -7640,31 +8003,6 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 6 unmodified lines
76408003 bounds_ptr[0] = addr;
76418004 bounds_ptr[1] = size;
76428005 }
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) {
76688006 if (v) {
76698007 /* local variable */
76708008 sym_push(v, type, r, addr);
@@ -7674,24 +8012,76 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 3 unmodified lines
76748012 }
76758013 } else {
76768014 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+
76788066 if (v) {
76798067 if (scope == VT_CONST) {
7680− /* global scope: see if already defined */
7681− sym = sym_find(v);
76828068 if (!sym)
76838069 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;
76908070 } else {
76918071 do_def:
76928072 sym = sym_push(v, type, r | VT_SYM, 0);
76938073 }
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+ }
76958085 } else {
76968086 CValue cval;
76978087
@@ -7722,6 +8112,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 24 unmodified lines
77228112 restore_parse_state(&saved_parse_state);
77238113 }
77248114 }
8115+ no_alloc: ;
77258116 }
77268117
77278118 void put_func_debug(Sym *sym)
@@ -7829,8 +8220,10 @@ static void func_decl_list(Sym *func_sym) · 101 unmodified lines
78298220 get_tok_str(v, NULL));
78308221 found:
78318222 /* 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)
78338224 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;
78348227 /* we can add the type (NOTE: it could be local to the function) */
78358228 s->type = type;
78368229 /* accept other parameters */
@@ -7902,6 +8295,9 @@ static void decl(int l) · 65 unmodified lines
79028295 error("cannot use local functions");
79038296 if (!(type.t & VT_FUNC))
79048297 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;
79058301
79068302 #ifdef CONFIG_REG_VARS
79078303 /* parse all function code and record it */
@@ -7965,31 +8361,11 @@ static void decl(int l) · 57 unmodified lines
79658361 macro_ptr = func_str.str;
79668362 next();
79678363 #endif
7968− block(NULL, NULL, NULL, NULL, 0);
8364+ block(NULL, NULL, NULL, NULL, 0, 0);
79698365 gsym(rsym);
79708366 gfunc_epilog();
79718367 cur_text_section->data_offset = ind;
7972− /* look if any labels are undefined. Define symbols if
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− }
8368+ label_pop(&global_label_stack, NULL);
79938369 sym_pop(&local_stack, NULL); /* reset local stack */
79948370 /* end of function */
79958371 /* patch symbol size */
@@ -8021,15 +8397,20 @@ static void decl(int l) · 25 unmodified lines
80218397 r = 0;
80228398 if (!(type.t & VT_ARRAY))
80238399 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)) {
80258404 /* external variable */
8405+ /* NOTE: as GCC, uninitialized global static
8406+ arrays of null size are considered as
8407+ extern */
80268408 external_sym(v, &type, r);
80278409 } else {
80288410 if (type.t & VT_STATIC)
80298411 r |= VT_CONST;
80308412 else
80318413 r |= l;
8032− has_init = (tok == '=');
80338414 if (has_init)
80348415 next();
80358416 decl_initializer_alloc(&type, &ad, r,
@@ -8046,6 +8427,20 @@ static void decl(int l) · 10 unmodified lines
80468427 }
80478428 }
80488429
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+
80498444 /* compile the C file opened in 'file'. Return non zero if errors. */
80508445 static int tcc_compile(TCCState *s1)
80518446 {
@@ -8056,15 +8451,9 @@ static int tcc_compile(TCCState *s1) · 4 unmodified lines
80568451 #ifdef INC_DEBUG
80578452 printf("%s: **** new file\n", file->filename);
80588453 #endif
8059− funcname = "";
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;
8454+ preprocess_init(s1);
80658455
8066− /* XXX: not ANSI compliant: bound checking says error */
8067− vtop = vstack - 1;
8456+ funcname = "";
80688457 anon_sym = SYM_FIRST_ANOM;
80698458
80708459 /* file info: full path + filename */
@@ -8117,6 +8506,7 @@ static int tcc_compile(TCCState *s1) · 46 unmodified lines
81178506
81188507 ch = file->buf_ptr[0];
81198508 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
8509+ parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
81208510 next();
81218511 decl(VT_CONST);
81228512 if (tok != -1)
@@ -8133,9 +8523,9 @@ static int tcc_compile(TCCState *s1) · 10 unmodified lines
81338523 /* reset define stack, but leave -Dsymbols (may be incorrect if
81348524 they are undefined) */
81358525 free_defines(define_start);
8136−
8526+
81378527 sym_pop(&global_stack, NULL);
8138−
8528+
81398529 return s1->nb_errors != 0 ? -1 : 0;
81408530 }
81418531
@@ -8212,6 +8602,18 @@ void tcc_undefine_symbol(TCCState *s1, const char *sym) · 70 unmodified lines
82128602 define_undef(s);
82138603 }
82148604
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+
82158617 #include "tccelf.c"
82168618
82178619 /* print the position in the source file of PC value 'pc' by reading
@@ -8526,6 +8928,8 @@ TCCState *tcc_new(void) · 308 unmodified lines
85268928 {
85278929 const char *p, *r;
85288930 TCCState *s;
8931+ TokenSym *ts;
8932+ int i, c;
85298933
85308934 s = tcc_mallocz(sizeof(TCCState));
85318935 if (!s)
@@ -8533,10 +8937,9 @@ TCCState *tcc_new(void) · 1 unmodified line
85338937 tcc_state = s;
85348938 s->output_type = TCC_OUTPUT_MEMORY;
85358939
8536− /* default include paths */
8537− tcc_add_sysinclude_path(s, "/usr/local/include");
8538− tcc_add_sysinclude_path(s, "/usr/include");
8539− tcc_add_sysinclude_path(s, CONFIG_TCC_PREFIX "/lib/tcc/include");
8940+ /* init isid table */
8941+ for(i=0;i<256;i++)
8942+ isidnum_table[i] = isid(i) || isnum(i);
85408943
85418944 /* add all tokens */
85428945 table_ident = NULL;
@@ -8546,8 +8949,12 @@ TCCState *tcc_new(void) · 3 unmodified lines
85468949 p = tcc_keywords;
85478950 while (*p) {
85488951 r = p;
8549− while (*r++);
8550− tok_alloc(p, r - p - 1);
8952+ for(;;) {
8953+ c = *r++;
8954+ if (c == '\0')
8955+ break;
8956+ }
8957+ ts = tok_alloc(p, r - p - 1);
85518958 p = r;
85528959 }
85538960
@@ -8564,6 +8971,7 @@ TCCState *tcc_new(void) · 10 unmodified lines
85648971 tcc_define_symbol(s, "__i386__", NULL);
85658972 #endif
85668973 #if defined(linux)
8974+ tcc_define_symbol(s, "__linux__", NULL);
85678975 tcc_define_symbol(s, "linux", NULL);
85688976 #endif
85698977 /* tiny C specific defines */
@@ -8700,7 +9108,17 @@ static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 130 unmodified lines
87009108 if (!ext || !strcmp(ext, "c")) {
87019109 /* C file assumed */
87029110 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+ {
87049122 fd = file->fd;
87059123 /* assume executable format: auto guess file type */
87069124 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
88199237
88209238 int tcc_set_output_type(TCCState *s, int output_type)
88219239 {
9240+ char buf[1024];
9241+
88229242 s->output_type = output_type;
88239243
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+
88249253 /* if bound checking, then add corresponding sections */
88259254 #ifdef CONFIG_TCC_BCHECK
88269255 if (do_bounds_check) {
@@ -8873,7 +9302,7 @@ static int64_t getclock_us(void) · 46 unmodified lines
88739302
88749303 void help(void)
88759304 {
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"
88779306 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
88789307 " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
88799308 " [--] infile1 [infile2... --] [infile_args...]\n"
@@ -8904,16 +9333,86 @@ void help(void) · 24 unmodified lines
89049333 );
89059334 }
89069335
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+
89079403 int main(int argc, char **argv)
89089404 {
8909− char *r, *outfile;
9405+ char *r;
89109406 int optind, output_type, multiple_files, i, reloc_output;
89119407 TCCState *s;
89129408 char **files;
89139409 int nb_files, nb_libraries, nb_objfiles, dminus, ret;
89149410 char objfilename[1024];
89159411 int64_t start_time = 0;
8916−
9412+ const TCCOption *popt;
9413+ const char *optarg, *p1, *r1, *outfile;
9414+ int print_search_dirs;
9415+
89179416 s = tcc_new();
89189417 output_type = TCC_OUTPUT_MEMORY;
89199418
@@ -8925,9 +9424,10 @@ int main(int argc, char **argv) · 5 unmodified lines
89259424 nb_files = 0;
89269425 nb_libraries = 0;
89279426 reloc_output = 0;
9427+ print_search_dirs = 0;
89289428 while (1) {
89299429 if (optind >= argc) {
8930− if (nb_files == 0)
9430+ if (nb_files == 0 && !print_search_dirs)
89319431 goto show_help;
89329432 else
89339433 break;
@@ -8941,75 +9441,134 @@ int main(int argc, char **argv) · 7 unmodified lines
89419441 /* argv[0] will be this file */
89429442 break;
89439443 }
8944− } else if (r[1] == '-') {
8945− /* '--' enables multiple files input and also ends several file input */
8946− if (dminus && multiple_files) {
8947− optind--; /* argv[0] will be '--' */
8948− break;
9444+ } else {
9445+ /* find option in table (match only the first chars */
9446+ popt = tcc_options;
9447+ for(;;) {
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++;
89499461 }
8950− dminus = 1;
8951− multiple_files = 1;
8952− } else if (r[1] == 'h' || r[1] == '?') {
8953− show_help:
8954− help();
8955− return 1;
8956− } else if (r[1] == 'I') {
8957− if (tcc_add_include_path(s, r + 2) < 0)
8958− error("too many include paths");
8959− } else if (r[1] == 'D') {
8960− char *sym, *value;
8961− sym = r + 2;
8962− value = strchr(sym, '=');
8963− if (value) {
8964− *value = '\0';
8965− value++;
9462+ option_found:
9463+ if (popt->flags & TCC_OPTION_HAS_ARG) {
9464+ if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
9465+ optarg = r1;
9466+ } else {
9467+ if (optind >= argc)
9468+ error("argument to '%s' is missing", r);
9469+ optarg = argv[optind++];
9470+ }
9471+ } else {
9472+ if (*r1 != '\0')
9473+ goto show_help;
9474+ optarg = NULL;
89669475 }
8967− tcc_define_symbol(s, sym, value);
8968− } else if (r[1] == 'U') {
8969− tcc_undefine_symbol(s, r + 2);
8970− } else if (r[1] == 'L') {
8971− tcc_add_library_path(s, r + 2);
8972− } else if (r[1] == 'B') {
8973− /* set tcc utilities path (mainly for tcc development) */
8974− tcc_lib_path = r + 2;
8975− } else if (r[1] == 'l') {
8976− dynarray_add((void ***)&files, &nb_files, r);
8977− nb_libraries++;
8978− } else if (!strcmp(r + 1, "bench")) {
8979− do_bench = 1;
8980− } else if (!strcmp(r + 1, "bt")) {
8981− num_callers = atoi(argv[optind++]);
8982− } else
9476+
9477+ switch(popt->index) {
9478+ case TCC_OPTION_HELP:
9479+ show_help:
9480+ help();
9481+ return 1;
9482+ case TCC_OPTION_MARKER:
9483+ /* '--' enables multiple files input and also ends several
9484+ file input */
9485+ if (dminus && multiple_files) {
9486+ optind--; /* argv[0] will be '--' */
9487+ goto end_parse;
9488+ }
9489+ dminus = 1;
9490+ multiple_files = 1;
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;
89839527 #ifdef CONFIG_TCC_BCHECK
8984− if (r[1] == 'b') {
8985− do_bounds_check = 1;
8986− do_debug = 1;
8987− } else
9528+ case TCC_OPTION_b:
9529+ do_bounds_check = 1;
9530+ do_debug = 1;
9531+ break;
89889532 #endif
8989− if (r[1] == 'g') {
8990− do_debug = 1;
8991− } else if (r[1] == 'c') {
8992− multiple_files = 1;
8993− output_type = TCC_OUTPUT_OBJ;
8994− } else if (!strcmp(r + 1, "static")) {
8995− s->static_link = 1;
8996− } else if (!strcmp(r + 1, "shared")) {
8997− output_type = TCC_OUTPUT_DLL;
8998− } else if (r[1] == 'o') {
8999− if (optind >= argc)
9000− goto show_help;
9001− multiple_files = 1;
9002− outfile = argv[optind++];
9003− } else if (r[1] == 'r') {
9004− /* generate a .o merging several output files */
9005− reloc_output = 1;
9006− output_type = TCC_OUTPUT_OBJ;
9007− } else if (r[1] == 'W' || r[1] == 'O' || r[1] == 'm' || r[1] == 'f') {
9008− /* ignore those options to be a drop-in replacement for gcc */
9009− } else {
9010− error("invalid option -- '%s'", r);
9533+ case TCC_OPTION_g:
9534+ do_debug = 1;
9535+ break;
9536+ case TCC_OPTION_c:
9537+ multiple_files = 1;
9538+ output_type = TCC_OUTPUT_OBJ;
9539+ break;
9540+ case TCC_OPTION_static:
9541+ s->static_link = 1;
9542+ break;
9543+ case TCC_OPTION_shared:
9544+ output_type = TCC_OUTPUT_DLL;
9545+ break;
9546+ case TCC_OPTION_o:
9547+ multiple_files = 1;
9548+ outfile = optarg;
9549+ break;
9550+ case TCC_OPTION_r:
9551+ /* generate a .o merging several output files */
9552+ reloc_output = 1;
9553+ output_type = TCC_OUTPUT_OBJ;
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+ }
90119564 }
90129565 }
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+ }
90139572
90149573 nb_objfiles = nb_files - nb_libraries;
90159574

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t