added -f[no-]leading-underscore
[tinycc.git] / tcc.c
blob2a59808bf579b6bacd982faf34f755d3808046c9
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 <fcntl.h>
39 #include <setjmp.h>
40 #include <time.h>
41 #ifdef WIN32
42 #include <sys/timeb.h>
43 #endif
44 #ifndef WIN32
45 #include <sys/time.h>
46 #include <sys/ucontext.h>
47 #endif
49 #endif /* !CONFIG_TCCBOOT */
51 #include "elf.h"
52 #include "stab.h"
54 #ifndef O_BINARY
55 #define O_BINARY 0
56 #endif
58 #include "libtcc.h"
60 /* parser debug */
61 //#define PARSE_DEBUG
62 /* preprocessor debug */
63 //#define PP_DEBUG
64 /* include file debug */
65 //#define INC_DEBUG
67 //#define MEM_DEBUG
69 /* assembler debug */
70 //#define ASM_DEBUG
72 /* target selection */
73 //#define TCC_TARGET_I386 /* i386 code generator */
74 //#define TCC_TARGET_ARM /* ARMv4 code generator */
75 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
77 /* default target is I386 */
78 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
79 !defined(TCC_TARGET_C67)
80 #define TCC_TARGET_I386
81 #endif
83 #if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
84 !defined(TCC_TARGET_C67)
85 #define CONFIG_TCC_BCHECK /* enable bound checking code */
86 #endif
88 #if defined(WIN32) && !defined(TCC_TARGET_PE)
89 #define CONFIG_TCC_STATIC
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 256
115 #define STRING_MAX_SIZE 1024
116 #define PACK_STACK_SIZE 8
118 #define TOK_HASH_SIZE 8192 /* must be a power of two */
119 #define TOK_ALLOC_INCR 512 /* must be a power of two */
120 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
122 /* token symbol management */
123 typedef struct TokenSym {
124 struct TokenSym *hash_next;
125 struct Sym *sym_define; /* direct pointer to define */
126 struct Sym *sym_label; /* direct pointer to label */
127 struct Sym *sym_struct; /* direct pointer to structure */
128 struct Sym *sym_identifier; /* direct pointer to identifier */
129 int tok; /* token number */
130 int len;
131 char str[1];
132 } TokenSym;
134 typedef struct CString {
135 int size; /* size in bytes */
136 void *data; /* either 'char *' or 'int *' */
137 int size_allocated;
138 void *data_allocated; /* if non NULL, data has been malloced */
139 } CString;
141 /* type definition */
142 typedef struct CType {
143 int t;
144 struct Sym *ref;
145 } CType;
147 /* constant value */
148 typedef union CValue {
149 long double ld;
150 double d;
151 float f;
152 int i;
153 unsigned int ui;
154 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
155 long long ll;
156 unsigned long long ull;
157 struct CString *cstr;
158 void *ptr;
159 int tab[1];
160 } CValue;
162 /* value on stack */
163 typedef struct SValue {
164 CType type; /* type */
165 unsigned short r; /* register + flags */
166 unsigned short r2; /* second register, used for 'long long'
167 type. If not used, set to VT_CONST */
168 CValue c; /* constant, if VT_CONST */
169 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
170 } SValue;
172 /* symbol management */
173 typedef struct Sym {
174 int v; /* symbol token */
175 int r; /* associated register */
176 int c; /* associated number */
177 CType type; /* associated type */
178 struct Sym *next; /* next related symbol */
179 struct Sym *prev; /* prev symbol in stack */
180 struct Sym *prev_tok; /* previous symbol for this token */
181 } Sym;
183 /* section definition */
184 /* XXX: use directly ELF structure for parameters ? */
185 /* special flag to indicate that the section should not be linked to
186 the other ones */
187 #define SHF_PRIVATE 0x80000000
189 typedef struct Section {
190 unsigned long data_offset; /* current data offset */
191 unsigned char *data; /* section data */
192 unsigned long data_allocated; /* used for realloc() handling */
193 int sh_name; /* elf section name (only used during output) */
194 int sh_num; /* elf section number */
195 int sh_type; /* elf section type */
196 int sh_flags; /* elf section flags */
197 int sh_info; /* elf section info */
198 int sh_addralign; /* elf section alignment */
199 int sh_entsize; /* elf entry size */
200 unsigned long sh_size; /* section size (only used during output) */
201 unsigned long sh_addr; /* address at which the section is relocated */
202 unsigned long sh_offset; /* address at which the section is relocated */
203 int nb_hashed_syms; /* used to resize the hash table */
204 struct Section *link; /* link to another section */
205 struct Section *reloc; /* corresponding section for relocation, if any */
206 struct Section *hash; /* hash table for symbols */
207 struct Section *next;
208 char name[1]; /* section name */
209 } Section;
211 typedef struct DLLReference {
212 int level;
213 char name[1];
214 } DLLReference;
216 /* GNUC attribute definition */
217 typedef struct AttributeDef {
218 int aligned;
219 int packed;
220 Section *section;
221 unsigned char func_call; /* FUNC_CDECL, FUNC_STDCALL, FUNC_FASTCALLx */
222 unsigned char dllexport;
223 } AttributeDef;
225 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
226 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
227 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
229 /* stored in 'Sym.c' field */
230 #define FUNC_NEW 1 /* ansi function prototype */
231 #define FUNC_OLD 2 /* old function prototype */
232 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
234 /* stored in 'Sym.r' field */
235 #define FUNC_CDECL 0 /* standard c call */
236 #define FUNC_STDCALL 1 /* pascal c call */
237 #define FUNC_FASTCALL1 2 /* first param in %eax */
238 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
239 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
241 /* field 'Sym.t' for macros */
242 #define MACRO_OBJ 0 /* object like macro */
243 #define MACRO_FUNC 1 /* function like macro */
245 /* field 'Sym.r' for C labels */
246 #define LABEL_DEFINED 0 /* label is defined */
247 #define LABEL_FORWARD 1 /* label is forward defined */
248 #define LABEL_DECLARED 2 /* label is declared but never used */
250 /* type_decl() types */
251 #define TYPE_ABSTRACT 1 /* type without variable */
252 #define TYPE_DIRECT 2 /* type with variable */
254 #define IO_BUF_SIZE 8192
256 typedef struct BufferedFile {
257 uint8_t *buf_ptr;
258 uint8_t *buf_end;
259 int fd;
260 int line_num; /* current line number - here to simplify code */
261 int ifndef_macro; /* #ifndef macro / #endif search */
262 int ifndef_macro_saved; /* saved ifndef_macro */
263 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
264 char inc_type; /* type of include */
265 char inc_filename[512]; /* filename specified by the user */
266 char filename[1024]; /* current filename - here to simplify code */
267 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
268 } BufferedFile;
270 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
271 #define CH_EOF (-1) /* end of file */
273 /* parsing state (used to save parser state to reparse part of the
274 source several times) */
275 typedef struct ParseState {
276 int *macro_ptr;
277 int line_num;
278 int tok;
279 CValue tokc;
280 } ParseState;
282 /* used to record tokens */
283 typedef struct TokenString {
284 int *str;
285 int len;
286 int allocated_len;
287 int last_line_num;
288 } TokenString;
290 /* include file cache, used to find files faster and also to eliminate
291 inclusion if the include file is protected by #ifndef ... #endif */
292 typedef struct CachedInclude {
293 int ifndef_macro;
294 int hash_next; /* -1 if none */
295 char type; /* '"' or '>' to give include type */
296 char filename[1]; /* path specified in #include */
297 } CachedInclude;
299 #define CACHED_INCLUDES_HASH_SIZE 512
301 /* parser */
302 static struct BufferedFile *file;
303 static int ch, tok;
304 static CValue tokc;
305 static CString tokcstr; /* current parsed string, if any */
306 /* additional informations about token */
307 static int tok_flags;
308 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
309 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
310 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
312 static int *macro_ptr, *macro_ptr_allocated;
313 static int *unget_saved_macro_ptr;
314 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
315 static int unget_buffer_enabled;
316 static int parse_flags;
317 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
318 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
319 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
320 token. line feed is also
321 returned at eof */
322 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
324 static Section *text_section, *data_section, *bss_section; /* predefined sections */
325 static Section *cur_text_section; /* current section where function code is
326 generated */
327 #ifdef CONFIG_TCC_ASM
328 static Section *last_text_section; /* to handle .previous asm directive */
329 #endif
330 /* bound check related sections */
331 static Section *bounds_section; /* contains global data bound description */
332 static Section *lbounds_section; /* contains local data bound description */
333 /* symbol sections */
334 static Section *symtab_section, *strtab_section;
336 /* debug sections */
337 static Section *stab_section, *stabstr_section;
339 /* loc : local variable index
340 ind : output code index
341 rsym: return symbol
342 anon_sym: anonymous symbol index
344 static int rsym, anon_sym, ind, loc;
345 /* expression generation modifiers */
346 static int const_wanted; /* true if constant wanted */
347 static int nocode_wanted; /* true if no code generation wanted for an expression */
348 static int global_expr; /* true if compound literals must be allocated
349 globally (used during initializers parsing */
350 static CType func_vt; /* current function return type (used by return
351 instruction) */
352 static int func_vc;
353 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
354 static int tok_ident;
355 static TokenSym **table_ident;
356 static TokenSym *hash_ident[TOK_HASH_SIZE];
357 static char token_buf[STRING_MAX_SIZE + 1];
358 static char *funcname;
359 static Sym *global_stack, *local_stack;
360 static Sym *define_stack;
361 static Sym *global_label_stack, *local_label_stack;
362 /* symbol allocator */
363 #define SYM_POOL_NB (8192 / sizeof(Sym))
364 static Sym *sym_free_first;
366 static SValue vstack[VSTACK_SIZE], *vtop;
367 /* some predefined types */
368 static CType char_pointer_type, func_old_type, int_type;
369 /* true if isid(c) || isnum(c) */
370 static unsigned char isidnum_table[256];
372 /* compile with debug symbol (and use them if error during execution) */
373 static int do_debug = 0;
375 /* compile with built-in memory and bounds checker */
376 static int do_bounds_check = 0;
378 /* display benchmark infos */
379 #if !defined(LIBTCC)
380 static int do_bench = 0;
381 #endif
382 static int total_lines;
383 static int total_bytes;
385 /* use GNU C extensions */
386 static int gnu_ext = 1;
388 /* use Tiny C extensions */
389 static int tcc_ext = 1;
391 /* max number of callers shown if error */
392 static int num_callers = 6;
393 static const char **rt_bound_error_msg;
395 /* XXX: get rid of this ASAP */
396 static struct TCCState *tcc_state;
398 /* give the path of the tcc libraries */
399 static const char *tcc_lib_path = CONFIG_TCCDIR;
401 struct TCCState {
402 int output_type;
404 BufferedFile **include_stack_ptr;
405 int *ifdef_stack_ptr;
407 /* include file handling */
408 char **include_paths;
409 int nb_include_paths;
410 char **sysinclude_paths;
411 int nb_sysinclude_paths;
412 CachedInclude **cached_includes;
413 int nb_cached_includes;
415 char **library_paths;
416 int nb_library_paths;
418 /* array of all loaded dlls (including those referenced by loaded
419 dlls) */
420 DLLReference **loaded_dlls;
421 int nb_loaded_dlls;
423 /* sections */
424 Section **sections;
425 int nb_sections; /* number of sections, including first dummy section */
427 /* got handling */
428 Section *got;
429 Section *plt;
430 unsigned long *got_offsets;
431 int nb_got_offsets;
432 /* give the correspondance from symtab indexes to dynsym indexes */
433 int *symtab_to_dynsym;
435 /* temporary dynamic symbol sections (for dll loading) */
436 Section *dynsymtab_section;
437 /* exported dynamic symbol section */
438 Section *dynsym;
440 int nostdinc; /* if true, no standard headers are added */
441 int nostdlib; /* if true, no standard libraries are added */
443 int nocommon; /* if true, do not use common symbols for .bss data */
445 /* if true, static linking is performed */
446 int static_link;
448 /* if true, all symbols are exported */
449 int rdynamic;
451 /* if true, only link in referenced objects from archive */
452 int alacarte_link;
454 /* address of text section */
455 unsigned long text_addr;
456 int has_text_addr;
458 /* output format, see TCC_OUTPUT_FORMAT_xxx */
459 int output_format;
461 /* C language options */
462 int char_is_unsigned;
464 /* warning switches */
465 int warn_write_strings;
466 int warn_unsupported;
467 int warn_error;
468 int warn_none;
469 int warn_implicit_function_declaration;
471 /* error handling */
472 void *error_opaque;
473 void (*error_func)(void *opaque, const char *msg);
474 int error_set_jmp_enabled;
475 jmp_buf error_jmp_buf;
476 int nb_errors;
478 /* tiny assembler state */
479 Sym *asm_labels;
481 /* see include_stack_ptr */
482 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
484 /* see ifdef_stack_ptr */
485 int ifdef_stack[IFDEF_STACK_SIZE];
487 /* see cached_includes */
488 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
490 /* pack stack */
491 int pack_stack[PACK_STACK_SIZE];
492 int *pack_stack_ptr;
495 /* The current value can be: */
496 #define VT_VALMASK 0x00ff
497 #define VT_CONST 0x00f0 /* constant in vc
498 (must be first non register value) */
499 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
500 #define VT_LOCAL 0x00f2 /* offset on stack */
501 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
502 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
503 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
504 #define VT_LVAL 0x0100 /* var is an lvalue */
505 #define VT_SYM 0x0200 /* a symbol value is added */
506 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
507 char/short stored in integer registers) */
508 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
509 dereferencing value */
510 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
511 bounding function call point is in vc */
512 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
513 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
514 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
515 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
517 /* types */
518 #define VT_INT 0 /* integer type */
519 #define VT_BYTE 1 /* signed byte type */
520 #define VT_SHORT 2 /* short type */
521 #define VT_VOID 3 /* void type */
522 #define VT_PTR 4 /* pointer */
523 #define VT_ENUM 5 /* enum definition */
524 #define VT_FUNC 6 /* function type */
525 #define VT_STRUCT 7 /* struct/union definition */
526 #define VT_FLOAT 8 /* IEEE float */
527 #define VT_DOUBLE 9 /* IEEE double */
528 #define VT_LDOUBLE 10 /* IEEE long double */
529 #define VT_BOOL 11 /* ISOC99 boolean type */
530 #define VT_LLONG 12 /* 64 bit integer */
531 #define VT_LONG 13 /* long integer (NEVER USED as type, only
532 during parsing) */
533 #define VT_BTYPE 0x000f /* mask for basic type */
534 #define VT_UNSIGNED 0x0010 /* unsigned type */
535 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
536 #define VT_BITFIELD 0x0040 /* bitfield modifier */
537 #define VT_CONSTANT 0x0800 /* const modifier */
538 #define VT_VOLATILE 0x1000 /* volatile modifier */
539 #define VT_SIGNED 0x2000 /* signed type */
541 /* storage */
542 #define VT_EXTERN 0x00000080 /* extern definition */
543 #define VT_STATIC 0x00000100 /* static variable */
544 #define VT_TYPEDEF 0x00000200 /* typedef definition */
545 #define VT_INLINE 0x00000400 /* inline definition */
547 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
549 /* type mask (except storage) */
550 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
551 #define VT_TYPE (~(VT_STORAGE))
553 /* token values */
555 /* warning: the following compare tokens depend on i386 asm code */
556 #define TOK_ULT 0x92
557 #define TOK_UGE 0x93
558 #define TOK_EQ 0x94
559 #define TOK_NE 0x95
560 #define TOK_ULE 0x96
561 #define TOK_UGT 0x97
562 #define TOK_LT 0x9c
563 #define TOK_GE 0x9d
564 #define TOK_LE 0x9e
565 #define TOK_GT 0x9f
567 #define TOK_LAND 0xa0
568 #define TOK_LOR 0xa1
570 #define TOK_DEC 0xa2
571 #define TOK_MID 0xa3 /* inc/dec, to void constant */
572 #define TOK_INC 0xa4
573 #define TOK_UDIV 0xb0 /* unsigned division */
574 #define TOK_UMOD 0xb1 /* unsigned modulo */
575 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
576 #define TOK_CINT 0xb3 /* number in tokc */
577 #define TOK_CCHAR 0xb4 /* char constant in tokc */
578 #define TOK_STR 0xb5 /* pointer to string in tokc */
579 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
580 #define TOK_LCHAR 0xb7
581 #define TOK_LSTR 0xb8
582 #define TOK_CFLOAT 0xb9 /* float constant */
583 #define TOK_LINENUM 0xba /* line number info */
584 #define TOK_CDOUBLE 0xc0 /* double constant */
585 #define TOK_CLDOUBLE 0xc1 /* long double constant */
586 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
587 #define TOK_ADDC1 0xc3 /* add with carry generation */
588 #define TOK_ADDC2 0xc4 /* add with carry use */
589 #define TOK_SUBC1 0xc5 /* add with carry generation */
590 #define TOK_SUBC2 0xc6 /* add with carry use */
591 #define TOK_CUINT 0xc8 /* unsigned int constant */
592 #define TOK_CLLONG 0xc9 /* long long constant */
593 #define TOK_CULLONG 0xca /* unsigned long long constant */
594 #define TOK_ARROW 0xcb
595 #define TOK_DOTS 0xcc /* three dots */
596 #define TOK_SHR 0xcd /* unsigned shift right */
597 #define TOK_PPNUM 0xce /* preprocessor number */
599 #define TOK_SHL 0x01 /* shift left */
600 #define TOK_SAR 0x02 /* signed shift right */
602 /* assignement operators : normal operator or 0x80 */
603 #define TOK_A_MOD 0xa5
604 #define TOK_A_AND 0xa6
605 #define TOK_A_MUL 0xaa
606 #define TOK_A_ADD 0xab
607 #define TOK_A_SUB 0xad
608 #define TOK_A_DIV 0xaf
609 #define TOK_A_XOR 0xde
610 #define TOK_A_OR 0xfc
611 #define TOK_A_SHL 0x81
612 #define TOK_A_SAR 0x82
614 #ifndef offsetof
615 #define offsetof(type, field) ((size_t) &((type *)0)->field)
616 #endif
618 #ifndef countof
619 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
620 #endif
622 /* WARNING: the content of this string encodes token numbers */
623 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";
625 #define TOK_EOF (-1) /* end of file */
626 #define TOK_LINEFEED 10 /* line feed */
628 /* all identificators and strings have token above that */
629 #define TOK_IDENT 256
631 /* only used for i386 asm opcodes definitions */
632 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
634 #define DEF_BWL(x) \
635 DEF(TOK_ASM_ ## x ## b, #x "b") \
636 DEF(TOK_ASM_ ## x ## w, #x "w") \
637 DEF(TOK_ASM_ ## x ## l, #x "l") \
638 DEF(TOK_ASM_ ## x, #x)
640 #define DEF_WL(x) \
641 DEF(TOK_ASM_ ## x ## w, #x "w") \
642 DEF(TOK_ASM_ ## x ## l, #x "l") \
643 DEF(TOK_ASM_ ## x, #x)
645 #define DEF_FP1(x) \
646 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
647 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
648 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
649 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
651 #define DEF_FP(x) \
652 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
653 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
654 DEF_FP1(x)
656 #define DEF_ASMTEST(x) \
657 DEF_ASM(x ## o) \
658 DEF_ASM(x ## no) \
659 DEF_ASM(x ## b) \
660 DEF_ASM(x ## c) \
661 DEF_ASM(x ## nae) \
662 DEF_ASM(x ## nb) \
663 DEF_ASM(x ## nc) \
664 DEF_ASM(x ## ae) \
665 DEF_ASM(x ## e) \
666 DEF_ASM(x ## z) \
667 DEF_ASM(x ## ne) \
668 DEF_ASM(x ## nz) \
669 DEF_ASM(x ## be) \
670 DEF_ASM(x ## na) \
671 DEF_ASM(x ## nbe) \
672 DEF_ASM(x ## a) \
673 DEF_ASM(x ## s) \
674 DEF_ASM(x ## ns) \
675 DEF_ASM(x ## p) \
676 DEF_ASM(x ## pe) \
677 DEF_ASM(x ## np) \
678 DEF_ASM(x ## po) \
679 DEF_ASM(x ## l) \
680 DEF_ASM(x ## nge) \
681 DEF_ASM(x ## nl) \
682 DEF_ASM(x ## ge) \
683 DEF_ASM(x ## le) \
684 DEF_ASM(x ## ng) \
685 DEF_ASM(x ## nle) \
686 DEF_ASM(x ## g)
688 #define TOK_ASM_int TOK_INT
690 enum tcc_token {
691 TOK_LAST = TOK_IDENT - 1,
692 #define DEF(id, str) id,
693 #include "tcctok.h"
694 #undef DEF
697 static const char tcc_keywords[] =
698 #define DEF(id, str) str "\0"
699 #include "tcctok.h"
700 #undef DEF
703 #define TOK_UIDENT TOK_DEFINE
705 #ifdef WIN32
706 int __stdcall GetModuleFileNameA(void *, char *, int);
707 void *__stdcall GetProcAddress(void *, const char *);
708 void *__stdcall GetModuleHandleA(const char *);
709 void *__stdcall LoadLibraryA(const char *);
710 int __stdcall FreeConsole(void);
712 #define snprintf _snprintf
713 #define vsnprintf _vsnprintf
714 #ifndef __GNUC__
715 #define strtold (long double)strtod
716 #define strtof (float)strtod
717 #define strtoll (long long)strtol
718 #endif
719 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__)
720 /* currently incorrect */
721 long double strtold(const char *nptr, char **endptr)
723 return (long double)strtod(nptr, endptr);
725 float strtof(const char *nptr, char **endptr)
727 return (float)strtod(nptr, endptr);
729 #else
730 /* XXX: need to define this to use them in non ISOC99 context */
731 extern float strtof (const char *__nptr, char **__endptr);
732 extern long double strtold (const char *__nptr, char **__endptr);
733 #endif
735 static char *pstrcpy(char *buf, int buf_size, const char *s);
736 static char *pstrcat(char *buf, int buf_size, const char *s);
737 static const char *tcc_basename(const char *name);
739 static void next(void);
740 static void next_nomacro(void);
741 static void parse_expr_type(CType *type);
742 static void expr_type(CType *type);
743 static void unary_type(CType *type);
744 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
745 int case_reg, int is_expr);
746 static int expr_const(void);
747 static void expr_eq(void);
748 static void gexpr(void);
749 static void gen_inline_functions(void);
750 static void decl(int l);
751 static void decl_initializer(CType *type, Section *sec, unsigned long c,
752 int first, int size_only);
753 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
754 int has_init, int v, int scope);
755 int gv(int rc);
756 void gv2(int rc1, int rc2);
757 void move_reg(int r, int s);
758 void save_regs(int n);
759 void save_reg(int r);
760 void vpop(void);
761 void vswap(void);
762 void vdup(void);
763 int get_reg(int rc);
764 int get_reg_ex(int rc,int rc2);
766 static void macro_subst(TokenString *tok_str, Sym **nested_list,
767 const int *macro_str, int can_read_stream);
768 void gen_op(int op);
769 void force_charshort_cast(int t);
770 static void gen_cast(CType *type);
771 void vstore(void);
772 static Sym *sym_find(int v);
773 static Sym *sym_push(int v, CType *type, int r, int c);
775 /* type handling */
776 static int type_size(CType *type, int *a);
777 static inline CType *pointed_type(CType *type);
778 static int pointed_size(CType *type);
779 static int lvalue_type(int t);
780 static int parse_btype(CType *type, AttributeDef *ad);
781 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
782 static int is_compatible_types(CType *type1, CType *type2);
784 int ieee_finite(double d);
785 void error(const char *fmt, ...);
786 void vpushi(int v);
787 void vrott(int n);
788 void vnrott(int n);
789 void lexpand_nr(void);
790 static void vpush_global_sym(CType *type, int v);
791 void vset(CType *type, int r, int v);
792 void type_to_str(char *buf, int buf_size,
793 CType *type, const char *varstr);
794 char *get_tok_str(int v, CValue *cv);
795 static Sym *get_sym_ref(CType *type, Section *sec,
796 unsigned long offset, unsigned long size);
797 static Sym *external_global_sym(int v, CType *type, int r);
799 /* section generation */
800 static void section_realloc(Section *sec, unsigned long new_size);
801 static void *section_ptr_add(Section *sec, unsigned long size);
802 static void put_extern_sym(Sym *sym, Section *section,
803 unsigned long value, unsigned long size);
804 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
805 static int put_elf_str(Section *s, const char *sym);
806 static int put_elf_sym(Section *s,
807 unsigned long value, unsigned long size,
808 int info, int other, int shndx, const char *name);
809 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
810 int info, int other, int sh_num, const char *name);
811 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
812 int type, int symbol);
813 static void put_stabs(const char *str, int type, int other, int desc,
814 unsigned long value);
815 static void put_stabs_r(const char *str, int type, int other, int desc,
816 unsigned long value, Section *sec, int sym_index);
817 static void put_stabn(int type, int other, int desc, int value);
818 static void put_stabd(int type, int other, int desc);
819 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
821 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
822 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
823 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
825 /* tcccoff.c */
826 int tcc_output_coff(TCCState *s1, FILE *f);
828 /* tccpe.c */
829 void *resolve_sym(TCCState *s1, const char *sym, int type);
830 int pe_load_def_file(struct TCCState *s1, FILE *fp);
831 void pe_setup_paths(struct TCCState *s1, int *p_output_type, const char **p_outfile, char *first_file);
832 unsigned long pe_add_runtime(struct TCCState *s1);
833 int tcc_output_pe(struct TCCState *s1, const char *filename);
835 /* tccasm.c */
837 #ifdef CONFIG_TCC_ASM
839 typedef struct ExprValue {
840 uint32_t v;
841 Sym *sym;
842 } ExprValue;
844 #define MAX_ASM_OPERANDS 30
846 typedef struct ASMOperand {
847 int id; /* GCC 3 optionnal identifier (0 if number only supported */
848 char *constraint;
849 char asm_str[16]; /* computed asm string for operand */
850 SValue *vt; /* C value of the expression */
851 int ref_index; /* if >= 0, gives reference to a output constraint */
852 int input_index; /* if >= 0, gives reference to an input constraint */
853 int priority; /* priority, used to assign registers */
854 int reg; /* if >= 0, register number used for this operand */
855 int is_llong; /* true if double register value */
856 int is_memory; /* true if memory operand */
857 int is_rw; /* for '+' modifier */
858 } ASMOperand;
860 static void asm_expr(TCCState *s1, ExprValue *pe);
861 static int asm_int_expr(TCCState *s1);
862 static int find_constraint(ASMOperand *operands, int nb_operands,
863 const char *name, const char **pp);
865 static int tcc_assemble(TCCState *s1, int do_preprocess);
867 #endif
869 static void asm_instr(void);
870 static void asm_global_instr(void);
872 /* true if float/double/long double type */
873 static inline int is_float(int t)
875 int bt;
876 bt = t & VT_BTYPE;
877 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
880 #ifdef TCC_TARGET_I386
881 #include "i386-gen.c"
882 #endif
884 #ifdef TCC_TARGET_ARM
885 #include "arm-gen.c"
886 #endif
888 #ifdef TCC_TARGET_C67
889 #include "c67-gen.c"
890 #endif
892 #ifdef CONFIG_TCC_STATIC
894 #define RTLD_LAZY 0x001
895 #define RTLD_NOW 0x002
896 #define RTLD_GLOBAL 0x100
897 #define RTLD_DEFAULT NULL
899 /* dummy function for profiling */
900 void *dlopen(const char *filename, int flag)
902 return NULL;
905 const char *dlerror(void)
907 return "error";
910 typedef struct TCCSyms {
911 char *str;
912 void *ptr;
913 } TCCSyms;
915 #define TCCSYM(a) { #a, &a, },
917 /* add the symbol you want here if no dynamic linking is done */
918 static TCCSyms tcc_syms[] = {
919 #if !defined(CONFIG_TCCBOOT)
920 TCCSYM(printf)
921 TCCSYM(fprintf)
922 TCCSYM(fopen)
923 TCCSYM(fclose)
924 #endif
925 { NULL, NULL },
928 void *resolve_sym(TCCState *s1, const char *symbol, int type)
930 TCCSyms *p;
931 p = tcc_syms;
932 while (p->str != NULL) {
933 if (!strcmp(p->str, symbol))
934 return p->ptr;
935 p++;
937 return NULL;
940 #elif !defined(WIN32)
942 #include <dlfcn.h>
944 void *resolve_sym(TCCState *s1, const char *sym, int type)
946 return dlsym(RTLD_DEFAULT, sym);
949 #endif
951 /********************************************************/
953 /* we use our own 'finite' function to avoid potential problems with
954 non standard math libs */
955 /* XXX: endianness dependent */
956 int ieee_finite(double d)
958 int *p = (int *)&d;
959 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
962 /* copy a string and truncate it. */
963 static char *pstrcpy(char *buf, int buf_size, const char *s)
965 char *q, *q_end;
966 int c;
968 if (buf_size > 0) {
969 q = buf;
970 q_end = buf + buf_size - 1;
971 while (q < q_end) {
972 c = *s++;
973 if (c == '\0')
974 break;
975 *q++ = c;
977 *q = '\0';
979 return buf;
982 /* strcat and truncate. */
983 static char *pstrcat(char *buf, int buf_size, const char *s)
985 int len;
986 len = strlen(buf);
987 if (len < buf_size)
988 pstrcpy(buf + len, buf_size - len, s);
989 return buf;
992 static int strstart(const char *str, const char *val, const char **ptr)
994 const char *p, *q;
995 p = str;
996 q = val;
997 while (*q != '\0') {
998 if (*p != *q)
999 return 0;
1000 p++;
1001 q++;
1003 if (ptr)
1004 *ptr = p;
1005 return 1;
1008 /* memory management */
1009 #ifdef MEM_DEBUG
1010 int mem_cur_size;
1011 int mem_max_size;
1012 #endif
1014 static inline void tcc_free(void *ptr)
1016 #ifdef MEM_DEBUG
1017 mem_cur_size -= malloc_usable_size(ptr);
1018 #endif
1019 free(ptr);
1022 static void *tcc_malloc(unsigned long size)
1024 void *ptr;
1025 ptr = malloc(size);
1026 if (!ptr && size)
1027 error("memory full");
1028 #ifdef MEM_DEBUG
1029 mem_cur_size += malloc_usable_size(ptr);
1030 if (mem_cur_size > mem_max_size)
1031 mem_max_size = mem_cur_size;
1032 #endif
1033 return ptr;
1036 static void *tcc_mallocz(unsigned long size)
1038 void *ptr;
1039 ptr = tcc_malloc(size);
1040 memset(ptr, 0, size);
1041 return ptr;
1044 static inline void *tcc_realloc(void *ptr, unsigned long size)
1046 void *ptr1;
1047 #ifdef MEM_DEBUG
1048 mem_cur_size -= malloc_usable_size(ptr);
1049 #endif
1050 ptr1 = realloc(ptr, size);
1051 #ifdef MEM_DEBUG
1052 /* NOTE: count not correct if alloc error, but not critical */
1053 mem_cur_size += malloc_usable_size(ptr1);
1054 if (mem_cur_size > mem_max_size)
1055 mem_max_size = mem_cur_size;
1056 #endif
1057 return ptr1;
1060 static char *tcc_strdup(const char *str)
1062 char *ptr;
1063 ptr = tcc_malloc(strlen(str) + 1);
1064 strcpy(ptr, str);
1065 return ptr;
1068 #define free(p) use_tcc_free(p)
1069 #define malloc(s) use_tcc_malloc(s)
1070 #define realloc(p, s) use_tcc_realloc(p, s)
1072 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1074 int nb, nb_alloc;
1075 void **pp;
1077 nb = *nb_ptr;
1078 pp = *ptab;
1079 /* every power of two we double array size */
1080 if ((nb & (nb - 1)) == 0) {
1081 if (!nb)
1082 nb_alloc = 1;
1083 else
1084 nb_alloc = nb * 2;
1085 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1086 if (!pp)
1087 error("memory full");
1088 *ptab = pp;
1090 pp[nb++] = data;
1091 *nb_ptr = nb;
1094 /* symbol allocator */
1095 static Sym *__sym_malloc(void)
1097 Sym *sym_pool, *sym, *last_sym;
1098 int i;
1100 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1102 last_sym = sym_free_first;
1103 sym = sym_pool;
1104 for(i = 0; i < SYM_POOL_NB; i++) {
1105 sym->next = last_sym;
1106 last_sym = sym;
1107 sym++;
1109 sym_free_first = last_sym;
1110 return last_sym;
1113 static inline Sym *sym_malloc(void)
1115 Sym *sym;
1116 sym = sym_free_first;
1117 if (!sym)
1118 sym = __sym_malloc();
1119 sym_free_first = sym->next;
1120 return sym;
1123 static inline void sym_free(Sym *sym)
1125 sym->next = sym_free_first;
1126 sym_free_first = sym;
1129 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1131 Section *sec;
1133 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1134 strcpy(sec->name, name);
1135 sec->sh_type = sh_type;
1136 sec->sh_flags = sh_flags;
1137 switch(sh_type) {
1138 case SHT_HASH:
1139 case SHT_REL:
1140 case SHT_DYNSYM:
1141 case SHT_SYMTAB:
1142 case SHT_DYNAMIC:
1143 sec->sh_addralign = 4;
1144 break;
1145 case SHT_STRTAB:
1146 sec->sh_addralign = 1;
1147 break;
1148 default:
1149 sec->sh_addralign = 32; /* default conservative alignment */
1150 break;
1153 /* only add section if not private */
1154 if (!(sh_flags & SHF_PRIVATE)) {
1155 sec->sh_num = s1->nb_sections;
1156 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1158 return sec;
1161 static void free_section(Section *s)
1163 tcc_free(s->data);
1164 tcc_free(s);
1167 /* realloc section and set its content to zero */
1168 static void section_realloc(Section *sec, unsigned long new_size)
1170 unsigned long size;
1171 unsigned char *data;
1173 size = sec->data_allocated;
1174 if (size == 0)
1175 size = 1;
1176 while (size < new_size)
1177 size = size * 2;
1178 data = tcc_realloc(sec->data, size);
1179 if (!data)
1180 error("memory full");
1181 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1182 sec->data = data;
1183 sec->data_allocated = size;
1186 /* reserve at least 'size' bytes in section 'sec' from
1187 sec->data_offset. */
1188 static void *section_ptr_add(Section *sec, unsigned long size)
1190 unsigned long offset, offset1;
1192 offset = sec->data_offset;
1193 offset1 = offset + size;
1194 if (offset1 > sec->data_allocated)
1195 section_realloc(sec, offset1);
1196 sec->data_offset = offset1;
1197 return sec->data + offset;
1200 /* return a reference to a section, and create it if it does not
1201 exists */
1202 Section *find_section(TCCState *s1, const char *name)
1204 Section *sec;
1205 int i;
1206 for(i = 1; i < s1->nb_sections; i++) {
1207 sec = s1->sections[i];
1208 if (!strcmp(name, sec->name))
1209 return sec;
1211 /* sections are created as PROGBITS */
1212 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1215 #define SECTION_ABS ((void *)1)
1217 /* update sym->c so that it points to an external symbol in section
1218 'section' with value 'value' */
1219 static void put_extern_sym(Sym *sym, Section *section,
1220 unsigned long value, unsigned long size)
1222 int sym_type, sym_bind, sh_num, info;
1223 Elf32_Sym *esym;
1224 const char *name;
1226 if (section == NULL)
1227 sh_num = SHN_UNDEF;
1228 else if (section == SECTION_ABS)
1229 sh_num = SHN_ABS;
1230 else
1231 sh_num = section->sh_num;
1232 if (!sym->c) {
1233 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1234 sym_type = STT_FUNC;
1235 else
1236 sym_type = STT_OBJECT;
1237 if (sym->type.t & VT_STATIC)
1238 sym_bind = STB_LOCAL;
1239 else
1240 sym_bind = STB_GLOBAL;
1242 name = get_tok_str(sym->v, NULL);
1243 #ifdef CONFIG_TCC_BCHECK
1244 if (do_bounds_check) {
1245 char buf[32];
1247 /* XXX: avoid doing that for statics ? */
1248 /* if bound checking is activated, we change some function
1249 names by adding the "__bound" prefix */
1250 switch(sym->v) {
1251 #if 0
1252 /* XXX: we rely only on malloc hooks */
1253 case TOK_malloc:
1254 case TOK_free:
1255 case TOK_realloc:
1256 case TOK_memalign:
1257 case TOK_calloc:
1258 #endif
1259 case TOK_memcpy:
1260 case TOK_memmove:
1261 case TOK_memset:
1262 case TOK_strlen:
1263 case TOK_strcpy:
1264 strcpy(buf, "__bound_");
1265 strcat(buf, name);
1266 name = buf;
1267 break;
1270 #endif
1271 info = ELF32_ST_INFO(sym_bind, sym_type);
1272 sym->c = add_elf_sym(symtab_section, value, size, info, 0, sh_num, name);
1273 } else {
1274 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1275 esym->st_value = value;
1276 esym->st_size = size;
1277 esym->st_shndx = sh_num;
1281 /* add a new relocation entry to symbol 'sym' in section 's' */
1282 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1284 if (!sym->c)
1285 put_extern_sym(sym, NULL, 0, 0);
1286 /* now we can add ELF relocation info */
1287 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1290 static inline int isid(int c)
1292 return (c >= 'a' && c <= 'z') ||
1293 (c >= 'A' && c <= 'Z') ||
1294 c == '_';
1297 static inline int isnum(int c)
1299 return c >= '0' && c <= '9';
1302 static inline int isoct(int c)
1304 return c >= '0' && c <= '7';
1307 static inline int toup(int c)
1309 if (c >= 'a' && c <= 'z')
1310 return c - 'a' + 'A';
1311 else
1312 return c;
1315 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1317 int len;
1318 len = strlen(buf);
1319 vsnprintf(buf + len, buf_size - len, fmt, ap);
1322 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1324 va_list ap;
1325 va_start(ap, fmt);
1326 strcat_vprintf(buf, buf_size, fmt, ap);
1327 va_end(ap);
1330 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1332 char buf[2048];
1333 BufferedFile **f;
1335 buf[0] = '\0';
1336 if (file) {
1337 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1338 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1339 (*f)->filename, (*f)->line_num);
1340 if (file->line_num > 0) {
1341 strcat_printf(buf, sizeof(buf),
1342 "%s:%d: ", file->filename, file->line_num);
1343 } else {
1344 strcat_printf(buf, sizeof(buf),
1345 "%s: ", file->filename);
1347 } else {
1348 strcat_printf(buf, sizeof(buf),
1349 "tcc: ");
1351 if (is_warning)
1352 strcat_printf(buf, sizeof(buf), "warning: ");
1353 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1355 if (!s1->error_func) {
1356 /* default case: stderr */
1357 fprintf(stderr, "%s\n", buf);
1358 } else {
1359 s1->error_func(s1->error_opaque, buf);
1361 if (!is_warning || s1->warn_error)
1362 s1->nb_errors++;
1365 #ifdef LIBTCC
1366 void tcc_set_error_func(TCCState *s, void *error_opaque,
1367 void (*error_func)(void *opaque, const char *msg))
1369 s->error_opaque = error_opaque;
1370 s->error_func = error_func;
1372 #endif
1374 /* error without aborting current compilation */
1375 void error_noabort(const char *fmt, ...)
1377 TCCState *s1 = tcc_state;
1378 va_list ap;
1380 va_start(ap, fmt);
1381 error1(s1, 0, fmt, ap);
1382 va_end(ap);
1385 void error(const char *fmt, ...)
1387 TCCState *s1 = tcc_state;
1388 va_list ap;
1390 va_start(ap, fmt);
1391 error1(s1, 0, fmt, ap);
1392 va_end(ap);
1393 /* better than nothing: in some cases, we accept to handle errors */
1394 if (s1->error_set_jmp_enabled) {
1395 longjmp(s1->error_jmp_buf, 1);
1396 } else {
1397 /* XXX: eliminate this someday */
1398 exit(1);
1402 void expect(const char *msg)
1404 error("%s expected", msg);
1407 void warning(const char *fmt, ...)
1409 TCCState *s1 = tcc_state;
1410 va_list ap;
1412 if (s1->warn_none)
1413 return;
1415 va_start(ap, fmt);
1416 error1(s1, 1, fmt, ap);
1417 va_end(ap);
1420 void skip(int c)
1422 if (tok != c)
1423 error("'%c' expected", c);
1424 next();
1427 static void test_lvalue(void)
1429 if (!(vtop->r & VT_LVAL))
1430 expect("lvalue");
1433 /* allocate a new token */
1434 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1436 TokenSym *ts, **ptable;
1437 int i;
1439 if (tok_ident >= SYM_FIRST_ANOM)
1440 error("memory full");
1442 /* expand token table if needed */
1443 i = tok_ident - TOK_IDENT;
1444 if ((i % TOK_ALLOC_INCR) == 0) {
1445 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1446 if (!ptable)
1447 error("memory full");
1448 table_ident = ptable;
1451 ts = tcc_malloc(sizeof(TokenSym) + len);
1452 table_ident[i] = ts;
1453 ts->tok = tok_ident++;
1454 ts->sym_define = NULL;
1455 ts->sym_label = NULL;
1456 ts->sym_struct = NULL;
1457 ts->sym_identifier = NULL;
1458 ts->len = len;
1459 ts->hash_next = NULL;
1460 memcpy(ts->str, str, len);
1461 ts->str[len] = '\0';
1462 *pts = ts;
1463 return ts;
1466 #define TOK_HASH_INIT 1
1467 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1469 /* find a token and add it if not found */
1470 static TokenSym *tok_alloc(const char *str, int len)
1472 TokenSym *ts, **pts;
1473 int i;
1474 unsigned int h;
1476 h = TOK_HASH_INIT;
1477 for(i=0;i<len;i++)
1478 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1479 h &= (TOK_HASH_SIZE - 1);
1481 pts = &hash_ident[h];
1482 for(;;) {
1483 ts = *pts;
1484 if (!ts)
1485 break;
1486 if (ts->len == len && !memcmp(ts->str, str, len))
1487 return ts;
1488 pts = &(ts->hash_next);
1490 return tok_alloc_new(pts, str, len);
1493 /* CString handling */
1495 static void cstr_realloc(CString *cstr, int new_size)
1497 int size;
1498 void *data;
1500 size = cstr->size_allocated;
1501 if (size == 0)
1502 size = 8; /* no need to allocate a too small first string */
1503 while (size < new_size)
1504 size = size * 2;
1505 data = tcc_realloc(cstr->data_allocated, size);
1506 if (!data)
1507 error("memory full");
1508 cstr->data_allocated = data;
1509 cstr->size_allocated = size;
1510 cstr->data = data;
1513 /* add a byte */
1514 static inline void cstr_ccat(CString *cstr, int ch)
1516 int size;
1517 size = cstr->size + 1;
1518 if (size > cstr->size_allocated)
1519 cstr_realloc(cstr, size);
1520 ((unsigned char *)cstr->data)[size - 1] = ch;
1521 cstr->size = size;
1524 static void cstr_cat(CString *cstr, const char *str)
1526 int c;
1527 for(;;) {
1528 c = *str;
1529 if (c == '\0')
1530 break;
1531 cstr_ccat(cstr, c);
1532 str++;
1536 /* add a wide char */
1537 static void cstr_wccat(CString *cstr, int ch)
1539 int size;
1540 size = cstr->size + sizeof(int);
1541 if (size > cstr->size_allocated)
1542 cstr_realloc(cstr, size);
1543 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1544 cstr->size = size;
1547 static void cstr_new(CString *cstr)
1549 memset(cstr, 0, sizeof(CString));
1552 /* free string and reset it to NULL */
1553 static void cstr_free(CString *cstr)
1555 tcc_free(cstr->data_allocated);
1556 cstr_new(cstr);
1559 #define cstr_reset(cstr) cstr_free(cstr)
1561 /* XXX: unicode ? */
1562 static void add_char(CString *cstr, int c)
1564 if (c == '\'' || c == '\"' || c == '\\') {
1565 /* XXX: could be more precise if char or string */
1566 cstr_ccat(cstr, '\\');
1568 if (c >= 32 && c <= 126) {
1569 cstr_ccat(cstr, c);
1570 } else {
1571 cstr_ccat(cstr, '\\');
1572 if (c == '\n') {
1573 cstr_ccat(cstr, 'n');
1574 } else {
1575 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1576 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1577 cstr_ccat(cstr, '0' + (c & 7));
1582 /* XXX: buffer overflow */
1583 /* XXX: float tokens */
1584 char *get_tok_str(int v, CValue *cv)
1586 static char buf[STRING_MAX_SIZE + 1];
1587 static CString cstr_buf;
1588 CString *cstr;
1589 unsigned char *q;
1590 char *p;
1591 int i, len;
1593 /* NOTE: to go faster, we give a fixed buffer for small strings */
1594 cstr_reset(&cstr_buf);
1595 cstr_buf.data = buf;
1596 cstr_buf.size_allocated = sizeof(buf);
1597 p = buf;
1599 switch(v) {
1600 case TOK_CINT:
1601 case TOK_CUINT:
1602 /* XXX: not quite exact, but only useful for testing */
1603 sprintf(p, "%u", cv->ui);
1604 break;
1605 case TOK_CLLONG:
1606 case TOK_CULLONG:
1607 /* XXX: not quite exact, but only useful for testing */
1608 sprintf(p, "%Lu", cv->ull);
1609 break;
1610 case TOK_CCHAR:
1611 case TOK_LCHAR:
1612 cstr_ccat(&cstr_buf, '\'');
1613 add_char(&cstr_buf, cv->i);
1614 cstr_ccat(&cstr_buf, '\'');
1615 cstr_ccat(&cstr_buf, '\0');
1616 break;
1617 case TOK_PPNUM:
1618 cstr = cv->cstr;
1619 len = cstr->size - 1;
1620 for(i=0;i<len;i++)
1621 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1622 cstr_ccat(&cstr_buf, '\0');
1623 break;
1624 case TOK_STR:
1625 case TOK_LSTR:
1626 cstr = cv->cstr;
1627 cstr_ccat(&cstr_buf, '\"');
1628 if (v == TOK_STR) {
1629 len = cstr->size - 1;
1630 for(i=0;i<len;i++)
1631 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1632 } else {
1633 len = (cstr->size / sizeof(int)) - 1;
1634 for(i=0;i<len;i++)
1635 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1637 cstr_ccat(&cstr_buf, '\"');
1638 cstr_ccat(&cstr_buf, '\0');
1639 break;
1640 case TOK_LT:
1641 v = '<';
1642 goto addv;
1643 case TOK_GT:
1644 v = '>';
1645 goto addv;
1646 case TOK_A_SHL:
1647 return strcpy(p, "<<=");
1648 case TOK_A_SAR:
1649 return strcpy(p, ">>=");
1650 default:
1651 if (v < TOK_IDENT) {
1652 /* search in two bytes table */
1653 q = tok_two_chars;
1654 while (*q) {
1655 if (q[2] == v) {
1656 *p++ = q[0];
1657 *p++ = q[1];
1658 *p = '\0';
1659 return buf;
1661 q += 3;
1663 addv:
1664 *p++ = v;
1665 *p = '\0';
1666 } else if (v < tok_ident) {
1667 return table_ident[v - TOK_IDENT]->str;
1668 } else if (v >= SYM_FIRST_ANOM) {
1669 /* special name for anonymous symbol */
1670 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1671 } else {
1672 /* should never happen */
1673 return NULL;
1675 break;
1677 return cstr_buf.data;
1680 /* push, without hashing */
1681 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1683 Sym *s;
1684 s = sym_malloc();
1685 s->v = v;
1686 s->type.t = t;
1687 s->c = c;
1688 s->next = NULL;
1689 /* add in stack */
1690 s->prev = *ps;
1691 *ps = s;
1692 return s;
1695 /* find a symbol and return its associated structure. 's' is the top
1696 of the symbol stack */
1697 static Sym *sym_find2(Sym *s, int v)
1699 while (s) {
1700 if (s->v == v)
1701 return s;
1702 s = s->prev;
1704 return NULL;
1707 /* structure lookup */
1708 static inline Sym *struct_find(int v)
1710 v -= TOK_IDENT;
1711 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1712 return NULL;
1713 return table_ident[v]->sym_struct;
1716 /* find an identifier */
1717 static inline Sym *sym_find(int v)
1719 v -= TOK_IDENT;
1720 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1721 return NULL;
1722 return table_ident[v]->sym_identifier;
1725 /* push a given symbol on the symbol stack */
1726 static Sym *sym_push(int v, CType *type, int r, int c)
1728 Sym *s, **ps;
1729 TokenSym *ts;
1731 if (local_stack)
1732 ps = &local_stack;
1733 else
1734 ps = &global_stack;
1735 s = sym_push2(ps, v, type->t, c);
1736 s->type.ref = type->ref;
1737 s->r = r;
1738 /* don't record fields or anonymous symbols */
1739 /* XXX: simplify */
1740 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1741 /* record symbol in token array */
1742 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1743 if (v & SYM_STRUCT)
1744 ps = &ts->sym_struct;
1745 else
1746 ps = &ts->sym_identifier;
1747 s->prev_tok = *ps;
1748 *ps = s;
1750 return s;
1753 /* push a global identifier */
1754 static Sym *global_identifier_push(int v, int t, int c)
1756 Sym *s, **ps;
1757 s = sym_push2(&global_stack, v, t, c);
1758 /* don't record anonymous symbol */
1759 if (v < SYM_FIRST_ANOM) {
1760 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1761 /* modify the top most local identifier, so that
1762 sym_identifier will point to 's' when popped */
1763 while (*ps != NULL)
1764 ps = &(*ps)->prev_tok;
1765 s->prev_tok = NULL;
1766 *ps = s;
1768 return s;
1771 /* pop symbols until top reaches 'b' */
1772 static void sym_pop(Sym **ptop, Sym *b)
1774 Sym *s, *ss, **ps;
1775 TokenSym *ts;
1776 int v;
1778 s = *ptop;
1779 while(s != b) {
1780 ss = s->prev;
1781 v = s->v;
1782 /* remove symbol in token array */
1783 /* XXX: simplify */
1784 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1785 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1786 if (v & SYM_STRUCT)
1787 ps = &ts->sym_struct;
1788 else
1789 ps = &ts->sym_identifier;
1790 *ps = s->prev_tok;
1792 sym_free(s);
1793 s = ss;
1795 *ptop = b;
1798 /* I/O layer */
1800 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1802 int fd;
1803 BufferedFile *bf;
1805 fd = open(filename, O_RDONLY | O_BINARY);
1806 if (fd < 0)
1807 return NULL;
1808 bf = tcc_malloc(sizeof(BufferedFile));
1809 if (!bf) {
1810 close(fd);
1811 return NULL;
1813 bf->fd = fd;
1814 bf->buf_ptr = bf->buffer;
1815 bf->buf_end = bf->buffer;
1816 bf->buffer[0] = CH_EOB; /* put eob symbol */
1817 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1818 bf->line_num = 1;
1819 bf->ifndef_macro = 0;
1820 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1821 // printf("opening '%s'\n", filename);
1822 return bf;
1825 void tcc_close(BufferedFile *bf)
1827 total_lines += bf->line_num;
1828 close(bf->fd);
1829 tcc_free(bf);
1832 /* fill input buffer and peek next char */
1833 static int tcc_peekc_slow(BufferedFile *bf)
1835 int len;
1836 /* only tries to read if really end of buffer */
1837 if (bf->buf_ptr >= bf->buf_end) {
1838 if (bf->fd != -1) {
1839 #if defined(PARSE_DEBUG)
1840 len = 8;
1841 #else
1842 len = IO_BUF_SIZE;
1843 #endif
1844 len = read(bf->fd, bf->buffer, len);
1845 if (len < 0)
1846 len = 0;
1847 } else {
1848 len = 0;
1850 total_bytes += len;
1851 bf->buf_ptr = bf->buffer;
1852 bf->buf_end = bf->buffer + len;
1853 *bf->buf_end = CH_EOB;
1855 if (bf->buf_ptr < bf->buf_end) {
1856 return bf->buf_ptr[0];
1857 } else {
1858 bf->buf_ptr = bf->buf_end;
1859 return CH_EOF;
1863 /* return the current character, handling end of block if necessary
1864 (but not stray) */
1865 static int handle_eob(void)
1867 return tcc_peekc_slow(file);
1870 /* read next char from current input file and handle end of input buffer */
1871 static inline void inp(void)
1873 ch = *(++(file->buf_ptr));
1874 /* end of buffer/file handling */
1875 if (ch == CH_EOB)
1876 ch = handle_eob();
1879 /* handle '\[\r]\n' */
1880 static void handle_stray(void)
1882 while (ch == '\\') {
1883 inp();
1884 if (ch == '\n') {
1885 file->line_num++;
1886 inp();
1887 } else if (ch == '\r') {
1888 inp();
1889 if (ch != '\n')
1890 goto fail;
1891 file->line_num++;
1892 inp();
1893 } else {
1894 fail:
1895 error("stray '\\' in program");
1900 /* skip the stray and handle the \\n case. Output an error if
1901 incorrect char after the stray */
1902 static int handle_stray1(uint8_t *p)
1904 int c;
1906 if (p >= file->buf_end) {
1907 file->buf_ptr = p;
1908 c = handle_eob();
1909 p = file->buf_ptr;
1910 if (c == '\\')
1911 goto parse_stray;
1912 } else {
1913 parse_stray:
1914 file->buf_ptr = p;
1915 ch = *p;
1916 handle_stray();
1917 p = file->buf_ptr;
1918 c = *p;
1920 return c;
1923 /* handle just the EOB case, but not stray */
1924 #define PEEKC_EOB(c, p)\
1926 p++;\
1927 c = *p;\
1928 if (c == '\\') {\
1929 file->buf_ptr = p;\
1930 c = handle_eob();\
1931 p = file->buf_ptr;\
1935 /* handle the complicated stray case */
1936 #define PEEKC(c, p)\
1938 p++;\
1939 c = *p;\
1940 if (c == '\\') {\
1941 c = handle_stray1(p);\
1942 p = file->buf_ptr;\
1946 /* input with '\[\r]\n' handling. Note that this function cannot
1947 handle other characters after '\', so you cannot call it inside
1948 strings or comments */
1949 static void minp(void)
1951 inp();
1952 if (ch == '\\')
1953 handle_stray();
1957 /* single line C++ comments */
1958 static uint8_t *parse_line_comment(uint8_t *p)
1960 int c;
1962 p++;
1963 for(;;) {
1964 c = *p;
1965 redo:
1966 if (c == '\n' || c == CH_EOF) {
1967 break;
1968 } else if (c == '\\') {
1969 file->buf_ptr = p;
1970 c = handle_eob();
1971 p = file->buf_ptr;
1972 if (c == '\\') {
1973 PEEKC_EOB(c, p);
1974 if (c == '\n') {
1975 file->line_num++;
1976 PEEKC_EOB(c, p);
1977 } else if (c == '\r') {
1978 PEEKC_EOB(c, p);
1979 if (c == '\n') {
1980 file->line_num++;
1981 PEEKC_EOB(c, p);
1984 } else {
1985 goto redo;
1987 } else {
1988 p++;
1991 return p;
1994 /* C comments */
1995 static uint8_t *parse_comment(uint8_t *p)
1997 int c;
1999 p++;
2000 for(;;) {
2001 /* fast skip loop */
2002 for(;;) {
2003 c = *p;
2004 if (c == '\n' || c == '*' || c == '\\')
2005 break;
2006 p++;
2007 c = *p;
2008 if (c == '\n' || c == '*' || c == '\\')
2009 break;
2010 p++;
2012 /* now we can handle all the cases */
2013 if (c == '\n') {
2014 file->line_num++;
2015 p++;
2016 } else if (c == '*') {
2017 p++;
2018 for(;;) {
2019 c = *p;
2020 if (c == '*') {
2021 p++;
2022 } else if (c == '/') {
2023 goto end_of_comment;
2024 } else if (c == '\\') {
2025 file->buf_ptr = p;
2026 c = handle_eob();
2027 p = file->buf_ptr;
2028 if (c == '\\') {
2029 /* skip '\[\r]\n', otherwise just skip the stray */
2030 while (c == '\\') {
2031 PEEKC_EOB(c, p);
2032 if (c == '\n') {
2033 file->line_num++;
2034 PEEKC_EOB(c, p);
2035 } else if (c == '\r') {
2036 PEEKC_EOB(c, p);
2037 if (c == '\n') {
2038 file->line_num++;
2039 PEEKC_EOB(c, p);
2041 } else {
2042 goto after_star;
2046 } else {
2047 break;
2050 after_star: ;
2051 } else {
2052 /* stray, eob or eof */
2053 file->buf_ptr = p;
2054 c = handle_eob();
2055 p = file->buf_ptr;
2056 if (c == CH_EOF) {
2057 error("unexpected end of file in comment");
2058 } else if (c == '\\') {
2059 p++;
2063 end_of_comment:
2064 p++;
2065 return p;
2068 #define cinp minp
2070 /* space exlcuding newline */
2071 static inline int is_space(int ch)
2073 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2076 static inline void skip_spaces(void)
2078 while (is_space(ch))
2079 cinp();
2082 /* parse a string without interpreting escapes */
2083 static uint8_t *parse_pp_string(uint8_t *p,
2084 int sep, CString *str)
2086 int c;
2087 p++;
2088 for(;;) {
2089 c = *p;
2090 if (c == sep) {
2091 break;
2092 } else if (c == '\\') {
2093 file->buf_ptr = p;
2094 c = handle_eob();
2095 p = file->buf_ptr;
2096 if (c == CH_EOF) {
2097 unterminated_string:
2098 /* XXX: indicate line number of start of string */
2099 error("missing terminating %c character", sep);
2100 } else if (c == '\\') {
2101 /* escape : just skip \[\r]\n */
2102 PEEKC_EOB(c, p);
2103 if (c == '\n') {
2104 file->line_num++;
2105 p++;
2106 } else if (c == '\r') {
2107 PEEKC_EOB(c, p);
2108 if (c != '\n')
2109 expect("'\n' after '\r'");
2110 file->line_num++;
2111 p++;
2112 } else if (c == CH_EOF) {
2113 goto unterminated_string;
2114 } else {
2115 if (str) {
2116 cstr_ccat(str, '\\');
2117 cstr_ccat(str, c);
2119 p++;
2122 } else if (c == '\n') {
2123 file->line_num++;
2124 goto add_char;
2125 } else if (c == '\r') {
2126 PEEKC_EOB(c, p);
2127 if (c != '\n') {
2128 if (str)
2129 cstr_ccat(str, '\r');
2130 } else {
2131 file->line_num++;
2132 goto add_char;
2134 } else {
2135 add_char:
2136 if (str)
2137 cstr_ccat(str, c);
2138 p++;
2141 p++;
2142 return p;
2145 /* skip block of text until #else, #elif or #endif. skip also pairs of
2146 #if/#endif */
2147 void preprocess_skip(void)
2149 int a, start_of_line, c;
2150 uint8_t *p;
2152 p = file->buf_ptr;
2153 start_of_line = 1;
2154 a = 0;
2155 for(;;) {
2156 redo_no_start:
2157 c = *p;
2158 switch(c) {
2159 case ' ':
2160 case '\t':
2161 case '\f':
2162 case '\v':
2163 case '\r':
2164 p++;
2165 goto redo_no_start;
2166 case '\n':
2167 start_of_line = 1;
2168 file->line_num++;
2169 p++;
2170 goto redo_no_start;
2171 case '\\':
2172 file->buf_ptr = p;
2173 c = handle_eob();
2174 if (c == CH_EOF) {
2175 expect("#endif");
2176 } else if (c == '\\') {
2177 /* XXX: incorrect: should not give an error */
2178 ch = file->buf_ptr[0];
2179 handle_stray();
2181 p = file->buf_ptr;
2182 goto redo_no_start;
2183 /* skip strings */
2184 case '\"':
2185 case '\'':
2186 p = parse_pp_string(p, c, NULL);
2187 break;
2188 /* skip comments */
2189 case '/':
2190 file->buf_ptr = p;
2191 ch = *p;
2192 minp();
2193 p = file->buf_ptr;
2194 if (ch == '*') {
2195 p = parse_comment(p);
2196 } else if (ch == '/') {
2197 p = parse_line_comment(p);
2199 break;
2201 case '#':
2202 p++;
2203 if (start_of_line) {
2204 file->buf_ptr = p;
2205 next_nomacro();
2206 p = file->buf_ptr;
2207 if (a == 0 &&
2208 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2209 goto the_end;
2210 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2211 a++;
2212 else if (tok == TOK_ENDIF)
2213 a--;
2215 break;
2216 default:
2217 p++;
2218 break;
2220 start_of_line = 0;
2222 the_end: ;
2223 file->buf_ptr = p;
2226 /* ParseState handling */
2228 /* XXX: currently, no include file info is stored. Thus, we cannot display
2229 accurate messages if the function or data definition spans multiple
2230 files */
2232 /* save current parse state in 's' */
2233 void save_parse_state(ParseState *s)
2235 s->line_num = file->line_num;
2236 s->macro_ptr = macro_ptr;
2237 s->tok = tok;
2238 s->tokc = tokc;
2241 /* restore parse state from 's' */
2242 void restore_parse_state(ParseState *s)
2244 file->line_num = s->line_num;
2245 macro_ptr = s->macro_ptr;
2246 tok = s->tok;
2247 tokc = s->tokc;
2250 /* return the number of additional 'ints' necessary to store the
2251 token */
2252 static inline int tok_ext_size(int t)
2254 switch(t) {
2255 /* 4 bytes */
2256 case TOK_CINT:
2257 case TOK_CUINT:
2258 case TOK_CCHAR:
2259 case TOK_LCHAR:
2260 case TOK_CFLOAT:
2261 case TOK_LINENUM:
2262 return 1;
2263 case TOK_STR:
2264 case TOK_LSTR:
2265 case TOK_PPNUM:
2266 error("unsupported token");
2267 return 1;
2268 case TOK_CDOUBLE:
2269 case TOK_CLLONG:
2270 case TOK_CULLONG:
2271 return 2;
2272 case TOK_CLDOUBLE:
2273 return LDOUBLE_SIZE / 4;
2274 default:
2275 return 0;
2279 /* token string handling */
2281 static inline void tok_str_new(TokenString *s)
2283 s->str = NULL;
2284 s->len = 0;
2285 s->allocated_len = 0;
2286 s->last_line_num = -1;
2289 static void tok_str_free(int *str)
2291 tcc_free(str);
2294 static int *tok_str_realloc(TokenString *s)
2296 int *str, len;
2298 if (s->allocated_len == 0) {
2299 len = 8;
2300 } else {
2301 len = s->allocated_len * 2;
2303 str = tcc_realloc(s->str, len * sizeof(int));
2304 if (!str)
2305 error("memory full");
2306 s->allocated_len = len;
2307 s->str = str;
2308 return str;
2311 static void tok_str_add(TokenString *s, int t)
2313 int len, *str;
2315 len = s->len;
2316 str = s->str;
2317 if (len >= s->allocated_len)
2318 str = tok_str_realloc(s);
2319 str[len++] = t;
2320 s->len = len;
2323 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2325 int len, *str;
2327 len = s->len;
2328 str = s->str;
2330 /* allocate space for worst case */
2331 if (len + TOK_MAX_SIZE > s->allocated_len)
2332 str = tok_str_realloc(s);
2333 str[len++] = t;
2334 switch(t) {
2335 case TOK_CINT:
2336 case TOK_CUINT:
2337 case TOK_CCHAR:
2338 case TOK_LCHAR:
2339 case TOK_CFLOAT:
2340 case TOK_LINENUM:
2341 str[len++] = cv->tab[0];
2342 break;
2343 case TOK_PPNUM:
2344 case TOK_STR:
2345 case TOK_LSTR:
2347 int nb_words;
2348 CString *cstr;
2350 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2351 while ((len + nb_words) > s->allocated_len)
2352 str = tok_str_realloc(s);
2353 cstr = (CString *)(str + len);
2354 cstr->data = NULL;
2355 cstr->size = cv->cstr->size;
2356 cstr->data_allocated = NULL;
2357 cstr->size_allocated = cstr->size;
2358 memcpy((char *)cstr + sizeof(CString),
2359 cv->cstr->data, cstr->size);
2360 len += nb_words;
2362 break;
2363 case TOK_CDOUBLE:
2364 case TOK_CLLONG:
2365 case TOK_CULLONG:
2366 #if LDOUBLE_SIZE == 8
2367 case TOK_CLDOUBLE:
2368 #endif
2369 str[len++] = cv->tab[0];
2370 str[len++] = cv->tab[1];
2371 break;
2372 #if LDOUBLE_SIZE == 12
2373 case TOK_CLDOUBLE:
2374 str[len++] = cv->tab[0];
2375 str[len++] = cv->tab[1];
2376 str[len++] = cv->tab[2];
2377 #elif LDOUBLE_SIZE != 8
2378 #error add long double size support
2379 #endif
2380 break;
2381 default:
2382 break;
2384 s->len = len;
2387 /* add the current parse token in token string 's' */
2388 static void tok_str_add_tok(TokenString *s)
2390 CValue cval;
2392 /* save line number info */
2393 if (file->line_num != s->last_line_num) {
2394 s->last_line_num = file->line_num;
2395 cval.i = s->last_line_num;
2396 tok_str_add2(s, TOK_LINENUM, &cval);
2398 tok_str_add2(s, tok, &tokc);
2401 #if LDOUBLE_SIZE == 12
2402 #define LDOUBLE_GET(p, cv) \
2403 cv.tab[0] = p[0]; \
2404 cv.tab[1] = p[1]; \
2405 cv.tab[2] = p[2];
2406 #elif LDOUBLE_SIZE == 8
2407 #define LDOUBLE_GET(p, cv) \
2408 cv.tab[0] = p[0]; \
2409 cv.tab[1] = p[1];
2410 #else
2411 #error add long double size support
2412 #endif
2415 /* get a token from an integer array and increment pointer
2416 accordingly. we code it as a macro to avoid pointer aliasing. */
2417 #define TOK_GET(t, p, cv) \
2419 t = *p++; \
2420 switch(t) { \
2421 case TOK_CINT: \
2422 case TOK_CUINT: \
2423 case TOK_CCHAR: \
2424 case TOK_LCHAR: \
2425 case TOK_CFLOAT: \
2426 case TOK_LINENUM: \
2427 cv.tab[0] = *p++; \
2428 break; \
2429 case TOK_STR: \
2430 case TOK_LSTR: \
2431 case TOK_PPNUM: \
2432 cv.cstr = (CString *)p; \
2433 cv.cstr->data = (char *)p + sizeof(CString);\
2434 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2435 break; \
2436 case TOK_CDOUBLE: \
2437 case TOK_CLLONG: \
2438 case TOK_CULLONG: \
2439 cv.tab[0] = p[0]; \
2440 cv.tab[1] = p[1]; \
2441 p += 2; \
2442 break; \
2443 case TOK_CLDOUBLE: \
2444 LDOUBLE_GET(p, cv); \
2445 p += LDOUBLE_SIZE / 4; \
2446 break; \
2447 default: \
2448 break; \
2452 /* defines handling */
2453 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2455 Sym *s;
2457 s = sym_push2(&define_stack, v, macro_type, (int)str);
2458 s->next = first_arg;
2459 table_ident[v - TOK_IDENT]->sym_define = s;
2462 /* undefined a define symbol. Its name is just set to zero */
2463 static void define_undef(Sym *s)
2465 int v;
2466 v = s->v;
2467 if (v >= TOK_IDENT && v < tok_ident)
2468 table_ident[v - TOK_IDENT]->sym_define = NULL;
2469 s->v = 0;
2472 static inline Sym *define_find(int v)
2474 v -= TOK_IDENT;
2475 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2476 return NULL;
2477 return table_ident[v]->sym_define;
2480 /* free define stack until top reaches 'b' */
2481 static void free_defines(Sym *b)
2483 Sym *top, *top1;
2484 int v;
2486 top = define_stack;
2487 while (top != b) {
2488 top1 = top->prev;
2489 /* do not free args or predefined defines */
2490 if (top->c)
2491 tok_str_free((int *)top->c);
2492 v = top->v;
2493 if (v >= TOK_IDENT && v < tok_ident)
2494 table_ident[v - TOK_IDENT]->sym_define = NULL;
2495 sym_free(top);
2496 top = top1;
2498 define_stack = b;
2501 /* label lookup */
2502 static Sym *label_find(int v)
2504 v -= TOK_IDENT;
2505 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2506 return NULL;
2507 return table_ident[v]->sym_label;
2510 static Sym *label_push(Sym **ptop, int v, int flags)
2512 Sym *s, **ps;
2513 s = sym_push2(ptop, v, 0, 0);
2514 s->r = flags;
2515 ps = &table_ident[v - TOK_IDENT]->sym_label;
2516 if (ptop == &global_label_stack) {
2517 /* modify the top most local identifier, so that
2518 sym_identifier will point to 's' when popped */
2519 while (*ps != NULL)
2520 ps = &(*ps)->prev_tok;
2522 s->prev_tok = *ps;
2523 *ps = s;
2524 return s;
2527 /* pop labels until element last is reached. Look if any labels are
2528 undefined. Define symbols if '&&label' was used. */
2529 static void label_pop(Sym **ptop, Sym *slast)
2531 Sym *s, *s1;
2532 for(s = *ptop; s != slast; s = s1) {
2533 s1 = s->prev;
2534 if (s->r == LABEL_DECLARED) {
2535 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2536 } else if (s->r == LABEL_FORWARD) {
2537 error("label '%s' used but not defined",
2538 get_tok_str(s->v, NULL));
2539 } else {
2540 if (s->c) {
2541 /* define corresponding symbol. A size of
2542 1 is put. */
2543 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2546 /* remove label */
2547 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2548 sym_free(s);
2550 *ptop = slast;
2553 /* eval an expression for #if/#elif */
2554 static int expr_preprocess(void)
2556 int c, t;
2557 TokenString str;
2559 tok_str_new(&str);
2560 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2561 next(); /* do macro subst */
2562 if (tok == TOK_DEFINED) {
2563 next_nomacro();
2564 t = tok;
2565 if (t == '(')
2566 next_nomacro();
2567 c = define_find(tok) != 0;
2568 if (t == '(')
2569 next_nomacro();
2570 tok = TOK_CINT;
2571 tokc.i = c;
2572 } else if (tok >= TOK_IDENT) {
2573 /* if undefined macro */
2574 tok = TOK_CINT;
2575 tokc.i = 0;
2577 tok_str_add_tok(&str);
2579 tok_str_add(&str, -1); /* simulate end of file */
2580 tok_str_add(&str, 0);
2581 /* now evaluate C constant expression */
2582 macro_ptr = str.str;
2583 next();
2584 c = expr_const();
2585 macro_ptr = NULL;
2586 tok_str_free(str.str);
2587 return c != 0;
2590 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2591 static void tok_print(int *str)
2593 int t;
2594 CValue cval;
2596 while (1) {
2597 TOK_GET(t, str, cval);
2598 if (!t)
2599 break;
2600 printf(" %s", get_tok_str(t, &cval));
2602 printf("\n");
2604 #endif
2606 /* parse after #define */
2607 static void parse_define(void)
2609 Sym *s, *first, **ps;
2610 int v, t, varg, is_vaargs, c;
2611 TokenString str;
2613 v = tok;
2614 if (v < TOK_IDENT)
2615 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2616 /* XXX: should check if same macro (ANSI) */
2617 first = NULL;
2618 t = MACRO_OBJ;
2619 /* '(' must be just after macro definition for MACRO_FUNC */
2620 c = file->buf_ptr[0];
2621 if (c == '\\')
2622 c = handle_stray1(file->buf_ptr);
2623 if (c == '(') {
2624 next_nomacro();
2625 next_nomacro();
2626 ps = &first;
2627 while (tok != ')') {
2628 varg = tok;
2629 next_nomacro();
2630 is_vaargs = 0;
2631 if (varg == TOK_DOTS) {
2632 varg = TOK___VA_ARGS__;
2633 is_vaargs = 1;
2634 } else if (tok == TOK_DOTS && gnu_ext) {
2635 is_vaargs = 1;
2636 next_nomacro();
2638 if (varg < TOK_IDENT)
2639 error("badly punctuated parameter list");
2640 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2641 *ps = s;
2642 ps = &s->next;
2643 if (tok != ',')
2644 break;
2645 next_nomacro();
2647 t = MACRO_FUNC;
2649 tok_str_new(&str);
2650 next_nomacro();
2651 /* EOF testing necessary for '-D' handling */
2652 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2653 tok_str_add2(&str, tok, &tokc);
2654 next_nomacro();
2656 tok_str_add(&str, 0);
2657 #ifdef PP_DEBUG
2658 printf("define %s %d: ", get_tok_str(v, NULL), t);
2659 tok_print(str.str);
2660 #endif
2661 define_push(v, t, str.str, first);
2664 static inline int hash_cached_include(int type, const char *filename)
2666 const unsigned char *s;
2667 unsigned int h;
2669 h = TOK_HASH_INIT;
2670 h = TOK_HASH_FUNC(h, type);
2671 s = filename;
2672 while (*s) {
2673 h = TOK_HASH_FUNC(h, *s);
2674 s++;
2676 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2677 return h;
2680 /* XXX: use a token or a hash table to accelerate matching ? */
2681 static CachedInclude *search_cached_include(TCCState *s1,
2682 int type, const char *filename)
2684 CachedInclude *e;
2685 int i, h;
2686 h = hash_cached_include(type, filename);
2687 i = s1->cached_includes_hash[h];
2688 for(;;) {
2689 if (i == 0)
2690 break;
2691 e = s1->cached_includes[i - 1];
2692 if (e->type == type && !strcmp(e->filename, filename))
2693 return e;
2694 i = e->hash_next;
2696 return NULL;
2699 static inline void add_cached_include(TCCState *s1, int type,
2700 const char *filename, int ifndef_macro)
2702 CachedInclude *e;
2703 int h;
2705 if (search_cached_include(s1, type, filename))
2706 return;
2707 #ifdef INC_DEBUG
2708 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2709 #endif
2710 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2711 if (!e)
2712 return;
2713 e->type = type;
2714 strcpy(e->filename, filename);
2715 e->ifndef_macro = ifndef_macro;
2716 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2717 /* add in hash table */
2718 h = hash_cached_include(type, filename);
2719 e->hash_next = s1->cached_includes_hash[h];
2720 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2723 static void pragma_parse(TCCState *s1)
2725 int val;
2727 next();
2728 if (tok == TOK_pack) {
2730 This may be:
2731 #pragma pack(1) // set
2732 #pragma pack() // reset to default
2733 #pragma pack(push,1) // push & set
2734 #pragma pack(pop) // restore previous
2736 next();
2737 skip('(');
2738 if (tok == TOK_ASM_pop) {
2739 next();
2740 if (s1->pack_stack_ptr <= s1->pack_stack) {
2741 stk_error:
2742 error("out of pack stack");
2744 s1->pack_stack_ptr--;
2745 } else {
2746 val = 0;
2747 if (tok != ')') {
2748 if (tok == TOK_ASM_push) {
2749 next();
2750 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2751 goto stk_error;
2752 s1->pack_stack_ptr++;
2753 skip(',');
2755 if (tok != TOK_CINT) {
2756 pack_error:
2757 error("invalid pack pragma");
2759 val = tokc.i;
2760 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2761 goto pack_error;
2762 next();
2764 *s1->pack_stack_ptr = val;
2765 skip(')');
2770 /* is_bof is true if first non space token at beginning of file */
2771 static void preprocess(int is_bof)
2773 TCCState *s1 = tcc_state;
2774 int size, i, c, n, saved_parse_flags;
2775 char buf[1024], *q, *p;
2776 char buf1[1024];
2777 BufferedFile *f;
2778 Sym *s;
2779 CachedInclude *e;
2781 saved_parse_flags = parse_flags;
2782 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2783 PARSE_FLAG_LINEFEED;
2784 next_nomacro();
2785 redo:
2786 switch(tok) {
2787 case TOK_DEFINE:
2788 next_nomacro();
2789 parse_define();
2790 break;
2791 case TOK_UNDEF:
2792 next_nomacro();
2793 s = define_find(tok);
2794 /* undefine symbol by putting an invalid name */
2795 if (s)
2796 define_undef(s);
2797 break;
2798 case TOK_INCLUDE:
2799 case TOK_INCLUDE_NEXT:
2800 ch = file->buf_ptr[0];
2801 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2802 skip_spaces();
2803 if (ch == '<') {
2804 c = '>';
2805 goto read_name;
2806 } else if (ch == '\"') {
2807 c = ch;
2808 read_name:
2809 /* XXX: better stray handling */
2810 minp();
2811 q = buf;
2812 while (ch != c && ch != '\n' && ch != CH_EOF) {
2813 if ((q - buf) < sizeof(buf) - 1)
2814 *q++ = ch;
2815 minp();
2817 *q = '\0';
2818 minp();
2819 #if 0
2820 /* eat all spaces and comments after include */
2821 /* XXX: slightly incorrect */
2822 while (ch1 != '\n' && ch1 != CH_EOF)
2823 inp();
2824 #endif
2825 } else {
2826 /* computed #include : either we have only strings or
2827 we have anything enclosed in '<>' */
2828 next();
2829 buf[0] = '\0';
2830 if (tok == TOK_STR) {
2831 while (tok != TOK_LINEFEED) {
2832 if (tok != TOK_STR) {
2833 include_syntax:
2834 error("'#include' expects \"FILENAME\" or <FILENAME>");
2836 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2837 next();
2839 c = '\"';
2840 } else {
2841 int len;
2842 while (tok != TOK_LINEFEED) {
2843 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2844 next();
2846 len = strlen(buf);
2847 /* check syntax and remove '<>' */
2848 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2849 goto include_syntax;
2850 memmove(buf, buf + 1, len - 2);
2851 buf[len - 2] = '\0';
2852 c = '>';
2856 e = search_cached_include(s1, c, buf);
2857 if (e && define_find(e->ifndef_macro)) {
2858 /* no need to parse the include because the 'ifndef macro'
2859 is defined */
2860 #ifdef INC_DEBUG
2861 printf("%s: skipping %s\n", file->filename, buf);
2862 #endif
2863 } else {
2864 if (c == '\"') {
2865 /* first search in current dir if "header.h" */
2866 size = 0;
2867 p = strrchr(file->filename, '/');
2868 if (p)
2869 size = p + 1 - file->filename;
2870 if (size > sizeof(buf1) - 1)
2871 size = sizeof(buf1) - 1;
2872 memcpy(buf1, file->filename, size);
2873 buf1[size] = '\0';
2874 pstrcat(buf1, sizeof(buf1), buf);
2875 f = tcc_open(s1, buf1);
2876 if (f) {
2877 if (tok == TOK_INCLUDE_NEXT)
2878 tok = TOK_INCLUDE;
2879 else
2880 goto found;
2883 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2884 error("#include recursion too deep");
2885 /* now search in all the include paths */
2886 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2887 for(i = 0; i < n; i++) {
2888 const char *path;
2889 if (i < s1->nb_include_paths)
2890 path = s1->include_paths[i];
2891 else
2892 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2893 pstrcpy(buf1, sizeof(buf1), path);
2894 pstrcat(buf1, sizeof(buf1), "/");
2895 pstrcat(buf1, sizeof(buf1), buf);
2896 f = tcc_open(s1, buf1);
2897 if (f) {
2898 if (tok == TOK_INCLUDE_NEXT)
2899 tok = TOK_INCLUDE;
2900 else
2901 goto found;
2904 error("include file '%s' not found", buf);
2905 f = NULL;
2906 found:
2907 #ifdef INC_DEBUG
2908 printf("%s: including %s\n", file->filename, buf1);
2909 #endif
2910 f->inc_type = c;
2911 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2912 /* push current file in stack */
2913 /* XXX: fix current line init */
2914 *s1->include_stack_ptr++ = file;
2915 file = f;
2916 /* add include file debug info */
2917 if (do_debug) {
2918 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2920 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2921 ch = file->buf_ptr[0];
2922 goto the_end;
2924 break;
2925 case TOK_IFNDEF:
2926 c = 1;
2927 goto do_ifdef;
2928 case TOK_IF:
2929 c = expr_preprocess();
2930 goto do_if;
2931 case TOK_IFDEF:
2932 c = 0;
2933 do_ifdef:
2934 next_nomacro();
2935 if (tok < TOK_IDENT)
2936 error("invalid argument for '#if%sdef'", c ? "n" : "");
2937 if (is_bof) {
2938 if (c) {
2939 #ifdef INC_DEBUG
2940 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2941 #endif
2942 file->ifndef_macro = tok;
2945 c = (define_find(tok) != 0) ^ c;
2946 do_if:
2947 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2948 error("memory full");
2949 *s1->ifdef_stack_ptr++ = c;
2950 goto test_skip;
2951 case TOK_ELSE:
2952 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2953 error("#else without matching #if");
2954 if (s1->ifdef_stack_ptr[-1] & 2)
2955 error("#else after #else");
2956 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2957 goto test_skip;
2958 case TOK_ELIF:
2959 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2960 error("#elif without matching #if");
2961 c = s1->ifdef_stack_ptr[-1];
2962 if (c > 1)
2963 error("#elif after #else");
2964 /* last #if/#elif expression was true: we skip */
2965 if (c == 1)
2966 goto skip;
2967 c = expr_preprocess();
2968 s1->ifdef_stack_ptr[-1] = c;
2969 test_skip:
2970 if (!(c & 1)) {
2971 skip:
2972 preprocess_skip();
2973 is_bof = 0;
2974 goto redo;
2976 break;
2977 case TOK_ENDIF:
2978 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2979 error("#endif without matching #if");
2980 s1->ifdef_stack_ptr--;
2981 /* '#ifndef macro' was at the start of file. Now we check if
2982 an '#endif' is exactly at the end of file */
2983 if (file->ifndef_macro &&
2984 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2985 file->ifndef_macro_saved = file->ifndef_macro;
2986 /* need to set to zero to avoid false matches if another
2987 #ifndef at middle of file */
2988 file->ifndef_macro = 0;
2989 while (tok != TOK_LINEFEED)
2990 next_nomacro();
2991 tok_flags |= TOK_FLAG_ENDIF;
2992 goto the_end;
2994 break;
2995 case TOK_LINE:
2996 next();
2997 if (tok != TOK_CINT)
2998 error("#line");
2999 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3000 next();
3001 if (tok != TOK_LINEFEED) {
3002 if (tok != TOK_STR)
3003 error("#line");
3004 pstrcpy(file->filename, sizeof(file->filename),
3005 (char *)tokc.cstr->data);
3007 break;
3008 case TOK_ERROR:
3009 case TOK_WARNING:
3010 c = tok;
3011 ch = file->buf_ptr[0];
3012 skip_spaces();
3013 q = buf;
3014 while (ch != '\n' && ch != CH_EOF) {
3015 if ((q - buf) < sizeof(buf) - 1)
3016 *q++ = ch;
3017 minp();
3019 *q = '\0';
3020 if (c == TOK_ERROR)
3021 error("#error %s", buf);
3022 else
3023 warning("#warning %s", buf);
3024 break;
3025 case TOK_PRAGMA:
3026 pragma_parse(s1);
3027 break;
3028 default:
3029 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3030 /* '!' is ignored to allow C scripts. numbers are ignored
3031 to emulate cpp behaviour */
3032 } else {
3033 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3034 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
3036 break;
3038 /* ignore other preprocess commands or #! for C scripts */
3039 while (tok != TOK_LINEFEED)
3040 next_nomacro();
3041 the_end:
3042 parse_flags = saved_parse_flags;
3045 /* evaluate escape codes in a string. */
3046 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3048 int c, n;
3049 const uint8_t *p;
3051 p = buf;
3052 for(;;) {
3053 c = *p;
3054 if (c == '\0')
3055 break;
3056 if (c == '\\') {
3057 p++;
3058 /* escape */
3059 c = *p;
3060 switch(c) {
3061 case '0': case '1': case '2': case '3':
3062 case '4': case '5': case '6': case '7':
3063 /* at most three octal digits */
3064 n = c - '0';
3065 p++;
3066 c = *p;
3067 if (isoct(c)) {
3068 n = n * 8 + c - '0';
3069 p++;
3070 c = *p;
3071 if (isoct(c)) {
3072 n = n * 8 + c - '0';
3073 p++;
3076 c = n;
3077 goto add_char_nonext;
3078 case 'x':
3079 p++;
3080 n = 0;
3081 for(;;) {
3082 c = *p;
3083 if (c >= 'a' && c <= 'f')
3084 c = c - 'a' + 10;
3085 else if (c >= 'A' && c <= 'F')
3086 c = c - 'A' + 10;
3087 else if (isnum(c))
3088 c = c - '0';
3089 else
3090 break;
3091 n = n * 16 + c;
3092 p++;
3094 c = n;
3095 goto add_char_nonext;
3096 case 'a':
3097 c = '\a';
3098 break;
3099 case 'b':
3100 c = '\b';
3101 break;
3102 case 'f':
3103 c = '\f';
3104 break;
3105 case 'n':
3106 c = '\n';
3107 break;
3108 case 'r':
3109 c = '\r';
3110 break;
3111 case 't':
3112 c = '\t';
3113 break;
3114 case 'v':
3115 c = '\v';
3116 break;
3117 case 'e':
3118 if (!gnu_ext)
3119 goto invalid_escape;
3120 c = 27;
3121 break;
3122 case '\'':
3123 case '\"':
3124 case '\\':
3125 case '?':
3126 break;
3127 default:
3128 invalid_escape:
3129 if (c >= '!' && c <= '~')
3130 warning("unknown escape sequence: \'\\%c\'", c);
3131 else
3132 warning("unknown escape sequence: \'\\x%x\'", c);
3133 break;
3136 p++;
3137 add_char_nonext:
3138 if (!is_long)
3139 cstr_ccat(outstr, c);
3140 else
3141 cstr_wccat(outstr, c);
3143 /* add a trailing '\0' */
3144 if (!is_long)
3145 cstr_ccat(outstr, '\0');
3146 else
3147 cstr_wccat(outstr, '\0');
3150 /* we use 64 bit numbers */
3151 #define BN_SIZE 2
3153 /* bn = (bn << shift) | or_val */
3154 void bn_lshift(unsigned int *bn, int shift, int or_val)
3156 int i;
3157 unsigned int v;
3158 for(i=0;i<BN_SIZE;i++) {
3159 v = bn[i];
3160 bn[i] = (v << shift) | or_val;
3161 or_val = v >> (32 - shift);
3165 void bn_zero(unsigned int *bn)
3167 int i;
3168 for(i=0;i<BN_SIZE;i++) {
3169 bn[i] = 0;
3173 /* parse number in null terminated string 'p' and return it in the
3174 current token */
3175 void parse_number(const char *p)
3177 int b, t, shift, frac_bits, s, exp_val, ch;
3178 char *q;
3179 unsigned int bn[BN_SIZE];
3180 double d;
3182 /* number */
3183 q = token_buf;
3184 ch = *p++;
3185 t = ch;
3186 ch = *p++;
3187 *q++ = t;
3188 b = 10;
3189 if (t == '.') {
3190 goto float_frac_parse;
3191 } else if (t == '0') {
3192 if (ch == 'x' || ch == 'X') {
3193 q--;
3194 ch = *p++;
3195 b = 16;
3196 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3197 q--;
3198 ch = *p++;
3199 b = 2;
3202 /* parse all digits. cannot check octal numbers at this stage
3203 because of floating point constants */
3204 while (1) {
3205 if (ch >= 'a' && ch <= 'f')
3206 t = ch - 'a' + 10;
3207 else if (ch >= 'A' && ch <= 'F')
3208 t = ch - 'A' + 10;
3209 else if (isnum(ch))
3210 t = ch - '0';
3211 else
3212 break;
3213 if (t >= b)
3214 break;
3215 if (q >= token_buf + STRING_MAX_SIZE) {
3216 num_too_long:
3217 error("number too long");
3219 *q++ = ch;
3220 ch = *p++;
3222 if (ch == '.' ||
3223 ((ch == 'e' || ch == 'E') && b == 10) ||
3224 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3225 if (b != 10) {
3226 /* NOTE: strtox should support that for hexa numbers, but
3227 non ISOC99 libcs do not support it, so we prefer to do
3228 it by hand */
3229 /* hexadecimal or binary floats */
3230 /* XXX: handle overflows */
3231 *q = '\0';
3232 if (b == 16)
3233 shift = 4;
3234 else
3235 shift = 2;
3236 bn_zero(bn);
3237 q = token_buf;
3238 while (1) {
3239 t = *q++;
3240 if (t == '\0') {
3241 break;
3242 } else if (t >= 'a') {
3243 t = t - 'a' + 10;
3244 } else if (t >= 'A') {
3245 t = t - 'A' + 10;
3246 } else {
3247 t = t - '0';
3249 bn_lshift(bn, shift, t);
3251 frac_bits = 0;
3252 if (ch == '.') {
3253 ch = *p++;
3254 while (1) {
3255 t = ch;
3256 if (t >= 'a' && t <= 'f') {
3257 t = t - 'a' + 10;
3258 } else if (t >= 'A' && t <= 'F') {
3259 t = t - 'A' + 10;
3260 } else if (t >= '0' && t <= '9') {
3261 t = t - '0';
3262 } else {
3263 break;
3265 if (t >= b)
3266 error("invalid digit");
3267 bn_lshift(bn, shift, t);
3268 frac_bits += shift;
3269 ch = *p++;
3272 if (ch != 'p' && ch != 'P')
3273 expect("exponent");
3274 ch = *p++;
3275 s = 1;
3276 exp_val = 0;
3277 if (ch == '+') {
3278 ch = *p++;
3279 } else if (ch == '-') {
3280 s = -1;
3281 ch = *p++;
3283 if (ch < '0' || ch > '9')
3284 expect("exponent digits");
3285 while (ch >= '0' && ch <= '9') {
3286 exp_val = exp_val * 10 + ch - '0';
3287 ch = *p++;
3289 exp_val = exp_val * s;
3291 /* now we can generate the number */
3292 /* XXX: should patch directly float number */
3293 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3294 d = ldexp(d, exp_val - frac_bits);
3295 t = toup(ch);
3296 if (t == 'F') {
3297 ch = *p++;
3298 tok = TOK_CFLOAT;
3299 /* float : should handle overflow */
3300 tokc.f = (float)d;
3301 } else if (t == 'L') {
3302 ch = *p++;
3303 tok = TOK_CLDOUBLE;
3304 /* XXX: not large enough */
3305 tokc.ld = (long double)d;
3306 } else {
3307 tok = TOK_CDOUBLE;
3308 tokc.d = d;
3310 } else {
3311 /* decimal floats */
3312 if (ch == '.') {
3313 if (q >= token_buf + STRING_MAX_SIZE)
3314 goto num_too_long;
3315 *q++ = ch;
3316 ch = *p++;
3317 float_frac_parse:
3318 while (ch >= '0' && ch <= '9') {
3319 if (q >= token_buf + STRING_MAX_SIZE)
3320 goto num_too_long;
3321 *q++ = ch;
3322 ch = *p++;
3325 if (ch == 'e' || ch == 'E') {
3326 if (q >= token_buf + STRING_MAX_SIZE)
3327 goto num_too_long;
3328 *q++ = ch;
3329 ch = *p++;
3330 if (ch == '-' || ch == '+') {
3331 if (q >= token_buf + STRING_MAX_SIZE)
3332 goto num_too_long;
3333 *q++ = ch;
3334 ch = *p++;
3336 if (ch < '0' || ch > '9')
3337 expect("exponent digits");
3338 while (ch >= '0' && ch <= '9') {
3339 if (q >= token_buf + STRING_MAX_SIZE)
3340 goto num_too_long;
3341 *q++ = ch;
3342 ch = *p++;
3345 *q = '\0';
3346 t = toup(ch);
3347 errno = 0;
3348 if (t == 'F') {
3349 ch = *p++;
3350 tok = TOK_CFLOAT;
3351 tokc.f = strtof(token_buf, NULL);
3352 } else if (t == 'L') {
3353 ch = *p++;
3354 tok = TOK_CLDOUBLE;
3355 tokc.ld = strtold(token_buf, NULL);
3356 } else {
3357 tok = TOK_CDOUBLE;
3358 tokc.d = strtod(token_buf, NULL);
3361 } else {
3362 unsigned long long n, n1;
3363 int lcount, ucount;
3365 /* integer number */
3366 *q = '\0';
3367 q = token_buf;
3368 if (b == 10 && *q == '0') {
3369 b = 8;
3370 q++;
3372 n = 0;
3373 while(1) {
3374 t = *q++;
3375 /* no need for checks except for base 10 / 8 errors */
3376 if (t == '\0') {
3377 break;
3378 } else if (t >= 'a') {
3379 t = t - 'a' + 10;
3380 } else if (t >= 'A') {
3381 t = t - 'A' + 10;
3382 } else {
3383 t = t - '0';
3384 if (t >= b)
3385 error("invalid digit");
3387 n1 = n;
3388 n = n * b + t;
3389 /* detect overflow */
3390 /* XXX: this test is not reliable */
3391 if (n < n1)
3392 error("integer constant overflow");
3395 /* XXX: not exactly ANSI compliant */
3396 if ((n & 0xffffffff00000000LL) != 0) {
3397 if ((n >> 63) != 0)
3398 tok = TOK_CULLONG;
3399 else
3400 tok = TOK_CLLONG;
3401 } else if (n > 0x7fffffff) {
3402 tok = TOK_CUINT;
3403 } else {
3404 tok = TOK_CINT;
3406 lcount = 0;
3407 ucount = 0;
3408 for(;;) {
3409 t = toup(ch);
3410 if (t == 'L') {
3411 if (lcount >= 2)
3412 error("three 'l's in integer constant");
3413 lcount++;
3414 if (lcount == 2) {
3415 if (tok == TOK_CINT)
3416 tok = TOK_CLLONG;
3417 else if (tok == TOK_CUINT)
3418 tok = TOK_CULLONG;
3420 ch = *p++;
3421 } else if (t == 'U') {
3422 if (ucount >= 1)
3423 error("two 'u's in integer constant");
3424 ucount++;
3425 if (tok == TOK_CINT)
3426 tok = TOK_CUINT;
3427 else if (tok == TOK_CLLONG)
3428 tok = TOK_CULLONG;
3429 ch = *p++;
3430 } else {
3431 break;
3434 if (tok == TOK_CINT || tok == TOK_CUINT)
3435 tokc.ui = n;
3436 else
3437 tokc.ull = n;
3442 #define PARSE2(c1, tok1, c2, tok2) \
3443 case c1: \
3444 PEEKC(c, p); \
3445 if (c == c2) { \
3446 p++; \
3447 tok = tok2; \
3448 } else { \
3449 tok = tok1; \
3451 break;
3453 /* return next token without macro substitution */
3454 static inline void next_nomacro1(void)
3456 int t, c, is_long;
3457 TokenSym *ts;
3458 uint8_t *p, *p1;
3459 unsigned int h;
3461 p = file->buf_ptr;
3462 redo_no_start:
3463 c = *p;
3464 switch(c) {
3465 case ' ':
3466 case '\t':
3467 case '\f':
3468 case '\v':
3469 case '\r':
3470 p++;
3471 goto redo_no_start;
3473 case '\\':
3474 /* first look if it is in fact an end of buffer */
3475 if (p >= file->buf_end) {
3476 file->buf_ptr = p;
3477 handle_eob();
3478 p = file->buf_ptr;
3479 if (p >= file->buf_end)
3480 goto parse_eof;
3481 else
3482 goto redo_no_start;
3483 } else {
3484 file->buf_ptr = p;
3485 ch = *p;
3486 handle_stray();
3487 p = file->buf_ptr;
3488 goto redo_no_start;
3490 parse_eof:
3492 TCCState *s1 = tcc_state;
3493 if (parse_flags & PARSE_FLAG_LINEFEED) {
3494 tok = TOK_LINEFEED;
3495 } else if (s1->include_stack_ptr == s1->include_stack ||
3496 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3497 /* no include left : end of file. */
3498 tok = TOK_EOF;
3499 } else {
3500 /* pop include file */
3502 /* test if previous '#endif' was after a #ifdef at
3503 start of file */
3504 if (tok_flags & TOK_FLAG_ENDIF) {
3505 #ifdef INC_DEBUG
3506 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3507 #endif
3508 add_cached_include(s1, file->inc_type, file->inc_filename,
3509 file->ifndef_macro_saved);
3512 /* add end of include file debug info */
3513 if (do_debug) {
3514 put_stabd(N_EINCL, 0, 0);
3516 /* pop include stack */
3517 tcc_close(file);
3518 s1->include_stack_ptr--;
3519 file = *s1->include_stack_ptr;
3520 p = file->buf_ptr;
3521 goto redo_no_start;
3524 break;
3526 case '\n':
3527 if (parse_flags & PARSE_FLAG_LINEFEED) {
3528 tok = TOK_LINEFEED;
3529 } else {
3530 file->line_num++;
3531 tok_flags |= TOK_FLAG_BOL;
3532 p++;
3533 goto redo_no_start;
3535 break;
3537 case '#':
3538 /* XXX: simplify */
3539 PEEKC(c, p);
3540 if ((tok_flags & TOK_FLAG_BOL) &&
3541 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3542 file->buf_ptr = p;
3543 preprocess(tok_flags & TOK_FLAG_BOF);
3544 p = file->buf_ptr;
3545 goto redo_no_start;
3546 } else {
3547 if (c == '#') {
3548 p++;
3549 tok = TOK_TWOSHARPS;
3550 } else {
3551 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3552 p = parse_line_comment(p - 1);
3553 goto redo_no_start;
3554 } else {
3555 tok = '#';
3559 break;
3561 case 'a': case 'b': case 'c': case 'd':
3562 case 'e': case 'f': case 'g': case 'h':
3563 case 'i': case 'j': case 'k': case 'l':
3564 case 'm': case 'n': case 'o': case 'p':
3565 case 'q': case 'r': case 's': case 't':
3566 case 'u': case 'v': case 'w': case 'x':
3567 case 'y': case 'z':
3568 case 'A': case 'B': case 'C': case 'D':
3569 case 'E': case 'F': case 'G': case 'H':
3570 case 'I': case 'J': case 'K':
3571 case 'M': case 'N': case 'O': case 'P':
3572 case 'Q': case 'R': case 'S': case 'T':
3573 case 'U': case 'V': case 'W': case 'X':
3574 case 'Y': case 'Z':
3575 case '_':
3576 parse_ident_fast:
3577 p1 = p;
3578 h = TOK_HASH_INIT;
3579 h = TOK_HASH_FUNC(h, c);
3580 p++;
3581 for(;;) {
3582 c = *p;
3583 if (!isidnum_table[c])
3584 break;
3585 h = TOK_HASH_FUNC(h, c);
3586 p++;
3588 if (c != '\\') {
3589 TokenSym **pts;
3590 int len;
3592 /* fast case : no stray found, so we have the full token
3593 and we have already hashed it */
3594 len = p - p1;
3595 h &= (TOK_HASH_SIZE - 1);
3596 pts = &hash_ident[h];
3597 for(;;) {
3598 ts = *pts;
3599 if (!ts)
3600 break;
3601 if (ts->len == len && !memcmp(ts->str, p1, len))
3602 goto token_found;
3603 pts = &(ts->hash_next);
3605 ts = tok_alloc_new(pts, p1, len);
3606 token_found: ;
3607 } else {
3608 /* slower case */
3609 cstr_reset(&tokcstr);
3611 while (p1 < p) {
3612 cstr_ccat(&tokcstr, *p1);
3613 p1++;
3615 p--;
3616 PEEKC(c, p);
3617 parse_ident_slow:
3618 while (isidnum_table[c]) {
3619 cstr_ccat(&tokcstr, c);
3620 PEEKC(c, p);
3622 ts = tok_alloc(tokcstr.data, tokcstr.size);
3624 tok = ts->tok;
3625 break;
3626 case 'L':
3627 t = p[1];
3628 if (t != '\\' && t != '\'' && t != '\"') {
3629 /* fast case */
3630 goto parse_ident_fast;
3631 } else {
3632 PEEKC(c, p);
3633 if (c == '\'' || c == '\"') {
3634 is_long = 1;
3635 goto str_const;
3636 } else {
3637 cstr_reset(&tokcstr);
3638 cstr_ccat(&tokcstr, 'L');
3639 goto parse_ident_slow;
3642 break;
3643 case '0': case '1': case '2': case '3':
3644 case '4': case '5': case '6': case '7':
3645 case '8': case '9':
3647 cstr_reset(&tokcstr);
3648 /* after the first digit, accept digits, alpha, '.' or sign if
3649 prefixed by 'eEpP' */
3650 parse_num:
3651 for(;;) {
3652 t = c;
3653 cstr_ccat(&tokcstr, c);
3654 PEEKC(c, p);
3655 if (!(isnum(c) || isid(c) || c == '.' ||
3656 ((c == '+' || c == '-') &&
3657 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3658 break;
3660 /* We add a trailing '\0' to ease parsing */
3661 cstr_ccat(&tokcstr, '\0');
3662 tokc.cstr = &tokcstr;
3663 tok = TOK_PPNUM;
3664 break;
3665 case '.':
3666 /* special dot handling because it can also start a number */
3667 PEEKC(c, p);
3668 if (isnum(c)) {
3669 cstr_reset(&tokcstr);
3670 cstr_ccat(&tokcstr, '.');
3671 goto parse_num;
3672 } else if (c == '.') {
3673 PEEKC(c, p);
3674 if (c != '.')
3675 expect("'.'");
3676 PEEKC(c, p);
3677 tok = TOK_DOTS;
3678 } else {
3679 tok = '.';
3681 break;
3682 case '\'':
3683 case '\"':
3684 is_long = 0;
3685 str_const:
3687 CString str;
3688 int sep;
3690 sep = c;
3692 /* parse the string */
3693 cstr_new(&str);
3694 p = parse_pp_string(p, sep, &str);
3695 cstr_ccat(&str, '\0');
3697 /* eval the escape (should be done as TOK_PPNUM) */
3698 cstr_reset(&tokcstr);
3699 parse_escape_string(&tokcstr, str.data, is_long);
3700 cstr_free(&str);
3702 if (sep == '\'') {
3703 int char_size;
3704 /* XXX: make it portable */
3705 if (!is_long)
3706 char_size = 1;
3707 else
3708 char_size = sizeof(int);
3709 if (tokcstr.size <= char_size)
3710 error("empty character constant");
3711 if (tokcstr.size > 2 * char_size)
3712 warning("multi-character character constant");
3713 if (!is_long) {
3714 tokc.i = *(int8_t *)tokcstr.data;
3715 tok = TOK_CCHAR;
3716 } else {
3717 tokc.i = *(int *)tokcstr.data;
3718 tok = TOK_LCHAR;
3720 } else {
3721 tokc.cstr = &tokcstr;
3722 if (!is_long)
3723 tok = TOK_STR;
3724 else
3725 tok = TOK_LSTR;
3728 break;
3730 case '<':
3731 PEEKC(c, p);
3732 if (c == '=') {
3733 p++;
3734 tok = TOK_LE;
3735 } else if (c == '<') {
3736 PEEKC(c, p);
3737 if (c == '=') {
3738 p++;
3739 tok = TOK_A_SHL;
3740 } else {
3741 tok = TOK_SHL;
3743 } else {
3744 tok = TOK_LT;
3746 break;
3748 case '>':
3749 PEEKC(c, p);
3750 if (c == '=') {
3751 p++;
3752 tok = TOK_GE;
3753 } else if (c == '>') {
3754 PEEKC(c, p);
3755 if (c == '=') {
3756 p++;
3757 tok = TOK_A_SAR;
3758 } else {
3759 tok = TOK_SAR;
3761 } else {
3762 tok = TOK_GT;
3764 break;
3766 case '&':
3767 PEEKC(c, p);
3768 if (c == '&') {
3769 p++;
3770 tok = TOK_LAND;
3771 } else if (c == '=') {
3772 p++;
3773 tok = TOK_A_AND;
3774 } else {
3775 tok = '&';
3777 break;
3779 case '|':
3780 PEEKC(c, p);
3781 if (c == '|') {
3782 p++;
3783 tok = TOK_LOR;
3784 } else if (c == '=') {
3785 p++;
3786 tok = TOK_A_OR;
3787 } else {
3788 tok = '|';
3790 break;
3792 case '+':
3793 PEEKC(c, p);
3794 if (c == '+') {
3795 p++;
3796 tok = TOK_INC;
3797 } else if (c == '=') {
3798 p++;
3799 tok = TOK_A_ADD;
3800 } else {
3801 tok = '+';
3803 break;
3805 case '-':
3806 PEEKC(c, p);
3807 if (c == '-') {
3808 p++;
3809 tok = TOK_DEC;
3810 } else if (c == '=') {
3811 p++;
3812 tok = TOK_A_SUB;
3813 } else if (c == '>') {
3814 p++;
3815 tok = TOK_ARROW;
3816 } else {
3817 tok = '-';
3819 break;
3821 PARSE2('!', '!', '=', TOK_NE)
3822 PARSE2('=', '=', '=', TOK_EQ)
3823 PARSE2('*', '*', '=', TOK_A_MUL)
3824 PARSE2('%', '%', '=', TOK_A_MOD)
3825 PARSE2('^', '^', '=', TOK_A_XOR)
3827 /* comments or operator */
3828 case '/':
3829 PEEKC(c, p);
3830 if (c == '*') {
3831 p = parse_comment(p);
3832 goto redo_no_start;
3833 } else if (c == '/') {
3834 p = parse_line_comment(p);
3835 goto redo_no_start;
3836 } else if (c == '=') {
3837 p++;
3838 tok = TOK_A_DIV;
3839 } else {
3840 tok = '/';
3842 break;
3844 /* simple tokens */
3845 case '(':
3846 case ')':
3847 case '[':
3848 case ']':
3849 case '{':
3850 case '}':
3851 case ',':
3852 case ';':
3853 case ':':
3854 case '?':
3855 case '~':
3856 case '$': /* only used in assembler */
3857 tok = c;
3858 p++;
3859 break;
3860 default:
3861 error("unrecognized character \\x%02x", c);
3862 break;
3864 file->buf_ptr = p;
3865 tok_flags = 0;
3866 #if defined(PARSE_DEBUG)
3867 printf("token = %s\n", get_tok_str(tok, &tokc));
3868 #endif
3871 /* return next token without macro substitution. Can read input from
3872 macro_ptr buffer */
3873 static void next_nomacro(void)
3875 if (macro_ptr) {
3876 redo:
3877 tok = *macro_ptr;
3878 if (tok) {
3879 TOK_GET(tok, macro_ptr, tokc);
3880 if (tok == TOK_LINENUM) {
3881 file->line_num = tokc.i;
3882 goto redo;
3885 } else {
3886 next_nomacro1();
3890 /* substitute args in macro_str and return allocated string */
3891 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3893 int *st, last_tok, t, notfirst;
3894 Sym *s;
3895 CValue cval;
3896 TokenString str;
3897 CString cstr;
3899 tok_str_new(&str);
3900 last_tok = 0;
3901 while(1) {
3902 TOK_GET(t, macro_str, cval);
3903 if (!t)
3904 break;
3905 if (t == '#') {
3906 /* stringize */
3907 TOK_GET(t, macro_str, cval);
3908 if (!t)
3909 break;
3910 s = sym_find2(args, t);
3911 if (s) {
3912 cstr_new(&cstr);
3913 st = (int *)s->c;
3914 notfirst = 0;
3915 while (*st) {
3916 if (notfirst)
3917 cstr_ccat(&cstr, ' ');
3918 TOK_GET(t, st, cval);
3919 cstr_cat(&cstr, get_tok_str(t, &cval));
3920 notfirst = 1;
3922 cstr_ccat(&cstr, '\0');
3923 #ifdef PP_DEBUG
3924 printf("stringize: %s\n", (char *)cstr.data);
3925 #endif
3926 /* add string */
3927 cval.cstr = &cstr;
3928 tok_str_add2(&str, TOK_STR, &cval);
3929 cstr_free(&cstr);
3930 } else {
3931 tok_str_add2(&str, t, &cval);
3933 } else if (t >= TOK_IDENT) {
3934 s = sym_find2(args, t);
3935 if (s) {
3936 st = (int *)s->c;
3937 /* if '##' is present before or after, no arg substitution */
3938 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3939 /* special case for var arg macros : ## eats the
3940 ',' if empty VA_ARGS variable. */
3941 /* XXX: test of the ',' is not 100%
3942 reliable. should fix it to avoid security
3943 problems */
3944 if (gnu_ext && s->type.t &&
3945 last_tok == TOK_TWOSHARPS &&
3946 str.len >= 2 && str.str[str.len - 2] == ',') {
3947 if (*st == 0) {
3948 /* suppress ',' '##' */
3949 str.len -= 2;
3950 } else {
3951 /* suppress '##' and add variable */
3952 str.len--;
3953 goto add_var;
3955 } else {
3956 int t1;
3957 add_var:
3958 for(;;) {
3959 TOK_GET(t1, st, cval);
3960 if (!t1)
3961 break;
3962 tok_str_add2(&str, t1, &cval);
3965 } else {
3966 /* NOTE: the stream cannot be read when macro
3967 substituing an argument */
3968 macro_subst(&str, nested_list, st, 0);
3970 } else {
3971 tok_str_add(&str, t);
3973 } else {
3974 tok_str_add2(&str, t, &cval);
3976 last_tok = t;
3978 tok_str_add(&str, 0);
3979 return str.str;
3982 static char const ab_month_name[12][4] =
3984 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3985 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3988 /* do macro substitution of current token with macro 's' and add
3989 result to (tok_str,tok_len). 'nested_list' is the list of all
3990 macros we got inside to avoid recursing. Return non zero if no
3991 substitution needs to be done */
3992 static int macro_subst_tok(TokenString *tok_str,
3993 Sym **nested_list, Sym *s, int can_read_stream)
3995 Sym *args, *sa, *sa1;
3996 int mstr_allocated, parlevel, *mstr, t, t1;
3997 TokenString str;
3998 char *cstrval;
3999 CValue cval;
4000 CString cstr;
4001 char buf[32];
4003 /* if symbol is a macro, prepare substitution */
4004 /* special macros */
4005 if (tok == TOK___LINE__) {
4006 snprintf(buf, sizeof(buf), "%d", file->line_num);
4007 cstrval = buf;
4008 t1 = TOK_PPNUM;
4009 goto add_cstr1;
4010 } else if (tok == TOK___FILE__) {
4011 cstrval = file->filename;
4012 goto add_cstr;
4013 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4014 time_t ti;
4015 struct tm *tm;
4017 time(&ti);
4018 tm = localtime(&ti);
4019 if (tok == TOK___DATE__) {
4020 snprintf(buf, sizeof(buf), "%s %2d %d",
4021 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4022 } else {
4023 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4024 tm->tm_hour, tm->tm_min, tm->tm_sec);
4026 cstrval = buf;
4027 add_cstr:
4028 t1 = TOK_STR;
4029 add_cstr1:
4030 cstr_new(&cstr);
4031 cstr_cat(&cstr, cstrval);
4032 cstr_ccat(&cstr, '\0');
4033 cval.cstr = &cstr;
4034 tok_str_add2(tok_str, t1, &cval);
4035 cstr_free(&cstr);
4036 } else {
4037 mstr = (int *)s->c;
4038 mstr_allocated = 0;
4039 if (s->type.t == MACRO_FUNC) {
4040 /* NOTE: we do not use next_nomacro to avoid eating the
4041 next token. XXX: find better solution */
4042 if (macro_ptr) {
4043 t = *macro_ptr;
4044 if (t == 0 && can_read_stream) {
4045 /* end of macro stream: we must look at the token
4046 after in the file */
4047 macro_ptr = NULL;
4048 goto parse_stream;
4050 } else {
4051 parse_stream:
4052 /* XXX: incorrect with comments */
4053 ch = file->buf_ptr[0];
4054 while (is_space(ch) || ch == '\n')
4055 cinp();
4056 t = ch;
4058 if (t != '(') /* no macro subst */
4059 return -1;
4061 /* argument macro */
4062 next_nomacro();
4063 next_nomacro();
4064 args = NULL;
4065 sa = s->next;
4066 /* NOTE: empty args are allowed, except if no args */
4067 for(;;) {
4068 /* handle '()' case */
4069 if (!args && !sa && tok == ')')
4070 break;
4071 if (!sa)
4072 error("macro '%s' used with too many args",
4073 get_tok_str(s->v, 0));
4074 tok_str_new(&str);
4075 parlevel = 0;
4076 /* NOTE: non zero sa->t indicates VA_ARGS */
4077 while ((parlevel > 0 ||
4078 (tok != ')' &&
4079 (tok != ',' || sa->type.t))) &&
4080 tok != -1) {
4081 if (tok == '(')
4082 parlevel++;
4083 else if (tok == ')')
4084 parlevel--;
4085 tok_str_add2(&str, tok, &tokc);
4086 next_nomacro();
4088 tok_str_add(&str, 0);
4089 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
4090 sa = sa->next;
4091 if (tok == ')') {
4092 /* special case for gcc var args: add an empty
4093 var arg argument if it is omitted */
4094 if (sa && sa->type.t && gnu_ext)
4095 continue;
4096 else
4097 break;
4099 if (tok != ',')
4100 expect(",");
4101 next_nomacro();
4103 if (sa) {
4104 error("macro '%s' used with too few args",
4105 get_tok_str(s->v, 0));
4108 /* now subst each arg */
4109 mstr = macro_arg_subst(nested_list, mstr, args);
4110 /* free memory */
4111 sa = args;
4112 while (sa) {
4113 sa1 = sa->prev;
4114 tok_str_free((int *)sa->c);
4115 sym_free(sa);
4116 sa = sa1;
4118 mstr_allocated = 1;
4120 sym_push2(nested_list, s->v, 0, 0);
4121 macro_subst(tok_str, nested_list, mstr, 1);
4122 /* pop nested defined symbol */
4123 sa1 = *nested_list;
4124 *nested_list = sa1->prev;
4125 sym_free(sa1);
4126 if (mstr_allocated)
4127 tok_str_free(mstr);
4129 return 0;
4132 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4133 return the resulting string (which must be freed). */
4134 static inline int *macro_twosharps(const int *macro_str)
4136 TokenSym *ts;
4137 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4138 int t;
4139 const char *p1, *p2;
4140 CValue cval;
4141 TokenString macro_str1;
4142 CString cstr;
4144 start_macro_ptr = macro_str;
4145 /* we search the first '##' */
4146 for(;;) {
4147 macro_ptr1 = macro_str;
4148 TOK_GET(t, macro_str, cval);
4149 /* nothing more to do if end of string */
4150 if (t == 0)
4151 return NULL;
4152 if (*macro_str == TOK_TWOSHARPS)
4153 break;
4156 /* we saw '##', so we need more processing to handle it */
4157 cstr_new(&cstr);
4158 tok_str_new(&macro_str1);
4159 tok = t;
4160 tokc = cval;
4162 /* add all tokens seen so far */
4163 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4164 TOK_GET(t, ptr, cval);
4165 tok_str_add2(&macro_str1, t, &cval);
4167 saved_macro_ptr = macro_ptr;
4168 /* XXX: get rid of the use of macro_ptr here */
4169 macro_ptr = (int *)macro_str;
4170 for(;;) {
4171 while (*macro_ptr == TOK_TWOSHARPS) {
4172 macro_ptr++;
4173 macro_ptr1 = macro_ptr;
4174 t = *macro_ptr;
4175 if (t) {
4176 TOK_GET(t, macro_ptr, cval);
4177 /* We concatenate the two tokens if we have an
4178 identifier or a preprocessing number */
4179 cstr_reset(&cstr);
4180 p1 = get_tok_str(tok, &tokc);
4181 cstr_cat(&cstr, p1);
4182 p2 = get_tok_str(t, &cval);
4183 cstr_cat(&cstr, p2);
4184 cstr_ccat(&cstr, '\0');
4186 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4187 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4188 if (tok == TOK_PPNUM) {
4189 /* if number, then create a number token */
4190 /* NOTE: no need to allocate because
4191 tok_str_add2() does it */
4192 tokc.cstr = &cstr;
4193 } else {
4194 /* if identifier, we must do a test to
4195 validate we have a correct identifier */
4196 if (t == TOK_PPNUM) {
4197 const char *p;
4198 int c;
4200 p = p2;
4201 for(;;) {
4202 c = *p;
4203 if (c == '\0')
4204 break;
4205 p++;
4206 if (!isnum(c) && !isid(c))
4207 goto error_pasting;
4210 ts = tok_alloc(cstr.data, strlen(cstr.data));
4211 tok = ts->tok; /* modify current token */
4213 } else {
4214 const char *str = cstr.data;
4215 const unsigned char *q;
4217 /* we look for a valid token */
4218 /* XXX: do more extensive checks */
4219 if (!strcmp(str, ">>=")) {
4220 tok = TOK_A_SAR;
4221 } else if (!strcmp(str, "<<=")) {
4222 tok = TOK_A_SHL;
4223 } else if (strlen(str) == 2) {
4224 /* search in two bytes table */
4225 q = tok_two_chars;
4226 for(;;) {
4227 if (!*q)
4228 goto error_pasting;
4229 if (q[0] == str[0] && q[1] == str[1])
4230 break;
4231 q += 3;
4233 tok = q[2];
4234 } else {
4235 error_pasting:
4236 /* NOTE: because get_tok_str use a static buffer,
4237 we must save it */
4238 cstr_reset(&cstr);
4239 p1 = get_tok_str(tok, &tokc);
4240 cstr_cat(&cstr, p1);
4241 cstr_ccat(&cstr, '\0');
4242 p2 = get_tok_str(t, &cval);
4243 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4244 /* cannot merge tokens: just add them separately */
4245 tok_str_add2(&macro_str1, tok, &tokc);
4246 /* XXX: free associated memory ? */
4247 tok = t;
4248 tokc = cval;
4253 tok_str_add2(&macro_str1, tok, &tokc);
4254 next_nomacro();
4255 if (tok == 0)
4256 break;
4258 macro_ptr = (int *)saved_macro_ptr;
4259 cstr_free(&cstr);
4260 tok_str_add(&macro_str1, 0);
4261 return macro_str1.str;
4265 /* do macro substitution of macro_str and add result to
4266 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4267 inside to avoid recursing. */
4268 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4269 const int *macro_str, int can_read_stream)
4271 Sym *s;
4272 int *saved_macro_ptr, *macro_str1;
4273 const int *ptr;
4274 int t, ret;
4275 CValue cval;
4277 /* first scan for '##' operator handling */
4278 ptr = macro_str;
4279 macro_str1 = macro_twosharps(ptr);
4280 if (macro_str1)
4281 ptr = macro_str1;
4282 while (1) {
4283 /* NOTE: ptr == NULL can only happen if tokens are read from
4284 file stream due to a macro function call */
4285 if (ptr == NULL)
4286 break;
4287 TOK_GET(t, ptr, cval);
4288 if (t == 0)
4289 break;
4290 s = define_find(t);
4291 if (s != NULL) {
4292 /* if nested substitution, do nothing */
4293 if (sym_find2(*nested_list, t))
4294 goto no_subst;
4295 saved_macro_ptr = macro_ptr;
4296 macro_ptr = (int *)ptr;
4297 tok = t;
4298 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4299 ptr = (int *)macro_ptr;
4300 macro_ptr = saved_macro_ptr;
4301 if (ret != 0)
4302 goto no_subst;
4303 } else {
4304 no_subst:
4305 tok_str_add2(tok_str, t, &cval);
4308 if (macro_str1)
4309 tok_str_free(macro_str1);
4312 /* return next token with macro substitution */
4313 static void next(void)
4315 Sym *nested_list, *s;
4316 TokenString str;
4318 redo:
4319 next_nomacro();
4320 if (!macro_ptr) {
4321 /* if not reading from macro substituted string, then try
4322 to substitute macros */
4323 if (tok >= TOK_IDENT &&
4324 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4325 s = define_find(tok);
4326 if (s) {
4327 /* we have a macro: we try to substitute */
4328 tok_str_new(&str);
4329 nested_list = NULL;
4330 if (macro_subst_tok(&str, &nested_list, s, 1) == 0) {
4331 /* substitution done, NOTE: maybe empty */
4332 tok_str_add(&str, 0);
4333 macro_ptr = str.str;
4334 macro_ptr_allocated = str.str;
4335 goto redo;
4339 } else {
4340 if (tok == 0) {
4341 /* end of macro or end of unget buffer */
4342 if (unget_buffer_enabled) {
4343 macro_ptr = unget_saved_macro_ptr;
4344 unget_buffer_enabled = 0;
4345 } else {
4346 /* end of macro string: free it */
4347 tok_str_free(macro_ptr_allocated);
4348 macro_ptr = NULL;
4350 goto redo;
4354 /* convert preprocessor tokens into C tokens */
4355 if (tok == TOK_PPNUM &&
4356 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4357 parse_number((char *)tokc.cstr->data);
4361 /* push back current token and set current token to 'last_tok'. Only
4362 identifier case handled for labels. */
4363 static inline void unget_tok(int last_tok)
4365 int i, n;
4366 int *q;
4367 unget_saved_macro_ptr = macro_ptr;
4368 unget_buffer_enabled = 1;
4369 q = unget_saved_buffer;
4370 macro_ptr = q;
4371 *q++ = tok;
4372 n = tok_ext_size(tok) - 1;
4373 for(i=0;i<n;i++)
4374 *q++ = tokc.tab[i];
4375 *q = 0; /* end of token string */
4376 tok = last_tok;
4380 void swap(int *p, int *q)
4382 int t;
4383 t = *p;
4384 *p = *q;
4385 *q = t;
4388 void vsetc(CType *type, int r, CValue *vc)
4390 int v;
4392 if (vtop >= vstack + (VSTACK_SIZE - 1))
4393 error("memory full");
4394 /* cannot let cpu flags if other instruction are generated. Also
4395 avoid leaving VT_JMP anywhere except on the top of the stack
4396 because it would complicate the code generator. */
4397 if (vtop >= vstack) {
4398 v = vtop->r & VT_VALMASK;
4399 if (v == VT_CMP || (v & ~1) == VT_JMP)
4400 gv(RC_INT);
4402 vtop++;
4403 vtop->type = *type;
4404 vtop->r = r;
4405 vtop->r2 = VT_CONST;
4406 vtop->c = *vc;
4409 /* push integer constant */
4410 void vpushi(int v)
4412 CValue cval;
4413 cval.i = v;
4414 vsetc(&int_type, VT_CONST, &cval);
4417 /* Return a static symbol pointing to a section */
4418 static Sym *get_sym_ref(CType *type, Section *sec,
4419 unsigned long offset, unsigned long size)
4421 int v;
4422 Sym *sym;
4424 v = anon_sym++;
4425 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4426 sym->type.ref = type->ref;
4427 sym->r = VT_CONST | VT_SYM;
4428 put_extern_sym(sym, sec, offset, size);
4429 return sym;
4432 /* push a reference to a section offset by adding a dummy symbol */
4433 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4435 CValue cval;
4437 cval.ul = 0;
4438 vsetc(type, VT_CONST | VT_SYM, &cval);
4439 vtop->sym = get_sym_ref(type, sec, offset, size);
4442 /* define a new external reference to a symbol 'v' of type 'u' */
4443 static Sym *external_global_sym(int v, CType *type, int r)
4445 Sym *s;
4447 s = sym_find(v);
4448 if (!s) {
4449 /* push forward reference */
4450 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4451 s->type.ref = type->ref;
4452 s->r = r | VT_CONST | VT_SYM;
4454 return s;
4457 /* define a new external reference to a symbol 'v' of type 'u' */
4458 static Sym *external_sym(int v, CType *type, int r)
4460 Sym *s;
4462 s = sym_find(v);
4463 if (!s) {
4464 /* push forward reference */
4465 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4466 s->type.t |= VT_EXTERN;
4467 } else {
4468 if (!is_compatible_types(&s->type, type))
4469 error("incompatible types for redefinition of '%s'",
4470 get_tok_str(v, NULL));
4472 return s;
4475 /* push a reference to global symbol v */
4476 static void vpush_global_sym(CType *type, int v)
4478 Sym *sym;
4479 CValue cval;
4481 sym = external_global_sym(v, type, 0);
4482 cval.ul = 0;
4483 vsetc(type, VT_CONST | VT_SYM, &cval);
4484 vtop->sym = sym;
4487 void vset(CType *type, int r, int v)
4489 CValue cval;
4491 cval.i = v;
4492 vsetc(type, r, &cval);
4495 void vseti(int r, int v)
4497 CType type;
4498 type.t = VT_INT;
4499 vset(&type, r, v);
4502 void vswap(void)
4504 SValue tmp;
4506 tmp = vtop[0];
4507 vtop[0] = vtop[-1];
4508 vtop[-1] = tmp;
4511 void vpushv(SValue *v)
4513 if (vtop >= vstack + (VSTACK_SIZE - 1))
4514 error("memory full");
4515 vtop++;
4516 *vtop = *v;
4519 void vdup(void)
4521 vpushv(vtop);
4524 /* save r to the memory stack, and mark it as being free */
4525 void save_reg(int r)
4527 int l, saved, size, align;
4528 SValue *p, sv;
4529 CType *type;
4531 /* modify all stack values */
4532 saved = 0;
4533 l = 0;
4534 for(p=vstack;p<=vtop;p++) {
4535 if ((p->r & VT_VALMASK) == r ||
4536 (p->r2 & VT_VALMASK) == r) {
4537 /* must save value on stack if not already done */
4538 if (!saved) {
4539 /* NOTE: must reload 'r' because r might be equal to r2 */
4540 r = p->r & VT_VALMASK;
4541 /* store register in the stack */
4542 type = &p->type;
4543 if ((p->r & VT_LVAL) ||
4544 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4545 type = &int_type;
4546 size = type_size(type, &align);
4547 loc = (loc - size) & -align;
4548 sv.type.t = type->t;
4549 sv.r = VT_LOCAL | VT_LVAL;
4550 sv.c.ul = loc;
4551 store(r, &sv);
4552 #ifdef TCC_TARGET_I386
4553 /* x86 specific: need to pop fp register ST0 if saved */
4554 if (r == TREG_ST0) {
4555 o(0xd9dd); /* fstp %st(1) */
4557 #endif
4558 /* special long long case */
4559 if ((type->t & VT_BTYPE) == VT_LLONG) {
4560 sv.c.ul += 4;
4561 store(p->r2, &sv);
4563 l = loc;
4564 saved = 1;
4566 /* mark that stack entry as being saved on the stack */
4567 if (p->r & VT_LVAL) {
4568 /* also clear the bounded flag because the
4569 relocation address of the function was stored in
4570 p->c.ul */
4571 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4572 } else {
4573 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4575 p->r2 = VT_CONST;
4576 p->c.ul = l;
4581 /* find a register of class 'rc2' with at most one reference on stack.
4582 * If none, call get_reg(rc) */
4583 int get_reg_ex(int rc, int rc2)
4585 int r;
4586 SValue *p;
4588 for(r=0;r<NB_REGS;r++) {
4589 if (reg_classes[r] & rc2) {
4590 int n;
4591 n=0;
4592 for(p = vstack; p <= vtop; p++) {
4593 if ((p->r & VT_VALMASK) == r ||
4594 (p->r2 & VT_VALMASK) == r)
4595 n++;
4597 if (n <= 1)
4598 return r;
4601 return get_reg(rc);
4604 /* find a free register of class 'rc'. If none, save one register */
4605 int get_reg(int rc)
4607 int r;
4608 SValue *p;
4610 /* find a free register */
4611 for(r=0;r<NB_REGS;r++) {
4612 if (reg_classes[r] & rc) {
4613 for(p=vstack;p<=vtop;p++) {
4614 if ((p->r & VT_VALMASK) == r ||
4615 (p->r2 & VT_VALMASK) == r)
4616 goto notfound;
4618 return r;
4620 notfound: ;
4623 /* no register left : free the first one on the stack (VERY
4624 IMPORTANT to start from the bottom to ensure that we don't
4625 spill registers used in gen_opi()) */
4626 for(p=vstack;p<=vtop;p++) {
4627 r = p->r & VT_VALMASK;
4628 if (r < VT_CONST && (reg_classes[r] & rc))
4629 goto save_found;
4630 /* also look at second register (if long long) */
4631 r = p->r2 & VT_VALMASK;
4632 if (r < VT_CONST && (reg_classes[r] & rc)) {
4633 save_found:
4634 save_reg(r);
4635 return r;
4638 /* Should never comes here */
4639 return -1;
4642 /* save registers up to (vtop - n) stack entry */
4643 void save_regs(int n)
4645 int r;
4646 SValue *p, *p1;
4647 p1 = vtop - n;
4648 for(p = vstack;p <= p1; p++) {
4649 r = p->r & VT_VALMASK;
4650 if (r < VT_CONST) {
4651 save_reg(r);
4656 /* move register 's' to 'r', and flush previous value of r to memory
4657 if needed */
4658 void move_reg(int r, int s)
4660 SValue sv;
4662 if (r != s) {
4663 save_reg(r);
4664 sv.type.t = VT_INT;
4665 sv.r = s;
4666 sv.c.ul = 0;
4667 load(r, &sv);
4671 /* get address of vtop (vtop MUST BE an lvalue) */
4672 void gaddrof(void)
4674 vtop->r &= ~VT_LVAL;
4675 /* tricky: if saved lvalue, then we can go back to lvalue */
4676 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4677 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4680 #ifdef CONFIG_TCC_BCHECK
4681 /* generate lvalue bound code */
4682 void gbound(void)
4684 int lval_type;
4685 CType type1;
4687 vtop->r &= ~VT_MUSTBOUND;
4688 /* if lvalue, then use checking code before dereferencing */
4689 if (vtop->r & VT_LVAL) {
4690 /* if not VT_BOUNDED value, then make one */
4691 if (!(vtop->r & VT_BOUNDED)) {
4692 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4693 /* must save type because we must set it to int to get pointer */
4694 type1 = vtop->type;
4695 vtop->type.t = VT_INT;
4696 gaddrof();
4697 vpushi(0);
4698 gen_bounded_ptr_add();
4699 vtop->r |= lval_type;
4700 vtop->type = type1;
4702 /* then check for dereferencing */
4703 gen_bounded_ptr_deref();
4706 #endif
4708 /* store vtop a register belonging to class 'rc'. lvalues are
4709 converted to values. Cannot be used if cannot be converted to
4710 register value (such as structures). */
4711 int gv(int rc)
4713 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4714 unsigned long long ll;
4716 /* NOTE: get_reg can modify vstack[] */
4717 if (vtop->type.t & VT_BITFIELD) {
4718 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4719 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4720 /* remove bit field info to avoid loops */
4721 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4722 /* generate shifts */
4723 vpushi(32 - (bit_pos + bit_size));
4724 gen_op(TOK_SHL);
4725 vpushi(32 - bit_size);
4726 /* NOTE: transformed to SHR if unsigned */
4727 gen_op(TOK_SAR);
4728 r = gv(rc);
4729 } else {
4730 if (is_float(vtop->type.t) &&
4731 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4732 Sym *sym;
4733 int *ptr;
4734 unsigned long offset;
4736 /* XXX: unify with initializers handling ? */
4737 /* CPUs usually cannot use float constants, so we store them
4738 generically in data segment */
4739 size = type_size(&vtop->type, &align);
4740 offset = (data_section->data_offset + align - 1) & -align;
4741 data_section->data_offset = offset;
4742 /* XXX: not portable yet */
4743 ptr = section_ptr_add(data_section, size);
4744 size = size >> 2;
4745 for(i=0;i<size;i++)
4746 ptr[i] = vtop->c.tab[i];
4747 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4748 vtop->r |= VT_LVAL | VT_SYM;
4749 vtop->sym = sym;
4750 vtop->c.ul = 0;
4752 #ifdef CONFIG_TCC_BCHECK
4753 if (vtop->r & VT_MUSTBOUND)
4754 gbound();
4755 #endif
4757 r = vtop->r & VT_VALMASK;
4758 /* need to reload if:
4759 - constant
4760 - lvalue (need to dereference pointer)
4761 - already a register, but not in the right class */
4762 if (r >= VT_CONST ||
4763 (vtop->r & VT_LVAL) ||
4764 !(reg_classes[r] & rc) ||
4765 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4766 !(reg_classes[vtop->r2] & rc))) {
4767 r = get_reg(rc);
4768 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4769 /* two register type load : expand to two words
4770 temporarily */
4771 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4772 /* load constant */
4773 ll = vtop->c.ull;
4774 vtop->c.ui = ll; /* first word */
4775 load(r, vtop);
4776 vtop->r = r; /* save register value */
4777 vpushi(ll >> 32); /* second word */
4778 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
4779 (vtop->r & VT_LVAL)) {
4780 /* We do not want to modifier the long long
4781 pointer here, so the safest (and less
4782 efficient) is to save all the other registers
4783 in the stack. XXX: totally inefficient. */
4784 save_regs(1);
4785 /* load from memory */
4786 load(r, vtop);
4787 vdup();
4788 vtop[-1].r = r; /* save register value */
4789 /* increment pointer to get second word */
4790 vtop->type.t = VT_INT;
4791 gaddrof();
4792 vpushi(4);
4793 gen_op('+');
4794 vtop->r |= VT_LVAL;
4795 } else {
4796 /* move registers */
4797 load(r, vtop);
4798 vdup();
4799 vtop[-1].r = r; /* save register value */
4800 vtop->r = vtop[-1].r2;
4802 /* allocate second register */
4803 rc2 = RC_INT;
4804 if (rc == RC_IRET)
4805 rc2 = RC_LRET;
4806 r2 = get_reg(rc2);
4807 load(r2, vtop);
4808 vpop();
4809 /* write second register */
4810 vtop->r2 = r2;
4811 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4812 int t1, t;
4813 /* lvalue of scalar type : need to use lvalue type
4814 because of possible cast */
4815 t = vtop->type.t;
4816 t1 = t;
4817 /* compute memory access type */
4818 if (vtop->r & VT_LVAL_BYTE)
4819 t = VT_BYTE;
4820 else if (vtop->r & VT_LVAL_SHORT)
4821 t = VT_SHORT;
4822 if (vtop->r & VT_LVAL_UNSIGNED)
4823 t |= VT_UNSIGNED;
4824 vtop->type.t = t;
4825 load(r, vtop);
4826 /* restore wanted type */
4827 vtop->type.t = t1;
4828 } else {
4829 /* one register type load */
4830 load(r, vtop);
4833 vtop->r = r;
4834 #ifdef TCC_TARGET_C67
4835 /* uses register pairs for doubles */
4836 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
4837 vtop->r2 = r+1;
4838 #endif
4840 return r;
4843 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4844 void gv2(int rc1, int rc2)
4846 int v;
4848 /* generate more generic register first. But VT_JMP or VT_CMP
4849 values must be generated first in all cases to avoid possible
4850 reload errors */
4851 v = vtop[0].r & VT_VALMASK;
4852 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4853 vswap();
4854 gv(rc1);
4855 vswap();
4856 gv(rc2);
4857 /* test if reload is needed for first register */
4858 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4859 vswap();
4860 gv(rc1);
4861 vswap();
4863 } else {
4864 gv(rc2);
4865 vswap();
4866 gv(rc1);
4867 vswap();
4868 /* test if reload is needed for first register */
4869 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4870 gv(rc2);
4875 /* expand long long on stack in two int registers */
4876 void lexpand(void)
4878 int u;
4880 u = vtop->type.t & VT_UNSIGNED;
4881 gv(RC_INT);
4882 vdup();
4883 vtop[0].r = vtop[-1].r2;
4884 vtop[0].r2 = VT_CONST;
4885 vtop[-1].r2 = VT_CONST;
4886 vtop[0].type.t = VT_INT | u;
4887 vtop[-1].type.t = VT_INT | u;
4890 #ifdef TCC_TARGET_ARM
4891 /* expand long long on stack */
4892 void lexpand_nr(void)
4894 int u,v;
4896 u = vtop->type.t & VT_UNSIGNED;
4897 vdup();
4898 vtop->r2 = VT_CONST;
4899 vtop->type.t = VT_INT | u;
4900 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
4901 if (v == VT_CONST) {
4902 vtop[-1].c.ui = vtop->c.ull;
4903 vtop->c.ui = vtop->c.ull >> 32;
4904 vtop->r = VT_CONST;
4905 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
4906 vtop->c.ui += 4;
4907 vtop->r = vtop[-1].r;
4908 } else if (v > VT_CONST) {
4909 vtop--;
4910 lexpand();
4911 } else
4912 vtop->r = vtop[-1].r2;
4913 vtop[-1].r2 = VT_CONST;
4914 vtop[-1].type.t = VT_INT | u;
4916 #endif
4918 /* build a long long from two ints */
4919 void lbuild(int t)
4921 gv2(RC_INT, RC_INT);
4922 vtop[-1].r2 = vtop[0].r;
4923 vtop[-1].type.t = t;
4924 vpop();
4927 /* rotate n first stack elements to the bottom
4928 I1 ... In -> I2 ... In I1 [top is right]
4930 void vrotb(int n)
4932 int i;
4933 SValue tmp;
4935 tmp = vtop[-n + 1];
4936 for(i=-n+1;i!=0;i++)
4937 vtop[i] = vtop[i+1];
4938 vtop[0] = tmp;
4941 /* rotate n first stack elements to the top
4942 I1 ... In -> In I1 ... I(n-1) [top is right]
4944 void vrott(int n)
4946 int i;
4947 SValue tmp;
4949 tmp = vtop[0];
4950 for(i = 0;i < n - 1; i++)
4951 vtop[-i] = vtop[-i - 1];
4952 vtop[-n + 1] = tmp;
4955 #ifdef TCC_TARGET_ARM
4956 /* like vrott but in other direction
4957 In ... I1 -> I(n-1) ... I1 In [top is right]
4959 void vnrott(int n)
4961 int i;
4962 SValue tmp;
4964 tmp = vtop[-n + 1];
4965 for(i = n - 1; i > 0; i--)
4966 vtop[-i] = vtop[-i + 1];
4967 vtop[0] = tmp;
4969 #endif
4971 /* pop stack value */
4972 void vpop(void)
4974 int v;
4975 v = vtop->r & VT_VALMASK;
4976 #ifdef TCC_TARGET_I386
4977 /* for x86, we need to pop the FP stack */
4978 if (v == TREG_ST0 && !nocode_wanted) {
4979 o(0xd9dd); /* fstp %st(1) */
4980 } else
4981 #endif
4982 if (v == VT_JMP || v == VT_JMPI) {
4983 /* need to put correct jump if && or || without test */
4984 gsym(vtop->c.ul);
4986 vtop--;
4989 /* convert stack entry to register and duplicate its value in another
4990 register */
4991 void gv_dup(void)
4993 int rc, t, r, r1;
4994 SValue sv;
4996 t = vtop->type.t;
4997 if ((t & VT_BTYPE) == VT_LLONG) {
4998 lexpand();
4999 gv_dup();
5000 vswap();
5001 vrotb(3);
5002 gv_dup();
5003 vrotb(4);
5004 /* stack: H L L1 H1 */
5005 lbuild(t);
5006 vrotb(3);
5007 vrotb(3);
5008 vswap();
5009 lbuild(t);
5010 vswap();
5011 } else {
5012 /* duplicate value */
5013 rc = RC_INT;
5014 sv.type.t = VT_INT;
5015 if (is_float(t)) {
5016 rc = RC_FLOAT;
5017 sv.type.t = t;
5019 r = gv(rc);
5020 r1 = get_reg(rc);
5021 sv.r = r;
5022 sv.c.ul = 0;
5023 load(r1, &sv); /* move r to r1 */
5024 vdup();
5025 /* duplicates value */
5026 vtop->r = r1;
5030 /* generate CPU independent (unsigned) long long operations */
5031 void gen_opl(int op)
5033 int t, a, b, op1, c, i;
5034 int func;
5035 SValue tmp;
5037 switch(op) {
5038 case '/':
5039 case TOK_PDIV:
5040 func = TOK___divdi3;
5041 goto gen_func;
5042 case TOK_UDIV:
5043 func = TOK___udivdi3;
5044 goto gen_func;
5045 case '%':
5046 func = TOK___moddi3;
5047 goto gen_func;
5048 case TOK_UMOD:
5049 func = TOK___umoddi3;
5050 gen_func:
5051 /* call generic long long function */
5052 vpush_global_sym(&func_old_type, func);
5053 vrott(3);
5054 gfunc_call(2);
5055 vpushi(0);
5056 vtop->r = REG_IRET;
5057 vtop->r2 = REG_LRET;
5058 break;
5059 case '^':
5060 case '&':
5061 case '|':
5062 case '*':
5063 case '+':
5064 case '-':
5065 t = vtop->type.t;
5066 vswap();
5067 lexpand();
5068 vrotb(3);
5069 lexpand();
5070 /* stack: L1 H1 L2 H2 */
5071 tmp = vtop[0];
5072 vtop[0] = vtop[-3];
5073 vtop[-3] = tmp;
5074 tmp = vtop[-2];
5075 vtop[-2] = vtop[-3];
5076 vtop[-3] = tmp;
5077 vswap();
5078 /* stack: H1 H2 L1 L2 */
5079 if (op == '*') {
5080 vpushv(vtop - 1);
5081 vpushv(vtop - 1);
5082 gen_op(TOK_UMULL);
5083 lexpand();
5084 /* stack: H1 H2 L1 L2 ML MH */
5085 for(i=0;i<4;i++)
5086 vrotb(6);
5087 /* stack: ML MH H1 H2 L1 L2 */
5088 tmp = vtop[0];
5089 vtop[0] = vtop[-2];
5090 vtop[-2] = tmp;
5091 /* stack: ML MH H1 L2 H2 L1 */
5092 gen_op('*');
5093 vrotb(3);
5094 vrotb(3);
5095 gen_op('*');
5096 /* stack: ML MH M1 M2 */
5097 gen_op('+');
5098 gen_op('+');
5099 } else if (op == '+' || op == '-') {
5100 /* XXX: add non carry method too (for MIPS or alpha) */
5101 if (op == '+')
5102 op1 = TOK_ADDC1;
5103 else
5104 op1 = TOK_SUBC1;
5105 gen_op(op1);
5106 /* stack: H1 H2 (L1 op L2) */
5107 vrotb(3);
5108 vrotb(3);
5109 gen_op(op1 + 1); /* TOK_xxxC2 */
5110 } else {
5111 gen_op(op);
5112 /* stack: H1 H2 (L1 op L2) */
5113 vrotb(3);
5114 vrotb(3);
5115 /* stack: (L1 op L2) H1 H2 */
5116 gen_op(op);
5117 /* stack: (L1 op L2) (H1 op H2) */
5119 /* stack: L H */
5120 lbuild(t);
5121 break;
5122 case TOK_SAR:
5123 case TOK_SHR:
5124 case TOK_SHL:
5125 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5126 t = vtop[-1].type.t;
5127 vswap();
5128 lexpand();
5129 vrotb(3);
5130 /* stack: L H shift */
5131 c = (int)vtop->c.i;
5132 /* constant: simpler */
5133 /* NOTE: all comments are for SHL. the other cases are
5134 done by swaping words */
5135 vpop();
5136 if (op != TOK_SHL)
5137 vswap();
5138 if (c >= 32) {
5139 /* stack: L H */
5140 vpop();
5141 if (c > 32) {
5142 vpushi(c - 32);
5143 gen_op(op);
5145 if (op != TOK_SAR) {
5146 vpushi(0);
5147 } else {
5148 gv_dup();
5149 vpushi(31);
5150 gen_op(TOK_SAR);
5152 vswap();
5153 } else {
5154 vswap();
5155 gv_dup();
5156 /* stack: H L L */
5157 vpushi(c);
5158 gen_op(op);
5159 vswap();
5160 vpushi(32 - c);
5161 if (op == TOK_SHL)
5162 gen_op(TOK_SHR);
5163 else
5164 gen_op(TOK_SHL);
5165 vrotb(3);
5166 /* stack: L L H */
5167 vpushi(c);
5168 if (op == TOK_SHL)
5169 gen_op(TOK_SHL);
5170 else
5171 gen_op(TOK_SHR);
5172 gen_op('|');
5174 if (op != TOK_SHL)
5175 vswap();
5176 lbuild(t);
5177 } else {
5178 /* XXX: should provide a faster fallback on x86 ? */
5179 switch(op) {
5180 case TOK_SAR:
5181 func = TOK___sardi3;
5182 goto gen_func;
5183 case TOK_SHR:
5184 func = TOK___shrdi3;
5185 goto gen_func;
5186 case TOK_SHL:
5187 func = TOK___shldi3;
5188 goto gen_func;
5191 break;
5192 default:
5193 /* compare operations */
5194 t = vtop->type.t;
5195 vswap();
5196 lexpand();
5197 vrotb(3);
5198 lexpand();
5199 /* stack: L1 H1 L2 H2 */
5200 tmp = vtop[-1];
5201 vtop[-1] = vtop[-2];
5202 vtop[-2] = tmp;
5203 /* stack: L1 L2 H1 H2 */
5204 /* compare high */
5205 op1 = op;
5206 /* when values are equal, we need to compare low words. since
5207 the jump is inverted, we invert the test too. */
5208 if (op1 == TOK_LT)
5209 op1 = TOK_LE;
5210 else if (op1 == TOK_GT)
5211 op1 = TOK_GE;
5212 else if (op1 == TOK_ULT)
5213 op1 = TOK_ULE;
5214 else if (op1 == TOK_UGT)
5215 op1 = TOK_UGE;
5216 a = 0;
5217 b = 0;
5218 gen_op(op1);
5219 if (op1 != TOK_NE) {
5220 a = gtst(1, 0);
5222 if (op != TOK_EQ) {
5223 /* generate non equal test */
5224 /* XXX: NOT PORTABLE yet */
5225 if (a == 0) {
5226 b = gtst(0, 0);
5227 } else {
5228 #if defined(TCC_TARGET_I386)
5229 b = psym(0x850f, 0);
5230 #elif defined(TCC_TARGET_ARM)
5231 b = ind;
5232 o(0x1A000000 | encbranch(ind, 0, 1));
5233 #elif defined(TCC_TARGET_C67)
5234 error("not implemented");
5235 #else
5236 #error not supported
5237 #endif
5240 /* compare low. Always unsigned */
5241 op1 = op;
5242 if (op1 == TOK_LT)
5243 op1 = TOK_ULT;
5244 else if (op1 == TOK_LE)
5245 op1 = TOK_ULE;
5246 else if (op1 == TOK_GT)
5247 op1 = TOK_UGT;
5248 else if (op1 == TOK_GE)
5249 op1 = TOK_UGE;
5250 gen_op(op1);
5251 a = gtst(1, a);
5252 gsym(b);
5253 vseti(VT_JMPI, a);
5254 break;
5258 /* handle integer constant optimizations and various machine
5259 independent opt */
5260 void gen_opic(int op)
5262 int fc, c1, c2, n;
5263 SValue *v1, *v2;
5265 v1 = vtop - 1;
5266 v2 = vtop;
5267 /* currently, we cannot do computations with forward symbols */
5268 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5269 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5270 if (c1 && c2) {
5271 fc = v2->c.i;
5272 switch(op) {
5273 case '+': v1->c.i += fc; break;
5274 case '-': v1->c.i -= fc; break;
5275 case '&': v1->c.i &= fc; break;
5276 case '^': v1->c.i ^= fc; break;
5277 case '|': v1->c.i |= fc; break;
5278 case '*': v1->c.i *= fc; break;
5280 case TOK_PDIV:
5281 case '/':
5282 case '%':
5283 case TOK_UDIV:
5284 case TOK_UMOD:
5285 /* if division by zero, generate explicit division */
5286 if (fc == 0) {
5287 if (const_wanted)
5288 error("division by zero in constant");
5289 goto general_case;
5291 switch(op) {
5292 default: v1->c.i /= fc; break;
5293 case '%': v1->c.i %= fc; break;
5294 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
5295 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
5297 break;
5298 case TOK_SHL: v1->c.i <<= fc; break;
5299 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
5300 case TOK_SAR: v1->c.i >>= fc; break;
5301 /* tests */
5302 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
5303 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
5304 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
5305 case TOK_NE: v1->c.i = v1->c.i != fc; break;
5306 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
5307 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
5308 case TOK_LT: v1->c.i = v1->c.i < fc; break;
5309 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
5310 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
5311 case TOK_GT: v1->c.i = v1->c.i > fc; break;
5312 /* logical */
5313 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
5314 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
5315 default:
5316 goto general_case;
5318 vtop--;
5319 } else {
5320 /* if commutative ops, put c2 as constant */
5321 if (c1 && (op == '+' || op == '&' || op == '^' ||
5322 op == '|' || op == '*')) {
5323 vswap();
5324 swap(&c1, &c2);
5326 fc = vtop->c.i;
5327 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5328 op == TOK_PDIV) &&
5329 fc == 1) ||
5330 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5331 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5332 fc == 0) ||
5333 (op == '&' &&
5334 fc == -1))) {
5335 /* nothing to do */
5336 vtop--;
5337 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5338 /* try to use shifts instead of muls or divs */
5339 if (fc > 0 && (fc & (fc - 1)) == 0) {
5340 n = -1;
5341 while (fc) {
5342 fc >>= 1;
5343 n++;
5345 vtop->c.i = n;
5346 if (op == '*')
5347 op = TOK_SHL;
5348 else if (op == TOK_PDIV)
5349 op = TOK_SAR;
5350 else
5351 op = TOK_SHR;
5353 goto general_case;
5354 } else if (c2 && (op == '+' || op == '-') &&
5355 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5356 (VT_CONST | VT_SYM)) {
5357 /* symbol + constant case */
5358 if (op == '-')
5359 fc = -fc;
5360 vtop--;
5361 vtop->c.i += fc;
5362 } else {
5363 general_case:
5364 if (!nocode_wanted) {
5365 /* call low level op generator */
5366 gen_opi(op);
5367 } else {
5368 vtop--;
5374 /* generate a floating point operation with constant propagation */
5375 void gen_opif(int op)
5377 int c1, c2;
5378 SValue *v1, *v2;
5379 long double f1, f2;
5381 v1 = vtop - 1;
5382 v2 = vtop;
5383 /* currently, we cannot do computations with forward symbols */
5384 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5385 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5386 if (c1 && c2) {
5387 if (v1->type.t == VT_FLOAT) {
5388 f1 = v1->c.f;
5389 f2 = v2->c.f;
5390 } else if (v1->type.t == VT_DOUBLE) {
5391 f1 = v1->c.d;
5392 f2 = v2->c.d;
5393 } else {
5394 f1 = v1->c.ld;
5395 f2 = v2->c.ld;
5398 /* NOTE: we only do constant propagation if finite number (not
5399 NaN or infinity) (ANSI spec) */
5400 if (!ieee_finite(f1) || !ieee_finite(f2))
5401 goto general_case;
5403 switch(op) {
5404 case '+': f1 += f2; break;
5405 case '-': f1 -= f2; break;
5406 case '*': f1 *= f2; break;
5407 case '/':
5408 if (f2 == 0.0) {
5409 if (const_wanted)
5410 error("division by zero in constant");
5411 goto general_case;
5413 f1 /= f2;
5414 break;
5415 /* XXX: also handles tests ? */
5416 default:
5417 goto general_case;
5419 /* XXX: overflow test ? */
5420 if (v1->type.t == VT_FLOAT) {
5421 v1->c.f = f1;
5422 } else if (v1->type.t == VT_DOUBLE) {
5423 v1->c.d = f1;
5424 } else {
5425 v1->c.ld = f1;
5427 vtop--;
5428 } else {
5429 general_case:
5430 if (!nocode_wanted) {
5431 gen_opf(op);
5432 } else {
5433 vtop--;
5438 static int pointed_size(CType *type)
5440 int align;
5441 return type_size(pointed_type(type), &align);
5444 static inline int is_null_pointer(SValue *p)
5446 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5447 return 0;
5448 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5449 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5452 static inline int is_integer_btype(int bt)
5454 return (bt == VT_BYTE || bt == VT_SHORT ||
5455 bt == VT_INT || bt == VT_LLONG);
5458 /* check types for comparison or substraction of pointers */
5459 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5461 CType *type1, *type2, tmp_type1, tmp_type2;
5462 int bt1, bt2;
5464 /* null pointers are accepted for all comparisons as gcc */
5465 if (is_null_pointer(p1) || is_null_pointer(p2))
5466 return;
5467 type1 = &p1->type;
5468 type2 = &p2->type;
5469 bt1 = type1->t & VT_BTYPE;
5470 bt2 = type2->t & VT_BTYPE;
5471 /* accept comparison between pointer and integer with a warning */
5472 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5473 warning("comparison between pointer and integer");
5474 return;
5477 /* both must be pointers or implicit function pointers */
5478 if (bt1 == VT_PTR) {
5479 type1 = pointed_type(type1);
5480 } else if (bt1 != VT_FUNC)
5481 goto invalid_operands;
5483 if (bt2 == VT_PTR) {
5484 type2 = pointed_type(type2);
5485 } else if (bt2 != VT_FUNC) {
5486 invalid_operands:
5487 error("invalid operands to binary %s", get_tok_str(op, NULL));
5489 if ((type1->t & VT_BTYPE) == VT_VOID ||
5490 (type2->t & VT_BTYPE) == VT_VOID)
5491 return;
5492 tmp_type1 = *type1;
5493 tmp_type2 = *type2;
5494 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5495 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5496 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5497 /* gcc-like error if '-' is used */
5498 if (op == '-')
5499 goto invalid_operands;
5500 else
5501 warning("comparison of distinct pointer types lacks a cast");
5505 /* generic gen_op: handles types problems */
5506 void gen_op(int op)
5508 int u, t1, t2, bt1, bt2, t;
5509 CType type1;
5511 t1 = vtop[-1].type.t;
5512 t2 = vtop[0].type.t;
5513 bt1 = t1 & VT_BTYPE;
5514 bt2 = t2 & VT_BTYPE;
5516 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5517 /* at least one operand is a pointer */
5518 /* relationnal op: must be both pointers */
5519 if (op >= TOK_ULT && op <= TOK_GT) {
5520 check_comparison_pointer_types(vtop - 1, vtop, op);
5521 /* pointers are handled are unsigned */
5522 t = VT_INT | VT_UNSIGNED;
5523 goto std_op;
5525 /* if both pointers, then it must be the '-' op */
5526 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5527 if (op != '-')
5528 error("cannot use pointers here");
5529 check_comparison_pointer_types(vtop - 1, vtop, op);
5530 /* XXX: check that types are compatible */
5531 u = pointed_size(&vtop[-1].type);
5532 gen_opic(op);
5533 /* set to integer type */
5534 vtop->type.t = VT_INT;
5535 vpushi(u);
5536 gen_op(TOK_PDIV);
5537 } else {
5538 /* exactly one pointer : must be '+' or '-'. */
5539 if (op != '-' && op != '+')
5540 error("cannot use pointers here");
5541 /* Put pointer as first operand */
5542 if (bt2 == VT_PTR) {
5543 vswap();
5544 swap(&t1, &t2);
5546 type1 = vtop[-1].type;
5547 /* XXX: cast to int ? (long long case) */
5548 vpushi(pointed_size(&vtop[-1].type));
5549 gen_op('*');
5550 #ifdef CONFIG_TCC_BCHECK
5551 /* if evaluating constant expression, no code should be
5552 generated, so no bound check */
5553 if (do_bounds_check && !const_wanted) {
5554 /* if bounded pointers, we generate a special code to
5555 test bounds */
5556 if (op == '-') {
5557 vpushi(0);
5558 vswap();
5559 gen_op('-');
5561 gen_bounded_ptr_add();
5562 } else
5563 #endif
5565 gen_opic(op);
5567 /* put again type if gen_opic() swaped operands */
5568 vtop->type = type1;
5570 } else if (is_float(bt1) || is_float(bt2)) {
5571 /* compute bigger type and do implicit casts */
5572 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5573 t = VT_LDOUBLE;
5574 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5575 t = VT_DOUBLE;
5576 } else {
5577 t = VT_FLOAT;
5579 /* floats can only be used for a few operations */
5580 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5581 (op < TOK_ULT || op > TOK_GT))
5582 error("invalid operands for binary operation");
5583 goto std_op;
5584 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5585 /* cast to biggest op */
5586 t = VT_LLONG;
5587 /* convert to unsigned if it does not fit in a long long */
5588 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5589 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5590 t |= VT_UNSIGNED;
5591 goto std_op;
5592 } else {
5593 /* integer operations */
5594 t = VT_INT;
5595 /* convert to unsigned if it does not fit in an integer */
5596 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5597 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5598 t |= VT_UNSIGNED;
5599 std_op:
5600 /* XXX: currently, some unsigned operations are explicit, so
5601 we modify them here */
5602 if (t & VT_UNSIGNED) {
5603 if (op == TOK_SAR)
5604 op = TOK_SHR;
5605 else if (op == '/')
5606 op = TOK_UDIV;
5607 else if (op == '%')
5608 op = TOK_UMOD;
5609 else if (op == TOK_LT)
5610 op = TOK_ULT;
5611 else if (op == TOK_GT)
5612 op = TOK_UGT;
5613 else if (op == TOK_LE)
5614 op = TOK_ULE;
5615 else if (op == TOK_GE)
5616 op = TOK_UGE;
5618 vswap();
5619 type1.t = t;
5620 gen_cast(&type1);
5621 vswap();
5622 /* special case for shifts and long long: we keep the shift as
5623 an integer */
5624 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5625 type1.t = VT_INT;
5626 gen_cast(&type1);
5627 if (is_float(t))
5628 gen_opif(op);
5629 else if ((t & VT_BTYPE) == VT_LLONG)
5630 gen_opl(op);
5631 else
5632 gen_opic(op);
5633 if (op >= TOK_ULT && op <= TOK_GT) {
5634 /* relationnal op: the result is an int */
5635 vtop->type.t = VT_INT;
5636 } else {
5637 vtop->type.t = t;
5642 /* generic itof for unsigned long long case */
5643 void gen_cvt_itof1(int t)
5645 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5646 (VT_LLONG | VT_UNSIGNED)) {
5648 if (t == VT_FLOAT)
5649 vpush_global_sym(&func_old_type, TOK___ulltof);
5650 else if (t == VT_DOUBLE)
5651 vpush_global_sym(&func_old_type, TOK___ulltod);
5652 else
5653 vpush_global_sym(&func_old_type, TOK___ulltold);
5654 vrott(2);
5655 gfunc_call(1);
5656 vpushi(0);
5657 vtop->r = REG_FRET;
5658 } else {
5659 gen_cvt_itof(t);
5663 /* generic ftoi for unsigned long long case */
5664 void gen_cvt_ftoi1(int t)
5666 int st;
5668 if (t == (VT_LLONG | VT_UNSIGNED)) {
5669 /* not handled natively */
5670 st = vtop->type.t & VT_BTYPE;
5671 if (st == VT_FLOAT)
5672 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5673 else if (st == VT_DOUBLE)
5674 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5675 else
5676 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5677 vrott(2);
5678 gfunc_call(1);
5679 vpushi(0);
5680 vtop->r = REG_IRET;
5681 vtop->r2 = REG_LRET;
5682 } else {
5683 gen_cvt_ftoi(t);
5687 /* force char or short cast */
5688 void force_charshort_cast(int t)
5690 int bits, dbt;
5691 dbt = t & VT_BTYPE;
5692 /* XXX: add optimization if lvalue : just change type and offset */
5693 if (dbt == VT_BYTE)
5694 bits = 8;
5695 else
5696 bits = 16;
5697 if (t & VT_UNSIGNED) {
5698 vpushi((1 << bits) - 1);
5699 gen_op('&');
5700 } else {
5701 bits = 32 - bits;
5702 vpushi(bits);
5703 gen_op(TOK_SHL);
5704 vpushi(bits);
5705 gen_op(TOK_SAR);
5709 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5710 static void gen_cast(CType *type)
5712 int sbt, dbt, sf, df, c;
5714 /* special delayed cast for char/short */
5715 /* XXX: in some cases (multiple cascaded casts), it may still
5716 be incorrect */
5717 if (vtop->r & VT_MUSTCAST) {
5718 vtop->r &= ~VT_MUSTCAST;
5719 force_charshort_cast(vtop->type.t);
5722 /* bitfields first get cast to ints */
5723 if (vtop->type.t & VT_BITFIELD) {
5724 gv(RC_INT);
5727 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5728 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5730 if (sbt != dbt && !nocode_wanted) {
5731 sf = is_float(sbt);
5732 df = is_float(dbt);
5733 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5734 if (sf && df) {
5735 /* convert from fp to fp */
5736 if (c) {
5737 /* constant case: we can do it now */
5738 /* XXX: in ISOC, cannot do it if error in convert */
5739 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5740 vtop->c.f = (float)vtop->c.d;
5741 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5742 vtop->c.f = (float)vtop->c.ld;
5743 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5744 vtop->c.d = (double)vtop->c.f;
5745 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5746 vtop->c.d = (double)vtop->c.ld;
5747 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5748 vtop->c.ld = (long double)vtop->c.f;
5749 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5750 vtop->c.ld = (long double)vtop->c.d;
5751 } else {
5752 /* non constant case: generate code */
5753 gen_cvt_ftof(dbt);
5755 } else if (df) {
5756 /* convert int to fp */
5757 if (c) {
5758 switch(sbt) {
5759 case VT_LLONG | VT_UNSIGNED:
5760 case VT_LLONG:
5761 /* XXX: add const cases for long long */
5762 goto do_itof;
5763 case VT_INT | VT_UNSIGNED:
5764 switch(dbt) {
5765 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5766 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5767 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5769 break;
5770 default:
5771 switch(dbt) {
5772 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5773 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5774 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5776 break;
5778 } else {
5779 do_itof:
5780 #if !defined(TCC_TARGET_ARM)
5781 gen_cvt_itof1(dbt);
5782 #else
5783 gen_cvt_itof(dbt);
5784 #endif
5786 } else if (sf) {
5787 /* convert fp to int */
5788 /* we handle char/short/etc... with generic code */
5789 if (dbt != (VT_INT | VT_UNSIGNED) &&
5790 dbt != (VT_LLONG | VT_UNSIGNED) &&
5791 dbt != VT_LLONG)
5792 dbt = VT_INT;
5793 if (c) {
5794 switch(dbt) {
5795 case VT_LLONG | VT_UNSIGNED:
5796 case VT_LLONG:
5797 /* XXX: add const cases for long long */
5798 goto do_ftoi;
5799 case VT_INT | VT_UNSIGNED:
5800 switch(sbt) {
5801 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5802 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5803 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5805 break;
5806 default:
5807 /* int case */
5808 switch(sbt) {
5809 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5810 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5811 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5813 break;
5815 } else {
5816 do_ftoi:
5817 gen_cvt_ftoi1(dbt);
5819 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5820 /* additional cast for char/short/bool... */
5821 vtop->type.t = dbt;
5822 gen_cast(type);
5824 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5825 if ((sbt & VT_BTYPE) != VT_LLONG) {
5826 /* scalar to long long */
5827 if (c) {
5828 if (sbt == (VT_INT | VT_UNSIGNED))
5829 vtop->c.ll = vtop->c.ui;
5830 else
5831 vtop->c.ll = vtop->c.i;
5832 } else {
5833 /* machine independent conversion */
5834 gv(RC_INT);
5835 /* generate high word */
5836 if (sbt == (VT_INT | VT_UNSIGNED)) {
5837 vpushi(0);
5838 gv(RC_INT);
5839 } else {
5840 gv_dup();
5841 vpushi(31);
5842 gen_op(TOK_SAR);
5844 /* patch second register */
5845 vtop[-1].r2 = vtop->r;
5846 vpop();
5849 } else if (dbt == VT_BOOL) {
5850 /* scalar to bool */
5851 vpushi(0);
5852 gen_op(TOK_NE);
5853 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5854 (dbt & VT_BTYPE) == VT_SHORT) {
5855 force_charshort_cast(dbt);
5856 } else if ((dbt & VT_BTYPE) == VT_INT) {
5857 /* scalar to int */
5858 if (sbt == VT_LLONG) {
5859 /* from long long: just take low order word */
5860 lexpand();
5861 vpop();
5863 /* if lvalue and single word type, nothing to do because
5864 the lvalue already contains the real type size (see
5865 VT_LVAL_xxx constants) */
5868 vtop->type = *type;
5871 /* return type size. Put alignment at 'a' */
5872 static int type_size(CType *type, int *a)
5874 Sym *s;
5875 int bt;
5877 bt = type->t & VT_BTYPE;
5878 if (bt == VT_STRUCT) {
5879 /* struct/union */
5880 s = type->ref;
5881 *a = s->r;
5882 return s->c;
5883 } else if (bt == VT_PTR) {
5884 if (type->t & VT_ARRAY) {
5885 s = type->ref;
5886 return type_size(&s->type, a) * s->c;
5887 } else {
5888 *a = PTR_SIZE;
5889 return PTR_SIZE;
5891 } else if (bt == VT_LDOUBLE) {
5892 *a = LDOUBLE_ALIGN;
5893 return LDOUBLE_SIZE;
5894 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5895 #ifdef TCC_TARGET_I386
5896 *a = 4;
5897 #else
5898 *a = 8;
5899 #endif
5900 return 8;
5901 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5902 *a = 4;
5903 return 4;
5904 } else if (bt == VT_SHORT) {
5905 *a = 2;
5906 return 2;
5907 } else {
5908 /* char, void, function, _Bool */
5909 *a = 1;
5910 return 1;
5914 /* return the pointed type of t */
5915 static inline CType *pointed_type(CType *type)
5917 return &type->ref->type;
5920 /* modify type so that its it is a pointer to type. */
5921 static void mk_pointer(CType *type)
5923 Sym *s;
5924 s = sym_push(SYM_FIELD, type, 0, -1);
5925 type->t = VT_PTR | (type->t & ~VT_TYPE);
5926 type->ref = s;
5929 /* compare function types. OLD functions match any new functions */
5930 static int is_compatible_func(CType *type1, CType *type2)
5932 Sym *s1, *s2;
5934 s1 = type1->ref;
5935 s2 = type2->ref;
5936 if (!is_compatible_types(&s1->type, &s2->type))
5937 return 0;
5938 /* check func_call */
5939 if (s1->r != s2->r)
5940 return 0;
5941 /* XXX: not complete */
5942 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5943 return 1;
5944 if (s1->c != s2->c)
5945 return 0;
5946 while (s1 != NULL) {
5947 if (s2 == NULL)
5948 return 0;
5949 if (!is_compatible_types(&s1->type, &s2->type))
5950 return 0;
5951 s1 = s1->next;
5952 s2 = s2->next;
5954 if (s2)
5955 return 0;
5956 return 1;
5959 /* return true if type1 and type2 are exactly the same (including
5960 qualifiers).
5962 - enums are not checked as gcc __builtin_types_compatible_p ()
5964 static int is_compatible_types(CType *type1, CType *type2)
5966 int bt1, t1, t2;
5968 t1 = type1->t & VT_TYPE;
5969 t2 = type2->t & VT_TYPE;
5970 /* XXX: bitfields ? */
5971 if (t1 != t2)
5972 return 0;
5973 /* test more complicated cases */
5974 bt1 = t1 & VT_BTYPE;
5975 if (bt1 == VT_PTR) {
5976 type1 = pointed_type(type1);
5977 type2 = pointed_type(type2);
5978 return is_compatible_types(type1, type2);
5979 } else if (bt1 == VT_STRUCT) {
5980 return (type1->ref == type2->ref);
5981 } else if (bt1 == VT_FUNC) {
5982 return is_compatible_func(type1, type2);
5983 } else {
5984 return 1;
5988 /* print a type. If 'varstr' is not NULL, then the variable is also
5989 printed in the type */
5990 /* XXX: union */
5991 /* XXX: add array and function pointers */
5992 void type_to_str(char *buf, int buf_size,
5993 CType *type, const char *varstr)
5995 int bt, v, t;
5996 Sym *s, *sa;
5997 char buf1[256];
5998 const char *tstr;
6000 t = type->t & VT_TYPE;
6001 bt = t & VT_BTYPE;
6002 buf[0] = '\0';
6003 if (t & VT_CONSTANT)
6004 pstrcat(buf, buf_size, "const ");
6005 if (t & VT_VOLATILE)
6006 pstrcat(buf, buf_size, "volatile ");
6007 if (t & VT_UNSIGNED)
6008 pstrcat(buf, buf_size, "unsigned ");
6009 switch(bt) {
6010 case VT_VOID:
6011 tstr = "void";
6012 goto add_tstr;
6013 case VT_BOOL:
6014 tstr = "_Bool";
6015 goto add_tstr;
6016 case VT_BYTE:
6017 tstr = "char";
6018 goto add_tstr;
6019 case VT_SHORT:
6020 tstr = "short";
6021 goto add_tstr;
6022 case VT_INT:
6023 tstr = "int";
6024 goto add_tstr;
6025 case VT_LONG:
6026 tstr = "long";
6027 goto add_tstr;
6028 case VT_LLONG:
6029 tstr = "long long";
6030 goto add_tstr;
6031 case VT_FLOAT:
6032 tstr = "float";
6033 goto add_tstr;
6034 case VT_DOUBLE:
6035 tstr = "double";
6036 goto add_tstr;
6037 case VT_LDOUBLE:
6038 tstr = "long double";
6039 add_tstr:
6040 pstrcat(buf, buf_size, tstr);
6041 break;
6042 case VT_ENUM:
6043 case VT_STRUCT:
6044 if (bt == VT_STRUCT)
6045 tstr = "struct ";
6046 else
6047 tstr = "enum ";
6048 pstrcat(buf, buf_size, tstr);
6049 v = type->ref->v & ~SYM_STRUCT;
6050 if (v >= SYM_FIRST_ANOM)
6051 pstrcat(buf, buf_size, "<anonymous>");
6052 else
6053 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6054 break;
6055 case VT_FUNC:
6056 s = type->ref;
6057 type_to_str(buf, buf_size, &s->type, varstr);
6058 pstrcat(buf, buf_size, "(");
6059 sa = s->next;
6060 while (sa != NULL) {
6061 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6062 pstrcat(buf, buf_size, buf1);
6063 sa = sa->next;
6064 if (sa)
6065 pstrcat(buf, buf_size, ", ");
6067 pstrcat(buf, buf_size, ")");
6068 goto no_var;
6069 case VT_PTR:
6070 s = type->ref;
6071 pstrcpy(buf1, sizeof(buf1), "*");
6072 if (varstr)
6073 pstrcat(buf1, sizeof(buf1), varstr);
6074 type_to_str(buf, buf_size, &s->type, buf1);
6075 goto no_var;
6077 if (varstr) {
6078 pstrcat(buf, buf_size, " ");
6079 pstrcat(buf, buf_size, varstr);
6081 no_var: ;
6084 /* verify type compatibility to store vtop in 'dt' type, and generate
6085 casts if needed. */
6086 static void gen_assign_cast(CType *dt)
6088 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6089 char buf1[256], buf2[256];
6090 int dbt, sbt;
6092 st = &vtop->type; /* source type */
6093 dbt = dt->t & VT_BTYPE;
6094 sbt = st->t & VT_BTYPE;
6095 if (dt->t & VT_CONSTANT)
6096 warning("assignment of read-only location");
6097 switch(dbt) {
6098 case VT_PTR:
6099 /* special cases for pointers */
6100 /* '0' can also be a pointer */
6101 if (is_null_pointer(vtop))
6102 goto type_ok;
6103 /* accept implicit pointer to integer cast with warning */
6104 if (is_integer_btype(sbt)) {
6105 warning("assignment makes pointer from integer without a cast");
6106 goto type_ok;
6108 type1 = pointed_type(dt);
6109 /* a function is implicitely a function pointer */
6110 if (sbt == VT_FUNC) {
6111 if ((type1->t & VT_BTYPE) != VT_VOID &&
6112 !is_compatible_types(pointed_type(dt), st))
6113 goto error;
6114 else
6115 goto type_ok;
6117 if (sbt != VT_PTR)
6118 goto error;
6119 type2 = pointed_type(st);
6120 if ((type1->t & VT_BTYPE) == VT_VOID ||
6121 (type2->t & VT_BTYPE) == VT_VOID) {
6122 /* void * can match anything */
6123 } else {
6124 /* exact type match, except for unsigned */
6125 tmp_type1 = *type1;
6126 tmp_type2 = *type2;
6127 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6128 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6129 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6130 goto error;
6132 /* check const and volatile */
6133 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6134 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6135 warning("assignment discards qualifiers from pointer target type");
6136 break;
6137 case VT_BYTE:
6138 case VT_SHORT:
6139 case VT_INT:
6140 case VT_LLONG:
6141 if (sbt == VT_PTR || sbt == VT_FUNC) {
6142 warning("assignment makes integer from pointer without a cast");
6144 /* XXX: more tests */
6145 break;
6146 case VT_STRUCT:
6147 tmp_type1 = *dt;
6148 tmp_type2 = *st;
6149 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6150 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6151 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6152 error:
6153 type_to_str(buf1, sizeof(buf1), st, NULL);
6154 type_to_str(buf2, sizeof(buf2), dt, NULL);
6155 error("cannot cast '%s' to '%s'", buf1, buf2);
6157 break;
6159 type_ok:
6160 gen_cast(dt);
6163 /* store vtop in lvalue pushed on stack */
6164 void vstore(void)
6166 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6168 ft = vtop[-1].type.t;
6169 sbt = vtop->type.t & VT_BTYPE;
6170 dbt = ft & VT_BTYPE;
6171 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6172 (sbt == VT_INT && dbt == VT_SHORT)) {
6173 /* optimize char/short casts */
6174 delayed_cast = VT_MUSTCAST;
6175 vtop->type.t = ft & VT_TYPE;
6176 /* XXX: factorize */
6177 if (ft & VT_CONSTANT)
6178 warning("assignment of read-only location");
6179 } else {
6180 delayed_cast = 0;
6181 if (!(ft & VT_BITFIELD))
6182 gen_assign_cast(&vtop[-1].type);
6185 if (sbt == VT_STRUCT) {
6186 /* if structure, only generate pointer */
6187 /* structure assignment : generate memcpy */
6188 /* XXX: optimize if small size */
6189 if (!nocode_wanted) {
6190 size = type_size(&vtop->type, &align);
6192 vpush_global_sym(&func_old_type, TOK_memcpy);
6194 /* destination */
6195 vpushv(vtop - 2);
6196 vtop->type.t = VT_INT;
6197 gaddrof();
6198 /* source */
6199 vpushv(vtop - 2);
6200 vtop->type.t = VT_INT;
6201 gaddrof();
6202 /* type size */
6203 vpushi(size);
6204 gfunc_call(3);
6206 vswap();
6207 vpop();
6208 } else {
6209 vswap();
6210 vpop();
6212 /* leave source on stack */
6213 } else if (ft & VT_BITFIELD) {
6214 /* bitfield store handling */
6215 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6216 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6217 /* remove bit field info to avoid loops */
6218 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6220 /* duplicate destination */
6221 vdup();
6222 vtop[-1] = vtop[-2];
6224 /* mask and shift source */
6225 vpushi((1 << bit_size) - 1);
6226 gen_op('&');
6227 vpushi(bit_pos);
6228 gen_op(TOK_SHL);
6229 /* load destination, mask and or with source */
6230 vswap();
6231 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6232 gen_op('&');
6233 gen_op('|');
6234 /* store result */
6235 vstore();
6236 } else {
6237 #ifdef CONFIG_TCC_BCHECK
6238 /* bound check case */
6239 if (vtop[-1].r & VT_MUSTBOUND) {
6240 vswap();
6241 gbound();
6242 vswap();
6244 #endif
6245 if (!nocode_wanted) {
6246 rc = RC_INT;
6247 if (is_float(ft))
6248 rc = RC_FLOAT;
6249 r = gv(rc); /* generate value */
6250 /* if lvalue was saved on stack, must read it */
6251 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6252 SValue sv;
6253 t = get_reg(RC_INT);
6254 sv.type.t = VT_INT;
6255 sv.r = VT_LOCAL | VT_LVAL;
6256 sv.c.ul = vtop[-1].c.ul;
6257 load(t, &sv);
6258 vtop[-1].r = t | VT_LVAL;
6260 store(r, vtop - 1);
6261 /* two word case handling : store second register at word + 4 */
6262 if ((ft & VT_BTYPE) == VT_LLONG) {
6263 vswap();
6264 /* convert to int to increment easily */
6265 vtop->type.t = VT_INT;
6266 gaddrof();
6267 vpushi(4);
6268 gen_op('+');
6269 vtop->r |= VT_LVAL;
6270 vswap();
6271 /* XXX: it works because r2 is spilled last ! */
6272 store(vtop->r2, vtop - 1);
6275 vswap();
6276 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6277 vtop->r |= delayed_cast;
6281 /* post defines POST/PRE add. c is the token ++ or -- */
6282 void inc(int post, int c)
6284 test_lvalue();
6285 vdup(); /* save lvalue */
6286 if (post) {
6287 gv_dup(); /* duplicate value */
6288 vrotb(3);
6289 vrotb(3);
6291 /* add constant */
6292 vpushi(c - TOK_MID);
6293 gen_op('+');
6294 vstore(); /* store value */
6295 if (post)
6296 vpop(); /* if post op, return saved value */
6299 /* Parse GNUC __attribute__ extension. Currently, the following
6300 extensions are recognized:
6301 - aligned(n) : set data/function alignment.
6302 - packed : force data alignment to 1
6303 - section(x) : generate data/code in this section.
6304 - unused : currently ignored, but may be used someday.
6305 - regparm(n) : pass function parameters in registers (i386 only)
6307 static void parse_attribute(AttributeDef *ad)
6309 int t, n;
6311 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6312 next();
6313 skip('(');
6314 skip('(');
6315 while (tok != ')') {
6316 if (tok < TOK_IDENT)
6317 expect("attribute name");
6318 t = tok;
6319 next();
6320 switch(t) {
6321 case TOK_SECTION1:
6322 case TOK_SECTION2:
6323 skip('(');
6324 if (tok != TOK_STR)
6325 expect("section name");
6326 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6327 next();
6328 skip(')');
6329 break;
6330 case TOK_ALIGNED1:
6331 case TOK_ALIGNED2:
6332 if (tok == '(') {
6333 next();
6334 n = expr_const();
6335 if (n <= 0 || (n & (n - 1)) != 0)
6336 error("alignment must be a positive power of two");
6337 skip(')');
6338 } else {
6339 n = MAX_ALIGN;
6341 ad->aligned = n;
6342 break;
6343 case TOK_PACKED1:
6344 case TOK_PACKED2:
6345 ad->packed = 1;
6346 break;
6347 case TOK_UNUSED1:
6348 case TOK_UNUSED2:
6349 /* currently, no need to handle it because tcc does not
6350 track unused objects */
6351 break;
6352 case TOK_NORETURN1:
6353 case TOK_NORETURN2:
6354 /* currently, no need to handle it because tcc does not
6355 track unused objects */
6356 break;
6357 case TOK_CDECL1:
6358 case TOK_CDECL2:
6359 case TOK_CDECL3:
6360 ad->func_call = FUNC_CDECL;
6361 break;
6362 case TOK_STDCALL1:
6363 case TOK_STDCALL2:
6364 case TOK_STDCALL3:
6365 ad->func_call = FUNC_STDCALL;
6366 break;
6367 #ifdef TCC_TARGET_I386
6368 case TOK_REGPARM1:
6369 case TOK_REGPARM2:
6370 skip('(');
6371 n = expr_const();
6372 if (n > 3)
6373 n = 3;
6374 else if (n < 0)
6375 n = 0;
6376 if (n > 0)
6377 ad->func_call = FUNC_FASTCALL1 + n - 1;
6378 skip(')');
6379 break;
6380 #endif
6381 case TOK_DLLEXPORT:
6382 ad->dllexport = 1;
6383 break;
6384 default:
6385 if (tcc_state->warn_unsupported)
6386 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6387 /* skip parameters */
6388 /* XXX: skip parenthesis too */
6389 if (tok == '(') {
6390 next();
6391 while (tok != ')' && tok != -1)
6392 next();
6393 next();
6395 break;
6397 if (tok != ',')
6398 break;
6399 next();
6401 skip(')');
6402 skip(')');
6406 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6407 static void struct_decl(CType *type, int u)
6409 int a, v, size, align, maxalign, c, offset;
6410 int bit_size, bit_pos, bsize, bt, lbit_pos;
6411 Sym *s, *ss, **ps;
6412 AttributeDef ad;
6413 CType type1, btype;
6415 a = tok; /* save decl type */
6416 next();
6417 if (tok != '{') {
6418 v = tok;
6419 next();
6420 /* struct already defined ? return it */
6421 if (v < TOK_IDENT)
6422 expect("struct/union/enum name");
6423 s = struct_find(v);
6424 if (s) {
6425 if (s->type.t != a)
6426 error("invalid type");
6427 goto do_decl;
6429 } else {
6430 v = anon_sym++;
6432 type1.t = a;
6433 /* we put an undefined size for struct/union */
6434 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6435 s->r = 0; /* default alignment is zero as gcc */
6436 /* put struct/union/enum name in type */
6437 do_decl:
6438 type->t = u;
6439 type->ref = s;
6441 if (tok == '{') {
6442 next();
6443 if (s->c != -1)
6444 error("struct/union/enum already defined");
6445 /* cannot be empty */
6446 c = 0;
6447 /* non empty enums are not allowed */
6448 if (a == TOK_ENUM) {
6449 for(;;) {
6450 v = tok;
6451 if (v < TOK_UIDENT)
6452 expect("identifier");
6453 next();
6454 if (tok == '=') {
6455 next();
6456 c = expr_const();
6458 /* enum symbols have static storage */
6459 ss = sym_push(v, &int_type, VT_CONST, c);
6460 ss->type.t |= VT_STATIC;
6461 if (tok != ',')
6462 break;
6463 next();
6464 c++;
6465 /* NOTE: we accept a trailing comma */
6466 if (tok == '}')
6467 break;
6469 skip('}');
6470 } else {
6471 maxalign = 1;
6472 ps = &s->next;
6473 bit_pos = 0;
6474 offset = 0;
6475 while (tok != '}') {
6476 parse_btype(&btype, &ad);
6477 while (1) {
6478 bit_size = -1;
6479 v = 0;
6480 type1 = btype;
6481 if (tok != ':') {
6482 type_decl(&type1, &ad, &v, TYPE_DIRECT);
6483 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6484 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6485 error("invalid type for '%s'",
6486 get_tok_str(v, NULL));
6488 if (tok == ':') {
6489 next();
6490 bit_size = expr_const();
6491 /* XXX: handle v = 0 case for messages */
6492 if (bit_size < 0)
6493 error("negative width in bit-field '%s'",
6494 get_tok_str(v, NULL));
6495 if (v && bit_size == 0)
6496 error("zero width for bit-field '%s'",
6497 get_tok_str(v, NULL));
6499 size = type_size(&type1, &align);
6500 if (ad.aligned) {
6501 if (align < ad.aligned)
6502 align = ad.aligned;
6503 } else if (ad.packed) {
6504 align = 1;
6505 } else if (*tcc_state->pack_stack_ptr) {
6506 if (align > *tcc_state->pack_stack_ptr)
6507 align = *tcc_state->pack_stack_ptr;
6509 lbit_pos = 0;
6510 if (bit_size >= 0) {
6511 bt = type1.t & VT_BTYPE;
6512 if (bt != VT_INT &&
6513 bt != VT_BYTE &&
6514 bt != VT_SHORT &&
6515 bt != VT_BOOL &&
6516 bt != VT_ENUM)
6517 error("bitfields must have scalar type");
6518 bsize = size * 8;
6519 if (bit_size > bsize) {
6520 error("width of '%s' exceeds its type",
6521 get_tok_str(v, NULL));
6522 } else if (bit_size == bsize) {
6523 /* no need for bit fields */
6524 bit_pos = 0;
6525 } else if (bit_size == 0) {
6526 /* XXX: what to do if only padding in a
6527 structure ? */
6528 /* zero size: means to pad */
6529 if (bit_pos > 0)
6530 bit_pos = bsize;
6531 } else {
6532 /* we do not have enough room ? */
6533 if ((bit_pos + bit_size) > bsize)
6534 bit_pos = 0;
6535 lbit_pos = bit_pos;
6536 /* XXX: handle LSB first */
6537 type1.t |= VT_BITFIELD |
6538 (bit_pos << VT_STRUCT_SHIFT) |
6539 (bit_size << (VT_STRUCT_SHIFT + 6));
6540 bit_pos += bit_size;
6542 } else {
6543 bit_pos = 0;
6545 if (v) {
6546 /* add new memory data only if starting
6547 bit field */
6548 if (lbit_pos == 0) {
6549 if (a == TOK_STRUCT) {
6550 c = (c + align - 1) & -align;
6551 offset = c;
6552 c += size;
6553 } else {
6554 offset = 0;
6555 if (size > c)
6556 c = size;
6558 if (align > maxalign)
6559 maxalign = align;
6561 #if 0
6562 printf("add field %s offset=%d",
6563 get_tok_str(v, NULL), offset);
6564 if (type1.t & VT_BITFIELD) {
6565 printf(" pos=%d size=%d",
6566 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6567 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6569 printf("\n");
6570 #endif
6571 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6572 *ps = ss;
6573 ps = &ss->next;
6575 if (tok == ';' || tok == TOK_EOF)
6576 break;
6577 skip(',');
6579 skip(';');
6581 skip('}');
6582 /* store size and alignment */
6583 s->c = (c + maxalign - 1) & -maxalign;
6584 s->r = maxalign;
6589 /* return 0 if no type declaration. otherwise, return the basic type
6590 and skip it.
6592 static int parse_btype(CType *type, AttributeDef *ad)
6594 int t, u, type_found, typespec_found;
6595 Sym *s;
6596 CType type1;
6598 memset(ad, 0, sizeof(AttributeDef));
6599 type_found = 0;
6600 typespec_found = 0;
6601 t = 0;
6602 while(1) {
6603 switch(tok) {
6604 case TOK_EXTENSION:
6605 /* currently, we really ignore extension */
6606 next();
6607 continue;
6609 /* basic types */
6610 case TOK_CHAR:
6611 u = VT_BYTE;
6612 basic_type:
6613 next();
6614 basic_type1:
6615 if ((t & VT_BTYPE) != 0)
6616 error("too many basic types");
6617 t |= u;
6618 typespec_found = 1;
6619 break;
6620 case TOK_VOID:
6621 u = VT_VOID;
6622 goto basic_type;
6623 case TOK_SHORT:
6624 u = VT_SHORT;
6625 goto basic_type;
6626 case TOK_INT:
6627 next();
6628 typespec_found = 1;
6629 break;
6630 case TOK_LONG:
6631 next();
6632 if ((t & VT_BTYPE) == VT_DOUBLE) {
6633 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6634 } else if ((t & VT_BTYPE) == VT_LONG) {
6635 t = (t & ~VT_BTYPE) | VT_LLONG;
6636 } else {
6637 u = VT_LONG;
6638 goto basic_type1;
6640 break;
6641 case TOK_BOOL:
6642 u = VT_BOOL;
6643 goto basic_type;
6644 case TOK_FLOAT:
6645 u = VT_FLOAT;
6646 goto basic_type;
6647 case TOK_DOUBLE:
6648 next();
6649 if ((t & VT_BTYPE) == VT_LONG) {
6650 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6651 } else {
6652 u = VT_DOUBLE;
6653 goto basic_type1;
6655 break;
6656 case TOK_ENUM:
6657 struct_decl(&type1, VT_ENUM);
6658 basic_type2:
6659 u = type1.t;
6660 type->ref = type1.ref;
6661 goto basic_type1;
6662 case TOK_STRUCT:
6663 case TOK_UNION:
6664 struct_decl(&type1, VT_STRUCT);
6665 goto basic_type2;
6667 /* type modifiers */
6668 case TOK_CONST1:
6669 case TOK_CONST2:
6670 case TOK_CONST3:
6671 t |= VT_CONSTANT;
6672 next();
6673 break;
6674 case TOK_VOLATILE1:
6675 case TOK_VOLATILE2:
6676 case TOK_VOLATILE3:
6677 t |= VT_VOLATILE;
6678 next();
6679 break;
6680 case TOK_SIGNED1:
6681 case TOK_SIGNED2:
6682 case TOK_SIGNED3:
6683 typespec_found = 1;
6684 t |= VT_SIGNED;
6685 next();
6686 break;
6687 case TOK_REGISTER:
6688 case TOK_AUTO:
6689 case TOK_RESTRICT1:
6690 case TOK_RESTRICT2:
6691 case TOK_RESTRICT3:
6692 next();
6693 break;
6694 case TOK_UNSIGNED:
6695 t |= VT_UNSIGNED;
6696 next();
6697 typespec_found = 1;
6698 break;
6700 /* storage */
6701 case TOK_EXTERN:
6702 t |= VT_EXTERN;
6703 next();
6704 break;
6705 case TOK_STATIC:
6706 t |= VT_STATIC;
6707 next();
6708 break;
6709 case TOK_TYPEDEF:
6710 t |= VT_TYPEDEF;
6711 next();
6712 break;
6713 case TOK_INLINE1:
6714 case TOK_INLINE2:
6715 case TOK_INLINE3:
6716 t |= VT_INLINE;
6717 next();
6718 break;
6720 /* GNUC attribute */
6721 case TOK_ATTRIBUTE1:
6722 case TOK_ATTRIBUTE2:
6723 parse_attribute(ad);
6724 break;
6725 /* GNUC typeof */
6726 case TOK_TYPEOF1:
6727 case TOK_TYPEOF2:
6728 case TOK_TYPEOF3:
6729 next();
6730 parse_expr_type(&type1);
6731 goto basic_type2;
6732 default:
6733 if (typespec_found)
6734 goto the_end;
6735 s = sym_find(tok);
6736 if (!s || !(s->type.t & VT_TYPEDEF))
6737 goto the_end;
6738 t |= (s->type.t & ~VT_TYPEDEF);
6739 type->ref = s->type.ref;
6740 next();
6741 break;
6743 type_found = 1;
6745 the_end:
6746 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
6747 error("signed and unsigned modifier");
6748 if (tcc_state->char_is_unsigned) {
6749 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
6750 t |= VT_UNSIGNED;
6752 t &= ~VT_SIGNED;
6754 /* long is never used as type */
6755 if ((t & VT_BTYPE) == VT_LONG)
6756 t = (t & ~VT_BTYPE) | VT_INT;
6757 type->t = t;
6758 return type_found;
6761 /* convert a function parameter type (array to pointer and function to
6762 function pointer) */
6763 static inline void convert_parameter_type(CType *pt)
6765 /* remove const and volatile qualifiers (XXX: const could be used
6766 to indicate a const function parameter */
6767 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
6768 /* array must be transformed to pointer according to ANSI C */
6769 pt->t &= ~VT_ARRAY;
6770 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6771 mk_pointer(pt);
6775 static void post_type(CType *type, AttributeDef *ad)
6777 int n, l, t1;
6778 Sym **plast, *s, *first;
6779 AttributeDef ad1;
6780 CType pt;
6782 if (tok == '(') {
6783 /* function declaration */
6784 next();
6785 l = 0;
6786 first = NULL;
6787 plast = &first;
6788 while (tok != ')') {
6789 /* read param name and compute offset */
6790 if (l != FUNC_OLD) {
6791 if (!parse_btype(&pt, &ad1)) {
6792 if (l) {
6793 error("invalid type");
6794 } else {
6795 l = FUNC_OLD;
6796 goto old_proto;
6799 l = FUNC_NEW;
6800 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6801 break;
6802 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6803 if ((pt.t & VT_BTYPE) == VT_VOID)
6804 error("parameter declared as void");
6805 } else {
6806 old_proto:
6807 n = tok;
6808 pt.t = VT_INT;
6809 next();
6811 convert_parameter_type(&pt);
6812 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6813 *plast = s;
6814 plast = &s->next;
6815 if (tok == ',') {
6816 next();
6817 if (l == FUNC_NEW && tok == TOK_DOTS) {
6818 l = FUNC_ELLIPSIS;
6819 next();
6820 break;
6824 /* if no parameters, then old type prototype */
6825 if (l == 0)
6826 l = FUNC_OLD;
6827 skip(')');
6828 t1 = type->t & VT_STORAGE;
6829 /* NOTE: const is ignored in returned type as it has a special
6830 meaning in gcc / C++ */
6831 type->t &= ~(VT_STORAGE | VT_CONSTANT);
6832 post_type(type, ad);
6833 /* we push a anonymous symbol which will contain the function prototype */
6834 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6835 s->next = first;
6836 type->t = t1 | VT_FUNC;
6837 type->ref = s;
6838 } else if (tok == '[') {
6839 /* array definition */
6840 next();
6841 n = -1;
6842 if (tok != ']') {
6843 n = expr_const();
6844 if (n < 0)
6845 error("invalid array size");
6847 skip(']');
6848 /* parse next post type */
6849 t1 = type->t & VT_STORAGE;
6850 type->t &= ~VT_STORAGE;
6851 post_type(type, ad);
6853 /* we push a anonymous symbol which will contain the array
6854 element type */
6855 s = sym_push(SYM_FIELD, type, 0, n);
6856 type->t = t1 | VT_ARRAY | VT_PTR;
6857 type->ref = s;
6861 /* Parse a type declaration (except basic type), and return the type
6862 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6863 expected. 'type' should contain the basic type. 'ad' is the
6864 attribute definition of the basic type. It can be modified by
6865 type_decl().
6867 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6869 Sym *s;
6870 CType type1, *type2;
6871 int qualifiers;
6873 while (tok == '*') {
6874 qualifiers = 0;
6875 redo:
6876 next();
6877 switch(tok) {
6878 case TOK_CONST1:
6879 case TOK_CONST2:
6880 case TOK_CONST3:
6881 qualifiers |= VT_CONSTANT;
6882 goto redo;
6883 case TOK_VOLATILE1:
6884 case TOK_VOLATILE2:
6885 case TOK_VOLATILE3:
6886 qualifiers |= VT_VOLATILE;
6887 goto redo;
6888 case TOK_RESTRICT1:
6889 case TOK_RESTRICT2:
6890 case TOK_RESTRICT3:
6891 goto redo;
6893 mk_pointer(type);
6894 type->t |= qualifiers;
6897 /* XXX: clarify attribute handling */
6898 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6899 parse_attribute(ad);
6901 /* recursive type */
6902 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6903 type1.t = 0; /* XXX: same as int */
6904 if (tok == '(') {
6905 next();
6906 /* XXX: this is not correct to modify 'ad' at this point, but
6907 the syntax is not clear */
6908 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6909 parse_attribute(ad);
6910 type_decl(&type1, ad, v, td);
6911 skip(')');
6912 } else {
6913 /* type identifier */
6914 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6915 *v = tok;
6916 next();
6917 } else {
6918 if (!(td & TYPE_ABSTRACT))
6919 expect("identifier");
6920 *v = 0;
6923 post_type(type, ad);
6924 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6925 parse_attribute(ad);
6926 if (!type1.t)
6927 return;
6928 /* append type at the end of type1 */
6929 type2 = &type1;
6930 for(;;) {
6931 s = type2->ref;
6932 type2 = &s->type;
6933 if (!type2->t) {
6934 *type2 = *type;
6935 break;
6938 *type = type1;
6941 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6942 static int lvalue_type(int t)
6944 int bt, r;
6945 r = VT_LVAL;
6946 bt = t & VT_BTYPE;
6947 if (bt == VT_BYTE || bt == VT_BOOL)
6948 r |= VT_LVAL_BYTE;
6949 else if (bt == VT_SHORT)
6950 r |= VT_LVAL_SHORT;
6951 else
6952 return r;
6953 if (t & VT_UNSIGNED)
6954 r |= VT_LVAL_UNSIGNED;
6955 return r;
6958 /* indirection with full error checking and bound check */
6959 static void indir(void)
6961 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6962 expect("pointer");
6963 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6964 gv(RC_INT);
6965 vtop->type = *pointed_type(&vtop->type);
6966 /* an array is never an lvalue */
6967 if (!(vtop->type.t & VT_ARRAY)) {
6968 vtop->r |= lvalue_type(vtop->type.t);
6969 /* if bound checking, the referenced pointer must be checked */
6970 if (do_bounds_check)
6971 vtop->r |= VT_MUSTBOUND;
6975 /* pass a parameter to a function and do type checking and casting */
6976 static void gfunc_param_typed(Sym *func, Sym *arg)
6978 int func_type;
6979 CType type;
6981 func_type = func->c;
6982 if (func_type == FUNC_OLD ||
6983 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6984 /* default casting : only need to convert float to double */
6985 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6986 type.t = VT_DOUBLE;
6987 gen_cast(&type);
6989 } else if (arg == NULL) {
6990 error("too many arguments to function");
6991 } else {
6992 type = arg->type;
6993 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
6994 gen_assign_cast(&type);
6998 /* parse an expression of the form '(type)' or '(expr)' and return its
6999 type */
7000 static void parse_expr_type(CType *type)
7002 int n;
7003 AttributeDef ad;
7005 skip('(');
7006 if (parse_btype(type, &ad)) {
7007 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7008 } else {
7009 expr_type(type);
7011 skip(')');
7014 static void parse_type(CType *type)
7016 AttributeDef ad;
7017 int n;
7019 if (!parse_btype(type, &ad)) {
7020 expect("type");
7022 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7025 static void vpush_tokc(int t)
7027 CType type;
7028 type.t = t;
7029 vsetc(&type, VT_CONST, &tokc);
7032 static void unary(void)
7034 int n, t, align, size, r;
7035 CType type;
7036 Sym *s;
7037 AttributeDef ad;
7039 /* XXX: GCC 2.95.3 does not generate a table although it should be
7040 better here */
7041 tok_next:
7042 switch(tok) {
7043 case TOK_EXTENSION:
7044 next();
7045 goto tok_next;
7046 case TOK_CINT:
7047 case TOK_CCHAR:
7048 case TOK_LCHAR:
7049 vpushi(tokc.i);
7050 next();
7051 break;
7052 case TOK_CUINT:
7053 vpush_tokc(VT_INT | VT_UNSIGNED);
7054 next();
7055 break;
7056 case TOK_CLLONG:
7057 vpush_tokc(VT_LLONG);
7058 next();
7059 break;
7060 case TOK_CULLONG:
7061 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7062 next();
7063 break;
7064 case TOK_CFLOAT:
7065 vpush_tokc(VT_FLOAT);
7066 next();
7067 break;
7068 case TOK_CDOUBLE:
7069 vpush_tokc(VT_DOUBLE);
7070 next();
7071 break;
7072 case TOK_CLDOUBLE:
7073 vpush_tokc(VT_LDOUBLE);
7074 next();
7075 break;
7076 case TOK___FUNCTION__:
7077 if (!gnu_ext)
7078 goto tok_identifier;
7079 /* fall thru */
7080 case TOK___FUNC__:
7082 void *ptr;
7083 int len;
7084 /* special function name identifier */
7085 len = strlen(funcname) + 1;
7086 /* generate char[len] type */
7087 type.t = VT_BYTE;
7088 mk_pointer(&type);
7089 type.t |= VT_ARRAY;
7090 type.ref->c = len;
7091 vpush_ref(&type, data_section, data_section->data_offset, len);
7092 ptr = section_ptr_add(data_section, len);
7093 memcpy(ptr, funcname, len);
7094 next();
7096 break;
7097 case TOK_LSTR:
7098 t = VT_INT;
7099 goto str_init;
7100 case TOK_STR:
7101 /* string parsing */
7102 t = VT_BYTE;
7103 str_init:
7104 if (tcc_state->warn_write_strings)
7105 t |= VT_CONSTANT;
7106 type.t = t;
7107 mk_pointer(&type);
7108 type.t |= VT_ARRAY;
7109 memset(&ad, 0, sizeof(AttributeDef));
7110 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7111 break;
7112 case '(':
7113 next();
7114 /* cast ? */
7115 if (parse_btype(&type, &ad)) {
7116 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7117 skip(')');
7118 /* check ISOC99 compound literal */
7119 if (tok == '{') {
7120 /* data is allocated locally by default */
7121 if (global_expr)
7122 r = VT_CONST;
7123 else
7124 r = VT_LOCAL;
7125 /* all except arrays are lvalues */
7126 if (!(type.t & VT_ARRAY))
7127 r |= lvalue_type(type.t);
7128 memset(&ad, 0, sizeof(AttributeDef));
7129 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7130 } else {
7131 unary();
7132 gen_cast(&type);
7134 } else if (tok == '{') {
7135 /* save all registers */
7136 save_regs(0);
7137 /* statement expression : we do not accept break/continue
7138 inside as GCC does */
7139 block(NULL, NULL, NULL, NULL, 0, 1);
7140 skip(')');
7141 } else {
7142 gexpr();
7143 skip(')');
7145 break;
7146 case '*':
7147 next();
7148 unary();
7149 indir();
7150 break;
7151 case '&':
7152 next();
7153 unary();
7154 /* functions names must be treated as function pointers,
7155 except for unary '&' and sizeof. Since we consider that
7156 functions are not lvalues, we only have to handle it
7157 there and in function calls. */
7158 /* arrays can also be used although they are not lvalues */
7159 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7160 !(vtop->type.t & VT_ARRAY))
7161 test_lvalue();
7162 mk_pointer(&vtop->type);
7163 gaddrof();
7164 break;
7165 case '!':
7166 next();
7167 unary();
7168 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
7169 vtop->c.i = !vtop->c.i;
7170 else if ((vtop->r & VT_VALMASK) == VT_CMP)
7171 vtop->c.i = vtop->c.i ^ 1;
7172 else
7173 vseti(VT_JMP, gtst(1, 0));
7174 break;
7175 case '~':
7176 next();
7177 unary();
7178 vpushi(-1);
7179 gen_op('^');
7180 break;
7181 case '+':
7182 next();
7183 /* in order to force cast, we add zero */
7184 unary();
7185 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7186 error("pointer not accepted for unary plus");
7187 vpushi(0);
7188 gen_op('+');
7189 break;
7190 case TOK_SIZEOF:
7191 case TOK_ALIGNOF1:
7192 case TOK_ALIGNOF2:
7193 t = tok;
7194 next();
7195 if (tok == '(') {
7196 parse_expr_type(&type);
7197 } else {
7198 unary_type(&type);
7200 size = type_size(&type, &align);
7201 if (t == TOK_SIZEOF) {
7202 if (size < 0)
7203 error("sizeof applied to an incomplete type");
7204 vpushi(size);
7205 } else {
7206 vpushi(align);
7208 break;
7210 case TOK_builtin_types_compatible_p:
7212 CType type1, type2;
7213 next();
7214 skip('(');
7215 parse_type(&type1);
7216 skip(',');
7217 parse_type(&type2);
7218 skip(')');
7219 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7220 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7221 vpushi(is_compatible_types(&type1, &type2));
7223 break;
7224 case TOK_builtin_constant_p:
7226 int saved_nocode_wanted, res;
7227 next();
7228 skip('(');
7229 saved_nocode_wanted = nocode_wanted;
7230 nocode_wanted = 1;
7231 gexpr();
7232 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7233 vpop();
7234 nocode_wanted = saved_nocode_wanted;
7235 skip(')');
7236 vpushi(res);
7238 break;
7239 case TOK_INC:
7240 case TOK_DEC:
7241 t = tok;
7242 next();
7243 unary();
7244 inc(0, t);
7245 break;
7246 case '-':
7247 next();
7248 vpushi(0);
7249 unary();
7250 gen_op('-');
7251 break;
7252 case TOK_LAND:
7253 if (!gnu_ext)
7254 goto tok_identifier;
7255 next();
7256 /* allow to take the address of a label */
7257 if (tok < TOK_UIDENT)
7258 expect("label identifier");
7259 s = label_find(tok);
7260 if (!s) {
7261 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7262 } else {
7263 if (s->r == LABEL_DECLARED)
7264 s->r = LABEL_FORWARD;
7266 if (!s->type.t) {
7267 s->type.t = VT_VOID;
7268 mk_pointer(&s->type);
7269 s->type.t |= VT_STATIC;
7271 vset(&s->type, VT_CONST | VT_SYM, 0);
7272 vtop->sym = s;
7273 next();
7274 break;
7275 default:
7276 tok_identifier:
7277 t = tok;
7278 next();
7279 if (t < TOK_UIDENT)
7280 expect("identifier");
7281 s = sym_find(t);
7282 if (!s) {
7283 if (tok != '(')
7284 error("'%s' undeclared", get_tok_str(t, NULL));
7285 /* for simple function calls, we tolerate undeclared
7286 external reference to int() function */
7287 if (tcc_state->warn_implicit_function_declaration)
7288 warning("implicit declaration of function '%s'",
7289 get_tok_str(t, NULL));
7290 s = external_global_sym(t, &func_old_type, 0);
7292 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7293 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7294 /* if referencing an inline function, then we generate a
7295 symbol to it if not already done. It will have the
7296 effect to generate code for it at the end of the
7297 compilation unit. Inline function as always
7298 generated in the text section. */
7299 if (!s->c)
7300 put_extern_sym(s, text_section, 0, 0);
7301 r = VT_SYM | VT_CONST;
7302 } else {
7303 r = s->r;
7305 vset(&s->type, r, s->c);
7306 /* if forward reference, we must point to s */
7307 if (vtop->r & VT_SYM) {
7308 vtop->sym = s;
7309 vtop->c.ul = 0;
7311 break;
7314 /* post operations */
7315 while (1) {
7316 if (tok == TOK_INC || tok == TOK_DEC) {
7317 inc(1, tok);
7318 next();
7319 } else if (tok == '.' || tok == TOK_ARROW) {
7320 /* field */
7321 if (tok == TOK_ARROW)
7322 indir();
7323 test_lvalue();
7324 gaddrof();
7325 next();
7326 /* expect pointer on structure */
7327 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7328 expect("struct or union");
7329 s = vtop->type.ref;
7330 /* find field */
7331 tok |= SYM_FIELD;
7332 while ((s = s->next) != NULL) {
7333 if (s->v == tok)
7334 break;
7336 if (!s)
7337 error("field not found");
7338 /* add field offset to pointer */
7339 vtop->type = char_pointer_type; /* change type to 'char *' */
7340 vpushi(s->c);
7341 gen_op('+');
7342 /* change type to field type, and set to lvalue */
7343 vtop->type = s->type;
7344 /* an array is never an lvalue */
7345 if (!(vtop->type.t & VT_ARRAY)) {
7346 vtop->r |= lvalue_type(vtop->type.t);
7347 /* if bound checking, the referenced pointer must be checked */
7348 if (do_bounds_check)
7349 vtop->r |= VT_MUSTBOUND;
7351 next();
7352 } else if (tok == '[') {
7353 next();
7354 gexpr();
7355 gen_op('+');
7356 indir();
7357 skip(']');
7358 } else if (tok == '(') {
7359 SValue ret;
7360 Sym *sa;
7361 int nb_args;
7363 /* function call */
7364 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7365 /* pointer test (no array accepted) */
7366 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7367 vtop->type = *pointed_type(&vtop->type);
7368 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7369 goto error_func;
7370 } else {
7371 error_func:
7372 expect("function pointer");
7374 } else {
7375 vtop->r &= ~VT_LVAL; /* no lvalue */
7377 /* get return type */
7378 s = vtop->type.ref;
7379 next();
7380 sa = s->next; /* first parameter */
7381 nb_args = 0;
7382 /* compute first implicit argument if a structure is returned */
7383 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7384 /* get some space for the returned structure */
7385 size = type_size(&s->type, &align);
7386 loc = (loc - size) & -align;
7387 ret.type = s->type;
7388 ret.r = VT_LOCAL | VT_LVAL;
7389 /* pass it as 'int' to avoid structure arg passing
7390 problems */
7391 vseti(VT_LOCAL, loc);
7392 ret.c = vtop->c;
7393 nb_args++;
7394 } else {
7395 ret.type = s->type;
7396 ret.r2 = VT_CONST;
7397 /* return in register */
7398 if (is_float(ret.type.t)) {
7399 ret.r = REG_FRET;
7400 } else {
7401 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7402 ret.r2 = REG_LRET;
7403 ret.r = REG_IRET;
7405 ret.c.i = 0;
7407 if (tok != ')') {
7408 for(;;) {
7409 expr_eq();
7410 gfunc_param_typed(s, sa);
7411 nb_args++;
7412 if (sa)
7413 sa = sa->next;
7414 if (tok == ')')
7415 break;
7416 skip(',');
7419 if (sa)
7420 error("too few arguments to function");
7421 skip(')');
7422 if (!nocode_wanted) {
7423 gfunc_call(nb_args);
7424 } else {
7425 vtop -= (nb_args + 1);
7427 /* return value */
7428 vsetc(&ret.type, ret.r, &ret.c);
7429 vtop->r2 = ret.r2;
7430 } else {
7431 break;
7436 static void uneq(void)
7438 int t;
7440 unary();
7441 if (tok == '=' ||
7442 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7443 tok == TOK_A_XOR || tok == TOK_A_OR ||
7444 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7445 test_lvalue();
7446 t = tok;
7447 next();
7448 if (t == '=') {
7449 expr_eq();
7450 } else {
7451 vdup();
7452 expr_eq();
7453 gen_op(t & 0x7f);
7455 vstore();
7459 static void expr_prod(void)
7461 int t;
7463 uneq();
7464 while (tok == '*' || tok == '/' || tok == '%') {
7465 t = tok;
7466 next();
7467 uneq();
7468 gen_op(t);
7472 static void expr_sum(void)
7474 int t;
7476 expr_prod();
7477 while (tok == '+' || tok == '-') {
7478 t = tok;
7479 next();
7480 expr_prod();
7481 gen_op(t);
7485 static void expr_shift(void)
7487 int t;
7489 expr_sum();
7490 while (tok == TOK_SHL || tok == TOK_SAR) {
7491 t = tok;
7492 next();
7493 expr_sum();
7494 gen_op(t);
7498 static void expr_cmp(void)
7500 int t;
7502 expr_shift();
7503 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7504 tok == TOK_ULT || tok == TOK_UGE) {
7505 t = tok;
7506 next();
7507 expr_shift();
7508 gen_op(t);
7512 static void expr_cmpeq(void)
7514 int t;
7516 expr_cmp();
7517 while (tok == TOK_EQ || tok == TOK_NE) {
7518 t = tok;
7519 next();
7520 expr_cmp();
7521 gen_op(t);
7525 static void expr_and(void)
7527 expr_cmpeq();
7528 while (tok == '&') {
7529 next();
7530 expr_cmpeq();
7531 gen_op('&');
7535 static void expr_xor(void)
7537 expr_and();
7538 while (tok == '^') {
7539 next();
7540 expr_and();
7541 gen_op('^');
7545 static void expr_or(void)
7547 expr_xor();
7548 while (tok == '|') {
7549 next();
7550 expr_xor();
7551 gen_op('|');
7555 /* XXX: fix this mess */
7556 static void expr_land_const(void)
7558 expr_or();
7559 while (tok == TOK_LAND) {
7560 next();
7561 expr_or();
7562 gen_op(TOK_LAND);
7566 /* XXX: fix this mess */
7567 static void expr_lor_const(void)
7569 expr_land_const();
7570 while (tok == TOK_LOR) {
7571 next();
7572 expr_land_const();
7573 gen_op(TOK_LOR);
7577 /* only used if non constant */
7578 static void expr_land(void)
7580 int t;
7582 expr_or();
7583 if (tok == TOK_LAND) {
7584 t = 0;
7585 for(;;) {
7586 t = gtst(1, t);
7587 if (tok != TOK_LAND) {
7588 vseti(VT_JMPI, t);
7589 break;
7591 next();
7592 expr_or();
7597 static void expr_lor(void)
7599 int t;
7601 expr_land();
7602 if (tok == TOK_LOR) {
7603 t = 0;
7604 for(;;) {
7605 t = gtst(0, t);
7606 if (tok != TOK_LOR) {
7607 vseti(VT_JMP, t);
7608 break;
7610 next();
7611 expr_land();
7616 /* XXX: better constant handling */
7617 static void expr_eq(void)
7619 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7620 SValue sv;
7621 CType type, type1, type2;
7623 if (const_wanted) {
7624 int c1, c;
7625 expr_lor_const();
7626 if (tok == '?') {
7627 c = vtop->c.i;
7628 vpop();
7629 next();
7630 if (tok == ':' && gnu_ext) {
7631 c1 = c;
7632 } else {
7633 gexpr();
7634 c1 = vtop->c.i;
7635 vpop();
7637 skip(':');
7638 expr_eq();
7639 if (c)
7640 vtop->c.i = c1;
7642 } else {
7643 expr_lor();
7644 if (tok == '?') {
7645 next();
7646 if (vtop != vstack) {
7647 /* needed to avoid having different registers saved in
7648 each branch */
7649 if (is_float(vtop->type.t))
7650 rc = RC_FLOAT;
7651 else
7652 rc = RC_INT;
7653 gv(rc);
7654 save_regs(1);
7656 if (tok == ':' && gnu_ext) {
7657 gv_dup();
7658 tt = gtst(1, 0);
7659 } else {
7660 tt = gtst(1, 0);
7661 gexpr();
7663 type1 = vtop->type;
7664 sv = *vtop; /* save value to handle it later */
7665 vtop--; /* no vpop so that FP stack is not flushed */
7666 skip(':');
7667 u = gjmp(0);
7668 gsym(tt);
7669 expr_eq();
7670 type2 = vtop->type;
7672 t1 = type1.t;
7673 bt1 = t1 & VT_BTYPE;
7674 t2 = type2.t;
7675 bt2 = t2 & VT_BTYPE;
7676 /* cast operands to correct type according to ISOC rules */
7677 if (is_float(bt1) || is_float(bt2)) {
7678 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7679 type.t = VT_LDOUBLE;
7680 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7681 type.t = VT_DOUBLE;
7682 } else {
7683 type.t = VT_FLOAT;
7685 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7686 /* cast to biggest op */
7687 type.t = VT_LLONG;
7688 /* convert to unsigned if it does not fit in a long long */
7689 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7690 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7691 type.t |= VT_UNSIGNED;
7692 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7693 /* XXX: test pointer compatibility */
7694 type = type1;
7695 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7696 /* XXX: test structure compatibility */
7697 type = type1;
7698 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7699 /* NOTE: as an extension, we accept void on only one side */
7700 type.t = VT_VOID;
7701 } else {
7702 /* integer operations */
7703 type.t = VT_INT;
7704 /* convert to unsigned if it does not fit in an integer */
7705 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7706 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7707 type.t |= VT_UNSIGNED;
7710 /* now we convert second operand */
7711 gen_cast(&type);
7712 rc = RC_INT;
7713 if (is_float(type.t)) {
7714 rc = RC_FLOAT;
7715 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7716 /* for long longs, we use fixed registers to avoid having
7717 to handle a complicated move */
7718 rc = RC_IRET;
7721 r2 = gv(rc);
7722 /* this is horrible, but we must also convert first
7723 operand */
7724 tt = gjmp(0);
7725 gsym(u);
7726 /* put again first value and cast it */
7727 *vtop = sv;
7728 gen_cast(&type);
7729 r1 = gv(rc);
7730 move_reg(r2, r1);
7731 vtop->r = r2;
7732 gsym(tt);
7737 static void gexpr(void)
7739 while (1) {
7740 expr_eq();
7741 if (tok != ',')
7742 break;
7743 vpop();
7744 next();
7748 /* parse an expression and return its type without any side effect. */
7749 static void expr_type(CType *type)
7751 int saved_nocode_wanted;
7753 saved_nocode_wanted = nocode_wanted;
7754 nocode_wanted = 1;
7755 gexpr();
7756 *type = vtop->type;
7757 vpop();
7758 nocode_wanted = saved_nocode_wanted;
7761 /* parse a unary expression and return its type without any side
7762 effect. */
7763 static void unary_type(CType *type)
7765 int a;
7767 a = nocode_wanted;
7768 nocode_wanted = 1;
7769 unary();
7770 *type = vtop->type;
7771 vpop();
7772 nocode_wanted = a;
7775 /* parse a constant expression and return value in vtop. */
7776 static void expr_const1(void)
7778 int a;
7779 a = const_wanted;
7780 const_wanted = 1;
7781 expr_eq();
7782 const_wanted = a;
7785 /* parse an integer constant and return its value. */
7786 static int expr_const(void)
7788 int c;
7789 expr_const1();
7790 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7791 expect("constant expression");
7792 c = vtop->c.i;
7793 vpop();
7794 return c;
7797 /* return the label token if current token is a label, otherwise
7798 return zero */
7799 static int is_label(void)
7801 int last_tok;
7803 /* fast test first */
7804 if (tok < TOK_UIDENT)
7805 return 0;
7806 /* no need to save tokc because tok is an identifier */
7807 last_tok = tok;
7808 next();
7809 if (tok == ':') {
7810 next();
7811 return last_tok;
7812 } else {
7813 unget_tok(last_tok);
7814 return 0;
7818 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7819 int case_reg, int is_expr)
7821 int a, b, c, d;
7822 Sym *s;
7824 /* generate line number info */
7825 if (do_debug &&
7826 (last_line_num != file->line_num || last_ind != ind)) {
7827 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7828 last_ind = ind;
7829 last_line_num = file->line_num;
7832 if (is_expr) {
7833 /* default return value is (void) */
7834 vpushi(0);
7835 vtop->type.t = VT_VOID;
7838 if (tok == TOK_IF) {
7839 /* if test */
7840 next();
7841 skip('(');
7842 gexpr();
7843 skip(')');
7844 a = gtst(1, 0);
7845 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7846 c = tok;
7847 if (c == TOK_ELSE) {
7848 next();
7849 d = gjmp(0);
7850 gsym(a);
7851 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7852 gsym(d); /* patch else jmp */
7853 } else
7854 gsym(a);
7855 } else if (tok == TOK_WHILE) {
7856 next();
7857 d = ind;
7858 skip('(');
7859 gexpr();
7860 skip(')');
7861 a = gtst(1, 0);
7862 b = 0;
7863 block(&a, &b, case_sym, def_sym, case_reg, 0);
7864 gjmp_addr(d);
7865 gsym(a);
7866 gsym_addr(b, d);
7867 } else if (tok == '{') {
7868 Sym *llabel;
7870 next();
7871 /* record local declaration stack position */
7872 s = local_stack;
7873 llabel = local_label_stack;
7874 /* handle local labels declarations */
7875 if (tok == TOK_LABEL) {
7876 next();
7877 for(;;) {
7878 if (tok < TOK_UIDENT)
7879 expect("label identifier");
7880 label_push(&local_label_stack, tok, LABEL_DECLARED);
7881 next();
7882 if (tok == ',') {
7883 next();
7884 } else {
7885 skip(';');
7886 break;
7890 while (tok != '}') {
7891 decl(VT_LOCAL);
7892 if (tok != '}') {
7893 if (is_expr)
7894 vpop();
7895 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7898 /* pop locally defined labels */
7899 label_pop(&local_label_stack, llabel);
7900 /* pop locally defined symbols */
7901 sym_pop(&local_stack, s);
7902 next();
7903 } else if (tok == TOK_RETURN) {
7904 next();
7905 if (tok != ';') {
7906 gexpr();
7907 gen_assign_cast(&func_vt);
7908 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7909 CType type;
7910 /* if returning structure, must copy it to implicit
7911 first pointer arg location */
7912 type = func_vt;
7913 mk_pointer(&type);
7914 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7915 indir();
7916 vswap();
7917 /* copy structure value to pointer */
7918 vstore();
7919 } else if (is_float(func_vt.t)) {
7920 gv(RC_FRET);
7921 } else {
7922 gv(RC_IRET);
7924 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7926 skip(';');
7927 rsym = gjmp(rsym); /* jmp */
7928 } else if (tok == TOK_BREAK) {
7929 /* compute jump */
7930 if (!bsym)
7931 error("cannot break");
7932 *bsym = gjmp(*bsym);
7933 next();
7934 skip(';');
7935 } else if (tok == TOK_CONTINUE) {
7936 /* compute jump */
7937 if (!csym)
7938 error("cannot continue");
7939 *csym = gjmp(*csym);
7940 next();
7941 skip(';');
7942 } else if (tok == TOK_FOR) {
7943 int e;
7944 next();
7945 skip('(');
7946 if (tok != ';') {
7947 gexpr();
7948 vpop();
7950 skip(';');
7951 d = ind;
7952 c = ind;
7953 a = 0;
7954 b = 0;
7955 if (tok != ';') {
7956 gexpr();
7957 a = gtst(1, 0);
7959 skip(';');
7960 if (tok != ')') {
7961 e = gjmp(0);
7962 c = ind;
7963 gexpr();
7964 vpop();
7965 gjmp_addr(d);
7966 gsym(e);
7968 skip(')');
7969 block(&a, &b, case_sym, def_sym, case_reg, 0);
7970 gjmp_addr(c);
7971 gsym(a);
7972 gsym_addr(b, c);
7973 } else
7974 if (tok == TOK_DO) {
7975 next();
7976 a = 0;
7977 b = 0;
7978 d = ind;
7979 block(&a, &b, case_sym, def_sym, case_reg, 0);
7980 skip(TOK_WHILE);
7981 skip('(');
7982 gsym(b);
7983 gexpr();
7984 c = gtst(0, 0);
7985 gsym_addr(c, d);
7986 skip(')');
7987 gsym(a);
7988 skip(';');
7989 } else
7990 if (tok == TOK_SWITCH) {
7991 next();
7992 skip('(');
7993 gexpr();
7994 /* XXX: other types than integer */
7995 case_reg = gv(RC_INT);
7996 vpop();
7997 skip(')');
7998 a = 0;
7999 b = gjmp(0); /* jump to first case */
8000 c = 0;
8001 block(&a, csym, &b, &c, case_reg, 0);
8002 /* if no default, jmp after switch */
8003 if (c == 0)
8004 c = ind;
8005 /* default label */
8006 gsym_addr(b, c);
8007 /* break label */
8008 gsym(a);
8009 } else
8010 if (tok == TOK_CASE) {
8011 int v1, v2;
8012 if (!case_sym)
8013 expect("switch");
8014 next();
8015 v1 = expr_const();
8016 v2 = v1;
8017 if (gnu_ext && tok == TOK_DOTS) {
8018 next();
8019 v2 = expr_const();
8020 if (v2 < v1)
8021 warning("empty case range");
8023 /* since a case is like a label, we must skip it with a jmp */
8024 b = gjmp(0);
8025 gsym(*case_sym);
8026 vseti(case_reg, 0);
8027 vpushi(v1);
8028 if (v1 == v2) {
8029 gen_op(TOK_EQ);
8030 *case_sym = gtst(1, 0);
8031 } else {
8032 gen_op(TOK_GE);
8033 *case_sym = gtst(1, 0);
8034 vseti(case_reg, 0);
8035 vpushi(v2);
8036 gen_op(TOK_LE);
8037 *case_sym = gtst(1, *case_sym);
8039 gsym(b);
8040 skip(':');
8041 is_expr = 0;
8042 goto block_after_label;
8043 } else
8044 if (tok == TOK_DEFAULT) {
8045 next();
8046 skip(':');
8047 if (!def_sym)
8048 expect("switch");
8049 if (*def_sym)
8050 error("too many 'default'");
8051 *def_sym = ind;
8052 is_expr = 0;
8053 goto block_after_label;
8054 } else
8055 if (tok == TOK_GOTO) {
8056 next();
8057 if (tok == '*' && gnu_ext) {
8058 /* computed goto */
8059 next();
8060 gexpr();
8061 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8062 expect("pointer");
8063 ggoto();
8064 } else if (tok >= TOK_UIDENT) {
8065 s = label_find(tok);
8066 /* put forward definition if needed */
8067 if (!s) {
8068 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8069 } else {
8070 if (s->r == LABEL_DECLARED)
8071 s->r = LABEL_FORWARD;
8073 /* label already defined */
8074 if (s->r & LABEL_FORWARD)
8075 s->next = (void *)gjmp((long)s->next);
8076 else
8077 gjmp_addr((long)s->next);
8078 next();
8079 } else {
8080 expect("label identifier");
8082 skip(';');
8083 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8084 asm_instr();
8085 } else {
8086 b = is_label();
8087 if (b) {
8088 /* label case */
8089 s = label_find(b);
8090 if (s) {
8091 if (s->r == LABEL_DEFINED)
8092 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8093 gsym((long)s->next);
8094 s->r = LABEL_DEFINED;
8095 } else {
8096 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8098 s->next = (void *)ind;
8099 /* we accept this, but it is a mistake */
8100 block_after_label:
8101 if (tok == '}') {
8102 warning("deprecated use of label at end of compound statement");
8103 } else {
8104 if (is_expr)
8105 vpop();
8106 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8108 } else {
8109 /* expression case */
8110 if (tok != ';') {
8111 if (is_expr) {
8112 vpop();
8113 gexpr();
8114 } else {
8115 gexpr();
8116 vpop();
8119 skip(';');
8124 /* t is the array or struct type. c is the array or struct
8125 address. cur_index/cur_field is the pointer to the current
8126 value. 'size_only' is true if only size info is needed (only used
8127 in arrays) */
8128 static void decl_designator(CType *type, Section *sec, unsigned long c,
8129 int *cur_index, Sym **cur_field,
8130 int size_only)
8132 Sym *s, *f;
8133 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8134 CType type1;
8136 notfirst = 0;
8137 elem_size = 0;
8138 nb_elems = 1;
8139 if (gnu_ext && (l = is_label()) != 0)
8140 goto struct_field;
8141 while (tok == '[' || tok == '.') {
8142 if (tok == '[') {
8143 if (!(type->t & VT_ARRAY))
8144 expect("array type");
8145 s = type->ref;
8146 next();
8147 index = expr_const();
8148 if (index < 0 || (s->c >= 0 && index >= s->c))
8149 expect("invalid index");
8150 if (tok == TOK_DOTS && gnu_ext) {
8151 next();
8152 index_last = expr_const();
8153 if (index_last < 0 ||
8154 (s->c >= 0 && index_last >= s->c) ||
8155 index_last < index)
8156 expect("invalid index");
8157 } else {
8158 index_last = index;
8160 skip(']');
8161 if (!notfirst)
8162 *cur_index = index_last;
8163 type = pointed_type(type);
8164 elem_size = type_size(type, &align);
8165 c += index * elem_size;
8166 /* NOTE: we only support ranges for last designator */
8167 nb_elems = index_last - index + 1;
8168 if (nb_elems != 1) {
8169 notfirst = 1;
8170 break;
8172 } else {
8173 next();
8174 l = tok;
8175 next();
8176 struct_field:
8177 if ((type->t & VT_BTYPE) != VT_STRUCT)
8178 expect("struct/union type");
8179 s = type->ref;
8180 l |= SYM_FIELD;
8181 f = s->next;
8182 while (f) {
8183 if (f->v == l)
8184 break;
8185 f = f->next;
8187 if (!f)
8188 expect("field");
8189 if (!notfirst)
8190 *cur_field = f;
8191 /* XXX: fix this mess by using explicit storage field */
8192 type1 = f->type;
8193 type1.t |= (type->t & ~VT_TYPE);
8194 type = &type1;
8195 c += f->c;
8197 notfirst = 1;
8199 if (notfirst) {
8200 if (tok == '=') {
8201 next();
8202 } else {
8203 if (!gnu_ext)
8204 expect("=");
8206 } else {
8207 if (type->t & VT_ARRAY) {
8208 index = *cur_index;
8209 type = pointed_type(type);
8210 c += index * type_size(type, &align);
8211 } else {
8212 f = *cur_field;
8213 if (!f)
8214 error("too many field init");
8215 /* XXX: fix this mess by using explicit storage field */
8216 type1 = f->type;
8217 type1.t |= (type->t & ~VT_TYPE);
8218 type = &type1;
8219 c += f->c;
8222 decl_initializer(type, sec, c, 0, size_only);
8224 /* XXX: make it more general */
8225 if (!size_only && nb_elems > 1) {
8226 unsigned long c_end;
8227 uint8_t *src, *dst;
8228 int i;
8230 if (!sec)
8231 error("range init not supported yet for dynamic storage");
8232 c_end = c + nb_elems * elem_size;
8233 if (c_end > sec->data_allocated)
8234 section_realloc(sec, c_end);
8235 src = sec->data + c;
8236 dst = src;
8237 for(i = 1; i < nb_elems; i++) {
8238 dst += elem_size;
8239 memcpy(dst, src, elem_size);
8244 #define EXPR_VAL 0
8245 #define EXPR_CONST 1
8246 #define EXPR_ANY 2
8248 /* store a value or an expression directly in global data or in local array */
8249 static void init_putv(CType *type, Section *sec, unsigned long c,
8250 int v, int expr_type)
8252 int saved_global_expr, bt, bit_pos, bit_size;
8253 void *ptr;
8254 unsigned long long bit_mask;
8255 CType dtype;
8257 switch(expr_type) {
8258 case EXPR_VAL:
8259 vpushi(v);
8260 break;
8261 case EXPR_CONST:
8262 /* compound literals must be allocated globally in this case */
8263 saved_global_expr = global_expr;
8264 global_expr = 1;
8265 expr_const1();
8266 global_expr = saved_global_expr;
8267 /* NOTE: symbols are accepted */
8268 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8269 error("initializer element is not constant");
8270 break;
8271 case EXPR_ANY:
8272 expr_eq();
8273 break;
8276 dtype = *type;
8277 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8279 if (sec) {
8280 /* XXX: not portable */
8281 /* XXX: generate error if incorrect relocation */
8282 gen_assign_cast(&dtype);
8283 bt = type->t & VT_BTYPE;
8284 ptr = sec->data + c;
8285 /* XXX: make code faster ? */
8286 if (!(type->t & VT_BITFIELD)) {
8287 bit_pos = 0;
8288 bit_size = 32;
8289 bit_mask = -1LL;
8290 } else {
8291 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8292 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8293 bit_mask = (1LL << bit_size) - 1;
8295 if ((vtop->r & VT_SYM) &&
8296 (bt == VT_BYTE ||
8297 bt == VT_SHORT ||
8298 bt == VT_DOUBLE ||
8299 bt == VT_LDOUBLE ||
8300 bt == VT_LLONG ||
8301 (bt == VT_INT && bit_size != 32)))
8302 error("initializer element is not computable at load time");
8303 switch(bt) {
8304 case VT_BYTE:
8305 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8306 break;
8307 case VT_SHORT:
8308 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8309 break;
8310 case VT_DOUBLE:
8311 *(double *)ptr = vtop->c.d;
8312 break;
8313 case VT_LDOUBLE:
8314 *(long double *)ptr = vtop->c.ld;
8315 break;
8316 case VT_LLONG:
8317 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8318 break;
8319 default:
8320 if (vtop->r & VT_SYM) {
8321 greloc(sec, vtop->sym, c, R_DATA_32);
8323 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8324 break;
8326 vtop--;
8327 } else {
8328 vset(&dtype, VT_LOCAL, c);
8329 vswap();
8330 vstore();
8331 vpop();
8335 /* put zeros for variable based init */
8336 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8338 if (sec) {
8339 /* nothing to do because globals are already set to zero */
8340 } else {
8341 vpush_global_sym(&func_old_type, TOK_memset);
8342 vseti(VT_LOCAL, c);
8343 vpushi(0);
8344 vpushi(size);
8345 gfunc_call(3);
8349 /* 't' contains the type and storage info. 'c' is the offset of the
8350 object in section 'sec'. If 'sec' is NULL, it means stack based
8351 allocation. 'first' is true if array '{' must be read (multi
8352 dimension implicit array init handling). 'size_only' is true if
8353 size only evaluation is wanted (only for arrays). */
8354 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8355 int first, int size_only)
8357 int index, array_length, n, no_oblock, nb, parlevel, i;
8358 int size1, align1, expr_type;
8359 Sym *s, *f;
8360 CType *t1;
8362 if (type->t & VT_ARRAY) {
8363 s = type->ref;
8364 n = s->c;
8365 array_length = 0;
8366 t1 = pointed_type(type);
8367 size1 = type_size(t1, &align1);
8369 no_oblock = 1;
8370 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8371 tok == '{') {
8372 skip('{');
8373 no_oblock = 0;
8376 /* only parse strings here if correct type (otherwise: handle
8377 them as ((w)char *) expressions */
8378 if ((tok == TOK_LSTR &&
8379 (t1->t & VT_BTYPE) == VT_INT) ||
8380 (tok == TOK_STR &&
8381 (t1->t & VT_BTYPE) == VT_BYTE)) {
8382 while (tok == TOK_STR || tok == TOK_LSTR) {
8383 int cstr_len, ch;
8384 CString *cstr;
8386 cstr = tokc.cstr;
8387 /* compute maximum number of chars wanted */
8388 if (tok == TOK_STR)
8389 cstr_len = cstr->size;
8390 else
8391 cstr_len = cstr->size / sizeof(int);
8392 cstr_len--;
8393 nb = cstr_len;
8394 if (n >= 0 && nb > (n - array_length))
8395 nb = n - array_length;
8396 if (!size_only) {
8397 if (cstr_len > nb)
8398 warning("initializer-string for array is too long");
8399 /* in order to go faster for common case (char
8400 string in global variable, we handle it
8401 specifically */
8402 if (sec && tok == TOK_STR && size1 == 1) {
8403 memcpy(sec->data + c + array_length, cstr->data, nb);
8404 } else {
8405 for(i=0;i<nb;i++) {
8406 if (tok == TOK_STR)
8407 ch = ((unsigned char *)cstr->data)[i];
8408 else
8409 ch = ((int *)cstr->data)[i];
8410 init_putv(t1, sec, c + (array_length + i) * size1,
8411 ch, EXPR_VAL);
8415 array_length += nb;
8416 next();
8418 /* only add trailing zero if enough storage (no
8419 warning in this case since it is standard) */
8420 if (n < 0 || array_length < n) {
8421 if (!size_only) {
8422 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8424 array_length++;
8426 } else {
8427 index = 0;
8428 while (tok != '}') {
8429 decl_designator(type, sec, c, &index, NULL, size_only);
8430 if (n >= 0 && index >= n)
8431 error("index too large");
8432 /* must put zero in holes (note that doing it that way
8433 ensures that it even works with designators) */
8434 if (!size_only && array_length < index) {
8435 init_putz(t1, sec, c + array_length * size1,
8436 (index - array_length) * size1);
8438 index++;
8439 if (index > array_length)
8440 array_length = index;
8441 /* special test for multi dimensional arrays (may not
8442 be strictly correct if designators are used at the
8443 same time) */
8444 if (index >= n && no_oblock)
8445 break;
8446 if (tok == '}')
8447 break;
8448 skip(',');
8451 if (!no_oblock)
8452 skip('}');
8453 /* put zeros at the end */
8454 if (!size_only && n >= 0 && array_length < n) {
8455 init_putz(t1, sec, c + array_length * size1,
8456 (n - array_length) * size1);
8458 /* patch type size if needed */
8459 if (n < 0)
8460 s->c = array_length;
8461 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8462 (sec || !first || tok == '{')) {
8463 int par_count;
8465 /* NOTE: the previous test is a specific case for automatic
8466 struct/union init */
8467 /* XXX: union needs only one init */
8469 /* XXX: this test is incorrect for local initializers
8470 beginning with ( without {. It would be much more difficult
8471 to do it correctly (ideally, the expression parser should
8472 be used in all cases) */
8473 par_count = 0;
8474 if (tok == '(') {
8475 AttributeDef ad1;
8476 CType type1;
8477 next();
8478 while (tok == '(') {
8479 par_count++;
8480 next();
8482 if (!parse_btype(&type1, &ad1))
8483 expect("cast");
8484 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8485 #if 0
8486 if (!is_assignable_types(type, &type1))
8487 error("invalid type for cast");
8488 #endif
8489 skip(')');
8491 no_oblock = 1;
8492 if (first || tok == '{') {
8493 skip('{');
8494 no_oblock = 0;
8496 s = type->ref;
8497 f = s->next;
8498 array_length = 0;
8499 index = 0;
8500 n = s->c;
8501 while (tok != '}') {
8502 decl_designator(type, sec, c, NULL, &f, size_only);
8503 index = f->c;
8504 if (!size_only && array_length < index) {
8505 init_putz(type, sec, c + array_length,
8506 index - array_length);
8508 index = index + type_size(&f->type, &align1);
8509 if (index > array_length)
8510 array_length = index;
8511 f = f->next;
8512 if (no_oblock && f == NULL)
8513 break;
8514 if (tok == '}')
8515 break;
8516 skip(',');
8518 /* put zeros at the end */
8519 if (!size_only && array_length < n) {
8520 init_putz(type, sec, c + array_length,
8521 n - array_length);
8523 if (!no_oblock)
8524 skip('}');
8525 while (par_count) {
8526 skip(')');
8527 par_count--;
8529 } else if (tok == '{') {
8530 next();
8531 decl_initializer(type, sec, c, first, size_only);
8532 skip('}');
8533 } else if (size_only) {
8534 /* just skip expression */
8535 parlevel = 0;
8536 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8537 tok != -1) {
8538 if (tok == '(')
8539 parlevel++;
8540 else if (tok == ')')
8541 parlevel--;
8542 next();
8544 } else {
8545 /* currently, we always use constant expression for globals
8546 (may change for scripting case) */
8547 expr_type = EXPR_CONST;
8548 if (!sec)
8549 expr_type = EXPR_ANY;
8550 init_putv(type, sec, c, 0, expr_type);
8554 /* parse an initializer for type 't' if 'has_init' is non zero, and
8555 allocate space in local or global data space ('r' is either
8556 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8557 variable 'v' of scope 'scope' is declared before initializers are
8558 parsed. If 'v' is zero, then a reference to the new object is put
8559 in the value stack. If 'has_init' is 2, a special parsing is done
8560 to handle string constants. */
8561 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8562 int has_init, int v, int scope)
8564 int size, align, addr, data_offset;
8565 int level;
8566 ParseState saved_parse_state;
8567 TokenString init_str;
8568 Section *sec;
8570 size = type_size(type, &align);
8571 /* If unknown size, we must evaluate it before
8572 evaluating initializers because
8573 initializers can generate global data too
8574 (e.g. string pointers or ISOC99 compound
8575 literals). It also simplifies local
8576 initializers handling */
8577 tok_str_new(&init_str);
8578 if (size < 0) {
8579 if (!has_init)
8580 error("unknown type size");
8581 /* get all init string */
8582 if (has_init == 2) {
8583 /* only get strings */
8584 while (tok == TOK_STR || tok == TOK_LSTR) {
8585 tok_str_add_tok(&init_str);
8586 next();
8588 } else {
8589 level = 0;
8590 while (level > 0 || (tok != ',' && tok != ';')) {
8591 if (tok < 0)
8592 error("unexpected end of file in initializer");
8593 tok_str_add_tok(&init_str);
8594 if (tok == '{')
8595 level++;
8596 else if (tok == '}') {
8597 if (level == 0)
8598 break;
8599 level--;
8601 next();
8604 tok_str_add(&init_str, -1);
8605 tok_str_add(&init_str, 0);
8607 /* compute size */
8608 save_parse_state(&saved_parse_state);
8610 macro_ptr = init_str.str;
8611 next();
8612 decl_initializer(type, NULL, 0, 1, 1);
8613 /* prepare second initializer parsing */
8614 macro_ptr = init_str.str;
8615 next();
8617 /* if still unknown size, error */
8618 size = type_size(type, &align);
8619 if (size < 0)
8620 error("unknown type size");
8622 /* take into account specified alignment if bigger */
8623 if (ad->aligned) {
8624 if (ad->aligned > align)
8625 align = ad->aligned;
8626 } else if (ad->packed) {
8627 align = 1;
8629 if ((r & VT_VALMASK) == VT_LOCAL) {
8630 sec = NULL;
8631 if (do_bounds_check && (type->t & VT_ARRAY))
8632 loc--;
8633 loc = (loc - size) & -align;
8634 addr = loc;
8635 /* handles bounds */
8636 /* XXX: currently, since we do only one pass, we cannot track
8637 '&' operators, so we add only arrays */
8638 if (do_bounds_check && (type->t & VT_ARRAY)) {
8639 unsigned long *bounds_ptr;
8640 /* add padding between regions */
8641 loc--;
8642 /* then add local bound info */
8643 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8644 bounds_ptr[0] = addr;
8645 bounds_ptr[1] = size;
8647 if (v) {
8648 /* local variable */
8649 sym_push(v, type, r, addr);
8650 } else {
8651 /* push local reference */
8652 vset(type, r, addr);
8654 } else {
8655 Sym *sym;
8657 sym = NULL;
8658 if (v && scope == VT_CONST) {
8659 /* see if the symbol was already defined */
8660 sym = sym_find(v);
8661 if (sym) {
8662 if (!is_compatible_types(&sym->type, type))
8663 error("incompatible types for redefinition of '%s'",
8664 get_tok_str(v, NULL));
8665 if (sym->type.t & VT_EXTERN) {
8666 /* if the variable is extern, it was not allocated */
8667 sym->type.t &= ~VT_EXTERN;
8668 /* set array size if it was ommited in extern
8669 declaration */
8670 if ((sym->type.t & VT_ARRAY) &&
8671 sym->type.ref->c < 0 &&
8672 type->ref->c >= 0)
8673 sym->type.ref->c = type->ref->c;
8674 } else {
8675 /* we accept several definitions of the same
8676 global variable. this is tricky, because we
8677 must play with the SHN_COMMON type of the symbol */
8678 /* XXX: should check if the variable was already
8679 initialized. It is incorrect to initialized it
8680 twice */
8681 /* no init data, we won't add more to the symbol */
8682 if (!has_init)
8683 goto no_alloc;
8688 /* allocate symbol in corresponding section */
8689 sec = ad->section;
8690 if (!sec) {
8691 if (has_init)
8692 sec = data_section;
8693 else if (tcc_state->nocommon)
8694 sec = bss_section;
8696 if (sec) {
8697 data_offset = sec->data_offset;
8698 data_offset = (data_offset + align - 1) & -align;
8699 addr = data_offset;
8700 /* very important to increment global pointer at this time
8701 because initializers themselves can create new initializers */
8702 data_offset += size;
8703 /* add padding if bound check */
8704 if (do_bounds_check)
8705 data_offset++;
8706 sec->data_offset = data_offset;
8707 /* allocate section space to put the data */
8708 if (sec->sh_type != SHT_NOBITS &&
8709 data_offset > sec->data_allocated)
8710 section_realloc(sec, data_offset);
8711 /* align section if needed */
8712 if (align > sec->sh_addralign)
8713 sec->sh_addralign = align;
8714 } else {
8715 addr = 0; /* avoid warning */
8718 if (v) {
8719 if (scope == VT_CONST) {
8720 if (!sym)
8721 goto do_def;
8722 } else {
8723 do_def:
8724 sym = sym_push(v, type, r | VT_SYM, 0);
8726 /* update symbol definition */
8727 if (sec) {
8728 put_extern_sym(sym, sec, addr, size);
8729 } else {
8730 Elf32_Sym *esym;
8731 /* put a common area */
8732 put_extern_sym(sym, NULL, align, size);
8733 /* XXX: find a nicer way */
8734 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8735 esym->st_shndx = SHN_COMMON;
8737 } else {
8738 CValue cval;
8740 /* push global reference */
8741 sym = get_sym_ref(type, sec, addr, size);
8742 cval.ul = 0;
8743 vsetc(type, VT_CONST | VT_SYM, &cval);
8744 vtop->sym = sym;
8747 /* handles bounds now because the symbol must be defined
8748 before for the relocation */
8749 if (do_bounds_check) {
8750 unsigned long *bounds_ptr;
8752 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8753 /* then add global bound info */
8754 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8755 bounds_ptr[0] = 0; /* relocated */
8756 bounds_ptr[1] = size;
8759 if (has_init) {
8760 decl_initializer(type, sec, addr, 1, 0);
8761 /* restore parse state if needed */
8762 if (init_str.str) {
8763 tok_str_free(init_str.str);
8764 restore_parse_state(&saved_parse_state);
8767 no_alloc: ;
8770 void put_func_debug(Sym *sym)
8772 char buf[512];
8774 /* stabs info */
8775 /* XXX: we put here a dummy type */
8776 snprintf(buf, sizeof(buf), "%s:%c1",
8777 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8778 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8779 cur_text_section, sym->c);
8780 last_ind = 0;
8781 last_line_num = 0;
8784 /* parse an old style function declaration list */
8785 /* XXX: check multiple parameter */
8786 static void func_decl_list(Sym *func_sym)
8788 AttributeDef ad;
8789 int v;
8790 Sym *s;
8791 CType btype, type;
8793 /* parse each declaration */
8794 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8795 if (!parse_btype(&btype, &ad))
8796 expect("declaration list");
8797 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8798 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8799 tok == ';') {
8800 /* we accept no variable after */
8801 } else {
8802 for(;;) {
8803 type = btype;
8804 type_decl(&type, &ad, &v, TYPE_DIRECT);
8805 /* find parameter in function parameter list */
8806 s = func_sym->next;
8807 while (s != NULL) {
8808 if ((s->v & ~SYM_FIELD) == v)
8809 goto found;
8810 s = s->next;
8812 error("declaration for parameter '%s' but no such parameter",
8813 get_tok_str(v, NULL));
8814 found:
8815 /* check that no storage specifier except 'register' was given */
8816 if (type.t & VT_STORAGE)
8817 error("storage class specified for '%s'", get_tok_str(v, NULL));
8818 convert_parameter_type(&type);
8819 /* we can add the type (NOTE: it could be local to the function) */
8820 s->type = type;
8821 /* accept other parameters */
8822 if (tok == ',')
8823 next();
8824 else
8825 break;
8828 skip(';');
8832 /* parse a function defined by symbol 'sym' and generate its code in
8833 'cur_text_section' */
8834 static void gen_function(Sym *sym)
8836 ind = cur_text_section->data_offset;
8837 /* NOTE: we patch the symbol size later */
8838 put_extern_sym(sym, cur_text_section, ind, 0);
8839 funcname = get_tok_str(sym->v, NULL);
8840 func_ind = ind;
8841 /* put debug symbol */
8842 if (do_debug)
8843 put_func_debug(sym);
8844 /* push a dummy symbol to enable local sym storage */
8845 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8846 gfunc_prolog(&sym->type);
8847 rsym = 0;
8848 block(NULL, NULL, NULL, NULL, 0, 0);
8849 gsym(rsym);
8850 gfunc_epilog();
8851 cur_text_section->data_offset = ind;
8852 label_pop(&global_label_stack, NULL);
8853 sym_pop(&local_stack, NULL); /* reset local stack */
8854 /* end of function */
8855 /* patch symbol size */
8856 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8857 ind - func_ind;
8858 if (do_debug) {
8859 put_stabn(N_FUN, 0, 0, ind - func_ind);
8861 funcname = ""; /* for safety */
8862 func_vt.t = VT_VOID; /* for safety */
8863 ind = 0; /* for safety */
8866 static void gen_inline_functions(void)
8868 Sym *sym;
8869 CType *type;
8870 int *str, inline_generated;
8872 /* iterate while inline function are referenced */
8873 for(;;) {
8874 inline_generated = 0;
8875 for(sym = global_stack; sym != NULL; sym = sym->prev) {
8876 type = &sym->type;
8877 if (((type->t & VT_BTYPE) == VT_FUNC) &&
8878 (type->t & (VT_STATIC | VT_INLINE)) ==
8879 (VT_STATIC | VT_INLINE) &&
8880 sym->c != 0) {
8881 /* the function was used: generate its code and
8882 convert it to a normal function */
8883 str = (int *)sym->r;
8884 sym->r = VT_SYM | VT_CONST;
8885 type->t &= ~VT_INLINE;
8887 macro_ptr = str;
8888 next();
8889 cur_text_section = text_section;
8890 gen_function(sym);
8891 macro_ptr = NULL; /* fail safe */
8893 tok_str_free(str);
8894 inline_generated = 1;
8897 if (!inline_generated)
8898 break;
8901 /* free all remaining inline function tokens */
8902 for(sym = global_stack; sym != NULL; sym = sym->prev) {
8903 type = &sym->type;
8904 if (((type->t & VT_BTYPE) == VT_FUNC) &&
8905 (type->t & (VT_STATIC | VT_INLINE)) ==
8906 (VT_STATIC | VT_INLINE)) {
8907 str = (int *)sym->r;
8908 tok_str_free(str);
8909 sym->r = 0; /* fail safe */
8914 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8915 static void decl(int l)
8917 int v, has_init, r;
8918 CType type, btype;
8919 Sym *sym;
8920 AttributeDef ad;
8922 while (1) {
8923 if (!parse_btype(&btype, &ad)) {
8924 /* skip redundant ';' */
8925 /* XXX: find more elegant solution */
8926 if (tok == ';') {
8927 next();
8928 continue;
8930 if (l == VT_CONST &&
8931 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
8932 /* global asm block */
8933 asm_global_instr();
8934 continue;
8936 /* special test for old K&R protos without explicit int
8937 type. Only accepted when defining global data */
8938 if (l == VT_LOCAL || tok < TOK_DEFINE)
8939 break;
8940 btype.t = VT_INT;
8942 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8943 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8944 tok == ';') {
8945 /* we accept no variable after */
8946 next();
8947 continue;
8949 while (1) { /* iterate thru each declaration */
8950 type = btype;
8951 type_decl(&type, &ad, &v, TYPE_DIRECT);
8952 #if 0
8954 char buf[500];
8955 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8956 printf("type = '%s'\n", buf);
8958 #endif
8959 if ((type.t & VT_BTYPE) == VT_FUNC) {
8960 /* if old style function prototype, we accept a
8961 declaration list */
8962 sym = type.ref;
8963 if (sym->c == FUNC_OLD)
8964 func_decl_list(sym);
8967 if (tok == '{') {
8968 if (l == VT_LOCAL)
8969 error("cannot use local functions");
8970 if (!(type.t & VT_FUNC))
8971 expect("function definition");
8973 /* reject abstract declarators in function definition */
8974 sym = type.ref;
8975 while ((sym = sym->next) != NULL)
8976 if (!(sym->v & ~SYM_FIELD))
8977 expect("identifier");
8979 /* XXX: cannot do better now: convert extern line to static inline */
8980 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8981 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8983 sym = sym_find(v);
8984 if (sym) {
8985 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
8986 goto func_error1;
8987 /* specific case: if not func_call defined, we put
8988 the one of the prototype */
8989 /* XXX: should have default value */
8990 if (sym->type.ref->r != FUNC_CDECL &&
8991 type.ref->r == FUNC_CDECL)
8992 type.ref->r = sym->type.ref->r;
8993 if (!is_compatible_types(&sym->type, &type)) {
8994 func_error1:
8995 error("incompatible types for redefinition of '%s'",
8996 get_tok_str(v, NULL));
8998 /* if symbol is already defined, then put complete type */
8999 sym->type = type;
9000 } else {
9001 /* put function symbol */
9002 sym = global_identifier_push(v, type.t, 0);
9003 sym->type.ref = type.ref;
9006 /* static inline functions are just recorded as a kind
9007 of macro. Their code will be emitted at the end of
9008 the compilation unit only if they are used */
9009 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9010 (VT_INLINE | VT_STATIC)) {
9011 TokenString func_str;
9012 int block_level;
9014 tok_str_new(&func_str);
9016 block_level = 0;
9017 for(;;) {
9018 int t;
9019 if (tok == TOK_EOF)
9020 error("unexpected end of file");
9021 tok_str_add_tok(&func_str);
9022 t = tok;
9023 next();
9024 if (t == '{') {
9025 block_level++;
9026 } else if (t == '}') {
9027 block_level--;
9028 if (block_level == 0)
9029 break;
9032 tok_str_add(&func_str, -1);
9033 tok_str_add(&func_str, 0);
9034 sym->r = (int)func_str.str;
9035 } else {
9036 /* compute text section */
9037 cur_text_section = ad.section;
9038 if (!cur_text_section)
9039 cur_text_section = text_section;
9040 sym->r = VT_SYM | VT_CONST;
9041 gen_function(sym);
9042 #ifdef TCC_TARGET_PE
9043 if (ad.dllexport) {
9044 ((Elf32_Sym *)symtab_section->data)[sym->c].st_other |= 1;
9046 #endif
9048 break;
9049 } else {
9050 if (btype.t & VT_TYPEDEF) {
9051 /* save typedefed type */
9052 /* XXX: test storage specifiers ? */
9053 sym = sym_push(v, &type, 0, 0);
9054 sym->type.t |= VT_TYPEDEF;
9055 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9056 /* external function definition */
9057 /* specific case for func_call attribute */
9058 if (ad.func_call)
9059 type.ref->r = ad.func_call;
9060 external_sym(v, &type, 0);
9061 } else {
9062 /* not lvalue if array */
9063 r = 0;
9064 if (!(type.t & VT_ARRAY))
9065 r |= lvalue_type(type.t);
9066 has_init = (tok == '=');
9067 if ((btype.t & VT_EXTERN) ||
9068 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9069 !has_init && l == VT_CONST && type.ref->c < 0)) {
9070 /* external variable */
9071 /* NOTE: as GCC, uninitialized global static
9072 arrays of null size are considered as
9073 extern */
9074 external_sym(v, &type, r);
9075 } else {
9076 if (type.t & VT_STATIC)
9077 r |= VT_CONST;
9078 else
9079 r |= l;
9080 if (has_init)
9081 next();
9082 decl_initializer_alloc(&type, &ad, r,
9083 has_init, v, l);
9086 if (tok != ',') {
9087 skip(';');
9088 break;
9090 next();
9096 /* better than nothing, but needs extension to handle '-E' option
9097 correctly too */
9098 static void preprocess_init(TCCState *s1)
9100 s1->include_stack_ptr = s1->include_stack;
9101 /* XXX: move that before to avoid having to initialize
9102 file->ifdef_stack_ptr ? */
9103 s1->ifdef_stack_ptr = s1->ifdef_stack;
9104 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9106 /* XXX: not ANSI compliant: bound checking says error */
9107 vtop = vstack - 1;
9108 s1->pack_stack[0] = 0;
9109 s1->pack_stack_ptr = s1->pack_stack;
9112 /* compile the C file opened in 'file'. Return non zero if errors. */
9113 static int tcc_compile(TCCState *s1)
9115 Sym *define_start;
9116 char buf[512];
9117 volatile int section_sym;
9119 #ifdef INC_DEBUG
9120 printf("%s: **** new file\n", file->filename);
9121 #endif
9122 preprocess_init(s1);
9124 funcname = "";
9125 anon_sym = SYM_FIRST_ANOM;
9127 /* file info: full path + filename */
9128 section_sym = 0; /* avoid warning */
9129 if (do_debug) {
9130 section_sym = put_elf_sym(symtab_section, 0, 0,
9131 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
9132 text_section->sh_num, NULL);
9133 getcwd(buf, sizeof(buf));
9134 pstrcat(buf, sizeof(buf), "/");
9135 put_stabs_r(buf, N_SO, 0, 0,
9136 text_section->data_offset, text_section, section_sym);
9137 put_stabs_r(file->filename, N_SO, 0, 0,
9138 text_section->data_offset, text_section, section_sym);
9140 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9141 symbols can be safely used */
9142 put_elf_sym(symtab_section, 0, 0,
9143 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
9144 SHN_ABS, file->filename);
9146 /* define some often used types */
9147 int_type.t = VT_INT;
9149 char_pointer_type.t = VT_BYTE;
9150 mk_pointer(&char_pointer_type);
9152 func_old_type.t = VT_FUNC;
9153 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9155 #if 0
9156 /* define 'void *alloca(unsigned int)' builtin function */
9158 Sym *s1;
9160 p = anon_sym++;
9161 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9162 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9163 s1->next = NULL;
9164 sym->next = s1;
9165 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9167 #endif
9169 define_start = define_stack;
9171 if (setjmp(s1->error_jmp_buf) == 0) {
9172 s1->nb_errors = 0;
9173 s1->error_set_jmp_enabled = 1;
9175 ch = file->buf_ptr[0];
9176 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9177 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9178 next();
9179 decl(VT_CONST);
9180 if (tok != TOK_EOF)
9181 expect("declaration");
9183 /* end of translation unit info */
9184 if (do_debug) {
9185 put_stabs_r(NULL, N_SO, 0, 0,
9186 text_section->data_offset, text_section, section_sym);
9189 s1->error_set_jmp_enabled = 0;
9191 /* reset define stack, but leave -Dsymbols (may be incorrect if
9192 they are undefined) */
9193 free_defines(define_start);
9195 gen_inline_functions();
9197 sym_pop(&global_stack, NULL);
9199 return s1->nb_errors != 0 ? -1 : 0;
9202 #ifdef LIBTCC
9203 int tcc_compile_string(TCCState *s, const char *str)
9205 BufferedFile bf1, *bf = &bf1;
9206 int ret, len;
9207 char *buf;
9209 /* init file structure */
9210 bf->fd = -1;
9211 /* XXX: avoid copying */
9212 len = strlen(str);
9213 buf = tcc_malloc(len + 1);
9214 if (!buf)
9215 return -1;
9216 memcpy(buf, str, len);
9217 buf[len] = CH_EOB;
9218 bf->buf_ptr = buf;
9219 bf->buf_end = buf + len;
9220 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9221 bf->line_num = 1;
9222 file = bf;
9224 ret = tcc_compile(s);
9226 tcc_free(buf);
9228 /* currently, no need to close */
9229 return ret;
9231 #endif
9233 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9234 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9236 BufferedFile bf1, *bf = &bf1;
9238 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9239 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9240 /* default value */
9241 if (!value)
9242 value = "1";
9243 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9245 /* init file structure */
9246 bf->fd = -1;
9247 bf->buf_ptr = bf->buffer;
9248 bf->buf_end = bf->buffer + strlen(bf->buffer);
9249 *bf->buf_end = CH_EOB;
9250 bf->filename[0] = '\0';
9251 bf->line_num = 1;
9252 file = bf;
9254 s1->include_stack_ptr = s1->include_stack;
9256 /* parse with define parser */
9257 ch = file->buf_ptr[0];
9258 next_nomacro();
9259 parse_define();
9260 file = NULL;
9263 /* undefine a preprocessor symbol */
9264 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9266 TokenSym *ts;
9267 Sym *s;
9268 ts = tok_alloc(sym, strlen(sym));
9269 s = define_find(ts->tok);
9270 /* undefine symbol by putting an invalid name */
9271 if (s)
9272 define_undef(s);
9275 #ifdef CONFIG_TCC_ASM
9277 #ifdef TCC_TARGET_I386
9278 #include "i386-asm.c"
9279 #endif
9280 #include "tccasm.c"
9282 #else
9283 static void asm_instr(void)
9285 error("inline asm() not supported");
9287 static void asm_global_instr(void)
9289 error("inline asm() not supported");
9291 #endif
9293 #include "tccelf.c"
9295 #ifdef TCC_TARGET_COFF
9296 #include "tcccoff.c"
9297 #endif
9299 #ifdef TCC_TARGET_PE
9300 #include "tccpe.c"
9301 #endif
9303 /* print the position in the source file of PC value 'pc' by reading
9304 the stabs debug information */
9305 static void rt_printline(unsigned long wanted_pc)
9307 Stab_Sym *sym, *sym_end;
9308 char func_name[128], last_func_name[128];
9309 unsigned long func_addr, last_pc, pc;
9310 const char *incl_files[INCLUDE_STACK_SIZE];
9311 int incl_index, len, last_line_num, i;
9312 const char *str, *p;
9314 fprintf(stderr, "0x%08lx:", wanted_pc);
9316 func_name[0] = '\0';
9317 func_addr = 0;
9318 incl_index = 0;
9319 last_func_name[0] = '\0';
9320 last_pc = 0xffffffff;
9321 last_line_num = 1;
9322 sym = (Stab_Sym *)stab_section->data + 1;
9323 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9324 while (sym < sym_end) {
9325 switch(sym->n_type) {
9326 /* function start or end */
9327 case N_FUN:
9328 if (sym->n_strx == 0) {
9329 /* we test if between last line and end of function */
9330 pc = sym->n_value + func_addr;
9331 if (wanted_pc >= last_pc && wanted_pc < pc)
9332 goto found;
9333 func_name[0] = '\0';
9334 func_addr = 0;
9335 } else {
9336 str = stabstr_section->data + sym->n_strx;
9337 p = strchr(str, ':');
9338 if (!p) {
9339 pstrcpy(func_name, sizeof(func_name), str);
9340 } else {
9341 len = p - str;
9342 if (len > sizeof(func_name) - 1)
9343 len = sizeof(func_name) - 1;
9344 memcpy(func_name, str, len);
9345 func_name[len] = '\0';
9347 func_addr = sym->n_value;
9349 break;
9350 /* line number info */
9351 case N_SLINE:
9352 pc = sym->n_value + func_addr;
9353 if (wanted_pc >= last_pc && wanted_pc < pc)
9354 goto found;
9355 last_pc = pc;
9356 last_line_num = sym->n_desc;
9357 /* XXX: slow! */
9358 strcpy(last_func_name, func_name);
9359 break;
9360 /* include files */
9361 case N_BINCL:
9362 str = stabstr_section->data + sym->n_strx;
9363 add_incl:
9364 if (incl_index < INCLUDE_STACK_SIZE) {
9365 incl_files[incl_index++] = str;
9367 break;
9368 case N_EINCL:
9369 if (incl_index > 1)
9370 incl_index--;
9371 break;
9372 case N_SO:
9373 if (sym->n_strx == 0) {
9374 incl_index = 0; /* end of translation unit */
9375 } else {
9376 str = stabstr_section->data + sym->n_strx;
9377 /* do not add path */
9378 len = strlen(str);
9379 if (len > 0 && str[len - 1] != '/')
9380 goto add_incl;
9382 break;
9384 sym++;
9387 /* second pass: we try symtab symbols (no line number info) */
9388 incl_index = 0;
9390 Elf32_Sym *sym, *sym_end;
9391 int type;
9393 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
9394 for(sym = (Elf32_Sym *)symtab_section->data + 1;
9395 sym < sym_end;
9396 sym++) {
9397 type = ELF32_ST_TYPE(sym->st_info);
9398 if (type == STT_FUNC) {
9399 if (wanted_pc >= sym->st_value &&
9400 wanted_pc < sym->st_value + sym->st_size) {
9401 pstrcpy(last_func_name, sizeof(last_func_name),
9402 strtab_section->data + sym->st_name);
9403 goto found;
9408 /* did not find any info: */
9409 fprintf(stderr, " ???\n");
9410 return;
9411 found:
9412 if (last_func_name[0] != '\0') {
9413 fprintf(stderr, " %s()", last_func_name);
9415 if (incl_index > 0) {
9416 fprintf(stderr, " (%s:%d",
9417 incl_files[incl_index - 1], last_line_num);
9418 for(i = incl_index - 2; i >= 0; i--)
9419 fprintf(stderr, ", included from %s", incl_files[i]);
9420 fprintf(stderr, ")");
9422 fprintf(stderr, "\n");
9425 #if !defined(WIN32) && !defined(CONFIG_TCCBOOT)
9427 #ifdef __i386__
9429 /* fix for glibc 2.1 */
9430 #ifndef REG_EIP
9431 #define REG_EIP EIP
9432 #define REG_EBP EBP
9433 #endif
9435 /* return the PC at frame level 'level'. Return non zero if not found */
9436 static int rt_get_caller_pc(unsigned long *paddr,
9437 ucontext_t *uc, int level)
9439 unsigned long fp;
9440 int i;
9442 if (level == 0) {
9443 #if defined(__FreeBSD__)
9444 *paddr = uc->uc_mcontext.mc_eip;
9445 #elif defined(__dietlibc__)
9446 *paddr = uc->uc_mcontext.eip;
9447 #else
9448 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9449 #endif
9450 return 0;
9451 } else {
9452 #if defined(__FreeBSD__)
9453 fp = uc->uc_mcontext.mc_ebp;
9454 #elif defined(__dietlibc__)
9455 fp = uc->uc_mcontext.ebp;
9456 #else
9457 fp = uc->uc_mcontext.gregs[REG_EBP];
9458 #endif
9459 for(i=1;i<level;i++) {
9460 /* XXX: check address validity with program info */
9461 if (fp <= 0x1000 || fp >= 0xc0000000)
9462 return -1;
9463 fp = ((unsigned long *)fp)[0];
9465 *paddr = ((unsigned long *)fp)[1];
9466 return 0;
9469 #else
9471 #warning add arch specific rt_get_caller_pc()
9473 static int rt_get_caller_pc(unsigned long *paddr,
9474 ucontext_t *uc, int level)
9476 return -1;
9478 #endif
9480 /* emit a run time error at position 'pc' */
9481 void rt_error(ucontext_t *uc, const char *fmt, ...)
9483 va_list ap;
9484 unsigned long pc;
9485 int i;
9487 va_start(ap, fmt);
9488 fprintf(stderr, "Runtime error: ");
9489 vfprintf(stderr, fmt, ap);
9490 fprintf(stderr, "\n");
9491 for(i=0;i<num_callers;i++) {
9492 if (rt_get_caller_pc(&pc, uc, i) < 0)
9493 break;
9494 if (i == 0)
9495 fprintf(stderr, "at ");
9496 else
9497 fprintf(stderr, "by ");
9498 rt_printline(pc);
9500 exit(255);
9501 va_end(ap);
9504 /* signal handler for fatal errors */
9505 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9507 ucontext_t *uc = puc;
9509 switch(signum) {
9510 case SIGFPE:
9511 switch(siginf->si_code) {
9512 case FPE_INTDIV:
9513 case FPE_FLTDIV:
9514 rt_error(uc, "division by zero");
9515 break;
9516 default:
9517 rt_error(uc, "floating point exception");
9518 break;
9520 break;
9521 case SIGBUS:
9522 case SIGSEGV:
9523 if (rt_bound_error_msg && *rt_bound_error_msg)
9524 rt_error(uc, *rt_bound_error_msg);
9525 else
9526 rt_error(uc, "dereferencing invalid pointer");
9527 break;
9528 case SIGILL:
9529 rt_error(uc, "illegal instruction");
9530 break;
9531 case SIGABRT:
9532 rt_error(uc, "abort() called");
9533 break;
9534 default:
9535 rt_error(uc, "caught signal %d", signum);
9536 break;
9538 exit(255);
9540 #endif
9542 /* do all relocations (needed before using tcc_get_symbol()) */
9543 int tcc_relocate(TCCState *s1)
9545 Section *s;
9546 int i;
9548 s1->nb_errors = 0;
9550 #ifdef TCC_TARGET_PE
9551 pe_add_runtime(s1);
9552 #else
9553 tcc_add_runtime(s1);
9554 #endif
9556 relocate_common_syms();
9558 tcc_add_linker_symbols(s1);
9560 build_got_entries(s1);
9562 /* compute relocation address : section are relocated in place. We
9563 also alloc the bss space */
9564 for(i = 1; i < s1->nb_sections; i++) {
9565 s = s1->sections[i];
9566 if (s->sh_flags & SHF_ALLOC) {
9567 if (s->sh_type == SHT_NOBITS)
9568 s->data = tcc_mallocz(s->data_offset);
9569 s->sh_addr = (unsigned long)s->data;
9573 relocate_syms(s1, 1);
9575 if (s1->nb_errors != 0)
9576 return -1;
9578 /* relocate each section */
9579 for(i = 1; i < s1->nb_sections; i++) {
9580 s = s1->sections[i];
9581 if (s->reloc)
9582 relocate_section(s1, s);
9584 return 0;
9587 /* launch the compiled program with the given arguments */
9588 int tcc_run(TCCState *s1, int argc, char **argv)
9590 int (*prog_main)(int, char **);
9592 if (tcc_relocate(s1) < 0)
9593 return -1;
9595 prog_main = tcc_get_symbol_err(s1, "main");
9597 if (do_debug) {
9598 #if defined(WIN32) || defined(CONFIG_TCCBOOT)
9599 error("debug mode currently not available for Windows");
9600 #else
9601 struct sigaction sigact;
9602 /* install TCC signal handlers to print debug info on fatal
9603 runtime errors */
9604 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
9605 sigact.sa_sigaction = sig_error;
9606 sigemptyset(&sigact.sa_mask);
9607 sigaction(SIGFPE, &sigact, NULL);
9608 sigaction(SIGILL, &sigact, NULL);
9609 sigaction(SIGSEGV, &sigact, NULL);
9610 sigaction(SIGBUS, &sigact, NULL);
9611 sigaction(SIGABRT, &sigact, NULL);
9612 #endif
9615 #ifdef CONFIG_TCC_BCHECK
9616 if (do_bounds_check) {
9617 void (*bound_init)(void);
9619 /* set error function */
9620 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
9621 "__bound_error_msg");
9623 /* XXX: use .init section so that it also work in binary ? */
9624 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
9625 bound_init();
9627 #endif
9628 return (*prog_main)(argc, argv);
9631 TCCState *tcc_new(void)
9633 const char *p, *r;
9634 TCCState *s;
9635 TokenSym *ts;
9636 int i, c;
9638 s = tcc_mallocz(sizeof(TCCState));
9639 if (!s)
9640 return NULL;
9641 tcc_state = s;
9642 s->output_type = TCC_OUTPUT_MEMORY;
9644 /* init isid table */
9645 for(i=0;i<256;i++)
9646 isidnum_table[i] = isid(i) || isnum(i);
9648 /* add all tokens */
9649 table_ident = NULL;
9650 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9652 tok_ident = TOK_IDENT;
9653 p = tcc_keywords;
9654 while (*p) {
9655 r = p;
9656 for(;;) {
9657 c = *r++;
9658 if (c == '\0')
9659 break;
9661 ts = tok_alloc(p, r - p - 1);
9662 p = r;
9665 /* we add dummy defines for some special macros to speed up tests
9666 and to have working defined() */
9667 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9668 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9669 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9670 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9672 /* standard defines */
9673 tcc_define_symbol(s, "__STDC__", NULL);
9674 #if defined(TCC_TARGET_I386)
9675 tcc_define_symbol(s, "__i386__", NULL);
9676 #endif
9677 #if defined(TCC_TARGET_ARM)
9678 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
9679 tcc_define_symbol(s, "__arm_elf__", NULL);
9680 tcc_define_symbol(s, "__arm_elf", NULL);
9681 tcc_define_symbol(s, "arm_elf", NULL);
9682 tcc_define_symbol(s, "__arm__", NULL);
9683 tcc_define_symbol(s, "__arm", NULL);
9684 tcc_define_symbol(s, "arm", NULL);
9685 tcc_define_symbol(s, "__APCS_32__", NULL);
9686 #endif
9687 #if defined(linux)
9688 tcc_define_symbol(s, "__linux__", NULL);
9689 tcc_define_symbol(s, "linux", NULL);
9690 #endif
9691 /* tiny C specific defines */
9692 tcc_define_symbol(s, "__TINYC__", NULL);
9694 /* tiny C & gcc defines */
9695 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9696 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9697 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9699 /* default library paths */
9700 #ifdef TCC_TARGET_PE
9702 char buf[1024];
9703 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
9704 tcc_add_library_path(s, buf);
9706 #else
9707 tcc_add_library_path(s, "/usr/local/lib");
9708 tcc_add_library_path(s, "/usr/lib");
9709 tcc_add_library_path(s, "/lib");
9710 #endif
9712 /* no section zero */
9713 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9715 /* create standard sections */
9716 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9717 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9718 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9720 /* symbols are always generated for linking stage */
9721 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9722 ".strtab",
9723 ".hashtab", SHF_PRIVATE);
9724 strtab_section = symtab_section->link;
9726 /* private symbol table for dynamic symbols */
9727 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9728 ".dynstrtab",
9729 ".dynhashtab", SHF_PRIVATE);
9730 s->alacarte_link = 1;
9732 #ifdef CHAR_IS_UNSIGNED
9733 s->char_is_unsigned = 1;
9734 #endif
9735 return s;
9738 void tcc_delete(TCCState *s1)
9740 int i, n;
9742 /* free -D defines */
9743 free_defines(NULL);
9745 /* free tokens */
9746 n = tok_ident - TOK_IDENT;
9747 for(i = 0; i < n; i++)
9748 tcc_free(table_ident[i]);
9749 tcc_free(table_ident);
9751 /* free all sections */
9753 free_section(symtab_section->hash);
9755 free_section(s1->dynsymtab_section->hash);
9756 free_section(s1->dynsymtab_section->link);
9757 free_section(s1->dynsymtab_section);
9759 for(i = 1; i < s1->nb_sections; i++)
9760 free_section(s1->sections[i]);
9761 tcc_free(s1->sections);
9763 /* free loaded dlls array */
9764 for(i = 0; i < s1->nb_loaded_dlls; i++)
9765 tcc_free(s1->loaded_dlls[i]);
9766 tcc_free(s1->loaded_dlls);
9768 /* library paths */
9769 for(i = 0; i < s1->nb_library_paths; i++)
9770 tcc_free(s1->library_paths[i]);
9771 tcc_free(s1->library_paths);
9773 /* cached includes */
9774 for(i = 0; i < s1->nb_cached_includes; i++)
9775 tcc_free(s1->cached_includes[i]);
9776 tcc_free(s1->cached_includes);
9778 for(i = 0; i < s1->nb_include_paths; i++)
9779 tcc_free(s1->include_paths[i]);
9780 tcc_free(s1->include_paths);
9782 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9783 tcc_free(s1->sysinclude_paths[i]);
9784 tcc_free(s1->sysinclude_paths);
9786 tcc_free(s1);
9789 int tcc_add_include_path(TCCState *s1, const char *pathname)
9791 char *pathname1;
9793 pathname1 = tcc_strdup(pathname);
9794 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9795 return 0;
9798 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9800 char *pathname1;
9802 pathname1 = tcc_strdup(pathname);
9803 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9804 return 0;
9807 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9809 const char *ext, *filename1;
9810 Elf32_Ehdr ehdr;
9811 int fd, ret;
9812 BufferedFile *saved_file;
9814 /* find source file type with extension */
9815 filename1 = strrchr(filename, '/');
9816 if (filename1)
9817 filename1++;
9818 else
9819 filename1 = filename;
9820 ext = strrchr(filename1, '.');
9821 if (ext)
9822 ext++;
9824 /* open the file */
9825 saved_file = file;
9826 file = tcc_open(s1, filename);
9827 if (!file) {
9828 if (flags & AFF_PRINT_ERROR) {
9829 error_noabort("file '%s' not found", filename);
9831 ret = -1;
9832 goto fail1;
9835 if (!ext || !strcmp(ext, "c")) {
9836 /* C file assumed */
9837 ret = tcc_compile(s1);
9838 } else
9839 #ifdef CONFIG_TCC_ASM
9840 if (!strcmp(ext, "S")) {
9841 /* preprocessed assembler */
9842 ret = tcc_assemble(s1, 1);
9843 } else if (!strcmp(ext, "s")) {
9844 /* non preprocessed assembler */
9845 ret = tcc_assemble(s1, 0);
9846 } else
9847 #endif
9848 #ifdef TCC_TARGET_PE
9849 if (!strcmp(ext, "def")) {
9850 ret = pe_load_def_file(s1, fdopen(file->fd, "rb"));
9851 } else
9852 #endif
9854 fd = file->fd;
9855 /* assume executable format: auto guess file type */
9856 ret = read(fd, &ehdr, sizeof(ehdr));
9857 lseek(fd, 0, SEEK_SET);
9858 if (ret <= 0) {
9859 error_noabort("could not read header");
9860 goto fail;
9861 } else if (ret != sizeof(ehdr)) {
9862 goto try_load_script;
9865 if (ehdr.e_ident[0] == ELFMAG0 &&
9866 ehdr.e_ident[1] == ELFMAG1 &&
9867 ehdr.e_ident[2] == ELFMAG2 &&
9868 ehdr.e_ident[3] == ELFMAG3) {
9869 file->line_num = 0; /* do not display line number if error */
9870 if (ehdr.e_type == ET_REL) {
9871 ret = tcc_load_object_file(s1, fd, 0);
9872 } else if (ehdr.e_type == ET_DYN) {
9873 if (s1->output_type == TCC_OUTPUT_MEMORY) {
9874 #ifdef TCC_TARGET_PE
9875 ret = -1;
9876 #else
9877 void *h;
9878 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
9879 if (h)
9880 ret = 0;
9881 else
9882 ret = -1;
9883 #endif
9884 } else {
9885 ret = tcc_load_dll(s1, fd, filename,
9886 (flags & AFF_REFERENCED_DLL) != 0);
9888 } else {
9889 error_noabort("unrecognized ELF file");
9890 goto fail;
9892 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9893 file->line_num = 0; /* do not display line number if error */
9894 ret = tcc_load_archive(s1, fd);
9895 } else
9896 #ifdef TCC_TARGET_COFF
9897 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
9898 ret = tcc_load_coff(s1, fd);
9899 } else
9900 #endif
9902 /* as GNU ld, consider it is an ld script if not recognized */
9903 try_load_script:
9904 ret = tcc_load_ldscript(s1);
9905 if (ret < 0) {
9906 error_noabort("unrecognized file type");
9907 goto fail;
9911 the_end:
9912 tcc_close(file);
9913 fail1:
9914 file = saved_file;
9915 return ret;
9916 fail:
9917 ret = -1;
9918 goto the_end;
9921 int tcc_add_file(TCCState *s, const char *filename)
9923 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9926 int tcc_add_library_path(TCCState *s, const char *pathname)
9928 char *pathname1;
9930 pathname1 = tcc_strdup(pathname);
9931 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9932 return 0;
9935 /* find and load a dll. Return non zero if not found */
9936 /* XXX: add '-rpath' option support ? */
9937 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9939 char buf[1024];
9940 int i;
9942 for(i = 0; i < s->nb_library_paths; i++) {
9943 snprintf(buf, sizeof(buf), "%s/%s",
9944 s->library_paths[i], filename);
9945 if (tcc_add_file_internal(s, buf, flags) == 0)
9946 return 0;
9948 return -1;
9951 /* the library name is the same as the argument of the '-l' option */
9952 int tcc_add_library(TCCState *s, const char *libraryname)
9954 char buf[1024];
9955 int i;
9957 /* first we look for the dynamic library if not static linking */
9958 if (!s->static_link) {
9959 #ifdef TCC_TARGET_PE
9960 snprintf(buf, sizeof(buf), "%s.def", libraryname);
9961 #else
9962 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9963 #endif
9964 if (tcc_add_dll(s, buf, 0) == 0)
9965 return 0;
9968 /* then we look for the static library */
9969 for(i = 0; i < s->nb_library_paths; i++) {
9970 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9971 s->library_paths[i], libraryname);
9972 if (tcc_add_file_internal(s, buf, 0) == 0)
9973 return 0;
9975 return -1;
9978 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9980 add_elf_sym(symtab_section, val, 0,
9981 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
9982 SHN_ABS, name);
9983 return 0;
9986 int tcc_set_output_type(TCCState *s, int output_type)
9988 s->output_type = output_type;
9990 if (!s->nostdinc) {
9991 char buf[1024];
9993 /* default include paths */
9994 /* XXX: reverse order needed if -isystem support */
9995 #ifndef TCC_TARGET_PE
9996 tcc_add_sysinclude_path(s, "/usr/local/include");
9997 tcc_add_sysinclude_path(s, "/usr/include");
9998 #endif
9999 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10000 tcc_add_sysinclude_path(s, buf);
10001 #ifdef TCC_TARGET_PE
10002 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10003 tcc_add_sysinclude_path(s, buf);
10004 #endif
10007 /* if bound checking, then add corresponding sections */
10008 #ifdef CONFIG_TCC_BCHECK
10009 if (do_bounds_check) {
10010 /* define symbol */
10011 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10012 /* create bounds sections */
10013 bounds_section = new_section(s, ".bounds",
10014 SHT_PROGBITS, SHF_ALLOC);
10015 lbounds_section = new_section(s, ".lbounds",
10016 SHT_PROGBITS, SHF_ALLOC);
10018 #endif
10020 if (s->char_is_unsigned) {
10021 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10024 /* add debug sections */
10025 if (do_debug) {
10026 /* stab symbols */
10027 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10028 stab_section->sh_entsize = sizeof(Stab_Sym);
10029 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10030 put_elf_str(stabstr_section, "");
10031 stab_section->link = stabstr_section;
10032 /* put first entry */
10033 put_stabs("", 0, 0, 0, 0);
10036 /* add libc crt1/crti objects */
10037 #ifndef TCC_TARGET_PE
10038 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10039 !s->nostdlib) {
10040 if (output_type != TCC_OUTPUT_DLL)
10041 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10042 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10044 #endif
10045 return 0;
10048 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10049 #define FD_INVERT 0x0002 /* invert value before storing */
10051 typedef struct FlagDef {
10052 uint16_t offset;
10053 uint16_t flags;
10054 const char *name;
10055 } FlagDef;
10057 static const FlagDef warning_defs[] = {
10058 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10059 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10060 { offsetof(TCCState, warn_error), 0, "error" },
10061 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10062 "implicit-function-declaration" },
10065 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10066 const char *name, int value)
10068 int i;
10069 const FlagDef *p;
10070 const char *r;
10072 r = name;
10073 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10074 r += 3;
10075 value = !value;
10077 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10078 if (!strcmp(r, p->name))
10079 goto found;
10081 return -1;
10082 found:
10083 if (p->flags & FD_INVERT)
10084 value = !value;
10085 *(int *)((uint8_t *)s + p->offset) = value;
10086 return 0;
10090 /* set/reset a warning */
10091 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10093 int i;
10094 const FlagDef *p;
10096 if (!strcmp(warning_name, "all")) {
10097 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10098 if (p->flags & WD_ALL)
10099 *(int *)((uint8_t *)s + p->offset) = 1;
10101 return 0;
10102 } else {
10103 return set_flag(s, warning_defs, countof(warning_defs),
10104 warning_name, value);
10108 static const FlagDef flag_defs[] = {
10109 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10110 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10111 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10114 /* set/reset a flag */
10115 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10117 return set_flag(s, flag_defs, countof(flag_defs),
10118 flag_name, value);
10121 #if !defined(LIBTCC)
10123 /* extract the basename of a file */
10124 static const char *tcc_basename(const char *name)
10126 const char *p;
10127 p = strrchr(name, '/');
10128 #ifdef WIN32
10129 if (!p)
10130 p = strrchr(name, '\\');
10131 #endif
10132 if (!p)
10133 p = name;
10134 else
10135 p++;
10136 return p;
10139 static int64_t getclock_us(void)
10141 #ifdef WIN32
10142 struct _timeb tb;
10143 _ftime(&tb);
10144 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10145 #else
10146 struct timeval tv;
10147 gettimeofday(&tv, NULL);
10148 return tv.tv_sec * 1000000LL + tv.tv_usec;
10149 #endif
10152 void help(void)
10154 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2005 Fabrice Bellard\n"
10155 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10156 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
10157 " [infile1 infile2...] [-run infile args...]\n"
10158 "\n"
10159 "General options:\n"
10160 " -v display current version\n"
10161 " -c compile only - generate an object file\n"
10162 " -o outfile set output filename\n"
10163 " -Bdir set tcc internal library path\n"
10164 " -bench output compilation statistics\n"
10165 " -run run compiled source\n"
10166 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10167 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10168 " -w disable all warnings\n"
10169 "Preprocessor options:\n"
10170 " -Idir add include path 'dir'\n"
10171 " -Dsym[=val] define 'sym' with value 'val'\n"
10172 " -Usym undefine 'sym'\n"
10173 "Linker options:\n"
10174 " -Ldir add library path 'dir'\n"
10175 " -llib link with dynamic or static library 'lib'\n"
10176 " -shared generate a shared library\n"
10177 " -static static linking\n"
10178 " -rdynamic export all global symbols to dynamic linker\n"
10179 " -r relocatable output\n"
10180 "Debugger options:\n"
10181 " -g generate runtime debug info\n"
10182 #ifdef CONFIG_TCC_BCHECK
10183 " -b compile with built-in memory and bounds checker (implies -g)\n"
10184 #endif
10185 " -bt N show N callers in stack traces\n"
10189 #define TCC_OPTION_HAS_ARG 0x0001
10190 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10192 typedef struct TCCOption {
10193 const char *name;
10194 uint16_t index;
10195 uint16_t flags;
10196 } TCCOption;
10198 enum {
10199 TCC_OPTION_HELP,
10200 TCC_OPTION_I,
10201 TCC_OPTION_D,
10202 TCC_OPTION_U,
10203 TCC_OPTION_L,
10204 TCC_OPTION_B,
10205 TCC_OPTION_l,
10206 TCC_OPTION_bench,
10207 TCC_OPTION_bt,
10208 TCC_OPTION_b,
10209 TCC_OPTION_g,
10210 TCC_OPTION_c,
10211 TCC_OPTION_static,
10212 TCC_OPTION_shared,
10213 TCC_OPTION_o,
10214 TCC_OPTION_r,
10215 TCC_OPTION_Wl,
10216 TCC_OPTION_W,
10217 TCC_OPTION_O,
10218 TCC_OPTION_m,
10219 TCC_OPTION_f,
10220 TCC_OPTION_nostdinc,
10221 TCC_OPTION_nostdlib,
10222 TCC_OPTION_print_search_dirs,
10223 TCC_OPTION_rdynamic,
10224 TCC_OPTION_run,
10225 TCC_OPTION_v,
10226 TCC_OPTION_w,
10227 TCC_OPTION_pipe,
10230 static const TCCOption tcc_options[] = {
10231 { "h", TCC_OPTION_HELP, 0 },
10232 { "?", TCC_OPTION_HELP, 0 },
10233 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10234 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10235 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10236 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10237 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10238 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10239 { "bench", TCC_OPTION_bench, 0 },
10240 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10241 #ifdef CONFIG_TCC_BCHECK
10242 { "b", TCC_OPTION_b, 0 },
10243 #endif
10244 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10245 { "c", TCC_OPTION_c, 0 },
10246 { "static", TCC_OPTION_static, 0 },
10247 { "shared", TCC_OPTION_shared, 0 },
10248 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10249 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10250 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10251 { "r", TCC_OPTION_r, 0 },
10252 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10253 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10254 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10255 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10256 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10257 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10258 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10259 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10260 { "v", TCC_OPTION_v, 0 },
10261 { "w", TCC_OPTION_w, 0 },
10262 { "pipe", TCC_OPTION_pipe, 0},
10263 { NULL },
10266 /* convert 'str' into an array of space separated strings */
10267 static int expand_args(char ***pargv, const char *str)
10269 const char *s1;
10270 char **argv, *arg;
10271 int argc, len;
10273 argc = 0;
10274 argv = NULL;
10275 for(;;) {
10276 while (is_space(*str))
10277 str++;
10278 if (*str == '\0')
10279 break;
10280 s1 = str;
10281 while (*str != '\0' && !is_space(*str))
10282 str++;
10283 len = str - s1;
10284 arg = tcc_malloc(len + 1);
10285 memcpy(arg, s1, len);
10286 arg[len] = '\0';
10287 dynarray_add((void ***)&argv, &argc, arg);
10289 *pargv = argv;
10290 return argc;
10293 static char **files;
10294 static int nb_files, nb_libraries;
10295 static int multiple_files;
10296 static int print_search_dirs;
10297 static int output_type;
10298 static int reloc_output;
10299 static const char *outfile;
10301 int parse_args(TCCState *s, int argc, char **argv)
10303 int optind;
10304 const TCCOption *popt;
10305 const char *optarg, *p1, *r1;
10306 char *r;
10308 optind = 0;
10309 while (1) {
10310 if (optind >= argc) {
10311 if (nb_files == 0 && !print_search_dirs)
10312 goto show_help;
10313 else
10314 break;
10316 r = argv[optind++];
10317 if (r[0] != '-') {
10318 /* add a new file */
10319 dynarray_add((void ***)&files, &nb_files, r);
10320 if (!multiple_files) {
10321 optind--;
10322 /* argv[0] will be this file */
10323 break;
10325 } else {
10326 /* find option in table (match only the first chars */
10327 popt = tcc_options;
10328 for(;;) {
10329 p1 = popt->name;
10330 if (p1 == NULL)
10331 error("invalid option -- '%s'", r);
10332 r1 = r + 1;
10333 for(;;) {
10334 if (*p1 == '\0')
10335 goto option_found;
10336 if (*r1 != *p1)
10337 break;
10338 p1++;
10339 r1++;
10341 popt++;
10343 option_found:
10344 if (popt->flags & TCC_OPTION_HAS_ARG) {
10345 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
10346 optarg = r1;
10347 } else {
10348 if (optind >= argc)
10349 error("argument to '%s' is missing", r);
10350 optarg = argv[optind++];
10352 } else {
10353 if (*r1 != '\0')
10354 goto show_help;
10355 optarg = NULL;
10358 switch(popt->index) {
10359 case TCC_OPTION_HELP:
10360 show_help:
10361 help();
10362 exit(1);
10363 case TCC_OPTION_I:
10364 if (tcc_add_include_path(s, optarg) < 0)
10365 error("too many include paths");
10366 break;
10367 case TCC_OPTION_D:
10369 char *sym, *value;
10370 sym = (char *)optarg;
10371 value = strchr(sym, '=');
10372 if (value) {
10373 *value = '\0';
10374 value++;
10376 tcc_define_symbol(s, sym, value);
10378 break;
10379 case TCC_OPTION_U:
10380 tcc_undefine_symbol(s, optarg);
10381 break;
10382 case TCC_OPTION_L:
10383 tcc_add_library_path(s, optarg);
10384 break;
10385 case TCC_OPTION_B:
10386 /* set tcc utilities path (mainly for tcc development) */
10387 tcc_lib_path = optarg;
10388 break;
10389 case TCC_OPTION_l:
10390 dynarray_add((void ***)&files, &nb_files, r);
10391 nb_libraries++;
10392 break;
10393 case TCC_OPTION_bench:
10394 do_bench = 1;
10395 break;
10396 case TCC_OPTION_bt:
10397 num_callers = atoi(optarg);
10398 break;
10399 #ifdef CONFIG_TCC_BCHECK
10400 case TCC_OPTION_b:
10401 do_bounds_check = 1;
10402 do_debug = 1;
10403 break;
10404 #endif
10405 case TCC_OPTION_g:
10406 do_debug = 1;
10407 break;
10408 case TCC_OPTION_c:
10409 multiple_files = 1;
10410 output_type = TCC_OUTPUT_OBJ;
10411 break;
10412 case TCC_OPTION_static:
10413 s->static_link = 1;
10414 break;
10415 case TCC_OPTION_shared:
10416 output_type = TCC_OUTPUT_DLL;
10417 break;
10418 case TCC_OPTION_o:
10419 multiple_files = 1;
10420 outfile = optarg;
10421 break;
10422 case TCC_OPTION_r:
10423 /* generate a .o merging several output files */
10424 reloc_output = 1;
10425 output_type = TCC_OUTPUT_OBJ;
10426 break;
10427 case TCC_OPTION_nostdinc:
10428 s->nostdinc = 1;
10429 break;
10430 case TCC_OPTION_nostdlib:
10431 s->nostdlib = 1;
10432 break;
10433 case TCC_OPTION_print_search_dirs:
10434 print_search_dirs = 1;
10435 break;
10436 case TCC_OPTION_run:
10438 int argc1;
10439 char **argv1;
10440 argc1 = expand_args(&argv1, optarg);
10441 if (argc1 > 0) {
10442 parse_args(s, argc1, argv1);
10444 multiple_files = 0;
10445 output_type = TCC_OUTPUT_MEMORY;
10447 break;
10448 case TCC_OPTION_v:
10449 printf("tcc version %s\n", TCC_VERSION);
10450 exit(0);
10451 case TCC_OPTION_f:
10452 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
10453 goto unsupported_option;
10454 break;
10455 case TCC_OPTION_W:
10456 if (tcc_set_warning(s, optarg, 1) < 0 &&
10457 s->warn_unsupported)
10458 goto unsupported_option;
10459 break;
10460 case TCC_OPTION_w:
10461 s->warn_none = 1;
10462 break;
10463 case TCC_OPTION_rdynamic:
10464 s->rdynamic = 1;
10465 break;
10466 case TCC_OPTION_Wl:
10468 const char *p;
10469 if (strstart(optarg, "-Ttext,", &p)) {
10470 s->text_addr = strtoul(p, NULL, 16);
10471 s->has_text_addr = 1;
10472 } else if (strstart(optarg, "--oformat,", &p)) {
10473 if (strstart(p, "elf32-", NULL)) {
10474 s->output_format = TCC_OUTPUT_FORMAT_ELF;
10475 } else if (!strcmp(p, "binary")) {
10476 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
10477 } else
10478 #ifdef TCC_TARGET_COFF
10479 if (!strcmp(p, "coff")) {
10480 s->output_format = TCC_OUTPUT_FORMAT_COFF;
10481 } else
10482 #endif
10484 error("target %s not found", p);
10486 } else {
10487 error("unsupported linker option '%s'", optarg);
10490 break;
10491 default:
10492 if (s->warn_unsupported) {
10493 unsupported_option:
10494 warning("unsupported option '%s'", r);
10496 break;
10500 return optind;
10503 int main(int argc, char **argv)
10505 int i;
10506 TCCState *s;
10507 int nb_objfiles, ret, optind;
10508 char objfilename[1024];
10509 int64_t start_time = 0;
10511 #ifdef WIN32
10512 /* on win32, we suppose the lib and includes are at the location
10513 of 'tcc.exe' */
10515 static char path[1024];
10516 char *p, *d;
10518 GetModuleFileNameA(NULL, path, sizeof path);
10519 p = d = strlwr(path);
10520 while (*d)
10522 if (*d == '\\') *d = '/', p = d;
10523 ++d;
10525 *p = '\0';
10526 tcc_lib_path = path;
10528 #endif
10530 s = tcc_new();
10531 output_type = TCC_OUTPUT_EXE;
10532 outfile = NULL;
10533 multiple_files = 1;
10534 files = NULL;
10535 nb_files = 0;
10536 nb_libraries = 0;
10537 reloc_output = 0;
10538 print_search_dirs = 0;
10540 optind = parse_args(s, argc - 1, argv + 1) + 1;
10542 if (print_search_dirs) {
10543 /* enough for Linux kernel */
10544 printf("install: %s/\n", tcc_lib_path);
10545 return 0;
10548 nb_objfiles = nb_files - nb_libraries;
10550 /* if outfile provided without other options, we output an
10551 executable */
10552 if (outfile && output_type == TCC_OUTPUT_MEMORY)
10553 output_type = TCC_OUTPUT_EXE;
10555 /* check -c consistency : only single file handled. XXX: checks file type */
10556 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10557 /* accepts only a single input file */
10558 if (nb_objfiles != 1)
10559 error("cannot specify multiple files with -c");
10560 if (nb_libraries != 0)
10561 error("cannot specify libraries with -c");
10564 if (output_type != TCC_OUTPUT_MEMORY) {
10565 if (!outfile) {
10566 /* compute default outfile name */
10567 pstrcpy(objfilename, sizeof(objfilename) - 1,
10568 /* strip path */
10569 tcc_basename(files[0]));
10570 #ifdef TCC_TARGET_PE
10571 pe_guess_outfile(objfilename, output_type);
10572 #else
10573 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10574 char *ext = strrchr(objfilename, '.');
10575 if (!ext)
10576 goto default_outfile;
10577 /* add .o extension */
10578 strcpy(ext + 1, "o");
10579 } else {
10580 default_outfile:
10581 pstrcpy(objfilename, sizeof(objfilename), "a.out");
10583 #endif
10584 outfile = objfilename;
10588 if (do_bench) {
10589 start_time = getclock_us();
10592 tcc_set_output_type(s, output_type);
10594 /* compile or add each files or library */
10595 for(i = 0;i < nb_files; i++) {
10596 const char *filename;
10598 filename = files[i];
10599 if (filename[0] == '-') {
10600 if (tcc_add_library(s, filename + 2) < 0)
10601 error("cannot find %s", filename);
10602 } else {
10603 if (tcc_add_file(s, filename) < 0) {
10604 ret = 1;
10605 goto the_end;
10610 /* free all files */
10611 tcc_free(files);
10613 if (do_bench) {
10614 double total_time;
10615 total_time = (double)(getclock_us() - start_time) / 1000000.0;
10616 if (total_time < 0.001)
10617 total_time = 0.001;
10618 if (total_bytes < 1)
10619 total_bytes = 1;
10620 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
10621 tok_ident - TOK_IDENT, total_lines, total_bytes,
10622 total_time, (int)(total_lines / total_time),
10623 total_bytes / total_time / 1000000.0);
10626 if (s->output_type == TCC_OUTPUT_MEMORY) {
10627 ret = tcc_run(s, argc - optind, argv + optind);
10628 } else
10629 #ifdef TCC_TARGET_PE
10630 if (s->output_type != TCC_OUTPUT_OBJ) {
10631 ret = tcc_output_pe(s, outfile);
10632 } else
10633 #endif
10635 tcc_output_file(s, outfile);
10636 ret = 0;
10638 the_end:
10639 /* XXX: cannot do it with bound checking because of the malloc hooks */
10640 if (!do_bounds_check)
10641 tcc_delete(s);
10643 #ifdef MEM_DEBUG
10644 if (do_bench) {
10645 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
10647 #endif
10648 return ret;
10651 #endif