Suport LDOUBLE_SIZE == 16 environment.
[tinycc.git] / tcc.c
blobafb1343da4ffd7e05fd6550c6140dc0e356a29c7
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 #include <windows.h>
44 #endif
45 #ifndef _WIN32
46 #include <sys/time.h>
47 #include <sys/ucontext.h>
48 #include <sys/mman.h>
49 #endif
51 #endif /* !CONFIG_TCCBOOT */
53 #ifndef PAGESIZE
54 #define PAGESIZE 4096
55 #endif
57 #include "elf.h"
58 #include "stab.h"
60 #ifndef O_BINARY
61 #define O_BINARY 0
62 #endif
64 #include "libtcc.h"
66 /* parser debug */
67 //#define PARSE_DEBUG
68 /* preprocessor debug */
69 //#define PP_DEBUG
70 /* include file debug */
71 //#define INC_DEBUG
73 //#define MEM_DEBUG
75 /* assembler debug */
76 //#define ASM_DEBUG
78 /* target selection */
79 //#define TCC_TARGET_I386 /* i386 code generator */
80 //#define TCC_TARGET_ARM /* ARMv4 code generator */
81 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
83 /* default target is I386 */
84 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
85 !defined(TCC_TARGET_C67)
86 #define TCC_TARGET_I386
87 #endif
89 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
90 !defined(TCC_TARGET_C67)
91 #define CONFIG_TCC_BCHECK /* enable bound checking code */
92 #endif
94 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
95 #define CONFIG_TCC_STATIC
96 #endif
98 /* define it to include assembler support */
99 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
100 #define CONFIG_TCC_ASM
101 #endif
103 /* object format selection */
104 #if defined(TCC_TARGET_C67)
105 #define TCC_TARGET_COFF
106 #endif
108 #define FALSE 0
109 #define false 0
110 #define TRUE 1
111 #define true 1
112 typedef int BOOL;
114 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
115 executables or dlls */
116 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
118 #define INCLUDE_STACK_SIZE 32
119 #define IFDEF_STACK_SIZE 64
120 #define VSTACK_SIZE 256
121 #define STRING_MAX_SIZE 1024
122 #define PACK_STACK_SIZE 8
124 #define TOK_HASH_SIZE 8192 /* must be a power of two */
125 #define TOK_ALLOC_INCR 512 /* must be a power of two */
126 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
128 /* token symbol management */
129 typedef struct TokenSym {
130 struct TokenSym *hash_next;
131 struct Sym *sym_define; /* direct pointer to define */
132 struct Sym *sym_label; /* direct pointer to label */
133 struct Sym *sym_struct; /* direct pointer to structure */
134 struct Sym *sym_identifier; /* direct pointer to identifier */
135 int tok; /* token number */
136 int len;
137 char str[1];
138 } TokenSym;
140 #ifdef TCC_TARGET_PE
141 typedef unsigned short nwchar_t;
142 #else
143 typedef int nwchar_t;
144 #endif
146 typedef struct CString {
147 int size; /* size in bytes */
148 void *data; /* either 'char *' or 'nwchar_t *' */
149 int size_allocated;
150 void *data_allocated; /* if non NULL, data has been malloced */
151 } CString;
153 /* type definition */
154 typedef struct CType {
155 int t;
156 struct Sym *ref;
157 } CType;
159 /* constant value */
160 typedef union CValue {
161 long double ld;
162 double d;
163 float f;
164 int i;
165 unsigned int ui;
166 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
167 long long ll;
168 unsigned long long ull;
169 struct CString *cstr;
170 void *ptr;
171 int tab[1];
172 } CValue;
174 /* value on stack */
175 typedef struct SValue {
176 CType type; /* type */
177 unsigned short r; /* register + flags */
178 unsigned short r2; /* second register, used for 'long long'
179 type. If not used, set to VT_CONST */
180 CValue c; /* constant, if VT_CONST */
181 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
182 } SValue;
184 /* symbol management */
185 typedef struct Sym {
186 int v; /* symbol token */
187 long r; /* associated register */
188 long c; /* associated number */
189 CType type; /* associated type */
190 struct Sym *next; /* next related symbol */
191 struct Sym *prev; /* prev symbol in stack */
192 struct Sym *prev_tok; /* previous symbol for this token */
193 } Sym;
195 /* section definition */
196 /* XXX: use directly ELF structure for parameters ? */
197 /* special flag to indicate that the section should not be linked to
198 the other ones */
199 #define SHF_PRIVATE 0x80000000
201 typedef struct Section {
202 unsigned long data_offset; /* current data offset */
203 unsigned char *data; /* section data */
204 unsigned long data_allocated; /* used for realloc() handling */
205 int sh_name; /* elf section name (only used during output) */
206 int sh_num; /* elf section number */
207 int sh_type; /* elf section type */
208 int sh_flags; /* elf section flags */
209 int sh_info; /* elf section info */
210 int sh_addralign; /* elf section alignment */
211 int sh_entsize; /* elf entry size */
212 unsigned long sh_size; /* section size (only used during output) */
213 unsigned long sh_addr; /* address at which the section is relocated */
214 unsigned long sh_offset; /* file offset */
215 int nb_hashed_syms; /* used to resize the hash table */
216 struct Section *link; /* link to another section */
217 struct Section *reloc; /* corresponding section for relocation, if any */
218 struct Section *hash; /* hash table for symbols */
219 struct Section *next;
220 char name[1]; /* section name */
221 } Section;
223 typedef struct DLLReference {
224 int level;
225 void *handle;
226 char name[1];
227 } DLLReference;
229 /* GNUC attribute definition */
230 typedef struct AttributeDef {
231 int aligned;
232 int packed;
233 Section *section;
234 int func_attr; /* calling convention, exports, ... */
235 } AttributeDef;
237 /* -------------------------------------------------- */
238 /* gr: wrappers for casting sym->r for other purposes */
239 typedef struct {
240 unsigned
241 func_call : 8,
242 func_args : 8,
243 func_export : 1;
244 } func_attr_t;
246 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
247 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
248 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
249 #define INLINE_DEF(r) (*(int **)&(r))
250 /* -------------------------------------------------- */
252 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
253 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
254 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
256 /* stored in 'Sym.c' field */
257 #define FUNC_NEW 1 /* ansi function prototype */
258 #define FUNC_OLD 2 /* old function prototype */
259 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
261 /* stored in 'Sym.r' field */
262 #define FUNC_CDECL 0 /* standard c call */
263 #define FUNC_STDCALL 1 /* pascal c call */
264 #define FUNC_FASTCALL1 2 /* first param in %eax */
265 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
266 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
267 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
269 /* field 'Sym.t' for macros */
270 #define MACRO_OBJ 0 /* object like macro */
271 #define MACRO_FUNC 1 /* function like macro */
273 /* field 'Sym.r' for C labels */
274 #define LABEL_DEFINED 0 /* label is defined */
275 #define LABEL_FORWARD 1 /* label is forward defined */
276 #define LABEL_DECLARED 2 /* label is declared but never used */
278 /* type_decl() types */
279 #define TYPE_ABSTRACT 1 /* type without variable */
280 #define TYPE_DIRECT 2 /* type with variable */
282 #define IO_BUF_SIZE 8192
284 typedef struct BufferedFile {
285 uint8_t *buf_ptr;
286 uint8_t *buf_end;
287 int fd;
288 int line_num; /* current line number - here to simplify code */
289 int ifndef_macro; /* #ifndef macro / #endif search */
290 int ifndef_macro_saved; /* saved ifndef_macro */
291 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
292 char inc_type; /* type of include */
293 char inc_filename[512]; /* filename specified by the user */
294 char filename[1024]; /* current filename - here to simplify code */
295 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
296 } BufferedFile;
298 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
299 #define CH_EOF (-1) /* end of file */
301 /* parsing state (used to save parser state to reparse part of the
302 source several times) */
303 typedef struct ParseState {
304 int *macro_ptr;
305 int line_num;
306 int tok;
307 CValue tokc;
308 } ParseState;
310 /* used to record tokens */
311 typedef struct TokenString {
312 int *str;
313 int len;
314 int allocated_len;
315 int last_line_num;
316 } TokenString;
318 /* include file cache, used to find files faster and also to eliminate
319 inclusion if the include file is protected by #ifndef ... #endif */
320 typedef struct CachedInclude {
321 int ifndef_macro;
322 int hash_next; /* -1 if none */
323 char type; /* '"' or '>' to give include type */
324 char filename[1]; /* path specified in #include */
325 } CachedInclude;
327 #define CACHED_INCLUDES_HASH_SIZE 512
329 /* parser */
330 static struct BufferedFile *file;
331 static int ch, tok;
332 static CValue tokc;
333 static CString tokcstr; /* current parsed string, if any */
334 /* additional informations about token */
335 static int tok_flags;
336 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
337 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
338 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
339 #define TOK_FLAG_EOF 0x0008 /* end of file */
341 static int *macro_ptr, *macro_ptr_allocated;
342 static int *unget_saved_macro_ptr;
343 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
344 static int unget_buffer_enabled;
345 static int parse_flags;
346 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
347 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
348 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
349 token. line feed is also
350 returned at eof */
351 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
353 static Section *text_section, *data_section, *bss_section; /* predefined sections */
354 static Section *cur_text_section; /* current section where function code is
355 generated */
356 #ifdef CONFIG_TCC_ASM
357 static Section *last_text_section; /* to handle .previous asm directive */
358 #endif
359 /* bound check related sections */
360 static Section *bounds_section; /* contains global data bound description */
361 static Section *lbounds_section; /* contains local data bound description */
362 /* symbol sections */
363 static Section *symtab_section, *strtab_section;
365 /* debug sections */
366 static Section *stab_section, *stabstr_section;
368 /* loc : local variable index
369 ind : output code index
370 rsym: return symbol
371 anon_sym: anonymous symbol index
373 static int rsym, anon_sym, ind, loc;
374 /* expression generation modifiers */
375 static int const_wanted; /* true if constant wanted */
376 static int nocode_wanted; /* true if no code generation wanted for an expression */
377 static int global_expr; /* true if compound literals must be allocated
378 globally (used during initializers parsing */
379 static CType func_vt; /* current function return type (used by return
380 instruction) */
381 static int func_vc;
382 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
383 static int tok_ident;
384 static TokenSym **table_ident;
385 static TokenSym *hash_ident[TOK_HASH_SIZE];
386 static char token_buf[STRING_MAX_SIZE + 1];
387 static char *funcname;
388 static Sym *global_stack, *local_stack;
389 static Sym *define_stack;
390 static Sym *global_label_stack, *local_label_stack;
391 /* symbol allocator */
392 #define SYM_POOL_NB (8192 / sizeof(Sym))
393 static Sym *sym_free_first;
394 static void **sym_pools;
395 static int nb_sym_pools;
397 static SValue vstack[VSTACK_SIZE], *vtop;
398 /* some predefined types */
399 static CType char_pointer_type, func_old_type, int_type;
400 /* true if isid(c) || isnum(c) */
401 static unsigned char isidnum_table[256-CH_EOF];
403 /* display some information during compilation */
404 static int verbose = 0;
406 /* compile with debug symbol (and use them if error during execution) */
407 static int do_debug = 0;
409 /* compile with built-in memory and bounds checker */
410 static int do_bounds_check = 0;
412 /* display benchmark infos */
413 #if !defined(LIBTCC)
414 static int do_bench = 0;
415 #endif
416 static int total_lines;
417 static int total_bytes;
419 /* use GNU C extensions */
420 static int gnu_ext = 1;
422 /* use Tiny C extensions */
423 static int tcc_ext = 1;
425 /* max number of callers shown if error */
426 static int num_callers = 6;
427 static const char **rt_bound_error_msg;
429 /* XXX: get rid of this ASAP */
430 static struct TCCState *tcc_state;
432 /* give the path of the tcc libraries */
433 static const char *tcc_lib_path = CONFIG_TCCDIR;
435 struct TCCState {
436 int output_type;
438 BufferedFile **include_stack_ptr;
439 int *ifdef_stack_ptr;
441 /* include file handling */
442 char **include_paths;
443 int nb_include_paths;
444 char **sysinclude_paths;
445 int nb_sysinclude_paths;
446 CachedInclude **cached_includes;
447 int nb_cached_includes;
449 char **library_paths;
450 int nb_library_paths;
452 /* array of all loaded dlls (including those referenced by loaded
453 dlls) */
454 DLLReference **loaded_dlls;
455 int nb_loaded_dlls;
457 /* sections */
458 Section **sections;
459 int nb_sections; /* number of sections, including first dummy section */
461 /* got handling */
462 Section *got;
463 Section *plt;
464 unsigned long *got_offsets;
465 int nb_got_offsets;
466 /* give the correspondance from symtab indexes to dynsym indexes */
467 int *symtab_to_dynsym;
469 /* temporary dynamic symbol sections (for dll loading) */
470 Section *dynsymtab_section;
471 /* exported dynamic symbol section */
472 Section *dynsym;
474 int nostdinc; /* if true, no standard headers are added */
475 int nostdlib; /* if true, no standard libraries are added */
477 int nocommon; /* if true, do not use common symbols for .bss data */
479 /* if true, static linking is performed */
480 int static_link;
482 /* soname as specified on the command line (-soname) */
483 const char *soname;
485 /* if true, all symbols are exported */
486 int rdynamic;
488 /* if true, only link in referenced objects from archive */
489 int alacarte_link;
491 /* address of text section */
492 unsigned long text_addr;
493 int has_text_addr;
495 /* output format, see TCC_OUTPUT_FORMAT_xxx */
496 int output_format;
498 /* C language options */
499 int char_is_unsigned;
500 int leading_underscore;
502 /* warning switches */
503 int warn_write_strings;
504 int warn_unsupported;
505 int warn_error;
506 int warn_none;
507 int warn_implicit_function_declaration;
509 /* error handling */
510 void *error_opaque;
511 void (*error_func)(void *opaque, const char *msg);
512 int error_set_jmp_enabled;
513 jmp_buf error_jmp_buf;
514 int nb_errors;
516 /* tiny assembler state */
517 Sym *asm_labels;
519 /* see include_stack_ptr */
520 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
522 /* see ifdef_stack_ptr */
523 int ifdef_stack[IFDEF_STACK_SIZE];
525 /* see cached_includes */
526 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
528 /* pack stack */
529 int pack_stack[PACK_STACK_SIZE];
530 int *pack_stack_ptr;
532 /* output file for preprocessing */
533 FILE *outfile;
536 /* The current value can be: */
537 #define VT_VALMASK 0x00ff
538 #define VT_CONST 0x00f0 /* constant in vc
539 (must be first non register value) */
540 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
541 #define VT_LOCAL 0x00f2 /* offset on stack */
542 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
543 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
544 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
545 #define VT_LVAL 0x0100 /* var is an lvalue */
546 #define VT_SYM 0x0200 /* a symbol value is added */
547 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
548 char/short stored in integer registers) */
549 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
550 dereferencing value */
551 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
552 bounding function call point is in vc */
553 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
554 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
555 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
556 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
558 /* types */
559 #define VT_INT 0 /* integer type */
560 #define VT_BYTE 1 /* signed byte type */
561 #define VT_SHORT 2 /* short type */
562 #define VT_VOID 3 /* void type */
563 #define VT_PTR 4 /* pointer */
564 #define VT_ENUM 5 /* enum definition */
565 #define VT_FUNC 6 /* function type */
566 #define VT_STRUCT 7 /* struct/union definition */
567 #define VT_FLOAT 8 /* IEEE float */
568 #define VT_DOUBLE 9 /* IEEE double */
569 #define VT_LDOUBLE 10 /* IEEE long double */
570 #define VT_BOOL 11 /* ISOC99 boolean type */
571 #define VT_LLONG 12 /* 64 bit integer */
572 #define VT_LONG 13 /* long integer (NEVER USED as type, only
573 during parsing) */
574 #define VT_BTYPE 0x000f /* mask for basic type */
575 #define VT_UNSIGNED 0x0010 /* unsigned type */
576 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
577 #define VT_BITFIELD 0x0040 /* bitfield modifier */
578 #define VT_CONSTANT 0x0800 /* const modifier */
579 #define VT_VOLATILE 0x1000 /* volatile modifier */
580 #define VT_SIGNED 0x2000 /* signed type */
582 /* storage */
583 #define VT_EXTERN 0x00000080 /* extern definition */
584 #define VT_STATIC 0x00000100 /* static variable */
585 #define VT_TYPEDEF 0x00000200 /* typedef definition */
586 #define VT_INLINE 0x00000400 /* inline definition */
588 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
590 /* type mask (except storage) */
591 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
592 #define VT_TYPE (~(VT_STORAGE))
594 /* token values */
596 /* warning: the following compare tokens depend on i386 asm code */
597 #define TOK_ULT 0x92
598 #define TOK_UGE 0x93
599 #define TOK_EQ 0x94
600 #define TOK_NE 0x95
601 #define TOK_ULE 0x96
602 #define TOK_UGT 0x97
603 #define TOK_Nset 0x98
604 #define TOK_Nclear 0x99
605 #define TOK_LT 0x9c
606 #define TOK_GE 0x9d
607 #define TOK_LE 0x9e
608 #define TOK_GT 0x9f
610 #define TOK_LAND 0xa0
611 #define TOK_LOR 0xa1
613 #define TOK_DEC 0xa2
614 #define TOK_MID 0xa3 /* inc/dec, to void constant */
615 #define TOK_INC 0xa4
616 #define TOK_UDIV 0xb0 /* unsigned division */
617 #define TOK_UMOD 0xb1 /* unsigned modulo */
618 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
619 #define TOK_CINT 0xb3 /* number in tokc */
620 #define TOK_CCHAR 0xb4 /* char constant in tokc */
621 #define TOK_STR 0xb5 /* pointer to string in tokc */
622 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
623 #define TOK_LCHAR 0xb7
624 #define TOK_LSTR 0xb8
625 #define TOK_CFLOAT 0xb9 /* float constant */
626 #define TOK_LINENUM 0xba /* line number info */
627 #define TOK_CDOUBLE 0xc0 /* double constant */
628 #define TOK_CLDOUBLE 0xc1 /* long double constant */
629 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
630 #define TOK_ADDC1 0xc3 /* add with carry generation */
631 #define TOK_ADDC2 0xc4 /* add with carry use */
632 #define TOK_SUBC1 0xc5 /* add with carry generation */
633 #define TOK_SUBC2 0xc6 /* add with carry use */
634 #define TOK_CUINT 0xc8 /* unsigned int constant */
635 #define TOK_CLLONG 0xc9 /* long long constant */
636 #define TOK_CULLONG 0xca /* unsigned long long constant */
637 #define TOK_ARROW 0xcb
638 #define TOK_DOTS 0xcc /* three dots */
639 #define TOK_SHR 0xcd /* unsigned shift right */
640 #define TOK_PPNUM 0xce /* preprocessor number */
642 #define TOK_SHL 0x01 /* shift left */
643 #define TOK_SAR 0x02 /* signed shift right */
645 /* assignement operators : normal operator or 0x80 */
646 #define TOK_A_MOD 0xa5
647 #define TOK_A_AND 0xa6
648 #define TOK_A_MUL 0xaa
649 #define TOK_A_ADD 0xab
650 #define TOK_A_SUB 0xad
651 #define TOK_A_DIV 0xaf
652 #define TOK_A_XOR 0xde
653 #define TOK_A_OR 0xfc
654 #define TOK_A_SHL 0x81
655 #define TOK_A_SAR 0x82
657 #ifndef offsetof
658 #define offsetof(type, field) ((size_t) &((type *)0)->field)
659 #endif
661 #ifndef countof
662 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
663 #endif
665 /* WARNING: the content of this string encodes token numbers */
666 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";
668 #define TOK_EOF (-1) /* end of file */
669 #define TOK_LINEFEED 10 /* line feed */
671 /* all identificators and strings have token above that */
672 #define TOK_IDENT 256
674 /* only used for i386 asm opcodes definitions */
675 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
677 #define DEF_BWL(x) \
678 DEF(TOK_ASM_ ## x ## b, #x "b") \
679 DEF(TOK_ASM_ ## x ## w, #x "w") \
680 DEF(TOK_ASM_ ## x ## l, #x "l") \
681 DEF(TOK_ASM_ ## x, #x)
683 #define DEF_WL(x) \
684 DEF(TOK_ASM_ ## x ## w, #x "w") \
685 DEF(TOK_ASM_ ## x ## l, #x "l") \
686 DEF(TOK_ASM_ ## x, #x)
688 #define DEF_FP1(x) \
689 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
690 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
691 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
692 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
694 #define DEF_FP(x) \
695 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
696 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
697 DEF_FP1(x)
699 #define DEF_ASMTEST(x) \
700 DEF_ASM(x ## o) \
701 DEF_ASM(x ## no) \
702 DEF_ASM(x ## b) \
703 DEF_ASM(x ## c) \
704 DEF_ASM(x ## nae) \
705 DEF_ASM(x ## nb) \
706 DEF_ASM(x ## nc) \
707 DEF_ASM(x ## ae) \
708 DEF_ASM(x ## e) \
709 DEF_ASM(x ## z) \
710 DEF_ASM(x ## ne) \
711 DEF_ASM(x ## nz) \
712 DEF_ASM(x ## be) \
713 DEF_ASM(x ## na) \
714 DEF_ASM(x ## nbe) \
715 DEF_ASM(x ## a) \
716 DEF_ASM(x ## s) \
717 DEF_ASM(x ## ns) \
718 DEF_ASM(x ## p) \
719 DEF_ASM(x ## pe) \
720 DEF_ASM(x ## np) \
721 DEF_ASM(x ## po) \
722 DEF_ASM(x ## l) \
723 DEF_ASM(x ## nge) \
724 DEF_ASM(x ## nl) \
725 DEF_ASM(x ## ge) \
726 DEF_ASM(x ## le) \
727 DEF_ASM(x ## ng) \
728 DEF_ASM(x ## nle) \
729 DEF_ASM(x ## g)
731 #define TOK_ASM_int TOK_INT
733 enum tcc_token {
734 TOK_LAST = TOK_IDENT - 1,
735 #define DEF(id, str) id,
736 #include "tcctok.h"
737 #undef DEF
740 static const char tcc_keywords[] =
741 #define DEF(id, str) str "\0"
742 #include "tcctok.h"
743 #undef DEF
746 #define TOK_UIDENT TOK_DEFINE
748 #ifdef _WIN32
749 #define snprintf _snprintf
750 #define vsnprintf _vsnprintf
751 #ifndef __GNUC__
752 #define strtold (long double)strtod
753 #define strtof (float)strtod
754 #define strtoll (long long)strtol
755 #endif
756 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
757 || defined(__OpenBSD__)
758 /* currently incorrect */
759 long double strtold(const char *nptr, char **endptr)
761 return (long double)strtod(nptr, endptr);
763 float strtof(const char *nptr, char **endptr)
765 return (float)strtod(nptr, endptr);
767 #else
768 /* XXX: need to define this to use them in non ISOC99 context */
769 extern float strtof (const char *__nptr, char **__endptr);
770 extern long double strtold (const char *__nptr, char **__endptr);
771 #endif
773 static char *pstrcpy(char *buf, int buf_size, const char *s);
774 static char *pstrcat(char *buf, int buf_size, const char *s);
775 static char *tcc_basename(const char *name);
776 static char *tcc_fileextension (const char *p);
778 static void next(void);
779 static void next_nomacro(void);
780 static void parse_expr_type(CType *type);
781 static void expr_type(CType *type);
782 static void unary_type(CType *type);
783 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
784 int case_reg, int is_expr);
785 static int expr_const(void);
786 static void expr_eq(void);
787 static void gexpr(void);
788 static void gen_inline_functions(void);
789 static void decl(int l);
790 static void decl_initializer(CType *type, Section *sec, unsigned long c,
791 int first, int size_only);
792 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
793 int has_init, int v, int scope);
794 int gv(int rc);
795 void gv2(int rc1, int rc2);
796 void move_reg(int r, int s);
797 void save_regs(int n);
798 void save_reg(int r);
799 void vpop(void);
800 void vswap(void);
801 void vdup(void);
802 int get_reg(int rc);
803 int get_reg_ex(int rc,int rc2);
805 struct macro_level {
806 struct macro_level *prev;
807 int *p;
810 static void macro_subst(TokenString *tok_str, Sym **nested_list,
811 const int *macro_str, struct macro_level **can_read_stream);
812 void gen_op(int op);
813 void force_charshort_cast(int t);
814 static void gen_cast(CType *type);
815 void vstore(void);
816 static Sym *sym_find(int v);
817 static Sym *sym_push(int v, CType *type, int r, int c);
819 /* type handling */
820 static int type_size(CType *type, int *a);
821 static inline CType *pointed_type(CType *type);
822 static int pointed_size(CType *type);
823 static int lvalue_type(int t);
824 static int parse_btype(CType *type, AttributeDef *ad);
825 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
826 static int compare_types(CType *type1, CType *type2, int unqualified);
827 static int is_compatible_types(CType *type1, CType *type2);
828 static int is_compatible_parameter_types(CType *type1, CType *type2);
830 int ieee_finite(double d);
831 void error(const char *fmt, ...);
832 void vpushi(int v);
833 void vrott(int n);
834 void vnrott(int n);
835 void lexpand_nr(void);
836 static void vpush_global_sym(CType *type, int v);
837 void vset(CType *type, int r, int v);
838 void type_to_str(char *buf, int buf_size,
839 CType *type, const char *varstr);
840 char *get_tok_str(int v, CValue *cv);
841 static Sym *get_sym_ref(CType *type, Section *sec,
842 unsigned long offset, unsigned long size);
843 static Sym *external_global_sym(int v, CType *type, int r);
845 /* section generation */
846 static void section_realloc(Section *sec, unsigned long new_size);
847 static void *section_ptr_add(Section *sec, unsigned long size);
848 static void put_extern_sym(Sym *sym, Section *section,
849 unsigned long value, unsigned long size);
850 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
851 static int put_elf_str(Section *s, const char *sym);
852 static int put_elf_sym(Section *s,
853 unsigned long value, unsigned long size,
854 int info, int other, int shndx, const char *name);
855 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
856 int info, int other, int sh_num, const char *name);
857 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
858 int type, int symbol);
859 static void put_stabs(const char *str, int type, int other, int desc,
860 unsigned long value);
861 static void put_stabs_r(const char *str, int type, int other, int desc,
862 unsigned long value, Section *sec, int sym_index);
863 static void put_stabn(int type, int other, int desc, int value);
864 static void put_stabd(int type, int other, int desc);
865 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
867 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
868 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
869 #define AFF_PREPROCESS 0x0004 /* preprocess file */
870 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
872 /* tcccoff.c */
873 int tcc_output_coff(TCCState *s1, FILE *f);
875 /* tccpe.c */
876 void *resolve_sym(TCCState *s1, const char *sym, int type);
877 int pe_load_def_file(struct TCCState *s1, int fd);
878 int pe_test_res_file(void *v, int size);
879 int pe_load_res_file(struct TCCState *s1, int fd);
880 void pe_add_runtime(struct TCCState *s1);
881 void pe_guess_outfile(char *objfilename, int output_type);
882 int pe_output_file(struct TCCState *s1, const char *filename);
884 /* tccasm.c */
886 #ifdef CONFIG_TCC_ASM
888 typedef struct ExprValue {
889 uint32_t v;
890 Sym *sym;
891 } ExprValue;
893 #define MAX_ASM_OPERANDS 30
895 typedef struct ASMOperand {
896 int id; /* GCC 3 optionnal identifier (0 if number only supported */
897 char *constraint;
898 char asm_str[16]; /* computed asm string for operand */
899 SValue *vt; /* C value of the expression */
900 int ref_index; /* if >= 0, gives reference to a output constraint */
901 int input_index; /* if >= 0, gives reference to an input constraint */
902 int priority; /* priority, used to assign registers */
903 int reg; /* if >= 0, register number used for this operand */
904 int is_llong; /* true if double register value */
905 int is_memory; /* true if memory operand */
906 int is_rw; /* for '+' modifier */
907 } ASMOperand;
909 static void asm_expr(TCCState *s1, ExprValue *pe);
910 static int asm_int_expr(TCCState *s1);
911 static int find_constraint(ASMOperand *operands, int nb_operands,
912 const char *name, const char **pp);
914 static int tcc_assemble(TCCState *s1, int do_preprocess);
916 #endif
918 static void asm_instr(void);
919 static void asm_global_instr(void);
921 /* true if float/double/long double type */
922 static inline int is_float(int t)
924 int bt;
925 bt = t & VT_BTYPE;
926 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
929 #ifdef TCC_TARGET_I386
930 #include "i386-gen.c"
931 #endif
933 #ifdef TCC_TARGET_ARM
934 #include "arm-gen.c"
935 #endif
937 #ifdef TCC_TARGET_C67
938 #include "c67-gen.c"
939 #endif
941 #ifdef CONFIG_TCC_STATIC
943 #define RTLD_LAZY 0x001
944 #define RTLD_NOW 0x002
945 #define RTLD_GLOBAL 0x100
946 #define RTLD_DEFAULT NULL
948 /* dummy function for profiling */
949 void *dlopen(const char *filename, int flag)
951 return NULL;
954 const char *dlerror(void)
956 return "error";
959 typedef struct TCCSyms {
960 char *str;
961 void *ptr;
962 } TCCSyms;
964 #define TCCSYM(a) { #a, &a, },
966 /* add the symbol you want here if no dynamic linking is done */
967 static TCCSyms tcc_syms[] = {
968 #if !defined(CONFIG_TCCBOOT)
969 TCCSYM(printf)
970 TCCSYM(fprintf)
971 TCCSYM(fopen)
972 TCCSYM(fclose)
973 #endif
974 { NULL, NULL },
977 void *resolve_sym(TCCState *s1, const char *symbol, int type)
979 TCCSyms *p;
980 p = tcc_syms;
981 while (p->str != NULL) {
982 if (!strcmp(p->str, symbol))
983 return p->ptr;
984 p++;
986 return NULL;
989 #elif !defined(_WIN32)
991 #include <dlfcn.h>
993 void *resolve_sym(TCCState *s1, const char *sym, int type)
995 return dlsym(RTLD_DEFAULT, sym);
998 #endif
1000 /********************************************************/
1002 /* we use our own 'finite' function to avoid potential problems with
1003 non standard math libs */
1004 /* XXX: endianness dependent */
1005 int ieee_finite(double d)
1007 int *p = (int *)&d;
1008 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
1011 /* copy a string and truncate it. */
1012 static char *pstrcpy(char *buf, int buf_size, const char *s)
1014 char *q, *q_end;
1015 int c;
1017 if (buf_size > 0) {
1018 q = buf;
1019 q_end = buf + buf_size - 1;
1020 while (q < q_end) {
1021 c = *s++;
1022 if (c == '\0')
1023 break;
1024 *q++ = c;
1026 *q = '\0';
1028 return buf;
1031 /* strcat and truncate. */
1032 static char *pstrcat(char *buf, int buf_size, const char *s)
1034 int len;
1035 len = strlen(buf);
1036 if (len < buf_size)
1037 pstrcpy(buf + len, buf_size - len, s);
1038 return buf;
1041 #ifndef LIBTCC
1042 static int strstart(const char *str, const char *val, const char **ptr)
1044 const char *p, *q;
1045 p = str;
1046 q = val;
1047 while (*q != '\0') {
1048 if (*p != *q)
1049 return 0;
1050 p++;
1051 q++;
1053 if (ptr)
1054 *ptr = p;
1055 return 1;
1057 #endif
1059 /* extract the basename of a file */
1060 static char *tcc_basename(const char *name)
1062 char *p = strchr(name, 0);
1063 while (p > name
1064 && p[-1] != '/'
1065 #ifdef _WIN32
1066 && p[-1] != '\\'
1067 #endif
1069 --p;
1070 return p;
1073 static char *tcc_fileextension (const char *name)
1075 char *b = tcc_basename(name);
1076 char *e = strrchr(b, '.');
1077 return e ? e : strchr(b, 0);
1080 #ifdef _WIN32
1081 char *normalize_slashes(char *path)
1083 char *p;
1084 for (p = path; *p; ++p)
1085 if (*p == '\\')
1086 *p = '/';
1087 return path;
1090 char *w32_tcc_lib_path(void)
1092 /* on win32, we suppose the lib and includes are at the location
1093 of 'tcc.exe' */
1094 char path[1024], *p;
1095 GetModuleFileNameA(NULL, path, sizeof path);
1096 p = tcc_basename(normalize_slashes(strlwr(path)));
1097 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
1098 p -= 5;
1099 else if (p > path)
1100 p--;
1101 *p = 0;
1102 return strdup(path);
1104 #endif
1106 void set_pages_executable(void *ptr, unsigned long length)
1108 #ifdef _WIN32
1109 unsigned long old_protect;
1110 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
1111 #else
1112 unsigned long start, end;
1113 start = (unsigned long)ptr & ~(PAGESIZE - 1);
1114 end = (unsigned long)ptr + length;
1115 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
1116 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
1117 #endif
1120 /* memory management */
1121 #ifdef MEM_DEBUG
1122 int mem_cur_size;
1123 int mem_max_size;
1124 unsigned malloc_usable_size(void*);
1125 #endif
1127 static inline void tcc_free(void *ptr)
1129 #ifdef MEM_DEBUG
1130 mem_cur_size -= malloc_usable_size(ptr);
1131 #endif
1132 free(ptr);
1135 static void *tcc_malloc(unsigned long size)
1137 void *ptr;
1138 ptr = malloc(size);
1139 if (!ptr && size)
1140 error("memory full");
1141 #ifdef MEM_DEBUG
1142 mem_cur_size += malloc_usable_size(ptr);
1143 if (mem_cur_size > mem_max_size)
1144 mem_max_size = mem_cur_size;
1145 #endif
1146 return ptr;
1149 static void *tcc_mallocz(unsigned long size)
1151 void *ptr;
1152 ptr = tcc_malloc(size);
1153 memset(ptr, 0, size);
1154 return ptr;
1157 static inline void *tcc_realloc(void *ptr, unsigned long size)
1159 void *ptr1;
1160 #ifdef MEM_DEBUG
1161 mem_cur_size -= malloc_usable_size(ptr);
1162 #endif
1163 ptr1 = realloc(ptr, size);
1164 #ifdef MEM_DEBUG
1165 /* NOTE: count not correct if alloc error, but not critical */
1166 mem_cur_size += malloc_usable_size(ptr1);
1167 if (mem_cur_size > mem_max_size)
1168 mem_max_size = mem_cur_size;
1169 #endif
1170 return ptr1;
1173 static char *tcc_strdup(const char *str)
1175 char *ptr;
1176 ptr = tcc_malloc(strlen(str) + 1);
1177 strcpy(ptr, str);
1178 return ptr;
1181 #define free(p) use_tcc_free(p)
1182 #define malloc(s) use_tcc_malloc(s)
1183 #define realloc(p, s) use_tcc_realloc(p, s)
1185 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1187 int nb, nb_alloc;
1188 void **pp;
1190 nb = *nb_ptr;
1191 pp = *ptab;
1192 /* every power of two we double array size */
1193 if ((nb & (nb - 1)) == 0) {
1194 if (!nb)
1195 nb_alloc = 1;
1196 else
1197 nb_alloc = nb * 2;
1198 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1199 if (!pp)
1200 error("memory full");
1201 *ptab = pp;
1203 pp[nb++] = data;
1204 *nb_ptr = nb;
1207 static void dynarray_reset(void *pp, int *n)
1209 void **p;
1210 for (p = *(void***)pp; *n; ++p, --*n)
1211 if (*p)
1212 tcc_free(*p);
1213 tcc_free(*(void**)pp);
1214 *(void**)pp = NULL;
1217 /* symbol allocator */
1218 static Sym *__sym_malloc(void)
1220 Sym *sym_pool, *sym, *last_sym;
1221 int i;
1223 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1224 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
1226 last_sym = sym_free_first;
1227 sym = sym_pool;
1228 for(i = 0; i < SYM_POOL_NB; i++) {
1229 sym->next = last_sym;
1230 last_sym = sym;
1231 sym++;
1233 sym_free_first = last_sym;
1234 return last_sym;
1237 static inline Sym *sym_malloc(void)
1239 Sym *sym;
1240 sym = sym_free_first;
1241 if (!sym)
1242 sym = __sym_malloc();
1243 sym_free_first = sym->next;
1244 return sym;
1247 static inline void sym_free(Sym *sym)
1249 sym->next = sym_free_first;
1250 sym_free_first = sym;
1253 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1255 Section *sec;
1257 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1258 strcpy(sec->name, name);
1259 sec->sh_type = sh_type;
1260 sec->sh_flags = sh_flags;
1261 switch(sh_type) {
1262 case SHT_HASH:
1263 case SHT_REL:
1264 case SHT_RELA:
1265 case SHT_DYNSYM:
1266 case SHT_SYMTAB:
1267 case SHT_DYNAMIC:
1268 sec->sh_addralign = 4;
1269 break;
1270 case SHT_STRTAB:
1271 sec->sh_addralign = 1;
1272 break;
1273 default:
1274 sec->sh_addralign = 32; /* default conservative alignment */
1275 break;
1278 /* only add section if not private */
1279 if (!(sh_flags & SHF_PRIVATE)) {
1280 sec->sh_num = s1->nb_sections;
1281 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1283 return sec;
1286 static void free_section(Section *s)
1288 if (s->link && (s->link->sh_flags & SHF_PRIVATE))
1289 free_section(s->link);
1290 if (s->hash && (s->hash->sh_flags & SHF_PRIVATE))
1291 s->hash->link = NULL, free_section(s->hash);
1292 tcc_free(s->data);
1293 tcc_free(s);
1296 /* realloc section and set its content to zero */
1297 static void section_realloc(Section *sec, unsigned long new_size)
1299 unsigned long size;
1300 unsigned char *data;
1302 size = sec->data_allocated;
1303 if (size == 0)
1304 size = 1;
1305 while (size < new_size)
1306 size = size * 2;
1307 data = tcc_realloc(sec->data, size);
1308 if (!data)
1309 error("memory full");
1310 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1311 sec->data = data;
1312 sec->data_allocated = size;
1315 /* reserve at least 'size' bytes in section 'sec' from
1316 sec->data_offset. */
1317 static void *section_ptr_add(Section *sec, unsigned long size)
1319 unsigned long offset, offset1;
1321 offset = sec->data_offset;
1322 offset1 = offset + size;
1323 if (offset1 > sec->data_allocated)
1324 section_realloc(sec, offset1);
1325 sec->data_offset = offset1;
1326 return sec->data + offset;
1329 /* return a reference to a section, and create it if it does not
1330 exists */
1331 Section *find_section(TCCState *s1, const char *name)
1333 Section *sec;
1334 int i;
1335 for(i = 1; i < s1->nb_sections; i++) {
1336 sec = s1->sections[i];
1337 if (!strcmp(name, sec->name))
1338 return sec;
1340 /* sections are created as PROGBITS */
1341 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1344 #define SECTION_ABS ((void *)1)
1346 /* update sym->c so that it points to an external symbol in section
1347 'section' with value 'value' */
1348 static void put_extern_sym2(Sym *sym, Section *section,
1349 unsigned long value, unsigned long size,
1350 int can_add_underscore)
1352 int sym_type, sym_bind, sh_num, info, other, attr;
1353 ElfW(Sym) *esym;
1354 const char *name;
1355 char buf1[256];
1357 if (section == NULL)
1358 sh_num = SHN_UNDEF;
1359 else if (section == SECTION_ABS)
1360 sh_num = SHN_ABS;
1361 else
1362 sh_num = section->sh_num;
1364 other = attr = 0;
1366 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1367 sym_type = STT_FUNC;
1368 #ifdef TCC_TARGET_PE
1369 if (sym->type.ref)
1370 attr = sym->type.ref->r;
1371 if (FUNC_EXPORT(attr))
1372 other |= 1;
1373 if (FUNC_CALL(attr) == FUNC_STDCALL)
1374 other |= 2;
1375 #endif
1376 } else {
1377 sym_type = STT_OBJECT;
1380 if (sym->type.t & VT_STATIC)
1381 sym_bind = STB_LOCAL;
1382 else
1383 sym_bind = STB_GLOBAL;
1385 if (!sym->c) {
1386 name = get_tok_str(sym->v, NULL);
1387 #ifdef CONFIG_TCC_BCHECK
1388 if (do_bounds_check) {
1389 char buf[32];
1391 /* XXX: avoid doing that for statics ? */
1392 /* if bound checking is activated, we change some function
1393 names by adding the "__bound" prefix */
1394 switch(sym->v) {
1395 #if 0
1396 /* XXX: we rely only on malloc hooks */
1397 case TOK_malloc:
1398 case TOK_free:
1399 case TOK_realloc:
1400 case TOK_memalign:
1401 case TOK_calloc:
1402 #endif
1403 case TOK_memcpy:
1404 case TOK_memmove:
1405 case TOK_memset:
1406 case TOK_strlen:
1407 case TOK_strcpy:
1408 case TOK__alloca:
1409 strcpy(buf, "__bound_");
1410 strcat(buf, name);
1411 name = buf;
1412 break;
1415 #endif
1417 #ifdef TCC_TARGET_PE
1418 if ((other & 2) && can_add_underscore) {
1419 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1420 name = buf1;
1421 } else
1422 #endif
1423 if (tcc_state->leading_underscore && can_add_underscore) {
1424 buf1[0] = '_';
1425 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1426 name = buf1;
1428 info = ELFW(ST_INFO)(sym_bind, sym_type);
1429 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1430 } else {
1431 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
1432 esym->st_value = value;
1433 esym->st_size = size;
1434 esym->st_shndx = sh_num;
1435 esym->st_other |= other;
1439 static void put_extern_sym(Sym *sym, Section *section,
1440 unsigned long value, unsigned long size)
1442 put_extern_sym2(sym, section, value, size, 1);
1445 /* add a new relocation entry to symbol 'sym' in section 's' */
1446 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1448 if (!sym->c)
1449 put_extern_sym(sym, NULL, 0, 0);
1450 /* now we can add ELF relocation info */
1451 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1454 static inline int isid(int c)
1456 return (c >= 'a' && c <= 'z') ||
1457 (c >= 'A' && c <= 'Z') ||
1458 c == '_';
1461 static inline int isnum(int c)
1463 return c >= '0' && c <= '9';
1466 static inline int isoct(int c)
1468 return c >= '0' && c <= '7';
1471 static inline int toup(int c)
1473 if (c >= 'a' && c <= 'z')
1474 return c - 'a' + 'A';
1475 else
1476 return c;
1479 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1481 int len;
1482 len = strlen(buf);
1483 vsnprintf(buf + len, buf_size - len, fmt, ap);
1486 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1488 va_list ap;
1489 va_start(ap, fmt);
1490 strcat_vprintf(buf, buf_size, fmt, ap);
1491 va_end(ap);
1494 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1496 char buf[2048];
1497 BufferedFile **f;
1499 buf[0] = '\0';
1500 if (file) {
1501 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1502 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1503 (*f)->filename, (*f)->line_num);
1504 if (file->line_num > 0) {
1505 strcat_printf(buf, sizeof(buf),
1506 "%s:%d: ", file->filename, file->line_num);
1507 } else {
1508 strcat_printf(buf, sizeof(buf),
1509 "%s: ", file->filename);
1511 } else {
1512 strcat_printf(buf, sizeof(buf),
1513 "tcc: ");
1515 if (is_warning)
1516 strcat_printf(buf, sizeof(buf), "warning: ");
1517 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1519 if (!s1->error_func) {
1520 /* default case: stderr */
1521 fprintf(stderr, "%s\n", buf);
1522 } else {
1523 s1->error_func(s1->error_opaque, buf);
1525 if (!is_warning || s1->warn_error)
1526 s1->nb_errors++;
1529 #ifdef LIBTCC
1530 void tcc_set_error_func(TCCState *s, void *error_opaque,
1531 void (*error_func)(void *opaque, const char *msg))
1533 s->error_opaque = error_opaque;
1534 s->error_func = error_func;
1536 #endif
1538 /* error without aborting current compilation */
1539 void error_noabort(const char *fmt, ...)
1541 TCCState *s1 = tcc_state;
1542 va_list ap;
1544 va_start(ap, fmt);
1545 error1(s1, 0, fmt, ap);
1546 va_end(ap);
1549 void error(const char *fmt, ...)
1551 TCCState *s1 = tcc_state;
1552 va_list ap;
1554 va_start(ap, fmt);
1555 error1(s1, 0, fmt, ap);
1556 va_end(ap);
1557 /* better than nothing: in some cases, we accept to handle errors */
1558 if (s1->error_set_jmp_enabled) {
1559 longjmp(s1->error_jmp_buf, 1);
1560 } else {
1561 /* XXX: eliminate this someday */
1562 exit(1);
1566 void expect(const char *msg)
1568 error("%s expected", msg);
1571 void warning(const char *fmt, ...)
1573 TCCState *s1 = tcc_state;
1574 va_list ap;
1576 if (s1->warn_none)
1577 return;
1579 va_start(ap, fmt);
1580 error1(s1, 1, fmt, ap);
1581 va_end(ap);
1584 void skip(int c)
1586 if (tok != c)
1587 error("'%c' expected", c);
1588 next();
1591 static void test_lvalue(void)
1593 if (!(vtop->r & VT_LVAL))
1594 expect("lvalue");
1597 /* allocate a new token */
1598 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1600 TokenSym *ts, **ptable;
1601 int i;
1603 if (tok_ident >= SYM_FIRST_ANOM)
1604 error("memory full");
1606 /* expand token table if needed */
1607 i = tok_ident - TOK_IDENT;
1608 if ((i % TOK_ALLOC_INCR) == 0) {
1609 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1610 if (!ptable)
1611 error("memory full");
1612 table_ident = ptable;
1615 ts = tcc_malloc(sizeof(TokenSym) + len);
1616 table_ident[i] = ts;
1617 ts->tok = tok_ident++;
1618 ts->sym_define = NULL;
1619 ts->sym_label = NULL;
1620 ts->sym_struct = NULL;
1621 ts->sym_identifier = NULL;
1622 ts->len = len;
1623 ts->hash_next = NULL;
1624 memcpy(ts->str, str, len);
1625 ts->str[len] = '\0';
1626 *pts = ts;
1627 return ts;
1630 #define TOK_HASH_INIT 1
1631 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1633 /* find a token and add it if not found */
1634 static TokenSym *tok_alloc(const char *str, int len)
1636 TokenSym *ts, **pts;
1637 int i;
1638 unsigned int h;
1640 h = TOK_HASH_INIT;
1641 for(i=0;i<len;i++)
1642 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1643 h &= (TOK_HASH_SIZE - 1);
1645 pts = &hash_ident[h];
1646 for(;;) {
1647 ts = *pts;
1648 if (!ts)
1649 break;
1650 if (ts->len == len && !memcmp(ts->str, str, len))
1651 return ts;
1652 pts = &(ts->hash_next);
1654 return tok_alloc_new(pts, str, len);
1657 /* CString handling */
1659 static void cstr_realloc(CString *cstr, int new_size)
1661 int size;
1662 void *data;
1664 size = cstr->size_allocated;
1665 if (size == 0)
1666 size = 8; /* no need to allocate a too small first string */
1667 while (size < new_size)
1668 size = size * 2;
1669 data = tcc_realloc(cstr->data_allocated, size);
1670 if (!data)
1671 error("memory full");
1672 cstr->data_allocated = data;
1673 cstr->size_allocated = size;
1674 cstr->data = data;
1677 /* add a byte */
1678 static inline void cstr_ccat(CString *cstr, int ch)
1680 int size;
1681 size = cstr->size + 1;
1682 if (size > cstr->size_allocated)
1683 cstr_realloc(cstr, size);
1684 ((unsigned char *)cstr->data)[size - 1] = ch;
1685 cstr->size = size;
1688 static void cstr_cat(CString *cstr, const char *str)
1690 int c;
1691 for(;;) {
1692 c = *str;
1693 if (c == '\0')
1694 break;
1695 cstr_ccat(cstr, c);
1696 str++;
1700 /* add a wide char */
1701 static void cstr_wccat(CString *cstr, int ch)
1703 int size;
1704 size = cstr->size + sizeof(nwchar_t);
1705 if (size > cstr->size_allocated)
1706 cstr_realloc(cstr, size);
1707 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
1708 cstr->size = size;
1711 static void cstr_new(CString *cstr)
1713 memset(cstr, 0, sizeof(CString));
1716 /* free string and reset it to NULL */
1717 static void cstr_free(CString *cstr)
1719 tcc_free(cstr->data_allocated);
1720 cstr_new(cstr);
1723 #define cstr_reset(cstr) cstr_free(cstr)
1725 /* XXX: unicode ? */
1726 static void add_char(CString *cstr, int c)
1728 if (c == '\'' || c == '\"' || c == '\\') {
1729 /* XXX: could be more precise if char or string */
1730 cstr_ccat(cstr, '\\');
1732 if (c >= 32 && c <= 126) {
1733 cstr_ccat(cstr, c);
1734 } else {
1735 cstr_ccat(cstr, '\\');
1736 if (c == '\n') {
1737 cstr_ccat(cstr, 'n');
1738 } else {
1739 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1740 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1741 cstr_ccat(cstr, '0' + (c & 7));
1746 /* XXX: buffer overflow */
1747 /* XXX: float tokens */
1748 char *get_tok_str(int v, CValue *cv)
1750 static char buf[STRING_MAX_SIZE + 1];
1751 static CString cstr_buf;
1752 CString *cstr;
1753 unsigned char *q;
1754 char *p;
1755 int i, len;
1757 /* NOTE: to go faster, we give a fixed buffer for small strings */
1758 cstr_reset(&cstr_buf);
1759 cstr_buf.data = buf;
1760 cstr_buf.size_allocated = sizeof(buf);
1761 p = buf;
1763 switch(v) {
1764 case TOK_CINT:
1765 case TOK_CUINT:
1766 /* XXX: not quite exact, but only useful for testing */
1767 sprintf(p, "%u", cv->ui);
1768 break;
1769 case TOK_CLLONG:
1770 case TOK_CULLONG:
1771 /* XXX: not quite exact, but only useful for testing */
1772 sprintf(p, "%Lu", cv->ull);
1773 break;
1774 case TOK_LCHAR:
1775 cstr_ccat(&cstr_buf, 'L');
1776 case TOK_CCHAR:
1777 cstr_ccat(&cstr_buf, '\'');
1778 add_char(&cstr_buf, cv->i);
1779 cstr_ccat(&cstr_buf, '\'');
1780 cstr_ccat(&cstr_buf, '\0');
1781 break;
1782 case TOK_PPNUM:
1783 cstr = cv->cstr;
1784 len = cstr->size - 1;
1785 for(i=0;i<len;i++)
1786 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1787 cstr_ccat(&cstr_buf, '\0');
1788 break;
1789 case TOK_LSTR:
1790 cstr_ccat(&cstr_buf, 'L');
1791 case TOK_STR:
1792 cstr = cv->cstr;
1793 cstr_ccat(&cstr_buf, '\"');
1794 if (v == TOK_STR) {
1795 len = cstr->size - 1;
1796 for(i=0;i<len;i++)
1797 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1798 } else {
1799 len = (cstr->size / sizeof(nwchar_t)) - 1;
1800 for(i=0;i<len;i++)
1801 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1803 cstr_ccat(&cstr_buf, '\"');
1804 cstr_ccat(&cstr_buf, '\0');
1805 break;
1806 case TOK_LT:
1807 v = '<';
1808 goto addv;
1809 case TOK_GT:
1810 v = '>';
1811 goto addv;
1812 case TOK_DOTS:
1813 return strcpy(p, "...");
1814 case TOK_A_SHL:
1815 return strcpy(p, "<<=");
1816 case TOK_A_SAR:
1817 return strcpy(p, ">>=");
1818 default:
1819 if (v < TOK_IDENT) {
1820 /* search in two bytes table */
1821 q = tok_two_chars;
1822 while (*q) {
1823 if (q[2] == v) {
1824 *p++ = q[0];
1825 *p++ = q[1];
1826 *p = '\0';
1827 return buf;
1829 q += 3;
1831 addv:
1832 *p++ = v;
1833 *p = '\0';
1834 } else if (v < tok_ident) {
1835 return table_ident[v - TOK_IDENT]->str;
1836 } else if (v >= SYM_FIRST_ANOM) {
1837 /* special name for anonymous symbol */
1838 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1839 } else {
1840 /* should never happen */
1841 return NULL;
1843 break;
1845 return cstr_buf.data;
1848 /* push, without hashing */
1849 static Sym *sym_push2(Sym **ps, int v, int t, long c)
1851 Sym *s;
1852 s = sym_malloc();
1853 s->v = v;
1854 s->type.t = t;
1855 s->c = c;
1856 s->next = NULL;
1857 /* add in stack */
1858 s->prev = *ps;
1859 *ps = s;
1860 return s;
1863 /* find a symbol and return its associated structure. 's' is the top
1864 of the symbol stack */
1865 static Sym *sym_find2(Sym *s, int v)
1867 while (s) {
1868 if (s->v == v)
1869 return s;
1870 s = s->prev;
1872 return NULL;
1875 /* structure lookup */
1876 static inline Sym *struct_find(int v)
1878 v -= TOK_IDENT;
1879 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1880 return NULL;
1881 return table_ident[v]->sym_struct;
1884 /* find an identifier */
1885 static inline Sym *sym_find(int v)
1887 v -= TOK_IDENT;
1888 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1889 return NULL;
1890 return table_ident[v]->sym_identifier;
1893 /* push a given symbol on the symbol stack */
1894 static Sym *sym_push(int v, CType *type, int r, int c)
1896 Sym *s, **ps;
1897 TokenSym *ts;
1899 if (local_stack)
1900 ps = &local_stack;
1901 else
1902 ps = &global_stack;
1903 s = sym_push2(ps, v, type->t, c);
1904 s->type.ref = type->ref;
1905 s->r = r;
1906 /* don't record fields or anonymous symbols */
1907 /* XXX: simplify */
1908 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1909 /* record symbol in token array */
1910 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1911 if (v & SYM_STRUCT)
1912 ps = &ts->sym_struct;
1913 else
1914 ps = &ts->sym_identifier;
1915 s->prev_tok = *ps;
1916 *ps = s;
1918 return s;
1921 /* push a global identifier */
1922 static Sym *global_identifier_push(int v, int t, int c)
1924 Sym *s, **ps;
1925 s = sym_push2(&global_stack, v, t, c);
1926 /* don't record anonymous symbol */
1927 if (v < SYM_FIRST_ANOM) {
1928 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1929 /* modify the top most local identifier, so that
1930 sym_identifier will point to 's' when popped */
1931 while (*ps != NULL)
1932 ps = &(*ps)->prev_tok;
1933 s->prev_tok = NULL;
1934 *ps = s;
1936 return s;
1939 /* pop symbols until top reaches 'b' */
1940 static void sym_pop(Sym **ptop, Sym *b)
1942 Sym *s, *ss, **ps;
1943 TokenSym *ts;
1944 int v;
1946 s = *ptop;
1947 while(s != b) {
1948 ss = s->prev;
1949 v = s->v;
1950 /* remove symbol in token array */
1951 /* XXX: simplify */
1952 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1953 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1954 if (v & SYM_STRUCT)
1955 ps = &ts->sym_struct;
1956 else
1957 ps = &ts->sym_identifier;
1958 *ps = s->prev_tok;
1960 sym_free(s);
1961 s = ss;
1963 *ptop = b;
1966 /* I/O layer */
1968 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1970 int fd;
1971 BufferedFile *bf;
1973 if (strcmp(filename, "-") == 0)
1974 fd = 0, filename = "stdin";
1975 else
1976 fd = open(filename, O_RDONLY | O_BINARY);
1977 if ((verbose == 2 && fd >= 0) || verbose == 3)
1978 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
1979 (s1->include_stack_ptr - s1->include_stack), "", filename);
1980 if (fd < 0)
1981 return NULL;
1982 bf = tcc_malloc(sizeof(BufferedFile));
1983 bf->fd = fd;
1984 bf->buf_ptr = bf->buffer;
1985 bf->buf_end = bf->buffer;
1986 bf->buffer[0] = CH_EOB; /* put eob symbol */
1987 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1988 #ifdef _WIN32
1989 normalize_slashes(bf->filename);
1990 #endif
1991 bf->line_num = 1;
1992 bf->ifndef_macro = 0;
1993 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1994 // printf("opening '%s'\n", filename);
1995 return bf;
1998 void tcc_close(BufferedFile *bf)
2000 total_lines += bf->line_num;
2001 close(bf->fd);
2002 tcc_free(bf);
2005 /* fill input buffer and peek next char */
2006 static int tcc_peekc_slow(BufferedFile *bf)
2008 int len;
2009 /* only tries to read if really end of buffer */
2010 if (bf->buf_ptr >= bf->buf_end) {
2011 if (bf->fd != -1) {
2012 #if defined(PARSE_DEBUG)
2013 len = 8;
2014 #else
2015 len = IO_BUF_SIZE;
2016 #endif
2017 len = read(bf->fd, bf->buffer, len);
2018 if (len < 0)
2019 len = 0;
2020 } else {
2021 len = 0;
2023 total_bytes += len;
2024 bf->buf_ptr = bf->buffer;
2025 bf->buf_end = bf->buffer + len;
2026 *bf->buf_end = CH_EOB;
2028 if (bf->buf_ptr < bf->buf_end) {
2029 return bf->buf_ptr[0];
2030 } else {
2031 bf->buf_ptr = bf->buf_end;
2032 return CH_EOF;
2036 /* return the current character, handling end of block if necessary
2037 (but not stray) */
2038 static int handle_eob(void)
2040 return tcc_peekc_slow(file);
2043 /* read next char from current input file and handle end of input buffer */
2044 static inline void inp(void)
2046 ch = *(++(file->buf_ptr));
2047 /* end of buffer/file handling */
2048 if (ch == CH_EOB)
2049 ch = handle_eob();
2052 /* handle '\[\r]\n' */
2053 static int handle_stray_noerror(void)
2055 while (ch == '\\') {
2056 inp();
2057 if (ch == '\n') {
2058 file->line_num++;
2059 inp();
2060 } else if (ch == '\r') {
2061 inp();
2062 if (ch != '\n')
2063 goto fail;
2064 file->line_num++;
2065 inp();
2066 } else {
2067 fail:
2068 return 1;
2071 return 0;
2074 static void handle_stray(void)
2076 if (handle_stray_noerror())
2077 error("stray '\\' in program");
2080 /* skip the stray and handle the \\n case. Output an error if
2081 incorrect char after the stray */
2082 static int handle_stray1(uint8_t *p)
2084 int c;
2086 if (p >= file->buf_end) {
2087 file->buf_ptr = p;
2088 c = handle_eob();
2089 p = file->buf_ptr;
2090 if (c == '\\')
2091 goto parse_stray;
2092 } else {
2093 parse_stray:
2094 file->buf_ptr = p;
2095 ch = *p;
2096 handle_stray();
2097 p = file->buf_ptr;
2098 c = *p;
2100 return c;
2103 /* handle just the EOB case, but not stray */
2104 #define PEEKC_EOB(c, p)\
2106 p++;\
2107 c = *p;\
2108 if (c == '\\') {\
2109 file->buf_ptr = p;\
2110 c = handle_eob();\
2111 p = file->buf_ptr;\
2115 /* handle the complicated stray case */
2116 #define PEEKC(c, p)\
2118 p++;\
2119 c = *p;\
2120 if (c == '\\') {\
2121 c = handle_stray1(p);\
2122 p = file->buf_ptr;\
2126 /* input with '\[\r]\n' handling. Note that this function cannot
2127 handle other characters after '\', so you cannot call it inside
2128 strings or comments */
2129 static void minp(void)
2131 inp();
2132 if (ch == '\\')
2133 handle_stray();
2137 /* single line C++ comments */
2138 static uint8_t *parse_line_comment(uint8_t *p)
2140 int c;
2142 p++;
2143 for(;;) {
2144 c = *p;
2145 redo:
2146 if (c == '\n' || c == CH_EOF) {
2147 break;
2148 } else if (c == '\\') {
2149 file->buf_ptr = p;
2150 c = handle_eob();
2151 p = file->buf_ptr;
2152 if (c == '\\') {
2153 PEEKC_EOB(c, p);
2154 if (c == '\n') {
2155 file->line_num++;
2156 PEEKC_EOB(c, p);
2157 } else if (c == '\r') {
2158 PEEKC_EOB(c, p);
2159 if (c == '\n') {
2160 file->line_num++;
2161 PEEKC_EOB(c, p);
2164 } else {
2165 goto redo;
2167 } else {
2168 p++;
2171 return p;
2174 /* C comments */
2175 static uint8_t *parse_comment(uint8_t *p)
2177 int c;
2179 p++;
2180 for(;;) {
2181 /* fast skip loop */
2182 for(;;) {
2183 c = *p;
2184 if (c == '\n' || c == '*' || c == '\\')
2185 break;
2186 p++;
2187 c = *p;
2188 if (c == '\n' || c == '*' || c == '\\')
2189 break;
2190 p++;
2192 /* now we can handle all the cases */
2193 if (c == '\n') {
2194 file->line_num++;
2195 p++;
2196 } else if (c == '*') {
2197 p++;
2198 for(;;) {
2199 c = *p;
2200 if (c == '*') {
2201 p++;
2202 } else if (c == '/') {
2203 goto end_of_comment;
2204 } else if (c == '\\') {
2205 file->buf_ptr = p;
2206 c = handle_eob();
2207 p = file->buf_ptr;
2208 if (c == '\\') {
2209 /* skip '\[\r]\n', otherwise just skip the stray */
2210 while (c == '\\') {
2211 PEEKC_EOB(c, p);
2212 if (c == '\n') {
2213 file->line_num++;
2214 PEEKC_EOB(c, p);
2215 } else if (c == '\r') {
2216 PEEKC_EOB(c, p);
2217 if (c == '\n') {
2218 file->line_num++;
2219 PEEKC_EOB(c, p);
2221 } else {
2222 goto after_star;
2226 } else {
2227 break;
2230 after_star: ;
2231 } else {
2232 /* stray, eob or eof */
2233 file->buf_ptr = p;
2234 c = handle_eob();
2235 p = file->buf_ptr;
2236 if (c == CH_EOF) {
2237 error("unexpected end of file in comment");
2238 } else if (c == '\\') {
2239 p++;
2243 end_of_comment:
2244 p++;
2245 return p;
2248 #define cinp minp
2250 /* space exlcuding newline */
2251 static inline int is_space(int ch)
2253 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2256 static inline void skip_spaces(void)
2258 while (is_space(ch))
2259 cinp();
2262 /* parse a string without interpreting escapes */
2263 static uint8_t *parse_pp_string(uint8_t *p,
2264 int sep, CString *str)
2266 int c;
2267 p++;
2268 for(;;) {
2269 c = *p;
2270 if (c == sep) {
2271 break;
2272 } else if (c == '\\') {
2273 file->buf_ptr = p;
2274 c = handle_eob();
2275 p = file->buf_ptr;
2276 if (c == CH_EOF) {
2277 unterminated_string:
2278 /* XXX: indicate line number of start of string */
2279 error("missing terminating %c character", sep);
2280 } else if (c == '\\') {
2281 /* escape : just skip \[\r]\n */
2282 PEEKC_EOB(c, p);
2283 if (c == '\n') {
2284 file->line_num++;
2285 p++;
2286 } else if (c == '\r') {
2287 PEEKC_EOB(c, p);
2288 if (c != '\n')
2289 expect("'\n' after '\r'");
2290 file->line_num++;
2291 p++;
2292 } else if (c == CH_EOF) {
2293 goto unterminated_string;
2294 } else {
2295 if (str) {
2296 cstr_ccat(str, '\\');
2297 cstr_ccat(str, c);
2299 p++;
2302 } else if (c == '\n') {
2303 file->line_num++;
2304 goto add_char;
2305 } else if (c == '\r') {
2306 PEEKC_EOB(c, p);
2307 if (c != '\n') {
2308 if (str)
2309 cstr_ccat(str, '\r');
2310 } else {
2311 file->line_num++;
2312 goto add_char;
2314 } else {
2315 add_char:
2316 if (str)
2317 cstr_ccat(str, c);
2318 p++;
2321 p++;
2322 return p;
2325 /* skip block of text until #else, #elif or #endif. skip also pairs of
2326 #if/#endif */
2327 void preprocess_skip(void)
2329 int a, start_of_line, c, in_warn_or_error;
2330 uint8_t *p;
2332 p = file->buf_ptr;
2333 a = 0;
2334 redo_start:
2335 start_of_line = 1;
2336 in_warn_or_error = 0;
2337 for(;;) {
2338 redo_no_start:
2339 c = *p;
2340 switch(c) {
2341 case ' ':
2342 case '\t':
2343 case '\f':
2344 case '\v':
2345 case '\r':
2346 p++;
2347 goto redo_no_start;
2348 case '\n':
2349 file->line_num++;
2350 p++;
2351 goto redo_start;
2352 case '\\':
2353 file->buf_ptr = p;
2354 c = handle_eob();
2355 if (c == CH_EOF) {
2356 expect("#endif");
2357 } else if (c == '\\') {
2358 ch = file->buf_ptr[0];
2359 handle_stray_noerror();
2361 p = file->buf_ptr;
2362 goto redo_no_start;
2363 /* skip strings */
2364 case '\"':
2365 case '\'':
2366 if (in_warn_or_error)
2367 goto _default;
2368 p = parse_pp_string(p, c, NULL);
2369 break;
2370 /* skip comments */
2371 case '/':
2372 if (in_warn_or_error)
2373 goto _default;
2374 file->buf_ptr = p;
2375 ch = *p;
2376 minp();
2377 p = file->buf_ptr;
2378 if (ch == '*') {
2379 p = parse_comment(p);
2380 } else if (ch == '/') {
2381 p = parse_line_comment(p);
2383 break;
2384 case '#':
2385 p++;
2386 if (start_of_line) {
2387 file->buf_ptr = p;
2388 next_nomacro();
2389 p = file->buf_ptr;
2390 if (a == 0 &&
2391 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2392 goto the_end;
2393 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2394 a++;
2395 else if (tok == TOK_ENDIF)
2396 a--;
2397 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2398 in_warn_or_error = 1;
2400 break;
2401 _default:
2402 default:
2403 p++;
2404 break;
2406 start_of_line = 0;
2408 the_end: ;
2409 file->buf_ptr = p;
2412 /* ParseState handling */
2414 /* XXX: currently, no include file info is stored. Thus, we cannot display
2415 accurate messages if the function or data definition spans multiple
2416 files */
2418 /* save current parse state in 's' */
2419 void save_parse_state(ParseState *s)
2421 s->line_num = file->line_num;
2422 s->macro_ptr = macro_ptr;
2423 s->tok = tok;
2424 s->tokc = tokc;
2427 /* restore parse state from 's' */
2428 void restore_parse_state(ParseState *s)
2430 file->line_num = s->line_num;
2431 macro_ptr = s->macro_ptr;
2432 tok = s->tok;
2433 tokc = s->tokc;
2436 /* return the number of additional 'ints' necessary to store the
2437 token */
2438 static inline int tok_ext_size(int t)
2440 switch(t) {
2441 /* 4 bytes */
2442 case TOK_CINT:
2443 case TOK_CUINT:
2444 case TOK_CCHAR:
2445 case TOK_LCHAR:
2446 case TOK_CFLOAT:
2447 case TOK_LINENUM:
2448 return 1;
2449 case TOK_STR:
2450 case TOK_LSTR:
2451 case TOK_PPNUM:
2452 error("unsupported token");
2453 return 1;
2454 case TOK_CDOUBLE:
2455 case TOK_CLLONG:
2456 case TOK_CULLONG:
2457 return 2;
2458 case TOK_CLDOUBLE:
2459 return LDOUBLE_SIZE / 4;
2460 default:
2461 return 0;
2465 /* token string handling */
2467 static inline void tok_str_new(TokenString *s)
2469 s->str = NULL;
2470 s->len = 0;
2471 s->allocated_len = 0;
2472 s->last_line_num = -1;
2475 static void tok_str_free(int *str)
2477 tcc_free(str);
2480 static int *tok_str_realloc(TokenString *s)
2482 int *str, len;
2484 if (s->allocated_len == 0) {
2485 len = 8;
2486 } else {
2487 len = s->allocated_len * 2;
2489 str = tcc_realloc(s->str, len * sizeof(int));
2490 if (!str)
2491 error("memory full");
2492 s->allocated_len = len;
2493 s->str = str;
2494 return str;
2497 static void tok_str_add(TokenString *s, int t)
2499 int len, *str;
2501 len = s->len;
2502 str = s->str;
2503 if (len >= s->allocated_len)
2504 str = tok_str_realloc(s);
2505 str[len++] = t;
2506 s->len = len;
2509 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2511 int len, *str;
2513 len = s->len;
2514 str = s->str;
2516 /* allocate space for worst case */
2517 if (len + TOK_MAX_SIZE > s->allocated_len)
2518 str = tok_str_realloc(s);
2519 str[len++] = t;
2520 switch(t) {
2521 case TOK_CINT:
2522 case TOK_CUINT:
2523 case TOK_CCHAR:
2524 case TOK_LCHAR:
2525 case TOK_CFLOAT:
2526 case TOK_LINENUM:
2527 str[len++] = cv->tab[0];
2528 break;
2529 case TOK_PPNUM:
2530 case TOK_STR:
2531 case TOK_LSTR:
2533 int nb_words;
2534 CString *cstr;
2536 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2537 while ((len + nb_words) > s->allocated_len)
2538 str = tok_str_realloc(s);
2539 cstr = (CString *)(str + len);
2540 cstr->data = NULL;
2541 cstr->size = cv->cstr->size;
2542 cstr->data_allocated = NULL;
2543 cstr->size_allocated = cstr->size;
2544 memcpy((char *)cstr + sizeof(CString),
2545 cv->cstr->data, cstr->size);
2546 len += nb_words;
2548 break;
2549 case TOK_CDOUBLE:
2550 case TOK_CLLONG:
2551 case TOK_CULLONG:
2552 #if LDOUBLE_SIZE == 8
2553 case TOK_CLDOUBLE:
2554 #endif
2555 str[len++] = cv->tab[0];
2556 str[len++] = cv->tab[1];
2557 break;
2558 #if LDOUBLE_SIZE == 12
2559 case TOK_CLDOUBLE:
2560 str[len++] = cv->tab[0];
2561 str[len++] = cv->tab[1];
2562 str[len++] = cv->tab[2];
2563 #elif LDOUBLE_SIZE == 16
2564 case TOK_CLDOUBLE:
2565 str[len++] = cv->tab[0];
2566 str[len++] = cv->tab[1];
2567 str[len++] = cv->tab[2];
2568 str[len++] = cv->tab[3];
2569 #elif LDOUBLE_SIZE != 8
2570 #error add long double size support
2571 #endif
2572 break;
2573 default:
2574 break;
2576 s->len = len;
2579 /* add the current parse token in token string 's' */
2580 static void tok_str_add_tok(TokenString *s)
2582 CValue cval;
2584 /* save line number info */
2585 if (file->line_num != s->last_line_num) {
2586 s->last_line_num = file->line_num;
2587 cval.i = s->last_line_num;
2588 tok_str_add2(s, TOK_LINENUM, &cval);
2590 tok_str_add2(s, tok, &tokc);
2593 #if LDOUBLE_SIZE == 16
2594 #define LDOUBLE_GET(p, cv) \
2595 cv.tab[0] = p[0]; \
2596 cv.tab[1] = p[1]; \
2597 cv.tab[2] = p[2]; \
2598 cv.tab[3] = p[3];
2599 #elif LDOUBLE_SIZE == 12
2600 #define LDOUBLE_GET(p, cv) \
2601 cv.tab[0] = p[0]; \
2602 cv.tab[1] = p[1]; \
2603 cv.tab[2] = p[2];
2604 #elif LDOUBLE_SIZE == 8
2605 #define LDOUBLE_GET(p, cv) \
2606 cv.tab[0] = p[0]; \
2607 cv.tab[1] = p[1];
2608 #else
2609 #error add long double size support
2610 #endif
2613 /* get a token from an integer array and increment pointer
2614 accordingly. we code it as a macro to avoid pointer aliasing. */
2615 #define TOK_GET(t, p, cv) \
2617 t = *p++; \
2618 switch(t) { \
2619 case TOK_CINT: \
2620 case TOK_CUINT: \
2621 case TOK_CCHAR: \
2622 case TOK_LCHAR: \
2623 case TOK_CFLOAT: \
2624 case TOK_LINENUM: \
2625 cv.tab[0] = *p++; \
2626 break; \
2627 case TOK_STR: \
2628 case TOK_LSTR: \
2629 case TOK_PPNUM: \
2630 cv.cstr = (CString *)p; \
2631 cv.cstr->data = (char *)p + sizeof(CString);\
2632 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2633 break; \
2634 case TOK_CDOUBLE: \
2635 case TOK_CLLONG: \
2636 case TOK_CULLONG: \
2637 cv.tab[0] = p[0]; \
2638 cv.tab[1] = p[1]; \
2639 p += 2; \
2640 break; \
2641 case TOK_CLDOUBLE: \
2642 LDOUBLE_GET(p, cv); \
2643 p += LDOUBLE_SIZE / 4; \
2644 break; \
2645 default: \
2646 break; \
2650 /* defines handling */
2651 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2653 Sym *s;
2655 s = sym_push2(&define_stack, v, macro_type, (long)str);
2656 s->next = first_arg;
2657 table_ident[v - TOK_IDENT]->sym_define = s;
2660 /* undefined a define symbol. Its name is just set to zero */
2661 static void define_undef(Sym *s)
2663 int v;
2664 v = s->v;
2665 if (v >= TOK_IDENT && v < tok_ident)
2666 table_ident[v - TOK_IDENT]->sym_define = NULL;
2667 s->v = 0;
2670 static inline Sym *define_find(int v)
2672 v -= TOK_IDENT;
2673 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2674 return NULL;
2675 return table_ident[v]->sym_define;
2678 /* free define stack until top reaches 'b' */
2679 static void free_defines(Sym *b)
2681 Sym *top, *top1;
2682 int v;
2684 top = define_stack;
2685 while (top != b) {
2686 top1 = top->prev;
2687 /* do not free args or predefined defines */
2688 if (top->c)
2689 tok_str_free((int *)top->c);
2690 v = top->v;
2691 if (v >= TOK_IDENT && v < tok_ident)
2692 table_ident[v - TOK_IDENT]->sym_define = NULL;
2693 sym_free(top);
2694 top = top1;
2696 define_stack = b;
2699 /* label lookup */
2700 static Sym *label_find(int v)
2702 v -= TOK_IDENT;
2703 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2704 return NULL;
2705 return table_ident[v]->sym_label;
2708 static Sym *label_push(Sym **ptop, int v, int flags)
2710 Sym *s, **ps;
2711 s = sym_push2(ptop, v, 0, 0);
2712 s->r = flags;
2713 ps = &table_ident[v - TOK_IDENT]->sym_label;
2714 if (ptop == &global_label_stack) {
2715 /* modify the top most local identifier, so that
2716 sym_identifier will point to 's' when popped */
2717 while (*ps != NULL)
2718 ps = &(*ps)->prev_tok;
2720 s->prev_tok = *ps;
2721 *ps = s;
2722 return s;
2725 /* pop labels until element last is reached. Look if any labels are
2726 undefined. Define symbols if '&&label' was used. */
2727 static void label_pop(Sym **ptop, Sym *slast)
2729 Sym *s, *s1;
2730 for(s = *ptop; s != slast; s = s1) {
2731 s1 = s->prev;
2732 if (s->r == LABEL_DECLARED) {
2733 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2734 } else if (s->r == LABEL_FORWARD) {
2735 error("label '%s' used but not defined",
2736 get_tok_str(s->v, NULL));
2737 } else {
2738 if (s->c) {
2739 /* define corresponding symbol. A size of
2740 1 is put. */
2741 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2744 /* remove label */
2745 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2746 sym_free(s);
2748 *ptop = slast;
2751 /* eval an expression for #if/#elif */
2752 static int expr_preprocess(void)
2754 int c, t;
2755 TokenString str;
2757 tok_str_new(&str);
2758 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2759 next(); /* do macro subst */
2760 if (tok == TOK_DEFINED) {
2761 next_nomacro();
2762 t = tok;
2763 if (t == '(')
2764 next_nomacro();
2765 c = define_find(tok) != 0;
2766 if (t == '(')
2767 next_nomacro();
2768 tok = TOK_CINT;
2769 tokc.i = c;
2770 } else if (tok >= TOK_IDENT) {
2771 /* if undefined macro */
2772 tok = TOK_CINT;
2773 tokc.i = 0;
2775 tok_str_add_tok(&str);
2777 tok_str_add(&str, -1); /* simulate end of file */
2778 tok_str_add(&str, 0);
2779 /* now evaluate C constant expression */
2780 macro_ptr = str.str;
2781 next();
2782 c = expr_const();
2783 macro_ptr = NULL;
2784 tok_str_free(str.str);
2785 return c != 0;
2788 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2789 static void tok_print(int *str)
2791 int t;
2792 CValue cval;
2794 while (1) {
2795 TOK_GET(t, str, cval);
2796 if (!t)
2797 break;
2798 printf(" %s", get_tok_str(t, &cval));
2800 printf("\n");
2802 #endif
2804 /* parse after #define */
2805 static void parse_define(void)
2807 Sym *s, *first, **ps;
2808 int v, t, varg, is_vaargs, c;
2809 TokenString str;
2811 v = tok;
2812 if (v < TOK_IDENT)
2813 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2814 /* XXX: should check if same macro (ANSI) */
2815 first = NULL;
2816 t = MACRO_OBJ;
2817 /* '(' must be just after macro definition for MACRO_FUNC */
2818 c = file->buf_ptr[0];
2819 if (c == '\\')
2820 c = handle_stray1(file->buf_ptr);
2821 if (c == '(') {
2822 next_nomacro();
2823 next_nomacro();
2824 ps = &first;
2825 while (tok != ')') {
2826 varg = tok;
2827 next_nomacro();
2828 is_vaargs = 0;
2829 if (varg == TOK_DOTS) {
2830 varg = TOK___VA_ARGS__;
2831 is_vaargs = 1;
2832 } else if (tok == TOK_DOTS && gnu_ext) {
2833 is_vaargs = 1;
2834 next_nomacro();
2836 if (varg < TOK_IDENT)
2837 error("badly punctuated parameter list");
2838 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2839 *ps = s;
2840 ps = &s->next;
2841 if (tok != ',')
2842 break;
2843 next_nomacro();
2845 t = MACRO_FUNC;
2847 tok_str_new(&str);
2848 next_nomacro();
2849 /* EOF testing necessary for '-D' handling */
2850 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2851 tok_str_add2(&str, tok, &tokc);
2852 next_nomacro();
2854 tok_str_add(&str, 0);
2855 #ifdef PP_DEBUG
2856 printf("define %s %d: ", get_tok_str(v, NULL), t);
2857 tok_print(str.str);
2858 #endif
2859 define_push(v, t, str.str, first);
2862 static inline int hash_cached_include(int type, const char *filename)
2864 const unsigned char *s;
2865 unsigned int h;
2867 h = TOK_HASH_INIT;
2868 h = TOK_HASH_FUNC(h, type);
2869 s = filename;
2870 while (*s) {
2871 h = TOK_HASH_FUNC(h, *s);
2872 s++;
2874 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2875 return h;
2878 /* XXX: use a token or a hash table to accelerate matching ? */
2879 static CachedInclude *search_cached_include(TCCState *s1,
2880 int type, const char *filename)
2882 CachedInclude *e;
2883 int i, h;
2884 h = hash_cached_include(type, filename);
2885 i = s1->cached_includes_hash[h];
2886 for(;;) {
2887 if (i == 0)
2888 break;
2889 e = s1->cached_includes[i - 1];
2890 if (e->type == type && !strcmp(e->filename, filename))
2891 return e;
2892 i = e->hash_next;
2894 return NULL;
2897 static inline void add_cached_include(TCCState *s1, int type,
2898 const char *filename, int ifndef_macro)
2900 CachedInclude *e;
2901 int h;
2903 if (search_cached_include(s1, type, filename))
2904 return;
2905 #ifdef INC_DEBUG
2906 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2907 #endif
2908 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2909 if (!e)
2910 return;
2911 e->type = type;
2912 strcpy(e->filename, filename);
2913 e->ifndef_macro = ifndef_macro;
2914 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2915 /* add in hash table */
2916 h = hash_cached_include(type, filename);
2917 e->hash_next = s1->cached_includes_hash[h];
2918 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2921 static void pragma_parse(TCCState *s1)
2923 int val;
2925 next();
2926 if (tok == TOK_pack) {
2928 This may be:
2929 #pragma pack(1) // set
2930 #pragma pack() // reset to default
2931 #pragma pack(push,1) // push & set
2932 #pragma pack(pop) // restore previous
2934 next();
2935 skip('(');
2936 if (tok == TOK_ASM_pop) {
2937 next();
2938 if (s1->pack_stack_ptr <= s1->pack_stack) {
2939 stk_error:
2940 error("out of pack stack");
2942 s1->pack_stack_ptr--;
2943 } else {
2944 val = 0;
2945 if (tok != ')') {
2946 if (tok == TOK_ASM_push) {
2947 next();
2948 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2949 goto stk_error;
2950 s1->pack_stack_ptr++;
2951 skip(',');
2953 if (tok != TOK_CINT) {
2954 pack_error:
2955 error("invalid pack pragma");
2957 val = tokc.i;
2958 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2959 goto pack_error;
2960 next();
2962 *s1->pack_stack_ptr = val;
2963 skip(')');
2968 /* is_bof is true if first non space token at beginning of file */
2969 static void preprocess(int is_bof)
2971 TCCState *s1 = tcc_state;
2972 int size, i, c, n, saved_parse_flags;
2973 char buf[1024], *q;
2974 char buf1[1024];
2975 BufferedFile *f;
2976 Sym *s;
2977 CachedInclude *e;
2979 saved_parse_flags = parse_flags;
2980 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2981 PARSE_FLAG_LINEFEED;
2982 next_nomacro();
2983 redo:
2984 switch(tok) {
2985 case TOK_DEFINE:
2986 next_nomacro();
2987 parse_define();
2988 break;
2989 case TOK_UNDEF:
2990 next_nomacro();
2991 s = define_find(tok);
2992 /* undefine symbol by putting an invalid name */
2993 if (s)
2994 define_undef(s);
2995 break;
2996 case TOK_INCLUDE:
2997 case TOK_INCLUDE_NEXT:
2998 ch = file->buf_ptr[0];
2999 /* XXX: incorrect if comments : use next_nomacro with a special mode */
3000 skip_spaces();
3001 if (ch == '<') {
3002 c = '>';
3003 goto read_name;
3004 } else if (ch == '\"') {
3005 c = ch;
3006 read_name:
3007 inp();
3008 q = buf;
3009 while (ch != c && ch != '\n' && ch != CH_EOF) {
3010 if ((q - buf) < sizeof(buf) - 1)
3011 *q++ = ch;
3012 if (ch == '\\') {
3013 if (handle_stray_noerror() == 0)
3014 --q;
3015 } else
3016 inp();
3018 *q = '\0';
3019 minp();
3020 #if 0
3021 /* eat all spaces and comments after include */
3022 /* XXX: slightly incorrect */
3023 while (ch1 != '\n' && ch1 != CH_EOF)
3024 inp();
3025 #endif
3026 } else {
3027 /* computed #include : either we have only strings or
3028 we have anything enclosed in '<>' */
3029 next();
3030 buf[0] = '\0';
3031 if (tok == TOK_STR) {
3032 while (tok != TOK_LINEFEED) {
3033 if (tok != TOK_STR) {
3034 include_syntax:
3035 error("'#include' expects \"FILENAME\" or <FILENAME>");
3037 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3038 next();
3040 c = '\"';
3041 } else {
3042 int len;
3043 while (tok != TOK_LINEFEED) {
3044 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3045 next();
3047 len = strlen(buf);
3048 /* check syntax and remove '<>' */
3049 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3050 goto include_syntax;
3051 memmove(buf, buf + 1, len - 2);
3052 buf[len - 2] = '\0';
3053 c = '>';
3057 e = search_cached_include(s1, c, buf);
3058 if (e && define_find(e->ifndef_macro)) {
3059 /* no need to parse the include because the 'ifndef macro'
3060 is defined */
3061 #ifdef INC_DEBUG
3062 printf("%s: skipping %s\n", file->filename, buf);
3063 #endif
3064 } else {
3065 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3066 error("#include recursion too deep");
3067 /* push current file in stack */
3068 /* XXX: fix current line init */
3069 *s1->include_stack_ptr++ = file;
3070 if (c == '\"') {
3071 /* first search in current dir if "header.h" */
3072 size = tcc_basename(file->filename) - file->filename;
3073 if (size > sizeof(buf1) - 1)
3074 size = sizeof(buf1) - 1;
3075 memcpy(buf1, file->filename, size);
3076 buf1[size] = '\0';
3077 pstrcat(buf1, sizeof(buf1), buf);
3078 f = tcc_open(s1, buf1);
3079 if (f) {
3080 if (tok == TOK_INCLUDE_NEXT)
3081 tok = TOK_INCLUDE;
3082 else
3083 goto found;
3086 /* now search in all the include paths */
3087 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3088 for(i = 0; i < n; i++) {
3089 const char *path;
3090 if (i < s1->nb_include_paths)
3091 path = s1->include_paths[i];
3092 else
3093 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3094 pstrcpy(buf1, sizeof(buf1), path);
3095 pstrcat(buf1, sizeof(buf1), "/");
3096 pstrcat(buf1, sizeof(buf1), buf);
3097 f = tcc_open(s1, buf1);
3098 if (f) {
3099 if (tok == TOK_INCLUDE_NEXT)
3100 tok = TOK_INCLUDE;
3101 else
3102 goto found;
3105 --s1->include_stack_ptr;
3106 error("include file '%s' not found", buf);
3107 break;
3108 found:
3109 #ifdef INC_DEBUG
3110 printf("%s: including %s\n", file->filename, buf1);
3111 #endif
3112 f->inc_type = c;
3113 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3114 file = f;
3115 /* add include file debug info */
3116 if (do_debug) {
3117 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3119 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3120 ch = file->buf_ptr[0];
3121 goto the_end;
3123 break;
3124 case TOK_IFNDEF:
3125 c = 1;
3126 goto do_ifdef;
3127 case TOK_IF:
3128 c = expr_preprocess();
3129 goto do_if;
3130 case TOK_IFDEF:
3131 c = 0;
3132 do_ifdef:
3133 next_nomacro();
3134 if (tok < TOK_IDENT)
3135 error("invalid argument for '#if%sdef'", c ? "n" : "");
3136 if (is_bof) {
3137 if (c) {
3138 #ifdef INC_DEBUG
3139 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3140 #endif
3141 file->ifndef_macro = tok;
3144 c = (define_find(tok) != 0) ^ c;
3145 do_if:
3146 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3147 error("memory full");
3148 *s1->ifdef_stack_ptr++ = c;
3149 goto test_skip;
3150 case TOK_ELSE:
3151 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3152 error("#else without matching #if");
3153 if (s1->ifdef_stack_ptr[-1] & 2)
3154 error("#else after #else");
3155 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3156 goto test_skip;
3157 case TOK_ELIF:
3158 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3159 error("#elif without matching #if");
3160 c = s1->ifdef_stack_ptr[-1];
3161 if (c > 1)
3162 error("#elif after #else");
3163 /* last #if/#elif expression was true: we skip */
3164 if (c == 1)
3165 goto skip;
3166 c = expr_preprocess();
3167 s1->ifdef_stack_ptr[-1] = c;
3168 test_skip:
3169 if (!(c & 1)) {
3170 skip:
3171 preprocess_skip();
3172 is_bof = 0;
3173 goto redo;
3175 break;
3176 case TOK_ENDIF:
3177 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3178 error("#endif without matching #if");
3179 s1->ifdef_stack_ptr--;
3180 /* '#ifndef macro' was at the start of file. Now we check if
3181 an '#endif' is exactly at the end of file */
3182 if (file->ifndef_macro &&
3183 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3184 file->ifndef_macro_saved = file->ifndef_macro;
3185 /* need to set to zero to avoid false matches if another
3186 #ifndef at middle of file */
3187 file->ifndef_macro = 0;
3188 while (tok != TOK_LINEFEED)
3189 next_nomacro();
3190 tok_flags |= TOK_FLAG_ENDIF;
3191 goto the_end;
3193 break;
3194 case TOK_LINE:
3195 next();
3196 if (tok != TOK_CINT)
3197 error("#line");
3198 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3199 next();
3200 if (tok != TOK_LINEFEED) {
3201 if (tok != TOK_STR)
3202 error("#line");
3203 pstrcpy(file->filename, sizeof(file->filename),
3204 (char *)tokc.cstr->data);
3206 break;
3207 case TOK_ERROR:
3208 case TOK_WARNING:
3209 c = tok;
3210 ch = file->buf_ptr[0];
3211 skip_spaces();
3212 q = buf;
3213 while (ch != '\n' && ch != CH_EOF) {
3214 if ((q - buf) < sizeof(buf) - 1)
3215 *q++ = ch;
3216 if (ch == '\\') {
3217 if (handle_stray_noerror() == 0)
3218 --q;
3219 } else
3220 inp();
3222 *q = '\0';
3223 if (c == TOK_ERROR)
3224 error("#error %s", buf);
3225 else
3226 warning("#warning %s", buf);
3227 break;
3228 case TOK_PRAGMA:
3229 pragma_parse(s1);
3230 break;
3231 default:
3232 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3233 /* '!' is ignored to allow C scripts. numbers are ignored
3234 to emulate cpp behaviour */
3235 } else {
3236 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3237 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3239 break;
3241 /* ignore other preprocess commands or #! for C scripts */
3242 while (tok != TOK_LINEFEED)
3243 next_nomacro();
3244 the_end:
3245 parse_flags = saved_parse_flags;
3248 /* evaluate escape codes in a string. */
3249 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3251 int c, n;
3252 const uint8_t *p;
3254 p = buf;
3255 for(;;) {
3256 c = *p;
3257 if (c == '\0')
3258 break;
3259 if (c == '\\') {
3260 p++;
3261 /* escape */
3262 c = *p;
3263 switch(c) {
3264 case '0': case '1': case '2': case '3':
3265 case '4': case '5': case '6': case '7':
3266 /* at most three octal digits */
3267 n = c - '0';
3268 p++;
3269 c = *p;
3270 if (isoct(c)) {
3271 n = n * 8 + c - '0';
3272 p++;
3273 c = *p;
3274 if (isoct(c)) {
3275 n = n * 8 + c - '0';
3276 p++;
3279 c = n;
3280 goto add_char_nonext;
3281 case 'x':
3282 case 'u':
3283 case 'U':
3284 p++;
3285 n = 0;
3286 for(;;) {
3287 c = *p;
3288 if (c >= 'a' && c <= 'f')
3289 c = c - 'a' + 10;
3290 else if (c >= 'A' && c <= 'F')
3291 c = c - 'A' + 10;
3292 else if (isnum(c))
3293 c = c - '0';
3294 else
3295 break;
3296 n = n * 16 + c;
3297 p++;
3299 c = n;
3300 goto add_char_nonext;
3301 case 'a':
3302 c = '\a';
3303 break;
3304 case 'b':
3305 c = '\b';
3306 break;
3307 case 'f':
3308 c = '\f';
3309 break;
3310 case 'n':
3311 c = '\n';
3312 break;
3313 case 'r':
3314 c = '\r';
3315 break;
3316 case 't':
3317 c = '\t';
3318 break;
3319 case 'v':
3320 c = '\v';
3321 break;
3322 case 'e':
3323 if (!gnu_ext)
3324 goto invalid_escape;
3325 c = 27;
3326 break;
3327 case '\'':
3328 case '\"':
3329 case '\\':
3330 case '?':
3331 break;
3332 default:
3333 invalid_escape:
3334 if (c >= '!' && c <= '~')
3335 warning("unknown escape sequence: \'\\%c\'", c);
3336 else
3337 warning("unknown escape sequence: \'\\x%x\'", c);
3338 break;
3341 p++;
3342 add_char_nonext:
3343 if (!is_long)
3344 cstr_ccat(outstr, c);
3345 else
3346 cstr_wccat(outstr, c);
3348 /* add a trailing '\0' */
3349 if (!is_long)
3350 cstr_ccat(outstr, '\0');
3351 else
3352 cstr_wccat(outstr, '\0');
3355 /* we use 64 bit numbers */
3356 #define BN_SIZE 2
3358 /* bn = (bn << shift) | or_val */
3359 void bn_lshift(unsigned int *bn, int shift, int or_val)
3361 int i;
3362 unsigned int v;
3363 for(i=0;i<BN_SIZE;i++) {
3364 v = bn[i];
3365 bn[i] = (v << shift) | or_val;
3366 or_val = v >> (32 - shift);
3370 void bn_zero(unsigned int *bn)
3372 int i;
3373 for(i=0;i<BN_SIZE;i++) {
3374 bn[i] = 0;
3378 /* parse number in null terminated string 'p' and return it in the
3379 current token */
3380 void parse_number(const char *p)
3382 int b, t, shift, frac_bits, s, exp_val, ch;
3383 char *q;
3384 unsigned int bn[BN_SIZE];
3385 double d;
3387 /* number */
3388 q = token_buf;
3389 ch = *p++;
3390 t = ch;
3391 ch = *p++;
3392 *q++ = t;
3393 b = 10;
3394 if (t == '.') {
3395 goto float_frac_parse;
3396 } else if (t == '0') {
3397 if (ch == 'x' || ch == 'X') {
3398 q--;
3399 ch = *p++;
3400 b = 16;
3401 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3402 q--;
3403 ch = *p++;
3404 b = 2;
3407 /* parse all digits. cannot check octal numbers at this stage
3408 because of floating point constants */
3409 while (1) {
3410 if (ch >= 'a' && ch <= 'f')
3411 t = ch - 'a' + 10;
3412 else if (ch >= 'A' && ch <= 'F')
3413 t = ch - 'A' + 10;
3414 else if (isnum(ch))
3415 t = ch - '0';
3416 else
3417 break;
3418 if (t >= b)
3419 break;
3420 if (q >= token_buf + STRING_MAX_SIZE) {
3421 num_too_long:
3422 error("number too long");
3424 *q++ = ch;
3425 ch = *p++;
3427 if (ch == '.' ||
3428 ((ch == 'e' || ch == 'E') && b == 10) ||
3429 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3430 if (b != 10) {
3431 /* NOTE: strtox should support that for hexa numbers, but
3432 non ISOC99 libcs do not support it, so we prefer to do
3433 it by hand */
3434 /* hexadecimal or binary floats */
3435 /* XXX: handle overflows */
3436 *q = '\0';
3437 if (b == 16)
3438 shift = 4;
3439 else
3440 shift = 2;
3441 bn_zero(bn);
3442 q = token_buf;
3443 while (1) {
3444 t = *q++;
3445 if (t == '\0') {
3446 break;
3447 } else if (t >= 'a') {
3448 t = t - 'a' + 10;
3449 } else if (t >= 'A') {
3450 t = t - 'A' + 10;
3451 } else {
3452 t = t - '0';
3454 bn_lshift(bn, shift, t);
3456 frac_bits = 0;
3457 if (ch == '.') {
3458 ch = *p++;
3459 while (1) {
3460 t = ch;
3461 if (t >= 'a' && t <= 'f') {
3462 t = t - 'a' + 10;
3463 } else if (t >= 'A' && t <= 'F') {
3464 t = t - 'A' + 10;
3465 } else if (t >= '0' && t <= '9') {
3466 t = t - '0';
3467 } else {
3468 break;
3470 if (t >= b)
3471 error("invalid digit");
3472 bn_lshift(bn, shift, t);
3473 frac_bits += shift;
3474 ch = *p++;
3477 if (ch != 'p' && ch != 'P')
3478 expect("exponent");
3479 ch = *p++;
3480 s = 1;
3481 exp_val = 0;
3482 if (ch == '+') {
3483 ch = *p++;
3484 } else if (ch == '-') {
3485 s = -1;
3486 ch = *p++;
3488 if (ch < '0' || ch > '9')
3489 expect("exponent digits");
3490 while (ch >= '0' && ch <= '9') {
3491 exp_val = exp_val * 10 + ch - '0';
3492 ch = *p++;
3494 exp_val = exp_val * s;
3496 /* now we can generate the number */
3497 /* XXX: should patch directly float number */
3498 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3499 d = ldexp(d, exp_val - frac_bits);
3500 t = toup(ch);
3501 if (t == 'F') {
3502 ch = *p++;
3503 tok = TOK_CFLOAT;
3504 /* float : should handle overflow */
3505 tokc.f = (float)d;
3506 } else if (t == 'L') {
3507 ch = *p++;
3508 tok = TOK_CLDOUBLE;
3509 /* XXX: not large enough */
3510 tokc.ld = (long double)d;
3511 } else {
3512 tok = TOK_CDOUBLE;
3513 tokc.d = d;
3515 } else {
3516 /* decimal floats */
3517 if (ch == '.') {
3518 if (q >= token_buf + STRING_MAX_SIZE)
3519 goto num_too_long;
3520 *q++ = ch;
3521 ch = *p++;
3522 float_frac_parse:
3523 while (ch >= '0' && ch <= '9') {
3524 if (q >= token_buf + STRING_MAX_SIZE)
3525 goto num_too_long;
3526 *q++ = ch;
3527 ch = *p++;
3530 if (ch == 'e' || ch == 'E') {
3531 if (q >= token_buf + STRING_MAX_SIZE)
3532 goto num_too_long;
3533 *q++ = ch;
3534 ch = *p++;
3535 if (ch == '-' || ch == '+') {
3536 if (q >= token_buf + STRING_MAX_SIZE)
3537 goto num_too_long;
3538 *q++ = ch;
3539 ch = *p++;
3541 if (ch < '0' || ch > '9')
3542 expect("exponent digits");
3543 while (ch >= '0' && ch <= '9') {
3544 if (q >= token_buf + STRING_MAX_SIZE)
3545 goto num_too_long;
3546 *q++ = ch;
3547 ch = *p++;
3550 *q = '\0';
3551 t = toup(ch);
3552 errno = 0;
3553 if (t == 'F') {
3554 ch = *p++;
3555 tok = TOK_CFLOAT;
3556 tokc.f = strtof(token_buf, NULL);
3557 } else if (t == 'L') {
3558 ch = *p++;
3559 tok = TOK_CLDOUBLE;
3560 tokc.ld = strtold(token_buf, NULL);
3561 } else {
3562 tok = TOK_CDOUBLE;
3563 tokc.d = strtod(token_buf, NULL);
3566 } else {
3567 unsigned long long n, n1;
3568 int lcount, ucount;
3570 /* integer number */
3571 *q = '\0';
3572 q = token_buf;
3573 if (b == 10 && *q == '0') {
3574 b = 8;
3575 q++;
3577 n = 0;
3578 while(1) {
3579 t = *q++;
3580 /* no need for checks except for base 10 / 8 errors */
3581 if (t == '\0') {
3582 break;
3583 } else if (t >= 'a') {
3584 t = t - 'a' + 10;
3585 } else if (t >= 'A') {
3586 t = t - 'A' + 10;
3587 } else {
3588 t = t - '0';
3589 if (t >= b)
3590 error("invalid digit");
3592 n1 = n;
3593 n = n * b + t;
3594 /* detect overflow */
3595 /* XXX: this test is not reliable */
3596 if (n < n1)
3597 error("integer constant overflow");
3600 /* XXX: not exactly ANSI compliant */
3601 if ((n & 0xffffffff00000000LL) != 0) {
3602 if ((n >> 63) != 0)
3603 tok = TOK_CULLONG;
3604 else
3605 tok = TOK_CLLONG;
3606 } else if (n > 0x7fffffff) {
3607 tok = TOK_CUINT;
3608 } else {
3609 tok = TOK_CINT;
3611 lcount = 0;
3612 ucount = 0;
3613 for(;;) {
3614 t = toup(ch);
3615 if (t == 'L') {
3616 if (lcount >= 2)
3617 error("three 'l's in integer constant");
3618 lcount++;
3619 if (lcount == 2) {
3620 if (tok == TOK_CINT)
3621 tok = TOK_CLLONG;
3622 else if (tok == TOK_CUINT)
3623 tok = TOK_CULLONG;
3625 ch = *p++;
3626 } else if (t == 'U') {
3627 if (ucount >= 1)
3628 error("two 'u's in integer constant");
3629 ucount++;
3630 if (tok == TOK_CINT)
3631 tok = TOK_CUINT;
3632 else if (tok == TOK_CLLONG)
3633 tok = TOK_CULLONG;
3634 ch = *p++;
3635 } else {
3636 break;
3639 if (tok == TOK_CINT || tok == TOK_CUINT)
3640 tokc.ui = n;
3641 else
3642 tokc.ull = n;
3647 #define PARSE2(c1, tok1, c2, tok2) \
3648 case c1: \
3649 PEEKC(c, p); \
3650 if (c == c2) { \
3651 p++; \
3652 tok = tok2; \
3653 } else { \
3654 tok = tok1; \
3656 break;
3658 /* return next token without macro substitution */
3659 static inline void next_nomacro1(void)
3661 int t, c, is_long;
3662 TokenSym *ts;
3663 uint8_t *p, *p1;
3664 unsigned int h;
3666 p = file->buf_ptr;
3667 redo_no_start:
3668 c = *p;
3669 switch(c) {
3670 case ' ':
3671 case '\t':
3672 case '\f':
3673 case '\v':
3674 case '\r':
3675 p++;
3676 goto redo_no_start;
3678 case '\\':
3679 /* first look if it is in fact an end of buffer */
3680 if (p >= file->buf_end) {
3681 file->buf_ptr = p;
3682 handle_eob();
3683 p = file->buf_ptr;
3684 if (p >= file->buf_end)
3685 goto parse_eof;
3686 else
3687 goto redo_no_start;
3688 } else {
3689 file->buf_ptr = p;
3690 ch = *p;
3691 handle_stray();
3692 p = file->buf_ptr;
3693 goto redo_no_start;
3695 parse_eof:
3697 TCCState *s1 = tcc_state;
3698 if ((parse_flags & PARSE_FLAG_LINEFEED)
3699 && !(tok_flags & TOK_FLAG_EOF)) {
3700 tok_flags |= TOK_FLAG_EOF;
3701 tok = TOK_LINEFEED;
3702 goto keep_tok_flags;
3703 } else if (s1->include_stack_ptr == s1->include_stack ||
3704 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3705 /* no include left : end of file. */
3706 tok = TOK_EOF;
3707 } else {
3708 tok_flags &= ~TOK_FLAG_EOF;
3709 /* pop include file */
3711 /* test if previous '#endif' was after a #ifdef at
3712 start of file */
3713 if (tok_flags & TOK_FLAG_ENDIF) {
3714 #ifdef INC_DEBUG
3715 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3716 #endif
3717 add_cached_include(s1, file->inc_type, file->inc_filename,
3718 file->ifndef_macro_saved);
3721 /* add end of include file debug info */
3722 if (do_debug) {
3723 put_stabd(N_EINCL, 0, 0);
3725 /* pop include stack */
3726 tcc_close(file);
3727 s1->include_stack_ptr--;
3728 file = *s1->include_stack_ptr;
3729 p = file->buf_ptr;
3730 goto redo_no_start;
3733 break;
3735 case '\n':
3736 file->line_num++;
3737 tok_flags |= TOK_FLAG_BOL;
3738 p++;
3739 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3740 goto redo_no_start;
3741 tok = TOK_LINEFEED;
3742 goto keep_tok_flags;
3744 case '#':
3745 /* XXX: simplify */
3746 PEEKC(c, p);
3747 if ((tok_flags & TOK_FLAG_BOL) &&
3748 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3749 file->buf_ptr = p;
3750 preprocess(tok_flags & TOK_FLAG_BOF);
3751 p = file->buf_ptr;
3752 goto redo_no_start;
3753 } else {
3754 if (c == '#') {
3755 p++;
3756 tok = TOK_TWOSHARPS;
3757 } else {
3758 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3759 p = parse_line_comment(p - 1);
3760 goto redo_no_start;
3761 } else {
3762 tok = '#';
3766 break;
3768 case 'a': case 'b': case 'c': case 'd':
3769 case 'e': case 'f': case 'g': case 'h':
3770 case 'i': case 'j': case 'k': case 'l':
3771 case 'm': case 'n': case 'o': case 'p':
3772 case 'q': case 'r': case 's': case 't':
3773 case 'u': case 'v': case 'w': case 'x':
3774 case 'y': case 'z':
3775 case 'A': case 'B': case 'C': case 'D':
3776 case 'E': case 'F': case 'G': case 'H':
3777 case 'I': case 'J': case 'K':
3778 case 'M': case 'N': case 'O': case 'P':
3779 case 'Q': case 'R': case 'S': case 'T':
3780 case 'U': case 'V': case 'W': case 'X':
3781 case 'Y': case 'Z':
3782 case '_':
3783 parse_ident_fast:
3784 p1 = p;
3785 h = TOK_HASH_INIT;
3786 h = TOK_HASH_FUNC(h, c);
3787 p++;
3788 for(;;) {
3789 c = *p;
3790 if (!isidnum_table[c-CH_EOF])
3791 break;
3792 h = TOK_HASH_FUNC(h, c);
3793 p++;
3795 if (c != '\\') {
3796 TokenSym **pts;
3797 int len;
3799 /* fast case : no stray found, so we have the full token
3800 and we have already hashed it */
3801 len = p - p1;
3802 h &= (TOK_HASH_SIZE - 1);
3803 pts = &hash_ident[h];
3804 for(;;) {
3805 ts = *pts;
3806 if (!ts)
3807 break;
3808 if (ts->len == len && !memcmp(ts->str, p1, len))
3809 goto token_found;
3810 pts = &(ts->hash_next);
3812 ts = tok_alloc_new(pts, p1, len);
3813 token_found: ;
3814 } else {
3815 /* slower case */
3816 cstr_reset(&tokcstr);
3818 while (p1 < p) {
3819 cstr_ccat(&tokcstr, *p1);
3820 p1++;
3822 p--;
3823 PEEKC(c, p);
3824 parse_ident_slow:
3825 while (isidnum_table[c-CH_EOF]) {
3826 cstr_ccat(&tokcstr, c);
3827 PEEKC(c, p);
3829 ts = tok_alloc(tokcstr.data, tokcstr.size);
3831 tok = ts->tok;
3832 break;
3833 case 'L':
3834 t = p[1];
3835 if (t != '\\' && t != '\'' && t != '\"') {
3836 /* fast case */
3837 goto parse_ident_fast;
3838 } else {
3839 PEEKC(c, p);
3840 if (c == '\'' || c == '\"') {
3841 is_long = 1;
3842 goto str_const;
3843 } else {
3844 cstr_reset(&tokcstr);
3845 cstr_ccat(&tokcstr, 'L');
3846 goto parse_ident_slow;
3849 break;
3850 case '0': case '1': case '2': case '3':
3851 case '4': case '5': case '6': case '7':
3852 case '8': case '9':
3854 cstr_reset(&tokcstr);
3855 /* after the first digit, accept digits, alpha, '.' or sign if
3856 prefixed by 'eEpP' */
3857 parse_num:
3858 for(;;) {
3859 t = c;
3860 cstr_ccat(&tokcstr, c);
3861 PEEKC(c, p);
3862 if (!(isnum(c) || isid(c) || c == '.' ||
3863 ((c == '+' || c == '-') &&
3864 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3865 break;
3867 /* We add a trailing '\0' to ease parsing */
3868 cstr_ccat(&tokcstr, '\0');
3869 tokc.cstr = &tokcstr;
3870 tok = TOK_PPNUM;
3871 break;
3872 case '.':
3873 /* special dot handling because it can also start a number */
3874 PEEKC(c, p);
3875 if (isnum(c)) {
3876 cstr_reset(&tokcstr);
3877 cstr_ccat(&tokcstr, '.');
3878 goto parse_num;
3879 } else if (c == '.') {
3880 PEEKC(c, p);
3881 if (c != '.')
3882 expect("'.'");
3883 PEEKC(c, p);
3884 tok = TOK_DOTS;
3885 } else {
3886 tok = '.';
3888 break;
3889 case '\'':
3890 case '\"':
3891 is_long = 0;
3892 str_const:
3894 CString str;
3895 int sep;
3897 sep = c;
3899 /* parse the string */
3900 cstr_new(&str);
3901 p = parse_pp_string(p, sep, &str);
3902 cstr_ccat(&str, '\0');
3904 /* eval the escape (should be done as TOK_PPNUM) */
3905 cstr_reset(&tokcstr);
3906 parse_escape_string(&tokcstr, str.data, is_long);
3907 cstr_free(&str);
3909 if (sep == '\'') {
3910 int char_size;
3911 /* XXX: make it portable */
3912 if (!is_long)
3913 char_size = 1;
3914 else
3915 char_size = sizeof(nwchar_t);
3916 if (tokcstr.size <= char_size)
3917 error("empty character constant");
3918 if (tokcstr.size > 2 * char_size)
3919 warning("multi-character character constant");
3920 if (!is_long) {
3921 tokc.i = *(int8_t *)tokcstr.data;
3922 tok = TOK_CCHAR;
3923 } else {
3924 tokc.i = *(nwchar_t *)tokcstr.data;
3925 tok = TOK_LCHAR;
3927 } else {
3928 tokc.cstr = &tokcstr;
3929 if (!is_long)
3930 tok = TOK_STR;
3931 else
3932 tok = TOK_LSTR;
3935 break;
3937 case '<':
3938 PEEKC(c, p);
3939 if (c == '=') {
3940 p++;
3941 tok = TOK_LE;
3942 } else if (c == '<') {
3943 PEEKC(c, p);
3944 if (c == '=') {
3945 p++;
3946 tok = TOK_A_SHL;
3947 } else {
3948 tok = TOK_SHL;
3950 } else {
3951 tok = TOK_LT;
3953 break;
3955 case '>':
3956 PEEKC(c, p);
3957 if (c == '=') {
3958 p++;
3959 tok = TOK_GE;
3960 } else if (c == '>') {
3961 PEEKC(c, p);
3962 if (c == '=') {
3963 p++;
3964 tok = TOK_A_SAR;
3965 } else {
3966 tok = TOK_SAR;
3968 } else {
3969 tok = TOK_GT;
3971 break;
3973 case '&':
3974 PEEKC(c, p);
3975 if (c == '&') {
3976 p++;
3977 tok = TOK_LAND;
3978 } else if (c == '=') {
3979 p++;
3980 tok = TOK_A_AND;
3981 } else {
3982 tok = '&';
3984 break;
3986 case '|':
3987 PEEKC(c, p);
3988 if (c == '|') {
3989 p++;
3990 tok = TOK_LOR;
3991 } else if (c == '=') {
3992 p++;
3993 tok = TOK_A_OR;
3994 } else {
3995 tok = '|';
3997 break;
3999 case '+':
4000 PEEKC(c, p);
4001 if (c == '+') {
4002 p++;
4003 tok = TOK_INC;
4004 } else if (c == '=') {
4005 p++;
4006 tok = TOK_A_ADD;
4007 } else {
4008 tok = '+';
4010 break;
4012 case '-':
4013 PEEKC(c, p);
4014 if (c == '-') {
4015 p++;
4016 tok = TOK_DEC;
4017 } else if (c == '=') {
4018 p++;
4019 tok = TOK_A_SUB;
4020 } else if (c == '>') {
4021 p++;
4022 tok = TOK_ARROW;
4023 } else {
4024 tok = '-';
4026 break;
4028 PARSE2('!', '!', '=', TOK_NE)
4029 PARSE2('=', '=', '=', TOK_EQ)
4030 PARSE2('*', '*', '=', TOK_A_MUL)
4031 PARSE2('%', '%', '=', TOK_A_MOD)
4032 PARSE2('^', '^', '=', TOK_A_XOR)
4034 /* comments or operator */
4035 case '/':
4036 PEEKC(c, p);
4037 if (c == '*') {
4038 p = parse_comment(p);
4039 goto redo_no_start;
4040 } else if (c == '/') {
4041 p = parse_line_comment(p);
4042 goto redo_no_start;
4043 } else if (c == '=') {
4044 p++;
4045 tok = TOK_A_DIV;
4046 } else {
4047 tok = '/';
4049 break;
4051 /* simple tokens */
4052 case '(':
4053 case ')':
4054 case '[':
4055 case ']':
4056 case '{':
4057 case '}':
4058 case ',':
4059 case ';':
4060 case ':':
4061 case '?':
4062 case '~':
4063 case '$': /* only used in assembler */
4064 case '@': /* dito */
4065 tok = c;
4066 p++;
4067 break;
4068 default:
4069 error("unrecognized character \\x%02x", c);
4070 break;
4072 tok_flags = 0;
4073 keep_tok_flags:
4074 file->buf_ptr = p;
4075 #if defined(PARSE_DEBUG)
4076 printf("token = %s\n", get_tok_str(tok, &tokc));
4077 #endif
4080 /* return next token without macro substitution. Can read input from
4081 macro_ptr buffer */
4082 static void next_nomacro(void)
4084 if (macro_ptr) {
4085 redo:
4086 tok = *macro_ptr;
4087 if (tok) {
4088 TOK_GET(tok, macro_ptr, tokc);
4089 if (tok == TOK_LINENUM) {
4090 file->line_num = tokc.i;
4091 goto redo;
4094 } else {
4095 next_nomacro1();
4099 /* substitute args in macro_str and return allocated string */
4100 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4102 int *st, last_tok, t, notfirst;
4103 Sym *s;
4104 CValue cval;
4105 TokenString str;
4106 CString cstr;
4108 tok_str_new(&str);
4109 last_tok = 0;
4110 while(1) {
4111 TOK_GET(t, macro_str, cval);
4112 if (!t)
4113 break;
4114 if (t == '#') {
4115 /* stringize */
4116 TOK_GET(t, macro_str, cval);
4117 if (!t)
4118 break;
4119 s = sym_find2(args, t);
4120 if (s) {
4121 cstr_new(&cstr);
4122 st = (int *)s->c;
4123 notfirst = 0;
4124 while (*st) {
4125 if (notfirst)
4126 cstr_ccat(&cstr, ' ');
4127 TOK_GET(t, st, cval);
4128 cstr_cat(&cstr, get_tok_str(t, &cval));
4129 #ifndef PP_NOSPACES
4130 notfirst = 1;
4131 #endif
4133 cstr_ccat(&cstr, '\0');
4134 #ifdef PP_DEBUG
4135 printf("stringize: %s\n", (char *)cstr.data);
4136 #endif
4137 /* add string */
4138 cval.cstr = &cstr;
4139 tok_str_add2(&str, TOK_STR, &cval);
4140 cstr_free(&cstr);
4141 } else {
4142 tok_str_add2(&str, t, &cval);
4144 } else if (t >= TOK_IDENT) {
4145 s = sym_find2(args, t);
4146 if (s) {
4147 st = (int *)s->c;
4148 /* if '##' is present before or after, no arg substitution */
4149 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4150 /* special case for var arg macros : ## eats the
4151 ',' if empty VA_ARGS variable. */
4152 /* XXX: test of the ',' is not 100%
4153 reliable. should fix it to avoid security
4154 problems */
4155 if (gnu_ext && s->type.t &&
4156 last_tok == TOK_TWOSHARPS &&
4157 str.len >= 2 && str.str[str.len - 2] == ',') {
4158 if (*st == 0) {
4159 /* suppress ',' '##' */
4160 str.len -= 2;
4161 } else {
4162 /* suppress '##' and add variable */
4163 str.len--;
4164 goto add_var;
4166 } else {
4167 int t1;
4168 add_var:
4169 for(;;) {
4170 TOK_GET(t1, st, cval);
4171 if (!t1)
4172 break;
4173 tok_str_add2(&str, t1, &cval);
4176 } else {
4177 /* NOTE: the stream cannot be read when macro
4178 substituing an argument */
4179 macro_subst(&str, nested_list, st, NULL);
4181 } else {
4182 tok_str_add(&str, t);
4184 } else {
4185 tok_str_add2(&str, t, &cval);
4187 last_tok = t;
4189 tok_str_add(&str, 0);
4190 return str.str;
4193 static char const ab_month_name[12][4] =
4195 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4196 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4199 /* do macro substitution of current token with macro 's' and add
4200 result to (tok_str,tok_len). 'nested_list' is the list of all
4201 macros we got inside to avoid recursing. Return non zero if no
4202 substitution needs to be done */
4203 static int macro_subst_tok(TokenString *tok_str,
4204 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4206 Sym *args, *sa, *sa1;
4207 int mstr_allocated, parlevel, *mstr, t, t1;
4208 TokenString str;
4209 char *cstrval;
4210 CValue cval;
4211 CString cstr;
4212 char buf[32];
4214 /* if symbol is a macro, prepare substitution */
4215 /* special macros */
4216 if (tok == TOK___LINE__) {
4217 snprintf(buf, sizeof(buf), "%d", file->line_num);
4218 cstrval = buf;
4219 t1 = TOK_PPNUM;
4220 goto add_cstr1;
4221 } else if (tok == TOK___FILE__) {
4222 cstrval = file->filename;
4223 goto add_cstr;
4224 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4225 time_t ti;
4226 struct tm *tm;
4228 time(&ti);
4229 tm = localtime(&ti);
4230 if (tok == TOK___DATE__) {
4231 snprintf(buf, sizeof(buf), "%s %2d %d",
4232 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4233 } else {
4234 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4235 tm->tm_hour, tm->tm_min, tm->tm_sec);
4237 cstrval = buf;
4238 add_cstr:
4239 t1 = TOK_STR;
4240 add_cstr1:
4241 cstr_new(&cstr);
4242 cstr_cat(&cstr, cstrval);
4243 cstr_ccat(&cstr, '\0');
4244 cval.cstr = &cstr;
4245 tok_str_add2(tok_str, t1, &cval);
4246 cstr_free(&cstr);
4247 } else {
4248 mstr = (int *)s->c;
4249 mstr_allocated = 0;
4250 if (s->type.t == MACRO_FUNC) {
4251 /* NOTE: we do not use next_nomacro to avoid eating the
4252 next token. XXX: find better solution */
4253 redo:
4254 if (macro_ptr) {
4255 t = *macro_ptr;
4256 if (t == 0 && can_read_stream) {
4257 /* end of macro stream: we must look at the token
4258 after in the file */
4259 struct macro_level *ml = *can_read_stream;
4260 macro_ptr = NULL;
4261 if (ml)
4263 macro_ptr = ml->p;
4264 ml->p = NULL;
4265 *can_read_stream = ml -> prev;
4267 goto redo;
4269 } else {
4270 /* XXX: incorrect with comments */
4271 ch = file->buf_ptr[0];
4272 while (is_space(ch) || ch == '\n')
4273 cinp();
4274 t = ch;
4276 if (t != '(') /* no macro subst */
4277 return -1;
4279 /* argument macro */
4280 next_nomacro();
4281 next_nomacro();
4282 args = NULL;
4283 sa = s->next;
4284 /* NOTE: empty args are allowed, except if no args */
4285 for(;;) {
4286 /* handle '()' case */
4287 if (!args && !sa && tok == ')')
4288 break;
4289 if (!sa)
4290 error("macro '%s' used with too many args",
4291 get_tok_str(s->v, 0));
4292 tok_str_new(&str);
4293 parlevel = 0;
4294 /* NOTE: non zero sa->t indicates VA_ARGS */
4295 while ((parlevel > 0 ||
4296 (tok != ')' &&
4297 (tok != ',' || sa->type.t))) &&
4298 tok != -1) {
4299 if (tok == '(')
4300 parlevel++;
4301 else if (tok == ')')
4302 parlevel--;
4303 if (tok != TOK_LINEFEED)
4304 tok_str_add2(&str, tok, &tokc);
4305 next_nomacro();
4307 tok_str_add(&str, 0);
4308 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (long)str.str);
4309 sa = sa->next;
4310 if (tok == ')') {
4311 /* special case for gcc var args: add an empty
4312 var arg argument if it is omitted */
4313 if (sa && sa->type.t && gnu_ext)
4314 continue;
4315 else
4316 break;
4318 if (tok != ',')
4319 expect(",");
4320 next_nomacro();
4322 if (sa) {
4323 error("macro '%s' used with too few args",
4324 get_tok_str(s->v, 0));
4327 /* now subst each arg */
4328 mstr = macro_arg_subst(nested_list, mstr, args);
4329 /* free memory */
4330 sa = args;
4331 while (sa) {
4332 sa1 = sa->prev;
4333 tok_str_free((int *)sa->c);
4334 sym_free(sa);
4335 sa = sa1;
4337 mstr_allocated = 1;
4339 sym_push2(nested_list, s->v, 0, 0);
4340 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4341 /* pop nested defined symbol */
4342 sa1 = *nested_list;
4343 *nested_list = sa1->prev;
4344 sym_free(sa1);
4345 if (mstr_allocated)
4346 tok_str_free(mstr);
4348 return 0;
4351 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4352 return the resulting string (which must be freed). */
4353 static inline int *macro_twosharps(const int *macro_str)
4355 TokenSym *ts;
4356 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4357 int t;
4358 const char *p1, *p2;
4359 CValue cval;
4360 TokenString macro_str1;
4361 CString cstr;
4363 start_macro_ptr = macro_str;
4364 /* we search the first '##' */
4365 for(;;) {
4366 macro_ptr1 = macro_str;
4367 TOK_GET(t, macro_str, cval);
4368 /* nothing more to do if end of string */
4369 if (t == 0)
4370 return NULL;
4371 if (*macro_str == TOK_TWOSHARPS)
4372 break;
4375 /* we saw '##', so we need more processing to handle it */
4376 cstr_new(&cstr);
4377 tok_str_new(&macro_str1);
4378 tok = t;
4379 tokc = cval;
4381 /* add all tokens seen so far */
4382 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4383 TOK_GET(t, ptr, cval);
4384 tok_str_add2(&macro_str1, t, &cval);
4386 saved_macro_ptr = macro_ptr;
4387 /* XXX: get rid of the use of macro_ptr here */
4388 macro_ptr = (int *)macro_str;
4389 for(;;) {
4390 while (*macro_ptr == TOK_TWOSHARPS) {
4391 macro_ptr++;
4392 macro_ptr1 = macro_ptr;
4393 t = *macro_ptr;
4394 if (t) {
4395 TOK_GET(t, macro_ptr, cval);
4396 /* We concatenate the two tokens if we have an
4397 identifier or a preprocessing number */
4398 cstr_reset(&cstr);
4399 p1 = get_tok_str(tok, &tokc);
4400 cstr_cat(&cstr, p1);
4401 p2 = get_tok_str(t, &cval);
4402 cstr_cat(&cstr, p2);
4403 cstr_ccat(&cstr, '\0');
4405 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4406 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4407 if (tok == TOK_PPNUM) {
4408 /* if number, then create a number token */
4409 /* NOTE: no need to allocate because
4410 tok_str_add2() does it */
4411 cstr_reset(&tokcstr);
4412 tokcstr = cstr;
4413 cstr_new(&cstr);
4414 tokc.cstr = &tokcstr;
4415 } else {
4416 /* if identifier, we must do a test to
4417 validate we have a correct identifier */
4418 if (t == TOK_PPNUM) {
4419 const char *p;
4420 int c;
4422 p = p2;
4423 for(;;) {
4424 c = *p;
4425 if (c == '\0')
4426 break;
4427 p++;
4428 if (!isnum(c) && !isid(c))
4429 goto error_pasting;
4432 ts = tok_alloc(cstr.data, strlen(cstr.data));
4433 tok = ts->tok; /* modify current token */
4435 } else {
4436 const char *str = cstr.data;
4437 const unsigned char *q;
4439 /* we look for a valid token */
4440 /* XXX: do more extensive checks */
4441 if (!strcmp(str, ">>=")) {
4442 tok = TOK_A_SAR;
4443 } else if (!strcmp(str, "<<=")) {
4444 tok = TOK_A_SHL;
4445 } else if (strlen(str) == 2) {
4446 /* search in two bytes table */
4447 q = tok_two_chars;
4448 for(;;) {
4449 if (!*q)
4450 goto error_pasting;
4451 if (q[0] == str[0] && q[1] == str[1])
4452 break;
4453 q += 3;
4455 tok = q[2];
4456 } else {
4457 error_pasting:
4458 /* NOTE: because get_tok_str use a static buffer,
4459 we must save it */
4460 cstr_reset(&cstr);
4461 p1 = get_tok_str(tok, &tokc);
4462 cstr_cat(&cstr, p1);
4463 cstr_ccat(&cstr, '\0');
4464 p2 = get_tok_str(t, &cval);
4465 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4466 /* cannot merge tokens: just add them separately */
4467 tok_str_add2(&macro_str1, tok, &tokc);
4468 /* XXX: free associated memory ? */
4469 tok = t;
4470 tokc = cval;
4475 tok_str_add2(&macro_str1, tok, &tokc);
4476 next_nomacro();
4477 if (tok == 0)
4478 break;
4480 macro_ptr = (int *)saved_macro_ptr;
4481 cstr_free(&cstr);
4482 tok_str_add(&macro_str1, 0);
4483 return macro_str1.str;
4487 /* do macro substitution of macro_str and add result to
4488 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4489 inside to avoid recursing. */
4490 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4491 const int *macro_str, struct macro_level ** can_read_stream)
4493 Sym *s;
4494 int *macro_str1;
4495 const int *ptr;
4496 int t, ret;
4497 CValue cval;
4498 struct macro_level ml;
4500 /* first scan for '##' operator handling */
4501 ptr = macro_str;
4502 macro_str1 = macro_twosharps(ptr);
4503 if (macro_str1)
4504 ptr = macro_str1;
4505 while (1) {
4506 /* NOTE: ptr == NULL can only happen if tokens are read from
4507 file stream due to a macro function call */
4508 if (ptr == NULL)
4509 break;
4510 TOK_GET(t, ptr, cval);
4511 if (t == 0)
4512 break;
4513 s = define_find(t);
4514 if (s != NULL) {
4515 /* if nested substitution, do nothing */
4516 if (sym_find2(*nested_list, t))
4517 goto no_subst;
4518 ml.p = macro_ptr;
4519 if (can_read_stream)
4520 ml.prev = *can_read_stream, *can_read_stream = &ml;
4521 macro_ptr = (int *)ptr;
4522 tok = t;
4523 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4524 ptr = (int *)macro_ptr;
4525 macro_ptr = ml.p;
4526 if (can_read_stream && *can_read_stream == &ml)
4527 *can_read_stream = ml.prev;
4528 if (ret != 0)
4529 goto no_subst;
4530 } else {
4531 no_subst:
4532 tok_str_add2(tok_str, t, &cval);
4535 if (macro_str1)
4536 tok_str_free(macro_str1);
4539 /* return next token with macro substitution */
4540 static void next(void)
4542 Sym *nested_list, *s;
4543 TokenString str;
4544 struct macro_level *ml;
4546 redo:
4547 next_nomacro();
4548 if (!macro_ptr) {
4549 /* if not reading from macro substituted string, then try
4550 to substitute macros */
4551 if (tok >= TOK_IDENT &&
4552 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4553 s = define_find(tok);
4554 if (s) {
4555 /* we have a macro: we try to substitute */
4556 tok_str_new(&str);
4557 nested_list = NULL;
4558 ml = NULL;
4559 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4560 /* substitution done, NOTE: maybe empty */
4561 tok_str_add(&str, 0);
4562 macro_ptr = str.str;
4563 macro_ptr_allocated = str.str;
4564 goto redo;
4568 } else {
4569 if (tok == 0) {
4570 /* end of macro or end of unget buffer */
4571 if (unget_buffer_enabled) {
4572 macro_ptr = unget_saved_macro_ptr;
4573 unget_buffer_enabled = 0;
4574 } else {
4575 /* end of macro string: free it */
4576 tok_str_free(macro_ptr_allocated);
4577 macro_ptr = NULL;
4579 goto redo;
4583 /* convert preprocessor tokens into C tokens */
4584 if (tok == TOK_PPNUM &&
4585 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4586 parse_number((char *)tokc.cstr->data);
4590 /* push back current token and set current token to 'last_tok'. Only
4591 identifier case handled for labels. */
4592 static inline void unget_tok(int last_tok)
4594 int i, n;
4595 int *q;
4596 unget_saved_macro_ptr = macro_ptr;
4597 unget_buffer_enabled = 1;
4598 q = unget_saved_buffer;
4599 macro_ptr = q;
4600 *q++ = tok;
4601 n = tok_ext_size(tok) - 1;
4602 for(i=0;i<n;i++)
4603 *q++ = tokc.tab[i];
4604 *q = 0; /* end of token string */
4605 tok = last_tok;
4609 void swap(int *p, int *q)
4611 int t;
4612 t = *p;
4613 *p = *q;
4614 *q = t;
4617 void vsetc(CType *type, int r, CValue *vc)
4619 int v;
4621 if (vtop >= vstack + (VSTACK_SIZE - 1))
4622 error("memory full");
4623 /* cannot let cpu flags if other instruction are generated. Also
4624 avoid leaving VT_JMP anywhere except on the top of the stack
4625 because it would complicate the code generator. */
4626 if (vtop >= vstack) {
4627 v = vtop->r & VT_VALMASK;
4628 if (v == VT_CMP || (v & ~1) == VT_JMP)
4629 gv(RC_INT);
4631 vtop++;
4632 vtop->type = *type;
4633 vtop->r = r;
4634 vtop->r2 = VT_CONST;
4635 vtop->c = *vc;
4638 /* push integer constant */
4639 void vpushi(int v)
4641 CValue cval;
4642 cval.i = v;
4643 vsetc(&int_type, VT_CONST, &cval);
4646 /* Return a static symbol pointing to a section */
4647 static Sym *get_sym_ref(CType *type, Section *sec,
4648 unsigned long offset, unsigned long size)
4650 int v;
4651 Sym *sym;
4653 v = anon_sym++;
4654 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4655 sym->type.ref = type->ref;
4656 sym->r = VT_CONST | VT_SYM;
4657 put_extern_sym(sym, sec, offset, size);
4658 return sym;
4661 /* push a reference to a section offset by adding a dummy symbol */
4662 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4664 CValue cval;
4666 cval.ul = 0;
4667 vsetc(type, VT_CONST | VT_SYM, &cval);
4668 vtop->sym = get_sym_ref(type, sec, offset, size);
4671 /* define a new external reference to a symbol 'v' of type 'u' */
4672 static Sym *external_global_sym(int v, CType *type, int r)
4674 Sym *s;
4676 s = sym_find(v);
4677 if (!s) {
4678 /* push forward reference */
4679 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4680 s->type.ref = type->ref;
4681 s->r = r | VT_CONST | VT_SYM;
4683 return s;
4686 /* define a new external reference to a symbol 'v' of type 'u' */
4687 static Sym *external_sym(int v, CType *type, int r)
4689 Sym *s;
4691 s = sym_find(v);
4692 if (!s) {
4693 /* push forward reference */
4694 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4695 s->type.t |= VT_EXTERN;
4696 } else {
4697 if (!is_compatible_types(&s->type, type))
4698 error("incompatible types for redefinition of '%s'",
4699 get_tok_str(v, NULL));
4701 return s;
4704 /* push a reference to global symbol v */
4705 static void vpush_global_sym(CType *type, int v)
4707 Sym *sym;
4708 CValue cval;
4710 sym = external_global_sym(v, type, 0);
4711 cval.ul = 0;
4712 vsetc(type, VT_CONST | VT_SYM, &cval);
4713 vtop->sym = sym;
4716 void vset(CType *type, int r, int v)
4718 CValue cval;
4720 cval.i = v;
4721 vsetc(type, r, &cval);
4724 void vseti(int r, int v)
4726 CType type;
4727 type.t = VT_INT;
4728 vset(&type, r, v);
4731 void vswap(void)
4733 SValue tmp;
4735 tmp = vtop[0];
4736 vtop[0] = vtop[-1];
4737 vtop[-1] = tmp;
4740 void vpushv(SValue *v)
4742 if (vtop >= vstack + (VSTACK_SIZE - 1))
4743 error("memory full");
4744 vtop++;
4745 *vtop = *v;
4748 void vdup(void)
4750 vpushv(vtop);
4753 /* save r to the memory stack, and mark it as being free */
4754 void save_reg(int r)
4756 int l, saved, size, align;
4757 SValue *p, sv;
4758 CType *type;
4760 /* modify all stack values */
4761 saved = 0;
4762 l = 0;
4763 for(p=vstack;p<=vtop;p++) {
4764 if ((p->r & VT_VALMASK) == r ||
4765 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4766 /* must save value on stack if not already done */
4767 if (!saved) {
4768 /* NOTE: must reload 'r' because r might be equal to r2 */
4769 r = p->r & VT_VALMASK;
4770 /* store register in the stack */
4771 type = &p->type;
4772 if ((p->r & VT_LVAL) ||
4773 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4774 type = &int_type;
4775 size = type_size(type, &align);
4776 loc = (loc - size) & -align;
4777 sv.type.t = type->t;
4778 sv.r = VT_LOCAL | VT_LVAL;
4779 sv.c.ul = loc;
4780 store(r, &sv);
4781 #ifdef TCC_TARGET_I386
4782 /* x86 specific: need to pop fp register ST0 if saved */
4783 if (r == TREG_ST0) {
4784 o(0xd9dd); /* fstp %st(1) */
4786 #endif
4787 /* special long long case */
4788 if ((type->t & VT_BTYPE) == VT_LLONG) {
4789 sv.c.ul += 4;
4790 store(p->r2, &sv);
4792 l = loc;
4793 saved = 1;
4795 /* mark that stack entry as being saved on the stack */
4796 if (p->r & VT_LVAL) {
4797 /* also clear the bounded flag because the
4798 relocation address of the function was stored in
4799 p->c.ul */
4800 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4801 } else {
4802 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4804 p->r2 = VT_CONST;
4805 p->c.ul = l;
4810 /* find a register of class 'rc2' with at most one reference on stack.
4811 * If none, call get_reg(rc) */
4812 int get_reg_ex(int rc, int rc2)
4814 int r;
4815 SValue *p;
4817 for(r=0;r<NB_REGS;r++) {
4818 if (reg_classes[r] & rc2) {
4819 int n;
4820 n=0;
4821 for(p = vstack; p <= vtop; p++) {
4822 if ((p->r & VT_VALMASK) == r ||
4823 (p->r2 & VT_VALMASK) == r)
4824 n++;
4826 if (n <= 1)
4827 return r;
4830 return get_reg(rc);
4833 /* find a free register of class 'rc'. If none, save one register */
4834 int get_reg(int rc)
4836 int r;
4837 SValue *p;
4839 /* find a free register */
4840 for(r=0;r<NB_REGS;r++) {
4841 if (reg_classes[r] & rc) {
4842 for(p=vstack;p<=vtop;p++) {
4843 if ((p->r & VT_VALMASK) == r ||
4844 (p->r2 & VT_VALMASK) == r)
4845 goto notfound;
4847 return r;
4849 notfound: ;
4852 /* no register left : free the first one on the stack (VERY
4853 IMPORTANT to start from the bottom to ensure that we don't
4854 spill registers used in gen_opi()) */
4855 for(p=vstack;p<=vtop;p++) {
4856 r = p->r & VT_VALMASK;
4857 if (r < VT_CONST && (reg_classes[r] & rc))
4858 goto save_found;
4859 /* also look at second register (if long long) */
4860 r = p->r2 & VT_VALMASK;
4861 if (r < VT_CONST && (reg_classes[r] & rc)) {
4862 save_found:
4863 save_reg(r);
4864 return r;
4867 /* Should never comes here */
4868 return -1;
4871 /* save registers up to (vtop - n) stack entry */
4872 void save_regs(int n)
4874 int r;
4875 SValue *p, *p1;
4876 p1 = vtop - n;
4877 for(p = vstack;p <= p1; p++) {
4878 r = p->r & VT_VALMASK;
4879 if (r < VT_CONST) {
4880 save_reg(r);
4885 /* move register 's' to 'r', and flush previous value of r to memory
4886 if needed */
4887 void move_reg(int r, int s)
4889 SValue sv;
4891 if (r != s) {
4892 save_reg(r);
4893 sv.type.t = VT_INT;
4894 sv.r = s;
4895 sv.c.ul = 0;
4896 load(r, &sv);
4900 /* get address of vtop (vtop MUST BE an lvalue) */
4901 void gaddrof(void)
4903 vtop->r &= ~VT_LVAL;
4904 /* tricky: if saved lvalue, then we can go back to lvalue */
4905 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4906 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4909 #ifdef CONFIG_TCC_BCHECK
4910 /* generate lvalue bound code */
4911 void gbound(void)
4913 int lval_type;
4914 CType type1;
4916 vtop->r &= ~VT_MUSTBOUND;
4917 /* if lvalue, then use checking code before dereferencing */
4918 if (vtop->r & VT_LVAL) {
4919 /* if not VT_BOUNDED value, then make one */
4920 if (!(vtop->r & VT_BOUNDED)) {
4921 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4922 /* must save type because we must set it to int to get pointer */
4923 type1 = vtop->type;
4924 vtop->type.t = VT_INT;
4925 gaddrof();
4926 vpushi(0);
4927 gen_bounded_ptr_add();
4928 vtop->r |= lval_type;
4929 vtop->type = type1;
4931 /* then check for dereferencing */
4932 gen_bounded_ptr_deref();
4935 #endif
4937 /* store vtop a register belonging to class 'rc'. lvalues are
4938 converted to values. Cannot be used if cannot be converted to
4939 register value (such as structures). */
4940 int gv(int rc)
4942 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4943 unsigned long long ll;
4945 /* NOTE: get_reg can modify vstack[] */
4946 if (vtop->type.t & VT_BITFIELD) {
4947 CType type;
4948 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4949 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4950 /* remove bit field info to avoid loops */
4951 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4952 /* cast to int to propagate signedness in following ops */
4953 type.t = VT_INT;
4954 if((vtop->type.t & VT_UNSIGNED) ||
4955 (vtop->type.t & VT_BTYPE) == VT_BOOL)
4956 type.t |= VT_UNSIGNED;
4957 gen_cast(&type);
4958 /* generate shifts */
4959 vpushi(32 - (bit_pos + bit_size));
4960 gen_op(TOK_SHL);
4961 vpushi(32 - bit_size);
4962 /* NOTE: transformed to SHR if unsigned */
4963 gen_op(TOK_SAR);
4964 r = gv(rc);
4965 } else {
4966 if (is_float(vtop->type.t) &&
4967 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4968 Sym *sym;
4969 int *ptr;
4970 unsigned long offset;
4971 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4972 CValue check;
4973 #endif
4975 /* XXX: unify with initializers handling ? */
4976 /* CPUs usually cannot use float constants, so we store them
4977 generically in data segment */
4978 size = type_size(&vtop->type, &align);
4979 offset = (data_section->data_offset + align - 1) & -align;
4980 data_section->data_offset = offset;
4981 /* XXX: not portable yet */
4982 #if defined(__i386__) || defined(__x86_64__)
4983 /* Zero pad x87 tenbyte long doubles */
4984 if (size == LDOUBLE_SIZE)
4985 vtop->c.tab[2] &= 0xffff;
4986 #endif
4987 ptr = section_ptr_add(data_section, size);
4988 size = size >> 2;
4989 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4990 check.d = 1;
4991 if(check.tab[0])
4992 for(i=0;i<size;i++)
4993 ptr[i] = vtop->c.tab[size-1-i];
4994 else
4995 #endif
4996 for(i=0;i<size;i++)
4997 ptr[i] = vtop->c.tab[i];
4998 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4999 vtop->r |= VT_LVAL | VT_SYM;
5000 vtop->sym = sym;
5001 vtop->c.ul = 0;
5003 #ifdef CONFIG_TCC_BCHECK
5004 if (vtop->r & VT_MUSTBOUND)
5005 gbound();
5006 #endif
5008 r = vtop->r & VT_VALMASK;
5009 rc2 = RC_INT;
5010 if (rc == RC_IRET)
5011 rc2 = RC_LRET;
5012 /* need to reload if:
5013 - constant
5014 - lvalue (need to dereference pointer)
5015 - already a register, but not in the right class */
5016 if (r >= VT_CONST ||
5017 (vtop->r & VT_LVAL) ||
5018 !(reg_classes[r] & rc) ||
5019 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
5020 !(reg_classes[vtop->r2] & rc2))) {
5021 r = get_reg(rc);
5022 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
5023 /* two register type load : expand to two words
5024 temporarily */
5025 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
5026 /* load constant */
5027 ll = vtop->c.ull;
5028 vtop->c.ui = ll; /* first word */
5029 load(r, vtop);
5030 vtop->r = r; /* save register value */
5031 vpushi(ll >> 32); /* second word */
5032 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
5033 (vtop->r & VT_LVAL)) {
5034 /* We do not want to modifier the long long
5035 pointer here, so the safest (and less
5036 efficient) is to save all the other registers
5037 in the stack. XXX: totally inefficient. */
5038 save_regs(1);
5039 /* load from memory */
5040 load(r, vtop);
5041 vdup();
5042 vtop[-1].r = r; /* save register value */
5043 /* increment pointer to get second word */
5044 vtop->type.t = VT_INT;
5045 gaddrof();
5046 vpushi(4);
5047 gen_op('+');
5048 vtop->r |= VT_LVAL;
5049 } else {
5050 /* move registers */
5051 load(r, vtop);
5052 vdup();
5053 vtop[-1].r = r; /* save register value */
5054 vtop->r = vtop[-1].r2;
5056 /* allocate second register */
5057 r2 = get_reg(rc2);
5058 load(r2, vtop);
5059 vpop();
5060 /* write second register */
5061 vtop->r2 = r2;
5062 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5063 int t1, t;
5064 /* lvalue of scalar type : need to use lvalue type
5065 because of possible cast */
5066 t = vtop->type.t;
5067 t1 = t;
5068 /* compute memory access type */
5069 if (vtop->r & VT_LVAL_BYTE)
5070 t = VT_BYTE;
5071 else if (vtop->r & VT_LVAL_SHORT)
5072 t = VT_SHORT;
5073 if (vtop->r & VT_LVAL_UNSIGNED)
5074 t |= VT_UNSIGNED;
5075 vtop->type.t = t;
5076 load(r, vtop);
5077 /* restore wanted type */
5078 vtop->type.t = t1;
5079 } else {
5080 /* one register type load */
5081 load(r, vtop);
5084 vtop->r = r;
5085 #ifdef TCC_TARGET_C67
5086 /* uses register pairs for doubles */
5087 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5088 vtop->r2 = r+1;
5089 #endif
5091 return r;
5094 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5095 void gv2(int rc1, int rc2)
5097 int v;
5099 /* generate more generic register first. But VT_JMP or VT_CMP
5100 values must be generated first in all cases to avoid possible
5101 reload errors */
5102 v = vtop[0].r & VT_VALMASK;
5103 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5104 vswap();
5105 gv(rc1);
5106 vswap();
5107 gv(rc2);
5108 /* test if reload is needed for first register */
5109 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5110 vswap();
5111 gv(rc1);
5112 vswap();
5114 } else {
5115 gv(rc2);
5116 vswap();
5117 gv(rc1);
5118 vswap();
5119 /* test if reload is needed for first register */
5120 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5121 gv(rc2);
5126 /* expand long long on stack in two int registers */
5127 void lexpand(void)
5129 int u;
5131 u = vtop->type.t & VT_UNSIGNED;
5132 gv(RC_INT);
5133 vdup();
5134 vtop[0].r = vtop[-1].r2;
5135 vtop[0].r2 = VT_CONST;
5136 vtop[-1].r2 = VT_CONST;
5137 vtop[0].type.t = VT_INT | u;
5138 vtop[-1].type.t = VT_INT | u;
5141 #ifdef TCC_TARGET_ARM
5142 /* expand long long on stack */
5143 void lexpand_nr(void)
5145 int u,v;
5147 u = vtop->type.t & VT_UNSIGNED;
5148 vdup();
5149 vtop->r2 = VT_CONST;
5150 vtop->type.t = VT_INT | u;
5151 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5152 if (v == VT_CONST) {
5153 vtop[-1].c.ui = vtop->c.ull;
5154 vtop->c.ui = vtop->c.ull >> 32;
5155 vtop->r = VT_CONST;
5156 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5157 vtop->c.ui += 4;
5158 vtop->r = vtop[-1].r;
5159 } else if (v > VT_CONST) {
5160 vtop--;
5161 lexpand();
5162 } else
5163 vtop->r = vtop[-1].r2;
5164 vtop[-1].r2 = VT_CONST;
5165 vtop[-1].type.t = VT_INT | u;
5167 #endif
5169 /* build a long long from two ints */
5170 void lbuild(int t)
5172 gv2(RC_INT, RC_INT);
5173 vtop[-1].r2 = vtop[0].r;
5174 vtop[-1].type.t = t;
5175 vpop();
5178 /* rotate n first stack elements to the bottom
5179 I1 ... In -> I2 ... In I1 [top is right]
5181 void vrotb(int n)
5183 int i;
5184 SValue tmp;
5186 tmp = vtop[-n + 1];
5187 for(i=-n+1;i!=0;i++)
5188 vtop[i] = vtop[i+1];
5189 vtop[0] = tmp;
5192 /* rotate n first stack elements to the top
5193 I1 ... In -> In I1 ... I(n-1) [top is right]
5195 void vrott(int n)
5197 int i;
5198 SValue tmp;
5200 tmp = vtop[0];
5201 for(i = 0;i < n - 1; i++)
5202 vtop[-i] = vtop[-i - 1];
5203 vtop[-n + 1] = tmp;
5206 #ifdef TCC_TARGET_ARM
5207 /* like vrott but in other direction
5208 In ... I1 -> I(n-1) ... I1 In [top is right]
5210 void vnrott(int n)
5212 int i;
5213 SValue tmp;
5215 tmp = vtop[-n + 1];
5216 for(i = n - 1; i > 0; i--)
5217 vtop[-i] = vtop[-i + 1];
5218 vtop[0] = tmp;
5220 #endif
5222 /* pop stack value */
5223 void vpop(void)
5225 int v;
5226 v = vtop->r & VT_VALMASK;
5227 #ifdef TCC_TARGET_I386
5228 /* for x86, we need to pop the FP stack */
5229 if (v == TREG_ST0 && !nocode_wanted) {
5230 o(0xd9dd); /* fstp %st(1) */
5231 } else
5232 #endif
5233 if (v == VT_JMP || v == VT_JMPI) {
5234 /* need to put correct jump if && or || without test */
5235 gsym(vtop->c.ul);
5237 vtop--;
5240 /* convert stack entry to register and duplicate its value in another
5241 register */
5242 void gv_dup(void)
5244 int rc, t, r, r1;
5245 SValue sv;
5247 t = vtop->type.t;
5248 if ((t & VT_BTYPE) == VT_LLONG) {
5249 lexpand();
5250 gv_dup();
5251 vswap();
5252 vrotb(3);
5253 gv_dup();
5254 vrotb(4);
5255 /* stack: H L L1 H1 */
5256 lbuild(t);
5257 vrotb(3);
5258 vrotb(3);
5259 vswap();
5260 lbuild(t);
5261 vswap();
5262 } else {
5263 /* duplicate value */
5264 rc = RC_INT;
5265 sv.type.t = VT_INT;
5266 if (is_float(t)) {
5267 rc = RC_FLOAT;
5268 sv.type.t = t;
5270 r = gv(rc);
5271 r1 = get_reg(rc);
5272 sv.r = r;
5273 sv.c.ul = 0;
5274 load(r1, &sv); /* move r to r1 */
5275 vdup();
5276 /* duplicates value */
5277 vtop->r = r1;
5281 /* generate CPU independent (unsigned) long long operations */
5282 void gen_opl(int op)
5284 int t, a, b, op1, c, i;
5285 int func;
5286 unsigned short reg_iret = REG_IRET;
5287 unsigned short reg_lret = REG_LRET;
5288 SValue tmp;
5290 switch(op) {
5291 case '/':
5292 case TOK_PDIV:
5293 func = TOK___divdi3;
5294 goto gen_func;
5295 case TOK_UDIV:
5296 func = TOK___udivdi3;
5297 goto gen_func;
5298 case '%':
5299 func = TOK___moddi3;
5300 goto gen_mod_func;
5301 case TOK_UMOD:
5302 func = TOK___umoddi3;
5303 gen_mod_func:
5304 #ifdef TCC_ARM_EABI
5305 reg_iret = TREG_R2;
5306 reg_lret = TREG_R3;
5307 #endif
5308 gen_func:
5309 /* call generic long long function */
5310 vpush_global_sym(&func_old_type, func);
5311 vrott(3);
5312 gfunc_call(2);
5313 vpushi(0);
5314 vtop->r = reg_iret;
5315 vtop->r2 = reg_lret;
5316 break;
5317 case '^':
5318 case '&':
5319 case '|':
5320 case '*':
5321 case '+':
5322 case '-':
5323 t = vtop->type.t;
5324 vswap();
5325 lexpand();
5326 vrotb(3);
5327 lexpand();
5328 /* stack: L1 H1 L2 H2 */
5329 tmp = vtop[0];
5330 vtop[0] = vtop[-3];
5331 vtop[-3] = tmp;
5332 tmp = vtop[-2];
5333 vtop[-2] = vtop[-3];
5334 vtop[-3] = tmp;
5335 vswap();
5336 /* stack: H1 H2 L1 L2 */
5337 if (op == '*') {
5338 vpushv(vtop - 1);
5339 vpushv(vtop - 1);
5340 gen_op(TOK_UMULL);
5341 lexpand();
5342 /* stack: H1 H2 L1 L2 ML MH */
5343 for(i=0;i<4;i++)
5344 vrotb(6);
5345 /* stack: ML MH H1 H2 L1 L2 */
5346 tmp = vtop[0];
5347 vtop[0] = vtop[-2];
5348 vtop[-2] = tmp;
5349 /* stack: ML MH H1 L2 H2 L1 */
5350 gen_op('*');
5351 vrotb(3);
5352 vrotb(3);
5353 gen_op('*');
5354 /* stack: ML MH M1 M2 */
5355 gen_op('+');
5356 gen_op('+');
5357 } else if (op == '+' || op == '-') {
5358 /* XXX: add non carry method too (for MIPS or alpha) */
5359 if (op == '+')
5360 op1 = TOK_ADDC1;
5361 else
5362 op1 = TOK_SUBC1;
5363 gen_op(op1);
5364 /* stack: H1 H2 (L1 op L2) */
5365 vrotb(3);
5366 vrotb(3);
5367 gen_op(op1 + 1); /* TOK_xxxC2 */
5368 } else {
5369 gen_op(op);
5370 /* stack: H1 H2 (L1 op L2) */
5371 vrotb(3);
5372 vrotb(3);
5373 /* stack: (L1 op L2) H1 H2 */
5374 gen_op(op);
5375 /* stack: (L1 op L2) (H1 op H2) */
5377 /* stack: L H */
5378 lbuild(t);
5379 break;
5380 case TOK_SAR:
5381 case TOK_SHR:
5382 case TOK_SHL:
5383 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5384 t = vtop[-1].type.t;
5385 vswap();
5386 lexpand();
5387 vrotb(3);
5388 /* stack: L H shift */
5389 c = (int)vtop->c.i;
5390 /* constant: simpler */
5391 /* NOTE: all comments are for SHL. the other cases are
5392 done by swaping words */
5393 vpop();
5394 if (op != TOK_SHL)
5395 vswap();
5396 if (c >= 32) {
5397 /* stack: L H */
5398 vpop();
5399 if (c > 32) {
5400 vpushi(c - 32);
5401 gen_op(op);
5403 if (op != TOK_SAR) {
5404 vpushi(0);
5405 } else {
5406 gv_dup();
5407 vpushi(31);
5408 gen_op(TOK_SAR);
5410 vswap();
5411 } else {
5412 vswap();
5413 gv_dup();
5414 /* stack: H L L */
5415 vpushi(c);
5416 gen_op(op);
5417 vswap();
5418 vpushi(32 - c);
5419 if (op == TOK_SHL)
5420 gen_op(TOK_SHR);
5421 else
5422 gen_op(TOK_SHL);
5423 vrotb(3);
5424 /* stack: L L H */
5425 vpushi(c);
5426 if (op == TOK_SHL)
5427 gen_op(TOK_SHL);
5428 else
5429 gen_op(TOK_SHR);
5430 gen_op('|');
5432 if (op != TOK_SHL)
5433 vswap();
5434 lbuild(t);
5435 } else {
5436 /* XXX: should provide a faster fallback on x86 ? */
5437 switch(op) {
5438 case TOK_SAR:
5439 func = TOK___ashrdi3;
5440 goto gen_func;
5441 case TOK_SHR:
5442 func = TOK___lshrdi3;
5443 goto gen_func;
5444 case TOK_SHL:
5445 func = TOK___ashldi3;
5446 goto gen_func;
5449 break;
5450 default:
5451 /* compare operations */
5452 t = vtop->type.t;
5453 vswap();
5454 lexpand();
5455 vrotb(3);
5456 lexpand();
5457 /* stack: L1 H1 L2 H2 */
5458 tmp = vtop[-1];
5459 vtop[-1] = vtop[-2];
5460 vtop[-2] = tmp;
5461 /* stack: L1 L2 H1 H2 */
5462 /* compare high */
5463 op1 = op;
5464 /* when values are equal, we need to compare low words. since
5465 the jump is inverted, we invert the test too. */
5466 if (op1 == TOK_LT)
5467 op1 = TOK_LE;
5468 else if (op1 == TOK_GT)
5469 op1 = TOK_GE;
5470 else if (op1 == TOK_ULT)
5471 op1 = TOK_ULE;
5472 else if (op1 == TOK_UGT)
5473 op1 = TOK_UGE;
5474 a = 0;
5475 b = 0;
5476 gen_op(op1);
5477 if (op1 != TOK_NE) {
5478 a = gtst(1, 0);
5480 if (op != TOK_EQ) {
5481 /* generate non equal test */
5482 /* XXX: NOT PORTABLE yet */
5483 if (a == 0) {
5484 b = gtst(0, 0);
5485 } else {
5486 #if defined(TCC_TARGET_I386)
5487 b = psym(0x850f, 0);
5488 #elif defined(TCC_TARGET_ARM)
5489 b = ind;
5490 o(0x1A000000 | encbranch(ind, 0, 1));
5491 #elif defined(TCC_TARGET_C67)
5492 error("not implemented");
5493 #else
5494 #error not supported
5495 #endif
5498 /* compare low. Always unsigned */
5499 op1 = op;
5500 if (op1 == TOK_LT)
5501 op1 = TOK_ULT;
5502 else if (op1 == TOK_LE)
5503 op1 = TOK_ULE;
5504 else if (op1 == TOK_GT)
5505 op1 = TOK_UGT;
5506 else if (op1 == TOK_GE)
5507 op1 = TOK_UGE;
5508 gen_op(op1);
5509 a = gtst(1, a);
5510 gsym(b);
5511 vseti(VT_JMPI, a);
5512 break;
5516 /* handle integer constant optimizations and various machine
5517 independent opt */
5518 void gen_opic(int op)
5520 int c1, c2, t1, t2, n;
5521 SValue *v1, *v2;
5522 long long l1, l2;
5523 typedef unsigned long long U;
5525 v1 = vtop - 1;
5526 v2 = vtop;
5527 t1 = v1->type.t & VT_BTYPE;
5528 t2 = v2->type.t & VT_BTYPE;
5529 l1 = (t1 == VT_LLONG) ? v1->c.ll : v1->c.i;
5530 l2 = (t2 == VT_LLONG) ? v2->c.ll : v2->c.i;
5532 /* currently, we cannot do computations with forward symbols */
5533 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5534 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5535 if (c1 && c2) {
5536 switch(op) {
5537 case '+': l1 += l2; break;
5538 case '-': l1 -= l2; break;
5539 case '&': l1 &= l2; break;
5540 case '^': l1 ^= l2; break;
5541 case '|': l1 |= l2; break;
5542 case '*': l1 *= l2; break;
5544 case TOK_PDIV:
5545 case '/':
5546 case '%':
5547 case TOK_UDIV:
5548 case TOK_UMOD:
5549 /* if division by zero, generate explicit division */
5550 if (l2 == 0) {
5551 if (const_wanted)
5552 error("division by zero in constant");
5553 goto general_case;
5555 switch(op) {
5556 default: l1 /= l2; break;
5557 case '%': l1 %= l2; break;
5558 case TOK_UDIV: l1 = (U)l1 / l2; break;
5559 case TOK_UMOD: l1 = (U)l1 % l2; break;
5561 break;
5562 case TOK_SHL: l1 <<= l2; break;
5563 case TOK_SHR: l1 = (U)l1 >> l2; break;
5564 case TOK_SAR: l1 >>= l2; break;
5565 /* tests */
5566 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5567 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5568 case TOK_EQ: l1 = l1 == l2; break;
5569 case TOK_NE: l1 = l1 != l2; break;
5570 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5571 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5572 case TOK_LT: l1 = l1 < l2; break;
5573 case TOK_GE: l1 = l1 >= l2; break;
5574 case TOK_LE: l1 = l1 <= l2; break;
5575 case TOK_GT: l1 = l1 > l2; break;
5576 /* logical */
5577 case TOK_LAND: l1 = l1 && l2; break;
5578 case TOK_LOR: l1 = l1 || l2; break;
5579 default:
5580 goto general_case;
5582 v1->c.ll = l1;
5583 vtop--;
5584 } else {
5585 /* if commutative ops, put c2 as constant */
5586 if (c1 && (op == '+' || op == '&' || op == '^' ||
5587 op == '|' || op == '*')) {
5588 vswap();
5589 c2 = c1; //c = c1, c1 = c2, c2 = c;
5590 l2 = l1; //l = l1, l1 = l2, l2 = l;
5592 /* Filter out NOP operations like x*1, x-0, x&-1... */
5593 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5594 op == TOK_PDIV) &&
5595 l2 == 1) ||
5596 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5597 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5598 l2 == 0) ||
5599 (op == '&' &&
5600 l2 == -1))) {
5601 /* nothing to do */
5602 vtop--;
5603 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5604 /* try to use shifts instead of muls or divs */
5605 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5606 n = -1;
5607 while (l2) {
5608 l2 >>= 1;
5609 n++;
5611 vtop->c.ll = n;
5612 if (op == '*')
5613 op = TOK_SHL;
5614 else if (op == TOK_PDIV)
5615 op = TOK_SAR;
5616 else
5617 op = TOK_SHR;
5619 goto general_case;
5620 } else if (c2 && (op == '+' || op == '-') &&
5621 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5622 (VT_CONST | VT_SYM) ||
5623 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
5624 /* symbol + constant case */
5625 if (op == '-')
5626 l2 = -l2;
5627 vtop--;
5628 vtop->c.ll += l2;
5629 } else {
5630 general_case:
5631 if (!nocode_wanted) {
5632 /* call low level op generator */
5633 if (t1 == VT_LLONG || t2 == VT_LLONG)
5634 gen_opl(op);
5635 else
5636 gen_opi(op);
5637 } else {
5638 vtop--;
5644 /* generate a floating point operation with constant propagation */
5645 void gen_opif(int op)
5647 int c1, c2;
5648 SValue *v1, *v2;
5649 long double f1, f2;
5651 v1 = vtop - 1;
5652 v2 = vtop;
5653 /* currently, we cannot do computations with forward symbols */
5654 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5655 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5656 if (c1 && c2) {
5657 if (v1->type.t == VT_FLOAT) {
5658 f1 = v1->c.f;
5659 f2 = v2->c.f;
5660 } else if (v1->type.t == VT_DOUBLE) {
5661 f1 = v1->c.d;
5662 f2 = v2->c.d;
5663 } else {
5664 f1 = v1->c.ld;
5665 f2 = v2->c.ld;
5668 /* NOTE: we only do constant propagation if finite number (not
5669 NaN or infinity) (ANSI spec) */
5670 if (!ieee_finite(f1) || !ieee_finite(f2))
5671 goto general_case;
5673 switch(op) {
5674 case '+': f1 += f2; break;
5675 case '-': f1 -= f2; break;
5676 case '*': f1 *= f2; break;
5677 case '/':
5678 if (f2 == 0.0) {
5679 if (const_wanted)
5680 error("division by zero in constant");
5681 goto general_case;
5683 f1 /= f2;
5684 break;
5685 /* XXX: also handles tests ? */
5686 default:
5687 goto general_case;
5689 /* XXX: overflow test ? */
5690 if (v1->type.t == VT_FLOAT) {
5691 v1->c.f = f1;
5692 } else if (v1->type.t == VT_DOUBLE) {
5693 v1->c.d = f1;
5694 } else {
5695 v1->c.ld = f1;
5697 vtop--;
5698 } else {
5699 general_case:
5700 if (!nocode_wanted) {
5701 gen_opf(op);
5702 } else {
5703 vtop--;
5708 static int pointed_size(CType *type)
5710 int align;
5711 return type_size(pointed_type(type), &align);
5714 static inline int is_null_pointer(SValue *p)
5716 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5717 return 0;
5718 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5719 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5722 static inline int is_integer_btype(int bt)
5724 return (bt == VT_BYTE || bt == VT_SHORT ||
5725 bt == VT_INT || bt == VT_LLONG);
5728 /* check types for comparison or substraction of pointers */
5729 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5731 CType *type1, *type2, tmp_type1, tmp_type2;
5732 int bt1, bt2;
5734 /* null pointers are accepted for all comparisons as gcc */
5735 if (is_null_pointer(p1) || is_null_pointer(p2))
5736 return;
5737 type1 = &p1->type;
5738 type2 = &p2->type;
5739 bt1 = type1->t & VT_BTYPE;
5740 bt2 = type2->t & VT_BTYPE;
5741 /* accept comparison between pointer and integer with a warning */
5742 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5743 if (op != TOK_LOR && op != TOK_LAND )
5744 warning("comparison between pointer and integer");
5745 return;
5748 /* both must be pointers or implicit function pointers */
5749 if (bt1 == VT_PTR) {
5750 type1 = pointed_type(type1);
5751 } else if (bt1 != VT_FUNC)
5752 goto invalid_operands;
5754 if (bt2 == VT_PTR) {
5755 type2 = pointed_type(type2);
5756 } else if (bt2 != VT_FUNC) {
5757 invalid_operands:
5758 error("invalid operands to binary %s", get_tok_str(op, NULL));
5760 if ((type1->t & VT_BTYPE) == VT_VOID ||
5761 (type2->t & VT_BTYPE) == VT_VOID)
5762 return;
5763 tmp_type1 = *type1;
5764 tmp_type2 = *type2;
5765 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5766 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5767 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5768 /* gcc-like error if '-' is used */
5769 if (op == '-')
5770 goto invalid_operands;
5771 else
5772 warning("comparison of distinct pointer types lacks a cast");
5776 /* generic gen_op: handles types problems */
5777 void gen_op(int op)
5779 int u, t1, t2, bt1, bt2, t;
5780 CType type1;
5782 t1 = vtop[-1].type.t;
5783 t2 = vtop[0].type.t;
5784 bt1 = t1 & VT_BTYPE;
5785 bt2 = t2 & VT_BTYPE;
5787 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5788 /* at least one operand is a pointer */
5789 /* relationnal op: must be both pointers */
5790 if (op >= TOK_ULT && op <= TOK_LOR) {
5791 check_comparison_pointer_types(vtop - 1, vtop, op);
5792 /* pointers are handled are unsigned */
5793 t = VT_INT | VT_UNSIGNED;
5794 goto std_op;
5796 /* if both pointers, then it must be the '-' op */
5797 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5798 if (op != '-')
5799 error("cannot use pointers here");
5800 check_comparison_pointer_types(vtop - 1, vtop, op);
5801 /* XXX: check that types are compatible */
5802 u = pointed_size(&vtop[-1].type);
5803 gen_opic(op);
5804 /* set to integer type */
5805 vtop->type.t = VT_INT;
5806 vpushi(u);
5807 gen_op(TOK_PDIV);
5808 } else {
5809 /* exactly one pointer : must be '+' or '-'. */
5810 if (op != '-' && op != '+')
5811 error("cannot use pointers here");
5812 /* Put pointer as first operand */
5813 if (bt2 == VT_PTR) {
5814 vswap();
5815 swap(&t1, &t2);
5817 type1 = vtop[-1].type;
5818 /* XXX: cast to int ? (long long case) */
5819 vpushi(pointed_size(&vtop[-1].type));
5820 gen_op('*');
5821 #ifdef CONFIG_TCC_BCHECK
5822 /* if evaluating constant expression, no code should be
5823 generated, so no bound check */
5824 if (do_bounds_check && !const_wanted) {
5825 /* if bounded pointers, we generate a special code to
5826 test bounds */
5827 if (op == '-') {
5828 vpushi(0);
5829 vswap();
5830 gen_op('-');
5832 gen_bounded_ptr_add();
5833 } else
5834 #endif
5836 gen_opic(op);
5838 /* put again type if gen_opic() swaped operands */
5839 vtop->type = type1;
5841 } else if (is_float(bt1) || is_float(bt2)) {
5842 /* compute bigger type and do implicit casts */
5843 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5844 t = VT_LDOUBLE;
5845 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5846 t = VT_DOUBLE;
5847 } else {
5848 t = VT_FLOAT;
5850 /* floats can only be used for a few operations */
5851 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5852 (op < TOK_ULT || op > TOK_GT))
5853 error("invalid operands for binary operation");
5854 goto std_op;
5855 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5856 /* cast to biggest op */
5857 t = VT_LLONG;
5858 /* convert to unsigned if it does not fit in a long long */
5859 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5860 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5861 t |= VT_UNSIGNED;
5862 goto std_op;
5863 } else {
5864 /* integer operations */
5865 t = VT_INT;
5866 /* convert to unsigned if it does not fit in an integer */
5867 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5868 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5869 t |= VT_UNSIGNED;
5870 std_op:
5871 /* XXX: currently, some unsigned operations are explicit, so
5872 we modify them here */
5873 if (t & VT_UNSIGNED) {
5874 if (op == TOK_SAR)
5875 op = TOK_SHR;
5876 else if (op == '/')
5877 op = TOK_UDIV;
5878 else if (op == '%')
5879 op = TOK_UMOD;
5880 else if (op == TOK_LT)
5881 op = TOK_ULT;
5882 else if (op == TOK_GT)
5883 op = TOK_UGT;
5884 else if (op == TOK_LE)
5885 op = TOK_ULE;
5886 else if (op == TOK_GE)
5887 op = TOK_UGE;
5889 vswap();
5890 type1.t = t;
5891 gen_cast(&type1);
5892 vswap();
5893 /* special case for shifts and long long: we keep the shift as
5894 an integer */
5895 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5896 type1.t = VT_INT;
5897 gen_cast(&type1);
5898 if (is_float(t))
5899 gen_opif(op);
5900 else
5901 gen_opic(op);
5902 if (op >= TOK_ULT && op <= TOK_GT) {
5903 /* relationnal op: the result is an int */
5904 vtop->type.t = VT_INT;
5905 } else {
5906 vtop->type.t = t;
5911 #ifndef TCC_TARGET_ARM
5912 /* generic itof for unsigned long long case */
5913 void gen_cvt_itof1(int t)
5915 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5916 (VT_LLONG | VT_UNSIGNED)) {
5918 if (t == VT_FLOAT)
5919 vpush_global_sym(&func_old_type, TOK___floatundisf);
5920 #if LDOUBLE_SIZE != 8
5921 else if (t == VT_LDOUBLE)
5922 vpush_global_sym(&func_old_type, TOK___floatundixf);
5923 #endif
5924 else
5925 vpush_global_sym(&func_old_type, TOK___floatundidf);
5926 vrott(2);
5927 gfunc_call(1);
5928 vpushi(0);
5929 vtop->r = REG_FRET;
5930 } else {
5931 gen_cvt_itof(t);
5934 #endif
5936 /* generic ftoi for unsigned long long case */
5937 void gen_cvt_ftoi1(int t)
5939 int st;
5941 if (t == (VT_LLONG | VT_UNSIGNED)) {
5942 /* not handled natively */
5943 st = vtop->type.t & VT_BTYPE;
5944 if (st == VT_FLOAT)
5945 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5946 #if LDOUBLE_SIZE != 8
5947 else if (st == VT_LDOUBLE)
5948 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5949 #endif
5950 else
5951 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5952 vrott(2);
5953 gfunc_call(1);
5954 vpushi(0);
5955 vtop->r = REG_IRET;
5956 vtop->r2 = REG_LRET;
5957 } else {
5958 gen_cvt_ftoi(t);
5962 /* force char or short cast */
5963 void force_charshort_cast(int t)
5965 int bits, dbt;
5966 dbt = t & VT_BTYPE;
5967 /* XXX: add optimization if lvalue : just change type and offset */
5968 if (dbt == VT_BYTE)
5969 bits = 8;
5970 else
5971 bits = 16;
5972 if (t & VT_UNSIGNED) {
5973 vpushi((1 << bits) - 1);
5974 gen_op('&');
5975 } else {
5976 bits = 32 - bits;
5977 vpushi(bits);
5978 gen_op(TOK_SHL);
5979 /* result must be signed or the SAR is converted to an SHL
5980 This was not the case when "t" was a signed short
5981 and the last value on the stack was an unsigned int */
5982 vtop->type.t &= ~VT_UNSIGNED;
5983 vpushi(bits);
5984 gen_op(TOK_SAR);
5988 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5989 static void gen_cast(CType *type)
5991 int sbt, dbt, sf, df, c, p;
5993 /* special delayed cast for char/short */
5994 /* XXX: in some cases (multiple cascaded casts), it may still
5995 be incorrect */
5996 if (vtop->r & VT_MUSTCAST) {
5997 vtop->r &= ~VT_MUSTCAST;
5998 force_charshort_cast(vtop->type.t);
6001 /* bitfields first get cast to ints */
6002 if (vtop->type.t & VT_BITFIELD) {
6003 gv(RC_INT);
6006 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
6007 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
6009 if (sbt != dbt) {
6010 sf = is_float(sbt);
6011 df = is_float(dbt);
6012 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6013 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
6014 if (c) {
6015 /* constant case: we can do it now */
6016 /* XXX: in ISOC, cannot do it if error in convert */
6017 if (sbt == VT_FLOAT)
6018 vtop->c.ld = vtop->c.f;
6019 else if (sbt == VT_DOUBLE)
6020 vtop->c.ld = vtop->c.d;
6022 if (df) {
6023 if ((sbt & VT_BTYPE) == VT_LLONG) {
6024 if (sbt & VT_UNSIGNED)
6025 vtop->c.ld = vtop->c.ull;
6026 else
6027 vtop->c.ld = vtop->c.ll;
6028 } else if(!sf) {
6029 if (sbt & VT_UNSIGNED)
6030 vtop->c.ld = vtop->c.ui;
6031 else
6032 vtop->c.ld = vtop->c.i;
6035 if (dbt == VT_FLOAT)
6036 vtop->c.f = (float)vtop->c.ld;
6037 else if (dbt == VT_DOUBLE)
6038 vtop->c.d = (double)vtop->c.ld;
6039 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
6040 vtop->c.ull = (unsigned long long)vtop->c.ld;
6041 } else if (sf && dbt == VT_BOOL) {
6042 vtop->c.i = (vtop->c.ld != 0);
6043 } else {
6044 if(sf)
6045 vtop->c.ll = (long long)vtop->c.ld;
6046 else if (sbt == (VT_LLONG|VT_UNSIGNED))
6047 vtop->c.ll = vtop->c.ull;
6048 else if (sbt & VT_UNSIGNED)
6049 vtop->c.ll = vtop->c.ui;
6050 else if (sbt != VT_LLONG)
6051 vtop->c.ll = vtop->c.i;
6053 if (dbt == (VT_LLONG|VT_UNSIGNED))
6054 vtop->c.ull = vtop->c.ll;
6055 else if (dbt == VT_BOOL)
6056 vtop->c.i = (vtop->c.ll != 0);
6057 else if (dbt != VT_LLONG) {
6058 int s = 0;
6059 if ((dbt & VT_BTYPE) == VT_BYTE)
6060 s = 24;
6061 else if ((dbt & VT_BTYPE) == VT_SHORT)
6062 s = 16;
6064 if(dbt & VT_UNSIGNED)
6065 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
6066 else
6067 vtop->c.i = ((int)vtop->c.ll << s) >> s;
6070 } else if (p && dbt == VT_BOOL) {
6071 vtop->r = VT_CONST;
6072 vtop->c.i = 1;
6073 } else if (!nocode_wanted) {
6074 /* non constant case: generate code */
6075 if (sf && df) {
6076 /* convert from fp to fp */
6077 gen_cvt_ftof(dbt);
6078 } else if (df) {
6079 /* convert int to fp */
6080 gen_cvt_itof1(dbt);
6081 } else if (sf) {
6082 /* convert fp to int */
6083 if (dbt == VT_BOOL) {
6084 vpushi(0);
6085 gen_op(TOK_NE);
6086 } else {
6087 /* we handle char/short/etc... with generic code */
6088 if (dbt != (VT_INT | VT_UNSIGNED) &&
6089 dbt != (VT_LLONG | VT_UNSIGNED) &&
6090 dbt != VT_LLONG)
6091 dbt = VT_INT;
6092 gen_cvt_ftoi1(dbt);
6093 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6094 /* additional cast for char/short... */
6095 vtop->type.t = dbt;
6096 gen_cast(type);
6099 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6100 if ((sbt & VT_BTYPE) != VT_LLONG) {
6101 /* scalar to long long */
6102 /* machine independent conversion */
6103 gv(RC_INT);
6104 /* generate high word */
6105 if (sbt == (VT_INT | VT_UNSIGNED)) {
6106 vpushi(0);
6107 gv(RC_INT);
6108 } else {
6109 gv_dup();
6110 vpushi(31);
6111 gen_op(TOK_SAR);
6113 /* patch second register */
6114 vtop[-1].r2 = vtop->r;
6115 vpop();
6117 } else if (dbt == VT_BOOL) {
6118 /* scalar to bool */
6119 vpushi(0);
6120 gen_op(TOK_NE);
6121 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6122 (dbt & VT_BTYPE) == VT_SHORT) {
6123 if (sbt == VT_PTR) {
6124 vtop->type.t = VT_INT;
6125 warning("nonportable conversion from pointer to char/short");
6127 force_charshort_cast(dbt);
6128 } else if ((dbt & VT_BTYPE) == VT_INT) {
6129 /* scalar to int */
6130 if (sbt == VT_LLONG) {
6131 /* from long long: just take low order word */
6132 lexpand();
6133 vpop();
6135 /* if lvalue and single word type, nothing to do because
6136 the lvalue already contains the real type size (see
6137 VT_LVAL_xxx constants) */
6140 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6141 /* if we are casting between pointer types,
6142 we must update the VT_LVAL_xxx size */
6143 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6144 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6146 vtop->type = *type;
6149 /* return type size. Put alignment at 'a' */
6150 static int type_size(CType *type, int *a)
6152 Sym *s;
6153 int bt;
6155 bt = type->t & VT_BTYPE;
6156 if (bt == VT_STRUCT) {
6157 /* struct/union */
6158 s = type->ref;
6159 *a = s->r;
6160 return s->c;
6161 } else if (bt == VT_PTR) {
6162 if (type->t & VT_ARRAY) {
6163 s = type->ref;
6164 return type_size(&s->type, a) * s->c;
6165 } else {
6166 *a = PTR_SIZE;
6167 return PTR_SIZE;
6169 } else if (bt == VT_LDOUBLE) {
6170 *a = LDOUBLE_ALIGN;
6171 return LDOUBLE_SIZE;
6172 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6173 #ifdef TCC_TARGET_I386
6174 *a = 4;
6175 #elif defined(TCC_TARGET_ARM)
6176 #ifdef TCC_ARM_EABI
6177 *a = 8;
6178 #else
6179 *a = 4;
6180 #endif
6181 #else
6182 *a = 8;
6183 #endif
6184 return 8;
6185 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6186 *a = 4;
6187 return 4;
6188 } else if (bt == VT_SHORT) {
6189 *a = 2;
6190 return 2;
6191 } else {
6192 /* char, void, function, _Bool */
6193 *a = 1;
6194 return 1;
6198 /* return the pointed type of t */
6199 static inline CType *pointed_type(CType *type)
6201 return &type->ref->type;
6204 /* modify type so that its it is a pointer to type. */
6205 static void mk_pointer(CType *type)
6207 Sym *s;
6208 s = sym_push(SYM_FIELD, type, 0, -1);
6209 type->t = VT_PTR | (type->t & ~VT_TYPE);
6210 type->ref = s;
6213 /* compare function types. OLD functions match any new functions */
6214 static int is_compatible_func(CType *type1, CType *type2)
6216 Sym *s1, *s2;
6218 s1 = type1->ref;
6219 s2 = type2->ref;
6220 if (!is_compatible_types(&s1->type, &s2->type))
6221 return 0;
6222 /* check func_call */
6223 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6224 return 0;
6225 /* XXX: not complete */
6226 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6227 return 1;
6228 if (s1->c != s2->c)
6229 return 0;
6230 while (s1 != NULL) {
6231 if (s2 == NULL)
6232 return 0;
6233 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6234 return 0;
6235 s1 = s1->next;
6236 s2 = s2->next;
6238 if (s2)
6239 return 0;
6240 return 1;
6243 /* return true if type1 and type2 are the same. If unqualified is
6244 true, qualifiers on the types are ignored.
6246 - enums are not checked as gcc __builtin_types_compatible_p ()
6248 static int compare_types(CType *type1, CType *type2, int unqualified)
6250 int bt1, t1, t2;
6252 t1 = type1->t & VT_TYPE;
6253 t2 = type2->t & VT_TYPE;
6254 if (unqualified) {
6255 /* strip qualifiers before comparing */
6256 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6257 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6259 /* XXX: bitfields ? */
6260 if (t1 != t2)
6261 return 0;
6262 /* test more complicated cases */
6263 bt1 = t1 & VT_BTYPE;
6264 if (bt1 == VT_PTR) {
6265 type1 = pointed_type(type1);
6266 type2 = pointed_type(type2);
6267 return is_compatible_types(type1, type2);
6268 } else if (bt1 == VT_STRUCT) {
6269 return (type1->ref == type2->ref);
6270 } else if (bt1 == VT_FUNC) {
6271 return is_compatible_func(type1, type2);
6272 } else {
6273 return 1;
6277 /* return true if type1 and type2 are exactly the same (including
6278 qualifiers).
6280 static int is_compatible_types(CType *type1, CType *type2)
6282 return compare_types(type1,type2,0);
6285 /* return true if type1 and type2 are the same (ignoring qualifiers).
6287 static int is_compatible_parameter_types(CType *type1, CType *type2)
6289 return compare_types(type1,type2,1);
6292 /* print a type. If 'varstr' is not NULL, then the variable is also
6293 printed in the type */
6294 /* XXX: union */
6295 /* XXX: add array and function pointers */
6296 void type_to_str(char *buf, int buf_size,
6297 CType *type, const char *varstr)
6299 int bt, v, t;
6300 Sym *s, *sa;
6301 char buf1[256];
6302 const char *tstr;
6304 t = type->t & VT_TYPE;
6305 bt = t & VT_BTYPE;
6306 buf[0] = '\0';
6307 if (t & VT_CONSTANT)
6308 pstrcat(buf, buf_size, "const ");
6309 if (t & VT_VOLATILE)
6310 pstrcat(buf, buf_size, "volatile ");
6311 if (t & VT_UNSIGNED)
6312 pstrcat(buf, buf_size, "unsigned ");
6313 switch(bt) {
6314 case VT_VOID:
6315 tstr = "void";
6316 goto add_tstr;
6317 case VT_BOOL:
6318 tstr = "_Bool";
6319 goto add_tstr;
6320 case VT_BYTE:
6321 tstr = "char";
6322 goto add_tstr;
6323 case VT_SHORT:
6324 tstr = "short";
6325 goto add_tstr;
6326 case VT_INT:
6327 tstr = "int";
6328 goto add_tstr;
6329 case VT_LONG:
6330 tstr = "long";
6331 goto add_tstr;
6332 case VT_LLONG:
6333 tstr = "long long";
6334 goto add_tstr;
6335 case VT_FLOAT:
6336 tstr = "float";
6337 goto add_tstr;
6338 case VT_DOUBLE:
6339 tstr = "double";
6340 goto add_tstr;
6341 case VT_LDOUBLE:
6342 tstr = "long double";
6343 add_tstr:
6344 pstrcat(buf, buf_size, tstr);
6345 break;
6346 case VT_ENUM:
6347 case VT_STRUCT:
6348 if (bt == VT_STRUCT)
6349 tstr = "struct ";
6350 else
6351 tstr = "enum ";
6352 pstrcat(buf, buf_size, tstr);
6353 v = type->ref->v & ~SYM_STRUCT;
6354 if (v >= SYM_FIRST_ANOM)
6355 pstrcat(buf, buf_size, "<anonymous>");
6356 else
6357 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6358 break;
6359 case VT_FUNC:
6360 s = type->ref;
6361 type_to_str(buf, buf_size, &s->type, varstr);
6362 pstrcat(buf, buf_size, "(");
6363 sa = s->next;
6364 while (sa != NULL) {
6365 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6366 pstrcat(buf, buf_size, buf1);
6367 sa = sa->next;
6368 if (sa)
6369 pstrcat(buf, buf_size, ", ");
6371 pstrcat(buf, buf_size, ")");
6372 goto no_var;
6373 case VT_PTR:
6374 s = type->ref;
6375 pstrcpy(buf1, sizeof(buf1), "*");
6376 if (varstr)
6377 pstrcat(buf1, sizeof(buf1), varstr);
6378 type_to_str(buf, buf_size, &s->type, buf1);
6379 goto no_var;
6381 if (varstr) {
6382 pstrcat(buf, buf_size, " ");
6383 pstrcat(buf, buf_size, varstr);
6385 no_var: ;
6388 /* verify type compatibility to store vtop in 'dt' type, and generate
6389 casts if needed. */
6390 static void gen_assign_cast(CType *dt)
6392 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6393 char buf1[256], buf2[256];
6394 int dbt, sbt;
6396 st = &vtop->type; /* source type */
6397 dbt = dt->t & VT_BTYPE;
6398 sbt = st->t & VT_BTYPE;
6399 if (dt->t & VT_CONSTANT)
6400 warning("assignment of read-only location");
6401 switch(dbt) {
6402 case VT_PTR:
6403 /* special cases for pointers */
6404 /* '0' can also be a pointer */
6405 if (is_null_pointer(vtop))
6406 goto type_ok;
6407 /* accept implicit pointer to integer cast with warning */
6408 if (is_integer_btype(sbt)) {
6409 warning("assignment makes pointer from integer without a cast");
6410 goto type_ok;
6412 type1 = pointed_type(dt);
6413 /* a function is implicitely a function pointer */
6414 if (sbt == VT_FUNC) {
6415 if ((type1->t & VT_BTYPE) != VT_VOID &&
6416 !is_compatible_types(pointed_type(dt), st))
6417 goto error;
6418 else
6419 goto type_ok;
6421 if (sbt != VT_PTR)
6422 goto error;
6423 type2 = pointed_type(st);
6424 if ((type1->t & VT_BTYPE) == VT_VOID ||
6425 (type2->t & VT_BTYPE) == VT_VOID) {
6426 /* void * can match anything */
6427 } else {
6428 /* exact type match, except for unsigned */
6429 tmp_type1 = *type1;
6430 tmp_type2 = *type2;
6431 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6432 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6433 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6434 warning("assignment from incompatible pointer type");
6436 /* check const and volatile */
6437 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6438 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6439 warning("assignment discards qualifiers from pointer target type");
6440 break;
6441 case VT_BYTE:
6442 case VT_SHORT:
6443 case VT_INT:
6444 case VT_LLONG:
6445 if (sbt == VT_PTR || sbt == VT_FUNC) {
6446 warning("assignment makes integer from pointer without a cast");
6448 /* XXX: more tests */
6449 break;
6450 case VT_STRUCT:
6451 tmp_type1 = *dt;
6452 tmp_type2 = *st;
6453 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6454 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6455 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6456 error:
6457 type_to_str(buf1, sizeof(buf1), st, NULL);
6458 type_to_str(buf2, sizeof(buf2), dt, NULL);
6459 error("cannot cast '%s' to '%s'", buf1, buf2);
6461 break;
6463 type_ok:
6464 gen_cast(dt);
6467 /* store vtop in lvalue pushed on stack */
6468 void vstore(void)
6470 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6472 ft = vtop[-1].type.t;
6473 sbt = vtop->type.t & VT_BTYPE;
6474 dbt = ft & VT_BTYPE;
6475 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6476 (sbt == VT_INT && dbt == VT_SHORT)) {
6477 /* optimize char/short casts */
6478 delayed_cast = VT_MUSTCAST;
6479 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
6480 /* XXX: factorize */
6481 if (ft & VT_CONSTANT)
6482 warning("assignment of read-only location");
6483 } else {
6484 delayed_cast = 0;
6485 if (!(ft & VT_BITFIELD))
6486 gen_assign_cast(&vtop[-1].type);
6489 if (sbt == VT_STRUCT) {
6490 /* if structure, only generate pointer */
6491 /* structure assignment : generate memcpy */
6492 /* XXX: optimize if small size */
6493 if (!nocode_wanted) {
6494 size = type_size(&vtop->type, &align);
6496 #ifdef TCC_ARM_EABI
6497 if(!(align & 7))
6498 vpush_global_sym(&func_old_type, TOK_memcpy8);
6499 else if(!(align & 3))
6500 vpush_global_sym(&func_old_type, TOK_memcpy4);
6501 else
6502 #endif
6503 vpush_global_sym(&func_old_type, TOK_memcpy);
6505 /* destination */
6506 vpushv(vtop - 2);
6507 vtop->type.t = VT_INT;
6508 gaddrof();
6509 /* source */
6510 vpushv(vtop - 2);
6511 vtop->type.t = VT_INT;
6512 gaddrof();
6513 /* type size */
6514 vpushi(size);
6515 gfunc_call(3);
6517 vswap();
6518 vpop();
6519 } else {
6520 vswap();
6521 vpop();
6523 /* leave source on stack */
6524 } else if (ft & VT_BITFIELD) {
6525 /* bitfield store handling */
6526 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6527 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6528 /* remove bit field info to avoid loops */
6529 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6531 /* duplicate source into other register */
6532 gv_dup();
6533 vswap();
6534 vrott(3);
6536 if((ft & VT_BTYPE) == VT_BOOL) {
6537 gen_cast(&vtop[-1].type);
6538 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
6541 /* duplicate destination */
6542 vdup();
6543 vtop[-1] = vtop[-2];
6545 /* mask and shift source */
6546 if((ft & VT_BTYPE) != VT_BOOL) {
6547 vpushi((1 << bit_size) - 1);
6548 gen_op('&');
6550 vpushi(bit_pos);
6551 gen_op(TOK_SHL);
6552 /* load destination, mask and or with source */
6553 vswap();
6554 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6555 gen_op('&');
6556 gen_op('|');
6557 /* store result */
6558 vstore();
6560 /* pop off shifted source from "duplicate source..." above */
6561 vpop();
6563 } else {
6564 #ifdef CONFIG_TCC_BCHECK
6565 /* bound check case */
6566 if (vtop[-1].r & VT_MUSTBOUND) {
6567 vswap();
6568 gbound();
6569 vswap();
6571 #endif
6572 if (!nocode_wanted) {
6573 rc = RC_INT;
6574 if (is_float(ft))
6575 rc = RC_FLOAT;
6576 r = gv(rc); /* generate value */
6577 /* if lvalue was saved on stack, must read it */
6578 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6579 SValue sv;
6580 t = get_reg(RC_INT);
6581 sv.type.t = VT_INT;
6582 sv.r = VT_LOCAL | VT_LVAL;
6583 sv.c.ul = vtop[-1].c.ul;
6584 load(t, &sv);
6585 vtop[-1].r = t | VT_LVAL;
6587 store(r, vtop - 1);
6588 /* two word case handling : store second register at word + 4 */
6589 if ((ft & VT_BTYPE) == VT_LLONG) {
6590 vswap();
6591 /* convert to int to increment easily */
6592 vtop->type.t = VT_INT;
6593 gaddrof();
6594 vpushi(4);
6595 gen_op('+');
6596 vtop->r |= VT_LVAL;
6597 vswap();
6598 /* XXX: it works because r2 is spilled last ! */
6599 store(vtop->r2, vtop - 1);
6602 vswap();
6603 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6604 vtop->r |= delayed_cast;
6608 /* post defines POST/PRE add. c is the token ++ or -- */
6609 void inc(int post, int c)
6611 test_lvalue();
6612 vdup(); /* save lvalue */
6613 if (post) {
6614 gv_dup(); /* duplicate value */
6615 vrotb(3);
6616 vrotb(3);
6618 /* add constant */
6619 vpushi(c - TOK_MID);
6620 gen_op('+');
6621 vstore(); /* store value */
6622 if (post)
6623 vpop(); /* if post op, return saved value */
6626 /* Parse GNUC __attribute__ extension. Currently, the following
6627 extensions are recognized:
6628 - aligned(n) : set data/function alignment.
6629 - packed : force data alignment to 1
6630 - section(x) : generate data/code in this section.
6631 - unused : currently ignored, but may be used someday.
6632 - regparm(n) : pass function parameters in registers (i386 only)
6634 static void parse_attribute(AttributeDef *ad)
6636 int t, n;
6638 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6639 next();
6640 skip('(');
6641 skip('(');
6642 while (tok != ')') {
6643 if (tok < TOK_IDENT)
6644 expect("attribute name");
6645 t = tok;
6646 next();
6647 switch(t) {
6648 case TOK_SECTION1:
6649 case TOK_SECTION2:
6650 skip('(');
6651 if (tok != TOK_STR)
6652 expect("section name");
6653 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6654 next();
6655 skip(')');
6656 break;
6657 case TOK_ALIGNED1:
6658 case TOK_ALIGNED2:
6659 if (tok == '(') {
6660 next();
6661 n = expr_const();
6662 if (n <= 0 || (n & (n - 1)) != 0)
6663 error("alignment must be a positive power of two");
6664 skip(')');
6665 } else {
6666 n = MAX_ALIGN;
6668 ad->aligned = n;
6669 break;
6670 case TOK_PACKED1:
6671 case TOK_PACKED2:
6672 ad->packed = 1;
6673 break;
6674 case TOK_UNUSED1:
6675 case TOK_UNUSED2:
6676 /* currently, no need to handle it because tcc does not
6677 track unused objects */
6678 break;
6679 case TOK_NORETURN1:
6680 case TOK_NORETURN2:
6681 /* currently, no need to handle it because tcc does not
6682 track unused objects */
6683 break;
6684 case TOK_CDECL1:
6685 case TOK_CDECL2:
6686 case TOK_CDECL3:
6687 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6688 break;
6689 case TOK_STDCALL1:
6690 case TOK_STDCALL2:
6691 case TOK_STDCALL3:
6692 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6693 break;
6694 #ifdef TCC_TARGET_I386
6695 case TOK_REGPARM1:
6696 case TOK_REGPARM2:
6697 skip('(');
6698 n = expr_const();
6699 if (n > 3)
6700 n = 3;
6701 else if (n < 0)
6702 n = 0;
6703 if (n > 0)
6704 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6705 skip(')');
6706 break;
6707 case TOK_FASTCALL1:
6708 case TOK_FASTCALL2:
6709 case TOK_FASTCALL3:
6710 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6711 break;
6712 #endif
6713 case TOK_DLLEXPORT:
6714 FUNC_EXPORT(ad->func_attr) = 1;
6715 break;
6716 default:
6717 if (tcc_state->warn_unsupported)
6718 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6719 /* skip parameters */
6720 if (tok == '(') {
6721 int parenthesis = 0;
6722 do {
6723 if (tok == '(')
6724 parenthesis++;
6725 else if (tok == ')')
6726 parenthesis--;
6727 next();
6728 } while (parenthesis && tok != -1);
6730 break;
6732 if (tok != ',')
6733 break;
6734 next();
6736 skip(')');
6737 skip(')');
6741 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6742 static void struct_decl(CType *type, int u)
6744 int a, v, size, align, maxalign, c, offset;
6745 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
6746 Sym *s, *ss, *ass, **ps;
6747 AttributeDef ad;
6748 CType type1, btype;
6750 a = tok; /* save decl type */
6751 next();
6752 if (tok != '{') {
6753 v = tok;
6754 next();
6755 /* struct already defined ? return it */
6756 if (v < TOK_IDENT)
6757 expect("struct/union/enum name");
6758 s = struct_find(v);
6759 if (s) {
6760 if (s->type.t != a)
6761 error("invalid type");
6762 goto do_decl;
6764 } else {
6765 v = anon_sym++;
6767 type1.t = a;
6768 /* we put an undefined size for struct/union */
6769 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6770 s->r = 0; /* default alignment is zero as gcc */
6771 /* put struct/union/enum name in type */
6772 do_decl:
6773 type->t = u;
6774 type->ref = s;
6776 if (tok == '{') {
6777 next();
6778 if (s->c != -1)
6779 error("struct/union/enum already defined");
6780 /* cannot be empty */
6781 c = 0;
6782 /* non empty enums are not allowed */
6783 if (a == TOK_ENUM) {
6784 for(;;) {
6785 v = tok;
6786 if (v < TOK_UIDENT)
6787 expect("identifier");
6788 next();
6789 if (tok == '=') {
6790 next();
6791 c = expr_const();
6793 /* enum symbols have static storage */
6794 ss = sym_push(v, &int_type, VT_CONST, c);
6795 ss->type.t |= VT_STATIC;
6796 if (tok != ',')
6797 break;
6798 next();
6799 c++;
6800 /* NOTE: we accept a trailing comma */
6801 if (tok == '}')
6802 break;
6804 skip('}');
6805 } else {
6806 maxalign = 1;
6807 ps = &s->next;
6808 prevbt = VT_INT;
6809 bit_pos = 0;
6810 offset = 0;
6811 while (tok != '}') {
6812 parse_btype(&btype, &ad);
6813 while (1) {
6814 bit_size = -1;
6815 v = 0;
6816 type1 = btype;
6817 if (tok != ':') {
6818 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6819 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6820 expect("identifier");
6821 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6822 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6823 error("invalid type for '%s'",
6824 get_tok_str(v, NULL));
6826 if (tok == ':') {
6827 next();
6828 bit_size = expr_const();
6829 /* XXX: handle v = 0 case for messages */
6830 if (bit_size < 0)
6831 error("negative width in bit-field '%s'",
6832 get_tok_str(v, NULL));
6833 if (v && bit_size == 0)
6834 error("zero width for bit-field '%s'",
6835 get_tok_str(v, NULL));
6837 size = type_size(&type1, &align);
6838 if (ad.aligned) {
6839 if (align < ad.aligned)
6840 align = ad.aligned;
6841 } else if (ad.packed) {
6842 align = 1;
6843 } else if (*tcc_state->pack_stack_ptr) {
6844 if (align > *tcc_state->pack_stack_ptr)
6845 align = *tcc_state->pack_stack_ptr;
6847 lbit_pos = 0;
6848 if (bit_size >= 0) {
6849 bt = type1.t & VT_BTYPE;
6850 if (bt != VT_INT &&
6851 bt != VT_BYTE &&
6852 bt != VT_SHORT &&
6853 bt != VT_BOOL &&
6854 bt != VT_ENUM)
6855 error("bitfields must have scalar type");
6856 bsize = size * 8;
6857 if (bit_size > bsize) {
6858 error("width of '%s' exceeds its type",
6859 get_tok_str(v, NULL));
6860 } else if (bit_size == bsize) {
6861 /* no need for bit fields */
6862 bit_pos = 0;
6863 } else if (bit_size == 0) {
6864 /* XXX: what to do if only padding in a
6865 structure ? */
6866 /* zero size: means to pad */
6867 bit_pos = 0;
6868 } else {
6869 /* we do not have enough room ?
6870 did the type change?
6871 is it a union? */
6872 if ((bit_pos + bit_size) > bsize ||
6873 bt != prevbt || a == TOK_UNION)
6874 bit_pos = 0;
6875 lbit_pos = bit_pos;
6876 /* XXX: handle LSB first */
6877 type1.t |= VT_BITFIELD |
6878 (bit_pos << VT_STRUCT_SHIFT) |
6879 (bit_size << (VT_STRUCT_SHIFT + 6));
6880 bit_pos += bit_size;
6882 prevbt = bt;
6883 } else {
6884 bit_pos = 0;
6886 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
6887 /* add new memory data only if starting
6888 bit field */
6889 if (lbit_pos == 0) {
6890 if (a == TOK_STRUCT) {
6891 c = (c + align - 1) & -align;
6892 offset = c;
6893 if (size > 0)
6894 c += size;
6895 } else {
6896 offset = 0;
6897 if (size > c)
6898 c = size;
6900 if (align > maxalign)
6901 maxalign = align;
6903 #if 0
6904 printf("add field %s offset=%d",
6905 get_tok_str(v, NULL), offset);
6906 if (type1.t & VT_BITFIELD) {
6907 printf(" pos=%d size=%d",
6908 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6909 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6911 printf("\n");
6912 #endif
6914 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
6915 ass = type1.ref;
6916 while ((ass = ass->next) != NULL) {
6917 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
6918 *ps = ss;
6919 ps = &ss->next;
6921 } else if (v) {
6922 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6923 *ps = ss;
6924 ps = &ss->next;
6926 if (tok == ';' || tok == TOK_EOF)
6927 break;
6928 skip(',');
6930 skip(';');
6932 skip('}');
6933 /* store size and alignment */
6934 s->c = (c + maxalign - 1) & -maxalign;
6935 s->r = maxalign;
6940 /* return 0 if no type declaration. otherwise, return the basic type
6941 and skip it.
6943 static int parse_btype(CType *type, AttributeDef *ad)
6945 int t, u, type_found, typespec_found, typedef_found;
6946 Sym *s;
6947 CType type1;
6949 memset(ad, 0, sizeof(AttributeDef));
6950 type_found = 0;
6951 typespec_found = 0;
6952 typedef_found = 0;
6953 t = 0;
6954 while(1) {
6955 switch(tok) {
6956 case TOK_EXTENSION:
6957 /* currently, we really ignore extension */
6958 next();
6959 continue;
6961 /* basic types */
6962 case TOK_CHAR:
6963 u = VT_BYTE;
6964 basic_type:
6965 next();
6966 basic_type1:
6967 if ((t & VT_BTYPE) != 0)
6968 error("too many basic types");
6969 t |= u;
6970 typespec_found = 1;
6971 break;
6972 case TOK_VOID:
6973 u = VT_VOID;
6974 goto basic_type;
6975 case TOK_SHORT:
6976 u = VT_SHORT;
6977 goto basic_type;
6978 case TOK_INT:
6979 next();
6980 typespec_found = 1;
6981 break;
6982 case TOK_LONG:
6983 next();
6984 if ((t & VT_BTYPE) == VT_DOUBLE) {
6985 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6986 } else if ((t & VT_BTYPE) == VT_LONG) {
6987 t = (t & ~VT_BTYPE) | VT_LLONG;
6988 } else {
6989 u = VT_LONG;
6990 goto basic_type1;
6992 break;
6993 case TOK_BOOL:
6994 u = VT_BOOL;
6995 goto basic_type;
6996 case TOK_FLOAT:
6997 u = VT_FLOAT;
6998 goto basic_type;
6999 case TOK_DOUBLE:
7000 next();
7001 if ((t & VT_BTYPE) == VT_LONG) {
7002 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
7003 } else {
7004 u = VT_DOUBLE;
7005 goto basic_type1;
7007 break;
7008 case TOK_ENUM:
7009 struct_decl(&type1, VT_ENUM);
7010 basic_type2:
7011 u = type1.t;
7012 type->ref = type1.ref;
7013 goto basic_type1;
7014 case TOK_STRUCT:
7015 case TOK_UNION:
7016 struct_decl(&type1, VT_STRUCT);
7017 goto basic_type2;
7019 /* type modifiers */
7020 case TOK_CONST1:
7021 case TOK_CONST2:
7022 case TOK_CONST3:
7023 t |= VT_CONSTANT;
7024 next();
7025 break;
7026 case TOK_VOLATILE1:
7027 case TOK_VOLATILE2:
7028 case TOK_VOLATILE3:
7029 t |= VT_VOLATILE;
7030 next();
7031 break;
7032 case TOK_SIGNED1:
7033 case TOK_SIGNED2:
7034 case TOK_SIGNED3:
7035 typespec_found = 1;
7036 t |= VT_SIGNED;
7037 next();
7038 break;
7039 case TOK_REGISTER:
7040 case TOK_AUTO:
7041 case TOK_RESTRICT1:
7042 case TOK_RESTRICT2:
7043 case TOK_RESTRICT3:
7044 next();
7045 break;
7046 case TOK_UNSIGNED:
7047 t |= VT_UNSIGNED;
7048 next();
7049 typespec_found = 1;
7050 break;
7052 /* storage */
7053 case TOK_EXTERN:
7054 t |= VT_EXTERN;
7055 next();
7056 break;
7057 case TOK_STATIC:
7058 t |= VT_STATIC;
7059 next();
7060 break;
7061 case TOK_TYPEDEF:
7062 t |= VT_TYPEDEF;
7063 next();
7064 break;
7065 case TOK_INLINE1:
7066 case TOK_INLINE2:
7067 case TOK_INLINE3:
7068 t |= VT_INLINE;
7069 next();
7070 break;
7072 /* GNUC attribute */
7073 case TOK_ATTRIBUTE1:
7074 case TOK_ATTRIBUTE2:
7075 parse_attribute(ad);
7076 break;
7077 /* GNUC typeof */
7078 case TOK_TYPEOF1:
7079 case TOK_TYPEOF2:
7080 case TOK_TYPEOF3:
7081 next();
7082 parse_expr_type(&type1);
7083 goto basic_type2;
7084 default:
7085 if (typespec_found || typedef_found)
7086 goto the_end;
7087 s = sym_find(tok);
7088 if (!s || !(s->type.t & VT_TYPEDEF))
7089 goto the_end;
7090 typedef_found = 1;
7091 t |= (s->type.t & ~VT_TYPEDEF);
7092 type->ref = s->type.ref;
7093 next();
7094 typespec_found = 1;
7095 break;
7097 type_found = 1;
7099 the_end:
7100 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7101 error("signed and unsigned modifier");
7102 if (tcc_state->char_is_unsigned) {
7103 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7104 t |= VT_UNSIGNED;
7106 t &= ~VT_SIGNED;
7108 /* long is never used as type */
7109 if ((t & VT_BTYPE) == VT_LONG)
7110 t = (t & ~VT_BTYPE) | VT_INT;
7111 type->t = t;
7112 return type_found;
7115 /* convert a function parameter type (array to pointer and function to
7116 function pointer) */
7117 static inline void convert_parameter_type(CType *pt)
7119 /* remove const and volatile qualifiers (XXX: const could be used
7120 to indicate a const function parameter */
7121 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7122 /* array must be transformed to pointer according to ANSI C */
7123 pt->t &= ~VT_ARRAY;
7124 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7125 mk_pointer(pt);
7129 static void post_type(CType *type, AttributeDef *ad)
7131 int n, l, t1, arg_size, align;
7132 Sym **plast, *s, *first;
7133 AttributeDef ad1;
7134 CType pt;
7136 if (tok == '(') {
7137 /* function declaration */
7138 next();
7139 l = 0;
7140 first = NULL;
7141 plast = &first;
7142 arg_size = 0;
7143 if (tok != ')') {
7144 for(;;) {
7145 /* read param name and compute offset */
7146 if (l != FUNC_OLD) {
7147 if (!parse_btype(&pt, &ad1)) {
7148 if (l) {
7149 error("invalid type");
7150 } else {
7151 l = FUNC_OLD;
7152 goto old_proto;
7155 l = FUNC_NEW;
7156 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7157 break;
7158 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7159 if ((pt.t & VT_BTYPE) == VT_VOID)
7160 error("parameter declared as void");
7161 arg_size += (type_size(&pt, &align) + 3) & ~3;
7162 } else {
7163 old_proto:
7164 n = tok;
7165 if (n < TOK_UIDENT)
7166 expect("identifier");
7167 pt.t = VT_INT;
7168 next();
7170 convert_parameter_type(&pt);
7171 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7172 *plast = s;
7173 plast = &s->next;
7174 if (tok == ')')
7175 break;
7176 skip(',');
7177 if (l == FUNC_NEW && tok == TOK_DOTS) {
7178 l = FUNC_ELLIPSIS;
7179 next();
7180 break;
7184 /* if no parameters, then old type prototype */
7185 if (l == 0)
7186 l = FUNC_OLD;
7187 skip(')');
7188 t1 = type->t & VT_STORAGE;
7189 /* NOTE: const is ignored in returned type as it has a special
7190 meaning in gcc / C++ */
7191 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7192 post_type(type, ad);
7193 /* we push a anonymous symbol which will contain the function prototype */
7194 FUNC_ARGS(ad->func_attr) = arg_size;
7195 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7196 s->next = first;
7197 type->t = t1 | VT_FUNC;
7198 type->ref = s;
7199 } else if (tok == '[') {
7200 /* array definition */
7201 next();
7202 n = -1;
7203 if (tok != ']') {
7204 n = expr_const();
7205 if (n < 0)
7206 error("invalid array size");
7208 skip(']');
7209 /* parse next post type */
7210 t1 = type->t & VT_STORAGE;
7211 type->t &= ~VT_STORAGE;
7212 post_type(type, ad);
7214 /* we push a anonymous symbol which will contain the array
7215 element type */
7216 s = sym_push(SYM_FIELD, type, 0, n);
7217 type->t = t1 | VT_ARRAY | VT_PTR;
7218 type->ref = s;
7222 /* Parse a type declaration (except basic type), and return the type
7223 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7224 expected. 'type' should contain the basic type. 'ad' is the
7225 attribute definition of the basic type. It can be modified by
7226 type_decl().
7228 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7230 Sym *s;
7231 CType type1, *type2;
7232 int qualifiers;
7234 while (tok == '*') {
7235 qualifiers = 0;
7236 redo:
7237 next();
7238 switch(tok) {
7239 case TOK_CONST1:
7240 case TOK_CONST2:
7241 case TOK_CONST3:
7242 qualifiers |= VT_CONSTANT;
7243 goto redo;
7244 case TOK_VOLATILE1:
7245 case TOK_VOLATILE2:
7246 case TOK_VOLATILE3:
7247 qualifiers |= VT_VOLATILE;
7248 goto redo;
7249 case TOK_RESTRICT1:
7250 case TOK_RESTRICT2:
7251 case TOK_RESTRICT3:
7252 goto redo;
7254 mk_pointer(type);
7255 type->t |= qualifiers;
7258 /* XXX: clarify attribute handling */
7259 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7260 parse_attribute(ad);
7262 /* recursive type */
7263 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7264 type1.t = 0; /* XXX: same as int */
7265 if (tok == '(') {
7266 next();
7267 /* XXX: this is not correct to modify 'ad' at this point, but
7268 the syntax is not clear */
7269 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7270 parse_attribute(ad);
7271 type_decl(&type1, ad, v, td);
7272 skip(')');
7273 } else {
7274 /* type identifier */
7275 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7276 *v = tok;
7277 next();
7278 } else {
7279 if (!(td & TYPE_ABSTRACT))
7280 expect("identifier");
7281 *v = 0;
7284 post_type(type, ad);
7285 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7286 parse_attribute(ad);
7287 if (!type1.t)
7288 return;
7289 /* append type at the end of type1 */
7290 type2 = &type1;
7291 for(;;) {
7292 s = type2->ref;
7293 type2 = &s->type;
7294 if (!type2->t) {
7295 *type2 = *type;
7296 break;
7299 *type = type1;
7302 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7303 static int lvalue_type(int t)
7305 int bt, r;
7306 r = VT_LVAL;
7307 bt = t & VT_BTYPE;
7308 if (bt == VT_BYTE || bt == VT_BOOL)
7309 r |= VT_LVAL_BYTE;
7310 else if (bt == VT_SHORT)
7311 r |= VT_LVAL_SHORT;
7312 else
7313 return r;
7314 if (t & VT_UNSIGNED)
7315 r |= VT_LVAL_UNSIGNED;
7316 return r;
7319 /* indirection with full error checking and bound check */
7320 static void indir(void)
7322 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7323 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7324 return;
7325 expect("pointer");
7327 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7328 gv(RC_INT);
7329 vtop->type = *pointed_type(&vtop->type);
7330 /* Arrays and functions are never lvalues */
7331 if (!(vtop->type.t & VT_ARRAY)
7332 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7333 vtop->r |= lvalue_type(vtop->type.t);
7334 /* if bound checking, the referenced pointer must be checked */
7335 if (do_bounds_check)
7336 vtop->r |= VT_MUSTBOUND;
7340 /* pass a parameter to a function and do type checking and casting */
7341 static void gfunc_param_typed(Sym *func, Sym *arg)
7343 int func_type;
7344 CType type;
7346 func_type = func->c;
7347 if (func_type == FUNC_OLD ||
7348 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7349 /* default casting : only need to convert float to double */
7350 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7351 type.t = VT_DOUBLE;
7352 gen_cast(&type);
7354 } else if (arg == NULL) {
7355 error("too many arguments to function");
7356 } else {
7357 type = arg->type;
7358 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7359 gen_assign_cast(&type);
7363 /* parse an expression of the form '(type)' or '(expr)' and return its
7364 type */
7365 static void parse_expr_type(CType *type)
7367 int n;
7368 AttributeDef ad;
7370 skip('(');
7371 if (parse_btype(type, &ad)) {
7372 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7373 } else {
7374 expr_type(type);
7376 skip(')');
7379 static void parse_type(CType *type)
7381 AttributeDef ad;
7382 int n;
7384 if (!parse_btype(type, &ad)) {
7385 expect("type");
7387 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7390 static void vpush_tokc(int t)
7392 CType type;
7393 type.t = t;
7394 vsetc(&type, VT_CONST, &tokc);
7397 static void unary(void)
7399 int n, t, align, size, r;
7400 CType type;
7401 Sym *s;
7402 AttributeDef ad;
7404 /* XXX: GCC 2.95.3 does not generate a table although it should be
7405 better here */
7406 tok_next:
7407 switch(tok) {
7408 case TOK_EXTENSION:
7409 next();
7410 goto tok_next;
7411 case TOK_CINT:
7412 case TOK_CCHAR:
7413 case TOK_LCHAR:
7414 vpushi(tokc.i);
7415 next();
7416 break;
7417 case TOK_CUINT:
7418 vpush_tokc(VT_INT | VT_UNSIGNED);
7419 next();
7420 break;
7421 case TOK_CLLONG:
7422 vpush_tokc(VT_LLONG);
7423 next();
7424 break;
7425 case TOK_CULLONG:
7426 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7427 next();
7428 break;
7429 case TOK_CFLOAT:
7430 vpush_tokc(VT_FLOAT);
7431 next();
7432 break;
7433 case TOK_CDOUBLE:
7434 vpush_tokc(VT_DOUBLE);
7435 next();
7436 break;
7437 case TOK_CLDOUBLE:
7438 vpush_tokc(VT_LDOUBLE);
7439 next();
7440 break;
7441 case TOK___FUNCTION__:
7442 if (!gnu_ext)
7443 goto tok_identifier;
7444 /* fall thru */
7445 case TOK___FUNC__:
7447 void *ptr;
7448 int len;
7449 /* special function name identifier */
7450 len = strlen(funcname) + 1;
7451 /* generate char[len] type */
7452 type.t = VT_BYTE;
7453 mk_pointer(&type);
7454 type.t |= VT_ARRAY;
7455 type.ref->c = len;
7456 vpush_ref(&type, data_section, data_section->data_offset, len);
7457 ptr = section_ptr_add(data_section, len);
7458 memcpy(ptr, funcname, len);
7459 next();
7461 break;
7462 case TOK_LSTR:
7463 #ifdef TCC_TARGET_PE
7464 t = VT_SHORT | VT_UNSIGNED;
7465 #else
7466 t = VT_INT;
7467 #endif
7468 goto str_init;
7469 case TOK_STR:
7470 /* string parsing */
7471 t = VT_BYTE;
7472 str_init:
7473 if (tcc_state->warn_write_strings)
7474 t |= VT_CONSTANT;
7475 type.t = t;
7476 mk_pointer(&type);
7477 type.t |= VT_ARRAY;
7478 memset(&ad, 0, sizeof(AttributeDef));
7479 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7480 break;
7481 case '(':
7482 next();
7483 /* cast ? */
7484 if (parse_btype(&type, &ad)) {
7485 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7486 skip(')');
7487 /* check ISOC99 compound literal */
7488 if (tok == '{') {
7489 /* data is allocated locally by default */
7490 if (global_expr)
7491 r = VT_CONST;
7492 else
7493 r = VT_LOCAL;
7494 /* all except arrays are lvalues */
7495 if (!(type.t & VT_ARRAY))
7496 r |= lvalue_type(type.t);
7497 memset(&ad, 0, sizeof(AttributeDef));
7498 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7499 } else {
7500 unary();
7501 gen_cast(&type);
7503 } else if (tok == '{') {
7504 /* save all registers */
7505 save_regs(0);
7506 /* statement expression : we do not accept break/continue
7507 inside as GCC does */
7508 block(NULL, NULL, NULL, NULL, 0, 1);
7509 skip(')');
7510 } else {
7511 gexpr();
7512 skip(')');
7514 break;
7515 case '*':
7516 next();
7517 unary();
7518 indir();
7519 break;
7520 case '&':
7521 next();
7522 unary();
7523 /* functions names must be treated as function pointers,
7524 except for unary '&' and sizeof. Since we consider that
7525 functions are not lvalues, we only have to handle it
7526 there and in function calls. */
7527 /* arrays can also be used although they are not lvalues */
7528 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7529 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7530 test_lvalue();
7531 mk_pointer(&vtop->type);
7532 gaddrof();
7533 break;
7534 case '!':
7535 next();
7536 unary();
7537 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
7538 CType boolean;
7539 boolean.t = VT_BOOL;
7540 gen_cast(&boolean);
7541 vtop->c.i = !vtop->c.i;
7542 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
7543 vtop->c.i = vtop->c.i ^ 1;
7544 else {
7545 save_regs(1);
7546 vseti(VT_JMP, gtst(1, 0));
7548 break;
7549 case '~':
7550 next();
7551 unary();
7552 vpushi(-1);
7553 gen_op('^');
7554 break;
7555 case '+':
7556 next();
7557 /* in order to force cast, we add zero */
7558 unary();
7559 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7560 error("pointer not accepted for unary plus");
7561 vpushi(0);
7562 gen_op('+');
7563 break;
7564 case TOK_SIZEOF:
7565 case TOK_ALIGNOF1:
7566 case TOK_ALIGNOF2:
7567 t = tok;
7568 next();
7569 if (tok == '(') {
7570 parse_expr_type(&type);
7571 } else {
7572 unary_type(&type);
7574 size = type_size(&type, &align);
7575 if (t == TOK_SIZEOF) {
7576 if (size < 0)
7577 error("sizeof applied to an incomplete type");
7578 vpushi(size);
7579 } else {
7580 vpushi(align);
7582 vtop->type.t |= VT_UNSIGNED;
7583 break;
7585 case TOK_builtin_types_compatible_p:
7587 CType type1, type2;
7588 next();
7589 skip('(');
7590 parse_type(&type1);
7591 skip(',');
7592 parse_type(&type2);
7593 skip(')');
7594 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7595 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7596 vpushi(is_compatible_types(&type1, &type2));
7598 break;
7599 case TOK_builtin_constant_p:
7601 int saved_nocode_wanted, res;
7602 next();
7603 skip('(');
7604 saved_nocode_wanted = nocode_wanted;
7605 nocode_wanted = 1;
7606 gexpr();
7607 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7608 vpop();
7609 nocode_wanted = saved_nocode_wanted;
7610 skip(')');
7611 vpushi(res);
7613 break;
7614 case TOK_builtin_frame_address:
7616 CType type;
7617 next();
7618 skip('(');
7619 if (tok != TOK_CINT) {
7620 error("__builtin_frame_address only takes integers");
7622 if (tokc.i != 0) {
7623 error("TCC only supports __builtin_frame_address(0)");
7625 next();
7626 skip(')');
7627 type.t = VT_VOID;
7628 mk_pointer(&type);
7629 vset(&type, VT_LOCAL, 0);
7631 break;
7632 case TOK_INC:
7633 case TOK_DEC:
7634 t = tok;
7635 next();
7636 unary();
7637 inc(0, t);
7638 break;
7639 case '-':
7640 next();
7641 vpushi(0);
7642 unary();
7643 gen_op('-');
7644 break;
7645 case TOK_LAND:
7646 if (!gnu_ext)
7647 goto tok_identifier;
7648 next();
7649 /* allow to take the address of a label */
7650 if (tok < TOK_UIDENT)
7651 expect("label identifier");
7652 s = label_find(tok);
7653 if (!s) {
7654 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7655 } else {
7656 if (s->r == LABEL_DECLARED)
7657 s->r = LABEL_FORWARD;
7659 if (!s->type.t) {
7660 s->type.t = VT_VOID;
7661 mk_pointer(&s->type);
7662 s->type.t |= VT_STATIC;
7664 vset(&s->type, VT_CONST | VT_SYM, 0);
7665 vtop->sym = s;
7666 next();
7667 break;
7668 default:
7669 tok_identifier:
7670 t = tok;
7671 next();
7672 if (t < TOK_UIDENT)
7673 expect("identifier");
7674 s = sym_find(t);
7675 if (!s) {
7676 if (tok != '(')
7677 error("'%s' undeclared", get_tok_str(t, NULL));
7678 /* for simple function calls, we tolerate undeclared
7679 external reference to int() function */
7680 if (tcc_state->warn_implicit_function_declaration)
7681 warning("implicit declaration of function '%s'",
7682 get_tok_str(t, NULL));
7683 s = external_global_sym(t, &func_old_type, 0);
7685 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7686 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7687 /* if referencing an inline function, then we generate a
7688 symbol to it if not already done. It will have the
7689 effect to generate code for it at the end of the
7690 compilation unit. Inline function as always
7691 generated in the text section. */
7692 if (!s->c)
7693 put_extern_sym(s, text_section, 0, 0);
7694 r = VT_SYM | VT_CONST;
7695 } else {
7696 r = s->r;
7698 vset(&s->type, r, s->c);
7699 /* if forward reference, we must point to s */
7700 if (vtop->r & VT_SYM) {
7701 vtop->sym = s;
7702 vtop->c.ul = 0;
7704 break;
7707 /* post operations */
7708 while (1) {
7709 if (tok == TOK_INC || tok == TOK_DEC) {
7710 inc(1, tok);
7711 next();
7712 } else if (tok == '.' || tok == TOK_ARROW) {
7713 /* field */
7714 if (tok == TOK_ARROW)
7715 indir();
7716 test_lvalue();
7717 gaddrof();
7718 next();
7719 /* expect pointer on structure */
7720 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7721 expect("struct or union");
7722 s = vtop->type.ref;
7723 /* find field */
7724 tok |= SYM_FIELD;
7725 while ((s = s->next) != NULL) {
7726 if (s->v == tok)
7727 break;
7729 if (!s)
7730 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7731 /* add field offset to pointer */
7732 vtop->type = char_pointer_type; /* change type to 'char *' */
7733 vpushi(s->c);
7734 gen_op('+');
7735 /* change type to field type, and set to lvalue */
7736 vtop->type = s->type;
7737 /* an array is never an lvalue */
7738 if (!(vtop->type.t & VT_ARRAY)) {
7739 vtop->r |= lvalue_type(vtop->type.t);
7740 /* if bound checking, the referenced pointer must be checked */
7741 if (do_bounds_check)
7742 vtop->r |= VT_MUSTBOUND;
7744 next();
7745 } else if (tok == '[') {
7746 next();
7747 gexpr();
7748 gen_op('+');
7749 indir();
7750 skip(']');
7751 } else if (tok == '(') {
7752 SValue ret;
7753 Sym *sa;
7754 int nb_args;
7756 /* function call */
7757 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7758 /* pointer test (no array accepted) */
7759 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7760 vtop->type = *pointed_type(&vtop->type);
7761 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7762 goto error_func;
7763 } else {
7764 error_func:
7765 expect("function pointer");
7767 } else {
7768 vtop->r &= ~VT_LVAL; /* no lvalue */
7770 /* get return type */
7771 s = vtop->type.ref;
7772 next();
7773 sa = s->next; /* first parameter */
7774 nb_args = 0;
7775 ret.r2 = VT_CONST;
7776 /* compute first implicit argument if a structure is returned */
7777 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7778 /* get some space for the returned structure */
7779 size = type_size(&s->type, &align);
7780 loc = (loc - size) & -align;
7781 ret.type = s->type;
7782 ret.r = VT_LOCAL | VT_LVAL;
7783 /* pass it as 'int' to avoid structure arg passing
7784 problems */
7785 vseti(VT_LOCAL, loc);
7786 ret.c = vtop->c;
7787 nb_args++;
7788 } else {
7789 ret.type = s->type;
7790 /* return in register */
7791 if (is_float(ret.type.t)) {
7792 ret.r = REG_FRET;
7793 } else {
7794 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7795 ret.r2 = REG_LRET;
7796 ret.r = REG_IRET;
7798 ret.c.i = 0;
7800 if (tok != ')') {
7801 for(;;) {
7802 expr_eq();
7803 gfunc_param_typed(s, sa);
7804 nb_args++;
7805 if (sa)
7806 sa = sa->next;
7807 if (tok == ')')
7808 break;
7809 skip(',');
7812 if (sa)
7813 error("too few arguments to function");
7814 skip(')');
7815 if (!nocode_wanted) {
7816 gfunc_call(nb_args);
7817 } else {
7818 vtop -= (nb_args + 1);
7820 /* return value */
7821 vsetc(&ret.type, ret.r, &ret.c);
7822 vtop->r2 = ret.r2;
7823 } else {
7824 break;
7829 static void uneq(void)
7831 int t;
7833 unary();
7834 if (tok == '=' ||
7835 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7836 tok == TOK_A_XOR || tok == TOK_A_OR ||
7837 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7838 test_lvalue();
7839 t = tok;
7840 next();
7841 if (t == '=') {
7842 expr_eq();
7843 } else {
7844 vdup();
7845 expr_eq();
7846 gen_op(t & 0x7f);
7848 vstore();
7852 static void expr_prod(void)
7854 int t;
7856 uneq();
7857 while (tok == '*' || tok == '/' || tok == '%') {
7858 t = tok;
7859 next();
7860 uneq();
7861 gen_op(t);
7865 static void expr_sum(void)
7867 int t;
7869 expr_prod();
7870 while (tok == '+' || tok == '-') {
7871 t = tok;
7872 next();
7873 expr_prod();
7874 gen_op(t);
7878 static void expr_shift(void)
7880 int t;
7882 expr_sum();
7883 while (tok == TOK_SHL || tok == TOK_SAR) {
7884 t = tok;
7885 next();
7886 expr_sum();
7887 gen_op(t);
7891 static void expr_cmp(void)
7893 int t;
7895 expr_shift();
7896 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7897 tok == TOK_ULT || tok == TOK_UGE) {
7898 t = tok;
7899 next();
7900 expr_shift();
7901 gen_op(t);
7905 static void expr_cmpeq(void)
7907 int t;
7909 expr_cmp();
7910 while (tok == TOK_EQ || tok == TOK_NE) {
7911 t = tok;
7912 next();
7913 expr_cmp();
7914 gen_op(t);
7918 static void expr_and(void)
7920 expr_cmpeq();
7921 while (tok == '&') {
7922 next();
7923 expr_cmpeq();
7924 gen_op('&');
7928 static void expr_xor(void)
7930 expr_and();
7931 while (tok == '^') {
7932 next();
7933 expr_and();
7934 gen_op('^');
7938 static void expr_or(void)
7940 expr_xor();
7941 while (tok == '|') {
7942 next();
7943 expr_xor();
7944 gen_op('|');
7948 /* XXX: fix this mess */
7949 static void expr_land_const(void)
7951 expr_or();
7952 while (tok == TOK_LAND) {
7953 next();
7954 expr_or();
7955 gen_op(TOK_LAND);
7959 /* XXX: fix this mess */
7960 static void expr_lor_const(void)
7962 expr_land_const();
7963 while (tok == TOK_LOR) {
7964 next();
7965 expr_land_const();
7966 gen_op(TOK_LOR);
7970 /* only used if non constant */
7971 static void expr_land(void)
7973 int t;
7975 expr_or();
7976 if (tok == TOK_LAND) {
7977 t = 0;
7978 save_regs(1);
7979 for(;;) {
7980 t = gtst(1, t);
7981 if (tok != TOK_LAND) {
7982 vseti(VT_JMPI, t);
7983 break;
7985 next();
7986 expr_or();
7991 static void expr_lor(void)
7993 int t;
7995 expr_land();
7996 if (tok == TOK_LOR) {
7997 t = 0;
7998 save_regs(1);
7999 for(;;) {
8000 t = gtst(0, t);
8001 if (tok != TOK_LOR) {
8002 vseti(VT_JMP, t);
8003 break;
8005 next();
8006 expr_land();
8011 /* XXX: better constant handling */
8012 static void expr_eq(void)
8014 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
8015 SValue sv;
8016 CType type, type1, type2;
8018 if (const_wanted) {
8019 expr_lor_const();
8020 if (tok == '?') {
8021 CType boolean;
8022 int c;
8023 boolean.t = VT_BOOL;
8024 vdup();
8025 gen_cast(&boolean);
8026 c = vtop->c.i;
8027 vpop();
8028 next();
8029 if (tok != ':' || !gnu_ext) {
8030 vpop();
8031 gexpr();
8033 if (!c)
8034 vpop();
8035 skip(':');
8036 expr_eq();
8037 if (c)
8038 vpop();
8040 } else {
8041 expr_lor();
8042 if (tok == '?') {
8043 next();
8044 if (vtop != vstack) {
8045 /* needed to avoid having different registers saved in
8046 each branch */
8047 if (is_float(vtop->type.t))
8048 rc = RC_FLOAT;
8049 else
8050 rc = RC_INT;
8051 gv(rc);
8052 save_regs(1);
8054 if (tok == ':' && gnu_ext) {
8055 gv_dup();
8056 tt = gtst(1, 0);
8057 } else {
8058 tt = gtst(1, 0);
8059 gexpr();
8061 type1 = vtop->type;
8062 sv = *vtop; /* save value to handle it later */
8063 vtop--; /* no vpop so that FP stack is not flushed */
8064 skip(':');
8065 u = gjmp(0);
8066 gsym(tt);
8067 expr_eq();
8068 type2 = vtop->type;
8070 t1 = type1.t;
8071 bt1 = t1 & VT_BTYPE;
8072 t2 = type2.t;
8073 bt2 = t2 & VT_BTYPE;
8074 /* cast operands to correct type according to ISOC rules */
8075 if (is_float(bt1) || is_float(bt2)) {
8076 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8077 type.t = VT_LDOUBLE;
8078 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8079 type.t = VT_DOUBLE;
8080 } else {
8081 type.t = VT_FLOAT;
8083 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8084 /* cast to biggest op */
8085 type.t = VT_LLONG;
8086 /* convert to unsigned if it does not fit in a long long */
8087 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8088 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8089 type.t |= VT_UNSIGNED;
8090 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8091 /* XXX: test pointer compatibility */
8092 type = type1;
8093 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8094 /* XXX: test function pointer compatibility */
8095 type = type1;
8096 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8097 /* XXX: test structure compatibility */
8098 type = type1;
8099 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8100 /* NOTE: as an extension, we accept void on only one side */
8101 type.t = VT_VOID;
8102 } else {
8103 /* integer operations */
8104 type.t = VT_INT;
8105 /* convert to unsigned if it does not fit in an integer */
8106 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8107 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8108 type.t |= VT_UNSIGNED;
8111 /* now we convert second operand */
8112 gen_cast(&type);
8113 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8114 gaddrof();
8115 rc = RC_INT;
8116 if (is_float(type.t)) {
8117 rc = RC_FLOAT;
8118 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8119 /* for long longs, we use fixed registers to avoid having
8120 to handle a complicated move */
8121 rc = RC_IRET;
8124 r2 = gv(rc);
8125 /* this is horrible, but we must also convert first
8126 operand */
8127 tt = gjmp(0);
8128 gsym(u);
8129 /* put again first value and cast it */
8130 *vtop = sv;
8131 gen_cast(&type);
8132 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8133 gaddrof();
8134 r1 = gv(rc);
8135 move_reg(r2, r1);
8136 vtop->r = r2;
8137 gsym(tt);
8142 static void gexpr(void)
8144 while (1) {
8145 expr_eq();
8146 if (tok != ',')
8147 break;
8148 vpop();
8149 next();
8153 /* parse an expression and return its type without any side effect. */
8154 static void expr_type(CType *type)
8156 int saved_nocode_wanted;
8158 saved_nocode_wanted = nocode_wanted;
8159 nocode_wanted = 1;
8160 gexpr();
8161 *type = vtop->type;
8162 vpop();
8163 nocode_wanted = saved_nocode_wanted;
8166 /* parse a unary expression and return its type without any side
8167 effect. */
8168 static void unary_type(CType *type)
8170 int a;
8172 a = nocode_wanted;
8173 nocode_wanted = 1;
8174 unary();
8175 *type = vtop->type;
8176 vpop();
8177 nocode_wanted = a;
8180 /* parse a constant expression and return value in vtop. */
8181 static void expr_const1(void)
8183 int a;
8184 a = const_wanted;
8185 const_wanted = 1;
8186 expr_eq();
8187 const_wanted = a;
8190 /* parse an integer constant and return its value. */
8191 static int expr_const(void)
8193 int c;
8194 expr_const1();
8195 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8196 expect("constant expression");
8197 c = vtop->c.i;
8198 vpop();
8199 return c;
8202 /* return the label token if current token is a label, otherwise
8203 return zero */
8204 static int is_label(void)
8206 int last_tok;
8208 /* fast test first */
8209 if (tok < TOK_UIDENT)
8210 return 0;
8211 /* no need to save tokc because tok is an identifier */
8212 last_tok = tok;
8213 next();
8214 if (tok == ':') {
8215 next();
8216 return last_tok;
8217 } else {
8218 unget_tok(last_tok);
8219 return 0;
8223 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8224 int case_reg, int is_expr)
8226 int a, b, c, d;
8227 Sym *s;
8229 /* generate line number info */
8230 if (do_debug &&
8231 (last_line_num != file->line_num || last_ind != ind)) {
8232 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8233 last_ind = ind;
8234 last_line_num = file->line_num;
8237 if (is_expr) {
8238 /* default return value is (void) */
8239 vpushi(0);
8240 vtop->type.t = VT_VOID;
8243 if (tok == TOK_IF) {
8244 /* if test */
8245 next();
8246 skip('(');
8247 gexpr();
8248 skip(')');
8249 a = gtst(1, 0);
8250 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8251 c = tok;
8252 if (c == TOK_ELSE) {
8253 next();
8254 d = gjmp(0);
8255 gsym(a);
8256 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8257 gsym(d); /* patch else jmp */
8258 } else
8259 gsym(a);
8260 } else if (tok == TOK_WHILE) {
8261 next();
8262 d = ind;
8263 skip('(');
8264 gexpr();
8265 skip(')');
8266 a = gtst(1, 0);
8267 b = 0;
8268 block(&a, &b, case_sym, def_sym, case_reg, 0);
8269 gjmp_addr(d);
8270 gsym(a);
8271 gsym_addr(b, d);
8272 } else if (tok == '{') {
8273 Sym *llabel;
8275 next();
8276 /* record local declaration stack position */
8277 s = local_stack;
8278 llabel = local_label_stack;
8279 /* handle local labels declarations */
8280 if (tok == TOK_LABEL) {
8281 next();
8282 for(;;) {
8283 if (tok < TOK_UIDENT)
8284 expect("label identifier");
8285 label_push(&local_label_stack, tok, LABEL_DECLARED);
8286 next();
8287 if (tok == ',') {
8288 next();
8289 } else {
8290 skip(';');
8291 break;
8295 while (tok != '}') {
8296 decl(VT_LOCAL);
8297 if (tok != '}') {
8298 if (is_expr)
8299 vpop();
8300 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8303 /* pop locally defined labels */
8304 label_pop(&local_label_stack, llabel);
8305 /* pop locally defined symbols */
8306 if(is_expr) {
8307 /* XXX: this solution makes only valgrind happy...
8308 triggered by gcc.c-torture/execute/20000917-1.c */
8309 Sym *p;
8310 switch(vtop->type.t & VT_BTYPE) {
8311 case VT_PTR:
8312 case VT_STRUCT:
8313 case VT_ENUM:
8314 case VT_FUNC:
8315 for(p=vtop->type.ref;p;p=p->prev)
8316 if(p->prev==s)
8317 error("unsupported expression type");
8320 sym_pop(&local_stack, s);
8321 next();
8322 } else if (tok == TOK_RETURN) {
8323 next();
8324 if (tok != ';') {
8325 gexpr();
8326 gen_assign_cast(&func_vt);
8327 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8328 CType type;
8329 /* if returning structure, must copy it to implicit
8330 first pointer arg location */
8331 #ifdef TCC_ARM_EABI
8332 int align, size;
8333 size = type_size(&func_vt,&align);
8334 if(size <= 4)
8336 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8337 && (align & 3))
8339 int addr;
8340 loc = (loc - size) & -4;
8341 addr = loc;
8342 type = func_vt;
8343 vset(&type, VT_LOCAL | VT_LVAL, addr);
8344 vswap();
8345 vstore();
8346 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8348 vtop->type = int_type;
8349 gv(RC_IRET);
8350 } else {
8351 #endif
8352 type = func_vt;
8353 mk_pointer(&type);
8354 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8355 indir();
8356 vswap();
8357 /* copy structure value to pointer */
8358 vstore();
8359 #ifdef TCC_ARM_EABI
8361 #endif
8362 } else if (is_float(func_vt.t)) {
8363 gv(RC_FRET);
8364 } else {
8365 gv(RC_IRET);
8367 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8369 skip(';');
8370 rsym = gjmp(rsym); /* jmp */
8371 } else if (tok == TOK_BREAK) {
8372 /* compute jump */
8373 if (!bsym)
8374 error("cannot break");
8375 *bsym = gjmp(*bsym);
8376 next();
8377 skip(';');
8378 } else if (tok == TOK_CONTINUE) {
8379 /* compute jump */
8380 if (!csym)
8381 error("cannot continue");
8382 *csym = gjmp(*csym);
8383 next();
8384 skip(';');
8385 } else if (tok == TOK_FOR) {
8386 int e;
8387 next();
8388 skip('(');
8389 if (tok != ';') {
8390 gexpr();
8391 vpop();
8393 skip(';');
8394 d = ind;
8395 c = ind;
8396 a = 0;
8397 b = 0;
8398 if (tok != ';') {
8399 gexpr();
8400 a = gtst(1, 0);
8402 skip(';');
8403 if (tok != ')') {
8404 e = gjmp(0);
8405 c = ind;
8406 gexpr();
8407 vpop();
8408 gjmp_addr(d);
8409 gsym(e);
8411 skip(')');
8412 block(&a, &b, case_sym, def_sym, case_reg, 0);
8413 gjmp_addr(c);
8414 gsym(a);
8415 gsym_addr(b, c);
8416 } else
8417 if (tok == TOK_DO) {
8418 next();
8419 a = 0;
8420 b = 0;
8421 d = ind;
8422 block(&a, &b, case_sym, def_sym, case_reg, 0);
8423 skip(TOK_WHILE);
8424 skip('(');
8425 gsym(b);
8426 gexpr();
8427 c = gtst(0, 0);
8428 gsym_addr(c, d);
8429 skip(')');
8430 gsym(a);
8431 skip(';');
8432 } else
8433 if (tok == TOK_SWITCH) {
8434 next();
8435 skip('(');
8436 gexpr();
8437 /* XXX: other types than integer */
8438 case_reg = gv(RC_INT);
8439 vpop();
8440 skip(')');
8441 a = 0;
8442 b = gjmp(0); /* jump to first case */
8443 c = 0;
8444 block(&a, csym, &b, &c, case_reg, 0);
8445 /* if no default, jmp after switch */
8446 if (c == 0)
8447 c = ind;
8448 /* default label */
8449 gsym_addr(b, c);
8450 /* break label */
8451 gsym(a);
8452 } else
8453 if (tok == TOK_CASE) {
8454 int v1, v2;
8455 if (!case_sym)
8456 expect("switch");
8457 next();
8458 v1 = expr_const();
8459 v2 = v1;
8460 if (gnu_ext && tok == TOK_DOTS) {
8461 next();
8462 v2 = expr_const();
8463 if (v2 < v1)
8464 warning("empty case range");
8466 /* since a case is like a label, we must skip it with a jmp */
8467 b = gjmp(0);
8468 gsym(*case_sym);
8469 vseti(case_reg, 0);
8470 vpushi(v1);
8471 if (v1 == v2) {
8472 gen_op(TOK_EQ);
8473 *case_sym = gtst(1, 0);
8474 } else {
8475 gen_op(TOK_GE);
8476 *case_sym = gtst(1, 0);
8477 vseti(case_reg, 0);
8478 vpushi(v2);
8479 gen_op(TOK_LE);
8480 *case_sym = gtst(1, *case_sym);
8482 gsym(b);
8483 skip(':');
8484 is_expr = 0;
8485 goto block_after_label;
8486 } else
8487 if (tok == TOK_DEFAULT) {
8488 next();
8489 skip(':');
8490 if (!def_sym)
8491 expect("switch");
8492 if (*def_sym)
8493 error("too many 'default'");
8494 *def_sym = ind;
8495 is_expr = 0;
8496 goto block_after_label;
8497 } else
8498 if (tok == TOK_GOTO) {
8499 next();
8500 if (tok == '*' && gnu_ext) {
8501 /* computed goto */
8502 next();
8503 gexpr();
8504 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8505 expect("pointer");
8506 ggoto();
8507 } else if (tok >= TOK_UIDENT) {
8508 s = label_find(tok);
8509 /* put forward definition if needed */
8510 if (!s) {
8511 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8512 } else {
8513 if (s->r == LABEL_DECLARED)
8514 s->r = LABEL_FORWARD;
8516 /* label already defined */
8517 if (s->r & LABEL_FORWARD)
8518 s->next = (void *)gjmp((long)s->next);
8519 else
8520 gjmp_addr((long)s->next);
8521 next();
8522 } else {
8523 expect("label identifier");
8525 skip(';');
8526 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8527 asm_instr();
8528 } else {
8529 b = is_label();
8530 if (b) {
8531 /* label case */
8532 s = label_find(b);
8533 if (s) {
8534 if (s->r == LABEL_DEFINED)
8535 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8536 gsym((long)s->next);
8537 s->r = LABEL_DEFINED;
8538 } else {
8539 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8541 s->next = (void *)ind;
8542 /* we accept this, but it is a mistake */
8543 block_after_label:
8544 if (tok == '}') {
8545 warning("deprecated use of label at end of compound statement");
8546 } else {
8547 if (is_expr)
8548 vpop();
8549 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8551 } else {
8552 /* expression case */
8553 if (tok != ';') {
8554 if (is_expr) {
8555 vpop();
8556 gexpr();
8557 } else {
8558 gexpr();
8559 vpop();
8562 skip(';');
8567 /* t is the array or struct type. c is the array or struct
8568 address. cur_index/cur_field is the pointer to the current
8569 value. 'size_only' is true if only size info is needed (only used
8570 in arrays) */
8571 static void decl_designator(CType *type, Section *sec, unsigned long c,
8572 int *cur_index, Sym **cur_field,
8573 int size_only)
8575 Sym *s, *f;
8576 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8577 CType type1;
8579 notfirst = 0;
8580 elem_size = 0;
8581 nb_elems = 1;
8582 if (gnu_ext && (l = is_label()) != 0)
8583 goto struct_field;
8584 while (tok == '[' || tok == '.') {
8585 if (tok == '[') {
8586 if (!(type->t & VT_ARRAY))
8587 expect("array type");
8588 s = type->ref;
8589 next();
8590 index = expr_const();
8591 if (index < 0 || (s->c >= 0 && index >= s->c))
8592 expect("invalid index");
8593 if (tok == TOK_DOTS && gnu_ext) {
8594 next();
8595 index_last = expr_const();
8596 if (index_last < 0 ||
8597 (s->c >= 0 && index_last >= s->c) ||
8598 index_last < index)
8599 expect("invalid index");
8600 } else {
8601 index_last = index;
8603 skip(']');
8604 if (!notfirst)
8605 *cur_index = index_last;
8606 type = pointed_type(type);
8607 elem_size = type_size(type, &align);
8608 c += index * elem_size;
8609 /* NOTE: we only support ranges for last designator */
8610 nb_elems = index_last - index + 1;
8611 if (nb_elems != 1) {
8612 notfirst = 1;
8613 break;
8615 } else {
8616 next();
8617 l = tok;
8618 next();
8619 struct_field:
8620 if ((type->t & VT_BTYPE) != VT_STRUCT)
8621 expect("struct/union type");
8622 s = type->ref;
8623 l |= SYM_FIELD;
8624 f = s->next;
8625 while (f) {
8626 if (f->v == l)
8627 break;
8628 f = f->next;
8630 if (!f)
8631 expect("field");
8632 if (!notfirst)
8633 *cur_field = f;
8634 /* XXX: fix this mess by using explicit storage field */
8635 type1 = f->type;
8636 type1.t |= (type->t & ~VT_TYPE);
8637 type = &type1;
8638 c += f->c;
8640 notfirst = 1;
8642 if (notfirst) {
8643 if (tok == '=') {
8644 next();
8645 } else {
8646 if (!gnu_ext)
8647 expect("=");
8649 } else {
8650 if (type->t & VT_ARRAY) {
8651 index = *cur_index;
8652 type = pointed_type(type);
8653 c += index * type_size(type, &align);
8654 } else {
8655 f = *cur_field;
8656 if (!f)
8657 error("too many field init");
8658 /* XXX: fix this mess by using explicit storage field */
8659 type1 = f->type;
8660 type1.t |= (type->t & ~VT_TYPE);
8661 type = &type1;
8662 c += f->c;
8665 decl_initializer(type, sec, c, 0, size_only);
8667 /* XXX: make it more general */
8668 if (!size_only && nb_elems > 1) {
8669 unsigned long c_end;
8670 uint8_t *src, *dst;
8671 int i;
8673 if (!sec)
8674 error("range init not supported yet for dynamic storage");
8675 c_end = c + nb_elems * elem_size;
8676 if (c_end > sec->data_allocated)
8677 section_realloc(sec, c_end);
8678 src = sec->data + c;
8679 dst = src;
8680 for(i = 1; i < nb_elems; i++) {
8681 dst += elem_size;
8682 memcpy(dst, src, elem_size);
8687 #define EXPR_VAL 0
8688 #define EXPR_CONST 1
8689 #define EXPR_ANY 2
8691 /* store a value or an expression directly in global data or in local array */
8692 static void init_putv(CType *type, Section *sec, unsigned long c,
8693 int v, int expr_type)
8695 int saved_global_expr, bt, bit_pos, bit_size;
8696 void *ptr;
8697 unsigned long long bit_mask;
8698 CType dtype;
8700 switch(expr_type) {
8701 case EXPR_VAL:
8702 vpushi(v);
8703 break;
8704 case EXPR_CONST:
8705 /* compound literals must be allocated globally in this case */
8706 saved_global_expr = global_expr;
8707 global_expr = 1;
8708 expr_const1();
8709 global_expr = saved_global_expr;
8710 /* NOTE: symbols are accepted */
8711 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8712 error("initializer element is not constant");
8713 break;
8714 case EXPR_ANY:
8715 expr_eq();
8716 break;
8719 dtype = *type;
8720 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8722 if (sec) {
8723 /* XXX: not portable */
8724 /* XXX: generate error if incorrect relocation */
8725 gen_assign_cast(&dtype);
8726 bt = type->t & VT_BTYPE;
8727 ptr = sec->data + c;
8728 /* XXX: make code faster ? */
8729 if (!(type->t & VT_BITFIELD)) {
8730 bit_pos = 0;
8731 bit_size = 32;
8732 bit_mask = -1LL;
8733 } else {
8734 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8735 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8736 bit_mask = (1LL << bit_size) - 1;
8738 if ((vtop->r & VT_SYM) &&
8739 (bt == VT_BYTE ||
8740 bt == VT_SHORT ||
8741 bt == VT_DOUBLE ||
8742 bt == VT_LDOUBLE ||
8743 bt == VT_LLONG ||
8744 (bt == VT_INT && bit_size != 32)))
8745 error("initializer element is not computable at load time");
8746 switch(bt) {
8747 case VT_BOOL:
8748 vtop->c.i = (vtop->c.i != 0);
8749 case VT_BYTE:
8750 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8751 break;
8752 case VT_SHORT:
8753 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8754 break;
8755 case VT_DOUBLE:
8756 *(double *)ptr = vtop->c.d;
8757 break;
8758 case VT_LDOUBLE:
8759 *(long double *)ptr = vtop->c.ld;
8760 break;
8761 case VT_LLONG:
8762 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8763 break;
8764 default:
8765 if (vtop->r & VT_SYM) {
8766 greloc(sec, vtop->sym, c, R_DATA_32);
8768 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8769 break;
8771 vtop--;
8772 } else {
8773 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8774 vswap();
8775 vstore();
8776 vpop();
8780 /* put zeros for variable based init */
8781 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8783 if (sec) {
8784 /* nothing to do because globals are already set to zero */
8785 } else {
8786 vpush_global_sym(&func_old_type, TOK_memset);
8787 vseti(VT_LOCAL, c);
8788 vpushi(0);
8789 vpushi(size);
8790 gfunc_call(3);
8794 /* 't' contains the type and storage info. 'c' is the offset of the
8795 object in section 'sec'. If 'sec' is NULL, it means stack based
8796 allocation. 'first' is true if array '{' must be read (multi
8797 dimension implicit array init handling). 'size_only' is true if
8798 size only evaluation is wanted (only for arrays). */
8799 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8800 int first, int size_only)
8802 int index, array_length, n, no_oblock, nb, parlevel, i;
8803 int size1, align1, expr_type;
8804 Sym *s, *f;
8805 CType *t1;
8807 if (type->t & VT_ARRAY) {
8808 s = type->ref;
8809 n = s->c;
8810 array_length = 0;
8811 t1 = pointed_type(type);
8812 size1 = type_size(t1, &align1);
8814 no_oblock = 1;
8815 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8816 tok == '{') {
8817 skip('{');
8818 no_oblock = 0;
8821 /* only parse strings here if correct type (otherwise: handle
8822 them as ((w)char *) expressions */
8823 if ((tok == TOK_LSTR &&
8824 #ifdef TCC_TARGET_PE
8825 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8826 #else
8827 (t1->t & VT_BTYPE) == VT_INT
8828 #endif
8829 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8830 while (tok == TOK_STR || tok == TOK_LSTR) {
8831 int cstr_len, ch;
8832 CString *cstr;
8834 cstr = tokc.cstr;
8835 /* compute maximum number of chars wanted */
8836 if (tok == TOK_STR)
8837 cstr_len = cstr->size;
8838 else
8839 cstr_len = cstr->size / sizeof(nwchar_t);
8840 cstr_len--;
8841 nb = cstr_len;
8842 if (n >= 0 && nb > (n - array_length))
8843 nb = n - array_length;
8844 if (!size_only) {
8845 if (cstr_len > nb)
8846 warning("initializer-string for array is too long");
8847 /* in order to go faster for common case (char
8848 string in global variable, we handle it
8849 specifically */
8850 if (sec && tok == TOK_STR && size1 == 1) {
8851 memcpy(sec->data + c + array_length, cstr->data, nb);
8852 } else {
8853 for(i=0;i<nb;i++) {
8854 if (tok == TOK_STR)
8855 ch = ((unsigned char *)cstr->data)[i];
8856 else
8857 ch = ((nwchar_t *)cstr->data)[i];
8858 init_putv(t1, sec, c + (array_length + i) * size1,
8859 ch, EXPR_VAL);
8863 array_length += nb;
8864 next();
8866 /* only add trailing zero if enough storage (no
8867 warning in this case since it is standard) */
8868 if (n < 0 || array_length < n) {
8869 if (!size_only) {
8870 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8872 array_length++;
8874 } else {
8875 index = 0;
8876 while (tok != '}') {
8877 decl_designator(type, sec, c, &index, NULL, size_only);
8878 if (n >= 0 && index >= n)
8879 error("index too large");
8880 /* must put zero in holes (note that doing it that way
8881 ensures that it even works with designators) */
8882 if (!size_only && array_length < index) {
8883 init_putz(t1, sec, c + array_length * size1,
8884 (index - array_length) * size1);
8886 index++;
8887 if (index > array_length)
8888 array_length = index;
8889 /* special test for multi dimensional arrays (may not
8890 be strictly correct if designators are used at the
8891 same time) */
8892 if (index >= n && no_oblock)
8893 break;
8894 if (tok == '}')
8895 break;
8896 skip(',');
8899 if (!no_oblock)
8900 skip('}');
8901 /* put zeros at the end */
8902 if (!size_only && n >= 0 && array_length < n) {
8903 init_putz(t1, sec, c + array_length * size1,
8904 (n - array_length) * size1);
8906 /* patch type size if needed */
8907 if (n < 0)
8908 s->c = array_length;
8909 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8910 (sec || !first || tok == '{')) {
8911 int par_count;
8913 /* NOTE: the previous test is a specific case for automatic
8914 struct/union init */
8915 /* XXX: union needs only one init */
8917 /* XXX: this test is incorrect for local initializers
8918 beginning with ( without {. It would be much more difficult
8919 to do it correctly (ideally, the expression parser should
8920 be used in all cases) */
8921 par_count = 0;
8922 if (tok == '(') {
8923 AttributeDef ad1;
8924 CType type1;
8925 next();
8926 while (tok == '(') {
8927 par_count++;
8928 next();
8930 if (!parse_btype(&type1, &ad1))
8931 expect("cast");
8932 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8933 #if 0
8934 if (!is_assignable_types(type, &type1))
8935 error("invalid type for cast");
8936 #endif
8937 skip(')');
8939 no_oblock = 1;
8940 if (first || tok == '{') {
8941 skip('{');
8942 no_oblock = 0;
8944 s = type->ref;
8945 f = s->next;
8946 array_length = 0;
8947 index = 0;
8948 n = s->c;
8949 while (tok != '}') {
8950 decl_designator(type, sec, c, NULL, &f, size_only);
8951 index = f->c;
8952 if (!size_only && array_length < index) {
8953 init_putz(type, sec, c + array_length,
8954 index - array_length);
8956 index = index + type_size(&f->type, &align1);
8957 if (index > array_length)
8958 array_length = index;
8959 f = f->next;
8960 if (no_oblock && f == NULL)
8961 break;
8962 if (tok == '}')
8963 break;
8964 skip(',');
8966 /* put zeros at the end */
8967 if (!size_only && array_length < n) {
8968 init_putz(type, sec, c + array_length,
8969 n - array_length);
8971 if (!no_oblock)
8972 skip('}');
8973 while (par_count) {
8974 skip(')');
8975 par_count--;
8977 } else if (tok == '{') {
8978 next();
8979 decl_initializer(type, sec, c, first, size_only);
8980 skip('}');
8981 } else if (size_only) {
8982 /* just skip expression */
8983 parlevel = 0;
8984 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8985 tok != -1) {
8986 if (tok == '(')
8987 parlevel++;
8988 else if (tok == ')')
8989 parlevel--;
8990 next();
8992 } else {
8993 /* currently, we always use constant expression for globals
8994 (may change for scripting case) */
8995 expr_type = EXPR_CONST;
8996 if (!sec)
8997 expr_type = EXPR_ANY;
8998 init_putv(type, sec, c, 0, expr_type);
9002 /* parse an initializer for type 't' if 'has_init' is non zero, and
9003 allocate space in local or global data space ('r' is either
9004 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
9005 variable 'v' of scope 'scope' is declared before initializers are
9006 parsed. If 'v' is zero, then a reference to the new object is put
9007 in the value stack. If 'has_init' is 2, a special parsing is done
9008 to handle string constants. */
9009 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
9010 int has_init, int v, int scope)
9012 int size, align, addr, data_offset;
9013 int level;
9014 ParseState saved_parse_state;
9015 TokenString init_str;
9016 Section *sec;
9018 size = type_size(type, &align);
9019 /* If unknown size, we must evaluate it before
9020 evaluating initializers because
9021 initializers can generate global data too
9022 (e.g. string pointers or ISOC99 compound
9023 literals). It also simplifies local
9024 initializers handling */
9025 tok_str_new(&init_str);
9026 if (size < 0) {
9027 if (!has_init)
9028 error("unknown type size");
9029 /* get all init string */
9030 if (has_init == 2) {
9031 /* only get strings */
9032 while (tok == TOK_STR || tok == TOK_LSTR) {
9033 tok_str_add_tok(&init_str);
9034 next();
9036 } else {
9037 level = 0;
9038 while (level > 0 || (tok != ',' && tok != ';')) {
9039 if (tok < 0)
9040 error("unexpected end of file in initializer");
9041 tok_str_add_tok(&init_str);
9042 if (tok == '{')
9043 level++;
9044 else if (tok == '}') {
9045 if (level == 0)
9046 break;
9047 level--;
9049 next();
9052 tok_str_add(&init_str, -1);
9053 tok_str_add(&init_str, 0);
9055 /* compute size */
9056 save_parse_state(&saved_parse_state);
9058 macro_ptr = init_str.str;
9059 next();
9060 decl_initializer(type, NULL, 0, 1, 1);
9061 /* prepare second initializer parsing */
9062 macro_ptr = init_str.str;
9063 next();
9065 /* if still unknown size, error */
9066 size = type_size(type, &align);
9067 if (size < 0)
9068 error("unknown type size");
9070 /* take into account specified alignment if bigger */
9071 if (ad->aligned) {
9072 if (ad->aligned > align)
9073 align = ad->aligned;
9074 } else if (ad->packed) {
9075 align = 1;
9077 if ((r & VT_VALMASK) == VT_LOCAL) {
9078 sec = NULL;
9079 if (do_bounds_check && (type->t & VT_ARRAY))
9080 loc--;
9081 loc = (loc - size) & -align;
9082 addr = loc;
9083 /* handles bounds */
9084 /* XXX: currently, since we do only one pass, we cannot track
9085 '&' operators, so we add only arrays */
9086 if (do_bounds_check && (type->t & VT_ARRAY)) {
9087 unsigned long *bounds_ptr;
9088 /* add padding between regions */
9089 loc--;
9090 /* then add local bound info */
9091 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9092 bounds_ptr[0] = addr;
9093 bounds_ptr[1] = size;
9095 if (v) {
9096 /* local variable */
9097 sym_push(v, type, r, addr);
9098 } else {
9099 /* push local reference */
9100 vset(type, r, addr);
9102 } else {
9103 Sym *sym;
9105 sym = NULL;
9106 if (v && scope == VT_CONST) {
9107 /* see if the symbol was already defined */
9108 sym = sym_find(v);
9109 if (sym) {
9110 if (!is_compatible_types(&sym->type, type))
9111 error("incompatible types for redefinition of '%s'",
9112 get_tok_str(v, NULL));
9113 if (sym->type.t & VT_EXTERN) {
9114 /* if the variable is extern, it was not allocated */
9115 sym->type.t &= ~VT_EXTERN;
9116 /* set array size if it was ommited in extern
9117 declaration */
9118 if ((sym->type.t & VT_ARRAY) &&
9119 sym->type.ref->c < 0 &&
9120 type->ref->c >= 0)
9121 sym->type.ref->c = type->ref->c;
9122 } else {
9123 /* we accept several definitions of the same
9124 global variable. this is tricky, because we
9125 must play with the SHN_COMMON type of the symbol */
9126 /* XXX: should check if the variable was already
9127 initialized. It is incorrect to initialized it
9128 twice */
9129 /* no init data, we won't add more to the symbol */
9130 if (!has_init)
9131 goto no_alloc;
9136 /* allocate symbol in corresponding section */
9137 sec = ad->section;
9138 if (!sec) {
9139 if (has_init)
9140 sec = data_section;
9141 else if (tcc_state->nocommon)
9142 sec = bss_section;
9144 if (sec) {
9145 data_offset = sec->data_offset;
9146 data_offset = (data_offset + align - 1) & -align;
9147 addr = data_offset;
9148 /* very important to increment global pointer at this time
9149 because initializers themselves can create new initializers */
9150 data_offset += size;
9151 /* add padding if bound check */
9152 if (do_bounds_check)
9153 data_offset++;
9154 sec->data_offset = data_offset;
9155 /* allocate section space to put the data */
9156 if (sec->sh_type != SHT_NOBITS &&
9157 data_offset > sec->data_allocated)
9158 section_realloc(sec, data_offset);
9159 /* align section if needed */
9160 if (align > sec->sh_addralign)
9161 sec->sh_addralign = align;
9162 } else {
9163 addr = 0; /* avoid warning */
9166 if (v) {
9167 if (scope != VT_CONST || !sym) {
9168 sym = sym_push(v, type, r | VT_SYM, 0);
9170 /* update symbol definition */
9171 if (sec) {
9172 put_extern_sym(sym, sec, addr, size);
9173 } else {
9174 ElfW(Sym) *esym;
9175 /* put a common area */
9176 put_extern_sym(sym, NULL, align, size);
9177 /* XXX: find a nicer way */
9178 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
9179 esym->st_shndx = SHN_COMMON;
9181 } else {
9182 CValue cval;
9184 /* push global reference */
9185 sym = get_sym_ref(type, sec, addr, size);
9186 cval.ul = 0;
9187 vsetc(type, VT_CONST | VT_SYM, &cval);
9188 vtop->sym = sym;
9191 /* handles bounds now because the symbol must be defined
9192 before for the relocation */
9193 if (do_bounds_check) {
9194 unsigned long *bounds_ptr;
9196 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9197 /* then add global bound info */
9198 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9199 bounds_ptr[0] = 0; /* relocated */
9200 bounds_ptr[1] = size;
9203 if (has_init) {
9204 decl_initializer(type, sec, addr, 1, 0);
9205 /* restore parse state if needed */
9206 if (init_str.str) {
9207 tok_str_free(init_str.str);
9208 restore_parse_state(&saved_parse_state);
9211 no_alloc: ;
9214 void put_func_debug(Sym *sym)
9216 char buf[512];
9218 /* stabs info */
9219 /* XXX: we put here a dummy type */
9220 snprintf(buf, sizeof(buf), "%s:%c1",
9221 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9222 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9223 cur_text_section, sym->c);
9224 /* //gr gdb wants a line at the function */
9225 put_stabn(N_SLINE, 0, file->line_num, 0);
9226 last_ind = 0;
9227 last_line_num = 0;
9230 /* parse an old style function declaration list */
9231 /* XXX: check multiple parameter */
9232 static void func_decl_list(Sym *func_sym)
9234 AttributeDef ad;
9235 int v;
9236 Sym *s;
9237 CType btype, type;
9239 /* parse each declaration */
9240 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9241 if (!parse_btype(&btype, &ad))
9242 expect("declaration list");
9243 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9244 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9245 tok == ';') {
9246 /* we accept no variable after */
9247 } else {
9248 for(;;) {
9249 type = btype;
9250 type_decl(&type, &ad, &v, TYPE_DIRECT);
9251 /* find parameter in function parameter list */
9252 s = func_sym->next;
9253 while (s != NULL) {
9254 if ((s->v & ~SYM_FIELD) == v)
9255 goto found;
9256 s = s->next;
9258 error("declaration for parameter '%s' but no such parameter",
9259 get_tok_str(v, NULL));
9260 found:
9261 /* check that no storage specifier except 'register' was given */
9262 if (type.t & VT_STORAGE)
9263 error("storage class specified for '%s'", get_tok_str(v, NULL));
9264 convert_parameter_type(&type);
9265 /* we can add the type (NOTE: it could be local to the function) */
9266 s->type = type;
9267 /* accept other parameters */
9268 if (tok == ',')
9269 next();
9270 else
9271 break;
9274 skip(';');
9278 /* parse a function defined by symbol 'sym' and generate its code in
9279 'cur_text_section' */
9280 static void gen_function(Sym *sym)
9282 int saved_nocode_wanted = nocode_wanted;
9283 nocode_wanted = 0;
9284 ind = cur_text_section->data_offset;
9285 /* NOTE: we patch the symbol size later */
9286 put_extern_sym(sym, cur_text_section, ind, 0);
9287 funcname = get_tok_str(sym->v, NULL);
9288 func_ind = ind;
9289 /* put debug symbol */
9290 if (do_debug)
9291 put_func_debug(sym);
9292 /* push a dummy symbol to enable local sym storage */
9293 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9294 gfunc_prolog(&sym->type);
9295 rsym = 0;
9296 block(NULL, NULL, NULL, NULL, 0, 0);
9297 gsym(rsym);
9298 gfunc_epilog();
9299 cur_text_section->data_offset = ind;
9300 label_pop(&global_label_stack, NULL);
9301 sym_pop(&local_stack, NULL); /* reset local stack */
9302 /* end of function */
9303 /* patch symbol size */
9304 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
9305 ind - func_ind;
9306 if (do_debug) {
9307 put_stabn(N_FUN, 0, 0, ind - func_ind);
9309 /* It's better to crash than to generate wrong code */
9310 cur_text_section = NULL;
9311 funcname = ""; /* for safety */
9312 func_vt.t = VT_VOID; /* for safety */
9313 ind = 0; /* for safety */
9314 nocode_wanted = saved_nocode_wanted;
9317 static void gen_inline_functions(void)
9319 Sym *sym;
9320 CType *type;
9321 int *str, inline_generated;
9323 /* iterate while inline function are referenced */
9324 for(;;) {
9325 inline_generated = 0;
9326 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9327 type = &sym->type;
9328 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9329 (type->t & (VT_STATIC | VT_INLINE)) ==
9330 (VT_STATIC | VT_INLINE) &&
9331 sym->c != 0) {
9332 /* the function was used: generate its code and
9333 convert it to a normal function */
9334 str = INLINE_DEF(sym->r);
9335 sym->r = VT_SYM | VT_CONST;
9336 sym->type.t &= ~VT_INLINE;
9338 macro_ptr = str;
9339 next();
9340 cur_text_section = text_section;
9341 gen_function(sym);
9342 macro_ptr = NULL; /* fail safe */
9344 tok_str_free(str);
9345 inline_generated = 1;
9348 if (!inline_generated)
9349 break;
9352 /* free all remaining inline function tokens */
9353 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9354 type = &sym->type;
9355 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9356 (type->t & (VT_STATIC | VT_INLINE)) ==
9357 (VT_STATIC | VT_INLINE)) {
9358 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9359 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9360 continue;
9361 str = INLINE_DEF(sym->r);
9362 tok_str_free(str);
9363 sym->r = 0; /* fail safe */
9368 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9369 static void decl(int l)
9371 int v, has_init, r;
9372 CType type, btype;
9373 Sym *sym;
9374 AttributeDef ad;
9376 while (1) {
9377 if (!parse_btype(&btype, &ad)) {
9378 /* skip redundant ';' */
9379 /* XXX: find more elegant solution */
9380 if (tok == ';') {
9381 next();
9382 continue;
9384 if (l == VT_CONST &&
9385 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9386 /* global asm block */
9387 asm_global_instr();
9388 continue;
9390 /* special test for old K&R protos without explicit int
9391 type. Only accepted when defining global data */
9392 if (l == VT_LOCAL || tok < TOK_DEFINE)
9393 break;
9394 btype.t = VT_INT;
9396 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9397 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9398 tok == ';') {
9399 /* we accept no variable after */
9400 next();
9401 continue;
9403 while (1) { /* iterate thru each declaration */
9404 type = btype;
9405 type_decl(&type, &ad, &v, TYPE_DIRECT);
9406 #if 0
9408 char buf[500];
9409 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9410 printf("type = '%s'\n", buf);
9412 #endif
9413 if ((type.t & VT_BTYPE) == VT_FUNC) {
9414 /* if old style function prototype, we accept a
9415 declaration list */
9416 sym = type.ref;
9417 if (sym->c == FUNC_OLD)
9418 func_decl_list(sym);
9421 if (tok == '{') {
9422 if (l == VT_LOCAL)
9423 error("cannot use local functions");
9424 if ((type.t & VT_BTYPE) != VT_FUNC)
9425 expect("function definition");
9427 /* reject abstract declarators in function definition */
9428 sym = type.ref;
9429 while ((sym = sym->next) != NULL)
9430 if (!(sym->v & ~SYM_FIELD))
9431 expect("identifier");
9433 /* XXX: cannot do better now: convert extern line to static inline */
9434 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9435 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9437 sym = sym_find(v);
9438 if (sym) {
9439 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9440 goto func_error1;
9441 /* specific case: if not func_call defined, we put
9442 the one of the prototype */
9443 /* XXX: should have default value */
9444 r = sym->type.ref->r;
9445 if (FUNC_CALL(r) != FUNC_CDECL
9446 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9447 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9448 if (FUNC_EXPORT(r))
9449 FUNC_EXPORT(type.ref->r) = 1;
9451 if (!is_compatible_types(&sym->type, &type)) {
9452 func_error1:
9453 error("incompatible types for redefinition of '%s'",
9454 get_tok_str(v, NULL));
9456 /* if symbol is already defined, then put complete type */
9457 sym->type = type;
9458 } else {
9459 /* put function symbol */
9460 sym = global_identifier_push(v, type.t, 0);
9461 sym->type.ref = type.ref;
9464 /* static inline functions are just recorded as a kind
9465 of macro. Their code will be emitted at the end of
9466 the compilation unit only if they are used */
9467 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9468 (VT_INLINE | VT_STATIC)) {
9469 TokenString func_str;
9470 int block_level;
9472 tok_str_new(&func_str);
9474 block_level = 0;
9475 for(;;) {
9476 int t;
9477 if (tok == TOK_EOF)
9478 error("unexpected end of file");
9479 tok_str_add_tok(&func_str);
9480 t = tok;
9481 next();
9482 if (t == '{') {
9483 block_level++;
9484 } else if (t == '}') {
9485 block_level--;
9486 if (block_level == 0)
9487 break;
9490 tok_str_add(&func_str, -1);
9491 tok_str_add(&func_str, 0);
9492 INLINE_DEF(sym->r) = func_str.str;
9493 } else {
9494 /* compute text section */
9495 cur_text_section = ad.section;
9496 if (!cur_text_section)
9497 cur_text_section = text_section;
9498 sym->r = VT_SYM | VT_CONST;
9499 gen_function(sym);
9501 break;
9502 } else {
9503 if (btype.t & VT_TYPEDEF) {
9504 /* save typedefed type */
9505 /* XXX: test storage specifiers ? */
9506 sym = sym_push(v, &type, 0, 0);
9507 sym->type.t |= VT_TYPEDEF;
9508 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9509 /* external function definition */
9510 /* specific case for func_call attribute */
9511 if (ad.func_attr)
9512 type.ref->r = ad.func_attr;
9513 external_sym(v, &type, 0);
9514 } else {
9515 /* not lvalue if array */
9516 r = 0;
9517 if (!(type.t & VT_ARRAY))
9518 r |= lvalue_type(type.t);
9519 has_init = (tok == '=');
9520 if ((btype.t & VT_EXTERN) ||
9521 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9522 !has_init && l == VT_CONST && type.ref->c < 0)) {
9523 /* external variable */
9524 /* NOTE: as GCC, uninitialized global static
9525 arrays of null size are considered as
9526 extern */
9527 external_sym(v, &type, r);
9528 } else {
9529 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9530 if (type.t & VT_STATIC)
9531 r |= VT_CONST;
9532 else
9533 r |= l;
9534 if (has_init)
9535 next();
9536 decl_initializer_alloc(&type, &ad, r,
9537 has_init, v, l);
9540 if (tok != ',') {
9541 skip(';');
9542 break;
9544 next();
9550 /* better than nothing, but needs extension to handle '-E' option
9551 correctly too */
9552 static void preprocess_init(TCCState *s1)
9554 s1->include_stack_ptr = s1->include_stack;
9555 /* XXX: move that before to avoid having to initialize
9556 file->ifdef_stack_ptr ? */
9557 s1->ifdef_stack_ptr = s1->ifdef_stack;
9558 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9560 /* XXX: not ANSI compliant: bound checking says error */
9561 vtop = vstack - 1;
9562 s1->pack_stack[0] = 0;
9563 s1->pack_stack_ptr = s1->pack_stack;
9566 /* compile the C file opened in 'file'. Return non zero if errors. */
9567 static int tcc_compile(TCCState *s1)
9569 Sym *define_start;
9570 char buf[512];
9571 volatile int section_sym;
9573 #ifdef INC_DEBUG
9574 printf("%s: **** new file\n", file->filename);
9575 #endif
9576 preprocess_init(s1);
9578 cur_text_section = NULL;
9579 funcname = "";
9580 anon_sym = SYM_FIRST_ANOM;
9582 /* file info: full path + filename */
9583 section_sym = 0; /* avoid warning */
9584 if (do_debug) {
9585 section_sym = put_elf_sym(symtab_section, 0, 0,
9586 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
9587 text_section->sh_num, NULL);
9588 getcwd(buf, sizeof(buf));
9589 #ifdef _WIN32
9590 normalize_slashes(buf);
9591 #endif
9592 pstrcat(buf, sizeof(buf), "/");
9593 put_stabs_r(buf, N_SO, 0, 0,
9594 text_section->data_offset, text_section, section_sym);
9595 put_stabs_r(file->filename, N_SO, 0, 0,
9596 text_section->data_offset, text_section, section_sym);
9598 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9599 symbols can be safely used */
9600 put_elf_sym(symtab_section, 0, 0,
9601 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
9602 SHN_ABS, file->filename);
9604 /* define some often used types */
9605 int_type.t = VT_INT;
9607 char_pointer_type.t = VT_BYTE;
9608 mk_pointer(&char_pointer_type);
9610 func_old_type.t = VT_FUNC;
9611 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9613 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9614 float_type.t = VT_FLOAT;
9615 double_type.t = VT_DOUBLE;
9617 func_float_type.t = VT_FUNC;
9618 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9619 func_double_type.t = VT_FUNC;
9620 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9621 #endif
9623 #if 0
9624 /* define 'void *alloca(unsigned int)' builtin function */
9626 Sym *s1;
9628 p = anon_sym++;
9629 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9630 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9631 s1->next = NULL;
9632 sym->next = s1;
9633 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9635 #endif
9637 define_start = define_stack;
9638 nocode_wanted = 1;
9640 if (setjmp(s1->error_jmp_buf) == 0) {
9641 s1->nb_errors = 0;
9642 s1->error_set_jmp_enabled = 1;
9644 ch = file->buf_ptr[0];
9645 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9646 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9647 next();
9648 decl(VT_CONST);
9649 if (tok != TOK_EOF)
9650 expect("declaration");
9652 /* end of translation unit info */
9653 if (do_debug) {
9654 put_stabs_r(NULL, N_SO, 0, 0,
9655 text_section->data_offset, text_section, section_sym);
9658 s1->error_set_jmp_enabled = 0;
9660 /* reset define stack, but leave -Dsymbols (may be incorrect if
9661 they are undefined) */
9662 free_defines(define_start);
9664 gen_inline_functions();
9666 sym_pop(&global_stack, NULL);
9667 sym_pop(&local_stack, NULL);
9669 return s1->nb_errors != 0 ? -1 : 0;
9672 /* Preprocess the current file */
9673 /* XXX: add line and file infos, add options to preserve spaces */
9674 static int tcc_preprocess(TCCState *s1)
9676 Sym *define_start;
9677 BufferedFile *file_ref;
9678 int token_seen, line_ref;
9680 preprocess_init(s1);
9681 define_start = define_stack;
9682 ch = file->buf_ptr[0];
9684 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9685 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9686 PARSE_FLAG_LINEFEED;
9688 token_seen = 0;
9689 line_ref = 0;
9690 file_ref = NULL;
9692 for (;;) {
9693 next();
9694 if (tok == TOK_EOF) {
9695 break;
9696 } else if (tok == TOK_LINEFEED) {
9697 if (!token_seen)
9698 continue;
9699 ++line_ref;
9700 token_seen = 0;
9701 } else if (token_seen) {
9702 fputc(' ', s1->outfile);
9703 } else {
9704 int d = file->line_num - line_ref;
9705 if (file != file_ref || d < 0 || d >= 8)
9706 fprintf(s1->outfile, "# %d \"%s\"\n", file->line_num, file->filename);
9707 else
9708 while (d)
9709 fputs("\n", s1->outfile), --d;
9710 line_ref = (file_ref = file)->line_num;
9711 token_seen = 1;
9713 fputs(get_tok_str(tok, &tokc), s1->outfile);
9715 free_defines(define_start);
9716 return 0;
9719 #ifdef LIBTCC
9720 int tcc_compile_string(TCCState *s, const char *str)
9722 BufferedFile bf1, *bf = &bf1;
9723 int ret, len;
9724 char *buf;
9726 /* init file structure */
9727 bf->fd = -1;
9728 /* XXX: avoid copying */
9729 len = strlen(str);
9730 buf = tcc_malloc(len + 1);
9731 if (!buf)
9732 return -1;
9733 memcpy(buf, str, len);
9734 buf[len] = CH_EOB;
9735 bf->buf_ptr = buf;
9736 bf->buf_end = buf + len;
9737 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9738 bf->line_num = 1;
9739 file = bf;
9740 ret = tcc_compile(s);
9741 file = NULL;
9742 tcc_free(buf);
9744 /* currently, no need to close */
9745 return ret;
9747 #endif
9749 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9750 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9752 BufferedFile bf1, *bf = &bf1;
9754 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9755 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9756 /* default value */
9757 if (!value)
9758 value = "1";
9759 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9761 /* init file structure */
9762 bf->fd = -1;
9763 bf->buf_ptr = bf->buffer;
9764 bf->buf_end = bf->buffer + strlen(bf->buffer);
9765 *bf->buf_end = CH_EOB;
9766 bf->filename[0] = '\0';
9767 bf->line_num = 1;
9768 file = bf;
9770 s1->include_stack_ptr = s1->include_stack;
9772 /* parse with define parser */
9773 ch = file->buf_ptr[0];
9774 next_nomacro();
9775 parse_define();
9776 file = NULL;
9779 /* undefine a preprocessor symbol */
9780 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9782 TokenSym *ts;
9783 Sym *s;
9784 ts = tok_alloc(sym, strlen(sym));
9785 s = define_find(ts->tok);
9786 /* undefine symbol by putting an invalid name */
9787 if (s)
9788 define_undef(s);
9791 #ifdef CONFIG_TCC_ASM
9793 #ifdef TCC_TARGET_I386
9794 #include "i386-asm.c"
9795 #endif
9796 #include "tccasm.c"
9798 #else
9799 static void asm_instr(void)
9801 error("inline asm() not supported");
9803 static void asm_global_instr(void)
9805 error("inline asm() not supported");
9807 #endif
9809 #include "tccelf.c"
9811 #ifdef TCC_TARGET_COFF
9812 #include "tcccoff.c"
9813 #endif
9815 #ifdef TCC_TARGET_PE
9816 #include "tccpe.c"
9817 #endif
9819 /* print the position in the source file of PC value 'pc' by reading
9820 the stabs debug information */
9821 static void rt_printline(unsigned long wanted_pc)
9823 Stab_Sym *sym, *sym_end;
9824 char func_name[128], last_func_name[128];
9825 unsigned long func_addr, last_pc, pc;
9826 const char *incl_files[INCLUDE_STACK_SIZE];
9827 int incl_index, len, last_line_num, i;
9828 const char *str, *p;
9830 fprintf(stderr, "0x%08lx:", wanted_pc);
9832 func_name[0] = '\0';
9833 func_addr = 0;
9834 incl_index = 0;
9835 last_func_name[0] = '\0';
9836 last_pc = 0xffffffff;
9837 last_line_num = 1;
9838 sym = (Stab_Sym *)stab_section->data + 1;
9839 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9840 while (sym < sym_end) {
9841 switch(sym->n_type) {
9842 /* function start or end */
9843 case N_FUN:
9844 if (sym->n_strx == 0) {
9845 /* we test if between last line and end of function */
9846 pc = sym->n_value + func_addr;
9847 if (wanted_pc >= last_pc && wanted_pc < pc)
9848 goto found;
9849 func_name[0] = '\0';
9850 func_addr = 0;
9851 } else {
9852 str = stabstr_section->data + sym->n_strx;
9853 p = strchr(str, ':');
9854 if (!p) {
9855 pstrcpy(func_name, sizeof(func_name), str);
9856 } else {
9857 len = p - str;
9858 if (len > sizeof(func_name) - 1)
9859 len = sizeof(func_name) - 1;
9860 memcpy(func_name, str, len);
9861 func_name[len] = '\0';
9863 func_addr = sym->n_value;
9865 break;
9866 /* line number info */
9867 case N_SLINE:
9868 pc = sym->n_value + func_addr;
9869 if (wanted_pc >= last_pc && wanted_pc < pc)
9870 goto found;
9871 last_pc = pc;
9872 last_line_num = sym->n_desc;
9873 /* XXX: slow! */
9874 strcpy(last_func_name, func_name);
9875 break;
9876 /* include files */
9877 case N_BINCL:
9878 str = stabstr_section->data + sym->n_strx;
9879 add_incl:
9880 if (incl_index < INCLUDE_STACK_SIZE) {
9881 incl_files[incl_index++] = str;
9883 break;
9884 case N_EINCL:
9885 if (incl_index > 1)
9886 incl_index--;
9887 break;
9888 case N_SO:
9889 if (sym->n_strx == 0) {
9890 incl_index = 0; /* end of translation unit */
9891 } else {
9892 str = stabstr_section->data + sym->n_strx;
9893 /* do not add path */
9894 len = strlen(str);
9895 if (len > 0 && str[len - 1] != '/')
9896 goto add_incl;
9898 break;
9900 sym++;
9903 /* second pass: we try symtab symbols (no line number info) */
9904 incl_index = 0;
9906 ElfW(Sym) *sym, *sym_end;
9907 int type;
9909 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
9910 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
9911 sym < sym_end;
9912 sym++) {
9913 type = ELFW(ST_TYPE)(sym->st_info);
9914 if (type == STT_FUNC) {
9915 if (wanted_pc >= sym->st_value &&
9916 wanted_pc < sym->st_value + sym->st_size) {
9917 pstrcpy(last_func_name, sizeof(last_func_name),
9918 strtab_section->data + sym->st_name);
9919 goto found;
9924 /* did not find any info: */
9925 fprintf(stderr, " ???\n");
9926 return;
9927 found:
9928 if (last_func_name[0] != '\0') {
9929 fprintf(stderr, " %s()", last_func_name);
9931 if (incl_index > 0) {
9932 fprintf(stderr, " (%s:%d",
9933 incl_files[incl_index - 1], last_line_num);
9934 for(i = incl_index - 2; i >= 0; i--)
9935 fprintf(stderr, ", included from %s", incl_files[i]);
9936 fprintf(stderr, ")");
9938 fprintf(stderr, "\n");
9941 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
9943 #ifdef __i386__
9945 /* fix for glibc 2.1 */
9946 #ifndef REG_EIP
9947 #define REG_EIP EIP
9948 #define REG_EBP EBP
9949 #endif
9951 /* return the PC at frame level 'level'. Return non zero if not found */
9952 static int rt_get_caller_pc(unsigned long *paddr,
9953 ucontext_t *uc, int level)
9955 unsigned long fp;
9956 int i;
9958 if (level == 0) {
9959 #if defined(__FreeBSD__)
9960 *paddr = uc->uc_mcontext.mc_eip;
9961 #elif defined(__dietlibc__)
9962 *paddr = uc->uc_mcontext.eip;
9963 #else
9964 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9965 #endif
9966 return 0;
9967 } else {
9968 #if defined(__FreeBSD__)
9969 fp = uc->uc_mcontext.mc_ebp;
9970 #elif defined(__dietlibc__)
9971 fp = uc->uc_mcontext.ebp;
9972 #else
9973 fp = uc->uc_mcontext.gregs[REG_EBP];
9974 #endif
9975 for(i=1;i<level;i++) {
9976 /* XXX: check address validity with program info */
9977 if (fp <= 0x1000 || fp >= 0xc0000000)
9978 return -1;
9979 fp = ((unsigned long *)fp)[0];
9981 *paddr = ((unsigned long *)fp)[1];
9982 return 0;
9985 #else
9987 #warning add arch specific rt_get_caller_pc()
9989 static int rt_get_caller_pc(unsigned long *paddr,
9990 ucontext_t *uc, int level)
9992 return -1;
9994 #endif
9996 /* emit a run time error at position 'pc' */
9997 void rt_error(ucontext_t *uc, const char *fmt, ...)
9999 va_list ap;
10000 unsigned long pc;
10001 int i;
10003 va_start(ap, fmt);
10004 fprintf(stderr, "Runtime error: ");
10005 vfprintf(stderr, fmt, ap);
10006 fprintf(stderr, "\n");
10007 for(i=0;i<num_callers;i++) {
10008 if (rt_get_caller_pc(&pc, uc, i) < 0)
10009 break;
10010 if (i == 0)
10011 fprintf(stderr, "at ");
10012 else
10013 fprintf(stderr, "by ");
10014 rt_printline(pc);
10016 exit(255);
10017 va_end(ap);
10020 /* signal handler for fatal errors */
10021 static void sig_error(int signum, siginfo_t *siginf, void *puc)
10023 ucontext_t *uc = puc;
10025 switch(signum) {
10026 case SIGFPE:
10027 switch(siginf->si_code) {
10028 case FPE_INTDIV:
10029 case FPE_FLTDIV:
10030 rt_error(uc, "division by zero");
10031 break;
10032 default:
10033 rt_error(uc, "floating point exception");
10034 break;
10036 break;
10037 case SIGBUS:
10038 case SIGSEGV:
10039 if (rt_bound_error_msg && *rt_bound_error_msg)
10040 rt_error(uc, *rt_bound_error_msg);
10041 else
10042 rt_error(uc, "dereferencing invalid pointer");
10043 break;
10044 case SIGILL:
10045 rt_error(uc, "illegal instruction");
10046 break;
10047 case SIGABRT:
10048 rt_error(uc, "abort() called");
10049 break;
10050 default:
10051 rt_error(uc, "caught signal %d", signum);
10052 break;
10054 exit(255);
10056 #endif
10058 /* do all relocations (needed before using tcc_get_symbol()) */
10059 int tcc_relocate(TCCState *s1)
10061 Section *s;
10062 int i;
10064 s1->nb_errors = 0;
10066 #ifdef TCC_TARGET_PE
10067 pe_add_runtime(s1);
10068 #else
10069 tcc_add_runtime(s1);
10070 #endif
10072 relocate_common_syms();
10074 tcc_add_linker_symbols(s1);
10075 #ifndef TCC_TARGET_PE
10076 build_got_entries(s1);
10077 #endif
10078 /* compute relocation address : section are relocated in place. We
10079 also alloc the bss space */
10080 for(i = 1; i < s1->nb_sections; i++) {
10081 s = s1->sections[i];
10082 if (s->sh_flags & SHF_ALLOC) {
10083 if (s->sh_type == SHT_NOBITS)
10084 s->data = tcc_mallocz(s->data_offset);
10085 s->sh_addr = (unsigned long)s->data;
10089 relocate_syms(s1, 1);
10091 if (s1->nb_errors != 0)
10092 return -1;
10094 /* relocate each section */
10095 for(i = 1; i < s1->nb_sections; i++) {
10096 s = s1->sections[i];
10097 if (s->reloc)
10098 relocate_section(s1, s);
10101 /* mark executable sections as executable in memory */
10102 for(i = 1; i < s1->nb_sections; i++) {
10103 s = s1->sections[i];
10104 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10105 (SHF_ALLOC | SHF_EXECINSTR))
10106 set_pages_executable(s->data, s->data_offset);
10108 return 0;
10111 /* launch the compiled program with the given arguments */
10112 int tcc_run(TCCState *s1, int argc, char **argv)
10114 int (*prog_main)(int, char **);
10116 if (tcc_relocate(s1) < 0)
10117 return -1;
10119 prog_main = tcc_get_symbol_err(s1, "main");
10121 if (do_debug) {
10122 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10123 error("debug mode currently not available for Windows");
10124 #else
10125 struct sigaction sigact;
10126 /* install TCC signal handlers to print debug info on fatal
10127 runtime errors */
10128 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10129 sigact.sa_sigaction = sig_error;
10130 sigemptyset(&sigact.sa_mask);
10131 sigaction(SIGFPE, &sigact, NULL);
10132 sigaction(SIGILL, &sigact, NULL);
10133 sigaction(SIGSEGV, &sigact, NULL);
10134 sigaction(SIGBUS, &sigact, NULL);
10135 sigaction(SIGABRT, &sigact, NULL);
10136 #endif
10139 #ifdef CONFIG_TCC_BCHECK
10140 if (do_bounds_check) {
10141 void (*bound_init)(void);
10143 /* set error function */
10144 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10145 "__bound_error_msg");
10147 /* XXX: use .init section so that it also work in binary ? */
10148 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10149 bound_init();
10151 #endif
10152 return (*prog_main)(argc, argv);
10155 void tcc_memstats(void)
10157 #ifdef MEM_DEBUG
10158 printf("memory in use: %d\n", mem_cur_size);
10159 #endif
10162 static void tcc_cleanup(void)
10164 int i, n;
10166 if (NULL == tcc_state)
10167 return;
10168 tcc_state = NULL;
10170 /* free -D defines */
10171 free_defines(NULL);
10173 /* free tokens */
10174 n = tok_ident - TOK_IDENT;
10175 for(i = 0; i < n; i++)
10176 tcc_free(table_ident[i]);
10177 tcc_free(table_ident);
10179 /* free sym_pools */
10180 dynarray_reset(&sym_pools, &nb_sym_pools);
10181 /* string buffer */
10182 cstr_free(&tokcstr);
10183 /* reset symbol stack */
10184 sym_free_first = NULL;
10185 /* cleanup from error/setjmp */
10186 macro_ptr = NULL;
10189 TCCState *tcc_new(void)
10191 const char *p, *r;
10192 TCCState *s;
10193 TokenSym *ts;
10194 int i, c;
10196 tcc_cleanup();
10198 s = tcc_mallocz(sizeof(TCCState));
10199 if (!s)
10200 return NULL;
10201 tcc_state = s;
10202 s->output_type = TCC_OUTPUT_MEMORY;
10204 /* init isid table */
10205 for(i=CH_EOF;i<256;i++)
10206 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
10208 /* add all tokens */
10209 table_ident = NULL;
10210 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10212 tok_ident = TOK_IDENT;
10213 p = tcc_keywords;
10214 while (*p) {
10215 r = p;
10216 for(;;) {
10217 c = *r++;
10218 if (c == '\0')
10219 break;
10221 ts = tok_alloc(p, r - p - 1);
10222 p = r;
10225 /* we add dummy defines for some special macros to speed up tests
10226 and to have working defined() */
10227 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10228 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10229 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10230 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10232 /* standard defines */
10233 tcc_define_symbol(s, "__STDC__", NULL);
10234 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10235 #if defined(TCC_TARGET_I386)
10236 tcc_define_symbol(s, "__i386__", NULL);
10237 #endif
10238 #if defined(TCC_TARGET_ARM)
10239 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10240 tcc_define_symbol(s, "__arm_elf__", NULL);
10241 tcc_define_symbol(s, "__arm_elf", NULL);
10242 tcc_define_symbol(s, "arm_elf", NULL);
10243 tcc_define_symbol(s, "__arm__", NULL);
10244 tcc_define_symbol(s, "__arm", NULL);
10245 tcc_define_symbol(s, "arm", NULL);
10246 tcc_define_symbol(s, "__APCS_32__", NULL);
10247 #endif
10248 #ifdef TCC_TARGET_PE
10249 tcc_define_symbol(s, "_WIN32", NULL);
10250 #else
10251 tcc_define_symbol(s, "__unix__", NULL);
10252 tcc_define_symbol(s, "__unix", NULL);
10253 #if defined(__linux)
10254 tcc_define_symbol(s, "__linux__", NULL);
10255 tcc_define_symbol(s, "__linux", NULL);
10256 #endif
10257 #endif
10258 /* tiny C specific defines */
10259 tcc_define_symbol(s, "__TINYC__", NULL);
10261 /* tiny C & gcc defines */
10262 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10263 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10264 #ifdef TCC_TARGET_PE
10265 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10266 #else
10267 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10268 #endif
10270 #ifndef TCC_TARGET_PE
10271 /* default library paths */
10272 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
10273 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
10274 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
10275 #endif
10277 /* no section zero */
10278 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10280 /* create standard sections */
10281 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10282 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10283 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10285 /* symbols are always generated for linking stage */
10286 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10287 ".strtab",
10288 ".hashtab", SHF_PRIVATE);
10289 strtab_section = symtab_section->link;
10291 /* private symbol table for dynamic symbols */
10292 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10293 ".dynstrtab",
10294 ".dynhashtab", SHF_PRIVATE);
10295 s->alacarte_link = 1;
10297 #ifdef CHAR_IS_UNSIGNED
10298 s->char_is_unsigned = 1;
10299 #endif
10300 #if defined(TCC_TARGET_PE) && 0
10301 /* XXX: currently the PE linker is not ready to support that */
10302 s->leading_underscore = 1;
10303 #endif
10304 return s;
10307 void tcc_delete(TCCState *s1)
10309 int i;
10311 tcc_cleanup();
10313 /* free all sections */
10314 free_section(s1->dynsymtab_section);
10316 for(i = 1; i < s1->nb_sections; i++)
10317 free_section(s1->sections[i]);
10318 tcc_free(s1->sections);
10320 /* free any loaded DLLs */
10321 for ( i = 0; i < s1->nb_loaded_dlls; i++)
10323 DLLReference *ref = s1->loaded_dlls[i];
10324 if ( ref->handle )
10325 dlclose(ref->handle);
10328 /* free loaded dlls array */
10329 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10331 /* free library paths */
10332 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10334 /* free include paths */
10335 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10336 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10337 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10339 tcc_free(s1);
10342 int tcc_add_include_path(TCCState *s1, const char *pathname)
10344 char *pathname1;
10346 pathname1 = tcc_strdup(pathname);
10347 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10348 return 0;
10351 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10353 char *pathname1;
10355 pathname1 = tcc_strdup(pathname);
10356 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10357 return 0;
10360 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10362 const char *ext;
10363 ElfW(Ehdr) ehdr;
10364 int fd, ret;
10365 BufferedFile *saved_file;
10367 /* find source file type with extension */
10368 ext = tcc_fileextension(filename);
10369 if (ext[0])
10370 ext++;
10372 /* open the file */
10373 saved_file = file;
10374 file = tcc_open(s1, filename);
10375 if (!file) {
10376 if (flags & AFF_PRINT_ERROR) {
10377 error_noabort("file '%s' not found", filename);
10379 ret = -1;
10380 goto fail1;
10383 if (flags & AFF_PREPROCESS) {
10384 ret = tcc_preprocess(s1);
10385 } else if (!ext[0] || !strcmp(ext, "c")) {
10386 /* C file assumed */
10387 ret = tcc_compile(s1);
10388 } else
10389 #ifdef CONFIG_TCC_ASM
10390 if (!strcmp(ext, "S")) {
10391 /* preprocessed assembler */
10392 ret = tcc_assemble(s1, 1);
10393 } else if (!strcmp(ext, "s")) {
10394 /* non preprocessed assembler */
10395 ret = tcc_assemble(s1, 0);
10396 } else
10397 #endif
10398 #ifdef TCC_TARGET_PE
10399 if (!strcmp(ext, "def")) {
10400 ret = pe_load_def_file(s1, file->fd);
10401 } else
10402 #endif
10404 fd = file->fd;
10405 /* assume executable format: auto guess file type */
10406 ret = read(fd, &ehdr, sizeof(ehdr));
10407 lseek(fd, 0, SEEK_SET);
10408 if (ret <= 0) {
10409 error_noabort("could not read header");
10410 goto fail;
10411 } else if (ret != sizeof(ehdr)) {
10412 goto try_load_script;
10415 if (ehdr.e_ident[0] == ELFMAG0 &&
10416 ehdr.e_ident[1] == ELFMAG1 &&
10417 ehdr.e_ident[2] == ELFMAG2 &&
10418 ehdr.e_ident[3] == ELFMAG3) {
10419 file->line_num = 0; /* do not display line number if error */
10420 if (ehdr.e_type == ET_REL) {
10421 ret = tcc_load_object_file(s1, fd, 0);
10422 } else if (ehdr.e_type == ET_DYN) {
10423 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10424 #ifdef TCC_TARGET_PE
10425 ret = -1;
10426 #else
10427 void *h;
10428 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10429 if (h)
10430 ret = 0;
10431 else
10432 ret = -1;
10433 #endif
10434 } else {
10435 ret = tcc_load_dll(s1, fd, filename,
10436 (flags & AFF_REFERENCED_DLL) != 0);
10438 } else {
10439 error_noabort("unrecognized ELF file");
10440 goto fail;
10442 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10443 file->line_num = 0; /* do not display line number if error */
10444 ret = tcc_load_archive(s1, fd);
10445 } else
10446 #ifdef TCC_TARGET_COFF
10447 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10448 ret = tcc_load_coff(s1, fd);
10449 } else
10450 #endif
10451 #ifdef TCC_TARGET_PE
10452 if (pe_test_res_file(&ehdr, ret)) {
10453 ret = pe_load_res_file(s1, fd);
10454 } else
10455 #endif
10457 /* as GNU ld, consider it is an ld script if not recognized */
10458 try_load_script:
10459 ret = tcc_load_ldscript(s1);
10460 if (ret < 0) {
10461 error_noabort("unrecognized file type");
10462 goto fail;
10466 the_end:
10467 tcc_close(file);
10468 fail1:
10469 file = saved_file;
10470 return ret;
10471 fail:
10472 ret = -1;
10473 goto the_end;
10476 int tcc_add_file(TCCState *s, const char *filename)
10478 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10481 int tcc_add_library_path(TCCState *s, const char *pathname)
10483 char *pathname1;
10485 pathname1 = tcc_strdup(pathname);
10486 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10487 return 0;
10490 /* find and load a dll. Return non zero if not found */
10491 /* XXX: add '-rpath' option support ? */
10492 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10494 char buf[1024];
10495 int i;
10497 for(i = 0; i < s->nb_library_paths; i++) {
10498 snprintf(buf, sizeof(buf), "%s/%s",
10499 s->library_paths[i], filename);
10500 if (tcc_add_file_internal(s, buf, flags) == 0)
10501 return 0;
10503 return -1;
10506 /* the library name is the same as the argument of the '-l' option */
10507 int tcc_add_library(TCCState *s, const char *libraryname)
10509 char buf[1024];
10510 int i;
10512 /* first we look for the dynamic library if not static linking */
10513 if (!s->static_link) {
10514 #ifdef TCC_TARGET_PE
10515 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10516 #else
10517 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10518 #endif
10519 if (tcc_add_dll(s, buf, 0) == 0)
10520 return 0;
10523 /* then we look for the static library */
10524 for(i = 0; i < s->nb_library_paths; i++) {
10525 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10526 s->library_paths[i], libraryname);
10527 if (tcc_add_file_internal(s, buf, 0) == 0)
10528 return 0;
10530 return -1;
10533 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10535 add_elf_sym(symtab_section, val, 0,
10536 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
10537 SHN_ABS, name);
10538 return 0;
10541 int tcc_set_output_type(TCCState *s, int output_type)
10543 char buf[1024];
10545 s->output_type = output_type;
10547 if (!s->nostdinc) {
10548 /* default include paths */
10549 /* XXX: reverse order needed if -isystem support */
10550 #ifndef TCC_TARGET_PE
10551 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
10552 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
10553 #endif
10554 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10555 tcc_add_sysinclude_path(s, buf);
10556 #ifdef TCC_TARGET_PE
10557 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10558 tcc_add_sysinclude_path(s, buf);
10559 #endif
10562 /* if bound checking, then add corresponding sections */
10563 #ifdef CONFIG_TCC_BCHECK
10564 if (do_bounds_check) {
10565 /* define symbol */
10566 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10567 /* create bounds sections */
10568 bounds_section = new_section(s, ".bounds",
10569 SHT_PROGBITS, SHF_ALLOC);
10570 lbounds_section = new_section(s, ".lbounds",
10571 SHT_PROGBITS, SHF_ALLOC);
10573 #endif
10575 if (s->char_is_unsigned) {
10576 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10579 /* add debug sections */
10580 if (do_debug) {
10581 /* stab symbols */
10582 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10583 stab_section->sh_entsize = sizeof(Stab_Sym);
10584 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10585 put_elf_str(stabstr_section, "");
10586 stab_section->link = stabstr_section;
10587 /* put first entry */
10588 put_stabs("", 0, 0, 0, 0);
10591 /* add libc crt1/crti objects */
10592 #ifndef TCC_TARGET_PE
10593 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10594 !s->nostdlib) {
10595 if (output_type != TCC_OUTPUT_DLL)
10596 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10597 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10599 #endif
10601 #ifdef TCC_TARGET_PE
10602 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10603 tcc_add_library_path(s, buf);
10604 #endif
10606 return 0;
10609 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10610 #define FD_INVERT 0x0002 /* invert value before storing */
10612 typedef struct FlagDef {
10613 uint16_t offset;
10614 uint16_t flags;
10615 const char *name;
10616 } FlagDef;
10618 static const FlagDef warning_defs[] = {
10619 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10620 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10621 { offsetof(TCCState, warn_error), 0, "error" },
10622 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10623 "implicit-function-declaration" },
10626 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10627 const char *name, int value)
10629 int i;
10630 const FlagDef *p;
10631 const char *r;
10633 r = name;
10634 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10635 r += 3;
10636 value = !value;
10638 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10639 if (!strcmp(r, p->name))
10640 goto found;
10642 return -1;
10643 found:
10644 if (p->flags & FD_INVERT)
10645 value = !value;
10646 *(int *)((uint8_t *)s + p->offset) = value;
10647 return 0;
10651 /* set/reset a warning */
10652 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10654 int i;
10655 const FlagDef *p;
10657 if (!strcmp(warning_name, "all")) {
10658 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10659 if (p->flags & WD_ALL)
10660 *(int *)((uint8_t *)s + p->offset) = 1;
10662 return 0;
10663 } else {
10664 return set_flag(s, warning_defs, countof(warning_defs),
10665 warning_name, value);
10669 static const FlagDef flag_defs[] = {
10670 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10671 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10672 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10673 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10676 /* set/reset a flag */
10677 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10679 return set_flag(s, flag_defs, countof(flag_defs),
10680 flag_name, value);
10683 #if !defined(LIBTCC)
10685 static int64_t getclock_us(void)
10687 #ifdef _WIN32
10688 struct _timeb tb;
10689 _ftime(&tb);
10690 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10691 #else
10692 struct timeval tv;
10693 gettimeofday(&tv, NULL);
10694 return tv.tv_sec * 1000000LL + tv.tv_usec;
10695 #endif
10698 void help(void)
10700 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10701 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10702 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10703 " [-static] [infile1 infile2...] [-run infile args...]\n"
10704 "\n"
10705 "General options:\n"
10706 " -v display current version, increase verbosity\n"
10707 " -c compile only - generate an object file\n"
10708 " -o outfile set output filename\n"
10709 " -Bdir set tcc internal library path\n"
10710 " -bench output compilation statistics\n"
10711 " -run run compiled source\n"
10712 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10713 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10714 " -w disable all warnings\n"
10715 "Preprocessor options:\n"
10716 " -E preprocess only\n"
10717 " -Idir add include path 'dir'\n"
10718 " -Dsym[=val] define 'sym' with value 'val'\n"
10719 " -Usym undefine 'sym'\n"
10720 "Linker options:\n"
10721 " -Ldir add library path 'dir'\n"
10722 " -llib link with dynamic or static library 'lib'\n"
10723 " -shared generate a shared library\n"
10724 " -soname set name for shared library to be used at runtime\n"
10725 " -static static linking\n"
10726 " -rdynamic export all global symbols to dynamic linker\n"
10727 " -r generate (relocatable) object file\n"
10728 "Debugger options:\n"
10729 " -g generate runtime debug info\n"
10730 #ifdef CONFIG_TCC_BCHECK
10731 " -b compile with built-in memory and bounds checker (implies -g)\n"
10732 #endif
10733 " -bt N show N callers in stack traces\n"
10737 #define TCC_OPTION_HAS_ARG 0x0001
10738 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10740 typedef struct TCCOption {
10741 const char *name;
10742 uint16_t index;
10743 uint16_t flags;
10744 } TCCOption;
10746 enum {
10747 TCC_OPTION_HELP,
10748 TCC_OPTION_I,
10749 TCC_OPTION_D,
10750 TCC_OPTION_U,
10751 TCC_OPTION_L,
10752 TCC_OPTION_B,
10753 TCC_OPTION_l,
10754 TCC_OPTION_bench,
10755 TCC_OPTION_bt,
10756 TCC_OPTION_b,
10757 TCC_OPTION_g,
10758 TCC_OPTION_c,
10759 TCC_OPTION_static,
10760 TCC_OPTION_shared,
10761 TCC_OPTION_soname,
10762 TCC_OPTION_o,
10763 TCC_OPTION_r,
10764 TCC_OPTION_Wl,
10765 TCC_OPTION_W,
10766 TCC_OPTION_O,
10767 TCC_OPTION_m,
10768 TCC_OPTION_f,
10769 TCC_OPTION_nostdinc,
10770 TCC_OPTION_nostdlib,
10771 TCC_OPTION_print_search_dirs,
10772 TCC_OPTION_rdynamic,
10773 TCC_OPTION_run,
10774 TCC_OPTION_v,
10775 TCC_OPTION_w,
10776 TCC_OPTION_pipe,
10777 TCC_OPTION_E,
10780 static const TCCOption tcc_options[] = {
10781 { "h", TCC_OPTION_HELP, 0 },
10782 { "?", TCC_OPTION_HELP, 0 },
10783 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10784 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10785 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10786 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10787 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10788 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10789 { "bench", TCC_OPTION_bench, 0 },
10790 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10791 #ifdef CONFIG_TCC_BCHECK
10792 { "b", TCC_OPTION_b, 0 },
10793 #endif
10794 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10795 { "c", TCC_OPTION_c, 0 },
10796 { "static", TCC_OPTION_static, 0 },
10797 { "shared", TCC_OPTION_shared, 0 },
10798 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10799 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10800 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10801 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10802 { "r", TCC_OPTION_r, 0 },
10803 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10804 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10805 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10806 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10807 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10808 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10809 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10810 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10811 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10812 { "w", TCC_OPTION_w, 0 },
10813 { "pipe", TCC_OPTION_pipe, 0},
10814 { "E", TCC_OPTION_E, 0},
10815 { NULL },
10818 /* convert 'str' into an array of space separated strings */
10819 static int expand_args(char ***pargv, const char *str)
10821 const char *s1;
10822 char **argv, *arg;
10823 int argc, len;
10825 argc = 0;
10826 argv = NULL;
10827 for(;;) {
10828 while (is_space(*str))
10829 str++;
10830 if (*str == '\0')
10831 break;
10832 s1 = str;
10833 while (*str != '\0' && !is_space(*str))
10834 str++;
10835 len = str - s1;
10836 arg = tcc_malloc(len + 1);
10837 memcpy(arg, s1, len);
10838 arg[len] = '\0';
10839 dynarray_add((void ***)&argv, &argc, arg);
10841 *pargv = argv;
10842 return argc;
10845 static char **files;
10846 static int nb_files, nb_libraries;
10847 static int multiple_files;
10848 static int print_search_dirs;
10849 static int output_type;
10850 static int reloc_output;
10851 static const char *outfile;
10853 int parse_args(TCCState *s, int argc, char **argv)
10855 int optind;
10856 const TCCOption *popt;
10857 const char *optarg, *p1, *r1;
10858 char *r;
10860 optind = 0;
10861 while (optind < argc) {
10863 r = argv[optind++];
10864 if (r[0] != '-' || r[1] == '\0') {
10865 /* add a new file */
10866 dynarray_add((void ***)&files, &nb_files, r);
10867 if (!multiple_files) {
10868 optind--;
10869 /* argv[0] will be this file */
10870 break;
10872 } else {
10873 /* find option in table (match only the first chars */
10874 popt = tcc_options;
10875 for(;;) {
10876 p1 = popt->name;
10877 if (p1 == NULL)
10878 error("invalid option -- '%s'", r);
10879 r1 = r + 1;
10880 for(;;) {
10881 if (*p1 == '\0')
10882 goto option_found;
10883 if (*r1 != *p1)
10884 break;
10885 p1++;
10886 r1++;
10888 popt++;
10890 option_found:
10891 if (popt->flags & TCC_OPTION_HAS_ARG) {
10892 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
10893 optarg = r1;
10894 } else {
10895 if (optind >= argc)
10896 error("argument to '%s' is missing", r);
10897 optarg = argv[optind++];
10899 } else {
10900 if (*r1 != '\0')
10901 return 0;
10902 optarg = NULL;
10905 switch(popt->index) {
10906 case TCC_OPTION_HELP:
10907 return 0;
10909 case TCC_OPTION_I:
10910 if (tcc_add_include_path(s, optarg) < 0)
10911 error("too many include paths");
10912 break;
10913 case TCC_OPTION_D:
10915 char *sym, *value;
10916 sym = (char *)optarg;
10917 value = strchr(sym, '=');
10918 if (value) {
10919 *value = '\0';
10920 value++;
10922 tcc_define_symbol(s, sym, value);
10924 break;
10925 case TCC_OPTION_U:
10926 tcc_undefine_symbol(s, optarg);
10927 break;
10928 case TCC_OPTION_L:
10929 tcc_add_library_path(s, optarg);
10930 break;
10931 case TCC_OPTION_B:
10932 /* set tcc utilities path (mainly for tcc development) */
10933 tcc_lib_path = optarg;
10934 break;
10935 case TCC_OPTION_l:
10936 dynarray_add((void ***)&files, &nb_files, r);
10937 nb_libraries++;
10938 break;
10939 case TCC_OPTION_bench:
10940 do_bench = 1;
10941 break;
10942 case TCC_OPTION_bt:
10943 num_callers = atoi(optarg);
10944 break;
10945 #ifdef CONFIG_TCC_BCHECK
10946 case TCC_OPTION_b:
10947 do_bounds_check = 1;
10948 do_debug = 1;
10949 break;
10950 #endif
10951 case TCC_OPTION_g:
10952 do_debug = 1;
10953 break;
10954 case TCC_OPTION_c:
10955 multiple_files = 1;
10956 output_type = TCC_OUTPUT_OBJ;
10957 break;
10958 case TCC_OPTION_static:
10959 s->static_link = 1;
10960 break;
10961 case TCC_OPTION_shared:
10962 output_type = TCC_OUTPUT_DLL;
10963 break;
10964 case TCC_OPTION_soname:
10965 s->soname = optarg;
10966 break;
10967 case TCC_OPTION_o:
10968 multiple_files = 1;
10969 outfile = optarg;
10970 break;
10971 case TCC_OPTION_r:
10972 /* generate a .o merging several output files */
10973 reloc_output = 1;
10974 output_type = TCC_OUTPUT_OBJ;
10975 break;
10976 case TCC_OPTION_nostdinc:
10977 s->nostdinc = 1;
10978 break;
10979 case TCC_OPTION_nostdlib:
10980 s->nostdlib = 1;
10981 break;
10982 case TCC_OPTION_print_search_dirs:
10983 print_search_dirs = 1;
10984 break;
10985 case TCC_OPTION_run:
10987 int argc1;
10988 char **argv1;
10989 argc1 = expand_args(&argv1, optarg);
10990 if (argc1 > 0) {
10991 parse_args(s, argc1, argv1);
10993 multiple_files = 0;
10994 output_type = TCC_OUTPUT_MEMORY;
10996 break;
10997 case TCC_OPTION_v:
10998 do {
10999 if (0 == verbose++)
11000 printf("tcc version %s\n", TCC_VERSION);
11001 } while (*optarg++ == 'v');
11002 break;
11003 case TCC_OPTION_f:
11004 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
11005 goto unsupported_option;
11006 break;
11007 case TCC_OPTION_W:
11008 if (tcc_set_warning(s, optarg, 1) < 0 &&
11009 s->warn_unsupported)
11010 goto unsupported_option;
11011 break;
11012 case TCC_OPTION_w:
11013 s->warn_none = 1;
11014 break;
11015 case TCC_OPTION_rdynamic:
11016 s->rdynamic = 1;
11017 break;
11018 case TCC_OPTION_Wl:
11020 const char *p;
11021 if (strstart(optarg, "-Ttext,", &p)) {
11022 s->text_addr = strtoul(p, NULL, 16);
11023 s->has_text_addr = 1;
11024 } else if (strstart(optarg, "--oformat,", &p)) {
11025 if (strstart(p, "elf32-", NULL)) {
11026 s->output_format = TCC_OUTPUT_FORMAT_ELF;
11027 } else if (!strcmp(p, "binary")) {
11028 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
11029 } else
11030 #ifdef TCC_TARGET_COFF
11031 if (!strcmp(p, "coff")) {
11032 s->output_format = TCC_OUTPUT_FORMAT_COFF;
11033 } else
11034 #endif
11036 error("target %s not found", p);
11038 } else {
11039 error("unsupported linker option '%s'", optarg);
11042 break;
11043 case TCC_OPTION_E:
11044 output_type = TCC_OUTPUT_PREPROCESS;
11045 break;
11046 default:
11047 if (s->warn_unsupported) {
11048 unsupported_option:
11049 warning("unsupported option '%s'", r);
11051 break;
11055 return optind + 1;
11058 int main(int argc, char **argv)
11060 int i;
11061 TCCState *s;
11062 int nb_objfiles, ret, optind;
11063 char objfilename[1024];
11064 int64_t start_time = 0;
11066 #ifdef _WIN32
11067 tcc_lib_path = w32_tcc_lib_path();
11068 #endif
11070 s = tcc_new();
11071 output_type = TCC_OUTPUT_EXE;
11072 outfile = NULL;
11073 multiple_files = 1;
11074 files = NULL;
11075 nb_files = 0;
11076 nb_libraries = 0;
11077 reloc_output = 0;
11078 print_search_dirs = 0;
11079 ret = 0;
11081 optind = parse_args(s, argc - 1, argv + 1);
11082 if (print_search_dirs) {
11083 /* enough for Linux kernel */
11084 printf("install: %s/\n", tcc_lib_path);
11085 return 0;
11087 if (optind == 0 || nb_files == 0) {
11088 if (optind && verbose)
11089 return 0;
11090 help();
11091 return 1;
11094 nb_objfiles = nb_files - nb_libraries;
11096 /* if outfile provided without other options, we output an
11097 executable */
11098 if (outfile && output_type == TCC_OUTPUT_MEMORY)
11099 output_type = TCC_OUTPUT_EXE;
11101 /* check -c consistency : only single file handled. XXX: checks file type */
11102 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
11103 /* accepts only a single input file */
11104 if (nb_objfiles != 1)
11105 error("cannot specify multiple files with -c");
11106 if (nb_libraries != 0)
11107 error("cannot specify libraries with -c");
11111 if (output_type == TCC_OUTPUT_PREPROCESS) {
11112 if (!outfile) {
11113 s->outfile = stdout;
11114 } else {
11115 s->outfile = fopen(outfile, "w");
11116 if (!s->outfile)
11117 error("could not open '%s", outfile);
11119 } else if (output_type != TCC_OUTPUT_MEMORY) {
11120 if (!outfile) {
11121 /* compute default outfile name */
11122 char *ext;
11123 const char *name =
11124 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
11125 pstrcpy(objfilename, sizeof(objfilename), name);
11126 ext = tcc_fileextension(objfilename);
11127 #ifdef TCC_TARGET_PE
11128 if (output_type == TCC_OUTPUT_DLL)
11129 strcpy(ext, ".dll");
11130 else
11131 if (output_type == TCC_OUTPUT_EXE)
11132 strcpy(ext, ".exe");
11133 else
11134 #endif
11135 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
11136 strcpy(ext, ".o");
11137 else
11138 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11139 outfile = objfilename;
11143 if (do_bench) {
11144 start_time = getclock_us();
11147 tcc_set_output_type(s, output_type);
11149 /* compile or add each files or library */
11150 for(i = 0; i < nb_files && ret == 0; i++) {
11151 const char *filename;
11153 filename = files[i];
11154 if (output_type == TCC_OUTPUT_PREPROCESS) {
11155 if (tcc_add_file_internal(s, filename,
11156 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11157 ret = 1;
11158 } else if (filename[0] == '-' && filename[1]) {
11159 if (tcc_add_library(s, filename + 2) < 0)
11160 error("cannot find %s", filename);
11161 } else {
11162 if (1 == verbose)
11163 printf("-> %s\n", filename);
11164 if (tcc_add_file(s, filename) < 0)
11165 ret = 1;
11169 /* free all files */
11170 tcc_free(files);
11172 if (ret)
11173 goto the_end;
11175 if (do_bench) {
11176 double total_time;
11177 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11178 if (total_time < 0.001)
11179 total_time = 0.001;
11180 if (total_bytes < 1)
11181 total_bytes = 1;
11182 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11183 tok_ident - TOK_IDENT, total_lines, total_bytes,
11184 total_time, (int)(total_lines / total_time),
11185 total_bytes / total_time / 1000000.0);
11188 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11189 if (outfile)
11190 fclose(s->outfile);
11191 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11192 ret = tcc_run(s, argc - optind, argv + optind);
11193 } else
11194 ret = tcc_output_file(s, outfile) ? 1 : 0;
11195 the_end:
11196 /* XXX: cannot do it with bound checking because of the malloc hooks */
11197 if (!do_bounds_check)
11198 tcc_delete(s);
11200 #ifdef MEM_DEBUG
11201 if (do_bench) {
11202 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11204 #endif
11205 return ret;
11208 #endif