tiny_libmaker: fix function array overflow
[tinycc.git] / tcc.c
blob2a2b82e352e16963f996ca46149bb6cbdb09b8d0
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 vpushll(long long v);
848 void vrott(int n);
849 void vnrott(int n);
850 void lexpand_nr(void);
851 static void vpush_global_sym(CType *type, int v);
852 void vset(CType *type, int r, int v);
853 void type_to_str(char *buf, int buf_size,
854 CType *type, const char *varstr);
855 char *get_tok_str(int v, CValue *cv);
856 static Sym *get_sym_ref(CType *type, Section *sec,
857 unsigned long offset, unsigned long size);
858 static Sym *external_global_sym(int v, CType *type, int r);
860 /* section generation */
861 static void section_realloc(Section *sec, unsigned long new_size);
862 static void *section_ptr_add(Section *sec, unsigned long size);
863 static void put_extern_sym(Sym *sym, Section *section,
864 unsigned long value, unsigned long size);
865 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
866 static int put_elf_str(Section *s, const char *sym);
867 static int put_elf_sym(Section *s,
868 unsigned long value, unsigned long size,
869 int info, int other, int shndx, const char *name);
870 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
871 int info, int other, int sh_num, const char *name);
872 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
873 int type, int symbol);
874 static void put_stabs(const char *str, int type, int other, int desc,
875 unsigned long value);
876 static void put_stabs_r(const char *str, int type, int other, int desc,
877 unsigned long value, Section *sec, int sym_index);
878 static void put_stabn(int type, int other, int desc, int value);
879 static void put_stabd(int type, int other, int desc);
880 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
882 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
883 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
884 #define AFF_PREPROCESS 0x0004 /* preprocess file */
885 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
887 /* tcccoff.c */
888 int tcc_output_coff(TCCState *s1, FILE *f);
890 /* tccpe.c */
891 void *resolve_sym(TCCState *s1, const char *sym, int type);
892 int pe_load_def_file(struct TCCState *s1, int fd);
893 int pe_test_res_file(void *v, int size);
894 int pe_load_res_file(struct TCCState *s1, int fd);
895 void pe_add_runtime(struct TCCState *s1);
896 void pe_guess_outfile(char *objfilename, int output_type);
897 int pe_output_file(struct TCCState *s1, const char *filename);
899 /* tccasm.c */
901 #ifdef CONFIG_TCC_ASM
903 typedef struct ExprValue {
904 uint32_t v;
905 Sym *sym;
906 } ExprValue;
908 #define MAX_ASM_OPERANDS 30
910 typedef struct ASMOperand {
911 int id; /* GCC 3 optionnal identifier (0 if number only supported */
912 char *constraint;
913 char asm_str[16]; /* computed asm string for operand */
914 SValue *vt; /* C value of the expression */
915 int ref_index; /* if >= 0, gives reference to a output constraint */
916 int input_index; /* if >= 0, gives reference to an input constraint */
917 int priority; /* priority, used to assign registers */
918 int reg; /* if >= 0, register number used for this operand */
919 int is_llong; /* true if double register value */
920 int is_memory; /* true if memory operand */
921 int is_rw; /* for '+' modifier */
922 } ASMOperand;
924 static void asm_expr(TCCState *s1, ExprValue *pe);
925 static int asm_int_expr(TCCState *s1);
926 static int find_constraint(ASMOperand *operands, int nb_operands,
927 const char *name, const char **pp);
929 static int tcc_assemble(TCCState *s1, int do_preprocess);
931 #endif
933 static void asm_instr(void);
934 static void asm_global_instr(void);
936 /* true if float/double/long double type */
937 static inline int is_float(int t)
939 int bt;
940 bt = t & VT_BTYPE;
941 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
944 #ifdef TCC_TARGET_I386
945 #include "i386-gen.c"
946 #endif
948 #ifdef TCC_TARGET_ARM
949 #include "arm-gen.c"
950 #endif
952 #ifdef TCC_TARGET_C67
953 #include "c67-gen.c"
954 #endif
956 #ifdef TCC_TARGET_X86_64
957 #include "x86_64-gen.c"
958 #endif
960 #ifdef CONFIG_TCC_STATIC
962 #define RTLD_LAZY 0x001
963 #define RTLD_NOW 0x002
964 #define RTLD_GLOBAL 0x100
965 #define RTLD_DEFAULT NULL
967 /* dummy function for profiling */
968 void *dlopen(const char *filename, int flag)
970 return NULL;
973 const char *dlerror(void)
975 return "error";
978 typedef struct TCCSyms {
979 char *str;
980 void *ptr;
981 } TCCSyms;
983 #define TCCSYM(a) { #a, &a, },
985 /* add the symbol you want here if no dynamic linking is done */
986 static TCCSyms tcc_syms[] = {
987 #if !defined(CONFIG_TCCBOOT)
988 TCCSYM(printf)
989 TCCSYM(fprintf)
990 TCCSYM(fopen)
991 TCCSYM(fclose)
992 #endif
993 { NULL, NULL },
996 void *resolve_sym(TCCState *s1, const char *symbol, int type)
998 TCCSyms *p;
999 p = tcc_syms;
1000 while (p->str != NULL) {
1001 if (!strcmp(p->str, symbol))
1002 return p->ptr;
1003 p++;
1005 return NULL;
1008 #elif !defined(_WIN32)
1010 #include <dlfcn.h>
1012 void *resolve_sym(TCCState *s1, const char *sym, int type)
1014 return dlsym(RTLD_DEFAULT, sym);
1017 #endif
1019 /********************************************************/
1021 /* we use our own 'finite' function to avoid potential problems with
1022 non standard math libs */
1023 /* XXX: endianness dependent */
1024 int ieee_finite(double d)
1026 int *p = (int *)&d;
1027 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
1030 /* copy a string and truncate it. */
1031 static char *pstrcpy(char *buf, int buf_size, const char *s)
1033 char *q, *q_end;
1034 int c;
1036 if (buf_size > 0) {
1037 q = buf;
1038 q_end = buf + buf_size - 1;
1039 while (q < q_end) {
1040 c = *s++;
1041 if (c == '\0')
1042 break;
1043 *q++ = c;
1045 *q = '\0';
1047 return buf;
1050 /* strcat and truncate. */
1051 static char *pstrcat(char *buf, int buf_size, const char *s)
1053 int len;
1054 len = strlen(buf);
1055 if (len < buf_size)
1056 pstrcpy(buf + len, buf_size - len, s);
1057 return buf;
1060 #ifndef LIBTCC
1061 static int strstart(const char *str, const char *val, const char **ptr)
1063 const char *p, *q;
1064 p = str;
1065 q = val;
1066 while (*q != '\0') {
1067 if (*p != *q)
1068 return 0;
1069 p++;
1070 q++;
1072 if (ptr)
1073 *ptr = p;
1074 return 1;
1076 #endif
1078 /* extract the basename of a file */
1079 static char *tcc_basename(const char *name)
1081 char *p = strchr(name, 0);
1082 while (p > name
1083 && p[-1] != '/'
1084 #ifdef _WIN32
1085 && p[-1] != '\\'
1086 #endif
1088 --p;
1089 return p;
1092 static char *tcc_fileextension (const char *name)
1094 char *b = tcc_basename(name);
1095 char *e = strrchr(b, '.');
1096 return e ? e : strchr(b, 0);
1099 #ifdef _WIN32
1100 char *normalize_slashes(char *path)
1102 char *p;
1103 for (p = path; *p; ++p)
1104 if (*p == '\\')
1105 *p = '/';
1106 return path;
1109 char *w32_tcc_lib_path(void)
1111 /* on win32, we suppose the lib and includes are at the location
1112 of 'tcc.exe' */
1113 char path[1024], *p;
1114 GetModuleFileNameA(NULL, path, sizeof path);
1115 p = tcc_basename(normalize_slashes(strlwr(path)));
1116 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
1117 p -= 5;
1118 else if (p > path)
1119 p--;
1120 *p = 0;
1121 return strdup(path);
1123 #endif
1125 void set_pages_executable(void *ptr, unsigned long length)
1127 #ifdef _WIN32
1128 unsigned long old_protect;
1129 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
1130 #else
1131 unsigned long start, end;
1132 start = (unsigned long)ptr & ~(PAGESIZE - 1);
1133 end = (unsigned long)ptr + length;
1134 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
1135 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
1136 #endif
1139 /* memory management */
1140 #ifdef MEM_DEBUG
1141 int mem_cur_size;
1142 int mem_max_size;
1143 unsigned malloc_usable_size(void*);
1144 #endif
1146 static inline void tcc_free(void *ptr)
1148 #ifdef MEM_DEBUG
1149 mem_cur_size -= malloc_usable_size(ptr);
1150 #endif
1151 free(ptr);
1154 static void *tcc_malloc(unsigned long size)
1156 void *ptr;
1157 ptr = malloc(size);
1158 if (!ptr && size)
1159 error("memory full");
1160 #ifdef MEM_DEBUG
1161 mem_cur_size += malloc_usable_size(ptr);
1162 if (mem_cur_size > mem_max_size)
1163 mem_max_size = mem_cur_size;
1164 #endif
1165 return ptr;
1168 static void *tcc_mallocz(unsigned long size)
1170 void *ptr;
1171 ptr = tcc_malloc(size);
1172 memset(ptr, 0, size);
1173 return ptr;
1176 static inline void *tcc_realloc(void *ptr, unsigned long size)
1178 void *ptr1;
1179 #ifdef MEM_DEBUG
1180 mem_cur_size -= malloc_usable_size(ptr);
1181 #endif
1182 ptr1 = realloc(ptr, size);
1183 #ifdef MEM_DEBUG
1184 /* NOTE: count not correct if alloc error, but not critical */
1185 mem_cur_size += malloc_usable_size(ptr1);
1186 if (mem_cur_size > mem_max_size)
1187 mem_max_size = mem_cur_size;
1188 #endif
1189 return ptr1;
1192 static char *tcc_strdup(const char *str)
1194 char *ptr;
1195 ptr = tcc_malloc(strlen(str) + 1);
1196 strcpy(ptr, str);
1197 return ptr;
1200 #define free(p) use_tcc_free(p)
1201 #define malloc(s) use_tcc_malloc(s)
1202 #define realloc(p, s) use_tcc_realloc(p, s)
1204 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1206 int nb, nb_alloc;
1207 void **pp;
1209 nb = *nb_ptr;
1210 pp = *ptab;
1211 /* every power of two we double array size */
1212 if ((nb & (nb - 1)) == 0) {
1213 if (!nb)
1214 nb_alloc = 1;
1215 else
1216 nb_alloc = nb * 2;
1217 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1218 if (!pp)
1219 error("memory full");
1220 *ptab = pp;
1222 pp[nb++] = data;
1223 *nb_ptr = nb;
1226 static void dynarray_reset(void *pp, int *n)
1228 void **p;
1229 for (p = *(void***)pp; *n; ++p, --*n)
1230 if (*p)
1231 tcc_free(*p);
1232 tcc_free(*(void**)pp);
1233 *(void**)pp = NULL;
1236 /* symbol allocator */
1237 static Sym *__sym_malloc(void)
1239 Sym *sym_pool, *sym, *last_sym;
1240 int i;
1242 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1243 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
1245 last_sym = sym_free_first;
1246 sym = sym_pool;
1247 for(i = 0; i < SYM_POOL_NB; i++) {
1248 sym->next = last_sym;
1249 last_sym = sym;
1250 sym++;
1252 sym_free_first = last_sym;
1253 return last_sym;
1256 static inline Sym *sym_malloc(void)
1258 Sym *sym;
1259 sym = sym_free_first;
1260 if (!sym)
1261 sym = __sym_malloc();
1262 sym_free_first = sym->next;
1263 return sym;
1266 static inline void sym_free(Sym *sym)
1268 sym->next = sym_free_first;
1269 sym_free_first = sym;
1272 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1274 Section *sec;
1276 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1277 strcpy(sec->name, name);
1278 sec->sh_type = sh_type;
1279 sec->sh_flags = sh_flags;
1280 switch(sh_type) {
1281 case SHT_HASH:
1282 case SHT_REL:
1283 case SHT_RELA:
1284 case SHT_DYNSYM:
1285 case SHT_SYMTAB:
1286 case SHT_DYNAMIC:
1287 sec->sh_addralign = 4;
1288 break;
1289 case SHT_STRTAB:
1290 sec->sh_addralign = 1;
1291 break;
1292 default:
1293 sec->sh_addralign = 32; /* default conservative alignment */
1294 break;
1297 /* only add section if not private */
1298 if (!(sh_flags & SHF_PRIVATE)) {
1299 sec->sh_num = s1->nb_sections;
1300 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1302 return sec;
1305 static void free_section(Section *s)
1307 if (s->link && (s->link->sh_flags & SHF_PRIVATE))
1308 free_section(s->link);
1309 if (s->hash && (s->hash->sh_flags & SHF_PRIVATE))
1310 s->hash->link = NULL, free_section(s->hash);
1311 tcc_free(s->data);
1312 tcc_free(s);
1315 /* realloc section and set its content to zero */
1316 static void section_realloc(Section *sec, unsigned long new_size)
1318 unsigned long size;
1319 unsigned char *data;
1321 size = sec->data_allocated;
1322 if (size == 0)
1323 size = 1;
1324 while (size < new_size)
1325 size = size * 2;
1326 data = tcc_realloc(sec->data, size);
1327 if (!data)
1328 error("memory full");
1329 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1330 sec->data = data;
1331 sec->data_allocated = size;
1334 /* reserve at least 'size' bytes in section 'sec' from
1335 sec->data_offset. */
1336 static void *section_ptr_add(Section *sec, unsigned long size)
1338 unsigned long offset, offset1;
1340 offset = sec->data_offset;
1341 offset1 = offset + size;
1342 if (offset1 > sec->data_allocated)
1343 section_realloc(sec, offset1);
1344 sec->data_offset = offset1;
1345 return sec->data + offset;
1348 /* return a reference to a section, and create it if it does not
1349 exists */
1350 Section *find_section(TCCState *s1, const char *name)
1352 Section *sec;
1353 int i;
1354 for(i = 1; i < s1->nb_sections; i++) {
1355 sec = s1->sections[i];
1356 if (!strcmp(name, sec->name))
1357 return sec;
1359 /* sections are created as PROGBITS */
1360 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1363 #define SECTION_ABS ((void *)1)
1365 /* update sym->c so that it points to an external symbol in section
1366 'section' with value 'value' */
1367 static void put_extern_sym2(Sym *sym, Section *section,
1368 unsigned long value, unsigned long size,
1369 int can_add_underscore)
1371 int sym_type, sym_bind, sh_num, info, other, attr;
1372 ElfW(Sym) *esym;
1373 const char *name;
1374 char buf1[256];
1376 if (section == NULL)
1377 sh_num = SHN_UNDEF;
1378 else if (section == SECTION_ABS)
1379 sh_num = SHN_ABS;
1380 else
1381 sh_num = section->sh_num;
1383 other = attr = 0;
1385 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1386 sym_type = STT_FUNC;
1387 #ifdef TCC_TARGET_PE
1388 if (sym->type.ref)
1389 attr = sym->type.ref->r;
1390 if (FUNC_EXPORT(attr))
1391 other |= 1;
1392 if (FUNC_CALL(attr) == FUNC_STDCALL)
1393 other |= 2;
1394 #endif
1395 } else {
1396 sym_type = STT_OBJECT;
1399 if (sym->type.t & VT_STATIC)
1400 sym_bind = STB_LOCAL;
1401 else
1402 sym_bind = STB_GLOBAL;
1404 if (!sym->c) {
1405 name = get_tok_str(sym->v, NULL);
1406 #ifdef CONFIG_TCC_BCHECK
1407 if (do_bounds_check) {
1408 char buf[32];
1410 /* XXX: avoid doing that for statics ? */
1411 /* if bound checking is activated, we change some function
1412 names by adding the "__bound" prefix */
1413 switch(sym->v) {
1414 #if 0
1415 /* XXX: we rely only on malloc hooks */
1416 case TOK_malloc:
1417 case TOK_free:
1418 case TOK_realloc:
1419 case TOK_memalign:
1420 case TOK_calloc:
1421 #endif
1422 case TOK_memcpy:
1423 case TOK_memmove:
1424 case TOK_memset:
1425 case TOK_strlen:
1426 case TOK_strcpy:
1427 case TOK__alloca:
1428 strcpy(buf, "__bound_");
1429 strcat(buf, name);
1430 name = buf;
1431 break;
1434 #endif
1436 #ifdef TCC_TARGET_PE
1437 if ((other & 2) && can_add_underscore) {
1438 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1439 name = buf1;
1440 } else
1441 #endif
1442 if (tcc_state->leading_underscore && can_add_underscore) {
1443 buf1[0] = '_';
1444 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1445 name = buf1;
1447 info = ELFW(ST_INFO)(sym_bind, sym_type);
1448 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1449 } else {
1450 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
1451 esym->st_value = value;
1452 esym->st_size = size;
1453 esym->st_shndx = sh_num;
1454 esym->st_other |= other;
1458 static void put_extern_sym(Sym *sym, Section *section,
1459 unsigned long value, unsigned long size)
1461 put_extern_sym2(sym, section, value, size, 1);
1464 /* add a new relocation entry to symbol 'sym' in section 's' */
1465 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1467 if (!sym->c)
1468 put_extern_sym(sym, NULL, 0, 0);
1469 /* now we can add ELF relocation info */
1470 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1473 static inline int isid(int c)
1475 return (c >= 'a' && c <= 'z') ||
1476 (c >= 'A' && c <= 'Z') ||
1477 c == '_';
1480 static inline int isnum(int c)
1482 return c >= '0' && c <= '9';
1485 static inline int isoct(int c)
1487 return c >= '0' && c <= '7';
1490 static inline int toup(int c)
1492 if (c >= 'a' && c <= 'z')
1493 return c - 'a' + 'A';
1494 else
1495 return c;
1498 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1500 int len;
1501 len = strlen(buf);
1502 vsnprintf(buf + len, buf_size - len, fmt, ap);
1505 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1507 va_list ap;
1508 va_start(ap, fmt);
1509 strcat_vprintf(buf, buf_size, fmt, ap);
1510 va_end(ap);
1513 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1515 char buf[2048];
1516 BufferedFile **f;
1518 buf[0] = '\0';
1519 if (file) {
1520 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1521 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1522 (*f)->filename, (*f)->line_num);
1523 if (file->line_num > 0) {
1524 strcat_printf(buf, sizeof(buf),
1525 "%s:%d: ", file->filename, file->line_num);
1526 } else {
1527 strcat_printf(buf, sizeof(buf),
1528 "%s: ", file->filename);
1530 } else {
1531 strcat_printf(buf, sizeof(buf),
1532 "tcc: ");
1534 if (is_warning)
1535 strcat_printf(buf, sizeof(buf), "warning: ");
1536 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1538 if (!s1->error_func) {
1539 /* default case: stderr */
1540 fprintf(stderr, "%s\n", buf);
1541 } else {
1542 s1->error_func(s1->error_opaque, buf);
1544 if (!is_warning || s1->warn_error)
1545 s1->nb_errors++;
1548 #ifdef LIBTCC
1549 void tcc_set_error_func(TCCState *s, void *error_opaque,
1550 void (*error_func)(void *opaque, const char *msg))
1552 s->error_opaque = error_opaque;
1553 s->error_func = error_func;
1555 #endif
1557 /* error without aborting current compilation */
1558 void error_noabort(const char *fmt, ...)
1560 TCCState *s1 = tcc_state;
1561 va_list ap;
1563 va_start(ap, fmt);
1564 error1(s1, 0, fmt, ap);
1565 va_end(ap);
1568 void error(const char *fmt, ...)
1570 TCCState *s1 = tcc_state;
1571 va_list ap;
1573 va_start(ap, fmt);
1574 error1(s1, 0, fmt, ap);
1575 va_end(ap);
1576 /* better than nothing: in some cases, we accept to handle errors */
1577 if (s1->error_set_jmp_enabled) {
1578 longjmp(s1->error_jmp_buf, 1);
1579 } else {
1580 /* XXX: eliminate this someday */
1581 exit(1);
1585 void expect(const char *msg)
1587 error("%s expected", msg);
1590 void warning(const char *fmt, ...)
1592 TCCState *s1 = tcc_state;
1593 va_list ap;
1595 if (s1->warn_none)
1596 return;
1598 va_start(ap, fmt);
1599 error1(s1, 1, fmt, ap);
1600 va_end(ap);
1603 void skip(int c)
1605 if (tok != c)
1606 error("'%c' expected", c);
1607 next();
1610 static void test_lvalue(void)
1612 if (!(vtop->r & VT_LVAL))
1613 expect("lvalue");
1616 /* allocate a new token */
1617 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1619 TokenSym *ts, **ptable;
1620 int i;
1622 if (tok_ident >= SYM_FIRST_ANOM)
1623 error("memory full");
1625 /* expand token table if needed */
1626 i = tok_ident - TOK_IDENT;
1627 if ((i % TOK_ALLOC_INCR) == 0) {
1628 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1629 if (!ptable)
1630 error("memory full");
1631 table_ident = ptable;
1634 ts = tcc_malloc(sizeof(TokenSym) + len);
1635 table_ident[i] = ts;
1636 ts->tok = tok_ident++;
1637 ts->sym_define = NULL;
1638 ts->sym_label = NULL;
1639 ts->sym_struct = NULL;
1640 ts->sym_identifier = NULL;
1641 ts->len = len;
1642 ts->hash_next = NULL;
1643 memcpy(ts->str, str, len);
1644 ts->str[len] = '\0';
1645 *pts = ts;
1646 return ts;
1649 #define TOK_HASH_INIT 1
1650 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1652 /* find a token and add it if not found */
1653 static TokenSym *tok_alloc(const char *str, int len)
1655 TokenSym *ts, **pts;
1656 int i;
1657 unsigned int h;
1659 h = TOK_HASH_INIT;
1660 for(i=0;i<len;i++)
1661 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1662 h &= (TOK_HASH_SIZE - 1);
1664 pts = &hash_ident[h];
1665 for(;;) {
1666 ts = *pts;
1667 if (!ts)
1668 break;
1669 if (ts->len == len && !memcmp(ts->str, str, len))
1670 return ts;
1671 pts = &(ts->hash_next);
1673 return tok_alloc_new(pts, str, len);
1676 /* CString handling */
1678 static void cstr_realloc(CString *cstr, int new_size)
1680 int size;
1681 void *data;
1683 size = cstr->size_allocated;
1684 if (size == 0)
1685 size = 8; /* no need to allocate a too small first string */
1686 while (size < new_size)
1687 size = size * 2;
1688 data = tcc_realloc(cstr->data_allocated, size);
1689 if (!data)
1690 error("memory full");
1691 cstr->data_allocated = data;
1692 cstr->size_allocated = size;
1693 cstr->data = data;
1696 /* add a byte */
1697 static inline void cstr_ccat(CString *cstr, int ch)
1699 int size;
1700 size = cstr->size + 1;
1701 if (size > cstr->size_allocated)
1702 cstr_realloc(cstr, size);
1703 ((unsigned char *)cstr->data)[size - 1] = ch;
1704 cstr->size = size;
1707 static void cstr_cat(CString *cstr, const char *str)
1709 int c;
1710 for(;;) {
1711 c = *str;
1712 if (c == '\0')
1713 break;
1714 cstr_ccat(cstr, c);
1715 str++;
1719 /* add a wide char */
1720 static void cstr_wccat(CString *cstr, int ch)
1722 int size;
1723 size = cstr->size + sizeof(nwchar_t);
1724 if (size > cstr->size_allocated)
1725 cstr_realloc(cstr, size);
1726 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
1727 cstr->size = size;
1730 static void cstr_new(CString *cstr)
1732 memset(cstr, 0, sizeof(CString));
1735 /* free string and reset it to NULL */
1736 static void cstr_free(CString *cstr)
1738 tcc_free(cstr->data_allocated);
1739 cstr_new(cstr);
1742 #define cstr_reset(cstr) cstr_free(cstr)
1744 /* XXX: unicode ? */
1745 static void add_char(CString *cstr, int c)
1747 if (c == '\'' || c == '\"' || c == '\\') {
1748 /* XXX: could be more precise if char or string */
1749 cstr_ccat(cstr, '\\');
1751 if (c >= 32 && c <= 126) {
1752 cstr_ccat(cstr, c);
1753 } else {
1754 cstr_ccat(cstr, '\\');
1755 if (c == '\n') {
1756 cstr_ccat(cstr, 'n');
1757 } else {
1758 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1759 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1760 cstr_ccat(cstr, '0' + (c & 7));
1765 /* XXX: buffer overflow */
1766 /* XXX: float tokens */
1767 char *get_tok_str(int v, CValue *cv)
1769 static char buf[STRING_MAX_SIZE + 1];
1770 static CString cstr_buf;
1771 CString *cstr;
1772 unsigned char *q;
1773 char *p;
1774 int i, len;
1776 /* NOTE: to go faster, we give a fixed buffer for small strings */
1777 cstr_reset(&cstr_buf);
1778 cstr_buf.data = buf;
1779 cstr_buf.size_allocated = sizeof(buf);
1780 p = buf;
1782 switch(v) {
1783 case TOK_CINT:
1784 case TOK_CUINT:
1785 /* XXX: not quite exact, but only useful for testing */
1786 sprintf(p, "%u", cv->ui);
1787 break;
1788 case TOK_CLLONG:
1789 case TOK_CULLONG:
1790 /* XXX: not quite exact, but only useful for testing */
1791 sprintf(p, "%Lu", cv->ull);
1792 break;
1793 case TOK_LCHAR:
1794 cstr_ccat(&cstr_buf, 'L');
1795 case TOK_CCHAR:
1796 cstr_ccat(&cstr_buf, '\'');
1797 add_char(&cstr_buf, cv->i);
1798 cstr_ccat(&cstr_buf, '\'');
1799 cstr_ccat(&cstr_buf, '\0');
1800 break;
1801 case TOK_PPNUM:
1802 cstr = cv->cstr;
1803 len = cstr->size - 1;
1804 for(i=0;i<len;i++)
1805 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1806 cstr_ccat(&cstr_buf, '\0');
1807 break;
1808 case TOK_LSTR:
1809 cstr_ccat(&cstr_buf, 'L');
1810 case TOK_STR:
1811 cstr = cv->cstr;
1812 cstr_ccat(&cstr_buf, '\"');
1813 if (v == TOK_STR) {
1814 len = cstr->size - 1;
1815 for(i=0;i<len;i++)
1816 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1817 } else {
1818 len = (cstr->size / sizeof(nwchar_t)) - 1;
1819 for(i=0;i<len;i++)
1820 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1822 cstr_ccat(&cstr_buf, '\"');
1823 cstr_ccat(&cstr_buf, '\0');
1824 break;
1825 case TOK_LT:
1826 v = '<';
1827 goto addv;
1828 case TOK_GT:
1829 v = '>';
1830 goto addv;
1831 case TOK_DOTS:
1832 return strcpy(p, "...");
1833 case TOK_A_SHL:
1834 return strcpy(p, "<<=");
1835 case TOK_A_SAR:
1836 return strcpy(p, ">>=");
1837 default:
1838 if (v < TOK_IDENT) {
1839 /* search in two bytes table */
1840 q = tok_two_chars;
1841 while (*q) {
1842 if (q[2] == v) {
1843 *p++ = q[0];
1844 *p++ = q[1];
1845 *p = '\0';
1846 return buf;
1848 q += 3;
1850 addv:
1851 *p++ = v;
1852 *p = '\0';
1853 } else if (v < tok_ident) {
1854 return table_ident[v - TOK_IDENT]->str;
1855 } else if (v >= SYM_FIRST_ANOM) {
1856 /* special name for anonymous symbol */
1857 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1858 } else {
1859 /* should never happen */
1860 return NULL;
1862 break;
1864 return cstr_buf.data;
1867 /* push, without hashing */
1868 static Sym *sym_push2(Sym **ps, int v, int t, long c)
1870 Sym *s;
1871 s = sym_malloc();
1872 s->v = v;
1873 s->type.t = t;
1874 s->c = c;
1875 s->next = NULL;
1876 /* add in stack */
1877 s->prev = *ps;
1878 *ps = s;
1879 return s;
1882 /* find a symbol and return its associated structure. 's' is the top
1883 of the symbol stack */
1884 static Sym *sym_find2(Sym *s, int v)
1886 while (s) {
1887 if (s->v == v)
1888 return s;
1889 s = s->prev;
1891 return NULL;
1894 /* structure lookup */
1895 static inline Sym *struct_find(int v)
1897 v -= TOK_IDENT;
1898 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1899 return NULL;
1900 return table_ident[v]->sym_struct;
1903 /* find an identifier */
1904 static inline Sym *sym_find(int v)
1906 v -= TOK_IDENT;
1907 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1908 return NULL;
1909 return table_ident[v]->sym_identifier;
1912 /* push a given symbol on the symbol stack */
1913 static Sym *sym_push(int v, CType *type, int r, int c)
1915 Sym *s, **ps;
1916 TokenSym *ts;
1918 if (local_stack)
1919 ps = &local_stack;
1920 else
1921 ps = &global_stack;
1922 s = sym_push2(ps, v, type->t, c);
1923 s->type.ref = type->ref;
1924 s->r = r;
1925 /* don't record fields or anonymous symbols */
1926 /* XXX: simplify */
1927 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1928 /* record symbol in token array */
1929 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1930 if (v & SYM_STRUCT)
1931 ps = &ts->sym_struct;
1932 else
1933 ps = &ts->sym_identifier;
1934 s->prev_tok = *ps;
1935 *ps = s;
1937 return s;
1940 /* push a global identifier */
1941 static Sym *global_identifier_push(int v, int t, int c)
1943 Sym *s, **ps;
1944 s = sym_push2(&global_stack, v, t, c);
1945 /* don't record anonymous symbol */
1946 if (v < SYM_FIRST_ANOM) {
1947 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1948 /* modify the top most local identifier, so that
1949 sym_identifier will point to 's' when popped */
1950 while (*ps != NULL)
1951 ps = &(*ps)->prev_tok;
1952 s->prev_tok = NULL;
1953 *ps = s;
1955 return s;
1958 /* pop symbols until top reaches 'b' */
1959 static void sym_pop(Sym **ptop, Sym *b)
1961 Sym *s, *ss, **ps;
1962 TokenSym *ts;
1963 int v;
1965 s = *ptop;
1966 while(s != b) {
1967 ss = s->prev;
1968 v = s->v;
1969 /* remove symbol in token array */
1970 /* XXX: simplify */
1971 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1972 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1973 if (v & SYM_STRUCT)
1974 ps = &ts->sym_struct;
1975 else
1976 ps = &ts->sym_identifier;
1977 *ps = s->prev_tok;
1979 sym_free(s);
1980 s = ss;
1982 *ptop = b;
1985 /* I/O layer */
1987 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1989 int fd;
1990 BufferedFile *bf;
1992 if (strcmp(filename, "-") == 0)
1993 fd = 0, filename = "stdin";
1994 else
1995 fd = open(filename, O_RDONLY | O_BINARY);
1996 if ((verbose == 2 && fd >= 0) || verbose == 3)
1997 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
1998 (s1->include_stack_ptr - s1->include_stack), "", filename);
1999 if (fd < 0)
2000 return NULL;
2001 bf = tcc_malloc(sizeof(BufferedFile));
2002 bf->fd = fd;
2003 bf->buf_ptr = bf->buffer;
2004 bf->buf_end = bf->buffer;
2005 bf->buffer[0] = CH_EOB; /* put eob symbol */
2006 pstrcpy(bf->filename, sizeof(bf->filename), filename);
2007 #ifdef _WIN32
2008 normalize_slashes(bf->filename);
2009 #endif
2010 bf->line_num = 1;
2011 bf->ifndef_macro = 0;
2012 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2013 // printf("opening '%s'\n", filename);
2014 return bf;
2017 void tcc_close(BufferedFile *bf)
2019 total_lines += bf->line_num;
2020 close(bf->fd);
2021 tcc_free(bf);
2024 /* fill input buffer and peek next char */
2025 static int tcc_peekc_slow(BufferedFile *bf)
2027 int len;
2028 /* only tries to read if really end of buffer */
2029 if (bf->buf_ptr >= bf->buf_end) {
2030 if (bf->fd != -1) {
2031 #if defined(PARSE_DEBUG)
2032 len = 8;
2033 #else
2034 len = IO_BUF_SIZE;
2035 #endif
2036 len = read(bf->fd, bf->buffer, len);
2037 if (len < 0)
2038 len = 0;
2039 } else {
2040 len = 0;
2042 total_bytes += len;
2043 bf->buf_ptr = bf->buffer;
2044 bf->buf_end = bf->buffer + len;
2045 *bf->buf_end = CH_EOB;
2047 if (bf->buf_ptr < bf->buf_end) {
2048 return bf->buf_ptr[0];
2049 } else {
2050 bf->buf_ptr = bf->buf_end;
2051 return CH_EOF;
2055 /* return the current character, handling end of block if necessary
2056 (but not stray) */
2057 static int handle_eob(void)
2059 return tcc_peekc_slow(file);
2062 /* read next char from current input file and handle end of input buffer */
2063 static inline void inp(void)
2065 ch = *(++(file->buf_ptr));
2066 /* end of buffer/file handling */
2067 if (ch == CH_EOB)
2068 ch = handle_eob();
2071 /* handle '\[\r]\n' */
2072 static int handle_stray_noerror(void)
2074 while (ch == '\\') {
2075 inp();
2076 if (ch == '\n') {
2077 file->line_num++;
2078 inp();
2079 } else if (ch == '\r') {
2080 inp();
2081 if (ch != '\n')
2082 goto fail;
2083 file->line_num++;
2084 inp();
2085 } else {
2086 fail:
2087 return 1;
2090 return 0;
2093 static void handle_stray(void)
2095 if (handle_stray_noerror())
2096 error("stray '\\' in program");
2099 /* skip the stray and handle the \\n case. Output an error if
2100 incorrect char after the stray */
2101 static int handle_stray1(uint8_t *p)
2103 int c;
2105 if (p >= file->buf_end) {
2106 file->buf_ptr = p;
2107 c = handle_eob();
2108 p = file->buf_ptr;
2109 if (c == '\\')
2110 goto parse_stray;
2111 } else {
2112 parse_stray:
2113 file->buf_ptr = p;
2114 ch = *p;
2115 handle_stray();
2116 p = file->buf_ptr;
2117 c = *p;
2119 return c;
2122 /* handle just the EOB case, but not stray */
2123 #define PEEKC_EOB(c, p)\
2125 p++;\
2126 c = *p;\
2127 if (c == '\\') {\
2128 file->buf_ptr = p;\
2129 c = handle_eob();\
2130 p = file->buf_ptr;\
2134 /* handle the complicated stray case */
2135 #define PEEKC(c, p)\
2137 p++;\
2138 c = *p;\
2139 if (c == '\\') {\
2140 c = handle_stray1(p);\
2141 p = file->buf_ptr;\
2145 /* input with '\[\r]\n' handling. Note that this function cannot
2146 handle other characters after '\', so you cannot call it inside
2147 strings or comments */
2148 static void minp(void)
2150 inp();
2151 if (ch == '\\')
2152 handle_stray();
2156 /* single line C++ comments */
2157 static uint8_t *parse_line_comment(uint8_t *p)
2159 int c;
2161 p++;
2162 for(;;) {
2163 c = *p;
2164 redo:
2165 if (c == '\n' || c == CH_EOF) {
2166 break;
2167 } else if (c == '\\') {
2168 file->buf_ptr = p;
2169 c = handle_eob();
2170 p = file->buf_ptr;
2171 if (c == '\\') {
2172 PEEKC_EOB(c, p);
2173 if (c == '\n') {
2174 file->line_num++;
2175 PEEKC_EOB(c, p);
2176 } else if (c == '\r') {
2177 PEEKC_EOB(c, p);
2178 if (c == '\n') {
2179 file->line_num++;
2180 PEEKC_EOB(c, p);
2183 } else {
2184 goto redo;
2186 } else {
2187 p++;
2190 return p;
2193 /* C comments */
2194 static uint8_t *parse_comment(uint8_t *p)
2196 int c;
2198 p++;
2199 for(;;) {
2200 /* fast skip loop */
2201 for(;;) {
2202 c = *p;
2203 if (c == '\n' || c == '*' || c == '\\')
2204 break;
2205 p++;
2206 c = *p;
2207 if (c == '\n' || c == '*' || c == '\\')
2208 break;
2209 p++;
2211 /* now we can handle all the cases */
2212 if (c == '\n') {
2213 file->line_num++;
2214 p++;
2215 } else if (c == '*') {
2216 p++;
2217 for(;;) {
2218 c = *p;
2219 if (c == '*') {
2220 p++;
2221 } else if (c == '/') {
2222 goto end_of_comment;
2223 } else if (c == '\\') {
2224 file->buf_ptr = p;
2225 c = handle_eob();
2226 p = file->buf_ptr;
2227 if (c == '\\') {
2228 /* skip '\[\r]\n', otherwise just skip the stray */
2229 while (c == '\\') {
2230 PEEKC_EOB(c, p);
2231 if (c == '\n') {
2232 file->line_num++;
2233 PEEKC_EOB(c, p);
2234 } else if (c == '\r') {
2235 PEEKC_EOB(c, p);
2236 if (c == '\n') {
2237 file->line_num++;
2238 PEEKC_EOB(c, p);
2240 } else {
2241 goto after_star;
2245 } else {
2246 break;
2249 after_star: ;
2250 } else {
2251 /* stray, eob or eof */
2252 file->buf_ptr = p;
2253 c = handle_eob();
2254 p = file->buf_ptr;
2255 if (c == CH_EOF) {
2256 error("unexpected end of file in comment");
2257 } else if (c == '\\') {
2258 p++;
2262 end_of_comment:
2263 p++;
2264 return p;
2267 #define cinp minp
2269 /* space exlcuding newline */
2270 static inline int is_space(int ch)
2272 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2275 static inline void skip_spaces(void)
2277 while (is_space(ch))
2278 cinp();
2281 /* parse a string without interpreting escapes */
2282 static uint8_t *parse_pp_string(uint8_t *p,
2283 int sep, CString *str)
2285 int c;
2286 p++;
2287 for(;;) {
2288 c = *p;
2289 if (c == sep) {
2290 break;
2291 } else if (c == '\\') {
2292 file->buf_ptr = p;
2293 c = handle_eob();
2294 p = file->buf_ptr;
2295 if (c == CH_EOF) {
2296 unterminated_string:
2297 /* XXX: indicate line number of start of string */
2298 error("missing terminating %c character", sep);
2299 } else if (c == '\\') {
2300 /* escape : just skip \[\r]\n */
2301 PEEKC_EOB(c, p);
2302 if (c == '\n') {
2303 file->line_num++;
2304 p++;
2305 } else if (c == '\r') {
2306 PEEKC_EOB(c, p);
2307 if (c != '\n')
2308 expect("'\n' after '\r'");
2309 file->line_num++;
2310 p++;
2311 } else if (c == CH_EOF) {
2312 goto unterminated_string;
2313 } else {
2314 if (str) {
2315 cstr_ccat(str, '\\');
2316 cstr_ccat(str, c);
2318 p++;
2321 } else if (c == '\n') {
2322 file->line_num++;
2323 goto add_char;
2324 } else if (c == '\r') {
2325 PEEKC_EOB(c, p);
2326 if (c != '\n') {
2327 if (str)
2328 cstr_ccat(str, '\r');
2329 } else {
2330 file->line_num++;
2331 goto add_char;
2333 } else {
2334 add_char:
2335 if (str)
2336 cstr_ccat(str, c);
2337 p++;
2340 p++;
2341 return p;
2344 /* skip block of text until #else, #elif or #endif. skip also pairs of
2345 #if/#endif */
2346 void preprocess_skip(void)
2348 int a, start_of_line, c, in_warn_or_error;
2349 uint8_t *p;
2351 p = file->buf_ptr;
2352 a = 0;
2353 redo_start:
2354 start_of_line = 1;
2355 in_warn_or_error = 0;
2356 for(;;) {
2357 redo_no_start:
2358 c = *p;
2359 switch(c) {
2360 case ' ':
2361 case '\t':
2362 case '\f':
2363 case '\v':
2364 case '\r':
2365 p++;
2366 goto redo_no_start;
2367 case '\n':
2368 file->line_num++;
2369 p++;
2370 goto redo_start;
2371 case '\\':
2372 file->buf_ptr = p;
2373 c = handle_eob();
2374 if (c == CH_EOF) {
2375 expect("#endif");
2376 } else if (c == '\\') {
2377 ch = file->buf_ptr[0];
2378 handle_stray_noerror();
2380 p = file->buf_ptr;
2381 goto redo_no_start;
2382 /* skip strings */
2383 case '\"':
2384 case '\'':
2385 if (in_warn_or_error)
2386 goto _default;
2387 p = parse_pp_string(p, c, NULL);
2388 break;
2389 /* skip comments */
2390 case '/':
2391 if (in_warn_or_error)
2392 goto _default;
2393 file->buf_ptr = p;
2394 ch = *p;
2395 minp();
2396 p = file->buf_ptr;
2397 if (ch == '*') {
2398 p = parse_comment(p);
2399 } else if (ch == '/') {
2400 p = parse_line_comment(p);
2402 break;
2403 case '#':
2404 p++;
2405 if (start_of_line) {
2406 file->buf_ptr = p;
2407 next_nomacro();
2408 p = file->buf_ptr;
2409 if (a == 0 &&
2410 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2411 goto the_end;
2412 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2413 a++;
2414 else if (tok == TOK_ENDIF)
2415 a--;
2416 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2417 in_warn_or_error = 1;
2419 break;
2420 _default:
2421 default:
2422 p++;
2423 break;
2425 start_of_line = 0;
2427 the_end: ;
2428 file->buf_ptr = p;
2431 /* ParseState handling */
2433 /* XXX: currently, no include file info is stored. Thus, we cannot display
2434 accurate messages if the function or data definition spans multiple
2435 files */
2437 /* save current parse state in 's' */
2438 void save_parse_state(ParseState *s)
2440 s->line_num = file->line_num;
2441 s->macro_ptr = macro_ptr;
2442 s->tok = tok;
2443 s->tokc = tokc;
2446 /* restore parse state from 's' */
2447 void restore_parse_state(ParseState *s)
2449 file->line_num = s->line_num;
2450 macro_ptr = s->macro_ptr;
2451 tok = s->tok;
2452 tokc = s->tokc;
2455 /* return the number of additional 'ints' necessary to store the
2456 token */
2457 static inline int tok_ext_size(int t)
2459 switch(t) {
2460 /* 4 bytes */
2461 case TOK_CINT:
2462 case TOK_CUINT:
2463 case TOK_CCHAR:
2464 case TOK_LCHAR:
2465 case TOK_CFLOAT:
2466 case TOK_LINENUM:
2467 return 1;
2468 case TOK_STR:
2469 case TOK_LSTR:
2470 case TOK_PPNUM:
2471 error("unsupported token");
2472 return 1;
2473 case TOK_CDOUBLE:
2474 case TOK_CLLONG:
2475 case TOK_CULLONG:
2476 return 2;
2477 case TOK_CLDOUBLE:
2478 return LDOUBLE_SIZE / 4;
2479 default:
2480 return 0;
2484 /* token string handling */
2486 static inline void tok_str_new(TokenString *s)
2488 s->str = NULL;
2489 s->len = 0;
2490 s->allocated_len = 0;
2491 s->last_line_num = -1;
2494 static void tok_str_free(int *str)
2496 tcc_free(str);
2499 static int *tok_str_realloc(TokenString *s)
2501 int *str, len;
2503 if (s->allocated_len == 0) {
2504 len = 8;
2505 } else {
2506 len = s->allocated_len * 2;
2508 str = tcc_realloc(s->str, len * sizeof(int));
2509 if (!str)
2510 error("memory full");
2511 s->allocated_len = len;
2512 s->str = str;
2513 return str;
2516 static void tok_str_add(TokenString *s, int t)
2518 int len, *str;
2520 len = s->len;
2521 str = s->str;
2522 if (len >= s->allocated_len)
2523 str = tok_str_realloc(s);
2524 str[len++] = t;
2525 s->len = len;
2528 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2530 int len, *str;
2532 len = s->len;
2533 str = s->str;
2535 /* allocate space for worst case */
2536 if (len + TOK_MAX_SIZE > s->allocated_len)
2537 str = tok_str_realloc(s);
2538 str[len++] = t;
2539 switch(t) {
2540 case TOK_CINT:
2541 case TOK_CUINT:
2542 case TOK_CCHAR:
2543 case TOK_LCHAR:
2544 case TOK_CFLOAT:
2545 case TOK_LINENUM:
2546 str[len++] = cv->tab[0];
2547 break;
2548 case TOK_PPNUM:
2549 case TOK_STR:
2550 case TOK_LSTR:
2552 int nb_words;
2553 CString *cstr;
2555 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2556 while ((len + nb_words) > s->allocated_len)
2557 str = tok_str_realloc(s);
2558 cstr = (CString *)(str + len);
2559 cstr->data = NULL;
2560 cstr->size = cv->cstr->size;
2561 cstr->data_allocated = NULL;
2562 cstr->size_allocated = cstr->size;
2563 memcpy((char *)cstr + sizeof(CString),
2564 cv->cstr->data, cstr->size);
2565 len += nb_words;
2567 break;
2568 case TOK_CDOUBLE:
2569 case TOK_CLLONG:
2570 case TOK_CULLONG:
2571 #if LDOUBLE_SIZE == 8
2572 case TOK_CLDOUBLE:
2573 #endif
2574 str[len++] = cv->tab[0];
2575 str[len++] = cv->tab[1];
2576 break;
2577 #if LDOUBLE_SIZE == 12
2578 case TOK_CLDOUBLE:
2579 str[len++] = cv->tab[0];
2580 str[len++] = cv->tab[1];
2581 str[len++] = cv->tab[2];
2582 #elif LDOUBLE_SIZE == 16
2583 case TOK_CLDOUBLE:
2584 str[len++] = cv->tab[0];
2585 str[len++] = cv->tab[1];
2586 str[len++] = cv->tab[2];
2587 str[len++] = cv->tab[3];
2588 #elif LDOUBLE_SIZE != 8
2589 #error add long double size support
2590 #endif
2591 break;
2592 default:
2593 break;
2595 s->len = len;
2598 /* add the current parse token in token string 's' */
2599 static void tok_str_add_tok(TokenString *s)
2601 CValue cval;
2603 /* save line number info */
2604 if (file->line_num != s->last_line_num) {
2605 s->last_line_num = file->line_num;
2606 cval.i = s->last_line_num;
2607 tok_str_add2(s, TOK_LINENUM, &cval);
2609 tok_str_add2(s, tok, &tokc);
2612 #if LDOUBLE_SIZE == 16
2613 #define LDOUBLE_GET(p, cv) \
2614 cv.tab[0] = p[0]; \
2615 cv.tab[1] = p[1]; \
2616 cv.tab[2] = p[2]; \
2617 cv.tab[3] = p[3];
2618 #elif LDOUBLE_SIZE == 12
2619 #define LDOUBLE_GET(p, cv) \
2620 cv.tab[0] = p[0]; \
2621 cv.tab[1] = p[1]; \
2622 cv.tab[2] = p[2];
2623 #elif LDOUBLE_SIZE == 8
2624 #define LDOUBLE_GET(p, cv) \
2625 cv.tab[0] = p[0]; \
2626 cv.tab[1] = p[1];
2627 #else
2628 #error add long double size support
2629 #endif
2632 /* get a token from an integer array and increment pointer
2633 accordingly. we code it as a macro to avoid pointer aliasing. */
2634 #define TOK_GET(t, p, cv) \
2636 t = *p++; \
2637 switch(t) { \
2638 case TOK_CINT: \
2639 case TOK_CUINT: \
2640 case TOK_CCHAR: \
2641 case TOK_LCHAR: \
2642 case TOK_CFLOAT: \
2643 case TOK_LINENUM: \
2644 cv.tab[0] = *p++; \
2645 break; \
2646 case TOK_STR: \
2647 case TOK_LSTR: \
2648 case TOK_PPNUM: \
2649 cv.cstr = (CString *)p; \
2650 cv.cstr->data = (char *)p + sizeof(CString);\
2651 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2652 break; \
2653 case TOK_CDOUBLE: \
2654 case TOK_CLLONG: \
2655 case TOK_CULLONG: \
2656 cv.tab[0] = p[0]; \
2657 cv.tab[1] = p[1]; \
2658 p += 2; \
2659 break; \
2660 case TOK_CLDOUBLE: \
2661 LDOUBLE_GET(p, cv); \
2662 p += LDOUBLE_SIZE / 4; \
2663 break; \
2664 default: \
2665 break; \
2669 /* defines handling */
2670 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2672 Sym *s;
2674 s = sym_push2(&define_stack, v, macro_type, (long)str);
2675 s->next = first_arg;
2676 table_ident[v - TOK_IDENT]->sym_define = s;
2679 /* undefined a define symbol. Its name is just set to zero */
2680 static void define_undef(Sym *s)
2682 int v;
2683 v = s->v;
2684 if (v >= TOK_IDENT && v < tok_ident)
2685 table_ident[v - TOK_IDENT]->sym_define = NULL;
2686 s->v = 0;
2689 static inline Sym *define_find(int v)
2691 v -= TOK_IDENT;
2692 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2693 return NULL;
2694 return table_ident[v]->sym_define;
2697 /* free define stack until top reaches 'b' */
2698 static void free_defines(Sym *b)
2700 Sym *top, *top1;
2701 int v;
2703 top = define_stack;
2704 while (top != b) {
2705 top1 = top->prev;
2706 /* do not free args or predefined defines */
2707 if (top->c)
2708 tok_str_free((int *)top->c);
2709 v = top->v;
2710 if (v >= TOK_IDENT && v < tok_ident)
2711 table_ident[v - TOK_IDENT]->sym_define = NULL;
2712 sym_free(top);
2713 top = top1;
2715 define_stack = b;
2718 /* label lookup */
2719 static Sym *label_find(int v)
2721 v -= TOK_IDENT;
2722 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2723 return NULL;
2724 return table_ident[v]->sym_label;
2727 static Sym *label_push(Sym **ptop, int v, int flags)
2729 Sym *s, **ps;
2730 s = sym_push2(ptop, v, 0, 0);
2731 s->r = flags;
2732 ps = &table_ident[v - TOK_IDENT]->sym_label;
2733 if (ptop == &global_label_stack) {
2734 /* modify the top most local identifier, so that
2735 sym_identifier will point to 's' when popped */
2736 while (*ps != NULL)
2737 ps = &(*ps)->prev_tok;
2739 s->prev_tok = *ps;
2740 *ps = s;
2741 return s;
2744 /* pop labels until element last is reached. Look if any labels are
2745 undefined. Define symbols if '&&label' was used. */
2746 static void label_pop(Sym **ptop, Sym *slast)
2748 Sym *s, *s1;
2749 for(s = *ptop; s != slast; s = s1) {
2750 s1 = s->prev;
2751 if (s->r == LABEL_DECLARED) {
2752 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2753 } else if (s->r == LABEL_FORWARD) {
2754 error("label '%s' used but not defined",
2755 get_tok_str(s->v, NULL));
2756 } else {
2757 if (s->c) {
2758 /* define corresponding symbol. A size of
2759 1 is put. */
2760 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2763 /* remove label */
2764 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2765 sym_free(s);
2767 *ptop = slast;
2770 /* eval an expression for #if/#elif */
2771 static int expr_preprocess(void)
2773 int c, t;
2774 TokenString str;
2776 tok_str_new(&str);
2777 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2778 next(); /* do macro subst */
2779 if (tok == TOK_DEFINED) {
2780 next_nomacro();
2781 t = tok;
2782 if (t == '(')
2783 next_nomacro();
2784 c = define_find(tok) != 0;
2785 if (t == '(')
2786 next_nomacro();
2787 tok = TOK_CINT;
2788 tokc.i = c;
2789 } else if (tok >= TOK_IDENT) {
2790 /* if undefined macro */
2791 tok = TOK_CINT;
2792 tokc.i = 0;
2794 tok_str_add_tok(&str);
2796 tok_str_add(&str, -1); /* simulate end of file */
2797 tok_str_add(&str, 0);
2798 /* now evaluate C constant expression */
2799 macro_ptr = str.str;
2800 next();
2801 c = expr_const();
2802 macro_ptr = NULL;
2803 tok_str_free(str.str);
2804 return c != 0;
2807 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2808 static void tok_print(int *str)
2810 int t;
2811 CValue cval;
2813 while (1) {
2814 TOK_GET(t, str, cval);
2815 if (!t)
2816 break;
2817 printf(" %s", get_tok_str(t, &cval));
2819 printf("\n");
2821 #endif
2823 /* parse after #define */
2824 static void parse_define(void)
2826 Sym *s, *first, **ps;
2827 int v, t, varg, is_vaargs, c;
2828 TokenString str;
2830 v = tok;
2831 if (v < TOK_IDENT)
2832 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2833 /* XXX: should check if same macro (ANSI) */
2834 first = NULL;
2835 t = MACRO_OBJ;
2836 /* '(' must be just after macro definition for MACRO_FUNC */
2837 c = file->buf_ptr[0];
2838 if (c == '\\')
2839 c = handle_stray1(file->buf_ptr);
2840 if (c == '(') {
2841 next_nomacro();
2842 next_nomacro();
2843 ps = &first;
2844 while (tok != ')') {
2845 varg = tok;
2846 next_nomacro();
2847 is_vaargs = 0;
2848 if (varg == TOK_DOTS) {
2849 varg = TOK___VA_ARGS__;
2850 is_vaargs = 1;
2851 } else if (tok == TOK_DOTS && gnu_ext) {
2852 is_vaargs = 1;
2853 next_nomacro();
2855 if (varg < TOK_IDENT)
2856 error("badly punctuated parameter list");
2857 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2858 *ps = s;
2859 ps = &s->next;
2860 if (tok != ',')
2861 break;
2862 next_nomacro();
2864 t = MACRO_FUNC;
2866 tok_str_new(&str);
2867 next_nomacro();
2868 /* EOF testing necessary for '-D' handling */
2869 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2870 tok_str_add2(&str, tok, &tokc);
2871 next_nomacro();
2873 tok_str_add(&str, 0);
2874 #ifdef PP_DEBUG
2875 printf("define %s %d: ", get_tok_str(v, NULL), t);
2876 tok_print(str.str);
2877 #endif
2878 define_push(v, t, str.str, first);
2881 static inline int hash_cached_include(int type, const char *filename)
2883 const unsigned char *s;
2884 unsigned int h;
2886 h = TOK_HASH_INIT;
2887 h = TOK_HASH_FUNC(h, type);
2888 s = filename;
2889 while (*s) {
2890 h = TOK_HASH_FUNC(h, *s);
2891 s++;
2893 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2894 return h;
2897 /* XXX: use a token or a hash table to accelerate matching ? */
2898 static CachedInclude *search_cached_include(TCCState *s1,
2899 int type, const char *filename)
2901 CachedInclude *e;
2902 int i, h;
2903 h = hash_cached_include(type, filename);
2904 i = s1->cached_includes_hash[h];
2905 for(;;) {
2906 if (i == 0)
2907 break;
2908 e = s1->cached_includes[i - 1];
2909 if (e->type == type && !strcmp(e->filename, filename))
2910 return e;
2911 i = e->hash_next;
2913 return NULL;
2916 static inline void add_cached_include(TCCState *s1, int type,
2917 const char *filename, int ifndef_macro)
2919 CachedInclude *e;
2920 int h;
2922 if (search_cached_include(s1, type, filename))
2923 return;
2924 #ifdef INC_DEBUG
2925 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2926 #endif
2927 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2928 if (!e)
2929 return;
2930 e->type = type;
2931 strcpy(e->filename, filename);
2932 e->ifndef_macro = ifndef_macro;
2933 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2934 /* add in hash table */
2935 h = hash_cached_include(type, filename);
2936 e->hash_next = s1->cached_includes_hash[h];
2937 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2940 static void pragma_parse(TCCState *s1)
2942 int val;
2944 next();
2945 if (tok == TOK_pack) {
2947 This may be:
2948 #pragma pack(1) // set
2949 #pragma pack() // reset to default
2950 #pragma pack(push,1) // push & set
2951 #pragma pack(pop) // restore previous
2953 next();
2954 skip('(');
2955 if (tok == TOK_ASM_pop) {
2956 next();
2957 if (s1->pack_stack_ptr <= s1->pack_stack) {
2958 stk_error:
2959 error("out of pack stack");
2961 s1->pack_stack_ptr--;
2962 } else {
2963 val = 0;
2964 if (tok != ')') {
2965 if (tok == TOK_ASM_push) {
2966 next();
2967 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2968 goto stk_error;
2969 s1->pack_stack_ptr++;
2970 skip(',');
2972 if (tok != TOK_CINT) {
2973 pack_error:
2974 error("invalid pack pragma");
2976 val = tokc.i;
2977 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2978 goto pack_error;
2979 next();
2981 *s1->pack_stack_ptr = val;
2982 skip(')');
2987 /* is_bof is true if first non space token at beginning of file */
2988 static void preprocess(int is_bof)
2990 TCCState *s1 = tcc_state;
2991 int size, i, c, n, saved_parse_flags;
2992 char buf[1024], *q;
2993 char buf1[1024];
2994 BufferedFile *f;
2995 Sym *s;
2996 CachedInclude *e;
2998 saved_parse_flags = parse_flags;
2999 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
3000 PARSE_FLAG_LINEFEED;
3001 next_nomacro();
3002 redo:
3003 switch(tok) {
3004 case TOK_DEFINE:
3005 next_nomacro();
3006 parse_define();
3007 break;
3008 case TOK_UNDEF:
3009 next_nomacro();
3010 s = define_find(tok);
3011 /* undefine symbol by putting an invalid name */
3012 if (s)
3013 define_undef(s);
3014 break;
3015 case TOK_INCLUDE:
3016 case TOK_INCLUDE_NEXT:
3017 ch = file->buf_ptr[0];
3018 /* XXX: incorrect if comments : use next_nomacro with a special mode */
3019 skip_spaces();
3020 if (ch == '<') {
3021 c = '>';
3022 goto read_name;
3023 } else if (ch == '\"') {
3024 c = ch;
3025 read_name:
3026 inp();
3027 q = buf;
3028 while (ch != c && ch != '\n' && ch != CH_EOF) {
3029 if ((q - buf) < sizeof(buf) - 1)
3030 *q++ = ch;
3031 if (ch == '\\') {
3032 if (handle_stray_noerror() == 0)
3033 --q;
3034 } else
3035 inp();
3037 *q = '\0';
3038 minp();
3039 #if 0
3040 /* eat all spaces and comments after include */
3041 /* XXX: slightly incorrect */
3042 while (ch1 != '\n' && ch1 != CH_EOF)
3043 inp();
3044 #endif
3045 } else {
3046 /* computed #include : either we have only strings or
3047 we have anything enclosed in '<>' */
3048 next();
3049 buf[0] = '\0';
3050 if (tok == TOK_STR) {
3051 while (tok != TOK_LINEFEED) {
3052 if (tok != TOK_STR) {
3053 include_syntax:
3054 error("'#include' expects \"FILENAME\" or <FILENAME>");
3056 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3057 next();
3059 c = '\"';
3060 } else {
3061 int len;
3062 while (tok != TOK_LINEFEED) {
3063 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3064 next();
3066 len = strlen(buf);
3067 /* check syntax and remove '<>' */
3068 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3069 goto include_syntax;
3070 memmove(buf, buf + 1, len - 2);
3071 buf[len - 2] = '\0';
3072 c = '>';
3076 e = search_cached_include(s1, c, buf);
3077 if (e && define_find(e->ifndef_macro)) {
3078 /* no need to parse the include because the 'ifndef macro'
3079 is defined */
3080 #ifdef INC_DEBUG
3081 printf("%s: skipping %s\n", file->filename, buf);
3082 #endif
3083 } else {
3084 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3085 error("#include recursion too deep");
3086 /* push current file in stack */
3087 /* XXX: fix current line init */
3088 *s1->include_stack_ptr++ = file;
3089 if (c == '\"') {
3090 /* first search in current dir if "header.h" */
3091 size = tcc_basename(file->filename) - file->filename;
3092 if (size > sizeof(buf1) - 1)
3093 size = sizeof(buf1) - 1;
3094 memcpy(buf1, file->filename, size);
3095 buf1[size] = '\0';
3096 pstrcat(buf1, sizeof(buf1), buf);
3097 f = tcc_open(s1, buf1);
3098 if (f) {
3099 if (tok == TOK_INCLUDE_NEXT)
3100 tok = TOK_INCLUDE;
3101 else
3102 goto found;
3105 /* now search in all the include paths */
3106 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3107 for(i = 0; i < n; i++) {
3108 const char *path;
3109 if (i < s1->nb_include_paths)
3110 path = s1->include_paths[i];
3111 else
3112 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3113 pstrcpy(buf1, sizeof(buf1), path);
3114 pstrcat(buf1, sizeof(buf1), "/");
3115 pstrcat(buf1, sizeof(buf1), buf);
3116 f = tcc_open(s1, buf1);
3117 if (f) {
3118 if (tok == TOK_INCLUDE_NEXT)
3119 tok = TOK_INCLUDE;
3120 else
3121 goto found;
3124 --s1->include_stack_ptr;
3125 error("include file '%s' not found", buf);
3126 break;
3127 found:
3128 #ifdef INC_DEBUG
3129 printf("%s: including %s\n", file->filename, buf1);
3130 #endif
3131 f->inc_type = c;
3132 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3133 file = f;
3134 /* add include file debug info */
3135 if (do_debug) {
3136 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3138 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3139 ch = file->buf_ptr[0];
3140 goto the_end;
3142 break;
3143 case TOK_IFNDEF:
3144 c = 1;
3145 goto do_ifdef;
3146 case TOK_IF:
3147 c = expr_preprocess();
3148 goto do_if;
3149 case TOK_IFDEF:
3150 c = 0;
3151 do_ifdef:
3152 next_nomacro();
3153 if (tok < TOK_IDENT)
3154 error("invalid argument for '#if%sdef'", c ? "n" : "");
3155 if (is_bof) {
3156 if (c) {
3157 #ifdef INC_DEBUG
3158 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3159 #endif
3160 file->ifndef_macro = tok;
3163 c = (define_find(tok) != 0) ^ c;
3164 do_if:
3165 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3166 error("memory full");
3167 *s1->ifdef_stack_ptr++ = c;
3168 goto test_skip;
3169 case TOK_ELSE:
3170 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3171 error("#else without matching #if");
3172 if (s1->ifdef_stack_ptr[-1] & 2)
3173 error("#else after #else");
3174 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3175 goto test_skip;
3176 case TOK_ELIF:
3177 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3178 error("#elif without matching #if");
3179 c = s1->ifdef_stack_ptr[-1];
3180 if (c > 1)
3181 error("#elif after #else");
3182 /* last #if/#elif expression was true: we skip */
3183 if (c == 1)
3184 goto skip;
3185 c = expr_preprocess();
3186 s1->ifdef_stack_ptr[-1] = c;
3187 test_skip:
3188 if (!(c & 1)) {
3189 skip:
3190 preprocess_skip();
3191 is_bof = 0;
3192 goto redo;
3194 break;
3195 case TOK_ENDIF:
3196 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3197 error("#endif without matching #if");
3198 s1->ifdef_stack_ptr--;
3199 /* '#ifndef macro' was at the start of file. Now we check if
3200 an '#endif' is exactly at the end of file */
3201 if (file->ifndef_macro &&
3202 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3203 file->ifndef_macro_saved = file->ifndef_macro;
3204 /* need to set to zero to avoid false matches if another
3205 #ifndef at middle of file */
3206 file->ifndef_macro = 0;
3207 while (tok != TOK_LINEFEED)
3208 next_nomacro();
3209 tok_flags |= TOK_FLAG_ENDIF;
3210 goto the_end;
3212 break;
3213 case TOK_LINE:
3214 next();
3215 if (tok != TOK_CINT)
3216 error("#line");
3217 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3218 next();
3219 if (tok != TOK_LINEFEED) {
3220 if (tok != TOK_STR)
3221 error("#line");
3222 pstrcpy(file->filename, sizeof(file->filename),
3223 (char *)tokc.cstr->data);
3225 break;
3226 case TOK_ERROR:
3227 case TOK_WARNING:
3228 c = tok;
3229 ch = file->buf_ptr[0];
3230 skip_spaces();
3231 q = buf;
3232 while (ch != '\n' && ch != CH_EOF) {
3233 if ((q - buf) < sizeof(buf) - 1)
3234 *q++ = ch;
3235 if (ch == '\\') {
3236 if (handle_stray_noerror() == 0)
3237 --q;
3238 } else
3239 inp();
3241 *q = '\0';
3242 if (c == TOK_ERROR)
3243 error("#error %s", buf);
3244 else
3245 warning("#warning %s", buf);
3246 break;
3247 case TOK_PRAGMA:
3248 pragma_parse(s1);
3249 break;
3250 default:
3251 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3252 /* '!' is ignored to allow C scripts. numbers are ignored
3253 to emulate cpp behaviour */
3254 } else {
3255 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3256 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3258 break;
3260 /* ignore other preprocess commands or #! for C scripts */
3261 while (tok != TOK_LINEFEED)
3262 next_nomacro();
3263 the_end:
3264 parse_flags = saved_parse_flags;
3267 /* evaluate escape codes in a string. */
3268 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3270 int c, n;
3271 const uint8_t *p;
3273 p = buf;
3274 for(;;) {
3275 c = *p;
3276 if (c == '\0')
3277 break;
3278 if (c == '\\') {
3279 p++;
3280 /* escape */
3281 c = *p;
3282 switch(c) {
3283 case '0': case '1': case '2': case '3':
3284 case '4': case '5': case '6': case '7':
3285 /* at most three octal digits */
3286 n = c - '0';
3287 p++;
3288 c = *p;
3289 if (isoct(c)) {
3290 n = n * 8 + c - '0';
3291 p++;
3292 c = *p;
3293 if (isoct(c)) {
3294 n = n * 8 + c - '0';
3295 p++;
3298 c = n;
3299 goto add_char_nonext;
3300 case 'x':
3301 case 'u':
3302 case 'U':
3303 p++;
3304 n = 0;
3305 for(;;) {
3306 c = *p;
3307 if (c >= 'a' && c <= 'f')
3308 c = c - 'a' + 10;
3309 else if (c >= 'A' && c <= 'F')
3310 c = c - 'A' + 10;
3311 else if (isnum(c))
3312 c = c - '0';
3313 else
3314 break;
3315 n = n * 16 + c;
3316 p++;
3318 c = n;
3319 goto add_char_nonext;
3320 case 'a':
3321 c = '\a';
3322 break;
3323 case 'b':
3324 c = '\b';
3325 break;
3326 case 'f':
3327 c = '\f';
3328 break;
3329 case 'n':
3330 c = '\n';
3331 break;
3332 case 'r':
3333 c = '\r';
3334 break;
3335 case 't':
3336 c = '\t';
3337 break;
3338 case 'v':
3339 c = '\v';
3340 break;
3341 case 'e':
3342 if (!gnu_ext)
3343 goto invalid_escape;
3344 c = 27;
3345 break;
3346 case '\'':
3347 case '\"':
3348 case '\\':
3349 case '?':
3350 break;
3351 default:
3352 invalid_escape:
3353 if (c >= '!' && c <= '~')
3354 warning("unknown escape sequence: \'\\%c\'", c);
3355 else
3356 warning("unknown escape sequence: \'\\x%x\'", c);
3357 break;
3360 p++;
3361 add_char_nonext:
3362 if (!is_long)
3363 cstr_ccat(outstr, c);
3364 else
3365 cstr_wccat(outstr, c);
3367 /* add a trailing '\0' */
3368 if (!is_long)
3369 cstr_ccat(outstr, '\0');
3370 else
3371 cstr_wccat(outstr, '\0');
3374 /* we use 64 bit numbers */
3375 #define BN_SIZE 2
3377 /* bn = (bn << shift) | or_val */
3378 void bn_lshift(unsigned int *bn, int shift, int or_val)
3380 int i;
3381 unsigned int v;
3382 for(i=0;i<BN_SIZE;i++) {
3383 v = bn[i];
3384 bn[i] = (v << shift) | or_val;
3385 or_val = v >> (32 - shift);
3389 void bn_zero(unsigned int *bn)
3391 int i;
3392 for(i=0;i<BN_SIZE;i++) {
3393 bn[i] = 0;
3397 /* parse number in null terminated string 'p' and return it in the
3398 current token */
3399 void parse_number(const char *p)
3401 int b, t, shift, frac_bits, s, exp_val, ch;
3402 char *q;
3403 unsigned int bn[BN_SIZE];
3404 double d;
3406 /* number */
3407 q = token_buf;
3408 ch = *p++;
3409 t = ch;
3410 ch = *p++;
3411 *q++ = t;
3412 b = 10;
3413 if (t == '.') {
3414 goto float_frac_parse;
3415 } else if (t == '0') {
3416 if (ch == 'x' || ch == 'X') {
3417 q--;
3418 ch = *p++;
3419 b = 16;
3420 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3421 q--;
3422 ch = *p++;
3423 b = 2;
3426 /* parse all digits. cannot check octal numbers at this stage
3427 because of floating point constants */
3428 while (1) {
3429 if (ch >= 'a' && ch <= 'f')
3430 t = ch - 'a' + 10;
3431 else if (ch >= 'A' && ch <= 'F')
3432 t = ch - 'A' + 10;
3433 else if (isnum(ch))
3434 t = ch - '0';
3435 else
3436 break;
3437 if (t >= b)
3438 break;
3439 if (q >= token_buf + STRING_MAX_SIZE) {
3440 num_too_long:
3441 error("number too long");
3443 *q++ = ch;
3444 ch = *p++;
3446 if (ch == '.' ||
3447 ((ch == 'e' || ch == 'E') && b == 10) ||
3448 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3449 if (b != 10) {
3450 /* NOTE: strtox should support that for hexa numbers, but
3451 non ISOC99 libcs do not support it, so we prefer to do
3452 it by hand */
3453 /* hexadecimal or binary floats */
3454 /* XXX: handle overflows */
3455 *q = '\0';
3456 if (b == 16)
3457 shift = 4;
3458 else
3459 shift = 2;
3460 bn_zero(bn);
3461 q = token_buf;
3462 while (1) {
3463 t = *q++;
3464 if (t == '\0') {
3465 break;
3466 } else if (t >= 'a') {
3467 t = t - 'a' + 10;
3468 } else if (t >= 'A') {
3469 t = t - 'A' + 10;
3470 } else {
3471 t = t - '0';
3473 bn_lshift(bn, shift, t);
3475 frac_bits = 0;
3476 if (ch == '.') {
3477 ch = *p++;
3478 while (1) {
3479 t = ch;
3480 if (t >= 'a' && t <= 'f') {
3481 t = t - 'a' + 10;
3482 } else if (t >= 'A' && t <= 'F') {
3483 t = t - 'A' + 10;
3484 } else if (t >= '0' && t <= '9') {
3485 t = t - '0';
3486 } else {
3487 break;
3489 if (t >= b)
3490 error("invalid digit");
3491 bn_lshift(bn, shift, t);
3492 frac_bits += shift;
3493 ch = *p++;
3496 if (ch != 'p' && ch != 'P')
3497 expect("exponent");
3498 ch = *p++;
3499 s = 1;
3500 exp_val = 0;
3501 if (ch == '+') {
3502 ch = *p++;
3503 } else if (ch == '-') {
3504 s = -1;
3505 ch = *p++;
3507 if (ch < '0' || ch > '9')
3508 expect("exponent digits");
3509 while (ch >= '0' && ch <= '9') {
3510 exp_val = exp_val * 10 + ch - '0';
3511 ch = *p++;
3513 exp_val = exp_val * s;
3515 /* now we can generate the number */
3516 /* XXX: should patch directly float number */
3517 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3518 d = ldexp(d, exp_val - frac_bits);
3519 t = toup(ch);
3520 if (t == 'F') {
3521 ch = *p++;
3522 tok = TOK_CFLOAT;
3523 /* float : should handle overflow */
3524 tokc.f = (float)d;
3525 } else if (t == 'L') {
3526 ch = *p++;
3527 tok = TOK_CLDOUBLE;
3528 /* XXX: not large enough */
3529 tokc.ld = (long double)d;
3530 } else {
3531 tok = TOK_CDOUBLE;
3532 tokc.d = d;
3534 } else {
3535 /* decimal floats */
3536 if (ch == '.') {
3537 if (q >= token_buf + STRING_MAX_SIZE)
3538 goto num_too_long;
3539 *q++ = ch;
3540 ch = *p++;
3541 float_frac_parse:
3542 while (ch >= '0' && ch <= '9') {
3543 if (q >= token_buf + STRING_MAX_SIZE)
3544 goto num_too_long;
3545 *q++ = ch;
3546 ch = *p++;
3549 if (ch == 'e' || ch == 'E') {
3550 if (q >= token_buf + STRING_MAX_SIZE)
3551 goto num_too_long;
3552 *q++ = ch;
3553 ch = *p++;
3554 if (ch == '-' || ch == '+') {
3555 if (q >= token_buf + STRING_MAX_SIZE)
3556 goto num_too_long;
3557 *q++ = ch;
3558 ch = *p++;
3560 if (ch < '0' || ch > '9')
3561 expect("exponent digits");
3562 while (ch >= '0' && ch <= '9') {
3563 if (q >= token_buf + STRING_MAX_SIZE)
3564 goto num_too_long;
3565 *q++ = ch;
3566 ch = *p++;
3569 *q = '\0';
3570 t = toup(ch);
3571 errno = 0;
3572 if (t == 'F') {
3573 ch = *p++;
3574 tok = TOK_CFLOAT;
3575 tokc.f = strtof(token_buf, NULL);
3576 } else if (t == 'L') {
3577 ch = *p++;
3578 tok = TOK_CLDOUBLE;
3579 tokc.ld = strtold(token_buf, NULL);
3580 } else {
3581 tok = TOK_CDOUBLE;
3582 tokc.d = strtod(token_buf, NULL);
3585 } else {
3586 unsigned long long n, n1;
3587 int lcount, ucount;
3589 /* integer number */
3590 *q = '\0';
3591 q = token_buf;
3592 if (b == 10 && *q == '0') {
3593 b = 8;
3594 q++;
3596 n = 0;
3597 while(1) {
3598 t = *q++;
3599 /* no need for checks except for base 10 / 8 errors */
3600 if (t == '\0') {
3601 break;
3602 } else if (t >= 'a') {
3603 t = t - 'a' + 10;
3604 } else if (t >= 'A') {
3605 t = t - 'A' + 10;
3606 } else {
3607 t = t - '0';
3608 if (t >= b)
3609 error("invalid digit");
3611 n1 = n;
3612 n = n * b + t;
3613 /* detect overflow */
3614 /* XXX: this test is not reliable */
3615 if (n < n1)
3616 error("integer constant overflow");
3619 /* XXX: not exactly ANSI compliant */
3620 if ((n & 0xffffffff00000000LL) != 0) {
3621 if ((n >> 63) != 0)
3622 tok = TOK_CULLONG;
3623 else
3624 tok = TOK_CLLONG;
3625 } else if (n > 0x7fffffff) {
3626 tok = TOK_CUINT;
3627 } else {
3628 tok = TOK_CINT;
3630 lcount = 0;
3631 ucount = 0;
3632 for(;;) {
3633 t = toup(ch);
3634 if (t == 'L') {
3635 if (lcount >= 2)
3636 error("three 'l's in integer constant");
3637 lcount++;
3638 if (lcount == 2) {
3639 if (tok == TOK_CINT)
3640 tok = TOK_CLLONG;
3641 else if (tok == TOK_CUINT)
3642 tok = TOK_CULLONG;
3644 ch = *p++;
3645 } else if (t == 'U') {
3646 if (ucount >= 1)
3647 error("two 'u's in integer constant");
3648 ucount++;
3649 if (tok == TOK_CINT)
3650 tok = TOK_CUINT;
3651 else if (tok == TOK_CLLONG)
3652 tok = TOK_CULLONG;
3653 ch = *p++;
3654 } else {
3655 break;
3658 if (tok == TOK_CINT || tok == TOK_CUINT)
3659 tokc.ui = n;
3660 else
3661 tokc.ull = n;
3663 if (ch)
3664 error("invalid number\n");
3668 #define PARSE2(c1, tok1, c2, tok2) \
3669 case c1: \
3670 PEEKC(c, p); \
3671 if (c == c2) { \
3672 p++; \
3673 tok = tok2; \
3674 } else { \
3675 tok = tok1; \
3677 break;
3679 /* return next token without macro substitution */
3680 static inline void next_nomacro1(void)
3682 int t, c, is_long;
3683 TokenSym *ts;
3684 uint8_t *p, *p1;
3685 unsigned int h;
3687 cstr_reset(&tok_spaces);
3688 p = file->buf_ptr;
3689 redo_no_start:
3690 c = *p;
3691 switch(c) {
3692 case ' ':
3693 case '\t':
3694 case '\f':
3695 case '\v':
3696 case '\r':
3697 cstr_ccat(&tok_spaces, c);
3698 p++;
3699 goto redo_no_start;
3701 case '\\':
3702 /* first look if it is in fact an end of buffer */
3703 if (p >= file->buf_end) {
3704 file->buf_ptr = p;
3705 handle_eob();
3706 p = file->buf_ptr;
3707 if (p >= file->buf_end)
3708 goto parse_eof;
3709 else
3710 goto redo_no_start;
3711 } else {
3712 file->buf_ptr = p;
3713 ch = *p;
3714 handle_stray();
3715 p = file->buf_ptr;
3716 goto redo_no_start;
3718 parse_eof:
3720 TCCState *s1 = tcc_state;
3721 if ((parse_flags & PARSE_FLAG_LINEFEED)
3722 && !(tok_flags & TOK_FLAG_EOF)) {
3723 tok_flags |= TOK_FLAG_EOF;
3724 tok = TOK_LINEFEED;
3725 goto keep_tok_flags;
3726 } else if (s1->include_stack_ptr == s1->include_stack ||
3727 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3728 /* no include left : end of file. */
3729 tok = TOK_EOF;
3730 } else {
3731 tok_flags &= ~TOK_FLAG_EOF;
3732 /* pop include file */
3734 /* test if previous '#endif' was after a #ifdef at
3735 start of file */
3736 if (tok_flags & TOK_FLAG_ENDIF) {
3737 #ifdef INC_DEBUG
3738 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3739 #endif
3740 add_cached_include(s1, file->inc_type, file->inc_filename,
3741 file->ifndef_macro_saved);
3744 /* add end of include file debug info */
3745 if (do_debug) {
3746 put_stabd(N_EINCL, 0, 0);
3748 /* pop include stack */
3749 tcc_close(file);
3750 s1->include_stack_ptr--;
3751 file = *s1->include_stack_ptr;
3752 p = file->buf_ptr;
3753 goto redo_no_start;
3756 break;
3758 case '\n':
3759 file->line_num++;
3760 tok_flags |= TOK_FLAG_BOL;
3761 p++;
3762 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3763 goto redo_no_start;
3764 tok = TOK_LINEFEED;
3765 goto keep_tok_flags;
3767 case '#':
3768 /* XXX: simplify */
3769 PEEKC(c, p);
3770 if ((tok_flags & TOK_FLAG_BOL) &&
3771 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3772 file->buf_ptr = p;
3773 preprocess(tok_flags & TOK_FLAG_BOF);
3774 p = file->buf_ptr;
3775 goto redo_no_start;
3776 } else {
3777 if (c == '#') {
3778 p++;
3779 tok = TOK_TWOSHARPS;
3780 } else {
3781 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3782 p = parse_line_comment(p - 1);
3783 goto redo_no_start;
3784 } else {
3785 tok = '#';
3789 break;
3791 case 'a': case 'b': case 'c': case 'd':
3792 case 'e': case 'f': case 'g': case 'h':
3793 case 'i': case 'j': case 'k': case 'l':
3794 case 'm': case 'n': case 'o': case 'p':
3795 case 'q': case 'r': case 's': case 't':
3796 case 'u': case 'v': case 'w': case 'x':
3797 case 'y': case 'z':
3798 case 'A': case 'B': case 'C': case 'D':
3799 case 'E': case 'F': case 'G': case 'H':
3800 case 'I': case 'J': case 'K':
3801 case 'M': case 'N': case 'O': case 'P':
3802 case 'Q': case 'R': case 'S': case 'T':
3803 case 'U': case 'V': case 'W': case 'X':
3804 case 'Y': case 'Z':
3805 case '_':
3806 parse_ident_fast:
3807 p1 = p;
3808 h = TOK_HASH_INIT;
3809 h = TOK_HASH_FUNC(h, c);
3810 p++;
3811 for(;;) {
3812 c = *p;
3813 if (!isidnum_table[c-CH_EOF])
3814 break;
3815 h = TOK_HASH_FUNC(h, c);
3816 p++;
3818 if (c != '\\') {
3819 TokenSym **pts;
3820 int len;
3822 /* fast case : no stray found, so we have the full token
3823 and we have already hashed it */
3824 len = p - p1;
3825 h &= (TOK_HASH_SIZE - 1);
3826 pts = &hash_ident[h];
3827 for(;;) {
3828 ts = *pts;
3829 if (!ts)
3830 break;
3831 if (ts->len == len && !memcmp(ts->str, p1, len))
3832 goto token_found;
3833 pts = &(ts->hash_next);
3835 ts = tok_alloc_new(pts, p1, len);
3836 token_found: ;
3837 } else {
3838 /* slower case */
3839 cstr_reset(&tokcstr);
3841 while (p1 < p) {
3842 cstr_ccat(&tokcstr, *p1);
3843 p1++;
3845 p--;
3846 PEEKC(c, p);
3847 parse_ident_slow:
3848 while (isidnum_table[c-CH_EOF]) {
3849 cstr_ccat(&tokcstr, c);
3850 PEEKC(c, p);
3852 ts = tok_alloc(tokcstr.data, tokcstr.size);
3854 tok = ts->tok;
3855 break;
3856 case 'L':
3857 t = p[1];
3858 if (t != '\\' && t != '\'' && t != '\"') {
3859 /* fast case */
3860 goto parse_ident_fast;
3861 } else {
3862 PEEKC(c, p);
3863 if (c == '\'' || c == '\"') {
3864 is_long = 1;
3865 goto str_const;
3866 } else {
3867 cstr_reset(&tokcstr);
3868 cstr_ccat(&tokcstr, 'L');
3869 goto parse_ident_slow;
3872 break;
3873 case '0': case '1': case '2': case '3':
3874 case '4': case '5': case '6': case '7':
3875 case '8': case '9':
3877 cstr_reset(&tokcstr);
3878 /* after the first digit, accept digits, alpha, '.' or sign if
3879 prefixed by 'eEpP' */
3880 parse_num:
3881 for(;;) {
3882 t = c;
3883 cstr_ccat(&tokcstr, c);
3884 PEEKC(c, p);
3885 if (!(isnum(c) || isid(c) || c == '.' ||
3886 ((c == '+' || c == '-') &&
3887 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3888 break;
3890 /* We add a trailing '\0' to ease parsing */
3891 cstr_ccat(&tokcstr, '\0');
3892 tokc.cstr = &tokcstr;
3893 tok = TOK_PPNUM;
3894 break;
3895 case '.':
3896 /* special dot handling because it can also start a number */
3897 PEEKC(c, p);
3898 if (isnum(c)) {
3899 cstr_reset(&tokcstr);
3900 cstr_ccat(&tokcstr, '.');
3901 goto parse_num;
3902 } else if (c == '.') {
3903 PEEKC(c, p);
3904 if (c != '.')
3905 expect("'.'");
3906 PEEKC(c, p);
3907 tok = TOK_DOTS;
3908 } else {
3909 tok = '.';
3911 break;
3912 case '\'':
3913 case '\"':
3914 is_long = 0;
3915 str_const:
3917 CString str;
3918 int sep;
3920 sep = c;
3922 /* parse the string */
3923 cstr_new(&str);
3924 p = parse_pp_string(p, sep, &str);
3925 cstr_ccat(&str, '\0');
3927 /* eval the escape (should be done as TOK_PPNUM) */
3928 cstr_reset(&tokcstr);
3929 parse_escape_string(&tokcstr, str.data, is_long);
3930 cstr_free(&str);
3932 if (sep == '\'') {
3933 int char_size;
3934 /* XXX: make it portable */
3935 if (!is_long)
3936 char_size = 1;
3937 else
3938 char_size = sizeof(nwchar_t);
3939 if (tokcstr.size <= char_size)
3940 error("empty character constant");
3941 if (tokcstr.size > 2 * char_size)
3942 warning("multi-character character constant");
3943 if (!is_long) {
3944 tokc.i = *(int8_t *)tokcstr.data;
3945 tok = TOK_CCHAR;
3946 } else {
3947 tokc.i = *(nwchar_t *)tokcstr.data;
3948 tok = TOK_LCHAR;
3950 } else {
3951 tokc.cstr = &tokcstr;
3952 if (!is_long)
3953 tok = TOK_STR;
3954 else
3955 tok = TOK_LSTR;
3958 break;
3960 case '<':
3961 PEEKC(c, p);
3962 if (c == '=') {
3963 p++;
3964 tok = TOK_LE;
3965 } else if (c == '<') {
3966 PEEKC(c, p);
3967 if (c == '=') {
3968 p++;
3969 tok = TOK_A_SHL;
3970 } else {
3971 tok = TOK_SHL;
3973 } else {
3974 tok = TOK_LT;
3976 break;
3978 case '>':
3979 PEEKC(c, p);
3980 if (c == '=') {
3981 p++;
3982 tok = TOK_GE;
3983 } else if (c == '>') {
3984 PEEKC(c, p);
3985 if (c == '=') {
3986 p++;
3987 tok = TOK_A_SAR;
3988 } else {
3989 tok = TOK_SAR;
3991 } else {
3992 tok = TOK_GT;
3994 break;
3996 case '&':
3997 PEEKC(c, p);
3998 if (c == '&') {
3999 p++;
4000 tok = TOK_LAND;
4001 } else if (c == '=') {
4002 p++;
4003 tok = TOK_A_AND;
4004 } else {
4005 tok = '&';
4007 break;
4009 case '|':
4010 PEEKC(c, p);
4011 if (c == '|') {
4012 p++;
4013 tok = TOK_LOR;
4014 } else if (c == '=') {
4015 p++;
4016 tok = TOK_A_OR;
4017 } else {
4018 tok = '|';
4020 break;
4022 case '+':
4023 PEEKC(c, p);
4024 if (c == '+') {
4025 p++;
4026 tok = TOK_INC;
4027 } else if (c == '=') {
4028 p++;
4029 tok = TOK_A_ADD;
4030 } else {
4031 tok = '+';
4033 break;
4035 case '-':
4036 PEEKC(c, p);
4037 if (c == '-') {
4038 p++;
4039 tok = TOK_DEC;
4040 } else if (c == '=') {
4041 p++;
4042 tok = TOK_A_SUB;
4043 } else if (c == '>') {
4044 p++;
4045 tok = TOK_ARROW;
4046 } else {
4047 tok = '-';
4049 break;
4051 PARSE2('!', '!', '=', TOK_NE)
4052 PARSE2('=', '=', '=', TOK_EQ)
4053 PARSE2('*', '*', '=', TOK_A_MUL)
4054 PARSE2('%', '%', '=', TOK_A_MOD)
4055 PARSE2('^', '^', '=', TOK_A_XOR)
4057 /* comments or operator */
4058 case '/':
4059 PEEKC(c, p);
4060 if (c == '*') {
4061 p = parse_comment(p);
4062 goto redo_no_start;
4063 } else if (c == '/') {
4064 p = parse_line_comment(p);
4065 goto redo_no_start;
4066 } else if (c == '=') {
4067 p++;
4068 tok = TOK_A_DIV;
4069 } else {
4070 tok = '/';
4072 break;
4074 /* simple tokens */
4075 case '(':
4076 case ')':
4077 case '[':
4078 case ']':
4079 case '{':
4080 case '}':
4081 case ',':
4082 case ';':
4083 case ':':
4084 case '?':
4085 case '~':
4086 case '$': /* only used in assembler */
4087 case '@': /* dito */
4088 tok = c;
4089 p++;
4090 break;
4091 default:
4092 error("unrecognized character \\x%02x", c);
4093 break;
4095 tok_flags = 0;
4096 keep_tok_flags:
4097 file->buf_ptr = p;
4098 #if defined(PARSE_DEBUG)
4099 printf("token = %s\n", get_tok_str(tok, &tokc));
4100 #endif
4103 /* return next token without macro substitution. Can read input from
4104 macro_ptr buffer */
4105 static void next_nomacro(void)
4107 if (macro_ptr) {
4108 redo:
4109 tok = *macro_ptr;
4110 if (tok) {
4111 TOK_GET(tok, macro_ptr, tokc);
4112 if (tok == TOK_LINENUM) {
4113 file->line_num = tokc.i;
4114 goto redo;
4117 } else {
4118 next_nomacro1();
4122 /* substitute args in macro_str and return allocated string */
4123 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4125 int *st, last_tok, t, notfirst;
4126 Sym *s;
4127 CValue cval;
4128 TokenString str;
4129 CString cstr;
4131 tok_str_new(&str);
4132 last_tok = 0;
4133 while(1) {
4134 TOK_GET(t, macro_str, cval);
4135 if (!t)
4136 break;
4137 if (t == '#') {
4138 /* stringize */
4139 TOK_GET(t, macro_str, cval);
4140 if (!t)
4141 break;
4142 s = sym_find2(args, t);
4143 if (s) {
4144 cstr_new(&cstr);
4145 st = (int *)s->c;
4146 notfirst = 0;
4147 while (*st) {
4148 if (notfirst)
4149 cstr_ccat(&cstr, ' ');
4150 TOK_GET(t, st, cval);
4151 cstr_cat(&cstr, get_tok_str(t, &cval));
4152 #ifndef PP_NOSPACES
4153 notfirst = 1;
4154 #endif
4156 cstr_ccat(&cstr, '\0');
4157 #ifdef PP_DEBUG
4158 printf("stringize: %s\n", (char *)cstr.data);
4159 #endif
4160 /* add string */
4161 cval.cstr = &cstr;
4162 tok_str_add2(&str, TOK_STR, &cval);
4163 cstr_free(&cstr);
4164 } else {
4165 tok_str_add2(&str, t, &cval);
4167 } else if (t >= TOK_IDENT) {
4168 s = sym_find2(args, t);
4169 if (s) {
4170 st = (int *)s->c;
4171 /* if '##' is present before or after, no arg substitution */
4172 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4173 /* special case for var arg macros : ## eats the
4174 ',' if empty VA_ARGS variable. */
4175 /* XXX: test of the ',' is not 100%
4176 reliable. should fix it to avoid security
4177 problems */
4178 if (gnu_ext && s->type.t &&
4179 last_tok == TOK_TWOSHARPS &&
4180 str.len >= 2 && str.str[str.len - 2] == ',') {
4181 if (*st == 0) {
4182 /* suppress ',' '##' */
4183 str.len -= 2;
4184 } else {
4185 /* suppress '##' and add variable */
4186 str.len--;
4187 goto add_var;
4189 } else {
4190 int t1;
4191 add_var:
4192 for(;;) {
4193 TOK_GET(t1, st, cval);
4194 if (!t1)
4195 break;
4196 tok_str_add2(&str, t1, &cval);
4199 } else {
4200 /* NOTE: the stream cannot be read when macro
4201 substituing an argument */
4202 macro_subst(&str, nested_list, st, NULL);
4204 } else {
4205 tok_str_add(&str, t);
4207 } else {
4208 tok_str_add2(&str, t, &cval);
4210 last_tok = t;
4212 tok_str_add(&str, 0);
4213 return str.str;
4216 static char const ab_month_name[12][4] =
4218 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4219 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4222 /* do macro substitution of current token with macro 's' and add
4223 result to (tok_str,tok_len). 'nested_list' is the list of all
4224 macros we got inside to avoid recursing. Return non zero if no
4225 substitution needs to be done */
4226 static int macro_subst_tok(TokenString *tok_str,
4227 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4229 Sym *args, *sa, *sa1;
4230 int mstr_allocated, parlevel, *mstr, t, t1;
4231 TokenString str;
4232 char *cstrval;
4233 CValue cval;
4234 CString cstr;
4235 char buf[32];
4237 /* if symbol is a macro, prepare substitution */
4238 /* special macros */
4239 if (tok == TOK___LINE__) {
4240 snprintf(buf, sizeof(buf), "%d", file->line_num);
4241 cstrval = buf;
4242 t1 = TOK_PPNUM;
4243 goto add_cstr1;
4244 } else if (tok == TOK___FILE__) {
4245 cstrval = file->filename;
4246 goto add_cstr;
4247 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4248 time_t ti;
4249 struct tm *tm;
4251 time(&ti);
4252 tm = localtime(&ti);
4253 if (tok == TOK___DATE__) {
4254 snprintf(buf, sizeof(buf), "%s %2d %d",
4255 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4256 } else {
4257 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4258 tm->tm_hour, tm->tm_min, tm->tm_sec);
4260 cstrval = buf;
4261 add_cstr:
4262 t1 = TOK_STR;
4263 add_cstr1:
4264 cstr_new(&cstr);
4265 cstr_cat(&cstr, cstrval);
4266 cstr_ccat(&cstr, '\0');
4267 cval.cstr = &cstr;
4268 tok_str_add2(tok_str, t1, &cval);
4269 cstr_free(&cstr);
4270 } else {
4271 mstr = (int *)s->c;
4272 mstr_allocated = 0;
4273 if (s->type.t == MACRO_FUNC) {
4274 /* NOTE: we do not use next_nomacro to avoid eating the
4275 next token. XXX: find better solution */
4276 redo:
4277 if (macro_ptr) {
4278 t = *macro_ptr;
4279 if (t == 0 && can_read_stream) {
4280 /* end of macro stream: we must look at the token
4281 after in the file */
4282 struct macro_level *ml = *can_read_stream;
4283 macro_ptr = NULL;
4284 if (ml)
4286 macro_ptr = ml->p;
4287 ml->p = NULL;
4288 *can_read_stream = ml -> prev;
4290 goto redo;
4292 } else {
4293 /* XXX: incorrect with comments */
4294 ch = file->buf_ptr[0];
4295 while (is_space(ch) || ch == '\n')
4296 cinp();
4297 t = ch;
4299 if (t != '(') /* no macro subst */
4300 return -1;
4302 /* argument macro */
4303 next_nomacro();
4304 next_nomacro();
4305 args = NULL;
4306 sa = s->next;
4307 /* NOTE: empty args are allowed, except if no args */
4308 for(;;) {
4309 /* handle '()' case */
4310 if (!args && !sa && tok == ')')
4311 break;
4312 if (!sa)
4313 error("macro '%s' used with too many args",
4314 get_tok_str(s->v, 0));
4315 tok_str_new(&str);
4316 parlevel = 0;
4317 /* NOTE: non zero sa->t indicates VA_ARGS */
4318 while ((parlevel > 0 ||
4319 (tok != ')' &&
4320 (tok != ',' || sa->type.t))) &&
4321 tok != -1) {
4322 if (tok == '(')
4323 parlevel++;
4324 else if (tok == ')')
4325 parlevel--;
4326 if (tok != TOK_LINEFEED)
4327 tok_str_add2(&str, tok, &tokc);
4328 next_nomacro();
4330 tok_str_add(&str, 0);
4331 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (long)str.str);
4332 sa = sa->next;
4333 if (tok == ')') {
4334 /* special case for gcc var args: add an empty
4335 var arg argument if it is omitted */
4336 if (sa && sa->type.t && gnu_ext)
4337 continue;
4338 else
4339 break;
4341 if (tok != ',')
4342 expect(",");
4343 next_nomacro();
4345 if (sa) {
4346 error("macro '%s' used with too few args",
4347 get_tok_str(s->v, 0));
4350 /* now subst each arg */
4351 mstr = macro_arg_subst(nested_list, mstr, args);
4352 /* free memory */
4353 sa = args;
4354 while (sa) {
4355 sa1 = sa->prev;
4356 tok_str_free((int *)sa->c);
4357 sym_free(sa);
4358 sa = sa1;
4360 mstr_allocated = 1;
4362 sym_push2(nested_list, s->v, 0, 0);
4363 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4364 /* pop nested defined symbol */
4365 sa1 = *nested_list;
4366 *nested_list = sa1->prev;
4367 sym_free(sa1);
4368 if (mstr_allocated)
4369 tok_str_free(mstr);
4371 return 0;
4374 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4375 return the resulting string (which must be freed). */
4376 static inline int *macro_twosharps(const int *macro_str)
4378 TokenSym *ts;
4379 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4380 int t;
4381 const char *p1, *p2;
4382 CValue cval;
4383 TokenString macro_str1;
4384 CString cstr;
4386 start_macro_ptr = macro_str;
4387 /* we search the first '##' */
4388 for(;;) {
4389 macro_ptr1 = macro_str;
4390 TOK_GET(t, macro_str, cval);
4391 /* nothing more to do if end of string */
4392 if (t == 0)
4393 return NULL;
4394 if (*macro_str == TOK_TWOSHARPS)
4395 break;
4398 /* we saw '##', so we need more processing to handle it */
4399 cstr_new(&cstr);
4400 tok_str_new(&macro_str1);
4401 tok = t;
4402 tokc = cval;
4404 /* add all tokens seen so far */
4405 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4406 TOK_GET(t, ptr, cval);
4407 tok_str_add2(&macro_str1, t, &cval);
4409 saved_macro_ptr = macro_ptr;
4410 /* XXX: get rid of the use of macro_ptr here */
4411 macro_ptr = (int *)macro_str;
4412 for(;;) {
4413 while (*macro_ptr == TOK_TWOSHARPS) {
4414 macro_ptr++;
4415 macro_ptr1 = macro_ptr;
4416 t = *macro_ptr;
4417 if (t) {
4418 TOK_GET(t, macro_ptr, cval);
4419 /* We concatenate the two tokens if we have an
4420 identifier or a preprocessing number */
4421 cstr_reset(&cstr);
4422 p1 = get_tok_str(tok, &tokc);
4423 cstr_cat(&cstr, p1);
4424 p2 = get_tok_str(t, &cval);
4425 cstr_cat(&cstr, p2);
4426 cstr_ccat(&cstr, '\0');
4428 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4429 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4430 if (tok == TOK_PPNUM) {
4431 /* if number, then create a number token */
4432 /* NOTE: no need to allocate because
4433 tok_str_add2() does it */
4434 cstr_reset(&tokcstr);
4435 tokcstr = cstr;
4436 cstr_new(&cstr);
4437 tokc.cstr = &tokcstr;
4438 } else {
4439 /* if identifier, we must do a test to
4440 validate we have a correct identifier */
4441 if (t == TOK_PPNUM) {
4442 const char *p;
4443 int c;
4445 p = p2;
4446 for(;;) {
4447 c = *p;
4448 if (c == '\0')
4449 break;
4450 p++;
4451 if (!isnum(c) && !isid(c))
4452 goto error_pasting;
4455 ts = tok_alloc(cstr.data, strlen(cstr.data));
4456 tok = ts->tok; /* modify current token */
4458 } else {
4459 const char *str = cstr.data;
4460 const unsigned char *q;
4462 /* we look for a valid token */
4463 /* XXX: do more extensive checks */
4464 if (!strcmp(str, ">>=")) {
4465 tok = TOK_A_SAR;
4466 } else if (!strcmp(str, "<<=")) {
4467 tok = TOK_A_SHL;
4468 } else if (strlen(str) == 2) {
4469 /* search in two bytes table */
4470 q = tok_two_chars;
4471 for(;;) {
4472 if (!*q)
4473 goto error_pasting;
4474 if (q[0] == str[0] && q[1] == str[1])
4475 break;
4476 q += 3;
4478 tok = q[2];
4479 } else {
4480 error_pasting:
4481 /* NOTE: because get_tok_str use a static buffer,
4482 we must save it */
4483 cstr_reset(&cstr);
4484 p1 = get_tok_str(tok, &tokc);
4485 cstr_cat(&cstr, p1);
4486 cstr_ccat(&cstr, '\0');
4487 p2 = get_tok_str(t, &cval);
4488 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4489 /* cannot merge tokens: just add them separately */
4490 tok_str_add2(&macro_str1, tok, &tokc);
4491 /* XXX: free associated memory ? */
4492 tok = t;
4493 tokc = cval;
4498 tok_str_add2(&macro_str1, tok, &tokc);
4499 next_nomacro();
4500 if (tok == 0)
4501 break;
4503 macro_ptr = (int *)saved_macro_ptr;
4504 cstr_free(&cstr);
4505 tok_str_add(&macro_str1, 0);
4506 return macro_str1.str;
4510 /* do macro substitution of macro_str and add result to
4511 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4512 inside to avoid recursing. */
4513 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4514 const int *macro_str, struct macro_level ** can_read_stream)
4516 Sym *s;
4517 int *macro_str1;
4518 const int *ptr;
4519 int t, ret;
4520 CValue cval;
4521 struct macro_level ml;
4523 /* first scan for '##' operator handling */
4524 ptr = macro_str;
4525 macro_str1 = macro_twosharps(ptr);
4526 if (macro_str1)
4527 ptr = macro_str1;
4528 while (1) {
4529 /* NOTE: ptr == NULL can only happen if tokens are read from
4530 file stream due to a macro function call */
4531 if (ptr == NULL)
4532 break;
4533 TOK_GET(t, ptr, cval);
4534 if (t == 0)
4535 break;
4536 s = define_find(t);
4537 if (s != NULL) {
4538 /* if nested substitution, do nothing */
4539 if (sym_find2(*nested_list, t))
4540 goto no_subst;
4541 ml.p = macro_ptr;
4542 if (can_read_stream)
4543 ml.prev = *can_read_stream, *can_read_stream = &ml;
4544 macro_ptr = (int *)ptr;
4545 tok = t;
4546 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4547 ptr = (int *)macro_ptr;
4548 macro_ptr = ml.p;
4549 if (can_read_stream && *can_read_stream == &ml)
4550 *can_read_stream = ml.prev;
4551 if (ret != 0)
4552 goto no_subst;
4553 } else {
4554 no_subst:
4555 tok_str_add2(tok_str, t, &cval);
4558 if (macro_str1)
4559 tok_str_free(macro_str1);
4562 /* return next token with macro substitution */
4563 static void next(void)
4565 Sym *nested_list, *s;
4566 TokenString str;
4567 struct macro_level *ml;
4569 redo:
4570 next_nomacro();
4571 if (!macro_ptr) {
4572 /* if not reading from macro substituted string, then try
4573 to substitute macros */
4574 if (tok >= TOK_IDENT &&
4575 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4576 s = define_find(tok);
4577 if (s) {
4578 /* we have a macro: we try to substitute */
4579 tok_str_new(&str);
4580 nested_list = NULL;
4581 ml = NULL;
4582 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4583 /* substitution done, NOTE: maybe empty */
4584 tok_str_add(&str, 0);
4585 macro_ptr = str.str;
4586 macro_ptr_allocated = str.str;
4587 goto redo;
4591 } else {
4592 if (tok == 0) {
4593 /* end of macro or end of unget buffer */
4594 if (unget_buffer_enabled) {
4595 macro_ptr = unget_saved_macro_ptr;
4596 unget_buffer_enabled = 0;
4597 } else {
4598 /* end of macro string: free it */
4599 tok_str_free(macro_ptr_allocated);
4600 macro_ptr = NULL;
4602 goto redo;
4606 /* convert preprocessor tokens into C tokens */
4607 if (tok == TOK_PPNUM &&
4608 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4609 parse_number((char *)tokc.cstr->data);
4613 /* push back current token and set current token to 'last_tok'. Only
4614 identifier case handled for labels. */
4615 static inline void unget_tok(int last_tok)
4617 int i, n;
4618 int *q;
4619 unget_saved_macro_ptr = macro_ptr;
4620 unget_buffer_enabled = 1;
4621 q = unget_saved_buffer;
4622 macro_ptr = q;
4623 *q++ = tok;
4624 n = tok_ext_size(tok) - 1;
4625 for(i=0;i<n;i++)
4626 *q++ = tokc.tab[i];
4627 *q = 0; /* end of token string */
4628 tok = last_tok;
4632 void swap(int *p, int *q)
4634 int t;
4635 t = *p;
4636 *p = *q;
4637 *q = t;
4640 void vsetc(CType *type, int r, CValue *vc)
4642 int v;
4644 if (vtop >= vstack + (VSTACK_SIZE - 1))
4645 error("memory full");
4646 /* cannot let cpu flags if other instruction are generated. Also
4647 avoid leaving VT_JMP anywhere except on the top of the stack
4648 because it would complicate the code generator. */
4649 if (vtop >= vstack) {
4650 v = vtop->r & VT_VALMASK;
4651 if (v == VT_CMP || (v & ~1) == VT_JMP)
4652 gv(RC_INT);
4654 vtop++;
4655 vtop->type = *type;
4656 vtop->r = r;
4657 vtop->r2 = VT_CONST;
4658 vtop->c = *vc;
4661 /* push integer constant */
4662 void vpushi(int v)
4664 CValue cval;
4665 cval.i = v;
4666 vsetc(&int_type, VT_CONST, &cval);
4669 /* push long long constant */
4670 void vpushll(long long v)
4672 CValue cval;
4673 CType ctype;
4674 ctype.t = VT_LLONG;
4675 cval.ull = v;
4676 vsetc(&ctype, VT_CONST, &cval);
4679 /* Return a static symbol pointing to a section */
4680 static Sym *get_sym_ref(CType *type, Section *sec,
4681 unsigned long offset, unsigned long size)
4683 int v;
4684 Sym *sym;
4686 v = anon_sym++;
4687 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4688 sym->type.ref = type->ref;
4689 sym->r = VT_CONST | VT_SYM;
4690 put_extern_sym(sym, sec, offset, size);
4691 return sym;
4694 /* push a reference to a section offset by adding a dummy symbol */
4695 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4697 CValue cval;
4699 cval.ul = 0;
4700 vsetc(type, VT_CONST | VT_SYM, &cval);
4701 vtop->sym = get_sym_ref(type, sec, offset, size);
4704 /* define a new external reference to a symbol 'v' of type 'u' */
4705 static Sym *external_global_sym(int v, CType *type, int r)
4707 Sym *s;
4709 s = sym_find(v);
4710 if (!s) {
4711 /* push forward reference */
4712 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4713 s->type.ref = type->ref;
4714 s->r = r | VT_CONST | VT_SYM;
4716 return s;
4719 /* define a new external reference to a symbol 'v' of type 'u' */
4720 static Sym *external_sym(int v, CType *type, int r)
4722 Sym *s;
4724 s = sym_find(v);
4725 if (!s) {
4726 /* push forward reference */
4727 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4728 s->type.t |= VT_EXTERN;
4729 } else {
4730 if (!is_compatible_types(&s->type, type))
4731 error("incompatible types for redefinition of '%s'",
4732 get_tok_str(v, NULL));
4734 return s;
4737 /* push a reference to global symbol v */
4738 static void vpush_global_sym(CType *type, int v)
4740 Sym *sym;
4741 CValue cval;
4743 sym = external_global_sym(v, type, 0);
4744 cval.ul = 0;
4745 vsetc(type, VT_CONST | VT_SYM, &cval);
4746 vtop->sym = sym;
4749 void vset(CType *type, int r, int v)
4751 CValue cval;
4753 cval.i = v;
4754 vsetc(type, r, &cval);
4757 void vseti(int r, int v)
4759 CType type;
4760 type.t = VT_INT;
4761 vset(&type, r, v);
4764 void vswap(void)
4766 SValue tmp;
4768 tmp = vtop[0];
4769 vtop[0] = vtop[-1];
4770 vtop[-1] = tmp;
4773 void vpushv(SValue *v)
4775 if (vtop >= vstack + (VSTACK_SIZE - 1))
4776 error("memory full");
4777 vtop++;
4778 *vtop = *v;
4781 void vdup(void)
4783 vpushv(vtop);
4786 /* save r to the memory stack, and mark it as being free */
4787 void save_reg(int r)
4789 int l, saved, size, align;
4790 SValue *p, sv;
4791 CType *type;
4793 /* modify all stack values */
4794 saved = 0;
4795 l = 0;
4796 for(p=vstack;p<=vtop;p++) {
4797 if ((p->r & VT_VALMASK) == r ||
4798 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4799 /* must save value on stack if not already done */
4800 if (!saved) {
4801 /* NOTE: must reload 'r' because r might be equal to r2 */
4802 r = p->r & VT_VALMASK;
4803 /* store register in the stack */
4804 type = &p->type;
4805 if ((p->r & VT_LVAL) ||
4806 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4807 #ifdef TCC_TARGET_X86_64
4808 type = &char_pointer_type;
4809 #else
4810 type = &int_type;
4811 #endif
4812 size = type_size(type, &align);
4813 loc = (loc - size) & -align;
4814 sv.type.t = type->t;
4815 sv.r = VT_LOCAL | VT_LVAL;
4816 sv.c.ul = loc;
4817 store(r, &sv);
4818 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
4819 /* x86 specific: need to pop fp register ST0 if saved */
4820 if (r == TREG_ST0) {
4821 o(0xd9dd); /* fstp %st(1) */
4823 #endif
4824 #ifndef TCC_TARGET_X86_64
4825 /* special long long case */
4826 if ((type->t & VT_BTYPE) == VT_LLONG) {
4827 sv.c.ul += 4;
4828 store(p->r2, &sv);
4830 #endif
4831 l = loc;
4832 saved = 1;
4834 /* mark that stack entry as being saved on the stack */
4835 if (p->r & VT_LVAL) {
4836 /* also clear the bounded flag because the
4837 relocation address of the function was stored in
4838 p->c.ul */
4839 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4840 } else {
4841 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4843 p->r2 = VT_CONST;
4844 p->c.ul = l;
4849 /* find a register of class 'rc2' with at most one reference on stack.
4850 * If none, call get_reg(rc) */
4851 int get_reg_ex(int rc, int rc2)
4853 int r;
4854 SValue *p;
4856 for(r=0;r<NB_REGS;r++) {
4857 if (reg_classes[r] & rc2) {
4858 int n;
4859 n=0;
4860 for(p = vstack; p <= vtop; p++) {
4861 if ((p->r & VT_VALMASK) == r ||
4862 (p->r2 & VT_VALMASK) == r)
4863 n++;
4865 if (n <= 1)
4866 return r;
4869 return get_reg(rc);
4872 /* find a free register of class 'rc'. If none, save one register */
4873 int get_reg(int rc)
4875 int r;
4876 SValue *p;
4878 /* find a free register */
4879 for(r=0;r<NB_REGS;r++) {
4880 if (reg_classes[r] & rc) {
4881 for(p=vstack;p<=vtop;p++) {
4882 if ((p->r & VT_VALMASK) == r ||
4883 (p->r2 & VT_VALMASK) == r)
4884 goto notfound;
4886 return r;
4888 notfound: ;
4891 /* no register left : free the first one on the stack (VERY
4892 IMPORTANT to start from the bottom to ensure that we don't
4893 spill registers used in gen_opi()) */
4894 for(p=vstack;p<=vtop;p++) {
4895 r = p->r & VT_VALMASK;
4896 if (r < VT_CONST && (reg_classes[r] & rc))
4897 goto save_found;
4898 /* also look at second register (if long long) */
4899 r = p->r2 & VT_VALMASK;
4900 if (r < VT_CONST && (reg_classes[r] & rc)) {
4901 save_found:
4902 save_reg(r);
4903 return r;
4906 /* Should never comes here */
4907 return -1;
4910 /* save registers up to (vtop - n) stack entry */
4911 void save_regs(int n)
4913 int r;
4914 SValue *p, *p1;
4915 p1 = vtop - n;
4916 for(p = vstack;p <= p1; p++) {
4917 r = p->r & VT_VALMASK;
4918 if (r < VT_CONST) {
4919 save_reg(r);
4924 /* move register 's' to 'r', and flush previous value of r to memory
4925 if needed */
4926 void move_reg(int r, int s)
4928 SValue sv;
4930 if (r != s) {
4931 save_reg(r);
4932 sv.type.t = VT_INT;
4933 sv.r = s;
4934 sv.c.ul = 0;
4935 load(r, &sv);
4939 /* get address of vtop (vtop MUST BE an lvalue) */
4940 void gaddrof(void)
4942 vtop->r &= ~VT_LVAL;
4943 /* tricky: if saved lvalue, then we can go back to lvalue */
4944 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4945 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4948 #ifdef CONFIG_TCC_BCHECK
4949 /* generate lvalue bound code */
4950 void gbound(void)
4952 int lval_type;
4953 CType type1;
4955 vtop->r &= ~VT_MUSTBOUND;
4956 /* if lvalue, then use checking code before dereferencing */
4957 if (vtop->r & VT_LVAL) {
4958 /* if not VT_BOUNDED value, then make one */
4959 if (!(vtop->r & VT_BOUNDED)) {
4960 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4961 /* must save type because we must set it to int to get pointer */
4962 type1 = vtop->type;
4963 vtop->type.t = VT_INT;
4964 gaddrof();
4965 vpushi(0);
4966 gen_bounded_ptr_add();
4967 vtop->r |= lval_type;
4968 vtop->type = type1;
4970 /* then check for dereferencing */
4971 gen_bounded_ptr_deref();
4974 #endif
4976 /* store vtop a register belonging to class 'rc'. lvalues are
4977 converted to values. Cannot be used if cannot be converted to
4978 register value (such as structures). */
4979 int gv(int rc)
4981 int r, rc2, bit_pos, bit_size, size, align, i;
4983 /* NOTE: get_reg can modify vstack[] */
4984 if (vtop->type.t & VT_BITFIELD) {
4985 CType type;
4986 int bits = 32;
4987 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4988 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4989 /* remove bit field info to avoid loops */
4990 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4991 /* cast to int to propagate signedness in following ops */
4992 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4993 type.t = VT_LLONG;
4994 bits = 64;
4995 } else
4996 type.t = VT_INT;
4997 if((vtop->type.t & VT_UNSIGNED) ||
4998 (vtop->type.t & VT_BTYPE) == VT_BOOL)
4999 type.t |= VT_UNSIGNED;
5000 gen_cast(&type);
5001 /* generate shifts */
5002 vpushi(bits - (bit_pos + bit_size));
5003 gen_op(TOK_SHL);
5004 vpushi(bits - bit_size);
5005 /* NOTE: transformed to SHR if unsigned */
5006 gen_op(TOK_SAR);
5007 r = gv(rc);
5008 } else {
5009 if (is_float(vtop->type.t) &&
5010 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
5011 Sym *sym;
5012 int *ptr;
5013 unsigned long offset;
5014 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5015 CValue check;
5016 #endif
5018 /* XXX: unify with initializers handling ? */
5019 /* CPUs usually cannot use float constants, so we store them
5020 generically in data segment */
5021 size = type_size(&vtop->type, &align);
5022 offset = (data_section->data_offset + align - 1) & -align;
5023 data_section->data_offset = offset;
5024 /* XXX: not portable yet */
5025 #if defined(__i386__) || defined(__x86_64__)
5026 /* Zero pad x87 tenbyte long doubles */
5027 if (size == LDOUBLE_SIZE)
5028 vtop->c.tab[2] &= 0xffff;
5029 #endif
5030 ptr = section_ptr_add(data_section, size);
5031 size = size >> 2;
5032 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5033 check.d = 1;
5034 if(check.tab[0])
5035 for(i=0;i<size;i++)
5036 ptr[i] = vtop->c.tab[size-1-i];
5037 else
5038 #endif
5039 for(i=0;i<size;i++)
5040 ptr[i] = vtop->c.tab[i];
5041 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
5042 vtop->r |= VT_LVAL | VT_SYM;
5043 vtop->sym = sym;
5044 vtop->c.ul = 0;
5046 #ifdef CONFIG_TCC_BCHECK
5047 if (vtop->r & VT_MUSTBOUND)
5048 gbound();
5049 #endif
5051 r = vtop->r & VT_VALMASK;
5052 rc2 = RC_INT;
5053 if (rc == RC_IRET)
5054 rc2 = RC_LRET;
5055 /* need to reload if:
5056 - constant
5057 - lvalue (need to dereference pointer)
5058 - already a register, but not in the right class */
5059 if (r >= VT_CONST ||
5060 (vtop->r & VT_LVAL) ||
5061 !(reg_classes[r] & rc) ||
5062 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
5063 !(reg_classes[vtop->r2] & rc2))) {
5064 r = get_reg(rc);
5065 #ifndef TCC_TARGET_X86_64
5066 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
5067 int r2;
5068 unsigned long long ll;
5069 /* two register type load : expand to two words
5070 temporarily */
5071 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
5072 /* load constant */
5073 ll = vtop->c.ull;
5074 vtop->c.ui = ll; /* first word */
5075 load(r, vtop);
5076 vtop->r = r; /* save register value */
5077 vpushi(ll >> 32); /* second word */
5078 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
5079 (vtop->r & VT_LVAL)) {
5080 /* We do not want to modifier the long long
5081 pointer here, so the safest (and less
5082 efficient) is to save all the other registers
5083 in the stack. XXX: totally inefficient. */
5084 save_regs(1);
5085 /* load from memory */
5086 load(r, vtop);
5087 vdup();
5088 vtop[-1].r = r; /* save register value */
5089 /* increment pointer to get second word */
5090 vtop->type.t = VT_INT;
5091 gaddrof();
5092 vpushi(4);
5093 gen_op('+');
5094 vtop->r |= VT_LVAL;
5095 } else {
5096 /* move registers */
5097 load(r, vtop);
5098 vdup();
5099 vtop[-1].r = r; /* save register value */
5100 vtop->r = vtop[-1].r2;
5102 /* allocate second register */
5103 r2 = get_reg(rc2);
5104 load(r2, vtop);
5105 vpop();
5106 /* write second register */
5107 vtop->r2 = r2;
5108 } else
5109 #endif
5110 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5111 int t1, t;
5112 /* lvalue of scalar type : need to use lvalue type
5113 because of possible cast */
5114 t = vtop->type.t;
5115 t1 = t;
5116 /* compute memory access type */
5117 if (vtop->r & VT_LVAL_BYTE)
5118 t = VT_BYTE;
5119 else if (vtop->r & VT_LVAL_SHORT)
5120 t = VT_SHORT;
5121 if (vtop->r & VT_LVAL_UNSIGNED)
5122 t |= VT_UNSIGNED;
5123 vtop->type.t = t;
5124 load(r, vtop);
5125 /* restore wanted type */
5126 vtop->type.t = t1;
5127 } else {
5128 /* one register type load */
5129 load(r, vtop);
5132 vtop->r = r;
5133 #ifdef TCC_TARGET_C67
5134 /* uses register pairs for doubles */
5135 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5136 vtop->r2 = r+1;
5137 #endif
5139 return r;
5142 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5143 void gv2(int rc1, int rc2)
5145 int v;
5147 /* generate more generic register first. But VT_JMP or VT_CMP
5148 values must be generated first in all cases to avoid possible
5149 reload errors */
5150 v = vtop[0].r & VT_VALMASK;
5151 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5152 vswap();
5153 gv(rc1);
5154 vswap();
5155 gv(rc2);
5156 /* test if reload is needed for first register */
5157 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5158 vswap();
5159 gv(rc1);
5160 vswap();
5162 } else {
5163 gv(rc2);
5164 vswap();
5165 gv(rc1);
5166 vswap();
5167 /* test if reload is needed for first register */
5168 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5169 gv(rc2);
5174 /* expand long long on stack in two int registers */
5175 void lexpand(void)
5177 int u;
5179 u = vtop->type.t & VT_UNSIGNED;
5180 gv(RC_INT);
5181 vdup();
5182 vtop[0].r = vtop[-1].r2;
5183 vtop[0].r2 = VT_CONST;
5184 vtop[-1].r2 = VT_CONST;
5185 vtop[0].type.t = VT_INT | u;
5186 vtop[-1].type.t = VT_INT | u;
5189 #ifdef TCC_TARGET_ARM
5190 /* expand long long on stack */
5191 void lexpand_nr(void)
5193 int u,v;
5195 u = vtop->type.t & VT_UNSIGNED;
5196 vdup();
5197 vtop->r2 = VT_CONST;
5198 vtop->type.t = VT_INT | u;
5199 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5200 if (v == VT_CONST) {
5201 vtop[-1].c.ui = vtop->c.ull;
5202 vtop->c.ui = vtop->c.ull >> 32;
5203 vtop->r = VT_CONST;
5204 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5205 vtop->c.ui += 4;
5206 vtop->r = vtop[-1].r;
5207 } else if (v > VT_CONST) {
5208 vtop--;
5209 lexpand();
5210 } else
5211 vtop->r = vtop[-1].r2;
5212 vtop[-1].r2 = VT_CONST;
5213 vtop[-1].type.t = VT_INT | u;
5215 #endif
5217 /* build a long long from two ints */
5218 void lbuild(int t)
5220 gv2(RC_INT, RC_INT);
5221 vtop[-1].r2 = vtop[0].r;
5222 vtop[-1].type.t = t;
5223 vpop();
5226 /* rotate n first stack elements to the bottom
5227 I1 ... In -> I2 ... In I1 [top is right]
5229 void vrotb(int n)
5231 int i;
5232 SValue tmp;
5234 tmp = vtop[-n + 1];
5235 for(i=-n+1;i!=0;i++)
5236 vtop[i] = vtop[i+1];
5237 vtop[0] = tmp;
5240 /* rotate n first stack elements to the top
5241 I1 ... In -> In I1 ... I(n-1) [top is right]
5243 void vrott(int n)
5245 int i;
5246 SValue tmp;
5248 tmp = vtop[0];
5249 for(i = 0;i < n - 1; i++)
5250 vtop[-i] = vtop[-i - 1];
5251 vtop[-n + 1] = tmp;
5254 #ifdef TCC_TARGET_ARM
5255 /* like vrott but in other direction
5256 In ... I1 -> I(n-1) ... I1 In [top is right]
5258 void vnrott(int n)
5260 int i;
5261 SValue tmp;
5263 tmp = vtop[-n + 1];
5264 for(i = n - 1; i > 0; i--)
5265 vtop[-i] = vtop[-i + 1];
5266 vtop[0] = tmp;
5268 #endif
5270 /* pop stack value */
5271 void vpop(void)
5273 int v;
5274 v = vtop->r & VT_VALMASK;
5275 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
5276 /* for x86, we need to pop the FP stack */
5277 if (v == TREG_ST0 && !nocode_wanted) {
5278 o(0xd9dd); /* fstp %st(1) */
5279 } else
5280 #endif
5281 if (v == VT_JMP || v == VT_JMPI) {
5282 /* need to put correct jump if && or || without test */
5283 gsym(vtop->c.ul);
5285 vtop--;
5288 /* convert stack entry to register and duplicate its value in another
5289 register */
5290 void gv_dup(void)
5292 int rc, t, r, r1;
5293 SValue sv;
5295 t = vtop->type.t;
5296 if ((t & VT_BTYPE) == VT_LLONG) {
5297 lexpand();
5298 gv_dup();
5299 vswap();
5300 vrotb(3);
5301 gv_dup();
5302 vrotb(4);
5303 /* stack: H L L1 H1 */
5304 lbuild(t);
5305 vrotb(3);
5306 vrotb(3);
5307 vswap();
5308 lbuild(t);
5309 vswap();
5310 } else {
5311 /* duplicate value */
5312 rc = RC_INT;
5313 sv.type.t = VT_INT;
5314 if (is_float(t)) {
5315 rc = RC_FLOAT;
5316 #ifdef TCC_TARGET_X86_64
5317 if ((t & VT_BTYPE) == VT_LDOUBLE) {
5318 rc = RC_ST0;
5320 #endif
5321 sv.type.t = t;
5323 r = gv(rc);
5324 r1 = get_reg(rc);
5325 sv.r = r;
5326 sv.c.ul = 0;
5327 load(r1, &sv); /* move r to r1 */
5328 vdup();
5329 /* duplicates value */
5330 vtop->r = r1;
5334 #ifndef TCC_TARGET_X86_64
5335 /* generate CPU independent (unsigned) long long operations */
5336 void gen_opl(int op)
5338 int t, a, b, op1, c, i;
5339 int func;
5340 unsigned short reg_iret = REG_IRET;
5341 unsigned short reg_lret = REG_LRET;
5342 SValue tmp;
5344 switch(op) {
5345 case '/':
5346 case TOK_PDIV:
5347 func = TOK___divdi3;
5348 goto gen_func;
5349 case TOK_UDIV:
5350 func = TOK___udivdi3;
5351 goto gen_func;
5352 case '%':
5353 func = TOK___moddi3;
5354 goto gen_mod_func;
5355 case TOK_UMOD:
5356 func = TOK___umoddi3;
5357 gen_mod_func:
5358 #ifdef TCC_ARM_EABI
5359 reg_iret = TREG_R2;
5360 reg_lret = TREG_R3;
5361 #endif
5362 gen_func:
5363 /* call generic long long function */
5364 vpush_global_sym(&func_old_type, func);
5365 vrott(3);
5366 gfunc_call(2);
5367 vpushi(0);
5368 vtop->r = reg_iret;
5369 vtop->r2 = reg_lret;
5370 break;
5371 case '^':
5372 case '&':
5373 case '|':
5374 case '*':
5375 case '+':
5376 case '-':
5377 t = vtop->type.t;
5378 vswap();
5379 lexpand();
5380 vrotb(3);
5381 lexpand();
5382 /* stack: L1 H1 L2 H2 */
5383 tmp = vtop[0];
5384 vtop[0] = vtop[-3];
5385 vtop[-3] = tmp;
5386 tmp = vtop[-2];
5387 vtop[-2] = vtop[-3];
5388 vtop[-3] = tmp;
5389 vswap();
5390 /* stack: H1 H2 L1 L2 */
5391 if (op == '*') {
5392 vpushv(vtop - 1);
5393 vpushv(vtop - 1);
5394 gen_op(TOK_UMULL);
5395 lexpand();
5396 /* stack: H1 H2 L1 L2 ML MH */
5397 for(i=0;i<4;i++)
5398 vrotb(6);
5399 /* stack: ML MH H1 H2 L1 L2 */
5400 tmp = vtop[0];
5401 vtop[0] = vtop[-2];
5402 vtop[-2] = tmp;
5403 /* stack: ML MH H1 L2 H2 L1 */
5404 gen_op('*');
5405 vrotb(3);
5406 vrotb(3);
5407 gen_op('*');
5408 /* stack: ML MH M1 M2 */
5409 gen_op('+');
5410 gen_op('+');
5411 } else if (op == '+' || op == '-') {
5412 /* XXX: add non carry method too (for MIPS or alpha) */
5413 if (op == '+')
5414 op1 = TOK_ADDC1;
5415 else
5416 op1 = TOK_SUBC1;
5417 gen_op(op1);
5418 /* stack: H1 H2 (L1 op L2) */
5419 vrotb(3);
5420 vrotb(3);
5421 gen_op(op1 + 1); /* TOK_xxxC2 */
5422 } else {
5423 gen_op(op);
5424 /* stack: H1 H2 (L1 op L2) */
5425 vrotb(3);
5426 vrotb(3);
5427 /* stack: (L1 op L2) H1 H2 */
5428 gen_op(op);
5429 /* stack: (L1 op L2) (H1 op H2) */
5431 /* stack: L H */
5432 lbuild(t);
5433 break;
5434 case TOK_SAR:
5435 case TOK_SHR:
5436 case TOK_SHL:
5437 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5438 t = vtop[-1].type.t;
5439 vswap();
5440 lexpand();
5441 vrotb(3);
5442 /* stack: L H shift */
5443 c = (int)vtop->c.i;
5444 /* constant: simpler */
5445 /* NOTE: all comments are for SHL. the other cases are
5446 done by swaping words */
5447 vpop();
5448 if (op != TOK_SHL)
5449 vswap();
5450 if (c >= 32) {
5451 /* stack: L H */
5452 vpop();
5453 if (c > 32) {
5454 vpushi(c - 32);
5455 gen_op(op);
5457 if (op != TOK_SAR) {
5458 vpushi(0);
5459 } else {
5460 gv_dup();
5461 vpushi(31);
5462 gen_op(TOK_SAR);
5464 vswap();
5465 } else {
5466 vswap();
5467 gv_dup();
5468 /* stack: H L L */
5469 vpushi(c);
5470 gen_op(op);
5471 vswap();
5472 vpushi(32 - c);
5473 if (op == TOK_SHL)
5474 gen_op(TOK_SHR);
5475 else
5476 gen_op(TOK_SHL);
5477 vrotb(3);
5478 /* stack: L L H */
5479 vpushi(c);
5480 if (op == TOK_SHL)
5481 gen_op(TOK_SHL);
5482 else
5483 gen_op(TOK_SHR);
5484 gen_op('|');
5486 if (op != TOK_SHL)
5487 vswap();
5488 lbuild(t);
5489 } else {
5490 /* XXX: should provide a faster fallback on x86 ? */
5491 switch(op) {
5492 case TOK_SAR:
5493 func = TOK___ashrdi3;
5494 goto gen_func;
5495 case TOK_SHR:
5496 func = TOK___lshrdi3;
5497 goto gen_func;
5498 case TOK_SHL:
5499 func = TOK___ashldi3;
5500 goto gen_func;
5503 break;
5504 default:
5505 /* compare operations */
5506 t = vtop->type.t;
5507 vswap();
5508 lexpand();
5509 vrotb(3);
5510 lexpand();
5511 /* stack: L1 H1 L2 H2 */
5512 tmp = vtop[-1];
5513 vtop[-1] = vtop[-2];
5514 vtop[-2] = tmp;
5515 /* stack: L1 L2 H1 H2 */
5516 /* compare high */
5517 op1 = op;
5518 /* when values are equal, we need to compare low words. since
5519 the jump is inverted, we invert the test too. */
5520 if (op1 == TOK_LT)
5521 op1 = TOK_LE;
5522 else if (op1 == TOK_GT)
5523 op1 = TOK_GE;
5524 else if (op1 == TOK_ULT)
5525 op1 = TOK_ULE;
5526 else if (op1 == TOK_UGT)
5527 op1 = TOK_UGE;
5528 a = 0;
5529 b = 0;
5530 gen_op(op1);
5531 if (op1 != TOK_NE) {
5532 a = gtst(1, 0);
5534 if (op != TOK_EQ) {
5535 /* generate non equal test */
5536 /* XXX: NOT PORTABLE yet */
5537 if (a == 0) {
5538 b = gtst(0, 0);
5539 } else {
5540 #if defined(TCC_TARGET_I386)
5541 b = psym(0x850f, 0);
5542 #elif defined(TCC_TARGET_ARM)
5543 b = ind;
5544 o(0x1A000000 | encbranch(ind, 0, 1));
5545 #elif defined(TCC_TARGET_C67)
5546 error("not implemented");
5547 #else
5548 #error not supported
5549 #endif
5552 /* compare low. Always unsigned */
5553 op1 = op;
5554 if (op1 == TOK_LT)
5555 op1 = TOK_ULT;
5556 else if (op1 == TOK_LE)
5557 op1 = TOK_ULE;
5558 else if (op1 == TOK_GT)
5559 op1 = TOK_UGT;
5560 else if (op1 == TOK_GE)
5561 op1 = TOK_UGE;
5562 gen_op(op1);
5563 a = gtst(1, a);
5564 gsym(b);
5565 vseti(VT_JMPI, a);
5566 break;
5569 #endif
5571 /* handle integer constant optimizations and various machine
5572 independent opt */
5573 void gen_opic(int op)
5575 int c1, c2, t1, t2, n;
5576 SValue *v1, *v2;
5577 long long l1, l2;
5578 typedef unsigned long long U;
5580 v1 = vtop - 1;
5581 v2 = vtop;
5582 t1 = v1->type.t & VT_BTYPE;
5583 t2 = v2->type.t & VT_BTYPE;
5585 if (t1 == VT_LLONG)
5586 l1 = v1->c.ll;
5587 else if (v1->type.t & VT_UNSIGNED)
5588 l1 = v1->c.ui;
5589 else
5590 l1 = v1->c.i;
5592 if (t2 == VT_LLONG)
5593 l2 = v2->c.ll;
5594 else if (v2->type.t & VT_UNSIGNED)
5595 l2 = v2->c.ui;
5596 else
5597 l2 = v2->c.i;
5599 /* currently, we cannot do computations with forward symbols */
5600 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5601 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5602 if (c1 && c2) {
5603 switch(op) {
5604 case '+': l1 += l2; break;
5605 case '-': l1 -= l2; break;
5606 case '&': l1 &= l2; break;
5607 case '^': l1 ^= l2; break;
5608 case '|': l1 |= l2; break;
5609 case '*': l1 *= l2; break;
5611 case TOK_PDIV:
5612 case '/':
5613 case '%':
5614 case TOK_UDIV:
5615 case TOK_UMOD:
5616 /* if division by zero, generate explicit division */
5617 if (l2 == 0) {
5618 if (const_wanted)
5619 error("division by zero in constant");
5620 goto general_case;
5622 switch(op) {
5623 default: l1 /= l2; break;
5624 case '%': l1 %= l2; break;
5625 case TOK_UDIV: l1 = (U)l1 / l2; break;
5626 case TOK_UMOD: l1 = (U)l1 % l2; break;
5628 break;
5629 case TOK_SHL: l1 <<= l2; break;
5630 case TOK_SHR: l1 = (U)l1 >> l2; break;
5631 case TOK_SAR: l1 >>= l2; break;
5632 /* tests */
5633 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5634 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5635 case TOK_EQ: l1 = l1 == l2; break;
5636 case TOK_NE: l1 = l1 != l2; break;
5637 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5638 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5639 case TOK_LT: l1 = l1 < l2; break;
5640 case TOK_GE: l1 = l1 >= l2; break;
5641 case TOK_LE: l1 = l1 <= l2; break;
5642 case TOK_GT: l1 = l1 > l2; break;
5643 /* logical */
5644 case TOK_LAND: l1 = l1 && l2; break;
5645 case TOK_LOR: l1 = l1 || l2; break;
5646 default:
5647 goto general_case;
5649 v1->c.ll = l1;
5650 vtop--;
5651 } else {
5652 /* if commutative ops, put c2 as constant */
5653 if (c1 && (op == '+' || op == '&' || op == '^' ||
5654 op == '|' || op == '*')) {
5655 vswap();
5656 c2 = c1; //c = c1, c1 = c2, c2 = c;
5657 l2 = l1; //l = l1, l1 = l2, l2 = l;
5659 /* Filter out NOP operations like x*1, x-0, x&-1... */
5660 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5661 op == TOK_PDIV) &&
5662 l2 == 1) ||
5663 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5664 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5665 l2 == 0) ||
5666 (op == '&' &&
5667 l2 == -1))) {
5668 /* nothing to do */
5669 vtop--;
5670 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5671 /* try to use shifts instead of muls or divs */
5672 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5673 n = -1;
5674 while (l2) {
5675 l2 >>= 1;
5676 n++;
5678 vtop->c.ll = n;
5679 if (op == '*')
5680 op = TOK_SHL;
5681 else if (op == TOK_PDIV)
5682 op = TOK_SAR;
5683 else
5684 op = TOK_SHR;
5686 goto general_case;
5687 } else if (c2 && (op == '+' || op == '-') &&
5688 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5689 (VT_CONST | VT_SYM) ||
5690 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
5691 /* symbol + constant case */
5692 if (op == '-')
5693 l2 = -l2;
5694 vtop--;
5695 vtop->c.ll += l2;
5696 } else {
5697 general_case:
5698 if (!nocode_wanted) {
5699 /* call low level op generator */
5700 if (t1 == VT_LLONG || t2 == VT_LLONG)
5701 gen_opl(op);
5702 else
5703 gen_opi(op);
5704 } else {
5705 vtop--;
5711 /* generate a floating point operation with constant propagation */
5712 void gen_opif(int op)
5714 int c1, c2;
5715 SValue *v1, *v2;
5716 long double f1, f2;
5718 v1 = vtop - 1;
5719 v2 = vtop;
5720 /* currently, we cannot do computations with forward symbols */
5721 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5722 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5723 if (c1 && c2) {
5724 if (v1->type.t == VT_FLOAT) {
5725 f1 = v1->c.f;
5726 f2 = v2->c.f;
5727 } else if (v1->type.t == VT_DOUBLE) {
5728 f1 = v1->c.d;
5729 f2 = v2->c.d;
5730 } else {
5731 f1 = v1->c.ld;
5732 f2 = v2->c.ld;
5735 /* NOTE: we only do constant propagation if finite number (not
5736 NaN or infinity) (ANSI spec) */
5737 if (!ieee_finite(f1) || !ieee_finite(f2))
5738 goto general_case;
5740 switch(op) {
5741 case '+': f1 += f2; break;
5742 case '-': f1 -= f2; break;
5743 case '*': f1 *= f2; break;
5744 case '/':
5745 if (f2 == 0.0) {
5746 if (const_wanted)
5747 error("division by zero in constant");
5748 goto general_case;
5750 f1 /= f2;
5751 break;
5752 /* XXX: also handles tests ? */
5753 default:
5754 goto general_case;
5756 /* XXX: overflow test ? */
5757 if (v1->type.t == VT_FLOAT) {
5758 v1->c.f = f1;
5759 } else if (v1->type.t == VT_DOUBLE) {
5760 v1->c.d = f1;
5761 } else {
5762 v1->c.ld = f1;
5764 vtop--;
5765 } else {
5766 general_case:
5767 if (!nocode_wanted) {
5768 gen_opf(op);
5769 } else {
5770 vtop--;
5775 static int pointed_size(CType *type)
5777 int align;
5778 return type_size(pointed_type(type), &align);
5781 static inline int is_null_pointer(SValue *p)
5783 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5784 return 0;
5785 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5786 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5789 static inline int is_integer_btype(int bt)
5791 return (bt == VT_BYTE || bt == VT_SHORT ||
5792 bt == VT_INT || bt == VT_LLONG);
5795 /* check types for comparison or substraction of pointers */
5796 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5798 CType *type1, *type2, tmp_type1, tmp_type2;
5799 int bt1, bt2;
5801 /* null pointers are accepted for all comparisons as gcc */
5802 if (is_null_pointer(p1) || is_null_pointer(p2))
5803 return;
5804 type1 = &p1->type;
5805 type2 = &p2->type;
5806 bt1 = type1->t & VT_BTYPE;
5807 bt2 = type2->t & VT_BTYPE;
5808 /* accept comparison between pointer and integer with a warning */
5809 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5810 if (op != TOK_LOR && op != TOK_LAND )
5811 warning("comparison between pointer and integer");
5812 return;
5815 /* both must be pointers or implicit function pointers */
5816 if (bt1 == VT_PTR) {
5817 type1 = pointed_type(type1);
5818 } else if (bt1 != VT_FUNC)
5819 goto invalid_operands;
5821 if (bt2 == VT_PTR) {
5822 type2 = pointed_type(type2);
5823 } else if (bt2 != VT_FUNC) {
5824 invalid_operands:
5825 error("invalid operands to binary %s", get_tok_str(op, NULL));
5827 if ((type1->t & VT_BTYPE) == VT_VOID ||
5828 (type2->t & VT_BTYPE) == VT_VOID)
5829 return;
5830 tmp_type1 = *type1;
5831 tmp_type2 = *type2;
5832 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5833 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5834 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5835 /* gcc-like error if '-' is used */
5836 if (op == '-')
5837 goto invalid_operands;
5838 else
5839 warning("comparison of distinct pointer types lacks a cast");
5843 /* generic gen_op: handles types problems */
5844 void gen_op(int op)
5846 int u, t1, t2, bt1, bt2, t;
5847 CType type1;
5849 t1 = vtop[-1].type.t;
5850 t2 = vtop[0].type.t;
5851 bt1 = t1 & VT_BTYPE;
5852 bt2 = t2 & VT_BTYPE;
5854 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5855 /* at least one operand is a pointer */
5856 /* relationnal op: must be both pointers */
5857 if (op >= TOK_ULT && op <= TOK_LOR) {
5858 check_comparison_pointer_types(vtop - 1, vtop, op);
5859 /* pointers are handled are unsigned */
5860 #ifdef TCC_TARGET_X86_64
5861 t = VT_LLONG | VT_UNSIGNED;
5862 #else
5863 t = VT_INT | VT_UNSIGNED;
5864 #endif
5865 goto std_op;
5867 /* if both pointers, then it must be the '-' op */
5868 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5869 if (op != '-')
5870 error("cannot use pointers here");
5871 check_comparison_pointer_types(vtop - 1, vtop, op);
5872 /* XXX: check that types are compatible */
5873 u = pointed_size(&vtop[-1].type);
5874 gen_opic(op);
5875 /* set to integer type */
5876 #ifdef TCC_TARGET_X86_64
5877 vtop->type.t = VT_LLONG;
5878 #else
5879 vtop->type.t = VT_INT;
5880 #endif
5881 vpushi(u);
5882 gen_op(TOK_PDIV);
5883 } else {
5884 /* exactly one pointer : must be '+' or '-'. */
5885 if (op != '-' && op != '+')
5886 error("cannot use pointers here");
5887 /* Put pointer as first operand */
5888 if (bt2 == VT_PTR) {
5889 vswap();
5890 swap(&t1, &t2);
5892 type1 = vtop[-1].type;
5893 #ifdef TCC_TARGET_X86_64
5894 vpushll(pointed_size(&vtop[-1].type));
5895 #else
5896 /* XXX: cast to int ? (long long case) */
5897 vpushi(pointed_size(&vtop[-1].type));
5898 #endif
5899 gen_op('*');
5900 #ifdef CONFIG_TCC_BCHECK
5901 /* if evaluating constant expression, no code should be
5902 generated, so no bound check */
5903 if (do_bounds_check && !const_wanted) {
5904 /* if bounded pointers, we generate a special code to
5905 test bounds */
5906 if (op == '-') {
5907 vpushi(0);
5908 vswap();
5909 gen_op('-');
5911 gen_bounded_ptr_add();
5912 } else
5913 #endif
5915 gen_opic(op);
5917 /* put again type if gen_opic() swaped operands */
5918 vtop->type = type1;
5920 } else if (is_float(bt1) || is_float(bt2)) {
5921 /* compute bigger type and do implicit casts */
5922 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5923 t = VT_LDOUBLE;
5924 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5925 t = VT_DOUBLE;
5926 } else {
5927 t = VT_FLOAT;
5929 /* floats can only be used for a few operations */
5930 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5931 (op < TOK_ULT || op > TOK_GT))
5932 error("invalid operands for binary operation");
5933 goto std_op;
5934 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5935 /* cast to biggest op */
5936 t = VT_LLONG;
5937 /* convert to unsigned if it does not fit in a long long */
5938 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5939 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5940 t |= VT_UNSIGNED;
5941 goto std_op;
5942 } else {
5943 /* integer operations */
5944 t = VT_INT;
5945 /* convert to unsigned if it does not fit in an integer */
5946 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5947 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5948 t |= VT_UNSIGNED;
5949 std_op:
5950 /* XXX: currently, some unsigned operations are explicit, so
5951 we modify them here */
5952 if (t & VT_UNSIGNED) {
5953 if (op == TOK_SAR)
5954 op = TOK_SHR;
5955 else if (op == '/')
5956 op = TOK_UDIV;
5957 else if (op == '%')
5958 op = TOK_UMOD;
5959 else if (op == TOK_LT)
5960 op = TOK_ULT;
5961 else if (op == TOK_GT)
5962 op = TOK_UGT;
5963 else if (op == TOK_LE)
5964 op = TOK_ULE;
5965 else if (op == TOK_GE)
5966 op = TOK_UGE;
5968 vswap();
5969 type1.t = t;
5970 gen_cast(&type1);
5971 vswap();
5972 /* special case for shifts and long long: we keep the shift as
5973 an integer */
5974 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5975 type1.t = VT_INT;
5976 gen_cast(&type1);
5977 if (is_float(t))
5978 gen_opif(op);
5979 else
5980 gen_opic(op);
5981 if (op >= TOK_ULT && op <= TOK_GT) {
5982 /* relationnal op: the result is an int */
5983 vtop->type.t = VT_INT;
5984 } else {
5985 vtop->type.t = t;
5990 #ifndef TCC_TARGET_ARM
5991 /* generic itof for unsigned long long case */
5992 void gen_cvt_itof1(int t)
5994 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5995 (VT_LLONG | VT_UNSIGNED)) {
5997 if (t == VT_FLOAT)
5998 vpush_global_sym(&func_old_type, TOK___floatundisf);
5999 #if LDOUBLE_SIZE != 8
6000 else if (t == VT_LDOUBLE)
6001 vpush_global_sym(&func_old_type, TOK___floatundixf);
6002 #endif
6003 else
6004 vpush_global_sym(&func_old_type, TOK___floatundidf);
6005 vrott(2);
6006 gfunc_call(1);
6007 vpushi(0);
6008 vtop->r = REG_FRET;
6009 } else {
6010 gen_cvt_itof(t);
6013 #endif
6015 /* generic ftoi for unsigned long long case */
6016 void gen_cvt_ftoi1(int t)
6018 int st;
6020 if (t == (VT_LLONG | VT_UNSIGNED)) {
6021 /* not handled natively */
6022 st = vtop->type.t & VT_BTYPE;
6023 if (st == VT_FLOAT)
6024 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
6025 #if LDOUBLE_SIZE != 8
6026 else if (st == VT_LDOUBLE)
6027 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
6028 #endif
6029 else
6030 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
6031 vrott(2);
6032 gfunc_call(1);
6033 vpushi(0);
6034 vtop->r = REG_IRET;
6035 vtop->r2 = REG_LRET;
6036 } else {
6037 gen_cvt_ftoi(t);
6041 /* force char or short cast */
6042 void force_charshort_cast(int t)
6044 int bits, dbt;
6045 dbt = t & VT_BTYPE;
6046 /* XXX: add optimization if lvalue : just change type and offset */
6047 if (dbt == VT_BYTE)
6048 bits = 8;
6049 else
6050 bits = 16;
6051 if (t & VT_UNSIGNED) {
6052 vpushi((1 << bits) - 1);
6053 gen_op('&');
6054 } else {
6055 bits = 32 - bits;
6056 vpushi(bits);
6057 gen_op(TOK_SHL);
6058 /* result must be signed or the SAR is converted to an SHL
6059 This was not the case when "t" was a signed short
6060 and the last value on the stack was an unsigned int */
6061 vtop->type.t &= ~VT_UNSIGNED;
6062 vpushi(bits);
6063 gen_op(TOK_SAR);
6067 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
6068 static void gen_cast(CType *type)
6070 int sbt, dbt, sf, df, c, p;
6072 /* special delayed cast for char/short */
6073 /* XXX: in some cases (multiple cascaded casts), it may still
6074 be incorrect */
6075 if (vtop->r & VT_MUSTCAST) {
6076 vtop->r &= ~VT_MUSTCAST;
6077 force_charshort_cast(vtop->type.t);
6080 /* bitfields first get cast to ints */
6081 if (vtop->type.t & VT_BITFIELD) {
6082 gv(RC_INT);
6085 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
6086 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
6088 if (sbt != dbt) {
6089 sf = is_float(sbt);
6090 df = is_float(dbt);
6091 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6092 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
6093 if (c) {
6094 /* constant case: we can do it now */
6095 /* XXX: in ISOC, cannot do it if error in convert */
6096 if (sbt == VT_FLOAT)
6097 vtop->c.ld = vtop->c.f;
6098 else if (sbt == VT_DOUBLE)
6099 vtop->c.ld = vtop->c.d;
6101 if (df) {
6102 if ((sbt & VT_BTYPE) == VT_LLONG) {
6103 if (sbt & VT_UNSIGNED)
6104 vtop->c.ld = vtop->c.ull;
6105 else
6106 vtop->c.ld = vtop->c.ll;
6107 } else if(!sf) {
6108 if (sbt & VT_UNSIGNED)
6109 vtop->c.ld = vtop->c.ui;
6110 else
6111 vtop->c.ld = vtop->c.i;
6114 if (dbt == VT_FLOAT)
6115 vtop->c.f = (float)vtop->c.ld;
6116 else if (dbt == VT_DOUBLE)
6117 vtop->c.d = (double)vtop->c.ld;
6118 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
6119 vtop->c.ull = (unsigned long long)vtop->c.ld;
6120 } else if (sf && dbt == VT_BOOL) {
6121 vtop->c.i = (vtop->c.ld != 0);
6122 } else {
6123 if(sf)
6124 vtop->c.ll = (long long)vtop->c.ld;
6125 else if (sbt == (VT_LLONG|VT_UNSIGNED))
6126 vtop->c.ll = vtop->c.ull;
6127 else if (sbt & VT_UNSIGNED)
6128 vtop->c.ll = vtop->c.ui;
6129 else if (sbt != VT_LLONG)
6130 vtop->c.ll = vtop->c.i;
6132 if (dbt == (VT_LLONG|VT_UNSIGNED))
6133 vtop->c.ull = vtop->c.ll;
6134 else if (dbt == VT_BOOL)
6135 vtop->c.i = (vtop->c.ll != 0);
6136 else if (dbt != VT_LLONG) {
6137 int s = 0;
6138 if ((dbt & VT_BTYPE) == VT_BYTE)
6139 s = 24;
6140 else if ((dbt & VT_BTYPE) == VT_SHORT)
6141 s = 16;
6143 if(dbt & VT_UNSIGNED)
6144 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
6145 else
6146 vtop->c.i = ((int)vtop->c.ll << s) >> s;
6149 } else if (p && dbt == VT_BOOL) {
6150 vtop->r = VT_CONST;
6151 vtop->c.i = 1;
6152 } else if (!nocode_wanted) {
6153 /* non constant case: generate code */
6154 if (sf && df) {
6155 /* convert from fp to fp */
6156 gen_cvt_ftof(dbt);
6157 } else if (df) {
6158 /* convert int to fp */
6159 gen_cvt_itof1(dbt);
6160 } else if (sf) {
6161 /* convert fp to int */
6162 if (dbt == VT_BOOL) {
6163 vpushi(0);
6164 gen_op(TOK_NE);
6165 } else {
6166 /* we handle char/short/etc... with generic code */
6167 if (dbt != (VT_INT | VT_UNSIGNED) &&
6168 dbt != (VT_LLONG | VT_UNSIGNED) &&
6169 dbt != VT_LLONG)
6170 dbt = VT_INT;
6171 gen_cvt_ftoi1(dbt);
6172 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6173 /* additional cast for char/short... */
6174 vtop->type.t = dbt;
6175 gen_cast(type);
6178 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6179 if ((sbt & VT_BTYPE) != VT_LLONG) {
6180 /* scalar to long long */
6181 #ifndef TCC_TARGET_X86_64
6182 /* machine independent conversion */
6183 gv(RC_INT);
6184 /* generate high word */
6185 if (sbt == (VT_INT | VT_UNSIGNED)) {
6186 vpushi(0);
6187 gv(RC_INT);
6188 } else {
6189 gv_dup();
6190 vpushi(31);
6191 gen_op(TOK_SAR);
6193 /* patch second register */
6194 vtop[-1].r2 = vtop->r;
6195 vpop();
6196 #else
6197 int r = gv(RC_INT);
6198 if (sbt != (VT_INT | VT_UNSIGNED)) {
6199 /* x86_64 specific: movslq */
6200 o(0x6348);
6201 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
6203 #endif
6205 } else if (dbt == VT_BOOL) {
6206 /* scalar to bool */
6207 vpushi(0);
6208 gen_op(TOK_NE);
6209 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6210 (dbt & VT_BTYPE) == VT_SHORT) {
6211 if (sbt == VT_PTR) {
6212 vtop->type.t = VT_INT;
6213 warning("nonportable conversion from pointer to char/short");
6215 force_charshort_cast(dbt);
6216 } else if ((dbt & VT_BTYPE) == VT_INT) {
6217 /* scalar to int */
6218 if (sbt == VT_LLONG) {
6219 /* from long long: just take low order word */
6220 lexpand();
6221 vpop();
6223 /* if lvalue and single word type, nothing to do because
6224 the lvalue already contains the real type size (see
6225 VT_LVAL_xxx constants) */
6228 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6229 /* if we are casting between pointer types,
6230 we must update the VT_LVAL_xxx size */
6231 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6232 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6234 vtop->type = *type;
6237 /* return type size. Put alignment at 'a' */
6238 static int type_size(CType *type, int *a)
6240 Sym *s;
6241 int bt;
6243 bt = type->t & VT_BTYPE;
6244 if (bt == VT_STRUCT) {
6245 /* struct/union */
6246 s = type->ref;
6247 *a = s->r;
6248 return s->c;
6249 } else if (bt == VT_PTR) {
6250 if (type->t & VT_ARRAY) {
6251 int ts;
6253 s = type->ref;
6254 ts = type_size(&s->type, a);
6256 if (ts < 0 && s->c < 0)
6257 ts = -ts;
6259 return ts * s->c;
6260 } else {
6261 *a = PTR_SIZE;
6262 return PTR_SIZE;
6264 } else if (bt == VT_LDOUBLE) {
6265 *a = LDOUBLE_ALIGN;
6266 return LDOUBLE_SIZE;
6267 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6268 #ifdef TCC_TARGET_I386
6269 #ifdef TCC_TARGET_PE
6270 *a = 8;
6271 #else
6272 *a = 4;
6273 #endif
6274 #elif defined(TCC_TARGET_ARM)
6275 #ifdef TCC_ARM_EABI
6276 *a = 8;
6277 #else
6278 *a = 4;
6279 #endif
6280 #else
6281 *a = 8;
6282 #endif
6283 return 8;
6284 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6285 *a = 4;
6286 return 4;
6287 } else if (bt == VT_SHORT) {
6288 *a = 2;
6289 return 2;
6290 } else {
6291 /* char, void, function, _Bool */
6292 *a = 1;
6293 return 1;
6297 /* return the pointed type of t */
6298 static inline CType *pointed_type(CType *type)
6300 return &type->ref->type;
6303 /* modify type so that its it is a pointer to type. */
6304 static void mk_pointer(CType *type)
6306 Sym *s;
6307 s = sym_push(SYM_FIELD, type, 0, -1);
6308 type->t = VT_PTR | (type->t & ~VT_TYPE);
6309 type->ref = s;
6312 /* compare function types. OLD functions match any new functions */
6313 static int is_compatible_func(CType *type1, CType *type2)
6315 Sym *s1, *s2;
6317 s1 = type1->ref;
6318 s2 = type2->ref;
6319 if (!is_compatible_types(&s1->type, &s2->type))
6320 return 0;
6321 /* check func_call */
6322 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6323 return 0;
6324 /* XXX: not complete */
6325 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6326 return 1;
6327 if (s1->c != s2->c)
6328 return 0;
6329 while (s1 != NULL) {
6330 if (s2 == NULL)
6331 return 0;
6332 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6333 return 0;
6334 s1 = s1->next;
6335 s2 = s2->next;
6337 if (s2)
6338 return 0;
6339 return 1;
6342 /* return true if type1 and type2 are the same. If unqualified is
6343 true, qualifiers on the types are ignored.
6345 - enums are not checked as gcc __builtin_types_compatible_p ()
6347 static int compare_types(CType *type1, CType *type2, int unqualified)
6349 int bt1, t1, t2;
6351 t1 = type1->t & VT_TYPE;
6352 t2 = type2->t & VT_TYPE;
6353 if (unqualified) {
6354 /* strip qualifiers before comparing */
6355 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6356 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6358 /* XXX: bitfields ? */
6359 if (t1 != t2)
6360 return 0;
6361 /* test more complicated cases */
6362 bt1 = t1 & VT_BTYPE;
6363 if (bt1 == VT_PTR) {
6364 type1 = pointed_type(type1);
6365 type2 = pointed_type(type2);
6366 return is_compatible_types(type1, type2);
6367 } else if (bt1 == VT_STRUCT) {
6368 return (type1->ref == type2->ref);
6369 } else if (bt1 == VT_FUNC) {
6370 return is_compatible_func(type1, type2);
6371 } else {
6372 return 1;
6376 /* return true if type1 and type2 are exactly the same (including
6377 qualifiers).
6379 static int is_compatible_types(CType *type1, CType *type2)
6381 return compare_types(type1,type2,0);
6384 /* return true if type1 and type2 are the same (ignoring qualifiers).
6386 static int is_compatible_parameter_types(CType *type1, CType *type2)
6388 return compare_types(type1,type2,1);
6391 /* print a type. If 'varstr' is not NULL, then the variable is also
6392 printed in the type */
6393 /* XXX: union */
6394 /* XXX: add array and function pointers */
6395 void type_to_str(char *buf, int buf_size,
6396 CType *type, const char *varstr)
6398 int bt, v, t;
6399 Sym *s, *sa;
6400 char buf1[256];
6401 const char *tstr;
6403 t = type->t & VT_TYPE;
6404 bt = t & VT_BTYPE;
6405 buf[0] = '\0';
6406 if (t & VT_CONSTANT)
6407 pstrcat(buf, buf_size, "const ");
6408 if (t & VT_VOLATILE)
6409 pstrcat(buf, buf_size, "volatile ");
6410 if (t & VT_UNSIGNED)
6411 pstrcat(buf, buf_size, "unsigned ");
6412 switch(bt) {
6413 case VT_VOID:
6414 tstr = "void";
6415 goto add_tstr;
6416 case VT_BOOL:
6417 tstr = "_Bool";
6418 goto add_tstr;
6419 case VT_BYTE:
6420 tstr = "char";
6421 goto add_tstr;
6422 case VT_SHORT:
6423 tstr = "short";
6424 goto add_tstr;
6425 case VT_INT:
6426 tstr = "int";
6427 goto add_tstr;
6428 case VT_LONG:
6429 tstr = "long";
6430 goto add_tstr;
6431 case VT_LLONG:
6432 tstr = "long long";
6433 goto add_tstr;
6434 case VT_FLOAT:
6435 tstr = "float";
6436 goto add_tstr;
6437 case VT_DOUBLE:
6438 tstr = "double";
6439 goto add_tstr;
6440 case VT_LDOUBLE:
6441 tstr = "long double";
6442 add_tstr:
6443 pstrcat(buf, buf_size, tstr);
6444 break;
6445 case VT_ENUM:
6446 case VT_STRUCT:
6447 if (bt == VT_STRUCT)
6448 tstr = "struct ";
6449 else
6450 tstr = "enum ";
6451 pstrcat(buf, buf_size, tstr);
6452 v = type->ref->v & ~SYM_STRUCT;
6453 if (v >= SYM_FIRST_ANOM)
6454 pstrcat(buf, buf_size, "<anonymous>");
6455 else
6456 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6457 break;
6458 case VT_FUNC:
6459 s = type->ref;
6460 type_to_str(buf, buf_size, &s->type, varstr);
6461 pstrcat(buf, buf_size, "(");
6462 sa = s->next;
6463 while (sa != NULL) {
6464 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6465 pstrcat(buf, buf_size, buf1);
6466 sa = sa->next;
6467 if (sa)
6468 pstrcat(buf, buf_size, ", ");
6470 pstrcat(buf, buf_size, ")");
6471 goto no_var;
6472 case VT_PTR:
6473 s = type->ref;
6474 pstrcpy(buf1, sizeof(buf1), "*");
6475 if (varstr)
6476 pstrcat(buf1, sizeof(buf1), varstr);
6477 type_to_str(buf, buf_size, &s->type, buf1);
6478 goto no_var;
6480 if (varstr) {
6481 pstrcat(buf, buf_size, " ");
6482 pstrcat(buf, buf_size, varstr);
6484 no_var: ;
6487 /* verify type compatibility to store vtop in 'dt' type, and generate
6488 casts if needed. */
6489 static void gen_assign_cast(CType *dt)
6491 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6492 char buf1[256], buf2[256];
6493 int dbt, sbt;
6495 st = &vtop->type; /* source type */
6496 dbt = dt->t & VT_BTYPE;
6497 sbt = st->t & VT_BTYPE;
6498 if (dt->t & VT_CONSTANT)
6499 warning("assignment of read-only location");
6500 switch(dbt) {
6501 case VT_PTR:
6502 /* special cases for pointers */
6503 /* '0' can also be a pointer */
6504 if (is_null_pointer(vtop))
6505 goto type_ok;
6506 /* accept implicit pointer to integer cast with warning */
6507 if (is_integer_btype(sbt)) {
6508 warning("assignment makes pointer from integer without a cast");
6509 goto type_ok;
6511 type1 = pointed_type(dt);
6512 /* a function is implicitely a function pointer */
6513 if (sbt == VT_FUNC) {
6514 if ((type1->t & VT_BTYPE) != VT_VOID &&
6515 !is_compatible_types(pointed_type(dt), st))
6516 goto error;
6517 else
6518 goto type_ok;
6520 if (sbt != VT_PTR)
6521 goto error;
6522 type2 = pointed_type(st);
6523 if ((type1->t & VT_BTYPE) == VT_VOID ||
6524 (type2->t & VT_BTYPE) == VT_VOID) {
6525 /* void * can match anything */
6526 } else {
6527 /* exact type match, except for unsigned */
6528 tmp_type1 = *type1;
6529 tmp_type2 = *type2;
6530 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6531 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6532 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6533 warning("assignment from incompatible pointer type");
6535 /* check const and volatile */
6536 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6537 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6538 warning("assignment discards qualifiers from pointer target type");
6539 break;
6540 case VT_BYTE:
6541 case VT_SHORT:
6542 case VT_INT:
6543 case VT_LLONG:
6544 if (sbt == VT_PTR || sbt == VT_FUNC) {
6545 warning("assignment makes integer from pointer without a cast");
6547 /* XXX: more tests */
6548 break;
6549 case VT_STRUCT:
6550 tmp_type1 = *dt;
6551 tmp_type2 = *st;
6552 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6553 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6554 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6555 error:
6556 type_to_str(buf1, sizeof(buf1), st, NULL);
6557 type_to_str(buf2, sizeof(buf2), dt, NULL);
6558 error("cannot cast '%s' to '%s'", buf1, buf2);
6560 break;
6562 type_ok:
6563 gen_cast(dt);
6566 /* store vtop in lvalue pushed on stack */
6567 void vstore(void)
6569 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6571 ft = vtop[-1].type.t;
6572 sbt = vtop->type.t & VT_BTYPE;
6573 dbt = ft & VT_BTYPE;
6574 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6575 (sbt == VT_INT && dbt == VT_SHORT)) {
6576 /* optimize char/short casts */
6577 delayed_cast = VT_MUSTCAST;
6578 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
6579 /* XXX: factorize */
6580 if (ft & VT_CONSTANT)
6581 warning("assignment of read-only location");
6582 } else {
6583 delayed_cast = 0;
6584 if (!(ft & VT_BITFIELD))
6585 gen_assign_cast(&vtop[-1].type);
6588 if (sbt == VT_STRUCT) {
6589 /* if structure, only generate pointer */
6590 /* structure assignment : generate memcpy */
6591 /* XXX: optimize if small size */
6592 if (!nocode_wanted) {
6593 size = type_size(&vtop->type, &align);
6595 #ifdef TCC_ARM_EABI
6596 if(!(align & 7))
6597 vpush_global_sym(&func_old_type, TOK_memcpy8);
6598 else if(!(align & 3))
6599 vpush_global_sym(&func_old_type, TOK_memcpy4);
6600 else
6601 #endif
6602 vpush_global_sym(&func_old_type, TOK_memcpy);
6604 /* destination */
6605 vpushv(vtop - 2);
6606 vtop->type.t = VT_INT;
6607 gaddrof();
6608 /* source */
6609 vpushv(vtop - 2);
6610 vtop->type.t = VT_INT;
6611 gaddrof();
6612 /* type size */
6613 vpushi(size);
6614 gfunc_call(3);
6616 vswap();
6617 vpop();
6618 } else {
6619 vswap();
6620 vpop();
6622 /* leave source on stack */
6623 } else if (ft & VT_BITFIELD) {
6624 /* bitfield store handling */
6625 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6626 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6627 /* remove bit field info to avoid loops */
6628 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6630 /* duplicate source into other register */
6631 gv_dup();
6632 vswap();
6633 vrott(3);
6635 if((ft & VT_BTYPE) == VT_BOOL) {
6636 gen_cast(&vtop[-1].type);
6637 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
6640 /* duplicate destination */
6641 vdup();
6642 vtop[-1] = vtop[-2];
6644 /* mask and shift source */
6645 if((ft & VT_BTYPE) != VT_BOOL) {
6646 if((ft & VT_BTYPE) == VT_LLONG) {
6647 vpushll((1ULL << bit_size) - 1ULL);
6648 } else {
6649 vpushi((1 << bit_size) - 1);
6651 gen_op('&');
6653 vpushi(bit_pos);
6654 gen_op(TOK_SHL);
6655 /* load destination, mask and or with source */
6656 vswap();
6657 if((ft & VT_BTYPE) == VT_LLONG) {
6658 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
6659 } else {
6660 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6662 gen_op('&');
6663 gen_op('|');
6664 /* store result */
6665 vstore();
6667 /* pop off shifted source from "duplicate source..." above */
6668 vpop();
6670 } else {
6671 #ifdef CONFIG_TCC_BCHECK
6672 /* bound check case */
6673 if (vtop[-1].r & VT_MUSTBOUND) {
6674 vswap();
6675 gbound();
6676 vswap();
6678 #endif
6679 if (!nocode_wanted) {
6680 rc = RC_INT;
6681 if (is_float(ft)) {
6682 rc = RC_FLOAT;
6683 #ifdef TCC_TARGET_X86_64
6684 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
6685 rc = RC_ST0;
6687 #endif
6689 r = gv(rc); /* generate value */
6690 /* if lvalue was saved on stack, must read it */
6691 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6692 SValue sv;
6693 t = get_reg(RC_INT);
6694 #ifdef TCC_TARGET_X86_64
6695 sv.type.t = VT_PTR;
6696 #else
6697 sv.type.t = VT_INT;
6698 #endif
6699 sv.r = VT_LOCAL | VT_LVAL;
6700 sv.c.ul = vtop[-1].c.ul;
6701 load(t, &sv);
6702 vtop[-1].r = t | VT_LVAL;
6704 store(r, vtop - 1);
6705 #ifndef TCC_TARGET_X86_64
6706 /* two word case handling : store second register at word + 4 */
6707 if ((ft & VT_BTYPE) == VT_LLONG) {
6708 vswap();
6709 /* convert to int to increment easily */
6710 vtop->type.t = VT_INT;
6711 gaddrof();
6712 vpushi(4);
6713 gen_op('+');
6714 vtop->r |= VT_LVAL;
6715 vswap();
6716 /* XXX: it works because r2 is spilled last ! */
6717 store(vtop->r2, vtop - 1);
6719 #endif
6721 vswap();
6722 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6723 vtop->r |= delayed_cast;
6727 /* post defines POST/PRE add. c is the token ++ or -- */
6728 void inc(int post, int c)
6730 test_lvalue();
6731 vdup(); /* save lvalue */
6732 if (post) {
6733 gv_dup(); /* duplicate value */
6734 vrotb(3);
6735 vrotb(3);
6737 /* add constant */
6738 vpushi(c - TOK_MID);
6739 gen_op('+');
6740 vstore(); /* store value */
6741 if (post)
6742 vpop(); /* if post op, return saved value */
6745 /* Parse GNUC __attribute__ extension. Currently, the following
6746 extensions are recognized:
6747 - aligned(n) : set data/function alignment.
6748 - packed : force data alignment to 1
6749 - section(x) : generate data/code in this section.
6750 - unused : currently ignored, but may be used someday.
6751 - regparm(n) : pass function parameters in registers (i386 only)
6753 static void parse_attribute(AttributeDef *ad)
6755 int t, n;
6757 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6758 next();
6759 skip('(');
6760 skip('(');
6761 while (tok != ')') {
6762 if (tok < TOK_IDENT)
6763 expect("attribute name");
6764 t = tok;
6765 next();
6766 switch(t) {
6767 case TOK_SECTION1:
6768 case TOK_SECTION2:
6769 skip('(');
6770 if (tok != TOK_STR)
6771 expect("section name");
6772 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6773 next();
6774 skip(')');
6775 break;
6776 case TOK_ALIGNED1:
6777 case TOK_ALIGNED2:
6778 if (tok == '(') {
6779 next();
6780 n = expr_const();
6781 if (n <= 0 || (n & (n - 1)) != 0)
6782 error("alignment must be a positive power of two");
6783 skip(')');
6784 } else {
6785 n = MAX_ALIGN;
6787 ad->aligned = n;
6788 break;
6789 case TOK_PACKED1:
6790 case TOK_PACKED2:
6791 ad->packed = 1;
6792 break;
6793 case TOK_UNUSED1:
6794 case TOK_UNUSED2:
6795 /* currently, no need to handle it because tcc does not
6796 track unused objects */
6797 break;
6798 case TOK_NORETURN1:
6799 case TOK_NORETURN2:
6800 /* currently, no need to handle it because tcc does not
6801 track unused objects */
6802 break;
6803 case TOK_CDECL1:
6804 case TOK_CDECL2:
6805 case TOK_CDECL3:
6806 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6807 break;
6808 case TOK_STDCALL1:
6809 case TOK_STDCALL2:
6810 case TOK_STDCALL3:
6811 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6812 break;
6813 #ifdef TCC_TARGET_I386
6814 case TOK_REGPARM1:
6815 case TOK_REGPARM2:
6816 skip('(');
6817 n = expr_const();
6818 if (n > 3)
6819 n = 3;
6820 else if (n < 0)
6821 n = 0;
6822 if (n > 0)
6823 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6824 skip(')');
6825 break;
6826 case TOK_FASTCALL1:
6827 case TOK_FASTCALL2:
6828 case TOK_FASTCALL3:
6829 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6830 break;
6831 #endif
6832 case TOK_DLLEXPORT:
6833 FUNC_EXPORT(ad->func_attr) = 1;
6834 break;
6835 default:
6836 if (tcc_state->warn_unsupported)
6837 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6838 /* skip parameters */
6839 if (tok == '(') {
6840 int parenthesis = 0;
6841 do {
6842 if (tok == '(')
6843 parenthesis++;
6844 else if (tok == ')')
6845 parenthesis--;
6846 next();
6847 } while (parenthesis && tok != -1);
6849 break;
6851 if (tok != ',')
6852 break;
6853 next();
6855 skip(')');
6856 skip(')');
6860 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6861 static void struct_decl(CType *type, int u)
6863 int a, v, size, align, maxalign, c, offset;
6864 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
6865 Sym *s, *ss, *ass, **ps;
6866 AttributeDef ad;
6867 CType type1, btype;
6869 a = tok; /* save decl type */
6870 next();
6871 if (tok != '{') {
6872 v = tok;
6873 next();
6874 /* struct already defined ? return it */
6875 if (v < TOK_IDENT)
6876 expect("struct/union/enum name");
6877 s = struct_find(v);
6878 if (s) {
6879 if (s->type.t != a)
6880 error("invalid type");
6881 goto do_decl;
6883 } else {
6884 v = anon_sym++;
6886 type1.t = a;
6887 /* we put an undefined size for struct/union */
6888 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6889 s->r = 0; /* default alignment is zero as gcc */
6890 /* put struct/union/enum name in type */
6891 do_decl:
6892 type->t = u;
6893 type->ref = s;
6895 if (tok == '{') {
6896 next();
6897 if (s->c != -1)
6898 error("struct/union/enum already defined");
6899 /* cannot be empty */
6900 c = 0;
6901 /* non empty enums are not allowed */
6902 if (a == TOK_ENUM) {
6903 for(;;) {
6904 v = tok;
6905 if (v < TOK_UIDENT)
6906 expect("identifier");
6907 next();
6908 if (tok == '=') {
6909 next();
6910 c = expr_const();
6912 /* enum symbols have static storage */
6913 ss = sym_push(v, &int_type, VT_CONST, c);
6914 ss->type.t |= VT_STATIC;
6915 if (tok != ',')
6916 break;
6917 next();
6918 c++;
6919 /* NOTE: we accept a trailing comma */
6920 if (tok == '}')
6921 break;
6923 skip('}');
6924 } else {
6925 maxalign = 1;
6926 ps = &s->next;
6927 prevbt = VT_INT;
6928 bit_pos = 0;
6929 offset = 0;
6930 while (tok != '}') {
6931 parse_btype(&btype, &ad);
6932 while (1) {
6933 bit_size = -1;
6934 v = 0;
6935 type1 = btype;
6936 if (tok != ':') {
6937 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6938 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6939 expect("identifier");
6940 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6941 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6942 error("invalid type for '%s'",
6943 get_tok_str(v, NULL));
6945 if (tok == ':') {
6946 next();
6947 bit_size = expr_const();
6948 /* XXX: handle v = 0 case for messages */
6949 if (bit_size < 0)
6950 error("negative width in bit-field '%s'",
6951 get_tok_str(v, NULL));
6952 if (v && bit_size == 0)
6953 error("zero width for bit-field '%s'",
6954 get_tok_str(v, NULL));
6956 size = type_size(&type1, &align);
6957 if (ad.aligned) {
6958 if (align < ad.aligned)
6959 align = ad.aligned;
6960 } else if (ad.packed) {
6961 align = 1;
6962 } else if (*tcc_state->pack_stack_ptr) {
6963 if (align > *tcc_state->pack_stack_ptr)
6964 align = *tcc_state->pack_stack_ptr;
6966 lbit_pos = 0;
6967 if (bit_size >= 0) {
6968 bt = type1.t & VT_BTYPE;
6969 if (bt != VT_INT &&
6970 bt != VT_BYTE &&
6971 bt != VT_SHORT &&
6972 bt != VT_BOOL &&
6973 bt != VT_ENUM &&
6974 bt != VT_LLONG)
6975 error("bitfields must have scalar type");
6976 bsize = size * 8;
6977 if (bit_size > bsize) {
6978 error("width of '%s' exceeds its type",
6979 get_tok_str(v, NULL));
6980 } else if (bit_size == bsize) {
6981 /* no need for bit fields */
6982 bit_pos = 0;
6983 } else if (bit_size == 0) {
6984 /* XXX: what to do if only padding in a
6985 structure ? */
6986 /* zero size: means to pad */
6987 bit_pos = 0;
6988 } else {
6989 /* we do not have enough room ?
6990 did the type change?
6991 is it a union? */
6992 if ((bit_pos + bit_size) > bsize ||
6993 bt != prevbt || a == TOK_UNION)
6994 bit_pos = 0;
6995 lbit_pos = bit_pos;
6996 /* XXX: handle LSB first */
6997 type1.t |= VT_BITFIELD |
6998 (bit_pos << VT_STRUCT_SHIFT) |
6999 (bit_size << (VT_STRUCT_SHIFT + 6));
7000 bit_pos += bit_size;
7002 prevbt = bt;
7003 } else {
7004 bit_pos = 0;
7006 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
7007 /* add new memory data only if starting
7008 bit field */
7009 if (lbit_pos == 0) {
7010 if (a == TOK_STRUCT) {
7011 c = (c + align - 1) & -align;
7012 offset = c;
7013 if (size > 0)
7014 c += size;
7015 } else {
7016 offset = 0;
7017 if (size > c)
7018 c = size;
7020 if (align > maxalign)
7021 maxalign = align;
7023 #if 0
7024 printf("add field %s offset=%d",
7025 get_tok_str(v, NULL), offset);
7026 if (type1.t & VT_BITFIELD) {
7027 printf(" pos=%d size=%d",
7028 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
7029 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
7031 printf("\n");
7032 #endif
7034 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
7035 ass = type1.ref;
7036 while ((ass = ass->next) != NULL) {
7037 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
7038 *ps = ss;
7039 ps = &ss->next;
7041 } else if (v) {
7042 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
7043 *ps = ss;
7044 ps = &ss->next;
7046 if (tok == ';' || tok == TOK_EOF)
7047 break;
7048 skip(',');
7050 skip(';');
7052 skip('}');
7053 /* store size and alignment */
7054 s->c = (c + maxalign - 1) & -maxalign;
7055 s->r = maxalign;
7060 /* return 0 if no type declaration. otherwise, return the basic type
7061 and skip it.
7063 static int parse_btype(CType *type, AttributeDef *ad)
7065 int t, u, type_found, typespec_found, typedef_found;
7066 Sym *s;
7067 CType type1;
7069 memset(ad, 0, sizeof(AttributeDef));
7070 type_found = 0;
7071 typespec_found = 0;
7072 typedef_found = 0;
7073 t = 0;
7074 while(1) {
7075 switch(tok) {
7076 case TOK_EXTENSION:
7077 /* currently, we really ignore extension */
7078 next();
7079 continue;
7081 /* basic types */
7082 case TOK_CHAR:
7083 u = VT_BYTE;
7084 basic_type:
7085 next();
7086 basic_type1:
7087 if ((t & VT_BTYPE) != 0)
7088 error("too many basic types");
7089 t |= u;
7090 typespec_found = 1;
7091 break;
7092 case TOK_VOID:
7093 u = VT_VOID;
7094 goto basic_type;
7095 case TOK_SHORT:
7096 u = VT_SHORT;
7097 goto basic_type;
7098 case TOK_INT:
7099 next();
7100 typespec_found = 1;
7101 break;
7102 case TOK_LONG:
7103 next();
7104 if ((t & VT_BTYPE) == VT_DOUBLE) {
7105 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
7106 } else if ((t & VT_BTYPE) == VT_LONG) {
7107 t = (t & ~VT_BTYPE) | VT_LLONG;
7108 } else {
7109 u = VT_LONG;
7110 goto basic_type1;
7112 break;
7113 case TOK_BOOL:
7114 u = VT_BOOL;
7115 goto basic_type;
7116 case TOK_FLOAT:
7117 u = VT_FLOAT;
7118 goto basic_type;
7119 case TOK_DOUBLE:
7120 next();
7121 if ((t & VT_BTYPE) == VT_LONG) {
7122 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
7123 } else {
7124 u = VT_DOUBLE;
7125 goto basic_type1;
7127 break;
7128 case TOK_ENUM:
7129 struct_decl(&type1, VT_ENUM);
7130 basic_type2:
7131 u = type1.t;
7132 type->ref = type1.ref;
7133 goto basic_type1;
7134 case TOK_STRUCT:
7135 case TOK_UNION:
7136 struct_decl(&type1, VT_STRUCT);
7137 goto basic_type2;
7139 /* type modifiers */
7140 case TOK_CONST1:
7141 case TOK_CONST2:
7142 case TOK_CONST3:
7143 t |= VT_CONSTANT;
7144 next();
7145 break;
7146 case TOK_VOLATILE1:
7147 case TOK_VOLATILE2:
7148 case TOK_VOLATILE3:
7149 t |= VT_VOLATILE;
7150 next();
7151 break;
7152 case TOK_SIGNED1:
7153 case TOK_SIGNED2:
7154 case TOK_SIGNED3:
7155 typespec_found = 1;
7156 t |= VT_SIGNED;
7157 next();
7158 break;
7159 case TOK_REGISTER:
7160 case TOK_AUTO:
7161 case TOK_RESTRICT1:
7162 case TOK_RESTRICT2:
7163 case TOK_RESTRICT3:
7164 next();
7165 break;
7166 case TOK_UNSIGNED:
7167 t |= VT_UNSIGNED;
7168 next();
7169 typespec_found = 1;
7170 break;
7172 /* storage */
7173 case TOK_EXTERN:
7174 t |= VT_EXTERN;
7175 next();
7176 break;
7177 case TOK_STATIC:
7178 t |= VT_STATIC;
7179 next();
7180 break;
7181 case TOK_TYPEDEF:
7182 t |= VT_TYPEDEF;
7183 next();
7184 break;
7185 case TOK_INLINE1:
7186 case TOK_INLINE2:
7187 case TOK_INLINE3:
7188 t |= VT_INLINE;
7189 next();
7190 break;
7192 /* GNUC attribute */
7193 case TOK_ATTRIBUTE1:
7194 case TOK_ATTRIBUTE2:
7195 parse_attribute(ad);
7196 break;
7197 /* GNUC typeof */
7198 case TOK_TYPEOF1:
7199 case TOK_TYPEOF2:
7200 case TOK_TYPEOF3:
7201 next();
7202 parse_expr_type(&type1);
7203 goto basic_type2;
7204 default:
7205 if (typespec_found || typedef_found)
7206 goto the_end;
7207 s = sym_find(tok);
7208 if (!s || !(s->type.t & VT_TYPEDEF))
7209 goto the_end;
7210 typedef_found = 1;
7211 t |= (s->type.t & ~VT_TYPEDEF);
7212 type->ref = s->type.ref;
7213 next();
7214 typespec_found = 1;
7215 break;
7217 type_found = 1;
7219 the_end:
7220 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7221 error("signed and unsigned modifier");
7222 if (tcc_state->char_is_unsigned) {
7223 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7224 t |= VT_UNSIGNED;
7226 t &= ~VT_SIGNED;
7228 /* long is never used as type */
7229 if ((t & VT_BTYPE) == VT_LONG)
7230 #ifndef TCC_TARGET_X86_64
7231 t = (t & ~VT_BTYPE) | VT_INT;
7232 #else
7233 t = (t & ~VT_BTYPE) | VT_LLONG;
7234 #endif
7235 type->t = t;
7236 return type_found;
7239 /* convert a function parameter type (array to pointer and function to
7240 function pointer) */
7241 static inline void convert_parameter_type(CType *pt)
7243 /* remove const and volatile qualifiers (XXX: const could be used
7244 to indicate a const function parameter */
7245 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7246 /* array must be transformed to pointer according to ANSI C */
7247 pt->t &= ~VT_ARRAY;
7248 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7249 mk_pointer(pt);
7253 static void post_type(CType *type, AttributeDef *ad)
7255 int n, l, t1, arg_size, align;
7256 Sym **plast, *s, *first;
7257 AttributeDef ad1;
7258 CType pt;
7260 if (tok == '(') {
7261 /* function declaration */
7262 next();
7263 l = 0;
7264 first = NULL;
7265 plast = &first;
7266 arg_size = 0;
7267 if (tok != ')') {
7268 for(;;) {
7269 /* read param name and compute offset */
7270 if (l != FUNC_OLD) {
7271 if (!parse_btype(&pt, &ad1)) {
7272 if (l) {
7273 error("invalid type");
7274 } else {
7275 l = FUNC_OLD;
7276 goto old_proto;
7279 l = FUNC_NEW;
7280 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7281 break;
7282 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7283 if ((pt.t & VT_BTYPE) == VT_VOID)
7284 error("parameter declared as void");
7285 arg_size += (type_size(&pt, &align) + 3) & ~3;
7286 } else {
7287 old_proto:
7288 n = tok;
7289 if (n < TOK_UIDENT)
7290 expect("identifier");
7291 pt.t = VT_INT;
7292 next();
7294 convert_parameter_type(&pt);
7295 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7296 *plast = s;
7297 plast = &s->next;
7298 if (tok == ')')
7299 break;
7300 skip(',');
7301 if (l == FUNC_NEW && tok == TOK_DOTS) {
7302 l = FUNC_ELLIPSIS;
7303 next();
7304 break;
7308 /* if no parameters, then old type prototype */
7309 if (l == 0)
7310 l = FUNC_OLD;
7311 skip(')');
7312 t1 = type->t & VT_STORAGE;
7313 /* NOTE: const is ignored in returned type as it has a special
7314 meaning in gcc / C++ */
7315 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7316 post_type(type, ad);
7317 /* we push a anonymous symbol which will contain the function prototype */
7318 FUNC_ARGS(ad->func_attr) = arg_size;
7319 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7320 s->next = first;
7321 type->t = t1 | VT_FUNC;
7322 type->ref = s;
7323 } else if (tok == '[') {
7324 /* array definition */
7325 next();
7326 if (tok == TOK_RESTRICT1)
7327 next();
7328 n = -1;
7329 if (tok != ']') {
7330 n = expr_const();
7331 if (n < 0)
7332 error("invalid array size");
7334 skip(']');
7335 /* parse next post type */
7336 t1 = type->t & VT_STORAGE;
7337 type->t &= ~VT_STORAGE;
7338 post_type(type, ad);
7340 /* we push a anonymous symbol which will contain the array
7341 element type */
7342 s = sym_push(SYM_FIELD, type, 0, n);
7343 type->t = t1 | VT_ARRAY | VT_PTR;
7344 type->ref = s;
7348 /* Parse a type declaration (except basic type), and return the type
7349 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7350 expected. 'type' should contain the basic type. 'ad' is the
7351 attribute definition of the basic type. It can be modified by
7352 type_decl().
7354 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7356 Sym *s;
7357 CType type1, *type2;
7358 int qualifiers;
7360 while (tok == '*') {
7361 qualifiers = 0;
7362 redo:
7363 next();
7364 switch(tok) {
7365 case TOK_CONST1:
7366 case TOK_CONST2:
7367 case TOK_CONST3:
7368 qualifiers |= VT_CONSTANT;
7369 goto redo;
7370 case TOK_VOLATILE1:
7371 case TOK_VOLATILE2:
7372 case TOK_VOLATILE3:
7373 qualifiers |= VT_VOLATILE;
7374 goto redo;
7375 case TOK_RESTRICT1:
7376 case TOK_RESTRICT2:
7377 case TOK_RESTRICT3:
7378 goto redo;
7380 mk_pointer(type);
7381 type->t |= qualifiers;
7384 /* XXX: clarify attribute handling */
7385 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7386 parse_attribute(ad);
7388 /* recursive type */
7389 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7390 type1.t = 0; /* XXX: same as int */
7391 if (tok == '(') {
7392 next();
7393 /* XXX: this is not correct to modify 'ad' at this point, but
7394 the syntax is not clear */
7395 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7396 parse_attribute(ad);
7397 type_decl(&type1, ad, v, td);
7398 skip(')');
7399 } else {
7400 /* type identifier */
7401 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7402 *v = tok;
7403 next();
7404 } else {
7405 if (!(td & TYPE_ABSTRACT))
7406 expect("identifier");
7407 *v = 0;
7410 post_type(type, ad);
7411 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7412 parse_attribute(ad);
7413 if (!type1.t)
7414 return;
7415 /* append type at the end of type1 */
7416 type2 = &type1;
7417 for(;;) {
7418 s = type2->ref;
7419 type2 = &s->type;
7420 if (!type2->t) {
7421 *type2 = *type;
7422 break;
7425 *type = type1;
7428 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7429 static int lvalue_type(int t)
7431 int bt, r;
7432 r = VT_LVAL;
7433 bt = t & VT_BTYPE;
7434 if (bt == VT_BYTE || bt == VT_BOOL)
7435 r |= VT_LVAL_BYTE;
7436 else if (bt == VT_SHORT)
7437 r |= VT_LVAL_SHORT;
7438 else
7439 return r;
7440 if (t & VT_UNSIGNED)
7441 r |= VT_LVAL_UNSIGNED;
7442 return r;
7445 /* indirection with full error checking and bound check */
7446 static void indir(void)
7448 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7449 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7450 return;
7451 expect("pointer");
7453 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7454 gv(RC_INT);
7455 vtop->type = *pointed_type(&vtop->type);
7456 /* Arrays and functions are never lvalues */
7457 if (!(vtop->type.t & VT_ARRAY)
7458 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7459 vtop->r |= lvalue_type(vtop->type.t);
7460 /* if bound checking, the referenced pointer must be checked */
7461 if (do_bounds_check)
7462 vtop->r |= VT_MUSTBOUND;
7466 /* pass a parameter to a function and do type checking and casting */
7467 static void gfunc_param_typed(Sym *func, Sym *arg)
7469 int func_type;
7470 CType type;
7472 func_type = func->c;
7473 if (func_type == FUNC_OLD ||
7474 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7475 /* default casting : only need to convert float to double */
7476 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7477 type.t = VT_DOUBLE;
7478 gen_cast(&type);
7480 } else if (arg == NULL) {
7481 error("too many arguments to function");
7482 } else {
7483 type = arg->type;
7484 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7485 gen_assign_cast(&type);
7489 /* parse an expression of the form '(type)' or '(expr)' and return its
7490 type */
7491 static void parse_expr_type(CType *type)
7493 int n;
7494 AttributeDef ad;
7496 skip('(');
7497 if (parse_btype(type, &ad)) {
7498 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7499 } else {
7500 expr_type(type);
7502 skip(')');
7505 static void parse_type(CType *type)
7507 AttributeDef ad;
7508 int n;
7510 if (!parse_btype(type, &ad)) {
7511 expect("type");
7513 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7516 static void vpush_tokc(int t)
7518 CType type;
7519 type.t = t;
7520 vsetc(&type, VT_CONST, &tokc);
7523 static void unary(void)
7525 int n, t, align, size, r;
7526 CType type;
7527 Sym *s;
7528 AttributeDef ad;
7530 /* XXX: GCC 2.95.3 does not generate a table although it should be
7531 better here */
7532 tok_next:
7533 switch(tok) {
7534 case TOK_EXTENSION:
7535 next();
7536 goto tok_next;
7537 case TOK_CINT:
7538 case TOK_CCHAR:
7539 case TOK_LCHAR:
7540 vpushi(tokc.i);
7541 next();
7542 break;
7543 case TOK_CUINT:
7544 vpush_tokc(VT_INT | VT_UNSIGNED);
7545 next();
7546 break;
7547 case TOK_CLLONG:
7548 vpush_tokc(VT_LLONG);
7549 next();
7550 break;
7551 case TOK_CULLONG:
7552 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7553 next();
7554 break;
7555 case TOK_CFLOAT:
7556 vpush_tokc(VT_FLOAT);
7557 next();
7558 break;
7559 case TOK_CDOUBLE:
7560 vpush_tokc(VT_DOUBLE);
7561 next();
7562 break;
7563 case TOK_CLDOUBLE:
7564 vpush_tokc(VT_LDOUBLE);
7565 next();
7566 break;
7567 case TOK___FUNCTION__:
7568 if (!gnu_ext)
7569 goto tok_identifier;
7570 /* fall thru */
7571 case TOK___FUNC__:
7573 void *ptr;
7574 int len;
7575 /* special function name identifier */
7576 len = strlen(funcname) + 1;
7577 /* generate char[len] type */
7578 type.t = VT_BYTE;
7579 mk_pointer(&type);
7580 type.t |= VT_ARRAY;
7581 type.ref->c = len;
7582 vpush_ref(&type, data_section, data_section->data_offset, len);
7583 ptr = section_ptr_add(data_section, len);
7584 memcpy(ptr, funcname, len);
7585 next();
7587 break;
7588 case TOK_LSTR:
7589 #ifdef TCC_TARGET_PE
7590 t = VT_SHORT | VT_UNSIGNED;
7591 #else
7592 t = VT_INT;
7593 #endif
7594 goto str_init;
7595 case TOK_STR:
7596 /* string parsing */
7597 t = VT_BYTE;
7598 str_init:
7599 if (tcc_state->warn_write_strings)
7600 t |= VT_CONSTANT;
7601 type.t = t;
7602 mk_pointer(&type);
7603 type.t |= VT_ARRAY;
7604 memset(&ad, 0, sizeof(AttributeDef));
7605 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7606 break;
7607 case '(':
7608 next();
7609 /* cast ? */
7610 if (parse_btype(&type, &ad)) {
7611 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7612 skip(')');
7613 /* check ISOC99 compound literal */
7614 if (tok == '{') {
7615 /* data is allocated locally by default */
7616 if (global_expr)
7617 r = VT_CONST;
7618 else
7619 r = VT_LOCAL;
7620 /* all except arrays are lvalues */
7621 if (!(type.t & VT_ARRAY))
7622 r |= lvalue_type(type.t);
7623 memset(&ad, 0, sizeof(AttributeDef));
7624 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7625 } else {
7626 unary();
7627 gen_cast(&type);
7629 } else if (tok == '{') {
7630 /* save all registers */
7631 save_regs(0);
7632 /* statement expression : we do not accept break/continue
7633 inside as GCC does */
7634 block(NULL, NULL, NULL, NULL, 0, 1);
7635 skip(')');
7636 } else {
7637 gexpr();
7638 skip(')');
7640 break;
7641 case '*':
7642 next();
7643 unary();
7644 indir();
7645 break;
7646 case '&':
7647 next();
7648 unary();
7649 /* functions names must be treated as function pointers,
7650 except for unary '&' and sizeof. Since we consider that
7651 functions are not lvalues, we only have to handle it
7652 there and in function calls. */
7653 /* arrays can also be used although they are not lvalues */
7654 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7655 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7656 test_lvalue();
7657 mk_pointer(&vtop->type);
7658 gaddrof();
7659 break;
7660 case '!':
7661 next();
7662 unary();
7663 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
7664 CType boolean;
7665 boolean.t = VT_BOOL;
7666 gen_cast(&boolean);
7667 vtop->c.i = !vtop->c.i;
7668 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
7669 vtop->c.i = vtop->c.i ^ 1;
7670 else {
7671 save_regs(1);
7672 vseti(VT_JMP, gtst(1, 0));
7674 break;
7675 case '~':
7676 next();
7677 unary();
7678 vpushi(-1);
7679 gen_op('^');
7680 break;
7681 case '+':
7682 next();
7683 /* in order to force cast, we add zero */
7684 unary();
7685 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7686 error("pointer not accepted for unary plus");
7687 vpushi(0);
7688 gen_op('+');
7689 break;
7690 case TOK_SIZEOF:
7691 case TOK_ALIGNOF1:
7692 case TOK_ALIGNOF2:
7693 t = tok;
7694 next();
7695 if (tok == '(') {
7696 parse_expr_type(&type);
7697 } else {
7698 unary_type(&type);
7700 size = type_size(&type, &align);
7701 if (t == TOK_SIZEOF) {
7702 if (size < 0)
7703 error("sizeof applied to an incomplete type");
7704 vpushi(size);
7705 } else {
7706 vpushi(align);
7708 vtop->type.t |= VT_UNSIGNED;
7709 break;
7711 case TOK_builtin_types_compatible_p:
7713 CType type1, type2;
7714 next();
7715 skip('(');
7716 parse_type(&type1);
7717 skip(',');
7718 parse_type(&type2);
7719 skip(')');
7720 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7721 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7722 vpushi(is_compatible_types(&type1, &type2));
7724 break;
7725 case TOK_builtin_constant_p:
7727 int saved_nocode_wanted, res;
7728 next();
7729 skip('(');
7730 saved_nocode_wanted = nocode_wanted;
7731 nocode_wanted = 1;
7732 gexpr();
7733 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7734 vpop();
7735 nocode_wanted = saved_nocode_wanted;
7736 skip(')');
7737 vpushi(res);
7739 break;
7740 case TOK_builtin_frame_address:
7742 CType type;
7743 next();
7744 skip('(');
7745 if (tok != TOK_CINT) {
7746 error("__builtin_frame_address only takes integers");
7748 if (tokc.i != 0) {
7749 error("TCC only supports __builtin_frame_address(0)");
7751 next();
7752 skip(')');
7753 type.t = VT_VOID;
7754 mk_pointer(&type);
7755 vset(&type, VT_LOCAL, 0);
7757 break;
7758 #ifdef TCC_TARGET_X86_64
7759 case TOK_builtin_malloc:
7761 char *p = file->buf_ptr;
7762 file->buf_ptr = "malloc";
7763 next_nomacro1();
7764 file->buf_ptr = p;
7765 goto tok_identifier;
7767 case TOK_builtin_free:
7769 char *p = file->buf_ptr;
7770 file->buf_ptr = "free";
7771 next_nomacro1();
7772 file->buf_ptr = p;
7773 goto tok_identifier;
7775 #endif
7776 case TOK_INC:
7777 case TOK_DEC:
7778 t = tok;
7779 next();
7780 unary();
7781 inc(0, t);
7782 break;
7783 case '-':
7784 next();
7785 vpushi(0);
7786 unary();
7787 gen_op('-');
7788 break;
7789 case TOK_LAND:
7790 if (!gnu_ext)
7791 goto tok_identifier;
7792 next();
7793 /* allow to take the address of a label */
7794 if (tok < TOK_UIDENT)
7795 expect("label identifier");
7796 s = label_find(tok);
7797 if (!s) {
7798 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7799 } else {
7800 if (s->r == LABEL_DECLARED)
7801 s->r = LABEL_FORWARD;
7803 if (!s->type.t) {
7804 s->type.t = VT_VOID;
7805 mk_pointer(&s->type);
7806 s->type.t |= VT_STATIC;
7808 vset(&s->type, VT_CONST | VT_SYM, 0);
7809 vtop->sym = s;
7810 next();
7811 break;
7812 default:
7813 tok_identifier:
7814 t = tok;
7815 next();
7816 if (t < TOK_UIDENT)
7817 expect("identifier");
7818 s = sym_find(t);
7819 if (!s) {
7820 if (tok != '(')
7821 error("'%s' undeclared", get_tok_str(t, NULL));
7822 /* for simple function calls, we tolerate undeclared
7823 external reference to int() function */
7824 if (tcc_state->warn_implicit_function_declaration)
7825 warning("implicit declaration of function '%s'",
7826 get_tok_str(t, NULL));
7827 s = external_global_sym(t, &func_old_type, 0);
7829 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7830 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7831 /* if referencing an inline function, then we generate a
7832 symbol to it if not already done. It will have the
7833 effect to generate code for it at the end of the
7834 compilation unit. Inline function as always
7835 generated in the text section. */
7836 if (!s->c)
7837 put_extern_sym(s, text_section, 0, 0);
7838 r = VT_SYM | VT_CONST;
7839 } else {
7840 r = s->r;
7842 vset(&s->type, r, s->c);
7843 /* if forward reference, we must point to s */
7844 if (vtop->r & VT_SYM) {
7845 vtop->sym = s;
7846 vtop->c.ul = 0;
7848 break;
7851 /* post operations */
7852 while (1) {
7853 if (tok == TOK_INC || tok == TOK_DEC) {
7854 inc(1, tok);
7855 next();
7856 } else if (tok == '.' || tok == TOK_ARROW) {
7857 /* field */
7858 if (tok == TOK_ARROW)
7859 indir();
7860 test_lvalue();
7861 gaddrof();
7862 next();
7863 /* expect pointer on structure */
7864 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7865 expect("struct or union");
7866 s = vtop->type.ref;
7867 /* find field */
7868 tok |= SYM_FIELD;
7869 while ((s = s->next) != NULL) {
7870 if (s->v == tok)
7871 break;
7873 if (!s)
7874 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7875 /* add field offset to pointer */
7876 vtop->type = char_pointer_type; /* change type to 'char *' */
7877 vpushi(s->c);
7878 gen_op('+');
7879 /* change type to field type, and set to lvalue */
7880 vtop->type = s->type;
7881 /* an array is never an lvalue */
7882 if (!(vtop->type.t & VT_ARRAY)) {
7883 vtop->r |= lvalue_type(vtop->type.t);
7884 /* if bound checking, the referenced pointer must be checked */
7885 if (do_bounds_check)
7886 vtop->r |= VT_MUSTBOUND;
7888 next();
7889 } else if (tok == '[') {
7890 next();
7891 gexpr();
7892 gen_op('+');
7893 indir();
7894 skip(']');
7895 } else if (tok == '(') {
7896 SValue ret;
7897 Sym *sa;
7898 int nb_args;
7900 /* function call */
7901 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7902 /* pointer test (no array accepted) */
7903 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7904 vtop->type = *pointed_type(&vtop->type);
7905 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7906 goto error_func;
7907 } else {
7908 error_func:
7909 expect("function pointer");
7911 } else {
7912 vtop->r &= ~VT_LVAL; /* no lvalue */
7914 /* get return type */
7915 s = vtop->type.ref;
7916 next();
7917 sa = s->next; /* first parameter */
7918 nb_args = 0;
7919 ret.r2 = VT_CONST;
7920 /* compute first implicit argument if a structure is returned */
7921 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7922 /* get some space for the returned structure */
7923 size = type_size(&s->type, &align);
7924 loc = (loc - size) & -align;
7925 ret.type = s->type;
7926 ret.r = VT_LOCAL | VT_LVAL;
7927 /* pass it as 'int' to avoid structure arg passing
7928 problems */
7929 vseti(VT_LOCAL, loc);
7930 ret.c = vtop->c;
7931 nb_args++;
7932 } else {
7933 ret.type = s->type;
7934 /* return in register */
7935 if (is_float(ret.type.t)) {
7936 ret.r = REG_FRET;
7937 } else {
7938 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7939 ret.r2 = REG_LRET;
7940 ret.r = REG_IRET;
7942 ret.c.i = 0;
7944 if (tok != ')') {
7945 for(;;) {
7946 expr_eq();
7947 gfunc_param_typed(s, sa);
7948 nb_args++;
7949 if (sa)
7950 sa = sa->next;
7951 if (tok == ')')
7952 break;
7953 skip(',');
7956 if (sa)
7957 error("too few arguments to function");
7958 skip(')');
7959 if (!nocode_wanted) {
7960 gfunc_call(nb_args);
7961 } else {
7962 vtop -= (nb_args + 1);
7964 /* return value */
7965 vsetc(&ret.type, ret.r, &ret.c);
7966 vtop->r2 = ret.r2;
7967 } else {
7968 break;
7973 static void uneq(void)
7975 int t;
7977 unary();
7978 if (tok == '=' ||
7979 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7980 tok == TOK_A_XOR || tok == TOK_A_OR ||
7981 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7982 test_lvalue();
7983 t = tok;
7984 next();
7985 if (t == '=') {
7986 expr_eq();
7987 } else {
7988 vdup();
7989 expr_eq();
7990 gen_op(t & 0x7f);
7992 vstore();
7996 static void expr_prod(void)
7998 int t;
8000 uneq();
8001 while (tok == '*' || tok == '/' || tok == '%') {
8002 t = tok;
8003 next();
8004 uneq();
8005 gen_op(t);
8009 static void expr_sum(void)
8011 int t;
8013 expr_prod();
8014 while (tok == '+' || tok == '-') {
8015 t = tok;
8016 next();
8017 expr_prod();
8018 gen_op(t);
8022 static void expr_shift(void)
8024 int t;
8026 expr_sum();
8027 while (tok == TOK_SHL || tok == TOK_SAR) {
8028 t = tok;
8029 next();
8030 expr_sum();
8031 gen_op(t);
8035 static void expr_cmp(void)
8037 int t;
8039 expr_shift();
8040 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
8041 tok == TOK_ULT || tok == TOK_UGE) {
8042 t = tok;
8043 next();
8044 expr_shift();
8045 gen_op(t);
8049 static void expr_cmpeq(void)
8051 int t;
8053 expr_cmp();
8054 while (tok == TOK_EQ || tok == TOK_NE) {
8055 t = tok;
8056 next();
8057 expr_cmp();
8058 gen_op(t);
8062 static void expr_and(void)
8064 expr_cmpeq();
8065 while (tok == '&') {
8066 next();
8067 expr_cmpeq();
8068 gen_op('&');
8072 static void expr_xor(void)
8074 expr_and();
8075 while (tok == '^') {
8076 next();
8077 expr_and();
8078 gen_op('^');
8082 static void expr_or(void)
8084 expr_xor();
8085 while (tok == '|') {
8086 next();
8087 expr_xor();
8088 gen_op('|');
8092 /* XXX: fix this mess */
8093 static void expr_land_const(void)
8095 expr_or();
8096 while (tok == TOK_LAND) {
8097 next();
8098 expr_or();
8099 gen_op(TOK_LAND);
8103 /* XXX: fix this mess */
8104 static void expr_lor_const(void)
8106 expr_land_const();
8107 while (tok == TOK_LOR) {
8108 next();
8109 expr_land_const();
8110 gen_op(TOK_LOR);
8114 /* only used if non constant */
8115 static void expr_land(void)
8117 int t;
8119 expr_or();
8120 if (tok == TOK_LAND) {
8121 t = 0;
8122 save_regs(1);
8123 for(;;) {
8124 t = gtst(1, t);
8125 if (tok != TOK_LAND) {
8126 vseti(VT_JMPI, t);
8127 break;
8129 next();
8130 expr_or();
8135 static void expr_lor(void)
8137 int t;
8139 expr_land();
8140 if (tok == TOK_LOR) {
8141 t = 0;
8142 save_regs(1);
8143 for(;;) {
8144 t = gtst(0, t);
8145 if (tok != TOK_LOR) {
8146 vseti(VT_JMP, t);
8147 break;
8149 next();
8150 expr_land();
8155 /* XXX: better constant handling */
8156 static void expr_eq(void)
8158 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
8159 SValue sv;
8160 CType type, type1, type2;
8162 if (const_wanted) {
8163 expr_lor_const();
8164 if (tok == '?') {
8165 CType boolean;
8166 int c;
8167 boolean.t = VT_BOOL;
8168 vdup();
8169 gen_cast(&boolean);
8170 c = vtop->c.i;
8171 vpop();
8172 next();
8173 if (tok != ':' || !gnu_ext) {
8174 vpop();
8175 gexpr();
8177 if (!c)
8178 vpop();
8179 skip(':');
8180 expr_eq();
8181 if (c)
8182 vpop();
8184 } else {
8185 expr_lor();
8186 if (tok == '?') {
8187 next();
8188 if (vtop != vstack) {
8189 /* needed to avoid having different registers saved in
8190 each branch */
8191 if (is_float(vtop->type.t)) {
8192 rc = RC_FLOAT;
8193 #ifdef TCC_TARGET_X86_64
8194 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
8195 rc = RC_ST0;
8197 #endif
8199 else
8200 rc = RC_INT;
8201 gv(rc);
8202 save_regs(1);
8204 if (tok == ':' && gnu_ext) {
8205 gv_dup();
8206 tt = gtst(1, 0);
8207 } else {
8208 tt = gtst(1, 0);
8209 gexpr();
8211 type1 = vtop->type;
8212 sv = *vtop; /* save value to handle it later */
8213 vtop--; /* no vpop so that FP stack is not flushed */
8214 skip(':');
8215 u = gjmp(0);
8216 gsym(tt);
8217 expr_eq();
8218 type2 = vtop->type;
8220 t1 = type1.t;
8221 bt1 = t1 & VT_BTYPE;
8222 t2 = type2.t;
8223 bt2 = t2 & VT_BTYPE;
8224 /* cast operands to correct type according to ISOC rules */
8225 if (is_float(bt1) || is_float(bt2)) {
8226 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8227 type.t = VT_LDOUBLE;
8228 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8229 type.t = VT_DOUBLE;
8230 } else {
8231 type.t = VT_FLOAT;
8233 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8234 /* cast to biggest op */
8235 type.t = VT_LLONG;
8236 /* convert to unsigned if it does not fit in a long long */
8237 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8238 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8239 type.t |= VT_UNSIGNED;
8240 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8241 /* XXX: test pointer compatibility */
8242 type = type1;
8243 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8244 /* XXX: test function pointer compatibility */
8245 type = type1;
8246 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8247 /* XXX: test structure compatibility */
8248 type = type1;
8249 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8250 /* NOTE: as an extension, we accept void on only one side */
8251 type.t = VT_VOID;
8252 } else {
8253 /* integer operations */
8254 type.t = VT_INT;
8255 /* convert to unsigned if it does not fit in an integer */
8256 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8257 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8258 type.t |= VT_UNSIGNED;
8261 /* now we convert second operand */
8262 gen_cast(&type);
8263 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8264 gaddrof();
8265 rc = RC_INT;
8266 if (is_float(type.t)) {
8267 rc = RC_FLOAT;
8268 #ifdef TCC_TARGET_X86_64
8269 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
8270 rc = RC_ST0;
8272 #endif
8273 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8274 /* for long longs, we use fixed registers to avoid having
8275 to handle a complicated move */
8276 rc = RC_IRET;
8279 r2 = gv(rc);
8280 /* this is horrible, but we must also convert first
8281 operand */
8282 tt = gjmp(0);
8283 gsym(u);
8284 /* put again first value and cast it */
8285 *vtop = sv;
8286 gen_cast(&type);
8287 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8288 gaddrof();
8289 r1 = gv(rc);
8290 move_reg(r2, r1);
8291 vtop->r = r2;
8292 gsym(tt);
8297 static void gexpr(void)
8299 while (1) {
8300 expr_eq();
8301 if (tok != ',')
8302 break;
8303 vpop();
8304 next();
8308 /* parse an expression and return its type without any side effect. */
8309 static void expr_type(CType *type)
8311 int saved_nocode_wanted;
8313 saved_nocode_wanted = nocode_wanted;
8314 nocode_wanted = 1;
8315 gexpr();
8316 *type = vtop->type;
8317 vpop();
8318 nocode_wanted = saved_nocode_wanted;
8321 /* parse a unary expression and return its type without any side
8322 effect. */
8323 static void unary_type(CType *type)
8325 int a;
8327 a = nocode_wanted;
8328 nocode_wanted = 1;
8329 unary();
8330 *type = vtop->type;
8331 vpop();
8332 nocode_wanted = a;
8335 /* parse a constant expression and return value in vtop. */
8336 static void expr_const1(void)
8338 int a;
8339 a = const_wanted;
8340 const_wanted = 1;
8341 expr_eq();
8342 const_wanted = a;
8345 /* parse an integer constant and return its value. */
8346 static int expr_const(void)
8348 int c;
8349 expr_const1();
8350 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8351 expect("constant expression");
8352 c = vtop->c.i;
8353 vpop();
8354 return c;
8357 /* return the label token if current token is a label, otherwise
8358 return zero */
8359 static int is_label(void)
8361 int last_tok;
8363 /* fast test first */
8364 if (tok < TOK_UIDENT)
8365 return 0;
8366 /* no need to save tokc because tok is an identifier */
8367 last_tok = tok;
8368 next();
8369 if (tok == ':') {
8370 next();
8371 return last_tok;
8372 } else {
8373 unget_tok(last_tok);
8374 return 0;
8378 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8379 int case_reg, int is_expr)
8381 int a, b, c, d;
8382 Sym *s;
8384 /* generate line number info */
8385 if (do_debug &&
8386 (last_line_num != file->line_num || last_ind != ind)) {
8387 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8388 last_ind = ind;
8389 last_line_num = file->line_num;
8392 if (is_expr) {
8393 /* default return value is (void) */
8394 vpushi(0);
8395 vtop->type.t = VT_VOID;
8398 if (tok == TOK_IF) {
8399 /* if test */
8400 next();
8401 skip('(');
8402 gexpr();
8403 skip(')');
8404 a = gtst(1, 0);
8405 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8406 c = tok;
8407 if (c == TOK_ELSE) {
8408 next();
8409 d = gjmp(0);
8410 gsym(a);
8411 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8412 gsym(d); /* patch else jmp */
8413 } else
8414 gsym(a);
8415 } else if (tok == TOK_WHILE) {
8416 next();
8417 d = ind;
8418 skip('(');
8419 gexpr();
8420 skip(')');
8421 a = gtst(1, 0);
8422 b = 0;
8423 block(&a, &b, case_sym, def_sym, case_reg, 0);
8424 gjmp_addr(d);
8425 gsym(a);
8426 gsym_addr(b, d);
8427 } else if (tok == '{') {
8428 Sym *llabel;
8430 next();
8431 /* record local declaration stack position */
8432 s = local_stack;
8433 llabel = local_label_stack;
8434 /* handle local labels declarations */
8435 if (tok == TOK_LABEL) {
8436 next();
8437 for(;;) {
8438 if (tok < TOK_UIDENT)
8439 expect("label identifier");
8440 label_push(&local_label_stack, tok, LABEL_DECLARED);
8441 next();
8442 if (tok == ',') {
8443 next();
8444 } else {
8445 skip(';');
8446 break;
8450 while (tok != '}') {
8451 decl(VT_LOCAL);
8452 if (tok != '}') {
8453 if (is_expr)
8454 vpop();
8455 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8458 /* pop locally defined labels */
8459 label_pop(&local_label_stack, llabel);
8460 /* pop locally defined symbols */
8461 if(is_expr) {
8462 /* XXX: this solution makes only valgrind happy...
8463 triggered by gcc.c-torture/execute/20000917-1.c */
8464 Sym *p;
8465 switch(vtop->type.t & VT_BTYPE) {
8466 case VT_PTR:
8467 case VT_STRUCT:
8468 case VT_ENUM:
8469 case VT_FUNC:
8470 for(p=vtop->type.ref;p;p=p->prev)
8471 if(p->prev==s)
8472 error("unsupported expression type");
8475 sym_pop(&local_stack, s);
8476 next();
8477 } else if (tok == TOK_RETURN) {
8478 next();
8479 if (tok != ';') {
8480 gexpr();
8481 gen_assign_cast(&func_vt);
8482 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8483 CType type;
8484 /* if returning structure, must copy it to implicit
8485 first pointer arg location */
8486 #ifdef TCC_ARM_EABI
8487 int align, size;
8488 size = type_size(&func_vt,&align);
8489 if(size <= 4)
8491 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8492 && (align & 3))
8494 int addr;
8495 loc = (loc - size) & -4;
8496 addr = loc;
8497 type = func_vt;
8498 vset(&type, VT_LOCAL | VT_LVAL, addr);
8499 vswap();
8500 vstore();
8501 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8503 vtop->type = int_type;
8504 gv(RC_IRET);
8505 } else {
8506 #endif
8507 type = func_vt;
8508 mk_pointer(&type);
8509 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8510 indir();
8511 vswap();
8512 /* copy structure value to pointer */
8513 vstore();
8514 #ifdef TCC_ARM_EABI
8516 #endif
8517 } else if (is_float(func_vt.t)) {
8518 gv(RC_FRET);
8519 } else {
8520 gv(RC_IRET);
8522 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8524 skip(';');
8525 rsym = gjmp(rsym); /* jmp */
8526 } else if (tok == TOK_BREAK) {
8527 /* compute jump */
8528 if (!bsym)
8529 error("cannot break");
8530 *bsym = gjmp(*bsym);
8531 next();
8532 skip(';');
8533 } else if (tok == TOK_CONTINUE) {
8534 /* compute jump */
8535 if (!csym)
8536 error("cannot continue");
8537 *csym = gjmp(*csym);
8538 next();
8539 skip(';');
8540 } else if (tok == TOK_FOR) {
8541 int e;
8542 next();
8543 skip('(');
8544 if (tok != ';') {
8545 gexpr();
8546 vpop();
8548 skip(';');
8549 d = ind;
8550 c = ind;
8551 a = 0;
8552 b = 0;
8553 if (tok != ';') {
8554 gexpr();
8555 a = gtst(1, 0);
8557 skip(';');
8558 if (tok != ')') {
8559 e = gjmp(0);
8560 c = ind;
8561 gexpr();
8562 vpop();
8563 gjmp_addr(d);
8564 gsym(e);
8566 skip(')');
8567 block(&a, &b, case_sym, def_sym, case_reg, 0);
8568 gjmp_addr(c);
8569 gsym(a);
8570 gsym_addr(b, c);
8571 } else
8572 if (tok == TOK_DO) {
8573 next();
8574 a = 0;
8575 b = 0;
8576 d = ind;
8577 block(&a, &b, case_sym, def_sym, case_reg, 0);
8578 skip(TOK_WHILE);
8579 skip('(');
8580 gsym(b);
8581 gexpr();
8582 c = gtst(0, 0);
8583 gsym_addr(c, d);
8584 skip(')');
8585 gsym(a);
8586 skip(';');
8587 } else
8588 if (tok == TOK_SWITCH) {
8589 next();
8590 skip('(');
8591 gexpr();
8592 /* XXX: other types than integer */
8593 case_reg = gv(RC_INT);
8594 vpop();
8595 skip(')');
8596 a = 0;
8597 b = gjmp(0); /* jump to first case */
8598 c = 0;
8599 block(&a, csym, &b, &c, case_reg, 0);
8600 /* if no default, jmp after switch */
8601 if (c == 0)
8602 c = ind;
8603 /* default label */
8604 gsym_addr(b, c);
8605 /* break label */
8606 gsym(a);
8607 } else
8608 if (tok == TOK_CASE) {
8609 int v1, v2;
8610 if (!case_sym)
8611 expect("switch");
8612 next();
8613 v1 = expr_const();
8614 v2 = v1;
8615 if (gnu_ext && tok == TOK_DOTS) {
8616 next();
8617 v2 = expr_const();
8618 if (v2 < v1)
8619 warning("empty case range");
8621 /* since a case is like a label, we must skip it with a jmp */
8622 b = gjmp(0);
8623 gsym(*case_sym);
8624 vseti(case_reg, 0);
8625 vpushi(v1);
8626 if (v1 == v2) {
8627 gen_op(TOK_EQ);
8628 *case_sym = gtst(1, 0);
8629 } else {
8630 gen_op(TOK_GE);
8631 *case_sym = gtst(1, 0);
8632 vseti(case_reg, 0);
8633 vpushi(v2);
8634 gen_op(TOK_LE);
8635 *case_sym = gtst(1, *case_sym);
8637 gsym(b);
8638 skip(':');
8639 is_expr = 0;
8640 goto block_after_label;
8641 } else
8642 if (tok == TOK_DEFAULT) {
8643 next();
8644 skip(':');
8645 if (!def_sym)
8646 expect("switch");
8647 if (*def_sym)
8648 error("too many 'default'");
8649 *def_sym = ind;
8650 is_expr = 0;
8651 goto block_after_label;
8652 } else
8653 if (tok == TOK_GOTO) {
8654 next();
8655 if (tok == '*' && gnu_ext) {
8656 /* computed goto */
8657 next();
8658 gexpr();
8659 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8660 expect("pointer");
8661 ggoto();
8662 } else if (tok >= TOK_UIDENT) {
8663 s = label_find(tok);
8664 /* put forward definition if needed */
8665 if (!s) {
8666 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8667 } else {
8668 if (s->r == LABEL_DECLARED)
8669 s->r = LABEL_FORWARD;
8671 /* label already defined */
8672 if (s->r & LABEL_FORWARD)
8673 s->next = (void *)gjmp((long)s->next);
8674 else
8675 gjmp_addr((long)s->next);
8676 next();
8677 } else {
8678 expect("label identifier");
8680 skip(';');
8681 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8682 asm_instr();
8683 } else {
8684 b = is_label();
8685 if (b) {
8686 /* label case */
8687 s = label_find(b);
8688 if (s) {
8689 if (s->r == LABEL_DEFINED)
8690 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8691 gsym((long)s->next);
8692 s->r = LABEL_DEFINED;
8693 } else {
8694 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8696 s->next = (void *)ind;
8697 /* we accept this, but it is a mistake */
8698 block_after_label:
8699 if (tok == '}') {
8700 warning("deprecated use of label at end of compound statement");
8701 } else {
8702 if (is_expr)
8703 vpop();
8704 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8706 } else {
8707 /* expression case */
8708 if (tok != ';') {
8709 if (is_expr) {
8710 vpop();
8711 gexpr();
8712 } else {
8713 gexpr();
8714 vpop();
8717 skip(';');
8722 /* t is the array or struct type. c is the array or struct
8723 address. cur_index/cur_field is the pointer to the current
8724 value. 'size_only' is true if only size info is needed (only used
8725 in arrays) */
8726 static void decl_designator(CType *type, Section *sec, unsigned long c,
8727 int *cur_index, Sym **cur_field,
8728 int size_only)
8730 Sym *s, *f;
8731 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8732 CType type1;
8734 notfirst = 0;
8735 elem_size = 0;
8736 nb_elems = 1;
8737 if (gnu_ext && (l = is_label()) != 0)
8738 goto struct_field;
8739 while (tok == '[' || tok == '.') {
8740 if (tok == '[') {
8741 if (!(type->t & VT_ARRAY))
8742 expect("array type");
8743 s = type->ref;
8744 next();
8745 index = expr_const();
8746 if (index < 0 || (s->c >= 0 && index >= s->c))
8747 expect("invalid index");
8748 if (tok == TOK_DOTS && gnu_ext) {
8749 next();
8750 index_last = expr_const();
8751 if (index_last < 0 ||
8752 (s->c >= 0 && index_last >= s->c) ||
8753 index_last < index)
8754 expect("invalid index");
8755 } else {
8756 index_last = index;
8758 skip(']');
8759 if (!notfirst)
8760 *cur_index = index_last;
8761 type = pointed_type(type);
8762 elem_size = type_size(type, &align);
8763 c += index * elem_size;
8764 /* NOTE: we only support ranges for last designator */
8765 nb_elems = index_last - index + 1;
8766 if (nb_elems != 1) {
8767 notfirst = 1;
8768 break;
8770 } else {
8771 next();
8772 l = tok;
8773 next();
8774 struct_field:
8775 if ((type->t & VT_BTYPE) != VT_STRUCT)
8776 expect("struct/union type");
8777 s = type->ref;
8778 l |= SYM_FIELD;
8779 f = s->next;
8780 while (f) {
8781 if (f->v == l)
8782 break;
8783 f = f->next;
8785 if (!f)
8786 expect("field");
8787 if (!notfirst)
8788 *cur_field = f;
8789 /* XXX: fix this mess by using explicit storage field */
8790 type1 = f->type;
8791 type1.t |= (type->t & ~VT_TYPE);
8792 type = &type1;
8793 c += f->c;
8795 notfirst = 1;
8797 if (notfirst) {
8798 if (tok == '=') {
8799 next();
8800 } else {
8801 if (!gnu_ext)
8802 expect("=");
8804 } else {
8805 if (type->t & VT_ARRAY) {
8806 index = *cur_index;
8807 type = pointed_type(type);
8808 c += index * type_size(type, &align);
8809 } else {
8810 f = *cur_field;
8811 if (!f)
8812 error("too many field init");
8813 /* XXX: fix this mess by using explicit storage field */
8814 type1 = f->type;
8815 type1.t |= (type->t & ~VT_TYPE);
8816 type = &type1;
8817 c += f->c;
8820 decl_initializer(type, sec, c, 0, size_only);
8822 /* XXX: make it more general */
8823 if (!size_only && nb_elems > 1) {
8824 unsigned long c_end;
8825 uint8_t *src, *dst;
8826 int i;
8828 if (!sec)
8829 error("range init not supported yet for dynamic storage");
8830 c_end = c + nb_elems * elem_size;
8831 if (c_end > sec->data_allocated)
8832 section_realloc(sec, c_end);
8833 src = sec->data + c;
8834 dst = src;
8835 for(i = 1; i < nb_elems; i++) {
8836 dst += elem_size;
8837 memcpy(dst, src, elem_size);
8842 #define EXPR_VAL 0
8843 #define EXPR_CONST 1
8844 #define EXPR_ANY 2
8846 /* store a value or an expression directly in global data or in local array */
8847 static void init_putv(CType *type, Section *sec, unsigned long c,
8848 int v, int expr_type)
8850 int saved_global_expr, bt, bit_pos, bit_size;
8851 void *ptr;
8852 unsigned long long bit_mask;
8853 CType dtype;
8855 switch(expr_type) {
8856 case EXPR_VAL:
8857 vpushi(v);
8858 break;
8859 case EXPR_CONST:
8860 /* compound literals must be allocated globally in this case */
8861 saved_global_expr = global_expr;
8862 global_expr = 1;
8863 expr_const1();
8864 global_expr = saved_global_expr;
8865 /* NOTE: symbols are accepted */
8866 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8867 error("initializer element is not constant");
8868 break;
8869 case EXPR_ANY:
8870 expr_eq();
8871 break;
8874 dtype = *type;
8875 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8877 if (sec) {
8878 /* XXX: not portable */
8879 /* XXX: generate error if incorrect relocation */
8880 gen_assign_cast(&dtype);
8881 bt = type->t & VT_BTYPE;
8882 /* we'll write at most 12 bytes */
8883 if (c + 12 > sec->data_allocated) {
8884 section_realloc(sec, c + 12);
8886 ptr = sec->data + c;
8887 /* XXX: make code faster ? */
8888 if (!(type->t & VT_BITFIELD)) {
8889 bit_pos = 0;
8890 bit_size = 32;
8891 bit_mask = -1LL;
8892 } else {
8893 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8894 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8895 bit_mask = (1LL << bit_size) - 1;
8897 if ((vtop->r & VT_SYM) &&
8898 (bt == VT_BYTE ||
8899 bt == VT_SHORT ||
8900 bt == VT_DOUBLE ||
8901 bt == VT_LDOUBLE ||
8902 bt == VT_LLONG ||
8903 (bt == VT_INT && bit_size != 32)))
8904 error("initializer element is not computable at load time");
8905 switch(bt) {
8906 case VT_BOOL:
8907 vtop->c.i = (vtop->c.i != 0);
8908 case VT_BYTE:
8909 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8910 break;
8911 case VT_SHORT:
8912 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8913 break;
8914 case VT_DOUBLE:
8915 *(double *)ptr = vtop->c.d;
8916 break;
8917 case VT_LDOUBLE:
8918 *(long double *)ptr = vtop->c.ld;
8919 break;
8920 case VT_LLONG:
8921 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8922 break;
8923 default:
8924 if (vtop->r & VT_SYM) {
8925 greloc(sec, vtop->sym, c, R_DATA_32);
8927 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8928 break;
8930 vtop--;
8931 } else {
8932 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8933 vswap();
8934 vstore();
8935 vpop();
8939 /* put zeros for variable based init */
8940 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8942 if (sec) {
8943 /* nothing to do because globals are already set to zero */
8944 } else {
8945 vpush_global_sym(&func_old_type, TOK_memset);
8946 vseti(VT_LOCAL, c);
8947 vpushi(0);
8948 vpushi(size);
8949 gfunc_call(3);
8953 /* 't' contains the type and storage info. 'c' is the offset of the
8954 object in section 'sec'. If 'sec' is NULL, it means stack based
8955 allocation. 'first' is true if array '{' must be read (multi
8956 dimension implicit array init handling). 'size_only' is true if
8957 size only evaluation is wanted (only for arrays). */
8958 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8959 int first, int size_only)
8961 int index, array_length, n, no_oblock, nb, parlevel, i;
8962 int size1, align1, expr_type;
8963 Sym *s, *f;
8964 CType *t1;
8966 if (type->t & VT_ARRAY) {
8967 s = type->ref;
8968 n = s->c;
8969 array_length = 0;
8970 t1 = pointed_type(type);
8971 size1 = type_size(t1, &align1);
8973 no_oblock = 1;
8974 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8975 tok == '{') {
8976 skip('{');
8977 no_oblock = 0;
8980 /* only parse strings here if correct type (otherwise: handle
8981 them as ((w)char *) expressions */
8982 if ((tok == TOK_LSTR &&
8983 #ifdef TCC_TARGET_PE
8984 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8985 #else
8986 (t1->t & VT_BTYPE) == VT_INT
8987 #endif
8988 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8989 while (tok == TOK_STR || tok == TOK_LSTR) {
8990 int cstr_len, ch;
8991 CString *cstr;
8993 cstr = tokc.cstr;
8994 /* compute maximum number of chars wanted */
8995 if (tok == TOK_STR)
8996 cstr_len = cstr->size;
8997 else
8998 cstr_len = cstr->size / sizeof(nwchar_t);
8999 cstr_len--;
9000 nb = cstr_len;
9001 if (n >= 0 && nb > (n - array_length))
9002 nb = n - array_length;
9003 if (!size_only) {
9004 if (cstr_len > nb)
9005 warning("initializer-string for array is too long");
9006 /* in order to go faster for common case (char
9007 string in global variable, we handle it
9008 specifically */
9009 if (sec && tok == TOK_STR && size1 == 1) {
9010 memcpy(sec->data + c + array_length, cstr->data, nb);
9011 } else {
9012 for(i=0;i<nb;i++) {
9013 if (tok == TOK_STR)
9014 ch = ((unsigned char *)cstr->data)[i];
9015 else
9016 ch = ((nwchar_t *)cstr->data)[i];
9017 init_putv(t1, sec, c + (array_length + i) * size1,
9018 ch, EXPR_VAL);
9022 array_length += nb;
9023 next();
9025 /* only add trailing zero if enough storage (no
9026 warning in this case since it is standard) */
9027 if (n < 0 || array_length < n) {
9028 if (!size_only) {
9029 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
9031 array_length++;
9033 } else {
9034 index = 0;
9035 while (tok != '}') {
9036 decl_designator(type, sec, c, &index, NULL, size_only);
9037 if (n >= 0 && index >= n)
9038 error("index too large");
9039 /* must put zero in holes (note that doing it that way
9040 ensures that it even works with designators) */
9041 if (!size_only && array_length < index) {
9042 init_putz(t1, sec, c + array_length * size1,
9043 (index - array_length) * size1);
9045 index++;
9046 if (index > array_length)
9047 array_length = index;
9048 /* special test for multi dimensional arrays (may not
9049 be strictly correct if designators are used at the
9050 same time) */
9051 if (index >= n && no_oblock)
9052 break;
9053 if (tok == '}')
9054 break;
9055 skip(',');
9058 if (!no_oblock)
9059 skip('}');
9060 /* put zeros at the end */
9061 if (!size_only && n >= 0 && array_length < n) {
9062 init_putz(t1, sec, c + array_length * size1,
9063 (n - array_length) * size1);
9065 /* patch type size if needed */
9066 if (n < 0)
9067 s->c = array_length;
9068 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
9069 (sec || !first || tok == '{')) {
9070 int par_count;
9072 /* NOTE: the previous test is a specific case for automatic
9073 struct/union init */
9074 /* XXX: union needs only one init */
9076 /* XXX: this test is incorrect for local initializers
9077 beginning with ( without {. It would be much more difficult
9078 to do it correctly (ideally, the expression parser should
9079 be used in all cases) */
9080 par_count = 0;
9081 if (tok == '(') {
9082 AttributeDef ad1;
9083 CType type1;
9084 next();
9085 while (tok == '(') {
9086 par_count++;
9087 next();
9089 if (!parse_btype(&type1, &ad1))
9090 expect("cast");
9091 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
9092 #if 0
9093 if (!is_assignable_types(type, &type1))
9094 error("invalid type for cast");
9095 #endif
9096 skip(')');
9098 no_oblock = 1;
9099 if (first || tok == '{') {
9100 skip('{');
9101 no_oblock = 0;
9103 s = type->ref;
9104 f = s->next;
9105 array_length = 0;
9106 index = 0;
9107 n = s->c;
9108 while (tok != '}') {
9109 decl_designator(type, sec, c, NULL, &f, size_only);
9110 index = f->c;
9111 if (!size_only && array_length < index) {
9112 init_putz(type, sec, c + array_length,
9113 index - array_length);
9115 index = index + type_size(&f->type, &align1);
9116 if (index > array_length)
9117 array_length = index;
9118 f = f->next;
9119 if (no_oblock && f == NULL)
9120 break;
9121 if (tok == '}')
9122 break;
9123 skip(',');
9125 /* put zeros at the end */
9126 if (!size_only && array_length < n) {
9127 init_putz(type, sec, c + array_length,
9128 n - array_length);
9130 if (!no_oblock)
9131 skip('}');
9132 while (par_count) {
9133 skip(')');
9134 par_count--;
9136 } else if (tok == '{') {
9137 next();
9138 decl_initializer(type, sec, c, first, size_only);
9139 skip('}');
9140 } else if (size_only) {
9141 /* just skip expression */
9142 parlevel = 0;
9143 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
9144 tok != -1) {
9145 if (tok == '(')
9146 parlevel++;
9147 else if (tok == ')')
9148 parlevel--;
9149 next();
9151 } else {
9152 /* currently, we always use constant expression for globals
9153 (may change for scripting case) */
9154 expr_type = EXPR_CONST;
9155 if (!sec)
9156 expr_type = EXPR_ANY;
9157 init_putv(type, sec, c, 0, expr_type);
9161 /* parse an initializer for type 't' if 'has_init' is non zero, and
9162 allocate space in local or global data space ('r' is either
9163 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
9164 variable 'v' of scope 'scope' is declared before initializers are
9165 parsed. If 'v' is zero, then a reference to the new object is put
9166 in the value stack. If 'has_init' is 2, a special parsing is done
9167 to handle string constants. */
9168 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
9169 int has_init, int v, int scope)
9171 int size, align, addr, data_offset;
9172 int level;
9173 ParseState saved_parse_state;
9174 TokenString init_str;
9175 Section *sec;
9177 size = type_size(type, &align);
9178 /* If unknown size, we must evaluate it before
9179 evaluating initializers because
9180 initializers can generate global data too
9181 (e.g. string pointers or ISOC99 compound
9182 literals). It also simplifies local
9183 initializers handling */
9184 tok_str_new(&init_str);
9185 if (size < 0) {
9186 if (!has_init)
9187 error("unknown type size");
9188 /* get all init string */
9189 if (has_init == 2) {
9190 /* only get strings */
9191 while (tok == TOK_STR || tok == TOK_LSTR) {
9192 tok_str_add_tok(&init_str);
9193 next();
9195 } else {
9196 level = 0;
9197 while (level > 0 || (tok != ',' && tok != ';')) {
9198 if (tok < 0)
9199 error("unexpected end of file in initializer");
9200 tok_str_add_tok(&init_str);
9201 if (tok == '{')
9202 level++;
9203 else if (tok == '}') {
9204 level--;
9205 if (level <= 0) {
9206 next();
9207 break;
9210 next();
9213 tok_str_add(&init_str, -1);
9214 tok_str_add(&init_str, 0);
9216 /* compute size */
9217 save_parse_state(&saved_parse_state);
9219 macro_ptr = init_str.str;
9220 next();
9221 decl_initializer(type, NULL, 0, 1, 1);
9222 /* prepare second initializer parsing */
9223 macro_ptr = init_str.str;
9224 next();
9226 /* if still unknown size, error */
9227 size = type_size(type, &align);
9228 if (size < 0)
9229 error("unknown type size");
9231 /* take into account specified alignment if bigger */
9232 if (ad->aligned) {
9233 if (ad->aligned > align)
9234 align = ad->aligned;
9235 } else if (ad->packed) {
9236 align = 1;
9238 if ((r & VT_VALMASK) == VT_LOCAL) {
9239 sec = NULL;
9240 if (do_bounds_check && (type->t & VT_ARRAY))
9241 loc--;
9242 loc = (loc - size) & -align;
9243 addr = loc;
9244 /* handles bounds */
9245 /* XXX: currently, since we do only one pass, we cannot track
9246 '&' operators, so we add only arrays */
9247 if (do_bounds_check && (type->t & VT_ARRAY)) {
9248 unsigned long *bounds_ptr;
9249 /* add padding between regions */
9250 loc--;
9251 /* then add local bound info */
9252 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9253 bounds_ptr[0] = addr;
9254 bounds_ptr[1] = size;
9256 if (v) {
9257 /* local variable */
9258 sym_push(v, type, r, addr);
9259 } else {
9260 /* push local reference */
9261 vset(type, r, addr);
9263 } else {
9264 Sym *sym;
9266 sym = NULL;
9267 if (v && scope == VT_CONST) {
9268 /* see if the symbol was already defined */
9269 sym = sym_find(v);
9270 if (sym) {
9271 if (!is_compatible_types(&sym->type, type))
9272 error("incompatible types for redefinition of '%s'",
9273 get_tok_str(v, NULL));
9274 if (sym->type.t & VT_EXTERN) {
9275 /* if the variable is extern, it was not allocated */
9276 sym->type.t &= ~VT_EXTERN;
9277 /* set array size if it was ommited in extern
9278 declaration */
9279 if ((sym->type.t & VT_ARRAY) &&
9280 sym->type.ref->c < 0 &&
9281 type->ref->c >= 0)
9282 sym->type.ref->c = type->ref->c;
9283 } else {
9284 /* we accept several definitions of the same
9285 global variable. this is tricky, because we
9286 must play with the SHN_COMMON type of the symbol */
9287 /* XXX: should check if the variable was already
9288 initialized. It is incorrect to initialized it
9289 twice */
9290 /* no init data, we won't add more to the symbol */
9291 if (!has_init)
9292 goto no_alloc;
9297 /* allocate symbol in corresponding section */
9298 sec = ad->section;
9299 if (!sec) {
9300 if (has_init)
9301 sec = data_section;
9302 else if (tcc_state->nocommon)
9303 sec = bss_section;
9305 if (sec) {
9306 data_offset = sec->data_offset;
9307 data_offset = (data_offset + align - 1) & -align;
9308 addr = data_offset;
9309 /* very important to increment global pointer at this time
9310 because initializers themselves can create new initializers */
9311 data_offset += size;
9312 /* add padding if bound check */
9313 if (do_bounds_check)
9314 data_offset++;
9315 sec->data_offset = data_offset;
9316 /* allocate section space to put the data */
9317 if (sec->sh_type != SHT_NOBITS &&
9318 data_offset > sec->data_allocated)
9319 section_realloc(sec, data_offset);
9320 /* align section if needed */
9321 if (align > sec->sh_addralign)
9322 sec->sh_addralign = align;
9323 } else {
9324 addr = 0; /* avoid warning */
9327 if (v) {
9328 if (scope != VT_CONST || !sym) {
9329 sym = sym_push(v, type, r | VT_SYM, 0);
9331 /* update symbol definition */
9332 if (sec) {
9333 put_extern_sym(sym, sec, addr, size);
9334 } else {
9335 ElfW(Sym) *esym;
9336 /* put a common area */
9337 put_extern_sym(sym, NULL, align, size);
9338 /* XXX: find a nicer way */
9339 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
9340 esym->st_shndx = SHN_COMMON;
9342 } else {
9343 CValue cval;
9345 /* push global reference */
9346 sym = get_sym_ref(type, sec, addr, size);
9347 cval.ul = 0;
9348 vsetc(type, VT_CONST | VT_SYM, &cval);
9349 vtop->sym = sym;
9352 /* handles bounds now because the symbol must be defined
9353 before for the relocation */
9354 if (do_bounds_check) {
9355 unsigned long *bounds_ptr;
9357 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9358 /* then add global bound info */
9359 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9360 bounds_ptr[0] = 0; /* relocated */
9361 bounds_ptr[1] = size;
9364 if (has_init) {
9365 decl_initializer(type, sec, addr, 1, 0);
9366 /* restore parse state if needed */
9367 if (init_str.str) {
9368 tok_str_free(init_str.str);
9369 restore_parse_state(&saved_parse_state);
9372 no_alloc: ;
9375 void put_func_debug(Sym *sym)
9377 char buf[512];
9379 /* stabs info */
9380 /* XXX: we put here a dummy type */
9381 snprintf(buf, sizeof(buf), "%s:%c1",
9382 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9383 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9384 cur_text_section, sym->c);
9385 /* //gr gdb wants a line at the function */
9386 put_stabn(N_SLINE, 0, file->line_num, 0);
9387 last_ind = 0;
9388 last_line_num = 0;
9391 /* parse an old style function declaration list */
9392 /* XXX: check multiple parameter */
9393 static void func_decl_list(Sym *func_sym)
9395 AttributeDef ad;
9396 int v;
9397 Sym *s;
9398 CType btype, type;
9400 /* parse each declaration */
9401 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9402 if (!parse_btype(&btype, &ad))
9403 expect("declaration list");
9404 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9405 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9406 tok == ';') {
9407 /* we accept no variable after */
9408 } else {
9409 for(;;) {
9410 type = btype;
9411 type_decl(&type, &ad, &v, TYPE_DIRECT);
9412 /* find parameter in function parameter list */
9413 s = func_sym->next;
9414 while (s != NULL) {
9415 if ((s->v & ~SYM_FIELD) == v)
9416 goto found;
9417 s = s->next;
9419 error("declaration for parameter '%s' but no such parameter",
9420 get_tok_str(v, NULL));
9421 found:
9422 /* check that no storage specifier except 'register' was given */
9423 if (type.t & VT_STORAGE)
9424 error("storage class specified for '%s'", get_tok_str(v, NULL));
9425 convert_parameter_type(&type);
9426 /* we can add the type (NOTE: it could be local to the function) */
9427 s->type = type;
9428 /* accept other parameters */
9429 if (tok == ',')
9430 next();
9431 else
9432 break;
9435 skip(';');
9439 /* parse a function defined by symbol 'sym' and generate its code in
9440 'cur_text_section' */
9441 static void gen_function(Sym *sym)
9443 int saved_nocode_wanted = nocode_wanted;
9444 nocode_wanted = 0;
9445 ind = cur_text_section->data_offset;
9446 /* NOTE: we patch the symbol size later */
9447 put_extern_sym(sym, cur_text_section, ind, 0);
9448 funcname = get_tok_str(sym->v, NULL);
9449 func_ind = ind;
9450 /* put debug symbol */
9451 if (do_debug)
9452 put_func_debug(sym);
9453 /* push a dummy symbol to enable local sym storage */
9454 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9455 gfunc_prolog(&sym->type);
9456 rsym = 0;
9457 block(NULL, NULL, NULL, NULL, 0, 0);
9458 gsym(rsym);
9459 gfunc_epilog();
9460 cur_text_section->data_offset = ind;
9461 label_pop(&global_label_stack, NULL);
9462 sym_pop(&local_stack, NULL); /* reset local stack */
9463 /* end of function */
9464 /* patch symbol size */
9465 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
9466 ind - func_ind;
9467 if (do_debug) {
9468 put_stabn(N_FUN, 0, 0, ind - func_ind);
9470 /* It's better to crash than to generate wrong code */
9471 cur_text_section = NULL;
9472 funcname = ""; /* for safety */
9473 func_vt.t = VT_VOID; /* for safety */
9474 ind = 0; /* for safety */
9475 nocode_wanted = saved_nocode_wanted;
9478 static void gen_inline_functions(void)
9480 Sym *sym;
9481 CType *type;
9482 int *str, inline_generated;
9484 /* iterate while inline function are referenced */
9485 for(;;) {
9486 inline_generated = 0;
9487 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9488 type = &sym->type;
9489 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9490 (type->t & (VT_STATIC | VT_INLINE)) ==
9491 (VT_STATIC | VT_INLINE) &&
9492 sym->c != 0) {
9493 /* the function was used: generate its code and
9494 convert it to a normal function */
9495 str = INLINE_DEF(sym->r);
9496 sym->r = VT_SYM | VT_CONST;
9497 sym->type.t &= ~VT_INLINE;
9499 macro_ptr = str;
9500 next();
9501 cur_text_section = text_section;
9502 gen_function(sym);
9503 macro_ptr = NULL; /* fail safe */
9505 tok_str_free(str);
9506 inline_generated = 1;
9509 if (!inline_generated)
9510 break;
9513 /* free all remaining inline function tokens */
9514 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9515 type = &sym->type;
9516 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9517 (type->t & (VT_STATIC | VT_INLINE)) ==
9518 (VT_STATIC | VT_INLINE)) {
9519 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9520 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9521 continue;
9522 str = INLINE_DEF(sym->r);
9523 tok_str_free(str);
9524 sym->r = 0; /* fail safe */
9529 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9530 static void decl(int l)
9532 int v, has_init, r;
9533 CType type, btype;
9534 Sym *sym;
9535 AttributeDef ad;
9537 while (1) {
9538 if (!parse_btype(&btype, &ad)) {
9539 /* skip redundant ';' */
9540 /* XXX: find more elegant solution */
9541 if (tok == ';') {
9542 next();
9543 continue;
9545 if (l == VT_CONST &&
9546 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9547 /* global asm block */
9548 asm_global_instr();
9549 continue;
9551 /* special test for old K&R protos without explicit int
9552 type. Only accepted when defining global data */
9553 if (l == VT_LOCAL || tok < TOK_DEFINE)
9554 break;
9555 btype.t = VT_INT;
9557 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9558 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9559 tok == ';') {
9560 /* we accept no variable after */
9561 next();
9562 continue;
9564 while (1) { /* iterate thru each declaration */
9565 type = btype;
9566 type_decl(&type, &ad, &v, TYPE_DIRECT);
9567 #if 0
9569 char buf[500];
9570 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9571 printf("type = '%s'\n", buf);
9573 #endif
9574 if ((type.t & VT_BTYPE) == VT_FUNC) {
9575 /* if old style function prototype, we accept a
9576 declaration list */
9577 sym = type.ref;
9578 if (sym->c == FUNC_OLD)
9579 func_decl_list(sym);
9582 if (tok == '{') {
9583 if (l == VT_LOCAL)
9584 error("cannot use local functions");
9585 if ((type.t & VT_BTYPE) != VT_FUNC)
9586 expect("function definition");
9588 /* reject abstract declarators in function definition */
9589 sym = type.ref;
9590 while ((sym = sym->next) != NULL)
9591 if (!(sym->v & ~SYM_FIELD))
9592 expect("identifier");
9594 /* XXX: cannot do better now: convert extern line to static inline */
9595 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9596 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9598 sym = sym_find(v);
9599 if (sym) {
9600 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9601 goto func_error1;
9602 /* specific case: if not func_call defined, we put
9603 the one of the prototype */
9604 /* XXX: should have default value */
9605 r = sym->type.ref->r;
9606 if (FUNC_CALL(r) != FUNC_CDECL
9607 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9608 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9609 if (FUNC_EXPORT(r))
9610 FUNC_EXPORT(type.ref->r) = 1;
9612 if (!is_compatible_types(&sym->type, &type)) {
9613 func_error1:
9614 error("incompatible types for redefinition of '%s'",
9615 get_tok_str(v, NULL));
9617 /* if symbol is already defined, then put complete type */
9618 sym->type = type;
9619 } else {
9620 /* put function symbol */
9621 sym = global_identifier_push(v, type.t, 0);
9622 sym->type.ref = type.ref;
9625 /* static inline functions are just recorded as a kind
9626 of macro. Their code will be emitted at the end of
9627 the compilation unit only if they are used */
9628 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9629 (VT_INLINE | VT_STATIC)) {
9630 TokenString func_str;
9631 int block_level;
9633 tok_str_new(&func_str);
9635 block_level = 0;
9636 for(;;) {
9637 int t;
9638 if (tok == TOK_EOF)
9639 error("unexpected end of file");
9640 tok_str_add_tok(&func_str);
9641 t = tok;
9642 next();
9643 if (t == '{') {
9644 block_level++;
9645 } else if (t == '}') {
9646 block_level--;
9647 if (block_level == 0)
9648 break;
9651 tok_str_add(&func_str, -1);
9652 tok_str_add(&func_str, 0);
9653 INLINE_DEF(sym->r) = func_str.str;
9654 } else {
9655 /* compute text section */
9656 cur_text_section = ad.section;
9657 if (!cur_text_section)
9658 cur_text_section = text_section;
9659 sym->r = VT_SYM | VT_CONST;
9660 gen_function(sym);
9662 break;
9663 } else {
9664 if (btype.t & VT_TYPEDEF) {
9665 /* save typedefed type */
9666 /* XXX: test storage specifiers ? */
9667 sym = sym_push(v, &type, 0, 0);
9668 sym->type.t |= VT_TYPEDEF;
9669 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9670 /* external function definition */
9671 /* specific case for func_call attribute */
9672 if (ad.func_attr)
9673 type.ref->r = ad.func_attr;
9674 external_sym(v, &type, 0);
9675 } else {
9676 /* not lvalue if array */
9677 r = 0;
9678 if (!(type.t & VT_ARRAY))
9679 r |= lvalue_type(type.t);
9680 has_init = (tok == '=');
9681 if ((btype.t & VT_EXTERN) ||
9682 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9683 !has_init && l == VT_CONST && type.ref->c < 0)) {
9684 /* external variable */
9685 /* NOTE: as GCC, uninitialized global static
9686 arrays of null size are considered as
9687 extern */
9688 external_sym(v, &type, r);
9689 } else {
9690 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9691 if (type.t & VT_STATIC)
9692 r |= VT_CONST;
9693 else
9694 r |= l;
9695 if (has_init)
9696 next();
9697 decl_initializer_alloc(&type, &ad, r,
9698 has_init, v, l);
9701 if (tok != ',') {
9702 skip(';');
9703 break;
9705 next();
9711 /* better than nothing, but needs extension to handle '-E' option
9712 correctly too */
9713 static void preprocess_init(TCCState *s1)
9715 s1->include_stack_ptr = s1->include_stack;
9716 /* XXX: move that before to avoid having to initialize
9717 file->ifdef_stack_ptr ? */
9718 s1->ifdef_stack_ptr = s1->ifdef_stack;
9719 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9721 /* XXX: not ANSI compliant: bound checking says error */
9722 vtop = vstack - 1;
9723 s1->pack_stack[0] = 0;
9724 s1->pack_stack_ptr = s1->pack_stack;
9727 /* compile the C file opened in 'file'. Return non zero if errors. */
9728 static int tcc_compile(TCCState *s1)
9730 Sym *define_start;
9731 char buf[512];
9732 volatile int section_sym;
9734 #ifdef INC_DEBUG
9735 printf("%s: **** new file\n", file->filename);
9736 #endif
9737 preprocess_init(s1);
9739 cur_text_section = NULL;
9740 funcname = "";
9741 anon_sym = SYM_FIRST_ANOM;
9743 /* file info: full path + filename */
9744 section_sym = 0; /* avoid warning */
9745 if (do_debug) {
9746 section_sym = put_elf_sym(symtab_section, 0, 0,
9747 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
9748 text_section->sh_num, NULL);
9749 getcwd(buf, sizeof(buf));
9750 #ifdef _WIN32
9751 normalize_slashes(buf);
9752 #endif
9753 pstrcat(buf, sizeof(buf), "/");
9754 put_stabs_r(buf, N_SO, 0, 0,
9755 text_section->data_offset, text_section, section_sym);
9756 put_stabs_r(file->filename, N_SO, 0, 0,
9757 text_section->data_offset, text_section, section_sym);
9759 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9760 symbols can be safely used */
9761 put_elf_sym(symtab_section, 0, 0,
9762 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
9763 SHN_ABS, file->filename);
9765 /* define some often used types */
9766 int_type.t = VT_INT;
9768 char_pointer_type.t = VT_BYTE;
9769 mk_pointer(&char_pointer_type);
9771 func_old_type.t = VT_FUNC;
9772 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9774 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9775 float_type.t = VT_FLOAT;
9776 double_type.t = VT_DOUBLE;
9778 func_float_type.t = VT_FUNC;
9779 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9780 func_double_type.t = VT_FUNC;
9781 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9782 #endif
9784 #if 0
9785 /* define 'void *alloca(unsigned int)' builtin function */
9787 Sym *s1;
9789 p = anon_sym++;
9790 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9791 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9792 s1->next = NULL;
9793 sym->next = s1;
9794 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9796 #endif
9798 define_start = define_stack;
9799 nocode_wanted = 1;
9801 if (setjmp(s1->error_jmp_buf) == 0) {
9802 s1->nb_errors = 0;
9803 s1->error_set_jmp_enabled = 1;
9805 ch = file->buf_ptr[0];
9806 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9807 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9808 next();
9809 decl(VT_CONST);
9810 if (tok != TOK_EOF)
9811 expect("declaration");
9813 /* end of translation unit info */
9814 if (do_debug) {
9815 put_stabs_r(NULL, N_SO, 0, 0,
9816 text_section->data_offset, text_section, section_sym);
9819 s1->error_set_jmp_enabled = 0;
9821 /* reset define stack, but leave -Dsymbols (may be incorrect if
9822 they are undefined) */
9823 free_defines(define_start);
9825 gen_inline_functions();
9827 sym_pop(&global_stack, NULL);
9828 sym_pop(&local_stack, NULL);
9830 return s1->nb_errors != 0 ? -1 : 0;
9833 /* Preprocess the current file */
9834 /* XXX: add line and file infos,
9835 * XXX: add options to preserve spaces (partly done, only spaces in macro are
9836 * not preserved)
9838 static int tcc_preprocess(TCCState *s1)
9840 Sym *define_start;
9841 BufferedFile *file_ref;
9842 int token_seen, line_ref;
9844 preprocess_init(s1);
9845 define_start = define_stack;
9846 ch = file->buf_ptr[0];
9848 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9849 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9850 PARSE_FLAG_LINEFEED;
9852 token_seen = 0;
9853 line_ref = 0;
9854 file_ref = NULL;
9856 for (;;) {
9857 next();
9858 if (tok == TOK_EOF) {
9859 break;
9860 } else if (tok == TOK_LINEFEED) {
9861 if (!token_seen)
9862 continue;
9863 ++line_ref;
9864 token_seen = 0;
9865 } else if (token_seen) {
9866 fwrite(tok_spaces.data, tok_spaces.size, 1, s1->outfile);
9867 } else {
9868 int d = file->line_num - line_ref;
9869 if (file != file_ref || d < 0 || d >= 8)
9870 fprintf(s1->outfile, "# %d \"%s\"\n", file->line_num, file->filename);
9871 else
9872 while (d)
9873 fputs("\n", s1->outfile), --d;
9874 line_ref = (file_ref = file)->line_num;
9875 token_seen = 1;
9877 fputs(get_tok_str(tok, &tokc), s1->outfile);
9879 free_defines(define_start);
9880 return 0;
9883 #ifdef LIBTCC
9884 int tcc_compile_string(TCCState *s, const char *str)
9886 BufferedFile bf1, *bf = &bf1;
9887 int ret, len;
9888 char *buf;
9890 /* init file structure */
9891 bf->fd = -1;
9892 /* XXX: avoid copying */
9893 len = strlen(str);
9894 buf = tcc_malloc(len + 1);
9895 if (!buf)
9896 return -1;
9897 memcpy(buf, str, len);
9898 buf[len] = CH_EOB;
9899 bf->buf_ptr = buf;
9900 bf->buf_end = buf + len;
9901 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9902 bf->line_num = 1;
9903 file = bf;
9904 ret = tcc_compile(s);
9905 file = NULL;
9906 tcc_free(buf);
9908 /* currently, no need to close */
9909 return ret;
9911 #endif
9913 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9914 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9916 BufferedFile bf1, *bf = &bf1;
9918 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9919 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9920 /* default value */
9921 if (!value)
9922 value = "1";
9923 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9925 /* init file structure */
9926 bf->fd = -1;
9927 bf->buf_ptr = bf->buffer;
9928 bf->buf_end = bf->buffer + strlen(bf->buffer);
9929 *bf->buf_end = CH_EOB;
9930 bf->filename[0] = '\0';
9931 bf->line_num = 1;
9932 file = bf;
9934 s1->include_stack_ptr = s1->include_stack;
9936 /* parse with define parser */
9937 ch = file->buf_ptr[0];
9938 next_nomacro();
9939 parse_define();
9940 file = NULL;
9943 /* undefine a preprocessor symbol */
9944 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9946 TokenSym *ts;
9947 Sym *s;
9948 ts = tok_alloc(sym, strlen(sym));
9949 s = define_find(ts->tok);
9950 /* undefine symbol by putting an invalid name */
9951 if (s)
9952 define_undef(s);
9955 #ifdef CONFIG_TCC_ASM
9957 #ifdef TCC_TARGET_I386
9958 #include "i386-asm.c"
9959 #endif
9960 #include "tccasm.c"
9962 #else
9963 static void asm_instr(void)
9965 error("inline asm() not supported");
9967 static void asm_global_instr(void)
9969 error("inline asm() not supported");
9971 #endif
9973 #include "tccelf.c"
9975 #ifdef TCC_TARGET_COFF
9976 #include "tcccoff.c"
9977 #endif
9979 #ifdef TCC_TARGET_PE
9980 #include "tccpe.c"
9981 #endif
9983 /* print the position in the source file of PC value 'pc' by reading
9984 the stabs debug information */
9985 static void rt_printline(unsigned long wanted_pc)
9987 Stab_Sym *sym, *sym_end;
9988 char func_name[128], last_func_name[128];
9989 unsigned long func_addr, last_pc, pc;
9990 const char *incl_files[INCLUDE_STACK_SIZE];
9991 int incl_index, len, last_line_num, i;
9992 const char *str, *p;
9994 fprintf(stderr, "0x%08lx:", wanted_pc);
9996 func_name[0] = '\0';
9997 func_addr = 0;
9998 incl_index = 0;
9999 last_func_name[0] = '\0';
10000 last_pc = 0xffffffff;
10001 last_line_num = 1;
10002 sym = (Stab_Sym *)stab_section->data + 1;
10003 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
10004 while (sym < sym_end) {
10005 switch(sym->n_type) {
10006 /* function start or end */
10007 case N_FUN:
10008 if (sym->n_strx == 0) {
10009 /* we test if between last line and end of function */
10010 pc = sym->n_value + func_addr;
10011 if (wanted_pc >= last_pc && wanted_pc < pc)
10012 goto found;
10013 func_name[0] = '\0';
10014 func_addr = 0;
10015 } else {
10016 str = stabstr_section->data + sym->n_strx;
10017 p = strchr(str, ':');
10018 if (!p) {
10019 pstrcpy(func_name, sizeof(func_name), str);
10020 } else {
10021 len = p - str;
10022 if (len > sizeof(func_name) - 1)
10023 len = sizeof(func_name) - 1;
10024 memcpy(func_name, str, len);
10025 func_name[len] = '\0';
10027 func_addr = sym->n_value;
10029 break;
10030 /* line number info */
10031 case N_SLINE:
10032 pc = sym->n_value + func_addr;
10033 if (wanted_pc >= last_pc && wanted_pc < pc)
10034 goto found;
10035 last_pc = pc;
10036 last_line_num = sym->n_desc;
10037 /* XXX: slow! */
10038 strcpy(last_func_name, func_name);
10039 break;
10040 /* include files */
10041 case N_BINCL:
10042 str = stabstr_section->data + sym->n_strx;
10043 add_incl:
10044 if (incl_index < INCLUDE_STACK_SIZE) {
10045 incl_files[incl_index++] = str;
10047 break;
10048 case N_EINCL:
10049 if (incl_index > 1)
10050 incl_index--;
10051 break;
10052 case N_SO:
10053 if (sym->n_strx == 0) {
10054 incl_index = 0; /* end of translation unit */
10055 } else {
10056 str = stabstr_section->data + sym->n_strx;
10057 /* do not add path */
10058 len = strlen(str);
10059 if (len > 0 && str[len - 1] != '/')
10060 goto add_incl;
10062 break;
10064 sym++;
10067 /* second pass: we try symtab symbols (no line number info) */
10068 incl_index = 0;
10070 ElfW(Sym) *sym, *sym_end;
10071 int type;
10073 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
10074 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
10075 sym < sym_end;
10076 sym++) {
10077 type = ELFW(ST_TYPE)(sym->st_info);
10078 if (type == STT_FUNC) {
10079 if (wanted_pc >= sym->st_value &&
10080 wanted_pc < sym->st_value + sym->st_size) {
10081 pstrcpy(last_func_name, sizeof(last_func_name),
10082 strtab_section->data + sym->st_name);
10083 goto found;
10088 /* did not find any info: */
10089 fprintf(stderr, " ???\n");
10090 return;
10091 found:
10092 if (last_func_name[0] != '\0') {
10093 fprintf(stderr, " %s()", last_func_name);
10095 if (incl_index > 0) {
10096 fprintf(stderr, " (%s:%d",
10097 incl_files[incl_index - 1], last_line_num);
10098 for(i = incl_index - 2; i >= 0; i--)
10099 fprintf(stderr, ", included from %s", incl_files[i]);
10100 fprintf(stderr, ")");
10102 fprintf(stderr, "\n");
10105 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
10107 #ifdef __i386__
10109 /* fix for glibc 2.1 */
10110 #ifndef REG_EIP
10111 #define REG_EIP EIP
10112 #define REG_EBP EBP
10113 #endif
10115 /* return the PC at frame level 'level'. Return non zero if not found */
10116 static int rt_get_caller_pc(unsigned long *paddr,
10117 ucontext_t *uc, int level)
10119 unsigned long fp;
10120 int i;
10122 if (level == 0) {
10123 #if defined(__FreeBSD__)
10124 *paddr = uc->uc_mcontext.mc_eip;
10125 #elif defined(__dietlibc__)
10126 *paddr = uc->uc_mcontext.eip;
10127 #else
10128 *paddr = uc->uc_mcontext.gregs[REG_EIP];
10129 #endif
10130 return 0;
10131 } else {
10132 #if defined(__FreeBSD__)
10133 fp = uc->uc_mcontext.mc_ebp;
10134 #elif defined(__dietlibc__)
10135 fp = uc->uc_mcontext.ebp;
10136 #else
10137 fp = uc->uc_mcontext.gregs[REG_EBP];
10138 #endif
10139 for(i=1;i<level;i++) {
10140 /* XXX: check address validity with program info */
10141 if (fp <= 0x1000 || fp >= 0xc0000000)
10142 return -1;
10143 fp = ((unsigned long *)fp)[0];
10145 *paddr = ((unsigned long *)fp)[1];
10146 return 0;
10149 #elif defined(__x86_64__)
10150 /* return the PC at frame level 'level'. Return non zero if not found */
10151 static int rt_get_caller_pc(unsigned long *paddr,
10152 ucontext_t *uc, int level)
10154 unsigned long fp;
10155 int i;
10157 if (level == 0) {
10158 /* XXX: only support linux */
10159 *paddr = uc->uc_mcontext.gregs[REG_RIP];
10160 return 0;
10161 } else {
10162 fp = uc->uc_mcontext.gregs[REG_RBP];
10163 for(i=1;i<level;i++) {
10164 /* XXX: check address validity with program info */
10165 if (fp <= 0x1000 || fp >= 0xc0000000)
10166 return -1;
10167 fp = ((unsigned long *)fp)[0];
10169 *paddr = ((unsigned long *)fp)[1];
10170 return 0;
10173 #else
10175 #warning add arch specific rt_get_caller_pc()
10177 static int rt_get_caller_pc(unsigned long *paddr,
10178 ucontext_t *uc, int level)
10180 return -1;
10182 #endif
10184 /* emit a run time error at position 'pc' */
10185 void rt_error(ucontext_t *uc, const char *fmt, ...)
10187 va_list ap;
10188 unsigned long pc;
10189 int i;
10191 va_start(ap, fmt);
10192 fprintf(stderr, "Runtime error: ");
10193 vfprintf(stderr, fmt, ap);
10194 fprintf(stderr, "\n");
10195 for(i=0;i<num_callers;i++) {
10196 if (rt_get_caller_pc(&pc, uc, i) < 0)
10197 break;
10198 if (i == 0)
10199 fprintf(stderr, "at ");
10200 else
10201 fprintf(stderr, "by ");
10202 rt_printline(pc);
10204 exit(255);
10205 va_end(ap);
10208 /* signal handler for fatal errors */
10209 static void sig_error(int signum, siginfo_t *siginf, void *puc)
10211 ucontext_t *uc = puc;
10213 switch(signum) {
10214 case SIGFPE:
10215 switch(siginf->si_code) {
10216 case FPE_INTDIV:
10217 case FPE_FLTDIV:
10218 rt_error(uc, "division by zero");
10219 break;
10220 default:
10221 rt_error(uc, "floating point exception");
10222 break;
10224 break;
10225 case SIGBUS:
10226 case SIGSEGV:
10227 if (rt_bound_error_msg && *rt_bound_error_msg)
10228 rt_error(uc, *rt_bound_error_msg);
10229 else
10230 rt_error(uc, "dereferencing invalid pointer");
10231 break;
10232 case SIGILL:
10233 rt_error(uc, "illegal instruction");
10234 break;
10235 case SIGABRT:
10236 rt_error(uc, "abort() called");
10237 break;
10238 default:
10239 rt_error(uc, "caught signal %d", signum);
10240 break;
10242 exit(255);
10244 #endif
10246 /* do all relocations (needed before using tcc_get_symbol()) */
10247 int tcc_relocate(TCCState *s1)
10249 Section *s;
10250 int i;
10252 s1->nb_errors = 0;
10254 #ifdef TCC_TARGET_PE
10255 pe_add_runtime(s1);
10256 #else
10257 tcc_add_runtime(s1);
10258 #endif
10260 relocate_common_syms();
10262 tcc_add_linker_symbols(s1);
10263 #ifndef TCC_TARGET_PE
10264 build_got_entries(s1);
10265 #endif
10266 /* compute relocation address : section are relocated in place. We
10267 also alloc the bss space */
10268 for(i = 1; i < s1->nb_sections; i++) {
10269 s = s1->sections[i];
10270 if (s->sh_flags & SHF_ALLOC) {
10271 if (s->sh_type == SHT_NOBITS)
10272 s->data = tcc_mallocz(s->data_offset);
10273 s->sh_addr = (unsigned long)s->data;
10277 relocate_syms(s1, 1);
10279 if (s1->nb_errors != 0)
10280 return -1;
10282 /* relocate each section */
10283 for(i = 1; i < s1->nb_sections; i++) {
10284 s = s1->sections[i];
10285 if (s->reloc)
10286 relocate_section(s1, s);
10289 /* mark executable sections as executable in memory */
10290 for(i = 1; i < s1->nb_sections; i++) {
10291 s = s1->sections[i];
10292 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10293 (SHF_ALLOC | SHF_EXECINSTR))
10294 set_pages_executable(s->data, s->data_offset);
10296 return 0;
10299 /* launch the compiled program with the given arguments */
10300 int tcc_run(TCCState *s1, int argc, char **argv)
10302 int (*prog_main)(int, char **);
10304 if (tcc_relocate(s1) < 0)
10305 return -1;
10307 prog_main = tcc_get_symbol_err(s1, "main");
10309 if (do_debug) {
10310 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10311 error("debug mode currently not available for Windows");
10312 #else
10313 struct sigaction sigact;
10314 /* install TCC signal handlers to print debug info on fatal
10315 runtime errors */
10316 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10317 sigact.sa_sigaction = sig_error;
10318 sigemptyset(&sigact.sa_mask);
10319 sigaction(SIGFPE, &sigact, NULL);
10320 sigaction(SIGILL, &sigact, NULL);
10321 sigaction(SIGSEGV, &sigact, NULL);
10322 sigaction(SIGBUS, &sigact, NULL);
10323 sigaction(SIGABRT, &sigact, NULL);
10324 #endif
10327 #ifdef CONFIG_TCC_BCHECK
10328 if (do_bounds_check) {
10329 void (*bound_init)(void);
10331 /* set error function */
10332 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10333 "__bound_error_msg");
10335 /* XXX: use .init section so that it also work in binary ? */
10336 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10337 bound_init();
10339 #endif
10340 return (*prog_main)(argc, argv);
10343 void tcc_memstats(void)
10345 #ifdef MEM_DEBUG
10346 printf("memory in use: %d\n", mem_cur_size);
10347 #endif
10350 static void tcc_cleanup(void)
10352 int i, n;
10354 if (NULL == tcc_state)
10355 return;
10356 tcc_state = NULL;
10358 /* free -D defines */
10359 free_defines(NULL);
10361 /* free tokens */
10362 n = tok_ident - TOK_IDENT;
10363 for(i = 0; i < n; i++)
10364 tcc_free(table_ident[i]);
10365 tcc_free(table_ident);
10367 /* free sym_pools */
10368 dynarray_reset(&sym_pools, &nb_sym_pools);
10369 /* string buffer */
10370 cstr_free(&tokcstr);
10371 /* reset symbol stack */
10372 sym_free_first = NULL;
10373 /* cleanup from error/setjmp */
10374 macro_ptr = NULL;
10377 TCCState *tcc_new(void)
10379 const char *p, *r;
10380 TCCState *s;
10381 TokenSym *ts;
10382 int i, c;
10384 tcc_cleanup();
10386 s = tcc_mallocz(sizeof(TCCState));
10387 if (!s)
10388 return NULL;
10389 tcc_state = s;
10390 s->output_type = TCC_OUTPUT_MEMORY;
10392 /* init isid table */
10393 for(i=CH_EOF;i<256;i++)
10394 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
10396 /* add all tokens */
10397 table_ident = NULL;
10398 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10400 tok_ident = TOK_IDENT;
10401 p = tcc_keywords;
10402 while (*p) {
10403 r = p;
10404 for(;;) {
10405 c = *r++;
10406 if (c == '\0')
10407 break;
10409 ts = tok_alloc(p, r - p - 1);
10410 p = r;
10413 /* we add dummy defines for some special macros to speed up tests
10414 and to have working defined() */
10415 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10416 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10417 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10418 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10420 /* standard defines */
10421 tcc_define_symbol(s, "__STDC__", NULL);
10422 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10423 #if defined(TCC_TARGET_I386)
10424 tcc_define_symbol(s, "__i386__", NULL);
10425 #endif
10426 #if defined(TCC_TARGET_X86_64)
10427 tcc_define_symbol(s, "__x86_64__", NULL);
10428 #endif
10429 #if defined(TCC_TARGET_ARM)
10430 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10431 tcc_define_symbol(s, "__arm_elf__", NULL);
10432 tcc_define_symbol(s, "__arm_elf", NULL);
10433 tcc_define_symbol(s, "arm_elf", NULL);
10434 tcc_define_symbol(s, "__arm__", NULL);
10435 tcc_define_symbol(s, "__arm", NULL);
10436 tcc_define_symbol(s, "arm", NULL);
10437 tcc_define_symbol(s, "__APCS_32__", NULL);
10438 #endif
10439 #ifdef TCC_TARGET_PE
10440 tcc_define_symbol(s, "_WIN32", NULL);
10441 #else
10442 tcc_define_symbol(s, "__unix__", NULL);
10443 tcc_define_symbol(s, "__unix", NULL);
10444 #if defined(__linux)
10445 tcc_define_symbol(s, "__linux__", NULL);
10446 tcc_define_symbol(s, "__linux", NULL);
10447 #endif
10448 #endif
10449 /* tiny C specific defines */
10450 tcc_define_symbol(s, "__TINYC__", NULL);
10452 /* tiny C & gcc defines */
10453 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10454 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10455 #ifdef TCC_TARGET_PE
10456 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10457 #else
10458 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10459 #endif
10461 #ifndef TCC_TARGET_PE
10462 /* default library paths */
10463 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
10464 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
10465 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
10466 #endif
10468 /* no section zero */
10469 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10471 /* create standard sections */
10472 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10473 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10474 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10476 /* symbols are always generated for linking stage */
10477 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10478 ".strtab",
10479 ".hashtab", SHF_PRIVATE);
10480 strtab_section = symtab_section->link;
10482 /* private symbol table for dynamic symbols */
10483 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10484 ".dynstrtab",
10485 ".dynhashtab", SHF_PRIVATE);
10486 s->alacarte_link = 1;
10488 #ifdef CHAR_IS_UNSIGNED
10489 s->char_is_unsigned = 1;
10490 #endif
10491 #if defined(TCC_TARGET_PE) && 0
10492 /* XXX: currently the PE linker is not ready to support that */
10493 s->leading_underscore = 1;
10494 #endif
10496 #ifdef TCC_TARGET_X86_64
10497 s->jmp_table = NULL;
10498 #endif
10499 return s;
10502 void tcc_delete(TCCState *s1)
10504 int i;
10506 tcc_cleanup();
10508 /* free all sections */
10509 free_section(s1->dynsymtab_section);
10511 for(i = 1; i < s1->nb_sections; i++)
10512 free_section(s1->sections[i]);
10513 tcc_free(s1->sections);
10515 /* free any loaded DLLs */
10516 for ( i = 0; i < s1->nb_loaded_dlls; i++)
10518 DLLReference *ref = s1->loaded_dlls[i];
10519 if ( ref->handle )
10520 dlclose(ref->handle);
10523 /* free loaded dlls array */
10524 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10526 /* free library paths */
10527 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10529 /* free include paths */
10530 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10531 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10532 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10534 #ifdef TCC_TARGET_X86_64
10535 tcc_free(s1->jmp_table);
10536 #endif
10537 tcc_free(s1);
10540 int tcc_add_include_path(TCCState *s1, const char *pathname)
10542 char *pathname1;
10544 pathname1 = tcc_strdup(pathname);
10545 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10546 return 0;
10549 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10551 char *pathname1;
10553 pathname1 = tcc_strdup(pathname);
10554 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10555 return 0;
10558 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10560 const char *ext;
10561 ElfW(Ehdr) ehdr;
10562 int fd, ret;
10563 BufferedFile *saved_file;
10565 /* find source file type with extension */
10566 ext = tcc_fileextension(filename);
10567 if (ext[0])
10568 ext++;
10570 /* open the file */
10571 saved_file = file;
10572 file = tcc_open(s1, filename);
10573 if (!file) {
10574 if (flags & AFF_PRINT_ERROR) {
10575 error_noabort("file '%s' not found", filename);
10577 ret = -1;
10578 goto fail1;
10581 if (flags & AFF_PREPROCESS) {
10582 ret = tcc_preprocess(s1);
10583 } else if (!ext[0] || !strcmp(ext, "c")) {
10584 /* C file assumed */
10585 ret = tcc_compile(s1);
10586 } else
10587 #ifdef CONFIG_TCC_ASM
10588 if (!strcmp(ext, "S")) {
10589 /* preprocessed assembler */
10590 ret = tcc_assemble(s1, 1);
10591 } else if (!strcmp(ext, "s")) {
10592 /* non preprocessed assembler */
10593 ret = tcc_assemble(s1, 0);
10594 } else
10595 #endif
10596 #ifdef TCC_TARGET_PE
10597 if (!strcmp(ext, "def")) {
10598 ret = pe_load_def_file(s1, file->fd);
10599 } else
10600 #endif
10602 fd = file->fd;
10603 /* assume executable format: auto guess file type */
10604 ret = read(fd, &ehdr, sizeof(ehdr));
10605 lseek(fd, 0, SEEK_SET);
10606 if (ret <= 0) {
10607 error_noabort("could not read header");
10608 goto fail;
10609 } else if (ret != sizeof(ehdr)) {
10610 goto try_load_script;
10613 if (ehdr.e_ident[0] == ELFMAG0 &&
10614 ehdr.e_ident[1] == ELFMAG1 &&
10615 ehdr.e_ident[2] == ELFMAG2 &&
10616 ehdr.e_ident[3] == ELFMAG3) {
10617 file->line_num = 0; /* do not display line number if error */
10618 if (ehdr.e_type == ET_REL) {
10619 ret = tcc_load_object_file(s1, fd, 0);
10620 } else if (ehdr.e_type == ET_DYN) {
10621 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10622 #ifdef TCC_TARGET_PE
10623 ret = -1;
10624 #else
10625 void *h;
10626 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10627 if (h)
10628 ret = 0;
10629 else
10630 ret = -1;
10631 #endif
10632 } else {
10633 ret = tcc_load_dll(s1, fd, filename,
10634 (flags & AFF_REFERENCED_DLL) != 0);
10636 } else {
10637 error_noabort("unrecognized ELF file");
10638 goto fail;
10640 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10641 file->line_num = 0; /* do not display line number if error */
10642 ret = tcc_load_archive(s1, fd);
10643 } else
10644 #ifdef TCC_TARGET_COFF
10645 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10646 ret = tcc_load_coff(s1, fd);
10647 } else
10648 #endif
10649 #ifdef TCC_TARGET_PE
10650 if (pe_test_res_file(&ehdr, ret)) {
10651 ret = pe_load_res_file(s1, fd);
10652 } else
10653 #endif
10655 /* as GNU ld, consider it is an ld script if not recognized */
10656 try_load_script:
10657 ret = tcc_load_ldscript(s1);
10658 if (ret < 0) {
10659 error_noabort("unrecognized file type");
10660 goto fail;
10664 the_end:
10665 tcc_close(file);
10666 fail1:
10667 file = saved_file;
10668 return ret;
10669 fail:
10670 ret = -1;
10671 goto the_end;
10674 int tcc_add_file(TCCState *s, const char *filename)
10676 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10679 int tcc_add_library_path(TCCState *s, const char *pathname)
10681 char *pathname1;
10683 pathname1 = tcc_strdup(pathname);
10684 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10685 return 0;
10688 /* find and load a dll. Return non zero if not found */
10689 /* XXX: add '-rpath' option support ? */
10690 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10692 char buf[1024];
10693 int i;
10695 for(i = 0; i < s->nb_library_paths; i++) {
10696 snprintf(buf, sizeof(buf), "%s/%s",
10697 s->library_paths[i], filename);
10698 if (tcc_add_file_internal(s, buf, flags) == 0)
10699 return 0;
10701 return -1;
10704 /* the library name is the same as the argument of the '-l' option */
10705 int tcc_add_library(TCCState *s, const char *libraryname)
10707 char buf[1024];
10708 int i;
10710 /* first we look for the dynamic library if not static linking */
10711 if (!s->static_link) {
10712 #ifdef TCC_TARGET_PE
10713 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10714 #else
10715 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10716 #endif
10717 if (tcc_add_dll(s, buf, 0) == 0)
10718 return 0;
10721 /* then we look for the static library */
10722 for(i = 0; i < s->nb_library_paths; i++) {
10723 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10724 s->library_paths[i], libraryname);
10725 if (tcc_add_file_internal(s, buf, 0) == 0)
10726 return 0;
10728 return -1;
10731 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10733 add_elf_sym(symtab_section, val, 0,
10734 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
10735 SHN_ABS, name);
10736 return 0;
10739 int tcc_set_output_type(TCCState *s, int output_type)
10741 char buf[1024];
10743 s->output_type = output_type;
10745 if (!s->nostdinc) {
10746 /* default include paths */
10747 /* XXX: reverse order needed if -isystem support */
10748 #ifndef TCC_TARGET_PE
10749 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
10750 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
10751 #endif
10752 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10753 tcc_add_sysinclude_path(s, buf);
10754 #ifdef TCC_TARGET_PE
10755 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10756 tcc_add_sysinclude_path(s, buf);
10757 #endif
10760 /* if bound checking, then add corresponding sections */
10761 #ifdef CONFIG_TCC_BCHECK
10762 if (do_bounds_check) {
10763 /* define symbol */
10764 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10765 /* create bounds sections */
10766 bounds_section = new_section(s, ".bounds",
10767 SHT_PROGBITS, SHF_ALLOC);
10768 lbounds_section = new_section(s, ".lbounds",
10769 SHT_PROGBITS, SHF_ALLOC);
10771 #endif
10773 if (s->char_is_unsigned) {
10774 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10777 /* add debug sections */
10778 if (do_debug) {
10779 /* stab symbols */
10780 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10781 stab_section->sh_entsize = sizeof(Stab_Sym);
10782 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10783 put_elf_str(stabstr_section, "");
10784 stab_section->link = stabstr_section;
10785 /* put first entry */
10786 put_stabs("", 0, 0, 0, 0);
10789 /* add libc crt1/crti objects */
10790 #ifndef TCC_TARGET_PE
10791 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10792 !s->nostdlib) {
10793 if (output_type != TCC_OUTPUT_DLL)
10794 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10795 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10797 #endif
10799 #ifdef TCC_TARGET_PE
10800 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10801 tcc_add_library_path(s, buf);
10802 #endif
10804 return 0;
10807 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10808 #define FD_INVERT 0x0002 /* invert value before storing */
10810 typedef struct FlagDef {
10811 uint16_t offset;
10812 uint16_t flags;
10813 const char *name;
10814 } FlagDef;
10816 static const FlagDef warning_defs[] = {
10817 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10818 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10819 { offsetof(TCCState, warn_error), 0, "error" },
10820 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10821 "implicit-function-declaration" },
10824 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10825 const char *name, int value)
10827 int i;
10828 const FlagDef *p;
10829 const char *r;
10831 r = name;
10832 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10833 r += 3;
10834 value = !value;
10836 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10837 if (!strcmp(r, p->name))
10838 goto found;
10840 return -1;
10841 found:
10842 if (p->flags & FD_INVERT)
10843 value = !value;
10844 *(int *)((uint8_t *)s + p->offset) = value;
10845 return 0;
10849 /* set/reset a warning */
10850 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10852 int i;
10853 const FlagDef *p;
10855 if (!strcmp(warning_name, "all")) {
10856 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10857 if (p->flags & WD_ALL)
10858 *(int *)((uint8_t *)s + p->offset) = 1;
10860 return 0;
10861 } else {
10862 return set_flag(s, warning_defs, countof(warning_defs),
10863 warning_name, value);
10867 static const FlagDef flag_defs[] = {
10868 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10869 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10870 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10871 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10874 /* set/reset a flag */
10875 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10877 return set_flag(s, flag_defs, countof(flag_defs),
10878 flag_name, value);
10881 #if !defined(LIBTCC)
10883 static int64_t getclock_us(void)
10885 #ifdef _WIN32
10886 struct _timeb tb;
10887 _ftime(&tb);
10888 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10889 #else
10890 struct timeval tv;
10891 gettimeofday(&tv, NULL);
10892 return tv.tv_sec * 1000000LL + tv.tv_usec;
10893 #endif
10896 void help(void)
10898 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10899 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10900 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10901 " [-static] [infile1 infile2...] [-run infile args...]\n"
10902 "\n"
10903 "General options:\n"
10904 " -v display current version, increase verbosity\n"
10905 " -c compile only - generate an object file\n"
10906 " -o outfile set output filename\n"
10907 " -Bdir set tcc internal library path\n"
10908 " -bench output compilation statistics\n"
10909 " -run run compiled source\n"
10910 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10911 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10912 " -w disable all warnings\n"
10913 "Preprocessor options:\n"
10914 " -E preprocess only\n"
10915 " -Idir add include path 'dir'\n"
10916 " -Dsym[=val] define 'sym' with value 'val'\n"
10917 " -Usym undefine 'sym'\n"
10918 "Linker options:\n"
10919 " -Ldir add library path 'dir'\n"
10920 " -llib link with dynamic or static library 'lib'\n"
10921 " -shared generate a shared library\n"
10922 " -soname set name for shared library to be used at runtime\n"
10923 " -static static linking\n"
10924 " -rdynamic export all global symbols to dynamic linker\n"
10925 " -r generate (relocatable) object file\n"
10926 "Debugger options:\n"
10927 " -g generate runtime debug info\n"
10928 #ifdef CONFIG_TCC_BCHECK
10929 " -b compile with built-in memory and bounds checker (implies -g)\n"
10930 #endif
10931 " -bt N show N callers in stack traces\n"
10935 #define TCC_OPTION_HAS_ARG 0x0001
10936 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10938 typedef struct TCCOption {
10939 const char *name;
10940 uint16_t index;
10941 uint16_t flags;
10942 } TCCOption;
10944 enum {
10945 TCC_OPTION_HELP,
10946 TCC_OPTION_I,
10947 TCC_OPTION_D,
10948 TCC_OPTION_U,
10949 TCC_OPTION_L,
10950 TCC_OPTION_B,
10951 TCC_OPTION_l,
10952 TCC_OPTION_bench,
10953 TCC_OPTION_bt,
10954 TCC_OPTION_b,
10955 TCC_OPTION_g,
10956 TCC_OPTION_c,
10957 TCC_OPTION_static,
10958 TCC_OPTION_shared,
10959 TCC_OPTION_soname,
10960 TCC_OPTION_o,
10961 TCC_OPTION_r,
10962 TCC_OPTION_Wl,
10963 TCC_OPTION_W,
10964 TCC_OPTION_O,
10965 TCC_OPTION_m,
10966 TCC_OPTION_f,
10967 TCC_OPTION_nostdinc,
10968 TCC_OPTION_nostdlib,
10969 TCC_OPTION_print_search_dirs,
10970 TCC_OPTION_rdynamic,
10971 TCC_OPTION_run,
10972 TCC_OPTION_v,
10973 TCC_OPTION_w,
10974 TCC_OPTION_pipe,
10975 TCC_OPTION_E,
10978 static const TCCOption tcc_options[] = {
10979 { "h", TCC_OPTION_HELP, 0 },
10980 { "?", TCC_OPTION_HELP, 0 },
10981 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10982 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10983 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10984 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10985 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10986 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10987 { "bench", TCC_OPTION_bench, 0 },
10988 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10989 #ifdef CONFIG_TCC_BCHECK
10990 { "b", TCC_OPTION_b, 0 },
10991 #endif
10992 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10993 { "c", TCC_OPTION_c, 0 },
10994 { "static", TCC_OPTION_static, 0 },
10995 { "shared", TCC_OPTION_shared, 0 },
10996 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10997 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10998 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10999 { "rdynamic", TCC_OPTION_rdynamic, 0 },
11000 { "r", TCC_OPTION_r, 0 },
11001 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
11002 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
11003 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
11004 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
11005 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
11006 { "nostdinc", TCC_OPTION_nostdinc, 0 },
11007 { "nostdlib", TCC_OPTION_nostdlib, 0 },
11008 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
11009 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
11010 { "w", TCC_OPTION_w, 0 },
11011 { "pipe", TCC_OPTION_pipe, 0},
11012 { "E", TCC_OPTION_E, 0},
11013 { NULL },
11016 /* convert 'str' into an array of space separated strings */
11017 static int expand_args(char ***pargv, const char *str)
11019 const char *s1;
11020 char **argv, *arg;
11021 int argc, len;
11023 argc = 0;
11024 argv = NULL;
11025 for(;;) {
11026 while (is_space(*str))
11027 str++;
11028 if (*str == '\0')
11029 break;
11030 s1 = str;
11031 while (*str != '\0' && !is_space(*str))
11032 str++;
11033 len = str - s1;
11034 arg = tcc_malloc(len + 1);
11035 memcpy(arg, s1, len);
11036 arg[len] = '\0';
11037 dynarray_add((void ***)&argv, &argc, arg);
11039 *pargv = argv;
11040 return argc;
11043 static char **files;
11044 static int nb_files, nb_libraries;
11045 static int multiple_files;
11046 static int print_search_dirs;
11047 static int output_type;
11048 static int reloc_output;
11049 static const char *outfile;
11051 int parse_args(TCCState *s, int argc, char **argv)
11053 int optind;
11054 const TCCOption *popt;
11055 const char *optarg, *p1, *r1;
11056 char *r;
11058 optind = 0;
11059 while (optind < argc) {
11061 r = argv[optind++];
11062 if (r[0] != '-' || r[1] == '\0') {
11063 /* add a new file */
11064 dynarray_add((void ***)&files, &nb_files, r);
11065 if (!multiple_files) {
11066 optind--;
11067 /* argv[0] will be this file */
11068 break;
11070 } else {
11071 /* find option in table (match only the first chars */
11072 popt = tcc_options;
11073 for(;;) {
11074 p1 = popt->name;
11075 if (p1 == NULL)
11076 error("invalid option -- '%s'", r);
11077 r1 = r + 1;
11078 for(;;) {
11079 if (*p1 == '\0')
11080 goto option_found;
11081 if (*r1 != *p1)
11082 break;
11083 p1++;
11084 r1++;
11086 popt++;
11088 option_found:
11089 if (popt->flags & TCC_OPTION_HAS_ARG) {
11090 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
11091 optarg = r1;
11092 } else {
11093 if (optind >= argc)
11094 error("argument to '%s' is missing", r);
11095 optarg = argv[optind++];
11097 } else {
11098 if (*r1 != '\0')
11099 return 0;
11100 optarg = NULL;
11103 switch(popt->index) {
11104 case TCC_OPTION_HELP:
11105 return 0;
11107 case TCC_OPTION_I:
11108 if (tcc_add_include_path(s, optarg) < 0)
11109 error("too many include paths");
11110 break;
11111 case TCC_OPTION_D:
11113 char *sym, *value;
11114 sym = (char *)optarg;
11115 value = strchr(sym, '=');
11116 if (value) {
11117 *value = '\0';
11118 value++;
11120 tcc_define_symbol(s, sym, value);
11122 break;
11123 case TCC_OPTION_U:
11124 tcc_undefine_symbol(s, optarg);
11125 break;
11126 case TCC_OPTION_L:
11127 tcc_add_library_path(s, optarg);
11128 break;
11129 case TCC_OPTION_B:
11130 /* set tcc utilities path (mainly for tcc development) */
11131 tcc_lib_path = optarg;
11132 break;
11133 case TCC_OPTION_l:
11134 dynarray_add((void ***)&files, &nb_files, r);
11135 nb_libraries++;
11136 break;
11137 case TCC_OPTION_bench:
11138 do_bench = 1;
11139 break;
11140 case TCC_OPTION_bt:
11141 num_callers = atoi(optarg);
11142 break;
11143 #ifdef CONFIG_TCC_BCHECK
11144 case TCC_OPTION_b:
11145 do_bounds_check = 1;
11146 do_debug = 1;
11147 break;
11148 #endif
11149 case TCC_OPTION_g:
11150 do_debug = 1;
11151 break;
11152 case TCC_OPTION_c:
11153 multiple_files = 1;
11154 output_type = TCC_OUTPUT_OBJ;
11155 break;
11156 case TCC_OPTION_static:
11157 s->static_link = 1;
11158 break;
11159 case TCC_OPTION_shared:
11160 output_type = TCC_OUTPUT_DLL;
11161 break;
11162 case TCC_OPTION_soname:
11163 s->soname = optarg;
11164 break;
11165 case TCC_OPTION_o:
11166 multiple_files = 1;
11167 outfile = optarg;
11168 break;
11169 case TCC_OPTION_r:
11170 /* generate a .o merging several output files */
11171 reloc_output = 1;
11172 output_type = TCC_OUTPUT_OBJ;
11173 break;
11174 case TCC_OPTION_nostdinc:
11175 s->nostdinc = 1;
11176 break;
11177 case TCC_OPTION_nostdlib:
11178 s->nostdlib = 1;
11179 break;
11180 case TCC_OPTION_print_search_dirs:
11181 print_search_dirs = 1;
11182 break;
11183 case TCC_OPTION_run:
11185 int argc1;
11186 char **argv1;
11187 argc1 = expand_args(&argv1, optarg);
11188 if (argc1 > 0) {
11189 parse_args(s, argc1, argv1);
11191 multiple_files = 0;
11192 output_type = TCC_OUTPUT_MEMORY;
11194 break;
11195 case TCC_OPTION_v:
11196 do {
11197 if (0 == verbose++)
11198 printf("tcc version %s\n", TCC_VERSION);
11199 } while (*optarg++ == 'v');
11200 break;
11201 case TCC_OPTION_f:
11202 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
11203 goto unsupported_option;
11204 break;
11205 case TCC_OPTION_W:
11206 if (tcc_set_warning(s, optarg, 1) < 0 &&
11207 s->warn_unsupported)
11208 goto unsupported_option;
11209 break;
11210 case TCC_OPTION_w:
11211 s->warn_none = 1;
11212 break;
11213 case TCC_OPTION_rdynamic:
11214 s->rdynamic = 1;
11215 break;
11216 case TCC_OPTION_Wl:
11218 const char *p;
11219 if (strstart(optarg, "-Ttext,", &p)) {
11220 s->text_addr = strtoul(p, NULL, 16);
11221 s->has_text_addr = 1;
11222 } else if (strstart(optarg, "--oformat,", &p)) {
11223 if (strstart(p, "elf32-", NULL)) {
11224 s->output_format = TCC_OUTPUT_FORMAT_ELF;
11225 } else if (!strcmp(p, "binary")) {
11226 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
11227 } else
11228 #ifdef TCC_TARGET_COFF
11229 if (!strcmp(p, "coff")) {
11230 s->output_format = TCC_OUTPUT_FORMAT_COFF;
11231 } else
11232 #endif
11234 error("target %s not found", p);
11236 } else {
11237 error("unsupported linker option '%s'", optarg);
11240 break;
11241 case TCC_OPTION_E:
11242 output_type = TCC_OUTPUT_PREPROCESS;
11243 break;
11244 default:
11245 if (s->warn_unsupported) {
11246 unsupported_option:
11247 warning("unsupported option '%s'", r);
11249 break;
11253 return optind + 1;
11256 int main(int argc, char **argv)
11258 int i;
11259 TCCState *s;
11260 int nb_objfiles, ret, optind;
11261 char objfilename[1024];
11262 int64_t start_time = 0;
11264 #ifdef _WIN32
11265 tcc_lib_path = w32_tcc_lib_path();
11266 #endif
11268 s = tcc_new();
11269 output_type = TCC_OUTPUT_EXE;
11270 outfile = NULL;
11271 multiple_files = 1;
11272 files = NULL;
11273 nb_files = 0;
11274 nb_libraries = 0;
11275 reloc_output = 0;
11276 print_search_dirs = 0;
11277 ret = 0;
11279 optind = parse_args(s, argc - 1, argv + 1);
11280 if (print_search_dirs) {
11281 /* enough for Linux kernel */
11282 printf("install: %s/\n", tcc_lib_path);
11283 return 0;
11285 if (optind == 0 || nb_files == 0) {
11286 if (optind && verbose)
11287 return 0;
11288 help();
11289 return 1;
11292 nb_objfiles = nb_files - nb_libraries;
11294 /* if outfile provided without other options, we output an
11295 executable */
11296 if (outfile && output_type == TCC_OUTPUT_MEMORY)
11297 output_type = TCC_OUTPUT_EXE;
11299 /* check -c consistency : only single file handled. XXX: checks file type */
11300 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
11301 /* accepts only a single input file */
11302 if (nb_objfiles != 1)
11303 error("cannot specify multiple files with -c");
11304 if (nb_libraries != 0)
11305 error("cannot specify libraries with -c");
11309 if (output_type == TCC_OUTPUT_PREPROCESS) {
11310 if (!outfile) {
11311 s->outfile = stdout;
11312 } else {
11313 s->outfile = fopen(outfile, "w");
11314 if (!s->outfile)
11315 error("could not open '%s", outfile);
11317 } else if (output_type != TCC_OUTPUT_MEMORY) {
11318 if (!outfile) {
11319 /* compute default outfile name */
11320 char *ext;
11321 const char *name =
11322 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
11323 pstrcpy(objfilename, sizeof(objfilename), name);
11324 ext = tcc_fileextension(objfilename);
11325 #ifdef TCC_TARGET_PE
11326 if (output_type == TCC_OUTPUT_DLL)
11327 strcpy(ext, ".dll");
11328 else
11329 if (output_type == TCC_OUTPUT_EXE)
11330 strcpy(ext, ".exe");
11331 else
11332 #endif
11333 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
11334 strcpy(ext, ".o");
11335 else
11336 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11337 outfile = objfilename;
11341 if (do_bench) {
11342 start_time = getclock_us();
11345 tcc_set_output_type(s, output_type);
11347 /* compile or add each files or library */
11348 for(i = 0; i < nb_files && ret == 0; i++) {
11349 const char *filename;
11351 filename = files[i];
11352 if (output_type == TCC_OUTPUT_PREPROCESS) {
11353 if (tcc_add_file_internal(s, filename,
11354 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11355 ret = 1;
11356 } else if (filename[0] == '-' && filename[1]) {
11357 if (tcc_add_library(s, filename + 2) < 0)
11358 error("cannot find %s", filename);
11359 } else {
11360 if (1 == verbose)
11361 printf("-> %s\n", filename);
11362 if (tcc_add_file(s, filename) < 0)
11363 ret = 1;
11367 /* free all files */
11368 tcc_free(files);
11370 if (ret)
11371 goto the_end;
11373 if (do_bench) {
11374 double total_time;
11375 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11376 if (total_time < 0.001)
11377 total_time = 0.001;
11378 if (total_bytes < 1)
11379 total_bytes = 1;
11380 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11381 tok_ident - TOK_IDENT, total_lines, total_bytes,
11382 total_time, (int)(total_lines / total_time),
11383 total_bytes / total_time / 1000000.0);
11386 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11387 if (outfile)
11388 fclose(s->outfile);
11389 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11390 ret = tcc_run(s, argc - optind, argv + optind);
11391 } else
11392 ret = tcc_output_file(s, outfile) ? 1 : 0;
11393 the_end:
11394 /* XXX: cannot do it with bound checking because of the malloc hooks */
11395 if (!do_bounds_check)
11396 tcc_delete(s);
11398 #ifdef MEM_DEBUG
11399 if (do_bench) {
11400 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11402 #endif
11403 return ret;
11406 #endif