fixed const and volatile function parameters typing - fixed string parsing when skipp...
[tinycc.git] / tcc.c
blobb702ed501026ef10cccc8bf897a934a3c4e49305
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 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 #ifdef CONFIG_TCCBOOT
25 #include "tccboot.h"
26 #define CONFIG_TCC_STATIC
28 #else
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <math.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <setjmp.h>
41 #include <time.h>
42 #ifdef WIN32
43 #include <sys/timeb.h>
44 #define CONFIG_TCC_STATIC
45 #endif
46 #ifndef WIN32
47 #include <sys/time.h>
48 #include <sys/ucontext.h>
49 #endif
51 #endif /* !CONFIG_TCCBOOT */
53 #include "elf.h"
54 #include "stab.h"
55 #ifndef CONFIG_TCC_STATIC
56 #include <dlfcn.h>
57 #endif
58 #ifndef O_BINARY
59 #define O_BINARY 0
60 #endif
62 #include "libtcc.h"
64 /* parser debug */
65 //#define PARSE_DEBUG
66 /* preprocessor debug */
67 //#define PP_DEBUG
68 /* include file debug */
69 //#define INC_DEBUG
71 //#define MEM_DEBUG
73 /* assembler debug */
74 //#define ASM_DEBUG
76 /* target selection */
77 //#define TCC_TARGET_I386 /* i386 code generator */
78 //#define TCC_TARGET_ARM /* ARMv4 code generator */
79 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
81 /* default target is I386 */
82 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
83 !defined(TCC_TARGET_C67)
84 #define TCC_TARGET_I386
85 #endif
87 #if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
88 !defined(TCC_TARGET_C67)
89 #define CONFIG_TCC_BCHECK /* enable bound checking code */
90 #endif
92 /* define it to include assembler support */
93 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
94 #define CONFIG_TCC_ASM
95 #endif
97 /* object format selection */
98 #if defined(TCC_TARGET_C67)
99 #define TCC_TARGET_COFF
100 #endif
102 #define FALSE 0
103 #define false 0
104 #define TRUE 1
105 #define true 1
106 typedef int BOOL;
108 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
109 executables or dlls */
110 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
112 #define INCLUDE_STACK_SIZE 32
113 #define IFDEF_STACK_SIZE 64
114 #define VSTACK_SIZE 64
115 #define STRING_MAX_SIZE 1024
117 #define TOK_HASH_SIZE 8192 /* must be a power of two */
118 #define TOK_ALLOC_INCR 512 /* must be a power of two */
119 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
121 /* token symbol management */
122 typedef struct TokenSym {
123 struct TokenSym *hash_next;
124 struct Sym *sym_define; /* direct pointer to define */
125 struct Sym *sym_label; /* direct pointer to label */
126 struct Sym *sym_struct; /* direct pointer to structure */
127 struct Sym *sym_identifier; /* direct pointer to identifier */
128 int tok; /* token number */
129 int len;
130 char str[1];
131 } TokenSym;
133 typedef struct CString {
134 int size; /* size in bytes */
135 void *data; /* either 'char *' or 'int *' */
136 int size_allocated;
137 void *data_allocated; /* if non NULL, data has been malloced */
138 } CString;
140 /* type definition */
141 typedef struct CType {
142 int t;
143 struct Sym *ref;
144 } CType;
146 /* constant value */
147 typedef union CValue {
148 long double ld;
149 double d;
150 float f;
151 int i;
152 unsigned int ui;
153 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
154 long long ll;
155 unsigned long long ull;
156 struct CString *cstr;
157 void *ptr;
158 int tab[1];
159 } CValue;
161 /* value on stack */
162 typedef struct SValue {
163 CType type; /* type */
164 unsigned short r; /* register + flags */
165 unsigned short r2; /* second register, used for 'long long'
166 type. If not used, set to VT_CONST */
167 CValue c; /* constant, if VT_CONST */
168 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
169 } SValue;
171 /* symbol management */
172 typedef struct Sym {
173 int v; /* symbol token */
174 int r; /* associated register */
175 int c; /* associated number */
176 CType type; /* associated type */
177 struct Sym *next; /* next related symbol */
178 struct Sym *prev; /* prev symbol in stack */
179 struct Sym *prev_tok; /* previous symbol for this token */
180 } Sym;
182 /* section definition */
183 /* XXX: use directly ELF structure for parameters ? */
184 /* special flag to indicate that the section should not be linked to
185 the other ones */
186 #define SHF_PRIVATE 0x80000000
188 typedef struct Section {
189 unsigned long data_offset; /* current data offset */
190 unsigned char *data; /* section data */
191 unsigned long data_allocated; /* used for realloc() handling */
192 int sh_name; /* elf section name (only used during output) */
193 int sh_num; /* elf section number */
194 int sh_type; /* elf section type */
195 int sh_flags; /* elf section flags */
196 int sh_info; /* elf section info */
197 int sh_addralign; /* elf section alignment */
198 int sh_entsize; /* elf entry size */
199 unsigned long sh_size; /* section size (only used during output) */
200 unsigned long sh_addr; /* address at which the section is relocated */
201 unsigned long sh_offset; /* address at which the section is relocated */
202 int nb_hashed_syms; /* used to resize the hash table */
203 struct Section *link; /* link to another section */
204 struct Section *reloc; /* corresponding section for relocation, if any */
205 struct Section *hash; /* hash table for symbols */
206 struct Section *next;
207 char name[1]; /* section name */
208 } Section;
210 typedef struct DLLReference {
211 int level;
212 char name[1];
213 } DLLReference;
215 /* GNUC attribute definition */
216 typedef struct AttributeDef {
217 int aligned;
218 Section *section;
219 unsigned char func_call; /* FUNC_CDECL, FUNC_STDCALL, FUNC_FASTCALLx */
220 } AttributeDef;
222 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
223 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
224 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
226 /* stored in 'Sym.c' field */
227 #define FUNC_NEW 1 /* ansi function prototype */
228 #define FUNC_OLD 2 /* old function prototype */
229 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
231 /* stored in 'Sym.r' field */
232 #define FUNC_CDECL 0 /* standard c call */
233 #define FUNC_STDCALL 1 /* pascal c call */
234 #define FUNC_FASTCALL1 2 /* first param in %eax */
235 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
236 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
238 /* field 'Sym.t' for macros */
239 #define MACRO_OBJ 0 /* object like macro */
240 #define MACRO_FUNC 1 /* function like macro */
242 /* field 'Sym.r' for C labels */
243 #define LABEL_DEFINED 0 /* label is defined */
244 #define LABEL_FORWARD 1 /* label is forward defined */
245 #define LABEL_DECLARED 2 /* label is declared but never used */
247 /* type_decl() types */
248 #define TYPE_ABSTRACT 1 /* type without variable */
249 #define TYPE_DIRECT 2 /* type with variable */
251 #define IO_BUF_SIZE 8192
253 typedef struct BufferedFile {
254 uint8_t *buf_ptr;
255 uint8_t *buf_end;
256 int fd;
257 int line_num; /* current line number - here to simplify code */
258 int ifndef_macro; /* #ifndef macro / #endif search */
259 int ifndef_macro_saved; /* saved ifndef_macro */
260 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
261 char inc_type; /* type of include */
262 char inc_filename[512]; /* filename specified by the user */
263 char filename[1024]; /* current filename - here to simplify code */
264 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
265 } BufferedFile;
267 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
268 #define CH_EOF (-1) /* end of file */
270 /* parsing state (used to save parser state to reparse part of the
271 source several times) */
272 typedef struct ParseState {
273 int *macro_ptr;
274 int line_num;
275 int tok;
276 CValue tokc;
277 } ParseState;
279 /* used to record tokens */
280 typedef struct TokenString {
281 int *str;
282 int len;
283 int allocated_len;
284 int last_line_num;
285 } TokenString;
287 /* include file cache, used to find files faster and also to eliminate
288 inclusion if the include file is protected by #ifndef ... #endif */
289 typedef struct CachedInclude {
290 int ifndef_macro;
291 char type; /* '"' or '>' to give include type */
292 char filename[1]; /* path specified in #include */
293 } CachedInclude;
295 /* parser */
296 static struct BufferedFile *file;
297 static int ch, tok;
298 static CValue tokc;
299 static CString tokcstr; /* current parsed string, if any */
300 /* additional informations about token */
301 static int tok_flags;
302 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
303 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
304 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
306 static int *macro_ptr, *macro_ptr_allocated;
307 static int *unget_saved_macro_ptr;
308 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
309 static int unget_buffer_enabled;
310 static int parse_flags;
311 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
312 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
313 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
314 token. line feed is also
315 returned at eof */
316 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
318 static Section *text_section, *data_section, *bss_section; /* predefined sections */
319 static Section *cur_text_section; /* current section where function code is
320 generated */
321 static Section *last_text_section; /* to handle .previous asm directive */
322 /* bound check related sections */
323 static Section *bounds_section; /* contains global data bound description */
324 static Section *lbounds_section; /* contains local data bound description */
325 /* symbol sections */
326 static Section *symtab_section, *strtab_section;
328 /* debug sections */
329 static Section *stab_section, *stabstr_section;
331 /* loc : local variable index
332 ind : output code index
333 rsym: return symbol
334 anon_sym: anonymous symbol index
336 static int rsym, anon_sym, ind, loc;
337 /* expression generation modifiers */
338 static int const_wanted; /* true if constant wanted */
339 static int nocode_wanted; /* true if no code generation wanted for an expression */
340 static int global_expr; /* true if compound literals must be allocated
341 globally (used during initializers parsing */
342 static CType func_vt; /* current function return type (used by return
343 instruction) */
344 static int func_vc;
345 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
346 static int tok_ident;
347 static TokenSym **table_ident;
348 static TokenSym *hash_ident[TOK_HASH_SIZE];
349 static char token_buf[STRING_MAX_SIZE + 1];
350 static char *funcname;
351 static Sym *global_stack, *local_stack;
352 static Sym *define_stack;
353 static Sym *global_label_stack, *local_label_stack;
355 static SValue vstack[VSTACK_SIZE], *vtop;
356 /* some predefined types */
357 static CType char_pointer_type, func_old_type, int_type;
358 /* true if isid(c) || isnum(c) */
359 static unsigned char isidnum_table[256];
361 /* compile with debug symbol (and use them if error during execution) */
362 static int do_debug = 0;
364 /* compile with built-in memory and bounds checker */
365 static int do_bounds_check = 0;
367 /* display benchmark infos */
368 #if !defined(LIBTCC)
369 static int do_bench = 0;
370 #endif
371 static int total_lines;
372 static int total_bytes;
374 /* use GNU C extensions */
375 static int gnu_ext = 1;
377 /* use Tiny C extensions */
378 static int tcc_ext = 1;
380 /* max number of callers shown if error */
381 static int num_callers = 6;
382 static const char **rt_bound_error_msg;
384 /* XXX: get rid of this ASAP */
385 static struct TCCState *tcc_state;
387 /* give the path of the tcc libraries */
388 static const char *tcc_lib_path = CONFIG_TCC_LIBDIR "/tcc";
390 struct TCCState {
391 int output_type;
393 BufferedFile **include_stack_ptr;
394 int *ifdef_stack_ptr;
396 /* include file handling */
397 char **include_paths;
398 int nb_include_paths;
399 char **sysinclude_paths;
400 int nb_sysinclude_paths;
401 CachedInclude **cached_includes;
402 int nb_cached_includes;
404 char **library_paths;
405 int nb_library_paths;
407 /* array of all loaded dlls (including those referenced by loaded
408 dlls) */
409 DLLReference **loaded_dlls;
410 int nb_loaded_dlls;
412 /* sections */
413 Section **sections;
414 int nb_sections; /* number of sections, including first dummy section */
416 /* got handling */
417 Section *got;
418 Section *plt;
419 unsigned long *got_offsets;
420 int nb_got_offsets;
421 /* give the correspondance from symtab indexes to dynsym indexes */
422 int *symtab_to_dynsym;
424 /* temporary dynamic symbol sections (for dll loading) */
425 Section *dynsymtab_section;
426 /* exported dynamic symbol section */
427 Section *dynsym;
429 int nostdinc; /* if true, no standard headers are added */
430 int nostdlib; /* if true, no standard libraries are added */
432 int nocommon; /* if true, do not use common symbols for .bss data */
434 /* if true, static linking is performed */
435 int static_link;
437 /* if true, all symbols are exported */
438 int rdynamic;
440 /* if true, only link in referenced objects from archive */
441 int alacarte_link;
443 /* address of text section */
444 unsigned long text_addr;
445 int has_text_addr;
447 /* output format, see TCC_OUTPUT_FORMAT_xxx */
448 int output_format;
450 /* C language options */
451 int char_is_unsigned;
453 /* warning switches */
454 int warn_write_strings;
455 int warn_unsupported;
456 int warn_error;
457 int warn_none;
458 int warn_implicit_function_declaration;
460 /* error handling */
461 void *error_opaque;
462 void (*error_func)(void *opaque, const char *msg);
463 int error_set_jmp_enabled;
464 jmp_buf error_jmp_buf;
465 int nb_errors;
467 /* tiny assembler state */
468 Sym *asm_labels;
470 /* see include_stack_ptr */
471 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
473 /* see ifdef_stack_ptr */
474 int ifdef_stack[IFDEF_STACK_SIZE];
477 /* The current value can be: */
478 #define VT_VALMASK 0x00ff
479 #define VT_CONST 0x00f0 /* constant in vc
480 (must be first non register value) */
481 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
482 #define VT_LOCAL 0x00f2 /* offset on stack */
483 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
484 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
485 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
486 #define VT_LVAL 0x0100 /* var is an lvalue */
487 #define VT_SYM 0x0200 /* a symbol value is added */
488 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
489 char/short stored in integer registers) */
490 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
491 dereferencing value */
492 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
493 bounding function call point is in vc */
494 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
495 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
496 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
497 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
499 /* types */
500 #define VT_INT 0 /* integer type */
501 #define VT_BYTE 1 /* signed byte type */
502 #define VT_SHORT 2 /* short type */
503 #define VT_VOID 3 /* void type */
504 #define VT_PTR 4 /* pointer */
505 #define VT_ENUM 5 /* enum definition */
506 #define VT_FUNC 6 /* function type */
507 #define VT_STRUCT 7 /* struct/union definition */
508 #define VT_FLOAT 8 /* IEEE float */
509 #define VT_DOUBLE 9 /* IEEE double */
510 #define VT_LDOUBLE 10 /* IEEE long double */
511 #define VT_BOOL 11 /* ISOC99 boolean type */
512 #define VT_LLONG 12 /* 64 bit integer */
513 #define VT_LONG 13 /* long integer (NEVER USED as type, only
514 during parsing) */
515 #define VT_BTYPE 0x000f /* mask for basic type */
516 #define VT_UNSIGNED 0x0010 /* unsigned type */
517 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
518 #define VT_BITFIELD 0x0040 /* bitfield modifier */
519 #define VT_CONSTANT 0x0800 /* const modifier */
520 #define VT_VOLATILE 0x1000 /* volatile modifier */
521 #define VT_SIGNED 0x2000 /* signed type */
523 /* storage */
524 #define VT_EXTERN 0x00000080 /* extern definition */
525 #define VT_STATIC 0x00000100 /* static variable */
526 #define VT_TYPEDEF 0x00000200 /* typedef definition */
527 #define VT_INLINE 0x00000400 /* inline definition */
529 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
531 /* type mask (except storage) */
532 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
533 #define VT_TYPE (~(VT_STORAGE))
535 /* token values */
537 /* warning: the following compare tokens depend on i386 asm code */
538 #define TOK_ULT 0x92
539 #define TOK_UGE 0x93
540 #define TOK_EQ 0x94
541 #define TOK_NE 0x95
542 #define TOK_ULE 0x96
543 #define TOK_UGT 0x97
544 #define TOK_LT 0x9c
545 #define TOK_GE 0x9d
546 #define TOK_LE 0x9e
547 #define TOK_GT 0x9f
549 #define TOK_LAND 0xa0
550 #define TOK_LOR 0xa1
552 #define TOK_DEC 0xa2
553 #define TOK_MID 0xa3 /* inc/dec, to void constant */
554 #define TOK_INC 0xa4
555 #define TOK_UDIV 0xb0 /* unsigned division */
556 #define TOK_UMOD 0xb1 /* unsigned modulo */
557 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
558 #define TOK_CINT 0xb3 /* number in tokc */
559 #define TOK_CCHAR 0xb4 /* char constant in tokc */
560 #define TOK_STR 0xb5 /* pointer to string in tokc */
561 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
562 #define TOK_LCHAR 0xb7
563 #define TOK_LSTR 0xb8
564 #define TOK_CFLOAT 0xb9 /* float constant */
565 #define TOK_LINENUM 0xba /* line number info */
566 #define TOK_CDOUBLE 0xc0 /* double constant */
567 #define TOK_CLDOUBLE 0xc1 /* long double constant */
568 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
569 #define TOK_ADDC1 0xc3 /* add with carry generation */
570 #define TOK_ADDC2 0xc4 /* add with carry use */
571 #define TOK_SUBC1 0xc5 /* add with carry generation */
572 #define TOK_SUBC2 0xc6 /* add with carry use */
573 #define TOK_CUINT 0xc8 /* unsigned int constant */
574 #define TOK_CLLONG 0xc9 /* long long constant */
575 #define TOK_CULLONG 0xca /* unsigned long long constant */
576 #define TOK_ARROW 0xcb
577 #define TOK_DOTS 0xcc /* three dots */
578 #define TOK_SHR 0xcd /* unsigned shift right */
579 #define TOK_PPNUM 0xce /* preprocessor number */
581 #define TOK_SHL 0x01 /* shift left */
582 #define TOK_SAR 0x02 /* signed shift right */
584 /* assignement operators : normal operator or 0x80 */
585 #define TOK_A_MOD 0xa5
586 #define TOK_A_AND 0xa6
587 #define TOK_A_MUL 0xaa
588 #define TOK_A_ADD 0xab
589 #define TOK_A_SUB 0xad
590 #define TOK_A_DIV 0xaf
591 #define TOK_A_XOR 0xde
592 #define TOK_A_OR 0xfc
593 #define TOK_A_SHL 0x81
594 #define TOK_A_SAR 0x82
596 #ifndef offsetof
597 #define offsetof(type, field) ((size_t) &((type *)0)->field)
598 #endif
600 #ifndef countof
601 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
602 #endif
604 /* WARNING: the content of this string encodes token numbers */
605 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";
607 #define TOK_EOF (-1) /* end of file */
608 #define TOK_LINEFEED 10 /* line feed */
610 /* all identificators and strings have token above that */
611 #define TOK_IDENT 256
613 /* only used for i386 asm opcodes definitions */
614 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
616 #define DEF_BWL(x) \
617 DEF(TOK_ASM_ ## x ## b, #x "b") \
618 DEF(TOK_ASM_ ## x ## w, #x "w") \
619 DEF(TOK_ASM_ ## x ## l, #x "l") \
620 DEF(TOK_ASM_ ## x, #x)
622 #define DEF_WL(x) \
623 DEF(TOK_ASM_ ## x ## w, #x "w") \
624 DEF(TOK_ASM_ ## x ## l, #x "l") \
625 DEF(TOK_ASM_ ## x, #x)
627 #define DEF_FP1(x) \
628 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
629 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
630 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
631 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
633 #define DEF_FP(x) \
634 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
635 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
636 DEF_FP1(x)
638 #define DEF_ASMTEST(x) \
639 DEF_ASM(x ## o) \
640 DEF_ASM(x ## no) \
641 DEF_ASM(x ## b) \
642 DEF_ASM(x ## c) \
643 DEF_ASM(x ## nae) \
644 DEF_ASM(x ## nb) \
645 DEF_ASM(x ## nc) \
646 DEF_ASM(x ## ae) \
647 DEF_ASM(x ## e) \
648 DEF_ASM(x ## z) \
649 DEF_ASM(x ## ne) \
650 DEF_ASM(x ## nz) \
651 DEF_ASM(x ## be) \
652 DEF_ASM(x ## na) \
653 DEF_ASM(x ## nbe) \
654 DEF_ASM(x ## a) \
655 DEF_ASM(x ## s) \
656 DEF_ASM(x ## ns) \
657 DEF_ASM(x ## p) \
658 DEF_ASM(x ## pe) \
659 DEF_ASM(x ## np) \
660 DEF_ASM(x ## po) \
661 DEF_ASM(x ## l) \
662 DEF_ASM(x ## nge) \
663 DEF_ASM(x ## nl) \
664 DEF_ASM(x ## ge) \
665 DEF_ASM(x ## le) \
666 DEF_ASM(x ## ng) \
667 DEF_ASM(x ## nle) \
668 DEF_ASM(x ## g)
670 #define TOK_ASM_int TOK_INT
672 enum tcc_token {
673 TOK_LAST = TOK_IDENT - 1,
674 #define DEF(id, str) id,
675 #include "tcctok.h"
676 #undef DEF
679 static const char tcc_keywords[] =
680 #define DEF(id, str) str "\0"
681 #include "tcctok.h"
682 #undef DEF
685 #define TOK_UIDENT TOK_DEFINE
687 #ifdef WIN32
688 #define snprintf _snprintf
689 #define vsnprintf _vsnprintf
690 #endif
692 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
693 /* currently incorrect */
694 long double strtold(const char *nptr, char **endptr)
696 return (long double)strtod(nptr, endptr);
698 float strtof(const char *nptr, char **endptr)
700 return (float)strtod(nptr, endptr);
702 #else
703 /* XXX: need to define this to use them in non ISOC99 context */
704 extern float strtof (const char *__nptr, char **__endptr);
705 extern long double strtold (const char *__nptr, char **__endptr);
706 #endif
708 static char *pstrcpy(char *buf, int buf_size, const char *s);
709 static char *pstrcat(char *buf, int buf_size, const char *s);
711 static void next(void);
712 static void next_nomacro(void);
713 static void parse_expr_type(CType *type);
714 static void expr_type(CType *type);
715 static void unary_type(CType *type);
716 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
717 int case_reg, int is_expr);
718 static int expr_const(void);
719 static void expr_eq(void);
720 static void gexpr(void);
721 static void gen_inline_functions(void);
722 static void decl(int l);
723 static void decl_initializer(CType *type, Section *sec, unsigned long c,
724 int first, int size_only);
725 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
726 int has_init, int v, int scope);
727 int gv(int rc);
728 void gv2(int rc1, int rc2);
729 void move_reg(int r, int s);
730 void save_regs(int n);
731 void save_reg(int r);
732 void vpop(void);
733 void vswap(void);
734 void vdup(void);
735 int get_reg(int rc);
736 int get_reg_ex(int rc,int rc2);
738 static void macro_subst(TokenString *tok_str, Sym **nested_list,
739 const int *macro_str, int can_read_stream);
740 void gen_op(int op);
741 void force_charshort_cast(int t);
742 static void gen_cast(CType *type);
743 void vstore(void);
744 static Sym *sym_find(int v);
745 static Sym *sym_push(int v, CType *type, int r, int c);
747 /* type handling */
748 static int type_size(CType *type, int *a);
749 static inline CType *pointed_type(CType *type);
750 static int pointed_size(CType *type);
751 static int lvalue_type(int t);
752 static int parse_btype(CType *type, AttributeDef *ad);
753 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
754 static int is_compatible_types(CType *type1, CType *type2);
756 int ieee_finite(double d);
757 void error(const char *fmt, ...);
758 void vpushi(int v);
759 void vrott(int n);
760 void vnrott(int n);
761 void lexpand_nr(void);
762 static void vpush_global_sym(CType *type, int v);
763 void vset(CType *type, int r, int v);
764 void type_to_str(char *buf, int buf_size,
765 CType *type, const char *varstr);
766 char *get_tok_str(int v, CValue *cv);
767 static Sym *get_sym_ref(CType *type, Section *sec,
768 unsigned long offset, unsigned long size);
769 static Sym *external_global_sym(int v, CType *type, int r);
771 /* section generation */
772 static void section_realloc(Section *sec, unsigned long new_size);
773 static void *section_ptr_add(Section *sec, unsigned long size);
774 static void put_extern_sym(Sym *sym, Section *section,
775 unsigned long value, unsigned long size);
776 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
777 static int put_elf_str(Section *s, const char *sym);
778 static int put_elf_sym(Section *s,
779 unsigned long value, unsigned long size,
780 int info, int other, int shndx, const char *name);
781 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
782 int info, int sh_num, const char *name);
783 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
784 int type, int symbol);
785 static void put_stabs(const char *str, int type, int other, int desc,
786 unsigned long value);
787 static void put_stabs_r(const char *str, int type, int other, int desc,
788 unsigned long value, Section *sec, int sym_index);
789 static void put_stabn(int type, int other, int desc, int value);
790 static void put_stabd(int type, int other, int desc);
791 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
793 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
794 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
795 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
797 /* tcccoff.c */
798 int tcc_output_coff(TCCState *s1, FILE *f);
800 /* tccasm.c */
802 #ifdef CONFIG_TCC_ASM
804 typedef struct ExprValue {
805 uint32_t v;
806 Sym *sym;
807 } ExprValue;
809 #define MAX_ASM_OPERANDS 30
811 typedef struct ASMOperand {
812 int id; /* GCC 3 optionnal identifier (0 if number only supported */
813 char *constraint;
814 char asm_str[16]; /* computed asm string for operand */
815 SValue *vt; /* C value of the expression */
816 int ref_index; /* if >= 0, gives reference to a output constraint */
817 int input_index; /* if >= 0, gives reference to an input constraint */
818 int priority; /* priority, used to assign registers */
819 int reg; /* if >= 0, register number used for this operand */
820 int is_llong; /* true if double register value */
821 int is_memory; /* true if memory operand */
822 int is_rw; /* for '+' modifier */
823 } ASMOperand;
825 static void asm_expr(TCCState *s1, ExprValue *pe);
826 static int asm_int_expr(TCCState *s1);
827 static int find_constraint(ASMOperand *operands, int nb_operands,
828 const char *name, const char **pp);
830 static int tcc_assemble(TCCState *s1, int do_preprocess);
832 #endif
834 static void asm_instr(void);
835 static void asm_global_instr(void);
837 /* true if float/double/long double type */
838 static inline int is_float(int t)
840 int bt;
841 bt = t & VT_BTYPE;
842 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
845 #ifdef TCC_TARGET_I386
846 #include "i386-gen.c"
847 #endif
849 #ifdef TCC_TARGET_ARM
850 #include "arm-gen.c"
851 #endif
853 #ifdef TCC_TARGET_C67
854 #include "c67-gen.c"
855 #endif
857 #ifdef CONFIG_TCC_STATIC
859 #define RTLD_LAZY 0x001
860 #define RTLD_NOW 0x002
861 #define RTLD_GLOBAL 0x100
862 #define RTLD_DEFAULT NULL
864 /* dummy function for profiling */
865 void *dlopen(const char *filename, int flag)
867 return NULL;
870 const char *dlerror(void)
872 return "error";
875 typedef struct TCCSyms {
876 char *str;
877 void *ptr;
878 } TCCSyms;
880 #define TCCSYM(a) { #a, &a, },
882 /* add the symbol you want here if no dynamic linking is done */
883 static TCCSyms tcc_syms[] = {
884 #if !defined(CONFIG_TCCBOOT)
885 TCCSYM(printf)
886 TCCSYM(fprintf)
887 TCCSYM(fopen)
888 TCCSYM(fclose)
889 #endif
890 { NULL, NULL },
893 void *dlsym(void *handle, const char *symbol)
895 TCCSyms *p;
896 p = tcc_syms;
897 while (p->str != NULL) {
898 if (!strcmp(p->str, symbol))
899 return p->ptr;
900 p++;
902 return NULL;
905 #endif
907 /********************************************************/
909 /* we use our own 'finite' function to avoid potential problems with
910 non standard math libs */
911 /* XXX: endianness dependent */
912 int ieee_finite(double d)
914 int *p = (int *)&d;
915 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
918 /* copy a string and truncate it. */
919 static char *pstrcpy(char *buf, int buf_size, const char *s)
921 char *q, *q_end;
922 int c;
924 if (buf_size > 0) {
925 q = buf;
926 q_end = buf + buf_size - 1;
927 while (q < q_end) {
928 c = *s++;
929 if (c == '\0')
930 break;
931 *q++ = c;
933 *q = '\0';
935 return buf;
938 /* strcat and truncate. */
939 static char *pstrcat(char *buf, int buf_size, const char *s)
941 int len;
942 len = strlen(buf);
943 if (len < buf_size)
944 pstrcpy(buf + len, buf_size - len, s);
945 return buf;
948 static int strstart(const char *str, const char *val, const char **ptr)
950 const char *p, *q;
951 p = str;
952 q = val;
953 while (*q != '\0') {
954 if (*p != *q)
955 return 0;
956 p++;
957 q++;
959 if (ptr)
960 *ptr = p;
961 return 1;
964 /* memory management */
965 #ifdef MEM_DEBUG
966 int mem_cur_size;
967 int mem_max_size;
968 #endif
970 static inline void tcc_free(void *ptr)
972 #ifdef MEM_DEBUG
973 mem_cur_size -= malloc_usable_size(ptr);
974 #endif
975 free(ptr);
978 static void *tcc_malloc(unsigned long size)
980 void *ptr;
981 ptr = malloc(size);
982 if (!ptr && size)
983 error("memory full");
984 #ifdef MEM_DEBUG
985 mem_cur_size += malloc_usable_size(ptr);
986 if (mem_cur_size > mem_max_size)
987 mem_max_size = mem_cur_size;
988 #endif
989 return ptr;
992 static void *tcc_mallocz(unsigned long size)
994 void *ptr;
995 ptr = tcc_malloc(size);
996 memset(ptr, 0, size);
997 return ptr;
1000 static inline void *tcc_realloc(void *ptr, unsigned long size)
1002 void *ptr1;
1003 #ifdef MEM_DEBUG
1004 mem_cur_size -= malloc_usable_size(ptr);
1005 #endif
1006 ptr1 = realloc(ptr, size);
1007 #ifdef MEM_DEBUG
1008 /* NOTE: count not correct if alloc error, but not critical */
1009 mem_cur_size += malloc_usable_size(ptr1);
1010 if (mem_cur_size > mem_max_size)
1011 mem_max_size = mem_cur_size;
1012 #endif
1013 return ptr1;
1016 static char *tcc_strdup(const char *str)
1018 char *ptr;
1019 ptr = tcc_malloc(strlen(str) + 1);
1020 strcpy(ptr, str);
1021 return ptr;
1024 #define free(p) use_tcc_free(p)
1025 #define malloc(s) use_tcc_malloc(s)
1026 #define realloc(p, s) use_tcc_realloc(p, s)
1028 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1030 int nb, nb_alloc;
1031 void **pp;
1033 nb = *nb_ptr;
1034 pp = *ptab;
1035 /* every power of two we double array size */
1036 if ((nb & (nb - 1)) == 0) {
1037 if (!nb)
1038 nb_alloc = 1;
1039 else
1040 nb_alloc = nb * 2;
1041 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1042 if (!pp)
1043 error("memory full");
1044 *ptab = pp;
1046 pp[nb++] = data;
1047 *nb_ptr = nb;
1050 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1052 Section *sec;
1054 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1055 strcpy(sec->name, name);
1056 sec->sh_type = sh_type;
1057 sec->sh_flags = sh_flags;
1058 switch(sh_type) {
1059 case SHT_HASH:
1060 case SHT_REL:
1061 case SHT_DYNSYM:
1062 case SHT_SYMTAB:
1063 case SHT_DYNAMIC:
1064 sec->sh_addralign = 4;
1065 break;
1066 case SHT_STRTAB:
1067 sec->sh_addralign = 1;
1068 break;
1069 default:
1070 sec->sh_addralign = 32; /* default conservative alignment */
1071 break;
1074 /* only add section if not private */
1075 if (!(sh_flags & SHF_PRIVATE)) {
1076 sec->sh_num = s1->nb_sections;
1077 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1079 return sec;
1082 static void free_section(Section *s)
1084 tcc_free(s->data);
1085 tcc_free(s);
1088 /* realloc section and set its content to zero */
1089 static void section_realloc(Section *sec, unsigned long new_size)
1091 unsigned long size;
1092 unsigned char *data;
1094 size = sec->data_allocated;
1095 if (size == 0)
1096 size = 1;
1097 while (size < new_size)
1098 size = size * 2;
1099 data = tcc_realloc(sec->data, size);
1100 if (!data)
1101 error("memory full");
1102 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1103 sec->data = data;
1104 sec->data_allocated = size;
1107 /* reserve at least 'size' bytes in section 'sec' from
1108 sec->data_offset. */
1109 static void *section_ptr_add(Section *sec, unsigned long size)
1111 unsigned long offset, offset1;
1113 offset = sec->data_offset;
1114 offset1 = offset + size;
1115 if (offset1 > sec->data_allocated)
1116 section_realloc(sec, offset1);
1117 sec->data_offset = offset1;
1118 return sec->data + offset;
1121 /* return a reference to a section, and create it if it does not
1122 exists */
1123 Section *find_section(TCCState *s1, const char *name)
1125 Section *sec;
1126 int i;
1127 for(i = 1; i < s1->nb_sections; i++) {
1128 sec = s1->sections[i];
1129 if (!strcmp(name, sec->name))
1130 return sec;
1132 /* sections are created as PROGBITS */
1133 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1136 #define SECTION_ABS ((void *)1)
1138 /* update sym->c so that it points to an external symbol in section
1139 'section' with value 'value' */
1140 static void put_extern_sym(Sym *sym, Section *section,
1141 unsigned long value, unsigned long size)
1143 int sym_type, sym_bind, sh_num, info;
1144 Elf32_Sym *esym;
1145 const char *name;
1147 if (section == NULL)
1148 sh_num = SHN_UNDEF;
1149 else if (section == SECTION_ABS)
1150 sh_num = SHN_ABS;
1151 else
1152 sh_num = section->sh_num;
1153 if (!sym->c) {
1154 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1155 sym_type = STT_FUNC;
1156 else
1157 sym_type = STT_OBJECT;
1158 if (sym->type.t & VT_STATIC)
1159 sym_bind = STB_LOCAL;
1160 else
1161 sym_bind = STB_GLOBAL;
1163 name = get_tok_str(sym->v, NULL);
1164 #ifdef CONFIG_TCC_BCHECK
1165 if (do_bounds_check) {
1166 char buf[32];
1168 /* XXX: avoid doing that for statics ? */
1169 /* if bound checking is activated, we change some function
1170 names by adding the "__bound" prefix */
1171 switch(sym->v) {
1172 #if 0
1173 /* XXX: we rely only on malloc hooks */
1174 case TOK_malloc:
1175 case TOK_free:
1176 case TOK_realloc:
1177 case TOK_memalign:
1178 case TOK_calloc:
1179 #endif
1180 case TOK_memcpy:
1181 case TOK_memmove:
1182 case TOK_memset:
1183 case TOK_strlen:
1184 case TOK_strcpy:
1185 strcpy(buf, "__bound_");
1186 strcat(buf, name);
1187 name = buf;
1188 break;
1191 #endif
1192 info = ELF32_ST_INFO(sym_bind, sym_type);
1193 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1194 } else {
1195 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1196 esym->st_value = value;
1197 esym->st_size = size;
1198 esym->st_shndx = sh_num;
1202 /* add a new relocation entry to symbol 'sym' in section 's' */
1203 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1205 if (!sym->c)
1206 put_extern_sym(sym, NULL, 0, 0);
1207 /* now we can add ELF relocation info */
1208 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1211 static inline int isid(int c)
1213 return (c >= 'a' && c <= 'z') ||
1214 (c >= 'A' && c <= 'Z') ||
1215 c == '_';
1218 static inline int isnum(int c)
1220 return c >= '0' && c <= '9';
1223 static inline int isoct(int c)
1225 return c >= '0' && c <= '7';
1228 static inline int toup(int c)
1230 if (c >= 'a' && c <= 'z')
1231 return c - 'a' + 'A';
1232 else
1233 return c;
1236 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1238 int len;
1239 len = strlen(buf);
1240 vsnprintf(buf + len, buf_size - len, fmt, ap);
1243 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1245 va_list ap;
1246 va_start(ap, fmt);
1247 strcat_vprintf(buf, buf_size, fmt, ap);
1248 va_end(ap);
1251 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1253 char buf[2048];
1254 BufferedFile **f;
1256 buf[0] = '\0';
1257 if (file) {
1258 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1259 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1260 (*f)->filename, (*f)->line_num);
1261 if (file->line_num > 0) {
1262 strcat_printf(buf, sizeof(buf),
1263 "%s:%d: ", file->filename, file->line_num);
1264 } else {
1265 strcat_printf(buf, sizeof(buf),
1266 "%s: ", file->filename);
1268 } else {
1269 strcat_printf(buf, sizeof(buf),
1270 "tcc: ");
1272 if (is_warning)
1273 strcat_printf(buf, sizeof(buf), "warning: ");
1274 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1276 if (!s1->error_func) {
1277 /* default case: stderr */
1278 fprintf(stderr, "%s\n", buf);
1279 } else {
1280 s1->error_func(s1->error_opaque, buf);
1282 if (!is_warning || s1->warn_error)
1283 s1->nb_errors++;
1286 #ifdef LIBTCC
1287 void tcc_set_error_func(TCCState *s, void *error_opaque,
1288 void (*error_func)(void *opaque, const char *msg))
1290 s->error_opaque = error_opaque;
1291 s->error_func = error_func;
1293 #endif
1295 /* error without aborting current compilation */
1296 void error_noabort(const char *fmt, ...)
1298 TCCState *s1 = tcc_state;
1299 va_list ap;
1301 va_start(ap, fmt);
1302 error1(s1, 0, fmt, ap);
1303 va_end(ap);
1306 void error(const char *fmt, ...)
1308 TCCState *s1 = tcc_state;
1309 va_list ap;
1311 va_start(ap, fmt);
1312 error1(s1, 0, fmt, ap);
1313 va_end(ap);
1314 /* better than nothing: in some cases, we accept to handle errors */
1315 if (s1->error_set_jmp_enabled) {
1316 longjmp(s1->error_jmp_buf, 1);
1317 } else {
1318 /* XXX: eliminate this someday */
1319 exit(1);
1323 void expect(const char *msg)
1325 error("%s expected", msg);
1328 void warning(const char *fmt, ...)
1330 TCCState *s1 = tcc_state;
1331 va_list ap;
1333 if (s1->warn_none)
1334 return;
1336 va_start(ap, fmt);
1337 error1(s1, 1, fmt, ap);
1338 va_end(ap);
1341 void skip(int c)
1343 if (tok != c)
1344 error("'%c' expected", c);
1345 next();
1348 static void test_lvalue(void)
1350 if (!(vtop->r & VT_LVAL))
1351 expect("lvalue");
1354 /* allocate a new token */
1355 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1357 TokenSym *ts, **ptable;
1358 int i;
1360 if (tok_ident >= SYM_FIRST_ANOM)
1361 error("memory full");
1363 /* expand token table if needed */
1364 i = tok_ident - TOK_IDENT;
1365 if ((i % TOK_ALLOC_INCR) == 0) {
1366 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1367 if (!ptable)
1368 error("memory full");
1369 table_ident = ptable;
1372 ts = tcc_malloc(sizeof(TokenSym) + len);
1373 table_ident[i] = ts;
1374 ts->tok = tok_ident++;
1375 ts->sym_define = NULL;
1376 ts->sym_label = NULL;
1377 ts->sym_struct = NULL;
1378 ts->sym_identifier = NULL;
1379 ts->len = len;
1380 ts->hash_next = NULL;
1381 memcpy(ts->str, str, len);
1382 ts->str[len] = '\0';
1383 *pts = ts;
1384 return ts;
1387 #define TOK_HASH_INIT 1
1388 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1390 /* find a token and add it if not found */
1391 static TokenSym *tok_alloc(const char *str, int len)
1393 TokenSym *ts, **pts;
1394 int i;
1395 unsigned int h;
1397 h = TOK_HASH_INIT;
1398 for(i=0;i<len;i++)
1399 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1400 h &= (TOK_HASH_SIZE - 1);
1402 pts = &hash_ident[h];
1403 for(;;) {
1404 ts = *pts;
1405 if (!ts)
1406 break;
1407 if (ts->len == len && !memcmp(ts->str, str, len))
1408 return ts;
1409 pts = &(ts->hash_next);
1411 return tok_alloc_new(pts, str, len);
1414 /* CString handling */
1416 static void cstr_realloc(CString *cstr, int new_size)
1418 int size;
1419 void *data;
1421 size = cstr->size_allocated;
1422 if (size == 0)
1423 size = 8; /* no need to allocate a too small first string */
1424 while (size < new_size)
1425 size = size * 2;
1426 data = tcc_realloc(cstr->data_allocated, size);
1427 if (!data)
1428 error("memory full");
1429 cstr->data_allocated = data;
1430 cstr->size_allocated = size;
1431 cstr->data = data;
1434 /* add a byte */
1435 static void cstr_ccat(CString *cstr, int ch)
1437 int size;
1438 size = cstr->size + 1;
1439 if (size > cstr->size_allocated)
1440 cstr_realloc(cstr, size);
1441 ((unsigned char *)cstr->data)[size - 1] = ch;
1442 cstr->size = size;
1445 static void cstr_cat(CString *cstr, const char *str)
1447 int c;
1448 for(;;) {
1449 c = *str;
1450 if (c == '\0')
1451 break;
1452 cstr_ccat(cstr, c);
1453 str++;
1457 /* add a wide char */
1458 static void cstr_wccat(CString *cstr, int ch)
1460 int size;
1461 size = cstr->size + sizeof(int);
1462 if (size > cstr->size_allocated)
1463 cstr_realloc(cstr, size);
1464 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1465 cstr->size = size;
1468 static void cstr_new(CString *cstr)
1470 memset(cstr, 0, sizeof(CString));
1473 /* free string and reset it to NULL */
1474 static void cstr_free(CString *cstr)
1476 tcc_free(cstr->data_allocated);
1477 cstr_new(cstr);
1480 #define cstr_reset(cstr) cstr_free(cstr)
1482 static CString *cstr_dup(CString *cstr1)
1484 CString *cstr;
1485 int size;
1487 cstr = tcc_malloc(sizeof(CString));
1488 size = cstr1->size;
1489 cstr->size = size;
1490 cstr->size_allocated = size;
1491 cstr->data_allocated = tcc_malloc(size);
1492 cstr->data = cstr->data_allocated;
1493 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1494 return cstr;
1497 /* XXX: unicode ? */
1498 static void add_char(CString *cstr, int c)
1500 if (c == '\'' || c == '\"' || c == '\\') {
1501 /* XXX: could be more precise if char or string */
1502 cstr_ccat(cstr, '\\');
1504 if (c >= 32 && c <= 126) {
1505 cstr_ccat(cstr, c);
1506 } else {
1507 cstr_ccat(cstr, '\\');
1508 if (c == '\n') {
1509 cstr_ccat(cstr, 'n');
1510 } else {
1511 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1512 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1513 cstr_ccat(cstr, '0' + (c & 7));
1518 /* XXX: buffer overflow */
1519 /* XXX: float tokens */
1520 char *get_tok_str(int v, CValue *cv)
1522 static char buf[STRING_MAX_SIZE + 1];
1523 static CString cstr_buf;
1524 CString *cstr;
1525 unsigned char *q;
1526 char *p;
1527 int i, len;
1529 /* NOTE: to go faster, we give a fixed buffer for small strings */
1530 cstr_reset(&cstr_buf);
1531 cstr_buf.data = buf;
1532 cstr_buf.size_allocated = sizeof(buf);
1533 p = buf;
1535 switch(v) {
1536 case TOK_CINT:
1537 case TOK_CUINT:
1538 /* XXX: not quite exact, but only useful for testing */
1539 sprintf(p, "%u", cv->ui);
1540 break;
1541 case TOK_CLLONG:
1542 case TOK_CULLONG:
1543 /* XXX: not quite exact, but only useful for testing */
1544 sprintf(p, "%Lu", cv->ull);
1545 break;
1546 case TOK_CCHAR:
1547 case TOK_LCHAR:
1548 cstr_ccat(&cstr_buf, '\'');
1549 add_char(&cstr_buf, cv->i);
1550 cstr_ccat(&cstr_buf, '\'');
1551 cstr_ccat(&cstr_buf, '\0');
1552 break;
1553 case TOK_PPNUM:
1554 cstr = cv->cstr;
1555 len = cstr->size - 1;
1556 for(i=0;i<len;i++)
1557 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1558 cstr_ccat(&cstr_buf, '\0');
1559 break;
1560 case TOK_STR:
1561 case TOK_LSTR:
1562 cstr = cv->cstr;
1563 cstr_ccat(&cstr_buf, '\"');
1564 if (v == TOK_STR) {
1565 len = cstr->size - 1;
1566 for(i=0;i<len;i++)
1567 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1568 } else {
1569 len = (cstr->size / sizeof(int)) - 1;
1570 for(i=0;i<len;i++)
1571 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1573 cstr_ccat(&cstr_buf, '\"');
1574 cstr_ccat(&cstr_buf, '\0');
1575 break;
1576 case TOK_LT:
1577 v = '<';
1578 goto addv;
1579 case TOK_GT:
1580 v = '>';
1581 goto addv;
1582 case TOK_A_SHL:
1583 return strcpy(p, "<<=");
1584 case TOK_A_SAR:
1585 return strcpy(p, ">>=");
1586 default:
1587 if (v < TOK_IDENT) {
1588 /* search in two bytes table */
1589 q = tok_two_chars;
1590 while (*q) {
1591 if (q[2] == v) {
1592 *p++ = q[0];
1593 *p++ = q[1];
1594 *p = '\0';
1595 return buf;
1597 q += 3;
1599 addv:
1600 *p++ = v;
1601 *p = '\0';
1602 } else if (v < tok_ident) {
1603 return table_ident[v - TOK_IDENT]->str;
1604 } else if (v >= SYM_FIRST_ANOM) {
1605 /* special name for anonymous symbol */
1606 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1607 } else {
1608 /* should never happen */
1609 return NULL;
1611 break;
1613 return cstr_buf.data;
1616 /* push, without hashing */
1617 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1619 Sym *s;
1620 s = tcc_malloc(sizeof(Sym));
1621 s->v = v;
1622 s->type.t = t;
1623 s->c = c;
1624 s->next = NULL;
1625 /* add in stack */
1626 s->prev = *ps;
1627 *ps = s;
1628 return s;
1631 /* find a symbol and return its associated structure. 's' is the top
1632 of the symbol stack */
1633 static Sym *sym_find2(Sym *s, int v)
1635 while (s) {
1636 if (s->v == v)
1637 return s;
1638 s = s->prev;
1640 return NULL;
1643 /* structure lookup */
1644 static inline Sym *struct_find(int v)
1646 v -= TOK_IDENT;
1647 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1648 return NULL;
1649 return table_ident[v]->sym_struct;
1652 /* find an identifier */
1653 static inline Sym *sym_find(int v)
1655 v -= TOK_IDENT;
1656 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1657 return NULL;
1658 return table_ident[v]->sym_identifier;
1661 /* push a given symbol on the symbol stack */
1662 static Sym *sym_push(int v, CType *type, int r, int c)
1664 Sym *s, **ps;
1665 TokenSym *ts;
1667 if (local_stack)
1668 ps = &local_stack;
1669 else
1670 ps = &global_stack;
1671 s = sym_push2(ps, v, type->t, c);
1672 s->type.ref = type->ref;
1673 s->r = r;
1674 /* don't record fields or anonymous symbols */
1675 /* XXX: simplify */
1676 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1677 /* record symbol in token array */
1678 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1679 if (v & SYM_STRUCT)
1680 ps = &ts->sym_struct;
1681 else
1682 ps = &ts->sym_identifier;
1683 s->prev_tok = *ps;
1684 *ps = s;
1686 return s;
1689 /* push a global identifier */
1690 static Sym *global_identifier_push(int v, int t, int c)
1692 Sym *s, **ps;
1693 s = sym_push2(&global_stack, v, t, c);
1694 /* don't record anonymous symbol */
1695 if (v < SYM_FIRST_ANOM) {
1696 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1697 /* modify the top most local identifier, so that
1698 sym_identifier will point to 's' when popped */
1699 while (*ps != NULL)
1700 ps = &(*ps)->prev_tok;
1701 s->prev_tok = NULL;
1702 *ps = s;
1704 return s;
1707 /* pop symbols until top reaches 'b' */
1708 static void sym_pop(Sym **ptop, Sym *b)
1710 Sym *s, *ss, **ps;
1711 TokenSym *ts;
1712 int v;
1714 s = *ptop;
1715 while(s != b) {
1716 ss = s->prev;
1717 v = s->v;
1718 /* remove symbol in token array */
1719 /* XXX: simplify */
1720 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1721 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1722 if (v & SYM_STRUCT)
1723 ps = &ts->sym_struct;
1724 else
1725 ps = &ts->sym_identifier;
1726 *ps = s->prev_tok;
1728 tcc_free(s);
1729 s = ss;
1731 *ptop = b;
1734 /* I/O layer */
1736 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1738 int fd;
1739 BufferedFile *bf;
1741 fd = open(filename, O_RDONLY | O_BINARY);
1742 if (fd < 0)
1743 return NULL;
1744 bf = tcc_malloc(sizeof(BufferedFile));
1745 if (!bf) {
1746 close(fd);
1747 return NULL;
1749 bf->fd = fd;
1750 bf->buf_ptr = bf->buffer;
1751 bf->buf_end = bf->buffer;
1752 bf->buffer[0] = CH_EOB; /* put eob symbol */
1753 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1754 bf->line_num = 1;
1755 bf->ifndef_macro = 0;
1756 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1757 // printf("opening '%s'\n", filename);
1758 return bf;
1761 void tcc_close(BufferedFile *bf)
1763 total_lines += bf->line_num;
1764 close(bf->fd);
1765 tcc_free(bf);
1768 /* fill input buffer and peek next char */
1769 static int tcc_peekc_slow(BufferedFile *bf)
1771 int len;
1772 /* only tries to read if really end of buffer */
1773 if (bf->buf_ptr >= bf->buf_end) {
1774 if (bf->fd != -1) {
1775 #if defined(PARSE_DEBUG)
1776 len = 8;
1777 #else
1778 len = IO_BUF_SIZE;
1779 #endif
1780 len = read(bf->fd, bf->buffer, len);
1781 if (len < 0)
1782 len = 0;
1783 } else {
1784 len = 0;
1786 total_bytes += len;
1787 bf->buf_ptr = bf->buffer;
1788 bf->buf_end = bf->buffer + len;
1789 *bf->buf_end = CH_EOB;
1791 if (bf->buf_ptr < bf->buf_end) {
1792 return bf->buf_ptr[0];
1793 } else {
1794 bf->buf_ptr = bf->buf_end;
1795 return CH_EOF;
1799 /* return the current character, handling end of block if necessary
1800 (but not stray) */
1801 static int handle_eob(void)
1803 return tcc_peekc_slow(file);
1806 /* read next char from current input file and handle end of input buffer */
1807 static inline void inp(void)
1809 ch = *(++(file->buf_ptr));
1810 /* end of buffer/file handling */
1811 if (ch == CH_EOB)
1812 ch = handle_eob();
1815 /* handle '\[\r]\n' */
1816 static void handle_stray(void)
1818 while (ch == '\\') {
1819 inp();
1820 if (ch == '\n') {
1821 file->line_num++;
1822 inp();
1823 } else if (ch == '\r') {
1824 inp();
1825 if (ch != '\n')
1826 goto fail;
1827 file->line_num++;
1828 inp();
1829 } else {
1830 fail:
1831 error("stray '\\' in program");
1836 /* skip the stray and handle the \\n case. Output an error if
1837 incorrect char after the stray */
1838 static int handle_stray1(uint8_t *p)
1840 int c;
1842 if (p >= file->buf_end) {
1843 file->buf_ptr = p;
1844 c = handle_eob();
1845 p = file->buf_ptr;
1846 if (c == '\\')
1847 goto parse_stray;
1848 } else {
1849 parse_stray:
1850 file->buf_ptr = p;
1851 ch = *p;
1852 handle_stray();
1853 p = file->buf_ptr;
1854 c = *p;
1856 return c;
1859 /* handle just the EOB case, but not stray */
1860 #define PEEKC_EOB(c, p)\
1862 p++;\
1863 c = *p;\
1864 if (c == '\\') {\
1865 file->buf_ptr = p;\
1866 c = handle_eob();\
1867 p = file->buf_ptr;\
1871 /* handle the complicated stray case */
1872 #define PEEKC(c, p)\
1874 p++;\
1875 c = *p;\
1876 if (c == '\\') {\
1877 c = handle_stray1(p);\
1878 p = file->buf_ptr;\
1882 /* input with '\[\r]\n' handling. Note that this function cannot
1883 handle other characters after '\', so you cannot call it inside
1884 strings or comments */
1885 static void minp(void)
1887 inp();
1888 if (ch == '\\')
1889 handle_stray();
1893 /* single line C++ comments */
1894 static uint8_t *parse_line_comment(uint8_t *p)
1896 int c;
1898 p++;
1899 for(;;) {
1900 c = *p;
1901 redo:
1902 if (c == '\n' || c == CH_EOF) {
1903 break;
1904 } else if (c == '\\') {
1905 file->buf_ptr = p;
1906 c = handle_eob();
1907 p = file->buf_ptr;
1908 if (c == '\\') {
1909 PEEKC_EOB(c, p);
1910 if (c == '\n') {
1911 file->line_num++;
1912 PEEKC_EOB(c, p);
1913 } else if (c == '\r') {
1914 PEEKC_EOB(c, p);
1915 if (c == '\n') {
1916 file->line_num++;
1917 PEEKC_EOB(c, p);
1920 } else {
1921 goto redo;
1923 } else {
1924 p++;
1927 return p;
1930 /* C comments */
1931 static uint8_t *parse_comment(uint8_t *p)
1933 int c;
1935 p++;
1936 for(;;) {
1937 /* fast skip loop */
1938 for(;;) {
1939 c = *p;
1940 if (c == '\n' || c == '*' || c == '\\')
1941 break;
1942 p++;
1943 c = *p;
1944 if (c == '\n' || c == '*' || c == '\\')
1945 break;
1946 p++;
1948 /* now we can handle all the cases */
1949 if (c == '\n') {
1950 file->line_num++;
1951 p++;
1952 } else if (c == '*') {
1953 p++;
1954 for(;;) {
1955 c = *p;
1956 if (c == '*') {
1957 p++;
1958 } else if (c == '/') {
1959 goto end_of_comment;
1960 } else if (c == '\\') {
1961 file->buf_ptr = p;
1962 c = handle_eob();
1963 p = file->buf_ptr;
1964 if (c == '\\') {
1965 /* skip '\[\r]\n', otherwise just skip the stray */
1966 while (c == '\\') {
1967 PEEKC_EOB(c, p);
1968 if (c == '\n') {
1969 file->line_num++;
1970 PEEKC_EOB(c, p);
1971 } else if (c == '\r') {
1972 PEEKC_EOB(c, p);
1973 if (c == '\n') {
1974 file->line_num++;
1975 PEEKC_EOB(c, p);
1977 } else {
1978 goto after_star;
1982 } else {
1983 break;
1986 after_star: ;
1987 } else {
1988 /* stray, eob or eof */
1989 file->buf_ptr = p;
1990 c = handle_eob();
1991 p = file->buf_ptr;
1992 if (c == CH_EOF) {
1993 error("unexpected end of file in comment");
1994 } else if (c == '\\') {
1995 p++;
1999 end_of_comment:
2000 p++;
2001 return p;
2004 #define cinp minp
2006 /* space exlcuding newline */
2007 static inline int is_space(int ch)
2009 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2012 static inline void skip_spaces(void)
2014 while (is_space(ch))
2015 cinp();
2018 /* parse a string without interpreting escapes */
2019 static uint8_t *parse_pp_string(uint8_t *p,
2020 int sep, CString *str)
2022 int c;
2023 p++;
2024 for(;;) {
2025 c = *p;
2026 if (c == sep) {
2027 break;
2028 } else if (c == '\\') {
2029 file->buf_ptr = p;
2030 c = handle_eob();
2031 p = file->buf_ptr;
2032 if (c == CH_EOF) {
2033 unterminated_string:
2034 /* XXX: indicate line number of start of string */
2035 error("missing terminating %c character", sep);
2036 } else if (c == '\\') {
2037 /* escape : just skip \[\r]\n */
2038 PEEKC_EOB(c, p);
2039 if (c == '\n') {
2040 file->line_num++;
2041 p++;
2042 } else if (c == '\r') {
2043 PEEKC_EOB(c, p);
2044 if (c != '\n')
2045 expect("'\n' after '\r'");
2046 file->line_num++;
2047 p++;
2048 } else if (c == CH_EOF) {
2049 goto unterminated_string;
2050 } else {
2051 if (str) {
2052 cstr_ccat(str, '\\');
2053 cstr_ccat(str, c);
2055 p++;
2058 } else if (c == '\n') {
2059 file->line_num++;
2060 goto add_char;
2061 } else if (c == '\r') {
2062 PEEKC_EOB(c, p);
2063 if (c != '\n') {
2064 if (str)
2065 cstr_ccat(str, '\r');
2066 } else {
2067 file->line_num++;
2068 goto add_char;
2070 } else {
2071 add_char:
2072 if (str)
2073 cstr_ccat(str, c);
2074 p++;
2077 p++;
2078 return p;
2081 /* skip block of text until #else, #elif or #endif. skip also pairs of
2082 #if/#endif */
2083 void preprocess_skip(void)
2085 int a, start_of_line, c;
2086 uint8_t *p;
2088 p = file->buf_ptr;
2089 start_of_line = 1;
2090 a = 0;
2091 for(;;) {
2092 redo_no_start:
2093 c = *p;
2094 switch(c) {
2095 case ' ':
2096 case '\t':
2097 case '\f':
2098 case '\v':
2099 case '\r':
2100 p++;
2101 goto redo_no_start;
2102 case '\n':
2103 start_of_line = 1;
2104 file->line_num++;
2105 p++;
2106 goto redo_no_start;
2107 case '\\':
2108 file->buf_ptr = p;
2109 c = handle_eob();
2110 if (c == CH_EOF) {
2111 expect("#endif");
2112 } else if (c == '\\') {
2113 /* XXX: incorrect: should not give an error */
2114 ch = file->buf_ptr[0];
2115 handle_stray();
2117 p = file->buf_ptr;
2118 goto redo_no_start;
2119 /* skip strings */
2120 case '\"':
2121 case '\'':
2122 p = parse_pp_string(p, c, NULL);
2123 break;
2124 /* skip comments */
2125 case '/':
2126 file->buf_ptr = p;
2127 ch = *p;
2128 minp();
2129 p = file->buf_ptr;
2130 if (ch == '*') {
2131 p = parse_comment(p);
2132 } else if (ch == '/') {
2133 p = parse_line_comment(p);
2135 break;
2137 case '#':
2138 p++;
2139 if (start_of_line) {
2140 file->buf_ptr = p;
2141 next_nomacro();
2142 p = file->buf_ptr;
2143 if (a == 0 &&
2144 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2145 goto the_end;
2146 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2147 a++;
2148 else if (tok == TOK_ENDIF)
2149 a--;
2151 break;
2152 default:
2153 p++;
2154 break;
2156 start_of_line = 0;
2158 the_end: ;
2159 file->buf_ptr = p;
2162 /* ParseState handling */
2164 /* XXX: currently, no include file info is stored. Thus, we cannot display
2165 accurate messages if the function or data definition spans multiple
2166 files */
2168 /* save current parse state in 's' */
2169 void save_parse_state(ParseState *s)
2171 s->line_num = file->line_num;
2172 s->macro_ptr = macro_ptr;
2173 s->tok = tok;
2174 s->tokc = tokc;
2177 /* restore parse state from 's' */
2178 void restore_parse_state(ParseState *s)
2180 file->line_num = s->line_num;
2181 macro_ptr = s->macro_ptr;
2182 tok = s->tok;
2183 tokc = s->tokc;
2186 /* return the number of additional 'ints' necessary to store the
2187 token */
2188 static inline int tok_ext_size(int t)
2190 switch(t) {
2191 /* 4 bytes */
2192 case TOK_CINT:
2193 case TOK_CUINT:
2194 case TOK_CCHAR:
2195 case TOK_LCHAR:
2196 case TOK_STR:
2197 case TOK_LSTR:
2198 case TOK_CFLOAT:
2199 case TOK_LINENUM:
2200 case TOK_PPNUM:
2201 return 1;
2202 case TOK_CDOUBLE:
2203 case TOK_CLLONG:
2204 case TOK_CULLONG:
2205 return 2;
2206 case TOK_CLDOUBLE:
2207 return LDOUBLE_SIZE / 4;
2208 default:
2209 return 0;
2213 /* token string handling */
2215 static inline void tok_str_new(TokenString *s)
2217 s->str = NULL;
2218 s->len = 0;
2219 s->allocated_len = 0;
2220 s->last_line_num = -1;
2223 static void tok_str_free(int *str)
2225 const int *p;
2226 CString *cstr;
2227 int t;
2229 p = str;
2230 for(;;) {
2231 t = *p;
2232 /* NOTE: we test zero separately so that GCC can generate a
2233 table for the following switch */
2234 if (t == 0)
2235 break;
2236 switch(t) {
2237 case TOK_CINT:
2238 case TOK_CUINT:
2239 case TOK_CCHAR:
2240 case TOK_LCHAR:
2241 case TOK_CFLOAT:
2242 case TOK_LINENUM:
2243 p += 2;
2244 break;
2245 case TOK_PPNUM:
2246 case TOK_STR:
2247 case TOK_LSTR:
2248 /* XXX: use a macro to be portable on 64 bit ? */
2249 cstr = (CString *)p[1];
2250 cstr_free(cstr);
2251 tcc_free(cstr);
2252 p += 2;
2253 break;
2254 case TOK_CDOUBLE:
2255 case TOK_CLLONG:
2256 case TOK_CULLONG:
2257 p += 3;
2258 break;
2259 case TOK_CLDOUBLE:
2260 p += 1 + (LDOUBLE_SIZE / 4);
2261 break;
2262 default:
2263 p++;
2264 break;
2267 tcc_free(str);
2270 static int *tok_str_realloc(TokenString *s)
2272 int *str, len;
2274 if (s->allocated_len == 0) {
2275 len = 8;
2276 } else {
2277 len = s->allocated_len * 2;
2279 str = tcc_realloc(s->str, len * sizeof(int));
2280 if (!str)
2281 error("memory full");
2282 s->allocated_len = len;
2283 s->str = str;
2284 return str;
2287 static void tok_str_add(TokenString *s, int t)
2289 int len, *str;
2291 len = s->len;
2292 str = s->str;
2293 if (len >= s->allocated_len)
2294 str = tok_str_realloc(s);
2295 str[len++] = t;
2296 s->len = len;
2299 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2301 int len, *str;
2303 len = s->len;
2304 str = s->str;
2306 /* allocate space for worst case */
2307 if (len + TOK_MAX_SIZE > s->allocated_len)
2308 str = tok_str_realloc(s);
2309 str[len++] = t;
2310 switch(t) {
2311 case TOK_CINT:
2312 case TOK_CUINT:
2313 case TOK_CCHAR:
2314 case TOK_LCHAR:
2315 case TOK_CFLOAT:
2316 case TOK_LINENUM:
2317 str[len++] = cv->tab[0];
2318 break;
2319 case TOK_PPNUM:
2320 case TOK_STR:
2321 case TOK_LSTR:
2322 str[len++] = (int)cstr_dup(cv->cstr);
2323 break;
2324 case TOK_CDOUBLE:
2325 case TOK_CLLONG:
2326 case TOK_CULLONG:
2327 #if LDOUBLE_SIZE == 8
2328 case TOK_CLDOUBLE:
2329 #endif
2330 str[len++] = cv->tab[0];
2331 str[len++] = cv->tab[1];
2332 break;
2333 #if LDOUBLE_SIZE == 12
2334 case TOK_CLDOUBLE:
2335 str[len++] = cv->tab[0];
2336 str[len++] = cv->tab[1];
2337 str[len++] = cv->tab[2];
2338 #elif LDOUBLE_SIZE != 8
2339 #error add long double size support
2340 #endif
2341 break;
2342 default:
2343 break;
2345 s->len = len;
2348 /* add the current parse token in token string 's' */
2349 static void tok_str_add_tok(TokenString *s)
2351 CValue cval;
2353 /* save line number info */
2354 if (file->line_num != s->last_line_num) {
2355 s->last_line_num = file->line_num;
2356 cval.i = s->last_line_num;
2357 tok_str_add2(s, TOK_LINENUM, &cval);
2359 tok_str_add2(s, tok, &tokc);
2362 #if LDOUBLE_SIZE == 12
2363 #define LDOUBLE_GET(p, cv) \
2364 cv.tab[0] = p[0]; \
2365 cv.tab[1] = p[1]; \
2366 cv.tab[2] = p[2];
2367 #elif LDOUBLE_SIZE == 8
2368 #define LDOUBLE_GET(p, cv) \
2369 cv.tab[0] = p[0]; \
2370 cv.tab[1] = p[1];
2371 #else
2372 #error add long double size support
2373 #endif
2376 /* get a token from an integer array and increment pointer
2377 accordingly. we code it as a macro to avoid pointer aliasing. */
2378 #define TOK_GET(t, p, cv) \
2380 t = *p++; \
2381 switch(t) { \
2382 case TOK_CINT: \
2383 case TOK_CUINT: \
2384 case TOK_CCHAR: \
2385 case TOK_LCHAR: \
2386 case TOK_CFLOAT: \
2387 case TOK_LINENUM: \
2388 case TOK_STR: \
2389 case TOK_LSTR: \
2390 case TOK_PPNUM: \
2391 cv.tab[0] = *p++; \
2392 break; \
2393 case TOK_CDOUBLE: \
2394 case TOK_CLLONG: \
2395 case TOK_CULLONG: \
2396 cv.tab[0] = p[0]; \
2397 cv.tab[1] = p[1]; \
2398 p += 2; \
2399 break; \
2400 case TOK_CLDOUBLE: \
2401 LDOUBLE_GET(p, cv); \
2402 p += LDOUBLE_SIZE / 4; \
2403 break; \
2404 default: \
2405 break; \
2409 /* defines handling */
2410 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2412 Sym *s;
2414 s = sym_push2(&define_stack, v, macro_type, (int)str);
2415 s->next = first_arg;
2416 table_ident[v - TOK_IDENT]->sym_define = s;
2419 /* undefined a define symbol. Its name is just set to zero */
2420 static void define_undef(Sym *s)
2422 int v;
2423 v = s->v;
2424 if (v >= TOK_IDENT && v < tok_ident)
2425 table_ident[v - TOK_IDENT]->sym_define = NULL;
2426 s->v = 0;
2429 static inline Sym *define_find(int v)
2431 v -= TOK_IDENT;
2432 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2433 return NULL;
2434 return table_ident[v]->sym_define;
2437 /* free define stack until top reaches 'b' */
2438 static void free_defines(Sym *b)
2440 Sym *top, *top1;
2441 int v;
2443 top = define_stack;
2444 while (top != b) {
2445 top1 = top->prev;
2446 /* do not free args or predefined defines */
2447 if (top->c)
2448 tok_str_free((int *)top->c);
2449 v = top->v;
2450 if (v >= TOK_IDENT && v < tok_ident)
2451 table_ident[v - TOK_IDENT]->sym_define = NULL;
2452 tcc_free(top);
2453 top = top1;
2455 define_stack = b;
2458 /* label lookup */
2459 static Sym *label_find(int v)
2461 v -= TOK_IDENT;
2462 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2463 return NULL;
2464 return table_ident[v]->sym_label;
2467 static Sym *label_push(Sym **ptop, int v, int flags)
2469 Sym *s, **ps;
2470 s = sym_push2(ptop, v, 0, 0);
2471 s->r = flags;
2472 ps = &table_ident[v - TOK_IDENT]->sym_label;
2473 if (ptop == &global_label_stack) {
2474 /* modify the top most local identifier, so that
2475 sym_identifier will point to 's' when popped */
2476 while (*ps != NULL)
2477 ps = &(*ps)->prev_tok;
2479 s->prev_tok = *ps;
2480 *ps = s;
2481 return s;
2484 /* pop labels until element last is reached. Look if any labels are
2485 undefined. Define symbols if '&&label' was used. */
2486 static void label_pop(Sym **ptop, Sym *slast)
2488 Sym *s, *s1;
2489 for(s = *ptop; s != slast; s = s1) {
2490 s1 = s->prev;
2491 if (s->r == LABEL_DECLARED) {
2492 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2493 } else if (s->r == LABEL_FORWARD) {
2494 error("label '%s' used but not defined",
2495 get_tok_str(s->v, NULL));
2496 } else {
2497 if (s->c) {
2498 /* define corresponding symbol. A size of
2499 1 is put. */
2500 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2503 /* remove label */
2504 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2505 tcc_free(s);
2507 *ptop = slast;
2510 /* eval an expression for #if/#elif */
2511 static int expr_preprocess(void)
2513 int c, t;
2514 TokenString str;
2516 tok_str_new(&str);
2517 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2518 next(); /* do macro subst */
2519 if (tok == TOK_DEFINED) {
2520 next_nomacro();
2521 t = tok;
2522 if (t == '(')
2523 next_nomacro();
2524 c = define_find(tok) != 0;
2525 if (t == '(')
2526 next_nomacro();
2527 tok = TOK_CINT;
2528 tokc.i = c;
2529 } else if (tok >= TOK_IDENT) {
2530 /* if undefined macro */
2531 tok = TOK_CINT;
2532 tokc.i = 0;
2534 tok_str_add_tok(&str);
2536 tok_str_add(&str, -1); /* simulate end of file */
2537 tok_str_add(&str, 0);
2538 /* now evaluate C constant expression */
2539 macro_ptr = str.str;
2540 next();
2541 c = expr_const();
2542 macro_ptr = NULL;
2543 tok_str_free(str.str);
2544 return c != 0;
2547 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2548 static void tok_print(int *str)
2550 int t;
2551 CValue cval;
2553 while (1) {
2554 TOK_GET(t, str, cval);
2555 if (!t)
2556 break;
2557 printf(" %s", get_tok_str(t, &cval));
2559 printf("\n");
2561 #endif
2563 /* parse after #define */
2564 static void parse_define(void)
2566 Sym *s, *first, **ps;
2567 int v, t, varg, is_vaargs, c;
2568 TokenString str;
2570 v = tok;
2571 if (v < TOK_IDENT)
2572 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2573 /* XXX: should check if same macro (ANSI) */
2574 first = NULL;
2575 t = MACRO_OBJ;
2576 /* '(' must be just after macro definition for MACRO_FUNC */
2577 c = file->buf_ptr[0];
2578 if (c == '\\')
2579 c = handle_stray1(file->buf_ptr);
2580 if (c == '(') {
2581 next_nomacro();
2582 next_nomacro();
2583 ps = &first;
2584 while (tok != ')') {
2585 varg = tok;
2586 next_nomacro();
2587 is_vaargs = 0;
2588 if (varg == TOK_DOTS) {
2589 varg = TOK___VA_ARGS__;
2590 is_vaargs = 1;
2591 } else if (tok == TOK_DOTS && gnu_ext) {
2592 is_vaargs = 1;
2593 next_nomacro();
2595 if (varg < TOK_IDENT)
2596 error("badly punctuated parameter list");
2597 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2598 *ps = s;
2599 ps = &s->next;
2600 if (tok != ',')
2601 break;
2602 next_nomacro();
2604 t = MACRO_FUNC;
2606 tok_str_new(&str);
2607 next_nomacro();
2608 /* EOF testing necessary for '-D' handling */
2609 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2610 tok_str_add2(&str, tok, &tokc);
2611 next_nomacro();
2613 tok_str_add(&str, 0);
2614 #ifdef PP_DEBUG
2615 printf("define %s %d: ", get_tok_str(v, NULL), t);
2616 tok_print(str.str);
2617 #endif
2618 define_push(v, t, str.str, first);
2621 /* XXX: use a token or a hash table to accelerate matching ? */
2622 static CachedInclude *search_cached_include(TCCState *s1,
2623 int type, const char *filename)
2625 CachedInclude *e;
2626 int i;
2628 for(i = 0;i < s1->nb_cached_includes; i++) {
2629 e = s1->cached_includes[i];
2630 if (e->type == type && !strcmp(e->filename, filename))
2631 return e;
2633 return NULL;
2636 static inline void add_cached_include(TCCState *s1, int type,
2637 const char *filename, int ifndef_macro)
2639 CachedInclude *e;
2641 if (search_cached_include(s1, type, filename))
2642 return;
2643 #ifdef INC_DEBUG
2644 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2645 #endif
2646 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2647 if (!e)
2648 return;
2649 e->type = type;
2650 strcpy(e->filename, filename);
2651 e->ifndef_macro = ifndef_macro;
2652 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2655 /* is_bof is true if first non space token at beginning of file */
2656 static void preprocess(int is_bof)
2658 TCCState *s1 = tcc_state;
2659 int size, i, c, n, saved_parse_flags;
2660 char buf[1024], *q, *p;
2661 char buf1[1024];
2662 BufferedFile *f;
2663 Sym *s;
2664 CachedInclude *e;
2666 saved_parse_flags = parse_flags;
2667 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2668 PARSE_FLAG_LINEFEED;
2669 next_nomacro();
2670 redo:
2671 switch(tok) {
2672 case TOK_DEFINE:
2673 next_nomacro();
2674 parse_define();
2675 break;
2676 case TOK_UNDEF:
2677 next_nomacro();
2678 s = define_find(tok);
2679 /* undefine symbol by putting an invalid name */
2680 if (s)
2681 define_undef(s);
2682 break;
2683 case TOK_INCLUDE:
2684 ch = file->buf_ptr[0];
2685 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2686 skip_spaces();
2687 if (ch == '<') {
2688 c = '>';
2689 goto read_name;
2690 } else if (ch == '\"') {
2691 c = ch;
2692 read_name:
2693 /* XXX: better stray handling */
2694 minp();
2695 q = buf;
2696 while (ch != c && ch != '\n' && ch != CH_EOF) {
2697 if ((q - buf) < sizeof(buf) - 1)
2698 *q++ = ch;
2699 minp();
2701 *q = '\0';
2702 minp();
2703 #if 0
2704 /* eat all spaces and comments after include */
2705 /* XXX: slightly incorrect */
2706 while (ch1 != '\n' && ch1 != CH_EOF)
2707 inp();
2708 #endif
2709 } else {
2710 /* computed #include : either we have only strings or
2711 we have anything enclosed in '<>' */
2712 next();
2713 buf[0] = '\0';
2714 if (tok == TOK_STR) {
2715 while (tok != TOK_LINEFEED) {
2716 if (tok != TOK_STR) {
2717 include_syntax:
2718 error("'#include' expects \"FILENAME\" or <FILENAME>");
2720 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2721 next();
2723 c = '\"';
2724 } else {
2725 int len;
2726 while (tok != TOK_LINEFEED) {
2727 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2728 next();
2730 len = strlen(buf);
2731 /* check syntax and remove '<>' */
2732 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2733 goto include_syntax;
2734 memmove(buf, buf + 1, len - 2);
2735 buf[len - 2] = '\0';
2736 c = '>';
2740 e = search_cached_include(s1, c, buf);
2741 if (e && define_find(e->ifndef_macro)) {
2742 /* no need to parse the include because the 'ifndef macro'
2743 is defined */
2744 #ifdef INC_DEBUG
2745 printf("%s: skipping %s\n", file->filename, buf);
2746 #endif
2747 } else {
2748 if (c == '\"') {
2749 /* first search in current dir if "header.h" */
2750 size = 0;
2751 p = strrchr(file->filename, '/');
2752 if (p)
2753 size = p + 1 - file->filename;
2754 if (size > sizeof(buf1) - 1)
2755 size = sizeof(buf1) - 1;
2756 memcpy(buf1, file->filename, size);
2757 buf1[size] = '\0';
2758 pstrcat(buf1, sizeof(buf1), buf);
2759 f = tcc_open(s1, buf1);
2760 if (f)
2761 goto found;
2763 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2764 error("#include recursion too deep");
2765 /* now search in all the include paths */
2766 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2767 for(i = 0; i < n; i++) {
2768 const char *path;
2769 if (i < s1->nb_include_paths)
2770 path = s1->include_paths[i];
2771 else
2772 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2773 pstrcpy(buf1, sizeof(buf1), path);
2774 pstrcat(buf1, sizeof(buf1), "/");
2775 pstrcat(buf1, sizeof(buf1), buf);
2776 f = tcc_open(s1, buf1);
2777 if (f)
2778 goto found;
2780 error("include file '%s' not found", buf);
2781 f = NULL;
2782 found:
2783 #ifdef INC_DEBUG
2784 printf("%s: including %s\n", file->filename, buf1);
2785 #endif
2786 f->inc_type = c;
2787 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2788 /* push current file in stack */
2789 /* XXX: fix current line init */
2790 *s1->include_stack_ptr++ = file;
2791 file = f;
2792 /* add include file debug info */
2793 if (do_debug) {
2794 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2796 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2797 ch = file->buf_ptr[0];
2798 goto the_end;
2800 break;
2801 case TOK_IFNDEF:
2802 c = 1;
2803 goto do_ifdef;
2804 case TOK_IF:
2805 c = expr_preprocess();
2806 goto do_if;
2807 case TOK_IFDEF:
2808 c = 0;
2809 do_ifdef:
2810 next_nomacro();
2811 if (tok < TOK_IDENT)
2812 error("invalid argument for '#if%sdef'", c ? "n" : "");
2813 if (is_bof) {
2814 if (c) {
2815 #ifdef INC_DEBUG
2816 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2817 #endif
2818 file->ifndef_macro = tok;
2821 c = (define_find(tok) != 0) ^ c;
2822 do_if:
2823 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2824 error("memory full");
2825 *s1->ifdef_stack_ptr++ = c;
2826 goto test_skip;
2827 case TOK_ELSE:
2828 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2829 error("#else without matching #if");
2830 if (s1->ifdef_stack_ptr[-1] & 2)
2831 error("#else after #else");
2832 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2833 goto test_skip;
2834 case TOK_ELIF:
2835 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2836 error("#elif without matching #if");
2837 c = s1->ifdef_stack_ptr[-1];
2838 if (c > 1)
2839 error("#elif after #else");
2840 /* last #if/#elif expression was true: we skip */
2841 if (c == 1)
2842 goto skip;
2843 c = expr_preprocess();
2844 s1->ifdef_stack_ptr[-1] = c;
2845 test_skip:
2846 if (!(c & 1)) {
2847 skip:
2848 preprocess_skip();
2849 is_bof = 0;
2850 goto redo;
2852 break;
2853 case TOK_ENDIF:
2854 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2855 error("#endif without matching #if");
2856 s1->ifdef_stack_ptr--;
2857 /* '#ifndef macro' was at the start of file. Now we check if
2858 an '#endif' is exactly at the end of file */
2859 if (file->ifndef_macro &&
2860 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2861 file->ifndef_macro_saved = file->ifndef_macro;
2862 /* need to set to zero to avoid false matches if another
2863 #ifndef at middle of file */
2864 file->ifndef_macro = 0;
2865 while (tok != TOK_LINEFEED)
2866 next_nomacro();
2867 tok_flags |= TOK_FLAG_ENDIF;
2868 goto the_end;
2870 break;
2871 case TOK_LINE:
2872 next();
2873 if (tok != TOK_CINT)
2874 error("#line");
2875 file->line_num = tokc.i - 1; /* the line number will be incremented after */
2876 next();
2877 if (tok != TOK_LINEFEED) {
2878 if (tok != TOK_STR)
2879 error("#line");
2880 pstrcpy(file->filename, sizeof(file->filename),
2881 (char *)tokc.cstr->data);
2883 break;
2884 case TOK_ERROR:
2885 case TOK_WARNING:
2886 c = tok;
2887 ch = file->buf_ptr[0];
2888 skip_spaces();
2889 q = buf;
2890 while (ch != '\n' && ch != CH_EOF) {
2891 if ((q - buf) < sizeof(buf) - 1)
2892 *q++ = ch;
2893 minp();
2895 *q = '\0';
2896 if (c == TOK_ERROR)
2897 error("#error %s", buf);
2898 else
2899 warning("#warning %s", buf);
2900 break;
2901 case TOK_PRAGMA:
2902 /* ignored */
2903 break;
2904 default:
2905 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
2906 /* '!' is ignored to allow C scripts. numbers are ignored
2907 to emulate cpp behaviour */
2908 } else {
2909 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
2910 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
2912 break;
2914 /* ignore other preprocess commands or #! for C scripts */
2915 while (tok != TOK_LINEFEED)
2916 next_nomacro();
2917 the_end:
2918 parse_flags = saved_parse_flags;
2921 /* evaluate escape codes in a string. */
2922 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2924 int c, n;
2925 const uint8_t *p;
2927 p = buf;
2928 for(;;) {
2929 c = *p;
2930 if (c == '\0')
2931 break;
2932 if (c == '\\') {
2933 p++;
2934 /* escape */
2935 c = *p;
2936 switch(c) {
2937 case '0': case '1': case '2': case '3':
2938 case '4': case '5': case '6': case '7':
2939 /* at most three octal digits */
2940 n = c - '0';
2941 p++;
2942 c = *p;
2943 if (isoct(c)) {
2944 n = n * 8 + c - '0';
2945 p++;
2946 c = *p;
2947 if (isoct(c)) {
2948 n = n * 8 + c - '0';
2949 p++;
2952 c = n;
2953 goto add_char_nonext;
2954 case 'x':
2955 p++;
2956 n = 0;
2957 for(;;) {
2958 c = *p;
2959 if (c >= 'a' && c <= 'f')
2960 c = c - 'a' + 10;
2961 else if (c >= 'A' && c <= 'F')
2962 c = c - 'A' + 10;
2963 else if (isnum(c))
2964 c = c - '0';
2965 else
2966 break;
2967 n = n * 16 + c;
2968 p++;
2970 c = n;
2971 goto add_char_nonext;
2972 case 'a':
2973 c = '\a';
2974 break;
2975 case 'b':
2976 c = '\b';
2977 break;
2978 case 'f':
2979 c = '\f';
2980 break;
2981 case 'n':
2982 c = '\n';
2983 break;
2984 case 'r':
2985 c = '\r';
2986 break;
2987 case 't':
2988 c = '\t';
2989 break;
2990 case 'v':
2991 c = '\v';
2992 break;
2993 case 'e':
2994 if (!gnu_ext)
2995 goto invalid_escape;
2996 c = 27;
2997 break;
2998 case '\'':
2999 case '\"':
3000 case '\\':
3001 case '?':
3002 break;
3003 default:
3004 invalid_escape:
3005 if (c >= '!' && c <= '~')
3006 warning("unknown escape sequence: \'\\%c\'", c);
3007 else
3008 warning("unknown escape sequence: \'\\x%x\'", c);
3009 break;
3012 p++;
3013 add_char_nonext:
3014 if (!is_long)
3015 cstr_ccat(outstr, c);
3016 else
3017 cstr_wccat(outstr, c);
3019 /* add a trailing '\0' */
3020 if (!is_long)
3021 cstr_ccat(outstr, '\0');
3022 else
3023 cstr_wccat(outstr, '\0');
3026 /* we use 64 bit numbers */
3027 #define BN_SIZE 2
3029 /* bn = (bn << shift) | or_val */
3030 void bn_lshift(unsigned int *bn, int shift, int or_val)
3032 int i;
3033 unsigned int v;
3034 for(i=0;i<BN_SIZE;i++) {
3035 v = bn[i];
3036 bn[i] = (v << shift) | or_val;
3037 or_val = v >> (32 - shift);
3041 void bn_zero(unsigned int *bn)
3043 int i;
3044 for(i=0;i<BN_SIZE;i++) {
3045 bn[i] = 0;
3049 /* parse number in null terminated string 'p' and return it in the
3050 current token */
3051 void parse_number(const char *p)
3053 int b, t, shift, frac_bits, s, exp_val, ch;
3054 char *q;
3055 unsigned int bn[BN_SIZE];
3056 double d;
3058 /* number */
3059 q = token_buf;
3060 ch = *p++;
3061 t = ch;
3062 ch = *p++;
3063 *q++ = t;
3064 b = 10;
3065 if (t == '.') {
3066 goto float_frac_parse;
3067 } else if (t == '0') {
3068 if (ch == 'x' || ch == 'X') {
3069 q--;
3070 ch = *p++;
3071 b = 16;
3072 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3073 q--;
3074 ch = *p++;
3075 b = 2;
3078 /* parse all digits. cannot check octal numbers at this stage
3079 because of floating point constants */
3080 while (1) {
3081 if (ch >= 'a' && ch <= 'f')
3082 t = ch - 'a' + 10;
3083 else if (ch >= 'A' && ch <= 'F')
3084 t = ch - 'A' + 10;
3085 else if (isnum(ch))
3086 t = ch - '0';
3087 else
3088 break;
3089 if (t >= b)
3090 break;
3091 if (q >= token_buf + STRING_MAX_SIZE) {
3092 num_too_long:
3093 error("number too long");
3095 *q++ = ch;
3096 ch = *p++;
3098 if (ch == '.' ||
3099 ((ch == 'e' || ch == 'E') && b == 10) ||
3100 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3101 if (b != 10) {
3102 /* NOTE: strtox should support that for hexa numbers, but
3103 non ISOC99 libcs do not support it, so we prefer to do
3104 it by hand */
3105 /* hexadecimal or binary floats */
3106 /* XXX: handle overflows */
3107 *q = '\0';
3108 if (b == 16)
3109 shift = 4;
3110 else
3111 shift = 2;
3112 bn_zero(bn);
3113 q = token_buf;
3114 while (1) {
3115 t = *q++;
3116 if (t == '\0') {
3117 break;
3118 } else if (t >= 'a') {
3119 t = t - 'a' + 10;
3120 } else if (t >= 'A') {
3121 t = t - 'A' + 10;
3122 } else {
3123 t = t - '0';
3125 bn_lshift(bn, shift, t);
3127 frac_bits = 0;
3128 if (ch == '.') {
3129 ch = *p++;
3130 while (1) {
3131 t = ch;
3132 if (t >= 'a' && t <= 'f') {
3133 t = t - 'a' + 10;
3134 } else if (t >= 'A' && t <= 'F') {
3135 t = t - 'A' + 10;
3136 } else if (t >= '0' && t <= '9') {
3137 t = t - '0';
3138 } else {
3139 break;
3141 if (t >= b)
3142 error("invalid digit");
3143 bn_lshift(bn, shift, t);
3144 frac_bits += shift;
3145 ch = *p++;
3148 if (ch != 'p' && ch != 'P')
3149 expect("exponent");
3150 ch = *p++;
3151 s = 1;
3152 exp_val = 0;
3153 if (ch == '+') {
3154 ch = *p++;
3155 } else if (ch == '-') {
3156 s = -1;
3157 ch = *p++;
3159 if (ch < '0' || ch > '9')
3160 expect("exponent digits");
3161 while (ch >= '0' && ch <= '9') {
3162 exp_val = exp_val * 10 + ch - '0';
3163 ch = *p++;
3165 exp_val = exp_val * s;
3167 /* now we can generate the number */
3168 /* XXX: should patch directly float number */
3169 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3170 d = ldexp(d, exp_val - frac_bits);
3171 t = toup(ch);
3172 if (t == 'F') {
3173 ch = *p++;
3174 tok = TOK_CFLOAT;
3175 /* float : should handle overflow */
3176 tokc.f = (float)d;
3177 } else if (t == 'L') {
3178 ch = *p++;
3179 tok = TOK_CLDOUBLE;
3180 /* XXX: not large enough */
3181 tokc.ld = (long double)d;
3182 } else {
3183 tok = TOK_CDOUBLE;
3184 tokc.d = d;
3186 } else {
3187 /* decimal floats */
3188 if (ch == '.') {
3189 if (q >= token_buf + STRING_MAX_SIZE)
3190 goto num_too_long;
3191 *q++ = ch;
3192 ch = *p++;
3193 float_frac_parse:
3194 while (ch >= '0' && ch <= '9') {
3195 if (q >= token_buf + STRING_MAX_SIZE)
3196 goto num_too_long;
3197 *q++ = ch;
3198 ch = *p++;
3201 if (ch == 'e' || ch == 'E') {
3202 if (q >= token_buf + STRING_MAX_SIZE)
3203 goto num_too_long;
3204 *q++ = ch;
3205 ch = *p++;
3206 if (ch == '-' || ch == '+') {
3207 if (q >= token_buf + STRING_MAX_SIZE)
3208 goto num_too_long;
3209 *q++ = ch;
3210 ch = *p++;
3212 if (ch < '0' || ch > '9')
3213 expect("exponent digits");
3214 while (ch >= '0' && ch <= '9') {
3215 if (q >= token_buf + STRING_MAX_SIZE)
3216 goto num_too_long;
3217 *q++ = ch;
3218 ch = *p++;
3221 *q = '\0';
3222 t = toup(ch);
3223 errno = 0;
3224 if (t == 'F') {
3225 ch = *p++;
3226 tok = TOK_CFLOAT;
3227 tokc.f = strtof(token_buf, NULL);
3228 } else if (t == 'L') {
3229 ch = *p++;
3230 tok = TOK_CLDOUBLE;
3231 tokc.ld = strtold(token_buf, NULL);
3232 } else {
3233 tok = TOK_CDOUBLE;
3234 tokc.d = strtod(token_buf, NULL);
3237 } else {
3238 unsigned long long n, n1;
3239 int lcount, ucount;
3241 /* integer number */
3242 *q = '\0';
3243 q = token_buf;
3244 if (b == 10 && *q == '0') {
3245 b = 8;
3246 q++;
3248 n = 0;
3249 while(1) {
3250 t = *q++;
3251 /* no need for checks except for base 10 / 8 errors */
3252 if (t == '\0') {
3253 break;
3254 } else if (t >= 'a') {
3255 t = t - 'a' + 10;
3256 } else if (t >= 'A') {
3257 t = t - 'A' + 10;
3258 } else {
3259 t = t - '0';
3260 if (t >= b)
3261 error("invalid digit");
3263 n1 = n;
3264 n = n * b + t;
3265 /* detect overflow */
3266 /* XXX: this test is not reliable */
3267 if (n < n1)
3268 error("integer constant overflow");
3271 /* XXX: not exactly ANSI compliant */
3272 if ((n & 0xffffffff00000000LL) != 0) {
3273 if ((n >> 63) != 0)
3274 tok = TOK_CULLONG;
3275 else
3276 tok = TOK_CLLONG;
3277 } else if (n > 0x7fffffff) {
3278 tok = TOK_CUINT;
3279 } else {
3280 tok = TOK_CINT;
3282 lcount = 0;
3283 ucount = 0;
3284 for(;;) {
3285 t = toup(ch);
3286 if (t == 'L') {
3287 if (lcount >= 2)
3288 error("three 'l's in integer constant");
3289 lcount++;
3290 if (lcount == 2) {
3291 if (tok == TOK_CINT)
3292 tok = TOK_CLLONG;
3293 else if (tok == TOK_CUINT)
3294 tok = TOK_CULLONG;
3296 ch = *p++;
3297 } else if (t == 'U') {
3298 if (ucount >= 1)
3299 error("two 'u's in integer constant");
3300 ucount++;
3301 if (tok == TOK_CINT)
3302 tok = TOK_CUINT;
3303 else if (tok == TOK_CLLONG)
3304 tok = TOK_CULLONG;
3305 ch = *p++;
3306 } else {
3307 break;
3310 if (tok == TOK_CINT || tok == TOK_CUINT)
3311 tokc.ui = n;
3312 else
3313 tokc.ull = n;
3318 #define PARSE2(c1, tok1, c2, tok2) \
3319 case c1: \
3320 PEEKC(c, p); \
3321 if (c == c2) { \
3322 p++; \
3323 tok = tok2; \
3324 } else { \
3325 tok = tok1; \
3327 break;
3329 /* return next token without macro substitution */
3330 static inline void next_nomacro1(void)
3332 int t, c, is_long;
3333 TokenSym *ts;
3334 uint8_t *p, *p1;
3335 unsigned int h;
3337 p = file->buf_ptr;
3338 redo_no_start:
3339 c = *p;
3340 switch(c) {
3341 case ' ':
3342 case '\t':
3343 case '\f':
3344 case '\v':
3345 case '\r':
3346 p++;
3347 goto redo_no_start;
3349 case '\\':
3350 /* first look if it is in fact an end of buffer */
3351 if (p >= file->buf_end) {
3352 file->buf_ptr = p;
3353 handle_eob();
3354 p = file->buf_ptr;
3355 if (p >= file->buf_end)
3356 goto parse_eof;
3357 else
3358 goto redo_no_start;
3359 } else {
3360 file->buf_ptr = p;
3361 ch = *p;
3362 handle_stray();
3363 p = file->buf_ptr;
3364 goto redo_no_start;
3366 parse_eof:
3368 TCCState *s1 = tcc_state;
3369 if (parse_flags & PARSE_FLAG_LINEFEED) {
3370 tok = TOK_LINEFEED;
3371 } else if (s1->include_stack_ptr == s1->include_stack ||
3372 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3373 /* no include left : end of file. */
3374 tok = TOK_EOF;
3375 } else {
3376 /* pop include file */
3378 /* test if previous '#endif' was after a #ifdef at
3379 start of file */
3380 if (tok_flags & TOK_FLAG_ENDIF) {
3381 #ifdef INC_DEBUG
3382 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3383 #endif
3384 add_cached_include(s1, file->inc_type, file->inc_filename,
3385 file->ifndef_macro_saved);
3388 /* add end of include file debug info */
3389 if (do_debug) {
3390 put_stabd(N_EINCL, 0, 0);
3392 /* pop include stack */
3393 tcc_close(file);
3394 s1->include_stack_ptr--;
3395 file = *s1->include_stack_ptr;
3396 p = file->buf_ptr;
3397 goto redo_no_start;
3400 break;
3402 case '\n':
3403 if (parse_flags & PARSE_FLAG_LINEFEED) {
3404 tok = TOK_LINEFEED;
3405 } else {
3406 file->line_num++;
3407 tok_flags |= TOK_FLAG_BOL;
3408 p++;
3409 goto redo_no_start;
3411 break;
3413 case '#':
3414 /* XXX: simplify */
3415 PEEKC(c, p);
3416 if ((tok_flags & TOK_FLAG_BOL) &&
3417 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3418 file->buf_ptr = p;
3419 preprocess(tok_flags & TOK_FLAG_BOF);
3420 p = file->buf_ptr;
3421 goto redo_no_start;
3422 } else {
3423 if (c == '#') {
3424 p++;
3425 tok = TOK_TWOSHARPS;
3426 } else {
3427 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3428 p = parse_line_comment(p - 1);
3429 goto redo_no_start;
3430 } else {
3431 tok = '#';
3435 break;
3437 case 'a': case 'b': case 'c': case 'd':
3438 case 'e': case 'f': case 'g': case 'h':
3439 case 'i': case 'j': case 'k': case 'l':
3440 case 'm': case 'n': case 'o': case 'p':
3441 case 'q': case 'r': case 's': case 't':
3442 case 'u': case 'v': case 'w': case 'x':
3443 case 'y': case 'z':
3444 case 'A': case 'B': case 'C': case 'D':
3445 case 'E': case 'F': case 'G': case 'H':
3446 case 'I': case 'J': case 'K':
3447 case 'M': case 'N': case 'O': case 'P':
3448 case 'Q': case 'R': case 'S': case 'T':
3449 case 'U': case 'V': case 'W': case 'X':
3450 case 'Y': case 'Z':
3451 case '_':
3452 parse_ident_fast:
3453 p1 = p;
3454 h = TOK_HASH_INIT;
3455 h = TOK_HASH_FUNC(h, c);
3456 p++;
3457 for(;;) {
3458 c = *p;
3459 if (!isidnum_table[c])
3460 break;
3461 h = TOK_HASH_FUNC(h, c);
3462 p++;
3464 if (c != '\\') {
3465 TokenSym **pts;
3466 int len;
3468 /* fast case : no stray found, so we have the full token
3469 and we have already hashed it */
3470 len = p - p1;
3471 h &= (TOK_HASH_SIZE - 1);
3472 pts = &hash_ident[h];
3473 for(;;) {
3474 ts = *pts;
3475 if (!ts)
3476 break;
3477 if (ts->len == len && !memcmp(ts->str, p1, len))
3478 goto token_found;
3479 pts = &(ts->hash_next);
3481 ts = tok_alloc_new(pts, p1, len);
3482 token_found: ;
3483 } else {
3484 /* slower case */
3485 cstr_reset(&tokcstr);
3487 while (p1 < p) {
3488 cstr_ccat(&tokcstr, *p1);
3489 p1++;
3491 p--;
3492 PEEKC(c, p);
3493 parse_ident_slow:
3494 while (isidnum_table[c]) {
3495 cstr_ccat(&tokcstr, c);
3496 PEEKC(c, p);
3498 ts = tok_alloc(tokcstr.data, tokcstr.size);
3500 tok = ts->tok;
3501 break;
3502 case 'L':
3503 t = p[1];
3504 if (t != '\\' && t != '\'' && t != '\"') {
3505 /* fast case */
3506 goto parse_ident_fast;
3507 } else {
3508 PEEKC(c, p);
3509 if (c == '\'' || c == '\"') {
3510 is_long = 1;
3511 goto str_const;
3512 } else {
3513 cstr_reset(&tokcstr);
3514 cstr_ccat(&tokcstr, 'L');
3515 goto parse_ident_slow;
3518 break;
3519 case '0': case '1': case '2': case '3':
3520 case '4': case '5': case '6': case '7':
3521 case '8': case '9':
3523 cstr_reset(&tokcstr);
3524 /* after the first digit, accept digits, alpha, '.' or sign if
3525 prefixed by 'eEpP' */
3526 parse_num:
3527 for(;;) {
3528 t = c;
3529 cstr_ccat(&tokcstr, c);
3530 PEEKC(c, p);
3531 if (!(isnum(c) || isid(c) || c == '.' ||
3532 ((c == '+' || c == '-') &&
3533 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3534 break;
3536 /* We add a trailing '\0' to ease parsing */
3537 cstr_ccat(&tokcstr, '\0');
3538 tokc.cstr = &tokcstr;
3539 tok = TOK_PPNUM;
3540 break;
3541 case '.':
3542 /* special dot handling because it can also start a number */
3543 PEEKC(c, p);
3544 if (isnum(c)) {
3545 cstr_reset(&tokcstr);
3546 cstr_ccat(&tokcstr, '.');
3547 goto parse_num;
3548 } else if (c == '.') {
3549 PEEKC(c, p);
3550 if (c != '.')
3551 expect("'.'");
3552 PEEKC(c, p);
3553 tok = TOK_DOTS;
3554 } else {
3555 tok = '.';
3557 break;
3558 case '\'':
3559 case '\"':
3560 is_long = 0;
3561 str_const:
3563 CString str;
3564 int sep;
3566 sep = c;
3568 /* parse the string */
3569 cstr_new(&str);
3570 p = parse_pp_string(p, sep, &str);
3571 cstr_ccat(&str, '\0');
3573 /* eval the escape (should be done as TOK_PPNUM) */
3574 cstr_reset(&tokcstr);
3575 parse_escape_string(&tokcstr, str.data, is_long);
3576 cstr_free(&str);
3578 if (sep == '\'') {
3579 int char_size;
3580 /* XXX: make it portable */
3581 if (!is_long)
3582 char_size = 1;
3583 else
3584 char_size = sizeof(int);
3585 if (tokcstr.size <= char_size)
3586 error("empty character constant");
3587 if (tokcstr.size > 2 * char_size)
3588 warning("multi-character character constant");
3589 if (!is_long) {
3590 tokc.i = *(int8_t *)tokcstr.data;
3591 tok = TOK_CCHAR;
3592 } else {
3593 tokc.i = *(int *)tokcstr.data;
3594 tok = TOK_LCHAR;
3596 } else {
3597 tokc.cstr = &tokcstr;
3598 if (!is_long)
3599 tok = TOK_STR;
3600 else
3601 tok = TOK_LSTR;
3604 break;
3606 case '<':
3607 PEEKC(c, p);
3608 if (c == '=') {
3609 p++;
3610 tok = TOK_LE;
3611 } else if (c == '<') {
3612 PEEKC(c, p);
3613 if (c == '=') {
3614 p++;
3615 tok = TOK_A_SHL;
3616 } else {
3617 tok = TOK_SHL;
3619 } else {
3620 tok = TOK_LT;
3622 break;
3624 case '>':
3625 PEEKC(c, p);
3626 if (c == '=') {
3627 p++;
3628 tok = TOK_GE;
3629 } else if (c == '>') {
3630 PEEKC(c, p);
3631 if (c == '=') {
3632 p++;
3633 tok = TOK_A_SAR;
3634 } else {
3635 tok = TOK_SAR;
3637 } else {
3638 tok = TOK_GT;
3640 break;
3642 case '&':
3643 PEEKC(c, p);
3644 if (c == '&') {
3645 p++;
3646 tok = TOK_LAND;
3647 } else if (c == '=') {
3648 p++;
3649 tok = TOK_A_AND;
3650 } else {
3651 tok = '&';
3653 break;
3655 case '|':
3656 PEEKC(c, p);
3657 if (c == '|') {
3658 p++;
3659 tok = TOK_LOR;
3660 } else if (c == '=') {
3661 p++;
3662 tok = TOK_A_OR;
3663 } else {
3664 tok = '|';
3666 break;
3668 case '+':
3669 PEEKC(c, p);
3670 if (c == '+') {
3671 p++;
3672 tok = TOK_INC;
3673 } else if (c == '=') {
3674 p++;
3675 tok = TOK_A_ADD;
3676 } else {
3677 tok = '+';
3679 break;
3681 case '-':
3682 PEEKC(c, p);
3683 if (c == '-') {
3684 p++;
3685 tok = TOK_DEC;
3686 } else if (c == '=') {
3687 p++;
3688 tok = TOK_A_SUB;
3689 } else if (c == '>') {
3690 p++;
3691 tok = TOK_ARROW;
3692 } else {
3693 tok = '-';
3695 break;
3697 PARSE2('!', '!', '=', TOK_NE)
3698 PARSE2('=', '=', '=', TOK_EQ)
3699 PARSE2('*', '*', '=', TOK_A_MUL)
3700 PARSE2('%', '%', '=', TOK_A_MOD)
3701 PARSE2('^', '^', '=', TOK_A_XOR)
3703 /* comments or operator */
3704 case '/':
3705 PEEKC(c, p);
3706 if (c == '*') {
3707 p = parse_comment(p);
3708 goto redo_no_start;
3709 } else if (c == '/') {
3710 p = parse_line_comment(p);
3711 goto redo_no_start;
3712 } else if (c == '=') {
3713 p++;
3714 tok = TOK_A_DIV;
3715 } else {
3716 tok = '/';
3718 break;
3720 /* simple tokens */
3721 case '(':
3722 case ')':
3723 case '[':
3724 case ']':
3725 case '{':
3726 case '}':
3727 case ',':
3728 case ';':
3729 case ':':
3730 case '?':
3731 case '~':
3732 case '$': /* only used in assembler */
3733 tok = c;
3734 p++;
3735 break;
3736 default:
3737 error("unrecognized character \\x%02x", c);
3738 break;
3740 file->buf_ptr = p;
3741 tok_flags = 0;
3742 #if defined(PARSE_DEBUG)
3743 printf("token = %s\n", get_tok_str(tok, &tokc));
3744 #endif
3747 /* return next token without macro substitution. Can read input from
3748 macro_ptr buffer */
3749 static void next_nomacro(void)
3751 if (macro_ptr) {
3752 redo:
3753 tok = *macro_ptr;
3754 if (tok) {
3755 TOK_GET(tok, macro_ptr, tokc);
3756 if (tok == TOK_LINENUM) {
3757 file->line_num = tokc.i;
3758 goto redo;
3761 } else {
3762 next_nomacro1();
3766 /* substitute args in macro_str and return allocated string */
3767 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3769 int *st, last_tok, t, notfirst;
3770 Sym *s;
3771 CValue cval;
3772 TokenString str;
3773 CString cstr;
3775 tok_str_new(&str);
3776 last_tok = 0;
3777 while(1) {
3778 TOK_GET(t, macro_str, cval);
3779 if (!t)
3780 break;
3781 if (t == '#') {
3782 /* stringize */
3783 TOK_GET(t, macro_str, cval);
3784 if (!t)
3785 break;
3786 s = sym_find2(args, t);
3787 if (s) {
3788 cstr_new(&cstr);
3789 st = (int *)s->c;
3790 notfirst = 0;
3791 while (*st) {
3792 if (notfirst)
3793 cstr_ccat(&cstr, ' ');
3794 TOK_GET(t, st, cval);
3795 cstr_cat(&cstr, get_tok_str(t, &cval));
3796 notfirst = 1;
3798 cstr_ccat(&cstr, '\0');
3799 #ifdef PP_DEBUG
3800 printf("stringize: %s\n", (char *)cstr.data);
3801 #endif
3802 /* add string */
3803 cval.cstr = &cstr;
3804 tok_str_add2(&str, TOK_STR, &cval);
3805 cstr_free(&cstr);
3806 } else {
3807 tok_str_add2(&str, t, &cval);
3809 } else if (t >= TOK_IDENT) {
3810 s = sym_find2(args, t);
3811 if (s) {
3812 st = (int *)s->c;
3813 /* if '##' is present before or after, no arg substitution */
3814 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3815 /* special case for var arg macros : ## eats the
3816 ',' if empty VA_ARGS variable. */
3817 /* XXX: test of the ',' is not 100%
3818 reliable. should fix it to avoid security
3819 problems */
3820 if (gnu_ext && s->type.t &&
3821 last_tok == TOK_TWOSHARPS &&
3822 str.len >= 2 && str.str[str.len - 2] == ',') {
3823 if (*st == 0) {
3824 /* suppress ',' '##' */
3825 str.len -= 2;
3826 } else {
3827 /* suppress '##' and add variable */
3828 str.len--;
3829 goto add_var;
3831 } else {
3832 int t1;
3833 add_var:
3834 for(;;) {
3835 TOK_GET(t1, st, cval);
3836 if (!t1)
3837 break;
3838 tok_str_add2(&str, t1, &cval);
3841 } else {
3842 /* NOTE: the stream cannot be read when macro
3843 substituing an argument */
3844 macro_subst(&str, nested_list, st, 0);
3846 } else {
3847 tok_str_add(&str, t);
3849 } else {
3850 tok_str_add2(&str, t, &cval);
3852 last_tok = t;
3854 tok_str_add(&str, 0);
3855 return str.str;
3858 static char const ab_month_name[12][4] =
3860 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3861 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3864 /* do macro substitution of current token with macro 's' and add
3865 result to (tok_str,tok_len). 'nested_list' is the list of all
3866 macros we got inside to avoid recursing. Return non zero if no
3867 substitution needs to be done */
3868 static int macro_subst_tok(TokenString *tok_str,
3869 Sym **nested_list, Sym *s, int can_read_stream)
3871 Sym *args, *sa, *sa1;
3872 int mstr_allocated, parlevel, *mstr, t, t1;
3873 TokenString str;
3874 char *cstrval;
3875 CValue cval;
3876 CString cstr;
3877 char buf[32];
3879 /* if symbol is a macro, prepare substitution */
3880 /* special macros */
3881 if (tok == TOK___LINE__) {
3882 snprintf(buf, sizeof(buf), "%d", file->line_num);
3883 cstrval = buf;
3884 t1 = TOK_PPNUM;
3885 goto add_cstr1;
3886 } else if (tok == TOK___FILE__) {
3887 cstrval = file->filename;
3888 goto add_cstr;
3889 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3890 time_t ti;
3891 struct tm *tm;
3893 time(&ti);
3894 tm = localtime(&ti);
3895 if (tok == TOK___DATE__) {
3896 snprintf(buf, sizeof(buf), "%s %2d %d",
3897 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3898 } else {
3899 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3900 tm->tm_hour, tm->tm_min, tm->tm_sec);
3902 cstrval = buf;
3903 add_cstr:
3904 t1 = TOK_STR;
3905 add_cstr1:
3906 cstr_new(&cstr);
3907 cstr_cat(&cstr, cstrval);
3908 cstr_ccat(&cstr, '\0');
3909 cval.cstr = &cstr;
3910 tok_str_add2(tok_str, t1, &cval);
3911 cstr_free(&cstr);
3912 } else {
3913 mstr = (int *)s->c;
3914 mstr_allocated = 0;
3915 if (s->type.t == MACRO_FUNC) {
3916 /* NOTE: we do not use next_nomacro to avoid eating the
3917 next token. XXX: find better solution */
3918 if (macro_ptr) {
3919 t = *macro_ptr;
3920 if (t == 0 && can_read_stream) {
3921 /* end of macro stream: we must look at the token
3922 after in the file */
3923 macro_ptr = NULL;
3924 goto parse_stream;
3926 } else {
3927 parse_stream:
3928 /* XXX: incorrect with comments */
3929 ch = file->buf_ptr[0];
3930 while (is_space(ch) || ch == '\n')
3931 cinp();
3932 t = ch;
3934 if (t != '(') /* no macro subst */
3935 return -1;
3937 /* argument macro */
3938 next_nomacro();
3939 next_nomacro();
3940 args = NULL;
3941 sa = s->next;
3942 /* NOTE: empty args are allowed, except if no args */
3943 for(;;) {
3944 /* handle '()' case */
3945 if (!args && !sa && tok == ')')
3946 break;
3947 if (!sa)
3948 error("macro '%s' used with too many args",
3949 get_tok_str(s->v, 0));
3950 tok_str_new(&str);
3951 parlevel = 0;
3952 /* NOTE: non zero sa->t indicates VA_ARGS */
3953 while ((parlevel > 0 ||
3954 (tok != ')' &&
3955 (tok != ',' || sa->type.t))) &&
3956 tok != -1) {
3957 if (tok == '(')
3958 parlevel++;
3959 else if (tok == ')')
3960 parlevel--;
3961 tok_str_add2(&str, tok, &tokc);
3962 next_nomacro();
3964 tok_str_add(&str, 0);
3965 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
3966 sa = sa->next;
3967 if (tok == ')') {
3968 /* special case for gcc var args: add an empty
3969 var arg argument if it is omitted */
3970 if (sa && sa->type.t && gnu_ext)
3971 continue;
3972 else
3973 break;
3975 if (tok != ',')
3976 expect(",");
3977 next_nomacro();
3979 if (sa) {
3980 error("macro '%s' used with too few args",
3981 get_tok_str(s->v, 0));
3984 /* now subst each arg */
3985 mstr = macro_arg_subst(nested_list, mstr, args);
3986 /* free memory */
3987 sa = args;
3988 while (sa) {
3989 sa1 = sa->prev;
3990 tok_str_free((int *)sa->c);
3991 tcc_free(sa);
3992 sa = sa1;
3994 mstr_allocated = 1;
3996 sym_push2(nested_list, s->v, 0, 0);
3997 macro_subst(tok_str, nested_list, mstr, 1);
3998 /* pop nested defined symbol */
3999 sa1 = *nested_list;
4000 *nested_list = sa1->prev;
4001 tcc_free(sa1);
4002 if (mstr_allocated)
4003 tok_str_free(mstr);
4005 return 0;
4008 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4009 return the resulting string (which must be freed). */
4010 static inline int *macro_twosharps(const int *macro_str)
4012 TokenSym *ts;
4013 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4014 int t;
4015 const char *p1, *p2;
4016 CValue cval;
4017 TokenString macro_str1;
4018 CString cstr;
4020 start_macro_ptr = macro_str;
4021 /* we search the first '##' */
4022 for(;;) {
4023 macro_ptr1 = macro_str;
4024 TOK_GET(t, macro_str, cval);
4025 /* nothing more to do if end of string */
4026 if (t == 0)
4027 return NULL;
4028 if (*macro_str == TOK_TWOSHARPS)
4029 break;
4032 /* we saw '##', so we need more processing to handle it */
4033 cstr_new(&cstr);
4034 tok_str_new(&macro_str1);
4035 tok = t;
4036 tokc = cval;
4038 /* add all tokens seen so far */
4039 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4040 TOK_GET(t, ptr, cval);
4041 tok_str_add2(&macro_str1, t, &cval);
4043 saved_macro_ptr = macro_ptr;
4044 /* XXX: get rid of the use of macro_ptr here */
4045 macro_ptr = (int *)macro_str;
4046 for(;;) {
4047 while (*macro_ptr == TOK_TWOSHARPS) {
4048 macro_ptr++;
4049 macro_ptr1 = macro_ptr;
4050 t = *macro_ptr;
4051 if (t) {
4052 TOK_GET(t, macro_ptr, cval);
4053 /* We concatenate the two tokens if we have an
4054 identifier or a preprocessing number */
4055 cstr_reset(&cstr);
4056 p1 = get_tok_str(tok, &tokc);
4057 cstr_cat(&cstr, p1);
4058 p2 = get_tok_str(t, &cval);
4059 cstr_cat(&cstr, p2);
4060 cstr_ccat(&cstr, '\0');
4062 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4063 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4064 if (tok == TOK_PPNUM) {
4065 /* if number, then create a number token */
4066 /* NOTE: no need to allocate because
4067 tok_str_add2() does it */
4068 tokc.cstr = &cstr;
4069 } else {
4070 /* if identifier, we must do a test to
4071 validate we have a correct identifier */
4072 if (t == TOK_PPNUM) {
4073 const char *p;
4074 int c;
4076 p = p2;
4077 for(;;) {
4078 c = *p;
4079 if (c == '\0')
4080 break;
4081 p++;
4082 if (!isnum(c) && !isid(c))
4083 goto error_pasting;
4086 ts = tok_alloc(cstr.data, strlen(cstr.data));
4087 tok = ts->tok; /* modify current token */
4089 } else {
4090 const char *str = cstr.data;
4091 const unsigned char *q;
4093 /* we look for a valid token */
4094 /* XXX: do more extensive checks */
4095 if (!strcmp(str, ">>=")) {
4096 tok = TOK_A_SAR;
4097 } else if (!strcmp(str, "<<=")) {
4098 tok = TOK_A_SHL;
4099 } else if (strlen(str) == 2) {
4100 /* search in two bytes table */
4101 q = tok_two_chars;
4102 for(;;) {
4103 if (!*q)
4104 goto error_pasting;
4105 if (q[0] == str[0] && q[1] == str[1])
4106 break;
4107 q += 3;
4109 tok = q[2];
4110 } else {
4111 error_pasting:
4112 /* NOTE: because get_tok_str use a static buffer,
4113 we must save it */
4114 cstr_reset(&cstr);
4115 p1 = get_tok_str(tok, &tokc);
4116 cstr_cat(&cstr, p1);
4117 cstr_ccat(&cstr, '\0');
4118 p2 = get_tok_str(t, &cval);
4119 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4120 /* cannot merge tokens: just add them separately */
4121 tok_str_add2(&macro_str1, tok, &tokc);
4122 /* XXX: free associated memory ? */
4123 tok = t;
4124 tokc = cval;
4129 tok_str_add2(&macro_str1, tok, &tokc);
4130 next_nomacro();
4131 if (tok == 0)
4132 break;
4134 macro_ptr = (int *)saved_macro_ptr;
4135 cstr_free(&cstr);
4136 tok_str_add(&macro_str1, 0);
4137 return macro_str1.str;
4141 /* do macro substitution of macro_str and add result to
4142 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4143 inside to avoid recursing. */
4144 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4145 const int *macro_str, int can_read_stream)
4147 Sym *s;
4148 int *saved_macro_ptr, *macro_str1;
4149 const int *ptr;
4150 int t, ret;
4151 CValue cval;
4153 /* first scan for '##' operator handling */
4154 ptr = macro_str;
4155 macro_str1 = macro_twosharps(ptr);
4156 if (macro_str1)
4157 ptr = macro_str1;
4158 while (1) {
4159 /* NOTE: ptr == NULL can only happen if tokens are read from
4160 file stream due to a macro function call */
4161 if (ptr == NULL)
4162 break;
4163 TOK_GET(t, ptr, cval);
4164 if (t == 0)
4165 break;
4166 s = define_find(t);
4167 if (s != NULL) {
4168 /* if nested substitution, do nothing */
4169 if (sym_find2(*nested_list, t))
4170 goto no_subst;
4171 saved_macro_ptr = macro_ptr;
4172 macro_ptr = (int *)ptr;
4173 tok = t;
4174 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4175 ptr = (int *)macro_ptr;
4176 macro_ptr = saved_macro_ptr;
4177 if (ret != 0)
4178 goto no_subst;
4179 } else {
4180 no_subst:
4181 tok_str_add2(tok_str, t, &cval);
4184 if (macro_str1)
4185 tok_str_free(macro_str1);
4188 /* return next token with macro substitution */
4189 static void next(void)
4191 Sym *nested_list, *s;
4192 TokenString str;
4194 redo:
4195 next_nomacro();
4196 if (!macro_ptr) {
4197 /* if not reading from macro substituted string, then try
4198 to substitute macros */
4199 if (tok >= TOK_IDENT &&
4200 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4201 s = define_find(tok);
4202 if (s) {
4203 /* we have a macro: we try to substitute */
4204 tok_str_new(&str);
4205 nested_list = NULL;
4206 if (macro_subst_tok(&str, &nested_list, s, 1) == 0) {
4207 /* substitution done, NOTE: maybe empty */
4208 tok_str_add(&str, 0);
4209 macro_ptr = str.str;
4210 macro_ptr_allocated = str.str;
4211 goto redo;
4215 } else {
4216 if (tok == 0) {
4217 /* end of macro or end of unget buffer */
4218 if (unget_buffer_enabled) {
4219 macro_ptr = unget_saved_macro_ptr;
4220 unget_buffer_enabled = 0;
4221 } else {
4222 /* end of macro string: free it */
4223 tok_str_free(macro_ptr_allocated);
4224 macro_ptr = NULL;
4226 goto redo;
4230 /* convert preprocessor tokens into C tokens */
4231 if (tok == TOK_PPNUM &&
4232 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4233 parse_number((char *)tokc.cstr->data);
4237 /* push back current token and set current token to 'last_tok'. Only
4238 identifier case handled for labels. */
4239 static inline void unget_tok(int last_tok)
4241 int i, n;
4242 int *q;
4243 unget_saved_macro_ptr = macro_ptr;
4244 unget_buffer_enabled = 1;
4245 q = unget_saved_buffer;
4246 macro_ptr = q;
4247 *q++ = tok;
4248 n = tok_ext_size(tok) - 1;
4249 for(i=0;i<n;i++)
4250 *q++ = tokc.tab[i];
4251 *q = 0; /* end of token string */
4252 tok = last_tok;
4256 void swap(int *p, int *q)
4258 int t;
4259 t = *p;
4260 *p = *q;
4261 *q = t;
4264 void vsetc(CType *type, int r, CValue *vc)
4266 int v;
4268 if (vtop >= vstack + VSTACK_SIZE)
4269 error("memory full");
4270 /* cannot let cpu flags if other instruction are generated. Also
4271 avoid leaving VT_JMP anywhere except on the top of the stack
4272 because it would complicate the code generator. */
4273 if (vtop >= vstack) {
4274 v = vtop->r & VT_VALMASK;
4275 if (v == VT_CMP || (v & ~1) == VT_JMP)
4276 gv(RC_INT);
4278 vtop++;
4279 vtop->type = *type;
4280 vtop->r = r;
4281 vtop->r2 = VT_CONST;
4282 vtop->c = *vc;
4285 /* push integer constant */
4286 void vpushi(int v)
4288 CValue cval;
4289 cval.i = v;
4290 vsetc(&int_type, VT_CONST, &cval);
4293 /* Return a static symbol pointing to a section */
4294 static Sym *get_sym_ref(CType *type, Section *sec,
4295 unsigned long offset, unsigned long size)
4297 int v;
4298 Sym *sym;
4300 v = anon_sym++;
4301 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4302 sym->type.ref = type->ref;
4303 sym->r = VT_CONST | VT_SYM;
4304 put_extern_sym(sym, sec, offset, size);
4305 return sym;
4308 /* push a reference to a section offset by adding a dummy symbol */
4309 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4311 CValue cval;
4313 cval.ul = 0;
4314 vsetc(type, VT_CONST | VT_SYM, &cval);
4315 vtop->sym = get_sym_ref(type, sec, offset, size);
4318 /* define a new external reference to a symbol 'v' of type 'u' */
4319 static Sym *external_global_sym(int v, CType *type, int r)
4321 Sym *s;
4323 s = sym_find(v);
4324 if (!s) {
4325 /* push forward reference */
4326 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4327 s->type.ref = type->ref;
4328 s->r = r | VT_CONST | VT_SYM;
4330 return s;
4333 /* define a new external reference to a symbol 'v' of type 'u' */
4334 static Sym *external_sym(int v, CType *type, int r)
4336 Sym *s;
4338 s = sym_find(v);
4339 if (!s) {
4340 /* push forward reference */
4341 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4342 s->type.t |= VT_EXTERN;
4343 } else {
4344 if (!is_compatible_types(&s->type, type))
4345 error("incompatible types for redefinition of '%s'",
4346 get_tok_str(v, NULL));
4348 return s;
4351 /* push a reference to global symbol v */
4352 static void vpush_global_sym(CType *type, int v)
4354 Sym *sym;
4355 CValue cval;
4357 sym = external_global_sym(v, type, 0);
4358 cval.ul = 0;
4359 vsetc(type, VT_CONST | VT_SYM, &cval);
4360 vtop->sym = sym;
4363 void vset(CType *type, int r, int v)
4365 CValue cval;
4367 cval.i = v;
4368 vsetc(type, r, &cval);
4371 void vseti(int r, int v)
4373 CType type;
4374 type.t = VT_INT;
4375 vset(&type, r, v);
4378 void vswap(void)
4380 SValue tmp;
4382 tmp = vtop[0];
4383 vtop[0] = vtop[-1];
4384 vtop[-1] = tmp;
4387 void vpushv(SValue *v)
4389 if (vtop >= vstack + VSTACK_SIZE)
4390 error("memory full");
4391 vtop++;
4392 *vtop = *v;
4395 void vdup(void)
4397 vpushv(vtop);
4400 /* save r to the memory stack, and mark it as being free */
4401 void save_reg(int r)
4403 int l, saved, size, align;
4404 SValue *p, sv;
4405 CType *type;
4407 /* modify all stack values */
4408 saved = 0;
4409 l = 0;
4410 for(p=vstack;p<=vtop;p++) {
4411 if ((p->r & VT_VALMASK) == r ||
4412 (p->r2 & VT_VALMASK) == r) {
4413 /* must save value on stack if not already done */
4414 if (!saved) {
4415 /* NOTE: must reload 'r' because r might be equal to r2 */
4416 r = p->r & VT_VALMASK;
4417 /* store register in the stack */
4418 type = &p->type;
4419 if ((p->r & VT_LVAL) ||
4420 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4421 type = &int_type;
4422 size = type_size(type, &align);
4423 loc = (loc - size) & -align;
4424 sv.type.t = type->t;
4425 sv.r = VT_LOCAL | VT_LVAL;
4426 sv.c.ul = loc;
4427 store(r, &sv);
4428 #ifdef TCC_TARGET_I386
4429 /* x86 specific: need to pop fp register ST0 if saved */
4430 if (r == TREG_ST0) {
4431 o(0xd9dd); /* fstp %st(1) */
4433 #endif
4434 /* special long long case */
4435 if ((type->t & VT_BTYPE) == VT_LLONG) {
4436 sv.c.ul += 4;
4437 store(p->r2, &sv);
4439 l = loc;
4440 saved = 1;
4442 /* mark that stack entry as being saved on the stack */
4443 if (p->r & VT_LVAL) {
4444 /* also clear the bounded flag because the
4445 relocation address of the function was stored in
4446 p->c.ul */
4447 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4448 } else {
4449 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4451 p->r2 = VT_CONST;
4452 p->c.ul = l;
4457 /* find a register of class 'rc2' with at most one reference on stack.
4458 * If none, call get_reg(rc) */
4459 int get_reg_ex(int rc, int rc2)
4461 int r;
4462 SValue *p;
4464 for(r=0;r<NB_REGS;r++) {
4465 if (reg_classes[r] & rc2) {
4466 int n;
4467 n=0;
4468 for(p = vstack; p <= vtop; p++) {
4469 if ((p->r & VT_VALMASK) == r ||
4470 (p->r2 & VT_VALMASK) == r)
4471 n++;
4473 if (n <= 1)
4474 return r;
4477 return get_reg(rc);
4480 /* find a free register of class 'rc'. If none, save one register */
4481 int get_reg(int rc)
4483 int r;
4484 SValue *p;
4486 /* find a free register */
4487 for(r=0;r<NB_REGS;r++) {
4488 if (reg_classes[r] & rc) {
4489 for(p=vstack;p<=vtop;p++) {
4490 if ((p->r & VT_VALMASK) == r ||
4491 (p->r2 & VT_VALMASK) == r)
4492 goto notfound;
4494 return r;
4496 notfound: ;
4499 /* no register left : free the first one on the stack (VERY
4500 IMPORTANT to start from the bottom to ensure that we don't
4501 spill registers used in gen_opi()) */
4502 for(p=vstack;p<=vtop;p++) {
4503 r = p->r & VT_VALMASK;
4504 if (r < VT_CONST && (reg_classes[r] & rc))
4505 goto save_found;
4506 /* also look at second register (if long long) */
4507 r = p->r2 & VT_VALMASK;
4508 if (r < VT_CONST && (reg_classes[r] & rc)) {
4509 save_found:
4510 save_reg(r);
4511 return r;
4514 /* Should never comes here */
4515 return -1;
4518 /* save registers up to (vtop - n) stack entry */
4519 void save_regs(int n)
4521 int r;
4522 SValue *p, *p1;
4523 p1 = vtop - n;
4524 for(p = vstack;p <= p1; p++) {
4525 r = p->r & VT_VALMASK;
4526 if (r < VT_CONST) {
4527 save_reg(r);
4532 /* move register 's' to 'r', and flush previous value of r to memory
4533 if needed */
4534 void move_reg(int r, int s)
4536 SValue sv;
4538 if (r != s) {
4539 save_reg(r);
4540 sv.type.t = VT_INT;
4541 sv.r = s;
4542 sv.c.ul = 0;
4543 load(r, &sv);
4547 /* get address of vtop (vtop MUST BE an lvalue) */
4548 void gaddrof(void)
4550 vtop->r &= ~VT_LVAL;
4551 /* tricky: if saved lvalue, then we can go back to lvalue */
4552 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4553 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4556 #ifdef CONFIG_TCC_BCHECK
4557 /* generate lvalue bound code */
4558 void gbound(void)
4560 int lval_type;
4561 CType type1;
4563 vtop->r &= ~VT_MUSTBOUND;
4564 /* if lvalue, then use checking code before dereferencing */
4565 if (vtop->r & VT_LVAL) {
4566 /* if not VT_BOUNDED value, then make one */
4567 if (!(vtop->r & VT_BOUNDED)) {
4568 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4569 /* must save type because we must set it to int to get pointer */
4570 type1 = vtop->type;
4571 vtop->type.t = VT_INT;
4572 gaddrof();
4573 vpushi(0);
4574 gen_bounded_ptr_add();
4575 vtop->r |= lval_type;
4576 vtop->type = type1;
4578 /* then check for dereferencing */
4579 gen_bounded_ptr_deref();
4582 #endif
4584 /* store vtop a register belonging to class 'rc'. lvalues are
4585 converted to values. Cannot be used if cannot be converted to
4586 register value (such as structures). */
4587 int gv(int rc)
4589 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4590 unsigned long long ll;
4592 /* NOTE: get_reg can modify vstack[] */
4593 if (vtop->type.t & VT_BITFIELD) {
4594 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4595 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4596 /* remove bit field info to avoid loops */
4597 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4598 /* generate shifts */
4599 vpushi(32 - (bit_pos + bit_size));
4600 gen_op(TOK_SHL);
4601 vpushi(32 - bit_size);
4602 /* NOTE: transformed to SHR if unsigned */
4603 gen_op(TOK_SAR);
4604 r = gv(rc);
4605 } else {
4606 if (is_float(vtop->type.t) &&
4607 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4608 Sym *sym;
4609 int *ptr;
4610 unsigned long offset;
4612 /* XXX: unify with initializers handling ? */
4613 /* CPUs usually cannot use float constants, so we store them
4614 generically in data segment */
4615 size = type_size(&vtop->type, &align);
4616 offset = (data_section->data_offset + align - 1) & -align;
4617 data_section->data_offset = offset;
4618 /* XXX: not portable yet */
4619 ptr = section_ptr_add(data_section, size);
4620 size = size >> 2;
4621 for(i=0;i<size;i++)
4622 ptr[i] = vtop->c.tab[i];
4623 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4624 vtop->r |= VT_LVAL | VT_SYM;
4625 vtop->sym = sym;
4626 vtop->c.ul = 0;
4628 #ifdef CONFIG_TCC_BCHECK
4629 if (vtop->r & VT_MUSTBOUND)
4630 gbound();
4631 #endif
4633 r = vtop->r & VT_VALMASK;
4634 /* need to reload if:
4635 - constant
4636 - lvalue (need to dereference pointer)
4637 - already a register, but not in the right class */
4638 if (r >= VT_CONST ||
4639 (vtop->r & VT_LVAL) ||
4640 !(reg_classes[r] & rc) ||
4641 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4642 !(reg_classes[vtop->r2] & rc))) {
4643 r = get_reg(rc);
4644 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4645 /* two register type load : expand to two words
4646 temporarily */
4647 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4648 /* load constant */
4649 ll = vtop->c.ull;
4650 vtop->c.ui = ll; /* first word */
4651 load(r, vtop);
4652 vtop->r = r; /* save register value */
4653 vpushi(ll >> 32); /* second word */
4654 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
4655 (vtop->r & VT_LVAL)) {
4656 /* We do not want to modifier the long long
4657 pointer here, so the safest (and less
4658 efficient) is to save all the other registers
4659 in the stack. XXX: totally inefficient. */
4660 save_regs(1);
4661 /* load from memory */
4662 load(r, vtop);
4663 vdup();
4664 vtop[-1].r = r; /* save register value */
4665 /* increment pointer to get second word */
4666 vtop->type.t = VT_INT;
4667 gaddrof();
4668 vpushi(4);
4669 gen_op('+');
4670 vtop->r |= VT_LVAL;
4671 } else {
4672 /* move registers */
4673 load(r, vtop);
4674 vdup();
4675 vtop[-1].r = r; /* save register value */
4676 vtop->r = vtop[-1].r2;
4678 /* allocate second register */
4679 rc2 = RC_INT;
4680 if (rc == RC_IRET)
4681 rc2 = RC_LRET;
4682 r2 = get_reg(rc2);
4683 load(r2, vtop);
4684 vpop();
4685 /* write second register */
4686 vtop->r2 = r2;
4687 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4688 int t1, t;
4689 /* lvalue of scalar type : need to use lvalue type
4690 because of possible cast */
4691 t = vtop->type.t;
4692 t1 = t;
4693 /* compute memory access type */
4694 if (vtop->r & VT_LVAL_BYTE)
4695 t = VT_BYTE;
4696 else if (vtop->r & VT_LVAL_SHORT)
4697 t = VT_SHORT;
4698 if (vtop->r & VT_LVAL_UNSIGNED)
4699 t |= VT_UNSIGNED;
4700 vtop->type.t = t;
4701 load(r, vtop);
4702 /* restore wanted type */
4703 vtop->type.t = t1;
4704 } else {
4705 /* one register type load */
4706 load(r, vtop);
4709 vtop->r = r;
4710 #ifdef TCC_TARGET_C67
4711 /* uses register pairs for doubles */
4712 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
4713 vtop->r2 = r+1;
4714 #endif
4716 return r;
4719 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4720 void gv2(int rc1, int rc2)
4722 int v;
4724 /* generate more generic register first. But VT_JMP or VT_CMP
4725 values must be generated first in all cases to avoid possible
4726 reload errors */
4727 v = vtop[0].r & VT_VALMASK;
4728 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4729 vswap();
4730 gv(rc1);
4731 vswap();
4732 gv(rc2);
4733 /* test if reload is needed for first register */
4734 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4735 vswap();
4736 gv(rc1);
4737 vswap();
4739 } else {
4740 gv(rc2);
4741 vswap();
4742 gv(rc1);
4743 vswap();
4744 /* test if reload is needed for first register */
4745 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4746 gv(rc2);
4751 /* expand long long on stack in two int registers */
4752 void lexpand(void)
4754 int u;
4756 u = vtop->type.t & VT_UNSIGNED;
4757 gv(RC_INT);
4758 vdup();
4759 vtop[0].r = vtop[-1].r2;
4760 vtop[0].r2 = VT_CONST;
4761 vtop[-1].r2 = VT_CONST;
4762 vtop[0].type.t = VT_INT | u;
4763 vtop[-1].type.t = VT_INT | u;
4766 #ifdef TCC_TARGET_ARM
4767 /* expand long long on stack */
4768 void lexpand_nr(void)
4770 int u,v;
4772 u = vtop->type.t & VT_UNSIGNED;
4773 vdup();
4774 vtop->r2 = VT_CONST;
4775 vtop->type.t = VT_INT | u;
4776 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
4777 if (v == VT_CONST) {
4778 vtop[-1].c.ui = vtop->c.ull;
4779 vtop->c.ui = vtop->c.ull >> 32;
4780 vtop->r = VT_CONST;
4781 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
4782 vtop->c.ui += 4;
4783 vtop->r = vtop[-1].r;
4784 } else if (v > VT_CONST) {
4785 vtop--;
4786 lexpand();
4787 } else
4788 vtop->r = vtop[-1].r2;
4789 vtop[-1].r2 = VT_CONST;
4790 vtop[-1].type.t = VT_INT | u;
4792 #endif
4794 /* build a long long from two ints */
4795 void lbuild(int t)
4797 gv2(RC_INT, RC_INT);
4798 vtop[-1].r2 = vtop[0].r;
4799 vtop[-1].type.t = t;
4800 vpop();
4803 /* rotate n first stack elements to the bottom
4804 I1 ... In -> I2 ... In I1 [top is right]
4806 void vrotb(int n)
4808 int i;
4809 SValue tmp;
4811 tmp = vtop[-n + 1];
4812 for(i=-n+1;i!=0;i++)
4813 vtop[i] = vtop[i+1];
4814 vtop[0] = tmp;
4817 /* rotate n first stack elements to the top
4818 I1 ... In -> In I1 ... I(n-1) [top is right]
4820 void vrott(int n)
4822 int i;
4823 SValue tmp;
4825 tmp = vtop[0];
4826 for(i = 0;i < n - 1; i++)
4827 vtop[-i] = vtop[-i - 1];
4828 vtop[-n + 1] = tmp;
4831 #ifdef TCC_TARGET_ARM
4832 /* like vrott but in other direction
4833 In ... I1 -> I(n-1) ... I1 In [top is right]
4835 void vnrott(int n)
4837 int i;
4838 SValue tmp;
4840 tmp = vtop[-n + 1];
4841 for(i = n - 1; i > 0; i--)
4842 vtop[-i] = vtop[-i + 1];
4843 vtop[0] = tmp;
4845 #endif
4847 /* pop stack value */
4848 void vpop(void)
4850 int v;
4851 v = vtop->r & VT_VALMASK;
4852 #ifdef TCC_TARGET_I386
4853 /* for x86, we need to pop the FP stack */
4854 if (v == TREG_ST0 && !nocode_wanted) {
4855 o(0xd9dd); /* fstp %st(1) */
4856 } else
4857 #endif
4858 if (v == VT_JMP || v == VT_JMPI) {
4859 /* need to put correct jump if && or || without test */
4860 gsym(vtop->c.ul);
4862 vtop--;
4865 /* convert stack entry to register and duplicate its value in another
4866 register */
4867 void gv_dup(void)
4869 int rc, t, r, r1;
4870 SValue sv;
4872 t = vtop->type.t;
4873 if ((t & VT_BTYPE) == VT_LLONG) {
4874 lexpand();
4875 gv_dup();
4876 vswap();
4877 vrotb(3);
4878 gv_dup();
4879 vrotb(4);
4880 /* stack: H L L1 H1 */
4881 lbuild(t);
4882 vrotb(3);
4883 vrotb(3);
4884 vswap();
4885 lbuild(t);
4886 vswap();
4887 } else {
4888 /* duplicate value */
4889 rc = RC_INT;
4890 sv.type.t = VT_INT;
4891 if (is_float(t)) {
4892 rc = RC_FLOAT;
4893 sv.type.t = t;
4895 r = gv(rc);
4896 r1 = get_reg(rc);
4897 sv.r = r;
4898 sv.c.ul = 0;
4899 load(r1, &sv); /* move r to r1 */
4900 vdup();
4901 /* duplicates value */
4902 vtop->r = r1;
4906 /* generate CPU independent (unsigned) long long operations */
4907 void gen_opl(int op)
4909 int t, a, b, op1, c, i;
4910 int func;
4911 SValue tmp;
4913 switch(op) {
4914 case '/':
4915 case TOK_PDIV:
4916 func = TOK___divdi3;
4917 goto gen_func;
4918 case TOK_UDIV:
4919 func = TOK___udivdi3;
4920 goto gen_func;
4921 case '%':
4922 func = TOK___moddi3;
4923 goto gen_func;
4924 case TOK_UMOD:
4925 func = TOK___umoddi3;
4926 gen_func:
4927 /* call generic long long function */
4928 vpush_global_sym(&func_old_type, func);
4929 vrott(3);
4930 gfunc_call(2);
4931 vpushi(0);
4932 vtop->r = REG_IRET;
4933 vtop->r2 = REG_LRET;
4934 break;
4935 case '^':
4936 case '&':
4937 case '|':
4938 case '*':
4939 case '+':
4940 case '-':
4941 t = vtop->type.t;
4942 vswap();
4943 lexpand();
4944 vrotb(3);
4945 lexpand();
4946 /* stack: L1 H1 L2 H2 */
4947 tmp = vtop[0];
4948 vtop[0] = vtop[-3];
4949 vtop[-3] = tmp;
4950 tmp = vtop[-2];
4951 vtop[-2] = vtop[-3];
4952 vtop[-3] = tmp;
4953 vswap();
4954 /* stack: H1 H2 L1 L2 */
4955 if (op == '*') {
4956 vpushv(vtop - 1);
4957 vpushv(vtop - 1);
4958 gen_op(TOK_UMULL);
4959 lexpand();
4960 /* stack: H1 H2 L1 L2 ML MH */
4961 for(i=0;i<4;i++)
4962 vrotb(6);
4963 /* stack: ML MH H1 H2 L1 L2 */
4964 tmp = vtop[0];
4965 vtop[0] = vtop[-2];
4966 vtop[-2] = tmp;
4967 /* stack: ML MH H1 L2 H2 L1 */
4968 gen_op('*');
4969 vrotb(3);
4970 vrotb(3);
4971 gen_op('*');
4972 /* stack: ML MH M1 M2 */
4973 gen_op('+');
4974 gen_op('+');
4975 } else if (op == '+' || op == '-') {
4976 /* XXX: add non carry method too (for MIPS or alpha) */
4977 if (op == '+')
4978 op1 = TOK_ADDC1;
4979 else
4980 op1 = TOK_SUBC1;
4981 gen_op(op1);
4982 /* stack: H1 H2 (L1 op L2) */
4983 vrotb(3);
4984 vrotb(3);
4985 gen_op(op1 + 1); /* TOK_xxxC2 */
4986 } else {
4987 gen_op(op);
4988 /* stack: H1 H2 (L1 op L2) */
4989 vrotb(3);
4990 vrotb(3);
4991 /* stack: (L1 op L2) H1 H2 */
4992 gen_op(op);
4993 /* stack: (L1 op L2) (H1 op H2) */
4995 /* stack: L H */
4996 lbuild(t);
4997 break;
4998 case TOK_SAR:
4999 case TOK_SHR:
5000 case TOK_SHL:
5001 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5002 t = vtop[-1].type.t;
5003 vswap();
5004 lexpand();
5005 vrotb(3);
5006 /* stack: L H shift */
5007 c = (int)vtop->c.i;
5008 /* constant: simpler */
5009 /* NOTE: all comments are for SHL. the other cases are
5010 done by swaping words */
5011 vpop();
5012 if (op != TOK_SHL)
5013 vswap();
5014 if (c >= 32) {
5015 /* stack: L H */
5016 vpop();
5017 if (c > 32) {
5018 vpushi(c - 32);
5019 gen_op(op);
5021 if (op != TOK_SAR) {
5022 vpushi(0);
5023 } else {
5024 gv_dup();
5025 vpushi(31);
5026 gen_op(TOK_SAR);
5028 vswap();
5029 } else {
5030 vswap();
5031 gv_dup();
5032 /* stack: H L L */
5033 vpushi(c);
5034 gen_op(op);
5035 vswap();
5036 vpushi(32 - c);
5037 if (op == TOK_SHL)
5038 gen_op(TOK_SHR);
5039 else
5040 gen_op(TOK_SHL);
5041 vrotb(3);
5042 /* stack: L L H */
5043 vpushi(c);
5044 if (op == TOK_SHL)
5045 gen_op(TOK_SHL);
5046 else
5047 gen_op(TOK_SHR);
5048 gen_op('|');
5050 if (op != TOK_SHL)
5051 vswap();
5052 lbuild(t);
5053 } else {
5054 /* XXX: should provide a faster fallback on x86 ? */
5055 switch(op) {
5056 case TOK_SAR:
5057 func = TOK___sardi3;
5058 goto gen_func;
5059 case TOK_SHR:
5060 func = TOK___shrdi3;
5061 goto gen_func;
5062 case TOK_SHL:
5063 func = TOK___shldi3;
5064 goto gen_func;
5067 break;
5068 default:
5069 /* compare operations */
5070 t = vtop->type.t;
5071 vswap();
5072 lexpand();
5073 vrotb(3);
5074 lexpand();
5075 /* stack: L1 H1 L2 H2 */
5076 tmp = vtop[-1];
5077 vtop[-1] = vtop[-2];
5078 vtop[-2] = tmp;
5079 /* stack: L1 L2 H1 H2 */
5080 /* compare high */
5081 op1 = op;
5082 /* when values are equal, we need to compare low words. since
5083 the jump is inverted, we invert the test too. */
5084 if (op1 == TOK_LT)
5085 op1 = TOK_LE;
5086 else if (op1 == TOK_GT)
5087 op1 = TOK_GE;
5088 else if (op1 == TOK_ULT)
5089 op1 = TOK_ULE;
5090 else if (op1 == TOK_UGT)
5091 op1 = TOK_UGE;
5092 a = 0;
5093 b = 0;
5094 gen_op(op1);
5095 if (op1 != TOK_NE) {
5096 a = gtst(1, 0);
5098 if (op != TOK_EQ) {
5099 /* generate non equal test */
5100 /* XXX: NOT PORTABLE yet */
5101 if (a == 0) {
5102 b = gtst(0, 0);
5103 } else {
5104 #if defined(TCC_TARGET_I386)
5105 b = psym(0x850f, 0);
5106 #elif defined(TCC_TARGET_ARM)
5107 b = ind;
5108 o(0x1A000000 | encbranch(ind, 0, 1));
5109 #elif defined(TCC_TARGET_C67)
5110 error("not implemented");
5111 #else
5112 #error not supported
5113 #endif
5116 /* compare low. Always unsigned */
5117 op1 = op;
5118 if (op1 == TOK_LT)
5119 op1 = TOK_ULT;
5120 else if (op1 == TOK_LE)
5121 op1 = TOK_ULE;
5122 else if (op1 == TOK_GT)
5123 op1 = TOK_UGT;
5124 else if (op1 == TOK_GE)
5125 op1 = TOK_UGE;
5126 gen_op(op1);
5127 a = gtst(1, a);
5128 gsym(b);
5129 vseti(VT_JMPI, a);
5130 break;
5134 /* handle integer constant optimizations and various machine
5135 independent opt */
5136 void gen_opic(int op)
5138 int fc, c1, c2, n;
5139 SValue *v1, *v2;
5141 v1 = vtop - 1;
5142 v2 = vtop;
5143 /* currently, we cannot do computations with forward symbols */
5144 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5145 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5146 if (c1 && c2) {
5147 fc = v2->c.i;
5148 switch(op) {
5149 case '+': v1->c.i += fc; break;
5150 case '-': v1->c.i -= fc; break;
5151 case '&': v1->c.i &= fc; break;
5152 case '^': v1->c.i ^= fc; break;
5153 case '|': v1->c.i |= fc; break;
5154 case '*': v1->c.i *= fc; break;
5156 case TOK_PDIV:
5157 case '/':
5158 case '%':
5159 case TOK_UDIV:
5160 case TOK_UMOD:
5161 /* if division by zero, generate explicit division */
5162 if (fc == 0) {
5163 if (const_wanted)
5164 error("division by zero in constant");
5165 goto general_case;
5167 switch(op) {
5168 default: v1->c.i /= fc; break;
5169 case '%': v1->c.i %= fc; break;
5170 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
5171 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
5173 break;
5174 case TOK_SHL: v1->c.i <<= fc; break;
5175 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
5176 case TOK_SAR: v1->c.i >>= fc; break;
5177 /* tests */
5178 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
5179 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
5180 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
5181 case TOK_NE: v1->c.i = v1->c.i != fc; break;
5182 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
5183 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
5184 case TOK_LT: v1->c.i = v1->c.i < fc; break;
5185 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
5186 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
5187 case TOK_GT: v1->c.i = v1->c.i > fc; break;
5188 /* logical */
5189 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
5190 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
5191 default:
5192 goto general_case;
5194 vtop--;
5195 } else {
5196 /* if commutative ops, put c2 as constant */
5197 if (c1 && (op == '+' || op == '&' || op == '^' ||
5198 op == '|' || op == '*')) {
5199 vswap();
5200 swap(&c1, &c2);
5202 fc = vtop->c.i;
5203 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5204 op == TOK_PDIV) &&
5205 fc == 1) ||
5206 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5207 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5208 fc == 0) ||
5209 (op == '&' &&
5210 fc == -1))) {
5211 /* nothing to do */
5212 vtop--;
5213 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5214 /* try to use shifts instead of muls or divs */
5215 if (fc > 0 && (fc & (fc - 1)) == 0) {
5216 n = -1;
5217 while (fc) {
5218 fc >>= 1;
5219 n++;
5221 vtop->c.i = n;
5222 if (op == '*')
5223 op = TOK_SHL;
5224 else if (op == TOK_PDIV)
5225 op = TOK_SAR;
5226 else
5227 op = TOK_SHR;
5229 goto general_case;
5230 } else if (c2 && (op == '+' || op == '-') &&
5231 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5232 (VT_CONST | VT_SYM)) {
5233 /* symbol + constant case */
5234 if (op == '-')
5235 fc = -fc;
5236 vtop--;
5237 vtop->c.i += fc;
5238 } else {
5239 general_case:
5240 if (!nocode_wanted) {
5241 /* call low level op generator */
5242 gen_opi(op);
5243 } else {
5244 vtop--;
5250 /* generate a floating point operation with constant propagation */
5251 void gen_opif(int op)
5253 int c1, c2;
5254 SValue *v1, *v2;
5255 long double f1, f2;
5257 v1 = vtop - 1;
5258 v2 = vtop;
5259 /* currently, we cannot do computations with forward symbols */
5260 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5261 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5262 if (c1 && c2) {
5263 if (v1->type.t == VT_FLOAT) {
5264 f1 = v1->c.f;
5265 f2 = v2->c.f;
5266 } else if (v1->type.t == VT_DOUBLE) {
5267 f1 = v1->c.d;
5268 f2 = v2->c.d;
5269 } else {
5270 f1 = v1->c.ld;
5271 f2 = v2->c.ld;
5274 /* NOTE: we only do constant propagation if finite number (not
5275 NaN or infinity) (ANSI spec) */
5276 if (!ieee_finite(f1) || !ieee_finite(f2))
5277 goto general_case;
5279 switch(op) {
5280 case '+': f1 += f2; break;
5281 case '-': f1 -= f2; break;
5282 case '*': f1 *= f2; break;
5283 case '/':
5284 if (f2 == 0.0) {
5285 if (const_wanted)
5286 error("division by zero in constant");
5287 goto general_case;
5289 f1 /= f2;
5290 break;
5291 /* XXX: also handles tests ? */
5292 default:
5293 goto general_case;
5295 /* XXX: overflow test ? */
5296 if (v1->type.t == VT_FLOAT) {
5297 v1->c.f = f1;
5298 } else if (v1->type.t == VT_DOUBLE) {
5299 v1->c.d = f1;
5300 } else {
5301 v1->c.ld = f1;
5303 vtop--;
5304 } else {
5305 general_case:
5306 if (!nocode_wanted) {
5307 gen_opf(op);
5308 } else {
5309 vtop--;
5314 static int pointed_size(CType *type)
5316 int align;
5317 return type_size(pointed_type(type), &align);
5320 static inline int is_null_pointer(SValue *p)
5322 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5323 return 0;
5324 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5325 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5328 static inline int is_integer_btype(int bt)
5330 return (bt == VT_BYTE || bt == VT_SHORT ||
5331 bt == VT_INT || bt == VT_LLONG);
5334 /* check types for comparison or substraction of pointers */
5335 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5337 CType *type1, *type2, tmp_type1, tmp_type2;
5338 int bt1, bt2;
5340 /* null pointers are accepted for all comparisons as gcc */
5341 if (is_null_pointer(p1) || is_null_pointer(p2))
5342 return;
5343 type1 = &p1->type;
5344 type2 = &p2->type;
5345 bt1 = type1->t & VT_BTYPE;
5346 bt2 = type2->t & VT_BTYPE;
5347 /* accept comparison between pointer and integer with a warning */
5348 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5349 warning("comparison between pointer and integer");
5350 return;
5353 /* both must be pointers or implicit function pointers */
5354 if (bt1 == VT_PTR) {
5355 type1 = pointed_type(type1);
5356 } else if (bt1 != VT_FUNC)
5357 goto invalid_operands;
5359 if (bt2 == VT_PTR) {
5360 type2 = pointed_type(type2);
5361 } else if (bt2 != VT_FUNC) {
5362 invalid_operands:
5363 error("invalid operands to binary %s", get_tok_str(op, NULL));
5365 if ((type1->t & VT_BTYPE) == VT_VOID ||
5366 (type2->t & VT_BTYPE) == VT_VOID)
5367 return;
5368 tmp_type1 = *type1;
5369 tmp_type2 = *type2;
5370 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5371 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5372 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5373 /* gcc-like error if '-' is used */
5374 if (op == '-')
5375 goto invalid_operands;
5376 else
5377 warning("comparison of distinct pointer types lacks a cast");
5381 /* generic gen_op: handles types problems */
5382 void gen_op(int op)
5384 int u, t1, t2, bt1, bt2, t;
5385 CType type1;
5387 t1 = vtop[-1].type.t;
5388 t2 = vtop[0].type.t;
5389 bt1 = t1 & VT_BTYPE;
5390 bt2 = t2 & VT_BTYPE;
5392 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5393 /* at least one operand is a pointer */
5394 /* relationnal op: must be both pointers */
5395 if (op >= TOK_ULT && op <= TOK_GT) {
5396 check_comparison_pointer_types(vtop - 1, vtop, op);
5397 /* pointers are handled are unsigned */
5398 t = VT_INT | VT_UNSIGNED;
5399 goto std_op;
5401 /* if both pointers, then it must be the '-' op */
5402 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5403 if (op != '-')
5404 error("cannot use pointers here");
5405 check_comparison_pointer_types(vtop - 1, vtop, op);
5406 /* XXX: check that types are compatible */
5407 u = pointed_size(&vtop[-1].type);
5408 gen_opic(op);
5409 /* set to integer type */
5410 vtop->type.t = VT_INT;
5411 vpushi(u);
5412 gen_op(TOK_PDIV);
5413 } else {
5414 /* exactly one pointer : must be '+' or '-'. */
5415 if (op != '-' && op != '+')
5416 error("cannot use pointers here");
5417 /* Put pointer as first operand */
5418 if (bt2 == VT_PTR) {
5419 vswap();
5420 swap(&t1, &t2);
5422 type1 = vtop[-1].type;
5423 /* XXX: cast to int ? (long long case) */
5424 vpushi(pointed_size(&vtop[-1].type));
5425 gen_op('*');
5426 #ifdef CONFIG_TCC_BCHECK
5427 /* if evaluating constant expression, no code should be
5428 generated, so no bound check */
5429 if (do_bounds_check && !const_wanted) {
5430 /* if bounded pointers, we generate a special code to
5431 test bounds */
5432 if (op == '-') {
5433 vpushi(0);
5434 vswap();
5435 gen_op('-');
5437 gen_bounded_ptr_add();
5438 } else
5439 #endif
5441 gen_opic(op);
5443 /* put again type if gen_opic() swaped operands */
5444 vtop->type = type1;
5446 } else if (is_float(bt1) || is_float(bt2)) {
5447 /* compute bigger type and do implicit casts */
5448 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5449 t = VT_LDOUBLE;
5450 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5451 t = VT_DOUBLE;
5452 } else {
5453 t = VT_FLOAT;
5455 /* floats can only be used for a few operations */
5456 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5457 (op < TOK_ULT || op > TOK_GT))
5458 error("invalid operands for binary operation");
5459 goto std_op;
5460 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5461 /* cast to biggest op */
5462 t = VT_LLONG;
5463 /* convert to unsigned if it does not fit in a long long */
5464 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5465 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5466 t |= VT_UNSIGNED;
5467 goto std_op;
5468 } else {
5469 /* integer operations */
5470 t = VT_INT;
5471 /* convert to unsigned if it does not fit in an integer */
5472 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5473 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5474 t |= VT_UNSIGNED;
5475 std_op:
5476 /* XXX: currently, some unsigned operations are explicit, so
5477 we modify them here */
5478 if (t & VT_UNSIGNED) {
5479 if (op == TOK_SAR)
5480 op = TOK_SHR;
5481 else if (op == '/')
5482 op = TOK_UDIV;
5483 else if (op == '%')
5484 op = TOK_UMOD;
5485 else if (op == TOK_LT)
5486 op = TOK_ULT;
5487 else if (op == TOK_GT)
5488 op = TOK_UGT;
5489 else if (op == TOK_LE)
5490 op = TOK_ULE;
5491 else if (op == TOK_GE)
5492 op = TOK_UGE;
5494 vswap();
5495 type1.t = t;
5496 gen_cast(&type1);
5497 vswap();
5498 /* special case for shifts and long long: we keep the shift as
5499 an integer */
5500 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5501 type1.t = VT_INT;
5502 gen_cast(&type1);
5503 if (is_float(t))
5504 gen_opif(op);
5505 else if ((t & VT_BTYPE) == VT_LLONG)
5506 gen_opl(op);
5507 else
5508 gen_opic(op);
5509 if (op >= TOK_ULT && op <= TOK_GT) {
5510 /* relationnal op: the result is an int */
5511 vtop->type.t = VT_INT;
5512 } else {
5513 vtop->type.t = t;
5518 /* generic itof for unsigned long long case */
5519 void gen_cvt_itof1(int t)
5521 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5522 (VT_LLONG | VT_UNSIGNED)) {
5524 if (t == VT_FLOAT)
5525 vpush_global_sym(&func_old_type, TOK___ulltof);
5526 else if (t == VT_DOUBLE)
5527 vpush_global_sym(&func_old_type, TOK___ulltod);
5528 else
5529 vpush_global_sym(&func_old_type, TOK___ulltold);
5530 vrott(2);
5531 gfunc_call(1);
5532 vpushi(0);
5533 vtop->r = REG_FRET;
5534 } else {
5535 gen_cvt_itof(t);
5539 /* generic ftoi for unsigned long long case */
5540 void gen_cvt_ftoi1(int t)
5542 int st;
5544 if (t == (VT_LLONG | VT_UNSIGNED)) {
5545 /* not handled natively */
5546 st = vtop->type.t & VT_BTYPE;
5547 if (st == VT_FLOAT)
5548 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5549 else if (st == VT_DOUBLE)
5550 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5551 else
5552 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5553 vrott(2);
5554 gfunc_call(1);
5555 vpushi(0);
5556 vtop->r = REG_IRET;
5557 vtop->r2 = REG_LRET;
5558 } else {
5559 gen_cvt_ftoi(t);
5563 /* force char or short cast */
5564 void force_charshort_cast(int t)
5566 int bits, dbt;
5567 dbt = t & VT_BTYPE;
5568 /* XXX: add optimization if lvalue : just change type and offset */
5569 if (dbt == VT_BYTE)
5570 bits = 8;
5571 else
5572 bits = 16;
5573 if (t & VT_UNSIGNED) {
5574 vpushi((1 << bits) - 1);
5575 gen_op('&');
5576 } else {
5577 bits = 32 - bits;
5578 vpushi(bits);
5579 gen_op(TOK_SHL);
5580 vpushi(bits);
5581 gen_op(TOK_SAR);
5585 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5586 static void gen_cast(CType *type)
5588 int sbt, dbt, sf, df, c;
5590 /* special delayed cast for char/short */
5591 /* XXX: in some cases (multiple cascaded casts), it may still
5592 be incorrect */
5593 if (vtop->r & VT_MUSTCAST) {
5594 vtop->r &= ~VT_MUSTCAST;
5595 force_charshort_cast(vtop->type.t);
5598 /* bitfields first get cast to ints */
5599 if (vtop->type.t & VT_BITFIELD) {
5600 gv(RC_INT);
5603 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5604 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5606 if (sbt != dbt && !nocode_wanted) {
5607 sf = is_float(sbt);
5608 df = is_float(dbt);
5609 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5610 if (sf && df) {
5611 /* convert from fp to fp */
5612 if (c) {
5613 /* constant case: we can do it now */
5614 /* XXX: in ISOC, cannot do it if error in convert */
5615 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5616 vtop->c.f = (float)vtop->c.d;
5617 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5618 vtop->c.f = (float)vtop->c.ld;
5619 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5620 vtop->c.d = (double)vtop->c.f;
5621 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5622 vtop->c.d = (double)vtop->c.ld;
5623 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5624 vtop->c.ld = (long double)vtop->c.f;
5625 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5626 vtop->c.ld = (long double)vtop->c.d;
5627 } else {
5628 /* non constant case: generate code */
5629 gen_cvt_ftof(dbt);
5631 } else if (df) {
5632 /* convert int to fp */
5633 if (c) {
5634 switch(sbt) {
5635 case VT_LLONG | VT_UNSIGNED:
5636 case VT_LLONG:
5637 /* XXX: add const cases for long long */
5638 goto do_itof;
5639 case VT_INT | VT_UNSIGNED:
5640 switch(dbt) {
5641 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5642 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5643 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5645 break;
5646 default:
5647 switch(dbt) {
5648 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5649 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5650 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5652 break;
5654 } else {
5655 do_itof:
5656 #if !defined(TCC_TARGET_ARM)
5657 gen_cvt_itof1(dbt);
5658 #else
5659 gen_cvt_itof(dbt);
5660 #endif
5662 } else if (sf) {
5663 /* convert fp to int */
5664 /* we handle char/short/etc... with generic code */
5665 if (dbt != (VT_INT | VT_UNSIGNED) &&
5666 dbt != (VT_LLONG | VT_UNSIGNED) &&
5667 dbt != VT_LLONG)
5668 dbt = VT_INT;
5669 if (c) {
5670 switch(dbt) {
5671 case VT_LLONG | VT_UNSIGNED:
5672 case VT_LLONG:
5673 /* XXX: add const cases for long long */
5674 goto do_ftoi;
5675 case VT_INT | VT_UNSIGNED:
5676 switch(sbt) {
5677 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5678 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5679 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5681 break;
5682 default:
5683 /* int case */
5684 switch(sbt) {
5685 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5686 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5687 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5689 break;
5691 } else {
5692 do_ftoi:
5693 gen_cvt_ftoi1(dbt);
5695 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5696 /* additional cast for char/short/bool... */
5697 vtop->type.t = dbt;
5698 gen_cast(type);
5700 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5701 if ((sbt & VT_BTYPE) != VT_LLONG) {
5702 /* scalar to long long */
5703 if (c) {
5704 if (sbt == (VT_INT | VT_UNSIGNED))
5705 vtop->c.ll = vtop->c.ui;
5706 else
5707 vtop->c.ll = vtop->c.i;
5708 } else {
5709 /* machine independent conversion */
5710 gv(RC_INT);
5711 /* generate high word */
5712 if (sbt == (VT_INT | VT_UNSIGNED)) {
5713 vpushi(0);
5714 gv(RC_INT);
5715 } else {
5716 gv_dup();
5717 vpushi(31);
5718 gen_op(TOK_SAR);
5720 /* patch second register */
5721 vtop[-1].r2 = vtop->r;
5722 vpop();
5725 } else if (dbt == VT_BOOL) {
5726 /* scalar to bool */
5727 vpushi(0);
5728 gen_op(TOK_NE);
5729 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5730 (dbt & VT_BTYPE) == VT_SHORT) {
5731 force_charshort_cast(dbt);
5732 } else if ((dbt & VT_BTYPE) == VT_INT) {
5733 /* scalar to int */
5734 if (sbt == VT_LLONG) {
5735 /* from long long: just take low order word */
5736 lexpand();
5737 vpop();
5739 /* if lvalue and single word type, nothing to do because
5740 the lvalue already contains the real type size (see
5741 VT_LVAL_xxx constants) */
5744 vtop->type = *type;
5747 /* return type size. Put alignment at 'a' */
5748 static int type_size(CType *type, int *a)
5750 Sym *s;
5751 int bt;
5753 bt = type->t & VT_BTYPE;
5754 if (bt == VT_STRUCT) {
5755 /* struct/union */
5756 s = type->ref;
5757 *a = s->r;
5758 return s->c;
5759 } else if (bt == VT_PTR) {
5760 if (type->t & VT_ARRAY) {
5761 s = type->ref;
5762 return type_size(&s->type, a) * s->c;
5763 } else {
5764 *a = PTR_SIZE;
5765 return PTR_SIZE;
5767 } else if (bt == VT_LDOUBLE) {
5768 *a = LDOUBLE_ALIGN;
5769 return LDOUBLE_SIZE;
5770 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5771 #ifdef TCC_TARGET_I386
5772 *a = 4;
5773 #else
5774 *a = 8;
5775 #endif
5776 return 8;
5777 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5778 *a = 4;
5779 return 4;
5780 } else if (bt == VT_SHORT) {
5781 *a = 2;
5782 return 2;
5783 } else {
5784 /* char, void, function, _Bool */
5785 *a = 1;
5786 return 1;
5790 /* return the pointed type of t */
5791 static inline CType *pointed_type(CType *type)
5793 return &type->ref->type;
5796 /* modify type so that its it is a pointer to type. */
5797 static void mk_pointer(CType *type)
5799 Sym *s;
5800 s = sym_push(SYM_FIELD, type, 0, -1);
5801 type->t = VT_PTR | (type->t & ~VT_TYPE);
5802 type->ref = s;
5805 /* compare function types. OLD functions match any new functions */
5806 static int is_compatible_func(CType *type1, CType *type2)
5808 Sym *s1, *s2;
5810 s1 = type1->ref;
5811 s2 = type2->ref;
5812 if (!is_compatible_types(&s1->type, &s2->type))
5813 return 0;
5814 /* check func_call */
5815 if (s1->r != s2->r)
5816 return 0;
5817 /* XXX: not complete */
5818 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5819 return 1;
5820 if (s1->c != s2->c)
5821 return 0;
5822 while (s1 != NULL) {
5823 if (s2 == NULL)
5824 return 0;
5825 if (!is_compatible_types(&s1->type, &s2->type))
5826 return 0;
5827 s1 = s1->next;
5828 s2 = s2->next;
5830 if (s2)
5831 return 0;
5832 return 1;
5835 /* return true if type1 and type2 are exactly the same (including
5836 qualifiers).
5838 - enums are not checked as gcc __builtin_types_compatible_p ()
5840 static int is_compatible_types(CType *type1, CType *type2)
5842 int bt1, t1, t2;
5844 t1 = type1->t & VT_TYPE;
5845 t2 = type2->t & VT_TYPE;
5846 /* XXX: bitfields ? */
5847 if (t1 != t2)
5848 return 0;
5849 /* test more complicated cases */
5850 bt1 = t1 & VT_BTYPE;
5851 if (bt1 == VT_PTR) {
5852 type1 = pointed_type(type1);
5853 type2 = pointed_type(type2);
5854 return is_compatible_types(type1, type2);
5855 } else if (bt1 == VT_STRUCT) {
5856 return (type1->ref == type2->ref);
5857 } else if (bt1 == VT_FUNC) {
5858 return is_compatible_func(type1, type2);
5859 } else {
5860 return 1;
5864 /* print a type. If 'varstr' is not NULL, then the variable is also
5865 printed in the type */
5866 /* XXX: union */
5867 /* XXX: add array and function pointers */
5868 void type_to_str(char *buf, int buf_size,
5869 CType *type, const char *varstr)
5871 int bt, v, t;
5872 Sym *s, *sa;
5873 char buf1[256];
5874 const char *tstr;
5876 t = type->t & VT_TYPE;
5877 bt = t & VT_BTYPE;
5878 buf[0] = '\0';
5879 if (t & VT_CONSTANT)
5880 pstrcat(buf, buf_size, "const ");
5881 if (t & VT_VOLATILE)
5882 pstrcat(buf, buf_size, "volatile ");
5883 if (t & VT_UNSIGNED)
5884 pstrcat(buf, buf_size, "unsigned ");
5885 switch(bt) {
5886 case VT_VOID:
5887 tstr = "void";
5888 goto add_tstr;
5889 case VT_BOOL:
5890 tstr = "_Bool";
5891 goto add_tstr;
5892 case VT_BYTE:
5893 tstr = "char";
5894 goto add_tstr;
5895 case VT_SHORT:
5896 tstr = "short";
5897 goto add_tstr;
5898 case VT_INT:
5899 tstr = "int";
5900 goto add_tstr;
5901 case VT_LONG:
5902 tstr = "long";
5903 goto add_tstr;
5904 case VT_LLONG:
5905 tstr = "long long";
5906 goto add_tstr;
5907 case VT_FLOAT:
5908 tstr = "float";
5909 goto add_tstr;
5910 case VT_DOUBLE:
5911 tstr = "double";
5912 goto add_tstr;
5913 case VT_LDOUBLE:
5914 tstr = "long double";
5915 add_tstr:
5916 pstrcat(buf, buf_size, tstr);
5917 break;
5918 case VT_ENUM:
5919 case VT_STRUCT:
5920 if (bt == VT_STRUCT)
5921 tstr = "struct ";
5922 else
5923 tstr = "enum ";
5924 pstrcat(buf, buf_size, tstr);
5925 v = type->ref->v & ~SYM_STRUCT;
5926 if (v >= SYM_FIRST_ANOM)
5927 pstrcat(buf, buf_size, "<anonymous>");
5928 else
5929 pstrcat(buf, buf_size, get_tok_str(v, NULL));
5930 break;
5931 case VT_FUNC:
5932 s = type->ref;
5933 type_to_str(buf, buf_size, &s->type, varstr);
5934 pstrcat(buf, buf_size, "(");
5935 sa = s->next;
5936 while (sa != NULL) {
5937 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
5938 pstrcat(buf, buf_size, buf1);
5939 sa = sa->next;
5940 if (sa)
5941 pstrcat(buf, buf_size, ", ");
5943 pstrcat(buf, buf_size, ")");
5944 goto no_var;
5945 case VT_PTR:
5946 s = type->ref;
5947 pstrcpy(buf1, sizeof(buf1), "*");
5948 if (varstr)
5949 pstrcat(buf1, sizeof(buf1), varstr);
5950 type_to_str(buf, buf_size, &s->type, buf1);
5951 goto no_var;
5953 if (varstr) {
5954 pstrcat(buf, buf_size, " ");
5955 pstrcat(buf, buf_size, varstr);
5957 no_var: ;
5960 /* verify type compatibility to store vtop in 'dt' type, and generate
5961 casts if needed. */
5962 static void gen_assign_cast(CType *dt)
5964 CType *st, *type1, *type2, tmp_type1, tmp_type2;
5965 char buf1[256], buf2[256];
5966 int dbt, sbt;
5968 st = &vtop->type; /* source type */
5969 dbt = dt->t & VT_BTYPE;
5970 sbt = st->t & VT_BTYPE;
5971 if (dt->t & VT_CONSTANT)
5972 warning("assignment of read-only location");
5973 switch(dbt) {
5974 case VT_PTR:
5975 /* special cases for pointers */
5976 /* '0' can also be a pointer */
5977 if (is_null_pointer(vtop))
5978 goto type_ok;
5979 /* accept implicit pointer to integer cast with warning */
5980 if (is_integer_btype(sbt)) {
5981 warning("assignment makes pointer from integer without a cast");
5982 goto type_ok;
5984 type1 = pointed_type(dt);
5985 /* a function is implicitely a function pointer */
5986 if (sbt == VT_FUNC) {
5987 if ((type1->t & VT_BTYPE) != VT_VOID &&
5988 !is_compatible_types(pointed_type(dt), st))
5989 goto error;
5990 else
5991 goto type_ok;
5993 if (sbt != VT_PTR)
5994 goto error;
5995 type2 = pointed_type(st);
5996 if ((type1->t & VT_BTYPE) == VT_VOID ||
5997 (type2->t & VT_BTYPE) == VT_VOID) {
5998 /* void * can match anything */
5999 } else {
6000 /* exact type match, except for unsigned */
6001 tmp_type1 = *type1;
6002 tmp_type2 = *type2;
6003 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6004 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6005 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6006 goto error;
6008 /* check const and volatile */
6009 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6010 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6011 warning("assignment discards qualifiers from pointer target type");
6012 break;
6013 case VT_BYTE:
6014 case VT_SHORT:
6015 case VT_INT:
6016 case VT_LLONG:
6017 if (sbt == VT_PTR || sbt == VT_FUNC) {
6018 warning("assignment makes integer from pointer without a cast");
6020 /* XXX: more tests */
6021 break;
6022 case VT_STRUCT:
6023 tmp_type1 = *dt;
6024 tmp_type2 = *st;
6025 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6026 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6027 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6028 error:
6029 type_to_str(buf1, sizeof(buf1), st, NULL);
6030 type_to_str(buf2, sizeof(buf2), dt, NULL);
6031 error("cannot cast '%s' to '%s'", buf1, buf2);
6033 break;
6035 type_ok:
6036 gen_cast(dt);
6039 /* store vtop in lvalue pushed on stack */
6040 void vstore(void)
6042 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6044 ft = vtop[-1].type.t;
6045 sbt = vtop->type.t & VT_BTYPE;
6046 dbt = ft & VT_BTYPE;
6047 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6048 (sbt == VT_INT && dbt == VT_SHORT)) {
6049 /* optimize char/short casts */
6050 delayed_cast = VT_MUSTCAST;
6051 vtop->type.t = ft & VT_TYPE;
6052 /* XXX: factorize */
6053 if (ft & VT_CONSTANT)
6054 warning("assignment of read-only location");
6055 } else {
6056 delayed_cast = 0;
6057 if (!(ft & VT_BITFIELD))
6058 gen_assign_cast(&vtop[-1].type);
6061 if (sbt == VT_STRUCT) {
6062 /* if structure, only generate pointer */
6063 /* structure assignment : generate memcpy */
6064 /* XXX: optimize if small size */
6065 if (!nocode_wanted) {
6066 size = type_size(&vtop->type, &align);
6068 vpush_global_sym(&func_old_type, TOK_memcpy);
6070 /* destination */
6071 vpushv(vtop - 2);
6072 vtop->type.t = VT_INT;
6073 gaddrof();
6074 /* source */
6075 vpushv(vtop - 2);
6076 vtop->type.t = VT_INT;
6077 gaddrof();
6078 /* type size */
6079 vpushi(size);
6080 gfunc_call(3);
6082 vswap();
6083 vpop();
6084 } else {
6085 vswap();
6086 vpop();
6088 /* leave source on stack */
6089 } else if (ft & VT_BITFIELD) {
6090 /* bitfield store handling */
6091 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6092 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6093 /* remove bit field info to avoid loops */
6094 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6096 /* duplicate destination */
6097 vdup();
6098 vtop[-1] = vtop[-2];
6100 /* mask and shift source */
6101 vpushi((1 << bit_size) - 1);
6102 gen_op('&');
6103 vpushi(bit_pos);
6104 gen_op(TOK_SHL);
6105 /* load destination, mask and or with source */
6106 vswap();
6107 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6108 gen_op('&');
6109 gen_op('|');
6110 /* store result */
6111 vstore();
6112 } else {
6113 #ifdef CONFIG_TCC_BCHECK
6114 /* bound check case */
6115 if (vtop[-1].r & VT_MUSTBOUND) {
6116 vswap();
6117 gbound();
6118 vswap();
6120 #endif
6121 if (!nocode_wanted) {
6122 rc = RC_INT;
6123 if (is_float(ft))
6124 rc = RC_FLOAT;
6125 r = gv(rc); /* generate value */
6126 /* if lvalue was saved on stack, must read it */
6127 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6128 SValue sv;
6129 t = get_reg(RC_INT);
6130 sv.type.t = VT_INT;
6131 sv.r = VT_LOCAL | VT_LVAL;
6132 sv.c.ul = vtop[-1].c.ul;
6133 load(t, &sv);
6134 vtop[-1].r = t | VT_LVAL;
6136 store(r, vtop - 1);
6137 /* two word case handling : store second register at word + 4 */
6138 if ((ft & VT_BTYPE) == VT_LLONG) {
6139 vswap();
6140 /* convert to int to increment easily */
6141 vtop->type.t = VT_INT;
6142 gaddrof();
6143 vpushi(4);
6144 gen_op('+');
6145 vtop->r |= VT_LVAL;
6146 vswap();
6147 /* XXX: it works because r2 is spilled last ! */
6148 store(vtop->r2, vtop - 1);
6151 vswap();
6152 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6153 vtop->r |= delayed_cast;
6157 /* post defines POST/PRE add. c is the token ++ or -- */
6158 void inc(int post, int c)
6160 test_lvalue();
6161 vdup(); /* save lvalue */
6162 if (post) {
6163 gv_dup(); /* duplicate value */
6164 vrotb(3);
6165 vrotb(3);
6167 /* add constant */
6168 vpushi(c - TOK_MID);
6169 gen_op('+');
6170 vstore(); /* store value */
6171 if (post)
6172 vpop(); /* if post op, return saved value */
6175 /* Parse GNUC __attribute__ extension. Currently, the following
6176 extensions are recognized:
6177 - aligned(n) : set data/function alignment.
6178 - section(x) : generate data/code in this section.
6179 - unused : currently ignored, but may be used someday.
6181 static void parse_attribute(AttributeDef *ad)
6183 int t, n;
6185 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6186 next();
6187 skip('(');
6188 skip('(');
6189 while (tok != ')') {
6190 if (tok < TOK_IDENT)
6191 expect("attribute name");
6192 t = tok;
6193 next();
6194 switch(t) {
6195 case TOK_SECTION1:
6196 case TOK_SECTION2:
6197 skip('(');
6198 if (tok != TOK_STR)
6199 expect("section name");
6200 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6201 next();
6202 skip(')');
6203 break;
6204 case TOK_ALIGNED1:
6205 case TOK_ALIGNED2:
6206 if (tok == '(') {
6207 next();
6208 n = expr_const();
6209 if (n <= 0 || (n & (n - 1)) != 0)
6210 error("alignment must be a positive power of two");
6211 skip(')');
6212 } else {
6213 n = MAX_ALIGN;
6215 ad->aligned = n;
6216 break;
6217 case TOK_UNUSED1:
6218 case TOK_UNUSED2:
6219 /* currently, no need to handle it because tcc does not
6220 track unused objects */
6221 break;
6222 case TOK_NORETURN1:
6223 case TOK_NORETURN2:
6224 /* currently, no need to handle it because tcc does not
6225 track unused objects */
6226 break;
6227 case TOK_CDECL1:
6228 case TOK_CDECL2:
6229 case TOK_CDECL3:
6230 ad->func_call = FUNC_CDECL;
6231 break;
6232 case TOK_STDCALL1:
6233 case TOK_STDCALL2:
6234 case TOK_STDCALL3:
6235 ad->func_call = FUNC_STDCALL;
6236 break;
6237 #ifdef TCC_TARGET_I386
6238 case TOK_REGPARM1:
6239 case TOK_REGPARM2:
6240 skip('(');
6241 n = expr_const();
6242 if (n > 3)
6243 n = 3;
6244 else if (n < 0)
6245 n = 0;
6246 if (n > 0)
6247 ad->func_call = FUNC_FASTCALL1 + n - 1;
6248 skip(')');
6249 break;
6250 #endif
6251 default:
6252 if (tcc_state->warn_unsupported)
6253 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6254 /* skip parameters */
6255 /* XXX: skip parenthesis too */
6256 if (tok == '(') {
6257 next();
6258 while (tok != ')' && tok != -1)
6259 next();
6260 next();
6262 break;
6264 if (tok != ',')
6265 break;
6266 next();
6268 skip(')');
6269 skip(')');
6273 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6274 static void struct_decl(CType *type, int u)
6276 int a, v, size, align, maxalign, c, offset;
6277 int bit_size, bit_pos, bsize, bt, lbit_pos;
6278 Sym *s, *ss, **ps;
6279 AttributeDef ad;
6280 CType type1, btype;
6282 a = tok; /* save decl type */
6283 next();
6284 if (tok != '{') {
6285 v = tok;
6286 next();
6287 /* struct already defined ? return it */
6288 if (v < TOK_IDENT)
6289 expect("struct/union/enum name");
6290 s = struct_find(v);
6291 if (s) {
6292 if (s->type.t != a)
6293 error("invalid type");
6294 goto do_decl;
6296 } else {
6297 v = anon_sym++;
6299 type1.t = a;
6300 /* we put an undefined size for struct/union */
6301 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6302 s->r = 0; /* default alignment is zero as gcc */
6303 /* put struct/union/enum name in type */
6304 do_decl:
6305 type->t = u;
6306 type->ref = s;
6308 if (tok == '{') {
6309 next();
6310 if (s->c != -1)
6311 error("struct/union/enum already defined");
6312 /* cannot be empty */
6313 c = 0;
6314 /* non empty enums are not allowed */
6315 if (a == TOK_ENUM) {
6316 for(;;) {
6317 v = tok;
6318 if (v < TOK_UIDENT)
6319 expect("identifier");
6320 next();
6321 if (tok == '=') {
6322 next();
6323 c = expr_const();
6325 /* enum symbols have static storage */
6326 ss = sym_push(v, &int_type, VT_CONST, c);
6327 ss->type.t |= VT_STATIC;
6328 if (tok != ',')
6329 break;
6330 next();
6331 c++;
6332 /* NOTE: we accept a trailing comma */
6333 if (tok == '}')
6334 break;
6336 skip('}');
6337 } else {
6338 maxalign = 1;
6339 ps = &s->next;
6340 bit_pos = 0;
6341 offset = 0;
6342 while (tok != '}') {
6343 parse_btype(&btype, &ad);
6344 while (1) {
6345 bit_size = -1;
6346 v = 0;
6347 type1 = btype;
6348 if (tok != ':') {
6349 type_decl(&type1, &ad, &v, TYPE_DIRECT);
6350 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6351 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6352 error("invalid type for '%s'",
6353 get_tok_str(v, NULL));
6355 if (tok == ':') {
6356 next();
6357 bit_size = expr_const();
6358 /* XXX: handle v = 0 case for messages */
6359 if (bit_size < 0)
6360 error("negative width in bit-field '%s'",
6361 get_tok_str(v, NULL));
6362 if (v && bit_size == 0)
6363 error("zero width for bit-field '%s'",
6364 get_tok_str(v, NULL));
6366 size = type_size(&type1, &align);
6367 if (align < ad.aligned)
6368 align = ad.aligned;
6369 lbit_pos = 0;
6370 if (bit_size >= 0) {
6371 bt = type1.t & VT_BTYPE;
6372 if (bt != VT_INT &&
6373 bt != VT_BYTE &&
6374 bt != VT_SHORT &&
6375 bt != VT_BOOL &&
6376 bt != VT_ENUM)
6377 error("bitfields must have scalar type");
6378 bsize = size * 8;
6379 if (bit_size > bsize) {
6380 error("width of '%s' exceeds its type",
6381 get_tok_str(v, NULL));
6382 } else if (bit_size == bsize) {
6383 /* no need for bit fields */
6384 bit_pos = 0;
6385 } else if (bit_size == 0) {
6386 /* XXX: what to do if only padding in a
6387 structure ? */
6388 /* zero size: means to pad */
6389 if (bit_pos > 0)
6390 bit_pos = bsize;
6391 } else {
6392 /* we do not have enough room ? */
6393 if ((bit_pos + bit_size) > bsize)
6394 bit_pos = 0;
6395 lbit_pos = bit_pos;
6396 /* XXX: handle LSB first */
6397 type1.t |= VT_BITFIELD |
6398 (bit_pos << VT_STRUCT_SHIFT) |
6399 (bit_size << (VT_STRUCT_SHIFT + 6));
6400 bit_pos += bit_size;
6402 } else {
6403 bit_pos = 0;
6405 if (v) {
6406 /* add new memory data only if starting
6407 bit field */
6408 if (lbit_pos == 0) {
6409 if (a == TOK_STRUCT) {
6410 c = (c + align - 1) & -align;
6411 offset = c;
6412 c += size;
6413 } else {
6414 offset = 0;
6415 if (size > c)
6416 c = size;
6418 if (align > maxalign)
6419 maxalign = align;
6421 #if 0
6422 printf("add field %s offset=%d",
6423 get_tok_str(v, NULL), offset);
6424 if (type1.t & VT_BITFIELD) {
6425 printf(" pos=%d size=%d",
6426 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6427 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6429 printf("\n");
6430 #endif
6431 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6432 *ps = ss;
6433 ps = &ss->next;
6435 if (tok == ';' || tok == TOK_EOF)
6436 break;
6437 skip(',');
6439 skip(';');
6441 skip('}');
6442 /* store size and alignment */
6443 s->c = (c + maxalign - 1) & -maxalign;
6444 s->r = maxalign;
6449 /* return 0 if no type declaration. otherwise, return the basic type
6450 and skip it.
6452 static int parse_btype(CType *type, AttributeDef *ad)
6454 int t, u, type_found, typespec_found;
6455 Sym *s;
6456 CType type1;
6458 memset(ad, 0, sizeof(AttributeDef));
6459 type_found = 0;
6460 typespec_found = 0;
6461 t = 0;
6462 while(1) {
6463 switch(tok) {
6464 case TOK_EXTENSION:
6465 /* currently, we really ignore extension */
6466 next();
6467 continue;
6469 /* basic types */
6470 case TOK_CHAR:
6471 u = VT_BYTE;
6472 basic_type:
6473 next();
6474 basic_type1:
6475 if ((t & VT_BTYPE) != 0)
6476 error("too many basic types");
6477 t |= u;
6478 typespec_found = 1;
6479 break;
6480 case TOK_VOID:
6481 u = VT_VOID;
6482 goto basic_type;
6483 case TOK_SHORT:
6484 u = VT_SHORT;
6485 goto basic_type;
6486 case TOK_INT:
6487 next();
6488 typespec_found = 1;
6489 break;
6490 case TOK_LONG:
6491 next();
6492 if ((t & VT_BTYPE) == VT_DOUBLE) {
6493 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6494 } else if ((t & VT_BTYPE) == VT_LONG) {
6495 t = (t & ~VT_BTYPE) | VT_LLONG;
6496 } else {
6497 u = VT_LONG;
6498 goto basic_type1;
6500 break;
6501 case TOK_BOOL:
6502 u = VT_BOOL;
6503 goto basic_type;
6504 case TOK_FLOAT:
6505 u = VT_FLOAT;
6506 goto basic_type;
6507 case TOK_DOUBLE:
6508 next();
6509 if ((t & VT_BTYPE) == VT_LONG) {
6510 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6511 } else {
6512 u = VT_DOUBLE;
6513 goto basic_type1;
6515 break;
6516 case TOK_ENUM:
6517 struct_decl(&type1, VT_ENUM);
6518 basic_type2:
6519 u = type1.t;
6520 type->ref = type1.ref;
6521 goto basic_type1;
6522 case TOK_STRUCT:
6523 case TOK_UNION:
6524 struct_decl(&type1, VT_STRUCT);
6525 goto basic_type2;
6527 /* type modifiers */
6528 case TOK_CONST1:
6529 case TOK_CONST2:
6530 case TOK_CONST3:
6531 t |= VT_CONSTANT;
6532 next();
6533 break;
6534 case TOK_VOLATILE1:
6535 case TOK_VOLATILE2:
6536 case TOK_VOLATILE3:
6537 t |= VT_VOLATILE;
6538 next();
6539 break;
6540 case TOK_SIGNED1:
6541 case TOK_SIGNED2:
6542 case TOK_SIGNED3:
6543 typespec_found = 1;
6544 t |= VT_SIGNED;
6545 next();
6546 break;
6547 case TOK_REGISTER:
6548 case TOK_AUTO:
6549 case TOK_RESTRICT1:
6550 case TOK_RESTRICT2:
6551 case TOK_RESTRICT3:
6552 next();
6553 break;
6554 case TOK_UNSIGNED:
6555 t |= VT_UNSIGNED;
6556 next();
6557 typespec_found = 1;
6558 break;
6560 /* storage */
6561 case TOK_EXTERN:
6562 t |= VT_EXTERN;
6563 next();
6564 break;
6565 case TOK_STATIC:
6566 t |= VT_STATIC;
6567 next();
6568 break;
6569 case TOK_TYPEDEF:
6570 t |= VT_TYPEDEF;
6571 next();
6572 break;
6573 case TOK_INLINE1:
6574 case TOK_INLINE2:
6575 case TOK_INLINE3:
6576 t |= VT_INLINE;
6577 next();
6578 break;
6580 /* GNUC attribute */
6581 case TOK_ATTRIBUTE1:
6582 case TOK_ATTRIBUTE2:
6583 parse_attribute(ad);
6584 break;
6585 /* GNUC typeof */
6586 case TOK_TYPEOF1:
6587 case TOK_TYPEOF2:
6588 case TOK_TYPEOF3:
6589 next();
6590 parse_expr_type(&type1);
6591 goto basic_type2;
6592 default:
6593 if (typespec_found)
6594 goto the_end;
6595 s = sym_find(tok);
6596 if (!s || !(s->type.t & VT_TYPEDEF))
6597 goto the_end;
6598 t |= (s->type.t & ~VT_TYPEDEF);
6599 type->ref = s->type.ref;
6600 next();
6601 break;
6603 type_found = 1;
6605 the_end:
6606 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
6607 error("signed and unsigned modifier");
6608 if (tcc_state->char_is_unsigned) {
6609 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
6610 t |= VT_UNSIGNED;
6612 t &= ~VT_SIGNED;
6614 /* long is never used as type */
6615 if ((t & VT_BTYPE) == VT_LONG)
6616 t = (t & ~VT_BTYPE) | VT_INT;
6617 type->t = t;
6618 return type_found;
6621 /* convert a function parameter type (array to pointer and function to
6622 function pointer) */
6623 static inline void convert_parameter_type(CType *pt)
6625 /* remove const and volatile qualifiers (XXX: const could be used
6626 to indicate a const function parameter */
6627 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
6628 /* array must be transformed to pointer according to ANSI C */
6629 pt->t &= ~VT_ARRAY;
6630 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6631 mk_pointer(pt);
6635 static void post_type(CType *type, AttributeDef *ad)
6637 int n, l, t1;
6638 Sym **plast, *s, *first;
6639 AttributeDef ad1;
6640 CType pt;
6642 if (tok == '(') {
6643 /* function declaration */
6644 next();
6645 l = 0;
6646 first = NULL;
6647 plast = &first;
6648 while (tok != ')') {
6649 /* read param name and compute offset */
6650 if (l != FUNC_OLD) {
6651 if (!parse_btype(&pt, &ad1)) {
6652 if (l) {
6653 error("invalid type");
6654 } else {
6655 l = FUNC_OLD;
6656 goto old_proto;
6659 l = FUNC_NEW;
6660 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6661 break;
6662 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6663 if ((pt.t & VT_BTYPE) == VT_VOID)
6664 error("parameter declared as void");
6665 } else {
6666 old_proto:
6667 n = tok;
6668 pt.t = VT_INT;
6669 next();
6671 convert_parameter_type(&pt);
6672 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6673 *plast = s;
6674 plast = &s->next;
6675 if (tok == ',') {
6676 next();
6677 if (l == FUNC_NEW && tok == TOK_DOTS) {
6678 l = FUNC_ELLIPSIS;
6679 next();
6680 break;
6684 /* if no parameters, then old type prototype */
6685 if (l == 0)
6686 l = FUNC_OLD;
6687 skip(')');
6688 t1 = type->t & VT_STORAGE;
6689 /* NOTE: const is ignored in returned type as it has a special
6690 meaning in gcc / C++ */
6691 type->t &= ~(VT_STORAGE | VT_CONSTANT);
6692 post_type(type, ad);
6693 /* we push a anonymous symbol which will contain the function prototype */
6694 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6695 s->next = first;
6696 type->t = t1 | VT_FUNC;
6697 type->ref = s;
6698 } else if (tok == '[') {
6699 /* array definition */
6700 next();
6701 n = -1;
6702 if (tok != ']') {
6703 n = expr_const();
6704 if (n < 0)
6705 error("invalid array size");
6707 skip(']');
6708 /* parse next post type */
6709 t1 = type->t & VT_STORAGE;
6710 type->t &= ~VT_STORAGE;
6711 post_type(type, ad);
6713 /* we push a anonymous symbol which will contain the array
6714 element type */
6715 s = sym_push(SYM_FIELD, type, 0, n);
6716 type->t = t1 | VT_ARRAY | VT_PTR;
6717 type->ref = s;
6721 /* Parse a type declaration (except basic type), and return the type
6722 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6723 expected. 'type' should contain the basic type. 'ad' is the
6724 attribute definition of the basic type. It can be modified by
6725 type_decl().
6727 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6729 Sym *s;
6730 CType type1, *type2;
6731 int qualifiers;
6733 while (tok == '*') {
6734 qualifiers = 0;
6735 redo:
6736 next();
6737 switch(tok) {
6738 case TOK_CONST1:
6739 case TOK_CONST2:
6740 case TOK_CONST3:
6741 qualifiers |= VT_CONSTANT;
6742 goto redo;
6743 case TOK_VOLATILE1:
6744 case TOK_VOLATILE2:
6745 case TOK_VOLATILE3:
6746 qualifiers |= VT_VOLATILE;
6747 goto redo;
6748 case TOK_RESTRICT1:
6749 case TOK_RESTRICT2:
6750 case TOK_RESTRICT3:
6751 goto redo;
6753 mk_pointer(type);
6754 type->t |= qualifiers;
6757 /* XXX: clarify attribute handling */
6758 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6759 parse_attribute(ad);
6761 /* recursive type */
6762 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6763 type1.t = 0; /* XXX: same as int */
6764 if (tok == '(') {
6765 next();
6766 /* XXX: this is not correct to modify 'ad' at this point, but
6767 the syntax is not clear */
6768 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6769 parse_attribute(ad);
6770 type_decl(&type1, ad, v, td);
6771 skip(')');
6772 } else {
6773 /* type identifier */
6774 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6775 *v = tok;
6776 next();
6777 } else {
6778 if (!(td & TYPE_ABSTRACT))
6779 expect("identifier");
6780 *v = 0;
6783 post_type(type, ad);
6784 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6785 parse_attribute(ad);
6786 if (!type1.t)
6787 return;
6788 /* append type at the end of type1 */
6789 type2 = &type1;
6790 for(;;) {
6791 s = type2->ref;
6792 type2 = &s->type;
6793 if (!type2->t) {
6794 *type2 = *type;
6795 break;
6798 *type = type1;
6801 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6802 static int lvalue_type(int t)
6804 int bt, r;
6805 r = VT_LVAL;
6806 bt = t & VT_BTYPE;
6807 if (bt == VT_BYTE || bt == VT_BOOL)
6808 r |= VT_LVAL_BYTE;
6809 else if (bt == VT_SHORT)
6810 r |= VT_LVAL_SHORT;
6811 else
6812 return r;
6813 if (t & VT_UNSIGNED)
6814 r |= VT_LVAL_UNSIGNED;
6815 return r;
6818 /* indirection with full error checking and bound check */
6819 static void indir(void)
6821 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6822 expect("pointer");
6823 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6824 gv(RC_INT);
6825 vtop->type = *pointed_type(&vtop->type);
6826 /* an array is never an lvalue */
6827 if (!(vtop->type.t & VT_ARRAY)) {
6828 vtop->r |= lvalue_type(vtop->type.t);
6829 /* if bound checking, the referenced pointer must be checked */
6830 if (do_bounds_check)
6831 vtop->r |= VT_MUSTBOUND;
6835 /* pass a parameter to a function and do type checking and casting */
6836 static void gfunc_param_typed(Sym *func, Sym *arg)
6838 int func_type;
6839 CType type;
6841 func_type = func->c;
6842 if (func_type == FUNC_OLD ||
6843 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6844 /* default casting : only need to convert float to double */
6845 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6846 type.t = VT_DOUBLE;
6847 gen_cast(&type);
6849 } else if (arg == NULL) {
6850 error("too many arguments to function");
6851 } else {
6852 type = arg->type;
6853 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
6854 gen_assign_cast(&type);
6858 /* parse an expression of the form '(type)' or '(expr)' and return its
6859 type */
6860 static void parse_expr_type(CType *type)
6862 int n;
6863 AttributeDef ad;
6865 skip('(');
6866 if (parse_btype(type, &ad)) {
6867 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6868 } else {
6869 expr_type(type);
6871 skip(')');
6874 static void parse_type(CType *type)
6876 AttributeDef ad;
6877 int n;
6879 if (!parse_btype(type, &ad)) {
6880 expect("type");
6882 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6885 static void vpush_tokc(int t)
6887 CType type;
6888 type.t = t;
6889 vsetc(&type, VT_CONST, &tokc);
6892 static void unary(void)
6894 int n, t, align, size, r;
6895 CType type;
6896 Sym *s;
6897 AttributeDef ad;
6899 /* XXX: GCC 2.95.3 does not generate a table although it should be
6900 better here */
6901 tok_next:
6902 switch(tok) {
6903 case TOK_EXTENSION:
6904 next();
6905 goto tok_next;
6906 case TOK_CINT:
6907 case TOK_CCHAR:
6908 case TOK_LCHAR:
6909 vpushi(tokc.i);
6910 next();
6911 break;
6912 case TOK_CUINT:
6913 vpush_tokc(VT_INT | VT_UNSIGNED);
6914 next();
6915 break;
6916 case TOK_CLLONG:
6917 vpush_tokc(VT_LLONG);
6918 next();
6919 break;
6920 case TOK_CULLONG:
6921 vpush_tokc(VT_LLONG | VT_UNSIGNED);
6922 next();
6923 break;
6924 case TOK_CFLOAT:
6925 vpush_tokc(VT_FLOAT);
6926 next();
6927 break;
6928 case TOK_CDOUBLE:
6929 vpush_tokc(VT_DOUBLE);
6930 next();
6931 break;
6932 case TOK_CLDOUBLE:
6933 vpush_tokc(VT_LDOUBLE);
6934 next();
6935 break;
6936 case TOK___FUNCTION__:
6937 if (!gnu_ext)
6938 goto tok_identifier;
6939 /* fall thru */
6940 case TOK___FUNC__:
6942 void *ptr;
6943 int len;
6944 /* special function name identifier */
6945 len = strlen(funcname) + 1;
6946 /* generate char[len] type */
6947 type.t = VT_BYTE;
6948 mk_pointer(&type);
6949 type.t |= VT_ARRAY;
6950 type.ref->c = len;
6951 vpush_ref(&type, data_section, data_section->data_offset, len);
6952 ptr = section_ptr_add(data_section, len);
6953 memcpy(ptr, funcname, len);
6954 next();
6956 break;
6957 case TOK_LSTR:
6958 t = VT_INT;
6959 goto str_init;
6960 case TOK_STR:
6961 /* string parsing */
6962 t = VT_BYTE;
6963 str_init:
6964 if (tcc_state->warn_write_strings)
6965 t |= VT_CONSTANT;
6966 type.t = t;
6967 mk_pointer(&type);
6968 type.t |= VT_ARRAY;
6969 memset(&ad, 0, sizeof(AttributeDef));
6970 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
6971 break;
6972 case '(':
6973 next();
6974 /* cast ? */
6975 if (parse_btype(&type, &ad)) {
6976 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
6977 skip(')');
6978 /* check ISOC99 compound literal */
6979 if (tok == '{') {
6980 /* data is allocated locally by default */
6981 if (global_expr)
6982 r = VT_CONST;
6983 else
6984 r = VT_LOCAL;
6985 /* all except arrays are lvalues */
6986 if (!(type.t & VT_ARRAY))
6987 r |= lvalue_type(type.t);
6988 memset(&ad, 0, sizeof(AttributeDef));
6989 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
6990 } else {
6991 unary();
6992 gen_cast(&type);
6994 } else if (tok == '{') {
6995 /* save all registers */
6996 save_regs(0);
6997 /* statement expression : we do not accept break/continue
6998 inside as GCC does */
6999 block(NULL, NULL, NULL, NULL, 0, 1);
7000 skip(')');
7001 } else {
7002 gexpr();
7003 skip(')');
7005 break;
7006 case '*':
7007 next();
7008 unary();
7009 indir();
7010 break;
7011 case '&':
7012 next();
7013 unary();
7014 /* functions names must be treated as function pointers,
7015 except for unary '&' and sizeof. Since we consider that
7016 functions are not lvalues, we only have to handle it
7017 there and in function calls. */
7018 /* arrays can also be used although they are not lvalues */
7019 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7020 !(vtop->type.t & VT_ARRAY))
7021 test_lvalue();
7022 mk_pointer(&vtop->type);
7023 gaddrof();
7024 break;
7025 case '!':
7026 next();
7027 unary();
7028 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
7029 vtop->c.i = !vtop->c.i;
7030 else if ((vtop->r & VT_VALMASK) == VT_CMP)
7031 vtop->c.i = vtop->c.i ^ 1;
7032 else
7033 vseti(VT_JMP, gtst(1, 0));
7034 break;
7035 case '~':
7036 next();
7037 unary();
7038 vpushi(-1);
7039 gen_op('^');
7040 break;
7041 case '+':
7042 next();
7043 /* in order to force cast, we add zero */
7044 unary();
7045 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7046 error("pointer not accepted for unary plus");
7047 vpushi(0);
7048 gen_op('+');
7049 break;
7050 case TOK_SIZEOF:
7051 case TOK_ALIGNOF1:
7052 case TOK_ALIGNOF2:
7053 t = tok;
7054 next();
7055 if (tok == '(') {
7056 parse_expr_type(&type);
7057 } else {
7058 unary_type(&type);
7060 size = type_size(&type, &align);
7061 if (t == TOK_SIZEOF) {
7062 if (size < 0)
7063 error("sizeof applied to an incomplete type");
7064 vpushi(size);
7065 } else {
7066 vpushi(align);
7068 break;
7070 case TOK_builtin_types_compatible_p:
7072 CType type1, type2;
7073 next();
7074 skip('(');
7075 parse_type(&type1);
7076 skip(',');
7077 parse_type(&type2);
7078 skip(')');
7079 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7080 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7081 vpushi(is_compatible_types(&type1, &type2));
7083 break;
7084 case TOK_builtin_constant_p:
7086 int saved_nocode_wanted, res;
7087 next();
7088 skip('(');
7089 saved_nocode_wanted = nocode_wanted;
7090 nocode_wanted = 1;
7091 gexpr();
7092 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7093 vpop();
7094 nocode_wanted = saved_nocode_wanted;
7095 skip(')');
7096 vpushi(res);
7098 break;
7099 case TOK_INC:
7100 case TOK_DEC:
7101 t = tok;
7102 next();
7103 unary();
7104 inc(0, t);
7105 break;
7106 case '-':
7107 next();
7108 vpushi(0);
7109 unary();
7110 gen_op('-');
7111 break;
7112 case TOK_LAND:
7113 if (!gnu_ext)
7114 goto tok_identifier;
7115 next();
7116 /* allow to take the address of a label */
7117 if (tok < TOK_UIDENT)
7118 expect("label identifier");
7119 s = label_find(tok);
7120 if (!s) {
7121 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7122 } else {
7123 if (s->r == LABEL_DECLARED)
7124 s->r = LABEL_FORWARD;
7126 if (!s->type.t) {
7127 s->type.t = VT_VOID;
7128 mk_pointer(&s->type);
7129 s->type.t |= VT_STATIC;
7131 vset(&s->type, VT_CONST | VT_SYM, 0);
7132 vtop->sym = s;
7133 next();
7134 break;
7135 default:
7136 tok_identifier:
7137 t = tok;
7138 next();
7139 if (t < TOK_UIDENT)
7140 expect("identifier");
7141 s = sym_find(t);
7142 if (!s) {
7143 if (tok != '(')
7144 error("'%s' undeclared", get_tok_str(t, NULL));
7145 /* for simple function calls, we tolerate undeclared
7146 external reference to int() function */
7147 if (tcc_state->warn_implicit_function_declaration)
7148 warning("implicit declaration of function '%s'",
7149 get_tok_str(t, NULL));
7150 s = external_global_sym(t, &func_old_type, 0);
7152 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7153 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7154 /* if referencing an inline function, then we generate a
7155 symbol to it if not already done. It will have the
7156 effect to generate code for it at the end of the
7157 compilation unit. Inline function as always
7158 generated in the text section. */
7159 if (!s->c)
7160 put_extern_sym(s, text_section, 0, 0);
7161 r = VT_SYM | VT_CONST;
7162 } else {
7163 r = s->r;
7165 vset(&s->type, r, s->c);
7166 /* if forward reference, we must point to s */
7167 if (vtop->r & VT_SYM) {
7168 vtop->sym = s;
7169 vtop->c.ul = 0;
7171 break;
7174 /* post operations */
7175 while (1) {
7176 if (tok == TOK_INC || tok == TOK_DEC) {
7177 inc(1, tok);
7178 next();
7179 } else if (tok == '.' || tok == TOK_ARROW) {
7180 /* field */
7181 if (tok == TOK_ARROW)
7182 indir();
7183 test_lvalue();
7184 gaddrof();
7185 next();
7186 /* expect pointer on structure */
7187 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7188 expect("struct or union");
7189 s = vtop->type.ref;
7190 /* find field */
7191 tok |= SYM_FIELD;
7192 while ((s = s->next) != NULL) {
7193 if (s->v == tok)
7194 break;
7196 if (!s)
7197 error("field not found");
7198 /* add field offset to pointer */
7199 vtop->type = char_pointer_type; /* change type to 'char *' */
7200 vpushi(s->c);
7201 gen_op('+');
7202 /* change type to field type, and set to lvalue */
7203 vtop->type = s->type;
7204 /* an array is never an lvalue */
7205 if (!(vtop->type.t & VT_ARRAY)) {
7206 vtop->r |= lvalue_type(vtop->type.t);
7207 /* if bound checking, the referenced pointer must be checked */
7208 if (do_bounds_check)
7209 vtop->r |= VT_MUSTBOUND;
7211 next();
7212 } else if (tok == '[') {
7213 next();
7214 gexpr();
7215 gen_op('+');
7216 indir();
7217 skip(']');
7218 } else if (tok == '(') {
7219 SValue ret;
7220 Sym *sa;
7221 int nb_args;
7223 /* function call */
7224 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7225 /* pointer test (no array accepted) */
7226 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7227 vtop->type = *pointed_type(&vtop->type);
7228 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7229 goto error_func;
7230 } else {
7231 error_func:
7232 expect("function pointer");
7234 } else {
7235 vtop->r &= ~VT_LVAL; /* no lvalue */
7237 /* get return type */
7238 s = vtop->type.ref;
7239 next();
7240 sa = s->next; /* first parameter */
7241 nb_args = 0;
7242 /* compute first implicit argument if a structure is returned */
7243 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7244 /* get some space for the returned structure */
7245 size = type_size(&s->type, &align);
7246 loc = (loc - size) & -align;
7247 ret.type = s->type;
7248 ret.r = VT_LOCAL | VT_LVAL;
7249 /* pass it as 'int' to avoid structure arg passing
7250 problems */
7251 vseti(VT_LOCAL, loc);
7252 ret.c = vtop->c;
7253 nb_args++;
7254 } else {
7255 ret.type = s->type;
7256 ret.r2 = VT_CONST;
7257 /* return in register */
7258 if (is_float(ret.type.t)) {
7259 ret.r = REG_FRET;
7260 } else {
7261 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7262 ret.r2 = REG_LRET;
7263 ret.r = REG_IRET;
7265 ret.c.i = 0;
7267 if (tok != ')') {
7268 for(;;) {
7269 expr_eq();
7270 gfunc_param_typed(s, sa);
7271 nb_args++;
7272 if (sa)
7273 sa = sa->next;
7274 if (tok == ')')
7275 break;
7276 skip(',');
7279 if (sa)
7280 error("too few arguments to function");
7281 skip(')');
7282 if (!nocode_wanted) {
7283 gfunc_call(nb_args);
7284 } else {
7285 vtop -= (nb_args + 1);
7287 /* return value */
7288 vsetc(&ret.type, ret.r, &ret.c);
7289 vtop->r2 = ret.r2;
7290 } else {
7291 break;
7296 static void uneq(void)
7298 int t;
7300 unary();
7301 if (tok == '=' ||
7302 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7303 tok == TOK_A_XOR || tok == TOK_A_OR ||
7304 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7305 test_lvalue();
7306 t = tok;
7307 next();
7308 if (t == '=') {
7309 expr_eq();
7310 } else {
7311 vdup();
7312 expr_eq();
7313 gen_op(t & 0x7f);
7315 vstore();
7319 static void expr_prod(void)
7321 int t;
7323 uneq();
7324 while (tok == '*' || tok == '/' || tok == '%') {
7325 t = tok;
7326 next();
7327 uneq();
7328 gen_op(t);
7332 static void expr_sum(void)
7334 int t;
7336 expr_prod();
7337 while (tok == '+' || tok == '-') {
7338 t = tok;
7339 next();
7340 expr_prod();
7341 gen_op(t);
7345 static void expr_shift(void)
7347 int t;
7349 expr_sum();
7350 while (tok == TOK_SHL || tok == TOK_SAR) {
7351 t = tok;
7352 next();
7353 expr_sum();
7354 gen_op(t);
7358 static void expr_cmp(void)
7360 int t;
7362 expr_shift();
7363 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7364 tok == TOK_ULT || tok == TOK_UGE) {
7365 t = tok;
7366 next();
7367 expr_shift();
7368 gen_op(t);
7372 static void expr_cmpeq(void)
7374 int t;
7376 expr_cmp();
7377 while (tok == TOK_EQ || tok == TOK_NE) {
7378 t = tok;
7379 next();
7380 expr_cmp();
7381 gen_op(t);
7385 static void expr_and(void)
7387 expr_cmpeq();
7388 while (tok == '&') {
7389 next();
7390 expr_cmpeq();
7391 gen_op('&');
7395 static void expr_xor(void)
7397 expr_and();
7398 while (tok == '^') {
7399 next();
7400 expr_and();
7401 gen_op('^');
7405 static void expr_or(void)
7407 expr_xor();
7408 while (tok == '|') {
7409 next();
7410 expr_xor();
7411 gen_op('|');
7415 /* XXX: fix this mess */
7416 static void expr_land_const(void)
7418 expr_or();
7419 while (tok == TOK_LAND) {
7420 next();
7421 expr_or();
7422 gen_op(TOK_LAND);
7426 /* XXX: fix this mess */
7427 static void expr_lor_const(void)
7429 expr_land_const();
7430 while (tok == TOK_LOR) {
7431 next();
7432 expr_land_const();
7433 gen_op(TOK_LOR);
7437 /* only used if non constant */
7438 static void expr_land(void)
7440 int t;
7442 expr_or();
7443 if (tok == TOK_LAND) {
7444 t = 0;
7445 for(;;) {
7446 t = gtst(1, t);
7447 if (tok != TOK_LAND) {
7448 vseti(VT_JMPI, t);
7449 break;
7451 next();
7452 expr_or();
7457 static void expr_lor(void)
7459 int t;
7461 expr_land();
7462 if (tok == TOK_LOR) {
7463 t = 0;
7464 for(;;) {
7465 t = gtst(0, t);
7466 if (tok != TOK_LOR) {
7467 vseti(VT_JMP, t);
7468 break;
7470 next();
7471 expr_land();
7476 /* XXX: better constant handling */
7477 static void expr_eq(void)
7479 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7480 SValue sv;
7481 CType type, type1, type2;
7483 if (const_wanted) {
7484 int c1, c;
7485 expr_lor_const();
7486 if (tok == '?') {
7487 c = vtop->c.i;
7488 vpop();
7489 next();
7490 if (tok == ':' && gnu_ext) {
7491 c1 = c;
7492 } else {
7493 gexpr();
7494 c1 = vtop->c.i;
7495 vpop();
7497 skip(':');
7498 expr_eq();
7499 if (c)
7500 vtop->c.i = c1;
7502 } else {
7503 expr_lor();
7504 if (tok == '?') {
7505 next();
7506 if (vtop != vstack) {
7507 /* needed to avoid having different registers saved in
7508 each branch */
7509 if (is_float(vtop->type.t))
7510 rc = RC_FLOAT;
7511 else
7512 rc = RC_INT;
7513 gv(rc);
7514 save_regs(1);
7516 if (tok == ':' && gnu_ext) {
7517 gv_dup();
7518 tt = gtst(1, 0);
7519 } else {
7520 tt = gtst(1, 0);
7521 gexpr();
7523 type1 = vtop->type;
7524 sv = *vtop; /* save value to handle it later */
7525 vtop--; /* no vpop so that FP stack is not flushed */
7526 skip(':');
7527 u = gjmp(0);
7528 gsym(tt);
7529 expr_eq();
7530 type2 = vtop->type;
7532 t1 = type1.t;
7533 bt1 = t1 & VT_BTYPE;
7534 t2 = type2.t;
7535 bt2 = t2 & VT_BTYPE;
7536 /* cast operands to correct type according to ISOC rules */
7537 if (is_float(bt1) || is_float(bt2)) {
7538 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7539 type.t = VT_LDOUBLE;
7540 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7541 type.t = VT_DOUBLE;
7542 } else {
7543 type.t = VT_FLOAT;
7545 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7546 /* cast to biggest op */
7547 type.t = VT_LLONG;
7548 /* convert to unsigned if it does not fit in a long long */
7549 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7550 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7551 type.t |= VT_UNSIGNED;
7552 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7553 /* XXX: test pointer compatibility */
7554 type = type1;
7555 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7556 /* XXX: test structure compatibility */
7557 type = type1;
7558 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7559 /* NOTE: as an extension, we accept void on only one side */
7560 type.t = VT_VOID;
7561 } else {
7562 /* integer operations */
7563 type.t = VT_INT;
7564 /* convert to unsigned if it does not fit in an integer */
7565 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7566 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7567 type.t |= VT_UNSIGNED;
7570 /* now we convert second operand */
7571 gen_cast(&type);
7572 rc = RC_INT;
7573 if (is_float(type.t)) {
7574 rc = RC_FLOAT;
7575 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7576 /* for long longs, we use fixed registers to avoid having
7577 to handle a complicated move */
7578 rc = RC_IRET;
7581 r2 = gv(rc);
7582 /* this is horrible, but we must also convert first
7583 operand */
7584 tt = gjmp(0);
7585 gsym(u);
7586 /* put again first value and cast it */
7587 *vtop = sv;
7588 gen_cast(&type);
7589 r1 = gv(rc);
7590 move_reg(r2, r1);
7591 vtop->r = r2;
7592 gsym(tt);
7597 static void gexpr(void)
7599 while (1) {
7600 expr_eq();
7601 if (tok != ',')
7602 break;
7603 vpop();
7604 next();
7608 /* parse an expression and return its type without any side effect. */
7609 static void expr_type(CType *type)
7611 int saved_nocode_wanted;
7613 saved_nocode_wanted = nocode_wanted;
7614 nocode_wanted = 1;
7615 gexpr();
7616 *type = vtop->type;
7617 vpop();
7618 nocode_wanted = saved_nocode_wanted;
7621 /* parse a unary expression and return its type without any side
7622 effect. */
7623 static void unary_type(CType *type)
7625 int a;
7627 a = nocode_wanted;
7628 nocode_wanted = 1;
7629 unary();
7630 *type = vtop->type;
7631 vpop();
7632 nocode_wanted = a;
7635 /* parse a constant expression and return value in vtop. */
7636 static void expr_const1(void)
7638 int a;
7639 a = const_wanted;
7640 const_wanted = 1;
7641 expr_eq();
7642 const_wanted = a;
7645 /* parse an integer constant and return its value. */
7646 static int expr_const(void)
7648 int c;
7649 expr_const1();
7650 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7651 expect("constant expression");
7652 c = vtop->c.i;
7653 vpop();
7654 return c;
7657 /* return the label token if current token is a label, otherwise
7658 return zero */
7659 static int is_label(void)
7661 int last_tok;
7663 /* fast test first */
7664 if (tok < TOK_UIDENT)
7665 return 0;
7666 /* no need to save tokc because tok is an identifier */
7667 last_tok = tok;
7668 next();
7669 if (tok == ':') {
7670 next();
7671 return last_tok;
7672 } else {
7673 unget_tok(last_tok);
7674 return 0;
7678 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7679 int case_reg, int is_expr)
7681 int a, b, c, d;
7682 Sym *s;
7684 /* generate line number info */
7685 if (do_debug &&
7686 (last_line_num != file->line_num || last_ind != ind)) {
7687 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7688 last_ind = ind;
7689 last_line_num = file->line_num;
7692 if (is_expr) {
7693 /* default return value is (void) */
7694 vpushi(0);
7695 vtop->type.t = VT_VOID;
7698 if (tok == TOK_IF) {
7699 /* if test */
7700 next();
7701 skip('(');
7702 gexpr();
7703 skip(')');
7704 a = gtst(1, 0);
7705 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7706 c = tok;
7707 if (c == TOK_ELSE) {
7708 next();
7709 d = gjmp(0);
7710 gsym(a);
7711 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7712 gsym(d); /* patch else jmp */
7713 } else
7714 gsym(a);
7715 } else if (tok == TOK_WHILE) {
7716 next();
7717 d = ind;
7718 skip('(');
7719 gexpr();
7720 skip(')');
7721 a = gtst(1, 0);
7722 b = 0;
7723 block(&a, &b, case_sym, def_sym, case_reg, 0);
7724 gjmp_addr(d);
7725 gsym(a);
7726 gsym_addr(b, d);
7727 } else if (tok == '{') {
7728 Sym *llabel;
7730 next();
7731 /* record local declaration stack position */
7732 s = local_stack;
7733 llabel = local_label_stack;
7734 /* handle local labels declarations */
7735 if (tok == TOK_LABEL) {
7736 next();
7737 for(;;) {
7738 if (tok < TOK_UIDENT)
7739 expect("label identifier");
7740 label_push(&local_label_stack, tok, LABEL_DECLARED);
7741 next();
7742 if (tok == ',') {
7743 next();
7744 } else {
7745 skip(';');
7746 break;
7750 while (tok != '}') {
7751 decl(VT_LOCAL);
7752 if (tok != '}') {
7753 if (is_expr)
7754 vpop();
7755 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7758 /* pop locally defined labels */
7759 label_pop(&local_label_stack, llabel);
7760 /* pop locally defined symbols */
7761 sym_pop(&local_stack, s);
7762 next();
7763 } else if (tok == TOK_RETURN) {
7764 next();
7765 if (tok != ';') {
7766 gexpr();
7767 gen_assign_cast(&func_vt);
7768 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7769 CType type;
7770 /* if returning structure, must copy it to implicit
7771 first pointer arg location */
7772 type = func_vt;
7773 mk_pointer(&type);
7774 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7775 indir();
7776 vswap();
7777 /* copy structure value to pointer */
7778 vstore();
7779 } else if (is_float(func_vt.t)) {
7780 gv(RC_FRET);
7781 } else {
7782 gv(RC_IRET);
7784 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7786 skip(';');
7787 rsym = gjmp(rsym); /* jmp */
7788 } else if (tok == TOK_BREAK) {
7789 /* compute jump */
7790 if (!bsym)
7791 error("cannot break");
7792 *bsym = gjmp(*bsym);
7793 next();
7794 skip(';');
7795 } else if (tok == TOK_CONTINUE) {
7796 /* compute jump */
7797 if (!csym)
7798 error("cannot continue");
7799 *csym = gjmp(*csym);
7800 next();
7801 skip(';');
7802 } else if (tok == TOK_FOR) {
7803 int e;
7804 next();
7805 skip('(');
7806 if (tok != ';') {
7807 gexpr();
7808 vpop();
7810 skip(';');
7811 d = ind;
7812 c = ind;
7813 a = 0;
7814 b = 0;
7815 if (tok != ';') {
7816 gexpr();
7817 a = gtst(1, 0);
7819 skip(';');
7820 if (tok != ')') {
7821 e = gjmp(0);
7822 c = ind;
7823 gexpr();
7824 vpop();
7825 gjmp_addr(d);
7826 gsym(e);
7828 skip(')');
7829 block(&a, &b, case_sym, def_sym, case_reg, 0);
7830 gjmp_addr(c);
7831 gsym(a);
7832 gsym_addr(b, c);
7833 } else
7834 if (tok == TOK_DO) {
7835 next();
7836 a = 0;
7837 b = 0;
7838 d = ind;
7839 block(&a, &b, case_sym, def_sym, case_reg, 0);
7840 skip(TOK_WHILE);
7841 skip('(');
7842 gsym(b);
7843 gexpr();
7844 c = gtst(0, 0);
7845 gsym_addr(c, d);
7846 skip(')');
7847 gsym(a);
7848 skip(';');
7849 } else
7850 if (tok == TOK_SWITCH) {
7851 next();
7852 skip('(');
7853 gexpr();
7854 /* XXX: other types than integer */
7855 case_reg = gv(RC_INT);
7856 vpop();
7857 skip(')');
7858 a = 0;
7859 b = gjmp(0); /* jump to first case */
7860 c = 0;
7861 block(&a, csym, &b, &c, case_reg, 0);
7862 /* if no default, jmp after switch */
7863 if (c == 0)
7864 c = ind;
7865 /* default label */
7866 gsym_addr(b, c);
7867 /* break label */
7868 gsym(a);
7869 } else
7870 if (tok == TOK_CASE) {
7871 int v1, v2;
7872 if (!case_sym)
7873 expect("switch");
7874 next();
7875 v1 = expr_const();
7876 v2 = v1;
7877 if (gnu_ext && tok == TOK_DOTS) {
7878 next();
7879 v2 = expr_const();
7880 if (v2 < v1)
7881 warning("empty case range");
7883 /* since a case is like a label, we must skip it with a jmp */
7884 b = gjmp(0);
7885 gsym(*case_sym);
7886 vseti(case_reg, 0);
7887 vpushi(v1);
7888 if (v1 == v2) {
7889 gen_op(TOK_EQ);
7890 *case_sym = gtst(1, 0);
7891 } else {
7892 gen_op(TOK_GE);
7893 *case_sym = gtst(1, 0);
7894 vseti(case_reg, 0);
7895 vpushi(v2);
7896 gen_op(TOK_LE);
7897 *case_sym = gtst(1, *case_sym);
7899 gsym(b);
7900 skip(':');
7901 is_expr = 0;
7902 goto block_after_label;
7903 } else
7904 if (tok == TOK_DEFAULT) {
7905 next();
7906 skip(':');
7907 if (!def_sym)
7908 expect("switch");
7909 if (*def_sym)
7910 error("too many 'default'");
7911 *def_sym = ind;
7912 is_expr = 0;
7913 goto block_after_label;
7914 } else
7915 if (tok == TOK_GOTO) {
7916 next();
7917 if (tok == '*' && gnu_ext) {
7918 /* computed goto */
7919 next();
7920 gexpr();
7921 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7922 expect("pointer");
7923 ggoto();
7924 } else if (tok >= TOK_UIDENT) {
7925 s = label_find(tok);
7926 /* put forward definition if needed */
7927 if (!s) {
7928 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7929 } else {
7930 if (s->r == LABEL_DECLARED)
7931 s->r = LABEL_FORWARD;
7933 /* label already defined */
7934 if (s->r & LABEL_FORWARD)
7935 s->next = (void *)gjmp((long)s->next);
7936 else
7937 gjmp_addr((long)s->next);
7938 next();
7939 } else {
7940 expect("label identifier");
7942 skip(';');
7943 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7944 asm_instr();
7945 } else {
7946 b = is_label();
7947 if (b) {
7948 /* label case */
7949 s = label_find(b);
7950 if (s) {
7951 if (s->r == LABEL_DEFINED)
7952 error("duplicate label '%s'", get_tok_str(s->v, NULL));
7953 gsym((long)s->next);
7954 s->r = LABEL_DEFINED;
7955 } else {
7956 s = label_push(&global_label_stack, b, LABEL_DEFINED);
7958 s->next = (void *)ind;
7959 /* we accept this, but it is a mistake */
7960 block_after_label:
7961 if (tok == '}') {
7962 warning("deprecated use of label at end of compound statement");
7963 } else {
7964 if (is_expr)
7965 vpop();
7966 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7968 } else {
7969 /* expression case */
7970 if (tok != ';') {
7971 if (is_expr) {
7972 vpop();
7973 gexpr();
7974 } else {
7975 gexpr();
7976 vpop();
7979 skip(';');
7984 /* t is the array or struct type. c is the array or struct
7985 address. cur_index/cur_field is the pointer to the current
7986 value. 'size_only' is true if only size info is needed (only used
7987 in arrays) */
7988 static void decl_designator(CType *type, Section *sec, unsigned long c,
7989 int *cur_index, Sym **cur_field,
7990 int size_only)
7992 Sym *s, *f;
7993 int notfirst, index, index_last, align, l, nb_elems, elem_size;
7994 CType type1;
7996 notfirst = 0;
7997 elem_size = 0;
7998 nb_elems = 1;
7999 if (gnu_ext && (l = is_label()) != 0)
8000 goto struct_field;
8001 while (tok == '[' || tok == '.') {
8002 if (tok == '[') {
8003 if (!(type->t & VT_ARRAY))
8004 expect("array type");
8005 s = type->ref;
8006 next();
8007 index = expr_const();
8008 if (index < 0 || (s->c >= 0 && index >= s->c))
8009 expect("invalid index");
8010 if (tok == TOK_DOTS && gnu_ext) {
8011 next();
8012 index_last = expr_const();
8013 if (index_last < 0 ||
8014 (s->c >= 0 && index_last >= s->c) ||
8015 index_last < index)
8016 expect("invalid index");
8017 } else {
8018 index_last = index;
8020 skip(']');
8021 if (!notfirst)
8022 *cur_index = index_last;
8023 type = pointed_type(type);
8024 elem_size = type_size(type, &align);
8025 c += index * elem_size;
8026 /* NOTE: we only support ranges for last designator */
8027 nb_elems = index_last - index + 1;
8028 if (nb_elems != 1) {
8029 notfirst = 1;
8030 break;
8032 } else {
8033 next();
8034 l = tok;
8035 next();
8036 struct_field:
8037 if ((type->t & VT_BTYPE) != VT_STRUCT)
8038 expect("struct/union type");
8039 s = type->ref;
8040 l |= SYM_FIELD;
8041 f = s->next;
8042 while (f) {
8043 if (f->v == l)
8044 break;
8045 f = f->next;
8047 if (!f)
8048 expect("field");
8049 if (!notfirst)
8050 *cur_field = f;
8051 /* XXX: fix this mess by using explicit storage field */
8052 type1 = f->type;
8053 type1.t |= (type->t & ~VT_TYPE);
8054 type = &type1;
8055 c += f->c;
8057 notfirst = 1;
8059 if (notfirst) {
8060 if (tok == '=') {
8061 next();
8062 } else {
8063 if (!gnu_ext)
8064 expect("=");
8066 } else {
8067 if (type->t & VT_ARRAY) {
8068 index = *cur_index;
8069 type = pointed_type(type);
8070 c += index * type_size(type, &align);
8071 } else {
8072 f = *cur_field;
8073 if (!f)
8074 error("too many field init");
8075 /* XXX: fix this mess by using explicit storage field */
8076 type1 = f->type;
8077 type1.t |= (type->t & ~VT_TYPE);
8078 type = &type1;
8079 c += f->c;
8082 decl_initializer(type, sec, c, 0, size_only);
8084 /* XXX: make it more general */
8085 if (!size_only && nb_elems > 1) {
8086 unsigned long c_end;
8087 uint8_t *src, *dst;
8088 int i;
8090 if (!sec)
8091 error("range init not supported yet for dynamic storage");
8092 c_end = c + nb_elems * elem_size;
8093 if (c_end > sec->data_allocated)
8094 section_realloc(sec, c_end);
8095 src = sec->data + c;
8096 dst = src;
8097 for(i = 1; i < nb_elems; i++) {
8098 dst += elem_size;
8099 memcpy(dst, src, elem_size);
8104 #define EXPR_VAL 0
8105 #define EXPR_CONST 1
8106 #define EXPR_ANY 2
8108 /* store a value or an expression directly in global data or in local array */
8109 static void init_putv(CType *type, Section *sec, unsigned long c,
8110 int v, int expr_type)
8112 int saved_global_expr, bt, bit_pos, bit_size;
8113 void *ptr;
8114 unsigned long long bit_mask;
8115 CType dtype;
8117 switch(expr_type) {
8118 case EXPR_VAL:
8119 vpushi(v);
8120 break;
8121 case EXPR_CONST:
8122 /* compound literals must be allocated globally in this case */
8123 saved_global_expr = global_expr;
8124 global_expr = 1;
8125 expr_const1();
8126 global_expr = saved_global_expr;
8127 /* NOTE: symbols are accepted */
8128 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8129 error("initializer element is not constant");
8130 break;
8131 case EXPR_ANY:
8132 expr_eq();
8133 break;
8136 dtype = *type;
8137 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8139 if (sec) {
8140 /* XXX: not portable */
8141 /* XXX: generate error if incorrect relocation */
8142 gen_assign_cast(&dtype);
8143 bt = type->t & VT_BTYPE;
8144 ptr = sec->data + c;
8145 /* XXX: make code faster ? */
8146 if (!(type->t & VT_BITFIELD)) {
8147 bit_pos = 0;
8148 bit_size = 32;
8149 bit_mask = -1LL;
8150 } else {
8151 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8152 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8153 bit_mask = (1LL << bit_size) - 1;
8155 if ((vtop->r & VT_SYM) &&
8156 (bt == VT_BYTE ||
8157 bt == VT_SHORT ||
8158 bt == VT_DOUBLE ||
8159 bt == VT_LDOUBLE ||
8160 bt == VT_LLONG ||
8161 (bt == VT_INT && bit_size != 32)))
8162 error("initializer element is not computable at load time");
8163 switch(bt) {
8164 case VT_BYTE:
8165 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8166 break;
8167 case VT_SHORT:
8168 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8169 break;
8170 case VT_DOUBLE:
8171 *(double *)ptr = vtop->c.d;
8172 break;
8173 case VT_LDOUBLE:
8174 *(long double *)ptr = vtop->c.ld;
8175 break;
8176 case VT_LLONG:
8177 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8178 break;
8179 default:
8180 if (vtop->r & VT_SYM) {
8181 greloc(sec, vtop->sym, c, R_DATA_32);
8183 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8184 break;
8186 vtop--;
8187 } else {
8188 vset(&dtype, VT_LOCAL, c);
8189 vswap();
8190 vstore();
8191 vpop();
8195 /* put zeros for variable based init */
8196 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8198 if (sec) {
8199 /* nothing to do because globals are already set to zero */
8200 } else {
8201 vpush_global_sym(&func_old_type, TOK_memset);
8202 vseti(VT_LOCAL, c);
8203 vpushi(0);
8204 vpushi(size);
8205 gfunc_call(3);
8209 /* 't' contains the type and storage info. 'c' is the offset of the
8210 object in section 'sec'. If 'sec' is NULL, it means stack based
8211 allocation. 'first' is true if array '{' must be read (multi
8212 dimension implicit array init handling). 'size_only' is true if
8213 size only evaluation is wanted (only for arrays). */
8214 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8215 int first, int size_only)
8217 int index, array_length, n, no_oblock, nb, parlevel, i;
8218 int size1, align1, expr_type;
8219 Sym *s, *f;
8220 CType *t1;
8222 if (type->t & VT_ARRAY) {
8223 s = type->ref;
8224 n = s->c;
8225 array_length = 0;
8226 t1 = pointed_type(type);
8227 size1 = type_size(t1, &align1);
8229 no_oblock = 1;
8230 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8231 tok == '{') {
8232 skip('{');
8233 no_oblock = 0;
8236 /* only parse strings here if correct type (otherwise: handle
8237 them as ((w)char *) expressions */
8238 if ((tok == TOK_LSTR &&
8239 (t1->t & VT_BTYPE) == VT_INT) ||
8240 (tok == TOK_STR &&
8241 (t1->t & VT_BTYPE) == VT_BYTE)) {
8242 while (tok == TOK_STR || tok == TOK_LSTR) {
8243 int cstr_len, ch;
8244 CString *cstr;
8246 cstr = tokc.cstr;
8247 /* compute maximum number of chars wanted */
8248 if (tok == TOK_STR)
8249 cstr_len = cstr->size;
8250 else
8251 cstr_len = cstr->size / sizeof(int);
8252 cstr_len--;
8253 nb = cstr_len;
8254 if (n >= 0 && nb > (n - array_length))
8255 nb = n - array_length;
8256 if (!size_only) {
8257 if (cstr_len > nb)
8258 warning("initializer-string for array is too long");
8259 /* in order to go faster for common case (char
8260 string in global variable, we handle it
8261 specifically */
8262 if (sec && tok == TOK_STR && size1 == 1) {
8263 memcpy(sec->data + c + array_length, cstr->data, nb);
8264 } else {
8265 for(i=0;i<nb;i++) {
8266 if (tok == TOK_STR)
8267 ch = ((unsigned char *)cstr->data)[i];
8268 else
8269 ch = ((int *)cstr->data)[i];
8270 init_putv(t1, sec, c + (array_length + i) * size1,
8271 ch, EXPR_VAL);
8275 array_length += nb;
8276 next();
8278 /* only add trailing zero if enough storage (no
8279 warning in this case since it is standard) */
8280 if (n < 0 || array_length < n) {
8281 if (!size_only) {
8282 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8284 array_length++;
8286 } else {
8287 index = 0;
8288 while (tok != '}') {
8289 decl_designator(type, sec, c, &index, NULL, size_only);
8290 if (n >= 0 && index >= n)
8291 error("index too large");
8292 /* must put zero in holes (note that doing it that way
8293 ensures that it even works with designators) */
8294 if (!size_only && array_length < index) {
8295 init_putz(t1, sec, c + array_length * size1,
8296 (index - array_length) * size1);
8298 index++;
8299 if (index > array_length)
8300 array_length = index;
8301 /* special test for multi dimensional arrays (may not
8302 be strictly correct if designators are used at the
8303 same time) */
8304 if (index >= n && no_oblock)
8305 break;
8306 if (tok == '}')
8307 break;
8308 skip(',');
8311 if (!no_oblock)
8312 skip('}');
8313 /* put zeros at the end */
8314 if (!size_only && n >= 0 && array_length < n) {
8315 init_putz(t1, sec, c + array_length * size1,
8316 (n - array_length) * size1);
8318 /* patch type size if needed */
8319 if (n < 0)
8320 s->c = array_length;
8321 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8322 (sec || !first || tok == '{')) {
8323 int par_count;
8325 /* NOTE: the previous test is a specific case for automatic
8326 struct/union init */
8327 /* XXX: union needs only one init */
8329 /* XXX: this test is incorrect for local initializers
8330 beginning with ( without {. It would be much more difficult
8331 to do it correctly (ideally, the expression parser should
8332 be used in all cases) */
8333 par_count = 0;
8334 if (tok == '(') {
8335 AttributeDef ad1;
8336 CType type1;
8337 next();
8338 while (tok == '(') {
8339 par_count++;
8340 next();
8342 if (!parse_btype(&type1, &ad1))
8343 expect("cast");
8344 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8345 #if 0
8346 if (!is_assignable_types(type, &type1))
8347 error("invalid type for cast");
8348 #endif
8349 skip(')');
8351 no_oblock = 1;
8352 if (first || tok == '{') {
8353 skip('{');
8354 no_oblock = 0;
8356 s = type->ref;
8357 f = s->next;
8358 array_length = 0;
8359 index = 0;
8360 n = s->c;
8361 while (tok != '}') {
8362 decl_designator(type, sec, c, NULL, &f, size_only);
8363 index = f->c;
8364 if (!size_only && array_length < index) {
8365 init_putz(type, sec, c + array_length,
8366 index - array_length);
8368 index = index + type_size(&f->type, &align1);
8369 if (index > array_length)
8370 array_length = index;
8371 f = f->next;
8372 if (no_oblock && f == NULL)
8373 break;
8374 if (tok == '}')
8375 break;
8376 skip(',');
8378 /* put zeros at the end */
8379 if (!size_only && array_length < n) {
8380 init_putz(type, sec, c + array_length,
8381 n - array_length);
8383 if (!no_oblock)
8384 skip('}');
8385 while (par_count) {
8386 skip(')');
8387 par_count--;
8389 } else if (tok == '{') {
8390 next();
8391 decl_initializer(type, sec, c, first, size_only);
8392 skip('}');
8393 } else if (size_only) {
8394 /* just skip expression */
8395 parlevel = 0;
8396 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8397 tok != -1) {
8398 if (tok == '(')
8399 parlevel++;
8400 else if (tok == ')')
8401 parlevel--;
8402 next();
8404 } else {
8405 /* currently, we always use constant expression for globals
8406 (may change for scripting case) */
8407 expr_type = EXPR_CONST;
8408 if (!sec)
8409 expr_type = EXPR_ANY;
8410 init_putv(type, sec, c, 0, expr_type);
8414 /* parse an initializer for type 't' if 'has_init' is non zero, and
8415 allocate space in local or global data space ('r' is either
8416 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8417 variable 'v' of scope 'scope' is declared before initializers are
8418 parsed. If 'v' is zero, then a reference to the new object is put
8419 in the value stack. If 'has_init' is 2, a special parsing is done
8420 to handle string constants. */
8421 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8422 int has_init, int v, int scope)
8424 int size, align, addr, data_offset;
8425 int level;
8426 ParseState saved_parse_state;
8427 TokenString init_str;
8428 Section *sec;
8430 size = type_size(type, &align);
8431 /* If unknown size, we must evaluate it before
8432 evaluating initializers because
8433 initializers can generate global data too
8434 (e.g. string pointers or ISOC99 compound
8435 literals). It also simplifies local
8436 initializers handling */
8437 tok_str_new(&init_str);
8438 if (size < 0) {
8439 if (!has_init)
8440 error("unknown type size");
8441 /* get all init string */
8442 if (has_init == 2) {
8443 /* only get strings */
8444 while (tok == TOK_STR || tok == TOK_LSTR) {
8445 tok_str_add_tok(&init_str);
8446 next();
8448 } else {
8449 level = 0;
8450 while (level > 0 || (tok != ',' && tok != ';')) {
8451 if (tok < 0)
8452 error("unexpected end of file in initializer");
8453 tok_str_add_tok(&init_str);
8454 if (tok == '{')
8455 level++;
8456 else if (tok == '}') {
8457 if (level == 0)
8458 break;
8459 level--;
8461 next();
8464 tok_str_add(&init_str, -1);
8465 tok_str_add(&init_str, 0);
8467 /* compute size */
8468 save_parse_state(&saved_parse_state);
8470 macro_ptr = init_str.str;
8471 next();
8472 decl_initializer(type, NULL, 0, 1, 1);
8473 /* prepare second initializer parsing */
8474 macro_ptr = init_str.str;
8475 next();
8477 /* if still unknown size, error */
8478 size = type_size(type, &align);
8479 if (size < 0)
8480 error("unknown type size");
8482 /* take into account specified alignment if bigger */
8483 if (ad->aligned > align)
8484 align = ad->aligned;
8485 if ((r & VT_VALMASK) == VT_LOCAL) {
8486 sec = NULL;
8487 if (do_bounds_check && (type->t & VT_ARRAY))
8488 loc--;
8489 loc = (loc - size) & -align;
8490 addr = loc;
8491 /* handles bounds */
8492 /* XXX: currently, since we do only one pass, we cannot track
8493 '&' operators, so we add only arrays */
8494 if (do_bounds_check && (type->t & VT_ARRAY)) {
8495 unsigned long *bounds_ptr;
8496 /* add padding between regions */
8497 loc--;
8498 /* then add local bound info */
8499 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8500 bounds_ptr[0] = addr;
8501 bounds_ptr[1] = size;
8503 if (v) {
8504 /* local variable */
8505 sym_push(v, type, r, addr);
8506 } else {
8507 /* push local reference */
8508 vset(type, r, addr);
8510 } else {
8511 Sym *sym;
8513 sym = NULL;
8514 if (v && scope == VT_CONST) {
8515 /* see if the symbol was already defined */
8516 sym = sym_find(v);
8517 if (sym) {
8518 if (!is_compatible_types(&sym->type, type))
8519 error("incompatible types for redefinition of '%s'",
8520 get_tok_str(v, NULL));
8521 if (sym->type.t & VT_EXTERN) {
8522 /* if the variable is extern, it was not allocated */
8523 sym->type.t &= ~VT_EXTERN;
8524 /* set array size if it was ommited in extern
8525 declaration */
8526 if ((sym->type.t & VT_ARRAY) &&
8527 sym->type.ref->c < 0 &&
8528 type->ref->c >= 0)
8529 sym->type.ref->c = type->ref->c;
8530 } else {
8531 /* we accept several definitions of the same
8532 global variable. this is tricky, because we
8533 must play with the SHN_COMMON type of the symbol */
8534 /* XXX: should check if the variable was already
8535 initialized. It is incorrect to initialized it
8536 twice */
8537 /* no init data, we won't add more to the symbol */
8538 if (!has_init)
8539 goto no_alloc;
8544 /* allocate symbol in corresponding section */
8545 sec = ad->section;
8546 if (!sec) {
8547 if (has_init)
8548 sec = data_section;
8549 else if (tcc_state->nocommon)
8550 sec = bss_section;
8552 if (sec) {
8553 data_offset = sec->data_offset;
8554 data_offset = (data_offset + align - 1) & -align;
8555 addr = data_offset;
8556 /* very important to increment global pointer at this time
8557 because initializers themselves can create new initializers */
8558 data_offset += size;
8559 /* add padding if bound check */
8560 if (do_bounds_check)
8561 data_offset++;
8562 sec->data_offset = data_offset;
8563 /* allocate section space to put the data */
8564 if (sec->sh_type != SHT_NOBITS &&
8565 data_offset > sec->data_allocated)
8566 section_realloc(sec, data_offset);
8567 /* align section if needed */
8568 if (align > sec->sh_addralign)
8569 sec->sh_addralign = align;
8570 } else {
8571 addr = 0; /* avoid warning */
8574 if (v) {
8575 if (scope == VT_CONST) {
8576 if (!sym)
8577 goto do_def;
8578 } else {
8579 do_def:
8580 sym = sym_push(v, type, r | VT_SYM, 0);
8582 /* update symbol definition */
8583 if (sec) {
8584 put_extern_sym(sym, sec, addr, size);
8585 } else {
8586 Elf32_Sym *esym;
8587 /* put a common area */
8588 put_extern_sym(sym, NULL, align, size);
8589 /* XXX: find a nicer way */
8590 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8591 esym->st_shndx = SHN_COMMON;
8593 } else {
8594 CValue cval;
8596 /* push global reference */
8597 sym = get_sym_ref(type, sec, addr, size);
8598 cval.ul = 0;
8599 vsetc(type, VT_CONST | VT_SYM, &cval);
8600 vtop->sym = sym;
8603 /* handles bounds now because the symbol must be defined
8604 before for the relocation */
8605 if (do_bounds_check) {
8606 unsigned long *bounds_ptr;
8608 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8609 /* then add global bound info */
8610 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8611 bounds_ptr[0] = 0; /* relocated */
8612 bounds_ptr[1] = size;
8615 if (has_init) {
8616 decl_initializer(type, sec, addr, 1, 0);
8617 /* restore parse state if needed */
8618 if (init_str.str) {
8619 tok_str_free(init_str.str);
8620 restore_parse_state(&saved_parse_state);
8623 no_alloc: ;
8626 void put_func_debug(Sym *sym)
8628 char buf[512];
8630 /* stabs info */
8631 /* XXX: we put here a dummy type */
8632 snprintf(buf, sizeof(buf), "%s:%c1",
8633 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8634 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8635 cur_text_section, sym->c);
8636 last_ind = 0;
8637 last_line_num = 0;
8640 /* parse an old style function declaration list */
8641 /* XXX: check multiple parameter */
8642 static void func_decl_list(Sym *func_sym)
8644 AttributeDef ad;
8645 int v;
8646 Sym *s;
8647 CType btype, type;
8649 /* parse each declaration */
8650 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8651 if (!parse_btype(&btype, &ad))
8652 expect("declaration list");
8653 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8654 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8655 tok == ';') {
8656 /* we accept no variable after */
8657 } else {
8658 for(;;) {
8659 type = btype;
8660 type_decl(&type, &ad, &v, TYPE_DIRECT);
8661 /* find parameter in function parameter list */
8662 s = func_sym->next;
8663 while (s != NULL) {
8664 if ((s->v & ~SYM_FIELD) == v)
8665 goto found;
8666 s = s->next;
8668 error("declaration for parameter '%s' but no such parameter",
8669 get_tok_str(v, NULL));
8670 found:
8671 /* check that no storage specifier except 'register' was given */
8672 if (type.t & VT_STORAGE)
8673 error("storage class specified for '%s'", get_tok_str(v, NULL));
8674 convert_parameter_type(&type);
8675 /* we can add the type (NOTE: it could be local to the function) */
8676 s->type = type;
8677 /* accept other parameters */
8678 if (tok == ',')
8679 next();
8680 else
8681 break;
8684 skip(';');
8688 /* parse a function defined by symbol 'sym' and generate its code in
8689 'cur_text_section' */
8690 static void gen_function(Sym *sym)
8692 ind = cur_text_section->data_offset;
8693 /* NOTE: we patch the symbol size later */
8694 put_extern_sym(sym, cur_text_section, ind, 0);
8695 funcname = get_tok_str(sym->v, NULL);
8696 func_ind = ind;
8697 /* put debug symbol */
8698 if (do_debug)
8699 put_func_debug(sym);
8700 /* push a dummy symbol to enable local sym storage */
8701 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8702 gfunc_prolog(&sym->type);
8703 rsym = 0;
8704 block(NULL, NULL, NULL, NULL, 0, 0);
8705 gsym(rsym);
8706 gfunc_epilog();
8707 cur_text_section->data_offset = ind;
8708 label_pop(&global_label_stack, NULL);
8709 sym_pop(&local_stack, NULL); /* reset local stack */
8710 /* end of function */
8711 /* patch symbol size */
8712 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8713 ind - func_ind;
8714 if (do_debug) {
8715 put_stabn(N_FUN, 0, 0, ind - func_ind);
8717 funcname = ""; /* for safety */
8718 func_vt.t = VT_VOID; /* for safety */
8719 ind = 0; /* for safety */
8722 static void gen_inline_functions(void)
8724 Sym *sym;
8725 CType *type;
8726 int *str, inline_generated;
8728 /* iterate while inline function are referenced */
8729 for(;;) {
8730 inline_generated = 0;
8731 for(sym = global_stack; sym != NULL; sym = sym->prev) {
8732 type = &sym->type;
8733 if (((type->t & VT_BTYPE) == VT_FUNC) &&
8734 (type->t & (VT_STATIC | VT_INLINE)) ==
8735 (VT_STATIC | VT_INLINE) &&
8736 sym->c != 0) {
8737 /* the function was used: generate its code and
8738 convert it to a normal function */
8739 str = (int *)sym->r;
8740 sym->r = VT_SYM | VT_CONST;
8741 type->t &= ~VT_INLINE;
8743 macro_ptr = str;
8744 next();
8745 cur_text_section = text_section;
8746 gen_function(sym);
8747 macro_ptr = NULL; /* fail safe */
8749 tok_str_free(str);
8750 inline_generated = 1;
8753 if (!inline_generated)
8754 break;
8757 /* free all remaining inline function tokens */
8758 for(sym = global_stack; sym != NULL; sym = sym->prev) {
8759 type = &sym->type;
8760 if (((type->t & VT_BTYPE) == VT_FUNC) &&
8761 (type->t & (VT_STATIC | VT_INLINE)) ==
8762 (VT_STATIC | VT_INLINE)) {
8763 str = (int *)sym->r;
8764 tok_str_free(str);
8765 sym->r = 0; /* fail safe */
8770 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8771 static void decl(int l)
8773 int v, has_init, r;
8774 CType type, btype;
8775 Sym *sym;
8776 AttributeDef ad;
8778 while (1) {
8779 if (!parse_btype(&btype, &ad)) {
8780 /* skip redundant ';' */
8781 /* XXX: find more elegant solution */
8782 if (tok == ';') {
8783 next();
8784 continue;
8786 if (l == VT_CONST &&
8787 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
8788 /* global asm block */
8789 asm_global_instr();
8790 continue;
8792 /* special test for old K&R protos without explicit int
8793 type. Only accepted when defining global data */
8794 if (l == VT_LOCAL || tok < TOK_DEFINE)
8795 break;
8796 btype.t = VT_INT;
8798 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8799 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8800 tok == ';') {
8801 /* we accept no variable after */
8802 next();
8803 continue;
8805 while (1) { /* iterate thru each declaration */
8806 type = btype;
8807 type_decl(&type, &ad, &v, TYPE_DIRECT);
8808 #if 0
8810 char buf[500];
8811 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8812 printf("type = '%s'\n", buf);
8814 #endif
8815 if ((type.t & VT_BTYPE) == VT_FUNC) {
8816 /* if old style function prototype, we accept a
8817 declaration list */
8818 sym = type.ref;
8819 if (sym->c == FUNC_OLD)
8820 func_decl_list(sym);
8823 if (tok == '{') {
8824 if (l == VT_LOCAL)
8825 error("cannot use local functions");
8826 if (!(type.t & VT_FUNC))
8827 expect("function definition");
8829 /* reject abstract declarators in function definition */
8830 sym = type.ref;
8831 while ((sym = sym->next) != NULL)
8832 if (!(sym->v & ~SYM_FIELD))
8833 expect("identifier");
8835 /* XXX: cannot do better now: convert extern line to static inline */
8836 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8837 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8839 sym = sym_find(v);
8840 if (sym) {
8841 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
8842 goto func_error1;
8843 /* specific case: if not func_call defined, we put
8844 the one of the prototype */
8845 /* XXX: should have default value */
8846 if (sym->type.ref->r != FUNC_CDECL &&
8847 type.ref->r == FUNC_CDECL)
8848 type.ref->r = sym->type.ref->r;
8849 if (!is_compatible_types(&sym->type, &type)) {
8850 func_error1:
8851 error("incompatible types for redefinition of '%s'",
8852 get_tok_str(v, NULL));
8854 /* if symbol is already defined, then put complete type */
8855 sym->type = type;
8856 } else {
8857 /* put function symbol */
8858 sym = global_identifier_push(v, type.t, 0);
8859 sym->type.ref = type.ref;
8862 /* static inline functions are just recorded as a kind
8863 of macro. Their code will be emitted at the end of
8864 the compilation unit only if they are used */
8865 if ((type.t & (VT_INLINE | VT_STATIC)) ==
8866 (VT_INLINE | VT_STATIC)) {
8867 TokenString func_str;
8868 int block_level;
8870 tok_str_new(&func_str);
8872 block_level = 0;
8873 for(;;) {
8874 int t;
8875 if (tok == TOK_EOF)
8876 error("unexpected end of file");
8877 tok_str_add_tok(&func_str);
8878 t = tok;
8879 next();
8880 if (t == '{') {
8881 block_level++;
8882 } else if (t == '}') {
8883 block_level--;
8884 if (block_level == 0)
8885 break;
8888 tok_str_add(&func_str, -1);
8889 tok_str_add(&func_str, 0);
8890 sym->r = (int)func_str.str;
8891 } else {
8892 /* compute text section */
8893 cur_text_section = ad.section;
8894 if (!cur_text_section)
8895 cur_text_section = text_section;
8896 sym->r = VT_SYM | VT_CONST;
8897 gen_function(sym);
8899 break;
8900 } else {
8901 if (btype.t & VT_TYPEDEF) {
8902 /* save typedefed type */
8903 /* XXX: test storage specifiers ? */
8904 sym = sym_push(v, &type, 0, 0);
8905 sym->type.t |= VT_TYPEDEF;
8906 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
8907 /* external function definition */
8908 /* specific case for func_call attribute */
8909 if (ad.func_call)
8910 type.ref->r = ad.func_call;
8911 external_sym(v, &type, 0);
8912 } else {
8913 /* not lvalue if array */
8914 r = 0;
8915 if (!(type.t & VT_ARRAY))
8916 r |= lvalue_type(type.t);
8917 has_init = (tok == '=');
8918 if ((btype.t & VT_EXTERN) ||
8919 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
8920 !has_init && l == VT_CONST && type.ref->c < 0)) {
8921 /* external variable */
8922 /* NOTE: as GCC, uninitialized global static
8923 arrays of null size are considered as
8924 extern */
8925 external_sym(v, &type, r);
8926 } else {
8927 if (type.t & VT_STATIC)
8928 r |= VT_CONST;
8929 else
8930 r |= l;
8931 if (has_init)
8932 next();
8933 decl_initializer_alloc(&type, &ad, r,
8934 has_init, v, l);
8937 if (tok != ',') {
8938 skip(';');
8939 break;
8941 next();
8947 /* better than nothing, but needs extension to handle '-E' option
8948 correctly too */
8949 static void preprocess_init(TCCState *s1)
8951 s1->include_stack_ptr = s1->include_stack;
8952 /* XXX: move that before to avoid having to initialize
8953 file->ifdef_stack_ptr ? */
8954 s1->ifdef_stack_ptr = s1->ifdef_stack;
8955 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
8957 /* XXX: not ANSI compliant: bound checking says error */
8958 vtop = vstack - 1;
8961 /* compile the C file opened in 'file'. Return non zero if errors. */
8962 static int tcc_compile(TCCState *s1)
8964 Sym *define_start;
8965 char buf[512];
8966 volatile int section_sym;
8968 #ifdef INC_DEBUG
8969 printf("%s: **** new file\n", file->filename);
8970 #endif
8971 preprocess_init(s1);
8973 funcname = "";
8974 anon_sym = SYM_FIRST_ANOM;
8976 /* file info: full path + filename */
8977 section_sym = 0; /* avoid warning */
8978 if (do_debug) {
8979 section_sym = put_elf_sym(symtab_section, 0, 0,
8980 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
8981 text_section->sh_num, NULL);
8982 getcwd(buf, sizeof(buf));
8983 pstrcat(buf, sizeof(buf), "/");
8984 put_stabs_r(buf, N_SO, 0, 0,
8985 text_section->data_offset, text_section, section_sym);
8986 put_stabs_r(file->filename, N_SO, 0, 0,
8987 text_section->data_offset, text_section, section_sym);
8989 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8990 symbols can be safely used */
8991 put_elf_sym(symtab_section, 0, 0,
8992 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
8993 SHN_ABS, file->filename);
8995 /* define some often used types */
8996 int_type.t = VT_INT;
8998 char_pointer_type.t = VT_BYTE;
8999 mk_pointer(&char_pointer_type);
9001 func_old_type.t = VT_FUNC;
9002 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9004 #if 0
9005 /* define 'void *alloca(unsigned int)' builtin function */
9007 Sym *s1;
9009 p = anon_sym++;
9010 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9011 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9012 s1->next = NULL;
9013 sym->next = s1;
9014 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9016 #endif
9018 define_start = define_stack;
9020 if (setjmp(s1->error_jmp_buf) == 0) {
9021 s1->nb_errors = 0;
9022 s1->error_set_jmp_enabled = 1;
9024 ch = file->buf_ptr[0];
9025 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9026 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9027 next();
9028 decl(VT_CONST);
9029 if (tok != TOK_EOF)
9030 expect("declaration");
9032 /* end of translation unit info */
9033 if (do_debug) {
9034 put_stabs_r(NULL, N_SO, 0, 0,
9035 text_section->data_offset, text_section, section_sym);
9038 s1->error_set_jmp_enabled = 0;
9040 /* reset define stack, but leave -Dsymbols (may be incorrect if
9041 they are undefined) */
9042 free_defines(define_start);
9044 gen_inline_functions();
9046 sym_pop(&global_stack, NULL);
9048 return s1->nb_errors != 0 ? -1 : 0;
9051 #ifdef LIBTCC
9052 int tcc_compile_string(TCCState *s, const char *str)
9054 BufferedFile bf1, *bf = &bf1;
9055 int ret, len;
9056 char *buf;
9058 /* init file structure */
9059 bf->fd = -1;
9060 /* XXX: avoid copying */
9061 len = strlen(str);
9062 buf = tcc_malloc(len + 1);
9063 if (!buf)
9064 return -1;
9065 memcpy(buf, str, len);
9066 buf[len] = CH_EOB;
9067 bf->buf_ptr = buf;
9068 bf->buf_end = buf + len;
9069 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9070 bf->line_num = 1;
9071 file = bf;
9073 ret = tcc_compile(s);
9075 tcc_free(buf);
9077 /* currently, no need to close */
9078 return ret;
9080 #endif
9082 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9083 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9085 BufferedFile bf1, *bf = &bf1;
9087 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9088 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9089 /* default value */
9090 if (!value)
9091 value = "1";
9092 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9094 /* init file structure */
9095 bf->fd = -1;
9096 bf->buf_ptr = bf->buffer;
9097 bf->buf_end = bf->buffer + strlen(bf->buffer);
9098 *bf->buf_end = CH_EOB;
9099 bf->filename[0] = '\0';
9100 bf->line_num = 1;
9101 file = bf;
9103 s1->include_stack_ptr = s1->include_stack;
9105 /* parse with define parser */
9106 ch = file->buf_ptr[0];
9107 next_nomacro();
9108 parse_define();
9109 file = NULL;
9112 /* undefine a preprocessor symbol */
9113 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9115 TokenSym *ts;
9116 Sym *s;
9117 ts = tok_alloc(sym, strlen(sym));
9118 s = define_find(ts->tok);
9119 /* undefine symbol by putting an invalid name */
9120 if (s)
9121 define_undef(s);
9124 #ifdef CONFIG_TCC_ASM
9126 #ifdef TCC_TARGET_I386
9127 #include "i386-asm.c"
9128 #endif
9129 #include "tccasm.c"
9131 #else
9132 static void asm_instr(void)
9134 error("inline asm() not supported");
9136 static void asm_global_instr(void)
9138 error("inline asm() not supported");
9140 #endif
9142 #include "tccelf.c"
9144 #ifdef TCC_TARGET_COFF
9145 #include "tcccoff.c"
9146 #endif
9148 /* print the position in the source file of PC value 'pc' by reading
9149 the stabs debug information */
9150 static void rt_printline(unsigned long wanted_pc)
9152 Stab_Sym *sym, *sym_end;
9153 char func_name[128], last_func_name[128];
9154 unsigned long func_addr, last_pc, pc;
9155 const char *incl_files[INCLUDE_STACK_SIZE];
9156 int incl_index, len, last_line_num, i;
9157 const char *str, *p;
9159 fprintf(stderr, "0x%08lx:", wanted_pc);
9161 func_name[0] = '\0';
9162 func_addr = 0;
9163 incl_index = 0;
9164 last_func_name[0] = '\0';
9165 last_pc = 0xffffffff;
9166 last_line_num = 1;
9167 sym = (Stab_Sym *)stab_section->data + 1;
9168 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9169 while (sym < sym_end) {
9170 switch(sym->n_type) {
9171 /* function start or end */
9172 case N_FUN:
9173 if (sym->n_strx == 0) {
9174 /* we test if between last line and end of function */
9175 pc = sym->n_value + func_addr;
9176 if (wanted_pc >= last_pc && wanted_pc < pc)
9177 goto found;
9178 func_name[0] = '\0';
9179 func_addr = 0;
9180 } else {
9181 str = stabstr_section->data + sym->n_strx;
9182 p = strchr(str, ':');
9183 if (!p) {
9184 pstrcpy(func_name, sizeof(func_name), str);
9185 } else {
9186 len = p - str;
9187 if (len > sizeof(func_name) - 1)
9188 len = sizeof(func_name) - 1;
9189 memcpy(func_name, str, len);
9190 func_name[len] = '\0';
9192 func_addr = sym->n_value;
9194 break;
9195 /* line number info */
9196 case N_SLINE:
9197 pc = sym->n_value + func_addr;
9198 if (wanted_pc >= last_pc && wanted_pc < pc)
9199 goto found;
9200 last_pc = pc;
9201 last_line_num = sym->n_desc;
9202 /* XXX: slow! */
9203 strcpy(last_func_name, func_name);
9204 break;
9205 /* include files */
9206 case N_BINCL:
9207 str = stabstr_section->data + sym->n_strx;
9208 add_incl:
9209 if (incl_index < INCLUDE_STACK_SIZE) {
9210 incl_files[incl_index++] = str;
9212 break;
9213 case N_EINCL:
9214 if (incl_index > 1)
9215 incl_index--;
9216 break;
9217 case N_SO:
9218 if (sym->n_strx == 0) {
9219 incl_index = 0; /* end of translation unit */
9220 } else {
9221 str = stabstr_section->data + sym->n_strx;
9222 /* do not add path */
9223 len = strlen(str);
9224 if (len > 0 && str[len - 1] != '/')
9225 goto add_incl;
9227 break;
9229 sym++;
9232 /* second pass: we try symtab symbols (no line number info) */
9233 incl_index = 0;
9235 Elf32_Sym *sym, *sym_end;
9236 int type;
9238 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
9239 for(sym = (Elf32_Sym *)symtab_section->data + 1;
9240 sym < sym_end;
9241 sym++) {
9242 type = ELF32_ST_TYPE(sym->st_info);
9243 if (type == STT_FUNC) {
9244 if (wanted_pc >= sym->st_value &&
9245 wanted_pc < sym->st_value + sym->st_size) {
9246 pstrcpy(last_func_name, sizeof(last_func_name),
9247 strtab_section->data + sym->st_name);
9248 goto found;
9253 /* did not find any info: */
9254 fprintf(stderr, " ???\n");
9255 return;
9256 found:
9257 if (last_func_name[0] != '\0') {
9258 fprintf(stderr, " %s()", last_func_name);
9260 if (incl_index > 0) {
9261 fprintf(stderr, " (%s:%d",
9262 incl_files[incl_index - 1], last_line_num);
9263 for(i = incl_index - 2; i >= 0; i--)
9264 fprintf(stderr, ", included from %s", incl_files[i]);
9265 fprintf(stderr, ")");
9267 fprintf(stderr, "\n");
9270 #if !defined(WIN32) && !defined(CONFIG_TCCBOOT)
9272 #ifdef __i386__
9274 /* fix for glibc 2.1 */
9275 #ifndef REG_EIP
9276 #define REG_EIP EIP
9277 #define REG_EBP EBP
9278 #endif
9280 /* return the PC at frame level 'level'. Return non zero if not found */
9281 static int rt_get_caller_pc(unsigned long *paddr,
9282 ucontext_t *uc, int level)
9284 unsigned long fp;
9285 int i;
9287 if (level == 0) {
9288 #if defined(__FreeBSD__)
9289 *paddr = uc->uc_mcontext.mc_eip;
9290 #elif defined(__dietlibc__)
9291 *paddr = uc->uc_mcontext.eip;
9292 #else
9293 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9294 #endif
9295 return 0;
9296 } else {
9297 #if defined(__FreeBSD__)
9298 fp = uc->uc_mcontext.mc_ebp;
9299 #elif defined(__dietlibc__)
9300 fp = uc->uc_mcontext.ebp;
9301 #else
9302 fp = uc->uc_mcontext.gregs[REG_EBP];
9303 #endif
9304 for(i=1;i<level;i++) {
9305 /* XXX: check address validity with program info */
9306 if (fp <= 0x1000 || fp >= 0xc0000000)
9307 return -1;
9308 fp = ((unsigned long *)fp)[0];
9310 *paddr = ((unsigned long *)fp)[1];
9311 return 0;
9314 #else
9316 #warning add arch specific rt_get_caller_pc()
9318 static int rt_get_caller_pc(unsigned long *paddr,
9319 ucontext_t *uc, int level)
9321 return -1;
9323 #endif
9325 /* emit a run time error at position 'pc' */
9326 void rt_error(ucontext_t *uc, const char *fmt, ...)
9328 va_list ap;
9329 unsigned long pc;
9330 int i;
9332 va_start(ap, fmt);
9333 fprintf(stderr, "Runtime error: ");
9334 vfprintf(stderr, fmt, ap);
9335 fprintf(stderr, "\n");
9336 for(i=0;i<num_callers;i++) {
9337 if (rt_get_caller_pc(&pc, uc, i) < 0)
9338 break;
9339 if (i == 0)
9340 fprintf(stderr, "at ");
9341 else
9342 fprintf(stderr, "by ");
9343 rt_printline(pc);
9345 exit(255);
9346 va_end(ap);
9349 /* signal handler for fatal errors */
9350 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9352 ucontext_t *uc = puc;
9354 switch(signum) {
9355 case SIGFPE:
9356 switch(siginf->si_code) {
9357 case FPE_INTDIV:
9358 case FPE_FLTDIV:
9359 rt_error(uc, "division by zero");
9360 break;
9361 default:
9362 rt_error(uc, "floating point exception");
9363 break;
9365 break;
9366 case SIGBUS:
9367 case SIGSEGV:
9368 if (rt_bound_error_msg && *rt_bound_error_msg)
9369 rt_error(uc, *rt_bound_error_msg);
9370 else
9371 rt_error(uc, "dereferencing invalid pointer");
9372 break;
9373 case SIGILL:
9374 rt_error(uc, "illegal instruction");
9375 break;
9376 case SIGABRT:
9377 rt_error(uc, "abort() called");
9378 break;
9379 default:
9380 rt_error(uc, "caught signal %d", signum);
9381 break;
9383 exit(255);
9385 #endif
9387 /* do all relocations (needed before using tcc_get_symbol()) */
9388 int tcc_relocate(TCCState *s1)
9390 Section *s;
9391 int i;
9393 s1->nb_errors = 0;
9395 tcc_add_runtime(s1);
9397 build_got_entries(s1);
9399 relocate_common_syms();
9401 /* compute relocation address : section are relocated in place. We
9402 also alloc the bss space */
9403 for(i = 1; i < s1->nb_sections; i++) {
9404 s = s1->sections[i];
9405 if (s->sh_flags & SHF_ALLOC) {
9406 if (s->sh_type == SHT_NOBITS)
9407 s->data = tcc_mallocz(s->data_offset);
9408 s->sh_addr = (unsigned long)s->data;
9412 relocate_syms(s1, 1);
9414 if (s1->nb_errors != 0)
9415 return -1;
9417 /* relocate each section */
9418 for(i = 1; i < s1->nb_sections; i++) {
9419 s = s1->sections[i];
9420 if (s->reloc)
9421 relocate_section(s1, s);
9423 return 0;
9426 /* launch the compiled program with the given arguments */
9427 int tcc_run(TCCState *s1, int argc, char **argv)
9429 int (*prog_main)(int, char **);
9431 if (tcc_relocate(s1) < 0)
9432 return -1;
9434 prog_main = tcc_get_symbol_err(s1, "main");
9436 if (do_debug) {
9437 #if defined(WIN32) || defined(CONFIG_TCCBOOT)
9438 error("debug mode currently not available for Windows");
9439 #else
9440 struct sigaction sigact;
9441 /* install TCC signal handlers to print debug info on fatal
9442 runtime errors */
9443 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
9444 sigact.sa_sigaction = sig_error;
9445 sigemptyset(&sigact.sa_mask);
9446 sigaction(SIGFPE, &sigact, NULL);
9447 sigaction(SIGILL, &sigact, NULL);
9448 sigaction(SIGSEGV, &sigact, NULL);
9449 sigaction(SIGBUS, &sigact, NULL);
9450 sigaction(SIGABRT, &sigact, NULL);
9451 #endif
9454 #ifdef CONFIG_TCC_BCHECK
9455 if (do_bounds_check) {
9456 void (*bound_init)(void);
9458 /* set error function */
9459 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
9460 "__bound_error_msg");
9462 /* XXX: use .init section so that it also work in binary ? */
9463 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
9464 bound_init();
9466 #endif
9467 return (*prog_main)(argc, argv);
9470 TCCState *tcc_new(void)
9472 const char *p, *r;
9473 TCCState *s;
9474 TokenSym *ts;
9475 int i, c;
9477 s = tcc_mallocz(sizeof(TCCState));
9478 if (!s)
9479 return NULL;
9480 tcc_state = s;
9481 s->output_type = TCC_OUTPUT_MEMORY;
9483 /* init isid table */
9484 for(i=0;i<256;i++)
9485 isidnum_table[i] = isid(i) || isnum(i);
9487 /* add all tokens */
9488 table_ident = NULL;
9489 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9491 tok_ident = TOK_IDENT;
9492 p = tcc_keywords;
9493 while (*p) {
9494 r = p;
9495 for(;;) {
9496 c = *r++;
9497 if (c == '\0')
9498 break;
9500 ts = tok_alloc(p, r - p - 1);
9501 p = r;
9504 /* we add dummy defines for some special macros to speed up tests
9505 and to have working defined() */
9506 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9507 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9508 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9509 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9511 /* standard defines */
9512 tcc_define_symbol(s, "__STDC__", NULL);
9513 #if defined(TCC_TARGET_I386)
9514 tcc_define_symbol(s, "__i386__", NULL);
9515 #endif
9516 #if defined(TCC_TARGET_ARM)
9517 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
9518 tcc_define_symbol(s, "__arm_elf__", NULL);
9519 tcc_define_symbol(s, "__arm_elf", NULL);
9520 tcc_define_symbol(s, "arm_elf", NULL);
9521 tcc_define_symbol(s, "__arm__", NULL);
9522 tcc_define_symbol(s, "__arm", NULL);
9523 tcc_define_symbol(s, "arm", NULL);
9524 tcc_define_symbol(s, "__APCS_32__", NULL);
9525 #endif
9526 #if defined(linux)
9527 tcc_define_symbol(s, "__linux__", NULL);
9528 tcc_define_symbol(s, "linux", NULL);
9529 #endif
9530 /* tiny C specific defines */
9531 tcc_define_symbol(s, "__TINYC__", NULL);
9533 /* tiny C & gcc defines */
9534 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9535 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9536 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9538 /* default library paths */
9539 tcc_add_library_path(s, "/usr/local/lib");
9540 tcc_add_library_path(s, "/usr/lib");
9541 tcc_add_library_path(s, "/lib");
9543 /* no section zero */
9544 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9546 /* create standard sections */
9547 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9548 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9549 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9551 /* symbols are always generated for linking stage */
9552 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9553 ".strtab",
9554 ".hashtab", SHF_PRIVATE);
9555 strtab_section = symtab_section->link;
9557 /* private symbol table for dynamic symbols */
9558 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9559 ".dynstrtab",
9560 ".dynhashtab", SHF_PRIVATE);
9561 s->alacarte_link = 1;
9563 #ifdef CHAR_IS_UNSIGNED
9564 s->char_is_unsigned = 1;
9565 #endif
9566 return s;
9569 void tcc_delete(TCCState *s1)
9571 int i, n;
9573 /* free -D defines */
9574 free_defines(NULL);
9576 /* free tokens */
9577 n = tok_ident - TOK_IDENT;
9578 for(i = 0; i < n; i++)
9579 tcc_free(table_ident[i]);
9580 tcc_free(table_ident);
9582 /* free all sections */
9584 free_section(symtab_section->hash);
9586 free_section(s1->dynsymtab_section->hash);
9587 free_section(s1->dynsymtab_section->link);
9588 free_section(s1->dynsymtab_section);
9590 for(i = 1; i < s1->nb_sections; i++)
9591 free_section(s1->sections[i]);
9592 tcc_free(s1->sections);
9594 /* free loaded dlls array */
9595 for(i = 0; i < s1->nb_loaded_dlls; i++)
9596 tcc_free(s1->loaded_dlls[i]);
9597 tcc_free(s1->loaded_dlls);
9599 /* library paths */
9600 for(i = 0; i < s1->nb_library_paths; i++)
9601 tcc_free(s1->library_paths[i]);
9602 tcc_free(s1->library_paths);
9604 /* cached includes */
9605 for(i = 0; i < s1->nb_cached_includes; i++)
9606 tcc_free(s1->cached_includes[i]);
9607 tcc_free(s1->cached_includes);
9609 for(i = 0; i < s1->nb_include_paths; i++)
9610 tcc_free(s1->include_paths[i]);
9611 tcc_free(s1->include_paths);
9613 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9614 tcc_free(s1->sysinclude_paths[i]);
9615 tcc_free(s1->sysinclude_paths);
9617 tcc_free(s1);
9620 int tcc_add_include_path(TCCState *s1, const char *pathname)
9622 char *pathname1;
9624 pathname1 = tcc_strdup(pathname);
9625 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9626 return 0;
9629 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9631 char *pathname1;
9633 pathname1 = tcc_strdup(pathname);
9634 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9635 return 0;
9638 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9640 const char *ext, *filename1;
9641 Elf32_Ehdr ehdr;
9642 int fd, ret;
9643 BufferedFile *saved_file;
9645 /* find source file type with extension */
9646 filename1 = strrchr(filename, '/');
9647 if (filename1)
9648 filename1++;
9649 else
9650 filename1 = filename;
9651 ext = strrchr(filename1, '.');
9652 if (ext)
9653 ext++;
9655 /* open the file */
9656 saved_file = file;
9657 file = tcc_open(s1, filename);
9658 if (!file) {
9659 if (flags & AFF_PRINT_ERROR) {
9660 error_noabort("file '%s' not found", filename);
9662 ret = -1;
9663 goto fail1;
9666 if (!ext || !strcmp(ext, "c")) {
9667 /* C file assumed */
9668 ret = tcc_compile(s1);
9669 } else
9670 #ifdef CONFIG_TCC_ASM
9671 if (!strcmp(ext, "S")) {
9672 /* preprocessed assembler */
9673 ret = tcc_assemble(s1, 1);
9674 } else if (!strcmp(ext, "s")) {
9675 /* non preprocessed assembler */
9676 ret = tcc_assemble(s1, 0);
9677 } else
9678 #endif
9680 fd = file->fd;
9681 /* assume executable format: auto guess file type */
9682 ret = read(fd, &ehdr, sizeof(ehdr));
9683 lseek(fd, 0, SEEK_SET);
9684 if (ret <= 0) {
9685 error_noabort("could not read header");
9686 goto fail;
9687 } else if (ret != sizeof(ehdr)) {
9688 goto try_load_script;
9691 if (ehdr.e_ident[0] == ELFMAG0 &&
9692 ehdr.e_ident[1] == ELFMAG1 &&
9693 ehdr.e_ident[2] == ELFMAG2 &&
9694 ehdr.e_ident[3] == ELFMAG3) {
9695 file->line_num = 0; /* do not display line number if error */
9696 if (ehdr.e_type == ET_REL) {
9697 ret = tcc_load_object_file(s1, fd, 0);
9698 } else if (ehdr.e_type == ET_DYN) {
9699 if (s1->output_type == TCC_OUTPUT_MEMORY) {
9700 void *h;
9701 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
9702 if (h)
9703 ret = 0;
9704 else
9705 ret = -1;
9706 } else {
9707 ret = tcc_load_dll(s1, fd, filename,
9708 (flags & AFF_REFERENCED_DLL) != 0);
9710 } else {
9711 error_noabort("unrecognized ELF file");
9712 goto fail;
9714 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9715 file->line_num = 0; /* do not display line number if error */
9716 ret = tcc_load_archive(s1, fd);
9717 } else
9718 #ifdef TCC_TARGET_COFF
9719 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
9720 ret = tcc_load_coff(s1, fd);
9721 } else
9722 #endif
9724 /* as GNU ld, consider it is an ld script if not recognized */
9725 try_load_script:
9726 ret = tcc_load_ldscript(s1);
9727 if (ret < 0) {
9728 error_noabort("unrecognized file type");
9729 goto fail;
9733 the_end:
9734 tcc_close(file);
9735 fail1:
9736 file = saved_file;
9737 return ret;
9738 fail:
9739 ret = -1;
9740 goto the_end;
9743 int tcc_add_file(TCCState *s, const char *filename)
9745 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9748 int tcc_add_library_path(TCCState *s, const char *pathname)
9750 char *pathname1;
9752 pathname1 = tcc_strdup(pathname);
9753 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9754 return 0;
9757 /* find and load a dll. Return non zero if not found */
9758 /* XXX: add '-rpath' option support ? */
9759 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9761 char buf[1024];
9762 int i;
9764 for(i = 0; i < s->nb_library_paths; i++) {
9765 snprintf(buf, sizeof(buf), "%s/%s",
9766 s->library_paths[i], filename);
9767 if (tcc_add_file_internal(s, buf, flags) == 0)
9768 return 0;
9770 return -1;
9773 /* the library name is the same as the argument of the '-l' option */
9774 int tcc_add_library(TCCState *s, const char *libraryname)
9776 char buf[1024];
9777 int i;
9779 /* first we look for the dynamic library if not static linking */
9780 if (!s->static_link) {
9781 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9782 if (tcc_add_dll(s, buf, 0) == 0)
9783 return 0;
9786 /* then we look for the static library */
9787 for(i = 0; i < s->nb_library_paths; i++) {
9788 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9789 s->library_paths[i], libraryname);
9790 if (tcc_add_file_internal(s, buf, 0) == 0)
9791 return 0;
9793 return -1;
9796 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9798 add_elf_sym(symtab_section, val, 0,
9799 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9800 SHN_ABS, name);
9801 return 0;
9804 int tcc_set_output_type(TCCState *s, int output_type)
9806 char buf[1024];
9808 s->output_type = output_type;
9810 if (!s->nostdinc) {
9811 /* default include paths */
9812 /* XXX: reverse order needed if -isystem support */
9813 tcc_add_sysinclude_path(s, "/usr/local/include");
9814 tcc_add_sysinclude_path(s, "/usr/include");
9815 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
9816 tcc_add_sysinclude_path(s, buf);
9819 /* if bound checking, then add corresponding sections */
9820 #ifdef CONFIG_TCC_BCHECK
9821 if (do_bounds_check) {
9822 /* define symbol */
9823 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
9824 /* create bounds sections */
9825 bounds_section = new_section(s, ".bounds",
9826 SHT_PROGBITS, SHF_ALLOC);
9827 lbounds_section = new_section(s, ".lbounds",
9828 SHT_PROGBITS, SHF_ALLOC);
9830 #endif
9832 if (s->char_is_unsigned) {
9833 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
9836 /* add debug sections */
9837 if (do_debug) {
9838 /* stab symbols */
9839 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
9840 stab_section->sh_entsize = sizeof(Stab_Sym);
9841 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
9842 put_elf_str(stabstr_section, "");
9843 stab_section->link = stabstr_section;
9844 /* put first entry */
9845 put_stabs("", 0, 0, 0, 0);
9848 /* add libc crt1/crti objects */
9849 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
9850 !s->nostdlib) {
9851 if (output_type != TCC_OUTPUT_DLL)
9852 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
9853 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
9855 return 0;
9858 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
9859 #define FD_INVERT 0x0002 /* invert value before storing */
9861 typedef struct FlagDef {
9862 uint16_t offset;
9863 uint16_t flags;
9864 const char *name;
9865 } FlagDef;
9867 static const FlagDef warning_defs[] = {
9868 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
9869 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
9870 { offsetof(TCCState, warn_error), 0, "error" },
9871 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
9872 "implicit-function-declaration" },
9875 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
9876 const char *name, int value)
9878 int i;
9879 const FlagDef *p;
9880 const char *r;
9882 r = name;
9883 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
9884 r += 3;
9885 value = !value;
9887 for(i = 0, p = flags; i < nb_flags; i++, p++) {
9888 if (!strcmp(r, p->name))
9889 goto found;
9891 return -1;
9892 found:
9893 if (p->flags & FD_INVERT)
9894 value = !value;
9895 *(int *)((uint8_t *)s + p->offset) = value;
9896 return 0;
9900 /* set/reset a warning */
9901 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
9903 int i;
9904 const FlagDef *p;
9906 if (!strcmp(warning_name, "all")) {
9907 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9908 if (p->flags & WD_ALL)
9909 *(int *)((uint8_t *)s + p->offset) = 1;
9911 return 0;
9912 } else {
9913 return set_flag(s, warning_defs, countof(warning_defs),
9914 warning_name, value);
9918 static const FlagDef flag_defs[] = {
9919 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
9920 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
9921 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
9924 /* set/reset a flag */
9925 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
9927 return set_flag(s, flag_defs, countof(flag_defs),
9928 flag_name, value);
9931 #if !defined(LIBTCC)
9933 /* extract the basename of a file */
9934 static const char *tcc_basename(const char *name)
9936 const char *p;
9937 p = strrchr(name, '/');
9938 #ifdef WIN32
9939 if (!p)
9940 p = strrchr(name, '\\');
9941 #endif
9942 if (!p)
9943 p = name;
9944 else
9945 p++;
9946 return p;
9949 static int64_t getclock_us(void)
9951 #ifdef WIN32
9952 struct _timeb tb;
9953 _ftime(&tb);
9954 return (tb.time * 1000LL + tb.millitm) * 1000LL;
9955 #else
9956 struct timeval tv;
9957 gettimeofday(&tv, NULL);
9958 return tv.tv_sec * 1000000LL + tv.tv_usec;
9959 #endif
9962 void help(void)
9964 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2004 Fabrice Bellard\n"
9965 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9966 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9967 " [infile1 infile2...] [-run infile args...]\n"
9968 "\n"
9969 "General options:\n"
9970 " -v display current version\n"
9971 " -c compile only - generate an object file\n"
9972 " -o outfile set output filename\n"
9973 " -Bdir set tcc internal library path\n"
9974 " -bench output compilation statistics\n"
9975 " -run run compiled source\n"
9976 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
9977 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
9978 " -w disable all warnings\n"
9979 "Preprocessor options:\n"
9980 " -Idir add include path 'dir'\n"
9981 " -Dsym[=val] define 'sym' with value 'val'\n"
9982 " -Usym undefine 'sym'\n"
9983 "Linker options:\n"
9984 " -Ldir add library path 'dir'\n"
9985 " -llib link with dynamic or static library 'lib'\n"
9986 " -shared generate a shared library\n"
9987 " -static static linking\n"
9988 " -rdynamic export all global symbols to dynamic linker\n"
9989 " -r relocatable output\n"
9990 "Debugger options:\n"
9991 " -g generate runtime debug info\n"
9992 #ifdef CONFIG_TCC_BCHECK
9993 " -b compile with built-in memory and bounds checker (implies -g)\n"
9994 #endif
9995 " -bt N show N callers in stack traces\n"
9999 #define TCC_OPTION_HAS_ARG 0x0001
10000 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10002 typedef struct TCCOption {
10003 const char *name;
10004 uint16_t index;
10005 uint16_t flags;
10006 } TCCOption;
10008 enum {
10009 TCC_OPTION_HELP,
10010 TCC_OPTION_I,
10011 TCC_OPTION_D,
10012 TCC_OPTION_U,
10013 TCC_OPTION_L,
10014 TCC_OPTION_B,
10015 TCC_OPTION_l,
10016 TCC_OPTION_bench,
10017 TCC_OPTION_bt,
10018 TCC_OPTION_b,
10019 TCC_OPTION_g,
10020 TCC_OPTION_c,
10021 TCC_OPTION_static,
10022 TCC_OPTION_shared,
10023 TCC_OPTION_o,
10024 TCC_OPTION_r,
10025 TCC_OPTION_Wl,
10026 TCC_OPTION_W,
10027 TCC_OPTION_O,
10028 TCC_OPTION_m,
10029 TCC_OPTION_f,
10030 TCC_OPTION_nostdinc,
10031 TCC_OPTION_nostdlib,
10032 TCC_OPTION_print_search_dirs,
10033 TCC_OPTION_rdynamic,
10034 TCC_OPTION_run,
10035 TCC_OPTION_v,
10036 TCC_OPTION_w,
10039 static const TCCOption tcc_options[] = {
10040 { "h", TCC_OPTION_HELP, 0 },
10041 { "?", TCC_OPTION_HELP, 0 },
10042 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10043 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10044 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10045 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10046 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10047 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10048 { "bench", TCC_OPTION_bench, 0 },
10049 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10050 #ifdef CONFIG_TCC_BCHECK
10051 { "b", TCC_OPTION_b, 0 },
10052 #endif
10053 { "g", TCC_OPTION_g, 0 },
10054 { "c", TCC_OPTION_c, 0 },
10055 { "static", TCC_OPTION_static, 0 },
10056 { "shared", TCC_OPTION_shared, 0 },
10057 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10058 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10059 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10060 { "r", TCC_OPTION_r, 0 },
10061 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10062 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10063 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10064 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10065 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10066 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10067 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10068 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10069 { "v", TCC_OPTION_v, 0 },
10070 { "w", TCC_OPTION_w, 0 },
10071 { NULL },
10074 /* convert 'str' into an array of space separated strings */
10075 static int expand_args(char ***pargv, const char *str)
10077 const char *s1;
10078 char **argv, *arg;
10079 int argc, len;
10081 argc = 0;
10082 argv = NULL;
10083 for(;;) {
10084 while (is_space(*str))
10085 str++;
10086 if (*str == '\0')
10087 break;
10088 s1 = str;
10089 while (*str != '\0' && !is_space(*str))
10090 str++;
10091 len = str - s1;
10092 arg = tcc_malloc(len + 1);
10093 memcpy(arg, s1, len);
10094 arg[len] = '\0';
10095 dynarray_add((void ***)&argv, &argc, arg);
10097 *pargv = argv;
10098 return argc;
10101 static char **files;
10102 static int nb_files, nb_libraries;
10103 static int multiple_files;
10104 static int print_search_dirs;
10105 static int output_type;
10106 static int reloc_output;
10107 static const char *outfile;
10109 int parse_args(TCCState *s, int argc, char **argv)
10111 int optind;
10112 const TCCOption *popt;
10113 const char *optarg, *p1, *r1;
10114 char *r;
10116 optind = 0;
10117 while (1) {
10118 if (optind >= argc) {
10119 if (nb_files == 0 && !print_search_dirs)
10120 goto show_help;
10121 else
10122 break;
10124 r = argv[optind++];
10125 if (r[0] != '-') {
10126 /* add a new file */
10127 dynarray_add((void ***)&files, &nb_files, r);
10128 if (!multiple_files) {
10129 optind--;
10130 /* argv[0] will be this file */
10131 break;
10133 } else {
10134 /* find option in table (match only the first chars */
10135 popt = tcc_options;
10136 for(;;) {
10137 p1 = popt->name;
10138 if (p1 == NULL)
10139 error("invalid option -- '%s'", r);
10140 r1 = r + 1;
10141 for(;;) {
10142 if (*p1 == '\0')
10143 goto option_found;
10144 if (*r1 != *p1)
10145 break;
10146 p1++;
10147 r1++;
10149 popt++;
10151 option_found:
10152 if (popt->flags & TCC_OPTION_HAS_ARG) {
10153 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
10154 optarg = r1;
10155 } else {
10156 if (optind >= argc)
10157 error("argument to '%s' is missing", r);
10158 optarg = argv[optind++];
10160 } else {
10161 if (*r1 != '\0')
10162 goto show_help;
10163 optarg = NULL;
10166 switch(popt->index) {
10167 case TCC_OPTION_HELP:
10168 show_help:
10169 help();
10170 exit(1);
10171 case TCC_OPTION_I:
10172 if (tcc_add_include_path(s, optarg) < 0)
10173 error("too many include paths");
10174 break;
10175 case TCC_OPTION_D:
10177 char *sym, *value;
10178 sym = (char *)optarg;
10179 value = strchr(sym, '=');
10180 if (value) {
10181 *value = '\0';
10182 value++;
10184 tcc_define_symbol(s, sym, value);
10186 break;
10187 case TCC_OPTION_U:
10188 tcc_undefine_symbol(s, optarg);
10189 break;
10190 case TCC_OPTION_L:
10191 tcc_add_library_path(s, optarg);
10192 break;
10193 case TCC_OPTION_B:
10194 /* set tcc utilities path (mainly for tcc development) */
10195 tcc_lib_path = optarg;
10196 break;
10197 case TCC_OPTION_l:
10198 dynarray_add((void ***)&files, &nb_files, r);
10199 nb_libraries++;
10200 break;
10201 case TCC_OPTION_bench:
10202 do_bench = 1;
10203 break;
10204 case TCC_OPTION_bt:
10205 num_callers = atoi(optarg);
10206 break;
10207 #ifdef CONFIG_TCC_BCHECK
10208 case TCC_OPTION_b:
10209 do_bounds_check = 1;
10210 do_debug = 1;
10211 break;
10212 #endif
10213 case TCC_OPTION_g:
10214 do_debug = 1;
10215 break;
10216 case TCC_OPTION_c:
10217 multiple_files = 1;
10218 output_type = TCC_OUTPUT_OBJ;
10219 break;
10220 case TCC_OPTION_static:
10221 s->static_link = 1;
10222 break;
10223 case TCC_OPTION_shared:
10224 output_type = TCC_OUTPUT_DLL;
10225 break;
10226 case TCC_OPTION_o:
10227 multiple_files = 1;
10228 outfile = optarg;
10229 break;
10230 case TCC_OPTION_r:
10231 /* generate a .o merging several output files */
10232 reloc_output = 1;
10233 output_type = TCC_OUTPUT_OBJ;
10234 break;
10235 case TCC_OPTION_nostdinc:
10236 s->nostdinc = 1;
10237 break;
10238 case TCC_OPTION_nostdlib:
10239 s->nostdlib = 1;
10240 break;
10241 case TCC_OPTION_print_search_dirs:
10242 print_search_dirs = 1;
10243 break;
10244 case TCC_OPTION_run:
10246 int argc1;
10247 char **argv1;
10248 argc1 = expand_args(&argv1, optarg);
10249 if (argc1 > 0) {
10250 parse_args(s, argc1, argv1);
10252 multiple_files = 0;
10253 output_type = TCC_OUTPUT_MEMORY;
10255 break;
10256 case TCC_OPTION_v:
10257 printf("tcc version %s\n", TCC_VERSION);
10258 exit(0);
10259 case TCC_OPTION_f:
10260 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
10261 goto unsupported_option;
10262 break;
10263 case TCC_OPTION_W:
10264 if (tcc_set_warning(s, optarg, 1) < 0 &&
10265 s->warn_unsupported)
10266 goto unsupported_option;
10267 break;
10268 case TCC_OPTION_w:
10269 s->warn_none = 1;
10270 break;
10271 case TCC_OPTION_rdynamic:
10272 s->rdynamic = 1;
10273 break;
10274 case TCC_OPTION_Wl:
10276 const char *p;
10277 if (strstart(optarg, "-Ttext,", &p)) {
10278 s->text_addr = strtoul(p, NULL, 16);
10279 s->has_text_addr = 1;
10280 } else if (strstart(optarg, "--oformat,", &p)) {
10281 if (strstart(p, "elf32-", NULL)) {
10282 s->output_format = TCC_OUTPUT_FORMAT_ELF;
10283 } else if (!strcmp(p, "binary")) {
10284 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
10285 } else
10286 #ifdef TCC_TARGET_COFF
10287 if (!strcmp(p, "coff")) {
10288 s->output_format = TCC_OUTPUT_FORMAT_COFF;
10289 } else
10290 #endif
10292 error("target %s not found", p);
10294 } else {
10295 error("unsupported linker option '%s'", optarg);
10298 break;
10299 default:
10300 if (s->warn_unsupported) {
10301 unsupported_option:
10302 warning("unsupported option '%s'", r);
10304 break;
10308 return optind;
10311 int main(int argc, char **argv)
10313 int i;
10314 TCCState *s;
10315 int nb_objfiles, ret, optind;
10316 char objfilename[1024];
10317 int64_t start_time = 0;
10319 s = tcc_new();
10320 output_type = TCC_OUTPUT_EXE;
10321 outfile = NULL;
10322 multiple_files = 1;
10323 files = NULL;
10324 nb_files = 0;
10325 nb_libraries = 0;
10326 reloc_output = 0;
10327 print_search_dirs = 0;
10329 optind = parse_args(s, argc - 1, argv + 1) + 1;
10331 if (print_search_dirs) {
10332 /* enough for Linux kernel */
10333 printf("install: %s/\n", tcc_lib_path);
10334 return 0;
10337 nb_objfiles = nb_files - nb_libraries;
10339 /* if outfile provided without other options, we output an
10340 executable */
10341 if (outfile && output_type == TCC_OUTPUT_MEMORY)
10342 output_type = TCC_OUTPUT_EXE;
10344 /* check -c consistency : only single file handled. XXX: checks file type */
10345 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10346 /* accepts only a single input file */
10347 if (nb_objfiles != 1)
10348 error("cannot specify multiple files with -c");
10349 if (nb_libraries != 0)
10350 error("cannot specify libraries with -c");
10353 /* compute default outfile name */
10354 if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
10355 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10356 char *ext;
10357 /* strip path */
10358 pstrcpy(objfilename, sizeof(objfilename) - 1,
10359 tcc_basename(files[0]));
10360 /* add .o extension */
10361 ext = strrchr(objfilename, '.');
10362 if (!ext)
10363 goto default_outfile;
10364 strcpy(ext + 1, "o");
10365 } else {
10366 default_outfile:
10367 pstrcpy(objfilename, sizeof(objfilename), "a.out");
10369 outfile = objfilename;
10372 if (do_bench) {
10373 start_time = getclock_us();
10376 tcc_set_output_type(s, output_type);
10378 /* compile or add each files or library */
10379 for(i = 0;i < nb_files; i++) {
10380 const char *filename;
10382 filename = files[i];
10383 if (filename[0] == '-') {
10384 if (tcc_add_library(s, filename + 2) < 0)
10385 error("cannot find %s", filename);
10386 } else {
10387 if (tcc_add_file(s, filename) < 0) {
10388 ret = 1;
10389 goto the_end;
10394 /* free all files */
10395 tcc_free(files);
10397 if (do_bench) {
10398 double total_time;
10399 total_time = (double)(getclock_us() - start_time) / 1000000.0;
10400 if (total_time < 0.001)
10401 total_time = 0.001;
10402 if (total_bytes < 1)
10403 total_bytes = 1;
10404 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
10405 tok_ident - TOK_IDENT, total_lines, total_bytes,
10406 total_time, (int)(total_lines / total_time),
10407 total_bytes / total_time / 1000000.0);
10410 if (s->output_type != TCC_OUTPUT_MEMORY) {
10411 tcc_output_file(s, outfile);
10412 ret = 0;
10413 } else {
10414 ret = tcc_run(s, argc - optind, argv + optind);
10416 the_end:
10417 /* XXX: cannot do it with bound checking because of the malloc hooks */
10418 if (!do_bounds_check)
10419 tcc_delete(s);
10421 #ifdef MEM_DEBUG
10422 if (do_bench) {
10423 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
10425 #endif
10426 return ret;
10429 #endif