win32: fix for VC8Express compiler
[tinycc.git] / tcc.c
blob0b731f3bb06f1c9a8e8d1cf1e1f0b51bde990ca8
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 <signal.h>
37 #include <fcntl.h>
38 #include <setjmp.h>
39 #include <time.h>
41 #ifdef _WIN32
42 #include <windows.h>
43 #include <sys/timeb.h>
44 #ifdef _MSC_VER
45 #define inline __inline
46 #endif
47 #endif
49 #ifndef _WIN32
50 #include <unistd.h>
51 #include <sys/time.h>
52 #include <sys/ucontext.h>
53 #include <sys/mman.h>
54 #endif
56 #endif /* !CONFIG_TCCBOOT */
58 #ifndef PAGESIZE
59 #define PAGESIZE 4096
60 #endif
62 #include "elf.h"
63 #include "stab.h"
65 #ifndef O_BINARY
66 #define O_BINARY 0
67 #endif
69 #include "libtcc.h"
71 /* parser debug */
72 //#define PARSE_DEBUG
73 /* preprocessor debug */
74 //#define PP_DEBUG
75 /* include file debug */
76 //#define INC_DEBUG
78 //#define MEM_DEBUG
80 /* assembler debug */
81 //#define ASM_DEBUG
83 /* target selection */
84 //#define TCC_TARGET_I386 /* i386 code generator */
85 //#define TCC_TARGET_ARM /* ARMv4 code generator */
86 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
87 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
89 /* default target is I386 */
90 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
91 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
92 #define TCC_TARGET_I386
93 #endif
95 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
96 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
97 #define CONFIG_TCC_BCHECK /* enable bound checking code */
98 #endif
100 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
101 #define CONFIG_TCC_STATIC
102 #endif
104 /* define it to include assembler support */
105 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67) && \
106 !defined(TCC_TARGET_X86_64)
107 #define CONFIG_TCC_ASM
108 #endif
110 /* object format selection */
111 #if defined(TCC_TARGET_C67)
112 #define TCC_TARGET_COFF
113 #endif
115 #define FALSE 0
116 #define false 0
117 #define TRUE 1
118 #define true 1
119 typedef int BOOL;
121 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
122 executables or dlls */
123 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
125 #define INCLUDE_STACK_SIZE 32
126 #define IFDEF_STACK_SIZE 64
127 #define VSTACK_SIZE 256
128 #define STRING_MAX_SIZE 1024
129 #define PACK_STACK_SIZE 8
131 #define TOK_HASH_SIZE 8192 /* must be a power of two */
132 #define TOK_ALLOC_INCR 512 /* must be a power of two */
133 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
135 /* token symbol management */
136 typedef struct TokenSym {
137 struct TokenSym *hash_next;
138 struct Sym *sym_define; /* direct pointer to define */
139 struct Sym *sym_label; /* direct pointer to label */
140 struct Sym *sym_struct; /* direct pointer to structure */
141 struct Sym *sym_identifier; /* direct pointer to identifier */
142 int tok; /* token number */
143 int len;
144 char str[1];
145 } TokenSym;
147 #ifdef TCC_TARGET_PE
148 typedef unsigned short nwchar_t;
149 #else
150 typedef int nwchar_t;
151 #endif
153 typedef struct CString {
154 int size; /* size in bytes */
155 void *data; /* either 'char *' or 'nwchar_t *' */
156 int size_allocated;
157 void *data_allocated; /* if non NULL, data has been malloced */
158 } CString;
160 /* type definition */
161 typedef struct CType {
162 int t;
163 struct Sym *ref;
164 } CType;
166 /* constant value */
167 typedef union CValue {
168 long double ld;
169 double d;
170 float f;
171 int i;
172 unsigned int ui;
173 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
174 long long ll;
175 unsigned long long ull;
176 struct CString *cstr;
177 void *ptr;
178 int tab[1];
179 } CValue;
181 /* value on stack */
182 typedef struct SValue {
183 CType type; /* type */
184 unsigned short r; /* register + flags */
185 unsigned short r2; /* second register, used for 'long long'
186 type. If not used, set to VT_CONST */
187 CValue c; /* constant, if VT_CONST */
188 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
189 } SValue;
191 /* symbol management */
192 typedef struct Sym {
193 int v; /* symbol token */
194 long r; /* associated register */
195 long c; /* associated number */
196 CType type; /* associated type */
197 struct Sym *next; /* next related symbol */
198 struct Sym *prev; /* prev symbol in stack */
199 struct Sym *prev_tok; /* previous symbol for this token */
200 } Sym;
202 /* section definition */
203 /* XXX: use directly ELF structure for parameters ? */
204 /* special flag to indicate that the section should not be linked to
205 the other ones */
206 #define SHF_PRIVATE 0x80000000
208 typedef struct Section {
209 unsigned long data_offset; /* current data offset */
210 unsigned char *data; /* section data */
211 unsigned long data_allocated; /* used for realloc() handling */
212 int sh_name; /* elf section name (only used during output) */
213 int sh_num; /* elf section number */
214 int sh_type; /* elf section type */
215 int sh_flags; /* elf section flags */
216 int sh_info; /* elf section info */
217 int sh_addralign; /* elf section alignment */
218 int sh_entsize; /* elf entry size */
219 unsigned long sh_size; /* section size (only used during output) */
220 unsigned long sh_addr; /* address at which the section is relocated */
221 unsigned long sh_offset; /* file offset */
222 int nb_hashed_syms; /* used to resize the hash table */
223 struct Section *link; /* link to another section */
224 struct Section *reloc; /* corresponding section for relocation, if any */
225 struct Section *hash; /* hash table for symbols */
226 struct Section *next;
227 char name[1]; /* section name */
228 } Section;
230 typedef struct DLLReference {
231 int level;
232 void *handle;
233 char name[1];
234 } DLLReference;
236 /* GNUC attribute definition */
237 typedef struct AttributeDef {
238 int aligned;
239 int packed;
240 Section *section;
241 int func_attr; /* calling convention, exports, ... */
242 } AttributeDef;
244 /* -------------------------------------------------- */
245 /* gr: wrappers for casting sym->r for other purposes */
246 typedef struct {
247 unsigned
248 func_call : 8,
249 func_args : 8,
250 func_export : 1;
251 } func_attr_t;
253 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
254 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
255 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
256 #define INLINE_DEF(r) (*(int **)&(r))
257 /* -------------------------------------------------- */
259 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
260 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
261 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
263 /* stored in 'Sym.c' field */
264 #define FUNC_NEW 1 /* ansi function prototype */
265 #define FUNC_OLD 2 /* old function prototype */
266 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
268 /* stored in 'Sym.r' field */
269 #define FUNC_CDECL 0 /* standard c call */
270 #define FUNC_STDCALL 1 /* pascal c call */
271 #define FUNC_FASTCALL1 2 /* first param in %eax */
272 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
273 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
274 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
276 /* field 'Sym.t' for macros */
277 #define MACRO_OBJ 0 /* object like macro */
278 #define MACRO_FUNC 1 /* function like macro */
280 /* field 'Sym.r' for C labels */
281 #define LABEL_DEFINED 0 /* label is defined */
282 #define LABEL_FORWARD 1 /* label is forward defined */
283 #define LABEL_DECLARED 2 /* label is declared but never used */
285 /* type_decl() types */
286 #define TYPE_ABSTRACT 1 /* type without variable */
287 #define TYPE_DIRECT 2 /* type with variable */
289 #define IO_BUF_SIZE 8192
291 typedef struct BufferedFile {
292 uint8_t *buf_ptr;
293 uint8_t *buf_end;
294 int fd;
295 int line_num; /* current line number - here to simplify code */
296 int ifndef_macro; /* #ifndef macro / #endif search */
297 int ifndef_macro_saved; /* saved ifndef_macro */
298 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
299 char inc_type; /* type of include */
300 char inc_filename[512]; /* filename specified by the user */
301 char filename[1024]; /* current filename - here to simplify code */
302 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
303 } BufferedFile;
305 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
306 #define CH_EOF (-1) /* end of file */
308 /* parsing state (used to save parser state to reparse part of the
309 source several times) */
310 typedef struct ParseState {
311 int *macro_ptr;
312 int line_num;
313 int tok;
314 CValue tokc;
315 } ParseState;
317 /* used to record tokens */
318 typedef struct TokenString {
319 int *str;
320 int len;
321 int allocated_len;
322 int last_line_num;
323 } TokenString;
325 /* include file cache, used to find files faster and also to eliminate
326 inclusion if the include file is protected by #ifndef ... #endif */
327 typedef struct CachedInclude {
328 int ifndef_macro;
329 int hash_next; /* -1 if none */
330 char type; /* '"' or '>' to give include type */
331 char filename[1]; /* path specified in #include */
332 } CachedInclude;
334 #define CACHED_INCLUDES_HASH_SIZE 512
336 /* parser */
337 static struct BufferedFile *file;
338 static int ch, tok;
339 static CString tok_spaces; /* spaces before current token */
340 static CValue tokc;
341 static CString tokcstr; /* current parsed string, if any */
342 /* additional informations about token */
343 static int tok_flags;
344 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
345 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
346 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
347 #define TOK_FLAG_EOF 0x0008 /* end of file */
349 static int *macro_ptr, *macro_ptr_allocated;
350 static int *unget_saved_macro_ptr;
351 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
352 static int unget_buffer_enabled;
353 static int parse_flags;
354 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
355 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
356 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
357 token. line feed is also
358 returned at eof */
359 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
361 static Section *text_section, *data_section, *bss_section; /* predefined sections */
362 static Section *cur_text_section; /* current section where function code is
363 generated */
364 #ifdef CONFIG_TCC_ASM
365 static Section *last_text_section; /* to handle .previous asm directive */
366 #endif
367 /* bound check related sections */
368 static Section *bounds_section; /* contains global data bound description */
369 static Section *lbounds_section; /* contains local data bound description */
370 /* symbol sections */
371 static Section *symtab_section, *strtab_section;
373 /* debug sections */
374 static Section *stab_section, *stabstr_section;
376 /* loc : local variable index
377 ind : output code index
378 rsym: return symbol
379 anon_sym: anonymous symbol index
381 static int rsym, anon_sym, ind, loc;
382 /* expression generation modifiers */
383 static int const_wanted; /* true if constant wanted */
384 static int nocode_wanted; /* true if no code generation wanted for an expression */
385 static int global_expr; /* true if compound literals must be allocated
386 globally (used during initializers parsing */
387 static CType func_vt; /* current function return type (used by return
388 instruction) */
389 static int func_vc;
390 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
391 static int tok_ident;
392 static TokenSym **table_ident;
393 static TokenSym *hash_ident[TOK_HASH_SIZE];
394 static char token_buf[STRING_MAX_SIZE + 1];
395 static char *funcname;
396 static Sym *global_stack, *local_stack;
397 static Sym *define_stack;
398 static Sym *global_label_stack, *local_label_stack;
399 /* symbol allocator */
400 #define SYM_POOL_NB (8192 / sizeof(Sym))
401 static Sym *sym_free_first;
402 static void **sym_pools;
403 static int nb_sym_pools;
405 static SValue vstack[VSTACK_SIZE], *vtop;
406 /* some predefined types */
407 static CType char_pointer_type, func_old_type, int_type;
408 /* true if isid(c) || isnum(c) */
409 static unsigned char isidnum_table[256-CH_EOF];
411 /* display some information during compilation */
412 static int verbose = 0;
414 /* compile with debug symbol (and use them if error during execution) */
415 static int do_debug = 0;
417 /* compile with built-in memory and bounds checker */
418 static int do_bounds_check = 0;
420 /* display benchmark infos */
421 #if !defined(LIBTCC)
422 static int do_bench = 0;
423 #endif
424 static int total_lines;
425 static int total_bytes;
427 /* use GNU C extensions */
428 static int gnu_ext = 1;
430 /* use Tiny C extensions */
431 static int tcc_ext = 1;
433 /* max number of callers shown if error */
434 static int num_callers = 6;
435 static const char **rt_bound_error_msg;
437 /* XXX: get rid of this ASAP */
438 static struct TCCState *tcc_state;
440 /* give the path of the tcc libraries */
441 static const char *tcc_lib_path = CONFIG_TCCDIR;
443 struct TCCState {
444 int output_type;
446 BufferedFile **include_stack_ptr;
447 int *ifdef_stack_ptr;
449 /* include file handling */
450 char **include_paths;
451 int nb_include_paths;
452 char **sysinclude_paths;
453 int nb_sysinclude_paths;
454 CachedInclude **cached_includes;
455 int nb_cached_includes;
457 char **library_paths;
458 int nb_library_paths;
460 /* array of all loaded dlls (including those referenced by loaded
461 dlls) */
462 DLLReference **loaded_dlls;
463 int nb_loaded_dlls;
465 /* sections */
466 Section **sections;
467 int nb_sections; /* number of sections, including first dummy section */
469 /* got handling */
470 Section *got;
471 Section *plt;
472 unsigned long *got_offsets;
473 int nb_got_offsets;
474 /* give the correspondance from symtab indexes to dynsym indexes */
475 int *symtab_to_dynsym;
477 /* temporary dynamic symbol sections (for dll loading) */
478 Section *dynsymtab_section;
479 /* exported dynamic symbol section */
480 Section *dynsym;
482 int nostdinc; /* if true, no standard headers are added */
483 int nostdlib; /* if true, no standard libraries are added */
485 int nocommon; /* if true, do not use common symbols for .bss data */
487 /* if true, static linking is performed */
488 int static_link;
490 /* soname as specified on the command line (-soname) */
491 const char *soname;
493 /* if true, all symbols are exported */
494 int rdynamic;
496 /* if true, only link in referenced objects from archive */
497 int alacarte_link;
499 /* address of text section */
500 unsigned long text_addr;
501 int has_text_addr;
503 /* output format, see TCC_OUTPUT_FORMAT_xxx */
504 int output_format;
506 /* C language options */
507 int char_is_unsigned;
508 int leading_underscore;
510 /* warning switches */
511 int warn_write_strings;
512 int warn_unsupported;
513 int warn_error;
514 int warn_none;
515 int warn_implicit_function_declaration;
517 /* error handling */
518 void *error_opaque;
519 void (*error_func)(void *opaque, const char *msg);
520 int error_set_jmp_enabled;
521 jmp_buf error_jmp_buf;
522 int nb_errors;
524 /* tiny assembler state */
525 Sym *asm_labels;
527 /* see include_stack_ptr */
528 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
530 /* see ifdef_stack_ptr */
531 int ifdef_stack[IFDEF_STACK_SIZE];
533 /* see cached_includes */
534 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
536 /* pack stack */
537 int pack_stack[PACK_STACK_SIZE];
538 int *pack_stack_ptr;
540 /* output file for preprocessing */
541 FILE *outfile;
543 #ifdef TCC_TARGET_X86_64
544 /* buffer to store jump tables */
545 char *jmp_table;
546 int jmp_table_num;
547 #endif
550 /* The current value can be: */
551 #define VT_VALMASK 0x00ff
552 #define VT_CONST 0x00f0 /* constant in vc
553 (must be first non register value) */
554 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
555 #define VT_LOCAL 0x00f2 /* offset on stack */
556 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
557 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
558 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
559 #define VT_LVAL 0x0100 /* var is an lvalue */
560 #define VT_SYM 0x0200 /* a symbol value is added */
561 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
562 char/short stored in integer registers) */
563 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
564 dereferencing value */
565 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
566 bounding function call point is in vc */
567 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
568 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
569 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
570 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
572 /* types */
573 #define VT_INT 0 /* integer type */
574 #define VT_BYTE 1 /* signed byte type */
575 #define VT_SHORT 2 /* short type */
576 #define VT_VOID 3 /* void type */
577 #define VT_PTR 4 /* pointer */
578 #define VT_ENUM 5 /* enum definition */
579 #define VT_FUNC 6 /* function type */
580 #define VT_STRUCT 7 /* struct/union definition */
581 #define VT_FLOAT 8 /* IEEE float */
582 #define VT_DOUBLE 9 /* IEEE double */
583 #define VT_LDOUBLE 10 /* IEEE long double */
584 #define VT_BOOL 11 /* ISOC99 boolean type */
585 #define VT_LLONG 12 /* 64 bit integer */
586 #define VT_LONG 13 /* long integer (NEVER USED as type, only
587 during parsing) */
588 #define VT_BTYPE 0x000f /* mask for basic type */
589 #define VT_UNSIGNED 0x0010 /* unsigned type */
590 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
591 #define VT_BITFIELD 0x0040 /* bitfield modifier */
592 #define VT_CONSTANT 0x0800 /* const modifier */
593 #define VT_VOLATILE 0x1000 /* volatile modifier */
594 #define VT_SIGNED 0x2000 /* signed type */
596 /* storage */
597 #define VT_EXTERN 0x00000080 /* extern definition */
598 #define VT_STATIC 0x00000100 /* static variable */
599 #define VT_TYPEDEF 0x00000200 /* typedef definition */
600 #define VT_INLINE 0x00000400 /* inline definition */
602 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
604 /* type mask (except storage) */
605 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
606 #define VT_TYPE (~(VT_STORAGE))
608 /* token values */
610 /* warning: the following compare tokens depend on i386 asm code */
611 #define TOK_ULT 0x92
612 #define TOK_UGE 0x93
613 #define TOK_EQ 0x94
614 #define TOK_NE 0x95
615 #define TOK_ULE 0x96
616 #define TOK_UGT 0x97
617 #define TOK_Nset 0x98
618 #define TOK_Nclear 0x99
619 #define TOK_LT 0x9c
620 #define TOK_GE 0x9d
621 #define TOK_LE 0x9e
622 #define TOK_GT 0x9f
624 #define TOK_LAND 0xa0
625 #define TOK_LOR 0xa1
627 #define TOK_DEC 0xa2
628 #define TOK_MID 0xa3 /* inc/dec, to void constant */
629 #define TOK_INC 0xa4
630 #define TOK_UDIV 0xb0 /* unsigned division */
631 #define TOK_UMOD 0xb1 /* unsigned modulo */
632 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
633 #define TOK_CINT 0xb3 /* number in tokc */
634 #define TOK_CCHAR 0xb4 /* char constant in tokc */
635 #define TOK_STR 0xb5 /* pointer to string in tokc */
636 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
637 #define TOK_LCHAR 0xb7
638 #define TOK_LSTR 0xb8
639 #define TOK_CFLOAT 0xb9 /* float constant */
640 #define TOK_LINENUM 0xba /* line number info */
641 #define TOK_CDOUBLE 0xc0 /* double constant */
642 #define TOK_CLDOUBLE 0xc1 /* long double constant */
643 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
644 #define TOK_ADDC1 0xc3 /* add with carry generation */
645 #define TOK_ADDC2 0xc4 /* add with carry use */
646 #define TOK_SUBC1 0xc5 /* add with carry generation */
647 #define TOK_SUBC2 0xc6 /* add with carry use */
648 #define TOK_CUINT 0xc8 /* unsigned int constant */
649 #define TOK_CLLONG 0xc9 /* long long constant */
650 #define TOK_CULLONG 0xca /* unsigned long long constant */
651 #define TOK_ARROW 0xcb
652 #define TOK_DOTS 0xcc /* three dots */
653 #define TOK_SHR 0xcd /* unsigned shift right */
654 #define TOK_PPNUM 0xce /* preprocessor number */
656 #define TOK_SHL 0x01 /* shift left */
657 #define TOK_SAR 0x02 /* signed shift right */
659 /* assignement operators : normal operator or 0x80 */
660 #define TOK_A_MOD 0xa5
661 #define TOK_A_AND 0xa6
662 #define TOK_A_MUL 0xaa
663 #define TOK_A_ADD 0xab
664 #define TOK_A_SUB 0xad
665 #define TOK_A_DIV 0xaf
666 #define TOK_A_XOR 0xde
667 #define TOK_A_OR 0xfc
668 #define TOK_A_SHL 0x81
669 #define TOK_A_SAR 0x82
671 #ifndef offsetof
672 #define offsetof(type, field) ((size_t) &((type *)0)->field)
673 #endif
675 #ifndef countof
676 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
677 #endif
679 /* WARNING: the content of this string encodes token numbers */
680 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";
682 #define TOK_EOF (-1) /* end of file */
683 #define TOK_LINEFEED 10 /* line feed */
685 /* all identificators and strings have token above that */
686 #define TOK_IDENT 256
688 /* only used for i386 asm opcodes definitions */
689 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
691 #define DEF_BWL(x) \
692 DEF(TOK_ASM_ ## x ## b, #x "b") \
693 DEF(TOK_ASM_ ## x ## w, #x "w") \
694 DEF(TOK_ASM_ ## x ## l, #x "l") \
695 DEF(TOK_ASM_ ## x, #x)
697 #define DEF_WL(x) \
698 DEF(TOK_ASM_ ## x ## w, #x "w") \
699 DEF(TOK_ASM_ ## x ## l, #x "l") \
700 DEF(TOK_ASM_ ## x, #x)
702 #define DEF_FP1(x) \
703 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
704 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
705 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
706 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
708 #define DEF_FP(x) \
709 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
710 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
711 DEF_FP1(x)
713 #define DEF_ASMTEST(x) \
714 DEF_ASM(x ## o) \
715 DEF_ASM(x ## no) \
716 DEF_ASM(x ## b) \
717 DEF_ASM(x ## c) \
718 DEF_ASM(x ## nae) \
719 DEF_ASM(x ## nb) \
720 DEF_ASM(x ## nc) \
721 DEF_ASM(x ## ae) \
722 DEF_ASM(x ## e) \
723 DEF_ASM(x ## z) \
724 DEF_ASM(x ## ne) \
725 DEF_ASM(x ## nz) \
726 DEF_ASM(x ## be) \
727 DEF_ASM(x ## na) \
728 DEF_ASM(x ## nbe) \
729 DEF_ASM(x ## a) \
730 DEF_ASM(x ## s) \
731 DEF_ASM(x ## ns) \
732 DEF_ASM(x ## p) \
733 DEF_ASM(x ## pe) \
734 DEF_ASM(x ## np) \
735 DEF_ASM(x ## po) \
736 DEF_ASM(x ## l) \
737 DEF_ASM(x ## nge) \
738 DEF_ASM(x ## nl) \
739 DEF_ASM(x ## ge) \
740 DEF_ASM(x ## le) \
741 DEF_ASM(x ## ng) \
742 DEF_ASM(x ## nle) \
743 DEF_ASM(x ## g)
745 #define TOK_ASM_int TOK_INT
747 enum tcc_token {
748 TOK_LAST = TOK_IDENT - 1,
749 #define DEF(id, str) id,
750 #include "tcctok.h"
751 #undef DEF
754 static const char tcc_keywords[] =
755 #define DEF(id, str) str "\0"
756 #include "tcctok.h"
757 #undef DEF
760 #define TOK_UIDENT TOK_DEFINE
762 #ifdef _WIN32
763 #define snprintf _snprintf
764 #define vsnprintf _vsnprintf
765 #ifndef __GNUC__
766 #define strtold (long double)strtod
767 #define strtof (float)strtod
768 #define strtoll (long long)strtol
769 #endif
770 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
771 || defined(__OpenBSD__)
772 /* currently incorrect */
773 long double strtold(const char *nptr, char **endptr)
775 return (long double)strtod(nptr, endptr);
777 float strtof(const char *nptr, char **endptr)
779 return (float)strtod(nptr, endptr);
781 #else
782 /* XXX: need to define this to use them in non ISOC99 context */
783 extern float strtof (const char *__nptr, char **__endptr);
784 extern long double strtold (const char *__nptr, char **__endptr);
785 #endif
787 static char *pstrcpy(char *buf, int buf_size, const char *s);
788 static char *pstrcat(char *buf, int buf_size, const char *s);
789 static char *tcc_basename(const char *name);
790 static char *tcc_fileextension (const char *p);
792 static void next(void);
793 static void next_nomacro(void);
794 static void parse_expr_type(CType *type);
795 static void expr_type(CType *type);
796 static void unary_type(CType *type);
797 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
798 int case_reg, int is_expr);
799 static int expr_const(void);
800 static void expr_eq(void);
801 static void gexpr(void);
802 static void gen_inline_functions(void);
803 static void decl(int l);
804 static void decl_initializer(CType *type, Section *sec, unsigned long c,
805 int first, int size_only);
806 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
807 int has_init, int v, int scope);
808 int gv(int rc);
809 void gv2(int rc1, int rc2);
810 void move_reg(int r, int s);
811 void save_regs(int n);
812 void save_reg(int r);
813 void vpop(void);
814 void vswap(void);
815 void vdup(void);
816 int get_reg(int rc);
817 int get_reg_ex(int rc,int rc2);
819 struct macro_level {
820 struct macro_level *prev;
821 int *p;
824 static void macro_subst(TokenString *tok_str, Sym **nested_list,
825 const int *macro_str, struct macro_level **can_read_stream);
826 void gen_op(int op);
827 void force_charshort_cast(int t);
828 static void gen_cast(CType *type);
829 void vstore(void);
830 static Sym *sym_find(int v);
831 static Sym *sym_push(int v, CType *type, int r, int c);
833 /* type handling */
834 static int type_size(CType *type, int *a);
835 static inline CType *pointed_type(CType *type);
836 static int pointed_size(CType *type);
837 static int lvalue_type(int t);
838 static int parse_btype(CType *type, AttributeDef *ad);
839 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
840 static int compare_types(CType *type1, CType *type2, int unqualified);
841 static int is_compatible_types(CType *type1, CType *type2);
842 static int is_compatible_parameter_types(CType *type1, CType *type2);
844 int ieee_finite(double d);
845 void error(const char *fmt, ...);
846 void vpushi(int v);
847 void vrott(int n);
848 void vnrott(int n);
849 void lexpand_nr(void);
850 static void vpush_global_sym(CType *type, int v);
851 void vset(CType *type, int r, int v);
852 void type_to_str(char *buf, int buf_size,
853 CType *type, const char *varstr);
854 char *get_tok_str(int v, CValue *cv);
855 static Sym *get_sym_ref(CType *type, Section *sec,
856 unsigned long offset, unsigned long size);
857 static Sym *external_global_sym(int v, CType *type, int r);
859 /* section generation */
860 static void section_realloc(Section *sec, unsigned long new_size);
861 static void *section_ptr_add(Section *sec, unsigned long size);
862 static void put_extern_sym(Sym *sym, Section *section,
863 unsigned long value, unsigned long size);
864 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
865 static int put_elf_str(Section *s, const char *sym);
866 static int put_elf_sym(Section *s,
867 unsigned long value, unsigned long size,
868 int info, int other, int shndx, const char *name);
869 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
870 int info, int other, int sh_num, const char *name);
871 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
872 int type, int symbol);
873 static void put_stabs(const char *str, int type, int other, int desc,
874 unsigned long value);
875 static void put_stabs_r(const char *str, int type, int other, int desc,
876 unsigned long value, Section *sec, int sym_index);
877 static void put_stabn(int type, int other, int desc, int value);
878 static void put_stabd(int type, int other, int desc);
879 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
881 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
882 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
883 #define AFF_PREPROCESS 0x0004 /* preprocess file */
884 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
886 /* tcccoff.c */
887 int tcc_output_coff(TCCState *s1, FILE *f);
889 /* tccpe.c */
890 void *resolve_sym(TCCState *s1, const char *sym, int type);
891 int pe_load_def_file(struct TCCState *s1, int fd);
892 int pe_test_res_file(void *v, int size);
893 int pe_load_res_file(struct TCCState *s1, int fd);
894 void pe_add_runtime(struct TCCState *s1);
895 void pe_guess_outfile(char *objfilename, int output_type);
896 int pe_output_file(struct TCCState *s1, const char *filename);
898 /* tccasm.c */
900 #ifdef CONFIG_TCC_ASM
902 typedef struct ExprValue {
903 uint32_t v;
904 Sym *sym;
905 } ExprValue;
907 #define MAX_ASM_OPERANDS 30
909 typedef struct ASMOperand {
910 int id; /* GCC 3 optionnal identifier (0 if number only supported */
911 char *constraint;
912 char asm_str[16]; /* computed asm string for operand */
913 SValue *vt; /* C value of the expression */
914 int ref_index; /* if >= 0, gives reference to a output constraint */
915 int input_index; /* if >= 0, gives reference to an input constraint */
916 int priority; /* priority, used to assign registers */
917 int reg; /* if >= 0, register number used for this operand */
918 int is_llong; /* true if double register value */
919 int is_memory; /* true if memory operand */
920 int is_rw; /* for '+' modifier */
921 } ASMOperand;
923 static void asm_expr(TCCState *s1, ExprValue *pe);
924 static int asm_int_expr(TCCState *s1);
925 static int find_constraint(ASMOperand *operands, int nb_operands,
926 const char *name, const char **pp);
928 static int tcc_assemble(TCCState *s1, int do_preprocess);
930 #endif
932 static void asm_instr(void);
933 static void asm_global_instr(void);
935 /* true if float/double/long double type */
936 static inline int is_float(int t)
938 int bt;
939 bt = t & VT_BTYPE;
940 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
943 #ifdef TCC_TARGET_I386
944 #include "i386-gen.c"
945 #endif
947 #ifdef TCC_TARGET_ARM
948 #include "arm-gen.c"
949 #endif
951 #ifdef TCC_TARGET_C67
952 #include "c67-gen.c"
953 #endif
955 #ifdef TCC_TARGET_X86_64
956 #include "x86_64-gen.c"
957 #endif
959 #ifdef CONFIG_TCC_STATIC
961 #define RTLD_LAZY 0x001
962 #define RTLD_NOW 0x002
963 #define RTLD_GLOBAL 0x100
964 #define RTLD_DEFAULT NULL
966 /* dummy function for profiling */
967 void *dlopen(const char *filename, int flag)
969 return NULL;
972 const char *dlerror(void)
974 return "error";
977 typedef struct TCCSyms {
978 char *str;
979 void *ptr;
980 } TCCSyms;
982 #define TCCSYM(a) { #a, &a, },
984 /* add the symbol you want here if no dynamic linking is done */
985 static TCCSyms tcc_syms[] = {
986 #if !defined(CONFIG_TCCBOOT)
987 TCCSYM(printf)
988 TCCSYM(fprintf)
989 TCCSYM(fopen)
990 TCCSYM(fclose)
991 #endif
992 { NULL, NULL },
995 void *resolve_sym(TCCState *s1, const char *symbol, int type)
997 TCCSyms *p;
998 p = tcc_syms;
999 while (p->str != NULL) {
1000 if (!strcmp(p->str, symbol))
1001 return p->ptr;
1002 p++;
1004 return NULL;
1007 #elif !defined(_WIN32)
1009 #include <dlfcn.h>
1011 void *resolve_sym(TCCState *s1, const char *sym, int type)
1013 return dlsym(RTLD_DEFAULT, sym);
1016 #endif
1018 /********************************************************/
1020 /* we use our own 'finite' function to avoid potential problems with
1021 non standard math libs */
1022 /* XXX: endianness dependent */
1023 int ieee_finite(double d)
1025 int *p = (int *)&d;
1026 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
1029 /* copy a string and truncate it. */
1030 static char *pstrcpy(char *buf, int buf_size, const char *s)
1032 char *q, *q_end;
1033 int c;
1035 if (buf_size > 0) {
1036 q = buf;
1037 q_end = buf + buf_size - 1;
1038 while (q < q_end) {
1039 c = *s++;
1040 if (c == '\0')
1041 break;
1042 *q++ = c;
1044 *q = '\0';
1046 return buf;
1049 /* strcat and truncate. */
1050 static char *pstrcat(char *buf, int buf_size, const char *s)
1052 int len;
1053 len = strlen(buf);
1054 if (len < buf_size)
1055 pstrcpy(buf + len, buf_size - len, s);
1056 return buf;
1059 #ifndef LIBTCC
1060 static int strstart(const char *str, const char *val, const char **ptr)
1062 const char *p, *q;
1063 p = str;
1064 q = val;
1065 while (*q != '\0') {
1066 if (*p != *q)
1067 return 0;
1068 p++;
1069 q++;
1071 if (ptr)
1072 *ptr = p;
1073 return 1;
1075 #endif
1077 /* extract the basename of a file */
1078 static char *tcc_basename(const char *name)
1080 char *p = strchr(name, 0);
1081 while (p > name
1082 && p[-1] != '/'
1083 #ifdef _WIN32
1084 && p[-1] != '\\'
1085 #endif
1087 --p;
1088 return p;
1091 static char *tcc_fileextension (const char *name)
1093 char *b = tcc_basename(name);
1094 char *e = strrchr(b, '.');
1095 return e ? e : strchr(b, 0);
1098 #ifdef _WIN32
1099 char *normalize_slashes(char *path)
1101 char *p;
1102 for (p = path; *p; ++p)
1103 if (*p == '\\')
1104 *p = '/';
1105 return path;
1108 char *w32_tcc_lib_path(void)
1110 /* on win32, we suppose the lib and includes are at the location
1111 of 'tcc.exe' */
1112 char path[1024], *p;
1113 GetModuleFileNameA(NULL, path, sizeof path);
1114 p = tcc_basename(normalize_slashes(strlwr(path)));
1115 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
1116 p -= 5;
1117 else if (p > path)
1118 p--;
1119 *p = 0;
1120 return strdup(path);
1122 #endif
1124 void set_pages_executable(void *ptr, unsigned long length)
1126 #ifdef _WIN32
1127 unsigned long old_protect;
1128 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
1129 #else
1130 unsigned long start, end;
1131 start = (unsigned long)ptr & ~(PAGESIZE - 1);
1132 end = (unsigned long)ptr + length;
1133 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
1134 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
1135 #endif
1138 /* memory management */
1139 #ifdef MEM_DEBUG
1140 int mem_cur_size;
1141 int mem_max_size;
1142 unsigned malloc_usable_size(void*);
1143 #endif
1145 static inline void tcc_free(void *ptr)
1147 #ifdef MEM_DEBUG
1148 mem_cur_size -= malloc_usable_size(ptr);
1149 #endif
1150 free(ptr);
1153 static void *tcc_malloc(unsigned long size)
1155 void *ptr;
1156 ptr = malloc(size);
1157 if (!ptr && size)
1158 error("memory full");
1159 #ifdef MEM_DEBUG
1160 mem_cur_size += malloc_usable_size(ptr);
1161 if (mem_cur_size > mem_max_size)
1162 mem_max_size = mem_cur_size;
1163 #endif
1164 return ptr;
1167 static void *tcc_mallocz(unsigned long size)
1169 void *ptr;
1170 ptr = tcc_malloc(size);
1171 memset(ptr, 0, size);
1172 return ptr;
1175 static inline void *tcc_realloc(void *ptr, unsigned long size)
1177 void *ptr1;
1178 #ifdef MEM_DEBUG
1179 mem_cur_size -= malloc_usable_size(ptr);
1180 #endif
1181 ptr1 = realloc(ptr, size);
1182 #ifdef MEM_DEBUG
1183 /* NOTE: count not correct if alloc error, but not critical */
1184 mem_cur_size += malloc_usable_size(ptr1);
1185 if (mem_cur_size > mem_max_size)
1186 mem_max_size = mem_cur_size;
1187 #endif
1188 return ptr1;
1191 static char *tcc_strdup(const char *str)
1193 char *ptr;
1194 ptr = tcc_malloc(strlen(str) + 1);
1195 strcpy(ptr, str);
1196 return ptr;
1199 #define free(p) use_tcc_free(p)
1200 #define malloc(s) use_tcc_malloc(s)
1201 #define realloc(p, s) use_tcc_realloc(p, s)
1203 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1205 int nb, nb_alloc;
1206 void **pp;
1208 nb = *nb_ptr;
1209 pp = *ptab;
1210 /* every power of two we double array size */
1211 if ((nb & (nb - 1)) == 0) {
1212 if (!nb)
1213 nb_alloc = 1;
1214 else
1215 nb_alloc = nb * 2;
1216 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1217 if (!pp)
1218 error("memory full");
1219 *ptab = pp;
1221 pp[nb++] = data;
1222 *nb_ptr = nb;
1225 static void dynarray_reset(void *pp, int *n)
1227 void **p;
1228 for (p = *(void***)pp; *n; ++p, --*n)
1229 if (*p)
1230 tcc_free(*p);
1231 tcc_free(*(void**)pp);
1232 *(void**)pp = NULL;
1235 /* symbol allocator */
1236 static Sym *__sym_malloc(void)
1238 Sym *sym_pool, *sym, *last_sym;
1239 int i;
1241 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1242 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
1244 last_sym = sym_free_first;
1245 sym = sym_pool;
1246 for(i = 0; i < SYM_POOL_NB; i++) {
1247 sym->next = last_sym;
1248 last_sym = sym;
1249 sym++;
1251 sym_free_first = last_sym;
1252 return last_sym;
1255 static inline Sym *sym_malloc(void)
1257 Sym *sym;
1258 sym = sym_free_first;
1259 if (!sym)
1260 sym = __sym_malloc();
1261 sym_free_first = sym->next;
1262 return sym;
1265 static inline void sym_free(Sym *sym)
1267 sym->next = sym_free_first;
1268 sym_free_first = sym;
1271 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1273 Section *sec;
1275 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1276 strcpy(sec->name, name);
1277 sec->sh_type = sh_type;
1278 sec->sh_flags = sh_flags;
1279 switch(sh_type) {
1280 case SHT_HASH:
1281 case SHT_REL:
1282 case SHT_RELA:
1283 case SHT_DYNSYM:
1284 case SHT_SYMTAB:
1285 case SHT_DYNAMIC:
1286 sec->sh_addralign = 4;
1287 break;
1288 case SHT_STRTAB:
1289 sec->sh_addralign = 1;
1290 break;
1291 default:
1292 sec->sh_addralign = 32; /* default conservative alignment */
1293 break;
1296 /* only add section if not private */
1297 if (!(sh_flags & SHF_PRIVATE)) {
1298 sec->sh_num = s1->nb_sections;
1299 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1301 return sec;
1304 static void free_section(Section *s)
1306 if (s->link && (s->link->sh_flags & SHF_PRIVATE))
1307 free_section(s->link);
1308 if (s->hash && (s->hash->sh_flags & SHF_PRIVATE))
1309 s->hash->link = NULL, free_section(s->hash);
1310 tcc_free(s->data);
1311 tcc_free(s);
1314 /* realloc section and set its content to zero */
1315 static void section_realloc(Section *sec, unsigned long new_size)
1317 unsigned long size;
1318 unsigned char *data;
1320 size = sec->data_allocated;
1321 if (size == 0)
1322 size = 1;
1323 while (size < new_size)
1324 size = size * 2;
1325 data = tcc_realloc(sec->data, size);
1326 if (!data)
1327 error("memory full");
1328 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1329 sec->data = data;
1330 sec->data_allocated = size;
1333 /* reserve at least 'size' bytes in section 'sec' from
1334 sec->data_offset. */
1335 static void *section_ptr_add(Section *sec, unsigned long size)
1337 unsigned long offset, offset1;
1339 offset = sec->data_offset;
1340 offset1 = offset + size;
1341 if (offset1 > sec->data_allocated)
1342 section_realloc(sec, offset1);
1343 sec->data_offset = offset1;
1344 return sec->data + offset;
1347 /* return a reference to a section, and create it if it does not
1348 exists */
1349 Section *find_section(TCCState *s1, const char *name)
1351 Section *sec;
1352 int i;
1353 for(i = 1; i < s1->nb_sections; i++) {
1354 sec = s1->sections[i];
1355 if (!strcmp(name, sec->name))
1356 return sec;
1358 /* sections are created as PROGBITS */
1359 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1362 #define SECTION_ABS ((void *)1)
1364 /* update sym->c so that it points to an external symbol in section
1365 'section' with value 'value' */
1366 static void put_extern_sym2(Sym *sym, Section *section,
1367 unsigned long value, unsigned long size,
1368 int can_add_underscore)
1370 int sym_type, sym_bind, sh_num, info, other, attr;
1371 ElfW(Sym) *esym;
1372 const char *name;
1373 char buf1[256];
1375 if (section == NULL)
1376 sh_num = SHN_UNDEF;
1377 else if (section == SECTION_ABS)
1378 sh_num = SHN_ABS;
1379 else
1380 sh_num = section->sh_num;
1382 other = attr = 0;
1384 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1385 sym_type = STT_FUNC;
1386 #ifdef TCC_TARGET_PE
1387 if (sym->type.ref)
1388 attr = sym->type.ref->r;
1389 if (FUNC_EXPORT(attr))
1390 other |= 1;
1391 if (FUNC_CALL(attr) == FUNC_STDCALL)
1392 other |= 2;
1393 #endif
1394 } else {
1395 sym_type = STT_OBJECT;
1398 if (sym->type.t & VT_STATIC)
1399 sym_bind = STB_LOCAL;
1400 else
1401 sym_bind = STB_GLOBAL;
1403 if (!sym->c) {
1404 name = get_tok_str(sym->v, NULL);
1405 #ifdef CONFIG_TCC_BCHECK
1406 if (do_bounds_check) {
1407 char buf[32];
1409 /* XXX: avoid doing that for statics ? */
1410 /* if bound checking is activated, we change some function
1411 names by adding the "__bound" prefix */
1412 switch(sym->v) {
1413 #if 0
1414 /* XXX: we rely only on malloc hooks */
1415 case TOK_malloc:
1416 case TOK_free:
1417 case TOK_realloc:
1418 case TOK_memalign:
1419 case TOK_calloc:
1420 #endif
1421 case TOK_memcpy:
1422 case TOK_memmove:
1423 case TOK_memset:
1424 case TOK_strlen:
1425 case TOK_strcpy:
1426 case TOK__alloca:
1427 strcpy(buf, "__bound_");
1428 strcat(buf, name);
1429 name = buf;
1430 break;
1433 #endif
1435 #ifdef TCC_TARGET_PE
1436 if ((other & 2) && can_add_underscore) {
1437 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1438 name = buf1;
1439 } else
1440 #endif
1441 if (tcc_state->leading_underscore && can_add_underscore) {
1442 buf1[0] = '_';
1443 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1444 name = buf1;
1446 info = ELFW(ST_INFO)(sym_bind, sym_type);
1447 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1448 } else {
1449 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
1450 esym->st_value = value;
1451 esym->st_size = size;
1452 esym->st_shndx = sh_num;
1453 esym->st_other |= other;
1457 static void put_extern_sym(Sym *sym, Section *section,
1458 unsigned long value, unsigned long size)
1460 put_extern_sym2(sym, section, value, size, 1);
1463 /* add a new relocation entry to symbol 'sym' in section 's' */
1464 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1466 if (!sym->c)
1467 put_extern_sym(sym, NULL, 0, 0);
1468 /* now we can add ELF relocation info */
1469 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1472 static inline int isid(int c)
1474 return (c >= 'a' && c <= 'z') ||
1475 (c >= 'A' && c <= 'Z') ||
1476 c == '_';
1479 static inline int isnum(int c)
1481 return c >= '0' && c <= '9';
1484 static inline int isoct(int c)
1486 return c >= '0' && c <= '7';
1489 static inline int toup(int c)
1491 if (c >= 'a' && c <= 'z')
1492 return c - 'a' + 'A';
1493 else
1494 return c;
1497 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1499 int len;
1500 len = strlen(buf);
1501 vsnprintf(buf + len, buf_size - len, fmt, ap);
1504 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1506 va_list ap;
1507 va_start(ap, fmt);
1508 strcat_vprintf(buf, buf_size, fmt, ap);
1509 va_end(ap);
1512 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1514 char buf[2048];
1515 BufferedFile **f;
1517 buf[0] = '\0';
1518 if (file) {
1519 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1520 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1521 (*f)->filename, (*f)->line_num);
1522 if (file->line_num > 0) {
1523 strcat_printf(buf, sizeof(buf),
1524 "%s:%d: ", file->filename, file->line_num);
1525 } else {
1526 strcat_printf(buf, sizeof(buf),
1527 "%s: ", file->filename);
1529 } else {
1530 strcat_printf(buf, sizeof(buf),
1531 "tcc: ");
1533 if (is_warning)
1534 strcat_printf(buf, sizeof(buf), "warning: ");
1535 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1537 if (!s1->error_func) {
1538 /* default case: stderr */
1539 fprintf(stderr, "%s\n", buf);
1540 } else {
1541 s1->error_func(s1->error_opaque, buf);
1543 if (!is_warning || s1->warn_error)
1544 s1->nb_errors++;
1547 #ifdef LIBTCC
1548 void tcc_set_error_func(TCCState *s, void *error_opaque,
1549 void (*error_func)(void *opaque, const char *msg))
1551 s->error_opaque = error_opaque;
1552 s->error_func = error_func;
1554 #endif
1556 /* error without aborting current compilation */
1557 void error_noabort(const char *fmt, ...)
1559 TCCState *s1 = tcc_state;
1560 va_list ap;
1562 va_start(ap, fmt);
1563 error1(s1, 0, fmt, ap);
1564 va_end(ap);
1567 void error(const char *fmt, ...)
1569 TCCState *s1 = tcc_state;
1570 va_list ap;
1572 va_start(ap, fmt);
1573 error1(s1, 0, fmt, ap);
1574 va_end(ap);
1575 /* better than nothing: in some cases, we accept to handle errors */
1576 if (s1->error_set_jmp_enabled) {
1577 longjmp(s1->error_jmp_buf, 1);
1578 } else {
1579 /* XXX: eliminate this someday */
1580 exit(1);
1584 void expect(const char *msg)
1586 error("%s expected", msg);
1589 void warning(const char *fmt, ...)
1591 TCCState *s1 = tcc_state;
1592 va_list ap;
1594 if (s1->warn_none)
1595 return;
1597 va_start(ap, fmt);
1598 error1(s1, 1, fmt, ap);
1599 va_end(ap);
1602 void skip(int c)
1604 if (tok != c)
1605 error("'%c' expected", c);
1606 next();
1609 static void test_lvalue(void)
1611 if (!(vtop->r & VT_LVAL))
1612 expect("lvalue");
1615 /* allocate a new token */
1616 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1618 TokenSym *ts, **ptable;
1619 int i;
1621 if (tok_ident >= SYM_FIRST_ANOM)
1622 error("memory full");
1624 /* expand token table if needed */
1625 i = tok_ident - TOK_IDENT;
1626 if ((i % TOK_ALLOC_INCR) == 0) {
1627 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1628 if (!ptable)
1629 error("memory full");
1630 table_ident = ptable;
1633 ts = tcc_malloc(sizeof(TokenSym) + len);
1634 table_ident[i] = ts;
1635 ts->tok = tok_ident++;
1636 ts->sym_define = NULL;
1637 ts->sym_label = NULL;
1638 ts->sym_struct = NULL;
1639 ts->sym_identifier = NULL;
1640 ts->len = len;
1641 ts->hash_next = NULL;
1642 memcpy(ts->str, str, len);
1643 ts->str[len] = '\0';
1644 *pts = ts;
1645 return ts;
1648 #define TOK_HASH_INIT 1
1649 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1651 /* find a token and add it if not found */
1652 static TokenSym *tok_alloc(const char *str, int len)
1654 TokenSym *ts, **pts;
1655 int i;
1656 unsigned int h;
1658 h = TOK_HASH_INIT;
1659 for(i=0;i<len;i++)
1660 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1661 h &= (TOK_HASH_SIZE - 1);
1663 pts = &hash_ident[h];
1664 for(;;) {
1665 ts = *pts;
1666 if (!ts)
1667 break;
1668 if (ts->len == len && !memcmp(ts->str, str, len))
1669 return ts;
1670 pts = &(ts->hash_next);
1672 return tok_alloc_new(pts, str, len);
1675 /* CString handling */
1677 static void cstr_realloc(CString *cstr, int new_size)
1679 int size;
1680 void *data;
1682 size = cstr->size_allocated;
1683 if (size == 0)
1684 size = 8; /* no need to allocate a too small first string */
1685 while (size < new_size)
1686 size = size * 2;
1687 data = tcc_realloc(cstr->data_allocated, size);
1688 if (!data)
1689 error("memory full");
1690 cstr->data_allocated = data;
1691 cstr->size_allocated = size;
1692 cstr->data = data;
1695 /* add a byte */
1696 static inline void cstr_ccat(CString *cstr, int ch)
1698 int size;
1699 size = cstr->size + 1;
1700 if (size > cstr->size_allocated)
1701 cstr_realloc(cstr, size);
1702 ((unsigned char *)cstr->data)[size - 1] = ch;
1703 cstr->size = size;
1706 static void cstr_cat(CString *cstr, const char *str)
1708 int c;
1709 for(;;) {
1710 c = *str;
1711 if (c == '\0')
1712 break;
1713 cstr_ccat(cstr, c);
1714 str++;
1718 /* add a wide char */
1719 static void cstr_wccat(CString *cstr, int ch)
1721 int size;
1722 size = cstr->size + sizeof(nwchar_t);
1723 if (size > cstr->size_allocated)
1724 cstr_realloc(cstr, size);
1725 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
1726 cstr->size = size;
1729 static void cstr_new(CString *cstr)
1731 memset(cstr, 0, sizeof(CString));
1734 /* free string and reset it to NULL */
1735 static void cstr_free(CString *cstr)
1737 tcc_free(cstr->data_allocated);
1738 cstr_new(cstr);
1741 #define cstr_reset(cstr) cstr_free(cstr)
1743 /* XXX: unicode ? */
1744 static void add_char(CString *cstr, int c)
1746 if (c == '\'' || c == '\"' || c == '\\') {
1747 /* XXX: could be more precise if char or string */
1748 cstr_ccat(cstr, '\\');
1750 if (c >= 32 && c <= 126) {
1751 cstr_ccat(cstr, c);
1752 } else {
1753 cstr_ccat(cstr, '\\');
1754 if (c == '\n') {
1755 cstr_ccat(cstr, 'n');
1756 } else {
1757 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1758 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1759 cstr_ccat(cstr, '0' + (c & 7));
1764 /* XXX: buffer overflow */
1765 /* XXX: float tokens */
1766 char *get_tok_str(int v, CValue *cv)
1768 static char buf[STRING_MAX_SIZE + 1];
1769 static CString cstr_buf;
1770 CString *cstr;
1771 unsigned char *q;
1772 char *p;
1773 int i, len;
1775 /* NOTE: to go faster, we give a fixed buffer for small strings */
1776 cstr_reset(&cstr_buf);
1777 cstr_buf.data = buf;
1778 cstr_buf.size_allocated = sizeof(buf);
1779 p = buf;
1781 switch(v) {
1782 case TOK_CINT:
1783 case TOK_CUINT:
1784 /* XXX: not quite exact, but only useful for testing */
1785 sprintf(p, "%u", cv->ui);
1786 break;
1787 case TOK_CLLONG:
1788 case TOK_CULLONG:
1789 /* XXX: not quite exact, but only useful for testing */
1790 sprintf(p, "%Lu", cv->ull);
1791 break;
1792 case TOK_LCHAR:
1793 cstr_ccat(&cstr_buf, 'L');
1794 case TOK_CCHAR:
1795 cstr_ccat(&cstr_buf, '\'');
1796 add_char(&cstr_buf, cv->i);
1797 cstr_ccat(&cstr_buf, '\'');
1798 cstr_ccat(&cstr_buf, '\0');
1799 break;
1800 case TOK_PPNUM:
1801 cstr = cv->cstr;
1802 len = cstr->size - 1;
1803 for(i=0;i<len;i++)
1804 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1805 cstr_ccat(&cstr_buf, '\0');
1806 break;
1807 case TOK_LSTR:
1808 cstr_ccat(&cstr_buf, 'L');
1809 case TOK_STR:
1810 cstr = cv->cstr;
1811 cstr_ccat(&cstr_buf, '\"');
1812 if (v == TOK_STR) {
1813 len = cstr->size - 1;
1814 for(i=0;i<len;i++)
1815 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1816 } else {
1817 len = (cstr->size / sizeof(nwchar_t)) - 1;
1818 for(i=0;i<len;i++)
1819 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1821 cstr_ccat(&cstr_buf, '\"');
1822 cstr_ccat(&cstr_buf, '\0');
1823 break;
1824 case TOK_LT:
1825 v = '<';
1826 goto addv;
1827 case TOK_GT:
1828 v = '>';
1829 goto addv;
1830 case TOK_DOTS:
1831 return strcpy(p, "...");
1832 case TOK_A_SHL:
1833 return strcpy(p, "<<=");
1834 case TOK_A_SAR:
1835 return strcpy(p, ">>=");
1836 default:
1837 if (v < TOK_IDENT) {
1838 /* search in two bytes table */
1839 q = tok_two_chars;
1840 while (*q) {
1841 if (q[2] == v) {
1842 *p++ = q[0];
1843 *p++ = q[1];
1844 *p = '\0';
1845 return buf;
1847 q += 3;
1849 addv:
1850 *p++ = v;
1851 *p = '\0';
1852 } else if (v < tok_ident) {
1853 return table_ident[v - TOK_IDENT]->str;
1854 } else if (v >= SYM_FIRST_ANOM) {
1855 /* special name for anonymous symbol */
1856 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1857 } else {
1858 /* should never happen */
1859 return NULL;
1861 break;
1863 return cstr_buf.data;
1866 /* push, without hashing */
1867 static Sym *sym_push2(Sym **ps, int v, int t, long c)
1869 Sym *s;
1870 s = sym_malloc();
1871 s->v = v;
1872 s->type.t = t;
1873 s->c = c;
1874 s->next = NULL;
1875 /* add in stack */
1876 s->prev = *ps;
1877 *ps = s;
1878 return s;
1881 /* find a symbol and return its associated structure. 's' is the top
1882 of the symbol stack */
1883 static Sym *sym_find2(Sym *s, int v)
1885 while (s) {
1886 if (s->v == v)
1887 return s;
1888 s = s->prev;
1890 return NULL;
1893 /* structure lookup */
1894 static inline Sym *struct_find(int v)
1896 v -= TOK_IDENT;
1897 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1898 return NULL;
1899 return table_ident[v]->sym_struct;
1902 /* find an identifier */
1903 static inline Sym *sym_find(int v)
1905 v -= TOK_IDENT;
1906 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1907 return NULL;
1908 return table_ident[v]->sym_identifier;
1911 /* push a given symbol on the symbol stack */
1912 static Sym *sym_push(int v, CType *type, int r, int c)
1914 Sym *s, **ps;
1915 TokenSym *ts;
1917 if (local_stack)
1918 ps = &local_stack;
1919 else
1920 ps = &global_stack;
1921 s = sym_push2(ps, v, type->t, c);
1922 s->type.ref = type->ref;
1923 s->r = r;
1924 /* don't record fields or anonymous symbols */
1925 /* XXX: simplify */
1926 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1927 /* record symbol in token array */
1928 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1929 if (v & SYM_STRUCT)
1930 ps = &ts->sym_struct;
1931 else
1932 ps = &ts->sym_identifier;
1933 s->prev_tok = *ps;
1934 *ps = s;
1936 return s;
1939 /* push a global identifier */
1940 static Sym *global_identifier_push(int v, int t, int c)
1942 Sym *s, **ps;
1943 s = sym_push2(&global_stack, v, t, c);
1944 /* don't record anonymous symbol */
1945 if (v < SYM_FIRST_ANOM) {
1946 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1947 /* modify the top most local identifier, so that
1948 sym_identifier will point to 's' when popped */
1949 while (*ps != NULL)
1950 ps = &(*ps)->prev_tok;
1951 s->prev_tok = NULL;
1952 *ps = s;
1954 return s;
1957 /* pop symbols until top reaches 'b' */
1958 static void sym_pop(Sym **ptop, Sym *b)
1960 Sym *s, *ss, **ps;
1961 TokenSym *ts;
1962 int v;
1964 s = *ptop;
1965 while(s != b) {
1966 ss = s->prev;
1967 v = s->v;
1968 /* remove symbol in token array */
1969 /* XXX: simplify */
1970 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1971 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1972 if (v & SYM_STRUCT)
1973 ps = &ts->sym_struct;
1974 else
1975 ps = &ts->sym_identifier;
1976 *ps = s->prev_tok;
1978 sym_free(s);
1979 s = ss;
1981 *ptop = b;
1984 /* I/O layer */
1986 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1988 int fd;
1989 BufferedFile *bf;
1991 if (strcmp(filename, "-") == 0)
1992 fd = 0, filename = "stdin";
1993 else
1994 fd = open(filename, O_RDONLY | O_BINARY);
1995 if ((verbose == 2 && fd >= 0) || verbose == 3)
1996 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
1997 (s1->include_stack_ptr - s1->include_stack), "", filename);
1998 if (fd < 0)
1999 return NULL;
2000 bf = tcc_malloc(sizeof(BufferedFile));
2001 bf->fd = fd;
2002 bf->buf_ptr = bf->buffer;
2003 bf->buf_end = bf->buffer;
2004 bf->buffer[0] = CH_EOB; /* put eob symbol */
2005 pstrcpy(bf->filename, sizeof(bf->filename), filename);
2006 #ifdef _WIN32
2007 normalize_slashes(bf->filename);
2008 #endif
2009 bf->line_num = 1;
2010 bf->ifndef_macro = 0;
2011 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2012 // printf("opening '%s'\n", filename);
2013 return bf;
2016 void tcc_close(BufferedFile *bf)
2018 total_lines += bf->line_num;
2019 close(bf->fd);
2020 tcc_free(bf);
2023 /* fill input buffer and peek next char */
2024 static int tcc_peekc_slow(BufferedFile *bf)
2026 int len;
2027 /* only tries to read if really end of buffer */
2028 if (bf->buf_ptr >= bf->buf_end) {
2029 if (bf->fd != -1) {
2030 #if defined(PARSE_DEBUG)
2031 len = 8;
2032 #else
2033 len = IO_BUF_SIZE;
2034 #endif
2035 len = read(bf->fd, bf->buffer, len);
2036 if (len < 0)
2037 len = 0;
2038 } else {
2039 len = 0;
2041 total_bytes += len;
2042 bf->buf_ptr = bf->buffer;
2043 bf->buf_end = bf->buffer + len;
2044 *bf->buf_end = CH_EOB;
2046 if (bf->buf_ptr < bf->buf_end) {
2047 return bf->buf_ptr[0];
2048 } else {
2049 bf->buf_ptr = bf->buf_end;
2050 return CH_EOF;
2054 /* return the current character, handling end of block if necessary
2055 (but not stray) */
2056 static int handle_eob(void)
2058 return tcc_peekc_slow(file);
2061 /* read next char from current input file and handle end of input buffer */
2062 static inline void inp(void)
2064 ch = *(++(file->buf_ptr));
2065 /* end of buffer/file handling */
2066 if (ch == CH_EOB)
2067 ch = handle_eob();
2070 /* handle '\[\r]\n' */
2071 static int handle_stray_noerror(void)
2073 while (ch == '\\') {
2074 inp();
2075 if (ch == '\n') {
2076 file->line_num++;
2077 inp();
2078 } else if (ch == '\r') {
2079 inp();
2080 if (ch != '\n')
2081 goto fail;
2082 file->line_num++;
2083 inp();
2084 } else {
2085 fail:
2086 return 1;
2089 return 0;
2092 static void handle_stray(void)
2094 if (handle_stray_noerror())
2095 error("stray '\\' in program");
2098 /* skip the stray and handle the \\n case. Output an error if
2099 incorrect char after the stray */
2100 static int handle_stray1(uint8_t *p)
2102 int c;
2104 if (p >= file->buf_end) {
2105 file->buf_ptr = p;
2106 c = handle_eob();
2107 p = file->buf_ptr;
2108 if (c == '\\')
2109 goto parse_stray;
2110 } else {
2111 parse_stray:
2112 file->buf_ptr = p;
2113 ch = *p;
2114 handle_stray();
2115 p = file->buf_ptr;
2116 c = *p;
2118 return c;
2121 /* handle just the EOB case, but not stray */
2122 #define PEEKC_EOB(c, p)\
2124 p++;\
2125 c = *p;\
2126 if (c == '\\') {\
2127 file->buf_ptr = p;\
2128 c = handle_eob();\
2129 p = file->buf_ptr;\
2133 /* handle the complicated stray case */
2134 #define PEEKC(c, p)\
2136 p++;\
2137 c = *p;\
2138 if (c == '\\') {\
2139 c = handle_stray1(p);\
2140 p = file->buf_ptr;\
2144 /* input with '\[\r]\n' handling. Note that this function cannot
2145 handle other characters after '\', so you cannot call it inside
2146 strings or comments */
2147 static void minp(void)
2149 inp();
2150 if (ch == '\\')
2151 handle_stray();
2155 /* single line C++ comments */
2156 static uint8_t *parse_line_comment(uint8_t *p)
2158 int c;
2160 p++;
2161 for(;;) {
2162 c = *p;
2163 redo:
2164 if (c == '\n' || c == CH_EOF) {
2165 break;
2166 } else if (c == '\\') {
2167 file->buf_ptr = p;
2168 c = handle_eob();
2169 p = file->buf_ptr;
2170 if (c == '\\') {
2171 PEEKC_EOB(c, p);
2172 if (c == '\n') {
2173 file->line_num++;
2174 PEEKC_EOB(c, p);
2175 } else if (c == '\r') {
2176 PEEKC_EOB(c, p);
2177 if (c == '\n') {
2178 file->line_num++;
2179 PEEKC_EOB(c, p);
2182 } else {
2183 goto redo;
2185 } else {
2186 p++;
2189 return p;
2192 /* C comments */
2193 static uint8_t *parse_comment(uint8_t *p)
2195 int c;
2197 p++;
2198 for(;;) {
2199 /* fast skip loop */
2200 for(;;) {
2201 c = *p;
2202 if (c == '\n' || c == '*' || c == '\\')
2203 break;
2204 p++;
2205 c = *p;
2206 if (c == '\n' || c == '*' || c == '\\')
2207 break;
2208 p++;
2210 /* now we can handle all the cases */
2211 if (c == '\n') {
2212 file->line_num++;
2213 p++;
2214 } else if (c == '*') {
2215 p++;
2216 for(;;) {
2217 c = *p;
2218 if (c == '*') {
2219 p++;
2220 } else if (c == '/') {
2221 goto end_of_comment;
2222 } else if (c == '\\') {
2223 file->buf_ptr = p;
2224 c = handle_eob();
2225 p = file->buf_ptr;
2226 if (c == '\\') {
2227 /* skip '\[\r]\n', otherwise just skip the stray */
2228 while (c == '\\') {
2229 PEEKC_EOB(c, p);
2230 if (c == '\n') {
2231 file->line_num++;
2232 PEEKC_EOB(c, p);
2233 } else if (c == '\r') {
2234 PEEKC_EOB(c, p);
2235 if (c == '\n') {
2236 file->line_num++;
2237 PEEKC_EOB(c, p);
2239 } else {
2240 goto after_star;
2244 } else {
2245 break;
2248 after_star: ;
2249 } else {
2250 /* stray, eob or eof */
2251 file->buf_ptr = p;
2252 c = handle_eob();
2253 p = file->buf_ptr;
2254 if (c == CH_EOF) {
2255 error("unexpected end of file in comment");
2256 } else if (c == '\\') {
2257 p++;
2261 end_of_comment:
2262 p++;
2263 return p;
2266 #define cinp minp
2268 /* space exlcuding newline */
2269 static inline int is_space(int ch)
2271 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2274 static inline void skip_spaces(void)
2276 while (is_space(ch))
2277 cinp();
2280 /* parse a string without interpreting escapes */
2281 static uint8_t *parse_pp_string(uint8_t *p,
2282 int sep, CString *str)
2284 int c;
2285 p++;
2286 for(;;) {
2287 c = *p;
2288 if (c == sep) {
2289 break;
2290 } else if (c == '\\') {
2291 file->buf_ptr = p;
2292 c = handle_eob();
2293 p = file->buf_ptr;
2294 if (c == CH_EOF) {
2295 unterminated_string:
2296 /* XXX: indicate line number of start of string */
2297 error("missing terminating %c character", sep);
2298 } else if (c == '\\') {
2299 /* escape : just skip \[\r]\n */
2300 PEEKC_EOB(c, p);
2301 if (c == '\n') {
2302 file->line_num++;
2303 p++;
2304 } else if (c == '\r') {
2305 PEEKC_EOB(c, p);
2306 if (c != '\n')
2307 expect("'\n' after '\r'");
2308 file->line_num++;
2309 p++;
2310 } else if (c == CH_EOF) {
2311 goto unterminated_string;
2312 } else {
2313 if (str) {
2314 cstr_ccat(str, '\\');
2315 cstr_ccat(str, c);
2317 p++;
2320 } else if (c == '\n') {
2321 file->line_num++;
2322 goto add_char;
2323 } else if (c == '\r') {
2324 PEEKC_EOB(c, p);
2325 if (c != '\n') {
2326 if (str)
2327 cstr_ccat(str, '\r');
2328 } else {
2329 file->line_num++;
2330 goto add_char;
2332 } else {
2333 add_char:
2334 if (str)
2335 cstr_ccat(str, c);
2336 p++;
2339 p++;
2340 return p;
2343 /* skip block of text until #else, #elif or #endif. skip also pairs of
2344 #if/#endif */
2345 void preprocess_skip(void)
2347 int a, start_of_line, c, in_warn_or_error;
2348 uint8_t *p;
2350 p = file->buf_ptr;
2351 a = 0;
2352 redo_start:
2353 start_of_line = 1;
2354 in_warn_or_error = 0;
2355 for(;;) {
2356 redo_no_start:
2357 c = *p;
2358 switch(c) {
2359 case ' ':
2360 case '\t':
2361 case '\f':
2362 case '\v':
2363 case '\r':
2364 p++;
2365 goto redo_no_start;
2366 case '\n':
2367 file->line_num++;
2368 p++;
2369 goto redo_start;
2370 case '\\':
2371 file->buf_ptr = p;
2372 c = handle_eob();
2373 if (c == CH_EOF) {
2374 expect("#endif");
2375 } else if (c == '\\') {
2376 ch = file->buf_ptr[0];
2377 handle_stray_noerror();
2379 p = file->buf_ptr;
2380 goto redo_no_start;
2381 /* skip strings */
2382 case '\"':
2383 case '\'':
2384 if (in_warn_or_error)
2385 goto _default;
2386 p = parse_pp_string(p, c, NULL);
2387 break;
2388 /* skip comments */
2389 case '/':
2390 if (in_warn_or_error)
2391 goto _default;
2392 file->buf_ptr = p;
2393 ch = *p;
2394 minp();
2395 p = file->buf_ptr;
2396 if (ch == '*') {
2397 p = parse_comment(p);
2398 } else if (ch == '/') {
2399 p = parse_line_comment(p);
2401 break;
2402 case '#':
2403 p++;
2404 if (start_of_line) {
2405 file->buf_ptr = p;
2406 next_nomacro();
2407 p = file->buf_ptr;
2408 if (a == 0 &&
2409 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2410 goto the_end;
2411 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2412 a++;
2413 else if (tok == TOK_ENDIF)
2414 a--;
2415 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2416 in_warn_or_error = 1;
2418 break;
2419 _default:
2420 default:
2421 p++;
2422 break;
2424 start_of_line = 0;
2426 the_end: ;
2427 file->buf_ptr = p;
2430 /* ParseState handling */
2432 /* XXX: currently, no include file info is stored. Thus, we cannot display
2433 accurate messages if the function or data definition spans multiple
2434 files */
2436 /* save current parse state in 's' */
2437 void save_parse_state(ParseState *s)
2439 s->line_num = file->line_num;
2440 s->macro_ptr = macro_ptr;
2441 s->tok = tok;
2442 s->tokc = tokc;
2445 /* restore parse state from 's' */
2446 void restore_parse_state(ParseState *s)
2448 file->line_num = s->line_num;
2449 macro_ptr = s->macro_ptr;
2450 tok = s->tok;
2451 tokc = s->tokc;
2454 /* return the number of additional 'ints' necessary to store the
2455 token */
2456 static inline int tok_ext_size(int t)
2458 switch(t) {
2459 /* 4 bytes */
2460 case TOK_CINT:
2461 case TOK_CUINT:
2462 case TOK_CCHAR:
2463 case TOK_LCHAR:
2464 case TOK_CFLOAT:
2465 case TOK_LINENUM:
2466 return 1;
2467 case TOK_STR:
2468 case TOK_LSTR:
2469 case TOK_PPNUM:
2470 error("unsupported token");
2471 return 1;
2472 case TOK_CDOUBLE:
2473 case TOK_CLLONG:
2474 case TOK_CULLONG:
2475 return 2;
2476 case TOK_CLDOUBLE:
2477 return LDOUBLE_SIZE / 4;
2478 default:
2479 return 0;
2483 /* token string handling */
2485 static inline void tok_str_new(TokenString *s)
2487 s->str = NULL;
2488 s->len = 0;
2489 s->allocated_len = 0;
2490 s->last_line_num = -1;
2493 static void tok_str_free(int *str)
2495 tcc_free(str);
2498 static int *tok_str_realloc(TokenString *s)
2500 int *str, len;
2502 if (s->allocated_len == 0) {
2503 len = 8;
2504 } else {
2505 len = s->allocated_len * 2;
2507 str = tcc_realloc(s->str, len * sizeof(int));
2508 if (!str)
2509 error("memory full");
2510 s->allocated_len = len;
2511 s->str = str;
2512 return str;
2515 static void tok_str_add(TokenString *s, int t)
2517 int len, *str;
2519 len = s->len;
2520 str = s->str;
2521 if (len >= s->allocated_len)
2522 str = tok_str_realloc(s);
2523 str[len++] = t;
2524 s->len = len;
2527 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2529 int len, *str;
2531 len = s->len;
2532 str = s->str;
2534 /* allocate space for worst case */
2535 if (len + TOK_MAX_SIZE > s->allocated_len)
2536 str = tok_str_realloc(s);
2537 str[len++] = t;
2538 switch(t) {
2539 case TOK_CINT:
2540 case TOK_CUINT:
2541 case TOK_CCHAR:
2542 case TOK_LCHAR:
2543 case TOK_CFLOAT:
2544 case TOK_LINENUM:
2545 str[len++] = cv->tab[0];
2546 break;
2547 case TOK_PPNUM:
2548 case TOK_STR:
2549 case TOK_LSTR:
2551 int nb_words;
2552 CString *cstr;
2554 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2555 while ((len + nb_words) > s->allocated_len)
2556 str = tok_str_realloc(s);
2557 cstr = (CString *)(str + len);
2558 cstr->data = NULL;
2559 cstr->size = cv->cstr->size;
2560 cstr->data_allocated = NULL;
2561 cstr->size_allocated = cstr->size;
2562 memcpy((char *)cstr + sizeof(CString),
2563 cv->cstr->data, cstr->size);
2564 len += nb_words;
2566 break;
2567 case TOK_CDOUBLE:
2568 case TOK_CLLONG:
2569 case TOK_CULLONG:
2570 #if LDOUBLE_SIZE == 8
2571 case TOK_CLDOUBLE:
2572 #endif
2573 str[len++] = cv->tab[0];
2574 str[len++] = cv->tab[1];
2575 break;
2576 #if LDOUBLE_SIZE == 12
2577 case TOK_CLDOUBLE:
2578 str[len++] = cv->tab[0];
2579 str[len++] = cv->tab[1];
2580 str[len++] = cv->tab[2];
2581 #elif LDOUBLE_SIZE == 16
2582 case TOK_CLDOUBLE:
2583 str[len++] = cv->tab[0];
2584 str[len++] = cv->tab[1];
2585 str[len++] = cv->tab[2];
2586 str[len++] = cv->tab[3];
2587 #elif LDOUBLE_SIZE != 8
2588 #error add long double size support
2589 #endif
2590 break;
2591 default:
2592 break;
2594 s->len = len;
2597 /* add the current parse token in token string 's' */
2598 static void tok_str_add_tok(TokenString *s)
2600 CValue cval;
2602 /* save line number info */
2603 if (file->line_num != s->last_line_num) {
2604 s->last_line_num = file->line_num;
2605 cval.i = s->last_line_num;
2606 tok_str_add2(s, TOK_LINENUM, &cval);
2608 tok_str_add2(s, tok, &tokc);
2611 #if LDOUBLE_SIZE == 16
2612 #define LDOUBLE_GET(p, cv) \
2613 cv.tab[0] = p[0]; \
2614 cv.tab[1] = p[1]; \
2615 cv.tab[2] = p[2]; \
2616 cv.tab[3] = p[3];
2617 #elif LDOUBLE_SIZE == 12
2618 #define LDOUBLE_GET(p, cv) \
2619 cv.tab[0] = p[0]; \
2620 cv.tab[1] = p[1]; \
2621 cv.tab[2] = p[2];
2622 #elif LDOUBLE_SIZE == 8
2623 #define LDOUBLE_GET(p, cv) \
2624 cv.tab[0] = p[0]; \
2625 cv.tab[1] = p[1];
2626 #else
2627 #error add long double size support
2628 #endif
2631 /* get a token from an integer array and increment pointer
2632 accordingly. we code it as a macro to avoid pointer aliasing. */
2633 #define TOK_GET(t, p, cv) \
2635 t = *p++; \
2636 switch(t) { \
2637 case TOK_CINT: \
2638 case TOK_CUINT: \
2639 case TOK_CCHAR: \
2640 case TOK_LCHAR: \
2641 case TOK_CFLOAT: \
2642 case TOK_LINENUM: \
2643 cv.tab[0] = *p++; \
2644 break; \
2645 case TOK_STR: \
2646 case TOK_LSTR: \
2647 case TOK_PPNUM: \
2648 cv.cstr = (CString *)p; \
2649 cv.cstr->data = (char *)p + sizeof(CString);\
2650 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2651 break; \
2652 case TOK_CDOUBLE: \
2653 case TOK_CLLONG: \
2654 case TOK_CULLONG: \
2655 cv.tab[0] = p[0]; \
2656 cv.tab[1] = p[1]; \
2657 p += 2; \
2658 break; \
2659 case TOK_CLDOUBLE: \
2660 LDOUBLE_GET(p, cv); \
2661 p += LDOUBLE_SIZE / 4; \
2662 break; \
2663 default: \
2664 break; \
2668 /* defines handling */
2669 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2671 Sym *s;
2673 s = sym_push2(&define_stack, v, macro_type, (long)str);
2674 s->next = first_arg;
2675 table_ident[v - TOK_IDENT]->sym_define = s;
2678 /* undefined a define symbol. Its name is just set to zero */
2679 static void define_undef(Sym *s)
2681 int v;
2682 v = s->v;
2683 if (v >= TOK_IDENT && v < tok_ident)
2684 table_ident[v - TOK_IDENT]->sym_define = NULL;
2685 s->v = 0;
2688 static inline Sym *define_find(int v)
2690 v -= TOK_IDENT;
2691 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2692 return NULL;
2693 return table_ident[v]->sym_define;
2696 /* free define stack until top reaches 'b' */
2697 static void free_defines(Sym *b)
2699 Sym *top, *top1;
2700 int v;
2702 top = define_stack;
2703 while (top != b) {
2704 top1 = top->prev;
2705 /* do not free args or predefined defines */
2706 if (top->c)
2707 tok_str_free((int *)top->c);
2708 v = top->v;
2709 if (v >= TOK_IDENT && v < tok_ident)
2710 table_ident[v - TOK_IDENT]->sym_define = NULL;
2711 sym_free(top);
2712 top = top1;
2714 define_stack = b;
2717 /* label lookup */
2718 static Sym *label_find(int v)
2720 v -= TOK_IDENT;
2721 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2722 return NULL;
2723 return table_ident[v]->sym_label;
2726 static Sym *label_push(Sym **ptop, int v, int flags)
2728 Sym *s, **ps;
2729 s = sym_push2(ptop, v, 0, 0);
2730 s->r = flags;
2731 ps = &table_ident[v - TOK_IDENT]->sym_label;
2732 if (ptop == &global_label_stack) {
2733 /* modify the top most local identifier, so that
2734 sym_identifier will point to 's' when popped */
2735 while (*ps != NULL)
2736 ps = &(*ps)->prev_tok;
2738 s->prev_tok = *ps;
2739 *ps = s;
2740 return s;
2743 /* pop labels until element last is reached. Look if any labels are
2744 undefined. Define symbols if '&&label' was used. */
2745 static void label_pop(Sym **ptop, Sym *slast)
2747 Sym *s, *s1;
2748 for(s = *ptop; s != slast; s = s1) {
2749 s1 = s->prev;
2750 if (s->r == LABEL_DECLARED) {
2751 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2752 } else if (s->r == LABEL_FORWARD) {
2753 error("label '%s' used but not defined",
2754 get_tok_str(s->v, NULL));
2755 } else {
2756 if (s->c) {
2757 /* define corresponding symbol. A size of
2758 1 is put. */
2759 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2762 /* remove label */
2763 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2764 sym_free(s);
2766 *ptop = slast;
2769 /* eval an expression for #if/#elif */
2770 static int expr_preprocess(void)
2772 int c, t;
2773 TokenString str;
2775 tok_str_new(&str);
2776 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2777 next(); /* do macro subst */
2778 if (tok == TOK_DEFINED) {
2779 next_nomacro();
2780 t = tok;
2781 if (t == '(')
2782 next_nomacro();
2783 c = define_find(tok) != 0;
2784 if (t == '(')
2785 next_nomacro();
2786 tok = TOK_CINT;
2787 tokc.i = c;
2788 } else if (tok >= TOK_IDENT) {
2789 /* if undefined macro */
2790 tok = TOK_CINT;
2791 tokc.i = 0;
2793 tok_str_add_tok(&str);
2795 tok_str_add(&str, -1); /* simulate end of file */
2796 tok_str_add(&str, 0);
2797 /* now evaluate C constant expression */
2798 macro_ptr = str.str;
2799 next();
2800 c = expr_const();
2801 macro_ptr = NULL;
2802 tok_str_free(str.str);
2803 return c != 0;
2806 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2807 static void tok_print(int *str)
2809 int t;
2810 CValue cval;
2812 while (1) {
2813 TOK_GET(t, str, cval);
2814 if (!t)
2815 break;
2816 printf(" %s", get_tok_str(t, &cval));
2818 printf("\n");
2820 #endif
2822 /* parse after #define */
2823 static void parse_define(void)
2825 Sym *s, *first, **ps;
2826 int v, t, varg, is_vaargs, c;
2827 TokenString str;
2829 v = tok;
2830 if (v < TOK_IDENT)
2831 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2832 /* XXX: should check if same macro (ANSI) */
2833 first = NULL;
2834 t = MACRO_OBJ;
2835 /* '(' must be just after macro definition for MACRO_FUNC */
2836 c = file->buf_ptr[0];
2837 if (c == '\\')
2838 c = handle_stray1(file->buf_ptr);
2839 if (c == '(') {
2840 next_nomacro();
2841 next_nomacro();
2842 ps = &first;
2843 while (tok != ')') {
2844 varg = tok;
2845 next_nomacro();
2846 is_vaargs = 0;
2847 if (varg == TOK_DOTS) {
2848 varg = TOK___VA_ARGS__;
2849 is_vaargs = 1;
2850 } else if (tok == TOK_DOTS && gnu_ext) {
2851 is_vaargs = 1;
2852 next_nomacro();
2854 if (varg < TOK_IDENT)
2855 error("badly punctuated parameter list");
2856 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2857 *ps = s;
2858 ps = &s->next;
2859 if (tok != ',')
2860 break;
2861 next_nomacro();
2863 t = MACRO_FUNC;
2865 tok_str_new(&str);
2866 next_nomacro();
2867 /* EOF testing necessary for '-D' handling */
2868 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2869 tok_str_add2(&str, tok, &tokc);
2870 next_nomacro();
2872 tok_str_add(&str, 0);
2873 #ifdef PP_DEBUG
2874 printf("define %s %d: ", get_tok_str(v, NULL), t);
2875 tok_print(str.str);
2876 #endif
2877 define_push(v, t, str.str, first);
2880 static inline int hash_cached_include(int type, const char *filename)
2882 const unsigned char *s;
2883 unsigned int h;
2885 h = TOK_HASH_INIT;
2886 h = TOK_HASH_FUNC(h, type);
2887 s = filename;
2888 while (*s) {
2889 h = TOK_HASH_FUNC(h, *s);
2890 s++;
2892 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2893 return h;
2896 /* XXX: use a token or a hash table to accelerate matching ? */
2897 static CachedInclude *search_cached_include(TCCState *s1,
2898 int type, const char *filename)
2900 CachedInclude *e;
2901 int i, h;
2902 h = hash_cached_include(type, filename);
2903 i = s1->cached_includes_hash[h];
2904 for(;;) {
2905 if (i == 0)
2906 break;
2907 e = s1->cached_includes[i - 1];
2908 if (e->type == type && !strcmp(e->filename, filename))
2909 return e;
2910 i = e->hash_next;
2912 return NULL;
2915 static inline void add_cached_include(TCCState *s1, int type,
2916 const char *filename, int ifndef_macro)
2918 CachedInclude *e;
2919 int h;
2921 if (search_cached_include(s1, type, filename))
2922 return;
2923 #ifdef INC_DEBUG
2924 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2925 #endif
2926 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2927 if (!e)
2928 return;
2929 e->type = type;
2930 strcpy(e->filename, filename);
2931 e->ifndef_macro = ifndef_macro;
2932 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2933 /* add in hash table */
2934 h = hash_cached_include(type, filename);
2935 e->hash_next = s1->cached_includes_hash[h];
2936 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2939 static void pragma_parse(TCCState *s1)
2941 int val;
2943 next();
2944 if (tok == TOK_pack) {
2946 This may be:
2947 #pragma pack(1) // set
2948 #pragma pack() // reset to default
2949 #pragma pack(push,1) // push & set
2950 #pragma pack(pop) // restore previous
2952 next();
2953 skip('(');
2954 if (tok == TOK_ASM_pop) {
2955 next();
2956 if (s1->pack_stack_ptr <= s1->pack_stack) {
2957 stk_error:
2958 error("out of pack stack");
2960 s1->pack_stack_ptr--;
2961 } else {
2962 val = 0;
2963 if (tok != ')') {
2964 if (tok == TOK_ASM_push) {
2965 next();
2966 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2967 goto stk_error;
2968 s1->pack_stack_ptr++;
2969 skip(',');
2971 if (tok != TOK_CINT) {
2972 pack_error:
2973 error("invalid pack pragma");
2975 val = tokc.i;
2976 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2977 goto pack_error;
2978 next();
2980 *s1->pack_stack_ptr = val;
2981 skip(')');
2986 /* is_bof is true if first non space token at beginning of file */
2987 static void preprocess(int is_bof)
2989 TCCState *s1 = tcc_state;
2990 int size, i, c, n, saved_parse_flags;
2991 char buf[1024], *q;
2992 char buf1[1024];
2993 BufferedFile *f;
2994 Sym *s;
2995 CachedInclude *e;
2997 saved_parse_flags = parse_flags;
2998 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2999 PARSE_FLAG_LINEFEED;
3000 next_nomacro();
3001 redo:
3002 switch(tok) {
3003 case TOK_DEFINE:
3004 next_nomacro();
3005 parse_define();
3006 break;
3007 case TOK_UNDEF:
3008 next_nomacro();
3009 s = define_find(tok);
3010 /* undefine symbol by putting an invalid name */
3011 if (s)
3012 define_undef(s);
3013 break;
3014 case TOK_INCLUDE:
3015 case TOK_INCLUDE_NEXT:
3016 ch = file->buf_ptr[0];
3017 /* XXX: incorrect if comments : use next_nomacro with a special mode */
3018 skip_spaces();
3019 if (ch == '<') {
3020 c = '>';
3021 goto read_name;
3022 } else if (ch == '\"') {
3023 c = ch;
3024 read_name:
3025 inp();
3026 q = buf;
3027 while (ch != c && ch != '\n' && ch != CH_EOF) {
3028 if ((q - buf) < sizeof(buf) - 1)
3029 *q++ = ch;
3030 if (ch == '\\') {
3031 if (handle_stray_noerror() == 0)
3032 --q;
3033 } else
3034 inp();
3036 *q = '\0';
3037 minp();
3038 #if 0
3039 /* eat all spaces and comments after include */
3040 /* XXX: slightly incorrect */
3041 while (ch1 != '\n' && ch1 != CH_EOF)
3042 inp();
3043 #endif
3044 } else {
3045 /* computed #include : either we have only strings or
3046 we have anything enclosed in '<>' */
3047 next();
3048 buf[0] = '\0';
3049 if (tok == TOK_STR) {
3050 while (tok != TOK_LINEFEED) {
3051 if (tok != TOK_STR) {
3052 include_syntax:
3053 error("'#include' expects \"FILENAME\" or <FILENAME>");
3055 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3056 next();
3058 c = '\"';
3059 } else {
3060 int len;
3061 while (tok != TOK_LINEFEED) {
3062 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3063 next();
3065 len = strlen(buf);
3066 /* check syntax and remove '<>' */
3067 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3068 goto include_syntax;
3069 memmove(buf, buf + 1, len - 2);
3070 buf[len - 2] = '\0';
3071 c = '>';
3075 e = search_cached_include(s1, c, buf);
3076 if (e && define_find(e->ifndef_macro)) {
3077 /* no need to parse the include because the 'ifndef macro'
3078 is defined */
3079 #ifdef INC_DEBUG
3080 printf("%s: skipping %s\n", file->filename, buf);
3081 #endif
3082 } else {
3083 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3084 error("#include recursion too deep");
3085 /* push current file in stack */
3086 /* XXX: fix current line init */
3087 *s1->include_stack_ptr++ = file;
3088 if (c == '\"') {
3089 /* first search in current dir if "header.h" */
3090 size = tcc_basename(file->filename) - file->filename;
3091 if (size > sizeof(buf1) - 1)
3092 size = sizeof(buf1) - 1;
3093 memcpy(buf1, file->filename, size);
3094 buf1[size] = '\0';
3095 pstrcat(buf1, sizeof(buf1), buf);
3096 f = tcc_open(s1, buf1);
3097 if (f) {
3098 if (tok == TOK_INCLUDE_NEXT)
3099 tok = TOK_INCLUDE;
3100 else
3101 goto found;
3104 /* now search in all the include paths */
3105 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3106 for(i = 0; i < n; i++) {
3107 const char *path;
3108 if (i < s1->nb_include_paths)
3109 path = s1->include_paths[i];
3110 else
3111 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3112 pstrcpy(buf1, sizeof(buf1), path);
3113 pstrcat(buf1, sizeof(buf1), "/");
3114 pstrcat(buf1, sizeof(buf1), buf);
3115 f = tcc_open(s1, buf1);
3116 if (f) {
3117 if (tok == TOK_INCLUDE_NEXT)
3118 tok = TOK_INCLUDE;
3119 else
3120 goto found;
3123 --s1->include_stack_ptr;
3124 error("include file '%s' not found", buf);
3125 break;
3126 found:
3127 #ifdef INC_DEBUG
3128 printf("%s: including %s\n", file->filename, buf1);
3129 #endif
3130 f->inc_type = c;
3131 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3132 file = f;
3133 /* add include file debug info */
3134 if (do_debug) {
3135 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3137 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3138 ch = file->buf_ptr[0];
3139 goto the_end;
3141 break;
3142 case TOK_IFNDEF:
3143 c = 1;
3144 goto do_ifdef;
3145 case TOK_IF:
3146 c = expr_preprocess();
3147 goto do_if;
3148 case TOK_IFDEF:
3149 c = 0;
3150 do_ifdef:
3151 next_nomacro();
3152 if (tok < TOK_IDENT)
3153 error("invalid argument for '#if%sdef'", c ? "n" : "");
3154 if (is_bof) {
3155 if (c) {
3156 #ifdef INC_DEBUG
3157 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3158 #endif
3159 file->ifndef_macro = tok;
3162 c = (define_find(tok) != 0) ^ c;
3163 do_if:
3164 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3165 error("memory full");
3166 *s1->ifdef_stack_ptr++ = c;
3167 goto test_skip;
3168 case TOK_ELSE:
3169 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3170 error("#else without matching #if");
3171 if (s1->ifdef_stack_ptr[-1] & 2)
3172 error("#else after #else");
3173 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3174 goto test_skip;
3175 case TOK_ELIF:
3176 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3177 error("#elif without matching #if");
3178 c = s1->ifdef_stack_ptr[-1];
3179 if (c > 1)
3180 error("#elif after #else");
3181 /* last #if/#elif expression was true: we skip */
3182 if (c == 1)
3183 goto skip;
3184 c = expr_preprocess();
3185 s1->ifdef_stack_ptr[-1] = c;
3186 test_skip:
3187 if (!(c & 1)) {
3188 skip:
3189 preprocess_skip();
3190 is_bof = 0;
3191 goto redo;
3193 break;
3194 case TOK_ENDIF:
3195 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3196 error("#endif without matching #if");
3197 s1->ifdef_stack_ptr--;
3198 /* '#ifndef macro' was at the start of file. Now we check if
3199 an '#endif' is exactly at the end of file */
3200 if (file->ifndef_macro &&
3201 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3202 file->ifndef_macro_saved = file->ifndef_macro;
3203 /* need to set to zero to avoid false matches if another
3204 #ifndef at middle of file */
3205 file->ifndef_macro = 0;
3206 while (tok != TOK_LINEFEED)
3207 next_nomacro();
3208 tok_flags |= TOK_FLAG_ENDIF;
3209 goto the_end;
3211 break;
3212 case TOK_LINE:
3213 next();
3214 if (tok != TOK_CINT)
3215 error("#line");
3216 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3217 next();
3218 if (tok != TOK_LINEFEED) {
3219 if (tok != TOK_STR)
3220 error("#line");
3221 pstrcpy(file->filename, sizeof(file->filename),
3222 (char *)tokc.cstr->data);
3224 break;
3225 case TOK_ERROR:
3226 case TOK_WARNING:
3227 c = tok;
3228 ch = file->buf_ptr[0];
3229 skip_spaces();
3230 q = buf;
3231 while (ch != '\n' && ch != CH_EOF) {
3232 if ((q - buf) < sizeof(buf) - 1)
3233 *q++ = ch;
3234 if (ch == '\\') {
3235 if (handle_stray_noerror() == 0)
3236 --q;
3237 } else
3238 inp();
3240 *q = '\0';
3241 if (c == TOK_ERROR)
3242 error("#error %s", buf);
3243 else
3244 warning("#warning %s", buf);
3245 break;
3246 case TOK_PRAGMA:
3247 pragma_parse(s1);
3248 break;
3249 default:
3250 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3251 /* '!' is ignored to allow C scripts. numbers are ignored
3252 to emulate cpp behaviour */
3253 } else {
3254 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3255 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3257 break;
3259 /* ignore other preprocess commands or #! for C scripts */
3260 while (tok != TOK_LINEFEED)
3261 next_nomacro();
3262 the_end:
3263 parse_flags = saved_parse_flags;
3266 /* evaluate escape codes in a string. */
3267 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3269 int c, n;
3270 const uint8_t *p;
3272 p = buf;
3273 for(;;) {
3274 c = *p;
3275 if (c == '\0')
3276 break;
3277 if (c == '\\') {
3278 p++;
3279 /* escape */
3280 c = *p;
3281 switch(c) {
3282 case '0': case '1': case '2': case '3':
3283 case '4': case '5': case '6': case '7':
3284 /* at most three octal digits */
3285 n = c - '0';
3286 p++;
3287 c = *p;
3288 if (isoct(c)) {
3289 n = n * 8 + c - '0';
3290 p++;
3291 c = *p;
3292 if (isoct(c)) {
3293 n = n * 8 + c - '0';
3294 p++;
3297 c = n;
3298 goto add_char_nonext;
3299 case 'x':
3300 case 'u':
3301 case 'U':
3302 p++;
3303 n = 0;
3304 for(;;) {
3305 c = *p;
3306 if (c >= 'a' && c <= 'f')
3307 c = c - 'a' + 10;
3308 else if (c >= 'A' && c <= 'F')
3309 c = c - 'A' + 10;
3310 else if (isnum(c))
3311 c = c - '0';
3312 else
3313 break;
3314 n = n * 16 + c;
3315 p++;
3317 c = n;
3318 goto add_char_nonext;
3319 case 'a':
3320 c = '\a';
3321 break;
3322 case 'b':
3323 c = '\b';
3324 break;
3325 case 'f':
3326 c = '\f';
3327 break;
3328 case 'n':
3329 c = '\n';
3330 break;
3331 case 'r':
3332 c = '\r';
3333 break;
3334 case 't':
3335 c = '\t';
3336 break;
3337 case 'v':
3338 c = '\v';
3339 break;
3340 case 'e':
3341 if (!gnu_ext)
3342 goto invalid_escape;
3343 c = 27;
3344 break;
3345 case '\'':
3346 case '\"':
3347 case '\\':
3348 case '?':
3349 break;
3350 default:
3351 invalid_escape:
3352 if (c >= '!' && c <= '~')
3353 warning("unknown escape sequence: \'\\%c\'", c);
3354 else
3355 warning("unknown escape sequence: \'\\x%x\'", c);
3356 break;
3359 p++;
3360 add_char_nonext:
3361 if (!is_long)
3362 cstr_ccat(outstr, c);
3363 else
3364 cstr_wccat(outstr, c);
3366 /* add a trailing '\0' */
3367 if (!is_long)
3368 cstr_ccat(outstr, '\0');
3369 else
3370 cstr_wccat(outstr, '\0');
3373 /* we use 64 bit numbers */
3374 #define BN_SIZE 2
3376 /* bn = (bn << shift) | or_val */
3377 void bn_lshift(unsigned int *bn, int shift, int or_val)
3379 int i;
3380 unsigned int v;
3381 for(i=0;i<BN_SIZE;i++) {
3382 v = bn[i];
3383 bn[i] = (v << shift) | or_val;
3384 or_val = v >> (32 - shift);
3388 void bn_zero(unsigned int *bn)
3390 int i;
3391 for(i=0;i<BN_SIZE;i++) {
3392 bn[i] = 0;
3396 /* parse number in null terminated string 'p' and return it in the
3397 current token */
3398 void parse_number(const char *p)
3400 int b, t, shift, frac_bits, s, exp_val, ch;
3401 char *q;
3402 unsigned int bn[BN_SIZE];
3403 double d;
3405 /* number */
3406 q = token_buf;
3407 ch = *p++;
3408 t = ch;
3409 ch = *p++;
3410 *q++ = t;
3411 b = 10;
3412 if (t == '.') {
3413 goto float_frac_parse;
3414 } else if (t == '0') {
3415 if (ch == 'x' || ch == 'X') {
3416 q--;
3417 ch = *p++;
3418 b = 16;
3419 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3420 q--;
3421 ch = *p++;
3422 b = 2;
3425 /* parse all digits. cannot check octal numbers at this stage
3426 because of floating point constants */
3427 while (1) {
3428 if (ch >= 'a' && ch <= 'f')
3429 t = ch - 'a' + 10;
3430 else if (ch >= 'A' && ch <= 'F')
3431 t = ch - 'A' + 10;
3432 else if (isnum(ch))
3433 t = ch - '0';
3434 else
3435 break;
3436 if (t >= b)
3437 break;
3438 if (q >= token_buf + STRING_MAX_SIZE) {
3439 num_too_long:
3440 error("number too long");
3442 *q++ = ch;
3443 ch = *p++;
3445 if (ch == '.' ||
3446 ((ch == 'e' || ch == 'E') && b == 10) ||
3447 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3448 if (b != 10) {
3449 /* NOTE: strtox should support that for hexa numbers, but
3450 non ISOC99 libcs do not support it, so we prefer to do
3451 it by hand */
3452 /* hexadecimal or binary floats */
3453 /* XXX: handle overflows */
3454 *q = '\0';
3455 if (b == 16)
3456 shift = 4;
3457 else
3458 shift = 2;
3459 bn_zero(bn);
3460 q = token_buf;
3461 while (1) {
3462 t = *q++;
3463 if (t == '\0') {
3464 break;
3465 } else if (t >= 'a') {
3466 t = t - 'a' + 10;
3467 } else if (t >= 'A') {
3468 t = t - 'A' + 10;
3469 } else {
3470 t = t - '0';
3472 bn_lshift(bn, shift, t);
3474 frac_bits = 0;
3475 if (ch == '.') {
3476 ch = *p++;
3477 while (1) {
3478 t = ch;
3479 if (t >= 'a' && t <= 'f') {
3480 t = t - 'a' + 10;
3481 } else if (t >= 'A' && t <= 'F') {
3482 t = t - 'A' + 10;
3483 } else if (t >= '0' && t <= '9') {
3484 t = t - '0';
3485 } else {
3486 break;
3488 if (t >= b)
3489 error("invalid digit");
3490 bn_lshift(bn, shift, t);
3491 frac_bits += shift;
3492 ch = *p++;
3495 if (ch != 'p' && ch != 'P')
3496 expect("exponent");
3497 ch = *p++;
3498 s = 1;
3499 exp_val = 0;
3500 if (ch == '+') {
3501 ch = *p++;
3502 } else if (ch == '-') {
3503 s = -1;
3504 ch = *p++;
3506 if (ch < '0' || ch > '9')
3507 expect("exponent digits");
3508 while (ch >= '0' && ch <= '9') {
3509 exp_val = exp_val * 10 + ch - '0';
3510 ch = *p++;
3512 exp_val = exp_val * s;
3514 /* now we can generate the number */
3515 /* XXX: should patch directly float number */
3516 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3517 d = ldexp(d, exp_val - frac_bits);
3518 t = toup(ch);
3519 if (t == 'F') {
3520 ch = *p++;
3521 tok = TOK_CFLOAT;
3522 /* float : should handle overflow */
3523 tokc.f = (float)d;
3524 } else if (t == 'L') {
3525 ch = *p++;
3526 tok = TOK_CLDOUBLE;
3527 /* XXX: not large enough */
3528 tokc.ld = (long double)d;
3529 } else {
3530 tok = TOK_CDOUBLE;
3531 tokc.d = d;
3533 } else {
3534 /* decimal floats */
3535 if (ch == '.') {
3536 if (q >= token_buf + STRING_MAX_SIZE)
3537 goto num_too_long;
3538 *q++ = ch;
3539 ch = *p++;
3540 float_frac_parse:
3541 while (ch >= '0' && ch <= '9') {
3542 if (q >= token_buf + STRING_MAX_SIZE)
3543 goto num_too_long;
3544 *q++ = ch;
3545 ch = *p++;
3548 if (ch == 'e' || ch == 'E') {
3549 if (q >= token_buf + STRING_MAX_SIZE)
3550 goto num_too_long;
3551 *q++ = ch;
3552 ch = *p++;
3553 if (ch == '-' || ch == '+') {
3554 if (q >= token_buf + STRING_MAX_SIZE)
3555 goto num_too_long;
3556 *q++ = ch;
3557 ch = *p++;
3559 if (ch < '0' || ch > '9')
3560 expect("exponent digits");
3561 while (ch >= '0' && ch <= '9') {
3562 if (q >= token_buf + STRING_MAX_SIZE)
3563 goto num_too_long;
3564 *q++ = ch;
3565 ch = *p++;
3568 *q = '\0';
3569 t = toup(ch);
3570 errno = 0;
3571 if (t == 'F') {
3572 ch = *p++;
3573 tok = TOK_CFLOAT;
3574 tokc.f = strtof(token_buf, NULL);
3575 } else if (t == 'L') {
3576 ch = *p++;
3577 tok = TOK_CLDOUBLE;
3578 tokc.ld = strtold(token_buf, NULL);
3579 } else {
3580 tok = TOK_CDOUBLE;
3581 tokc.d = strtod(token_buf, NULL);
3584 } else {
3585 unsigned long long n, n1;
3586 int lcount, ucount;
3588 /* integer number */
3589 *q = '\0';
3590 q = token_buf;
3591 if (b == 10 && *q == '0') {
3592 b = 8;
3593 q++;
3595 n = 0;
3596 while(1) {
3597 t = *q++;
3598 /* no need for checks except for base 10 / 8 errors */
3599 if (t == '\0') {
3600 break;
3601 } else if (t >= 'a') {
3602 t = t - 'a' + 10;
3603 } else if (t >= 'A') {
3604 t = t - 'A' + 10;
3605 } else {
3606 t = t - '0';
3607 if (t >= b)
3608 error("invalid digit");
3610 n1 = n;
3611 n = n * b + t;
3612 /* detect overflow */
3613 /* XXX: this test is not reliable */
3614 if (n < n1)
3615 error("integer constant overflow");
3618 /* XXX: not exactly ANSI compliant */
3619 if ((n & 0xffffffff00000000LL) != 0) {
3620 if ((n >> 63) != 0)
3621 tok = TOK_CULLONG;
3622 else
3623 tok = TOK_CLLONG;
3624 } else if (n > 0x7fffffff) {
3625 tok = TOK_CUINT;
3626 } else {
3627 tok = TOK_CINT;
3629 lcount = 0;
3630 ucount = 0;
3631 for(;;) {
3632 t = toup(ch);
3633 if (t == 'L') {
3634 if (lcount >= 2)
3635 error("three 'l's in integer constant");
3636 lcount++;
3637 if (lcount == 2) {
3638 if (tok == TOK_CINT)
3639 tok = TOK_CLLONG;
3640 else if (tok == TOK_CUINT)
3641 tok = TOK_CULLONG;
3643 ch = *p++;
3644 } else if (t == 'U') {
3645 if (ucount >= 1)
3646 error("two 'u's in integer constant");
3647 ucount++;
3648 if (tok == TOK_CINT)
3649 tok = TOK_CUINT;
3650 else if (tok == TOK_CLLONG)
3651 tok = TOK_CULLONG;
3652 ch = *p++;
3653 } else {
3654 break;
3657 if (tok == TOK_CINT || tok == TOK_CUINT)
3658 tokc.ui = n;
3659 else
3660 tokc.ull = n;
3665 #define PARSE2(c1, tok1, c2, tok2) \
3666 case c1: \
3667 PEEKC(c, p); \
3668 if (c == c2) { \
3669 p++; \
3670 tok = tok2; \
3671 } else { \
3672 tok = tok1; \
3674 break;
3676 /* return next token without macro substitution */
3677 static inline void next_nomacro1(void)
3679 int t, c, is_long;
3680 TokenSym *ts;
3681 uint8_t *p, *p1;
3682 unsigned int h;
3684 cstr_reset(&tok_spaces);
3685 p = file->buf_ptr;
3686 redo_no_start:
3687 c = *p;
3688 switch(c) {
3689 case ' ':
3690 case '\t':
3691 case '\f':
3692 case '\v':
3693 case '\r':
3694 cstr_ccat(&tok_spaces, c);
3695 p++;
3696 goto redo_no_start;
3698 case '\\':
3699 /* first look if it is in fact an end of buffer */
3700 if (p >= file->buf_end) {
3701 file->buf_ptr = p;
3702 handle_eob();
3703 p = file->buf_ptr;
3704 if (p >= file->buf_end)
3705 goto parse_eof;
3706 else
3707 goto redo_no_start;
3708 } else {
3709 file->buf_ptr = p;
3710 ch = *p;
3711 handle_stray();
3712 p = file->buf_ptr;
3713 goto redo_no_start;
3715 parse_eof:
3717 TCCState *s1 = tcc_state;
3718 if ((parse_flags & PARSE_FLAG_LINEFEED)
3719 && !(tok_flags & TOK_FLAG_EOF)) {
3720 tok_flags |= TOK_FLAG_EOF;
3721 tok = TOK_LINEFEED;
3722 goto keep_tok_flags;
3723 } else if (s1->include_stack_ptr == s1->include_stack ||
3724 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3725 /* no include left : end of file. */
3726 tok = TOK_EOF;
3727 } else {
3728 tok_flags &= ~TOK_FLAG_EOF;
3729 /* pop include file */
3731 /* test if previous '#endif' was after a #ifdef at
3732 start of file */
3733 if (tok_flags & TOK_FLAG_ENDIF) {
3734 #ifdef INC_DEBUG
3735 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3736 #endif
3737 add_cached_include(s1, file->inc_type, file->inc_filename,
3738 file->ifndef_macro_saved);
3741 /* add end of include file debug info */
3742 if (do_debug) {
3743 put_stabd(N_EINCL, 0, 0);
3745 /* pop include stack */
3746 tcc_close(file);
3747 s1->include_stack_ptr--;
3748 file = *s1->include_stack_ptr;
3749 p = file->buf_ptr;
3750 goto redo_no_start;
3753 break;
3755 case '\n':
3756 file->line_num++;
3757 tok_flags |= TOK_FLAG_BOL;
3758 p++;
3759 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3760 goto redo_no_start;
3761 tok = TOK_LINEFEED;
3762 goto keep_tok_flags;
3764 case '#':
3765 /* XXX: simplify */
3766 PEEKC(c, p);
3767 if ((tok_flags & TOK_FLAG_BOL) &&
3768 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3769 file->buf_ptr = p;
3770 preprocess(tok_flags & TOK_FLAG_BOF);
3771 p = file->buf_ptr;
3772 goto redo_no_start;
3773 } else {
3774 if (c == '#') {
3775 p++;
3776 tok = TOK_TWOSHARPS;
3777 } else {
3778 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3779 p = parse_line_comment(p - 1);
3780 goto redo_no_start;
3781 } else {
3782 tok = '#';
3786 break;
3788 case 'a': case 'b': case 'c': case 'd':
3789 case 'e': case 'f': case 'g': case 'h':
3790 case 'i': case 'j': case 'k': case 'l':
3791 case 'm': case 'n': case 'o': case 'p':
3792 case 'q': case 'r': case 's': case 't':
3793 case 'u': case 'v': case 'w': case 'x':
3794 case 'y': case 'z':
3795 case 'A': case 'B': case 'C': case 'D':
3796 case 'E': case 'F': case 'G': case 'H':
3797 case 'I': case 'J': case 'K':
3798 case 'M': case 'N': case 'O': case 'P':
3799 case 'Q': case 'R': case 'S': case 'T':
3800 case 'U': case 'V': case 'W': case 'X':
3801 case 'Y': case 'Z':
3802 case '_':
3803 parse_ident_fast:
3804 p1 = p;
3805 h = TOK_HASH_INIT;
3806 h = TOK_HASH_FUNC(h, c);
3807 p++;
3808 for(;;) {
3809 c = *p;
3810 if (!isidnum_table[c-CH_EOF])
3811 break;
3812 h = TOK_HASH_FUNC(h, c);
3813 p++;
3815 if (c != '\\') {
3816 TokenSym **pts;
3817 int len;
3819 /* fast case : no stray found, so we have the full token
3820 and we have already hashed it */
3821 len = p - p1;
3822 h &= (TOK_HASH_SIZE - 1);
3823 pts = &hash_ident[h];
3824 for(;;) {
3825 ts = *pts;
3826 if (!ts)
3827 break;
3828 if (ts->len == len && !memcmp(ts->str, p1, len))
3829 goto token_found;
3830 pts = &(ts->hash_next);
3832 ts = tok_alloc_new(pts, p1, len);
3833 token_found: ;
3834 } else {
3835 /* slower case */
3836 cstr_reset(&tokcstr);
3838 while (p1 < p) {
3839 cstr_ccat(&tokcstr, *p1);
3840 p1++;
3842 p--;
3843 PEEKC(c, p);
3844 parse_ident_slow:
3845 while (isidnum_table[c-CH_EOF]) {
3846 cstr_ccat(&tokcstr, c);
3847 PEEKC(c, p);
3849 ts = tok_alloc(tokcstr.data, tokcstr.size);
3851 tok = ts->tok;
3852 break;
3853 case 'L':
3854 t = p[1];
3855 if (t != '\\' && t != '\'' && t != '\"') {
3856 /* fast case */
3857 goto parse_ident_fast;
3858 } else {
3859 PEEKC(c, p);
3860 if (c == '\'' || c == '\"') {
3861 is_long = 1;
3862 goto str_const;
3863 } else {
3864 cstr_reset(&tokcstr);
3865 cstr_ccat(&tokcstr, 'L');
3866 goto parse_ident_slow;
3869 break;
3870 case '0': case '1': case '2': case '3':
3871 case '4': case '5': case '6': case '7':
3872 case '8': case '9':
3874 cstr_reset(&tokcstr);
3875 /* after the first digit, accept digits, alpha, '.' or sign if
3876 prefixed by 'eEpP' */
3877 parse_num:
3878 for(;;) {
3879 t = c;
3880 cstr_ccat(&tokcstr, c);
3881 PEEKC(c, p);
3882 if (!(isnum(c) || isid(c) || c == '.' ||
3883 ((c == '+' || c == '-') &&
3884 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3885 break;
3887 /* We add a trailing '\0' to ease parsing */
3888 cstr_ccat(&tokcstr, '\0');
3889 tokc.cstr = &tokcstr;
3890 tok = TOK_PPNUM;
3891 break;
3892 case '.':
3893 /* special dot handling because it can also start a number */
3894 PEEKC(c, p);
3895 if (isnum(c)) {
3896 cstr_reset(&tokcstr);
3897 cstr_ccat(&tokcstr, '.');
3898 goto parse_num;
3899 } else if (c == '.') {
3900 PEEKC(c, p);
3901 if (c != '.')
3902 expect("'.'");
3903 PEEKC(c, p);
3904 tok = TOK_DOTS;
3905 } else {
3906 tok = '.';
3908 break;
3909 case '\'':
3910 case '\"':
3911 is_long = 0;
3912 str_const:
3914 CString str;
3915 int sep;
3917 sep = c;
3919 /* parse the string */
3920 cstr_new(&str);
3921 p = parse_pp_string(p, sep, &str);
3922 cstr_ccat(&str, '\0');
3924 /* eval the escape (should be done as TOK_PPNUM) */
3925 cstr_reset(&tokcstr);
3926 parse_escape_string(&tokcstr, str.data, is_long);
3927 cstr_free(&str);
3929 if (sep == '\'') {
3930 int char_size;
3931 /* XXX: make it portable */
3932 if (!is_long)
3933 char_size = 1;
3934 else
3935 char_size = sizeof(nwchar_t);
3936 if (tokcstr.size <= char_size)
3937 error("empty character constant");
3938 if (tokcstr.size > 2 * char_size)
3939 warning("multi-character character constant");
3940 if (!is_long) {
3941 tokc.i = *(int8_t *)tokcstr.data;
3942 tok = TOK_CCHAR;
3943 } else {
3944 tokc.i = *(nwchar_t *)tokcstr.data;
3945 tok = TOK_LCHAR;
3947 } else {
3948 tokc.cstr = &tokcstr;
3949 if (!is_long)
3950 tok = TOK_STR;
3951 else
3952 tok = TOK_LSTR;
3955 break;
3957 case '<':
3958 PEEKC(c, p);
3959 if (c == '=') {
3960 p++;
3961 tok = TOK_LE;
3962 } else if (c == '<') {
3963 PEEKC(c, p);
3964 if (c == '=') {
3965 p++;
3966 tok = TOK_A_SHL;
3967 } else {
3968 tok = TOK_SHL;
3970 } else {
3971 tok = TOK_LT;
3973 break;
3975 case '>':
3976 PEEKC(c, p);
3977 if (c == '=') {
3978 p++;
3979 tok = TOK_GE;
3980 } else if (c == '>') {
3981 PEEKC(c, p);
3982 if (c == '=') {
3983 p++;
3984 tok = TOK_A_SAR;
3985 } else {
3986 tok = TOK_SAR;
3988 } else {
3989 tok = TOK_GT;
3991 break;
3993 case '&':
3994 PEEKC(c, p);
3995 if (c == '&') {
3996 p++;
3997 tok = TOK_LAND;
3998 } else if (c == '=') {
3999 p++;
4000 tok = TOK_A_AND;
4001 } else {
4002 tok = '&';
4004 break;
4006 case '|':
4007 PEEKC(c, p);
4008 if (c == '|') {
4009 p++;
4010 tok = TOK_LOR;
4011 } else if (c == '=') {
4012 p++;
4013 tok = TOK_A_OR;
4014 } else {
4015 tok = '|';
4017 break;
4019 case '+':
4020 PEEKC(c, p);
4021 if (c == '+') {
4022 p++;
4023 tok = TOK_INC;
4024 } else if (c == '=') {
4025 p++;
4026 tok = TOK_A_ADD;
4027 } else {
4028 tok = '+';
4030 break;
4032 case '-':
4033 PEEKC(c, p);
4034 if (c == '-') {
4035 p++;
4036 tok = TOK_DEC;
4037 } else if (c == '=') {
4038 p++;
4039 tok = TOK_A_SUB;
4040 } else if (c == '>') {
4041 p++;
4042 tok = TOK_ARROW;
4043 } else {
4044 tok = '-';
4046 break;
4048 PARSE2('!', '!', '=', TOK_NE)
4049 PARSE2('=', '=', '=', TOK_EQ)
4050 PARSE2('*', '*', '=', TOK_A_MUL)
4051 PARSE2('%', '%', '=', TOK_A_MOD)
4052 PARSE2('^', '^', '=', TOK_A_XOR)
4054 /* comments or operator */
4055 case '/':
4056 PEEKC(c, p);
4057 if (c == '*') {
4058 p = parse_comment(p);
4059 goto redo_no_start;
4060 } else if (c == '/') {
4061 p = parse_line_comment(p);
4062 goto redo_no_start;
4063 } else if (c == '=') {
4064 p++;
4065 tok = TOK_A_DIV;
4066 } else {
4067 tok = '/';
4069 break;
4071 /* simple tokens */
4072 case '(':
4073 case ')':
4074 case '[':
4075 case ']':
4076 case '{':
4077 case '}':
4078 case ',':
4079 case ';':
4080 case ':':
4081 case '?':
4082 case '~':
4083 case '$': /* only used in assembler */
4084 case '@': /* dito */
4085 tok = c;
4086 p++;
4087 break;
4088 default:
4089 error("unrecognized character \\x%02x", c);
4090 break;
4092 tok_flags = 0;
4093 keep_tok_flags:
4094 file->buf_ptr = p;
4095 #if defined(PARSE_DEBUG)
4096 printf("token = %s\n", get_tok_str(tok, &tokc));
4097 #endif
4100 /* return next token without macro substitution. Can read input from
4101 macro_ptr buffer */
4102 static void next_nomacro(void)
4104 if (macro_ptr) {
4105 redo:
4106 tok = *macro_ptr;
4107 if (tok) {
4108 TOK_GET(tok, macro_ptr, tokc);
4109 if (tok == TOK_LINENUM) {
4110 file->line_num = tokc.i;
4111 goto redo;
4114 } else {
4115 next_nomacro1();
4119 /* substitute args in macro_str and return allocated string */
4120 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4122 int *st, last_tok, t, notfirst;
4123 Sym *s;
4124 CValue cval;
4125 TokenString str;
4126 CString cstr;
4128 tok_str_new(&str);
4129 last_tok = 0;
4130 while(1) {
4131 TOK_GET(t, macro_str, cval);
4132 if (!t)
4133 break;
4134 if (t == '#') {
4135 /* stringize */
4136 TOK_GET(t, macro_str, cval);
4137 if (!t)
4138 break;
4139 s = sym_find2(args, t);
4140 if (s) {
4141 cstr_new(&cstr);
4142 st = (int *)s->c;
4143 notfirst = 0;
4144 while (*st) {
4145 if (notfirst)
4146 cstr_ccat(&cstr, ' ');
4147 TOK_GET(t, st, cval);
4148 cstr_cat(&cstr, get_tok_str(t, &cval));
4149 #ifndef PP_NOSPACES
4150 notfirst = 1;
4151 #endif
4153 cstr_ccat(&cstr, '\0');
4154 #ifdef PP_DEBUG
4155 printf("stringize: %s\n", (char *)cstr.data);
4156 #endif
4157 /* add string */
4158 cval.cstr = &cstr;
4159 tok_str_add2(&str, TOK_STR, &cval);
4160 cstr_free(&cstr);
4161 } else {
4162 tok_str_add2(&str, t, &cval);
4164 } else if (t >= TOK_IDENT) {
4165 s = sym_find2(args, t);
4166 if (s) {
4167 st = (int *)s->c;
4168 /* if '##' is present before or after, no arg substitution */
4169 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4170 /* special case for var arg macros : ## eats the
4171 ',' if empty VA_ARGS variable. */
4172 /* XXX: test of the ',' is not 100%
4173 reliable. should fix it to avoid security
4174 problems */
4175 if (gnu_ext && s->type.t &&
4176 last_tok == TOK_TWOSHARPS &&
4177 str.len >= 2 && str.str[str.len - 2] == ',') {
4178 if (*st == 0) {
4179 /* suppress ',' '##' */
4180 str.len -= 2;
4181 } else {
4182 /* suppress '##' and add variable */
4183 str.len--;
4184 goto add_var;
4186 } else {
4187 int t1;
4188 add_var:
4189 for(;;) {
4190 TOK_GET(t1, st, cval);
4191 if (!t1)
4192 break;
4193 tok_str_add2(&str, t1, &cval);
4196 } else {
4197 /* NOTE: the stream cannot be read when macro
4198 substituing an argument */
4199 macro_subst(&str, nested_list, st, NULL);
4201 } else {
4202 tok_str_add(&str, t);
4204 } else {
4205 tok_str_add2(&str, t, &cval);
4207 last_tok = t;
4209 tok_str_add(&str, 0);
4210 return str.str;
4213 static char const ab_month_name[12][4] =
4215 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4216 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4219 /* do macro substitution of current token with macro 's' and add
4220 result to (tok_str,tok_len). 'nested_list' is the list of all
4221 macros we got inside to avoid recursing. Return non zero if no
4222 substitution needs to be done */
4223 static int macro_subst_tok(TokenString *tok_str,
4224 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4226 Sym *args, *sa, *sa1;
4227 int mstr_allocated, parlevel, *mstr, t, t1;
4228 TokenString str;
4229 char *cstrval;
4230 CValue cval;
4231 CString cstr;
4232 char buf[32];
4234 /* if symbol is a macro, prepare substitution */
4235 /* special macros */
4236 if (tok == TOK___LINE__) {
4237 snprintf(buf, sizeof(buf), "%d", file->line_num);
4238 cstrval = buf;
4239 t1 = TOK_PPNUM;
4240 goto add_cstr1;
4241 } else if (tok == TOK___FILE__) {
4242 cstrval = file->filename;
4243 goto add_cstr;
4244 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4245 time_t ti;
4246 struct tm *tm;
4248 time(&ti);
4249 tm = localtime(&ti);
4250 if (tok == TOK___DATE__) {
4251 snprintf(buf, sizeof(buf), "%s %2d %d",
4252 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4253 } else {
4254 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4255 tm->tm_hour, tm->tm_min, tm->tm_sec);
4257 cstrval = buf;
4258 add_cstr:
4259 t1 = TOK_STR;
4260 add_cstr1:
4261 cstr_new(&cstr);
4262 cstr_cat(&cstr, cstrval);
4263 cstr_ccat(&cstr, '\0');
4264 cval.cstr = &cstr;
4265 tok_str_add2(tok_str, t1, &cval);
4266 cstr_free(&cstr);
4267 } else {
4268 mstr = (int *)s->c;
4269 mstr_allocated = 0;
4270 if (s->type.t == MACRO_FUNC) {
4271 /* NOTE: we do not use next_nomacro to avoid eating the
4272 next token. XXX: find better solution */
4273 redo:
4274 if (macro_ptr) {
4275 t = *macro_ptr;
4276 if (t == 0 && can_read_stream) {
4277 /* end of macro stream: we must look at the token
4278 after in the file */
4279 struct macro_level *ml = *can_read_stream;
4280 macro_ptr = NULL;
4281 if (ml)
4283 macro_ptr = ml->p;
4284 ml->p = NULL;
4285 *can_read_stream = ml -> prev;
4287 goto redo;
4289 } else {
4290 /* XXX: incorrect with comments */
4291 ch = file->buf_ptr[0];
4292 while (is_space(ch) || ch == '\n')
4293 cinp();
4294 t = ch;
4296 if (t != '(') /* no macro subst */
4297 return -1;
4299 /* argument macro */
4300 next_nomacro();
4301 next_nomacro();
4302 args = NULL;
4303 sa = s->next;
4304 /* NOTE: empty args are allowed, except if no args */
4305 for(;;) {
4306 /* handle '()' case */
4307 if (!args && !sa && tok == ')')
4308 break;
4309 if (!sa)
4310 error("macro '%s' used with too many args",
4311 get_tok_str(s->v, 0));
4312 tok_str_new(&str);
4313 parlevel = 0;
4314 /* NOTE: non zero sa->t indicates VA_ARGS */
4315 while ((parlevel > 0 ||
4316 (tok != ')' &&
4317 (tok != ',' || sa->type.t))) &&
4318 tok != -1) {
4319 if (tok == '(')
4320 parlevel++;
4321 else if (tok == ')')
4322 parlevel--;
4323 if (tok != TOK_LINEFEED)
4324 tok_str_add2(&str, tok, &tokc);
4325 next_nomacro();
4327 tok_str_add(&str, 0);
4328 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (long)str.str);
4329 sa = sa->next;
4330 if (tok == ')') {
4331 /* special case for gcc var args: add an empty
4332 var arg argument if it is omitted */
4333 if (sa && sa->type.t && gnu_ext)
4334 continue;
4335 else
4336 break;
4338 if (tok != ',')
4339 expect(",");
4340 next_nomacro();
4342 if (sa) {
4343 error("macro '%s' used with too few args",
4344 get_tok_str(s->v, 0));
4347 /* now subst each arg */
4348 mstr = macro_arg_subst(nested_list, mstr, args);
4349 /* free memory */
4350 sa = args;
4351 while (sa) {
4352 sa1 = sa->prev;
4353 tok_str_free((int *)sa->c);
4354 sym_free(sa);
4355 sa = sa1;
4357 mstr_allocated = 1;
4359 sym_push2(nested_list, s->v, 0, 0);
4360 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4361 /* pop nested defined symbol */
4362 sa1 = *nested_list;
4363 *nested_list = sa1->prev;
4364 sym_free(sa1);
4365 if (mstr_allocated)
4366 tok_str_free(mstr);
4368 return 0;
4371 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4372 return the resulting string (which must be freed). */
4373 static inline int *macro_twosharps(const int *macro_str)
4375 TokenSym *ts;
4376 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4377 int t;
4378 const char *p1, *p2;
4379 CValue cval;
4380 TokenString macro_str1;
4381 CString cstr;
4383 start_macro_ptr = macro_str;
4384 /* we search the first '##' */
4385 for(;;) {
4386 macro_ptr1 = macro_str;
4387 TOK_GET(t, macro_str, cval);
4388 /* nothing more to do if end of string */
4389 if (t == 0)
4390 return NULL;
4391 if (*macro_str == TOK_TWOSHARPS)
4392 break;
4395 /* we saw '##', so we need more processing to handle it */
4396 cstr_new(&cstr);
4397 tok_str_new(&macro_str1);
4398 tok = t;
4399 tokc = cval;
4401 /* add all tokens seen so far */
4402 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4403 TOK_GET(t, ptr, cval);
4404 tok_str_add2(&macro_str1, t, &cval);
4406 saved_macro_ptr = macro_ptr;
4407 /* XXX: get rid of the use of macro_ptr here */
4408 macro_ptr = (int *)macro_str;
4409 for(;;) {
4410 while (*macro_ptr == TOK_TWOSHARPS) {
4411 macro_ptr++;
4412 macro_ptr1 = macro_ptr;
4413 t = *macro_ptr;
4414 if (t) {
4415 TOK_GET(t, macro_ptr, cval);
4416 /* We concatenate the two tokens if we have an
4417 identifier or a preprocessing number */
4418 cstr_reset(&cstr);
4419 p1 = get_tok_str(tok, &tokc);
4420 cstr_cat(&cstr, p1);
4421 p2 = get_tok_str(t, &cval);
4422 cstr_cat(&cstr, p2);
4423 cstr_ccat(&cstr, '\0');
4425 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4426 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4427 if (tok == TOK_PPNUM) {
4428 /* if number, then create a number token */
4429 /* NOTE: no need to allocate because
4430 tok_str_add2() does it */
4431 cstr_reset(&tokcstr);
4432 tokcstr = cstr;
4433 cstr_new(&cstr);
4434 tokc.cstr = &tokcstr;
4435 } else {
4436 /* if identifier, we must do a test to
4437 validate we have a correct identifier */
4438 if (t == TOK_PPNUM) {
4439 const char *p;
4440 int c;
4442 p = p2;
4443 for(;;) {
4444 c = *p;
4445 if (c == '\0')
4446 break;
4447 p++;
4448 if (!isnum(c) && !isid(c))
4449 goto error_pasting;
4452 ts = tok_alloc(cstr.data, strlen(cstr.data));
4453 tok = ts->tok; /* modify current token */
4455 } else {
4456 const char *str = cstr.data;
4457 const unsigned char *q;
4459 /* we look for a valid token */
4460 /* XXX: do more extensive checks */
4461 if (!strcmp(str, ">>=")) {
4462 tok = TOK_A_SAR;
4463 } else if (!strcmp(str, "<<=")) {
4464 tok = TOK_A_SHL;
4465 } else if (strlen(str) == 2) {
4466 /* search in two bytes table */
4467 q = tok_two_chars;
4468 for(;;) {
4469 if (!*q)
4470 goto error_pasting;
4471 if (q[0] == str[0] && q[1] == str[1])
4472 break;
4473 q += 3;
4475 tok = q[2];
4476 } else {
4477 error_pasting:
4478 /* NOTE: because get_tok_str use a static buffer,
4479 we must save it */
4480 cstr_reset(&cstr);
4481 p1 = get_tok_str(tok, &tokc);
4482 cstr_cat(&cstr, p1);
4483 cstr_ccat(&cstr, '\0');
4484 p2 = get_tok_str(t, &cval);
4485 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4486 /* cannot merge tokens: just add them separately */
4487 tok_str_add2(&macro_str1, tok, &tokc);
4488 /* XXX: free associated memory ? */
4489 tok = t;
4490 tokc = cval;
4495 tok_str_add2(&macro_str1, tok, &tokc);
4496 next_nomacro();
4497 if (tok == 0)
4498 break;
4500 macro_ptr = (int *)saved_macro_ptr;
4501 cstr_free(&cstr);
4502 tok_str_add(&macro_str1, 0);
4503 return macro_str1.str;
4507 /* do macro substitution of macro_str and add result to
4508 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4509 inside to avoid recursing. */
4510 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4511 const int *macro_str, struct macro_level ** can_read_stream)
4513 Sym *s;
4514 int *macro_str1;
4515 const int *ptr;
4516 int t, ret;
4517 CValue cval;
4518 struct macro_level ml;
4520 /* first scan for '##' operator handling */
4521 ptr = macro_str;
4522 macro_str1 = macro_twosharps(ptr);
4523 if (macro_str1)
4524 ptr = macro_str1;
4525 while (1) {
4526 /* NOTE: ptr == NULL can only happen if tokens are read from
4527 file stream due to a macro function call */
4528 if (ptr == NULL)
4529 break;
4530 TOK_GET(t, ptr, cval);
4531 if (t == 0)
4532 break;
4533 s = define_find(t);
4534 if (s != NULL) {
4535 /* if nested substitution, do nothing */
4536 if (sym_find2(*nested_list, t))
4537 goto no_subst;
4538 ml.p = macro_ptr;
4539 if (can_read_stream)
4540 ml.prev = *can_read_stream, *can_read_stream = &ml;
4541 macro_ptr = (int *)ptr;
4542 tok = t;
4543 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4544 ptr = (int *)macro_ptr;
4545 macro_ptr = ml.p;
4546 if (can_read_stream && *can_read_stream == &ml)
4547 *can_read_stream = ml.prev;
4548 if (ret != 0)
4549 goto no_subst;
4550 } else {
4551 no_subst:
4552 tok_str_add2(tok_str, t, &cval);
4555 if (macro_str1)
4556 tok_str_free(macro_str1);
4559 /* return next token with macro substitution */
4560 static void next(void)
4562 Sym *nested_list, *s;
4563 TokenString str;
4564 struct macro_level *ml;
4566 redo:
4567 next_nomacro();
4568 if (!macro_ptr) {
4569 /* if not reading from macro substituted string, then try
4570 to substitute macros */
4571 if (tok >= TOK_IDENT &&
4572 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4573 s = define_find(tok);
4574 if (s) {
4575 /* we have a macro: we try to substitute */
4576 tok_str_new(&str);
4577 nested_list = NULL;
4578 ml = NULL;
4579 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4580 /* substitution done, NOTE: maybe empty */
4581 tok_str_add(&str, 0);
4582 macro_ptr = str.str;
4583 macro_ptr_allocated = str.str;
4584 goto redo;
4588 } else {
4589 if (tok == 0) {
4590 /* end of macro or end of unget buffer */
4591 if (unget_buffer_enabled) {
4592 macro_ptr = unget_saved_macro_ptr;
4593 unget_buffer_enabled = 0;
4594 } else {
4595 /* end of macro string: free it */
4596 tok_str_free(macro_ptr_allocated);
4597 macro_ptr = NULL;
4599 goto redo;
4603 /* convert preprocessor tokens into C tokens */
4604 if (tok == TOK_PPNUM &&
4605 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4606 parse_number((char *)tokc.cstr->data);
4610 /* push back current token and set current token to 'last_tok'. Only
4611 identifier case handled for labels. */
4612 static inline void unget_tok(int last_tok)
4614 int i, n;
4615 int *q;
4616 unget_saved_macro_ptr = macro_ptr;
4617 unget_buffer_enabled = 1;
4618 q = unget_saved_buffer;
4619 macro_ptr = q;
4620 *q++ = tok;
4621 n = tok_ext_size(tok) - 1;
4622 for(i=0;i<n;i++)
4623 *q++ = tokc.tab[i];
4624 *q = 0; /* end of token string */
4625 tok = last_tok;
4629 void swap(int *p, int *q)
4631 int t;
4632 t = *p;
4633 *p = *q;
4634 *q = t;
4637 void vsetc(CType *type, int r, CValue *vc)
4639 int v;
4641 if (vtop >= vstack + (VSTACK_SIZE - 1))
4642 error("memory full");
4643 /* cannot let cpu flags if other instruction are generated. Also
4644 avoid leaving VT_JMP anywhere except on the top of the stack
4645 because it would complicate the code generator. */
4646 if (vtop >= vstack) {
4647 v = vtop->r & VT_VALMASK;
4648 if (v == VT_CMP || (v & ~1) == VT_JMP)
4649 gv(RC_INT);
4651 vtop++;
4652 vtop->type = *type;
4653 vtop->r = r;
4654 vtop->r2 = VT_CONST;
4655 vtop->c = *vc;
4658 /* push integer constant */
4659 void vpushi(int v)
4661 CValue cval;
4662 cval.i = v;
4663 vsetc(&int_type, VT_CONST, &cval);
4666 /* Return a static symbol pointing to a section */
4667 static Sym *get_sym_ref(CType *type, Section *sec,
4668 unsigned long offset, unsigned long size)
4670 int v;
4671 Sym *sym;
4673 v = anon_sym++;
4674 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4675 sym->type.ref = type->ref;
4676 sym->r = VT_CONST | VT_SYM;
4677 put_extern_sym(sym, sec, offset, size);
4678 return sym;
4681 /* push a reference to a section offset by adding a dummy symbol */
4682 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4684 CValue cval;
4686 cval.ul = 0;
4687 vsetc(type, VT_CONST | VT_SYM, &cval);
4688 vtop->sym = get_sym_ref(type, sec, offset, size);
4691 /* define a new external reference to a symbol 'v' of type 'u' */
4692 static Sym *external_global_sym(int v, CType *type, int r)
4694 Sym *s;
4696 s = sym_find(v);
4697 if (!s) {
4698 /* push forward reference */
4699 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4700 s->type.ref = type->ref;
4701 s->r = r | VT_CONST | VT_SYM;
4703 return s;
4706 /* define a new external reference to a symbol 'v' of type 'u' */
4707 static Sym *external_sym(int v, CType *type, int r)
4709 Sym *s;
4711 s = sym_find(v);
4712 if (!s) {
4713 /* push forward reference */
4714 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4715 s->type.t |= VT_EXTERN;
4716 } else {
4717 if (!is_compatible_types(&s->type, type))
4718 error("incompatible types for redefinition of '%s'",
4719 get_tok_str(v, NULL));
4721 return s;
4724 /* push a reference to global symbol v */
4725 static void vpush_global_sym(CType *type, int v)
4727 Sym *sym;
4728 CValue cval;
4730 sym = external_global_sym(v, type, 0);
4731 cval.ul = 0;
4732 vsetc(type, VT_CONST | VT_SYM, &cval);
4733 vtop->sym = sym;
4736 void vset(CType *type, int r, int v)
4738 CValue cval;
4740 cval.i = v;
4741 vsetc(type, r, &cval);
4744 void vseti(int r, int v)
4746 CType type;
4747 type.t = VT_INT;
4748 vset(&type, r, v);
4751 void vswap(void)
4753 SValue tmp;
4755 tmp = vtop[0];
4756 vtop[0] = vtop[-1];
4757 vtop[-1] = tmp;
4760 void vpushv(SValue *v)
4762 if (vtop >= vstack + (VSTACK_SIZE - 1))
4763 error("memory full");
4764 vtop++;
4765 *vtop = *v;
4768 void vdup(void)
4770 vpushv(vtop);
4773 /* save r to the memory stack, and mark it as being free */
4774 void save_reg(int r)
4776 int l, saved, size, align;
4777 SValue *p, sv;
4778 CType *type;
4780 /* modify all stack values */
4781 saved = 0;
4782 l = 0;
4783 for(p=vstack;p<=vtop;p++) {
4784 if ((p->r & VT_VALMASK) == r ||
4785 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4786 /* must save value on stack if not already done */
4787 if (!saved) {
4788 /* NOTE: must reload 'r' because r might be equal to r2 */
4789 r = p->r & VT_VALMASK;
4790 /* store register in the stack */
4791 type = &p->type;
4792 #ifndef TCC_TARGET_X86_64
4793 if ((p->r & VT_LVAL) ||
4794 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4795 type = &int_type;
4796 #else
4797 if (p->r & VT_LVAL)
4798 type = &char_pointer_type;
4799 #endif
4800 size = type_size(type, &align);
4801 loc = (loc - size) & -align;
4802 sv.type.t = type->t;
4803 sv.r = VT_LOCAL | VT_LVAL;
4804 sv.c.ul = loc;
4805 store(r, &sv);
4806 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
4807 /* x86 specific: need to pop fp register ST0 if saved */
4808 if (r == TREG_ST0) {
4809 o(0xd9dd); /* fstp %st(1) */
4811 #endif
4812 #ifndef TCC_TARGET_X86_64
4813 /* special long long case */
4814 if ((type->t & VT_BTYPE) == VT_LLONG) {
4815 sv.c.ul += 4;
4816 store(p->r2, &sv);
4818 #endif
4819 l = loc;
4820 saved = 1;
4822 /* mark that stack entry as being saved on the stack */
4823 if (p->r & VT_LVAL) {
4824 /* also clear the bounded flag because the
4825 relocation address of the function was stored in
4826 p->c.ul */
4827 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4828 } else {
4829 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4831 p->r2 = VT_CONST;
4832 p->c.ul = l;
4837 /* find a register of class 'rc2' with at most one reference on stack.
4838 * If none, call get_reg(rc) */
4839 int get_reg_ex(int rc, int rc2)
4841 int r;
4842 SValue *p;
4844 for(r=0;r<NB_REGS;r++) {
4845 if (reg_classes[r] & rc2) {
4846 int n;
4847 n=0;
4848 for(p = vstack; p <= vtop; p++) {
4849 if ((p->r & VT_VALMASK) == r ||
4850 (p->r2 & VT_VALMASK) == r)
4851 n++;
4853 if (n <= 1)
4854 return r;
4857 return get_reg(rc);
4860 /* find a free register of class 'rc'. If none, save one register */
4861 int get_reg(int rc)
4863 int r;
4864 SValue *p;
4866 /* find a free register */
4867 for(r=0;r<NB_REGS;r++) {
4868 if (reg_classes[r] & rc) {
4869 for(p=vstack;p<=vtop;p++) {
4870 if ((p->r & VT_VALMASK) == r ||
4871 (p->r2 & VT_VALMASK) == r)
4872 goto notfound;
4874 return r;
4876 notfound: ;
4879 /* no register left : free the first one on the stack (VERY
4880 IMPORTANT to start from the bottom to ensure that we don't
4881 spill registers used in gen_opi()) */
4882 for(p=vstack;p<=vtop;p++) {
4883 r = p->r & VT_VALMASK;
4884 if (r < VT_CONST && (reg_classes[r] & rc))
4885 goto save_found;
4886 /* also look at second register (if long long) */
4887 r = p->r2 & VT_VALMASK;
4888 if (r < VT_CONST && (reg_classes[r] & rc)) {
4889 save_found:
4890 save_reg(r);
4891 return r;
4894 /* Should never comes here */
4895 return -1;
4898 /* save registers up to (vtop - n) stack entry */
4899 void save_regs(int n)
4901 int r;
4902 SValue *p, *p1;
4903 p1 = vtop - n;
4904 for(p = vstack;p <= p1; p++) {
4905 r = p->r & VT_VALMASK;
4906 if (r < VT_CONST) {
4907 save_reg(r);
4912 /* move register 's' to 'r', and flush previous value of r to memory
4913 if needed */
4914 void move_reg(int r, int s)
4916 SValue sv;
4918 if (r != s) {
4919 save_reg(r);
4920 sv.type.t = VT_INT;
4921 sv.r = s;
4922 sv.c.ul = 0;
4923 load(r, &sv);
4927 /* get address of vtop (vtop MUST BE an lvalue) */
4928 void gaddrof(void)
4930 vtop->r &= ~VT_LVAL;
4931 /* tricky: if saved lvalue, then we can go back to lvalue */
4932 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4933 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4936 #ifdef CONFIG_TCC_BCHECK
4937 /* generate lvalue bound code */
4938 void gbound(void)
4940 int lval_type;
4941 CType type1;
4943 vtop->r &= ~VT_MUSTBOUND;
4944 /* if lvalue, then use checking code before dereferencing */
4945 if (vtop->r & VT_LVAL) {
4946 /* if not VT_BOUNDED value, then make one */
4947 if (!(vtop->r & VT_BOUNDED)) {
4948 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4949 /* must save type because we must set it to int to get pointer */
4950 type1 = vtop->type;
4951 vtop->type.t = VT_INT;
4952 gaddrof();
4953 vpushi(0);
4954 gen_bounded_ptr_add();
4955 vtop->r |= lval_type;
4956 vtop->type = type1;
4958 /* then check for dereferencing */
4959 gen_bounded_ptr_deref();
4962 #endif
4964 /* store vtop a register belonging to class 'rc'. lvalues are
4965 converted to values. Cannot be used if cannot be converted to
4966 register value (such as structures). */
4967 int gv(int rc)
4969 int r, rc2, bit_pos, bit_size, size, align, i;
4971 /* NOTE: get_reg can modify vstack[] */
4972 if (vtop->type.t & VT_BITFIELD) {
4973 CType type;
4974 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4975 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4976 /* remove bit field info to avoid loops */
4977 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4978 /* cast to int to propagate signedness in following ops */
4979 type.t = VT_INT;
4980 if((vtop->type.t & VT_UNSIGNED) ||
4981 (vtop->type.t & VT_BTYPE) == VT_BOOL)
4982 type.t |= VT_UNSIGNED;
4983 gen_cast(&type);
4984 /* generate shifts */
4985 vpushi(32 - (bit_pos + bit_size));
4986 gen_op(TOK_SHL);
4987 vpushi(32 - bit_size);
4988 /* NOTE: transformed to SHR if unsigned */
4989 gen_op(TOK_SAR);
4990 r = gv(rc);
4991 } else {
4992 if (is_float(vtop->type.t) &&
4993 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4994 Sym *sym;
4995 int *ptr;
4996 unsigned long offset;
4997 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4998 CValue check;
4999 #endif
5001 /* XXX: unify with initializers handling ? */
5002 /* CPUs usually cannot use float constants, so we store them
5003 generically in data segment */
5004 size = type_size(&vtop->type, &align);
5005 offset = (data_section->data_offset + align - 1) & -align;
5006 data_section->data_offset = offset;
5007 /* XXX: not portable yet */
5008 #if defined(__i386__) || defined(__x86_64__)
5009 /* Zero pad x87 tenbyte long doubles */
5010 if (size == LDOUBLE_SIZE)
5011 vtop->c.tab[2] &= 0xffff;
5012 #endif
5013 ptr = section_ptr_add(data_section, size);
5014 size = size >> 2;
5015 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5016 check.d = 1;
5017 if(check.tab[0])
5018 for(i=0;i<size;i++)
5019 ptr[i] = vtop->c.tab[size-1-i];
5020 else
5021 #endif
5022 for(i=0;i<size;i++)
5023 ptr[i] = vtop->c.tab[i];
5024 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
5025 vtop->r |= VT_LVAL | VT_SYM;
5026 vtop->sym = sym;
5027 vtop->c.ul = 0;
5029 #ifdef CONFIG_TCC_BCHECK
5030 if (vtop->r & VT_MUSTBOUND)
5031 gbound();
5032 #endif
5034 r = vtop->r & VT_VALMASK;
5035 rc2 = RC_INT;
5036 if (rc == RC_IRET)
5037 rc2 = RC_LRET;
5038 /* need to reload if:
5039 - constant
5040 - lvalue (need to dereference pointer)
5041 - already a register, but not in the right class */
5042 if (r >= VT_CONST ||
5043 (vtop->r & VT_LVAL) ||
5044 !(reg_classes[r] & rc) ||
5045 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
5046 !(reg_classes[vtop->r2] & rc2))) {
5047 r = get_reg(rc);
5048 #ifndef TCC_TARGET_X86_64
5049 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
5050 int r2;
5051 unsigned long long ll;
5052 /* two register type load : expand to two words
5053 temporarily */
5054 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
5055 /* load constant */
5056 ll = vtop->c.ull;
5057 vtop->c.ui = ll; /* first word */
5058 load(r, vtop);
5059 vtop->r = r; /* save register value */
5060 vpushi(ll >> 32); /* second word */
5061 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
5062 (vtop->r & VT_LVAL)) {
5063 /* We do not want to modifier the long long
5064 pointer here, so the safest (and less
5065 efficient) is to save all the other registers
5066 in the stack. XXX: totally inefficient. */
5067 save_regs(1);
5068 /* load from memory */
5069 load(r, vtop);
5070 vdup();
5071 vtop[-1].r = r; /* save register value */
5072 /* increment pointer to get second word */
5073 vtop->type.t = VT_INT;
5074 gaddrof();
5075 vpushi(4);
5076 gen_op('+');
5077 vtop->r |= VT_LVAL;
5078 } else {
5079 /* move registers */
5080 load(r, vtop);
5081 vdup();
5082 vtop[-1].r = r; /* save register value */
5083 vtop->r = vtop[-1].r2;
5085 /* allocate second register */
5086 r2 = get_reg(rc2);
5087 load(r2, vtop);
5088 vpop();
5089 /* write second register */
5090 vtop->r2 = r2;
5091 } else
5092 #endif
5093 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5094 int t1, t;
5095 /* lvalue of scalar type : need to use lvalue type
5096 because of possible cast */
5097 t = vtop->type.t;
5098 t1 = t;
5099 /* compute memory access type */
5100 if (vtop->r & VT_LVAL_BYTE)
5101 t = VT_BYTE;
5102 else if (vtop->r & VT_LVAL_SHORT)
5103 t = VT_SHORT;
5104 if (vtop->r & VT_LVAL_UNSIGNED)
5105 t |= VT_UNSIGNED;
5106 vtop->type.t = t;
5107 load(r, vtop);
5108 /* restore wanted type */
5109 vtop->type.t = t1;
5110 } else {
5111 /* one register type load */
5112 load(r, vtop);
5115 vtop->r = r;
5116 #ifdef TCC_TARGET_C67
5117 /* uses register pairs for doubles */
5118 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5119 vtop->r2 = r+1;
5120 #endif
5122 return r;
5125 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5126 void gv2(int rc1, int rc2)
5128 int v;
5130 /* generate more generic register first. But VT_JMP or VT_CMP
5131 values must be generated first in all cases to avoid possible
5132 reload errors */
5133 v = vtop[0].r & VT_VALMASK;
5134 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5135 vswap();
5136 gv(rc1);
5137 vswap();
5138 gv(rc2);
5139 /* test if reload is needed for first register */
5140 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5141 vswap();
5142 gv(rc1);
5143 vswap();
5145 } else {
5146 gv(rc2);
5147 vswap();
5148 gv(rc1);
5149 vswap();
5150 /* test if reload is needed for first register */
5151 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5152 gv(rc2);
5157 /* expand long long on stack in two int registers */
5158 void lexpand(void)
5160 int u;
5162 u = vtop->type.t & VT_UNSIGNED;
5163 gv(RC_INT);
5164 vdup();
5165 vtop[0].r = vtop[-1].r2;
5166 vtop[0].r2 = VT_CONST;
5167 vtop[-1].r2 = VT_CONST;
5168 vtop[0].type.t = VT_INT | u;
5169 vtop[-1].type.t = VT_INT | u;
5172 #ifdef TCC_TARGET_ARM
5173 /* expand long long on stack */
5174 void lexpand_nr(void)
5176 int u,v;
5178 u = vtop->type.t & VT_UNSIGNED;
5179 vdup();
5180 vtop->r2 = VT_CONST;
5181 vtop->type.t = VT_INT | u;
5182 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5183 if (v == VT_CONST) {
5184 vtop[-1].c.ui = vtop->c.ull;
5185 vtop->c.ui = vtop->c.ull >> 32;
5186 vtop->r = VT_CONST;
5187 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5188 vtop->c.ui += 4;
5189 vtop->r = vtop[-1].r;
5190 } else if (v > VT_CONST) {
5191 vtop--;
5192 lexpand();
5193 } else
5194 vtop->r = vtop[-1].r2;
5195 vtop[-1].r2 = VT_CONST;
5196 vtop[-1].type.t = VT_INT | u;
5198 #endif
5200 /* build a long long from two ints */
5201 void lbuild(int t)
5203 gv2(RC_INT, RC_INT);
5204 vtop[-1].r2 = vtop[0].r;
5205 vtop[-1].type.t = t;
5206 vpop();
5209 /* rotate n first stack elements to the bottom
5210 I1 ... In -> I2 ... In I1 [top is right]
5212 void vrotb(int n)
5214 int i;
5215 SValue tmp;
5217 tmp = vtop[-n + 1];
5218 for(i=-n+1;i!=0;i++)
5219 vtop[i] = vtop[i+1];
5220 vtop[0] = tmp;
5223 /* rotate n first stack elements to the top
5224 I1 ... In -> In I1 ... I(n-1) [top is right]
5226 void vrott(int n)
5228 int i;
5229 SValue tmp;
5231 tmp = vtop[0];
5232 for(i = 0;i < n - 1; i++)
5233 vtop[-i] = vtop[-i - 1];
5234 vtop[-n + 1] = tmp;
5237 #ifdef TCC_TARGET_ARM
5238 /* like vrott but in other direction
5239 In ... I1 -> I(n-1) ... I1 In [top is right]
5241 void vnrott(int n)
5243 int i;
5244 SValue tmp;
5246 tmp = vtop[-n + 1];
5247 for(i = n - 1; i > 0; i--)
5248 vtop[-i] = vtop[-i + 1];
5249 vtop[0] = tmp;
5251 #endif
5253 /* pop stack value */
5254 void vpop(void)
5256 int v;
5257 v = vtop->r & VT_VALMASK;
5258 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
5259 /* for x86, we need to pop the FP stack */
5260 if (v == TREG_ST0 && !nocode_wanted) {
5261 o(0xd9dd); /* fstp %st(1) */
5262 } else
5263 #endif
5264 if (v == VT_JMP || v == VT_JMPI) {
5265 /* need to put correct jump if && or || without test */
5266 gsym(vtop->c.ul);
5268 vtop--;
5271 /* convert stack entry to register and duplicate its value in another
5272 register */
5273 void gv_dup(void)
5275 int rc, t, r, r1;
5276 SValue sv;
5278 t = vtop->type.t;
5279 if ((t & VT_BTYPE) == VT_LLONG) {
5280 lexpand();
5281 gv_dup();
5282 vswap();
5283 vrotb(3);
5284 gv_dup();
5285 vrotb(4);
5286 /* stack: H L L1 H1 */
5287 lbuild(t);
5288 vrotb(3);
5289 vrotb(3);
5290 vswap();
5291 lbuild(t);
5292 vswap();
5293 } else {
5294 /* duplicate value */
5295 rc = RC_INT;
5296 sv.type.t = VT_INT;
5297 if (is_float(t)) {
5298 rc = RC_FLOAT;
5299 #ifdef TCC_TARGET_X86_64
5300 if ((t & VT_BTYPE) == VT_LDOUBLE) {
5301 rc = RC_ST0;
5303 #endif
5304 sv.type.t = t;
5306 r = gv(rc);
5307 r1 = get_reg(rc);
5308 sv.r = r;
5309 sv.c.ul = 0;
5310 load(r1, &sv); /* move r to r1 */
5311 vdup();
5312 /* duplicates value */
5313 vtop->r = r1;
5317 #ifndef TCC_TARGET_X86_64
5318 /* generate CPU independent (unsigned) long long operations */
5319 void gen_opl(int op)
5321 int t, a, b, op1, c, i;
5322 int func;
5323 unsigned short reg_iret = REG_IRET;
5324 unsigned short reg_lret = REG_LRET;
5325 SValue tmp;
5327 switch(op) {
5328 case '/':
5329 case TOK_PDIV:
5330 func = TOK___divdi3;
5331 goto gen_func;
5332 case TOK_UDIV:
5333 func = TOK___udivdi3;
5334 goto gen_func;
5335 case '%':
5336 func = TOK___moddi3;
5337 goto gen_mod_func;
5338 case TOK_UMOD:
5339 func = TOK___umoddi3;
5340 gen_mod_func:
5341 #ifdef TCC_ARM_EABI
5342 reg_iret = TREG_R2;
5343 reg_lret = TREG_R3;
5344 #endif
5345 gen_func:
5346 /* call generic long long function */
5347 vpush_global_sym(&func_old_type, func);
5348 vrott(3);
5349 gfunc_call(2);
5350 vpushi(0);
5351 vtop->r = reg_iret;
5352 vtop->r2 = reg_lret;
5353 break;
5354 case '^':
5355 case '&':
5356 case '|':
5357 case '*':
5358 case '+':
5359 case '-':
5360 t = vtop->type.t;
5361 vswap();
5362 lexpand();
5363 vrotb(3);
5364 lexpand();
5365 /* stack: L1 H1 L2 H2 */
5366 tmp = vtop[0];
5367 vtop[0] = vtop[-3];
5368 vtop[-3] = tmp;
5369 tmp = vtop[-2];
5370 vtop[-2] = vtop[-3];
5371 vtop[-3] = tmp;
5372 vswap();
5373 /* stack: H1 H2 L1 L2 */
5374 if (op == '*') {
5375 vpushv(vtop - 1);
5376 vpushv(vtop - 1);
5377 gen_op(TOK_UMULL);
5378 lexpand();
5379 /* stack: H1 H2 L1 L2 ML MH */
5380 for(i=0;i<4;i++)
5381 vrotb(6);
5382 /* stack: ML MH H1 H2 L1 L2 */
5383 tmp = vtop[0];
5384 vtop[0] = vtop[-2];
5385 vtop[-2] = tmp;
5386 /* stack: ML MH H1 L2 H2 L1 */
5387 gen_op('*');
5388 vrotb(3);
5389 vrotb(3);
5390 gen_op('*');
5391 /* stack: ML MH M1 M2 */
5392 gen_op('+');
5393 gen_op('+');
5394 } else if (op == '+' || op == '-') {
5395 /* XXX: add non carry method too (for MIPS or alpha) */
5396 if (op == '+')
5397 op1 = TOK_ADDC1;
5398 else
5399 op1 = TOK_SUBC1;
5400 gen_op(op1);
5401 /* stack: H1 H2 (L1 op L2) */
5402 vrotb(3);
5403 vrotb(3);
5404 gen_op(op1 + 1); /* TOK_xxxC2 */
5405 } else {
5406 gen_op(op);
5407 /* stack: H1 H2 (L1 op L2) */
5408 vrotb(3);
5409 vrotb(3);
5410 /* stack: (L1 op L2) H1 H2 */
5411 gen_op(op);
5412 /* stack: (L1 op L2) (H1 op H2) */
5414 /* stack: L H */
5415 lbuild(t);
5416 break;
5417 case TOK_SAR:
5418 case TOK_SHR:
5419 case TOK_SHL:
5420 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5421 t = vtop[-1].type.t;
5422 vswap();
5423 lexpand();
5424 vrotb(3);
5425 /* stack: L H shift */
5426 c = (int)vtop->c.i;
5427 /* constant: simpler */
5428 /* NOTE: all comments are for SHL. the other cases are
5429 done by swaping words */
5430 vpop();
5431 if (op != TOK_SHL)
5432 vswap();
5433 if (c >= 32) {
5434 /* stack: L H */
5435 vpop();
5436 if (c > 32) {
5437 vpushi(c - 32);
5438 gen_op(op);
5440 if (op != TOK_SAR) {
5441 vpushi(0);
5442 } else {
5443 gv_dup();
5444 vpushi(31);
5445 gen_op(TOK_SAR);
5447 vswap();
5448 } else {
5449 vswap();
5450 gv_dup();
5451 /* stack: H L L */
5452 vpushi(c);
5453 gen_op(op);
5454 vswap();
5455 vpushi(32 - c);
5456 if (op == TOK_SHL)
5457 gen_op(TOK_SHR);
5458 else
5459 gen_op(TOK_SHL);
5460 vrotb(3);
5461 /* stack: L L H */
5462 vpushi(c);
5463 if (op == TOK_SHL)
5464 gen_op(TOK_SHL);
5465 else
5466 gen_op(TOK_SHR);
5467 gen_op('|');
5469 if (op != TOK_SHL)
5470 vswap();
5471 lbuild(t);
5472 } else {
5473 /* XXX: should provide a faster fallback on x86 ? */
5474 switch(op) {
5475 case TOK_SAR:
5476 func = TOK___ashrdi3;
5477 goto gen_func;
5478 case TOK_SHR:
5479 func = TOK___lshrdi3;
5480 goto gen_func;
5481 case TOK_SHL:
5482 func = TOK___ashldi3;
5483 goto gen_func;
5486 break;
5487 default:
5488 /* compare operations */
5489 t = vtop->type.t;
5490 vswap();
5491 lexpand();
5492 vrotb(3);
5493 lexpand();
5494 /* stack: L1 H1 L2 H2 */
5495 tmp = vtop[-1];
5496 vtop[-1] = vtop[-2];
5497 vtop[-2] = tmp;
5498 /* stack: L1 L2 H1 H2 */
5499 /* compare high */
5500 op1 = op;
5501 /* when values are equal, we need to compare low words. since
5502 the jump is inverted, we invert the test too. */
5503 if (op1 == TOK_LT)
5504 op1 = TOK_LE;
5505 else if (op1 == TOK_GT)
5506 op1 = TOK_GE;
5507 else if (op1 == TOK_ULT)
5508 op1 = TOK_ULE;
5509 else if (op1 == TOK_UGT)
5510 op1 = TOK_UGE;
5511 a = 0;
5512 b = 0;
5513 gen_op(op1);
5514 if (op1 != TOK_NE) {
5515 a = gtst(1, 0);
5517 if (op != TOK_EQ) {
5518 /* generate non equal test */
5519 /* XXX: NOT PORTABLE yet */
5520 if (a == 0) {
5521 b = gtst(0, 0);
5522 } else {
5523 #if defined(TCC_TARGET_I386)
5524 b = psym(0x850f, 0);
5525 #elif defined(TCC_TARGET_ARM)
5526 b = ind;
5527 o(0x1A000000 | encbranch(ind, 0, 1));
5528 #elif defined(TCC_TARGET_C67)
5529 error("not implemented");
5530 #else
5531 #error not supported
5532 #endif
5535 /* compare low. Always unsigned */
5536 op1 = op;
5537 if (op1 == TOK_LT)
5538 op1 = TOK_ULT;
5539 else if (op1 == TOK_LE)
5540 op1 = TOK_ULE;
5541 else if (op1 == TOK_GT)
5542 op1 = TOK_UGT;
5543 else if (op1 == TOK_GE)
5544 op1 = TOK_UGE;
5545 gen_op(op1);
5546 a = gtst(1, a);
5547 gsym(b);
5548 vseti(VT_JMPI, a);
5549 break;
5552 #endif
5554 /* handle integer constant optimizations and various machine
5555 independent opt */
5556 void gen_opic(int op)
5558 int c1, c2, t1, t2, n;
5559 SValue *v1, *v2;
5560 long long l1, l2;
5561 typedef unsigned long long U;
5563 v1 = vtop - 1;
5564 v2 = vtop;
5565 t1 = v1->type.t & VT_BTYPE;
5566 t2 = v2->type.t & VT_BTYPE;
5568 if (t1 == VT_LLONG)
5569 l1 = v1->c.ll;
5570 else if (v1->type.t & VT_UNSIGNED)
5571 l1 = v1->c.ui;
5572 else
5573 l1 = v1->c.i;
5575 if (t2 == VT_LLONG)
5576 l2 = v2->c.ll;
5577 else if (v2->type.t & VT_UNSIGNED)
5578 l2 = v2->c.ui;
5579 else
5580 l2 = v2->c.i;
5582 /* currently, we cannot do computations with forward symbols */
5583 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5584 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5585 if (c1 && c2) {
5586 switch(op) {
5587 case '+': l1 += l2; break;
5588 case '-': l1 -= l2; break;
5589 case '&': l1 &= l2; break;
5590 case '^': l1 ^= l2; break;
5591 case '|': l1 |= l2; break;
5592 case '*': l1 *= l2; break;
5594 case TOK_PDIV:
5595 case '/':
5596 case '%':
5597 case TOK_UDIV:
5598 case TOK_UMOD:
5599 /* if division by zero, generate explicit division */
5600 if (l2 == 0) {
5601 if (const_wanted)
5602 error("division by zero in constant");
5603 goto general_case;
5605 switch(op) {
5606 default: l1 /= l2; break;
5607 case '%': l1 %= l2; break;
5608 case TOK_UDIV: l1 = (U)l1 / l2; break;
5609 case TOK_UMOD: l1 = (U)l1 % l2; break;
5611 break;
5612 case TOK_SHL: l1 <<= l2; break;
5613 case TOK_SHR: l1 = (U)l1 >> l2; break;
5614 case TOK_SAR: l1 >>= l2; break;
5615 /* tests */
5616 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5617 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5618 case TOK_EQ: l1 = l1 == l2; break;
5619 case TOK_NE: l1 = l1 != l2; break;
5620 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5621 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5622 case TOK_LT: l1 = l1 < l2; break;
5623 case TOK_GE: l1 = l1 >= l2; break;
5624 case TOK_LE: l1 = l1 <= l2; break;
5625 case TOK_GT: l1 = l1 > l2; break;
5626 /* logical */
5627 case TOK_LAND: l1 = l1 && l2; break;
5628 case TOK_LOR: l1 = l1 || l2; break;
5629 default:
5630 goto general_case;
5632 v1->c.ll = l1;
5633 vtop--;
5634 } else {
5635 /* if commutative ops, put c2 as constant */
5636 if (c1 && (op == '+' || op == '&' || op == '^' ||
5637 op == '|' || op == '*')) {
5638 vswap();
5639 c2 = c1; //c = c1, c1 = c2, c2 = c;
5640 l2 = l1; //l = l1, l1 = l2, l2 = l;
5642 /* Filter out NOP operations like x*1, x-0, x&-1... */
5643 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5644 op == TOK_PDIV) &&
5645 l2 == 1) ||
5646 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5647 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5648 l2 == 0) ||
5649 (op == '&' &&
5650 l2 == -1))) {
5651 /* nothing to do */
5652 vtop--;
5653 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5654 /* try to use shifts instead of muls or divs */
5655 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5656 n = -1;
5657 while (l2) {
5658 l2 >>= 1;
5659 n++;
5661 vtop->c.ll = n;
5662 if (op == '*')
5663 op = TOK_SHL;
5664 else if (op == TOK_PDIV)
5665 op = TOK_SAR;
5666 else
5667 op = TOK_SHR;
5669 goto general_case;
5670 } else if (c2 && (op == '+' || op == '-') &&
5671 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5672 (VT_CONST | VT_SYM) ||
5673 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
5674 /* symbol + constant case */
5675 if (op == '-')
5676 l2 = -l2;
5677 vtop--;
5678 vtop->c.ll += l2;
5679 } else {
5680 general_case:
5681 if (!nocode_wanted) {
5682 /* call low level op generator */
5683 if (t1 == VT_LLONG || t2 == VT_LLONG)
5684 gen_opl(op);
5685 else
5686 gen_opi(op);
5687 } else {
5688 vtop--;
5694 /* generate a floating point operation with constant propagation */
5695 void gen_opif(int op)
5697 int c1, c2;
5698 SValue *v1, *v2;
5699 long double f1, f2;
5701 v1 = vtop - 1;
5702 v2 = vtop;
5703 /* currently, we cannot do computations with forward symbols */
5704 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5705 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5706 if (c1 && c2) {
5707 if (v1->type.t == VT_FLOAT) {
5708 f1 = v1->c.f;
5709 f2 = v2->c.f;
5710 } else if (v1->type.t == VT_DOUBLE) {
5711 f1 = v1->c.d;
5712 f2 = v2->c.d;
5713 } else {
5714 f1 = v1->c.ld;
5715 f2 = v2->c.ld;
5718 /* NOTE: we only do constant propagation if finite number (not
5719 NaN or infinity) (ANSI spec) */
5720 if (!ieee_finite(f1) || !ieee_finite(f2))
5721 goto general_case;
5723 switch(op) {
5724 case '+': f1 += f2; break;
5725 case '-': f1 -= f2; break;
5726 case '*': f1 *= f2; break;
5727 case '/':
5728 if (f2 == 0.0) {
5729 if (const_wanted)
5730 error("division by zero in constant");
5731 goto general_case;
5733 f1 /= f2;
5734 break;
5735 /* XXX: also handles tests ? */
5736 default:
5737 goto general_case;
5739 /* XXX: overflow test ? */
5740 if (v1->type.t == VT_FLOAT) {
5741 v1->c.f = f1;
5742 } else if (v1->type.t == VT_DOUBLE) {
5743 v1->c.d = f1;
5744 } else {
5745 v1->c.ld = f1;
5747 vtop--;
5748 } else {
5749 general_case:
5750 if (!nocode_wanted) {
5751 gen_opf(op);
5752 } else {
5753 vtop--;
5758 static int pointed_size(CType *type)
5760 int align;
5761 return type_size(pointed_type(type), &align);
5764 static inline int is_null_pointer(SValue *p)
5766 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5767 return 0;
5768 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5769 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5772 static inline int is_integer_btype(int bt)
5774 return (bt == VT_BYTE || bt == VT_SHORT ||
5775 bt == VT_INT || bt == VT_LLONG);
5778 /* check types for comparison or substraction of pointers */
5779 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5781 CType *type1, *type2, tmp_type1, tmp_type2;
5782 int bt1, bt2;
5784 /* null pointers are accepted for all comparisons as gcc */
5785 if (is_null_pointer(p1) || is_null_pointer(p2))
5786 return;
5787 type1 = &p1->type;
5788 type2 = &p2->type;
5789 bt1 = type1->t & VT_BTYPE;
5790 bt2 = type2->t & VT_BTYPE;
5791 /* accept comparison between pointer and integer with a warning */
5792 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5793 if (op != TOK_LOR && op != TOK_LAND )
5794 warning("comparison between pointer and integer");
5795 return;
5798 /* both must be pointers or implicit function pointers */
5799 if (bt1 == VT_PTR) {
5800 type1 = pointed_type(type1);
5801 } else if (bt1 != VT_FUNC)
5802 goto invalid_operands;
5804 if (bt2 == VT_PTR) {
5805 type2 = pointed_type(type2);
5806 } else if (bt2 != VT_FUNC) {
5807 invalid_operands:
5808 error("invalid operands to binary %s", get_tok_str(op, NULL));
5810 if ((type1->t & VT_BTYPE) == VT_VOID ||
5811 (type2->t & VT_BTYPE) == VT_VOID)
5812 return;
5813 tmp_type1 = *type1;
5814 tmp_type2 = *type2;
5815 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5816 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5817 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5818 /* gcc-like error if '-' is used */
5819 if (op == '-')
5820 goto invalid_operands;
5821 else
5822 warning("comparison of distinct pointer types lacks a cast");
5826 /* generic gen_op: handles types problems */
5827 void gen_op(int op)
5829 int u, t1, t2, bt1, bt2, t;
5830 CType type1;
5832 t1 = vtop[-1].type.t;
5833 t2 = vtop[0].type.t;
5834 bt1 = t1 & VT_BTYPE;
5835 bt2 = t2 & VT_BTYPE;
5837 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5838 /* at least one operand is a pointer */
5839 /* relationnal op: must be both pointers */
5840 if (op >= TOK_ULT && op <= TOK_LOR) {
5841 check_comparison_pointer_types(vtop - 1, vtop, op);
5842 /* pointers are handled are unsigned */
5843 #ifdef TCC_TARGET_X86_64
5844 t = VT_LLONG | VT_UNSIGNED;
5845 #else
5846 t = VT_INT | VT_UNSIGNED;
5847 #endif
5848 goto std_op;
5850 /* if both pointers, then it must be the '-' op */
5851 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5852 if (op != '-')
5853 error("cannot use pointers here");
5854 check_comparison_pointer_types(vtop - 1, vtop, op);
5855 /* XXX: check that types are compatible */
5856 u = pointed_size(&vtop[-1].type);
5857 gen_opic(op);
5858 /* set to integer type */
5859 #ifdef TCC_TARGET_X86_64
5860 vtop->type.t = VT_LLONG;
5861 #else
5862 vtop->type.t = VT_INT;
5863 #endif
5864 vpushi(u);
5865 gen_op(TOK_PDIV);
5866 } else {
5867 /* exactly one pointer : must be '+' or '-'. */
5868 if (op != '-' && op != '+')
5869 error("cannot use pointers here");
5870 /* Put pointer as first operand */
5871 if (bt2 == VT_PTR) {
5872 vswap();
5873 swap(&t1, &t2);
5875 type1 = vtop[-1].type;
5876 #ifdef TCC_TARGET_X86_64
5878 CValue cval;
5879 CType ctype;
5880 ctype.t = VT_LLONG;
5881 cval.ull = pointed_size(&vtop[-1].type);
5882 vsetc(&ctype, VT_CONST, &cval);
5884 #else
5885 /* XXX: cast to int ? (long long case) */
5886 vpushi(pointed_size(&vtop[-1].type));
5887 #endif
5888 gen_op('*');
5889 #ifdef CONFIG_TCC_BCHECK
5890 /* if evaluating constant expression, no code should be
5891 generated, so no bound check */
5892 if (do_bounds_check && !const_wanted) {
5893 /* if bounded pointers, we generate a special code to
5894 test bounds */
5895 if (op == '-') {
5896 vpushi(0);
5897 vswap();
5898 gen_op('-');
5900 gen_bounded_ptr_add();
5901 } else
5902 #endif
5904 gen_opic(op);
5906 /* put again type if gen_opic() swaped operands */
5907 vtop->type = type1;
5909 } else if (is_float(bt1) || is_float(bt2)) {
5910 /* compute bigger type and do implicit casts */
5911 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5912 t = VT_LDOUBLE;
5913 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5914 t = VT_DOUBLE;
5915 } else {
5916 t = VT_FLOAT;
5918 /* floats can only be used for a few operations */
5919 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5920 (op < TOK_ULT || op > TOK_GT))
5921 error("invalid operands for binary operation");
5922 goto std_op;
5923 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5924 /* cast to biggest op */
5925 t = VT_LLONG;
5926 /* convert to unsigned if it does not fit in a long long */
5927 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5928 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5929 t |= VT_UNSIGNED;
5930 goto std_op;
5931 } else {
5932 /* integer operations */
5933 t = VT_INT;
5934 /* convert to unsigned if it does not fit in an integer */
5935 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5936 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5937 t |= VT_UNSIGNED;
5938 std_op:
5939 /* XXX: currently, some unsigned operations are explicit, so
5940 we modify them here */
5941 if (t & VT_UNSIGNED) {
5942 if (op == TOK_SAR)
5943 op = TOK_SHR;
5944 else if (op == '/')
5945 op = TOK_UDIV;
5946 else if (op == '%')
5947 op = TOK_UMOD;
5948 else if (op == TOK_LT)
5949 op = TOK_ULT;
5950 else if (op == TOK_GT)
5951 op = TOK_UGT;
5952 else if (op == TOK_LE)
5953 op = TOK_ULE;
5954 else if (op == TOK_GE)
5955 op = TOK_UGE;
5957 vswap();
5958 type1.t = t;
5959 gen_cast(&type1);
5960 vswap();
5961 /* special case for shifts and long long: we keep the shift as
5962 an integer */
5963 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5964 type1.t = VT_INT;
5965 gen_cast(&type1);
5966 if (is_float(t))
5967 gen_opif(op);
5968 else
5969 gen_opic(op);
5970 if (op >= TOK_ULT && op <= TOK_GT) {
5971 /* relationnal op: the result is an int */
5972 vtop->type.t = VT_INT;
5973 } else {
5974 vtop->type.t = t;
5979 #ifndef TCC_TARGET_ARM
5980 /* generic itof for unsigned long long case */
5981 void gen_cvt_itof1(int t)
5983 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5984 (VT_LLONG | VT_UNSIGNED)) {
5986 if (t == VT_FLOAT)
5987 vpush_global_sym(&func_old_type, TOK___floatundisf);
5988 #if LDOUBLE_SIZE != 8
5989 else if (t == VT_LDOUBLE)
5990 vpush_global_sym(&func_old_type, TOK___floatundixf);
5991 #endif
5992 else
5993 vpush_global_sym(&func_old_type, TOK___floatundidf);
5994 vrott(2);
5995 gfunc_call(1);
5996 vpushi(0);
5997 vtop->r = REG_FRET;
5998 } else {
5999 gen_cvt_itof(t);
6002 #endif
6004 /* generic ftoi for unsigned long long case */
6005 void gen_cvt_ftoi1(int t)
6007 int st;
6009 if (t == (VT_LLONG | VT_UNSIGNED)) {
6010 /* not handled natively */
6011 st = vtop->type.t & VT_BTYPE;
6012 if (st == VT_FLOAT)
6013 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
6014 #if LDOUBLE_SIZE != 8
6015 else if (st == VT_LDOUBLE)
6016 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
6017 #endif
6018 else
6019 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
6020 vrott(2);
6021 gfunc_call(1);
6022 vpushi(0);
6023 vtop->r = REG_IRET;
6024 vtop->r2 = REG_LRET;
6025 } else {
6026 gen_cvt_ftoi(t);
6030 /* force char or short cast */
6031 void force_charshort_cast(int t)
6033 int bits, dbt;
6034 dbt = t & VT_BTYPE;
6035 /* XXX: add optimization if lvalue : just change type and offset */
6036 if (dbt == VT_BYTE)
6037 bits = 8;
6038 else
6039 bits = 16;
6040 if (t & VT_UNSIGNED) {
6041 vpushi((1 << bits) - 1);
6042 gen_op('&');
6043 } else {
6044 bits = 32 - bits;
6045 vpushi(bits);
6046 gen_op(TOK_SHL);
6047 /* result must be signed or the SAR is converted to an SHL
6048 This was not the case when "t" was a signed short
6049 and the last value on the stack was an unsigned int */
6050 vtop->type.t &= ~VT_UNSIGNED;
6051 vpushi(bits);
6052 gen_op(TOK_SAR);
6056 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
6057 static void gen_cast(CType *type)
6059 int sbt, dbt, sf, df, c, p;
6061 /* special delayed cast for char/short */
6062 /* XXX: in some cases (multiple cascaded casts), it may still
6063 be incorrect */
6064 if (vtop->r & VT_MUSTCAST) {
6065 vtop->r &= ~VT_MUSTCAST;
6066 force_charshort_cast(vtop->type.t);
6069 /* bitfields first get cast to ints */
6070 if (vtop->type.t & VT_BITFIELD) {
6071 gv(RC_INT);
6074 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
6075 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
6077 if (sbt != dbt) {
6078 sf = is_float(sbt);
6079 df = is_float(dbt);
6080 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6081 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
6082 if (c) {
6083 /* constant case: we can do it now */
6084 /* XXX: in ISOC, cannot do it if error in convert */
6085 if (sbt == VT_FLOAT)
6086 vtop->c.ld = vtop->c.f;
6087 else if (sbt == VT_DOUBLE)
6088 vtop->c.ld = vtop->c.d;
6090 if (df) {
6091 if ((sbt & VT_BTYPE) == VT_LLONG) {
6092 if (sbt & VT_UNSIGNED)
6093 vtop->c.ld = vtop->c.ull;
6094 else
6095 vtop->c.ld = vtop->c.ll;
6096 } else if(!sf) {
6097 if (sbt & VT_UNSIGNED)
6098 vtop->c.ld = vtop->c.ui;
6099 else
6100 vtop->c.ld = vtop->c.i;
6103 if (dbt == VT_FLOAT)
6104 vtop->c.f = (float)vtop->c.ld;
6105 else if (dbt == VT_DOUBLE)
6106 vtop->c.d = (double)vtop->c.ld;
6107 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
6108 vtop->c.ull = (unsigned long long)vtop->c.ld;
6109 } else if (sf && dbt == VT_BOOL) {
6110 vtop->c.i = (vtop->c.ld != 0);
6111 } else {
6112 if(sf)
6113 vtop->c.ll = (long long)vtop->c.ld;
6114 else if (sbt == (VT_LLONG|VT_UNSIGNED))
6115 vtop->c.ll = vtop->c.ull;
6116 else if (sbt & VT_UNSIGNED)
6117 vtop->c.ll = vtop->c.ui;
6118 else if (sbt != VT_LLONG)
6119 vtop->c.ll = vtop->c.i;
6121 if (dbt == (VT_LLONG|VT_UNSIGNED))
6122 vtop->c.ull = vtop->c.ll;
6123 else if (dbt == VT_BOOL)
6124 vtop->c.i = (vtop->c.ll != 0);
6125 else if (dbt != VT_LLONG) {
6126 int s = 0;
6127 if ((dbt & VT_BTYPE) == VT_BYTE)
6128 s = 24;
6129 else if ((dbt & VT_BTYPE) == VT_SHORT)
6130 s = 16;
6132 if(dbt & VT_UNSIGNED)
6133 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
6134 else
6135 vtop->c.i = ((int)vtop->c.ll << s) >> s;
6138 } else if (p && dbt == VT_BOOL) {
6139 vtop->r = VT_CONST;
6140 vtop->c.i = 1;
6141 } else if (!nocode_wanted) {
6142 /* non constant case: generate code */
6143 if (sf && df) {
6144 /* convert from fp to fp */
6145 gen_cvt_ftof(dbt);
6146 } else if (df) {
6147 /* convert int to fp */
6148 gen_cvt_itof1(dbt);
6149 } else if (sf) {
6150 /* convert fp to int */
6151 if (dbt == VT_BOOL) {
6152 vpushi(0);
6153 gen_op(TOK_NE);
6154 } else {
6155 /* we handle char/short/etc... with generic code */
6156 if (dbt != (VT_INT | VT_UNSIGNED) &&
6157 dbt != (VT_LLONG | VT_UNSIGNED) &&
6158 dbt != VT_LLONG)
6159 dbt = VT_INT;
6160 gen_cvt_ftoi1(dbt);
6161 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6162 /* additional cast for char/short... */
6163 vtop->type.t = dbt;
6164 gen_cast(type);
6167 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6168 if ((sbt & VT_BTYPE) != VT_LLONG) {
6169 /* scalar to long long */
6170 #ifndef TCC_TARGET_X86_64
6171 /* machine independent conversion */
6172 gv(RC_INT);
6173 /* generate high word */
6174 if (sbt == (VT_INT | VT_UNSIGNED)) {
6175 vpushi(0);
6176 gv(RC_INT);
6177 } else {
6178 gv_dup();
6179 vpushi(31);
6180 gen_op(TOK_SAR);
6182 /* patch second register */
6183 vtop[-1].r2 = vtop->r;
6184 vpop();
6185 #else
6186 int r = gv(RC_INT);
6187 if (sbt != (VT_INT | VT_UNSIGNED)) {
6188 /* x86_64 specific: movslq */
6189 o(0x6348);
6190 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
6192 #endif
6194 } else if (dbt == VT_BOOL) {
6195 /* scalar to bool */
6196 vpushi(0);
6197 gen_op(TOK_NE);
6198 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6199 (dbt & VT_BTYPE) == VT_SHORT) {
6200 if (sbt == VT_PTR) {
6201 vtop->type.t = VT_INT;
6202 warning("nonportable conversion from pointer to char/short");
6204 force_charshort_cast(dbt);
6205 } else if ((dbt & VT_BTYPE) == VT_INT) {
6206 /* scalar to int */
6207 if (sbt == VT_LLONG) {
6208 /* from long long: just take low order word */
6209 lexpand();
6210 vpop();
6212 /* if lvalue and single word type, nothing to do because
6213 the lvalue already contains the real type size (see
6214 VT_LVAL_xxx constants) */
6217 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6218 /* if we are casting between pointer types,
6219 we must update the VT_LVAL_xxx size */
6220 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6221 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6223 vtop->type = *type;
6226 /* return type size. Put alignment at 'a' */
6227 static int type_size(CType *type, int *a)
6229 Sym *s;
6230 int bt;
6232 bt = type->t & VT_BTYPE;
6233 if (bt == VT_STRUCT) {
6234 /* struct/union */
6235 s = type->ref;
6236 *a = s->r;
6237 return s->c;
6238 } else if (bt == VT_PTR) {
6239 if (type->t & VT_ARRAY) {
6240 int ts;
6242 s = type->ref;
6243 ts = type_size(&s->type, a);
6245 if (ts < 0 && s->c < 0)
6246 ts = -ts;
6248 return ts * s->c;
6249 } else {
6250 *a = PTR_SIZE;
6251 return PTR_SIZE;
6253 } else if (bt == VT_LDOUBLE) {
6254 *a = LDOUBLE_ALIGN;
6255 return LDOUBLE_SIZE;
6256 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6257 #ifdef TCC_TARGET_I386
6258 *a = 4;
6259 #elif defined(TCC_TARGET_ARM)
6260 #ifdef TCC_ARM_EABI
6261 *a = 8;
6262 #else
6263 *a = 4;
6264 #endif
6265 #else
6266 *a = 8;
6267 #endif
6268 return 8;
6269 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6270 *a = 4;
6271 return 4;
6272 } else if (bt == VT_SHORT) {
6273 *a = 2;
6274 return 2;
6275 } else {
6276 /* char, void, function, _Bool */
6277 *a = 1;
6278 return 1;
6282 /* return the pointed type of t */
6283 static inline CType *pointed_type(CType *type)
6285 return &type->ref->type;
6288 /* modify type so that its it is a pointer to type. */
6289 static void mk_pointer(CType *type)
6291 Sym *s;
6292 s = sym_push(SYM_FIELD, type, 0, -1);
6293 type->t = VT_PTR | (type->t & ~VT_TYPE);
6294 type->ref = s;
6297 /* compare function types. OLD functions match any new functions */
6298 static int is_compatible_func(CType *type1, CType *type2)
6300 Sym *s1, *s2;
6302 s1 = type1->ref;
6303 s2 = type2->ref;
6304 if (!is_compatible_types(&s1->type, &s2->type))
6305 return 0;
6306 /* check func_call */
6307 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6308 return 0;
6309 /* XXX: not complete */
6310 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6311 return 1;
6312 if (s1->c != s2->c)
6313 return 0;
6314 while (s1 != NULL) {
6315 if (s2 == NULL)
6316 return 0;
6317 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6318 return 0;
6319 s1 = s1->next;
6320 s2 = s2->next;
6322 if (s2)
6323 return 0;
6324 return 1;
6327 /* return true if type1 and type2 are the same. If unqualified is
6328 true, qualifiers on the types are ignored.
6330 - enums are not checked as gcc __builtin_types_compatible_p ()
6332 static int compare_types(CType *type1, CType *type2, int unqualified)
6334 int bt1, t1, t2;
6336 t1 = type1->t & VT_TYPE;
6337 t2 = type2->t & VT_TYPE;
6338 if (unqualified) {
6339 /* strip qualifiers before comparing */
6340 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6341 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6343 /* XXX: bitfields ? */
6344 if (t1 != t2)
6345 return 0;
6346 /* test more complicated cases */
6347 bt1 = t1 & VT_BTYPE;
6348 if (bt1 == VT_PTR) {
6349 type1 = pointed_type(type1);
6350 type2 = pointed_type(type2);
6351 return is_compatible_types(type1, type2);
6352 } else if (bt1 == VT_STRUCT) {
6353 return (type1->ref == type2->ref);
6354 } else if (bt1 == VT_FUNC) {
6355 return is_compatible_func(type1, type2);
6356 } else {
6357 return 1;
6361 /* return true if type1 and type2 are exactly the same (including
6362 qualifiers).
6364 static int is_compatible_types(CType *type1, CType *type2)
6366 return compare_types(type1,type2,0);
6369 /* return true if type1 and type2 are the same (ignoring qualifiers).
6371 static int is_compatible_parameter_types(CType *type1, CType *type2)
6373 return compare_types(type1,type2,1);
6376 /* print a type. If 'varstr' is not NULL, then the variable is also
6377 printed in the type */
6378 /* XXX: union */
6379 /* XXX: add array and function pointers */
6380 void type_to_str(char *buf, int buf_size,
6381 CType *type, const char *varstr)
6383 int bt, v, t;
6384 Sym *s, *sa;
6385 char buf1[256];
6386 const char *tstr;
6388 t = type->t & VT_TYPE;
6389 bt = t & VT_BTYPE;
6390 buf[0] = '\0';
6391 if (t & VT_CONSTANT)
6392 pstrcat(buf, buf_size, "const ");
6393 if (t & VT_VOLATILE)
6394 pstrcat(buf, buf_size, "volatile ");
6395 if (t & VT_UNSIGNED)
6396 pstrcat(buf, buf_size, "unsigned ");
6397 switch(bt) {
6398 case VT_VOID:
6399 tstr = "void";
6400 goto add_tstr;
6401 case VT_BOOL:
6402 tstr = "_Bool";
6403 goto add_tstr;
6404 case VT_BYTE:
6405 tstr = "char";
6406 goto add_tstr;
6407 case VT_SHORT:
6408 tstr = "short";
6409 goto add_tstr;
6410 case VT_INT:
6411 tstr = "int";
6412 goto add_tstr;
6413 case VT_LONG:
6414 tstr = "long";
6415 goto add_tstr;
6416 case VT_LLONG:
6417 tstr = "long long";
6418 goto add_tstr;
6419 case VT_FLOAT:
6420 tstr = "float";
6421 goto add_tstr;
6422 case VT_DOUBLE:
6423 tstr = "double";
6424 goto add_tstr;
6425 case VT_LDOUBLE:
6426 tstr = "long double";
6427 add_tstr:
6428 pstrcat(buf, buf_size, tstr);
6429 break;
6430 case VT_ENUM:
6431 case VT_STRUCT:
6432 if (bt == VT_STRUCT)
6433 tstr = "struct ";
6434 else
6435 tstr = "enum ";
6436 pstrcat(buf, buf_size, tstr);
6437 v = type->ref->v & ~SYM_STRUCT;
6438 if (v >= SYM_FIRST_ANOM)
6439 pstrcat(buf, buf_size, "<anonymous>");
6440 else
6441 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6442 break;
6443 case VT_FUNC:
6444 s = type->ref;
6445 type_to_str(buf, buf_size, &s->type, varstr);
6446 pstrcat(buf, buf_size, "(");
6447 sa = s->next;
6448 while (sa != NULL) {
6449 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6450 pstrcat(buf, buf_size, buf1);
6451 sa = sa->next;
6452 if (sa)
6453 pstrcat(buf, buf_size, ", ");
6455 pstrcat(buf, buf_size, ")");
6456 goto no_var;
6457 case VT_PTR:
6458 s = type->ref;
6459 pstrcpy(buf1, sizeof(buf1), "*");
6460 if (varstr)
6461 pstrcat(buf1, sizeof(buf1), varstr);
6462 type_to_str(buf, buf_size, &s->type, buf1);
6463 goto no_var;
6465 if (varstr) {
6466 pstrcat(buf, buf_size, " ");
6467 pstrcat(buf, buf_size, varstr);
6469 no_var: ;
6472 /* verify type compatibility to store vtop in 'dt' type, and generate
6473 casts if needed. */
6474 static void gen_assign_cast(CType *dt)
6476 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6477 char buf1[256], buf2[256];
6478 int dbt, sbt;
6480 st = &vtop->type; /* source type */
6481 dbt = dt->t & VT_BTYPE;
6482 sbt = st->t & VT_BTYPE;
6483 if (dt->t & VT_CONSTANT)
6484 warning("assignment of read-only location");
6485 switch(dbt) {
6486 case VT_PTR:
6487 /* special cases for pointers */
6488 /* '0' can also be a pointer */
6489 if (is_null_pointer(vtop))
6490 goto type_ok;
6491 /* accept implicit pointer to integer cast with warning */
6492 if (is_integer_btype(sbt)) {
6493 warning("assignment makes pointer from integer without a cast");
6494 goto type_ok;
6496 type1 = pointed_type(dt);
6497 /* a function is implicitely a function pointer */
6498 if (sbt == VT_FUNC) {
6499 if ((type1->t & VT_BTYPE) != VT_VOID &&
6500 !is_compatible_types(pointed_type(dt), st))
6501 goto error;
6502 else
6503 goto type_ok;
6505 if (sbt != VT_PTR)
6506 goto error;
6507 type2 = pointed_type(st);
6508 if ((type1->t & VT_BTYPE) == VT_VOID ||
6509 (type2->t & VT_BTYPE) == VT_VOID) {
6510 /* void * can match anything */
6511 } else {
6512 /* exact type match, except for unsigned */
6513 tmp_type1 = *type1;
6514 tmp_type2 = *type2;
6515 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6516 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6517 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6518 warning("assignment from incompatible pointer type");
6520 /* check const and volatile */
6521 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6522 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6523 warning("assignment discards qualifiers from pointer target type");
6524 break;
6525 case VT_BYTE:
6526 case VT_SHORT:
6527 case VT_INT:
6528 case VT_LLONG:
6529 if (sbt == VT_PTR || sbt == VT_FUNC) {
6530 warning("assignment makes integer from pointer without a cast");
6532 /* XXX: more tests */
6533 break;
6534 case VT_STRUCT:
6535 tmp_type1 = *dt;
6536 tmp_type2 = *st;
6537 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6538 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6539 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6540 error:
6541 type_to_str(buf1, sizeof(buf1), st, NULL);
6542 type_to_str(buf2, sizeof(buf2), dt, NULL);
6543 error("cannot cast '%s' to '%s'", buf1, buf2);
6545 break;
6547 type_ok:
6548 gen_cast(dt);
6551 /* store vtop in lvalue pushed on stack */
6552 void vstore(void)
6554 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6556 ft = vtop[-1].type.t;
6557 sbt = vtop->type.t & VT_BTYPE;
6558 dbt = ft & VT_BTYPE;
6559 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6560 (sbt == VT_INT && dbt == VT_SHORT)) {
6561 /* optimize char/short casts */
6562 delayed_cast = VT_MUSTCAST;
6563 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
6564 /* XXX: factorize */
6565 if (ft & VT_CONSTANT)
6566 warning("assignment of read-only location");
6567 } else {
6568 delayed_cast = 0;
6569 if (!(ft & VT_BITFIELD))
6570 gen_assign_cast(&vtop[-1].type);
6573 if (sbt == VT_STRUCT) {
6574 /* if structure, only generate pointer */
6575 /* structure assignment : generate memcpy */
6576 /* XXX: optimize if small size */
6577 if (!nocode_wanted) {
6578 size = type_size(&vtop->type, &align);
6580 #ifdef TCC_ARM_EABI
6581 if(!(align & 7))
6582 vpush_global_sym(&func_old_type, TOK_memcpy8);
6583 else if(!(align & 3))
6584 vpush_global_sym(&func_old_type, TOK_memcpy4);
6585 else
6586 #endif
6587 vpush_global_sym(&func_old_type, TOK_memcpy);
6589 /* destination */
6590 vpushv(vtop - 2);
6591 vtop->type.t = VT_INT;
6592 gaddrof();
6593 /* source */
6594 vpushv(vtop - 2);
6595 vtop->type.t = VT_INT;
6596 gaddrof();
6597 /* type size */
6598 vpushi(size);
6599 gfunc_call(3);
6601 vswap();
6602 vpop();
6603 } else {
6604 vswap();
6605 vpop();
6607 /* leave source on stack */
6608 } else if (ft & VT_BITFIELD) {
6609 /* bitfield store handling */
6610 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6611 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6612 /* remove bit field info to avoid loops */
6613 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6615 /* duplicate source into other register */
6616 gv_dup();
6617 vswap();
6618 vrott(3);
6620 if((ft & VT_BTYPE) == VT_BOOL) {
6621 gen_cast(&vtop[-1].type);
6622 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
6625 /* duplicate destination */
6626 vdup();
6627 vtop[-1] = vtop[-2];
6629 /* mask and shift source */
6630 if((ft & VT_BTYPE) != VT_BOOL) {
6631 vpushi((1 << bit_size) - 1);
6632 gen_op('&');
6634 vpushi(bit_pos);
6635 gen_op(TOK_SHL);
6636 /* load destination, mask and or with source */
6637 vswap();
6638 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6639 gen_op('&');
6640 gen_op('|');
6641 /* store result */
6642 vstore();
6644 /* pop off shifted source from "duplicate source..." above */
6645 vpop();
6647 } else {
6648 #ifdef CONFIG_TCC_BCHECK
6649 /* bound check case */
6650 if (vtop[-1].r & VT_MUSTBOUND) {
6651 vswap();
6652 gbound();
6653 vswap();
6655 #endif
6656 if (!nocode_wanted) {
6657 rc = RC_INT;
6658 if (is_float(ft)) {
6659 rc = RC_FLOAT;
6660 #ifdef TCC_TARGET_X86_64
6661 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
6662 rc = RC_ST0;
6664 #endif
6666 r = gv(rc); /* generate value */
6667 /* if lvalue was saved on stack, must read it */
6668 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6669 SValue sv;
6670 t = get_reg(RC_INT);
6671 #ifdef TCC_TARGET_X86_64
6672 sv.type.t = VT_PTR;
6673 #else
6674 sv.type.t = VT_INT;
6675 #endif
6676 sv.r = VT_LOCAL | VT_LVAL;
6677 sv.c.ul = vtop[-1].c.ul;
6678 load(t, &sv);
6679 vtop[-1].r = t | VT_LVAL;
6681 store(r, vtop - 1);
6682 #ifndef TCC_TARGET_X86_64
6683 /* two word case handling : store second register at word + 4 */
6684 if ((ft & VT_BTYPE) == VT_LLONG) {
6685 vswap();
6686 /* convert to int to increment easily */
6687 vtop->type.t = VT_INT;
6688 gaddrof();
6689 vpushi(4);
6690 gen_op('+');
6691 vtop->r |= VT_LVAL;
6692 vswap();
6693 /* XXX: it works because r2 is spilled last ! */
6694 store(vtop->r2, vtop - 1);
6696 #endif
6698 vswap();
6699 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6700 vtop->r |= delayed_cast;
6704 /* post defines POST/PRE add. c is the token ++ or -- */
6705 void inc(int post, int c)
6707 test_lvalue();
6708 vdup(); /* save lvalue */
6709 if (post) {
6710 gv_dup(); /* duplicate value */
6711 vrotb(3);
6712 vrotb(3);
6714 /* add constant */
6715 vpushi(c - TOK_MID);
6716 gen_op('+');
6717 vstore(); /* store value */
6718 if (post)
6719 vpop(); /* if post op, return saved value */
6722 /* Parse GNUC __attribute__ extension. Currently, the following
6723 extensions are recognized:
6724 - aligned(n) : set data/function alignment.
6725 - packed : force data alignment to 1
6726 - section(x) : generate data/code in this section.
6727 - unused : currently ignored, but may be used someday.
6728 - regparm(n) : pass function parameters in registers (i386 only)
6730 static void parse_attribute(AttributeDef *ad)
6732 int t, n;
6734 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6735 next();
6736 skip('(');
6737 skip('(');
6738 while (tok != ')') {
6739 if (tok < TOK_IDENT)
6740 expect("attribute name");
6741 t = tok;
6742 next();
6743 switch(t) {
6744 case TOK_SECTION1:
6745 case TOK_SECTION2:
6746 skip('(');
6747 if (tok != TOK_STR)
6748 expect("section name");
6749 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6750 next();
6751 skip(')');
6752 break;
6753 case TOK_ALIGNED1:
6754 case TOK_ALIGNED2:
6755 if (tok == '(') {
6756 next();
6757 n = expr_const();
6758 if (n <= 0 || (n & (n - 1)) != 0)
6759 error("alignment must be a positive power of two");
6760 skip(')');
6761 } else {
6762 n = MAX_ALIGN;
6764 ad->aligned = n;
6765 break;
6766 case TOK_PACKED1:
6767 case TOK_PACKED2:
6768 ad->packed = 1;
6769 break;
6770 case TOK_UNUSED1:
6771 case TOK_UNUSED2:
6772 /* currently, no need to handle it because tcc does not
6773 track unused objects */
6774 break;
6775 case TOK_NORETURN1:
6776 case TOK_NORETURN2:
6777 /* currently, no need to handle it because tcc does not
6778 track unused objects */
6779 break;
6780 case TOK_CDECL1:
6781 case TOK_CDECL2:
6782 case TOK_CDECL3:
6783 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6784 break;
6785 case TOK_STDCALL1:
6786 case TOK_STDCALL2:
6787 case TOK_STDCALL3:
6788 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6789 break;
6790 #ifdef TCC_TARGET_I386
6791 case TOK_REGPARM1:
6792 case TOK_REGPARM2:
6793 skip('(');
6794 n = expr_const();
6795 if (n > 3)
6796 n = 3;
6797 else if (n < 0)
6798 n = 0;
6799 if (n > 0)
6800 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6801 skip(')');
6802 break;
6803 case TOK_FASTCALL1:
6804 case TOK_FASTCALL2:
6805 case TOK_FASTCALL3:
6806 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6807 break;
6808 #endif
6809 case TOK_DLLEXPORT:
6810 FUNC_EXPORT(ad->func_attr) = 1;
6811 break;
6812 default:
6813 if (tcc_state->warn_unsupported)
6814 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6815 /* skip parameters */
6816 if (tok == '(') {
6817 int parenthesis = 0;
6818 do {
6819 if (tok == '(')
6820 parenthesis++;
6821 else if (tok == ')')
6822 parenthesis--;
6823 next();
6824 } while (parenthesis && tok != -1);
6826 break;
6828 if (tok != ',')
6829 break;
6830 next();
6832 skip(')');
6833 skip(')');
6837 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6838 static void struct_decl(CType *type, int u)
6840 int a, v, size, align, maxalign, c, offset;
6841 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
6842 Sym *s, *ss, *ass, **ps;
6843 AttributeDef ad;
6844 CType type1, btype;
6846 a = tok; /* save decl type */
6847 next();
6848 if (tok != '{') {
6849 v = tok;
6850 next();
6851 /* struct already defined ? return it */
6852 if (v < TOK_IDENT)
6853 expect("struct/union/enum name");
6854 s = struct_find(v);
6855 if (s) {
6856 if (s->type.t != a)
6857 error("invalid type");
6858 goto do_decl;
6860 } else {
6861 v = anon_sym++;
6863 type1.t = a;
6864 /* we put an undefined size for struct/union */
6865 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6866 s->r = 0; /* default alignment is zero as gcc */
6867 /* put struct/union/enum name in type */
6868 do_decl:
6869 type->t = u;
6870 type->ref = s;
6872 if (tok == '{') {
6873 next();
6874 if (s->c != -1)
6875 error("struct/union/enum already defined");
6876 /* cannot be empty */
6877 c = 0;
6878 /* non empty enums are not allowed */
6879 if (a == TOK_ENUM) {
6880 for(;;) {
6881 v = tok;
6882 if (v < TOK_UIDENT)
6883 expect("identifier");
6884 next();
6885 if (tok == '=') {
6886 next();
6887 c = expr_const();
6889 /* enum symbols have static storage */
6890 ss = sym_push(v, &int_type, VT_CONST, c);
6891 ss->type.t |= VT_STATIC;
6892 if (tok != ',')
6893 break;
6894 next();
6895 c++;
6896 /* NOTE: we accept a trailing comma */
6897 if (tok == '}')
6898 break;
6900 skip('}');
6901 } else {
6902 maxalign = 1;
6903 ps = &s->next;
6904 prevbt = VT_INT;
6905 bit_pos = 0;
6906 offset = 0;
6907 while (tok != '}') {
6908 parse_btype(&btype, &ad);
6909 while (1) {
6910 bit_size = -1;
6911 v = 0;
6912 type1 = btype;
6913 if (tok != ':') {
6914 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6915 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6916 expect("identifier");
6917 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6918 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6919 error("invalid type for '%s'",
6920 get_tok_str(v, NULL));
6922 if (tok == ':') {
6923 next();
6924 bit_size = expr_const();
6925 /* XXX: handle v = 0 case for messages */
6926 if (bit_size < 0)
6927 error("negative width in bit-field '%s'",
6928 get_tok_str(v, NULL));
6929 if (v && bit_size == 0)
6930 error("zero width for bit-field '%s'",
6931 get_tok_str(v, NULL));
6933 size = type_size(&type1, &align);
6934 if (ad.aligned) {
6935 if (align < ad.aligned)
6936 align = ad.aligned;
6937 } else if (ad.packed) {
6938 align = 1;
6939 } else if (*tcc_state->pack_stack_ptr) {
6940 if (align > *tcc_state->pack_stack_ptr)
6941 align = *tcc_state->pack_stack_ptr;
6943 lbit_pos = 0;
6944 if (bit_size >= 0) {
6945 bt = type1.t & VT_BTYPE;
6946 if (bt != VT_INT &&
6947 bt != VT_BYTE &&
6948 bt != VT_SHORT &&
6949 bt != VT_BOOL &&
6950 bt != VT_ENUM)
6951 error("bitfields must have scalar type");
6952 bsize = size * 8;
6953 if (bit_size > bsize) {
6954 error("width of '%s' exceeds its type",
6955 get_tok_str(v, NULL));
6956 } else if (bit_size == bsize) {
6957 /* no need for bit fields */
6958 bit_pos = 0;
6959 } else if (bit_size == 0) {
6960 /* XXX: what to do if only padding in a
6961 structure ? */
6962 /* zero size: means to pad */
6963 bit_pos = 0;
6964 } else {
6965 /* we do not have enough room ?
6966 did the type change?
6967 is it a union? */
6968 if ((bit_pos + bit_size) > bsize ||
6969 bt != prevbt || a == TOK_UNION)
6970 bit_pos = 0;
6971 lbit_pos = bit_pos;
6972 /* XXX: handle LSB first */
6973 type1.t |= VT_BITFIELD |
6974 (bit_pos << VT_STRUCT_SHIFT) |
6975 (bit_size << (VT_STRUCT_SHIFT + 6));
6976 bit_pos += bit_size;
6978 prevbt = bt;
6979 } else {
6980 bit_pos = 0;
6982 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
6983 /* add new memory data only if starting
6984 bit field */
6985 if (lbit_pos == 0) {
6986 if (a == TOK_STRUCT) {
6987 c = (c + align - 1) & -align;
6988 offset = c;
6989 if (size > 0)
6990 c += size;
6991 } else {
6992 offset = 0;
6993 if (size > c)
6994 c = size;
6996 if (align > maxalign)
6997 maxalign = align;
6999 #if 0
7000 printf("add field %s offset=%d",
7001 get_tok_str(v, NULL), offset);
7002 if (type1.t & VT_BITFIELD) {
7003 printf(" pos=%d size=%d",
7004 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
7005 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
7007 printf("\n");
7008 #endif
7010 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
7011 ass = type1.ref;
7012 while ((ass = ass->next) != NULL) {
7013 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
7014 *ps = ss;
7015 ps = &ss->next;
7017 } else if (v) {
7018 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
7019 *ps = ss;
7020 ps = &ss->next;
7022 if (tok == ';' || tok == TOK_EOF)
7023 break;
7024 skip(',');
7026 skip(';');
7028 skip('}');
7029 /* store size and alignment */
7030 s->c = (c + maxalign - 1) & -maxalign;
7031 s->r = maxalign;
7036 /* return 0 if no type declaration. otherwise, return the basic type
7037 and skip it.
7039 static int parse_btype(CType *type, AttributeDef *ad)
7041 int t, u, type_found, typespec_found, typedef_found;
7042 Sym *s;
7043 CType type1;
7045 memset(ad, 0, sizeof(AttributeDef));
7046 type_found = 0;
7047 typespec_found = 0;
7048 typedef_found = 0;
7049 t = 0;
7050 while(1) {
7051 switch(tok) {
7052 case TOK_EXTENSION:
7053 /* currently, we really ignore extension */
7054 next();
7055 continue;
7057 /* basic types */
7058 case TOK_CHAR:
7059 u = VT_BYTE;
7060 basic_type:
7061 next();
7062 basic_type1:
7063 if ((t & VT_BTYPE) != 0)
7064 error("too many basic types");
7065 t |= u;
7066 typespec_found = 1;
7067 break;
7068 case TOK_VOID:
7069 u = VT_VOID;
7070 goto basic_type;
7071 case TOK_SHORT:
7072 u = VT_SHORT;
7073 goto basic_type;
7074 case TOK_INT:
7075 next();
7076 typespec_found = 1;
7077 break;
7078 case TOK_LONG:
7079 next();
7080 if ((t & VT_BTYPE) == VT_DOUBLE) {
7081 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
7082 } else if ((t & VT_BTYPE) == VT_LONG) {
7083 t = (t & ~VT_BTYPE) | VT_LLONG;
7084 } else {
7085 u = VT_LONG;
7086 goto basic_type1;
7088 break;
7089 case TOK_BOOL:
7090 u = VT_BOOL;
7091 goto basic_type;
7092 case TOK_FLOAT:
7093 u = VT_FLOAT;
7094 goto basic_type;
7095 case TOK_DOUBLE:
7096 next();
7097 if ((t & VT_BTYPE) == VT_LONG) {
7098 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
7099 } else {
7100 u = VT_DOUBLE;
7101 goto basic_type1;
7103 break;
7104 case TOK_ENUM:
7105 struct_decl(&type1, VT_ENUM);
7106 basic_type2:
7107 u = type1.t;
7108 type->ref = type1.ref;
7109 goto basic_type1;
7110 case TOK_STRUCT:
7111 case TOK_UNION:
7112 struct_decl(&type1, VT_STRUCT);
7113 goto basic_type2;
7115 /* type modifiers */
7116 case TOK_CONST1:
7117 case TOK_CONST2:
7118 case TOK_CONST3:
7119 t |= VT_CONSTANT;
7120 next();
7121 break;
7122 case TOK_VOLATILE1:
7123 case TOK_VOLATILE2:
7124 case TOK_VOLATILE3:
7125 t |= VT_VOLATILE;
7126 next();
7127 break;
7128 case TOK_SIGNED1:
7129 case TOK_SIGNED2:
7130 case TOK_SIGNED3:
7131 typespec_found = 1;
7132 t |= VT_SIGNED;
7133 next();
7134 break;
7135 case TOK_REGISTER:
7136 case TOK_AUTO:
7137 case TOK_RESTRICT1:
7138 case TOK_RESTRICT2:
7139 case TOK_RESTRICT3:
7140 next();
7141 break;
7142 case TOK_UNSIGNED:
7143 t |= VT_UNSIGNED;
7144 next();
7145 typespec_found = 1;
7146 break;
7148 /* storage */
7149 case TOK_EXTERN:
7150 t |= VT_EXTERN;
7151 next();
7152 break;
7153 case TOK_STATIC:
7154 t |= VT_STATIC;
7155 next();
7156 break;
7157 case TOK_TYPEDEF:
7158 t |= VT_TYPEDEF;
7159 next();
7160 break;
7161 case TOK_INLINE1:
7162 case TOK_INLINE2:
7163 case TOK_INLINE3:
7164 t |= VT_INLINE;
7165 next();
7166 break;
7168 /* GNUC attribute */
7169 case TOK_ATTRIBUTE1:
7170 case TOK_ATTRIBUTE2:
7171 parse_attribute(ad);
7172 break;
7173 /* GNUC typeof */
7174 case TOK_TYPEOF1:
7175 case TOK_TYPEOF2:
7176 case TOK_TYPEOF3:
7177 next();
7178 parse_expr_type(&type1);
7179 goto basic_type2;
7180 default:
7181 if (typespec_found || typedef_found)
7182 goto the_end;
7183 s = sym_find(tok);
7184 if (!s || !(s->type.t & VT_TYPEDEF))
7185 goto the_end;
7186 typedef_found = 1;
7187 t |= (s->type.t & ~VT_TYPEDEF);
7188 type->ref = s->type.ref;
7189 next();
7190 typespec_found = 1;
7191 break;
7193 type_found = 1;
7195 the_end:
7196 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7197 error("signed and unsigned modifier");
7198 if (tcc_state->char_is_unsigned) {
7199 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7200 t |= VT_UNSIGNED;
7202 t &= ~VT_SIGNED;
7204 /* long is never used as type */
7205 if ((t & VT_BTYPE) == VT_LONG)
7206 #ifndef TCC_TARGET_X86_64
7207 t = (t & ~VT_BTYPE) | VT_INT;
7208 #else
7209 t = (t & ~VT_BTYPE) | VT_LLONG;
7210 #endif
7211 type->t = t;
7212 return type_found;
7215 /* convert a function parameter type (array to pointer and function to
7216 function pointer) */
7217 static inline void convert_parameter_type(CType *pt)
7219 /* remove const and volatile qualifiers (XXX: const could be used
7220 to indicate a const function parameter */
7221 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7222 /* array must be transformed to pointer according to ANSI C */
7223 pt->t &= ~VT_ARRAY;
7224 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7225 mk_pointer(pt);
7229 static void post_type(CType *type, AttributeDef *ad)
7231 int n, l, t1, arg_size, align;
7232 Sym **plast, *s, *first;
7233 AttributeDef ad1;
7234 CType pt;
7236 if (tok == '(') {
7237 /* function declaration */
7238 next();
7239 l = 0;
7240 first = NULL;
7241 plast = &first;
7242 arg_size = 0;
7243 if (tok != ')') {
7244 for(;;) {
7245 /* read param name and compute offset */
7246 if (l != FUNC_OLD) {
7247 if (!parse_btype(&pt, &ad1)) {
7248 if (l) {
7249 error("invalid type");
7250 } else {
7251 l = FUNC_OLD;
7252 goto old_proto;
7255 l = FUNC_NEW;
7256 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7257 break;
7258 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7259 if ((pt.t & VT_BTYPE) == VT_VOID)
7260 error("parameter declared as void");
7261 arg_size += (type_size(&pt, &align) + 3) & ~3;
7262 } else {
7263 old_proto:
7264 n = tok;
7265 if (n < TOK_UIDENT)
7266 expect("identifier");
7267 pt.t = VT_INT;
7268 next();
7270 convert_parameter_type(&pt);
7271 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7272 *plast = s;
7273 plast = &s->next;
7274 if (tok == ')')
7275 break;
7276 skip(',');
7277 if (l == FUNC_NEW && tok == TOK_DOTS) {
7278 l = FUNC_ELLIPSIS;
7279 next();
7280 break;
7284 /* if no parameters, then old type prototype */
7285 if (l == 0)
7286 l = FUNC_OLD;
7287 skip(')');
7288 t1 = type->t & VT_STORAGE;
7289 /* NOTE: const is ignored in returned type as it has a special
7290 meaning in gcc / C++ */
7291 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7292 post_type(type, ad);
7293 /* we push a anonymous symbol which will contain the function prototype */
7294 FUNC_ARGS(ad->func_attr) = arg_size;
7295 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7296 s->next = first;
7297 type->t = t1 | VT_FUNC;
7298 type->ref = s;
7299 } else if (tok == '[') {
7300 /* array definition */
7301 next();
7302 n = -1;
7303 if (tok != ']') {
7304 n = expr_const();
7305 if (n < 0)
7306 error("invalid array size");
7308 skip(']');
7309 /* parse next post type */
7310 t1 = type->t & VT_STORAGE;
7311 type->t &= ~VT_STORAGE;
7312 post_type(type, ad);
7314 /* we push a anonymous symbol which will contain the array
7315 element type */
7316 s = sym_push(SYM_FIELD, type, 0, n);
7317 type->t = t1 | VT_ARRAY | VT_PTR;
7318 type->ref = s;
7322 /* Parse a type declaration (except basic type), and return the type
7323 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7324 expected. 'type' should contain the basic type. 'ad' is the
7325 attribute definition of the basic type. It can be modified by
7326 type_decl().
7328 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7330 Sym *s;
7331 CType type1, *type2;
7332 int qualifiers;
7334 while (tok == '*') {
7335 qualifiers = 0;
7336 redo:
7337 next();
7338 switch(tok) {
7339 case TOK_CONST1:
7340 case TOK_CONST2:
7341 case TOK_CONST3:
7342 qualifiers |= VT_CONSTANT;
7343 goto redo;
7344 case TOK_VOLATILE1:
7345 case TOK_VOLATILE2:
7346 case TOK_VOLATILE3:
7347 qualifiers |= VT_VOLATILE;
7348 goto redo;
7349 case TOK_RESTRICT1:
7350 case TOK_RESTRICT2:
7351 case TOK_RESTRICT3:
7352 goto redo;
7354 mk_pointer(type);
7355 type->t |= qualifiers;
7358 /* XXX: clarify attribute handling */
7359 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7360 parse_attribute(ad);
7362 /* recursive type */
7363 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7364 type1.t = 0; /* XXX: same as int */
7365 if (tok == '(') {
7366 next();
7367 /* XXX: this is not correct to modify 'ad' at this point, but
7368 the syntax is not clear */
7369 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7370 parse_attribute(ad);
7371 type_decl(&type1, ad, v, td);
7372 skip(')');
7373 } else {
7374 /* type identifier */
7375 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7376 *v = tok;
7377 next();
7378 } else {
7379 if (!(td & TYPE_ABSTRACT))
7380 expect("identifier");
7381 *v = 0;
7384 post_type(type, ad);
7385 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7386 parse_attribute(ad);
7387 if (!type1.t)
7388 return;
7389 /* append type at the end of type1 */
7390 type2 = &type1;
7391 for(;;) {
7392 s = type2->ref;
7393 type2 = &s->type;
7394 if (!type2->t) {
7395 *type2 = *type;
7396 break;
7399 *type = type1;
7402 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7403 static int lvalue_type(int t)
7405 int bt, r;
7406 r = VT_LVAL;
7407 bt = t & VT_BTYPE;
7408 if (bt == VT_BYTE || bt == VT_BOOL)
7409 r |= VT_LVAL_BYTE;
7410 else if (bt == VT_SHORT)
7411 r |= VT_LVAL_SHORT;
7412 else
7413 return r;
7414 if (t & VT_UNSIGNED)
7415 r |= VT_LVAL_UNSIGNED;
7416 return r;
7419 /* indirection with full error checking and bound check */
7420 static void indir(void)
7422 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7423 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7424 return;
7425 expect("pointer");
7427 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7428 gv(RC_INT);
7429 vtop->type = *pointed_type(&vtop->type);
7430 /* Arrays and functions are never lvalues */
7431 if (!(vtop->type.t & VT_ARRAY)
7432 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7433 vtop->r |= lvalue_type(vtop->type.t);
7434 /* if bound checking, the referenced pointer must be checked */
7435 if (do_bounds_check)
7436 vtop->r |= VT_MUSTBOUND;
7440 /* pass a parameter to a function and do type checking and casting */
7441 static void gfunc_param_typed(Sym *func, Sym *arg)
7443 int func_type;
7444 CType type;
7446 func_type = func->c;
7447 if (func_type == FUNC_OLD ||
7448 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7449 /* default casting : only need to convert float to double */
7450 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7451 type.t = VT_DOUBLE;
7452 gen_cast(&type);
7454 } else if (arg == NULL) {
7455 error("too many arguments to function");
7456 } else {
7457 type = arg->type;
7458 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7459 gen_assign_cast(&type);
7463 /* parse an expression of the form '(type)' or '(expr)' and return its
7464 type */
7465 static void parse_expr_type(CType *type)
7467 int n;
7468 AttributeDef ad;
7470 skip('(');
7471 if (parse_btype(type, &ad)) {
7472 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7473 } else {
7474 expr_type(type);
7476 skip(')');
7479 static void parse_type(CType *type)
7481 AttributeDef ad;
7482 int n;
7484 if (!parse_btype(type, &ad)) {
7485 expect("type");
7487 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7490 static void vpush_tokc(int t)
7492 CType type;
7493 type.t = t;
7494 vsetc(&type, VT_CONST, &tokc);
7497 static void unary(void)
7499 int n, t, align, size, r;
7500 CType type;
7501 Sym *s;
7502 AttributeDef ad;
7504 /* XXX: GCC 2.95.3 does not generate a table although it should be
7505 better here */
7506 tok_next:
7507 switch(tok) {
7508 case TOK_EXTENSION:
7509 next();
7510 goto tok_next;
7511 case TOK_CINT:
7512 case TOK_CCHAR:
7513 case TOK_LCHAR:
7514 vpushi(tokc.i);
7515 next();
7516 break;
7517 case TOK_CUINT:
7518 vpush_tokc(VT_INT | VT_UNSIGNED);
7519 next();
7520 break;
7521 case TOK_CLLONG:
7522 vpush_tokc(VT_LLONG);
7523 next();
7524 break;
7525 case TOK_CULLONG:
7526 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7527 next();
7528 break;
7529 case TOK_CFLOAT:
7530 vpush_tokc(VT_FLOAT);
7531 next();
7532 break;
7533 case TOK_CDOUBLE:
7534 vpush_tokc(VT_DOUBLE);
7535 next();
7536 break;
7537 case TOK_CLDOUBLE:
7538 vpush_tokc(VT_LDOUBLE);
7539 next();
7540 break;
7541 case TOK___FUNCTION__:
7542 if (!gnu_ext)
7543 goto tok_identifier;
7544 /* fall thru */
7545 case TOK___FUNC__:
7547 void *ptr;
7548 int len;
7549 /* special function name identifier */
7550 len = strlen(funcname) + 1;
7551 /* generate char[len] type */
7552 type.t = VT_BYTE;
7553 mk_pointer(&type);
7554 type.t |= VT_ARRAY;
7555 type.ref->c = len;
7556 vpush_ref(&type, data_section, data_section->data_offset, len);
7557 ptr = section_ptr_add(data_section, len);
7558 memcpy(ptr, funcname, len);
7559 next();
7561 break;
7562 case TOK_LSTR:
7563 #ifdef TCC_TARGET_PE
7564 t = VT_SHORT | VT_UNSIGNED;
7565 #else
7566 t = VT_INT;
7567 #endif
7568 goto str_init;
7569 case TOK_STR:
7570 /* string parsing */
7571 t = VT_BYTE;
7572 str_init:
7573 if (tcc_state->warn_write_strings)
7574 t |= VT_CONSTANT;
7575 type.t = t;
7576 mk_pointer(&type);
7577 type.t |= VT_ARRAY;
7578 memset(&ad, 0, sizeof(AttributeDef));
7579 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7580 break;
7581 case '(':
7582 next();
7583 /* cast ? */
7584 if (parse_btype(&type, &ad)) {
7585 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7586 skip(')');
7587 /* check ISOC99 compound literal */
7588 if (tok == '{') {
7589 /* data is allocated locally by default */
7590 if (global_expr)
7591 r = VT_CONST;
7592 else
7593 r = VT_LOCAL;
7594 /* all except arrays are lvalues */
7595 if (!(type.t & VT_ARRAY))
7596 r |= lvalue_type(type.t);
7597 memset(&ad, 0, sizeof(AttributeDef));
7598 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7599 } else {
7600 unary();
7601 gen_cast(&type);
7603 } else if (tok == '{') {
7604 /* save all registers */
7605 save_regs(0);
7606 /* statement expression : we do not accept break/continue
7607 inside as GCC does */
7608 block(NULL, NULL, NULL, NULL, 0, 1);
7609 skip(')');
7610 } else {
7611 gexpr();
7612 skip(')');
7614 break;
7615 case '*':
7616 next();
7617 unary();
7618 indir();
7619 break;
7620 case '&':
7621 next();
7622 unary();
7623 /* functions names must be treated as function pointers,
7624 except for unary '&' and sizeof. Since we consider that
7625 functions are not lvalues, we only have to handle it
7626 there and in function calls. */
7627 /* arrays can also be used although they are not lvalues */
7628 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7629 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7630 test_lvalue();
7631 mk_pointer(&vtop->type);
7632 gaddrof();
7633 break;
7634 case '!':
7635 next();
7636 unary();
7637 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
7638 CType boolean;
7639 boolean.t = VT_BOOL;
7640 gen_cast(&boolean);
7641 vtop->c.i = !vtop->c.i;
7642 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
7643 vtop->c.i = vtop->c.i ^ 1;
7644 else {
7645 save_regs(1);
7646 vseti(VT_JMP, gtst(1, 0));
7648 break;
7649 case '~':
7650 next();
7651 unary();
7652 vpushi(-1);
7653 gen_op('^');
7654 break;
7655 case '+':
7656 next();
7657 /* in order to force cast, we add zero */
7658 unary();
7659 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7660 error("pointer not accepted for unary plus");
7661 vpushi(0);
7662 gen_op('+');
7663 break;
7664 case TOK_SIZEOF:
7665 case TOK_ALIGNOF1:
7666 case TOK_ALIGNOF2:
7667 t = tok;
7668 next();
7669 if (tok == '(') {
7670 parse_expr_type(&type);
7671 } else {
7672 unary_type(&type);
7674 size = type_size(&type, &align);
7675 if (t == TOK_SIZEOF) {
7676 if (size < 0)
7677 error("sizeof applied to an incomplete type");
7678 vpushi(size);
7679 } else {
7680 vpushi(align);
7682 vtop->type.t |= VT_UNSIGNED;
7683 break;
7685 case TOK_builtin_types_compatible_p:
7687 CType type1, type2;
7688 next();
7689 skip('(');
7690 parse_type(&type1);
7691 skip(',');
7692 parse_type(&type2);
7693 skip(')');
7694 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7695 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7696 vpushi(is_compatible_types(&type1, &type2));
7698 break;
7699 case TOK_builtin_constant_p:
7701 int saved_nocode_wanted, res;
7702 next();
7703 skip('(');
7704 saved_nocode_wanted = nocode_wanted;
7705 nocode_wanted = 1;
7706 gexpr();
7707 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7708 vpop();
7709 nocode_wanted = saved_nocode_wanted;
7710 skip(')');
7711 vpushi(res);
7713 break;
7714 case TOK_builtin_frame_address:
7716 CType type;
7717 next();
7718 skip('(');
7719 if (tok != TOK_CINT) {
7720 error("__builtin_frame_address only takes integers");
7722 if (tokc.i != 0) {
7723 error("TCC only supports __builtin_frame_address(0)");
7725 next();
7726 skip(')');
7727 type.t = VT_VOID;
7728 mk_pointer(&type);
7729 vset(&type, VT_LOCAL, 0);
7731 break;
7732 case TOK_INC:
7733 case TOK_DEC:
7734 t = tok;
7735 next();
7736 unary();
7737 inc(0, t);
7738 break;
7739 case '-':
7740 next();
7741 vpushi(0);
7742 unary();
7743 gen_op('-');
7744 break;
7745 case TOK_LAND:
7746 if (!gnu_ext)
7747 goto tok_identifier;
7748 next();
7749 /* allow to take the address of a label */
7750 if (tok < TOK_UIDENT)
7751 expect("label identifier");
7752 s = label_find(tok);
7753 if (!s) {
7754 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7755 } else {
7756 if (s->r == LABEL_DECLARED)
7757 s->r = LABEL_FORWARD;
7759 if (!s->type.t) {
7760 s->type.t = VT_VOID;
7761 mk_pointer(&s->type);
7762 s->type.t |= VT_STATIC;
7764 vset(&s->type, VT_CONST | VT_SYM, 0);
7765 vtop->sym = s;
7766 next();
7767 break;
7768 default:
7769 tok_identifier:
7770 t = tok;
7771 next();
7772 if (t < TOK_UIDENT)
7773 expect("identifier");
7774 s = sym_find(t);
7775 if (!s) {
7776 if (tok != '(')
7777 error("'%s' undeclared", get_tok_str(t, NULL));
7778 /* for simple function calls, we tolerate undeclared
7779 external reference to int() function */
7780 if (tcc_state->warn_implicit_function_declaration)
7781 warning("implicit declaration of function '%s'",
7782 get_tok_str(t, NULL));
7783 s = external_global_sym(t, &func_old_type, 0);
7785 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7786 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7787 /* if referencing an inline function, then we generate a
7788 symbol to it if not already done. It will have the
7789 effect to generate code for it at the end of the
7790 compilation unit. Inline function as always
7791 generated in the text section. */
7792 if (!s->c)
7793 put_extern_sym(s, text_section, 0, 0);
7794 r = VT_SYM | VT_CONST;
7795 } else {
7796 r = s->r;
7798 vset(&s->type, r, s->c);
7799 /* if forward reference, we must point to s */
7800 if (vtop->r & VT_SYM) {
7801 vtop->sym = s;
7802 vtop->c.ul = 0;
7804 break;
7807 /* post operations */
7808 while (1) {
7809 if (tok == TOK_INC || tok == TOK_DEC) {
7810 inc(1, tok);
7811 next();
7812 } else if (tok == '.' || tok == TOK_ARROW) {
7813 /* field */
7814 if (tok == TOK_ARROW)
7815 indir();
7816 test_lvalue();
7817 gaddrof();
7818 next();
7819 /* expect pointer on structure */
7820 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7821 expect("struct or union");
7822 s = vtop->type.ref;
7823 /* find field */
7824 tok |= SYM_FIELD;
7825 while ((s = s->next) != NULL) {
7826 if (s->v == tok)
7827 break;
7829 if (!s)
7830 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7831 /* add field offset to pointer */
7832 vtop->type = char_pointer_type; /* change type to 'char *' */
7833 vpushi(s->c);
7834 gen_op('+');
7835 /* change type to field type, and set to lvalue */
7836 vtop->type = s->type;
7837 /* an array is never an lvalue */
7838 if (!(vtop->type.t & VT_ARRAY)) {
7839 vtop->r |= lvalue_type(vtop->type.t);
7840 /* if bound checking, the referenced pointer must be checked */
7841 if (do_bounds_check)
7842 vtop->r |= VT_MUSTBOUND;
7844 next();
7845 } else if (tok == '[') {
7846 next();
7847 gexpr();
7848 gen_op('+');
7849 indir();
7850 skip(']');
7851 } else if (tok == '(') {
7852 SValue ret;
7853 Sym *sa;
7854 int nb_args;
7856 /* function call */
7857 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7858 /* pointer test (no array accepted) */
7859 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7860 vtop->type = *pointed_type(&vtop->type);
7861 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7862 goto error_func;
7863 } else {
7864 error_func:
7865 expect("function pointer");
7867 } else {
7868 vtop->r &= ~VT_LVAL; /* no lvalue */
7870 /* get return type */
7871 s = vtop->type.ref;
7872 next();
7873 sa = s->next; /* first parameter */
7874 nb_args = 0;
7875 ret.r2 = VT_CONST;
7876 /* compute first implicit argument if a structure is returned */
7877 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7878 /* get some space for the returned structure */
7879 size = type_size(&s->type, &align);
7880 loc = (loc - size) & -align;
7881 ret.type = s->type;
7882 ret.r = VT_LOCAL | VT_LVAL;
7883 /* pass it as 'int' to avoid structure arg passing
7884 problems */
7885 vseti(VT_LOCAL, loc);
7886 ret.c = vtop->c;
7887 nb_args++;
7888 } else {
7889 ret.type = s->type;
7890 /* return in register */
7891 if (is_float(ret.type.t)) {
7892 ret.r = REG_FRET;
7893 } else {
7894 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7895 ret.r2 = REG_LRET;
7896 ret.r = REG_IRET;
7898 ret.c.i = 0;
7900 if (tok != ')') {
7901 for(;;) {
7902 expr_eq();
7903 gfunc_param_typed(s, sa);
7904 nb_args++;
7905 if (sa)
7906 sa = sa->next;
7907 if (tok == ')')
7908 break;
7909 skip(',');
7912 if (sa)
7913 error("too few arguments to function");
7914 skip(')');
7915 if (!nocode_wanted) {
7916 gfunc_call(nb_args);
7917 } else {
7918 vtop -= (nb_args + 1);
7920 /* return value */
7921 vsetc(&ret.type, ret.r, &ret.c);
7922 vtop->r2 = ret.r2;
7923 } else {
7924 break;
7929 static void uneq(void)
7931 int t;
7933 unary();
7934 if (tok == '=' ||
7935 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7936 tok == TOK_A_XOR || tok == TOK_A_OR ||
7937 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7938 test_lvalue();
7939 t = tok;
7940 next();
7941 if (t == '=') {
7942 expr_eq();
7943 } else {
7944 vdup();
7945 expr_eq();
7946 gen_op(t & 0x7f);
7948 vstore();
7952 static void expr_prod(void)
7954 int t;
7956 uneq();
7957 while (tok == '*' || tok == '/' || tok == '%') {
7958 t = tok;
7959 next();
7960 uneq();
7961 gen_op(t);
7965 static void expr_sum(void)
7967 int t;
7969 expr_prod();
7970 while (tok == '+' || tok == '-') {
7971 t = tok;
7972 next();
7973 expr_prod();
7974 gen_op(t);
7978 static void expr_shift(void)
7980 int t;
7982 expr_sum();
7983 while (tok == TOK_SHL || tok == TOK_SAR) {
7984 t = tok;
7985 next();
7986 expr_sum();
7987 gen_op(t);
7991 static void expr_cmp(void)
7993 int t;
7995 expr_shift();
7996 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7997 tok == TOK_ULT || tok == TOK_UGE) {
7998 t = tok;
7999 next();
8000 expr_shift();
8001 gen_op(t);
8005 static void expr_cmpeq(void)
8007 int t;
8009 expr_cmp();
8010 while (tok == TOK_EQ || tok == TOK_NE) {
8011 t = tok;
8012 next();
8013 expr_cmp();
8014 gen_op(t);
8018 static void expr_and(void)
8020 expr_cmpeq();
8021 while (tok == '&') {
8022 next();
8023 expr_cmpeq();
8024 gen_op('&');
8028 static void expr_xor(void)
8030 expr_and();
8031 while (tok == '^') {
8032 next();
8033 expr_and();
8034 gen_op('^');
8038 static void expr_or(void)
8040 expr_xor();
8041 while (tok == '|') {
8042 next();
8043 expr_xor();
8044 gen_op('|');
8048 /* XXX: fix this mess */
8049 static void expr_land_const(void)
8051 expr_or();
8052 while (tok == TOK_LAND) {
8053 next();
8054 expr_or();
8055 gen_op(TOK_LAND);
8059 /* XXX: fix this mess */
8060 static void expr_lor_const(void)
8062 expr_land_const();
8063 while (tok == TOK_LOR) {
8064 next();
8065 expr_land_const();
8066 gen_op(TOK_LOR);
8070 /* only used if non constant */
8071 static void expr_land(void)
8073 int t;
8075 expr_or();
8076 if (tok == TOK_LAND) {
8077 t = 0;
8078 save_regs(1);
8079 for(;;) {
8080 t = gtst(1, t);
8081 if (tok != TOK_LAND) {
8082 vseti(VT_JMPI, t);
8083 break;
8085 next();
8086 expr_or();
8091 static void expr_lor(void)
8093 int t;
8095 expr_land();
8096 if (tok == TOK_LOR) {
8097 t = 0;
8098 save_regs(1);
8099 for(;;) {
8100 t = gtst(0, t);
8101 if (tok != TOK_LOR) {
8102 vseti(VT_JMP, t);
8103 break;
8105 next();
8106 expr_land();
8111 /* XXX: better constant handling */
8112 static void expr_eq(void)
8114 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
8115 SValue sv;
8116 CType type, type1, type2;
8118 if (const_wanted) {
8119 expr_lor_const();
8120 if (tok == '?') {
8121 CType boolean;
8122 int c;
8123 boolean.t = VT_BOOL;
8124 vdup();
8125 gen_cast(&boolean);
8126 c = vtop->c.i;
8127 vpop();
8128 next();
8129 if (tok != ':' || !gnu_ext) {
8130 vpop();
8131 gexpr();
8133 if (!c)
8134 vpop();
8135 skip(':');
8136 expr_eq();
8137 if (c)
8138 vpop();
8140 } else {
8141 expr_lor();
8142 if (tok == '?') {
8143 next();
8144 if (vtop != vstack) {
8145 /* needed to avoid having different registers saved in
8146 each branch */
8147 if (is_float(vtop->type.t)) {
8148 rc = RC_FLOAT;
8149 #ifdef TCC_TARGET_X86_64
8150 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
8151 rc = RC_ST0;
8153 #endif
8155 else
8156 rc = RC_INT;
8157 gv(rc);
8158 save_regs(1);
8160 if (tok == ':' && gnu_ext) {
8161 gv_dup();
8162 tt = gtst(1, 0);
8163 } else {
8164 tt = gtst(1, 0);
8165 gexpr();
8167 type1 = vtop->type;
8168 sv = *vtop; /* save value to handle it later */
8169 vtop--; /* no vpop so that FP stack is not flushed */
8170 skip(':');
8171 u = gjmp(0);
8172 gsym(tt);
8173 expr_eq();
8174 type2 = vtop->type;
8176 t1 = type1.t;
8177 bt1 = t1 & VT_BTYPE;
8178 t2 = type2.t;
8179 bt2 = t2 & VT_BTYPE;
8180 /* cast operands to correct type according to ISOC rules */
8181 if (is_float(bt1) || is_float(bt2)) {
8182 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8183 type.t = VT_LDOUBLE;
8184 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8185 type.t = VT_DOUBLE;
8186 } else {
8187 type.t = VT_FLOAT;
8189 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8190 /* cast to biggest op */
8191 type.t = VT_LLONG;
8192 /* convert to unsigned if it does not fit in a long long */
8193 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8194 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8195 type.t |= VT_UNSIGNED;
8196 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8197 /* XXX: test pointer compatibility */
8198 type = type1;
8199 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8200 /* XXX: test function pointer compatibility */
8201 type = type1;
8202 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8203 /* XXX: test structure compatibility */
8204 type = type1;
8205 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8206 /* NOTE: as an extension, we accept void on only one side */
8207 type.t = VT_VOID;
8208 } else {
8209 /* integer operations */
8210 type.t = VT_INT;
8211 /* convert to unsigned if it does not fit in an integer */
8212 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8213 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8214 type.t |= VT_UNSIGNED;
8217 /* now we convert second operand */
8218 gen_cast(&type);
8219 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8220 gaddrof();
8221 rc = RC_INT;
8222 if (is_float(type.t)) {
8223 rc = RC_FLOAT;
8224 #ifdef TCC_TARGET_X86_64
8225 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
8226 rc = RC_ST0;
8228 #endif
8229 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8230 /* for long longs, we use fixed registers to avoid having
8231 to handle a complicated move */
8232 rc = RC_IRET;
8235 r2 = gv(rc);
8236 /* this is horrible, but we must also convert first
8237 operand */
8238 tt = gjmp(0);
8239 gsym(u);
8240 /* put again first value and cast it */
8241 *vtop = sv;
8242 gen_cast(&type);
8243 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8244 gaddrof();
8245 r1 = gv(rc);
8246 move_reg(r2, r1);
8247 vtop->r = r2;
8248 gsym(tt);
8253 static void gexpr(void)
8255 while (1) {
8256 expr_eq();
8257 if (tok != ',')
8258 break;
8259 vpop();
8260 next();
8264 /* parse an expression and return its type without any side effect. */
8265 static void expr_type(CType *type)
8267 int saved_nocode_wanted;
8269 saved_nocode_wanted = nocode_wanted;
8270 nocode_wanted = 1;
8271 gexpr();
8272 *type = vtop->type;
8273 vpop();
8274 nocode_wanted = saved_nocode_wanted;
8277 /* parse a unary expression and return its type without any side
8278 effect. */
8279 static void unary_type(CType *type)
8281 int a;
8283 a = nocode_wanted;
8284 nocode_wanted = 1;
8285 unary();
8286 *type = vtop->type;
8287 vpop();
8288 nocode_wanted = a;
8291 /* parse a constant expression and return value in vtop. */
8292 static void expr_const1(void)
8294 int a;
8295 a = const_wanted;
8296 const_wanted = 1;
8297 expr_eq();
8298 const_wanted = a;
8301 /* parse an integer constant and return its value. */
8302 static int expr_const(void)
8304 int c;
8305 expr_const1();
8306 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8307 expect("constant expression");
8308 c = vtop->c.i;
8309 vpop();
8310 return c;
8313 /* return the label token if current token is a label, otherwise
8314 return zero */
8315 static int is_label(void)
8317 int last_tok;
8319 /* fast test first */
8320 if (tok < TOK_UIDENT)
8321 return 0;
8322 /* no need to save tokc because tok is an identifier */
8323 last_tok = tok;
8324 next();
8325 if (tok == ':') {
8326 next();
8327 return last_tok;
8328 } else {
8329 unget_tok(last_tok);
8330 return 0;
8334 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8335 int case_reg, int is_expr)
8337 int a, b, c, d;
8338 Sym *s;
8340 /* generate line number info */
8341 if (do_debug &&
8342 (last_line_num != file->line_num || last_ind != ind)) {
8343 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8344 last_ind = ind;
8345 last_line_num = file->line_num;
8348 if (is_expr) {
8349 /* default return value is (void) */
8350 vpushi(0);
8351 vtop->type.t = VT_VOID;
8354 if (tok == TOK_IF) {
8355 /* if test */
8356 next();
8357 skip('(');
8358 gexpr();
8359 skip(')');
8360 a = gtst(1, 0);
8361 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8362 c = tok;
8363 if (c == TOK_ELSE) {
8364 next();
8365 d = gjmp(0);
8366 gsym(a);
8367 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8368 gsym(d); /* patch else jmp */
8369 } else
8370 gsym(a);
8371 } else if (tok == TOK_WHILE) {
8372 next();
8373 d = ind;
8374 skip('(');
8375 gexpr();
8376 skip(')');
8377 a = gtst(1, 0);
8378 b = 0;
8379 block(&a, &b, case_sym, def_sym, case_reg, 0);
8380 gjmp_addr(d);
8381 gsym(a);
8382 gsym_addr(b, d);
8383 } else if (tok == '{') {
8384 Sym *llabel;
8386 next();
8387 /* record local declaration stack position */
8388 s = local_stack;
8389 llabel = local_label_stack;
8390 /* handle local labels declarations */
8391 if (tok == TOK_LABEL) {
8392 next();
8393 for(;;) {
8394 if (tok < TOK_UIDENT)
8395 expect("label identifier");
8396 label_push(&local_label_stack, tok, LABEL_DECLARED);
8397 next();
8398 if (tok == ',') {
8399 next();
8400 } else {
8401 skip(';');
8402 break;
8406 while (tok != '}') {
8407 decl(VT_LOCAL);
8408 if (tok != '}') {
8409 if (is_expr)
8410 vpop();
8411 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8414 /* pop locally defined labels */
8415 label_pop(&local_label_stack, llabel);
8416 /* pop locally defined symbols */
8417 if(is_expr) {
8418 /* XXX: this solution makes only valgrind happy...
8419 triggered by gcc.c-torture/execute/20000917-1.c */
8420 Sym *p;
8421 switch(vtop->type.t & VT_BTYPE) {
8422 case VT_PTR:
8423 case VT_STRUCT:
8424 case VT_ENUM:
8425 case VT_FUNC:
8426 for(p=vtop->type.ref;p;p=p->prev)
8427 if(p->prev==s)
8428 error("unsupported expression type");
8431 sym_pop(&local_stack, s);
8432 next();
8433 } else if (tok == TOK_RETURN) {
8434 next();
8435 if (tok != ';') {
8436 gexpr();
8437 gen_assign_cast(&func_vt);
8438 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8439 CType type;
8440 /* if returning structure, must copy it to implicit
8441 first pointer arg location */
8442 #ifdef TCC_ARM_EABI
8443 int align, size;
8444 size = type_size(&func_vt,&align);
8445 if(size <= 4)
8447 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8448 && (align & 3))
8450 int addr;
8451 loc = (loc - size) & -4;
8452 addr = loc;
8453 type = func_vt;
8454 vset(&type, VT_LOCAL | VT_LVAL, addr);
8455 vswap();
8456 vstore();
8457 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8459 vtop->type = int_type;
8460 gv(RC_IRET);
8461 } else {
8462 #endif
8463 type = func_vt;
8464 mk_pointer(&type);
8465 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8466 indir();
8467 vswap();
8468 /* copy structure value to pointer */
8469 vstore();
8470 #ifdef TCC_ARM_EABI
8472 #endif
8473 } else if (is_float(func_vt.t)) {
8474 gv(RC_FRET);
8475 } else {
8476 gv(RC_IRET);
8478 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8480 skip(';');
8481 rsym = gjmp(rsym); /* jmp */
8482 } else if (tok == TOK_BREAK) {
8483 /* compute jump */
8484 if (!bsym)
8485 error("cannot break");
8486 *bsym = gjmp(*bsym);
8487 next();
8488 skip(';');
8489 } else if (tok == TOK_CONTINUE) {
8490 /* compute jump */
8491 if (!csym)
8492 error("cannot continue");
8493 *csym = gjmp(*csym);
8494 next();
8495 skip(';');
8496 } else if (tok == TOK_FOR) {
8497 int e;
8498 next();
8499 skip('(');
8500 if (tok != ';') {
8501 gexpr();
8502 vpop();
8504 skip(';');
8505 d = ind;
8506 c = ind;
8507 a = 0;
8508 b = 0;
8509 if (tok != ';') {
8510 gexpr();
8511 a = gtst(1, 0);
8513 skip(';');
8514 if (tok != ')') {
8515 e = gjmp(0);
8516 c = ind;
8517 gexpr();
8518 vpop();
8519 gjmp_addr(d);
8520 gsym(e);
8522 skip(')');
8523 block(&a, &b, case_sym, def_sym, case_reg, 0);
8524 gjmp_addr(c);
8525 gsym(a);
8526 gsym_addr(b, c);
8527 } else
8528 if (tok == TOK_DO) {
8529 next();
8530 a = 0;
8531 b = 0;
8532 d = ind;
8533 block(&a, &b, case_sym, def_sym, case_reg, 0);
8534 skip(TOK_WHILE);
8535 skip('(');
8536 gsym(b);
8537 gexpr();
8538 c = gtst(0, 0);
8539 gsym_addr(c, d);
8540 skip(')');
8541 gsym(a);
8542 skip(';');
8543 } else
8544 if (tok == TOK_SWITCH) {
8545 next();
8546 skip('(');
8547 gexpr();
8548 /* XXX: other types than integer */
8549 case_reg = gv(RC_INT);
8550 vpop();
8551 skip(')');
8552 a = 0;
8553 b = gjmp(0); /* jump to first case */
8554 c = 0;
8555 block(&a, csym, &b, &c, case_reg, 0);
8556 /* if no default, jmp after switch */
8557 if (c == 0)
8558 c = ind;
8559 /* default label */
8560 gsym_addr(b, c);
8561 /* break label */
8562 gsym(a);
8563 } else
8564 if (tok == TOK_CASE) {
8565 int v1, v2;
8566 if (!case_sym)
8567 expect("switch");
8568 next();
8569 v1 = expr_const();
8570 v2 = v1;
8571 if (gnu_ext && tok == TOK_DOTS) {
8572 next();
8573 v2 = expr_const();
8574 if (v2 < v1)
8575 warning("empty case range");
8577 /* since a case is like a label, we must skip it with a jmp */
8578 b = gjmp(0);
8579 gsym(*case_sym);
8580 vseti(case_reg, 0);
8581 vpushi(v1);
8582 if (v1 == v2) {
8583 gen_op(TOK_EQ);
8584 *case_sym = gtst(1, 0);
8585 } else {
8586 gen_op(TOK_GE);
8587 *case_sym = gtst(1, 0);
8588 vseti(case_reg, 0);
8589 vpushi(v2);
8590 gen_op(TOK_LE);
8591 *case_sym = gtst(1, *case_sym);
8593 gsym(b);
8594 skip(':');
8595 is_expr = 0;
8596 goto block_after_label;
8597 } else
8598 if (tok == TOK_DEFAULT) {
8599 next();
8600 skip(':');
8601 if (!def_sym)
8602 expect("switch");
8603 if (*def_sym)
8604 error("too many 'default'");
8605 *def_sym = ind;
8606 is_expr = 0;
8607 goto block_after_label;
8608 } else
8609 if (tok == TOK_GOTO) {
8610 next();
8611 if (tok == '*' && gnu_ext) {
8612 /* computed goto */
8613 next();
8614 gexpr();
8615 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8616 expect("pointer");
8617 ggoto();
8618 } else if (tok >= TOK_UIDENT) {
8619 s = label_find(tok);
8620 /* put forward definition if needed */
8621 if (!s) {
8622 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8623 } else {
8624 if (s->r == LABEL_DECLARED)
8625 s->r = LABEL_FORWARD;
8627 /* label already defined */
8628 if (s->r & LABEL_FORWARD)
8629 s->next = (void *)gjmp((long)s->next);
8630 else
8631 gjmp_addr((long)s->next);
8632 next();
8633 } else {
8634 expect("label identifier");
8636 skip(';');
8637 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8638 asm_instr();
8639 } else {
8640 b = is_label();
8641 if (b) {
8642 /* label case */
8643 s = label_find(b);
8644 if (s) {
8645 if (s->r == LABEL_DEFINED)
8646 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8647 gsym((long)s->next);
8648 s->r = LABEL_DEFINED;
8649 } else {
8650 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8652 s->next = (void *)ind;
8653 /* we accept this, but it is a mistake */
8654 block_after_label:
8655 if (tok == '}') {
8656 warning("deprecated use of label at end of compound statement");
8657 } else {
8658 if (is_expr)
8659 vpop();
8660 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8662 } else {
8663 /* expression case */
8664 if (tok != ';') {
8665 if (is_expr) {
8666 vpop();
8667 gexpr();
8668 } else {
8669 gexpr();
8670 vpop();
8673 skip(';');
8678 /* t is the array or struct type. c is the array or struct
8679 address. cur_index/cur_field is the pointer to the current
8680 value. 'size_only' is true if only size info is needed (only used
8681 in arrays) */
8682 static void decl_designator(CType *type, Section *sec, unsigned long c,
8683 int *cur_index, Sym **cur_field,
8684 int size_only)
8686 Sym *s, *f;
8687 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8688 CType type1;
8690 notfirst = 0;
8691 elem_size = 0;
8692 nb_elems = 1;
8693 if (gnu_ext && (l = is_label()) != 0)
8694 goto struct_field;
8695 while (tok == '[' || tok == '.') {
8696 if (tok == '[') {
8697 if (!(type->t & VT_ARRAY))
8698 expect("array type");
8699 s = type->ref;
8700 next();
8701 index = expr_const();
8702 if (index < 0 || (s->c >= 0 && index >= s->c))
8703 expect("invalid index");
8704 if (tok == TOK_DOTS && gnu_ext) {
8705 next();
8706 index_last = expr_const();
8707 if (index_last < 0 ||
8708 (s->c >= 0 && index_last >= s->c) ||
8709 index_last < index)
8710 expect("invalid index");
8711 } else {
8712 index_last = index;
8714 skip(']');
8715 if (!notfirst)
8716 *cur_index = index_last;
8717 type = pointed_type(type);
8718 elem_size = type_size(type, &align);
8719 c += index * elem_size;
8720 /* NOTE: we only support ranges for last designator */
8721 nb_elems = index_last - index + 1;
8722 if (nb_elems != 1) {
8723 notfirst = 1;
8724 break;
8726 } else {
8727 next();
8728 l = tok;
8729 next();
8730 struct_field:
8731 if ((type->t & VT_BTYPE) != VT_STRUCT)
8732 expect("struct/union type");
8733 s = type->ref;
8734 l |= SYM_FIELD;
8735 f = s->next;
8736 while (f) {
8737 if (f->v == l)
8738 break;
8739 f = f->next;
8741 if (!f)
8742 expect("field");
8743 if (!notfirst)
8744 *cur_field = f;
8745 /* XXX: fix this mess by using explicit storage field */
8746 type1 = f->type;
8747 type1.t |= (type->t & ~VT_TYPE);
8748 type = &type1;
8749 c += f->c;
8751 notfirst = 1;
8753 if (notfirst) {
8754 if (tok == '=') {
8755 next();
8756 } else {
8757 if (!gnu_ext)
8758 expect("=");
8760 } else {
8761 if (type->t & VT_ARRAY) {
8762 index = *cur_index;
8763 type = pointed_type(type);
8764 c += index * type_size(type, &align);
8765 } else {
8766 f = *cur_field;
8767 if (!f)
8768 error("too many field init");
8769 /* XXX: fix this mess by using explicit storage field */
8770 type1 = f->type;
8771 type1.t |= (type->t & ~VT_TYPE);
8772 type = &type1;
8773 c += f->c;
8776 decl_initializer(type, sec, c, 0, size_only);
8778 /* XXX: make it more general */
8779 if (!size_only && nb_elems > 1) {
8780 unsigned long c_end;
8781 uint8_t *src, *dst;
8782 int i;
8784 if (!sec)
8785 error("range init not supported yet for dynamic storage");
8786 c_end = c + nb_elems * elem_size;
8787 if (c_end > sec->data_allocated)
8788 section_realloc(sec, c_end);
8789 src = sec->data + c;
8790 dst = src;
8791 for(i = 1; i < nb_elems; i++) {
8792 dst += elem_size;
8793 memcpy(dst, src, elem_size);
8798 #define EXPR_VAL 0
8799 #define EXPR_CONST 1
8800 #define EXPR_ANY 2
8802 /* store a value or an expression directly in global data or in local array */
8803 static void init_putv(CType *type, Section *sec, unsigned long c,
8804 int v, int expr_type)
8806 int saved_global_expr, bt, bit_pos, bit_size;
8807 void *ptr;
8808 unsigned long long bit_mask;
8809 CType dtype;
8811 switch(expr_type) {
8812 case EXPR_VAL:
8813 vpushi(v);
8814 break;
8815 case EXPR_CONST:
8816 /* compound literals must be allocated globally in this case */
8817 saved_global_expr = global_expr;
8818 global_expr = 1;
8819 expr_const1();
8820 global_expr = saved_global_expr;
8821 /* NOTE: symbols are accepted */
8822 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8823 error("initializer element is not constant");
8824 break;
8825 case EXPR_ANY:
8826 expr_eq();
8827 break;
8830 dtype = *type;
8831 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8833 if (sec) {
8834 /* XXX: not portable */
8835 /* XXX: generate error if incorrect relocation */
8836 gen_assign_cast(&dtype);
8837 bt = type->t & VT_BTYPE;
8838 /* we'll write at most 12 bytes */
8839 if (c + 12 > sec->data_allocated) {
8840 section_realloc(sec, c + 12);
8842 ptr = sec->data + c;
8843 /* XXX: make code faster ? */
8844 if (!(type->t & VT_BITFIELD)) {
8845 bit_pos = 0;
8846 bit_size = 32;
8847 bit_mask = -1LL;
8848 } else {
8849 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8850 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8851 bit_mask = (1LL << bit_size) - 1;
8853 if ((vtop->r & VT_SYM) &&
8854 (bt == VT_BYTE ||
8855 bt == VT_SHORT ||
8856 bt == VT_DOUBLE ||
8857 bt == VT_LDOUBLE ||
8858 bt == VT_LLONG ||
8859 (bt == VT_INT && bit_size != 32)))
8860 error("initializer element is not computable at load time");
8861 switch(bt) {
8862 case VT_BOOL:
8863 vtop->c.i = (vtop->c.i != 0);
8864 case VT_BYTE:
8865 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8866 break;
8867 case VT_SHORT:
8868 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8869 break;
8870 case VT_DOUBLE:
8871 *(double *)ptr = vtop->c.d;
8872 break;
8873 case VT_LDOUBLE:
8874 *(long double *)ptr = vtop->c.ld;
8875 break;
8876 case VT_LLONG:
8877 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8878 break;
8879 default:
8880 if (vtop->r & VT_SYM) {
8881 greloc(sec, vtop->sym, c, R_DATA_32);
8883 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8884 break;
8886 vtop--;
8887 } else {
8888 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8889 vswap();
8890 vstore();
8891 vpop();
8895 /* put zeros for variable based init */
8896 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8898 if (sec) {
8899 /* nothing to do because globals are already set to zero */
8900 } else {
8901 vpush_global_sym(&func_old_type, TOK_memset);
8902 vseti(VT_LOCAL, c);
8903 vpushi(0);
8904 vpushi(size);
8905 gfunc_call(3);
8909 /* 't' contains the type and storage info. 'c' is the offset of the
8910 object in section 'sec'. If 'sec' is NULL, it means stack based
8911 allocation. 'first' is true if array '{' must be read (multi
8912 dimension implicit array init handling). 'size_only' is true if
8913 size only evaluation is wanted (only for arrays). */
8914 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8915 int first, int size_only)
8917 int index, array_length, n, no_oblock, nb, parlevel, i;
8918 int size1, align1, expr_type;
8919 Sym *s, *f;
8920 CType *t1;
8922 if (type->t & VT_ARRAY) {
8923 s = type->ref;
8924 n = s->c;
8925 array_length = 0;
8926 t1 = pointed_type(type);
8927 size1 = type_size(t1, &align1);
8929 no_oblock = 1;
8930 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8931 tok == '{') {
8932 skip('{');
8933 no_oblock = 0;
8936 /* only parse strings here if correct type (otherwise: handle
8937 them as ((w)char *) expressions */
8938 if ((tok == TOK_LSTR &&
8939 #ifdef TCC_TARGET_PE
8940 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8941 #else
8942 (t1->t & VT_BTYPE) == VT_INT
8943 #endif
8944 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8945 while (tok == TOK_STR || tok == TOK_LSTR) {
8946 int cstr_len, ch;
8947 CString *cstr;
8949 cstr = tokc.cstr;
8950 /* compute maximum number of chars wanted */
8951 if (tok == TOK_STR)
8952 cstr_len = cstr->size;
8953 else
8954 cstr_len = cstr->size / sizeof(nwchar_t);
8955 cstr_len--;
8956 nb = cstr_len;
8957 if (n >= 0 && nb > (n - array_length))
8958 nb = n - array_length;
8959 if (!size_only) {
8960 if (cstr_len > nb)
8961 warning("initializer-string for array is too long");
8962 /* in order to go faster for common case (char
8963 string in global variable, we handle it
8964 specifically */
8965 if (sec && tok == TOK_STR && size1 == 1) {
8966 memcpy(sec->data + c + array_length, cstr->data, nb);
8967 } else {
8968 for(i=0;i<nb;i++) {
8969 if (tok == TOK_STR)
8970 ch = ((unsigned char *)cstr->data)[i];
8971 else
8972 ch = ((nwchar_t *)cstr->data)[i];
8973 init_putv(t1, sec, c + (array_length + i) * size1,
8974 ch, EXPR_VAL);
8978 array_length += nb;
8979 next();
8981 /* only add trailing zero if enough storage (no
8982 warning in this case since it is standard) */
8983 if (n < 0 || array_length < n) {
8984 if (!size_only) {
8985 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8987 array_length++;
8989 } else {
8990 index = 0;
8991 while (tok != '}') {
8992 decl_designator(type, sec, c, &index, NULL, size_only);
8993 if (n >= 0 && index >= n)
8994 error("index too large");
8995 /* must put zero in holes (note that doing it that way
8996 ensures that it even works with designators) */
8997 if (!size_only && array_length < index) {
8998 init_putz(t1, sec, c + array_length * size1,
8999 (index - array_length) * size1);
9001 index++;
9002 if (index > array_length)
9003 array_length = index;
9004 /* special test for multi dimensional arrays (may not
9005 be strictly correct if designators are used at the
9006 same time) */
9007 if (index >= n && no_oblock)
9008 break;
9009 if (tok == '}')
9010 break;
9011 skip(',');
9014 if (!no_oblock)
9015 skip('}');
9016 /* put zeros at the end */
9017 if (!size_only && n >= 0 && array_length < n) {
9018 init_putz(t1, sec, c + array_length * size1,
9019 (n - array_length) * size1);
9021 /* patch type size if needed */
9022 if (n < 0)
9023 s->c = array_length;
9024 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
9025 (sec || !first || tok == '{')) {
9026 int par_count;
9028 /* NOTE: the previous test is a specific case for automatic
9029 struct/union init */
9030 /* XXX: union needs only one init */
9032 /* XXX: this test is incorrect for local initializers
9033 beginning with ( without {. It would be much more difficult
9034 to do it correctly (ideally, the expression parser should
9035 be used in all cases) */
9036 par_count = 0;
9037 if (tok == '(') {
9038 AttributeDef ad1;
9039 CType type1;
9040 next();
9041 while (tok == '(') {
9042 par_count++;
9043 next();
9045 if (!parse_btype(&type1, &ad1))
9046 expect("cast");
9047 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
9048 #if 0
9049 if (!is_assignable_types(type, &type1))
9050 error("invalid type for cast");
9051 #endif
9052 skip(')');
9054 no_oblock = 1;
9055 if (first || tok == '{') {
9056 skip('{');
9057 no_oblock = 0;
9059 s = type->ref;
9060 f = s->next;
9061 array_length = 0;
9062 index = 0;
9063 n = s->c;
9064 while (tok != '}') {
9065 decl_designator(type, sec, c, NULL, &f, size_only);
9066 index = f->c;
9067 if (!size_only && array_length < index) {
9068 init_putz(type, sec, c + array_length,
9069 index - array_length);
9071 index = index + type_size(&f->type, &align1);
9072 if (index > array_length)
9073 array_length = index;
9074 f = f->next;
9075 if (no_oblock && f == NULL)
9076 break;
9077 if (tok == '}')
9078 break;
9079 skip(',');
9081 /* put zeros at the end */
9082 if (!size_only && array_length < n) {
9083 init_putz(type, sec, c + array_length,
9084 n - array_length);
9086 if (!no_oblock)
9087 skip('}');
9088 while (par_count) {
9089 skip(')');
9090 par_count--;
9092 } else if (tok == '{') {
9093 next();
9094 decl_initializer(type, sec, c, first, size_only);
9095 skip('}');
9096 } else if (size_only) {
9097 /* just skip expression */
9098 parlevel = 0;
9099 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
9100 tok != -1) {
9101 if (tok == '(')
9102 parlevel++;
9103 else if (tok == ')')
9104 parlevel--;
9105 next();
9107 } else {
9108 /* currently, we always use constant expression for globals
9109 (may change for scripting case) */
9110 expr_type = EXPR_CONST;
9111 if (!sec)
9112 expr_type = EXPR_ANY;
9113 init_putv(type, sec, c, 0, expr_type);
9117 /* parse an initializer for type 't' if 'has_init' is non zero, and
9118 allocate space in local or global data space ('r' is either
9119 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
9120 variable 'v' of scope 'scope' is declared before initializers are
9121 parsed. If 'v' is zero, then a reference to the new object is put
9122 in the value stack. If 'has_init' is 2, a special parsing is done
9123 to handle string constants. */
9124 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
9125 int has_init, int v, int scope)
9127 int size, align, addr, data_offset;
9128 int level;
9129 ParseState saved_parse_state;
9130 TokenString init_str;
9131 Section *sec;
9133 size = type_size(type, &align);
9134 /* If unknown size, we must evaluate it before
9135 evaluating initializers because
9136 initializers can generate global data too
9137 (e.g. string pointers or ISOC99 compound
9138 literals). It also simplifies local
9139 initializers handling */
9140 tok_str_new(&init_str);
9141 if (size < 0) {
9142 if (!has_init)
9143 error("unknown type size");
9144 /* get all init string */
9145 if (has_init == 2) {
9146 /* only get strings */
9147 while (tok == TOK_STR || tok == TOK_LSTR) {
9148 tok_str_add_tok(&init_str);
9149 next();
9151 } else {
9152 level = 0;
9153 while (level > 0 || (tok != ',' && tok != ';')) {
9154 if (tok < 0)
9155 error("unexpected end of file in initializer");
9156 tok_str_add_tok(&init_str);
9157 if (tok == '{')
9158 level++;
9159 else if (tok == '}') {
9160 if (level == 0)
9161 break;
9162 level--;
9164 next();
9167 tok_str_add(&init_str, -1);
9168 tok_str_add(&init_str, 0);
9170 /* compute size */
9171 save_parse_state(&saved_parse_state);
9173 macro_ptr = init_str.str;
9174 next();
9175 decl_initializer(type, NULL, 0, 1, 1);
9176 /* prepare second initializer parsing */
9177 macro_ptr = init_str.str;
9178 next();
9180 /* if still unknown size, error */
9181 size = type_size(type, &align);
9182 if (size < 0)
9183 error("unknown type size");
9185 /* take into account specified alignment if bigger */
9186 if (ad->aligned) {
9187 if (ad->aligned > align)
9188 align = ad->aligned;
9189 } else if (ad->packed) {
9190 align = 1;
9192 if ((r & VT_VALMASK) == VT_LOCAL) {
9193 sec = NULL;
9194 if (do_bounds_check && (type->t & VT_ARRAY))
9195 loc--;
9196 loc = (loc - size) & -align;
9197 addr = loc;
9198 /* handles bounds */
9199 /* XXX: currently, since we do only one pass, we cannot track
9200 '&' operators, so we add only arrays */
9201 if (do_bounds_check && (type->t & VT_ARRAY)) {
9202 unsigned long *bounds_ptr;
9203 /* add padding between regions */
9204 loc--;
9205 /* then add local bound info */
9206 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9207 bounds_ptr[0] = addr;
9208 bounds_ptr[1] = size;
9210 if (v) {
9211 /* local variable */
9212 sym_push(v, type, r, addr);
9213 } else {
9214 /* push local reference */
9215 vset(type, r, addr);
9217 } else {
9218 Sym *sym;
9220 sym = NULL;
9221 if (v && scope == VT_CONST) {
9222 /* see if the symbol was already defined */
9223 sym = sym_find(v);
9224 if (sym) {
9225 if (!is_compatible_types(&sym->type, type))
9226 error("incompatible types for redefinition of '%s'",
9227 get_tok_str(v, NULL));
9228 if (sym->type.t & VT_EXTERN) {
9229 /* if the variable is extern, it was not allocated */
9230 sym->type.t &= ~VT_EXTERN;
9231 /* set array size if it was ommited in extern
9232 declaration */
9233 if ((sym->type.t & VT_ARRAY) &&
9234 sym->type.ref->c < 0 &&
9235 type->ref->c >= 0)
9236 sym->type.ref->c = type->ref->c;
9237 } else {
9238 /* we accept several definitions of the same
9239 global variable. this is tricky, because we
9240 must play with the SHN_COMMON type of the symbol */
9241 /* XXX: should check if the variable was already
9242 initialized. It is incorrect to initialized it
9243 twice */
9244 /* no init data, we won't add more to the symbol */
9245 if (!has_init)
9246 goto no_alloc;
9251 /* allocate symbol in corresponding section */
9252 sec = ad->section;
9253 if (!sec) {
9254 if (has_init)
9255 sec = data_section;
9256 else if (tcc_state->nocommon)
9257 sec = bss_section;
9259 if (sec) {
9260 data_offset = sec->data_offset;
9261 data_offset = (data_offset + align - 1) & -align;
9262 addr = data_offset;
9263 /* very important to increment global pointer at this time
9264 because initializers themselves can create new initializers */
9265 data_offset += size;
9266 /* add padding if bound check */
9267 if (do_bounds_check)
9268 data_offset++;
9269 sec->data_offset = data_offset;
9270 /* allocate section space to put the data */
9271 if (sec->sh_type != SHT_NOBITS &&
9272 data_offset > sec->data_allocated)
9273 section_realloc(sec, data_offset);
9274 /* align section if needed */
9275 if (align > sec->sh_addralign)
9276 sec->sh_addralign = align;
9277 } else {
9278 addr = 0; /* avoid warning */
9281 if (v) {
9282 if (scope != VT_CONST || !sym) {
9283 sym = sym_push(v, type, r | VT_SYM, 0);
9285 /* update symbol definition */
9286 if (sec) {
9287 put_extern_sym(sym, sec, addr, size);
9288 } else {
9289 ElfW(Sym) *esym;
9290 /* put a common area */
9291 put_extern_sym(sym, NULL, align, size);
9292 /* XXX: find a nicer way */
9293 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
9294 esym->st_shndx = SHN_COMMON;
9296 } else {
9297 CValue cval;
9299 /* push global reference */
9300 sym = get_sym_ref(type, sec, addr, size);
9301 cval.ul = 0;
9302 vsetc(type, VT_CONST | VT_SYM, &cval);
9303 vtop->sym = sym;
9306 /* handles bounds now because the symbol must be defined
9307 before for the relocation */
9308 if (do_bounds_check) {
9309 unsigned long *bounds_ptr;
9311 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9312 /* then add global bound info */
9313 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9314 bounds_ptr[0] = 0; /* relocated */
9315 bounds_ptr[1] = size;
9318 if (has_init) {
9319 decl_initializer(type, sec, addr, 1, 0);
9320 /* restore parse state if needed */
9321 if (init_str.str) {
9322 tok_str_free(init_str.str);
9323 restore_parse_state(&saved_parse_state);
9326 no_alloc: ;
9329 void put_func_debug(Sym *sym)
9331 char buf[512];
9333 /* stabs info */
9334 /* XXX: we put here a dummy type */
9335 snprintf(buf, sizeof(buf), "%s:%c1",
9336 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9337 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9338 cur_text_section, sym->c);
9339 /* //gr gdb wants a line at the function */
9340 put_stabn(N_SLINE, 0, file->line_num, 0);
9341 last_ind = 0;
9342 last_line_num = 0;
9345 /* parse an old style function declaration list */
9346 /* XXX: check multiple parameter */
9347 static void func_decl_list(Sym *func_sym)
9349 AttributeDef ad;
9350 int v;
9351 Sym *s;
9352 CType btype, type;
9354 /* parse each declaration */
9355 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9356 if (!parse_btype(&btype, &ad))
9357 expect("declaration list");
9358 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9359 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9360 tok == ';') {
9361 /* we accept no variable after */
9362 } else {
9363 for(;;) {
9364 type = btype;
9365 type_decl(&type, &ad, &v, TYPE_DIRECT);
9366 /* find parameter in function parameter list */
9367 s = func_sym->next;
9368 while (s != NULL) {
9369 if ((s->v & ~SYM_FIELD) == v)
9370 goto found;
9371 s = s->next;
9373 error("declaration for parameter '%s' but no such parameter",
9374 get_tok_str(v, NULL));
9375 found:
9376 /* check that no storage specifier except 'register' was given */
9377 if (type.t & VT_STORAGE)
9378 error("storage class specified for '%s'", get_tok_str(v, NULL));
9379 convert_parameter_type(&type);
9380 /* we can add the type (NOTE: it could be local to the function) */
9381 s->type = type;
9382 /* accept other parameters */
9383 if (tok == ',')
9384 next();
9385 else
9386 break;
9389 skip(';');
9393 /* parse a function defined by symbol 'sym' and generate its code in
9394 'cur_text_section' */
9395 static void gen_function(Sym *sym)
9397 int saved_nocode_wanted = nocode_wanted;
9398 nocode_wanted = 0;
9399 ind = cur_text_section->data_offset;
9400 /* NOTE: we patch the symbol size later */
9401 put_extern_sym(sym, cur_text_section, ind, 0);
9402 funcname = get_tok_str(sym->v, NULL);
9403 func_ind = ind;
9404 /* put debug symbol */
9405 if (do_debug)
9406 put_func_debug(sym);
9407 /* push a dummy symbol to enable local sym storage */
9408 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9409 gfunc_prolog(&sym->type);
9410 rsym = 0;
9411 block(NULL, NULL, NULL, NULL, 0, 0);
9412 gsym(rsym);
9413 gfunc_epilog();
9414 cur_text_section->data_offset = ind;
9415 label_pop(&global_label_stack, NULL);
9416 sym_pop(&local_stack, NULL); /* reset local stack */
9417 /* end of function */
9418 /* patch symbol size */
9419 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
9420 ind - func_ind;
9421 if (do_debug) {
9422 put_stabn(N_FUN, 0, 0, ind - func_ind);
9424 /* It's better to crash than to generate wrong code */
9425 cur_text_section = NULL;
9426 funcname = ""; /* for safety */
9427 func_vt.t = VT_VOID; /* for safety */
9428 ind = 0; /* for safety */
9429 nocode_wanted = saved_nocode_wanted;
9432 static void gen_inline_functions(void)
9434 Sym *sym;
9435 CType *type;
9436 int *str, inline_generated;
9438 /* iterate while inline function are referenced */
9439 for(;;) {
9440 inline_generated = 0;
9441 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9442 type = &sym->type;
9443 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9444 (type->t & (VT_STATIC | VT_INLINE)) ==
9445 (VT_STATIC | VT_INLINE) &&
9446 sym->c != 0) {
9447 /* the function was used: generate its code and
9448 convert it to a normal function */
9449 str = INLINE_DEF(sym->r);
9450 sym->r = VT_SYM | VT_CONST;
9451 sym->type.t &= ~VT_INLINE;
9453 macro_ptr = str;
9454 next();
9455 cur_text_section = text_section;
9456 gen_function(sym);
9457 macro_ptr = NULL; /* fail safe */
9459 tok_str_free(str);
9460 inline_generated = 1;
9463 if (!inline_generated)
9464 break;
9467 /* free all remaining inline function tokens */
9468 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9469 type = &sym->type;
9470 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9471 (type->t & (VT_STATIC | VT_INLINE)) ==
9472 (VT_STATIC | VT_INLINE)) {
9473 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9474 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9475 continue;
9476 str = INLINE_DEF(sym->r);
9477 tok_str_free(str);
9478 sym->r = 0; /* fail safe */
9483 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9484 static void decl(int l)
9486 int v, has_init, r;
9487 CType type, btype;
9488 Sym *sym;
9489 AttributeDef ad;
9491 while (1) {
9492 if (!parse_btype(&btype, &ad)) {
9493 /* skip redundant ';' */
9494 /* XXX: find more elegant solution */
9495 if (tok == ';') {
9496 next();
9497 continue;
9499 if (l == VT_CONST &&
9500 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9501 /* global asm block */
9502 asm_global_instr();
9503 continue;
9505 /* special test for old K&R protos without explicit int
9506 type. Only accepted when defining global data */
9507 if (l == VT_LOCAL || tok < TOK_DEFINE)
9508 break;
9509 btype.t = VT_INT;
9511 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9512 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9513 tok == ';') {
9514 /* we accept no variable after */
9515 next();
9516 continue;
9518 while (1) { /* iterate thru each declaration */
9519 type = btype;
9520 type_decl(&type, &ad, &v, TYPE_DIRECT);
9521 #if 0
9523 char buf[500];
9524 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9525 printf("type = '%s'\n", buf);
9527 #endif
9528 if ((type.t & VT_BTYPE) == VT_FUNC) {
9529 /* if old style function prototype, we accept a
9530 declaration list */
9531 sym = type.ref;
9532 if (sym->c == FUNC_OLD)
9533 func_decl_list(sym);
9536 if (tok == '{') {
9537 if (l == VT_LOCAL)
9538 error("cannot use local functions");
9539 if ((type.t & VT_BTYPE) != VT_FUNC)
9540 expect("function definition");
9542 /* reject abstract declarators in function definition */
9543 sym = type.ref;
9544 while ((sym = sym->next) != NULL)
9545 if (!(sym->v & ~SYM_FIELD))
9546 expect("identifier");
9548 /* XXX: cannot do better now: convert extern line to static inline */
9549 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9550 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9552 sym = sym_find(v);
9553 if (sym) {
9554 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9555 goto func_error1;
9556 /* specific case: if not func_call defined, we put
9557 the one of the prototype */
9558 /* XXX: should have default value */
9559 r = sym->type.ref->r;
9560 if (FUNC_CALL(r) != FUNC_CDECL
9561 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9562 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9563 if (FUNC_EXPORT(r))
9564 FUNC_EXPORT(type.ref->r) = 1;
9566 if (!is_compatible_types(&sym->type, &type)) {
9567 func_error1:
9568 error("incompatible types for redefinition of '%s'",
9569 get_tok_str(v, NULL));
9571 /* if symbol is already defined, then put complete type */
9572 sym->type = type;
9573 } else {
9574 /* put function symbol */
9575 sym = global_identifier_push(v, type.t, 0);
9576 sym->type.ref = type.ref;
9579 /* static inline functions are just recorded as a kind
9580 of macro. Their code will be emitted at the end of
9581 the compilation unit only if they are used */
9582 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9583 (VT_INLINE | VT_STATIC)) {
9584 TokenString func_str;
9585 int block_level;
9587 tok_str_new(&func_str);
9589 block_level = 0;
9590 for(;;) {
9591 int t;
9592 if (tok == TOK_EOF)
9593 error("unexpected end of file");
9594 tok_str_add_tok(&func_str);
9595 t = tok;
9596 next();
9597 if (t == '{') {
9598 block_level++;
9599 } else if (t == '}') {
9600 block_level--;
9601 if (block_level == 0)
9602 break;
9605 tok_str_add(&func_str, -1);
9606 tok_str_add(&func_str, 0);
9607 INLINE_DEF(sym->r) = func_str.str;
9608 } else {
9609 /* compute text section */
9610 cur_text_section = ad.section;
9611 if (!cur_text_section)
9612 cur_text_section = text_section;
9613 sym->r = VT_SYM | VT_CONST;
9614 gen_function(sym);
9616 break;
9617 } else {
9618 if (btype.t & VT_TYPEDEF) {
9619 /* save typedefed type */
9620 /* XXX: test storage specifiers ? */
9621 sym = sym_push(v, &type, 0, 0);
9622 sym->type.t |= VT_TYPEDEF;
9623 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9624 /* external function definition */
9625 /* specific case for func_call attribute */
9626 if (ad.func_attr)
9627 type.ref->r = ad.func_attr;
9628 external_sym(v, &type, 0);
9629 } else {
9630 /* not lvalue if array */
9631 r = 0;
9632 if (!(type.t & VT_ARRAY))
9633 r |= lvalue_type(type.t);
9634 has_init = (tok == '=');
9635 if ((btype.t & VT_EXTERN) ||
9636 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9637 !has_init && l == VT_CONST && type.ref->c < 0)) {
9638 /* external variable */
9639 /* NOTE: as GCC, uninitialized global static
9640 arrays of null size are considered as
9641 extern */
9642 external_sym(v, &type, r);
9643 } else {
9644 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9645 if (type.t & VT_STATIC)
9646 r |= VT_CONST;
9647 else
9648 r |= l;
9649 if (has_init)
9650 next();
9651 decl_initializer_alloc(&type, &ad, r,
9652 has_init, v, l);
9655 if (tok != ',') {
9656 skip(';');
9657 break;
9659 next();
9665 /* better than nothing, but needs extension to handle '-E' option
9666 correctly too */
9667 static void preprocess_init(TCCState *s1)
9669 s1->include_stack_ptr = s1->include_stack;
9670 /* XXX: move that before to avoid having to initialize
9671 file->ifdef_stack_ptr ? */
9672 s1->ifdef_stack_ptr = s1->ifdef_stack;
9673 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9675 /* XXX: not ANSI compliant: bound checking says error */
9676 vtop = vstack - 1;
9677 s1->pack_stack[0] = 0;
9678 s1->pack_stack_ptr = s1->pack_stack;
9681 /* compile the C file opened in 'file'. Return non zero if errors. */
9682 static int tcc_compile(TCCState *s1)
9684 Sym *define_start;
9685 char buf[512];
9686 volatile int section_sym;
9688 #ifdef INC_DEBUG
9689 printf("%s: **** new file\n", file->filename);
9690 #endif
9691 preprocess_init(s1);
9693 cur_text_section = NULL;
9694 funcname = "";
9695 anon_sym = SYM_FIRST_ANOM;
9697 /* file info: full path + filename */
9698 section_sym = 0; /* avoid warning */
9699 if (do_debug) {
9700 section_sym = put_elf_sym(symtab_section, 0, 0,
9701 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
9702 text_section->sh_num, NULL);
9703 getcwd(buf, sizeof(buf));
9704 #ifdef _WIN32
9705 normalize_slashes(buf);
9706 #endif
9707 pstrcat(buf, sizeof(buf), "/");
9708 put_stabs_r(buf, N_SO, 0, 0,
9709 text_section->data_offset, text_section, section_sym);
9710 put_stabs_r(file->filename, N_SO, 0, 0,
9711 text_section->data_offset, text_section, section_sym);
9713 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9714 symbols can be safely used */
9715 put_elf_sym(symtab_section, 0, 0,
9716 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
9717 SHN_ABS, file->filename);
9719 /* define some often used types */
9720 int_type.t = VT_INT;
9722 char_pointer_type.t = VT_BYTE;
9723 mk_pointer(&char_pointer_type);
9725 func_old_type.t = VT_FUNC;
9726 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9728 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9729 float_type.t = VT_FLOAT;
9730 double_type.t = VT_DOUBLE;
9732 func_float_type.t = VT_FUNC;
9733 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9734 func_double_type.t = VT_FUNC;
9735 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9736 #endif
9738 #if 0
9739 /* define 'void *alloca(unsigned int)' builtin function */
9741 Sym *s1;
9743 p = anon_sym++;
9744 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9745 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9746 s1->next = NULL;
9747 sym->next = s1;
9748 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9750 #endif
9752 define_start = define_stack;
9753 nocode_wanted = 1;
9755 if (setjmp(s1->error_jmp_buf) == 0) {
9756 s1->nb_errors = 0;
9757 s1->error_set_jmp_enabled = 1;
9759 ch = file->buf_ptr[0];
9760 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9761 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9762 next();
9763 decl(VT_CONST);
9764 if (tok != TOK_EOF)
9765 expect("declaration");
9767 /* end of translation unit info */
9768 if (do_debug) {
9769 put_stabs_r(NULL, N_SO, 0, 0,
9770 text_section->data_offset, text_section, section_sym);
9773 s1->error_set_jmp_enabled = 0;
9775 /* reset define stack, but leave -Dsymbols (may be incorrect if
9776 they are undefined) */
9777 free_defines(define_start);
9779 gen_inline_functions();
9781 sym_pop(&global_stack, NULL);
9782 sym_pop(&local_stack, NULL);
9784 return s1->nb_errors != 0 ? -1 : 0;
9787 /* Preprocess the current file */
9788 /* XXX: add line and file infos,
9789 * XXX: add options to preserve spaces (partly done, only spaces in macro are
9790 * not preserved)
9792 static int tcc_preprocess(TCCState *s1)
9794 Sym *define_start;
9795 BufferedFile *file_ref;
9796 int token_seen, line_ref;
9798 preprocess_init(s1);
9799 define_start = define_stack;
9800 ch = file->buf_ptr[0];
9802 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9803 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9804 PARSE_FLAG_LINEFEED;
9806 token_seen = 0;
9807 line_ref = 0;
9808 file_ref = NULL;
9810 for (;;) {
9811 next();
9812 if (tok == TOK_EOF) {
9813 break;
9814 } else if (tok == TOK_LINEFEED) {
9815 if (!token_seen)
9816 continue;
9817 ++line_ref;
9818 token_seen = 0;
9819 } else if (token_seen) {
9820 fwrite(tok_spaces.data, tok_spaces.size, 1, s1->outfile);
9821 } else {
9822 int d = file->line_num - line_ref;
9823 if (file != file_ref || d < 0 || d >= 8)
9824 fprintf(s1->outfile, "# %d \"%s\"\n", file->line_num, file->filename);
9825 else
9826 while (d)
9827 fputs("\n", s1->outfile), --d;
9828 line_ref = (file_ref = file)->line_num;
9829 token_seen = 1;
9831 fputs(get_tok_str(tok, &tokc), s1->outfile);
9833 free_defines(define_start);
9834 return 0;
9837 #ifdef LIBTCC
9838 int tcc_compile_string(TCCState *s, const char *str)
9840 BufferedFile bf1, *bf = &bf1;
9841 int ret, len;
9842 char *buf;
9844 /* init file structure */
9845 bf->fd = -1;
9846 /* XXX: avoid copying */
9847 len = strlen(str);
9848 buf = tcc_malloc(len + 1);
9849 if (!buf)
9850 return -1;
9851 memcpy(buf, str, len);
9852 buf[len] = CH_EOB;
9853 bf->buf_ptr = buf;
9854 bf->buf_end = buf + len;
9855 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9856 bf->line_num = 1;
9857 file = bf;
9858 ret = tcc_compile(s);
9859 file = NULL;
9860 tcc_free(buf);
9862 /* currently, no need to close */
9863 return ret;
9865 #endif
9867 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9868 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9870 BufferedFile bf1, *bf = &bf1;
9872 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9873 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9874 /* default value */
9875 if (!value)
9876 value = "1";
9877 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9879 /* init file structure */
9880 bf->fd = -1;
9881 bf->buf_ptr = bf->buffer;
9882 bf->buf_end = bf->buffer + strlen(bf->buffer);
9883 *bf->buf_end = CH_EOB;
9884 bf->filename[0] = '\0';
9885 bf->line_num = 1;
9886 file = bf;
9888 s1->include_stack_ptr = s1->include_stack;
9890 /* parse with define parser */
9891 ch = file->buf_ptr[0];
9892 next_nomacro();
9893 parse_define();
9894 file = NULL;
9897 /* undefine a preprocessor symbol */
9898 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9900 TokenSym *ts;
9901 Sym *s;
9902 ts = tok_alloc(sym, strlen(sym));
9903 s = define_find(ts->tok);
9904 /* undefine symbol by putting an invalid name */
9905 if (s)
9906 define_undef(s);
9909 #ifdef CONFIG_TCC_ASM
9911 #ifdef TCC_TARGET_I386
9912 #include "i386-asm.c"
9913 #endif
9914 #include "tccasm.c"
9916 #else
9917 static void asm_instr(void)
9919 error("inline asm() not supported");
9921 static void asm_global_instr(void)
9923 error("inline asm() not supported");
9925 #endif
9927 #include "tccelf.c"
9929 #ifdef TCC_TARGET_COFF
9930 #include "tcccoff.c"
9931 #endif
9933 #ifdef TCC_TARGET_PE
9934 #include "tccpe.c"
9935 #endif
9937 /* print the position in the source file of PC value 'pc' by reading
9938 the stabs debug information */
9939 static void rt_printline(unsigned long wanted_pc)
9941 Stab_Sym *sym, *sym_end;
9942 char func_name[128], last_func_name[128];
9943 unsigned long func_addr, last_pc, pc;
9944 const char *incl_files[INCLUDE_STACK_SIZE];
9945 int incl_index, len, last_line_num, i;
9946 const char *str, *p;
9948 fprintf(stderr, "0x%08lx:", wanted_pc);
9950 func_name[0] = '\0';
9951 func_addr = 0;
9952 incl_index = 0;
9953 last_func_name[0] = '\0';
9954 last_pc = 0xffffffff;
9955 last_line_num = 1;
9956 sym = (Stab_Sym *)stab_section->data + 1;
9957 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9958 while (sym < sym_end) {
9959 switch(sym->n_type) {
9960 /* function start or end */
9961 case N_FUN:
9962 if (sym->n_strx == 0) {
9963 /* we test if between last line and end of function */
9964 pc = sym->n_value + func_addr;
9965 if (wanted_pc >= last_pc && wanted_pc < pc)
9966 goto found;
9967 func_name[0] = '\0';
9968 func_addr = 0;
9969 } else {
9970 str = stabstr_section->data + sym->n_strx;
9971 p = strchr(str, ':');
9972 if (!p) {
9973 pstrcpy(func_name, sizeof(func_name), str);
9974 } else {
9975 len = p - str;
9976 if (len > sizeof(func_name) - 1)
9977 len = sizeof(func_name) - 1;
9978 memcpy(func_name, str, len);
9979 func_name[len] = '\0';
9981 func_addr = sym->n_value;
9983 break;
9984 /* line number info */
9985 case N_SLINE:
9986 pc = sym->n_value + func_addr;
9987 if (wanted_pc >= last_pc && wanted_pc < pc)
9988 goto found;
9989 last_pc = pc;
9990 last_line_num = sym->n_desc;
9991 /* XXX: slow! */
9992 strcpy(last_func_name, func_name);
9993 break;
9994 /* include files */
9995 case N_BINCL:
9996 str = stabstr_section->data + sym->n_strx;
9997 add_incl:
9998 if (incl_index < INCLUDE_STACK_SIZE) {
9999 incl_files[incl_index++] = str;
10001 break;
10002 case N_EINCL:
10003 if (incl_index > 1)
10004 incl_index--;
10005 break;
10006 case N_SO:
10007 if (sym->n_strx == 0) {
10008 incl_index = 0; /* end of translation unit */
10009 } else {
10010 str = stabstr_section->data + sym->n_strx;
10011 /* do not add path */
10012 len = strlen(str);
10013 if (len > 0 && str[len - 1] != '/')
10014 goto add_incl;
10016 break;
10018 sym++;
10021 /* second pass: we try symtab symbols (no line number info) */
10022 incl_index = 0;
10024 ElfW(Sym) *sym, *sym_end;
10025 int type;
10027 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
10028 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
10029 sym < sym_end;
10030 sym++) {
10031 type = ELFW(ST_TYPE)(sym->st_info);
10032 if (type == STT_FUNC) {
10033 if (wanted_pc >= sym->st_value &&
10034 wanted_pc < sym->st_value + sym->st_size) {
10035 pstrcpy(last_func_name, sizeof(last_func_name),
10036 strtab_section->data + sym->st_name);
10037 goto found;
10042 /* did not find any info: */
10043 fprintf(stderr, " ???\n");
10044 return;
10045 found:
10046 if (last_func_name[0] != '\0') {
10047 fprintf(stderr, " %s()", last_func_name);
10049 if (incl_index > 0) {
10050 fprintf(stderr, " (%s:%d",
10051 incl_files[incl_index - 1], last_line_num);
10052 for(i = incl_index - 2; i >= 0; i--)
10053 fprintf(stderr, ", included from %s", incl_files[i]);
10054 fprintf(stderr, ")");
10056 fprintf(stderr, "\n");
10059 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
10061 #ifdef __i386__
10063 /* fix for glibc 2.1 */
10064 #ifndef REG_EIP
10065 #define REG_EIP EIP
10066 #define REG_EBP EBP
10067 #endif
10069 /* return the PC at frame level 'level'. Return non zero if not found */
10070 static int rt_get_caller_pc(unsigned long *paddr,
10071 ucontext_t *uc, int level)
10073 unsigned long fp;
10074 int i;
10076 if (level == 0) {
10077 #if defined(__FreeBSD__)
10078 *paddr = uc->uc_mcontext.mc_eip;
10079 #elif defined(__dietlibc__)
10080 *paddr = uc->uc_mcontext.eip;
10081 #else
10082 *paddr = uc->uc_mcontext.gregs[REG_EIP];
10083 #endif
10084 return 0;
10085 } else {
10086 #if defined(__FreeBSD__)
10087 fp = uc->uc_mcontext.mc_ebp;
10088 #elif defined(__dietlibc__)
10089 fp = uc->uc_mcontext.ebp;
10090 #else
10091 fp = uc->uc_mcontext.gregs[REG_EBP];
10092 #endif
10093 for(i=1;i<level;i++) {
10094 /* XXX: check address validity with program info */
10095 if (fp <= 0x1000 || fp >= 0xc0000000)
10096 return -1;
10097 fp = ((unsigned long *)fp)[0];
10099 *paddr = ((unsigned long *)fp)[1];
10100 return 0;
10103 #elif defined(__x86_64__)
10104 /* return the PC at frame level 'level'. Return non zero if not found */
10105 static int rt_get_caller_pc(unsigned long *paddr,
10106 ucontext_t *uc, int level)
10108 unsigned long fp;
10109 int i;
10111 if (level == 0) {
10112 /* XXX: only support linux */
10113 *paddr = uc->uc_mcontext.gregs[REG_RIP];
10114 return 0;
10115 } else {
10116 fp = uc->uc_mcontext.gregs[REG_RBP];
10117 for(i=1;i<level;i++) {
10118 /* XXX: check address validity with program info */
10119 if (fp <= 0x1000 || fp >= 0xc0000000)
10120 return -1;
10121 fp = ((unsigned long *)fp)[0];
10123 *paddr = ((unsigned long *)fp)[1];
10124 return 0;
10127 #else
10129 #warning add arch specific rt_get_caller_pc()
10131 static int rt_get_caller_pc(unsigned long *paddr,
10132 ucontext_t *uc, int level)
10134 return -1;
10136 #endif
10138 /* emit a run time error at position 'pc' */
10139 void rt_error(ucontext_t *uc, const char *fmt, ...)
10141 va_list ap;
10142 unsigned long pc;
10143 int i;
10145 va_start(ap, fmt);
10146 fprintf(stderr, "Runtime error: ");
10147 vfprintf(stderr, fmt, ap);
10148 fprintf(stderr, "\n");
10149 for(i=0;i<num_callers;i++) {
10150 if (rt_get_caller_pc(&pc, uc, i) < 0)
10151 break;
10152 if (i == 0)
10153 fprintf(stderr, "at ");
10154 else
10155 fprintf(stderr, "by ");
10156 rt_printline(pc);
10158 exit(255);
10159 va_end(ap);
10162 /* signal handler for fatal errors */
10163 static void sig_error(int signum, siginfo_t *siginf, void *puc)
10165 ucontext_t *uc = puc;
10167 switch(signum) {
10168 case SIGFPE:
10169 switch(siginf->si_code) {
10170 case FPE_INTDIV:
10171 case FPE_FLTDIV:
10172 rt_error(uc, "division by zero");
10173 break;
10174 default:
10175 rt_error(uc, "floating point exception");
10176 break;
10178 break;
10179 case SIGBUS:
10180 case SIGSEGV:
10181 if (rt_bound_error_msg && *rt_bound_error_msg)
10182 rt_error(uc, *rt_bound_error_msg);
10183 else
10184 rt_error(uc, "dereferencing invalid pointer");
10185 break;
10186 case SIGILL:
10187 rt_error(uc, "illegal instruction");
10188 break;
10189 case SIGABRT:
10190 rt_error(uc, "abort() called");
10191 break;
10192 default:
10193 rt_error(uc, "caught signal %d", signum);
10194 break;
10196 exit(255);
10198 #endif
10200 /* do all relocations (needed before using tcc_get_symbol()) */
10201 int tcc_relocate(TCCState *s1)
10203 Section *s;
10204 int i;
10206 s1->nb_errors = 0;
10208 #ifdef TCC_TARGET_PE
10209 pe_add_runtime(s1);
10210 #else
10211 tcc_add_runtime(s1);
10212 #endif
10214 relocate_common_syms();
10216 tcc_add_linker_symbols(s1);
10217 #ifndef TCC_TARGET_PE
10218 build_got_entries(s1);
10219 #endif
10220 /* compute relocation address : section are relocated in place. We
10221 also alloc the bss space */
10222 for(i = 1; i < s1->nb_sections; i++) {
10223 s = s1->sections[i];
10224 if (s->sh_flags & SHF_ALLOC) {
10225 if (s->sh_type == SHT_NOBITS)
10226 s->data = tcc_mallocz(s->data_offset);
10227 s->sh_addr = (unsigned long)s->data;
10231 relocate_syms(s1, 1);
10233 if (s1->nb_errors != 0)
10234 return -1;
10236 /* relocate each section */
10237 for(i = 1; i < s1->nb_sections; i++) {
10238 s = s1->sections[i];
10239 if (s->reloc)
10240 relocate_section(s1, s);
10243 /* mark executable sections as executable in memory */
10244 for(i = 1; i < s1->nb_sections; i++) {
10245 s = s1->sections[i];
10246 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10247 (SHF_ALLOC | SHF_EXECINSTR))
10248 set_pages_executable(s->data, s->data_offset);
10250 return 0;
10253 /* launch the compiled program with the given arguments */
10254 int tcc_run(TCCState *s1, int argc, char **argv)
10256 int (*prog_main)(int, char **);
10258 if (tcc_relocate(s1) < 0)
10259 return -1;
10261 prog_main = tcc_get_symbol_err(s1, "main");
10263 if (do_debug) {
10264 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10265 error("debug mode currently not available for Windows");
10266 #else
10267 struct sigaction sigact;
10268 /* install TCC signal handlers to print debug info on fatal
10269 runtime errors */
10270 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10271 sigact.sa_sigaction = sig_error;
10272 sigemptyset(&sigact.sa_mask);
10273 sigaction(SIGFPE, &sigact, NULL);
10274 sigaction(SIGILL, &sigact, NULL);
10275 sigaction(SIGSEGV, &sigact, NULL);
10276 sigaction(SIGBUS, &sigact, NULL);
10277 sigaction(SIGABRT, &sigact, NULL);
10278 #endif
10281 #ifdef CONFIG_TCC_BCHECK
10282 if (do_bounds_check) {
10283 void (*bound_init)(void);
10285 /* set error function */
10286 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10287 "__bound_error_msg");
10289 /* XXX: use .init section so that it also work in binary ? */
10290 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10291 bound_init();
10293 #endif
10294 return (*prog_main)(argc, argv);
10297 void tcc_memstats(void)
10299 #ifdef MEM_DEBUG
10300 printf("memory in use: %d\n", mem_cur_size);
10301 #endif
10304 static void tcc_cleanup(void)
10306 int i, n;
10308 if (NULL == tcc_state)
10309 return;
10310 tcc_state = NULL;
10312 /* free -D defines */
10313 free_defines(NULL);
10315 /* free tokens */
10316 n = tok_ident - TOK_IDENT;
10317 for(i = 0; i < n; i++)
10318 tcc_free(table_ident[i]);
10319 tcc_free(table_ident);
10321 /* free sym_pools */
10322 dynarray_reset(&sym_pools, &nb_sym_pools);
10323 /* string buffer */
10324 cstr_free(&tokcstr);
10325 /* reset symbol stack */
10326 sym_free_first = NULL;
10327 /* cleanup from error/setjmp */
10328 macro_ptr = NULL;
10331 TCCState *tcc_new(void)
10333 const char *p, *r;
10334 TCCState *s;
10335 TokenSym *ts;
10336 int i, c;
10338 tcc_cleanup();
10340 s = tcc_mallocz(sizeof(TCCState));
10341 if (!s)
10342 return NULL;
10343 tcc_state = s;
10344 s->output_type = TCC_OUTPUT_MEMORY;
10346 /* init isid table */
10347 for(i=CH_EOF;i<256;i++)
10348 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
10350 /* add all tokens */
10351 table_ident = NULL;
10352 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10354 tok_ident = TOK_IDENT;
10355 p = tcc_keywords;
10356 while (*p) {
10357 r = p;
10358 for(;;) {
10359 c = *r++;
10360 if (c == '\0')
10361 break;
10363 ts = tok_alloc(p, r - p - 1);
10364 p = r;
10367 /* we add dummy defines for some special macros to speed up tests
10368 and to have working defined() */
10369 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10370 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10371 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10372 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10374 /* standard defines */
10375 tcc_define_symbol(s, "__STDC__", NULL);
10376 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10377 #if defined(TCC_TARGET_I386)
10378 tcc_define_symbol(s, "__i386__", NULL);
10379 #endif
10380 #if defined(TCC_TARGET_X86_64)
10381 tcc_define_symbol(s, "__x86_64__", NULL);
10382 #endif
10383 #if defined(TCC_TARGET_ARM)
10384 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10385 tcc_define_symbol(s, "__arm_elf__", NULL);
10386 tcc_define_symbol(s, "__arm_elf", NULL);
10387 tcc_define_symbol(s, "arm_elf", NULL);
10388 tcc_define_symbol(s, "__arm__", NULL);
10389 tcc_define_symbol(s, "__arm", NULL);
10390 tcc_define_symbol(s, "arm", NULL);
10391 tcc_define_symbol(s, "__APCS_32__", NULL);
10392 #endif
10393 #ifdef TCC_TARGET_PE
10394 tcc_define_symbol(s, "_WIN32", NULL);
10395 #else
10396 tcc_define_symbol(s, "__unix__", NULL);
10397 tcc_define_symbol(s, "__unix", NULL);
10398 #if defined(__linux)
10399 tcc_define_symbol(s, "__linux__", NULL);
10400 tcc_define_symbol(s, "__linux", NULL);
10401 #endif
10402 #endif
10403 /* tiny C specific defines */
10404 tcc_define_symbol(s, "__TINYC__", NULL);
10406 /* tiny C & gcc defines */
10407 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10408 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10409 #ifdef TCC_TARGET_PE
10410 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10411 #else
10412 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10413 #endif
10415 #ifndef TCC_TARGET_PE
10416 /* default library paths */
10417 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
10418 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
10419 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
10420 #endif
10422 /* no section zero */
10423 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10425 /* create standard sections */
10426 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10427 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10428 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10430 /* symbols are always generated for linking stage */
10431 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10432 ".strtab",
10433 ".hashtab", SHF_PRIVATE);
10434 strtab_section = symtab_section->link;
10436 /* private symbol table for dynamic symbols */
10437 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10438 ".dynstrtab",
10439 ".dynhashtab", SHF_PRIVATE);
10440 s->alacarte_link = 1;
10442 #ifdef CHAR_IS_UNSIGNED
10443 s->char_is_unsigned = 1;
10444 #endif
10445 #if defined(TCC_TARGET_PE) && 0
10446 /* XXX: currently the PE linker is not ready to support that */
10447 s->leading_underscore = 1;
10448 #endif
10450 #ifdef TCC_TARGET_X86_64
10451 s->jmp_table = NULL;
10452 #endif
10453 return s;
10456 void tcc_delete(TCCState *s1)
10458 int i;
10460 tcc_cleanup();
10462 /* free all sections */
10463 free_section(s1->dynsymtab_section);
10465 for(i = 1; i < s1->nb_sections; i++)
10466 free_section(s1->sections[i]);
10467 tcc_free(s1->sections);
10469 /* free any loaded DLLs */
10470 for ( i = 0; i < s1->nb_loaded_dlls; i++)
10472 DLLReference *ref = s1->loaded_dlls[i];
10473 if ( ref->handle )
10474 dlclose(ref->handle);
10477 /* free loaded dlls array */
10478 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10480 /* free library paths */
10481 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10483 /* free include paths */
10484 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10485 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10486 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10488 #ifdef TCC_TARGET_X86_64
10489 tcc_free(s1->jmp_table);
10490 #endif
10491 tcc_free(s1);
10494 int tcc_add_include_path(TCCState *s1, const char *pathname)
10496 char *pathname1;
10498 pathname1 = tcc_strdup(pathname);
10499 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10500 return 0;
10503 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10505 char *pathname1;
10507 pathname1 = tcc_strdup(pathname);
10508 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10509 return 0;
10512 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10514 const char *ext;
10515 ElfW(Ehdr) ehdr;
10516 int fd, ret;
10517 BufferedFile *saved_file;
10519 /* find source file type with extension */
10520 ext = tcc_fileextension(filename);
10521 if (ext[0])
10522 ext++;
10524 /* open the file */
10525 saved_file = file;
10526 file = tcc_open(s1, filename);
10527 if (!file) {
10528 if (flags & AFF_PRINT_ERROR) {
10529 error_noabort("file '%s' not found", filename);
10531 ret = -1;
10532 goto fail1;
10535 if (flags & AFF_PREPROCESS) {
10536 ret = tcc_preprocess(s1);
10537 } else if (!ext[0] || !strcmp(ext, "c")) {
10538 /* C file assumed */
10539 ret = tcc_compile(s1);
10540 } else
10541 #ifdef CONFIG_TCC_ASM
10542 if (!strcmp(ext, "S")) {
10543 /* preprocessed assembler */
10544 ret = tcc_assemble(s1, 1);
10545 } else if (!strcmp(ext, "s")) {
10546 /* non preprocessed assembler */
10547 ret = tcc_assemble(s1, 0);
10548 } else
10549 #endif
10550 #ifdef TCC_TARGET_PE
10551 if (!strcmp(ext, "def")) {
10552 ret = pe_load_def_file(s1, file->fd);
10553 } else
10554 #endif
10556 fd = file->fd;
10557 /* assume executable format: auto guess file type */
10558 ret = read(fd, &ehdr, sizeof(ehdr));
10559 lseek(fd, 0, SEEK_SET);
10560 if (ret <= 0) {
10561 error_noabort("could not read header");
10562 goto fail;
10563 } else if (ret != sizeof(ehdr)) {
10564 goto try_load_script;
10567 if (ehdr.e_ident[0] == ELFMAG0 &&
10568 ehdr.e_ident[1] == ELFMAG1 &&
10569 ehdr.e_ident[2] == ELFMAG2 &&
10570 ehdr.e_ident[3] == ELFMAG3) {
10571 file->line_num = 0; /* do not display line number if error */
10572 if (ehdr.e_type == ET_REL) {
10573 ret = tcc_load_object_file(s1, fd, 0);
10574 } else if (ehdr.e_type == ET_DYN) {
10575 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10576 #ifdef TCC_TARGET_PE
10577 ret = -1;
10578 #else
10579 void *h;
10580 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10581 if (h)
10582 ret = 0;
10583 else
10584 ret = -1;
10585 #endif
10586 } else {
10587 ret = tcc_load_dll(s1, fd, filename,
10588 (flags & AFF_REFERENCED_DLL) != 0);
10590 } else {
10591 error_noabort("unrecognized ELF file");
10592 goto fail;
10594 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10595 file->line_num = 0; /* do not display line number if error */
10596 ret = tcc_load_archive(s1, fd);
10597 } else
10598 #ifdef TCC_TARGET_COFF
10599 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10600 ret = tcc_load_coff(s1, fd);
10601 } else
10602 #endif
10603 #ifdef TCC_TARGET_PE
10604 if (pe_test_res_file(&ehdr, ret)) {
10605 ret = pe_load_res_file(s1, fd);
10606 } else
10607 #endif
10609 /* as GNU ld, consider it is an ld script if not recognized */
10610 try_load_script:
10611 ret = tcc_load_ldscript(s1);
10612 if (ret < 0) {
10613 error_noabort("unrecognized file type");
10614 goto fail;
10618 the_end:
10619 tcc_close(file);
10620 fail1:
10621 file = saved_file;
10622 return ret;
10623 fail:
10624 ret = -1;
10625 goto the_end;
10628 int tcc_add_file(TCCState *s, const char *filename)
10630 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10633 int tcc_add_library_path(TCCState *s, const char *pathname)
10635 char *pathname1;
10637 pathname1 = tcc_strdup(pathname);
10638 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10639 return 0;
10642 /* find and load a dll. Return non zero if not found */
10643 /* XXX: add '-rpath' option support ? */
10644 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10646 char buf[1024];
10647 int i;
10649 for(i = 0; i < s->nb_library_paths; i++) {
10650 snprintf(buf, sizeof(buf), "%s/%s",
10651 s->library_paths[i], filename);
10652 if (tcc_add_file_internal(s, buf, flags) == 0)
10653 return 0;
10655 return -1;
10658 /* the library name is the same as the argument of the '-l' option */
10659 int tcc_add_library(TCCState *s, const char *libraryname)
10661 char buf[1024];
10662 int i;
10664 /* first we look for the dynamic library if not static linking */
10665 if (!s->static_link) {
10666 #ifdef TCC_TARGET_PE
10667 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10668 #else
10669 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10670 #endif
10671 if (tcc_add_dll(s, buf, 0) == 0)
10672 return 0;
10675 /* then we look for the static library */
10676 for(i = 0; i < s->nb_library_paths; i++) {
10677 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10678 s->library_paths[i], libraryname);
10679 if (tcc_add_file_internal(s, buf, 0) == 0)
10680 return 0;
10682 return -1;
10685 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10687 add_elf_sym(symtab_section, val, 0,
10688 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
10689 SHN_ABS, name);
10690 return 0;
10693 int tcc_set_output_type(TCCState *s, int output_type)
10695 char buf[1024];
10697 s->output_type = output_type;
10699 if (!s->nostdinc) {
10700 /* default include paths */
10701 /* XXX: reverse order needed if -isystem support */
10702 #ifndef TCC_TARGET_PE
10703 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
10704 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
10705 #endif
10706 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10707 tcc_add_sysinclude_path(s, buf);
10708 #ifdef TCC_TARGET_PE
10709 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10710 tcc_add_sysinclude_path(s, buf);
10711 #endif
10714 /* if bound checking, then add corresponding sections */
10715 #ifdef CONFIG_TCC_BCHECK
10716 if (do_bounds_check) {
10717 /* define symbol */
10718 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10719 /* create bounds sections */
10720 bounds_section = new_section(s, ".bounds",
10721 SHT_PROGBITS, SHF_ALLOC);
10722 lbounds_section = new_section(s, ".lbounds",
10723 SHT_PROGBITS, SHF_ALLOC);
10725 #endif
10727 if (s->char_is_unsigned) {
10728 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10731 /* add debug sections */
10732 if (do_debug) {
10733 /* stab symbols */
10734 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10735 stab_section->sh_entsize = sizeof(Stab_Sym);
10736 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10737 put_elf_str(stabstr_section, "");
10738 stab_section->link = stabstr_section;
10739 /* put first entry */
10740 put_stabs("", 0, 0, 0, 0);
10743 /* add libc crt1/crti objects */
10744 #ifndef TCC_TARGET_PE
10745 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10746 !s->nostdlib) {
10747 if (output_type != TCC_OUTPUT_DLL)
10748 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10749 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10751 #endif
10753 #ifdef TCC_TARGET_PE
10754 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10755 tcc_add_library_path(s, buf);
10756 #endif
10758 return 0;
10761 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10762 #define FD_INVERT 0x0002 /* invert value before storing */
10764 typedef struct FlagDef {
10765 uint16_t offset;
10766 uint16_t flags;
10767 const char *name;
10768 } FlagDef;
10770 static const FlagDef warning_defs[] = {
10771 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10772 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10773 { offsetof(TCCState, warn_error), 0, "error" },
10774 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10775 "implicit-function-declaration" },
10778 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10779 const char *name, int value)
10781 int i;
10782 const FlagDef *p;
10783 const char *r;
10785 r = name;
10786 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10787 r += 3;
10788 value = !value;
10790 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10791 if (!strcmp(r, p->name))
10792 goto found;
10794 return -1;
10795 found:
10796 if (p->flags & FD_INVERT)
10797 value = !value;
10798 *(int *)((uint8_t *)s + p->offset) = value;
10799 return 0;
10803 /* set/reset a warning */
10804 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10806 int i;
10807 const FlagDef *p;
10809 if (!strcmp(warning_name, "all")) {
10810 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10811 if (p->flags & WD_ALL)
10812 *(int *)((uint8_t *)s + p->offset) = 1;
10814 return 0;
10815 } else {
10816 return set_flag(s, warning_defs, countof(warning_defs),
10817 warning_name, value);
10821 static const FlagDef flag_defs[] = {
10822 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10823 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10824 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10825 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10828 /* set/reset a flag */
10829 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10831 return set_flag(s, flag_defs, countof(flag_defs),
10832 flag_name, value);
10835 #if !defined(LIBTCC)
10837 static int64_t getclock_us(void)
10839 #ifdef _WIN32
10840 struct _timeb tb;
10841 _ftime(&tb);
10842 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10843 #else
10844 struct timeval tv;
10845 gettimeofday(&tv, NULL);
10846 return tv.tv_sec * 1000000LL + tv.tv_usec;
10847 #endif
10850 void help(void)
10852 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10853 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10854 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10855 " [-static] [infile1 infile2...] [-run infile args...]\n"
10856 "\n"
10857 "General options:\n"
10858 " -v display current version, increase verbosity\n"
10859 " -c compile only - generate an object file\n"
10860 " -o outfile set output filename\n"
10861 " -Bdir set tcc internal library path\n"
10862 " -bench output compilation statistics\n"
10863 " -run run compiled source\n"
10864 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10865 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10866 " -w disable all warnings\n"
10867 "Preprocessor options:\n"
10868 " -E preprocess only\n"
10869 " -Idir add include path 'dir'\n"
10870 " -Dsym[=val] define 'sym' with value 'val'\n"
10871 " -Usym undefine 'sym'\n"
10872 "Linker options:\n"
10873 " -Ldir add library path 'dir'\n"
10874 " -llib link with dynamic or static library 'lib'\n"
10875 " -shared generate a shared library\n"
10876 " -soname set name for shared library to be used at runtime\n"
10877 " -static static linking\n"
10878 " -rdynamic export all global symbols to dynamic linker\n"
10879 " -r generate (relocatable) object file\n"
10880 "Debugger options:\n"
10881 " -g generate runtime debug info\n"
10882 #ifdef CONFIG_TCC_BCHECK
10883 " -b compile with built-in memory and bounds checker (implies -g)\n"
10884 #endif
10885 " -bt N show N callers in stack traces\n"
10889 #define TCC_OPTION_HAS_ARG 0x0001
10890 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10892 typedef struct TCCOption {
10893 const char *name;
10894 uint16_t index;
10895 uint16_t flags;
10896 } TCCOption;
10898 enum {
10899 TCC_OPTION_HELP,
10900 TCC_OPTION_I,
10901 TCC_OPTION_D,
10902 TCC_OPTION_U,
10903 TCC_OPTION_L,
10904 TCC_OPTION_B,
10905 TCC_OPTION_l,
10906 TCC_OPTION_bench,
10907 TCC_OPTION_bt,
10908 TCC_OPTION_b,
10909 TCC_OPTION_g,
10910 TCC_OPTION_c,
10911 TCC_OPTION_static,
10912 TCC_OPTION_shared,
10913 TCC_OPTION_soname,
10914 TCC_OPTION_o,
10915 TCC_OPTION_r,
10916 TCC_OPTION_Wl,
10917 TCC_OPTION_W,
10918 TCC_OPTION_O,
10919 TCC_OPTION_m,
10920 TCC_OPTION_f,
10921 TCC_OPTION_nostdinc,
10922 TCC_OPTION_nostdlib,
10923 TCC_OPTION_print_search_dirs,
10924 TCC_OPTION_rdynamic,
10925 TCC_OPTION_run,
10926 TCC_OPTION_v,
10927 TCC_OPTION_w,
10928 TCC_OPTION_pipe,
10929 TCC_OPTION_E,
10932 static const TCCOption tcc_options[] = {
10933 { "h", TCC_OPTION_HELP, 0 },
10934 { "?", TCC_OPTION_HELP, 0 },
10935 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10936 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10937 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10938 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10939 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10940 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10941 { "bench", TCC_OPTION_bench, 0 },
10942 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10943 #ifdef CONFIG_TCC_BCHECK
10944 { "b", TCC_OPTION_b, 0 },
10945 #endif
10946 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10947 { "c", TCC_OPTION_c, 0 },
10948 { "static", TCC_OPTION_static, 0 },
10949 { "shared", TCC_OPTION_shared, 0 },
10950 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10951 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10952 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10953 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10954 { "r", TCC_OPTION_r, 0 },
10955 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10956 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10957 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10958 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10959 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10960 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10961 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10962 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10963 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10964 { "w", TCC_OPTION_w, 0 },
10965 { "pipe", TCC_OPTION_pipe, 0},
10966 { "E", TCC_OPTION_E, 0},
10967 { NULL },
10970 /* convert 'str' into an array of space separated strings */
10971 static int expand_args(char ***pargv, const char *str)
10973 const char *s1;
10974 char **argv, *arg;
10975 int argc, len;
10977 argc = 0;
10978 argv = NULL;
10979 for(;;) {
10980 while (is_space(*str))
10981 str++;
10982 if (*str == '\0')
10983 break;
10984 s1 = str;
10985 while (*str != '\0' && !is_space(*str))
10986 str++;
10987 len = str - s1;
10988 arg = tcc_malloc(len + 1);
10989 memcpy(arg, s1, len);
10990 arg[len] = '\0';
10991 dynarray_add((void ***)&argv, &argc, arg);
10993 *pargv = argv;
10994 return argc;
10997 static char **files;
10998 static int nb_files, nb_libraries;
10999 static int multiple_files;
11000 static int print_search_dirs;
11001 static int output_type;
11002 static int reloc_output;
11003 static const char *outfile;
11005 int parse_args(TCCState *s, int argc, char **argv)
11007 int optind;
11008 const TCCOption *popt;
11009 const char *optarg, *p1, *r1;
11010 char *r;
11012 optind = 0;
11013 while (optind < argc) {
11015 r = argv[optind++];
11016 if (r[0] != '-' || r[1] == '\0') {
11017 /* add a new file */
11018 dynarray_add((void ***)&files, &nb_files, r);
11019 if (!multiple_files) {
11020 optind--;
11021 /* argv[0] will be this file */
11022 break;
11024 } else {
11025 /* find option in table (match only the first chars */
11026 popt = tcc_options;
11027 for(;;) {
11028 p1 = popt->name;
11029 if (p1 == NULL)
11030 error("invalid option -- '%s'", r);
11031 r1 = r + 1;
11032 for(;;) {
11033 if (*p1 == '\0')
11034 goto option_found;
11035 if (*r1 != *p1)
11036 break;
11037 p1++;
11038 r1++;
11040 popt++;
11042 option_found:
11043 if (popt->flags & TCC_OPTION_HAS_ARG) {
11044 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
11045 optarg = r1;
11046 } else {
11047 if (optind >= argc)
11048 error("argument to '%s' is missing", r);
11049 optarg = argv[optind++];
11051 } else {
11052 if (*r1 != '\0')
11053 return 0;
11054 optarg = NULL;
11057 switch(popt->index) {
11058 case TCC_OPTION_HELP:
11059 return 0;
11061 case TCC_OPTION_I:
11062 if (tcc_add_include_path(s, optarg) < 0)
11063 error("too many include paths");
11064 break;
11065 case TCC_OPTION_D:
11067 char *sym, *value;
11068 sym = (char *)optarg;
11069 value = strchr(sym, '=');
11070 if (value) {
11071 *value = '\0';
11072 value++;
11074 tcc_define_symbol(s, sym, value);
11076 break;
11077 case TCC_OPTION_U:
11078 tcc_undefine_symbol(s, optarg);
11079 break;
11080 case TCC_OPTION_L:
11081 tcc_add_library_path(s, optarg);
11082 break;
11083 case TCC_OPTION_B:
11084 /* set tcc utilities path (mainly for tcc development) */
11085 tcc_lib_path = optarg;
11086 break;
11087 case TCC_OPTION_l:
11088 dynarray_add((void ***)&files, &nb_files, r);
11089 nb_libraries++;
11090 break;
11091 case TCC_OPTION_bench:
11092 do_bench = 1;
11093 break;
11094 case TCC_OPTION_bt:
11095 num_callers = atoi(optarg);
11096 break;
11097 #ifdef CONFIG_TCC_BCHECK
11098 case TCC_OPTION_b:
11099 do_bounds_check = 1;
11100 do_debug = 1;
11101 break;
11102 #endif
11103 case TCC_OPTION_g:
11104 do_debug = 1;
11105 break;
11106 case TCC_OPTION_c:
11107 multiple_files = 1;
11108 output_type = TCC_OUTPUT_OBJ;
11109 break;
11110 case TCC_OPTION_static:
11111 s->static_link = 1;
11112 break;
11113 case TCC_OPTION_shared:
11114 output_type = TCC_OUTPUT_DLL;
11115 break;
11116 case TCC_OPTION_soname:
11117 s->soname = optarg;
11118 break;
11119 case TCC_OPTION_o:
11120 multiple_files = 1;
11121 outfile = optarg;
11122 break;
11123 case TCC_OPTION_r:
11124 /* generate a .o merging several output files */
11125 reloc_output = 1;
11126 output_type = TCC_OUTPUT_OBJ;
11127 break;
11128 case TCC_OPTION_nostdinc:
11129 s->nostdinc = 1;
11130 break;
11131 case TCC_OPTION_nostdlib:
11132 s->nostdlib = 1;
11133 break;
11134 case TCC_OPTION_print_search_dirs:
11135 print_search_dirs = 1;
11136 break;
11137 case TCC_OPTION_run:
11139 int argc1;
11140 char **argv1;
11141 argc1 = expand_args(&argv1, optarg);
11142 if (argc1 > 0) {
11143 parse_args(s, argc1, argv1);
11145 multiple_files = 0;
11146 output_type = TCC_OUTPUT_MEMORY;
11148 break;
11149 case TCC_OPTION_v:
11150 do {
11151 if (0 == verbose++)
11152 printf("tcc version %s\n", TCC_VERSION);
11153 } while (*optarg++ == 'v');
11154 break;
11155 case TCC_OPTION_f:
11156 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
11157 goto unsupported_option;
11158 break;
11159 case TCC_OPTION_W:
11160 if (tcc_set_warning(s, optarg, 1) < 0 &&
11161 s->warn_unsupported)
11162 goto unsupported_option;
11163 break;
11164 case TCC_OPTION_w:
11165 s->warn_none = 1;
11166 break;
11167 case TCC_OPTION_rdynamic:
11168 s->rdynamic = 1;
11169 break;
11170 case TCC_OPTION_Wl:
11172 const char *p;
11173 if (strstart(optarg, "-Ttext,", &p)) {
11174 s->text_addr = strtoul(p, NULL, 16);
11175 s->has_text_addr = 1;
11176 } else if (strstart(optarg, "--oformat,", &p)) {
11177 if (strstart(p, "elf32-", NULL)) {
11178 s->output_format = TCC_OUTPUT_FORMAT_ELF;
11179 } else if (!strcmp(p, "binary")) {
11180 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
11181 } else
11182 #ifdef TCC_TARGET_COFF
11183 if (!strcmp(p, "coff")) {
11184 s->output_format = TCC_OUTPUT_FORMAT_COFF;
11185 } else
11186 #endif
11188 error("target %s not found", p);
11190 } else {
11191 error("unsupported linker option '%s'", optarg);
11194 break;
11195 case TCC_OPTION_E:
11196 output_type = TCC_OUTPUT_PREPROCESS;
11197 break;
11198 default:
11199 if (s->warn_unsupported) {
11200 unsupported_option:
11201 warning("unsupported option '%s'", r);
11203 break;
11207 return optind + 1;
11210 int main(int argc, char **argv)
11212 int i;
11213 TCCState *s;
11214 int nb_objfiles, ret, optind;
11215 char objfilename[1024];
11216 int64_t start_time = 0;
11218 #ifdef _WIN32
11219 tcc_lib_path = w32_tcc_lib_path();
11220 #endif
11222 s = tcc_new();
11223 output_type = TCC_OUTPUT_EXE;
11224 outfile = NULL;
11225 multiple_files = 1;
11226 files = NULL;
11227 nb_files = 0;
11228 nb_libraries = 0;
11229 reloc_output = 0;
11230 print_search_dirs = 0;
11231 ret = 0;
11233 optind = parse_args(s, argc - 1, argv + 1);
11234 if (print_search_dirs) {
11235 /* enough for Linux kernel */
11236 printf("install: %s/\n", tcc_lib_path);
11237 return 0;
11239 if (optind == 0 || nb_files == 0) {
11240 if (optind && verbose)
11241 return 0;
11242 help();
11243 return 1;
11246 nb_objfiles = nb_files - nb_libraries;
11248 /* if outfile provided without other options, we output an
11249 executable */
11250 if (outfile && output_type == TCC_OUTPUT_MEMORY)
11251 output_type = TCC_OUTPUT_EXE;
11253 /* check -c consistency : only single file handled. XXX: checks file type */
11254 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
11255 /* accepts only a single input file */
11256 if (nb_objfiles != 1)
11257 error("cannot specify multiple files with -c");
11258 if (nb_libraries != 0)
11259 error("cannot specify libraries with -c");
11263 if (output_type == TCC_OUTPUT_PREPROCESS) {
11264 if (!outfile) {
11265 s->outfile = stdout;
11266 } else {
11267 s->outfile = fopen(outfile, "w");
11268 if (!s->outfile)
11269 error("could not open '%s", outfile);
11271 } else if (output_type != TCC_OUTPUT_MEMORY) {
11272 if (!outfile) {
11273 /* compute default outfile name */
11274 char *ext;
11275 const char *name =
11276 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
11277 pstrcpy(objfilename, sizeof(objfilename), name);
11278 ext = tcc_fileextension(objfilename);
11279 #ifdef TCC_TARGET_PE
11280 if (output_type == TCC_OUTPUT_DLL)
11281 strcpy(ext, ".dll");
11282 else
11283 if (output_type == TCC_OUTPUT_EXE)
11284 strcpy(ext, ".exe");
11285 else
11286 #endif
11287 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
11288 strcpy(ext, ".o");
11289 else
11290 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11291 outfile = objfilename;
11295 if (do_bench) {
11296 start_time = getclock_us();
11299 tcc_set_output_type(s, output_type);
11301 /* compile or add each files or library */
11302 for(i = 0; i < nb_files && ret == 0; i++) {
11303 const char *filename;
11305 filename = files[i];
11306 if (output_type == TCC_OUTPUT_PREPROCESS) {
11307 if (tcc_add_file_internal(s, filename,
11308 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11309 ret = 1;
11310 } else if (filename[0] == '-' && filename[1]) {
11311 if (tcc_add_library(s, filename + 2) < 0)
11312 error("cannot find %s", filename);
11313 } else {
11314 if (1 == verbose)
11315 printf("-> %s\n", filename);
11316 if (tcc_add_file(s, filename) < 0)
11317 ret = 1;
11321 /* free all files */
11322 tcc_free(files);
11324 if (ret)
11325 goto the_end;
11327 if (do_bench) {
11328 double total_time;
11329 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11330 if (total_time < 0.001)
11331 total_time = 0.001;
11332 if (total_bytes < 1)
11333 total_bytes = 1;
11334 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11335 tok_ident - TOK_IDENT, total_lines, total_bytes,
11336 total_time, (int)(total_lines / total_time),
11337 total_bytes / total_time / 1000000.0);
11340 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11341 if (outfile)
11342 fclose(s->outfile);
11343 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11344 ret = tcc_run(s, argc - optind, argv + optind);
11345 } else
11346 ret = tcc_output_file(s, outfile) ? 1 : 0;
11347 the_end:
11348 /* XXX: cannot do it with bound checking because of the malloc hooks */
11349 if (!do_bounds_check)
11350 tcc_delete(s);
11352 #ifdef MEM_DEBUG
11353 if (do_bench) {
11354 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11356 #endif
11357 return ret;
11360 #endif