was hash, not link
[tinycc/daniel.git] / tcc.c
blob4560b3f5aa977fd1f46582a2f41590059a5e5794
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_CCHAR:
1774 case TOK_LCHAR:
1775 cstr_ccat(&cstr_buf, '\'');
1776 add_char(&cstr_buf, cv->i);
1777 cstr_ccat(&cstr_buf, '\'');
1778 cstr_ccat(&cstr_buf, '\0');
1779 break;
1780 case TOK_PPNUM:
1781 cstr = cv->cstr;
1782 len = cstr->size - 1;
1783 for(i=0;i<len;i++)
1784 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1785 cstr_ccat(&cstr_buf, '\0');
1786 break;
1787 case TOK_STR:
1788 case TOK_LSTR:
1789 cstr = cv->cstr;
1790 cstr_ccat(&cstr_buf, '\"');
1791 if (v == TOK_STR) {
1792 len = cstr->size - 1;
1793 for(i=0;i<len;i++)
1794 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1795 } else {
1796 len = (cstr->size / sizeof(nwchar_t)) - 1;
1797 for(i=0;i<len;i++)
1798 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1800 cstr_ccat(&cstr_buf, '\"');
1801 cstr_ccat(&cstr_buf, '\0');
1802 break;
1803 case TOK_LT:
1804 v = '<';
1805 goto addv;
1806 case TOK_GT:
1807 v = '>';
1808 goto addv;
1809 case TOK_DOTS:
1810 return strcpy(p, "...");
1811 case TOK_A_SHL:
1812 return strcpy(p, "<<=");
1813 case TOK_A_SAR:
1814 return strcpy(p, ">>=");
1815 default:
1816 if (v < TOK_IDENT) {
1817 /* search in two bytes table */
1818 q = tok_two_chars;
1819 while (*q) {
1820 if (q[2] == v) {
1821 *p++ = q[0];
1822 *p++ = q[1];
1823 *p = '\0';
1824 return buf;
1826 q += 3;
1828 addv:
1829 *p++ = v;
1830 *p = '\0';
1831 } else if (v < tok_ident) {
1832 return table_ident[v - TOK_IDENT]->str;
1833 } else if (v >= SYM_FIRST_ANOM) {
1834 /* special name for anonymous symbol */
1835 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1836 } else {
1837 /* should never happen */
1838 return NULL;
1840 break;
1842 return cstr_buf.data;
1845 /* push, without hashing */
1846 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1848 Sym *s;
1849 s = sym_malloc();
1850 s->v = v;
1851 s->type.t = t;
1852 s->c = c;
1853 s->next = NULL;
1854 /* add in stack */
1855 s->prev = *ps;
1856 *ps = s;
1857 return s;
1860 /* find a symbol and return its associated structure. 's' is the top
1861 of the symbol stack */
1862 static Sym *sym_find2(Sym *s, int v)
1864 while (s) {
1865 if (s->v == v)
1866 return s;
1867 s = s->prev;
1869 return NULL;
1872 /* structure lookup */
1873 static inline Sym *struct_find(int v)
1875 v -= TOK_IDENT;
1876 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1877 return NULL;
1878 return table_ident[v]->sym_struct;
1881 /* find an identifier */
1882 static inline Sym *sym_find(int v)
1884 v -= TOK_IDENT;
1885 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1886 return NULL;
1887 return table_ident[v]->sym_identifier;
1890 /* push a given symbol on the symbol stack */
1891 static Sym *sym_push(int v, CType *type, int r, int c)
1893 Sym *s, **ps;
1894 TokenSym *ts;
1896 if (local_stack)
1897 ps = &local_stack;
1898 else
1899 ps = &global_stack;
1900 s = sym_push2(ps, v, type->t, c);
1901 s->type.ref = type->ref;
1902 s->r = r;
1903 /* don't record fields or anonymous symbols */
1904 /* XXX: simplify */
1905 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1906 /* record symbol in token array */
1907 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1908 if (v & SYM_STRUCT)
1909 ps = &ts->sym_struct;
1910 else
1911 ps = &ts->sym_identifier;
1912 s->prev_tok = *ps;
1913 *ps = s;
1915 return s;
1918 /* push a global identifier */
1919 static Sym *global_identifier_push(int v, int t, int c)
1921 Sym *s, **ps;
1922 s = sym_push2(&global_stack, v, t, c);
1923 /* don't record anonymous symbol */
1924 if (v < SYM_FIRST_ANOM) {
1925 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1926 /* modify the top most local identifier, so that
1927 sym_identifier will point to 's' when popped */
1928 while (*ps != NULL)
1929 ps = &(*ps)->prev_tok;
1930 s->prev_tok = NULL;
1931 *ps = s;
1933 return s;
1936 /* pop symbols until top reaches 'b' */
1937 static void sym_pop(Sym **ptop, Sym *b)
1939 Sym *s, *ss, **ps;
1940 TokenSym *ts;
1941 int v;
1943 s = *ptop;
1944 while(s != b) {
1945 ss = s->prev;
1946 v = s->v;
1947 /* remove symbol in token array */
1948 /* XXX: simplify */
1949 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1950 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1951 if (v & SYM_STRUCT)
1952 ps = &ts->sym_struct;
1953 else
1954 ps = &ts->sym_identifier;
1955 *ps = s->prev_tok;
1957 sym_free(s);
1958 s = ss;
1960 *ptop = b;
1963 /* I/O layer */
1965 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1967 int fd;
1968 BufferedFile *bf;
1970 if (strcmp(filename, "-") == 0)
1971 fd = 0, filename = "stdin";
1972 else
1973 fd = open(filename, O_RDONLY | O_BINARY);
1974 if ((verbose == 2 && fd >= 0) || verbose == 3)
1975 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
1976 (s1->include_stack_ptr - s1->include_stack), "", filename);
1977 if (fd < 0)
1978 return NULL;
1979 bf = tcc_malloc(sizeof(BufferedFile));
1980 bf->fd = fd;
1981 bf->buf_ptr = bf->buffer;
1982 bf->buf_end = bf->buffer;
1983 bf->buffer[0] = CH_EOB; /* put eob symbol */
1984 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1985 #ifdef _WIN32
1986 normalize_slashes(bf->filename);
1987 #endif
1988 bf->line_num = 1;
1989 bf->ifndef_macro = 0;
1990 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1991 // printf("opening '%s'\n", filename);
1992 return bf;
1995 void tcc_close(BufferedFile *bf)
1997 total_lines += bf->line_num;
1998 close(bf->fd);
1999 tcc_free(bf);
2002 /* fill input buffer and peek next char */
2003 static int tcc_peekc_slow(BufferedFile *bf)
2005 int len;
2006 /* only tries to read if really end of buffer */
2007 if (bf->buf_ptr >= bf->buf_end) {
2008 if (bf->fd != -1) {
2009 #if defined(PARSE_DEBUG)
2010 len = 8;
2011 #else
2012 len = IO_BUF_SIZE;
2013 #endif
2014 len = read(bf->fd, bf->buffer, len);
2015 if (len < 0)
2016 len = 0;
2017 } else {
2018 len = 0;
2020 total_bytes += len;
2021 bf->buf_ptr = bf->buffer;
2022 bf->buf_end = bf->buffer + len;
2023 *bf->buf_end = CH_EOB;
2025 if (bf->buf_ptr < bf->buf_end) {
2026 return bf->buf_ptr[0];
2027 } else {
2028 bf->buf_ptr = bf->buf_end;
2029 return CH_EOF;
2033 /* return the current character, handling end of block if necessary
2034 (but not stray) */
2035 static int handle_eob(void)
2037 return tcc_peekc_slow(file);
2040 /* read next char from current input file and handle end of input buffer */
2041 static inline void inp(void)
2043 ch = *(++(file->buf_ptr));
2044 /* end of buffer/file handling */
2045 if (ch == CH_EOB)
2046 ch = handle_eob();
2049 /* handle '\[\r]\n' */
2050 static int handle_stray_noerror(void)
2052 while (ch == '\\') {
2053 inp();
2054 if (ch == '\n') {
2055 file->line_num++;
2056 inp();
2057 } else if (ch == '\r') {
2058 inp();
2059 if (ch != '\n')
2060 goto fail;
2061 file->line_num++;
2062 inp();
2063 } else {
2064 fail:
2065 return 1;
2068 return 0;
2071 static void handle_stray(void)
2073 if (handle_stray_noerror())
2074 error("stray '\\' in program");
2077 /* skip the stray and handle the \\n case. Output an error if
2078 incorrect char after the stray */
2079 static int handle_stray1(uint8_t *p)
2081 int c;
2083 if (p >= file->buf_end) {
2084 file->buf_ptr = p;
2085 c = handle_eob();
2086 p = file->buf_ptr;
2087 if (c == '\\')
2088 goto parse_stray;
2089 } else {
2090 parse_stray:
2091 file->buf_ptr = p;
2092 ch = *p;
2093 handle_stray();
2094 p = file->buf_ptr;
2095 c = *p;
2097 return c;
2100 /* handle just the EOB case, but not stray */
2101 #define PEEKC_EOB(c, p)\
2103 p++;\
2104 c = *p;\
2105 if (c == '\\') {\
2106 file->buf_ptr = p;\
2107 c = handle_eob();\
2108 p = file->buf_ptr;\
2112 /* handle the complicated stray case */
2113 #define PEEKC(c, p)\
2115 p++;\
2116 c = *p;\
2117 if (c == '\\') {\
2118 c = handle_stray1(p);\
2119 p = file->buf_ptr;\
2123 /* input with '\[\r]\n' handling. Note that this function cannot
2124 handle other characters after '\', so you cannot call it inside
2125 strings or comments */
2126 static void minp(void)
2128 inp();
2129 if (ch == '\\')
2130 handle_stray();
2134 /* single line C++ comments */
2135 static uint8_t *parse_line_comment(uint8_t *p)
2137 int c;
2139 p++;
2140 for(;;) {
2141 c = *p;
2142 redo:
2143 if (c == '\n' || c == CH_EOF) {
2144 break;
2145 } else if (c == '\\') {
2146 file->buf_ptr = p;
2147 c = handle_eob();
2148 p = file->buf_ptr;
2149 if (c == '\\') {
2150 PEEKC_EOB(c, p);
2151 if (c == '\n') {
2152 file->line_num++;
2153 PEEKC_EOB(c, p);
2154 } else if (c == '\r') {
2155 PEEKC_EOB(c, p);
2156 if (c == '\n') {
2157 file->line_num++;
2158 PEEKC_EOB(c, p);
2161 } else {
2162 goto redo;
2164 } else {
2165 p++;
2168 return p;
2171 /* C comments */
2172 static uint8_t *parse_comment(uint8_t *p)
2174 int c;
2176 p++;
2177 for(;;) {
2178 /* fast skip loop */
2179 for(;;) {
2180 c = *p;
2181 if (c == '\n' || c == '*' || c == '\\')
2182 break;
2183 p++;
2184 c = *p;
2185 if (c == '\n' || c == '*' || c == '\\')
2186 break;
2187 p++;
2189 /* now we can handle all the cases */
2190 if (c == '\n') {
2191 file->line_num++;
2192 p++;
2193 } else if (c == '*') {
2194 p++;
2195 for(;;) {
2196 c = *p;
2197 if (c == '*') {
2198 p++;
2199 } else if (c == '/') {
2200 goto end_of_comment;
2201 } else if (c == '\\') {
2202 file->buf_ptr = p;
2203 c = handle_eob();
2204 p = file->buf_ptr;
2205 if (c == '\\') {
2206 /* skip '\[\r]\n', otherwise just skip the stray */
2207 while (c == '\\') {
2208 PEEKC_EOB(c, p);
2209 if (c == '\n') {
2210 file->line_num++;
2211 PEEKC_EOB(c, p);
2212 } else if (c == '\r') {
2213 PEEKC_EOB(c, p);
2214 if (c == '\n') {
2215 file->line_num++;
2216 PEEKC_EOB(c, p);
2218 } else {
2219 goto after_star;
2223 } else {
2224 break;
2227 after_star: ;
2228 } else {
2229 /* stray, eob or eof */
2230 file->buf_ptr = p;
2231 c = handle_eob();
2232 p = file->buf_ptr;
2233 if (c == CH_EOF) {
2234 error("unexpected end of file in comment");
2235 } else if (c == '\\') {
2236 p++;
2240 end_of_comment:
2241 p++;
2242 return p;
2245 #define cinp minp
2247 /* space exlcuding newline */
2248 static inline int is_space(int ch)
2250 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2253 static inline void skip_spaces(void)
2255 while (is_space(ch))
2256 cinp();
2259 /* parse a string without interpreting escapes */
2260 static uint8_t *parse_pp_string(uint8_t *p,
2261 int sep, CString *str)
2263 int c;
2264 p++;
2265 for(;;) {
2266 c = *p;
2267 if (c == sep) {
2268 break;
2269 } else if (c == '\\') {
2270 file->buf_ptr = p;
2271 c = handle_eob();
2272 p = file->buf_ptr;
2273 if (c == CH_EOF) {
2274 unterminated_string:
2275 /* XXX: indicate line number of start of string */
2276 error("missing terminating %c character", sep);
2277 } else if (c == '\\') {
2278 /* escape : just skip \[\r]\n */
2279 PEEKC_EOB(c, p);
2280 if (c == '\n') {
2281 file->line_num++;
2282 p++;
2283 } else if (c == '\r') {
2284 PEEKC_EOB(c, p);
2285 if (c != '\n')
2286 expect("'\n' after '\r'");
2287 file->line_num++;
2288 p++;
2289 } else if (c == CH_EOF) {
2290 goto unterminated_string;
2291 } else {
2292 if (str) {
2293 cstr_ccat(str, '\\');
2294 cstr_ccat(str, c);
2296 p++;
2299 } else if (c == '\n') {
2300 file->line_num++;
2301 goto add_char;
2302 } else if (c == '\r') {
2303 PEEKC_EOB(c, p);
2304 if (c != '\n') {
2305 if (str)
2306 cstr_ccat(str, '\r');
2307 } else {
2308 file->line_num++;
2309 goto add_char;
2311 } else {
2312 add_char:
2313 if (str)
2314 cstr_ccat(str, c);
2315 p++;
2318 p++;
2319 return p;
2322 /* skip block of text until #else, #elif or #endif. skip also pairs of
2323 #if/#endif */
2324 void preprocess_skip(void)
2326 int a, start_of_line, c, in_warn_or_error;
2327 uint8_t *p;
2329 p = file->buf_ptr;
2330 a = 0;
2331 redo_start:
2332 start_of_line = 1;
2333 in_warn_or_error = 0;
2334 for(;;) {
2335 redo_no_start:
2336 c = *p;
2337 switch(c) {
2338 case ' ':
2339 case '\t':
2340 case '\f':
2341 case '\v':
2342 case '\r':
2343 p++;
2344 goto redo_no_start;
2345 case '\n':
2346 file->line_num++;
2347 p++;
2348 goto redo_start;
2349 case '\\':
2350 file->buf_ptr = p;
2351 c = handle_eob();
2352 if (c == CH_EOF) {
2353 expect("#endif");
2354 } else if (c == '\\') {
2355 ch = file->buf_ptr[0];
2356 handle_stray_noerror();
2358 p = file->buf_ptr;
2359 goto redo_no_start;
2360 /* skip strings */
2361 case '\"':
2362 case '\'':
2363 if (in_warn_or_error)
2364 goto _default;
2365 p = parse_pp_string(p, c, NULL);
2366 break;
2367 /* skip comments */
2368 case '/':
2369 if (in_warn_or_error)
2370 goto _default;
2371 file->buf_ptr = p;
2372 ch = *p;
2373 minp();
2374 p = file->buf_ptr;
2375 if (ch == '*') {
2376 p = parse_comment(p);
2377 } else if (ch == '/') {
2378 p = parse_line_comment(p);
2380 break;
2381 case '#':
2382 p++;
2383 if (start_of_line) {
2384 file->buf_ptr = p;
2385 next_nomacro();
2386 p = file->buf_ptr;
2387 if (a == 0 &&
2388 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2389 goto the_end;
2390 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2391 a++;
2392 else if (tok == TOK_ENDIF)
2393 a--;
2394 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2395 in_warn_or_error = 1;
2397 break;
2398 _default:
2399 default:
2400 p++;
2401 break;
2403 start_of_line = 0;
2405 the_end: ;
2406 file->buf_ptr = p;
2409 /* ParseState handling */
2411 /* XXX: currently, no include file info is stored. Thus, we cannot display
2412 accurate messages if the function or data definition spans multiple
2413 files */
2415 /* save current parse state in 's' */
2416 void save_parse_state(ParseState *s)
2418 s->line_num = file->line_num;
2419 s->macro_ptr = macro_ptr;
2420 s->tok = tok;
2421 s->tokc = tokc;
2424 /* restore parse state from 's' */
2425 void restore_parse_state(ParseState *s)
2427 file->line_num = s->line_num;
2428 macro_ptr = s->macro_ptr;
2429 tok = s->tok;
2430 tokc = s->tokc;
2433 /* return the number of additional 'ints' necessary to store the
2434 token */
2435 static inline int tok_ext_size(int t)
2437 switch(t) {
2438 /* 4 bytes */
2439 case TOK_CINT:
2440 case TOK_CUINT:
2441 case TOK_CCHAR:
2442 case TOK_LCHAR:
2443 case TOK_CFLOAT:
2444 case TOK_LINENUM:
2445 return 1;
2446 case TOK_STR:
2447 case TOK_LSTR:
2448 case TOK_PPNUM:
2449 error("unsupported token");
2450 return 1;
2451 case TOK_CDOUBLE:
2452 case TOK_CLLONG:
2453 case TOK_CULLONG:
2454 return 2;
2455 case TOK_CLDOUBLE:
2456 return LDOUBLE_SIZE / 4;
2457 default:
2458 return 0;
2462 /* token string handling */
2464 static inline void tok_str_new(TokenString *s)
2466 s->str = NULL;
2467 s->len = 0;
2468 s->allocated_len = 0;
2469 s->last_line_num = -1;
2472 static void tok_str_free(int *str)
2474 tcc_free(str);
2477 static int *tok_str_realloc(TokenString *s)
2479 int *str, len;
2481 if (s->allocated_len == 0) {
2482 len = 8;
2483 } else {
2484 len = s->allocated_len * 2;
2486 str = tcc_realloc(s->str, len * sizeof(int));
2487 if (!str)
2488 error("memory full");
2489 s->allocated_len = len;
2490 s->str = str;
2491 return str;
2494 static void tok_str_add(TokenString *s, int t)
2496 int len, *str;
2498 len = s->len;
2499 str = s->str;
2500 if (len >= s->allocated_len)
2501 str = tok_str_realloc(s);
2502 str[len++] = t;
2503 s->len = len;
2506 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2508 int len, *str;
2510 len = s->len;
2511 str = s->str;
2513 /* allocate space for worst case */
2514 if (len + TOK_MAX_SIZE > s->allocated_len)
2515 str = tok_str_realloc(s);
2516 str[len++] = t;
2517 switch(t) {
2518 case TOK_CINT:
2519 case TOK_CUINT:
2520 case TOK_CCHAR:
2521 case TOK_LCHAR:
2522 case TOK_CFLOAT:
2523 case TOK_LINENUM:
2524 str[len++] = cv->tab[0];
2525 break;
2526 case TOK_PPNUM:
2527 case TOK_STR:
2528 case TOK_LSTR:
2530 int nb_words;
2531 CString *cstr;
2533 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2534 while ((len + nb_words) > s->allocated_len)
2535 str = tok_str_realloc(s);
2536 cstr = (CString *)(str + len);
2537 cstr->data = NULL;
2538 cstr->size = cv->cstr->size;
2539 cstr->data_allocated = NULL;
2540 cstr->size_allocated = cstr->size;
2541 memcpy((char *)cstr + sizeof(CString),
2542 cv->cstr->data, cstr->size);
2543 len += nb_words;
2545 break;
2546 case TOK_CDOUBLE:
2547 case TOK_CLLONG:
2548 case TOK_CULLONG:
2549 #if LDOUBLE_SIZE == 8
2550 case TOK_CLDOUBLE:
2551 #endif
2552 str[len++] = cv->tab[0];
2553 str[len++] = cv->tab[1];
2554 break;
2555 #if LDOUBLE_SIZE == 12
2556 case TOK_CLDOUBLE:
2557 str[len++] = cv->tab[0];
2558 str[len++] = cv->tab[1];
2559 str[len++] = cv->tab[2];
2560 #elif LDOUBLE_SIZE != 8
2561 #error add long double size support
2562 #endif
2563 break;
2564 default:
2565 break;
2567 s->len = len;
2570 /* add the current parse token in token string 's' */
2571 static void tok_str_add_tok(TokenString *s)
2573 CValue cval;
2575 /* save line number info */
2576 if (file->line_num != s->last_line_num) {
2577 s->last_line_num = file->line_num;
2578 cval.i = s->last_line_num;
2579 tok_str_add2(s, TOK_LINENUM, &cval);
2581 tok_str_add2(s, tok, &tokc);
2584 #if LDOUBLE_SIZE == 12
2585 #define LDOUBLE_GET(p, cv) \
2586 cv.tab[0] = p[0]; \
2587 cv.tab[1] = p[1]; \
2588 cv.tab[2] = p[2];
2589 #elif LDOUBLE_SIZE == 8
2590 #define LDOUBLE_GET(p, cv) \
2591 cv.tab[0] = p[0]; \
2592 cv.tab[1] = p[1];
2593 #else
2594 #error add long double size support
2595 #endif
2598 /* get a token from an integer array and increment pointer
2599 accordingly. we code it as a macro to avoid pointer aliasing. */
2600 #define TOK_GET(t, p, cv) \
2602 t = *p++; \
2603 switch(t) { \
2604 case TOK_CINT: \
2605 case TOK_CUINT: \
2606 case TOK_CCHAR: \
2607 case TOK_LCHAR: \
2608 case TOK_CFLOAT: \
2609 case TOK_LINENUM: \
2610 cv.tab[0] = *p++; \
2611 break; \
2612 case TOK_STR: \
2613 case TOK_LSTR: \
2614 case TOK_PPNUM: \
2615 cv.cstr = (CString *)p; \
2616 cv.cstr->data = (char *)p + sizeof(CString);\
2617 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2618 break; \
2619 case TOK_CDOUBLE: \
2620 case TOK_CLLONG: \
2621 case TOK_CULLONG: \
2622 cv.tab[0] = p[0]; \
2623 cv.tab[1] = p[1]; \
2624 p += 2; \
2625 break; \
2626 case TOK_CLDOUBLE: \
2627 LDOUBLE_GET(p, cv); \
2628 p += LDOUBLE_SIZE / 4; \
2629 break; \
2630 default: \
2631 break; \
2635 /* defines handling */
2636 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2638 Sym *s;
2640 s = sym_push2(&define_stack, v, macro_type, (int)str);
2641 s->next = first_arg;
2642 table_ident[v - TOK_IDENT]->sym_define = s;
2645 /* undefined a define symbol. Its name is just set to zero */
2646 static void define_undef(Sym *s)
2648 int v;
2649 v = s->v;
2650 if (v >= TOK_IDENT && v < tok_ident)
2651 table_ident[v - TOK_IDENT]->sym_define = NULL;
2652 s->v = 0;
2655 static inline Sym *define_find(int v)
2657 v -= TOK_IDENT;
2658 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2659 return NULL;
2660 return table_ident[v]->sym_define;
2663 /* free define stack until top reaches 'b' */
2664 static void free_defines(Sym *b)
2666 Sym *top, *top1;
2667 int v;
2669 top = define_stack;
2670 while (top != b) {
2671 top1 = top->prev;
2672 /* do not free args or predefined defines */
2673 if (top->c)
2674 tok_str_free((int *)top->c);
2675 v = top->v;
2676 if (v >= TOK_IDENT && v < tok_ident)
2677 table_ident[v - TOK_IDENT]->sym_define = NULL;
2678 sym_free(top);
2679 top = top1;
2681 define_stack = b;
2684 /* label lookup */
2685 static Sym *label_find(int v)
2687 v -= TOK_IDENT;
2688 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2689 return NULL;
2690 return table_ident[v]->sym_label;
2693 static Sym *label_push(Sym **ptop, int v, int flags)
2695 Sym *s, **ps;
2696 s = sym_push2(ptop, v, 0, 0);
2697 s->r = flags;
2698 ps = &table_ident[v - TOK_IDENT]->sym_label;
2699 if (ptop == &global_label_stack) {
2700 /* modify the top most local identifier, so that
2701 sym_identifier will point to 's' when popped */
2702 while (*ps != NULL)
2703 ps = &(*ps)->prev_tok;
2705 s->prev_tok = *ps;
2706 *ps = s;
2707 return s;
2710 /* pop labels until element last is reached. Look if any labels are
2711 undefined. Define symbols if '&&label' was used. */
2712 static void label_pop(Sym **ptop, Sym *slast)
2714 Sym *s, *s1;
2715 for(s = *ptop; s != slast; s = s1) {
2716 s1 = s->prev;
2717 if (s->r == LABEL_DECLARED) {
2718 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2719 } else if (s->r == LABEL_FORWARD) {
2720 error("label '%s' used but not defined",
2721 get_tok_str(s->v, NULL));
2722 } else {
2723 if (s->c) {
2724 /* define corresponding symbol. A size of
2725 1 is put. */
2726 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2729 /* remove label */
2730 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2731 sym_free(s);
2733 *ptop = slast;
2736 /* eval an expression for #if/#elif */
2737 static int expr_preprocess(void)
2739 int c, t;
2740 TokenString str;
2742 tok_str_new(&str);
2743 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2744 next(); /* do macro subst */
2745 if (tok == TOK_DEFINED) {
2746 next_nomacro();
2747 t = tok;
2748 if (t == '(')
2749 next_nomacro();
2750 c = define_find(tok) != 0;
2751 if (t == '(')
2752 next_nomacro();
2753 tok = TOK_CINT;
2754 tokc.i = c;
2755 } else if (tok >= TOK_IDENT) {
2756 /* if undefined macro */
2757 tok = TOK_CINT;
2758 tokc.i = 0;
2760 tok_str_add_tok(&str);
2762 tok_str_add(&str, -1); /* simulate end of file */
2763 tok_str_add(&str, 0);
2764 /* now evaluate C constant expression */
2765 macro_ptr = str.str;
2766 next();
2767 c = expr_const();
2768 macro_ptr = NULL;
2769 tok_str_free(str.str);
2770 return c != 0;
2773 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2774 static void tok_print(int *str)
2776 int t;
2777 CValue cval;
2779 while (1) {
2780 TOK_GET(t, str, cval);
2781 if (!t)
2782 break;
2783 printf(" %s", get_tok_str(t, &cval));
2785 printf("\n");
2787 #endif
2789 /* parse after #define */
2790 static void parse_define(void)
2792 Sym *s, *first, **ps;
2793 int v, t, varg, is_vaargs, c;
2794 TokenString str;
2796 v = tok;
2797 if (v < TOK_IDENT)
2798 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2799 /* XXX: should check if same macro (ANSI) */
2800 first = NULL;
2801 t = MACRO_OBJ;
2802 /* '(' must be just after macro definition for MACRO_FUNC */
2803 c = file->buf_ptr[0];
2804 if (c == '\\')
2805 c = handle_stray1(file->buf_ptr);
2806 if (c == '(') {
2807 next_nomacro();
2808 next_nomacro();
2809 ps = &first;
2810 while (tok != ')') {
2811 varg = tok;
2812 next_nomacro();
2813 is_vaargs = 0;
2814 if (varg == TOK_DOTS) {
2815 varg = TOK___VA_ARGS__;
2816 is_vaargs = 1;
2817 } else if (tok == TOK_DOTS && gnu_ext) {
2818 is_vaargs = 1;
2819 next_nomacro();
2821 if (varg < TOK_IDENT)
2822 error("badly punctuated parameter list");
2823 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2824 *ps = s;
2825 ps = &s->next;
2826 if (tok != ',')
2827 break;
2828 next_nomacro();
2830 t = MACRO_FUNC;
2832 tok_str_new(&str);
2833 next_nomacro();
2834 /* EOF testing necessary for '-D' handling */
2835 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2836 tok_str_add2(&str, tok, &tokc);
2837 next_nomacro();
2839 tok_str_add(&str, 0);
2840 #ifdef PP_DEBUG
2841 printf("define %s %d: ", get_tok_str(v, NULL), t);
2842 tok_print(str.str);
2843 #endif
2844 define_push(v, t, str.str, first);
2847 static inline int hash_cached_include(int type, const char *filename)
2849 const unsigned char *s;
2850 unsigned int h;
2852 h = TOK_HASH_INIT;
2853 h = TOK_HASH_FUNC(h, type);
2854 s = filename;
2855 while (*s) {
2856 h = TOK_HASH_FUNC(h, *s);
2857 s++;
2859 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2860 return h;
2863 /* XXX: use a token or a hash table to accelerate matching ? */
2864 static CachedInclude *search_cached_include(TCCState *s1,
2865 int type, const char *filename)
2867 CachedInclude *e;
2868 int i, h;
2869 h = hash_cached_include(type, filename);
2870 i = s1->cached_includes_hash[h];
2871 for(;;) {
2872 if (i == 0)
2873 break;
2874 e = s1->cached_includes[i - 1];
2875 if (e->type == type && !strcmp(e->filename, filename))
2876 return e;
2877 i = e->hash_next;
2879 return NULL;
2882 static inline void add_cached_include(TCCState *s1, int type,
2883 const char *filename, int ifndef_macro)
2885 CachedInclude *e;
2886 int h;
2888 if (search_cached_include(s1, type, filename))
2889 return;
2890 #ifdef INC_DEBUG
2891 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2892 #endif
2893 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2894 if (!e)
2895 return;
2896 e->type = type;
2897 strcpy(e->filename, filename);
2898 e->ifndef_macro = ifndef_macro;
2899 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2900 /* add in hash table */
2901 h = hash_cached_include(type, filename);
2902 e->hash_next = s1->cached_includes_hash[h];
2903 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2906 static void pragma_parse(TCCState *s1)
2908 int val;
2910 next();
2911 if (tok == TOK_pack) {
2913 This may be:
2914 #pragma pack(1) // set
2915 #pragma pack() // reset to default
2916 #pragma pack(push,1) // push & set
2917 #pragma pack(pop) // restore previous
2919 next();
2920 skip('(');
2921 if (tok == TOK_ASM_pop) {
2922 next();
2923 if (s1->pack_stack_ptr <= s1->pack_stack) {
2924 stk_error:
2925 error("out of pack stack");
2927 s1->pack_stack_ptr--;
2928 } else {
2929 val = 0;
2930 if (tok != ')') {
2931 if (tok == TOK_ASM_push) {
2932 next();
2933 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2934 goto stk_error;
2935 s1->pack_stack_ptr++;
2936 skip(',');
2938 if (tok != TOK_CINT) {
2939 pack_error:
2940 error("invalid pack pragma");
2942 val = tokc.i;
2943 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2944 goto pack_error;
2945 next();
2947 *s1->pack_stack_ptr = val;
2948 skip(')');
2953 /* is_bof is true if first non space token at beginning of file */
2954 static void preprocess(int is_bof)
2956 TCCState *s1 = tcc_state;
2957 int size, i, c, n, saved_parse_flags;
2958 char buf[1024], *q;
2959 char buf1[1024];
2960 BufferedFile *f;
2961 Sym *s;
2962 CachedInclude *e;
2964 saved_parse_flags = parse_flags;
2965 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2966 PARSE_FLAG_LINEFEED;
2967 next_nomacro();
2968 redo:
2969 switch(tok) {
2970 case TOK_DEFINE:
2971 next_nomacro();
2972 parse_define();
2973 break;
2974 case TOK_UNDEF:
2975 next_nomacro();
2976 s = define_find(tok);
2977 /* undefine symbol by putting an invalid name */
2978 if (s)
2979 define_undef(s);
2980 break;
2981 case TOK_INCLUDE:
2982 case TOK_INCLUDE_NEXT:
2983 ch = file->buf_ptr[0];
2984 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2985 skip_spaces();
2986 if (ch == '<') {
2987 c = '>';
2988 goto read_name;
2989 } else if (ch == '\"') {
2990 c = ch;
2991 read_name:
2992 inp();
2993 q = buf;
2994 while (ch != c && ch != '\n' && ch != CH_EOF) {
2995 if ((q - buf) < sizeof(buf) - 1)
2996 *q++ = ch;
2997 if (ch == '\\') {
2998 if (handle_stray_noerror() == 0)
2999 --q;
3000 } else
3001 inp();
3003 *q = '\0';
3004 minp();
3005 #if 0
3006 /* eat all spaces and comments after include */
3007 /* XXX: slightly incorrect */
3008 while (ch1 != '\n' && ch1 != CH_EOF)
3009 inp();
3010 #endif
3011 } else {
3012 /* computed #include : either we have only strings or
3013 we have anything enclosed in '<>' */
3014 next();
3015 buf[0] = '\0';
3016 if (tok == TOK_STR) {
3017 while (tok != TOK_LINEFEED) {
3018 if (tok != TOK_STR) {
3019 include_syntax:
3020 error("'#include' expects \"FILENAME\" or <FILENAME>");
3022 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3023 next();
3025 c = '\"';
3026 } else {
3027 int len;
3028 while (tok != TOK_LINEFEED) {
3029 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3030 next();
3032 len = strlen(buf);
3033 /* check syntax and remove '<>' */
3034 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3035 goto include_syntax;
3036 memmove(buf, buf + 1, len - 2);
3037 buf[len - 2] = '\0';
3038 c = '>';
3042 e = search_cached_include(s1, c, buf);
3043 if (e && define_find(e->ifndef_macro)) {
3044 /* no need to parse the include because the 'ifndef macro'
3045 is defined */
3046 #ifdef INC_DEBUG
3047 printf("%s: skipping %s\n", file->filename, buf);
3048 #endif
3049 } else {
3050 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3051 error("#include recursion too deep");
3052 /* push current file in stack */
3053 /* XXX: fix current line init */
3054 *s1->include_stack_ptr++ = file;
3055 if (c == '\"') {
3056 /* first search in current dir if "header.h" */
3057 size = tcc_basename(file->filename) - file->filename;
3058 if (size > sizeof(buf1) - 1)
3059 size = sizeof(buf1) - 1;
3060 memcpy(buf1, file->filename, size);
3061 buf1[size] = '\0';
3062 pstrcat(buf1, sizeof(buf1), buf);
3063 f = tcc_open(s1, buf1);
3064 if (f) {
3065 if (tok == TOK_INCLUDE_NEXT)
3066 tok = TOK_INCLUDE;
3067 else
3068 goto found;
3071 /* now search in all the include paths */
3072 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3073 for(i = 0; i < n; i++) {
3074 const char *path;
3075 if (i < s1->nb_include_paths)
3076 path = s1->include_paths[i];
3077 else
3078 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3079 pstrcpy(buf1, sizeof(buf1), path);
3080 pstrcat(buf1, sizeof(buf1), "/");
3081 pstrcat(buf1, sizeof(buf1), buf);
3082 f = tcc_open(s1, buf1);
3083 if (f) {
3084 if (tok == TOK_INCLUDE_NEXT)
3085 tok = TOK_INCLUDE;
3086 else
3087 goto found;
3090 --s1->include_stack_ptr;
3091 error("include file '%s' not found", buf);
3092 break;
3093 found:
3094 #ifdef INC_DEBUG
3095 printf("%s: including %s\n", file->filename, buf1);
3096 #endif
3097 f->inc_type = c;
3098 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3099 file = f;
3100 /* add include file debug info */
3101 if (do_debug) {
3102 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3104 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3105 ch = file->buf_ptr[0];
3106 goto the_end;
3108 break;
3109 case TOK_IFNDEF:
3110 c = 1;
3111 goto do_ifdef;
3112 case TOK_IF:
3113 c = expr_preprocess();
3114 goto do_if;
3115 case TOK_IFDEF:
3116 c = 0;
3117 do_ifdef:
3118 next_nomacro();
3119 if (tok < TOK_IDENT)
3120 error("invalid argument for '#if%sdef'", c ? "n" : "");
3121 if (is_bof) {
3122 if (c) {
3123 #ifdef INC_DEBUG
3124 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3125 #endif
3126 file->ifndef_macro = tok;
3129 c = (define_find(tok) != 0) ^ c;
3130 do_if:
3131 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3132 error("memory full");
3133 *s1->ifdef_stack_ptr++ = c;
3134 goto test_skip;
3135 case TOK_ELSE:
3136 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3137 error("#else without matching #if");
3138 if (s1->ifdef_stack_ptr[-1] & 2)
3139 error("#else after #else");
3140 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3141 goto test_skip;
3142 case TOK_ELIF:
3143 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3144 error("#elif without matching #if");
3145 c = s1->ifdef_stack_ptr[-1];
3146 if (c > 1)
3147 error("#elif after #else");
3148 /* last #if/#elif expression was true: we skip */
3149 if (c == 1)
3150 goto skip;
3151 c = expr_preprocess();
3152 s1->ifdef_stack_ptr[-1] = c;
3153 test_skip:
3154 if (!(c & 1)) {
3155 skip:
3156 preprocess_skip();
3157 is_bof = 0;
3158 goto redo;
3160 break;
3161 case TOK_ENDIF:
3162 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3163 error("#endif without matching #if");
3164 s1->ifdef_stack_ptr--;
3165 /* '#ifndef macro' was at the start of file. Now we check if
3166 an '#endif' is exactly at the end of file */
3167 if (file->ifndef_macro &&
3168 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3169 file->ifndef_macro_saved = file->ifndef_macro;
3170 /* need to set to zero to avoid false matches if another
3171 #ifndef at middle of file */
3172 file->ifndef_macro = 0;
3173 while (tok != TOK_LINEFEED)
3174 next_nomacro();
3175 tok_flags |= TOK_FLAG_ENDIF;
3176 goto the_end;
3178 break;
3179 case TOK_LINE:
3180 next();
3181 if (tok != TOK_CINT)
3182 error("#line");
3183 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3184 next();
3185 if (tok != TOK_LINEFEED) {
3186 if (tok != TOK_STR)
3187 error("#line");
3188 pstrcpy(file->filename, sizeof(file->filename),
3189 (char *)tokc.cstr->data);
3191 break;
3192 case TOK_ERROR:
3193 case TOK_WARNING:
3194 c = tok;
3195 ch = file->buf_ptr[0];
3196 skip_spaces();
3197 q = buf;
3198 while (ch != '\n' && ch != CH_EOF) {
3199 if ((q - buf) < sizeof(buf) - 1)
3200 *q++ = ch;
3201 if (ch == '\\') {
3202 if (handle_stray_noerror() == 0)
3203 --q;
3204 } else
3205 inp();
3207 *q = '\0';
3208 if (c == TOK_ERROR)
3209 error("#error %s", buf);
3210 else
3211 warning("#warning %s", buf);
3212 break;
3213 case TOK_PRAGMA:
3214 pragma_parse(s1);
3215 break;
3216 default:
3217 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3218 /* '!' is ignored to allow C scripts. numbers are ignored
3219 to emulate cpp behaviour */
3220 } else {
3221 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3222 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3224 break;
3226 /* ignore other preprocess commands or #! for C scripts */
3227 while (tok != TOK_LINEFEED)
3228 next_nomacro();
3229 the_end:
3230 parse_flags = saved_parse_flags;
3233 /* evaluate escape codes in a string. */
3234 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3236 int c, n;
3237 const uint8_t *p;
3239 p = buf;
3240 for(;;) {
3241 c = *p;
3242 if (c == '\0')
3243 break;
3244 if (c == '\\') {
3245 p++;
3246 /* escape */
3247 c = *p;
3248 switch(c) {
3249 case '0': case '1': case '2': case '3':
3250 case '4': case '5': case '6': case '7':
3251 /* at most three octal digits */
3252 n = c - '0';
3253 p++;
3254 c = *p;
3255 if (isoct(c)) {
3256 n = n * 8 + c - '0';
3257 p++;
3258 c = *p;
3259 if (isoct(c)) {
3260 n = n * 8 + c - '0';
3261 p++;
3264 c = n;
3265 goto add_char_nonext;
3266 case 'x':
3267 case 'u':
3268 case 'U':
3269 p++;
3270 n = 0;
3271 for(;;) {
3272 c = *p;
3273 if (c >= 'a' && c <= 'f')
3274 c = c - 'a' + 10;
3275 else if (c >= 'A' && c <= 'F')
3276 c = c - 'A' + 10;
3277 else if (isnum(c))
3278 c = c - '0';
3279 else
3280 break;
3281 n = n * 16 + c;
3282 p++;
3284 c = n;
3285 goto add_char_nonext;
3286 case 'a':
3287 c = '\a';
3288 break;
3289 case 'b':
3290 c = '\b';
3291 break;
3292 case 'f':
3293 c = '\f';
3294 break;
3295 case 'n':
3296 c = '\n';
3297 break;
3298 case 'r':
3299 c = '\r';
3300 break;
3301 case 't':
3302 c = '\t';
3303 break;
3304 case 'v':
3305 c = '\v';
3306 break;
3307 case 'e':
3308 if (!gnu_ext)
3309 goto invalid_escape;
3310 c = 27;
3311 break;
3312 case '\'':
3313 case '\"':
3314 case '\\':
3315 case '?':
3316 break;
3317 default:
3318 invalid_escape:
3319 if (c >= '!' && c <= '~')
3320 warning("unknown escape sequence: \'\\%c\'", c);
3321 else
3322 warning("unknown escape sequence: \'\\x%x\'", c);
3323 break;
3326 p++;
3327 add_char_nonext:
3328 if (!is_long)
3329 cstr_ccat(outstr, c);
3330 else
3331 cstr_wccat(outstr, c);
3333 /* add a trailing '\0' */
3334 if (!is_long)
3335 cstr_ccat(outstr, '\0');
3336 else
3337 cstr_wccat(outstr, '\0');
3340 /* we use 64 bit numbers */
3341 #define BN_SIZE 2
3343 /* bn = (bn << shift) | or_val */
3344 void bn_lshift(unsigned int *bn, int shift, int or_val)
3346 int i;
3347 unsigned int v;
3348 for(i=0;i<BN_SIZE;i++) {
3349 v = bn[i];
3350 bn[i] = (v << shift) | or_val;
3351 or_val = v >> (32 - shift);
3355 void bn_zero(unsigned int *bn)
3357 int i;
3358 for(i=0;i<BN_SIZE;i++) {
3359 bn[i] = 0;
3363 /* parse number in null terminated string 'p' and return it in the
3364 current token */
3365 void parse_number(const char *p)
3367 int b, t, shift, frac_bits, s, exp_val, ch;
3368 char *q;
3369 unsigned int bn[BN_SIZE];
3370 double d;
3372 /* number */
3373 q = token_buf;
3374 ch = *p++;
3375 t = ch;
3376 ch = *p++;
3377 *q++ = t;
3378 b = 10;
3379 if (t == '.') {
3380 goto float_frac_parse;
3381 } else if (t == '0') {
3382 if (ch == 'x' || ch == 'X') {
3383 q--;
3384 ch = *p++;
3385 b = 16;
3386 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3387 q--;
3388 ch = *p++;
3389 b = 2;
3392 /* parse all digits. cannot check octal numbers at this stage
3393 because of floating point constants */
3394 while (1) {
3395 if (ch >= 'a' && ch <= 'f')
3396 t = ch - 'a' + 10;
3397 else if (ch >= 'A' && ch <= 'F')
3398 t = ch - 'A' + 10;
3399 else if (isnum(ch))
3400 t = ch - '0';
3401 else
3402 break;
3403 if (t >= b)
3404 break;
3405 if (q >= token_buf + STRING_MAX_SIZE) {
3406 num_too_long:
3407 error("number too long");
3409 *q++ = ch;
3410 ch = *p++;
3412 if (ch == '.' ||
3413 ((ch == 'e' || ch == 'E') && b == 10) ||
3414 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3415 if (b != 10) {
3416 /* NOTE: strtox should support that for hexa numbers, but
3417 non ISOC99 libcs do not support it, so we prefer to do
3418 it by hand */
3419 /* hexadecimal or binary floats */
3420 /* XXX: handle overflows */
3421 *q = '\0';
3422 if (b == 16)
3423 shift = 4;
3424 else
3425 shift = 2;
3426 bn_zero(bn);
3427 q = token_buf;
3428 while (1) {
3429 t = *q++;
3430 if (t == '\0') {
3431 break;
3432 } else if (t >= 'a') {
3433 t = t - 'a' + 10;
3434 } else if (t >= 'A') {
3435 t = t - 'A' + 10;
3436 } else {
3437 t = t - '0';
3439 bn_lshift(bn, shift, t);
3441 frac_bits = 0;
3442 if (ch == '.') {
3443 ch = *p++;
3444 while (1) {
3445 t = ch;
3446 if (t >= 'a' && t <= 'f') {
3447 t = t - 'a' + 10;
3448 } else if (t >= 'A' && t <= 'F') {
3449 t = t - 'A' + 10;
3450 } else if (t >= '0' && t <= '9') {
3451 t = t - '0';
3452 } else {
3453 break;
3455 if (t >= b)
3456 error("invalid digit");
3457 bn_lshift(bn, shift, t);
3458 frac_bits += shift;
3459 ch = *p++;
3462 if (ch != 'p' && ch != 'P')
3463 expect("exponent");
3464 ch = *p++;
3465 s = 1;
3466 exp_val = 0;
3467 if (ch == '+') {
3468 ch = *p++;
3469 } else if (ch == '-') {
3470 s = -1;
3471 ch = *p++;
3473 if (ch < '0' || ch > '9')
3474 expect("exponent digits");
3475 while (ch >= '0' && ch <= '9') {
3476 exp_val = exp_val * 10 + ch - '0';
3477 ch = *p++;
3479 exp_val = exp_val * s;
3481 /* now we can generate the number */
3482 /* XXX: should patch directly float number */
3483 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3484 d = ldexp(d, exp_val - frac_bits);
3485 t = toup(ch);
3486 if (t == 'F') {
3487 ch = *p++;
3488 tok = TOK_CFLOAT;
3489 /* float : should handle overflow */
3490 tokc.f = (float)d;
3491 } else if (t == 'L') {
3492 ch = *p++;
3493 tok = TOK_CLDOUBLE;
3494 /* XXX: not large enough */
3495 tokc.ld = (long double)d;
3496 } else {
3497 tok = TOK_CDOUBLE;
3498 tokc.d = d;
3500 } else {
3501 /* decimal floats */
3502 if (ch == '.') {
3503 if (q >= token_buf + STRING_MAX_SIZE)
3504 goto num_too_long;
3505 *q++ = ch;
3506 ch = *p++;
3507 float_frac_parse:
3508 while (ch >= '0' && ch <= '9') {
3509 if (q >= token_buf + STRING_MAX_SIZE)
3510 goto num_too_long;
3511 *q++ = ch;
3512 ch = *p++;
3515 if (ch == 'e' || ch == 'E') {
3516 if (q >= token_buf + STRING_MAX_SIZE)
3517 goto num_too_long;
3518 *q++ = ch;
3519 ch = *p++;
3520 if (ch == '-' || ch == '+') {
3521 if (q >= token_buf + STRING_MAX_SIZE)
3522 goto num_too_long;
3523 *q++ = ch;
3524 ch = *p++;
3526 if (ch < '0' || ch > '9')
3527 expect("exponent digits");
3528 while (ch >= '0' && ch <= '9') {
3529 if (q >= token_buf + STRING_MAX_SIZE)
3530 goto num_too_long;
3531 *q++ = ch;
3532 ch = *p++;
3535 *q = '\0';
3536 t = toup(ch);
3537 errno = 0;
3538 if (t == 'F') {
3539 ch = *p++;
3540 tok = TOK_CFLOAT;
3541 tokc.f = strtof(token_buf, NULL);
3542 } else if (t == 'L') {
3543 ch = *p++;
3544 tok = TOK_CLDOUBLE;
3545 tokc.ld = strtold(token_buf, NULL);
3546 } else {
3547 tok = TOK_CDOUBLE;
3548 tokc.d = strtod(token_buf, NULL);
3551 } else {
3552 unsigned long long n, n1;
3553 int lcount, ucount;
3555 /* integer number */
3556 *q = '\0';
3557 q = token_buf;
3558 if (b == 10 && *q == '0') {
3559 b = 8;
3560 q++;
3562 n = 0;
3563 while(1) {
3564 t = *q++;
3565 /* no need for checks except for base 10 / 8 errors */
3566 if (t == '\0') {
3567 break;
3568 } else if (t >= 'a') {
3569 t = t - 'a' + 10;
3570 } else if (t >= 'A') {
3571 t = t - 'A' + 10;
3572 } else {
3573 t = t - '0';
3574 if (t >= b)
3575 error("invalid digit");
3577 n1 = n;
3578 n = n * b + t;
3579 /* detect overflow */
3580 /* XXX: this test is not reliable */
3581 if (n < n1)
3582 error("integer constant overflow");
3585 /* XXX: not exactly ANSI compliant */
3586 if ((n & 0xffffffff00000000LL) != 0) {
3587 if ((n >> 63) != 0)
3588 tok = TOK_CULLONG;
3589 else
3590 tok = TOK_CLLONG;
3591 } else if (n > 0x7fffffff) {
3592 tok = TOK_CUINT;
3593 } else {
3594 tok = TOK_CINT;
3596 lcount = 0;
3597 ucount = 0;
3598 for(;;) {
3599 t = toup(ch);
3600 if (t == 'L') {
3601 if (lcount >= 2)
3602 error("three 'l's in integer constant");
3603 lcount++;
3604 if (lcount == 2) {
3605 if (tok == TOK_CINT)
3606 tok = TOK_CLLONG;
3607 else if (tok == TOK_CUINT)
3608 tok = TOK_CULLONG;
3610 ch = *p++;
3611 } else if (t == 'U') {
3612 if (ucount >= 1)
3613 error("two 'u's in integer constant");
3614 ucount++;
3615 if (tok == TOK_CINT)
3616 tok = TOK_CUINT;
3617 else if (tok == TOK_CLLONG)
3618 tok = TOK_CULLONG;
3619 ch = *p++;
3620 } else {
3621 break;
3624 if (tok == TOK_CINT || tok == TOK_CUINT)
3625 tokc.ui = n;
3626 else
3627 tokc.ull = n;
3632 #define PARSE2(c1, tok1, c2, tok2) \
3633 case c1: \
3634 PEEKC(c, p); \
3635 if (c == c2) { \
3636 p++; \
3637 tok = tok2; \
3638 } else { \
3639 tok = tok1; \
3641 break;
3643 /* return next token without macro substitution */
3644 static inline void next_nomacro1(void)
3646 int t, c, is_long;
3647 TokenSym *ts;
3648 uint8_t *p, *p1;
3649 unsigned int h;
3651 p = file->buf_ptr;
3652 redo_no_start:
3653 c = *p;
3654 switch(c) {
3655 case ' ':
3656 case '\t':
3657 case '\f':
3658 case '\v':
3659 case '\r':
3660 p++;
3661 goto redo_no_start;
3663 case '\\':
3664 /* first look if it is in fact an end of buffer */
3665 if (p >= file->buf_end) {
3666 file->buf_ptr = p;
3667 handle_eob();
3668 p = file->buf_ptr;
3669 if (p >= file->buf_end)
3670 goto parse_eof;
3671 else
3672 goto redo_no_start;
3673 } else {
3674 file->buf_ptr = p;
3675 ch = *p;
3676 handle_stray();
3677 p = file->buf_ptr;
3678 goto redo_no_start;
3680 parse_eof:
3682 TCCState *s1 = tcc_state;
3683 if ((parse_flags & PARSE_FLAG_LINEFEED)
3684 && !(tok_flags & TOK_FLAG_EOF)) {
3685 tok_flags |= TOK_FLAG_EOF;
3686 tok = TOK_LINEFEED;
3687 goto keep_tok_flags;
3688 } else if (s1->include_stack_ptr == s1->include_stack ||
3689 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3690 /* no include left : end of file. */
3691 tok = TOK_EOF;
3692 } else {
3693 tok_flags &= ~TOK_FLAG_EOF;
3694 /* pop include file */
3696 /* test if previous '#endif' was after a #ifdef at
3697 start of file */
3698 if (tok_flags & TOK_FLAG_ENDIF) {
3699 #ifdef INC_DEBUG
3700 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3701 #endif
3702 add_cached_include(s1, file->inc_type, file->inc_filename,
3703 file->ifndef_macro_saved);
3706 /* add end of include file debug info */
3707 if (do_debug) {
3708 put_stabd(N_EINCL, 0, 0);
3710 /* pop include stack */
3711 tcc_close(file);
3712 s1->include_stack_ptr--;
3713 file = *s1->include_stack_ptr;
3714 p = file->buf_ptr;
3715 goto redo_no_start;
3718 break;
3720 case '\n':
3721 file->line_num++;
3722 tok_flags |= TOK_FLAG_BOL;
3723 p++;
3724 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3725 goto redo_no_start;
3726 tok = TOK_LINEFEED;
3727 goto keep_tok_flags;
3729 case '#':
3730 /* XXX: simplify */
3731 PEEKC(c, p);
3732 if ((tok_flags & TOK_FLAG_BOL) &&
3733 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3734 file->buf_ptr = p;
3735 preprocess(tok_flags & TOK_FLAG_BOF);
3736 p = file->buf_ptr;
3737 goto redo_no_start;
3738 } else {
3739 if (c == '#') {
3740 p++;
3741 tok = TOK_TWOSHARPS;
3742 } else {
3743 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3744 p = parse_line_comment(p - 1);
3745 goto redo_no_start;
3746 } else {
3747 tok = '#';
3751 break;
3753 case 'a': case 'b': case 'c': case 'd':
3754 case 'e': case 'f': case 'g': case 'h':
3755 case 'i': case 'j': case 'k': case 'l':
3756 case 'm': case 'n': case 'o': case 'p':
3757 case 'q': case 'r': case 's': case 't':
3758 case 'u': case 'v': case 'w': case 'x':
3759 case 'y': case 'z':
3760 case 'A': case 'B': case 'C': case 'D':
3761 case 'E': case 'F': case 'G': case 'H':
3762 case 'I': case 'J': case 'K':
3763 case 'M': case 'N': case 'O': case 'P':
3764 case 'Q': case 'R': case 'S': case 'T':
3765 case 'U': case 'V': case 'W': case 'X':
3766 case 'Y': case 'Z':
3767 case '_':
3768 parse_ident_fast:
3769 p1 = p;
3770 h = TOK_HASH_INIT;
3771 h = TOK_HASH_FUNC(h, c);
3772 p++;
3773 for(;;) {
3774 c = *p;
3775 if (!isidnum_table[c-CH_EOF])
3776 break;
3777 h = TOK_HASH_FUNC(h, c);
3778 p++;
3780 if (c != '\\') {
3781 TokenSym **pts;
3782 int len;
3784 /* fast case : no stray found, so we have the full token
3785 and we have already hashed it */
3786 len = p - p1;
3787 h &= (TOK_HASH_SIZE - 1);
3788 pts = &hash_ident[h];
3789 for(;;) {
3790 ts = *pts;
3791 if (!ts)
3792 break;
3793 if (ts->len == len && !memcmp(ts->str, p1, len))
3794 goto token_found;
3795 pts = &(ts->hash_next);
3797 ts = tok_alloc_new(pts, p1, len);
3798 token_found: ;
3799 } else {
3800 /* slower case */
3801 cstr_reset(&tokcstr);
3803 while (p1 < p) {
3804 cstr_ccat(&tokcstr, *p1);
3805 p1++;
3807 p--;
3808 PEEKC(c, p);
3809 parse_ident_slow:
3810 while (isidnum_table[c-CH_EOF]) {
3811 cstr_ccat(&tokcstr, c);
3812 PEEKC(c, p);
3814 ts = tok_alloc(tokcstr.data, tokcstr.size);
3816 tok = ts->tok;
3817 break;
3818 case 'L':
3819 t = p[1];
3820 if (t != '\\' && t != '\'' && t != '\"') {
3821 /* fast case */
3822 goto parse_ident_fast;
3823 } else {
3824 PEEKC(c, p);
3825 if (c == '\'' || c == '\"') {
3826 is_long = 1;
3827 goto str_const;
3828 } else {
3829 cstr_reset(&tokcstr);
3830 cstr_ccat(&tokcstr, 'L');
3831 goto parse_ident_slow;
3834 break;
3835 case '0': case '1': case '2': case '3':
3836 case '4': case '5': case '6': case '7':
3837 case '8': case '9':
3839 cstr_reset(&tokcstr);
3840 /* after the first digit, accept digits, alpha, '.' or sign if
3841 prefixed by 'eEpP' */
3842 parse_num:
3843 for(;;) {
3844 t = c;
3845 cstr_ccat(&tokcstr, c);
3846 PEEKC(c, p);
3847 if (!(isnum(c) || isid(c) || c == '.' ||
3848 ((c == '+' || c == '-') &&
3849 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3850 break;
3852 /* We add a trailing '\0' to ease parsing */
3853 cstr_ccat(&tokcstr, '\0');
3854 tokc.cstr = &tokcstr;
3855 tok = TOK_PPNUM;
3856 break;
3857 case '.':
3858 /* special dot handling because it can also start a number */
3859 PEEKC(c, p);
3860 if (isnum(c)) {
3861 cstr_reset(&tokcstr);
3862 cstr_ccat(&tokcstr, '.');
3863 goto parse_num;
3864 } else if (c == '.') {
3865 PEEKC(c, p);
3866 if (c != '.')
3867 expect("'.'");
3868 PEEKC(c, p);
3869 tok = TOK_DOTS;
3870 } else {
3871 tok = '.';
3873 break;
3874 case '\'':
3875 case '\"':
3876 is_long = 0;
3877 str_const:
3879 CString str;
3880 int sep;
3882 sep = c;
3884 /* parse the string */
3885 cstr_new(&str);
3886 p = parse_pp_string(p, sep, &str);
3887 cstr_ccat(&str, '\0');
3889 /* eval the escape (should be done as TOK_PPNUM) */
3890 cstr_reset(&tokcstr);
3891 parse_escape_string(&tokcstr, str.data, is_long);
3892 cstr_free(&str);
3894 if (sep == '\'') {
3895 int char_size;
3896 /* XXX: make it portable */
3897 if (!is_long)
3898 char_size = 1;
3899 else
3900 char_size = sizeof(nwchar_t);
3901 if (tokcstr.size <= char_size)
3902 error("empty character constant");
3903 if (tokcstr.size > 2 * char_size)
3904 warning("multi-character character constant");
3905 if (!is_long) {
3906 tokc.i = *(int8_t *)tokcstr.data;
3907 tok = TOK_CCHAR;
3908 } else {
3909 tokc.i = *(nwchar_t *)tokcstr.data;
3910 tok = TOK_LCHAR;
3912 } else {
3913 tokc.cstr = &tokcstr;
3914 if (!is_long)
3915 tok = TOK_STR;
3916 else
3917 tok = TOK_LSTR;
3920 break;
3922 case '<':
3923 PEEKC(c, p);
3924 if (c == '=') {
3925 p++;
3926 tok = TOK_LE;
3927 } else if (c == '<') {
3928 PEEKC(c, p);
3929 if (c == '=') {
3930 p++;
3931 tok = TOK_A_SHL;
3932 } else {
3933 tok = TOK_SHL;
3935 } else {
3936 tok = TOK_LT;
3938 break;
3940 case '>':
3941 PEEKC(c, p);
3942 if (c == '=') {
3943 p++;
3944 tok = TOK_GE;
3945 } else if (c == '>') {
3946 PEEKC(c, p);
3947 if (c == '=') {
3948 p++;
3949 tok = TOK_A_SAR;
3950 } else {
3951 tok = TOK_SAR;
3953 } else {
3954 tok = TOK_GT;
3956 break;
3958 case '&':
3959 PEEKC(c, p);
3960 if (c == '&') {
3961 p++;
3962 tok = TOK_LAND;
3963 } else if (c == '=') {
3964 p++;
3965 tok = TOK_A_AND;
3966 } else {
3967 tok = '&';
3969 break;
3971 case '|':
3972 PEEKC(c, p);
3973 if (c == '|') {
3974 p++;
3975 tok = TOK_LOR;
3976 } else if (c == '=') {
3977 p++;
3978 tok = TOK_A_OR;
3979 } else {
3980 tok = '|';
3982 break;
3984 case '+':
3985 PEEKC(c, p);
3986 if (c == '+') {
3987 p++;
3988 tok = TOK_INC;
3989 } else if (c == '=') {
3990 p++;
3991 tok = TOK_A_ADD;
3992 } else {
3993 tok = '+';
3995 break;
3997 case '-':
3998 PEEKC(c, p);
3999 if (c == '-') {
4000 p++;
4001 tok = TOK_DEC;
4002 } else if (c == '=') {
4003 p++;
4004 tok = TOK_A_SUB;
4005 } else if (c == '>') {
4006 p++;
4007 tok = TOK_ARROW;
4008 } else {
4009 tok = '-';
4011 break;
4013 PARSE2('!', '!', '=', TOK_NE)
4014 PARSE2('=', '=', '=', TOK_EQ)
4015 PARSE2('*', '*', '=', TOK_A_MUL)
4016 PARSE2('%', '%', '=', TOK_A_MOD)
4017 PARSE2('^', '^', '=', TOK_A_XOR)
4019 /* comments or operator */
4020 case '/':
4021 PEEKC(c, p);
4022 if (c == '*') {
4023 p = parse_comment(p);
4024 goto redo_no_start;
4025 } else if (c == '/') {
4026 p = parse_line_comment(p);
4027 goto redo_no_start;
4028 } else if (c == '=') {
4029 p++;
4030 tok = TOK_A_DIV;
4031 } else {
4032 tok = '/';
4034 break;
4036 /* simple tokens */
4037 case '(':
4038 case ')':
4039 case '[':
4040 case ']':
4041 case '{':
4042 case '}':
4043 case ',':
4044 case ';':
4045 case ':':
4046 case '?':
4047 case '~':
4048 case '$': /* only used in assembler */
4049 case '@': /* dito */
4050 tok = c;
4051 p++;
4052 break;
4053 default:
4054 error("unrecognized character \\x%02x", c);
4055 break;
4057 tok_flags = 0;
4058 keep_tok_flags:
4059 file->buf_ptr = p;
4060 #if defined(PARSE_DEBUG)
4061 printf("token = %s\n", get_tok_str(tok, &tokc));
4062 #endif
4065 /* return next token without macro substitution. Can read input from
4066 macro_ptr buffer */
4067 static void next_nomacro(void)
4069 if (macro_ptr) {
4070 redo:
4071 tok = *macro_ptr;
4072 if (tok) {
4073 TOK_GET(tok, macro_ptr, tokc);
4074 if (tok == TOK_LINENUM) {
4075 file->line_num = tokc.i;
4076 goto redo;
4079 } else {
4080 next_nomacro1();
4084 /* substitute args in macro_str and return allocated string */
4085 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4087 int *st, last_tok, t, notfirst;
4088 Sym *s;
4089 CValue cval;
4090 TokenString str;
4091 CString cstr;
4093 tok_str_new(&str);
4094 last_tok = 0;
4095 while(1) {
4096 TOK_GET(t, macro_str, cval);
4097 if (!t)
4098 break;
4099 if (t == '#') {
4100 /* stringize */
4101 TOK_GET(t, macro_str, cval);
4102 if (!t)
4103 break;
4104 s = sym_find2(args, t);
4105 if (s) {
4106 cstr_new(&cstr);
4107 st = (int *)s->c;
4108 notfirst = 0;
4109 while (*st) {
4110 if (notfirst)
4111 cstr_ccat(&cstr, ' ');
4112 TOK_GET(t, st, cval);
4113 cstr_cat(&cstr, get_tok_str(t, &cval));
4114 #ifndef PP_NOSPACES
4115 notfirst = 1;
4116 #endif
4118 cstr_ccat(&cstr, '\0');
4119 #ifdef PP_DEBUG
4120 printf("stringize: %s\n", (char *)cstr.data);
4121 #endif
4122 /* add string */
4123 cval.cstr = &cstr;
4124 tok_str_add2(&str, TOK_STR, &cval);
4125 cstr_free(&cstr);
4126 } else {
4127 tok_str_add2(&str, t, &cval);
4129 } else if (t >= TOK_IDENT) {
4130 s = sym_find2(args, t);
4131 if (s) {
4132 st = (int *)s->c;
4133 /* if '##' is present before or after, no arg substitution */
4134 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4135 /* special case for var arg macros : ## eats the
4136 ',' if empty VA_ARGS variable. */
4137 /* XXX: test of the ',' is not 100%
4138 reliable. should fix it to avoid security
4139 problems */
4140 if (gnu_ext && s->type.t &&
4141 last_tok == TOK_TWOSHARPS &&
4142 str.len >= 2 && str.str[str.len - 2] == ',') {
4143 if (*st == 0) {
4144 /* suppress ',' '##' */
4145 str.len -= 2;
4146 } else {
4147 /* suppress '##' and add variable */
4148 str.len--;
4149 goto add_var;
4151 } else {
4152 int t1;
4153 add_var:
4154 for(;;) {
4155 TOK_GET(t1, st, cval);
4156 if (!t1)
4157 break;
4158 tok_str_add2(&str, t1, &cval);
4161 } else {
4162 /* NOTE: the stream cannot be read when macro
4163 substituing an argument */
4164 macro_subst(&str, nested_list, st, NULL);
4166 } else {
4167 tok_str_add(&str, t);
4169 } else {
4170 tok_str_add2(&str, t, &cval);
4172 last_tok = t;
4174 tok_str_add(&str, 0);
4175 return str.str;
4178 static char const ab_month_name[12][4] =
4180 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4181 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4184 /* do macro substitution of current token with macro 's' and add
4185 result to (tok_str,tok_len). 'nested_list' is the list of all
4186 macros we got inside to avoid recursing. Return non zero if no
4187 substitution needs to be done */
4188 static int macro_subst_tok(TokenString *tok_str,
4189 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4191 Sym *args, *sa, *sa1;
4192 int mstr_allocated, parlevel, *mstr, t, t1;
4193 TokenString str;
4194 char *cstrval;
4195 CValue cval;
4196 CString cstr;
4197 char buf[32];
4199 /* if symbol is a macro, prepare substitution */
4200 /* special macros */
4201 if (tok == TOK___LINE__) {
4202 snprintf(buf, sizeof(buf), "%d", file->line_num);
4203 cstrval = buf;
4204 t1 = TOK_PPNUM;
4205 goto add_cstr1;
4206 } else if (tok == TOK___FILE__) {
4207 cstrval = file->filename;
4208 goto add_cstr;
4209 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4210 time_t ti;
4211 struct tm *tm;
4213 time(&ti);
4214 tm = localtime(&ti);
4215 if (tok == TOK___DATE__) {
4216 snprintf(buf, sizeof(buf), "%s %2d %d",
4217 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4218 } else {
4219 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4220 tm->tm_hour, tm->tm_min, tm->tm_sec);
4222 cstrval = buf;
4223 add_cstr:
4224 t1 = TOK_STR;
4225 add_cstr1:
4226 cstr_new(&cstr);
4227 cstr_cat(&cstr, cstrval);
4228 cstr_ccat(&cstr, '\0');
4229 cval.cstr = &cstr;
4230 tok_str_add2(tok_str, t1, &cval);
4231 cstr_free(&cstr);
4232 } else {
4233 mstr = (int *)s->c;
4234 mstr_allocated = 0;
4235 if (s->type.t == MACRO_FUNC) {
4236 /* NOTE: we do not use next_nomacro to avoid eating the
4237 next token. XXX: find better solution */
4238 redo:
4239 if (macro_ptr) {
4240 t = *macro_ptr;
4241 if (t == 0 && can_read_stream) {
4242 /* end of macro stream: we must look at the token
4243 after in the file */
4244 struct macro_level *ml = *can_read_stream;
4245 macro_ptr = NULL;
4246 if (ml)
4248 macro_ptr = ml->p;
4249 ml->p = NULL;
4250 *can_read_stream = ml -> prev;
4252 goto redo;
4254 } else {
4255 /* XXX: incorrect with comments */
4256 ch = file->buf_ptr[0];
4257 while (is_space(ch) || ch == '\n')
4258 cinp();
4259 t = ch;
4261 if (t != '(') /* no macro subst */
4262 return -1;
4264 /* argument macro */
4265 next_nomacro();
4266 next_nomacro();
4267 args = NULL;
4268 sa = s->next;
4269 /* NOTE: empty args are allowed, except if no args */
4270 for(;;) {
4271 /* handle '()' case */
4272 if (!args && !sa && tok == ')')
4273 break;
4274 if (!sa)
4275 error("macro '%s' used with too many args",
4276 get_tok_str(s->v, 0));
4277 tok_str_new(&str);
4278 parlevel = 0;
4279 /* NOTE: non zero sa->t indicates VA_ARGS */
4280 while ((parlevel > 0 ||
4281 (tok != ')' &&
4282 (tok != ',' || sa->type.t))) &&
4283 tok != -1) {
4284 if (tok == '(')
4285 parlevel++;
4286 else if (tok == ')')
4287 parlevel--;
4288 if (tok != TOK_LINEFEED)
4289 tok_str_add2(&str, tok, &tokc);
4290 next_nomacro();
4292 tok_str_add(&str, 0);
4293 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
4294 sa = sa->next;
4295 if (tok == ')') {
4296 /* special case for gcc var args: add an empty
4297 var arg argument if it is omitted */
4298 if (sa && sa->type.t && gnu_ext)
4299 continue;
4300 else
4301 break;
4303 if (tok != ',')
4304 expect(",");
4305 next_nomacro();
4307 if (sa) {
4308 error("macro '%s' used with too few args",
4309 get_tok_str(s->v, 0));
4312 /* now subst each arg */
4313 mstr = macro_arg_subst(nested_list, mstr, args);
4314 /* free memory */
4315 sa = args;
4316 while (sa) {
4317 sa1 = sa->prev;
4318 tok_str_free((int *)sa->c);
4319 sym_free(sa);
4320 sa = sa1;
4322 mstr_allocated = 1;
4324 sym_push2(nested_list, s->v, 0, 0);
4325 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4326 /* pop nested defined symbol */
4327 sa1 = *nested_list;
4328 *nested_list = sa1->prev;
4329 sym_free(sa1);
4330 if (mstr_allocated)
4331 tok_str_free(mstr);
4333 return 0;
4336 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4337 return the resulting string (which must be freed). */
4338 static inline int *macro_twosharps(const int *macro_str)
4340 TokenSym *ts;
4341 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4342 int t;
4343 const char *p1, *p2;
4344 CValue cval;
4345 TokenString macro_str1;
4346 CString cstr;
4348 start_macro_ptr = macro_str;
4349 /* we search the first '##' */
4350 for(;;) {
4351 macro_ptr1 = macro_str;
4352 TOK_GET(t, macro_str, cval);
4353 /* nothing more to do if end of string */
4354 if (t == 0)
4355 return NULL;
4356 if (*macro_str == TOK_TWOSHARPS)
4357 break;
4360 /* we saw '##', so we need more processing to handle it */
4361 cstr_new(&cstr);
4362 tok_str_new(&macro_str1);
4363 tok = t;
4364 tokc = cval;
4366 /* add all tokens seen so far */
4367 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4368 TOK_GET(t, ptr, cval);
4369 tok_str_add2(&macro_str1, t, &cval);
4371 saved_macro_ptr = macro_ptr;
4372 /* XXX: get rid of the use of macro_ptr here */
4373 macro_ptr = (int *)macro_str;
4374 for(;;) {
4375 while (*macro_ptr == TOK_TWOSHARPS) {
4376 macro_ptr++;
4377 macro_ptr1 = macro_ptr;
4378 t = *macro_ptr;
4379 if (t) {
4380 TOK_GET(t, macro_ptr, cval);
4381 /* We concatenate the two tokens if we have an
4382 identifier or a preprocessing number */
4383 cstr_reset(&cstr);
4384 p1 = get_tok_str(tok, &tokc);
4385 cstr_cat(&cstr, p1);
4386 p2 = get_tok_str(t, &cval);
4387 cstr_cat(&cstr, p2);
4388 cstr_ccat(&cstr, '\0');
4390 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4391 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4392 if (tok == TOK_PPNUM) {
4393 /* if number, then create a number token */
4394 /* NOTE: no need to allocate because
4395 tok_str_add2() does it */
4396 cstr_reset(&tokcstr);
4397 tokcstr = cstr;
4398 cstr_new(&cstr);
4399 tokc.cstr = &tokcstr;
4400 } else {
4401 /* if identifier, we must do a test to
4402 validate we have a correct identifier */
4403 if (t == TOK_PPNUM) {
4404 const char *p;
4405 int c;
4407 p = p2;
4408 for(;;) {
4409 c = *p;
4410 if (c == '\0')
4411 break;
4412 p++;
4413 if (!isnum(c) && !isid(c))
4414 goto error_pasting;
4417 ts = tok_alloc(cstr.data, strlen(cstr.data));
4418 tok = ts->tok; /* modify current token */
4420 } else {
4421 const char *str = cstr.data;
4422 const unsigned char *q;
4424 /* we look for a valid token */
4425 /* XXX: do more extensive checks */
4426 if (!strcmp(str, ">>=")) {
4427 tok = TOK_A_SAR;
4428 } else if (!strcmp(str, "<<=")) {
4429 tok = TOK_A_SHL;
4430 } else if (strlen(str) == 2) {
4431 /* search in two bytes table */
4432 q = tok_two_chars;
4433 for(;;) {
4434 if (!*q)
4435 goto error_pasting;
4436 if (q[0] == str[0] && q[1] == str[1])
4437 break;
4438 q += 3;
4440 tok = q[2];
4441 } else {
4442 error_pasting:
4443 /* NOTE: because get_tok_str use a static buffer,
4444 we must save it */
4445 cstr_reset(&cstr);
4446 p1 = get_tok_str(tok, &tokc);
4447 cstr_cat(&cstr, p1);
4448 cstr_ccat(&cstr, '\0');
4449 p2 = get_tok_str(t, &cval);
4450 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4451 /* cannot merge tokens: just add them separately */
4452 tok_str_add2(&macro_str1, tok, &tokc);
4453 /* XXX: free associated memory ? */
4454 tok = t;
4455 tokc = cval;
4460 tok_str_add2(&macro_str1, tok, &tokc);
4461 next_nomacro();
4462 if (tok == 0)
4463 break;
4465 macro_ptr = (int *)saved_macro_ptr;
4466 cstr_free(&cstr);
4467 tok_str_add(&macro_str1, 0);
4468 return macro_str1.str;
4472 /* do macro substitution of macro_str and add result to
4473 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4474 inside to avoid recursing. */
4475 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4476 const int *macro_str, struct macro_level ** can_read_stream)
4478 Sym *s;
4479 int *macro_str1;
4480 const int *ptr;
4481 int t, ret;
4482 CValue cval;
4483 struct macro_level ml;
4485 /* first scan for '##' operator handling */
4486 ptr = macro_str;
4487 macro_str1 = macro_twosharps(ptr);
4488 if (macro_str1)
4489 ptr = macro_str1;
4490 while (1) {
4491 /* NOTE: ptr == NULL can only happen if tokens are read from
4492 file stream due to a macro function call */
4493 if (ptr == NULL)
4494 break;
4495 TOK_GET(t, ptr, cval);
4496 if (t == 0)
4497 break;
4498 s = define_find(t);
4499 if (s != NULL) {
4500 /* if nested substitution, do nothing */
4501 if (sym_find2(*nested_list, t))
4502 goto no_subst;
4503 ml.p = macro_ptr;
4504 if (can_read_stream)
4505 ml.prev = *can_read_stream, *can_read_stream = &ml;
4506 macro_ptr = (int *)ptr;
4507 tok = t;
4508 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4509 ptr = (int *)macro_ptr;
4510 macro_ptr = ml.p;
4511 if (can_read_stream && *can_read_stream == &ml)
4512 *can_read_stream = ml.prev;
4513 if (ret != 0)
4514 goto no_subst;
4515 } else {
4516 no_subst:
4517 tok_str_add2(tok_str, t, &cval);
4520 if (macro_str1)
4521 tok_str_free(macro_str1);
4524 /* return next token with macro substitution */
4525 static void next(void)
4527 Sym *nested_list, *s;
4528 TokenString str;
4529 struct macro_level *ml;
4531 redo:
4532 next_nomacro();
4533 if (!macro_ptr) {
4534 /* if not reading from macro substituted string, then try
4535 to substitute macros */
4536 if (tok >= TOK_IDENT &&
4537 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4538 s = define_find(tok);
4539 if (s) {
4540 /* we have a macro: we try to substitute */
4541 tok_str_new(&str);
4542 nested_list = NULL;
4543 ml = NULL;
4544 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4545 /* substitution done, NOTE: maybe empty */
4546 tok_str_add(&str, 0);
4547 macro_ptr = str.str;
4548 macro_ptr_allocated = str.str;
4549 goto redo;
4553 } else {
4554 if (tok == 0) {
4555 /* end of macro or end of unget buffer */
4556 if (unget_buffer_enabled) {
4557 macro_ptr = unget_saved_macro_ptr;
4558 unget_buffer_enabled = 0;
4559 } else {
4560 /* end of macro string: free it */
4561 tok_str_free(macro_ptr_allocated);
4562 macro_ptr = NULL;
4564 goto redo;
4568 /* convert preprocessor tokens into C tokens */
4569 if (tok == TOK_PPNUM &&
4570 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4571 parse_number((char *)tokc.cstr->data);
4575 /* push back current token and set current token to 'last_tok'. Only
4576 identifier case handled for labels. */
4577 static inline void unget_tok(int last_tok)
4579 int i, n;
4580 int *q;
4581 unget_saved_macro_ptr = macro_ptr;
4582 unget_buffer_enabled = 1;
4583 q = unget_saved_buffer;
4584 macro_ptr = q;
4585 *q++ = tok;
4586 n = tok_ext_size(tok) - 1;
4587 for(i=0;i<n;i++)
4588 *q++ = tokc.tab[i];
4589 *q = 0; /* end of token string */
4590 tok = last_tok;
4594 void swap(int *p, int *q)
4596 int t;
4597 t = *p;
4598 *p = *q;
4599 *q = t;
4602 void vsetc(CType *type, int r, CValue *vc)
4604 int v;
4606 if (vtop >= vstack + (VSTACK_SIZE - 1))
4607 error("memory full");
4608 /* cannot let cpu flags if other instruction are generated. Also
4609 avoid leaving VT_JMP anywhere except on the top of the stack
4610 because it would complicate the code generator. */
4611 if (vtop >= vstack) {
4612 v = vtop->r & VT_VALMASK;
4613 if (v == VT_CMP || (v & ~1) == VT_JMP)
4614 gv(RC_INT);
4616 vtop++;
4617 vtop->type = *type;
4618 vtop->r = r;
4619 vtop->r2 = VT_CONST;
4620 vtop->c = *vc;
4623 /* push integer constant */
4624 void vpushi(int v)
4626 CValue cval;
4627 cval.i = v;
4628 vsetc(&int_type, VT_CONST, &cval);
4631 /* Return a static symbol pointing to a section */
4632 static Sym *get_sym_ref(CType *type, Section *sec,
4633 unsigned long offset, unsigned long size)
4635 int v;
4636 Sym *sym;
4638 v = anon_sym++;
4639 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4640 sym->type.ref = type->ref;
4641 sym->r = VT_CONST | VT_SYM;
4642 put_extern_sym(sym, sec, offset, size);
4643 return sym;
4646 /* push a reference to a section offset by adding a dummy symbol */
4647 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4649 CValue cval;
4651 cval.ul = 0;
4652 vsetc(type, VT_CONST | VT_SYM, &cval);
4653 vtop->sym = get_sym_ref(type, sec, offset, size);
4656 /* define a new external reference to a symbol 'v' of type 'u' */
4657 static Sym *external_global_sym(int v, CType *type, int r)
4659 Sym *s;
4661 s = sym_find(v);
4662 if (!s) {
4663 /* push forward reference */
4664 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4665 s->type.ref = type->ref;
4666 s->r = r | VT_CONST | VT_SYM;
4668 return s;
4671 /* define a new external reference to a symbol 'v' of type 'u' */
4672 static Sym *external_sym(int v, CType *type, int r)
4674 Sym *s;
4676 s = sym_find(v);
4677 if (!s) {
4678 /* push forward reference */
4679 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4680 s->type.t |= VT_EXTERN;
4681 } else {
4682 if (!is_compatible_types(&s->type, type))
4683 error("incompatible types for redefinition of '%s'",
4684 get_tok_str(v, NULL));
4686 return s;
4689 /* push a reference to global symbol v */
4690 static void vpush_global_sym(CType *type, int v)
4692 Sym *sym;
4693 CValue cval;
4695 sym = external_global_sym(v, type, 0);
4696 cval.ul = 0;
4697 vsetc(type, VT_CONST | VT_SYM, &cval);
4698 vtop->sym = sym;
4701 void vset(CType *type, int r, int v)
4703 CValue cval;
4705 cval.i = v;
4706 vsetc(type, r, &cval);
4709 void vseti(int r, int v)
4711 CType type;
4712 type.t = VT_INT;
4713 vset(&type, r, v);
4716 void vswap(void)
4718 SValue tmp;
4720 tmp = vtop[0];
4721 vtop[0] = vtop[-1];
4722 vtop[-1] = tmp;
4725 void vpushv(SValue *v)
4727 if (vtop >= vstack + (VSTACK_SIZE - 1))
4728 error("memory full");
4729 vtop++;
4730 *vtop = *v;
4733 void vdup(void)
4735 vpushv(vtop);
4738 /* save r to the memory stack, and mark it as being free */
4739 void save_reg(int r)
4741 int l, saved, size, align;
4742 SValue *p, sv;
4743 CType *type;
4745 /* modify all stack values */
4746 saved = 0;
4747 l = 0;
4748 for(p=vstack;p<=vtop;p++) {
4749 if ((p->r & VT_VALMASK) == r ||
4750 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4751 /* must save value on stack if not already done */
4752 if (!saved) {
4753 /* NOTE: must reload 'r' because r might be equal to r2 */
4754 r = p->r & VT_VALMASK;
4755 /* store register in the stack */
4756 type = &p->type;
4757 if ((p->r & VT_LVAL) ||
4758 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4759 type = &int_type;
4760 size = type_size(type, &align);
4761 loc = (loc - size) & -align;
4762 sv.type.t = type->t;
4763 sv.r = VT_LOCAL | VT_LVAL;
4764 sv.c.ul = loc;
4765 store(r, &sv);
4766 #ifdef TCC_TARGET_I386
4767 /* x86 specific: need to pop fp register ST0 if saved */
4768 if (r == TREG_ST0) {
4769 o(0xd9dd); /* fstp %st(1) */
4771 #endif
4772 /* special long long case */
4773 if ((type->t & VT_BTYPE) == VT_LLONG) {
4774 sv.c.ul += 4;
4775 store(p->r2, &sv);
4777 l = loc;
4778 saved = 1;
4780 /* mark that stack entry as being saved on the stack */
4781 if (p->r & VT_LVAL) {
4782 /* also clear the bounded flag because the
4783 relocation address of the function was stored in
4784 p->c.ul */
4785 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4786 } else {
4787 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4789 p->r2 = VT_CONST;
4790 p->c.ul = l;
4795 /* find a register of class 'rc2' with at most one reference on stack.
4796 * If none, call get_reg(rc) */
4797 int get_reg_ex(int rc, int rc2)
4799 int r;
4800 SValue *p;
4802 for(r=0;r<NB_REGS;r++) {
4803 if (reg_classes[r] & rc2) {
4804 int n;
4805 n=0;
4806 for(p = vstack; p <= vtop; p++) {
4807 if ((p->r & VT_VALMASK) == r ||
4808 (p->r2 & VT_VALMASK) == r)
4809 n++;
4811 if (n <= 1)
4812 return r;
4815 return get_reg(rc);
4818 /* find a free register of class 'rc'. If none, save one register */
4819 int get_reg(int rc)
4821 int r;
4822 SValue *p;
4824 /* find a free register */
4825 for(r=0;r<NB_REGS;r++) {
4826 if (reg_classes[r] & rc) {
4827 for(p=vstack;p<=vtop;p++) {
4828 if ((p->r & VT_VALMASK) == r ||
4829 (p->r2 & VT_VALMASK) == r)
4830 goto notfound;
4832 return r;
4834 notfound: ;
4837 /* no register left : free the first one on the stack (VERY
4838 IMPORTANT to start from the bottom to ensure that we don't
4839 spill registers used in gen_opi()) */
4840 for(p=vstack;p<=vtop;p++) {
4841 r = p->r & VT_VALMASK;
4842 if (r < VT_CONST && (reg_classes[r] & rc))
4843 goto save_found;
4844 /* also look at second register (if long long) */
4845 r = p->r2 & VT_VALMASK;
4846 if (r < VT_CONST && (reg_classes[r] & rc)) {
4847 save_found:
4848 save_reg(r);
4849 return r;
4852 /* Should never comes here */
4853 return -1;
4856 /* save registers up to (vtop - n) stack entry */
4857 void save_regs(int n)
4859 int r;
4860 SValue *p, *p1;
4861 p1 = vtop - n;
4862 for(p = vstack;p <= p1; p++) {
4863 r = p->r & VT_VALMASK;
4864 if (r < VT_CONST) {
4865 save_reg(r);
4870 /* move register 's' to 'r', and flush previous value of r to memory
4871 if needed */
4872 void move_reg(int r, int s)
4874 SValue sv;
4876 if (r != s) {
4877 save_reg(r);
4878 sv.type.t = VT_INT;
4879 sv.r = s;
4880 sv.c.ul = 0;
4881 load(r, &sv);
4885 /* get address of vtop (vtop MUST BE an lvalue) */
4886 void gaddrof(void)
4888 vtop->r &= ~VT_LVAL;
4889 /* tricky: if saved lvalue, then we can go back to lvalue */
4890 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4891 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4894 #ifdef CONFIG_TCC_BCHECK
4895 /* generate lvalue bound code */
4896 void gbound(void)
4898 int lval_type;
4899 CType type1;
4901 vtop->r &= ~VT_MUSTBOUND;
4902 /* if lvalue, then use checking code before dereferencing */
4903 if (vtop->r & VT_LVAL) {
4904 /* if not VT_BOUNDED value, then make one */
4905 if (!(vtop->r & VT_BOUNDED)) {
4906 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4907 /* must save type because we must set it to int to get pointer */
4908 type1 = vtop->type;
4909 vtop->type.t = VT_INT;
4910 gaddrof();
4911 vpushi(0);
4912 gen_bounded_ptr_add();
4913 vtop->r |= lval_type;
4914 vtop->type = type1;
4916 /* then check for dereferencing */
4917 gen_bounded_ptr_deref();
4920 #endif
4922 /* store vtop a register belonging to class 'rc'. lvalues are
4923 converted to values. Cannot be used if cannot be converted to
4924 register value (such as structures). */
4925 int gv(int rc)
4927 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4928 unsigned long long ll;
4930 /* NOTE: get_reg can modify vstack[] */
4931 if (vtop->type.t & VT_BITFIELD) {
4932 CType type;
4933 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4934 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4935 /* remove bit field info to avoid loops */
4936 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4937 /* cast to int to propagate signedness in following ops */
4938 type.t = VT_INT;
4939 if((vtop->type.t & VT_UNSIGNED) ||
4940 (vtop->type.t & VT_BTYPE) == VT_BOOL)
4941 type.t |= VT_UNSIGNED;
4942 gen_cast(&type);
4943 /* generate shifts */
4944 vpushi(32 - (bit_pos + bit_size));
4945 gen_op(TOK_SHL);
4946 vpushi(32 - bit_size);
4947 /* NOTE: transformed to SHR if unsigned */
4948 gen_op(TOK_SAR);
4949 r = gv(rc);
4950 } else {
4951 if (is_float(vtop->type.t) &&
4952 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4953 Sym *sym;
4954 int *ptr;
4955 unsigned long offset;
4956 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4957 CValue check;
4958 #endif
4960 /* XXX: unify with initializers handling ? */
4961 /* CPUs usually cannot use float constants, so we store them
4962 generically in data segment */
4963 size = type_size(&vtop->type, &align);
4964 offset = (data_section->data_offset + align - 1) & -align;
4965 data_section->data_offset = offset;
4966 /* XXX: not portable yet */
4967 #ifdef __i386__
4968 /* Zero pad x87 tenbyte long doubles */
4969 if (size == 12)
4970 vtop->c.tab[2] &= 0xffff;
4971 #endif
4972 ptr = section_ptr_add(data_section, size);
4973 size = size >> 2;
4974 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4975 check.d = 1;
4976 if(check.tab[0])
4977 for(i=0;i<size;i++)
4978 ptr[i] = vtop->c.tab[size-1-i];
4979 else
4980 #endif
4981 for(i=0;i<size;i++)
4982 ptr[i] = vtop->c.tab[i];
4983 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4984 vtop->r |= VT_LVAL | VT_SYM;
4985 vtop->sym = sym;
4986 vtop->c.ul = 0;
4988 #ifdef CONFIG_TCC_BCHECK
4989 if (vtop->r & VT_MUSTBOUND)
4990 gbound();
4991 #endif
4993 r = vtop->r & VT_VALMASK;
4994 rc2 = RC_INT;
4995 if (rc == RC_IRET)
4996 rc2 = RC_LRET;
4997 /* need to reload if:
4998 - constant
4999 - lvalue (need to dereference pointer)
5000 - already a register, but not in the right class */
5001 if (r >= VT_CONST ||
5002 (vtop->r & VT_LVAL) ||
5003 !(reg_classes[r] & rc) ||
5004 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
5005 !(reg_classes[vtop->r2] & rc2))) {
5006 r = get_reg(rc);
5007 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
5008 /* two register type load : expand to two words
5009 temporarily */
5010 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
5011 /* load constant */
5012 ll = vtop->c.ull;
5013 vtop->c.ui = ll; /* first word */
5014 load(r, vtop);
5015 vtop->r = r; /* save register value */
5016 vpushi(ll >> 32); /* second word */
5017 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
5018 (vtop->r & VT_LVAL)) {
5019 /* We do not want to modifier the long long
5020 pointer here, so the safest (and less
5021 efficient) is to save all the other registers
5022 in the stack. XXX: totally inefficient. */
5023 save_regs(1);
5024 /* load from memory */
5025 load(r, vtop);
5026 vdup();
5027 vtop[-1].r = r; /* save register value */
5028 /* increment pointer to get second word */
5029 vtop->type.t = VT_INT;
5030 gaddrof();
5031 vpushi(4);
5032 gen_op('+');
5033 vtop->r |= VT_LVAL;
5034 } else {
5035 /* move registers */
5036 load(r, vtop);
5037 vdup();
5038 vtop[-1].r = r; /* save register value */
5039 vtop->r = vtop[-1].r2;
5041 /* allocate second register */
5042 r2 = get_reg(rc2);
5043 load(r2, vtop);
5044 vpop();
5045 /* write second register */
5046 vtop->r2 = r2;
5047 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5048 int t1, t;
5049 /* lvalue of scalar type : need to use lvalue type
5050 because of possible cast */
5051 t = vtop->type.t;
5052 t1 = t;
5053 /* compute memory access type */
5054 if (vtop->r & VT_LVAL_BYTE)
5055 t = VT_BYTE;
5056 else if (vtop->r & VT_LVAL_SHORT)
5057 t = VT_SHORT;
5058 if (vtop->r & VT_LVAL_UNSIGNED)
5059 t |= VT_UNSIGNED;
5060 vtop->type.t = t;
5061 load(r, vtop);
5062 /* restore wanted type */
5063 vtop->type.t = t1;
5064 } else {
5065 /* one register type load */
5066 load(r, vtop);
5069 vtop->r = r;
5070 #ifdef TCC_TARGET_C67
5071 /* uses register pairs for doubles */
5072 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5073 vtop->r2 = r+1;
5074 #endif
5076 return r;
5079 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5080 void gv2(int rc1, int rc2)
5082 int v;
5084 /* generate more generic register first. But VT_JMP or VT_CMP
5085 values must be generated first in all cases to avoid possible
5086 reload errors */
5087 v = vtop[0].r & VT_VALMASK;
5088 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5089 vswap();
5090 gv(rc1);
5091 vswap();
5092 gv(rc2);
5093 /* test if reload is needed for first register */
5094 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5095 vswap();
5096 gv(rc1);
5097 vswap();
5099 } else {
5100 gv(rc2);
5101 vswap();
5102 gv(rc1);
5103 vswap();
5104 /* test if reload is needed for first register */
5105 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5106 gv(rc2);
5111 /* expand long long on stack in two int registers */
5112 void lexpand(void)
5114 int u;
5116 u = vtop->type.t & VT_UNSIGNED;
5117 gv(RC_INT);
5118 vdup();
5119 vtop[0].r = vtop[-1].r2;
5120 vtop[0].r2 = VT_CONST;
5121 vtop[-1].r2 = VT_CONST;
5122 vtop[0].type.t = VT_INT | u;
5123 vtop[-1].type.t = VT_INT | u;
5126 #ifdef TCC_TARGET_ARM
5127 /* expand long long on stack */
5128 void lexpand_nr(void)
5130 int u,v;
5132 u = vtop->type.t & VT_UNSIGNED;
5133 vdup();
5134 vtop->r2 = VT_CONST;
5135 vtop->type.t = VT_INT | u;
5136 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5137 if (v == VT_CONST) {
5138 vtop[-1].c.ui = vtop->c.ull;
5139 vtop->c.ui = vtop->c.ull >> 32;
5140 vtop->r = VT_CONST;
5141 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5142 vtop->c.ui += 4;
5143 vtop->r = vtop[-1].r;
5144 } else if (v > VT_CONST) {
5145 vtop--;
5146 lexpand();
5147 } else
5148 vtop->r = vtop[-1].r2;
5149 vtop[-1].r2 = VT_CONST;
5150 vtop[-1].type.t = VT_INT | u;
5152 #endif
5154 /* build a long long from two ints */
5155 void lbuild(int t)
5157 gv2(RC_INT, RC_INT);
5158 vtop[-1].r2 = vtop[0].r;
5159 vtop[-1].type.t = t;
5160 vpop();
5163 /* rotate n first stack elements to the bottom
5164 I1 ... In -> I2 ... In I1 [top is right]
5166 void vrotb(int n)
5168 int i;
5169 SValue tmp;
5171 tmp = vtop[-n + 1];
5172 for(i=-n+1;i!=0;i++)
5173 vtop[i] = vtop[i+1];
5174 vtop[0] = tmp;
5177 /* rotate n first stack elements to the top
5178 I1 ... In -> In I1 ... I(n-1) [top is right]
5180 void vrott(int n)
5182 int i;
5183 SValue tmp;
5185 tmp = vtop[0];
5186 for(i = 0;i < n - 1; i++)
5187 vtop[-i] = vtop[-i - 1];
5188 vtop[-n + 1] = tmp;
5191 #ifdef TCC_TARGET_ARM
5192 /* like vrott but in other direction
5193 In ... I1 -> I(n-1) ... I1 In [top is right]
5195 void vnrott(int n)
5197 int i;
5198 SValue tmp;
5200 tmp = vtop[-n + 1];
5201 for(i = n - 1; i > 0; i--)
5202 vtop[-i] = vtop[-i + 1];
5203 vtop[0] = tmp;
5205 #endif
5207 /* pop stack value */
5208 void vpop(void)
5210 int v;
5211 v = vtop->r & VT_VALMASK;
5212 #ifdef TCC_TARGET_I386
5213 /* for x86, we need to pop the FP stack */
5214 if (v == TREG_ST0 && !nocode_wanted) {
5215 o(0xd9dd); /* fstp %st(1) */
5216 } else
5217 #endif
5218 if (v == VT_JMP || v == VT_JMPI) {
5219 /* need to put correct jump if && or || without test */
5220 gsym(vtop->c.ul);
5222 vtop--;
5225 /* convert stack entry to register and duplicate its value in another
5226 register */
5227 void gv_dup(void)
5229 int rc, t, r, r1;
5230 SValue sv;
5232 t = vtop->type.t;
5233 if ((t & VT_BTYPE) == VT_LLONG) {
5234 lexpand();
5235 gv_dup();
5236 vswap();
5237 vrotb(3);
5238 gv_dup();
5239 vrotb(4);
5240 /* stack: H L L1 H1 */
5241 lbuild(t);
5242 vrotb(3);
5243 vrotb(3);
5244 vswap();
5245 lbuild(t);
5246 vswap();
5247 } else {
5248 /* duplicate value */
5249 rc = RC_INT;
5250 sv.type.t = VT_INT;
5251 if (is_float(t)) {
5252 rc = RC_FLOAT;
5253 sv.type.t = t;
5255 r = gv(rc);
5256 r1 = get_reg(rc);
5257 sv.r = r;
5258 sv.c.ul = 0;
5259 load(r1, &sv); /* move r to r1 */
5260 vdup();
5261 /* duplicates value */
5262 vtop->r = r1;
5266 /* generate CPU independent (unsigned) long long operations */
5267 void gen_opl(int op)
5269 int t, a, b, op1, c, i;
5270 int func;
5271 unsigned short reg_iret = REG_IRET;
5272 unsigned short reg_lret = REG_LRET;
5273 SValue tmp;
5275 switch(op) {
5276 case '/':
5277 case TOK_PDIV:
5278 func = TOK___divdi3;
5279 goto gen_func;
5280 case TOK_UDIV:
5281 func = TOK___udivdi3;
5282 goto gen_func;
5283 case '%':
5284 func = TOK___moddi3;
5285 goto gen_mod_func;
5286 case TOK_UMOD:
5287 func = TOK___umoddi3;
5288 gen_mod_func:
5289 #ifdef TCC_ARM_EABI
5290 reg_iret = TREG_R2;
5291 reg_lret = TREG_R3;
5292 #endif
5293 gen_func:
5294 /* call generic long long function */
5295 vpush_global_sym(&func_old_type, func);
5296 vrott(3);
5297 gfunc_call(2);
5298 vpushi(0);
5299 vtop->r = reg_iret;
5300 vtop->r2 = reg_lret;
5301 break;
5302 case '^':
5303 case '&':
5304 case '|':
5305 case '*':
5306 case '+':
5307 case '-':
5308 t = vtop->type.t;
5309 vswap();
5310 lexpand();
5311 vrotb(3);
5312 lexpand();
5313 /* stack: L1 H1 L2 H2 */
5314 tmp = vtop[0];
5315 vtop[0] = vtop[-3];
5316 vtop[-3] = tmp;
5317 tmp = vtop[-2];
5318 vtop[-2] = vtop[-3];
5319 vtop[-3] = tmp;
5320 vswap();
5321 /* stack: H1 H2 L1 L2 */
5322 if (op == '*') {
5323 vpushv(vtop - 1);
5324 vpushv(vtop - 1);
5325 gen_op(TOK_UMULL);
5326 lexpand();
5327 /* stack: H1 H2 L1 L2 ML MH */
5328 for(i=0;i<4;i++)
5329 vrotb(6);
5330 /* stack: ML MH H1 H2 L1 L2 */
5331 tmp = vtop[0];
5332 vtop[0] = vtop[-2];
5333 vtop[-2] = tmp;
5334 /* stack: ML MH H1 L2 H2 L1 */
5335 gen_op('*');
5336 vrotb(3);
5337 vrotb(3);
5338 gen_op('*');
5339 /* stack: ML MH M1 M2 */
5340 gen_op('+');
5341 gen_op('+');
5342 } else if (op == '+' || op == '-') {
5343 /* XXX: add non carry method too (for MIPS or alpha) */
5344 if (op == '+')
5345 op1 = TOK_ADDC1;
5346 else
5347 op1 = TOK_SUBC1;
5348 gen_op(op1);
5349 /* stack: H1 H2 (L1 op L2) */
5350 vrotb(3);
5351 vrotb(3);
5352 gen_op(op1 + 1); /* TOK_xxxC2 */
5353 } else {
5354 gen_op(op);
5355 /* stack: H1 H2 (L1 op L2) */
5356 vrotb(3);
5357 vrotb(3);
5358 /* stack: (L1 op L2) H1 H2 */
5359 gen_op(op);
5360 /* stack: (L1 op L2) (H1 op H2) */
5362 /* stack: L H */
5363 lbuild(t);
5364 break;
5365 case TOK_SAR:
5366 case TOK_SHR:
5367 case TOK_SHL:
5368 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5369 t = vtop[-1].type.t;
5370 vswap();
5371 lexpand();
5372 vrotb(3);
5373 /* stack: L H shift */
5374 c = (int)vtop->c.i;
5375 /* constant: simpler */
5376 /* NOTE: all comments are for SHL. the other cases are
5377 done by swaping words */
5378 vpop();
5379 if (op != TOK_SHL)
5380 vswap();
5381 if (c >= 32) {
5382 /* stack: L H */
5383 vpop();
5384 if (c > 32) {
5385 vpushi(c - 32);
5386 gen_op(op);
5388 if (op != TOK_SAR) {
5389 vpushi(0);
5390 } else {
5391 gv_dup();
5392 vpushi(31);
5393 gen_op(TOK_SAR);
5395 vswap();
5396 } else {
5397 vswap();
5398 gv_dup();
5399 /* stack: H L L */
5400 vpushi(c);
5401 gen_op(op);
5402 vswap();
5403 vpushi(32 - c);
5404 if (op == TOK_SHL)
5405 gen_op(TOK_SHR);
5406 else
5407 gen_op(TOK_SHL);
5408 vrotb(3);
5409 /* stack: L L H */
5410 vpushi(c);
5411 if (op == TOK_SHL)
5412 gen_op(TOK_SHL);
5413 else
5414 gen_op(TOK_SHR);
5415 gen_op('|');
5417 if (op != TOK_SHL)
5418 vswap();
5419 lbuild(t);
5420 } else {
5421 /* XXX: should provide a faster fallback on x86 ? */
5422 switch(op) {
5423 case TOK_SAR:
5424 func = TOK___ashrdi3;
5425 goto gen_func;
5426 case TOK_SHR:
5427 func = TOK___lshrdi3;
5428 goto gen_func;
5429 case TOK_SHL:
5430 func = TOK___ashldi3;
5431 goto gen_func;
5434 break;
5435 default:
5436 /* compare operations */
5437 t = vtop->type.t;
5438 vswap();
5439 lexpand();
5440 vrotb(3);
5441 lexpand();
5442 /* stack: L1 H1 L2 H2 */
5443 tmp = vtop[-1];
5444 vtop[-1] = vtop[-2];
5445 vtop[-2] = tmp;
5446 /* stack: L1 L2 H1 H2 */
5447 /* compare high */
5448 op1 = op;
5449 /* when values are equal, we need to compare low words. since
5450 the jump is inverted, we invert the test too. */
5451 if (op1 == TOK_LT)
5452 op1 = TOK_LE;
5453 else if (op1 == TOK_GT)
5454 op1 = TOK_GE;
5455 else if (op1 == TOK_ULT)
5456 op1 = TOK_ULE;
5457 else if (op1 == TOK_UGT)
5458 op1 = TOK_UGE;
5459 a = 0;
5460 b = 0;
5461 gen_op(op1);
5462 if (op1 != TOK_NE) {
5463 a = gtst(1, 0);
5465 if (op != TOK_EQ) {
5466 /* generate non equal test */
5467 /* XXX: NOT PORTABLE yet */
5468 if (a == 0) {
5469 b = gtst(0, 0);
5470 } else {
5471 #if defined(TCC_TARGET_I386)
5472 b = psym(0x850f, 0);
5473 #elif defined(TCC_TARGET_ARM)
5474 b = ind;
5475 o(0x1A000000 | encbranch(ind, 0, 1));
5476 #elif defined(TCC_TARGET_C67)
5477 error("not implemented");
5478 #else
5479 #error not supported
5480 #endif
5483 /* compare low. Always unsigned */
5484 op1 = op;
5485 if (op1 == TOK_LT)
5486 op1 = TOK_ULT;
5487 else if (op1 == TOK_LE)
5488 op1 = TOK_ULE;
5489 else if (op1 == TOK_GT)
5490 op1 = TOK_UGT;
5491 else if (op1 == TOK_GE)
5492 op1 = TOK_UGE;
5493 gen_op(op1);
5494 a = gtst(1, a);
5495 gsym(b);
5496 vseti(VT_JMPI, a);
5497 break;
5501 /* handle integer constant optimizations and various machine
5502 independent opt */
5503 void gen_opic(int op)
5505 int c1, c2, t1, t2, n;
5506 SValue *v1, *v2;
5507 long long l1, l2;
5508 typedef unsigned long long U;
5510 v1 = vtop - 1;
5511 v2 = vtop;
5512 t1 = v1->type.t & VT_BTYPE;
5513 t2 = v2->type.t & VT_BTYPE;
5514 l1 = (t1 == VT_LLONG) ? v1->c.ll : v1->c.i;
5515 l2 = (t2 == VT_LLONG) ? v2->c.ll : v2->c.i;
5517 /* currently, we cannot do computations with forward symbols */
5518 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5519 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5520 if (c1 && c2) {
5521 switch(op) {
5522 case '+': l1 += l2; break;
5523 case '-': l1 -= l2; break;
5524 case '&': l1 &= l2; break;
5525 case '^': l1 ^= l2; break;
5526 case '|': l1 |= l2; break;
5527 case '*': l1 *= l2; break;
5529 case TOK_PDIV:
5530 case '/':
5531 case '%':
5532 case TOK_UDIV:
5533 case TOK_UMOD:
5534 /* if division by zero, generate explicit division */
5535 if (l2 == 0) {
5536 if (const_wanted)
5537 error("division by zero in constant");
5538 goto general_case;
5540 switch(op) {
5541 default: l1 /= l2; break;
5542 case '%': l1 %= l2; break;
5543 case TOK_UDIV: l1 = (U)l1 / l2; break;
5544 case TOK_UMOD: l1 = (U)l1 % l2; break;
5546 break;
5547 case TOK_SHL: l1 <<= l2; break;
5548 case TOK_SHR: l1 = (U)l1 >> l2; break;
5549 case TOK_SAR: l1 >>= l2; break;
5550 /* tests */
5551 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5552 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5553 case TOK_EQ: l1 = l1 == l2; break;
5554 case TOK_NE: l1 = l1 != l2; break;
5555 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5556 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5557 case TOK_LT: l1 = l1 < l2; break;
5558 case TOK_GE: l1 = l1 >= l2; break;
5559 case TOK_LE: l1 = l1 <= l2; break;
5560 case TOK_GT: l1 = l1 > l2; break;
5561 /* logical */
5562 case TOK_LAND: l1 = l1 && l2; break;
5563 case TOK_LOR: l1 = l1 || l2; break;
5564 default:
5565 goto general_case;
5567 v1->c.ll = l1;
5568 vtop--;
5569 } else {
5570 /* if commutative ops, put c2 as constant */
5571 if (c1 && (op == '+' || op == '&' || op == '^' ||
5572 op == '|' || op == '*')) {
5573 vswap();
5574 c2 = c1; //c = c1, c1 = c2, c2 = c;
5575 l2 = l1; //l = l1, l1 = l2, l2 = l;
5577 /* Filter out NOP operations like x*1, x-0, x&-1... */
5578 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5579 op == TOK_PDIV) &&
5580 l2 == 1) ||
5581 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5582 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5583 l2 == 0) ||
5584 (op == '&' &&
5585 l2 == -1))) {
5586 /* nothing to do */
5587 vtop--;
5588 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5589 /* try to use shifts instead of muls or divs */
5590 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5591 n = -1;
5592 while (l2) {
5593 l2 >>= 1;
5594 n++;
5596 vtop->c.ll = n;
5597 if (op == '*')
5598 op = TOK_SHL;
5599 else if (op == TOK_PDIV)
5600 op = TOK_SAR;
5601 else
5602 op = TOK_SHR;
5604 goto general_case;
5605 } else if (c2 && (op == '+' || op == '-') &&
5606 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5607 (VT_CONST | VT_SYM) ||
5608 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
5609 /* symbol + constant case */
5610 if (op == '-')
5611 l2 = -l2;
5612 vtop--;
5613 vtop->c.ll += l2;
5614 } else {
5615 general_case:
5616 if (!nocode_wanted) {
5617 /* call low level op generator */
5618 if (t1 == VT_LLONG || t2 == VT_LLONG)
5619 gen_opl(op);
5620 else
5621 gen_opi(op);
5622 } else {
5623 vtop--;
5629 /* generate a floating point operation with constant propagation */
5630 void gen_opif(int op)
5632 int c1, c2;
5633 SValue *v1, *v2;
5634 long double f1, f2;
5636 v1 = vtop - 1;
5637 v2 = vtop;
5638 /* currently, we cannot do computations with forward symbols */
5639 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5640 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5641 if (c1 && c2) {
5642 if (v1->type.t == VT_FLOAT) {
5643 f1 = v1->c.f;
5644 f2 = v2->c.f;
5645 } else if (v1->type.t == VT_DOUBLE) {
5646 f1 = v1->c.d;
5647 f2 = v2->c.d;
5648 } else {
5649 f1 = v1->c.ld;
5650 f2 = v2->c.ld;
5653 /* NOTE: we only do constant propagation if finite number (not
5654 NaN or infinity) (ANSI spec) */
5655 if (!ieee_finite(f1) || !ieee_finite(f2))
5656 goto general_case;
5658 switch(op) {
5659 case '+': f1 += f2; break;
5660 case '-': f1 -= f2; break;
5661 case '*': f1 *= f2; break;
5662 case '/':
5663 if (f2 == 0.0) {
5664 if (const_wanted)
5665 error("division by zero in constant");
5666 goto general_case;
5668 f1 /= f2;
5669 break;
5670 /* XXX: also handles tests ? */
5671 default:
5672 goto general_case;
5674 /* XXX: overflow test ? */
5675 if (v1->type.t == VT_FLOAT) {
5676 v1->c.f = f1;
5677 } else if (v1->type.t == VT_DOUBLE) {
5678 v1->c.d = f1;
5679 } else {
5680 v1->c.ld = f1;
5682 vtop--;
5683 } else {
5684 general_case:
5685 if (!nocode_wanted) {
5686 gen_opf(op);
5687 } else {
5688 vtop--;
5693 static int pointed_size(CType *type)
5695 int align;
5696 return type_size(pointed_type(type), &align);
5699 static inline int is_null_pointer(SValue *p)
5701 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5702 return 0;
5703 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5704 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5707 static inline int is_integer_btype(int bt)
5709 return (bt == VT_BYTE || bt == VT_SHORT ||
5710 bt == VT_INT || bt == VT_LLONG);
5713 /* check types for comparison or substraction of pointers */
5714 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5716 CType *type1, *type2, tmp_type1, tmp_type2;
5717 int bt1, bt2;
5719 /* null pointers are accepted for all comparisons as gcc */
5720 if (is_null_pointer(p1) || is_null_pointer(p2))
5721 return;
5722 type1 = &p1->type;
5723 type2 = &p2->type;
5724 bt1 = type1->t & VT_BTYPE;
5725 bt2 = type2->t & VT_BTYPE;
5726 /* accept comparison between pointer and integer with a warning */
5727 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5728 if (op != TOK_LOR && op != TOK_LAND )
5729 warning("comparison between pointer and integer");
5730 return;
5733 /* both must be pointers or implicit function pointers */
5734 if (bt1 == VT_PTR) {
5735 type1 = pointed_type(type1);
5736 } else if (bt1 != VT_FUNC)
5737 goto invalid_operands;
5739 if (bt2 == VT_PTR) {
5740 type2 = pointed_type(type2);
5741 } else if (bt2 != VT_FUNC) {
5742 invalid_operands:
5743 error("invalid operands to binary %s", get_tok_str(op, NULL));
5745 if ((type1->t & VT_BTYPE) == VT_VOID ||
5746 (type2->t & VT_BTYPE) == VT_VOID)
5747 return;
5748 tmp_type1 = *type1;
5749 tmp_type2 = *type2;
5750 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5751 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5752 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5753 /* gcc-like error if '-' is used */
5754 if (op == '-')
5755 goto invalid_operands;
5756 else
5757 warning("comparison of distinct pointer types lacks a cast");
5761 /* generic gen_op: handles types problems */
5762 void gen_op(int op)
5764 int u, t1, t2, bt1, bt2, t;
5765 CType type1;
5767 t1 = vtop[-1].type.t;
5768 t2 = vtop[0].type.t;
5769 bt1 = t1 & VT_BTYPE;
5770 bt2 = t2 & VT_BTYPE;
5772 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5773 /* at least one operand is a pointer */
5774 /* relationnal op: must be both pointers */
5775 if (op >= TOK_ULT && op <= TOK_LOR) {
5776 check_comparison_pointer_types(vtop - 1, vtop, op);
5777 /* pointers are handled are unsigned */
5778 t = VT_INT | VT_UNSIGNED;
5779 goto std_op;
5781 /* if both pointers, then it must be the '-' op */
5782 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5783 if (op != '-')
5784 error("cannot use pointers here");
5785 check_comparison_pointer_types(vtop - 1, vtop, op);
5786 /* XXX: check that types are compatible */
5787 u = pointed_size(&vtop[-1].type);
5788 gen_opic(op);
5789 /* set to integer type */
5790 vtop->type.t = VT_INT;
5791 vpushi(u);
5792 gen_op(TOK_PDIV);
5793 } else {
5794 /* exactly one pointer : must be '+' or '-'. */
5795 if (op != '-' && op != '+')
5796 error("cannot use pointers here");
5797 /* Put pointer as first operand */
5798 if (bt2 == VT_PTR) {
5799 vswap();
5800 swap(&t1, &t2);
5802 type1 = vtop[-1].type;
5803 /* XXX: cast to int ? (long long case) */
5804 vpushi(pointed_size(&vtop[-1].type));
5805 gen_op('*');
5806 #ifdef CONFIG_TCC_BCHECK
5807 /* if evaluating constant expression, no code should be
5808 generated, so no bound check */
5809 if (do_bounds_check && !const_wanted) {
5810 /* if bounded pointers, we generate a special code to
5811 test bounds */
5812 if (op == '-') {
5813 vpushi(0);
5814 vswap();
5815 gen_op('-');
5817 gen_bounded_ptr_add();
5818 } else
5819 #endif
5821 gen_opic(op);
5823 /* put again type if gen_opic() swaped operands */
5824 vtop->type = type1;
5826 } else if (is_float(bt1) || is_float(bt2)) {
5827 /* compute bigger type and do implicit casts */
5828 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5829 t = VT_LDOUBLE;
5830 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5831 t = VT_DOUBLE;
5832 } else {
5833 t = VT_FLOAT;
5835 /* floats can only be used for a few operations */
5836 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5837 (op < TOK_ULT || op > TOK_GT))
5838 error("invalid operands for binary operation");
5839 goto std_op;
5840 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5841 /* cast to biggest op */
5842 t = VT_LLONG;
5843 /* convert to unsigned if it does not fit in a long long */
5844 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5845 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5846 t |= VT_UNSIGNED;
5847 goto std_op;
5848 } else {
5849 /* integer operations */
5850 t = VT_INT;
5851 /* convert to unsigned if it does not fit in an integer */
5852 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5853 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5854 t |= VT_UNSIGNED;
5855 std_op:
5856 /* XXX: currently, some unsigned operations are explicit, so
5857 we modify them here */
5858 if (t & VT_UNSIGNED) {
5859 if (op == TOK_SAR)
5860 op = TOK_SHR;
5861 else if (op == '/')
5862 op = TOK_UDIV;
5863 else if (op == '%')
5864 op = TOK_UMOD;
5865 else if (op == TOK_LT)
5866 op = TOK_ULT;
5867 else if (op == TOK_GT)
5868 op = TOK_UGT;
5869 else if (op == TOK_LE)
5870 op = TOK_ULE;
5871 else if (op == TOK_GE)
5872 op = TOK_UGE;
5874 vswap();
5875 type1.t = t;
5876 gen_cast(&type1);
5877 vswap();
5878 /* special case for shifts and long long: we keep the shift as
5879 an integer */
5880 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5881 type1.t = VT_INT;
5882 gen_cast(&type1);
5883 if (is_float(t))
5884 gen_opif(op);
5885 else
5886 gen_opic(op);
5887 if (op >= TOK_ULT && op <= TOK_GT) {
5888 /* relationnal op: the result is an int */
5889 vtop->type.t = VT_INT;
5890 } else {
5891 vtop->type.t = t;
5896 #ifndef TCC_TARGET_ARM
5897 /* generic itof for unsigned long long case */
5898 void gen_cvt_itof1(int t)
5900 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5901 (VT_LLONG | VT_UNSIGNED)) {
5903 if (t == VT_FLOAT)
5904 vpush_global_sym(&func_old_type, TOK___floatundisf);
5905 #if LDOUBLE_SIZE != 8
5906 else if (t == VT_LDOUBLE)
5907 vpush_global_sym(&func_old_type, TOK___floatundixf);
5908 #endif
5909 else
5910 vpush_global_sym(&func_old_type, TOK___floatundidf);
5911 vrott(2);
5912 gfunc_call(1);
5913 vpushi(0);
5914 vtop->r = REG_FRET;
5915 } else {
5916 gen_cvt_itof(t);
5919 #endif
5921 /* generic ftoi for unsigned long long case */
5922 void gen_cvt_ftoi1(int t)
5924 int st;
5926 if (t == (VT_LLONG | VT_UNSIGNED)) {
5927 /* not handled natively */
5928 st = vtop->type.t & VT_BTYPE;
5929 if (st == VT_FLOAT)
5930 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5931 #if LDOUBLE_SIZE != 8
5932 else if (st == VT_LDOUBLE)
5933 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5934 #endif
5935 else
5936 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5937 vrott(2);
5938 gfunc_call(1);
5939 vpushi(0);
5940 vtop->r = REG_IRET;
5941 vtop->r2 = REG_LRET;
5942 } else {
5943 gen_cvt_ftoi(t);
5947 /* force char or short cast */
5948 void force_charshort_cast(int t)
5950 int bits, dbt;
5951 dbt = t & VT_BTYPE;
5952 /* XXX: add optimization if lvalue : just change type and offset */
5953 if (dbt == VT_BYTE)
5954 bits = 8;
5955 else
5956 bits = 16;
5957 if (t & VT_UNSIGNED) {
5958 vpushi((1 << bits) - 1);
5959 gen_op('&');
5960 } else {
5961 bits = 32 - bits;
5962 vpushi(bits);
5963 gen_op(TOK_SHL);
5964 /* result must be signed or the SAR is converted to an SHL
5965 This was not the case when "t" was a signed short
5966 and the last value on the stack was an unsigned int */
5967 vtop->type.t &= ~VT_UNSIGNED;
5968 vpushi(bits);
5969 gen_op(TOK_SAR);
5973 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5974 static void gen_cast(CType *type)
5976 int sbt, dbt, sf, df, c;
5978 /* special delayed cast for char/short */
5979 /* XXX: in some cases (multiple cascaded casts), it may still
5980 be incorrect */
5981 if (vtop->r & VT_MUSTCAST) {
5982 vtop->r &= ~VT_MUSTCAST;
5983 force_charshort_cast(vtop->type.t);
5986 /* bitfields first get cast to ints */
5987 if (vtop->type.t & VT_BITFIELD) {
5988 gv(RC_INT);
5991 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5992 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5994 if (sbt != dbt && !nocode_wanted) {
5995 sf = is_float(sbt);
5996 df = is_float(dbt);
5997 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5998 if (sf && df) {
5999 /* convert from fp to fp */
6000 if (c) {
6001 /* constant case: we can do it now */
6002 /* XXX: in ISOC, cannot do it if error in convert */
6003 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
6004 vtop->c.f = (float)vtop->c.d;
6005 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
6006 vtop->c.f = (float)vtop->c.ld;
6007 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
6008 vtop->c.d = (double)vtop->c.f;
6009 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
6010 vtop->c.d = (double)vtop->c.ld;
6011 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
6012 vtop->c.ld = (long double)vtop->c.f;
6013 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
6014 vtop->c.ld = (long double)vtop->c.d;
6015 } else {
6016 /* non constant case: generate code */
6017 gen_cvt_ftof(dbt);
6019 } else if (df) {
6020 /* convert int to fp */
6021 if (c) {
6022 switch(sbt) {
6023 case VT_LLONG | VT_UNSIGNED:
6024 case VT_LLONG:
6025 /* XXX: add const cases for long long */
6026 goto do_itof;
6027 case VT_INT | VT_UNSIGNED:
6028 switch(dbt) {
6029 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
6030 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
6031 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
6033 break;
6034 default:
6035 switch(dbt) {
6036 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
6037 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
6038 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
6040 break;
6042 } else {
6043 do_itof:
6044 gen_cvt_itof1(dbt);
6046 } else if (sf) {
6047 /* convert fp to int */
6048 if (dbt == VT_BOOL) {
6049 vpushi(0);
6050 gen_op(TOK_NE);
6051 } else {
6052 /* we handle char/short/etc... with generic code */
6053 if (dbt != (VT_INT | VT_UNSIGNED) &&
6054 dbt != (VT_LLONG | VT_UNSIGNED) &&
6055 dbt != VT_LLONG)
6056 dbt = VT_INT;
6057 if (c) {
6058 switch(dbt) {
6059 case VT_LLONG | VT_UNSIGNED:
6060 case VT_LLONG:
6061 /* XXX: add const cases for long long */
6062 goto do_ftoi;
6063 case VT_INT | VT_UNSIGNED:
6064 switch(sbt) {
6065 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
6066 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
6067 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
6069 break;
6070 default:
6071 /* int case */
6072 switch(sbt) {
6073 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
6074 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
6075 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
6077 break;
6079 } else {
6080 do_ftoi:
6081 gen_cvt_ftoi1(dbt);
6083 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6084 /* additional cast for char/short... */
6085 vtop->type.t = dbt;
6086 gen_cast(type);
6089 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6090 if ((sbt & VT_BTYPE) != VT_LLONG) {
6091 /* scalar to long long */
6092 if (c) {
6093 if (sbt == (VT_INT | VT_UNSIGNED))
6094 vtop->c.ll = vtop->c.ui;
6095 else
6096 vtop->c.ll = vtop->c.i;
6097 } else {
6098 /* machine independent conversion */
6099 gv(RC_INT);
6100 /* generate high word */
6101 if (sbt == (VT_INT | VT_UNSIGNED)) {
6102 vpushi(0);
6103 gv(RC_INT);
6104 } else {
6105 gv_dup();
6106 vpushi(31);
6107 gen_op(TOK_SAR);
6109 /* patch second register */
6110 vtop[-1].r2 = vtop->r;
6111 vpop();
6114 } else if (dbt == VT_BOOL) {
6115 /* scalar to bool */
6116 vpushi(0);
6117 gen_op(TOK_NE);
6118 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6119 (dbt & VT_BTYPE) == VT_SHORT) {
6120 if (sbt == VT_PTR) {
6121 vtop->type.t = VT_INT;
6122 warning("nonportable conversion from pointer to char/short");
6124 force_charshort_cast(dbt);
6125 } else if ((dbt & VT_BTYPE) == VT_INT) {
6126 /* scalar to int */
6127 if (sbt == VT_LLONG) {
6128 /* from long long: just take low order word */
6129 lexpand();
6130 vpop();
6132 /* if lvalue and single word type, nothing to do because
6133 the lvalue already contains the real type size (see
6134 VT_LVAL_xxx constants) */
6136 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6137 /* if we are casting between pointer types,
6138 we must update the VT_LVAL_xxx size */
6139 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6140 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6142 vtop->type = *type;
6145 /* return type size. Put alignment at 'a' */
6146 static int type_size(CType *type, int *a)
6148 Sym *s;
6149 int bt;
6151 bt = type->t & VT_BTYPE;
6152 if (bt == VT_STRUCT) {
6153 /* struct/union */
6154 s = type->ref;
6155 *a = s->r;
6156 return s->c;
6157 } else if (bt == VT_PTR) {
6158 if (type->t & VT_ARRAY) {
6159 s = type->ref;
6160 return type_size(&s->type, a) * s->c;
6161 } else {
6162 *a = PTR_SIZE;
6163 return PTR_SIZE;
6165 } else if (bt == VT_LDOUBLE) {
6166 *a = LDOUBLE_ALIGN;
6167 return LDOUBLE_SIZE;
6168 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6169 #ifdef TCC_TARGET_I386
6170 *a = 4;
6171 #elif defined(TCC_TARGET_ARM)
6172 #ifdef TCC_ARM_EABI
6173 *a = 8;
6174 #else
6175 *a = 4;
6176 #endif
6177 #else
6178 *a = 8;
6179 #endif
6180 return 8;
6181 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6182 *a = 4;
6183 return 4;
6184 } else if (bt == VT_SHORT) {
6185 *a = 2;
6186 return 2;
6187 } else {
6188 /* char, void, function, _Bool */
6189 *a = 1;
6190 return 1;
6194 /* return the pointed type of t */
6195 static inline CType *pointed_type(CType *type)
6197 return &type->ref->type;
6200 /* modify type so that its it is a pointer to type. */
6201 static void mk_pointer(CType *type)
6203 Sym *s;
6204 s = sym_push(SYM_FIELD, type, 0, -1);
6205 type->t = VT_PTR | (type->t & ~VT_TYPE);
6206 type->ref = s;
6209 /* compare function types. OLD functions match any new functions */
6210 static int is_compatible_func(CType *type1, CType *type2)
6212 Sym *s1, *s2;
6214 s1 = type1->ref;
6215 s2 = type2->ref;
6216 if (!is_compatible_types(&s1->type, &s2->type))
6217 return 0;
6218 /* check func_call */
6219 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6220 return 0;
6221 /* XXX: not complete */
6222 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6223 return 1;
6224 if (s1->c != s2->c)
6225 return 0;
6226 while (s1 != NULL) {
6227 if (s2 == NULL)
6228 return 0;
6229 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6230 return 0;
6231 s1 = s1->next;
6232 s2 = s2->next;
6234 if (s2)
6235 return 0;
6236 return 1;
6239 /* return true if type1 and type2 are the same. If unqualified is
6240 true, qualifiers on the types are ignored.
6242 - enums are not checked as gcc __builtin_types_compatible_p ()
6244 static int compare_types(CType *type1, CType *type2, int unqualified)
6246 int bt1, t1, t2;
6248 t1 = type1->t & VT_TYPE;
6249 t2 = type2->t & VT_TYPE;
6250 if (unqualified) {
6251 /* strip qualifiers before comparing */
6252 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6253 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6255 /* XXX: bitfields ? */
6256 if (t1 != t2)
6257 return 0;
6258 /* test more complicated cases */
6259 bt1 = t1 & VT_BTYPE;
6260 if (bt1 == VT_PTR) {
6261 type1 = pointed_type(type1);
6262 type2 = pointed_type(type2);
6263 return is_compatible_types(type1, type2);
6264 } else if (bt1 == VT_STRUCT) {
6265 return (type1->ref == type2->ref);
6266 } else if (bt1 == VT_FUNC) {
6267 return is_compatible_func(type1, type2);
6268 } else {
6269 return 1;
6273 /* return true if type1 and type2 are exactly the same (including
6274 qualifiers).
6276 static int is_compatible_types(CType *type1, CType *type2)
6278 return compare_types(type1,type2,0);
6281 /* return true if type1 and type2 are the same (ignoring qualifiers).
6283 static int is_compatible_parameter_types(CType *type1, CType *type2)
6285 return compare_types(type1,type2,1);
6288 /* print a type. If 'varstr' is not NULL, then the variable is also
6289 printed in the type */
6290 /* XXX: union */
6291 /* XXX: add array and function pointers */
6292 void type_to_str(char *buf, int buf_size,
6293 CType *type, const char *varstr)
6295 int bt, v, t;
6296 Sym *s, *sa;
6297 char buf1[256];
6298 const char *tstr;
6300 t = type->t & VT_TYPE;
6301 bt = t & VT_BTYPE;
6302 buf[0] = '\0';
6303 if (t & VT_CONSTANT)
6304 pstrcat(buf, buf_size, "const ");
6305 if (t & VT_VOLATILE)
6306 pstrcat(buf, buf_size, "volatile ");
6307 if (t & VT_UNSIGNED)
6308 pstrcat(buf, buf_size, "unsigned ");
6309 switch(bt) {
6310 case VT_VOID:
6311 tstr = "void";
6312 goto add_tstr;
6313 case VT_BOOL:
6314 tstr = "_Bool";
6315 goto add_tstr;
6316 case VT_BYTE:
6317 tstr = "char";
6318 goto add_tstr;
6319 case VT_SHORT:
6320 tstr = "short";
6321 goto add_tstr;
6322 case VT_INT:
6323 tstr = "int";
6324 goto add_tstr;
6325 case VT_LONG:
6326 tstr = "long";
6327 goto add_tstr;
6328 case VT_LLONG:
6329 tstr = "long long";
6330 goto add_tstr;
6331 case VT_FLOAT:
6332 tstr = "float";
6333 goto add_tstr;
6334 case VT_DOUBLE:
6335 tstr = "double";
6336 goto add_tstr;
6337 case VT_LDOUBLE:
6338 tstr = "long double";
6339 add_tstr:
6340 pstrcat(buf, buf_size, tstr);
6341 break;
6342 case VT_ENUM:
6343 case VT_STRUCT:
6344 if (bt == VT_STRUCT)
6345 tstr = "struct ";
6346 else
6347 tstr = "enum ";
6348 pstrcat(buf, buf_size, tstr);
6349 v = type->ref->v & ~SYM_STRUCT;
6350 if (v >= SYM_FIRST_ANOM)
6351 pstrcat(buf, buf_size, "<anonymous>");
6352 else
6353 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6354 break;
6355 case VT_FUNC:
6356 s = type->ref;
6357 type_to_str(buf, buf_size, &s->type, varstr);
6358 pstrcat(buf, buf_size, "(");
6359 sa = s->next;
6360 while (sa != NULL) {
6361 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6362 pstrcat(buf, buf_size, buf1);
6363 sa = sa->next;
6364 if (sa)
6365 pstrcat(buf, buf_size, ", ");
6367 pstrcat(buf, buf_size, ")");
6368 goto no_var;
6369 case VT_PTR:
6370 s = type->ref;
6371 pstrcpy(buf1, sizeof(buf1), "*");
6372 if (varstr)
6373 pstrcat(buf1, sizeof(buf1), varstr);
6374 type_to_str(buf, buf_size, &s->type, buf1);
6375 goto no_var;
6377 if (varstr) {
6378 pstrcat(buf, buf_size, " ");
6379 pstrcat(buf, buf_size, varstr);
6381 no_var: ;
6384 /* verify type compatibility to store vtop in 'dt' type, and generate
6385 casts if needed. */
6386 static void gen_assign_cast(CType *dt)
6388 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6389 char buf1[256], buf2[256];
6390 int dbt, sbt;
6392 st = &vtop->type; /* source type */
6393 dbt = dt->t & VT_BTYPE;
6394 sbt = st->t & VT_BTYPE;
6395 if (dt->t & VT_CONSTANT)
6396 warning("assignment of read-only location");
6397 switch(dbt) {
6398 case VT_PTR:
6399 /* special cases for pointers */
6400 /* '0' can also be a pointer */
6401 if (is_null_pointer(vtop))
6402 goto type_ok;
6403 /* accept implicit pointer to integer cast with warning */
6404 if (is_integer_btype(sbt)) {
6405 warning("assignment makes pointer from integer without a cast");
6406 goto type_ok;
6408 type1 = pointed_type(dt);
6409 /* a function is implicitely a function pointer */
6410 if (sbt == VT_FUNC) {
6411 if ((type1->t & VT_BTYPE) != VT_VOID &&
6412 !is_compatible_types(pointed_type(dt), st))
6413 goto error;
6414 else
6415 goto type_ok;
6417 if (sbt != VT_PTR)
6418 goto error;
6419 type2 = pointed_type(st);
6420 if ((type1->t & VT_BTYPE) == VT_VOID ||
6421 (type2->t & VT_BTYPE) == VT_VOID) {
6422 /* void * can match anything */
6423 } else {
6424 /* exact type match, except for unsigned */
6425 tmp_type1 = *type1;
6426 tmp_type2 = *type2;
6427 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6428 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6429 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6430 warning("assignment from incompatible pointer type");
6432 /* check const and volatile */
6433 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6434 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6435 warning("assignment discards qualifiers from pointer target type");
6436 break;
6437 case VT_BYTE:
6438 case VT_SHORT:
6439 case VT_INT:
6440 case VT_LLONG:
6441 if (sbt == VT_PTR || sbt == VT_FUNC) {
6442 warning("assignment makes integer from pointer without a cast");
6444 /* XXX: more tests */
6445 break;
6446 case VT_STRUCT:
6447 tmp_type1 = *dt;
6448 tmp_type2 = *st;
6449 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6450 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6451 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6452 error:
6453 type_to_str(buf1, sizeof(buf1), st, NULL);
6454 type_to_str(buf2, sizeof(buf2), dt, NULL);
6455 error("cannot cast '%s' to '%s'", buf1, buf2);
6457 break;
6459 type_ok:
6460 gen_cast(dt);
6463 /* store vtop in lvalue pushed on stack */
6464 void vstore(void)
6466 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6468 ft = vtop[-1].type.t;
6469 sbt = vtop->type.t & VT_BTYPE;
6470 dbt = ft & VT_BTYPE;
6471 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6472 (sbt == VT_INT && dbt == VT_SHORT)) {
6473 /* optimize char/short casts */
6474 delayed_cast = VT_MUSTCAST;
6475 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
6476 /* XXX: factorize */
6477 if (ft & VT_CONSTANT)
6478 warning("assignment of read-only location");
6479 } else {
6480 delayed_cast = 0;
6481 if (!(ft & VT_BITFIELD))
6482 gen_assign_cast(&vtop[-1].type);
6485 if (sbt == VT_STRUCT) {
6486 /* if structure, only generate pointer */
6487 /* structure assignment : generate memcpy */
6488 /* XXX: optimize if small size */
6489 if (!nocode_wanted) {
6490 size = type_size(&vtop->type, &align);
6492 #ifdef TCC_ARM_EABI
6493 if(!(align & 7))
6494 vpush_global_sym(&func_old_type, TOK_memcpy8);
6495 else if(!(align & 3))
6496 vpush_global_sym(&func_old_type, TOK_memcpy4);
6497 else
6498 #endif
6499 vpush_global_sym(&func_old_type, TOK_memcpy);
6501 /* destination */
6502 vpushv(vtop - 2);
6503 vtop->type.t = VT_INT;
6504 gaddrof();
6505 /* source */
6506 vpushv(vtop - 2);
6507 vtop->type.t = VT_INT;
6508 gaddrof();
6509 /* type size */
6510 vpushi(size);
6511 gfunc_call(3);
6513 vswap();
6514 vpop();
6515 } else {
6516 vswap();
6517 vpop();
6519 /* leave source on stack */
6520 } else if (ft & VT_BITFIELD) {
6521 /* bitfield store handling */
6522 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6523 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6524 /* remove bit field info to avoid loops */
6525 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6527 /* duplicate source into other register */
6528 gv_dup();
6529 vswap();
6530 vrott(3);
6532 if((ft & VT_BTYPE) == VT_BOOL) {
6533 gen_cast(&vtop[-1].type);
6534 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
6537 /* duplicate destination */
6538 vdup();
6539 vtop[-1] = vtop[-2];
6541 /* mask and shift source */
6542 if((ft & VT_BTYPE) != VT_BOOL) {
6543 vpushi((1 << bit_size) - 1);
6544 gen_op('&');
6546 vpushi(bit_pos);
6547 gen_op(TOK_SHL);
6548 /* load destination, mask and or with source */
6549 vswap();
6550 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6551 gen_op('&');
6552 gen_op('|');
6553 /* store result */
6554 vstore();
6556 /* pop off shifted source from "duplicate source..." above */
6557 vpop();
6559 } else {
6560 #ifdef CONFIG_TCC_BCHECK
6561 /* bound check case */
6562 if (vtop[-1].r & VT_MUSTBOUND) {
6563 vswap();
6564 gbound();
6565 vswap();
6567 #endif
6568 if (!nocode_wanted) {
6569 rc = RC_INT;
6570 if (is_float(ft))
6571 rc = RC_FLOAT;
6572 r = gv(rc); /* generate value */
6573 /* if lvalue was saved on stack, must read it */
6574 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6575 SValue sv;
6576 t = get_reg(RC_INT);
6577 sv.type.t = VT_INT;
6578 sv.r = VT_LOCAL | VT_LVAL;
6579 sv.c.ul = vtop[-1].c.ul;
6580 load(t, &sv);
6581 vtop[-1].r = t | VT_LVAL;
6583 store(r, vtop - 1);
6584 /* two word case handling : store second register at word + 4 */
6585 if ((ft & VT_BTYPE) == VT_LLONG) {
6586 vswap();
6587 /* convert to int to increment easily */
6588 vtop->type.t = VT_INT;
6589 gaddrof();
6590 vpushi(4);
6591 gen_op('+');
6592 vtop->r |= VT_LVAL;
6593 vswap();
6594 /* XXX: it works because r2 is spilled last ! */
6595 store(vtop->r2, vtop - 1);
6598 vswap();
6599 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6600 vtop->r |= delayed_cast;
6604 /* post defines POST/PRE add. c is the token ++ or -- */
6605 void inc(int post, int c)
6607 test_lvalue();
6608 vdup(); /* save lvalue */
6609 if (post) {
6610 gv_dup(); /* duplicate value */
6611 vrotb(3);
6612 vrotb(3);
6614 /* add constant */
6615 vpushi(c - TOK_MID);
6616 gen_op('+');
6617 vstore(); /* store value */
6618 if (post)
6619 vpop(); /* if post op, return saved value */
6622 /* Parse GNUC __attribute__ extension. Currently, the following
6623 extensions are recognized:
6624 - aligned(n) : set data/function alignment.
6625 - packed : force data alignment to 1
6626 - section(x) : generate data/code in this section.
6627 - unused : currently ignored, but may be used someday.
6628 - regparm(n) : pass function parameters in registers (i386 only)
6630 static void parse_attribute(AttributeDef *ad)
6632 int t, n;
6634 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6635 next();
6636 skip('(');
6637 skip('(');
6638 while (tok != ')') {
6639 if (tok < TOK_IDENT)
6640 expect("attribute name");
6641 t = tok;
6642 next();
6643 switch(t) {
6644 case TOK_SECTION1:
6645 case TOK_SECTION2:
6646 skip('(');
6647 if (tok != TOK_STR)
6648 expect("section name");
6649 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6650 next();
6651 skip(')');
6652 break;
6653 case TOK_ALIGNED1:
6654 case TOK_ALIGNED2:
6655 if (tok == '(') {
6656 next();
6657 n = expr_const();
6658 if (n <= 0 || (n & (n - 1)) != 0)
6659 error("alignment must be a positive power of two");
6660 skip(')');
6661 } else {
6662 n = MAX_ALIGN;
6664 ad->aligned = n;
6665 break;
6666 case TOK_PACKED1:
6667 case TOK_PACKED2:
6668 ad->packed = 1;
6669 break;
6670 case TOK_UNUSED1:
6671 case TOK_UNUSED2:
6672 /* currently, no need to handle it because tcc does not
6673 track unused objects */
6674 break;
6675 case TOK_NORETURN1:
6676 case TOK_NORETURN2:
6677 /* currently, no need to handle it because tcc does not
6678 track unused objects */
6679 break;
6680 case TOK_CDECL1:
6681 case TOK_CDECL2:
6682 case TOK_CDECL3:
6683 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6684 break;
6685 case TOK_STDCALL1:
6686 case TOK_STDCALL2:
6687 case TOK_STDCALL3:
6688 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6689 break;
6690 #ifdef TCC_TARGET_I386
6691 case TOK_REGPARM1:
6692 case TOK_REGPARM2:
6693 skip('(');
6694 n = expr_const();
6695 if (n > 3)
6696 n = 3;
6697 else if (n < 0)
6698 n = 0;
6699 if (n > 0)
6700 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6701 skip(')');
6702 break;
6703 case TOK_FASTCALL1:
6704 case TOK_FASTCALL2:
6705 case TOK_FASTCALL3:
6706 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6707 break;
6708 #endif
6709 case TOK_DLLEXPORT:
6710 FUNC_EXPORT(ad->func_attr) = 1;
6711 break;
6712 default:
6713 if (tcc_state->warn_unsupported)
6714 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6715 /* skip parameters */
6716 if (tok == '(') {
6717 int parenthesis = 0;
6718 do {
6719 if (tok == '(')
6720 parenthesis++;
6721 else if (tok == ')')
6722 parenthesis--;
6723 next();
6724 } while (parenthesis && tok != -1);
6726 break;
6728 if (tok != ',')
6729 break;
6730 next();
6732 skip(')');
6733 skip(')');
6737 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6738 static void struct_decl(CType *type, int u)
6740 int a, v, size, align, maxalign, c, offset;
6741 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
6742 Sym *s, *ss, *ass, **ps;
6743 AttributeDef ad;
6744 CType type1, btype;
6746 a = tok; /* save decl type */
6747 next();
6748 if (tok != '{') {
6749 v = tok;
6750 next();
6751 /* struct already defined ? return it */
6752 if (v < TOK_IDENT)
6753 expect("struct/union/enum name");
6754 s = struct_find(v);
6755 if (s) {
6756 if (s->type.t != a)
6757 error("invalid type");
6758 goto do_decl;
6760 } else {
6761 v = anon_sym++;
6763 type1.t = a;
6764 /* we put an undefined size for struct/union */
6765 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6766 s->r = 0; /* default alignment is zero as gcc */
6767 /* put struct/union/enum name in type */
6768 do_decl:
6769 type->t = u;
6770 type->ref = s;
6772 if (tok == '{') {
6773 next();
6774 if (s->c != -1)
6775 error("struct/union/enum already defined");
6776 /* cannot be empty */
6777 c = 0;
6778 /* non empty enums are not allowed */
6779 if (a == TOK_ENUM) {
6780 for(;;) {
6781 v = tok;
6782 if (v < TOK_UIDENT)
6783 expect("identifier");
6784 next();
6785 if (tok == '=') {
6786 next();
6787 c = expr_const();
6789 /* enum symbols have static storage */
6790 ss = sym_push(v, &int_type, VT_CONST, c);
6791 ss->type.t |= VT_STATIC;
6792 if (tok != ',')
6793 break;
6794 next();
6795 c++;
6796 /* NOTE: we accept a trailing comma */
6797 if (tok == '}')
6798 break;
6800 skip('}');
6801 } else {
6802 maxalign = 1;
6803 ps = &s->next;
6804 prevbt = VT_INT;
6805 bit_pos = 0;
6806 offset = 0;
6807 while (tok != '}') {
6808 parse_btype(&btype, &ad);
6809 while (1) {
6810 bit_size = -1;
6811 v = 0;
6812 type1 = btype;
6813 if (tok != ':') {
6814 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6815 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6816 expect("identifier");
6817 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6818 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6819 error("invalid type for '%s'",
6820 get_tok_str(v, NULL));
6822 if (tok == ':') {
6823 next();
6824 bit_size = expr_const();
6825 /* XXX: handle v = 0 case for messages */
6826 if (bit_size < 0)
6827 error("negative width in bit-field '%s'",
6828 get_tok_str(v, NULL));
6829 if (v && bit_size == 0)
6830 error("zero width for bit-field '%s'",
6831 get_tok_str(v, NULL));
6833 size = type_size(&type1, &align);
6834 if (ad.aligned) {
6835 if (align < ad.aligned)
6836 align = ad.aligned;
6837 } else if (ad.packed) {
6838 align = 1;
6839 } else if (*tcc_state->pack_stack_ptr) {
6840 if (align > *tcc_state->pack_stack_ptr)
6841 align = *tcc_state->pack_stack_ptr;
6843 lbit_pos = 0;
6844 if (bit_size >= 0) {
6845 bt = type1.t & VT_BTYPE;
6846 if (bt != VT_INT &&
6847 bt != VT_BYTE &&
6848 bt != VT_SHORT &&
6849 bt != VT_BOOL &&
6850 bt != VT_ENUM)
6851 error("bitfields must have scalar type");
6852 bsize = size * 8;
6853 if (bit_size > bsize) {
6854 error("width of '%s' exceeds its type",
6855 get_tok_str(v, NULL));
6856 } else if (bit_size == bsize) {
6857 /* no need for bit fields */
6858 bit_pos = 0;
6859 } else if (bit_size == 0) {
6860 /* XXX: what to do if only padding in a
6861 structure ? */
6862 /* zero size: means to pad */
6863 bit_pos = 0;
6864 } else {
6865 /* we do not have enough room ?
6866 did the type change?
6867 is it a union? */
6868 if ((bit_pos + bit_size) > bsize ||
6869 bt != prevbt || a == TOK_UNION)
6870 bit_pos = 0;
6871 lbit_pos = bit_pos;
6872 /* XXX: handle LSB first */
6873 type1.t |= VT_BITFIELD |
6874 (bit_pos << VT_STRUCT_SHIFT) |
6875 (bit_size << (VT_STRUCT_SHIFT + 6));
6876 bit_pos += bit_size;
6878 prevbt = bt;
6879 } else {
6880 bit_pos = 0;
6882 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
6883 /* add new memory data only if starting
6884 bit field */
6885 if (lbit_pos == 0) {
6886 if (a == TOK_STRUCT) {
6887 c = (c + align - 1) & -align;
6888 offset = c;
6889 if (size > 0)
6890 c += size;
6891 } else {
6892 offset = 0;
6893 if (size > c)
6894 c = size;
6896 if (align > maxalign)
6897 maxalign = align;
6899 #if 0
6900 printf("add field %s offset=%d",
6901 get_tok_str(v, NULL), offset);
6902 if (type1.t & VT_BITFIELD) {
6903 printf(" pos=%d size=%d",
6904 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6905 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6907 printf("\n");
6908 #endif
6910 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
6911 ass = type1.ref;
6912 while ((ass = ass->next) != NULL) {
6913 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
6914 *ps = ss;
6915 ps = &ss->next;
6917 } else if (v) {
6918 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6919 *ps = ss;
6920 ps = &ss->next;
6922 if (tok == ';' || tok == TOK_EOF)
6923 break;
6924 skip(',');
6926 skip(';');
6928 skip('}');
6929 /* store size and alignment */
6930 s->c = (c + maxalign - 1) & -maxalign;
6931 s->r = maxalign;
6936 /* return 0 if no type declaration. otherwise, return the basic type
6937 and skip it.
6939 static int parse_btype(CType *type, AttributeDef *ad)
6941 int t, u, type_found, typespec_found, typedef_found;
6942 Sym *s;
6943 CType type1;
6945 memset(ad, 0, sizeof(AttributeDef));
6946 type_found = 0;
6947 typespec_found = 0;
6948 typedef_found = 0;
6949 t = 0;
6950 while(1) {
6951 switch(tok) {
6952 case TOK_EXTENSION:
6953 /* currently, we really ignore extension */
6954 next();
6955 continue;
6957 /* basic types */
6958 case TOK_CHAR:
6959 u = VT_BYTE;
6960 basic_type:
6961 next();
6962 basic_type1:
6963 if ((t & VT_BTYPE) != 0)
6964 error("too many basic types");
6965 t |= u;
6966 typespec_found = 1;
6967 break;
6968 case TOK_VOID:
6969 u = VT_VOID;
6970 goto basic_type;
6971 case TOK_SHORT:
6972 u = VT_SHORT;
6973 goto basic_type;
6974 case TOK_INT:
6975 next();
6976 typespec_found = 1;
6977 break;
6978 case TOK_LONG:
6979 next();
6980 if ((t & VT_BTYPE) == VT_DOUBLE) {
6981 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6982 } else if ((t & VT_BTYPE) == VT_LONG) {
6983 t = (t & ~VT_BTYPE) | VT_LLONG;
6984 } else {
6985 u = VT_LONG;
6986 goto basic_type1;
6988 break;
6989 case TOK_BOOL:
6990 u = VT_BOOL;
6991 goto basic_type;
6992 case TOK_FLOAT:
6993 u = VT_FLOAT;
6994 goto basic_type;
6995 case TOK_DOUBLE:
6996 next();
6997 if ((t & VT_BTYPE) == VT_LONG) {
6998 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6999 } else {
7000 u = VT_DOUBLE;
7001 goto basic_type1;
7003 break;
7004 case TOK_ENUM:
7005 struct_decl(&type1, VT_ENUM);
7006 basic_type2:
7007 u = type1.t;
7008 type->ref = type1.ref;
7009 goto basic_type1;
7010 case TOK_STRUCT:
7011 case TOK_UNION:
7012 struct_decl(&type1, VT_STRUCT);
7013 goto basic_type2;
7015 /* type modifiers */
7016 case TOK_CONST1:
7017 case TOK_CONST2:
7018 case TOK_CONST3:
7019 t |= VT_CONSTANT;
7020 next();
7021 break;
7022 case TOK_VOLATILE1:
7023 case TOK_VOLATILE2:
7024 case TOK_VOLATILE3:
7025 t |= VT_VOLATILE;
7026 next();
7027 break;
7028 case TOK_SIGNED1:
7029 case TOK_SIGNED2:
7030 case TOK_SIGNED3:
7031 typespec_found = 1;
7032 t |= VT_SIGNED;
7033 next();
7034 break;
7035 case TOK_REGISTER:
7036 case TOK_AUTO:
7037 case TOK_RESTRICT1:
7038 case TOK_RESTRICT2:
7039 case TOK_RESTRICT3:
7040 next();
7041 break;
7042 case TOK_UNSIGNED:
7043 t |= VT_UNSIGNED;
7044 next();
7045 typespec_found = 1;
7046 break;
7048 /* storage */
7049 case TOK_EXTERN:
7050 t |= VT_EXTERN;
7051 next();
7052 break;
7053 case TOK_STATIC:
7054 t |= VT_STATIC;
7055 next();
7056 break;
7057 case TOK_TYPEDEF:
7058 t |= VT_TYPEDEF;
7059 next();
7060 break;
7061 case TOK_INLINE1:
7062 case TOK_INLINE2:
7063 case TOK_INLINE3:
7064 t |= VT_INLINE;
7065 next();
7066 break;
7068 /* GNUC attribute */
7069 case TOK_ATTRIBUTE1:
7070 case TOK_ATTRIBUTE2:
7071 parse_attribute(ad);
7072 break;
7073 /* GNUC typeof */
7074 case TOK_TYPEOF1:
7075 case TOK_TYPEOF2:
7076 case TOK_TYPEOF3:
7077 next();
7078 parse_expr_type(&type1);
7079 goto basic_type2;
7080 default:
7081 if (typespec_found || typedef_found)
7082 goto the_end;
7083 s = sym_find(tok);
7084 if (!s || !(s->type.t & VT_TYPEDEF))
7085 goto the_end;
7086 typedef_found = 1;
7087 t |= (s->type.t & ~VT_TYPEDEF);
7088 type->ref = s->type.ref;
7089 next();
7090 typespec_found = 1;
7091 break;
7093 type_found = 1;
7095 the_end:
7096 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7097 error("signed and unsigned modifier");
7098 if (tcc_state->char_is_unsigned) {
7099 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7100 t |= VT_UNSIGNED;
7102 t &= ~VT_SIGNED;
7104 /* long is never used as type */
7105 if ((t & VT_BTYPE) == VT_LONG)
7106 t = (t & ~VT_BTYPE) | VT_INT;
7107 type->t = t;
7108 return type_found;
7111 /* convert a function parameter type (array to pointer and function to
7112 function pointer) */
7113 static inline void convert_parameter_type(CType *pt)
7115 /* remove const and volatile qualifiers (XXX: const could be used
7116 to indicate a const function parameter */
7117 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7118 /* array must be transformed to pointer according to ANSI C */
7119 pt->t &= ~VT_ARRAY;
7120 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7121 mk_pointer(pt);
7125 static void post_type(CType *type, AttributeDef *ad)
7127 int n, l, t1, arg_size, align;
7128 Sym **plast, *s, *first;
7129 AttributeDef ad1;
7130 CType pt;
7132 if (tok == '(') {
7133 /* function declaration */
7134 next();
7135 l = 0;
7136 first = NULL;
7137 plast = &first;
7138 arg_size = 0;
7139 if (tok != ')') {
7140 for(;;) {
7141 /* read param name and compute offset */
7142 if (l != FUNC_OLD) {
7143 if (!parse_btype(&pt, &ad1)) {
7144 if (l) {
7145 error("invalid type");
7146 } else {
7147 l = FUNC_OLD;
7148 goto old_proto;
7151 l = FUNC_NEW;
7152 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7153 break;
7154 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7155 if ((pt.t & VT_BTYPE) == VT_VOID)
7156 error("parameter declared as void");
7157 arg_size += (type_size(&pt, &align) + 3) & ~3;
7158 } else {
7159 old_proto:
7160 n = tok;
7161 if (n < TOK_UIDENT)
7162 expect("identifier");
7163 pt.t = VT_INT;
7164 next();
7166 convert_parameter_type(&pt);
7167 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7168 *plast = s;
7169 plast = &s->next;
7170 if (tok == ')')
7171 break;
7172 skip(',');
7173 if (l == FUNC_NEW && tok == TOK_DOTS) {
7174 l = FUNC_ELLIPSIS;
7175 next();
7176 break;
7180 /* if no parameters, then old type prototype */
7181 if (l == 0)
7182 l = FUNC_OLD;
7183 skip(')');
7184 t1 = type->t & VT_STORAGE;
7185 /* NOTE: const is ignored in returned type as it has a special
7186 meaning in gcc / C++ */
7187 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7188 post_type(type, ad);
7189 /* we push a anonymous symbol which will contain the function prototype */
7190 FUNC_ARGS(ad->func_attr) = arg_size;
7191 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7192 s->next = first;
7193 type->t = t1 | VT_FUNC;
7194 type->ref = s;
7195 } else if (tok == '[') {
7196 /* array definition */
7197 next();
7198 n = -1;
7199 if (tok != ']') {
7200 n = expr_const();
7201 if (n < 0)
7202 error("invalid array size");
7204 skip(']');
7205 /* parse next post type */
7206 t1 = type->t & VT_STORAGE;
7207 type->t &= ~VT_STORAGE;
7208 post_type(type, ad);
7210 /* we push a anonymous symbol which will contain the array
7211 element type */
7212 s = sym_push(SYM_FIELD, type, 0, n);
7213 type->t = t1 | VT_ARRAY | VT_PTR;
7214 type->ref = s;
7218 /* Parse a type declaration (except basic type), and return the type
7219 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7220 expected. 'type' should contain the basic type. 'ad' is the
7221 attribute definition of the basic type. It can be modified by
7222 type_decl().
7224 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7226 Sym *s;
7227 CType type1, *type2;
7228 int qualifiers;
7230 while (tok == '*') {
7231 qualifiers = 0;
7232 redo:
7233 next();
7234 switch(tok) {
7235 case TOK_CONST1:
7236 case TOK_CONST2:
7237 case TOK_CONST3:
7238 qualifiers |= VT_CONSTANT;
7239 goto redo;
7240 case TOK_VOLATILE1:
7241 case TOK_VOLATILE2:
7242 case TOK_VOLATILE3:
7243 qualifiers |= VT_VOLATILE;
7244 goto redo;
7245 case TOK_RESTRICT1:
7246 case TOK_RESTRICT2:
7247 case TOK_RESTRICT3:
7248 goto redo;
7250 mk_pointer(type);
7251 type->t |= qualifiers;
7254 /* XXX: clarify attribute handling */
7255 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7256 parse_attribute(ad);
7258 /* recursive type */
7259 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7260 type1.t = 0; /* XXX: same as int */
7261 if (tok == '(') {
7262 next();
7263 /* XXX: this is not correct to modify 'ad' at this point, but
7264 the syntax is not clear */
7265 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7266 parse_attribute(ad);
7267 type_decl(&type1, ad, v, td);
7268 skip(')');
7269 } else {
7270 /* type identifier */
7271 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7272 *v = tok;
7273 next();
7274 } else {
7275 if (!(td & TYPE_ABSTRACT))
7276 expect("identifier");
7277 *v = 0;
7280 post_type(type, ad);
7281 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7282 parse_attribute(ad);
7283 if (!type1.t)
7284 return;
7285 /* append type at the end of type1 */
7286 type2 = &type1;
7287 for(;;) {
7288 s = type2->ref;
7289 type2 = &s->type;
7290 if (!type2->t) {
7291 *type2 = *type;
7292 break;
7295 *type = type1;
7298 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7299 static int lvalue_type(int t)
7301 int bt, r;
7302 r = VT_LVAL;
7303 bt = t & VT_BTYPE;
7304 if (bt == VT_BYTE || bt == VT_BOOL)
7305 r |= VT_LVAL_BYTE;
7306 else if (bt == VT_SHORT)
7307 r |= VT_LVAL_SHORT;
7308 else
7309 return r;
7310 if (t & VT_UNSIGNED)
7311 r |= VT_LVAL_UNSIGNED;
7312 return r;
7315 /* indirection with full error checking and bound check */
7316 static void indir(void)
7318 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7319 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7320 return;
7321 expect("pointer");
7323 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7324 gv(RC_INT);
7325 vtop->type = *pointed_type(&vtop->type);
7326 /* Arrays and functions are never lvalues */
7327 if (!(vtop->type.t & VT_ARRAY)
7328 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7329 vtop->r |= lvalue_type(vtop->type.t);
7330 /* if bound checking, the referenced pointer must be checked */
7331 if (do_bounds_check)
7332 vtop->r |= VT_MUSTBOUND;
7336 /* pass a parameter to a function and do type checking and casting */
7337 static void gfunc_param_typed(Sym *func, Sym *arg)
7339 int func_type;
7340 CType type;
7342 func_type = func->c;
7343 if (func_type == FUNC_OLD ||
7344 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7345 /* default casting : only need to convert float to double */
7346 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7347 type.t = VT_DOUBLE;
7348 gen_cast(&type);
7350 } else if (arg == NULL) {
7351 error("too many arguments to function");
7352 } else {
7353 type = arg->type;
7354 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7355 gen_assign_cast(&type);
7359 /* parse an expression of the form '(type)' or '(expr)' and return its
7360 type */
7361 static void parse_expr_type(CType *type)
7363 int n;
7364 AttributeDef ad;
7366 skip('(');
7367 if (parse_btype(type, &ad)) {
7368 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7369 } else {
7370 expr_type(type);
7372 skip(')');
7375 static void parse_type(CType *type)
7377 AttributeDef ad;
7378 int n;
7380 if (!parse_btype(type, &ad)) {
7381 expect("type");
7383 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7386 static void vpush_tokc(int t)
7388 CType type;
7389 type.t = t;
7390 vsetc(&type, VT_CONST, &tokc);
7393 static void unary(void)
7395 int n, t, align, size, r;
7396 CType type;
7397 Sym *s;
7398 AttributeDef ad;
7400 /* XXX: GCC 2.95.3 does not generate a table although it should be
7401 better here */
7402 tok_next:
7403 switch(tok) {
7404 case TOK_EXTENSION:
7405 next();
7406 goto tok_next;
7407 case TOK_CINT:
7408 case TOK_CCHAR:
7409 case TOK_LCHAR:
7410 vpushi(tokc.i);
7411 next();
7412 break;
7413 case TOK_CUINT:
7414 vpush_tokc(VT_INT | VT_UNSIGNED);
7415 next();
7416 break;
7417 case TOK_CLLONG:
7418 vpush_tokc(VT_LLONG);
7419 next();
7420 break;
7421 case TOK_CULLONG:
7422 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7423 next();
7424 break;
7425 case TOK_CFLOAT:
7426 vpush_tokc(VT_FLOAT);
7427 next();
7428 break;
7429 case TOK_CDOUBLE:
7430 vpush_tokc(VT_DOUBLE);
7431 next();
7432 break;
7433 case TOK_CLDOUBLE:
7434 vpush_tokc(VT_LDOUBLE);
7435 next();
7436 break;
7437 case TOK___FUNCTION__:
7438 if (!gnu_ext)
7439 goto tok_identifier;
7440 /* fall thru */
7441 case TOK___FUNC__:
7443 void *ptr;
7444 int len;
7445 /* special function name identifier */
7446 len = strlen(funcname) + 1;
7447 /* generate char[len] type */
7448 type.t = VT_BYTE;
7449 mk_pointer(&type);
7450 type.t |= VT_ARRAY;
7451 type.ref->c = len;
7452 vpush_ref(&type, data_section, data_section->data_offset, len);
7453 ptr = section_ptr_add(data_section, len);
7454 memcpy(ptr, funcname, len);
7455 next();
7457 break;
7458 case TOK_LSTR:
7459 #ifdef TCC_TARGET_PE
7460 t = VT_SHORT | VT_UNSIGNED;
7461 #else
7462 t = VT_INT;
7463 #endif
7464 goto str_init;
7465 case TOK_STR:
7466 /* string parsing */
7467 t = VT_BYTE;
7468 str_init:
7469 if (tcc_state->warn_write_strings)
7470 t |= VT_CONSTANT;
7471 type.t = t;
7472 mk_pointer(&type);
7473 type.t |= VT_ARRAY;
7474 memset(&ad, 0, sizeof(AttributeDef));
7475 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7476 break;
7477 case '(':
7478 next();
7479 /* cast ? */
7480 if (parse_btype(&type, &ad)) {
7481 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7482 skip(')');
7483 /* check ISOC99 compound literal */
7484 if (tok == '{') {
7485 /* data is allocated locally by default */
7486 if (global_expr)
7487 r = VT_CONST;
7488 else
7489 r = VT_LOCAL;
7490 /* all except arrays are lvalues */
7491 if (!(type.t & VT_ARRAY))
7492 r |= lvalue_type(type.t);
7493 memset(&ad, 0, sizeof(AttributeDef));
7494 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7495 } else {
7496 unary();
7497 gen_cast(&type);
7499 } else if (tok == '{') {
7500 /* save all registers */
7501 save_regs(0);
7502 /* statement expression : we do not accept break/continue
7503 inside as GCC does */
7504 block(NULL, NULL, NULL, NULL, 0, 1);
7505 skip(')');
7506 } else {
7507 gexpr();
7508 skip(')');
7510 break;
7511 case '*':
7512 next();
7513 unary();
7514 indir();
7515 break;
7516 case '&':
7517 next();
7518 unary();
7519 /* functions names must be treated as function pointers,
7520 except for unary '&' and sizeof. Since we consider that
7521 functions are not lvalues, we only have to handle it
7522 there and in function calls. */
7523 /* arrays can also be used although they are not lvalues */
7524 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7525 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7526 test_lvalue();
7527 mk_pointer(&vtop->type);
7528 gaddrof();
7529 break;
7530 case '!':
7531 next();
7532 unary();
7533 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
7534 vtop->c.i = !vtop->c.i;
7535 else if ((vtop->r & VT_VALMASK) == VT_CMP)
7536 vtop->c.i = vtop->c.i ^ 1;
7537 else {
7538 save_regs(1);
7539 vseti(VT_JMP, gtst(1, 0));
7541 break;
7542 case '~':
7543 next();
7544 unary();
7545 vpushi(-1);
7546 gen_op('^');
7547 break;
7548 case '+':
7549 next();
7550 /* in order to force cast, we add zero */
7551 unary();
7552 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7553 error("pointer not accepted for unary plus");
7554 vpushi(0);
7555 gen_op('+');
7556 break;
7557 case TOK_SIZEOF:
7558 case TOK_ALIGNOF1:
7559 case TOK_ALIGNOF2:
7560 t = tok;
7561 next();
7562 if (tok == '(') {
7563 parse_expr_type(&type);
7564 } else {
7565 unary_type(&type);
7567 size = type_size(&type, &align);
7568 if (t == TOK_SIZEOF) {
7569 if (size < 0)
7570 error("sizeof applied to an incomplete type");
7571 vpushi(size);
7572 } else {
7573 vpushi(align);
7575 vtop->type.t |= VT_UNSIGNED;
7576 break;
7578 case TOK_builtin_types_compatible_p:
7580 CType type1, type2;
7581 next();
7582 skip('(');
7583 parse_type(&type1);
7584 skip(',');
7585 parse_type(&type2);
7586 skip(')');
7587 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7588 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7589 vpushi(is_compatible_types(&type1, &type2));
7591 break;
7592 case TOK_builtin_constant_p:
7594 int saved_nocode_wanted, res;
7595 next();
7596 skip('(');
7597 saved_nocode_wanted = nocode_wanted;
7598 nocode_wanted = 1;
7599 gexpr();
7600 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7601 vpop();
7602 nocode_wanted = saved_nocode_wanted;
7603 skip(')');
7604 vpushi(res);
7606 break;
7607 case TOK_INC:
7608 case TOK_DEC:
7609 t = tok;
7610 next();
7611 unary();
7612 inc(0, t);
7613 break;
7614 case '-':
7615 next();
7616 vpushi(0);
7617 unary();
7618 gen_op('-');
7619 break;
7620 case TOK_LAND:
7621 if (!gnu_ext)
7622 goto tok_identifier;
7623 next();
7624 /* allow to take the address of a label */
7625 if (tok < TOK_UIDENT)
7626 expect("label identifier");
7627 s = label_find(tok);
7628 if (!s) {
7629 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7630 } else {
7631 if (s->r == LABEL_DECLARED)
7632 s->r = LABEL_FORWARD;
7634 if (!s->type.t) {
7635 s->type.t = VT_VOID;
7636 mk_pointer(&s->type);
7637 s->type.t |= VT_STATIC;
7639 vset(&s->type, VT_CONST | VT_SYM, 0);
7640 vtop->sym = s;
7641 next();
7642 break;
7643 default:
7644 tok_identifier:
7645 t = tok;
7646 next();
7647 if (t < TOK_UIDENT)
7648 expect("identifier");
7649 s = sym_find(t);
7650 if (!s) {
7651 if (tok != '(')
7652 error("'%s' undeclared", get_tok_str(t, NULL));
7653 /* for simple function calls, we tolerate undeclared
7654 external reference to int() function */
7655 if (tcc_state->warn_implicit_function_declaration)
7656 warning("implicit declaration of function '%s'",
7657 get_tok_str(t, NULL));
7658 s = external_global_sym(t, &func_old_type, 0);
7660 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7661 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7662 /* if referencing an inline function, then we generate a
7663 symbol to it if not already done. It will have the
7664 effect to generate code for it at the end of the
7665 compilation unit. Inline function as always
7666 generated in the text section. */
7667 if (!s->c)
7668 put_extern_sym(s, text_section, 0, 0);
7669 r = VT_SYM | VT_CONST;
7670 } else {
7671 r = s->r;
7673 vset(&s->type, r, s->c);
7674 /* if forward reference, we must point to s */
7675 if (vtop->r & VT_SYM) {
7676 vtop->sym = s;
7677 vtop->c.ul = 0;
7679 break;
7682 /* post operations */
7683 while (1) {
7684 if (tok == TOK_INC || tok == TOK_DEC) {
7685 inc(1, tok);
7686 next();
7687 } else if (tok == '.' || tok == TOK_ARROW) {
7688 /* field */
7689 if (tok == TOK_ARROW)
7690 indir();
7691 test_lvalue();
7692 gaddrof();
7693 next();
7694 /* expect pointer on structure */
7695 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7696 expect("struct or union");
7697 s = vtop->type.ref;
7698 /* find field */
7699 tok |= SYM_FIELD;
7700 while ((s = s->next) != NULL) {
7701 if (s->v == tok)
7702 break;
7704 if (!s)
7705 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7706 /* add field offset to pointer */
7707 vtop->type = char_pointer_type; /* change type to 'char *' */
7708 vpushi(s->c);
7709 gen_op('+');
7710 /* change type to field type, and set to lvalue */
7711 vtop->type = s->type;
7712 /* an array is never an lvalue */
7713 if (!(vtop->type.t & VT_ARRAY)) {
7714 vtop->r |= lvalue_type(vtop->type.t);
7715 /* if bound checking, the referenced pointer must be checked */
7716 if (do_bounds_check)
7717 vtop->r |= VT_MUSTBOUND;
7719 next();
7720 } else if (tok == '[') {
7721 next();
7722 gexpr();
7723 gen_op('+');
7724 indir();
7725 skip(']');
7726 } else if (tok == '(') {
7727 SValue ret;
7728 Sym *sa;
7729 int nb_args;
7731 /* function call */
7732 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7733 /* pointer test (no array accepted) */
7734 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7735 vtop->type = *pointed_type(&vtop->type);
7736 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7737 goto error_func;
7738 } else {
7739 error_func:
7740 expect("function pointer");
7742 } else {
7743 vtop->r &= ~VT_LVAL; /* no lvalue */
7745 /* get return type */
7746 s = vtop->type.ref;
7747 next();
7748 sa = s->next; /* first parameter */
7749 nb_args = 0;
7750 ret.r2 = VT_CONST;
7751 /* compute first implicit argument if a structure is returned */
7752 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7753 /* get some space for the returned structure */
7754 size = type_size(&s->type, &align);
7755 loc = (loc - size) & -align;
7756 ret.type = s->type;
7757 ret.r = VT_LOCAL | VT_LVAL;
7758 /* pass it as 'int' to avoid structure arg passing
7759 problems */
7760 vseti(VT_LOCAL, loc);
7761 ret.c = vtop->c;
7762 nb_args++;
7763 } else {
7764 ret.type = s->type;
7765 /* return in register */
7766 if (is_float(ret.type.t)) {
7767 ret.r = REG_FRET;
7768 } else {
7769 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7770 ret.r2 = REG_LRET;
7771 ret.r = REG_IRET;
7773 ret.c.i = 0;
7775 if (tok != ')') {
7776 for(;;) {
7777 expr_eq();
7778 gfunc_param_typed(s, sa);
7779 nb_args++;
7780 if (sa)
7781 sa = sa->next;
7782 if (tok == ')')
7783 break;
7784 skip(',');
7787 if (sa)
7788 error("too few arguments to function");
7789 skip(')');
7790 if (!nocode_wanted) {
7791 gfunc_call(nb_args);
7792 } else {
7793 vtop -= (nb_args + 1);
7795 /* return value */
7796 vsetc(&ret.type, ret.r, &ret.c);
7797 vtop->r2 = ret.r2;
7798 } else {
7799 break;
7804 static void uneq(void)
7806 int t;
7808 unary();
7809 if (tok == '=' ||
7810 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7811 tok == TOK_A_XOR || tok == TOK_A_OR ||
7812 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7813 test_lvalue();
7814 t = tok;
7815 next();
7816 if (t == '=') {
7817 expr_eq();
7818 } else {
7819 vdup();
7820 expr_eq();
7821 gen_op(t & 0x7f);
7823 vstore();
7827 static void expr_prod(void)
7829 int t;
7831 uneq();
7832 while (tok == '*' || tok == '/' || tok == '%') {
7833 t = tok;
7834 next();
7835 uneq();
7836 gen_op(t);
7840 static void expr_sum(void)
7842 int t;
7844 expr_prod();
7845 while (tok == '+' || tok == '-') {
7846 t = tok;
7847 next();
7848 expr_prod();
7849 gen_op(t);
7853 static void expr_shift(void)
7855 int t;
7857 expr_sum();
7858 while (tok == TOK_SHL || tok == TOK_SAR) {
7859 t = tok;
7860 next();
7861 expr_sum();
7862 gen_op(t);
7866 static void expr_cmp(void)
7868 int t;
7870 expr_shift();
7871 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7872 tok == TOK_ULT || tok == TOK_UGE) {
7873 t = tok;
7874 next();
7875 expr_shift();
7876 gen_op(t);
7880 static void expr_cmpeq(void)
7882 int t;
7884 expr_cmp();
7885 while (tok == TOK_EQ || tok == TOK_NE) {
7886 t = tok;
7887 next();
7888 expr_cmp();
7889 gen_op(t);
7893 static void expr_and(void)
7895 expr_cmpeq();
7896 while (tok == '&') {
7897 next();
7898 expr_cmpeq();
7899 gen_op('&');
7903 static void expr_xor(void)
7905 expr_and();
7906 while (tok == '^') {
7907 next();
7908 expr_and();
7909 gen_op('^');
7913 static void expr_or(void)
7915 expr_xor();
7916 while (tok == '|') {
7917 next();
7918 expr_xor();
7919 gen_op('|');
7923 /* XXX: fix this mess */
7924 static void expr_land_const(void)
7926 expr_or();
7927 while (tok == TOK_LAND) {
7928 next();
7929 expr_or();
7930 gen_op(TOK_LAND);
7934 /* XXX: fix this mess */
7935 static void expr_lor_const(void)
7937 expr_land_const();
7938 while (tok == TOK_LOR) {
7939 next();
7940 expr_land_const();
7941 gen_op(TOK_LOR);
7945 /* only used if non constant */
7946 static void expr_land(void)
7948 int t;
7950 expr_or();
7951 if (tok == TOK_LAND) {
7952 t = 0;
7953 save_regs(1);
7954 for(;;) {
7955 t = gtst(1, t);
7956 if (tok != TOK_LAND) {
7957 vseti(VT_JMPI, t);
7958 break;
7960 next();
7961 expr_or();
7966 static void expr_lor(void)
7968 int t;
7970 expr_land();
7971 if (tok == TOK_LOR) {
7972 t = 0;
7973 save_regs(1);
7974 for(;;) {
7975 t = gtst(0, t);
7976 if (tok != TOK_LOR) {
7977 vseti(VT_JMP, t);
7978 break;
7980 next();
7981 expr_land();
7986 /* XXX: better constant handling */
7987 static void expr_eq(void)
7989 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7990 SValue sv;
7991 CType type, type1, type2;
7993 if (const_wanted) {
7994 int c1, c;
7995 expr_lor_const();
7996 if (tok == '?') {
7997 c = vtop->c.i;
7998 vpop();
7999 next();
8000 if (tok == ':' && gnu_ext) {
8001 c1 = c;
8002 } else {
8003 gexpr();
8004 c1 = vtop->c.i;
8005 vpop();
8007 skip(':');
8008 expr_eq();
8009 if (c)
8010 vtop->c.i = c1;
8012 } else {
8013 expr_lor();
8014 if (tok == '?') {
8015 next();
8016 if (vtop != vstack) {
8017 /* needed to avoid having different registers saved in
8018 each branch */
8019 if (is_float(vtop->type.t))
8020 rc = RC_FLOAT;
8021 else
8022 rc = RC_INT;
8023 gv(rc);
8024 save_regs(1);
8026 if (tok == ':' && gnu_ext) {
8027 gv_dup();
8028 tt = gtst(1, 0);
8029 } else {
8030 tt = gtst(1, 0);
8031 gexpr();
8033 type1 = vtop->type;
8034 sv = *vtop; /* save value to handle it later */
8035 vtop--; /* no vpop so that FP stack is not flushed */
8036 skip(':');
8037 u = gjmp(0);
8038 gsym(tt);
8039 expr_eq();
8040 type2 = vtop->type;
8042 t1 = type1.t;
8043 bt1 = t1 & VT_BTYPE;
8044 t2 = type2.t;
8045 bt2 = t2 & VT_BTYPE;
8046 /* cast operands to correct type according to ISOC rules */
8047 if (is_float(bt1) || is_float(bt2)) {
8048 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8049 type.t = VT_LDOUBLE;
8050 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8051 type.t = VT_DOUBLE;
8052 } else {
8053 type.t = VT_FLOAT;
8055 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8056 /* cast to biggest op */
8057 type.t = VT_LLONG;
8058 /* convert to unsigned if it does not fit in a long long */
8059 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8060 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8061 type.t |= VT_UNSIGNED;
8062 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8063 /* XXX: test pointer compatibility */
8064 type = type1;
8065 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8066 /* XXX: test function pointer compatibility */
8067 type = type1;
8068 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8069 /* XXX: test structure compatibility */
8070 type = type1;
8071 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8072 /* NOTE: as an extension, we accept void on only one side */
8073 type.t = VT_VOID;
8074 } else {
8075 /* integer operations */
8076 type.t = VT_INT;
8077 /* convert to unsigned if it does not fit in an integer */
8078 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8079 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8080 type.t |= VT_UNSIGNED;
8083 /* now we convert second operand */
8084 gen_cast(&type);
8085 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8086 gaddrof();
8087 rc = RC_INT;
8088 if (is_float(type.t)) {
8089 rc = RC_FLOAT;
8090 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8091 /* for long longs, we use fixed registers to avoid having
8092 to handle a complicated move */
8093 rc = RC_IRET;
8096 r2 = gv(rc);
8097 /* this is horrible, but we must also convert first
8098 operand */
8099 tt = gjmp(0);
8100 gsym(u);
8101 /* put again first value and cast it */
8102 *vtop = sv;
8103 gen_cast(&type);
8104 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8105 gaddrof();
8106 r1 = gv(rc);
8107 move_reg(r2, r1);
8108 vtop->r = r2;
8109 gsym(tt);
8114 static void gexpr(void)
8116 while (1) {
8117 expr_eq();
8118 if (tok != ',')
8119 break;
8120 vpop();
8121 next();
8125 /* parse an expression and return its type without any side effect. */
8126 static void expr_type(CType *type)
8128 int saved_nocode_wanted;
8130 saved_nocode_wanted = nocode_wanted;
8131 nocode_wanted = 1;
8132 gexpr();
8133 *type = vtop->type;
8134 vpop();
8135 nocode_wanted = saved_nocode_wanted;
8138 /* parse a unary expression and return its type without any side
8139 effect. */
8140 static void unary_type(CType *type)
8142 int a;
8144 a = nocode_wanted;
8145 nocode_wanted = 1;
8146 unary();
8147 *type = vtop->type;
8148 vpop();
8149 nocode_wanted = a;
8152 /* parse a constant expression and return value in vtop. */
8153 static void expr_const1(void)
8155 int a;
8156 a = const_wanted;
8157 const_wanted = 1;
8158 expr_eq();
8159 const_wanted = a;
8162 /* parse an integer constant and return its value. */
8163 static int expr_const(void)
8165 int c;
8166 expr_const1();
8167 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8168 expect("constant expression");
8169 c = vtop->c.i;
8170 vpop();
8171 return c;
8174 /* return the label token if current token is a label, otherwise
8175 return zero */
8176 static int is_label(void)
8178 int last_tok;
8180 /* fast test first */
8181 if (tok < TOK_UIDENT)
8182 return 0;
8183 /* no need to save tokc because tok is an identifier */
8184 last_tok = tok;
8185 next();
8186 if (tok == ':') {
8187 next();
8188 return last_tok;
8189 } else {
8190 unget_tok(last_tok);
8191 return 0;
8195 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8196 int case_reg, int is_expr)
8198 int a, b, c, d;
8199 Sym *s;
8201 /* generate line number info */
8202 if (do_debug &&
8203 (last_line_num != file->line_num || last_ind != ind)) {
8204 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8205 last_ind = ind;
8206 last_line_num = file->line_num;
8209 if (is_expr) {
8210 /* default return value is (void) */
8211 vpushi(0);
8212 vtop->type.t = VT_VOID;
8215 if (tok == TOK_IF) {
8216 /* if test */
8217 next();
8218 skip('(');
8219 gexpr();
8220 skip(')');
8221 a = gtst(1, 0);
8222 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8223 c = tok;
8224 if (c == TOK_ELSE) {
8225 next();
8226 d = gjmp(0);
8227 gsym(a);
8228 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8229 gsym(d); /* patch else jmp */
8230 } else
8231 gsym(a);
8232 } else if (tok == TOK_WHILE) {
8233 next();
8234 d = ind;
8235 skip('(');
8236 gexpr();
8237 skip(')');
8238 a = gtst(1, 0);
8239 b = 0;
8240 block(&a, &b, case_sym, def_sym, case_reg, 0);
8241 gjmp_addr(d);
8242 gsym(a);
8243 gsym_addr(b, d);
8244 } else if (tok == '{') {
8245 Sym *llabel;
8247 next();
8248 /* record local declaration stack position */
8249 s = local_stack;
8250 llabel = local_label_stack;
8251 /* handle local labels declarations */
8252 if (tok == TOK_LABEL) {
8253 next();
8254 for(;;) {
8255 if (tok < TOK_UIDENT)
8256 expect("label identifier");
8257 label_push(&local_label_stack, tok, LABEL_DECLARED);
8258 next();
8259 if (tok == ',') {
8260 next();
8261 } else {
8262 skip(';');
8263 break;
8267 while (tok != '}') {
8268 decl(VT_LOCAL);
8269 if (tok != '}') {
8270 if (is_expr)
8271 vpop();
8272 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8275 /* pop locally defined labels */
8276 label_pop(&local_label_stack, llabel);
8277 /* pop locally defined symbols */
8278 if(is_expr) {
8279 /* XXX: this solution makes only valgrind happy...
8280 triggered by gcc.c-torture/execute/20000917-1.c */
8281 Sym *p;
8282 switch(vtop->type.t & VT_BTYPE) {
8283 case VT_PTR:
8284 case VT_STRUCT:
8285 case VT_ENUM:
8286 case VT_FUNC:
8287 for(p=vtop->type.ref;p;p=p->prev)
8288 if(p->prev==s)
8289 error("unsupported expression type");
8292 sym_pop(&local_stack, s);
8293 next();
8294 } else if (tok == TOK_RETURN) {
8295 next();
8296 if (tok != ';') {
8297 gexpr();
8298 gen_assign_cast(&func_vt);
8299 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8300 CType type;
8301 /* if returning structure, must copy it to implicit
8302 first pointer arg location */
8303 #ifdef TCC_ARM_EABI
8304 int align, size;
8305 size = type_size(&func_vt,&align);
8306 if(size <= 4)
8308 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8309 && (align & 3))
8311 int addr;
8312 loc = (loc - size) & -4;
8313 addr = loc;
8314 type = func_vt;
8315 vset(&type, VT_LOCAL | VT_LVAL, addr);
8316 vswap();
8317 vstore();
8318 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8320 vtop->type = int_type;
8321 gv(RC_IRET);
8322 } else {
8323 #endif
8324 type = func_vt;
8325 mk_pointer(&type);
8326 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8327 indir();
8328 vswap();
8329 /* copy structure value to pointer */
8330 vstore();
8331 #ifdef TCC_ARM_EABI
8333 #endif
8334 } else if (is_float(func_vt.t)) {
8335 gv(RC_FRET);
8336 } else {
8337 gv(RC_IRET);
8339 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8341 skip(';');
8342 rsym = gjmp(rsym); /* jmp */
8343 } else if (tok == TOK_BREAK) {
8344 /* compute jump */
8345 if (!bsym)
8346 error("cannot break");
8347 *bsym = gjmp(*bsym);
8348 next();
8349 skip(';');
8350 } else if (tok == TOK_CONTINUE) {
8351 /* compute jump */
8352 if (!csym)
8353 error("cannot continue");
8354 *csym = gjmp(*csym);
8355 next();
8356 skip(';');
8357 } else if (tok == TOK_FOR) {
8358 int e;
8359 next();
8360 skip('(');
8361 if (tok != ';') {
8362 gexpr();
8363 vpop();
8365 skip(';');
8366 d = ind;
8367 c = ind;
8368 a = 0;
8369 b = 0;
8370 if (tok != ';') {
8371 gexpr();
8372 a = gtst(1, 0);
8374 skip(';');
8375 if (tok != ')') {
8376 e = gjmp(0);
8377 c = ind;
8378 gexpr();
8379 vpop();
8380 gjmp_addr(d);
8381 gsym(e);
8383 skip(')');
8384 block(&a, &b, case_sym, def_sym, case_reg, 0);
8385 gjmp_addr(c);
8386 gsym(a);
8387 gsym_addr(b, c);
8388 } else
8389 if (tok == TOK_DO) {
8390 next();
8391 a = 0;
8392 b = 0;
8393 d = ind;
8394 block(&a, &b, case_sym, def_sym, case_reg, 0);
8395 skip(TOK_WHILE);
8396 skip('(');
8397 gsym(b);
8398 gexpr();
8399 c = gtst(0, 0);
8400 gsym_addr(c, d);
8401 skip(')');
8402 gsym(a);
8403 skip(';');
8404 } else
8405 if (tok == TOK_SWITCH) {
8406 next();
8407 skip('(');
8408 gexpr();
8409 /* XXX: other types than integer */
8410 case_reg = gv(RC_INT);
8411 vpop();
8412 skip(')');
8413 a = 0;
8414 b = gjmp(0); /* jump to first case */
8415 c = 0;
8416 block(&a, csym, &b, &c, case_reg, 0);
8417 /* if no default, jmp after switch */
8418 if (c == 0)
8419 c = ind;
8420 /* default label */
8421 gsym_addr(b, c);
8422 /* break label */
8423 gsym(a);
8424 } else
8425 if (tok == TOK_CASE) {
8426 int v1, v2;
8427 if (!case_sym)
8428 expect("switch");
8429 next();
8430 v1 = expr_const();
8431 v2 = v1;
8432 if (gnu_ext && tok == TOK_DOTS) {
8433 next();
8434 v2 = expr_const();
8435 if (v2 < v1)
8436 warning("empty case range");
8438 /* since a case is like a label, we must skip it with a jmp */
8439 b = gjmp(0);
8440 gsym(*case_sym);
8441 vseti(case_reg, 0);
8442 vpushi(v1);
8443 if (v1 == v2) {
8444 gen_op(TOK_EQ);
8445 *case_sym = gtst(1, 0);
8446 } else {
8447 gen_op(TOK_GE);
8448 *case_sym = gtst(1, 0);
8449 vseti(case_reg, 0);
8450 vpushi(v2);
8451 gen_op(TOK_LE);
8452 *case_sym = gtst(1, *case_sym);
8454 gsym(b);
8455 skip(':');
8456 is_expr = 0;
8457 goto block_after_label;
8458 } else
8459 if (tok == TOK_DEFAULT) {
8460 next();
8461 skip(':');
8462 if (!def_sym)
8463 expect("switch");
8464 if (*def_sym)
8465 error("too many 'default'");
8466 *def_sym = ind;
8467 is_expr = 0;
8468 goto block_after_label;
8469 } else
8470 if (tok == TOK_GOTO) {
8471 next();
8472 if (tok == '*' && gnu_ext) {
8473 /* computed goto */
8474 next();
8475 gexpr();
8476 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8477 expect("pointer");
8478 ggoto();
8479 } else if (tok >= TOK_UIDENT) {
8480 s = label_find(tok);
8481 /* put forward definition if needed */
8482 if (!s) {
8483 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8484 } else {
8485 if (s->r == LABEL_DECLARED)
8486 s->r = LABEL_FORWARD;
8488 /* label already defined */
8489 if (s->r & LABEL_FORWARD)
8490 s->next = (void *)gjmp((long)s->next);
8491 else
8492 gjmp_addr((long)s->next);
8493 next();
8494 } else {
8495 expect("label identifier");
8497 skip(';');
8498 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8499 asm_instr();
8500 } else {
8501 b = is_label();
8502 if (b) {
8503 /* label case */
8504 s = label_find(b);
8505 if (s) {
8506 if (s->r == LABEL_DEFINED)
8507 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8508 gsym((long)s->next);
8509 s->r = LABEL_DEFINED;
8510 } else {
8511 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8513 s->next = (void *)ind;
8514 /* we accept this, but it is a mistake */
8515 block_after_label:
8516 if (tok == '}') {
8517 warning("deprecated use of label at end of compound statement");
8518 } else {
8519 if (is_expr)
8520 vpop();
8521 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8523 } else {
8524 /* expression case */
8525 if (tok != ';') {
8526 if (is_expr) {
8527 vpop();
8528 gexpr();
8529 } else {
8530 gexpr();
8531 vpop();
8534 skip(';');
8539 /* t is the array or struct type. c is the array or struct
8540 address. cur_index/cur_field is the pointer to the current
8541 value. 'size_only' is true if only size info is needed (only used
8542 in arrays) */
8543 static void decl_designator(CType *type, Section *sec, unsigned long c,
8544 int *cur_index, Sym **cur_field,
8545 int size_only)
8547 Sym *s, *f;
8548 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8549 CType type1;
8551 notfirst = 0;
8552 elem_size = 0;
8553 nb_elems = 1;
8554 if (gnu_ext && (l = is_label()) != 0)
8555 goto struct_field;
8556 while (tok == '[' || tok == '.') {
8557 if (tok == '[') {
8558 if (!(type->t & VT_ARRAY))
8559 expect("array type");
8560 s = type->ref;
8561 next();
8562 index = expr_const();
8563 if (index < 0 || (s->c >= 0 && index >= s->c))
8564 expect("invalid index");
8565 if (tok == TOK_DOTS && gnu_ext) {
8566 next();
8567 index_last = expr_const();
8568 if (index_last < 0 ||
8569 (s->c >= 0 && index_last >= s->c) ||
8570 index_last < index)
8571 expect("invalid index");
8572 } else {
8573 index_last = index;
8575 skip(']');
8576 if (!notfirst)
8577 *cur_index = index_last;
8578 type = pointed_type(type);
8579 elem_size = type_size(type, &align);
8580 c += index * elem_size;
8581 /* NOTE: we only support ranges for last designator */
8582 nb_elems = index_last - index + 1;
8583 if (nb_elems != 1) {
8584 notfirst = 1;
8585 break;
8587 } else {
8588 next();
8589 l = tok;
8590 next();
8591 struct_field:
8592 if ((type->t & VT_BTYPE) != VT_STRUCT)
8593 expect("struct/union type");
8594 s = type->ref;
8595 l |= SYM_FIELD;
8596 f = s->next;
8597 while (f) {
8598 if (f->v == l)
8599 break;
8600 f = f->next;
8602 if (!f)
8603 expect("field");
8604 if (!notfirst)
8605 *cur_field = f;
8606 /* XXX: fix this mess by using explicit storage field */
8607 type1 = f->type;
8608 type1.t |= (type->t & ~VT_TYPE);
8609 type = &type1;
8610 c += f->c;
8612 notfirst = 1;
8614 if (notfirst) {
8615 if (tok == '=') {
8616 next();
8617 } else {
8618 if (!gnu_ext)
8619 expect("=");
8621 } else {
8622 if (type->t & VT_ARRAY) {
8623 index = *cur_index;
8624 type = pointed_type(type);
8625 c += index * type_size(type, &align);
8626 } else {
8627 f = *cur_field;
8628 if (!f)
8629 error("too many field init");
8630 /* XXX: fix this mess by using explicit storage field */
8631 type1 = f->type;
8632 type1.t |= (type->t & ~VT_TYPE);
8633 type = &type1;
8634 c += f->c;
8637 decl_initializer(type, sec, c, 0, size_only);
8639 /* XXX: make it more general */
8640 if (!size_only && nb_elems > 1) {
8641 unsigned long c_end;
8642 uint8_t *src, *dst;
8643 int i;
8645 if (!sec)
8646 error("range init not supported yet for dynamic storage");
8647 c_end = c + nb_elems * elem_size;
8648 if (c_end > sec->data_allocated)
8649 section_realloc(sec, c_end);
8650 src = sec->data + c;
8651 dst = src;
8652 for(i = 1; i < nb_elems; i++) {
8653 dst += elem_size;
8654 memcpy(dst, src, elem_size);
8659 #define EXPR_VAL 0
8660 #define EXPR_CONST 1
8661 #define EXPR_ANY 2
8663 /* store a value or an expression directly in global data or in local array */
8664 static void init_putv(CType *type, Section *sec, unsigned long c,
8665 int v, int expr_type)
8667 int saved_global_expr, bt, bit_pos, bit_size;
8668 void *ptr;
8669 unsigned long long bit_mask;
8670 CType dtype;
8672 switch(expr_type) {
8673 case EXPR_VAL:
8674 vpushi(v);
8675 break;
8676 case EXPR_CONST:
8677 /* compound literals must be allocated globally in this case */
8678 saved_global_expr = global_expr;
8679 global_expr = 1;
8680 expr_const1();
8681 global_expr = saved_global_expr;
8682 /* NOTE: symbols are accepted */
8683 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8684 error("initializer element is not constant");
8685 break;
8686 case EXPR_ANY:
8687 expr_eq();
8688 break;
8691 dtype = *type;
8692 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8694 if (sec) {
8695 /* XXX: not portable */
8696 /* XXX: generate error if incorrect relocation */
8697 gen_assign_cast(&dtype);
8698 bt = type->t & VT_BTYPE;
8699 ptr = sec->data + c;
8700 /* XXX: make code faster ? */
8701 if (!(type->t & VT_BITFIELD)) {
8702 bit_pos = 0;
8703 bit_size = 32;
8704 bit_mask = -1LL;
8705 } else {
8706 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8707 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8708 bit_mask = (1LL << bit_size) - 1;
8710 if ((vtop->r & VT_SYM) &&
8711 (bt == VT_BYTE ||
8712 bt == VT_SHORT ||
8713 bt == VT_DOUBLE ||
8714 bt == VT_LDOUBLE ||
8715 bt == VT_LLONG ||
8716 (bt == VT_INT && bit_size != 32)))
8717 error("initializer element is not computable at load time");
8718 switch(bt) {
8719 case VT_BOOL:
8720 vtop->c.i = (vtop->c.i != 0);
8721 case VT_BYTE:
8722 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8723 break;
8724 case VT_SHORT:
8725 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8726 break;
8727 case VT_DOUBLE:
8728 *(double *)ptr = vtop->c.d;
8729 break;
8730 case VT_LDOUBLE:
8731 *(long double *)ptr = vtop->c.ld;
8732 break;
8733 case VT_LLONG:
8734 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8735 break;
8736 default:
8737 if (vtop->r & VT_SYM) {
8738 greloc(sec, vtop->sym, c, R_DATA_32);
8740 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8741 break;
8743 vtop--;
8744 } else {
8745 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8746 vswap();
8747 vstore();
8748 vpop();
8752 /* put zeros for variable based init */
8753 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8755 if (sec) {
8756 /* nothing to do because globals are already set to zero */
8757 } else {
8758 vpush_global_sym(&func_old_type, TOK_memset);
8759 vseti(VT_LOCAL, c);
8760 vpushi(0);
8761 vpushi(size);
8762 gfunc_call(3);
8766 /* 't' contains the type and storage info. 'c' is the offset of the
8767 object in section 'sec'. If 'sec' is NULL, it means stack based
8768 allocation. 'first' is true if array '{' must be read (multi
8769 dimension implicit array init handling). 'size_only' is true if
8770 size only evaluation is wanted (only for arrays). */
8771 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8772 int first, int size_only)
8774 int index, array_length, n, no_oblock, nb, parlevel, i;
8775 int size1, align1, expr_type;
8776 Sym *s, *f;
8777 CType *t1;
8779 if (type->t & VT_ARRAY) {
8780 s = type->ref;
8781 n = s->c;
8782 array_length = 0;
8783 t1 = pointed_type(type);
8784 size1 = type_size(t1, &align1);
8786 no_oblock = 1;
8787 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8788 tok == '{') {
8789 skip('{');
8790 no_oblock = 0;
8793 /* only parse strings here if correct type (otherwise: handle
8794 them as ((w)char *) expressions */
8795 if ((tok == TOK_LSTR &&
8796 #ifdef TCC_TARGET_PE
8797 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8798 #else
8799 (t1->t & VT_BTYPE) == VT_INT
8800 #endif
8801 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8802 while (tok == TOK_STR || tok == TOK_LSTR) {
8803 int cstr_len, ch;
8804 CString *cstr;
8806 cstr = tokc.cstr;
8807 /* compute maximum number of chars wanted */
8808 if (tok == TOK_STR)
8809 cstr_len = cstr->size;
8810 else
8811 cstr_len = cstr->size / sizeof(nwchar_t);
8812 cstr_len--;
8813 nb = cstr_len;
8814 if (n >= 0 && nb > (n - array_length))
8815 nb = n - array_length;
8816 if (!size_only) {
8817 if (cstr_len > nb)
8818 warning("initializer-string for array is too long");
8819 /* in order to go faster for common case (char
8820 string in global variable, we handle it
8821 specifically */
8822 if (sec && tok == TOK_STR && size1 == 1) {
8823 memcpy(sec->data + c + array_length, cstr->data, nb);
8824 } else {
8825 for(i=0;i<nb;i++) {
8826 if (tok == TOK_STR)
8827 ch = ((unsigned char *)cstr->data)[i];
8828 else
8829 ch = ((nwchar_t *)cstr->data)[i];
8830 init_putv(t1, sec, c + (array_length + i) * size1,
8831 ch, EXPR_VAL);
8835 array_length += nb;
8836 next();
8838 /* only add trailing zero if enough storage (no
8839 warning in this case since it is standard) */
8840 if (n < 0 || array_length < n) {
8841 if (!size_only) {
8842 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8844 array_length++;
8846 } else {
8847 index = 0;
8848 while (tok != '}') {
8849 decl_designator(type, sec, c, &index, NULL, size_only);
8850 if (n >= 0 && index >= n)
8851 error("index too large");
8852 /* must put zero in holes (note that doing it that way
8853 ensures that it even works with designators) */
8854 if (!size_only && array_length < index) {
8855 init_putz(t1, sec, c + array_length * size1,
8856 (index - array_length) * size1);
8858 index++;
8859 if (index > array_length)
8860 array_length = index;
8861 /* special test for multi dimensional arrays (may not
8862 be strictly correct if designators are used at the
8863 same time) */
8864 if (index >= n && no_oblock)
8865 break;
8866 if (tok == '}')
8867 break;
8868 skip(',');
8871 if (!no_oblock)
8872 skip('}');
8873 /* put zeros at the end */
8874 if (!size_only && n >= 0 && array_length < n) {
8875 init_putz(t1, sec, c + array_length * size1,
8876 (n - array_length) * size1);
8878 /* patch type size if needed */
8879 if (n < 0)
8880 s->c = array_length;
8881 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8882 (sec || !first || tok == '{')) {
8883 int par_count;
8885 /* NOTE: the previous test is a specific case for automatic
8886 struct/union init */
8887 /* XXX: union needs only one init */
8889 /* XXX: this test is incorrect for local initializers
8890 beginning with ( without {. It would be much more difficult
8891 to do it correctly (ideally, the expression parser should
8892 be used in all cases) */
8893 par_count = 0;
8894 if (tok == '(') {
8895 AttributeDef ad1;
8896 CType type1;
8897 next();
8898 while (tok == '(') {
8899 par_count++;
8900 next();
8902 if (!parse_btype(&type1, &ad1))
8903 expect("cast");
8904 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8905 #if 0
8906 if (!is_assignable_types(type, &type1))
8907 error("invalid type for cast");
8908 #endif
8909 skip(')');
8911 no_oblock = 1;
8912 if (first || tok == '{') {
8913 skip('{');
8914 no_oblock = 0;
8916 s = type->ref;
8917 f = s->next;
8918 array_length = 0;
8919 index = 0;
8920 n = s->c;
8921 while (tok != '}') {
8922 decl_designator(type, sec, c, NULL, &f, size_only);
8923 index = f->c;
8924 if (!size_only && array_length < index) {
8925 init_putz(type, sec, c + array_length,
8926 index - array_length);
8928 index = index + type_size(&f->type, &align1);
8929 if (index > array_length)
8930 array_length = index;
8931 f = f->next;
8932 if (no_oblock && f == NULL)
8933 break;
8934 if (tok == '}')
8935 break;
8936 skip(',');
8938 /* put zeros at the end */
8939 if (!size_only && array_length < n) {
8940 init_putz(type, sec, c + array_length,
8941 n - array_length);
8943 if (!no_oblock)
8944 skip('}');
8945 while (par_count) {
8946 skip(')');
8947 par_count--;
8949 } else if (tok == '{') {
8950 next();
8951 decl_initializer(type, sec, c, first, size_only);
8952 skip('}');
8953 } else if (size_only) {
8954 /* just skip expression */
8955 parlevel = 0;
8956 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8957 tok != -1) {
8958 if (tok == '(')
8959 parlevel++;
8960 else if (tok == ')')
8961 parlevel--;
8962 next();
8964 } else {
8965 /* currently, we always use constant expression for globals
8966 (may change for scripting case) */
8967 expr_type = EXPR_CONST;
8968 if (!sec)
8969 expr_type = EXPR_ANY;
8970 init_putv(type, sec, c, 0, expr_type);
8974 /* parse an initializer for type 't' if 'has_init' is non zero, and
8975 allocate space in local or global data space ('r' is either
8976 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8977 variable 'v' of scope 'scope' is declared before initializers are
8978 parsed. If 'v' is zero, then a reference to the new object is put
8979 in the value stack. If 'has_init' is 2, a special parsing is done
8980 to handle string constants. */
8981 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8982 int has_init, int v, int scope)
8984 int size, align, addr, data_offset;
8985 int level;
8986 ParseState saved_parse_state;
8987 TokenString init_str;
8988 Section *sec;
8990 size = type_size(type, &align);
8991 /* If unknown size, we must evaluate it before
8992 evaluating initializers because
8993 initializers can generate global data too
8994 (e.g. string pointers or ISOC99 compound
8995 literals). It also simplifies local
8996 initializers handling */
8997 tok_str_new(&init_str);
8998 if (size < 0) {
8999 if (!has_init)
9000 error("unknown type size");
9001 /* get all init string */
9002 if (has_init == 2) {
9003 /* only get strings */
9004 while (tok == TOK_STR || tok == TOK_LSTR) {
9005 tok_str_add_tok(&init_str);
9006 next();
9008 } else {
9009 level = 0;
9010 while (level > 0 || (tok != ',' && tok != ';')) {
9011 if (tok < 0)
9012 error("unexpected end of file in initializer");
9013 tok_str_add_tok(&init_str);
9014 if (tok == '{')
9015 level++;
9016 else if (tok == '}') {
9017 if (level == 0)
9018 break;
9019 level--;
9021 next();
9024 tok_str_add(&init_str, -1);
9025 tok_str_add(&init_str, 0);
9027 /* compute size */
9028 save_parse_state(&saved_parse_state);
9030 macro_ptr = init_str.str;
9031 next();
9032 decl_initializer(type, NULL, 0, 1, 1);
9033 /* prepare second initializer parsing */
9034 macro_ptr = init_str.str;
9035 next();
9037 /* if still unknown size, error */
9038 size = type_size(type, &align);
9039 if (size < 0)
9040 error("unknown type size");
9042 /* take into account specified alignment if bigger */
9043 if (ad->aligned) {
9044 if (ad->aligned > align)
9045 align = ad->aligned;
9046 } else if (ad->packed) {
9047 align = 1;
9049 if ((r & VT_VALMASK) == VT_LOCAL) {
9050 sec = NULL;
9051 if (do_bounds_check && (type->t & VT_ARRAY))
9052 loc--;
9053 loc = (loc - size) & -align;
9054 addr = loc;
9055 /* handles bounds */
9056 /* XXX: currently, since we do only one pass, we cannot track
9057 '&' operators, so we add only arrays */
9058 if (do_bounds_check && (type->t & VT_ARRAY)) {
9059 unsigned long *bounds_ptr;
9060 /* add padding between regions */
9061 loc--;
9062 /* then add local bound info */
9063 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9064 bounds_ptr[0] = addr;
9065 bounds_ptr[1] = size;
9067 if (v) {
9068 /* local variable */
9069 sym_push(v, type, r, addr);
9070 } else {
9071 /* push local reference */
9072 vset(type, r, addr);
9074 } else {
9075 Sym *sym;
9077 sym = NULL;
9078 if (v && scope == VT_CONST) {
9079 /* see if the symbol was already defined */
9080 sym = sym_find(v);
9081 if (sym) {
9082 if (!is_compatible_types(&sym->type, type))
9083 error("incompatible types for redefinition of '%s'",
9084 get_tok_str(v, NULL));
9085 if (sym->type.t & VT_EXTERN) {
9086 /* if the variable is extern, it was not allocated */
9087 sym->type.t &= ~VT_EXTERN;
9088 /* set array size if it was ommited in extern
9089 declaration */
9090 if ((sym->type.t & VT_ARRAY) &&
9091 sym->type.ref->c < 0 &&
9092 type->ref->c >= 0)
9093 sym->type.ref->c = type->ref->c;
9094 } else {
9095 /* we accept several definitions of the same
9096 global variable. this is tricky, because we
9097 must play with the SHN_COMMON type of the symbol */
9098 /* XXX: should check if the variable was already
9099 initialized. It is incorrect to initialized it
9100 twice */
9101 /* no init data, we won't add more to the symbol */
9102 if (!has_init)
9103 goto no_alloc;
9108 /* allocate symbol in corresponding section */
9109 sec = ad->section;
9110 if (!sec) {
9111 if (has_init)
9112 sec = data_section;
9113 else if (tcc_state->nocommon)
9114 sec = bss_section;
9116 if (sec) {
9117 data_offset = sec->data_offset;
9118 data_offset = (data_offset + align - 1) & -align;
9119 addr = data_offset;
9120 /* very important to increment global pointer at this time
9121 because initializers themselves can create new initializers */
9122 data_offset += size;
9123 /* add padding if bound check */
9124 if (do_bounds_check)
9125 data_offset++;
9126 sec->data_offset = data_offset;
9127 /* allocate section space to put the data */
9128 if (sec->sh_type != SHT_NOBITS &&
9129 data_offset > sec->data_allocated)
9130 section_realloc(sec, data_offset);
9131 /* align section if needed */
9132 if (align > sec->sh_addralign)
9133 sec->sh_addralign = align;
9134 } else {
9135 addr = 0; /* avoid warning */
9138 if (v) {
9139 if (scope != VT_CONST || !sym) {
9140 sym = sym_push(v, type, r | VT_SYM, 0);
9142 /* update symbol definition */
9143 if (sec) {
9144 put_extern_sym(sym, sec, addr, size);
9145 } else {
9146 Elf32_Sym *esym;
9147 /* put a common area */
9148 put_extern_sym(sym, NULL, align, size);
9149 /* XXX: find a nicer way */
9150 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
9151 esym->st_shndx = SHN_COMMON;
9153 } else {
9154 CValue cval;
9156 /* push global reference */
9157 sym = get_sym_ref(type, sec, addr, size);
9158 cval.ul = 0;
9159 vsetc(type, VT_CONST | VT_SYM, &cval);
9160 vtop->sym = sym;
9163 /* handles bounds now because the symbol must be defined
9164 before for the relocation */
9165 if (do_bounds_check) {
9166 unsigned long *bounds_ptr;
9168 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9169 /* then add global bound info */
9170 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9171 bounds_ptr[0] = 0; /* relocated */
9172 bounds_ptr[1] = size;
9175 if (has_init) {
9176 decl_initializer(type, sec, addr, 1, 0);
9177 /* restore parse state if needed */
9178 if (init_str.str) {
9179 tok_str_free(init_str.str);
9180 restore_parse_state(&saved_parse_state);
9183 no_alloc: ;
9186 void put_func_debug(Sym *sym)
9188 char buf[512];
9190 /* stabs info */
9191 /* XXX: we put here a dummy type */
9192 snprintf(buf, sizeof(buf), "%s:%c1",
9193 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9194 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9195 cur_text_section, sym->c);
9196 /* //gr gdb wants a line at the function */
9197 put_stabn(N_SLINE, 0, file->line_num, 0);
9198 last_ind = 0;
9199 last_line_num = 0;
9202 /* parse an old style function declaration list */
9203 /* XXX: check multiple parameter */
9204 static void func_decl_list(Sym *func_sym)
9206 AttributeDef ad;
9207 int v;
9208 Sym *s;
9209 CType btype, type;
9211 /* parse each declaration */
9212 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9213 if (!parse_btype(&btype, &ad))
9214 expect("declaration list");
9215 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9216 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9217 tok == ';') {
9218 /* we accept no variable after */
9219 } else {
9220 for(;;) {
9221 type = btype;
9222 type_decl(&type, &ad, &v, TYPE_DIRECT);
9223 /* find parameter in function parameter list */
9224 s = func_sym->next;
9225 while (s != NULL) {
9226 if ((s->v & ~SYM_FIELD) == v)
9227 goto found;
9228 s = s->next;
9230 error("declaration for parameter '%s' but no such parameter",
9231 get_tok_str(v, NULL));
9232 found:
9233 /* check that no storage specifier except 'register' was given */
9234 if (type.t & VT_STORAGE)
9235 error("storage class specified for '%s'", get_tok_str(v, NULL));
9236 convert_parameter_type(&type);
9237 /* we can add the type (NOTE: it could be local to the function) */
9238 s->type = type;
9239 /* accept other parameters */
9240 if (tok == ',')
9241 next();
9242 else
9243 break;
9246 skip(';');
9250 /* parse a function defined by symbol 'sym' and generate its code in
9251 'cur_text_section' */
9252 static void gen_function(Sym *sym)
9254 int saved_nocode_wanted = nocode_wanted;
9255 nocode_wanted = 0;
9256 ind = cur_text_section->data_offset;
9257 /* NOTE: we patch the symbol size later */
9258 put_extern_sym(sym, cur_text_section, ind, 0);
9259 funcname = get_tok_str(sym->v, NULL);
9260 func_ind = ind;
9261 /* put debug symbol */
9262 if (do_debug)
9263 put_func_debug(sym);
9264 /* push a dummy symbol to enable local sym storage */
9265 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9266 gfunc_prolog(&sym->type);
9267 rsym = 0;
9268 block(NULL, NULL, NULL, NULL, 0, 0);
9269 gsym(rsym);
9270 gfunc_epilog();
9271 cur_text_section->data_offset = ind;
9272 label_pop(&global_label_stack, NULL);
9273 sym_pop(&local_stack, NULL); /* reset local stack */
9274 /* end of function */
9275 /* patch symbol size */
9276 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
9277 ind - func_ind;
9278 if (do_debug) {
9279 put_stabn(N_FUN, 0, 0, ind - func_ind);
9281 /* It's better to crash than to generate wrong code */
9282 cur_text_section = NULL;
9283 funcname = ""; /* for safety */
9284 func_vt.t = VT_VOID; /* for safety */
9285 ind = 0; /* for safety */
9286 nocode_wanted = saved_nocode_wanted;
9289 static void gen_inline_functions(void)
9291 Sym *sym;
9292 CType *type;
9293 int *str, inline_generated;
9295 /* iterate while inline function are referenced */
9296 for(;;) {
9297 inline_generated = 0;
9298 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9299 type = &sym->type;
9300 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9301 (type->t & (VT_STATIC | VT_INLINE)) ==
9302 (VT_STATIC | VT_INLINE) &&
9303 sym->c != 0) {
9304 /* the function was used: generate its code and
9305 convert it to a normal function */
9306 str = INLINE_DEF(sym->r);
9307 sym->r = VT_SYM | VT_CONST;
9308 sym->type.t &= ~VT_INLINE;
9310 macro_ptr = str;
9311 next();
9312 cur_text_section = text_section;
9313 gen_function(sym);
9314 macro_ptr = NULL; /* fail safe */
9316 tok_str_free(str);
9317 inline_generated = 1;
9320 if (!inline_generated)
9321 break;
9324 /* free all remaining inline function tokens */
9325 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9326 type = &sym->type;
9327 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9328 (type->t & (VT_STATIC | VT_INLINE)) ==
9329 (VT_STATIC | VT_INLINE)) {
9330 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9331 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9332 continue;
9333 str = INLINE_DEF(sym->r);
9334 tok_str_free(str);
9335 sym->r = 0; /* fail safe */
9340 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9341 static void decl(int l)
9343 int v, has_init, r;
9344 CType type, btype;
9345 Sym *sym;
9346 AttributeDef ad;
9348 while (1) {
9349 if (!parse_btype(&btype, &ad)) {
9350 /* skip redundant ';' */
9351 /* XXX: find more elegant solution */
9352 if (tok == ';') {
9353 next();
9354 continue;
9356 if (l == VT_CONST &&
9357 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9358 /* global asm block */
9359 asm_global_instr();
9360 continue;
9362 /* special test for old K&R protos without explicit int
9363 type. Only accepted when defining global data */
9364 if (l == VT_LOCAL || tok < TOK_DEFINE)
9365 break;
9366 btype.t = VT_INT;
9368 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9369 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9370 tok == ';') {
9371 /* we accept no variable after */
9372 next();
9373 continue;
9375 while (1) { /* iterate thru each declaration */
9376 type = btype;
9377 type_decl(&type, &ad, &v, TYPE_DIRECT);
9378 #if 0
9380 char buf[500];
9381 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9382 printf("type = '%s'\n", buf);
9384 #endif
9385 if ((type.t & VT_BTYPE) == VT_FUNC) {
9386 /* if old style function prototype, we accept a
9387 declaration list */
9388 sym = type.ref;
9389 if (sym->c == FUNC_OLD)
9390 func_decl_list(sym);
9393 if (tok == '{') {
9394 if (l == VT_LOCAL)
9395 error("cannot use local functions");
9396 if ((type.t & VT_BTYPE) != VT_FUNC)
9397 expect("function definition");
9399 /* reject abstract declarators in function definition */
9400 sym = type.ref;
9401 while ((sym = sym->next) != NULL)
9402 if (!(sym->v & ~SYM_FIELD))
9403 expect("identifier");
9405 /* XXX: cannot do better now: convert extern line to static inline */
9406 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9407 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9409 sym = sym_find(v);
9410 if (sym) {
9411 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9412 goto func_error1;
9413 /* specific case: if not func_call defined, we put
9414 the one of the prototype */
9415 /* XXX: should have default value */
9416 r = sym->type.ref->r;
9417 if (FUNC_CALL(r) != FUNC_CDECL
9418 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9419 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9420 if (FUNC_EXPORT(r))
9421 FUNC_EXPORT(type.ref->r) = 1;
9423 if (!is_compatible_types(&sym->type, &type)) {
9424 func_error1:
9425 error("incompatible types for redefinition of '%s'",
9426 get_tok_str(v, NULL));
9428 /* if symbol is already defined, then put complete type */
9429 sym->type = type;
9430 } else {
9431 /* put function symbol */
9432 sym = global_identifier_push(v, type.t, 0);
9433 sym->type.ref = type.ref;
9436 /* static inline functions are just recorded as a kind
9437 of macro. Their code will be emitted at the end of
9438 the compilation unit only if they are used */
9439 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9440 (VT_INLINE | VT_STATIC)) {
9441 TokenString func_str;
9442 int block_level;
9444 tok_str_new(&func_str);
9446 block_level = 0;
9447 for(;;) {
9448 int t;
9449 if (tok == TOK_EOF)
9450 error("unexpected end of file");
9451 tok_str_add_tok(&func_str);
9452 t = tok;
9453 next();
9454 if (t == '{') {
9455 block_level++;
9456 } else if (t == '}') {
9457 block_level--;
9458 if (block_level == 0)
9459 break;
9462 tok_str_add(&func_str, -1);
9463 tok_str_add(&func_str, 0);
9464 INLINE_DEF(sym->r) = func_str.str;
9465 } else {
9466 /* compute text section */
9467 cur_text_section = ad.section;
9468 if (!cur_text_section)
9469 cur_text_section = text_section;
9470 sym->r = VT_SYM | VT_CONST;
9471 gen_function(sym);
9473 break;
9474 } else {
9475 if (btype.t & VT_TYPEDEF) {
9476 /* save typedefed type */
9477 /* XXX: test storage specifiers ? */
9478 sym = sym_push(v, &type, 0, 0);
9479 sym->type.t |= VT_TYPEDEF;
9480 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9481 /* external function definition */
9482 /* specific case for func_call attribute */
9483 if (ad.func_attr)
9484 type.ref->r = ad.func_attr;
9485 external_sym(v, &type, 0);
9486 } else {
9487 /* not lvalue if array */
9488 r = 0;
9489 if (!(type.t & VT_ARRAY))
9490 r |= lvalue_type(type.t);
9491 has_init = (tok == '=');
9492 if ((btype.t & VT_EXTERN) ||
9493 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9494 !has_init && l == VT_CONST && type.ref->c < 0)) {
9495 /* external variable */
9496 /* NOTE: as GCC, uninitialized global static
9497 arrays of null size are considered as
9498 extern */
9499 external_sym(v, &type, r);
9500 } else {
9501 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9502 if (type.t & VT_STATIC)
9503 r |= VT_CONST;
9504 else
9505 r |= l;
9506 if (has_init)
9507 next();
9508 decl_initializer_alloc(&type, &ad, r,
9509 has_init, v, l);
9512 if (tok != ',') {
9513 skip(';');
9514 break;
9516 next();
9522 /* better than nothing, but needs extension to handle '-E' option
9523 correctly too */
9524 static void preprocess_init(TCCState *s1)
9526 s1->include_stack_ptr = s1->include_stack;
9527 /* XXX: move that before to avoid having to initialize
9528 file->ifdef_stack_ptr ? */
9529 s1->ifdef_stack_ptr = s1->ifdef_stack;
9530 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9532 /* XXX: not ANSI compliant: bound checking says error */
9533 vtop = vstack - 1;
9534 s1->pack_stack[0] = 0;
9535 s1->pack_stack_ptr = s1->pack_stack;
9538 /* compile the C file opened in 'file'. Return non zero if errors. */
9539 static int tcc_compile(TCCState *s1)
9541 Sym *define_start;
9542 char buf[512];
9543 volatile int section_sym;
9545 #ifdef INC_DEBUG
9546 printf("%s: **** new file\n", file->filename);
9547 #endif
9548 preprocess_init(s1);
9550 cur_text_section = NULL;
9551 funcname = "";
9552 anon_sym = SYM_FIRST_ANOM;
9554 /* file info: full path + filename */
9555 section_sym = 0; /* avoid warning */
9556 if (do_debug) {
9557 section_sym = put_elf_sym(symtab_section, 0, 0,
9558 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
9559 text_section->sh_num, NULL);
9560 getcwd(buf, sizeof(buf));
9561 #ifdef _WIN32
9562 normalize_slashes(buf);
9563 #endif
9564 pstrcat(buf, sizeof(buf), "/");
9565 put_stabs_r(buf, N_SO, 0, 0,
9566 text_section->data_offset, text_section, section_sym);
9567 put_stabs_r(file->filename, N_SO, 0, 0,
9568 text_section->data_offset, text_section, section_sym);
9570 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9571 symbols can be safely used */
9572 put_elf_sym(symtab_section, 0, 0,
9573 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
9574 SHN_ABS, file->filename);
9576 /* define some often used types */
9577 int_type.t = VT_INT;
9579 char_pointer_type.t = VT_BYTE;
9580 mk_pointer(&char_pointer_type);
9582 func_old_type.t = VT_FUNC;
9583 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9585 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9586 float_type.t = VT_FLOAT;
9587 double_type.t = VT_DOUBLE;
9589 func_float_type.t = VT_FUNC;
9590 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9591 func_double_type.t = VT_FUNC;
9592 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9593 #endif
9595 #if 0
9596 /* define 'void *alloca(unsigned int)' builtin function */
9598 Sym *s1;
9600 p = anon_sym++;
9601 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9602 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9603 s1->next = NULL;
9604 sym->next = s1;
9605 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9607 #endif
9609 define_start = define_stack;
9610 nocode_wanted = 1;
9612 if (setjmp(s1->error_jmp_buf) == 0) {
9613 s1->nb_errors = 0;
9614 s1->error_set_jmp_enabled = 1;
9616 ch = file->buf_ptr[0];
9617 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9618 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9619 next();
9620 decl(VT_CONST);
9621 if (tok != TOK_EOF)
9622 expect("declaration");
9624 /* end of translation unit info */
9625 if (do_debug) {
9626 put_stabs_r(NULL, N_SO, 0, 0,
9627 text_section->data_offset, text_section, section_sym);
9630 s1->error_set_jmp_enabled = 0;
9632 /* reset define stack, but leave -Dsymbols (may be incorrect if
9633 they are undefined) */
9634 free_defines(define_start);
9636 gen_inline_functions();
9638 sym_pop(&global_stack, NULL);
9639 sym_pop(&local_stack, NULL);
9641 return s1->nb_errors != 0 ? -1 : 0;
9644 /* Preprocess the current file */
9645 /* XXX: add line and file infos, add options to preserve spaces */
9646 static int tcc_preprocess(TCCState *s1)
9648 Sym *define_start;
9649 int last_is_space;
9651 preprocess_init(s1);
9653 define_start = define_stack;
9655 ch = file->buf_ptr[0];
9656 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9657 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9658 PARSE_FLAG_LINEFEED;
9659 last_is_space = 1;
9660 next();
9661 for(;;) {
9662 if (tok == TOK_EOF) {
9663 break;
9664 } else if (tok == TOK_LINEFEED) {
9665 last_is_space = 1;
9666 } else {
9667 if (!last_is_space)
9668 fputc(' ', s1->outfile);
9669 last_is_space = 0;
9671 fputs(get_tok_str(tok, &tokc), s1->outfile);
9672 next();
9674 free_defines(define_start);
9675 return 0;
9678 #ifdef LIBTCC
9679 int tcc_compile_string(TCCState *s, const char *str)
9681 BufferedFile bf1, *bf = &bf1;
9682 int ret, len;
9683 char *buf;
9685 /* init file structure */
9686 bf->fd = -1;
9687 /* XXX: avoid copying */
9688 len = strlen(str);
9689 buf = tcc_malloc(len + 1);
9690 if (!buf)
9691 return -1;
9692 memcpy(buf, str, len);
9693 buf[len] = CH_EOB;
9694 bf->buf_ptr = buf;
9695 bf->buf_end = buf + len;
9696 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9697 bf->line_num = 1;
9698 file = bf;
9699 ret = tcc_compile(s);
9700 file = NULL;
9701 tcc_free(buf);
9703 /* currently, no need to close */
9704 return ret;
9706 #endif
9708 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9709 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9711 BufferedFile bf1, *bf = &bf1;
9713 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9714 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9715 /* default value */
9716 if (!value)
9717 value = "1";
9718 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9720 /* init file structure */
9721 bf->fd = -1;
9722 bf->buf_ptr = bf->buffer;
9723 bf->buf_end = bf->buffer + strlen(bf->buffer);
9724 *bf->buf_end = CH_EOB;
9725 bf->filename[0] = '\0';
9726 bf->line_num = 1;
9727 file = bf;
9729 s1->include_stack_ptr = s1->include_stack;
9731 /* parse with define parser */
9732 ch = file->buf_ptr[0];
9733 next_nomacro();
9734 parse_define();
9735 file = NULL;
9738 /* undefine a preprocessor symbol */
9739 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9741 TokenSym *ts;
9742 Sym *s;
9743 ts = tok_alloc(sym, strlen(sym));
9744 s = define_find(ts->tok);
9745 /* undefine symbol by putting an invalid name */
9746 if (s)
9747 define_undef(s);
9750 #ifdef CONFIG_TCC_ASM
9752 #ifdef TCC_TARGET_I386
9753 #include "i386-asm.c"
9754 #endif
9755 #include "tccasm.c"
9757 #else
9758 static void asm_instr(void)
9760 error("inline asm() not supported");
9762 static void asm_global_instr(void)
9764 error("inline asm() not supported");
9766 #endif
9768 #include "tccelf.c"
9770 #ifdef TCC_TARGET_COFF
9771 #include "tcccoff.c"
9772 #endif
9774 #ifdef TCC_TARGET_PE
9775 #include "tccpe.c"
9776 #endif
9778 /* print the position in the source file of PC value 'pc' by reading
9779 the stabs debug information */
9780 static void rt_printline(unsigned long wanted_pc)
9782 Stab_Sym *sym, *sym_end;
9783 char func_name[128], last_func_name[128];
9784 unsigned long func_addr, last_pc, pc;
9785 const char *incl_files[INCLUDE_STACK_SIZE];
9786 int incl_index, len, last_line_num, i;
9787 const char *str, *p;
9789 fprintf(stderr, "0x%08lx:", wanted_pc);
9791 func_name[0] = '\0';
9792 func_addr = 0;
9793 incl_index = 0;
9794 last_func_name[0] = '\0';
9795 last_pc = 0xffffffff;
9796 last_line_num = 1;
9797 sym = (Stab_Sym *)stab_section->data + 1;
9798 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9799 while (sym < sym_end) {
9800 switch(sym->n_type) {
9801 /* function start or end */
9802 case N_FUN:
9803 if (sym->n_strx == 0) {
9804 /* we test if between last line and end of function */
9805 pc = sym->n_value + func_addr;
9806 if (wanted_pc >= last_pc && wanted_pc < pc)
9807 goto found;
9808 func_name[0] = '\0';
9809 func_addr = 0;
9810 } else {
9811 str = stabstr_section->data + sym->n_strx;
9812 p = strchr(str, ':');
9813 if (!p) {
9814 pstrcpy(func_name, sizeof(func_name), str);
9815 } else {
9816 len = p - str;
9817 if (len > sizeof(func_name) - 1)
9818 len = sizeof(func_name) - 1;
9819 memcpy(func_name, str, len);
9820 func_name[len] = '\0';
9822 func_addr = sym->n_value;
9824 break;
9825 /* line number info */
9826 case N_SLINE:
9827 pc = sym->n_value + func_addr;
9828 if (wanted_pc >= last_pc && wanted_pc < pc)
9829 goto found;
9830 last_pc = pc;
9831 last_line_num = sym->n_desc;
9832 /* XXX: slow! */
9833 strcpy(last_func_name, func_name);
9834 break;
9835 /* include files */
9836 case N_BINCL:
9837 str = stabstr_section->data + sym->n_strx;
9838 add_incl:
9839 if (incl_index < INCLUDE_STACK_SIZE) {
9840 incl_files[incl_index++] = str;
9842 break;
9843 case N_EINCL:
9844 if (incl_index > 1)
9845 incl_index--;
9846 break;
9847 case N_SO:
9848 if (sym->n_strx == 0) {
9849 incl_index = 0; /* end of translation unit */
9850 } else {
9851 str = stabstr_section->data + sym->n_strx;
9852 /* do not add path */
9853 len = strlen(str);
9854 if (len > 0 && str[len - 1] != '/')
9855 goto add_incl;
9857 break;
9859 sym++;
9862 /* second pass: we try symtab symbols (no line number info) */
9863 incl_index = 0;
9865 Elf32_Sym *sym, *sym_end;
9866 int type;
9868 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
9869 for(sym = (Elf32_Sym *)symtab_section->data + 1;
9870 sym < sym_end;
9871 sym++) {
9872 type = ELF32_ST_TYPE(sym->st_info);
9873 if (type == STT_FUNC) {
9874 if (wanted_pc >= sym->st_value &&
9875 wanted_pc < sym->st_value + sym->st_size) {
9876 pstrcpy(last_func_name, sizeof(last_func_name),
9877 strtab_section->data + sym->st_name);
9878 goto found;
9883 /* did not find any info: */
9884 fprintf(stderr, " ???\n");
9885 return;
9886 found:
9887 if (last_func_name[0] != '\0') {
9888 fprintf(stderr, " %s()", last_func_name);
9890 if (incl_index > 0) {
9891 fprintf(stderr, " (%s:%d",
9892 incl_files[incl_index - 1], last_line_num);
9893 for(i = incl_index - 2; i >= 0; i--)
9894 fprintf(stderr, ", included from %s", incl_files[i]);
9895 fprintf(stderr, ")");
9897 fprintf(stderr, "\n");
9900 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
9902 #ifdef __i386__
9904 /* fix for glibc 2.1 */
9905 #ifndef REG_EIP
9906 #define REG_EIP EIP
9907 #define REG_EBP EBP
9908 #endif
9910 /* return the PC at frame level 'level'. Return non zero if not found */
9911 static int rt_get_caller_pc(unsigned long *paddr,
9912 ucontext_t *uc, int level)
9914 unsigned long fp;
9915 int i;
9917 if (level == 0) {
9918 #if defined(__FreeBSD__)
9919 *paddr = uc->uc_mcontext.mc_eip;
9920 #elif defined(__dietlibc__)
9921 *paddr = uc->uc_mcontext.eip;
9922 #else
9923 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9924 #endif
9925 return 0;
9926 } else {
9927 #if defined(__FreeBSD__)
9928 fp = uc->uc_mcontext.mc_ebp;
9929 #elif defined(__dietlibc__)
9930 fp = uc->uc_mcontext.ebp;
9931 #else
9932 fp = uc->uc_mcontext.gregs[REG_EBP];
9933 #endif
9934 for(i=1;i<level;i++) {
9935 /* XXX: check address validity with program info */
9936 if (fp <= 0x1000 || fp >= 0xc0000000)
9937 return -1;
9938 fp = ((unsigned long *)fp)[0];
9940 *paddr = ((unsigned long *)fp)[1];
9941 return 0;
9944 #else
9946 #warning add arch specific rt_get_caller_pc()
9948 static int rt_get_caller_pc(unsigned long *paddr,
9949 ucontext_t *uc, int level)
9951 return -1;
9953 #endif
9955 /* emit a run time error at position 'pc' */
9956 void rt_error(ucontext_t *uc, const char *fmt, ...)
9958 va_list ap;
9959 unsigned long pc;
9960 int i;
9962 va_start(ap, fmt);
9963 fprintf(stderr, "Runtime error: ");
9964 vfprintf(stderr, fmt, ap);
9965 fprintf(stderr, "\n");
9966 for(i=0;i<num_callers;i++) {
9967 if (rt_get_caller_pc(&pc, uc, i) < 0)
9968 break;
9969 if (i == 0)
9970 fprintf(stderr, "at ");
9971 else
9972 fprintf(stderr, "by ");
9973 rt_printline(pc);
9975 exit(255);
9976 va_end(ap);
9979 /* signal handler for fatal errors */
9980 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9982 ucontext_t *uc = puc;
9984 switch(signum) {
9985 case SIGFPE:
9986 switch(siginf->si_code) {
9987 case FPE_INTDIV:
9988 case FPE_FLTDIV:
9989 rt_error(uc, "division by zero");
9990 break;
9991 default:
9992 rt_error(uc, "floating point exception");
9993 break;
9995 break;
9996 case SIGBUS:
9997 case SIGSEGV:
9998 if (rt_bound_error_msg && *rt_bound_error_msg)
9999 rt_error(uc, *rt_bound_error_msg);
10000 else
10001 rt_error(uc, "dereferencing invalid pointer");
10002 break;
10003 case SIGILL:
10004 rt_error(uc, "illegal instruction");
10005 break;
10006 case SIGABRT:
10007 rt_error(uc, "abort() called");
10008 break;
10009 default:
10010 rt_error(uc, "caught signal %d", signum);
10011 break;
10013 exit(255);
10015 #endif
10017 /* do all relocations (needed before using tcc_get_symbol()) */
10018 int tcc_relocate(TCCState *s1)
10020 Section *s;
10021 int i;
10023 s1->nb_errors = 0;
10025 #ifdef TCC_TARGET_PE
10026 pe_add_runtime(s1);
10027 #else
10028 tcc_add_runtime(s1);
10029 #endif
10031 relocate_common_syms();
10033 tcc_add_linker_symbols(s1);
10034 #ifndef TCC_TARGET_PE
10035 build_got_entries(s1);
10036 #endif
10037 /* compute relocation address : section are relocated in place. We
10038 also alloc the bss space */
10039 for(i = 1; i < s1->nb_sections; i++) {
10040 s = s1->sections[i];
10041 if (s->sh_flags & SHF_ALLOC) {
10042 if (s->sh_type == SHT_NOBITS)
10043 s->data = tcc_mallocz(s->data_offset);
10044 s->sh_addr = (unsigned long)s->data;
10048 relocate_syms(s1, 1);
10050 if (s1->nb_errors != 0)
10051 return -1;
10053 /* relocate each section */
10054 for(i = 1; i < s1->nb_sections; i++) {
10055 s = s1->sections[i];
10056 if (s->reloc)
10057 relocate_section(s1, s);
10060 /* mark executable sections as executable in memory */
10061 for(i = 1; i < s1->nb_sections; i++) {
10062 s = s1->sections[i];
10063 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10064 (SHF_ALLOC | SHF_EXECINSTR))
10065 set_pages_executable(s->data, s->data_offset);
10067 return 0;
10070 /* launch the compiled program with the given arguments */
10071 int tcc_run(TCCState *s1, int argc, char **argv)
10073 int (*prog_main)(int, char **);
10075 if (tcc_relocate(s1) < 0)
10076 return -1;
10078 prog_main = tcc_get_symbol_err(s1, "main");
10080 if (do_debug) {
10081 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10082 error("debug mode currently not available for Windows");
10083 #else
10084 struct sigaction sigact;
10085 /* install TCC signal handlers to print debug info on fatal
10086 runtime errors */
10087 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10088 sigact.sa_sigaction = sig_error;
10089 sigemptyset(&sigact.sa_mask);
10090 sigaction(SIGFPE, &sigact, NULL);
10091 sigaction(SIGILL, &sigact, NULL);
10092 sigaction(SIGSEGV, &sigact, NULL);
10093 sigaction(SIGBUS, &sigact, NULL);
10094 sigaction(SIGABRT, &sigact, NULL);
10095 #endif
10098 #ifdef CONFIG_TCC_BCHECK
10099 if (do_bounds_check) {
10100 void (*bound_init)(void);
10102 /* set error function */
10103 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10104 "__bound_error_msg");
10106 /* XXX: use .init section so that it also work in binary ? */
10107 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10108 bound_init();
10110 #endif
10111 return (*prog_main)(argc, argv);
10114 void tcc_memstats(void)
10116 #ifdef MEM_DEBUG
10117 printf("memory in use: %d\n", mem_cur_size);
10118 #endif
10121 static void tcc_cleanup(void)
10123 int i, n;
10125 if (NULL == tcc_state)
10126 return;
10127 tcc_state = NULL;
10129 /* free -D defines */
10130 free_defines(NULL);
10132 /* free tokens */
10133 n = tok_ident - TOK_IDENT;
10134 for(i = 0; i < n; i++)
10135 tcc_free(table_ident[i]);
10136 tcc_free(table_ident);
10138 /* free sym_pools */
10139 dynarray_reset(&sym_pools, &nb_sym_pools);
10140 /* string buffer */
10141 cstr_free(&tokcstr);
10142 /* reset symbol stack */
10143 sym_free_first = NULL;
10144 /* cleanup from error/setjmp */
10145 macro_ptr = NULL;
10148 TCCState *tcc_new(void)
10150 const char *p, *r;
10151 TCCState *s;
10152 TokenSym *ts;
10153 int i, c;
10155 tcc_cleanup();
10157 s = tcc_mallocz(sizeof(TCCState));
10158 if (!s)
10159 return NULL;
10160 tcc_state = s;
10161 s->output_type = TCC_OUTPUT_MEMORY;
10163 /* init isid table */
10164 for(i=CH_EOF;i<256;i++)
10165 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
10167 /* add all tokens */
10168 table_ident = NULL;
10169 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10171 tok_ident = TOK_IDENT;
10172 p = tcc_keywords;
10173 while (*p) {
10174 r = p;
10175 for(;;) {
10176 c = *r++;
10177 if (c == '\0')
10178 break;
10180 ts = tok_alloc(p, r - p - 1);
10181 p = r;
10184 /* we add dummy defines for some special macros to speed up tests
10185 and to have working defined() */
10186 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10187 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10188 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10189 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10191 /* standard defines */
10192 tcc_define_symbol(s, "__STDC__", NULL);
10193 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10194 #if defined(TCC_TARGET_I386)
10195 tcc_define_symbol(s, "__i386__", NULL);
10196 #endif
10197 #if defined(TCC_TARGET_ARM)
10198 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10199 tcc_define_symbol(s, "__arm_elf__", NULL);
10200 tcc_define_symbol(s, "__arm_elf", NULL);
10201 tcc_define_symbol(s, "arm_elf", NULL);
10202 tcc_define_symbol(s, "__arm__", NULL);
10203 tcc_define_symbol(s, "__arm", NULL);
10204 tcc_define_symbol(s, "arm", NULL);
10205 tcc_define_symbol(s, "__APCS_32__", NULL);
10206 #endif
10207 #ifdef TCC_TARGET_PE
10208 tcc_define_symbol(s, "_WIN32", NULL);
10209 #else
10210 tcc_define_symbol(s, "__unix__", NULL);
10211 tcc_define_symbol(s, "__unix", NULL);
10212 #if defined(__linux)
10213 tcc_define_symbol(s, "__linux__", NULL);
10214 tcc_define_symbol(s, "__linux", NULL);
10215 #endif
10216 #endif
10217 /* tiny C specific defines */
10218 tcc_define_symbol(s, "__TINYC__", NULL);
10220 /* tiny C & gcc defines */
10221 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10222 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10223 #ifdef TCC_TARGET_PE
10224 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10225 #else
10226 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10227 #endif
10229 #ifndef TCC_TARGET_PE
10230 /* default library paths */
10231 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
10232 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
10233 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
10234 #endif
10236 /* no section zero */
10237 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10239 /* create standard sections */
10240 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10241 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10242 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10244 /* symbols are always generated for linking stage */
10245 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10246 ".strtab",
10247 ".hashtab", SHF_PRIVATE);
10248 strtab_section = symtab_section->link;
10250 /* private symbol table for dynamic symbols */
10251 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10252 ".dynstrtab",
10253 ".dynhashtab", SHF_PRIVATE);
10254 s->alacarte_link = 1;
10256 #ifdef CHAR_IS_UNSIGNED
10257 s->char_is_unsigned = 1;
10258 #endif
10259 #if defined(TCC_TARGET_PE) && 0
10260 /* XXX: currently the PE linker is not ready to support that */
10261 s->leading_underscore = 1;
10262 #endif
10263 return s;
10266 void tcc_delete(TCCState *s1)
10268 int i;
10270 tcc_cleanup();
10272 /* free all sections */
10273 free_section(s1->dynsymtab_section);
10275 for(i = 1; i < s1->nb_sections; i++)
10276 free_section(s1->sections[i]);
10277 tcc_free(s1->sections);
10279 /* free any loaded DLLs */
10280 for ( i = 0; i < s1->nb_loaded_dlls; i++)
10282 DLLReference *ref = s1->loaded_dlls[i];
10283 if ( ref->handle )
10284 dlclose(ref->handle);
10287 /* free loaded dlls array */
10288 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10290 /* free library paths */
10291 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10293 /* free include paths */
10294 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10295 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10296 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10298 tcc_free(s1);
10301 int tcc_add_include_path(TCCState *s1, const char *pathname)
10303 char *pathname1;
10305 pathname1 = tcc_strdup(pathname);
10306 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10307 return 0;
10310 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10312 char *pathname1;
10314 pathname1 = tcc_strdup(pathname);
10315 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10316 return 0;
10319 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10321 const char *ext;
10322 Elf32_Ehdr ehdr;
10323 int fd, ret;
10324 BufferedFile *saved_file;
10326 /* find source file type with extension */
10327 ext = tcc_fileextension(filename);
10328 if (ext[0])
10329 ext++;
10331 /* open the file */
10332 saved_file = file;
10333 file = tcc_open(s1, filename);
10334 if (!file) {
10335 if (flags & AFF_PRINT_ERROR) {
10336 error_noabort("file '%s' not found", filename);
10338 ret = -1;
10339 goto fail1;
10342 if (flags & AFF_PREPROCESS) {
10343 ret = tcc_preprocess(s1);
10344 } else if (!ext[0] || !strcmp(ext, "c")) {
10345 /* C file assumed */
10346 ret = tcc_compile(s1);
10347 } else
10348 #ifdef CONFIG_TCC_ASM
10349 if (!strcmp(ext, "S")) {
10350 /* preprocessed assembler */
10351 ret = tcc_assemble(s1, 1);
10352 } else if (!strcmp(ext, "s")) {
10353 /* non preprocessed assembler */
10354 ret = tcc_assemble(s1, 0);
10355 } else
10356 #endif
10357 #ifdef TCC_TARGET_PE
10358 if (!strcmp(ext, "def")) {
10359 ret = pe_load_def_file(s1, file->fd);
10360 } else
10361 #endif
10363 fd = file->fd;
10364 /* assume executable format: auto guess file type */
10365 ret = read(fd, &ehdr, sizeof(ehdr));
10366 lseek(fd, 0, SEEK_SET);
10367 if (ret <= 0) {
10368 error_noabort("could not read header");
10369 goto fail;
10370 } else if (ret != sizeof(ehdr)) {
10371 goto try_load_script;
10374 if (ehdr.e_ident[0] == ELFMAG0 &&
10375 ehdr.e_ident[1] == ELFMAG1 &&
10376 ehdr.e_ident[2] == ELFMAG2 &&
10377 ehdr.e_ident[3] == ELFMAG3) {
10378 file->line_num = 0; /* do not display line number if error */
10379 if (ehdr.e_type == ET_REL) {
10380 ret = tcc_load_object_file(s1, fd, 0);
10381 } else if (ehdr.e_type == ET_DYN) {
10382 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10383 #ifdef TCC_TARGET_PE
10384 ret = -1;
10385 #else
10386 void *h;
10387 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10388 if (h)
10389 ret = 0;
10390 else
10391 ret = -1;
10392 #endif
10393 } else {
10394 ret = tcc_load_dll(s1, fd, filename,
10395 (flags & AFF_REFERENCED_DLL) != 0);
10397 } else {
10398 error_noabort("unrecognized ELF file");
10399 goto fail;
10401 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10402 file->line_num = 0; /* do not display line number if error */
10403 ret = tcc_load_archive(s1, fd);
10404 } else
10405 #ifdef TCC_TARGET_COFF
10406 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10407 ret = tcc_load_coff(s1, fd);
10408 } else
10409 #endif
10410 #ifdef TCC_TARGET_PE
10411 if (pe_test_res_file(&ehdr, ret)) {
10412 ret = pe_load_res_file(s1, fd);
10413 } else
10414 #endif
10416 /* as GNU ld, consider it is an ld script if not recognized */
10417 try_load_script:
10418 ret = tcc_load_ldscript(s1);
10419 if (ret < 0) {
10420 error_noabort("unrecognized file type");
10421 goto fail;
10425 the_end:
10426 tcc_close(file);
10427 fail1:
10428 file = saved_file;
10429 return ret;
10430 fail:
10431 ret = -1;
10432 goto the_end;
10435 int tcc_add_file(TCCState *s, const char *filename)
10437 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10440 int tcc_add_library_path(TCCState *s, const char *pathname)
10442 char *pathname1;
10444 pathname1 = tcc_strdup(pathname);
10445 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10446 return 0;
10449 /* find and load a dll. Return non zero if not found */
10450 /* XXX: add '-rpath' option support ? */
10451 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10453 char buf[1024];
10454 int i;
10456 for(i = 0; i < s->nb_library_paths; i++) {
10457 snprintf(buf, sizeof(buf), "%s/%s",
10458 s->library_paths[i], filename);
10459 if (tcc_add_file_internal(s, buf, flags) == 0)
10460 return 0;
10462 return -1;
10465 /* the library name is the same as the argument of the '-l' option */
10466 int tcc_add_library(TCCState *s, const char *libraryname)
10468 char buf[1024];
10469 int i;
10471 /* first we look for the dynamic library if not static linking */
10472 if (!s->static_link) {
10473 #ifdef TCC_TARGET_PE
10474 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10475 #else
10476 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10477 #endif
10478 if (tcc_add_dll(s, buf, 0) == 0)
10479 return 0;
10482 /* then we look for the static library */
10483 for(i = 0; i < s->nb_library_paths; i++) {
10484 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10485 s->library_paths[i], libraryname);
10486 if (tcc_add_file_internal(s, buf, 0) == 0)
10487 return 0;
10489 return -1;
10492 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10494 add_elf_sym(symtab_section, val, 0,
10495 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10496 SHN_ABS, name);
10497 return 0;
10500 int tcc_set_output_type(TCCState *s, int output_type)
10502 char buf[1024];
10504 s->output_type = output_type;
10506 if (!s->nostdinc) {
10507 /* default include paths */
10508 /* XXX: reverse order needed if -isystem support */
10509 #ifndef TCC_TARGET_PE
10510 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
10511 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
10512 #endif
10513 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10514 tcc_add_sysinclude_path(s, buf);
10515 #ifdef TCC_TARGET_PE
10516 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10517 tcc_add_sysinclude_path(s, buf);
10518 #endif
10521 /* if bound checking, then add corresponding sections */
10522 #ifdef CONFIG_TCC_BCHECK
10523 if (do_bounds_check) {
10524 /* define symbol */
10525 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10526 /* create bounds sections */
10527 bounds_section = new_section(s, ".bounds",
10528 SHT_PROGBITS, SHF_ALLOC);
10529 lbounds_section = new_section(s, ".lbounds",
10530 SHT_PROGBITS, SHF_ALLOC);
10532 #endif
10534 if (s->char_is_unsigned) {
10535 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10538 /* add debug sections */
10539 if (do_debug) {
10540 /* stab symbols */
10541 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10542 stab_section->sh_entsize = sizeof(Stab_Sym);
10543 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10544 put_elf_str(stabstr_section, "");
10545 stab_section->link = stabstr_section;
10546 /* put first entry */
10547 put_stabs("", 0, 0, 0, 0);
10550 /* add libc crt1/crti objects */
10551 #ifndef TCC_TARGET_PE
10552 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10553 !s->nostdlib) {
10554 if (output_type != TCC_OUTPUT_DLL)
10555 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10556 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10558 #endif
10560 #ifdef TCC_TARGET_PE
10561 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10562 tcc_add_library_path(s, buf);
10563 #endif
10565 return 0;
10568 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10569 #define FD_INVERT 0x0002 /* invert value before storing */
10571 typedef struct FlagDef {
10572 uint16_t offset;
10573 uint16_t flags;
10574 const char *name;
10575 } FlagDef;
10577 static const FlagDef warning_defs[] = {
10578 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10579 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10580 { offsetof(TCCState, warn_error), 0, "error" },
10581 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10582 "implicit-function-declaration" },
10585 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10586 const char *name, int value)
10588 int i;
10589 const FlagDef *p;
10590 const char *r;
10592 r = name;
10593 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10594 r += 3;
10595 value = !value;
10597 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10598 if (!strcmp(r, p->name))
10599 goto found;
10601 return -1;
10602 found:
10603 if (p->flags & FD_INVERT)
10604 value = !value;
10605 *(int *)((uint8_t *)s + p->offset) = value;
10606 return 0;
10610 /* set/reset a warning */
10611 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10613 int i;
10614 const FlagDef *p;
10616 if (!strcmp(warning_name, "all")) {
10617 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10618 if (p->flags & WD_ALL)
10619 *(int *)((uint8_t *)s + p->offset) = 1;
10621 return 0;
10622 } else {
10623 return set_flag(s, warning_defs, countof(warning_defs),
10624 warning_name, value);
10628 static const FlagDef flag_defs[] = {
10629 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10630 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10631 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10632 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10635 /* set/reset a flag */
10636 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10638 return set_flag(s, flag_defs, countof(flag_defs),
10639 flag_name, value);
10642 #if !defined(LIBTCC)
10644 static int64_t getclock_us(void)
10646 #ifdef _WIN32
10647 struct _timeb tb;
10648 _ftime(&tb);
10649 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10650 #else
10651 struct timeval tv;
10652 gettimeofday(&tv, NULL);
10653 return tv.tv_sec * 1000000LL + tv.tv_usec;
10654 #endif
10657 void help(void)
10659 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10660 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10661 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10662 " [-static] [infile1 infile2...] [-run infile args...]\n"
10663 "\n"
10664 "General options:\n"
10665 " -v display current version, increase verbosity\n"
10666 " -c compile only - generate an object file\n"
10667 " -o outfile set output filename\n"
10668 " -Bdir set tcc internal library path\n"
10669 " -bench output compilation statistics\n"
10670 " -run run compiled source\n"
10671 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10672 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10673 " -w disable all warnings\n"
10674 "Preprocessor options:\n"
10675 " -E preprocess only\n"
10676 " -Idir add include path 'dir'\n"
10677 " -Dsym[=val] define 'sym' with value 'val'\n"
10678 " -Usym undefine 'sym'\n"
10679 "Linker options:\n"
10680 " -Ldir add library path 'dir'\n"
10681 " -llib link with dynamic or static library 'lib'\n"
10682 " -shared generate a shared library\n"
10683 " -soname set name for shared library to be used at runtime\n"
10684 " -static static linking\n"
10685 " -rdynamic export all global symbols to dynamic linker\n"
10686 " -r generate (relocatable) object file\n"
10687 "Debugger options:\n"
10688 " -g generate runtime debug info\n"
10689 #ifdef CONFIG_TCC_BCHECK
10690 " -b compile with built-in memory and bounds checker (implies -g)\n"
10691 #endif
10692 " -bt N show N callers in stack traces\n"
10696 #define TCC_OPTION_HAS_ARG 0x0001
10697 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10699 typedef struct TCCOption {
10700 const char *name;
10701 uint16_t index;
10702 uint16_t flags;
10703 } TCCOption;
10705 enum {
10706 TCC_OPTION_HELP,
10707 TCC_OPTION_I,
10708 TCC_OPTION_D,
10709 TCC_OPTION_U,
10710 TCC_OPTION_L,
10711 TCC_OPTION_B,
10712 TCC_OPTION_l,
10713 TCC_OPTION_bench,
10714 TCC_OPTION_bt,
10715 TCC_OPTION_b,
10716 TCC_OPTION_g,
10717 TCC_OPTION_c,
10718 TCC_OPTION_static,
10719 TCC_OPTION_shared,
10720 TCC_OPTION_soname,
10721 TCC_OPTION_o,
10722 TCC_OPTION_r,
10723 TCC_OPTION_Wl,
10724 TCC_OPTION_W,
10725 TCC_OPTION_O,
10726 TCC_OPTION_m,
10727 TCC_OPTION_f,
10728 TCC_OPTION_nostdinc,
10729 TCC_OPTION_nostdlib,
10730 TCC_OPTION_print_search_dirs,
10731 TCC_OPTION_rdynamic,
10732 TCC_OPTION_run,
10733 TCC_OPTION_v,
10734 TCC_OPTION_w,
10735 TCC_OPTION_pipe,
10736 TCC_OPTION_E,
10739 static const TCCOption tcc_options[] = {
10740 { "h", TCC_OPTION_HELP, 0 },
10741 { "?", TCC_OPTION_HELP, 0 },
10742 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10743 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10744 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10745 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10746 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10747 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10748 { "bench", TCC_OPTION_bench, 0 },
10749 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10750 #ifdef CONFIG_TCC_BCHECK
10751 { "b", TCC_OPTION_b, 0 },
10752 #endif
10753 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10754 { "c", TCC_OPTION_c, 0 },
10755 { "static", TCC_OPTION_static, 0 },
10756 { "shared", TCC_OPTION_shared, 0 },
10757 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10758 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10759 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10760 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10761 { "r", TCC_OPTION_r, 0 },
10762 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10763 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10764 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10765 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10766 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10767 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10768 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10769 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10770 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10771 { "w", TCC_OPTION_w, 0 },
10772 { "pipe", TCC_OPTION_pipe, 0},
10773 { "E", TCC_OPTION_E, 0},
10774 { NULL },
10777 /* convert 'str' into an array of space separated strings */
10778 static int expand_args(char ***pargv, const char *str)
10780 const char *s1;
10781 char **argv, *arg;
10782 int argc, len;
10784 argc = 0;
10785 argv = NULL;
10786 for(;;) {
10787 while (is_space(*str))
10788 str++;
10789 if (*str == '\0')
10790 break;
10791 s1 = str;
10792 while (*str != '\0' && !is_space(*str))
10793 str++;
10794 len = str - s1;
10795 arg = tcc_malloc(len + 1);
10796 memcpy(arg, s1, len);
10797 arg[len] = '\0';
10798 dynarray_add((void ***)&argv, &argc, arg);
10800 *pargv = argv;
10801 return argc;
10804 static char **files;
10805 static int nb_files, nb_libraries;
10806 static int multiple_files;
10807 static int print_search_dirs;
10808 static int output_type;
10809 static int reloc_output;
10810 static const char *outfile;
10812 int parse_args(TCCState *s, int argc, char **argv)
10814 int optind;
10815 const TCCOption *popt;
10816 const char *optarg, *p1, *r1;
10817 char *r;
10819 optind = 0;
10820 while (optind < argc) {
10822 r = argv[optind++];
10823 if (r[0] != '-' || r[1] == '\0') {
10824 /* add a new file */
10825 dynarray_add((void ***)&files, &nb_files, r);
10826 if (!multiple_files) {
10827 optind--;
10828 /* argv[0] will be this file */
10829 break;
10831 } else {
10832 /* find option in table (match only the first chars */
10833 popt = tcc_options;
10834 for(;;) {
10835 p1 = popt->name;
10836 if (p1 == NULL)
10837 error("invalid option -- '%s'", r);
10838 r1 = r + 1;
10839 for(;;) {
10840 if (*p1 == '\0')
10841 goto option_found;
10842 if (*r1 != *p1)
10843 break;
10844 p1++;
10845 r1++;
10847 popt++;
10849 option_found:
10850 if (popt->flags & TCC_OPTION_HAS_ARG) {
10851 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
10852 optarg = r1;
10853 } else {
10854 if (optind >= argc)
10855 error("argument to '%s' is missing", r);
10856 optarg = argv[optind++];
10858 } else {
10859 if (*r1 != '\0')
10860 return 0;
10861 optarg = NULL;
10864 switch(popt->index) {
10865 case TCC_OPTION_HELP:
10866 return 0;
10868 case TCC_OPTION_I:
10869 if (tcc_add_include_path(s, optarg) < 0)
10870 error("too many include paths");
10871 break;
10872 case TCC_OPTION_D:
10874 char *sym, *value;
10875 sym = (char *)optarg;
10876 value = strchr(sym, '=');
10877 if (value) {
10878 *value = '\0';
10879 value++;
10881 tcc_define_symbol(s, sym, value);
10883 break;
10884 case TCC_OPTION_U:
10885 tcc_undefine_symbol(s, optarg);
10886 break;
10887 case TCC_OPTION_L:
10888 tcc_add_library_path(s, optarg);
10889 break;
10890 case TCC_OPTION_B:
10891 /* set tcc utilities path (mainly for tcc development) */
10892 tcc_lib_path = optarg;
10893 break;
10894 case TCC_OPTION_l:
10895 dynarray_add((void ***)&files, &nb_files, r);
10896 nb_libraries++;
10897 break;
10898 case TCC_OPTION_bench:
10899 do_bench = 1;
10900 break;
10901 case TCC_OPTION_bt:
10902 num_callers = atoi(optarg);
10903 break;
10904 #ifdef CONFIG_TCC_BCHECK
10905 case TCC_OPTION_b:
10906 do_bounds_check = 1;
10907 do_debug = 1;
10908 break;
10909 #endif
10910 case TCC_OPTION_g:
10911 do_debug = 1;
10912 break;
10913 case TCC_OPTION_c:
10914 multiple_files = 1;
10915 output_type = TCC_OUTPUT_OBJ;
10916 break;
10917 case TCC_OPTION_static:
10918 s->static_link = 1;
10919 break;
10920 case TCC_OPTION_shared:
10921 output_type = TCC_OUTPUT_DLL;
10922 break;
10923 case TCC_OPTION_soname:
10924 s->soname = optarg;
10925 break;
10926 case TCC_OPTION_o:
10927 multiple_files = 1;
10928 outfile = optarg;
10929 break;
10930 case TCC_OPTION_r:
10931 /* generate a .o merging several output files */
10932 reloc_output = 1;
10933 output_type = TCC_OUTPUT_OBJ;
10934 break;
10935 case TCC_OPTION_nostdinc:
10936 s->nostdinc = 1;
10937 break;
10938 case TCC_OPTION_nostdlib:
10939 s->nostdlib = 1;
10940 break;
10941 case TCC_OPTION_print_search_dirs:
10942 print_search_dirs = 1;
10943 break;
10944 case TCC_OPTION_run:
10946 int argc1;
10947 char **argv1;
10948 argc1 = expand_args(&argv1, optarg);
10949 if (argc1 > 0) {
10950 parse_args(s, argc1, argv1);
10952 multiple_files = 0;
10953 output_type = TCC_OUTPUT_MEMORY;
10955 break;
10956 case TCC_OPTION_v:
10957 do {
10958 if (0 == verbose++)
10959 printf("tcc version %s\n", TCC_VERSION);
10960 } while (*optarg++ == 'v');
10961 break;
10962 case TCC_OPTION_f:
10963 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
10964 goto unsupported_option;
10965 break;
10966 case TCC_OPTION_W:
10967 if (tcc_set_warning(s, optarg, 1) < 0 &&
10968 s->warn_unsupported)
10969 goto unsupported_option;
10970 break;
10971 case TCC_OPTION_w:
10972 s->warn_none = 1;
10973 break;
10974 case TCC_OPTION_rdynamic:
10975 s->rdynamic = 1;
10976 break;
10977 case TCC_OPTION_Wl:
10979 const char *p;
10980 if (strstart(optarg, "-Ttext,", &p)) {
10981 s->text_addr = strtoul(p, NULL, 16);
10982 s->has_text_addr = 1;
10983 } else if (strstart(optarg, "--oformat,", &p)) {
10984 if (strstart(p, "elf32-", NULL)) {
10985 s->output_format = TCC_OUTPUT_FORMAT_ELF;
10986 } else if (!strcmp(p, "binary")) {
10987 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
10988 } else
10989 #ifdef TCC_TARGET_COFF
10990 if (!strcmp(p, "coff")) {
10991 s->output_format = TCC_OUTPUT_FORMAT_COFF;
10992 } else
10993 #endif
10995 error("target %s not found", p);
10997 } else {
10998 error("unsupported linker option '%s'", optarg);
11001 break;
11002 case TCC_OPTION_E:
11003 output_type = TCC_OUTPUT_PREPROCESS;
11004 break;
11005 default:
11006 if (s->warn_unsupported) {
11007 unsupported_option:
11008 warning("unsupported option '%s'", r);
11010 break;
11014 return optind + 1;
11017 int main(int argc, char **argv)
11019 int i;
11020 TCCState *s;
11021 int nb_objfiles, ret, optind;
11022 char objfilename[1024];
11023 int64_t start_time = 0;
11025 #ifdef _WIN32
11026 tcc_lib_path = w32_tcc_lib_path();
11027 #endif
11029 s = tcc_new();
11030 output_type = TCC_OUTPUT_EXE;
11031 outfile = NULL;
11032 multiple_files = 1;
11033 files = NULL;
11034 nb_files = 0;
11035 nb_libraries = 0;
11036 reloc_output = 0;
11037 print_search_dirs = 0;
11038 ret = 0;
11040 optind = parse_args(s, argc - 1, argv + 1);
11041 if (print_search_dirs) {
11042 /* enough for Linux kernel */
11043 printf("install: %s/\n", tcc_lib_path);
11044 return 0;
11046 if (optind == 0 || nb_files == 0) {
11047 if (optind && verbose)
11048 return 0;
11049 help();
11050 return 1;
11053 nb_objfiles = nb_files - nb_libraries;
11055 /* if outfile provided without other options, we output an
11056 executable */
11057 if (outfile && output_type == TCC_OUTPUT_MEMORY)
11058 output_type = TCC_OUTPUT_EXE;
11060 /* check -c consistency : only single file handled. XXX: checks file type */
11061 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
11062 /* accepts only a single input file */
11063 if (nb_objfiles != 1)
11064 error("cannot specify multiple files with -c");
11065 if (nb_libraries != 0)
11066 error("cannot specify libraries with -c");
11070 if (output_type == TCC_OUTPUT_PREPROCESS) {
11071 if (!outfile) {
11072 s->outfile = stdout;
11073 } else {
11074 s->outfile = fopen(outfile, "w");
11075 if (!s->outfile)
11076 error("could not open '%s", outfile);
11078 } else if (output_type != TCC_OUTPUT_MEMORY) {
11079 if (!outfile) {
11080 /* compute default outfile name */
11081 char *ext;
11082 const char *name =
11083 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
11084 pstrcpy(objfilename, sizeof(objfilename), name);
11085 ext = tcc_fileextension(objfilename);
11086 #ifdef TCC_TARGET_PE
11087 if (output_type == TCC_OUTPUT_DLL)
11088 strcpy(ext, ".dll");
11089 else
11090 if (output_type == TCC_OUTPUT_EXE)
11091 strcpy(ext, ".exe");
11092 else
11093 #endif
11094 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
11095 strcpy(ext, ".o");
11096 else
11097 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11098 outfile = objfilename;
11102 if (do_bench) {
11103 start_time = getclock_us();
11106 tcc_set_output_type(s, output_type);
11108 /* compile or add each files or library */
11109 for(i = 0; i < nb_files && ret == 0; i++) {
11110 const char *filename;
11112 filename = files[i];
11113 if (output_type == TCC_OUTPUT_PREPROCESS) {
11114 if (tcc_add_file_internal(s, filename,
11115 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11116 ret = 1;
11117 } else if (filename[0] == '-' && filename[1]) {
11118 if (tcc_add_library(s, filename + 2) < 0)
11119 error("cannot find %s", filename);
11120 } else {
11121 if (1 == verbose)
11122 printf("-> %s\n", filename);
11123 if (tcc_add_file(s, filename) < 0)
11124 ret = 1;
11128 /* free all files */
11129 tcc_free(files);
11131 if (ret)
11132 goto the_end;
11134 if (do_bench) {
11135 double total_time;
11136 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11137 if (total_time < 0.001)
11138 total_time = 0.001;
11139 if (total_bytes < 1)
11140 total_bytes = 1;
11141 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11142 tok_ident - TOK_IDENT, total_lines, total_bytes,
11143 total_time, (int)(total_lines / total_time),
11144 total_bytes / total_time / 1000000.0);
11147 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11148 if (outfile)
11149 fclose(s->outfile);
11150 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11151 ret = tcc_run(s, argc - optind, argv + optind);
11152 } else
11153 ret = tcc_output_file(s, outfile) ? 1 : 0;
11154 the_end:
11155 /* XXX: cannot do it with bound checking because of the malloc hooks */
11156 if (!do_bounds_check)
11157 tcc_delete(s);
11159 #ifdef MEM_DEBUG
11160 if (do_bench) {
11161 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11163 #endif
11164 return ret;
11167 #endif