both .globl and .global directives are accepted by as
[tinycc.git] / tcc.c
blob655a058ae08cba1f96710d88797ecea7cf852a3b
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 */
64 //#define TCC_TARGET_ARM /* ARMv4 code generator */
66 /* default target is I386 */
67 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM)
68 #define TCC_TARGET_I386
69 #endif
71 #if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM)
72 #define CONFIG_TCC_BCHECK /* enable bound checking code */
73 #endif
75 /* define it to include assembler support */
76 #if !defined(TCC_TARGET_ARM)
77 #define CONFIG_TCC_ASM
78 #endif
80 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
81 executables or dlls */
82 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
84 #define INCLUDE_STACK_SIZE 32
85 #define IFDEF_STACK_SIZE 64
86 #define VSTACK_SIZE 64
87 #define STRING_MAX_SIZE 1024
89 #define TOK_HASH_SIZE 2048 /* must be a power of two */
90 #define TOK_ALLOC_INCR 512 /* must be a power of two */
91 #define TOK_STR_ALLOC_INCR_BITS 6
92 #define TOK_STR_ALLOC_INCR (1 << TOK_STR_ALLOC_INCR_BITS)
93 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
95 /* token symbol management */
96 typedef struct TokenSym {
97 struct TokenSym *hash_next;
98 struct Sym *sym_define; /* direct pointer to define */
99 struct Sym *sym_label; /* direct pointer to label */
100 struct Sym *sym_struct; /* direct pointer to structure */
101 struct Sym *sym_identifier; /* direct pointer to identifier */
102 int tok; /* token number */
103 int len;
104 char str[1];
105 } TokenSym;
107 typedef struct CString {
108 int size; /* size in bytes */
109 void *data; /* either 'char *' or 'int *' */
110 int size_allocated;
111 void *data_allocated; /* if non NULL, data has been malloced */
112 } CString;
114 /* type definition */
115 typedef struct CType {
116 int t;
117 struct Sym *ref;
118 } CType;
120 /* constant value */
121 typedef union CValue {
122 long double ld;
123 double d;
124 float f;
125 int i;
126 unsigned int ui;
127 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
128 long long ll;
129 unsigned long long ull;
130 struct CString *cstr;
131 void *ptr;
132 int tab[1];
133 } CValue;
135 /* value on stack */
136 typedef struct SValue {
137 CType type; /* type */
138 unsigned short r; /* register + flags */
139 unsigned short r2; /* second register, used for 'long long'
140 type. If not used, set to VT_CONST */
141 CValue c; /* constant, if VT_CONST */
142 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
143 } SValue;
145 /* symbol management */
146 typedef struct Sym {
147 int v; /* symbol token */
148 int r; /* associated register */
149 int c; /* associated number */
150 CType type; /* associated type */
151 struct Sym *next; /* next related symbol */
152 struct Sym *prev; /* prev symbol in stack */
153 struct Sym *prev_tok; /* previous symbol for this token */
154 } Sym;
156 /* section definition */
157 /* XXX: use directly ELF structure for parameters ? */
158 /* special flag to indicate that the section should not be linked to
159 the other ones */
160 #define SHF_PRIVATE 0x80000000
162 typedef struct Section {
163 unsigned long data_offset; /* current data offset */
164 unsigned char *data; /* section data */
165 unsigned long data_allocated; /* used for realloc() handling */
166 int sh_name; /* elf section name (only used during output) */
167 int sh_num; /* elf section number */
168 int sh_type; /* elf section type */
169 int sh_flags; /* elf section flags */
170 int sh_info; /* elf section info */
171 int sh_addralign; /* elf section alignment */
172 int sh_entsize; /* elf entry size */
173 unsigned long sh_size; /* section size (only used during output) */
174 unsigned long sh_addr; /* address at which the section is relocated */
175 unsigned long sh_offset; /* address at which the section is relocated */
176 int nb_hashed_syms; /* used to resize the hash table */
177 struct Section *link; /* link to another section */
178 struct Section *reloc; /* corresponding section for relocation, if any */
179 struct Section *hash; /* hash table for symbols */
180 struct Section *next;
181 char name[1]; /* section name */
182 } Section;
184 typedef struct DLLReference {
185 int level;
186 char name[1];
187 } DLLReference;
189 /* GNUC attribute definition */
190 typedef struct AttributeDef {
191 int aligned;
192 Section *section;
193 unsigned char func_call; /* FUNC_CDECL or FUNC_STDCALL */
194 } AttributeDef;
196 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
197 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
198 #define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
200 /* stored in 'Sym.c' field */
201 #define FUNC_NEW 1 /* ansi function prototype */
202 #define FUNC_OLD 2 /* old function prototype */
203 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
205 /* stored in 'Sym.r' field */
206 #define FUNC_CDECL 0 /* standard c call */
207 #define FUNC_STDCALL 1 /* pascal c call */
209 /* field 'Sym.t' for macros */
210 #define MACRO_OBJ 0 /* object like macro */
211 #define MACRO_FUNC 1 /* function like macro */
213 /* field 'Sym.r' for C labels */
214 #define LABEL_DEFINED 0 /* label is defined */
215 #define LABEL_FORWARD 1 /* label is forward defined */
216 #define LABEL_DECLARED 2 /* label is declared but never used */
218 /* type_decl() types */
219 #define TYPE_ABSTRACT 1 /* type without variable */
220 #define TYPE_DIRECT 2 /* type with variable */
222 #define IO_BUF_SIZE 8192
224 typedef struct BufferedFile {
225 uint8_t *buf_ptr;
226 uint8_t *buf_end;
227 int fd;
228 int line_num; /* current line number - here to simplify code */
229 int ifndef_macro; /* #ifndef macro / #endif search */
230 int ifndef_macro_saved; /* saved ifndef_macro */
231 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
232 char inc_type; /* type of include */
233 char inc_filename[512]; /* filename specified by the user */
234 char filename[1024]; /* current filename - here to simplify code */
235 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
236 } BufferedFile;
238 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
239 #define CH_EOF (-1) /* end of file */
241 /* parsing state (used to save parser state to reparse part of the
242 source several times) */
243 typedef struct ParseState {
244 int *macro_ptr;
245 int line_num;
246 int tok;
247 CValue tokc;
248 } ParseState;
250 /* used to record tokens */
251 typedef struct TokenString {
252 int *str;
253 int len;
254 int allocated_len;
255 int last_line_num;
256 } TokenString;
258 /* include file cache, used to find files faster and also to eliminate
259 inclusion if the include file is protected by #ifndef ... #endif */
260 typedef struct CachedInclude {
261 int ifndef_macro;
262 char type; /* '"' or '>' to give include type */
263 char filename[1]; /* path specified in #include */
264 } CachedInclude;
266 /* parser */
267 static struct BufferedFile *file;
268 static int ch, tok;
269 static CValue tokc;
270 static CString tokcstr; /* current parsed string, if any */
271 /* additional informations about token */
272 static int tok_flags;
273 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
274 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
275 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
277 static int *macro_ptr, *macro_ptr_allocated;
278 static int *unget_saved_macro_ptr;
279 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
280 static int unget_buffer_enabled;
281 static int parse_flags;
282 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
283 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
284 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
285 token. line feed is also
286 returned at eof */
288 static Section *text_section, *data_section, *bss_section; /* predefined sections */
289 static Section *cur_text_section; /* current section where function code is
290 generated */
291 /* bound check related sections */
292 static Section *bounds_section; /* contains global data bound description */
293 static Section *lbounds_section; /* contains local data bound description */
294 /* symbol sections */
295 static Section *symtab_section, *strtab_section;
297 /* debug sections */
298 static Section *stab_section, *stabstr_section;
300 /* loc : local variable index
301 ind : output code index
302 rsym: return symbol
303 anon_sym: anonymous symbol index
305 static int rsym, anon_sym, ind, loc;
306 /* expression generation modifiers */
307 static int const_wanted; /* true if constant wanted */
308 static int nocode_wanted; /* true if no code generation wanted for an expression */
309 static int global_expr; /* true if compound literals must be allocated
310 globally (used during initializers parsing */
311 static CType func_vt; /* current function return type (used by return
312 instruction) */
313 static int func_vc;
314 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
315 static int tok_ident;
316 static TokenSym **table_ident;
317 static TokenSym *hash_ident[TOK_HASH_SIZE];
318 static char token_buf[STRING_MAX_SIZE + 1];
319 static char *funcname;
320 static Sym *global_stack, *local_stack;
321 static Sym *define_stack;
322 static Sym *global_label_stack, *local_label_stack;
324 static SValue vstack[VSTACK_SIZE], *vtop;
325 /* some predefined types */
326 static CType char_pointer_type, func_old_type, int_type;
327 /* true if isid(c) || isnum(c) */
328 static unsigned char isidnum_table[256];
330 /* compile with debug symbol (and use them if error during execution) */
331 static int do_debug = 0;
333 /* compile with built-in memory and bounds checker */
334 static int do_bounds_check = 0;
336 /* display benchmark infos */
337 #if !defined(LIBTCC)
338 static int do_bench = 0;
339 #endif
340 static int total_lines;
341 static int total_bytes;
343 /* use GNU C extensions */
344 static int gnu_ext = 1;
346 /* use Tiny C extensions */
347 static int tcc_ext = 1;
349 /* max number of callers shown if error */
350 static int num_callers = 6;
351 static const char **rt_bound_error_msg;
353 /* XXX: get rid of this ASAP */
354 static struct TCCState *tcc_state;
356 /* give the path of the tcc libraries */
357 static const char *tcc_lib_path = CONFIG_TCC_LIBDIR "/tcc";
359 struct TCCState {
360 int output_type;
362 BufferedFile **include_stack_ptr;
363 int *ifdef_stack_ptr;
365 /* include file handling */
366 char **include_paths;
367 int nb_include_paths;
368 char **sysinclude_paths;
369 int nb_sysinclude_paths;
370 CachedInclude **cached_includes;
371 int nb_cached_includes;
373 char **library_paths;
374 int nb_library_paths;
376 /* array of all loaded dlls (including those referenced by loaded
377 dlls) */
378 DLLReference **loaded_dlls;
379 int nb_loaded_dlls;
381 /* sections */
382 Section **sections;
383 int nb_sections; /* number of sections, including first dummy section */
385 /* got handling */
386 Section *got;
387 Section *plt;
388 unsigned long *got_offsets;
389 int nb_got_offsets;
390 /* give the correspondance from symtab indexes to dynsym indexes */
391 int *symtab_to_dynsym;
393 /* temporary dynamic symbol sections (for dll loading) */
394 Section *dynsymtab_section;
395 /* exported dynamic symbol section */
396 Section *dynsym;
398 int nostdinc; /* if true, no standard headers are added */
399 int nostdlib; /* if true, no standard libraries are added */
401 /* if true, static linking is performed */
402 int static_link;
404 /* if true, all symbols are exported */
405 int rdynamic;
407 /* if true, only link in referenced objects from archive */
408 int alacarte_link;
410 /* C language options */
411 int char_is_unsigned;
413 /* warning switches */
414 int warn_write_strings;
415 int warn_unsupported;
416 int warn_error;
417 int warn_none;
418 int warn_implicit_function_declaration;
420 /* error handling */
421 void *error_opaque;
422 void (*error_func)(void *opaque, const char *msg);
423 int error_set_jmp_enabled;
424 jmp_buf error_jmp_buf;
425 int nb_errors;
427 /* tiny assembler state */
428 Sym *asm_labels;
430 /* see include_stack_ptr */
431 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
433 /* see ifdef_stack_ptr */
434 int ifdef_stack[IFDEF_STACK_SIZE];
437 /* The current value can be: */
438 #define VT_VALMASK 0x00ff
439 #define VT_CONST 0x00f0 /* constant in vc
440 (must be first non register value) */
441 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
442 #define VT_LOCAL 0x00f2 /* offset on stack */
443 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
444 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
445 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
446 #define VT_LVAL 0x0100 /* var is an lvalue */
447 #define VT_SYM 0x0200 /* a symbol value is added */
448 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
449 char/short stored in integer registers) */
450 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
451 dereferencing value */
452 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
453 bounding function call point is in vc */
454 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
455 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
456 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
457 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
459 /* types */
460 #define VT_INT 0 /* integer type */
461 #define VT_BYTE 1 /* signed byte type */
462 #define VT_SHORT 2 /* short type */
463 #define VT_VOID 3 /* void type */
464 #define VT_PTR 4 /* pointer */
465 #define VT_ENUM 5 /* enum definition */
466 #define VT_FUNC 6 /* function type */
467 #define VT_STRUCT 7 /* struct/union definition */
468 #define VT_FLOAT 8 /* IEEE float */
469 #define VT_DOUBLE 9 /* IEEE double */
470 #define VT_LDOUBLE 10 /* IEEE long double */
471 #define VT_BOOL 11 /* ISOC99 boolean type */
472 #define VT_LLONG 12 /* 64 bit integer */
473 #define VT_LONG 13 /* long integer (NEVER USED as type, only
474 during parsing) */
475 #define VT_BTYPE 0x000f /* mask for basic type */
476 #define VT_UNSIGNED 0x0010 /* unsigned type */
477 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
478 #define VT_BITFIELD 0x0040 /* bitfield modifier */
479 #define VT_CONSTANT 0x0800 /* const modifier */
480 #define VT_VOLATILE 0x1000 /* volatile modifier */
481 #define VT_SIGNED 0x2000 /* signed type */
483 /* storage */
484 #define VT_EXTERN 0x00000080 /* extern definition */
485 #define VT_STATIC 0x00000100 /* static variable */
486 #define VT_TYPEDEF 0x00000200 /* typedef definition */
487 #define VT_INLINE 0x00000400 /* inline definition */
489 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
491 /* type mask (except storage) */
492 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
493 #define VT_TYPE (~(VT_STORAGE))
495 /* token values */
497 /* warning: the following compare tokens depend on i386 asm code */
498 #define TOK_ULT 0x92
499 #define TOK_UGE 0x93
500 #define TOK_EQ 0x94
501 #define TOK_NE 0x95
502 #define TOK_ULE 0x96
503 #define TOK_UGT 0x97
504 #define TOK_LT 0x9c
505 #define TOK_GE 0x9d
506 #define TOK_LE 0x9e
507 #define TOK_GT 0x9f
509 #define TOK_LAND 0xa0
510 #define TOK_LOR 0xa1
512 #define TOK_DEC 0xa2
513 #define TOK_MID 0xa3 /* inc/dec, to void constant */
514 #define TOK_INC 0xa4
515 #define TOK_UDIV 0xb0 /* unsigned division */
516 #define TOK_UMOD 0xb1 /* unsigned modulo */
517 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
518 #define TOK_CINT 0xb3 /* number in tokc */
519 #define TOK_CCHAR 0xb4 /* char constant in tokc */
520 #define TOK_STR 0xb5 /* pointer to string in tokc */
521 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
522 #define TOK_LCHAR 0xb7
523 #define TOK_LSTR 0xb8
524 #define TOK_CFLOAT 0xb9 /* float constant */
525 #define TOK_LINENUM 0xba /* line number info */
526 #define TOK_CDOUBLE 0xc0 /* double constant */
527 #define TOK_CLDOUBLE 0xc1 /* long double constant */
528 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
529 #define TOK_ADDC1 0xc3 /* add with carry generation */
530 #define TOK_ADDC2 0xc4 /* add with carry use */
531 #define TOK_SUBC1 0xc5 /* add with carry generation */
532 #define TOK_SUBC2 0xc6 /* add with carry use */
533 #define TOK_CUINT 0xc8 /* unsigned int constant */
534 #define TOK_CLLONG 0xc9 /* long long constant */
535 #define TOK_CULLONG 0xca /* unsigned long long constant */
536 #define TOK_ARROW 0xcb
537 #define TOK_DOTS 0xcc /* three dots */
538 #define TOK_SHR 0xcd /* unsigned shift right */
539 #define TOK_PPNUM 0xce /* preprocessor number */
541 #define TOK_SHL 0x01 /* shift left */
542 #define TOK_SAR 0x02 /* signed shift right */
544 /* assignement operators : normal operator or 0x80 */
545 #define TOK_A_MOD 0xa5
546 #define TOK_A_AND 0xa6
547 #define TOK_A_MUL 0xaa
548 #define TOK_A_ADD 0xab
549 #define TOK_A_SUB 0xad
550 #define TOK_A_DIV 0xaf
551 #define TOK_A_XOR 0xde
552 #define TOK_A_OR 0xfc
553 #define TOK_A_SHL 0x81
554 #define TOK_A_SAR 0x82
556 #ifndef offsetof
557 #define offsetof(type, field) ((size_t) &((type *)0)->field)
558 #endif
560 #ifndef countof
561 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
562 #endif
564 /* WARNING: the content of this string encodes token numbers */
565 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";
567 #define TOK_EOF (-1) /* end of file */
568 #define TOK_LINEFEED 10 /* line feed */
570 /* all identificators and strings have token above that */
571 #define TOK_IDENT 256
573 /* only used for i386 asm opcodes definitions */
574 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
576 #define DEF_BWL(x) \
577 DEF(TOK_ASM_ ## x ## b, #x "b") \
578 DEF(TOK_ASM_ ## x ## w, #x "w") \
579 DEF(TOK_ASM_ ## x ## l, #x "l") \
580 DEF(TOK_ASM_ ## x, #x)
582 #define DEF_WL(x) \
583 DEF(TOK_ASM_ ## x ## w, #x "w") \
584 DEF(TOK_ASM_ ## x ## l, #x "l") \
585 DEF(TOK_ASM_ ## x, #x)
587 #define DEF_FP1(x) \
588 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
589 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
590 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
591 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
593 #define DEF_FP(x) \
594 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
595 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
596 DEF_FP1(x)
598 #define DEF_ASMTEST(x) \
599 DEF_ASM(x ## o) \
600 DEF_ASM(x ## no) \
601 DEF_ASM(x ## b) \
602 DEF_ASM(x ## c) \
603 DEF_ASM(x ## nae) \
604 DEF_ASM(x ## nb) \
605 DEF_ASM(x ## nc) \
606 DEF_ASM(x ## ae) \
607 DEF_ASM(x ## e) \
608 DEF_ASM(x ## z) \
609 DEF_ASM(x ## ne) \
610 DEF_ASM(x ## nz) \
611 DEF_ASM(x ## be) \
612 DEF_ASM(x ## na) \
613 DEF_ASM(x ## nbe) \
614 DEF_ASM(x ## a) \
615 DEF_ASM(x ## s) \
616 DEF_ASM(x ## ns) \
617 DEF_ASM(x ## p) \
618 DEF_ASM(x ## pe) \
619 DEF_ASM(x ## np) \
620 DEF_ASM(x ## po) \
621 DEF_ASM(x ## l) \
622 DEF_ASM(x ## nge) \
623 DEF_ASM(x ## nl) \
624 DEF_ASM(x ## ge) \
625 DEF_ASM(x ## le) \
626 DEF_ASM(x ## ng) \
627 DEF_ASM(x ## nle) \
628 DEF_ASM(x ## g)
630 #define TOK_ASM_int TOK_INT
632 enum {
633 TOK_LAST = TOK_IDENT - 1,
634 #define DEF(id, str) id,
635 #include "tcctok.h"
636 #undef DEF
639 static const char tcc_keywords[] =
640 #define DEF(id, str) str "\0"
641 #include "tcctok.h"
642 #undef DEF
645 #define TOK_UIDENT TOK_DEFINE
647 #ifdef WIN32
648 #define snprintf _snprintf
649 #define vsnprintf _vsnprintf
650 #endif
652 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
653 /* currently incorrect */
654 long double strtold(const char *nptr, char **endptr)
656 return (long double)strtod(nptr, endptr);
658 float strtof(const char *nptr, char **endptr)
660 return (float)strtod(nptr, endptr);
662 #else
663 /* XXX: need to define this to use them in non ISOC99 context */
664 extern float strtof (const char *__nptr, char **__endptr);
665 extern long double strtold (const char *__nptr, char **__endptr);
666 #endif
668 static char *pstrcpy(char *buf, int buf_size, const char *s);
669 static char *pstrcat(char *buf, int buf_size, const char *s);
671 static void next(void);
672 static void next_nomacro(void);
673 static void parse_expr_type(CType *type);
674 static void expr_type(CType *type);
675 static void unary_type(CType *type);
676 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
677 int case_reg, int is_expr);
678 static int expr_const(void);
679 static void expr_eq(void);
680 static void gexpr(void);
681 static void decl(int l);
682 static void decl_initializer(CType *type, Section *sec, unsigned long c,
683 int first, int size_only);
684 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
685 int has_init, int v, int scope);
686 int gv(int rc);
687 void gv2(int rc1, int rc2);
688 void move_reg(int r, int s);
689 void save_regs(int n);
690 void save_reg(int r);
691 void vpop(void);
692 void vswap(void);
693 void vdup(void);
694 int get_reg(int rc);
695 int get_reg_ex(int rc,int rc2);
697 static void macro_subst(TokenString *tok_str, Sym **nested_list,
698 const int *macro_str, int can_read_stream);
699 int save_reg_forced(int r);
700 void gen_op(int op);
701 void force_charshort_cast(int t);
702 static void gen_cast(CType *type);
703 void vstore(void);
704 static Sym *sym_find(int v);
705 static Sym *sym_push(int v, CType *type, int r, int c);
707 /* type handling */
708 static int type_size(CType *type, int *a);
709 static inline CType *pointed_type(CType *type);
710 static int pointed_size(CType *type);
711 static int lvalue_type(int t);
712 static int parse_btype(CType *type, AttributeDef *ad);
713 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
714 static int is_compatible_types(CType *type1, CType *type2);
716 int ieee_finite(double d);
717 void error(const char *fmt, ...);
718 void vpushi(int v);
719 void vrott(int n);
720 static void vpush_global_sym(CType *type, int v);
721 void vset(CType *type, int r, int v);
722 void type_to_str(char *buf, int buf_size,
723 CType *type, const char *varstr);
724 char *get_tok_str(int v, CValue *cv);
725 static Sym *get_sym_ref(CType *type, Section *sec,
726 unsigned long offset, unsigned long size);
727 static Sym *external_global_sym(int v, CType *type, int r);
729 /* section generation */
730 static void section_realloc(Section *sec, unsigned long new_size);
731 static void *section_ptr_add(Section *sec, unsigned long size);
732 static void put_extern_sym(Sym *sym, Section *section,
733 unsigned long value, unsigned long size);
734 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
735 static int put_elf_str(Section *s, const char *sym);
736 static int put_elf_sym(Section *s,
737 unsigned long value, unsigned long size,
738 int info, int other, int shndx, const char *name);
739 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
740 int info, int sh_num, const char *name);
741 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
742 int type, int symbol);
743 static void put_stabs(const char *str, int type, int other, int desc,
744 unsigned long value);
745 static void put_stabs_r(const char *str, int type, int other, int desc,
746 unsigned long value, Section *sec, int sym_index);
747 static void put_stabn(int type, int other, int desc, int value);
748 static void put_stabd(int type, int other, int desc);
749 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
751 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
752 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
753 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
755 /* tccasm.c */
757 #ifdef CONFIG_TCC_ASM
759 typedef struct ExprValue {
760 uint32_t v;
761 Sym *sym;
762 } ExprValue;
764 #define MAX_ASM_OPERANDS 30
766 typedef struct ASMOperand {
767 int id; /* GCC 3 optionnal identifier (0 if number only supported */
768 char *constraint;
769 char asm_str[16]; /* computed asm string for operand */
770 SValue *vt; /* C value of the expression */
771 int ref_index; /* if >= 0, gives reference to a output constraint */
772 int priority; /* priority, used to assign registers */
773 int reg; /* if >= 0, register number used for this operand */
774 int is_llong; /* true if double register value */
775 } ASMOperand;
777 static void asm_expr(TCCState *s1, ExprValue *pe);
778 static int asm_int_expr(TCCState *s1);
779 static int find_constraint(ASMOperand *operands, int nb_operands,
780 const char *name, const char **pp);
782 static int tcc_assemble(TCCState *s1, int do_preprocess);
784 #endif
786 static void asm_instr(void);
788 /* true if float/double/long double type */
789 static inline int is_float(int t)
791 int bt;
792 bt = t & VT_BTYPE;
793 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
796 #ifdef TCC_TARGET_I386
797 #include "i386-gen.c"
798 #endif
800 #ifdef TCC_TARGET_ARM
801 #include "arm-gen.c"
802 #endif
804 #ifdef CONFIG_TCC_STATIC
806 #define RTLD_LAZY 0x001
807 #define RTLD_NOW 0x002
808 #define RTLD_GLOBAL 0x100
809 #define RTLD_DEFAULT NULL
811 /* dummy function for profiling */
812 void *dlopen(const char *filename, int flag)
814 return NULL;
817 const char *dlerror(void)
819 return "error";
822 typedef struct TCCSyms {
823 char *str;
824 void *ptr;
825 } TCCSyms;
827 #define TCCSYM(a) { #a, &a, },
829 /* add the symbol you want here if no dynamic linking is done */
830 static TCCSyms tcc_syms[] = {
831 TCCSYM(printf)
832 TCCSYM(fprintf)
833 TCCSYM(fopen)
834 TCCSYM(fclose)
835 { NULL, NULL },
838 void *dlsym(void *handle, const char *symbol)
840 TCCSyms *p;
841 p = tcc_syms;
842 while (p->str != NULL) {
843 if (!strcmp(p->str, symbol))
844 return p->ptr;
845 p++;
847 return NULL;
850 #endif
852 /********************************************************/
854 /* we use our own 'finite' function to avoid potential problems with
855 non standard math libs */
856 /* XXX: endianness dependent */
857 int ieee_finite(double d)
859 int *p = (int *)&d;
860 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
863 /* copy a string and truncate it. */
864 static char *pstrcpy(char *buf, int buf_size, const char *s)
866 char *q, *q_end;
867 int c;
869 if (buf_size > 0) {
870 q = buf;
871 q_end = buf + buf_size - 1;
872 while (q < q_end) {
873 c = *s++;
874 if (c == '\0')
875 break;
876 *q++ = c;
878 *q = '\0';
880 return buf;
883 /* strcat and truncate. */
884 static char *pstrcat(char *buf, int buf_size, const char *s)
886 int len;
887 len = strlen(buf);
888 if (len < buf_size)
889 pstrcpy(buf + len, buf_size - len, s);
890 return buf;
893 /* memory management */
894 #ifdef MEM_DEBUG
895 int mem_cur_size;
896 int mem_max_size;
897 #endif
899 static inline void tcc_free(void *ptr)
901 #ifdef MEM_DEBUG
902 mem_cur_size -= malloc_usable_size(ptr);
903 #endif
904 free(ptr);
907 static void *tcc_malloc(unsigned long size)
909 void *ptr;
910 ptr = malloc(size);
911 if (!ptr && size)
912 error("memory full");
913 #ifdef MEM_DEBUG
914 mem_cur_size += malloc_usable_size(ptr);
915 if (mem_cur_size > mem_max_size)
916 mem_max_size = mem_cur_size;
917 #endif
918 return ptr;
921 static void *tcc_mallocz(unsigned long size)
923 void *ptr;
924 ptr = tcc_malloc(size);
925 memset(ptr, 0, size);
926 return ptr;
929 static inline void *tcc_realloc(void *ptr, unsigned long size)
931 void *ptr1;
932 #ifdef MEM_DEBUG
933 mem_cur_size -= malloc_usable_size(ptr);
934 #endif
935 ptr1 = realloc(ptr, size);
936 #ifdef MEM_DEBUG
937 /* NOTE: count not correct if alloc error, but not critical */
938 mem_cur_size += malloc_usable_size(ptr1);
939 if (mem_cur_size > mem_max_size)
940 mem_max_size = mem_cur_size;
941 #endif
942 return ptr1;
945 static char *tcc_strdup(const char *str)
947 char *ptr;
948 ptr = tcc_malloc(strlen(str) + 1);
949 strcpy(ptr, str);
950 return ptr;
953 #define free(p) use_tcc_free(p)
954 #define malloc(s) use_tcc_malloc(s)
955 #define realloc(p, s) use_tcc_realloc(p, s)
957 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
959 int nb, nb_alloc;
960 void **pp;
962 nb = *nb_ptr;
963 pp = *ptab;
964 /* every power of two we double array size */
965 if ((nb & (nb - 1)) == 0) {
966 if (!nb)
967 nb_alloc = 1;
968 else
969 nb_alloc = nb * 2;
970 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
971 if (!pp)
972 error("memory full");
973 *ptab = pp;
975 pp[nb++] = data;
976 *nb_ptr = nb;
979 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
981 Section *sec;
983 sec = tcc_mallocz(sizeof(Section) + strlen(name));
984 strcpy(sec->name, name);
985 sec->sh_type = sh_type;
986 sec->sh_flags = sh_flags;
987 switch(sh_type) {
988 case SHT_HASH:
989 case SHT_REL:
990 case SHT_DYNSYM:
991 case SHT_SYMTAB:
992 case SHT_DYNAMIC:
993 sec->sh_addralign = 4;
994 break;
995 case SHT_STRTAB:
996 sec->sh_addralign = 1;
997 break;
998 default:
999 sec->sh_addralign = 32; /* default conservative alignment */
1000 break;
1003 /* only add section if not private */
1004 if (!(sh_flags & SHF_PRIVATE)) {
1005 sec->sh_num = s1->nb_sections;
1006 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1008 return sec;
1011 static void free_section(Section *s)
1013 tcc_free(s->data);
1014 tcc_free(s);
1017 /* realloc section and set its content to zero */
1018 static void section_realloc(Section *sec, unsigned long new_size)
1020 unsigned long size;
1021 unsigned char *data;
1023 size = sec->data_allocated;
1024 if (size == 0)
1025 size = 1;
1026 while (size < new_size)
1027 size = size * 2;
1028 data = tcc_realloc(sec->data, size);
1029 if (!data)
1030 error("memory full");
1031 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1032 sec->data = data;
1033 sec->data_allocated = size;
1036 /* reserve at least 'size' bytes in section 'sec' from
1037 sec->data_offset. */
1038 static void *section_ptr_add(Section *sec, unsigned long size)
1040 unsigned long offset, offset1;
1042 offset = sec->data_offset;
1043 offset1 = offset + size;
1044 if (offset1 > sec->data_allocated)
1045 section_realloc(sec, offset1);
1046 sec->data_offset = offset1;
1047 return sec->data + offset;
1050 /* return a reference to a section, and create it if it does not
1051 exists */
1052 Section *find_section(TCCState *s1, const char *name)
1054 Section *sec;
1055 int i;
1056 for(i = 1; i < s1->nb_sections; i++) {
1057 sec = s1->sections[i];
1058 if (!strcmp(name, sec->name))
1059 return sec;
1061 /* sections are created as PROGBITS */
1062 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1065 /* update sym->c so that it points to an external symbol in section
1066 'section' with value 'value' */
1067 static void put_extern_sym(Sym *sym, Section *section,
1068 unsigned long value, unsigned long size)
1070 int sym_type, sym_bind, sh_num, info;
1071 Elf32_Sym *esym;
1072 const char *name;
1074 if (section)
1075 sh_num = section->sh_num;
1076 else
1077 sh_num = SHN_UNDEF;
1078 if (!sym->c) {
1079 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1080 sym_type = STT_FUNC;
1081 else
1082 sym_type = STT_OBJECT;
1083 if (sym->type.t & VT_STATIC)
1084 sym_bind = STB_LOCAL;
1085 else
1086 sym_bind = STB_GLOBAL;
1088 name = get_tok_str(sym->v, NULL);
1089 #ifdef CONFIG_TCC_BCHECK
1090 if (do_bounds_check) {
1091 char buf[32];
1093 /* XXX: avoid doing that for statics ? */
1094 /* if bound checking is activated, we change some function
1095 names by adding the "__bound" prefix */
1096 switch(sym->v) {
1097 #if 0
1098 /* XXX: we rely only on malloc hooks */
1099 case TOK_malloc:
1100 case TOK_free:
1101 case TOK_realloc:
1102 case TOK_memalign:
1103 case TOK_calloc:
1104 #endif
1105 case TOK_memcpy:
1106 case TOK_memmove:
1107 case TOK_memset:
1108 case TOK_strlen:
1109 case TOK_strcpy:
1110 strcpy(buf, "__bound_");
1111 strcat(buf, name);
1112 name = buf;
1113 break;
1116 #endif
1117 info = ELF32_ST_INFO(sym_bind, sym_type);
1118 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1119 } else {
1120 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1121 esym->st_value = value;
1122 esym->st_size = size;
1123 esym->st_shndx = sh_num;
1127 /* add a new relocation entry to symbol 'sym' in section 's' */
1128 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1130 if (!sym->c)
1131 put_extern_sym(sym, NULL, 0, 0);
1132 /* now we can add ELF relocation info */
1133 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1136 static inline int isid(int c)
1138 return (c >= 'a' && c <= 'z') ||
1139 (c >= 'A' && c <= 'Z') ||
1140 c == '_';
1143 static inline int isnum(int c)
1145 return c >= '0' && c <= '9';
1148 static inline int isoct(int c)
1150 return c >= '0' && c <= '7';
1153 static inline int toup(int c)
1155 if (c >= 'a' && c <= 'z')
1156 return c - 'a' + 'A';
1157 else
1158 return c;
1161 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1163 int len;
1164 len = strlen(buf);
1165 vsnprintf(buf + len, buf_size - len, fmt, ap);
1168 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1170 va_list ap;
1171 va_start(ap, fmt);
1172 strcat_vprintf(buf, buf_size, fmt, ap);
1173 va_end(ap);
1176 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1178 char buf[2048];
1179 BufferedFile **f;
1181 buf[0] = '\0';
1182 if (file) {
1183 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1184 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1185 (*f)->filename, (*f)->line_num);
1186 if (file->line_num > 0) {
1187 strcat_printf(buf, sizeof(buf),
1188 "%s:%d: ", file->filename, file->line_num);
1189 } else {
1190 strcat_printf(buf, sizeof(buf),
1191 "%s: ", file->filename);
1193 } else {
1194 strcat_printf(buf, sizeof(buf),
1195 "tcc: ");
1197 if (is_warning)
1198 strcat_printf(buf, sizeof(buf), "warning: ");
1199 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1201 if (!s1->error_func) {
1202 /* default case: stderr */
1203 fprintf(stderr, "%s\n", buf);
1204 } else {
1205 s1->error_func(s1->error_opaque, buf);
1207 if (!is_warning || s1->warn_error)
1208 s1->nb_errors++;
1211 #ifdef LIBTCC
1212 void tcc_set_error_func(TCCState *s, void *error_opaque,
1213 void (*error_func)(void *opaque, const char *msg))
1215 s->error_opaque = error_opaque;
1216 s->error_func = error_func;
1218 #endif
1220 /* error without aborting current compilation */
1221 void error_noabort(const char *fmt, ...)
1223 TCCState *s1 = tcc_state;
1224 va_list ap;
1226 va_start(ap, fmt);
1227 error1(s1, 0, fmt, ap);
1228 va_end(ap);
1231 void error(const char *fmt, ...)
1233 TCCState *s1 = tcc_state;
1234 va_list ap;
1236 va_start(ap, fmt);
1237 error1(s1, 0, fmt, ap);
1238 va_end(ap);
1239 /* better than nothing: in some cases, we accept to handle errors */
1240 if (s1->error_set_jmp_enabled) {
1241 longjmp(s1->error_jmp_buf, 1);
1242 } else {
1243 /* XXX: eliminate this someday */
1244 exit(1);
1248 void expect(const char *msg)
1250 error("%s expected", msg);
1253 void warning(const char *fmt, ...)
1255 TCCState *s1 = tcc_state;
1256 va_list ap;
1258 if (s1->warn_none)
1259 return;
1261 va_start(ap, fmt);
1262 error1(s1, 1, fmt, ap);
1263 va_end(ap);
1266 void skip(int c)
1268 if (tok != c)
1269 error("'%c' expected", c);
1270 next();
1273 static void test_lvalue(void)
1275 if (!(vtop->r & VT_LVAL))
1276 expect("lvalue");
1279 /* allocate a new token */
1280 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1282 TokenSym *ts, **ptable;
1283 int i;
1285 if (tok_ident >= SYM_FIRST_ANOM)
1286 error("memory full");
1288 /* expand token table if needed */
1289 i = tok_ident - TOK_IDENT;
1290 if ((i % TOK_ALLOC_INCR) == 0) {
1291 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1292 if (!ptable)
1293 error("memory full");
1294 table_ident = ptable;
1297 ts = tcc_malloc(sizeof(TokenSym) + len);
1298 table_ident[i] = ts;
1299 ts->tok = tok_ident++;
1300 ts->sym_define = NULL;
1301 ts->sym_label = NULL;
1302 ts->sym_struct = NULL;
1303 ts->sym_identifier = NULL;
1304 ts->len = len;
1305 ts->hash_next = NULL;
1306 memcpy(ts->str, str, len);
1307 ts->str[len] = '\0';
1308 *pts = ts;
1309 return ts;
1312 #define TOK_HASH_INIT 1
1313 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1315 /* find a token and add it if not found */
1316 static TokenSym *tok_alloc(const char *str, int len)
1318 TokenSym *ts, **pts;
1319 int i;
1320 unsigned int h;
1322 h = TOK_HASH_INIT;
1323 for(i=0;i<len;i++)
1324 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1325 h &= (TOK_HASH_SIZE - 1);
1327 pts = &hash_ident[h];
1328 for(;;) {
1329 ts = *pts;
1330 if (!ts)
1331 break;
1332 if (ts->len == len && !memcmp(ts->str, str, len))
1333 return ts;
1334 pts = &(ts->hash_next);
1336 return tok_alloc_new(pts, str, len);
1339 /* CString handling */
1341 static void cstr_realloc(CString *cstr, int new_size)
1343 int size;
1344 void *data;
1346 size = cstr->size_allocated;
1347 if (size == 0)
1348 size = 8; /* no need to allocate a too small first string */
1349 while (size < new_size)
1350 size = size * 2;
1351 data = tcc_realloc(cstr->data_allocated, size);
1352 if (!data)
1353 error("memory full");
1354 cstr->data_allocated = data;
1355 cstr->size_allocated = size;
1356 cstr->data = data;
1359 /* add a byte */
1360 static void cstr_ccat(CString *cstr, int ch)
1362 int size;
1363 size = cstr->size + 1;
1364 if (size > cstr->size_allocated)
1365 cstr_realloc(cstr, size);
1366 ((unsigned char *)cstr->data)[size - 1] = ch;
1367 cstr->size = size;
1370 static void cstr_cat(CString *cstr, const char *str)
1372 int c;
1373 for(;;) {
1374 c = *str;
1375 if (c == '\0')
1376 break;
1377 cstr_ccat(cstr, c);
1378 str++;
1382 /* add a wide char */
1383 static void cstr_wccat(CString *cstr, int ch)
1385 int size;
1386 size = cstr->size + sizeof(int);
1387 if (size > cstr->size_allocated)
1388 cstr_realloc(cstr, size);
1389 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1390 cstr->size = size;
1393 static void cstr_new(CString *cstr)
1395 memset(cstr, 0, sizeof(CString));
1398 /* free string and reset it to NULL */
1399 static void cstr_free(CString *cstr)
1401 tcc_free(cstr->data_allocated);
1402 cstr_new(cstr);
1405 #define cstr_reset(cstr) cstr_free(cstr)
1407 static CString *cstr_dup(CString *cstr1)
1409 CString *cstr;
1410 int size;
1412 cstr = tcc_malloc(sizeof(CString));
1413 size = cstr1->size;
1414 cstr->size = size;
1415 cstr->size_allocated = size;
1416 cstr->data_allocated = tcc_malloc(size);
1417 cstr->data = cstr->data_allocated;
1418 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1419 return cstr;
1422 /* XXX: unicode ? */
1423 static void add_char(CString *cstr, int c)
1425 if (c == '\'' || c == '\"' || c == '\\') {
1426 /* XXX: could be more precise if char or string */
1427 cstr_ccat(cstr, '\\');
1429 if (c >= 32 && c <= 126) {
1430 cstr_ccat(cstr, c);
1431 } else {
1432 cstr_ccat(cstr, '\\');
1433 if (c == '\n') {
1434 cstr_ccat(cstr, 'n');
1435 } else {
1436 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1437 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1438 cstr_ccat(cstr, '0' + (c & 7));
1443 /* XXX: buffer overflow */
1444 /* XXX: float tokens */
1445 char *get_tok_str(int v, CValue *cv)
1447 static char buf[STRING_MAX_SIZE + 1];
1448 static CString cstr_buf;
1449 CString *cstr;
1450 unsigned char *q;
1451 char *p;
1452 int i, len;
1454 /* NOTE: to go faster, we give a fixed buffer for small strings */
1455 cstr_reset(&cstr_buf);
1456 cstr_buf.data = buf;
1457 cstr_buf.size_allocated = sizeof(buf);
1458 p = buf;
1460 switch(v) {
1461 case TOK_CINT:
1462 case TOK_CUINT:
1463 /* XXX: not quite exact, but only useful for testing */
1464 sprintf(p, "%u", cv->ui);
1465 break;
1466 case TOK_CLLONG:
1467 case TOK_CULLONG:
1468 /* XXX: not quite exact, but only useful for testing */
1469 sprintf(p, "%Lu", cv->ull);
1470 break;
1471 case TOK_CCHAR:
1472 case TOK_LCHAR:
1473 cstr_ccat(&cstr_buf, '\'');
1474 add_char(&cstr_buf, cv->i);
1475 cstr_ccat(&cstr_buf, '\'');
1476 cstr_ccat(&cstr_buf, '\0');
1477 break;
1478 case TOK_PPNUM:
1479 cstr = cv->cstr;
1480 len = cstr->size - 1;
1481 for(i=0;i<len;i++)
1482 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1483 cstr_ccat(&cstr_buf, '\0');
1484 break;
1485 case TOK_STR:
1486 case TOK_LSTR:
1487 cstr = cv->cstr;
1488 cstr_ccat(&cstr_buf, '\"');
1489 if (v == TOK_STR) {
1490 len = cstr->size - 1;
1491 for(i=0;i<len;i++)
1492 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1493 } else {
1494 len = (cstr->size / sizeof(int)) - 1;
1495 for(i=0;i<len;i++)
1496 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1498 cstr_ccat(&cstr_buf, '\"');
1499 cstr_ccat(&cstr_buf, '\0');
1500 break;
1501 case TOK_LT:
1502 v = '<';
1503 goto addv;
1504 case TOK_GT:
1505 v = '>';
1506 goto addv;
1507 case TOK_A_SHL:
1508 return strcpy(p, "<<=");
1509 case TOK_A_SAR:
1510 return strcpy(p, ">>=");
1511 default:
1512 if (v < TOK_IDENT) {
1513 /* search in two bytes table */
1514 q = tok_two_chars;
1515 while (*q) {
1516 if (q[2] == v) {
1517 *p++ = q[0];
1518 *p++ = q[1];
1519 *p = '\0';
1520 return buf;
1522 q += 3;
1524 addv:
1525 *p++ = v;
1526 *p = '\0';
1527 } else if (v < tok_ident) {
1528 return table_ident[v - TOK_IDENT]->str;
1529 } else if (v >= SYM_FIRST_ANOM) {
1530 /* special name for anonymous symbol */
1531 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1532 } else {
1533 /* should never happen */
1534 return NULL;
1536 break;
1538 return cstr_buf.data;
1541 /* push, without hashing */
1542 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1544 Sym *s;
1545 s = tcc_malloc(sizeof(Sym));
1546 s->v = v;
1547 s->type.t = t;
1548 s->c = c;
1549 s->next = NULL;
1550 /* add in stack */
1551 s->prev = *ps;
1552 *ps = s;
1553 return s;
1556 /* find a symbol and return its associated structure. 's' is the top
1557 of the symbol stack */
1558 static Sym *sym_find2(Sym *s, int v)
1560 while (s) {
1561 if (s->v == v)
1562 return s;
1563 s = s->prev;
1565 return NULL;
1568 /* structure lookup */
1569 static inline Sym *struct_find(int v)
1571 v -= TOK_IDENT;
1572 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1573 return NULL;
1574 return table_ident[v]->sym_struct;
1577 /* find an identifier */
1578 static inline Sym *sym_find(int v)
1580 v -= TOK_IDENT;
1581 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1582 return NULL;
1583 return table_ident[v]->sym_identifier;
1586 /* push a given symbol on the symbol stack */
1587 static Sym *sym_push(int v, CType *type, int r, int c)
1589 Sym *s, **ps;
1590 TokenSym *ts;
1592 if (local_stack)
1593 ps = &local_stack;
1594 else
1595 ps = &global_stack;
1596 s = sym_push2(ps, v, type->t, c);
1597 s->type.ref = type->ref;
1598 s->r = r;
1599 /* don't record fields or anonymous symbols */
1600 /* XXX: simplify */
1601 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1602 /* record symbol in token array */
1603 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1604 if (v & SYM_STRUCT)
1605 ps = &ts->sym_struct;
1606 else
1607 ps = &ts->sym_identifier;
1608 s->prev_tok = *ps;
1609 *ps = s;
1611 return s;
1614 /* push a global identifier */
1615 static Sym *global_identifier_push(int v, int t, int c)
1617 Sym *s, **ps;
1618 s = sym_push2(&global_stack, v, t, c);
1619 /* don't record anonymous symbol */
1620 if (v < SYM_FIRST_ANOM) {
1621 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1622 /* modify the top most local identifier, so that
1623 sym_identifier will point to 's' when popped */
1624 while (*ps != NULL)
1625 ps = &(*ps)->prev_tok;
1626 s->prev_tok = NULL;
1627 *ps = s;
1629 return s;
1632 /* pop symbols until top reaches 'b' */
1633 static void sym_pop(Sym **ptop, Sym *b)
1635 Sym *s, *ss, **ps;
1636 TokenSym *ts;
1637 int v;
1639 s = *ptop;
1640 while(s != b) {
1641 ss = s->prev;
1642 v = s->v;
1643 /* remove symbol in token array */
1644 /* XXX: simplify */
1645 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1646 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1647 if (v & SYM_STRUCT)
1648 ps = &ts->sym_struct;
1649 else
1650 ps = &ts->sym_identifier;
1651 *ps = s->prev_tok;
1653 tcc_free(s);
1654 s = ss;
1656 *ptop = b;
1659 /* I/O layer */
1661 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1663 int fd;
1664 BufferedFile *bf;
1666 fd = open(filename, O_RDONLY);
1667 if (fd < 0)
1668 return NULL;
1669 bf = tcc_malloc(sizeof(BufferedFile));
1670 if (!bf) {
1671 close(fd);
1672 return NULL;
1674 bf->fd = fd;
1675 bf->buf_ptr = bf->buffer;
1676 bf->buf_end = bf->buffer;
1677 bf->buffer[0] = CH_EOB; /* put eob symbol */
1678 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1679 bf->line_num = 1;
1680 bf->ifndef_macro = 0;
1681 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1682 // printf("opening '%s'\n", filename);
1683 return bf;
1686 void tcc_close(BufferedFile *bf)
1688 total_lines += bf->line_num;
1689 close(bf->fd);
1690 tcc_free(bf);
1693 /* fill input buffer and peek next char */
1694 static int tcc_peekc_slow(BufferedFile *bf)
1696 int len;
1697 /* only tries to read if really end of buffer */
1698 if (bf->buf_ptr >= bf->buf_end) {
1699 if (bf->fd != -1) {
1700 #if defined(PARSE_DEBUG)
1701 len = 8;
1702 #else
1703 len = IO_BUF_SIZE;
1704 #endif
1705 len = read(bf->fd, bf->buffer, len);
1706 if (len < 0)
1707 len = 0;
1708 } else {
1709 len = 0;
1711 total_bytes += len;
1712 bf->buf_ptr = bf->buffer;
1713 bf->buf_end = bf->buffer + len;
1714 *bf->buf_end = CH_EOB;
1716 if (bf->buf_ptr < bf->buf_end) {
1717 return bf->buf_ptr[0];
1718 } else {
1719 bf->buf_ptr = bf->buf_end;
1720 return CH_EOF;
1724 /* return the current character, handling end of block if necessary
1725 (but not stray) */
1726 static int handle_eob(void)
1728 return tcc_peekc_slow(file);
1731 /* read next char from current input file and handle end of input buffer */
1732 static inline void inp(void)
1734 ch = *(++(file->buf_ptr));
1735 /* end of buffer/file handling */
1736 if (ch == CH_EOB)
1737 ch = handle_eob();
1740 /* handle '\[\r]\n' */
1741 static void handle_stray(void)
1743 while (ch == '\\') {
1744 inp();
1745 if (ch == '\n') {
1746 file->line_num++;
1747 inp();
1748 } else if (ch == '\r') {
1749 inp();
1750 if (ch != '\n')
1751 goto fail;
1752 file->line_num++;
1753 inp();
1754 } else {
1755 fail:
1756 error("stray '\\' in program");
1761 /* skip the stray and handle the \\n case. Output an error if
1762 incorrect char after the stray */
1763 static int handle_stray1(uint8_t *p)
1765 int c;
1767 if (p >= file->buf_end) {
1768 file->buf_ptr = p;
1769 c = handle_eob();
1770 p = file->buf_ptr;
1771 if (c == '\\')
1772 goto parse_stray;
1773 } else {
1774 parse_stray:
1775 file->buf_ptr = p;
1776 ch = *p;
1777 handle_stray();
1778 p = file->buf_ptr;
1779 c = *p;
1781 return c;
1784 /* handle just the EOB case, but not stray */
1785 #define PEEKC_EOB(c, p)\
1787 p++;\
1788 c = *p;\
1789 if (c == '\\') {\
1790 file->buf_ptr = p;\
1791 c = handle_eob();\
1792 p = file->buf_ptr;\
1796 /* handle the complicated stray case */
1797 #define PEEKC(c, p)\
1799 p++;\
1800 c = *p;\
1801 if (c == '\\') {\
1802 c = handle_stray1(p);\
1803 p = file->buf_ptr;\
1807 /* input with '\[\r]\n' handling. Note that this function cannot
1808 handle other characters after '\', so you cannot call it inside
1809 strings or comments */
1810 static void minp(void)
1812 inp();
1813 if (ch == '\\')
1814 handle_stray();
1818 /* single line C++ comments */
1819 static uint8_t *parse_line_comment(uint8_t *p)
1821 int c;
1823 p++;
1824 for(;;) {
1825 c = *p;
1826 redo:
1827 if (c == '\n' || c == CH_EOF) {
1828 break;
1829 } else if (c == '\\') {
1830 file->buf_ptr = p;
1831 c = handle_eob();
1832 p = file->buf_ptr;
1833 if (c == '\\') {
1834 PEEKC_EOB(c, p);
1835 if (c == '\n') {
1836 file->line_num++;
1837 PEEKC_EOB(c, p);
1838 } else if (c == '\r') {
1839 PEEKC_EOB(c, p);
1840 if (c == '\n') {
1841 file->line_num++;
1842 PEEKC_EOB(c, p);
1845 } else {
1846 goto redo;
1848 } else {
1849 p++;
1852 return p;
1855 /* C comments */
1856 static uint8_t *parse_comment(uint8_t *p)
1858 int c;
1860 p++;
1861 for(;;) {
1862 /* fast skip loop */
1863 for(;;) {
1864 c = *p;
1865 if (c == '\n' || c == '*' || c == '\\')
1866 break;
1867 p++;
1868 c = *p;
1869 if (c == '\n' || c == '*' || c == '\\')
1870 break;
1871 p++;
1873 /* now we can handle all the cases */
1874 if (c == '\n') {
1875 file->line_num++;
1876 p++;
1877 } else if (c == '*') {
1878 p++;
1879 for(;;) {
1880 c = *p;
1881 if (c == '*') {
1882 p++;
1883 } else if (c == '/') {
1884 goto end_of_comment;
1885 } else if (c == '\\') {
1886 file->buf_ptr = p;
1887 c = handle_eob();
1888 p = file->buf_ptr;
1889 if (c == '\\') {
1890 /* skip '\[\r]\n', otherwise just skip the stray */
1891 while (c == '\\') {
1892 PEEKC_EOB(c, p);
1893 if (c == '\n') {
1894 file->line_num++;
1895 PEEKC_EOB(c, p);
1896 } else if (c == '\r') {
1897 PEEKC_EOB(c, p);
1898 if (c == '\n') {
1899 file->line_num++;
1900 PEEKC_EOB(c, p);
1902 } else {
1903 goto after_star;
1907 } else {
1908 break;
1911 after_star: ;
1912 } else {
1913 /* stray, eob or eof */
1914 file->buf_ptr = p;
1915 c = handle_eob();
1916 p = file->buf_ptr;
1917 if (c == CH_EOF) {
1918 error("unexpected end of file in comment");
1919 } else if (c == '\\') {
1920 p++;
1924 end_of_comment:
1925 p++;
1926 return p;
1929 #define cinp minp
1931 /* space exlcuding newline */
1932 static inline int is_space(int ch)
1934 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1937 static inline void skip_spaces(void)
1939 while (is_space(ch))
1940 cinp();
1943 /* parse a string without interpreting escapes */
1944 static uint8_t *parse_pp_string(uint8_t *p,
1945 int sep, CString *str)
1947 int c;
1948 p++;
1949 for(;;) {
1950 c = *p;
1951 if (c == sep) {
1952 break;
1953 } else if (c == '\\') {
1954 file->buf_ptr = p;
1955 c = handle_eob();
1956 p = file->buf_ptr;
1957 if (c == CH_EOF) {
1958 unterminated_string:
1959 /* XXX: indicate line number of start of string */
1960 error("missing terminating %c character", sep);
1961 } else if (c == '\\') {
1962 /* escape : just skip \[\r]\n */
1963 PEEKC_EOB(c, p);
1964 if (c == '\n') {
1965 file->line_num++;
1966 p++;
1967 } else if (c == '\r') {
1968 PEEKC_EOB(c, p);
1969 if (c != '\n')
1970 expect("'\n' after '\r'");
1971 file->line_num++;
1972 p++;
1973 } else if (c == CH_EOF) {
1974 goto unterminated_string;
1975 } else {
1976 if (str) {
1977 cstr_ccat(str, '\\');
1978 cstr_ccat(str, c);
1980 p++;
1983 } else if (c == '\n') {
1984 file->line_num++;
1985 goto add_char;
1986 } else if (c == '\r') {
1987 PEEKC_EOB(c, p);
1988 if (c != '\n') {
1989 cstr_ccat(str, '\r');
1990 } else {
1991 file->line_num++;
1992 goto add_char;
1994 } else {
1995 add_char:
1996 if (str)
1997 cstr_ccat(str, c);
1998 p++;
2001 p++;
2002 return p;
2005 /* skip block of text until #else, #elif or #endif. skip also pairs of
2006 #if/#endif */
2007 void preprocess_skip(void)
2009 int a, start_of_line, c;
2010 uint8_t *p;
2012 p = file->buf_ptr;
2013 start_of_line = 1;
2014 a = 0;
2015 for(;;) {
2016 redo_no_start:
2017 c = *p;
2018 switch(c) {
2019 case ' ':
2020 case '\t':
2021 case '\f':
2022 case '\v':
2023 case '\r':
2024 p++;
2025 goto redo_no_start;
2026 case '\n':
2027 start_of_line = 1;
2028 file->line_num++;
2029 p++;
2030 goto redo_no_start;
2031 case '\\':
2032 file->buf_ptr = p;
2033 c = handle_eob();
2034 if (c == CH_EOF) {
2035 expect("#endif");
2036 } else if (c == '\\') {
2037 /* XXX: incorrect: should not give an error */
2038 ch = file->buf_ptr[0];
2039 handle_stray();
2041 p = file->buf_ptr;
2042 goto redo_no_start;
2043 /* skip strings */
2044 case '\"':
2045 case '\'':
2046 p = parse_pp_string(p, c, NULL);
2047 break;
2048 /* skip comments */
2049 case '/':
2050 file->buf_ptr = p;
2051 ch = *p;
2052 minp();
2053 p = file->buf_ptr;
2054 if (ch == '*') {
2055 p = parse_comment(p);
2056 } else if (ch == '/') {
2057 p = parse_line_comment(p);
2059 break;
2061 case '#':
2062 p++;
2063 if (start_of_line) {
2064 file->buf_ptr = p;
2065 next_nomacro();
2066 p = file->buf_ptr;
2067 if (a == 0 &&
2068 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2069 goto the_end;
2070 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2071 a++;
2072 else if (tok == TOK_ENDIF)
2073 a--;
2075 break;
2076 default:
2077 p++;
2078 break;
2080 start_of_line = 0;
2082 the_end: ;
2083 file->buf_ptr = p;
2086 /* ParseState handling */
2088 /* XXX: currently, no include file info is stored. Thus, we cannot display
2089 accurate messages if the function or data definition spans multiple
2090 files */
2092 /* save current parse state in 's' */
2093 void save_parse_state(ParseState *s)
2095 s->line_num = file->line_num;
2096 s->macro_ptr = macro_ptr;
2097 s->tok = tok;
2098 s->tokc = tokc;
2101 /* restore parse state from 's' */
2102 void restore_parse_state(ParseState *s)
2104 file->line_num = s->line_num;
2105 macro_ptr = s->macro_ptr;
2106 tok = s->tok;
2107 tokc = s->tokc;
2110 /* return the number of additional 'ints' necessary to store the
2111 token */
2112 static inline int tok_ext_size(int t)
2114 switch(t) {
2115 /* 4 bytes */
2116 case TOK_CINT:
2117 case TOK_CUINT:
2118 case TOK_CCHAR:
2119 case TOK_LCHAR:
2120 case TOK_STR:
2121 case TOK_LSTR:
2122 case TOK_CFLOAT:
2123 case TOK_LINENUM:
2124 case TOK_PPNUM:
2125 return 1;
2126 case TOK_CDOUBLE:
2127 case TOK_CLLONG:
2128 case TOK_CULLONG:
2129 return 2;
2130 case TOK_CLDOUBLE:
2131 return LDOUBLE_SIZE / 4;
2132 default:
2133 return 0;
2137 /* token string handling */
2139 static inline void tok_str_new(TokenString *s)
2141 s->str = NULL;
2142 s->len = 0;
2143 s->allocated_len = 0;
2144 s->last_line_num = -1;
2147 static void tok_str_free(int *str)
2149 const int *p;
2150 CString *cstr;
2151 int t;
2153 p = str;
2154 for(;;) {
2155 t = *p;
2156 /* NOTE: we test zero separately so that GCC can generate a
2157 table for the following switch */
2158 if (t == 0)
2159 break;
2160 switch(t) {
2161 case TOK_CINT:
2162 case TOK_CUINT:
2163 case TOK_CCHAR:
2164 case TOK_LCHAR:
2165 case TOK_CFLOAT:
2166 case TOK_LINENUM:
2167 p += 2;
2168 break;
2169 case TOK_PPNUM:
2170 case TOK_STR:
2171 case TOK_LSTR:
2172 /* XXX: use a macro to be portable on 64 bit ? */
2173 cstr = (CString *)p[1];
2174 cstr_free(cstr);
2175 tcc_free(cstr);
2176 p += 2;
2177 break;
2178 case TOK_CDOUBLE:
2179 case TOK_CLLONG:
2180 case TOK_CULLONG:
2181 p += 3;
2182 break;
2183 case TOK_CLDOUBLE:
2184 p += 1 + (LDOUBLE_SIZE / 4);
2185 break;
2186 default:
2187 p++;
2188 break;
2191 tcc_free(str);
2194 static int *tok_str_realloc(TokenString *s)
2196 int *str, len;
2198 len = s->allocated_len + TOK_STR_ALLOC_INCR;
2199 str = tcc_realloc(s->str, len * sizeof(int));
2200 if (!str)
2201 error("memory full");
2202 s->allocated_len = len;
2203 s->str = str;
2204 return str;
2207 static void tok_str_add(TokenString *s, int t)
2209 int len, *str;
2211 len = s->len;
2212 str = s->str;
2213 if (len >= s->allocated_len)
2214 str = tok_str_realloc(s);
2215 str[len++] = t;
2216 s->len = len;
2219 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2221 int len, *str;
2223 len = s->len;
2224 str = s->str;
2226 /* allocate space for worst case */
2227 if (len + TOK_MAX_SIZE > s->allocated_len)
2228 str = tok_str_realloc(s);
2229 str[len++] = t;
2230 switch(t) {
2231 case TOK_CINT:
2232 case TOK_CUINT:
2233 case TOK_CCHAR:
2234 case TOK_LCHAR:
2235 case TOK_CFLOAT:
2236 case TOK_LINENUM:
2237 str[len++] = cv->tab[0];
2238 break;
2239 case TOK_PPNUM:
2240 case TOK_STR:
2241 case TOK_LSTR:
2242 str[len++] = (int)cstr_dup(cv->cstr);
2243 break;
2244 case TOK_CDOUBLE:
2245 case TOK_CLLONG:
2246 case TOK_CULLONG:
2247 #if LDOUBLE_SIZE == 8
2248 case TOK_CLDOUBLE:
2249 #endif
2250 str[len++] = cv->tab[0];
2251 str[len++] = cv->tab[1];
2252 break;
2253 #if LDOUBLE_SIZE == 12
2254 case TOK_CLDOUBLE:
2255 str[len++] = cv->tab[0];
2256 str[len++] = cv->tab[1];
2257 str[len++] = cv->tab[2];
2258 #elif LDOUBLE_SIZE != 8
2259 #error add long double size support
2260 #endif
2261 break;
2262 default:
2263 break;
2265 s->len = len;
2268 /* add the current parse token in token string 's' */
2269 static void tok_str_add_tok(TokenString *s)
2271 CValue cval;
2273 /* save line number info */
2274 if (file->line_num != s->last_line_num) {
2275 s->last_line_num = file->line_num;
2276 cval.i = s->last_line_num;
2277 tok_str_add2(s, TOK_LINENUM, &cval);
2279 tok_str_add2(s, tok, &tokc);
2282 #if LDOUBLE_SIZE == 12
2283 #define LDOUBLE_GET(p, cv) \
2284 cv.tab[0] = p[0]; \
2285 cv.tab[1] = p[1]; \
2286 cv.tab[2] = p[2];
2287 #elif LDOUBLE_SIZE == 8
2288 #define LDOUBLE_GET(p, cv) \
2289 cv.tab[0] = p[0]; \
2290 cv.tab[1] = p[1];
2291 #else
2292 #error add long double size support
2293 #endif
2296 /* get a token from an integer array and increment pointer
2297 accordingly. we code it as a macro to avoid pointer aliasing. */
2298 #define TOK_GET(t, p, cv) \
2300 t = *p++; \
2301 switch(t) { \
2302 case TOK_CINT: \
2303 case TOK_CUINT: \
2304 case TOK_CCHAR: \
2305 case TOK_LCHAR: \
2306 case TOK_CFLOAT: \
2307 case TOK_LINENUM: \
2308 case TOK_STR: \
2309 case TOK_LSTR: \
2310 case TOK_PPNUM: \
2311 cv.tab[0] = *p++; \
2312 break; \
2313 case TOK_CDOUBLE: \
2314 case TOK_CLLONG: \
2315 case TOK_CULLONG: \
2316 cv.tab[0] = p[0]; \
2317 cv.tab[1] = p[1]; \
2318 p += 2; \
2319 break; \
2320 case TOK_CLDOUBLE: \
2321 LDOUBLE_GET(p, cv); \
2322 p += LDOUBLE_SIZE / 4; \
2323 break; \
2324 default: \
2325 break; \
2329 /* defines handling */
2330 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2332 Sym *s;
2334 s = sym_push2(&define_stack, v, macro_type, (int)str);
2335 s->next = first_arg;
2336 table_ident[v - TOK_IDENT]->sym_define = s;
2339 /* undefined a define symbol. Its name is just set to zero */
2340 static void define_undef(Sym *s)
2342 int v;
2343 v = s->v;
2344 if (v >= TOK_IDENT && v < tok_ident)
2345 table_ident[v - TOK_IDENT]->sym_define = NULL;
2346 s->v = 0;
2349 static inline Sym *define_find(int v)
2351 v -= TOK_IDENT;
2352 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2353 return NULL;
2354 return table_ident[v]->sym_define;
2357 /* free define stack until top reaches 'b' */
2358 static void free_defines(Sym *b)
2360 Sym *top, *top1;
2361 int v;
2363 top = define_stack;
2364 while (top != b) {
2365 top1 = top->prev;
2366 /* do not free args or predefined defines */
2367 if (top->c)
2368 tok_str_free((int *)top->c);
2369 v = top->v;
2370 if (v >= TOK_IDENT && v < tok_ident)
2371 table_ident[v - TOK_IDENT]->sym_define = NULL;
2372 tcc_free(top);
2373 top = top1;
2375 define_stack = b;
2378 /* label lookup */
2379 static Sym *label_find(int v)
2381 v -= TOK_IDENT;
2382 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2383 return NULL;
2384 return table_ident[v]->sym_label;
2387 static Sym *label_push(Sym **ptop, int v, int flags)
2389 Sym *s, **ps;
2390 s = sym_push2(ptop, v, 0, 0);
2391 s->r = flags;
2392 ps = &table_ident[v - TOK_IDENT]->sym_label;
2393 if (ptop == &global_label_stack) {
2394 /* modify the top most local identifier, so that
2395 sym_identifier will point to 's' when popped */
2396 while (*ps != NULL)
2397 ps = &(*ps)->prev_tok;
2399 s->prev_tok = *ps;
2400 *ps = s;
2401 return s;
2404 /* pop labels until element last is reached. Look if any labels are
2405 undefined. Define symbols if '&&label' was used. */
2406 static void label_pop(Sym **ptop, Sym *slast)
2408 Sym *s, *s1;
2409 for(s = *ptop; s != slast; s = s1) {
2410 s1 = s->prev;
2411 if (s->r == LABEL_DECLARED) {
2412 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2413 } else if (s->r == LABEL_FORWARD) {
2414 error("label '%s' used but not defined",
2415 get_tok_str(s->v, NULL));
2416 } else {
2417 if (s->c) {
2418 /* define corresponding symbol. A size of
2419 1 is put. */
2420 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2423 /* remove label */
2424 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2425 tcc_free(s);
2427 *ptop = slast;
2430 /* eval an expression for #if/#elif */
2431 static int expr_preprocess(void)
2433 int c, t;
2434 TokenString str;
2436 tok_str_new(&str);
2437 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2438 next(); /* do macro subst */
2439 if (tok == TOK_DEFINED) {
2440 next_nomacro();
2441 t = tok;
2442 if (t == '(')
2443 next_nomacro();
2444 c = define_find(tok) != 0;
2445 if (t == '(')
2446 next_nomacro();
2447 tok = TOK_CINT;
2448 tokc.i = c;
2449 } else if (tok >= TOK_IDENT) {
2450 /* if undefined macro */
2451 tok = TOK_CINT;
2452 tokc.i = 0;
2454 tok_str_add_tok(&str);
2456 tok_str_add(&str, -1); /* simulate end of file */
2457 tok_str_add(&str, 0);
2458 /* now evaluate C constant expression */
2459 macro_ptr = str.str;
2460 next();
2461 c = expr_const();
2462 macro_ptr = NULL;
2463 tok_str_free(str.str);
2464 return c != 0;
2467 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2468 static void tok_print(int *str)
2470 int t;
2471 CValue cval;
2473 while (1) {
2474 TOK_GET(t, str, cval);
2475 if (!t)
2476 break;
2477 printf(" %s", get_tok_str(t, &cval));
2479 printf("\n");
2481 #endif
2483 /* parse after #define */
2484 static void parse_define(void)
2486 Sym *s, *first, **ps;
2487 int v, t, varg, is_vaargs, c;
2488 TokenString str;
2490 v = tok;
2491 if (v < TOK_IDENT)
2492 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2493 /* XXX: should check if same macro (ANSI) */
2494 first = NULL;
2495 t = MACRO_OBJ;
2496 /* '(' must be just after macro definition for MACRO_FUNC */
2497 c = file->buf_ptr[0];
2498 if (c == '\\')
2499 c = handle_stray1(file->buf_ptr);
2500 if (c == '(') {
2501 next_nomacro();
2502 next_nomacro();
2503 ps = &first;
2504 while (tok != ')') {
2505 varg = tok;
2506 next_nomacro();
2507 is_vaargs = 0;
2508 if (varg == TOK_DOTS) {
2509 varg = TOK___VA_ARGS__;
2510 is_vaargs = 1;
2511 } else if (tok == TOK_DOTS && gnu_ext) {
2512 is_vaargs = 1;
2513 next_nomacro();
2515 if (varg < TOK_IDENT)
2516 error("badly punctuated parameter list");
2517 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2518 *ps = s;
2519 ps = &s->next;
2520 if (tok != ',')
2521 break;
2522 next_nomacro();
2524 t = MACRO_FUNC;
2526 tok_str_new(&str);
2527 next_nomacro();
2528 /* EOF testing necessary for '-D' handling */
2529 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2530 tok_str_add2(&str, tok, &tokc);
2531 next_nomacro();
2533 tok_str_add(&str, 0);
2534 #ifdef PP_DEBUG
2535 printf("define %s %d: ", get_tok_str(v, NULL), t);
2536 tok_print(str.str);
2537 #endif
2538 define_push(v, t, str.str, first);
2541 /* XXX: use a token or a hash table to accelerate matching ? */
2542 static CachedInclude *search_cached_include(TCCState *s1,
2543 int type, const char *filename)
2545 CachedInclude *e;
2546 int i;
2548 for(i = 0;i < s1->nb_cached_includes; i++) {
2549 e = s1->cached_includes[i];
2550 if (e->type == type && !strcmp(e->filename, filename))
2551 return e;
2553 return NULL;
2556 static inline void add_cached_include(TCCState *s1, int type,
2557 const char *filename, int ifndef_macro)
2559 CachedInclude *e;
2561 if (search_cached_include(s1, type, filename))
2562 return;
2563 #ifdef INC_DEBUG
2564 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2565 #endif
2566 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2567 if (!e)
2568 return;
2569 e->type = type;
2570 strcpy(e->filename, filename);
2571 e->ifndef_macro = ifndef_macro;
2572 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2575 /* is_bof is true if first non space token at beginning of file */
2576 static void preprocess(int is_bof)
2578 TCCState *s1 = tcc_state;
2579 int size, i, c, n, saved_parse_flags;
2580 char buf[1024], *q, *p;
2581 char buf1[1024];
2582 BufferedFile *f;
2583 Sym *s;
2584 CachedInclude *e;
2586 saved_parse_flags = parse_flags;
2587 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2588 PARSE_FLAG_LINEFEED;
2589 next_nomacro();
2590 redo:
2591 switch(tok) {
2592 case TOK_DEFINE:
2593 next_nomacro();
2594 parse_define();
2595 break;
2596 case TOK_UNDEF:
2597 next_nomacro();
2598 s = define_find(tok);
2599 /* undefine symbol by putting an invalid name */
2600 if (s)
2601 define_undef(s);
2602 break;
2603 case TOK_INCLUDE:
2604 ch = file->buf_ptr[0];
2605 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2606 skip_spaces();
2607 if (ch == '<') {
2608 c = '>';
2609 goto read_name;
2610 } else if (ch == '\"') {
2611 c = ch;
2612 read_name:
2613 /* XXX: better stray handling */
2614 minp();
2615 q = buf;
2616 while (ch != c && ch != '\n' && ch != CH_EOF) {
2617 if ((q - buf) < sizeof(buf) - 1)
2618 *q++ = ch;
2619 minp();
2621 *q = '\0';
2622 minp();
2623 #if 0
2624 /* eat all spaces and comments after include */
2625 /* XXX: slightly incorrect */
2626 while (ch1 != '\n' && ch1 != CH_EOF)
2627 inp();
2628 #endif
2629 } else {
2630 /* computed #include : either we have only strings or
2631 we have anything enclosed in '<>' */
2632 next();
2633 buf[0] = '\0';
2634 if (tok == TOK_STR) {
2635 while (tok != TOK_LINEFEED) {
2636 if (tok != TOK_STR) {
2637 include_syntax:
2638 error("'#include' expects \"FILENAME\" or <FILENAME>");
2640 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2641 next();
2643 c = '\"';
2644 } else {
2645 int len;
2646 while (tok != TOK_LINEFEED) {
2647 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2648 next();
2650 len = strlen(buf);
2651 /* check syntax and remove '<>' */
2652 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2653 goto include_syntax;
2654 memmove(buf, buf + 1, len - 2);
2655 buf[len - 2] = '\0';
2656 c = '>';
2660 e = search_cached_include(s1, c, buf);
2661 if (e && define_find(e->ifndef_macro)) {
2662 /* no need to parse the include because the 'ifndef macro'
2663 is defined */
2664 #ifdef INC_DEBUG
2665 printf("%s: skipping %s\n", file->filename, buf);
2666 #endif
2667 } else {
2668 if (c == '\"') {
2669 /* first search in current dir if "header.h" */
2670 size = 0;
2671 p = strrchr(file->filename, '/');
2672 if (p)
2673 size = p + 1 - file->filename;
2674 if (size > sizeof(buf1) - 1)
2675 size = sizeof(buf1) - 1;
2676 memcpy(buf1, file->filename, size);
2677 buf1[size] = '\0';
2678 pstrcat(buf1, sizeof(buf1), buf);
2679 f = tcc_open(s1, buf1);
2680 if (f)
2681 goto found;
2683 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2684 error("#include recursion too deep");
2685 /* now search in all the include paths */
2686 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2687 for(i = 0; i < n; i++) {
2688 const char *path;
2689 if (i < s1->nb_include_paths)
2690 path = s1->include_paths[i];
2691 else
2692 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2693 pstrcpy(buf1, sizeof(buf1), path);
2694 pstrcat(buf1, sizeof(buf1), "/");
2695 pstrcat(buf1, sizeof(buf1), buf);
2696 f = tcc_open(s1, buf1);
2697 if (f)
2698 goto found;
2700 error("include file '%s' not found", buf);
2701 f = NULL;
2702 found:
2703 #ifdef INC_DEBUG
2704 printf("%s: including %s\n", file->filename, buf1);
2705 #endif
2706 f->inc_type = c;
2707 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2708 /* push current file in stack */
2709 /* XXX: fix current line init */
2710 *s1->include_stack_ptr++ = file;
2711 file = f;
2712 /* add include file debug info */
2713 if (do_debug) {
2714 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2716 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2717 ch = file->buf_ptr[0];
2718 goto the_end;
2720 break;
2721 case TOK_IFNDEF:
2722 c = 1;
2723 goto do_ifdef;
2724 case TOK_IF:
2725 c = expr_preprocess();
2726 goto do_if;
2727 case TOK_IFDEF:
2728 c = 0;
2729 do_ifdef:
2730 next_nomacro();
2731 if (tok < TOK_IDENT)
2732 error("invalid argument for '#if%sdef'", c ? "n" : "");
2733 if (is_bof) {
2734 if (c) {
2735 #ifdef INC_DEBUG
2736 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2737 #endif
2738 file->ifndef_macro = tok;
2741 c = (define_find(tok) != 0) ^ c;
2742 do_if:
2743 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2744 error("memory full");
2745 *s1->ifdef_stack_ptr++ = c;
2746 goto test_skip;
2747 case TOK_ELSE:
2748 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2749 error("#else without matching #if");
2750 if (s1->ifdef_stack_ptr[-1] & 2)
2751 error("#else after #else");
2752 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2753 goto test_skip;
2754 case TOK_ELIF:
2755 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2756 error("#elif without matching #if");
2757 c = s1->ifdef_stack_ptr[-1];
2758 if (c > 1)
2759 error("#elif after #else");
2760 /* last #if/#elif expression was true: we skip */
2761 if (c == 1)
2762 goto skip;
2763 c = expr_preprocess();
2764 s1->ifdef_stack_ptr[-1] = c;
2765 test_skip:
2766 if (!(c & 1)) {
2767 skip:
2768 preprocess_skip();
2769 is_bof = 0;
2770 goto redo;
2772 break;
2773 case TOK_ENDIF:
2774 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2775 error("#endif without matching #if");
2776 s1->ifdef_stack_ptr--;
2777 /* '#ifndef macro' was at the start of file. Now we check if
2778 an '#endif' is exactly at the end of file */
2779 if (file->ifndef_macro &&
2780 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2781 file->ifndef_macro_saved = file->ifndef_macro;
2782 /* need to set to zero to avoid false matches if another
2783 #ifndef at middle of file */
2784 file->ifndef_macro = 0;
2785 while (tok != TOK_LINEFEED)
2786 next_nomacro();
2787 tok_flags |= TOK_FLAG_ENDIF;
2788 goto the_end;
2790 break;
2791 case TOK_LINE:
2792 next();
2793 if (tok != TOK_CINT)
2794 error("#line");
2795 file->line_num = tokc.i - 1; /* the line number will be incremented after */
2796 next();
2797 if (tok != TOK_LINEFEED) {
2798 if (tok != TOK_STR)
2799 error("#line");
2800 pstrcpy(file->filename, sizeof(file->filename),
2801 (char *)tokc.cstr->data);
2803 break;
2804 case TOK_ERROR:
2805 case TOK_WARNING:
2806 c = tok;
2807 ch = file->buf_ptr[0];
2808 skip_spaces();
2809 q = buf;
2810 while (ch != '\n' && ch != CH_EOF) {
2811 if ((q - buf) < sizeof(buf) - 1)
2812 *q++ = ch;
2813 minp();
2815 *q = '\0';
2816 if (c == TOK_ERROR)
2817 error("#error %s", buf);
2818 else
2819 warning("#warning %s", buf);
2820 break;
2821 case TOK_PRAGMA:
2822 /* ignored */
2823 break;
2824 default:
2825 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
2826 /* '!' is ignored to allow C scripts. numbers are ignored
2827 to emulate cpp behaviour */
2828 } else {
2829 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
2831 break;
2833 /* ignore other preprocess commands or #! for C scripts */
2834 while (tok != TOK_LINEFEED)
2835 next_nomacro();
2836 the_end:
2837 parse_flags = saved_parse_flags;
2840 /* evaluate escape codes in a string. */
2841 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2843 int c, n;
2844 const uint8_t *p;
2846 p = buf;
2847 for(;;) {
2848 c = *p;
2849 if (c == '\0')
2850 break;
2851 if (c == '\\') {
2852 p++;
2853 /* escape */
2854 c = *p;
2855 switch(c) {
2856 case '0': case '1': case '2': case '3':
2857 case '4': case '5': case '6': case '7':
2858 /* at most three octal digits */
2859 n = c - '0';
2860 p++;
2861 c = *p;
2862 if (isoct(c)) {
2863 n = n * 8 + c - '0';
2864 p++;
2865 c = *p;
2866 if (isoct(c)) {
2867 n = n * 8 + c - '0';
2868 p++;
2871 c = n;
2872 goto add_char_nonext;
2873 case 'x':
2874 p++;
2875 n = 0;
2876 for(;;) {
2877 c = *p;
2878 if (c >= 'a' && c <= 'f')
2879 c = c - 'a' + 10;
2880 else if (c >= 'A' && c <= 'F')
2881 c = c - 'A' + 10;
2882 else if (isnum(c))
2883 c = c - '0';
2884 else
2885 break;
2886 n = n * 16 + c;
2887 p++;
2889 c = n;
2890 goto add_char_nonext;
2891 case 'a':
2892 c = '\a';
2893 break;
2894 case 'b':
2895 c = '\b';
2896 break;
2897 case 'f':
2898 c = '\f';
2899 break;
2900 case 'n':
2901 c = '\n';
2902 break;
2903 case 'r':
2904 c = '\r';
2905 break;
2906 case 't':
2907 c = '\t';
2908 break;
2909 case 'v':
2910 c = '\v';
2911 break;
2912 case 'e':
2913 if (!gnu_ext)
2914 goto invalid_escape;
2915 c = 27;
2916 break;
2917 case '\'':
2918 case '\"':
2919 case '\\':
2920 case '?':
2921 break;
2922 default:
2923 invalid_escape:
2924 if (c >= '!' && c <= '~')
2925 warning("unknown escape sequence: \'\\%c\'", c);
2926 else
2927 warning("unknown escape sequence: \'\\x%x\'", c);
2928 break;
2931 p++;
2932 add_char_nonext:
2933 if (!is_long)
2934 cstr_ccat(outstr, c);
2935 else
2936 cstr_wccat(outstr, c);
2938 /* add a trailing '\0' */
2939 if (!is_long)
2940 cstr_ccat(outstr, '\0');
2941 else
2942 cstr_wccat(outstr, '\0');
2945 /* we use 64 bit numbers */
2946 #define BN_SIZE 2
2948 /* bn = (bn << shift) | or_val */
2949 void bn_lshift(unsigned int *bn, int shift, int or_val)
2951 int i;
2952 unsigned int v;
2953 for(i=0;i<BN_SIZE;i++) {
2954 v = bn[i];
2955 bn[i] = (v << shift) | or_val;
2956 or_val = v >> (32 - shift);
2960 void bn_zero(unsigned int *bn)
2962 int i;
2963 for(i=0;i<BN_SIZE;i++) {
2964 bn[i] = 0;
2968 /* parse number in null terminated string 'p' and return it in the
2969 current token */
2970 void parse_number(const char *p)
2972 int b, t, shift, frac_bits, s, exp_val, ch;
2973 char *q;
2974 unsigned int bn[BN_SIZE];
2975 double d;
2977 /* number */
2978 q = token_buf;
2979 ch = *p++;
2980 t = ch;
2981 ch = *p++;
2982 *q++ = t;
2983 b = 10;
2984 if (t == '.') {
2985 goto float_frac_parse;
2986 } else if (t == '0') {
2987 if (ch == 'x' || ch == 'X') {
2988 q--;
2989 ch = *p++;
2990 b = 16;
2991 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2992 q--;
2993 ch = *p++;
2994 b = 2;
2997 /* parse all digits. cannot check octal numbers at this stage
2998 because of floating point constants */
2999 while (1) {
3000 if (ch >= 'a' && ch <= 'f')
3001 t = ch - 'a' + 10;
3002 else if (ch >= 'A' && ch <= 'F')
3003 t = ch - 'A' + 10;
3004 else if (isnum(ch))
3005 t = ch - '0';
3006 else
3007 break;
3008 if (t >= b)
3009 break;
3010 if (q >= token_buf + STRING_MAX_SIZE) {
3011 num_too_long:
3012 error("number too long");
3014 *q++ = ch;
3015 ch = *p++;
3017 if (ch == '.' ||
3018 ((ch == 'e' || ch == 'E') && b == 10) ||
3019 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3020 if (b != 10) {
3021 /* NOTE: strtox should support that for hexa numbers, but
3022 non ISOC99 libcs do not support it, so we prefer to do
3023 it by hand */
3024 /* hexadecimal or binary floats */
3025 /* XXX: handle overflows */
3026 *q = '\0';
3027 if (b == 16)
3028 shift = 4;
3029 else
3030 shift = 2;
3031 bn_zero(bn);
3032 q = token_buf;
3033 while (1) {
3034 t = *q++;
3035 if (t == '\0') {
3036 break;
3037 } else if (t >= 'a') {
3038 t = t - 'a' + 10;
3039 } else if (t >= 'A') {
3040 t = t - 'A' + 10;
3041 } else {
3042 t = t - '0';
3044 bn_lshift(bn, shift, t);
3046 frac_bits = 0;
3047 if (ch == '.') {
3048 ch = *p++;
3049 while (1) {
3050 t = ch;
3051 if (t >= 'a' && t <= 'f') {
3052 t = t - 'a' + 10;
3053 } else if (t >= 'A' && t <= 'F') {
3054 t = t - 'A' + 10;
3055 } else if (t >= '0' && t <= '9') {
3056 t = t - '0';
3057 } else {
3058 break;
3060 if (t >= b)
3061 error("invalid digit");
3062 bn_lshift(bn, shift, t);
3063 frac_bits += shift;
3064 ch = *p++;
3067 if (ch != 'p' && ch != 'P')
3068 expect("exponent");
3069 ch = *p++;
3070 s = 1;
3071 exp_val = 0;
3072 if (ch == '+') {
3073 ch = *p++;
3074 } else if (ch == '-') {
3075 s = -1;
3076 ch = *p++;
3078 if (ch < '0' || ch > '9')
3079 expect("exponent digits");
3080 while (ch >= '0' && ch <= '9') {
3081 exp_val = exp_val * 10 + ch - '0';
3082 ch = *p++;
3084 exp_val = exp_val * s;
3086 /* now we can generate the number */
3087 /* XXX: should patch directly float number */
3088 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3089 d = ldexp(d, exp_val - frac_bits);
3090 t = toup(ch);
3091 if (t == 'F') {
3092 ch = *p++;
3093 tok = TOK_CFLOAT;
3094 /* float : should handle overflow */
3095 tokc.f = (float)d;
3096 } else if (t == 'L') {
3097 ch = *p++;
3098 tok = TOK_CLDOUBLE;
3099 /* XXX: not large enough */
3100 tokc.ld = (long double)d;
3101 } else {
3102 tok = TOK_CDOUBLE;
3103 tokc.d = d;
3105 } else {
3106 /* decimal floats */
3107 if (ch == '.') {
3108 if (q >= token_buf + STRING_MAX_SIZE)
3109 goto num_too_long;
3110 *q++ = ch;
3111 ch = *p++;
3112 float_frac_parse:
3113 while (ch >= '0' && ch <= '9') {
3114 if (q >= token_buf + STRING_MAX_SIZE)
3115 goto num_too_long;
3116 *q++ = ch;
3117 ch = *p++;
3120 if (ch == 'e' || ch == 'E') {
3121 if (q >= token_buf + STRING_MAX_SIZE)
3122 goto num_too_long;
3123 *q++ = ch;
3124 ch = *p++;
3125 if (ch == '-' || ch == '+') {
3126 if (q >= token_buf + STRING_MAX_SIZE)
3127 goto num_too_long;
3128 *q++ = ch;
3129 ch = *p++;
3131 if (ch < '0' || ch > '9')
3132 expect("exponent digits");
3133 while (ch >= '0' && ch <= '9') {
3134 if (q >= token_buf + STRING_MAX_SIZE)
3135 goto num_too_long;
3136 *q++ = ch;
3137 ch = *p++;
3140 *q = '\0';
3141 t = toup(ch);
3142 errno = 0;
3143 if (t == 'F') {
3144 ch = *p++;
3145 tok = TOK_CFLOAT;
3146 tokc.f = strtof(token_buf, NULL);
3147 } else if (t == 'L') {
3148 ch = *p++;
3149 tok = TOK_CLDOUBLE;
3150 tokc.ld = strtold(token_buf, NULL);
3151 } else {
3152 tok = TOK_CDOUBLE;
3153 tokc.d = strtod(token_buf, NULL);
3156 } else {
3157 unsigned long long n, n1;
3158 int lcount, ucount;
3160 /* integer number */
3161 *q = '\0';
3162 q = token_buf;
3163 if (b == 10 && *q == '0') {
3164 b = 8;
3165 q++;
3167 n = 0;
3168 while(1) {
3169 t = *q++;
3170 /* no need for checks except for base 10 / 8 errors */
3171 if (t == '\0') {
3172 break;
3173 } else if (t >= 'a') {
3174 t = t - 'a' + 10;
3175 } else if (t >= 'A') {
3176 t = t - 'A' + 10;
3177 } else {
3178 t = t - '0';
3179 if (t >= b)
3180 error("invalid digit");
3182 n1 = n;
3183 n = n * b + t;
3184 /* detect overflow */
3185 /* XXX: this test is not reliable */
3186 if (n < n1)
3187 error("integer constant overflow");
3190 /* XXX: not exactly ANSI compliant */
3191 if ((n & 0xffffffff00000000LL) != 0) {
3192 if ((n >> 63) != 0)
3193 tok = TOK_CULLONG;
3194 else
3195 tok = TOK_CLLONG;
3196 } else if (n > 0x7fffffff) {
3197 tok = TOK_CUINT;
3198 } else {
3199 tok = TOK_CINT;
3201 lcount = 0;
3202 ucount = 0;
3203 for(;;) {
3204 t = toup(ch);
3205 if (t == 'L') {
3206 if (lcount >= 2)
3207 error("three 'l's in integer constant");
3208 lcount++;
3209 if (lcount == 2) {
3210 if (tok == TOK_CINT)
3211 tok = TOK_CLLONG;
3212 else if (tok == TOK_CUINT)
3213 tok = TOK_CULLONG;
3215 ch = *p++;
3216 } else if (t == 'U') {
3217 if (ucount >= 1)
3218 error("two 'u's in integer constant");
3219 ucount++;
3220 if (tok == TOK_CINT)
3221 tok = TOK_CUINT;
3222 else if (tok == TOK_CLLONG)
3223 tok = TOK_CULLONG;
3224 ch = *p++;
3225 } else {
3226 break;
3229 if (tok == TOK_CINT || tok == TOK_CUINT)
3230 tokc.ui = n;
3231 else
3232 tokc.ull = n;
3237 #define PARSE2(c1, tok1, c2, tok2) \
3238 case c1: \
3239 PEEKC(c, p); \
3240 if (c == c2) { \
3241 p++; \
3242 tok = tok2; \
3243 } else { \
3244 tok = tok1; \
3246 break;
3248 /* return next token without macro substitution */
3249 static inline void next_nomacro1(void)
3251 int t, c, is_long;
3252 TokenSym *ts;
3253 uint8_t *p, *p1;
3254 unsigned int h;
3256 p = file->buf_ptr;
3257 redo_no_start:
3258 c = *p;
3259 switch(c) {
3260 case ' ':
3261 case '\t':
3262 case '\f':
3263 case '\v':
3264 case '\r':
3265 p++;
3266 goto redo_no_start;
3268 case '\\':
3269 /* first look if it is in fact an end of buffer */
3270 if (p >= file->buf_end) {
3271 file->buf_ptr = p;
3272 handle_eob();
3273 p = file->buf_ptr;
3274 if (p >= file->buf_end)
3275 goto parse_eof;
3276 else
3277 goto redo_no_start;
3278 } else {
3279 file->buf_ptr = p;
3280 ch = *p;
3281 handle_stray();
3282 p = file->buf_ptr;
3283 goto redo_no_start;
3285 parse_eof:
3287 TCCState *s1 = tcc_state;
3288 if (parse_flags & PARSE_FLAG_LINEFEED) {
3289 tok = TOK_LINEFEED;
3290 } else if (s1->include_stack_ptr == s1->include_stack ||
3291 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3292 /* no include left : end of file. */
3293 tok = TOK_EOF;
3294 } else {
3295 /* pop include file */
3297 /* test if previous '#endif' was after a #ifdef at
3298 start of file */
3299 if (tok_flags & TOK_FLAG_ENDIF) {
3300 #ifdef INC_DEBUG
3301 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3302 #endif
3303 add_cached_include(s1, file->inc_type, file->inc_filename,
3304 file->ifndef_macro_saved);
3307 /* add end of include file debug info */
3308 if (do_debug) {
3309 put_stabd(N_EINCL, 0, 0);
3311 /* pop include stack */
3312 tcc_close(file);
3313 s1->include_stack_ptr--;
3314 file = *s1->include_stack_ptr;
3315 p = file->buf_ptr;
3316 goto redo_no_start;
3319 break;
3321 case '\n':
3322 if (parse_flags & PARSE_FLAG_LINEFEED) {
3323 tok = TOK_LINEFEED;
3324 } else {
3325 file->line_num++;
3326 tok_flags |= TOK_FLAG_BOL;
3327 p++;
3328 goto redo_no_start;
3330 break;
3332 case '#':
3333 /* XXX: simplify */
3334 PEEKC(c, p);
3335 if ((tok_flags & TOK_FLAG_BOL) &&
3336 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3337 file->buf_ptr = p;
3338 preprocess(tok_flags & TOK_FLAG_BOF);
3339 p = file->buf_ptr;
3340 goto redo_no_start;
3341 } else {
3342 if (c == '#') {
3343 p++;
3344 tok = TOK_TWOSHARPS;
3345 } else {
3346 tok = '#';
3349 break;
3351 case 'a': case 'b': case 'c': case 'd':
3352 case 'e': case 'f': case 'g': case 'h':
3353 case 'i': case 'j': case 'k': case 'l':
3354 case 'm': case 'n': case 'o': case 'p':
3355 case 'q': case 'r': case 's': case 't':
3356 case 'u': case 'v': case 'w': case 'x':
3357 case 'y': case 'z':
3358 case 'A': case 'B': case 'C': case 'D':
3359 case 'E': case 'F': case 'G': case 'H':
3360 case 'I': case 'J': case 'K':
3361 case 'M': case 'N': case 'O': case 'P':
3362 case 'Q': case 'R': case 'S': case 'T':
3363 case 'U': case 'V': case 'W': case 'X':
3364 case 'Y': case 'Z':
3365 case '_':
3366 parse_ident_fast:
3367 p1 = p;
3368 h = TOK_HASH_INIT;
3369 h = TOK_HASH_FUNC(h, c);
3370 p++;
3371 for(;;) {
3372 c = *p;
3373 if (!isidnum_table[c])
3374 break;
3375 h = TOK_HASH_FUNC(h, c);
3376 p++;
3378 if (c != '\\') {
3379 TokenSym **pts;
3380 int len;
3382 /* fast case : no stray found, so we have the full token
3383 and we have already hashed it */
3384 len = p - p1;
3385 h &= (TOK_HASH_SIZE - 1);
3386 pts = &hash_ident[h];
3387 for(;;) {
3388 ts = *pts;
3389 if (!ts)
3390 break;
3391 if (ts->len == len && !memcmp(ts->str, p1, len))
3392 goto token_found;
3393 pts = &(ts->hash_next);
3395 ts = tok_alloc_new(pts, p1, len);
3396 token_found: ;
3397 } else {
3398 /* slower case */
3399 cstr_reset(&tokcstr);
3401 while (p1 < p) {
3402 cstr_ccat(&tokcstr, *p1);
3403 p1++;
3405 p--;
3406 PEEKC(c, p);
3407 parse_ident_slow:
3408 while (isidnum_table[c]) {
3409 cstr_ccat(&tokcstr, c);
3410 PEEKC(c, p);
3412 ts = tok_alloc(tokcstr.data, tokcstr.size);
3414 tok = ts->tok;
3415 break;
3416 case 'L':
3417 t = p[1];
3418 if (t != '\\' && t != '\'' && t != '\"') {
3419 /* fast case */
3420 goto parse_ident_fast;
3421 } else {
3422 PEEKC(c, p);
3423 if (c == '\'' || c == '\"') {
3424 is_long = 1;
3425 goto str_const;
3426 } else {
3427 cstr_reset(&tokcstr);
3428 cstr_ccat(&tokcstr, 'L');
3429 goto parse_ident_slow;
3432 break;
3433 case '0': case '1': case '2': case '3':
3434 case '4': case '5': case '6': case '7':
3435 case '8': case '9':
3437 cstr_reset(&tokcstr);
3438 /* after the first digit, accept digits, alpha, '.' or sign if
3439 prefixed by 'eEpP' */
3440 parse_num:
3441 for(;;) {
3442 t = c;
3443 cstr_ccat(&tokcstr, c);
3444 PEEKC(c, p);
3445 if (!(isnum(c) || isid(c) || c == '.' ||
3446 ((c == '+' || c == '-') &&
3447 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3448 break;
3450 /* We add a trailing '\0' to ease parsing */
3451 cstr_ccat(&tokcstr, '\0');
3452 tokc.cstr = &tokcstr;
3453 tok = TOK_PPNUM;
3454 break;
3455 case '.':
3456 /* special dot handling because it can also start a number */
3457 PEEKC(c, p);
3458 if (isnum(c)) {
3459 cstr_reset(&tokcstr);
3460 cstr_ccat(&tokcstr, '.');
3461 goto parse_num;
3462 } else if (c == '.') {
3463 PEEKC(c, p);
3464 if (c != '.')
3465 expect("'.'");
3466 PEEKC(c, p);
3467 tok = TOK_DOTS;
3468 } else {
3469 tok = '.';
3471 break;
3472 case '\'':
3473 case '\"':
3474 is_long = 0;
3475 str_const:
3477 CString str;
3478 int sep;
3480 sep = c;
3482 /* parse the string */
3483 cstr_new(&str);
3484 p = parse_pp_string(p, sep, &str);
3485 cstr_ccat(&str, '\0');
3487 /* eval the escape (should be done as TOK_PPNUM) */
3488 cstr_reset(&tokcstr);
3489 parse_escape_string(&tokcstr, str.data, is_long);
3490 cstr_free(&str);
3492 if (sep == '\'') {
3493 int char_size;
3494 /* XXX: make it portable */
3495 if (!is_long)
3496 char_size = 1;
3497 else
3498 char_size = sizeof(int);
3499 if (tokcstr.size <= char_size)
3500 error("empty character constant");
3501 if (tokcstr.size > 2 * char_size)
3502 warning("multi-character character constant");
3503 if (!is_long) {
3504 tokc.i = *(int8_t *)tokcstr.data;
3505 tok = TOK_CCHAR;
3506 } else {
3507 tokc.i = *(int *)tokcstr.data;
3508 tok = TOK_LCHAR;
3510 } else {
3511 tokc.cstr = &tokcstr;
3512 if (!is_long)
3513 tok = TOK_STR;
3514 else
3515 tok = TOK_LSTR;
3518 break;
3520 case '<':
3521 PEEKC(c, p);
3522 if (c == '=') {
3523 p++;
3524 tok = TOK_LE;
3525 } else if (c == '<') {
3526 PEEKC(c, p);
3527 if (c == '=') {
3528 p++;
3529 tok = TOK_A_SHL;
3530 } else {
3531 tok = TOK_SHL;
3533 } else {
3534 tok = TOK_LT;
3536 break;
3538 case '>':
3539 PEEKC(c, p);
3540 if (c == '=') {
3541 p++;
3542 tok = TOK_GE;
3543 } else if (c == '>') {
3544 PEEKC(c, p);
3545 if (c == '=') {
3546 p++;
3547 tok = TOK_A_SAR;
3548 } else {
3549 tok = TOK_SAR;
3551 } else {
3552 tok = TOK_GT;
3554 break;
3556 case '&':
3557 PEEKC(c, p);
3558 if (c == '&') {
3559 p++;
3560 tok = TOK_LAND;
3561 } else if (c == '=') {
3562 p++;
3563 tok = TOK_A_AND;
3564 } else {
3565 tok = '&';
3567 break;
3569 case '|':
3570 PEEKC(c, p);
3571 if (c == '|') {
3572 p++;
3573 tok = TOK_LOR;
3574 } else if (c == '=') {
3575 p++;
3576 tok = TOK_A_OR;
3577 } else {
3578 tok = '|';
3580 break;
3582 case '+':
3583 PEEKC(c, p);
3584 if (c == '+') {
3585 p++;
3586 tok = TOK_INC;
3587 } else if (c == '=') {
3588 p++;
3589 tok = TOK_A_ADD;
3590 } else {
3591 tok = '+';
3593 break;
3595 case '-':
3596 PEEKC(c, p);
3597 if (c == '-') {
3598 p++;
3599 tok = TOK_DEC;
3600 } else if (c == '=') {
3601 p++;
3602 tok = TOK_A_SUB;
3603 } else if (c == '>') {
3604 p++;
3605 tok = TOK_ARROW;
3606 } else {
3607 tok = '-';
3609 break;
3611 PARSE2('!', '!', '=', TOK_NE)
3612 PARSE2('=', '=', '=', TOK_EQ)
3613 PARSE2('*', '*', '=', TOK_A_MUL)
3614 PARSE2('%', '%', '=', TOK_A_MOD)
3615 PARSE2('^', '^', '=', TOK_A_XOR)
3617 /* comments or operator */
3618 case '/':
3619 PEEKC(c, p);
3620 if (c == '*') {
3621 p = parse_comment(p);
3622 goto redo_no_start;
3623 } else if (c == '/') {
3624 p = parse_line_comment(p);
3625 goto redo_no_start;
3626 } else if (c == '=') {
3627 p++;
3628 tok = TOK_A_DIV;
3629 } else {
3630 tok = '/';
3632 break;
3634 /* simple tokens */
3635 case '(':
3636 case ')':
3637 case '[':
3638 case ']':
3639 case '{':
3640 case '}':
3641 case ',':
3642 case ';':
3643 case ':':
3644 case '?':
3645 case '~':
3646 case '$': /* only used in assembler */
3647 tok = c;
3648 p++;
3649 break;
3650 default:
3651 error("unrecognized character \\x%02x", c);
3652 break;
3654 file->buf_ptr = p;
3655 tok_flags = 0;
3656 #if defined(PARSE_DEBUG)
3657 printf("token = %s\n", get_tok_str(tok, &tokc));
3658 #endif
3661 /* return next token without macro substitution. Can read input from
3662 macro_ptr buffer */
3663 static void next_nomacro(void)
3665 if (macro_ptr) {
3666 redo:
3667 tok = *macro_ptr;
3668 if (tok) {
3669 TOK_GET(tok, macro_ptr, tokc);
3670 if (tok == TOK_LINENUM) {
3671 file->line_num = tokc.i;
3672 goto redo;
3675 } else {
3676 next_nomacro1();
3680 /* substitute args in macro_str and return allocated string */
3681 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3683 int *st, last_tok, t, notfirst;
3684 Sym *s;
3685 CValue cval;
3686 TokenString str;
3687 CString cstr;
3689 tok_str_new(&str);
3690 last_tok = 0;
3691 while(1) {
3692 TOK_GET(t, macro_str, cval);
3693 if (!t)
3694 break;
3695 if (t == '#') {
3696 /* stringize */
3697 TOK_GET(t, macro_str, cval);
3698 if (!t)
3699 break;
3700 s = sym_find2(args, t);
3701 if (s) {
3702 cstr_new(&cstr);
3703 st = (int *)s->c;
3704 notfirst = 0;
3705 while (*st) {
3706 if (notfirst)
3707 cstr_ccat(&cstr, ' ');
3708 TOK_GET(t, st, cval);
3709 cstr_cat(&cstr, get_tok_str(t, &cval));
3710 notfirst = 1;
3712 cstr_ccat(&cstr, '\0');
3713 #ifdef PP_DEBUG
3714 printf("stringize: %s\n", (char *)cstr.data);
3715 #endif
3716 /* add string */
3717 cval.cstr = &cstr;
3718 tok_str_add2(&str, TOK_STR, &cval);
3719 cstr_free(&cstr);
3720 } else {
3721 tok_str_add2(&str, t, &cval);
3723 } else if (t >= TOK_IDENT) {
3724 s = sym_find2(args, t);
3725 if (s) {
3726 st = (int *)s->c;
3727 /* if '##' is present before or after, no arg substitution */
3728 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3729 /* special case for var arg macros : ## eats the
3730 ',' if empty VA_ARGS variable. */
3731 /* XXX: test of the ',' is not 100%
3732 reliable. should fix it to avoid security
3733 problems */
3734 if (gnu_ext && s->type.t &&
3735 last_tok == TOK_TWOSHARPS &&
3736 str.len >= 2 && str.str[str.len - 2] == ',') {
3737 if (*st == 0) {
3738 /* suppress ',' '##' */
3739 str.len -= 2;
3740 } else {
3741 /* suppress '##' and add variable */
3742 str.len--;
3743 goto add_var;
3745 } else {
3746 int t1;
3747 add_var:
3748 for(;;) {
3749 TOK_GET(t1, st, cval);
3750 if (!t1)
3751 break;
3752 tok_str_add2(&str, t1, &cval);
3755 } else {
3756 /* NOTE: the stream cannot be read when macro
3757 substituing an argument */
3758 macro_subst(&str, nested_list, st, 0);
3760 } else {
3761 tok_str_add(&str, t);
3763 } else {
3764 tok_str_add2(&str, t, &cval);
3766 last_tok = t;
3768 tok_str_add(&str, 0);
3769 return str.str;
3772 static char const ab_month_name[12][4] =
3774 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3775 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3778 /* do macro substitution of current token with macro 's' and add
3779 result to (tok_str,tok_len). 'nested_list' is the list of all
3780 macros we got inside to avoid recursing. Return non zero if no
3781 substitution needs to be done */
3782 static int macro_subst_tok(TokenString *tok_str,
3783 Sym **nested_list, Sym *s, int can_read_stream)
3785 Sym *args, *sa, *sa1;
3786 int mstr_allocated, parlevel, *mstr, t;
3787 TokenString str;
3788 char *cstrval;
3789 CValue cval;
3790 CString cstr;
3792 /* if symbol is a macro, prepare substitution */
3794 /* special macros */
3795 if (tok == TOK___LINE__) {
3796 cval.i = file->line_num;
3797 tok_str_add2(tok_str, TOK_CINT, &cval);
3798 } else if (tok == TOK___FILE__) {
3799 cstrval = file->filename;
3800 goto add_cstr;
3801 tok_str_add2(tok_str, TOK_STR, &cval);
3802 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3803 time_t ti;
3804 struct tm *tm;
3805 char buf[64];
3807 time(&ti);
3808 tm = localtime(&ti);
3809 if (tok == TOK___DATE__) {
3810 snprintf(buf, sizeof(buf), "%s %2d %d",
3811 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3812 } else {
3813 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3814 tm->tm_hour, tm->tm_min, tm->tm_sec);
3816 cstrval = buf;
3817 add_cstr:
3818 cstr_new(&cstr);
3819 cstr_cat(&cstr, cstrval);
3820 cstr_ccat(&cstr, '\0');
3821 cval.cstr = &cstr;
3822 tok_str_add2(tok_str, TOK_STR, &cval);
3823 cstr_free(&cstr);
3824 } else {
3825 mstr = (int *)s->c;
3826 mstr_allocated = 0;
3827 if (s->type.t == MACRO_FUNC) {
3828 /* NOTE: we do not use next_nomacro to avoid eating the
3829 next token. XXX: find better solution */
3830 if (macro_ptr) {
3831 t = *macro_ptr;
3832 if (t == 0 && can_read_stream) {
3833 /* end of macro stream: we must look at the token
3834 after in the file */
3835 macro_ptr = NULL;
3836 goto parse_stream;
3838 } else {
3839 parse_stream:
3840 /* XXX: incorrect with comments */
3841 ch = file->buf_ptr[0];
3842 while (is_space(ch) || ch == '\n')
3843 cinp();
3844 t = ch;
3846 if (t != '(') /* no macro subst */
3847 return -1;
3849 /* argument macro */
3850 next_nomacro();
3851 next_nomacro();
3852 args = NULL;
3853 sa = s->next;
3854 /* NOTE: empty args are allowed, except if no args */
3855 for(;;) {
3856 /* handle '()' case */
3857 if (!args && !sa && tok == ')')
3858 break;
3859 if (!sa)
3860 error("macro '%s' used with too many args",
3861 get_tok_str(s->v, 0));
3862 tok_str_new(&str);
3863 parlevel = 0;
3864 /* NOTE: non zero sa->t indicates VA_ARGS */
3865 while ((parlevel > 0 ||
3866 (tok != ')' &&
3867 (tok != ',' || sa->type.t))) &&
3868 tok != -1) {
3869 if (tok == '(')
3870 parlevel++;
3871 else if (tok == ')')
3872 parlevel--;
3873 tok_str_add2(&str, tok, &tokc);
3874 next_nomacro();
3876 tok_str_add(&str, 0);
3877 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
3878 sa = sa->next;
3879 if (tok == ')') {
3880 /* special case for gcc var args: add an empty
3881 var arg argument if it is omitted */
3882 if (sa && sa->type.t && gnu_ext)
3883 continue;
3884 else
3885 break;
3887 if (tok != ',')
3888 expect(",");
3889 next_nomacro();
3891 if (sa) {
3892 error("macro '%s' used with too few args",
3893 get_tok_str(s->v, 0));
3896 /* now subst each arg */
3897 mstr = macro_arg_subst(nested_list, mstr, args);
3898 /* free memory */
3899 sa = args;
3900 while (sa) {
3901 sa1 = sa->prev;
3902 tok_str_free((int *)sa->c);
3903 tcc_free(sa);
3904 sa = sa1;
3906 mstr_allocated = 1;
3908 sym_push2(nested_list, s->v, 0, 0);
3909 macro_subst(tok_str, nested_list, mstr, 1);
3910 /* pop nested defined symbol */
3911 sa1 = *nested_list;
3912 *nested_list = sa1->prev;
3913 tcc_free(sa1);
3914 if (mstr_allocated)
3915 tok_str_free(mstr);
3917 return 0;
3920 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3921 return the resulting string (which must be freed). */
3922 static inline int *macro_twosharps(const int *macro_str)
3924 TokenSym *ts;
3925 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
3926 int t;
3927 const char *p1, *p2;
3928 CValue cval;
3929 TokenString macro_str1;
3930 CString cstr;
3932 start_macro_ptr = macro_str;
3933 /* we search the first '##' */
3934 for(;;) {
3935 macro_ptr1 = macro_str;
3936 TOK_GET(t, macro_str, cval);
3937 /* nothing more to do if end of string */
3938 if (t == 0)
3939 return NULL;
3940 if (*macro_str == TOK_TWOSHARPS)
3941 break;
3944 /* we saw '##', so we need more processing to handle it */
3945 cstr_new(&cstr);
3946 tok_str_new(&macro_str1);
3947 tok = t;
3948 tokc = cval;
3950 /* add all tokens seen so far */
3951 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
3952 TOK_GET(t, ptr, cval);
3953 tok_str_add2(&macro_str1, t, &cval);
3955 saved_macro_ptr = macro_ptr;
3956 /* XXX: get rid of the use of macro_ptr here */
3957 macro_ptr = (int *)macro_str;
3958 for(;;) {
3959 while (*macro_ptr == TOK_TWOSHARPS) {
3960 macro_ptr++;
3961 macro_ptr1 = macro_ptr;
3962 t = *macro_ptr;
3963 if (t) {
3964 TOK_GET(t, macro_ptr, cval);
3965 /* We concatenate the two tokens if we have an
3966 identifier or a preprocessing number */
3967 cstr_reset(&cstr);
3968 p1 = get_tok_str(tok, &tokc);
3969 cstr_cat(&cstr, p1);
3970 p2 = get_tok_str(t, &cval);
3971 cstr_cat(&cstr, p2);
3972 cstr_ccat(&cstr, '\0');
3974 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
3975 (t >= TOK_IDENT || t == TOK_PPNUM)) {
3976 if (tok == TOK_PPNUM) {
3977 /* if number, then create a number token */
3978 /* NOTE: no need to allocate because
3979 tok_str_add2() does it */
3980 tokc.cstr = &cstr;
3981 } else {
3982 /* if identifier, we must do a test to
3983 validate we have a correct identifier */
3984 if (t == TOK_PPNUM) {
3985 const char *p;
3986 int c;
3988 p = p2;
3989 for(;;) {
3990 c = *p;
3991 if (c == '\0')
3992 break;
3993 p++;
3994 if (!isnum(c) && !isid(c))
3995 goto error_pasting;
3998 ts = tok_alloc(cstr.data, strlen(cstr.data));
3999 tok = ts->tok; /* modify current token */
4001 } else {
4002 const char *str = cstr.data;
4003 const unsigned char *q;
4005 /* we look for a valid token */
4006 /* XXX: do more extensive checks */
4007 if (!strcmp(str, ">>=")) {
4008 tok = TOK_A_SAR;
4009 } else if (!strcmp(str, "<<=")) {
4010 tok = TOK_A_SHL;
4011 } else if (strlen(str) == 2) {
4012 /* search in two bytes table */
4013 q = tok_two_chars;
4014 for(;;) {
4015 if (!*q)
4016 goto error_pasting;
4017 if (q[0] == str[0] && q[1] == str[1])
4018 break;
4019 q += 3;
4021 tok = q[2];
4022 } else {
4023 error_pasting:
4024 /* NOTE: because get_tok_str use a static buffer,
4025 we must save it */
4026 cstr_reset(&cstr);
4027 p1 = get_tok_str(tok, &tokc);
4028 cstr_cat(&cstr, p1);
4029 cstr_ccat(&cstr, '\0');
4030 p2 = get_tok_str(t, &cval);
4031 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4032 /* cannot merge tokens: just add them separately */
4033 tok_str_add2(&macro_str1, tok, &tokc);
4034 /* XXX: free associated memory ? */
4035 tok = t;
4036 tokc = cval;
4041 tok_str_add2(&macro_str1, tok, &tokc);
4042 next_nomacro();
4043 if (tok == 0)
4044 break;
4046 macro_ptr = (int *)saved_macro_ptr;
4047 cstr_free(&cstr);
4048 tok_str_add(&macro_str1, 0);
4049 return macro_str1.str;
4053 /* do macro substitution of macro_str and add result to
4054 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4055 inside to avoid recursing. */
4056 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4057 const int *macro_str, int can_read_stream)
4059 Sym *s;
4060 int *saved_macro_ptr, *macro_str1;
4061 const int *ptr;
4062 int t, ret;
4063 CValue cval;
4065 /* first scan for '##' operator handling */
4066 ptr = macro_str;
4067 macro_str1 = macro_twosharps(ptr);
4068 if (macro_str1)
4069 ptr = macro_str1;
4070 while (1) {
4071 /* NOTE: ptr == NULL can only happen if tokens are read from
4072 file stream due to a macro function call */
4073 if (ptr == NULL)
4074 break;
4075 TOK_GET(t, ptr, cval);
4076 if (t == 0)
4077 break;
4078 s = define_find(t);
4079 if (s != NULL) {
4080 /* if nested substitution, do nothing */
4081 if (sym_find2(*nested_list, t))
4082 goto no_subst;
4083 saved_macro_ptr = macro_ptr;
4084 macro_ptr = (int *)ptr;
4085 tok = t;
4086 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4087 ptr = (int *)macro_ptr;
4088 macro_ptr = saved_macro_ptr;
4089 if (ret != 0)
4090 goto no_subst;
4091 } else {
4092 no_subst:
4093 tok_str_add2(tok_str, t, &cval);
4096 if (macro_str1)
4097 tok_str_free(macro_str1);
4100 /* return next token with macro substitution */
4101 static void next(void)
4103 Sym *nested_list, *s;
4104 TokenString str;
4106 redo:
4107 next_nomacro();
4108 if (!macro_ptr) {
4109 /* if not reading from macro substituted string, then try
4110 to substitute macros */
4111 if (tok >= TOK_IDENT &&
4112 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4113 s = define_find(tok);
4114 if (s) {
4115 /* we have a macro: we try to substitute */
4116 tok_str_new(&str);
4117 nested_list = NULL;
4118 if (macro_subst_tok(&str, &nested_list, s, 1) == 0) {
4119 /* substitution done, NOTE: maybe empty */
4120 tok_str_add(&str, 0);
4121 macro_ptr = str.str;
4122 macro_ptr_allocated = str.str;
4123 goto redo;
4127 } else {
4128 if (tok == 0) {
4129 /* end of macro or end of unget buffer */
4130 if (unget_buffer_enabled) {
4131 macro_ptr = unget_saved_macro_ptr;
4132 unget_buffer_enabled = 0;
4133 } else {
4134 /* end of macro string: free it */
4135 tok_str_free(macro_ptr_allocated);
4136 macro_ptr = NULL;
4138 goto redo;
4142 /* convert preprocessor tokens into C tokens */
4143 if (tok == TOK_PPNUM &&
4144 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4145 parse_number((char *)tokc.cstr->data);
4149 /* push back current token and set current token to 'last_tok'. Only
4150 identifier case handled for labels. */
4151 static inline void unget_tok(int last_tok)
4153 int i, n;
4154 int *q;
4155 unget_saved_macro_ptr = macro_ptr;
4156 unget_buffer_enabled = 1;
4157 q = unget_saved_buffer;
4158 macro_ptr = q;
4159 *q++ = tok;
4160 n = tok_ext_size(tok) - 1;
4161 for(i=0;i<n;i++)
4162 *q++ = tokc.tab[i];
4163 *q = 0; /* end of token string */
4164 tok = last_tok;
4168 void swap(int *p, int *q)
4170 int t;
4171 t = *p;
4172 *p = *q;
4173 *q = t;
4176 void vsetc(CType *type, int r, CValue *vc)
4178 int v;
4180 if (vtop >= vstack + VSTACK_SIZE)
4181 error("memory full");
4182 /* cannot let cpu flags if other instruction are generated. Also
4183 avoid leaving VT_JMP anywhere except on the top of the stack
4184 because it would complicate the code generator. */
4185 if (vtop >= vstack) {
4186 v = vtop->r & VT_VALMASK;
4187 if (v == VT_CMP || (v & ~1) == VT_JMP)
4188 gv(RC_INT);
4190 vtop++;
4191 vtop->type = *type;
4192 vtop->r = r;
4193 vtop->r2 = VT_CONST;
4194 vtop->c = *vc;
4197 /* push integer constant */
4198 void vpushi(int v)
4200 CValue cval;
4201 cval.i = v;
4202 vsetc(&int_type, VT_CONST, &cval);
4205 /* Return a static symbol pointing to a section */
4206 static Sym *get_sym_ref(CType *type, Section *sec,
4207 unsigned long offset, unsigned long size)
4209 int v;
4210 Sym *sym;
4212 v = anon_sym++;
4213 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4214 sym->type.ref = type->ref;
4215 sym->r = VT_CONST | VT_SYM;
4216 put_extern_sym(sym, sec, offset, size);
4217 return sym;
4220 /* push a reference to a section offset by adding a dummy symbol */
4221 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4223 CValue cval;
4225 cval.ul = 0;
4226 vsetc(type, VT_CONST | VT_SYM, &cval);
4227 vtop->sym = get_sym_ref(type, sec, offset, size);
4230 /* define a new external reference to a symbol 'v' of type 'u' */
4231 static Sym *external_global_sym(int v, CType *type, int r)
4233 Sym *s;
4235 s = sym_find(v);
4236 if (!s) {
4237 /* push forward reference */
4238 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4239 s->type.ref = type->ref;
4240 s->r = r | VT_CONST | VT_SYM;
4242 return s;
4245 /* define a new external reference to a symbol 'v' of type 'u' */
4246 static Sym *external_sym(int v, CType *type, int r)
4248 Sym *s;
4250 s = sym_find(v);
4251 if (!s) {
4252 /* push forward reference */
4253 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4254 s->type.t |= VT_EXTERN;
4255 } else {
4256 if (!is_compatible_types(&s->type, type))
4257 error("incompatible types for redefinition of '%s'",
4258 get_tok_str(v, NULL));
4260 return s;
4263 /* push a reference to global symbol v */
4264 static void vpush_global_sym(CType *type, int v)
4266 Sym *sym;
4267 CValue cval;
4269 sym = external_global_sym(v, type, 0);
4270 cval.ul = 0;
4271 vsetc(type, VT_CONST | VT_SYM, &cval);
4272 vtop->sym = sym;
4275 void vset(CType *type, int r, int v)
4277 CValue cval;
4279 cval.i = v;
4280 vsetc(type, r, &cval);
4283 void vseti(int r, int v)
4285 CType type;
4286 type.t = VT_INT;
4287 vset(&type, r, v);
4290 void vswap(void)
4292 SValue tmp;
4294 tmp = vtop[0];
4295 vtop[0] = vtop[-1];
4296 vtop[-1] = tmp;
4299 void vpushv(SValue *v)
4301 if (vtop >= vstack + VSTACK_SIZE)
4302 error("memory full");
4303 vtop++;
4304 *vtop = *v;
4307 void vdup(void)
4309 vpushv(vtop);
4312 /* save r to the memory stack, and mark it as being free */
4313 void save_reg(int r)
4315 int l, saved, size, align;
4316 SValue *p, sv;
4317 CType *type;
4319 /* modify all stack values */
4320 saved = 0;
4321 l = 0;
4322 for(p=vstack;p<=vtop;p++) {
4323 if ((p->r & VT_VALMASK) == r ||
4324 (p->r2 & VT_VALMASK) == r) {
4325 /* must save value on stack if not already done */
4326 if (!saved) {
4327 /* NOTE: must reload 'r' because r might be equal to r2 */
4328 r = p->r & VT_VALMASK;
4329 /* store register in the stack */
4330 type = &p->type;
4331 if ((p->r & VT_LVAL) ||
4332 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4333 type = &int_type;
4334 size = type_size(type, &align);
4335 loc = (loc - size) & -align;
4336 sv.type.t = type->t;
4337 sv.r = VT_LOCAL | VT_LVAL;
4338 sv.c.ul = loc;
4339 store(r, &sv);
4340 #ifdef TCC_TARGET_I386
4341 /* x86 specific: need to pop fp register ST0 if saved */
4342 if (r == TREG_ST0) {
4343 o(0xd9dd); /* fstp %st(1) */
4345 #endif
4346 /* special long long case */
4347 if ((type->t & VT_BTYPE) == VT_LLONG) {
4348 sv.c.ul += 4;
4349 store(p->r2, &sv);
4351 l = loc;
4352 saved = 1;
4354 /* mark that stack entry as being saved on the stack */
4355 if (p->r & VT_LVAL) {
4356 /* also clear the bounded flag because the
4357 relocation address of the function was stored in
4358 p->c.ul */
4359 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4360 } else {
4361 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4363 p->r2 = VT_CONST;
4364 p->c.ul = l;
4369 /* find a register of class 'rc2' with at most one reference on stack.
4370 * If none, call get_reg(rc) */
4371 int get_reg_ex(int rc, int rc2)
4373 int r;
4374 SValue *p;
4376 for(r=0;r<NB_REGS;r++) {
4377 if (reg_classes[r] & rc2) {
4378 int n;
4379 n=0;
4380 for(p = vstack; p <= vtop; p++) {
4381 if ((p->r & VT_VALMASK) == r ||
4382 (p->r2 & VT_VALMASK) == r)
4383 n++;
4385 if (n <= 1)
4386 return r;
4389 return get_reg(rc);
4392 /* find a free register of class 'rc'. If none, save one register */
4393 int get_reg(int rc)
4395 int r;
4396 SValue *p;
4398 /* find a free register */
4399 for(r=0;r<NB_REGS;r++) {
4400 if (reg_classes[r] & rc) {
4401 for(p=vstack;p<=vtop;p++) {
4402 if ((p->r & VT_VALMASK) == r ||
4403 (p->r2 & VT_VALMASK) == r)
4404 goto notfound;
4406 return r;
4408 notfound: ;
4411 /* no register left : free the first one on the stack (VERY
4412 IMPORTANT to start from the bottom to ensure that we don't
4413 spill registers used in gen_opi()) */
4414 for(p=vstack;p<=vtop;p++) {
4415 r = p->r & VT_VALMASK;
4416 if (r < VT_CONST && (reg_classes[r] & rc))
4417 goto save_found;
4418 /* also look at second register (if long long) */
4419 r = p->r2 & VT_VALMASK;
4420 if (r < VT_CONST && (reg_classes[r] & rc)) {
4421 save_found:
4422 save_reg(r);
4423 return r;
4426 /* Should never comes here */
4427 return -1;
4430 /* save registers up to (vtop - n) stack entry */
4431 void save_regs(int n)
4433 int r;
4434 SValue *p, *p1;
4435 p1 = vtop - n;
4436 for(p = vstack;p <= p1; p++) {
4437 r = p->r & VT_VALMASK;
4438 if (r < VT_CONST) {
4439 save_reg(r);
4444 /* move register 's' to 'r', and flush previous value of r to memory
4445 if needed */
4446 void move_reg(int r, int s)
4448 SValue sv;
4450 if (r != s) {
4451 save_reg(r);
4452 sv.type.t = VT_INT;
4453 sv.r = s;
4454 sv.c.ul = 0;
4455 load(r, &sv);
4459 /* get address of vtop (vtop MUST BE an lvalue) */
4460 void gaddrof(void)
4462 vtop->r &= ~VT_LVAL;
4463 /* tricky: if saved lvalue, then we can go back to lvalue */
4464 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4465 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4468 #ifdef CONFIG_TCC_BCHECK
4469 /* generate lvalue bound code */
4470 void gbound(void)
4472 int lval_type;
4473 CType type1;
4475 vtop->r &= ~VT_MUSTBOUND;
4476 /* if lvalue, then use checking code before dereferencing */
4477 if (vtop->r & VT_LVAL) {
4478 /* if not VT_BOUNDED value, then make one */
4479 if (!(vtop->r & VT_BOUNDED)) {
4480 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4481 /* must save type because we must set it to int to get pointer */
4482 type1 = vtop->type;
4483 vtop->type.t = VT_INT;
4484 gaddrof();
4485 vpushi(0);
4486 gen_bounded_ptr_add();
4487 vtop->r |= lval_type;
4488 vtop->type = type1;
4490 /* then check for dereferencing */
4491 gen_bounded_ptr_deref();
4494 #endif
4496 /* store vtop a register belonging to class 'rc'. lvalues are
4497 converted to values. Cannot be used if cannot be converted to
4498 register value (such as structures). */
4499 int gv(int rc)
4501 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4502 unsigned long long ll;
4504 /* NOTE: get_reg can modify vstack[] */
4505 if (vtop->type.t & VT_BITFIELD) {
4506 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4507 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4508 /* remove bit field info to avoid loops */
4509 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4510 /* generate shifts */
4511 vpushi(32 - (bit_pos + bit_size));
4512 gen_op(TOK_SHL);
4513 vpushi(32 - bit_size);
4514 /* NOTE: transformed to SHR if unsigned */
4515 gen_op(TOK_SAR);
4516 r = gv(rc);
4517 } else {
4518 if (is_float(vtop->type.t) &&
4519 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4520 Sym *sym;
4521 int *ptr;
4522 unsigned long offset;
4524 /* XXX: unify with initializers handling ? */
4525 /* CPUs usually cannot use float constants, so we store them
4526 generically in data segment */
4527 size = type_size(&vtop->type, &align);
4528 offset = (data_section->data_offset + align - 1) & -align;
4529 data_section->data_offset = offset;
4530 /* XXX: not portable yet */
4531 ptr = section_ptr_add(data_section, size);
4532 size = size >> 2;
4533 for(i=0;i<size;i++)
4534 ptr[i] = vtop->c.tab[i];
4535 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4536 vtop->r |= VT_LVAL | VT_SYM;
4537 vtop->sym = sym;
4538 vtop->c.ul = 0;
4540 #ifdef CONFIG_TCC_BCHECK
4541 if (vtop->r & VT_MUSTBOUND)
4542 gbound();
4543 #endif
4545 r = vtop->r & VT_VALMASK;
4546 /* need to reload if:
4547 - constant
4548 - lvalue (need to dereference pointer)
4549 - already a register, but not in the right class */
4550 if (r >= VT_CONST ||
4551 (vtop->r & VT_LVAL) ||
4552 !(reg_classes[r] & rc) ||
4553 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4554 !(reg_classes[vtop->r2] & rc))) {
4555 r = get_reg(rc);
4556 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4557 /* two register type load : expand to two words
4558 temporarily */
4559 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4560 /* load constant */
4561 ll = vtop->c.ull;
4562 vtop->c.ui = ll; /* first word */
4563 load(r, vtop);
4564 vtop->r = r; /* save register value */
4565 vpushi(ll >> 32); /* second word */
4566 } else if (r >= VT_CONST ||
4567 (vtop->r & VT_LVAL)) {
4568 /* load from memory */
4569 load(r, vtop);
4570 vdup();
4571 vtop[-1].r = r; /* save register value */
4572 /* increment pointer to get second word */
4573 vtop->type.t = VT_INT;
4574 gaddrof();
4575 vpushi(4);
4576 gen_op('+');
4577 vtop->r |= VT_LVAL;
4578 } else {
4579 /* move registers */
4580 load(r, vtop);
4581 vdup();
4582 vtop[-1].r = r; /* save register value */
4583 vtop->r = vtop[-1].r2;
4585 /* allocate second register */
4586 rc2 = RC_INT;
4587 if (rc == RC_IRET)
4588 rc2 = RC_LRET;
4589 r2 = get_reg(rc2);
4590 load(r2, vtop);
4591 vpop();
4592 /* write second register */
4593 vtop->r2 = r2;
4594 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4595 int t1, t;
4596 /* lvalue of scalar type : need to use lvalue type
4597 because of possible cast */
4598 t = vtop->type.t;
4599 t1 = t;
4600 /* compute memory access type */
4601 if (vtop->r & VT_LVAL_BYTE)
4602 t = VT_BYTE;
4603 else if (vtop->r & VT_LVAL_SHORT)
4604 t = VT_SHORT;
4605 if (vtop->r & VT_LVAL_UNSIGNED)
4606 t |= VT_UNSIGNED;
4607 vtop->type.t = t;
4608 load(r, vtop);
4609 /* restore wanted type */
4610 vtop->type.t = t1;
4611 } else {
4612 /* one register type load */
4613 load(r, vtop);
4616 vtop->r = r;
4618 return r;
4621 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4622 void gv2(int rc1, int rc2)
4624 int v;
4626 /* generate more generic register first. But VT_JMP or VT_CMP
4627 values must be generated first in all cases to avoid possible
4628 reload errors */
4629 v = vtop[0].r & VT_VALMASK;
4630 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4631 vswap();
4632 gv(rc1);
4633 vswap();
4634 gv(rc2);
4635 /* test if reload is needed for first register */
4636 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4637 vswap();
4638 gv(rc1);
4639 vswap();
4641 } else {
4642 gv(rc2);
4643 vswap();
4644 gv(rc1);
4645 vswap();
4646 /* test if reload is needed for first register */
4647 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4648 gv(rc2);
4653 /* expand long long on stack in two int registers */
4654 void lexpand(void)
4656 int u;
4658 u = vtop->type.t & VT_UNSIGNED;
4659 gv(RC_INT);
4660 vdup();
4661 vtop[0].r = vtop[-1].r2;
4662 vtop[0].r2 = VT_CONST;
4663 vtop[-1].r2 = VT_CONST;
4664 vtop[0].type.t = VT_INT | u;
4665 vtop[-1].type.t = VT_INT | u;
4668 /* build a long long from two ints */
4669 void lbuild(int t)
4671 gv2(RC_INT, RC_INT);
4672 vtop[-1].r2 = vtop[0].r;
4673 vtop[-1].type.t = t;
4674 vpop();
4677 /* rotate n first stack elements to the bottom
4678 I1 ... In -> I2 ... In I1 [top is right]
4680 void vrotb(int n)
4682 int i;
4683 SValue tmp;
4685 tmp = vtop[-n + 1];
4686 for(i=-n+1;i!=0;i++)
4687 vtop[i] = vtop[i+1];
4688 vtop[0] = tmp;
4691 /* rotate n first stack elements to the top
4692 I1 ... In -> In I1 ... I(n-1) [top is right]
4694 void vrott(int n)
4696 int i;
4697 SValue tmp;
4699 tmp = vtop[0];
4700 for(i = 0;i < n - 1; i++)
4701 vtop[-i] = vtop[-i - 1];
4702 vtop[-n + 1] = tmp;
4705 /* pop stack value */
4706 void vpop(void)
4708 int v;
4709 v = vtop->r & VT_VALMASK;
4710 #ifdef TCC_TARGET_I386
4711 /* for x86, we need to pop the FP stack */
4712 if (v == TREG_ST0 && !nocode_wanted) {
4713 o(0xd9dd); /* fstp %st(1) */
4714 } else
4715 #endif
4716 if (v == VT_JMP || v == VT_JMPI) {
4717 /* need to put correct jump if && or || without test */
4718 gsym(vtop->c.ul);
4720 vtop--;
4723 /* convert stack entry to register and duplicate its value in another
4724 register */
4725 void gv_dup(void)
4727 int rc, t, r, r1;
4728 SValue sv;
4730 t = vtop->type.t;
4731 if ((t & VT_BTYPE) == VT_LLONG) {
4732 lexpand();
4733 gv_dup();
4734 vswap();
4735 vrotb(3);
4736 gv_dup();
4737 vrotb(4);
4738 /* stack: H L L1 H1 */
4739 lbuild(t);
4740 vrotb(3);
4741 vrotb(3);
4742 vswap();
4743 lbuild(t);
4744 vswap();
4745 } else {
4746 /* duplicate value */
4747 rc = RC_INT;
4748 sv.type.t = VT_INT;
4749 if (is_float(t)) {
4750 rc = RC_FLOAT;
4751 sv.type.t = t;
4753 r = gv(rc);
4754 r1 = get_reg(rc);
4755 sv.r = r;
4756 sv.c.ul = 0;
4757 load(r1, &sv); /* move r to r1 */
4758 vdup();
4759 /* duplicates value */
4760 vtop->r = r1;
4764 /* generate CPU independent (unsigned) long long operations */
4765 void gen_opl(int op)
4767 int t, a, b, op1, c, i;
4768 int func;
4769 SValue tmp;
4771 switch(op) {
4772 case '/':
4773 case TOK_PDIV:
4774 func = TOK___divdi3;
4775 goto gen_func;
4776 case TOK_UDIV:
4777 func = TOK___udivdi3;
4778 goto gen_func;
4779 case '%':
4780 func = TOK___moddi3;
4781 goto gen_func;
4782 case TOK_UMOD:
4783 func = TOK___umoddi3;
4784 gen_func:
4785 /* call generic long long function */
4786 vpush_global_sym(&func_old_type, func);
4787 vrott(3);
4788 gfunc_call(2);
4789 vpushi(0);
4790 vtop->r = REG_IRET;
4791 vtop->r2 = REG_LRET;
4792 break;
4793 case '^':
4794 case '&':
4795 case '|':
4796 case '*':
4797 case '+':
4798 case '-':
4799 t = vtop->type.t;
4800 vswap();
4801 lexpand();
4802 vrotb(3);
4803 lexpand();
4804 /* stack: L1 H1 L2 H2 */
4805 tmp = vtop[0];
4806 vtop[0] = vtop[-3];
4807 vtop[-3] = tmp;
4808 tmp = vtop[-2];
4809 vtop[-2] = vtop[-3];
4810 vtop[-3] = tmp;
4811 vswap();
4812 /* stack: H1 H2 L1 L2 */
4813 if (op == '*') {
4814 vpushv(vtop - 1);
4815 vpushv(vtop - 1);
4816 gen_op(TOK_UMULL);
4817 lexpand();
4818 /* stack: H1 H2 L1 L2 ML MH */
4819 for(i=0;i<4;i++)
4820 vrotb(6);
4821 /* stack: ML MH H1 H2 L1 L2 */
4822 tmp = vtop[0];
4823 vtop[0] = vtop[-2];
4824 vtop[-2] = tmp;
4825 /* stack: ML MH H1 L2 H2 L1 */
4826 gen_op('*');
4827 vrotb(3);
4828 vrotb(3);
4829 gen_op('*');
4830 /* stack: ML MH M1 M2 */
4831 gen_op('+');
4832 gen_op('+');
4833 } else if (op == '+' || op == '-') {
4834 /* XXX: add non carry method too (for MIPS or alpha) */
4835 if (op == '+')
4836 op1 = TOK_ADDC1;
4837 else
4838 op1 = TOK_SUBC1;
4839 gen_op(op1);
4840 /* stack: H1 H2 (L1 op L2) */
4841 vrotb(3);
4842 vrotb(3);
4843 gen_op(op1 + 1); /* TOK_xxxC2 */
4844 } else {
4845 gen_op(op);
4846 /* stack: H1 H2 (L1 op L2) */
4847 vrotb(3);
4848 vrotb(3);
4849 /* stack: (L1 op L2) H1 H2 */
4850 gen_op(op);
4851 /* stack: (L1 op L2) (H1 op H2) */
4853 /* stack: L H */
4854 lbuild(t);
4855 break;
4856 case TOK_SAR:
4857 case TOK_SHR:
4858 case TOK_SHL:
4859 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4860 t = vtop[-1].type.t;
4861 vswap();
4862 lexpand();
4863 vrotb(3);
4864 /* stack: L H shift */
4865 c = (int)vtop->c.i;
4866 /* constant: simpler */
4867 /* NOTE: all comments are for SHL. the other cases are
4868 done by swaping words */
4869 vpop();
4870 if (op != TOK_SHL)
4871 vswap();
4872 if (c >= 32) {
4873 /* stack: L H */
4874 vpop();
4875 if (c > 32) {
4876 vpushi(c - 32);
4877 gen_op(op);
4879 if (op != TOK_SAR) {
4880 vpushi(0);
4881 } else {
4882 gv_dup();
4883 vpushi(31);
4884 gen_op(TOK_SAR);
4886 vswap();
4887 } else {
4888 vswap();
4889 gv_dup();
4890 /* stack: H L L */
4891 vpushi(c);
4892 gen_op(op);
4893 vswap();
4894 vpushi(32 - c);
4895 if (op == TOK_SHL)
4896 gen_op(TOK_SHR);
4897 else
4898 gen_op(TOK_SHL);
4899 vrotb(3);
4900 /* stack: L L H */
4901 vpushi(c);
4902 if (op == TOK_SHL)
4903 gen_op(TOK_SHL);
4904 else
4905 gen_op(TOK_SHR);
4906 gen_op('|');
4908 if (op != TOK_SHL)
4909 vswap();
4910 lbuild(t);
4911 } else {
4912 /* XXX: should provide a faster fallback on x86 ? */
4913 switch(op) {
4914 case TOK_SAR:
4915 func = TOK___sardi3;
4916 goto gen_func;
4917 case TOK_SHR:
4918 func = TOK___shrdi3;
4919 goto gen_func;
4920 case TOK_SHL:
4921 func = TOK___shldi3;
4922 goto gen_func;
4925 break;
4926 default:
4927 /* compare operations */
4928 t = vtop->type.t;
4929 vswap();
4930 lexpand();
4931 vrotb(3);
4932 lexpand();
4933 /* stack: L1 H1 L2 H2 */
4934 tmp = vtop[-1];
4935 vtop[-1] = vtop[-2];
4936 vtop[-2] = tmp;
4937 /* stack: L1 L2 H1 H2 */
4938 /* compare high */
4939 op1 = op;
4940 /* when values are equal, we need to compare low words. since
4941 the jump is inverted, we invert the test too. */
4942 if (op1 == TOK_LT)
4943 op1 = TOK_LE;
4944 else if (op1 == TOK_GT)
4945 op1 = TOK_GE;
4946 else if (op1 == TOK_ULT)
4947 op1 = TOK_ULE;
4948 else if (op1 == TOK_UGT)
4949 op1 = TOK_UGE;
4950 a = 0;
4951 b = 0;
4952 gen_op(op1);
4953 if (op1 != TOK_NE) {
4954 a = gtst(1, 0);
4956 if (op != TOK_EQ) {
4957 /* generate non equal test */
4958 /* XXX: NOT PORTABLE yet */
4959 if (a == 0) {
4960 b = gtst(0, 0);
4961 } else {
4962 #if defined(TCC_TARGET_I386)
4963 b = psym(0x850f, 0);
4964 #elif defined(TCC_TARGET_ARM)
4965 b = ind;
4966 o(0x1A000000 | encbranch(ind, 0, 1));
4967 #else
4968 #error not supported
4969 #endif
4972 /* compare low. Always unsigned */
4973 op1 = op;
4974 if (op1 == TOK_LT)
4975 op1 = TOK_ULT;
4976 else if (op1 == TOK_LE)
4977 op1 = TOK_ULE;
4978 else if (op1 == TOK_GT)
4979 op1 = TOK_UGT;
4980 else if (op1 == TOK_GE)
4981 op1 = TOK_UGE;
4982 gen_op(op1);
4983 a = gtst(1, a);
4984 gsym(b);
4985 vseti(VT_JMPI, a);
4986 break;
4990 /* handle integer constant optimizations and various machine
4991 independent opt */
4992 void gen_opic(int op)
4994 int fc, c1, c2, n;
4995 SValue *v1, *v2;
4997 v1 = vtop - 1;
4998 v2 = vtop;
4999 /* currently, we cannot do computations with forward symbols */
5000 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5001 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5002 if (c1 && c2) {
5003 fc = v2->c.i;
5004 switch(op) {
5005 case '+': v1->c.i += fc; break;
5006 case '-': v1->c.i -= fc; break;
5007 case '&': v1->c.i &= fc; break;
5008 case '^': v1->c.i ^= fc; break;
5009 case '|': v1->c.i |= fc; break;
5010 case '*': v1->c.i *= fc; break;
5012 case TOK_PDIV:
5013 case '/':
5014 case '%':
5015 case TOK_UDIV:
5016 case TOK_UMOD:
5017 /* if division by zero, generate explicit division */
5018 if (fc == 0) {
5019 if (const_wanted)
5020 error("division by zero in constant");
5021 goto general_case;
5023 switch(op) {
5024 default: v1->c.i /= fc; break;
5025 case '%': v1->c.i %= fc; break;
5026 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
5027 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
5029 break;
5030 case TOK_SHL: v1->c.i <<= fc; break;
5031 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
5032 case TOK_SAR: v1->c.i >>= fc; break;
5033 /* tests */
5034 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
5035 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
5036 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
5037 case TOK_NE: v1->c.i = v1->c.i != fc; break;
5038 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
5039 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
5040 case TOK_LT: v1->c.i = v1->c.i < fc; break;
5041 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
5042 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
5043 case TOK_GT: v1->c.i = v1->c.i > fc; break;
5044 /* logical */
5045 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
5046 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
5047 default:
5048 goto general_case;
5050 vtop--;
5051 } else {
5052 /* if commutative ops, put c2 as constant */
5053 if (c1 && (op == '+' || op == '&' || op == '^' ||
5054 op == '|' || op == '*')) {
5055 vswap();
5056 swap(&c1, &c2);
5058 fc = vtop->c.i;
5059 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5060 op == TOK_PDIV) &&
5061 fc == 1) ||
5062 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5063 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5064 fc == 0) ||
5065 (op == '&' &&
5066 fc == -1))) {
5067 /* nothing to do */
5068 vtop--;
5069 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5070 /* try to use shifts instead of muls or divs */
5071 if (fc > 0 && (fc & (fc - 1)) == 0) {
5072 n = -1;
5073 while (fc) {
5074 fc >>= 1;
5075 n++;
5077 vtop->c.i = n;
5078 if (op == '*')
5079 op = TOK_SHL;
5080 else if (op == TOK_PDIV)
5081 op = TOK_SAR;
5082 else
5083 op = TOK_SHR;
5085 goto general_case;
5086 } else if (c2 && (op == '+' || op == '-') &&
5087 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5088 (VT_CONST | VT_SYM)) {
5089 /* symbol + constant case */
5090 if (op == '-')
5091 fc = -fc;
5092 vtop--;
5093 vtop->c.i += fc;
5094 } else {
5095 general_case:
5096 if (!nocode_wanted) {
5097 /* call low level op generator */
5098 gen_opi(op);
5099 } else {
5100 vtop--;
5106 /* generate a floating point operation with constant propagation */
5107 void gen_opif(int op)
5109 int c1, c2;
5110 SValue *v1, *v2;
5111 long double f1, f2;
5113 v1 = vtop - 1;
5114 v2 = vtop;
5115 /* currently, we cannot do computations with forward symbols */
5116 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5117 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5118 if (c1 && c2) {
5119 if (v1->type.t == VT_FLOAT) {
5120 f1 = v1->c.f;
5121 f2 = v2->c.f;
5122 } else if (v1->type.t == VT_DOUBLE) {
5123 f1 = v1->c.d;
5124 f2 = v2->c.d;
5125 } else {
5126 f1 = v1->c.ld;
5127 f2 = v2->c.ld;
5130 /* NOTE: we only do constant propagation if finite number (not
5131 NaN or infinity) (ANSI spec) */
5132 if (!ieee_finite(f1) || !ieee_finite(f2))
5133 goto general_case;
5135 switch(op) {
5136 case '+': f1 += f2; break;
5137 case '-': f1 -= f2; break;
5138 case '*': f1 *= f2; break;
5139 case '/':
5140 if (f2 == 0.0) {
5141 if (const_wanted)
5142 error("division by zero in constant");
5143 goto general_case;
5145 f1 /= f2;
5146 break;
5147 /* XXX: also handles tests ? */
5148 default:
5149 goto general_case;
5151 /* XXX: overflow test ? */
5152 if (v1->type.t == VT_FLOAT) {
5153 v1->c.f = f1;
5154 } else if (v1->type.t == VT_DOUBLE) {
5155 v1->c.d = f1;
5156 } else {
5157 v1->c.ld = f1;
5159 vtop--;
5160 } else {
5161 general_case:
5162 if (!nocode_wanted) {
5163 gen_opf(op);
5164 } else {
5165 vtop--;
5170 static int pointed_size(CType *type)
5172 int align;
5173 return type_size(pointed_type(type), &align);
5176 static inline int is_null_pointer(SValue *p)
5178 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5179 return 0;
5180 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5181 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5184 static inline int is_integer_btype(int bt)
5186 return (bt == VT_BYTE || bt == VT_SHORT ||
5187 bt == VT_INT || bt == VT_LLONG);
5190 /* check types for comparison or substraction of pointers */
5191 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5193 CType *type1, *type2, tmp_type1, tmp_type2;
5194 int bt1, bt2;
5196 /* null pointers are accepted for all comparisons as gcc */
5197 if (is_null_pointer(p1) || is_null_pointer(p2))
5198 return;
5199 type1 = &p1->type;
5200 type2 = &p2->type;
5201 bt1 = type1->t & VT_BTYPE;
5202 bt2 = type2->t & VT_BTYPE;
5203 /* accept comparison between pointer and integer with a warning */
5204 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5205 warning("comparison between pointer and integer");
5206 return;
5209 /* both must be pointers or implicit function pointers */
5210 if (bt1 == VT_PTR) {
5211 type1 = pointed_type(type1);
5212 } else if (bt1 != VT_FUNC)
5213 goto invalid_operands;
5215 if (bt2 == VT_PTR) {
5216 type2 = pointed_type(type2);
5217 } else if (bt2 != VT_FUNC) {
5218 invalid_operands:
5219 error("invalid operands to binary %s", get_tok_str(op, NULL));
5221 if ((type1->t & VT_BTYPE) == VT_VOID ||
5222 (type2->t & VT_BTYPE) == VT_VOID)
5223 return;
5224 tmp_type1 = *type1;
5225 tmp_type2 = *type2;
5226 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5227 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5228 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5229 /* gcc-like error if '-' is used */
5230 if (op == '-')
5231 goto invalid_operands;
5232 else
5233 warning("comparison of distinct pointer types lacks a cast");
5237 /* generic gen_op: handles types problems */
5238 void gen_op(int op)
5240 int u, t1, t2, bt1, bt2, t;
5241 CType type1;
5243 t1 = vtop[-1].type.t;
5244 t2 = vtop[0].type.t;
5245 bt1 = t1 & VT_BTYPE;
5246 bt2 = t2 & VT_BTYPE;
5248 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5249 /* at least one operand is a pointer */
5250 /* relationnal op: must be both pointers */
5251 if (op >= TOK_ULT && op <= TOK_GT) {
5252 check_comparison_pointer_types(vtop - 1, vtop, op);
5253 /* pointers are handled are unsigned */
5254 t = VT_INT | VT_UNSIGNED;
5255 goto std_op;
5257 /* if both pointers, then it must be the '-' op */
5258 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5259 if (op != '-')
5260 error("cannot use pointers here");
5261 check_comparison_pointer_types(vtop - 1, vtop, op);
5262 /* XXX: check that types are compatible */
5263 u = pointed_size(&vtop[-1].type);
5264 gen_opic(op);
5265 /* set to integer type */
5266 vtop->type.t = VT_INT;
5267 vpushi(u);
5268 gen_op(TOK_PDIV);
5269 } else {
5270 /* exactly one pointer : must be '+' or '-'. */
5271 if (op != '-' && op != '+')
5272 error("cannot use pointers here");
5273 /* Put pointer as first operand */
5274 if (bt2 == VT_PTR) {
5275 vswap();
5276 swap(&t1, &t2);
5278 type1 = vtop[-1].type;
5279 /* XXX: cast to int ? (long long case) */
5280 vpushi(pointed_size(&vtop[-1].type));
5281 gen_op('*');
5282 #ifdef CONFIG_TCC_BCHECK
5283 /* if evaluating constant expression, no code should be
5284 generated, so no bound check */
5285 if (do_bounds_check && !const_wanted) {
5286 /* if bounded pointers, we generate a special code to
5287 test bounds */
5288 if (op == '-') {
5289 vpushi(0);
5290 vswap();
5291 gen_op('-');
5293 gen_bounded_ptr_add();
5294 } else
5295 #endif
5297 gen_opic(op);
5299 /* put again type if gen_opic() swaped operands */
5300 vtop->type = type1;
5302 } else if (is_float(bt1) || is_float(bt2)) {
5303 /* compute bigger type and do implicit casts */
5304 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5305 t = VT_LDOUBLE;
5306 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5307 t = VT_DOUBLE;
5308 } else {
5309 t = VT_FLOAT;
5311 /* floats can only be used for a few operations */
5312 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5313 (op < TOK_ULT || op > TOK_GT))
5314 error("invalid operands for binary operation");
5315 goto std_op;
5316 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5317 /* cast to biggest op */
5318 t = VT_LLONG;
5319 /* convert to unsigned if it does not fit in a long long */
5320 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5321 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5322 t |= VT_UNSIGNED;
5323 goto std_op;
5324 } else {
5325 /* integer operations */
5326 t = VT_INT;
5327 /* convert to unsigned if it does not fit in an integer */
5328 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5329 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5330 t |= VT_UNSIGNED;
5331 std_op:
5332 /* XXX: currently, some unsigned operations are explicit, so
5333 we modify them here */
5334 if (t & VT_UNSIGNED) {
5335 if (op == TOK_SAR)
5336 op = TOK_SHR;
5337 else if (op == '/')
5338 op = TOK_UDIV;
5339 else if (op == '%')
5340 op = TOK_UMOD;
5341 else if (op == TOK_LT)
5342 op = TOK_ULT;
5343 else if (op == TOK_GT)
5344 op = TOK_UGT;
5345 else if (op == TOK_LE)
5346 op = TOK_ULE;
5347 else if (op == TOK_GE)
5348 op = TOK_UGE;
5350 vswap();
5351 type1.t = t;
5352 gen_cast(&type1);
5353 vswap();
5354 /* special case for shifts and long long: we keep the shift as
5355 an integer */
5356 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5357 type1.t = VT_INT;
5358 gen_cast(&type1);
5359 if (is_float(t))
5360 gen_opif(op);
5361 else if ((t & VT_BTYPE) == VT_LLONG)
5362 gen_opl(op);
5363 else
5364 gen_opic(op);
5365 if (op >= TOK_ULT && op <= TOK_GT) {
5366 /* relationnal op: the result is an int */
5367 vtop->type.t = VT_INT;
5368 } else {
5369 vtop->type.t = t;
5374 /* generic itof for unsigned long long case */
5375 void gen_cvt_itof1(int t)
5377 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5378 (VT_LLONG | VT_UNSIGNED)) {
5380 if (t == VT_FLOAT)
5381 vpush_global_sym(&func_old_type, TOK___ulltof);
5382 else if (t == VT_DOUBLE)
5383 vpush_global_sym(&func_old_type, TOK___ulltod);
5384 else
5385 vpush_global_sym(&func_old_type, TOK___ulltold);
5386 vrott(2);
5387 gfunc_call(1);
5388 vpushi(0);
5389 vtop->r = REG_FRET;
5390 } else {
5391 gen_cvt_itof(t);
5395 /* generic ftoi for unsigned long long case */
5396 void gen_cvt_ftoi1(int t)
5398 int st;
5400 if (t == (VT_LLONG | VT_UNSIGNED)) {
5401 /* not handled natively */
5402 st = vtop->type.t & VT_BTYPE;
5403 if (st == VT_FLOAT)
5404 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5405 else if (st == VT_DOUBLE)
5406 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5407 else
5408 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5409 vrott(2);
5410 gfunc_call(1);
5411 vpushi(0);
5412 vtop->r = REG_IRET;
5413 vtop->r2 = REG_LRET;
5414 } else {
5415 gen_cvt_ftoi(t);
5419 /* force char or short cast */
5420 void force_charshort_cast(int t)
5422 int bits, dbt;
5423 dbt = t & VT_BTYPE;
5424 /* XXX: add optimization if lvalue : just change type and offset */
5425 if (dbt == VT_BYTE)
5426 bits = 8;
5427 else
5428 bits = 16;
5429 if (t & VT_UNSIGNED) {
5430 vpushi((1 << bits) - 1);
5431 gen_op('&');
5432 } else {
5433 bits = 32 - bits;
5434 vpushi(bits);
5435 gen_op(TOK_SHL);
5436 vpushi(bits);
5437 gen_op(TOK_SAR);
5441 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5442 static void gen_cast(CType *type)
5444 int sbt, dbt, sf, df, c;
5446 /* special delayed cast for char/short */
5447 /* XXX: in some cases (multiple cascaded casts), it may still
5448 be incorrect */
5449 if (vtop->r & VT_MUSTCAST) {
5450 vtop->r &= ~VT_MUSTCAST;
5451 force_charshort_cast(vtop->type.t);
5454 /* bitfields first get cast to ints */
5455 if (vtop->type.t & VT_BITFIELD) {
5456 gv(RC_INT);
5459 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5460 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5462 if (sbt != dbt && !nocode_wanted) {
5463 sf = is_float(sbt);
5464 df = is_float(dbt);
5465 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5466 if (sf && df) {
5467 /* convert from fp to fp */
5468 if (c) {
5469 /* constant case: we can do it now */
5470 /* XXX: in ISOC, cannot do it if error in convert */
5471 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5472 vtop->c.f = (float)vtop->c.d;
5473 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5474 vtop->c.f = (float)vtop->c.ld;
5475 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5476 vtop->c.d = (double)vtop->c.f;
5477 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5478 vtop->c.d = (double)vtop->c.ld;
5479 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5480 vtop->c.ld = (long double)vtop->c.f;
5481 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5482 vtop->c.ld = (long double)vtop->c.d;
5483 } else {
5484 /* non constant case: generate code */
5485 gen_cvt_ftof(dbt);
5487 } else if (df) {
5488 /* convert int to fp */
5489 if (c) {
5490 switch(sbt) {
5491 case VT_LLONG | VT_UNSIGNED:
5492 case VT_LLONG:
5493 /* XXX: add const cases for long long */
5494 goto do_itof;
5495 case VT_INT | VT_UNSIGNED:
5496 switch(dbt) {
5497 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5498 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5499 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5501 break;
5502 default:
5503 switch(dbt) {
5504 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5505 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5506 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5508 break;
5510 } else {
5511 do_itof:
5512 #if !defined(TCC_TARGET_ARM)
5513 gen_cvt_itof1(dbt);
5514 #else
5515 gen_cvt_itof(dbt);
5516 #endif
5518 } else if (sf) {
5519 /* convert fp to int */
5520 /* we handle char/short/etc... with generic code */
5521 if (dbt != (VT_INT | VT_UNSIGNED) &&
5522 dbt != (VT_LLONG | VT_UNSIGNED) &&
5523 dbt != VT_LLONG)
5524 dbt = VT_INT;
5525 if (c) {
5526 switch(dbt) {
5527 case VT_LLONG | VT_UNSIGNED:
5528 case VT_LLONG:
5529 /* XXX: add const cases for long long */
5530 goto do_ftoi;
5531 case VT_INT | VT_UNSIGNED:
5532 switch(sbt) {
5533 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5534 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5535 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5537 break;
5538 default:
5539 /* int case */
5540 switch(sbt) {
5541 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5542 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5543 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5545 break;
5547 } else {
5548 do_ftoi:
5549 gen_cvt_ftoi1(dbt);
5551 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5552 /* additional cast for char/short/bool... */
5553 vtop->type.t = dbt;
5554 gen_cast(type);
5556 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5557 if ((sbt & VT_BTYPE) != VT_LLONG) {
5558 /* scalar to long long */
5559 if (c) {
5560 if (sbt == (VT_INT | VT_UNSIGNED))
5561 vtop->c.ll = vtop->c.ui;
5562 else
5563 vtop->c.ll = vtop->c.i;
5564 } else {
5565 /* machine independent conversion */
5566 gv(RC_INT);
5567 /* generate high word */
5568 if (sbt == (VT_INT | VT_UNSIGNED)) {
5569 vpushi(0);
5570 gv(RC_INT);
5571 } else {
5572 gv_dup();
5573 vpushi(31);
5574 gen_op(TOK_SAR);
5576 /* patch second register */
5577 vtop[-1].r2 = vtop->r;
5578 vpop();
5581 } else if (dbt == VT_BOOL) {
5582 /* scalar to bool */
5583 vpushi(0);
5584 gen_op(TOK_NE);
5585 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5586 (dbt & VT_BTYPE) == VT_SHORT) {
5587 force_charshort_cast(dbt);
5588 } else if ((dbt & VT_BTYPE) == VT_INT) {
5589 /* scalar to int */
5590 if (sbt == VT_LLONG) {
5591 /* from long long: just take low order word */
5592 lexpand();
5593 vpop();
5595 /* if lvalue and single word type, nothing to do because
5596 the lvalue already contains the real type size (see
5597 VT_LVAL_xxx constants) */
5600 vtop->type = *type;
5603 /* return type size. Put alignment at 'a' */
5604 static int type_size(CType *type, int *a)
5606 Sym *s;
5607 int bt;
5609 bt = type->t & VT_BTYPE;
5610 if (bt == VT_STRUCT) {
5611 /* struct/union */
5612 s = type->ref;
5613 *a = s->r;
5614 return s->c;
5615 } else if (bt == VT_PTR) {
5616 if (type->t & VT_ARRAY) {
5617 s = type->ref;
5618 return type_size(&s->type, a) * s->c;
5619 } else {
5620 *a = PTR_SIZE;
5621 return PTR_SIZE;
5623 } else if (bt == VT_LDOUBLE) {
5624 *a = LDOUBLE_ALIGN;
5625 return LDOUBLE_SIZE;
5626 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5627 *a = 4; /* XXX: i386 specific */
5628 return 8;
5629 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5630 *a = 4;
5631 return 4;
5632 } else if (bt == VT_SHORT) {
5633 *a = 2;
5634 return 2;
5635 } else {
5636 /* char, void, function, _Bool */
5637 *a = 1;
5638 return 1;
5642 /* return the pointed type of t */
5643 static inline CType *pointed_type(CType *type)
5645 return &type->ref->type;
5648 /* modify type so that its it is a pointer to type. */
5649 static void mk_pointer(CType *type)
5651 Sym *s;
5652 s = sym_push(SYM_FIELD, type, 0, -1);
5653 type->t = VT_PTR | (type->t & ~VT_TYPE);
5654 type->ref = s;
5657 /* compare function types. OLD functions match any new functions */
5658 static int is_compatible_func(CType *type1, CType *type2)
5660 Sym *s1, *s2;
5662 s1 = type1->ref;
5663 s2 = type2->ref;
5664 if (!is_compatible_types(&s1->type, &s2->type))
5665 return 0;
5666 /* XXX: not complete */
5667 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5668 return 1;
5669 if (s1->c != s2->c)
5670 return 0;
5671 while (s1 != NULL) {
5672 if (s2 == NULL)
5673 return 0;
5674 if (!is_compatible_types(&s1->type, &s2->type))
5675 return 0;
5676 s1 = s1->next;
5677 s2 = s2->next;
5679 if (s2)
5680 return 0;
5681 return 1;
5684 /* return true if type1 and type2 are exactly the same (including
5685 qualifiers).
5687 - enums are not checked as gcc __builtin_types_compatible_p ()
5689 static int is_compatible_types(CType *type1, CType *type2)
5691 int bt1, t1, t2;
5693 t1 = type1->t & VT_TYPE;
5694 t2 = type2->t & VT_TYPE;
5695 /* XXX: bitfields ? */
5696 if (t1 != t2)
5697 return 0;
5698 /* test more complicated cases */
5699 bt1 = t1 & VT_BTYPE;
5700 if (bt1 == VT_PTR) {
5701 type1 = pointed_type(type1);
5702 type2 = pointed_type(type2);
5703 return is_compatible_types(type1, type2);
5704 } else if (bt1 == VT_STRUCT) {
5705 return (type1->ref == type2->ref);
5706 } else if (bt1 == VT_FUNC) {
5707 return is_compatible_func(type1, type2);
5708 } else {
5709 return 1;
5713 /* print a type. If 'varstr' is not NULL, then the variable is also
5714 printed in the type */
5715 /* XXX: union */
5716 /* XXX: add array and function pointers */
5717 void type_to_str(char *buf, int buf_size,
5718 CType *type, const char *varstr)
5720 int bt, v, t;
5721 Sym *s, *sa;
5722 char buf1[256];
5723 const char *tstr;
5725 t = type->t & VT_TYPE;
5726 bt = t & VT_BTYPE;
5727 buf[0] = '\0';
5728 if (t & VT_CONSTANT)
5729 pstrcat(buf, buf_size, "const ");
5730 if (t & VT_VOLATILE)
5731 pstrcat(buf, buf_size, "volatile ");
5732 if (t & VT_UNSIGNED)
5733 pstrcat(buf, buf_size, "unsigned ");
5734 switch(bt) {
5735 case VT_VOID:
5736 tstr = "void";
5737 goto add_tstr;
5738 case VT_BOOL:
5739 tstr = "_Bool";
5740 goto add_tstr;
5741 case VT_BYTE:
5742 tstr = "char";
5743 goto add_tstr;
5744 case VT_SHORT:
5745 tstr = "short";
5746 goto add_tstr;
5747 case VT_INT:
5748 tstr = "int";
5749 goto add_tstr;
5750 case VT_LONG:
5751 tstr = "long";
5752 goto add_tstr;
5753 case VT_LLONG:
5754 tstr = "long long";
5755 goto add_tstr;
5756 case VT_FLOAT:
5757 tstr = "float";
5758 goto add_tstr;
5759 case VT_DOUBLE:
5760 tstr = "double";
5761 goto add_tstr;
5762 case VT_LDOUBLE:
5763 tstr = "long double";
5764 add_tstr:
5765 pstrcat(buf, buf_size, tstr);
5766 break;
5767 case VT_ENUM:
5768 case VT_STRUCT:
5769 if (bt == VT_STRUCT)
5770 tstr = "struct ";
5771 else
5772 tstr = "enum ";
5773 pstrcat(buf, buf_size, tstr);
5774 v = type->ref->v & ~SYM_STRUCT;
5775 if (v >= SYM_FIRST_ANOM)
5776 pstrcat(buf, buf_size, "<anonymous>");
5777 else
5778 pstrcat(buf, buf_size, get_tok_str(v, NULL));
5779 break;
5780 case VT_FUNC:
5781 s = type->ref;
5782 type_to_str(buf, buf_size, &s->type, varstr);
5783 pstrcat(buf, buf_size, "(");
5784 sa = s->next;
5785 while (sa != NULL) {
5786 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
5787 pstrcat(buf, buf_size, buf1);
5788 sa = sa->next;
5789 if (sa)
5790 pstrcat(buf, buf_size, ", ");
5792 pstrcat(buf, buf_size, ")");
5793 goto no_var;
5794 case VT_PTR:
5795 s = type->ref;
5796 pstrcpy(buf1, sizeof(buf1), "*");
5797 if (varstr)
5798 pstrcat(buf1, sizeof(buf1), varstr);
5799 type_to_str(buf, buf_size, &s->type, buf1);
5800 goto no_var;
5802 if (varstr) {
5803 pstrcat(buf, buf_size, " ");
5804 pstrcat(buf, buf_size, varstr);
5806 no_var: ;
5809 /* verify type compatibility to store vtop in 'dt' type, and generate
5810 casts if needed. */
5811 static void gen_assign_cast(CType *dt)
5813 CType *st, *type1, *type2, tmp_type1, tmp_type2;
5814 char buf1[256], buf2[256];
5815 int dbt, sbt;
5817 st = &vtop->type; /* source type */
5818 dbt = dt->t & VT_BTYPE;
5819 sbt = st->t & VT_BTYPE;
5820 if (dt->t & VT_CONSTANT)
5821 warning("assignment of read-only location");
5822 switch(dbt) {
5823 case VT_PTR:
5824 /* special cases for pointers */
5825 /* '0' can also be a pointer */
5826 if (is_null_pointer(vtop))
5827 goto type_ok;
5828 /* accept implicit pointer to integer cast with warning */
5829 if (is_integer_btype(sbt)) {
5830 warning("assignment makes pointer from integer without a cast");
5831 goto type_ok;
5833 type1 = pointed_type(dt);
5834 /* a function is implicitely a function pointer */
5835 if (sbt == VT_FUNC) {
5836 if ((type1->t & VT_BTYPE) != VT_VOID &&
5837 !is_compatible_types(pointed_type(dt), st))
5838 goto error;
5839 else
5840 goto type_ok;
5842 if (sbt != VT_PTR)
5843 goto error;
5844 type2 = pointed_type(st);
5845 if ((type1->t & VT_BTYPE) == VT_VOID ||
5846 (type2->t & VT_BTYPE) == VT_VOID) {
5847 /* void * can match anything */
5848 } else {
5849 /* exact type match, except for unsigned */
5850 tmp_type1 = *type1;
5851 tmp_type2 = *type2;
5852 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5853 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5854 if (!is_compatible_types(&tmp_type1, &tmp_type2))
5855 goto error;
5857 /* check const and volatile */
5858 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
5859 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
5860 warning("assignment discards qualifiers from pointer target type");
5861 break;
5862 case VT_BYTE:
5863 case VT_SHORT:
5864 case VT_INT:
5865 case VT_LLONG:
5866 if (sbt == VT_PTR || sbt == VT_FUNC) {
5867 warning("assignment makes integer from pointer without a cast");
5869 /* XXX: more tests */
5870 break;
5871 case VT_STRUCT:
5872 tmp_type1 = *dt;
5873 tmp_type2 = *st;
5874 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
5875 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
5876 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5877 error:
5878 type_to_str(buf1, sizeof(buf1), st, NULL);
5879 type_to_str(buf2, sizeof(buf2), dt, NULL);
5880 error("cannot cast '%s' to '%s'", buf1, buf2);
5882 break;
5884 type_ok:
5885 gen_cast(dt);
5888 /* store vtop in lvalue pushed on stack */
5889 void vstore(void)
5891 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5893 ft = vtop[-1].type.t;
5894 sbt = vtop->type.t & VT_BTYPE;
5895 dbt = ft & VT_BTYPE;
5896 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
5897 (sbt == VT_INT && dbt == VT_SHORT)) {
5898 /* optimize char/short casts */
5899 delayed_cast = VT_MUSTCAST;
5900 vtop->type.t = ft & VT_TYPE;
5901 /* XXX: factorize */
5902 if (ft & VT_CONSTANT)
5903 warning("assignment of read-only location");
5904 } else {
5905 delayed_cast = 0;
5906 gen_assign_cast(&vtop[-1].type);
5909 if (sbt == VT_STRUCT) {
5910 /* if structure, only generate pointer */
5911 /* structure assignment : generate memcpy */
5912 /* XXX: optimize if small size */
5913 if (!nocode_wanted) {
5914 size = type_size(&vtop->type, &align);
5916 vpush_global_sym(&func_old_type, TOK_memcpy);
5918 /* destination */
5919 vpushv(vtop - 2);
5920 vtop->type.t = VT_INT;
5921 gaddrof();
5922 /* source */
5923 vpushv(vtop - 2);
5924 vtop->type.t = VT_INT;
5925 gaddrof();
5926 /* type size */
5927 vpushi(size);
5928 gfunc_call(3);
5930 vswap();
5931 vpop();
5932 } else {
5933 vswap();
5934 vpop();
5936 /* leave source on stack */
5937 } else if (ft & VT_BITFIELD) {
5938 /* bitfield store handling */
5939 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
5940 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5941 /* remove bit field info to avoid loops */
5942 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
5944 /* duplicate destination */
5945 vdup();
5946 vtop[-1] = vtop[-2];
5948 /* mask and shift source */
5949 vpushi((1 << bit_size) - 1);
5950 gen_op('&');
5951 vpushi(bit_pos);
5952 gen_op(TOK_SHL);
5953 /* load destination, mask and or with source */
5954 vswap();
5955 vpushi(~(((1 << bit_size) - 1) << bit_pos));
5956 gen_op('&');
5957 gen_op('|');
5958 /* store result */
5959 vstore();
5960 } else {
5961 #ifdef CONFIG_TCC_BCHECK
5962 /* bound check case */
5963 if (vtop[-1].r & VT_MUSTBOUND) {
5964 vswap();
5965 gbound();
5966 vswap();
5968 #endif
5969 if (!nocode_wanted) {
5970 rc = RC_INT;
5971 if (is_float(ft))
5972 rc = RC_FLOAT;
5973 r = gv(rc); /* generate value */
5974 /* if lvalue was saved on stack, must read it */
5975 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
5976 SValue sv;
5977 t = get_reg(RC_INT);
5978 sv.type.t = VT_INT;
5979 sv.r = VT_LOCAL | VT_LVAL;
5980 sv.c.ul = vtop[-1].c.ul;
5981 load(t, &sv);
5982 vtop[-1].r = t | VT_LVAL;
5984 store(r, vtop - 1);
5985 /* two word case handling : store second register at word + 4 */
5986 if ((ft & VT_BTYPE) == VT_LLONG) {
5987 vswap();
5988 /* convert to int to increment easily */
5989 vtop->type.t = VT_INT;
5990 gaddrof();
5991 vpushi(4);
5992 gen_op('+');
5993 vtop->r |= VT_LVAL;
5994 vswap();
5995 /* XXX: it works because r2 is spilled last ! */
5996 store(vtop->r2, vtop - 1);
5999 vswap();
6000 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6001 vtop->r |= delayed_cast;
6005 /* post defines POST/PRE add. c is the token ++ or -- */
6006 void inc(int post, int c)
6008 test_lvalue();
6009 vdup(); /* save lvalue */
6010 if (post) {
6011 gv_dup(); /* duplicate value */
6012 vrotb(3);
6013 vrotb(3);
6015 /* add constant */
6016 vpushi(c - TOK_MID);
6017 gen_op('+');
6018 vstore(); /* store value */
6019 if (post)
6020 vpop(); /* if post op, return saved value */
6023 /* Parse GNUC __attribute__ extension. Currently, the following
6024 extensions are recognized:
6025 - aligned(n) : set data/function alignment.
6026 - section(x) : generate data/code in this section.
6027 - unused : currently ignored, but may be used someday.
6029 static void parse_attribute(AttributeDef *ad)
6031 int t, n;
6033 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6034 next();
6035 skip('(');
6036 skip('(');
6037 while (tok != ')') {
6038 if (tok < TOK_IDENT)
6039 expect("attribute name");
6040 t = tok;
6041 next();
6042 switch(t) {
6043 case TOK_SECTION1:
6044 case TOK_SECTION2:
6045 skip('(');
6046 if (tok != TOK_STR)
6047 expect("section name");
6048 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6049 next();
6050 skip(')');
6051 break;
6052 case TOK_ALIGNED1:
6053 case TOK_ALIGNED2:
6054 if (tok == '(') {
6055 next();
6056 n = expr_const();
6057 if (n <= 0 || (n & (n - 1)) != 0)
6058 error("alignment must be a positive power of two");
6059 skip(')');
6060 } else {
6061 n = MAX_ALIGN;
6063 ad->aligned = n;
6064 break;
6065 case TOK_UNUSED1:
6066 case TOK_UNUSED2:
6067 /* currently, no need to handle it because tcc does not
6068 track unused objects */
6069 break;
6070 case TOK_NORETURN1:
6071 case TOK_NORETURN2:
6072 /* currently, no need to handle it because tcc does not
6073 track unused objects */
6074 break;
6075 case TOK_CDECL1:
6076 case TOK_CDECL2:
6077 case TOK_CDECL3:
6078 ad->func_call = FUNC_CDECL;
6079 break;
6080 case TOK_STDCALL1:
6081 case TOK_STDCALL2:
6082 case TOK_STDCALL3:
6083 ad->func_call = FUNC_STDCALL;
6084 break;
6085 default:
6086 if (tcc_state->warn_unsupported)
6087 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6088 /* skip parameters */
6089 /* XXX: skip parenthesis too */
6090 if (tok == '(') {
6091 next();
6092 while (tok != ')' && tok != -1)
6093 next();
6094 next();
6096 break;
6098 if (tok != ',')
6099 break;
6100 next();
6102 skip(')');
6103 skip(')');
6107 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6108 static void struct_decl(CType *type, int u)
6110 int a, v, size, align, maxalign, c, offset;
6111 int bit_size, bit_pos, bsize, bt, lbit_pos;
6112 Sym *s, *ss, **ps;
6113 AttributeDef ad;
6114 CType type1, btype;
6116 a = tok; /* save decl type */
6117 next();
6118 if (tok != '{') {
6119 v = tok;
6120 next();
6121 /* struct already defined ? return it */
6122 if (v < TOK_IDENT)
6123 expect("struct/union/enum name");
6124 s = struct_find(v);
6125 if (s) {
6126 if (s->type.t != a)
6127 error("invalid type");
6128 goto do_decl;
6130 } else {
6131 v = anon_sym++;
6133 type1.t = a;
6134 /* we put an undefined size for struct/union */
6135 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6136 s->r = 0; /* default alignment is zero as gcc */
6137 /* put struct/union/enum name in type */
6138 do_decl:
6139 type->t = u;
6140 type->ref = s;
6142 if (tok == '{') {
6143 next();
6144 if (s->c != -1)
6145 error("struct/union/enum already defined");
6146 /* cannot be empty */
6147 c = 0;
6148 /* non empty enums are not allowed */
6149 if (a == TOK_ENUM) {
6150 for(;;) {
6151 v = tok;
6152 if (v < TOK_UIDENT)
6153 expect("identifier");
6154 next();
6155 if (tok == '=') {
6156 next();
6157 c = expr_const();
6159 /* enum symbols have static storage */
6160 ss = sym_push(v, &int_type, VT_CONST, c);
6161 ss->type.t |= VT_STATIC;
6162 if (tok != ',')
6163 break;
6164 next();
6165 c++;
6166 /* NOTE: we accept a trailing comma */
6167 if (tok == '}')
6168 break;
6170 skip('}');
6171 } else {
6172 maxalign = 1;
6173 ps = &s->next;
6174 bit_pos = 0;
6175 offset = 0;
6176 while (tok != '}') {
6177 parse_btype(&btype, &ad);
6178 while (1) {
6179 bit_size = -1;
6180 v = 0;
6181 type1 = btype;
6182 if (tok != ':') {
6183 type_decl(&type1, &ad, &v, TYPE_DIRECT);
6184 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6185 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6186 error("invalid type for '%s'",
6187 get_tok_str(v, NULL));
6189 if (tok == ':') {
6190 next();
6191 bit_size = expr_const();
6192 /* XXX: handle v = 0 case for messages */
6193 if (bit_size < 0)
6194 error("negative width in bit-field '%s'",
6195 get_tok_str(v, NULL));
6196 if (v && bit_size == 0)
6197 error("zero width for bit-field '%s'",
6198 get_tok_str(v, NULL));
6200 size = type_size(&type1, &align);
6201 lbit_pos = 0;
6202 if (bit_size >= 0) {
6203 bt = type1.t & VT_BTYPE;
6204 if (bt != VT_INT &&
6205 bt != VT_BYTE &&
6206 bt != VT_SHORT &&
6207 bt != VT_ENUM)
6208 error("bitfields must have scalar type");
6209 bsize = size * 8;
6210 if (bit_size > bsize) {
6211 error("width of '%s' exceeds its type",
6212 get_tok_str(v, NULL));
6213 } else if (bit_size == bsize) {
6214 /* no need for bit fields */
6215 bit_pos = 0;
6216 } else if (bit_size == 0) {
6217 /* XXX: what to do if only padding in a
6218 structure ? */
6219 /* zero size: means to pad */
6220 if (bit_pos > 0)
6221 bit_pos = bsize;
6222 } else {
6223 /* we do not have enough room ? */
6224 if ((bit_pos + bit_size) > bsize)
6225 bit_pos = 0;
6226 lbit_pos = bit_pos;
6227 /* XXX: handle LSB first */
6228 type1.t |= VT_BITFIELD |
6229 (bit_pos << VT_STRUCT_SHIFT) |
6230 (bit_size << (VT_STRUCT_SHIFT + 6));
6231 bit_pos += bit_size;
6233 } else {
6234 bit_pos = 0;
6236 if (v) {
6237 /* add new memory data only if starting
6238 bit field */
6239 if (lbit_pos == 0) {
6240 if (a == TOK_STRUCT) {
6241 c = (c + align - 1) & -align;
6242 offset = c;
6243 c += size;
6244 } else {
6245 offset = 0;
6246 if (size > c)
6247 c = size;
6249 if (align > maxalign)
6250 maxalign = align;
6252 #if 0
6253 printf("add field %s offset=%d",
6254 get_tok_str(v, NULL), offset);
6255 if (type1.t & VT_BITFIELD) {
6256 printf(" pos=%d size=%d",
6257 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6258 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6260 printf("\n");
6261 #endif
6262 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6263 *ps = ss;
6264 ps = &ss->next;
6266 if (tok == ';' || tok == TOK_EOF)
6267 break;
6268 skip(',');
6270 skip(';');
6272 skip('}');
6273 /* store size and alignment */
6274 s->c = (c + maxalign - 1) & -maxalign;
6275 s->r = maxalign;
6280 /* return 0 if no type declaration. otherwise, return the basic type
6281 and skip it.
6283 static int parse_btype(CType *type, AttributeDef *ad)
6285 int t, u, type_found, typespec_found;
6286 Sym *s;
6287 CType type1;
6289 memset(ad, 0, sizeof(AttributeDef));
6290 type_found = 0;
6291 typespec_found = 0;
6292 t = 0;
6293 while(1) {
6294 switch(tok) {
6295 case TOK_EXTENSION:
6296 /* currently, we really ignore extension */
6297 next();
6298 continue;
6300 /* basic types */
6301 case TOK_CHAR:
6302 u = VT_BYTE;
6303 basic_type:
6304 next();
6305 basic_type1:
6306 if ((t & VT_BTYPE) != 0)
6307 error("too many basic types");
6308 t |= u;
6309 typespec_found = 1;
6310 break;
6311 case TOK_VOID:
6312 u = VT_VOID;
6313 goto basic_type;
6314 case TOK_SHORT:
6315 u = VT_SHORT;
6316 goto basic_type;
6317 case TOK_INT:
6318 next();
6319 typespec_found = 1;
6320 break;
6321 case TOK_LONG:
6322 next();
6323 if ((t & VT_BTYPE) == VT_DOUBLE) {
6324 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6325 } else if ((t & VT_BTYPE) == VT_LONG) {
6326 t = (t & ~VT_BTYPE) | VT_LLONG;
6327 } else {
6328 u = VT_LONG;
6329 goto basic_type1;
6331 break;
6332 case TOK_BOOL:
6333 u = VT_BOOL;
6334 goto basic_type;
6335 case TOK_FLOAT:
6336 u = VT_FLOAT;
6337 goto basic_type;
6338 case TOK_DOUBLE:
6339 next();
6340 if ((t & VT_BTYPE) == VT_LONG) {
6341 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6342 } else {
6343 u = VT_DOUBLE;
6344 goto basic_type1;
6346 break;
6347 case TOK_ENUM:
6348 struct_decl(&type1, VT_ENUM);
6349 basic_type2:
6350 u = type1.t;
6351 type->ref = type1.ref;
6352 goto basic_type1;
6353 case TOK_STRUCT:
6354 case TOK_UNION:
6355 struct_decl(&type1, VT_STRUCT);
6356 goto basic_type2;
6358 /* type modifiers */
6359 case TOK_CONST1:
6360 case TOK_CONST2:
6361 case TOK_CONST3:
6362 t |= VT_CONSTANT;
6363 next();
6364 break;
6365 case TOK_VOLATILE1:
6366 case TOK_VOLATILE2:
6367 case TOK_VOLATILE3:
6368 t |= VT_VOLATILE;
6369 next();
6370 break;
6371 case TOK_SIGNED1:
6372 case TOK_SIGNED2:
6373 case TOK_SIGNED3:
6374 typespec_found = 1;
6375 t |= VT_SIGNED;
6376 next();
6377 break;
6378 case TOK_REGISTER:
6379 case TOK_AUTO:
6380 case TOK_RESTRICT1:
6381 case TOK_RESTRICT2:
6382 case TOK_RESTRICT3:
6383 next();
6384 break;
6385 case TOK_UNSIGNED:
6386 t |= VT_UNSIGNED;
6387 next();
6388 typespec_found = 1;
6389 break;
6391 /* storage */
6392 case TOK_EXTERN:
6393 t |= VT_EXTERN;
6394 next();
6395 break;
6396 case TOK_STATIC:
6397 t |= VT_STATIC;
6398 next();
6399 break;
6400 case TOK_TYPEDEF:
6401 t |= VT_TYPEDEF;
6402 next();
6403 break;
6404 case TOK_INLINE1:
6405 case TOK_INLINE2:
6406 case TOK_INLINE3:
6407 t |= VT_INLINE;
6408 next();
6409 break;
6411 /* GNUC attribute */
6412 case TOK_ATTRIBUTE1:
6413 case TOK_ATTRIBUTE2:
6414 parse_attribute(ad);
6415 break;
6416 /* GNUC typeof */
6417 case TOK_TYPEOF1:
6418 case TOK_TYPEOF2:
6419 case TOK_TYPEOF3:
6420 next();
6421 parse_expr_type(&type1);
6422 goto basic_type2;
6423 default:
6424 if (typespec_found)
6425 goto the_end;
6426 s = sym_find(tok);
6427 if (!s || !(s->type.t & VT_TYPEDEF))
6428 goto the_end;
6429 t |= (s->type.t & ~VT_TYPEDEF);
6430 type->ref = s->type.ref;
6431 next();
6432 break;
6434 type_found = 1;
6436 the_end:
6437 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
6438 error("signed and unsigned modifier");
6439 if (tcc_state->char_is_unsigned) {
6440 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
6441 t |= VT_UNSIGNED;
6443 t &= ~VT_SIGNED;
6445 /* long is never used as type */
6446 if ((t & VT_BTYPE) == VT_LONG)
6447 t = (t & ~VT_BTYPE) | VT_INT;
6448 type->t = t;
6449 return type_found;
6452 /* convert a function parameter type (array to pointer and function to
6453 function pointer) */
6454 static inline void convert_parameter_type(CType *pt)
6456 /* array must be transformed to pointer according to ANSI C */
6457 pt->t &= ~VT_ARRAY;
6458 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6459 mk_pointer(pt);
6463 static void post_type(CType *type, AttributeDef *ad)
6465 int n, l, t1;
6466 Sym **plast, *s, *first;
6467 AttributeDef ad1;
6468 CType pt;
6470 if (tok == '(') {
6471 /* function declaration */
6472 next();
6473 l = 0;
6474 first = NULL;
6475 plast = &first;
6476 while (tok != ')') {
6477 /* read param name and compute offset */
6478 if (l != FUNC_OLD) {
6479 if (!parse_btype(&pt, &ad1)) {
6480 if (l) {
6481 error("invalid type");
6482 } else {
6483 l = FUNC_OLD;
6484 goto old_proto;
6487 l = FUNC_NEW;
6488 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6489 break;
6490 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6491 if ((pt.t & VT_BTYPE) == VT_VOID)
6492 error("parameter declared as void");
6493 } else {
6494 old_proto:
6495 n = tok;
6496 pt.t = VT_INT;
6497 next();
6499 convert_parameter_type(&pt);
6500 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6501 *plast = s;
6502 plast = &s->next;
6503 if (tok == ',') {
6504 next();
6505 if (l == FUNC_NEW && tok == TOK_DOTS) {
6506 l = FUNC_ELLIPSIS;
6507 next();
6508 break;
6512 /* if no parameters, then old type prototype */
6513 if (l == 0)
6514 l = FUNC_OLD;
6515 skip(')');
6516 t1 = type->t & VT_STORAGE;
6517 /* NOTE: const is ignored in returned type as it has a special
6518 meaning in gcc / C++ */
6519 type->t &= ~(VT_STORAGE | VT_CONSTANT);
6520 post_type(type, ad);
6521 /* we push a anonymous symbol which will contain the function prototype */
6522 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6523 s->next = first;
6524 type->t = t1 | VT_FUNC;
6525 type->ref = s;
6526 } else if (tok == '[') {
6527 /* array definition */
6528 next();
6529 n = -1;
6530 if (tok != ']') {
6531 n = expr_const();
6532 if (n < 0)
6533 error("invalid array size");
6535 skip(']');
6536 /* parse next post type */
6537 t1 = type->t & VT_STORAGE;
6538 type->t &= ~VT_STORAGE;
6539 post_type(type, ad);
6541 /* we push a anonymous symbol which will contain the array
6542 element type */
6543 s = sym_push(SYM_FIELD, type, 0, n);
6544 type->t = t1 | VT_ARRAY | VT_PTR;
6545 type->ref = s;
6549 /* Parse a type declaration (except basic type), and return the type
6550 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6551 expected. 'type' should contain the basic type. 'ad' is the
6552 attribute definition of the basic type. It can be modified by
6553 type_decl().
6555 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6557 Sym *s;
6558 CType type1, *type2;
6559 int qualifiers;
6561 while (tok == '*') {
6562 qualifiers = 0;
6563 redo:
6564 next();
6565 switch(tok) {
6566 case TOK_CONST1:
6567 case TOK_CONST2:
6568 case TOK_CONST3:
6569 qualifiers |= VT_CONSTANT;
6570 goto redo;
6571 case TOK_VOLATILE1:
6572 case TOK_VOLATILE2:
6573 case TOK_VOLATILE3:
6574 qualifiers |= VT_VOLATILE;
6575 goto redo;
6576 case TOK_RESTRICT1:
6577 case TOK_RESTRICT2:
6578 case TOK_RESTRICT3:
6579 goto redo;
6581 mk_pointer(type);
6582 type->t |= qualifiers;
6585 /* XXX: clarify attribute handling */
6586 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6587 parse_attribute(ad);
6589 /* recursive type */
6590 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6591 type1.t = 0; /* XXX: same as int */
6592 if (tok == '(') {
6593 next();
6594 /* XXX: this is not correct to modify 'ad' at this point, but
6595 the syntax is not clear */
6596 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6597 parse_attribute(ad);
6598 type_decl(&type1, ad, v, td);
6599 skip(')');
6600 } else {
6601 /* type identifier */
6602 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6603 *v = tok;
6604 next();
6605 } else {
6606 if (!(td & TYPE_ABSTRACT))
6607 expect("identifier");
6608 *v = 0;
6611 post_type(type, ad);
6612 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6613 parse_attribute(ad);
6614 if (!type1.t)
6615 return;
6616 /* append type at the end of type1 */
6617 type2 = &type1;
6618 for(;;) {
6619 s = type2->ref;
6620 type2 = &s->type;
6621 if (!type2->t) {
6622 *type2 = *type;
6623 break;
6626 *type = type1;
6629 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6630 static int lvalue_type(int t)
6632 int bt, r;
6633 r = VT_LVAL;
6634 bt = t & VT_BTYPE;
6635 if (bt == VT_BYTE || bt == VT_BOOL)
6636 r |= VT_LVAL_BYTE;
6637 else if (bt == VT_SHORT)
6638 r |= VT_LVAL_SHORT;
6639 else
6640 return r;
6641 if (t & VT_UNSIGNED)
6642 r |= VT_LVAL_UNSIGNED;
6643 return r;
6646 /* indirection with full error checking and bound check */
6647 static void indir(void)
6649 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6650 expect("pointer");
6651 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6652 gv(RC_INT);
6653 vtop->type = *pointed_type(&vtop->type);
6654 /* an array is never an lvalue */
6655 if (!(vtop->type.t & VT_ARRAY)) {
6656 vtop->r |= lvalue_type(vtop->type.t);
6657 /* if bound checking, the referenced pointer must be checked */
6658 if (do_bounds_check)
6659 vtop->r |= VT_MUSTBOUND;
6663 /* pass a parameter to a function and do type checking and casting */
6664 static void gfunc_param_typed(Sym *func, Sym *arg)
6666 int func_type;
6667 CType type;
6669 func_type = func->c;
6670 if (func_type == FUNC_OLD ||
6671 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6672 /* default casting : only need to convert float to double */
6673 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6674 type.t = VT_DOUBLE;
6675 gen_cast(&type);
6677 } else if (arg == NULL) {
6678 error("too many arguments to function");
6679 } else {
6680 type = arg->type;
6681 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
6682 gen_assign_cast(&type);
6686 /* parse an expression of the form '(type)' or '(expr)' and return its
6687 type */
6688 static void parse_expr_type(CType *type)
6690 int n;
6691 AttributeDef ad;
6693 skip('(');
6694 if (parse_btype(type, &ad)) {
6695 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6696 } else {
6697 expr_type(type);
6699 skip(')');
6702 static void parse_type(CType *type)
6704 AttributeDef ad;
6705 int n;
6707 if (!parse_btype(type, &ad)) {
6708 expect("type");
6710 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6713 static void vpush_tokc(int t)
6715 CType type;
6716 type.t = t;
6717 vsetc(&type, VT_CONST, &tokc);
6720 static void unary(void)
6722 int n, t, align, size, r;
6723 CType type;
6724 Sym *s;
6725 AttributeDef ad;
6727 /* XXX: GCC 2.95.3 does not generate a table although it should be
6728 better here */
6729 tok_next:
6730 switch(tok) {
6731 case TOK_EXTENSION:
6732 next();
6733 goto tok_next;
6734 case TOK_CINT:
6735 case TOK_CCHAR:
6736 case TOK_LCHAR:
6737 vpushi(tokc.i);
6738 next();
6739 break;
6740 case TOK_CUINT:
6741 vpush_tokc(VT_INT | VT_UNSIGNED);
6742 next();
6743 break;
6744 case TOK_CLLONG:
6745 vpush_tokc(VT_LLONG);
6746 next();
6747 break;
6748 case TOK_CULLONG:
6749 vpush_tokc(VT_LLONG | VT_UNSIGNED);
6750 next();
6751 break;
6752 case TOK_CFLOAT:
6753 vpush_tokc(VT_FLOAT);
6754 next();
6755 break;
6756 case TOK_CDOUBLE:
6757 vpush_tokc(VT_DOUBLE);
6758 next();
6759 break;
6760 case TOK_CLDOUBLE:
6761 vpush_tokc(VT_LDOUBLE);
6762 next();
6763 break;
6764 case TOK___FUNCTION__:
6765 if (!gnu_ext)
6766 goto tok_identifier;
6767 /* fall thru */
6768 case TOK___FUNC__:
6770 void *ptr;
6771 int len;
6772 /* special function name identifier */
6773 len = strlen(funcname) + 1;
6774 /* generate char[len] type */
6775 type.t = VT_BYTE;
6776 mk_pointer(&type);
6777 type.t |= VT_ARRAY;
6778 type.ref->c = len;
6779 vpush_ref(&type, data_section, data_section->data_offset, len);
6780 ptr = section_ptr_add(data_section, len);
6781 memcpy(ptr, funcname, len);
6782 next();
6784 break;
6785 case TOK_LSTR:
6786 t = VT_INT;
6787 goto str_init;
6788 case TOK_STR:
6789 /* string parsing */
6790 t = VT_BYTE;
6791 str_init:
6792 if (tcc_state->warn_write_strings)
6793 t |= VT_CONSTANT;
6794 type.t = t;
6795 mk_pointer(&type);
6796 type.t |= VT_ARRAY;
6797 memset(&ad, 0, sizeof(AttributeDef));
6798 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
6799 break;
6800 case '(':
6801 next();
6802 /* cast ? */
6803 if (parse_btype(&type, &ad)) {
6804 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
6805 skip(')');
6806 /* check ISOC99 compound literal */
6807 if (tok == '{') {
6808 /* data is allocated locally by default */
6809 if (global_expr)
6810 r = VT_CONST;
6811 else
6812 r = VT_LOCAL;
6813 /* all except arrays are lvalues */
6814 if (!(type.t & VT_ARRAY))
6815 r |= lvalue_type(type.t);
6816 memset(&ad, 0, sizeof(AttributeDef));
6817 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
6818 } else {
6819 unary();
6820 gen_cast(&type);
6822 } else if (tok == '{') {
6823 /* save all registers */
6824 save_regs(0);
6825 /* statement expression : we do not accept break/continue
6826 inside as GCC does */
6827 block(NULL, NULL, NULL, NULL, 0, 1);
6828 skip(')');
6829 } else {
6830 gexpr();
6831 skip(')');
6833 break;
6834 case '*':
6835 next();
6836 unary();
6837 indir();
6838 break;
6839 case '&':
6840 next();
6841 unary();
6842 /* functions names must be treated as function pointers,
6843 except for unary '&' and sizeof. Since we consider that
6844 functions are not lvalues, we only have to handle it
6845 there and in function calls. */
6846 /* arrays can also be used although they are not lvalues */
6847 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
6848 !(vtop->type.t & VT_ARRAY))
6849 test_lvalue();
6850 mk_pointer(&vtop->type);
6851 gaddrof();
6852 break;
6853 case '!':
6854 next();
6855 unary();
6856 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
6857 vtop->c.i = !vtop->c.i;
6858 else if ((vtop->r & VT_VALMASK) == VT_CMP)
6859 vtop->c.i = vtop->c.i ^ 1;
6860 else
6861 vseti(VT_JMP, gtst(1, 0));
6862 break;
6863 case '~':
6864 next();
6865 unary();
6866 vpushi(-1);
6867 gen_op('^');
6868 break;
6869 case '+':
6870 next();
6871 /* in order to force cast, we add zero */
6872 unary();
6873 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
6874 error("pointer not accepted for unary plus");
6875 vpushi(0);
6876 gen_op('+');
6877 break;
6878 case TOK_SIZEOF:
6879 case TOK_ALIGNOF1:
6880 case TOK_ALIGNOF2:
6881 t = tok;
6882 next();
6883 if (tok == '(') {
6884 parse_expr_type(&type);
6885 } else {
6886 unary_type(&type);
6888 size = type_size(&type, &align);
6889 if (t == TOK_SIZEOF) {
6890 if (size < 0)
6891 error("sizeof applied to an incomplete type");
6892 vpushi(size);
6893 } else {
6894 vpushi(align);
6896 break;
6898 case TOK_builtin_types_compatible_p:
6900 CType type1, type2;
6901 next();
6902 skip('(');
6903 parse_type(&type1);
6904 skip(',');
6905 parse_type(&type2);
6906 skip(')');
6907 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6908 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6909 vpushi(is_compatible_types(&type1, &type2));
6911 break;
6912 case TOK_builtin_constant_p:
6914 int saved_nocode_wanted, res;
6915 next();
6916 skip('(');
6917 saved_nocode_wanted = nocode_wanted;
6918 nocode_wanted = 1;
6919 gexpr();
6920 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6921 vpop();
6922 nocode_wanted = saved_nocode_wanted;
6923 skip(')');
6924 vpushi(res);
6926 break;
6927 case TOK_INC:
6928 case TOK_DEC:
6929 t = tok;
6930 next();
6931 unary();
6932 inc(0, t);
6933 break;
6934 case '-':
6935 next();
6936 vpushi(0);
6937 unary();
6938 gen_op('-');
6939 break;
6940 case TOK_LAND:
6941 if (!gnu_ext)
6942 goto tok_identifier;
6943 next();
6944 /* allow to take the address of a label */
6945 if (tok < TOK_UIDENT)
6946 expect("label identifier");
6947 s = label_find(tok);
6948 if (!s) {
6949 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
6950 } else {
6951 if (s->r == LABEL_DECLARED)
6952 s->r = LABEL_FORWARD;
6954 if (!s->type.t) {
6955 s->type.t = VT_VOID;
6956 mk_pointer(&s->type);
6957 s->type.t |= VT_STATIC;
6959 vset(&s->type, VT_CONST | VT_SYM, 0);
6960 vtop->sym = s;
6961 next();
6962 break;
6963 default:
6964 tok_identifier:
6965 t = tok;
6966 next();
6967 if (t < TOK_UIDENT)
6968 expect("identifier");
6969 s = sym_find(t);
6970 if (!s) {
6971 if (tok != '(')
6972 error("'%s' undeclared", get_tok_str(t, NULL));
6973 /* for simple function calls, we tolerate undeclared
6974 external reference to int() function */
6975 if (tcc_state->warn_implicit_function_declaration)
6976 warning("implicit declaration of function '%s'",
6977 get_tok_str(t, NULL));
6978 s = external_global_sym(t, &func_old_type, 0);
6980 vset(&s->type, s->r, s->c);
6981 /* if forward reference, we must point to s */
6982 if (vtop->r & VT_SYM) {
6983 vtop->sym = s;
6984 vtop->c.ul = 0;
6986 break;
6989 /* post operations */
6990 while (1) {
6991 if (tok == TOK_INC || tok == TOK_DEC) {
6992 inc(1, tok);
6993 next();
6994 } else if (tok == '.' || tok == TOK_ARROW) {
6995 /* field */
6996 if (tok == TOK_ARROW)
6997 indir();
6998 test_lvalue();
6999 gaddrof();
7000 next();
7001 /* expect pointer on structure */
7002 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7003 expect("struct or union");
7004 s = vtop->type.ref;
7005 /* find field */
7006 tok |= SYM_FIELD;
7007 while ((s = s->next) != NULL) {
7008 if (s->v == tok)
7009 break;
7011 if (!s)
7012 error("field not found");
7013 /* add field offset to pointer */
7014 vtop->type = char_pointer_type; /* change type to 'char *' */
7015 vpushi(s->c);
7016 gen_op('+');
7017 /* change type to field type, and set to lvalue */
7018 vtop->type = s->type;
7019 /* an array is never an lvalue */
7020 if (!(vtop->type.t & VT_ARRAY)) {
7021 vtop->r |= lvalue_type(vtop->type.t);
7022 /* if bound checking, the referenced pointer must be checked */
7023 if (do_bounds_check)
7024 vtop->r |= VT_MUSTBOUND;
7026 next();
7027 } else if (tok == '[') {
7028 next();
7029 gexpr();
7030 gen_op('+');
7031 indir();
7032 skip(']');
7033 } else if (tok == '(') {
7034 SValue ret;
7035 Sym *sa;
7036 int nb_args;
7038 /* function call */
7039 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7040 /* pointer test (no array accepted) */
7041 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7042 vtop->type = *pointed_type(&vtop->type);
7043 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7044 goto error_func;
7045 } else {
7046 error_func:
7047 expect("function pointer");
7049 } else {
7050 vtop->r &= ~VT_LVAL; /* no lvalue */
7052 /* get return type */
7053 s = vtop->type.ref;
7054 next();
7055 sa = s->next; /* first parameter */
7056 nb_args = 0;
7057 /* compute first implicit argument if a structure is returned */
7058 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7059 /* get some space for the returned structure */
7060 size = type_size(&s->type, &align);
7061 loc = (loc - size) & -align;
7062 ret.type = s->type;
7063 ret.r = VT_LOCAL | VT_LVAL;
7064 /* pass it as 'int' to avoid structure arg passing
7065 problems */
7066 vseti(VT_LOCAL, loc);
7067 ret.c = vtop->c;
7068 nb_args++;
7069 } else {
7070 ret.type = s->type;
7071 ret.r2 = VT_CONST;
7072 /* return in register */
7073 if (is_float(ret.type.t)) {
7074 ret.r = REG_FRET;
7075 } else {
7076 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7077 ret.r2 = REG_LRET;
7078 ret.r = REG_IRET;
7080 ret.c.i = 0;
7082 if (tok != ')') {
7083 for(;;) {
7084 expr_eq();
7085 gfunc_param_typed(s, sa);
7086 nb_args++;
7087 if (sa)
7088 sa = sa->next;
7089 if (tok == ')')
7090 break;
7091 skip(',');
7094 if (sa)
7095 error("too few arguments to function");
7096 skip(')');
7097 if (!nocode_wanted) {
7098 gfunc_call(nb_args);
7099 } else {
7100 vtop -= (nb_args + 1);
7102 /* return value */
7103 vsetc(&ret.type, ret.r, &ret.c);
7104 vtop->r2 = ret.r2;
7105 } else {
7106 break;
7111 static void uneq(void)
7113 int t;
7115 unary();
7116 if (tok == '=' ||
7117 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7118 tok == TOK_A_XOR || tok == TOK_A_OR ||
7119 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7120 test_lvalue();
7121 t = tok;
7122 next();
7123 if (t == '=') {
7124 expr_eq();
7125 } else {
7126 vdup();
7127 expr_eq();
7128 gen_op(t & 0x7f);
7130 vstore();
7134 static void expr_prod(void)
7136 int t;
7138 uneq();
7139 while (tok == '*' || tok == '/' || tok == '%') {
7140 t = tok;
7141 next();
7142 uneq();
7143 gen_op(t);
7147 static void expr_sum(void)
7149 int t;
7151 expr_prod();
7152 while (tok == '+' || tok == '-') {
7153 t = tok;
7154 next();
7155 expr_prod();
7156 gen_op(t);
7160 static void expr_shift(void)
7162 int t;
7164 expr_sum();
7165 while (tok == TOK_SHL || tok == TOK_SAR) {
7166 t = tok;
7167 next();
7168 expr_sum();
7169 gen_op(t);
7173 static void expr_cmp(void)
7175 int t;
7177 expr_shift();
7178 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7179 tok == TOK_ULT || tok == TOK_UGE) {
7180 t = tok;
7181 next();
7182 expr_shift();
7183 gen_op(t);
7187 static void expr_cmpeq(void)
7189 int t;
7191 expr_cmp();
7192 while (tok == TOK_EQ || tok == TOK_NE) {
7193 t = tok;
7194 next();
7195 expr_cmp();
7196 gen_op(t);
7200 static void expr_and(void)
7202 expr_cmpeq();
7203 while (tok == '&') {
7204 next();
7205 expr_cmpeq();
7206 gen_op('&');
7210 static void expr_xor(void)
7212 expr_and();
7213 while (tok == '^') {
7214 next();
7215 expr_and();
7216 gen_op('^');
7220 static void expr_or(void)
7222 expr_xor();
7223 while (tok == '|') {
7224 next();
7225 expr_xor();
7226 gen_op('|');
7230 /* XXX: fix this mess */
7231 static void expr_land_const(void)
7233 expr_or();
7234 while (tok == TOK_LAND) {
7235 next();
7236 expr_or();
7237 gen_op(TOK_LAND);
7241 /* XXX: fix this mess */
7242 static void expr_lor_const(void)
7244 expr_land_const();
7245 while (tok == TOK_LOR) {
7246 next();
7247 expr_land_const();
7248 gen_op(TOK_LOR);
7252 /* only used if non constant */
7253 static void expr_land(void)
7255 int t;
7257 expr_or();
7258 if (tok == TOK_LAND) {
7259 t = 0;
7260 for(;;) {
7261 t = gtst(1, t);
7262 if (tok != TOK_LAND) {
7263 vseti(VT_JMPI, t);
7264 break;
7266 next();
7267 expr_or();
7272 static void expr_lor(void)
7274 int t;
7276 expr_land();
7277 if (tok == TOK_LOR) {
7278 t = 0;
7279 for(;;) {
7280 t = gtst(0, t);
7281 if (tok != TOK_LOR) {
7282 vseti(VT_JMP, t);
7283 break;
7285 next();
7286 expr_land();
7291 /* XXX: better constant handling */
7292 static void expr_eq(void)
7294 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7295 SValue sv;
7296 CType type, type1, type2;
7298 if (const_wanted) {
7299 int c1, c;
7300 expr_lor_const();
7301 if (tok == '?') {
7302 c = vtop->c.i;
7303 vpop();
7304 next();
7305 if (tok == ':' && gnu_ext) {
7306 c1 = c;
7307 } else {
7308 gexpr();
7309 c1 = vtop->c.i;
7310 vpop();
7312 skip(':');
7313 expr_eq();
7314 if (c)
7315 vtop->c.i = c1;
7317 } else {
7318 expr_lor();
7319 if (tok == '?') {
7320 next();
7321 if (vtop != vstack) {
7322 /* needed to avoid having different registers saved in
7323 each branch */
7324 if (is_float(vtop->type.t))
7325 rc = RC_FLOAT;
7326 else
7327 rc = RC_INT;
7328 gv(rc);
7329 save_regs(1);
7331 if (tok == ':' && gnu_ext) {
7332 gv_dup();
7333 tt = gtst(1, 0);
7334 } else {
7335 tt = gtst(1, 0);
7336 gexpr();
7338 type1 = vtop->type;
7339 sv = *vtop; /* save value to handle it later */
7340 vtop--; /* no vpop so that FP stack is not flushed */
7341 skip(':');
7342 u = gjmp(0);
7343 gsym(tt);
7344 expr_eq();
7345 type2 = vtop->type;
7347 t1 = type1.t;
7348 bt1 = t1 & VT_BTYPE;
7349 t2 = type2.t;
7350 bt2 = t2 & VT_BTYPE;
7351 /* cast operands to correct type according to ISOC rules */
7352 if (is_float(bt1) || is_float(bt2)) {
7353 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7354 type.t = VT_LDOUBLE;
7355 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7356 type.t = VT_DOUBLE;
7357 } else {
7358 type.t = VT_FLOAT;
7360 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7361 /* cast to biggest op */
7362 type.t = VT_LLONG;
7363 /* convert to unsigned if it does not fit in a long long */
7364 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7365 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7366 type.t |= VT_UNSIGNED;
7367 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7368 /* XXX: test pointer compatibility */
7369 type = type1;
7370 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7371 /* XXX: test structure compatibility */
7372 type = type1;
7373 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7374 /* NOTE: as an extension, we accept void on only one side */
7375 type.t = VT_VOID;
7376 } else {
7377 /* integer operations */
7378 type.t = VT_INT;
7379 /* convert to unsigned if it does not fit in an integer */
7380 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7381 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7382 type.t |= VT_UNSIGNED;
7385 /* now we convert second operand */
7386 gen_cast(&type);
7387 rc = RC_INT;
7388 if (is_float(type.t)) {
7389 rc = RC_FLOAT;
7390 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7391 /* for long longs, we use fixed registers to avoid having
7392 to handle a complicated move */
7393 rc = RC_IRET;
7396 r2 = gv(rc);
7397 /* this is horrible, but we must also convert first
7398 operand */
7399 tt = gjmp(0);
7400 gsym(u);
7401 /* put again first value and cast it */
7402 *vtop = sv;
7403 gen_cast(&type);
7404 r1 = gv(rc);
7405 move_reg(r2, r1);
7406 vtop->r = r2;
7407 gsym(tt);
7412 static void gexpr(void)
7414 while (1) {
7415 expr_eq();
7416 if (tok != ',')
7417 break;
7418 vpop();
7419 next();
7423 /* parse an expression and return its type without any side effect. */
7424 static void expr_type(CType *type)
7426 int saved_nocode_wanted;
7428 saved_nocode_wanted = nocode_wanted;
7429 nocode_wanted = 1;
7430 gexpr();
7431 *type = vtop->type;
7432 vpop();
7433 nocode_wanted = saved_nocode_wanted;
7436 /* parse a unary expression and return its type without any side
7437 effect. */
7438 static void unary_type(CType *type)
7440 int a;
7442 a = nocode_wanted;
7443 nocode_wanted = 1;
7444 unary();
7445 *type = vtop->type;
7446 vpop();
7447 nocode_wanted = a;
7450 /* parse a constant expression and return value in vtop. */
7451 static void expr_const1(void)
7453 int a;
7454 a = const_wanted;
7455 const_wanted = 1;
7456 expr_eq();
7457 const_wanted = a;
7460 /* parse an integer constant and return its value. */
7461 static int expr_const(void)
7463 int c;
7464 expr_const1();
7465 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7466 expect("constant expression");
7467 c = vtop->c.i;
7468 vpop();
7469 return c;
7472 /* return the label token if current token is a label, otherwise
7473 return zero */
7474 static int is_label(void)
7476 int last_tok;
7478 /* fast test first */
7479 if (tok < TOK_UIDENT)
7480 return 0;
7481 /* no need to save tokc because tok is an identifier */
7482 last_tok = tok;
7483 next();
7484 if (tok == ':') {
7485 next();
7486 return last_tok;
7487 } else {
7488 unget_tok(last_tok);
7489 return 0;
7493 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7494 int case_reg, int is_expr)
7496 int a, b, c, d;
7497 Sym *s;
7499 /* generate line number info */
7500 if (do_debug &&
7501 (last_line_num != file->line_num || last_ind != ind)) {
7502 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7503 last_ind = ind;
7504 last_line_num = file->line_num;
7507 if (is_expr) {
7508 /* default return value is (void) */
7509 vpushi(0);
7510 vtop->type.t = VT_VOID;
7513 if (tok == TOK_IF) {
7514 /* if test */
7515 next();
7516 skip('(');
7517 gexpr();
7518 skip(')');
7519 a = gtst(1, 0);
7520 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7521 c = tok;
7522 if (c == TOK_ELSE) {
7523 next();
7524 d = gjmp(0);
7525 gsym(a);
7526 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7527 gsym(d); /* patch else jmp */
7528 } else
7529 gsym(a);
7530 } else if (tok == TOK_WHILE) {
7531 next();
7532 d = ind;
7533 skip('(');
7534 gexpr();
7535 skip(')');
7536 a = gtst(1, 0);
7537 b = 0;
7538 block(&a, &b, case_sym, def_sym, case_reg, 0);
7539 gjmp_addr(d);
7540 gsym(a);
7541 gsym_addr(b, d);
7542 } else if (tok == '{') {
7543 Sym *llabel;
7545 next();
7546 /* record local declaration stack position */
7547 s = local_stack;
7548 llabel = local_label_stack;
7549 /* handle local labels declarations */
7550 if (tok == TOK_LABEL) {
7551 next();
7552 for(;;) {
7553 if (tok < TOK_UIDENT)
7554 expect("label identifier");
7555 label_push(&local_label_stack, tok, LABEL_DECLARED);
7556 next();
7557 if (tok == ',') {
7558 next();
7559 } else {
7560 skip(';');
7561 break;
7565 while (tok != '}') {
7566 decl(VT_LOCAL);
7567 if (tok != '}') {
7568 if (is_expr)
7569 vpop();
7570 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7573 /* pop locally defined labels */
7574 label_pop(&local_label_stack, llabel);
7575 /* pop locally defined symbols */
7576 sym_pop(&local_stack, s);
7577 next();
7578 } else if (tok == TOK_RETURN) {
7579 next();
7580 if (tok != ';') {
7581 gexpr();
7582 gen_assign_cast(&func_vt);
7583 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7584 CType type;
7585 /* if returning structure, must copy it to implicit
7586 first pointer arg location */
7587 type = func_vt;
7588 mk_pointer(&type);
7589 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7590 indir();
7591 vswap();
7592 /* copy structure value to pointer */
7593 vstore();
7594 } else if (is_float(func_vt.t)) {
7595 gv(RC_FRET);
7596 } else {
7597 gv(RC_IRET);
7599 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7601 skip(';');
7602 rsym = gjmp(rsym); /* jmp */
7603 } else if (tok == TOK_BREAK) {
7604 /* compute jump */
7605 if (!bsym)
7606 error("cannot break");
7607 *bsym = gjmp(*bsym);
7608 next();
7609 skip(';');
7610 } else if (tok == TOK_CONTINUE) {
7611 /* compute jump */
7612 if (!csym)
7613 error("cannot continue");
7614 *csym = gjmp(*csym);
7615 next();
7616 skip(';');
7617 } else if (tok == TOK_FOR) {
7618 int e;
7619 next();
7620 skip('(');
7621 if (tok != ';') {
7622 gexpr();
7623 vpop();
7625 skip(';');
7626 d = ind;
7627 c = ind;
7628 a = 0;
7629 b = 0;
7630 if (tok != ';') {
7631 gexpr();
7632 a = gtst(1, 0);
7634 skip(';');
7635 if (tok != ')') {
7636 e = gjmp(0);
7637 c = ind;
7638 gexpr();
7639 vpop();
7640 gjmp_addr(d);
7641 gsym(e);
7643 skip(')');
7644 block(&a, &b, case_sym, def_sym, case_reg, 0);
7645 gjmp_addr(c);
7646 gsym(a);
7647 gsym_addr(b, c);
7648 } else
7649 if (tok == TOK_DO) {
7650 next();
7651 a = 0;
7652 b = 0;
7653 d = ind;
7654 block(&a, &b, case_sym, def_sym, case_reg, 0);
7655 skip(TOK_WHILE);
7656 skip('(');
7657 gsym(b);
7658 gexpr();
7659 c = gtst(0, 0);
7660 gsym_addr(c, d);
7661 skip(')');
7662 gsym(a);
7663 skip(';');
7664 } else
7665 if (tok == TOK_SWITCH) {
7666 next();
7667 skip('(');
7668 gexpr();
7669 /* XXX: other types than integer */
7670 case_reg = gv(RC_INT);
7671 vpop();
7672 skip(')');
7673 a = 0;
7674 b = gjmp(0); /* jump to first case */
7675 c = 0;
7676 block(&a, csym, &b, &c, case_reg, 0);
7677 /* if no default, jmp after switch */
7678 if (c == 0)
7679 c = ind;
7680 /* default label */
7681 gsym_addr(b, c);
7682 /* break label */
7683 gsym(a);
7684 } else
7685 if (tok == TOK_CASE) {
7686 int v1, v2;
7687 if (!case_sym)
7688 expect("switch");
7689 next();
7690 v1 = expr_const();
7691 v2 = v1;
7692 if (gnu_ext && tok == TOK_DOTS) {
7693 next();
7694 v2 = expr_const();
7695 if (v2 < v1)
7696 warning("empty case range");
7698 /* since a case is like a label, we must skip it with a jmp */
7699 b = gjmp(0);
7700 gsym(*case_sym);
7701 vseti(case_reg, 0);
7702 vpushi(v1);
7703 if (v1 == v2) {
7704 gen_op(TOK_EQ);
7705 *case_sym = gtst(1, 0);
7706 } else {
7707 gen_op(TOK_GE);
7708 *case_sym = gtst(1, 0);
7709 vseti(case_reg, 0);
7710 vpushi(v2);
7711 gen_op(TOK_LE);
7712 *case_sym = gtst(1, *case_sym);
7714 gsym(b);
7715 skip(':');
7716 is_expr = 0;
7717 goto block_after_label;
7718 } else
7719 if (tok == TOK_DEFAULT) {
7720 next();
7721 skip(':');
7722 if (!def_sym)
7723 expect("switch");
7724 if (*def_sym)
7725 error("too many 'default'");
7726 *def_sym = ind;
7727 is_expr = 0;
7728 goto block_after_label;
7729 } else
7730 if (tok == TOK_GOTO) {
7731 next();
7732 if (tok == '*' && gnu_ext) {
7733 /* computed goto */
7734 next();
7735 gexpr();
7736 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7737 expect("pointer");
7738 ggoto();
7739 } else if (tok >= TOK_UIDENT) {
7740 s = label_find(tok);
7741 /* put forward definition if needed */
7742 if (!s) {
7743 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7744 } else {
7745 if (s->r == LABEL_DECLARED)
7746 s->r = LABEL_FORWARD;
7748 /* label already defined */
7749 if (s->r & LABEL_FORWARD)
7750 s->next = (void *)gjmp((long)s->next);
7751 else
7752 gjmp_addr((long)s->next);
7753 next();
7754 } else {
7755 expect("label identifier");
7757 skip(';');
7758 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7759 asm_instr();
7760 } else {
7761 b = is_label();
7762 if (b) {
7763 /* label case */
7764 s = label_find(b);
7765 if (s) {
7766 if (s->r == LABEL_DEFINED)
7767 error("duplicate label '%s'", get_tok_str(s->v, NULL));
7768 gsym((long)s->next);
7769 s->r = LABEL_DEFINED;
7770 } else {
7771 s = label_push(&global_label_stack, b, LABEL_DEFINED);
7773 s->next = (void *)ind;
7774 /* we accept this, but it is a mistake */
7775 block_after_label:
7776 if (tok == '}') {
7777 warning("deprecated use of label at end of compound statement");
7778 } else {
7779 if (is_expr)
7780 vpop();
7781 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7783 } else {
7784 /* expression case */
7785 if (tok != ';') {
7786 if (is_expr) {
7787 vpop();
7788 gexpr();
7789 } else {
7790 gexpr();
7791 vpop();
7794 skip(';');
7799 /* t is the array or struct type. c is the array or struct
7800 address. cur_index/cur_field is the pointer to the current
7801 value. 'size_only' is true if only size info is needed (only used
7802 in arrays) */
7803 static void decl_designator(CType *type, Section *sec, unsigned long c,
7804 int *cur_index, Sym **cur_field,
7805 int size_only)
7807 Sym *s, *f;
7808 int notfirst, index, index_last, align, l, nb_elems, elem_size;
7809 CType type1;
7811 notfirst = 0;
7812 elem_size = 0;
7813 nb_elems = 1;
7814 if (gnu_ext && (l = is_label()) != 0)
7815 goto struct_field;
7816 while (tok == '[' || tok == '.') {
7817 if (tok == '[') {
7818 if (!(type->t & VT_ARRAY))
7819 expect("array type");
7820 s = type->ref;
7821 next();
7822 index = expr_const();
7823 if (index < 0 || (s->c >= 0 && index >= s->c))
7824 expect("invalid index");
7825 if (tok == TOK_DOTS && gnu_ext) {
7826 next();
7827 index_last = expr_const();
7828 if (index_last < 0 ||
7829 (s->c >= 0 && index_last >= s->c) ||
7830 index_last < index)
7831 expect("invalid index");
7832 } else {
7833 index_last = index;
7835 skip(']');
7836 if (!notfirst)
7837 *cur_index = index_last;
7838 type = pointed_type(type);
7839 elem_size = type_size(type, &align);
7840 c += index * elem_size;
7841 /* NOTE: we only support ranges for last designator */
7842 nb_elems = index_last - index + 1;
7843 if (nb_elems != 1) {
7844 notfirst = 1;
7845 break;
7847 } else {
7848 next();
7849 l = tok;
7850 next();
7851 struct_field:
7852 if ((type->t & VT_BTYPE) != VT_STRUCT)
7853 expect("struct/union type");
7854 s = type->ref;
7855 l |= SYM_FIELD;
7856 f = s->next;
7857 while (f) {
7858 if (f->v == l)
7859 break;
7860 f = f->next;
7862 if (!f)
7863 expect("field");
7864 if (!notfirst)
7865 *cur_field = f;
7866 /* XXX: fix this mess by using explicit storage field */
7867 type1 = f->type;
7868 type1.t |= (type->t & ~VT_TYPE);
7869 type = &type1;
7870 c += f->c;
7872 notfirst = 1;
7874 if (notfirst) {
7875 if (tok == '=') {
7876 next();
7877 } else {
7878 if (!gnu_ext)
7879 expect("=");
7881 } else {
7882 if (type->t & VT_ARRAY) {
7883 index = *cur_index;
7884 type = pointed_type(type);
7885 c += index * type_size(type, &align);
7886 } else {
7887 f = *cur_field;
7888 if (!f)
7889 error("too many field init");
7890 /* XXX: fix this mess by using explicit storage field */
7891 type1 = f->type;
7892 type1.t |= (type->t & ~VT_TYPE);
7893 type = &type1;
7894 c += f->c;
7897 decl_initializer(type, sec, c, 0, size_only);
7899 /* XXX: make it more general */
7900 if (!size_only && nb_elems > 1) {
7901 unsigned long c_end;
7902 uint8_t *src, *dst;
7903 int i;
7905 if (!sec)
7906 error("range init not supported yet for dynamic storage");
7907 c_end = c + nb_elems * elem_size;
7908 if (c_end > sec->data_allocated)
7909 section_realloc(sec, c_end);
7910 src = sec->data + c;
7911 dst = src;
7912 for(i = 1; i < nb_elems; i++) {
7913 dst += elem_size;
7914 memcpy(dst, src, elem_size);
7919 #define EXPR_VAL 0
7920 #define EXPR_CONST 1
7921 #define EXPR_ANY 2
7923 /* store a value or an expression directly in global data or in local array */
7924 static void init_putv(CType *type, Section *sec, unsigned long c,
7925 int v, int expr_type)
7927 int saved_global_expr, bt, bit_pos, bit_size;
7928 void *ptr;
7929 unsigned long long bit_mask;
7930 CType dtype;
7932 switch(expr_type) {
7933 case EXPR_VAL:
7934 vpushi(v);
7935 break;
7936 case EXPR_CONST:
7937 /* compound literals must be allocated globally in this case */
7938 saved_global_expr = global_expr;
7939 global_expr = 1;
7940 expr_const1();
7941 global_expr = saved_global_expr;
7942 /* NOTE: symbols are accepted */
7943 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
7944 error("initializer element is not constant");
7945 break;
7946 case EXPR_ANY:
7947 expr_eq();
7948 break;
7951 dtype = *type;
7952 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7954 if (sec) {
7955 /* XXX: not portable */
7956 /* XXX: generate error if incorrect relocation */
7957 gen_assign_cast(&dtype);
7958 bt = type->t & VT_BTYPE;
7959 ptr = sec->data + c;
7960 /* XXX: make code faster ? */
7961 if (!(type->t & VT_BITFIELD)) {
7962 bit_pos = 0;
7963 bit_size = 32;
7964 bit_mask = -1LL;
7965 } else {
7966 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
7967 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
7968 bit_mask = (1LL << bit_size) - 1;
7970 if ((vtop->r & VT_SYM) &&
7971 (bt == VT_BYTE ||
7972 bt == VT_SHORT ||
7973 bt == VT_DOUBLE ||
7974 bt == VT_LDOUBLE ||
7975 bt == VT_LLONG ||
7976 (bt == VT_INT && bit_size != 32)))
7977 error("initializer element is not computable at load time");
7978 switch(bt) {
7979 case VT_BYTE:
7980 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7981 break;
7982 case VT_SHORT:
7983 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7984 break;
7985 case VT_DOUBLE:
7986 *(double *)ptr = vtop->c.d;
7987 break;
7988 case VT_LDOUBLE:
7989 *(long double *)ptr = vtop->c.ld;
7990 break;
7991 case VT_LLONG:
7992 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
7993 break;
7994 default:
7995 if (vtop->r & VT_SYM) {
7996 greloc(sec, vtop->sym, c, R_DATA_32);
7998 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7999 break;
8001 vtop--;
8002 } else {
8003 vset(&dtype, VT_LOCAL, c);
8004 vswap();
8005 vstore();
8006 vpop();
8010 /* put zeros for variable based init */
8011 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8013 if (sec) {
8014 /* nothing to do because globals are already set to zero */
8015 } else {
8016 vpush_global_sym(&func_old_type, TOK_memset);
8017 vseti(VT_LOCAL, c);
8018 vpushi(0);
8019 vpushi(size);
8020 gfunc_call(3);
8024 /* 't' contains the type and storage info. 'c' is the offset of the
8025 object in section 'sec'. If 'sec' is NULL, it means stack based
8026 allocation. 'first' is true if array '{' must be read (multi
8027 dimension implicit array init handling). 'size_only' is true if
8028 size only evaluation is wanted (only for arrays). */
8029 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8030 int first, int size_only)
8032 int index, array_length, n, no_oblock, nb, parlevel, i;
8033 int size1, align1, expr_type;
8034 Sym *s, *f;
8035 CType *t1;
8037 if (type->t & VT_ARRAY) {
8038 s = type->ref;
8039 n = s->c;
8040 array_length = 0;
8041 t1 = pointed_type(type);
8042 size1 = type_size(t1, &align1);
8044 no_oblock = 1;
8045 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8046 tok == '{') {
8047 skip('{');
8048 no_oblock = 0;
8051 /* only parse strings here if correct type (otherwise: handle
8052 them as ((w)char *) expressions */
8053 if ((tok == TOK_LSTR &&
8054 (t1->t & VT_BTYPE) == VT_INT) ||
8055 (tok == TOK_STR &&
8056 (t1->t & VT_BTYPE) == VT_BYTE)) {
8057 while (tok == TOK_STR || tok == TOK_LSTR) {
8058 int cstr_len, ch;
8059 CString *cstr;
8061 cstr = tokc.cstr;
8062 /* compute maximum number of chars wanted */
8063 if (tok == TOK_STR)
8064 cstr_len = cstr->size;
8065 else
8066 cstr_len = cstr->size / sizeof(int);
8067 cstr_len--;
8068 nb = cstr_len;
8069 if (n >= 0 && nb > (n - array_length))
8070 nb = n - array_length;
8071 if (!size_only) {
8072 if (cstr_len > nb)
8073 warning("initializer-string for array is too long");
8074 /* in order to go faster for common case (char
8075 string in global variable, we handle it
8076 specifically */
8077 if (sec && tok == TOK_STR && size1 == 1) {
8078 memcpy(sec->data + c + array_length, cstr->data, nb);
8079 } else {
8080 for(i=0;i<nb;i++) {
8081 if (tok == TOK_STR)
8082 ch = ((unsigned char *)cstr->data)[i];
8083 else
8084 ch = ((int *)cstr->data)[i];
8085 init_putv(t1, sec, c + (array_length + i) * size1,
8086 ch, EXPR_VAL);
8090 array_length += nb;
8091 next();
8093 /* only add trailing zero if enough storage (no
8094 warning in this case since it is standard) */
8095 if (n < 0 || array_length < n) {
8096 if (!size_only) {
8097 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8099 array_length++;
8101 } else {
8102 index = 0;
8103 while (tok != '}') {
8104 decl_designator(type, sec, c, &index, NULL, size_only);
8105 if (n >= 0 && index >= n)
8106 error("index too large");
8107 /* must put zero in holes (note that doing it that way
8108 ensures that it even works with designators) */
8109 if (!size_only && array_length < index) {
8110 init_putz(t1, sec, c + array_length * size1,
8111 (index - array_length) * size1);
8113 index++;
8114 if (index > array_length)
8115 array_length = index;
8116 /* special test for multi dimensional arrays (may not
8117 be strictly correct if designators are used at the
8118 same time) */
8119 if (index >= n && no_oblock)
8120 break;
8121 if (tok == '}')
8122 break;
8123 skip(',');
8126 if (!no_oblock)
8127 skip('}');
8128 /* put zeros at the end */
8129 if (!size_only && n >= 0 && array_length < n) {
8130 init_putz(t1, sec, c + array_length * size1,
8131 (n - array_length) * size1);
8133 /* patch type size if needed */
8134 if (n < 0)
8135 s->c = array_length;
8136 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8137 (sec || !first || tok == '{')) {
8138 int par_count;
8140 /* NOTE: the previous test is a specific case for automatic
8141 struct/union init */
8142 /* XXX: union needs only one init */
8144 /* XXX: this test is incorrect for local initializers
8145 beginning with ( without {. It would be much more difficult
8146 to do it correctly (ideally, the expression parser should
8147 be used in all cases) */
8148 par_count = 0;
8149 if (tok == '(') {
8150 AttributeDef ad1;
8151 CType type1;
8152 next();
8153 while (tok == '(') {
8154 par_count++;
8155 next();
8157 if (!parse_btype(&type1, &ad1))
8158 expect("cast");
8159 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8160 #if 0
8161 if (!is_assignable_types(type, &type1))
8162 error("invalid type for cast");
8163 #endif
8164 skip(')');
8166 no_oblock = 1;
8167 if (first || tok == '{') {
8168 skip('{');
8169 no_oblock = 0;
8171 s = type->ref;
8172 f = s->next;
8173 array_length = 0;
8174 index = 0;
8175 n = s->c;
8176 while (tok != '}') {
8177 decl_designator(type, sec, c, NULL, &f, size_only);
8178 index = f->c;
8179 if (!size_only && array_length < index) {
8180 init_putz(type, sec, c + array_length,
8181 index - array_length);
8183 index = index + type_size(&f->type, &align1);
8184 if (index > array_length)
8185 array_length = index;
8186 f = f->next;
8187 if (no_oblock && f == NULL)
8188 break;
8189 if (tok == '}')
8190 break;
8191 skip(',');
8193 /* put zeros at the end */
8194 if (!size_only && array_length < n) {
8195 init_putz(type, sec, c + array_length,
8196 n - array_length);
8198 if (!no_oblock)
8199 skip('}');
8200 while (par_count) {
8201 skip(')');
8202 par_count--;
8204 } else if (tok == '{') {
8205 next();
8206 decl_initializer(type, sec, c, first, size_only);
8207 skip('}');
8208 } else if (size_only) {
8209 /* just skip expression */
8210 parlevel = 0;
8211 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8212 tok != -1) {
8213 if (tok == '(')
8214 parlevel++;
8215 else if (tok == ')')
8216 parlevel--;
8217 next();
8219 } else {
8220 /* currently, we always use constant expression for globals
8221 (may change for scripting case) */
8222 expr_type = EXPR_CONST;
8223 if (!sec)
8224 expr_type = EXPR_ANY;
8225 init_putv(type, sec, c, 0, expr_type);
8229 /* parse an initializer for type 't' if 'has_init' is non zero, and
8230 allocate space in local or global data space ('r' is either
8231 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8232 variable 'v' of scope 'scope' is declared before initializers are
8233 parsed. If 'v' is zero, then a reference to the new object is put
8234 in the value stack. If 'has_init' is 2, a special parsing is done
8235 to handle string constants. */
8236 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8237 int has_init, int v, int scope)
8239 int size, align, addr, data_offset;
8240 int level;
8241 ParseState saved_parse_state;
8242 TokenString init_str;
8243 Section *sec;
8245 size = type_size(type, &align);
8246 /* If unknown size, we must evaluate it before
8247 evaluating initializers because
8248 initializers can generate global data too
8249 (e.g. string pointers or ISOC99 compound
8250 literals). It also simplifies local
8251 initializers handling */
8252 tok_str_new(&init_str);
8253 if (size < 0) {
8254 if (!has_init)
8255 error("unknown type size");
8256 /* get all init string */
8257 if (has_init == 2) {
8258 /* only get strings */
8259 while (tok == TOK_STR || tok == TOK_LSTR) {
8260 tok_str_add_tok(&init_str);
8261 next();
8263 } else {
8264 level = 0;
8265 while (level > 0 || (tok != ',' && tok != ';')) {
8266 if (tok < 0)
8267 error("unexpected end of file in initializer");
8268 tok_str_add_tok(&init_str);
8269 if (tok == '{')
8270 level++;
8271 else if (tok == '}') {
8272 if (level == 0)
8273 break;
8274 level--;
8276 next();
8279 tok_str_add(&init_str, -1);
8280 tok_str_add(&init_str, 0);
8282 /* compute size */
8283 save_parse_state(&saved_parse_state);
8285 macro_ptr = init_str.str;
8286 next();
8287 decl_initializer(type, NULL, 0, 1, 1);
8288 /* prepare second initializer parsing */
8289 macro_ptr = init_str.str;
8290 next();
8292 /* if still unknown size, error */
8293 size = type_size(type, &align);
8294 if (size < 0)
8295 error("unknown type size");
8297 /* take into account specified alignment if bigger */
8298 if (ad->aligned > align)
8299 align = ad->aligned;
8300 if ((r & VT_VALMASK) == VT_LOCAL) {
8301 sec = NULL;
8302 if (do_bounds_check && (type->t & VT_ARRAY))
8303 loc--;
8304 loc = (loc - size) & -align;
8305 addr = loc;
8306 /* handles bounds */
8307 /* XXX: currently, since we do only one pass, we cannot track
8308 '&' operators, so we add only arrays */
8309 if (do_bounds_check && (type->t & VT_ARRAY)) {
8310 unsigned long *bounds_ptr;
8311 /* add padding between regions */
8312 loc--;
8313 /* then add local bound info */
8314 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8315 bounds_ptr[0] = addr;
8316 bounds_ptr[1] = size;
8318 if (v) {
8319 /* local variable */
8320 sym_push(v, type, r, addr);
8321 } else {
8322 /* push local reference */
8323 vset(type, r, addr);
8325 } else {
8326 Sym *sym;
8328 sym = NULL;
8329 if (v && scope == VT_CONST) {
8330 /* see if the symbol was already defined */
8331 sym = sym_find(v);
8332 if (sym) {
8333 if (!is_compatible_types(&sym->type, type))
8334 error("incompatible types for redefinition of '%s'",
8335 get_tok_str(v, NULL));
8336 if (sym->type.t & VT_EXTERN) {
8337 /* if the variable is extern, it was not allocated */
8338 sym->type.t &= ~VT_EXTERN;
8339 } else {
8340 /* we accept several definitions of the same
8341 global variable. this is tricky, because we
8342 must play with the SHN_COMMON type of the symbol */
8343 /* XXX: should check if the variable was already
8344 initialized. It is incorrect to initialized it
8345 twice */
8346 /* no init data, we won't add more to the symbol */
8347 if (!has_init)
8348 goto no_alloc;
8353 /* allocate symbol in corresponding section */
8354 sec = ad->section;
8355 if (!sec) {
8356 if (has_init)
8357 sec = data_section;
8359 if (sec) {
8360 data_offset = sec->data_offset;
8361 data_offset = (data_offset + align - 1) & -align;
8362 addr = data_offset;
8363 /* very important to increment global pointer at this time
8364 because initializers themselves can create new initializers */
8365 data_offset += size;
8366 /* add padding if bound check */
8367 if (do_bounds_check)
8368 data_offset++;
8369 sec->data_offset = data_offset;
8370 /* allocate section space to put the data */
8371 if (sec->sh_type != SHT_NOBITS &&
8372 data_offset > sec->data_allocated)
8373 section_realloc(sec, data_offset);
8374 } else {
8375 addr = 0; /* avoid warning */
8378 if (v) {
8379 if (scope == VT_CONST) {
8380 if (!sym)
8381 goto do_def;
8382 } else {
8383 do_def:
8384 sym = sym_push(v, type, r | VT_SYM, 0);
8386 /* update symbol definition */
8387 if (sec) {
8388 put_extern_sym(sym, sec, addr, size);
8389 } else {
8390 Elf32_Sym *esym;
8391 /* put a common area */
8392 put_extern_sym(sym, NULL, align, size);
8393 /* XXX: find a nicer way */
8394 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8395 esym->st_shndx = SHN_COMMON;
8397 } else {
8398 CValue cval;
8400 /* push global reference */
8401 sym = get_sym_ref(type, sec, addr, size);
8402 cval.ul = 0;
8403 vsetc(type, VT_CONST | VT_SYM, &cval);
8404 vtop->sym = sym;
8407 /* handles bounds now because the symbol must be defined
8408 before for the relocation */
8409 if (do_bounds_check) {
8410 unsigned long *bounds_ptr;
8412 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8413 /* then add global bound info */
8414 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8415 bounds_ptr[0] = 0; /* relocated */
8416 bounds_ptr[1] = size;
8419 if (has_init) {
8420 decl_initializer(type, sec, addr, 1, 0);
8421 /* restore parse state if needed */
8422 if (init_str.str) {
8423 tok_str_free(init_str.str);
8424 restore_parse_state(&saved_parse_state);
8427 no_alloc: ;
8430 void put_func_debug(Sym *sym)
8432 char buf[512];
8434 /* stabs info */
8435 /* XXX: we put here a dummy type */
8436 snprintf(buf, sizeof(buf), "%s:%c1",
8437 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8438 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8439 cur_text_section, sym->c);
8440 last_ind = 0;
8441 last_line_num = 0;
8444 /* not finished : try to put some local vars in registers */
8445 //#define CONFIG_REG_VARS
8447 #ifdef CONFIG_REG_VARS
8448 void add_var_ref(int t)
8450 printf("%s:%d: &%s\n",
8451 file->filename, file->line_num,
8452 get_tok_str(t, NULL));
8455 /* first pass on a function with heuristic to extract variable usage
8456 and pointer references to local variables for register allocation */
8457 void analyse_function(void)
8459 int level, t;
8461 for(;;) {
8462 if (tok == -1)
8463 break;
8464 /* any symbol coming after '&' is considered as being a
8465 variable whose reference is taken. It is highly unaccurate
8466 but it is difficult to do better without a complete parse */
8467 if (tok == '&') {
8468 next();
8469 /* if '& number', then no need to examine next tokens */
8470 if (tok == TOK_CINT ||
8471 tok == TOK_CUINT ||
8472 tok == TOK_CLLONG ||
8473 tok == TOK_CULLONG) {
8474 continue;
8475 } else if (tok >= TOK_UIDENT) {
8476 /* if '& ident [' or '& ident ->', then ident address
8477 is not needed */
8478 t = tok;
8479 next();
8480 if (tok != '[' && tok != TOK_ARROW)
8481 add_var_ref(t);
8482 } else {
8483 level = 0;
8484 while (tok != '}' && tok != ';' &&
8485 !((tok == ',' || tok == ')') && level == 0)) {
8486 if (tok >= TOK_UIDENT) {
8487 add_var_ref(tok);
8488 } else if (tok == '(') {
8489 level++;
8490 } else if (tok == ')') {
8491 level--;
8493 next();
8496 } else {
8497 next();
8501 #endif
8503 /* parse an old style function declaration list */
8504 /* XXX: check multiple parameter */
8505 static void func_decl_list(Sym *func_sym)
8507 AttributeDef ad;
8508 int v;
8509 Sym *s;
8510 CType btype, type;
8512 /* parse each declaration */
8513 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8514 if (!parse_btype(&btype, &ad))
8515 expect("declaration list");
8516 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8517 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8518 tok == ';') {
8519 /* we accept no variable after */
8520 } else {
8521 for(;;) {
8522 type = btype;
8523 type_decl(&type, &ad, &v, TYPE_DIRECT);
8524 /* find parameter in function parameter list */
8525 s = func_sym->next;
8526 while (s != NULL) {
8527 if ((s->v & ~SYM_FIELD) == v)
8528 goto found;
8529 s = s->next;
8531 error("declaration for parameter '%s' but no such parameter",
8532 get_tok_str(v, NULL));
8533 found:
8534 /* check that no storage specifier except 'register' was given */
8535 if (type.t & VT_STORAGE)
8536 error("storage class specified for '%s'", get_tok_str(v, NULL));
8537 convert_parameter_type(&type);
8538 /* we can add the type (NOTE: it could be local to the function) */
8539 s->type = type;
8540 /* accept other parameters */
8541 if (tok == ',')
8542 next();
8543 else
8544 break;
8547 skip(';');
8551 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8552 static void decl(int l)
8554 int v, has_init, r;
8555 CType type, btype;
8556 Sym *sym;
8557 AttributeDef ad;
8559 while (1) {
8560 if (!parse_btype(&btype, &ad)) {
8561 /* skip redundant ';' */
8562 /* XXX: find more elegant solution */
8563 if (tok == ';') {
8564 next();
8565 continue;
8567 /* special test for old K&R protos without explicit int
8568 type. Only accepted when defining global data */
8569 if (l == VT_LOCAL || tok < TOK_DEFINE)
8570 break;
8571 btype.t = VT_INT;
8573 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8574 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8575 tok == ';') {
8576 /* we accept no variable after */
8577 next();
8578 continue;
8580 while (1) { /* iterate thru each declaration */
8581 type = btype;
8582 type_decl(&type, &ad, &v, TYPE_DIRECT);
8583 #if 0
8585 char buf[500];
8586 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8587 printf("type = '%s'\n", buf);
8589 #endif
8590 if ((type.t & VT_BTYPE) == VT_FUNC) {
8591 /* if old style function prototype, we accept a
8592 declaration list */
8593 sym = type.ref;
8594 if (sym->c == FUNC_OLD)
8595 func_decl_list(sym);
8598 if (tok == '{') {
8599 #ifdef CONFIG_REG_VARS
8600 TokenString func_str;
8601 ParseState saved_parse_state;
8602 int block_level;
8603 #endif
8605 if (l == VT_LOCAL)
8606 error("cannot use local functions");
8607 if (!(type.t & VT_FUNC))
8608 expect("function definition");
8610 /* reject abstract declarators in function definition */
8611 sym = type.ref;
8612 while ((sym = sym->next) != NULL)
8613 if (!(sym->v & ~SYM_FIELD))
8614 expect("identifier");
8616 /* XXX: cannot do better now: convert extern line to static inline */
8617 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8618 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8620 #ifdef CONFIG_REG_VARS
8621 /* parse all function code and record it */
8623 tok_str_new(&func_str);
8625 block_level = 0;
8626 for(;;) {
8627 int t;
8628 if (tok == -1)
8629 error("unexpected end of file");
8630 tok_str_add_tok(&func_str);
8631 t = tok;
8632 next();
8633 if (t == '{') {
8634 block_level++;
8635 } else if (t == '}') {
8636 block_level--;
8637 if (block_level == 0)
8638 break;
8641 tok_str_add(&func_str, -1);
8642 tok_str_add(&func_str, 0);
8644 save_parse_state(&saved_parse_state);
8646 macro_ptr = func_str.str;
8647 next();
8648 analyse_function();
8649 #endif
8651 /* compute text section */
8652 cur_text_section = ad.section;
8653 if (!cur_text_section)
8654 cur_text_section = text_section;
8655 ind = cur_text_section->data_offset;
8656 funcname = get_tok_str(v, NULL);
8657 sym = sym_find(v);
8658 if (sym) {
8659 /* if symbol is already defined, then put complete type */
8660 sym->type = type;
8661 } else {
8662 /* put function symbol */
8663 sym = global_identifier_push(v, type.t, 0);
8664 sym->type.ref = type.ref;
8666 /* NOTE: we patch the symbol size later */
8667 put_extern_sym(sym, cur_text_section, ind, 0);
8668 func_ind = ind;
8669 sym->r = VT_SYM | VT_CONST;
8670 /* put debug symbol */
8671 if (do_debug)
8672 put_func_debug(sym);
8673 /* push a dummy symbol to enable local sym storage */
8674 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8675 gfunc_prolog(&type);
8676 loc = 0;
8677 rsym = 0;
8678 #ifdef CONFIG_REG_VARS
8679 macro_ptr = func_str.str;
8680 next();
8681 #endif
8682 block(NULL, NULL, NULL, NULL, 0, 0);
8683 gsym(rsym);
8684 gfunc_epilog();
8685 cur_text_section->data_offset = ind;
8686 label_pop(&global_label_stack, NULL);
8687 sym_pop(&local_stack, NULL); /* reset local stack */
8688 /* end of function */
8689 /* patch symbol size */
8690 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8691 ind - func_ind;
8692 if (do_debug) {
8693 put_stabn(N_FUN, 0, 0, ind - func_ind);
8695 funcname = ""; /* for safety */
8696 func_vt.t = VT_VOID; /* for safety */
8697 ind = 0; /* for safety */
8699 #ifdef CONFIG_REG_VARS
8700 tok_str_free(func_str.str);
8701 restore_parse_state(&saved_parse_state);
8702 #endif
8703 break;
8704 } else {
8705 if (btype.t & VT_TYPEDEF) {
8706 /* save typedefed type */
8707 /* XXX: test storage specifiers ? */
8708 sym = sym_push(v, &type, 0, 0);
8709 sym->type.t |= VT_TYPEDEF;
8710 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
8711 /* external function definition */
8712 external_sym(v, &type, 0);
8713 } else {
8714 /* not lvalue if array */
8715 r = 0;
8716 if (!(type.t & VT_ARRAY))
8717 r |= lvalue_type(type.t);
8718 has_init = (tok == '=');
8719 if ((btype.t & VT_EXTERN) ||
8720 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
8721 !has_init && l == VT_CONST && type.ref->c < 0)) {
8722 /* external variable */
8723 /* NOTE: as GCC, uninitialized global static
8724 arrays of null size are considered as
8725 extern */
8726 external_sym(v, &type, r);
8727 } else {
8728 if (type.t & VT_STATIC)
8729 r |= VT_CONST;
8730 else
8731 r |= l;
8732 if (has_init)
8733 next();
8734 decl_initializer_alloc(&type, &ad, r,
8735 has_init, v, l);
8738 if (tok != ',') {
8739 skip(';');
8740 break;
8742 next();
8748 /* better than nothing, but needs extension to handle '-E' option
8749 correctly too */
8750 static void preprocess_init(TCCState *s1)
8752 s1->include_stack_ptr = s1->include_stack;
8753 /* XXX: move that before to avoid having to initialize
8754 file->ifdef_stack_ptr ? */
8755 s1->ifdef_stack_ptr = s1->ifdef_stack;
8756 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
8758 /* XXX: not ANSI compliant: bound checking says error */
8759 vtop = vstack - 1;
8762 /* compile the C file opened in 'file'. Return non zero if errors. */
8763 static int tcc_compile(TCCState *s1)
8765 Sym *define_start;
8766 char buf[512];
8767 volatile int section_sym;
8769 #ifdef INC_DEBUG
8770 printf("%s: **** new file\n", file->filename);
8771 #endif
8772 preprocess_init(s1);
8774 funcname = "";
8775 anon_sym = SYM_FIRST_ANOM;
8777 /* file info: full path + filename */
8778 section_sym = 0; /* avoid warning */
8779 if (do_debug) {
8780 section_sym = put_elf_sym(symtab_section, 0, 0,
8781 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
8782 text_section->sh_num, NULL);
8783 getcwd(buf, sizeof(buf));
8784 pstrcat(buf, sizeof(buf), "/");
8785 put_stabs_r(buf, N_SO, 0, 0,
8786 text_section->data_offset, text_section, section_sym);
8787 put_stabs_r(file->filename, N_SO, 0, 0,
8788 text_section->data_offset, text_section, section_sym);
8790 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8791 symbols can be safely used */
8792 put_elf_sym(symtab_section, 0, 0,
8793 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
8794 SHN_ABS, file->filename);
8796 /* define some often used types */
8797 int_type.t = VT_INT;
8799 char_pointer_type.t = VT_BYTE;
8800 mk_pointer(&char_pointer_type);
8802 func_old_type.t = VT_FUNC;
8803 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
8805 #if 0
8806 /* define 'void *alloca(unsigned int)' builtin function */
8808 Sym *s1;
8810 p = anon_sym++;
8811 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
8812 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
8813 s1->next = NULL;
8814 sym->next = s1;
8815 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
8817 #endif
8819 define_start = define_stack;
8821 if (setjmp(s1->error_jmp_buf) == 0) {
8822 s1->nb_errors = 0;
8823 s1->error_set_jmp_enabled = 1;
8825 ch = file->buf_ptr[0];
8826 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
8827 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
8828 next();
8829 decl(VT_CONST);
8830 if (tok != TOK_EOF)
8831 expect("declaration");
8833 /* end of translation unit info */
8834 if (do_debug) {
8835 put_stabs_r(NULL, N_SO, 0, 0,
8836 text_section->data_offset, text_section, section_sym);
8839 s1->error_set_jmp_enabled = 0;
8841 /* reset define stack, but leave -Dsymbols (may be incorrect if
8842 they are undefined) */
8843 free_defines(define_start);
8845 sym_pop(&global_stack, NULL);
8847 return s1->nb_errors != 0 ? -1 : 0;
8850 #ifdef LIBTCC
8851 int tcc_compile_string(TCCState *s, const char *str)
8853 BufferedFile bf1, *bf = &bf1;
8854 int ret, len;
8855 char *buf;
8857 /* init file structure */
8858 bf->fd = -1;
8859 /* XXX: avoid copying */
8860 len = strlen(str);
8861 buf = tcc_malloc(len + 1);
8862 if (!buf)
8863 return -1;
8864 memcpy(buf, str, len);
8865 buf[len] = CH_EOB;
8866 bf->buf_ptr = buf;
8867 bf->buf_end = buf + len;
8868 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
8869 bf->line_num = 1;
8870 file = bf;
8872 ret = tcc_compile(s);
8874 tcc_free(buf);
8876 /* currently, no need to close */
8877 return ret;
8879 #endif
8881 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8882 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
8884 BufferedFile bf1, *bf = &bf1;
8886 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
8887 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
8888 /* default value */
8889 if (!value)
8890 value = "1";
8891 pstrcat(bf->buffer, IO_BUF_SIZE, value);
8893 /* init file structure */
8894 bf->fd = -1;
8895 bf->buf_ptr = bf->buffer;
8896 bf->buf_end = bf->buffer + strlen(bf->buffer);
8897 *bf->buf_end = CH_EOB;
8898 bf->filename[0] = '\0';
8899 bf->line_num = 1;
8900 file = bf;
8902 s1->include_stack_ptr = s1->include_stack;
8904 /* parse with define parser */
8905 ch = file->buf_ptr[0];
8906 next_nomacro();
8907 parse_define();
8908 file = NULL;
8911 /* undefine a preprocessor symbol */
8912 void tcc_undefine_symbol(TCCState *s1, const char *sym)
8914 TokenSym *ts;
8915 Sym *s;
8916 ts = tok_alloc(sym, strlen(sym));
8917 s = define_find(ts->tok);
8918 /* undefine symbol by putting an invalid name */
8919 if (s)
8920 define_undef(s);
8923 #ifdef CONFIG_TCC_ASM
8925 #ifdef TCC_TARGET_I386
8926 #include "i386-asm.c"
8927 #endif
8928 #include "tccasm.c"
8930 #else
8931 static void asm_instr(void)
8933 error("inline asm() not supported");
8935 #endif
8937 #include "tccelf.c"
8939 /* print the position in the source file of PC value 'pc' by reading
8940 the stabs debug information */
8941 static void rt_printline(unsigned long wanted_pc)
8943 Stab_Sym *sym, *sym_end;
8944 char func_name[128], last_func_name[128];
8945 unsigned long func_addr, last_pc, pc;
8946 const char *incl_files[INCLUDE_STACK_SIZE];
8947 int incl_index, len, last_line_num, i;
8948 const char *str, *p;
8950 fprintf(stderr, "0x%08lx:", wanted_pc);
8952 func_name[0] = '\0';
8953 func_addr = 0;
8954 incl_index = 0;
8955 last_func_name[0] = '\0';
8956 last_pc = 0xffffffff;
8957 last_line_num = 1;
8958 sym = (Stab_Sym *)stab_section->data + 1;
8959 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
8960 while (sym < sym_end) {
8961 switch(sym->n_type) {
8962 /* function start or end */
8963 case N_FUN:
8964 if (sym->n_strx == 0) {
8965 /* we test if between last line and end of function */
8966 pc = sym->n_value + func_addr;
8967 if (wanted_pc >= last_pc && wanted_pc < pc)
8968 goto found;
8969 func_name[0] = '\0';
8970 func_addr = 0;
8971 } else {
8972 str = stabstr_section->data + sym->n_strx;
8973 p = strchr(str, ':');
8974 if (!p) {
8975 pstrcpy(func_name, sizeof(func_name), str);
8976 } else {
8977 len = p - str;
8978 if (len > sizeof(func_name) - 1)
8979 len = sizeof(func_name) - 1;
8980 memcpy(func_name, str, len);
8981 func_name[len] = '\0';
8983 func_addr = sym->n_value;
8985 break;
8986 /* line number info */
8987 case N_SLINE:
8988 pc = sym->n_value + func_addr;
8989 if (wanted_pc >= last_pc && wanted_pc < pc)
8990 goto found;
8991 last_pc = pc;
8992 last_line_num = sym->n_desc;
8993 /* XXX: slow! */
8994 strcpy(last_func_name, func_name);
8995 break;
8996 /* include files */
8997 case N_BINCL:
8998 str = stabstr_section->data + sym->n_strx;
8999 add_incl:
9000 if (incl_index < INCLUDE_STACK_SIZE) {
9001 incl_files[incl_index++] = str;
9003 break;
9004 case N_EINCL:
9005 if (incl_index > 1)
9006 incl_index--;
9007 break;
9008 case N_SO:
9009 if (sym->n_strx == 0) {
9010 incl_index = 0; /* end of translation unit */
9011 } else {
9012 str = stabstr_section->data + sym->n_strx;
9013 /* do not add path */
9014 len = strlen(str);
9015 if (len > 0 && str[len - 1] != '/')
9016 goto add_incl;
9018 break;
9020 sym++;
9023 /* second pass: we try symtab symbols (no line number info) */
9024 incl_index = 0;
9026 Elf32_Sym *sym, *sym_end;
9027 int type;
9029 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
9030 for(sym = (Elf32_Sym *)symtab_section->data + 1;
9031 sym < sym_end;
9032 sym++) {
9033 type = ELF32_ST_TYPE(sym->st_info);
9034 if (type == STT_FUNC) {
9035 if (wanted_pc >= sym->st_value &&
9036 wanted_pc < sym->st_value + sym->st_size) {
9037 pstrcpy(last_func_name, sizeof(last_func_name),
9038 strtab_section->data + sym->st_name);
9039 goto found;
9044 /* did not find any info: */
9045 fprintf(stderr, " ???\n");
9046 return;
9047 found:
9048 if (last_func_name[0] != '\0') {
9049 fprintf(stderr, " %s()", last_func_name);
9051 if (incl_index > 0) {
9052 fprintf(stderr, " (%s:%d",
9053 incl_files[incl_index - 1], last_line_num);
9054 for(i = incl_index - 2; i >= 0; i--)
9055 fprintf(stderr, ", included from %s", incl_files[i]);
9056 fprintf(stderr, ")");
9058 fprintf(stderr, "\n");
9061 #ifndef WIN32
9063 #ifdef __i386__
9065 /* fix for glibc 2.1 */
9066 #ifndef REG_EIP
9067 #define REG_EIP EIP
9068 #define REG_EBP EBP
9069 #endif
9071 /* return the PC at frame level 'level'. Return non zero if not found */
9072 static int rt_get_caller_pc(unsigned long *paddr,
9073 ucontext_t *uc, int level)
9075 unsigned long fp;
9076 int i;
9078 if (level == 0) {
9079 #ifdef __FreeBSD__
9080 *paddr = uc->uc_mcontext.mc_eip;
9081 #else
9082 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9083 #endif
9084 return 0;
9085 } else {
9086 #ifdef __FreeBSD__
9087 fp = uc->uc_mcontext.mc_ebp;
9088 #else
9089 fp = uc->uc_mcontext.gregs[REG_EBP];
9090 #endif
9091 for(i=1;i<level;i++) {
9092 /* XXX: check address validity with program info */
9093 if (fp <= 0x1000 || fp >= 0xc0000000)
9094 return -1;
9095 fp = ((unsigned long *)fp)[0];
9097 *paddr = ((unsigned long *)fp)[1];
9098 return 0;
9101 #else
9103 #warning add arch specific rt_get_caller_pc()
9105 static int rt_get_caller_pc(unsigned long *paddr,
9106 ucontext_t *uc, int level)
9108 return -1;
9110 #endif
9112 /* emit a run time error at position 'pc' */
9113 void rt_error(ucontext_t *uc, const char *fmt, ...)
9115 va_list ap;
9116 unsigned long pc;
9117 int i;
9119 va_start(ap, fmt);
9120 fprintf(stderr, "Runtime error: ");
9121 vfprintf(stderr, fmt, ap);
9122 fprintf(stderr, "\n");
9123 for(i=0;i<num_callers;i++) {
9124 if (rt_get_caller_pc(&pc, uc, i) < 0)
9125 break;
9126 if (i == 0)
9127 fprintf(stderr, "at ");
9128 else
9129 fprintf(stderr, "by ");
9130 rt_printline(pc);
9132 exit(255);
9133 va_end(ap);
9136 /* signal handler for fatal errors */
9137 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9139 ucontext_t *uc = puc;
9141 switch(signum) {
9142 case SIGFPE:
9143 switch(siginf->si_code) {
9144 case FPE_INTDIV:
9145 case FPE_FLTDIV:
9146 rt_error(uc, "division by zero");
9147 break;
9148 default:
9149 rt_error(uc, "floating point exception");
9150 break;
9152 break;
9153 case SIGBUS:
9154 case SIGSEGV:
9155 if (rt_bound_error_msg && *rt_bound_error_msg)
9156 rt_error(uc, *rt_bound_error_msg);
9157 else
9158 rt_error(uc, "dereferencing invalid pointer");
9159 break;
9160 case SIGILL:
9161 rt_error(uc, "illegal instruction");
9162 break;
9163 case SIGABRT:
9164 rt_error(uc, "abort() called");
9165 break;
9166 default:
9167 rt_error(uc, "caught signal %d", signum);
9168 break;
9170 exit(255);
9172 #endif
9174 /* do all relocations (needed before using tcc_get_symbol()) */
9175 int tcc_relocate(TCCState *s1)
9177 Section *s;
9178 int i;
9180 s1->nb_errors = 0;
9182 tcc_add_runtime(s1);
9184 build_got_entries(s1);
9186 relocate_common_syms();
9188 /* compute relocation address : section are relocated in place. We
9189 also alloc the bss space */
9190 for(i = 1; i < s1->nb_sections; i++) {
9191 s = s1->sections[i];
9192 if (s->sh_flags & SHF_ALLOC) {
9193 if (s->sh_type == SHT_NOBITS)
9194 s->data = tcc_mallocz(s->data_offset);
9195 s->sh_addr = (unsigned long)s->data;
9199 relocate_syms(s1, 1);
9201 if (s1->nb_errors != 0)
9202 return -1;
9204 /* relocate each section */
9205 for(i = 1; i < s1->nb_sections; i++) {
9206 s = s1->sections[i];
9207 if (s->reloc)
9208 relocate_section(s1, s);
9210 return 0;
9213 /* launch the compiled program with the given arguments */
9214 int tcc_run(TCCState *s1, int argc, char **argv)
9216 int (*prog_main)(int, char **);
9218 if (tcc_relocate(s1) < 0)
9219 return -1;
9221 prog_main = tcc_get_symbol_err(s1, "main");
9223 if (do_debug) {
9224 #ifdef WIN32
9225 error("debug mode currently not available for Windows");
9226 #else
9227 struct sigaction sigact;
9228 /* install TCC signal handlers to print debug info on fatal
9229 runtime errors */
9230 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
9231 sigact.sa_sigaction = sig_error;
9232 sigemptyset(&sigact.sa_mask);
9233 sigaction(SIGFPE, &sigact, NULL);
9234 sigaction(SIGILL, &sigact, NULL);
9235 sigaction(SIGSEGV, &sigact, NULL);
9236 sigaction(SIGBUS, &sigact, NULL);
9237 sigaction(SIGABRT, &sigact, NULL);
9238 #endif
9241 #ifdef CONFIG_TCC_BCHECK
9242 if (do_bounds_check) {
9243 void (*bound_init)(void);
9245 /* set error function */
9246 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
9247 "__bound_error_msg");
9249 /* XXX: use .init section so that it also work in binary ? */
9250 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
9251 bound_init();
9253 #endif
9254 return (*prog_main)(argc, argv);
9257 TCCState *tcc_new(void)
9259 const char *p, *r;
9260 TCCState *s;
9261 TokenSym *ts;
9262 int i, c;
9264 s = tcc_mallocz(sizeof(TCCState));
9265 if (!s)
9266 return NULL;
9267 tcc_state = s;
9268 s->output_type = TCC_OUTPUT_MEMORY;
9270 /* init isid table */
9271 for(i=0;i<256;i++)
9272 isidnum_table[i] = isid(i) || isnum(i);
9274 /* add all tokens */
9275 table_ident = NULL;
9276 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9278 tok_ident = TOK_IDENT;
9279 p = tcc_keywords;
9280 while (*p) {
9281 r = p;
9282 for(;;) {
9283 c = *r++;
9284 if (c == '\0')
9285 break;
9287 ts = tok_alloc(p, r - p - 1);
9288 p = r;
9291 /* we add dummy defines for some special macros to speed up tests
9292 and to have working defined() */
9293 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9294 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9295 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9296 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9298 /* standard defines */
9299 tcc_define_symbol(s, "__STDC__", NULL);
9300 #if defined(TCC_TARGET_I386)
9301 tcc_define_symbol(s, "__i386__", NULL);
9302 #endif
9303 #if defined(TCC_TARGET_ARM)
9304 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
9305 tcc_define_symbol(s, "__arm_elf__", NULL);
9306 tcc_define_symbol(s, "__arm_elf", NULL);
9307 tcc_define_symbol(s, "arm_elf", NULL);
9308 tcc_define_symbol(s, "__arm__", NULL);
9309 tcc_define_symbol(s, "__arm", NULL);
9310 tcc_define_symbol(s, "arm", NULL);
9311 tcc_define_symbol(s, "__APCS_32__", NULL);
9312 #endif
9313 #if defined(linux)
9314 tcc_define_symbol(s, "__linux__", NULL);
9315 tcc_define_symbol(s, "linux", NULL);
9316 #endif
9317 /* tiny C specific defines */
9318 tcc_define_symbol(s, "__TINYC__", NULL);
9320 /* tiny C & gcc defines */
9321 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9322 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9323 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9325 /* default library paths */
9326 tcc_add_library_path(s, "/usr/local/lib");
9327 tcc_add_library_path(s, "/usr/lib");
9328 tcc_add_library_path(s, "/lib");
9330 /* no section zero */
9331 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9333 /* create standard sections */
9334 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9335 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9336 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9338 /* symbols are always generated for linking stage */
9339 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9340 ".strtab",
9341 ".hashtab", SHF_PRIVATE);
9342 strtab_section = symtab_section->link;
9344 /* private symbol table for dynamic symbols */
9345 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9346 ".dynstrtab",
9347 ".dynhashtab", SHF_PRIVATE);
9348 s->alacarte_link = 1;
9350 #ifdef CHAR_IS_UNSIGNED
9351 s->char_is_unsigned = 1;
9352 #endif
9353 return s;
9356 void tcc_delete(TCCState *s1)
9358 int i, n;
9360 /* free -D defines */
9361 free_defines(NULL);
9363 /* free tokens */
9364 n = tok_ident - TOK_IDENT;
9365 for(i = 0; i < n; i++)
9366 tcc_free(table_ident[i]);
9367 tcc_free(table_ident);
9369 /* free all sections */
9371 free_section(symtab_section->hash);
9373 free_section(s1->dynsymtab_section->hash);
9374 free_section(s1->dynsymtab_section->link);
9375 free_section(s1->dynsymtab_section);
9377 for(i = 1; i < s1->nb_sections; i++)
9378 free_section(s1->sections[i]);
9379 tcc_free(s1->sections);
9381 /* free loaded dlls array */
9382 for(i = 0; i < s1->nb_loaded_dlls; i++)
9383 tcc_free(s1->loaded_dlls[i]);
9384 tcc_free(s1->loaded_dlls);
9386 /* library paths */
9387 for(i = 0; i < s1->nb_library_paths; i++)
9388 tcc_free(s1->library_paths[i]);
9389 tcc_free(s1->library_paths);
9391 /* cached includes */
9392 for(i = 0; i < s1->nb_cached_includes; i++)
9393 tcc_free(s1->cached_includes[i]);
9394 tcc_free(s1->cached_includes);
9396 for(i = 0; i < s1->nb_include_paths; i++)
9397 tcc_free(s1->include_paths[i]);
9398 tcc_free(s1->include_paths);
9400 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9401 tcc_free(s1->sysinclude_paths[i]);
9402 tcc_free(s1->sysinclude_paths);
9404 tcc_free(s1);
9407 int tcc_add_include_path(TCCState *s1, const char *pathname)
9409 char *pathname1;
9411 pathname1 = tcc_strdup(pathname);
9412 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9413 return 0;
9416 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9418 char *pathname1;
9420 pathname1 = tcc_strdup(pathname);
9421 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9422 return 0;
9425 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9427 const char *ext, *filename1;
9428 Elf32_Ehdr ehdr;
9429 int fd, ret;
9430 BufferedFile *saved_file;
9432 /* find source file type with extension */
9433 filename1 = strrchr(filename, '/');
9434 if (filename1)
9435 filename1++;
9436 else
9437 filename1 = filename;
9438 ext = strrchr(filename1, '.');
9439 if (ext)
9440 ext++;
9442 /* open the file */
9443 saved_file = file;
9444 file = tcc_open(s1, filename);
9445 if (!file) {
9446 if (flags & AFF_PRINT_ERROR) {
9447 error_noabort("file '%s' not found", filename);
9449 ret = -1;
9450 goto fail1;
9453 if (!ext || !strcmp(ext, "c")) {
9454 /* C file assumed */
9455 ret = tcc_compile(s1);
9456 } else
9457 #ifdef CONFIG_TCC_ASM
9458 if (!strcmp(ext, "S")) {
9459 /* preprocessed assembler */
9460 ret = tcc_assemble(s1, 1);
9461 } else if (!strcmp(ext, "s")) {
9462 /* non preprocessed assembler */
9463 ret = tcc_assemble(s1, 0);
9464 } else
9465 #endif
9467 fd = file->fd;
9468 /* assume executable format: auto guess file type */
9469 ret = read(fd, &ehdr, sizeof(ehdr));
9470 lseek(fd, 0, SEEK_SET);
9471 if (ret <= 0) {
9472 error_noabort("could not read header");
9473 goto fail;
9474 } else if (ret != sizeof(ehdr)) {
9475 goto try_load_script;
9478 if (ehdr.e_ident[0] == ELFMAG0 &&
9479 ehdr.e_ident[1] == ELFMAG1 &&
9480 ehdr.e_ident[2] == ELFMAG2 &&
9481 ehdr.e_ident[3] == ELFMAG3) {
9482 file->line_num = 0; /* do not display line number if error */
9483 if (ehdr.e_type == ET_REL) {
9484 ret = tcc_load_object_file(s1, fd, 0);
9485 } else if (ehdr.e_type == ET_DYN) {
9486 if (s1->output_type == TCC_OUTPUT_MEMORY) {
9487 void *h;
9488 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
9489 if (h)
9490 ret = 0;
9491 else
9492 ret = -1;
9493 } else {
9494 ret = tcc_load_dll(s1, fd, filename,
9495 (flags & AFF_REFERENCED_DLL) != 0);
9497 } else {
9498 error_noabort("unrecognized ELF file");
9499 goto fail;
9501 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9502 file->line_num = 0; /* do not display line number if error */
9503 ret = tcc_load_archive(s1, fd);
9504 } else {
9505 /* as GNU ld, consider it is an ld script if not recognized */
9506 try_load_script:
9507 ret = tcc_load_ldscript(s1);
9508 if (ret < 0) {
9509 error_noabort("unrecognized file type");
9510 goto fail;
9514 the_end:
9515 tcc_close(file);
9516 fail1:
9517 file = saved_file;
9518 return ret;
9519 fail:
9520 ret = -1;
9521 goto the_end;
9524 int tcc_add_file(TCCState *s, const char *filename)
9526 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9529 int tcc_add_library_path(TCCState *s, const char *pathname)
9531 char *pathname1;
9533 pathname1 = tcc_strdup(pathname);
9534 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9535 return 0;
9538 /* find and load a dll. Return non zero if not found */
9539 /* XXX: add '-rpath' option support ? */
9540 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9542 char buf[1024];
9543 int i;
9545 for(i = 0; i < s->nb_library_paths; i++) {
9546 snprintf(buf, sizeof(buf), "%s/%s",
9547 s->library_paths[i], filename);
9548 if (tcc_add_file_internal(s, buf, flags) == 0)
9549 return 0;
9551 return -1;
9554 /* the library name is the same as the argument of the '-l' option */
9555 int tcc_add_library(TCCState *s, const char *libraryname)
9557 char buf[1024];
9558 int i;
9560 /* first we look for the dynamic library if not static linking */
9561 if (!s->static_link) {
9562 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9563 if (tcc_add_dll(s, buf, 0) == 0)
9564 return 0;
9567 /* then we look for the static library */
9568 for(i = 0; i < s->nb_library_paths; i++) {
9569 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9570 s->library_paths[i], libraryname);
9571 if (tcc_add_file_internal(s, buf, 0) == 0)
9572 return 0;
9574 return -1;
9577 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9579 add_elf_sym(symtab_section, val, 0,
9580 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9581 SHN_ABS, name);
9582 return 0;
9585 int tcc_set_output_type(TCCState *s, int output_type)
9587 char buf[1024];
9589 s->output_type = output_type;
9591 if (!s->nostdinc) {
9592 /* default include paths */
9593 /* XXX: reverse order needed if -isystem support */
9594 tcc_add_sysinclude_path(s, "/usr/local/include");
9595 tcc_add_sysinclude_path(s, "/usr/include");
9596 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
9597 tcc_add_sysinclude_path(s, buf);
9600 /* if bound checking, then add corresponding sections */
9601 #ifdef CONFIG_TCC_BCHECK
9602 if (do_bounds_check) {
9603 /* define symbol */
9604 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
9605 /* create bounds sections */
9606 bounds_section = new_section(s, ".bounds",
9607 SHT_PROGBITS, SHF_ALLOC);
9608 lbounds_section = new_section(s, ".lbounds",
9609 SHT_PROGBITS, SHF_ALLOC);
9611 #endif
9613 if (s->char_is_unsigned) {
9614 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
9617 /* add debug sections */
9618 if (do_debug) {
9619 /* stab symbols */
9620 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
9621 stab_section->sh_entsize = sizeof(Stab_Sym);
9622 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
9623 put_elf_str(stabstr_section, "");
9624 stab_section->link = stabstr_section;
9625 /* put first entry */
9626 put_stabs("", 0, 0, 0, 0);
9629 /* add libc crt1/crti objects */
9630 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
9631 !s->nostdlib) {
9632 if (output_type != TCC_OUTPUT_DLL)
9633 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
9634 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
9636 return 0;
9639 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
9640 #define FD_INVERT 0x0002 /* invert value before storing */
9642 typedef struct FlagDef {
9643 uint16_t offset;
9644 uint16_t flags;
9645 const char *name;
9646 } FlagDef;
9648 static const FlagDef warning_defs[] = {
9649 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
9650 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
9651 { offsetof(TCCState, warn_error), 0, "error" },
9652 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
9653 "implicit-function-declaration" },
9656 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
9657 const char *name, int value)
9659 int i;
9660 const FlagDef *p;
9661 const char *r;
9663 r = name;
9664 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
9665 r += 3;
9666 value = !value;
9668 for(i = 0, p = flags; i < nb_flags; i++, p++) {
9669 if (!strcmp(r, p->name))
9670 goto found;
9672 return -1;
9673 found:
9674 if (p->flags & FD_INVERT)
9675 value = !value;
9676 *(int *)((uint8_t *)s + p->offset) = value;
9677 return 0;
9681 /* set/reset a warning */
9682 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
9684 int i;
9685 const FlagDef *p;
9687 if (!strcmp(warning_name, "all")) {
9688 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9689 if (p->flags & WD_ALL)
9690 *(int *)((uint8_t *)s + p->offset) = 1;
9692 return 0;
9693 } else {
9694 return set_flag(s, warning_defs, countof(warning_defs),
9695 warning_name, value);
9699 static const FlagDef flag_defs[] = {
9700 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
9701 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
9704 /* set/reset a flag */
9705 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
9707 return set_flag(s, flag_defs, countof(flag_defs),
9708 flag_name, value);
9711 #if !defined(LIBTCC)
9713 /* extract the basename of a file */
9714 static const char *tcc_basename(const char *name)
9716 const char *p;
9717 p = strrchr(name, '/');
9718 #ifdef WIN32
9719 if (!p)
9720 p = strrchr(name, '\\');
9721 #endif
9722 if (!p)
9723 p = name;
9724 else
9725 p++;
9726 return p;
9729 static int64_t getclock_us(void)
9731 #ifdef WIN32
9732 struct _timeb tb;
9733 _ftime(&tb);
9734 return (tb.time * 1000LL + tb.millitm) * 1000LL;
9735 #else
9736 struct timeval tv;
9737 gettimeofday(&tv, NULL);
9738 return tv.tv_sec * 1000000LL + tv.tv_usec;
9739 #endif
9742 void help(void)
9744 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2003 Fabrice Bellard\n"
9745 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9746 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9747 " [infile1 infile2...] [-run infile args...]\n"
9748 "\n"
9749 "General options:\n"
9750 " -v display current version\n"
9751 " -c compile only - generate an object file\n"
9752 " -o outfile set output filename\n"
9753 " -Bdir set tcc internal library path\n"
9754 " -bench output compilation statistics\n"
9755 " -run run compiled source\n"
9756 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
9757 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
9758 " -w disable all warnings\n"
9759 "Preprocessor options:\n"
9760 " -Idir add include path 'dir'\n"
9761 " -Dsym[=val] define 'sym' with value 'val'\n"
9762 " -Usym undefine 'sym'\n"
9763 "Linker options:\n"
9764 " -Ldir add library path 'dir'\n"
9765 " -llib link with dynamic or static library 'lib'\n"
9766 " -shared generate a shared library\n"
9767 " -static static linking\n"
9768 " -rdynamic export all global symbols to dynamic linker\n"
9769 " -r relocatable output\n"
9770 "Debugger options:\n"
9771 " -g generate runtime debug info\n"
9772 #ifdef CONFIG_TCC_BCHECK
9773 " -b compile with built-in memory and bounds checker (implies -g)\n"
9774 #endif
9775 " -bt N show N callers in stack traces\n"
9779 #define TCC_OPTION_HAS_ARG 0x0001
9780 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9782 typedef struct TCCOption {
9783 const char *name;
9784 uint16_t index;
9785 uint16_t flags;
9786 } TCCOption;
9788 enum {
9789 TCC_OPTION_HELP,
9790 TCC_OPTION_I,
9791 TCC_OPTION_D,
9792 TCC_OPTION_U,
9793 TCC_OPTION_L,
9794 TCC_OPTION_B,
9795 TCC_OPTION_l,
9796 TCC_OPTION_bench,
9797 TCC_OPTION_bt,
9798 TCC_OPTION_b,
9799 TCC_OPTION_g,
9800 TCC_OPTION_c,
9801 TCC_OPTION_static,
9802 TCC_OPTION_shared,
9803 TCC_OPTION_o,
9804 TCC_OPTION_r,
9805 TCC_OPTION_W,
9806 TCC_OPTION_O,
9807 TCC_OPTION_m,
9808 TCC_OPTION_f,
9809 TCC_OPTION_nostdinc,
9810 TCC_OPTION_nostdlib,
9811 TCC_OPTION_print_search_dirs,
9812 TCC_OPTION_rdynamic,
9813 TCC_OPTION_run,
9814 TCC_OPTION_v,
9815 TCC_OPTION_w,
9818 static const TCCOption tcc_options[] = {
9819 { "h", TCC_OPTION_HELP, 0 },
9820 { "?", TCC_OPTION_HELP, 0 },
9821 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
9822 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
9823 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
9824 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
9825 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
9826 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9827 { "bench", TCC_OPTION_bench, 0 },
9828 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
9829 #ifdef CONFIG_TCC_BCHECK
9830 { "b", TCC_OPTION_b, 0 },
9831 #endif
9832 { "g", TCC_OPTION_g, 0 },
9833 { "c", TCC_OPTION_c, 0 },
9834 { "static", TCC_OPTION_static, 0 },
9835 { "shared", TCC_OPTION_shared, 0 },
9836 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
9837 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9838 { "rdynamic", TCC_OPTION_rdynamic, 0 },
9839 { "r", TCC_OPTION_r, 0 },
9840 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9841 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9842 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
9843 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9844 { "nostdinc", TCC_OPTION_nostdinc, 0 },
9845 { "nostdlib", TCC_OPTION_nostdlib, 0 },
9846 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
9847 { "v", TCC_OPTION_v, 0 },
9848 { "w", TCC_OPTION_w, 0 },
9849 { NULL },
9852 /* convert 'str' into an array of space separated strings */
9853 static int expand_args(char ***pargv, const char *str)
9855 const char *s1;
9856 char **argv, *arg;
9857 int argc, len;
9859 argc = 0;
9860 argv = NULL;
9861 for(;;) {
9862 while (is_space(*str))
9863 str++;
9864 if (*str == '\0')
9865 break;
9866 s1 = str;
9867 while (*str != '\0' && !is_space(*str))
9868 str++;
9869 len = str - s1;
9870 arg = tcc_malloc(len + 1);
9871 memcpy(arg, s1, len);
9872 arg[len] = '\0';
9873 dynarray_add((void ***)&argv, &argc, arg);
9875 *pargv = argv;
9876 return argc;
9879 static char **files;
9880 static int nb_files, nb_libraries;
9881 static int multiple_files;
9882 static int print_search_dirs;
9883 static int output_type;
9884 static int reloc_output;
9885 static const char *outfile;
9887 int parse_args(TCCState *s, int argc, char **argv)
9889 int optind;
9890 const TCCOption *popt;
9891 const char *optarg, *p1, *r1;
9892 char *r;
9894 optind = 0;
9895 while (1) {
9896 if (optind >= argc) {
9897 if (nb_files == 0 && !print_search_dirs)
9898 goto show_help;
9899 else
9900 break;
9902 r = argv[optind++];
9903 if (r[0] != '-') {
9904 /* add a new file */
9905 dynarray_add((void ***)&files, &nb_files, r);
9906 if (!multiple_files) {
9907 optind--;
9908 /* argv[0] will be this file */
9909 break;
9911 } else {
9912 /* find option in table (match only the first chars */
9913 popt = tcc_options;
9914 for(;;) {
9915 p1 = popt->name;
9916 if (p1 == NULL)
9917 error("invalid option -- '%s'", r);
9918 r1 = r + 1;
9919 for(;;) {
9920 if (*p1 == '\0')
9921 goto option_found;
9922 if (*r1 != *p1)
9923 break;
9924 p1++;
9925 r1++;
9927 popt++;
9929 option_found:
9930 if (popt->flags & TCC_OPTION_HAS_ARG) {
9931 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
9932 optarg = r1;
9933 } else {
9934 if (optind >= argc)
9935 error("argument to '%s' is missing", r);
9936 optarg = argv[optind++];
9938 } else {
9939 if (*r1 != '\0')
9940 goto show_help;
9941 optarg = NULL;
9944 switch(popt->index) {
9945 case TCC_OPTION_HELP:
9946 show_help:
9947 help();
9948 exit(1);
9949 case TCC_OPTION_I:
9950 if (tcc_add_include_path(s, optarg) < 0)
9951 error("too many include paths");
9952 break;
9953 case TCC_OPTION_D:
9955 char *sym, *value;
9956 sym = (char *)optarg;
9957 value = strchr(sym, '=');
9958 if (value) {
9959 *value = '\0';
9960 value++;
9962 tcc_define_symbol(s, sym, value);
9964 break;
9965 case TCC_OPTION_U:
9966 tcc_undefine_symbol(s, optarg);
9967 break;
9968 case TCC_OPTION_L:
9969 tcc_add_library_path(s, optarg);
9970 break;
9971 case TCC_OPTION_B:
9972 /* set tcc utilities path (mainly for tcc development) */
9973 tcc_lib_path = optarg;
9974 break;
9975 case TCC_OPTION_l:
9976 dynarray_add((void ***)&files, &nb_files, r);
9977 nb_libraries++;
9978 break;
9979 case TCC_OPTION_bench:
9980 do_bench = 1;
9981 break;
9982 case TCC_OPTION_bt:
9983 num_callers = atoi(optarg);
9984 break;
9985 #ifdef CONFIG_TCC_BCHECK
9986 case TCC_OPTION_b:
9987 do_bounds_check = 1;
9988 do_debug = 1;
9989 break;
9990 #endif
9991 case TCC_OPTION_g:
9992 do_debug = 1;
9993 break;
9994 case TCC_OPTION_c:
9995 multiple_files = 1;
9996 output_type = TCC_OUTPUT_OBJ;
9997 break;
9998 case TCC_OPTION_static:
9999 s->static_link = 1;
10000 break;
10001 case TCC_OPTION_shared:
10002 output_type = TCC_OUTPUT_DLL;
10003 break;
10004 case TCC_OPTION_o:
10005 multiple_files = 1;
10006 outfile = optarg;
10007 break;
10008 case TCC_OPTION_r:
10009 /* generate a .o merging several output files */
10010 reloc_output = 1;
10011 output_type = TCC_OUTPUT_OBJ;
10012 break;
10013 case TCC_OPTION_nostdinc:
10014 s->nostdinc = 1;
10015 break;
10016 case TCC_OPTION_nostdlib:
10017 s->nostdlib = 1;
10018 break;
10019 case TCC_OPTION_print_search_dirs:
10020 print_search_dirs = 1;
10021 break;
10022 case TCC_OPTION_run:
10024 int argc1;
10025 char **argv1;
10026 argc1 = expand_args(&argv1, optarg);
10027 if (argc1 > 0) {
10028 parse_args(s, argc1, argv1);
10030 multiple_files = 0;
10031 output_type = TCC_OUTPUT_MEMORY;
10033 break;
10034 case TCC_OPTION_v:
10035 printf("tcc version %s\n", TCC_VERSION);
10036 exit(0);
10037 case TCC_OPTION_f:
10038 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
10039 goto unsupported_option;
10040 break;
10041 case TCC_OPTION_W:
10042 if (tcc_set_warning(s, optarg, 1) < 0 &&
10043 s->warn_unsupported)
10044 goto unsupported_option;
10045 break;
10046 case TCC_OPTION_w:
10047 s->warn_none = 1;
10048 break;
10049 case TCC_OPTION_rdynamic:
10050 s->rdynamic = 1;
10051 break;
10052 default:
10053 if (s->warn_unsupported) {
10054 unsupported_option:
10055 warning("unsupported option '%s'", r);
10057 break;
10061 return optind;
10064 int main(int argc, char **argv)
10066 int i;
10067 TCCState *s;
10068 int nb_objfiles, ret, optind;
10069 char objfilename[1024];
10070 int64_t start_time = 0;
10072 s = tcc_new();
10073 output_type = TCC_OUTPUT_EXE;
10074 outfile = NULL;
10075 multiple_files = 1;
10076 files = NULL;
10077 nb_files = 0;
10078 nb_libraries = 0;
10079 reloc_output = 0;
10080 print_search_dirs = 0;
10082 optind = parse_args(s, argc - 1, argv + 1) + 1;
10084 if (print_search_dirs) {
10085 /* enough for Linux kernel */
10086 printf("install: %s/\n", tcc_lib_path);
10087 return 0;
10090 nb_objfiles = nb_files - nb_libraries;
10092 /* if outfile provided without other options, we output an
10093 executable */
10094 if (outfile && output_type == TCC_OUTPUT_MEMORY)
10095 output_type = TCC_OUTPUT_EXE;
10097 /* check -c consistency : only single file handled. XXX: checks file type */
10098 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10099 /* accepts only a single input file */
10100 if (nb_objfiles != 1)
10101 error("cannot specify multiple files with -c");
10102 if (nb_libraries != 0)
10103 error("cannot specify libraries with -c");
10106 /* compute default outfile name */
10107 if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
10108 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10109 char *ext;
10110 /* strip path */
10111 pstrcpy(objfilename, sizeof(objfilename) - 1,
10112 tcc_basename(files[0]));
10113 /* add .o extension */
10114 ext = strrchr(objfilename, '.');
10115 if (!ext)
10116 goto default_outfile;
10117 strcpy(ext + 1, "o");
10118 } else {
10119 default_outfile:
10120 pstrcpy(objfilename, sizeof(objfilename), "a.out");
10122 outfile = objfilename;
10125 if (do_bench) {
10126 start_time = getclock_us();
10129 tcc_set_output_type(s, output_type);
10131 /* compile or add each files or library */
10132 for(i = 0;i < nb_files; i++) {
10133 const char *filename;
10135 filename = files[i];
10136 if (filename[0] == '-') {
10137 if (tcc_add_library(s, filename + 2) < 0)
10138 error("cannot find %s", filename);
10139 } else {
10140 if (tcc_add_file(s, filename) < 0) {
10141 ret = 1;
10142 goto the_end;
10147 /* free all files */
10148 tcc_free(files);
10150 if (do_bench) {
10151 double total_time;
10152 total_time = (double)(getclock_us() - start_time) / 1000000.0;
10153 if (total_time < 0.001)
10154 total_time = 0.001;
10155 if (total_bytes < 1)
10156 total_bytes = 1;
10157 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
10158 tok_ident - TOK_IDENT, total_lines, total_bytes,
10159 total_time, (int)(total_lines / total_time),
10160 total_bytes / total_time / 1000000.0);
10163 if (s->output_type != TCC_OUTPUT_MEMORY) {
10164 tcc_output_file(s, outfile);
10165 ret = 0;
10166 } else {
10167 ret = tcc_run(s, argc - optind, argv + optind);
10169 the_end:
10170 /* XXX: cannot do it with bound checking because of the malloc hooks */
10171 if (!do_bounds_check)
10172 tcc_delete(s);
10174 #ifdef MEM_DEBUG
10175 if (do_bench) {
10176 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
10178 #endif
10179 return ret;
10182 #endif