string_test: we should always use 'unsigned int' for b
[tinycc.git] / tcc.c
blob831ffd55630f7667cea8442430a3d727cb83f39a
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define _GNU_SOURCE
21 #include "config.h"
23 #ifdef CONFIG_TCCBOOT
25 #include "tccboot.h"
26 #define CONFIG_TCC_STATIC
28 #else
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <math.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <fcntl.h>
39 #include <setjmp.h>
40 #include <time.h>
41 #ifdef _WIN32
42 #include <sys/timeb.h>
43 #include <windows.h>
44 #endif
45 #ifndef _WIN32
46 #include <sys/time.h>
47 #include <sys/ucontext.h>
48 #include <sys/mman.h>
49 #endif
51 #endif /* !CONFIG_TCCBOOT */
53 #ifndef PAGESIZE
54 #define PAGESIZE 4096
55 #endif
57 #include "elf.h"
58 #include "stab.h"
60 #ifndef O_BINARY
61 #define O_BINARY 0
62 #endif
64 #include "libtcc.h"
66 /* parser debug */
67 //#define PARSE_DEBUG
68 /* preprocessor debug */
69 //#define PP_DEBUG
70 /* include file debug */
71 //#define INC_DEBUG
73 //#define MEM_DEBUG
75 /* assembler debug */
76 //#define ASM_DEBUG
78 /* target selection */
79 //#define TCC_TARGET_I386 /* i386 code generator */
80 //#define TCC_TARGET_ARM /* ARMv4 code generator */
81 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
82 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
84 /* default target is I386 */
85 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
86 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
87 #define TCC_TARGET_I386
88 #endif
90 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
91 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
92 #define CONFIG_TCC_BCHECK /* enable bound checking code */
93 #endif
95 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
96 #define CONFIG_TCC_STATIC
97 #endif
99 /* define it to include assembler support */
100 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67) && \
101 !defined(TCC_TARGET_X86_64)
102 #define CONFIG_TCC_ASM
103 #endif
105 /* object format selection */
106 #if defined(TCC_TARGET_C67)
107 #define TCC_TARGET_COFF
108 #endif
110 #define FALSE 0
111 #define false 0
112 #define TRUE 1
113 #define true 1
114 typedef int BOOL;
116 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
117 executables or dlls */
118 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
120 #define INCLUDE_STACK_SIZE 32
121 #define IFDEF_STACK_SIZE 64
122 #define VSTACK_SIZE 256
123 #define STRING_MAX_SIZE 1024
124 #define PACK_STACK_SIZE 8
126 #define TOK_HASH_SIZE 8192 /* must be a power of two */
127 #define TOK_ALLOC_INCR 512 /* must be a power of two */
128 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
130 /* token symbol management */
131 typedef struct TokenSym {
132 struct TokenSym *hash_next;
133 struct Sym *sym_define; /* direct pointer to define */
134 struct Sym *sym_label; /* direct pointer to label */
135 struct Sym *sym_struct; /* direct pointer to structure */
136 struct Sym *sym_identifier; /* direct pointer to identifier */
137 int tok; /* token number */
138 int len;
139 char str[1];
140 } TokenSym;
142 #ifdef TCC_TARGET_PE
143 typedef unsigned short nwchar_t;
144 #else
145 typedef int nwchar_t;
146 #endif
148 typedef struct CString {
149 int size; /* size in bytes */
150 void *data; /* either 'char *' or 'nwchar_t *' */
151 int size_allocated;
152 void *data_allocated; /* if non NULL, data has been malloced */
153 } CString;
155 /* type definition */
156 typedef struct CType {
157 int t;
158 struct Sym *ref;
159 } CType;
161 /* constant value */
162 typedef union CValue {
163 long double ld;
164 double d;
165 float f;
166 int i;
167 unsigned int ui;
168 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
169 long long ll;
170 unsigned long long ull;
171 struct CString *cstr;
172 void *ptr;
173 int tab[1];
174 } CValue;
176 /* value on stack */
177 typedef struct SValue {
178 CType type; /* type */
179 unsigned short r; /* register + flags */
180 unsigned short r2; /* second register, used for 'long long'
181 type. If not used, set to VT_CONST */
182 CValue c; /* constant, if VT_CONST */
183 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
184 } SValue;
186 /* symbol management */
187 typedef struct Sym {
188 int v; /* symbol token */
189 long r; /* associated register */
190 long c; /* associated number */
191 CType type; /* associated type */
192 struct Sym *next; /* next related symbol */
193 struct Sym *prev; /* prev symbol in stack */
194 struct Sym *prev_tok; /* previous symbol for this token */
195 } Sym;
197 /* section definition */
198 /* XXX: use directly ELF structure for parameters ? */
199 /* special flag to indicate that the section should not be linked to
200 the other ones */
201 #define SHF_PRIVATE 0x80000000
203 typedef struct Section {
204 unsigned long data_offset; /* current data offset */
205 unsigned char *data; /* section data */
206 unsigned long data_allocated; /* used for realloc() handling */
207 int sh_name; /* elf section name (only used during output) */
208 int sh_num; /* elf section number */
209 int sh_type; /* elf section type */
210 int sh_flags; /* elf section flags */
211 int sh_info; /* elf section info */
212 int sh_addralign; /* elf section alignment */
213 int sh_entsize; /* elf entry size */
214 unsigned long sh_size; /* section size (only used during output) */
215 unsigned long sh_addr; /* address at which the section is relocated */
216 unsigned long sh_offset; /* file offset */
217 int nb_hashed_syms; /* used to resize the hash table */
218 struct Section *link; /* link to another section */
219 struct Section *reloc; /* corresponding section for relocation, if any */
220 struct Section *hash; /* hash table for symbols */
221 struct Section *next;
222 char name[1]; /* section name */
223 } Section;
225 typedef struct DLLReference {
226 int level;
227 void *handle;
228 char name[1];
229 } DLLReference;
231 /* GNUC attribute definition */
232 typedef struct AttributeDef {
233 int aligned;
234 int packed;
235 Section *section;
236 int func_attr; /* calling convention, exports, ... */
237 } AttributeDef;
239 /* -------------------------------------------------- */
240 /* gr: wrappers for casting sym->r for other purposes */
241 typedef struct {
242 unsigned
243 func_call : 8,
244 func_args : 8,
245 func_export : 1;
246 } func_attr_t;
248 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
249 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
250 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
251 #define INLINE_DEF(r) (*(int **)&(r))
252 /* -------------------------------------------------- */
254 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
255 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
256 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
258 /* stored in 'Sym.c' field */
259 #define FUNC_NEW 1 /* ansi function prototype */
260 #define FUNC_OLD 2 /* old function prototype */
261 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
263 /* stored in 'Sym.r' field */
264 #define FUNC_CDECL 0 /* standard c call */
265 #define FUNC_STDCALL 1 /* pascal c call */
266 #define FUNC_FASTCALL1 2 /* first param in %eax */
267 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
268 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
269 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
271 /* field 'Sym.t' for macros */
272 #define MACRO_OBJ 0 /* object like macro */
273 #define MACRO_FUNC 1 /* function like macro */
275 /* field 'Sym.r' for C labels */
276 #define LABEL_DEFINED 0 /* label is defined */
277 #define LABEL_FORWARD 1 /* label is forward defined */
278 #define LABEL_DECLARED 2 /* label is declared but never used */
280 /* type_decl() types */
281 #define TYPE_ABSTRACT 1 /* type without variable */
282 #define TYPE_DIRECT 2 /* type with variable */
284 #define IO_BUF_SIZE 8192
286 typedef struct BufferedFile {
287 uint8_t *buf_ptr;
288 uint8_t *buf_end;
289 int fd;
290 int line_num; /* current line number - here to simplify code */
291 int ifndef_macro; /* #ifndef macro / #endif search */
292 int ifndef_macro_saved; /* saved ifndef_macro */
293 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
294 char inc_type; /* type of include */
295 char inc_filename[512]; /* filename specified by the user */
296 char filename[1024]; /* current filename - here to simplify code */
297 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
298 } BufferedFile;
300 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
301 #define CH_EOF (-1) /* end of file */
303 /* parsing state (used to save parser state to reparse part of the
304 source several times) */
305 typedef struct ParseState {
306 int *macro_ptr;
307 int line_num;
308 int tok;
309 CValue tokc;
310 } ParseState;
312 /* used to record tokens */
313 typedef struct TokenString {
314 int *str;
315 int len;
316 int allocated_len;
317 int last_line_num;
318 } TokenString;
320 /* include file cache, used to find files faster and also to eliminate
321 inclusion if the include file is protected by #ifndef ... #endif */
322 typedef struct CachedInclude {
323 int ifndef_macro;
324 int hash_next; /* -1 if none */
325 char type; /* '"' or '>' to give include type */
326 char filename[1]; /* path specified in #include */
327 } CachedInclude;
329 #define CACHED_INCLUDES_HASH_SIZE 512
331 /* parser */
332 static struct BufferedFile *file;
333 static int ch, tok;
334 static CValue tokc;
335 static CString tokcstr; /* current parsed string, if any */
336 /* additional informations about token */
337 static int tok_flags;
338 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
339 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
340 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
341 #define TOK_FLAG_EOF 0x0008 /* end of file */
343 static int *macro_ptr, *macro_ptr_allocated;
344 static int *unget_saved_macro_ptr;
345 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
346 static int unget_buffer_enabled;
347 static int parse_flags;
348 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
349 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
350 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
351 token. line feed is also
352 returned at eof */
353 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
355 static Section *text_section, *data_section, *bss_section; /* predefined sections */
356 static Section *cur_text_section; /* current section where function code is
357 generated */
358 #ifdef CONFIG_TCC_ASM
359 static Section *last_text_section; /* to handle .previous asm directive */
360 #endif
361 /* bound check related sections */
362 static Section *bounds_section; /* contains global data bound description */
363 static Section *lbounds_section; /* contains local data bound description */
364 /* symbol sections */
365 static Section *symtab_section, *strtab_section;
367 /* debug sections */
368 static Section *stab_section, *stabstr_section;
370 /* loc : local variable index
371 ind : output code index
372 rsym: return symbol
373 anon_sym: anonymous symbol index
375 static int rsym, anon_sym, ind, loc;
376 /* expression generation modifiers */
377 static int const_wanted; /* true if constant wanted */
378 static int nocode_wanted; /* true if no code generation wanted for an expression */
379 static int global_expr; /* true if compound literals must be allocated
380 globally (used during initializers parsing */
381 static CType func_vt; /* current function return type (used by return
382 instruction) */
383 static int func_vc;
384 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
385 static int tok_ident;
386 static TokenSym **table_ident;
387 static TokenSym *hash_ident[TOK_HASH_SIZE];
388 static char token_buf[STRING_MAX_SIZE + 1];
389 static char *funcname;
390 static Sym *global_stack, *local_stack;
391 static Sym *define_stack;
392 static Sym *global_label_stack, *local_label_stack;
393 /* symbol allocator */
394 #define SYM_POOL_NB (8192 / sizeof(Sym))
395 static Sym *sym_free_first;
396 static void **sym_pools;
397 static int nb_sym_pools;
399 static SValue vstack[VSTACK_SIZE], *vtop;
400 /* some predefined types */
401 static CType char_pointer_type, func_old_type, int_type;
402 /* true if isid(c) || isnum(c) */
403 static unsigned char isidnum_table[256-CH_EOF];
405 /* display some information during compilation */
406 static int verbose = 0;
408 /* compile with debug symbol (and use them if error during execution) */
409 static int do_debug = 0;
411 /* compile with built-in memory and bounds checker */
412 static int do_bounds_check = 0;
414 /* display benchmark infos */
415 #if !defined(LIBTCC)
416 static int do_bench = 0;
417 #endif
418 static int total_lines;
419 static int total_bytes;
421 /* use GNU C extensions */
422 static int gnu_ext = 1;
424 /* use Tiny C extensions */
425 static int tcc_ext = 1;
427 /* max number of callers shown if error */
428 static int num_callers = 6;
429 static const char **rt_bound_error_msg;
431 /* XXX: get rid of this ASAP */
432 static struct TCCState *tcc_state;
434 /* give the path of the tcc libraries */
435 static const char *tcc_lib_path = CONFIG_TCCDIR;
437 struct TCCState {
438 int output_type;
440 BufferedFile **include_stack_ptr;
441 int *ifdef_stack_ptr;
443 /* include file handling */
444 char **include_paths;
445 int nb_include_paths;
446 char **sysinclude_paths;
447 int nb_sysinclude_paths;
448 CachedInclude **cached_includes;
449 int nb_cached_includes;
451 char **library_paths;
452 int nb_library_paths;
454 /* array of all loaded dlls (including those referenced by loaded
455 dlls) */
456 DLLReference **loaded_dlls;
457 int nb_loaded_dlls;
459 /* sections */
460 Section **sections;
461 int nb_sections; /* number of sections, including first dummy section */
463 /* got handling */
464 Section *got;
465 Section *plt;
466 unsigned long *got_offsets;
467 int nb_got_offsets;
468 /* give the correspondance from symtab indexes to dynsym indexes */
469 int *symtab_to_dynsym;
471 /* temporary dynamic symbol sections (for dll loading) */
472 Section *dynsymtab_section;
473 /* exported dynamic symbol section */
474 Section *dynsym;
476 int nostdinc; /* if true, no standard headers are added */
477 int nostdlib; /* if true, no standard libraries are added */
479 int nocommon; /* if true, do not use common symbols for .bss data */
481 /* if true, static linking is performed */
482 int static_link;
484 /* soname as specified on the command line (-soname) */
485 const char *soname;
487 /* if true, all symbols are exported */
488 int rdynamic;
490 /* if true, only link in referenced objects from archive */
491 int alacarte_link;
493 /* address of text section */
494 unsigned long text_addr;
495 int has_text_addr;
497 /* output format, see TCC_OUTPUT_FORMAT_xxx */
498 int output_format;
500 /* C language options */
501 int char_is_unsigned;
502 int leading_underscore;
504 /* warning switches */
505 int warn_write_strings;
506 int warn_unsupported;
507 int warn_error;
508 int warn_none;
509 int warn_implicit_function_declaration;
511 /* error handling */
512 void *error_opaque;
513 void (*error_func)(void *opaque, const char *msg);
514 int error_set_jmp_enabled;
515 jmp_buf error_jmp_buf;
516 int nb_errors;
518 /* tiny assembler state */
519 Sym *asm_labels;
521 /* see include_stack_ptr */
522 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
524 /* see ifdef_stack_ptr */
525 int ifdef_stack[IFDEF_STACK_SIZE];
527 /* see cached_includes */
528 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
530 /* pack stack */
531 int pack_stack[PACK_STACK_SIZE];
532 int *pack_stack_ptr;
534 /* output file for preprocessing */
535 FILE *outfile;
537 #ifdef TCC_TARGET_X86_64
538 /* buffer to store jump tables */
539 char *jmp_table;
540 int jmp_table_num;
541 #endif
544 /* The current value can be: */
545 #define VT_VALMASK 0x00ff
546 #define VT_CONST 0x00f0 /* constant in vc
547 (must be first non register value) */
548 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
549 #define VT_LOCAL 0x00f2 /* offset on stack */
550 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
551 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
552 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
553 #define VT_LVAL 0x0100 /* var is an lvalue */
554 #define VT_SYM 0x0200 /* a symbol value is added */
555 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
556 char/short stored in integer registers) */
557 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
558 dereferencing value */
559 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
560 bounding function call point is in vc */
561 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
562 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
563 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
564 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
566 /* types */
567 #define VT_INT 0 /* integer type */
568 #define VT_BYTE 1 /* signed byte type */
569 #define VT_SHORT 2 /* short type */
570 #define VT_VOID 3 /* void type */
571 #define VT_PTR 4 /* pointer */
572 #define VT_ENUM 5 /* enum definition */
573 #define VT_FUNC 6 /* function type */
574 #define VT_STRUCT 7 /* struct/union definition */
575 #define VT_FLOAT 8 /* IEEE float */
576 #define VT_DOUBLE 9 /* IEEE double */
577 #define VT_LDOUBLE 10 /* IEEE long double */
578 #define VT_BOOL 11 /* ISOC99 boolean type */
579 #define VT_LLONG 12 /* 64 bit integer */
580 #define VT_LONG 13 /* long integer (NEVER USED as type, only
581 during parsing) */
582 #define VT_BTYPE 0x000f /* mask for basic type */
583 #define VT_UNSIGNED 0x0010 /* unsigned type */
584 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
585 #define VT_BITFIELD 0x0040 /* bitfield modifier */
586 #define VT_CONSTANT 0x0800 /* const modifier */
587 #define VT_VOLATILE 0x1000 /* volatile modifier */
588 #define VT_SIGNED 0x2000 /* signed type */
590 /* storage */
591 #define VT_EXTERN 0x00000080 /* extern definition */
592 #define VT_STATIC 0x00000100 /* static variable */
593 #define VT_TYPEDEF 0x00000200 /* typedef definition */
594 #define VT_INLINE 0x00000400 /* inline definition */
596 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
598 /* type mask (except storage) */
599 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
600 #define VT_TYPE (~(VT_STORAGE))
602 /* token values */
604 /* warning: the following compare tokens depend on i386 asm code */
605 #define TOK_ULT 0x92
606 #define TOK_UGE 0x93
607 #define TOK_EQ 0x94
608 #define TOK_NE 0x95
609 #define TOK_ULE 0x96
610 #define TOK_UGT 0x97
611 #define TOK_Nset 0x98
612 #define TOK_Nclear 0x99
613 #define TOK_LT 0x9c
614 #define TOK_GE 0x9d
615 #define TOK_LE 0x9e
616 #define TOK_GT 0x9f
618 #define TOK_LAND 0xa0
619 #define TOK_LOR 0xa1
621 #define TOK_DEC 0xa2
622 #define TOK_MID 0xa3 /* inc/dec, to void constant */
623 #define TOK_INC 0xa4
624 #define TOK_UDIV 0xb0 /* unsigned division */
625 #define TOK_UMOD 0xb1 /* unsigned modulo */
626 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
627 #define TOK_CINT 0xb3 /* number in tokc */
628 #define TOK_CCHAR 0xb4 /* char constant in tokc */
629 #define TOK_STR 0xb5 /* pointer to string in tokc */
630 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
631 #define TOK_LCHAR 0xb7
632 #define TOK_LSTR 0xb8
633 #define TOK_CFLOAT 0xb9 /* float constant */
634 #define TOK_LINENUM 0xba /* line number info */
635 #define TOK_CDOUBLE 0xc0 /* double constant */
636 #define TOK_CLDOUBLE 0xc1 /* long double constant */
637 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
638 #define TOK_ADDC1 0xc3 /* add with carry generation */
639 #define TOK_ADDC2 0xc4 /* add with carry use */
640 #define TOK_SUBC1 0xc5 /* add with carry generation */
641 #define TOK_SUBC2 0xc6 /* add with carry use */
642 #define TOK_CUINT 0xc8 /* unsigned int constant */
643 #define TOK_CLLONG 0xc9 /* long long constant */
644 #define TOK_CULLONG 0xca /* unsigned long long constant */
645 #define TOK_ARROW 0xcb
646 #define TOK_DOTS 0xcc /* three dots */
647 #define TOK_SHR 0xcd /* unsigned shift right */
648 #define TOK_PPNUM 0xce /* preprocessor number */
650 #define TOK_SHL 0x01 /* shift left */
651 #define TOK_SAR 0x02 /* signed shift right */
653 /* assignement operators : normal operator or 0x80 */
654 #define TOK_A_MOD 0xa5
655 #define TOK_A_AND 0xa6
656 #define TOK_A_MUL 0xaa
657 #define TOK_A_ADD 0xab
658 #define TOK_A_SUB 0xad
659 #define TOK_A_DIV 0xaf
660 #define TOK_A_XOR 0xde
661 #define TOK_A_OR 0xfc
662 #define TOK_A_SHL 0x81
663 #define TOK_A_SAR 0x82
665 #ifndef offsetof
666 #define offsetof(type, field) ((size_t) &((type *)0)->field)
667 #endif
669 #ifndef countof
670 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
671 #endif
673 /* WARNING: the content of this string encodes token numbers */
674 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";
676 #define TOK_EOF (-1) /* end of file */
677 #define TOK_LINEFEED 10 /* line feed */
679 /* all identificators and strings have token above that */
680 #define TOK_IDENT 256
682 /* only used for i386 asm opcodes definitions */
683 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
685 #define DEF_BWL(x) \
686 DEF(TOK_ASM_ ## x ## b, #x "b") \
687 DEF(TOK_ASM_ ## x ## w, #x "w") \
688 DEF(TOK_ASM_ ## x ## l, #x "l") \
689 DEF(TOK_ASM_ ## x, #x)
691 #define DEF_WL(x) \
692 DEF(TOK_ASM_ ## x ## w, #x "w") \
693 DEF(TOK_ASM_ ## x ## l, #x "l") \
694 DEF(TOK_ASM_ ## x, #x)
696 #define DEF_FP1(x) \
697 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
698 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
699 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
700 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
702 #define DEF_FP(x) \
703 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
704 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
705 DEF_FP1(x)
707 #define DEF_ASMTEST(x) \
708 DEF_ASM(x ## o) \
709 DEF_ASM(x ## no) \
710 DEF_ASM(x ## b) \
711 DEF_ASM(x ## c) \
712 DEF_ASM(x ## nae) \
713 DEF_ASM(x ## nb) \
714 DEF_ASM(x ## nc) \
715 DEF_ASM(x ## ae) \
716 DEF_ASM(x ## e) \
717 DEF_ASM(x ## z) \
718 DEF_ASM(x ## ne) \
719 DEF_ASM(x ## nz) \
720 DEF_ASM(x ## be) \
721 DEF_ASM(x ## na) \
722 DEF_ASM(x ## nbe) \
723 DEF_ASM(x ## a) \
724 DEF_ASM(x ## s) \
725 DEF_ASM(x ## ns) \
726 DEF_ASM(x ## p) \
727 DEF_ASM(x ## pe) \
728 DEF_ASM(x ## np) \
729 DEF_ASM(x ## po) \
730 DEF_ASM(x ## l) \
731 DEF_ASM(x ## nge) \
732 DEF_ASM(x ## nl) \
733 DEF_ASM(x ## ge) \
734 DEF_ASM(x ## le) \
735 DEF_ASM(x ## ng) \
736 DEF_ASM(x ## nle) \
737 DEF_ASM(x ## g)
739 #define TOK_ASM_int TOK_INT
741 enum tcc_token {
742 TOK_LAST = TOK_IDENT - 1,
743 #define DEF(id, str) id,
744 #include "tcctok.h"
745 #undef DEF
748 static const char tcc_keywords[] =
749 #define DEF(id, str) str "\0"
750 #include "tcctok.h"
751 #undef DEF
754 #define TOK_UIDENT TOK_DEFINE
756 #ifdef _WIN32
757 #define snprintf _snprintf
758 #define vsnprintf _vsnprintf
759 #ifndef __GNUC__
760 #define strtold (long double)strtod
761 #define strtof (float)strtod
762 #define strtoll (long long)strtol
763 #endif
764 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
765 || defined(__OpenBSD__)
766 /* currently incorrect */
767 long double strtold(const char *nptr, char **endptr)
769 return (long double)strtod(nptr, endptr);
771 float strtof(const char *nptr, char **endptr)
773 return (float)strtod(nptr, endptr);
775 #else
776 /* XXX: need to define this to use them in non ISOC99 context */
777 extern float strtof (const char *__nptr, char **__endptr);
778 extern long double strtold (const char *__nptr, char **__endptr);
779 #endif
781 static char *pstrcpy(char *buf, int buf_size, const char *s);
782 static char *pstrcat(char *buf, int buf_size, const char *s);
783 static char *tcc_basename(const char *name);
784 static char *tcc_fileextension (const char *p);
786 static void next(void);
787 static void next_nomacro(void);
788 static void parse_expr_type(CType *type);
789 static void expr_type(CType *type);
790 static void unary_type(CType *type);
791 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
792 int case_reg, int is_expr);
793 static int expr_const(void);
794 static void expr_eq(void);
795 static void gexpr(void);
796 static void gen_inline_functions(void);
797 static void decl(int l);
798 static void decl_initializer(CType *type, Section *sec, unsigned long c,
799 int first, int size_only);
800 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
801 int has_init, int v, int scope);
802 int gv(int rc);
803 void gv2(int rc1, int rc2);
804 void move_reg(int r, int s);
805 void save_regs(int n);
806 void save_reg(int r);
807 void vpop(void);
808 void vswap(void);
809 void vdup(void);
810 int get_reg(int rc);
811 int get_reg_ex(int rc,int rc2);
813 struct macro_level {
814 struct macro_level *prev;
815 int *p;
818 static void macro_subst(TokenString *tok_str, Sym **nested_list,
819 const int *macro_str, struct macro_level **can_read_stream);
820 void gen_op(int op);
821 void force_charshort_cast(int t);
822 static void gen_cast(CType *type);
823 void vstore(void);
824 static Sym *sym_find(int v);
825 static Sym *sym_push(int v, CType *type, int r, int c);
827 /* type handling */
828 static int type_size(CType *type, int *a);
829 static inline CType *pointed_type(CType *type);
830 static int pointed_size(CType *type);
831 static int lvalue_type(int t);
832 static int parse_btype(CType *type, AttributeDef *ad);
833 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
834 static int compare_types(CType *type1, CType *type2, int unqualified);
835 static int is_compatible_types(CType *type1, CType *type2);
836 static int is_compatible_parameter_types(CType *type1, CType *type2);
838 int ieee_finite(double d);
839 void error(const char *fmt, ...);
840 void vpushi(int v);
841 void vrott(int n);
842 void vnrott(int n);
843 void lexpand_nr(void);
844 static void vpush_global_sym(CType *type, int v);
845 void vset(CType *type, int r, int v);
846 void type_to_str(char *buf, int buf_size,
847 CType *type, const char *varstr);
848 char *get_tok_str(int v, CValue *cv);
849 static Sym *get_sym_ref(CType *type, Section *sec,
850 unsigned long offset, unsigned long size);
851 static Sym *external_global_sym(int v, CType *type, int r);
853 /* section generation */
854 static void section_realloc(Section *sec, unsigned long new_size);
855 static void *section_ptr_add(Section *sec, unsigned long size);
856 static void put_extern_sym(Sym *sym, Section *section,
857 unsigned long value, unsigned long size);
858 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
859 static int put_elf_str(Section *s, const char *sym);
860 static int put_elf_sym(Section *s,
861 unsigned long value, unsigned long size,
862 int info, int other, int shndx, const char *name);
863 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
864 int info, int other, int sh_num, const char *name);
865 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
866 int type, int symbol);
867 static void put_stabs(const char *str, int type, int other, int desc,
868 unsigned long value);
869 static void put_stabs_r(const char *str, int type, int other, int desc,
870 unsigned long value, Section *sec, int sym_index);
871 static void put_stabn(int type, int other, int desc, int value);
872 static void put_stabd(int type, int other, int desc);
873 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
875 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
876 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
877 #define AFF_PREPROCESS 0x0004 /* preprocess file */
878 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
880 /* tcccoff.c */
881 int tcc_output_coff(TCCState *s1, FILE *f);
883 /* tccpe.c */
884 void *resolve_sym(TCCState *s1, const char *sym, int type);
885 int pe_load_def_file(struct TCCState *s1, int fd);
886 int pe_test_res_file(void *v, int size);
887 int pe_load_res_file(struct TCCState *s1, int fd);
888 void pe_add_runtime(struct TCCState *s1);
889 void pe_guess_outfile(char *objfilename, int output_type);
890 int pe_output_file(struct TCCState *s1, const char *filename);
892 /* tccasm.c */
894 #ifdef CONFIG_TCC_ASM
896 typedef struct ExprValue {
897 uint32_t v;
898 Sym *sym;
899 } ExprValue;
901 #define MAX_ASM_OPERANDS 30
903 typedef struct ASMOperand {
904 int id; /* GCC 3 optionnal identifier (0 if number only supported */
905 char *constraint;
906 char asm_str[16]; /* computed asm string for operand */
907 SValue *vt; /* C value of the expression */
908 int ref_index; /* if >= 0, gives reference to a output constraint */
909 int input_index; /* if >= 0, gives reference to an input constraint */
910 int priority; /* priority, used to assign registers */
911 int reg; /* if >= 0, register number used for this operand */
912 int is_llong; /* true if double register value */
913 int is_memory; /* true if memory operand */
914 int is_rw; /* for '+' modifier */
915 } ASMOperand;
917 static void asm_expr(TCCState *s1, ExprValue *pe);
918 static int asm_int_expr(TCCState *s1);
919 static int find_constraint(ASMOperand *operands, int nb_operands,
920 const char *name, const char **pp);
922 static int tcc_assemble(TCCState *s1, int do_preprocess);
924 #endif
926 static void asm_instr(void);
927 static void asm_global_instr(void);
929 /* true if float/double/long double type */
930 static inline int is_float(int t)
932 int bt;
933 bt = t & VT_BTYPE;
934 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
937 #ifdef TCC_TARGET_I386
938 #include "i386-gen.c"
939 #endif
941 #ifdef TCC_TARGET_ARM
942 #include "arm-gen.c"
943 #endif
945 #ifdef TCC_TARGET_C67
946 #include "c67-gen.c"
947 #endif
949 #ifdef TCC_TARGET_X86_64
950 #include "x86_64-gen.c"
951 #endif
953 #ifdef CONFIG_TCC_STATIC
955 #define RTLD_LAZY 0x001
956 #define RTLD_NOW 0x002
957 #define RTLD_GLOBAL 0x100
958 #define RTLD_DEFAULT NULL
960 /* dummy function for profiling */
961 void *dlopen(const char *filename, int flag)
963 return NULL;
966 const char *dlerror(void)
968 return "error";
971 typedef struct TCCSyms {
972 char *str;
973 void *ptr;
974 } TCCSyms;
976 #define TCCSYM(a) { #a, &a, },
978 /* add the symbol you want here if no dynamic linking is done */
979 static TCCSyms tcc_syms[] = {
980 #if !defined(CONFIG_TCCBOOT)
981 TCCSYM(printf)
982 TCCSYM(fprintf)
983 TCCSYM(fopen)
984 TCCSYM(fclose)
985 #endif
986 { NULL, NULL },
989 void *resolve_sym(TCCState *s1, const char *symbol, int type)
991 TCCSyms *p;
992 p = tcc_syms;
993 while (p->str != NULL) {
994 if (!strcmp(p->str, symbol))
995 return p->ptr;
996 p++;
998 return NULL;
1001 #elif !defined(_WIN32)
1003 #include <dlfcn.h>
1005 void *resolve_sym(TCCState *s1, const char *sym, int type)
1007 return dlsym(RTLD_DEFAULT, sym);
1010 #endif
1012 /********************************************************/
1014 /* we use our own 'finite' function to avoid potential problems with
1015 non standard math libs */
1016 /* XXX: endianness dependent */
1017 int ieee_finite(double d)
1019 int *p = (int *)&d;
1020 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
1023 /* copy a string and truncate it. */
1024 static char *pstrcpy(char *buf, int buf_size, const char *s)
1026 char *q, *q_end;
1027 int c;
1029 if (buf_size > 0) {
1030 q = buf;
1031 q_end = buf + buf_size - 1;
1032 while (q < q_end) {
1033 c = *s++;
1034 if (c == '\0')
1035 break;
1036 *q++ = c;
1038 *q = '\0';
1040 return buf;
1043 /* strcat and truncate. */
1044 static char *pstrcat(char *buf, int buf_size, const char *s)
1046 int len;
1047 len = strlen(buf);
1048 if (len < buf_size)
1049 pstrcpy(buf + len, buf_size - len, s);
1050 return buf;
1053 #ifndef LIBTCC
1054 static int strstart(const char *str, const char *val, const char **ptr)
1056 const char *p, *q;
1057 p = str;
1058 q = val;
1059 while (*q != '\0') {
1060 if (*p != *q)
1061 return 0;
1062 p++;
1063 q++;
1065 if (ptr)
1066 *ptr = p;
1067 return 1;
1069 #endif
1071 /* extract the basename of a file */
1072 static char *tcc_basename(const char *name)
1074 char *p = strchr(name, 0);
1075 while (p > name
1076 && p[-1] != '/'
1077 #ifdef _WIN32
1078 && p[-1] != '\\'
1079 #endif
1081 --p;
1082 return p;
1085 static char *tcc_fileextension (const char *name)
1087 char *b = tcc_basename(name);
1088 char *e = strrchr(b, '.');
1089 return e ? e : strchr(b, 0);
1092 #ifdef _WIN32
1093 char *normalize_slashes(char *path)
1095 char *p;
1096 for (p = path; *p; ++p)
1097 if (*p == '\\')
1098 *p = '/';
1099 return path;
1102 char *w32_tcc_lib_path(void)
1104 /* on win32, we suppose the lib and includes are at the location
1105 of 'tcc.exe' */
1106 char path[1024], *p;
1107 GetModuleFileNameA(NULL, path, sizeof path);
1108 p = tcc_basename(normalize_slashes(strlwr(path)));
1109 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
1110 p -= 5;
1111 else if (p > path)
1112 p--;
1113 *p = 0;
1114 return strdup(path);
1116 #endif
1118 void set_pages_executable(void *ptr, unsigned long length)
1120 #ifdef _WIN32
1121 unsigned long old_protect;
1122 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
1123 #else
1124 unsigned long start, end;
1125 start = (unsigned long)ptr & ~(PAGESIZE - 1);
1126 end = (unsigned long)ptr + length;
1127 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
1128 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
1129 #endif
1132 /* memory management */
1133 #ifdef MEM_DEBUG
1134 int mem_cur_size;
1135 int mem_max_size;
1136 unsigned malloc_usable_size(void*);
1137 #endif
1139 static inline void tcc_free(void *ptr)
1141 #ifdef MEM_DEBUG
1142 mem_cur_size -= malloc_usable_size(ptr);
1143 #endif
1144 free(ptr);
1147 static void *tcc_malloc(unsigned long size)
1149 void *ptr;
1150 ptr = malloc(size);
1151 if (!ptr && size)
1152 error("memory full");
1153 #ifdef MEM_DEBUG
1154 mem_cur_size += malloc_usable_size(ptr);
1155 if (mem_cur_size > mem_max_size)
1156 mem_max_size = mem_cur_size;
1157 #endif
1158 return ptr;
1161 static void *tcc_mallocz(unsigned long size)
1163 void *ptr;
1164 ptr = tcc_malloc(size);
1165 memset(ptr, 0, size);
1166 return ptr;
1169 static inline void *tcc_realloc(void *ptr, unsigned long size)
1171 void *ptr1;
1172 #ifdef MEM_DEBUG
1173 mem_cur_size -= malloc_usable_size(ptr);
1174 #endif
1175 ptr1 = realloc(ptr, size);
1176 #ifdef MEM_DEBUG
1177 /* NOTE: count not correct if alloc error, but not critical */
1178 mem_cur_size += malloc_usable_size(ptr1);
1179 if (mem_cur_size > mem_max_size)
1180 mem_max_size = mem_cur_size;
1181 #endif
1182 return ptr1;
1185 static char *tcc_strdup(const char *str)
1187 char *ptr;
1188 ptr = tcc_malloc(strlen(str) + 1);
1189 strcpy(ptr, str);
1190 return ptr;
1193 #define free(p) use_tcc_free(p)
1194 #define malloc(s) use_tcc_malloc(s)
1195 #define realloc(p, s) use_tcc_realloc(p, s)
1197 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1199 int nb, nb_alloc;
1200 void **pp;
1202 nb = *nb_ptr;
1203 pp = *ptab;
1204 /* every power of two we double array size */
1205 if ((nb & (nb - 1)) == 0) {
1206 if (!nb)
1207 nb_alloc = 1;
1208 else
1209 nb_alloc = nb * 2;
1210 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1211 if (!pp)
1212 error("memory full");
1213 *ptab = pp;
1215 pp[nb++] = data;
1216 *nb_ptr = nb;
1219 static void dynarray_reset(void *pp, int *n)
1221 void **p;
1222 for (p = *(void***)pp; *n; ++p, --*n)
1223 if (*p)
1224 tcc_free(*p);
1225 tcc_free(*(void**)pp);
1226 *(void**)pp = NULL;
1229 /* symbol allocator */
1230 static Sym *__sym_malloc(void)
1232 Sym *sym_pool, *sym, *last_sym;
1233 int i;
1235 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1236 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
1238 last_sym = sym_free_first;
1239 sym = sym_pool;
1240 for(i = 0; i < SYM_POOL_NB; i++) {
1241 sym->next = last_sym;
1242 last_sym = sym;
1243 sym++;
1245 sym_free_first = last_sym;
1246 return last_sym;
1249 static inline Sym *sym_malloc(void)
1251 Sym *sym;
1252 sym = sym_free_first;
1253 if (!sym)
1254 sym = __sym_malloc();
1255 sym_free_first = sym->next;
1256 return sym;
1259 static inline void sym_free(Sym *sym)
1261 sym->next = sym_free_first;
1262 sym_free_first = sym;
1265 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1267 Section *sec;
1269 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1270 strcpy(sec->name, name);
1271 sec->sh_type = sh_type;
1272 sec->sh_flags = sh_flags;
1273 switch(sh_type) {
1274 case SHT_HASH:
1275 case SHT_REL:
1276 case SHT_RELA:
1277 case SHT_DYNSYM:
1278 case SHT_SYMTAB:
1279 case SHT_DYNAMIC:
1280 sec->sh_addralign = 4;
1281 break;
1282 case SHT_STRTAB:
1283 sec->sh_addralign = 1;
1284 break;
1285 default:
1286 sec->sh_addralign = 32; /* default conservative alignment */
1287 break;
1290 /* only add section if not private */
1291 if (!(sh_flags & SHF_PRIVATE)) {
1292 sec->sh_num = s1->nb_sections;
1293 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1295 return sec;
1298 static void free_section(Section *s)
1300 if (s->link && (s->link->sh_flags & SHF_PRIVATE))
1301 free_section(s->link);
1302 if (s->hash && (s->hash->sh_flags & SHF_PRIVATE))
1303 s->hash->link = NULL, free_section(s->hash);
1304 tcc_free(s->data);
1305 tcc_free(s);
1308 /* realloc section and set its content to zero */
1309 static void section_realloc(Section *sec, unsigned long new_size)
1311 unsigned long size;
1312 unsigned char *data;
1314 size = sec->data_allocated;
1315 if (size == 0)
1316 size = 1;
1317 while (size < new_size)
1318 size = size * 2;
1319 data = tcc_realloc(sec->data, size);
1320 if (!data)
1321 error("memory full");
1322 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1323 sec->data = data;
1324 sec->data_allocated = size;
1327 /* reserve at least 'size' bytes in section 'sec' from
1328 sec->data_offset. */
1329 static void *section_ptr_add(Section *sec, unsigned long size)
1331 unsigned long offset, offset1;
1333 offset = sec->data_offset;
1334 offset1 = offset + size;
1335 if (offset1 > sec->data_allocated)
1336 section_realloc(sec, offset1);
1337 sec->data_offset = offset1;
1338 return sec->data + offset;
1341 /* return a reference to a section, and create it if it does not
1342 exists */
1343 Section *find_section(TCCState *s1, const char *name)
1345 Section *sec;
1346 int i;
1347 for(i = 1; i < s1->nb_sections; i++) {
1348 sec = s1->sections[i];
1349 if (!strcmp(name, sec->name))
1350 return sec;
1352 /* sections are created as PROGBITS */
1353 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1356 #define SECTION_ABS ((void *)1)
1358 /* update sym->c so that it points to an external symbol in section
1359 'section' with value 'value' */
1360 static void put_extern_sym2(Sym *sym, Section *section,
1361 unsigned long value, unsigned long size,
1362 int can_add_underscore)
1364 int sym_type, sym_bind, sh_num, info, other, attr;
1365 ElfW(Sym) *esym;
1366 const char *name;
1367 char buf1[256];
1369 if (section == NULL)
1370 sh_num = SHN_UNDEF;
1371 else if (section == SECTION_ABS)
1372 sh_num = SHN_ABS;
1373 else
1374 sh_num = section->sh_num;
1376 other = attr = 0;
1378 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1379 sym_type = STT_FUNC;
1380 #ifdef TCC_TARGET_PE
1381 if (sym->type.ref)
1382 attr = sym->type.ref->r;
1383 if (FUNC_EXPORT(attr))
1384 other |= 1;
1385 if (FUNC_CALL(attr) == FUNC_STDCALL)
1386 other |= 2;
1387 #endif
1388 } else {
1389 sym_type = STT_OBJECT;
1392 if (sym->type.t & VT_STATIC)
1393 sym_bind = STB_LOCAL;
1394 else
1395 sym_bind = STB_GLOBAL;
1397 if (!sym->c) {
1398 name = get_tok_str(sym->v, NULL);
1399 #ifdef CONFIG_TCC_BCHECK
1400 if (do_bounds_check) {
1401 char buf[32];
1403 /* XXX: avoid doing that for statics ? */
1404 /* if bound checking is activated, we change some function
1405 names by adding the "__bound" prefix */
1406 switch(sym->v) {
1407 #if 0
1408 /* XXX: we rely only on malloc hooks */
1409 case TOK_malloc:
1410 case TOK_free:
1411 case TOK_realloc:
1412 case TOK_memalign:
1413 case TOK_calloc:
1414 #endif
1415 case TOK_memcpy:
1416 case TOK_memmove:
1417 case TOK_memset:
1418 case TOK_strlen:
1419 case TOK_strcpy:
1420 case TOK__alloca:
1421 strcpy(buf, "__bound_");
1422 strcat(buf, name);
1423 name = buf;
1424 break;
1427 #endif
1429 #ifdef TCC_TARGET_PE
1430 if ((other & 2) && can_add_underscore) {
1431 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1432 name = buf1;
1433 } else
1434 #endif
1435 if (tcc_state->leading_underscore && can_add_underscore) {
1436 buf1[0] = '_';
1437 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1438 name = buf1;
1440 info = ELFW(ST_INFO)(sym_bind, sym_type);
1441 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1442 } else {
1443 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
1444 esym->st_value = value;
1445 esym->st_size = size;
1446 esym->st_shndx = sh_num;
1447 esym->st_other |= other;
1451 static void put_extern_sym(Sym *sym, Section *section,
1452 unsigned long value, unsigned long size)
1454 put_extern_sym2(sym, section, value, size, 1);
1457 /* add a new relocation entry to symbol 'sym' in section 's' */
1458 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1460 if (!sym->c)
1461 put_extern_sym(sym, NULL, 0, 0);
1462 /* now we can add ELF relocation info */
1463 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1466 static inline int isid(int c)
1468 return (c >= 'a' && c <= 'z') ||
1469 (c >= 'A' && c <= 'Z') ||
1470 c == '_';
1473 static inline int isnum(int c)
1475 return c >= '0' && c <= '9';
1478 static inline int isoct(int c)
1480 return c >= '0' && c <= '7';
1483 static inline int toup(int c)
1485 if (c >= 'a' && c <= 'z')
1486 return c - 'a' + 'A';
1487 else
1488 return c;
1491 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1493 int len;
1494 len = strlen(buf);
1495 vsnprintf(buf + len, buf_size - len, fmt, ap);
1498 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1500 va_list ap;
1501 va_start(ap, fmt);
1502 strcat_vprintf(buf, buf_size, fmt, ap);
1503 va_end(ap);
1506 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1508 char buf[2048];
1509 BufferedFile **f;
1511 buf[0] = '\0';
1512 if (file) {
1513 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1514 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1515 (*f)->filename, (*f)->line_num);
1516 if (file->line_num > 0) {
1517 strcat_printf(buf, sizeof(buf),
1518 "%s:%d: ", file->filename, file->line_num);
1519 } else {
1520 strcat_printf(buf, sizeof(buf),
1521 "%s: ", file->filename);
1523 } else {
1524 strcat_printf(buf, sizeof(buf),
1525 "tcc: ");
1527 if (is_warning)
1528 strcat_printf(buf, sizeof(buf), "warning: ");
1529 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1531 if (!s1->error_func) {
1532 /* default case: stderr */
1533 fprintf(stderr, "%s\n", buf);
1534 } else {
1535 s1->error_func(s1->error_opaque, buf);
1537 if (!is_warning || s1->warn_error)
1538 s1->nb_errors++;
1541 #ifdef LIBTCC
1542 void tcc_set_error_func(TCCState *s, void *error_opaque,
1543 void (*error_func)(void *opaque, const char *msg))
1545 s->error_opaque = error_opaque;
1546 s->error_func = error_func;
1548 #endif
1550 /* error without aborting current compilation */
1551 void error_noabort(const char *fmt, ...)
1553 TCCState *s1 = tcc_state;
1554 va_list ap;
1556 va_start(ap, fmt);
1557 error1(s1, 0, fmt, ap);
1558 va_end(ap);
1561 void error(const char *fmt, ...)
1563 TCCState *s1 = tcc_state;
1564 va_list ap;
1566 va_start(ap, fmt);
1567 error1(s1, 0, fmt, ap);
1568 va_end(ap);
1569 /* better than nothing: in some cases, we accept to handle errors */
1570 if (s1->error_set_jmp_enabled) {
1571 longjmp(s1->error_jmp_buf, 1);
1572 } else {
1573 /* XXX: eliminate this someday */
1574 exit(1);
1578 void expect(const char *msg)
1580 error("%s expected", msg);
1583 void warning(const char *fmt, ...)
1585 TCCState *s1 = tcc_state;
1586 va_list ap;
1588 if (s1->warn_none)
1589 return;
1591 va_start(ap, fmt);
1592 error1(s1, 1, fmt, ap);
1593 va_end(ap);
1596 void skip(int c)
1598 if (tok != c)
1599 error("'%c' expected", c);
1600 next();
1603 static void test_lvalue(void)
1605 if (!(vtop->r & VT_LVAL))
1606 expect("lvalue");
1609 /* allocate a new token */
1610 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1612 TokenSym *ts, **ptable;
1613 int i;
1615 if (tok_ident >= SYM_FIRST_ANOM)
1616 error("memory full");
1618 /* expand token table if needed */
1619 i = tok_ident - TOK_IDENT;
1620 if ((i % TOK_ALLOC_INCR) == 0) {
1621 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1622 if (!ptable)
1623 error("memory full");
1624 table_ident = ptable;
1627 ts = tcc_malloc(sizeof(TokenSym) + len);
1628 table_ident[i] = ts;
1629 ts->tok = tok_ident++;
1630 ts->sym_define = NULL;
1631 ts->sym_label = NULL;
1632 ts->sym_struct = NULL;
1633 ts->sym_identifier = NULL;
1634 ts->len = len;
1635 ts->hash_next = NULL;
1636 memcpy(ts->str, str, len);
1637 ts->str[len] = '\0';
1638 *pts = ts;
1639 return ts;
1642 #define TOK_HASH_INIT 1
1643 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1645 /* find a token and add it if not found */
1646 static TokenSym *tok_alloc(const char *str, int len)
1648 TokenSym *ts, **pts;
1649 int i;
1650 unsigned int h;
1652 h = TOK_HASH_INIT;
1653 for(i=0;i<len;i++)
1654 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1655 h &= (TOK_HASH_SIZE - 1);
1657 pts = &hash_ident[h];
1658 for(;;) {
1659 ts = *pts;
1660 if (!ts)
1661 break;
1662 if (ts->len == len && !memcmp(ts->str, str, len))
1663 return ts;
1664 pts = &(ts->hash_next);
1666 return tok_alloc_new(pts, str, len);
1669 /* CString handling */
1671 static void cstr_realloc(CString *cstr, int new_size)
1673 int size;
1674 void *data;
1676 size = cstr->size_allocated;
1677 if (size == 0)
1678 size = 8; /* no need to allocate a too small first string */
1679 while (size < new_size)
1680 size = size * 2;
1681 data = tcc_realloc(cstr->data_allocated, size);
1682 if (!data)
1683 error("memory full");
1684 cstr->data_allocated = data;
1685 cstr->size_allocated = size;
1686 cstr->data = data;
1689 /* add a byte */
1690 static inline void cstr_ccat(CString *cstr, int ch)
1692 int size;
1693 size = cstr->size + 1;
1694 if (size > cstr->size_allocated)
1695 cstr_realloc(cstr, size);
1696 ((unsigned char *)cstr->data)[size - 1] = ch;
1697 cstr->size = size;
1700 static void cstr_cat(CString *cstr, const char *str)
1702 int c;
1703 for(;;) {
1704 c = *str;
1705 if (c == '\0')
1706 break;
1707 cstr_ccat(cstr, c);
1708 str++;
1712 /* add a wide char */
1713 static void cstr_wccat(CString *cstr, int ch)
1715 int size;
1716 size = cstr->size + sizeof(nwchar_t);
1717 if (size > cstr->size_allocated)
1718 cstr_realloc(cstr, size);
1719 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
1720 cstr->size = size;
1723 static void cstr_new(CString *cstr)
1725 memset(cstr, 0, sizeof(CString));
1728 /* free string and reset it to NULL */
1729 static void cstr_free(CString *cstr)
1731 tcc_free(cstr->data_allocated);
1732 cstr_new(cstr);
1735 #define cstr_reset(cstr) cstr_free(cstr)
1737 /* XXX: unicode ? */
1738 static void add_char(CString *cstr, int c)
1740 if (c == '\'' || c == '\"' || c == '\\') {
1741 /* XXX: could be more precise if char or string */
1742 cstr_ccat(cstr, '\\');
1744 if (c >= 32 && c <= 126) {
1745 cstr_ccat(cstr, c);
1746 } else {
1747 cstr_ccat(cstr, '\\');
1748 if (c == '\n') {
1749 cstr_ccat(cstr, 'n');
1750 } else {
1751 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1752 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1753 cstr_ccat(cstr, '0' + (c & 7));
1758 /* XXX: buffer overflow */
1759 /* XXX: float tokens */
1760 char *get_tok_str(int v, CValue *cv)
1762 static char buf[STRING_MAX_SIZE + 1];
1763 static CString cstr_buf;
1764 CString *cstr;
1765 unsigned char *q;
1766 char *p;
1767 int i, len;
1769 /* NOTE: to go faster, we give a fixed buffer for small strings */
1770 cstr_reset(&cstr_buf);
1771 cstr_buf.data = buf;
1772 cstr_buf.size_allocated = sizeof(buf);
1773 p = buf;
1775 switch(v) {
1776 case TOK_CINT:
1777 case TOK_CUINT:
1778 /* XXX: not quite exact, but only useful for testing */
1779 sprintf(p, "%u", cv->ui);
1780 break;
1781 case TOK_CLLONG:
1782 case TOK_CULLONG:
1783 /* XXX: not quite exact, but only useful for testing */
1784 sprintf(p, "%Lu", cv->ull);
1785 break;
1786 case TOK_LCHAR:
1787 cstr_ccat(&cstr_buf, 'L');
1788 case TOK_CCHAR:
1789 cstr_ccat(&cstr_buf, '\'');
1790 add_char(&cstr_buf, cv->i);
1791 cstr_ccat(&cstr_buf, '\'');
1792 cstr_ccat(&cstr_buf, '\0');
1793 break;
1794 case TOK_PPNUM:
1795 cstr = cv->cstr;
1796 len = cstr->size - 1;
1797 for(i=0;i<len;i++)
1798 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1799 cstr_ccat(&cstr_buf, '\0');
1800 break;
1801 case TOK_LSTR:
1802 cstr_ccat(&cstr_buf, 'L');
1803 case TOK_STR:
1804 cstr = cv->cstr;
1805 cstr_ccat(&cstr_buf, '\"');
1806 if (v == TOK_STR) {
1807 len = cstr->size - 1;
1808 for(i=0;i<len;i++)
1809 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1810 } else {
1811 len = (cstr->size / sizeof(nwchar_t)) - 1;
1812 for(i=0;i<len;i++)
1813 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1815 cstr_ccat(&cstr_buf, '\"');
1816 cstr_ccat(&cstr_buf, '\0');
1817 break;
1818 case TOK_LT:
1819 v = '<';
1820 goto addv;
1821 case TOK_GT:
1822 v = '>';
1823 goto addv;
1824 case TOK_DOTS:
1825 return strcpy(p, "...");
1826 case TOK_A_SHL:
1827 return strcpy(p, "<<=");
1828 case TOK_A_SAR:
1829 return strcpy(p, ">>=");
1830 default:
1831 if (v < TOK_IDENT) {
1832 /* search in two bytes table */
1833 q = tok_two_chars;
1834 while (*q) {
1835 if (q[2] == v) {
1836 *p++ = q[0];
1837 *p++ = q[1];
1838 *p = '\0';
1839 return buf;
1841 q += 3;
1843 addv:
1844 *p++ = v;
1845 *p = '\0';
1846 } else if (v < tok_ident) {
1847 return table_ident[v - TOK_IDENT]->str;
1848 } else if (v >= SYM_FIRST_ANOM) {
1849 /* special name for anonymous symbol */
1850 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1851 } else {
1852 /* should never happen */
1853 return NULL;
1855 break;
1857 return cstr_buf.data;
1860 /* push, without hashing */
1861 static Sym *sym_push2(Sym **ps, int v, int t, long c)
1863 Sym *s;
1864 s = sym_malloc();
1865 s->v = v;
1866 s->type.t = t;
1867 s->c = c;
1868 s->next = NULL;
1869 /* add in stack */
1870 s->prev = *ps;
1871 *ps = s;
1872 return s;
1875 /* find a symbol and return its associated structure. 's' is the top
1876 of the symbol stack */
1877 static Sym *sym_find2(Sym *s, int v)
1879 while (s) {
1880 if (s->v == v)
1881 return s;
1882 s = s->prev;
1884 return NULL;
1887 /* structure lookup */
1888 static inline Sym *struct_find(int v)
1890 v -= TOK_IDENT;
1891 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1892 return NULL;
1893 return table_ident[v]->sym_struct;
1896 /* find an identifier */
1897 static inline Sym *sym_find(int v)
1899 v -= TOK_IDENT;
1900 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1901 return NULL;
1902 return table_ident[v]->sym_identifier;
1905 /* push a given symbol on the symbol stack */
1906 static Sym *sym_push(int v, CType *type, int r, int c)
1908 Sym *s, **ps;
1909 TokenSym *ts;
1911 if (local_stack)
1912 ps = &local_stack;
1913 else
1914 ps = &global_stack;
1915 s = sym_push2(ps, v, type->t, c);
1916 s->type.ref = type->ref;
1917 s->r = r;
1918 /* don't record fields or anonymous symbols */
1919 /* XXX: simplify */
1920 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1921 /* record symbol in token array */
1922 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1923 if (v & SYM_STRUCT)
1924 ps = &ts->sym_struct;
1925 else
1926 ps = &ts->sym_identifier;
1927 s->prev_tok = *ps;
1928 *ps = s;
1930 return s;
1933 /* push a global identifier */
1934 static Sym *global_identifier_push(int v, int t, int c)
1936 Sym *s, **ps;
1937 s = sym_push2(&global_stack, v, t, c);
1938 /* don't record anonymous symbol */
1939 if (v < SYM_FIRST_ANOM) {
1940 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1941 /* modify the top most local identifier, so that
1942 sym_identifier will point to 's' when popped */
1943 while (*ps != NULL)
1944 ps = &(*ps)->prev_tok;
1945 s->prev_tok = NULL;
1946 *ps = s;
1948 return s;
1951 /* pop symbols until top reaches 'b' */
1952 static void sym_pop(Sym **ptop, Sym *b)
1954 Sym *s, *ss, **ps;
1955 TokenSym *ts;
1956 int v;
1958 s = *ptop;
1959 while(s != b) {
1960 ss = s->prev;
1961 v = s->v;
1962 /* remove symbol in token array */
1963 /* XXX: simplify */
1964 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1965 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1966 if (v & SYM_STRUCT)
1967 ps = &ts->sym_struct;
1968 else
1969 ps = &ts->sym_identifier;
1970 *ps = s->prev_tok;
1972 sym_free(s);
1973 s = ss;
1975 *ptop = b;
1978 /* I/O layer */
1980 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1982 int fd;
1983 BufferedFile *bf;
1985 if (strcmp(filename, "-") == 0)
1986 fd = 0, filename = "stdin";
1987 else
1988 fd = open(filename, O_RDONLY | O_BINARY);
1989 if ((verbose == 2 && fd >= 0) || verbose == 3)
1990 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
1991 (s1->include_stack_ptr - s1->include_stack), "", filename);
1992 if (fd < 0)
1993 return NULL;
1994 bf = tcc_malloc(sizeof(BufferedFile));
1995 bf->fd = fd;
1996 bf->buf_ptr = bf->buffer;
1997 bf->buf_end = bf->buffer;
1998 bf->buffer[0] = CH_EOB; /* put eob symbol */
1999 pstrcpy(bf->filename, sizeof(bf->filename), filename);
2000 #ifdef _WIN32
2001 normalize_slashes(bf->filename);
2002 #endif
2003 bf->line_num = 1;
2004 bf->ifndef_macro = 0;
2005 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
2006 // printf("opening '%s'\n", filename);
2007 return bf;
2010 void tcc_close(BufferedFile *bf)
2012 total_lines += bf->line_num;
2013 close(bf->fd);
2014 tcc_free(bf);
2017 /* fill input buffer and peek next char */
2018 static int tcc_peekc_slow(BufferedFile *bf)
2020 int len;
2021 /* only tries to read if really end of buffer */
2022 if (bf->buf_ptr >= bf->buf_end) {
2023 if (bf->fd != -1) {
2024 #if defined(PARSE_DEBUG)
2025 len = 8;
2026 #else
2027 len = IO_BUF_SIZE;
2028 #endif
2029 len = read(bf->fd, bf->buffer, len);
2030 if (len < 0)
2031 len = 0;
2032 } else {
2033 len = 0;
2035 total_bytes += len;
2036 bf->buf_ptr = bf->buffer;
2037 bf->buf_end = bf->buffer + len;
2038 *bf->buf_end = CH_EOB;
2040 if (bf->buf_ptr < bf->buf_end) {
2041 return bf->buf_ptr[0];
2042 } else {
2043 bf->buf_ptr = bf->buf_end;
2044 return CH_EOF;
2048 /* return the current character, handling end of block if necessary
2049 (but not stray) */
2050 static int handle_eob(void)
2052 return tcc_peekc_slow(file);
2055 /* read next char from current input file and handle end of input buffer */
2056 static inline void inp(void)
2058 ch = *(++(file->buf_ptr));
2059 /* end of buffer/file handling */
2060 if (ch == CH_EOB)
2061 ch = handle_eob();
2064 /* handle '\[\r]\n' */
2065 static int handle_stray_noerror(void)
2067 while (ch == '\\') {
2068 inp();
2069 if (ch == '\n') {
2070 file->line_num++;
2071 inp();
2072 } else if (ch == '\r') {
2073 inp();
2074 if (ch != '\n')
2075 goto fail;
2076 file->line_num++;
2077 inp();
2078 } else {
2079 fail:
2080 return 1;
2083 return 0;
2086 static void handle_stray(void)
2088 if (handle_stray_noerror())
2089 error("stray '\\' in program");
2092 /* skip the stray and handle the \\n case. Output an error if
2093 incorrect char after the stray */
2094 static int handle_stray1(uint8_t *p)
2096 int c;
2098 if (p >= file->buf_end) {
2099 file->buf_ptr = p;
2100 c = handle_eob();
2101 p = file->buf_ptr;
2102 if (c == '\\')
2103 goto parse_stray;
2104 } else {
2105 parse_stray:
2106 file->buf_ptr = p;
2107 ch = *p;
2108 handle_stray();
2109 p = file->buf_ptr;
2110 c = *p;
2112 return c;
2115 /* handle just the EOB case, but not stray */
2116 #define PEEKC_EOB(c, p)\
2118 p++;\
2119 c = *p;\
2120 if (c == '\\') {\
2121 file->buf_ptr = p;\
2122 c = handle_eob();\
2123 p = file->buf_ptr;\
2127 /* handle the complicated stray case */
2128 #define PEEKC(c, p)\
2130 p++;\
2131 c = *p;\
2132 if (c == '\\') {\
2133 c = handle_stray1(p);\
2134 p = file->buf_ptr;\
2138 /* input with '\[\r]\n' handling. Note that this function cannot
2139 handle other characters after '\', so you cannot call it inside
2140 strings or comments */
2141 static void minp(void)
2143 inp();
2144 if (ch == '\\')
2145 handle_stray();
2149 /* single line C++ comments */
2150 static uint8_t *parse_line_comment(uint8_t *p)
2152 int c;
2154 p++;
2155 for(;;) {
2156 c = *p;
2157 redo:
2158 if (c == '\n' || c == CH_EOF) {
2159 break;
2160 } else if (c == '\\') {
2161 file->buf_ptr = p;
2162 c = handle_eob();
2163 p = file->buf_ptr;
2164 if (c == '\\') {
2165 PEEKC_EOB(c, p);
2166 if (c == '\n') {
2167 file->line_num++;
2168 PEEKC_EOB(c, p);
2169 } else if (c == '\r') {
2170 PEEKC_EOB(c, p);
2171 if (c == '\n') {
2172 file->line_num++;
2173 PEEKC_EOB(c, p);
2176 } else {
2177 goto redo;
2179 } else {
2180 p++;
2183 return p;
2186 /* C comments */
2187 static uint8_t *parse_comment(uint8_t *p)
2189 int c;
2191 p++;
2192 for(;;) {
2193 /* fast skip loop */
2194 for(;;) {
2195 c = *p;
2196 if (c == '\n' || c == '*' || c == '\\')
2197 break;
2198 p++;
2199 c = *p;
2200 if (c == '\n' || c == '*' || c == '\\')
2201 break;
2202 p++;
2204 /* now we can handle all the cases */
2205 if (c == '\n') {
2206 file->line_num++;
2207 p++;
2208 } else if (c == '*') {
2209 p++;
2210 for(;;) {
2211 c = *p;
2212 if (c == '*') {
2213 p++;
2214 } else if (c == '/') {
2215 goto end_of_comment;
2216 } else if (c == '\\') {
2217 file->buf_ptr = p;
2218 c = handle_eob();
2219 p = file->buf_ptr;
2220 if (c == '\\') {
2221 /* skip '\[\r]\n', otherwise just skip the stray */
2222 while (c == '\\') {
2223 PEEKC_EOB(c, p);
2224 if (c == '\n') {
2225 file->line_num++;
2226 PEEKC_EOB(c, p);
2227 } else if (c == '\r') {
2228 PEEKC_EOB(c, p);
2229 if (c == '\n') {
2230 file->line_num++;
2231 PEEKC_EOB(c, p);
2233 } else {
2234 goto after_star;
2238 } else {
2239 break;
2242 after_star: ;
2243 } else {
2244 /* stray, eob or eof */
2245 file->buf_ptr = p;
2246 c = handle_eob();
2247 p = file->buf_ptr;
2248 if (c == CH_EOF) {
2249 error("unexpected end of file in comment");
2250 } else if (c == '\\') {
2251 p++;
2255 end_of_comment:
2256 p++;
2257 return p;
2260 #define cinp minp
2262 /* space exlcuding newline */
2263 static inline int is_space(int ch)
2265 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2268 static inline void skip_spaces(void)
2270 while (is_space(ch))
2271 cinp();
2274 /* parse a string without interpreting escapes */
2275 static uint8_t *parse_pp_string(uint8_t *p,
2276 int sep, CString *str)
2278 int c;
2279 p++;
2280 for(;;) {
2281 c = *p;
2282 if (c == sep) {
2283 break;
2284 } else if (c == '\\') {
2285 file->buf_ptr = p;
2286 c = handle_eob();
2287 p = file->buf_ptr;
2288 if (c == CH_EOF) {
2289 unterminated_string:
2290 /* XXX: indicate line number of start of string */
2291 error("missing terminating %c character", sep);
2292 } else if (c == '\\') {
2293 /* escape : just skip \[\r]\n */
2294 PEEKC_EOB(c, p);
2295 if (c == '\n') {
2296 file->line_num++;
2297 p++;
2298 } else if (c == '\r') {
2299 PEEKC_EOB(c, p);
2300 if (c != '\n')
2301 expect("'\n' after '\r'");
2302 file->line_num++;
2303 p++;
2304 } else if (c == CH_EOF) {
2305 goto unterminated_string;
2306 } else {
2307 if (str) {
2308 cstr_ccat(str, '\\');
2309 cstr_ccat(str, c);
2311 p++;
2314 } else if (c == '\n') {
2315 file->line_num++;
2316 goto add_char;
2317 } else if (c == '\r') {
2318 PEEKC_EOB(c, p);
2319 if (c != '\n') {
2320 if (str)
2321 cstr_ccat(str, '\r');
2322 } else {
2323 file->line_num++;
2324 goto add_char;
2326 } else {
2327 add_char:
2328 if (str)
2329 cstr_ccat(str, c);
2330 p++;
2333 p++;
2334 return p;
2337 /* skip block of text until #else, #elif or #endif. skip also pairs of
2338 #if/#endif */
2339 void preprocess_skip(void)
2341 int a, start_of_line, c, in_warn_or_error;
2342 uint8_t *p;
2344 p = file->buf_ptr;
2345 a = 0;
2346 redo_start:
2347 start_of_line = 1;
2348 in_warn_or_error = 0;
2349 for(;;) {
2350 redo_no_start:
2351 c = *p;
2352 switch(c) {
2353 case ' ':
2354 case '\t':
2355 case '\f':
2356 case '\v':
2357 case '\r':
2358 p++;
2359 goto redo_no_start;
2360 case '\n':
2361 file->line_num++;
2362 p++;
2363 goto redo_start;
2364 case '\\':
2365 file->buf_ptr = p;
2366 c = handle_eob();
2367 if (c == CH_EOF) {
2368 expect("#endif");
2369 } else if (c == '\\') {
2370 ch = file->buf_ptr[0];
2371 handle_stray_noerror();
2373 p = file->buf_ptr;
2374 goto redo_no_start;
2375 /* skip strings */
2376 case '\"':
2377 case '\'':
2378 if (in_warn_or_error)
2379 goto _default;
2380 p = parse_pp_string(p, c, NULL);
2381 break;
2382 /* skip comments */
2383 case '/':
2384 if (in_warn_or_error)
2385 goto _default;
2386 file->buf_ptr = p;
2387 ch = *p;
2388 minp();
2389 p = file->buf_ptr;
2390 if (ch == '*') {
2391 p = parse_comment(p);
2392 } else if (ch == '/') {
2393 p = parse_line_comment(p);
2395 break;
2396 case '#':
2397 p++;
2398 if (start_of_line) {
2399 file->buf_ptr = p;
2400 next_nomacro();
2401 p = file->buf_ptr;
2402 if (a == 0 &&
2403 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2404 goto the_end;
2405 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2406 a++;
2407 else if (tok == TOK_ENDIF)
2408 a--;
2409 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2410 in_warn_or_error = 1;
2412 break;
2413 _default:
2414 default:
2415 p++;
2416 break;
2418 start_of_line = 0;
2420 the_end: ;
2421 file->buf_ptr = p;
2424 /* ParseState handling */
2426 /* XXX: currently, no include file info is stored. Thus, we cannot display
2427 accurate messages if the function or data definition spans multiple
2428 files */
2430 /* save current parse state in 's' */
2431 void save_parse_state(ParseState *s)
2433 s->line_num = file->line_num;
2434 s->macro_ptr = macro_ptr;
2435 s->tok = tok;
2436 s->tokc = tokc;
2439 /* restore parse state from 's' */
2440 void restore_parse_state(ParseState *s)
2442 file->line_num = s->line_num;
2443 macro_ptr = s->macro_ptr;
2444 tok = s->tok;
2445 tokc = s->tokc;
2448 /* return the number of additional 'ints' necessary to store the
2449 token */
2450 static inline int tok_ext_size(int t)
2452 switch(t) {
2453 /* 4 bytes */
2454 case TOK_CINT:
2455 case TOK_CUINT:
2456 case TOK_CCHAR:
2457 case TOK_LCHAR:
2458 case TOK_CFLOAT:
2459 case TOK_LINENUM:
2460 return 1;
2461 case TOK_STR:
2462 case TOK_LSTR:
2463 case TOK_PPNUM:
2464 error("unsupported token");
2465 return 1;
2466 case TOK_CDOUBLE:
2467 case TOK_CLLONG:
2468 case TOK_CULLONG:
2469 return 2;
2470 case TOK_CLDOUBLE:
2471 return LDOUBLE_SIZE / 4;
2472 default:
2473 return 0;
2477 /* token string handling */
2479 static inline void tok_str_new(TokenString *s)
2481 s->str = NULL;
2482 s->len = 0;
2483 s->allocated_len = 0;
2484 s->last_line_num = -1;
2487 static void tok_str_free(int *str)
2489 tcc_free(str);
2492 static int *tok_str_realloc(TokenString *s)
2494 int *str, len;
2496 if (s->allocated_len == 0) {
2497 len = 8;
2498 } else {
2499 len = s->allocated_len * 2;
2501 str = tcc_realloc(s->str, len * sizeof(int));
2502 if (!str)
2503 error("memory full");
2504 s->allocated_len = len;
2505 s->str = str;
2506 return str;
2509 static void tok_str_add(TokenString *s, int t)
2511 int len, *str;
2513 len = s->len;
2514 str = s->str;
2515 if (len >= s->allocated_len)
2516 str = tok_str_realloc(s);
2517 str[len++] = t;
2518 s->len = len;
2521 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2523 int len, *str;
2525 len = s->len;
2526 str = s->str;
2528 /* allocate space for worst case */
2529 if (len + TOK_MAX_SIZE > s->allocated_len)
2530 str = tok_str_realloc(s);
2531 str[len++] = t;
2532 switch(t) {
2533 case TOK_CINT:
2534 case TOK_CUINT:
2535 case TOK_CCHAR:
2536 case TOK_LCHAR:
2537 case TOK_CFLOAT:
2538 case TOK_LINENUM:
2539 str[len++] = cv->tab[0];
2540 break;
2541 case TOK_PPNUM:
2542 case TOK_STR:
2543 case TOK_LSTR:
2545 int nb_words;
2546 CString *cstr;
2548 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2549 while ((len + nb_words) > s->allocated_len)
2550 str = tok_str_realloc(s);
2551 cstr = (CString *)(str + len);
2552 cstr->data = NULL;
2553 cstr->size = cv->cstr->size;
2554 cstr->data_allocated = NULL;
2555 cstr->size_allocated = cstr->size;
2556 memcpy((char *)cstr + sizeof(CString),
2557 cv->cstr->data, cstr->size);
2558 len += nb_words;
2560 break;
2561 case TOK_CDOUBLE:
2562 case TOK_CLLONG:
2563 case TOK_CULLONG:
2564 #if LDOUBLE_SIZE == 8
2565 case TOK_CLDOUBLE:
2566 #endif
2567 str[len++] = cv->tab[0];
2568 str[len++] = cv->tab[1];
2569 break;
2570 #if LDOUBLE_SIZE == 12
2571 case TOK_CLDOUBLE:
2572 str[len++] = cv->tab[0];
2573 str[len++] = cv->tab[1];
2574 str[len++] = cv->tab[2];
2575 #elif LDOUBLE_SIZE == 16
2576 case TOK_CLDOUBLE:
2577 str[len++] = cv->tab[0];
2578 str[len++] = cv->tab[1];
2579 str[len++] = cv->tab[2];
2580 str[len++] = cv->tab[3];
2581 #elif LDOUBLE_SIZE != 8
2582 #error add long double size support
2583 #endif
2584 break;
2585 default:
2586 break;
2588 s->len = len;
2591 /* add the current parse token in token string 's' */
2592 static void tok_str_add_tok(TokenString *s)
2594 CValue cval;
2596 /* save line number info */
2597 if (file->line_num != s->last_line_num) {
2598 s->last_line_num = file->line_num;
2599 cval.i = s->last_line_num;
2600 tok_str_add2(s, TOK_LINENUM, &cval);
2602 tok_str_add2(s, tok, &tokc);
2605 #if LDOUBLE_SIZE == 16
2606 #define LDOUBLE_GET(p, cv) \
2607 cv.tab[0] = p[0]; \
2608 cv.tab[1] = p[1]; \
2609 cv.tab[2] = p[2]; \
2610 cv.tab[3] = p[3];
2611 #elif LDOUBLE_SIZE == 12
2612 #define LDOUBLE_GET(p, cv) \
2613 cv.tab[0] = p[0]; \
2614 cv.tab[1] = p[1]; \
2615 cv.tab[2] = p[2];
2616 #elif LDOUBLE_SIZE == 8
2617 #define LDOUBLE_GET(p, cv) \
2618 cv.tab[0] = p[0]; \
2619 cv.tab[1] = p[1];
2620 #else
2621 #error add long double size support
2622 #endif
2625 /* get a token from an integer array and increment pointer
2626 accordingly. we code it as a macro to avoid pointer aliasing. */
2627 #define TOK_GET(t, p, cv) \
2629 t = *p++; \
2630 switch(t) { \
2631 case TOK_CINT: \
2632 case TOK_CUINT: \
2633 case TOK_CCHAR: \
2634 case TOK_LCHAR: \
2635 case TOK_CFLOAT: \
2636 case TOK_LINENUM: \
2637 cv.tab[0] = *p++; \
2638 break; \
2639 case TOK_STR: \
2640 case TOK_LSTR: \
2641 case TOK_PPNUM: \
2642 cv.cstr = (CString *)p; \
2643 cv.cstr->data = (char *)p + sizeof(CString);\
2644 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2645 break; \
2646 case TOK_CDOUBLE: \
2647 case TOK_CLLONG: \
2648 case TOK_CULLONG: \
2649 cv.tab[0] = p[0]; \
2650 cv.tab[1] = p[1]; \
2651 p += 2; \
2652 break; \
2653 case TOK_CLDOUBLE: \
2654 LDOUBLE_GET(p, cv); \
2655 p += LDOUBLE_SIZE / 4; \
2656 break; \
2657 default: \
2658 break; \
2662 /* defines handling */
2663 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2665 Sym *s;
2667 s = sym_push2(&define_stack, v, macro_type, (long)str);
2668 s->next = first_arg;
2669 table_ident[v - TOK_IDENT]->sym_define = s;
2672 /* undefined a define symbol. Its name is just set to zero */
2673 static void define_undef(Sym *s)
2675 int v;
2676 v = s->v;
2677 if (v >= TOK_IDENT && v < tok_ident)
2678 table_ident[v - TOK_IDENT]->sym_define = NULL;
2679 s->v = 0;
2682 static inline Sym *define_find(int v)
2684 v -= TOK_IDENT;
2685 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2686 return NULL;
2687 return table_ident[v]->sym_define;
2690 /* free define stack until top reaches 'b' */
2691 static void free_defines(Sym *b)
2693 Sym *top, *top1;
2694 int v;
2696 top = define_stack;
2697 while (top != b) {
2698 top1 = top->prev;
2699 /* do not free args or predefined defines */
2700 if (top->c)
2701 tok_str_free((int *)top->c);
2702 v = top->v;
2703 if (v >= TOK_IDENT && v < tok_ident)
2704 table_ident[v - TOK_IDENT]->sym_define = NULL;
2705 sym_free(top);
2706 top = top1;
2708 define_stack = b;
2711 /* label lookup */
2712 static Sym *label_find(int v)
2714 v -= TOK_IDENT;
2715 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2716 return NULL;
2717 return table_ident[v]->sym_label;
2720 static Sym *label_push(Sym **ptop, int v, int flags)
2722 Sym *s, **ps;
2723 s = sym_push2(ptop, v, 0, 0);
2724 s->r = flags;
2725 ps = &table_ident[v - TOK_IDENT]->sym_label;
2726 if (ptop == &global_label_stack) {
2727 /* modify the top most local identifier, so that
2728 sym_identifier will point to 's' when popped */
2729 while (*ps != NULL)
2730 ps = &(*ps)->prev_tok;
2732 s->prev_tok = *ps;
2733 *ps = s;
2734 return s;
2737 /* pop labels until element last is reached. Look if any labels are
2738 undefined. Define symbols if '&&label' was used. */
2739 static void label_pop(Sym **ptop, Sym *slast)
2741 Sym *s, *s1;
2742 for(s = *ptop; s != slast; s = s1) {
2743 s1 = s->prev;
2744 if (s->r == LABEL_DECLARED) {
2745 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2746 } else if (s->r == LABEL_FORWARD) {
2747 error("label '%s' used but not defined",
2748 get_tok_str(s->v, NULL));
2749 } else {
2750 if (s->c) {
2751 /* define corresponding symbol. A size of
2752 1 is put. */
2753 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2756 /* remove label */
2757 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2758 sym_free(s);
2760 *ptop = slast;
2763 /* eval an expression for #if/#elif */
2764 static int expr_preprocess(void)
2766 int c, t;
2767 TokenString str;
2769 tok_str_new(&str);
2770 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2771 next(); /* do macro subst */
2772 if (tok == TOK_DEFINED) {
2773 next_nomacro();
2774 t = tok;
2775 if (t == '(')
2776 next_nomacro();
2777 c = define_find(tok) != 0;
2778 if (t == '(')
2779 next_nomacro();
2780 tok = TOK_CINT;
2781 tokc.i = c;
2782 } else if (tok >= TOK_IDENT) {
2783 /* if undefined macro */
2784 tok = TOK_CINT;
2785 tokc.i = 0;
2787 tok_str_add_tok(&str);
2789 tok_str_add(&str, -1); /* simulate end of file */
2790 tok_str_add(&str, 0);
2791 /* now evaluate C constant expression */
2792 macro_ptr = str.str;
2793 next();
2794 c = expr_const();
2795 macro_ptr = NULL;
2796 tok_str_free(str.str);
2797 return c != 0;
2800 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2801 static void tok_print(int *str)
2803 int t;
2804 CValue cval;
2806 while (1) {
2807 TOK_GET(t, str, cval);
2808 if (!t)
2809 break;
2810 printf(" %s", get_tok_str(t, &cval));
2812 printf("\n");
2814 #endif
2816 /* parse after #define */
2817 static void parse_define(void)
2819 Sym *s, *first, **ps;
2820 int v, t, varg, is_vaargs, c;
2821 TokenString str;
2823 v = tok;
2824 if (v < TOK_IDENT)
2825 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2826 /* XXX: should check if same macro (ANSI) */
2827 first = NULL;
2828 t = MACRO_OBJ;
2829 /* '(' must be just after macro definition for MACRO_FUNC */
2830 c = file->buf_ptr[0];
2831 if (c == '\\')
2832 c = handle_stray1(file->buf_ptr);
2833 if (c == '(') {
2834 next_nomacro();
2835 next_nomacro();
2836 ps = &first;
2837 while (tok != ')') {
2838 varg = tok;
2839 next_nomacro();
2840 is_vaargs = 0;
2841 if (varg == TOK_DOTS) {
2842 varg = TOK___VA_ARGS__;
2843 is_vaargs = 1;
2844 } else if (tok == TOK_DOTS && gnu_ext) {
2845 is_vaargs = 1;
2846 next_nomacro();
2848 if (varg < TOK_IDENT)
2849 error("badly punctuated parameter list");
2850 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2851 *ps = s;
2852 ps = &s->next;
2853 if (tok != ',')
2854 break;
2855 next_nomacro();
2857 t = MACRO_FUNC;
2859 tok_str_new(&str);
2860 next_nomacro();
2861 /* EOF testing necessary for '-D' handling */
2862 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2863 tok_str_add2(&str, tok, &tokc);
2864 next_nomacro();
2866 tok_str_add(&str, 0);
2867 #ifdef PP_DEBUG
2868 printf("define %s %d: ", get_tok_str(v, NULL), t);
2869 tok_print(str.str);
2870 #endif
2871 define_push(v, t, str.str, first);
2874 static inline int hash_cached_include(int type, const char *filename)
2876 const unsigned char *s;
2877 unsigned int h;
2879 h = TOK_HASH_INIT;
2880 h = TOK_HASH_FUNC(h, type);
2881 s = filename;
2882 while (*s) {
2883 h = TOK_HASH_FUNC(h, *s);
2884 s++;
2886 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2887 return h;
2890 /* XXX: use a token or a hash table to accelerate matching ? */
2891 static CachedInclude *search_cached_include(TCCState *s1,
2892 int type, const char *filename)
2894 CachedInclude *e;
2895 int i, h;
2896 h = hash_cached_include(type, filename);
2897 i = s1->cached_includes_hash[h];
2898 for(;;) {
2899 if (i == 0)
2900 break;
2901 e = s1->cached_includes[i - 1];
2902 if (e->type == type && !strcmp(e->filename, filename))
2903 return e;
2904 i = e->hash_next;
2906 return NULL;
2909 static inline void add_cached_include(TCCState *s1, int type,
2910 const char *filename, int ifndef_macro)
2912 CachedInclude *e;
2913 int h;
2915 if (search_cached_include(s1, type, filename))
2916 return;
2917 #ifdef INC_DEBUG
2918 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2919 #endif
2920 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2921 if (!e)
2922 return;
2923 e->type = type;
2924 strcpy(e->filename, filename);
2925 e->ifndef_macro = ifndef_macro;
2926 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2927 /* add in hash table */
2928 h = hash_cached_include(type, filename);
2929 e->hash_next = s1->cached_includes_hash[h];
2930 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2933 static void pragma_parse(TCCState *s1)
2935 int val;
2937 next();
2938 if (tok == TOK_pack) {
2940 This may be:
2941 #pragma pack(1) // set
2942 #pragma pack() // reset to default
2943 #pragma pack(push,1) // push & set
2944 #pragma pack(pop) // restore previous
2946 next();
2947 skip('(');
2948 if (tok == TOK_ASM_pop) {
2949 next();
2950 if (s1->pack_stack_ptr <= s1->pack_stack) {
2951 stk_error:
2952 error("out of pack stack");
2954 s1->pack_stack_ptr--;
2955 } else {
2956 val = 0;
2957 if (tok != ')') {
2958 if (tok == TOK_ASM_push) {
2959 next();
2960 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2961 goto stk_error;
2962 s1->pack_stack_ptr++;
2963 skip(',');
2965 if (tok != TOK_CINT) {
2966 pack_error:
2967 error("invalid pack pragma");
2969 val = tokc.i;
2970 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2971 goto pack_error;
2972 next();
2974 *s1->pack_stack_ptr = val;
2975 skip(')');
2980 /* is_bof is true if first non space token at beginning of file */
2981 static void preprocess(int is_bof)
2983 TCCState *s1 = tcc_state;
2984 int size, i, c, n, saved_parse_flags;
2985 char buf[1024], *q;
2986 char buf1[1024];
2987 BufferedFile *f;
2988 Sym *s;
2989 CachedInclude *e;
2991 saved_parse_flags = parse_flags;
2992 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2993 PARSE_FLAG_LINEFEED;
2994 next_nomacro();
2995 redo:
2996 switch(tok) {
2997 case TOK_DEFINE:
2998 next_nomacro();
2999 parse_define();
3000 break;
3001 case TOK_UNDEF:
3002 next_nomacro();
3003 s = define_find(tok);
3004 /* undefine symbol by putting an invalid name */
3005 if (s)
3006 define_undef(s);
3007 break;
3008 case TOK_INCLUDE:
3009 case TOK_INCLUDE_NEXT:
3010 ch = file->buf_ptr[0];
3011 /* XXX: incorrect if comments : use next_nomacro with a special mode */
3012 skip_spaces();
3013 if (ch == '<') {
3014 c = '>';
3015 goto read_name;
3016 } else if (ch == '\"') {
3017 c = ch;
3018 read_name:
3019 inp();
3020 q = buf;
3021 while (ch != c && ch != '\n' && ch != CH_EOF) {
3022 if ((q - buf) < sizeof(buf) - 1)
3023 *q++ = ch;
3024 if (ch == '\\') {
3025 if (handle_stray_noerror() == 0)
3026 --q;
3027 } else
3028 inp();
3030 *q = '\0';
3031 minp();
3032 #if 0
3033 /* eat all spaces and comments after include */
3034 /* XXX: slightly incorrect */
3035 while (ch1 != '\n' && ch1 != CH_EOF)
3036 inp();
3037 #endif
3038 } else {
3039 /* computed #include : either we have only strings or
3040 we have anything enclosed in '<>' */
3041 next();
3042 buf[0] = '\0';
3043 if (tok == TOK_STR) {
3044 while (tok != TOK_LINEFEED) {
3045 if (tok != TOK_STR) {
3046 include_syntax:
3047 error("'#include' expects \"FILENAME\" or <FILENAME>");
3049 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3050 next();
3052 c = '\"';
3053 } else {
3054 int len;
3055 while (tok != TOK_LINEFEED) {
3056 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3057 next();
3059 len = strlen(buf);
3060 /* check syntax and remove '<>' */
3061 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3062 goto include_syntax;
3063 memmove(buf, buf + 1, len - 2);
3064 buf[len - 2] = '\0';
3065 c = '>';
3069 e = search_cached_include(s1, c, buf);
3070 if (e && define_find(e->ifndef_macro)) {
3071 /* no need to parse the include because the 'ifndef macro'
3072 is defined */
3073 #ifdef INC_DEBUG
3074 printf("%s: skipping %s\n", file->filename, buf);
3075 #endif
3076 } else {
3077 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3078 error("#include recursion too deep");
3079 /* push current file in stack */
3080 /* XXX: fix current line init */
3081 *s1->include_stack_ptr++ = file;
3082 if (c == '\"') {
3083 /* first search in current dir if "header.h" */
3084 size = tcc_basename(file->filename) - file->filename;
3085 if (size > sizeof(buf1) - 1)
3086 size = sizeof(buf1) - 1;
3087 memcpy(buf1, file->filename, size);
3088 buf1[size] = '\0';
3089 pstrcat(buf1, sizeof(buf1), buf);
3090 f = tcc_open(s1, buf1);
3091 if (f) {
3092 if (tok == TOK_INCLUDE_NEXT)
3093 tok = TOK_INCLUDE;
3094 else
3095 goto found;
3098 /* now search in all the include paths */
3099 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3100 for(i = 0; i < n; i++) {
3101 const char *path;
3102 if (i < s1->nb_include_paths)
3103 path = s1->include_paths[i];
3104 else
3105 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3106 pstrcpy(buf1, sizeof(buf1), path);
3107 pstrcat(buf1, sizeof(buf1), "/");
3108 pstrcat(buf1, sizeof(buf1), buf);
3109 f = tcc_open(s1, buf1);
3110 if (f) {
3111 if (tok == TOK_INCLUDE_NEXT)
3112 tok = TOK_INCLUDE;
3113 else
3114 goto found;
3117 --s1->include_stack_ptr;
3118 error("include file '%s' not found", buf);
3119 break;
3120 found:
3121 #ifdef INC_DEBUG
3122 printf("%s: including %s\n", file->filename, buf1);
3123 #endif
3124 f->inc_type = c;
3125 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3126 file = f;
3127 /* add include file debug info */
3128 if (do_debug) {
3129 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3131 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3132 ch = file->buf_ptr[0];
3133 goto the_end;
3135 break;
3136 case TOK_IFNDEF:
3137 c = 1;
3138 goto do_ifdef;
3139 case TOK_IF:
3140 c = expr_preprocess();
3141 goto do_if;
3142 case TOK_IFDEF:
3143 c = 0;
3144 do_ifdef:
3145 next_nomacro();
3146 if (tok < TOK_IDENT)
3147 error("invalid argument for '#if%sdef'", c ? "n" : "");
3148 if (is_bof) {
3149 if (c) {
3150 #ifdef INC_DEBUG
3151 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3152 #endif
3153 file->ifndef_macro = tok;
3156 c = (define_find(tok) != 0) ^ c;
3157 do_if:
3158 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3159 error("memory full");
3160 *s1->ifdef_stack_ptr++ = c;
3161 goto test_skip;
3162 case TOK_ELSE:
3163 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3164 error("#else without matching #if");
3165 if (s1->ifdef_stack_ptr[-1] & 2)
3166 error("#else after #else");
3167 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3168 goto test_skip;
3169 case TOK_ELIF:
3170 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3171 error("#elif without matching #if");
3172 c = s1->ifdef_stack_ptr[-1];
3173 if (c > 1)
3174 error("#elif after #else");
3175 /* last #if/#elif expression was true: we skip */
3176 if (c == 1)
3177 goto skip;
3178 c = expr_preprocess();
3179 s1->ifdef_stack_ptr[-1] = c;
3180 test_skip:
3181 if (!(c & 1)) {
3182 skip:
3183 preprocess_skip();
3184 is_bof = 0;
3185 goto redo;
3187 break;
3188 case TOK_ENDIF:
3189 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3190 error("#endif without matching #if");
3191 s1->ifdef_stack_ptr--;
3192 /* '#ifndef macro' was at the start of file. Now we check if
3193 an '#endif' is exactly at the end of file */
3194 if (file->ifndef_macro &&
3195 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3196 file->ifndef_macro_saved = file->ifndef_macro;
3197 /* need to set to zero to avoid false matches if another
3198 #ifndef at middle of file */
3199 file->ifndef_macro = 0;
3200 while (tok != TOK_LINEFEED)
3201 next_nomacro();
3202 tok_flags |= TOK_FLAG_ENDIF;
3203 goto the_end;
3205 break;
3206 case TOK_LINE:
3207 next();
3208 if (tok != TOK_CINT)
3209 error("#line");
3210 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3211 next();
3212 if (tok != TOK_LINEFEED) {
3213 if (tok != TOK_STR)
3214 error("#line");
3215 pstrcpy(file->filename, sizeof(file->filename),
3216 (char *)tokc.cstr->data);
3218 break;
3219 case TOK_ERROR:
3220 case TOK_WARNING:
3221 c = tok;
3222 ch = file->buf_ptr[0];
3223 skip_spaces();
3224 q = buf;
3225 while (ch != '\n' && ch != CH_EOF) {
3226 if ((q - buf) < sizeof(buf) - 1)
3227 *q++ = ch;
3228 if (ch == '\\') {
3229 if (handle_stray_noerror() == 0)
3230 --q;
3231 } else
3232 inp();
3234 *q = '\0';
3235 if (c == TOK_ERROR)
3236 error("#error %s", buf);
3237 else
3238 warning("#warning %s", buf);
3239 break;
3240 case TOK_PRAGMA:
3241 pragma_parse(s1);
3242 break;
3243 default:
3244 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3245 /* '!' is ignored to allow C scripts. numbers are ignored
3246 to emulate cpp behaviour */
3247 } else {
3248 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3249 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3251 break;
3253 /* ignore other preprocess commands or #! for C scripts */
3254 while (tok != TOK_LINEFEED)
3255 next_nomacro();
3256 the_end:
3257 parse_flags = saved_parse_flags;
3260 /* evaluate escape codes in a string. */
3261 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3263 int c, n;
3264 const uint8_t *p;
3266 p = buf;
3267 for(;;) {
3268 c = *p;
3269 if (c == '\0')
3270 break;
3271 if (c == '\\') {
3272 p++;
3273 /* escape */
3274 c = *p;
3275 switch(c) {
3276 case '0': case '1': case '2': case '3':
3277 case '4': case '5': case '6': case '7':
3278 /* at most three octal digits */
3279 n = c - '0';
3280 p++;
3281 c = *p;
3282 if (isoct(c)) {
3283 n = n * 8 + c - '0';
3284 p++;
3285 c = *p;
3286 if (isoct(c)) {
3287 n = n * 8 + c - '0';
3288 p++;
3291 c = n;
3292 goto add_char_nonext;
3293 case 'x':
3294 case 'u':
3295 case 'U':
3296 p++;
3297 n = 0;
3298 for(;;) {
3299 c = *p;
3300 if (c >= 'a' && c <= 'f')
3301 c = c - 'a' + 10;
3302 else if (c >= 'A' && c <= 'F')
3303 c = c - 'A' + 10;
3304 else if (isnum(c))
3305 c = c - '0';
3306 else
3307 break;
3308 n = n * 16 + c;
3309 p++;
3311 c = n;
3312 goto add_char_nonext;
3313 case 'a':
3314 c = '\a';
3315 break;
3316 case 'b':
3317 c = '\b';
3318 break;
3319 case 'f':
3320 c = '\f';
3321 break;
3322 case 'n':
3323 c = '\n';
3324 break;
3325 case 'r':
3326 c = '\r';
3327 break;
3328 case 't':
3329 c = '\t';
3330 break;
3331 case 'v':
3332 c = '\v';
3333 break;
3334 case 'e':
3335 if (!gnu_ext)
3336 goto invalid_escape;
3337 c = 27;
3338 break;
3339 case '\'':
3340 case '\"':
3341 case '\\':
3342 case '?':
3343 break;
3344 default:
3345 invalid_escape:
3346 if (c >= '!' && c <= '~')
3347 warning("unknown escape sequence: \'\\%c\'", c);
3348 else
3349 warning("unknown escape sequence: \'\\x%x\'", c);
3350 break;
3353 p++;
3354 add_char_nonext:
3355 if (!is_long)
3356 cstr_ccat(outstr, c);
3357 else
3358 cstr_wccat(outstr, c);
3360 /* add a trailing '\0' */
3361 if (!is_long)
3362 cstr_ccat(outstr, '\0');
3363 else
3364 cstr_wccat(outstr, '\0');
3367 /* we use 64 bit numbers */
3368 #define BN_SIZE 2
3370 /* bn = (bn << shift) | or_val */
3371 void bn_lshift(unsigned int *bn, int shift, int or_val)
3373 int i;
3374 unsigned int v;
3375 for(i=0;i<BN_SIZE;i++) {
3376 v = bn[i];
3377 bn[i] = (v << shift) | or_val;
3378 or_val = v >> (32 - shift);
3382 void bn_zero(unsigned int *bn)
3384 int i;
3385 for(i=0;i<BN_SIZE;i++) {
3386 bn[i] = 0;
3390 /* parse number in null terminated string 'p' and return it in the
3391 current token */
3392 void parse_number(const char *p)
3394 int b, t, shift, frac_bits, s, exp_val, ch;
3395 char *q;
3396 unsigned int bn[BN_SIZE];
3397 double d;
3399 /* number */
3400 q = token_buf;
3401 ch = *p++;
3402 t = ch;
3403 ch = *p++;
3404 *q++ = t;
3405 b = 10;
3406 if (t == '.') {
3407 goto float_frac_parse;
3408 } else if (t == '0') {
3409 if (ch == 'x' || ch == 'X') {
3410 q--;
3411 ch = *p++;
3412 b = 16;
3413 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3414 q--;
3415 ch = *p++;
3416 b = 2;
3419 /* parse all digits. cannot check octal numbers at this stage
3420 because of floating point constants */
3421 while (1) {
3422 if (ch >= 'a' && ch <= 'f')
3423 t = ch - 'a' + 10;
3424 else if (ch >= 'A' && ch <= 'F')
3425 t = ch - 'A' + 10;
3426 else if (isnum(ch))
3427 t = ch - '0';
3428 else
3429 break;
3430 if (t >= b)
3431 break;
3432 if (q >= token_buf + STRING_MAX_SIZE) {
3433 num_too_long:
3434 error("number too long");
3436 *q++ = ch;
3437 ch = *p++;
3439 if (ch == '.' ||
3440 ((ch == 'e' || ch == 'E') && b == 10) ||
3441 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3442 if (b != 10) {
3443 /* NOTE: strtox should support that for hexa numbers, but
3444 non ISOC99 libcs do not support it, so we prefer to do
3445 it by hand */
3446 /* hexadecimal or binary floats */
3447 /* XXX: handle overflows */
3448 *q = '\0';
3449 if (b == 16)
3450 shift = 4;
3451 else
3452 shift = 2;
3453 bn_zero(bn);
3454 q = token_buf;
3455 while (1) {
3456 t = *q++;
3457 if (t == '\0') {
3458 break;
3459 } else if (t >= 'a') {
3460 t = t - 'a' + 10;
3461 } else if (t >= 'A') {
3462 t = t - 'A' + 10;
3463 } else {
3464 t = t - '0';
3466 bn_lshift(bn, shift, t);
3468 frac_bits = 0;
3469 if (ch == '.') {
3470 ch = *p++;
3471 while (1) {
3472 t = ch;
3473 if (t >= 'a' && t <= 'f') {
3474 t = t - 'a' + 10;
3475 } else if (t >= 'A' && t <= 'F') {
3476 t = t - 'A' + 10;
3477 } else if (t >= '0' && t <= '9') {
3478 t = t - '0';
3479 } else {
3480 break;
3482 if (t >= b)
3483 error("invalid digit");
3484 bn_lshift(bn, shift, t);
3485 frac_bits += shift;
3486 ch = *p++;
3489 if (ch != 'p' && ch != 'P')
3490 expect("exponent");
3491 ch = *p++;
3492 s = 1;
3493 exp_val = 0;
3494 if (ch == '+') {
3495 ch = *p++;
3496 } else if (ch == '-') {
3497 s = -1;
3498 ch = *p++;
3500 if (ch < '0' || ch > '9')
3501 expect("exponent digits");
3502 while (ch >= '0' && ch <= '9') {
3503 exp_val = exp_val * 10 + ch - '0';
3504 ch = *p++;
3506 exp_val = exp_val * s;
3508 /* now we can generate the number */
3509 /* XXX: should patch directly float number */
3510 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3511 d = ldexp(d, exp_val - frac_bits);
3512 t = toup(ch);
3513 if (t == 'F') {
3514 ch = *p++;
3515 tok = TOK_CFLOAT;
3516 /* float : should handle overflow */
3517 tokc.f = (float)d;
3518 } else if (t == 'L') {
3519 ch = *p++;
3520 tok = TOK_CLDOUBLE;
3521 /* XXX: not large enough */
3522 tokc.ld = (long double)d;
3523 } else {
3524 tok = TOK_CDOUBLE;
3525 tokc.d = d;
3527 } else {
3528 /* decimal floats */
3529 if (ch == '.') {
3530 if (q >= token_buf + STRING_MAX_SIZE)
3531 goto num_too_long;
3532 *q++ = ch;
3533 ch = *p++;
3534 float_frac_parse:
3535 while (ch >= '0' && ch <= '9') {
3536 if (q >= token_buf + STRING_MAX_SIZE)
3537 goto num_too_long;
3538 *q++ = ch;
3539 ch = *p++;
3542 if (ch == 'e' || ch == 'E') {
3543 if (q >= token_buf + STRING_MAX_SIZE)
3544 goto num_too_long;
3545 *q++ = ch;
3546 ch = *p++;
3547 if (ch == '-' || ch == '+') {
3548 if (q >= token_buf + STRING_MAX_SIZE)
3549 goto num_too_long;
3550 *q++ = ch;
3551 ch = *p++;
3553 if (ch < '0' || ch > '9')
3554 expect("exponent digits");
3555 while (ch >= '0' && ch <= '9') {
3556 if (q >= token_buf + STRING_MAX_SIZE)
3557 goto num_too_long;
3558 *q++ = ch;
3559 ch = *p++;
3562 *q = '\0';
3563 t = toup(ch);
3564 errno = 0;
3565 if (t == 'F') {
3566 ch = *p++;
3567 tok = TOK_CFLOAT;
3568 tokc.f = strtof(token_buf, NULL);
3569 } else if (t == 'L') {
3570 ch = *p++;
3571 tok = TOK_CLDOUBLE;
3572 tokc.ld = strtold(token_buf, NULL);
3573 } else {
3574 tok = TOK_CDOUBLE;
3575 tokc.d = strtod(token_buf, NULL);
3578 } else {
3579 unsigned long long n, n1;
3580 int lcount, ucount;
3582 /* integer number */
3583 *q = '\0';
3584 q = token_buf;
3585 if (b == 10 && *q == '0') {
3586 b = 8;
3587 q++;
3589 n = 0;
3590 while(1) {
3591 t = *q++;
3592 /* no need for checks except for base 10 / 8 errors */
3593 if (t == '\0') {
3594 break;
3595 } else if (t >= 'a') {
3596 t = t - 'a' + 10;
3597 } else if (t >= 'A') {
3598 t = t - 'A' + 10;
3599 } else {
3600 t = t - '0';
3601 if (t >= b)
3602 error("invalid digit");
3604 n1 = n;
3605 n = n * b + t;
3606 /* detect overflow */
3607 /* XXX: this test is not reliable */
3608 if (n < n1)
3609 error("integer constant overflow");
3612 /* XXX: not exactly ANSI compliant */
3613 if ((n & 0xffffffff00000000LL) != 0) {
3614 if ((n >> 63) != 0)
3615 tok = TOK_CULLONG;
3616 else
3617 tok = TOK_CLLONG;
3618 } else if (n > 0x7fffffff) {
3619 tok = TOK_CUINT;
3620 } else {
3621 tok = TOK_CINT;
3623 lcount = 0;
3624 ucount = 0;
3625 for(;;) {
3626 t = toup(ch);
3627 if (t == 'L') {
3628 if (lcount >= 2)
3629 error("three 'l's in integer constant");
3630 lcount++;
3631 if (lcount == 2) {
3632 if (tok == TOK_CINT)
3633 tok = TOK_CLLONG;
3634 else if (tok == TOK_CUINT)
3635 tok = TOK_CULLONG;
3637 ch = *p++;
3638 } else if (t == 'U') {
3639 if (ucount >= 1)
3640 error("two 'u's in integer constant");
3641 ucount++;
3642 if (tok == TOK_CINT)
3643 tok = TOK_CUINT;
3644 else if (tok == TOK_CLLONG)
3645 tok = TOK_CULLONG;
3646 ch = *p++;
3647 } else {
3648 break;
3651 if (tok == TOK_CINT || tok == TOK_CUINT)
3652 tokc.ui = n;
3653 else
3654 tokc.ull = n;
3659 #define PARSE2(c1, tok1, c2, tok2) \
3660 case c1: \
3661 PEEKC(c, p); \
3662 if (c == c2) { \
3663 p++; \
3664 tok = tok2; \
3665 } else { \
3666 tok = tok1; \
3668 break;
3670 /* return next token without macro substitution */
3671 static inline void next_nomacro1(void)
3673 int t, c, is_long;
3674 TokenSym *ts;
3675 uint8_t *p, *p1;
3676 unsigned int h;
3678 p = file->buf_ptr;
3679 redo_no_start:
3680 c = *p;
3681 switch(c) {
3682 case ' ':
3683 case '\t':
3684 case '\f':
3685 case '\v':
3686 case '\r':
3687 p++;
3688 goto redo_no_start;
3690 case '\\':
3691 /* first look if it is in fact an end of buffer */
3692 if (p >= file->buf_end) {
3693 file->buf_ptr = p;
3694 handle_eob();
3695 p = file->buf_ptr;
3696 if (p >= file->buf_end)
3697 goto parse_eof;
3698 else
3699 goto redo_no_start;
3700 } else {
3701 file->buf_ptr = p;
3702 ch = *p;
3703 handle_stray();
3704 p = file->buf_ptr;
3705 goto redo_no_start;
3707 parse_eof:
3709 TCCState *s1 = tcc_state;
3710 if ((parse_flags & PARSE_FLAG_LINEFEED)
3711 && !(tok_flags & TOK_FLAG_EOF)) {
3712 tok_flags |= TOK_FLAG_EOF;
3713 tok = TOK_LINEFEED;
3714 goto keep_tok_flags;
3715 } else if (s1->include_stack_ptr == s1->include_stack ||
3716 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3717 /* no include left : end of file. */
3718 tok = TOK_EOF;
3719 } else {
3720 tok_flags &= ~TOK_FLAG_EOF;
3721 /* pop include file */
3723 /* test if previous '#endif' was after a #ifdef at
3724 start of file */
3725 if (tok_flags & TOK_FLAG_ENDIF) {
3726 #ifdef INC_DEBUG
3727 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3728 #endif
3729 add_cached_include(s1, file->inc_type, file->inc_filename,
3730 file->ifndef_macro_saved);
3733 /* add end of include file debug info */
3734 if (do_debug) {
3735 put_stabd(N_EINCL, 0, 0);
3737 /* pop include stack */
3738 tcc_close(file);
3739 s1->include_stack_ptr--;
3740 file = *s1->include_stack_ptr;
3741 p = file->buf_ptr;
3742 goto redo_no_start;
3745 break;
3747 case '\n':
3748 file->line_num++;
3749 tok_flags |= TOK_FLAG_BOL;
3750 p++;
3751 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3752 goto redo_no_start;
3753 tok = TOK_LINEFEED;
3754 goto keep_tok_flags;
3756 case '#':
3757 /* XXX: simplify */
3758 PEEKC(c, p);
3759 if ((tok_flags & TOK_FLAG_BOL) &&
3760 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3761 file->buf_ptr = p;
3762 preprocess(tok_flags & TOK_FLAG_BOF);
3763 p = file->buf_ptr;
3764 goto redo_no_start;
3765 } else {
3766 if (c == '#') {
3767 p++;
3768 tok = TOK_TWOSHARPS;
3769 } else {
3770 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3771 p = parse_line_comment(p - 1);
3772 goto redo_no_start;
3773 } else {
3774 tok = '#';
3778 break;
3780 case 'a': case 'b': case 'c': case 'd':
3781 case 'e': case 'f': case 'g': case 'h':
3782 case 'i': case 'j': case 'k': case 'l':
3783 case 'm': case 'n': case 'o': case 'p':
3784 case 'q': case 'r': case 's': case 't':
3785 case 'u': case 'v': case 'w': case 'x':
3786 case 'y': case 'z':
3787 case 'A': case 'B': case 'C': case 'D':
3788 case 'E': case 'F': case 'G': case 'H':
3789 case 'I': case 'J': case 'K':
3790 case 'M': case 'N': case 'O': case 'P':
3791 case 'Q': case 'R': case 'S': case 'T':
3792 case 'U': case 'V': case 'W': case 'X':
3793 case 'Y': case 'Z':
3794 case '_':
3795 parse_ident_fast:
3796 p1 = p;
3797 h = TOK_HASH_INIT;
3798 h = TOK_HASH_FUNC(h, c);
3799 p++;
3800 for(;;) {
3801 c = *p;
3802 if (!isidnum_table[c-CH_EOF])
3803 break;
3804 h = TOK_HASH_FUNC(h, c);
3805 p++;
3807 if (c != '\\') {
3808 TokenSym **pts;
3809 int len;
3811 /* fast case : no stray found, so we have the full token
3812 and we have already hashed it */
3813 len = p - p1;
3814 h &= (TOK_HASH_SIZE - 1);
3815 pts = &hash_ident[h];
3816 for(;;) {
3817 ts = *pts;
3818 if (!ts)
3819 break;
3820 if (ts->len == len && !memcmp(ts->str, p1, len))
3821 goto token_found;
3822 pts = &(ts->hash_next);
3824 ts = tok_alloc_new(pts, p1, len);
3825 token_found: ;
3826 } else {
3827 /* slower case */
3828 cstr_reset(&tokcstr);
3830 while (p1 < p) {
3831 cstr_ccat(&tokcstr, *p1);
3832 p1++;
3834 p--;
3835 PEEKC(c, p);
3836 parse_ident_slow:
3837 while (isidnum_table[c-CH_EOF]) {
3838 cstr_ccat(&tokcstr, c);
3839 PEEKC(c, p);
3841 ts = tok_alloc(tokcstr.data, tokcstr.size);
3843 tok = ts->tok;
3844 break;
3845 case 'L':
3846 t = p[1];
3847 if (t != '\\' && t != '\'' && t != '\"') {
3848 /* fast case */
3849 goto parse_ident_fast;
3850 } else {
3851 PEEKC(c, p);
3852 if (c == '\'' || c == '\"') {
3853 is_long = 1;
3854 goto str_const;
3855 } else {
3856 cstr_reset(&tokcstr);
3857 cstr_ccat(&tokcstr, 'L');
3858 goto parse_ident_slow;
3861 break;
3862 case '0': case '1': case '2': case '3':
3863 case '4': case '5': case '6': case '7':
3864 case '8': case '9':
3866 cstr_reset(&tokcstr);
3867 /* after the first digit, accept digits, alpha, '.' or sign if
3868 prefixed by 'eEpP' */
3869 parse_num:
3870 for(;;) {
3871 t = c;
3872 cstr_ccat(&tokcstr, c);
3873 PEEKC(c, p);
3874 if (!(isnum(c) || isid(c) || c == '.' ||
3875 ((c == '+' || c == '-') &&
3876 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3877 break;
3879 /* We add a trailing '\0' to ease parsing */
3880 cstr_ccat(&tokcstr, '\0');
3881 tokc.cstr = &tokcstr;
3882 tok = TOK_PPNUM;
3883 break;
3884 case '.':
3885 /* special dot handling because it can also start a number */
3886 PEEKC(c, p);
3887 if (isnum(c)) {
3888 cstr_reset(&tokcstr);
3889 cstr_ccat(&tokcstr, '.');
3890 goto parse_num;
3891 } else if (c == '.') {
3892 PEEKC(c, p);
3893 if (c != '.')
3894 expect("'.'");
3895 PEEKC(c, p);
3896 tok = TOK_DOTS;
3897 } else {
3898 tok = '.';
3900 break;
3901 case '\'':
3902 case '\"':
3903 is_long = 0;
3904 str_const:
3906 CString str;
3907 int sep;
3909 sep = c;
3911 /* parse the string */
3912 cstr_new(&str);
3913 p = parse_pp_string(p, sep, &str);
3914 cstr_ccat(&str, '\0');
3916 /* eval the escape (should be done as TOK_PPNUM) */
3917 cstr_reset(&tokcstr);
3918 parse_escape_string(&tokcstr, str.data, is_long);
3919 cstr_free(&str);
3921 if (sep == '\'') {
3922 int char_size;
3923 /* XXX: make it portable */
3924 if (!is_long)
3925 char_size = 1;
3926 else
3927 char_size = sizeof(nwchar_t);
3928 if (tokcstr.size <= char_size)
3929 error("empty character constant");
3930 if (tokcstr.size > 2 * char_size)
3931 warning("multi-character character constant");
3932 if (!is_long) {
3933 tokc.i = *(int8_t *)tokcstr.data;
3934 tok = TOK_CCHAR;
3935 } else {
3936 tokc.i = *(nwchar_t *)tokcstr.data;
3937 tok = TOK_LCHAR;
3939 } else {
3940 tokc.cstr = &tokcstr;
3941 if (!is_long)
3942 tok = TOK_STR;
3943 else
3944 tok = TOK_LSTR;
3947 break;
3949 case '<':
3950 PEEKC(c, p);
3951 if (c == '=') {
3952 p++;
3953 tok = TOK_LE;
3954 } else if (c == '<') {
3955 PEEKC(c, p);
3956 if (c == '=') {
3957 p++;
3958 tok = TOK_A_SHL;
3959 } else {
3960 tok = TOK_SHL;
3962 } else {
3963 tok = TOK_LT;
3965 break;
3967 case '>':
3968 PEEKC(c, p);
3969 if (c == '=') {
3970 p++;
3971 tok = TOK_GE;
3972 } else if (c == '>') {
3973 PEEKC(c, p);
3974 if (c == '=') {
3975 p++;
3976 tok = TOK_A_SAR;
3977 } else {
3978 tok = TOK_SAR;
3980 } else {
3981 tok = TOK_GT;
3983 break;
3985 case '&':
3986 PEEKC(c, p);
3987 if (c == '&') {
3988 p++;
3989 tok = TOK_LAND;
3990 } else if (c == '=') {
3991 p++;
3992 tok = TOK_A_AND;
3993 } else {
3994 tok = '&';
3996 break;
3998 case '|':
3999 PEEKC(c, p);
4000 if (c == '|') {
4001 p++;
4002 tok = TOK_LOR;
4003 } else if (c == '=') {
4004 p++;
4005 tok = TOK_A_OR;
4006 } else {
4007 tok = '|';
4009 break;
4011 case '+':
4012 PEEKC(c, p);
4013 if (c == '+') {
4014 p++;
4015 tok = TOK_INC;
4016 } else if (c == '=') {
4017 p++;
4018 tok = TOK_A_ADD;
4019 } else {
4020 tok = '+';
4022 break;
4024 case '-':
4025 PEEKC(c, p);
4026 if (c == '-') {
4027 p++;
4028 tok = TOK_DEC;
4029 } else if (c == '=') {
4030 p++;
4031 tok = TOK_A_SUB;
4032 } else if (c == '>') {
4033 p++;
4034 tok = TOK_ARROW;
4035 } else {
4036 tok = '-';
4038 break;
4040 PARSE2('!', '!', '=', TOK_NE)
4041 PARSE2('=', '=', '=', TOK_EQ)
4042 PARSE2('*', '*', '=', TOK_A_MUL)
4043 PARSE2('%', '%', '=', TOK_A_MOD)
4044 PARSE2('^', '^', '=', TOK_A_XOR)
4046 /* comments or operator */
4047 case '/':
4048 PEEKC(c, p);
4049 if (c == '*') {
4050 p = parse_comment(p);
4051 goto redo_no_start;
4052 } else if (c == '/') {
4053 p = parse_line_comment(p);
4054 goto redo_no_start;
4055 } else if (c == '=') {
4056 p++;
4057 tok = TOK_A_DIV;
4058 } else {
4059 tok = '/';
4061 break;
4063 /* simple tokens */
4064 case '(':
4065 case ')':
4066 case '[':
4067 case ']':
4068 case '{':
4069 case '}':
4070 case ',':
4071 case ';':
4072 case ':':
4073 case '?':
4074 case '~':
4075 case '$': /* only used in assembler */
4076 case '@': /* dito */
4077 tok = c;
4078 p++;
4079 break;
4080 default:
4081 error("unrecognized character \\x%02x", c);
4082 break;
4084 tok_flags = 0;
4085 keep_tok_flags:
4086 file->buf_ptr = p;
4087 #if defined(PARSE_DEBUG)
4088 printf("token = %s\n", get_tok_str(tok, &tokc));
4089 #endif
4092 /* return next token without macro substitution. Can read input from
4093 macro_ptr buffer */
4094 static void next_nomacro(void)
4096 if (macro_ptr) {
4097 redo:
4098 tok = *macro_ptr;
4099 if (tok) {
4100 TOK_GET(tok, macro_ptr, tokc);
4101 if (tok == TOK_LINENUM) {
4102 file->line_num = tokc.i;
4103 goto redo;
4106 } else {
4107 next_nomacro1();
4111 /* substitute args in macro_str and return allocated string */
4112 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4114 int *st, last_tok, t, notfirst;
4115 Sym *s;
4116 CValue cval;
4117 TokenString str;
4118 CString cstr;
4120 tok_str_new(&str);
4121 last_tok = 0;
4122 while(1) {
4123 TOK_GET(t, macro_str, cval);
4124 if (!t)
4125 break;
4126 if (t == '#') {
4127 /* stringize */
4128 TOK_GET(t, macro_str, cval);
4129 if (!t)
4130 break;
4131 s = sym_find2(args, t);
4132 if (s) {
4133 cstr_new(&cstr);
4134 st = (int *)s->c;
4135 notfirst = 0;
4136 while (*st) {
4137 if (notfirst)
4138 cstr_ccat(&cstr, ' ');
4139 TOK_GET(t, st, cval);
4140 cstr_cat(&cstr, get_tok_str(t, &cval));
4141 #ifndef PP_NOSPACES
4142 notfirst = 1;
4143 #endif
4145 cstr_ccat(&cstr, '\0');
4146 #ifdef PP_DEBUG
4147 printf("stringize: %s\n", (char *)cstr.data);
4148 #endif
4149 /* add string */
4150 cval.cstr = &cstr;
4151 tok_str_add2(&str, TOK_STR, &cval);
4152 cstr_free(&cstr);
4153 } else {
4154 tok_str_add2(&str, t, &cval);
4156 } else if (t >= TOK_IDENT) {
4157 s = sym_find2(args, t);
4158 if (s) {
4159 st = (int *)s->c;
4160 /* if '##' is present before or after, no arg substitution */
4161 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4162 /* special case for var arg macros : ## eats the
4163 ',' if empty VA_ARGS variable. */
4164 /* XXX: test of the ',' is not 100%
4165 reliable. should fix it to avoid security
4166 problems */
4167 if (gnu_ext && s->type.t &&
4168 last_tok == TOK_TWOSHARPS &&
4169 str.len >= 2 && str.str[str.len - 2] == ',') {
4170 if (*st == 0) {
4171 /* suppress ',' '##' */
4172 str.len -= 2;
4173 } else {
4174 /* suppress '##' and add variable */
4175 str.len--;
4176 goto add_var;
4178 } else {
4179 int t1;
4180 add_var:
4181 for(;;) {
4182 TOK_GET(t1, st, cval);
4183 if (!t1)
4184 break;
4185 tok_str_add2(&str, t1, &cval);
4188 } else {
4189 /* NOTE: the stream cannot be read when macro
4190 substituing an argument */
4191 macro_subst(&str, nested_list, st, NULL);
4193 } else {
4194 tok_str_add(&str, t);
4196 } else {
4197 tok_str_add2(&str, t, &cval);
4199 last_tok = t;
4201 tok_str_add(&str, 0);
4202 return str.str;
4205 static char const ab_month_name[12][4] =
4207 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4208 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4211 /* do macro substitution of current token with macro 's' and add
4212 result to (tok_str,tok_len). 'nested_list' is the list of all
4213 macros we got inside to avoid recursing. Return non zero if no
4214 substitution needs to be done */
4215 static int macro_subst_tok(TokenString *tok_str,
4216 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4218 Sym *args, *sa, *sa1;
4219 int mstr_allocated, parlevel, *mstr, t, t1;
4220 TokenString str;
4221 char *cstrval;
4222 CValue cval;
4223 CString cstr;
4224 char buf[32];
4226 /* if symbol is a macro, prepare substitution */
4227 /* special macros */
4228 if (tok == TOK___LINE__) {
4229 snprintf(buf, sizeof(buf), "%d", file->line_num);
4230 cstrval = buf;
4231 t1 = TOK_PPNUM;
4232 goto add_cstr1;
4233 } else if (tok == TOK___FILE__) {
4234 cstrval = file->filename;
4235 goto add_cstr;
4236 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4237 time_t ti;
4238 struct tm *tm;
4240 time(&ti);
4241 tm = localtime(&ti);
4242 if (tok == TOK___DATE__) {
4243 snprintf(buf, sizeof(buf), "%s %2d %d",
4244 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4245 } else {
4246 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4247 tm->tm_hour, tm->tm_min, tm->tm_sec);
4249 cstrval = buf;
4250 add_cstr:
4251 t1 = TOK_STR;
4252 add_cstr1:
4253 cstr_new(&cstr);
4254 cstr_cat(&cstr, cstrval);
4255 cstr_ccat(&cstr, '\0');
4256 cval.cstr = &cstr;
4257 tok_str_add2(tok_str, t1, &cval);
4258 cstr_free(&cstr);
4259 } else {
4260 mstr = (int *)s->c;
4261 mstr_allocated = 0;
4262 if (s->type.t == MACRO_FUNC) {
4263 /* NOTE: we do not use next_nomacro to avoid eating the
4264 next token. XXX: find better solution */
4265 redo:
4266 if (macro_ptr) {
4267 t = *macro_ptr;
4268 if (t == 0 && can_read_stream) {
4269 /* end of macro stream: we must look at the token
4270 after in the file */
4271 struct macro_level *ml = *can_read_stream;
4272 macro_ptr = NULL;
4273 if (ml)
4275 macro_ptr = ml->p;
4276 ml->p = NULL;
4277 *can_read_stream = ml -> prev;
4279 goto redo;
4281 } else {
4282 /* XXX: incorrect with comments */
4283 ch = file->buf_ptr[0];
4284 while (is_space(ch) || ch == '\n')
4285 cinp();
4286 t = ch;
4288 if (t != '(') /* no macro subst */
4289 return -1;
4291 /* argument macro */
4292 next_nomacro();
4293 next_nomacro();
4294 args = NULL;
4295 sa = s->next;
4296 /* NOTE: empty args are allowed, except if no args */
4297 for(;;) {
4298 /* handle '()' case */
4299 if (!args && !sa && tok == ')')
4300 break;
4301 if (!sa)
4302 error("macro '%s' used with too many args",
4303 get_tok_str(s->v, 0));
4304 tok_str_new(&str);
4305 parlevel = 0;
4306 /* NOTE: non zero sa->t indicates VA_ARGS */
4307 while ((parlevel > 0 ||
4308 (tok != ')' &&
4309 (tok != ',' || sa->type.t))) &&
4310 tok != -1) {
4311 if (tok == '(')
4312 parlevel++;
4313 else if (tok == ')')
4314 parlevel--;
4315 if (tok != TOK_LINEFEED)
4316 tok_str_add2(&str, tok, &tokc);
4317 next_nomacro();
4319 tok_str_add(&str, 0);
4320 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (long)str.str);
4321 sa = sa->next;
4322 if (tok == ')') {
4323 /* special case for gcc var args: add an empty
4324 var arg argument if it is omitted */
4325 if (sa && sa->type.t && gnu_ext)
4326 continue;
4327 else
4328 break;
4330 if (tok != ',')
4331 expect(",");
4332 next_nomacro();
4334 if (sa) {
4335 error("macro '%s' used with too few args",
4336 get_tok_str(s->v, 0));
4339 /* now subst each arg */
4340 mstr = macro_arg_subst(nested_list, mstr, args);
4341 /* free memory */
4342 sa = args;
4343 while (sa) {
4344 sa1 = sa->prev;
4345 tok_str_free((int *)sa->c);
4346 sym_free(sa);
4347 sa = sa1;
4349 mstr_allocated = 1;
4351 sym_push2(nested_list, s->v, 0, 0);
4352 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4353 /* pop nested defined symbol */
4354 sa1 = *nested_list;
4355 *nested_list = sa1->prev;
4356 sym_free(sa1);
4357 if (mstr_allocated)
4358 tok_str_free(mstr);
4360 return 0;
4363 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4364 return the resulting string (which must be freed). */
4365 static inline int *macro_twosharps(const int *macro_str)
4367 TokenSym *ts;
4368 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4369 int t;
4370 const char *p1, *p2;
4371 CValue cval;
4372 TokenString macro_str1;
4373 CString cstr;
4375 start_macro_ptr = macro_str;
4376 /* we search the first '##' */
4377 for(;;) {
4378 macro_ptr1 = macro_str;
4379 TOK_GET(t, macro_str, cval);
4380 /* nothing more to do if end of string */
4381 if (t == 0)
4382 return NULL;
4383 if (*macro_str == TOK_TWOSHARPS)
4384 break;
4387 /* we saw '##', so we need more processing to handle it */
4388 cstr_new(&cstr);
4389 tok_str_new(&macro_str1);
4390 tok = t;
4391 tokc = cval;
4393 /* add all tokens seen so far */
4394 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4395 TOK_GET(t, ptr, cval);
4396 tok_str_add2(&macro_str1, t, &cval);
4398 saved_macro_ptr = macro_ptr;
4399 /* XXX: get rid of the use of macro_ptr here */
4400 macro_ptr = (int *)macro_str;
4401 for(;;) {
4402 while (*macro_ptr == TOK_TWOSHARPS) {
4403 macro_ptr++;
4404 macro_ptr1 = macro_ptr;
4405 t = *macro_ptr;
4406 if (t) {
4407 TOK_GET(t, macro_ptr, cval);
4408 /* We concatenate the two tokens if we have an
4409 identifier or a preprocessing number */
4410 cstr_reset(&cstr);
4411 p1 = get_tok_str(tok, &tokc);
4412 cstr_cat(&cstr, p1);
4413 p2 = get_tok_str(t, &cval);
4414 cstr_cat(&cstr, p2);
4415 cstr_ccat(&cstr, '\0');
4417 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4418 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4419 if (tok == TOK_PPNUM) {
4420 /* if number, then create a number token */
4421 /* NOTE: no need to allocate because
4422 tok_str_add2() does it */
4423 cstr_reset(&tokcstr);
4424 tokcstr = cstr;
4425 cstr_new(&cstr);
4426 tokc.cstr = &tokcstr;
4427 } else {
4428 /* if identifier, we must do a test to
4429 validate we have a correct identifier */
4430 if (t == TOK_PPNUM) {
4431 const char *p;
4432 int c;
4434 p = p2;
4435 for(;;) {
4436 c = *p;
4437 if (c == '\0')
4438 break;
4439 p++;
4440 if (!isnum(c) && !isid(c))
4441 goto error_pasting;
4444 ts = tok_alloc(cstr.data, strlen(cstr.data));
4445 tok = ts->tok; /* modify current token */
4447 } else {
4448 const char *str = cstr.data;
4449 const unsigned char *q;
4451 /* we look for a valid token */
4452 /* XXX: do more extensive checks */
4453 if (!strcmp(str, ">>=")) {
4454 tok = TOK_A_SAR;
4455 } else if (!strcmp(str, "<<=")) {
4456 tok = TOK_A_SHL;
4457 } else if (strlen(str) == 2) {
4458 /* search in two bytes table */
4459 q = tok_two_chars;
4460 for(;;) {
4461 if (!*q)
4462 goto error_pasting;
4463 if (q[0] == str[0] && q[1] == str[1])
4464 break;
4465 q += 3;
4467 tok = q[2];
4468 } else {
4469 error_pasting:
4470 /* NOTE: because get_tok_str use a static buffer,
4471 we must save it */
4472 cstr_reset(&cstr);
4473 p1 = get_tok_str(tok, &tokc);
4474 cstr_cat(&cstr, p1);
4475 cstr_ccat(&cstr, '\0');
4476 p2 = get_tok_str(t, &cval);
4477 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4478 /* cannot merge tokens: just add them separately */
4479 tok_str_add2(&macro_str1, tok, &tokc);
4480 /* XXX: free associated memory ? */
4481 tok = t;
4482 tokc = cval;
4487 tok_str_add2(&macro_str1, tok, &tokc);
4488 next_nomacro();
4489 if (tok == 0)
4490 break;
4492 macro_ptr = (int *)saved_macro_ptr;
4493 cstr_free(&cstr);
4494 tok_str_add(&macro_str1, 0);
4495 return macro_str1.str;
4499 /* do macro substitution of macro_str and add result to
4500 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4501 inside to avoid recursing. */
4502 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4503 const int *macro_str, struct macro_level ** can_read_stream)
4505 Sym *s;
4506 int *macro_str1;
4507 const int *ptr;
4508 int t, ret;
4509 CValue cval;
4510 struct macro_level ml;
4512 /* first scan for '##' operator handling */
4513 ptr = macro_str;
4514 macro_str1 = macro_twosharps(ptr);
4515 if (macro_str1)
4516 ptr = macro_str1;
4517 while (1) {
4518 /* NOTE: ptr == NULL can only happen if tokens are read from
4519 file stream due to a macro function call */
4520 if (ptr == NULL)
4521 break;
4522 TOK_GET(t, ptr, cval);
4523 if (t == 0)
4524 break;
4525 s = define_find(t);
4526 if (s != NULL) {
4527 /* if nested substitution, do nothing */
4528 if (sym_find2(*nested_list, t))
4529 goto no_subst;
4530 ml.p = macro_ptr;
4531 if (can_read_stream)
4532 ml.prev = *can_read_stream, *can_read_stream = &ml;
4533 macro_ptr = (int *)ptr;
4534 tok = t;
4535 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4536 ptr = (int *)macro_ptr;
4537 macro_ptr = ml.p;
4538 if (can_read_stream && *can_read_stream == &ml)
4539 *can_read_stream = ml.prev;
4540 if (ret != 0)
4541 goto no_subst;
4542 } else {
4543 no_subst:
4544 tok_str_add2(tok_str, t, &cval);
4547 if (macro_str1)
4548 tok_str_free(macro_str1);
4551 /* return next token with macro substitution */
4552 static void next(void)
4554 Sym *nested_list, *s;
4555 TokenString str;
4556 struct macro_level *ml;
4558 redo:
4559 next_nomacro();
4560 if (!macro_ptr) {
4561 /* if not reading from macro substituted string, then try
4562 to substitute macros */
4563 if (tok >= TOK_IDENT &&
4564 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4565 s = define_find(tok);
4566 if (s) {
4567 /* we have a macro: we try to substitute */
4568 tok_str_new(&str);
4569 nested_list = NULL;
4570 ml = NULL;
4571 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4572 /* substitution done, NOTE: maybe empty */
4573 tok_str_add(&str, 0);
4574 macro_ptr = str.str;
4575 macro_ptr_allocated = str.str;
4576 goto redo;
4580 } else {
4581 if (tok == 0) {
4582 /* end of macro or end of unget buffer */
4583 if (unget_buffer_enabled) {
4584 macro_ptr = unget_saved_macro_ptr;
4585 unget_buffer_enabled = 0;
4586 } else {
4587 /* end of macro string: free it */
4588 tok_str_free(macro_ptr_allocated);
4589 macro_ptr = NULL;
4591 goto redo;
4595 /* convert preprocessor tokens into C tokens */
4596 if (tok == TOK_PPNUM &&
4597 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4598 parse_number((char *)tokc.cstr->data);
4602 /* push back current token and set current token to 'last_tok'. Only
4603 identifier case handled for labels. */
4604 static inline void unget_tok(int last_tok)
4606 int i, n;
4607 int *q;
4608 unget_saved_macro_ptr = macro_ptr;
4609 unget_buffer_enabled = 1;
4610 q = unget_saved_buffer;
4611 macro_ptr = q;
4612 *q++ = tok;
4613 n = tok_ext_size(tok) - 1;
4614 for(i=0;i<n;i++)
4615 *q++ = tokc.tab[i];
4616 *q = 0; /* end of token string */
4617 tok = last_tok;
4621 void swap(int *p, int *q)
4623 int t;
4624 t = *p;
4625 *p = *q;
4626 *q = t;
4629 void vsetc(CType *type, int r, CValue *vc)
4631 int v;
4633 if (vtop >= vstack + (VSTACK_SIZE - 1))
4634 error("memory full");
4635 /* cannot let cpu flags if other instruction are generated. Also
4636 avoid leaving VT_JMP anywhere except on the top of the stack
4637 because it would complicate the code generator. */
4638 if (vtop >= vstack) {
4639 v = vtop->r & VT_VALMASK;
4640 if (v == VT_CMP || (v & ~1) == VT_JMP)
4641 gv(RC_INT);
4643 vtop++;
4644 vtop->type = *type;
4645 vtop->r = r;
4646 vtop->r2 = VT_CONST;
4647 vtop->c = *vc;
4650 /* push integer constant */
4651 void vpushi(int v)
4653 CValue cval;
4654 cval.i = v;
4655 vsetc(&int_type, VT_CONST, &cval);
4658 /* Return a static symbol pointing to a section */
4659 static Sym *get_sym_ref(CType *type, Section *sec,
4660 unsigned long offset, unsigned long size)
4662 int v;
4663 Sym *sym;
4665 v = anon_sym++;
4666 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4667 sym->type.ref = type->ref;
4668 sym->r = VT_CONST | VT_SYM;
4669 put_extern_sym(sym, sec, offset, size);
4670 return sym;
4673 /* push a reference to a section offset by adding a dummy symbol */
4674 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4676 CValue cval;
4678 cval.ul = 0;
4679 vsetc(type, VT_CONST | VT_SYM, &cval);
4680 vtop->sym = get_sym_ref(type, sec, offset, size);
4683 /* define a new external reference to a symbol 'v' of type 'u' */
4684 static Sym *external_global_sym(int v, CType *type, int r)
4686 Sym *s;
4688 s = sym_find(v);
4689 if (!s) {
4690 /* push forward reference */
4691 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4692 s->type.ref = type->ref;
4693 s->r = r | VT_CONST | VT_SYM;
4695 return s;
4698 /* define a new external reference to a symbol 'v' of type 'u' */
4699 static Sym *external_sym(int v, CType *type, int r)
4701 Sym *s;
4703 s = sym_find(v);
4704 if (!s) {
4705 /* push forward reference */
4706 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4707 s->type.t |= VT_EXTERN;
4708 } else {
4709 if (!is_compatible_types(&s->type, type))
4710 error("incompatible types for redefinition of '%s'",
4711 get_tok_str(v, NULL));
4713 return s;
4716 /* push a reference to global symbol v */
4717 static void vpush_global_sym(CType *type, int v)
4719 Sym *sym;
4720 CValue cval;
4722 sym = external_global_sym(v, type, 0);
4723 cval.ul = 0;
4724 vsetc(type, VT_CONST | VT_SYM, &cval);
4725 vtop->sym = sym;
4728 void vset(CType *type, int r, int v)
4730 CValue cval;
4732 cval.i = v;
4733 vsetc(type, r, &cval);
4736 void vseti(int r, int v)
4738 CType type;
4739 type.t = VT_INT;
4740 vset(&type, r, v);
4743 void vswap(void)
4745 SValue tmp;
4747 tmp = vtop[0];
4748 vtop[0] = vtop[-1];
4749 vtop[-1] = tmp;
4752 void vpushv(SValue *v)
4754 if (vtop >= vstack + (VSTACK_SIZE - 1))
4755 error("memory full");
4756 vtop++;
4757 *vtop = *v;
4760 void vdup(void)
4762 vpushv(vtop);
4765 /* save r to the memory stack, and mark it as being free */
4766 void save_reg(int r)
4768 int l, saved, size, align;
4769 SValue *p, sv;
4770 CType *type;
4772 /* modify all stack values */
4773 saved = 0;
4774 l = 0;
4775 for(p=vstack;p<=vtop;p++) {
4776 if ((p->r & VT_VALMASK) == r ||
4777 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4778 /* must save value on stack if not already done */
4779 if (!saved) {
4780 /* NOTE: must reload 'r' because r might be equal to r2 */
4781 r = p->r & VT_VALMASK;
4782 /* store register in the stack */
4783 type = &p->type;
4784 #ifndef TCC_TARGET_X86_64
4785 if ((p->r & VT_LVAL) ||
4786 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4787 type = &int_type;
4788 #else
4789 if (p->r & VT_LVAL)
4790 type = &char_pointer_type;
4791 #endif
4792 size = type_size(type, &align);
4793 loc = (loc - size) & -align;
4794 sv.type.t = type->t;
4795 sv.r = VT_LOCAL | VT_LVAL;
4796 sv.c.ul = loc;
4797 store(r, &sv);
4798 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
4799 /* x86 specific: need to pop fp register ST0 if saved */
4800 if (r == TREG_ST0) {
4801 o(0xd9dd); /* fstp %st(1) */
4803 #endif
4804 #ifndef TCC_TARGET_X86_64
4805 /* special long long case */
4806 if ((type->t & VT_BTYPE) == VT_LLONG) {
4807 sv.c.ul += 4;
4808 store(p->r2, &sv);
4810 #endif
4811 l = loc;
4812 saved = 1;
4814 /* mark that stack entry as being saved on the stack */
4815 if (p->r & VT_LVAL) {
4816 /* also clear the bounded flag because the
4817 relocation address of the function was stored in
4818 p->c.ul */
4819 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4820 } else {
4821 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4823 p->r2 = VT_CONST;
4824 p->c.ul = l;
4829 /* find a register of class 'rc2' with at most one reference on stack.
4830 * If none, call get_reg(rc) */
4831 int get_reg_ex(int rc, int rc2)
4833 int r;
4834 SValue *p;
4836 for(r=0;r<NB_REGS;r++) {
4837 if (reg_classes[r] & rc2) {
4838 int n;
4839 n=0;
4840 for(p = vstack; p <= vtop; p++) {
4841 if ((p->r & VT_VALMASK) == r ||
4842 (p->r2 & VT_VALMASK) == r)
4843 n++;
4845 if (n <= 1)
4846 return r;
4849 return get_reg(rc);
4852 /* find a free register of class 'rc'. If none, save one register */
4853 int get_reg(int rc)
4855 int r;
4856 SValue *p;
4858 /* find a free register */
4859 for(r=0;r<NB_REGS;r++) {
4860 if (reg_classes[r] & rc) {
4861 for(p=vstack;p<=vtop;p++) {
4862 if ((p->r & VT_VALMASK) == r ||
4863 (p->r2 & VT_VALMASK) == r)
4864 goto notfound;
4866 return r;
4868 notfound: ;
4871 /* no register left : free the first one on the stack (VERY
4872 IMPORTANT to start from the bottom to ensure that we don't
4873 spill registers used in gen_opi()) */
4874 for(p=vstack;p<=vtop;p++) {
4875 r = p->r & VT_VALMASK;
4876 if (r < VT_CONST && (reg_classes[r] & rc))
4877 goto save_found;
4878 /* also look at second register (if long long) */
4879 r = p->r2 & VT_VALMASK;
4880 if (r < VT_CONST && (reg_classes[r] & rc)) {
4881 save_found:
4882 save_reg(r);
4883 return r;
4886 /* Should never comes here */
4887 return -1;
4890 /* save registers up to (vtop - n) stack entry */
4891 void save_regs(int n)
4893 int r;
4894 SValue *p, *p1;
4895 p1 = vtop - n;
4896 for(p = vstack;p <= p1; p++) {
4897 r = p->r & VT_VALMASK;
4898 if (r < VT_CONST) {
4899 save_reg(r);
4904 /* move register 's' to 'r', and flush previous value of r to memory
4905 if needed */
4906 void move_reg(int r, int s)
4908 SValue sv;
4910 if (r != s) {
4911 save_reg(r);
4912 sv.type.t = VT_INT;
4913 sv.r = s;
4914 sv.c.ul = 0;
4915 load(r, &sv);
4919 /* get address of vtop (vtop MUST BE an lvalue) */
4920 void gaddrof(void)
4922 vtop->r &= ~VT_LVAL;
4923 /* tricky: if saved lvalue, then we can go back to lvalue */
4924 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4925 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4928 #ifdef CONFIG_TCC_BCHECK
4929 /* generate lvalue bound code */
4930 void gbound(void)
4932 int lval_type;
4933 CType type1;
4935 vtop->r &= ~VT_MUSTBOUND;
4936 /* if lvalue, then use checking code before dereferencing */
4937 if (vtop->r & VT_LVAL) {
4938 /* if not VT_BOUNDED value, then make one */
4939 if (!(vtop->r & VT_BOUNDED)) {
4940 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4941 /* must save type because we must set it to int to get pointer */
4942 type1 = vtop->type;
4943 vtop->type.t = VT_INT;
4944 gaddrof();
4945 vpushi(0);
4946 gen_bounded_ptr_add();
4947 vtop->r |= lval_type;
4948 vtop->type = type1;
4950 /* then check for dereferencing */
4951 gen_bounded_ptr_deref();
4954 #endif
4956 /* store vtop a register belonging to class 'rc'. lvalues are
4957 converted to values. Cannot be used if cannot be converted to
4958 register value (such as structures). */
4959 int gv(int rc)
4961 int r, rc2, bit_pos, bit_size, size, align, i;
4963 /* NOTE: get_reg can modify vstack[] */
4964 if (vtop->type.t & VT_BITFIELD) {
4965 CType type;
4966 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4967 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4968 /* remove bit field info to avoid loops */
4969 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4970 /* cast to int to propagate signedness in following ops */
4971 type.t = VT_INT;
4972 if((vtop->type.t & VT_UNSIGNED) ||
4973 (vtop->type.t & VT_BTYPE) == VT_BOOL)
4974 type.t |= VT_UNSIGNED;
4975 gen_cast(&type);
4976 /* generate shifts */
4977 vpushi(32 - (bit_pos + bit_size));
4978 gen_op(TOK_SHL);
4979 vpushi(32 - bit_size);
4980 /* NOTE: transformed to SHR if unsigned */
4981 gen_op(TOK_SAR);
4982 r = gv(rc);
4983 } else {
4984 if (is_float(vtop->type.t) &&
4985 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4986 Sym *sym;
4987 int *ptr;
4988 unsigned long offset;
4989 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4990 CValue check;
4991 #endif
4993 /* XXX: unify with initializers handling ? */
4994 /* CPUs usually cannot use float constants, so we store them
4995 generically in data segment */
4996 size = type_size(&vtop->type, &align);
4997 offset = (data_section->data_offset + align - 1) & -align;
4998 data_section->data_offset = offset;
4999 /* XXX: not portable yet */
5000 #if defined(__i386__) || defined(__x86_64__)
5001 /* Zero pad x87 tenbyte long doubles */
5002 if (size == LDOUBLE_SIZE)
5003 vtop->c.tab[2] &= 0xffff;
5004 #endif
5005 ptr = section_ptr_add(data_section, size);
5006 size = size >> 2;
5007 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5008 check.d = 1;
5009 if(check.tab[0])
5010 for(i=0;i<size;i++)
5011 ptr[i] = vtop->c.tab[size-1-i];
5012 else
5013 #endif
5014 for(i=0;i<size;i++)
5015 ptr[i] = vtop->c.tab[i];
5016 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
5017 vtop->r |= VT_LVAL | VT_SYM;
5018 vtop->sym = sym;
5019 vtop->c.ul = 0;
5021 #ifdef CONFIG_TCC_BCHECK
5022 if (vtop->r & VT_MUSTBOUND)
5023 gbound();
5024 #endif
5026 r = vtop->r & VT_VALMASK;
5027 rc2 = RC_INT;
5028 if (rc == RC_IRET)
5029 rc2 = RC_LRET;
5030 /* need to reload if:
5031 - constant
5032 - lvalue (need to dereference pointer)
5033 - already a register, but not in the right class */
5034 if (r >= VT_CONST ||
5035 (vtop->r & VT_LVAL) ||
5036 !(reg_classes[r] & rc) ||
5037 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
5038 !(reg_classes[vtop->r2] & rc2))) {
5039 r = get_reg(rc);
5040 #ifndef TCC_TARGET_X86_64
5041 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
5042 int r2;
5043 unsigned long long ll;
5044 /* two register type load : expand to two words
5045 temporarily */
5046 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
5047 /* load constant */
5048 ll = vtop->c.ull;
5049 vtop->c.ui = ll; /* first word */
5050 load(r, vtop);
5051 vtop->r = r; /* save register value */
5052 vpushi(ll >> 32); /* second word */
5053 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
5054 (vtop->r & VT_LVAL)) {
5055 /* We do not want to modifier the long long
5056 pointer here, so the safest (and less
5057 efficient) is to save all the other registers
5058 in the stack. XXX: totally inefficient. */
5059 save_regs(1);
5060 /* load from memory */
5061 load(r, vtop);
5062 vdup();
5063 vtop[-1].r = r; /* save register value */
5064 /* increment pointer to get second word */
5065 vtop->type.t = VT_INT;
5066 gaddrof();
5067 vpushi(4);
5068 gen_op('+');
5069 vtop->r |= VT_LVAL;
5070 } else {
5071 /* move registers */
5072 load(r, vtop);
5073 vdup();
5074 vtop[-1].r = r; /* save register value */
5075 vtop->r = vtop[-1].r2;
5077 /* allocate second register */
5078 r2 = get_reg(rc2);
5079 load(r2, vtop);
5080 vpop();
5081 /* write second register */
5082 vtop->r2 = r2;
5083 } else
5084 #endif
5085 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5086 int t1, t;
5087 /* lvalue of scalar type : need to use lvalue type
5088 because of possible cast */
5089 t = vtop->type.t;
5090 t1 = t;
5091 /* compute memory access type */
5092 if (vtop->r & VT_LVAL_BYTE)
5093 t = VT_BYTE;
5094 else if (vtop->r & VT_LVAL_SHORT)
5095 t = VT_SHORT;
5096 if (vtop->r & VT_LVAL_UNSIGNED)
5097 t |= VT_UNSIGNED;
5098 vtop->type.t = t;
5099 load(r, vtop);
5100 /* restore wanted type */
5101 vtop->type.t = t1;
5102 } else {
5103 /* one register type load */
5104 load(r, vtop);
5107 vtop->r = r;
5108 #ifdef TCC_TARGET_C67
5109 /* uses register pairs for doubles */
5110 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5111 vtop->r2 = r+1;
5112 #endif
5114 return r;
5117 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5118 void gv2(int rc1, int rc2)
5120 int v;
5122 /* generate more generic register first. But VT_JMP or VT_CMP
5123 values must be generated first in all cases to avoid possible
5124 reload errors */
5125 v = vtop[0].r & VT_VALMASK;
5126 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5127 vswap();
5128 gv(rc1);
5129 vswap();
5130 gv(rc2);
5131 /* test if reload is needed for first register */
5132 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5133 vswap();
5134 gv(rc1);
5135 vswap();
5137 } else {
5138 gv(rc2);
5139 vswap();
5140 gv(rc1);
5141 vswap();
5142 /* test if reload is needed for first register */
5143 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5144 gv(rc2);
5149 /* expand long long on stack in two int registers */
5150 void lexpand(void)
5152 int u;
5154 u = vtop->type.t & VT_UNSIGNED;
5155 gv(RC_INT);
5156 vdup();
5157 vtop[0].r = vtop[-1].r2;
5158 vtop[0].r2 = VT_CONST;
5159 vtop[-1].r2 = VT_CONST;
5160 vtop[0].type.t = VT_INT | u;
5161 vtop[-1].type.t = VT_INT | u;
5164 #ifdef TCC_TARGET_ARM
5165 /* expand long long on stack */
5166 void lexpand_nr(void)
5168 int u,v;
5170 u = vtop->type.t & VT_UNSIGNED;
5171 vdup();
5172 vtop->r2 = VT_CONST;
5173 vtop->type.t = VT_INT | u;
5174 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5175 if (v == VT_CONST) {
5176 vtop[-1].c.ui = vtop->c.ull;
5177 vtop->c.ui = vtop->c.ull >> 32;
5178 vtop->r = VT_CONST;
5179 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5180 vtop->c.ui += 4;
5181 vtop->r = vtop[-1].r;
5182 } else if (v > VT_CONST) {
5183 vtop--;
5184 lexpand();
5185 } else
5186 vtop->r = vtop[-1].r2;
5187 vtop[-1].r2 = VT_CONST;
5188 vtop[-1].type.t = VT_INT | u;
5190 #endif
5192 /* build a long long from two ints */
5193 void lbuild(int t)
5195 gv2(RC_INT, RC_INT);
5196 vtop[-1].r2 = vtop[0].r;
5197 vtop[-1].type.t = t;
5198 vpop();
5201 /* rotate n first stack elements to the bottom
5202 I1 ... In -> I2 ... In I1 [top is right]
5204 void vrotb(int n)
5206 int i;
5207 SValue tmp;
5209 tmp = vtop[-n + 1];
5210 for(i=-n+1;i!=0;i++)
5211 vtop[i] = vtop[i+1];
5212 vtop[0] = tmp;
5215 /* rotate n first stack elements to the top
5216 I1 ... In -> In I1 ... I(n-1) [top is right]
5218 void vrott(int n)
5220 int i;
5221 SValue tmp;
5223 tmp = vtop[0];
5224 for(i = 0;i < n - 1; i++)
5225 vtop[-i] = vtop[-i - 1];
5226 vtop[-n + 1] = tmp;
5229 #ifdef TCC_TARGET_ARM
5230 /* like vrott but in other direction
5231 In ... I1 -> I(n-1) ... I1 In [top is right]
5233 void vnrott(int n)
5235 int i;
5236 SValue tmp;
5238 tmp = vtop[-n + 1];
5239 for(i = n - 1; i > 0; i--)
5240 vtop[-i] = vtop[-i + 1];
5241 vtop[0] = tmp;
5243 #endif
5245 /* pop stack value */
5246 void vpop(void)
5248 int v;
5249 v = vtop->r & VT_VALMASK;
5250 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
5251 /* for x86, we need to pop the FP stack */
5252 if (v == TREG_ST0 && !nocode_wanted) {
5253 o(0xd9dd); /* fstp %st(1) */
5254 } else
5255 #endif
5256 if (v == VT_JMP || v == VT_JMPI) {
5257 /* need to put correct jump if && or || without test */
5258 gsym(vtop->c.ul);
5260 vtop--;
5263 /* convert stack entry to register and duplicate its value in another
5264 register */
5265 void gv_dup(void)
5267 int rc, t, r, r1;
5268 SValue sv;
5270 t = vtop->type.t;
5271 if ((t & VT_BTYPE) == VT_LLONG) {
5272 lexpand();
5273 gv_dup();
5274 vswap();
5275 vrotb(3);
5276 gv_dup();
5277 vrotb(4);
5278 /* stack: H L L1 H1 */
5279 lbuild(t);
5280 vrotb(3);
5281 vrotb(3);
5282 vswap();
5283 lbuild(t);
5284 vswap();
5285 } else {
5286 /* duplicate value */
5287 rc = RC_INT;
5288 sv.type.t = VT_INT;
5289 if (is_float(t)) {
5290 rc = RC_FLOAT;
5291 #ifdef TCC_TARGET_X86_64
5292 if ((t & VT_BTYPE) == VT_LDOUBLE) {
5293 rc = RC_ST0;
5295 #endif
5296 sv.type.t = t;
5298 r = gv(rc);
5299 r1 = get_reg(rc);
5300 sv.r = r;
5301 sv.c.ul = 0;
5302 load(r1, &sv); /* move r to r1 */
5303 vdup();
5304 /* duplicates value */
5305 vtop->r = r1;
5309 #ifndef TCC_TARGET_X86_64
5310 /* generate CPU independent (unsigned) long long operations */
5311 void gen_opl(int op)
5313 int t, a, b, op1, c, i;
5314 int func;
5315 unsigned short reg_iret = REG_IRET;
5316 unsigned short reg_lret = REG_LRET;
5317 SValue tmp;
5319 switch(op) {
5320 case '/':
5321 case TOK_PDIV:
5322 func = TOK___divdi3;
5323 goto gen_func;
5324 case TOK_UDIV:
5325 func = TOK___udivdi3;
5326 goto gen_func;
5327 case '%':
5328 func = TOK___moddi3;
5329 goto gen_mod_func;
5330 case TOK_UMOD:
5331 func = TOK___umoddi3;
5332 gen_mod_func:
5333 #ifdef TCC_ARM_EABI
5334 reg_iret = TREG_R2;
5335 reg_lret = TREG_R3;
5336 #endif
5337 gen_func:
5338 /* call generic long long function */
5339 vpush_global_sym(&func_old_type, func);
5340 vrott(3);
5341 gfunc_call(2);
5342 vpushi(0);
5343 vtop->r = reg_iret;
5344 vtop->r2 = reg_lret;
5345 break;
5346 case '^':
5347 case '&':
5348 case '|':
5349 case '*':
5350 case '+':
5351 case '-':
5352 t = vtop->type.t;
5353 vswap();
5354 lexpand();
5355 vrotb(3);
5356 lexpand();
5357 /* stack: L1 H1 L2 H2 */
5358 tmp = vtop[0];
5359 vtop[0] = vtop[-3];
5360 vtop[-3] = tmp;
5361 tmp = vtop[-2];
5362 vtop[-2] = vtop[-3];
5363 vtop[-3] = tmp;
5364 vswap();
5365 /* stack: H1 H2 L1 L2 */
5366 if (op == '*') {
5367 vpushv(vtop - 1);
5368 vpushv(vtop - 1);
5369 gen_op(TOK_UMULL);
5370 lexpand();
5371 /* stack: H1 H2 L1 L2 ML MH */
5372 for(i=0;i<4;i++)
5373 vrotb(6);
5374 /* stack: ML MH H1 H2 L1 L2 */
5375 tmp = vtop[0];
5376 vtop[0] = vtop[-2];
5377 vtop[-2] = tmp;
5378 /* stack: ML MH H1 L2 H2 L1 */
5379 gen_op('*');
5380 vrotb(3);
5381 vrotb(3);
5382 gen_op('*');
5383 /* stack: ML MH M1 M2 */
5384 gen_op('+');
5385 gen_op('+');
5386 } else if (op == '+' || op == '-') {
5387 /* XXX: add non carry method too (for MIPS or alpha) */
5388 if (op == '+')
5389 op1 = TOK_ADDC1;
5390 else
5391 op1 = TOK_SUBC1;
5392 gen_op(op1);
5393 /* stack: H1 H2 (L1 op L2) */
5394 vrotb(3);
5395 vrotb(3);
5396 gen_op(op1 + 1); /* TOK_xxxC2 */
5397 } else {
5398 gen_op(op);
5399 /* stack: H1 H2 (L1 op L2) */
5400 vrotb(3);
5401 vrotb(3);
5402 /* stack: (L1 op L2) H1 H2 */
5403 gen_op(op);
5404 /* stack: (L1 op L2) (H1 op H2) */
5406 /* stack: L H */
5407 lbuild(t);
5408 break;
5409 case TOK_SAR:
5410 case TOK_SHR:
5411 case TOK_SHL:
5412 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5413 t = vtop[-1].type.t;
5414 vswap();
5415 lexpand();
5416 vrotb(3);
5417 /* stack: L H shift */
5418 c = (int)vtop->c.i;
5419 /* constant: simpler */
5420 /* NOTE: all comments are for SHL. the other cases are
5421 done by swaping words */
5422 vpop();
5423 if (op != TOK_SHL)
5424 vswap();
5425 if (c >= 32) {
5426 /* stack: L H */
5427 vpop();
5428 if (c > 32) {
5429 vpushi(c - 32);
5430 gen_op(op);
5432 if (op != TOK_SAR) {
5433 vpushi(0);
5434 } else {
5435 gv_dup();
5436 vpushi(31);
5437 gen_op(TOK_SAR);
5439 vswap();
5440 } else {
5441 vswap();
5442 gv_dup();
5443 /* stack: H L L */
5444 vpushi(c);
5445 gen_op(op);
5446 vswap();
5447 vpushi(32 - c);
5448 if (op == TOK_SHL)
5449 gen_op(TOK_SHR);
5450 else
5451 gen_op(TOK_SHL);
5452 vrotb(3);
5453 /* stack: L L H */
5454 vpushi(c);
5455 if (op == TOK_SHL)
5456 gen_op(TOK_SHL);
5457 else
5458 gen_op(TOK_SHR);
5459 gen_op('|');
5461 if (op != TOK_SHL)
5462 vswap();
5463 lbuild(t);
5464 } else {
5465 /* XXX: should provide a faster fallback on x86 ? */
5466 switch(op) {
5467 case TOK_SAR:
5468 func = TOK___ashrdi3;
5469 goto gen_func;
5470 case TOK_SHR:
5471 func = TOK___lshrdi3;
5472 goto gen_func;
5473 case TOK_SHL:
5474 func = TOK___ashldi3;
5475 goto gen_func;
5478 break;
5479 default:
5480 /* compare operations */
5481 t = vtop->type.t;
5482 vswap();
5483 lexpand();
5484 vrotb(3);
5485 lexpand();
5486 /* stack: L1 H1 L2 H2 */
5487 tmp = vtop[-1];
5488 vtop[-1] = vtop[-2];
5489 vtop[-2] = tmp;
5490 /* stack: L1 L2 H1 H2 */
5491 /* compare high */
5492 op1 = op;
5493 /* when values are equal, we need to compare low words. since
5494 the jump is inverted, we invert the test too. */
5495 if (op1 == TOK_LT)
5496 op1 = TOK_LE;
5497 else if (op1 == TOK_GT)
5498 op1 = TOK_GE;
5499 else if (op1 == TOK_ULT)
5500 op1 = TOK_ULE;
5501 else if (op1 == TOK_UGT)
5502 op1 = TOK_UGE;
5503 a = 0;
5504 b = 0;
5505 gen_op(op1);
5506 if (op1 != TOK_NE) {
5507 a = gtst(1, 0);
5509 if (op != TOK_EQ) {
5510 /* generate non equal test */
5511 /* XXX: NOT PORTABLE yet */
5512 if (a == 0) {
5513 b = gtst(0, 0);
5514 } else {
5515 #if defined(TCC_TARGET_I386)
5516 b = psym(0x850f, 0);
5517 #elif defined(TCC_TARGET_ARM)
5518 b = ind;
5519 o(0x1A000000 | encbranch(ind, 0, 1));
5520 #elif defined(TCC_TARGET_C67)
5521 error("not implemented");
5522 #else
5523 #error not supported
5524 #endif
5527 /* compare low. Always unsigned */
5528 op1 = op;
5529 if (op1 == TOK_LT)
5530 op1 = TOK_ULT;
5531 else if (op1 == TOK_LE)
5532 op1 = TOK_ULE;
5533 else if (op1 == TOK_GT)
5534 op1 = TOK_UGT;
5535 else if (op1 == TOK_GE)
5536 op1 = TOK_UGE;
5537 gen_op(op1);
5538 a = gtst(1, a);
5539 gsym(b);
5540 vseti(VT_JMPI, a);
5541 break;
5544 #endif
5546 /* handle integer constant optimizations and various machine
5547 independent opt */
5548 void gen_opic(int op)
5550 int c1, c2, t1, t2, n;
5551 SValue *v1, *v2;
5552 long long l1, l2;
5553 typedef unsigned long long U;
5555 v1 = vtop - 1;
5556 v2 = vtop;
5557 t1 = v1->type.t & VT_BTYPE;
5558 t2 = v2->type.t & VT_BTYPE;
5560 if (t1 == VT_LLONG)
5561 l1 = v1->c.ll;
5562 else if (v1->type.t & VT_UNSIGNED)
5563 l1 = v1->c.ui;
5564 else
5565 l1 = v1->c.i;
5567 if (t2 == VT_LLONG)
5568 l2 = v2->c.ll;
5569 else if (v2->type.t & VT_UNSIGNED)
5570 l2 = v2->c.ui;
5571 else
5572 l2 = v2->c.i;
5574 /* currently, we cannot do computations with forward symbols */
5575 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5576 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5577 if (c1 && c2) {
5578 switch(op) {
5579 case '+': l1 += l2; break;
5580 case '-': l1 -= l2; break;
5581 case '&': l1 &= l2; break;
5582 case '^': l1 ^= l2; break;
5583 case '|': l1 |= l2; break;
5584 case '*': l1 *= l2; break;
5586 case TOK_PDIV:
5587 case '/':
5588 case '%':
5589 case TOK_UDIV:
5590 case TOK_UMOD:
5591 /* if division by zero, generate explicit division */
5592 if (l2 == 0) {
5593 if (const_wanted)
5594 error("division by zero in constant");
5595 goto general_case;
5597 switch(op) {
5598 default: l1 /= l2; break;
5599 case '%': l1 %= l2; break;
5600 case TOK_UDIV: l1 = (U)l1 / l2; break;
5601 case TOK_UMOD: l1 = (U)l1 % l2; break;
5603 break;
5604 case TOK_SHL: l1 <<= l2; break;
5605 case TOK_SHR: l1 = (U)l1 >> l2; break;
5606 case TOK_SAR: l1 >>= l2; break;
5607 /* tests */
5608 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5609 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5610 case TOK_EQ: l1 = l1 == l2; break;
5611 case TOK_NE: l1 = l1 != l2; break;
5612 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5613 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5614 case TOK_LT: l1 = l1 < l2; break;
5615 case TOK_GE: l1 = l1 >= l2; break;
5616 case TOK_LE: l1 = l1 <= l2; break;
5617 case TOK_GT: l1 = l1 > l2; break;
5618 /* logical */
5619 case TOK_LAND: l1 = l1 && l2; break;
5620 case TOK_LOR: l1 = l1 || l2; break;
5621 default:
5622 goto general_case;
5624 v1->c.ll = l1;
5625 vtop--;
5626 } else {
5627 /* if commutative ops, put c2 as constant */
5628 if (c1 && (op == '+' || op == '&' || op == '^' ||
5629 op == '|' || op == '*')) {
5630 vswap();
5631 c2 = c1; //c = c1, c1 = c2, c2 = c;
5632 l2 = l1; //l = l1, l1 = l2, l2 = l;
5634 /* Filter out NOP operations like x*1, x-0, x&-1... */
5635 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5636 op == TOK_PDIV) &&
5637 l2 == 1) ||
5638 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5639 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5640 l2 == 0) ||
5641 (op == '&' &&
5642 l2 == -1))) {
5643 /* nothing to do */
5644 vtop--;
5645 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5646 /* try to use shifts instead of muls or divs */
5647 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5648 n = -1;
5649 while (l2) {
5650 l2 >>= 1;
5651 n++;
5653 vtop->c.ll = n;
5654 if (op == '*')
5655 op = TOK_SHL;
5656 else if (op == TOK_PDIV)
5657 op = TOK_SAR;
5658 else
5659 op = TOK_SHR;
5661 goto general_case;
5662 } else if (c2 && (op == '+' || op == '-') &&
5663 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5664 (VT_CONST | VT_SYM) ||
5665 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
5666 /* symbol + constant case */
5667 if (op == '-')
5668 l2 = -l2;
5669 vtop--;
5670 vtop->c.ll += l2;
5671 } else {
5672 general_case:
5673 if (!nocode_wanted) {
5674 /* call low level op generator */
5675 if (t1 == VT_LLONG || t2 == VT_LLONG)
5676 gen_opl(op);
5677 else
5678 gen_opi(op);
5679 } else {
5680 vtop--;
5686 /* generate a floating point operation with constant propagation */
5687 void gen_opif(int op)
5689 int c1, c2;
5690 SValue *v1, *v2;
5691 long double f1, f2;
5693 v1 = vtop - 1;
5694 v2 = vtop;
5695 /* currently, we cannot do computations with forward symbols */
5696 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5697 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5698 if (c1 && c2) {
5699 if (v1->type.t == VT_FLOAT) {
5700 f1 = v1->c.f;
5701 f2 = v2->c.f;
5702 } else if (v1->type.t == VT_DOUBLE) {
5703 f1 = v1->c.d;
5704 f2 = v2->c.d;
5705 } else {
5706 f1 = v1->c.ld;
5707 f2 = v2->c.ld;
5710 /* NOTE: we only do constant propagation if finite number (not
5711 NaN or infinity) (ANSI spec) */
5712 if (!ieee_finite(f1) || !ieee_finite(f2))
5713 goto general_case;
5715 switch(op) {
5716 case '+': f1 += f2; break;
5717 case '-': f1 -= f2; break;
5718 case '*': f1 *= f2; break;
5719 case '/':
5720 if (f2 == 0.0) {
5721 if (const_wanted)
5722 error("division by zero in constant");
5723 goto general_case;
5725 f1 /= f2;
5726 break;
5727 /* XXX: also handles tests ? */
5728 default:
5729 goto general_case;
5731 /* XXX: overflow test ? */
5732 if (v1->type.t == VT_FLOAT) {
5733 v1->c.f = f1;
5734 } else if (v1->type.t == VT_DOUBLE) {
5735 v1->c.d = f1;
5736 } else {
5737 v1->c.ld = f1;
5739 vtop--;
5740 } else {
5741 general_case:
5742 if (!nocode_wanted) {
5743 gen_opf(op);
5744 } else {
5745 vtop--;
5750 static int pointed_size(CType *type)
5752 int align;
5753 return type_size(pointed_type(type), &align);
5756 static inline int is_null_pointer(SValue *p)
5758 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5759 return 0;
5760 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5761 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5764 static inline int is_integer_btype(int bt)
5766 return (bt == VT_BYTE || bt == VT_SHORT ||
5767 bt == VT_INT || bt == VT_LLONG);
5770 /* check types for comparison or substraction of pointers */
5771 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5773 CType *type1, *type2, tmp_type1, tmp_type2;
5774 int bt1, bt2;
5776 /* null pointers are accepted for all comparisons as gcc */
5777 if (is_null_pointer(p1) || is_null_pointer(p2))
5778 return;
5779 type1 = &p1->type;
5780 type2 = &p2->type;
5781 bt1 = type1->t & VT_BTYPE;
5782 bt2 = type2->t & VT_BTYPE;
5783 /* accept comparison between pointer and integer with a warning */
5784 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5785 if (op != TOK_LOR && op != TOK_LAND )
5786 warning("comparison between pointer and integer");
5787 return;
5790 /* both must be pointers or implicit function pointers */
5791 if (bt1 == VT_PTR) {
5792 type1 = pointed_type(type1);
5793 } else if (bt1 != VT_FUNC)
5794 goto invalid_operands;
5796 if (bt2 == VT_PTR) {
5797 type2 = pointed_type(type2);
5798 } else if (bt2 != VT_FUNC) {
5799 invalid_operands:
5800 error("invalid operands to binary %s", get_tok_str(op, NULL));
5802 if ((type1->t & VT_BTYPE) == VT_VOID ||
5803 (type2->t & VT_BTYPE) == VT_VOID)
5804 return;
5805 tmp_type1 = *type1;
5806 tmp_type2 = *type2;
5807 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5808 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5809 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5810 /* gcc-like error if '-' is used */
5811 if (op == '-')
5812 goto invalid_operands;
5813 else
5814 warning("comparison of distinct pointer types lacks a cast");
5818 /* generic gen_op: handles types problems */
5819 void gen_op(int op)
5821 int u, t1, t2, bt1, bt2, t;
5822 CType type1;
5824 t1 = vtop[-1].type.t;
5825 t2 = vtop[0].type.t;
5826 bt1 = t1 & VT_BTYPE;
5827 bt2 = t2 & VT_BTYPE;
5829 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5830 /* at least one operand is a pointer */
5831 /* relationnal op: must be both pointers */
5832 if (op >= TOK_ULT && op <= TOK_LOR) {
5833 check_comparison_pointer_types(vtop - 1, vtop, op);
5834 /* pointers are handled are unsigned */
5835 #ifdef TCC_TARGET_X86_64
5836 t = VT_LLONG | VT_UNSIGNED;
5837 #else
5838 t = VT_INT | VT_UNSIGNED;
5839 #endif
5840 goto std_op;
5842 /* if both pointers, then it must be the '-' op */
5843 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5844 if (op != '-')
5845 error("cannot use pointers here");
5846 check_comparison_pointer_types(vtop - 1, vtop, op);
5847 /* XXX: check that types are compatible */
5848 u = pointed_size(&vtop[-1].type);
5849 gen_opic(op);
5850 /* set to integer type */
5851 #ifdef TCC_TARGET_X86_64
5852 vtop->type.t = VT_LLONG;
5853 #else
5854 vtop->type.t = VT_INT;
5855 #endif
5856 vpushi(u);
5857 gen_op(TOK_PDIV);
5858 } else {
5859 /* exactly one pointer : must be '+' or '-'. */
5860 if (op != '-' && op != '+')
5861 error("cannot use pointers here");
5862 /* Put pointer as first operand */
5863 if (bt2 == VT_PTR) {
5864 vswap();
5865 swap(&t1, &t2);
5867 type1 = vtop[-1].type;
5868 #ifdef TCC_TARGET_X86_64
5870 CValue cval;
5871 CType ctype;
5872 ctype.t = VT_LLONG;
5873 cval.ull = pointed_size(&vtop[-1].type);
5874 vsetc(&ctype, VT_CONST, &cval);
5876 #else
5877 /* XXX: cast to int ? (long long case) */
5878 vpushi(pointed_size(&vtop[-1].type));
5879 #endif
5880 gen_op('*');
5881 #ifdef CONFIG_TCC_BCHECK
5882 /* if evaluating constant expression, no code should be
5883 generated, so no bound check */
5884 if (do_bounds_check && !const_wanted) {
5885 /* if bounded pointers, we generate a special code to
5886 test bounds */
5887 if (op == '-') {
5888 vpushi(0);
5889 vswap();
5890 gen_op('-');
5892 gen_bounded_ptr_add();
5893 } else
5894 #endif
5896 gen_opic(op);
5898 /* put again type if gen_opic() swaped operands */
5899 vtop->type = type1;
5901 } else if (is_float(bt1) || is_float(bt2)) {
5902 /* compute bigger type and do implicit casts */
5903 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5904 t = VT_LDOUBLE;
5905 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5906 t = VT_DOUBLE;
5907 } else {
5908 t = VT_FLOAT;
5910 /* floats can only be used for a few operations */
5911 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5912 (op < TOK_ULT || op > TOK_GT))
5913 error("invalid operands for binary operation");
5914 goto std_op;
5915 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5916 /* cast to biggest op */
5917 t = VT_LLONG;
5918 /* convert to unsigned if it does not fit in a long long */
5919 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5920 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5921 t |= VT_UNSIGNED;
5922 goto std_op;
5923 } else {
5924 /* integer operations */
5925 t = VT_INT;
5926 /* convert to unsigned if it does not fit in an integer */
5927 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5928 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5929 t |= VT_UNSIGNED;
5930 std_op:
5931 /* XXX: currently, some unsigned operations are explicit, so
5932 we modify them here */
5933 if (t & VT_UNSIGNED) {
5934 if (op == TOK_SAR)
5935 op = TOK_SHR;
5936 else if (op == '/')
5937 op = TOK_UDIV;
5938 else if (op == '%')
5939 op = TOK_UMOD;
5940 else if (op == TOK_LT)
5941 op = TOK_ULT;
5942 else if (op == TOK_GT)
5943 op = TOK_UGT;
5944 else if (op == TOK_LE)
5945 op = TOK_ULE;
5946 else if (op == TOK_GE)
5947 op = TOK_UGE;
5949 vswap();
5950 type1.t = t;
5951 gen_cast(&type1);
5952 vswap();
5953 /* special case for shifts and long long: we keep the shift as
5954 an integer */
5955 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5956 type1.t = VT_INT;
5957 gen_cast(&type1);
5958 if (is_float(t))
5959 gen_opif(op);
5960 else
5961 gen_opic(op);
5962 if (op >= TOK_ULT && op <= TOK_GT) {
5963 /* relationnal op: the result is an int */
5964 vtop->type.t = VT_INT;
5965 } else {
5966 vtop->type.t = t;
5971 #ifndef TCC_TARGET_ARM
5972 /* generic itof for unsigned long long case */
5973 void gen_cvt_itof1(int t)
5975 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5976 (VT_LLONG | VT_UNSIGNED)) {
5978 if (t == VT_FLOAT)
5979 vpush_global_sym(&func_old_type, TOK___floatundisf);
5980 #if LDOUBLE_SIZE != 8
5981 else if (t == VT_LDOUBLE)
5982 vpush_global_sym(&func_old_type, TOK___floatundixf);
5983 #endif
5984 else
5985 vpush_global_sym(&func_old_type, TOK___floatundidf);
5986 vrott(2);
5987 gfunc_call(1);
5988 vpushi(0);
5989 vtop->r = REG_FRET;
5990 } else {
5991 gen_cvt_itof(t);
5994 #endif
5996 /* generic ftoi for unsigned long long case */
5997 void gen_cvt_ftoi1(int t)
5999 int st;
6001 if (t == (VT_LLONG | VT_UNSIGNED)) {
6002 /* not handled natively */
6003 st = vtop->type.t & VT_BTYPE;
6004 if (st == VT_FLOAT)
6005 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
6006 #if LDOUBLE_SIZE != 8
6007 else if (st == VT_LDOUBLE)
6008 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
6009 #endif
6010 else
6011 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
6012 vrott(2);
6013 gfunc_call(1);
6014 vpushi(0);
6015 vtop->r = REG_IRET;
6016 vtop->r2 = REG_LRET;
6017 } else {
6018 gen_cvt_ftoi(t);
6022 /* force char or short cast */
6023 void force_charshort_cast(int t)
6025 int bits, dbt;
6026 dbt = t & VT_BTYPE;
6027 /* XXX: add optimization if lvalue : just change type and offset */
6028 if (dbt == VT_BYTE)
6029 bits = 8;
6030 else
6031 bits = 16;
6032 if (t & VT_UNSIGNED) {
6033 vpushi((1 << bits) - 1);
6034 gen_op('&');
6035 } else {
6036 bits = 32 - bits;
6037 vpushi(bits);
6038 gen_op(TOK_SHL);
6039 /* result must be signed or the SAR is converted to an SHL
6040 This was not the case when "t" was a signed short
6041 and the last value on the stack was an unsigned int */
6042 vtop->type.t &= ~VT_UNSIGNED;
6043 vpushi(bits);
6044 gen_op(TOK_SAR);
6048 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
6049 static void gen_cast(CType *type)
6051 int sbt, dbt, sf, df, c, p;
6053 /* special delayed cast for char/short */
6054 /* XXX: in some cases (multiple cascaded casts), it may still
6055 be incorrect */
6056 if (vtop->r & VT_MUSTCAST) {
6057 vtop->r &= ~VT_MUSTCAST;
6058 force_charshort_cast(vtop->type.t);
6061 /* bitfields first get cast to ints */
6062 if (vtop->type.t & VT_BITFIELD) {
6063 gv(RC_INT);
6066 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
6067 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
6069 if (sbt != dbt) {
6070 sf = is_float(sbt);
6071 df = is_float(dbt);
6072 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6073 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
6074 if (c) {
6075 /* constant case: we can do it now */
6076 /* XXX: in ISOC, cannot do it if error in convert */
6077 if (sbt == VT_FLOAT)
6078 vtop->c.ld = vtop->c.f;
6079 else if (sbt == VT_DOUBLE)
6080 vtop->c.ld = vtop->c.d;
6082 if (df) {
6083 if ((sbt & VT_BTYPE) == VT_LLONG) {
6084 if (sbt & VT_UNSIGNED)
6085 vtop->c.ld = vtop->c.ull;
6086 else
6087 vtop->c.ld = vtop->c.ll;
6088 } else if(!sf) {
6089 if (sbt & VT_UNSIGNED)
6090 vtop->c.ld = vtop->c.ui;
6091 else
6092 vtop->c.ld = vtop->c.i;
6095 if (dbt == VT_FLOAT)
6096 vtop->c.f = (float)vtop->c.ld;
6097 else if (dbt == VT_DOUBLE)
6098 vtop->c.d = (double)vtop->c.ld;
6099 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
6100 vtop->c.ull = (unsigned long long)vtop->c.ld;
6101 } else if (sf && dbt == VT_BOOL) {
6102 vtop->c.i = (vtop->c.ld != 0);
6103 } else {
6104 if(sf)
6105 vtop->c.ll = (long long)vtop->c.ld;
6106 else if (sbt == (VT_LLONG|VT_UNSIGNED))
6107 vtop->c.ll = vtop->c.ull;
6108 else if (sbt & VT_UNSIGNED)
6109 vtop->c.ll = vtop->c.ui;
6110 else if (sbt != VT_LLONG)
6111 vtop->c.ll = vtop->c.i;
6113 if (dbt == (VT_LLONG|VT_UNSIGNED))
6114 vtop->c.ull = vtop->c.ll;
6115 else if (dbt == VT_BOOL)
6116 vtop->c.i = (vtop->c.ll != 0);
6117 else if (dbt != VT_LLONG) {
6118 int s = 0;
6119 if ((dbt & VT_BTYPE) == VT_BYTE)
6120 s = 24;
6121 else if ((dbt & VT_BTYPE) == VT_SHORT)
6122 s = 16;
6124 if(dbt & VT_UNSIGNED)
6125 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
6126 else
6127 vtop->c.i = ((int)vtop->c.ll << s) >> s;
6130 } else if (p && dbt == VT_BOOL) {
6131 vtop->r = VT_CONST;
6132 vtop->c.i = 1;
6133 } else if (!nocode_wanted) {
6134 /* non constant case: generate code */
6135 if (sf && df) {
6136 /* convert from fp to fp */
6137 gen_cvt_ftof(dbt);
6138 } else if (df) {
6139 /* convert int to fp */
6140 gen_cvt_itof1(dbt);
6141 } else if (sf) {
6142 /* convert fp to int */
6143 if (dbt == VT_BOOL) {
6144 vpushi(0);
6145 gen_op(TOK_NE);
6146 } else {
6147 /* we handle char/short/etc... with generic code */
6148 if (dbt != (VT_INT | VT_UNSIGNED) &&
6149 dbt != (VT_LLONG | VT_UNSIGNED) &&
6150 dbt != VT_LLONG)
6151 dbt = VT_INT;
6152 gen_cvt_ftoi1(dbt);
6153 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6154 /* additional cast for char/short... */
6155 vtop->type.t = dbt;
6156 gen_cast(type);
6159 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6160 if ((sbt & VT_BTYPE) != VT_LLONG) {
6161 /* scalar to long long */
6162 #ifndef TCC_TARGET_X86_64
6163 /* machine independent conversion */
6164 gv(RC_INT);
6165 /* generate high word */
6166 if (sbt == (VT_INT | VT_UNSIGNED)) {
6167 vpushi(0);
6168 gv(RC_INT);
6169 } else {
6170 gv_dup();
6171 vpushi(31);
6172 gen_op(TOK_SAR);
6174 /* patch second register */
6175 vtop[-1].r2 = vtop->r;
6176 vpop();
6177 #else
6178 int r = gv(RC_INT);
6179 if (sbt != (VT_INT | VT_UNSIGNED)) {
6180 /* x86_64 specific: movslq */
6181 o(0x6348);
6182 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
6184 #endif
6186 } else if (dbt == VT_BOOL) {
6187 /* scalar to bool */
6188 vpushi(0);
6189 gen_op(TOK_NE);
6190 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6191 (dbt & VT_BTYPE) == VT_SHORT) {
6192 if (sbt == VT_PTR) {
6193 vtop->type.t = VT_INT;
6194 warning("nonportable conversion from pointer to char/short");
6196 force_charshort_cast(dbt);
6197 } else if ((dbt & VT_BTYPE) == VT_INT) {
6198 /* scalar to int */
6199 if (sbt == VT_LLONG) {
6200 /* from long long: just take low order word */
6201 lexpand();
6202 vpop();
6204 /* if lvalue and single word type, nothing to do because
6205 the lvalue already contains the real type size (see
6206 VT_LVAL_xxx constants) */
6209 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6210 /* if we are casting between pointer types,
6211 we must update the VT_LVAL_xxx size */
6212 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6213 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6215 vtop->type = *type;
6218 /* return type size. Put alignment at 'a' */
6219 static int type_size(CType *type, int *a)
6221 Sym *s;
6222 int bt;
6224 bt = type->t & VT_BTYPE;
6225 if (bt == VT_STRUCT) {
6226 /* struct/union */
6227 s = type->ref;
6228 *a = s->r;
6229 return s->c;
6230 } else if (bt == VT_PTR) {
6231 if (type->t & VT_ARRAY) {
6232 int ts;
6234 s = type->ref;
6235 ts = type_size(&s->type, a);
6237 if (ts < 0 && s->c < 0)
6238 ts = -ts;
6240 return ts * s->c;
6241 } else {
6242 *a = PTR_SIZE;
6243 return PTR_SIZE;
6245 } else if (bt == VT_LDOUBLE) {
6246 *a = LDOUBLE_ALIGN;
6247 return LDOUBLE_SIZE;
6248 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6249 #ifdef TCC_TARGET_I386
6250 *a = 4;
6251 #elif defined(TCC_TARGET_ARM)
6252 #ifdef TCC_ARM_EABI
6253 *a = 8;
6254 #else
6255 *a = 4;
6256 #endif
6257 #else
6258 *a = 8;
6259 #endif
6260 return 8;
6261 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6262 *a = 4;
6263 return 4;
6264 } else if (bt == VT_SHORT) {
6265 *a = 2;
6266 return 2;
6267 } else {
6268 /* char, void, function, _Bool */
6269 *a = 1;
6270 return 1;
6274 /* return the pointed type of t */
6275 static inline CType *pointed_type(CType *type)
6277 return &type->ref->type;
6280 /* modify type so that its it is a pointer to type. */
6281 static void mk_pointer(CType *type)
6283 Sym *s;
6284 s = sym_push(SYM_FIELD, type, 0, -1);
6285 type->t = VT_PTR | (type->t & ~VT_TYPE);
6286 type->ref = s;
6289 /* compare function types. OLD functions match any new functions */
6290 static int is_compatible_func(CType *type1, CType *type2)
6292 Sym *s1, *s2;
6294 s1 = type1->ref;
6295 s2 = type2->ref;
6296 if (!is_compatible_types(&s1->type, &s2->type))
6297 return 0;
6298 /* check func_call */
6299 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6300 return 0;
6301 /* XXX: not complete */
6302 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6303 return 1;
6304 if (s1->c != s2->c)
6305 return 0;
6306 while (s1 != NULL) {
6307 if (s2 == NULL)
6308 return 0;
6309 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6310 return 0;
6311 s1 = s1->next;
6312 s2 = s2->next;
6314 if (s2)
6315 return 0;
6316 return 1;
6319 /* return true if type1 and type2 are the same. If unqualified is
6320 true, qualifiers on the types are ignored.
6322 - enums are not checked as gcc __builtin_types_compatible_p ()
6324 static int compare_types(CType *type1, CType *type2, int unqualified)
6326 int bt1, t1, t2;
6328 t1 = type1->t & VT_TYPE;
6329 t2 = type2->t & VT_TYPE;
6330 if (unqualified) {
6331 /* strip qualifiers before comparing */
6332 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6333 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6335 /* XXX: bitfields ? */
6336 if (t1 != t2)
6337 return 0;
6338 /* test more complicated cases */
6339 bt1 = t1 & VT_BTYPE;
6340 if (bt1 == VT_PTR) {
6341 type1 = pointed_type(type1);
6342 type2 = pointed_type(type2);
6343 return is_compatible_types(type1, type2);
6344 } else if (bt1 == VT_STRUCT) {
6345 return (type1->ref == type2->ref);
6346 } else if (bt1 == VT_FUNC) {
6347 return is_compatible_func(type1, type2);
6348 } else {
6349 return 1;
6353 /* return true if type1 and type2 are exactly the same (including
6354 qualifiers).
6356 static int is_compatible_types(CType *type1, CType *type2)
6358 return compare_types(type1,type2,0);
6361 /* return true if type1 and type2 are the same (ignoring qualifiers).
6363 static int is_compatible_parameter_types(CType *type1, CType *type2)
6365 return compare_types(type1,type2,1);
6368 /* print a type. If 'varstr' is not NULL, then the variable is also
6369 printed in the type */
6370 /* XXX: union */
6371 /* XXX: add array and function pointers */
6372 void type_to_str(char *buf, int buf_size,
6373 CType *type, const char *varstr)
6375 int bt, v, t;
6376 Sym *s, *sa;
6377 char buf1[256];
6378 const char *tstr;
6380 t = type->t & VT_TYPE;
6381 bt = t & VT_BTYPE;
6382 buf[0] = '\0';
6383 if (t & VT_CONSTANT)
6384 pstrcat(buf, buf_size, "const ");
6385 if (t & VT_VOLATILE)
6386 pstrcat(buf, buf_size, "volatile ");
6387 if (t & VT_UNSIGNED)
6388 pstrcat(buf, buf_size, "unsigned ");
6389 switch(bt) {
6390 case VT_VOID:
6391 tstr = "void";
6392 goto add_tstr;
6393 case VT_BOOL:
6394 tstr = "_Bool";
6395 goto add_tstr;
6396 case VT_BYTE:
6397 tstr = "char";
6398 goto add_tstr;
6399 case VT_SHORT:
6400 tstr = "short";
6401 goto add_tstr;
6402 case VT_INT:
6403 tstr = "int";
6404 goto add_tstr;
6405 case VT_LONG:
6406 tstr = "long";
6407 goto add_tstr;
6408 case VT_LLONG:
6409 tstr = "long long";
6410 goto add_tstr;
6411 case VT_FLOAT:
6412 tstr = "float";
6413 goto add_tstr;
6414 case VT_DOUBLE:
6415 tstr = "double";
6416 goto add_tstr;
6417 case VT_LDOUBLE:
6418 tstr = "long double";
6419 add_tstr:
6420 pstrcat(buf, buf_size, tstr);
6421 break;
6422 case VT_ENUM:
6423 case VT_STRUCT:
6424 if (bt == VT_STRUCT)
6425 tstr = "struct ";
6426 else
6427 tstr = "enum ";
6428 pstrcat(buf, buf_size, tstr);
6429 v = type->ref->v & ~SYM_STRUCT;
6430 if (v >= SYM_FIRST_ANOM)
6431 pstrcat(buf, buf_size, "<anonymous>");
6432 else
6433 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6434 break;
6435 case VT_FUNC:
6436 s = type->ref;
6437 type_to_str(buf, buf_size, &s->type, varstr);
6438 pstrcat(buf, buf_size, "(");
6439 sa = s->next;
6440 while (sa != NULL) {
6441 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6442 pstrcat(buf, buf_size, buf1);
6443 sa = sa->next;
6444 if (sa)
6445 pstrcat(buf, buf_size, ", ");
6447 pstrcat(buf, buf_size, ")");
6448 goto no_var;
6449 case VT_PTR:
6450 s = type->ref;
6451 pstrcpy(buf1, sizeof(buf1), "*");
6452 if (varstr)
6453 pstrcat(buf1, sizeof(buf1), varstr);
6454 type_to_str(buf, buf_size, &s->type, buf1);
6455 goto no_var;
6457 if (varstr) {
6458 pstrcat(buf, buf_size, " ");
6459 pstrcat(buf, buf_size, varstr);
6461 no_var: ;
6464 /* verify type compatibility to store vtop in 'dt' type, and generate
6465 casts if needed. */
6466 static void gen_assign_cast(CType *dt)
6468 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6469 char buf1[256], buf2[256];
6470 int dbt, sbt;
6472 st = &vtop->type; /* source type */
6473 dbt = dt->t & VT_BTYPE;
6474 sbt = st->t & VT_BTYPE;
6475 if (dt->t & VT_CONSTANT)
6476 warning("assignment of read-only location");
6477 switch(dbt) {
6478 case VT_PTR:
6479 /* special cases for pointers */
6480 /* '0' can also be a pointer */
6481 if (is_null_pointer(vtop))
6482 goto type_ok;
6483 /* accept implicit pointer to integer cast with warning */
6484 if (is_integer_btype(sbt)) {
6485 warning("assignment makes pointer from integer without a cast");
6486 goto type_ok;
6488 type1 = pointed_type(dt);
6489 /* a function is implicitely a function pointer */
6490 if (sbt == VT_FUNC) {
6491 if ((type1->t & VT_BTYPE) != VT_VOID &&
6492 !is_compatible_types(pointed_type(dt), st))
6493 goto error;
6494 else
6495 goto type_ok;
6497 if (sbt != VT_PTR)
6498 goto error;
6499 type2 = pointed_type(st);
6500 if ((type1->t & VT_BTYPE) == VT_VOID ||
6501 (type2->t & VT_BTYPE) == VT_VOID) {
6502 /* void * can match anything */
6503 } else {
6504 /* exact type match, except for unsigned */
6505 tmp_type1 = *type1;
6506 tmp_type2 = *type2;
6507 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6508 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6509 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6510 warning("assignment from incompatible pointer type");
6512 /* check const and volatile */
6513 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6514 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6515 warning("assignment discards qualifiers from pointer target type");
6516 break;
6517 case VT_BYTE:
6518 case VT_SHORT:
6519 case VT_INT:
6520 case VT_LLONG:
6521 if (sbt == VT_PTR || sbt == VT_FUNC) {
6522 warning("assignment makes integer from pointer without a cast");
6524 /* XXX: more tests */
6525 break;
6526 case VT_STRUCT:
6527 tmp_type1 = *dt;
6528 tmp_type2 = *st;
6529 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6530 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6531 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6532 error:
6533 type_to_str(buf1, sizeof(buf1), st, NULL);
6534 type_to_str(buf2, sizeof(buf2), dt, NULL);
6535 error("cannot cast '%s' to '%s'", buf1, buf2);
6537 break;
6539 type_ok:
6540 gen_cast(dt);
6543 /* store vtop in lvalue pushed on stack */
6544 void vstore(void)
6546 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6548 ft = vtop[-1].type.t;
6549 sbt = vtop->type.t & VT_BTYPE;
6550 dbt = ft & VT_BTYPE;
6551 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6552 (sbt == VT_INT && dbt == VT_SHORT)) {
6553 /* optimize char/short casts */
6554 delayed_cast = VT_MUSTCAST;
6555 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
6556 /* XXX: factorize */
6557 if (ft & VT_CONSTANT)
6558 warning("assignment of read-only location");
6559 } else {
6560 delayed_cast = 0;
6561 if (!(ft & VT_BITFIELD))
6562 gen_assign_cast(&vtop[-1].type);
6565 if (sbt == VT_STRUCT) {
6566 /* if structure, only generate pointer */
6567 /* structure assignment : generate memcpy */
6568 /* XXX: optimize if small size */
6569 if (!nocode_wanted) {
6570 size = type_size(&vtop->type, &align);
6572 #ifdef TCC_ARM_EABI
6573 if(!(align & 7))
6574 vpush_global_sym(&func_old_type, TOK_memcpy8);
6575 else if(!(align & 3))
6576 vpush_global_sym(&func_old_type, TOK_memcpy4);
6577 else
6578 #endif
6579 vpush_global_sym(&func_old_type, TOK_memcpy);
6581 /* destination */
6582 vpushv(vtop - 2);
6583 vtop->type.t = VT_INT;
6584 gaddrof();
6585 /* source */
6586 vpushv(vtop - 2);
6587 vtop->type.t = VT_INT;
6588 gaddrof();
6589 /* type size */
6590 vpushi(size);
6591 gfunc_call(3);
6593 vswap();
6594 vpop();
6595 } else {
6596 vswap();
6597 vpop();
6599 /* leave source on stack */
6600 } else if (ft & VT_BITFIELD) {
6601 /* bitfield store handling */
6602 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6603 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6604 /* remove bit field info to avoid loops */
6605 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6607 /* duplicate source into other register */
6608 gv_dup();
6609 vswap();
6610 vrott(3);
6612 if((ft & VT_BTYPE) == VT_BOOL) {
6613 gen_cast(&vtop[-1].type);
6614 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
6617 /* duplicate destination */
6618 vdup();
6619 vtop[-1] = vtop[-2];
6621 /* mask and shift source */
6622 if((ft & VT_BTYPE) != VT_BOOL) {
6623 vpushi((1 << bit_size) - 1);
6624 gen_op('&');
6626 vpushi(bit_pos);
6627 gen_op(TOK_SHL);
6628 /* load destination, mask and or with source */
6629 vswap();
6630 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6631 gen_op('&');
6632 gen_op('|');
6633 /* store result */
6634 vstore();
6636 /* pop off shifted source from "duplicate source..." above */
6637 vpop();
6639 } else {
6640 #ifdef CONFIG_TCC_BCHECK
6641 /* bound check case */
6642 if (vtop[-1].r & VT_MUSTBOUND) {
6643 vswap();
6644 gbound();
6645 vswap();
6647 #endif
6648 if (!nocode_wanted) {
6649 rc = RC_INT;
6650 if (is_float(ft)) {
6651 rc = RC_FLOAT;
6652 #ifdef TCC_TARGET_X86_64
6653 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
6654 rc = RC_ST0;
6656 #endif
6658 r = gv(rc); /* generate value */
6659 /* if lvalue was saved on stack, must read it */
6660 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6661 SValue sv;
6662 t = get_reg(RC_INT);
6663 #ifdef TCC_TARGET_X86_64
6664 sv.type.t = VT_PTR;
6665 #else
6666 sv.type.t = VT_INT;
6667 #endif
6668 sv.r = VT_LOCAL | VT_LVAL;
6669 sv.c.ul = vtop[-1].c.ul;
6670 load(t, &sv);
6671 vtop[-1].r = t | VT_LVAL;
6673 store(r, vtop - 1);
6674 #ifndef TCC_TARGET_X86_64
6675 /* two word case handling : store second register at word + 4 */
6676 if ((ft & VT_BTYPE) == VT_LLONG) {
6677 vswap();
6678 /* convert to int to increment easily */
6679 vtop->type.t = VT_INT;
6680 gaddrof();
6681 vpushi(4);
6682 gen_op('+');
6683 vtop->r |= VT_LVAL;
6684 vswap();
6685 /* XXX: it works because r2 is spilled last ! */
6686 store(vtop->r2, vtop - 1);
6688 #endif
6690 vswap();
6691 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6692 vtop->r |= delayed_cast;
6696 /* post defines POST/PRE add. c is the token ++ or -- */
6697 void inc(int post, int c)
6699 test_lvalue();
6700 vdup(); /* save lvalue */
6701 if (post) {
6702 gv_dup(); /* duplicate value */
6703 vrotb(3);
6704 vrotb(3);
6706 /* add constant */
6707 vpushi(c - TOK_MID);
6708 gen_op('+');
6709 vstore(); /* store value */
6710 if (post)
6711 vpop(); /* if post op, return saved value */
6714 /* Parse GNUC __attribute__ extension. Currently, the following
6715 extensions are recognized:
6716 - aligned(n) : set data/function alignment.
6717 - packed : force data alignment to 1
6718 - section(x) : generate data/code in this section.
6719 - unused : currently ignored, but may be used someday.
6720 - regparm(n) : pass function parameters in registers (i386 only)
6722 static void parse_attribute(AttributeDef *ad)
6724 int t, n;
6726 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6727 next();
6728 skip('(');
6729 skip('(');
6730 while (tok != ')') {
6731 if (tok < TOK_IDENT)
6732 expect("attribute name");
6733 t = tok;
6734 next();
6735 switch(t) {
6736 case TOK_SECTION1:
6737 case TOK_SECTION2:
6738 skip('(');
6739 if (tok != TOK_STR)
6740 expect("section name");
6741 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6742 next();
6743 skip(')');
6744 break;
6745 case TOK_ALIGNED1:
6746 case TOK_ALIGNED2:
6747 if (tok == '(') {
6748 next();
6749 n = expr_const();
6750 if (n <= 0 || (n & (n - 1)) != 0)
6751 error("alignment must be a positive power of two");
6752 skip(')');
6753 } else {
6754 n = MAX_ALIGN;
6756 ad->aligned = n;
6757 break;
6758 case TOK_PACKED1:
6759 case TOK_PACKED2:
6760 ad->packed = 1;
6761 break;
6762 case TOK_UNUSED1:
6763 case TOK_UNUSED2:
6764 /* currently, no need to handle it because tcc does not
6765 track unused objects */
6766 break;
6767 case TOK_NORETURN1:
6768 case TOK_NORETURN2:
6769 /* currently, no need to handle it because tcc does not
6770 track unused objects */
6771 break;
6772 case TOK_CDECL1:
6773 case TOK_CDECL2:
6774 case TOK_CDECL3:
6775 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6776 break;
6777 case TOK_STDCALL1:
6778 case TOK_STDCALL2:
6779 case TOK_STDCALL3:
6780 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6781 break;
6782 #ifdef TCC_TARGET_I386
6783 case TOK_REGPARM1:
6784 case TOK_REGPARM2:
6785 skip('(');
6786 n = expr_const();
6787 if (n > 3)
6788 n = 3;
6789 else if (n < 0)
6790 n = 0;
6791 if (n > 0)
6792 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6793 skip(')');
6794 break;
6795 case TOK_FASTCALL1:
6796 case TOK_FASTCALL2:
6797 case TOK_FASTCALL3:
6798 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6799 break;
6800 #endif
6801 case TOK_DLLEXPORT:
6802 FUNC_EXPORT(ad->func_attr) = 1;
6803 break;
6804 default:
6805 if (tcc_state->warn_unsupported)
6806 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6807 /* skip parameters */
6808 if (tok == '(') {
6809 int parenthesis = 0;
6810 do {
6811 if (tok == '(')
6812 parenthesis++;
6813 else if (tok == ')')
6814 parenthesis--;
6815 next();
6816 } while (parenthesis && tok != -1);
6818 break;
6820 if (tok != ',')
6821 break;
6822 next();
6824 skip(')');
6825 skip(')');
6829 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6830 static void struct_decl(CType *type, int u)
6832 int a, v, size, align, maxalign, c, offset;
6833 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
6834 Sym *s, *ss, *ass, **ps;
6835 AttributeDef ad;
6836 CType type1, btype;
6838 a = tok; /* save decl type */
6839 next();
6840 if (tok != '{') {
6841 v = tok;
6842 next();
6843 /* struct already defined ? return it */
6844 if (v < TOK_IDENT)
6845 expect("struct/union/enum name");
6846 s = struct_find(v);
6847 if (s) {
6848 if (s->type.t != a)
6849 error("invalid type");
6850 goto do_decl;
6852 } else {
6853 v = anon_sym++;
6855 type1.t = a;
6856 /* we put an undefined size for struct/union */
6857 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6858 s->r = 0; /* default alignment is zero as gcc */
6859 /* put struct/union/enum name in type */
6860 do_decl:
6861 type->t = u;
6862 type->ref = s;
6864 if (tok == '{') {
6865 next();
6866 if (s->c != -1)
6867 error("struct/union/enum already defined");
6868 /* cannot be empty */
6869 c = 0;
6870 /* non empty enums are not allowed */
6871 if (a == TOK_ENUM) {
6872 for(;;) {
6873 v = tok;
6874 if (v < TOK_UIDENT)
6875 expect("identifier");
6876 next();
6877 if (tok == '=') {
6878 next();
6879 c = expr_const();
6881 /* enum symbols have static storage */
6882 ss = sym_push(v, &int_type, VT_CONST, c);
6883 ss->type.t |= VT_STATIC;
6884 if (tok != ',')
6885 break;
6886 next();
6887 c++;
6888 /* NOTE: we accept a trailing comma */
6889 if (tok == '}')
6890 break;
6892 skip('}');
6893 } else {
6894 maxalign = 1;
6895 ps = &s->next;
6896 prevbt = VT_INT;
6897 bit_pos = 0;
6898 offset = 0;
6899 while (tok != '}') {
6900 parse_btype(&btype, &ad);
6901 while (1) {
6902 bit_size = -1;
6903 v = 0;
6904 type1 = btype;
6905 if (tok != ':') {
6906 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6907 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6908 expect("identifier");
6909 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6910 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6911 error("invalid type for '%s'",
6912 get_tok_str(v, NULL));
6914 if (tok == ':') {
6915 next();
6916 bit_size = expr_const();
6917 /* XXX: handle v = 0 case for messages */
6918 if (bit_size < 0)
6919 error("negative width in bit-field '%s'",
6920 get_tok_str(v, NULL));
6921 if (v && bit_size == 0)
6922 error("zero width for bit-field '%s'",
6923 get_tok_str(v, NULL));
6925 size = type_size(&type1, &align);
6926 if (ad.aligned) {
6927 if (align < ad.aligned)
6928 align = ad.aligned;
6929 } else if (ad.packed) {
6930 align = 1;
6931 } else if (*tcc_state->pack_stack_ptr) {
6932 if (align > *tcc_state->pack_stack_ptr)
6933 align = *tcc_state->pack_stack_ptr;
6935 lbit_pos = 0;
6936 if (bit_size >= 0) {
6937 bt = type1.t & VT_BTYPE;
6938 if (bt != VT_INT &&
6939 bt != VT_BYTE &&
6940 bt != VT_SHORT &&
6941 bt != VT_BOOL &&
6942 bt != VT_ENUM)
6943 error("bitfields must have scalar type");
6944 bsize = size * 8;
6945 if (bit_size > bsize) {
6946 error("width of '%s' exceeds its type",
6947 get_tok_str(v, NULL));
6948 } else if (bit_size == bsize) {
6949 /* no need for bit fields */
6950 bit_pos = 0;
6951 } else if (bit_size == 0) {
6952 /* XXX: what to do if only padding in a
6953 structure ? */
6954 /* zero size: means to pad */
6955 bit_pos = 0;
6956 } else {
6957 /* we do not have enough room ?
6958 did the type change?
6959 is it a union? */
6960 if ((bit_pos + bit_size) > bsize ||
6961 bt != prevbt || a == TOK_UNION)
6962 bit_pos = 0;
6963 lbit_pos = bit_pos;
6964 /* XXX: handle LSB first */
6965 type1.t |= VT_BITFIELD |
6966 (bit_pos << VT_STRUCT_SHIFT) |
6967 (bit_size << (VT_STRUCT_SHIFT + 6));
6968 bit_pos += bit_size;
6970 prevbt = bt;
6971 } else {
6972 bit_pos = 0;
6974 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
6975 /* add new memory data only if starting
6976 bit field */
6977 if (lbit_pos == 0) {
6978 if (a == TOK_STRUCT) {
6979 c = (c + align - 1) & -align;
6980 offset = c;
6981 if (size > 0)
6982 c += size;
6983 } else {
6984 offset = 0;
6985 if (size > c)
6986 c = size;
6988 if (align > maxalign)
6989 maxalign = align;
6991 #if 0
6992 printf("add field %s offset=%d",
6993 get_tok_str(v, NULL), offset);
6994 if (type1.t & VT_BITFIELD) {
6995 printf(" pos=%d size=%d",
6996 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6997 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6999 printf("\n");
7000 #endif
7002 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
7003 ass = type1.ref;
7004 while ((ass = ass->next) != NULL) {
7005 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
7006 *ps = ss;
7007 ps = &ss->next;
7009 } else if (v) {
7010 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
7011 *ps = ss;
7012 ps = &ss->next;
7014 if (tok == ';' || tok == TOK_EOF)
7015 break;
7016 skip(',');
7018 skip(';');
7020 skip('}');
7021 /* store size and alignment */
7022 s->c = (c + maxalign - 1) & -maxalign;
7023 s->r = maxalign;
7028 /* return 0 if no type declaration. otherwise, return the basic type
7029 and skip it.
7031 static int parse_btype(CType *type, AttributeDef *ad)
7033 int t, u, type_found, typespec_found, typedef_found;
7034 Sym *s;
7035 CType type1;
7037 memset(ad, 0, sizeof(AttributeDef));
7038 type_found = 0;
7039 typespec_found = 0;
7040 typedef_found = 0;
7041 t = 0;
7042 while(1) {
7043 switch(tok) {
7044 case TOK_EXTENSION:
7045 /* currently, we really ignore extension */
7046 next();
7047 continue;
7049 /* basic types */
7050 case TOK_CHAR:
7051 u = VT_BYTE;
7052 basic_type:
7053 next();
7054 basic_type1:
7055 if ((t & VT_BTYPE) != 0)
7056 error("too many basic types");
7057 t |= u;
7058 typespec_found = 1;
7059 break;
7060 case TOK_VOID:
7061 u = VT_VOID;
7062 goto basic_type;
7063 case TOK_SHORT:
7064 u = VT_SHORT;
7065 goto basic_type;
7066 case TOK_INT:
7067 next();
7068 typespec_found = 1;
7069 break;
7070 case TOK_LONG:
7071 next();
7072 if ((t & VT_BTYPE) == VT_DOUBLE) {
7073 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
7074 } else if ((t & VT_BTYPE) == VT_LONG) {
7075 t = (t & ~VT_BTYPE) | VT_LLONG;
7076 } else {
7077 u = VT_LONG;
7078 goto basic_type1;
7080 break;
7081 case TOK_BOOL:
7082 u = VT_BOOL;
7083 goto basic_type;
7084 case TOK_FLOAT:
7085 u = VT_FLOAT;
7086 goto basic_type;
7087 case TOK_DOUBLE:
7088 next();
7089 if ((t & VT_BTYPE) == VT_LONG) {
7090 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
7091 } else {
7092 u = VT_DOUBLE;
7093 goto basic_type1;
7095 break;
7096 case TOK_ENUM:
7097 struct_decl(&type1, VT_ENUM);
7098 basic_type2:
7099 u = type1.t;
7100 type->ref = type1.ref;
7101 goto basic_type1;
7102 case TOK_STRUCT:
7103 case TOK_UNION:
7104 struct_decl(&type1, VT_STRUCT);
7105 goto basic_type2;
7107 /* type modifiers */
7108 case TOK_CONST1:
7109 case TOK_CONST2:
7110 case TOK_CONST3:
7111 t |= VT_CONSTANT;
7112 next();
7113 break;
7114 case TOK_VOLATILE1:
7115 case TOK_VOLATILE2:
7116 case TOK_VOLATILE3:
7117 t |= VT_VOLATILE;
7118 next();
7119 break;
7120 case TOK_SIGNED1:
7121 case TOK_SIGNED2:
7122 case TOK_SIGNED3:
7123 typespec_found = 1;
7124 t |= VT_SIGNED;
7125 next();
7126 break;
7127 case TOK_REGISTER:
7128 case TOK_AUTO:
7129 case TOK_RESTRICT1:
7130 case TOK_RESTRICT2:
7131 case TOK_RESTRICT3:
7132 next();
7133 break;
7134 case TOK_UNSIGNED:
7135 t |= VT_UNSIGNED;
7136 next();
7137 typespec_found = 1;
7138 break;
7140 /* storage */
7141 case TOK_EXTERN:
7142 t |= VT_EXTERN;
7143 next();
7144 break;
7145 case TOK_STATIC:
7146 t |= VT_STATIC;
7147 next();
7148 break;
7149 case TOK_TYPEDEF:
7150 t |= VT_TYPEDEF;
7151 next();
7152 break;
7153 case TOK_INLINE1:
7154 case TOK_INLINE2:
7155 case TOK_INLINE3:
7156 t |= VT_INLINE;
7157 next();
7158 break;
7160 /* GNUC attribute */
7161 case TOK_ATTRIBUTE1:
7162 case TOK_ATTRIBUTE2:
7163 parse_attribute(ad);
7164 break;
7165 /* GNUC typeof */
7166 case TOK_TYPEOF1:
7167 case TOK_TYPEOF2:
7168 case TOK_TYPEOF3:
7169 next();
7170 parse_expr_type(&type1);
7171 goto basic_type2;
7172 default:
7173 if (typespec_found || typedef_found)
7174 goto the_end;
7175 s = sym_find(tok);
7176 if (!s || !(s->type.t & VT_TYPEDEF))
7177 goto the_end;
7178 typedef_found = 1;
7179 t |= (s->type.t & ~VT_TYPEDEF);
7180 type->ref = s->type.ref;
7181 next();
7182 typespec_found = 1;
7183 break;
7185 type_found = 1;
7187 the_end:
7188 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7189 error("signed and unsigned modifier");
7190 if (tcc_state->char_is_unsigned) {
7191 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7192 t |= VT_UNSIGNED;
7194 t &= ~VT_SIGNED;
7196 /* long is never used as type */
7197 if ((t & VT_BTYPE) == VT_LONG)
7198 #ifndef TCC_TARGET_X86_64
7199 t = (t & ~VT_BTYPE) | VT_INT;
7200 #else
7201 t = (t & ~VT_BTYPE) | VT_LLONG;
7202 #endif
7203 type->t = t;
7204 return type_found;
7207 /* convert a function parameter type (array to pointer and function to
7208 function pointer) */
7209 static inline void convert_parameter_type(CType *pt)
7211 /* remove const and volatile qualifiers (XXX: const could be used
7212 to indicate a const function parameter */
7213 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7214 /* array must be transformed to pointer according to ANSI C */
7215 pt->t &= ~VT_ARRAY;
7216 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7217 mk_pointer(pt);
7221 static void post_type(CType *type, AttributeDef *ad)
7223 int n, l, t1, arg_size, align;
7224 Sym **plast, *s, *first;
7225 AttributeDef ad1;
7226 CType pt;
7228 if (tok == '(') {
7229 /* function declaration */
7230 next();
7231 l = 0;
7232 first = NULL;
7233 plast = &first;
7234 arg_size = 0;
7235 if (tok != ')') {
7236 for(;;) {
7237 /* read param name and compute offset */
7238 if (l != FUNC_OLD) {
7239 if (!parse_btype(&pt, &ad1)) {
7240 if (l) {
7241 error("invalid type");
7242 } else {
7243 l = FUNC_OLD;
7244 goto old_proto;
7247 l = FUNC_NEW;
7248 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7249 break;
7250 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7251 if ((pt.t & VT_BTYPE) == VT_VOID)
7252 error("parameter declared as void");
7253 arg_size += (type_size(&pt, &align) + 3) & ~3;
7254 } else {
7255 old_proto:
7256 n = tok;
7257 if (n < TOK_UIDENT)
7258 expect("identifier");
7259 pt.t = VT_INT;
7260 next();
7262 convert_parameter_type(&pt);
7263 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7264 *plast = s;
7265 plast = &s->next;
7266 if (tok == ')')
7267 break;
7268 skip(',');
7269 if (l == FUNC_NEW && tok == TOK_DOTS) {
7270 l = FUNC_ELLIPSIS;
7271 next();
7272 break;
7276 /* if no parameters, then old type prototype */
7277 if (l == 0)
7278 l = FUNC_OLD;
7279 skip(')');
7280 t1 = type->t & VT_STORAGE;
7281 /* NOTE: const is ignored in returned type as it has a special
7282 meaning in gcc / C++ */
7283 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7284 post_type(type, ad);
7285 /* we push a anonymous symbol which will contain the function prototype */
7286 FUNC_ARGS(ad->func_attr) = arg_size;
7287 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7288 s->next = first;
7289 type->t = t1 | VT_FUNC;
7290 type->ref = s;
7291 } else if (tok == '[') {
7292 /* array definition */
7293 next();
7294 n = -1;
7295 if (tok != ']') {
7296 n = expr_const();
7297 if (n < 0)
7298 error("invalid array size");
7300 skip(']');
7301 /* parse next post type */
7302 t1 = type->t & VT_STORAGE;
7303 type->t &= ~VT_STORAGE;
7304 post_type(type, ad);
7306 /* we push a anonymous symbol which will contain the array
7307 element type */
7308 s = sym_push(SYM_FIELD, type, 0, n);
7309 type->t = t1 | VT_ARRAY | VT_PTR;
7310 type->ref = s;
7314 /* Parse a type declaration (except basic type), and return the type
7315 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7316 expected. 'type' should contain the basic type. 'ad' is the
7317 attribute definition of the basic type. It can be modified by
7318 type_decl().
7320 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7322 Sym *s;
7323 CType type1, *type2;
7324 int qualifiers;
7326 while (tok == '*') {
7327 qualifiers = 0;
7328 redo:
7329 next();
7330 switch(tok) {
7331 case TOK_CONST1:
7332 case TOK_CONST2:
7333 case TOK_CONST3:
7334 qualifiers |= VT_CONSTANT;
7335 goto redo;
7336 case TOK_VOLATILE1:
7337 case TOK_VOLATILE2:
7338 case TOK_VOLATILE3:
7339 qualifiers |= VT_VOLATILE;
7340 goto redo;
7341 case TOK_RESTRICT1:
7342 case TOK_RESTRICT2:
7343 case TOK_RESTRICT3:
7344 goto redo;
7346 mk_pointer(type);
7347 type->t |= qualifiers;
7350 /* XXX: clarify attribute handling */
7351 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7352 parse_attribute(ad);
7354 /* recursive type */
7355 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7356 type1.t = 0; /* XXX: same as int */
7357 if (tok == '(') {
7358 next();
7359 /* XXX: this is not correct to modify 'ad' at this point, but
7360 the syntax is not clear */
7361 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7362 parse_attribute(ad);
7363 type_decl(&type1, ad, v, td);
7364 skip(')');
7365 } else {
7366 /* type identifier */
7367 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7368 *v = tok;
7369 next();
7370 } else {
7371 if (!(td & TYPE_ABSTRACT))
7372 expect("identifier");
7373 *v = 0;
7376 post_type(type, ad);
7377 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7378 parse_attribute(ad);
7379 if (!type1.t)
7380 return;
7381 /* append type at the end of type1 */
7382 type2 = &type1;
7383 for(;;) {
7384 s = type2->ref;
7385 type2 = &s->type;
7386 if (!type2->t) {
7387 *type2 = *type;
7388 break;
7391 *type = type1;
7394 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7395 static int lvalue_type(int t)
7397 int bt, r;
7398 r = VT_LVAL;
7399 bt = t & VT_BTYPE;
7400 if (bt == VT_BYTE || bt == VT_BOOL)
7401 r |= VT_LVAL_BYTE;
7402 else if (bt == VT_SHORT)
7403 r |= VT_LVAL_SHORT;
7404 else
7405 return r;
7406 if (t & VT_UNSIGNED)
7407 r |= VT_LVAL_UNSIGNED;
7408 return r;
7411 /* indirection with full error checking and bound check */
7412 static void indir(void)
7414 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7415 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7416 return;
7417 expect("pointer");
7419 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7420 gv(RC_INT);
7421 vtop->type = *pointed_type(&vtop->type);
7422 /* Arrays and functions are never lvalues */
7423 if (!(vtop->type.t & VT_ARRAY)
7424 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7425 vtop->r |= lvalue_type(vtop->type.t);
7426 /* if bound checking, the referenced pointer must be checked */
7427 if (do_bounds_check)
7428 vtop->r |= VT_MUSTBOUND;
7432 /* pass a parameter to a function and do type checking and casting */
7433 static void gfunc_param_typed(Sym *func, Sym *arg)
7435 int func_type;
7436 CType type;
7438 func_type = func->c;
7439 if (func_type == FUNC_OLD ||
7440 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7441 /* default casting : only need to convert float to double */
7442 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7443 type.t = VT_DOUBLE;
7444 gen_cast(&type);
7446 } else if (arg == NULL) {
7447 error("too many arguments to function");
7448 } else {
7449 type = arg->type;
7450 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7451 gen_assign_cast(&type);
7455 /* parse an expression of the form '(type)' or '(expr)' and return its
7456 type */
7457 static void parse_expr_type(CType *type)
7459 int n;
7460 AttributeDef ad;
7462 skip('(');
7463 if (parse_btype(type, &ad)) {
7464 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7465 } else {
7466 expr_type(type);
7468 skip(')');
7471 static void parse_type(CType *type)
7473 AttributeDef ad;
7474 int n;
7476 if (!parse_btype(type, &ad)) {
7477 expect("type");
7479 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7482 static void vpush_tokc(int t)
7484 CType type;
7485 type.t = t;
7486 vsetc(&type, VT_CONST, &tokc);
7489 static void unary(void)
7491 int n, t, align, size, r;
7492 CType type;
7493 Sym *s;
7494 AttributeDef ad;
7496 /* XXX: GCC 2.95.3 does not generate a table although it should be
7497 better here */
7498 tok_next:
7499 switch(tok) {
7500 case TOK_EXTENSION:
7501 next();
7502 goto tok_next;
7503 case TOK_CINT:
7504 case TOK_CCHAR:
7505 case TOK_LCHAR:
7506 vpushi(tokc.i);
7507 next();
7508 break;
7509 case TOK_CUINT:
7510 vpush_tokc(VT_INT | VT_UNSIGNED);
7511 next();
7512 break;
7513 case TOK_CLLONG:
7514 vpush_tokc(VT_LLONG);
7515 next();
7516 break;
7517 case TOK_CULLONG:
7518 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7519 next();
7520 break;
7521 case TOK_CFLOAT:
7522 vpush_tokc(VT_FLOAT);
7523 next();
7524 break;
7525 case TOK_CDOUBLE:
7526 vpush_tokc(VT_DOUBLE);
7527 next();
7528 break;
7529 case TOK_CLDOUBLE:
7530 vpush_tokc(VT_LDOUBLE);
7531 next();
7532 break;
7533 case TOK___FUNCTION__:
7534 if (!gnu_ext)
7535 goto tok_identifier;
7536 /* fall thru */
7537 case TOK___FUNC__:
7539 void *ptr;
7540 int len;
7541 /* special function name identifier */
7542 len = strlen(funcname) + 1;
7543 /* generate char[len] type */
7544 type.t = VT_BYTE;
7545 mk_pointer(&type);
7546 type.t |= VT_ARRAY;
7547 type.ref->c = len;
7548 vpush_ref(&type, data_section, data_section->data_offset, len);
7549 ptr = section_ptr_add(data_section, len);
7550 memcpy(ptr, funcname, len);
7551 next();
7553 break;
7554 case TOK_LSTR:
7555 #ifdef TCC_TARGET_PE
7556 t = VT_SHORT | VT_UNSIGNED;
7557 #else
7558 t = VT_INT;
7559 #endif
7560 goto str_init;
7561 case TOK_STR:
7562 /* string parsing */
7563 t = VT_BYTE;
7564 str_init:
7565 if (tcc_state->warn_write_strings)
7566 t |= VT_CONSTANT;
7567 type.t = t;
7568 mk_pointer(&type);
7569 type.t |= VT_ARRAY;
7570 memset(&ad, 0, sizeof(AttributeDef));
7571 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7572 break;
7573 case '(':
7574 next();
7575 /* cast ? */
7576 if (parse_btype(&type, &ad)) {
7577 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7578 skip(')');
7579 /* check ISOC99 compound literal */
7580 if (tok == '{') {
7581 /* data is allocated locally by default */
7582 if (global_expr)
7583 r = VT_CONST;
7584 else
7585 r = VT_LOCAL;
7586 /* all except arrays are lvalues */
7587 if (!(type.t & VT_ARRAY))
7588 r |= lvalue_type(type.t);
7589 memset(&ad, 0, sizeof(AttributeDef));
7590 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7591 } else {
7592 unary();
7593 gen_cast(&type);
7595 } else if (tok == '{') {
7596 /* save all registers */
7597 save_regs(0);
7598 /* statement expression : we do not accept break/continue
7599 inside as GCC does */
7600 block(NULL, NULL, NULL, NULL, 0, 1);
7601 skip(')');
7602 } else {
7603 gexpr();
7604 skip(')');
7606 break;
7607 case '*':
7608 next();
7609 unary();
7610 indir();
7611 break;
7612 case '&':
7613 next();
7614 unary();
7615 /* functions names must be treated as function pointers,
7616 except for unary '&' and sizeof. Since we consider that
7617 functions are not lvalues, we only have to handle it
7618 there and in function calls. */
7619 /* arrays can also be used although they are not lvalues */
7620 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7621 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7622 test_lvalue();
7623 mk_pointer(&vtop->type);
7624 gaddrof();
7625 break;
7626 case '!':
7627 next();
7628 unary();
7629 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
7630 CType boolean;
7631 boolean.t = VT_BOOL;
7632 gen_cast(&boolean);
7633 vtop->c.i = !vtop->c.i;
7634 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
7635 vtop->c.i = vtop->c.i ^ 1;
7636 else {
7637 save_regs(1);
7638 vseti(VT_JMP, gtst(1, 0));
7640 break;
7641 case '~':
7642 next();
7643 unary();
7644 vpushi(-1);
7645 gen_op('^');
7646 break;
7647 case '+':
7648 next();
7649 /* in order to force cast, we add zero */
7650 unary();
7651 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7652 error("pointer not accepted for unary plus");
7653 vpushi(0);
7654 gen_op('+');
7655 break;
7656 case TOK_SIZEOF:
7657 case TOK_ALIGNOF1:
7658 case TOK_ALIGNOF2:
7659 t = tok;
7660 next();
7661 if (tok == '(') {
7662 parse_expr_type(&type);
7663 } else {
7664 unary_type(&type);
7666 size = type_size(&type, &align);
7667 if (t == TOK_SIZEOF) {
7668 if (size < 0)
7669 error("sizeof applied to an incomplete type");
7670 vpushi(size);
7671 } else {
7672 vpushi(align);
7674 vtop->type.t |= VT_UNSIGNED;
7675 break;
7677 case TOK_builtin_types_compatible_p:
7679 CType type1, type2;
7680 next();
7681 skip('(');
7682 parse_type(&type1);
7683 skip(',');
7684 parse_type(&type2);
7685 skip(')');
7686 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7687 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7688 vpushi(is_compatible_types(&type1, &type2));
7690 break;
7691 case TOK_builtin_constant_p:
7693 int saved_nocode_wanted, res;
7694 next();
7695 skip('(');
7696 saved_nocode_wanted = nocode_wanted;
7697 nocode_wanted = 1;
7698 gexpr();
7699 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7700 vpop();
7701 nocode_wanted = saved_nocode_wanted;
7702 skip(')');
7703 vpushi(res);
7705 break;
7706 case TOK_builtin_frame_address:
7708 CType type;
7709 next();
7710 skip('(');
7711 if (tok != TOK_CINT) {
7712 error("__builtin_frame_address only takes integers");
7714 if (tokc.i != 0) {
7715 error("TCC only supports __builtin_frame_address(0)");
7717 next();
7718 skip(')');
7719 type.t = VT_VOID;
7720 mk_pointer(&type);
7721 vset(&type, VT_LOCAL, 0);
7723 break;
7724 case TOK_INC:
7725 case TOK_DEC:
7726 t = tok;
7727 next();
7728 unary();
7729 inc(0, t);
7730 break;
7731 case '-':
7732 next();
7733 vpushi(0);
7734 unary();
7735 gen_op('-');
7736 break;
7737 case TOK_LAND:
7738 if (!gnu_ext)
7739 goto tok_identifier;
7740 next();
7741 /* allow to take the address of a label */
7742 if (tok < TOK_UIDENT)
7743 expect("label identifier");
7744 s = label_find(tok);
7745 if (!s) {
7746 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7747 } else {
7748 if (s->r == LABEL_DECLARED)
7749 s->r = LABEL_FORWARD;
7751 if (!s->type.t) {
7752 s->type.t = VT_VOID;
7753 mk_pointer(&s->type);
7754 s->type.t |= VT_STATIC;
7756 vset(&s->type, VT_CONST | VT_SYM, 0);
7757 vtop->sym = s;
7758 next();
7759 break;
7760 default:
7761 tok_identifier:
7762 t = tok;
7763 next();
7764 if (t < TOK_UIDENT)
7765 expect("identifier");
7766 s = sym_find(t);
7767 if (!s) {
7768 if (tok != '(')
7769 error("'%s' undeclared", get_tok_str(t, NULL));
7770 /* for simple function calls, we tolerate undeclared
7771 external reference to int() function */
7772 if (tcc_state->warn_implicit_function_declaration)
7773 warning("implicit declaration of function '%s'",
7774 get_tok_str(t, NULL));
7775 s = external_global_sym(t, &func_old_type, 0);
7777 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7778 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7779 /* if referencing an inline function, then we generate a
7780 symbol to it if not already done. It will have the
7781 effect to generate code for it at the end of the
7782 compilation unit. Inline function as always
7783 generated in the text section. */
7784 if (!s->c)
7785 put_extern_sym(s, text_section, 0, 0);
7786 r = VT_SYM | VT_CONST;
7787 } else {
7788 r = s->r;
7790 vset(&s->type, r, s->c);
7791 /* if forward reference, we must point to s */
7792 if (vtop->r & VT_SYM) {
7793 vtop->sym = s;
7794 vtop->c.ul = 0;
7796 break;
7799 /* post operations */
7800 while (1) {
7801 if (tok == TOK_INC || tok == TOK_DEC) {
7802 inc(1, tok);
7803 next();
7804 } else if (tok == '.' || tok == TOK_ARROW) {
7805 /* field */
7806 if (tok == TOK_ARROW)
7807 indir();
7808 test_lvalue();
7809 gaddrof();
7810 next();
7811 /* expect pointer on structure */
7812 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7813 expect("struct or union");
7814 s = vtop->type.ref;
7815 /* find field */
7816 tok |= SYM_FIELD;
7817 while ((s = s->next) != NULL) {
7818 if (s->v == tok)
7819 break;
7821 if (!s)
7822 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7823 /* add field offset to pointer */
7824 vtop->type = char_pointer_type; /* change type to 'char *' */
7825 vpushi(s->c);
7826 gen_op('+');
7827 /* change type to field type, and set to lvalue */
7828 vtop->type = s->type;
7829 /* an array is never an lvalue */
7830 if (!(vtop->type.t & VT_ARRAY)) {
7831 vtop->r |= lvalue_type(vtop->type.t);
7832 /* if bound checking, the referenced pointer must be checked */
7833 if (do_bounds_check)
7834 vtop->r |= VT_MUSTBOUND;
7836 next();
7837 } else if (tok == '[') {
7838 next();
7839 gexpr();
7840 gen_op('+');
7841 indir();
7842 skip(']');
7843 } else if (tok == '(') {
7844 SValue ret;
7845 Sym *sa;
7846 int nb_args;
7848 /* function call */
7849 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7850 /* pointer test (no array accepted) */
7851 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7852 vtop->type = *pointed_type(&vtop->type);
7853 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7854 goto error_func;
7855 } else {
7856 error_func:
7857 expect("function pointer");
7859 } else {
7860 vtop->r &= ~VT_LVAL; /* no lvalue */
7862 /* get return type */
7863 s = vtop->type.ref;
7864 next();
7865 sa = s->next; /* first parameter */
7866 nb_args = 0;
7867 ret.r2 = VT_CONST;
7868 /* compute first implicit argument if a structure is returned */
7869 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7870 /* get some space for the returned structure */
7871 size = type_size(&s->type, &align);
7872 loc = (loc - size) & -align;
7873 ret.type = s->type;
7874 ret.r = VT_LOCAL | VT_LVAL;
7875 /* pass it as 'int' to avoid structure arg passing
7876 problems */
7877 vseti(VT_LOCAL, loc);
7878 ret.c = vtop->c;
7879 nb_args++;
7880 } else {
7881 ret.type = s->type;
7882 /* return in register */
7883 if (is_float(ret.type.t)) {
7884 ret.r = REG_FRET;
7885 } else {
7886 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7887 ret.r2 = REG_LRET;
7888 ret.r = REG_IRET;
7890 ret.c.i = 0;
7892 if (tok != ')') {
7893 for(;;) {
7894 expr_eq();
7895 gfunc_param_typed(s, sa);
7896 nb_args++;
7897 if (sa)
7898 sa = sa->next;
7899 if (tok == ')')
7900 break;
7901 skip(',');
7904 if (sa)
7905 error("too few arguments to function");
7906 skip(')');
7907 if (!nocode_wanted) {
7908 gfunc_call(nb_args);
7909 } else {
7910 vtop -= (nb_args + 1);
7912 /* return value */
7913 vsetc(&ret.type, ret.r, &ret.c);
7914 vtop->r2 = ret.r2;
7915 } else {
7916 break;
7921 static void uneq(void)
7923 int t;
7925 unary();
7926 if (tok == '=' ||
7927 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7928 tok == TOK_A_XOR || tok == TOK_A_OR ||
7929 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7930 test_lvalue();
7931 t = tok;
7932 next();
7933 if (t == '=') {
7934 expr_eq();
7935 } else {
7936 vdup();
7937 expr_eq();
7938 gen_op(t & 0x7f);
7940 vstore();
7944 static void expr_prod(void)
7946 int t;
7948 uneq();
7949 while (tok == '*' || tok == '/' || tok == '%') {
7950 t = tok;
7951 next();
7952 uneq();
7953 gen_op(t);
7957 static void expr_sum(void)
7959 int t;
7961 expr_prod();
7962 while (tok == '+' || tok == '-') {
7963 t = tok;
7964 next();
7965 expr_prod();
7966 gen_op(t);
7970 static void expr_shift(void)
7972 int t;
7974 expr_sum();
7975 while (tok == TOK_SHL || tok == TOK_SAR) {
7976 t = tok;
7977 next();
7978 expr_sum();
7979 gen_op(t);
7983 static void expr_cmp(void)
7985 int t;
7987 expr_shift();
7988 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7989 tok == TOK_ULT || tok == TOK_UGE) {
7990 t = tok;
7991 next();
7992 expr_shift();
7993 gen_op(t);
7997 static void expr_cmpeq(void)
7999 int t;
8001 expr_cmp();
8002 while (tok == TOK_EQ || tok == TOK_NE) {
8003 t = tok;
8004 next();
8005 expr_cmp();
8006 gen_op(t);
8010 static void expr_and(void)
8012 expr_cmpeq();
8013 while (tok == '&') {
8014 next();
8015 expr_cmpeq();
8016 gen_op('&');
8020 static void expr_xor(void)
8022 expr_and();
8023 while (tok == '^') {
8024 next();
8025 expr_and();
8026 gen_op('^');
8030 static void expr_or(void)
8032 expr_xor();
8033 while (tok == '|') {
8034 next();
8035 expr_xor();
8036 gen_op('|');
8040 /* XXX: fix this mess */
8041 static void expr_land_const(void)
8043 expr_or();
8044 while (tok == TOK_LAND) {
8045 next();
8046 expr_or();
8047 gen_op(TOK_LAND);
8051 /* XXX: fix this mess */
8052 static void expr_lor_const(void)
8054 expr_land_const();
8055 while (tok == TOK_LOR) {
8056 next();
8057 expr_land_const();
8058 gen_op(TOK_LOR);
8062 /* only used if non constant */
8063 static void expr_land(void)
8065 int t;
8067 expr_or();
8068 if (tok == TOK_LAND) {
8069 t = 0;
8070 save_regs(1);
8071 for(;;) {
8072 t = gtst(1, t);
8073 if (tok != TOK_LAND) {
8074 vseti(VT_JMPI, t);
8075 break;
8077 next();
8078 expr_or();
8083 static void expr_lor(void)
8085 int t;
8087 expr_land();
8088 if (tok == TOK_LOR) {
8089 t = 0;
8090 save_regs(1);
8091 for(;;) {
8092 t = gtst(0, t);
8093 if (tok != TOK_LOR) {
8094 vseti(VT_JMP, t);
8095 break;
8097 next();
8098 expr_land();
8103 /* XXX: better constant handling */
8104 static void expr_eq(void)
8106 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
8107 SValue sv;
8108 CType type, type1, type2;
8110 if (const_wanted) {
8111 expr_lor_const();
8112 if (tok == '?') {
8113 CType boolean;
8114 int c;
8115 boolean.t = VT_BOOL;
8116 vdup();
8117 gen_cast(&boolean);
8118 c = vtop->c.i;
8119 vpop();
8120 next();
8121 if (tok != ':' || !gnu_ext) {
8122 vpop();
8123 gexpr();
8125 if (!c)
8126 vpop();
8127 skip(':');
8128 expr_eq();
8129 if (c)
8130 vpop();
8132 } else {
8133 expr_lor();
8134 if (tok == '?') {
8135 next();
8136 if (vtop != vstack) {
8137 /* needed to avoid having different registers saved in
8138 each branch */
8139 if (is_float(vtop->type.t)) {
8140 rc = RC_FLOAT;
8141 #ifdef TCC_TARGET_X86_64
8142 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
8143 rc = RC_ST0;
8145 #endif
8147 else
8148 rc = RC_INT;
8149 gv(rc);
8150 save_regs(1);
8152 if (tok == ':' && gnu_ext) {
8153 gv_dup();
8154 tt = gtst(1, 0);
8155 } else {
8156 tt = gtst(1, 0);
8157 gexpr();
8159 type1 = vtop->type;
8160 sv = *vtop; /* save value to handle it later */
8161 vtop--; /* no vpop so that FP stack is not flushed */
8162 skip(':');
8163 u = gjmp(0);
8164 gsym(tt);
8165 expr_eq();
8166 type2 = vtop->type;
8168 t1 = type1.t;
8169 bt1 = t1 & VT_BTYPE;
8170 t2 = type2.t;
8171 bt2 = t2 & VT_BTYPE;
8172 /* cast operands to correct type according to ISOC rules */
8173 if (is_float(bt1) || is_float(bt2)) {
8174 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8175 type.t = VT_LDOUBLE;
8176 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8177 type.t = VT_DOUBLE;
8178 } else {
8179 type.t = VT_FLOAT;
8181 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8182 /* cast to biggest op */
8183 type.t = VT_LLONG;
8184 /* convert to unsigned if it does not fit in a long long */
8185 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8186 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8187 type.t |= VT_UNSIGNED;
8188 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8189 /* XXX: test pointer compatibility */
8190 type = type1;
8191 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8192 /* XXX: test function pointer compatibility */
8193 type = type1;
8194 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8195 /* XXX: test structure compatibility */
8196 type = type1;
8197 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8198 /* NOTE: as an extension, we accept void on only one side */
8199 type.t = VT_VOID;
8200 } else {
8201 /* integer operations */
8202 type.t = VT_INT;
8203 /* convert to unsigned if it does not fit in an integer */
8204 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8205 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8206 type.t |= VT_UNSIGNED;
8209 /* now we convert second operand */
8210 gen_cast(&type);
8211 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8212 gaddrof();
8213 rc = RC_INT;
8214 if (is_float(type.t)) {
8215 rc = RC_FLOAT;
8216 #ifdef TCC_TARGET_X86_64
8217 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
8218 rc = RC_ST0;
8220 #endif
8221 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8222 /* for long longs, we use fixed registers to avoid having
8223 to handle a complicated move */
8224 rc = RC_IRET;
8227 r2 = gv(rc);
8228 /* this is horrible, but we must also convert first
8229 operand */
8230 tt = gjmp(0);
8231 gsym(u);
8232 /* put again first value and cast it */
8233 *vtop = sv;
8234 gen_cast(&type);
8235 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8236 gaddrof();
8237 r1 = gv(rc);
8238 move_reg(r2, r1);
8239 vtop->r = r2;
8240 gsym(tt);
8245 static void gexpr(void)
8247 while (1) {
8248 expr_eq();
8249 if (tok != ',')
8250 break;
8251 vpop();
8252 next();
8256 /* parse an expression and return its type without any side effect. */
8257 static void expr_type(CType *type)
8259 int saved_nocode_wanted;
8261 saved_nocode_wanted = nocode_wanted;
8262 nocode_wanted = 1;
8263 gexpr();
8264 *type = vtop->type;
8265 vpop();
8266 nocode_wanted = saved_nocode_wanted;
8269 /* parse a unary expression and return its type without any side
8270 effect. */
8271 static void unary_type(CType *type)
8273 int a;
8275 a = nocode_wanted;
8276 nocode_wanted = 1;
8277 unary();
8278 *type = vtop->type;
8279 vpop();
8280 nocode_wanted = a;
8283 /* parse a constant expression and return value in vtop. */
8284 static void expr_const1(void)
8286 int a;
8287 a = const_wanted;
8288 const_wanted = 1;
8289 expr_eq();
8290 const_wanted = a;
8293 /* parse an integer constant and return its value. */
8294 static int expr_const(void)
8296 int c;
8297 expr_const1();
8298 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8299 expect("constant expression");
8300 c = vtop->c.i;
8301 vpop();
8302 return c;
8305 /* return the label token if current token is a label, otherwise
8306 return zero */
8307 static int is_label(void)
8309 int last_tok;
8311 /* fast test first */
8312 if (tok < TOK_UIDENT)
8313 return 0;
8314 /* no need to save tokc because tok is an identifier */
8315 last_tok = tok;
8316 next();
8317 if (tok == ':') {
8318 next();
8319 return last_tok;
8320 } else {
8321 unget_tok(last_tok);
8322 return 0;
8326 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8327 int case_reg, int is_expr)
8329 int a, b, c, d;
8330 Sym *s;
8332 /* generate line number info */
8333 if (do_debug &&
8334 (last_line_num != file->line_num || last_ind != ind)) {
8335 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8336 last_ind = ind;
8337 last_line_num = file->line_num;
8340 if (is_expr) {
8341 /* default return value is (void) */
8342 vpushi(0);
8343 vtop->type.t = VT_VOID;
8346 if (tok == TOK_IF) {
8347 /* if test */
8348 next();
8349 skip('(');
8350 gexpr();
8351 skip(')');
8352 a = gtst(1, 0);
8353 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8354 c = tok;
8355 if (c == TOK_ELSE) {
8356 next();
8357 d = gjmp(0);
8358 gsym(a);
8359 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8360 gsym(d); /* patch else jmp */
8361 } else
8362 gsym(a);
8363 } else if (tok == TOK_WHILE) {
8364 next();
8365 d = ind;
8366 skip('(');
8367 gexpr();
8368 skip(')');
8369 a = gtst(1, 0);
8370 b = 0;
8371 block(&a, &b, case_sym, def_sym, case_reg, 0);
8372 gjmp_addr(d);
8373 gsym(a);
8374 gsym_addr(b, d);
8375 } else if (tok == '{') {
8376 Sym *llabel;
8378 next();
8379 /* record local declaration stack position */
8380 s = local_stack;
8381 llabel = local_label_stack;
8382 /* handle local labels declarations */
8383 if (tok == TOK_LABEL) {
8384 next();
8385 for(;;) {
8386 if (tok < TOK_UIDENT)
8387 expect("label identifier");
8388 label_push(&local_label_stack, tok, LABEL_DECLARED);
8389 next();
8390 if (tok == ',') {
8391 next();
8392 } else {
8393 skip(';');
8394 break;
8398 while (tok != '}') {
8399 decl(VT_LOCAL);
8400 if (tok != '}') {
8401 if (is_expr)
8402 vpop();
8403 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8406 /* pop locally defined labels */
8407 label_pop(&local_label_stack, llabel);
8408 /* pop locally defined symbols */
8409 if(is_expr) {
8410 /* XXX: this solution makes only valgrind happy...
8411 triggered by gcc.c-torture/execute/20000917-1.c */
8412 Sym *p;
8413 switch(vtop->type.t & VT_BTYPE) {
8414 case VT_PTR:
8415 case VT_STRUCT:
8416 case VT_ENUM:
8417 case VT_FUNC:
8418 for(p=vtop->type.ref;p;p=p->prev)
8419 if(p->prev==s)
8420 error("unsupported expression type");
8423 sym_pop(&local_stack, s);
8424 next();
8425 } else if (tok == TOK_RETURN) {
8426 next();
8427 if (tok != ';') {
8428 gexpr();
8429 gen_assign_cast(&func_vt);
8430 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8431 CType type;
8432 /* if returning structure, must copy it to implicit
8433 first pointer arg location */
8434 #ifdef TCC_ARM_EABI
8435 int align, size;
8436 size = type_size(&func_vt,&align);
8437 if(size <= 4)
8439 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8440 && (align & 3))
8442 int addr;
8443 loc = (loc - size) & -4;
8444 addr = loc;
8445 type = func_vt;
8446 vset(&type, VT_LOCAL | VT_LVAL, addr);
8447 vswap();
8448 vstore();
8449 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8451 vtop->type = int_type;
8452 gv(RC_IRET);
8453 } else {
8454 #endif
8455 type = func_vt;
8456 mk_pointer(&type);
8457 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8458 indir();
8459 vswap();
8460 /* copy structure value to pointer */
8461 vstore();
8462 #ifdef TCC_ARM_EABI
8464 #endif
8465 } else if (is_float(func_vt.t)) {
8466 gv(RC_FRET);
8467 } else {
8468 gv(RC_IRET);
8470 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8472 skip(';');
8473 rsym = gjmp(rsym); /* jmp */
8474 } else if (tok == TOK_BREAK) {
8475 /* compute jump */
8476 if (!bsym)
8477 error("cannot break");
8478 *bsym = gjmp(*bsym);
8479 next();
8480 skip(';');
8481 } else if (tok == TOK_CONTINUE) {
8482 /* compute jump */
8483 if (!csym)
8484 error("cannot continue");
8485 *csym = gjmp(*csym);
8486 next();
8487 skip(';');
8488 } else if (tok == TOK_FOR) {
8489 int e;
8490 next();
8491 skip('(');
8492 if (tok != ';') {
8493 gexpr();
8494 vpop();
8496 skip(';');
8497 d = ind;
8498 c = ind;
8499 a = 0;
8500 b = 0;
8501 if (tok != ';') {
8502 gexpr();
8503 a = gtst(1, 0);
8505 skip(';');
8506 if (tok != ')') {
8507 e = gjmp(0);
8508 c = ind;
8509 gexpr();
8510 vpop();
8511 gjmp_addr(d);
8512 gsym(e);
8514 skip(')');
8515 block(&a, &b, case_sym, def_sym, case_reg, 0);
8516 gjmp_addr(c);
8517 gsym(a);
8518 gsym_addr(b, c);
8519 } else
8520 if (tok == TOK_DO) {
8521 next();
8522 a = 0;
8523 b = 0;
8524 d = ind;
8525 block(&a, &b, case_sym, def_sym, case_reg, 0);
8526 skip(TOK_WHILE);
8527 skip('(');
8528 gsym(b);
8529 gexpr();
8530 c = gtst(0, 0);
8531 gsym_addr(c, d);
8532 skip(')');
8533 gsym(a);
8534 skip(';');
8535 } else
8536 if (tok == TOK_SWITCH) {
8537 next();
8538 skip('(');
8539 gexpr();
8540 /* XXX: other types than integer */
8541 case_reg = gv(RC_INT);
8542 vpop();
8543 skip(')');
8544 a = 0;
8545 b = gjmp(0); /* jump to first case */
8546 c = 0;
8547 block(&a, csym, &b, &c, case_reg, 0);
8548 /* if no default, jmp after switch */
8549 if (c == 0)
8550 c = ind;
8551 /* default label */
8552 gsym_addr(b, c);
8553 /* break label */
8554 gsym(a);
8555 } else
8556 if (tok == TOK_CASE) {
8557 int v1, v2;
8558 if (!case_sym)
8559 expect("switch");
8560 next();
8561 v1 = expr_const();
8562 v2 = v1;
8563 if (gnu_ext && tok == TOK_DOTS) {
8564 next();
8565 v2 = expr_const();
8566 if (v2 < v1)
8567 warning("empty case range");
8569 /* since a case is like a label, we must skip it with a jmp */
8570 b = gjmp(0);
8571 gsym(*case_sym);
8572 vseti(case_reg, 0);
8573 vpushi(v1);
8574 if (v1 == v2) {
8575 gen_op(TOK_EQ);
8576 *case_sym = gtst(1, 0);
8577 } else {
8578 gen_op(TOK_GE);
8579 *case_sym = gtst(1, 0);
8580 vseti(case_reg, 0);
8581 vpushi(v2);
8582 gen_op(TOK_LE);
8583 *case_sym = gtst(1, *case_sym);
8585 gsym(b);
8586 skip(':');
8587 is_expr = 0;
8588 goto block_after_label;
8589 } else
8590 if (tok == TOK_DEFAULT) {
8591 next();
8592 skip(':');
8593 if (!def_sym)
8594 expect("switch");
8595 if (*def_sym)
8596 error("too many 'default'");
8597 *def_sym = ind;
8598 is_expr = 0;
8599 goto block_after_label;
8600 } else
8601 if (tok == TOK_GOTO) {
8602 next();
8603 if (tok == '*' && gnu_ext) {
8604 /* computed goto */
8605 next();
8606 gexpr();
8607 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8608 expect("pointer");
8609 ggoto();
8610 } else if (tok >= TOK_UIDENT) {
8611 s = label_find(tok);
8612 /* put forward definition if needed */
8613 if (!s) {
8614 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8615 } else {
8616 if (s->r == LABEL_DECLARED)
8617 s->r = LABEL_FORWARD;
8619 /* label already defined */
8620 if (s->r & LABEL_FORWARD)
8621 s->next = (void *)gjmp((long)s->next);
8622 else
8623 gjmp_addr((long)s->next);
8624 next();
8625 } else {
8626 expect("label identifier");
8628 skip(';');
8629 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8630 asm_instr();
8631 } else {
8632 b = is_label();
8633 if (b) {
8634 /* label case */
8635 s = label_find(b);
8636 if (s) {
8637 if (s->r == LABEL_DEFINED)
8638 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8639 gsym((long)s->next);
8640 s->r = LABEL_DEFINED;
8641 } else {
8642 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8644 s->next = (void *)ind;
8645 /* we accept this, but it is a mistake */
8646 block_after_label:
8647 if (tok == '}') {
8648 warning("deprecated use of label at end of compound statement");
8649 } else {
8650 if (is_expr)
8651 vpop();
8652 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8654 } else {
8655 /* expression case */
8656 if (tok != ';') {
8657 if (is_expr) {
8658 vpop();
8659 gexpr();
8660 } else {
8661 gexpr();
8662 vpop();
8665 skip(';');
8670 /* t is the array or struct type. c is the array or struct
8671 address. cur_index/cur_field is the pointer to the current
8672 value. 'size_only' is true if only size info is needed (only used
8673 in arrays) */
8674 static void decl_designator(CType *type, Section *sec, unsigned long c,
8675 int *cur_index, Sym **cur_field,
8676 int size_only)
8678 Sym *s, *f;
8679 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8680 CType type1;
8682 notfirst = 0;
8683 elem_size = 0;
8684 nb_elems = 1;
8685 if (gnu_ext && (l = is_label()) != 0)
8686 goto struct_field;
8687 while (tok == '[' || tok == '.') {
8688 if (tok == '[') {
8689 if (!(type->t & VT_ARRAY))
8690 expect("array type");
8691 s = type->ref;
8692 next();
8693 index = expr_const();
8694 if (index < 0 || (s->c >= 0 && index >= s->c))
8695 expect("invalid index");
8696 if (tok == TOK_DOTS && gnu_ext) {
8697 next();
8698 index_last = expr_const();
8699 if (index_last < 0 ||
8700 (s->c >= 0 && index_last >= s->c) ||
8701 index_last < index)
8702 expect("invalid index");
8703 } else {
8704 index_last = index;
8706 skip(']');
8707 if (!notfirst)
8708 *cur_index = index_last;
8709 type = pointed_type(type);
8710 elem_size = type_size(type, &align);
8711 c += index * elem_size;
8712 /* NOTE: we only support ranges for last designator */
8713 nb_elems = index_last - index + 1;
8714 if (nb_elems != 1) {
8715 notfirst = 1;
8716 break;
8718 } else {
8719 next();
8720 l = tok;
8721 next();
8722 struct_field:
8723 if ((type->t & VT_BTYPE) != VT_STRUCT)
8724 expect("struct/union type");
8725 s = type->ref;
8726 l |= SYM_FIELD;
8727 f = s->next;
8728 while (f) {
8729 if (f->v == l)
8730 break;
8731 f = f->next;
8733 if (!f)
8734 expect("field");
8735 if (!notfirst)
8736 *cur_field = f;
8737 /* XXX: fix this mess by using explicit storage field */
8738 type1 = f->type;
8739 type1.t |= (type->t & ~VT_TYPE);
8740 type = &type1;
8741 c += f->c;
8743 notfirst = 1;
8745 if (notfirst) {
8746 if (tok == '=') {
8747 next();
8748 } else {
8749 if (!gnu_ext)
8750 expect("=");
8752 } else {
8753 if (type->t & VT_ARRAY) {
8754 index = *cur_index;
8755 type = pointed_type(type);
8756 c += index * type_size(type, &align);
8757 } else {
8758 f = *cur_field;
8759 if (!f)
8760 error("too many field init");
8761 /* XXX: fix this mess by using explicit storage field */
8762 type1 = f->type;
8763 type1.t |= (type->t & ~VT_TYPE);
8764 type = &type1;
8765 c += f->c;
8768 decl_initializer(type, sec, c, 0, size_only);
8770 /* XXX: make it more general */
8771 if (!size_only && nb_elems > 1) {
8772 unsigned long c_end;
8773 uint8_t *src, *dst;
8774 int i;
8776 if (!sec)
8777 error("range init not supported yet for dynamic storage");
8778 c_end = c + nb_elems * elem_size;
8779 if (c_end > sec->data_allocated)
8780 section_realloc(sec, c_end);
8781 src = sec->data + c;
8782 dst = src;
8783 for(i = 1; i < nb_elems; i++) {
8784 dst += elem_size;
8785 memcpy(dst, src, elem_size);
8790 #define EXPR_VAL 0
8791 #define EXPR_CONST 1
8792 #define EXPR_ANY 2
8794 /* store a value or an expression directly in global data or in local array */
8795 static void init_putv(CType *type, Section *sec, unsigned long c,
8796 int v, int expr_type)
8798 int saved_global_expr, bt, bit_pos, bit_size;
8799 void *ptr;
8800 unsigned long long bit_mask;
8801 CType dtype;
8803 switch(expr_type) {
8804 case EXPR_VAL:
8805 vpushi(v);
8806 break;
8807 case EXPR_CONST:
8808 /* compound literals must be allocated globally in this case */
8809 saved_global_expr = global_expr;
8810 global_expr = 1;
8811 expr_const1();
8812 global_expr = saved_global_expr;
8813 /* NOTE: symbols are accepted */
8814 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8815 error("initializer element is not constant");
8816 break;
8817 case EXPR_ANY:
8818 expr_eq();
8819 break;
8822 dtype = *type;
8823 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8825 if (sec) {
8826 /* XXX: not portable */
8827 /* XXX: generate error if incorrect relocation */
8828 gen_assign_cast(&dtype);
8829 bt = type->t & VT_BTYPE;
8830 /* we'll write at most 12 bytes */
8831 if (c + 12 > sec->data_allocated) {
8832 section_realloc(sec, c + 12);
8834 ptr = sec->data + c;
8835 /* XXX: make code faster ? */
8836 if (!(type->t & VT_BITFIELD)) {
8837 bit_pos = 0;
8838 bit_size = 32;
8839 bit_mask = -1LL;
8840 } else {
8841 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8842 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8843 bit_mask = (1LL << bit_size) - 1;
8845 if ((vtop->r & VT_SYM) &&
8846 (bt == VT_BYTE ||
8847 bt == VT_SHORT ||
8848 bt == VT_DOUBLE ||
8849 bt == VT_LDOUBLE ||
8850 bt == VT_LLONG ||
8851 (bt == VT_INT && bit_size != 32)))
8852 error("initializer element is not computable at load time");
8853 switch(bt) {
8854 case VT_BOOL:
8855 vtop->c.i = (vtop->c.i != 0);
8856 case VT_BYTE:
8857 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8858 break;
8859 case VT_SHORT:
8860 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8861 break;
8862 case VT_DOUBLE:
8863 *(double *)ptr = vtop->c.d;
8864 break;
8865 case VT_LDOUBLE:
8866 *(long double *)ptr = vtop->c.ld;
8867 break;
8868 case VT_LLONG:
8869 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8870 break;
8871 default:
8872 if (vtop->r & VT_SYM) {
8873 greloc(sec, vtop->sym, c, R_DATA_32);
8875 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8876 break;
8878 vtop--;
8879 } else {
8880 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8881 vswap();
8882 vstore();
8883 vpop();
8887 /* put zeros for variable based init */
8888 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8890 if (sec) {
8891 /* nothing to do because globals are already set to zero */
8892 } else {
8893 vpush_global_sym(&func_old_type, TOK_memset);
8894 vseti(VT_LOCAL, c);
8895 vpushi(0);
8896 vpushi(size);
8897 gfunc_call(3);
8901 /* 't' contains the type and storage info. 'c' is the offset of the
8902 object in section 'sec'. If 'sec' is NULL, it means stack based
8903 allocation. 'first' is true if array '{' must be read (multi
8904 dimension implicit array init handling). 'size_only' is true if
8905 size only evaluation is wanted (only for arrays). */
8906 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8907 int first, int size_only)
8909 int index, array_length, n, no_oblock, nb, parlevel, i;
8910 int size1, align1, expr_type;
8911 Sym *s, *f;
8912 CType *t1;
8914 if (type->t & VT_ARRAY) {
8915 s = type->ref;
8916 n = s->c;
8917 array_length = 0;
8918 t1 = pointed_type(type);
8919 size1 = type_size(t1, &align1);
8921 no_oblock = 1;
8922 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8923 tok == '{') {
8924 skip('{');
8925 no_oblock = 0;
8928 /* only parse strings here if correct type (otherwise: handle
8929 them as ((w)char *) expressions */
8930 if ((tok == TOK_LSTR &&
8931 #ifdef TCC_TARGET_PE
8932 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8933 #else
8934 (t1->t & VT_BTYPE) == VT_INT
8935 #endif
8936 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8937 while (tok == TOK_STR || tok == TOK_LSTR) {
8938 int cstr_len, ch;
8939 CString *cstr;
8941 cstr = tokc.cstr;
8942 /* compute maximum number of chars wanted */
8943 if (tok == TOK_STR)
8944 cstr_len = cstr->size;
8945 else
8946 cstr_len = cstr->size / sizeof(nwchar_t);
8947 cstr_len--;
8948 nb = cstr_len;
8949 if (n >= 0 && nb > (n - array_length))
8950 nb = n - array_length;
8951 if (!size_only) {
8952 if (cstr_len > nb)
8953 warning("initializer-string for array is too long");
8954 /* in order to go faster for common case (char
8955 string in global variable, we handle it
8956 specifically */
8957 if (sec && tok == TOK_STR && size1 == 1) {
8958 memcpy(sec->data + c + array_length, cstr->data, nb);
8959 } else {
8960 for(i=0;i<nb;i++) {
8961 if (tok == TOK_STR)
8962 ch = ((unsigned char *)cstr->data)[i];
8963 else
8964 ch = ((nwchar_t *)cstr->data)[i];
8965 init_putv(t1, sec, c + (array_length + i) * size1,
8966 ch, EXPR_VAL);
8970 array_length += nb;
8971 next();
8973 /* only add trailing zero if enough storage (no
8974 warning in this case since it is standard) */
8975 if (n < 0 || array_length < n) {
8976 if (!size_only) {
8977 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8979 array_length++;
8981 } else {
8982 index = 0;
8983 while (tok != '}') {
8984 decl_designator(type, sec, c, &index, NULL, size_only);
8985 if (n >= 0 && index >= n)
8986 error("index too large");
8987 /* must put zero in holes (note that doing it that way
8988 ensures that it even works with designators) */
8989 if (!size_only && array_length < index) {
8990 init_putz(t1, sec, c + array_length * size1,
8991 (index - array_length) * size1);
8993 index++;
8994 if (index > array_length)
8995 array_length = index;
8996 /* special test for multi dimensional arrays (may not
8997 be strictly correct if designators are used at the
8998 same time) */
8999 if (index >= n && no_oblock)
9000 break;
9001 if (tok == '}')
9002 break;
9003 skip(',');
9006 if (!no_oblock)
9007 skip('}');
9008 /* put zeros at the end */
9009 if (!size_only && n >= 0 && array_length < n) {
9010 init_putz(t1, sec, c + array_length * size1,
9011 (n - array_length) * size1);
9013 /* patch type size if needed */
9014 if (n < 0)
9015 s->c = array_length;
9016 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
9017 (sec || !first || tok == '{')) {
9018 int par_count;
9020 /* NOTE: the previous test is a specific case for automatic
9021 struct/union init */
9022 /* XXX: union needs only one init */
9024 /* XXX: this test is incorrect for local initializers
9025 beginning with ( without {. It would be much more difficult
9026 to do it correctly (ideally, the expression parser should
9027 be used in all cases) */
9028 par_count = 0;
9029 if (tok == '(') {
9030 AttributeDef ad1;
9031 CType type1;
9032 next();
9033 while (tok == '(') {
9034 par_count++;
9035 next();
9037 if (!parse_btype(&type1, &ad1))
9038 expect("cast");
9039 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
9040 #if 0
9041 if (!is_assignable_types(type, &type1))
9042 error("invalid type for cast");
9043 #endif
9044 skip(')');
9046 no_oblock = 1;
9047 if (first || tok == '{') {
9048 skip('{');
9049 no_oblock = 0;
9051 s = type->ref;
9052 f = s->next;
9053 array_length = 0;
9054 index = 0;
9055 n = s->c;
9056 while (tok != '}') {
9057 decl_designator(type, sec, c, NULL, &f, size_only);
9058 index = f->c;
9059 if (!size_only && array_length < index) {
9060 init_putz(type, sec, c + array_length,
9061 index - array_length);
9063 index = index + type_size(&f->type, &align1);
9064 if (index > array_length)
9065 array_length = index;
9066 f = f->next;
9067 if (no_oblock && f == NULL)
9068 break;
9069 if (tok == '}')
9070 break;
9071 skip(',');
9073 /* put zeros at the end */
9074 if (!size_only && array_length < n) {
9075 init_putz(type, sec, c + array_length,
9076 n - array_length);
9078 if (!no_oblock)
9079 skip('}');
9080 while (par_count) {
9081 skip(')');
9082 par_count--;
9084 } else if (tok == '{') {
9085 next();
9086 decl_initializer(type, sec, c, first, size_only);
9087 skip('}');
9088 } else if (size_only) {
9089 /* just skip expression */
9090 parlevel = 0;
9091 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
9092 tok != -1) {
9093 if (tok == '(')
9094 parlevel++;
9095 else if (tok == ')')
9096 parlevel--;
9097 next();
9099 } else {
9100 /* currently, we always use constant expression for globals
9101 (may change for scripting case) */
9102 expr_type = EXPR_CONST;
9103 if (!sec)
9104 expr_type = EXPR_ANY;
9105 init_putv(type, sec, c, 0, expr_type);
9109 /* parse an initializer for type 't' if 'has_init' is non zero, and
9110 allocate space in local or global data space ('r' is either
9111 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
9112 variable 'v' of scope 'scope' is declared before initializers are
9113 parsed. If 'v' is zero, then a reference to the new object is put
9114 in the value stack. If 'has_init' is 2, a special parsing is done
9115 to handle string constants. */
9116 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
9117 int has_init, int v, int scope)
9119 int size, align, addr, data_offset;
9120 int level;
9121 ParseState saved_parse_state;
9122 TokenString init_str;
9123 Section *sec;
9125 size = type_size(type, &align);
9126 /* If unknown size, we must evaluate it before
9127 evaluating initializers because
9128 initializers can generate global data too
9129 (e.g. string pointers or ISOC99 compound
9130 literals). It also simplifies local
9131 initializers handling */
9132 tok_str_new(&init_str);
9133 if (size < 0) {
9134 if (!has_init)
9135 error("unknown type size");
9136 /* get all init string */
9137 if (has_init == 2) {
9138 /* only get strings */
9139 while (tok == TOK_STR || tok == TOK_LSTR) {
9140 tok_str_add_tok(&init_str);
9141 next();
9143 } else {
9144 level = 0;
9145 while (level > 0 || (tok != ',' && tok != ';')) {
9146 if (tok < 0)
9147 error("unexpected end of file in initializer");
9148 tok_str_add_tok(&init_str);
9149 if (tok == '{')
9150 level++;
9151 else if (tok == '}') {
9152 if (level == 0)
9153 break;
9154 level--;
9156 next();
9159 tok_str_add(&init_str, -1);
9160 tok_str_add(&init_str, 0);
9162 /* compute size */
9163 save_parse_state(&saved_parse_state);
9165 macro_ptr = init_str.str;
9166 next();
9167 decl_initializer(type, NULL, 0, 1, 1);
9168 /* prepare second initializer parsing */
9169 macro_ptr = init_str.str;
9170 next();
9172 /* if still unknown size, error */
9173 size = type_size(type, &align);
9174 if (size < 0)
9175 error("unknown type size");
9177 /* take into account specified alignment if bigger */
9178 if (ad->aligned) {
9179 if (ad->aligned > align)
9180 align = ad->aligned;
9181 } else if (ad->packed) {
9182 align = 1;
9184 if ((r & VT_VALMASK) == VT_LOCAL) {
9185 sec = NULL;
9186 if (do_bounds_check && (type->t & VT_ARRAY))
9187 loc--;
9188 loc = (loc - size) & -align;
9189 addr = loc;
9190 /* handles bounds */
9191 /* XXX: currently, since we do only one pass, we cannot track
9192 '&' operators, so we add only arrays */
9193 if (do_bounds_check && (type->t & VT_ARRAY)) {
9194 unsigned long *bounds_ptr;
9195 /* add padding between regions */
9196 loc--;
9197 /* then add local bound info */
9198 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9199 bounds_ptr[0] = addr;
9200 bounds_ptr[1] = size;
9202 if (v) {
9203 /* local variable */
9204 sym_push(v, type, r, addr);
9205 } else {
9206 /* push local reference */
9207 vset(type, r, addr);
9209 } else {
9210 Sym *sym;
9212 sym = NULL;
9213 if (v && scope == VT_CONST) {
9214 /* see if the symbol was already defined */
9215 sym = sym_find(v);
9216 if (sym) {
9217 if (!is_compatible_types(&sym->type, type))
9218 error("incompatible types for redefinition of '%s'",
9219 get_tok_str(v, NULL));
9220 if (sym->type.t & VT_EXTERN) {
9221 /* if the variable is extern, it was not allocated */
9222 sym->type.t &= ~VT_EXTERN;
9223 /* set array size if it was ommited in extern
9224 declaration */
9225 if ((sym->type.t & VT_ARRAY) &&
9226 sym->type.ref->c < 0 &&
9227 type->ref->c >= 0)
9228 sym->type.ref->c = type->ref->c;
9229 } else {
9230 /* we accept several definitions of the same
9231 global variable. this is tricky, because we
9232 must play with the SHN_COMMON type of the symbol */
9233 /* XXX: should check if the variable was already
9234 initialized. It is incorrect to initialized it
9235 twice */
9236 /* no init data, we won't add more to the symbol */
9237 if (!has_init)
9238 goto no_alloc;
9243 /* allocate symbol in corresponding section */
9244 sec = ad->section;
9245 if (!sec) {
9246 if (has_init)
9247 sec = data_section;
9248 else if (tcc_state->nocommon)
9249 sec = bss_section;
9251 if (sec) {
9252 data_offset = sec->data_offset;
9253 data_offset = (data_offset + align - 1) & -align;
9254 addr = data_offset;
9255 /* very important to increment global pointer at this time
9256 because initializers themselves can create new initializers */
9257 data_offset += size;
9258 /* add padding if bound check */
9259 if (do_bounds_check)
9260 data_offset++;
9261 sec->data_offset = data_offset;
9262 /* allocate section space to put the data */
9263 if (sec->sh_type != SHT_NOBITS &&
9264 data_offset > sec->data_allocated)
9265 section_realloc(sec, data_offset);
9266 /* align section if needed */
9267 if (align > sec->sh_addralign)
9268 sec->sh_addralign = align;
9269 } else {
9270 addr = 0; /* avoid warning */
9273 if (v) {
9274 if (scope != VT_CONST || !sym) {
9275 sym = sym_push(v, type, r | VT_SYM, 0);
9277 /* update symbol definition */
9278 if (sec) {
9279 put_extern_sym(sym, sec, addr, size);
9280 } else {
9281 ElfW(Sym) *esym;
9282 /* put a common area */
9283 put_extern_sym(sym, NULL, align, size);
9284 /* XXX: find a nicer way */
9285 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
9286 esym->st_shndx = SHN_COMMON;
9288 } else {
9289 CValue cval;
9291 /* push global reference */
9292 sym = get_sym_ref(type, sec, addr, size);
9293 cval.ul = 0;
9294 vsetc(type, VT_CONST | VT_SYM, &cval);
9295 vtop->sym = sym;
9298 /* handles bounds now because the symbol must be defined
9299 before for the relocation */
9300 if (do_bounds_check) {
9301 unsigned long *bounds_ptr;
9303 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9304 /* then add global bound info */
9305 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9306 bounds_ptr[0] = 0; /* relocated */
9307 bounds_ptr[1] = size;
9310 if (has_init) {
9311 decl_initializer(type, sec, addr, 1, 0);
9312 /* restore parse state if needed */
9313 if (init_str.str) {
9314 tok_str_free(init_str.str);
9315 restore_parse_state(&saved_parse_state);
9318 no_alloc: ;
9321 void put_func_debug(Sym *sym)
9323 char buf[512];
9325 /* stabs info */
9326 /* XXX: we put here a dummy type */
9327 snprintf(buf, sizeof(buf), "%s:%c1",
9328 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9329 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9330 cur_text_section, sym->c);
9331 /* //gr gdb wants a line at the function */
9332 put_stabn(N_SLINE, 0, file->line_num, 0);
9333 last_ind = 0;
9334 last_line_num = 0;
9337 /* parse an old style function declaration list */
9338 /* XXX: check multiple parameter */
9339 static void func_decl_list(Sym *func_sym)
9341 AttributeDef ad;
9342 int v;
9343 Sym *s;
9344 CType btype, type;
9346 /* parse each declaration */
9347 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9348 if (!parse_btype(&btype, &ad))
9349 expect("declaration list");
9350 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9351 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9352 tok == ';') {
9353 /* we accept no variable after */
9354 } else {
9355 for(;;) {
9356 type = btype;
9357 type_decl(&type, &ad, &v, TYPE_DIRECT);
9358 /* find parameter in function parameter list */
9359 s = func_sym->next;
9360 while (s != NULL) {
9361 if ((s->v & ~SYM_FIELD) == v)
9362 goto found;
9363 s = s->next;
9365 error("declaration for parameter '%s' but no such parameter",
9366 get_tok_str(v, NULL));
9367 found:
9368 /* check that no storage specifier except 'register' was given */
9369 if (type.t & VT_STORAGE)
9370 error("storage class specified for '%s'", get_tok_str(v, NULL));
9371 convert_parameter_type(&type);
9372 /* we can add the type (NOTE: it could be local to the function) */
9373 s->type = type;
9374 /* accept other parameters */
9375 if (tok == ',')
9376 next();
9377 else
9378 break;
9381 skip(';');
9385 /* parse a function defined by symbol 'sym' and generate its code in
9386 'cur_text_section' */
9387 static void gen_function(Sym *sym)
9389 int saved_nocode_wanted = nocode_wanted;
9390 nocode_wanted = 0;
9391 ind = cur_text_section->data_offset;
9392 /* NOTE: we patch the symbol size later */
9393 put_extern_sym(sym, cur_text_section, ind, 0);
9394 funcname = get_tok_str(sym->v, NULL);
9395 func_ind = ind;
9396 /* put debug symbol */
9397 if (do_debug)
9398 put_func_debug(sym);
9399 /* push a dummy symbol to enable local sym storage */
9400 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9401 gfunc_prolog(&sym->type);
9402 rsym = 0;
9403 block(NULL, NULL, NULL, NULL, 0, 0);
9404 gsym(rsym);
9405 gfunc_epilog();
9406 cur_text_section->data_offset = ind;
9407 label_pop(&global_label_stack, NULL);
9408 sym_pop(&local_stack, NULL); /* reset local stack */
9409 /* end of function */
9410 /* patch symbol size */
9411 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
9412 ind - func_ind;
9413 if (do_debug) {
9414 put_stabn(N_FUN, 0, 0, ind - func_ind);
9416 /* It's better to crash than to generate wrong code */
9417 cur_text_section = NULL;
9418 funcname = ""; /* for safety */
9419 func_vt.t = VT_VOID; /* for safety */
9420 ind = 0; /* for safety */
9421 nocode_wanted = saved_nocode_wanted;
9424 static void gen_inline_functions(void)
9426 Sym *sym;
9427 CType *type;
9428 int *str, inline_generated;
9430 /* iterate while inline function are referenced */
9431 for(;;) {
9432 inline_generated = 0;
9433 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9434 type = &sym->type;
9435 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9436 (type->t & (VT_STATIC | VT_INLINE)) ==
9437 (VT_STATIC | VT_INLINE) &&
9438 sym->c != 0) {
9439 /* the function was used: generate its code and
9440 convert it to a normal function */
9441 str = INLINE_DEF(sym->r);
9442 sym->r = VT_SYM | VT_CONST;
9443 sym->type.t &= ~VT_INLINE;
9445 macro_ptr = str;
9446 next();
9447 cur_text_section = text_section;
9448 gen_function(sym);
9449 macro_ptr = NULL; /* fail safe */
9451 tok_str_free(str);
9452 inline_generated = 1;
9455 if (!inline_generated)
9456 break;
9459 /* free all remaining inline function tokens */
9460 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9461 type = &sym->type;
9462 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9463 (type->t & (VT_STATIC | VT_INLINE)) ==
9464 (VT_STATIC | VT_INLINE)) {
9465 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9466 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9467 continue;
9468 str = INLINE_DEF(sym->r);
9469 tok_str_free(str);
9470 sym->r = 0; /* fail safe */
9475 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9476 static void decl(int l)
9478 int v, has_init, r;
9479 CType type, btype;
9480 Sym *sym;
9481 AttributeDef ad;
9483 while (1) {
9484 if (!parse_btype(&btype, &ad)) {
9485 /* skip redundant ';' */
9486 /* XXX: find more elegant solution */
9487 if (tok == ';') {
9488 next();
9489 continue;
9491 if (l == VT_CONST &&
9492 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9493 /* global asm block */
9494 asm_global_instr();
9495 continue;
9497 /* special test for old K&R protos without explicit int
9498 type. Only accepted when defining global data */
9499 if (l == VT_LOCAL || tok < TOK_DEFINE)
9500 break;
9501 btype.t = VT_INT;
9503 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9504 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9505 tok == ';') {
9506 /* we accept no variable after */
9507 next();
9508 continue;
9510 while (1) { /* iterate thru each declaration */
9511 type = btype;
9512 type_decl(&type, &ad, &v, TYPE_DIRECT);
9513 #if 0
9515 char buf[500];
9516 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9517 printf("type = '%s'\n", buf);
9519 #endif
9520 if ((type.t & VT_BTYPE) == VT_FUNC) {
9521 /* if old style function prototype, we accept a
9522 declaration list */
9523 sym = type.ref;
9524 if (sym->c == FUNC_OLD)
9525 func_decl_list(sym);
9528 if (tok == '{') {
9529 if (l == VT_LOCAL)
9530 error("cannot use local functions");
9531 if ((type.t & VT_BTYPE) != VT_FUNC)
9532 expect("function definition");
9534 /* reject abstract declarators in function definition */
9535 sym = type.ref;
9536 while ((sym = sym->next) != NULL)
9537 if (!(sym->v & ~SYM_FIELD))
9538 expect("identifier");
9540 /* XXX: cannot do better now: convert extern line to static inline */
9541 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9542 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9544 sym = sym_find(v);
9545 if (sym) {
9546 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9547 goto func_error1;
9548 /* specific case: if not func_call defined, we put
9549 the one of the prototype */
9550 /* XXX: should have default value */
9551 r = sym->type.ref->r;
9552 if (FUNC_CALL(r) != FUNC_CDECL
9553 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9554 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9555 if (FUNC_EXPORT(r))
9556 FUNC_EXPORT(type.ref->r) = 1;
9558 if (!is_compatible_types(&sym->type, &type)) {
9559 func_error1:
9560 error("incompatible types for redefinition of '%s'",
9561 get_tok_str(v, NULL));
9563 /* if symbol is already defined, then put complete type */
9564 sym->type = type;
9565 } else {
9566 /* put function symbol */
9567 sym = global_identifier_push(v, type.t, 0);
9568 sym->type.ref = type.ref;
9571 /* static inline functions are just recorded as a kind
9572 of macro. Their code will be emitted at the end of
9573 the compilation unit only if they are used */
9574 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9575 (VT_INLINE | VT_STATIC)) {
9576 TokenString func_str;
9577 int block_level;
9579 tok_str_new(&func_str);
9581 block_level = 0;
9582 for(;;) {
9583 int t;
9584 if (tok == TOK_EOF)
9585 error("unexpected end of file");
9586 tok_str_add_tok(&func_str);
9587 t = tok;
9588 next();
9589 if (t == '{') {
9590 block_level++;
9591 } else if (t == '}') {
9592 block_level--;
9593 if (block_level == 0)
9594 break;
9597 tok_str_add(&func_str, -1);
9598 tok_str_add(&func_str, 0);
9599 INLINE_DEF(sym->r) = func_str.str;
9600 } else {
9601 /* compute text section */
9602 cur_text_section = ad.section;
9603 if (!cur_text_section)
9604 cur_text_section = text_section;
9605 sym->r = VT_SYM | VT_CONST;
9606 gen_function(sym);
9608 break;
9609 } else {
9610 if (btype.t & VT_TYPEDEF) {
9611 /* save typedefed type */
9612 /* XXX: test storage specifiers ? */
9613 sym = sym_push(v, &type, 0, 0);
9614 sym->type.t |= VT_TYPEDEF;
9615 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9616 /* external function definition */
9617 /* specific case for func_call attribute */
9618 if (ad.func_attr)
9619 type.ref->r = ad.func_attr;
9620 external_sym(v, &type, 0);
9621 } else {
9622 /* not lvalue if array */
9623 r = 0;
9624 if (!(type.t & VT_ARRAY))
9625 r |= lvalue_type(type.t);
9626 has_init = (tok == '=');
9627 if ((btype.t & VT_EXTERN) ||
9628 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9629 !has_init && l == VT_CONST && type.ref->c < 0)) {
9630 /* external variable */
9631 /* NOTE: as GCC, uninitialized global static
9632 arrays of null size are considered as
9633 extern */
9634 external_sym(v, &type, r);
9635 } else {
9636 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9637 if (type.t & VT_STATIC)
9638 r |= VT_CONST;
9639 else
9640 r |= l;
9641 if (has_init)
9642 next();
9643 decl_initializer_alloc(&type, &ad, r,
9644 has_init, v, l);
9647 if (tok != ',') {
9648 skip(';');
9649 break;
9651 next();
9657 /* better than nothing, but needs extension to handle '-E' option
9658 correctly too */
9659 static void preprocess_init(TCCState *s1)
9661 s1->include_stack_ptr = s1->include_stack;
9662 /* XXX: move that before to avoid having to initialize
9663 file->ifdef_stack_ptr ? */
9664 s1->ifdef_stack_ptr = s1->ifdef_stack;
9665 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9667 /* XXX: not ANSI compliant: bound checking says error */
9668 vtop = vstack - 1;
9669 s1->pack_stack[0] = 0;
9670 s1->pack_stack_ptr = s1->pack_stack;
9673 /* compile the C file opened in 'file'. Return non zero if errors. */
9674 static int tcc_compile(TCCState *s1)
9676 Sym *define_start;
9677 char buf[512];
9678 volatile int section_sym;
9680 #ifdef INC_DEBUG
9681 printf("%s: **** new file\n", file->filename);
9682 #endif
9683 preprocess_init(s1);
9685 cur_text_section = NULL;
9686 funcname = "";
9687 anon_sym = SYM_FIRST_ANOM;
9689 /* file info: full path + filename */
9690 section_sym = 0; /* avoid warning */
9691 if (do_debug) {
9692 section_sym = put_elf_sym(symtab_section, 0, 0,
9693 ELFW(ST_INFO)(STB_LOCAL, STT_SECTION), 0,
9694 text_section->sh_num, NULL);
9695 getcwd(buf, sizeof(buf));
9696 #ifdef _WIN32
9697 normalize_slashes(buf);
9698 #endif
9699 pstrcat(buf, sizeof(buf), "/");
9700 put_stabs_r(buf, N_SO, 0, 0,
9701 text_section->data_offset, text_section, section_sym);
9702 put_stabs_r(file->filename, N_SO, 0, 0,
9703 text_section->data_offset, text_section, section_sym);
9705 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9706 symbols can be safely used */
9707 put_elf_sym(symtab_section, 0, 0,
9708 ELFW(ST_INFO)(STB_LOCAL, STT_FILE), 0,
9709 SHN_ABS, file->filename);
9711 /* define some often used types */
9712 int_type.t = VT_INT;
9714 char_pointer_type.t = VT_BYTE;
9715 mk_pointer(&char_pointer_type);
9717 func_old_type.t = VT_FUNC;
9718 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9720 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9721 float_type.t = VT_FLOAT;
9722 double_type.t = VT_DOUBLE;
9724 func_float_type.t = VT_FUNC;
9725 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9726 func_double_type.t = VT_FUNC;
9727 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9728 #endif
9730 #if 0
9731 /* define 'void *alloca(unsigned int)' builtin function */
9733 Sym *s1;
9735 p = anon_sym++;
9736 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9737 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9738 s1->next = NULL;
9739 sym->next = s1;
9740 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9742 #endif
9744 define_start = define_stack;
9745 nocode_wanted = 1;
9747 if (setjmp(s1->error_jmp_buf) == 0) {
9748 s1->nb_errors = 0;
9749 s1->error_set_jmp_enabled = 1;
9751 ch = file->buf_ptr[0];
9752 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9753 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9754 next();
9755 decl(VT_CONST);
9756 if (tok != TOK_EOF)
9757 expect("declaration");
9759 /* end of translation unit info */
9760 if (do_debug) {
9761 put_stabs_r(NULL, N_SO, 0, 0,
9762 text_section->data_offset, text_section, section_sym);
9765 s1->error_set_jmp_enabled = 0;
9767 /* reset define stack, but leave -Dsymbols (may be incorrect if
9768 they are undefined) */
9769 free_defines(define_start);
9771 gen_inline_functions();
9773 sym_pop(&global_stack, NULL);
9774 sym_pop(&local_stack, NULL);
9776 return s1->nb_errors != 0 ? -1 : 0;
9779 /* Preprocess the current file */
9780 /* XXX: add line and file infos, add options to preserve spaces */
9781 static int tcc_preprocess(TCCState *s1)
9783 Sym *define_start;
9784 BufferedFile *file_ref;
9785 int token_seen, line_ref;
9787 preprocess_init(s1);
9788 define_start = define_stack;
9789 ch = file->buf_ptr[0];
9791 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9792 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9793 PARSE_FLAG_LINEFEED;
9795 token_seen = 0;
9796 line_ref = 0;
9797 file_ref = NULL;
9799 for (;;) {
9800 next();
9801 if (tok == TOK_EOF) {
9802 break;
9803 } else if (tok == TOK_LINEFEED) {
9804 if (!token_seen)
9805 continue;
9806 ++line_ref;
9807 token_seen = 0;
9808 } else if (token_seen) {
9809 fputc(' ', s1->outfile);
9810 } else {
9811 int d = file->line_num - line_ref;
9812 if (file != file_ref || d < 0 || d >= 8)
9813 fprintf(s1->outfile, "# %d \"%s\"\n", file->line_num, file->filename);
9814 else
9815 while (d)
9816 fputs("\n", s1->outfile), --d;
9817 line_ref = (file_ref = file)->line_num;
9818 token_seen = 1;
9820 fputs(get_tok_str(tok, &tokc), s1->outfile);
9822 free_defines(define_start);
9823 return 0;
9826 #ifdef LIBTCC
9827 int tcc_compile_string(TCCState *s, const char *str)
9829 BufferedFile bf1, *bf = &bf1;
9830 int ret, len;
9831 char *buf;
9833 /* init file structure */
9834 bf->fd = -1;
9835 /* XXX: avoid copying */
9836 len = strlen(str);
9837 buf = tcc_malloc(len + 1);
9838 if (!buf)
9839 return -1;
9840 memcpy(buf, str, len);
9841 buf[len] = CH_EOB;
9842 bf->buf_ptr = buf;
9843 bf->buf_end = buf + len;
9844 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9845 bf->line_num = 1;
9846 file = bf;
9847 ret = tcc_compile(s);
9848 file = NULL;
9849 tcc_free(buf);
9851 /* currently, no need to close */
9852 return ret;
9854 #endif
9856 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9857 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9859 BufferedFile bf1, *bf = &bf1;
9861 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9862 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9863 /* default value */
9864 if (!value)
9865 value = "1";
9866 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9868 /* init file structure */
9869 bf->fd = -1;
9870 bf->buf_ptr = bf->buffer;
9871 bf->buf_end = bf->buffer + strlen(bf->buffer);
9872 *bf->buf_end = CH_EOB;
9873 bf->filename[0] = '\0';
9874 bf->line_num = 1;
9875 file = bf;
9877 s1->include_stack_ptr = s1->include_stack;
9879 /* parse with define parser */
9880 ch = file->buf_ptr[0];
9881 next_nomacro();
9882 parse_define();
9883 file = NULL;
9886 /* undefine a preprocessor symbol */
9887 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9889 TokenSym *ts;
9890 Sym *s;
9891 ts = tok_alloc(sym, strlen(sym));
9892 s = define_find(ts->tok);
9893 /* undefine symbol by putting an invalid name */
9894 if (s)
9895 define_undef(s);
9898 #ifdef CONFIG_TCC_ASM
9900 #ifdef TCC_TARGET_I386
9901 #include "i386-asm.c"
9902 #endif
9903 #include "tccasm.c"
9905 #else
9906 static void asm_instr(void)
9908 error("inline asm() not supported");
9910 static void asm_global_instr(void)
9912 error("inline asm() not supported");
9914 #endif
9916 #include "tccelf.c"
9918 #ifdef TCC_TARGET_COFF
9919 #include "tcccoff.c"
9920 #endif
9922 #ifdef TCC_TARGET_PE
9923 #include "tccpe.c"
9924 #endif
9926 /* print the position in the source file of PC value 'pc' by reading
9927 the stabs debug information */
9928 static void rt_printline(unsigned long wanted_pc)
9930 Stab_Sym *sym, *sym_end;
9931 char func_name[128], last_func_name[128];
9932 unsigned long func_addr, last_pc, pc;
9933 const char *incl_files[INCLUDE_STACK_SIZE];
9934 int incl_index, len, last_line_num, i;
9935 const char *str, *p;
9937 fprintf(stderr, "0x%08lx:", wanted_pc);
9939 func_name[0] = '\0';
9940 func_addr = 0;
9941 incl_index = 0;
9942 last_func_name[0] = '\0';
9943 last_pc = 0xffffffff;
9944 last_line_num = 1;
9945 sym = (Stab_Sym *)stab_section->data + 1;
9946 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9947 while (sym < sym_end) {
9948 switch(sym->n_type) {
9949 /* function start or end */
9950 case N_FUN:
9951 if (sym->n_strx == 0) {
9952 /* we test if between last line and end of function */
9953 pc = sym->n_value + func_addr;
9954 if (wanted_pc >= last_pc && wanted_pc < pc)
9955 goto found;
9956 func_name[0] = '\0';
9957 func_addr = 0;
9958 } else {
9959 str = stabstr_section->data + sym->n_strx;
9960 p = strchr(str, ':');
9961 if (!p) {
9962 pstrcpy(func_name, sizeof(func_name), str);
9963 } else {
9964 len = p - str;
9965 if (len > sizeof(func_name) - 1)
9966 len = sizeof(func_name) - 1;
9967 memcpy(func_name, str, len);
9968 func_name[len] = '\0';
9970 func_addr = sym->n_value;
9972 break;
9973 /* line number info */
9974 case N_SLINE:
9975 pc = sym->n_value + func_addr;
9976 if (wanted_pc >= last_pc && wanted_pc < pc)
9977 goto found;
9978 last_pc = pc;
9979 last_line_num = sym->n_desc;
9980 /* XXX: slow! */
9981 strcpy(last_func_name, func_name);
9982 break;
9983 /* include files */
9984 case N_BINCL:
9985 str = stabstr_section->data + sym->n_strx;
9986 add_incl:
9987 if (incl_index < INCLUDE_STACK_SIZE) {
9988 incl_files[incl_index++] = str;
9990 break;
9991 case N_EINCL:
9992 if (incl_index > 1)
9993 incl_index--;
9994 break;
9995 case N_SO:
9996 if (sym->n_strx == 0) {
9997 incl_index = 0; /* end of translation unit */
9998 } else {
9999 str = stabstr_section->data + sym->n_strx;
10000 /* do not add path */
10001 len = strlen(str);
10002 if (len > 0 && str[len - 1] != '/')
10003 goto add_incl;
10005 break;
10007 sym++;
10010 /* second pass: we try symtab symbols (no line number info) */
10011 incl_index = 0;
10013 ElfW(Sym) *sym, *sym_end;
10014 int type;
10016 sym_end = (ElfW(Sym) *)(symtab_section->data + symtab_section->data_offset);
10017 for(sym = (ElfW(Sym) *)symtab_section->data + 1;
10018 sym < sym_end;
10019 sym++) {
10020 type = ELFW(ST_TYPE)(sym->st_info);
10021 if (type == STT_FUNC) {
10022 if (wanted_pc >= sym->st_value &&
10023 wanted_pc < sym->st_value + sym->st_size) {
10024 pstrcpy(last_func_name, sizeof(last_func_name),
10025 strtab_section->data + sym->st_name);
10026 goto found;
10031 /* did not find any info: */
10032 fprintf(stderr, " ???\n");
10033 return;
10034 found:
10035 if (last_func_name[0] != '\0') {
10036 fprintf(stderr, " %s()", last_func_name);
10038 if (incl_index > 0) {
10039 fprintf(stderr, " (%s:%d",
10040 incl_files[incl_index - 1], last_line_num);
10041 for(i = incl_index - 2; i >= 0; i--)
10042 fprintf(stderr, ", included from %s", incl_files[i]);
10043 fprintf(stderr, ")");
10045 fprintf(stderr, "\n");
10048 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
10050 #ifdef __i386__
10052 /* fix for glibc 2.1 */
10053 #ifndef REG_EIP
10054 #define REG_EIP EIP
10055 #define REG_EBP EBP
10056 #endif
10058 /* return the PC at frame level 'level'. Return non zero if not found */
10059 static int rt_get_caller_pc(unsigned long *paddr,
10060 ucontext_t *uc, int level)
10062 unsigned long fp;
10063 int i;
10065 if (level == 0) {
10066 #if defined(__FreeBSD__)
10067 *paddr = uc->uc_mcontext.mc_eip;
10068 #elif defined(__dietlibc__)
10069 *paddr = uc->uc_mcontext.eip;
10070 #else
10071 *paddr = uc->uc_mcontext.gregs[REG_EIP];
10072 #endif
10073 return 0;
10074 } else {
10075 #if defined(__FreeBSD__)
10076 fp = uc->uc_mcontext.mc_ebp;
10077 #elif defined(__dietlibc__)
10078 fp = uc->uc_mcontext.ebp;
10079 #else
10080 fp = uc->uc_mcontext.gregs[REG_EBP];
10081 #endif
10082 for(i=1;i<level;i++) {
10083 /* XXX: check address validity with program info */
10084 if (fp <= 0x1000 || fp >= 0xc0000000)
10085 return -1;
10086 fp = ((unsigned long *)fp)[0];
10088 *paddr = ((unsigned long *)fp)[1];
10089 return 0;
10092 #elif defined(__x86_64__)
10093 /* return the PC at frame level 'level'. Return non zero if not found */
10094 static int rt_get_caller_pc(unsigned long *paddr,
10095 ucontext_t *uc, int level)
10097 unsigned long fp;
10098 int i;
10100 if (level == 0) {
10101 /* XXX: only support linux */
10102 *paddr = uc->uc_mcontext.gregs[REG_RIP];
10103 return 0;
10104 } else {
10105 fp = uc->uc_mcontext.gregs[REG_RBP];
10106 for(i=1;i<level;i++) {
10107 /* XXX: check address validity with program info */
10108 if (fp <= 0x1000 || fp >= 0xc0000000)
10109 return -1;
10110 fp = ((unsigned long *)fp)[0];
10112 *paddr = ((unsigned long *)fp)[1];
10113 return 0;
10116 #else
10118 #warning add arch specific rt_get_caller_pc()
10120 static int rt_get_caller_pc(unsigned long *paddr,
10121 ucontext_t *uc, int level)
10123 return -1;
10125 #endif
10127 /* emit a run time error at position 'pc' */
10128 void rt_error(ucontext_t *uc, const char *fmt, ...)
10130 va_list ap;
10131 unsigned long pc;
10132 int i;
10134 va_start(ap, fmt);
10135 fprintf(stderr, "Runtime error: ");
10136 vfprintf(stderr, fmt, ap);
10137 fprintf(stderr, "\n");
10138 for(i=0;i<num_callers;i++) {
10139 if (rt_get_caller_pc(&pc, uc, i) < 0)
10140 break;
10141 if (i == 0)
10142 fprintf(stderr, "at ");
10143 else
10144 fprintf(stderr, "by ");
10145 rt_printline(pc);
10147 exit(255);
10148 va_end(ap);
10151 /* signal handler for fatal errors */
10152 static void sig_error(int signum, siginfo_t *siginf, void *puc)
10154 ucontext_t *uc = puc;
10156 switch(signum) {
10157 case SIGFPE:
10158 switch(siginf->si_code) {
10159 case FPE_INTDIV:
10160 case FPE_FLTDIV:
10161 rt_error(uc, "division by zero");
10162 break;
10163 default:
10164 rt_error(uc, "floating point exception");
10165 break;
10167 break;
10168 case SIGBUS:
10169 case SIGSEGV:
10170 if (rt_bound_error_msg && *rt_bound_error_msg)
10171 rt_error(uc, *rt_bound_error_msg);
10172 else
10173 rt_error(uc, "dereferencing invalid pointer");
10174 break;
10175 case SIGILL:
10176 rt_error(uc, "illegal instruction");
10177 break;
10178 case SIGABRT:
10179 rt_error(uc, "abort() called");
10180 break;
10181 default:
10182 rt_error(uc, "caught signal %d", signum);
10183 break;
10185 exit(255);
10187 #endif
10189 /* do all relocations (needed before using tcc_get_symbol()) */
10190 int tcc_relocate(TCCState *s1)
10192 Section *s;
10193 int i;
10195 s1->nb_errors = 0;
10197 #ifdef TCC_TARGET_PE
10198 pe_add_runtime(s1);
10199 #else
10200 tcc_add_runtime(s1);
10201 #endif
10203 relocate_common_syms();
10205 tcc_add_linker_symbols(s1);
10206 #ifndef TCC_TARGET_PE
10207 build_got_entries(s1);
10208 #endif
10209 /* compute relocation address : section are relocated in place. We
10210 also alloc the bss space */
10211 for(i = 1; i < s1->nb_sections; i++) {
10212 s = s1->sections[i];
10213 if (s->sh_flags & SHF_ALLOC) {
10214 if (s->sh_type == SHT_NOBITS)
10215 s->data = tcc_mallocz(s->data_offset);
10216 s->sh_addr = (unsigned long)s->data;
10220 relocate_syms(s1, 1);
10222 if (s1->nb_errors != 0)
10223 return -1;
10225 /* relocate each section */
10226 for(i = 1; i < s1->nb_sections; i++) {
10227 s = s1->sections[i];
10228 if (s->reloc)
10229 relocate_section(s1, s);
10232 /* mark executable sections as executable in memory */
10233 for(i = 1; i < s1->nb_sections; i++) {
10234 s = s1->sections[i];
10235 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10236 (SHF_ALLOC | SHF_EXECINSTR))
10237 set_pages_executable(s->data, s->data_offset);
10239 return 0;
10242 /* launch the compiled program with the given arguments */
10243 int tcc_run(TCCState *s1, int argc, char **argv)
10245 int (*prog_main)(int, char **);
10247 if (tcc_relocate(s1) < 0)
10248 return -1;
10250 prog_main = tcc_get_symbol_err(s1, "main");
10252 if (do_debug) {
10253 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10254 error("debug mode currently not available for Windows");
10255 #else
10256 struct sigaction sigact;
10257 /* install TCC signal handlers to print debug info on fatal
10258 runtime errors */
10259 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10260 sigact.sa_sigaction = sig_error;
10261 sigemptyset(&sigact.sa_mask);
10262 sigaction(SIGFPE, &sigact, NULL);
10263 sigaction(SIGILL, &sigact, NULL);
10264 sigaction(SIGSEGV, &sigact, NULL);
10265 sigaction(SIGBUS, &sigact, NULL);
10266 sigaction(SIGABRT, &sigact, NULL);
10267 #endif
10270 #ifdef CONFIG_TCC_BCHECK
10271 if (do_bounds_check) {
10272 void (*bound_init)(void);
10274 /* set error function */
10275 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10276 "__bound_error_msg");
10278 /* XXX: use .init section so that it also work in binary ? */
10279 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10280 bound_init();
10282 #endif
10283 return (*prog_main)(argc, argv);
10286 void tcc_memstats(void)
10288 #ifdef MEM_DEBUG
10289 printf("memory in use: %d\n", mem_cur_size);
10290 #endif
10293 static void tcc_cleanup(void)
10295 int i, n;
10297 if (NULL == tcc_state)
10298 return;
10299 tcc_state = NULL;
10301 /* free -D defines */
10302 free_defines(NULL);
10304 /* free tokens */
10305 n = tok_ident - TOK_IDENT;
10306 for(i = 0; i < n; i++)
10307 tcc_free(table_ident[i]);
10308 tcc_free(table_ident);
10310 /* free sym_pools */
10311 dynarray_reset(&sym_pools, &nb_sym_pools);
10312 /* string buffer */
10313 cstr_free(&tokcstr);
10314 /* reset symbol stack */
10315 sym_free_first = NULL;
10316 /* cleanup from error/setjmp */
10317 macro_ptr = NULL;
10320 TCCState *tcc_new(void)
10322 const char *p, *r;
10323 TCCState *s;
10324 TokenSym *ts;
10325 int i, c;
10327 tcc_cleanup();
10329 s = tcc_mallocz(sizeof(TCCState));
10330 if (!s)
10331 return NULL;
10332 tcc_state = s;
10333 s->output_type = TCC_OUTPUT_MEMORY;
10335 /* init isid table */
10336 for(i=CH_EOF;i<256;i++)
10337 isidnum_table[i-CH_EOF] = isid(i) || isnum(i);
10339 /* add all tokens */
10340 table_ident = NULL;
10341 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10343 tok_ident = TOK_IDENT;
10344 p = tcc_keywords;
10345 while (*p) {
10346 r = p;
10347 for(;;) {
10348 c = *r++;
10349 if (c == '\0')
10350 break;
10352 ts = tok_alloc(p, r - p - 1);
10353 p = r;
10356 /* we add dummy defines for some special macros to speed up tests
10357 and to have working defined() */
10358 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10359 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10360 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10361 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10363 /* standard defines */
10364 tcc_define_symbol(s, "__STDC__", NULL);
10365 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10366 #if defined(TCC_TARGET_I386)
10367 tcc_define_symbol(s, "__i386__", NULL);
10368 #endif
10369 #if defined(TCC_TARGET_X86_64)
10370 tcc_define_symbol(s, "__x86_64__", NULL);
10371 #endif
10372 #if defined(TCC_TARGET_ARM)
10373 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10374 tcc_define_symbol(s, "__arm_elf__", NULL);
10375 tcc_define_symbol(s, "__arm_elf", NULL);
10376 tcc_define_symbol(s, "arm_elf", NULL);
10377 tcc_define_symbol(s, "__arm__", NULL);
10378 tcc_define_symbol(s, "__arm", NULL);
10379 tcc_define_symbol(s, "arm", NULL);
10380 tcc_define_symbol(s, "__APCS_32__", NULL);
10381 #endif
10382 #ifdef TCC_TARGET_PE
10383 tcc_define_symbol(s, "_WIN32", NULL);
10384 #else
10385 tcc_define_symbol(s, "__unix__", NULL);
10386 tcc_define_symbol(s, "__unix", NULL);
10387 #if defined(__linux)
10388 tcc_define_symbol(s, "__linux__", NULL);
10389 tcc_define_symbol(s, "__linux", NULL);
10390 #endif
10391 #endif
10392 /* tiny C specific defines */
10393 tcc_define_symbol(s, "__TINYC__", NULL);
10395 /* tiny C & gcc defines */
10396 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10397 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10398 #ifdef TCC_TARGET_PE
10399 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10400 #else
10401 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10402 #endif
10404 #ifndef TCC_TARGET_PE
10405 /* default library paths */
10406 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/local/lib");
10407 tcc_add_library_path(s, CONFIG_SYSROOT "/usr/lib");
10408 tcc_add_library_path(s, CONFIG_SYSROOT "/lib");
10409 #endif
10411 /* no section zero */
10412 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10414 /* create standard sections */
10415 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10416 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10417 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10419 /* symbols are always generated for linking stage */
10420 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10421 ".strtab",
10422 ".hashtab", SHF_PRIVATE);
10423 strtab_section = symtab_section->link;
10425 /* private symbol table for dynamic symbols */
10426 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10427 ".dynstrtab",
10428 ".dynhashtab", SHF_PRIVATE);
10429 s->alacarte_link = 1;
10431 #ifdef CHAR_IS_UNSIGNED
10432 s->char_is_unsigned = 1;
10433 #endif
10434 #if defined(TCC_TARGET_PE) && 0
10435 /* XXX: currently the PE linker is not ready to support that */
10436 s->leading_underscore = 1;
10437 #endif
10439 #ifdef TCC_TARGET_X86_64
10440 s->jmp_table = NULL;
10441 #endif
10442 return s;
10445 void tcc_delete(TCCState *s1)
10447 int i;
10449 tcc_cleanup();
10451 /* free all sections */
10452 free_section(s1->dynsymtab_section);
10454 for(i = 1; i < s1->nb_sections; i++)
10455 free_section(s1->sections[i]);
10456 tcc_free(s1->sections);
10458 /* free any loaded DLLs */
10459 for ( i = 0; i < s1->nb_loaded_dlls; i++)
10461 DLLReference *ref = s1->loaded_dlls[i];
10462 if ( ref->handle )
10463 dlclose(ref->handle);
10466 /* free loaded dlls array */
10467 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10469 /* free library paths */
10470 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10472 /* free include paths */
10473 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10474 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10475 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10477 #ifdef TCC_TARGET_X86_64
10478 tcc_free(s1->jmp_table);
10479 #endif
10480 tcc_free(s1);
10483 int tcc_add_include_path(TCCState *s1, const char *pathname)
10485 char *pathname1;
10487 pathname1 = tcc_strdup(pathname);
10488 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10489 return 0;
10492 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10494 char *pathname1;
10496 pathname1 = tcc_strdup(pathname);
10497 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10498 return 0;
10501 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10503 const char *ext;
10504 ElfW(Ehdr) ehdr;
10505 int fd, ret;
10506 BufferedFile *saved_file;
10508 /* find source file type with extension */
10509 ext = tcc_fileextension(filename);
10510 if (ext[0])
10511 ext++;
10513 /* open the file */
10514 saved_file = file;
10515 file = tcc_open(s1, filename);
10516 if (!file) {
10517 if (flags & AFF_PRINT_ERROR) {
10518 error_noabort("file '%s' not found", filename);
10520 ret = -1;
10521 goto fail1;
10524 if (flags & AFF_PREPROCESS) {
10525 ret = tcc_preprocess(s1);
10526 } else if (!ext[0] || !strcmp(ext, "c")) {
10527 /* C file assumed */
10528 ret = tcc_compile(s1);
10529 } else
10530 #ifdef CONFIG_TCC_ASM
10531 if (!strcmp(ext, "S")) {
10532 /* preprocessed assembler */
10533 ret = tcc_assemble(s1, 1);
10534 } else if (!strcmp(ext, "s")) {
10535 /* non preprocessed assembler */
10536 ret = tcc_assemble(s1, 0);
10537 } else
10538 #endif
10539 #ifdef TCC_TARGET_PE
10540 if (!strcmp(ext, "def")) {
10541 ret = pe_load_def_file(s1, file->fd);
10542 } else
10543 #endif
10545 fd = file->fd;
10546 /* assume executable format: auto guess file type */
10547 ret = read(fd, &ehdr, sizeof(ehdr));
10548 lseek(fd, 0, SEEK_SET);
10549 if (ret <= 0) {
10550 error_noabort("could not read header");
10551 goto fail;
10552 } else if (ret != sizeof(ehdr)) {
10553 goto try_load_script;
10556 if (ehdr.e_ident[0] == ELFMAG0 &&
10557 ehdr.e_ident[1] == ELFMAG1 &&
10558 ehdr.e_ident[2] == ELFMAG2 &&
10559 ehdr.e_ident[3] == ELFMAG3) {
10560 file->line_num = 0; /* do not display line number if error */
10561 if (ehdr.e_type == ET_REL) {
10562 ret = tcc_load_object_file(s1, fd, 0);
10563 } else if (ehdr.e_type == ET_DYN) {
10564 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10565 #ifdef TCC_TARGET_PE
10566 ret = -1;
10567 #else
10568 void *h;
10569 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10570 if (h)
10571 ret = 0;
10572 else
10573 ret = -1;
10574 #endif
10575 } else {
10576 ret = tcc_load_dll(s1, fd, filename,
10577 (flags & AFF_REFERENCED_DLL) != 0);
10579 } else {
10580 error_noabort("unrecognized ELF file");
10581 goto fail;
10583 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10584 file->line_num = 0; /* do not display line number if error */
10585 ret = tcc_load_archive(s1, fd);
10586 } else
10587 #ifdef TCC_TARGET_COFF
10588 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10589 ret = tcc_load_coff(s1, fd);
10590 } else
10591 #endif
10592 #ifdef TCC_TARGET_PE
10593 if (pe_test_res_file(&ehdr, ret)) {
10594 ret = pe_load_res_file(s1, fd);
10595 } else
10596 #endif
10598 /* as GNU ld, consider it is an ld script if not recognized */
10599 try_load_script:
10600 ret = tcc_load_ldscript(s1);
10601 if (ret < 0) {
10602 error_noabort("unrecognized file type");
10603 goto fail;
10607 the_end:
10608 tcc_close(file);
10609 fail1:
10610 file = saved_file;
10611 return ret;
10612 fail:
10613 ret = -1;
10614 goto the_end;
10617 int tcc_add_file(TCCState *s, const char *filename)
10619 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10622 int tcc_add_library_path(TCCState *s, const char *pathname)
10624 char *pathname1;
10626 pathname1 = tcc_strdup(pathname);
10627 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10628 return 0;
10631 /* find and load a dll. Return non zero if not found */
10632 /* XXX: add '-rpath' option support ? */
10633 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10635 char buf[1024];
10636 int i;
10638 for(i = 0; i < s->nb_library_paths; i++) {
10639 snprintf(buf, sizeof(buf), "%s/%s",
10640 s->library_paths[i], filename);
10641 if (tcc_add_file_internal(s, buf, flags) == 0)
10642 return 0;
10644 return -1;
10647 /* the library name is the same as the argument of the '-l' option */
10648 int tcc_add_library(TCCState *s, const char *libraryname)
10650 char buf[1024];
10651 int i;
10653 /* first we look for the dynamic library if not static linking */
10654 if (!s->static_link) {
10655 #ifdef TCC_TARGET_PE
10656 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10657 #else
10658 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10659 #endif
10660 if (tcc_add_dll(s, buf, 0) == 0)
10661 return 0;
10664 /* then we look for the static library */
10665 for(i = 0; i < s->nb_library_paths; i++) {
10666 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10667 s->library_paths[i], libraryname);
10668 if (tcc_add_file_internal(s, buf, 0) == 0)
10669 return 0;
10671 return -1;
10674 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10676 add_elf_sym(symtab_section, val, 0,
10677 ELFW(ST_INFO)(STB_GLOBAL, STT_NOTYPE), 0,
10678 SHN_ABS, name);
10679 return 0;
10682 int tcc_set_output_type(TCCState *s, int output_type)
10684 char buf[1024];
10686 s->output_type = output_type;
10688 if (!s->nostdinc) {
10689 /* default include paths */
10690 /* XXX: reverse order needed if -isystem support */
10691 #ifndef TCC_TARGET_PE
10692 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/local/include");
10693 tcc_add_sysinclude_path(s, CONFIG_SYSROOT "/usr/include");
10694 #endif
10695 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10696 tcc_add_sysinclude_path(s, buf);
10697 #ifdef TCC_TARGET_PE
10698 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10699 tcc_add_sysinclude_path(s, buf);
10700 #endif
10703 /* if bound checking, then add corresponding sections */
10704 #ifdef CONFIG_TCC_BCHECK
10705 if (do_bounds_check) {
10706 /* define symbol */
10707 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10708 /* create bounds sections */
10709 bounds_section = new_section(s, ".bounds",
10710 SHT_PROGBITS, SHF_ALLOC);
10711 lbounds_section = new_section(s, ".lbounds",
10712 SHT_PROGBITS, SHF_ALLOC);
10714 #endif
10716 if (s->char_is_unsigned) {
10717 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10720 /* add debug sections */
10721 if (do_debug) {
10722 /* stab symbols */
10723 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10724 stab_section->sh_entsize = sizeof(Stab_Sym);
10725 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10726 put_elf_str(stabstr_section, "");
10727 stab_section->link = stabstr_section;
10728 /* put first entry */
10729 put_stabs("", 0, 0, 0, 0);
10732 /* add libc crt1/crti objects */
10733 #ifndef TCC_TARGET_PE
10734 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10735 !s->nostdlib) {
10736 if (output_type != TCC_OUTPUT_DLL)
10737 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10738 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10740 #endif
10742 #ifdef TCC_TARGET_PE
10743 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10744 tcc_add_library_path(s, buf);
10745 #endif
10747 return 0;
10750 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10751 #define FD_INVERT 0x0002 /* invert value before storing */
10753 typedef struct FlagDef {
10754 uint16_t offset;
10755 uint16_t flags;
10756 const char *name;
10757 } FlagDef;
10759 static const FlagDef warning_defs[] = {
10760 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10761 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10762 { offsetof(TCCState, warn_error), 0, "error" },
10763 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10764 "implicit-function-declaration" },
10767 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10768 const char *name, int value)
10770 int i;
10771 const FlagDef *p;
10772 const char *r;
10774 r = name;
10775 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10776 r += 3;
10777 value = !value;
10779 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10780 if (!strcmp(r, p->name))
10781 goto found;
10783 return -1;
10784 found:
10785 if (p->flags & FD_INVERT)
10786 value = !value;
10787 *(int *)((uint8_t *)s + p->offset) = value;
10788 return 0;
10792 /* set/reset a warning */
10793 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10795 int i;
10796 const FlagDef *p;
10798 if (!strcmp(warning_name, "all")) {
10799 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10800 if (p->flags & WD_ALL)
10801 *(int *)((uint8_t *)s + p->offset) = 1;
10803 return 0;
10804 } else {
10805 return set_flag(s, warning_defs, countof(warning_defs),
10806 warning_name, value);
10810 static const FlagDef flag_defs[] = {
10811 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10812 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10813 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10814 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10817 /* set/reset a flag */
10818 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10820 return set_flag(s, flag_defs, countof(flag_defs),
10821 flag_name, value);
10824 #if !defined(LIBTCC)
10826 static int64_t getclock_us(void)
10828 #ifdef _WIN32
10829 struct _timeb tb;
10830 _ftime(&tb);
10831 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10832 #else
10833 struct timeval tv;
10834 gettimeofday(&tv, NULL);
10835 return tv.tv_sec * 1000000LL + tv.tv_usec;
10836 #endif
10839 void help(void)
10841 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10842 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10843 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10844 " [-static] [infile1 infile2...] [-run infile args...]\n"
10845 "\n"
10846 "General options:\n"
10847 " -v display current version, increase verbosity\n"
10848 " -c compile only - generate an object file\n"
10849 " -o outfile set output filename\n"
10850 " -Bdir set tcc internal library path\n"
10851 " -bench output compilation statistics\n"
10852 " -run run compiled source\n"
10853 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10854 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10855 " -w disable all warnings\n"
10856 "Preprocessor options:\n"
10857 " -E preprocess only\n"
10858 " -Idir add include path 'dir'\n"
10859 " -Dsym[=val] define 'sym' with value 'val'\n"
10860 " -Usym undefine 'sym'\n"
10861 "Linker options:\n"
10862 " -Ldir add library path 'dir'\n"
10863 " -llib link with dynamic or static library 'lib'\n"
10864 " -shared generate a shared library\n"
10865 " -soname set name for shared library to be used at runtime\n"
10866 " -static static linking\n"
10867 " -rdynamic export all global symbols to dynamic linker\n"
10868 " -r generate (relocatable) object file\n"
10869 "Debugger options:\n"
10870 " -g generate runtime debug info\n"
10871 #ifdef CONFIG_TCC_BCHECK
10872 " -b compile with built-in memory and bounds checker (implies -g)\n"
10873 #endif
10874 " -bt N show N callers in stack traces\n"
10878 #define TCC_OPTION_HAS_ARG 0x0001
10879 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10881 typedef struct TCCOption {
10882 const char *name;
10883 uint16_t index;
10884 uint16_t flags;
10885 } TCCOption;
10887 enum {
10888 TCC_OPTION_HELP,
10889 TCC_OPTION_I,
10890 TCC_OPTION_D,
10891 TCC_OPTION_U,
10892 TCC_OPTION_L,
10893 TCC_OPTION_B,
10894 TCC_OPTION_l,
10895 TCC_OPTION_bench,
10896 TCC_OPTION_bt,
10897 TCC_OPTION_b,
10898 TCC_OPTION_g,
10899 TCC_OPTION_c,
10900 TCC_OPTION_static,
10901 TCC_OPTION_shared,
10902 TCC_OPTION_soname,
10903 TCC_OPTION_o,
10904 TCC_OPTION_r,
10905 TCC_OPTION_Wl,
10906 TCC_OPTION_W,
10907 TCC_OPTION_O,
10908 TCC_OPTION_m,
10909 TCC_OPTION_f,
10910 TCC_OPTION_nostdinc,
10911 TCC_OPTION_nostdlib,
10912 TCC_OPTION_print_search_dirs,
10913 TCC_OPTION_rdynamic,
10914 TCC_OPTION_run,
10915 TCC_OPTION_v,
10916 TCC_OPTION_w,
10917 TCC_OPTION_pipe,
10918 TCC_OPTION_E,
10921 static const TCCOption tcc_options[] = {
10922 { "h", TCC_OPTION_HELP, 0 },
10923 { "?", TCC_OPTION_HELP, 0 },
10924 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10925 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10926 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10927 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10928 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10929 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10930 { "bench", TCC_OPTION_bench, 0 },
10931 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10932 #ifdef CONFIG_TCC_BCHECK
10933 { "b", TCC_OPTION_b, 0 },
10934 #endif
10935 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10936 { "c", TCC_OPTION_c, 0 },
10937 { "static", TCC_OPTION_static, 0 },
10938 { "shared", TCC_OPTION_shared, 0 },
10939 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10940 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10941 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10942 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10943 { "r", TCC_OPTION_r, 0 },
10944 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10945 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10946 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10947 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10948 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10949 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10950 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10951 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10952 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10953 { "w", TCC_OPTION_w, 0 },
10954 { "pipe", TCC_OPTION_pipe, 0},
10955 { "E", TCC_OPTION_E, 0},
10956 { NULL },
10959 /* convert 'str' into an array of space separated strings */
10960 static int expand_args(char ***pargv, const char *str)
10962 const char *s1;
10963 char **argv, *arg;
10964 int argc, len;
10966 argc = 0;
10967 argv = NULL;
10968 for(;;) {
10969 while (is_space(*str))
10970 str++;
10971 if (*str == '\0')
10972 break;
10973 s1 = str;
10974 while (*str != '\0' && !is_space(*str))
10975 str++;
10976 len = str - s1;
10977 arg = tcc_malloc(len + 1);
10978 memcpy(arg, s1, len);
10979 arg[len] = '\0';
10980 dynarray_add((void ***)&argv, &argc, arg);
10982 *pargv = argv;
10983 return argc;
10986 static char **files;
10987 static int nb_files, nb_libraries;
10988 static int multiple_files;
10989 static int print_search_dirs;
10990 static int output_type;
10991 static int reloc_output;
10992 static const char *outfile;
10994 int parse_args(TCCState *s, int argc, char **argv)
10996 int optind;
10997 const TCCOption *popt;
10998 const char *optarg, *p1, *r1;
10999 char *r;
11001 optind = 0;
11002 while (optind < argc) {
11004 r = argv[optind++];
11005 if (r[0] != '-' || r[1] == '\0') {
11006 /* add a new file */
11007 dynarray_add((void ***)&files, &nb_files, r);
11008 if (!multiple_files) {
11009 optind--;
11010 /* argv[0] will be this file */
11011 break;
11013 } else {
11014 /* find option in table (match only the first chars */
11015 popt = tcc_options;
11016 for(;;) {
11017 p1 = popt->name;
11018 if (p1 == NULL)
11019 error("invalid option -- '%s'", r);
11020 r1 = r + 1;
11021 for(;;) {
11022 if (*p1 == '\0')
11023 goto option_found;
11024 if (*r1 != *p1)
11025 break;
11026 p1++;
11027 r1++;
11029 popt++;
11031 option_found:
11032 if (popt->flags & TCC_OPTION_HAS_ARG) {
11033 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
11034 optarg = r1;
11035 } else {
11036 if (optind >= argc)
11037 error("argument to '%s' is missing", r);
11038 optarg = argv[optind++];
11040 } else {
11041 if (*r1 != '\0')
11042 return 0;
11043 optarg = NULL;
11046 switch(popt->index) {
11047 case TCC_OPTION_HELP:
11048 return 0;
11050 case TCC_OPTION_I:
11051 if (tcc_add_include_path(s, optarg) < 0)
11052 error("too many include paths");
11053 break;
11054 case TCC_OPTION_D:
11056 char *sym, *value;
11057 sym = (char *)optarg;
11058 value = strchr(sym, '=');
11059 if (value) {
11060 *value = '\0';
11061 value++;
11063 tcc_define_symbol(s, sym, value);
11065 break;
11066 case TCC_OPTION_U:
11067 tcc_undefine_symbol(s, optarg);
11068 break;
11069 case TCC_OPTION_L:
11070 tcc_add_library_path(s, optarg);
11071 break;
11072 case TCC_OPTION_B:
11073 /* set tcc utilities path (mainly for tcc development) */
11074 tcc_lib_path = optarg;
11075 break;
11076 case TCC_OPTION_l:
11077 dynarray_add((void ***)&files, &nb_files, r);
11078 nb_libraries++;
11079 break;
11080 case TCC_OPTION_bench:
11081 do_bench = 1;
11082 break;
11083 case TCC_OPTION_bt:
11084 num_callers = atoi(optarg);
11085 break;
11086 #ifdef CONFIG_TCC_BCHECK
11087 case TCC_OPTION_b:
11088 do_bounds_check = 1;
11089 do_debug = 1;
11090 break;
11091 #endif
11092 case TCC_OPTION_g:
11093 do_debug = 1;
11094 break;
11095 case TCC_OPTION_c:
11096 multiple_files = 1;
11097 output_type = TCC_OUTPUT_OBJ;
11098 break;
11099 case TCC_OPTION_static:
11100 s->static_link = 1;
11101 break;
11102 case TCC_OPTION_shared:
11103 output_type = TCC_OUTPUT_DLL;
11104 break;
11105 case TCC_OPTION_soname:
11106 s->soname = optarg;
11107 break;
11108 case TCC_OPTION_o:
11109 multiple_files = 1;
11110 outfile = optarg;
11111 break;
11112 case TCC_OPTION_r:
11113 /* generate a .o merging several output files */
11114 reloc_output = 1;
11115 output_type = TCC_OUTPUT_OBJ;
11116 break;
11117 case TCC_OPTION_nostdinc:
11118 s->nostdinc = 1;
11119 break;
11120 case TCC_OPTION_nostdlib:
11121 s->nostdlib = 1;
11122 break;
11123 case TCC_OPTION_print_search_dirs:
11124 print_search_dirs = 1;
11125 break;
11126 case TCC_OPTION_run:
11128 int argc1;
11129 char **argv1;
11130 argc1 = expand_args(&argv1, optarg);
11131 if (argc1 > 0) {
11132 parse_args(s, argc1, argv1);
11134 multiple_files = 0;
11135 output_type = TCC_OUTPUT_MEMORY;
11137 break;
11138 case TCC_OPTION_v:
11139 do {
11140 if (0 == verbose++)
11141 printf("tcc version %s\n", TCC_VERSION);
11142 } while (*optarg++ == 'v');
11143 break;
11144 case TCC_OPTION_f:
11145 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
11146 goto unsupported_option;
11147 break;
11148 case TCC_OPTION_W:
11149 if (tcc_set_warning(s, optarg, 1) < 0 &&
11150 s->warn_unsupported)
11151 goto unsupported_option;
11152 break;
11153 case TCC_OPTION_w:
11154 s->warn_none = 1;
11155 break;
11156 case TCC_OPTION_rdynamic:
11157 s->rdynamic = 1;
11158 break;
11159 case TCC_OPTION_Wl:
11161 const char *p;
11162 if (strstart(optarg, "-Ttext,", &p)) {
11163 s->text_addr = strtoul(p, NULL, 16);
11164 s->has_text_addr = 1;
11165 } else if (strstart(optarg, "--oformat,", &p)) {
11166 if (strstart(p, "elf32-", NULL)) {
11167 s->output_format = TCC_OUTPUT_FORMAT_ELF;
11168 } else if (!strcmp(p, "binary")) {
11169 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
11170 } else
11171 #ifdef TCC_TARGET_COFF
11172 if (!strcmp(p, "coff")) {
11173 s->output_format = TCC_OUTPUT_FORMAT_COFF;
11174 } else
11175 #endif
11177 error("target %s not found", p);
11179 } else {
11180 error("unsupported linker option '%s'", optarg);
11183 break;
11184 case TCC_OPTION_E:
11185 output_type = TCC_OUTPUT_PREPROCESS;
11186 break;
11187 default:
11188 if (s->warn_unsupported) {
11189 unsupported_option:
11190 warning("unsupported option '%s'", r);
11192 break;
11196 return optind + 1;
11199 int main(int argc, char **argv)
11201 int i;
11202 TCCState *s;
11203 int nb_objfiles, ret, optind;
11204 char objfilename[1024];
11205 int64_t start_time = 0;
11207 #ifdef _WIN32
11208 tcc_lib_path = w32_tcc_lib_path();
11209 #endif
11211 s = tcc_new();
11212 output_type = TCC_OUTPUT_EXE;
11213 outfile = NULL;
11214 multiple_files = 1;
11215 files = NULL;
11216 nb_files = 0;
11217 nb_libraries = 0;
11218 reloc_output = 0;
11219 print_search_dirs = 0;
11220 ret = 0;
11222 optind = parse_args(s, argc - 1, argv + 1);
11223 if (print_search_dirs) {
11224 /* enough for Linux kernel */
11225 printf("install: %s/\n", tcc_lib_path);
11226 return 0;
11228 if (optind == 0 || nb_files == 0) {
11229 if (optind && verbose)
11230 return 0;
11231 help();
11232 return 1;
11235 nb_objfiles = nb_files - nb_libraries;
11237 /* if outfile provided without other options, we output an
11238 executable */
11239 if (outfile && output_type == TCC_OUTPUT_MEMORY)
11240 output_type = TCC_OUTPUT_EXE;
11242 /* check -c consistency : only single file handled. XXX: checks file type */
11243 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
11244 /* accepts only a single input file */
11245 if (nb_objfiles != 1)
11246 error("cannot specify multiple files with -c");
11247 if (nb_libraries != 0)
11248 error("cannot specify libraries with -c");
11252 if (output_type == TCC_OUTPUT_PREPROCESS) {
11253 if (!outfile) {
11254 s->outfile = stdout;
11255 } else {
11256 s->outfile = fopen(outfile, "w");
11257 if (!s->outfile)
11258 error("could not open '%s", outfile);
11260 } else if (output_type != TCC_OUTPUT_MEMORY) {
11261 if (!outfile) {
11262 /* compute default outfile name */
11263 char *ext;
11264 const char *name =
11265 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
11266 pstrcpy(objfilename, sizeof(objfilename), name);
11267 ext = tcc_fileextension(objfilename);
11268 #ifdef TCC_TARGET_PE
11269 if (output_type == TCC_OUTPUT_DLL)
11270 strcpy(ext, ".dll");
11271 else
11272 if (output_type == TCC_OUTPUT_EXE)
11273 strcpy(ext, ".exe");
11274 else
11275 #endif
11276 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
11277 strcpy(ext, ".o");
11278 else
11279 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11280 outfile = objfilename;
11284 if (do_bench) {
11285 start_time = getclock_us();
11288 tcc_set_output_type(s, output_type);
11290 /* compile or add each files or library */
11291 for(i = 0; i < nb_files && ret == 0; i++) {
11292 const char *filename;
11294 filename = files[i];
11295 if (output_type == TCC_OUTPUT_PREPROCESS) {
11296 if (tcc_add_file_internal(s, filename,
11297 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11298 ret = 1;
11299 } else if (filename[0] == '-' && filename[1]) {
11300 if (tcc_add_library(s, filename + 2) < 0)
11301 error("cannot find %s", filename);
11302 } else {
11303 if (1 == verbose)
11304 printf("-> %s\n", filename);
11305 if (tcc_add_file(s, filename) < 0)
11306 ret = 1;
11310 /* free all files */
11311 tcc_free(files);
11313 if (ret)
11314 goto the_end;
11316 if (do_bench) {
11317 double total_time;
11318 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11319 if (total_time < 0.001)
11320 total_time = 0.001;
11321 if (total_bytes < 1)
11322 total_bytes = 1;
11323 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11324 tok_ident - TOK_IDENT, total_lines, total_bytes,
11325 total_time, (int)(total_lines / total_time),
11326 total_bytes / total_time / 1000000.0);
11329 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11330 if (outfile)
11331 fclose(s->outfile);
11332 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11333 ret = tcc_run(s, argc - optind, argv + optind);
11334 } else
11335 ret = tcc_output_file(s, outfile) ? 1 : 0;
11336 the_end:
11337 /* XXX: cannot do it with bound checking because of the malloc hooks */
11338 if (!do_bounds_check)
11339 tcc_delete(s);
11341 #ifdef MEM_DEBUG
11342 if (do_bench) {
11343 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11345 #endif
11346 return ret;
11349 #endif