Fix get_tok_str wrt wide characters
[tinycc/k1w1.git] / tcc.c
blobb0e86cda90bbe81410b059515f6853c1868d4084
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 int r; /* associated register */
188 int 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_DYNSYM:
1265 case SHT_SYMTAB:
1266 case SHT_DYNAMIC:
1267 sec->sh_addralign = 4;
1268 break;
1269 case SHT_STRTAB:
1270 sec->sh_addralign = 1;
1271 break;
1272 default:
1273 sec->sh_addralign = 32; /* default conservative alignment */
1274 break;
1277 /* only add section if not private */
1278 if (!(sh_flags & SHF_PRIVATE)) {
1279 sec->sh_num = s1->nb_sections;
1280 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1282 return sec;
1285 static void free_section(Section *s)
1287 if (s->link && (s->link->sh_flags & SHF_PRIVATE))
1288 free_section(s->link);
1289 if (s->hash && (s->hash->sh_flags & SHF_PRIVATE))
1290 s->hash->link = NULL, free_section(s->hash);
1291 tcc_free(s->data);
1292 tcc_free(s);
1295 /* realloc section and set its content to zero */
1296 static void section_realloc(Section *sec, unsigned long new_size)
1298 unsigned long size;
1299 unsigned char *data;
1301 size = sec->data_allocated;
1302 if (size == 0)
1303 size = 1;
1304 while (size < new_size)
1305 size = size * 2;
1306 data = tcc_realloc(sec->data, size);
1307 if (!data)
1308 error("memory full");
1309 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1310 sec->data = data;
1311 sec->data_allocated = size;
1314 /* reserve at least 'size' bytes in section 'sec' from
1315 sec->data_offset. */
1316 static void *section_ptr_add(Section *sec, unsigned long size)
1318 unsigned long offset, offset1;
1320 offset = sec->data_offset;
1321 offset1 = offset + size;
1322 if (offset1 > sec->data_allocated)
1323 section_realloc(sec, offset1);
1324 sec->data_offset = offset1;
1325 return sec->data + offset;
1328 /* return a reference to a section, and create it if it does not
1329 exists */
1330 Section *find_section(TCCState *s1, const char *name)
1332 Section *sec;
1333 int i;
1334 for(i = 1; i < s1->nb_sections; i++) {
1335 sec = s1->sections[i];
1336 if (!strcmp(name, sec->name))
1337 return sec;
1339 /* sections are created as PROGBITS */
1340 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1343 #define SECTION_ABS ((void *)1)
1345 /* update sym->c so that it points to an external symbol in section
1346 'section' with value 'value' */
1347 static void put_extern_sym2(Sym *sym, Section *section,
1348 unsigned long value, unsigned long size,
1349 int can_add_underscore)
1351 int sym_type, sym_bind, sh_num, info, other, attr;
1352 Elf32_Sym *esym;
1353 const char *name;
1354 char buf1[256];
1356 if (section == NULL)
1357 sh_num = SHN_UNDEF;
1358 else if (section == SECTION_ABS)
1359 sh_num = SHN_ABS;
1360 else
1361 sh_num = section->sh_num;
1363 other = attr = 0;
1365 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1366 sym_type = STT_FUNC;
1367 #ifdef TCC_TARGET_PE
1368 if (sym->type.ref)
1369 attr = sym->type.ref->r;
1370 if (FUNC_EXPORT(attr))
1371 other |= 1;
1372 if (FUNC_CALL(attr) == FUNC_STDCALL)
1373 other |= 2;
1374 #endif
1375 } else {
1376 sym_type = STT_OBJECT;
1379 if (sym->type.t & VT_STATIC)
1380 sym_bind = STB_LOCAL;
1381 else
1382 sym_bind = STB_GLOBAL;
1384 if (!sym->c) {
1385 name = get_tok_str(sym->v, NULL);
1386 #ifdef CONFIG_TCC_BCHECK
1387 if (do_bounds_check) {
1388 char buf[32];
1390 /* XXX: avoid doing that for statics ? */
1391 /* if bound checking is activated, we change some function
1392 names by adding the "__bound" prefix */
1393 switch(sym->v) {
1394 #if 0
1395 /* XXX: we rely only on malloc hooks */
1396 case TOK_malloc:
1397 case TOK_free:
1398 case TOK_realloc:
1399 case TOK_memalign:
1400 case TOK_calloc:
1401 #endif
1402 case TOK_memcpy:
1403 case TOK_memmove:
1404 case TOK_memset:
1405 case TOK_strlen:
1406 case TOK_strcpy:
1407 case TOK__alloca:
1408 strcpy(buf, "__bound_");
1409 strcat(buf, name);
1410 name = buf;
1411 break;
1414 #endif
1416 #ifdef TCC_TARGET_PE
1417 if ((other & 2) && can_add_underscore) {
1418 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1419 name = buf1;
1420 } else
1421 #endif
1422 if (tcc_state->leading_underscore && can_add_underscore) {
1423 buf1[0] = '_';
1424 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1425 name = buf1;
1427 info = ELF32_ST_INFO(sym_bind, sym_type);
1428 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1429 } else {
1430 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1431 esym->st_value = value;
1432 esym->st_size = size;
1433 esym->st_shndx = sh_num;
1434 esym->st_other |= other;
1438 static void put_extern_sym(Sym *sym, Section *section,
1439 unsigned long value, unsigned long size)
1441 put_extern_sym2(sym, section, value, size, 1);
1444 /* add a new relocation entry to symbol 'sym' in section 's' */
1445 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1447 if (!sym->c)
1448 put_extern_sym(sym, NULL, 0, 0);
1449 /* now we can add ELF relocation info */
1450 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1453 static inline int isid(int c)
1455 return (c >= 'a' && c <= 'z') ||
1456 (c >= 'A' && c <= 'Z') ||
1457 c == '_';
1460 static inline int isnum(int c)
1462 return c >= '0' && c <= '9';
1465 static inline int isoct(int c)
1467 return c >= '0' && c <= '7';
1470 static inline int toup(int c)
1472 if (c >= 'a' && c <= 'z')
1473 return c - 'a' + 'A';
1474 else
1475 return c;
1478 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1480 int len;
1481 len = strlen(buf);
1482 vsnprintf(buf + len, buf_size - len, fmt, ap);
1485 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1487 va_list ap;
1488 va_start(ap, fmt);
1489 strcat_vprintf(buf, buf_size, fmt, ap);
1490 va_end(ap);
1493 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1495 char buf[2048];
1496 BufferedFile **f;
1498 buf[0] = '\0';
1499 if (file) {
1500 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1501 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1502 (*f)->filename, (*f)->line_num);
1503 if (file->line_num > 0) {
1504 strcat_printf(buf, sizeof(buf),
1505 "%s:%d: ", file->filename, file->line_num);
1506 } else {
1507 strcat_printf(buf, sizeof(buf),
1508 "%s: ", file->filename);
1510 } else {
1511 strcat_printf(buf, sizeof(buf),
1512 "tcc: ");
1514 if (is_warning)
1515 strcat_printf(buf, sizeof(buf), "warning: ");
1516 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1518 if (!s1->error_func) {
1519 /* default case: stderr */
1520 fprintf(stderr, "%s\n", buf);
1521 } else {
1522 s1->error_func(s1->error_opaque, buf);
1524 if (!is_warning || s1->warn_error)
1525 s1->nb_errors++;
1528 #ifdef LIBTCC
1529 void tcc_set_error_func(TCCState *s, void *error_opaque,
1530 void (*error_func)(void *opaque, const char *msg))
1532 s->error_opaque = error_opaque;
1533 s->error_func = error_func;
1535 #endif
1537 /* error without aborting current compilation */
1538 void error_noabort(const char *fmt, ...)
1540 TCCState *s1 = tcc_state;
1541 va_list ap;
1543 va_start(ap, fmt);
1544 error1(s1, 0, fmt, ap);
1545 va_end(ap);
1548 void error(const char *fmt, ...)
1550 TCCState *s1 = tcc_state;
1551 va_list ap;
1553 va_start(ap, fmt);
1554 error1(s1, 0, fmt, ap);
1555 va_end(ap);
1556 /* better than nothing: in some cases, we accept to handle errors */
1557 if (s1->error_set_jmp_enabled) {
1558 longjmp(s1->error_jmp_buf, 1);
1559 } else {
1560 /* XXX: eliminate this someday */
1561 exit(1);
1565 void expect(const char *msg)
1567 error("%s expected", msg);
1570 void warning(const char *fmt, ...)
1572 TCCState *s1 = tcc_state;
1573 va_list ap;
1575 if (s1->warn_none)
1576 return;
1578 va_start(ap, fmt);
1579 error1(s1, 1, fmt, ap);
1580 va_end(ap);
1583 void skip(int c)
1585 if (tok != c)
1586 error("'%c' expected", c);
1587 next();
1590 static void test_lvalue(void)
1592 if (!(vtop->r & VT_LVAL))
1593 expect("lvalue");
1596 /* allocate a new token */
1597 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1599 TokenSym *ts, **ptable;
1600 int i;
1602 if (tok_ident >= SYM_FIRST_ANOM)
1603 error("memory full");
1605 /* expand token table if needed */
1606 i = tok_ident - TOK_IDENT;
1607 if ((i % TOK_ALLOC_INCR) == 0) {
1608 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1609 if (!ptable)
1610 error("memory full");
1611 table_ident = ptable;
1614 ts = tcc_malloc(sizeof(TokenSym) + len);
1615 table_ident[i] = ts;
1616 ts->tok = tok_ident++;
1617 ts->sym_define = NULL;
1618 ts->sym_label = NULL;
1619 ts->sym_struct = NULL;
1620 ts->sym_identifier = NULL;
1621 ts->len = len;
1622 ts->hash_next = NULL;
1623 memcpy(ts->str, str, len);
1624 ts->str[len] = '\0';
1625 *pts = ts;
1626 return ts;
1629 #define TOK_HASH_INIT 1
1630 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1632 /* find a token and add it if not found */
1633 static TokenSym *tok_alloc(const char *str, int len)
1635 TokenSym *ts, **pts;
1636 int i;
1637 unsigned int h;
1639 h = TOK_HASH_INIT;
1640 for(i=0;i<len;i++)
1641 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1642 h &= (TOK_HASH_SIZE - 1);
1644 pts = &hash_ident[h];
1645 for(;;) {
1646 ts = *pts;
1647 if (!ts)
1648 break;
1649 if (ts->len == len && !memcmp(ts->str, str, len))
1650 return ts;
1651 pts = &(ts->hash_next);
1653 return tok_alloc_new(pts, str, len);
1656 /* CString handling */
1658 static void cstr_realloc(CString *cstr, int new_size)
1660 int size;
1661 void *data;
1663 size = cstr->size_allocated;
1664 if (size == 0)
1665 size = 8; /* no need to allocate a too small first string */
1666 while (size < new_size)
1667 size = size * 2;
1668 data = tcc_realloc(cstr->data_allocated, size);
1669 if (!data)
1670 error("memory full");
1671 cstr->data_allocated = data;
1672 cstr->size_allocated = size;
1673 cstr->data = data;
1676 /* add a byte */
1677 static inline void cstr_ccat(CString *cstr, int ch)
1679 int size;
1680 size = cstr->size + 1;
1681 if (size > cstr->size_allocated)
1682 cstr_realloc(cstr, size);
1683 ((unsigned char *)cstr->data)[size - 1] = ch;
1684 cstr->size = size;
1687 static void cstr_cat(CString *cstr, const char *str)
1689 int c;
1690 for(;;) {
1691 c = *str;
1692 if (c == '\0')
1693 break;
1694 cstr_ccat(cstr, c);
1695 str++;
1699 /* add a wide char */
1700 static void cstr_wccat(CString *cstr, int ch)
1702 int size;
1703 size = cstr->size + sizeof(nwchar_t);
1704 if (size > cstr->size_allocated)
1705 cstr_realloc(cstr, size);
1706 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
1707 cstr->size = size;
1710 static void cstr_new(CString *cstr)
1712 memset(cstr, 0, sizeof(CString));
1715 /* free string and reset it to NULL */
1716 static void cstr_free(CString *cstr)
1718 tcc_free(cstr->data_allocated);
1719 cstr_new(cstr);
1722 #define cstr_reset(cstr) cstr_free(cstr)
1724 /* XXX: unicode ? */
1725 static void add_char(CString *cstr, int c)
1727 if (c == '\'' || c == '\"' || c == '\\') {
1728 /* XXX: could be more precise if char or string */
1729 cstr_ccat(cstr, '\\');
1731 if (c >= 32 && c <= 126) {
1732 cstr_ccat(cstr, c);
1733 } else {
1734 cstr_ccat(cstr, '\\');
1735 if (c == '\n') {
1736 cstr_ccat(cstr, 'n');
1737 } else {
1738 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1739 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1740 cstr_ccat(cstr, '0' + (c & 7));
1745 /* XXX: buffer overflow */
1746 /* XXX: float tokens */
1747 char *get_tok_str(int v, CValue *cv)
1749 static char buf[STRING_MAX_SIZE + 1];
1750 static CString cstr_buf;
1751 CString *cstr;
1752 unsigned char *q;
1753 char *p;
1754 int i, len;
1756 /* NOTE: to go faster, we give a fixed buffer for small strings */
1757 cstr_reset(&cstr_buf);
1758 cstr_buf.data = buf;
1759 cstr_buf.size_allocated = sizeof(buf);
1760 p = buf;
1762 switch(v) {
1763 case TOK_CINT:
1764 case TOK_CUINT:
1765 /* XXX: not quite exact, but only useful for testing */
1766 sprintf(p, "%u", cv->ui);
1767 break;
1768 case TOK_CLLONG:
1769 case TOK_CULLONG:
1770 /* XXX: not quite exact, but only useful for testing */
1771 sprintf(p, "%Lu", cv->ull);
1772 break;
1773 case TOK_LCHAR:
1774 cstr_ccat(&cstr_buf, 'L');
1775 case TOK_CCHAR:
1776 cstr_ccat(&cstr_buf, '\'');
1777 add_char(&cstr_buf, cv->i);
1778 cstr_ccat(&cstr_buf, '\'');
1779 cstr_ccat(&cstr_buf, '\0');
1780 break;
1781 case TOK_PPNUM:
1782 cstr = cv->cstr;
1783 len = cstr->size - 1;
1784 for(i=0;i<len;i++)
1785 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1786 cstr_ccat(&cstr_buf, '\0');
1787 break;
1788 case TOK_LSTR:
1789 cstr_ccat(&cstr_buf, 'L');
1790 case TOK_STR:
1791 cstr = cv->cstr;
1792 cstr_ccat(&cstr_buf, '\"');
1793 if (v == TOK_STR) {
1794 len = cstr->size - 1;
1795 for(i=0;i<len;i++)
1796 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1797 } else {
1798 len = (cstr->size / sizeof(nwchar_t)) - 1;
1799 for(i=0;i<len;i++)
1800 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1802 cstr_ccat(&cstr_buf, '\"');
1803 cstr_ccat(&cstr_buf, '\0');
1804 break;
1805 case TOK_LT:
1806 v = '<';
1807 goto addv;
1808 case TOK_GT:
1809 v = '>';
1810 goto addv;
1811 case TOK_DOTS:
1812 return strcpy(p, "...");
1813 case TOK_A_SHL:
1814 return strcpy(p, "<<=");
1815 case TOK_A_SAR:
1816 return strcpy(p, ">>=");
1817 default:
1818 if (v < TOK_IDENT) {
1819 /* search in two bytes table */
1820 q = tok_two_chars;
1821 while (*q) {
1822 if (q[2] == v) {
1823 *p++ = q[0];
1824 *p++ = q[1];
1825 *p = '\0';
1826 return buf;
1828 q += 3;
1830 addv:
1831 *p++ = v;
1832 *p = '\0';
1833 } else if (v < tok_ident) {
1834 return table_ident[v - TOK_IDENT]->str;
1835 } else if (v >= SYM_FIRST_ANOM) {
1836 /* special name for anonymous symbol */
1837 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1838 } else {
1839 /* should never happen */
1840 return NULL;
1842 break;
1844 return cstr_buf.data;
1847 /* push, without hashing */
1848 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1850 Sym *s;
1851 s = sym_malloc();
1852 s->v = v;
1853 s->type.t = t;
1854 s->c = c;
1855 s->next = NULL;
1856 /* add in stack */
1857 s->prev = *ps;
1858 *ps = s;
1859 return s;
1862 /* find a symbol and return its associated structure. 's' is the top
1863 of the symbol stack */
1864 static Sym *sym_find2(Sym *s, int v)
1866 while (s) {
1867 if (s->v == v)
1868 return s;
1869 s = s->prev;
1871 return NULL;
1874 /* structure lookup */
1875 static inline Sym *struct_find(int v)
1877 v -= TOK_IDENT;
1878 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1879 return NULL;
1880 return table_ident[v]->sym_struct;
1883 /* find an identifier */
1884 static inline Sym *sym_find(int v)
1886 v -= TOK_IDENT;
1887 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1888 return NULL;
1889 return table_ident[v]->sym_identifier;
1892 /* push a given symbol on the symbol stack */
1893 static Sym *sym_push(int v, CType *type, int r, int c)
1895 Sym *s, **ps;
1896 TokenSym *ts;
1898 if (local_stack)
1899 ps = &local_stack;
1900 else
1901 ps = &global_stack;
1902 s = sym_push2(ps, v, type->t, c);
1903 s->type.ref = type->ref;
1904 s->r = r;
1905 /* don't record fields or anonymous symbols */
1906 /* XXX: simplify */
1907 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1908 /* record symbol in token array */
1909 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1910 if (v & SYM_STRUCT)
1911 ps = &ts->sym_struct;
1912 else
1913 ps = &ts->sym_identifier;
1914 s->prev_tok = *ps;
1915 *ps = s;
1917 return s;
1920 /* push a global identifier */
1921 static Sym *global_identifier_push(int v, int t, int c)
1923 Sym *s, **ps;
1924 s = sym_push2(&global_stack, v, t, c);
1925 /* don't record anonymous symbol */
1926 if (v < SYM_FIRST_ANOM) {
1927 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1928 /* modify the top most local identifier, so that
1929 sym_identifier will point to 's' when popped */
1930 while (*ps != NULL)
1931 ps = &(*ps)->prev_tok;
1932 s->prev_tok = NULL;
1933 *ps = s;
1935 return s;
1938 /* pop symbols until top reaches 'b' */
1939 static void sym_pop(Sym **ptop, Sym *b)
1941 Sym *s, *ss, **ps;
1942 TokenSym *ts;
1943 int v;
1945 s = *ptop;
1946 while(s != b) {
1947 ss = s->prev;
1948 v = s->v;
1949 /* remove symbol in token array */
1950 /* XXX: simplify */
1951 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1952 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1953 if (v & SYM_STRUCT)
1954 ps = &ts->sym_struct;
1955 else
1956 ps = &ts->sym_identifier;
1957 *ps = s->prev_tok;
1959 sym_free(s);
1960 s = ss;
1962 *ptop = b;
1965 /* I/O layer */
1967 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1969 int fd;
1970 BufferedFile *bf;
1972 if (strcmp(filename, "-") == 0)
1973 fd = 0, filename = "stdin";
1974 else
1975 fd = open(filename, O_RDONLY | O_BINARY);
1976 if ((verbose == 2 && fd >= 0) || verbose == 3)
1977 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
1978 (s1->include_stack_ptr - s1->include_stack), "", filename);
1979 if (fd < 0)
1980 return NULL;
1981 bf = tcc_malloc(sizeof(BufferedFile));
1982 bf->fd = fd;
1983 bf->buf_ptr = bf->buffer;
1984 bf->buf_end = bf->buffer;
1985 bf->buffer[0] = CH_EOB; /* put eob symbol */
1986 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1987 #ifdef _WIN32
1988 normalize_slashes(bf->filename);
1989 #endif
1990 bf->line_num = 1;
1991 bf->ifndef_macro = 0;
1992 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1993 // printf("opening '%s'\n", filename);
1994 return bf;
1997 void tcc_close(BufferedFile *bf)
1999 total_lines += bf->line_num;
2000 close(bf->fd);
2001 tcc_free(bf);
2004 /* fill input buffer and peek next char */
2005 static int tcc_peekc_slow(BufferedFile *bf)
2007 int len;
2008 /* only tries to read if really end of buffer */
2009 if (bf->buf_ptr >= bf->buf_end) {
2010 if (bf->fd != -1) {
2011 #if defined(PARSE_DEBUG)
2012 len = 8;
2013 #else
2014 len = IO_BUF_SIZE;
2015 #endif
2016 len = read(bf->fd, bf->buffer, len);
2017 if (len < 0)
2018 len = 0;
2019 } else {
2020 len = 0;
2022 total_bytes += len;
2023 bf->buf_ptr = bf->buffer;
2024 bf->buf_end = bf->buffer + len;
2025 *bf->buf_end = CH_EOB;
2027 if (bf->buf_ptr < bf->buf_end) {
2028 return bf->buf_ptr[0];
2029 } else {
2030 bf->buf_ptr = bf->buf_end;
2031 return CH_EOF;
2035 /* return the current character, handling end of block if necessary
2036 (but not stray) */
2037 static int handle_eob(void)
2039 return tcc_peekc_slow(file);
2042 /* read next char from current input file and handle end of input buffer */
2043 static inline void inp(void)
2045 ch = *(++(file->buf_ptr));
2046 /* end of buffer/file handling */
2047 if (ch == CH_EOB)
2048 ch = handle_eob();
2051 /* handle '\[\r]\n' */
2052 static int handle_stray_noerror(void)
2054 while (ch == '\\') {
2055 inp();
2056 if (ch == '\n') {
2057 file->line_num++;
2058 inp();
2059 } else if (ch == '\r') {
2060 inp();
2061 if (ch != '\n')
2062 goto fail;
2063 file->line_num++;
2064 inp();
2065 } else {
2066 fail:
2067 return 1;
2070 return 0;
2073 static void handle_stray(void)
2075 if (handle_stray_noerror())
2076 error("stray '\\' in program");
2079 /* skip the stray and handle the \\n case. Output an error if
2080 incorrect char after the stray */
2081 static int handle_stray1(uint8_t *p)
2083 int c;
2085 if (p >= file->buf_end) {
2086 file->buf_ptr = p;
2087 c = handle_eob();
2088 p = file->buf_ptr;
2089 if (c == '\\')
2090 goto parse_stray;
2091 } else {
2092 parse_stray:
2093 file->buf_ptr = p;
2094 ch = *p;
2095 handle_stray();
2096 p = file->buf_ptr;
2097 c = *p;
2099 return c;
2102 /* handle just the EOB case, but not stray */
2103 #define PEEKC_EOB(c, p)\
2105 p++;\
2106 c = *p;\
2107 if (c == '\\') {\
2108 file->buf_ptr = p;\
2109 c = handle_eob();\
2110 p = file->buf_ptr;\
2114 /* handle the complicated stray case */
2115 #define PEEKC(c, p)\
2117 p++;\
2118 c = *p;\
2119 if (c == '\\') {\
2120 c = handle_stray1(p);\
2121 p = file->buf_ptr;\
2125 /* input with '\[\r]\n' handling. Note that this function cannot
2126 handle other characters after '\', so you cannot call it inside
2127 strings or comments */
2128 static void minp(void)
2130 inp();
2131 if (ch == '\\')
2132 handle_stray();
2136 /* single line C++ comments */
2137 static uint8_t *parse_line_comment(uint8_t *p)
2139 int c;
2141 p++;
2142 for(;;) {
2143 c = *p;
2144 redo:
2145 if (c == '\n' || c == CH_EOF) {
2146 break;
2147 } else if (c == '\\') {
2148 file->buf_ptr = p;
2149 c = handle_eob();
2150 p = file->buf_ptr;
2151 if (c == '\\') {
2152 PEEKC_EOB(c, p);
2153 if (c == '\n') {
2154 file->line_num++;
2155 PEEKC_EOB(c, p);
2156 } else if (c == '\r') {
2157 PEEKC_EOB(c, p);
2158 if (c == '\n') {
2159 file->line_num++;
2160 PEEKC_EOB(c, p);
2163 } else {
2164 goto redo;
2166 } else {
2167 p++;
2170 return p;
2173 /* C comments */
2174 static uint8_t *parse_comment(uint8_t *p)
2176 int c;
2178 p++;
2179 for(;;) {
2180 /* fast skip loop */
2181 for(;;) {
2182 c = *p;
2183 if (c == '\n' || c == '*' || c == '\\')
2184 break;
2185 p++;
2186 c = *p;
2187 if (c == '\n' || c == '*' || c == '\\')
2188 break;
2189 p++;
2191 /* now we can handle all the cases */
2192 if (c == '\n') {
2193 file->line_num++;
2194 p++;
2195 } else if (c == '*') {
2196 p++;
2197 for(;;) {
2198 c = *p;
2199 if (c == '*') {
2200 p++;
2201 } else if (c == '/') {
2202 goto end_of_comment;
2203 } else if (c == '\\') {
2204 file->buf_ptr = p;
2205 c = handle_eob();
2206 p = file->buf_ptr;
2207 if (c == '\\') {
2208 /* skip '\[\r]\n', otherwise just skip the stray */
2209 while (c == '\\') {
2210 PEEKC_EOB(c, p);
2211 if (c == '\n') {
2212 file->line_num++;
2213 PEEKC_EOB(c, p);
2214 } else if (c == '\r') {
2215 PEEKC_EOB(c, p);
2216 if (c == '\n') {
2217 file->line_num++;
2218 PEEKC_EOB(c, p);
2220 } else {
2221 goto after_star;
2225 } else {
2226 break;
2229 after_star: ;
2230 } else {
2231 /* stray, eob or eof */
2232 file->buf_ptr = p;
2233 c = handle_eob();
2234 p = file->buf_ptr;
2235 if (c == CH_EOF) {
2236 error("unexpected end of file in comment");
2237 } else if (c == '\\') {
2238 p++;
2242 end_of_comment:
2243 p++;
2244 return p;
2247 #define cinp minp
2249 /* space exlcuding newline */
2250 static inline int is_space(int ch)
2252 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2255 static inline void skip_spaces(void)
2257 while (is_space(ch))
2258 cinp();
2261 /* parse a string without interpreting escapes */
2262 static uint8_t *parse_pp_string(uint8_t *p,
2263 int sep, CString *str)
2265 int c;
2266 p++;
2267 for(;;) {
2268 c = *p;
2269 if (c == sep) {
2270 break;
2271 } else if (c == '\\') {
2272 file->buf_ptr = p;
2273 c = handle_eob();
2274 p = file->buf_ptr;
2275 if (c == CH_EOF) {
2276 unterminated_string:
2277 /* XXX: indicate line number of start of string */
2278 error("missing terminating %c character", sep);
2279 } else if (c == '\\') {
2280 /* escape : just skip \[\r]\n */
2281 PEEKC_EOB(c, p);
2282 if (c == '\n') {
2283 file->line_num++;
2284 p++;
2285 } else if (c == '\r') {
2286 PEEKC_EOB(c, p);
2287 if (c != '\n')
2288 expect("'\n' after '\r'");
2289 file->line_num++;
2290 p++;
2291 } else if (c == CH_EOF) {
2292 goto unterminated_string;
2293 } else {
2294 if (str) {
2295 cstr_ccat(str, '\\');
2296 cstr_ccat(str, c);
2298 p++;
2301 } else if (c == '\n') {
2302 file->line_num++;
2303 goto add_char;
2304 } else if (c == '\r') {
2305 PEEKC_EOB(c, p);
2306 if (c != '\n') {
2307 if (str)
2308 cstr_ccat(str, '\r');
2309 } else {
2310 file->line_num++;
2311 goto add_char;
2313 } else {
2314 add_char:
2315 if (str)
2316 cstr_ccat(str, c);
2317 p++;
2320 p++;
2321 return p;
2324 /* skip block of text until #else, #elif or #endif. skip also pairs of
2325 #if/#endif */
2326 void preprocess_skip(void)
2328 int a, start_of_line, c, in_warn_or_error;
2329 uint8_t *p;
2331 p = file->buf_ptr;
2332 a = 0;
2333 redo_start:
2334 start_of_line = 1;
2335 in_warn_or_error = 0;
2336 for(;;) {
2337 redo_no_start:
2338 c = *p;
2339 switch(c) {
2340 case ' ':
2341 case '\t':
2342 case '\f':
2343 case '\v':
2344 case '\r':
2345 p++;
2346 goto redo_no_start;
2347 case '\n':
2348 file->line_num++;
2349 p++;
2350 goto redo_start;
2351 case '\\':
2352 file->buf_ptr = p;
2353 c = handle_eob();
2354 if (c == CH_EOF) {
2355 expect("#endif");
2356 } else if (c == '\\') {
2357 ch = file->buf_ptr[0];
2358 handle_stray_noerror();
2360 p = file->buf_ptr;
2361 goto redo_no_start;
2362 /* skip strings */
2363 case '\"':
2364 case '\'':
2365 if (in_warn_or_error)
2366 goto _default;
2367 p = parse_pp_string(p, c, NULL);
2368 break;
2369 /* skip comments */
2370 case '/':
2371 if (in_warn_or_error)
2372 goto _default;
2373 file->buf_ptr = p;
2374 ch = *p;
2375 minp();
2376 p = file->buf_ptr;
2377 if (ch == '*') {
2378 p = parse_comment(p);
2379 } else if (ch == '/') {
2380 p = parse_line_comment(p);
2382 break;
2383 case '#':
2384 p++;
2385 if (start_of_line) {
2386 file->buf_ptr = p;
2387 next_nomacro();
2388 p = file->buf_ptr;
2389 if (a == 0 &&
2390 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2391 goto the_end;
2392 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2393 a++;
2394 else if (tok == TOK_ENDIF)
2395 a--;
2396 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2397 in_warn_or_error = 1;
2399 break;
2400 _default:
2401 default:
2402 p++;
2403 break;
2405 start_of_line = 0;
2407 the_end: ;
2408 file->buf_ptr = p;
2411 /* ParseState handling */
2413 /* XXX: currently, no include file info is stored. Thus, we cannot display
2414 accurate messages if the function or data definition spans multiple
2415 files */
2417 /* save current parse state in 's' */
2418 void save_parse_state(ParseState *s)
2420 s->line_num = file->line_num;
2421 s->macro_ptr = macro_ptr;
2422 s->tok = tok;
2423 s->tokc = tokc;
2426 /* restore parse state from 's' */
2427 void restore_parse_state(ParseState *s)
2429 file->line_num = s->line_num;
2430 macro_ptr = s->macro_ptr;
2431 tok = s->tok;
2432 tokc = s->tokc;
2435 /* return the number of additional 'ints' necessary to store the
2436 token */
2437 static inline int tok_ext_size(int t)
2439 switch(t) {
2440 /* 4 bytes */
2441 case TOK_CINT:
2442 case TOK_CUINT:
2443 case TOK_CCHAR:
2444 case TOK_LCHAR:
2445 case TOK_CFLOAT:
2446 case TOK_LINENUM:
2447 return 1;
2448 case TOK_STR:
2449 case TOK_LSTR:
2450 case TOK_PPNUM:
2451 error("unsupported token");
2452 return 1;
2453 case TOK_CDOUBLE:
2454 case TOK_CLLONG:
2455 case TOK_CULLONG:
2456 return 2;
2457 case TOK_CLDOUBLE:
2458 return LDOUBLE_SIZE / 4;
2459 default:
2460 return 0;
2464 /* token string handling */
2466 static inline void tok_str_new(TokenString *s)
2468 s->str = NULL;
2469 s->len = 0;
2470 s->allocated_len = 0;
2471 s->last_line_num = -1;
2474 static void tok_str_free(int *str)
2476 tcc_free(str);
2479 static int *tok_str_realloc(TokenString *s)
2481 int *str, len;
2483 if (s->allocated_len == 0) {
2484 len = 8;
2485 } else {
2486 len = s->allocated_len * 2;
2488 str = tcc_realloc(s->str, len * sizeof(int));
2489 if (!str)
2490 error("memory full");
2491 s->allocated_len = len;
2492 s->str = str;
2493 return str;
2496 static void tok_str_add(TokenString *s, int t)
2498 int len, *str;
2500 len = s->len;
2501 str = s->str;
2502 if (len >= s->allocated_len)
2503 str = tok_str_realloc(s);
2504 str[len++] = t;
2505 s->len = len;
2508 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2510 int len, *str;
2512 len = s->len;
2513 str = s->str;
2515 /* allocate space for worst case */
2516 if (len + TOK_MAX_SIZE > s->allocated_len)
2517 str = tok_str_realloc(s);
2518 str[len++] = t;
2519 switch(t) {
2520 case TOK_CINT:
2521 case TOK_CUINT:
2522 case TOK_CCHAR:
2523 case TOK_LCHAR:
2524 case TOK_CFLOAT:
2525 case TOK_LINENUM:
2526 str[len++] = cv->tab[0];
2527 break;
2528 case TOK_PPNUM:
2529 case TOK_STR:
2530 case TOK_LSTR:
2532 int nb_words;
2533 CString *cstr;
2535 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2536 while ((len + nb_words) > s->allocated_len)
2537 str = tok_str_realloc(s);
2538 cstr = (CString *)(str + len);
2539 cstr->data = NULL;
2540 cstr->size = cv->cstr->size;
2541 cstr->data_allocated = NULL;
2542 cstr->size_allocated = cstr->size;
2543 memcpy((char *)cstr + sizeof(CString),
2544 cv->cstr->data, cstr->size);
2545 len += nb_words;
2547 break;
2548 case TOK_CDOUBLE:
2549 case TOK_CLLONG:
2550 case TOK_CULLONG:
2551 #if LDOUBLE_SIZE == 8
2552 case TOK_CLDOUBLE:
2553 #endif
2554 str[len++] = cv->tab[0];
2555 str[len++] = cv->tab[1];
2556 break;
2557 #if LDOUBLE_SIZE == 12
2558 case TOK_CLDOUBLE:
2559 str[len++] = cv->tab[0];
2560 str[len++] = cv->tab[1];
2561 str[len++] = cv->tab[2];
2562 #elif LDOUBLE_SIZE != 8
2563 #error add long double size support
2564 #endif
2565 break;
2566 default:
2567 break;
2569 s->len = len;
2572 /* add the current parse token in token string 's' */
2573 static void tok_str_add_tok(TokenString *s)
2575 CValue cval;
2577 /* save line number info */
2578 if (file->line_num != s->last_line_num) {
2579 s->last_line_num = file->line_num;
2580 cval.i = s->last_line_num;
2581 tok_str_add2(s, TOK_LINENUM, &cval);
2583 tok_str_add2(s, tok, &tokc);
2586 #if LDOUBLE_SIZE == 12
2587 #define LDOUBLE_GET(p, cv) \
2588 cv.tab[0] = p[0]; \
2589 cv.tab[1] = p[1]; \
2590 cv.tab[2] = p[2];
2591 #elif LDOUBLE_SIZE == 8
2592 #define LDOUBLE_GET(p, cv) \
2593 cv.tab[0] = p[0]; \
2594 cv.tab[1] = p[1];
2595 #else
2596 #error add long double size support
2597 #endif
2600 /* get a token from an integer array and increment pointer
2601 accordingly. we code it as a macro to avoid pointer aliasing. */
2602 #define TOK_GET(t, p, cv) \
2604 t = *p++; \
2605 switch(t) { \
2606 case TOK_CINT: \
2607 case TOK_CUINT: \
2608 case TOK_CCHAR: \
2609 case TOK_LCHAR: \
2610 case TOK_CFLOAT: \
2611 case TOK_LINENUM: \
2612 cv.tab[0] = *p++; \
2613 break; \
2614 case TOK_STR: \
2615 case TOK_LSTR: \
2616 case TOK_PPNUM: \
2617 cv.cstr = (CString *)p; \
2618 cv.cstr->data = (char *)p + sizeof(CString);\
2619 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2620 break; \
2621 case TOK_CDOUBLE: \
2622 case TOK_CLLONG: \
2623 case TOK_CULLONG: \
2624 cv.tab[0] = p[0]; \
2625 cv.tab[1] = p[1]; \
2626 p += 2; \
2627 break; \
2628 case TOK_CLDOUBLE: \
2629 LDOUBLE_GET(p, cv); \
2630 p += LDOUBLE_SIZE / 4; \
2631 break; \
2632 default: \
2633 break; \
2637 /* defines handling */
2638 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2640 Sym *s;
2642 s = sym_push2(&define_stack, v, macro_type, (int)str);
2643 s->next = first_arg;
2644 table_ident[v - TOK_IDENT]->sym_define = s;
2647 /* undefined a define symbol. Its name is just set to zero */
2648 static void define_undef(Sym *s)
2650 int v;
2651 v = s->v;
2652 if (v >= TOK_IDENT && v < tok_ident)
2653 table_ident[v - TOK_IDENT]->sym_define = NULL;
2654 s->v = 0;
2657 static inline Sym *define_find(int v)
2659 v -= TOK_IDENT;
2660 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2661 return NULL;
2662 return table_ident[v]->sym_define;
2665 /* free define stack until top reaches 'b' */
2666 static void free_defines(Sym *b)
2668 Sym *top, *top1;
2669 int v;
2671 top = define_stack;
2672 while (top != b) {
2673 top1 = top->prev;
2674 /* do not free args or predefined defines */
2675 if (top->c)
2676 tok_str_free((int *)top->c);
2677 v = top->v;
2678 if (v >= TOK_IDENT && v < tok_ident)
2679 table_ident[v - TOK_IDENT]->sym_define = NULL;
2680 sym_free(top);
2681 top = top1;
2683 define_stack = b;
2686 /* label lookup */
2687 static Sym *label_find(int v)
2689 v -= TOK_IDENT;
2690 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2691 return NULL;
2692 return table_ident[v]->sym_label;
2695 static Sym *label_push(Sym **ptop, int v, int flags)
2697 Sym *s, **ps;
2698 s = sym_push2(ptop, v, 0, 0);
2699 s->r = flags;
2700 ps = &table_ident[v - TOK_IDENT]->sym_label;
2701 if (ptop == &global_label_stack) {
2702 /* modify the top most local identifier, so that
2703 sym_identifier will point to 's' when popped */
2704 while (*ps != NULL)
2705 ps = &(*ps)->prev_tok;
2707 s->prev_tok = *ps;
2708 *ps = s;
2709 return s;
2712 /* pop labels until element last is reached. Look if any labels are
2713 undefined. Define symbols if '&&label' was used. */
2714 static void label_pop(Sym **ptop, Sym *slast)
2716 Sym *s, *s1;
2717 for(s = *ptop; s != slast; s = s1) {
2718 s1 = s->prev;
2719 if (s->r == LABEL_DECLARED) {
2720 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2721 } else if (s->r == LABEL_FORWARD) {
2722 error("label '%s' used but not defined",
2723 get_tok_str(s->v, NULL));
2724 } else {
2725 if (s->c) {
2726 /* define corresponding symbol. A size of
2727 1 is put. */
2728 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2731 /* remove label */
2732 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2733 sym_free(s);
2735 *ptop = slast;
2738 /* eval an expression for #if/#elif */
2739 static int expr_preprocess(void)
2741 int c, t;
2742 TokenString str;
2744 tok_str_new(&str);
2745 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2746 next(); /* do macro subst */
2747 if (tok == TOK_DEFINED) {
2748 next_nomacro();
2749 t = tok;
2750 if (t == '(')
2751 next_nomacro();
2752 c = define_find(tok) != 0;
2753 if (t == '(')
2754 next_nomacro();
2755 tok = TOK_CINT;
2756 tokc.i = c;
2757 } else if (tok >= TOK_IDENT) {
2758 /* if undefined macro */
2759 tok = TOK_CINT;
2760 tokc.i = 0;
2762 tok_str_add_tok(&str);
2764 tok_str_add(&str, -1); /* simulate end of file */
2765 tok_str_add(&str, 0);
2766 /* now evaluate C constant expression */
2767 macro_ptr = str.str;
2768 next();
2769 c = expr_const();
2770 macro_ptr = NULL;
2771 tok_str_free(str.str);
2772 return c != 0;
2775 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2776 static void tok_print(int *str)
2778 int t;
2779 CValue cval;
2781 while (1) {
2782 TOK_GET(t, str, cval);
2783 if (!t)
2784 break;
2785 printf(" %s", get_tok_str(t, &cval));
2787 printf("\n");
2789 #endif
2791 /* parse after #define */
2792 static void parse_define(void)
2794 Sym *s, *first, **ps;
2795 int v, t, varg, is_vaargs, c;
2796 TokenString str;
2798 v = tok;
2799 if (v < TOK_IDENT)
2800 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2801 /* XXX: should check if same macro (ANSI) */
2802 first = NULL;
2803 t = MACRO_OBJ;
2804 /* '(' must be just after macro definition for MACRO_FUNC */
2805 c = file->buf_ptr[0];
2806 if (c == '\\')
2807 c = handle_stray1(file->buf_ptr);
2808 if (c == '(') {
2809 next_nomacro();
2810 next_nomacro();
2811 ps = &first;
2812 while (tok != ')') {
2813 varg = tok;
2814 next_nomacro();
2815 is_vaargs = 0;
2816 if (varg == TOK_DOTS) {
2817 varg = TOK___VA_ARGS__;
2818 is_vaargs = 1;
2819 } else if (tok == TOK_DOTS && gnu_ext) {
2820 is_vaargs = 1;
2821 next_nomacro();
2823 if (varg < TOK_IDENT)
2824 error("badly punctuated parameter list");
2825 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2826 *ps = s;
2827 ps = &s->next;
2828 if (tok != ',')
2829 break;
2830 next_nomacro();
2832 t = MACRO_FUNC;
2834 tok_str_new(&str);
2835 next_nomacro();
2836 /* EOF testing necessary for '-D' handling */
2837 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2838 tok_str_add2(&str, tok, &tokc);
2839 next_nomacro();
2841 tok_str_add(&str, 0);
2842 #ifdef PP_DEBUG
2843 printf("define %s %d: ", get_tok_str(v, NULL), t);
2844 tok_print(str.str);
2845 #endif
2846 define_push(v, t, str.str, first);
2849 static inline int hash_cached_include(int type, const char *filename)
2851 const unsigned char *s;
2852 unsigned int h;
2854 h = TOK_HASH_INIT;
2855 h = TOK_HASH_FUNC(h, type);
2856 s = filename;
2857 while (*s) {
2858 h = TOK_HASH_FUNC(h, *s);
2859 s++;
2861 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2862 return h;
2865 /* XXX: use a token or a hash table to accelerate matching ? */
2866 static CachedInclude *search_cached_include(TCCState *s1,
2867 int type, const char *filename)
2869 CachedInclude *e;
2870 int i, h;
2871 h = hash_cached_include(type, filename);
2872 i = s1->cached_includes_hash[h];
2873 for(;;) {
2874 if (i == 0)
2875 break;
2876 e = s1->cached_includes[i - 1];
2877 if (e->type == type && !strcmp(e->filename, filename))
2878 return e;
2879 i = e->hash_next;
2881 return NULL;
2884 static inline void add_cached_include(TCCState *s1, int type,
2885 const char *filename, int ifndef_macro)
2887 CachedInclude *e;
2888 int h;
2890 if (search_cached_include(s1, type, filename))
2891 return;
2892 #ifdef INC_DEBUG
2893 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2894 #endif
2895 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2896 if (!e)
2897 return;
2898 e->type = type;
2899 strcpy(e->filename, filename);
2900 e->ifndef_macro = ifndef_macro;
2901 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2902 /* add in hash table */
2903 h = hash_cached_include(type, filename);
2904 e->hash_next = s1->cached_includes_hash[h];
2905 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2908 static void pragma_parse(TCCState *s1)
2910 int val;
2912 next();
2913 if (tok == TOK_pack) {
2915 This may be:
2916 #pragma pack(1) // set
2917 #pragma pack() // reset to default
2918 #pragma pack(push,1) // push & set
2919 #pragma pack(pop) // restore previous
2921 next();
2922 skip('(');
2923 if (tok == TOK_ASM_pop) {
2924 next();
2925 if (s1->pack_stack_ptr <= s1->pack_stack) {
2926 stk_error:
2927 error("out of pack stack");
2929 s1->pack_stack_ptr--;
2930 } else {
2931 val = 0;
2932 if (tok != ')') {
2933 if (tok == TOK_ASM_push) {
2934 next();
2935 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2936 goto stk_error;
2937 s1->pack_stack_ptr++;
2938 skip(',');
2940 if (tok != TOK_CINT) {
2941 pack_error:
2942 error("invalid pack pragma");
2944 val = tokc.i;
2945 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2946 goto pack_error;
2947 next();
2949 *s1->pack_stack_ptr = val;
2950 skip(')');
2955 /* is_bof is true if first non space token at beginning of file */
2956 static void preprocess(int is_bof)
2958 TCCState *s1 = tcc_state;
2959 int size, i, c, n, saved_parse_flags;
2960 char buf[1024], *q;
2961 char buf1[1024];
2962 BufferedFile *f;
2963 Sym *s;
2964 CachedInclude *e;
2966 saved_parse_flags = parse_flags;
2967 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2968 PARSE_FLAG_LINEFEED;
2969 next_nomacro();
2970 redo:
2971 switch(tok) {
2972 case TOK_DEFINE:
2973 next_nomacro();
2974 parse_define();
2975 break;
2976 case TOK_UNDEF:
2977 next_nomacro();
2978 s = define_find(tok);
2979 /* undefine symbol by putting an invalid name */
2980 if (s)
2981 define_undef(s);
2982 break;
2983 case TOK_INCLUDE:
2984 case TOK_INCLUDE_NEXT:
2985 ch = file->buf_ptr[0];
2986 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2987 skip_spaces();
2988 if (ch == '<') {
2989 c = '>';
2990 goto read_name;
2991 } else if (ch == '\"') {
2992 c = ch;
2993 read_name:
2994 inp();
2995 q = buf;
2996 while (ch != c && ch != '\n' && ch != CH_EOF) {
2997 if ((q - buf) < sizeof(buf) - 1)
2998 *q++ = ch;
2999 if (ch == '\\') {
3000 if (handle_stray_noerror() == 0)
3001 --q;
3002 } else
3003 inp();
3005 *q = '\0';
3006 minp();
3007 #if 0
3008 /* eat all spaces and comments after include */
3009 /* XXX: slightly incorrect */
3010 while (ch1 != '\n' && ch1 != CH_EOF)
3011 inp();
3012 #endif
3013 } else {
3014 /* computed #include : either we have only strings or
3015 we have anything enclosed in '<>' */
3016 next();
3017 buf[0] = '\0';
3018 if (tok == TOK_STR) {
3019 while (tok != TOK_LINEFEED) {
3020 if (tok != TOK_STR) {
3021 include_syntax:
3022 error("'#include' expects \"FILENAME\" or <FILENAME>");
3024 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3025 next();
3027 c = '\"';
3028 } else {
3029 int len;
3030 while (tok != TOK_LINEFEED) {
3031 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3032 next();
3034 len = strlen(buf);
3035 /* check syntax and remove '<>' */
3036 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3037 goto include_syntax;
3038 memmove(buf, buf + 1, len - 2);
3039 buf[len - 2] = '\0';
3040 c = '>';
3044 e = search_cached_include(s1, c, buf);
3045 if (e && define_find(e->ifndef_macro)) {
3046 /* no need to parse the include because the 'ifndef macro'
3047 is defined */
3048 #ifdef INC_DEBUG
3049 printf("%s: skipping %s\n", file->filename, buf);
3050 #endif
3051 } else {
3052 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3053 error("#include recursion too deep");
3054 /* push current file in stack */
3055 /* XXX: fix current line init */
3056 *s1->include_stack_ptr++ = file;
3057 if (c == '\"') {
3058 /* first search in current dir if "header.h" */
3059 size = tcc_basename(file->filename) - file->filename;
3060 if (size > sizeof(buf1) - 1)
3061 size = sizeof(buf1) - 1;
3062 memcpy(buf1, file->filename, size);
3063 buf1[size] = '\0';
3064 pstrcat(buf1, sizeof(buf1), buf);
3065 f = tcc_open(s1, buf1);
3066 if (f) {
3067 if (tok == TOK_INCLUDE_NEXT)
3068 tok = TOK_INCLUDE;
3069 else
3070 goto found;
3073 /* now search in all the include paths */
3074 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3075 for(i = 0; i < n; i++) {
3076 const char *path;
3077 if (i < s1->nb_include_paths)
3078 path = s1->include_paths[i];
3079 else
3080 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3081 pstrcpy(buf1, sizeof(buf1), path);
3082 pstrcat(buf1, sizeof(buf1), "/");
3083 pstrcat(buf1, sizeof(buf1), buf);
3084 f = tcc_open(s1, buf1);
3085 if (f) {
3086 if (tok == TOK_INCLUDE_NEXT)
3087 tok = TOK_INCLUDE;
3088 else
3089 goto found;
3092 --s1->include_stack_ptr;
3093 error("include file '%s' not found", buf);
3094 break;
3095 found:
3096 #ifdef INC_DEBUG
3097 printf("%s: including %s\n", file->filename, buf1);
3098 #endif
3099 f->inc_type = c;
3100 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3101 file = f;
3102 /* add include file debug info */
3103 if (do_debug) {
3104 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3106 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3107 ch = file->buf_ptr[0];
3108 goto the_end;
3110 break;
3111 case TOK_IFNDEF:
3112 c = 1;
3113 goto do_ifdef;
3114 case TOK_IF:
3115 c = expr_preprocess();
3116 goto do_if;
3117 case TOK_IFDEF:
3118 c = 0;
3119 do_ifdef:
3120 next_nomacro();
3121 if (tok < TOK_IDENT)
3122 error("invalid argument for '#if%sdef'", c ? "n" : "");
3123 if (is_bof) {
3124 if (c) {
3125 #ifdef INC_DEBUG
3126 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3127 #endif
3128 file->ifndef_macro = tok;
3131 c = (define_find(tok) != 0) ^ c;
3132 do_if:
3133 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3134 error("memory full");
3135 *s1->ifdef_stack_ptr++ = c;
3136 goto test_skip;
3137 case TOK_ELSE:
3138 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3139 error("#else without matching #if");
3140 if (s1->ifdef_stack_ptr[-1] & 2)
3141 error("#else after #else");
3142 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3143 goto test_skip;
3144 case TOK_ELIF:
3145 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3146 error("#elif without matching #if");
3147 c = s1->ifdef_stack_ptr[-1];
3148 if (c > 1)
3149 error("#elif after #else");
3150 /* last #if/#elif expression was true: we skip */
3151 if (c == 1)
3152 goto skip;
3153 c = expr_preprocess();
3154 s1->ifdef_stack_ptr[-1] = c;
3155 test_skip:
3156 if (!(c & 1)) {
3157 skip:
3158 preprocess_skip();
3159 is_bof = 0;
3160 goto redo;
3162 break;
3163 case TOK_ENDIF:
3164 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3165 error("#endif without matching #if");
3166 s1->ifdef_stack_ptr--;
3167 /* '#ifndef macro' was at the start of file. Now we check if
3168 an '#endif' is exactly at the end of file */
3169 if (file->ifndef_macro &&
3170 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3171 file->ifndef_macro_saved = file->ifndef_macro;
3172 /* need to set to zero to avoid false matches if another
3173 #ifndef at middle of file */
3174 file->ifndef_macro = 0;
3175 while (tok != TOK_LINEFEED)
3176 next_nomacro();
3177 tok_flags |= TOK_FLAG_ENDIF;
3178 goto the_end;
3180 break;
3181 case TOK_LINE:
3182 next();
3183 if (tok != TOK_CINT)
3184 error("#line");
3185 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3186 next();
3187 if (tok != TOK_LINEFEED) {
3188 if (tok != TOK_STR)
3189 error("#line");
3190 pstrcpy(file->filename, sizeof(file->filename),
3191 (char *)tokc.cstr->data);
3193 break;
3194 case TOK_ERROR:
3195 case TOK_WARNING:
3196 c = tok;
3197 ch = file->buf_ptr[0];
3198 skip_spaces();
3199 q = buf;
3200 while (ch != '\n' && ch != CH_EOF) {
3201 if ((q - buf) < sizeof(buf) - 1)
3202 *q++ = ch;
3203 if (ch == '\\') {
3204 if (handle_stray_noerror() == 0)
3205 --q;
3206 } else
3207 inp();
3209 *q = '\0';
3210 if (c == TOK_ERROR)
3211 error("#error %s", buf);
3212 else
3213 warning("#warning %s", buf);
3214 break;
3215 case TOK_PRAGMA:
3216 pragma_parse(s1);
3217 break;
3218 default:
3219 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3220 /* '!' is ignored to allow C scripts. numbers are ignored
3221 to emulate cpp behaviour */
3222 } else {
3223 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3224 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3226 break;
3228 /* ignore other preprocess commands or #! for C scripts */
3229 while (tok != TOK_LINEFEED)
3230 next_nomacro();
3231 the_end:
3232 parse_flags = saved_parse_flags;
3235 /* evaluate escape codes in a string. */
3236 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3238 int c, n;
3239 const uint8_t *p;
3241 p = buf;
3242 for(;;) {
3243 c = *p;
3244 if (c == '\0')
3245 break;
3246 if (c == '\\') {
3247 p++;
3248 /* escape */
3249 c = *p;
3250 switch(c) {
3251 case '0': case '1': case '2': case '3':
3252 case '4': case '5': case '6': case '7':
3253 /* at most three octal digits */
3254 n = c - '0';
3255 p++;
3256 c = *p;
3257 if (isoct(c)) {
3258 n = n * 8 + c - '0';
3259 p++;
3260 c = *p;
3261 if (isoct(c)) {
3262 n = n * 8 + c - '0';
3263 p++;
3266 c = n;
3267 goto add_char_nonext;
3268 case 'x':
3269 case 'u':
3270 case 'U':
3271 p++;
3272 n = 0;
3273 for(;;) {
3274 c = *p;
3275 if (c >= 'a' && c <= 'f')
3276 c = c - 'a' + 10;
3277 else if (c >= 'A' && c <= 'F')
3278 c = c - 'A' + 10;
3279 else if (isnum(c))
3280 c = c - '0';
3281 else
3282 break;
3283 n = n * 16 + c;
3284 p++;
3286 c = n;
3287 goto add_char_nonext;
3288 case 'a':
3289 c = '\a';
3290 break;
3291 case 'b':
3292 c = '\b';
3293 break;
3294 case 'f':
3295 c = '\f';
3296 break;
3297 case 'n':
3298 c = '\n';
3299 break;
3300 case 'r':
3301 c = '\r';
3302 break;
3303 case 't':
3304 c = '\t';
3305 break;
3306 case 'v':
3307 c = '\v';
3308 break;
3309 case 'e':
3310 if (!gnu_ext)
3311 goto invalid_escape;
3312 c = 27;
3313 break;
3314 case '\'':
3315 case '\"':
3316 case '\\':
3317 case '?':
3318 break;
3319 default:
3320 invalid_escape:
3321 if (c >= '!' && c <= '~')
3322 warning("unknown escape sequence: \'\\%c\'", c);
3323 else
3324 warning("unknown escape sequence: \'\\x%x\'", c);
3325 break;
3328 p++;
3329 add_char_nonext:
3330 if (!is_long)
3331 cstr_ccat(outstr, c);
3332 else
3333 cstr_wccat(outstr, c);
3335 /* add a trailing '\0' */
3336 if (!is_long)
3337 cstr_ccat(outstr, '\0');
3338 else
3339 cstr_wccat(outstr, '\0');
3342 /* we use 64 bit numbers */
3343 #define BN_SIZE 2
3345 /* bn = (bn << shift) | or_val */
3346 void bn_lshift(unsigned int *bn, int shift, int or_val)
3348 int i;
3349 unsigned int v;
3350 for(i=0;i<BN_SIZE;i++) {
3351 v = bn[i];
3352 bn[i] = (v << shift) | or_val;
3353 or_val = v >> (32 - shift);
3357 void bn_zero(unsigned int *bn)
3359 int i;
3360 for(i=0;i<BN_SIZE;i++) {
3361 bn[i] = 0;
3365 /* parse number in null terminated string 'p' and return it in the
3366 current token */
3367 void parse_number(const char *p)
3369 int b, t, shift, frac_bits, s, exp_val, ch;
3370 char *q;
3371 unsigned int bn[BN_SIZE];
3372 double d;
3374 /* number */
3375 q = token_buf;
3376 ch = *p++;
3377 t = ch;
3378 ch = *p++;
3379 *q++ = t;
3380 b = 10;
3381 if (t == '.') {
3382 goto float_frac_parse;
3383 } else if (t == '0') {
3384 if (ch == 'x' || ch == 'X') {
3385 q--;
3386 ch = *p++;
3387 b = 16;
3388 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3389 q--;
3390 ch = *p++;
3391 b = 2;
3394 /* parse all digits. cannot check octal numbers at this stage
3395 because of floating point constants */
3396 while (1) {
3397 if (ch >= 'a' && ch <= 'f')
3398 t = ch - 'a' + 10;
3399 else if (ch >= 'A' && ch <= 'F')
3400 t = ch - 'A' + 10;
3401 else if (isnum(ch))
3402 t = ch - '0';
3403 else
3404 break;
3405 if (t >= b)
3406 break;
3407 if (q >= token_buf + STRING_MAX_SIZE) {
3408 num_too_long:
3409 error("number too long");
3411 *q++ = ch;
3412 ch = *p++;
3414 if (ch == '.' ||
3415 ((ch == 'e' || ch == 'E') && b == 10) ||
3416 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3417 if (b != 10) {
3418 /* NOTE: strtox should support that for hexa numbers, but
3419 non ISOC99 libcs do not support it, so we prefer to do
3420 it by hand */
3421 /* hexadecimal or binary floats */
3422 /* XXX: handle overflows */
3423 *q = '\0';
3424 if (b == 16)
3425 shift = 4;
3426 else
3427 shift = 2;
3428 bn_zero(bn);
3429 q = token_buf;
3430 while (1) {
3431 t = *q++;
3432 if (t == '\0') {
3433 break;
3434 } else if (t >= 'a') {
3435 t = t - 'a' + 10;
3436 } else if (t >= 'A') {
3437 t = t - 'A' + 10;
3438 } else {
3439 t = t - '0';
3441 bn_lshift(bn, shift, t);
3443 frac_bits = 0;
3444 if (ch == '.') {
3445 ch = *p++;
3446 while (1) {
3447 t = ch;
3448 if (t >= 'a' && t <= 'f') {
3449 t = t - 'a' + 10;
3450 } else if (t >= 'A' && t <= 'F') {
3451 t = t - 'A' + 10;
3452 } else if (t >= '0' && t <= '9') {
3453 t = t - '0';
3454 } else {
3455 break;
3457 if (t >= b)
3458 error("invalid digit");
3459 bn_lshift(bn, shift, t);
3460 frac_bits += shift;
3461 ch = *p++;
3464 if (ch != 'p' && ch != 'P')
3465 expect("exponent");
3466 ch = *p++;
3467 s = 1;
3468 exp_val = 0;
3469 if (ch == '+') {
3470 ch = *p++;
3471 } else if (ch == '-') {
3472 s = -1;
3473 ch = *p++;
3475 if (ch < '0' || ch > '9')
3476 expect("exponent digits");
3477 while (ch >= '0' && ch <= '9') {
3478 exp_val = exp_val * 10 + ch - '0';
3479 ch = *p++;
3481 exp_val = exp_val * s;
3483 /* now we can generate the number */
3484 /* XXX: should patch directly float number */
3485 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3486 d = ldexp(d, exp_val - frac_bits);
3487 t = toup(ch);
3488 if (t == 'F') {
3489 ch = *p++;
3490 tok = TOK_CFLOAT;
3491 /* float : should handle overflow */
3492 tokc.f = (float)d;
3493 } else if (t == 'L') {
3494 ch = *p++;
3495 tok = TOK_CLDOUBLE;
3496 /* XXX: not large enough */
3497 tokc.ld = (long double)d;
3498 } else {
3499 tok = TOK_CDOUBLE;
3500 tokc.d = d;
3502 } else {
3503 /* decimal floats */
3504 if (ch == '.') {
3505 if (q >= token_buf + STRING_MAX_SIZE)
3506 goto num_too_long;
3507 *q++ = ch;
3508 ch = *p++;
3509 float_frac_parse:
3510 while (ch >= '0' && ch <= '9') {
3511 if (q >= token_buf + STRING_MAX_SIZE)
3512 goto num_too_long;
3513 *q++ = ch;
3514 ch = *p++;
3517 if (ch == 'e' || ch == 'E') {
3518 if (q >= token_buf + STRING_MAX_SIZE)
3519 goto num_too_long;
3520 *q++ = ch;
3521 ch = *p++;
3522 if (ch == '-' || ch == '+') {
3523 if (q >= token_buf + STRING_MAX_SIZE)
3524 goto num_too_long;
3525 *q++ = ch;
3526 ch = *p++;
3528 if (ch < '0' || ch > '9')
3529 expect("exponent digits");
3530 while (ch >= '0' && ch <= '9') {
3531 if (q >= token_buf + STRING_MAX_SIZE)
3532 goto num_too_long;
3533 *q++ = ch;
3534 ch = *p++;
3537 *q = '\0';
3538 t = toup(ch);
3539 errno = 0;
3540 if (t == 'F') {
3541 ch = *p++;
3542 tok = TOK_CFLOAT;
3543 tokc.f = strtof(token_buf, NULL);
3544 } else if (t == 'L') {
3545 ch = *p++;
3546 tok = TOK_CLDOUBLE;
3547 tokc.ld = strtold(token_buf, NULL);
3548 } else {
3549 tok = TOK_CDOUBLE;
3550 tokc.d = strtod(token_buf, NULL);
3553 } else {
3554 unsigned long long n, n1;
3555 int lcount, ucount;
3557 /* integer number */
3558 *q = '\0';
3559 q = token_buf;
3560 if (b == 10 && *q == '0') {
3561 b = 8;
3562 q++;
3564 n = 0;
3565 while(1) {
3566 t = *q++;
3567 /* no need for checks except for base 10 / 8 errors */
3568 if (t == '\0') {
3569 break;
3570 } else if (t >= 'a') {
3571 t = t - 'a' + 10;
3572 } else if (t >= 'A') {
3573 t = t - 'A' + 10;
3574 } else {
3575 t = t - '0';
3576 if (t >= b)
3577 error("invalid digit");
3579 n1 = n;
3580 n = n * b + t;
3581 /* detect overflow */
3582 /* XXX: this test is not reliable */
3583 if (n < n1)
3584 error("integer constant overflow");
3587 /* XXX: not exactly ANSI compliant */
3588 if ((n & 0xffffffff00000000LL) != 0) {
3589 if ((n >> 63) != 0)
3590 tok = TOK_CULLONG;
3591 else
3592 tok = TOK_CLLONG;
3593 } else if (n > 0x7fffffff) {
3594 tok = TOK_CUINT;
3595 } else {
3596 tok = TOK_CINT;
3598 lcount = 0;
3599 ucount = 0;
3600 for(;;) {
3601 t = toup(ch);
3602 if (t == 'L') {
3603 if (lcount >= 2)
3604 error("three 'l's in integer constant");
3605 lcount++;
3606 if (lcount == 2) {
3607 if (tok == TOK_CINT)
3608 tok = TOK_CLLONG;
3609 else if (tok == TOK_CUINT)
3610 tok = TOK_CULLONG;
3612 ch = *p++;
3613 } else if (t == 'U') {
3614 if (ucount >= 1)
3615 error("two 'u's in integer constant");
3616 ucount++;
3617 if (tok == TOK_CINT)
3618 tok = TOK_CUINT;
3619 else if (tok == TOK_CLLONG)
3620 tok = TOK_CULLONG;
3621 ch = *p++;
3622 } else {
3623 break;
3626 if (tok == TOK_CINT || tok == TOK_CUINT)
3627 tokc.ui = n;
3628 else
3629 tokc.ull = n;
3634 #define PARSE2(c1, tok1, c2, tok2) \
3635 case c1: \
3636 PEEKC(c, p); \
3637 if (c == c2) { \
3638 p++; \
3639 tok = tok2; \
3640 } else { \
3641 tok = tok1; \
3643 break;
3645 /* return next token without macro substitution */
3646 static inline void next_nomacro1(void)
3648 int t, c, is_long;
3649 TokenSym *ts;
3650 uint8_t *p, *p1;
3651 unsigned int h;
3653 p = file->buf_ptr;
3654 redo_no_start:
3655 c = *p;
3656 switch(c) {
3657 case ' ':
3658 case '\t':
3659 case '\f':
3660 case '\v':
3661 case '\r':
3662 p++;
3663 goto redo_no_start;
3665 case '\\':
3666 /* first look if it is in fact an end of buffer */
3667 if (p >= file->buf_end) {
3668 file->buf_ptr = p;
3669 handle_eob();
3670 p = file->buf_ptr;
3671 if (p >= file->buf_end)
3672 goto parse_eof;
3673 else
3674 goto redo_no_start;
3675 } else {
3676 file->buf_ptr = p;
3677 ch = *p;
3678 handle_stray();
3679 p = file->buf_ptr;
3680 goto redo_no_start;
3682 parse_eof:
3684 TCCState *s1 = tcc_state;
3685 if ((parse_flags & PARSE_FLAG_LINEFEED)
3686 && !(tok_flags & TOK_FLAG_EOF)) {
3687 tok_flags |= TOK_FLAG_EOF;
3688 tok = TOK_LINEFEED;
3689 goto keep_tok_flags;
3690 } else if (s1->include_stack_ptr == s1->include_stack ||
3691 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3692 /* no include left : end of file. */
3693 tok = TOK_EOF;
3694 } else {
3695 tok_flags &= ~TOK_FLAG_EOF;
3696 /* pop include file */
3698 /* test if previous '#endif' was after a #ifdef at
3699 start of file */
3700 if (tok_flags & TOK_FLAG_ENDIF) {
3701 #ifdef INC_DEBUG
3702 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3703 #endif
3704 add_cached_include(s1, file->inc_type, file->inc_filename,
3705 file->ifndef_macro_saved);
3708 /* add end of include file debug info */
3709 if (do_debug) {
3710 put_stabd(N_EINCL, 0, 0);
3712 /* pop include stack */
3713 tcc_close(file);
3714 s1->include_stack_ptr--;
3715 file = *s1->include_stack_ptr;
3716 p = file->buf_ptr;
3717 goto redo_no_start;
3720 break;
3722 case '\n':
3723 file->line_num++;
3724 tok_flags |= TOK_FLAG_BOL;
3725 p++;
3726 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3727 goto redo_no_start;
3728 tok = TOK_LINEFEED;
3729 goto keep_tok_flags;
3731 case '#':
3732 /* XXX: simplify */
3733 PEEKC(c, p);
3734 if ((tok_flags & TOK_FLAG_BOL) &&
3735 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3736 file->buf_ptr = p;
3737 preprocess(tok_flags & TOK_FLAG_BOF);
3738 p = file->buf_ptr;
3739 goto redo_no_start;
3740 } else {
3741 if (c == '#') {
3742 p++;
3743 tok = TOK_TWOSHARPS;
3744 } else {
3745 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3746 p = parse_line_comment(p - 1);
3747 goto redo_no_start;
3748 } else {
3749 tok = '#';
3753 break;
3755 case 'a': case 'b': case 'c': case 'd':
3756 case 'e': case 'f': case 'g': case 'h':
3757 case 'i': case 'j': case 'k': case 'l':
3758 case 'm': case 'n': case 'o': case 'p':
3759 case 'q': case 'r': case 's': case 't':
3760 case 'u': case 'v': case 'w': case 'x':
3761 case 'y': case 'z':
3762 case 'A': case 'B': case 'C': case 'D':
3763 case 'E': case 'F': case 'G': case 'H':
3764 case 'I': case 'J': case 'K':
3765 case 'M': case 'N': case 'O': case 'P':
3766 case 'Q': case 'R': case 'S': case 'T':
3767 case 'U': case 'V': case 'W': case 'X':
3768 case 'Y': case 'Z':
3769 case '_':
3770 parse_ident_fast:
3771 p1 = p;
3772 h = TOK_HASH_INIT;
3773 h = TOK_HASH_FUNC(h, c);
3774 p++;
3775 for(;;) {
3776 c = *p;
3777 if (!isidnum_table[c-CH_EOF])
3778 break;
3779 h = TOK_HASH_FUNC(h, c);
3780 p++;
3782 if (c != '\\') {
3783 TokenSym **pts;
3784 int len;
3786 /* fast case : no stray found, so we have the full token
3787 and we have already hashed it */
3788 len = p - p1;
3789 h &= (TOK_HASH_SIZE - 1);
3790 pts = &hash_ident[h];
3791 for(;;) {
3792 ts = *pts;
3793 if (!ts)
3794 break;
3795 if (ts->len == len && !memcmp(ts->str, p1, len))
3796 goto token_found;
3797 pts = &(ts->hash_next);
3799 ts = tok_alloc_new(pts, p1, len);
3800 token_found: ;
3801 } else {
3802 /* slower case */
3803 cstr_reset(&tokcstr);
3805 while (p1 < p) {
3806 cstr_ccat(&tokcstr, *p1);
3807 p1++;
3809 p--;
3810 PEEKC(c, p);
3811 parse_ident_slow:
3812 while (isidnum_table[c-CH_EOF]) {
3813 cstr_ccat(&tokcstr, c);
3814 PEEKC(c, p);
3816 ts = tok_alloc(tokcstr.data, tokcstr.size);
3818 tok = ts->tok;
3819 break;
3820 case 'L':
3821 t = p[1];
3822 if (t != '\\' && t != '\'' && t != '\"') {
3823 /* fast case */
3824 goto parse_ident_fast;
3825 } else {
3826 PEEKC(c, p);
3827 if (c == '\'' || c == '\"') {
3828 is_long = 1;
3829 goto str_const;
3830 } else {
3831 cstr_reset(&tokcstr);
3832 cstr_ccat(&tokcstr, 'L');
3833 goto parse_ident_slow;
3836 break;
3837 case '0': case '1': case '2': case '3':
3838 case '4': case '5': case '6': case '7':
3839 case '8': case '9':
3841 cstr_reset(&tokcstr);
3842 /* after the first digit, accept digits, alpha, '.' or sign if
3843 prefixed by 'eEpP' */
3844 parse_num:
3845 for(;;) {
3846 t = c;
3847 cstr_ccat(&tokcstr, c);
3848 PEEKC(c, p);
3849 if (!(isnum(c) || isid(c) || c == '.' ||
3850 ((c == '+' || c == '-') &&
3851 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3852 break;
3854 /* We add a trailing '\0' to ease parsing */
3855 cstr_ccat(&tokcstr, '\0');
3856 tokc.cstr = &tokcstr;
3857 tok = TOK_PPNUM;
3858 break;
3859 case '.':
3860 /* special dot handling because it can also start a number */
3861 PEEKC(c, p);
3862 if (isnum(c)) {
3863 cstr_reset(&tokcstr);
3864 cstr_ccat(&tokcstr, '.');
3865 goto parse_num;
3866 } else if (c == '.') {
3867 PEEKC(c, p);
3868 if (c != '.')
3869 expect("'.'");
3870 PEEKC(c, p);
3871 tok = TOK_DOTS;
3872 } else {
3873 tok = '.';
3875 break;
3876 case '\'':
3877 case '\"':
3878 is_long = 0;
3879 str_const:
3881 CString str;
3882 int sep;
3884 sep = c;
3886 /* parse the string */
3887 cstr_new(&str);
3888 p = parse_pp_string(p, sep, &str);
3889 cstr_ccat(&str, '\0');
3891 /* eval the escape (should be done as TOK_PPNUM) */
3892 cstr_reset(&tokcstr);
3893 parse_escape_string(&tokcstr, str.data, is_long);
3894 cstr_free(&str);
3896 if (sep == '\'') {
3897 int char_size;
3898 /* XXX: make it portable */
3899 if (!is_long)
3900 char_size = 1;
3901 else
3902 char_size = sizeof(nwchar_t);
3903 if (tokcstr.size <= char_size)
3904 error("empty character constant");
3905 if (tokcstr.size > 2 * char_size)
3906 warning("multi-character character constant");
3907 if (!is_long) {
3908 tokc.i = *(int8_t *)tokcstr.data;
3909 tok = TOK_CCHAR;
3910 } else {
3911 tokc.i = *(nwchar_t *)tokcstr.data;
3912 tok = TOK_LCHAR;
3914 } else {
3915 tokc.cstr = &tokcstr;
3916 if (!is_long)
3917 tok = TOK_STR;
3918 else
3919 tok = TOK_LSTR;
3922 break;
3924 case '<':
3925 PEEKC(c, p);
3926 if (c == '=') {
3927 p++;
3928 tok = TOK_LE;
3929 } else if (c == '<') {
3930 PEEKC(c, p);
3931 if (c == '=') {
3932 p++;
3933 tok = TOK_A_SHL;
3934 } else {
3935 tok = TOK_SHL;
3937 } else {
3938 tok = TOK_LT;
3940 break;
3942 case '>':
3943 PEEKC(c, p);
3944 if (c == '=') {
3945 p++;
3946 tok = TOK_GE;
3947 } else if (c == '>') {
3948 PEEKC(c, p);
3949 if (c == '=') {
3950 p++;
3951 tok = TOK_A_SAR;
3952 } else {
3953 tok = TOK_SAR;
3955 } else {
3956 tok = TOK_GT;
3958 break;
3960 case '&':
3961 PEEKC(c, p);
3962 if (c == '&') {
3963 p++;
3964 tok = TOK_LAND;
3965 } else if (c == '=') {
3966 p++;
3967 tok = TOK_A_AND;
3968 } else {
3969 tok = '&';
3971 break;
3973 case '|':
3974 PEEKC(c, p);
3975 if (c == '|') {
3976 p++;
3977 tok = TOK_LOR;
3978 } else if (c == '=') {
3979 p++;
3980 tok = TOK_A_OR;
3981 } else {
3982 tok = '|';
3984 break;
3986 case '+':
3987 PEEKC(c, p);
3988 if (c == '+') {
3989 p++;
3990 tok = TOK_INC;
3991 } else if (c == '=') {
3992 p++;
3993 tok = TOK_A_ADD;
3994 } else {
3995 tok = '+';
3997 break;
3999 case '-':
4000 PEEKC(c, p);
4001 if (c == '-') {
4002 p++;
4003 tok = TOK_DEC;
4004 } else if (c == '=') {
4005 p++;
4006 tok = TOK_A_SUB;
4007 } else if (c == '>') {
4008 p++;
4009 tok = TOK_ARROW;
4010 } else {
4011 tok = '-';
4013 break;
4015 PARSE2('!', '!', '=', TOK_NE)
4016 PARSE2('=', '=', '=', TOK_EQ)
4017 PARSE2('*', '*', '=', TOK_A_MUL)
4018 PARSE2('%', '%', '=', TOK_A_MOD)
4019 PARSE2('^', '^', '=', TOK_A_XOR)
4021 /* comments or operator */
4022 case '/':
4023 PEEKC(c, p);
4024 if (c == '*') {
4025 p = parse_comment(p);
4026 goto redo_no_start;
4027 } else if (c == '/') {
4028 p = parse_line_comment(p);
4029 goto redo_no_start;
4030 } else if (c == '=') {
4031 p++;
4032 tok = TOK_A_DIV;
4033 } else {
4034 tok = '/';
4036 break;
4038 /* simple tokens */
4039 case '(':
4040 case ')':
4041 case '[':
4042 case ']':
4043 case '{':
4044 case '}':
4045 case ',':
4046 case ';':
4047 case ':':
4048 case '?':
4049 case '~':
4050 case '$': /* only used in assembler */
4051 case '@': /* dito */
4052 tok = c;
4053 p++;
4054 break;
4055 default:
4056 error("unrecognized character \\x%02x", c);
4057 break;
4059 tok_flags = 0;
4060 keep_tok_flags:
4061 file->buf_ptr = p;
4062 #if defined(PARSE_DEBUG)
4063 printf("token = %s\n", get_tok_str(tok, &tokc));
4064 #endif
4067 /* return next token without macro substitution. Can read input from
4068 macro_ptr buffer */
4069 static void next_nomacro(void)
4071 if (macro_ptr) {
4072 redo:
4073 tok = *macro_ptr;
4074 if (tok) {
4075 TOK_GET(tok, macro_ptr, tokc);
4076 if (tok == TOK_LINENUM) {
4077 file->line_num = tokc.i;
4078 goto redo;
4081 } else {
4082 next_nomacro1();
4086 /* substitute args in macro_str and return allocated string */
4087 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4089 int *st, last_tok, t, notfirst;
4090 Sym *s;
4091 CValue cval;
4092 TokenString str;
4093 CString cstr;
4095 tok_str_new(&str);
4096 last_tok = 0;
4097 while(1) {
4098 TOK_GET(t, macro_str, cval);
4099 if (!t)
4100 break;
4101 if (t == '#') {
4102 /* stringize */
4103 TOK_GET(t, macro_str, cval);
4104 if (!t)
4105 break;
4106 s = sym_find2(args, t);
4107 if (s) {
4108 cstr_new(&cstr);
4109 st = (int *)s->c;
4110 notfirst = 0;
4111 while (*st) {
4112 if (notfirst)
4113 cstr_ccat(&cstr, ' ');
4114 TOK_GET(t, st, cval);
4115 cstr_cat(&cstr, get_tok_str(t, &cval));
4116 #ifndef PP_NOSPACES
4117 notfirst = 1;
4118 #endif
4120 cstr_ccat(&cstr, '\0');
4121 #ifdef PP_DEBUG
4122 printf("stringize: %s\n", (char *)cstr.data);
4123 #endif
4124 /* add string */
4125 cval.cstr = &cstr;
4126 tok_str_add2(&str, TOK_STR, &cval);
4127 cstr_free(&cstr);
4128 } else {
4129 tok_str_add2(&str, t, &cval);
4131 } else if (t >= TOK_IDENT) {
4132 s = sym_find2(args, t);
4133 if (s) {
4134 st = (int *)s->c;
4135 /* if '##' is present before or after, no arg substitution */
4136 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4137 /* special case for var arg macros : ## eats the
4138 ',' if empty VA_ARGS variable. */
4139 /* XXX: test of the ',' is not 100%
4140 reliable. should fix it to avoid security
4141 problems */
4142 if (gnu_ext && s->type.t &&
4143 last_tok == TOK_TWOSHARPS &&
4144 str.len >= 2 && str.str[str.len - 2] == ',') {
4145 if (*st == 0) {
4146 /* suppress ',' '##' */
4147 str.len -= 2;
4148 } else {
4149 /* suppress '##' and add variable */
4150 str.len--;
4151 goto add_var;
4153 } else {
4154 int t1;
4155 add_var:
4156 for(;;) {
4157 TOK_GET(t1, st, cval);
4158 if (!t1)
4159 break;
4160 tok_str_add2(&str, t1, &cval);
4163 } else {
4164 /* NOTE: the stream cannot be read when macro
4165 substituing an argument */
4166 macro_subst(&str, nested_list, st, NULL);
4168 } else {
4169 tok_str_add(&str, t);
4171 } else {
4172 tok_str_add2(&str, t, &cval);
4174 last_tok = t;
4176 tok_str_add(&str, 0);
4177 return str.str;
4180 static char const ab_month_name[12][4] =
4182 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4183 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4186 /* do macro substitution of current token with macro 's' and add
4187 result to (tok_str,tok_len). 'nested_list' is the list of all
4188 macros we got inside to avoid recursing. Return non zero if no
4189 substitution needs to be done */
4190 static int macro_subst_tok(TokenString *tok_str,
4191 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4193 Sym *args, *sa, *sa1;
4194 int mstr_allocated, parlevel, *mstr, t, t1;
4195 TokenString str;
4196 char *cstrval;
4197 CValue cval;
4198 CString cstr;
4199 char buf[32];
4201 /* if symbol is a macro, prepare substitution */
4202 /* special macros */
4203 if (tok == TOK___LINE__) {
4204 snprintf(buf, sizeof(buf), "%d", file->line_num);
4205 cstrval = buf;
4206 t1 = TOK_PPNUM;
4207 goto add_cstr1;
4208 } else if (tok == TOK___FILE__) {
4209 cstrval = file->filename;
4210 goto add_cstr;
4211 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4212 time_t ti;
4213 struct tm *tm;
4215 time(&ti);
4216 tm = localtime(&ti);
4217 if (tok == TOK___DATE__) {
4218 snprintf(buf, sizeof(buf), "%s %2d %d",
4219 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4220 } else {
4221 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4222 tm->tm_hour, tm->tm_min, tm->tm_sec);
4224 cstrval = buf;
4225 add_cstr:
4226 t1 = TOK_STR;
4227 add_cstr1:
4228 cstr_new(&cstr);
4229 cstr_cat(&cstr, cstrval);
4230 cstr_ccat(&cstr, '\0');
4231 cval.cstr = &cstr;
4232 tok_str_add2(tok_str, t1, &cval);
4233 cstr_free(&cstr);
4234 } else {
4235 mstr = (int *)s->c;
4236 mstr_allocated = 0;
4237 if (s->type.t == MACRO_FUNC) {
4238 /* NOTE: we do not use next_nomacro to avoid eating the
4239 next token. XXX: find better solution */
4240 redo:
4241 if (macro_ptr) {
4242 t = *macro_ptr;
4243 if (t == 0 && can_read_stream) {
4244 /* end of macro stream: we must look at the token
4245 after in the file */
4246 struct macro_level *ml = *can_read_stream;
4247 macro_ptr = NULL;
4248 if (ml)
4250 macro_ptr = ml->p;
4251 ml->p = NULL;
4252 *can_read_stream = ml -> prev;
4254 goto redo;
4256 } else {
4257 /* XXX: incorrect with comments */
4258 ch = file->buf_ptr[0];
4259 while (is_space(ch) || ch == '\n')
4260 cinp();
4261 t = ch;
4263 if (t != '(') /* no macro subst */
4264 return -1;
4266 /* argument macro */
4267 next_nomacro();
4268 next_nomacro();
4269 args = NULL;
4270 sa = s->next;
4271 /* NOTE: empty args are allowed, except if no args */
4272 for(;;) {
4273 /* handle '()' case */
4274 if (!args && !sa && tok == ')')
4275 break;
4276 if (!sa)
4277 error("macro '%s' used with too many args",
4278 get_tok_str(s->v, 0));
4279 tok_str_new(&str);
4280 parlevel = 0;
4281 /* NOTE: non zero sa->t indicates VA_ARGS */
4282 while ((parlevel > 0 ||
4283 (tok != ')' &&
4284 (tok != ',' || sa->type.t))) &&
4285 tok != -1) {
4286 if (tok == '(')
4287 parlevel++;
4288 else if (tok == ')')
4289 parlevel--;
4290 if (tok != TOK_LINEFEED)
4291 tok_str_add2(&str, tok, &tokc);
4292 next_nomacro();
4294 tok_str_add(&str, 0);
4295 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
4296 sa = sa->next;
4297 if (tok == ')') {
4298 /* special case for gcc var args: add an empty
4299 var arg argument if it is omitted */
4300 if (sa && sa->type.t && gnu_ext)
4301 continue;
4302 else
4303 break;
4305 if (tok != ',')
4306 expect(",");
4307 next_nomacro();
4309 if (sa) {
4310 error("macro '%s' used with too few args",
4311 get_tok_str(s->v, 0));
4314 /* now subst each arg */
4315 mstr = macro_arg_subst(nested_list, mstr, args);
4316 /* free memory */
4317 sa = args;
4318 while (sa) {
4319 sa1 = sa->prev;
4320 tok_str_free((int *)sa->c);
4321 sym_free(sa);
4322 sa = sa1;
4324 mstr_allocated = 1;
4326 sym_push2(nested_list, s->v, 0, 0);
4327 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4328 /* pop nested defined symbol */
4329 sa1 = *nested_list;
4330 *nested_list = sa1->prev;
4331 sym_free(sa1);
4332 if (mstr_allocated)
4333 tok_str_free(mstr);
4335 return 0;
4338 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4339 return the resulting string (which must be freed). */
4340 static inline int *macro_twosharps(const int *macro_str)
4342 TokenSym *ts;
4343 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4344 int t;
4345 const char *p1, *p2;
4346 CValue cval;
4347 TokenString macro_str1;
4348 CString cstr;
4350 start_macro_ptr = macro_str;
4351 /* we search the first '##' */
4352 for(;;) {
4353 macro_ptr1 = macro_str;
4354 TOK_GET(t, macro_str, cval);
4355 /* nothing more to do if end of string */
4356 if (t == 0)
4357 return NULL;
4358 if (*macro_str == TOK_TWOSHARPS)
4359 break;
4362 /* we saw '##', so we need more processing to handle it */
4363 cstr_new(&cstr);
4364 tok_str_new(&macro_str1);
4365 tok = t;
4366 tokc = cval;
4368 /* add all tokens seen so far */
4369 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4370 TOK_GET(t, ptr, cval);
4371 tok_str_add2(&macro_str1, t, &cval);
4373 saved_macro_ptr = macro_ptr;
4374 /* XXX: get rid of the use of macro_ptr here */
4375 macro_ptr = (int *)macro_str;
4376 for(;;) {
4377 while (*macro_ptr == TOK_TWOSHARPS) {
4378 macro_ptr++;
4379 macro_ptr1 = macro_ptr;
4380 t = *macro_ptr;
4381 if (t) {
4382 TOK_GET(t, macro_ptr, cval);
4383 /* We concatenate the two tokens if we have an
4384 identifier or a preprocessing number */
4385 cstr_reset(&cstr);
4386 p1 = get_tok_str(tok, &tokc);
4387 cstr_cat(&cstr, p1);
4388 p2 = get_tok_str(t, &cval);
4389 cstr_cat(&cstr, p2);
4390 cstr_ccat(&cstr, '\0');
4392 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4393 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4394 if (tok == TOK_PPNUM) {
4395 /* if number, then create a number token */
4396 /* NOTE: no need to allocate because
4397 tok_str_add2() does it */
4398 cstr_reset(&tokcstr);
4399 tokcstr = cstr;
4400 cstr_new(&cstr);
4401 tokc.cstr = &tokcstr;
4402 } else {
4403 /* if identifier, we must do a test to
4404 validate we have a correct identifier */
4405 if (t == TOK_PPNUM) {
4406 const char *p;
4407 int c;
4409 p = p2;
4410 for(;;) {
4411 c = *p;
4412 if (c == '\0')
4413 break;
4414 p++;
4415 if (!isnum(c) && !isid(c))
4416 goto error_pasting;
4419 ts = tok_alloc(cstr.data, strlen(cstr.data));
4420 tok = ts->tok; /* modify current token */
4422 } else {
4423 const char *str = cstr.data;
4424 const unsigned char *q;
4426 /* we look for a valid token */
4427 /* XXX: do more extensive checks */
4428 if (!strcmp(str, ">>=")) {
4429 tok = TOK_A_SAR;
4430 } else if (!strcmp(str, "<<=")) {
4431 tok = TOK_A_SHL;
4432 } else if (strlen(str) == 2) {
4433 /* search in two bytes table */
4434 q = tok_two_chars;
4435 for(;;) {
4436 if (!*q)
4437 goto error_pasting;
4438 if (q[0] == str[0] && q[1] == str[1])
4439 break;
4440 q += 3;
4442 tok = q[2];
4443 } else {
4444 error_pasting:
4445 /* NOTE: because get_tok_str use a static buffer,
4446 we must save it */
4447 cstr_reset(&cstr);
4448 p1 = get_tok_str(tok, &tokc);
4449 cstr_cat(&cstr, p1);
4450 cstr_ccat(&cstr, '\0');
4451 p2 = get_tok_str(t, &cval);
4452 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4453 /* cannot merge tokens: just add them separately */
4454 tok_str_add2(&macro_str1, tok, &tokc);
4455 /* XXX: free associated memory ? */
4456 tok = t;
4457 tokc = cval;
4462 tok_str_add2(&macro_str1, tok, &tokc);
4463 next_nomacro();
4464 if (tok == 0)
4465 break;
4467 macro_ptr = (int *)saved_macro_ptr;
4468 cstr_free(&cstr);
4469 tok_str_add(&macro_str1, 0);
4470 return macro_str1.str;
4474 /* do macro substitution of macro_str and add result to
4475 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4476 inside to avoid recursing. */
4477 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4478 const int *macro_str, struct macro_level ** can_read_stream)
4480 Sym *s;
4481 int *macro_str1;
4482 const int *ptr;
4483 int t, ret;
4484 CValue cval;
4485 struct macro_level ml;
4487 /* first scan for '##' operator handling */
4488 ptr = macro_str;
4489 macro_str1 = macro_twosharps(ptr);
4490 if (macro_str1)
4491 ptr = macro_str1;
4492 while (1) {
4493 /* NOTE: ptr == NULL can only happen if tokens are read from
4494 file stream due to a macro function call */
4495 if (ptr == NULL)
4496 break;
4497 TOK_GET(t, ptr, cval);
4498 if (t == 0)
4499 break;
4500 s = define_find(t);
4501 if (s != NULL) {
4502 /* if nested substitution, do nothing */
4503 if (sym_find2(*nested_list, t))
4504 goto no_subst;
4505 ml.p = macro_ptr;
4506 if (can_read_stream)
4507 ml.prev = *can_read_stream, *can_read_stream = &ml;
4508 macro_ptr = (int *)ptr;
4509 tok = t;
4510 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4511 ptr = (int *)macro_ptr;
4512 macro_ptr = ml.p;
4513 if (can_read_stream && *can_read_stream == &ml)
4514 *can_read_stream = ml.prev;
4515 if (ret != 0)
4516 goto no_subst;
4517 } else {
4518 no_subst:
4519 tok_str_add2(tok_str, t, &cval);
4522 if (macro_str1)
4523 tok_str_free(macro_str1);
4526 /* return next token with macro substitution */
4527 static void next(void)
4529 Sym *nested_list, *s;
4530 TokenString str;
4531 struct macro_level *ml;
4533 redo:
4534 next_nomacro();
4535 if (!macro_ptr) {
4536 /* if not reading from macro substituted string, then try
4537 to substitute macros */
4538 if (tok >= TOK_IDENT &&
4539 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4540 s = define_find(tok);
4541 if (s) {
4542 /* we have a macro: we try to substitute */
4543 tok_str_new(&str);
4544 nested_list = NULL;
4545 ml = NULL;
4546 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4547 /* substitution done, NOTE: maybe empty */
4548 tok_str_add(&str, 0);
4549 macro_ptr = str.str;
4550 macro_ptr_allocated = str.str;
4551 goto redo;
4555 } else {
4556 if (tok == 0) {
4557 /* end of macro or end of unget buffer */
4558 if (unget_buffer_enabled) {
4559 macro_ptr = unget_saved_macro_ptr;
4560 unget_buffer_enabled = 0;
4561 } else {
4562 /* end of macro string: free it */
4563 tok_str_free(macro_ptr_allocated);
4564 macro_ptr = NULL;
4566 goto redo;
4570 /* convert preprocessor tokens into C tokens */
4571 if (tok == TOK_PPNUM &&
4572 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4573 parse_number((char *)tokc.cstr->data);
4577 /* push back current token and set current token to 'last_tok'. Only
4578 identifier case handled for labels. */
4579 static inline void unget_tok(int last_tok)
4581 int i, n;
4582 int *q;
4583 unget_saved_macro_ptr = macro_ptr;
4584 unget_buffer_enabled = 1;
4585 q = unget_saved_buffer;
4586 macro_ptr = q;
4587 *q++ = tok;
4588 n = tok_ext_size(tok) - 1;
4589 for(i=0;i<n;i++)
4590 *q++ = tokc.tab[i];
4591 *q = 0; /* end of token string */
4592 tok = last_tok;
4596 void swap(int *p, int *q)
4598 int t;
4599 t = *p;
4600 *p = *q;
4601 *q = t;
4604 void vsetc(CType *type, int r, CValue *vc)
4606 int v;
4608 if (vtop >= vstack + (VSTACK_SIZE - 1))
4609 error("memory full");
4610 /* cannot let cpu flags if other instruction are generated. Also
4611 avoid leaving VT_JMP anywhere except on the top of the stack
4612 because it would complicate the code generator. */
4613 if (vtop >= vstack) {
4614 v = vtop->r & VT_VALMASK;
4615 if (v == VT_CMP || (v & ~1) == VT_JMP)
4616 gv(RC_INT);
4618 vtop++;
4619 vtop->type = *type;
4620 vtop->r = r;
4621 vtop->r2 = VT_CONST;
4622 vtop->c = *vc;
4625 /* push integer constant */
4626 void vpushi(int v)
4628 CValue cval;
4629 cval.i = v;
4630 vsetc(&int_type, VT_CONST, &cval);
4633 /* Return a static symbol pointing to a section */
4634 static Sym *get_sym_ref(CType *type, Section *sec,
4635 unsigned long offset, unsigned long size)
4637 int v;
4638 Sym *sym;
4640 v = anon_sym++;
4641 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4642 sym->type.ref = type->ref;
4643 sym->r = VT_CONST | VT_SYM;
4644 put_extern_sym(sym, sec, offset, size);
4645 return sym;
4648 /* push a reference to a section offset by adding a dummy symbol */
4649 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4651 CValue cval;
4653 cval.ul = 0;
4654 vsetc(type, VT_CONST | VT_SYM, &cval);
4655 vtop->sym = get_sym_ref(type, sec, offset, size);
4658 /* define a new external reference to a symbol 'v' of type 'u' */
4659 static Sym *external_global_sym(int v, CType *type, int r)
4661 Sym *s;
4663 s = sym_find(v);
4664 if (!s) {
4665 /* push forward reference */
4666 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4667 s->type.ref = type->ref;
4668 s->r = r | VT_CONST | VT_SYM;
4670 return s;
4673 /* define a new external reference to a symbol 'v' of type 'u' */
4674 static Sym *external_sym(int v, CType *type, int r)
4676 Sym *s;
4678 s = sym_find(v);
4679 if (!s) {
4680 /* push forward reference */
4681 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4682 s->type.t |= VT_EXTERN;
4683 } else {
4684 if (!is_compatible_types(&s->type, type))
4685 error("incompatible types for redefinition of '%s'",
4686 get_tok_str(v, NULL));
4688 return s;
4691 /* push a reference to global symbol v */
4692 static void vpush_global_sym(CType *type, int v)
4694 Sym *sym;
4695 CValue cval;
4697 sym = external_global_sym(v, type, 0);
4698 cval.ul = 0;
4699 vsetc(type, VT_CONST | VT_SYM, &cval);
4700 vtop->sym = sym;
4703 void vset(CType *type, int r, int v)
4705 CValue cval;
4707 cval.i = v;
4708 vsetc(type, r, &cval);
4711 void vseti(int r, int v)
4713 CType type;
4714 type.t = VT_INT;
4715 vset(&type, r, v);
4718 void vswap(void)
4720 SValue tmp;
4722 tmp = vtop[0];
4723 vtop[0] = vtop[-1];
4724 vtop[-1] = tmp;
4727 void vpushv(SValue *v)
4729 if (vtop >= vstack + (VSTACK_SIZE - 1))
4730 error("memory full");
4731 vtop++;
4732 *vtop = *v;
4735 void vdup(void)
4737 vpushv(vtop);
4740 /* save r to the memory stack, and mark it as being free */
4741 void save_reg(int r)
4743 int l, saved, size, align;
4744 SValue *p, sv;
4745 CType *type;
4747 /* modify all stack values */
4748 saved = 0;
4749 l = 0;
4750 for(p=vstack;p<=vtop;p++) {
4751 if ((p->r & VT_VALMASK) == r ||
4752 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4753 /* must save value on stack if not already done */
4754 if (!saved) {
4755 /* NOTE: must reload 'r' because r might be equal to r2 */
4756 r = p->r & VT_VALMASK;
4757 /* store register in the stack */
4758 type = &p->type;
4759 if ((p->r & VT_LVAL) ||
4760 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4761 type = &int_type;
4762 size = type_size(type, &align);
4763 loc = (loc - size) & -align;
4764 sv.type.t = type->t;
4765 sv.r = VT_LOCAL | VT_LVAL;
4766 sv.c.ul = loc;
4767 store(r, &sv);
4768 #ifdef TCC_TARGET_I386
4769 /* x86 specific: need to pop fp register ST0 if saved */
4770 if (r == TREG_ST0) {
4771 o(0xd9dd); /* fstp %st(1) */
4773 #endif
4774 /* special long long case */
4775 if ((type->t & VT_BTYPE) == VT_LLONG) {
4776 sv.c.ul += 4;
4777 store(p->r2, &sv);
4779 l = loc;
4780 saved = 1;
4782 /* mark that stack entry as being saved on the stack */
4783 if (p->r & VT_LVAL) {
4784 /* also clear the bounded flag because the
4785 relocation address of the function was stored in
4786 p->c.ul */
4787 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4788 } else {
4789 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4791 p->r2 = VT_CONST;
4792 p->c.ul = l;
4797 /* find a register of class 'rc2' with at most one reference on stack.
4798 * If none, call get_reg(rc) */
4799 int get_reg_ex(int rc, int rc2)
4801 int r;
4802 SValue *p;
4804 for(r=0;r<NB_REGS;r++) {
4805 if (reg_classes[r] & rc2) {
4806 int n;
4807 n=0;
4808 for(p = vstack; p <= vtop; p++) {
4809 if ((p->r & VT_VALMASK) == r ||
4810 (p->r2 & VT_VALMASK) == r)
4811 n++;
4813 if (n <= 1)
4814 return r;
4817 return get_reg(rc);
4820 /* find a free register of class 'rc'. If none, save one register */
4821 int get_reg(int rc)
4823 int r;
4824 SValue *p;
4826 /* find a free register */
4827 for(r=0;r<NB_REGS;r++) {
4828 if (reg_classes[r] & rc) {
4829 for(p=vstack;p<=vtop;p++) {
4830 if ((p->r & VT_VALMASK) == r ||
4831 (p->r2 & VT_VALMASK) == r)
4832 goto notfound;
4834 return r;
4836 notfound: ;
4839 /* no register left : free the first one on the stack (VERY
4840 IMPORTANT to start from the bottom to ensure that we don't
4841 spill registers used in gen_opi()) */
4842 for(p=vstack;p<=vtop;p++) {
4843 r = p->r & VT_VALMASK;
4844 if (r < VT_CONST && (reg_classes[r] & rc))
4845 goto save_found;
4846 /* also look at second register (if long long) */
4847 r = p->r2 & VT_VALMASK;
4848 if (r < VT_CONST && (reg_classes[r] & rc)) {
4849 save_found:
4850 save_reg(r);
4851 return r;
4854 /* Should never comes here */
4855 return -1;
4858 /* save registers up to (vtop - n) stack entry */
4859 void save_regs(int n)
4861 int r;
4862 SValue *p, *p1;
4863 p1 = vtop - n;
4864 for(p = vstack;p <= p1; p++) {
4865 r = p->r & VT_VALMASK;
4866 if (r < VT_CONST) {
4867 save_reg(r);
4872 /* move register 's' to 'r', and flush previous value of r to memory
4873 if needed */
4874 void move_reg(int r, int s)
4876 SValue sv;
4878 if (r != s) {
4879 save_reg(r);
4880 sv.type.t = VT_INT;
4881 sv.r = s;
4882 sv.c.ul = 0;
4883 load(r, &sv);
4887 /* get address of vtop (vtop MUST BE an lvalue) */
4888 void gaddrof(void)
4890 vtop->r &= ~VT_LVAL;
4891 /* tricky: if saved lvalue, then we can go back to lvalue */
4892 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4893 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4896 #ifdef CONFIG_TCC_BCHECK
4897 /* generate lvalue bound code */
4898 void gbound(void)
4900 int lval_type;
4901 CType type1;
4903 vtop->r &= ~VT_MUSTBOUND;
4904 /* if lvalue, then use checking code before dereferencing */
4905 if (vtop->r & VT_LVAL) {
4906 /* if not VT_BOUNDED value, then make one */
4907 if (!(vtop->r & VT_BOUNDED)) {
4908 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4909 /* must save type because we must set it to int to get pointer */
4910 type1 = vtop->type;
4911 vtop->type.t = VT_INT;
4912 gaddrof();
4913 vpushi(0);
4914 gen_bounded_ptr_add();
4915 vtop->r |= lval_type;
4916 vtop->type = type1;
4918 /* then check for dereferencing */
4919 gen_bounded_ptr_deref();
4922 #endif
4924 /* store vtop a register belonging to class 'rc'. lvalues are
4925 converted to values. Cannot be used if cannot be converted to
4926 register value (such as structures). */
4927 int gv(int rc)
4929 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4930 unsigned long long ll;
4932 /* NOTE: get_reg can modify vstack[] */
4933 if (vtop->type.t & VT_BITFIELD) {
4934 CType type;
4935 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4936 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4937 /* remove bit field info to avoid loops */
4938 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4939 /* cast to int to propagate signedness in following ops */
4940 type.t = VT_INT;
4941 if((vtop->type.t & VT_UNSIGNED) ||
4942 (vtop->type.t & VT_BTYPE) == VT_BOOL)
4943 type.t |= VT_UNSIGNED;
4944 gen_cast(&type);
4945 /* generate shifts */
4946 vpushi(32 - (bit_pos + bit_size));
4947 gen_op(TOK_SHL);
4948 vpushi(32 - bit_size);
4949 /* NOTE: transformed to SHR if unsigned */
4950 gen_op(TOK_SAR);
4951 r = gv(rc);
4952 } else {
4953 if (is_float(vtop->type.t) &&
4954 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4955 Sym *sym;
4956 int *ptr;
4957 unsigned long offset;
4958 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4959 CValue check;
4960 #endif
4962 /* XXX: unify with initializers handling ? */
4963 /* CPUs usually cannot use float constants, so we store them
4964 generically in data segment */
4965 size = type_size(&vtop->type, &align);
4966 offset = (data_section->data_offset + align - 1) & -align;
4967 data_section->data_offset = offset;
4968 /* XXX: not portable yet */
4969 #ifdef __i386__
4970 /* Zero pad x87 tenbyte long doubles */
4971 if (size == 12)
4972 vtop->c.tab[2] &= 0xffff;
4973 #endif
4974 ptr = section_ptr_add(data_section, size);
4975 size = size >> 2;
4976 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4977 check.d = 1;
4978 if(check.tab[0])
4979 for(i=0;i<size;i++)
4980 ptr[i] = vtop->c.tab[size-1-i];
4981 else
4982 #endif
4983 for(i=0;i<size;i++)
4984 ptr[i] = vtop->c.tab[i];
4985 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4986 vtop->r |= VT_LVAL | VT_SYM;
4987 vtop->sym = sym;
4988 vtop->c.ul = 0;
4990 #ifdef CONFIG_TCC_BCHECK
4991 if (vtop->r & VT_MUSTBOUND)
4992 gbound();
4993 #endif
4995 r = vtop->r & VT_VALMASK;
4996 rc2 = RC_INT;
4997 if (rc == RC_IRET)
4998 rc2 = RC_LRET;
4999 /* need to reload if:
5000 - constant
5001 - lvalue (need to dereference pointer)
5002 - already a register, but not in the right class */
5003 if (r >= VT_CONST ||
5004 (vtop->r & VT_LVAL) ||
5005 !(reg_classes[r] & rc) ||
5006 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
5007 !(reg_classes[vtop->r2] & rc2))) {
5008 r = get_reg(rc);
5009 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
5010 /* two register type load : expand to two words
5011 temporarily */
5012 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
5013 /* load constant */
5014 ll = vtop->c.ull;
5015 vtop->c.ui = ll; /* first word */
5016 load(r, vtop);
5017 vtop->r = r; /* save register value */
5018 vpushi(ll >> 32); /* second word */
5019 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
5020 (vtop->r & VT_LVAL)) {
5021 /* We do not want to modifier the long long
5022 pointer here, so the safest (and less
5023 efficient) is to save all the other registers
5024 in the stack. XXX: totally inefficient. */
5025 save_regs(1);
5026 /* load from memory */
5027 load(r, vtop);
5028 vdup();
5029 vtop[-1].r = r; /* save register value */
5030 /* increment pointer to get second word */
5031 vtop->type.t = VT_INT;
5032 gaddrof();
5033 vpushi(4);
5034 gen_op('+');
5035 vtop->r |= VT_LVAL;
5036 } else {
5037 /* move registers */
5038 load(r, vtop);
5039 vdup();
5040 vtop[-1].r = r; /* save register value */
5041 vtop->r = vtop[-1].r2;
5043 /* allocate second register */
5044 r2 = get_reg(rc2);
5045 load(r2, vtop);
5046 vpop();
5047 /* write second register */
5048 vtop->r2 = r2;
5049 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5050 int t1, t;
5051 /* lvalue of scalar type : need to use lvalue type
5052 because of possible cast */
5053 t = vtop->type.t;
5054 t1 = t;
5055 /* compute memory access type */
5056 if (vtop->r & VT_LVAL_BYTE)
5057 t = VT_BYTE;
5058 else if (vtop->r & VT_LVAL_SHORT)
5059 t = VT_SHORT;
5060 if (vtop->r & VT_LVAL_UNSIGNED)
5061 t |= VT_UNSIGNED;
5062 vtop->type.t = t;
5063 load(r, vtop);
5064 /* restore wanted type */
5065 vtop->type.t = t1;
5066 } else {
5067 /* one register type load */
5068 load(r, vtop);
5071 vtop->r = r;
5072 #ifdef TCC_TARGET_C67
5073 /* uses register pairs for doubles */
5074 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5075 vtop->r2 = r+1;
5076 #endif
5078 return r;
5081 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5082 void gv2(int rc1, int rc2)
5084 int v;
5086 /* generate more generic register first. But VT_JMP or VT_CMP
5087 values must be generated first in all cases to avoid possible
5088 reload errors */
5089 v = vtop[0].r & VT_VALMASK;
5090 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5091 vswap();
5092 gv(rc1);
5093 vswap();
5094 gv(rc2);
5095 /* test if reload is needed for first register */
5096 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5097 vswap();
5098 gv(rc1);
5099 vswap();
5101 } else {
5102 gv(rc2);
5103 vswap();
5104 gv(rc1);
5105 vswap();
5106 /* test if reload is needed for first register */
5107 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5108 gv(rc2);
5113 /* expand long long on stack in two int registers */
5114 void lexpand(void)
5116 int u;
5118 u = vtop->type.t & VT_UNSIGNED;
5119 gv(RC_INT);
5120 vdup();
5121 vtop[0].r = vtop[-1].r2;
5122 vtop[0].r2 = VT_CONST;
5123 vtop[-1].r2 = VT_CONST;
5124 vtop[0].type.t = VT_INT | u;
5125 vtop[-1].type.t = VT_INT | u;
5128 #ifdef TCC_TARGET_ARM
5129 /* expand long long on stack */
5130 void lexpand_nr(void)
5132 int u,v;
5134 u = vtop->type.t & VT_UNSIGNED;
5135 vdup();
5136 vtop->r2 = VT_CONST;
5137 vtop->type.t = VT_INT | u;
5138 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5139 if (v == VT_CONST) {
5140 vtop[-1].c.ui = vtop->c.ull;
5141 vtop->c.ui = vtop->c.ull >> 32;
5142 vtop->r = VT_CONST;
5143 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5144 vtop->c.ui += 4;
5145 vtop->r = vtop[-1].r;
5146 } else if (v > VT_CONST) {
5147 vtop--;
5148 lexpand();
5149 } else
5150 vtop->r = vtop[-1].r2;
5151 vtop[-1].r2 = VT_CONST;
5152 vtop[-1].type.t = VT_INT | u;
5154 #endif
5156 /* build a long long from two ints */
5157 void lbuild(int t)
5159 gv2(RC_INT, RC_INT);
5160 vtop[-1].r2 = vtop[0].r;
5161 vtop[-1].type.t = t;
5162 vpop();
5165 /* rotate n first stack elements to the bottom
5166 I1 ... In -> I2 ... In I1 [top is right]
5168 void vrotb(int n)
5170 int i;
5171 SValue tmp;
5173 tmp = vtop[-n + 1];
5174 for(i=-n+1;i!=0;i++)
5175 vtop[i] = vtop[i+1];
5176 vtop[0] = tmp;
5179 /* rotate n first stack elements to the top
5180 I1 ... In -> In I1 ... I(n-1) [top is right]
5182 void vrott(int n)
5184 int i;
5185 SValue tmp;
5187 tmp = vtop[0];
5188 for(i = 0;i < n - 1; i++)
5189 vtop[-i] = vtop[-i - 1];
5190 vtop[-n + 1] = tmp;
5193 #ifdef TCC_TARGET_ARM
5194 /* like vrott but in other direction
5195 In ... I1 -> I(n-1) ... I1 In [top is right]
5197 void vnrott(int n)
5199 int i;
5200 SValue tmp;
5202 tmp = vtop[-n + 1];
5203 for(i = n - 1; i > 0; i--)
5204 vtop[-i] = vtop[-i + 1];
5205 vtop[0] = tmp;
5207 #endif
5209 /* pop stack value */
5210 void vpop(void)
5212 int v;
5213 v = vtop->r & VT_VALMASK;
5214 #ifdef TCC_TARGET_I386
5215 /* for x86, we need to pop the FP stack */
5216 if (v == TREG_ST0 && !nocode_wanted) {
5217 o(0xd9dd); /* fstp %st(1) */
5218 } else
5219 #endif
5220 if (v == VT_JMP || v == VT_JMPI) {
5221 /* need to put correct jump if && or || without test */
5222 gsym(vtop->c.ul);
5224 vtop--;
5227 /* convert stack entry to register and duplicate its value in another
5228 register */
5229 void gv_dup(void)
5231 int rc, t, r, r1;
5232 SValue sv;
5234 t = vtop->type.t;
5235 if ((t & VT_BTYPE) == VT_LLONG) {
5236 lexpand();
5237 gv_dup();
5238 vswap();
5239 vrotb(3);
5240 gv_dup();
5241 vrotb(4);
5242 /* stack: H L L1 H1 */
5243 lbuild(t);
5244 vrotb(3);
5245 vrotb(3);
5246 vswap();
5247 lbuild(t);
5248 vswap();
5249 } else {
5250 /* duplicate value */
5251 rc = RC_INT;
5252 sv.type.t = VT_INT;
5253 if (is_float(t)) {
5254 rc = RC_FLOAT;
5255 sv.type.t = t;
5257 r = gv(rc);
5258 r1 = get_reg(rc);
5259 sv.r = r;
5260 sv.c.ul = 0;
5261 load(r1, &sv); /* move r to r1 */
5262 vdup();
5263 /* duplicates value */
5264 vtop->r = r1;
5268 /* generate CPU independent (unsigned) long long operations */
5269 void gen_opl(int op)
5271 int t, a, b, op1, c, i;
5272 int func;
5273 unsigned short reg_iret = REG_IRET;
5274 unsigned short reg_lret = REG_LRET;
5275 SValue tmp;
5277 switch(op) {
5278 case '/':
5279 case TOK_PDIV:
5280 func = TOK___divdi3;
5281 goto gen_func;
5282 case TOK_UDIV:
5283 func = TOK___udivdi3;
5284 goto gen_func;
5285 case '%':
5286 func = TOK___moddi3;
5287 goto gen_mod_func;
5288 case TOK_UMOD:
5289 func = TOK___umoddi3;
5290 gen_mod_func:
5291 #ifdef TCC_ARM_EABI
5292 reg_iret = TREG_R2;
5293 reg_lret = TREG_R3;
5294 #endif
5295 gen_func:
5296 /* call generic long long function */
5297 vpush_global_sym(&func_old_type, func);
5298 vrott(3);
5299 gfunc_call(2);
5300 vpushi(0);
5301 vtop->r = reg_iret;
5302 vtop->r2 = reg_lret;
5303 break;
5304 case '^':
5305 case '&':
5306 case '|':
5307 case '*':
5308 case '+':
5309 case '-':
5310 t = vtop->type.t;
5311 vswap();
5312 lexpand();
5313 vrotb(3);
5314 lexpand();
5315 /* stack: L1 H1 L2 H2 */
5316 tmp = vtop[0];
5317 vtop[0] = vtop[-3];
5318 vtop[-3] = tmp;
5319 tmp = vtop[-2];
5320 vtop[-2] = vtop[-3];
5321 vtop[-3] = tmp;
5322 vswap();
5323 /* stack: H1 H2 L1 L2 */
5324 if (op == '*') {
5325 vpushv(vtop - 1);
5326 vpushv(vtop - 1);
5327 gen_op(TOK_UMULL);
5328 lexpand();
5329 /* stack: H1 H2 L1 L2 ML MH */
5330 for(i=0;i<4;i++)
5331 vrotb(6);
5332 /* stack: ML MH H1 H2 L1 L2 */
5333 tmp = vtop[0];
5334 vtop[0] = vtop[-2];
5335 vtop[-2] = tmp;
5336 /* stack: ML MH H1 L2 H2 L1 */
5337 gen_op('*');
5338 vrotb(3);
5339 vrotb(3);
5340 gen_op('*');
5341 /* stack: ML MH M1 M2 */
5342 gen_op('+');
5343 gen_op('+');
5344 } else if (op == '+' || op == '-') {
5345 /* XXX: add non carry method too (for MIPS or alpha) */
5346 if (op == '+')
5347 op1 = TOK_ADDC1;
5348 else
5349 op1 = TOK_SUBC1;
5350 gen_op(op1);
5351 /* stack: H1 H2 (L1 op L2) */
5352 vrotb(3);
5353 vrotb(3);
5354 gen_op(op1 + 1); /* TOK_xxxC2 */
5355 } else {
5356 gen_op(op);
5357 /* stack: H1 H2 (L1 op L2) */
5358 vrotb(3);
5359 vrotb(3);
5360 /* stack: (L1 op L2) H1 H2 */
5361 gen_op(op);
5362 /* stack: (L1 op L2) (H1 op H2) */
5364 /* stack: L H */
5365 lbuild(t);
5366 break;
5367 case TOK_SAR:
5368 case TOK_SHR:
5369 case TOK_SHL:
5370 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5371 t = vtop[-1].type.t;
5372 vswap();
5373 lexpand();
5374 vrotb(3);
5375 /* stack: L H shift */
5376 c = (int)vtop->c.i;
5377 /* constant: simpler */
5378 /* NOTE: all comments are for SHL. the other cases are
5379 done by swaping words */
5380 vpop();
5381 if (op != TOK_SHL)
5382 vswap();
5383 if (c >= 32) {
5384 /* stack: L H */
5385 vpop();
5386 if (c > 32) {
5387 vpushi(c - 32);
5388 gen_op(op);
5390 if (op != TOK_SAR) {
5391 vpushi(0);
5392 } else {
5393 gv_dup();
5394 vpushi(31);
5395 gen_op(TOK_SAR);
5397 vswap();
5398 } else {
5399 vswap();
5400 gv_dup();
5401 /* stack: H L L */
5402 vpushi(c);
5403 gen_op(op);
5404 vswap();
5405 vpushi(32 - c);
5406 if (op == TOK_SHL)
5407 gen_op(TOK_SHR);
5408 else
5409 gen_op(TOK_SHL);
5410 vrotb(3);
5411 /* stack: L L H */
5412 vpushi(c);
5413 if (op == TOK_SHL)
5414 gen_op(TOK_SHL);
5415 else
5416 gen_op(TOK_SHR);
5417 gen_op('|');
5419 if (op != TOK_SHL)
5420 vswap();
5421 lbuild(t);
5422 } else {
5423 /* XXX: should provide a faster fallback on x86 ? */
5424 switch(op) {
5425 case TOK_SAR:
5426 func = TOK___ashrdi3;
5427 goto gen_func;
5428 case TOK_SHR:
5429 func = TOK___lshrdi3;
5430 goto gen_func;
5431 case TOK_SHL:
5432 func = TOK___ashldi3;
5433 goto gen_func;
5436 break;
5437 default:
5438 /* compare operations */
5439 t = vtop->type.t;
5440 vswap();
5441 lexpand();
5442 vrotb(3);
5443 lexpand();
5444 /* stack: L1 H1 L2 H2 */
5445 tmp = vtop[-1];
5446 vtop[-1] = vtop[-2];
5447 vtop[-2] = tmp;
5448 /* stack: L1 L2 H1 H2 */
5449 /* compare high */
5450 op1 = op;
5451 /* when values are equal, we need to compare low words. since
5452 the jump is inverted, we invert the test too. */
5453 if (op1 == TOK_LT)
5454 op1 = TOK_LE;
5455 else if (op1 == TOK_GT)
5456 op1 = TOK_GE;
5457 else if (op1 == TOK_ULT)
5458 op1 = TOK_ULE;
5459 else if (op1 == TOK_UGT)
5460 op1 = TOK_UGE;
5461 a = 0;
5462 b = 0;
5463 gen_op(op1);
5464 if (op1 != TOK_NE) {
5465 a = gtst(1, 0);
5467 if (op != TOK_EQ) {
5468 /* generate non equal test */
5469 /* XXX: NOT PORTABLE yet */
5470 if (a == 0) {
5471 b = gtst(0, 0);
5472 } else {
5473 #if defined(TCC_TARGET_I386)
5474 b = psym(0x850f, 0);
5475 #elif defined(TCC_TARGET_ARM)
5476 b = ind;
5477 o(0x1A000000 | encbranch(ind, 0, 1));
5478 #elif defined(TCC_TARGET_C67)
5479 error("not implemented");
5480 #else
5481 #error not supported
5482 #endif
5485 /* compare low. Always unsigned */
5486 op1 = op;
5487 if (op1 == TOK_LT)
5488 op1 = TOK_ULT;
5489 else if (op1 == TOK_LE)
5490 op1 = TOK_ULE;
5491 else if (op1 == TOK_GT)
5492 op1 = TOK_UGT;
5493 else if (op1 == TOK_GE)
5494 op1 = TOK_UGE;
5495 gen_op(op1);
5496 a = gtst(1, a);
5497 gsym(b);
5498 vseti(VT_JMPI, a);
5499 break;
5503 /* handle integer constant optimizations and various machine
5504 independent opt */
5505 void gen_opic(int op)
5507 int c1, c2, t1, t2, n;
5508 SValue *v1, *v2;
5509 long long l1, l2;
5510 typedef unsigned long long U;
5512 v1 = vtop - 1;
5513 v2 = vtop;
5514 t1 = v1->type.t & VT_BTYPE;
5515 t2 = v2->type.t & VT_BTYPE;
5516 l1 = (t1 == VT_LLONG) ? v1->c.ll : v1->c.i;
5517 l2 = (t2 == VT_LLONG) ? v2->c.ll : v2->c.i;
5519 /* currently, we cannot do computations with forward symbols */
5520 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5521 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5522 if (c1 && c2) {
5523 switch(op) {
5524 case '+': l1 += l2; break;
5525 case '-': l1 -= l2; break;
5526 case '&': l1 &= l2; break;
5527 case '^': l1 ^= l2; break;
5528 case '|': l1 |= l2; break;
5529 case '*': l1 *= l2; break;
5531 case TOK_PDIV:
5532 case '/':
5533 case '%':
5534 case TOK_UDIV:
5535 case TOK_UMOD:
5536 /* if division by zero, generate explicit division */
5537 if (l2 == 0) {
5538 if (const_wanted)
5539 error("division by zero in constant");
5540 goto general_case;
5542 switch(op) {
5543 default: l1 /= l2; break;
5544 case '%': l1 %= l2; break;
5545 case TOK_UDIV: l1 = (U)l1 / l2; break;
5546 case TOK_UMOD: l1 = (U)l1 % l2; break;
5548 break;
5549 case TOK_SHL: l1 <<= l2; break;
5550 case TOK_SHR: l1 = (U)l1 >> l2; break;
5551 case TOK_SAR: l1 >>= l2; break;
5552 /* tests */
5553 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5554 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5555 case TOK_EQ: l1 = l1 == l2; break;
5556 case TOK_NE: l1 = l1 != l2; break;
5557 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5558 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5559 case TOK_LT: l1 = l1 < l2; break;
5560 case TOK_GE: l1 = l1 >= l2; break;
5561 case TOK_LE: l1 = l1 <= l2; break;
5562 case TOK_GT: l1 = l1 > l2; break;
5563 /* logical */
5564 case TOK_LAND: l1 = l1 && l2; break;
5565 case TOK_LOR: l1 = l1 || l2; break;
5566 default:
5567 goto general_case;
5569 v1->c.ll = l1;
5570 vtop--;
5571 } else {
5572 /* if commutative ops, put c2 as constant */
5573 if (c1 && (op == '+' || op == '&' || op == '^' ||
5574 op == '|' || op == '*')) {
5575 vswap();
5576 c2 = c1; //c = c1, c1 = c2, c2 = c;
5577 l2 = l1; //l = l1, l1 = l2, l2 = l;
5579 /* Filter out NOP operations like x*1, x-0, x&-1... */
5580 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5581 op == TOK_PDIV) &&
5582 l2 == 1) ||
5583 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5584 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5585 l2 == 0) ||
5586 (op == '&' &&
5587 l2 == -1))) {
5588 /* nothing to do */
5589 vtop--;
5590 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5591 /* try to use shifts instead of muls or divs */
5592 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5593 n = -1;
5594 while (l2) {
5595 l2 >>= 1;
5596 n++;
5598 vtop->c.ll = n;
5599 if (op == '*')
5600 op = TOK_SHL;
5601 else if (op == TOK_PDIV)
5602 op = TOK_SAR;
5603 else
5604 op = TOK_SHR;
5606 goto general_case;
5607 } else if (c2 && (op == '+' || op == '-') &&
5608 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5609 (VT_CONST | VT_SYM) ||
5610 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
5611 /* symbol + constant case */
5612 if (op == '-')
5613 l2 = -l2;
5614 vtop--;
5615 vtop->c.ll += l2;
5616 } else {
5617 general_case:
5618 if (!nocode_wanted) {
5619 /* call low level op generator */
5620 if (t1 == VT_LLONG || t2 == VT_LLONG)
5621 gen_opl(op);
5622 else
5623 gen_opi(op);
5624 } else {
5625 vtop--;
5631 /* generate a floating point operation with constant propagation */
5632 void gen_opif(int op)
5634 int c1, c2;
5635 SValue *v1, *v2;
5636 long double f1, f2;
5638 v1 = vtop - 1;
5639 v2 = vtop;
5640 /* currently, we cannot do computations with forward symbols */
5641 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5642 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5643 if (c1 && c2) {
5644 if (v1->type.t == VT_FLOAT) {
5645 f1 = v1->c.f;
5646 f2 = v2->c.f;
5647 } else if (v1->type.t == VT_DOUBLE) {
5648 f1 = v1->c.d;
5649 f2 = v2->c.d;
5650 } else {
5651 f1 = v1->c.ld;
5652 f2 = v2->c.ld;
5655 /* NOTE: we only do constant propagation if finite number (not
5656 NaN or infinity) (ANSI spec) */
5657 if (!ieee_finite(f1) || !ieee_finite(f2))
5658 goto general_case;
5660 switch(op) {
5661 case '+': f1 += f2; break;
5662 case '-': f1 -= f2; break;
5663 case '*': f1 *= f2; break;
5664 case '/':
5665 if (f2 == 0.0) {
5666 if (const_wanted)
5667 error("division by zero in constant");
5668 goto general_case;
5670 f1 /= f2;
5671 break;
5672 /* XXX: also handles tests ? */
5673 default:
5674 goto general_case;
5676 /* XXX: overflow test ? */
5677 if (v1->type.t == VT_FLOAT) {
5678 v1->c.f = f1;
5679 } else if (v1->type.t == VT_DOUBLE) {
5680 v1->c.d = f1;
5681 } else {
5682 v1->c.ld = f1;
5684 vtop--;
5685 } else {
5686 general_case:
5687 if (!nocode_wanted) {
5688 gen_opf(op);
5689 } else {
5690 vtop--;
5695 static int pointed_size(CType *type)
5697 int align;
5698 return type_size(pointed_type(type), &align);
5701 static inline int is_null_pointer(SValue *p)
5703 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5704 return 0;
5705 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5706 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5709 static inline int is_integer_btype(int bt)
5711 return (bt == VT_BYTE || bt == VT_SHORT ||
5712 bt == VT_INT || bt == VT_LLONG);
5715 /* check types for comparison or substraction of pointers */
5716 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5718 CType *type1, *type2, tmp_type1, tmp_type2;
5719 int bt1, bt2;
5721 /* null pointers are accepted for all comparisons as gcc */
5722 if (is_null_pointer(p1) || is_null_pointer(p2))
5723 return;
5724 type1 = &p1->type;
5725 type2 = &p2->type;
5726 bt1 = type1->t & VT_BTYPE;
5727 bt2 = type2->t & VT_BTYPE;
5728 /* accept comparison between pointer and integer with a warning */
5729 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5730 if (op != TOK_LOR && op != TOK_LAND )
5731 warning("comparison between pointer and integer");
5732 return;
5735 /* both must be pointers or implicit function pointers */
5736 if (bt1 == VT_PTR) {
5737 type1 = pointed_type(type1);
5738 } else if (bt1 != VT_FUNC)
5739 goto invalid_operands;
5741 if (bt2 == VT_PTR) {
5742 type2 = pointed_type(type2);
5743 } else if (bt2 != VT_FUNC) {
5744 invalid_operands:
5745 error("invalid operands to binary %s", get_tok_str(op, NULL));
5747 if ((type1->t & VT_BTYPE) == VT_VOID ||
5748 (type2->t & VT_BTYPE) == VT_VOID)
5749 return;
5750 tmp_type1 = *type1;
5751 tmp_type2 = *type2;
5752 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5753 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5754 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5755 /* gcc-like error if '-' is used */
5756 if (op == '-')
5757 goto invalid_operands;
5758 else
5759 warning("comparison of distinct pointer types lacks a cast");
5763 /* generic gen_op: handles types problems */
5764 void gen_op(int op)
5766 int u, t1, t2, bt1, bt2, t;
5767 CType type1;
5769 t1 = vtop[-1].type.t;
5770 t2 = vtop[0].type.t;
5771 bt1 = t1 & VT_BTYPE;
5772 bt2 = t2 & VT_BTYPE;
5774 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5775 /* at least one operand is a pointer */
5776 /* relationnal op: must be both pointers */
5777 if (op >= TOK_ULT && op <= TOK_LOR) {
5778 check_comparison_pointer_types(vtop - 1, vtop, op);
5779 /* pointers are handled are unsigned */
5780 t = VT_INT | VT_UNSIGNED;
5781 goto std_op;
5783 /* if both pointers, then it must be the '-' op */
5784 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5785 if (op != '-')
5786 error("cannot use pointers here");
5787 check_comparison_pointer_types(vtop - 1, vtop, op);
5788 /* XXX: check that types are compatible */
5789 u = pointed_size(&vtop[-1].type);
5790 gen_opic(op);
5791 /* set to integer type */
5792 vtop->type.t = VT_INT;
5793 vpushi(u);
5794 gen_op(TOK_PDIV);
5795 } else {
5796 /* exactly one pointer : must be '+' or '-'. */
5797 if (op != '-' && op != '+')
5798 error("cannot use pointers here");
5799 /* Put pointer as first operand */
5800 if (bt2 == VT_PTR) {
5801 vswap();
5802 swap(&t1, &t2);
5804 type1 = vtop[-1].type;
5805 /* XXX: cast to int ? (long long case) */
5806 vpushi(pointed_size(&vtop[-1].type));
5807 gen_op('*');
5808 #ifdef CONFIG_TCC_BCHECK
5809 /* if evaluating constant expression, no code should be
5810 generated, so no bound check */
5811 if (do_bounds_check && !const_wanted) {
5812 /* if bounded pointers, we generate a special code to
5813 test bounds */
5814 if (op == '-') {
5815 vpushi(0);
5816 vswap();
5817 gen_op('-');
5819 gen_bounded_ptr_add();
5820 } else
5821 #endif
5823 gen_opic(op);
5825 /* put again type if gen_opic() swaped operands */
5826 vtop->type = type1;
5828 } else if (is_float(bt1) || is_float(bt2)) {
5829 /* compute bigger type and do implicit casts */
5830 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5831 t = VT_LDOUBLE;
5832 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5833 t = VT_DOUBLE;
5834 } else {
5835 t = VT_FLOAT;
5837 /* floats can only be used for a few operations */
5838 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5839 (op < TOK_ULT || op > TOK_GT))
5840 error("invalid operands for binary operation");
5841 goto std_op;
5842 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5843 /* cast to biggest op */
5844 t = VT_LLONG;
5845 /* convert to unsigned if it does not fit in a long long */
5846 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5847 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5848 t |= VT_UNSIGNED;
5849 goto std_op;
5850 } else {
5851 /* integer operations */
5852 t = VT_INT;
5853 /* convert to unsigned if it does not fit in an integer */
5854 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5855 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5856 t |= VT_UNSIGNED;
5857 std_op:
5858 /* XXX: currently, some unsigned operations are explicit, so
5859 we modify them here */
5860 if (t & VT_UNSIGNED) {
5861 if (op == TOK_SAR)
5862 op = TOK_SHR;
5863 else if (op == '/')
5864 op = TOK_UDIV;
5865 else if (op == '%')
5866 op = TOK_UMOD;
5867 else if (op == TOK_LT)
5868 op = TOK_ULT;
5869 else if (op == TOK_GT)
5870 op = TOK_UGT;
5871 else if (op == TOK_LE)
5872 op = TOK_ULE;
5873 else if (op == TOK_GE)
5874 op = TOK_UGE;
5876 vswap();
5877 type1.t = t;
5878 gen_cast(&type1);
5879 vswap();
5880 /* special case for shifts and long long: we keep the shift as
5881 an integer */
5882 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5883 type1.t = VT_INT;
5884 gen_cast(&type1);
5885 if (is_float(t))
5886 gen_opif(op);
5887 else
5888 gen_opic(op);
5889 if (op >= TOK_ULT && op <= TOK_GT) {
5890 /* relationnal op: the result is an int */
5891 vtop->type.t = VT_INT;
5892 } else {
5893 vtop->type.t = t;
5898 #ifndef TCC_TARGET_ARM
5899 /* generic itof for unsigned long long case */
5900 void gen_cvt_itof1(int t)
5902 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5903 (VT_LLONG | VT_UNSIGNED)) {
5905 if (t == VT_FLOAT)
5906 vpush_global_sym(&func_old_type, TOK___floatundisf);
5907 #if LDOUBLE_SIZE != 8
5908 else if (t == VT_LDOUBLE)
5909 vpush_global_sym(&func_old_type, TOK___floatundixf);
5910 #endif
5911 else
5912 vpush_global_sym(&func_old_type, TOK___floatundidf);
5913 vrott(2);
5914 gfunc_call(1);
5915 vpushi(0);
5916 vtop->r = REG_FRET;
5917 } else {
5918 gen_cvt_itof(t);
5921 #endif
5923 /* generic ftoi for unsigned long long case */
5924 void gen_cvt_ftoi1(int t)
5926 int st;
5928 if (t == (VT_LLONG | VT_UNSIGNED)) {
5929 /* not handled natively */
5930 st = vtop->type.t & VT_BTYPE;
5931 if (st == VT_FLOAT)
5932 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5933 #if LDOUBLE_SIZE != 8
5934 else if (st == VT_LDOUBLE)
5935 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5936 #endif
5937 else
5938 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5939 vrott(2);
5940 gfunc_call(1);
5941 vpushi(0);
5942 vtop->r = REG_IRET;
5943 vtop->r2 = REG_LRET;
5944 } else {
5945 gen_cvt_ftoi(t);
5949 /* force char or short cast */
5950 void force_charshort_cast(int t)
5952 int bits, dbt;
5953 dbt = t & VT_BTYPE;
5954 /* XXX: add optimization if lvalue : just change type and offset */
5955 if (dbt == VT_BYTE)
5956 bits = 8;
5957 else
5958 bits = 16;
5959 if (t & VT_UNSIGNED) {
5960 vpushi((1 << bits) - 1);
5961 gen_op('&');
5962 } else {
5963 bits = 32 - bits;
5964 vpushi(bits);
5965 gen_op(TOK_SHL);
5966 /* result must be signed or the SAR is converted to an SHL
5967 This was not the case when "t" was a signed short
5968 and the last value on the stack was an unsigned int */
5969 vtop->type.t &= ~VT_UNSIGNED;
5970 vpushi(bits);
5971 gen_op(TOK_SAR);
5975 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5976 static void gen_cast(CType *type)
5978 int sbt, dbt, sf, df, c;
5980 /* special delayed cast for char/short */
5981 /* XXX: in some cases (multiple cascaded casts), it may still
5982 be incorrect */
5983 if (vtop->r & VT_MUSTCAST) {
5984 vtop->r &= ~VT_MUSTCAST;
5985 force_charshort_cast(vtop->type.t);
5988 /* bitfields first get cast to ints */
5989 if (vtop->type.t & VT_BITFIELD) {
5990 gv(RC_INT);
5993 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5994 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5996 if (sbt != dbt) {
5997 sf = is_float(sbt);
5998 df = is_float(dbt);
5999 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6000 if (c) {
6001 /* constant case: we can do it now */
6002 /* XXX: in ISOC, cannot do it if error in convert */
6003 if (sbt == VT_FLOAT)
6004 vtop->c.ld = vtop->c.f;
6005 else if (sbt == VT_DOUBLE)
6006 vtop->c.ld = vtop->c.d;
6008 if (df) {
6009 if ((sbt & VT_BTYPE) == VT_LLONG) {
6010 if (sbt & VT_UNSIGNED)
6011 vtop->c.ld = vtop->c.ull;
6012 else
6013 vtop->c.ld = vtop->c.ll;
6014 } else if(!sf) {
6015 if (sbt & VT_UNSIGNED)
6016 vtop->c.ld = vtop->c.ui;
6017 else
6018 vtop->c.ld = vtop->c.i;
6021 if (dbt == VT_FLOAT)
6022 vtop->c.f = (float)vtop->c.ld;
6023 else if (dbt == VT_DOUBLE)
6024 vtop->c.d = (double)vtop->c.ld;
6025 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
6026 vtop->c.ull = (unsigned long long)vtop->c.ld;
6027 } else if (sf && dbt == VT_BOOL) {
6028 vtop->c.i = (vtop->c.ld != 0);
6029 } else {
6030 if(sf)
6031 vtop->c.ll = (long long)vtop->c.ld;
6032 else if (sbt == (VT_LLONG|VT_UNSIGNED))
6033 vtop->c.ll = vtop->c.ull;
6034 else if (sbt & VT_UNSIGNED)
6035 vtop->c.ll = vtop->c.ui;
6036 else if (sbt != VT_LLONG)
6037 vtop->c.ll = vtop->c.i;
6039 if (dbt == (VT_LLONG|VT_UNSIGNED))
6040 vtop->c.ull = vtop->c.ll;
6041 else if (dbt == VT_BOOL)
6042 vtop->c.i = (vtop->c.ll != 0);
6043 else if (dbt != VT_LLONG) {
6044 int s = 0;
6045 if ((dbt & VT_BTYPE) == VT_BYTE)
6046 s = 24;
6047 else if ((dbt & VT_BTYPE) == VT_SHORT)
6048 s = 16;
6050 if(dbt & VT_UNSIGNED)
6051 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
6052 else
6053 vtop->c.i = ((int)vtop->c.ll << s) >> s;
6056 } else if (!nocode_wanted) {
6057 /* non constant case: generate code */
6058 if (sf && df) {
6059 /* convert from fp to fp */
6060 gen_cvt_ftof(dbt);
6061 } else if (df) {
6062 /* convert int to fp */
6063 gen_cvt_itof1(dbt);
6064 } else if (sf) {
6065 /* convert fp to int */
6066 if (dbt == VT_BOOL) {
6067 vpushi(0);
6068 gen_op(TOK_NE);
6069 } else {
6070 /* we handle char/short/etc... with generic code */
6071 if (dbt != (VT_INT | VT_UNSIGNED) &&
6072 dbt != (VT_LLONG | VT_UNSIGNED) &&
6073 dbt != VT_LLONG)
6074 dbt = VT_INT;
6075 gen_cvt_ftoi1(dbt);
6076 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6077 /* additional cast for char/short... */
6078 vtop->type.t = dbt;
6079 gen_cast(type);
6082 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6083 if ((sbt & VT_BTYPE) != VT_LLONG) {
6084 /* scalar to long long */
6085 /* machine independent conversion */
6086 gv(RC_INT);
6087 /* generate high word */
6088 if (sbt == (VT_INT | VT_UNSIGNED)) {
6089 vpushi(0);
6090 gv(RC_INT);
6091 } else {
6092 gv_dup();
6093 vpushi(31);
6094 gen_op(TOK_SAR);
6096 /* patch second register */
6097 vtop[-1].r2 = vtop->r;
6098 vpop();
6100 } else if (dbt == VT_BOOL) {
6101 /* scalar to bool */
6102 vpushi(0);
6103 gen_op(TOK_NE);
6104 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6105 (dbt & VT_BTYPE) == VT_SHORT) {
6106 if (sbt == VT_PTR) {
6107 vtop->type.t = VT_INT;
6108 warning("nonportable conversion from pointer to char/short");
6110 force_charshort_cast(dbt);
6111 } else if ((dbt & VT_BTYPE) == VT_INT) {
6112 /* scalar to int */
6113 if (sbt == VT_LLONG) {
6114 /* from long long: just take low order word */
6115 lexpand();
6116 vpop();
6118 /* if lvalue and single word type, nothing to do because
6119 the lvalue already contains the real type size (see
6120 VT_LVAL_xxx constants) */
6122 } else
6123 expect("constant expression");
6124 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6125 /* if we are casting between pointer types,
6126 we must update the VT_LVAL_xxx size */
6127 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6128 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6130 vtop->type = *type;
6133 /* return type size. Put alignment at 'a' */
6134 static int type_size(CType *type, int *a)
6136 Sym *s;
6137 int bt;
6139 bt = type->t & VT_BTYPE;
6140 if (bt == VT_STRUCT) {
6141 /* struct/union */
6142 s = type->ref;
6143 *a = s->r;
6144 return s->c;
6145 } else if (bt == VT_PTR) {
6146 if (type->t & VT_ARRAY) {
6147 s = type->ref;
6148 return type_size(&s->type, a) * s->c;
6149 } else {
6150 *a = PTR_SIZE;
6151 return PTR_SIZE;
6153 } else if (bt == VT_LDOUBLE) {
6154 *a = LDOUBLE_ALIGN;
6155 return LDOUBLE_SIZE;
6156 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6157 #ifdef TCC_TARGET_I386
6158 *a = 4;
6159 #elif defined(TCC_TARGET_ARM)
6160 #ifdef TCC_ARM_EABI
6161 *a = 8;
6162 #else
6163 *a = 4;
6164 #endif
6165 #else
6166 *a = 8;
6167 #endif
6168 return 8;
6169 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6170 *a = 4;
6171 return 4;
6172 } else if (bt == VT_SHORT) {
6173 *a = 2;
6174 return 2;
6175 } else {
6176 /* char, void, function, _Bool */
6177 *a = 1;
6178 return 1;
6182 /* return the pointed type of t */
6183 static inline CType *pointed_type(CType *type)
6185 return &type->ref->type;
6188 /* modify type so that its it is a pointer to type. */
6189 static void mk_pointer(CType *type)
6191 Sym *s;
6192 s = sym_push(SYM_FIELD, type, 0, -1);
6193 type->t = VT_PTR | (type->t & ~VT_TYPE);
6194 type->ref = s;
6197 /* compare function types. OLD functions match any new functions */
6198 static int is_compatible_func(CType *type1, CType *type2)
6200 Sym *s1, *s2;
6202 s1 = type1->ref;
6203 s2 = type2->ref;
6204 if (!is_compatible_types(&s1->type, &s2->type))
6205 return 0;
6206 /* check func_call */
6207 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6208 return 0;
6209 /* XXX: not complete */
6210 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6211 return 1;
6212 if (s1->c != s2->c)
6213 return 0;
6214 while (s1 != NULL) {
6215 if (s2 == NULL)
6216 return 0;
6217 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6218 return 0;
6219 s1 = s1->next;
6220 s2 = s2->next;
6222 if (s2)
6223 return 0;
6224 return 1;
6227 /* return true if type1 and type2 are the same. If unqualified is
6228 true, qualifiers on the types are ignored.
6230 - enums are not checked as gcc __builtin_types_compatible_p ()
6232 static int compare_types(CType *type1, CType *type2, int unqualified)
6234 int bt1, t1, t2;
6236 t1 = type1->t & VT_TYPE;
6237 t2 = type2->t & VT_TYPE;
6238 if (unqualified) {
6239 /* strip qualifiers before comparing */
6240 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6241 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6243 /* XXX: bitfields ? */
6244 if (t1 != t2)
6245 return 0;
6246 /* test more complicated cases */
6247 bt1 = t1 & VT_BTYPE;
6248 if (bt1 == VT_PTR) {
6249 type1 = pointed_type(type1);
6250 type2 = pointed_type(type2);
6251 return is_compatible_types(type1, type2);
6252 } else if (bt1 == VT_STRUCT) {
6253 return (type1->ref == type2->ref);
6254 } else if (bt1 == VT_FUNC) {
6255 return is_compatible_func(type1, type2);
6256 } else {
6257 return 1;
6261 /* return true if type1 and type2 are exactly the same (including
6262 qualifiers).
6264 static int is_compatible_types(CType *type1, CType *type2)
6266 return compare_types(type1,type2,0);
6269 /* return true if type1 and type2 are the same (ignoring qualifiers).
6271 static int is_compatible_parameter_types(CType *type1, CType *type2)
6273 return compare_types(type1,type2,1);
6276 /* print a type. If 'varstr' is not NULL, then the variable is also
6277 printed in the type */
6278 /* XXX: union */
6279 /* XXX: add array and function pointers */
6280 void type_to_str(char *buf, int buf_size,
6281 CType *type, const char *varstr)
6283 int bt, v, t;
6284 Sym *s, *sa;
6285 char buf1[256];
6286 const char *tstr;
6288 t = type->t & VT_TYPE;
6289 bt = t & VT_BTYPE;
6290 buf[0] = '\0';
6291 if (t & VT_CONSTANT)
6292 pstrcat(buf, buf_size, "const ");
6293 if (t & VT_VOLATILE)
6294 pstrcat(buf, buf_size, "volatile ");
6295 if (t & VT_UNSIGNED)
6296 pstrcat(buf, buf_size, "unsigned ");
6297 switch(bt) {
6298 case VT_VOID:
6299 tstr = "void";
6300 goto add_tstr;
6301 case VT_BOOL:
6302 tstr = "_Bool";
6303 goto add_tstr;
6304 case VT_BYTE:
6305 tstr = "char";
6306 goto add_tstr;
6307 case VT_SHORT:
6308 tstr = "short";
6309 goto add_tstr;
6310 case VT_INT:
6311 tstr = "int";
6312 goto add_tstr;
6313 case VT_LONG:
6314 tstr = "long";
6315 goto add_tstr;
6316 case VT_LLONG:
6317 tstr = "long long";
6318 goto add_tstr;
6319 case VT_FLOAT:
6320 tstr = "float";
6321 goto add_tstr;
6322 case VT_DOUBLE:
6323 tstr = "double";
6324 goto add_tstr;
6325 case VT_LDOUBLE:
6326 tstr = "long double";
6327 add_tstr:
6328 pstrcat(buf, buf_size, tstr);
6329 break;
6330 case VT_ENUM:
6331 case VT_STRUCT:
6332 if (bt == VT_STRUCT)
6333 tstr = "struct ";
6334 else
6335 tstr = "enum ";
6336 pstrcat(buf, buf_size, tstr);
6337 v = type->ref->v & ~SYM_STRUCT;
6338 if (v >= SYM_FIRST_ANOM)
6339 pstrcat(buf, buf_size, "<anonymous>");
6340 else
6341 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6342 break;
6343 case VT_FUNC:
6344 s = type->ref;
6345 type_to_str(buf, buf_size, &s->type, varstr);
6346 pstrcat(buf, buf_size, "(");
6347 sa = s->next;
6348 while (sa != NULL) {
6349 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6350 pstrcat(buf, buf_size, buf1);
6351 sa = sa->next;
6352 if (sa)
6353 pstrcat(buf, buf_size, ", ");
6355 pstrcat(buf, buf_size, ")");
6356 goto no_var;
6357 case VT_PTR:
6358 s = type->ref;
6359 pstrcpy(buf1, sizeof(buf1), "*");
6360 if (varstr)
6361 pstrcat(buf1, sizeof(buf1), varstr);
6362 type_to_str(buf, buf_size, &s->type, buf1);
6363 goto no_var;
6365 if (varstr) {
6366 pstrcat(buf, buf_size, " ");
6367 pstrcat(buf, buf_size, varstr);
6369 no_var: ;
6372 /* verify type compatibility to store vtop in 'dt' type, and generate
6373 casts if needed. */
6374 static void gen_assign_cast(CType *dt)
6376 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6377 char buf1[256], buf2[256];
6378 int dbt, sbt;
6380 st = &vtop->type; /* source type */
6381 dbt = dt->t & VT_BTYPE;
6382 sbt = st->t & VT_BTYPE;
6383 if (dt->t & VT_CONSTANT)
6384 warning("assignment of read-only location");
6385 switch(dbt) {
6386 case VT_PTR:
6387 /* special cases for pointers */
6388 /* '0' can also be a pointer */
6389 if (is_null_pointer(vtop))
6390 goto type_ok;
6391 /* accept implicit pointer to integer cast with warning */
6392 if (is_integer_btype(sbt)) {
6393 warning("assignment makes pointer from integer without a cast");
6394 goto type_ok;
6396 type1 = pointed_type(dt);
6397 /* a function is implicitely a function pointer */
6398 if (sbt == VT_FUNC) {
6399 if ((type1->t & VT_BTYPE) != VT_VOID &&
6400 !is_compatible_types(pointed_type(dt), st))
6401 goto error;
6402 else
6403 goto type_ok;
6405 if (sbt != VT_PTR)
6406 goto error;
6407 type2 = pointed_type(st);
6408 if ((type1->t & VT_BTYPE) == VT_VOID ||
6409 (type2->t & VT_BTYPE) == VT_VOID) {
6410 /* void * can match anything */
6411 } else {
6412 /* exact type match, except for unsigned */
6413 tmp_type1 = *type1;
6414 tmp_type2 = *type2;
6415 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6416 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6417 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6418 warning("assignment from incompatible pointer type");
6420 /* check const and volatile */
6421 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6422 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6423 warning("assignment discards qualifiers from pointer target type");
6424 break;
6425 case VT_BYTE:
6426 case VT_SHORT:
6427 case VT_INT:
6428 case VT_LLONG:
6429 if (sbt == VT_PTR || sbt == VT_FUNC) {
6430 warning("assignment makes integer from pointer without a cast");
6432 /* XXX: more tests */
6433 break;
6434 case VT_STRUCT:
6435 tmp_type1 = *dt;
6436 tmp_type2 = *st;
6437 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6438 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6439 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6440 error:
6441 type_to_str(buf1, sizeof(buf1), st, NULL);
6442 type_to_str(buf2, sizeof(buf2), dt, NULL);
6443 error("cannot cast '%s' to '%s'", buf1, buf2);
6445 break;
6447 type_ok:
6448 gen_cast(dt);
6451 /* store vtop in lvalue pushed on stack */
6452 void vstore(void)
6454 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6456 ft = vtop[-1].type.t;
6457 sbt = vtop->type.t & VT_BTYPE;
6458 dbt = ft & VT_BTYPE;
6459 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6460 (sbt == VT_INT && dbt == VT_SHORT)) {
6461 /* optimize char/short casts */
6462 delayed_cast = VT_MUSTCAST;
6463 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
6464 /* XXX: factorize */
6465 if (ft & VT_CONSTANT)
6466 warning("assignment of read-only location");
6467 } else {
6468 delayed_cast = 0;
6469 if (!(ft & VT_BITFIELD))
6470 gen_assign_cast(&vtop[-1].type);
6473 if (sbt == VT_STRUCT) {
6474 /* if structure, only generate pointer */
6475 /* structure assignment : generate memcpy */
6476 /* XXX: optimize if small size */
6477 if (!nocode_wanted) {
6478 size = type_size(&vtop->type, &align);
6480 #ifdef TCC_ARM_EABI
6481 if(!(align & 7))
6482 vpush_global_sym(&func_old_type, TOK_memcpy8);
6483 else if(!(align & 3))
6484 vpush_global_sym(&func_old_type, TOK_memcpy4);
6485 else
6486 #endif
6487 vpush_global_sym(&func_old_type, TOK_memcpy);
6489 /* destination */
6490 vpushv(vtop - 2);
6491 vtop->type.t = VT_INT;
6492 gaddrof();
6493 /* source */
6494 vpushv(vtop - 2);
6495 vtop->type.t = VT_INT;
6496 gaddrof();
6497 /* type size */
6498 vpushi(size);
6499 gfunc_call(3);
6501 vswap();
6502 vpop();
6503 } else {
6504 vswap();
6505 vpop();
6507 /* leave source on stack */
6508 } else if (ft & VT_BITFIELD) {
6509 /* bitfield store handling */
6510 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6511 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6512 /* remove bit field info to avoid loops */
6513 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6515 /* duplicate source into other register */
6516 gv_dup();
6517 vswap();
6518 vrott(3);
6520 if((ft & VT_BTYPE) == VT_BOOL) {
6521 gen_cast(&vtop[-1].type);
6522 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
6525 /* duplicate destination */
6526 vdup();
6527 vtop[-1] = vtop[-2];
6529 /* mask and shift source */
6530 if((ft & VT_BTYPE) != VT_BOOL) {
6531 vpushi((1 << bit_size) - 1);
6532 gen_op('&');
6534 vpushi(bit_pos);
6535 gen_op(TOK_SHL);
6536 /* load destination, mask and or with source */
6537 vswap();
6538 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6539 gen_op('&');
6540 gen_op('|');
6541 /* store result */
6542 vstore();
6544 /* pop off shifted source from "duplicate source..." above */
6545 vpop();
6547 } else {
6548 #ifdef CONFIG_TCC_BCHECK
6549 /* bound check case */
6550 if (vtop[-1].r & VT_MUSTBOUND) {
6551 vswap();
6552 gbound();
6553 vswap();
6555 #endif
6556 if (!nocode_wanted) {
6557 rc = RC_INT;
6558 if (is_float(ft))
6559 rc = RC_FLOAT;
6560 r = gv(rc); /* generate value */
6561 /* if lvalue was saved on stack, must read it */
6562 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6563 SValue sv;
6564 t = get_reg(RC_INT);
6565 sv.type.t = VT_INT;
6566 sv.r = VT_LOCAL | VT_LVAL;
6567 sv.c.ul = vtop[-1].c.ul;
6568 load(t, &sv);
6569 vtop[-1].r = t | VT_LVAL;
6571 store(r, vtop - 1);
6572 /* two word case handling : store second register at word + 4 */
6573 if ((ft & VT_BTYPE) == VT_LLONG) {
6574 vswap();
6575 /* convert to int to increment easily */
6576 vtop->type.t = VT_INT;
6577 gaddrof();
6578 vpushi(4);
6579 gen_op('+');
6580 vtop->r |= VT_LVAL;
6581 vswap();
6582 /* XXX: it works because r2 is spilled last ! */
6583 store(vtop->r2, vtop - 1);
6586 vswap();
6587 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6588 vtop->r |= delayed_cast;
6592 /* post defines POST/PRE add. c is the token ++ or -- */
6593 void inc(int post, int c)
6595 test_lvalue();
6596 vdup(); /* save lvalue */
6597 if (post) {
6598 gv_dup(); /* duplicate value */
6599 vrotb(3);
6600 vrotb(3);
6602 /* add constant */
6603 vpushi(c - TOK_MID);
6604 gen_op('+');
6605 vstore(); /* store value */
6606 if (post)
6607 vpop(); /* if post op, return saved value */
6610 /* Parse GNUC __attribute__ extension. Currently, the following
6611 extensions are recognized:
6612 - aligned(n) : set data/function alignment.
6613 - packed : force data alignment to 1
6614 - section(x) : generate data/code in this section.
6615 - unused : currently ignored, but may be used someday.
6616 - regparm(n) : pass function parameters in registers (i386 only)
6618 static void parse_attribute(AttributeDef *ad)
6620 int t, n;
6622 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6623 next();
6624 skip('(');
6625 skip('(');
6626 while (tok != ')') {
6627 if (tok < TOK_IDENT)
6628 expect("attribute name");
6629 t = tok;
6630 next();
6631 switch(t) {
6632 case TOK_SECTION1:
6633 case TOK_SECTION2:
6634 skip('(');
6635 if (tok != TOK_STR)
6636 expect("section name");
6637 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6638 next();
6639 skip(')');
6640 break;
6641 case TOK_ALIGNED1:
6642 case TOK_ALIGNED2:
6643 if (tok == '(') {
6644 next();
6645 n = expr_const();
6646 if (n <= 0 || (n & (n - 1)) != 0)
6647 error("alignment must be a positive power of two");
6648 skip(')');
6649 } else {
6650 n = MAX_ALIGN;
6652 ad->aligned = n;
6653 break;
6654 case TOK_PACKED1:
6655 case TOK_PACKED2:
6656 ad->packed = 1;
6657 break;
6658 case TOK_UNUSED1:
6659 case TOK_UNUSED2:
6660 /* currently, no need to handle it because tcc does not
6661 track unused objects */
6662 break;
6663 case TOK_NORETURN1:
6664 case TOK_NORETURN2:
6665 /* currently, no need to handle it because tcc does not
6666 track unused objects */
6667 break;
6668 case TOK_CDECL1:
6669 case TOK_CDECL2:
6670 case TOK_CDECL3:
6671 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6672 break;
6673 case TOK_STDCALL1:
6674 case TOK_STDCALL2:
6675 case TOK_STDCALL3:
6676 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6677 break;
6678 #ifdef TCC_TARGET_I386
6679 case TOK_REGPARM1:
6680 case TOK_REGPARM2:
6681 skip('(');
6682 n = expr_const();
6683 if (n > 3)
6684 n = 3;
6685 else if (n < 0)
6686 n = 0;
6687 if (n > 0)
6688 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6689 skip(')');
6690 break;
6691 case TOK_FASTCALL1:
6692 case TOK_FASTCALL2:
6693 case TOK_FASTCALL3:
6694 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6695 break;
6696 #endif
6697 case TOK_DLLEXPORT:
6698 FUNC_EXPORT(ad->func_attr) = 1;
6699 break;
6700 default:
6701 if (tcc_state->warn_unsupported)
6702 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6703 /* skip parameters */
6704 if (tok == '(') {
6705 int parenthesis = 0;
6706 do {
6707 if (tok == '(')
6708 parenthesis++;
6709 else if (tok == ')')
6710 parenthesis--;
6711 next();
6712 } while (parenthesis && tok != -1);
6714 break;
6716 if (tok != ',')
6717 break;
6718 next();
6720 skip(')');
6721 skip(')');
6725 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6726 static void struct_decl(CType *type, int u)
6728 int a, v, size, align, maxalign, c, offset;
6729 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
6730 Sym *s, *ss, *ass, **ps;
6731 AttributeDef ad;
6732 CType type1, btype;
6734 a = tok; /* save decl type */
6735 next();
6736 if (tok != '{') {
6737 v = tok;
6738 next();
6739 /* struct already defined ? return it */
6740 if (v < TOK_IDENT)
6741 expect("struct/union/enum name");
6742 s = struct_find(v);
6743 if (s) {
6744 if (s->type.t != a)
6745 error("invalid type");
6746 goto do_decl;
6748 } else {
6749 v = anon_sym++;
6751 type1.t = a;
6752 /* we put an undefined size for struct/union */
6753 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6754 s->r = 0; /* default alignment is zero as gcc */
6755 /* put struct/union/enum name in type */
6756 do_decl:
6757 type->t = u;
6758 type->ref = s;
6760 if (tok == '{') {
6761 next();
6762 if (s->c != -1)
6763 error("struct/union/enum already defined");
6764 /* cannot be empty */
6765 c = 0;
6766 /* non empty enums are not allowed */
6767 if (a == TOK_ENUM) {
6768 for(;;) {
6769 v = tok;
6770 if (v < TOK_UIDENT)
6771 expect("identifier");
6772 next();
6773 if (tok == '=') {
6774 next();
6775 c = expr_const();
6777 /* enum symbols have static storage */
6778 ss = sym_push(v, &int_type, VT_CONST, c);
6779 ss->type.t |= VT_STATIC;
6780 if (tok != ',')
6781 break;
6782 next();
6783 c++;
6784 /* NOTE: we accept a trailing comma */
6785 if (tok == '}')
6786 break;
6788 skip('}');
6789 } else {
6790 maxalign = 1;
6791 ps = &s->next;
6792 prevbt = VT_INT;
6793 bit_pos = 0;
6794 offset = 0;
6795 while (tok != '}') {
6796 parse_btype(&btype, &ad);
6797 while (1) {
6798 bit_size = -1;
6799 v = 0;
6800 type1 = btype;
6801 if (tok != ':') {
6802 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6803 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6804 expect("identifier");
6805 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6806 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6807 error("invalid type for '%s'",
6808 get_tok_str(v, NULL));
6810 if (tok == ':') {
6811 next();
6812 bit_size = expr_const();
6813 /* XXX: handle v = 0 case for messages */
6814 if (bit_size < 0)
6815 error("negative width in bit-field '%s'",
6816 get_tok_str(v, NULL));
6817 if (v && bit_size == 0)
6818 error("zero width for bit-field '%s'",
6819 get_tok_str(v, NULL));
6821 size = type_size(&type1, &align);
6822 if (ad.aligned) {
6823 if (align < ad.aligned)
6824 align = ad.aligned;
6825 } else if (ad.packed) {
6826 align = 1;
6827 } else if (*tcc_state->pack_stack_ptr) {
6828 if (align > *tcc_state->pack_stack_ptr)
6829 align = *tcc_state->pack_stack_ptr;
6831 lbit_pos = 0;
6832 if (bit_size >= 0) {
6833 bt = type1.t & VT_BTYPE;
6834 if (bt != VT_INT &&
6835 bt != VT_BYTE &&
6836 bt != VT_SHORT &&
6837 bt != VT_BOOL &&
6838 bt != VT_ENUM)
6839 error("bitfields must have scalar type");
6840 bsize = size * 8;
6841 if (bit_size > bsize) {
6842 error("width of '%s' exceeds its type",
6843 get_tok_str(v, NULL));
6844 } else if (bit_size == bsize) {
6845 /* no need for bit fields */
6846 bit_pos = 0;
6847 } else if (bit_size == 0) {
6848 /* XXX: what to do if only padding in a
6849 structure ? */
6850 /* zero size: means to pad */
6851 bit_pos = 0;
6852 } else {
6853 /* we do not have enough room ?
6854 did the type change?
6855 is it a union? */
6856 if ((bit_pos + bit_size) > bsize ||
6857 bt != prevbt || a == TOK_UNION)
6858 bit_pos = 0;
6859 lbit_pos = bit_pos;
6860 /* XXX: handle LSB first */
6861 type1.t |= VT_BITFIELD |
6862 (bit_pos << VT_STRUCT_SHIFT) |
6863 (bit_size << (VT_STRUCT_SHIFT + 6));
6864 bit_pos += bit_size;
6866 prevbt = bt;
6867 } else {
6868 bit_pos = 0;
6870 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
6871 /* add new memory data only if starting
6872 bit field */
6873 if (lbit_pos == 0) {
6874 if (a == TOK_STRUCT) {
6875 c = (c + align - 1) & -align;
6876 offset = c;
6877 if (size > 0)
6878 c += size;
6879 } else {
6880 offset = 0;
6881 if (size > c)
6882 c = size;
6884 if (align > maxalign)
6885 maxalign = align;
6887 #if 0
6888 printf("add field %s offset=%d",
6889 get_tok_str(v, NULL), offset);
6890 if (type1.t & VT_BITFIELD) {
6891 printf(" pos=%d size=%d",
6892 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6893 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6895 printf("\n");
6896 #endif
6898 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
6899 ass = type1.ref;
6900 while ((ass = ass->next) != NULL) {
6901 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
6902 *ps = ss;
6903 ps = &ss->next;
6905 } else if (v) {
6906 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6907 *ps = ss;
6908 ps = &ss->next;
6910 if (tok == ';' || tok == TOK_EOF)
6911 break;
6912 skip(',');
6914 skip(';');
6916 skip('}');
6917 /* store size and alignment */
6918 s->c = (c + maxalign - 1) & -maxalign;
6919 s->r = maxalign;
6924 /* return 0 if no type declaration. otherwise, return the basic type
6925 and skip it.
6927 static int parse_btype(CType *type, AttributeDef *ad)
6929 int t, u, type_found, typespec_found, typedef_found;
6930 Sym *s;
6931 CType type1;
6933 memset(ad, 0, sizeof(AttributeDef));
6934 type_found = 0;
6935 typespec_found = 0;
6936 typedef_found = 0;
6937 t = 0;
6938 while(1) {
6939 switch(tok) {
6940 case TOK_EXTENSION:
6941 /* currently, we really ignore extension */
6942 next();
6943 continue;
6945 /* basic types */
6946 case TOK_CHAR:
6947 u = VT_BYTE;
6948 basic_type:
6949 next();
6950 basic_type1:
6951 if ((t & VT_BTYPE) != 0)
6952 error("too many basic types");
6953 t |= u;
6954 typespec_found = 1;
6955 break;
6956 case TOK_VOID:
6957 u = VT_VOID;
6958 goto basic_type;
6959 case TOK_SHORT:
6960 u = VT_SHORT;
6961 goto basic_type;
6962 case TOK_INT:
6963 next();
6964 typespec_found = 1;
6965 break;
6966 case TOK_LONG:
6967 next();
6968 if ((t & VT_BTYPE) == VT_DOUBLE) {
6969 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6970 } else if ((t & VT_BTYPE) == VT_LONG) {
6971 t = (t & ~VT_BTYPE) | VT_LLONG;
6972 } else {
6973 u = VT_LONG;
6974 goto basic_type1;
6976 break;
6977 case TOK_BOOL:
6978 u = VT_BOOL;
6979 goto basic_type;
6980 case TOK_FLOAT:
6981 u = VT_FLOAT;
6982 goto basic_type;
6983 case TOK_DOUBLE:
6984 next();
6985 if ((t & VT_BTYPE) == VT_LONG) {
6986 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6987 } else {
6988 u = VT_DOUBLE;
6989 goto basic_type1;
6991 break;
6992 case TOK_ENUM:
6993 struct_decl(&type1, VT_ENUM);
6994 basic_type2:
6995 u = type1.t;
6996 type->ref = type1.ref;
6997 goto basic_type1;
6998 case TOK_STRUCT:
6999 case TOK_UNION:
7000 struct_decl(&type1, VT_STRUCT);
7001 goto basic_type2;
7003 /* type modifiers */
7004 case TOK_CONST1:
7005 case TOK_CONST2:
7006 case TOK_CONST3:
7007 t |= VT_CONSTANT;
7008 next();
7009 break;
7010 case TOK_VOLATILE1:
7011 case TOK_VOLATILE2:
7012 case TOK_VOLATILE3:
7013 t |= VT_VOLATILE;
7014 next();
7015 break;
7016 case TOK_SIGNED1:
7017 case TOK_SIGNED2:
7018 case TOK_SIGNED3:
7019 typespec_found = 1;
7020 t |= VT_SIGNED;
7021 next();
7022 break;
7023 case TOK_REGISTER:
7024 case TOK_AUTO:
7025 case TOK_RESTRICT1:
7026 case TOK_RESTRICT2:
7027 case TOK_RESTRICT3:
7028 next();
7029 break;
7030 case TOK_UNSIGNED:
7031 t |= VT_UNSIGNED;
7032 next();
7033 typespec_found = 1;
7034 break;
7036 /* storage */
7037 case TOK_EXTERN:
7038 t |= VT_EXTERN;
7039 next();
7040 break;
7041 case TOK_STATIC:
7042 t |= VT_STATIC;
7043 next();
7044 break;
7045 case TOK_TYPEDEF:
7046 t |= VT_TYPEDEF;
7047 next();
7048 break;
7049 case TOK_INLINE1:
7050 case TOK_INLINE2:
7051 case TOK_INLINE3:
7052 t |= VT_INLINE;
7053 next();
7054 break;
7056 /* GNUC attribute */
7057 case TOK_ATTRIBUTE1:
7058 case TOK_ATTRIBUTE2:
7059 parse_attribute(ad);
7060 break;
7061 /* GNUC typeof */
7062 case TOK_TYPEOF1:
7063 case TOK_TYPEOF2:
7064 case TOK_TYPEOF3:
7065 next();
7066 parse_expr_type(&type1);
7067 goto basic_type2;
7068 default:
7069 if (typespec_found || typedef_found)
7070 goto the_end;
7071 s = sym_find(tok);
7072 if (!s || !(s->type.t & VT_TYPEDEF))
7073 goto the_end;
7074 typedef_found = 1;
7075 t |= (s->type.t & ~VT_TYPEDEF);
7076 type->ref = s->type.ref;
7077 next();
7078 typespec_found = 1;
7079 break;
7081 type_found = 1;
7083 the_end:
7084 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7085 error("signed and unsigned modifier");
7086 if (tcc_state->char_is_unsigned) {
7087 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7088 t |= VT_UNSIGNED;
7090 t &= ~VT_SIGNED;
7092 /* long is never used as type */
7093 if ((t & VT_BTYPE) == VT_LONG)
7094 t = (t & ~VT_BTYPE) | VT_INT;
7095 type->t = t;
7096 return type_found;
7099 /* convert a function parameter type (array to pointer and function to
7100 function pointer) */
7101 static inline void convert_parameter_type(CType *pt)
7103 /* remove const and volatile qualifiers (XXX: const could be used
7104 to indicate a const function parameter */
7105 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7106 /* array must be transformed to pointer according to ANSI C */
7107 pt->t &= ~VT_ARRAY;
7108 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7109 mk_pointer(pt);
7113 static void post_type(CType *type, AttributeDef *ad)
7115 int n, l, t1, arg_size, align;
7116 Sym **plast, *s, *first;
7117 AttributeDef ad1;
7118 CType pt;
7120 if (tok == '(') {
7121 /* function declaration */
7122 next();
7123 l = 0;
7124 first = NULL;
7125 plast = &first;
7126 arg_size = 0;
7127 if (tok != ')') {
7128 for(;;) {
7129 /* read param name and compute offset */
7130 if (l != FUNC_OLD) {
7131 if (!parse_btype(&pt, &ad1)) {
7132 if (l) {
7133 error("invalid type");
7134 } else {
7135 l = FUNC_OLD;
7136 goto old_proto;
7139 l = FUNC_NEW;
7140 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7141 break;
7142 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7143 if ((pt.t & VT_BTYPE) == VT_VOID)
7144 error("parameter declared as void");
7145 arg_size += (type_size(&pt, &align) + 3) & ~3;
7146 } else {
7147 old_proto:
7148 n = tok;
7149 if (n < TOK_UIDENT)
7150 expect("identifier");
7151 pt.t = VT_INT;
7152 next();
7154 convert_parameter_type(&pt);
7155 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7156 *plast = s;
7157 plast = &s->next;
7158 if (tok == ')')
7159 break;
7160 skip(',');
7161 if (l == FUNC_NEW && tok == TOK_DOTS) {
7162 l = FUNC_ELLIPSIS;
7163 next();
7164 break;
7168 /* if no parameters, then old type prototype */
7169 if (l == 0)
7170 l = FUNC_OLD;
7171 skip(')');
7172 t1 = type->t & VT_STORAGE;
7173 /* NOTE: const is ignored in returned type as it has a special
7174 meaning in gcc / C++ */
7175 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7176 post_type(type, ad);
7177 /* we push a anonymous symbol which will contain the function prototype */
7178 FUNC_ARGS(ad->func_attr) = arg_size;
7179 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7180 s->next = first;
7181 type->t = t1 | VT_FUNC;
7182 type->ref = s;
7183 } else if (tok == '[') {
7184 /* array definition */
7185 next();
7186 n = -1;
7187 if (tok != ']') {
7188 n = expr_const();
7189 if (n < 0)
7190 error("invalid array size");
7192 skip(']');
7193 /* parse next post type */
7194 t1 = type->t & VT_STORAGE;
7195 type->t &= ~VT_STORAGE;
7196 post_type(type, ad);
7198 /* we push a anonymous symbol which will contain the array
7199 element type */
7200 s = sym_push(SYM_FIELD, type, 0, n);
7201 type->t = t1 | VT_ARRAY | VT_PTR;
7202 type->ref = s;
7206 /* Parse a type declaration (except basic type), and return the type
7207 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7208 expected. 'type' should contain the basic type. 'ad' is the
7209 attribute definition of the basic type. It can be modified by
7210 type_decl().
7212 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7214 Sym *s;
7215 CType type1, *type2;
7216 int qualifiers;
7218 while (tok == '*') {
7219 qualifiers = 0;
7220 redo:
7221 next();
7222 switch(tok) {
7223 case TOK_CONST1:
7224 case TOK_CONST2:
7225 case TOK_CONST3:
7226 qualifiers |= VT_CONSTANT;
7227 goto redo;
7228 case TOK_VOLATILE1:
7229 case TOK_VOLATILE2:
7230 case TOK_VOLATILE3:
7231 qualifiers |= VT_VOLATILE;
7232 goto redo;
7233 case TOK_RESTRICT1:
7234 case TOK_RESTRICT2:
7235 case TOK_RESTRICT3:
7236 goto redo;
7238 mk_pointer(type);
7239 type->t |= qualifiers;
7242 /* XXX: clarify attribute handling */
7243 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7244 parse_attribute(ad);
7246 /* recursive type */
7247 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7248 type1.t = 0; /* XXX: same as int */
7249 if (tok == '(') {
7250 next();
7251 /* XXX: this is not correct to modify 'ad' at this point, but
7252 the syntax is not clear */
7253 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7254 parse_attribute(ad);
7255 type_decl(&type1, ad, v, td);
7256 skip(')');
7257 } else {
7258 /* type identifier */
7259 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7260 *v = tok;
7261 next();
7262 } else {
7263 if (!(td & TYPE_ABSTRACT))
7264 expect("identifier");
7265 *v = 0;
7268 post_type(type, ad);
7269 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7270 parse_attribute(ad);
7271 if (!type1.t)
7272 return;
7273 /* append type at the end of type1 */
7274 type2 = &type1;
7275 for(;;) {
7276 s = type2->ref;
7277 type2 = &s->type;
7278 if (!type2->t) {
7279 *type2 = *type;
7280 break;
7283 *type = type1;
7286 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7287 static int lvalue_type(int t)
7289 int bt, r;
7290 r = VT_LVAL;
7291 bt = t & VT_BTYPE;
7292 if (bt == VT_BYTE || bt == VT_BOOL)
7293 r |= VT_LVAL_BYTE;
7294 else if (bt == VT_SHORT)
7295 r |= VT_LVAL_SHORT;
7296 else
7297 return r;
7298 if (t & VT_UNSIGNED)
7299 r |= VT_LVAL_UNSIGNED;
7300 return r;
7303 /* indirection with full error checking and bound check */
7304 static void indir(void)
7306 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7307 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7308 return;
7309 expect("pointer");
7311 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7312 gv(RC_INT);
7313 vtop->type = *pointed_type(&vtop->type);
7314 /* Arrays and functions are never lvalues */
7315 if (!(vtop->type.t & VT_ARRAY)
7316 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7317 vtop->r |= lvalue_type(vtop->type.t);
7318 /* if bound checking, the referenced pointer must be checked */
7319 if (do_bounds_check)
7320 vtop->r |= VT_MUSTBOUND;
7324 /* pass a parameter to a function and do type checking and casting */
7325 static void gfunc_param_typed(Sym *func, Sym *arg)
7327 int func_type;
7328 CType type;
7330 func_type = func->c;
7331 if (func_type == FUNC_OLD ||
7332 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7333 /* default casting : only need to convert float to double */
7334 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7335 type.t = VT_DOUBLE;
7336 gen_cast(&type);
7338 } else if (arg == NULL) {
7339 error("too many arguments to function");
7340 } else {
7341 type = arg->type;
7342 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7343 gen_assign_cast(&type);
7347 /* parse an expression of the form '(type)' or '(expr)' and return its
7348 type */
7349 static void parse_expr_type(CType *type)
7351 int n;
7352 AttributeDef ad;
7354 skip('(');
7355 if (parse_btype(type, &ad)) {
7356 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7357 } else {
7358 expr_type(type);
7360 skip(')');
7363 static void parse_type(CType *type)
7365 AttributeDef ad;
7366 int n;
7368 if (!parse_btype(type, &ad)) {
7369 expect("type");
7371 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7374 static void vpush_tokc(int t)
7376 CType type;
7377 type.t = t;
7378 vsetc(&type, VT_CONST, &tokc);
7381 static void unary(void)
7383 int n, t, align, size, r;
7384 CType type;
7385 Sym *s;
7386 AttributeDef ad;
7388 /* XXX: GCC 2.95.3 does not generate a table although it should be
7389 better here */
7390 tok_next:
7391 switch(tok) {
7392 case TOK_EXTENSION:
7393 next();
7394 goto tok_next;
7395 case TOK_CINT:
7396 case TOK_CCHAR:
7397 case TOK_LCHAR:
7398 vpushi(tokc.i);
7399 next();
7400 break;
7401 case TOK_CUINT:
7402 vpush_tokc(VT_INT | VT_UNSIGNED);
7403 next();
7404 break;
7405 case TOK_CLLONG:
7406 vpush_tokc(VT_LLONG);
7407 next();
7408 break;
7409 case TOK_CULLONG:
7410 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7411 next();
7412 break;
7413 case TOK_CFLOAT:
7414 vpush_tokc(VT_FLOAT);
7415 next();
7416 break;
7417 case TOK_CDOUBLE:
7418 vpush_tokc(VT_DOUBLE);
7419 next();
7420 break;
7421 case TOK_CLDOUBLE:
7422 vpush_tokc(VT_LDOUBLE);
7423 next();
7424 break;
7425 case TOK___FUNCTION__:
7426 if (!gnu_ext)
7427 goto tok_identifier;
7428 /* fall thru */
7429 case TOK___FUNC__:
7431 void *ptr;
7432 int len;
7433 /* special function name identifier */
7434 len = strlen(funcname) + 1;
7435 /* generate char[len] type */
7436 type.t = VT_BYTE;
7437 mk_pointer(&type);
7438 type.t |= VT_ARRAY;
7439 type.ref->c = len;
7440 vpush_ref(&type, data_section, data_section->data_offset, len);
7441 ptr = section_ptr_add(data_section, len);
7442 memcpy(ptr, funcname, len);
7443 next();
7445 break;
7446 case TOK_LSTR:
7447 #ifdef TCC_TARGET_PE
7448 t = VT_SHORT | VT_UNSIGNED;
7449 #else
7450 t = VT_INT;
7451 #endif
7452 goto str_init;
7453 case TOK_STR:
7454 /* string parsing */
7455 t = VT_BYTE;
7456 str_init:
7457 if (tcc_state->warn_write_strings)
7458 t |= VT_CONSTANT;
7459 type.t = t;
7460 mk_pointer(&type);
7461 type.t |= VT_ARRAY;
7462 memset(&ad, 0, sizeof(AttributeDef));
7463 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7464 break;
7465 case '(':
7466 next();
7467 /* cast ? */
7468 if (parse_btype(&type, &ad)) {
7469 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7470 skip(')');
7471 /* check ISOC99 compound literal */
7472 if (tok == '{') {
7473 /* data is allocated locally by default */
7474 if (global_expr)
7475 r = VT_CONST;
7476 else
7477 r = VT_LOCAL;
7478 /* all except arrays are lvalues */
7479 if (!(type.t & VT_ARRAY))
7480 r |= lvalue_type(type.t);
7481 memset(&ad, 0, sizeof(AttributeDef));
7482 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7483 } else {
7484 unary();
7485 gen_cast(&type);
7487 } else if (tok == '{') {
7488 /* save all registers */
7489 save_regs(0);
7490 /* statement expression : we do not accept break/continue
7491 inside as GCC does */
7492 block(NULL, NULL, NULL, NULL, 0, 1);
7493 skip(')');
7494 } else {
7495 gexpr();
7496 skip(')');
7498 break;
7499 case '*':
7500 next();
7501 unary();
7502 indir();
7503 break;
7504 case '&':
7505 next();
7506 unary();
7507 /* functions names must be treated as function pointers,
7508 except for unary '&' and sizeof. Since we consider that
7509 functions are not lvalues, we only have to handle it
7510 there and in function calls. */
7511 /* arrays can also be used although they are not lvalues */
7512 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7513 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7514 test_lvalue();
7515 mk_pointer(&vtop->type);
7516 gaddrof();
7517 break;
7518 case '!':
7519 next();
7520 unary();
7521 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
7522 CType boolean;
7523 boolean.t = VT_BOOL;
7524 gen_cast(&boolean);
7525 vtop->c.i = !vtop->c.i;
7526 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
7527 vtop->c.i = vtop->c.i ^ 1;
7528 else {
7529 save_regs(1);
7530 vseti(VT_JMP, gtst(1, 0));
7532 break;
7533 case '~':
7534 next();
7535 unary();
7536 vpushi(-1);
7537 gen_op('^');
7538 break;
7539 case '+':
7540 next();
7541 /* in order to force cast, we add zero */
7542 unary();
7543 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7544 error("pointer not accepted for unary plus");
7545 vpushi(0);
7546 gen_op('+');
7547 break;
7548 case TOK_SIZEOF:
7549 case TOK_ALIGNOF1:
7550 case TOK_ALIGNOF2:
7551 t = tok;
7552 next();
7553 if (tok == '(') {
7554 parse_expr_type(&type);
7555 } else {
7556 unary_type(&type);
7558 size = type_size(&type, &align);
7559 if (t == TOK_SIZEOF) {
7560 if (size < 0)
7561 error("sizeof applied to an incomplete type");
7562 vpushi(size);
7563 } else {
7564 vpushi(align);
7566 vtop->type.t |= VT_UNSIGNED;
7567 break;
7569 case TOK_builtin_types_compatible_p:
7571 CType type1, type2;
7572 next();
7573 skip('(');
7574 parse_type(&type1);
7575 skip(',');
7576 parse_type(&type2);
7577 skip(')');
7578 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7579 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7580 vpushi(is_compatible_types(&type1, &type2));
7582 break;
7583 case TOK_builtin_constant_p:
7585 int saved_nocode_wanted, res;
7586 next();
7587 skip('(');
7588 saved_nocode_wanted = nocode_wanted;
7589 nocode_wanted = 1;
7590 gexpr();
7591 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7592 vpop();
7593 nocode_wanted = saved_nocode_wanted;
7594 skip(')');
7595 vpushi(res);
7597 break;
7598 case TOK_INC:
7599 case TOK_DEC:
7600 t = tok;
7601 next();
7602 unary();
7603 inc(0, t);
7604 break;
7605 case '-':
7606 next();
7607 vpushi(0);
7608 unary();
7609 gen_op('-');
7610 break;
7611 case TOK_LAND:
7612 if (!gnu_ext)
7613 goto tok_identifier;
7614 next();
7615 /* allow to take the address of a label */
7616 if (tok < TOK_UIDENT)
7617 expect("label identifier");
7618 s = label_find(tok);
7619 if (!s) {
7620 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7621 } else {
7622 if (s->r == LABEL_DECLARED)
7623 s->r = LABEL_FORWARD;
7625 if (!s->type.t) {
7626 s->type.t = VT_VOID;
7627 mk_pointer(&s->type);
7628 s->type.t |= VT_STATIC;
7630 vset(&s->type, VT_CONST | VT_SYM, 0);
7631 vtop->sym = s;
7632 next();
7633 break;
7634 default:
7635 tok_identifier:
7636 t = tok;
7637 next();
7638 if (t < TOK_UIDENT)
7639 expect("identifier");
7640 s = sym_find(t);
7641 if (!s) {
7642 if (tok != '(')
7643 error("'%s' undeclared", get_tok_str(t, NULL));
7644 /* for simple function calls, we tolerate undeclared
7645 external reference to int() function */
7646 if (tcc_state->warn_implicit_function_declaration)
7647 warning("implicit declaration of function '%s'",
7648 get_tok_str(t, NULL));
7649 s = external_global_sym(t, &func_old_type, 0);
7651 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7652 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7653 /* if referencing an inline function, then we generate a
7654 symbol to it if not already done. It will have the
7655 effect to generate code for it at the end of the
7656 compilation unit. Inline function as always
7657 generated in the text section. */
7658 if (!s->c)
7659 put_extern_sym(s, text_section, 0, 0);
7660 r = VT_SYM | VT_CONST;
7661 } else {
7662 r = s->r;
7664 vset(&s->type, r, s->c);
7665 /* if forward reference, we must point to s */
7666 if (vtop->r & VT_SYM) {
7667 vtop->sym = s;
7668 vtop->c.ul = 0;
7670 break;
7673 /* post operations */
7674 while (1) {
7675 if (tok == TOK_INC || tok == TOK_DEC) {
7676 inc(1, tok);
7677 next();
7678 } else if (tok == '.' || tok == TOK_ARROW) {
7679 /* field */
7680 if (tok == TOK_ARROW)
7681 indir();
7682 test_lvalue();
7683 gaddrof();
7684 next();
7685 /* expect pointer on structure */
7686 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7687 expect("struct or union");
7688 s = vtop->type.ref;
7689 /* find field */
7690 tok |= SYM_FIELD;
7691 while ((s = s->next) != NULL) {
7692 if (s->v == tok)
7693 break;
7695 if (!s)
7696 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7697 /* add field offset to pointer */
7698 vtop->type = char_pointer_type; /* change type to 'char *' */
7699 vpushi(s->c);
7700 gen_op('+');
7701 /* change type to field type, and set to lvalue */
7702 vtop->type = s->type;
7703 /* an array is never an lvalue */
7704 if (!(vtop->type.t & VT_ARRAY)) {
7705 vtop->r |= lvalue_type(vtop->type.t);
7706 /* if bound checking, the referenced pointer must be checked */
7707 if (do_bounds_check)
7708 vtop->r |= VT_MUSTBOUND;
7710 next();
7711 } else if (tok == '[') {
7712 next();
7713 gexpr();
7714 gen_op('+');
7715 indir();
7716 skip(']');
7717 } else if (tok == '(') {
7718 SValue ret;
7719 Sym *sa;
7720 int nb_args;
7722 /* function call */
7723 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7724 /* pointer test (no array accepted) */
7725 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7726 vtop->type = *pointed_type(&vtop->type);
7727 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7728 goto error_func;
7729 } else {
7730 error_func:
7731 expect("function pointer");
7733 } else {
7734 vtop->r &= ~VT_LVAL; /* no lvalue */
7736 /* get return type */
7737 s = vtop->type.ref;
7738 next();
7739 sa = s->next; /* first parameter */
7740 nb_args = 0;
7741 ret.r2 = VT_CONST;
7742 /* compute first implicit argument if a structure is returned */
7743 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7744 /* get some space for the returned structure */
7745 size = type_size(&s->type, &align);
7746 loc = (loc - size) & -align;
7747 ret.type = s->type;
7748 ret.r = VT_LOCAL | VT_LVAL;
7749 /* pass it as 'int' to avoid structure arg passing
7750 problems */
7751 vseti(VT_LOCAL, loc);
7752 ret.c = vtop->c;
7753 nb_args++;
7754 } else {
7755 ret.type = s->type;
7756 /* return in register */
7757 if (is_float(ret.type.t)) {
7758 ret.r = REG_FRET;
7759 } else {
7760 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7761 ret.r2 = REG_LRET;
7762 ret.r = REG_IRET;
7764 ret.c.i = 0;
7766 if (tok != ')') {
7767 for(;;) {
7768 expr_eq();
7769 gfunc_param_typed(s, sa);
7770 nb_args++;
7771 if (sa)
7772 sa = sa->next;
7773 if (tok == ')')
7774 break;
7775 skip(',');
7778 if (sa)
7779 error("too few arguments to function");
7780 skip(')');
7781 if (!nocode_wanted) {
7782 gfunc_call(nb_args);
7783 } else {
7784 vtop -= (nb_args + 1);
7786 /* return value */
7787 vsetc(&ret.type, ret.r, &ret.c);
7788 vtop->r2 = ret.r2;
7789 } else {
7790 break;
7795 static void uneq(void)
7797 int t;
7799 unary();
7800 if (tok == '=' ||
7801 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7802 tok == TOK_A_XOR || tok == TOK_A_OR ||
7803 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7804 test_lvalue();
7805 t = tok;
7806 next();
7807 if (t == '=') {
7808 expr_eq();
7809 } else {
7810 vdup();
7811 expr_eq();
7812 gen_op(t & 0x7f);
7814 vstore();
7818 static void expr_prod(void)
7820 int t;
7822 uneq();
7823 while (tok == '*' || tok == '/' || tok == '%') {
7824 t = tok;
7825 next();
7826 uneq();
7827 gen_op(t);
7831 static void expr_sum(void)
7833 int t;
7835 expr_prod();
7836 while (tok == '+' || tok == '-') {
7837 t = tok;
7838 next();
7839 expr_prod();
7840 gen_op(t);
7844 static void expr_shift(void)
7846 int t;
7848 expr_sum();
7849 while (tok == TOK_SHL || tok == TOK_SAR) {
7850 t = tok;
7851 next();
7852 expr_sum();
7853 gen_op(t);
7857 static void expr_cmp(void)
7859 int t;
7861 expr_shift();
7862 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7863 tok == TOK_ULT || tok == TOK_UGE) {
7864 t = tok;
7865 next();
7866 expr_shift();
7867 gen_op(t);
7871 static void expr_cmpeq(void)
7873 int t;
7875 expr_cmp();
7876 while (tok == TOK_EQ || tok == TOK_NE) {
7877 t = tok;
7878 next();
7879 expr_cmp();
7880 gen_op(t);
7884 static void expr_and(void)
7886 expr_cmpeq();
7887 while (tok == '&') {
7888 next();
7889 expr_cmpeq();
7890 gen_op('&');
7894 static void expr_xor(void)
7896 expr_and();
7897 while (tok == '^') {
7898 next();
7899 expr_and();
7900 gen_op('^');
7904 static void expr_or(void)
7906 expr_xor();
7907 while (tok == '|') {
7908 next();
7909 expr_xor();
7910 gen_op('|');
7914 /* XXX: fix this mess */
7915 static void expr_land_const(void)
7917 expr_or();
7918 while (tok == TOK_LAND) {
7919 next();
7920 expr_or();
7921 gen_op(TOK_LAND);
7925 /* XXX: fix this mess */
7926 static void expr_lor_const(void)
7928 expr_land_const();
7929 while (tok == TOK_LOR) {
7930 next();
7931 expr_land_const();
7932 gen_op(TOK_LOR);
7936 /* only used if non constant */
7937 static void expr_land(void)
7939 int t;
7941 expr_or();
7942 if (tok == TOK_LAND) {
7943 t = 0;
7944 save_regs(1);
7945 for(;;) {
7946 t = gtst(1, t);
7947 if (tok != TOK_LAND) {
7948 vseti(VT_JMPI, t);
7949 break;
7951 next();
7952 expr_or();
7957 static void expr_lor(void)
7959 int t;
7961 expr_land();
7962 if (tok == TOK_LOR) {
7963 t = 0;
7964 save_regs(1);
7965 for(;;) {
7966 t = gtst(0, t);
7967 if (tok != TOK_LOR) {
7968 vseti(VT_JMP, t);
7969 break;
7971 next();
7972 expr_land();
7977 /* XXX: better constant handling */
7978 static void expr_eq(void)
7980 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7981 SValue sv;
7982 CType type, type1, type2;
7984 if (const_wanted) {
7985 expr_lor_const();
7986 if (tok == '?') {
7987 CType boolean;
7988 int c;
7989 boolean.t = VT_BOOL;
7990 vdup();
7991 gen_cast(&boolean);
7992 c = vtop->c.i;
7993 vpop();
7994 next();
7995 if (tok != ':' || !gnu_ext) {
7996 vpop();
7997 gexpr();
7999 if (!c)
8000 vpop();
8001 skip(':');
8002 expr_eq();
8003 if (c)
8004 vpop();
8006 } else {
8007 expr_lor();
8008 if (tok == '?') {
8009 next();
8010 if (vtop != vstack) {
8011 /* needed to avoid having different registers saved in
8012 each branch */
8013 if (is_float(vtop->type.t))
8014 rc = RC_FLOAT;
8015 else
8016 rc = RC_INT;
8017 gv(rc);
8018 save_regs(1);
8020 if (tok == ':' && gnu_ext) {
8021 gv_dup();
8022 tt = gtst(1, 0);
8023 } else {
8024 tt = gtst(1, 0);
8025 gexpr();
8027 type1 = vtop->type;
8028 sv = *vtop; /* save value to handle it later */
8029 vtop--; /* no vpop so that FP stack is not flushed */
8030 skip(':');
8031 u = gjmp(0);
8032 gsym(tt);
8033 expr_eq();
8034 type2 = vtop->type;
8036 t1 = type1.t;
8037 bt1 = t1 & VT_BTYPE;
8038 t2 = type2.t;
8039 bt2 = t2 & VT_BTYPE;
8040 /* cast operands to correct type according to ISOC rules */
8041 if (is_float(bt1) || is_float(bt2)) {
8042 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8043 type.t = VT_LDOUBLE;
8044 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8045 type.t = VT_DOUBLE;
8046 } else {
8047 type.t = VT_FLOAT;
8049 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8050 /* cast to biggest op */
8051 type.t = VT_LLONG;
8052 /* convert to unsigned if it does not fit in a long long */
8053 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8054 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8055 type.t |= VT_UNSIGNED;
8056 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8057 /* XXX: test pointer compatibility */
8058 type = type1;
8059 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8060 /* XXX: test function pointer compatibility */
8061 type = type1;
8062 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8063 /* XXX: test structure compatibility */
8064 type = type1;
8065 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8066 /* NOTE: as an extension, we accept void on only one side */
8067 type.t = VT_VOID;
8068 } else {
8069 /* integer operations */
8070 type.t = VT_INT;
8071 /* convert to unsigned if it does not fit in an integer */
8072 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8073 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8074 type.t |= VT_UNSIGNED;
8077 /* now we convert second operand */
8078 gen_cast(&type);
8079 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8080 gaddrof();
8081 rc = RC_INT;
8082 if (is_float(type.t)) {
8083 rc = RC_FLOAT;
8084 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8085 /* for long longs, we use fixed registers to avoid having
8086 to handle a complicated move */
8087 rc = RC_IRET;
8090 r2 = gv(rc);
8091 /* this is horrible, but we must also convert first
8092 operand */
8093 tt = gjmp(0);
8094 gsym(u);
8095 /* put again first value and cast it */
8096 *vtop = sv;
8097 gen_cast(&type);
8098 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8099 gaddrof();
8100 r1 = gv(rc);
8101 move_reg(r2, r1);
8102 vtop->r = r2;
8103 gsym(tt);
8108 static void gexpr(void)
8110 while (1) {
8111 expr_eq();
8112 if (tok != ',')
8113 break;
8114 vpop();
8115 next();
8119 /* parse an expression and return its type without any side effect. */
8120 static void expr_type(CType *type)
8122 int saved_nocode_wanted;
8124 saved_nocode_wanted = nocode_wanted;
8125 nocode_wanted = 1;
8126 gexpr();
8127 *type = vtop->type;
8128 vpop();
8129 nocode_wanted = saved_nocode_wanted;
8132 /* parse a unary expression and return its type without any side
8133 effect. */
8134 static void unary_type(CType *type)
8136 int a;
8138 a = nocode_wanted;
8139 nocode_wanted = 1;
8140 unary();
8141 *type = vtop->type;
8142 vpop();
8143 nocode_wanted = a;
8146 /* parse a constant expression and return value in vtop. */
8147 static void expr_const1(void)
8149 int a;
8150 a = const_wanted;
8151 const_wanted = 1;
8152 expr_eq();
8153 const_wanted = a;
8156 /* parse an integer constant and return its value. */
8157 static int expr_const(void)
8159 int c;
8160 expr_const1();
8161 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8162 expect("constant expression");
8163 c = vtop->c.i;
8164 vpop();
8165 return c;
8168 /* return the label token if current token is a label, otherwise
8169 return zero */
8170 static int is_label(void)
8172 int last_tok;
8174 /* fast test first */
8175 if (tok < TOK_UIDENT)
8176 return 0;
8177 /* no need to save tokc because tok is an identifier */
8178 last_tok = tok;
8179 next();
8180 if (tok == ':') {
8181 next();
8182 return last_tok;
8183 } else {
8184 unget_tok(last_tok);
8185 return 0;
8189 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8190 int case_reg, int is_expr)
8192 int a, b, c, d;
8193 Sym *s;
8195 /* generate line number info */
8196 if (do_debug &&
8197 (last_line_num != file->line_num || last_ind != ind)) {
8198 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8199 last_ind = ind;
8200 last_line_num = file->line_num;
8203 if (is_expr) {
8204 /* default return value is (void) */
8205 vpushi(0);
8206 vtop->type.t = VT_VOID;
8209 if (tok == TOK_IF) {
8210 /* if test */
8211 next();
8212 skip('(');
8213 gexpr();
8214 skip(')');
8215 a = gtst(1, 0);
8216 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8217 c = tok;
8218 if (c == TOK_ELSE) {
8219 next();
8220 d = gjmp(0);
8221 gsym(a);
8222 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8223 gsym(d); /* patch else jmp */
8224 } else
8225 gsym(a);
8226 } else if (tok == TOK_WHILE) {
8227 next();
8228 d = ind;
8229 skip('(');
8230 gexpr();
8231 skip(')');
8232 a = gtst(1, 0);
8233 b = 0;
8234 block(&a, &b, case_sym, def_sym, case_reg, 0);
8235 gjmp_addr(d);
8236 gsym(a);
8237 gsym_addr(b, d);
8238 } else if (tok == '{') {
8239 Sym *llabel;
8241 next();
8242 /* record local declaration stack position */
8243 s = local_stack;
8244 llabel = local_label_stack;
8245 /* handle local labels declarations */
8246 if (tok == TOK_LABEL) {
8247 next();
8248 for(;;) {
8249 if (tok < TOK_UIDENT)
8250 expect("label identifier");
8251 label_push(&local_label_stack, tok, LABEL_DECLARED);
8252 next();
8253 if (tok == ',') {
8254 next();
8255 } else {
8256 skip(';');
8257 break;
8261 while (tok != '}') {
8262 decl(VT_LOCAL);
8263 if (tok != '}') {
8264 if (is_expr)
8265 vpop();
8266 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8269 /* pop locally defined labels */
8270 label_pop(&local_label_stack, llabel);
8271 /* pop locally defined symbols */
8272 if(is_expr) {
8273 /* XXX: this solution makes only valgrind happy...
8274 triggered by gcc.c-torture/execute/20000917-1.c */
8275 Sym *p;
8276 switch(vtop->type.t & VT_BTYPE) {
8277 case VT_PTR:
8278 case VT_STRUCT:
8279 case VT_ENUM:
8280 case VT_FUNC:
8281 for(p=vtop->type.ref;p;p=p->prev)
8282 if(p->prev==s)
8283 error("unsupported expression type");
8286 sym_pop(&local_stack, s);
8287 next();
8288 } else if (tok == TOK_RETURN) {
8289 next();
8290 if (tok != ';') {
8291 gexpr();
8292 gen_assign_cast(&func_vt);
8293 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8294 CType type;
8295 /* if returning structure, must copy it to implicit
8296 first pointer arg location */
8297 #ifdef TCC_ARM_EABI
8298 int align, size;
8299 size = type_size(&func_vt,&align);
8300 if(size <= 4)
8302 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8303 && (align & 3))
8305 int addr;
8306 loc = (loc - size) & -4;
8307 addr = loc;
8308 type = func_vt;
8309 vset(&type, VT_LOCAL | VT_LVAL, addr);
8310 vswap();
8311 vstore();
8312 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8314 vtop->type = int_type;
8315 gv(RC_IRET);
8316 } else {
8317 #endif
8318 type = func_vt;
8319 mk_pointer(&type);
8320 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8321 indir();
8322 vswap();
8323 /* copy structure value to pointer */
8324 vstore();
8325 #ifdef TCC_ARM_EABI
8327 #endif
8328 } else if (is_float(func_vt.t)) {
8329 gv(RC_FRET);
8330 } else {
8331 gv(RC_IRET);
8333 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8335 skip(';');
8336 rsym = gjmp(rsym); /* jmp */
8337 } else if (tok == TOK_BREAK) {
8338 /* compute jump */
8339 if (!bsym)
8340 error("cannot break");
8341 *bsym = gjmp(*bsym);
8342 next();
8343 skip(';');
8344 } else if (tok == TOK_CONTINUE) {
8345 /* compute jump */
8346 if (!csym)
8347 error("cannot continue");
8348 *csym = gjmp(*csym);
8349 next();
8350 skip(';');
8351 } else if (tok == TOK_FOR) {
8352 int e;
8353 next();
8354 skip('(');
8355 if (tok != ';') {
8356 gexpr();
8357 vpop();
8359 skip(';');
8360 d = ind;
8361 c = ind;
8362 a = 0;
8363 b = 0;
8364 if (tok != ';') {
8365 gexpr();
8366 a = gtst(1, 0);
8368 skip(';');
8369 if (tok != ')') {
8370 e = gjmp(0);
8371 c = ind;
8372 gexpr();
8373 vpop();
8374 gjmp_addr(d);
8375 gsym(e);
8377 skip(')');
8378 block(&a, &b, case_sym, def_sym, case_reg, 0);
8379 gjmp_addr(c);
8380 gsym(a);
8381 gsym_addr(b, c);
8382 } else
8383 if (tok == TOK_DO) {
8384 next();
8385 a = 0;
8386 b = 0;
8387 d = ind;
8388 block(&a, &b, case_sym, def_sym, case_reg, 0);
8389 skip(TOK_WHILE);
8390 skip('(');
8391 gsym(b);
8392 gexpr();
8393 c = gtst(0, 0);
8394 gsym_addr(c, d);
8395 skip(')');
8396 gsym(a);
8397 skip(';');
8398 } else
8399 if (tok == TOK_SWITCH) {
8400 next();
8401 skip('(');
8402 gexpr();
8403 /* XXX: other types than integer */
8404 case_reg = gv(RC_INT);
8405 vpop();
8406 skip(')');
8407 a = 0;
8408 b = gjmp(0); /* jump to first case */
8409 c = 0;
8410 block(&a, csym, &b, &c, case_reg, 0);
8411 /* if no default, jmp after switch */
8412 if (c == 0)
8413 c = ind;
8414 /* default label */
8415 gsym_addr(b, c);
8416 /* break label */
8417 gsym(a);
8418 } else
8419 if (tok == TOK_CASE) {
8420 int v1, v2;
8421 if (!case_sym)
8422 expect("switch");
8423 next();
8424 v1 = expr_const();
8425 v2 = v1;
8426 if (gnu_ext && tok == TOK_DOTS) {
8427 next();
8428 v2 = expr_const();
8429 if (v2 < v1)
8430 warning("empty case range");
8432 /* since a case is like a label, we must skip it with a jmp */
8433 b = gjmp(0);
8434 gsym(*case_sym);
8435 vseti(case_reg, 0);
8436 vpushi(v1);
8437 if (v1 == v2) {
8438 gen_op(TOK_EQ);
8439 *case_sym = gtst(1, 0);
8440 } else {
8441 gen_op(TOK_GE);
8442 *case_sym = gtst(1, 0);
8443 vseti(case_reg, 0);
8444 vpushi(v2);
8445 gen_op(TOK_LE);
8446 *case_sym = gtst(1, *case_sym);
8448 gsym(b);
8449 skip(':');
8450 is_expr = 0;
8451 goto block_after_label;
8452 } else
8453 if (tok == TOK_DEFAULT) {
8454 next();
8455 skip(':');
8456 if (!def_sym)
8457 expect("switch");
8458 if (*def_sym)
8459 error("too many 'default'");
8460 *def_sym = ind;
8461 is_expr = 0;
8462 goto block_after_label;
8463 } else
8464 if (tok == TOK_GOTO) {
8465 next();
8466 if (tok == '*' && gnu_ext) {
8467 /* computed goto */
8468 next();
8469 gexpr();
8470 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8471 expect("pointer");
8472 ggoto();
8473 } else if (tok >= TOK_UIDENT) {
8474 s = label_find(tok);
8475 /* put forward definition if needed */
8476 if (!s) {
8477 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8478 } else {
8479 if (s->r == LABEL_DECLARED)
8480 s->r = LABEL_FORWARD;
8482 /* label already defined */
8483 if (s->r & LABEL_FORWARD)
8484 s->next = (void *)gjmp((long)s->next);
8485 else
8486 gjmp_addr((long)s->next);
8487 next();
8488 } else {
8489 expect("label identifier");
8491 skip(';');
8492 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8493 asm_instr();
8494 } else {
8495 b = is_label();
8496 if (b) {
8497 /* label case */
8498 s = label_find(b);
8499 if (s) {
8500 if (s->r == LABEL_DEFINED)
8501 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8502 gsym((long)s->next);
8503 s->r = LABEL_DEFINED;
8504 } else {
8505 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8507 s->next = (void *)ind;
8508 /* we accept this, but it is a mistake */
8509 block_after_label:
8510 if (tok == '}') {
8511 warning("deprecated use of label at end of compound statement");
8512 } else {
8513 if (is_expr)
8514 vpop();
8515 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8517 } else {
8518 /* expression case */
8519 if (tok != ';') {
8520 if (is_expr) {
8521 vpop();
8522 gexpr();
8523 } else {
8524 gexpr();
8525 vpop();
8528 skip(';');
8533 /* t is the array or struct type. c is the array or struct
8534 address. cur_index/cur_field is the pointer to the current
8535 value. 'size_only' is true if only size info is needed (only used
8536 in arrays) */
8537 static void decl_designator(CType *type, Section *sec, unsigned long c,
8538 int *cur_index, Sym **cur_field,
8539 int size_only)
8541 Sym *s, *f;
8542 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8543 CType type1;
8545 notfirst = 0;
8546 elem_size = 0;
8547 nb_elems = 1;
8548 if (gnu_ext && (l = is_label()) != 0)
8549 goto struct_field;
8550 while (tok == '[' || tok == '.') {
8551 if (tok == '[') {
8552 if (!(type->t & VT_ARRAY))
8553 expect("array type");
8554 s = type->ref;
8555 next();
8556 index = expr_const();
8557 if (index < 0 || (s->c >= 0 && index >= s->c))
8558 expect("invalid index");
8559 if (tok == TOK_DOTS && gnu_ext) {
8560 next();
8561 index_last = expr_const();
8562 if (index_last < 0 ||
8563 (s->c >= 0 && index_last >= s->c) ||
8564 index_last < index)
8565 expect("invalid index");
8566 } else {
8567 index_last = index;
8569 skip(']');
8570 if (!notfirst)
8571 *cur_index = index_last;
8572 type = pointed_type(type);
8573 elem_size = type_size(type, &align);
8574 c += index * elem_size;
8575 /* NOTE: we only support ranges for last designator */
8576 nb_elems = index_last - index + 1;
8577 if (nb_elems != 1) {
8578 notfirst = 1;
8579 break;
8581 } else {
8582 next();
8583 l = tok;
8584 next();
8585 struct_field:
8586 if ((type->t & VT_BTYPE) != VT_STRUCT)
8587 expect("struct/union type");
8588 s = type->ref;
8589 l |= SYM_FIELD;
8590 f = s->next;
8591 while (f) {
8592 if (f->v == l)
8593 break;
8594 f = f->next;
8596 if (!f)
8597 expect("field");
8598 if (!notfirst)
8599 *cur_field = f;
8600 /* XXX: fix this mess by using explicit storage field */
8601 type1 = f->type;
8602 type1.t |= (type->t & ~VT_TYPE);
8603 type = &type1;
8604 c += f->c;
8606 notfirst = 1;
8608 if (notfirst) {
8609 if (tok == '=') {
8610 next();
8611 } else {
8612 if (!gnu_ext)
8613 expect("=");
8615 } else {
8616 if (type->t & VT_ARRAY) {
8617 index = *cur_index;
8618 type = pointed_type(type);
8619 c += index * type_size(type, &align);
8620 } else {
8621 f = *cur_field;
8622 if (!f)
8623 error("too many field init");
8624 /* XXX: fix this mess by using explicit storage field */
8625 type1 = f->type;
8626 type1.t |= (type->t & ~VT_TYPE);
8627 type = &type1;
8628 c += f->c;
8631 decl_initializer(type, sec, c, 0, size_only);
8633 /* XXX: make it more general */
8634 if (!size_only && nb_elems > 1) {
8635 unsigned long c_end;
8636 uint8_t *src, *dst;
8637 int i;
8639 if (!sec)
8640 error("range init not supported yet for dynamic storage");
8641 c_end = c + nb_elems * elem_size;
8642 if (c_end > sec->data_allocated)
8643 section_realloc(sec, c_end);
8644 src = sec->data + c;
8645 dst = src;
8646 for(i = 1; i < nb_elems; i++) {
8647 dst += elem_size;
8648 memcpy(dst, src, elem_size);
8653 #define EXPR_VAL 0
8654 #define EXPR_CONST 1
8655 #define EXPR_ANY 2
8657 /* store a value or an expression directly in global data or in local array */
8658 static void init_putv(CType *type, Section *sec, unsigned long c,
8659 int v, int expr_type)
8661 int saved_global_expr, bt, bit_pos, bit_size;
8662 void *ptr;
8663 unsigned long long bit_mask;
8664 CType dtype;
8666 switch(expr_type) {
8667 case EXPR_VAL:
8668 vpushi(v);
8669 break;
8670 case EXPR_CONST:
8671 /* compound literals must be allocated globally in this case */
8672 saved_global_expr = global_expr;
8673 global_expr = 1;
8674 expr_const1();
8675 global_expr = saved_global_expr;
8676 /* NOTE: symbols are accepted */
8677 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8678 error("initializer element is not constant");
8679 break;
8680 case EXPR_ANY:
8681 expr_eq();
8682 break;
8685 dtype = *type;
8686 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8688 if (sec) {
8689 /* XXX: not portable */
8690 /* XXX: generate error if incorrect relocation */
8691 gen_assign_cast(&dtype);
8692 bt = type->t & VT_BTYPE;
8693 ptr = sec->data + c;
8694 /* XXX: make code faster ? */
8695 if (!(type->t & VT_BITFIELD)) {
8696 bit_pos = 0;
8697 bit_size = 32;
8698 bit_mask = -1LL;
8699 } else {
8700 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8701 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8702 bit_mask = (1LL << bit_size) - 1;
8704 if ((vtop->r & VT_SYM) &&
8705 (bt == VT_BYTE ||
8706 bt == VT_SHORT ||
8707 bt == VT_DOUBLE ||
8708 bt == VT_LDOUBLE ||
8709 bt == VT_LLONG ||
8710 (bt == VT_INT && bit_size != 32)))
8711 error("initializer element is not computable at load time");
8712 switch(bt) {
8713 case VT_BOOL:
8714 vtop->c.i = (vtop->c.i != 0);
8715 case VT_BYTE:
8716 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8717 break;
8718 case VT_SHORT:
8719 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8720 break;
8721 case VT_DOUBLE:
8722 *(double *)ptr = vtop->c.d;
8723 break;
8724 case VT_LDOUBLE:
8725 *(long double *)ptr = vtop->c.ld;
8726 break;
8727 case VT_LLONG:
8728 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8729 break;
8730 default:
8731 if (vtop->r & VT_SYM) {
8732 greloc(sec, vtop->sym, c, R_DATA_32);
8734 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8735 break;
8737 vtop--;
8738 } else {
8739 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8740 vswap();
8741 vstore();
8742 vpop();
8746 /* put zeros for variable based init */
8747 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8749 if (sec) {
8750 /* nothing to do because globals are already set to zero */
8751 } else {
8752 vpush_global_sym(&func_old_type, TOK_memset);
8753 vseti(VT_LOCAL, c);
8754 vpushi(0);
8755 vpushi(size);
8756 gfunc_call(3);
8760 /* 't' contains the type and storage info. 'c' is the offset of the
8761 object in section 'sec'. If 'sec' is NULL, it means stack based
8762 allocation. 'first' is true if array '{' must be read (multi
8763 dimension implicit array init handling). 'size_only' is true if
8764 size only evaluation is wanted (only for arrays). */
8765 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8766 int first, int size_only)
8768 int index, array_length, n, no_oblock, nb, parlevel, i;
8769 int size1, align1, expr_type;
8770 Sym *s, *f;
8771 CType *t1;
8773 if (type->t & VT_ARRAY) {
8774 s = type->ref;
8775 n = s->c;
8776 array_length = 0;
8777 t1 = pointed_type(type);
8778 size1 = type_size(t1, &align1);
8780 no_oblock = 1;
8781 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8782 tok == '{') {
8783 skip('{');
8784 no_oblock = 0;
8787 /* only parse strings here if correct type (otherwise: handle
8788 them as ((w)char *) expressions */
8789 if ((tok == TOK_LSTR &&
8790 #ifdef TCC_TARGET_PE
8791 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8792 #else
8793 (t1->t & VT_BTYPE) == VT_INT
8794 #endif
8795 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8796 while (tok == TOK_STR || tok == TOK_LSTR) {
8797 int cstr_len, ch;
8798 CString *cstr;
8800 cstr = tokc.cstr;
8801 /* compute maximum number of chars wanted */
8802 if (tok == TOK_STR)
8803 cstr_len = cstr->size;
8804 else
8805 cstr_len = cstr->size / sizeof(nwchar_t);
8806 cstr_len--;
8807 nb = cstr_len;
8808 if (n >= 0 && nb > (n - array_length))
8809 nb = n - array_length;
8810 if (!size_only) {
8811 if (cstr_len > nb)
8812 warning("initializer-string for array is too long");
8813 /* in order to go faster for common case (char
8814 string in global variable, we handle it
8815 specifically */
8816 if (sec && tok == TOK_STR && size1 == 1) {
8817 memcpy(sec->data + c + array_length, cstr->data, nb);
8818 } else {
8819 for(i=0;i<nb;i++) {
8820 if (tok == TOK_STR)
8821 ch = ((unsigned char *)cstr->data)[i];
8822 else
8823 ch = ((nwchar_t *)cstr->data)[i];
8824 init_putv(t1, sec, c + (array_length + i) * size1,
8825 ch, EXPR_VAL);
8829 array_length += nb;
8830 next();
8832 /* only add trailing zero if enough storage (no
8833 warning in this case since it is standard) */
8834 if (n < 0 || array_length < n) {
8835 if (!size_only) {
8836 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8838 array_length++;
8840 } else {
8841 index = 0;
8842 while (tok != '}') {
8843 decl_designator(type, sec, c, &index, NULL, size_only);
8844 if (n >= 0 && index >= n)
8845 error("index too large");
8846 /* must put zero in holes (note that doing it that way
8847 ensures that it even works with designators) */
8848 if (!size_only && array_length < index) {
8849 init_putz(t1, sec, c + array_length * size1,
8850 (index - array_length) * size1);
8852 index++;
8853 if (index > array_length)
8854 array_length = index;
8855 /* special test for multi dimensional arrays (may not
8856 be strictly correct if designators are used at the
8857 same time) */
8858 if (index >= n && no_oblock)
8859 break;
8860 if (tok == '}')
8861 break;
8862 skip(',');
8865 if (!no_oblock)
8866 skip('}');
8867 /* put zeros at the end */
8868 if (!size_only && n >= 0 && array_length < n) {
8869 init_putz(t1, sec, c + array_length * size1,
8870 (n - array_length) * size1);
8872 /* patch type size if needed */
8873 if (n < 0)
8874 s->c = array_length;
8875 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8876 (sec || !first || tok == '{')) {
8877 int par_count;
8879 /* NOTE: the previous test is a specific case for automatic
8880 struct/union init */
8881 /* XXX: union needs only one init */
8883 /* XXX: this test is incorrect for local initializers
8884 beginning with ( without {. It would be much more difficult
8885 to do it correctly (ideally, the expression parser should
8886 be used in all cases) */
8887 par_count = 0;
8888 if (tok == '(') {
8889 AttributeDef ad1;
8890 CType type1;
8891 next();
8892 while (tok == '(') {
8893 par_count++;
8894 next();
8896 if (!parse_btype(&type1, &ad1))
8897 expect("cast");
8898 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8899 #if 0
8900 if (!is_assignable_types(type, &type1))
8901 error("invalid type for cast");
8902 #endif
8903 skip(')');
8905 no_oblock = 1;
8906 if (first || tok == '{') {
8907 skip('{');
8908 no_oblock = 0;
8910 s = type->ref;
8911 f = s->next;
8912 array_length = 0;
8913 index = 0;
8914 n = s->c;
8915 while (tok != '}') {
8916 decl_designator(type, sec, c, NULL, &f, size_only);
8917 index = f->c;
8918 if (!size_only && array_length < index) {
8919 init_putz(type, sec, c + array_length,
8920 index - array_length);
8922 index = index + type_size(&f->type, &align1);
8923 if (index > array_length)
8924 array_length = index;
8925 f = f->next;
8926 if (no_oblock && f == NULL)
8927 break;
8928 if (tok == '}')
8929 break;
8930 skip(',');
8932 /* put zeros at the end */
8933 if (!size_only && array_length < n) {
8934 init_putz(type, sec, c + array_length,
8935 n - array_length);
8937 if (!no_oblock)
8938 skip('}');
8939 while (par_count) {
8940 skip(')');
8941 par_count--;
8943 } else if (tok == '{') {
8944 next();
8945 decl_initializer(type, sec, c, first, size_only);
8946 skip('}');
8947 } else if (size_only) {
8948 /* just skip expression */
8949 parlevel = 0;
8950 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8951 tok != -1) {
8952 if (tok == '(')
8953 parlevel++;
8954 else if (tok == ')')
8955 parlevel--;
8956 next();
8958 } else {
8959 /* currently, we always use constant expression for globals
8960 (may change for scripting case) */
8961 expr_type = EXPR_CONST;
8962 if (!sec)
8963 expr_type = EXPR_ANY;
8964 init_putv(type, sec, c, 0, expr_type);
8968 /* parse an initializer for type 't' if 'has_init' is non zero, and
8969 allocate space in local or global data space ('r' is either
8970 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8971 variable 'v' of scope 'scope' is declared before initializers are
8972 parsed. If 'v' is zero, then a reference to the new object is put
8973 in the value stack. If 'has_init' is 2, a special parsing is done
8974 to handle string constants. */
8975 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8976 int has_init, int v, int scope)
8978 int size, align, addr, data_offset;
8979 int level;
8980 ParseState saved_parse_state;
8981 TokenString init_str;
8982 Section *sec;
8984 size = type_size(type, &align);
8985 /* If unknown size, we must evaluate it before
8986 evaluating initializers because
8987 initializers can generate global data too
8988 (e.g. string pointers or ISOC99 compound
8989 literals). It also simplifies local
8990 initializers handling */
8991 tok_str_new(&init_str);
8992 if (size < 0) {
8993 if (!has_init)
8994 error("unknown type size");
8995 /* get all init string */
8996 if (has_init == 2) {
8997 /* only get strings */
8998 while (tok == TOK_STR || tok == TOK_LSTR) {
8999 tok_str_add_tok(&init_str);
9000 next();
9002 } else {
9003 level = 0;
9004 while (level > 0 || (tok != ',' && tok != ';')) {
9005 if (tok < 0)
9006 error("unexpected end of file in initializer");
9007 tok_str_add_tok(&init_str);
9008 if (tok == '{')
9009 level++;
9010 else if (tok == '}') {
9011 if (level == 0)
9012 break;
9013 level--;
9015 next();
9018 tok_str_add(&init_str, -1);
9019 tok_str_add(&init_str, 0);
9021 /* compute size */
9022 save_parse_state(&saved_parse_state);
9024 macro_ptr = init_str.str;
9025 next();
9026 decl_initializer(type, NULL, 0, 1, 1);
9027 /* prepare second initializer parsing */
9028 macro_ptr = init_str.str;
9029 next();
9031 /* if still unknown size, error */
9032 size = type_size(type, &align);
9033 if (size < 0)
9034 error("unknown type size");
9036 /* take into account specified alignment if bigger */
9037 if (ad->aligned) {
9038 if (ad->aligned > align)
9039 align = ad->aligned;
9040 } else if (ad->packed) {
9041 align = 1;
9043 if ((r & VT_VALMASK) == VT_LOCAL) {
9044 sec = NULL;
9045 if (do_bounds_check && (type->t & VT_ARRAY))
9046 loc--;
9047 loc = (loc - size) & -align;
9048 addr = loc;
9049 /* handles bounds */
9050 /* XXX: currently, since we do only one pass, we cannot track
9051 '&' operators, so we add only arrays */
9052 if (do_bounds_check && (type->t & VT_ARRAY)) {
9053 unsigned long *bounds_ptr;
9054 /* add padding between regions */
9055 loc--;
9056 /* then add local bound info */
9057 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9058 bounds_ptr[0] = addr;
9059 bounds_ptr[1] = size;
9061 if (v) {
9062 /* local variable */
9063 sym_push(v, type, r, addr);
9064 } else {
9065 /* push local reference */
9066 vset(type, r, addr);
9068 } else {
9069 Sym *sym;
9071 sym = NULL;
9072 if (v && scope == VT_CONST) {
9073 /* see if the symbol was already defined */
9074 sym = sym_find(v);
9075 if (sym) {
9076 if (!is_compatible_types(&sym->type, type))
9077 error("incompatible types for redefinition of '%s'",
9078 get_tok_str(v, NULL));
9079 if (sym->type.t & VT_EXTERN) {
9080 /* if the variable is extern, it was not allocated */
9081 sym->type.t &= ~VT_EXTERN;
9082 /* set array size if it was ommited in extern
9083 declaration */
9084 if ((sym->type.t & VT_ARRAY) &&
9085 sym->type.ref->c < 0 &&
9086 type->ref->c >= 0)
9087 sym->type.ref->c = type->ref->c;
9088 } else {
9089 /* we accept several definitions of the same
9090 global variable. this is tricky, because we
9091 must play with the SHN_COMMON type of the symbol */
9092 /* XXX: should check if the variable was already
9093 initialized. It is incorrect to initialized it
9094 twice */
9095 /* no init data, we won't add more to the symbol */
9096 if (!has_init)
9097 goto no_alloc;
9102 /* allocate symbol in corresponding section */
9103 sec = ad->section;
9104 if (!sec) {
9105 if (has_init)
9106 sec = data_section;
9107 else if (tcc_state->nocommon)
9108 sec = bss_section;
9110 if (sec) {
9111 data_offset = sec->data_offset;
9112 data_offset = (data_offset + align - 1) & -align;
9113 addr = data_offset;
9114 /* very important to increment global pointer at this time
9115 because initializers themselves can create new initializers */
9116 data_offset += size;
9117 /* add padding if bound check */
9118 if (do_bounds_check)
9119 data_offset++;
9120 sec->data_offset = data_offset;
9121 /* allocate section space to put the data */
9122 if (sec->sh_type != SHT_NOBITS &&
9123 data_offset > sec->data_allocated)
9124 section_realloc(sec, data_offset);
9125 /* align section if needed */
9126 if (align > sec->sh_addralign)
9127 sec->sh_addralign = align;
9128 } else {
9129 addr = 0; /* avoid warning */
9132 if (v) {
9133 if (scope != VT_CONST || !sym) {
9134 sym = sym_push(v, type, r | VT_SYM, 0);
9136 /* update symbol definition */
9137 if (sec) {
9138 put_extern_sym(sym, sec, addr, size);
9139 } else {
9140 Elf32_Sym *esym;
9141 /* put a common area */
9142 put_extern_sym(sym, NULL, align, size);
9143 /* XXX: find a nicer way */
9144 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
9145 esym->st_shndx = SHN_COMMON;
9147 } else {
9148 CValue cval;
9150 /* push global reference */
9151 sym = get_sym_ref(type, sec, addr, size);
9152 cval.ul = 0;
9153 vsetc(type, VT_CONST | VT_SYM, &cval);
9154 vtop->sym = sym;
9157 /* handles bounds now because the symbol must be defined
9158 before for the relocation */
9159 if (do_bounds_check) {
9160 unsigned long *bounds_ptr;
9162 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9163 /* then add global bound info */
9164 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9165 bounds_ptr[0] = 0; /* relocated */
9166 bounds_ptr[1] = size;
9169 if (has_init) {
9170 decl_initializer(type, sec, addr, 1, 0);
9171 /* restore parse state if needed */
9172 if (init_str.str) {
9173 tok_str_free(init_str.str);
9174 restore_parse_state(&saved_parse_state);
9177 no_alloc: ;
9180 void put_func_debug(Sym *sym)
9182 char buf[512];
9184 /* stabs info */
9185 /* XXX: we put here a dummy type */
9186 snprintf(buf, sizeof(buf), "%s:%c1",
9187 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9188 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9189 cur_text_section, sym->c);
9190 /* //gr gdb wants a line at the function */
9191 put_stabn(N_SLINE, 0, file->line_num, 0);
9192 last_ind = 0;
9193 last_line_num = 0;
9196 /* parse an old style function declaration list */
9197 /* XXX: check multiple parameter */
9198 static void func_decl_list(Sym *func_sym)
9200 AttributeDef ad;
9201 int v;
9202 Sym *s;
9203 CType btype, type;
9205 /* parse each declaration */
9206 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9207 if (!parse_btype(&btype, &ad))
9208 expect("declaration list");
9209 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9210 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9211 tok == ';') {
9212 /* we accept no variable after */
9213 } else {
9214 for(;;) {
9215 type = btype;
9216 type_decl(&type, &ad, &v, TYPE_DIRECT);
9217 /* find parameter in function parameter list */
9218 s = func_sym->next;
9219 while (s != NULL) {
9220 if ((s->v & ~SYM_FIELD) == v)
9221 goto found;
9222 s = s->next;
9224 error("declaration for parameter '%s' but no such parameter",
9225 get_tok_str(v, NULL));
9226 found:
9227 /* check that no storage specifier except 'register' was given */
9228 if (type.t & VT_STORAGE)
9229 error("storage class specified for '%s'", get_tok_str(v, NULL));
9230 convert_parameter_type(&type);
9231 /* we can add the type (NOTE: it could be local to the function) */
9232 s->type = type;
9233 /* accept other parameters */
9234 if (tok == ',')
9235 next();
9236 else
9237 break;
9240 skip(';');
9244 /* parse a function defined by symbol 'sym' and generate its code in
9245 'cur_text_section' */
9246 static void gen_function(Sym *sym)
9248 int saved_nocode_wanted = nocode_wanted;
9249 nocode_wanted = 0;
9250 ind = cur_text_section->data_offset;
9251 /* NOTE: we patch the symbol size later */
9252 put_extern_sym(sym, cur_text_section, ind, 0);
9253 funcname = get_tok_str(sym->v, NULL);
9254 func_ind = ind;
9255 /* put debug symbol */
9256 if (do_debug)
9257 put_func_debug(sym);
9258 /* push a dummy symbol to enable local sym storage */
9259 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9260 gfunc_prolog(&sym->type);
9261 rsym = 0;
9262 block(NULL, NULL, NULL, NULL, 0, 0);
9263 gsym(rsym);
9264 gfunc_epilog();
9265 cur_text_section->data_offset = ind;
9266 label_pop(&global_label_stack, NULL);
9267 sym_pop(&local_stack, NULL); /* reset local stack */
9268 /* end of function */
9269 /* patch symbol size */
9270 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
9271 ind - func_ind;
9272 if (do_debug) {
9273 put_stabn(N_FUN, 0, 0, ind - func_ind);
9275 /* It's better to crash than to generate wrong code */
9276 cur_text_section = NULL;
9277 funcname = ""; /* for safety */
9278 func_vt.t = VT_VOID; /* for safety */
9279 ind = 0; /* for safety */
9280 nocode_wanted = saved_nocode_wanted;
9283 static void gen_inline_functions(void)
9285 Sym *sym;
9286 CType *type;
9287 int *str, inline_generated;
9289 /* iterate while inline function are referenced */
9290 for(;;) {
9291 inline_generated = 0;
9292 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9293 type = &sym->type;
9294 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9295 (type->t & (VT_STATIC | VT_INLINE)) ==
9296 (VT_STATIC | VT_INLINE) &&
9297 sym->c != 0) {
9298 /* the function was used: generate its code and
9299 convert it to a normal function */
9300 str = INLINE_DEF(sym->r);
9301 sym->r = VT_SYM | VT_CONST;
9302 sym->type.t &= ~VT_INLINE;
9304 macro_ptr = str;
9305 next();
9306 cur_text_section = text_section;
9307 gen_function(sym);
9308 macro_ptr = NULL; /* fail safe */
9310 tok_str_free(str);
9311 inline_generated = 1;
9314 if (!inline_generated)
9315 break;
9318 /* free all remaining inline function tokens */
9319 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9320 type = &sym->type;
9321 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9322 (type->t & (VT_STATIC | VT_INLINE)) ==
9323 (VT_STATIC | VT_INLINE)) {
9324 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9325 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9326 continue;
9327 str = INLINE_DEF(sym->r);
9328 tok_str_free(str);
9329 sym->r = 0; /* fail safe */
9334 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9335 static void decl(int l)
9337 int v, has_init, r;
9338 CType type, btype;
9339 Sym *sym;
9340 AttributeDef ad;
9342 while (1) {
9343 if (!parse_btype(&btype, &ad)) {
9344 /* skip redundant ';' */
9345 /* XXX: find more elegant solution */
9346 if (tok == ';') {
9347 next();
9348 continue;
9350 if (l == VT_CONST &&
9351 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9352 /* global asm block */
9353 asm_global_instr();
9354 continue;
9356 /* special test for old K&R protos without explicit int
9357 type. Only accepted when defining global data */
9358 if (l == VT_LOCAL || tok < TOK_DEFINE)
9359 break;
9360 btype.t = VT_INT;
9362 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9363 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9364 tok == ';') {
9365 /* we accept no variable after */
9366 next();
9367 continue;
9369 while (1) { /* iterate thru each declaration */
9370 type = btype;
9371 type_decl(&type, &ad, &v, TYPE_DIRECT);
9372 #if 0
9374 char buf[500];
9375 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9376 printf("type = '%s'\n", buf);
9378 #endif
9379 if ((type.t & VT_BTYPE) == VT_FUNC) {
9380 /* if old style function prototype, we accept a
9381 declaration list */
9382 sym = type.ref;
9383 if (sym->c == FUNC_OLD)
9384 func_decl_list(sym);
9387 if (tok == '{') {
9388 if (l == VT_LOCAL)
9389 error("cannot use local functions");
9390 if ((type.t & VT_BTYPE) != VT_FUNC)
9391 expect("function definition");
9393 /* reject abstract declarators in function definition */
9394 sym = type.ref;
9395 while ((sym = sym->next) != NULL)
9396 if (!(sym->v & ~SYM_FIELD))
9397 expect("identifier");
9399 /* XXX: cannot do better now: convert extern line to static inline */
9400 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9401 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9403 sym = sym_find(v);
9404 if (sym) {
9405 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9406 goto func_error1;
9407 /* specific case: if not func_call defined, we put
9408 the one of the prototype */
9409 /* XXX: should have default value */
9410 r = sym->type.ref->r;
9411 if (FUNC_CALL(r) != FUNC_CDECL
9412 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9413 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9414 if (FUNC_EXPORT(r))
9415 FUNC_EXPORT(type.ref->r) = 1;
9417 if (!is_compatible_types(&sym->type, &type)) {
9418 func_error1:
9419 error("incompatible types for redefinition of '%s'",
9420 get_tok_str(v, NULL));
9422 /* if symbol is already defined, then put complete type */
9423 sym->type = type;
9424 } else {
9425 /* put function symbol */
9426 sym = global_identifier_push(v, type.t, 0);
9427 sym->type.ref = type.ref;
9430 /* static inline functions are just recorded as a kind
9431 of macro. Their code will be emitted at the end of
9432 the compilation unit only if they are used */
9433 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9434 (VT_INLINE | VT_STATIC)) {
9435 TokenString func_str;
9436 int block_level;
9438 tok_str_new(&func_str);
9440 block_level = 0;
9441 for(;;) {
9442 int t;
9443 if (tok == TOK_EOF)
9444 error("unexpected end of file");
9445 tok_str_add_tok(&func_str);
9446 t = tok;
9447 next();
9448 if (t == '{') {
9449 block_level++;
9450 } else if (t == '}') {
9451 block_level--;
9452 if (block_level == 0)
9453 break;
9456 tok_str_add(&func_str, -1);
9457 tok_str_add(&func_str, 0);
9458 INLINE_DEF(sym->r) = func_str.str;
9459 } else {
9460 /* compute text section */
9461 cur_text_section = ad.section;
9462 if (!cur_text_section)
9463 cur_text_section = text_section;
9464 sym->r = VT_SYM | VT_CONST;
9465 gen_function(sym);
9467 break;
9468 } else {
9469 if (btype.t & VT_TYPEDEF) {
9470 /* save typedefed type */
9471 /* XXX: test storage specifiers ? */
9472 sym = sym_push(v, &type, 0, 0);
9473 sym->type.t |= VT_TYPEDEF;
9474 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9475 /* external function definition */
9476 /* specific case for func_call attribute */
9477 if (ad.func_attr)
9478 type.ref->r = ad.func_attr;
9479 external_sym(v, &type, 0);
9480 } else {
9481 /* not lvalue if array */
9482 r = 0;
9483 if (!(type.t & VT_ARRAY))
9484 r |= lvalue_type(type.t);
9485 has_init = (tok == '=');
9486 if ((btype.t & VT_EXTERN) ||
9487 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9488 !has_init && l == VT_CONST && type.ref->c < 0)) {
9489 /* external variable */
9490 /* NOTE: as GCC, uninitialized global static
9491 arrays of null size are considered as
9492 extern */
9493 external_sym(v, &type, r);
9494 } else {
9495 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9496 if (type.t & VT_STATIC)
9497 r |= VT_CONST;
9498 else
9499 r |= l;
9500 if (has_init)
9501 next();
9502 decl_initializer_alloc(&type, &ad, r,
9503 has_init, v, l);
9506 if (tok != ',') {
9507 skip(';');
9508 break;
9510 next();
9516 /* better than nothing, but needs extension to handle '-E' option
9517 correctly too */
9518 static void preprocess_init(TCCState *s1)
9520 s1->include_stack_ptr = s1->include_stack;
9521 /* XXX: move that before to avoid having to initialize
9522 file->ifdef_stack_ptr ? */
9523 s1->ifdef_stack_ptr = s1->ifdef_stack;
9524 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9526 /* XXX: not ANSI compliant: bound checking says error */
9527 vtop = vstack - 1;
9528 s1->pack_stack[0] = 0;
9529 s1->pack_stack_ptr = s1->pack_stack;
9532 /* compile the C file opened in 'file'. Return non zero if errors. */
9533 static int tcc_compile(TCCState *s1)
9535 Sym *define_start;
9536 char buf[512];
9537 volatile int section_sym;
9539 #ifdef INC_DEBUG
9540 printf("%s: **** new file\n", file->filename);
9541 #endif
9542 preprocess_init(s1);
9544 cur_text_section = NULL;
9545 funcname = "";
9546 anon_sym = SYM_FIRST_ANOM;
9548 /* file info: full path + filename */
9549 section_sym = 0; /* avoid warning */
9550 if (do_debug) {
9551 section_sym = put_elf_sym(symtab_section, 0, 0,
9552 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
9553 text_section->sh_num, NULL);
9554 getcwd(buf, sizeof(buf));
9555 #ifdef _WIN32
9556 normalize_slashes(buf);
9557 #endif
9558 pstrcat(buf, sizeof(buf), "/");
9559 put_stabs_r(buf, N_SO, 0, 0,
9560 text_section->data_offset, text_section, section_sym);
9561 put_stabs_r(file->filename, N_SO, 0, 0,
9562 text_section->data_offset, text_section, section_sym);
9564 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9565 symbols can be safely used */
9566 put_elf_sym(symtab_section, 0, 0,
9567 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
9568 SHN_ABS, file->filename);
9570 /* define some often used types */
9571 int_type.t = VT_INT;
9573 char_pointer_type.t = VT_BYTE;
9574 mk_pointer(&char_pointer_type);
9576 func_old_type.t = VT_FUNC;
9577 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9579 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9580 float_type.t = VT_FLOAT;
9581 double_type.t = VT_DOUBLE;
9583 func_float_type.t = VT_FUNC;
9584 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9585 func_double_type.t = VT_FUNC;
9586 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9587 #endif
9589 #if 0
9590 /* define 'void *alloca(unsigned int)' builtin function */
9592 Sym *s1;
9594 p = anon_sym++;
9595 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9596 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9597 s1->next = NULL;
9598 sym->next = s1;
9599 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9601 #endif
9603 define_start = define_stack;
9604 nocode_wanted = 1;
9606 if (setjmp(s1->error_jmp_buf) == 0) {
9607 s1->nb_errors = 0;
9608 s1->error_set_jmp_enabled = 1;
9610 ch = file->buf_ptr[0];
9611 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9612 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9613 next();
9614 decl(VT_CONST);
9615 if (tok != TOK_EOF)
9616 expect("declaration");
9618 /* end of translation unit info */
9619 if (do_debug) {
9620 put_stabs_r(NULL, N_SO, 0, 0,
9621 text_section->data_offset, text_section, section_sym);
9624 s1->error_set_jmp_enabled = 0;
9626 /* reset define stack, but leave -Dsymbols (may be incorrect if
9627 they are undefined) */
9628 free_defines(define_start);
9630 gen_inline_functions();
9632 sym_pop(&global_stack, NULL);
9633 sym_pop(&local_stack, NULL);
9635 return s1->nb_errors != 0 ? -1 : 0;
9638 /* Preprocess the current file */
9639 /* XXX: add line and file infos, add options to preserve spaces */
9640 static int tcc_preprocess(TCCState *s1)
9642 Sym *define_start;
9643 BufferedFile *file_ref;
9644 int token_seen, line_ref;
9646 preprocess_init(s1);
9647 define_start = define_stack;
9648 ch = file->buf_ptr[0];
9650 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9651 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9652 PARSE_FLAG_LINEFEED;
9654 token_seen = 0;
9655 line_ref = 0;
9656 file_ref = NULL;
9658 for (;;) {
9659 next();
9660 if (tok == TOK_EOF) {
9661 break;
9662 } else if (tok == TOK_LINEFEED) {
9663 if (!token_seen)
9664 continue;
9665 ++line_ref;
9666 token_seen = 0;
9667 } else if (token_seen) {
9668 fputc(' ', s1->outfile);
9669 } else {
9670 int d = file->line_num - line_ref;
9671 if (file != file_ref || d < 0 || d >= 8)
9672 fprintf(s1->outfile, "# %d \"%s\"\n", file->line_num, file->filename);
9673 else
9674 while (d)
9675 fputs("\n", s1->outfile), --d;
9676 line_ref = (file_ref = file)->line_num;
9677 token_seen = 1;
9679 fputs(get_tok_str(tok, &tokc), s1->outfile);
9681 free_defines(define_start);
9682 return 0;
9685 #ifdef LIBTCC
9686 int tcc_compile_string(TCCState *s, const char *str)
9688 BufferedFile bf1, *bf = &bf1;
9689 int ret, len;
9690 char *buf;
9692 /* init file structure */
9693 bf->fd = -1;
9694 /* XXX: avoid copying */
9695 len = strlen(str);
9696 buf = tcc_malloc(len + 1);
9697 if (!buf)
9698 return -1;
9699 memcpy(buf, str, len);
9700 buf[len] = CH_EOB;
9701 bf->buf_ptr = buf;
9702 bf->buf_end = buf + len;
9703 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9704 bf->line_num = 1;
9705 file = bf;
9706 ret = tcc_compile(s);
9707 file = NULL;
9708 tcc_free(buf);
9710 /* currently, no need to close */
9711 return ret;
9713 #endif
9715 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9716 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9718 BufferedFile bf1, *bf = &bf1;
9720 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9721 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9722 /* default value */
9723 if (!value)
9724 value = "1";
9725 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9727 /* init file structure */
9728 bf->fd = -1;
9729 bf->buf_ptr = bf->buffer;
9730 bf->buf_end = bf->buffer + strlen(bf->buffer);
9731 *bf->buf_end = CH_EOB;
9732 bf->filename[0] = '\0';
9733 bf->line_num = 1;
9734 file = bf;
9736 s1->include_stack_ptr = s1->include_stack;
9738 /* parse with define parser */
9739 ch = file->buf_ptr[0];
9740 next_nomacro();
9741 parse_define();
9742 file = NULL;
9745 /* undefine a preprocessor symbol */
9746 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9748 TokenSym *ts;
9749 Sym *s;
9750 ts = tok_alloc(sym, strlen(sym));
9751 s = define_find(ts->tok);
9752 /* undefine symbol by putting an invalid name */
9753 if (s)
9754 define_undef(s);
9757 #ifdef CONFIG_TCC_ASM
9759 #ifdef TCC_TARGET_I386
9760 #include "i386-asm.c"
9761 #endif
9762 #include "tccasm.c"
9764 #else
9765 static void asm_instr(void)
9767 error("inline asm() not supported");
9769 static void asm_global_instr(void)
9771 error("inline asm() not supported");
9773 #endif
9775 #include "tccelf.c"
9777 #ifdef TCC_TARGET_COFF
9778 #include "tcccoff.c"
9779 #endif
9781 #ifdef TCC_TARGET_PE
9782 #include "tccpe.c"
9783 #endif
9785 /* print the position in the source file of PC value 'pc' by reading
9786 the stabs debug information */
9787 static void rt_printline(unsigned long wanted_pc)
9789 Stab_Sym *sym, *sym_end;
9790 char func_name[128], last_func_name[128];
9791 unsigned long func_addr, last_pc, pc;
9792 const char *incl_files[INCLUDE_STACK_SIZE];
9793 int incl_index, len, last_line_num, i;
9794 const char *str, *p;
9796 fprintf(stderr, "0x%08lx:", wanted_pc);
9798 func_name[0] = '\0';
9799 func_addr = 0;
9800 incl_index = 0;
9801 last_func_name[0] = '\0';
9802 last_pc = 0xffffffff;
9803 last_line_num = 1;
9804 sym = (Stab_Sym *)stab_section->data + 1;
9805 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9806 while (sym < sym_end) {
9807 switch(sym->n_type) {
9808 /* function start or end */
9809 case N_FUN:
9810 if (sym->n_strx == 0) {
9811 /* we test if between last line and end of function */
9812 pc = sym->n_value + func_addr;
9813 if (wanted_pc >= last_pc && wanted_pc < pc)
9814 goto found;
9815 func_name[0] = '\0';
9816 func_addr = 0;
9817 } else {
9818 str = stabstr_section->data + sym->n_strx;
9819 p = strchr(str, ':');
9820 if (!p) {
9821 pstrcpy(func_name, sizeof(func_name), str);
9822 } else {
9823 len = p - str;
9824 if (len > sizeof(func_name) - 1)
9825 len = sizeof(func_name) - 1;
9826 memcpy(func_name, str, len);
9827 func_name[len] = '\0';
9829 func_addr = sym->n_value;
9831 break;
9832 /* line number info */
9833 case N_SLINE:
9834 pc = sym->n_value + func_addr;
9835 if (wanted_pc >= last_pc && wanted_pc < pc)
9836 goto found;
9837 last_pc = pc;
9838 last_line_num = sym->n_desc;
9839 /* XXX: slow! */
9840 strcpy(last_func_name, func_name);
9841 break;
9842 /* include files */
9843 case N_BINCL:
9844 str = stabstr_section->data + sym->n_strx;
9845 add_incl:
9846 if (incl_index < INCLUDE_STACK_SIZE) {
9847 incl_files[incl_index++] = str;
9849 break;
9850 case N_EINCL:
9851 if (incl_index > 1)
9852 incl_index--;
9853 break;
9854 case N_SO:
9855 if (sym->n_strx == 0) {
9856 incl_index = 0; /* end of translation unit */
9857 } else {
9858 str = stabstr_section->data + sym->n_strx;
9859 /* do not add path */
9860 len = strlen(str);
9861 if (len > 0 && str[len - 1] != '/')
9862 goto add_incl;
9864 break;
9866 sym++;
9869 /* second pass: we try symtab symbols (no line number info) */
9870 incl_index = 0;
9872 Elf32_Sym *sym, *sym_end;
9873 int type;
9875 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
9876 for(sym = (Elf32_Sym *)symtab_section->data + 1;
9877 sym < sym_end;
9878 sym++) {
9879 type = ELF32_ST_TYPE(sym->st_info);
9880 if (type == STT_FUNC) {
9881 if (wanted_pc >= sym->st_value &&
9882 wanted_pc < sym->st_value + sym->st_size) {
9883 pstrcpy(last_func_name, sizeof(last_func_name),
9884 strtab_section->data + sym->st_name);
9885 goto found;
9890 /* did not find any info: */
9891 fprintf(stderr, " ???\n");
9892 return;
9893 found:
9894 if (last_func_name[0] != '\0') {
9895 fprintf(stderr, " %s()", last_func_name);
9897 if (incl_index > 0) {
9898 fprintf(stderr, " (%s:%d",
9899 incl_files[incl_index - 1], last_line_num);
9900 for(i = incl_index - 2; i >= 0; i--)
9901 fprintf(stderr, ", included from %s", incl_files[i]);
9902 fprintf(stderr, ")");
9904 fprintf(stderr, "\n");
9907 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
9909 #ifdef __i386__
9911 /* fix for glibc 2.1 */
9912 #ifndef REG_EIP
9913 #define REG_EIP EIP
9914 #define REG_EBP EBP
9915 #endif
9917 /* return the PC at frame level 'level'. Return non zero if not found */
9918 static int rt_get_caller_pc(unsigned long *paddr,
9919 ucontext_t *uc, int level)
9921 unsigned long fp;
9922 int i;
9924 if (level == 0) {
9925 #if defined(__FreeBSD__)
9926 *paddr = uc->uc_mcontext.mc_eip;
9927 #elif defined(__dietlibc__)
9928 *paddr = uc->uc_mcontext.eip;
9929 #else
9930 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9931 #endif
9932 return 0;
9933 } else {
9934 #if defined(__FreeBSD__)
9935 fp = uc->uc_mcontext.mc_ebp;
9936 #elif defined(__dietlibc__)
9937 fp = uc->uc_mcontext.ebp;
9938 #else
9939 fp = uc->uc_mcontext.gregs[REG_EBP];
9940 #endif
9941 for(i=1;i<level;i++) {
9942 /* XXX: check address validity with program info */
9943 if (fp <= 0x1000 || fp >= 0xc0000000)
9944 return -1;
9945 fp = ((unsigned long *)fp)[0];
9947 *paddr = ((unsigned long *)fp)[1];
9948 return 0;
9951 #else
9953 #warning add arch specific rt_get_caller_pc()
9955 static int rt_get_caller_pc(unsigned long *paddr,
9956 ucontext_t *uc, int level)
9958 return -1;
9960 #endif
9962 /* emit a run time error at position 'pc' */
9963 void rt_error(ucontext_t *uc, const char *fmt, ...)
9965 va_list ap;
9966 unsigned long pc;
9967 int i;
9969 va_start(ap, fmt);
9970 fprintf(stderr, "Runtime error: ");
9971 vfprintf(stderr, fmt, ap);
9972 fprintf(stderr, "\n");
9973 for(i=0;i<num_callers;i++) {
9974 if (rt_get_caller_pc(&pc, uc, i) < 0)
9975 break;
9976 if (i == 0)
9977 fprintf(stderr, "at ");
9978 else
9979 fprintf(stderr, "by ");
9980 rt_printline(pc);
9982 exit(255);
9983 va_end(ap);
9986 /* signal handler for fatal errors */
9987 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9989 ucontext_t *uc = puc;
9991 switch(signum) {
9992 case SIGFPE:
9993 switch(siginf->si_code) {
9994 case FPE_INTDIV:
9995 case FPE_FLTDIV:
9996 rt_error(uc, "division by zero");
9997 break;
9998 default:
9999 rt_error(uc, "floating point exception");
10000 break;
10002 break;
10003 case SIGBUS:
10004 case SIGSEGV:
10005 if (rt_bound_error_msg && *rt_bound_error_msg)
10006 rt_error(uc, *rt_bound_error_msg);
10007 else
10008 rt_error(uc, "dereferencing invalid pointer");
10009 break;
10010 case SIGILL:
10011 rt_error(uc, "illegal instruction");
10012 break;
10013 case SIGABRT:
10014 rt_error(uc, "abort() called");
10015 break;
10016 default:
10017 rt_error(uc, "caught signal %d", signum);
10018 break;
10020 exit(255);
10022 #endif
10024 /* do all relocations (needed before using tcc_get_symbol()) */
10025 int tcc_relocate(TCCState *s1)
10027 Section *s;
10028 int i;
10030 s1->nb_errors = 0;
10032 #ifdef TCC_TARGET_PE
10033 pe_add_runtime(s1);
10034 #else
10035 tcc_add_runtime(s1);
10036 #endif
10038 relocate_common_syms();
10040 tcc_add_linker_symbols(s1);
10041 #ifndef TCC_TARGET_PE
10042 build_got_entries(s1);
10043 #endif
10044 /* compute relocation address : section are relocated in place. We
10045 also alloc the bss space */
10046 for(i = 1; i < s1->nb_sections; i++) {
10047 s = s1->sections[i];
10048 if (s->sh_flags & SHF_ALLOC) {
10049 if (s->sh_type == SHT_NOBITS)
10050 s->data = tcc_mallocz(s->data_offset);
10051 s->sh_addr = (unsigned long)s->data;
10055 relocate_syms(s1, 1);
10057 if (s1->nb_errors != 0)
10058 return -1;
10060 /* relocate each section */
10061 for(i = 1; i < s1->nb_sections; i++) {
10062 s = s1->sections[i];
10063 if (s->reloc)
10064 relocate_section(s1, s);
10067 /* mark executable sections as executable in memory */
10068 for(i = 1; i < s1->nb_sections; i++) {
10069 s = s1->sections[i];
10070 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10071 (SHF_ALLOC | SHF_EXECINSTR))
10072 set_pages_executable(s->data, s->data_offset);
10074 return 0;
10077 /* launch the compiled program with the given arguments */
10078 int tcc_run(TCCState *s1, int argc, char **argv)
10080 int (*prog_main)(int, char **);
10082 if (tcc_relocate(s1) < 0)
10083 return -1;
10085 prog_main = tcc_get_symbol_err(s1, "main");
10087 if (do_debug) {
10088 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10089 error("debug mode currently not available for Windows");
10090 #else
10091 struct sigaction sigact;
10092 /* install TCC signal handlers to print debug info on fatal
10093 runtime errors */
10094 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10095 sigact.sa_sigaction = sig_error;
10096 sigemptyset(&sigact.sa_mask);
10097 sigaction(SIGFPE, &sigact, NULL);
10098 sigaction(SIGILL, &sigact, NULL);
10099 sigaction(SIGSEGV, &sigact, NULL);
10100 sigaction(SIGBUS, &sigact, NULL);
10101 sigaction(SIGABRT, &sigact, NULL);
10102 #endif
10105 #ifdef CONFIG_TCC_BCHECK
10106 if (do_bounds_check) {
10107 void (*bound_init)(void);
10109 /* set error function */
10110 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10111 "__bound_error_msg");
10113 /* XXX: use .init section so that it also work in binary ? */
10114 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10115 bound_init();
10117 #endif
10118 return (*prog_main)(argc, argv);
10121 void tcc_memstats(void)
10123 #ifdef MEM_DEBUG
10124 printf("memory in use: %d\n", mem_cur_size);
10125 #endif
10128 static void tcc_cleanup(void)
10130 int i, n;
10132 if (NULL == tcc_state)
10133 return;
10134 tcc_state = NULL;
10136 /* free -D defines */
10137 free_defines(NULL);
10139 /* free tokens */
10140 n = tok_ident - TOK_IDENT;
10141 for(i = 0; i < n; i++)
10142 tcc_free(table_ident[i]);
10143 tcc_free(table_ident);
10145 /* free sym_pools */
10146 dynarray_reset(&sym_pools, &nb_sym_pools);
10147 /* string buffer */
10148 cstr_free(&tokcstr);
10149 /* reset symbol stack */
10150 sym_free_first = NULL;
10151 /* cleanup from error/setjmp */
10152 macro_ptr = NULL;
10155 TCCState *tcc_new(void)
10157 const char *p, *r;
10158 TCCState *s;
10159 TokenSym *ts;
10160 int i, c;
10162 tcc_cleanup();
10164 s = tcc_mallocz(sizeof(TCCState));
10165 if (!s)
10166 return NULL;
10167 tcc_state = s;
10168 s->output_type = TCC_OUTPUT_MEMORY;
10170 /* init isid table */
10171 for(i=CH_EOF;i<256;i++)
10172 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
10174 /* add all tokens */
10175 table_ident = NULL;
10176 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10178 tok_ident = TOK_IDENT;
10179 p = tcc_keywords;
10180 while (*p) {
10181 r = p;
10182 for(;;) {
10183 c = *r++;
10184 if (c == '\0')
10185 break;
10187 ts = tok_alloc(p, r - p - 1);
10188 p = r;
10191 /* we add dummy defines for some special macros to speed up tests
10192 and to have working defined() */
10193 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10194 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10195 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10196 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10198 /* standard defines */
10199 tcc_define_symbol(s, "__STDC__", NULL);
10200 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10201 #if defined(TCC_TARGET_I386)
10202 tcc_define_symbol(s, "__i386__", NULL);
10203 #endif
10204 #if defined(TCC_TARGET_ARM)
10205 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10206 tcc_define_symbol(s, "__arm_elf__", NULL);
10207 tcc_define_symbol(s, "__arm_elf", NULL);
10208 tcc_define_symbol(s, "arm_elf", NULL);
10209 tcc_define_symbol(s, "__arm__", NULL);
10210 tcc_define_symbol(s, "__arm", NULL);
10211 tcc_define_symbol(s, "arm", NULL);
10212 tcc_define_symbol(s, "__APCS_32__", NULL);
10213 #endif
10214 #ifdef TCC_TARGET_PE
10215 tcc_define_symbol(s, "_WIN32", NULL);
10216 #else
10217 tcc_define_symbol(s, "__unix__", NULL);
10218 tcc_define_symbol(s, "__unix", NULL);
10219 #if defined(__linux)
10220 tcc_define_symbol(s, "__linux__", NULL);
10221 tcc_define_symbol(s, "__linux", NULL);
10222 #endif
10223 #endif
10224 /* tiny C specific defines */
10225 tcc_define_symbol(s, "__TINYC__", NULL);
10227 /* tiny C & gcc defines */
10228 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10229 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10230 #ifdef TCC_TARGET_PE
10231 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10232 #else
10233 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10234 #endif
10236 #ifndef TCC_TARGET_PE
10237 /* default library paths */
10238 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
10239 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
10240 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
10241 #endif
10243 /* no section zero */
10244 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10246 /* create standard sections */
10247 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10248 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10249 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10251 /* symbols are always generated for linking stage */
10252 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10253 ".strtab",
10254 ".hashtab", SHF_PRIVATE);
10255 strtab_section = symtab_section->link;
10257 /* private symbol table for dynamic symbols */
10258 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10259 ".dynstrtab",
10260 ".dynhashtab", SHF_PRIVATE);
10261 s->alacarte_link = 1;
10263 #ifdef CHAR_IS_UNSIGNED
10264 s->char_is_unsigned = 1;
10265 #endif
10266 #if defined(TCC_TARGET_PE) && 0
10267 /* XXX: currently the PE linker is not ready to support that */
10268 s->leading_underscore = 1;
10269 #endif
10270 return s;
10273 void tcc_delete(TCCState *s1)
10275 int i;
10277 tcc_cleanup();
10279 /* free all sections */
10280 free_section(s1->dynsymtab_section);
10282 for(i = 1; i < s1->nb_sections; i++)
10283 free_section(s1->sections[i]);
10284 tcc_free(s1->sections);
10286 /* free any loaded DLLs */
10287 for ( i = 0; i < s1->nb_loaded_dlls; i++)
10289 DLLReference *ref = s1->loaded_dlls[i];
10290 if ( ref->handle )
10291 dlclose(ref->handle);
10294 /* free loaded dlls array */
10295 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10297 /* free library paths */
10298 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10300 /* free include paths */
10301 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10302 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10303 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10305 tcc_free(s1);
10308 int tcc_add_include_path(TCCState *s1, const char *pathname)
10310 char *pathname1;
10312 pathname1 = tcc_strdup(pathname);
10313 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10314 return 0;
10317 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10319 char *pathname1;
10321 pathname1 = tcc_strdup(pathname);
10322 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10323 return 0;
10326 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10328 const char *ext;
10329 Elf32_Ehdr ehdr;
10330 int fd, ret;
10331 BufferedFile *saved_file;
10333 /* find source file type with extension */
10334 ext = tcc_fileextension(filename);
10335 if (ext[0])
10336 ext++;
10338 /* open the file */
10339 saved_file = file;
10340 file = tcc_open(s1, filename);
10341 if (!file) {
10342 if (flags & AFF_PRINT_ERROR) {
10343 error_noabort("file '%s' not found", filename);
10345 ret = -1;
10346 goto fail1;
10349 if (flags & AFF_PREPROCESS) {
10350 ret = tcc_preprocess(s1);
10351 } else if (!ext[0] || !strcmp(ext, "c")) {
10352 /* C file assumed */
10353 ret = tcc_compile(s1);
10354 } else
10355 #ifdef CONFIG_TCC_ASM
10356 if (!strcmp(ext, "S")) {
10357 /* preprocessed assembler */
10358 ret = tcc_assemble(s1, 1);
10359 } else if (!strcmp(ext, "s")) {
10360 /* non preprocessed assembler */
10361 ret = tcc_assemble(s1, 0);
10362 } else
10363 #endif
10364 #ifdef TCC_TARGET_PE
10365 if (!strcmp(ext, "def")) {
10366 ret = pe_load_def_file(s1, file->fd);
10367 } else
10368 #endif
10370 fd = file->fd;
10371 /* assume executable format: auto guess file type */
10372 ret = read(fd, &ehdr, sizeof(ehdr));
10373 lseek(fd, 0, SEEK_SET);
10374 if (ret <= 0) {
10375 error_noabort("could not read header");
10376 goto fail;
10377 } else if (ret != sizeof(ehdr)) {
10378 goto try_load_script;
10381 if (ehdr.e_ident[0] == ELFMAG0 &&
10382 ehdr.e_ident[1] == ELFMAG1 &&
10383 ehdr.e_ident[2] == ELFMAG2 &&
10384 ehdr.e_ident[3] == ELFMAG3) {
10385 file->line_num = 0; /* do not display line number if error */
10386 if (ehdr.e_type == ET_REL) {
10387 ret = tcc_load_object_file(s1, fd, 0);
10388 } else if (ehdr.e_type == ET_DYN) {
10389 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10390 #ifdef TCC_TARGET_PE
10391 ret = -1;
10392 #else
10393 void *h;
10394 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10395 if (h)
10396 ret = 0;
10397 else
10398 ret = -1;
10399 #endif
10400 } else {
10401 ret = tcc_load_dll(s1, fd, filename,
10402 (flags & AFF_REFERENCED_DLL) != 0);
10404 } else {
10405 error_noabort("unrecognized ELF file");
10406 goto fail;
10408 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10409 file->line_num = 0; /* do not display line number if error */
10410 ret = tcc_load_archive(s1, fd);
10411 } else
10412 #ifdef TCC_TARGET_COFF
10413 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10414 ret = tcc_load_coff(s1, fd);
10415 } else
10416 #endif
10417 #ifdef TCC_TARGET_PE
10418 if (pe_test_res_file(&ehdr, ret)) {
10419 ret = pe_load_res_file(s1, fd);
10420 } else
10421 #endif
10423 /* as GNU ld, consider it is an ld script if not recognized */
10424 try_load_script:
10425 ret = tcc_load_ldscript(s1);
10426 if (ret < 0) {
10427 error_noabort("unrecognized file type");
10428 goto fail;
10432 the_end:
10433 tcc_close(file);
10434 fail1:
10435 file = saved_file;
10436 return ret;
10437 fail:
10438 ret = -1;
10439 goto the_end;
10442 int tcc_add_file(TCCState *s, const char *filename)
10444 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10447 int tcc_add_library_path(TCCState *s, const char *pathname)
10449 char *pathname1;
10451 pathname1 = tcc_strdup(pathname);
10452 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10453 return 0;
10456 /* find and load a dll. Return non zero if not found */
10457 /* XXX: add '-rpath' option support ? */
10458 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10460 char buf[1024];
10461 int i;
10463 for(i = 0; i < s->nb_library_paths; i++) {
10464 snprintf(buf, sizeof(buf), "%s/%s",
10465 s->library_paths[i], filename);
10466 if (tcc_add_file_internal(s, buf, flags) == 0)
10467 return 0;
10469 return -1;
10472 /* the library name is the same as the argument of the '-l' option */
10473 int tcc_add_library(TCCState *s, const char *libraryname)
10475 char buf[1024];
10476 int i;
10478 /* first we look for the dynamic library if not static linking */
10479 if (!s->static_link) {
10480 #ifdef TCC_TARGET_PE
10481 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10482 #else
10483 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10484 #endif
10485 if (tcc_add_dll(s, buf, 0) == 0)
10486 return 0;
10489 /* then we look for the static library */
10490 for(i = 0; i < s->nb_library_paths; i++) {
10491 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10492 s->library_paths[i], libraryname);
10493 if (tcc_add_file_internal(s, buf, 0) == 0)
10494 return 0;
10496 return -1;
10499 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10501 add_elf_sym(symtab_section, val, 0,
10502 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10503 SHN_ABS, name);
10504 return 0;
10507 int tcc_set_output_type(TCCState *s, int output_type)
10509 char buf[1024];
10511 s->output_type = output_type;
10513 if (!s->nostdinc) {
10514 /* default include paths */
10515 /* XXX: reverse order needed if -isystem support */
10516 #ifndef TCC_TARGET_PE
10517 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
10518 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
10519 #endif
10520 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10521 tcc_add_sysinclude_path(s, buf);
10522 #ifdef TCC_TARGET_PE
10523 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10524 tcc_add_sysinclude_path(s, buf);
10525 #endif
10528 /* if bound checking, then add corresponding sections */
10529 #ifdef CONFIG_TCC_BCHECK
10530 if (do_bounds_check) {
10531 /* define symbol */
10532 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10533 /* create bounds sections */
10534 bounds_section = new_section(s, ".bounds",
10535 SHT_PROGBITS, SHF_ALLOC);
10536 lbounds_section = new_section(s, ".lbounds",
10537 SHT_PROGBITS, SHF_ALLOC);
10539 #endif
10541 if (s->char_is_unsigned) {
10542 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10545 /* add debug sections */
10546 if (do_debug) {
10547 /* stab symbols */
10548 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10549 stab_section->sh_entsize = sizeof(Stab_Sym);
10550 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10551 put_elf_str(stabstr_section, "");
10552 stab_section->link = stabstr_section;
10553 /* put first entry */
10554 put_stabs("", 0, 0, 0, 0);
10557 /* add libc crt1/crti objects */
10558 #ifndef TCC_TARGET_PE
10559 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10560 !s->nostdlib) {
10561 if (output_type != TCC_OUTPUT_DLL)
10562 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10563 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10565 #endif
10567 #ifdef TCC_TARGET_PE
10568 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10569 tcc_add_library_path(s, buf);
10570 #endif
10572 return 0;
10575 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10576 #define FD_INVERT 0x0002 /* invert value before storing */
10578 typedef struct FlagDef {
10579 uint16_t offset;
10580 uint16_t flags;
10581 const char *name;
10582 } FlagDef;
10584 static const FlagDef warning_defs[] = {
10585 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10586 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10587 { offsetof(TCCState, warn_error), 0, "error" },
10588 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10589 "implicit-function-declaration" },
10592 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10593 const char *name, int value)
10595 int i;
10596 const FlagDef *p;
10597 const char *r;
10599 r = name;
10600 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10601 r += 3;
10602 value = !value;
10604 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10605 if (!strcmp(r, p->name))
10606 goto found;
10608 return -1;
10609 found:
10610 if (p->flags & FD_INVERT)
10611 value = !value;
10612 *(int *)((uint8_t *)s + p->offset) = value;
10613 return 0;
10617 /* set/reset a warning */
10618 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10620 int i;
10621 const FlagDef *p;
10623 if (!strcmp(warning_name, "all")) {
10624 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10625 if (p->flags & WD_ALL)
10626 *(int *)((uint8_t *)s + p->offset) = 1;
10628 return 0;
10629 } else {
10630 return set_flag(s, warning_defs, countof(warning_defs),
10631 warning_name, value);
10635 static const FlagDef flag_defs[] = {
10636 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10637 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10638 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10639 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10642 /* set/reset a flag */
10643 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10645 return set_flag(s, flag_defs, countof(flag_defs),
10646 flag_name, value);
10649 #if !defined(LIBTCC)
10651 static int64_t getclock_us(void)
10653 #ifdef _WIN32
10654 struct _timeb tb;
10655 _ftime(&tb);
10656 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10657 #else
10658 struct timeval tv;
10659 gettimeofday(&tv, NULL);
10660 return tv.tv_sec * 1000000LL + tv.tv_usec;
10661 #endif
10664 void help(void)
10666 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10667 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10668 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10669 " [-static] [infile1 infile2...] [-run infile args...]\n"
10670 "\n"
10671 "General options:\n"
10672 " -v display current version, increase verbosity\n"
10673 " -c compile only - generate an object file\n"
10674 " -o outfile set output filename\n"
10675 " -Bdir set tcc internal library path\n"
10676 " -bench output compilation statistics\n"
10677 " -run run compiled source\n"
10678 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10679 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10680 " -w disable all warnings\n"
10681 "Preprocessor options:\n"
10682 " -E preprocess only\n"
10683 " -Idir add include path 'dir'\n"
10684 " -Dsym[=val] define 'sym' with value 'val'\n"
10685 " -Usym undefine 'sym'\n"
10686 "Linker options:\n"
10687 " -Ldir add library path 'dir'\n"
10688 " -llib link with dynamic or static library 'lib'\n"
10689 " -shared generate a shared library\n"
10690 " -soname set name for shared library to be used at runtime\n"
10691 " -static static linking\n"
10692 " -rdynamic export all global symbols to dynamic linker\n"
10693 " -r generate (relocatable) object file\n"
10694 "Debugger options:\n"
10695 " -g generate runtime debug info\n"
10696 #ifdef CONFIG_TCC_BCHECK
10697 " -b compile with built-in memory and bounds checker (implies -g)\n"
10698 #endif
10699 " -bt N show N callers in stack traces\n"
10703 #define TCC_OPTION_HAS_ARG 0x0001
10704 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10706 typedef struct TCCOption {
10707 const char *name;
10708 uint16_t index;
10709 uint16_t flags;
10710 } TCCOption;
10712 enum {
10713 TCC_OPTION_HELP,
10714 TCC_OPTION_I,
10715 TCC_OPTION_D,
10716 TCC_OPTION_U,
10717 TCC_OPTION_L,
10718 TCC_OPTION_B,
10719 TCC_OPTION_l,
10720 TCC_OPTION_bench,
10721 TCC_OPTION_bt,
10722 TCC_OPTION_b,
10723 TCC_OPTION_g,
10724 TCC_OPTION_c,
10725 TCC_OPTION_static,
10726 TCC_OPTION_shared,
10727 TCC_OPTION_soname,
10728 TCC_OPTION_o,
10729 TCC_OPTION_r,
10730 TCC_OPTION_Wl,
10731 TCC_OPTION_W,
10732 TCC_OPTION_O,
10733 TCC_OPTION_m,
10734 TCC_OPTION_f,
10735 TCC_OPTION_nostdinc,
10736 TCC_OPTION_nostdlib,
10737 TCC_OPTION_print_search_dirs,
10738 TCC_OPTION_rdynamic,
10739 TCC_OPTION_run,
10740 TCC_OPTION_v,
10741 TCC_OPTION_w,
10742 TCC_OPTION_pipe,
10743 TCC_OPTION_E,
10746 static const TCCOption tcc_options[] = {
10747 { "h", TCC_OPTION_HELP, 0 },
10748 { "?", TCC_OPTION_HELP, 0 },
10749 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10750 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10751 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10752 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10753 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10754 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10755 { "bench", TCC_OPTION_bench, 0 },
10756 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10757 #ifdef CONFIG_TCC_BCHECK
10758 { "b", TCC_OPTION_b, 0 },
10759 #endif
10760 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10761 { "c", TCC_OPTION_c, 0 },
10762 { "static", TCC_OPTION_static, 0 },
10763 { "shared", TCC_OPTION_shared, 0 },
10764 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10765 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10766 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10767 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10768 { "r", TCC_OPTION_r, 0 },
10769 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10770 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10771 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10772 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10773 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10774 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10775 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10776 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10777 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10778 { "w", TCC_OPTION_w, 0 },
10779 { "pipe", TCC_OPTION_pipe, 0},
10780 { "E", TCC_OPTION_E, 0},
10781 { NULL },
10784 /* convert 'str' into an array of space separated strings */
10785 static int expand_args(char ***pargv, const char *str)
10787 const char *s1;
10788 char **argv, *arg;
10789 int argc, len;
10791 argc = 0;
10792 argv = NULL;
10793 for(;;) {
10794 while (is_space(*str))
10795 str++;
10796 if (*str == '\0')
10797 break;
10798 s1 = str;
10799 while (*str != '\0' && !is_space(*str))
10800 str++;
10801 len = str - s1;
10802 arg = tcc_malloc(len + 1);
10803 memcpy(arg, s1, len);
10804 arg[len] = '\0';
10805 dynarray_add((void ***)&argv, &argc, arg);
10807 *pargv = argv;
10808 return argc;
10811 static char **files;
10812 static int nb_files, nb_libraries;
10813 static int multiple_files;
10814 static int print_search_dirs;
10815 static int output_type;
10816 static int reloc_output;
10817 static const char *outfile;
10819 int parse_args(TCCState *s, int argc, char **argv)
10821 int optind;
10822 const TCCOption *popt;
10823 const char *optarg, *p1, *r1;
10824 char *r;
10826 optind = 0;
10827 while (optind < argc) {
10829 r = argv[optind++];
10830 if (r[0] != '-' || r[1] == '\0') {
10831 /* add a new file */
10832 dynarray_add((void ***)&files, &nb_files, r);
10833 if (!multiple_files) {
10834 optind--;
10835 /* argv[0] will be this file */
10836 break;
10838 } else {
10839 /* find option in table (match only the first chars */
10840 popt = tcc_options;
10841 for(;;) {
10842 p1 = popt->name;
10843 if (p1 == NULL)
10844 error("invalid option -- '%s'", r);
10845 r1 = r + 1;
10846 for(;;) {
10847 if (*p1 == '\0')
10848 goto option_found;
10849 if (*r1 != *p1)
10850 break;
10851 p1++;
10852 r1++;
10854 popt++;
10856 option_found:
10857 if (popt->flags & TCC_OPTION_HAS_ARG) {
10858 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
10859 optarg = r1;
10860 } else {
10861 if (optind >= argc)
10862 error("argument to '%s' is missing", r);
10863 optarg = argv[optind++];
10865 } else {
10866 if (*r1 != '\0')
10867 return 0;
10868 optarg = NULL;
10871 switch(popt->index) {
10872 case TCC_OPTION_HELP:
10873 return 0;
10875 case TCC_OPTION_I:
10876 if (tcc_add_include_path(s, optarg) < 0)
10877 error("too many include paths");
10878 break;
10879 case TCC_OPTION_D:
10881 char *sym, *value;
10882 sym = (char *)optarg;
10883 value = strchr(sym, '=');
10884 if (value) {
10885 *value = '\0';
10886 value++;
10888 tcc_define_symbol(s, sym, value);
10890 break;
10891 case TCC_OPTION_U:
10892 tcc_undefine_symbol(s, optarg);
10893 break;
10894 case TCC_OPTION_L:
10895 tcc_add_library_path(s, optarg);
10896 break;
10897 case TCC_OPTION_B:
10898 /* set tcc utilities path (mainly for tcc development) */
10899 tcc_lib_path = optarg;
10900 break;
10901 case TCC_OPTION_l:
10902 dynarray_add((void ***)&files, &nb_files, r);
10903 nb_libraries++;
10904 break;
10905 case TCC_OPTION_bench:
10906 do_bench = 1;
10907 break;
10908 case TCC_OPTION_bt:
10909 num_callers = atoi(optarg);
10910 break;
10911 #ifdef CONFIG_TCC_BCHECK
10912 case TCC_OPTION_b:
10913 do_bounds_check = 1;
10914 do_debug = 1;
10915 break;
10916 #endif
10917 case TCC_OPTION_g:
10918 do_debug = 1;
10919 break;
10920 case TCC_OPTION_c:
10921 multiple_files = 1;
10922 output_type = TCC_OUTPUT_OBJ;
10923 break;
10924 case TCC_OPTION_static:
10925 s->static_link = 1;
10926 break;
10927 case TCC_OPTION_shared:
10928 output_type = TCC_OUTPUT_DLL;
10929 break;
10930 case TCC_OPTION_soname:
10931 s->soname = optarg;
10932 break;
10933 case TCC_OPTION_o:
10934 multiple_files = 1;
10935 outfile = optarg;
10936 break;
10937 case TCC_OPTION_r:
10938 /* generate a .o merging several output files */
10939 reloc_output = 1;
10940 output_type = TCC_OUTPUT_OBJ;
10941 break;
10942 case TCC_OPTION_nostdinc:
10943 s->nostdinc = 1;
10944 break;
10945 case TCC_OPTION_nostdlib:
10946 s->nostdlib = 1;
10947 break;
10948 case TCC_OPTION_print_search_dirs:
10949 print_search_dirs = 1;
10950 break;
10951 case TCC_OPTION_run:
10953 int argc1;
10954 char **argv1;
10955 argc1 = expand_args(&argv1, optarg);
10956 if (argc1 > 0) {
10957 parse_args(s, argc1, argv1);
10959 multiple_files = 0;
10960 output_type = TCC_OUTPUT_MEMORY;
10962 break;
10963 case TCC_OPTION_v:
10964 do {
10965 if (0 == verbose++)
10966 printf("tcc version %s\n", TCC_VERSION);
10967 } while (*optarg++ == 'v');
10968 break;
10969 case TCC_OPTION_f:
10970 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
10971 goto unsupported_option;
10972 break;
10973 case TCC_OPTION_W:
10974 if (tcc_set_warning(s, optarg, 1) < 0 &&
10975 s->warn_unsupported)
10976 goto unsupported_option;
10977 break;
10978 case TCC_OPTION_w:
10979 s->warn_none = 1;
10980 break;
10981 case TCC_OPTION_rdynamic:
10982 s->rdynamic = 1;
10983 break;
10984 case TCC_OPTION_Wl:
10986 const char *p;
10987 if (strstart(optarg, "-Ttext,", &p)) {
10988 s->text_addr = strtoul(p, NULL, 16);
10989 s->has_text_addr = 1;
10990 } else if (strstart(optarg, "--oformat,", &p)) {
10991 if (strstart(p, "elf32-", NULL)) {
10992 s->output_format = TCC_OUTPUT_FORMAT_ELF;
10993 } else if (!strcmp(p, "binary")) {
10994 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
10995 } else
10996 #ifdef TCC_TARGET_COFF
10997 if (!strcmp(p, "coff")) {
10998 s->output_format = TCC_OUTPUT_FORMAT_COFF;
10999 } else
11000 #endif
11002 error("target %s not found", p);
11004 } else {
11005 error("unsupported linker option '%s'", optarg);
11008 break;
11009 case TCC_OPTION_E:
11010 output_type = TCC_OUTPUT_PREPROCESS;
11011 break;
11012 default:
11013 if (s->warn_unsupported) {
11014 unsupported_option:
11015 warning("unsupported option '%s'", r);
11017 break;
11021 return optind + 1;
11024 int main(int argc, char **argv)
11026 int i;
11027 TCCState *s;
11028 int nb_objfiles, ret, optind;
11029 char objfilename[1024];
11030 int64_t start_time = 0;
11032 #ifdef _WIN32
11033 tcc_lib_path = w32_tcc_lib_path();
11034 #endif
11036 s = tcc_new();
11037 output_type = TCC_OUTPUT_EXE;
11038 outfile = NULL;
11039 multiple_files = 1;
11040 files = NULL;
11041 nb_files = 0;
11042 nb_libraries = 0;
11043 reloc_output = 0;
11044 print_search_dirs = 0;
11045 ret = 0;
11047 optind = parse_args(s, argc - 1, argv + 1);
11048 if (print_search_dirs) {
11049 /* enough for Linux kernel */
11050 printf("install: %s/\n", tcc_lib_path);
11051 return 0;
11053 if (optind == 0 || nb_files == 0) {
11054 if (optind && verbose)
11055 return 0;
11056 help();
11057 return 1;
11060 nb_objfiles = nb_files - nb_libraries;
11062 /* if outfile provided without other options, we output an
11063 executable */
11064 if (outfile && output_type == TCC_OUTPUT_MEMORY)
11065 output_type = TCC_OUTPUT_EXE;
11067 /* check -c consistency : only single file handled. XXX: checks file type */
11068 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
11069 /* accepts only a single input file */
11070 if (nb_objfiles != 1)
11071 error("cannot specify multiple files with -c");
11072 if (nb_libraries != 0)
11073 error("cannot specify libraries with -c");
11077 if (output_type == TCC_OUTPUT_PREPROCESS) {
11078 if (!outfile) {
11079 s->outfile = stdout;
11080 } else {
11081 s->outfile = fopen(outfile, "w");
11082 if (!s->outfile)
11083 error("could not open '%s", outfile);
11085 } else if (output_type != TCC_OUTPUT_MEMORY) {
11086 if (!outfile) {
11087 /* compute default outfile name */
11088 char *ext;
11089 const char *name =
11090 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
11091 pstrcpy(objfilename, sizeof(objfilename), name);
11092 ext = tcc_fileextension(objfilename);
11093 #ifdef TCC_TARGET_PE
11094 if (output_type == TCC_OUTPUT_DLL)
11095 strcpy(ext, ".dll");
11096 else
11097 if (output_type == TCC_OUTPUT_EXE)
11098 strcpy(ext, ".exe");
11099 else
11100 #endif
11101 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
11102 strcpy(ext, ".o");
11103 else
11104 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11105 outfile = objfilename;
11109 if (do_bench) {
11110 start_time = getclock_us();
11113 tcc_set_output_type(s, output_type);
11115 /* compile or add each files or library */
11116 for(i = 0; i < nb_files && ret == 0; i++) {
11117 const char *filename;
11119 filename = files[i];
11120 if (output_type == TCC_OUTPUT_PREPROCESS) {
11121 if (tcc_add_file_internal(s, filename,
11122 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11123 ret = 1;
11124 } else if (filename[0] == '-' && filename[1]) {
11125 if (tcc_add_library(s, filename + 2) < 0)
11126 error("cannot find %s", filename);
11127 } else {
11128 if (1 == verbose)
11129 printf("-> %s\n", filename);
11130 if (tcc_add_file(s, filename) < 0)
11131 ret = 1;
11135 /* free all files */
11136 tcc_free(files);
11138 if (ret)
11139 goto the_end;
11141 if (do_bench) {
11142 double total_time;
11143 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11144 if (total_time < 0.001)
11145 total_time = 0.001;
11146 if (total_bytes < 1)
11147 total_bytes = 1;
11148 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11149 tok_ident - TOK_IDENT, total_lines, total_bytes,
11150 total_time, (int)(total_lines / total_time),
11151 total_bytes / total_time / 1000000.0);
11154 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11155 if (outfile)
11156 fclose(s->outfile);
11157 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11158 ret = tcc_run(s, argc - optind, argv + optind);
11159 } else
11160 ret = tcc_output_file(s, outfile) ? 1 : 0;
11161 the_end:
11162 /* XXX: cannot do it with bound checking because of the malloc hooks */
11163 if (!do_bounds_check)
11164 tcc_delete(s);
11166 #ifdef MEM_DEBUG
11167 if (do_bench) {
11168 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11170 #endif
11171 return ret;
11174 #endif