compile fix
[tinycc.git] / tcc.c
blob41efaba03654bf8955e455e2b28d0d08d3784b83
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001, 2002 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #define _GNU_SOURCE
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <setjmp.h>
34 #include <time.h>
35 #ifdef WIN32
36 #include <sys/timeb.h>
37 #endif
38 #ifndef WIN32
39 #include <sys/time.h>
40 #include <sys/ucontext.h>
41 #endif
42 #include "elf.h"
43 #include "stab.h"
44 #ifndef CONFIG_TCC_STATIC
45 #include <dlfcn.h>
46 #endif
48 #include "libtcc.h"
50 /* parser debug */
51 //#define PARSE_DEBUG
52 /* preprocessor debug */
53 //#define PP_DEBUG
54 /* include file debug */
55 //#define INC_DEBUG
57 //#define MEM_DEBUG
59 /* assembler debug */
60 //#define ASM_DEBUG
62 /* target selection */
63 //#define TCC_TARGET_I386 /* i386 code generator */
65 /* default target is I386 */
66 #if !defined(TCC_TARGET_I386)
67 #define TCC_TARGET_I386
68 #endif
70 #if !defined(WIN32) && !defined(TCC_UCLIBC)
71 #define CONFIG_TCC_BCHECK /* enable bound checking code */
72 #endif
74 /* define it to include assembler support */
75 #define CONFIG_TCC_ASM
77 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
78 executables or dlls */
79 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
81 #define INCLUDE_STACK_SIZE 32
82 #define IFDEF_STACK_SIZE 64
83 #define VSTACK_SIZE 64
84 #define STRING_MAX_SIZE 1024
86 #define TOK_HASH_SIZE 2048 /* must be a power of two */
87 #define TOK_ALLOC_INCR 512 /* must be a power of two */
88 #define TOK_STR_ALLOC_INCR_BITS 6
89 #define TOK_STR_ALLOC_INCR (1 << TOK_STR_ALLOC_INCR_BITS)
90 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
92 /* token symbol management */
93 typedef struct TokenSym {
94 struct TokenSym *hash_next;
95 struct Sym *sym_define; /* direct pointer to define */
96 struct Sym *sym_label; /* direct pointer to label */
97 struct Sym *sym_struct; /* direct pointer to structure */
98 struct Sym *sym_identifier; /* direct pointer to identifier */
99 int tok; /* token number */
100 int len;
101 char str[1];
102 } TokenSym;
104 typedef struct CString {
105 int size; /* size in bytes */
106 void *data; /* either 'char *' or 'int *' */
107 int size_allocated;
108 void *data_allocated; /* if non NULL, data has been malloced */
109 } CString;
111 /* type definition */
112 typedef struct CType {
113 int t;
114 struct Sym *ref;
115 } CType;
117 /* constant value */
118 typedef union CValue {
119 long double ld;
120 double d;
121 float f;
122 int i;
123 unsigned int ui;
124 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
125 long long ll;
126 unsigned long long ull;
127 struct CString *cstr;
128 void *ptr;
129 int tab[1];
130 } CValue;
132 /* value on stack */
133 typedef struct SValue {
134 CType type; /* type */
135 unsigned short r; /* register + flags */
136 unsigned short r2; /* second register, used for 'long long'
137 type. If not used, set to VT_CONST */
138 CValue c; /* constant, if VT_CONST */
139 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
140 } SValue;
142 /* symbol management */
143 typedef struct Sym {
144 int v; /* symbol token */
145 int r; /* associated register */
146 int c; /* associated number */
147 CType type; /* associated type */
148 struct Sym *next; /* next related symbol */
149 struct Sym *prev; /* prev symbol in stack */
150 struct Sym *prev_tok; /* previous symbol for this token */
151 } Sym;
153 /* section definition */
154 /* XXX: use directly ELF structure for parameters ? */
155 /* special flag to indicate that the section should not be linked to
156 the other ones */
157 #define SHF_PRIVATE 0x80000000
159 typedef struct Section {
160 unsigned long data_offset; /* current data offset */
161 unsigned char *data; /* section data */
162 unsigned long data_allocated; /* used for realloc() handling */
163 int sh_name; /* elf section name (only used during output) */
164 int sh_num; /* elf section number */
165 int sh_type; /* elf section type */
166 int sh_flags; /* elf section flags */
167 int sh_info; /* elf section info */
168 int sh_addralign; /* elf section alignment */
169 int sh_entsize; /* elf entry size */
170 unsigned long sh_size; /* section size (only used during output) */
171 unsigned long sh_addr; /* address at which the section is relocated */
172 unsigned long sh_offset; /* address at which the section is relocated */
173 int nb_hashed_syms; /* used to resize the hash table */
174 struct Section *link; /* link to another section */
175 struct Section *reloc; /* corresponding section for relocation, if any */
176 struct Section *hash; /* hash table for symbols */
177 struct Section *next;
178 char name[64]; /* section name */
179 } Section;
181 typedef struct DLLReference {
182 int level;
183 char name[1];
184 } DLLReference;
186 /* GNUC attribute definition */
187 typedef struct AttributeDef {
188 int aligned;
189 Section *section;
190 unsigned char func_call; /* FUNC_CDECL or FUNC_STDCALL */
191 } AttributeDef;
193 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
194 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
195 #define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
197 /* stored in 'Sym.c' field */
198 #define FUNC_NEW 1 /* ansi function prototype */
199 #define FUNC_OLD 2 /* old function prototype */
200 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
202 /* stored in 'Sym.r' field */
203 #define FUNC_CDECL 0 /* standard c call */
204 #define FUNC_STDCALL 1 /* pascal c call */
206 /* field 'Sym.t' for macros */
207 #define MACRO_OBJ 0 /* object like macro */
208 #define MACRO_FUNC 1 /* function like macro */
210 /* field 'Sym.r' for C labels */
211 #define LABEL_DEFINED 0 /* label is defined */
212 #define LABEL_FORWARD 1 /* label is forward defined */
213 #define LABEL_DECLARED 2 /* label is declared but never used */
215 /* type_decl() types */
216 #define TYPE_ABSTRACT 1 /* type without variable */
217 #define TYPE_DIRECT 2 /* type with variable */
219 #define IO_BUF_SIZE 8192
221 typedef struct BufferedFile {
222 uint8_t *buf_ptr;
223 uint8_t *buf_end;
224 int fd;
225 int line_num; /* current line number - here to simplify code */
226 int ifndef_macro; /* #ifndef macro / #endif search */
227 int ifndef_macro_saved; /* saved ifndef_macro */
228 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
229 char inc_type; /* type of include */
230 char inc_filename[512]; /* filename specified by the user */
231 char filename[1024]; /* current filename - here to simplify code */
232 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
233 } BufferedFile;
235 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
236 #define CH_EOF (-1) /* end of file */
238 /* parsing state (used to save parser state to reparse part of the
239 source several times) */
240 typedef struct ParseState {
241 int *macro_ptr;
242 int line_num;
243 int tok;
244 CValue tokc;
245 } ParseState;
247 /* used to record tokens */
248 typedef struct TokenString {
249 int *str;
250 int len;
251 int allocated_len;
252 int last_line_num;
253 } TokenString;
255 /* include file cache, used to find files faster and also to eliminate
256 inclusion if the include file is protected by #ifndef ... #endif */
257 typedef struct CachedInclude {
258 int ifndef_macro;
259 char type; /* '"' or '>' to give include type */
260 char filename[1]; /* path specified in #include */
261 } CachedInclude;
263 /* parser */
264 static struct BufferedFile *file;
265 static int ch, tok;
266 static CValue tokc;
267 static CString tokcstr; /* current parsed string, if any */
268 /* additional informations about token */
269 static int tok_flags;
270 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
271 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
272 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
274 static int *macro_ptr, *macro_ptr_allocated;
275 static int *unget_saved_macro_ptr;
276 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
277 static int unget_buffer_enabled;
278 static int parse_flags;
279 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
280 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
281 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
282 token. line feed is also
283 returned at eof */
285 static Section *text_section, *data_section, *bss_section; /* predefined sections */
286 static Section *cur_text_section; /* current section where function code is
287 generated */
288 /* bound check related sections */
289 static Section *bounds_section; /* contains global data bound description */
290 static Section *lbounds_section; /* contains local data bound description */
291 /* symbol sections */
292 static Section *symtab_section, *strtab_section;
294 /* debug sections */
295 static Section *stab_section, *stabstr_section;
297 /* loc : local variable index
298 ind : output code index
299 rsym: return symbol
300 anon_sym: anonymous symbol index
302 static int rsym, anon_sym, ind, loc;
303 /* expression generation modifiers */
304 static int const_wanted; /* true if constant wanted */
305 static int nocode_wanted; /* true if no code generation wanted for an expression */
306 static int global_expr; /* true if compound literals must be allocated
307 globally (used during initializers parsing */
308 static CType func_vt; /* current function return type (used by return
309 instruction) */
310 static int func_vc;
311 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
312 static int tok_ident;
313 static TokenSym **table_ident;
314 static TokenSym *hash_ident[TOK_HASH_SIZE];
315 static char token_buf[STRING_MAX_SIZE + 1];
316 static char *funcname;
317 static Sym *global_stack, *local_stack;
318 static Sym *define_stack;
319 static Sym *global_label_stack, *local_label_stack;
321 static SValue vstack[VSTACK_SIZE], *vtop;
322 /* some predefined types */
323 static CType char_pointer_type, func_old_type, int_type;
324 /* true if isid(c) || isnum(c) */
325 static unsigned char isidnum_table[256];
327 /* compile with debug symbol (and use them if error during execution) */
328 static int do_debug = 0;
330 /* compile with built-in memory and bounds checker */
331 static int do_bounds_check = 0;
333 /* display benchmark infos */
334 #if !defined(LIBTCC)
335 static int do_bench = 0;
336 #endif
337 static int total_lines;
338 static int total_bytes;
340 /* use GNU C extensions */
341 static int gnu_ext = 1;
343 /* use Tiny C extensions */
344 static int tcc_ext = 1;
346 /* max number of callers shown if error */
347 static int num_callers = 6;
348 static const char **rt_bound_error_msg;
350 /* XXX: get rid of this ASAP */
351 static struct TCCState *tcc_state;
353 /* give the path of the tcc libraries */
354 static const char *tcc_lib_path = CONFIG_TCC_LIBDIR "/tcc";
356 struct TCCState {
357 int output_type;
359 BufferedFile **include_stack_ptr;
360 int *ifdef_stack_ptr;
362 /* include file handling */
363 char **include_paths;
364 int nb_include_paths;
365 char **sysinclude_paths;
366 int nb_sysinclude_paths;
367 CachedInclude **cached_includes;
368 int nb_cached_includes;
370 char **library_paths;
371 int nb_library_paths;
373 /* array of all loaded dlls (including those referenced by loaded
374 dlls) */
375 DLLReference **loaded_dlls;
376 int nb_loaded_dlls;
378 /* sections */
379 Section **sections;
380 int nb_sections; /* number of sections, including first dummy section */
382 /* got handling */
383 Section *got;
384 Section *plt;
385 unsigned long *got_offsets;
386 int nb_got_offsets;
387 /* give the correspondance from symtab indexes to dynsym indexes */
388 int *symtab_to_dynsym;
390 /* temporary dynamic symbol sections (for dll loading) */
391 Section *dynsymtab_section;
392 /* exported dynamic symbol section */
393 Section *dynsym;
395 /* if true, no standard headers are added */
396 int nostdinc;
398 /* if true, static linking is performed */
399 int static_link;
401 /* error handling */
402 void *error_opaque;
403 void (*error_func)(void *opaque, const char *msg);
404 int error_set_jmp_enabled;
405 jmp_buf error_jmp_buf;
406 int nb_errors;
408 /* tiny assembler state */
409 Sym *asm_labels;
411 /* see include_stack_ptr */
412 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
414 /* see ifdef_stack_ptr */
415 int ifdef_stack[IFDEF_STACK_SIZE];
418 /* The current value can be: */
419 #define VT_VALMASK 0x00ff
420 #define VT_CONST 0x00f0 /* constant in vc
421 (must be first non register value) */
422 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
423 #define VT_LOCAL 0x00f2 /* offset on stack */
424 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
425 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
426 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
427 #define VT_LVAL 0x0100 /* var is an lvalue */
428 #define VT_SYM 0x0200 /* a symbol value is added */
429 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
430 char/short stored in integer registers) */
431 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
432 dereferencing value */
433 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
434 bounding function call point is in vc */
435 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
436 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
437 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
438 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
440 /* types */
441 #define VT_STRUCT_SHIFT 12 /* structure/enum name shift (20 bits left) */
443 #define VT_INT 0 /* integer type */
444 #define VT_BYTE 1 /* signed byte type */
445 #define VT_SHORT 2 /* short type */
446 #define VT_VOID 3 /* void type */
447 #define VT_PTR 4 /* pointer */
448 #define VT_ENUM 5 /* enum definition */
449 #define VT_FUNC 6 /* function type */
450 #define VT_STRUCT 7 /* struct/union definition */
451 #define VT_FLOAT 8 /* IEEE float */
452 #define VT_DOUBLE 9 /* IEEE double */
453 #define VT_LDOUBLE 10 /* IEEE long double */
454 #define VT_BOOL 11 /* ISOC99 boolean type */
455 #define VT_LLONG 12 /* 64 bit integer */
456 #define VT_LONG 13 /* long integer (NEVER USED as type, only
457 during parsing) */
458 #define VT_BTYPE 0x000f /* mask for basic type */
459 #define VT_UNSIGNED 0x0010 /* unsigned type */
460 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
461 #define VT_BITFIELD 0x0040 /* bitfield modifier */
463 /* storage */
464 #define VT_EXTERN 0x00000080 /* extern definition */
465 #define VT_STATIC 0x00000100 /* static variable */
466 #define VT_TYPEDEF 0x00000200 /* typedef definition */
467 #define VT_INLINE 0x00000400 /* inline definition */
469 /* type mask (except storage) */
470 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
471 #define VT_TYPE (~(VT_STORAGE))
473 /* token values */
475 /* warning: the following compare tokens depend on i386 asm code */
476 #define TOK_ULT 0x92
477 #define TOK_UGE 0x93
478 #define TOK_EQ 0x94
479 #define TOK_NE 0x95
480 #define TOK_ULE 0x96
481 #define TOK_UGT 0x97
482 #define TOK_LT 0x9c
483 #define TOK_GE 0x9d
484 #define TOK_LE 0x9e
485 #define TOK_GT 0x9f
487 #define TOK_LAND 0xa0
488 #define TOK_LOR 0xa1
490 #define TOK_DEC 0xa2
491 #define TOK_MID 0xa3 /* inc/dec, to void constant */
492 #define TOK_INC 0xa4
493 #define TOK_UDIV 0xb0 /* unsigned division */
494 #define TOK_UMOD 0xb1 /* unsigned modulo */
495 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
496 #define TOK_CINT 0xb3 /* number in tokc */
497 #define TOK_CCHAR 0xb4 /* char constant in tokc */
498 #define TOK_STR 0xb5 /* pointer to string in tokc */
499 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
500 #define TOK_LCHAR 0xb7
501 #define TOK_LSTR 0xb8
502 #define TOK_CFLOAT 0xb9 /* float constant */
503 #define TOK_LINENUM 0xba /* line number info */
504 #define TOK_CDOUBLE 0xc0 /* double constant */
505 #define TOK_CLDOUBLE 0xc1 /* long double constant */
506 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
507 #define TOK_ADDC1 0xc3 /* add with carry generation */
508 #define TOK_ADDC2 0xc4 /* add with carry use */
509 #define TOK_SUBC1 0xc5 /* add with carry generation */
510 #define TOK_SUBC2 0xc6 /* add with carry use */
511 #define TOK_CUINT 0xc8 /* unsigned int constant */
512 #define TOK_CLLONG 0xc9 /* long long constant */
513 #define TOK_CULLONG 0xca /* unsigned long long constant */
514 #define TOK_ARROW 0xcb
515 #define TOK_DOTS 0xcc /* three dots */
516 #define TOK_SHR 0xcd /* unsigned shift right */
517 #define TOK_PPNUM 0xce /* preprocessor number */
519 #define TOK_SHL 0x01 /* shift left */
520 #define TOK_SAR 0x02 /* signed shift right */
522 /* assignement operators : normal operator or 0x80 */
523 #define TOK_A_MOD 0xa5
524 #define TOK_A_AND 0xa6
525 #define TOK_A_MUL 0xaa
526 #define TOK_A_ADD 0xab
527 #define TOK_A_SUB 0xad
528 #define TOK_A_DIV 0xaf
529 #define TOK_A_XOR 0xde
530 #define TOK_A_OR 0xfc
531 #define TOK_A_SHL 0x81
532 #define TOK_A_SAR 0x82
534 /* WARNING: the content of this string encodes token numbers */
535 static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
537 #define TOK_EOF (-1) /* end of file */
538 #define TOK_LINEFEED 10 /* line feed */
540 /* all identificators and strings have token above that */
541 #define TOK_IDENT 256
543 /* only used for i386 asm opcodes definitions */
544 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
546 #define DEF_BWL(x) \
547 DEF(TOK_ASM_ ## x ## b, #x "b") \
548 DEF(TOK_ASM_ ## x ## w, #x "w") \
549 DEF(TOK_ASM_ ## x ## l, #x "l") \
550 DEF(TOK_ASM_ ## x, #x)
552 #define DEF_WL(x) \
553 DEF(TOK_ASM_ ## x ## w, #x "w") \
554 DEF(TOK_ASM_ ## x ## l, #x "l") \
555 DEF(TOK_ASM_ ## x, #x)
557 #define DEF_FP1(x) \
558 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
559 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
560 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
561 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
563 #define DEF_FP(x) \
564 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
565 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
566 DEF_FP1(x)
568 #define DEF_ASMTEST(x) \
569 DEF_ASM(x ## o) \
570 DEF_ASM(x ## no) \
571 DEF_ASM(x ## b) \
572 DEF_ASM(x ## c) \
573 DEF_ASM(x ## nae) \
574 DEF_ASM(x ## nb) \
575 DEF_ASM(x ## nc) \
576 DEF_ASM(x ## ae) \
577 DEF_ASM(x ## e) \
578 DEF_ASM(x ## z) \
579 DEF_ASM(x ## ne) \
580 DEF_ASM(x ## nz) \
581 DEF_ASM(x ## be) \
582 DEF_ASM(x ## na) \
583 DEF_ASM(x ## nbe) \
584 DEF_ASM(x ## a) \
585 DEF_ASM(x ## s) \
586 DEF_ASM(x ## ns) \
587 DEF_ASM(x ## p) \
588 DEF_ASM(x ## pe) \
589 DEF_ASM(x ## np) \
590 DEF_ASM(x ## po) \
591 DEF_ASM(x ## l) \
592 DEF_ASM(x ## nge) \
593 DEF_ASM(x ## nl) \
594 DEF_ASM(x ## ge) \
595 DEF_ASM(x ## le) \
596 DEF_ASM(x ## ng) \
597 DEF_ASM(x ## nle) \
598 DEF_ASM(x ## g)
600 #define TOK_ASM_int TOK_INT
602 enum {
603 TOK_LAST = TOK_IDENT - 1,
604 #define DEF(id, str) id,
605 #include "tcctok.h"
606 #undef DEF
609 static const char tcc_keywords[] =
610 #define DEF(id, str) str "\0"
611 #include "tcctok.h"
612 #undef DEF
615 #define TOK_UIDENT TOK_DEFINE
617 #ifdef WIN32
618 #define snprintf _snprintf
619 #define vsnprintf _vsnprintf
620 #endif
622 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
623 /* currently incorrect */
624 long double strtold(const char *nptr, char **endptr)
626 return (long double)strtod(nptr, endptr);
628 float strtof(const char *nptr, char **endptr)
630 return (float)strtod(nptr, endptr);
632 #else
633 /* XXX: need to define this to use them in non ISOC99 context */
634 extern float strtof (const char *__nptr, char **__endptr);
635 extern long double strtold (const char *__nptr, char **__endptr);
636 #endif
638 static char *pstrcpy(char *buf, int buf_size, const char *s);
639 static char *pstrcat(char *buf, int buf_size, const char *s);
641 static void next(void);
642 static void next_nomacro(void);
643 static void parse_expr_type(CType *type);
644 static void expr_type(CType *type);
645 static void unary_type(CType *type);
646 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
647 int case_reg, int is_expr);
648 static int expr_const(void);
649 static void expr_eq(void);
650 static void gexpr(void);
651 static void decl(int l);
652 static void decl_initializer(CType *type, Section *sec, unsigned long c,
653 int first, int size_only);
654 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
655 int has_init, int v, int scope);
656 int gv(int rc);
657 void gv2(int rc1, int rc2);
658 void move_reg(int r, int s);
659 void save_regs(int n);
660 void save_reg(int r);
661 void vpop(void);
662 void vswap(void);
663 void vdup(void);
664 int get_reg(int rc);
666 static void macro_subst(TokenString *tok_str,
667 Sym **nested_list, const int *macro_str);
668 int save_reg_forced(int r);
669 void gen_op(int op);
670 void force_charshort_cast(int t);
671 static void gen_cast(CType *type);
672 void vstore(void);
673 static Sym *sym_find(int v);
674 static Sym *sym_push(int v, CType *type, int r, int c);
676 /* type handling */
677 static int type_size(CType *type, int *a);
678 static inline CType *pointed_type(CType *type);
679 static int pointed_size(CType *type);
680 static int lvalue_type(int t);
681 static int is_compatible_types(CType *type1, CType *type2);
682 static int parse_btype(CType *type, AttributeDef *ad);
683 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
685 void error(const char *fmt, ...);
686 void vpushi(int v);
687 void vset(CType *type, int r, int v);
688 void type_to_str(char *buf, int buf_size,
689 CType *type, const char *varstr);
690 char *get_tok_str(int v, CValue *cv);
691 static Sym *get_sym_ref(CType *type, Section *sec,
692 unsigned long offset, unsigned long size);
693 static Sym *external_global_sym(int v, CType *type, int r);
695 /* section generation */
696 static void section_realloc(Section *sec, unsigned long new_size);
697 static void *section_ptr_add(Section *sec, unsigned long size);
698 static void put_extern_sym(Sym *sym, Section *section,
699 unsigned long value, unsigned long size);
700 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
701 static int put_elf_str(Section *s, const char *sym);
702 static int put_elf_sym(Section *s,
703 unsigned long value, unsigned long size,
704 int info, int other, int shndx, const char *name);
705 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
706 int info, int sh_num, const char *name);
707 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
708 int type, int symbol);
709 static void put_stabs(const char *str, int type, int other, int desc,
710 unsigned long value);
711 static void put_stabs_r(const char *str, int type, int other, int desc,
712 unsigned long value, Section *sec, int sym_index);
713 static void put_stabn(int type, int other, int desc, int value);
714 static void put_stabd(int type, int other, int desc);
715 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
717 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
718 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
719 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
721 /* tccasm.c */
723 #ifdef CONFIG_TCC_ASM
725 typedef struct ExprValue {
726 uint32_t v;
727 Sym *sym;
728 } ExprValue;
730 #define MAX_ASM_OPERANDS 30
732 typedef struct ASMOperand {
733 int id; /* GCC 3 optionnal identifier (0 if number only supported */
734 char *constraint;
735 char asm_str[16]; /* computed asm string for operand */
736 SValue *vt; /* C value of the expression */
737 int ref_index; /* if >= 0, gives reference to a output constraint */
738 int priority; /* priority, used to assign registers */
739 int reg; /* if >= 0, register number used for this operand */
740 } ASMOperand;
742 static void asm_expr(TCCState *s1, ExprValue *pe);
743 static int asm_int_expr(TCCState *s1);
744 static int find_constraint(ASMOperand *operands, int nb_operands,
745 const char *name, const char **pp);
747 static int tcc_assemble(TCCState *s1, int do_preprocess);
749 #endif
751 static void asm_instr(void);
753 /* true if float/double/long double type */
754 static inline int is_float(int t)
756 int bt;
757 bt = t & VT_BTYPE;
758 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
761 #ifdef TCC_TARGET_I386
762 #include "i386-gen.c"
763 #endif
765 #ifdef CONFIG_TCC_STATIC
767 #define RTLD_LAZY 0x001
768 #define RTLD_NOW 0x002
769 #define RTLD_GLOBAL 0x100
770 #define RTLD_DEFAULT NULL
772 /* dummy function for profiling */
773 void *dlopen(const char *filename, int flag)
775 return NULL;
778 const char *dlerror(void)
780 return "error";
783 typedef struct TCCSyms {
784 char *str;
785 void *ptr;
786 } TCCSyms;
788 #define TCCSYM(a) { #a, &a, },
790 /* add the symbol you want here if no dynamic linking is done */
791 static TCCSyms tcc_syms[] = {
792 TCCSYM(printf)
793 TCCSYM(fprintf)
794 TCCSYM(fopen)
795 TCCSYM(fclose)
796 { NULL, NULL },
799 void *dlsym(void *handle, const char *symbol)
801 TCCSyms *p;
802 p = tcc_syms;
803 while (p->str != NULL) {
804 if (!strcmp(p->str, symbol))
805 return p->ptr;
806 p++;
808 return NULL;
811 #endif
813 /********************************************************/
815 /* we use our own 'finite' function to avoid potential problems with
816 non standard math libs */
817 /* XXX: endianness dependent */
818 int ieee_finite(double d)
820 int *p = (int *)&d;
821 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
824 /* copy a string and truncate it. */
825 static char *pstrcpy(char *buf, int buf_size, const char *s)
827 char *q, *q_end;
828 int c;
830 if (buf_size > 0) {
831 q = buf;
832 q_end = buf + buf_size - 1;
833 while (q < q_end) {
834 c = *s++;
835 if (c == '\0')
836 break;
837 *q++ = c;
839 *q = '\0';
841 return buf;
844 /* strcat and truncate. */
845 static char *pstrcat(char *buf, int buf_size, const char *s)
847 int len;
848 len = strlen(buf);
849 if (len < buf_size)
850 pstrcpy(buf + len, buf_size - len, s);
851 return buf;
854 /* memory management */
855 #ifdef MEM_DEBUG
856 int mem_cur_size;
857 int mem_max_size;
858 #endif
860 static inline void tcc_free(void *ptr)
862 #ifdef MEM_DEBUG
863 mem_cur_size -= malloc_usable_size(ptr);
864 #endif
865 free(ptr);
868 static void *tcc_malloc(unsigned long size)
870 void *ptr;
871 ptr = malloc(size);
872 if (!ptr && size)
873 error("memory full");
874 #ifdef MEM_DEBUG
875 mem_cur_size += malloc_usable_size(ptr);
876 if (mem_cur_size > mem_max_size)
877 mem_max_size = mem_cur_size;
878 #endif
879 return ptr;
882 static void *tcc_mallocz(unsigned long size)
884 void *ptr;
885 ptr = tcc_malloc(size);
886 memset(ptr, 0, size);
887 return ptr;
890 static inline void *tcc_realloc(void *ptr, unsigned long size)
892 void *ptr1;
893 #ifdef MEM_DEBUG
894 mem_cur_size -= malloc_usable_size(ptr);
895 #endif
896 ptr1 = realloc(ptr, size);
897 #ifdef MEM_DEBUG
898 /* NOTE: count not correct if alloc error, but not critical */
899 mem_cur_size += malloc_usable_size(ptr1);
900 if (mem_cur_size > mem_max_size)
901 mem_max_size = mem_cur_size;
902 #endif
903 return ptr1;
906 static char *tcc_strdup(const char *str)
908 char *ptr;
909 ptr = tcc_malloc(strlen(str) + 1);
910 strcpy(ptr, str);
911 return ptr;
914 #define free(p) use_tcc_free(p)
915 #define malloc(s) use_tcc_malloc(s)
916 #define realloc(p, s) use_tcc_realloc(p, s)
918 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
920 int nb, nb_alloc;
921 void **pp;
923 nb = *nb_ptr;
924 pp = *ptab;
925 /* every power of two we double array size */
926 if ((nb & (nb - 1)) == 0) {
927 if (!nb)
928 nb_alloc = 1;
929 else
930 nb_alloc = nb * 2;
931 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
932 if (!pp)
933 error("memory full");
934 *ptab = pp;
936 pp[nb++] = data;
937 *nb_ptr = nb;
940 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
942 Section *sec;
944 sec = tcc_mallocz(sizeof(Section));
945 pstrcpy(sec->name, sizeof(sec->name), name);
946 sec->sh_type = sh_type;
947 sec->sh_flags = sh_flags;
948 switch(sh_type) {
949 case SHT_HASH:
950 case SHT_REL:
951 case SHT_DYNSYM:
952 case SHT_SYMTAB:
953 case SHT_DYNAMIC:
954 sec->sh_addralign = 4;
955 break;
956 case SHT_STRTAB:
957 sec->sh_addralign = 1;
958 break;
959 default:
960 sec->sh_addralign = 32; /* default conservative alignment */
961 break;
964 /* only add section if not private */
965 if (!(sh_flags & SHF_PRIVATE)) {
966 sec->sh_num = s1->nb_sections;
967 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
969 return sec;
972 static void free_section(Section *s)
974 tcc_free(s->data);
975 tcc_free(s);
978 /* realloc section and set its content to zero */
979 static void section_realloc(Section *sec, unsigned long new_size)
981 unsigned long size;
982 unsigned char *data;
984 size = sec->data_allocated;
985 if (size == 0)
986 size = 1;
987 while (size < new_size)
988 size = size * 2;
989 data = tcc_realloc(sec->data, size);
990 if (!data)
991 error("memory full");
992 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
993 sec->data = data;
994 sec->data_allocated = size;
997 /* reserve at least 'size' bytes in section 'sec' from
998 sec->data_offset. */
999 static void *section_ptr_add(Section *sec, unsigned long size)
1001 unsigned long offset, offset1;
1003 offset = sec->data_offset;
1004 offset1 = offset + size;
1005 if (offset1 > sec->data_allocated)
1006 section_realloc(sec, offset1);
1007 sec->data_offset = offset1;
1008 return sec->data + offset;
1011 /* return a reference to a section, and create it if it does not
1012 exists */
1013 Section *find_section(TCCState *s1, const char *name)
1015 Section *sec;
1016 int i;
1017 for(i = 1; i < s1->nb_sections; i++) {
1018 sec = s1->sections[i];
1019 if (!strcmp(name, sec->name))
1020 return sec;
1022 /* sections are created as PROGBITS */
1023 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1026 /* update sym->c so that it points to an external symbol in section
1027 'section' with value 'value' */
1028 static void put_extern_sym(Sym *sym, Section *section,
1029 unsigned long value, unsigned long size)
1031 int sym_type, sym_bind, sh_num, info;
1032 Elf32_Sym *esym;
1033 const char *name;
1035 if (section)
1036 sh_num = section->sh_num;
1037 else
1038 sh_num = SHN_UNDEF;
1039 if (!sym->c) {
1040 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1041 sym_type = STT_FUNC;
1042 else
1043 sym_type = STT_OBJECT;
1044 if (sym->type.t & VT_STATIC)
1045 sym_bind = STB_LOCAL;
1046 else
1047 sym_bind = STB_GLOBAL;
1049 name = get_tok_str(sym->v, NULL);
1050 #ifdef CONFIG_TCC_BCHECK
1051 if (do_bounds_check) {
1052 char buf[32];
1054 /* XXX: avoid doing that for statics ? */
1055 /* if bound checking is activated, we change some function
1056 names by adding the "__bound" prefix */
1057 switch(sym->v) {
1058 #if 0
1059 /* XXX: we rely only on malloc hooks */
1060 case TOK_malloc:
1061 case TOK_free:
1062 case TOK_realloc:
1063 case TOK_memalign:
1064 case TOK_calloc:
1065 #endif
1066 case TOK_memcpy:
1067 case TOK_memmove:
1068 case TOK_memset:
1069 case TOK_strlen:
1070 case TOK_strcpy:
1071 strcpy(buf, "__bound_");
1072 strcat(buf, name);
1073 name = buf;
1074 break;
1077 #endif
1078 info = ELF32_ST_INFO(sym_bind, sym_type);
1079 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1080 } else {
1081 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1082 esym->st_value = value;
1083 esym->st_size = size;
1084 esym->st_shndx = sh_num;
1088 /* add a new relocation entry to symbol 'sym' in section 's' */
1089 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1091 if (!sym->c)
1092 put_extern_sym(sym, NULL, 0, 0);
1093 /* now we can add ELF relocation info */
1094 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1097 static inline int isid(int c)
1099 return (c >= 'a' && c <= 'z') ||
1100 (c >= 'A' && c <= 'Z') ||
1101 c == '_';
1104 static inline int isnum(int c)
1106 return c >= '0' && c <= '9';
1109 static inline int isoct(int c)
1111 return c >= '0' && c <= '7';
1114 static inline int toup(int c)
1116 if (c >= 'a' && c <= 'z')
1117 return c - 'a' + 'A';
1118 else
1119 return c;
1122 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1124 int len;
1125 len = strlen(buf);
1126 vsnprintf(buf + len, buf_size - len, fmt, ap);
1129 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1131 va_list ap;
1132 va_start(ap, fmt);
1133 strcat_vprintf(buf, buf_size, fmt, ap);
1134 va_end(ap);
1137 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1139 char buf[2048];
1140 BufferedFile **f;
1142 buf[0] = '\0';
1143 if (file) {
1144 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1145 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1146 (*f)->filename, (*f)->line_num);
1147 if (file->line_num > 0) {
1148 strcat_printf(buf, sizeof(buf),
1149 "%s:%d: ", file->filename, file->line_num);
1150 } else {
1151 strcat_printf(buf, sizeof(buf),
1152 "%s: ", file->filename);
1154 } else {
1155 strcat_printf(buf, sizeof(buf),
1156 "tcc: ");
1158 if (is_warning)
1159 strcat_printf(buf, sizeof(buf), "warning: ");
1160 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1162 if (!s1->error_func) {
1163 /* default case: stderr */
1164 fprintf(stderr, "%s\n", buf);
1165 } else {
1166 s1->error_func(s1->error_opaque, buf);
1168 if (!is_warning)
1169 s1->nb_errors++;
1172 #ifdef LIBTCC
1173 void tcc_set_error_func(TCCState *s, void *error_opaque,
1174 void (*error_func)(void *opaque, const char *msg))
1176 s->error_opaque = error_opaque;
1177 s->error_func = error_func;
1179 #endif
1181 /* error without aborting current compilation */
1182 void error_noabort(const char *fmt, ...)
1184 TCCState *s1 = tcc_state;
1185 va_list ap;
1187 va_start(ap, fmt);
1188 error1(s1, 0, fmt, ap);
1189 va_end(ap);
1192 void error(const char *fmt, ...)
1194 TCCState *s1 = tcc_state;
1195 va_list ap;
1197 va_start(ap, fmt);
1198 error1(s1, 0, fmt, ap);
1199 va_end(ap);
1200 /* better than nothing: in some cases, we accept to handle errors */
1201 if (s1->error_set_jmp_enabled) {
1202 longjmp(s1->error_jmp_buf, 1);
1203 } else {
1204 /* XXX: eliminate this someday */
1205 exit(1);
1209 void expect(const char *msg)
1211 error("%s expected", msg);
1214 void warning(const char *fmt, ...)
1216 TCCState *s1 = tcc_state;
1217 va_list ap;
1219 va_start(ap, fmt);
1220 error1(s1, 1, fmt, ap);
1221 va_end(ap);
1224 void skip(int c)
1226 if (tok != c)
1227 error("'%c' expected", c);
1228 next();
1231 static void test_lvalue(void)
1233 if (!(vtop->r & VT_LVAL))
1234 expect("lvalue");
1237 /* allocate a new token */
1238 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1240 TokenSym *ts, **ptable;
1241 int i;
1243 if (tok_ident >= SYM_FIRST_ANOM)
1244 error("memory full");
1246 /* expand token table if needed */
1247 i = tok_ident - TOK_IDENT;
1248 if ((i % TOK_ALLOC_INCR) == 0) {
1249 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1250 if (!ptable)
1251 error("memory full");
1252 table_ident = ptable;
1255 ts = tcc_malloc(sizeof(TokenSym) + len);
1256 table_ident[i] = ts;
1257 ts->tok = tok_ident++;
1258 ts->sym_define = NULL;
1259 ts->sym_label = NULL;
1260 ts->sym_struct = NULL;
1261 ts->sym_identifier = NULL;
1262 ts->len = len;
1263 ts->hash_next = NULL;
1264 memcpy(ts->str, str, len);
1265 ts->str[len] = '\0';
1266 *pts = ts;
1267 return ts;
1270 #define TOK_HASH_INIT 1
1271 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1273 /* find a token and add it if not found */
1274 static TokenSym *tok_alloc(const char *str, int len)
1276 TokenSym *ts, **pts;
1277 int i;
1278 unsigned int h;
1280 h = TOK_HASH_INIT;
1281 for(i=0;i<len;i++)
1282 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1283 h &= (TOK_HASH_SIZE - 1);
1285 pts = &hash_ident[h];
1286 for(;;) {
1287 ts = *pts;
1288 if (!ts)
1289 break;
1290 if (ts->len == len && !memcmp(ts->str, str, len))
1291 return ts;
1292 pts = &(ts->hash_next);
1294 return tok_alloc_new(pts, str, len);
1297 /* CString handling */
1299 static void cstr_realloc(CString *cstr, int new_size)
1301 int size;
1302 void *data;
1304 size = cstr->size_allocated;
1305 if (size == 0)
1306 size = 8; /* no need to allocate a too small first string */
1307 while (size < new_size)
1308 size = size * 2;
1309 data = tcc_realloc(cstr->data_allocated, size);
1310 if (!data)
1311 error("memory full");
1312 cstr->data_allocated = data;
1313 cstr->size_allocated = size;
1314 cstr->data = data;
1317 /* add a byte */
1318 static void cstr_ccat(CString *cstr, int ch)
1320 int size;
1321 size = cstr->size + 1;
1322 if (size > cstr->size_allocated)
1323 cstr_realloc(cstr, size);
1324 ((unsigned char *)cstr->data)[size - 1] = ch;
1325 cstr->size = size;
1328 static void cstr_cat(CString *cstr, const char *str)
1330 int c;
1331 for(;;) {
1332 c = *str;
1333 if (c == '\0')
1334 break;
1335 cstr_ccat(cstr, c);
1336 str++;
1340 /* add a wide char */
1341 static void cstr_wccat(CString *cstr, int ch)
1343 int size;
1344 size = cstr->size + sizeof(int);
1345 if (size > cstr->size_allocated)
1346 cstr_realloc(cstr, size);
1347 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1348 cstr->size = size;
1351 static void cstr_new(CString *cstr)
1353 memset(cstr, 0, sizeof(CString));
1356 /* free string and reset it to NULL */
1357 static void cstr_free(CString *cstr)
1359 tcc_free(cstr->data_allocated);
1360 cstr_new(cstr);
1363 #define cstr_reset(cstr) cstr_free(cstr)
1365 static CString *cstr_dup(CString *cstr1)
1367 CString *cstr;
1368 int size;
1370 cstr = tcc_malloc(sizeof(CString));
1371 size = cstr1->size;
1372 cstr->size = size;
1373 cstr->size_allocated = size;
1374 cstr->data_allocated = tcc_malloc(size);
1375 cstr->data = cstr->data_allocated;
1376 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1377 return cstr;
1380 /* XXX: unicode ? */
1381 static void add_char(CString *cstr, int c)
1383 if (c == '\'' || c == '\"' || c == '\\') {
1384 /* XXX: could be more precise if char or string */
1385 cstr_ccat(cstr, '\\');
1387 if (c >= 32 && c <= 126) {
1388 cstr_ccat(cstr, c);
1389 } else {
1390 cstr_ccat(cstr, '\\');
1391 if (c == '\n') {
1392 cstr_ccat(cstr, 'n');
1393 } else {
1394 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1395 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1396 cstr_ccat(cstr, '0' + (c & 7));
1401 /* XXX: buffer overflow */
1402 /* XXX: float tokens */
1403 char *get_tok_str(int v, CValue *cv)
1405 static char buf[STRING_MAX_SIZE + 1];
1406 static CString cstr_buf;
1407 CString *cstr;
1408 unsigned char *q;
1409 char *p;
1410 int i, len;
1412 /* NOTE: to go faster, we give a fixed buffer for small strings */
1413 cstr_reset(&cstr_buf);
1414 cstr_buf.data = buf;
1415 cstr_buf.size_allocated = sizeof(buf);
1416 p = buf;
1418 switch(v) {
1419 case TOK_CINT:
1420 case TOK_CUINT:
1421 /* XXX: not quite exact, but only useful for testing */
1422 sprintf(p, "%u", cv->ui);
1423 break;
1424 case TOK_CLLONG:
1425 case TOK_CULLONG:
1426 /* XXX: not quite exact, but only useful for testing */
1427 sprintf(p, "%Lu", cv->ull);
1428 break;
1429 case TOK_CCHAR:
1430 case TOK_LCHAR:
1431 cstr_ccat(&cstr_buf, '\'');
1432 add_char(&cstr_buf, cv->i);
1433 cstr_ccat(&cstr_buf, '\'');
1434 cstr_ccat(&cstr_buf, '\0');
1435 break;
1436 case TOK_PPNUM:
1437 cstr = cv->cstr;
1438 len = cstr->size - 1;
1439 for(i=0;i<len;i++)
1440 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1441 cstr_ccat(&cstr_buf, '\0');
1442 break;
1443 case TOK_STR:
1444 case TOK_LSTR:
1445 cstr = cv->cstr;
1446 cstr_ccat(&cstr_buf, '\"');
1447 if (v == TOK_STR) {
1448 len = cstr->size - 1;
1449 for(i=0;i<len;i++)
1450 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1451 } else {
1452 len = (cstr->size / sizeof(int)) - 1;
1453 for(i=0;i<len;i++)
1454 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1456 cstr_ccat(&cstr_buf, '\"');
1457 cstr_ccat(&cstr_buf, '\0');
1458 break;
1459 case TOK_LT:
1460 v = '<';
1461 goto addv;
1462 case TOK_GT:
1463 v = '>';
1464 goto addv;
1465 case TOK_A_SHL:
1466 return strcpy(p, "<<=");
1467 case TOK_A_SAR:
1468 return strcpy(p, ">>=");
1469 default:
1470 if (v < TOK_IDENT) {
1471 /* search in two bytes table */
1472 q = tok_two_chars;
1473 while (*q) {
1474 if (q[2] == v) {
1475 *p++ = q[0];
1476 *p++ = q[1];
1477 *p = '\0';
1478 return buf;
1480 q += 3;
1482 addv:
1483 *p++ = v;
1484 *p = '\0';
1485 } else if (v < tok_ident) {
1486 return table_ident[v - TOK_IDENT]->str;
1487 } else if (v >= SYM_FIRST_ANOM) {
1488 /* special name for anonymous symbol */
1489 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1490 } else {
1491 /* should never happen */
1492 return NULL;
1494 break;
1496 return cstr_buf.data;
1499 /* push, without hashing */
1500 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1502 Sym *s;
1503 s = tcc_malloc(sizeof(Sym));
1504 s->v = v;
1505 s->type.t = t;
1506 s->c = c;
1507 s->next = NULL;
1508 /* add in stack */
1509 s->prev = *ps;
1510 *ps = s;
1511 return s;
1514 /* find a symbol and return its associated structure. 's' is the top
1515 of the symbol stack */
1516 static Sym *sym_find2(Sym *s, int v)
1518 while (s) {
1519 if (s->v == v)
1520 return s;
1521 s = s->prev;
1523 return NULL;
1526 /* structure lookup */
1527 static inline Sym *struct_find(int v)
1529 v -= TOK_IDENT;
1530 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1531 return NULL;
1532 return table_ident[v]->sym_struct;
1535 /* find an identifier */
1536 static inline Sym *sym_find(int v)
1538 v -= TOK_IDENT;
1539 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1540 return NULL;
1541 return table_ident[v]->sym_identifier;
1544 /* push a given symbol on the symbol stack */
1545 static Sym *sym_push(int v, CType *type, int r, int c)
1547 Sym *s, **ps;
1548 TokenSym *ts;
1550 if (local_stack)
1551 ps = &local_stack;
1552 else
1553 ps = &global_stack;
1554 s = sym_push2(ps, v, type->t, c);
1555 s->type.ref = type->ref;
1556 s->r = r;
1557 /* don't record fields or anonymous symbols */
1558 /* XXX: simplify */
1559 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1560 /* record symbol in token array */
1561 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1562 if (v & SYM_STRUCT)
1563 ps = &ts->sym_struct;
1564 else
1565 ps = &ts->sym_identifier;
1566 s->prev_tok = *ps;
1567 *ps = s;
1569 return s;
1572 /* push a global identifier */
1573 static Sym *global_identifier_push(int v, int t, int c)
1575 Sym *s, **ps;
1576 s = sym_push2(&global_stack, v, t, c);
1577 /* don't record anonymous symbol */
1578 if (v < SYM_FIRST_ANOM) {
1579 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1580 /* modify the top most local identifier, so that
1581 sym_identifier will point to 's' when popped */
1582 while (*ps != NULL)
1583 ps = &(*ps)->prev_tok;
1584 s->prev_tok = NULL;
1585 *ps = s;
1587 return s;
1590 /* pop symbols until top reaches 'b' */
1591 static void sym_pop(Sym **ptop, Sym *b)
1593 Sym *s, *ss, **ps;
1594 TokenSym *ts;
1595 int v;
1597 s = *ptop;
1598 while(s != b) {
1599 ss = s->prev;
1600 v = s->v;
1601 /* remove symbol in token array */
1602 /* XXX: simplify */
1603 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1604 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1605 if (v & SYM_STRUCT)
1606 ps = &ts->sym_struct;
1607 else
1608 ps = &ts->sym_identifier;
1609 *ps = s->prev_tok;
1611 tcc_free(s);
1612 s = ss;
1614 *ptop = b;
1617 /* I/O layer */
1619 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1621 int fd;
1622 BufferedFile *bf;
1624 fd = open(filename, O_RDONLY);
1625 if (fd < 0)
1626 return NULL;
1627 bf = tcc_malloc(sizeof(BufferedFile));
1628 if (!bf) {
1629 close(fd);
1630 return NULL;
1632 bf->fd = fd;
1633 bf->buf_ptr = bf->buffer;
1634 bf->buf_end = bf->buffer;
1635 bf->buffer[0] = CH_EOB; /* put eob symbol */
1636 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1637 bf->line_num = 1;
1638 bf->ifndef_macro = 0;
1639 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1640 // printf("opening '%s'\n", filename);
1641 return bf;
1644 void tcc_close(BufferedFile *bf)
1646 total_lines += bf->line_num;
1647 close(bf->fd);
1648 tcc_free(bf);
1651 /* fill input buffer and peek next char */
1652 static int tcc_peekc_slow(BufferedFile *bf)
1654 int len;
1655 /* only tries to read if really end of buffer */
1656 if (bf->buf_ptr >= bf->buf_end) {
1657 if (bf->fd != -1) {
1658 #if defined(PARSE_DEBUG)
1659 len = 8;
1660 #else
1661 len = IO_BUF_SIZE;
1662 #endif
1663 len = read(bf->fd, bf->buffer, len);
1664 if (len < 0)
1665 len = 0;
1666 } else {
1667 len = 0;
1669 total_bytes += len;
1670 bf->buf_ptr = bf->buffer;
1671 bf->buf_end = bf->buffer + len;
1672 *bf->buf_end = CH_EOB;
1674 if (bf->buf_ptr < bf->buf_end) {
1675 return bf->buf_ptr[0];
1676 } else {
1677 bf->buf_ptr = bf->buf_end;
1678 return CH_EOF;
1682 /* return the current character, handling end of block if necessary
1683 (but not stray) */
1684 static int handle_eob(void)
1686 return tcc_peekc_slow(file);
1689 /* read next char from current input file and handle end of input buffer */
1690 static inline void inp(void)
1692 ch = *(++(file->buf_ptr));
1693 /* end of buffer/file handling */
1694 if (ch == CH_EOB)
1695 ch = handle_eob();
1698 /* handle '\[\r]\n' */
1699 static void handle_stray(void)
1701 while (ch == '\\') {
1702 inp();
1703 if (ch == '\n') {
1704 file->line_num++;
1705 inp();
1706 } else if (ch == '\r') {
1707 inp();
1708 if (ch != '\n')
1709 goto fail;
1710 file->line_num++;
1711 inp();
1712 } else {
1713 fail:
1714 error("stray '\\' in program");
1719 /* skip the stray and handle the \\n case. Output an error if
1720 incorrect char after the stray */
1721 static int handle_stray1(uint8_t *p)
1723 int c;
1725 if (p >= file->buf_end) {
1726 file->buf_ptr = p;
1727 c = handle_eob();
1728 p = file->buf_ptr;
1729 if (c == '\\')
1730 goto parse_stray;
1731 } else {
1732 parse_stray:
1733 file->buf_ptr = p;
1734 ch = *p;
1735 handle_stray();
1736 p = file->buf_ptr;
1737 c = *p;
1739 return c;
1742 /* handle just the EOB case, but not stray */
1743 #define PEEKC_EOB(c, p)\
1745 p++;\
1746 c = *p;\
1747 if (c == '\\') {\
1748 file->buf_ptr = p;\
1749 c = handle_eob();\
1750 p = file->buf_ptr;\
1754 /* handle the complicated stray case */
1755 #define PEEKC(c, p)\
1757 p++;\
1758 c = *p;\
1759 if (c == '\\') {\
1760 c = handle_stray1(p);\
1761 p = file->buf_ptr;\
1765 /* input with '\[\r]\n' handling. Note that this function cannot
1766 handle other characters after '\', so you cannot call it inside
1767 strings or comments */
1768 static void minp(void)
1770 inp();
1771 if (ch == '\\')
1772 handle_stray();
1776 /* single line C++ comments */
1777 static uint8_t *parse_line_comment(uint8_t *p)
1779 int c;
1781 p++;
1782 for(;;) {
1783 c = *p;
1784 if (c == '\n' || c == CH_EOF) {
1785 break;
1786 } else if (c == '\\') {
1787 PEEKC_EOB(c, p);
1788 if (c == '\n') {
1789 file->line_num++;
1790 PEEKC_EOB(c, p);
1791 } else if (c == '\r') {
1792 PEEKC_EOB(c, p);
1793 if (c == '\n') {
1794 file->line_num++;
1795 PEEKC_EOB(c, p);
1798 } else {
1799 p++;
1802 return p;
1805 /* C comments */
1806 static uint8_t *parse_comment(uint8_t *p)
1808 int c;
1810 p++;
1811 for(;;) {
1812 /* fast skip loop */
1813 for(;;) {
1814 c = *p;
1815 if (c == '\n' || c == '*' || c == '\\')
1816 break;
1817 p++;
1818 c = *p;
1819 if (c == '\n' || c == '*' || c == '\\')
1820 break;
1821 p++;
1823 /* now we can handle all the cases */
1824 if (c == '\n') {
1825 file->line_num++;
1826 p++;
1827 } else if (c == '*') {
1828 p++;
1829 for(;;) {
1830 c = *p;
1831 if (c == '*') {
1832 p++;
1833 } else if (c == '/') {
1834 goto end_of_comment;
1835 } else if (c == '\\') {
1836 file->buf_ptr = p;
1837 c = handle_eob();
1838 p = file->buf_ptr;
1839 if (c == '\\') {
1840 /* skip '\[\r]\n', otherwise just skip the stray */
1841 while (c == '\\') {
1842 PEEKC_EOB(c, p);
1843 if (c == '\n') {
1844 file->line_num++;
1845 PEEKC_EOB(c, p);
1846 } else if (c == '\r') {
1847 PEEKC_EOB(c, p);
1848 if (c == '\n') {
1849 file->line_num++;
1850 PEEKC_EOB(c, p);
1852 } else {
1853 goto after_star;
1857 } else {
1858 break;
1861 after_star: ;
1862 } else {
1863 /* stray, eob or eof */
1864 file->buf_ptr = p;
1865 c = handle_eob();
1866 p = file->buf_ptr;
1867 if (c == CH_EOF) {
1868 error("unexpected end of file in comment");
1869 } else if (c == '\\') {
1870 p++;
1874 end_of_comment:
1875 p++;
1876 return p;
1879 #define cinp minp
1881 /* space exlcuding newline */
1882 static inline int is_space(int ch)
1884 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1887 static inline void skip_spaces(void)
1889 while (is_space(ch))
1890 cinp();
1893 /* parse a string without interpreting escapes */
1894 static uint8_t *parse_pp_string(uint8_t *p,
1895 int sep, CString *str)
1897 int c;
1898 p++;
1899 for(;;) {
1900 c = *p;
1901 if (c == sep) {
1902 break;
1903 } else if (c == '\\') {
1904 file->buf_ptr = p;
1905 c = handle_eob();
1906 p = file->buf_ptr;
1907 if (c == CH_EOF) {
1908 unterminated_string:
1909 /* XXX: indicate line number of start of string */
1910 error("missing terminating %c character", sep);
1911 } else if (c == '\\') {
1912 /* escape : just skip \[\r]\n */
1913 PEEKC_EOB(c, p);
1914 if (c == '\n') {
1915 file->line_num++;
1916 p++;
1917 } else if (c == '\r') {
1918 PEEKC_EOB(c, p);
1919 if (c != '\n')
1920 expect("'\n' after '\r'");
1921 file->line_num++;
1922 p++;
1923 } else if (c == CH_EOF) {
1924 goto unterminated_string;
1925 } else {
1926 if (str) {
1927 cstr_ccat(str, '\\');
1928 cstr_ccat(str, c);
1930 p++;
1933 } else if (c == '\n') {
1934 file->line_num++;
1935 goto add_char;
1936 } else if (c == '\r') {
1937 PEEKC_EOB(c, p);
1938 if (c != '\n') {
1939 cstr_ccat(str, '\r');
1940 } else {
1941 file->line_num++;
1942 goto add_char;
1944 } else {
1945 add_char:
1946 if (str)
1947 cstr_ccat(str, c);
1948 p++;
1951 p++;
1952 return p;
1955 /* skip block of text until #else, #elif or #endif. skip also pairs of
1956 #if/#endif */
1957 void preprocess_skip(void)
1959 int a, start_of_line, c;
1960 uint8_t *p;
1962 p = file->buf_ptr;
1963 start_of_line = 1;
1964 a = 0;
1965 for(;;) {
1966 redo_no_start:
1967 c = *p;
1968 switch(c) {
1969 case ' ':
1970 case '\t':
1971 case '\f':
1972 case '\v':
1973 case '\r':
1974 p++;
1975 goto redo_no_start;
1976 case '\n':
1977 start_of_line = 1;
1978 file->line_num++;
1979 p++;
1980 goto redo_no_start;
1981 case '\\':
1982 file->buf_ptr = p;
1983 c = handle_eob();
1984 if (c == CH_EOF) {
1985 expect("#endif");
1986 } else if (c == '\\') {
1987 /* XXX: incorrect: should not give an error */
1988 ch = file->buf_ptr[0];
1989 handle_stray();
1991 p = file->buf_ptr;
1992 goto redo_no_start;
1993 /* skip strings */
1994 case '\"':
1995 case '\'':
1996 p = parse_pp_string(p, c, NULL);
1997 break;
1998 /* skip comments */
1999 case '/':
2000 file->buf_ptr = p;
2001 ch = *p;
2002 minp();
2003 p = file->buf_ptr;
2004 if (ch == '*') {
2005 p = parse_comment(p);
2006 } else if (ch == '/') {
2007 p = parse_line_comment(p);
2009 break;
2011 case '#':
2012 p++;
2013 if (start_of_line) {
2014 file->buf_ptr = p;
2015 next_nomacro();
2016 p = file->buf_ptr;
2017 if (a == 0 &&
2018 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2019 goto the_end;
2020 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2021 a++;
2022 else if (tok == TOK_ENDIF)
2023 a--;
2025 break;
2026 default:
2027 p++;
2028 break;
2030 start_of_line = 0;
2032 the_end: ;
2033 file->buf_ptr = p;
2036 /* ParseState handling */
2038 /* XXX: currently, no include file info is stored. Thus, we cannot display
2039 accurate messages if the function or data definition spans multiple
2040 files */
2042 /* save current parse state in 's' */
2043 void save_parse_state(ParseState *s)
2045 s->line_num = file->line_num;
2046 s->macro_ptr = macro_ptr;
2047 s->tok = tok;
2048 s->tokc = tokc;
2051 /* restore parse state from 's' */
2052 void restore_parse_state(ParseState *s)
2054 file->line_num = s->line_num;
2055 macro_ptr = s->macro_ptr;
2056 tok = s->tok;
2057 tokc = s->tokc;
2060 /* return the number of additional 'ints' necessary to store the
2061 token */
2062 static inline int tok_ext_size(int t)
2064 switch(t) {
2065 /* 4 bytes */
2066 case TOK_CINT:
2067 case TOK_CUINT:
2068 case TOK_CCHAR:
2069 case TOK_LCHAR:
2070 case TOK_STR:
2071 case TOK_LSTR:
2072 case TOK_CFLOAT:
2073 case TOK_LINENUM:
2074 case TOK_PPNUM:
2075 return 1;
2076 case TOK_CDOUBLE:
2077 case TOK_CLLONG:
2078 case TOK_CULLONG:
2079 return 2;
2080 case TOK_CLDOUBLE:
2081 return LDOUBLE_SIZE / 4;
2082 default:
2083 return 0;
2087 /* token string handling */
2089 static inline void tok_str_new(TokenString *s)
2091 s->str = NULL;
2092 s->len = 0;
2093 s->allocated_len = 0;
2094 s->last_line_num = -1;
2097 static void tok_str_free(int *str)
2099 const int *p;
2100 CString *cstr;
2101 int t;
2103 p = str;
2104 for(;;) {
2105 t = *p;
2106 /* NOTE: we test zero separately so that GCC can generate a
2107 table for the following switch */
2108 if (t == 0)
2109 break;
2110 switch(t) {
2111 case TOK_CINT:
2112 case TOK_CUINT:
2113 case TOK_CCHAR:
2114 case TOK_LCHAR:
2115 case TOK_CFLOAT:
2116 case TOK_LINENUM:
2117 p += 2;
2118 break;
2119 case TOK_PPNUM:
2120 case TOK_STR:
2121 case TOK_LSTR:
2122 /* XXX: use a macro to be portable on 64 bit ? */
2123 cstr = (CString *)p[1];
2124 cstr_free(cstr);
2125 tcc_free(cstr);
2126 p += 2;
2127 break;
2128 case TOK_CDOUBLE:
2129 case TOK_CLLONG:
2130 case TOK_CULLONG:
2131 p += 3;
2132 break;
2133 case TOK_CLDOUBLE:
2134 p += 1 + (LDOUBLE_SIZE / 4);
2135 break;
2136 default:
2137 p++;
2138 break;
2141 tcc_free(str);
2144 static int *tok_str_realloc(TokenString *s)
2146 int *str, len;
2148 len = s->allocated_len + TOK_STR_ALLOC_INCR;
2149 str = tcc_realloc(s->str, len * sizeof(int));
2150 if (!str)
2151 error("memory full");
2152 s->allocated_len = len;
2153 s->str = str;
2154 return str;
2157 static void tok_str_add(TokenString *s, int t)
2159 int len, *str;
2161 len = s->len;
2162 str = s->str;
2163 if (len >= s->allocated_len)
2164 str = tok_str_realloc(s);
2165 str[len++] = t;
2166 s->len = len;
2169 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2171 int len, *str;
2173 len = s->len;
2174 str = s->str;
2176 /* allocate space for worst case */
2177 if (len + TOK_MAX_SIZE > s->allocated_len)
2178 str = tok_str_realloc(s);
2179 str[len++] = t;
2180 switch(t) {
2181 case TOK_CINT:
2182 case TOK_CUINT:
2183 case TOK_CCHAR:
2184 case TOK_LCHAR:
2185 case TOK_CFLOAT:
2186 case TOK_LINENUM:
2187 str[len++] = cv->tab[0];
2188 break;
2189 case TOK_PPNUM:
2190 case TOK_STR:
2191 case TOK_LSTR:
2192 str[len++] = (int)cstr_dup(cv->cstr);
2193 break;
2194 case TOK_CDOUBLE:
2195 case TOK_CLLONG:
2196 case TOK_CULLONG:
2197 str[len++] = cv->tab[0];
2198 str[len++] = cv->tab[1];
2199 break;
2200 case TOK_CLDOUBLE:
2201 #if LDOUBLE_SIZE == 12
2202 str[len++] = cv->tab[0];
2203 str[len++] = cv->tab[1];
2204 str[len++] = cv->tab[2];
2205 #else
2206 #error add long double size support
2207 #endif
2208 break;
2209 default:
2210 break;
2212 s->len = len;
2215 /* add the current parse token in token string 's' */
2216 static void tok_str_add_tok(TokenString *s)
2218 CValue cval;
2220 /* save line number info */
2221 if (file->line_num != s->last_line_num) {
2222 s->last_line_num = file->line_num;
2223 cval.i = s->last_line_num;
2224 tok_str_add2(s, TOK_LINENUM, &cval);
2226 tok_str_add2(s, tok, &tokc);
2229 #if LDOUBLE_SIZE == 12
2230 #define LDOUBLE_GET(p, cv) \
2231 cv.tab[0] = p[0]; \
2232 cv.tab[1] = p[1]; \
2233 cv.tab[2] = p[2];
2234 #else
2235 #error add long double size support
2236 #endif
2239 /* get a token from an integer array and increment pointer
2240 accordingly. we code it as a macro to avoid pointer aliasing. */
2241 #define TOK_GET(t, p, cv) \
2243 t = *p++; \
2244 switch(t) { \
2245 case TOK_CINT: \
2246 case TOK_CUINT: \
2247 case TOK_CCHAR: \
2248 case TOK_LCHAR: \
2249 case TOK_CFLOAT: \
2250 case TOK_LINENUM: \
2251 case TOK_STR: \
2252 case TOK_LSTR: \
2253 case TOK_PPNUM: \
2254 cv.tab[0] = *p++; \
2255 break; \
2256 case TOK_CDOUBLE: \
2257 case TOK_CLLONG: \
2258 case TOK_CULLONG: \
2259 cv.tab[0] = p[0]; \
2260 cv.tab[1] = p[1]; \
2261 p += 2; \
2262 break; \
2263 case TOK_CLDOUBLE: \
2264 LDOUBLE_GET(p, cv); \
2265 p += LDOUBLE_SIZE / 4; \
2266 break; \
2267 default: \
2268 break; \
2272 /* defines handling */
2273 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2275 Sym *s;
2277 s = sym_push2(&define_stack, v, macro_type, (int)str);
2278 s->next = first_arg;
2279 table_ident[v - TOK_IDENT]->sym_define = s;
2282 /* undefined a define symbol. Its name is just set to zero */
2283 static void define_undef(Sym *s)
2285 int v;
2286 v = s->v;
2287 if (v >= TOK_IDENT && v < tok_ident)
2288 table_ident[v - TOK_IDENT]->sym_define = NULL;
2289 s->v = 0;
2292 static inline Sym *define_find(int v)
2294 v -= TOK_IDENT;
2295 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2296 return NULL;
2297 return table_ident[v]->sym_define;
2300 /* free define stack until top reaches 'b' */
2301 static void free_defines(Sym *b)
2303 Sym *top, *top1;
2304 int v;
2306 top = define_stack;
2307 while (top != b) {
2308 top1 = top->prev;
2309 /* do not free args or predefined defines */
2310 if (top->c)
2311 tok_str_free((int *)top->c);
2312 v = top->v;
2313 if (v >= TOK_IDENT && v < tok_ident)
2314 table_ident[v - TOK_IDENT]->sym_define = NULL;
2315 tcc_free(top);
2316 top = top1;
2318 define_stack = b;
2321 /* label lookup */
2322 static Sym *label_find(int v)
2324 v -= TOK_IDENT;
2325 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2326 return NULL;
2327 return table_ident[v]->sym_label;
2330 static Sym *label_push(Sym **ptop, int v, int flags)
2332 Sym *s, **ps;
2333 s = sym_push2(ptop, v, 0, 0);
2334 s->r = flags;
2335 ps = &table_ident[v - TOK_IDENT]->sym_label;
2336 if (ptop == &global_label_stack) {
2337 /* modify the top most local identifier, so that
2338 sym_identifier will point to 's' when popped */
2339 while (*ps != NULL)
2340 ps = &(*ps)->prev_tok;
2342 s->prev_tok = *ps;
2343 *ps = s;
2344 return s;
2347 /* pop labels until element last is reached. Look if any labels are
2348 undefined. Define symbols if '&&label' was used. */
2349 static void label_pop(Sym **ptop, Sym *slast)
2351 Sym *s, *s1;
2352 for(s = *ptop; s != slast; s = s1) {
2353 s1 = s->prev;
2354 if (s->r == LABEL_DECLARED) {
2355 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2356 } else if (s->r == LABEL_FORWARD) {
2357 error("label '%s' used but not defined",
2358 get_tok_str(s->v, NULL));
2359 } else {
2360 if (s->c) {
2361 /* define corresponding symbol. A size of
2362 1 is put. */
2363 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2366 /* remove label */
2367 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2368 tcc_free(s);
2370 *ptop = slast;
2373 /* eval an expression for #if/#elif */
2374 static int expr_preprocess(void)
2376 int c, t;
2377 TokenString str;
2379 tok_str_new(&str);
2380 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2381 next(); /* do macro subst */
2382 if (tok == TOK_DEFINED) {
2383 next_nomacro();
2384 t = tok;
2385 if (t == '(')
2386 next_nomacro();
2387 c = define_find(tok) != 0;
2388 if (t == '(')
2389 next_nomacro();
2390 tok = TOK_CINT;
2391 tokc.i = c;
2392 } else if (tok >= TOK_IDENT) {
2393 /* if undefined macro */
2394 tok = TOK_CINT;
2395 tokc.i = 0;
2397 tok_str_add_tok(&str);
2399 tok_str_add(&str, -1); /* simulate end of file */
2400 tok_str_add(&str, 0);
2401 /* now evaluate C constant expression */
2402 macro_ptr = str.str;
2403 next();
2404 c = expr_const();
2405 macro_ptr = NULL;
2406 tok_str_free(str.str);
2407 return c != 0;
2410 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2411 static void tok_print(int *str)
2413 int t;
2414 CValue cval;
2416 while (1) {
2417 TOK_GET(t, str, cval);
2418 if (!t)
2419 break;
2420 printf(" %s", get_tok_str(t, &cval));
2422 printf("\n");
2424 #endif
2426 /* parse after #define */
2427 static void parse_define(void)
2429 Sym *s, *first, **ps;
2430 int v, t, varg, is_vaargs, c;
2431 TokenString str;
2433 v = tok;
2434 if (v < TOK_IDENT)
2435 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2436 /* XXX: should check if same macro (ANSI) */
2437 first = NULL;
2438 t = MACRO_OBJ;
2439 /* '(' must be just after macro definition for MACRO_FUNC */
2440 c = file->buf_ptr[0];
2441 if (c == '\\')
2442 c = handle_stray1(file->buf_ptr);
2443 if (c == '(') {
2444 next_nomacro();
2445 next_nomacro();
2446 ps = &first;
2447 while (tok != ')') {
2448 varg = tok;
2449 next_nomacro();
2450 is_vaargs = 0;
2451 if (varg == TOK_DOTS) {
2452 varg = TOK___VA_ARGS__;
2453 is_vaargs = 1;
2454 } else if (tok == TOK_DOTS && gnu_ext) {
2455 is_vaargs = 1;
2456 next_nomacro();
2458 if (varg < TOK_IDENT)
2459 error("badly punctuated parameter list");
2460 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2461 *ps = s;
2462 ps = &s->next;
2463 if (tok != ',')
2464 break;
2465 next_nomacro();
2467 t = MACRO_FUNC;
2469 tok_str_new(&str);
2470 next_nomacro();
2471 /* EOF testing necessary for '-D' handling */
2472 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2473 tok_str_add2(&str, tok, &tokc);
2474 next_nomacro();
2476 tok_str_add(&str, 0);
2477 #ifdef PP_DEBUG
2478 printf("define %s %d: ", get_tok_str(v, NULL), t);
2479 tok_print(str.str);
2480 #endif
2481 define_push(v, t, str.str, first);
2484 /* XXX: use a token or a hash table to accelerate matching ? */
2485 static CachedInclude *search_cached_include(TCCState *s1,
2486 int type, const char *filename)
2488 CachedInclude *e;
2489 int i;
2491 for(i = 0;i < s1->nb_cached_includes; i++) {
2492 e = s1->cached_includes[i];
2493 if (e->type == type && !strcmp(e->filename, filename))
2494 return e;
2496 return NULL;
2499 static inline void add_cached_include(TCCState *s1, int type,
2500 const char *filename, int ifndef_macro)
2502 CachedInclude *e;
2504 if (search_cached_include(s1, type, filename))
2505 return;
2506 #ifdef INC_DEBUG
2507 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2508 #endif
2509 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2510 if (!e)
2511 return;
2512 e->type = type;
2513 strcpy(e->filename, filename);
2514 e->ifndef_macro = ifndef_macro;
2515 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2518 /* is_bof is true if first non space token at beginning of file */
2519 static void preprocess(int is_bof)
2521 TCCState *s1 = tcc_state;
2522 int size, i, c, n, saved_parse_flags;
2523 char buf[1024], *q, *p;
2524 char buf1[1024];
2525 BufferedFile *f;
2526 Sym *s;
2527 CachedInclude *e;
2529 saved_parse_flags = parse_flags;
2530 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2531 PARSE_FLAG_LINEFEED;
2532 next_nomacro();
2533 redo:
2534 switch(tok) {
2535 case TOK_DEFINE:
2536 next_nomacro();
2537 parse_define();
2538 break;
2539 case TOK_UNDEF:
2540 next_nomacro();
2541 s = define_find(tok);
2542 /* undefine symbol by putting an invalid name */
2543 if (s)
2544 define_undef(s);
2545 break;
2546 case TOK_INCLUDE:
2547 ch = file->buf_ptr[0];
2548 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2549 skip_spaces();
2550 if (ch == '<') {
2551 c = '>';
2552 goto read_name;
2553 } else if (ch == '\"') {
2554 c = ch;
2555 read_name:
2556 /* XXX: better stray handling */
2557 minp();
2558 q = buf;
2559 while (ch != c && ch != '\n' && ch != CH_EOF) {
2560 if ((q - buf) < sizeof(buf) - 1)
2561 *q++ = ch;
2562 minp();
2564 *q = '\0';
2565 minp();
2566 #if 0
2567 /* eat all spaces and comments after include */
2568 /* XXX: slightly incorrect */
2569 while (ch1 != '\n' && ch1 != CH_EOF)
2570 inp();
2571 #endif
2572 } else {
2573 /* computed #include : either we have only strings or
2574 we have anything enclosed in '<>' */
2575 next();
2576 buf[0] = '\0';
2577 if (tok == TOK_STR) {
2578 while (tok != TOK_LINEFEED) {
2579 if (tok != TOK_STR) {
2580 include_syntax:
2581 error("'#include' expects \"FILENAME\" or <FILENAME>");
2583 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2584 next();
2586 c = '\"';
2587 } else {
2588 int len;
2589 while (tok != TOK_LINEFEED) {
2590 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2591 next();
2593 len = strlen(buf);
2594 /* check syntax and remove '<>' */
2595 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2596 goto include_syntax;
2597 memmove(buf, buf + 1, len - 2);
2598 buf[len - 2] = '\0';
2599 c = '>';
2603 e = search_cached_include(s1, c, buf);
2604 if (e && define_find(e->ifndef_macro)) {
2605 /* no need to parse the include because the 'ifndef macro'
2606 is defined */
2607 #ifdef INC_DEBUG
2608 printf("%s: skipping %s\n", file->filename, buf);
2609 #endif
2610 } else {
2611 if (c == '\"') {
2612 /* first search in current dir if "header.h" */
2613 size = 0;
2614 p = strrchr(file->filename, '/');
2615 if (p)
2616 size = p + 1 - file->filename;
2617 if (size > sizeof(buf1) - 1)
2618 size = sizeof(buf1) - 1;
2619 memcpy(buf1, file->filename, size);
2620 buf1[size] = '\0';
2621 pstrcat(buf1, sizeof(buf1), buf);
2622 f = tcc_open(s1, buf1);
2623 if (f)
2624 goto found;
2626 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2627 error("#include recursion too deep");
2628 /* now search in all the include paths */
2629 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2630 for(i = 0; i < n; i++) {
2631 const char *path;
2632 if (i < s1->nb_include_paths)
2633 path = s1->include_paths[i];
2634 else
2635 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2636 pstrcpy(buf1, sizeof(buf1), path);
2637 pstrcat(buf1, sizeof(buf1), "/");
2638 pstrcat(buf1, sizeof(buf1), buf);
2639 f = tcc_open(s1, buf1);
2640 if (f)
2641 goto found;
2643 error("include file '%s' not found", buf);
2644 f = NULL;
2645 found:
2646 #ifdef INC_DEBUG
2647 printf("%s: including %s\n", file->filename, buf1);
2648 #endif
2649 f->inc_type = c;
2650 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2651 /* push current file in stack */
2652 /* XXX: fix current line init */
2653 *s1->include_stack_ptr++ = file;
2654 file = f;
2655 /* add include file debug info */
2656 if (do_debug) {
2657 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2659 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2660 ch = file->buf_ptr[0];
2661 goto the_end;
2663 break;
2664 case TOK_IFNDEF:
2665 c = 1;
2666 goto do_ifdef;
2667 case TOK_IF:
2668 c = expr_preprocess();
2669 goto do_if;
2670 case TOK_IFDEF:
2671 c = 0;
2672 do_ifdef:
2673 next_nomacro();
2674 if (tok < TOK_IDENT)
2675 error("invalid argument for '#if%sdef'", c ? "n" : "");
2676 if (is_bof) {
2677 if (c) {
2678 #ifdef INC_DEBUG
2679 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2680 #endif
2681 file->ifndef_macro = tok;
2684 c = (define_find(tok) != 0) ^ c;
2685 do_if:
2686 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2687 error("memory full");
2688 *s1->ifdef_stack_ptr++ = c;
2689 goto test_skip;
2690 case TOK_ELSE:
2691 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2692 error("#else without matching #if");
2693 if (s1->ifdef_stack_ptr[-1] & 2)
2694 error("#else after #else");
2695 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2696 goto test_skip;
2697 case TOK_ELIF:
2698 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2699 error("#elif without matching #if");
2700 c = s1->ifdef_stack_ptr[-1];
2701 if (c > 1)
2702 error("#elif after #else");
2703 /* last #if/#elif expression was true: we skip */
2704 if (c == 1)
2705 goto skip;
2706 c = expr_preprocess();
2707 s1->ifdef_stack_ptr[-1] = c;
2708 test_skip:
2709 if (!(c & 1)) {
2710 skip:
2711 preprocess_skip();
2712 is_bof = 0;
2713 goto redo;
2715 break;
2716 case TOK_ENDIF:
2717 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2718 error("#endif without matching #if");
2719 s1->ifdef_stack_ptr--;
2720 /* '#ifndef macro' was at the start of file. Now we check if
2721 an '#endif' is exactly at the end of file */
2722 if (file->ifndef_macro &&
2723 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2724 file->ifndef_macro_saved = file->ifndef_macro;
2725 /* need to set to zero to avoid false matches if another
2726 #ifndef at middle of file */
2727 file->ifndef_macro = 0;
2728 while (tok != TOK_LINEFEED)
2729 next_nomacro();
2730 tok_flags |= TOK_FLAG_ENDIF;
2731 goto the_end;
2733 break;
2734 case TOK_LINE:
2735 next();
2736 if (tok != TOK_CINT)
2737 error("#line");
2738 file->line_num = tokc.i - 1; /* the line number will be incremented after */
2739 next();
2740 if (tok != TOK_LINEFEED) {
2741 if (tok != TOK_STR)
2742 error("#line");
2743 pstrcpy(file->filename, sizeof(file->filename),
2744 (char *)tokc.cstr->data);
2746 break;
2747 case TOK_ERROR:
2748 case TOK_WARNING:
2749 c = tok;
2750 ch = file->buf_ptr[0];
2751 skip_spaces();
2752 q = buf;
2753 while (ch != '\n' && ch != CH_EOF) {
2754 if ((q - buf) < sizeof(buf) - 1)
2755 *q++ = ch;
2756 minp();
2758 *q = '\0';
2759 if (c == TOK_ERROR)
2760 error("#error %s", buf);
2761 else
2762 warning("#warning %s", buf);
2763 break;
2764 case TOK_PRAGMA:
2765 /* ignored */
2766 break;
2767 default:
2768 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
2769 /* '!' is ignored to allow C scripts. numbers are ignored
2770 to emulate cpp behaviour */
2771 } else {
2772 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
2774 break;
2776 /* ignore other preprocess commands or #! for C scripts */
2777 while (tok != TOK_LINEFEED)
2778 next_nomacro();
2779 the_end:
2780 parse_flags = saved_parse_flags;
2783 /* evaluate escape codes in a string. */
2784 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2786 int c, n;
2787 const char *p;
2789 p = buf;
2790 for(;;) {
2791 c = *p;
2792 if (c == '\0')
2793 break;
2794 if (c == '\\') {
2795 p++;
2796 /* escape */
2797 c = *p;
2798 switch(c) {
2799 case '0': case '1': case '2': case '3':
2800 case '4': case '5': case '6': case '7':
2801 /* at most three octal digits */
2802 n = c - '0';
2803 p++;
2804 c = *p;
2805 if (isoct(c)) {
2806 n = n * 8 + c - '0';
2807 p++;
2808 c = *p;
2809 if (isoct(c)) {
2810 n = n * 8 + c - '0';
2811 p++;
2814 c = n;
2815 goto add_char_nonext;
2816 case 'x':
2817 p++;
2818 n = 0;
2819 for(;;) {
2820 c = *p;
2821 if (c >= 'a' && c <= 'f')
2822 c = c - 'a' + 10;
2823 else if (c >= 'A' && c <= 'F')
2824 c = c - 'A' + 10;
2825 else if (isnum(c))
2826 c = c - '0';
2827 else
2828 break;
2829 n = n * 16 + c;
2830 p++;
2832 c = n;
2833 goto add_char_nonext;
2834 case 'a':
2835 c = '\a';
2836 break;
2837 case 'b':
2838 c = '\b';
2839 break;
2840 case 'f':
2841 c = '\f';
2842 break;
2843 case 'n':
2844 c = '\n';
2845 break;
2846 case 'r':
2847 c = '\r';
2848 break;
2849 case 't':
2850 c = '\t';
2851 break;
2852 case 'v':
2853 c = '\v';
2854 break;
2855 case 'e':
2856 if (!gnu_ext)
2857 goto invalid_escape;
2858 c = 27;
2859 break;
2860 case '\'':
2861 case '\"':
2862 case '\\':
2863 case '?':
2864 break;
2865 default:
2866 invalid_escape:
2867 error("invalid escaped char");
2870 p++;
2871 add_char_nonext:
2872 if (!is_long)
2873 cstr_ccat(outstr, c);
2874 else
2875 cstr_wccat(outstr, c);
2877 /* add a trailing '\0' */
2878 if (!is_long)
2879 cstr_ccat(outstr, '\0');
2880 else
2881 cstr_wccat(outstr, '\0');
2884 /* we use 64 bit numbers */
2885 #define BN_SIZE 2
2887 /* bn = (bn << shift) | or_val */
2888 void bn_lshift(unsigned int *bn, int shift, int or_val)
2890 int i;
2891 unsigned int v;
2892 for(i=0;i<BN_SIZE;i++) {
2893 v = bn[i];
2894 bn[i] = (v << shift) | or_val;
2895 or_val = v >> (32 - shift);
2899 void bn_zero(unsigned int *bn)
2901 int i;
2902 for(i=0;i<BN_SIZE;i++) {
2903 bn[i] = 0;
2907 /* parse number in null terminated string 'p' and return it in the
2908 current token */
2909 void parse_number(const char *p)
2911 int b, t, shift, frac_bits, s, exp_val, ch;
2912 char *q;
2913 unsigned int bn[BN_SIZE];
2914 double d;
2916 /* number */
2917 q = token_buf;
2918 ch = *p++;
2919 t = ch;
2920 ch = *p++;
2921 *q++ = t;
2922 b = 10;
2923 if (t == '.') {
2924 goto float_frac_parse;
2925 } else if (t == '0') {
2926 if (ch == 'x' || ch == 'X') {
2927 q--;
2928 ch = *p++;
2929 b = 16;
2930 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2931 q--;
2932 ch = *p++;
2933 b = 2;
2936 /* parse all digits. cannot check octal numbers at this stage
2937 because of floating point constants */
2938 while (1) {
2939 if (ch >= 'a' && ch <= 'f')
2940 t = ch - 'a' + 10;
2941 else if (ch >= 'A' && ch <= 'F')
2942 t = ch - 'A' + 10;
2943 else if (isnum(ch))
2944 t = ch - '0';
2945 else
2946 break;
2947 if (t >= b)
2948 break;
2949 if (q >= token_buf + STRING_MAX_SIZE) {
2950 num_too_long:
2951 error("number too long");
2953 *q++ = ch;
2954 ch = *p++;
2956 if (ch == '.' ||
2957 ((ch == 'e' || ch == 'E') && b == 10) ||
2958 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2959 if (b != 10) {
2960 /* NOTE: strtox should support that for hexa numbers, but
2961 non ISOC99 libcs do not support it, so we prefer to do
2962 it by hand */
2963 /* hexadecimal or binary floats */
2964 /* XXX: handle overflows */
2965 *q = '\0';
2966 if (b == 16)
2967 shift = 4;
2968 else
2969 shift = 2;
2970 bn_zero(bn);
2971 q = token_buf;
2972 while (1) {
2973 t = *q++;
2974 if (t == '\0') {
2975 break;
2976 } else if (t >= 'a') {
2977 t = t - 'a' + 10;
2978 } else if (t >= 'A') {
2979 t = t - 'A' + 10;
2980 } else {
2981 t = t - '0';
2983 bn_lshift(bn, shift, t);
2985 frac_bits = 0;
2986 if (ch == '.') {
2987 ch = *p++;
2988 while (1) {
2989 t = ch;
2990 if (t >= 'a' && t <= 'f') {
2991 t = t - 'a' + 10;
2992 } else if (t >= 'A' && t <= 'F') {
2993 t = t - 'A' + 10;
2994 } else if (t >= '0' && t <= '9') {
2995 t = t - '0';
2996 } else {
2997 break;
2999 if (t >= b)
3000 error("invalid digit");
3001 bn_lshift(bn, shift, t);
3002 frac_bits += shift;
3003 ch = *p++;
3006 if (ch != 'p' && ch != 'P')
3007 expect("exponent");
3008 ch = *p++;
3009 s = 1;
3010 exp_val = 0;
3011 if (ch == '+') {
3012 ch = *p++;
3013 } else if (ch == '-') {
3014 s = -1;
3015 ch = *p++;
3017 if (ch < '0' || ch > '9')
3018 expect("exponent digits");
3019 while (ch >= '0' && ch <= '9') {
3020 exp_val = exp_val * 10 + ch - '0';
3021 ch = *p++;
3023 exp_val = exp_val * s;
3025 /* now we can generate the number */
3026 /* XXX: should patch directly float number */
3027 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3028 d = ldexp(d, exp_val - frac_bits);
3029 t = toup(ch);
3030 if (t == 'F') {
3031 ch = *p++;
3032 tok = TOK_CFLOAT;
3033 /* float : should handle overflow */
3034 tokc.f = (float)d;
3035 } else if (t == 'L') {
3036 ch = *p++;
3037 tok = TOK_CLDOUBLE;
3038 /* XXX: not large enough */
3039 tokc.ld = (long double)d;
3040 } else {
3041 tok = TOK_CDOUBLE;
3042 tokc.d = d;
3044 } else {
3045 /* decimal floats */
3046 if (ch == '.') {
3047 if (q >= token_buf + STRING_MAX_SIZE)
3048 goto num_too_long;
3049 *q++ = ch;
3050 ch = *p++;
3051 float_frac_parse:
3052 while (ch >= '0' && ch <= '9') {
3053 if (q >= token_buf + STRING_MAX_SIZE)
3054 goto num_too_long;
3055 *q++ = ch;
3056 ch = *p++;
3059 if (ch == 'e' || ch == 'E') {
3060 if (q >= token_buf + STRING_MAX_SIZE)
3061 goto num_too_long;
3062 *q++ = ch;
3063 ch = *p++;
3064 if (ch == '-' || ch == '+') {
3065 if (q >= token_buf + STRING_MAX_SIZE)
3066 goto num_too_long;
3067 *q++ = ch;
3068 ch = *p++;
3070 if (ch < '0' || ch > '9')
3071 expect("exponent digits");
3072 while (ch >= '0' && ch <= '9') {
3073 if (q >= token_buf + STRING_MAX_SIZE)
3074 goto num_too_long;
3075 *q++ = ch;
3076 ch = *p++;
3079 *q = '\0';
3080 t = toup(ch);
3081 errno = 0;
3082 if (t == 'F') {
3083 ch = *p++;
3084 tok = TOK_CFLOAT;
3085 tokc.f = strtof(token_buf, NULL);
3086 } else if (t == 'L') {
3087 ch = *p++;
3088 tok = TOK_CLDOUBLE;
3089 tokc.ld = strtold(token_buf, NULL);
3090 } else {
3091 tok = TOK_CDOUBLE;
3092 tokc.d = strtod(token_buf, NULL);
3095 } else {
3096 unsigned long long n, n1;
3097 int lcount, ucount;
3099 /* integer number */
3100 *q = '\0';
3101 q = token_buf;
3102 if (b == 10 && *q == '0') {
3103 b = 8;
3104 q++;
3106 n = 0;
3107 while(1) {
3108 t = *q++;
3109 /* no need for checks except for base 10 / 8 errors */
3110 if (t == '\0') {
3111 break;
3112 } else if (t >= 'a') {
3113 t = t - 'a' + 10;
3114 } else if (t >= 'A') {
3115 t = t - 'A' + 10;
3116 } else {
3117 t = t - '0';
3118 if (t >= b)
3119 error("invalid digit");
3121 n1 = n;
3122 n = n * b + t;
3123 /* detect overflow */
3124 /* XXX: this test is not reliable */
3125 if (n < n1)
3126 error("integer constant overflow");
3129 /* XXX: not exactly ANSI compliant */
3130 if ((n & 0xffffffff00000000LL) != 0) {
3131 if ((n >> 63) != 0)
3132 tok = TOK_CULLONG;
3133 else
3134 tok = TOK_CLLONG;
3135 } else if (n > 0x7fffffff) {
3136 tok = TOK_CUINT;
3137 } else {
3138 tok = TOK_CINT;
3140 lcount = 0;
3141 ucount = 0;
3142 for(;;) {
3143 t = toup(ch);
3144 if (t == 'L') {
3145 if (lcount >= 2)
3146 error("three 'l's in integer constant");
3147 lcount++;
3148 if (lcount == 2) {
3149 if (tok == TOK_CINT)
3150 tok = TOK_CLLONG;
3151 else if (tok == TOK_CUINT)
3152 tok = TOK_CULLONG;
3154 ch = *p++;
3155 } else if (t == 'U') {
3156 if (ucount >= 1)
3157 error("two 'u's in integer constant");
3158 ucount++;
3159 if (tok == TOK_CINT)
3160 tok = TOK_CUINT;
3161 else if (tok == TOK_CLLONG)
3162 tok = TOK_CULLONG;
3163 ch = *p++;
3164 } else {
3165 break;
3168 if (tok == TOK_CINT || tok == TOK_CUINT)
3169 tokc.ui = n;
3170 else
3171 tokc.ull = n;
3176 #define PARSE2(c1, tok1, c2, tok2) \
3177 case c1: \
3178 PEEKC(c, p); \
3179 if (c == c2) { \
3180 p++; \
3181 tok = tok2; \
3182 } else { \
3183 tok = tok1; \
3185 break;
3187 /* return next token without macro substitution */
3188 static inline void next_nomacro1(void)
3190 int t, c, is_long;
3191 TokenSym *ts;
3192 uint8_t *p, *p1;
3193 unsigned int h;
3195 p = file->buf_ptr;
3196 redo_no_start:
3197 c = *p;
3198 switch(c) {
3199 case ' ':
3200 case '\t':
3201 case '\f':
3202 case '\v':
3203 case '\r':
3204 p++;
3205 goto redo_no_start;
3207 case '\\':
3208 /* first look if it is in fact an end of buffer */
3209 if (p >= file->buf_end) {
3210 file->buf_ptr = p;
3211 handle_eob();
3212 p = file->buf_ptr;
3213 if (p >= file->buf_end)
3214 goto parse_eof;
3215 else
3216 goto redo_no_start;
3217 } else {
3218 file->buf_ptr = p;
3219 ch = *p;
3220 handle_stray();
3221 p = file->buf_ptr;
3222 goto redo_no_start;
3224 parse_eof:
3226 TCCState *s1 = tcc_state;
3227 if (parse_flags & PARSE_FLAG_LINEFEED) {
3228 tok = TOK_LINEFEED;
3229 } else if (s1->include_stack_ptr == s1->include_stack ||
3230 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3231 /* no include left : end of file. */
3232 tok = TOK_EOF;
3233 } else {
3234 /* pop include file */
3236 /* test if previous '#endif' was after a #ifdef at
3237 start of file */
3238 if (tok_flags & TOK_FLAG_ENDIF) {
3239 #ifdef INC_DEBUG
3240 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3241 #endif
3242 add_cached_include(s1, file->inc_type, file->inc_filename,
3243 file->ifndef_macro_saved);
3246 /* add end of include file debug info */
3247 if (do_debug) {
3248 put_stabd(N_EINCL, 0, 0);
3250 /* pop include stack */
3251 tcc_close(file);
3252 s1->include_stack_ptr--;
3253 file = *s1->include_stack_ptr;
3254 p = file->buf_ptr;
3255 goto redo_no_start;
3258 break;
3260 case '\n':
3261 if (parse_flags & PARSE_FLAG_LINEFEED) {
3262 tok = TOK_LINEFEED;
3263 } else {
3264 file->line_num++;
3265 tok_flags |= TOK_FLAG_BOL;
3266 p++;
3267 goto redo_no_start;
3269 break;
3271 case '#':
3272 /* XXX: simplify */
3273 PEEKC(c, p);
3274 if ((tok_flags & TOK_FLAG_BOL) &&
3275 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3276 file->buf_ptr = p;
3277 preprocess(tok_flags & TOK_FLAG_BOF);
3278 p = file->buf_ptr;
3279 goto redo_no_start;
3280 } else {
3281 if (c == '#') {
3282 p++;
3283 tok = TOK_TWOSHARPS;
3284 } else {
3285 tok = '#';
3288 break;
3290 case 'a': case 'b': case 'c': case 'd':
3291 case 'e': case 'f': case 'g': case 'h':
3292 case 'i': case 'j': case 'k': case 'l':
3293 case 'm': case 'n': case 'o': case 'p':
3294 case 'q': case 'r': case 's': case 't':
3295 case 'u': case 'v': case 'w': case 'x':
3296 case 'y': case 'z':
3297 case 'A': case 'B': case 'C': case 'D':
3298 case 'E': case 'F': case 'G': case 'H':
3299 case 'I': case 'J': case 'K':
3300 case 'M': case 'N': case 'O': case 'P':
3301 case 'Q': case 'R': case 'S': case 'T':
3302 case 'U': case 'V': case 'W': case 'X':
3303 case 'Y': case 'Z':
3304 case '_':
3305 parse_ident_fast:
3306 p1 = p;
3307 h = TOK_HASH_INIT;
3308 h = TOK_HASH_FUNC(h, c);
3309 p++;
3310 for(;;) {
3311 c = *p;
3312 if (!isidnum_table[c])
3313 break;
3314 h = TOK_HASH_FUNC(h, c);
3315 p++;
3317 if (c != '\\') {
3318 TokenSym **pts;
3319 int len;
3321 /* fast case : no stray found, so we have the full token
3322 and we have already hashed it */
3323 len = p - p1;
3324 h &= (TOK_HASH_SIZE - 1);
3325 pts = &hash_ident[h];
3326 for(;;) {
3327 ts = *pts;
3328 if (!ts)
3329 break;
3330 if (ts->len == len && !memcmp(ts->str, p1, len))
3331 goto token_found;
3332 pts = &(ts->hash_next);
3334 ts = tok_alloc_new(pts, p1, len);
3335 token_found: ;
3336 } else {
3337 /* slower case */
3338 cstr_reset(&tokcstr);
3340 while (p1 < p) {
3341 cstr_ccat(&tokcstr, *p1);
3342 p1++;
3344 p--;
3345 PEEKC(c, p);
3346 parse_ident_slow:
3347 while (isidnum_table[c]) {
3348 cstr_ccat(&tokcstr, c);
3349 PEEKC(c, p);
3351 ts = tok_alloc(tokcstr.data, tokcstr.size);
3353 tok = ts->tok;
3354 break;
3355 case 'L':
3356 t = p[1];
3357 if (t != '\\' && t != '\'' && t != '\"') {
3358 /* fast case */
3359 goto parse_ident_fast;
3360 } else {
3361 PEEKC(c, p);
3362 if (c == '\'' || c == '\"') {
3363 is_long = 1;
3364 goto str_const;
3365 } else {
3366 cstr_reset(&tokcstr);
3367 cstr_ccat(&tokcstr, 'L');
3368 goto parse_ident_slow;
3371 break;
3372 case '0': case '1': case '2': case '3':
3373 case '4': case '5': case '6': case '7':
3374 case '8': case '9':
3376 cstr_reset(&tokcstr);
3377 /* after the first digit, accept digits, alpha, '.' or sign if
3378 prefixed by 'eEpP' */
3379 parse_num:
3380 for(;;) {
3381 t = c;
3382 cstr_ccat(&tokcstr, c);
3383 PEEKC(c, p);
3384 if (!(isnum(c) || isid(c) || c == '.' ||
3385 ((c == '+' || c == '-') &&
3386 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3387 break;
3389 /* We add a trailing '\0' to ease parsing */
3390 cstr_ccat(&tokcstr, '\0');
3391 tokc.cstr = &tokcstr;
3392 tok = TOK_PPNUM;
3393 break;
3394 case '.':
3395 /* special dot handling because it can also start a number */
3396 PEEKC(c, p);
3397 if (isnum(c)) {
3398 cstr_reset(&tokcstr);
3399 cstr_ccat(&tokcstr, '.');
3400 goto parse_num;
3401 } else if (c == '.') {
3402 PEEKC(c, p);
3403 if (c != '.')
3404 expect("'.'");
3405 PEEKC(c, p);
3406 tok = TOK_DOTS;
3407 } else {
3408 tok = '.';
3410 break;
3411 case '\'':
3412 case '\"':
3413 is_long = 0;
3414 str_const:
3416 CString str;
3417 int sep;
3419 sep = c;
3421 /* parse the string */
3422 cstr_new(&str);
3423 p = parse_pp_string(p, sep, &str);
3424 cstr_ccat(&str, '\0');
3426 /* eval the escape (should be done as TOK_PPNUM) */
3427 cstr_reset(&tokcstr);
3428 parse_escape_string(&tokcstr, str.data, is_long);
3429 cstr_free(&str);
3431 if (sep == '\'') {
3432 int char_size;
3433 /* XXX: make it portable */
3434 if (!is_long)
3435 char_size = 1;
3436 else
3437 char_size = sizeof(int);
3438 if (tokcstr.size <= char_size)
3439 error("empty character constant");
3440 if (tokcstr.size > 2 * char_size)
3441 warning("multi-character character constant");
3442 if (!is_long) {
3443 tokc.i = *(int8_t *)tokcstr.data;
3444 tok = TOK_CCHAR;
3445 } else {
3446 tokc.i = *(int *)tokcstr.data;
3447 tok = TOK_LCHAR;
3449 } else {
3450 tokc.cstr = &tokcstr;
3451 if (!is_long)
3452 tok = TOK_STR;
3453 else
3454 tok = TOK_LSTR;
3457 break;
3459 case '<':
3460 PEEKC(c, p);
3461 if (c == '=') {
3462 p++;
3463 tok = TOK_LE;
3464 } else if (c == '<') {
3465 PEEKC(c, p);
3466 if (c == '=') {
3467 p++;
3468 tok = TOK_A_SHL;
3469 } else {
3470 tok = TOK_SHL;
3472 } else {
3473 tok = TOK_LT;
3475 break;
3477 case '>':
3478 PEEKC(c, p);
3479 if (c == '=') {
3480 p++;
3481 tok = TOK_GE;
3482 } else if (c == '>') {
3483 PEEKC(c, p);
3484 if (c == '=') {
3485 p++;
3486 tok = TOK_A_SAR;
3487 } else {
3488 tok = TOK_SAR;
3490 } else {
3491 tok = TOK_GT;
3493 break;
3495 case '&':
3496 PEEKC(c, p);
3497 if (c == '&') {
3498 p++;
3499 tok = TOK_LAND;
3500 } else if (c == '=') {
3501 p++;
3502 tok = TOK_A_AND;
3503 } else {
3504 tok = '&';
3506 break;
3508 case '|':
3509 PEEKC(c, p);
3510 if (c == '|') {
3511 p++;
3512 tok = TOK_LOR;
3513 } else if (c == '=') {
3514 p++;
3515 tok = TOK_A_OR;
3516 } else {
3517 tok = '|';
3519 break;
3521 case '+':
3522 PEEKC(c, p);
3523 if (c == '+') {
3524 p++;
3525 tok = TOK_INC;
3526 } else if (c == '=') {
3527 p++;
3528 tok = TOK_A_ADD;
3529 } else {
3530 tok = '+';
3532 break;
3534 case '-':
3535 PEEKC(c, p);
3536 if (c == '-') {
3537 p++;
3538 tok = TOK_DEC;
3539 } else if (c == '=') {
3540 p++;
3541 tok = TOK_A_SUB;
3542 } else if (c == '>') {
3543 p++;
3544 tok = TOK_ARROW;
3545 } else {
3546 tok = '-';
3548 break;
3550 PARSE2('!', '!', '=', TOK_NE)
3551 PARSE2('=', '=', '=', TOK_EQ)
3552 PARSE2('*', '*', '=', TOK_A_MUL)
3553 PARSE2('%', '%', '=', TOK_A_MOD)
3554 PARSE2('^', '^', '=', TOK_A_XOR)
3556 /* comments or operator */
3557 case '/':
3558 PEEKC(c, p);
3559 if (c == '*') {
3560 p = parse_comment(p);
3561 goto redo_no_start;
3562 } else if (c == '/') {
3563 p = parse_line_comment(p);
3564 goto redo_no_start;
3565 } else if (c == '=') {
3566 p++;
3567 tok = TOK_A_DIV;
3568 } else {
3569 tok = '/';
3571 break;
3573 /* simple tokens */
3574 case '(':
3575 case ')':
3576 case '[':
3577 case ']':
3578 case '{':
3579 case '}':
3580 case ',':
3581 case ';':
3582 case ':':
3583 case '?':
3584 case '~':
3585 case '$': /* only used in assembler */
3586 tok = c;
3587 p++;
3588 break;
3589 default:
3590 error("unrecognized character \\x%02x", c);
3591 break;
3593 file->buf_ptr = p;
3594 tok_flags = 0;
3595 #if defined(PARSE_DEBUG)
3596 printf("token = %s\n", get_tok_str(tok, &tokc));
3597 #endif
3600 /* return next token without macro substitution. Can read input from
3601 macro_ptr buffer */
3602 static void next_nomacro(void)
3604 if (macro_ptr) {
3605 redo:
3606 tok = *macro_ptr;
3607 if (tok) {
3608 TOK_GET(tok, macro_ptr, tokc);
3609 if (tok == TOK_LINENUM) {
3610 file->line_num = tokc.i;
3611 goto redo;
3614 } else {
3615 next_nomacro1();
3619 /* substitute args in macro_str and return allocated string */
3620 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3622 int *st, last_tok, t, notfirst;
3623 Sym *s;
3624 CValue cval;
3625 TokenString str;
3626 CString cstr;
3628 tok_str_new(&str);
3629 last_tok = 0;
3630 while(1) {
3631 TOK_GET(t, macro_str, cval);
3632 if (!t)
3633 break;
3634 if (t == '#') {
3635 /* stringize */
3636 TOK_GET(t, macro_str, cval);
3637 if (!t)
3638 break;
3639 s = sym_find2(args, t);
3640 if (s) {
3641 cstr_new(&cstr);
3642 st = (int *)s->c;
3643 notfirst = 0;
3644 while (*st) {
3645 if (notfirst)
3646 cstr_ccat(&cstr, ' ');
3647 TOK_GET(t, st, cval);
3648 cstr_cat(&cstr, get_tok_str(t, &cval));
3649 notfirst = 1;
3651 cstr_ccat(&cstr, '\0');
3652 #ifdef PP_DEBUG
3653 printf("stringize: %s\n", (char *)cstr.data);
3654 #endif
3655 /* add string */
3656 cval.cstr = &cstr;
3657 tok_str_add2(&str, TOK_STR, &cval);
3658 cstr_free(&cstr);
3659 } else {
3660 tok_str_add2(&str, t, &cval);
3662 } else if (t >= TOK_IDENT) {
3663 s = sym_find2(args, t);
3664 if (s) {
3665 st = (int *)s->c;
3666 /* if '##' is present before or after, no arg substitution */
3667 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3668 /* special case for var arg macros : ## eats the
3669 ',' if empty VA_ARGS variable. */
3670 /* XXX: test of the ',' is not 100%
3671 reliable. should fix it to avoid security
3672 problems */
3673 if (gnu_ext && s->type.t &&
3674 last_tok == TOK_TWOSHARPS &&
3675 str.len >= 2 && str.str[str.len - 2] == ',') {
3676 if (*st == 0) {
3677 /* suppress ',' '##' */
3678 str.len -= 2;
3679 } else {
3680 /* suppress '##' and add variable */
3681 str.len--;
3682 goto add_var;
3684 } else {
3685 int t1;
3686 add_var:
3687 for(;;) {
3688 TOK_GET(t1, st, cval);
3689 if (!t1)
3690 break;
3691 tok_str_add2(&str, t1, &cval);
3694 } else {
3695 macro_subst(&str, nested_list, st);
3697 } else {
3698 tok_str_add(&str, t);
3700 } else {
3701 tok_str_add2(&str, t, &cval);
3703 last_tok = t;
3705 tok_str_add(&str, 0);
3706 return str.str;
3709 static char const ab_month_name[12][4] =
3711 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3712 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3715 /* do macro substitution of current token with macro 's' and add
3716 result to (tok_str,tok_len). 'nested_list' is the list of all
3717 macros we got inside to avoid recursing. Return non zero if no
3718 substitution needs to be done */
3719 static int macro_subst_tok(TokenString *tok_str,
3720 Sym **nested_list, Sym *s)
3722 Sym *args, *sa, *sa1;
3723 int mstr_allocated, parlevel, *mstr, t;
3724 TokenString str;
3725 char *cstrval;
3726 CValue cval;
3727 CString cstr;
3729 /* if symbol is a macro, prepare substitution */
3731 /* special macros */
3732 if (tok == TOK___LINE__) {
3733 cval.i = file->line_num;
3734 tok_str_add2(tok_str, TOK_CINT, &cval);
3735 } else if (tok == TOK___FILE__) {
3736 cstrval = file->filename;
3737 goto add_cstr;
3738 tok_str_add2(tok_str, TOK_STR, &cval);
3739 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3740 time_t ti;
3741 struct tm *tm;
3742 char buf[64];
3744 time(&ti);
3745 tm = localtime(&ti);
3746 if (tok == TOK___DATE__) {
3747 snprintf(buf, sizeof(buf), "%s %2d %d",
3748 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3749 } else {
3750 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3751 tm->tm_hour, tm->tm_min, tm->tm_sec);
3753 cstrval = buf;
3754 add_cstr:
3755 cstr_new(&cstr);
3756 cstr_cat(&cstr, cstrval);
3757 cstr_ccat(&cstr, '\0');
3758 cval.cstr = &cstr;
3759 tok_str_add2(tok_str, TOK_STR, &cval);
3760 cstr_free(&cstr);
3761 } else {
3762 mstr = (int *)s->c;
3763 mstr_allocated = 0;
3764 if (s->type.t == MACRO_FUNC) {
3765 /* NOTE: we do not use next_nomacro to avoid eating the
3766 next token. XXX: find better solution */
3767 if (macro_ptr) {
3768 t = *macro_ptr;
3769 if (t == 0) {
3770 /* end of macro stream: we must look at the token
3771 after in the file */
3772 macro_ptr = NULL;
3773 goto parse_stream;
3775 } else {
3776 parse_stream:
3777 /* XXX: incorrect with comments */
3778 ch = file->buf_ptr[0];
3779 while (is_space(ch) || ch == '\n')
3780 cinp();
3781 t = ch;
3783 if (t != '(') /* no macro subst */
3784 return -1;
3786 /* argument macro */
3787 next_nomacro();
3788 next_nomacro();
3789 args = NULL;
3790 sa = s->next;
3791 /* NOTE: empty args are allowed, except if no args */
3792 for(;;) {
3793 /* handle '()' case */
3794 if (!args && tok == ')')
3795 break;
3796 if (!sa)
3797 error("macro '%s' used with too many args",
3798 get_tok_str(s->v, 0));
3799 tok_str_new(&str);
3800 parlevel = 0;
3801 /* NOTE: non zero sa->t indicates VA_ARGS */
3802 while ((parlevel > 0 ||
3803 (tok != ')' &&
3804 (tok != ',' || sa->type.t))) &&
3805 tok != -1) {
3806 if (tok == '(')
3807 parlevel++;
3808 else if (tok == ')')
3809 parlevel--;
3810 tok_str_add2(&str, tok, &tokc);
3811 next_nomacro();
3813 tok_str_add(&str, 0);
3814 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
3815 sa = sa->next;
3816 if (tok == ')') {
3817 /* special case for gcc var args: add an empty
3818 var arg argument if it is omitted */
3819 if (sa && sa->type.t && gnu_ext)
3820 continue;
3821 else
3822 break;
3824 if (tok != ',')
3825 expect(",");
3826 next_nomacro();
3828 if (sa) {
3829 error("macro '%s' used with too few args",
3830 get_tok_str(s->v, 0));
3833 /* now subst each arg */
3834 mstr = macro_arg_subst(nested_list, mstr, args);
3835 /* free memory */
3836 sa = args;
3837 while (sa) {
3838 sa1 = sa->prev;
3839 tok_str_free((int *)sa->c);
3840 tcc_free(sa);
3841 sa = sa1;
3843 mstr_allocated = 1;
3845 sym_push2(nested_list, s->v, 0, 0);
3846 macro_subst(tok_str, nested_list, mstr);
3847 /* pop nested defined symbol */
3848 sa1 = *nested_list;
3849 *nested_list = sa1->prev;
3850 tcc_free(sa1);
3851 if (mstr_allocated)
3852 tok_str_free(mstr);
3854 return 0;
3857 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3858 return the resulting string (which must be freed). */
3859 static inline int *macro_twosharps(const int *macro_str)
3861 TokenSym *ts;
3862 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
3863 int t;
3864 const char *p1, *p2;
3865 CValue cval;
3866 TokenString macro_str1;
3867 CString cstr;
3869 start_macro_ptr = macro_str;
3870 /* we search the first '##' */
3871 for(;;) {
3872 macro_ptr1 = macro_str;
3873 TOK_GET(t, macro_str, cval);
3874 /* nothing more to do if end of string */
3875 if (t == 0)
3876 return NULL;
3877 if (*macro_str == TOK_TWOSHARPS)
3878 break;
3881 /* we saw '##', so we need more processing to handle it */
3882 cstr_new(&cstr);
3883 tok_str_new(&macro_str1);
3884 tok = t;
3885 tokc = cval;
3887 /* add all tokens seen so far */
3888 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
3889 TOK_GET(t, ptr, cval);
3890 tok_str_add2(&macro_str1, t, &cval);
3892 saved_macro_ptr = macro_ptr;
3893 /* XXX: get rid of the use of macro_ptr here */
3894 macro_ptr = (int *)macro_str;
3895 for(;;) {
3896 while (*macro_ptr == TOK_TWOSHARPS) {
3897 macro_ptr++;
3898 macro_ptr1 = macro_ptr;
3899 t = *macro_ptr;
3900 if (t) {
3901 TOK_GET(t, macro_ptr, cval);
3902 /* We concatenate the two tokens if we have an
3903 identifier or a preprocessing number */
3904 cstr_reset(&cstr);
3905 p1 = get_tok_str(tok, &tokc);
3906 cstr_cat(&cstr, p1);
3907 p2 = get_tok_str(t, &cval);
3908 cstr_cat(&cstr, p2);
3909 cstr_ccat(&cstr, '\0');
3911 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
3912 (t >= TOK_IDENT || t == TOK_PPNUM)) {
3913 if (tok == TOK_PPNUM) {
3914 /* if number, then create a number token */
3915 /* NOTE: no need to allocate because
3916 tok_str_add2() does it */
3917 tokc.cstr = &cstr;
3918 } else {
3919 /* if identifier, we must do a test to
3920 validate we have a correct identifier */
3921 if (t == TOK_PPNUM) {
3922 const char *p;
3923 int c;
3925 p = p2;
3926 for(;;) {
3927 c = *p;
3928 if (c == '\0')
3929 break;
3930 p++;
3931 if (!isnum(c) && !isid(c))
3932 goto error_pasting;
3935 ts = tok_alloc(cstr.data, strlen(cstr.data));
3936 tok = ts->tok; /* modify current token */
3938 } else {
3939 const char *str = cstr.data;
3940 const unsigned char *q;
3942 /* we look for a valid token */
3943 /* XXX: do more extensive checks */
3944 if (!strcmp(str, ">>=")) {
3945 tok = TOK_A_SAR;
3946 } else if (!strcmp(str, "<<=")) {
3947 tok = TOK_A_SHL;
3948 } else if (strlen(str) == 2) {
3949 /* search in two bytes table */
3950 q = tok_two_chars;
3951 for(;;) {
3952 if (!*q)
3953 goto error_pasting;
3954 if (q[0] == str[0] && q[1] == str[1])
3955 break;
3956 q += 3;
3958 tok = q[2];
3959 } else {
3960 error_pasting:
3961 /* NOTE: because get_tok_str use a static buffer,
3962 we must save it */
3963 cstr_reset(&cstr);
3964 p1 = get_tok_str(tok, &tokc);
3965 cstr_cat(&cstr, p1);
3966 cstr_ccat(&cstr, '\0');
3967 p2 = get_tok_str(t, &cval);
3968 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
3969 /* cannot merge tokens: just add them separately */
3970 tok_str_add2(&macro_str1, tok, &tokc);
3971 /* XXX: free associated memory ? */
3972 tok = t;
3973 tokc = cval;
3978 tok_str_add2(&macro_str1, tok, &tokc);
3979 next_nomacro();
3980 if (tok == 0)
3981 break;
3983 macro_ptr = (int *)saved_macro_ptr;
3984 cstr_free(&cstr);
3985 tok_str_add(&macro_str1, 0);
3986 return macro_str1.str;
3990 /* do macro substitution of macro_str and add result to
3991 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3992 inside to avoid recursing. */
3993 static void macro_subst(TokenString *tok_str,
3994 Sym **nested_list, const int *macro_str)
3996 Sym *s;
3997 int *saved_macro_ptr, *macro_str1;
3998 const int *ptr;
3999 int t, ret;
4000 CValue cval;
4002 /* first scan for '##' operator handling */
4003 ptr = macro_str;
4004 macro_str1 = macro_twosharps(ptr);
4005 if (macro_str1)
4006 ptr = macro_str1;
4007 while (1) {
4008 /* NOTE: ptr == NULL can only happen if tokens are read from
4009 file stream due to a macro function call */
4010 if (ptr == NULL)
4011 break;
4012 TOK_GET(t, ptr, cval);
4013 if (t == 0)
4014 break;
4015 s = define_find(t);
4016 if (s != NULL) {
4017 /* if nested substitution, do nothing */
4018 if (sym_find2(*nested_list, t))
4019 goto no_subst;
4020 saved_macro_ptr = macro_ptr;
4021 macro_ptr = (int *)ptr;
4022 tok = t;
4023 ret = macro_subst_tok(tok_str, nested_list, s);
4024 ptr = (int *)macro_ptr;
4025 macro_ptr = saved_macro_ptr;
4026 if (ret != 0)
4027 goto no_subst;
4028 } else {
4029 no_subst:
4030 tok_str_add2(tok_str, t, &cval);
4033 if (macro_str1)
4034 tok_str_free(macro_str1);
4037 /* return next token with macro substitution */
4038 static void next(void)
4040 Sym *nested_list, *s;
4041 TokenString str;
4043 redo:
4044 next_nomacro();
4045 if (!macro_ptr) {
4046 /* if not reading from macro substituted string, then try
4047 to substitute macros */
4048 if (tok >= TOK_IDENT &&
4049 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4050 s = define_find(tok);
4051 if (s) {
4052 /* we have a macro: we try to substitute */
4053 tok_str_new(&str);
4054 nested_list = NULL;
4055 if (macro_subst_tok(&str, &nested_list, s) == 0) {
4056 /* substitution done, NOTE: maybe empty */
4057 tok_str_add(&str, 0);
4058 macro_ptr = str.str;
4059 macro_ptr_allocated = str.str;
4060 goto redo;
4064 } else {
4065 if (tok == 0) {
4066 /* end of macro or end of unget buffer */
4067 if (unget_buffer_enabled) {
4068 macro_ptr = unget_saved_macro_ptr;
4069 unget_buffer_enabled = 0;
4070 } else {
4071 /* end of macro string: free it */
4072 tok_str_free(macro_ptr_allocated);
4073 macro_ptr = NULL;
4075 goto redo;
4079 /* convert preprocessor tokens into C tokens */
4080 if (tok == TOK_PPNUM &&
4081 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4082 parse_number((char *)tokc.cstr->data);
4086 /* push back current token and set current token to 'last_tok'. Only
4087 identifier case handled for labels. */
4088 static inline void unget_tok(int last_tok)
4090 int i, n;
4091 int *q;
4092 unget_saved_macro_ptr = macro_ptr;
4093 unget_buffer_enabled = 1;
4094 q = unget_saved_buffer;
4095 macro_ptr = q;
4096 *q++ = tok;
4097 n = tok_ext_size(tok) - 1;
4098 for(i=0;i<n;i++)
4099 *q++ = tokc.tab[i];
4100 *q = 0; /* end of token string */
4101 tok = last_tok;
4105 void swap(int *p, int *q)
4107 int t;
4108 t = *p;
4109 *p = *q;
4110 *q = t;
4113 void vsetc(CType *type, int r, CValue *vc)
4115 int v;
4117 if (vtop >= vstack + VSTACK_SIZE)
4118 error("memory full");
4119 /* cannot let cpu flags if other instruction are generated. Also
4120 avoid leaving VT_JMP anywhere except on the top of the stack
4121 because it would complicate the code generator. */
4122 if (vtop >= vstack) {
4123 v = vtop->r & VT_VALMASK;
4124 if (v == VT_CMP || (v & ~1) == VT_JMP)
4125 gv(RC_INT);
4127 vtop++;
4128 vtop->type = *type;
4129 vtop->r = r;
4130 vtop->r2 = VT_CONST;
4131 vtop->c = *vc;
4134 /* push integer constant */
4135 void vpushi(int v)
4137 CValue cval;
4138 cval.i = v;
4139 vsetc(&int_type, VT_CONST, &cval);
4142 /* Return a static symbol pointing to a section */
4143 static Sym *get_sym_ref(CType *type, Section *sec,
4144 unsigned long offset, unsigned long size)
4146 int v;
4147 Sym *sym;
4149 v = anon_sym++;
4150 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4151 sym->type.ref = type->ref;
4152 sym->r = VT_CONST | VT_SYM;
4153 put_extern_sym(sym, sec, offset, size);
4154 return sym;
4157 /* push a reference to a section offset by adding a dummy symbol */
4158 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4160 CValue cval;
4162 cval.ul = 0;
4163 vsetc(type, VT_CONST | VT_SYM, &cval);
4164 vtop->sym = get_sym_ref(type, sec, offset, size);
4167 /* define a new external reference to a symbol 'v' of type 'u' */
4168 static Sym *external_global_sym(int v, CType *type, int r)
4170 Sym *s;
4172 s = sym_find(v);
4173 if (!s) {
4174 /* push forward reference */
4175 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4176 s->type.ref = type->ref;
4177 s->r = r | VT_CONST | VT_SYM;
4179 return s;
4182 /* define a new external reference to a symbol 'v' of type 'u' */
4183 static Sym *external_sym(int v, CType *type, int r)
4185 Sym *s;
4187 s = sym_find(v);
4188 if (!s) {
4189 /* push forward reference */
4190 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4191 s->type.t |= VT_EXTERN;
4192 } else {
4193 if (!is_compatible_types(&s->type, type))
4194 error("incompatible types for redefinition of '%s'",
4195 get_tok_str(v, NULL));
4197 return s;
4200 /* push a reference to global symbol v */
4201 static void vpush_global_sym(CType *type, int v)
4203 Sym *sym;
4204 CValue cval;
4206 sym = external_global_sym(v, type, 0);
4207 cval.ul = 0;
4208 vsetc(type, VT_CONST | VT_SYM, &cval);
4209 vtop->sym = sym;
4212 void vset(CType *type, int r, int v)
4214 CValue cval;
4216 cval.i = v;
4217 vsetc(type, r, &cval);
4220 void vseti(int r, int v)
4222 CType type;
4223 type.t = VT_INT;
4224 vset(&type, r, v);
4227 void vswap(void)
4229 SValue tmp;
4231 tmp = vtop[0];
4232 vtop[0] = vtop[-1];
4233 vtop[-1] = tmp;
4236 void vpushv(SValue *v)
4238 if (vtop >= vstack + VSTACK_SIZE)
4239 error("memory full");
4240 vtop++;
4241 *vtop = *v;
4244 void vdup(void)
4246 vpushv(vtop);
4249 /* save r to the memory stack, and mark it as being free */
4250 void save_reg(int r)
4252 int l, saved, size, align;
4253 SValue *p, sv;
4254 CType *type;
4256 /* modify all stack values */
4257 saved = 0;
4258 l = 0;
4259 for(p=vstack;p<=vtop;p++) {
4260 if ((p->r & VT_VALMASK) == r ||
4261 (p->r2 & VT_VALMASK) == r) {
4262 /* must save value on stack if not already done */
4263 if (!saved) {
4264 /* NOTE: must reload 'r' because r might be equal to r2 */
4265 r = p->r & VT_VALMASK;
4266 /* store register in the stack */
4267 type = &p->type;
4268 if ((p->r & VT_LVAL) ||
4269 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4270 type = &int_type;
4271 size = type_size(type, &align);
4272 loc = (loc - size) & -align;
4273 sv.type.t = type->t;
4274 sv.r = VT_LOCAL | VT_LVAL;
4275 sv.c.ul = loc;
4276 store(r, &sv);
4277 #ifdef TCC_TARGET_I386
4278 /* x86 specific: need to pop fp register ST0 if saved */
4279 if (r == TREG_ST0) {
4280 o(0xd9dd); /* fstp %st(1) */
4282 #endif
4283 /* special long long case */
4284 if ((type->t & VT_BTYPE) == VT_LLONG) {
4285 sv.c.ul += 4;
4286 store(p->r2, &sv);
4288 l = loc;
4289 saved = 1;
4291 /* mark that stack entry as being saved on the stack */
4292 if (p->r & VT_LVAL) {
4293 /* also clear the bounded flag because the
4294 relocation address of the function was stored in
4295 p->c.ul */
4296 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4297 } else {
4298 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4300 p->r2 = VT_CONST;
4301 p->c.ul = l;
4306 /* find a free register of class 'rc'. If none, save one register */
4307 int get_reg(int rc)
4309 int r;
4310 SValue *p;
4312 /* find a free register */
4313 for(r=0;r<NB_REGS;r++) {
4314 if (reg_classes[r] & rc) {
4315 for(p=vstack;p<=vtop;p++) {
4316 if ((p->r & VT_VALMASK) == r ||
4317 (p->r2 & VT_VALMASK) == r)
4318 goto notfound;
4320 return r;
4322 notfound: ;
4325 /* no register left : free the first one on the stack (VERY
4326 IMPORTANT to start from the bottom to ensure that we don't
4327 spill registers used in gen_opi()) */
4328 for(p=vstack;p<=vtop;p++) {
4329 r = p->r & VT_VALMASK;
4330 if (r < VT_CONST && (reg_classes[r] & rc))
4331 goto save_found;
4332 /* also look at second register (if long long) */
4333 r = p->r2 & VT_VALMASK;
4334 if (r < VT_CONST && (reg_classes[r] & rc)) {
4335 save_found:
4336 save_reg(r);
4337 return r;
4340 /* Should never comes here */
4341 return -1;
4344 /* save registers up to (vtop - n) stack entry */
4345 void save_regs(int n)
4347 int r;
4348 SValue *p, *p1;
4349 p1 = vtop - n;
4350 for(p = vstack;p <= p1; p++) {
4351 r = p->r & VT_VALMASK;
4352 if (r < VT_CONST) {
4353 save_reg(r);
4358 /* move register 's' to 'r', and flush previous value of r to memory
4359 if needed */
4360 void move_reg(int r, int s)
4362 SValue sv;
4364 if (r != s) {
4365 save_reg(r);
4366 sv.type.t = VT_INT;
4367 sv.r = s;
4368 sv.c.ul = 0;
4369 load(r, &sv);
4373 /* get address of vtop (vtop MUST BE an lvalue) */
4374 void gaddrof(void)
4376 vtop->r &= ~VT_LVAL;
4377 /* tricky: if saved lvalue, then we can go back to lvalue */
4378 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4379 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4382 #ifdef CONFIG_TCC_BCHECK
4383 /* generate lvalue bound code */
4384 void gbound(void)
4386 int lval_type;
4387 CType type1;
4389 vtop->r &= ~VT_MUSTBOUND;
4390 /* if lvalue, then use checking code before dereferencing */
4391 if (vtop->r & VT_LVAL) {
4392 /* if not VT_BOUNDED value, then make one */
4393 if (!(vtop->r & VT_BOUNDED)) {
4394 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4395 /* must save type because we must set it to int to get pointer */
4396 type1 = vtop->type;
4397 vtop->type.t = VT_INT;
4398 gaddrof();
4399 vpushi(0);
4400 gen_bounded_ptr_add();
4401 vtop->r |= lval_type;
4402 vtop->type = type1;
4404 /* then check for dereferencing */
4405 gen_bounded_ptr_deref();
4408 #endif
4410 /* store vtop a register belonging to class 'rc'. lvalues are
4411 converted to values. Cannot be used if cannot be converted to
4412 register value (such as structures). */
4413 int gv(int rc)
4415 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4416 unsigned long long ll;
4418 /* NOTE: get_reg can modify vstack[] */
4419 if (vtop->type.t & VT_BITFIELD) {
4420 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4421 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4422 /* remove bit field info to avoid loops */
4423 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4424 /* generate shifts */
4425 vpushi(32 - (bit_pos + bit_size));
4426 gen_op(TOK_SHL);
4427 vpushi(32 - bit_size);
4428 /* NOTE: transformed to SHR if unsigned */
4429 gen_op(TOK_SAR);
4430 r = gv(rc);
4431 } else {
4432 if (is_float(vtop->type.t) &&
4433 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4434 Sym *sym;
4435 int *ptr;
4436 unsigned long offset;
4438 /* XXX: unify with initializers handling ? */
4439 /* CPUs usually cannot use float constants, so we store them
4440 generically in data segment */
4441 size = type_size(&vtop->type, &align);
4442 offset = (data_section->data_offset + align - 1) & -align;
4443 data_section->data_offset = offset;
4444 /* XXX: not portable yet */
4445 ptr = section_ptr_add(data_section, size);
4446 size = size >> 2;
4447 for(i=0;i<size;i++)
4448 ptr[i] = vtop->c.tab[i];
4449 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4450 vtop->r |= VT_LVAL | VT_SYM;
4451 vtop->sym = sym;
4452 vtop->c.ul = 0;
4454 #ifdef CONFIG_TCC_BCHECK
4455 if (vtop->r & VT_MUSTBOUND)
4456 gbound();
4457 #endif
4459 r = vtop->r & VT_VALMASK;
4460 /* need to reload if:
4461 - constant
4462 - lvalue (need to dereference pointer)
4463 - already a register, but not in the right class */
4464 if (r >= VT_CONST ||
4465 (vtop->r & VT_LVAL) ||
4466 !(reg_classes[r] & rc) ||
4467 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4468 !(reg_classes[vtop->r2] & rc))) {
4469 r = get_reg(rc);
4470 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4471 /* two register type load : expand to two words
4472 temporarily */
4473 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4474 /* load constant */
4475 ll = vtop->c.ull;
4476 vtop->c.ui = ll; /* first word */
4477 load(r, vtop);
4478 vtop->r = r; /* save register value */
4479 vpushi(ll >> 32); /* second word */
4480 } else if (r >= VT_CONST ||
4481 (vtop->r & VT_LVAL)) {
4482 /* load from memory */
4483 load(r, vtop);
4484 vdup();
4485 vtop[-1].r = r; /* save register value */
4486 /* increment pointer to get second word */
4487 vtop->type.t = VT_INT;
4488 gaddrof();
4489 vpushi(4);
4490 gen_op('+');
4491 vtop->r |= VT_LVAL;
4492 } else {
4493 /* move registers */
4494 load(r, vtop);
4495 vdup();
4496 vtop[-1].r = r; /* save register value */
4497 vtop->r = vtop[-1].r2;
4499 /* allocate second register */
4500 rc2 = RC_INT;
4501 if (rc == RC_IRET)
4502 rc2 = RC_LRET;
4503 r2 = get_reg(rc2);
4504 load(r2, vtop);
4505 vpop();
4506 /* write second register */
4507 vtop->r2 = r2;
4508 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4509 int t1, t;
4510 /* lvalue of scalar type : need to use lvalue type
4511 because of possible cast */
4512 t = vtop->type.t;
4513 t1 = t;
4514 /* compute memory access type */
4515 if (vtop->r & VT_LVAL_BYTE)
4516 t = VT_BYTE;
4517 else if (vtop->r & VT_LVAL_SHORT)
4518 t = VT_SHORT;
4519 if (vtop->r & VT_LVAL_UNSIGNED)
4520 t |= VT_UNSIGNED;
4521 vtop->type.t = t;
4522 load(r, vtop);
4523 /* restore wanted type */
4524 vtop->type.t = t1;
4525 } else {
4526 /* one register type load */
4527 load(r, vtop);
4530 vtop->r = r;
4532 return r;
4535 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4536 void gv2(int rc1, int rc2)
4538 int v;
4540 /* generate more generic register first. But VT_JMP or VT_CMP
4541 values must be generated first in all cases to avoid possible
4542 reload errors */
4543 v = vtop[0].r & VT_VALMASK;
4544 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4545 vswap();
4546 gv(rc1);
4547 vswap();
4548 gv(rc2);
4549 /* test if reload is needed for first register */
4550 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4551 vswap();
4552 gv(rc1);
4553 vswap();
4555 } else {
4556 gv(rc2);
4557 vswap();
4558 gv(rc1);
4559 vswap();
4560 /* test if reload is needed for first register */
4561 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4562 gv(rc2);
4567 /* expand long long on stack in two int registers */
4568 void lexpand(void)
4570 int u;
4572 u = vtop->type.t & VT_UNSIGNED;
4573 gv(RC_INT);
4574 vdup();
4575 vtop[0].r = vtop[-1].r2;
4576 vtop[0].r2 = VT_CONST;
4577 vtop[-1].r2 = VT_CONST;
4578 vtop[0].type.t = VT_INT | u;
4579 vtop[-1].type.t = VT_INT | u;
4582 /* build a long long from two ints */
4583 void lbuild(int t)
4585 gv2(RC_INT, RC_INT);
4586 vtop[-1].r2 = vtop[0].r;
4587 vtop[-1].type.t = t;
4588 vpop();
4591 /* rotate n first stack elements to the bottom */
4592 void vrotb(int n)
4594 int i;
4595 SValue tmp;
4597 tmp = vtop[-n + 1];
4598 for(i=-n+1;i!=0;i++)
4599 vtop[i] = vtop[i+1];
4600 vtop[0] = tmp;
4603 /* pop stack value */
4604 void vpop(void)
4606 int v;
4607 v = vtop->r & VT_VALMASK;
4608 #ifdef TCC_TARGET_I386
4609 /* for x86, we need to pop the FP stack */
4610 if (v == TREG_ST0 && !nocode_wanted) {
4611 o(0xd9dd); /* fstp %st(1) */
4612 } else
4613 #endif
4614 if (v == VT_JMP || v == VT_JMPI) {
4615 /* need to put correct jump if && or || without test */
4616 gsym(vtop->c.ul);
4618 vtop--;
4621 /* convert stack entry to register and duplicate its value in another
4622 register */
4623 void gv_dup(void)
4625 int rc, t, r, r1;
4626 SValue sv;
4628 t = vtop->type.t;
4629 if ((t & VT_BTYPE) == VT_LLONG) {
4630 lexpand();
4631 gv_dup();
4632 vswap();
4633 vrotb(3);
4634 gv_dup();
4635 vrotb(4);
4636 /* stack: H L L1 H1 */
4637 lbuild(t);
4638 vrotb(3);
4639 vrotb(3);
4640 vswap();
4641 lbuild(t);
4642 vswap();
4643 } else {
4644 /* duplicate value */
4645 rc = RC_INT;
4646 sv.type.t = VT_INT;
4647 if (is_float(t)) {
4648 rc = RC_FLOAT;
4649 sv.type.t = t;
4651 r = gv(rc);
4652 r1 = get_reg(rc);
4653 sv.r = r;
4654 sv.c.ul = 0;
4655 load(r1, &sv); /* move r to r1 */
4656 vdup();
4657 /* duplicates value */
4658 vtop->r = r1;
4662 /* generate CPU independent (unsigned) long long operations */
4663 void gen_opl(int op)
4665 int t, a, b, op1, c, i;
4666 int func;
4667 GFuncContext gf;
4668 SValue tmp;
4670 switch(op) {
4671 case '/':
4672 case TOK_PDIV:
4673 func = TOK___divdi3;
4674 goto gen_func;
4675 case TOK_UDIV:
4676 func = TOK___udivdi3;
4677 goto gen_func;
4678 case '%':
4679 func = TOK___moddi3;
4680 goto gen_func;
4681 case TOK_UMOD:
4682 func = TOK___umoddi3;
4683 gen_func:
4684 /* call generic long long function */
4685 gfunc_start(&gf, FUNC_CDECL);
4686 gfunc_param(&gf);
4687 gfunc_param(&gf);
4688 vpush_global_sym(&func_old_type, func);
4689 gfunc_call(&gf);
4690 vpushi(0);
4691 vtop->r = REG_IRET;
4692 vtop->r2 = REG_LRET;
4693 break;
4694 case '^':
4695 case '&':
4696 case '|':
4697 case '*':
4698 case '+':
4699 case '-':
4700 t = vtop->type.t;
4701 vswap();
4702 lexpand();
4703 vrotb(3);
4704 lexpand();
4705 /* stack: L1 H1 L2 H2 */
4706 tmp = vtop[0];
4707 vtop[0] = vtop[-3];
4708 vtop[-3] = tmp;
4709 tmp = vtop[-2];
4710 vtop[-2] = vtop[-3];
4711 vtop[-3] = tmp;
4712 vswap();
4713 /* stack: H1 H2 L1 L2 */
4714 if (op == '*') {
4715 vpushv(vtop - 1);
4716 vpushv(vtop - 1);
4717 gen_op(TOK_UMULL);
4718 lexpand();
4719 /* stack: H1 H2 L1 L2 ML MH */
4720 for(i=0;i<4;i++)
4721 vrotb(6);
4722 /* stack: ML MH H1 H2 L1 L2 */
4723 tmp = vtop[0];
4724 vtop[0] = vtop[-2];
4725 vtop[-2] = tmp;
4726 /* stack: ML MH H1 L2 H2 L1 */
4727 gen_op('*');
4728 vrotb(3);
4729 vrotb(3);
4730 gen_op('*');
4731 /* stack: ML MH M1 M2 */
4732 gen_op('+');
4733 gen_op('+');
4734 } else if (op == '+' || op == '-') {
4735 /* XXX: add non carry method too (for MIPS or alpha) */
4736 if (op == '+')
4737 op1 = TOK_ADDC1;
4738 else
4739 op1 = TOK_SUBC1;
4740 gen_op(op1);
4741 /* stack: H1 H2 (L1 op L2) */
4742 vrotb(3);
4743 vrotb(3);
4744 gen_op(op1 + 1); /* TOK_xxxC2 */
4745 } else {
4746 gen_op(op);
4747 /* stack: H1 H2 (L1 op L2) */
4748 vrotb(3);
4749 vrotb(3);
4750 /* stack: (L1 op L2) H1 H2 */
4751 gen_op(op);
4752 /* stack: (L1 op L2) (H1 op H2) */
4754 /* stack: L H */
4755 lbuild(t);
4756 break;
4757 case TOK_SAR:
4758 case TOK_SHR:
4759 case TOK_SHL:
4760 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4761 t = vtop[-1].type.t;
4762 vswap();
4763 lexpand();
4764 vrotb(3);
4765 /* stack: L H shift */
4766 c = (int)vtop->c.i;
4767 /* constant: simpler */
4768 /* NOTE: all comments are for SHL. the other cases are
4769 done by swaping words */
4770 vpop();
4771 if (op != TOK_SHL)
4772 vswap();
4773 if (c >= 32) {
4774 /* stack: L H */
4775 vpop();
4776 if (c > 32) {
4777 vpushi(c - 32);
4778 gen_op(op);
4780 if (op != TOK_SAR) {
4781 vpushi(0);
4782 } else {
4783 gv_dup();
4784 vpushi(31);
4785 gen_op(TOK_SAR);
4787 vswap();
4788 } else {
4789 vswap();
4790 gv_dup();
4791 /* stack: H L L */
4792 vpushi(c);
4793 gen_op(op);
4794 vswap();
4795 vpushi(32 - c);
4796 if (op == TOK_SHL)
4797 gen_op(TOK_SHR);
4798 else
4799 gen_op(TOK_SHL);
4800 vrotb(3);
4801 /* stack: L L H */
4802 vpushi(c);
4803 if (op == TOK_SHL)
4804 gen_op(TOK_SHL);
4805 else
4806 gen_op(TOK_SHR);
4807 gen_op('|');
4809 if (op != TOK_SHL)
4810 vswap();
4811 lbuild(t);
4812 } else {
4813 /* XXX: should provide a faster fallback on x86 ? */
4814 switch(op) {
4815 case TOK_SAR:
4816 func = TOK___sardi3;
4817 goto gen_func;
4818 case TOK_SHR:
4819 func = TOK___shrdi3;
4820 goto gen_func;
4821 case TOK_SHL:
4822 func = TOK___shldi3;
4823 goto gen_func;
4826 break;
4827 default:
4828 /* compare operations */
4829 t = vtop->type.t;
4830 vswap();
4831 lexpand();
4832 vrotb(3);
4833 lexpand();
4834 /* stack: L1 H1 L2 H2 */
4835 tmp = vtop[-1];
4836 vtop[-1] = vtop[-2];
4837 vtop[-2] = tmp;
4838 /* stack: L1 L2 H1 H2 */
4839 /* compare high */
4840 op1 = op;
4841 /* when values are equal, we need to compare low words. since
4842 the jump is inverted, we invert the test too. */
4843 if (op1 == TOK_LT)
4844 op1 = TOK_LE;
4845 else if (op1 == TOK_GT)
4846 op1 = TOK_GE;
4847 else if (op1 == TOK_ULT)
4848 op1 = TOK_ULE;
4849 else if (op1 == TOK_UGT)
4850 op1 = TOK_UGE;
4851 a = 0;
4852 b = 0;
4853 gen_op(op1);
4854 if (op1 != TOK_NE) {
4855 a = gtst(1, 0);
4857 if (op != TOK_EQ) {
4858 /* generate non equal test */
4859 /* XXX: NOT PORTABLE yet */
4860 if (a == 0) {
4861 b = gtst(0, 0);
4862 } else {
4863 #ifdef TCC_TARGET_I386
4864 b = psym(0x850f, 0);
4865 #else
4866 error("not implemented");
4867 #endif
4870 /* compare low. Always unsigned */
4871 op1 = op;
4872 if (op1 == TOK_LT)
4873 op1 = TOK_ULT;
4874 else if (op1 == TOK_LE)
4875 op1 = TOK_ULE;
4876 else if (op1 == TOK_GT)
4877 op1 = TOK_UGT;
4878 else if (op1 == TOK_GE)
4879 op1 = TOK_UGE;
4880 gen_op(op1);
4881 a = gtst(1, a);
4882 gsym(b);
4883 vseti(VT_JMPI, a);
4884 break;
4888 /* handle integer constant optimizations and various machine
4889 independent opt */
4890 void gen_opic(int op)
4892 int fc, c1, c2, n;
4893 SValue *v1, *v2;
4895 v1 = vtop - 1;
4896 v2 = vtop;
4897 /* currently, we cannot do computations with forward symbols */
4898 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4899 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4900 if (c1 && c2) {
4901 fc = v2->c.i;
4902 switch(op) {
4903 case '+': v1->c.i += fc; break;
4904 case '-': v1->c.i -= fc; break;
4905 case '&': v1->c.i &= fc; break;
4906 case '^': v1->c.i ^= fc; break;
4907 case '|': v1->c.i |= fc; break;
4908 case '*': v1->c.i *= fc; break;
4910 case TOK_PDIV:
4911 case '/':
4912 case '%':
4913 case TOK_UDIV:
4914 case TOK_UMOD:
4915 /* if division by zero, generate explicit division */
4916 if (fc == 0) {
4917 if (const_wanted)
4918 error("division by zero in constant");
4919 goto general_case;
4921 switch(op) {
4922 default: v1->c.i /= fc; break;
4923 case '%': v1->c.i %= fc; break;
4924 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
4925 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
4927 break;
4928 case TOK_SHL: v1->c.i <<= fc; break;
4929 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
4930 case TOK_SAR: v1->c.i >>= fc; break;
4931 /* tests */
4932 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
4933 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
4934 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
4935 case TOK_NE: v1->c.i = v1->c.i != fc; break;
4936 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
4937 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
4938 case TOK_LT: v1->c.i = v1->c.i < fc; break;
4939 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
4940 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
4941 case TOK_GT: v1->c.i = v1->c.i > fc; break;
4942 /* logical */
4943 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
4944 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
4945 default:
4946 goto general_case;
4948 vtop--;
4949 } else {
4950 /* if commutative ops, put c2 as constant */
4951 if (c1 && (op == '+' || op == '&' || op == '^' ||
4952 op == '|' || op == '*')) {
4953 vswap();
4954 swap(&c1, &c2);
4956 fc = vtop->c.i;
4957 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
4958 op == TOK_PDIV) &&
4959 fc == 1) ||
4960 ((op == '+' || op == '-' || op == '|' || op == '^' ||
4961 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
4962 fc == 0) ||
4963 (op == '&' &&
4964 fc == -1))) {
4965 /* nothing to do */
4966 vtop--;
4967 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
4968 /* try to use shifts instead of muls or divs */
4969 if (fc > 0 && (fc & (fc - 1)) == 0) {
4970 n = -1;
4971 while (fc) {
4972 fc >>= 1;
4973 n++;
4975 vtop->c.i = n;
4976 if (op == '*')
4977 op = TOK_SHL;
4978 else if (op == TOK_PDIV)
4979 op = TOK_SAR;
4980 else
4981 op = TOK_SHR;
4983 goto general_case;
4984 } else if (c2 && (op == '+' || op == '-') &&
4985 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
4986 (VT_CONST | VT_SYM)) {
4987 /* symbol + constant case */
4988 if (op == '-')
4989 fc = -fc;
4990 vtop--;
4991 vtop->c.i += fc;
4992 } else {
4993 general_case:
4994 if (!nocode_wanted) {
4995 /* call low level op generator */
4996 gen_opi(op);
4997 } else {
4998 vtop--;
5004 /* generate a floating point operation with constant propagation */
5005 void gen_opif(int op)
5007 int c1, c2;
5008 SValue *v1, *v2;
5009 long double f1, f2;
5011 v1 = vtop - 1;
5012 v2 = vtop;
5013 /* currently, we cannot do computations with forward symbols */
5014 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5015 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5016 if (c1 && c2) {
5017 if (v1->type.t == VT_FLOAT) {
5018 f1 = v1->c.f;
5019 f2 = v2->c.f;
5020 } else if (v1->type.t == VT_DOUBLE) {
5021 f1 = v1->c.d;
5022 f2 = v2->c.d;
5023 } else {
5024 f1 = v1->c.ld;
5025 f2 = v2->c.ld;
5028 /* NOTE: we only do constant propagation if finite number (not
5029 NaN or infinity) (ANSI spec) */
5030 if (!ieee_finite(f1) || !ieee_finite(f2))
5031 goto general_case;
5033 switch(op) {
5034 case '+': f1 += f2; break;
5035 case '-': f1 -= f2; break;
5036 case '*': f1 *= f2; break;
5037 case '/':
5038 if (f2 == 0.0) {
5039 if (const_wanted)
5040 error("division by zero in constant");
5041 goto general_case;
5043 f1 /= f2;
5044 break;
5045 /* XXX: also handles tests ? */
5046 default:
5047 goto general_case;
5049 /* XXX: overflow test ? */
5050 if (v1->type.t == VT_FLOAT) {
5051 v1->c.f = f1;
5052 } else if (v1->type.t == VT_DOUBLE) {
5053 v1->c.d = f1;
5054 } else {
5055 v1->c.ld = f1;
5057 vtop--;
5058 } else {
5059 general_case:
5060 if (!nocode_wanted) {
5061 gen_opf(op);
5062 } else {
5063 vtop--;
5068 static int pointed_size(CType *type)
5070 int align;
5071 return type_size(pointed_type(type), &align);
5074 #if 0
5075 void check_pointer_types(SValue *p1, SValue *p2)
5077 char buf1[256], buf2[256];
5078 int t1, t2;
5079 t1 = p1->t;
5080 t2 = p2->t;
5081 if (!is_compatible_types(t1, t2)) {
5082 type_to_str(buf1, sizeof(buf1), t1, NULL);
5083 type_to_str(buf2, sizeof(buf2), t2, NULL);
5084 error("incompatible pointers '%s' and '%s'", buf1, buf2);
5087 #endif
5089 /* generic gen_op: handles types problems */
5090 void gen_op(int op)
5092 int u, t1, t2, bt1, bt2, t;
5093 CType type1;
5095 t1 = vtop[-1].type.t;
5096 t2 = vtop[0].type.t;
5097 bt1 = t1 & VT_BTYPE;
5098 bt2 = t2 & VT_BTYPE;
5100 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5101 /* at least one operand is a pointer */
5102 /* relationnal op: must be both pointers */
5103 if (op >= TOK_ULT && op <= TOK_GT) {
5104 // check_pointer_types(vtop, vtop - 1);
5105 /* pointers are handled are unsigned */
5106 t = VT_INT | VT_UNSIGNED;
5107 goto std_op;
5109 /* if both pointers, then it must be the '-' op */
5110 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5111 if (op != '-')
5112 error("cannot use pointers here");
5113 // check_pointer_types(vtop - 1, vtop);
5114 /* XXX: check that types are compatible */
5115 u = pointed_size(&vtop[-1].type);
5116 gen_opic(op);
5117 /* set to integer type */
5118 vtop->type.t = VT_INT;
5119 vpushi(u);
5120 gen_op(TOK_PDIV);
5121 } else {
5122 /* exactly one pointer : must be '+' or '-'. */
5123 if (op != '-' && op != '+')
5124 error("cannot use pointers here");
5125 /* Put pointer as first operand */
5126 if (bt2 == VT_PTR) {
5127 vswap();
5128 swap(&t1, &t2);
5130 type1 = vtop[-1].type;
5131 /* XXX: cast to int ? (long long case) */
5132 vpushi(pointed_size(&vtop[-1].type));
5133 gen_op('*');
5134 #ifdef CONFIG_TCC_BCHECK
5135 /* if evaluating constant expression, no code should be
5136 generated, so no bound check */
5137 if (do_bounds_check && !const_wanted) {
5138 /* if bounded pointers, we generate a special code to
5139 test bounds */
5140 if (op == '-') {
5141 vpushi(0);
5142 vswap();
5143 gen_op('-');
5145 gen_bounded_ptr_add();
5146 } else
5147 #endif
5149 gen_opic(op);
5151 /* put again type if gen_opic() swaped operands */
5152 vtop->type = type1;
5154 } else if (is_float(bt1) || is_float(bt2)) {
5155 /* compute bigger type and do implicit casts */
5156 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5157 t = VT_LDOUBLE;
5158 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5159 t = VT_DOUBLE;
5160 } else {
5161 t = VT_FLOAT;
5163 /* floats can only be used for a few operations */
5164 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5165 (op < TOK_ULT || op > TOK_GT))
5166 error("invalid operands for binary operation");
5167 goto std_op;
5168 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5169 /* cast to biggest op */
5170 t = VT_LLONG;
5171 /* convert to unsigned if it does not fit in a long long */
5172 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5173 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5174 t |= VT_UNSIGNED;
5175 goto std_op;
5176 } else {
5177 /* integer operations */
5178 t = VT_INT;
5179 /* convert to unsigned if it does not fit in an integer */
5180 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5181 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5182 t |= VT_UNSIGNED;
5183 std_op:
5184 /* XXX: currently, some unsigned operations are explicit, so
5185 we modify them here */
5186 if (t & VT_UNSIGNED) {
5187 if (op == TOK_SAR)
5188 op = TOK_SHR;
5189 else if (op == '/')
5190 op = TOK_UDIV;
5191 else if (op == '%')
5192 op = TOK_UMOD;
5193 else if (op == TOK_LT)
5194 op = TOK_ULT;
5195 else if (op == TOK_GT)
5196 op = TOK_UGT;
5197 else if (op == TOK_LE)
5198 op = TOK_ULE;
5199 else if (op == TOK_GE)
5200 op = TOK_UGE;
5202 vswap();
5203 type1.t = t;
5204 gen_cast(&type1);
5205 vswap();
5206 /* special case for shifts and long long: we keep the shift as
5207 an integer */
5208 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5209 type1.t = VT_INT;
5210 gen_cast(&type1);
5211 if (is_float(t))
5212 gen_opif(op);
5213 else if ((t & VT_BTYPE) == VT_LLONG)
5214 gen_opl(op);
5215 else
5216 gen_opic(op);
5217 if (op >= TOK_ULT && op <= TOK_GT) {
5218 /* relationnal op: the result is an int */
5219 vtop->type.t = VT_INT;
5220 } else {
5221 vtop->type.t = t;
5226 /* generic itof for unsigned long long case */
5227 void gen_cvt_itof1(int t)
5229 GFuncContext gf;
5231 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5232 (VT_LLONG | VT_UNSIGNED)) {
5234 gfunc_start(&gf, FUNC_CDECL);
5235 gfunc_param(&gf);
5236 if (t == VT_FLOAT)
5237 vpush_global_sym(&func_old_type, TOK___ulltof);
5238 else if (t == VT_DOUBLE)
5239 vpush_global_sym(&func_old_type, TOK___ulltod);
5240 else
5241 vpush_global_sym(&func_old_type, TOK___ulltold);
5242 gfunc_call(&gf);
5243 vpushi(0);
5244 vtop->r = REG_FRET;
5245 } else {
5246 gen_cvt_itof(t);
5250 /* generic ftoi for unsigned long long case */
5251 void gen_cvt_ftoi1(int t)
5253 GFuncContext gf;
5254 int st;
5256 if (t == (VT_LLONG | VT_UNSIGNED)) {
5257 /* not handled natively */
5258 gfunc_start(&gf, FUNC_CDECL);
5259 st = vtop->type.t & VT_BTYPE;
5260 gfunc_param(&gf);
5261 if (st == VT_FLOAT)
5262 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5263 else if (st == VT_DOUBLE)
5264 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5265 else
5266 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5267 gfunc_call(&gf);
5268 vpushi(0);
5269 vtop->r = REG_IRET;
5270 vtop->r2 = REG_LRET;
5271 } else {
5272 gen_cvt_ftoi(t);
5276 /* force char or short cast */
5277 void force_charshort_cast(int t)
5279 int bits, dbt;
5280 dbt = t & VT_BTYPE;
5281 /* XXX: add optimization if lvalue : just change type and offset */
5282 if (dbt == VT_BYTE)
5283 bits = 8;
5284 else
5285 bits = 16;
5286 if (t & VT_UNSIGNED) {
5287 vpushi((1 << bits) - 1);
5288 gen_op('&');
5289 } else {
5290 bits = 32 - bits;
5291 vpushi(bits);
5292 gen_op(TOK_SHL);
5293 vpushi(bits);
5294 gen_op(TOK_SAR);
5298 /* cast 'vtop' to 'type' */
5299 static void gen_cast(CType *type)
5301 int sbt, dbt, sf, df, c;
5303 /* special delayed cast for char/short */
5304 /* XXX: in some cases (multiple cascaded casts), it may still
5305 be incorrect */
5306 if (vtop->r & VT_MUSTCAST) {
5307 vtop->r &= ~VT_MUSTCAST;
5308 force_charshort_cast(vtop->type.t);
5311 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5312 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5314 if (sbt != dbt && !nocode_wanted) {
5315 sf = is_float(sbt);
5316 df = is_float(dbt);
5317 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5318 if (sf && df) {
5319 /* convert from fp to fp */
5320 if (c) {
5321 /* constant case: we can do it now */
5322 /* XXX: in ISOC, cannot do it if error in convert */
5323 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5324 vtop->c.f = (float)vtop->c.d;
5325 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5326 vtop->c.f = (float)vtop->c.ld;
5327 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5328 vtop->c.d = (double)vtop->c.f;
5329 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5330 vtop->c.d = (double)vtop->c.ld;
5331 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5332 vtop->c.ld = (long double)vtop->c.f;
5333 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5334 vtop->c.ld = (long double)vtop->c.d;
5335 } else {
5336 /* non constant case: generate code */
5337 gen_cvt_ftof(dbt);
5339 } else if (df) {
5340 /* convert int to fp */
5341 if (c) {
5342 switch(sbt) {
5343 case VT_LLONG | VT_UNSIGNED:
5344 case VT_LLONG:
5345 /* XXX: add const cases for long long */
5346 goto do_itof;
5347 case VT_INT | VT_UNSIGNED:
5348 switch(dbt) {
5349 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5350 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5351 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5353 break;
5354 default:
5355 switch(dbt) {
5356 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5357 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5358 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5360 break;
5362 } else {
5363 do_itof:
5364 gen_cvt_itof1(dbt);
5366 } else if (sf) {
5367 /* convert fp to int */
5368 /* we handle char/short/etc... with generic code */
5369 if (dbt != (VT_INT | VT_UNSIGNED) &&
5370 dbt != (VT_LLONG | VT_UNSIGNED) &&
5371 dbt != VT_LLONG)
5372 dbt = VT_INT;
5373 if (c) {
5374 switch(dbt) {
5375 case VT_LLONG | VT_UNSIGNED:
5376 case VT_LLONG:
5377 /* XXX: add const cases for long long */
5378 goto do_ftoi;
5379 case VT_INT | VT_UNSIGNED:
5380 switch(sbt) {
5381 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5382 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5383 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5385 break;
5386 default:
5387 /* int case */
5388 switch(sbt) {
5389 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5390 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5391 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5393 break;
5395 } else {
5396 do_ftoi:
5397 gen_cvt_ftoi1(dbt);
5399 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5400 /* additional cast for char/short/bool... */
5401 vtop->type.t = dbt;
5402 gen_cast(type);
5404 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5405 if ((sbt & VT_BTYPE) != VT_LLONG) {
5406 /* scalar to long long */
5407 if (c) {
5408 if (sbt == (VT_INT | VT_UNSIGNED))
5409 vtop->c.ll = vtop->c.ui;
5410 else
5411 vtop->c.ll = vtop->c.i;
5412 } else {
5413 /* machine independent conversion */
5414 gv(RC_INT);
5415 /* generate high word */
5416 if (sbt == (VT_INT | VT_UNSIGNED)) {
5417 vpushi(0);
5418 gv(RC_INT);
5419 } else {
5420 gv_dup();
5421 vpushi(31);
5422 gen_op(TOK_SAR);
5424 /* patch second register */
5425 vtop[-1].r2 = vtop->r;
5426 vpop();
5429 } else if (dbt == VT_BOOL) {
5430 /* scalar to bool */
5431 vpushi(0);
5432 gen_op(TOK_NE);
5433 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5434 (dbt & VT_BTYPE) == VT_SHORT) {
5435 force_charshort_cast(dbt);
5436 } else if ((dbt & VT_BTYPE) == VT_INT) {
5437 /* scalar to int */
5438 if (sbt == VT_LLONG) {
5439 /* from long long: just take low order word */
5440 lexpand();
5441 vpop();
5443 /* if lvalue and single word type, nothing to do because
5444 the lvalue already contains the real type size (see
5445 VT_LVAL_xxx constants) */
5448 vtop->type = *type;
5451 /* return type size. Put alignment at 'a' */
5452 static int type_size(CType *type, int *a)
5454 Sym *s;
5455 int bt;
5457 bt = type->t & VT_BTYPE;
5458 if (bt == VT_STRUCT) {
5459 /* struct/union */
5460 s = type->ref;
5461 *a = s->r;
5462 return s->c;
5463 } else if (bt == VT_PTR) {
5464 if (type->t & VT_ARRAY) {
5465 s = type->ref;
5466 return type_size(&s->type, a) * s->c;
5467 } else {
5468 *a = PTR_SIZE;
5469 return PTR_SIZE;
5471 } else if (bt == VT_LDOUBLE) {
5472 *a = LDOUBLE_ALIGN;
5473 return LDOUBLE_SIZE;
5474 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5475 *a = 4; /* XXX: i386 specific */
5476 return 8;
5477 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5478 *a = 4;
5479 return 4;
5480 } else if (bt == VT_SHORT) {
5481 *a = 2;
5482 return 2;
5483 } else {
5484 /* char, void, function, _Bool */
5485 *a = 1;
5486 return 1;
5490 /* return the pointed type of t */
5491 static inline CType *pointed_type(CType *type)
5493 return &type->ref->type;
5496 /* modify type so that its it is a pointer to type. */
5497 static void mk_pointer(CType *type)
5499 Sym *s;
5500 s = sym_push(SYM_FIELD, type, 0, -1);
5501 type->t = VT_PTR | (type->t & ~VT_TYPE);
5502 type->ref = s;
5505 static int is_compatible_types(CType *type1, CType *type2)
5507 Sym *s1, *s2;
5508 int bt1, bt2, t1, t2;
5510 t1 = type1->t & VT_TYPE;
5511 t2 = type2->t & VT_TYPE;
5512 bt1 = t1 & VT_BTYPE;
5513 bt2 = t2 & VT_BTYPE;
5514 if (bt1 == VT_PTR) {
5515 type1 = pointed_type(type1);
5516 /* if function, then convert implicitely to function pointer */
5517 if (bt2 != VT_FUNC) {
5518 if (bt2 != VT_PTR)
5519 return 0;
5520 type2 = pointed_type(type2);
5522 /* void matches everything */
5523 /* XXX: not fully compliant */
5524 if ((type1->t & VT_TYPE) == VT_VOID || (type2->t & VT_TYPE) == VT_VOID)
5525 return 1;
5526 return is_compatible_types(type1, type2);
5527 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
5528 return (type1->ref == type2->ref);
5529 } else if (bt1 == VT_FUNC) {
5530 if (bt2 != VT_FUNC)
5531 return 0;
5532 s1 = type1->ref;
5533 s2 = type2->ref;
5534 if (!is_compatible_types(&s1->type, &s2->type))
5535 return 0;
5536 /* XXX: not complete */
5537 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5538 return 1;
5539 if (s1->c != s2->c)
5540 return 0;
5541 while (s1 != NULL) {
5542 if (s2 == NULL)
5543 return 0;
5544 if (!is_compatible_types(&s1->type, &s2->type))
5545 return 0;
5546 s1 = s1->next;
5547 s2 = s2->next;
5549 if (s2)
5550 return 0;
5551 return 1;
5552 } else {
5553 /* XXX: not complete */
5554 return 1;
5558 /* print a type. If 'varstr' is not NULL, then the variable is also
5559 printed in the type */
5560 /* XXX: union */
5561 /* XXX: add array and function pointers */
5562 void type_to_str(char *buf, int buf_size,
5563 CType *type, const char *varstr)
5565 int bt, v, t;
5566 Sym *s, *sa;
5567 char buf1[256];
5568 const char *tstr;
5570 t = type->t & VT_TYPE;
5571 bt = t & VT_BTYPE;
5572 buf[0] = '\0';
5573 if (t & VT_UNSIGNED)
5574 pstrcat(buf, buf_size, "unsigned ");
5575 switch(bt) {
5576 case VT_VOID:
5577 tstr = "void";
5578 goto add_tstr;
5579 case VT_BOOL:
5580 tstr = "_Bool";
5581 goto add_tstr;
5582 case VT_BYTE:
5583 tstr = "char";
5584 goto add_tstr;
5585 case VT_SHORT:
5586 tstr = "short";
5587 goto add_tstr;
5588 case VT_INT:
5589 tstr = "int";
5590 goto add_tstr;
5591 case VT_LONG:
5592 tstr = "long";
5593 goto add_tstr;
5594 case VT_LLONG:
5595 tstr = "long long";
5596 goto add_tstr;
5597 case VT_FLOAT:
5598 tstr = "float";
5599 goto add_tstr;
5600 case VT_DOUBLE:
5601 tstr = "double";
5602 goto add_tstr;
5603 case VT_LDOUBLE:
5604 tstr = "long double";
5605 add_tstr:
5606 pstrcat(buf, buf_size, tstr);
5607 break;
5608 case VT_ENUM:
5609 case VT_STRUCT:
5610 if (bt == VT_STRUCT)
5611 tstr = "struct ";
5612 else
5613 tstr = "enum ";
5614 pstrcat(buf, buf_size, tstr);
5615 v = type->ref->v & ~SYM_STRUCT;
5616 if (v >= SYM_FIRST_ANOM)
5617 pstrcat(buf, buf_size, "<anonymous>");
5618 else
5619 pstrcat(buf, buf_size, get_tok_str(v, NULL));
5620 break;
5621 case VT_FUNC:
5622 s = type->ref;
5623 type_to_str(buf, buf_size, &s->type, varstr);
5624 pstrcat(buf, buf_size, "(");
5625 sa = s->next;
5626 while (sa != NULL) {
5627 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
5628 pstrcat(buf, buf_size, buf1);
5629 sa = sa->next;
5630 if (sa)
5631 pstrcat(buf, buf_size, ", ");
5633 pstrcat(buf, buf_size, ")");
5634 goto no_var;
5635 case VT_PTR:
5636 s = type->ref;
5637 pstrcpy(buf1, sizeof(buf1), "*");
5638 if (varstr)
5639 pstrcat(buf1, sizeof(buf1), varstr);
5640 type_to_str(buf, buf_size, &s->type, buf1);
5641 goto no_var;
5643 if (varstr) {
5644 pstrcat(buf, buf_size, " ");
5645 pstrcat(buf, buf_size, varstr);
5647 no_var: ;
5650 /* verify type compatibility to store vtop in 'dt' type, and generate
5651 casts if needed. */
5652 static void gen_assign_cast(CType *dt)
5654 CType *st;
5655 char buf1[256], buf2[256];
5656 int dbt, sbt;
5658 st = &vtop->type; /* source type */
5659 dbt = dt->t & VT_BTYPE;
5660 sbt = st->t & VT_BTYPE;
5661 if (dbt == VT_PTR) {
5662 /* special cases for pointers */
5663 /* a function is implicitely a function pointer */
5664 if (sbt == VT_FUNC) {
5665 if (!is_compatible_types(pointed_type(dt), st))
5666 goto error;
5667 else
5668 goto type_ok;
5670 /* '0' can also be a pointer */
5671 if (sbt == VT_INT &&
5672 ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) &&
5673 vtop->c.i == 0)
5674 goto type_ok;
5675 /* accept implicit pointer to integer cast with warning */
5676 if (sbt == VT_BYTE || sbt == VT_SHORT ||
5677 sbt == VT_INT || sbt == VT_LLONG) {
5678 warning("assignment makes pointer from integer without a cast");
5679 goto type_ok;
5681 } else if (dbt == VT_BYTE || dbt == VT_SHORT ||
5682 dbt == VT_INT || dbt == VT_LLONG) {
5683 if (sbt == VT_PTR || sbt == VT_FUNC) {
5684 warning("assignment makes integer from pointer without a cast");
5685 goto type_ok;
5688 if (!is_compatible_types(dt, st)) {
5689 error:
5690 type_to_str(buf1, sizeof(buf1), st, NULL);
5691 type_to_str(buf2, sizeof(buf2), dt, NULL);
5692 error("cannot cast '%s' to '%s'", buf1, buf2);
5694 type_ok:
5695 gen_cast(dt);
5698 /* store vtop in lvalue pushed on stack */
5699 void vstore(void)
5701 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5702 GFuncContext gf;
5704 ft = vtop[-1].type.t;
5705 sbt = vtop->type.t & VT_BTYPE;
5706 dbt = ft & VT_BTYPE;
5707 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
5708 (sbt == VT_INT && dbt == VT_SHORT)) {
5709 /* optimize char/short casts */
5710 delayed_cast = VT_MUSTCAST;
5711 vtop->type.t = ft & VT_TYPE;
5712 } else {
5713 delayed_cast = 0;
5714 gen_assign_cast(&vtop[-1].type);
5717 if (sbt == VT_STRUCT) {
5718 /* if structure, only generate pointer */
5719 /* structure assignment : generate memcpy */
5720 /* XXX: optimize if small size */
5721 if (!nocode_wanted) {
5722 vdup();
5723 gfunc_start(&gf, FUNC_CDECL);
5724 /* type size */
5725 size = type_size(&vtop->type, &align);
5726 vpushi(size);
5727 gfunc_param(&gf);
5728 /* source */
5729 vtop->type.t = VT_INT;
5730 gaddrof();
5731 gfunc_param(&gf);
5732 /* destination */
5733 vswap();
5734 vtop->type.t = VT_INT;
5735 gaddrof();
5736 gfunc_param(&gf);
5738 save_regs(0);
5739 vpush_global_sym(&func_old_type, TOK_memcpy);
5740 gfunc_call(&gf);
5741 } else {
5742 vswap();
5743 vpop();
5745 /* leave source on stack */
5746 } else if (ft & VT_BITFIELD) {
5747 /* bitfield store handling */
5748 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
5749 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5750 /* remove bit field info to avoid loops */
5751 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
5753 /* duplicate destination */
5754 vdup();
5755 vtop[-1] = vtop[-2];
5757 /* mask and shift source */
5758 vpushi((1 << bit_size) - 1);
5759 gen_op('&');
5760 vpushi(bit_pos);
5761 gen_op(TOK_SHL);
5762 /* load destination, mask and or with source */
5763 vswap();
5764 vpushi(~(((1 << bit_size) - 1) << bit_pos));
5765 gen_op('&');
5766 gen_op('|');
5767 /* store result */
5768 vstore();
5769 } else {
5770 #ifdef CONFIG_TCC_BCHECK
5771 /* bound check case */
5772 if (vtop[-1].r & VT_MUSTBOUND) {
5773 vswap();
5774 gbound();
5775 vswap();
5777 #endif
5778 if (!nocode_wanted) {
5779 rc = RC_INT;
5780 if (is_float(ft))
5781 rc = RC_FLOAT;
5782 r = gv(rc); /* generate value */
5783 /* if lvalue was saved on stack, must read it */
5784 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
5785 SValue sv;
5786 t = get_reg(RC_INT);
5787 sv.type.t = VT_INT;
5788 sv.r = VT_LOCAL | VT_LVAL;
5789 sv.c.ul = vtop[-1].c.ul;
5790 load(t, &sv);
5791 vtop[-1].r = t | VT_LVAL;
5793 store(r, vtop - 1);
5794 /* two word case handling : store second register at word + 4 */
5795 if ((ft & VT_BTYPE) == VT_LLONG) {
5796 vswap();
5797 /* convert to int to increment easily */
5798 vtop->type.t = VT_INT;
5799 gaddrof();
5800 vpushi(4);
5801 gen_op('+');
5802 vtop->r |= VT_LVAL;
5803 vswap();
5804 /* XXX: it works because r2 is spilled last ! */
5805 store(vtop->r2, vtop - 1);
5808 vswap();
5809 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5810 vtop->r |= delayed_cast;
5814 /* post defines POST/PRE add. c is the token ++ or -- */
5815 void inc(int post, int c)
5817 test_lvalue();
5818 vdup(); /* save lvalue */
5819 if (post) {
5820 gv_dup(); /* duplicate value */
5821 vrotb(3);
5822 vrotb(3);
5824 /* add constant */
5825 vpushi(c - TOK_MID);
5826 gen_op('+');
5827 vstore(); /* store value */
5828 if (post)
5829 vpop(); /* if post op, return saved value */
5832 /* Parse GNUC __attribute__ extension. Currently, the following
5833 extensions are recognized:
5834 - aligned(n) : set data/function alignment.
5835 - section(x) : generate data/code in this section.
5836 - unused : currently ignored, but may be used someday.
5838 static void parse_attribute(AttributeDef *ad)
5840 int t, n;
5842 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
5843 next();
5844 skip('(');
5845 skip('(');
5846 while (tok != ')') {
5847 if (tok < TOK_IDENT)
5848 expect("attribute name");
5849 t = tok;
5850 next();
5851 switch(t) {
5852 case TOK_SECTION1:
5853 case TOK_SECTION2:
5854 skip('(');
5855 if (tok != TOK_STR)
5856 expect("section name");
5857 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
5858 next();
5859 skip(')');
5860 break;
5861 case TOK_ALIGNED1:
5862 case TOK_ALIGNED2:
5863 if (tok == '(') {
5864 next();
5865 n = expr_const();
5866 if (n <= 0 || (n & (n - 1)) != 0)
5867 error("alignment must be a positive power of two");
5868 skip(')');
5869 } else {
5870 n = MAX_ALIGN;
5872 ad->aligned = n;
5873 break;
5874 case TOK_UNUSED1:
5875 case TOK_UNUSED2:
5876 /* currently, no need to handle it because tcc does not
5877 track unused objects */
5878 break;
5879 case TOK_NORETURN1:
5880 case TOK_NORETURN2:
5881 /* currently, no need to handle it because tcc does not
5882 track unused objects */
5883 break;
5884 case TOK_CDECL1:
5885 case TOK_CDECL2:
5886 case TOK_CDECL3:
5887 ad->func_call = FUNC_CDECL;
5888 break;
5889 case TOK_STDCALL1:
5890 case TOK_STDCALL2:
5891 case TOK_STDCALL3:
5892 ad->func_call = FUNC_STDCALL;
5893 break;
5894 default:
5895 // warning("'%s' attribute ignored", get_tok_str(t, NULL));
5896 /* skip parameters */
5897 /* XXX: skip parenthesis too */
5898 if (tok == '(') {
5899 next();
5900 while (tok != ')' && tok != -1)
5901 next();
5902 next();
5904 break;
5906 if (tok != ',')
5907 break;
5908 next();
5910 skip(')');
5911 skip(')');
5915 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
5916 static void struct_decl(CType *type, int u)
5918 int a, v, size, align, maxalign, c, offset;
5919 int bit_size, bit_pos, bsize, bt, lbit_pos;
5920 Sym *s, *ss, **ps;
5921 AttributeDef ad;
5922 CType type1, btype;
5924 a = tok; /* save decl type */
5925 next();
5926 if (tok != '{') {
5927 v = tok;
5928 next();
5929 /* struct already defined ? return it */
5930 if (v < TOK_IDENT)
5931 expect("struct/union/enum name");
5932 s = struct_find(v);
5933 if (s) {
5934 if (s->type.t != a)
5935 error("invalid type");
5936 goto do_decl;
5938 } else {
5939 v = anon_sym++;
5941 type1.t = a;
5942 s = sym_push(v | SYM_STRUCT, &type1, 0, 0);
5943 /* put struct/union/enum name in type */
5944 do_decl:
5945 type->t = u;
5946 type->ref = s;
5948 if (tok == '{') {
5949 next();
5950 if (s->c)
5951 error("struct/union/enum already defined");
5952 /* cannot be empty */
5953 c = 0;
5954 /* non empty enums are not allowed */
5955 if (a == TOK_ENUM) {
5956 for(;;) {
5957 v = tok;
5958 if (v < TOK_UIDENT)
5959 expect("identifier");
5960 next();
5961 if (tok == '=') {
5962 next();
5963 c = expr_const();
5965 /* enum symbols have static storage */
5966 ss = sym_push(v, &int_type, VT_CONST, c);
5967 ss->type.t |= VT_STATIC;
5968 if (tok != ',')
5969 break;
5970 next();
5971 c++;
5972 /* NOTE: we accept a trailing comma */
5973 if (tok == '}')
5974 break;
5976 skip('}');
5977 } else {
5978 maxalign = 1;
5979 ps = &s->next;
5980 bit_pos = 0;
5981 offset = 0;
5982 while (tok != '}') {
5983 parse_btype(&btype, &ad);
5984 while (1) {
5985 bit_size = -1;
5986 v = 0;
5987 type1 = btype;
5988 if (tok != ':') {
5989 type_decl(&type1, &ad, &v, TYPE_DIRECT);
5990 if ((type1.t & VT_BTYPE) == VT_FUNC ||
5991 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
5992 error("invalid type for '%s'",
5993 get_tok_str(v, NULL));
5995 if (tok == ':') {
5996 next();
5997 bit_size = expr_const();
5998 /* XXX: handle v = 0 case for messages */
5999 if (bit_size < 0)
6000 error("negative width in bit-field '%s'",
6001 get_tok_str(v, NULL));
6002 if (v && bit_size == 0)
6003 error("zero width for bit-field '%s'",
6004 get_tok_str(v, NULL));
6006 size = type_size(&type1, &align);
6007 lbit_pos = 0;
6008 if (bit_size >= 0) {
6009 bt = type1.t & VT_BTYPE;
6010 if (bt != VT_INT &&
6011 bt != VT_BYTE &&
6012 bt != VT_SHORT &&
6013 bt != VT_ENUM)
6014 error("bitfields must have scalar type");
6015 bsize = size * 8;
6016 if (bit_size > bsize) {
6017 error("width of '%s' exceeds its type",
6018 get_tok_str(v, NULL));
6019 } else if (bit_size == bsize) {
6020 /* no need for bit fields */
6021 bit_pos = 0;
6022 } else if (bit_size == 0) {
6023 /* XXX: what to do if only padding in a
6024 structure ? */
6025 /* zero size: means to pad */
6026 if (bit_pos > 0)
6027 bit_pos = bsize;
6028 } else {
6029 /* we do not have enough room ? */
6030 if ((bit_pos + bit_size) > bsize)
6031 bit_pos = 0;
6032 lbit_pos = bit_pos;
6033 /* XXX: handle LSB first */
6034 type1.t |= VT_BITFIELD |
6035 (bit_pos << VT_STRUCT_SHIFT) |
6036 (bit_size << (VT_STRUCT_SHIFT + 6));
6037 bit_pos += bit_size;
6039 } else {
6040 bit_pos = 0;
6042 if (v) {
6043 /* add new memory data only if starting
6044 bit field */
6045 if (lbit_pos == 0) {
6046 if (a == TOK_STRUCT) {
6047 c = (c + align - 1) & -align;
6048 offset = c;
6049 c += size;
6050 } else {
6051 offset = 0;
6052 if (size > c)
6053 c = size;
6055 if (align > maxalign)
6056 maxalign = align;
6058 #if 0
6059 printf("add field %s offset=%d",
6060 get_tok_str(v, NULL), offset);
6061 if (type1.t & VT_BITFIELD) {
6062 printf(" pos=%d size=%d",
6063 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6064 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6066 printf("\n");
6067 #endif
6068 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6069 *ps = ss;
6070 ps = &ss->next;
6072 if (tok == ';' || tok == TOK_EOF)
6073 break;
6074 skip(',');
6076 skip(';');
6078 skip('}');
6079 /* store size and alignment */
6080 s->c = (c + maxalign - 1) & -maxalign;
6081 s->r = maxalign;
6086 /* return 0 if no type declaration. otherwise, return the basic type
6087 and skip it.
6089 static int parse_btype(CType *type, AttributeDef *ad)
6091 int t, u, type_found;
6092 Sym *s;
6093 CType type1;
6095 memset(ad, 0, sizeof(AttributeDef));
6096 type_found = 0;
6097 t = 0;
6098 while(1) {
6099 switch(tok) {
6100 case TOK_EXTENSION:
6101 /* currently, we really ignore extension */
6102 next();
6103 continue;
6105 /* basic types */
6106 case TOK_CHAR:
6107 u = VT_BYTE;
6108 basic_type:
6109 next();
6110 basic_type1:
6111 if ((t & VT_BTYPE) != 0)
6112 error("too many basic types");
6113 t |= u;
6114 break;
6115 case TOK_VOID:
6116 u = VT_VOID;
6117 goto basic_type;
6118 case TOK_SHORT:
6119 u = VT_SHORT;
6120 goto basic_type;
6121 case TOK_INT:
6122 next();
6123 break;
6124 case TOK_LONG:
6125 next();
6126 if ((t & VT_BTYPE) == VT_DOUBLE) {
6127 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6128 } else if ((t & VT_BTYPE) == VT_LONG) {
6129 t = (t & ~VT_BTYPE) | VT_LLONG;
6130 } else {
6131 u = VT_LONG;
6132 goto basic_type1;
6134 break;
6135 case TOK_BOOL:
6136 u = VT_BOOL;
6137 goto basic_type;
6138 case TOK_FLOAT:
6139 u = VT_FLOAT;
6140 goto basic_type;
6141 case TOK_DOUBLE:
6142 next();
6143 if ((t & VT_BTYPE) == VT_LONG) {
6144 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6145 } else {
6146 u = VT_DOUBLE;
6147 goto basic_type1;
6149 break;
6150 case TOK_ENUM:
6151 struct_decl(&type1, VT_ENUM);
6152 basic_type2:
6153 u = type1.t;
6154 type->ref = type1.ref;
6155 goto basic_type1;
6156 case TOK_STRUCT:
6157 case TOK_UNION:
6158 struct_decl(&type1, VT_STRUCT);
6159 goto basic_type2;
6161 /* type modifiers */
6162 case TOK_CONST1:
6163 case TOK_CONST2:
6164 case TOK_CONST3:
6165 case TOK_VOLATILE1:
6166 case TOK_VOLATILE2:
6167 case TOK_VOLATILE3:
6168 case TOK_REGISTER:
6169 case TOK_SIGNED1:
6170 case TOK_SIGNED2:
6171 case TOK_SIGNED3:
6172 case TOK_AUTO:
6173 case TOK_RESTRICT1:
6174 case TOK_RESTRICT2:
6175 case TOK_RESTRICT3:
6176 next();
6177 break;
6178 case TOK_UNSIGNED:
6179 t |= VT_UNSIGNED;
6180 next();
6181 break;
6183 /* storage */
6184 case TOK_EXTERN:
6185 t |= VT_EXTERN;
6186 next();
6187 break;
6188 case TOK_STATIC:
6189 t |= VT_STATIC;
6190 next();
6191 break;
6192 case TOK_TYPEDEF:
6193 t |= VT_TYPEDEF;
6194 next();
6195 break;
6196 case TOK_INLINE1:
6197 case TOK_INLINE2:
6198 case TOK_INLINE3:
6199 t |= VT_INLINE;
6200 next();
6201 break;
6203 /* GNUC attribute */
6204 case TOK_ATTRIBUTE1:
6205 case TOK_ATTRIBUTE2:
6206 parse_attribute(ad);
6207 break;
6208 /* GNUC typeof */
6209 case TOK_TYPEOF1:
6210 case TOK_TYPEOF2:
6211 case TOK_TYPEOF3:
6212 next();
6213 parse_expr_type(&type1);
6214 goto basic_type2;
6215 default:
6216 s = sym_find(tok);
6217 if (!s || !(s->type.t & VT_TYPEDEF))
6218 goto the_end;
6219 t |= (s->type.t & ~VT_TYPEDEF);
6220 type->ref = s->type.ref;
6221 next();
6222 break;
6224 type_found = 1;
6226 the_end:
6227 /* long is never used as type */
6228 if ((t & VT_BTYPE) == VT_LONG)
6229 t = (t & ~VT_BTYPE) | VT_INT;
6230 type->t = t;
6231 return type_found;
6234 /* convert a function parameter type (array to pointer and function to
6235 function pointer) */
6236 static inline void convert_parameter_type(CType *pt)
6238 /* array must be transformed to pointer according to ANSI C */
6239 pt->t &= ~VT_ARRAY;
6240 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6241 mk_pointer(pt);
6245 static void post_type(CType *type, AttributeDef *ad)
6247 int n, l, t1;
6248 Sym **plast, *s, *first;
6249 AttributeDef ad1;
6250 CType pt;
6252 if (tok == '(') {
6253 /* function declaration */
6254 next();
6255 l = 0;
6256 first = NULL;
6257 plast = &first;
6258 while (tok != ')') {
6259 /* read param name and compute offset */
6260 if (l != FUNC_OLD) {
6261 if (!parse_btype(&pt, &ad1)) {
6262 if (l) {
6263 error("invalid type");
6264 } else {
6265 l = FUNC_OLD;
6266 goto old_proto;
6269 l = FUNC_NEW;
6270 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6271 break;
6272 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6273 if ((pt.t & VT_BTYPE) == VT_VOID)
6274 error("parameter declared as void");
6275 } else {
6276 old_proto:
6277 n = tok;
6278 pt.t = VT_INT;
6279 next();
6281 convert_parameter_type(&pt);
6282 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6283 *plast = s;
6284 plast = &s->next;
6285 if (tok == ',') {
6286 next();
6287 if (l == FUNC_NEW && tok == TOK_DOTS) {
6288 l = FUNC_ELLIPSIS;
6289 next();
6290 break;
6294 /* if no parameters, then old type prototype */
6295 if (l == 0)
6296 l = FUNC_OLD;
6297 skip(')');
6298 t1 = type->t & VT_STORAGE;
6299 type->t &= ~VT_STORAGE;
6300 post_type(type, ad);
6301 /* we push a anonymous symbol which will contain the function prototype */
6302 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6303 s->next = first;
6304 type->t = t1 | VT_FUNC;
6305 type->ref = s;
6306 } else if (tok == '[') {
6307 /* array definition */
6308 next();
6309 n = -1;
6310 if (tok != ']') {
6311 n = expr_const();
6312 if (n < 0)
6313 error("invalid array size");
6315 skip(']');
6316 /* parse next post type */
6317 t1 = type->t & VT_STORAGE;
6318 type->t &= ~VT_STORAGE;
6319 post_type(type, ad);
6321 /* we push a anonymous symbol which will contain the array
6322 element type */
6323 s = sym_push(SYM_FIELD, type, 0, n);
6324 type->t = t1 | VT_ARRAY | VT_PTR;
6325 type->ref = s;
6329 /* Parse a type declaration (except basic type), and return the type
6330 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6331 expected. 'type' should contain the basic type. 'ad' is the
6332 attribute definition of the basic type. It can be modified by
6333 type_decl().
6335 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6337 Sym *s;
6338 CType type1, *type2;
6340 while (tok == '*') {
6341 next();
6342 redo:
6343 switch(tok) {
6344 case TOK_CONST1:
6345 case TOK_CONST2:
6346 case TOK_CONST3:
6347 case TOK_VOLATILE1:
6348 case TOK_VOLATILE2:
6349 case TOK_VOLATILE3:
6350 case TOK_RESTRICT1:
6351 case TOK_RESTRICT2:
6352 case TOK_RESTRICT3:
6353 next();
6354 goto redo;
6356 mk_pointer(type);
6359 /* XXX: clarify attribute handling */
6360 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6361 parse_attribute(ad);
6363 /* recursive type */
6364 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6365 type1.t = 0; /* XXX: same as int */
6366 if (tok == '(') {
6367 next();
6368 /* XXX: this is not correct to modify 'ad' at this point, but
6369 the syntax is not clear */
6370 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6371 parse_attribute(ad);
6372 type_decl(&type1, ad, v, td);
6373 skip(')');
6374 } else {
6375 /* type identifier */
6376 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6377 *v = tok;
6378 next();
6379 } else {
6380 if (!(td & TYPE_ABSTRACT))
6381 expect("identifier");
6382 *v = 0;
6385 post_type(type, ad);
6386 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6387 parse_attribute(ad);
6388 if (!type1.t)
6389 return;
6390 /* append type at the end of type1 */
6391 type2 = &type1;
6392 for(;;) {
6393 s = type2->ref;
6394 type2 = &s->type;
6395 if (!type2->t) {
6396 *type2 = *type;
6397 break;
6400 *type = type1;
6403 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6404 static int lvalue_type(int t)
6406 int bt, r;
6407 r = VT_LVAL;
6408 bt = t & VT_BTYPE;
6409 if (bt == VT_BYTE || bt == VT_BOOL)
6410 r |= VT_LVAL_BYTE;
6411 else if (bt == VT_SHORT)
6412 r |= VT_LVAL_SHORT;
6413 else
6414 return r;
6415 if (t & VT_UNSIGNED)
6416 r |= VT_LVAL_UNSIGNED;
6417 return r;
6420 /* indirection with full error checking and bound check */
6421 static void indir(void)
6423 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6424 expect("pointer");
6425 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6426 gv(RC_INT);
6427 vtop->type = *pointed_type(&vtop->type);
6428 /* an array is never an lvalue */
6429 if (!(vtop->type.t & VT_ARRAY)) {
6430 vtop->r |= lvalue_type(vtop->type.t);
6431 /* if bound checking, the referenced pointer must be checked */
6432 if (do_bounds_check)
6433 vtop->r |= VT_MUSTBOUND;
6437 /* pass a parameter to a function and do type checking and casting */
6438 void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)
6440 int func_type;
6441 CType type;
6443 func_type = func->c;
6444 if (func_type == FUNC_OLD ||
6445 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6446 /* default casting : only need to convert float to double */
6447 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6448 type.t = VT_DOUBLE;
6449 gen_cast(&type);
6451 } else if (arg == NULL) {
6452 error("too many arguments to function");
6453 } else {
6454 gen_assign_cast(&arg->type);
6456 if (!nocode_wanted) {
6457 gfunc_param(gf);
6458 } else {
6459 vpop();
6463 /* parse an expression of the form '(type)' or '(expr)' and return its
6464 type */
6465 static void parse_expr_type(CType *type)
6467 int n;
6468 AttributeDef ad;
6470 skip('(');
6471 if (parse_btype(type, &ad)) {
6472 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6473 } else {
6474 expr_type(type);
6476 skip(')');
6479 static void vpush_tokc(int t)
6481 CType type;
6482 type.t = t;
6483 vsetc(&type, VT_CONST, &tokc);
6486 static void unary(void)
6488 int n, t, align, size, r;
6489 CType type;
6490 Sym *s;
6491 GFuncContext gf;
6492 AttributeDef ad;
6494 /* XXX: GCC 2.95.3 does not generate a table although it should be
6495 better here */
6496 tok_next:
6497 switch(tok) {
6498 case TOK_EXTENSION:
6499 next();
6500 goto tok_next;
6501 case TOK_CINT:
6502 case TOK_CCHAR:
6503 case TOK_LCHAR:
6504 vpushi(tokc.i);
6505 next();
6506 break;
6507 case TOK_CUINT:
6508 vpush_tokc(VT_INT | VT_UNSIGNED);
6509 next();
6510 break;
6511 case TOK_CLLONG:
6512 vpush_tokc(VT_LLONG);
6513 next();
6514 break;
6515 case TOK_CULLONG:
6516 vpush_tokc(VT_LLONG | VT_UNSIGNED);
6517 next();
6518 break;
6519 case TOK_CFLOAT:
6520 vpush_tokc(VT_FLOAT);
6521 next();
6522 break;
6523 case TOK_CDOUBLE:
6524 vpush_tokc(VT_DOUBLE);
6525 next();
6526 break;
6527 case TOK_CLDOUBLE:
6528 vpush_tokc(VT_LDOUBLE);
6529 next();
6530 break;
6531 case TOK___FUNCTION__:
6532 if (!gnu_ext)
6533 goto tok_identifier;
6534 /* fall thru */
6535 case TOK___FUNC__:
6537 void *ptr;
6538 int len;
6539 /* special function name identifier */
6540 len = strlen(funcname) + 1;
6541 /* generate char[len] type */
6542 type.t = VT_BYTE;
6543 mk_pointer(&type);
6544 type.t |= VT_ARRAY;
6545 type.ref->c = len;
6546 vpush_ref(&type, data_section, data_section->data_offset, len);
6547 ptr = section_ptr_add(data_section, len);
6548 memcpy(ptr, funcname, len);
6549 next();
6551 break;
6552 case TOK_LSTR:
6553 t = VT_INT;
6554 goto str_init;
6555 case TOK_STR:
6556 /* string parsing */
6557 t = VT_BYTE;
6558 str_init:
6559 type.t = t;
6560 mk_pointer(&type);
6561 type.t |= VT_ARRAY;
6562 memset(&ad, 0, sizeof(AttributeDef));
6563 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
6564 break;
6565 case '(':
6566 next();
6567 /* cast ? */
6568 if (parse_btype(&type, &ad)) {
6569 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
6570 skip(')');
6571 /* check ISOC99 compound literal */
6572 if (tok == '{') {
6573 /* data is allocated locally by default */
6574 if (global_expr)
6575 r = VT_CONST;
6576 else
6577 r = VT_LOCAL;
6578 /* all except arrays are lvalues */
6579 if (!(type.t & VT_ARRAY))
6580 r |= lvalue_type(type.t);
6581 memset(&ad, 0, sizeof(AttributeDef));
6582 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
6583 } else {
6584 unary();
6585 gen_cast(&type);
6587 } else if (tok == '{') {
6588 /* save all registers */
6589 save_regs(0);
6590 /* statement expression : we do not accept break/continue
6591 inside as GCC does */
6592 block(NULL, NULL, NULL, NULL, 0, 1);
6593 skip(')');
6594 } else {
6595 gexpr();
6596 skip(')');
6598 break;
6599 case '*':
6600 next();
6601 unary();
6602 indir();
6603 break;
6604 case '&':
6605 next();
6606 unary();
6607 /* functions names must be treated as function pointers,
6608 except for unary '&' and sizeof. Since we consider that
6609 functions are not lvalues, we only have to handle it
6610 there and in function calls. */
6611 /* arrays can also be used although they are not lvalues */
6612 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
6613 !(vtop->type.t & VT_ARRAY))
6614 test_lvalue();
6615 mk_pointer(&vtop->type);
6616 gaddrof();
6617 break;
6618 case '!':
6619 next();
6620 unary();
6621 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
6622 vtop->c.i = !vtop->c.i;
6623 else if ((vtop->r & VT_VALMASK) == VT_CMP)
6624 vtop->c.i = vtop->c.i ^ 1;
6625 else
6626 vseti(VT_JMP, gtst(1, 0));
6627 break;
6628 case '~':
6629 next();
6630 unary();
6631 vpushi(-1);
6632 gen_op('^');
6633 break;
6634 case '+':
6635 next();
6636 /* in order to force cast, we add zero */
6637 unary();
6638 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
6639 error("pointer not accepted for unary plus");
6640 vpushi(0);
6641 gen_op('+');
6642 break;
6643 case TOK_SIZEOF:
6644 case TOK_ALIGNOF1:
6645 case TOK_ALIGNOF2:
6646 t = tok;
6647 next();
6648 if (tok == '(') {
6649 parse_expr_type(&type);
6650 } else {
6651 unary_type(&type);
6653 size = type_size(&type, &align);
6654 if (t == TOK_SIZEOF)
6655 vpushi(size);
6656 else
6657 vpushi(align);
6658 break;
6660 case TOK_INC:
6661 case TOK_DEC:
6662 t = tok;
6663 next();
6664 unary();
6665 inc(0, t);
6666 break;
6667 case '-':
6668 next();
6669 vpushi(0);
6670 unary();
6671 gen_op('-');
6672 break;
6673 case TOK_LAND:
6674 if (!gnu_ext)
6675 goto tok_identifier;
6676 next();
6677 /* allow to take the address of a label */
6678 if (tok < TOK_UIDENT)
6679 expect("label identifier");
6680 s = label_find(tok);
6681 if (!s) {
6682 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
6683 } else {
6684 if (s->r == LABEL_DECLARED)
6685 s->r = LABEL_FORWARD;
6687 if (!s->type.t) {
6688 s->type.t = VT_VOID;
6689 mk_pointer(&s->type);
6690 s->type.t |= VT_STATIC;
6692 vset(&s->type, VT_CONST | VT_SYM, 0);
6693 vtop->sym = s;
6694 next();
6695 break;
6696 default:
6697 tok_identifier:
6698 t = tok;
6699 next();
6700 if (t < TOK_UIDENT)
6701 expect("identifier");
6702 s = sym_find(t);
6703 if (!s) {
6704 if (tok != '(')
6705 error("'%s' undeclared", get_tok_str(t, NULL));
6706 /* for simple function calls, we tolerate undeclared
6707 external reference to int() function */
6708 s = external_global_sym(t, &func_old_type, 0);
6710 vset(&s->type, s->r, s->c);
6711 /* if forward reference, we must point to s */
6712 if (vtop->r & VT_SYM) {
6713 vtop->sym = s;
6714 vtop->c.ul = 0;
6716 break;
6719 /* post operations */
6720 while (1) {
6721 if (tok == TOK_INC || tok == TOK_DEC) {
6722 inc(1, tok);
6723 next();
6724 } else if (tok == '.' || tok == TOK_ARROW) {
6725 /* field */
6726 if (tok == TOK_ARROW)
6727 indir();
6728 test_lvalue();
6729 gaddrof();
6730 next();
6731 /* expect pointer on structure */
6732 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
6733 expect("struct or union");
6734 s = vtop->type.ref;
6735 /* find field */
6736 tok |= SYM_FIELD;
6737 while ((s = s->next) != NULL) {
6738 if (s->v == tok)
6739 break;
6741 if (!s)
6742 error("field not found");
6743 /* add field offset to pointer */
6744 vtop->type = char_pointer_type; /* change type to 'char *' */
6745 vpushi(s->c);
6746 gen_op('+');
6747 /* change type to field type, and set to lvalue */
6748 vtop->type = s->type;
6749 /* an array is never an lvalue */
6750 if (!(vtop->type.t & VT_ARRAY)) {
6751 vtop->r |= lvalue_type(vtop->type.t);
6752 /* if bound checking, the referenced pointer must be checked */
6753 if (do_bounds_check)
6754 vtop->r |= VT_MUSTBOUND;
6756 next();
6757 } else if (tok == '[') {
6758 next();
6759 gexpr();
6760 gen_op('+');
6761 indir();
6762 skip(']');
6763 } else if (tok == '(') {
6764 SValue ret;
6765 Sym *sa;
6767 /* function call */
6768 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
6769 /* pointer test (no array accepted) */
6770 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
6771 vtop->type = *pointed_type(&vtop->type);
6772 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
6773 goto error_func;
6774 } else {
6775 error_func:
6776 expect("function pointer");
6778 } else {
6779 vtop->r &= ~VT_LVAL; /* no lvalue */
6781 /* get return type */
6782 s = vtop->type.ref;
6783 if (!nocode_wanted) {
6784 save_regs(0); /* save used temporary registers */
6785 gfunc_start(&gf, s->r);
6787 next();
6788 sa = s->next; /* first parameter */
6789 #ifdef INVERT_FUNC_PARAMS
6791 int parlevel;
6792 Sym *args, *s1;
6793 ParseState saved_parse_state;
6794 TokenString str;
6796 /* read each argument and store it on a stack */
6797 args = NULL;
6798 if (tok != ')') {
6799 for(;;) {
6800 tok_str_new(&str);
6801 parlevel = 0;
6802 while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
6803 tok != TOK_EOF) {
6804 if (tok == '(')
6805 parlevel++;
6806 else if (tok == ')')
6807 parlevel--;
6808 tok_str_add_tok(&str);
6809 next();
6811 tok_str_add(&str, -1); /* end of file added */
6812 tok_str_add(&str, 0);
6813 s1 = sym_push2(&args, 0, 0, (int)str.str);
6814 s1->next = sa; /* add reference to argument */
6815 if (sa)
6816 sa = sa->next;
6817 if (tok == ')')
6818 break;
6819 skip(',');
6823 /* now generate code in reverse order by reading the stack */
6824 save_parse_state(&saved_parse_state);
6825 while (args) {
6826 macro_ptr = (int *)args->c;
6827 next();
6828 expr_eq();
6829 if (tok != -1)
6830 expect("',' or ')'");
6831 gfunc_param_typed(&gf, s, args->next);
6832 s1 = args->prev;
6833 tok_str_free((int *)args->c);
6834 tcc_free(args);
6835 args = s1;
6837 restore_parse_state(&saved_parse_state);
6839 #endif
6840 /* compute first implicit argument if a structure is returned */
6841 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
6842 /* get some space for the returned structure */
6843 size = type_size(&s->type, &align);
6844 loc = (loc - size) & -align;
6845 ret.type = s->type;
6846 ret.r = VT_LOCAL | VT_LVAL;
6847 /* pass it as 'int' to avoid structure arg passing
6848 problems */
6849 vseti(VT_LOCAL, loc);
6850 ret.c = vtop->c;
6851 if (!nocode_wanted)
6852 gfunc_param(&gf);
6853 else
6854 vtop--;
6855 } else {
6856 ret.type = s->type;
6857 ret.r2 = VT_CONST;
6858 /* return in register */
6859 if (is_float(ret.type.t)) {
6860 ret.r = REG_FRET;
6861 } else {
6862 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
6863 ret.r2 = REG_LRET;
6864 ret.r = REG_IRET;
6866 ret.c.i = 0;
6868 #ifndef INVERT_FUNC_PARAMS
6869 if (tok != ')') {
6870 for(;;) {
6871 expr_eq();
6872 gfunc_param_typed(&gf, s, sa);
6873 if (sa)
6874 sa = sa->next;
6875 if (tok == ')')
6876 break;
6877 skip(',');
6880 #endif
6881 if (sa)
6882 error("too few arguments to function");
6883 skip(')');
6884 if (!nocode_wanted)
6885 gfunc_call(&gf);
6886 else
6887 vtop--;
6888 /* return value */
6889 vsetc(&ret.type, ret.r, &ret.c);
6890 vtop->r2 = ret.r2;
6891 } else {
6892 break;
6897 static void uneq(void)
6899 int t;
6901 unary();
6902 if (tok == '=' ||
6903 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
6904 tok == TOK_A_XOR || tok == TOK_A_OR ||
6905 tok == TOK_A_SHL || tok == TOK_A_SAR) {
6906 test_lvalue();
6907 t = tok;
6908 next();
6909 if (t == '=') {
6910 expr_eq();
6911 } else {
6912 vdup();
6913 expr_eq();
6914 gen_op(t & 0x7f);
6916 vstore();
6920 static void expr_prod(void)
6922 int t;
6924 uneq();
6925 while (tok == '*' || tok == '/' || tok == '%') {
6926 t = tok;
6927 next();
6928 uneq();
6929 gen_op(t);
6933 static void expr_sum(void)
6935 int t;
6937 expr_prod();
6938 while (tok == '+' || tok == '-') {
6939 t = tok;
6940 next();
6941 expr_prod();
6942 gen_op(t);
6946 static void expr_shift(void)
6948 int t;
6950 expr_sum();
6951 while (tok == TOK_SHL || tok == TOK_SAR) {
6952 t = tok;
6953 next();
6954 expr_sum();
6955 gen_op(t);
6959 static void expr_cmp(void)
6961 int t;
6963 expr_shift();
6964 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
6965 tok == TOK_ULT || tok == TOK_UGE) {
6966 t = tok;
6967 next();
6968 expr_shift();
6969 gen_op(t);
6973 static void expr_cmpeq(void)
6975 int t;
6977 expr_cmp();
6978 while (tok == TOK_EQ || tok == TOK_NE) {
6979 t = tok;
6980 next();
6981 expr_cmp();
6982 gen_op(t);
6986 static void expr_and(void)
6988 expr_cmpeq();
6989 while (tok == '&') {
6990 next();
6991 expr_cmpeq();
6992 gen_op('&');
6996 static void expr_xor(void)
6998 expr_and();
6999 while (tok == '^') {
7000 next();
7001 expr_and();
7002 gen_op('^');
7006 static void expr_or(void)
7008 expr_xor();
7009 while (tok == '|') {
7010 next();
7011 expr_xor();
7012 gen_op('|');
7016 /* XXX: fix this mess */
7017 static void expr_land_const(void)
7019 expr_or();
7020 while (tok == TOK_LAND) {
7021 next();
7022 expr_or();
7023 gen_op(TOK_LAND);
7027 /* XXX: fix this mess */
7028 static void expr_lor_const(void)
7030 expr_land_const();
7031 while (tok == TOK_LOR) {
7032 next();
7033 expr_land_const();
7034 gen_op(TOK_LOR);
7038 /* only used if non constant */
7039 static void expr_land(void)
7041 int t;
7043 expr_or();
7044 if (tok == TOK_LAND) {
7045 t = 0;
7046 for(;;) {
7047 t = gtst(1, t);
7048 if (tok != TOK_LAND) {
7049 vseti(VT_JMPI, t);
7050 break;
7052 next();
7053 expr_or();
7058 static void expr_lor(void)
7060 int t;
7062 expr_land();
7063 if (tok == TOK_LOR) {
7064 t = 0;
7065 for(;;) {
7066 t = gtst(0, t);
7067 if (tok != TOK_LOR) {
7068 vseti(VT_JMP, t);
7069 break;
7071 next();
7072 expr_land();
7077 /* XXX: better constant handling */
7078 static void expr_eq(void)
7080 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7081 SValue sv;
7082 CType type, type1, type2;
7084 if (const_wanted) {
7085 int c1, c;
7086 expr_lor_const();
7087 if (tok == '?') {
7088 c = vtop->c.i;
7089 vpop();
7090 next();
7091 if (tok == ':' && gnu_ext) {
7092 c1 = c;
7093 } else {
7094 gexpr();
7095 c1 = vtop->c.i;
7096 vpop();
7098 skip(':');
7099 expr_eq();
7100 if (c)
7101 vtop->c.i = c1;
7103 } else {
7104 expr_lor();
7105 if (tok == '?') {
7106 next();
7107 if (vtop != vstack) {
7108 /* needed to avoid having different registers saved in
7109 each branch */
7110 if (is_float(vtop->type.t))
7111 rc = RC_FLOAT;
7112 else
7113 rc = RC_INT;
7114 gv(rc);
7115 save_regs(1);
7117 if (tok == ':' && gnu_ext) {
7118 gv_dup();
7119 tt = gtst(1, 0);
7120 } else {
7121 tt = gtst(1, 0);
7122 gexpr();
7124 type1 = vtop->type;
7125 sv = *vtop; /* save value to handle it later */
7126 vtop--; /* no vpop so that FP stack is not flushed */
7127 skip(':');
7128 u = gjmp(0);
7129 gsym(tt);
7130 expr_eq();
7131 type2 = vtop->type;
7133 t1 = type1.t;
7134 bt1 = t1 & VT_BTYPE;
7135 t2 = type2.t;
7136 bt2 = t2 & VT_BTYPE;
7137 /* cast operands to correct type according to ISOC rules */
7138 if (is_float(bt1) || is_float(bt2)) {
7139 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7140 type.t = VT_LDOUBLE;
7141 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7142 type.t = VT_DOUBLE;
7143 } else {
7144 type.t = VT_FLOAT;
7146 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7147 /* cast to biggest op */
7148 type.t = VT_LLONG;
7149 /* convert to unsigned if it does not fit in a long long */
7150 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7151 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7152 type.t |= VT_UNSIGNED;
7153 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7154 /* XXX: test pointer compatibility */
7155 type = type1;
7156 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7157 /* XXX: test structure compatibility */
7158 type = type1;
7159 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7160 /* NOTE: as an extension, we accept void on only one side */
7161 type.t = VT_VOID;
7162 } else {
7163 /* integer operations */
7164 type.t = VT_INT;
7165 /* convert to unsigned if it does not fit in an integer */
7166 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7167 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7168 type.t |= VT_UNSIGNED;
7171 /* now we convert second operand */
7172 gen_cast(&type);
7173 rc = RC_INT;
7174 if (is_float(type.t)) {
7175 rc = RC_FLOAT;
7176 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7177 /* for long longs, we use fixed registers to avoid having
7178 to handle a complicated move */
7179 rc = RC_IRET;
7182 r2 = gv(rc);
7183 /* this is horrible, but we must also convert first
7184 operand */
7185 tt = gjmp(0);
7186 gsym(u);
7187 /* put again first value and cast it */
7188 *vtop = sv;
7189 gen_cast(&type);
7190 r1 = gv(rc);
7191 move_reg(r2, r1);
7192 vtop->r = r2;
7193 gsym(tt);
7198 static void gexpr(void)
7200 while (1) {
7201 expr_eq();
7202 if (tok != ',')
7203 break;
7204 vpop();
7205 next();
7209 /* parse an expression and return its type without any side effect. */
7210 static void expr_type(CType *type)
7212 int a;
7214 a = nocode_wanted;
7215 nocode_wanted = 1;
7216 gexpr();
7217 *type = vtop->type;
7218 vpop();
7219 nocode_wanted = a;
7222 /* parse a unary expression and return its type without any side
7223 effect. */
7224 static void unary_type(CType *type)
7226 int a;
7228 a = nocode_wanted;
7229 nocode_wanted = 1;
7230 unary();
7231 *type = vtop->type;
7232 vpop();
7233 nocode_wanted = a;
7236 /* parse a constant expression and return value in vtop. */
7237 static void expr_const1(void)
7239 int a;
7240 a = const_wanted;
7241 const_wanted = 1;
7242 expr_eq();
7243 const_wanted = a;
7246 /* parse an integer constant and return its value. */
7247 static int expr_const(void)
7249 int c;
7250 expr_const1();
7251 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7252 expect("constant expression");
7253 c = vtop->c.i;
7254 vpop();
7255 return c;
7258 /* return the label token if current token is a label, otherwise
7259 return zero */
7260 static int is_label(void)
7262 int last_tok;
7264 /* fast test first */
7265 if (tok < TOK_UIDENT)
7266 return 0;
7267 /* no need to save tokc because tok is an identifier */
7268 last_tok = tok;
7269 next();
7270 if (tok == ':') {
7271 next();
7272 return last_tok;
7273 } else {
7274 unget_tok(last_tok);
7275 return 0;
7279 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7280 int case_reg, int is_expr)
7282 int a, b, c, d;
7283 Sym *s;
7285 /* generate line number info */
7286 if (do_debug &&
7287 (last_line_num != file->line_num || last_ind != ind)) {
7288 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7289 last_ind = ind;
7290 last_line_num = file->line_num;
7293 if (is_expr) {
7294 /* default return value is (void) */
7295 vpushi(0);
7296 vtop->type.t = VT_VOID;
7299 if (tok == TOK_IF) {
7300 /* if test */
7301 next();
7302 skip('(');
7303 gexpr();
7304 skip(')');
7305 a = gtst(1, 0);
7306 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7307 c = tok;
7308 if (c == TOK_ELSE) {
7309 next();
7310 d = gjmp(0);
7311 gsym(a);
7312 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7313 gsym(d); /* patch else jmp */
7314 } else
7315 gsym(a);
7316 } else if (tok == TOK_WHILE) {
7317 next();
7318 d = ind;
7319 skip('(');
7320 gexpr();
7321 skip(')');
7322 a = gtst(1, 0);
7323 b = 0;
7324 block(&a, &b, case_sym, def_sym, case_reg, 0);
7325 gjmp_addr(d);
7326 gsym(a);
7327 gsym_addr(b, d);
7328 } else if (tok == '{') {
7329 Sym *llabel;
7331 next();
7332 /* record local declaration stack position */
7333 s = local_stack;
7334 llabel = local_label_stack;
7335 /* handle local labels declarations */
7336 if (tok == TOK_LABEL) {
7337 next();
7338 for(;;) {
7339 if (tok < TOK_UIDENT)
7340 expect("label identifier");
7341 label_push(&local_label_stack, tok, LABEL_DECLARED);
7342 next();
7343 if (tok == ',') {
7344 next();
7345 } else {
7346 skip(';');
7347 break;
7351 while (tok != '}') {
7352 decl(VT_LOCAL);
7353 if (tok != '}') {
7354 if (is_expr)
7355 vpop();
7356 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7359 /* pop locally defined labels */
7360 label_pop(&local_label_stack, llabel);
7361 /* pop locally defined symbols */
7362 sym_pop(&local_stack, s);
7363 next();
7364 } else if (tok == TOK_RETURN) {
7365 next();
7366 if (tok != ';') {
7367 gexpr();
7368 gen_assign_cast(&func_vt);
7369 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7370 CType type;
7371 /* if returning structure, must copy it to implicit
7372 first pointer arg location */
7373 type = func_vt;
7374 mk_pointer(&type);
7375 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7376 indir();
7377 vswap();
7378 /* copy structure value to pointer */
7379 vstore();
7380 } else if (is_float(func_vt.t)) {
7381 gv(RC_FRET);
7382 } else {
7383 gv(RC_IRET);
7385 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7387 skip(';');
7388 rsym = gjmp(rsym); /* jmp */
7389 } else if (tok == TOK_BREAK) {
7390 /* compute jump */
7391 if (!bsym)
7392 error("cannot break");
7393 *bsym = gjmp(*bsym);
7394 next();
7395 skip(';');
7396 } else if (tok == TOK_CONTINUE) {
7397 /* compute jump */
7398 if (!csym)
7399 error("cannot continue");
7400 *csym = gjmp(*csym);
7401 next();
7402 skip(';');
7403 } else if (tok == TOK_FOR) {
7404 int e;
7405 next();
7406 skip('(');
7407 if (tok != ';') {
7408 gexpr();
7409 vpop();
7411 skip(';');
7412 d = ind;
7413 c = ind;
7414 a = 0;
7415 b = 0;
7416 if (tok != ';') {
7417 gexpr();
7418 a = gtst(1, 0);
7420 skip(';');
7421 if (tok != ')') {
7422 e = gjmp(0);
7423 c = ind;
7424 gexpr();
7425 vpop();
7426 gjmp_addr(d);
7427 gsym(e);
7429 skip(')');
7430 block(&a, &b, case_sym, def_sym, case_reg, 0);
7431 gjmp_addr(c);
7432 gsym(a);
7433 gsym_addr(b, c);
7434 } else
7435 if (tok == TOK_DO) {
7436 next();
7437 a = 0;
7438 b = 0;
7439 d = ind;
7440 block(&a, &b, case_sym, def_sym, case_reg, 0);
7441 skip(TOK_WHILE);
7442 skip('(');
7443 gsym(b);
7444 gexpr();
7445 c = gtst(0, 0);
7446 gsym_addr(c, d);
7447 skip(')');
7448 gsym(a);
7449 skip(';');
7450 } else
7451 if (tok == TOK_SWITCH) {
7452 next();
7453 skip('(');
7454 gexpr();
7455 /* XXX: other types than integer */
7456 case_reg = gv(RC_INT);
7457 vpop();
7458 skip(')');
7459 a = 0;
7460 b = gjmp(0); /* jump to first case */
7461 c = 0;
7462 block(&a, csym, &b, &c, case_reg, 0);
7463 /* if no default, jmp after switch */
7464 if (c == 0)
7465 c = ind;
7466 /* default label */
7467 gsym_addr(b, c);
7468 /* break label */
7469 gsym(a);
7470 } else
7471 if (tok == TOK_CASE) {
7472 int v1, v2;
7473 if (!case_sym)
7474 expect("switch");
7475 next();
7476 v1 = expr_const();
7477 v2 = v1;
7478 if (gnu_ext && tok == TOK_DOTS) {
7479 next();
7480 v2 = expr_const();
7481 if (v2 < v1)
7482 warning("empty case range");
7484 /* since a case is like a label, we must skip it with a jmp */
7485 b = gjmp(0);
7486 gsym(*case_sym);
7487 vseti(case_reg, 0);
7488 vpushi(v1);
7489 if (v1 == v2) {
7490 gen_op(TOK_EQ);
7491 *case_sym = gtst(1, 0);
7492 } else {
7493 gen_op(TOK_GE);
7494 *case_sym = gtst(1, 0);
7495 vseti(case_reg, 0);
7496 vpushi(v2);
7497 gen_op(TOK_LE);
7498 *case_sym = gtst(1, *case_sym);
7500 gsym(b);
7501 skip(':');
7502 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7503 } else
7504 if (tok == TOK_DEFAULT) {
7505 next();
7506 skip(':');
7507 if (!def_sym)
7508 expect("switch");
7509 if (*def_sym)
7510 error("too many 'default'");
7511 *def_sym = ind;
7512 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7513 } else
7514 if (tok == TOK_GOTO) {
7515 next();
7516 if (tok == '*' && gnu_ext) {
7517 /* computed goto */
7518 next();
7519 gexpr();
7520 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7521 expect("pointer");
7522 ggoto();
7523 } else if (tok >= TOK_UIDENT) {
7524 s = label_find(tok);
7525 /* put forward definition if needed */
7526 if (!s) {
7527 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7528 } else {
7529 if (s->r == LABEL_DECLARED)
7530 s->r = LABEL_FORWARD;
7532 /* label already defined */
7533 if (s->r & LABEL_FORWARD)
7534 s->next = (void *)gjmp((long)s->next);
7535 else
7536 gjmp_addr((long)s->next);
7537 next();
7538 } else {
7539 expect("label identifier");
7541 skip(';');
7542 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7543 asm_instr();
7544 } else {
7545 b = is_label();
7546 if (b) {
7547 /* label case */
7548 s = label_find(b);
7549 if (s) {
7550 if (s->r == LABEL_DEFINED)
7551 error("duplicate label '%s'", get_tok_str(s->v, NULL));
7552 gsym((long)s->next);
7553 s->r = LABEL_DEFINED;
7554 } else {
7555 s = label_push(&global_label_stack, b, LABEL_DEFINED);
7557 s->next = (void *)ind;
7558 /* we accept this, but it is a mistake */
7559 if (tok == '}') {
7560 warning("deprecated use of label at end of compound statement");
7561 } else {
7562 if (is_expr)
7563 vpop();
7564 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7566 } else {
7567 /* expression case */
7568 if (tok != ';') {
7569 if (is_expr) {
7570 vpop();
7571 gexpr();
7572 } else {
7573 gexpr();
7574 vpop();
7577 skip(';');
7582 /* t is the array or struct type. c is the array or struct
7583 address. cur_index/cur_field is the pointer to the current
7584 value. 'size_only' is true if only size info is needed (only used
7585 in arrays) */
7586 static void decl_designator(CType *type, Section *sec, unsigned long c,
7587 int *cur_index, Sym **cur_field,
7588 int size_only)
7590 Sym *s, *f;
7591 int notfirst, index, index_last, align, l, nb_elems, elem_size;
7592 CType type1;
7594 notfirst = 0;
7595 elem_size = 0;
7596 nb_elems = 1;
7597 if (gnu_ext && (l = is_label()) != 0)
7598 goto struct_field;
7599 while (tok == '[' || tok == '.') {
7600 if (tok == '[') {
7601 if (!(type->t & VT_ARRAY))
7602 expect("array type");
7603 s = type->ref;
7604 next();
7605 index = expr_const();
7606 if (index < 0 || (s->c >= 0 && index >= s->c))
7607 expect("invalid index");
7608 if (tok == TOK_DOTS && gnu_ext) {
7609 next();
7610 index_last = expr_const();
7611 if (index_last < 0 ||
7612 (s->c >= 0 && index_last >= s->c) ||
7613 index_last < index)
7614 expect("invalid index");
7615 } else {
7616 index_last = index;
7618 skip(']');
7619 if (!notfirst)
7620 *cur_index = index_last;
7621 type = pointed_type(type);
7622 elem_size = type_size(type, &align);
7623 c += index * elem_size;
7624 /* NOTE: we only support ranges for last designator */
7625 nb_elems = index_last - index + 1;
7626 if (nb_elems != 1) {
7627 notfirst = 1;
7628 break;
7630 } else {
7631 next();
7632 l = tok;
7633 next();
7634 struct_field:
7635 if ((type->t & VT_BTYPE) != VT_STRUCT)
7636 expect("struct/union type");
7637 s = type->ref;
7638 l |= SYM_FIELD;
7639 f = s->next;
7640 while (f) {
7641 if (f->v == l)
7642 break;
7643 f = f->next;
7645 if (!f)
7646 expect("field");
7647 if (!notfirst)
7648 *cur_field = f;
7649 /* XXX: fix this mess by using explicit storage field */
7650 type1 = f->type;
7651 type1.t |= (type->t & ~VT_TYPE);
7652 type = &type1;
7653 c += f->c;
7655 notfirst = 1;
7657 if (notfirst) {
7658 if (tok == '=') {
7659 next();
7660 } else {
7661 if (!gnu_ext)
7662 expect("=");
7664 } else {
7665 if (type->t & VT_ARRAY) {
7666 index = *cur_index;
7667 type = pointed_type(type);
7668 c += index * type_size(type, &align);
7669 } else {
7670 f = *cur_field;
7671 if (!f)
7672 error("too many field init");
7673 /* XXX: fix this mess by using explicit storage field */
7674 type1 = f->type;
7675 type1.t |= (type->t & ~VT_TYPE);
7676 type = &type1;
7677 c += f->c;
7680 decl_initializer(type, sec, c, 0, size_only);
7682 /* XXX: make it more general */
7683 if (!size_only && nb_elems > 1) {
7684 unsigned long c_end;
7685 uint8_t *src, *dst;
7686 int i;
7688 if (!sec)
7689 error("range init not supported yet for dynamic storage");
7690 c_end = c + nb_elems * elem_size;
7691 if (c_end > sec->data_allocated)
7692 section_realloc(sec, c_end);
7693 src = sec->data + c;
7694 dst = src;
7695 for(i = 1; i < nb_elems; i++) {
7696 dst += elem_size;
7697 memcpy(dst, src, elem_size);
7702 #define EXPR_VAL 0
7703 #define EXPR_CONST 1
7704 #define EXPR_ANY 2
7706 /* store a value or an expression directly in global data or in local array */
7707 static void init_putv(CType *type, Section *sec, unsigned long c,
7708 int v, int expr_type)
7710 int saved_global_expr, bt, bit_pos, bit_size;
7711 void *ptr;
7712 unsigned long long bit_mask;
7714 switch(expr_type) {
7715 case EXPR_VAL:
7716 vpushi(v);
7717 break;
7718 case EXPR_CONST:
7719 /* compound literals must be allocated globally in this case */
7720 saved_global_expr = global_expr;
7721 global_expr = 1;
7722 expr_const1();
7723 global_expr = saved_global_expr;
7724 /* NOTE: symbols are accepted */
7725 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
7726 error("initializer element is not constant");
7727 break;
7728 case EXPR_ANY:
7729 expr_eq();
7730 break;
7733 if (sec) {
7734 /* XXX: not portable */
7735 /* XXX: generate error if incorrect relocation */
7736 gen_assign_cast(type);
7737 bt = type->t & VT_BTYPE;
7738 ptr = sec->data + c;
7739 /* XXX: make code faster ? */
7740 if (!(type->t & VT_BITFIELD)) {
7741 bit_pos = 0;
7742 bit_size = 32;
7743 bit_mask = -1LL;
7744 } else {
7745 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
7746 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
7747 bit_mask = (1LL << bit_size) - 1;
7749 if ((vtop->r & VT_SYM) &&
7750 (bt == VT_BYTE ||
7751 bt == VT_SHORT ||
7752 bt == VT_DOUBLE ||
7753 bt == VT_LDOUBLE ||
7754 bt == VT_LLONG ||
7755 (bt == VT_INT && bit_size != 32)))
7756 error("initializer element is not computable at load time");
7757 switch(bt) {
7758 case VT_BYTE:
7759 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7760 break;
7761 case VT_SHORT:
7762 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7763 break;
7764 case VT_DOUBLE:
7765 *(double *)ptr = vtop->c.d;
7766 break;
7767 case VT_LDOUBLE:
7768 *(long double *)ptr = vtop->c.ld;
7769 break;
7770 case VT_LLONG:
7771 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
7772 break;
7773 default:
7774 if (vtop->r & VT_SYM) {
7775 greloc(sec, vtop->sym, c, R_DATA_32);
7777 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7778 break;
7780 vtop--;
7781 } else {
7782 vset(type, VT_LOCAL, c);
7783 vswap();
7784 vstore();
7785 vpop();
7789 /* put zeros for variable based init */
7790 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
7792 GFuncContext gf;
7794 if (sec) {
7795 /* nothing to do because globals are already set to zero */
7796 } else {
7797 gfunc_start(&gf, FUNC_CDECL);
7798 vpushi(size);
7799 gfunc_param(&gf);
7800 vpushi(0);
7801 gfunc_param(&gf);
7802 vseti(VT_LOCAL, c);
7803 gfunc_param(&gf);
7804 vpush_global_sym(&func_old_type, TOK_memset);
7805 gfunc_call(&gf);
7809 /* 't' contains the type and storage info. 'c' is the offset of the
7810 object in section 'sec'. If 'sec' is NULL, it means stack based
7811 allocation. 'first' is true if array '{' must be read (multi
7812 dimension implicit array init handling). 'size_only' is true if
7813 size only evaluation is wanted (only for arrays). */
7814 static void decl_initializer(CType *type, Section *sec, unsigned long c,
7815 int first, int size_only)
7817 int index, array_length, n, no_oblock, nb, parlevel, i;
7818 int size1, align1, expr_type;
7819 Sym *s, *f;
7820 CType *t1;
7822 if (type->t & VT_ARRAY) {
7823 s = type->ref;
7824 n = s->c;
7825 array_length = 0;
7826 t1 = pointed_type(type);
7827 size1 = type_size(t1, &align1);
7829 no_oblock = 1;
7830 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
7831 tok == '{') {
7832 skip('{');
7833 no_oblock = 0;
7836 /* only parse strings here if correct type (otherwise: handle
7837 them as ((w)char *) expressions */
7838 if ((tok == TOK_LSTR &&
7839 (t1->t & VT_BTYPE) == VT_INT) ||
7840 (tok == TOK_STR &&
7841 (t1->t & VT_BTYPE) == VT_BYTE)) {
7842 while (tok == TOK_STR || tok == TOK_LSTR) {
7843 int cstr_len, ch;
7844 CString *cstr;
7846 cstr = tokc.cstr;
7847 /* compute maximum number of chars wanted */
7848 if (tok == TOK_STR)
7849 cstr_len = cstr->size;
7850 else
7851 cstr_len = cstr->size / sizeof(int);
7852 cstr_len--;
7853 nb = cstr_len;
7854 if (n >= 0 && nb > (n - array_length))
7855 nb = n - array_length;
7856 if (!size_only) {
7857 if (cstr_len > nb)
7858 warning("initializer-string for array is too long");
7859 /* in order to go faster for common case (char
7860 string in global variable, we handle it
7861 specifically */
7862 if (sec && tok == TOK_STR && size1 == 1) {
7863 memcpy(sec->data + c + array_length, cstr->data, nb);
7864 } else {
7865 for(i=0;i<nb;i++) {
7866 if (tok == TOK_STR)
7867 ch = ((unsigned char *)cstr->data)[i];
7868 else
7869 ch = ((int *)cstr->data)[i];
7870 init_putv(t1, sec, c + (array_length + i) * size1,
7871 ch, EXPR_VAL);
7875 array_length += nb;
7876 next();
7878 /* only add trailing zero if enough storage (no
7879 warning in this case since it is standard) */
7880 if (n < 0 || array_length < n) {
7881 if (!size_only) {
7882 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
7884 array_length++;
7886 } else {
7887 index = 0;
7888 while (tok != '}') {
7889 decl_designator(type, sec, c, &index, NULL, size_only);
7890 if (n >= 0 && index >= n)
7891 error("index too large");
7892 /* must put zero in holes (note that doing it that way
7893 ensures that it even works with designators) */
7894 if (!size_only && array_length < index) {
7895 init_putz(t1, sec, c + array_length * size1,
7896 (index - array_length) * size1);
7898 index++;
7899 if (index > array_length)
7900 array_length = index;
7901 /* special test for multi dimensional arrays (may not
7902 be strictly correct if designators are used at the
7903 same time) */
7904 if (index >= n && no_oblock)
7905 break;
7906 if (tok == '}')
7907 break;
7908 skip(',');
7911 if (!no_oblock)
7912 skip('}');
7913 /* put zeros at the end */
7914 if (!size_only && n >= 0 && array_length < n) {
7915 init_putz(t1, sec, c + array_length * size1,
7916 (n - array_length) * size1);
7918 /* patch type size if needed */
7919 if (n < 0)
7920 s->c = array_length;
7921 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
7922 (sec || !first || tok == '{')) {
7923 int par_count;
7925 /* NOTE: the previous test is a specific case for automatic
7926 struct/union init */
7927 /* XXX: union needs only one init */
7929 /* XXX: this test is incorrect for local initializers
7930 beginning with ( without {. It would be much more difficult
7931 to do it correctly (ideally, the expression parser should
7932 be used in all cases) */
7933 par_count = 0;
7934 if (tok == '(') {
7935 AttributeDef ad1;
7936 CType type1;
7937 next();
7938 while (tok == '(') {
7939 par_count++;
7940 next();
7942 if (!parse_btype(&type1, &ad1))
7943 expect("cast");
7944 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
7945 if (!is_compatible_types(type, &type1))
7946 error("invalid type for cast");
7947 skip(')');
7949 no_oblock = 1;
7950 if (first || tok == '{') {
7951 skip('{');
7952 no_oblock = 0;
7954 s = type->ref;
7955 f = s->next;
7956 array_length = 0;
7957 index = 0;
7958 n = s->c;
7959 while (tok != '}') {
7960 decl_designator(type, sec, c, NULL, &f, size_only);
7961 index = f->c;
7962 if (!size_only && array_length < index) {
7963 init_putz(type, sec, c + array_length,
7964 index - array_length);
7966 index = index + type_size(&f->type, &align1);
7967 if (index > array_length)
7968 array_length = index;
7969 f = f->next;
7970 if (no_oblock && f == NULL)
7971 break;
7972 if (tok == '}')
7973 break;
7974 skip(',');
7976 /* put zeros at the end */
7977 if (!size_only && array_length < n) {
7978 init_putz(type, sec, c + array_length,
7979 n - array_length);
7981 if (!no_oblock)
7982 skip('}');
7983 while (par_count) {
7984 skip(')');
7985 par_count--;
7987 } else if (tok == '{') {
7988 next();
7989 decl_initializer(type, sec, c, first, size_only);
7990 skip('}');
7991 } else if (size_only) {
7992 /* just skip expression */
7993 parlevel = 0;
7994 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
7995 tok != -1) {
7996 if (tok == '(')
7997 parlevel++;
7998 else if (tok == ')')
7999 parlevel--;
8000 next();
8002 } else {
8003 /* currently, we always use constant expression for globals
8004 (may change for scripting case) */
8005 expr_type = EXPR_CONST;
8006 if (!sec)
8007 expr_type = EXPR_ANY;
8008 init_putv(type, sec, c, 0, expr_type);
8012 /* parse an initializer for type 't' if 'has_init' is non zero, and
8013 allocate space in local or global data space ('r' is either
8014 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8015 variable 'v' of scope 'scope' is declared before initializers are
8016 parsed. If 'v' is zero, then a reference to the new object is put
8017 in the value stack. If 'has_init' is 2, a special parsing is done
8018 to handle string constants. */
8019 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8020 int has_init, int v, int scope)
8022 int size, align, addr, data_offset;
8023 int level;
8024 ParseState saved_parse_state;
8025 TokenString init_str;
8026 Section *sec;
8028 size = type_size(type, &align);
8029 /* If unknown size, we must evaluate it before
8030 evaluating initializers because
8031 initializers can generate global data too
8032 (e.g. string pointers or ISOC99 compound
8033 literals). It also simplifies local
8034 initializers handling */
8035 tok_str_new(&init_str);
8036 if (size < 0) {
8037 if (!has_init)
8038 error("unknown type size");
8039 /* get all init string */
8040 if (has_init == 2) {
8041 /* only get strings */
8042 while (tok == TOK_STR || tok == TOK_LSTR) {
8043 tok_str_add_tok(&init_str);
8044 next();
8046 } else {
8047 level = 0;
8048 while (level > 0 || (tok != ',' && tok != ';')) {
8049 if (tok < 0)
8050 error("unexpected end of file in initializer");
8051 tok_str_add_tok(&init_str);
8052 if (tok == '{')
8053 level++;
8054 else if (tok == '}') {
8055 if (level == 0)
8056 break;
8057 level--;
8059 next();
8062 tok_str_add(&init_str, -1);
8063 tok_str_add(&init_str, 0);
8065 /* compute size */
8066 save_parse_state(&saved_parse_state);
8068 macro_ptr = init_str.str;
8069 next();
8070 decl_initializer(type, NULL, 0, 1, 1);
8071 /* prepare second initializer parsing */
8072 macro_ptr = init_str.str;
8073 next();
8075 /* if still unknown size, error */
8076 size = type_size(type, &align);
8077 if (size < 0)
8078 error("unknown type size");
8080 /* take into account specified alignment if bigger */
8081 if (ad->aligned > align)
8082 align = ad->aligned;
8083 if ((r & VT_VALMASK) == VT_LOCAL) {
8084 sec = NULL;
8085 if (do_bounds_check && (type->t & VT_ARRAY))
8086 loc--;
8087 loc = (loc - size) & -align;
8088 addr = loc;
8089 /* handles bounds */
8090 /* XXX: currently, since we do only one pass, we cannot track
8091 '&' operators, so we add only arrays */
8092 if (do_bounds_check && (type->t & VT_ARRAY)) {
8093 unsigned long *bounds_ptr;
8094 /* add padding between regions */
8095 loc--;
8096 /* then add local bound info */
8097 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8098 bounds_ptr[0] = addr;
8099 bounds_ptr[1] = size;
8101 if (v) {
8102 /* local variable */
8103 sym_push(v, type, r, addr);
8104 } else {
8105 /* push local reference */
8106 vset(type, r, addr);
8108 } else {
8109 Sym *sym;
8111 sym = NULL;
8112 if (v && scope == VT_CONST) {
8113 /* see if the symbol was already defined */
8114 sym = sym_find(v);
8115 if (sym) {
8116 if (!is_compatible_types(&sym->type, type))
8117 error("incompatible types for redefinition of '%s'",
8118 get_tok_str(v, NULL));
8119 if (sym->type.t & VT_EXTERN) {
8120 /* if the variable is extern, it was not allocated */
8121 sym->type.t &= ~VT_EXTERN;
8122 } else {
8123 /* we accept several definitions of the same
8124 global variable. this is tricky, because we
8125 must play with the SHN_COMMON type of the symbol */
8126 /* XXX: should check if the variable was already
8127 initialized. It is incorrect to initialized it
8128 twice */
8129 /* no init data, we won't add more to the symbol */
8130 if (!has_init)
8131 goto no_alloc;
8136 /* allocate symbol in corresponding section */
8137 sec = ad->section;
8138 if (!sec) {
8139 if (has_init)
8140 sec = data_section;
8142 if (sec) {
8143 data_offset = sec->data_offset;
8144 data_offset = (data_offset + align - 1) & -align;
8145 addr = data_offset;
8146 /* very important to increment global pointer at this time
8147 because initializers themselves can create new initializers */
8148 data_offset += size;
8149 /* add padding if bound check */
8150 if (do_bounds_check)
8151 data_offset++;
8152 sec->data_offset = data_offset;
8153 /* allocate section space to put the data */
8154 if (sec->sh_type != SHT_NOBITS &&
8155 data_offset > sec->data_allocated)
8156 section_realloc(sec, data_offset);
8157 } else {
8158 addr = 0; /* avoid warning */
8161 if (v) {
8162 if (scope == VT_CONST) {
8163 if (!sym)
8164 goto do_def;
8165 } else {
8166 do_def:
8167 sym = sym_push(v, type, r | VT_SYM, 0);
8169 /* update symbol definition */
8170 if (sec) {
8171 put_extern_sym(sym, sec, addr, size);
8172 } else {
8173 Elf32_Sym *esym;
8174 /* put a common area */
8175 put_extern_sym(sym, NULL, align, size);
8176 /* XXX: find a nicer way */
8177 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8178 esym->st_shndx = SHN_COMMON;
8180 } else {
8181 CValue cval;
8183 /* push global reference */
8184 sym = get_sym_ref(type, sec, addr, size);
8185 cval.ul = 0;
8186 vsetc(type, VT_CONST | VT_SYM, &cval);
8187 vtop->sym = sym;
8190 /* handles bounds now because the symbol must be defined
8191 before for the relocation */
8192 if (do_bounds_check) {
8193 unsigned long *bounds_ptr;
8195 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8196 /* then add global bound info */
8197 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8198 bounds_ptr[0] = 0; /* relocated */
8199 bounds_ptr[1] = size;
8202 if (has_init) {
8203 decl_initializer(type, sec, addr, 1, 0);
8204 /* restore parse state if needed */
8205 if (init_str.str) {
8206 tok_str_free(init_str.str);
8207 restore_parse_state(&saved_parse_state);
8210 no_alloc: ;
8213 void put_func_debug(Sym *sym)
8215 char buf[512];
8217 /* stabs info */
8218 /* XXX: we put here a dummy type */
8219 snprintf(buf, sizeof(buf), "%s:%c1",
8220 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8221 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8222 cur_text_section, sym->c);
8223 last_ind = 0;
8224 last_line_num = 0;
8227 /* not finished : try to put some local vars in registers */
8228 //#define CONFIG_REG_VARS
8230 #ifdef CONFIG_REG_VARS
8231 void add_var_ref(int t)
8233 printf("%s:%d: &%s\n",
8234 file->filename, file->line_num,
8235 get_tok_str(t, NULL));
8238 /* first pass on a function with heuristic to extract variable usage
8239 and pointer references to local variables for register allocation */
8240 void analyse_function(void)
8242 int level, t;
8244 for(;;) {
8245 if (tok == -1)
8246 break;
8247 /* any symbol coming after '&' is considered as being a
8248 variable whose reference is taken. It is highly unaccurate
8249 but it is difficult to do better without a complete parse */
8250 if (tok == '&') {
8251 next();
8252 /* if '& number', then no need to examine next tokens */
8253 if (tok == TOK_CINT ||
8254 tok == TOK_CUINT ||
8255 tok == TOK_CLLONG ||
8256 tok == TOK_CULLONG) {
8257 continue;
8258 } else if (tok >= TOK_UIDENT) {
8259 /* if '& ident [' or '& ident ->', then ident address
8260 is not needed */
8261 t = tok;
8262 next();
8263 if (tok != '[' && tok != TOK_ARROW)
8264 add_var_ref(t);
8265 } else {
8266 level = 0;
8267 while (tok != '}' && tok != ';' &&
8268 !((tok == ',' || tok == ')') && level == 0)) {
8269 if (tok >= TOK_UIDENT) {
8270 add_var_ref(tok);
8271 } else if (tok == '(') {
8272 level++;
8273 } else if (tok == ')') {
8274 level--;
8276 next();
8279 } else {
8280 next();
8284 #endif
8286 /* parse an old style function declaration list */
8287 /* XXX: check multiple parameter */
8288 static void func_decl_list(Sym *func_sym)
8290 AttributeDef ad;
8291 int v;
8292 Sym *s;
8293 CType btype, type;
8295 /* parse each declaration */
8296 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8297 if (!parse_btype(&btype, &ad))
8298 expect("declaration list");
8299 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8300 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8301 tok == ';') {
8302 /* we accept no variable after */
8303 } else {
8304 for(;;) {
8305 type = btype;
8306 type_decl(&type, &ad, &v, TYPE_DIRECT);
8307 /* find parameter in function parameter list */
8308 s = func_sym->next;
8309 while (s != NULL) {
8310 if ((s->v & ~SYM_FIELD) == v)
8311 goto found;
8312 s = s->next;
8314 error("declaration for parameter '%s' but no such parameter",
8315 get_tok_str(v, NULL));
8316 found:
8317 /* check that no storage specifier except 'register' was given */
8318 if (type.t & VT_STORAGE)
8319 error("storage class specified for '%s'", get_tok_str(v, NULL));
8320 convert_parameter_type(&type);
8321 /* we can add the type (NOTE: it could be local to the function) */
8322 s->type = type;
8323 /* accept other parameters */
8324 if (tok == ',')
8325 next();
8326 else
8327 break;
8330 skip(';');
8334 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8335 static void decl(int l)
8337 int v, has_init, r;
8338 CType type, btype;
8339 Sym *sym;
8340 AttributeDef ad;
8342 while (1) {
8343 if (!parse_btype(&btype, &ad)) {
8344 /* skip redundant ';' */
8345 /* XXX: find more elegant solution */
8346 if (tok == ';') {
8347 next();
8348 continue;
8350 /* special test for old K&R protos without explicit int
8351 type. Only accepted when defining global data */
8352 if (l == VT_LOCAL || tok < TOK_DEFINE)
8353 break;
8354 btype.t = VT_INT;
8356 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8357 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8358 tok == ';') {
8359 /* we accept no variable after */
8360 next();
8361 continue;
8363 while (1) { /* iterate thru each declaration */
8364 type = btype;
8365 type_decl(&type, &ad, &v, TYPE_DIRECT);
8366 #if 0
8368 char buf[500];
8369 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8370 printf("type = '%s'\n", buf);
8372 #endif
8373 if ((type.t & VT_BTYPE) == VT_FUNC) {
8374 /* if old style function prototype, we accept a
8375 declaration list */
8376 sym = type.ref;
8377 if (sym->c == FUNC_OLD)
8378 func_decl_list(sym);
8381 if (tok == '{') {
8382 #ifdef CONFIG_REG_VARS
8383 TokenString func_str;
8384 ParseState saved_parse_state;
8385 int block_level;
8386 #endif
8388 if (l == VT_LOCAL)
8389 error("cannot use local functions");
8390 if (!(type.t & VT_FUNC))
8391 expect("function definition");
8392 /* XXX: cannot do better now: convert extern line to static inline */
8393 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8394 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8396 #ifdef CONFIG_REG_VARS
8397 /* parse all function code and record it */
8399 tok_str_new(&func_str);
8401 block_level = 0;
8402 for(;;) {
8403 int t;
8404 if (tok == -1)
8405 error("unexpected end of file");
8406 tok_str_add_tok(&func_str);
8407 t = tok;
8408 next();
8409 if (t == '{') {
8410 block_level++;
8411 } else if (t == '}') {
8412 block_level--;
8413 if (block_level == 0)
8414 break;
8417 tok_str_add(&func_str, -1);
8418 tok_str_add(&func_str, 0);
8420 save_parse_state(&saved_parse_state);
8422 macro_ptr = func_str.str;
8423 next();
8424 analyse_function();
8425 #endif
8427 /* compute text section */
8428 cur_text_section = ad.section;
8429 if (!cur_text_section)
8430 cur_text_section = text_section;
8431 ind = cur_text_section->data_offset;
8432 funcname = get_tok_str(v, NULL);
8433 sym = sym_find(v);
8434 if (sym) {
8435 /* if symbol is already defined, then put complete type */
8436 sym->type = type;
8437 } else {
8438 /* put function symbol */
8439 sym = global_identifier_push(v, type.t, 0);
8440 sym->type.ref = type.ref;
8442 /* NOTE: we patch the symbol size later */
8443 put_extern_sym(sym, cur_text_section, ind, 0);
8444 func_ind = ind;
8445 sym->r = VT_SYM | VT_CONST;
8446 /* put debug symbol */
8447 if (do_debug)
8448 put_func_debug(sym);
8449 /* push a dummy symbol to enable local sym storage */
8450 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8451 gfunc_prolog(&type);
8452 loc = 0;
8453 rsym = 0;
8454 #ifdef CONFIG_REG_VARS
8455 macro_ptr = func_str.str;
8456 next();
8457 #endif
8458 block(NULL, NULL, NULL, NULL, 0, 0);
8459 gsym(rsym);
8460 gfunc_epilog();
8461 cur_text_section->data_offset = ind;
8462 label_pop(&global_label_stack, NULL);
8463 sym_pop(&local_stack, NULL); /* reset local stack */
8464 /* end of function */
8465 /* patch symbol size */
8466 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8467 ind - func_ind;
8468 if (do_debug) {
8469 put_stabn(N_FUN, 0, 0, ind - func_ind);
8471 funcname = ""; /* for safety */
8472 func_vt.t = VT_VOID; /* for safety */
8473 ind = 0; /* for safety */
8475 #ifdef CONFIG_REG_VARS
8476 tok_str_free(func_str.str);
8477 restore_parse_state(&saved_parse_state);
8478 #endif
8479 break;
8480 } else {
8481 if (btype.t & VT_TYPEDEF) {
8482 /* save typedefed type */
8483 /* XXX: test storage specifiers ? */
8484 sym = sym_push(v, &type, 0, 0);
8485 sym->type.t |= VT_TYPEDEF;
8486 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
8487 /* external function definition */
8488 external_sym(v, &type, 0);
8489 } else {
8490 /* not lvalue if array */
8491 r = 0;
8492 if (!(type.t & VT_ARRAY))
8493 r |= lvalue_type(type.t);
8494 has_init = (tok == '=');
8495 if ((btype.t & VT_EXTERN) ||
8496 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
8497 !has_init && l == VT_CONST && type.ref->c < 0)) {
8498 /* external variable */
8499 /* NOTE: as GCC, uninitialized global static
8500 arrays of null size are considered as
8501 extern */
8502 external_sym(v, &type, r);
8503 } else {
8504 if (type.t & VT_STATIC)
8505 r |= VT_CONST;
8506 else
8507 r |= l;
8508 if (has_init)
8509 next();
8510 decl_initializer_alloc(&type, &ad, r,
8511 has_init, v, l);
8514 if (tok != ',') {
8515 skip(';');
8516 break;
8518 next();
8524 /* better than nothing, but needs extension to handle '-E' option
8525 correctly too */
8526 static void preprocess_init(TCCState *s1)
8528 s1->include_stack_ptr = s1->include_stack;
8529 /* XXX: move that before to avoid having to initialize
8530 file->ifdef_stack_ptr ? */
8531 s1->ifdef_stack_ptr = s1->ifdef_stack;
8532 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
8534 /* XXX: not ANSI compliant: bound checking says error */
8535 vtop = vstack - 1;
8538 /* compile the C file opened in 'file'. Return non zero if errors. */
8539 static int tcc_compile(TCCState *s1)
8541 Sym *define_start;
8542 char buf[512];
8543 volatile int section_sym;
8545 #ifdef INC_DEBUG
8546 printf("%s: **** new file\n", file->filename);
8547 #endif
8548 preprocess_init(s1);
8550 funcname = "";
8551 anon_sym = SYM_FIRST_ANOM;
8553 /* file info: full path + filename */
8554 section_sym = 0; /* avoid warning */
8555 if (do_debug) {
8556 section_sym = put_elf_sym(symtab_section, 0, 0,
8557 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
8558 text_section->sh_num, NULL);
8559 getcwd(buf, sizeof(buf));
8560 pstrcat(buf, sizeof(buf), "/");
8561 put_stabs_r(buf, N_SO, 0, 0,
8562 text_section->data_offset, text_section, section_sym);
8563 put_stabs_r(file->filename, N_SO, 0, 0,
8564 text_section->data_offset, text_section, section_sym);
8566 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8567 symbols can be safely used */
8568 put_elf_sym(symtab_section, 0, 0,
8569 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
8570 SHN_ABS, file->filename);
8572 /* define some often used types */
8573 int_type.t = VT_INT;
8575 char_pointer_type.t = VT_BYTE;
8576 mk_pointer(&char_pointer_type);
8578 func_old_type.t = VT_FUNC;
8579 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
8581 #if 0
8582 /* define 'void *alloca(unsigned int)' builtin function */
8584 Sym *s1;
8586 p = anon_sym++;
8587 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
8588 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
8589 s1->next = NULL;
8590 sym->next = s1;
8591 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
8593 #endif
8595 define_start = define_stack;
8597 if (setjmp(s1->error_jmp_buf) == 0) {
8598 s1->nb_errors = 0;
8599 s1->error_set_jmp_enabled = 1;
8601 ch = file->buf_ptr[0];
8602 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
8603 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
8604 next();
8605 decl(VT_CONST);
8606 if (tok != -1)
8607 expect("declaration");
8609 /* end of translation unit info */
8610 if (do_debug) {
8611 put_stabs_r(NULL, N_SO, 0, 0,
8612 text_section->data_offset, text_section, section_sym);
8615 s1->error_set_jmp_enabled = 0;
8617 /* reset define stack, but leave -Dsymbols (may be incorrect if
8618 they are undefined) */
8619 free_defines(define_start);
8621 sym_pop(&global_stack, NULL);
8623 return s1->nb_errors != 0 ? -1 : 0;
8626 #ifdef LIBTCC
8627 int tcc_compile_string(TCCState *s, const char *str)
8629 BufferedFile bf1, *bf = &bf1;
8630 int ret, len;
8631 char *buf;
8633 /* init file structure */
8634 bf->fd = -1;
8635 /* XXX: avoid copying */
8636 len = strlen(str);
8637 buf = tcc_malloc(len + 1);
8638 if (!buf)
8639 return -1;
8640 memcpy(buf, str, len);
8641 buf[len] = CH_EOB;
8642 bf->buf_ptr = buf;
8643 bf->buf_end = buf + len;
8644 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
8645 bf->line_num = 1;
8646 file = bf;
8648 ret = tcc_compile(s);
8650 tcc_free(buf);
8652 /* currently, no need to close */
8653 return ret;
8655 #endif
8657 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8658 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
8660 BufferedFile bf1, *bf = &bf1;
8662 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
8663 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
8664 /* default value */
8665 if (!value)
8666 value = "1";
8667 pstrcat(bf->buffer, IO_BUF_SIZE, value);
8669 /* init file structure */
8670 bf->fd = -1;
8671 bf->buf_ptr = bf->buffer;
8672 bf->buf_end = bf->buffer + strlen(bf->buffer);
8673 *bf->buf_end = CH_EOB;
8674 bf->filename[0] = '\0';
8675 bf->line_num = 1;
8676 file = bf;
8678 s1->include_stack_ptr = s1->include_stack;
8680 /* parse with define parser */
8681 ch = file->buf_ptr[0];
8682 next_nomacro();
8683 parse_define();
8684 file = NULL;
8687 /* undefine a preprocessor symbol */
8688 void tcc_undefine_symbol(TCCState *s1, const char *sym)
8690 TokenSym *ts;
8691 Sym *s;
8692 ts = tok_alloc(sym, strlen(sym));
8693 s = define_find(ts->tok);
8694 /* undefine symbol by putting an invalid name */
8695 if (s)
8696 define_undef(s);
8699 #ifdef CONFIG_TCC_ASM
8701 #include "i386-asm.c"
8702 #include "tccasm.c"
8704 #else
8705 static void asm_instr(void)
8707 error("inline asm() not supported");
8709 #endif
8711 #include "tccelf.c"
8713 /* print the position in the source file of PC value 'pc' by reading
8714 the stabs debug information */
8715 static void rt_printline(unsigned long wanted_pc)
8717 Stab_Sym *sym, *sym_end;
8718 char func_name[128], last_func_name[128];
8719 unsigned long func_addr, last_pc, pc;
8720 const char *incl_files[INCLUDE_STACK_SIZE];
8721 int incl_index, len, last_line_num, i;
8722 const char *str, *p;
8724 fprintf(stderr, "0x%08lx:", wanted_pc);
8726 func_name[0] = '\0';
8727 func_addr = 0;
8728 incl_index = 0;
8729 last_func_name[0] = '\0';
8730 last_pc = 0xffffffff;
8731 last_line_num = 1;
8732 sym = (Stab_Sym *)stab_section->data + 1;
8733 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
8734 while (sym < sym_end) {
8735 switch(sym->n_type) {
8736 /* function start or end */
8737 case N_FUN:
8738 if (sym->n_strx == 0) {
8739 /* we test if between last line and end of function */
8740 pc = sym->n_value + func_addr;
8741 if (wanted_pc >= last_pc && wanted_pc < pc)
8742 goto found;
8743 func_name[0] = '\0';
8744 func_addr = 0;
8745 } else {
8746 str = stabstr_section->data + sym->n_strx;
8747 p = strchr(str, ':');
8748 if (!p) {
8749 pstrcpy(func_name, sizeof(func_name), str);
8750 } else {
8751 len = p - str;
8752 if (len > sizeof(func_name) - 1)
8753 len = sizeof(func_name) - 1;
8754 memcpy(func_name, str, len);
8755 func_name[len] = '\0';
8757 func_addr = sym->n_value;
8759 break;
8760 /* line number info */
8761 case N_SLINE:
8762 pc = sym->n_value + func_addr;
8763 if (wanted_pc >= last_pc && wanted_pc < pc)
8764 goto found;
8765 last_pc = pc;
8766 last_line_num = sym->n_desc;
8767 /* XXX: slow! */
8768 strcpy(last_func_name, func_name);
8769 break;
8770 /* include files */
8771 case N_BINCL:
8772 str = stabstr_section->data + sym->n_strx;
8773 add_incl:
8774 if (incl_index < INCLUDE_STACK_SIZE) {
8775 incl_files[incl_index++] = str;
8777 break;
8778 case N_EINCL:
8779 if (incl_index > 1)
8780 incl_index--;
8781 break;
8782 case N_SO:
8783 if (sym->n_strx == 0) {
8784 incl_index = 0; /* end of translation unit */
8785 } else {
8786 str = stabstr_section->data + sym->n_strx;
8787 /* do not add path */
8788 len = strlen(str);
8789 if (len > 0 && str[len - 1] != '/')
8790 goto add_incl;
8792 break;
8794 sym++;
8797 /* second pass: we try symtab symbols (no line number info) */
8798 incl_index = 0;
8800 Elf32_Sym *sym, *sym_end;
8801 int type;
8803 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
8804 for(sym = (Elf32_Sym *)symtab_section->data + 1;
8805 sym < sym_end;
8806 sym++) {
8807 type = ELF32_ST_TYPE(sym->st_info);
8808 if (type == STT_FUNC) {
8809 if (wanted_pc >= sym->st_value &&
8810 wanted_pc < sym->st_value + sym->st_size) {
8811 pstrcpy(last_func_name, sizeof(last_func_name),
8812 strtab_section->data + sym->st_name);
8813 goto found;
8818 /* did not find any info: */
8819 fprintf(stderr, " ???\n");
8820 return;
8821 found:
8822 if (last_func_name[0] != '\0') {
8823 fprintf(stderr, " %s()", last_func_name);
8825 if (incl_index > 0) {
8826 fprintf(stderr, " (%s:%d",
8827 incl_files[incl_index - 1], last_line_num);
8828 for(i = incl_index - 2; i >= 0; i--)
8829 fprintf(stderr, ", included from %s", incl_files[i]);
8830 fprintf(stderr, ")");
8832 fprintf(stderr, "\n");
8835 #ifndef WIN32
8837 #ifdef __i386__
8839 /* fix for glibc 2.1 */
8840 #ifndef REG_EIP
8841 #define REG_EIP EIP
8842 #define REG_EBP EBP
8843 #endif
8845 /* return the PC at frame level 'level'. Return non zero if not found */
8846 static int rt_get_caller_pc(unsigned long *paddr,
8847 ucontext_t *uc, int level)
8849 unsigned long fp;
8850 int i;
8852 if (level == 0) {
8853 #ifdef __FreeBSD__
8854 *paddr = uc->uc_mcontext.mc_eip;
8855 #else
8856 *paddr = uc->uc_mcontext.gregs[REG_EIP];
8857 #endif
8858 return 0;
8859 } else {
8860 #ifdef __FreeBSD__
8861 fp = uc->uc_mcontext.mc_ebp;
8862 #else
8863 fp = uc->uc_mcontext.gregs[REG_EBP];
8864 #endif
8865 for(i=1;i<level;i++) {
8866 /* XXX: check address validity with program info */
8867 if (fp <= 0x1000 || fp >= 0xc0000000)
8868 return -1;
8869 fp = ((unsigned long *)fp)[0];
8871 *paddr = ((unsigned long *)fp)[1];
8872 return 0;
8875 #else
8876 #error add arch specific rt_get_caller_pc()
8877 #endif
8879 /* emit a run time error at position 'pc' */
8880 void rt_error(ucontext_t *uc, const char *fmt, ...)
8882 va_list ap;
8883 unsigned long pc;
8884 int i;
8886 va_start(ap, fmt);
8887 fprintf(stderr, "Runtime error: ");
8888 vfprintf(stderr, fmt, ap);
8889 fprintf(stderr, "\n");
8890 for(i=0;i<num_callers;i++) {
8891 if (rt_get_caller_pc(&pc, uc, i) < 0)
8892 break;
8893 if (i == 0)
8894 fprintf(stderr, "at ");
8895 else
8896 fprintf(stderr, "by ");
8897 rt_printline(pc);
8899 exit(255);
8900 va_end(ap);
8903 /* signal handler for fatal errors */
8904 static void sig_error(int signum, siginfo_t *siginf, void *puc)
8906 ucontext_t *uc = puc;
8908 switch(signum) {
8909 case SIGFPE:
8910 switch(siginf->si_code) {
8911 case FPE_INTDIV:
8912 case FPE_FLTDIV:
8913 rt_error(uc, "division by zero");
8914 break;
8915 default:
8916 rt_error(uc, "floating point exception");
8917 break;
8919 break;
8920 case SIGBUS:
8921 case SIGSEGV:
8922 if (rt_bound_error_msg && *rt_bound_error_msg)
8923 rt_error(uc, *rt_bound_error_msg);
8924 else
8925 rt_error(uc, "dereferencing invalid pointer");
8926 break;
8927 case SIGILL:
8928 rt_error(uc, "illegal instruction");
8929 break;
8930 case SIGABRT:
8931 rt_error(uc, "abort() called");
8932 break;
8933 default:
8934 rt_error(uc, "caught signal %d", signum);
8935 break;
8937 exit(255);
8939 #endif
8941 /* do all relocations (needed before using tcc_get_symbol()) */
8942 int tcc_relocate(TCCState *s1)
8944 Section *s;
8945 int i;
8947 s1->nb_errors = 0;
8949 tcc_add_runtime(s1);
8951 relocate_common_syms();
8953 /* compute relocation address : section are relocated in place. We
8954 also alloc the bss space */
8955 for(i = 1; i < s1->nb_sections; i++) {
8956 s = s1->sections[i];
8957 if (s->sh_flags & SHF_ALLOC) {
8958 if (s->sh_type == SHT_NOBITS)
8959 s->data = tcc_mallocz(s->data_offset);
8960 s->sh_addr = (unsigned long)s->data;
8964 relocate_syms(s1, 1);
8966 if (s1->nb_errors != 0)
8967 return -1;
8969 /* relocate each section */
8970 for(i = 1; i < s1->nb_sections; i++) {
8971 s = s1->sections[i];
8972 if (s->reloc)
8973 relocate_section(s1, s);
8975 return 0;
8978 /* launch the compiled program with the given arguments */
8979 int tcc_run(TCCState *s1, int argc, char **argv)
8981 int (*prog_main)(int, char **);
8983 if (tcc_relocate(s1) < 0)
8984 return -1;
8986 prog_main = tcc_get_symbol(s1, "main");
8988 if (do_debug) {
8989 #ifdef WIN32
8990 error("debug mode currently not available for Windows");
8991 #else
8992 struct sigaction sigact;
8993 /* install TCC signal handlers to print debug info on fatal
8994 runtime errors */
8995 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
8996 sigact.sa_sigaction = sig_error;
8997 sigemptyset(&sigact.sa_mask);
8998 sigaction(SIGFPE, &sigact, NULL);
8999 sigaction(SIGILL, &sigact, NULL);
9000 sigaction(SIGSEGV, &sigact, NULL);
9001 sigaction(SIGBUS, &sigact, NULL);
9002 sigaction(SIGABRT, &sigact, NULL);
9003 #endif
9006 #ifdef CONFIG_TCC_BCHECK
9007 if (do_bounds_check) {
9008 void (*bound_init)(void);
9010 /* set error function */
9011 rt_bound_error_msg = (void *)tcc_get_symbol(s1, "__bound_error_msg");
9013 /* XXX: use .init section so that it also work in binary ? */
9014 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
9015 bound_init();
9017 #endif
9018 return (*prog_main)(argc, argv);
9021 TCCState *tcc_new(void)
9023 const char *p, *r;
9024 TCCState *s;
9025 TokenSym *ts;
9026 int i, c;
9028 s = tcc_mallocz(sizeof(TCCState));
9029 if (!s)
9030 return NULL;
9031 tcc_state = s;
9032 s->output_type = TCC_OUTPUT_MEMORY;
9034 /* init isid table */
9035 for(i=0;i<256;i++)
9036 isidnum_table[i] = isid(i) || isnum(i);
9038 /* add all tokens */
9039 table_ident = NULL;
9040 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9042 tok_ident = TOK_IDENT;
9043 p = tcc_keywords;
9044 while (*p) {
9045 r = p;
9046 for(;;) {
9047 c = *r++;
9048 if (c == '\0')
9049 break;
9051 ts = tok_alloc(p, r - p - 1);
9052 p = r;
9055 /* we add dummy defines for some special macros to speed up tests
9056 and to have working defined() */
9057 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9058 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9059 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9060 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9062 /* standard defines */
9063 tcc_define_symbol(s, "__STDC__", NULL);
9064 #if defined(TCC_TARGET_I386)
9065 tcc_define_symbol(s, "__i386__", NULL);
9066 #endif
9067 #if defined(linux)
9068 tcc_define_symbol(s, "__linux__", NULL);
9069 tcc_define_symbol(s, "linux", NULL);
9070 #endif
9071 /* tiny C specific defines */
9072 tcc_define_symbol(s, "__TINYC__", NULL);
9074 /* tiny C & gcc defines */
9075 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9076 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9077 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9079 /* default library paths */
9080 tcc_add_library_path(s, "/usr/local/lib");
9081 tcc_add_library_path(s, "/usr/lib");
9082 tcc_add_library_path(s, "/lib");
9084 /* no section zero */
9085 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9087 /* create standard sections */
9088 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9089 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9090 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9092 /* symbols are always generated for linking stage */
9093 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9094 ".strtab",
9095 ".hashtab", SHF_PRIVATE);
9096 strtab_section = symtab_section->link;
9098 /* private symbol table for dynamic symbols */
9099 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9100 ".dynstrtab",
9101 ".dynhashtab", SHF_PRIVATE);
9102 return s;
9105 void tcc_delete(TCCState *s1)
9107 int i, n;
9109 /* free -D defines */
9110 free_defines(NULL);
9112 /* free tokens */
9113 n = tok_ident - TOK_IDENT;
9114 for(i = 0; i < n; i++)
9115 tcc_free(table_ident[i]);
9116 tcc_free(table_ident);
9118 /* free all sections */
9120 free_section(symtab_section->hash);
9122 free_section(s1->dynsymtab_section->hash);
9123 free_section(s1->dynsymtab_section->link);
9124 free_section(s1->dynsymtab_section);
9126 for(i = 1; i < s1->nb_sections; i++)
9127 free_section(s1->sections[i]);
9128 tcc_free(s1->sections);
9130 /* free loaded dlls array */
9131 for(i = 0; i < s1->nb_loaded_dlls; i++)
9132 tcc_free(s1->loaded_dlls[i]);
9133 tcc_free(s1->loaded_dlls);
9135 /* library paths */
9136 for(i = 0; i < s1->nb_library_paths; i++)
9137 tcc_free(s1->library_paths[i]);
9138 tcc_free(s1->library_paths);
9140 /* cached includes */
9141 for(i = 0; i < s1->nb_cached_includes; i++)
9142 tcc_free(s1->cached_includes[i]);
9143 tcc_free(s1->cached_includes);
9145 for(i = 0; i < s1->nb_include_paths; i++)
9146 tcc_free(s1->include_paths[i]);
9147 tcc_free(s1->include_paths);
9149 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9150 tcc_free(s1->sysinclude_paths[i]);
9151 tcc_free(s1->sysinclude_paths);
9153 tcc_free(s1);
9156 int tcc_add_include_path(TCCState *s1, const char *pathname)
9158 char *pathname1;
9160 pathname1 = tcc_strdup(pathname);
9161 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9162 return 0;
9165 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9167 char *pathname1;
9169 pathname1 = tcc_strdup(pathname);
9170 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9171 return 0;
9174 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9176 const char *ext, *filename1;
9177 Elf32_Ehdr ehdr;
9178 int fd, ret;
9179 BufferedFile *saved_file;
9181 /* find source file type with extension */
9182 filename1 = strrchr(filename, '/');
9183 if (filename1)
9184 filename1++;
9185 else
9186 filename1 = filename;
9187 ext = strrchr(filename1, '.');
9188 if (ext)
9189 ext++;
9191 /* open the file */
9192 saved_file = file;
9193 file = tcc_open(s1, filename);
9194 if (!file) {
9195 if (flags & AFF_PRINT_ERROR) {
9196 error_noabort("file '%s' not found", filename);
9198 ret = -1;
9199 goto fail1;
9202 if (!ext || !strcmp(ext, "c")) {
9203 /* C file assumed */
9204 ret = tcc_compile(s1);
9205 } else
9206 #ifdef CONFIG_TCC_ASM
9207 if (!strcmp(ext, "S")) {
9208 /* preprocessed assembler */
9209 ret = tcc_assemble(s1, 1);
9210 } else if (!strcmp(ext, "s")) {
9211 /* non preprocessed assembler */
9212 ret = tcc_assemble(s1, 0);
9213 } else
9214 #endif
9216 fd = file->fd;
9217 /* assume executable format: auto guess file type */
9218 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
9219 error_noabort("could not read header");
9220 goto fail;
9222 lseek(fd, 0, SEEK_SET);
9224 if (ehdr.e_ident[0] == ELFMAG0 &&
9225 ehdr.e_ident[1] == ELFMAG1 &&
9226 ehdr.e_ident[2] == ELFMAG2 &&
9227 ehdr.e_ident[3] == ELFMAG3) {
9228 file->line_num = 0; /* do not display line number if error */
9229 if (ehdr.e_type == ET_REL) {
9230 ret = tcc_load_object_file(s1, fd, 0);
9231 } else if (ehdr.e_type == ET_DYN) {
9232 ret = tcc_load_dll(s1, fd, filename,
9233 (flags & AFF_REFERENCED_DLL) != 0);
9234 } else {
9235 error_noabort("unrecognized ELF file");
9236 goto fail;
9238 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9239 file->line_num = 0; /* do not display line number if error */
9240 ret = tcc_load_archive(s1, fd);
9241 } else {
9242 /* as GNU ld, consider it is an ld script if not recognized */
9243 ret = tcc_load_ldscript(s1);
9244 if (ret < 0) {
9245 error_noabort("unrecognized file type");
9246 goto fail;
9250 the_end:
9251 tcc_close(file);
9252 fail1:
9253 file = saved_file;
9254 return ret;
9255 fail:
9256 ret = -1;
9257 goto the_end;
9260 int tcc_add_file(TCCState *s, const char *filename)
9262 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9265 int tcc_add_library_path(TCCState *s, const char *pathname)
9267 char *pathname1;
9269 pathname1 = tcc_strdup(pathname);
9270 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9271 return 0;
9274 /* find and load a dll. Return non zero if not found */
9275 /* XXX: add '-rpath' option support ? */
9276 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9278 char buf[1024];
9279 int i;
9281 for(i = 0; i < s->nb_library_paths; i++) {
9282 snprintf(buf, sizeof(buf), "%s/%s",
9283 s->library_paths[i], filename);
9284 if (tcc_add_file_internal(s, buf, flags) == 0)
9285 return 0;
9287 return -1;
9290 /* the library name is the same as the argument of the '-l' option */
9291 int tcc_add_library(TCCState *s, const char *libraryname)
9293 char buf[1024];
9294 int i;
9295 void *h;
9297 /* first we look for the dynamic library if not static linking */
9298 if (!s->static_link) {
9299 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9300 /* if we output to memory, then we simply we dlopen(). */
9301 if (s->output_type == TCC_OUTPUT_MEMORY) {
9302 /* Since the libc is already loaded, we don't need to load it again */
9303 if (!strcmp(libraryname, "c"))
9304 return 0;
9305 h = dlopen(buf, RTLD_GLOBAL | RTLD_LAZY);
9306 if (h)
9307 return 0;
9308 } else {
9309 if (tcc_add_dll(s, buf, 0) == 0)
9310 return 0;
9314 /* then we look for the static library */
9315 for(i = 0; i < s->nb_library_paths; i++) {
9316 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9317 s->library_paths[i], libraryname);
9318 if (tcc_add_file_internal(s, buf, 0) == 0)
9319 return 0;
9321 return -1;
9324 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9326 add_elf_sym(symtab_section, val, 0,
9327 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9328 SHN_ABS, name);
9329 return 0;
9332 int tcc_set_output_type(TCCState *s, int output_type)
9334 char buf[1024];
9336 s->output_type = output_type;
9338 if (!s->nostdinc) {
9339 /* default include paths */
9340 /* XXX: reverse order needed if -isystem support */
9341 tcc_add_sysinclude_path(s, "/usr/local/include");
9342 tcc_add_sysinclude_path(s, "/usr/include");
9343 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
9344 tcc_add_sysinclude_path(s, buf);
9347 /* if bound checking, then add corresponding sections */
9348 #ifdef CONFIG_TCC_BCHECK
9349 if (do_bounds_check) {
9350 /* define symbol */
9351 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
9352 /* create bounds sections */
9353 bounds_section = new_section(s, ".bounds",
9354 SHT_PROGBITS, SHF_ALLOC);
9355 lbounds_section = new_section(s, ".lbounds",
9356 SHT_PROGBITS, SHF_ALLOC);
9358 #endif
9360 /* add debug sections */
9361 if (do_debug) {
9362 /* stab symbols */
9363 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
9364 stab_section->sh_entsize = sizeof(Stab_Sym);
9365 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
9366 put_elf_str(stabstr_section, "");
9367 stab_section->link = stabstr_section;
9368 /* put first entry */
9369 put_stabs("", 0, 0, 0, 0);
9372 /* add libc crt1/crti objects */
9373 if (output_type == TCC_OUTPUT_EXE ||
9374 output_type == TCC_OUTPUT_DLL) {
9375 if (output_type != TCC_OUTPUT_DLL)
9376 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
9377 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
9379 return 0;
9382 #if !defined(LIBTCC)
9384 static int64_t getclock_us(void)
9386 #ifdef WIN32
9387 struct _timeb tb;
9388 _ftime(&tb);
9389 return (tb.time * 1000LL + tb.millitm) * 1000LL;
9390 #else
9391 struct timeval tv;
9392 gettimeofday(&tv, NULL);
9393 return tv.tv_sec * 1000000LL + tv.tv_usec;
9394 #endif
9397 void help(void)
9399 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
9400 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9401 " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9402 " [infile1 infile2...] [-run infile args...]\n"
9403 "\n"
9404 "General options:\n"
9405 " -v display current version\n"
9406 " -c compile only - generate an object file\n"
9407 " -o outfile set output filename\n"
9408 " -Bdir set tcc internal library path\n"
9409 " -bench output compilation statistics\n"
9410 " -run run compiled source\n"
9411 "Preprocessor options:\n"
9412 " -Idir add include path 'dir'\n"
9413 " -Dsym[=val] define 'sym' with value 'val'\n"
9414 " -Usym undefine 'sym'\n"
9415 "Linker options:\n"
9416 " -Ldir add library path 'dir'\n"
9417 " -llib link with dynamic or static library 'lib'\n"
9418 " -shared generate a shared library\n"
9419 " -static static linking\n"
9420 " -r relocatable output\n"
9421 "Debugger options:\n"
9422 " -g generate runtime debug info\n"
9423 #ifdef CONFIG_TCC_BCHECK
9424 " -b compile with built-in memory and bounds checker (implies -g)\n"
9425 #endif
9426 " -bt N show N callers in stack traces\n"
9430 #define TCC_OPTION_HAS_ARG 0x0001
9431 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9433 typedef struct TCCOption {
9434 const char *name;
9435 uint16_t index;
9436 uint16_t flags;
9437 } TCCOption;
9439 enum {
9440 TCC_OPTION_HELP,
9441 TCC_OPTION_I,
9442 TCC_OPTION_D,
9443 TCC_OPTION_U,
9444 TCC_OPTION_L,
9445 TCC_OPTION_B,
9446 TCC_OPTION_l,
9447 TCC_OPTION_bench,
9448 TCC_OPTION_bt,
9449 TCC_OPTION_b,
9450 TCC_OPTION_g,
9451 TCC_OPTION_c,
9452 TCC_OPTION_static,
9453 TCC_OPTION_shared,
9454 TCC_OPTION_o,
9455 TCC_OPTION_r,
9456 TCC_OPTION_W,
9457 TCC_OPTION_O,
9458 TCC_OPTION_m,
9459 TCC_OPTION_f,
9460 TCC_OPTION_nostdinc,
9461 TCC_OPTION_print_search_dirs,
9462 TCC_OPTION_rdynamic,
9463 TCC_OPTION_run,
9464 TCC_OPTION_v,
9467 static const TCCOption tcc_options[] = {
9468 { "h", TCC_OPTION_HELP, 0 },
9469 { "?", TCC_OPTION_HELP, 0 },
9470 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
9471 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
9472 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
9473 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
9474 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
9475 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9476 { "bench", TCC_OPTION_bench, 0 },
9477 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
9478 #ifdef CONFIG_TCC_BCHECK
9479 { "b", TCC_OPTION_b, 0 },
9480 #endif
9481 { "g", TCC_OPTION_g, 0 },
9482 { "c", TCC_OPTION_c, 0 },
9483 { "static", TCC_OPTION_static, 0 },
9484 { "shared", TCC_OPTION_shared, 0 },
9485 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
9486 { "run", TCC_OPTION_run, 0 },
9487 { "rdynamic", TCC_OPTION_rdynamic, 0 }, /* currently ignored */
9488 { "r", TCC_OPTION_r, 0 },
9489 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9490 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9491 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
9492 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9493 { "nostdinc", TCC_OPTION_nostdinc, 0 },
9494 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
9495 { "v", TCC_OPTION_v, 0 },
9496 { NULL },
9499 int main(int argc, char **argv)
9501 char *r;
9502 int optind, output_type, multiple_files, i, reloc_output;
9503 TCCState *s;
9504 char **files;
9505 int nb_files, nb_libraries, nb_objfiles, dminus, ret;
9506 char objfilename[1024];
9507 int64_t start_time = 0;
9508 const TCCOption *popt;
9509 const char *optarg, *p1, *r1, *outfile;
9510 int print_search_dirs;
9512 s = tcc_new();
9513 output_type = TCC_OUTPUT_EXE;
9515 optind = 1;
9516 outfile = NULL;
9517 multiple_files = 1;
9518 dminus = 0;
9519 files = NULL;
9520 nb_files = 0;
9521 nb_libraries = 0;
9522 reloc_output = 0;
9523 print_search_dirs = 0;
9524 while (1) {
9525 if (optind >= argc) {
9526 if (nb_files == 0 && !print_search_dirs)
9527 goto show_help;
9528 else
9529 break;
9531 r = argv[optind++];
9532 if (r[0] != '-') {
9533 /* add a new file */
9534 dynarray_add((void ***)&files, &nb_files, r);
9535 if (!multiple_files) {
9536 optind--;
9537 /* argv[0] will be this file */
9538 break;
9540 } else {
9541 /* find option in table (match only the first chars */
9542 popt = tcc_options;
9543 for(;;) {
9544 p1 = popt->name;
9545 if (p1 == NULL)
9546 error("invalid option -- '%s'", r);
9547 r1 = r + 1;
9548 for(;;) {
9549 if (*p1 == '\0')
9550 goto option_found;
9551 if (*r1 != *p1)
9552 break;
9553 p1++;
9554 r1++;
9556 popt++;
9558 option_found:
9559 if (popt->flags & TCC_OPTION_HAS_ARG) {
9560 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
9561 optarg = r1;
9562 } else {
9563 if (optind >= argc)
9564 error("argument to '%s' is missing", r);
9565 optarg = argv[optind++];
9567 } else {
9568 if (*r1 != '\0')
9569 goto show_help;
9570 optarg = NULL;
9573 switch(popt->index) {
9574 case TCC_OPTION_HELP:
9575 show_help:
9576 help();
9577 return 1;
9578 case TCC_OPTION_I:
9579 if (tcc_add_include_path(s, optarg) < 0)
9580 error("too many include paths");
9581 break;
9582 case TCC_OPTION_D:
9584 char *sym, *value;
9585 sym = (char *)optarg;
9586 value = strchr(sym, '=');
9587 if (value) {
9588 *value = '\0';
9589 value++;
9591 tcc_define_symbol(s, sym, value);
9593 break;
9594 case TCC_OPTION_U:
9595 tcc_undefine_symbol(s, optarg);
9596 break;
9597 case TCC_OPTION_L:
9598 tcc_add_library_path(s, optarg);
9599 break;
9600 case TCC_OPTION_B:
9601 /* set tcc utilities path (mainly for tcc development) */
9602 tcc_lib_path = optarg;
9603 break;
9604 case TCC_OPTION_l:
9605 dynarray_add((void ***)&files, &nb_files, r);
9606 nb_libraries++;
9607 break;
9608 case TCC_OPTION_bench:
9609 do_bench = 1;
9610 break;
9611 case TCC_OPTION_bt:
9612 num_callers = atoi(optarg);
9613 break;
9614 #ifdef CONFIG_TCC_BCHECK
9615 case TCC_OPTION_b:
9616 do_bounds_check = 1;
9617 do_debug = 1;
9618 break;
9619 #endif
9620 case TCC_OPTION_g:
9621 do_debug = 1;
9622 break;
9623 case TCC_OPTION_c:
9624 multiple_files = 1;
9625 output_type = TCC_OUTPUT_OBJ;
9626 break;
9627 case TCC_OPTION_static:
9628 s->static_link = 1;
9629 break;
9630 case TCC_OPTION_shared:
9631 output_type = TCC_OUTPUT_DLL;
9632 break;
9633 case TCC_OPTION_o:
9634 multiple_files = 1;
9635 outfile = optarg;
9636 break;
9637 case TCC_OPTION_r:
9638 /* generate a .o merging several output files */
9639 reloc_output = 1;
9640 output_type = TCC_OUTPUT_OBJ;
9641 break;
9642 case TCC_OPTION_nostdinc:
9643 s->nostdinc = 1;
9644 break;
9645 case TCC_OPTION_print_search_dirs:
9646 print_search_dirs = 1;
9647 break;
9648 case TCC_OPTION_run:
9649 multiple_files = 0;
9650 output_type = TCC_OUTPUT_MEMORY;
9651 break;
9652 case TCC_OPTION_v:
9653 printf("tcc version %s\n", TCC_VERSION);
9654 return 0;
9655 default:
9656 break;
9660 if (print_search_dirs) {
9661 /* enough for Linux kernel */
9662 printf("install: %s/\n", tcc_lib_path);
9663 return 0;
9666 nb_objfiles = nb_files - nb_libraries;
9668 /* if outfile provided without other options, we output an
9669 executable */
9670 if (outfile && output_type == TCC_OUTPUT_MEMORY)
9671 output_type = TCC_OUTPUT_EXE;
9673 /* check -c consistency : only single file handled. XXX: checks file type */
9674 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9675 /* accepts only a single input file */
9676 if (nb_objfiles != 1)
9677 error("cannot specify multiple files with -c");
9678 if (nb_libraries != 0)
9679 error("cannot specify libraries with -c");
9682 /* compute default outfile name */
9683 if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
9684 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9685 char *ext;
9686 /* add .o extension */
9687 pstrcpy(objfilename, sizeof(objfilename) - 1, files[0]);
9688 ext = strrchr(objfilename, '.');
9689 if (!ext)
9690 goto default_outfile;
9691 strcpy(ext + 1, "o");
9692 } else {
9693 default_outfile:
9694 pstrcpy(objfilename, sizeof(objfilename), "a.out");
9696 outfile = objfilename;
9699 if (do_bench) {
9700 start_time = getclock_us();
9703 tcc_set_output_type(s, output_type);
9705 /* compile or add each files or library */
9706 for(i = 0;i < nb_files; i++) {
9707 const char *filename;
9709 filename = files[i];
9710 if (filename[0] == '-') {
9711 if (tcc_add_library(s, filename + 2) < 0)
9712 error("cannot find %s", filename);
9713 } else {
9714 if (tcc_add_file(s, filename) < 0) {
9715 ret = 1;
9716 goto the_end;
9721 /* free all files */
9722 tcc_free(files);
9724 if (do_bench) {
9725 double total_time;
9726 total_time = (double)(getclock_us() - start_time) / 1000000.0;
9727 if (total_time < 0.001)
9728 total_time = 0.001;
9729 if (total_bytes < 1)
9730 total_bytes = 1;
9731 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
9732 tok_ident - TOK_IDENT, total_lines, total_bytes,
9733 total_time, (int)(total_lines / total_time),
9734 total_bytes / total_time / 1000000.0);
9737 if (s->output_type != TCC_OUTPUT_MEMORY) {
9738 tcc_output_file(s, outfile);
9739 ret = 0;
9740 } else {
9741 ret = tcc_run(s, argc - optind, argv + optind);
9743 the_end:
9744 /* XXX: cannot do it with bound checking because of the malloc hooks */
9745 if (!do_bounds_check)
9746 tcc_delete(s);
9748 #ifdef MEM_DEBUG
9749 if (do_bench) {
9750 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
9752 #endif
9753 return ret;
9756 #endif