Accept standard input as an inputstream (Hanzac Chen)
[tinycc/daniel.git] / tcc.c
bloba57344c0643f5f1fa68c1ade9bc2026e64e05ccc
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define _GNU_SOURCE
21 #include "config.h"
23 #ifdef CONFIG_TCCBOOT
25 #include "tccboot.h"
26 #define CONFIG_TCC_STATIC
28 #else
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <math.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <fcntl.h>
39 #include <setjmp.h>
40 #include <time.h>
41 #ifdef _WIN32
42 #include <sys/timeb.h>
43 #include <windows.h>
44 #endif
45 #ifndef _WIN32
46 #include <sys/time.h>
47 #include <sys/ucontext.h>
48 #include <sys/mman.h>
49 #endif
51 #endif /* !CONFIG_TCCBOOT */
53 #ifndef PAGESIZE
54 #define PAGESIZE 4096
55 #endif
57 #include "elf.h"
58 #include "stab.h"
60 #ifndef O_BINARY
61 #define O_BINARY 0
62 #endif
64 #include "libtcc.h"
66 /* parser debug */
67 //#define PARSE_DEBUG
68 /* preprocessor debug */
69 //#define PP_DEBUG
70 /* include file debug */
71 //#define INC_DEBUG
73 //#define MEM_DEBUG
75 /* assembler debug */
76 //#define ASM_DEBUG
78 /* target selection */
79 //#define TCC_TARGET_I386 /* i386 code generator */
80 //#define TCC_TARGET_ARM /* ARMv4 code generator */
81 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
83 /* default target is I386 */
84 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
85 !defined(TCC_TARGET_C67)
86 #define TCC_TARGET_I386
87 #endif
89 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
90 !defined(TCC_TARGET_C67)
91 #define CONFIG_TCC_BCHECK /* enable bound checking code */
92 #endif
94 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
95 #define CONFIG_TCC_STATIC
96 #endif
98 /* define it to include assembler support */
99 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
100 #define CONFIG_TCC_ASM
101 #endif
103 /* object format selection */
104 #if defined(TCC_TARGET_C67)
105 #define TCC_TARGET_COFF
106 #endif
108 #define FALSE 0
109 #define false 0
110 #define TRUE 1
111 #define true 1
112 typedef int BOOL;
114 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
115 executables or dlls */
116 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
118 #define INCLUDE_STACK_SIZE 32
119 #define IFDEF_STACK_SIZE 64
120 #define VSTACK_SIZE 256
121 #define STRING_MAX_SIZE 1024
122 #define PACK_STACK_SIZE 8
124 #define TOK_HASH_SIZE 8192 /* must be a power of two */
125 #define TOK_ALLOC_INCR 512 /* must be a power of two */
126 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
128 /* token symbol management */
129 typedef struct TokenSym {
130 struct TokenSym *hash_next;
131 struct Sym *sym_define; /* direct pointer to define */
132 struct Sym *sym_label; /* direct pointer to label */
133 struct Sym *sym_struct; /* direct pointer to structure */
134 struct Sym *sym_identifier; /* direct pointer to identifier */
135 int tok; /* token number */
136 int len;
137 char str[1];
138 } TokenSym;
140 #ifdef TCC_TARGET_PE
141 typedef unsigned short nwchar_t;
142 #else
143 typedef int nwchar_t;
144 #endif
146 typedef struct CString {
147 int size; /* size in bytes */
148 void *data; /* either 'char *' or 'nwchar_t *' */
149 int size_allocated;
150 void *data_allocated; /* if non NULL, data has been malloced */
151 } CString;
153 /* type definition */
154 typedef struct CType {
155 int t;
156 struct Sym *ref;
157 } CType;
159 /* constant value */
160 typedef union CValue {
161 long double ld;
162 double d;
163 float f;
164 int i;
165 unsigned int ui;
166 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
167 long long ll;
168 unsigned long long ull;
169 struct CString *cstr;
170 void *ptr;
171 int tab[1];
172 } CValue;
174 /* value on stack */
175 typedef struct SValue {
176 CType type; /* type */
177 unsigned short r; /* register + flags */
178 unsigned short r2; /* second register, used for 'long long'
179 type. If not used, set to VT_CONST */
180 CValue c; /* constant, if VT_CONST */
181 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
182 } SValue;
184 /* symbol management */
185 typedef struct Sym {
186 int v; /* symbol token */
187 int r; /* associated register */
188 int c; /* associated number */
189 CType type; /* associated type */
190 struct Sym *next; /* next related symbol */
191 struct Sym *prev; /* prev symbol in stack */
192 struct Sym *prev_tok; /* previous symbol for this token */
193 } Sym;
195 /* section definition */
196 /* XXX: use directly ELF structure for parameters ? */
197 /* special flag to indicate that the section should not be linked to
198 the other ones */
199 #define SHF_PRIVATE 0x80000000
201 typedef struct Section {
202 unsigned long data_offset; /* current data offset */
203 unsigned char *data; /* section data */
204 unsigned long data_allocated; /* used for realloc() handling */
205 int sh_name; /* elf section name (only used during output) */
206 int sh_num; /* elf section number */
207 int sh_type; /* elf section type */
208 int sh_flags; /* elf section flags */
209 int sh_info; /* elf section info */
210 int sh_addralign; /* elf section alignment */
211 int sh_entsize; /* elf entry size */
212 unsigned long sh_size; /* section size (only used during output) */
213 unsigned long sh_addr; /* address at which the section is relocated */
214 unsigned long sh_offset; /* file offset */
215 int nb_hashed_syms; /* used to resize the hash table */
216 struct Section *link; /* link to another section */
217 struct Section *reloc; /* corresponding section for relocation, if any */
218 struct Section *hash; /* hash table for symbols */
219 struct Section *next;
220 char name[1]; /* section name */
221 } Section;
223 typedef struct DLLReference {
224 int level;
225 char name[1];
226 } DLLReference;
228 /* GNUC attribute definition */
229 typedef struct AttributeDef {
230 int aligned;
231 int packed;
232 Section *section;
233 int func_attr; /* calling convention, exports, ... */
234 } AttributeDef;
236 /* -------------------------------------------------- */
237 /* gr: wrappers for casting sym->r for other purposes */
238 typedef struct {
239 unsigned
240 func_call : 8,
241 func_args : 8,
242 func_export : 1;
243 } func_attr_t;
245 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
246 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
247 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
248 #define INLINE_DEF(r) (*(int **)&(r))
249 /* -------------------------------------------------- */
251 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
252 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
253 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
255 /* stored in 'Sym.c' field */
256 #define FUNC_NEW 1 /* ansi function prototype */
257 #define FUNC_OLD 2 /* old function prototype */
258 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
260 /* stored in 'Sym.r' field */
261 #define FUNC_CDECL 0 /* standard c call */
262 #define FUNC_STDCALL 1 /* pascal c call */
263 #define FUNC_FASTCALL1 2 /* first param in %eax */
264 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
265 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
266 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
268 /* field 'Sym.t' for macros */
269 #define MACRO_OBJ 0 /* object like macro */
270 #define MACRO_FUNC 1 /* function like macro */
272 /* field 'Sym.r' for C labels */
273 #define LABEL_DEFINED 0 /* label is defined */
274 #define LABEL_FORWARD 1 /* label is forward defined */
275 #define LABEL_DECLARED 2 /* label is declared but never used */
277 /* type_decl() types */
278 #define TYPE_ABSTRACT 1 /* type without variable */
279 #define TYPE_DIRECT 2 /* type with variable */
281 #define IO_BUF_SIZE 8192
283 typedef struct BufferedFile {
284 uint8_t *buf_ptr;
285 uint8_t *buf_end;
286 int fd;
287 int line_num; /* current line number - here to simplify code */
288 int ifndef_macro; /* #ifndef macro / #endif search */
289 int ifndef_macro_saved; /* saved ifndef_macro */
290 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
291 char inc_type; /* type of include */
292 char inc_filename[512]; /* filename specified by the user */
293 char filename[1024]; /* current filename - here to simplify code */
294 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
295 } BufferedFile;
297 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
298 #define CH_EOF (-1) /* end of file */
300 /* parsing state (used to save parser state to reparse part of the
301 source several times) */
302 typedef struct ParseState {
303 int *macro_ptr;
304 int line_num;
305 int tok;
306 CValue tokc;
307 } ParseState;
309 /* used to record tokens */
310 typedef struct TokenString {
311 int *str;
312 int len;
313 int allocated_len;
314 int last_line_num;
315 } TokenString;
317 /* include file cache, used to find files faster and also to eliminate
318 inclusion if the include file is protected by #ifndef ... #endif */
319 typedef struct CachedInclude {
320 int ifndef_macro;
321 int hash_next; /* -1 if none */
322 char type; /* '"' or '>' to give include type */
323 char filename[1]; /* path specified in #include */
324 } CachedInclude;
326 #define CACHED_INCLUDES_HASH_SIZE 512
328 /* parser */
329 static struct BufferedFile *file;
330 static int ch, tok;
331 static CValue tokc;
332 static CString tokcstr; /* current parsed string, if any */
333 /* additional informations about token */
334 static int tok_flags;
335 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
336 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
337 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
338 #define TOK_FLAG_EOF 0x0008 /* end of file */
340 static int *macro_ptr, *macro_ptr_allocated;
341 static int *unget_saved_macro_ptr;
342 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
343 static int unget_buffer_enabled;
344 static int parse_flags;
345 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
346 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
347 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
348 token. line feed is also
349 returned at eof */
350 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
352 static Section *text_section, *data_section, *bss_section; /* predefined sections */
353 static Section *cur_text_section; /* current section where function code is
354 generated */
355 #ifdef CONFIG_TCC_ASM
356 static Section *last_text_section; /* to handle .previous asm directive */
357 #endif
358 /* bound check related sections */
359 static Section *bounds_section; /* contains global data bound description */
360 static Section *lbounds_section; /* contains local data bound description */
361 /* symbol sections */
362 static Section *symtab_section, *strtab_section;
364 /* debug sections */
365 static Section *stab_section, *stabstr_section;
367 /* loc : local variable index
368 ind : output code index
369 rsym: return symbol
370 anon_sym: anonymous symbol index
372 static int rsym, anon_sym, ind, loc;
373 /* expression generation modifiers */
374 static int const_wanted; /* true if constant wanted */
375 static int nocode_wanted; /* true if no code generation wanted for an expression */
376 static int global_expr; /* true if compound literals must be allocated
377 globally (used during initializers parsing */
378 static CType func_vt; /* current function return type (used by return
379 instruction) */
380 static int func_vc;
381 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
382 static int tok_ident;
383 static TokenSym **table_ident;
384 static TokenSym *hash_ident[TOK_HASH_SIZE];
385 static char token_buf[STRING_MAX_SIZE + 1];
386 static char *funcname;
387 static Sym *global_stack, *local_stack;
388 static Sym *define_stack;
389 static Sym *global_label_stack, *local_label_stack;
390 /* symbol allocator */
391 #define SYM_POOL_NB (8192 / sizeof(Sym))
392 static Sym *sym_free_first;
394 static SValue vstack[VSTACK_SIZE], *vtop;
395 /* some predefined types */
396 static CType char_pointer_type, func_old_type, int_type;
397 /* true if isid(c) || isnum(c) */
398 static unsigned char isidnum_table[256];
400 /* display some information during compilation */
401 static int verbose = 0;
403 /* compile with debug symbol (and use them if error during execution) */
404 static int do_debug = 0;
406 /* compile with built-in memory and bounds checker */
407 static int do_bounds_check = 0;
409 /* display benchmark infos */
410 #if !defined(LIBTCC)
411 static int do_bench = 0;
412 #endif
413 static int total_lines;
414 static int total_bytes;
416 /* use GNU C extensions */
417 static int gnu_ext = 1;
419 /* use Tiny C extensions */
420 static int tcc_ext = 1;
422 /* max number of callers shown if error */
423 static int num_callers = 6;
424 static const char **rt_bound_error_msg;
426 /* XXX: get rid of this ASAP */
427 static struct TCCState *tcc_state;
429 /* give the path of the tcc libraries */
430 static const char *tcc_lib_path = CONFIG_TCCDIR;
432 struct TCCState {
433 int output_type;
435 BufferedFile **include_stack_ptr;
436 int *ifdef_stack_ptr;
438 /* include file handling */
439 char **include_paths;
440 int nb_include_paths;
441 char **sysinclude_paths;
442 int nb_sysinclude_paths;
443 CachedInclude **cached_includes;
444 int nb_cached_includes;
446 char **library_paths;
447 int nb_library_paths;
449 /* array of all loaded dlls (including those referenced by loaded
450 dlls) */
451 DLLReference **loaded_dlls;
452 int nb_loaded_dlls;
454 /* sections */
455 Section **sections;
456 int nb_sections; /* number of sections, including first dummy section */
458 /* got handling */
459 Section *got;
460 Section *plt;
461 unsigned long *got_offsets;
462 int nb_got_offsets;
463 /* give the correspondance from symtab indexes to dynsym indexes */
464 int *symtab_to_dynsym;
466 /* temporary dynamic symbol sections (for dll loading) */
467 Section *dynsymtab_section;
468 /* exported dynamic symbol section */
469 Section *dynsym;
471 int nostdinc; /* if true, no standard headers are added */
472 int nostdlib; /* if true, no standard libraries are added */
474 int nocommon; /* if true, do not use common symbols for .bss data */
476 /* if true, static linking is performed */
477 int static_link;
479 /* soname as specified on the command line (-soname) */
480 const char *soname;
482 /* if true, all symbols are exported */
483 int rdynamic;
485 /* if true, only link in referenced objects from archive */
486 int alacarte_link;
488 /* address of text section */
489 unsigned long text_addr;
490 int has_text_addr;
492 /* output format, see TCC_OUTPUT_FORMAT_xxx */
493 int output_format;
495 /* C language options */
496 int char_is_unsigned;
497 int leading_underscore;
499 /* warning switches */
500 int warn_write_strings;
501 int warn_unsupported;
502 int warn_error;
503 int warn_none;
504 int warn_implicit_function_declaration;
506 /* error handling */
507 void *error_opaque;
508 void (*error_func)(void *opaque, const char *msg);
509 int error_set_jmp_enabled;
510 jmp_buf error_jmp_buf;
511 int nb_errors;
513 /* tiny assembler state */
514 Sym *asm_labels;
516 /* see include_stack_ptr */
517 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
519 /* see ifdef_stack_ptr */
520 int ifdef_stack[IFDEF_STACK_SIZE];
522 /* see cached_includes */
523 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
525 /* pack stack */
526 int pack_stack[PACK_STACK_SIZE];
527 int *pack_stack_ptr;
529 /* output file for preprocessing */
530 FILE *outfile;
533 /* The current value can be: */
534 #define VT_VALMASK 0x00ff
535 #define VT_CONST 0x00f0 /* constant in vc
536 (must be first non register value) */
537 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
538 #define VT_LOCAL 0x00f2 /* offset on stack */
539 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
540 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
541 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
542 #define VT_LVAL 0x0100 /* var is an lvalue */
543 #define VT_SYM 0x0200 /* a symbol value is added */
544 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
545 char/short stored in integer registers) */
546 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
547 dereferencing value */
548 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
549 bounding function call point is in vc */
550 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
551 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
552 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
553 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
555 /* types */
556 #define VT_INT 0 /* integer type */
557 #define VT_BYTE 1 /* signed byte type */
558 #define VT_SHORT 2 /* short type */
559 #define VT_VOID 3 /* void type */
560 #define VT_PTR 4 /* pointer */
561 #define VT_ENUM 5 /* enum definition */
562 #define VT_FUNC 6 /* function type */
563 #define VT_STRUCT 7 /* struct/union definition */
564 #define VT_FLOAT 8 /* IEEE float */
565 #define VT_DOUBLE 9 /* IEEE double */
566 #define VT_LDOUBLE 10 /* IEEE long double */
567 #define VT_BOOL 11 /* ISOC99 boolean type */
568 #define VT_LLONG 12 /* 64 bit integer */
569 #define VT_LONG 13 /* long integer (NEVER USED as type, only
570 during parsing) */
571 #define VT_BTYPE 0x000f /* mask for basic type */
572 #define VT_UNSIGNED 0x0010 /* unsigned type */
573 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
574 #define VT_BITFIELD 0x0040 /* bitfield modifier */
575 #define VT_CONSTANT 0x0800 /* const modifier */
576 #define VT_VOLATILE 0x1000 /* volatile modifier */
577 #define VT_SIGNED 0x2000 /* signed type */
579 /* storage */
580 #define VT_EXTERN 0x00000080 /* extern definition */
581 #define VT_STATIC 0x00000100 /* static variable */
582 #define VT_TYPEDEF 0x00000200 /* typedef definition */
583 #define VT_INLINE 0x00000400 /* inline definition */
585 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
587 /* type mask (except storage) */
588 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
589 #define VT_TYPE (~(VT_STORAGE))
591 /* token values */
593 /* warning: the following compare tokens depend on i386 asm code */
594 #define TOK_ULT 0x92
595 #define TOK_UGE 0x93
596 #define TOK_EQ 0x94
597 #define TOK_NE 0x95
598 #define TOK_ULE 0x96
599 #define TOK_UGT 0x97
600 #define TOK_Nset 0x98
601 #define TOK_Nclear 0x99
602 #define TOK_LT 0x9c
603 #define TOK_GE 0x9d
604 #define TOK_LE 0x9e
605 #define TOK_GT 0x9f
607 #define TOK_LAND 0xa0
608 #define TOK_LOR 0xa1
610 #define TOK_DEC 0xa2
611 #define TOK_MID 0xa3 /* inc/dec, to void constant */
612 #define TOK_INC 0xa4
613 #define TOK_UDIV 0xb0 /* unsigned division */
614 #define TOK_UMOD 0xb1 /* unsigned modulo */
615 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
616 #define TOK_CINT 0xb3 /* number in tokc */
617 #define TOK_CCHAR 0xb4 /* char constant in tokc */
618 #define TOK_STR 0xb5 /* pointer to string in tokc */
619 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
620 #define TOK_LCHAR 0xb7
621 #define TOK_LSTR 0xb8
622 #define TOK_CFLOAT 0xb9 /* float constant */
623 #define TOK_LINENUM 0xba /* line number info */
624 #define TOK_CDOUBLE 0xc0 /* double constant */
625 #define TOK_CLDOUBLE 0xc1 /* long double constant */
626 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
627 #define TOK_ADDC1 0xc3 /* add with carry generation */
628 #define TOK_ADDC2 0xc4 /* add with carry use */
629 #define TOK_SUBC1 0xc5 /* add with carry generation */
630 #define TOK_SUBC2 0xc6 /* add with carry use */
631 #define TOK_CUINT 0xc8 /* unsigned int constant */
632 #define TOK_CLLONG 0xc9 /* long long constant */
633 #define TOK_CULLONG 0xca /* unsigned long long constant */
634 #define TOK_ARROW 0xcb
635 #define TOK_DOTS 0xcc /* three dots */
636 #define TOK_SHR 0xcd /* unsigned shift right */
637 #define TOK_PPNUM 0xce /* preprocessor number */
639 #define TOK_SHL 0x01 /* shift left */
640 #define TOK_SAR 0x02 /* signed shift right */
642 /* assignement operators : normal operator or 0x80 */
643 #define TOK_A_MOD 0xa5
644 #define TOK_A_AND 0xa6
645 #define TOK_A_MUL 0xaa
646 #define TOK_A_ADD 0xab
647 #define TOK_A_SUB 0xad
648 #define TOK_A_DIV 0xaf
649 #define TOK_A_XOR 0xde
650 #define TOK_A_OR 0xfc
651 #define TOK_A_SHL 0x81
652 #define TOK_A_SAR 0x82
654 #ifndef offsetof
655 #define offsetof(type, field) ((size_t) &((type *)0)->field)
656 #endif
658 #ifndef countof
659 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
660 #endif
662 /* WARNING: the content of this string encodes token numbers */
663 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";
665 #define TOK_EOF (-1) /* end of file */
666 #define TOK_LINEFEED 10 /* line feed */
668 /* all identificators and strings have token above that */
669 #define TOK_IDENT 256
671 /* only used for i386 asm opcodes definitions */
672 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
674 #define DEF_BWL(x) \
675 DEF(TOK_ASM_ ## x ## b, #x "b") \
676 DEF(TOK_ASM_ ## x ## w, #x "w") \
677 DEF(TOK_ASM_ ## x ## l, #x "l") \
678 DEF(TOK_ASM_ ## x, #x)
680 #define DEF_WL(x) \
681 DEF(TOK_ASM_ ## x ## w, #x "w") \
682 DEF(TOK_ASM_ ## x ## l, #x "l") \
683 DEF(TOK_ASM_ ## x, #x)
685 #define DEF_FP1(x) \
686 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
687 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
688 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
689 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
691 #define DEF_FP(x) \
692 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
693 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
694 DEF_FP1(x)
696 #define DEF_ASMTEST(x) \
697 DEF_ASM(x ## o) \
698 DEF_ASM(x ## no) \
699 DEF_ASM(x ## b) \
700 DEF_ASM(x ## c) \
701 DEF_ASM(x ## nae) \
702 DEF_ASM(x ## nb) \
703 DEF_ASM(x ## nc) \
704 DEF_ASM(x ## ae) \
705 DEF_ASM(x ## e) \
706 DEF_ASM(x ## z) \
707 DEF_ASM(x ## ne) \
708 DEF_ASM(x ## nz) \
709 DEF_ASM(x ## be) \
710 DEF_ASM(x ## na) \
711 DEF_ASM(x ## nbe) \
712 DEF_ASM(x ## a) \
713 DEF_ASM(x ## s) \
714 DEF_ASM(x ## ns) \
715 DEF_ASM(x ## p) \
716 DEF_ASM(x ## pe) \
717 DEF_ASM(x ## np) \
718 DEF_ASM(x ## po) \
719 DEF_ASM(x ## l) \
720 DEF_ASM(x ## nge) \
721 DEF_ASM(x ## nl) \
722 DEF_ASM(x ## ge) \
723 DEF_ASM(x ## le) \
724 DEF_ASM(x ## ng) \
725 DEF_ASM(x ## nle) \
726 DEF_ASM(x ## g)
728 #define TOK_ASM_int TOK_INT
730 enum tcc_token {
731 TOK_LAST = TOK_IDENT - 1,
732 #define DEF(id, str) id,
733 #include "tcctok.h"
734 #undef DEF
737 static const char tcc_keywords[] =
738 #define DEF(id, str) str "\0"
739 #include "tcctok.h"
740 #undef DEF
743 #define TOK_UIDENT TOK_DEFINE
745 #ifdef _WIN32
746 #define snprintf _snprintf
747 #define vsnprintf _vsnprintf
748 #ifndef __GNUC__
749 #define strtold (long double)strtod
750 #define strtof (float)strtod
751 #define strtoll (long long)strtol
752 #endif
753 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
754 || defined(__OpenBSD__)
755 /* currently incorrect */
756 long double strtold(const char *nptr, char **endptr)
758 return (long double)strtod(nptr, endptr);
760 float strtof(const char *nptr, char **endptr)
762 return (float)strtod(nptr, endptr);
764 #else
765 /* XXX: need to define this to use them in non ISOC99 context */
766 extern float strtof (const char *__nptr, char **__endptr);
767 extern long double strtold (const char *__nptr, char **__endptr);
768 #endif
770 static char *pstrcpy(char *buf, int buf_size, const char *s);
771 static char *pstrcat(char *buf, int buf_size, const char *s);
772 static char *tcc_basename(const char *name);
773 static char *tcc_fileextension (const char *p);
775 static void next(void);
776 static void next_nomacro(void);
777 static void parse_expr_type(CType *type);
778 static void expr_type(CType *type);
779 static void unary_type(CType *type);
780 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
781 int case_reg, int is_expr);
782 static int expr_const(void);
783 static void expr_eq(void);
784 static void gexpr(void);
785 static void gen_inline_functions(void);
786 static void decl(int l);
787 static void decl_initializer(CType *type, Section *sec, unsigned long c,
788 int first, int size_only);
789 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
790 int has_init, int v, int scope);
791 int gv(int rc);
792 void gv2(int rc1, int rc2);
793 void move_reg(int r, int s);
794 void save_regs(int n);
795 void save_reg(int r);
796 void vpop(void);
797 void vswap(void);
798 void vdup(void);
799 int get_reg(int rc);
800 int get_reg_ex(int rc,int rc2);
802 struct macro_level {
803 struct macro_level *prev;
804 int *p;
807 static void macro_subst(TokenString *tok_str, Sym **nested_list,
808 const int *macro_str, struct macro_level **can_read_stream);
809 void gen_op(int op);
810 void force_charshort_cast(int t);
811 static void gen_cast(CType *type);
812 void vstore(void);
813 static Sym *sym_find(int v);
814 static Sym *sym_push(int v, CType *type, int r, int c);
816 /* type handling */
817 static int type_size(CType *type, int *a);
818 static inline CType *pointed_type(CType *type);
819 static int pointed_size(CType *type);
820 static int lvalue_type(int t);
821 static int parse_btype(CType *type, AttributeDef *ad);
822 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
823 static int compare_types(CType *type1, CType *type2, int unqualified);
824 static int is_compatible_types(CType *type1, CType *type2);
825 static int is_compatible_parameter_types(CType *type1, CType *type2);
827 int ieee_finite(double d);
828 void error(const char *fmt, ...);
829 void vpushi(int v);
830 void vrott(int n);
831 void vnrott(int n);
832 void lexpand_nr(void);
833 static void vpush_global_sym(CType *type, int v);
834 void vset(CType *type, int r, int v);
835 void type_to_str(char *buf, int buf_size,
836 CType *type, const char *varstr);
837 char *get_tok_str(int v, CValue *cv);
838 static Sym *get_sym_ref(CType *type, Section *sec,
839 unsigned long offset, unsigned long size);
840 static Sym *external_global_sym(int v, CType *type, int r);
842 /* section generation */
843 static void section_realloc(Section *sec, unsigned long new_size);
844 static void *section_ptr_add(Section *sec, unsigned long size);
845 static void put_extern_sym(Sym *sym, Section *section,
846 unsigned long value, unsigned long size);
847 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
848 static int put_elf_str(Section *s, const char *sym);
849 static int put_elf_sym(Section *s,
850 unsigned long value, unsigned long size,
851 int info, int other, int shndx, const char *name);
852 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
853 int info, int other, int sh_num, const char *name);
854 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
855 int type, int symbol);
856 static void put_stabs(const char *str, int type, int other, int desc,
857 unsigned long value);
858 static void put_stabs_r(const char *str, int type, int other, int desc,
859 unsigned long value, Section *sec, int sym_index);
860 static void put_stabn(int type, int other, int desc, int value);
861 static void put_stabd(int type, int other, int desc);
862 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
864 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
865 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
866 #define AFF_PREPROCESS 0x0004 /* preprocess file */
867 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
869 /* tcccoff.c */
870 int tcc_output_coff(TCCState *s1, FILE *f);
872 /* tccpe.c */
873 void *resolve_sym(TCCState *s1, const char *sym, int type);
874 int pe_load_def_file(struct TCCState *s1, int fd);
875 int pe_test_res_file(void *v, int size);
876 int pe_load_res_file(struct TCCState *s1, int fd);
877 void pe_add_runtime(struct TCCState *s1);
878 void pe_guess_outfile(char *objfilename, int output_type);
879 int pe_output_file(struct TCCState *s1, const char *filename);
881 /* tccasm.c */
883 #ifdef CONFIG_TCC_ASM
885 typedef struct ExprValue {
886 uint32_t v;
887 Sym *sym;
888 } ExprValue;
890 #define MAX_ASM_OPERANDS 30
892 typedef struct ASMOperand {
893 int id; /* GCC 3 optionnal identifier (0 if number only supported */
894 char *constraint;
895 char asm_str[16]; /* computed asm string for operand */
896 SValue *vt; /* C value of the expression */
897 int ref_index; /* if >= 0, gives reference to a output constraint */
898 int input_index; /* if >= 0, gives reference to an input constraint */
899 int priority; /* priority, used to assign registers */
900 int reg; /* if >= 0, register number used for this operand */
901 int is_llong; /* true if double register value */
902 int is_memory; /* true if memory operand */
903 int is_rw; /* for '+' modifier */
904 } ASMOperand;
906 static void asm_expr(TCCState *s1, ExprValue *pe);
907 static int asm_int_expr(TCCState *s1);
908 static int find_constraint(ASMOperand *operands, int nb_operands,
909 const char *name, const char **pp);
911 static int tcc_assemble(TCCState *s1, int do_preprocess);
913 #endif
915 static void asm_instr(void);
916 static void asm_global_instr(void);
918 /* true if float/double/long double type */
919 static inline int is_float(int t)
921 int bt;
922 bt = t & VT_BTYPE;
923 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
926 #ifdef TCC_TARGET_I386
927 #include "i386-gen.c"
928 #endif
930 #ifdef TCC_TARGET_ARM
931 #include "arm-gen.c"
932 #endif
934 #ifdef TCC_TARGET_C67
935 #include "c67-gen.c"
936 #endif
938 #ifdef CONFIG_TCC_STATIC
940 #define RTLD_LAZY 0x001
941 #define RTLD_NOW 0x002
942 #define RTLD_GLOBAL 0x100
943 #define RTLD_DEFAULT NULL
945 /* dummy function for profiling */
946 void *dlopen(const char *filename, int flag)
948 return NULL;
951 const char *dlerror(void)
953 return "error";
956 typedef struct TCCSyms {
957 char *str;
958 void *ptr;
959 } TCCSyms;
961 #define TCCSYM(a) { #a, &a, },
963 /* add the symbol you want here if no dynamic linking is done */
964 static TCCSyms tcc_syms[] = {
965 #if !defined(CONFIG_TCCBOOT)
966 TCCSYM(printf)
967 TCCSYM(fprintf)
968 TCCSYM(fopen)
969 TCCSYM(fclose)
970 #endif
971 { NULL, NULL },
974 void *resolve_sym(TCCState *s1, const char *symbol, int type)
976 TCCSyms *p;
977 p = tcc_syms;
978 while (p->str != NULL) {
979 if (!strcmp(p->str, symbol))
980 return p->ptr;
981 p++;
983 return NULL;
986 #elif !defined(_WIN32)
988 #include <dlfcn.h>
990 void *resolve_sym(TCCState *s1, const char *sym, int type)
992 return dlsym(RTLD_DEFAULT, sym);
995 #endif
997 /********************************************************/
999 /* we use our own 'finite' function to avoid potential problems with
1000 non standard math libs */
1001 /* XXX: endianness dependent */
1002 int ieee_finite(double d)
1004 int *p = (int *)&d;
1005 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
1008 /* copy a string and truncate it. */
1009 static char *pstrcpy(char *buf, int buf_size, const char *s)
1011 char *q, *q_end;
1012 int c;
1014 if (buf_size > 0) {
1015 q = buf;
1016 q_end = buf + buf_size - 1;
1017 while (q < q_end) {
1018 c = *s++;
1019 if (c == '\0')
1020 break;
1021 *q++ = c;
1023 *q = '\0';
1025 return buf;
1028 /* strcat and truncate. */
1029 static char *pstrcat(char *buf, int buf_size, const char *s)
1031 int len;
1032 len = strlen(buf);
1033 if (len < buf_size)
1034 pstrcpy(buf + len, buf_size - len, s);
1035 return buf;
1038 static int strstart(const char *str, const char *val, const char **ptr)
1040 const char *p, *q;
1041 p = str;
1042 q = val;
1043 while (*q != '\0') {
1044 if (*p != *q)
1045 return 0;
1046 p++;
1047 q++;
1049 if (ptr)
1050 *ptr = p;
1051 return 1;
1054 /* extract the basename of a file */
1055 static char *tcc_basename(const char *name)
1057 char *p = strchr(name, 0);
1058 while (p > name
1059 && p[-1] != '/'
1060 #ifdef _WIN32
1061 && p[-1] != '\\'
1062 #endif
1064 --p;
1065 return p;
1068 static char *tcc_fileextension (const char *name)
1070 char *b = tcc_basename(name);
1071 char *e = strrchr(b, '.');
1072 return e ? e : strchr(b, 0);
1075 #ifdef _WIN32
1076 char *normalize_slashes(char *path)
1078 char *p;
1079 for (p = path; *p; ++p)
1080 if (*p == '\\')
1081 *p = '/';
1082 return path;
1085 char *w32_tcc_lib_path(void)
1087 /* on win32, we suppose the lib and includes are at the location
1088 of 'tcc.exe' */
1089 char path[1024], *p;
1090 GetModuleFileNameA(NULL, path, sizeof path);
1091 p = tcc_basename(normalize_slashes(strlwr(path)));
1092 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
1093 p -= 5;
1094 else if (p > path)
1095 p--;
1096 *p = 0;
1097 return strdup(path);
1099 #endif
1101 void set_pages_executable(void *ptr, unsigned long length)
1103 #ifdef _WIN32
1104 unsigned long old_protect;
1105 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
1106 #else
1107 unsigned long start, end;
1108 start = (unsigned long)ptr & ~(PAGESIZE - 1);
1109 end = (unsigned long)ptr + length;
1110 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
1111 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
1112 #endif
1115 /* memory management */
1116 #ifdef MEM_DEBUG
1117 int mem_cur_size;
1118 int mem_max_size;
1119 #endif
1121 static inline void tcc_free(void *ptr)
1123 #ifdef MEM_DEBUG
1124 mem_cur_size -= malloc_usable_size(ptr);
1125 #endif
1126 free(ptr);
1129 static void *tcc_malloc(unsigned long size)
1131 void *ptr;
1132 ptr = malloc(size);
1133 if (!ptr && size)
1134 error("memory full");
1135 #ifdef MEM_DEBUG
1136 mem_cur_size += malloc_usable_size(ptr);
1137 if (mem_cur_size > mem_max_size)
1138 mem_max_size = mem_cur_size;
1139 #endif
1140 return ptr;
1143 static void *tcc_mallocz(unsigned long size)
1145 void *ptr;
1146 ptr = tcc_malloc(size);
1147 memset(ptr, 0, size);
1148 return ptr;
1151 static inline void *tcc_realloc(void *ptr, unsigned long size)
1153 void *ptr1;
1154 #ifdef MEM_DEBUG
1155 mem_cur_size -= malloc_usable_size(ptr);
1156 #endif
1157 ptr1 = realloc(ptr, size);
1158 #ifdef MEM_DEBUG
1159 /* NOTE: count not correct if alloc error, but not critical */
1160 mem_cur_size += malloc_usable_size(ptr1);
1161 if (mem_cur_size > mem_max_size)
1162 mem_max_size = mem_cur_size;
1163 #endif
1164 return ptr1;
1167 static char *tcc_strdup(const char *str)
1169 char *ptr;
1170 ptr = tcc_malloc(strlen(str) + 1);
1171 strcpy(ptr, str);
1172 return ptr;
1175 #define free(p) use_tcc_free(p)
1176 #define malloc(s) use_tcc_malloc(s)
1177 #define realloc(p, s) use_tcc_realloc(p, s)
1179 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1181 int nb, nb_alloc;
1182 void **pp;
1184 nb = *nb_ptr;
1185 pp = *ptab;
1186 /* every power of two we double array size */
1187 if ((nb & (nb - 1)) == 0) {
1188 if (!nb)
1189 nb_alloc = 1;
1190 else
1191 nb_alloc = nb * 2;
1192 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1193 if (!pp)
1194 error("memory full");
1195 *ptab = pp;
1197 pp[nb++] = data;
1198 *nb_ptr = nb;
1201 static void dynarray_reset(void *pp, int *n)
1203 void **p;
1204 for (p = *(void***)pp; *n; ++p, --*n)
1205 if (*p)
1206 tcc_free(*p);
1207 tcc_free(*(void**)pp);
1208 *(void**)pp = NULL;
1211 /* symbol allocator */
1212 static Sym *__sym_malloc(void)
1214 Sym *sym_pool, *sym, *last_sym;
1215 int i;
1217 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1219 last_sym = sym_free_first;
1220 sym = sym_pool;
1221 for(i = 0; i < SYM_POOL_NB; i++) {
1222 sym->next = last_sym;
1223 last_sym = sym;
1224 sym++;
1226 sym_free_first = last_sym;
1227 return last_sym;
1230 static inline Sym *sym_malloc(void)
1232 Sym *sym;
1233 sym = sym_free_first;
1234 if (!sym)
1235 sym = __sym_malloc();
1236 sym_free_first = sym->next;
1237 return sym;
1240 static inline void sym_free(Sym *sym)
1242 sym->next = sym_free_first;
1243 sym_free_first = sym;
1246 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1248 Section *sec;
1250 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1251 strcpy(sec->name, name);
1252 sec->sh_type = sh_type;
1253 sec->sh_flags = sh_flags;
1254 switch(sh_type) {
1255 case SHT_HASH:
1256 case SHT_REL:
1257 case SHT_DYNSYM:
1258 case SHT_SYMTAB:
1259 case SHT_DYNAMIC:
1260 sec->sh_addralign = 4;
1261 break;
1262 case SHT_STRTAB:
1263 sec->sh_addralign = 1;
1264 break;
1265 default:
1266 sec->sh_addralign = 32; /* default conservative alignment */
1267 break;
1270 /* only add section if not private */
1271 if (!(sh_flags & SHF_PRIVATE)) {
1272 sec->sh_num = s1->nb_sections;
1273 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1275 return sec;
1278 static void free_section(Section *s)
1280 tcc_free(s->data);
1281 tcc_free(s);
1284 /* realloc section and set its content to zero */
1285 static void section_realloc(Section *sec, unsigned long new_size)
1287 unsigned long size;
1288 unsigned char *data;
1290 size = sec->data_allocated;
1291 if (size == 0)
1292 size = 1;
1293 while (size < new_size)
1294 size = size * 2;
1295 data = tcc_realloc(sec->data, size);
1296 if (!data)
1297 error("memory full");
1298 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1299 sec->data = data;
1300 sec->data_allocated = size;
1303 /* reserve at least 'size' bytes in section 'sec' from
1304 sec->data_offset. */
1305 static void *section_ptr_add(Section *sec, unsigned long size)
1307 unsigned long offset, offset1;
1309 offset = sec->data_offset;
1310 offset1 = offset + size;
1311 if (offset1 > sec->data_allocated)
1312 section_realloc(sec, offset1);
1313 sec->data_offset = offset1;
1314 return sec->data + offset;
1317 /* return a reference to a section, and create it if it does not
1318 exists */
1319 Section *find_section(TCCState *s1, const char *name)
1321 Section *sec;
1322 int i;
1323 for(i = 1; i < s1->nb_sections; i++) {
1324 sec = s1->sections[i];
1325 if (!strcmp(name, sec->name))
1326 return sec;
1328 /* sections are created as PROGBITS */
1329 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1332 #define SECTION_ABS ((void *)1)
1334 /* update sym->c so that it points to an external symbol in section
1335 'section' with value 'value' */
1336 static void put_extern_sym2(Sym *sym, Section *section,
1337 unsigned long value, unsigned long size,
1338 int can_add_underscore)
1340 int sym_type, sym_bind, sh_num, info, other, attr;
1341 Elf32_Sym *esym;
1342 const char *name;
1343 char buf1[256];
1345 if (section == NULL)
1346 sh_num = SHN_UNDEF;
1347 else if (section == SECTION_ABS)
1348 sh_num = SHN_ABS;
1349 else
1350 sh_num = section->sh_num;
1352 other = attr = 0;
1354 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1355 sym_type = STT_FUNC;
1356 #ifdef TCC_TARGET_PE
1357 if (sym->type.ref)
1358 attr = sym->type.ref->r;
1359 if (FUNC_EXPORT(attr))
1360 other |= 1;
1361 if (FUNC_CALL(attr) == FUNC_STDCALL)
1362 other |= 2;
1363 #endif
1364 } else {
1365 sym_type = STT_OBJECT;
1368 if (sym->type.t & VT_STATIC)
1369 sym_bind = STB_LOCAL;
1370 else
1371 sym_bind = STB_GLOBAL;
1373 if (!sym->c) {
1374 name = get_tok_str(sym->v, NULL);
1375 #ifdef CONFIG_TCC_BCHECK
1376 if (do_bounds_check) {
1377 char buf[32];
1379 /* XXX: avoid doing that for statics ? */
1380 /* if bound checking is activated, we change some function
1381 names by adding the "__bound" prefix */
1382 switch(sym->v) {
1383 #if 0
1384 /* XXX: we rely only on malloc hooks */
1385 case TOK_malloc:
1386 case TOK_free:
1387 case TOK_realloc:
1388 case TOK_memalign:
1389 case TOK_calloc:
1390 #endif
1391 case TOK_memcpy:
1392 case TOK_memmove:
1393 case TOK_memset:
1394 case TOK_strlen:
1395 case TOK_strcpy:
1396 case TOK__alloca:
1397 strcpy(buf, "__bound_");
1398 strcat(buf, name);
1399 name = buf;
1400 break;
1403 #endif
1405 #ifdef TCC_TARGET_PE
1406 if ((other & 2) && can_add_underscore) {
1407 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1408 name = buf1;
1409 } else
1410 #endif
1411 if (tcc_state->leading_underscore && can_add_underscore) {
1412 buf1[0] = '_';
1413 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1414 name = buf1;
1416 info = ELF32_ST_INFO(sym_bind, sym_type);
1417 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1418 } else {
1419 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1420 esym->st_value = value;
1421 esym->st_size = size;
1422 esym->st_shndx = sh_num;
1423 esym->st_other |= other;
1427 static void put_extern_sym(Sym *sym, Section *section,
1428 unsigned long value, unsigned long size)
1430 put_extern_sym2(sym, section, value, size, 1);
1433 /* add a new relocation entry to symbol 'sym' in section 's' */
1434 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1436 if (!sym->c)
1437 put_extern_sym(sym, NULL, 0, 0);
1438 /* now we can add ELF relocation info */
1439 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1442 static inline int isid(int c)
1444 return (c >= 'a' && c <= 'z') ||
1445 (c >= 'A' && c <= 'Z') ||
1446 c == '_';
1449 static inline int isnum(int c)
1451 return c >= '0' && c <= '9';
1454 static inline int isoct(int c)
1456 return c >= '0' && c <= '7';
1459 static inline int toup(int c)
1461 if (c >= 'a' && c <= 'z')
1462 return c - 'a' + 'A';
1463 else
1464 return c;
1467 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1469 int len;
1470 len = strlen(buf);
1471 vsnprintf(buf + len, buf_size - len, fmt, ap);
1474 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1476 va_list ap;
1477 va_start(ap, fmt);
1478 strcat_vprintf(buf, buf_size, fmt, ap);
1479 va_end(ap);
1482 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1484 char buf[2048];
1485 BufferedFile **f;
1487 buf[0] = '\0';
1488 if (file) {
1489 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1490 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1491 (*f)->filename, (*f)->line_num);
1492 if (file->line_num > 0) {
1493 strcat_printf(buf, sizeof(buf),
1494 "%s:%d: ", file->filename, file->line_num);
1495 } else {
1496 strcat_printf(buf, sizeof(buf),
1497 "%s: ", file->filename);
1499 } else {
1500 strcat_printf(buf, sizeof(buf),
1501 "tcc: ");
1503 if (is_warning)
1504 strcat_printf(buf, sizeof(buf), "warning: ");
1505 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1507 if (!s1->error_func) {
1508 /* default case: stderr */
1509 fprintf(stderr, "%s\n", buf);
1510 } else {
1511 s1->error_func(s1->error_opaque, buf);
1513 if (!is_warning || s1->warn_error)
1514 s1->nb_errors++;
1517 #ifdef LIBTCC
1518 void tcc_set_error_func(TCCState *s, void *error_opaque,
1519 void (*error_func)(void *opaque, const char *msg))
1521 s->error_opaque = error_opaque;
1522 s->error_func = error_func;
1524 #endif
1526 /* error without aborting current compilation */
1527 void error_noabort(const char *fmt, ...)
1529 TCCState *s1 = tcc_state;
1530 va_list ap;
1532 va_start(ap, fmt);
1533 error1(s1, 0, fmt, ap);
1534 va_end(ap);
1537 void error(const char *fmt, ...)
1539 TCCState *s1 = tcc_state;
1540 va_list ap;
1542 va_start(ap, fmt);
1543 error1(s1, 0, fmt, ap);
1544 va_end(ap);
1545 /* better than nothing: in some cases, we accept to handle errors */
1546 if (s1->error_set_jmp_enabled) {
1547 longjmp(s1->error_jmp_buf, 1);
1548 } else {
1549 /* XXX: eliminate this someday */
1550 exit(1);
1554 void expect(const char *msg)
1556 error("%s expected", msg);
1559 void warning(const char *fmt, ...)
1561 TCCState *s1 = tcc_state;
1562 va_list ap;
1564 if (s1->warn_none)
1565 return;
1567 va_start(ap, fmt);
1568 error1(s1, 1, fmt, ap);
1569 va_end(ap);
1572 void skip(int c)
1574 if (tok != c)
1575 error("'%c' expected", c);
1576 next();
1579 static void test_lvalue(void)
1581 if (!(vtop->r & VT_LVAL))
1582 expect("lvalue");
1585 /* allocate a new token */
1586 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1588 TokenSym *ts, **ptable;
1589 int i;
1591 if (tok_ident >= SYM_FIRST_ANOM)
1592 error("memory full");
1594 /* expand token table if needed */
1595 i = tok_ident - TOK_IDENT;
1596 if ((i % TOK_ALLOC_INCR) == 0) {
1597 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1598 if (!ptable)
1599 error("memory full");
1600 table_ident = ptable;
1603 ts = tcc_malloc(sizeof(TokenSym) + len);
1604 table_ident[i] = ts;
1605 ts->tok = tok_ident++;
1606 ts->sym_define = NULL;
1607 ts->sym_label = NULL;
1608 ts->sym_struct = NULL;
1609 ts->sym_identifier = NULL;
1610 ts->len = len;
1611 ts->hash_next = NULL;
1612 memcpy(ts->str, str, len);
1613 ts->str[len] = '\0';
1614 *pts = ts;
1615 return ts;
1618 #define TOK_HASH_INIT 1
1619 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1621 /* find a token and add it if not found */
1622 static TokenSym *tok_alloc(const char *str, int len)
1624 TokenSym *ts, **pts;
1625 int i;
1626 unsigned int h;
1628 h = TOK_HASH_INIT;
1629 for(i=0;i<len;i++)
1630 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1631 h &= (TOK_HASH_SIZE - 1);
1633 pts = &hash_ident[h];
1634 for(;;) {
1635 ts = *pts;
1636 if (!ts)
1637 break;
1638 if (ts->len == len && !memcmp(ts->str, str, len))
1639 return ts;
1640 pts = &(ts->hash_next);
1642 return tok_alloc_new(pts, str, len);
1645 /* CString handling */
1647 static void cstr_realloc(CString *cstr, int new_size)
1649 int size;
1650 void *data;
1652 size = cstr->size_allocated;
1653 if (size == 0)
1654 size = 8; /* no need to allocate a too small first string */
1655 while (size < new_size)
1656 size = size * 2;
1657 data = tcc_realloc(cstr->data_allocated, size);
1658 if (!data)
1659 error("memory full");
1660 cstr->data_allocated = data;
1661 cstr->size_allocated = size;
1662 cstr->data = data;
1665 /* add a byte */
1666 static inline void cstr_ccat(CString *cstr, int ch)
1668 int size;
1669 size = cstr->size + 1;
1670 if (size > cstr->size_allocated)
1671 cstr_realloc(cstr, size);
1672 ((unsigned char *)cstr->data)[size - 1] = ch;
1673 cstr->size = size;
1676 static void cstr_cat(CString *cstr, const char *str)
1678 int c;
1679 for(;;) {
1680 c = *str;
1681 if (c == '\0')
1682 break;
1683 cstr_ccat(cstr, c);
1684 str++;
1688 /* add a wide char */
1689 static void cstr_wccat(CString *cstr, int ch)
1691 int size;
1692 size = cstr->size + sizeof(nwchar_t);
1693 if (size > cstr->size_allocated)
1694 cstr_realloc(cstr, size);
1695 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
1696 cstr->size = size;
1699 static void cstr_new(CString *cstr)
1701 memset(cstr, 0, sizeof(CString));
1704 /* free string and reset it to NULL */
1705 static void cstr_free(CString *cstr)
1707 tcc_free(cstr->data_allocated);
1708 cstr_new(cstr);
1711 #define cstr_reset(cstr) cstr_free(cstr)
1713 /* XXX: unicode ? */
1714 static void add_char(CString *cstr, int c)
1716 if (c == '\'' || c == '\"' || c == '\\') {
1717 /* XXX: could be more precise if char or string */
1718 cstr_ccat(cstr, '\\');
1720 if (c >= 32 && c <= 126) {
1721 cstr_ccat(cstr, c);
1722 } else {
1723 cstr_ccat(cstr, '\\');
1724 if (c == '\n') {
1725 cstr_ccat(cstr, 'n');
1726 } else {
1727 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1728 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1729 cstr_ccat(cstr, '0' + (c & 7));
1734 /* XXX: buffer overflow */
1735 /* XXX: float tokens */
1736 char *get_tok_str(int v, CValue *cv)
1738 static char buf[STRING_MAX_SIZE + 1];
1739 static CString cstr_buf;
1740 CString *cstr;
1741 unsigned char *q;
1742 char *p;
1743 int i, len;
1745 /* NOTE: to go faster, we give a fixed buffer for small strings */
1746 cstr_reset(&cstr_buf);
1747 cstr_buf.data = buf;
1748 cstr_buf.size_allocated = sizeof(buf);
1749 p = buf;
1751 switch(v) {
1752 case TOK_CINT:
1753 case TOK_CUINT:
1754 /* XXX: not quite exact, but only useful for testing */
1755 sprintf(p, "%u", cv->ui);
1756 break;
1757 case TOK_CLLONG:
1758 case TOK_CULLONG:
1759 /* XXX: not quite exact, but only useful for testing */
1760 sprintf(p, "%Lu", cv->ull);
1761 break;
1762 case TOK_CCHAR:
1763 case TOK_LCHAR:
1764 cstr_ccat(&cstr_buf, '\'');
1765 add_char(&cstr_buf, cv->i);
1766 cstr_ccat(&cstr_buf, '\'');
1767 cstr_ccat(&cstr_buf, '\0');
1768 break;
1769 case TOK_PPNUM:
1770 cstr = cv->cstr;
1771 len = cstr->size - 1;
1772 for(i=0;i<len;i++)
1773 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1774 cstr_ccat(&cstr_buf, '\0');
1775 break;
1776 case TOK_STR:
1777 case TOK_LSTR:
1778 cstr = cv->cstr;
1779 cstr_ccat(&cstr_buf, '\"');
1780 if (v == TOK_STR) {
1781 len = cstr->size - 1;
1782 for(i=0;i<len;i++)
1783 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1784 } else {
1785 len = (cstr->size / sizeof(nwchar_t)) - 1;
1786 for(i=0;i<len;i++)
1787 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1789 cstr_ccat(&cstr_buf, '\"');
1790 cstr_ccat(&cstr_buf, '\0');
1791 break;
1792 case TOK_LT:
1793 v = '<';
1794 goto addv;
1795 case TOK_GT:
1796 v = '>';
1797 goto addv;
1798 case TOK_DOTS:
1799 return strcpy(p, "...");
1800 case TOK_A_SHL:
1801 return strcpy(p, "<<=");
1802 case TOK_A_SAR:
1803 return strcpy(p, ">>=");
1804 default:
1805 if (v < TOK_IDENT) {
1806 /* search in two bytes table */
1807 q = tok_two_chars;
1808 while (*q) {
1809 if (q[2] == v) {
1810 *p++ = q[0];
1811 *p++ = q[1];
1812 *p = '\0';
1813 return buf;
1815 q += 3;
1817 addv:
1818 *p++ = v;
1819 *p = '\0';
1820 } else if (v < tok_ident) {
1821 return table_ident[v - TOK_IDENT]->str;
1822 } else if (v >= SYM_FIRST_ANOM) {
1823 /* special name for anonymous symbol */
1824 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1825 } else {
1826 /* should never happen */
1827 return NULL;
1829 break;
1831 return cstr_buf.data;
1834 /* push, without hashing */
1835 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1837 Sym *s;
1838 s = sym_malloc();
1839 s->v = v;
1840 s->type.t = t;
1841 s->c = c;
1842 s->next = NULL;
1843 /* add in stack */
1844 s->prev = *ps;
1845 *ps = s;
1846 return s;
1849 /* find a symbol and return its associated structure. 's' is the top
1850 of the symbol stack */
1851 static Sym *sym_find2(Sym *s, int v)
1853 while (s) {
1854 if (s->v == v)
1855 return s;
1856 s = s->prev;
1858 return NULL;
1861 /* structure lookup */
1862 static inline Sym *struct_find(int v)
1864 v -= TOK_IDENT;
1865 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1866 return NULL;
1867 return table_ident[v]->sym_struct;
1870 /* find an identifier */
1871 static inline Sym *sym_find(int v)
1873 v -= TOK_IDENT;
1874 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1875 return NULL;
1876 return table_ident[v]->sym_identifier;
1879 /* push a given symbol on the symbol stack */
1880 static Sym *sym_push(int v, CType *type, int r, int c)
1882 Sym *s, **ps;
1883 TokenSym *ts;
1885 if (local_stack)
1886 ps = &local_stack;
1887 else
1888 ps = &global_stack;
1889 s = sym_push2(ps, v, type->t, c);
1890 s->type.ref = type->ref;
1891 s->r = r;
1892 /* don't record fields or anonymous symbols */
1893 /* XXX: simplify */
1894 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1895 /* record symbol in token array */
1896 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1897 if (v & SYM_STRUCT)
1898 ps = &ts->sym_struct;
1899 else
1900 ps = &ts->sym_identifier;
1901 s->prev_tok = *ps;
1902 *ps = s;
1904 return s;
1907 /* push a global identifier */
1908 static Sym *global_identifier_push(int v, int t, int c)
1910 Sym *s, **ps;
1911 s = sym_push2(&global_stack, v, t, c);
1912 /* don't record anonymous symbol */
1913 if (v < SYM_FIRST_ANOM) {
1914 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1915 /* modify the top most local identifier, so that
1916 sym_identifier will point to 's' when popped */
1917 while (*ps != NULL)
1918 ps = &(*ps)->prev_tok;
1919 s->prev_tok = NULL;
1920 *ps = s;
1922 return s;
1925 /* pop symbols until top reaches 'b' */
1926 static void sym_pop(Sym **ptop, Sym *b)
1928 Sym *s, *ss, **ps;
1929 TokenSym *ts;
1930 int v;
1932 s = *ptop;
1933 while(s != b) {
1934 ss = s->prev;
1935 v = s->v;
1936 /* remove symbol in token array */
1937 /* XXX: simplify */
1938 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1939 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1940 if (v & SYM_STRUCT)
1941 ps = &ts->sym_struct;
1942 else
1943 ps = &ts->sym_identifier;
1944 *ps = s->prev_tok;
1946 sym_free(s);
1947 s = ss;
1949 *ptop = b;
1952 /* I/O layer */
1954 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1956 int fd;
1957 BufferedFile *bf;
1959 if (strcmp(filename, "-") == 0)
1960 fd = 0, filename = "stdin";
1961 else
1962 fd = open(filename, O_RDONLY | O_BINARY);
1963 if (fd < 0)
1964 return NULL;
1965 bf = tcc_malloc(sizeof(BufferedFile));
1966 bf->fd = fd;
1967 bf->buf_ptr = bf->buffer;
1968 bf->buf_end = bf->buffer;
1969 bf->buffer[0] = CH_EOB; /* put eob symbol */
1970 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1971 #ifdef _WIN32
1972 normalize_slashes(bf->filename);
1973 #endif
1974 bf->line_num = 1;
1975 bf->ifndef_macro = 0;
1976 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1977 // printf("opening '%s'\n", filename);
1978 return bf;
1981 void tcc_close(BufferedFile *bf)
1983 total_lines += bf->line_num;
1984 close(bf->fd);
1985 tcc_free(bf);
1988 /* fill input buffer and peek next char */
1989 static int tcc_peekc_slow(BufferedFile *bf)
1991 int len;
1992 /* only tries to read if really end of buffer */
1993 if (bf->buf_ptr >= bf->buf_end) {
1994 if (bf->fd != -1) {
1995 #if defined(PARSE_DEBUG)
1996 len = 8;
1997 #else
1998 len = IO_BUF_SIZE;
1999 #endif
2000 len = read(bf->fd, bf->buffer, len);
2001 if (len < 0)
2002 len = 0;
2003 } else {
2004 len = 0;
2006 total_bytes += len;
2007 bf->buf_ptr = bf->buffer;
2008 bf->buf_end = bf->buffer + len;
2009 *bf->buf_end = CH_EOB;
2011 if (bf->buf_ptr < bf->buf_end) {
2012 return bf->buf_ptr[0];
2013 } else {
2014 bf->buf_ptr = bf->buf_end;
2015 return CH_EOF;
2019 /* return the current character, handling end of block if necessary
2020 (but not stray) */
2021 static int handle_eob(void)
2023 return tcc_peekc_slow(file);
2026 /* read next char from current input file and handle end of input buffer */
2027 static inline void inp(void)
2029 ch = *(++(file->buf_ptr));
2030 /* end of buffer/file handling */
2031 if (ch == CH_EOB)
2032 ch = handle_eob();
2035 /* handle '\[\r]\n' */
2036 static int handle_stray_noerror(void)
2038 while (ch == '\\') {
2039 inp();
2040 if (ch == '\n') {
2041 file->line_num++;
2042 inp();
2043 } else if (ch == '\r') {
2044 inp();
2045 if (ch != '\n')
2046 goto fail;
2047 file->line_num++;
2048 inp();
2049 } else {
2050 fail:
2051 return 1;
2054 return 0;
2057 static void handle_stray(void)
2059 if (handle_stray_noerror())
2060 error("stray '\\' in program");
2063 /* skip the stray and handle the \\n case. Output an error if
2064 incorrect char after the stray */
2065 static int handle_stray1(uint8_t *p)
2067 int c;
2069 if (p >= file->buf_end) {
2070 file->buf_ptr = p;
2071 c = handle_eob();
2072 p = file->buf_ptr;
2073 if (c == '\\')
2074 goto parse_stray;
2075 } else {
2076 parse_stray:
2077 file->buf_ptr = p;
2078 ch = *p;
2079 handle_stray();
2080 p = file->buf_ptr;
2081 c = *p;
2083 return c;
2086 /* handle just the EOB case, but not stray */
2087 #define PEEKC_EOB(c, p)\
2089 p++;\
2090 c = *p;\
2091 if (c == '\\') {\
2092 file->buf_ptr = p;\
2093 c = handle_eob();\
2094 p = file->buf_ptr;\
2098 /* handle the complicated stray case */
2099 #define PEEKC(c, p)\
2101 p++;\
2102 c = *p;\
2103 if (c == '\\') {\
2104 c = handle_stray1(p);\
2105 p = file->buf_ptr;\
2109 /* input with '\[\r]\n' handling. Note that this function cannot
2110 handle other characters after '\', so you cannot call it inside
2111 strings or comments */
2112 static void minp(void)
2114 inp();
2115 if (ch == '\\')
2116 handle_stray();
2120 /* single line C++ comments */
2121 static uint8_t *parse_line_comment(uint8_t *p)
2123 int c;
2125 p++;
2126 for(;;) {
2127 c = *p;
2128 redo:
2129 if (c == '\n' || c == CH_EOF) {
2130 break;
2131 } else if (c == '\\') {
2132 file->buf_ptr = p;
2133 c = handle_eob();
2134 p = file->buf_ptr;
2135 if (c == '\\') {
2136 PEEKC_EOB(c, p);
2137 if (c == '\n') {
2138 file->line_num++;
2139 PEEKC_EOB(c, p);
2140 } else if (c == '\r') {
2141 PEEKC_EOB(c, p);
2142 if (c == '\n') {
2143 file->line_num++;
2144 PEEKC_EOB(c, p);
2147 } else {
2148 goto redo;
2150 } else {
2151 p++;
2154 return p;
2157 /* C comments */
2158 static uint8_t *parse_comment(uint8_t *p)
2160 int c;
2162 p++;
2163 for(;;) {
2164 /* fast skip loop */
2165 for(;;) {
2166 c = *p;
2167 if (c == '\n' || c == '*' || c == '\\')
2168 break;
2169 p++;
2170 c = *p;
2171 if (c == '\n' || c == '*' || c == '\\')
2172 break;
2173 p++;
2175 /* now we can handle all the cases */
2176 if (c == '\n') {
2177 file->line_num++;
2178 p++;
2179 } else if (c == '*') {
2180 p++;
2181 for(;;) {
2182 c = *p;
2183 if (c == '*') {
2184 p++;
2185 } else if (c == '/') {
2186 goto end_of_comment;
2187 } else if (c == '\\') {
2188 file->buf_ptr = p;
2189 c = handle_eob();
2190 p = file->buf_ptr;
2191 if (c == '\\') {
2192 /* skip '\[\r]\n', otherwise just skip the stray */
2193 while (c == '\\') {
2194 PEEKC_EOB(c, p);
2195 if (c == '\n') {
2196 file->line_num++;
2197 PEEKC_EOB(c, p);
2198 } else if (c == '\r') {
2199 PEEKC_EOB(c, p);
2200 if (c == '\n') {
2201 file->line_num++;
2202 PEEKC_EOB(c, p);
2204 } else {
2205 goto after_star;
2209 } else {
2210 break;
2213 after_star: ;
2214 } else {
2215 /* stray, eob or eof */
2216 file->buf_ptr = p;
2217 c = handle_eob();
2218 p = file->buf_ptr;
2219 if (c == CH_EOF) {
2220 error("unexpected end of file in comment");
2221 } else if (c == '\\') {
2222 p++;
2226 end_of_comment:
2227 p++;
2228 return p;
2231 #define cinp minp
2233 /* space exlcuding newline */
2234 static inline int is_space(int ch)
2236 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2239 static inline void skip_spaces(void)
2241 while (is_space(ch))
2242 cinp();
2245 /* parse a string without interpreting escapes */
2246 static uint8_t *parse_pp_string(uint8_t *p,
2247 int sep, CString *str)
2249 int c;
2250 p++;
2251 for(;;) {
2252 c = *p;
2253 if (c == sep) {
2254 break;
2255 } else if (c == '\\') {
2256 file->buf_ptr = p;
2257 c = handle_eob();
2258 p = file->buf_ptr;
2259 if (c == CH_EOF) {
2260 unterminated_string:
2261 /* XXX: indicate line number of start of string */
2262 error("missing terminating %c character", sep);
2263 } else if (c == '\\') {
2264 /* escape : just skip \[\r]\n */
2265 PEEKC_EOB(c, p);
2266 if (c == '\n') {
2267 file->line_num++;
2268 p++;
2269 } else if (c == '\r') {
2270 PEEKC_EOB(c, p);
2271 if (c != '\n')
2272 expect("'\n' after '\r'");
2273 file->line_num++;
2274 p++;
2275 } else if (c == CH_EOF) {
2276 goto unterminated_string;
2277 } else {
2278 if (str) {
2279 cstr_ccat(str, '\\');
2280 cstr_ccat(str, c);
2282 p++;
2285 } else if (c == '\n') {
2286 file->line_num++;
2287 goto add_char;
2288 } else if (c == '\r') {
2289 PEEKC_EOB(c, p);
2290 if (c != '\n') {
2291 if (str)
2292 cstr_ccat(str, '\r');
2293 } else {
2294 file->line_num++;
2295 goto add_char;
2297 } else {
2298 add_char:
2299 if (str)
2300 cstr_ccat(str, c);
2301 p++;
2304 p++;
2305 return p;
2308 /* skip block of text until #else, #elif or #endif. skip also pairs of
2309 #if/#endif */
2310 void preprocess_skip(void)
2312 int a, start_of_line, c, in_warn_or_error;
2313 uint8_t *p;
2315 p = file->buf_ptr;
2316 a = 0;
2317 redo_start:
2318 start_of_line = 1;
2319 in_warn_or_error = 0;
2320 for(;;) {
2321 redo_no_start:
2322 c = *p;
2323 switch(c) {
2324 case ' ':
2325 case '\t':
2326 case '\f':
2327 case '\v':
2328 case '\r':
2329 p++;
2330 goto redo_no_start;
2331 case '\n':
2332 file->line_num++;
2333 p++;
2334 goto redo_start;
2335 case '\\':
2336 file->buf_ptr = p;
2337 c = handle_eob();
2338 if (c == CH_EOF) {
2339 expect("#endif");
2340 } else if (c == '\\') {
2341 ch = file->buf_ptr[0];
2342 handle_stray_noerror();
2344 p = file->buf_ptr;
2345 goto redo_no_start;
2346 /* skip strings */
2347 case '\"':
2348 case '\'':
2349 if (in_warn_or_error)
2350 goto _default;
2351 p = parse_pp_string(p, c, NULL);
2352 break;
2353 /* skip comments */
2354 case '/':
2355 if (in_warn_or_error)
2356 goto _default;
2357 file->buf_ptr = p;
2358 ch = *p;
2359 minp();
2360 p = file->buf_ptr;
2361 if (ch == '*') {
2362 p = parse_comment(p);
2363 } else if (ch == '/') {
2364 p = parse_line_comment(p);
2366 break;
2367 case '#':
2368 p++;
2369 if (start_of_line) {
2370 file->buf_ptr = p;
2371 next_nomacro();
2372 p = file->buf_ptr;
2373 if (a == 0 &&
2374 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2375 goto the_end;
2376 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2377 a++;
2378 else if (tok == TOK_ENDIF)
2379 a--;
2380 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2381 in_warn_or_error = 1;
2383 break;
2384 _default:
2385 default:
2386 p++;
2387 break;
2389 start_of_line = 0;
2391 the_end: ;
2392 file->buf_ptr = p;
2395 /* ParseState handling */
2397 /* XXX: currently, no include file info is stored. Thus, we cannot display
2398 accurate messages if the function or data definition spans multiple
2399 files */
2401 /* save current parse state in 's' */
2402 void save_parse_state(ParseState *s)
2404 s->line_num = file->line_num;
2405 s->macro_ptr = macro_ptr;
2406 s->tok = tok;
2407 s->tokc = tokc;
2410 /* restore parse state from 's' */
2411 void restore_parse_state(ParseState *s)
2413 file->line_num = s->line_num;
2414 macro_ptr = s->macro_ptr;
2415 tok = s->tok;
2416 tokc = s->tokc;
2419 /* return the number of additional 'ints' necessary to store the
2420 token */
2421 static inline int tok_ext_size(int t)
2423 switch(t) {
2424 /* 4 bytes */
2425 case TOK_CINT:
2426 case TOK_CUINT:
2427 case TOK_CCHAR:
2428 case TOK_LCHAR:
2429 case TOK_CFLOAT:
2430 case TOK_LINENUM:
2431 return 1;
2432 case TOK_STR:
2433 case TOK_LSTR:
2434 case TOK_PPNUM:
2435 error("unsupported token");
2436 return 1;
2437 case TOK_CDOUBLE:
2438 case TOK_CLLONG:
2439 case TOK_CULLONG:
2440 return 2;
2441 case TOK_CLDOUBLE:
2442 return LDOUBLE_SIZE / 4;
2443 default:
2444 return 0;
2448 /* token string handling */
2450 static inline void tok_str_new(TokenString *s)
2452 s->str = NULL;
2453 s->len = 0;
2454 s->allocated_len = 0;
2455 s->last_line_num = -1;
2458 static void tok_str_free(int *str)
2460 tcc_free(str);
2463 static int *tok_str_realloc(TokenString *s)
2465 int *str, len;
2467 if (s->allocated_len == 0) {
2468 len = 8;
2469 } else {
2470 len = s->allocated_len * 2;
2472 str = tcc_realloc(s->str, len * sizeof(int));
2473 if (!str)
2474 error("memory full");
2475 s->allocated_len = len;
2476 s->str = str;
2477 return str;
2480 static void tok_str_add(TokenString *s, int t)
2482 int len, *str;
2484 len = s->len;
2485 str = s->str;
2486 if (len >= s->allocated_len)
2487 str = tok_str_realloc(s);
2488 str[len++] = t;
2489 s->len = len;
2492 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2494 int len, *str;
2496 len = s->len;
2497 str = s->str;
2499 /* allocate space for worst case */
2500 if (len + TOK_MAX_SIZE > s->allocated_len)
2501 str = tok_str_realloc(s);
2502 str[len++] = t;
2503 switch(t) {
2504 case TOK_CINT:
2505 case TOK_CUINT:
2506 case TOK_CCHAR:
2507 case TOK_LCHAR:
2508 case TOK_CFLOAT:
2509 case TOK_LINENUM:
2510 str[len++] = cv->tab[0];
2511 break;
2512 case TOK_PPNUM:
2513 case TOK_STR:
2514 case TOK_LSTR:
2516 int nb_words;
2517 CString *cstr;
2519 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2520 while ((len + nb_words) > s->allocated_len)
2521 str = tok_str_realloc(s);
2522 cstr = (CString *)(str + len);
2523 cstr->data = NULL;
2524 cstr->size = cv->cstr->size;
2525 cstr->data_allocated = NULL;
2526 cstr->size_allocated = cstr->size;
2527 memcpy((char *)cstr + sizeof(CString),
2528 cv->cstr->data, cstr->size);
2529 len += nb_words;
2531 break;
2532 case TOK_CDOUBLE:
2533 case TOK_CLLONG:
2534 case TOK_CULLONG:
2535 #if LDOUBLE_SIZE == 8
2536 case TOK_CLDOUBLE:
2537 #endif
2538 str[len++] = cv->tab[0];
2539 str[len++] = cv->tab[1];
2540 break;
2541 #if LDOUBLE_SIZE == 12
2542 case TOK_CLDOUBLE:
2543 str[len++] = cv->tab[0];
2544 str[len++] = cv->tab[1];
2545 str[len++] = cv->tab[2];
2546 #elif LDOUBLE_SIZE != 8
2547 #error add long double size support
2548 #endif
2549 break;
2550 default:
2551 break;
2553 s->len = len;
2556 /* add the current parse token in token string 's' */
2557 static void tok_str_add_tok(TokenString *s)
2559 CValue cval;
2561 /* save line number info */
2562 if (file->line_num != s->last_line_num) {
2563 s->last_line_num = file->line_num;
2564 cval.i = s->last_line_num;
2565 tok_str_add2(s, TOK_LINENUM, &cval);
2567 tok_str_add2(s, tok, &tokc);
2570 #if LDOUBLE_SIZE == 12
2571 #define LDOUBLE_GET(p, cv) \
2572 cv.tab[0] = p[0]; \
2573 cv.tab[1] = p[1]; \
2574 cv.tab[2] = p[2];
2575 #elif LDOUBLE_SIZE == 8
2576 #define LDOUBLE_GET(p, cv) \
2577 cv.tab[0] = p[0]; \
2578 cv.tab[1] = p[1];
2579 #else
2580 #error add long double size support
2581 #endif
2584 /* get a token from an integer array and increment pointer
2585 accordingly. we code it as a macro to avoid pointer aliasing. */
2586 #define TOK_GET(t, p, cv) \
2588 t = *p++; \
2589 switch(t) { \
2590 case TOK_CINT: \
2591 case TOK_CUINT: \
2592 case TOK_CCHAR: \
2593 case TOK_LCHAR: \
2594 case TOK_CFLOAT: \
2595 case TOK_LINENUM: \
2596 cv.tab[0] = *p++; \
2597 break; \
2598 case TOK_STR: \
2599 case TOK_LSTR: \
2600 case TOK_PPNUM: \
2601 cv.cstr = (CString *)p; \
2602 cv.cstr->data = (char *)p + sizeof(CString);\
2603 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2604 break; \
2605 case TOK_CDOUBLE: \
2606 case TOK_CLLONG: \
2607 case TOK_CULLONG: \
2608 cv.tab[0] = p[0]; \
2609 cv.tab[1] = p[1]; \
2610 p += 2; \
2611 break; \
2612 case TOK_CLDOUBLE: \
2613 LDOUBLE_GET(p, cv); \
2614 p += LDOUBLE_SIZE / 4; \
2615 break; \
2616 default: \
2617 break; \
2621 /* defines handling */
2622 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2624 Sym *s;
2626 s = sym_push2(&define_stack, v, macro_type, (int)str);
2627 s->next = first_arg;
2628 table_ident[v - TOK_IDENT]->sym_define = s;
2631 /* undefined a define symbol. Its name is just set to zero */
2632 static void define_undef(Sym *s)
2634 int v;
2635 v = s->v;
2636 if (v >= TOK_IDENT && v < tok_ident)
2637 table_ident[v - TOK_IDENT]->sym_define = NULL;
2638 s->v = 0;
2641 static inline Sym *define_find(int v)
2643 v -= TOK_IDENT;
2644 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2645 return NULL;
2646 return table_ident[v]->sym_define;
2649 /* free define stack until top reaches 'b' */
2650 static void free_defines(Sym *b)
2652 Sym *top, *top1;
2653 int v;
2655 top = define_stack;
2656 while (top != b) {
2657 top1 = top->prev;
2658 /* do not free args or predefined defines */
2659 if (top->c)
2660 tok_str_free((int *)top->c);
2661 v = top->v;
2662 if (v >= TOK_IDENT && v < tok_ident)
2663 table_ident[v - TOK_IDENT]->sym_define = NULL;
2664 sym_free(top);
2665 top = top1;
2667 define_stack = b;
2670 /* label lookup */
2671 static Sym *label_find(int v)
2673 v -= TOK_IDENT;
2674 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2675 return NULL;
2676 return table_ident[v]->sym_label;
2679 static Sym *label_push(Sym **ptop, int v, int flags)
2681 Sym *s, **ps;
2682 s = sym_push2(ptop, v, 0, 0);
2683 s->r = flags;
2684 ps = &table_ident[v - TOK_IDENT]->sym_label;
2685 if (ptop == &global_label_stack) {
2686 /* modify the top most local identifier, so that
2687 sym_identifier will point to 's' when popped */
2688 while (*ps != NULL)
2689 ps = &(*ps)->prev_tok;
2691 s->prev_tok = *ps;
2692 *ps = s;
2693 return s;
2696 /* pop labels until element last is reached. Look if any labels are
2697 undefined. Define symbols if '&&label' was used. */
2698 static void label_pop(Sym **ptop, Sym *slast)
2700 Sym *s, *s1;
2701 for(s = *ptop; s != slast; s = s1) {
2702 s1 = s->prev;
2703 if (s->r == LABEL_DECLARED) {
2704 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2705 } else if (s->r == LABEL_FORWARD) {
2706 error("label '%s' used but not defined",
2707 get_tok_str(s->v, NULL));
2708 } else {
2709 if (s->c) {
2710 /* define corresponding symbol. A size of
2711 1 is put. */
2712 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2715 /* remove label */
2716 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2717 sym_free(s);
2719 *ptop = slast;
2722 /* eval an expression for #if/#elif */
2723 static int expr_preprocess(void)
2725 int c, t;
2726 TokenString str;
2728 tok_str_new(&str);
2729 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2730 next(); /* do macro subst */
2731 if (tok == TOK_DEFINED) {
2732 next_nomacro();
2733 t = tok;
2734 if (t == '(')
2735 next_nomacro();
2736 c = define_find(tok) != 0;
2737 if (t == '(')
2738 next_nomacro();
2739 tok = TOK_CINT;
2740 tokc.i = c;
2741 } else if (tok >= TOK_IDENT) {
2742 /* if undefined macro */
2743 tok = TOK_CINT;
2744 tokc.i = 0;
2746 tok_str_add_tok(&str);
2748 tok_str_add(&str, -1); /* simulate end of file */
2749 tok_str_add(&str, 0);
2750 /* now evaluate C constant expression */
2751 macro_ptr = str.str;
2752 next();
2753 c = expr_const();
2754 macro_ptr = NULL;
2755 tok_str_free(str.str);
2756 return c != 0;
2759 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2760 static void tok_print(int *str)
2762 int t;
2763 CValue cval;
2765 while (1) {
2766 TOK_GET(t, str, cval);
2767 if (!t)
2768 break;
2769 printf(" %s", get_tok_str(t, &cval));
2771 printf("\n");
2773 #endif
2775 /* parse after #define */
2776 static void parse_define(void)
2778 Sym *s, *first, **ps;
2779 int v, t, varg, is_vaargs, c;
2780 TokenString str;
2782 v = tok;
2783 if (v < TOK_IDENT)
2784 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2785 /* XXX: should check if same macro (ANSI) */
2786 first = NULL;
2787 t = MACRO_OBJ;
2788 /* '(' must be just after macro definition for MACRO_FUNC */
2789 c = file->buf_ptr[0];
2790 if (c == '\\')
2791 c = handle_stray1(file->buf_ptr);
2792 if (c == '(') {
2793 next_nomacro();
2794 next_nomacro();
2795 ps = &first;
2796 while (tok != ')') {
2797 varg = tok;
2798 next_nomacro();
2799 is_vaargs = 0;
2800 if (varg == TOK_DOTS) {
2801 varg = TOK___VA_ARGS__;
2802 is_vaargs = 1;
2803 } else if (tok == TOK_DOTS && gnu_ext) {
2804 is_vaargs = 1;
2805 next_nomacro();
2807 if (varg < TOK_IDENT)
2808 error("badly punctuated parameter list");
2809 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2810 *ps = s;
2811 ps = &s->next;
2812 if (tok != ',')
2813 break;
2814 next_nomacro();
2816 t = MACRO_FUNC;
2818 tok_str_new(&str);
2819 next_nomacro();
2820 /* EOF testing necessary for '-D' handling */
2821 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2822 tok_str_add2(&str, tok, &tokc);
2823 next_nomacro();
2825 tok_str_add(&str, 0);
2826 #ifdef PP_DEBUG
2827 printf("define %s %d: ", get_tok_str(v, NULL), t);
2828 tok_print(str.str);
2829 #endif
2830 define_push(v, t, str.str, first);
2833 static inline int hash_cached_include(int type, const char *filename)
2835 const unsigned char *s;
2836 unsigned int h;
2838 h = TOK_HASH_INIT;
2839 h = TOK_HASH_FUNC(h, type);
2840 s = filename;
2841 while (*s) {
2842 h = TOK_HASH_FUNC(h, *s);
2843 s++;
2845 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2846 return h;
2849 /* XXX: use a token or a hash table to accelerate matching ? */
2850 static CachedInclude *search_cached_include(TCCState *s1,
2851 int type, const char *filename)
2853 CachedInclude *e;
2854 int i, h;
2855 h = hash_cached_include(type, filename);
2856 i = s1->cached_includes_hash[h];
2857 for(;;) {
2858 if (i == 0)
2859 break;
2860 e = s1->cached_includes[i - 1];
2861 if (e->type == type && !strcmp(e->filename, filename))
2862 return e;
2863 i = e->hash_next;
2865 return NULL;
2868 static inline void add_cached_include(TCCState *s1, int type,
2869 const char *filename, int ifndef_macro)
2871 CachedInclude *e;
2872 int h;
2874 if (search_cached_include(s1, type, filename))
2875 return;
2876 #ifdef INC_DEBUG
2877 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2878 #endif
2879 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2880 if (!e)
2881 return;
2882 e->type = type;
2883 strcpy(e->filename, filename);
2884 e->ifndef_macro = ifndef_macro;
2885 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2886 /* add in hash table */
2887 h = hash_cached_include(type, filename);
2888 e->hash_next = s1->cached_includes_hash[h];
2889 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2892 static void pragma_parse(TCCState *s1)
2894 int val;
2896 next();
2897 if (tok == TOK_pack) {
2899 This may be:
2900 #pragma pack(1) // set
2901 #pragma pack() // reset to default
2902 #pragma pack(push,1) // push & set
2903 #pragma pack(pop) // restore previous
2905 next();
2906 skip('(');
2907 if (tok == TOK_ASM_pop) {
2908 next();
2909 if (s1->pack_stack_ptr <= s1->pack_stack) {
2910 stk_error:
2911 error("out of pack stack");
2913 s1->pack_stack_ptr--;
2914 } else {
2915 val = 0;
2916 if (tok != ')') {
2917 if (tok == TOK_ASM_push) {
2918 next();
2919 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2920 goto stk_error;
2921 s1->pack_stack_ptr++;
2922 skip(',');
2924 if (tok != TOK_CINT) {
2925 pack_error:
2926 error("invalid pack pragma");
2928 val = tokc.i;
2929 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2930 goto pack_error;
2931 next();
2933 *s1->pack_stack_ptr = val;
2934 skip(')');
2939 /* is_bof is true if first non space token at beginning of file */
2940 static void preprocess(int is_bof)
2942 TCCState *s1 = tcc_state;
2943 int size, i, c, n, saved_parse_flags;
2944 char buf[1024], *q;
2945 char buf1[1024];
2946 BufferedFile *f;
2947 Sym *s;
2948 CachedInclude *e;
2950 saved_parse_flags = parse_flags;
2951 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2952 PARSE_FLAG_LINEFEED;
2953 next_nomacro();
2954 redo:
2955 switch(tok) {
2956 case TOK_DEFINE:
2957 next_nomacro();
2958 parse_define();
2959 break;
2960 case TOK_UNDEF:
2961 next_nomacro();
2962 s = define_find(tok);
2963 /* undefine symbol by putting an invalid name */
2964 if (s)
2965 define_undef(s);
2966 break;
2967 case TOK_INCLUDE:
2968 case TOK_INCLUDE_NEXT:
2969 ch = file->buf_ptr[0];
2970 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2971 skip_spaces();
2972 if (ch == '<') {
2973 c = '>';
2974 goto read_name;
2975 } else if (ch == '\"') {
2976 c = ch;
2977 read_name:
2978 inp();
2979 q = buf;
2980 while (ch != c && ch != '\n' && ch != CH_EOF) {
2981 if ((q - buf) < sizeof(buf) - 1)
2982 *q++ = ch;
2983 if (ch == '\\') {
2984 if (handle_stray_noerror() == 0)
2985 --q;
2986 } else
2987 inp();
2989 *q = '\0';
2990 minp();
2991 #if 0
2992 /* eat all spaces and comments after include */
2993 /* XXX: slightly incorrect */
2994 while (ch1 != '\n' && ch1 != CH_EOF)
2995 inp();
2996 #endif
2997 } else {
2998 /* computed #include : either we have only strings or
2999 we have anything enclosed in '<>' */
3000 next();
3001 buf[0] = '\0';
3002 if (tok == TOK_STR) {
3003 while (tok != TOK_LINEFEED) {
3004 if (tok != TOK_STR) {
3005 include_syntax:
3006 error("'#include' expects \"FILENAME\" or <FILENAME>");
3008 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3009 next();
3011 c = '\"';
3012 } else {
3013 int len;
3014 while (tok != TOK_LINEFEED) {
3015 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3016 next();
3018 len = strlen(buf);
3019 /* check syntax and remove '<>' */
3020 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3021 goto include_syntax;
3022 memmove(buf, buf + 1, len - 2);
3023 buf[len - 2] = '\0';
3024 c = '>';
3028 e = search_cached_include(s1, c, buf);
3029 if (e && define_find(e->ifndef_macro)) {
3030 /* no need to parse the include because the 'ifndef macro'
3031 is defined */
3032 #ifdef INC_DEBUG
3033 printf("%s: skipping %s\n", file->filename, buf);
3034 #endif
3035 } else {
3036 if (c == '\"') {
3037 /* first search in current dir if "header.h" */
3038 size = tcc_basename(file->filename) - file->filename;
3039 if (size > sizeof(buf1) - 1)
3040 size = sizeof(buf1) - 1;
3041 memcpy(buf1, file->filename, size);
3042 buf1[size] = '\0';
3043 pstrcat(buf1, sizeof(buf1), buf);
3044 f = tcc_open(s1, buf1);
3045 if (f) {
3046 if (tok == TOK_INCLUDE_NEXT)
3047 tok = TOK_INCLUDE;
3048 else
3049 goto found;
3052 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3053 error("#include recursion too deep");
3054 /* now search in all the include paths */
3055 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3056 for(i = 0; i < n; i++) {
3057 const char *path;
3058 if (i < s1->nb_include_paths)
3059 path = s1->include_paths[i];
3060 else
3061 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3062 pstrcpy(buf1, sizeof(buf1), path);
3063 pstrcat(buf1, sizeof(buf1), "/");
3064 pstrcat(buf1, sizeof(buf1), buf);
3065 f = tcc_open(s1, buf1);
3066 if (f) {
3067 if (tok == TOK_INCLUDE_NEXT)
3068 tok = TOK_INCLUDE;
3069 else
3070 goto found;
3073 error("include file '%s' not found", buf);
3074 f = NULL;
3075 found:
3076 #ifdef INC_DEBUG
3077 printf("%s: including %s\n", file->filename, buf1);
3078 #endif
3079 f->inc_type = c;
3080 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3081 /* push current file in stack */
3082 /* XXX: fix current line init */
3083 *s1->include_stack_ptr++ = file;
3084 file = f;
3085 /* add include file debug info */
3086 if (do_debug) {
3087 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3089 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3090 ch = file->buf_ptr[0];
3091 goto the_end;
3093 break;
3094 case TOK_IFNDEF:
3095 c = 1;
3096 goto do_ifdef;
3097 case TOK_IF:
3098 c = expr_preprocess();
3099 goto do_if;
3100 case TOK_IFDEF:
3101 c = 0;
3102 do_ifdef:
3103 next_nomacro();
3104 if (tok < TOK_IDENT)
3105 error("invalid argument for '#if%sdef'", c ? "n" : "");
3106 if (is_bof) {
3107 if (c) {
3108 #ifdef INC_DEBUG
3109 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3110 #endif
3111 file->ifndef_macro = tok;
3114 c = (define_find(tok) != 0) ^ c;
3115 do_if:
3116 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3117 error("memory full");
3118 *s1->ifdef_stack_ptr++ = c;
3119 goto test_skip;
3120 case TOK_ELSE:
3121 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3122 error("#else without matching #if");
3123 if (s1->ifdef_stack_ptr[-1] & 2)
3124 error("#else after #else");
3125 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3126 goto test_skip;
3127 case TOK_ELIF:
3128 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3129 error("#elif without matching #if");
3130 c = s1->ifdef_stack_ptr[-1];
3131 if (c > 1)
3132 error("#elif after #else");
3133 /* last #if/#elif expression was true: we skip */
3134 if (c == 1)
3135 goto skip;
3136 c = expr_preprocess();
3137 s1->ifdef_stack_ptr[-1] = c;
3138 test_skip:
3139 if (!(c & 1)) {
3140 skip:
3141 preprocess_skip();
3142 is_bof = 0;
3143 goto redo;
3145 break;
3146 case TOK_ENDIF:
3147 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3148 error("#endif without matching #if");
3149 s1->ifdef_stack_ptr--;
3150 /* '#ifndef macro' was at the start of file. Now we check if
3151 an '#endif' is exactly at the end of file */
3152 if (file->ifndef_macro &&
3153 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3154 file->ifndef_macro_saved = file->ifndef_macro;
3155 /* need to set to zero to avoid false matches if another
3156 #ifndef at middle of file */
3157 file->ifndef_macro = 0;
3158 while (tok != TOK_LINEFEED)
3159 next_nomacro();
3160 tok_flags |= TOK_FLAG_ENDIF;
3161 goto the_end;
3163 break;
3164 case TOK_LINE:
3165 next();
3166 if (tok != TOK_CINT)
3167 error("#line");
3168 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3169 next();
3170 if (tok != TOK_LINEFEED) {
3171 if (tok != TOK_STR)
3172 error("#line");
3173 pstrcpy(file->filename, sizeof(file->filename),
3174 (char *)tokc.cstr->data);
3176 break;
3177 case TOK_ERROR:
3178 case TOK_WARNING:
3179 c = tok;
3180 ch = file->buf_ptr[0];
3181 skip_spaces();
3182 q = buf;
3183 while (ch != '\n' && ch != CH_EOF) {
3184 if ((q - buf) < sizeof(buf) - 1)
3185 *q++ = ch;
3186 if (ch == '\\') {
3187 if (handle_stray_noerror() == 0)
3188 --q;
3189 } else
3190 inp();
3192 *q = '\0';
3193 if (c == TOK_ERROR)
3194 error("#error %s", buf);
3195 else
3196 warning("#warning %s", buf);
3197 break;
3198 case TOK_PRAGMA:
3199 pragma_parse(s1);
3200 break;
3201 default:
3202 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3203 /* '!' is ignored to allow C scripts. numbers are ignored
3204 to emulate cpp behaviour */
3205 } else {
3206 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3207 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3209 break;
3211 /* ignore other preprocess commands or #! for C scripts */
3212 while (tok != TOK_LINEFEED)
3213 next_nomacro();
3214 the_end:
3215 parse_flags = saved_parse_flags;
3218 /* evaluate escape codes in a string. */
3219 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3221 int c, n;
3222 const uint8_t *p;
3224 p = buf;
3225 for(;;) {
3226 c = *p;
3227 if (c == '\0')
3228 break;
3229 if (c == '\\') {
3230 p++;
3231 /* escape */
3232 c = *p;
3233 switch(c) {
3234 case '0': case '1': case '2': case '3':
3235 case '4': case '5': case '6': case '7':
3236 /* at most three octal digits */
3237 n = c - '0';
3238 p++;
3239 c = *p;
3240 if (isoct(c)) {
3241 n = n * 8 + c - '0';
3242 p++;
3243 c = *p;
3244 if (isoct(c)) {
3245 n = n * 8 + c - '0';
3246 p++;
3249 c = n;
3250 goto add_char_nonext;
3251 case 'x':
3252 case 'u':
3253 case 'U':
3254 p++;
3255 n = 0;
3256 for(;;) {
3257 c = *p;
3258 if (c >= 'a' && c <= 'f')
3259 c = c - 'a' + 10;
3260 else if (c >= 'A' && c <= 'F')
3261 c = c - 'A' + 10;
3262 else if (isnum(c))
3263 c = c - '0';
3264 else
3265 break;
3266 n = n * 16 + c;
3267 p++;
3269 c = n;
3270 goto add_char_nonext;
3271 case 'a':
3272 c = '\a';
3273 break;
3274 case 'b':
3275 c = '\b';
3276 break;
3277 case 'f':
3278 c = '\f';
3279 break;
3280 case 'n':
3281 c = '\n';
3282 break;
3283 case 'r':
3284 c = '\r';
3285 break;
3286 case 't':
3287 c = '\t';
3288 break;
3289 case 'v':
3290 c = '\v';
3291 break;
3292 case 'e':
3293 if (!gnu_ext)
3294 goto invalid_escape;
3295 c = 27;
3296 break;
3297 case '\'':
3298 case '\"':
3299 case '\\':
3300 case '?':
3301 break;
3302 default:
3303 invalid_escape:
3304 if (c >= '!' && c <= '~')
3305 warning("unknown escape sequence: \'\\%c\'", c);
3306 else
3307 warning("unknown escape sequence: \'\\x%x\'", c);
3308 break;
3311 p++;
3312 add_char_nonext:
3313 if (!is_long)
3314 cstr_ccat(outstr, c);
3315 else
3316 cstr_wccat(outstr, c);
3318 /* add a trailing '\0' */
3319 if (!is_long)
3320 cstr_ccat(outstr, '\0');
3321 else
3322 cstr_wccat(outstr, '\0');
3325 /* we use 64 bit numbers */
3326 #define BN_SIZE 2
3328 /* bn = (bn << shift) | or_val */
3329 void bn_lshift(unsigned int *bn, int shift, int or_val)
3331 int i;
3332 unsigned int v;
3333 for(i=0;i<BN_SIZE;i++) {
3334 v = bn[i];
3335 bn[i] = (v << shift) | or_val;
3336 or_val = v >> (32 - shift);
3340 void bn_zero(unsigned int *bn)
3342 int i;
3343 for(i=0;i<BN_SIZE;i++) {
3344 bn[i] = 0;
3348 /* parse number in null terminated string 'p' and return it in the
3349 current token */
3350 void parse_number(const char *p)
3352 int b, t, shift, frac_bits, s, exp_val, ch;
3353 char *q;
3354 unsigned int bn[BN_SIZE];
3355 double d;
3357 /* number */
3358 q = token_buf;
3359 ch = *p++;
3360 t = ch;
3361 ch = *p++;
3362 *q++ = t;
3363 b = 10;
3364 if (t == '.') {
3365 goto float_frac_parse;
3366 } else if (t == '0') {
3367 if (ch == 'x' || ch == 'X') {
3368 q--;
3369 ch = *p++;
3370 b = 16;
3371 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3372 q--;
3373 ch = *p++;
3374 b = 2;
3377 /* parse all digits. cannot check octal numbers at this stage
3378 because of floating point constants */
3379 while (1) {
3380 if (ch >= 'a' && ch <= 'f')
3381 t = ch - 'a' + 10;
3382 else if (ch >= 'A' && ch <= 'F')
3383 t = ch - 'A' + 10;
3384 else if (isnum(ch))
3385 t = ch - '0';
3386 else
3387 break;
3388 if (t >= b)
3389 break;
3390 if (q >= token_buf + STRING_MAX_SIZE) {
3391 num_too_long:
3392 error("number too long");
3394 *q++ = ch;
3395 ch = *p++;
3397 if (ch == '.' ||
3398 ((ch == 'e' || ch == 'E') && b == 10) ||
3399 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3400 if (b != 10) {
3401 /* NOTE: strtox should support that for hexa numbers, but
3402 non ISOC99 libcs do not support it, so we prefer to do
3403 it by hand */
3404 /* hexadecimal or binary floats */
3405 /* XXX: handle overflows */
3406 *q = '\0';
3407 if (b == 16)
3408 shift = 4;
3409 else
3410 shift = 2;
3411 bn_zero(bn);
3412 q = token_buf;
3413 while (1) {
3414 t = *q++;
3415 if (t == '\0') {
3416 break;
3417 } else if (t >= 'a') {
3418 t = t - 'a' + 10;
3419 } else if (t >= 'A') {
3420 t = t - 'A' + 10;
3421 } else {
3422 t = t - '0';
3424 bn_lshift(bn, shift, t);
3426 frac_bits = 0;
3427 if (ch == '.') {
3428 ch = *p++;
3429 while (1) {
3430 t = ch;
3431 if (t >= 'a' && t <= 'f') {
3432 t = t - 'a' + 10;
3433 } else if (t >= 'A' && t <= 'F') {
3434 t = t - 'A' + 10;
3435 } else if (t >= '0' && t <= '9') {
3436 t = t - '0';
3437 } else {
3438 break;
3440 if (t >= b)
3441 error("invalid digit");
3442 bn_lshift(bn, shift, t);
3443 frac_bits += shift;
3444 ch = *p++;
3447 if (ch != 'p' && ch != 'P')
3448 expect("exponent");
3449 ch = *p++;
3450 s = 1;
3451 exp_val = 0;
3452 if (ch == '+') {
3453 ch = *p++;
3454 } else if (ch == '-') {
3455 s = -1;
3456 ch = *p++;
3458 if (ch < '0' || ch > '9')
3459 expect("exponent digits");
3460 while (ch >= '0' && ch <= '9') {
3461 exp_val = exp_val * 10 + ch - '0';
3462 ch = *p++;
3464 exp_val = exp_val * s;
3466 /* now we can generate the number */
3467 /* XXX: should patch directly float number */
3468 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3469 d = ldexp(d, exp_val - frac_bits);
3470 t = toup(ch);
3471 if (t == 'F') {
3472 ch = *p++;
3473 tok = TOK_CFLOAT;
3474 /* float : should handle overflow */
3475 tokc.f = (float)d;
3476 } else if (t == 'L') {
3477 ch = *p++;
3478 tok = TOK_CLDOUBLE;
3479 /* XXX: not large enough */
3480 tokc.ld = (long double)d;
3481 } else {
3482 tok = TOK_CDOUBLE;
3483 tokc.d = d;
3485 } else {
3486 /* decimal floats */
3487 if (ch == '.') {
3488 if (q >= token_buf + STRING_MAX_SIZE)
3489 goto num_too_long;
3490 *q++ = ch;
3491 ch = *p++;
3492 float_frac_parse:
3493 while (ch >= '0' && ch <= '9') {
3494 if (q >= token_buf + STRING_MAX_SIZE)
3495 goto num_too_long;
3496 *q++ = ch;
3497 ch = *p++;
3500 if (ch == 'e' || ch == 'E') {
3501 if (q >= token_buf + STRING_MAX_SIZE)
3502 goto num_too_long;
3503 *q++ = ch;
3504 ch = *p++;
3505 if (ch == '-' || ch == '+') {
3506 if (q >= token_buf + STRING_MAX_SIZE)
3507 goto num_too_long;
3508 *q++ = ch;
3509 ch = *p++;
3511 if (ch < '0' || ch > '9')
3512 expect("exponent digits");
3513 while (ch >= '0' && ch <= '9') {
3514 if (q >= token_buf + STRING_MAX_SIZE)
3515 goto num_too_long;
3516 *q++ = ch;
3517 ch = *p++;
3520 *q = '\0';
3521 t = toup(ch);
3522 errno = 0;
3523 if (t == 'F') {
3524 ch = *p++;
3525 tok = TOK_CFLOAT;
3526 tokc.f = strtof(token_buf, NULL);
3527 } else if (t == 'L') {
3528 ch = *p++;
3529 tok = TOK_CLDOUBLE;
3530 tokc.ld = strtold(token_buf, NULL);
3531 } else {
3532 tok = TOK_CDOUBLE;
3533 tokc.d = strtod(token_buf, NULL);
3536 } else {
3537 unsigned long long n, n1;
3538 int lcount, ucount;
3540 /* integer number */
3541 *q = '\0';
3542 q = token_buf;
3543 if (b == 10 && *q == '0') {
3544 b = 8;
3545 q++;
3547 n = 0;
3548 while(1) {
3549 t = *q++;
3550 /* no need for checks except for base 10 / 8 errors */
3551 if (t == '\0') {
3552 break;
3553 } else if (t >= 'a') {
3554 t = t - 'a' + 10;
3555 } else if (t >= 'A') {
3556 t = t - 'A' + 10;
3557 } else {
3558 t = t - '0';
3559 if (t >= b)
3560 error("invalid digit");
3562 n1 = n;
3563 n = n * b + t;
3564 /* detect overflow */
3565 /* XXX: this test is not reliable */
3566 if (n < n1)
3567 error("integer constant overflow");
3570 /* XXX: not exactly ANSI compliant */
3571 if ((n & 0xffffffff00000000LL) != 0) {
3572 if ((n >> 63) != 0)
3573 tok = TOK_CULLONG;
3574 else
3575 tok = TOK_CLLONG;
3576 } else if (n > 0x7fffffff) {
3577 tok = TOK_CUINT;
3578 } else {
3579 tok = TOK_CINT;
3581 lcount = 0;
3582 ucount = 0;
3583 for(;;) {
3584 t = toup(ch);
3585 if (t == 'L') {
3586 if (lcount >= 2)
3587 error("three 'l's in integer constant");
3588 lcount++;
3589 if (lcount == 2) {
3590 if (tok == TOK_CINT)
3591 tok = TOK_CLLONG;
3592 else if (tok == TOK_CUINT)
3593 tok = TOK_CULLONG;
3595 ch = *p++;
3596 } else if (t == 'U') {
3597 if (ucount >= 1)
3598 error("two 'u's in integer constant");
3599 ucount++;
3600 if (tok == TOK_CINT)
3601 tok = TOK_CUINT;
3602 else if (tok == TOK_CLLONG)
3603 tok = TOK_CULLONG;
3604 ch = *p++;
3605 } else {
3606 break;
3609 if (tok == TOK_CINT || tok == TOK_CUINT)
3610 tokc.ui = n;
3611 else
3612 tokc.ull = n;
3617 #define PARSE2(c1, tok1, c2, tok2) \
3618 case c1: \
3619 PEEKC(c, p); \
3620 if (c == c2) { \
3621 p++; \
3622 tok = tok2; \
3623 } else { \
3624 tok = tok1; \
3626 break;
3628 /* return next token without macro substitution */
3629 static inline void next_nomacro1(void)
3631 int t, c, is_long;
3632 TokenSym *ts;
3633 uint8_t *p, *p1;
3634 unsigned int h;
3636 p = file->buf_ptr;
3637 redo_no_start:
3638 c = *p;
3639 switch(c) {
3640 case ' ':
3641 case '\t':
3642 case '\f':
3643 case '\v':
3644 case '\r':
3645 p++;
3646 goto redo_no_start;
3648 case '\\':
3649 /* first look if it is in fact an end of buffer */
3650 if (p >= file->buf_end) {
3651 file->buf_ptr = p;
3652 handle_eob();
3653 p = file->buf_ptr;
3654 if (p >= file->buf_end)
3655 goto parse_eof;
3656 else
3657 goto redo_no_start;
3658 } else {
3659 file->buf_ptr = p;
3660 ch = *p;
3661 handle_stray();
3662 p = file->buf_ptr;
3663 goto redo_no_start;
3665 parse_eof:
3667 TCCState *s1 = tcc_state;
3668 if ((parse_flags & PARSE_FLAG_LINEFEED)
3669 && !(tok_flags & TOK_FLAG_EOF)) {
3670 tok_flags |= TOK_FLAG_EOF;
3671 tok = TOK_LINEFEED;
3672 goto keep_tok_flags;
3673 } else if (s1->include_stack_ptr == s1->include_stack ||
3674 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3675 /* no include left : end of file. */
3676 tok = TOK_EOF;
3677 } else {
3678 tok_flags &= ~TOK_FLAG_EOF;
3679 /* pop include file */
3681 /* test if previous '#endif' was after a #ifdef at
3682 start of file */
3683 if (tok_flags & TOK_FLAG_ENDIF) {
3684 #ifdef INC_DEBUG
3685 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3686 #endif
3687 add_cached_include(s1, file->inc_type, file->inc_filename,
3688 file->ifndef_macro_saved);
3691 /* add end of include file debug info */
3692 if (do_debug) {
3693 put_stabd(N_EINCL, 0, 0);
3695 /* pop include stack */
3696 tcc_close(file);
3697 s1->include_stack_ptr--;
3698 file = *s1->include_stack_ptr;
3699 p = file->buf_ptr;
3700 goto redo_no_start;
3703 break;
3705 case '\n':
3706 file->line_num++;
3707 tok_flags |= TOK_FLAG_BOL;
3708 p++;
3709 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3710 goto redo_no_start;
3711 tok = TOK_LINEFEED;
3712 goto keep_tok_flags;
3714 case '#':
3715 /* XXX: simplify */
3716 PEEKC(c, p);
3717 if ((tok_flags & TOK_FLAG_BOL) &&
3718 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3719 file->buf_ptr = p;
3720 preprocess(tok_flags & TOK_FLAG_BOF);
3721 p = file->buf_ptr;
3722 goto redo_no_start;
3723 } else {
3724 if (c == '#') {
3725 p++;
3726 tok = TOK_TWOSHARPS;
3727 } else {
3728 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3729 p = parse_line_comment(p - 1);
3730 goto redo_no_start;
3731 } else {
3732 tok = '#';
3736 break;
3738 case 'a': case 'b': case 'c': case 'd':
3739 case 'e': case 'f': case 'g': case 'h':
3740 case 'i': case 'j': case 'k': case 'l':
3741 case 'm': case 'n': case 'o': case 'p':
3742 case 'q': case 'r': case 's': case 't':
3743 case 'u': case 'v': case 'w': case 'x':
3744 case 'y': case 'z':
3745 case 'A': case 'B': case 'C': case 'D':
3746 case 'E': case 'F': case 'G': case 'H':
3747 case 'I': case 'J': case 'K':
3748 case 'M': case 'N': case 'O': case 'P':
3749 case 'Q': case 'R': case 'S': case 'T':
3750 case 'U': case 'V': case 'W': case 'X':
3751 case 'Y': case 'Z':
3752 case '_':
3753 parse_ident_fast:
3754 p1 = p;
3755 h = TOK_HASH_INIT;
3756 h = TOK_HASH_FUNC(h, c);
3757 p++;
3758 for(;;) {
3759 c = *p;
3760 if (!isidnum_table[c])
3761 break;
3762 h = TOK_HASH_FUNC(h, c);
3763 p++;
3765 if (c != '\\') {
3766 TokenSym **pts;
3767 int len;
3769 /* fast case : no stray found, so we have the full token
3770 and we have already hashed it */
3771 len = p - p1;
3772 h &= (TOK_HASH_SIZE - 1);
3773 pts = &hash_ident[h];
3774 for(;;) {
3775 ts = *pts;
3776 if (!ts)
3777 break;
3778 if (ts->len == len && !memcmp(ts->str, p1, len))
3779 goto token_found;
3780 pts = &(ts->hash_next);
3782 ts = tok_alloc_new(pts, p1, len);
3783 token_found: ;
3784 } else {
3785 /* slower case */
3786 cstr_reset(&tokcstr);
3788 while (p1 < p) {
3789 cstr_ccat(&tokcstr, *p1);
3790 p1++;
3792 p--;
3793 PEEKC(c, p);
3794 parse_ident_slow:
3795 while (isidnum_table[c]) {
3796 cstr_ccat(&tokcstr, c);
3797 PEEKC(c, p);
3799 ts = tok_alloc(tokcstr.data, tokcstr.size);
3801 tok = ts->tok;
3802 break;
3803 case 'L':
3804 t = p[1];
3805 if (t != '\\' && t != '\'' && t != '\"') {
3806 /* fast case */
3807 goto parse_ident_fast;
3808 } else {
3809 PEEKC(c, p);
3810 if (c == '\'' || c == '\"') {
3811 is_long = 1;
3812 goto str_const;
3813 } else {
3814 cstr_reset(&tokcstr);
3815 cstr_ccat(&tokcstr, 'L');
3816 goto parse_ident_slow;
3819 break;
3820 case '0': case '1': case '2': case '3':
3821 case '4': case '5': case '6': case '7':
3822 case '8': case '9':
3824 cstr_reset(&tokcstr);
3825 /* after the first digit, accept digits, alpha, '.' or sign if
3826 prefixed by 'eEpP' */
3827 parse_num:
3828 for(;;) {
3829 t = c;
3830 cstr_ccat(&tokcstr, c);
3831 PEEKC(c, p);
3832 if (!(isnum(c) || isid(c) || c == '.' ||
3833 ((c == '+' || c == '-') &&
3834 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3835 break;
3837 /* We add a trailing '\0' to ease parsing */
3838 cstr_ccat(&tokcstr, '\0');
3839 tokc.cstr = &tokcstr;
3840 tok = TOK_PPNUM;
3841 break;
3842 case '.':
3843 /* special dot handling because it can also start a number */
3844 PEEKC(c, p);
3845 if (isnum(c)) {
3846 cstr_reset(&tokcstr);
3847 cstr_ccat(&tokcstr, '.');
3848 goto parse_num;
3849 } else if (c == '.') {
3850 PEEKC(c, p);
3851 if (c != '.')
3852 expect("'.'");
3853 PEEKC(c, p);
3854 tok = TOK_DOTS;
3855 } else {
3856 tok = '.';
3858 break;
3859 case '\'':
3860 case '\"':
3861 is_long = 0;
3862 str_const:
3864 CString str;
3865 int sep;
3867 sep = c;
3869 /* parse the string */
3870 cstr_new(&str);
3871 p = parse_pp_string(p, sep, &str);
3872 cstr_ccat(&str, '\0');
3874 /* eval the escape (should be done as TOK_PPNUM) */
3875 cstr_reset(&tokcstr);
3876 parse_escape_string(&tokcstr, str.data, is_long);
3877 cstr_free(&str);
3879 if (sep == '\'') {
3880 int char_size;
3881 /* XXX: make it portable */
3882 if (!is_long)
3883 char_size = 1;
3884 else
3885 char_size = sizeof(nwchar_t);
3886 if (tokcstr.size <= char_size)
3887 error("empty character constant");
3888 if (tokcstr.size > 2 * char_size)
3889 warning("multi-character character constant");
3890 if (!is_long) {
3891 tokc.i = *(int8_t *)tokcstr.data;
3892 tok = TOK_CCHAR;
3893 } else {
3894 tokc.i = *(nwchar_t *)tokcstr.data;
3895 tok = TOK_LCHAR;
3897 } else {
3898 tokc.cstr = &tokcstr;
3899 if (!is_long)
3900 tok = TOK_STR;
3901 else
3902 tok = TOK_LSTR;
3905 break;
3907 case '<':
3908 PEEKC(c, p);
3909 if (c == '=') {
3910 p++;
3911 tok = TOK_LE;
3912 } else if (c == '<') {
3913 PEEKC(c, p);
3914 if (c == '=') {
3915 p++;
3916 tok = TOK_A_SHL;
3917 } else {
3918 tok = TOK_SHL;
3920 } else {
3921 tok = TOK_LT;
3923 break;
3925 case '>':
3926 PEEKC(c, p);
3927 if (c == '=') {
3928 p++;
3929 tok = TOK_GE;
3930 } else if (c == '>') {
3931 PEEKC(c, p);
3932 if (c == '=') {
3933 p++;
3934 tok = TOK_A_SAR;
3935 } else {
3936 tok = TOK_SAR;
3938 } else {
3939 tok = TOK_GT;
3941 break;
3943 case '&':
3944 PEEKC(c, p);
3945 if (c == '&') {
3946 p++;
3947 tok = TOK_LAND;
3948 } else if (c == '=') {
3949 p++;
3950 tok = TOK_A_AND;
3951 } else {
3952 tok = '&';
3954 break;
3956 case '|':
3957 PEEKC(c, p);
3958 if (c == '|') {
3959 p++;
3960 tok = TOK_LOR;
3961 } else if (c == '=') {
3962 p++;
3963 tok = TOK_A_OR;
3964 } else {
3965 tok = '|';
3967 break;
3969 case '+':
3970 PEEKC(c, p);
3971 if (c == '+') {
3972 p++;
3973 tok = TOK_INC;
3974 } else if (c == '=') {
3975 p++;
3976 tok = TOK_A_ADD;
3977 } else {
3978 tok = '+';
3980 break;
3982 case '-':
3983 PEEKC(c, p);
3984 if (c == '-') {
3985 p++;
3986 tok = TOK_DEC;
3987 } else if (c == '=') {
3988 p++;
3989 tok = TOK_A_SUB;
3990 } else if (c == '>') {
3991 p++;
3992 tok = TOK_ARROW;
3993 } else {
3994 tok = '-';
3996 break;
3998 PARSE2('!', '!', '=', TOK_NE)
3999 PARSE2('=', '=', '=', TOK_EQ)
4000 PARSE2('*', '*', '=', TOK_A_MUL)
4001 PARSE2('%', '%', '=', TOK_A_MOD)
4002 PARSE2('^', '^', '=', TOK_A_XOR)
4004 /* comments or operator */
4005 case '/':
4006 PEEKC(c, p);
4007 if (c == '*') {
4008 p = parse_comment(p);
4009 goto redo_no_start;
4010 } else if (c == '/') {
4011 p = parse_line_comment(p);
4012 goto redo_no_start;
4013 } else if (c == '=') {
4014 p++;
4015 tok = TOK_A_DIV;
4016 } else {
4017 tok = '/';
4019 break;
4021 /* simple tokens */
4022 case '(':
4023 case ')':
4024 case '[':
4025 case ']':
4026 case '{':
4027 case '}':
4028 case ',':
4029 case ';':
4030 case ':':
4031 case '?':
4032 case '~':
4033 case '$': /* only used in assembler */
4034 case '@': /* dito */
4035 tok = c;
4036 p++;
4037 break;
4038 default:
4039 error("unrecognized character \\x%02x", c);
4040 break;
4042 tok_flags = 0;
4043 keep_tok_flags:
4044 file->buf_ptr = p;
4045 #if defined(PARSE_DEBUG)
4046 printf("token = %s\n", get_tok_str(tok, &tokc));
4047 #endif
4050 /* return next token without macro substitution. Can read input from
4051 macro_ptr buffer */
4052 static void next_nomacro(void)
4054 if (macro_ptr) {
4055 redo:
4056 tok = *macro_ptr;
4057 if (tok) {
4058 TOK_GET(tok, macro_ptr, tokc);
4059 if (tok == TOK_LINENUM) {
4060 file->line_num = tokc.i;
4061 goto redo;
4064 } else {
4065 next_nomacro1();
4069 /* substitute args in macro_str and return allocated string */
4070 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4072 int *st, last_tok, t, notfirst;
4073 Sym *s;
4074 CValue cval;
4075 TokenString str;
4076 CString cstr;
4078 tok_str_new(&str);
4079 last_tok = 0;
4080 while(1) {
4081 TOK_GET(t, macro_str, cval);
4082 if (!t)
4083 break;
4084 if (t == '#') {
4085 /* stringize */
4086 TOK_GET(t, macro_str, cval);
4087 if (!t)
4088 break;
4089 s = sym_find2(args, t);
4090 if (s) {
4091 cstr_new(&cstr);
4092 st = (int *)s->c;
4093 notfirst = 0;
4094 while (*st) {
4095 if (notfirst)
4096 cstr_ccat(&cstr, ' ');
4097 TOK_GET(t, st, cval);
4098 cstr_cat(&cstr, get_tok_str(t, &cval));
4099 #ifndef PP_NOSPACES
4100 notfirst = 1;
4101 #endif
4103 cstr_ccat(&cstr, '\0');
4104 #ifdef PP_DEBUG
4105 printf("stringize: %s\n", (char *)cstr.data);
4106 #endif
4107 /* add string */
4108 cval.cstr = &cstr;
4109 tok_str_add2(&str, TOK_STR, &cval);
4110 cstr_free(&cstr);
4111 } else {
4112 tok_str_add2(&str, t, &cval);
4114 } else if (t >= TOK_IDENT) {
4115 s = sym_find2(args, t);
4116 if (s) {
4117 st = (int *)s->c;
4118 /* if '##' is present before or after, no arg substitution */
4119 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4120 /* special case for var arg macros : ## eats the
4121 ',' if empty VA_ARGS variable. */
4122 /* XXX: test of the ',' is not 100%
4123 reliable. should fix it to avoid security
4124 problems */
4125 if (gnu_ext && s->type.t &&
4126 last_tok == TOK_TWOSHARPS &&
4127 str.len >= 2 && str.str[str.len - 2] == ',') {
4128 if (*st == 0) {
4129 /* suppress ',' '##' */
4130 str.len -= 2;
4131 } else {
4132 /* suppress '##' and add variable */
4133 str.len--;
4134 goto add_var;
4136 } else {
4137 int t1;
4138 add_var:
4139 for(;;) {
4140 TOK_GET(t1, st, cval);
4141 if (!t1)
4142 break;
4143 tok_str_add2(&str, t1, &cval);
4146 } else {
4147 /* NOTE: the stream cannot be read when macro
4148 substituing an argument */
4149 macro_subst(&str, nested_list, st, NULL);
4151 } else {
4152 tok_str_add(&str, t);
4154 } else {
4155 tok_str_add2(&str, t, &cval);
4157 last_tok = t;
4159 tok_str_add(&str, 0);
4160 return str.str;
4163 static char const ab_month_name[12][4] =
4165 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4166 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4169 /* do macro substitution of current token with macro 's' and add
4170 result to (tok_str,tok_len). 'nested_list' is the list of all
4171 macros we got inside to avoid recursing. Return non zero if no
4172 substitution needs to be done */
4173 static int macro_subst_tok(TokenString *tok_str,
4174 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4176 Sym *args, *sa, *sa1;
4177 int mstr_allocated, parlevel, *mstr, t, t1;
4178 TokenString str;
4179 char *cstrval;
4180 CValue cval;
4181 CString cstr;
4182 char buf[32];
4184 /* if symbol is a macro, prepare substitution */
4185 /* special macros */
4186 if (tok == TOK___LINE__) {
4187 snprintf(buf, sizeof(buf), "%d", file->line_num);
4188 cstrval = buf;
4189 t1 = TOK_PPNUM;
4190 goto add_cstr1;
4191 } else if (tok == TOK___FILE__) {
4192 cstrval = file->filename;
4193 goto add_cstr;
4194 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4195 time_t ti;
4196 struct tm *tm;
4198 time(&ti);
4199 tm = localtime(&ti);
4200 if (tok == TOK___DATE__) {
4201 snprintf(buf, sizeof(buf), "%s %2d %d",
4202 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4203 } else {
4204 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4205 tm->tm_hour, tm->tm_min, tm->tm_sec);
4207 cstrval = buf;
4208 add_cstr:
4209 t1 = TOK_STR;
4210 add_cstr1:
4211 cstr_new(&cstr);
4212 cstr_cat(&cstr, cstrval);
4213 cstr_ccat(&cstr, '\0');
4214 cval.cstr = &cstr;
4215 tok_str_add2(tok_str, t1, &cval);
4216 cstr_free(&cstr);
4217 } else {
4218 mstr = (int *)s->c;
4219 mstr_allocated = 0;
4220 if (s->type.t == MACRO_FUNC) {
4221 /* NOTE: we do not use next_nomacro to avoid eating the
4222 next token. XXX: find better solution */
4223 redo:
4224 if (macro_ptr) {
4225 t = *macro_ptr;
4226 if (t == 0 && can_read_stream) {
4227 /* end of macro stream: we must look at the token
4228 after in the file */
4229 struct macro_level *ml = *can_read_stream;
4230 macro_ptr = NULL;
4231 if (ml)
4233 macro_ptr = ml->p;
4234 ml->p = NULL;
4235 *can_read_stream = ml -> prev;
4237 goto redo;
4239 } else {
4240 /* XXX: incorrect with comments */
4241 ch = file->buf_ptr[0];
4242 while (is_space(ch) || ch == '\n')
4243 cinp();
4244 t = ch;
4246 if (t != '(') /* no macro subst */
4247 return -1;
4249 /* argument macro */
4250 next_nomacro();
4251 next_nomacro();
4252 args = NULL;
4253 sa = s->next;
4254 /* NOTE: empty args are allowed, except if no args */
4255 for(;;) {
4256 /* handle '()' case */
4257 if (!args && !sa && tok == ')')
4258 break;
4259 if (!sa)
4260 error("macro '%s' used with too many args",
4261 get_tok_str(s->v, 0));
4262 tok_str_new(&str);
4263 parlevel = 0;
4264 /* NOTE: non zero sa->t indicates VA_ARGS */
4265 while ((parlevel > 0 ||
4266 (tok != ')' &&
4267 (tok != ',' || sa->type.t))) &&
4268 tok != -1) {
4269 if (tok == '(')
4270 parlevel++;
4271 else if (tok == ')')
4272 parlevel--;
4273 if (tok != TOK_LINEFEED)
4274 tok_str_add2(&str, tok, &tokc);
4275 next_nomacro();
4277 tok_str_add(&str, 0);
4278 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
4279 sa = sa->next;
4280 if (tok == ')') {
4281 /* special case for gcc var args: add an empty
4282 var arg argument if it is omitted */
4283 if (sa && sa->type.t && gnu_ext)
4284 continue;
4285 else
4286 break;
4288 if (tok != ',')
4289 expect(",");
4290 next_nomacro();
4292 if (sa) {
4293 error("macro '%s' used with too few args",
4294 get_tok_str(s->v, 0));
4297 /* now subst each arg */
4298 mstr = macro_arg_subst(nested_list, mstr, args);
4299 /* free memory */
4300 sa = args;
4301 while (sa) {
4302 sa1 = sa->prev;
4303 tok_str_free((int *)sa->c);
4304 sym_free(sa);
4305 sa = sa1;
4307 mstr_allocated = 1;
4309 sym_push2(nested_list, s->v, 0, 0);
4310 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4311 /* pop nested defined symbol */
4312 sa1 = *nested_list;
4313 *nested_list = sa1->prev;
4314 sym_free(sa1);
4315 if (mstr_allocated)
4316 tok_str_free(mstr);
4318 return 0;
4321 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4322 return the resulting string (which must be freed). */
4323 static inline int *macro_twosharps(const int *macro_str)
4325 TokenSym *ts;
4326 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4327 int t;
4328 const char *p1, *p2;
4329 CValue cval;
4330 TokenString macro_str1;
4331 CString cstr;
4333 start_macro_ptr = macro_str;
4334 /* we search the first '##' */
4335 for(;;) {
4336 macro_ptr1 = macro_str;
4337 TOK_GET(t, macro_str, cval);
4338 /* nothing more to do if end of string */
4339 if (t == 0)
4340 return NULL;
4341 if (*macro_str == TOK_TWOSHARPS)
4342 break;
4345 /* we saw '##', so we need more processing to handle it */
4346 cstr_new(&cstr);
4347 tok_str_new(&macro_str1);
4348 tok = t;
4349 tokc = cval;
4351 /* add all tokens seen so far */
4352 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4353 TOK_GET(t, ptr, cval);
4354 tok_str_add2(&macro_str1, t, &cval);
4356 saved_macro_ptr = macro_ptr;
4357 /* XXX: get rid of the use of macro_ptr here */
4358 macro_ptr = (int *)macro_str;
4359 for(;;) {
4360 while (*macro_ptr == TOK_TWOSHARPS) {
4361 macro_ptr++;
4362 macro_ptr1 = macro_ptr;
4363 t = *macro_ptr;
4364 if (t) {
4365 TOK_GET(t, macro_ptr, cval);
4366 /* We concatenate the two tokens if we have an
4367 identifier or a preprocessing number */
4368 cstr_reset(&cstr);
4369 p1 = get_tok_str(tok, &tokc);
4370 cstr_cat(&cstr, p1);
4371 p2 = get_tok_str(t, &cval);
4372 cstr_cat(&cstr, p2);
4373 cstr_ccat(&cstr, '\0');
4375 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4376 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4377 if (tok == TOK_PPNUM) {
4378 /* if number, then create a number token */
4379 /* NOTE: no need to allocate because
4380 tok_str_add2() does it */
4381 cstr_reset(&tokcstr);
4382 tokcstr = cstr;
4383 cstr_new(&cstr);
4384 tokc.cstr = &tokcstr;
4385 } else {
4386 /* if identifier, we must do a test to
4387 validate we have a correct identifier */
4388 if (t == TOK_PPNUM) {
4389 const char *p;
4390 int c;
4392 p = p2;
4393 for(;;) {
4394 c = *p;
4395 if (c == '\0')
4396 break;
4397 p++;
4398 if (!isnum(c) && !isid(c))
4399 goto error_pasting;
4402 ts = tok_alloc(cstr.data, strlen(cstr.data));
4403 tok = ts->tok; /* modify current token */
4405 } else {
4406 const char *str = cstr.data;
4407 const unsigned char *q;
4409 /* we look for a valid token */
4410 /* XXX: do more extensive checks */
4411 if (!strcmp(str, ">>=")) {
4412 tok = TOK_A_SAR;
4413 } else if (!strcmp(str, "<<=")) {
4414 tok = TOK_A_SHL;
4415 } else if (strlen(str) == 2) {
4416 /* search in two bytes table */
4417 q = tok_two_chars;
4418 for(;;) {
4419 if (!*q)
4420 goto error_pasting;
4421 if (q[0] == str[0] && q[1] == str[1])
4422 break;
4423 q += 3;
4425 tok = q[2];
4426 } else {
4427 error_pasting:
4428 /* NOTE: because get_tok_str use a static buffer,
4429 we must save it */
4430 cstr_reset(&cstr);
4431 p1 = get_tok_str(tok, &tokc);
4432 cstr_cat(&cstr, p1);
4433 cstr_ccat(&cstr, '\0');
4434 p2 = get_tok_str(t, &cval);
4435 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4436 /* cannot merge tokens: just add them separately */
4437 tok_str_add2(&macro_str1, tok, &tokc);
4438 /* XXX: free associated memory ? */
4439 tok = t;
4440 tokc = cval;
4445 tok_str_add2(&macro_str1, tok, &tokc);
4446 next_nomacro();
4447 if (tok == 0)
4448 break;
4450 macro_ptr = (int *)saved_macro_ptr;
4451 cstr_free(&cstr);
4452 tok_str_add(&macro_str1, 0);
4453 return macro_str1.str;
4457 /* do macro substitution of macro_str and add result to
4458 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4459 inside to avoid recursing. */
4460 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4461 const int *macro_str, struct macro_level ** can_read_stream)
4463 Sym *s;
4464 int *macro_str1;
4465 const int *ptr;
4466 int t, ret;
4467 CValue cval;
4468 struct macro_level ml;
4470 /* first scan for '##' operator handling */
4471 ptr = macro_str;
4472 macro_str1 = macro_twosharps(ptr);
4473 if (macro_str1)
4474 ptr = macro_str1;
4475 while (1) {
4476 /* NOTE: ptr == NULL can only happen if tokens are read from
4477 file stream due to a macro function call */
4478 if (ptr == NULL)
4479 break;
4480 TOK_GET(t, ptr, cval);
4481 if (t == 0)
4482 break;
4483 s = define_find(t);
4484 if (s != NULL) {
4485 /* if nested substitution, do nothing */
4486 if (sym_find2(*nested_list, t))
4487 goto no_subst;
4488 ml.p = macro_ptr;
4489 if (can_read_stream)
4490 ml.prev = *can_read_stream, *can_read_stream = &ml;
4491 macro_ptr = (int *)ptr;
4492 tok = t;
4493 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4494 ptr = (int *)macro_ptr;
4495 macro_ptr = ml.p;
4496 if (can_read_stream && *can_read_stream == &ml)
4497 *can_read_stream = ml.prev;
4498 if (ret != 0)
4499 goto no_subst;
4500 } else {
4501 no_subst:
4502 tok_str_add2(tok_str, t, &cval);
4505 if (macro_str1)
4506 tok_str_free(macro_str1);
4509 /* return next token with macro substitution */
4510 static void next(void)
4512 Sym *nested_list, *s;
4513 TokenString str;
4514 struct macro_level *ml;
4516 redo:
4517 next_nomacro();
4518 if (!macro_ptr) {
4519 /* if not reading from macro substituted string, then try
4520 to substitute macros */
4521 if (tok >= TOK_IDENT &&
4522 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4523 s = define_find(tok);
4524 if (s) {
4525 /* we have a macro: we try to substitute */
4526 tok_str_new(&str);
4527 nested_list = NULL;
4528 ml = NULL;
4529 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4530 /* substitution done, NOTE: maybe empty */
4531 tok_str_add(&str, 0);
4532 macro_ptr = str.str;
4533 macro_ptr_allocated = str.str;
4534 goto redo;
4538 } else {
4539 if (tok == 0) {
4540 /* end of macro or end of unget buffer */
4541 if (unget_buffer_enabled) {
4542 macro_ptr = unget_saved_macro_ptr;
4543 unget_buffer_enabled = 0;
4544 } else {
4545 /* end of macro string: free it */
4546 tok_str_free(macro_ptr_allocated);
4547 macro_ptr = NULL;
4549 goto redo;
4553 /* convert preprocessor tokens into C tokens */
4554 if (tok == TOK_PPNUM &&
4555 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4556 parse_number((char *)tokc.cstr->data);
4560 /* push back current token and set current token to 'last_tok'. Only
4561 identifier case handled for labels. */
4562 static inline void unget_tok(int last_tok)
4564 int i, n;
4565 int *q;
4566 unget_saved_macro_ptr = macro_ptr;
4567 unget_buffer_enabled = 1;
4568 q = unget_saved_buffer;
4569 macro_ptr = q;
4570 *q++ = tok;
4571 n = tok_ext_size(tok) - 1;
4572 for(i=0;i<n;i++)
4573 *q++ = tokc.tab[i];
4574 *q = 0; /* end of token string */
4575 tok = last_tok;
4579 void swap(int *p, int *q)
4581 int t;
4582 t = *p;
4583 *p = *q;
4584 *q = t;
4587 void vsetc(CType *type, int r, CValue *vc)
4589 int v;
4591 if (vtop >= vstack + (VSTACK_SIZE - 1))
4592 error("memory full");
4593 /* cannot let cpu flags if other instruction are generated. Also
4594 avoid leaving VT_JMP anywhere except on the top of the stack
4595 because it would complicate the code generator. */
4596 if (vtop >= vstack) {
4597 v = vtop->r & VT_VALMASK;
4598 if (v == VT_CMP || (v & ~1) == VT_JMP)
4599 gv(RC_INT);
4601 vtop++;
4602 vtop->type = *type;
4603 vtop->r = r;
4604 vtop->r2 = VT_CONST;
4605 vtop->c = *vc;
4608 /* push integer constant */
4609 void vpushi(int v)
4611 CValue cval;
4612 cval.i = v;
4613 vsetc(&int_type, VT_CONST, &cval);
4616 /* Return a static symbol pointing to a section */
4617 static Sym *get_sym_ref(CType *type, Section *sec,
4618 unsigned long offset, unsigned long size)
4620 int v;
4621 Sym *sym;
4623 v = anon_sym++;
4624 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4625 sym->type.ref = type->ref;
4626 sym->r = VT_CONST | VT_SYM;
4627 put_extern_sym(sym, sec, offset, size);
4628 return sym;
4631 /* push a reference to a section offset by adding a dummy symbol */
4632 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4634 CValue cval;
4636 cval.ul = 0;
4637 vsetc(type, VT_CONST | VT_SYM, &cval);
4638 vtop->sym = get_sym_ref(type, sec, offset, size);
4641 /* define a new external reference to a symbol 'v' of type 'u' */
4642 static Sym *external_global_sym(int v, CType *type, int r)
4644 Sym *s;
4646 s = sym_find(v);
4647 if (!s) {
4648 /* push forward reference */
4649 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4650 s->type.ref = type->ref;
4651 s->r = r | VT_CONST | VT_SYM;
4653 return s;
4656 /* define a new external reference to a symbol 'v' of type 'u' */
4657 static Sym *external_sym(int v, CType *type, int r)
4659 Sym *s;
4661 s = sym_find(v);
4662 if (!s) {
4663 /* push forward reference */
4664 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4665 s->type.t |= VT_EXTERN;
4666 } else {
4667 if (!is_compatible_types(&s->type, type))
4668 error("incompatible types for redefinition of '%s'",
4669 get_tok_str(v, NULL));
4671 return s;
4674 /* push a reference to global symbol v */
4675 static void vpush_global_sym(CType *type, int v)
4677 Sym *sym;
4678 CValue cval;
4680 sym = external_global_sym(v, type, 0);
4681 cval.ul = 0;
4682 vsetc(type, VT_CONST | VT_SYM, &cval);
4683 vtop->sym = sym;
4686 void vset(CType *type, int r, int v)
4688 CValue cval;
4690 cval.i = v;
4691 vsetc(type, r, &cval);
4694 void vseti(int r, int v)
4696 CType type;
4697 type.t = VT_INT;
4698 vset(&type, r, v);
4701 void vswap(void)
4703 SValue tmp;
4705 tmp = vtop[0];
4706 vtop[0] = vtop[-1];
4707 vtop[-1] = tmp;
4710 void vpushv(SValue *v)
4712 if (vtop >= vstack + (VSTACK_SIZE - 1))
4713 error("memory full");
4714 vtop++;
4715 *vtop = *v;
4718 void vdup(void)
4720 vpushv(vtop);
4723 /* save r to the memory stack, and mark it as being free */
4724 void save_reg(int r)
4726 int l, saved, size, align;
4727 SValue *p, sv;
4728 CType *type;
4730 /* modify all stack values */
4731 saved = 0;
4732 l = 0;
4733 for(p=vstack;p<=vtop;p++) {
4734 if ((p->r & VT_VALMASK) == r ||
4735 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4736 /* must save value on stack if not already done */
4737 if (!saved) {
4738 /* NOTE: must reload 'r' because r might be equal to r2 */
4739 r = p->r & VT_VALMASK;
4740 /* store register in the stack */
4741 type = &p->type;
4742 if ((p->r & VT_LVAL) ||
4743 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4744 type = &int_type;
4745 size = type_size(type, &align);
4746 loc = (loc - size) & -align;
4747 sv.type.t = type->t;
4748 sv.r = VT_LOCAL | VT_LVAL;
4749 sv.c.ul = loc;
4750 store(r, &sv);
4751 #ifdef TCC_TARGET_I386
4752 /* x86 specific: need to pop fp register ST0 if saved */
4753 if (r == TREG_ST0) {
4754 o(0xd9dd); /* fstp %st(1) */
4756 #endif
4757 /* special long long case */
4758 if ((type->t & VT_BTYPE) == VT_LLONG) {
4759 sv.c.ul += 4;
4760 store(p->r2, &sv);
4762 l = loc;
4763 saved = 1;
4765 /* mark that stack entry as being saved on the stack */
4766 if (p->r & VT_LVAL) {
4767 /* also clear the bounded flag because the
4768 relocation address of the function was stored in
4769 p->c.ul */
4770 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4771 } else {
4772 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4774 p->r2 = VT_CONST;
4775 p->c.ul = l;
4780 /* find a register of class 'rc2' with at most one reference on stack.
4781 * If none, call get_reg(rc) */
4782 int get_reg_ex(int rc, int rc2)
4784 int r;
4785 SValue *p;
4787 for(r=0;r<NB_REGS;r++) {
4788 if (reg_classes[r] & rc2) {
4789 int n;
4790 n=0;
4791 for(p = vstack; p <= vtop; p++) {
4792 if ((p->r & VT_VALMASK) == r ||
4793 (p->r2 & VT_VALMASK) == r)
4794 n++;
4796 if (n <= 1)
4797 return r;
4800 return get_reg(rc);
4803 /* find a free register of class 'rc'. If none, save one register */
4804 int get_reg(int rc)
4806 int r;
4807 SValue *p;
4809 /* find a free register */
4810 for(r=0;r<NB_REGS;r++) {
4811 if (reg_classes[r] & rc) {
4812 for(p=vstack;p<=vtop;p++) {
4813 if ((p->r & VT_VALMASK) == r ||
4814 (p->r2 & VT_VALMASK) == r)
4815 goto notfound;
4817 return r;
4819 notfound: ;
4822 /* no register left : free the first one on the stack (VERY
4823 IMPORTANT to start from the bottom to ensure that we don't
4824 spill registers used in gen_opi()) */
4825 for(p=vstack;p<=vtop;p++) {
4826 r = p->r & VT_VALMASK;
4827 if (r < VT_CONST && (reg_classes[r] & rc))
4828 goto save_found;
4829 /* also look at second register (if long long) */
4830 r = p->r2 & VT_VALMASK;
4831 if (r < VT_CONST && (reg_classes[r] & rc)) {
4832 save_found:
4833 save_reg(r);
4834 return r;
4837 /* Should never comes here */
4838 return -1;
4841 /* save registers up to (vtop - n) stack entry */
4842 void save_regs(int n)
4844 int r;
4845 SValue *p, *p1;
4846 p1 = vtop - n;
4847 for(p = vstack;p <= p1; p++) {
4848 r = p->r & VT_VALMASK;
4849 if (r < VT_CONST) {
4850 save_reg(r);
4855 /* move register 's' to 'r', and flush previous value of r to memory
4856 if needed */
4857 void move_reg(int r, int s)
4859 SValue sv;
4861 if (r != s) {
4862 save_reg(r);
4863 sv.type.t = VT_INT;
4864 sv.r = s;
4865 sv.c.ul = 0;
4866 load(r, &sv);
4870 /* get address of vtop (vtop MUST BE an lvalue) */
4871 void gaddrof(void)
4873 vtop->r &= ~VT_LVAL;
4874 /* tricky: if saved lvalue, then we can go back to lvalue */
4875 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4876 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4879 #ifdef CONFIG_TCC_BCHECK
4880 /* generate lvalue bound code */
4881 void gbound(void)
4883 int lval_type;
4884 CType type1;
4886 vtop->r &= ~VT_MUSTBOUND;
4887 /* if lvalue, then use checking code before dereferencing */
4888 if (vtop->r & VT_LVAL) {
4889 /* if not VT_BOUNDED value, then make one */
4890 if (!(vtop->r & VT_BOUNDED)) {
4891 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4892 /* must save type because we must set it to int to get pointer */
4893 type1 = vtop->type;
4894 vtop->type.t = VT_INT;
4895 gaddrof();
4896 vpushi(0);
4897 gen_bounded_ptr_add();
4898 vtop->r |= lval_type;
4899 vtop->type = type1;
4901 /* then check for dereferencing */
4902 gen_bounded_ptr_deref();
4905 #endif
4907 /* store vtop a register belonging to class 'rc'. lvalues are
4908 converted to values. Cannot be used if cannot be converted to
4909 register value (such as structures). */
4910 int gv(int rc)
4912 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4913 unsigned long long ll;
4915 /* NOTE: get_reg can modify vstack[] */
4916 if (vtop->type.t & VT_BITFIELD) {
4917 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4918 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4919 /* remove bit field info to avoid loops */
4920 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4921 /* generate shifts */
4922 vpushi(32 - (bit_pos + bit_size));
4923 gen_op(TOK_SHL);
4924 vpushi(32 - bit_size);
4925 /* NOTE: transformed to SHR if unsigned */
4926 gen_op(TOK_SAR);
4927 r = gv(rc);
4928 } else {
4929 if (is_float(vtop->type.t) &&
4930 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4931 Sym *sym;
4932 int *ptr;
4933 unsigned long offset;
4934 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4935 CValue check;
4936 #endif
4938 /* XXX: unify with initializers handling ? */
4939 /* CPUs usually cannot use float constants, so we store them
4940 generically in data segment */
4941 size = type_size(&vtop->type, &align);
4942 offset = (data_section->data_offset + align - 1) & -align;
4943 data_section->data_offset = offset;
4944 /* XXX: not portable yet */
4945 #ifdef __i386__
4946 /* Zero pad x87 tenbyte long doubles */
4947 if (size == 12)
4948 vtop->c.tab[2] &= 0xffff;
4949 #endif
4950 ptr = section_ptr_add(data_section, size);
4951 size = size >> 2;
4952 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4953 check.d = 1;
4954 if(check.tab[0])
4955 for(i=0;i<size;i++)
4956 ptr[i] = vtop->c.tab[size-1-i];
4957 else
4958 #endif
4959 for(i=0;i<size;i++)
4960 ptr[i] = vtop->c.tab[i];
4961 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4962 vtop->r |= VT_LVAL | VT_SYM;
4963 vtop->sym = sym;
4964 vtop->c.ul = 0;
4966 #ifdef CONFIG_TCC_BCHECK
4967 if (vtop->r & VT_MUSTBOUND)
4968 gbound();
4969 #endif
4971 r = vtop->r & VT_VALMASK;
4972 /* need to reload if:
4973 - constant
4974 - lvalue (need to dereference pointer)
4975 - already a register, but not in the right class */
4976 if (r >= VT_CONST ||
4977 (vtop->r & VT_LVAL) ||
4978 !(reg_classes[r] & rc) ||
4979 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4980 !(reg_classes[vtop->r2] & rc))) {
4981 r = get_reg(rc);
4982 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4983 /* two register type load : expand to two words
4984 temporarily */
4985 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4986 /* load constant */
4987 ll = vtop->c.ull;
4988 vtop->c.ui = ll; /* first word */
4989 load(r, vtop);
4990 vtop->r = r; /* save register value */
4991 vpushi(ll >> 32); /* second word */
4992 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
4993 (vtop->r & VT_LVAL)) {
4994 /* We do not want to modifier the long long
4995 pointer here, so the safest (and less
4996 efficient) is to save all the other registers
4997 in the stack. XXX: totally inefficient. */
4998 save_regs(1);
4999 /* load from memory */
5000 load(r, vtop);
5001 vdup();
5002 vtop[-1].r = r; /* save register value */
5003 /* increment pointer to get second word */
5004 vtop->type.t = VT_INT;
5005 gaddrof();
5006 vpushi(4);
5007 gen_op('+');
5008 vtop->r |= VT_LVAL;
5009 } else {
5010 /* move registers */
5011 load(r, vtop);
5012 vdup();
5013 vtop[-1].r = r; /* save register value */
5014 vtop->r = vtop[-1].r2;
5016 /* allocate second register */
5017 rc2 = RC_INT;
5018 if (rc == RC_IRET)
5019 rc2 = RC_LRET;
5020 r2 = get_reg(rc2);
5021 load(r2, vtop);
5022 vpop();
5023 /* write second register */
5024 vtop->r2 = r2;
5025 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5026 int t1, t;
5027 /* lvalue of scalar type : need to use lvalue type
5028 because of possible cast */
5029 t = vtop->type.t;
5030 t1 = t;
5031 /* compute memory access type */
5032 if (vtop->r & VT_LVAL_BYTE)
5033 t = VT_BYTE;
5034 else if (vtop->r & VT_LVAL_SHORT)
5035 t = VT_SHORT;
5036 if (vtop->r & VT_LVAL_UNSIGNED)
5037 t |= VT_UNSIGNED;
5038 vtop->type.t = t;
5039 load(r, vtop);
5040 /* restore wanted type */
5041 vtop->type.t = t1;
5042 } else {
5043 /* one register type load */
5044 load(r, vtop);
5047 vtop->r = r;
5048 #ifdef TCC_TARGET_C67
5049 /* uses register pairs for doubles */
5050 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5051 vtop->r2 = r+1;
5052 #endif
5054 return r;
5057 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5058 void gv2(int rc1, int rc2)
5060 int v;
5062 /* generate more generic register first. But VT_JMP or VT_CMP
5063 values must be generated first in all cases to avoid possible
5064 reload errors */
5065 v = vtop[0].r & VT_VALMASK;
5066 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5067 vswap();
5068 gv(rc1);
5069 vswap();
5070 gv(rc2);
5071 /* test if reload is needed for first register */
5072 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5073 vswap();
5074 gv(rc1);
5075 vswap();
5077 } else {
5078 gv(rc2);
5079 vswap();
5080 gv(rc1);
5081 vswap();
5082 /* test if reload is needed for first register */
5083 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5084 gv(rc2);
5089 /* expand long long on stack in two int registers */
5090 void lexpand(void)
5092 int u;
5094 u = vtop->type.t & VT_UNSIGNED;
5095 gv(RC_INT);
5096 vdup();
5097 vtop[0].r = vtop[-1].r2;
5098 vtop[0].r2 = VT_CONST;
5099 vtop[-1].r2 = VT_CONST;
5100 vtop[0].type.t = VT_INT | u;
5101 vtop[-1].type.t = VT_INT | u;
5104 #ifdef TCC_TARGET_ARM
5105 /* expand long long on stack */
5106 void lexpand_nr(void)
5108 int u,v;
5110 u = vtop->type.t & VT_UNSIGNED;
5111 vdup();
5112 vtop->r2 = VT_CONST;
5113 vtop->type.t = VT_INT | u;
5114 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5115 if (v == VT_CONST) {
5116 vtop[-1].c.ui = vtop->c.ull;
5117 vtop->c.ui = vtop->c.ull >> 32;
5118 vtop->r = VT_CONST;
5119 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5120 vtop->c.ui += 4;
5121 vtop->r = vtop[-1].r;
5122 } else if (v > VT_CONST) {
5123 vtop--;
5124 lexpand();
5125 } else
5126 vtop->r = vtop[-1].r2;
5127 vtop[-1].r2 = VT_CONST;
5128 vtop[-1].type.t = VT_INT | u;
5130 #endif
5132 /* build a long long from two ints */
5133 void lbuild(int t)
5135 gv2(RC_INT, RC_INT);
5136 vtop[-1].r2 = vtop[0].r;
5137 vtop[-1].type.t = t;
5138 vpop();
5141 /* rotate n first stack elements to the bottom
5142 I1 ... In -> I2 ... In I1 [top is right]
5144 void vrotb(int n)
5146 int i;
5147 SValue tmp;
5149 tmp = vtop[-n + 1];
5150 for(i=-n+1;i!=0;i++)
5151 vtop[i] = vtop[i+1];
5152 vtop[0] = tmp;
5155 /* rotate n first stack elements to the top
5156 I1 ... In -> In I1 ... I(n-1) [top is right]
5158 void vrott(int n)
5160 int i;
5161 SValue tmp;
5163 tmp = vtop[0];
5164 for(i = 0;i < n - 1; i++)
5165 vtop[-i] = vtop[-i - 1];
5166 vtop[-n + 1] = tmp;
5169 #ifdef TCC_TARGET_ARM
5170 /* like vrott but in other direction
5171 In ... I1 -> I(n-1) ... I1 In [top is right]
5173 void vnrott(int n)
5175 int i;
5176 SValue tmp;
5178 tmp = vtop[-n + 1];
5179 for(i = n - 1; i > 0; i--)
5180 vtop[-i] = vtop[-i + 1];
5181 vtop[0] = tmp;
5183 #endif
5185 /* pop stack value */
5186 void vpop(void)
5188 int v;
5189 v = vtop->r & VT_VALMASK;
5190 #ifdef TCC_TARGET_I386
5191 /* for x86, we need to pop the FP stack */
5192 if (v == TREG_ST0 && !nocode_wanted) {
5193 o(0xd9dd); /* fstp %st(1) */
5194 } else
5195 #endif
5196 if (v == VT_JMP || v == VT_JMPI) {
5197 /* need to put correct jump if && or || without test */
5198 gsym(vtop->c.ul);
5200 vtop--;
5203 /* convert stack entry to register and duplicate its value in another
5204 register */
5205 void gv_dup(void)
5207 int rc, t, r, r1;
5208 SValue sv;
5210 t = vtop->type.t;
5211 if ((t & VT_BTYPE) == VT_LLONG) {
5212 lexpand();
5213 gv_dup();
5214 vswap();
5215 vrotb(3);
5216 gv_dup();
5217 vrotb(4);
5218 /* stack: H L L1 H1 */
5219 lbuild(t);
5220 vrotb(3);
5221 vrotb(3);
5222 vswap();
5223 lbuild(t);
5224 vswap();
5225 } else {
5226 /* duplicate value */
5227 rc = RC_INT;
5228 sv.type.t = VT_INT;
5229 if (is_float(t)) {
5230 rc = RC_FLOAT;
5231 sv.type.t = t;
5233 r = gv(rc);
5234 r1 = get_reg(rc);
5235 sv.r = r;
5236 sv.c.ul = 0;
5237 load(r1, &sv); /* move r to r1 */
5238 vdup();
5239 /* duplicates value */
5240 vtop->r = r1;
5244 /* generate CPU independent (unsigned) long long operations */
5245 void gen_opl(int op)
5247 int t, a, b, op1, c, i;
5248 int func;
5249 SValue tmp;
5251 switch(op) {
5252 case '/':
5253 case TOK_PDIV:
5254 func = TOK___divdi3;
5255 goto gen_func;
5256 case TOK_UDIV:
5257 func = TOK___udivdi3;
5258 goto gen_func;
5259 case '%':
5260 func = TOK___moddi3;
5261 goto gen_func;
5262 case TOK_UMOD:
5263 func = TOK___umoddi3;
5264 gen_func:
5265 /* call generic long long function */
5266 vpush_global_sym(&func_old_type, func);
5267 vrott(3);
5268 gfunc_call(2);
5269 vpushi(0);
5270 vtop->r = REG_IRET;
5271 vtop->r2 = REG_LRET;
5272 break;
5273 case '^':
5274 case '&':
5275 case '|':
5276 case '*':
5277 case '+':
5278 case '-':
5279 t = vtop->type.t;
5280 vswap();
5281 lexpand();
5282 vrotb(3);
5283 lexpand();
5284 /* stack: L1 H1 L2 H2 */
5285 tmp = vtop[0];
5286 vtop[0] = vtop[-3];
5287 vtop[-3] = tmp;
5288 tmp = vtop[-2];
5289 vtop[-2] = vtop[-3];
5290 vtop[-3] = tmp;
5291 vswap();
5292 /* stack: H1 H2 L1 L2 */
5293 if (op == '*') {
5294 vpushv(vtop - 1);
5295 vpushv(vtop - 1);
5296 gen_op(TOK_UMULL);
5297 lexpand();
5298 /* stack: H1 H2 L1 L2 ML MH */
5299 for(i=0;i<4;i++)
5300 vrotb(6);
5301 /* stack: ML MH H1 H2 L1 L2 */
5302 tmp = vtop[0];
5303 vtop[0] = vtop[-2];
5304 vtop[-2] = tmp;
5305 /* stack: ML MH H1 L2 H2 L1 */
5306 gen_op('*');
5307 vrotb(3);
5308 vrotb(3);
5309 gen_op('*');
5310 /* stack: ML MH M1 M2 */
5311 gen_op('+');
5312 gen_op('+');
5313 } else if (op == '+' || op == '-') {
5314 /* XXX: add non carry method too (for MIPS or alpha) */
5315 if (op == '+')
5316 op1 = TOK_ADDC1;
5317 else
5318 op1 = TOK_SUBC1;
5319 gen_op(op1);
5320 /* stack: H1 H2 (L1 op L2) */
5321 vrotb(3);
5322 vrotb(3);
5323 gen_op(op1 + 1); /* TOK_xxxC2 */
5324 } else {
5325 gen_op(op);
5326 /* stack: H1 H2 (L1 op L2) */
5327 vrotb(3);
5328 vrotb(3);
5329 /* stack: (L1 op L2) H1 H2 */
5330 gen_op(op);
5331 /* stack: (L1 op L2) (H1 op H2) */
5333 /* stack: L H */
5334 lbuild(t);
5335 break;
5336 case TOK_SAR:
5337 case TOK_SHR:
5338 case TOK_SHL:
5339 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5340 t = vtop[-1].type.t;
5341 vswap();
5342 lexpand();
5343 vrotb(3);
5344 /* stack: L H shift */
5345 c = (int)vtop->c.i;
5346 /* constant: simpler */
5347 /* NOTE: all comments are for SHL. the other cases are
5348 done by swaping words */
5349 vpop();
5350 if (op != TOK_SHL)
5351 vswap();
5352 if (c >= 32) {
5353 /* stack: L H */
5354 vpop();
5355 if (c > 32) {
5356 vpushi(c - 32);
5357 gen_op(op);
5359 if (op != TOK_SAR) {
5360 vpushi(0);
5361 } else {
5362 gv_dup();
5363 vpushi(31);
5364 gen_op(TOK_SAR);
5366 vswap();
5367 } else {
5368 vswap();
5369 gv_dup();
5370 /* stack: H L L */
5371 vpushi(c);
5372 gen_op(op);
5373 vswap();
5374 vpushi(32 - c);
5375 if (op == TOK_SHL)
5376 gen_op(TOK_SHR);
5377 else
5378 gen_op(TOK_SHL);
5379 vrotb(3);
5380 /* stack: L L H */
5381 vpushi(c);
5382 if (op == TOK_SHL)
5383 gen_op(TOK_SHL);
5384 else
5385 gen_op(TOK_SHR);
5386 gen_op('|');
5388 if (op != TOK_SHL)
5389 vswap();
5390 lbuild(t);
5391 } else {
5392 /* XXX: should provide a faster fallback on x86 ? */
5393 switch(op) {
5394 case TOK_SAR:
5395 func = TOK___sardi3;
5396 goto gen_func;
5397 case TOK_SHR:
5398 func = TOK___shrdi3;
5399 goto gen_func;
5400 case TOK_SHL:
5401 func = TOK___shldi3;
5402 goto gen_func;
5405 break;
5406 default:
5407 /* compare operations */
5408 t = vtop->type.t;
5409 vswap();
5410 lexpand();
5411 vrotb(3);
5412 lexpand();
5413 /* stack: L1 H1 L2 H2 */
5414 tmp = vtop[-1];
5415 vtop[-1] = vtop[-2];
5416 vtop[-2] = tmp;
5417 /* stack: L1 L2 H1 H2 */
5418 /* compare high */
5419 op1 = op;
5420 /* when values are equal, we need to compare low words. since
5421 the jump is inverted, we invert the test too. */
5422 if (op1 == TOK_LT)
5423 op1 = TOK_LE;
5424 else if (op1 == TOK_GT)
5425 op1 = TOK_GE;
5426 else if (op1 == TOK_ULT)
5427 op1 = TOK_ULE;
5428 else if (op1 == TOK_UGT)
5429 op1 = TOK_UGE;
5430 a = 0;
5431 b = 0;
5432 gen_op(op1);
5433 if (op1 != TOK_NE) {
5434 a = gtst(1, 0);
5436 if (op != TOK_EQ) {
5437 /* generate non equal test */
5438 /* XXX: NOT PORTABLE yet */
5439 if (a == 0) {
5440 b = gtst(0, 0);
5441 } else {
5442 #if defined(TCC_TARGET_I386)
5443 b = psym(0x850f, 0);
5444 #elif defined(TCC_TARGET_ARM)
5445 b = ind;
5446 o(0x1A000000 | encbranch(ind, 0, 1));
5447 #elif defined(TCC_TARGET_C67)
5448 error("not implemented");
5449 #else
5450 #error not supported
5451 #endif
5454 /* compare low. Always unsigned */
5455 op1 = op;
5456 if (op1 == TOK_LT)
5457 op1 = TOK_ULT;
5458 else if (op1 == TOK_LE)
5459 op1 = TOK_ULE;
5460 else if (op1 == TOK_GT)
5461 op1 = TOK_UGT;
5462 else if (op1 == TOK_GE)
5463 op1 = TOK_UGE;
5464 gen_op(op1);
5465 a = gtst(1, a);
5466 gsym(b);
5467 vseti(VT_JMPI, a);
5468 break;
5472 /* handle integer constant optimizations and various machine
5473 independent opt */
5474 void gen_opic(int op)
5476 int c1, c2, t1, t2, n;
5477 SValue *v1, *v2;
5478 long long l1, l2;
5479 typedef unsigned long long U;
5481 v1 = vtop - 1;
5482 v2 = vtop;
5483 t1 = v1->type.t & VT_BTYPE;
5484 t2 = v2->type.t & VT_BTYPE;
5485 l1 = (t1 == VT_LLONG) ? v1->c.ll : v1->c.i;
5486 l2 = (t2 == VT_LLONG) ? v2->c.ll : v2->c.i;
5488 /* currently, we cannot do computations with forward symbols */
5489 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5490 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5491 if (c1 && c2) {
5492 switch(op) {
5493 case '+': l1 += l2; break;
5494 case '-': l1 -= l2; break;
5495 case '&': l1 &= l2; break;
5496 case '^': l1 ^= l2; break;
5497 case '|': l1 |= l2; break;
5498 case '*': l1 *= l2; break;
5500 case TOK_PDIV:
5501 case '/':
5502 case '%':
5503 case TOK_UDIV:
5504 case TOK_UMOD:
5505 /* if division by zero, generate explicit division */
5506 if (l2 == 0) {
5507 if (const_wanted)
5508 error("division by zero in constant");
5509 goto general_case;
5511 switch(op) {
5512 default: l1 /= l2; break;
5513 case '%': l1 %= l2; break;
5514 case TOK_UDIV: l1 = (U)l1 / l2; break;
5515 case TOK_UMOD: l1 = (U)l1 % l2; break;
5517 break;
5518 case TOK_SHL: l1 <<= l2; break;
5519 case TOK_SHR: l1 = (U)l1 >> l2; break;
5520 case TOK_SAR: l1 >>= l2; break;
5521 /* tests */
5522 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5523 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5524 case TOK_EQ: l1 = l1 == l2; break;
5525 case TOK_NE: l1 = l1 != l2; break;
5526 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5527 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5528 case TOK_LT: l1 = l1 < l2; break;
5529 case TOK_GE: l1 = l1 >= l2; break;
5530 case TOK_LE: l1 = l1 <= l2; break;
5531 case TOK_GT: l1 = l1 > l2; break;
5532 /* logical */
5533 case TOK_LAND: l1 = l1 && l2; break;
5534 case TOK_LOR: l1 = l1 || l2; break;
5535 default:
5536 goto general_case;
5538 v1->c.ll = l1;
5539 vtop--;
5540 } else {
5541 /* if commutative ops, put c2 as constant */
5542 if (c1 && (op == '+' || op == '&' || op == '^' ||
5543 op == '|' || op == '*')) {
5544 vswap();
5545 c2 = c1; //c = c1, c1 = c2, c2 = c;
5546 l2 = l1; //l = l1, l1 = l2, l2 = l;
5548 /* Filter out NOP operations like x*1, x-0, x&-1... */
5549 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5550 op == TOK_PDIV) &&
5551 l2 == 1) ||
5552 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5553 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5554 l2 == 0) ||
5555 (op == '&' &&
5556 l2 == -1))) {
5557 /* nothing to do */
5558 vtop--;
5559 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5560 /* try to use shifts instead of muls or divs */
5561 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5562 n = -1;
5563 while (l2) {
5564 l2 >>= 1;
5565 n++;
5567 vtop->c.ll = n;
5568 if (op == '*')
5569 op = TOK_SHL;
5570 else if (op == TOK_PDIV)
5571 op = TOK_SAR;
5572 else
5573 op = TOK_SHR;
5575 goto general_case;
5576 } else if (c2 && (op == '+' || op == '-') &&
5577 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5578 (VT_CONST | VT_SYM)) {
5579 /* symbol + constant case */
5580 if (op == '-')
5581 l2 = -l2;
5582 vtop--;
5583 vtop->c.ll += l2;
5584 } else {
5585 general_case:
5586 if (!nocode_wanted) {
5587 /* call low level op generator */
5588 if (t1 == VT_LLONG || t2 == VT_LLONG)
5589 gen_opl(op);
5590 else
5591 gen_opi(op);
5592 } else {
5593 vtop--;
5599 /* generate a floating point operation with constant propagation */
5600 void gen_opif(int op)
5602 int c1, c2;
5603 SValue *v1, *v2;
5604 long double f1, f2;
5606 v1 = vtop - 1;
5607 v2 = vtop;
5608 /* currently, we cannot do computations with forward symbols */
5609 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5610 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5611 if (c1 && c2) {
5612 if (v1->type.t == VT_FLOAT) {
5613 f1 = v1->c.f;
5614 f2 = v2->c.f;
5615 } else if (v1->type.t == VT_DOUBLE) {
5616 f1 = v1->c.d;
5617 f2 = v2->c.d;
5618 } else {
5619 f1 = v1->c.ld;
5620 f2 = v2->c.ld;
5623 /* NOTE: we only do constant propagation if finite number (not
5624 NaN or infinity) (ANSI spec) */
5625 if (!ieee_finite(f1) || !ieee_finite(f2))
5626 goto general_case;
5628 switch(op) {
5629 case '+': f1 += f2; break;
5630 case '-': f1 -= f2; break;
5631 case '*': f1 *= f2; break;
5632 case '/':
5633 if (f2 == 0.0) {
5634 if (const_wanted)
5635 error("division by zero in constant");
5636 goto general_case;
5638 f1 /= f2;
5639 break;
5640 /* XXX: also handles tests ? */
5641 default:
5642 goto general_case;
5644 /* XXX: overflow test ? */
5645 if (v1->type.t == VT_FLOAT) {
5646 v1->c.f = f1;
5647 } else if (v1->type.t == VT_DOUBLE) {
5648 v1->c.d = f1;
5649 } else {
5650 v1->c.ld = f1;
5652 vtop--;
5653 } else {
5654 general_case:
5655 if (!nocode_wanted) {
5656 gen_opf(op);
5657 } else {
5658 vtop--;
5663 static int pointed_size(CType *type)
5665 int align;
5666 return type_size(pointed_type(type), &align);
5669 static inline int is_null_pointer(SValue *p)
5671 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5672 return 0;
5673 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5674 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5677 static inline int is_integer_btype(int bt)
5679 return (bt == VT_BYTE || bt == VT_SHORT ||
5680 bt == VT_INT || bt == VT_LLONG);
5683 /* check types for comparison or substraction of pointers */
5684 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5686 CType *type1, *type2, tmp_type1, tmp_type2;
5687 int bt1, bt2;
5689 /* null pointers are accepted for all comparisons as gcc */
5690 if (is_null_pointer(p1) || is_null_pointer(p2))
5691 return;
5692 type1 = &p1->type;
5693 type2 = &p2->type;
5694 bt1 = type1->t & VT_BTYPE;
5695 bt2 = type2->t & VT_BTYPE;
5696 /* accept comparison between pointer and integer with a warning */
5697 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5698 if (op != TOK_LOR && op != TOK_LAND )
5699 warning("comparison between pointer and integer");
5700 return;
5703 /* both must be pointers or implicit function pointers */
5704 if (bt1 == VT_PTR) {
5705 type1 = pointed_type(type1);
5706 } else if (bt1 != VT_FUNC)
5707 goto invalid_operands;
5709 if (bt2 == VT_PTR) {
5710 type2 = pointed_type(type2);
5711 } else if (bt2 != VT_FUNC) {
5712 invalid_operands:
5713 error("invalid operands to binary %s", get_tok_str(op, NULL));
5715 if ((type1->t & VT_BTYPE) == VT_VOID ||
5716 (type2->t & VT_BTYPE) == VT_VOID)
5717 return;
5718 tmp_type1 = *type1;
5719 tmp_type2 = *type2;
5720 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5721 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5722 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5723 /* gcc-like error if '-' is used */
5724 if (op == '-')
5725 goto invalid_operands;
5726 else
5727 warning("comparison of distinct pointer types lacks a cast");
5731 /* generic gen_op: handles types problems */
5732 void gen_op(int op)
5734 int u, t1, t2, bt1, bt2, t;
5735 CType type1;
5737 t1 = vtop[-1].type.t;
5738 t2 = vtop[0].type.t;
5739 bt1 = t1 & VT_BTYPE;
5740 bt2 = t2 & VT_BTYPE;
5742 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5743 /* at least one operand is a pointer */
5744 /* relationnal op: must be both pointers */
5745 if (op >= TOK_ULT && op <= TOK_LOR) {
5746 check_comparison_pointer_types(vtop - 1, vtop, op);
5747 /* pointers are handled are unsigned */
5748 t = VT_INT | VT_UNSIGNED;
5749 goto std_op;
5751 /* if both pointers, then it must be the '-' op */
5752 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5753 if (op != '-')
5754 error("cannot use pointers here");
5755 check_comparison_pointer_types(vtop - 1, vtop, op);
5756 /* XXX: check that types are compatible */
5757 u = pointed_size(&vtop[-1].type);
5758 gen_opic(op);
5759 /* set to integer type */
5760 vtop->type.t = VT_INT;
5761 vpushi(u);
5762 gen_op(TOK_PDIV);
5763 } else {
5764 /* exactly one pointer : must be '+' or '-'. */
5765 if (op != '-' && op != '+')
5766 error("cannot use pointers here");
5767 /* Put pointer as first operand */
5768 if (bt2 == VT_PTR) {
5769 vswap();
5770 swap(&t1, &t2);
5772 type1 = vtop[-1].type;
5773 /* XXX: cast to int ? (long long case) */
5774 vpushi(pointed_size(&vtop[-1].type));
5775 gen_op('*');
5776 #ifdef CONFIG_TCC_BCHECK
5777 /* if evaluating constant expression, no code should be
5778 generated, so no bound check */
5779 if (do_bounds_check && !const_wanted) {
5780 /* if bounded pointers, we generate a special code to
5781 test bounds */
5782 if (op == '-') {
5783 vpushi(0);
5784 vswap();
5785 gen_op('-');
5787 gen_bounded_ptr_add();
5788 } else
5789 #endif
5791 gen_opic(op);
5793 /* put again type if gen_opic() swaped operands */
5794 vtop->type = type1;
5796 } else if (is_float(bt1) || is_float(bt2)) {
5797 /* compute bigger type and do implicit casts */
5798 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5799 t = VT_LDOUBLE;
5800 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5801 t = VT_DOUBLE;
5802 } else {
5803 t = VT_FLOAT;
5805 /* floats can only be used for a few operations */
5806 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5807 (op < TOK_ULT || op > TOK_GT))
5808 error("invalid operands for binary operation");
5809 goto std_op;
5810 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5811 /* cast to biggest op */
5812 t = VT_LLONG;
5813 /* convert to unsigned if it does not fit in a long long */
5814 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5815 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5816 t |= VT_UNSIGNED;
5817 goto std_op;
5818 } else {
5819 /* integer operations */
5820 t = VT_INT;
5821 /* convert to unsigned if it does not fit in an integer */
5822 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5823 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5824 t |= VT_UNSIGNED;
5825 std_op:
5826 /* XXX: currently, some unsigned operations are explicit, so
5827 we modify them here */
5828 if (t & VT_UNSIGNED) {
5829 if (op == TOK_SAR)
5830 op = TOK_SHR;
5831 else if (op == '/')
5832 op = TOK_UDIV;
5833 else if (op == '%')
5834 op = TOK_UMOD;
5835 else if (op == TOK_LT)
5836 op = TOK_ULT;
5837 else if (op == TOK_GT)
5838 op = TOK_UGT;
5839 else if (op == TOK_LE)
5840 op = TOK_ULE;
5841 else if (op == TOK_GE)
5842 op = TOK_UGE;
5844 vswap();
5845 type1.t = t;
5846 gen_cast(&type1);
5847 vswap();
5848 /* special case for shifts and long long: we keep the shift as
5849 an integer */
5850 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5851 type1.t = VT_INT;
5852 gen_cast(&type1);
5853 if (is_float(t))
5854 gen_opif(op);
5855 else
5856 gen_opic(op);
5857 if (op >= TOK_ULT && op <= TOK_GT) {
5858 /* relationnal op: the result is an int */
5859 vtop->type.t = VT_INT;
5860 } else {
5861 vtop->type.t = t;
5866 /* generic itof for unsigned long long case */
5867 void gen_cvt_itof1(int t)
5869 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5870 (VT_LLONG | VT_UNSIGNED)) {
5872 if (t == VT_FLOAT)
5873 vpush_global_sym(&func_old_type, TOK___ulltof);
5874 else if (t == VT_DOUBLE)
5875 vpush_global_sym(&func_old_type, TOK___ulltod);
5876 else
5877 vpush_global_sym(&func_old_type, TOK___ulltold);
5878 vrott(2);
5879 gfunc_call(1);
5880 vpushi(0);
5881 vtop->r = REG_FRET;
5882 } else {
5883 gen_cvt_itof(t);
5887 /* generic ftoi for unsigned long long case */
5888 void gen_cvt_ftoi1(int t)
5890 int st;
5892 if (t == (VT_LLONG | VT_UNSIGNED)) {
5893 /* not handled natively */
5894 st = vtop->type.t & VT_BTYPE;
5895 if (st == VT_FLOAT)
5896 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5897 else if (st == VT_DOUBLE)
5898 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5899 else
5900 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5901 vrott(2);
5902 gfunc_call(1);
5903 vpushi(0);
5904 vtop->r = REG_IRET;
5905 vtop->r2 = REG_LRET;
5906 } else {
5907 gen_cvt_ftoi(t);
5911 /* force char or short cast */
5912 void force_charshort_cast(int t)
5914 int bits, dbt;
5915 dbt = t & VT_BTYPE;
5916 /* XXX: add optimization if lvalue : just change type and offset */
5917 if (dbt == VT_BYTE)
5918 bits = 8;
5919 else
5920 bits = 16;
5921 if (t & VT_UNSIGNED) {
5922 vpushi((1 << bits) - 1);
5923 gen_op('&');
5924 } else {
5925 bits = 32 - bits;
5926 vpushi(bits);
5927 gen_op(TOK_SHL);
5928 /* result must be signed or the SAR is converted to an SHL
5929 This was not the case when "t" was a signed short
5930 and the last value on the stack was an unsigned int */
5931 vtop->type.t &= ~VT_UNSIGNED;
5932 vpushi(bits);
5933 gen_op(TOK_SAR);
5937 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5938 static void gen_cast(CType *type)
5940 int sbt, dbt, sf, df, c;
5942 /* special delayed cast for char/short */
5943 /* XXX: in some cases (multiple cascaded casts), it may still
5944 be incorrect */
5945 if (vtop->r & VT_MUSTCAST) {
5946 vtop->r &= ~VT_MUSTCAST;
5947 force_charshort_cast(vtop->type.t);
5950 /* bitfields first get cast to ints */
5951 if (vtop->type.t & VT_BITFIELD) {
5952 gv(RC_INT);
5955 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5956 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5958 if (sbt != dbt && !nocode_wanted) {
5959 sf = is_float(sbt);
5960 df = is_float(dbt);
5961 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5962 if (sf && df) {
5963 /* convert from fp to fp */
5964 if (c) {
5965 /* constant case: we can do it now */
5966 /* XXX: in ISOC, cannot do it if error in convert */
5967 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5968 vtop->c.f = (float)vtop->c.d;
5969 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5970 vtop->c.f = (float)vtop->c.ld;
5971 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5972 vtop->c.d = (double)vtop->c.f;
5973 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5974 vtop->c.d = (double)vtop->c.ld;
5975 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5976 vtop->c.ld = (long double)vtop->c.f;
5977 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5978 vtop->c.ld = (long double)vtop->c.d;
5979 } else {
5980 /* non constant case: generate code */
5981 gen_cvt_ftof(dbt);
5983 } else if (df) {
5984 /* convert int to fp */
5985 if (c) {
5986 switch(sbt) {
5987 case VT_LLONG | VT_UNSIGNED:
5988 case VT_LLONG:
5989 /* XXX: add const cases for long long */
5990 goto do_itof;
5991 case VT_INT | VT_UNSIGNED:
5992 switch(dbt) {
5993 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5994 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5995 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5997 break;
5998 default:
5999 switch(dbt) {
6000 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
6001 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
6002 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
6004 break;
6006 } else {
6007 do_itof:
6008 #if !defined(TCC_TARGET_ARM)
6009 gen_cvt_itof1(dbt);
6010 #else
6011 gen_cvt_itof(dbt);
6012 #endif
6014 } else if (sf) {
6015 /* convert fp to int */
6016 if (dbt == VT_BOOL) {
6017 vpushi(0);
6018 gen_op(TOK_NE);
6019 } else {
6020 /* we handle char/short/etc... with generic code */
6021 if (dbt != (VT_INT | VT_UNSIGNED) &&
6022 dbt != (VT_LLONG | VT_UNSIGNED) &&
6023 dbt != VT_LLONG)
6024 dbt = VT_INT;
6025 if (c) {
6026 switch(dbt) {
6027 case VT_LLONG | VT_UNSIGNED:
6028 case VT_LLONG:
6029 /* XXX: add const cases for long long */
6030 goto do_ftoi;
6031 case VT_INT | VT_UNSIGNED:
6032 switch(sbt) {
6033 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
6034 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
6035 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
6037 break;
6038 default:
6039 /* int case */
6040 switch(sbt) {
6041 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
6042 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
6043 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
6045 break;
6047 } else {
6048 do_ftoi:
6049 gen_cvt_ftoi1(dbt);
6051 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6052 /* additional cast for char/short... */
6053 vtop->type.t = dbt;
6054 gen_cast(type);
6057 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6058 if ((sbt & VT_BTYPE) != VT_LLONG) {
6059 /* scalar to long long */
6060 if (c) {
6061 if (sbt == (VT_INT | VT_UNSIGNED))
6062 vtop->c.ll = vtop->c.ui;
6063 else
6064 vtop->c.ll = vtop->c.i;
6065 } else {
6066 /* machine independent conversion */
6067 gv(RC_INT);
6068 /* generate high word */
6069 if (sbt == (VT_INT | VT_UNSIGNED)) {
6070 vpushi(0);
6071 gv(RC_INT);
6072 } else {
6073 gv_dup();
6074 vpushi(31);
6075 gen_op(TOK_SAR);
6077 /* patch second register */
6078 vtop[-1].r2 = vtop->r;
6079 vpop();
6082 } else if (dbt == VT_BOOL) {
6083 /* scalar to bool */
6084 vpushi(0);
6085 gen_op(TOK_NE);
6086 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6087 (dbt & VT_BTYPE) == VT_SHORT) {
6088 if (sbt == VT_PTR) {
6089 vtop->type.t = VT_INT;
6090 warning("nonportable conversion from pointer to char/short");
6092 force_charshort_cast(dbt);
6093 } else if ((dbt & VT_BTYPE) == VT_INT) {
6094 /* scalar to int */
6095 if (sbt == VT_LLONG) {
6096 /* from long long: just take low order word */
6097 lexpand();
6098 vpop();
6100 /* if lvalue and single word type, nothing to do because
6101 the lvalue already contains the real type size (see
6102 VT_LVAL_xxx constants) */
6104 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6105 /* if we are casting between pointer types,
6106 we must update the VT_LVAL_xxx size */
6107 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6108 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6110 vtop->type = *type;
6113 /* return type size. Put alignment at 'a' */
6114 static int type_size(CType *type, int *a)
6116 Sym *s;
6117 int bt;
6119 bt = type->t & VT_BTYPE;
6120 if (bt == VT_STRUCT) {
6121 /* struct/union */
6122 s = type->ref;
6123 *a = s->r;
6124 return s->c;
6125 } else if (bt == VT_PTR) {
6126 if (type->t & VT_ARRAY) {
6127 s = type->ref;
6128 return type_size(&s->type, a) * s->c;
6129 } else {
6130 *a = PTR_SIZE;
6131 return PTR_SIZE;
6133 } else if (bt == VT_LDOUBLE) {
6134 *a = LDOUBLE_ALIGN;
6135 return LDOUBLE_SIZE;
6136 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6137 #ifdef TCC_TARGET_I386
6138 *a = 4;
6139 #elif defined(TCC_TARGET_ARM)
6140 #ifdef TCC_ARM_EABI
6141 *a = 8;
6142 #else
6143 *a = 4;
6144 #endif
6145 #else
6146 *a = 8;
6147 #endif
6148 return 8;
6149 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6150 *a = 4;
6151 return 4;
6152 } else if (bt == VT_SHORT) {
6153 *a = 2;
6154 return 2;
6155 } else {
6156 /* char, void, function, _Bool */
6157 *a = 1;
6158 return 1;
6162 /* return the pointed type of t */
6163 static inline CType *pointed_type(CType *type)
6165 return &type->ref->type;
6168 /* modify type so that its it is a pointer to type. */
6169 static void mk_pointer(CType *type)
6171 Sym *s;
6172 s = sym_push(SYM_FIELD, type, 0, -1);
6173 type->t = VT_PTR | (type->t & ~VT_TYPE);
6174 type->ref = s;
6177 /* compare function types. OLD functions match any new functions */
6178 static int is_compatible_func(CType *type1, CType *type2)
6180 Sym *s1, *s2;
6182 s1 = type1->ref;
6183 s2 = type2->ref;
6184 if (!is_compatible_types(&s1->type, &s2->type))
6185 return 0;
6186 /* check func_call */
6187 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6188 return 0;
6189 /* XXX: not complete */
6190 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6191 return 1;
6192 if (s1->c != s2->c)
6193 return 0;
6194 while (s1 != NULL) {
6195 if (s2 == NULL)
6196 return 0;
6197 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6198 return 0;
6199 s1 = s1->next;
6200 s2 = s2->next;
6202 if (s2)
6203 return 0;
6204 return 1;
6207 /* return true if type1 and type2 are the same. If unqualified is
6208 true, qualifiers on the types are ignored.
6210 - enums are not checked as gcc __builtin_types_compatible_p ()
6212 static int compare_types(CType *type1, CType *type2, int unqualified)
6214 int bt1, t1, t2;
6216 t1 = type1->t & VT_TYPE;
6217 t2 = type2->t & VT_TYPE;
6218 if (unqualified) {
6219 /* strip qualifiers before comparing */
6220 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6221 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6223 /* XXX: bitfields ? */
6224 if (t1 != t2)
6225 return 0;
6226 /* test more complicated cases */
6227 bt1 = t1 & VT_BTYPE;
6228 if (bt1 == VT_PTR) {
6229 type1 = pointed_type(type1);
6230 type2 = pointed_type(type2);
6231 return is_compatible_types(type1, type2);
6232 } else if (bt1 == VT_STRUCT) {
6233 return (type1->ref == type2->ref);
6234 } else if (bt1 == VT_FUNC) {
6235 return is_compatible_func(type1, type2);
6236 } else {
6237 return 1;
6241 /* return true if type1 and type2 are exactly the same (including
6242 qualifiers).
6244 static int is_compatible_types(CType *type1, CType *type2)
6246 return compare_types(type1,type2,0);
6249 /* return true if type1 and type2 are the same (ignoring qualifiers).
6251 static int is_compatible_parameter_types(CType *type1, CType *type2)
6253 return compare_types(type1,type2,1);
6256 /* print a type. If 'varstr' is not NULL, then the variable is also
6257 printed in the type */
6258 /* XXX: union */
6259 /* XXX: add array and function pointers */
6260 void type_to_str(char *buf, int buf_size,
6261 CType *type, const char *varstr)
6263 int bt, v, t;
6264 Sym *s, *sa;
6265 char buf1[256];
6266 const char *tstr;
6268 t = type->t & VT_TYPE;
6269 bt = t & VT_BTYPE;
6270 buf[0] = '\0';
6271 if (t & VT_CONSTANT)
6272 pstrcat(buf, buf_size, "const ");
6273 if (t & VT_VOLATILE)
6274 pstrcat(buf, buf_size, "volatile ");
6275 if (t & VT_UNSIGNED)
6276 pstrcat(buf, buf_size, "unsigned ");
6277 switch(bt) {
6278 case VT_VOID:
6279 tstr = "void";
6280 goto add_tstr;
6281 case VT_BOOL:
6282 tstr = "_Bool";
6283 goto add_tstr;
6284 case VT_BYTE:
6285 tstr = "char";
6286 goto add_tstr;
6287 case VT_SHORT:
6288 tstr = "short";
6289 goto add_tstr;
6290 case VT_INT:
6291 tstr = "int";
6292 goto add_tstr;
6293 case VT_LONG:
6294 tstr = "long";
6295 goto add_tstr;
6296 case VT_LLONG:
6297 tstr = "long long";
6298 goto add_tstr;
6299 case VT_FLOAT:
6300 tstr = "float";
6301 goto add_tstr;
6302 case VT_DOUBLE:
6303 tstr = "double";
6304 goto add_tstr;
6305 case VT_LDOUBLE:
6306 tstr = "long double";
6307 add_tstr:
6308 pstrcat(buf, buf_size, tstr);
6309 break;
6310 case VT_ENUM:
6311 case VT_STRUCT:
6312 if (bt == VT_STRUCT)
6313 tstr = "struct ";
6314 else
6315 tstr = "enum ";
6316 pstrcat(buf, buf_size, tstr);
6317 v = type->ref->v & ~SYM_STRUCT;
6318 if (v >= SYM_FIRST_ANOM)
6319 pstrcat(buf, buf_size, "<anonymous>");
6320 else
6321 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6322 break;
6323 case VT_FUNC:
6324 s = type->ref;
6325 type_to_str(buf, buf_size, &s->type, varstr);
6326 pstrcat(buf, buf_size, "(");
6327 sa = s->next;
6328 while (sa != NULL) {
6329 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6330 pstrcat(buf, buf_size, buf1);
6331 sa = sa->next;
6332 if (sa)
6333 pstrcat(buf, buf_size, ", ");
6335 pstrcat(buf, buf_size, ")");
6336 goto no_var;
6337 case VT_PTR:
6338 s = type->ref;
6339 pstrcpy(buf1, sizeof(buf1), "*");
6340 if (varstr)
6341 pstrcat(buf1, sizeof(buf1), varstr);
6342 type_to_str(buf, buf_size, &s->type, buf1);
6343 goto no_var;
6345 if (varstr) {
6346 pstrcat(buf, buf_size, " ");
6347 pstrcat(buf, buf_size, varstr);
6349 no_var: ;
6352 /* verify type compatibility to store vtop in 'dt' type, and generate
6353 casts if needed. */
6354 static void gen_assign_cast(CType *dt)
6356 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6357 char buf1[256], buf2[256];
6358 int dbt, sbt;
6360 st = &vtop->type; /* source type */
6361 dbt = dt->t & VT_BTYPE;
6362 sbt = st->t & VT_BTYPE;
6363 if (dt->t & VT_CONSTANT)
6364 warning("assignment of read-only location");
6365 switch(dbt) {
6366 case VT_PTR:
6367 /* special cases for pointers */
6368 /* '0' can also be a pointer */
6369 if (is_null_pointer(vtop))
6370 goto type_ok;
6371 /* accept implicit pointer to integer cast with warning */
6372 if (is_integer_btype(sbt)) {
6373 warning("assignment makes pointer from integer without a cast");
6374 goto type_ok;
6376 type1 = pointed_type(dt);
6377 /* a function is implicitely a function pointer */
6378 if (sbt == VT_FUNC) {
6379 if ((type1->t & VT_BTYPE) != VT_VOID &&
6380 !is_compatible_types(pointed_type(dt), st))
6381 goto error;
6382 else
6383 goto type_ok;
6385 if (sbt != VT_PTR)
6386 goto error;
6387 type2 = pointed_type(st);
6388 if ((type1->t & VT_BTYPE) == VT_VOID ||
6389 (type2->t & VT_BTYPE) == VT_VOID) {
6390 /* void * can match anything */
6391 } else {
6392 /* exact type match, except for unsigned */
6393 tmp_type1 = *type1;
6394 tmp_type2 = *type2;
6395 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6396 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6397 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6398 warning("assignment from incompatible pointer type");
6400 /* check const and volatile */
6401 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6402 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6403 warning("assignment discards qualifiers from pointer target type");
6404 break;
6405 case VT_BYTE:
6406 case VT_SHORT:
6407 case VT_INT:
6408 case VT_LLONG:
6409 if (sbt == VT_PTR || sbt == VT_FUNC) {
6410 warning("assignment makes integer from pointer without a cast");
6412 /* XXX: more tests */
6413 break;
6414 case VT_STRUCT:
6415 tmp_type1 = *dt;
6416 tmp_type2 = *st;
6417 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6418 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6419 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6420 error:
6421 type_to_str(buf1, sizeof(buf1), st, NULL);
6422 type_to_str(buf2, sizeof(buf2), dt, NULL);
6423 error("cannot cast '%s' to '%s'", buf1, buf2);
6425 break;
6427 type_ok:
6428 gen_cast(dt);
6431 /* store vtop in lvalue pushed on stack */
6432 void vstore(void)
6434 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6436 ft = vtop[-1].type.t;
6437 sbt = vtop->type.t & VT_BTYPE;
6438 dbt = ft & VT_BTYPE;
6439 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6440 (sbt == VT_INT && dbt == VT_SHORT)) {
6441 /* optimize char/short casts */
6442 delayed_cast = VT_MUSTCAST;
6443 vtop->type.t = ft & VT_TYPE;
6444 /* XXX: factorize */
6445 if (ft & VT_CONSTANT)
6446 warning("assignment of read-only location");
6447 } else {
6448 delayed_cast = 0;
6449 if (!(ft & VT_BITFIELD))
6450 gen_assign_cast(&vtop[-1].type);
6453 if (sbt == VT_STRUCT) {
6454 /* if structure, only generate pointer */
6455 /* structure assignment : generate memcpy */
6456 /* XXX: optimize if small size */
6457 if (!nocode_wanted) {
6458 size = type_size(&vtop->type, &align);
6460 #ifdef TCC_ARM_EABI
6461 if(!(align & 7))
6462 vpush_global_sym(&func_old_type, TOK_memcpy8);
6463 else if(!(align & 3))
6464 vpush_global_sym(&func_old_type, TOK_memcpy4);
6465 else
6466 #endif
6467 vpush_global_sym(&func_old_type, TOK_memcpy);
6469 /* destination */
6470 vpushv(vtop - 2);
6471 vtop->type.t = VT_INT;
6472 gaddrof();
6473 /* source */
6474 vpushv(vtop - 2);
6475 vtop->type.t = VT_INT;
6476 gaddrof();
6477 /* type size */
6478 vpushi(size);
6479 gfunc_call(3);
6481 vswap();
6482 vpop();
6483 } else {
6484 vswap();
6485 vpop();
6487 /* leave source on stack */
6488 } else if (ft & VT_BITFIELD) {
6489 /* bitfield store handling */
6490 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6491 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6492 /* remove bit field info to avoid loops */
6493 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6495 /* duplicate source into other register */
6496 gv_dup();
6497 vswap();
6498 vrott(3);
6500 /* duplicate destination */
6501 vdup();
6502 vtop[-1] = vtop[-2];
6504 /* mask and shift source */
6505 vpushi((1 << bit_size) - 1);
6506 gen_op('&');
6507 vpushi(bit_pos);
6508 gen_op(TOK_SHL);
6509 /* load destination, mask and or with source */
6510 vswap();
6511 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6512 gen_op('&');
6513 gen_op('|');
6514 /* store result */
6515 vstore();
6517 /* pop off shifted source from "duplicate source..." above */
6518 vpop();
6520 } else {
6521 #ifdef CONFIG_TCC_BCHECK
6522 /* bound check case */
6523 if (vtop[-1].r & VT_MUSTBOUND) {
6524 vswap();
6525 gbound();
6526 vswap();
6528 #endif
6529 if (!nocode_wanted) {
6530 rc = RC_INT;
6531 if (is_float(ft))
6532 rc = RC_FLOAT;
6533 r = gv(rc); /* generate value */
6534 /* if lvalue was saved on stack, must read it */
6535 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6536 SValue sv;
6537 t = get_reg(RC_INT);
6538 sv.type.t = VT_INT;
6539 sv.r = VT_LOCAL | VT_LVAL;
6540 sv.c.ul = vtop[-1].c.ul;
6541 load(t, &sv);
6542 vtop[-1].r = t | VT_LVAL;
6544 store(r, vtop - 1);
6545 /* two word case handling : store second register at word + 4 */
6546 if ((ft & VT_BTYPE) == VT_LLONG) {
6547 vswap();
6548 /* convert to int to increment easily */
6549 vtop->type.t = VT_INT;
6550 gaddrof();
6551 vpushi(4);
6552 gen_op('+');
6553 vtop->r |= VT_LVAL;
6554 vswap();
6555 /* XXX: it works because r2 is spilled last ! */
6556 store(vtop->r2, vtop - 1);
6559 vswap();
6560 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6561 vtop->r |= delayed_cast;
6565 /* post defines POST/PRE add. c is the token ++ or -- */
6566 void inc(int post, int c)
6568 test_lvalue();
6569 vdup(); /* save lvalue */
6570 if (post) {
6571 gv_dup(); /* duplicate value */
6572 vrotb(3);
6573 vrotb(3);
6575 /* add constant */
6576 vpushi(c - TOK_MID);
6577 gen_op('+');
6578 vstore(); /* store value */
6579 if (post)
6580 vpop(); /* if post op, return saved value */
6583 /* Parse GNUC __attribute__ extension. Currently, the following
6584 extensions are recognized:
6585 - aligned(n) : set data/function alignment.
6586 - packed : force data alignment to 1
6587 - section(x) : generate data/code in this section.
6588 - unused : currently ignored, but may be used someday.
6589 - regparm(n) : pass function parameters in registers (i386 only)
6591 static void parse_attribute(AttributeDef *ad)
6593 int t, n;
6595 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6596 next();
6597 skip('(');
6598 skip('(');
6599 while (tok != ')') {
6600 if (tok < TOK_IDENT)
6601 expect("attribute name");
6602 t = tok;
6603 next();
6604 switch(t) {
6605 case TOK_SECTION1:
6606 case TOK_SECTION2:
6607 skip('(');
6608 if (tok != TOK_STR)
6609 expect("section name");
6610 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6611 next();
6612 skip(')');
6613 break;
6614 case TOK_ALIGNED1:
6615 case TOK_ALIGNED2:
6616 if (tok == '(') {
6617 next();
6618 n = expr_const();
6619 if (n <= 0 || (n & (n - 1)) != 0)
6620 error("alignment must be a positive power of two");
6621 skip(')');
6622 } else {
6623 n = MAX_ALIGN;
6625 ad->aligned = n;
6626 break;
6627 case TOK_PACKED1:
6628 case TOK_PACKED2:
6629 ad->packed = 1;
6630 break;
6631 case TOK_UNUSED1:
6632 case TOK_UNUSED2:
6633 /* currently, no need to handle it because tcc does not
6634 track unused objects */
6635 break;
6636 case TOK_NORETURN1:
6637 case TOK_NORETURN2:
6638 /* currently, no need to handle it because tcc does not
6639 track unused objects */
6640 break;
6641 case TOK_CDECL1:
6642 case TOK_CDECL2:
6643 case TOK_CDECL3:
6644 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6645 break;
6646 case TOK_STDCALL1:
6647 case TOK_STDCALL2:
6648 case TOK_STDCALL3:
6649 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6650 break;
6651 #ifdef TCC_TARGET_I386
6652 case TOK_REGPARM1:
6653 case TOK_REGPARM2:
6654 skip('(');
6655 n = expr_const();
6656 if (n > 3)
6657 n = 3;
6658 else if (n < 0)
6659 n = 0;
6660 if (n > 0)
6661 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6662 skip(')');
6663 break;
6664 case TOK_FASTCALL1:
6665 case TOK_FASTCALL2:
6666 case TOK_FASTCALL3:
6667 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6668 break;
6669 #endif
6670 case TOK_DLLEXPORT:
6671 FUNC_EXPORT(ad->func_attr) = 1;
6672 break;
6673 default:
6674 if (tcc_state->warn_unsupported)
6675 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6676 /* skip parameters */
6677 if (tok == '(') {
6678 int parenthesis = 0;
6679 do {
6680 if (tok == '(')
6681 parenthesis++;
6682 else if (tok == ')')
6683 parenthesis--;
6684 next();
6685 } while (parenthesis && tok != -1);
6687 break;
6689 if (tok != ',')
6690 break;
6691 next();
6693 skip(')');
6694 skip(')');
6698 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6699 static void struct_decl(CType *type, int u)
6701 int a, v, size, align, maxalign, c, offset;
6702 int bit_size, bit_pos, bsize, bt, lbit_pos;
6703 Sym *s, *ss, *ass, **ps;
6704 AttributeDef ad;
6705 CType type1, btype;
6707 a = tok; /* save decl type */
6708 next();
6709 if (tok != '{') {
6710 v = tok;
6711 next();
6712 /* struct already defined ? return it */
6713 if (v < TOK_IDENT)
6714 expect("struct/union/enum name");
6715 s = struct_find(v);
6716 if (s) {
6717 if (s->type.t != a)
6718 error("invalid type");
6719 goto do_decl;
6721 } else {
6722 v = anon_sym++;
6724 type1.t = a;
6725 /* we put an undefined size for struct/union */
6726 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6727 s->r = 0; /* default alignment is zero as gcc */
6728 /* put struct/union/enum name in type */
6729 do_decl:
6730 type->t = u;
6731 type->ref = s;
6733 if (tok == '{') {
6734 next();
6735 if (s->c != -1)
6736 error("struct/union/enum already defined");
6737 /* cannot be empty */
6738 c = 0;
6739 /* non empty enums are not allowed */
6740 if (a == TOK_ENUM) {
6741 for(;;) {
6742 v = tok;
6743 if (v < TOK_UIDENT)
6744 expect("identifier");
6745 next();
6746 if (tok == '=') {
6747 next();
6748 c = expr_const();
6750 /* enum symbols have static storage */
6751 ss = sym_push(v, &int_type, VT_CONST, c);
6752 ss->type.t |= VT_STATIC;
6753 if (tok != ',')
6754 break;
6755 next();
6756 c++;
6757 /* NOTE: we accept a trailing comma */
6758 if (tok == '}')
6759 break;
6761 skip('}');
6762 } else {
6763 maxalign = 1;
6764 ps = &s->next;
6765 bit_pos = 0;
6766 offset = 0;
6767 while (tok != '}') {
6768 parse_btype(&btype, &ad);
6769 while (1) {
6770 bit_size = -1;
6771 v = 0;
6772 type1 = btype;
6773 if (tok != ':') {
6774 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6775 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6776 expect("identifier");
6777 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6778 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6779 error("invalid type for '%s'",
6780 get_tok_str(v, NULL));
6782 if (tok == ':') {
6783 next();
6784 bit_size = expr_const();
6785 /* XXX: handle v = 0 case for messages */
6786 if (bit_size < 0)
6787 error("negative width in bit-field '%s'",
6788 get_tok_str(v, NULL));
6789 if (v && bit_size == 0)
6790 error("zero width for bit-field '%s'",
6791 get_tok_str(v, NULL));
6793 size = type_size(&type1, &align);
6794 if (ad.aligned) {
6795 if (align < ad.aligned)
6796 align = ad.aligned;
6797 } else if (ad.packed) {
6798 align = 1;
6799 } else if (*tcc_state->pack_stack_ptr) {
6800 if (align > *tcc_state->pack_stack_ptr)
6801 align = *tcc_state->pack_stack_ptr;
6803 lbit_pos = 0;
6804 if (bit_size >= 0) {
6805 bt = type1.t & VT_BTYPE;
6806 if (bt != VT_INT &&
6807 bt != VT_BYTE &&
6808 bt != VT_SHORT &&
6809 bt != VT_BOOL &&
6810 bt != VT_ENUM)
6811 error("bitfields must have scalar type");
6812 bsize = size * 8;
6813 if (bit_size > bsize) {
6814 error("width of '%s' exceeds its type",
6815 get_tok_str(v, NULL));
6816 } else if (bit_size == bsize) {
6817 /* no need for bit fields */
6818 bit_pos = 0;
6819 } else if (bit_size == 0) {
6820 /* XXX: what to do if only padding in a
6821 structure ? */
6822 /* zero size: means to pad */
6823 if (bit_pos > 0)
6824 bit_pos = bsize;
6825 } else {
6826 /* we do not have enough room ? */
6827 if ((bit_pos + bit_size) > bsize)
6828 bit_pos = 0;
6829 lbit_pos = bit_pos;
6830 /* XXX: handle LSB first */
6831 type1.t |= VT_BITFIELD |
6832 (bit_pos << VT_STRUCT_SHIFT) |
6833 (bit_size << (VT_STRUCT_SHIFT + 6));
6834 bit_pos += bit_size;
6836 } else {
6837 bit_pos = 0;
6839 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
6840 /* add new memory data only if starting
6841 bit field */
6842 if (lbit_pos == 0) {
6843 if (a == TOK_STRUCT) {
6844 c = (c + align - 1) & -align;
6845 offset = c;
6846 if (size > 0)
6847 c += size;
6848 } else {
6849 offset = 0;
6850 if (size > c)
6851 c = size;
6853 if (align > maxalign)
6854 maxalign = align;
6856 #if 0
6857 printf("add field %s offset=%d",
6858 get_tok_str(v, NULL), offset);
6859 if (type1.t & VT_BITFIELD) {
6860 printf(" pos=%d size=%d",
6861 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6862 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6864 printf("\n");
6865 #endif
6867 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
6868 ass = type1.ref;
6869 while ((ass = ass->next) != NULL) {
6870 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
6871 *ps = ss;
6872 ps = &ss->next;
6874 } else if (v) {
6875 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6876 *ps = ss;
6877 ps = &ss->next;
6879 if (tok == ';' || tok == TOK_EOF)
6880 break;
6881 skip(',');
6883 skip(';');
6885 skip('}');
6886 /* store size and alignment */
6887 s->c = (c + maxalign - 1) & -maxalign;
6888 s->r = maxalign;
6893 /* return 0 if no type declaration. otherwise, return the basic type
6894 and skip it.
6896 static int parse_btype(CType *type, AttributeDef *ad)
6898 int t, u, type_found, typespec_found, typedef_found;
6899 Sym *s;
6900 CType type1;
6902 memset(ad, 0, sizeof(AttributeDef));
6903 type_found = 0;
6904 typespec_found = 0;
6905 typedef_found = 0;
6906 t = 0;
6907 while(1) {
6908 switch(tok) {
6909 case TOK_EXTENSION:
6910 /* currently, we really ignore extension */
6911 next();
6912 continue;
6914 /* basic types */
6915 case TOK_CHAR:
6916 u = VT_BYTE;
6917 basic_type:
6918 next();
6919 basic_type1:
6920 if ((t & VT_BTYPE) != 0)
6921 error("too many basic types");
6922 t |= u;
6923 typespec_found = 1;
6924 break;
6925 case TOK_VOID:
6926 u = VT_VOID;
6927 goto basic_type;
6928 case TOK_SHORT:
6929 u = VT_SHORT;
6930 goto basic_type;
6931 case TOK_INT:
6932 next();
6933 typespec_found = 1;
6934 break;
6935 case TOK_LONG:
6936 next();
6937 if ((t & VT_BTYPE) == VT_DOUBLE) {
6938 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6939 } else if ((t & VT_BTYPE) == VT_LONG) {
6940 t = (t & ~VT_BTYPE) | VT_LLONG;
6941 } else {
6942 u = VT_LONG;
6943 goto basic_type1;
6945 break;
6946 case TOK_BOOL:
6947 u = VT_BOOL;
6948 goto basic_type;
6949 case TOK_FLOAT:
6950 u = VT_FLOAT;
6951 goto basic_type;
6952 case TOK_DOUBLE:
6953 next();
6954 if ((t & VT_BTYPE) == VT_LONG) {
6955 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6956 } else {
6957 u = VT_DOUBLE;
6958 goto basic_type1;
6960 break;
6961 case TOK_ENUM:
6962 struct_decl(&type1, VT_ENUM);
6963 basic_type2:
6964 u = type1.t;
6965 type->ref = type1.ref;
6966 goto basic_type1;
6967 case TOK_STRUCT:
6968 case TOK_UNION:
6969 struct_decl(&type1, VT_STRUCT);
6970 goto basic_type2;
6972 /* type modifiers */
6973 case TOK_CONST1:
6974 case TOK_CONST2:
6975 case TOK_CONST3:
6976 t |= VT_CONSTANT;
6977 next();
6978 break;
6979 case TOK_VOLATILE1:
6980 case TOK_VOLATILE2:
6981 case TOK_VOLATILE3:
6982 t |= VT_VOLATILE;
6983 next();
6984 break;
6985 case TOK_SIGNED1:
6986 case TOK_SIGNED2:
6987 case TOK_SIGNED3:
6988 typespec_found = 1;
6989 t |= VT_SIGNED;
6990 next();
6991 break;
6992 case TOK_REGISTER:
6993 case TOK_AUTO:
6994 case TOK_RESTRICT1:
6995 case TOK_RESTRICT2:
6996 case TOK_RESTRICT3:
6997 next();
6998 break;
6999 case TOK_UNSIGNED:
7000 t |= VT_UNSIGNED;
7001 next();
7002 typespec_found = 1;
7003 break;
7005 /* storage */
7006 case TOK_EXTERN:
7007 t |= VT_EXTERN;
7008 next();
7009 break;
7010 case TOK_STATIC:
7011 t |= VT_STATIC;
7012 next();
7013 break;
7014 case TOK_TYPEDEF:
7015 t |= VT_TYPEDEF;
7016 next();
7017 break;
7018 case TOK_INLINE1:
7019 case TOK_INLINE2:
7020 case TOK_INLINE3:
7021 t |= VT_INLINE;
7022 next();
7023 break;
7025 /* GNUC attribute */
7026 case TOK_ATTRIBUTE1:
7027 case TOK_ATTRIBUTE2:
7028 parse_attribute(ad);
7029 break;
7030 /* GNUC typeof */
7031 case TOK_TYPEOF1:
7032 case TOK_TYPEOF2:
7033 case TOK_TYPEOF3:
7034 next();
7035 parse_expr_type(&type1);
7036 goto basic_type2;
7037 default:
7038 if (typespec_found || typedef_found)
7039 goto the_end;
7040 s = sym_find(tok);
7041 if (!s || !(s->type.t & VT_TYPEDEF))
7042 goto the_end;
7043 typedef_found = 1;
7044 t |= (s->type.t & ~VT_TYPEDEF);
7045 type->ref = s->type.ref;
7046 next();
7047 typespec_found = 1;
7048 break;
7050 type_found = 1;
7052 the_end:
7053 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7054 error("signed and unsigned modifier");
7055 if (tcc_state->char_is_unsigned) {
7056 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7057 t |= VT_UNSIGNED;
7059 t &= ~VT_SIGNED;
7061 /* long is never used as type */
7062 if ((t & VT_BTYPE) == VT_LONG)
7063 t = (t & ~VT_BTYPE) | VT_INT;
7064 type->t = t;
7065 return type_found;
7068 /* convert a function parameter type (array to pointer and function to
7069 function pointer) */
7070 static inline void convert_parameter_type(CType *pt)
7072 /* remove const and volatile qualifiers (XXX: const could be used
7073 to indicate a const function parameter */
7074 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7075 /* array must be transformed to pointer according to ANSI C */
7076 pt->t &= ~VT_ARRAY;
7077 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7078 mk_pointer(pt);
7082 static void post_type(CType *type, AttributeDef *ad)
7084 int n, l, t1, arg_size, align;
7085 Sym **plast, *s, *first;
7086 AttributeDef ad1;
7087 CType pt;
7089 if (tok == '(') {
7090 /* function declaration */
7091 next();
7092 l = 0;
7093 first = NULL;
7094 plast = &first;
7095 arg_size = 0;
7096 if (tok != ')') {
7097 for(;;) {
7098 /* read param name and compute offset */
7099 if (l != FUNC_OLD) {
7100 if (!parse_btype(&pt, &ad1)) {
7101 if (l) {
7102 error("invalid type");
7103 } else {
7104 l = FUNC_OLD;
7105 goto old_proto;
7108 l = FUNC_NEW;
7109 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7110 break;
7111 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7112 if ((pt.t & VT_BTYPE) == VT_VOID)
7113 error("parameter declared as void");
7114 arg_size += (type_size(&pt, &align) + 3) & ~3;
7115 } else {
7116 old_proto:
7117 n = tok;
7118 if (n < TOK_UIDENT)
7119 expect("identifier");
7120 pt.t = VT_INT;
7121 next();
7123 convert_parameter_type(&pt);
7124 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7125 *plast = s;
7126 plast = &s->next;
7127 if (tok == ')')
7128 break;
7129 skip(',');
7130 if (l == FUNC_NEW && tok == TOK_DOTS) {
7131 l = FUNC_ELLIPSIS;
7132 next();
7133 break;
7137 /* if no parameters, then old type prototype */
7138 if (l == 0)
7139 l = FUNC_OLD;
7140 skip(')');
7141 t1 = type->t & VT_STORAGE;
7142 /* NOTE: const is ignored in returned type as it has a special
7143 meaning in gcc / C++ */
7144 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7145 post_type(type, ad);
7146 /* we push a anonymous symbol which will contain the function prototype */
7147 FUNC_ARGS(ad->func_attr) = arg_size;
7148 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7149 s->next = first;
7150 type->t = t1 | VT_FUNC;
7151 type->ref = s;
7152 } else if (tok == '[') {
7153 /* array definition */
7154 next();
7155 n = -1;
7156 if (tok != ']') {
7157 n = expr_const();
7158 if (n < 0)
7159 error("invalid array size");
7161 skip(']');
7162 /* parse next post type */
7163 t1 = type->t & VT_STORAGE;
7164 type->t &= ~VT_STORAGE;
7165 post_type(type, ad);
7167 /* we push a anonymous symbol which will contain the array
7168 element type */
7169 s = sym_push(SYM_FIELD, type, 0, n);
7170 type->t = t1 | VT_ARRAY | VT_PTR;
7171 type->ref = s;
7175 /* Parse a type declaration (except basic type), and return the type
7176 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7177 expected. 'type' should contain the basic type. 'ad' is the
7178 attribute definition of the basic type. It can be modified by
7179 type_decl().
7181 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7183 Sym *s;
7184 CType type1, *type2;
7185 int qualifiers;
7187 while (tok == '*') {
7188 qualifiers = 0;
7189 redo:
7190 next();
7191 switch(tok) {
7192 case TOK_CONST1:
7193 case TOK_CONST2:
7194 case TOK_CONST3:
7195 qualifiers |= VT_CONSTANT;
7196 goto redo;
7197 case TOK_VOLATILE1:
7198 case TOK_VOLATILE2:
7199 case TOK_VOLATILE3:
7200 qualifiers |= VT_VOLATILE;
7201 goto redo;
7202 case TOK_RESTRICT1:
7203 case TOK_RESTRICT2:
7204 case TOK_RESTRICT3:
7205 goto redo;
7207 mk_pointer(type);
7208 type->t |= qualifiers;
7211 /* XXX: clarify attribute handling */
7212 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7213 parse_attribute(ad);
7215 /* recursive type */
7216 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7217 type1.t = 0; /* XXX: same as int */
7218 if (tok == '(') {
7219 next();
7220 /* XXX: this is not correct to modify 'ad' at this point, but
7221 the syntax is not clear */
7222 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7223 parse_attribute(ad);
7224 type_decl(&type1, ad, v, td);
7225 skip(')');
7226 } else {
7227 /* type identifier */
7228 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7229 *v = tok;
7230 next();
7231 } else {
7232 if (!(td & TYPE_ABSTRACT))
7233 expect("identifier");
7234 *v = 0;
7237 post_type(type, ad);
7238 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7239 parse_attribute(ad);
7240 if (!type1.t)
7241 return;
7242 /* append type at the end of type1 */
7243 type2 = &type1;
7244 for(;;) {
7245 s = type2->ref;
7246 type2 = &s->type;
7247 if (!type2->t) {
7248 *type2 = *type;
7249 break;
7252 *type = type1;
7255 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7256 static int lvalue_type(int t)
7258 int bt, r;
7259 r = VT_LVAL;
7260 bt = t & VT_BTYPE;
7261 if (bt == VT_BYTE || bt == VT_BOOL)
7262 r |= VT_LVAL_BYTE;
7263 else if (bt == VT_SHORT)
7264 r |= VT_LVAL_SHORT;
7265 else
7266 return r;
7267 if (t & VT_UNSIGNED)
7268 r |= VT_LVAL_UNSIGNED;
7269 return r;
7272 /* indirection with full error checking and bound check */
7273 static void indir(void)
7275 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7276 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7277 return;
7278 expect("pointer");
7280 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7281 gv(RC_INT);
7282 vtop->type = *pointed_type(&vtop->type);
7283 /* Arrays and functions are never lvalues */
7284 if (!(vtop->type.t & VT_ARRAY)
7285 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7286 vtop->r |= lvalue_type(vtop->type.t);
7287 /* if bound checking, the referenced pointer must be checked */
7288 if (do_bounds_check)
7289 vtop->r |= VT_MUSTBOUND;
7293 /* pass a parameter to a function and do type checking and casting */
7294 static void gfunc_param_typed(Sym *func, Sym *arg)
7296 int func_type;
7297 CType type;
7299 func_type = func->c;
7300 if (func_type == FUNC_OLD ||
7301 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7302 /* default casting : only need to convert float to double */
7303 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7304 type.t = VT_DOUBLE;
7305 gen_cast(&type);
7307 } else if (arg == NULL) {
7308 error("too many arguments to function");
7309 } else {
7310 type = arg->type;
7311 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7312 gen_assign_cast(&type);
7316 /* parse an expression of the form '(type)' or '(expr)' and return its
7317 type */
7318 static void parse_expr_type(CType *type)
7320 int n;
7321 AttributeDef ad;
7323 skip('(');
7324 if (parse_btype(type, &ad)) {
7325 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7326 } else {
7327 expr_type(type);
7329 skip(')');
7332 static void parse_type(CType *type)
7334 AttributeDef ad;
7335 int n;
7337 if (!parse_btype(type, &ad)) {
7338 expect("type");
7340 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7343 static void vpush_tokc(int t)
7345 CType type;
7346 type.t = t;
7347 vsetc(&type, VT_CONST, &tokc);
7350 static void unary(void)
7352 int n, t, align, size, r;
7353 CType type;
7354 Sym *s;
7355 AttributeDef ad;
7357 /* XXX: GCC 2.95.3 does not generate a table although it should be
7358 better here */
7359 tok_next:
7360 switch(tok) {
7361 case TOK_EXTENSION:
7362 next();
7363 goto tok_next;
7364 case TOK_CINT:
7365 case TOK_CCHAR:
7366 case TOK_LCHAR:
7367 vpushi(tokc.i);
7368 next();
7369 break;
7370 case TOK_CUINT:
7371 vpush_tokc(VT_INT | VT_UNSIGNED);
7372 next();
7373 break;
7374 case TOK_CLLONG:
7375 vpush_tokc(VT_LLONG);
7376 next();
7377 break;
7378 case TOK_CULLONG:
7379 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7380 next();
7381 break;
7382 case TOK_CFLOAT:
7383 vpush_tokc(VT_FLOAT);
7384 next();
7385 break;
7386 case TOK_CDOUBLE:
7387 vpush_tokc(VT_DOUBLE);
7388 next();
7389 break;
7390 case TOK_CLDOUBLE:
7391 vpush_tokc(VT_LDOUBLE);
7392 next();
7393 break;
7394 case TOK___FUNCTION__:
7395 if (!gnu_ext)
7396 goto tok_identifier;
7397 /* fall thru */
7398 case TOK___FUNC__:
7400 void *ptr;
7401 int len;
7402 /* special function name identifier */
7403 len = strlen(funcname) + 1;
7404 /* generate char[len] type */
7405 type.t = VT_BYTE;
7406 mk_pointer(&type);
7407 type.t |= VT_ARRAY;
7408 type.ref->c = len;
7409 vpush_ref(&type, data_section, data_section->data_offset, len);
7410 ptr = section_ptr_add(data_section, len);
7411 memcpy(ptr, funcname, len);
7412 next();
7414 break;
7415 case TOK_LSTR:
7416 #ifdef TCC_TARGET_PE
7417 t = VT_SHORT | VT_UNSIGNED;
7418 #else
7419 t = VT_INT;
7420 #endif
7421 goto str_init;
7422 case TOK_STR:
7423 /* string parsing */
7424 t = VT_BYTE;
7425 str_init:
7426 if (tcc_state->warn_write_strings)
7427 t |= VT_CONSTANT;
7428 type.t = t;
7429 mk_pointer(&type);
7430 type.t |= VT_ARRAY;
7431 memset(&ad, 0, sizeof(AttributeDef));
7432 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7433 break;
7434 case '(':
7435 next();
7436 /* cast ? */
7437 if (parse_btype(&type, &ad)) {
7438 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7439 skip(')');
7440 /* check ISOC99 compound literal */
7441 if (tok == '{') {
7442 /* data is allocated locally by default */
7443 if (global_expr)
7444 r = VT_CONST;
7445 else
7446 r = VT_LOCAL;
7447 /* all except arrays are lvalues */
7448 if (!(type.t & VT_ARRAY))
7449 r |= lvalue_type(type.t);
7450 memset(&ad, 0, sizeof(AttributeDef));
7451 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7452 } else {
7453 unary();
7454 gen_cast(&type);
7456 } else if (tok == '{') {
7457 /* save all registers */
7458 save_regs(0);
7459 /* statement expression : we do not accept break/continue
7460 inside as GCC does */
7461 block(NULL, NULL, NULL, NULL, 0, 1);
7462 skip(')');
7463 } else {
7464 gexpr();
7465 skip(')');
7467 break;
7468 case '*':
7469 next();
7470 unary();
7471 indir();
7472 break;
7473 case '&':
7474 next();
7475 unary();
7476 /* functions names must be treated as function pointers,
7477 except for unary '&' and sizeof. Since we consider that
7478 functions are not lvalues, we only have to handle it
7479 there and in function calls. */
7480 /* arrays can also be used although they are not lvalues */
7481 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7482 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7483 test_lvalue();
7484 mk_pointer(&vtop->type);
7485 gaddrof();
7486 break;
7487 case '!':
7488 next();
7489 unary();
7490 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
7491 vtop->c.i = !vtop->c.i;
7492 else if ((vtop->r & VT_VALMASK) == VT_CMP)
7493 vtop->c.i = vtop->c.i ^ 1;
7494 else {
7495 save_regs(1);
7496 vseti(VT_JMP, gtst(1, 0));
7498 break;
7499 case '~':
7500 next();
7501 unary();
7502 vpushi(-1);
7503 gen_op('^');
7504 break;
7505 case '+':
7506 next();
7507 /* in order to force cast, we add zero */
7508 unary();
7509 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7510 error("pointer not accepted for unary plus");
7511 vpushi(0);
7512 gen_op('+');
7513 break;
7514 case TOK_SIZEOF:
7515 case TOK_ALIGNOF1:
7516 case TOK_ALIGNOF2:
7517 t = tok;
7518 next();
7519 if (tok == '(') {
7520 parse_expr_type(&type);
7521 } else {
7522 unary_type(&type);
7524 size = type_size(&type, &align);
7525 if (t == TOK_SIZEOF) {
7526 if (size < 0)
7527 error("sizeof applied to an incomplete type");
7528 vpushi(size);
7529 } else {
7530 vpushi(align);
7532 vtop->type.t |= VT_UNSIGNED;
7533 break;
7535 case TOK_builtin_types_compatible_p:
7537 CType type1, type2;
7538 next();
7539 skip('(');
7540 parse_type(&type1);
7541 skip(',');
7542 parse_type(&type2);
7543 skip(')');
7544 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7545 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7546 vpushi(is_compatible_types(&type1, &type2));
7548 break;
7549 case TOK_builtin_constant_p:
7551 int saved_nocode_wanted, res;
7552 next();
7553 skip('(');
7554 saved_nocode_wanted = nocode_wanted;
7555 nocode_wanted = 1;
7556 gexpr();
7557 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7558 vpop();
7559 nocode_wanted = saved_nocode_wanted;
7560 skip(')');
7561 vpushi(res);
7563 break;
7564 case TOK_INC:
7565 case TOK_DEC:
7566 t = tok;
7567 next();
7568 unary();
7569 inc(0, t);
7570 break;
7571 case '-':
7572 next();
7573 vpushi(0);
7574 unary();
7575 gen_op('-');
7576 break;
7577 case TOK_LAND:
7578 if (!gnu_ext)
7579 goto tok_identifier;
7580 next();
7581 /* allow to take the address of a label */
7582 if (tok < TOK_UIDENT)
7583 expect("label identifier");
7584 s = label_find(tok);
7585 if (!s) {
7586 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7587 } else {
7588 if (s->r == LABEL_DECLARED)
7589 s->r = LABEL_FORWARD;
7591 if (!s->type.t) {
7592 s->type.t = VT_VOID;
7593 mk_pointer(&s->type);
7594 s->type.t |= VT_STATIC;
7596 vset(&s->type, VT_CONST | VT_SYM, 0);
7597 vtop->sym = s;
7598 next();
7599 break;
7600 default:
7601 tok_identifier:
7602 t = tok;
7603 next();
7604 if (t < TOK_UIDENT)
7605 expect("identifier");
7606 s = sym_find(t);
7607 if (!s) {
7608 if (tok != '(')
7609 error("'%s' undeclared", get_tok_str(t, NULL));
7610 /* for simple function calls, we tolerate undeclared
7611 external reference to int() function */
7612 if (tcc_state->warn_implicit_function_declaration)
7613 warning("implicit declaration of function '%s'",
7614 get_tok_str(t, NULL));
7615 s = external_global_sym(t, &func_old_type, 0);
7617 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7618 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7619 /* if referencing an inline function, then we generate a
7620 symbol to it if not already done. It will have the
7621 effect to generate code for it at the end of the
7622 compilation unit. Inline function as always
7623 generated in the text section. */
7624 if (!s->c)
7625 put_extern_sym(s, text_section, 0, 0);
7626 r = VT_SYM | VT_CONST;
7627 } else {
7628 r = s->r;
7630 vset(&s->type, r, s->c);
7631 /* if forward reference, we must point to s */
7632 if (vtop->r & VT_SYM) {
7633 vtop->sym = s;
7634 vtop->c.ul = 0;
7636 break;
7639 /* post operations */
7640 while (1) {
7641 if (tok == TOK_INC || tok == TOK_DEC) {
7642 inc(1, tok);
7643 next();
7644 } else if (tok == '.' || tok == TOK_ARROW) {
7645 /* field */
7646 if (tok == TOK_ARROW)
7647 indir();
7648 test_lvalue();
7649 gaddrof();
7650 next();
7651 /* expect pointer on structure */
7652 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7653 expect("struct or union");
7654 s = vtop->type.ref;
7655 /* find field */
7656 tok |= SYM_FIELD;
7657 while ((s = s->next) != NULL) {
7658 if (s->v == tok)
7659 break;
7661 if (!s)
7662 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7663 /* add field offset to pointer */
7664 vtop->type = char_pointer_type; /* change type to 'char *' */
7665 vpushi(s->c);
7666 gen_op('+');
7667 /* change type to field type, and set to lvalue */
7668 vtop->type = s->type;
7669 /* an array is never an lvalue */
7670 if (!(vtop->type.t & VT_ARRAY)) {
7671 vtop->r |= lvalue_type(vtop->type.t);
7672 /* if bound checking, the referenced pointer must be checked */
7673 if (do_bounds_check)
7674 vtop->r |= VT_MUSTBOUND;
7676 next();
7677 } else if (tok == '[') {
7678 next();
7679 gexpr();
7680 gen_op('+');
7681 indir();
7682 skip(']');
7683 } else if (tok == '(') {
7684 SValue ret;
7685 Sym *sa;
7686 int nb_args;
7688 /* function call */
7689 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7690 /* pointer test (no array accepted) */
7691 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7692 vtop->type = *pointed_type(&vtop->type);
7693 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7694 goto error_func;
7695 } else {
7696 error_func:
7697 expect("function pointer");
7699 } else {
7700 vtop->r &= ~VT_LVAL; /* no lvalue */
7702 /* get return type */
7703 s = vtop->type.ref;
7704 next();
7705 sa = s->next; /* first parameter */
7706 nb_args = 0;
7707 ret.r2 = VT_CONST;
7708 /* compute first implicit argument if a structure is returned */
7709 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7710 /* get some space for the returned structure */
7711 size = type_size(&s->type, &align);
7712 loc = (loc - size) & -align;
7713 ret.type = s->type;
7714 ret.r = VT_LOCAL | VT_LVAL;
7715 /* pass it as 'int' to avoid structure arg passing
7716 problems */
7717 vseti(VT_LOCAL, loc);
7718 ret.c = vtop->c;
7719 nb_args++;
7720 } else {
7721 ret.type = s->type;
7722 /* return in register */
7723 if (is_float(ret.type.t)) {
7724 ret.r = REG_FRET;
7725 } else {
7726 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7727 ret.r2 = REG_LRET;
7728 ret.r = REG_IRET;
7730 ret.c.i = 0;
7732 if (tok != ')') {
7733 for(;;) {
7734 expr_eq();
7735 gfunc_param_typed(s, sa);
7736 nb_args++;
7737 if (sa)
7738 sa = sa->next;
7739 if (tok == ')')
7740 break;
7741 skip(',');
7744 if (sa)
7745 error("too few arguments to function");
7746 skip(')');
7747 if (!nocode_wanted) {
7748 gfunc_call(nb_args);
7749 } else {
7750 vtop -= (nb_args + 1);
7752 /* return value */
7753 vsetc(&ret.type, ret.r, &ret.c);
7754 vtop->r2 = ret.r2;
7755 } else {
7756 break;
7761 static void uneq(void)
7763 int t;
7765 unary();
7766 if (tok == '=' ||
7767 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7768 tok == TOK_A_XOR || tok == TOK_A_OR ||
7769 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7770 test_lvalue();
7771 t = tok;
7772 next();
7773 if (t == '=') {
7774 expr_eq();
7775 } else {
7776 vdup();
7777 expr_eq();
7778 gen_op(t & 0x7f);
7780 vstore();
7784 static void expr_prod(void)
7786 int t;
7788 uneq();
7789 while (tok == '*' || tok == '/' || tok == '%') {
7790 t = tok;
7791 next();
7792 uneq();
7793 gen_op(t);
7797 static void expr_sum(void)
7799 int t;
7801 expr_prod();
7802 while (tok == '+' || tok == '-') {
7803 t = tok;
7804 next();
7805 expr_prod();
7806 gen_op(t);
7810 static void expr_shift(void)
7812 int t;
7814 expr_sum();
7815 while (tok == TOK_SHL || tok == TOK_SAR) {
7816 t = tok;
7817 next();
7818 expr_sum();
7819 gen_op(t);
7823 static void expr_cmp(void)
7825 int t;
7827 expr_shift();
7828 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7829 tok == TOK_ULT || tok == TOK_UGE) {
7830 t = tok;
7831 next();
7832 expr_shift();
7833 gen_op(t);
7837 static void expr_cmpeq(void)
7839 int t;
7841 expr_cmp();
7842 while (tok == TOK_EQ || tok == TOK_NE) {
7843 t = tok;
7844 next();
7845 expr_cmp();
7846 gen_op(t);
7850 static void expr_and(void)
7852 expr_cmpeq();
7853 while (tok == '&') {
7854 next();
7855 expr_cmpeq();
7856 gen_op('&');
7860 static void expr_xor(void)
7862 expr_and();
7863 while (tok == '^') {
7864 next();
7865 expr_and();
7866 gen_op('^');
7870 static void expr_or(void)
7872 expr_xor();
7873 while (tok == '|') {
7874 next();
7875 expr_xor();
7876 gen_op('|');
7880 /* XXX: fix this mess */
7881 static void expr_land_const(void)
7883 expr_or();
7884 while (tok == TOK_LAND) {
7885 next();
7886 expr_or();
7887 gen_op(TOK_LAND);
7891 /* XXX: fix this mess */
7892 static void expr_lor_const(void)
7894 expr_land_const();
7895 while (tok == TOK_LOR) {
7896 next();
7897 expr_land_const();
7898 gen_op(TOK_LOR);
7902 /* only used if non constant */
7903 static void expr_land(void)
7905 int t;
7907 expr_or();
7908 if (tok == TOK_LAND) {
7909 t = 0;
7910 save_regs(1);
7911 for(;;) {
7912 t = gtst(1, t);
7913 if (tok != TOK_LAND) {
7914 vseti(VT_JMPI, t);
7915 break;
7917 next();
7918 expr_or();
7923 static void expr_lor(void)
7925 int t;
7927 expr_land();
7928 if (tok == TOK_LOR) {
7929 t = 0;
7930 save_regs(1);
7931 for(;;) {
7932 t = gtst(0, t);
7933 if (tok != TOK_LOR) {
7934 vseti(VT_JMP, t);
7935 break;
7937 next();
7938 expr_land();
7943 /* XXX: better constant handling */
7944 static void expr_eq(void)
7946 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7947 SValue sv;
7948 CType type, type1, type2;
7950 if (const_wanted) {
7951 int c1, c;
7952 expr_lor_const();
7953 if (tok == '?') {
7954 c = vtop->c.i;
7955 vpop();
7956 next();
7957 if (tok == ':' && gnu_ext) {
7958 c1 = c;
7959 } else {
7960 gexpr();
7961 c1 = vtop->c.i;
7962 vpop();
7964 skip(':');
7965 expr_eq();
7966 if (c)
7967 vtop->c.i = c1;
7969 } else {
7970 expr_lor();
7971 if (tok == '?') {
7972 next();
7973 if (vtop != vstack) {
7974 /* needed to avoid having different registers saved in
7975 each branch */
7976 if (is_float(vtop->type.t))
7977 rc = RC_FLOAT;
7978 else
7979 rc = RC_INT;
7980 gv(rc);
7981 save_regs(1);
7983 if (tok == ':' && gnu_ext) {
7984 gv_dup();
7985 tt = gtst(1, 0);
7986 } else {
7987 tt = gtst(1, 0);
7988 gexpr();
7990 type1 = vtop->type;
7991 sv = *vtop; /* save value to handle it later */
7992 vtop--; /* no vpop so that FP stack is not flushed */
7993 skip(':');
7994 u = gjmp(0);
7995 gsym(tt);
7996 expr_eq();
7997 type2 = vtop->type;
7999 t1 = type1.t;
8000 bt1 = t1 & VT_BTYPE;
8001 t2 = type2.t;
8002 bt2 = t2 & VT_BTYPE;
8003 /* cast operands to correct type according to ISOC rules */
8004 if (is_float(bt1) || is_float(bt2)) {
8005 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8006 type.t = VT_LDOUBLE;
8007 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8008 type.t = VT_DOUBLE;
8009 } else {
8010 type.t = VT_FLOAT;
8012 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8013 /* cast to biggest op */
8014 type.t = VT_LLONG;
8015 /* convert to unsigned if it does not fit in a long long */
8016 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8017 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8018 type.t |= VT_UNSIGNED;
8019 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8020 /* XXX: test pointer compatibility */
8021 type = type1;
8022 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8023 /* XXX: test function pointer compatibility */
8024 type = type1;
8025 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8026 /* XXX: test structure compatibility */
8027 type = type1;
8028 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8029 /* NOTE: as an extension, we accept void on only one side */
8030 type.t = VT_VOID;
8031 } else {
8032 /* integer operations */
8033 type.t = VT_INT;
8034 /* convert to unsigned if it does not fit in an integer */
8035 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8036 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8037 type.t |= VT_UNSIGNED;
8040 /* now we convert second operand */
8041 gen_cast(&type);
8042 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8043 gaddrof();
8044 rc = RC_INT;
8045 if (is_float(type.t)) {
8046 rc = RC_FLOAT;
8047 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8048 /* for long longs, we use fixed registers to avoid having
8049 to handle a complicated move */
8050 rc = RC_IRET;
8053 r2 = gv(rc);
8054 /* this is horrible, but we must also convert first
8055 operand */
8056 tt = gjmp(0);
8057 gsym(u);
8058 /* put again first value and cast it */
8059 *vtop = sv;
8060 gen_cast(&type);
8061 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8062 gaddrof();
8063 r1 = gv(rc);
8064 move_reg(r2, r1);
8065 vtop->r = r2;
8066 gsym(tt);
8071 static void gexpr(void)
8073 while (1) {
8074 expr_eq();
8075 if (tok != ',')
8076 break;
8077 vpop();
8078 next();
8082 /* parse an expression and return its type without any side effect. */
8083 static void expr_type(CType *type)
8085 int saved_nocode_wanted;
8087 saved_nocode_wanted = nocode_wanted;
8088 nocode_wanted = 1;
8089 gexpr();
8090 *type = vtop->type;
8091 vpop();
8092 nocode_wanted = saved_nocode_wanted;
8095 /* parse a unary expression and return its type without any side
8096 effect. */
8097 static void unary_type(CType *type)
8099 int a;
8101 a = nocode_wanted;
8102 nocode_wanted = 1;
8103 unary();
8104 *type = vtop->type;
8105 vpop();
8106 nocode_wanted = a;
8109 /* parse a constant expression and return value in vtop. */
8110 static void expr_const1(void)
8112 int a;
8113 a = const_wanted;
8114 const_wanted = 1;
8115 expr_eq();
8116 const_wanted = a;
8119 /* parse an integer constant and return its value. */
8120 static int expr_const(void)
8122 int c;
8123 expr_const1();
8124 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8125 expect("constant expression");
8126 c = vtop->c.i;
8127 vpop();
8128 return c;
8131 /* return the label token if current token is a label, otherwise
8132 return zero */
8133 static int is_label(void)
8135 int last_tok;
8137 /* fast test first */
8138 if (tok < TOK_UIDENT)
8139 return 0;
8140 /* no need to save tokc because tok is an identifier */
8141 last_tok = tok;
8142 next();
8143 if (tok == ':') {
8144 next();
8145 return last_tok;
8146 } else {
8147 unget_tok(last_tok);
8148 return 0;
8152 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8153 int case_reg, int is_expr)
8155 int a, b, c, d;
8156 Sym *s;
8158 /* generate line number info */
8159 if (do_debug &&
8160 (last_line_num != file->line_num || last_ind != ind)) {
8161 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8162 last_ind = ind;
8163 last_line_num = file->line_num;
8166 if (is_expr) {
8167 /* default return value is (void) */
8168 vpushi(0);
8169 vtop->type.t = VT_VOID;
8172 if (tok == TOK_IF) {
8173 /* if test */
8174 next();
8175 skip('(');
8176 gexpr();
8177 skip(')');
8178 a = gtst(1, 0);
8179 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8180 c = tok;
8181 if (c == TOK_ELSE) {
8182 next();
8183 d = gjmp(0);
8184 gsym(a);
8185 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8186 gsym(d); /* patch else jmp */
8187 } else
8188 gsym(a);
8189 } else if (tok == TOK_WHILE) {
8190 next();
8191 d = ind;
8192 skip('(');
8193 gexpr();
8194 skip(')');
8195 a = gtst(1, 0);
8196 b = 0;
8197 block(&a, &b, case_sym, def_sym, case_reg, 0);
8198 gjmp_addr(d);
8199 gsym(a);
8200 gsym_addr(b, d);
8201 } else if (tok == '{') {
8202 Sym *llabel;
8204 next();
8205 /* record local declaration stack position */
8206 s = local_stack;
8207 llabel = local_label_stack;
8208 /* handle local labels declarations */
8209 if (tok == TOK_LABEL) {
8210 next();
8211 for(;;) {
8212 if (tok < TOK_UIDENT)
8213 expect("label identifier");
8214 label_push(&local_label_stack, tok, LABEL_DECLARED);
8215 next();
8216 if (tok == ',') {
8217 next();
8218 } else {
8219 skip(';');
8220 break;
8224 while (tok != '}') {
8225 decl(VT_LOCAL);
8226 if (tok != '}') {
8227 if (is_expr)
8228 vpop();
8229 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8232 /* pop locally defined labels */
8233 label_pop(&local_label_stack, llabel);
8234 /* pop locally defined symbols */
8235 sym_pop(&local_stack, s);
8236 next();
8237 } else if (tok == TOK_RETURN) {
8238 next();
8239 if (tok != ';') {
8240 gexpr();
8241 gen_assign_cast(&func_vt);
8242 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8243 CType type;
8244 /* if returning structure, must copy it to implicit
8245 first pointer arg location */
8246 #ifdef TCC_ARM_EABI
8247 int align, size;
8248 size = type_size(&func_vt,&align);
8249 if(size <= 4)
8251 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8252 && (align & 3))
8254 int addr;
8255 loc = (loc - size) & -4;
8256 addr = loc;
8257 type = func_vt;
8258 vset(&type, VT_LOCAL | VT_LVAL, addr);
8259 vswap();
8260 vstore();
8261 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8263 vtop->type = int_type;
8264 gv(RC_IRET);
8265 } else {
8266 #endif
8267 type = func_vt;
8268 mk_pointer(&type);
8269 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8270 indir();
8271 vswap();
8272 /* copy structure value to pointer */
8273 vstore();
8274 #ifdef TCC_ARM_EABI
8276 #endif
8277 } else if (is_float(func_vt.t)) {
8278 gv(RC_FRET);
8279 } else {
8280 gv(RC_IRET);
8282 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8284 skip(';');
8285 rsym = gjmp(rsym); /* jmp */
8286 } else if (tok == TOK_BREAK) {
8287 /* compute jump */
8288 if (!bsym)
8289 error("cannot break");
8290 *bsym = gjmp(*bsym);
8291 next();
8292 skip(';');
8293 } else if (tok == TOK_CONTINUE) {
8294 /* compute jump */
8295 if (!csym)
8296 error("cannot continue");
8297 *csym = gjmp(*csym);
8298 next();
8299 skip(';');
8300 } else if (tok == TOK_FOR) {
8301 int e;
8302 next();
8303 skip('(');
8304 if (tok != ';') {
8305 gexpr();
8306 vpop();
8308 skip(';');
8309 d = ind;
8310 c = ind;
8311 a = 0;
8312 b = 0;
8313 if (tok != ';') {
8314 gexpr();
8315 a = gtst(1, 0);
8317 skip(';');
8318 if (tok != ')') {
8319 e = gjmp(0);
8320 c = ind;
8321 gexpr();
8322 vpop();
8323 gjmp_addr(d);
8324 gsym(e);
8326 skip(')');
8327 block(&a, &b, case_sym, def_sym, case_reg, 0);
8328 gjmp_addr(c);
8329 gsym(a);
8330 gsym_addr(b, c);
8331 } else
8332 if (tok == TOK_DO) {
8333 next();
8334 a = 0;
8335 b = 0;
8336 d = ind;
8337 block(&a, &b, case_sym, def_sym, case_reg, 0);
8338 skip(TOK_WHILE);
8339 skip('(');
8340 gsym(b);
8341 gexpr();
8342 c = gtst(0, 0);
8343 gsym_addr(c, d);
8344 skip(')');
8345 gsym(a);
8346 skip(';');
8347 } else
8348 if (tok == TOK_SWITCH) {
8349 next();
8350 skip('(');
8351 gexpr();
8352 /* XXX: other types than integer */
8353 case_reg = gv(RC_INT);
8354 vpop();
8355 skip(')');
8356 a = 0;
8357 b = gjmp(0); /* jump to first case */
8358 c = 0;
8359 block(&a, csym, &b, &c, case_reg, 0);
8360 /* if no default, jmp after switch */
8361 if (c == 0)
8362 c = ind;
8363 /* default label */
8364 gsym_addr(b, c);
8365 /* break label */
8366 gsym(a);
8367 } else
8368 if (tok == TOK_CASE) {
8369 int v1, v2;
8370 if (!case_sym)
8371 expect("switch");
8372 next();
8373 v1 = expr_const();
8374 v2 = v1;
8375 if (gnu_ext && tok == TOK_DOTS) {
8376 next();
8377 v2 = expr_const();
8378 if (v2 < v1)
8379 warning("empty case range");
8381 /* since a case is like a label, we must skip it with a jmp */
8382 b = gjmp(0);
8383 gsym(*case_sym);
8384 vseti(case_reg, 0);
8385 vpushi(v1);
8386 if (v1 == v2) {
8387 gen_op(TOK_EQ);
8388 *case_sym = gtst(1, 0);
8389 } else {
8390 gen_op(TOK_GE);
8391 *case_sym = gtst(1, 0);
8392 vseti(case_reg, 0);
8393 vpushi(v2);
8394 gen_op(TOK_LE);
8395 *case_sym = gtst(1, *case_sym);
8397 gsym(b);
8398 skip(':');
8399 is_expr = 0;
8400 goto block_after_label;
8401 } else
8402 if (tok == TOK_DEFAULT) {
8403 next();
8404 skip(':');
8405 if (!def_sym)
8406 expect("switch");
8407 if (*def_sym)
8408 error("too many 'default'");
8409 *def_sym = ind;
8410 is_expr = 0;
8411 goto block_after_label;
8412 } else
8413 if (tok == TOK_GOTO) {
8414 next();
8415 if (tok == '*' && gnu_ext) {
8416 /* computed goto */
8417 next();
8418 gexpr();
8419 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8420 expect("pointer");
8421 ggoto();
8422 } else if (tok >= TOK_UIDENT) {
8423 s = label_find(tok);
8424 /* put forward definition if needed */
8425 if (!s) {
8426 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8427 } else {
8428 if (s->r == LABEL_DECLARED)
8429 s->r = LABEL_FORWARD;
8431 /* label already defined */
8432 if (s->r & LABEL_FORWARD)
8433 s->next = (void *)gjmp((long)s->next);
8434 else
8435 gjmp_addr((long)s->next);
8436 next();
8437 } else {
8438 expect("label identifier");
8440 skip(';');
8441 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8442 asm_instr();
8443 } else {
8444 b = is_label();
8445 if (b) {
8446 /* label case */
8447 s = label_find(b);
8448 if (s) {
8449 if (s->r == LABEL_DEFINED)
8450 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8451 gsym((long)s->next);
8452 s->r = LABEL_DEFINED;
8453 } else {
8454 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8456 s->next = (void *)ind;
8457 /* we accept this, but it is a mistake */
8458 block_after_label:
8459 if (tok == '}') {
8460 warning("deprecated use of label at end of compound statement");
8461 } else {
8462 if (is_expr)
8463 vpop();
8464 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8466 } else {
8467 /* expression case */
8468 if (tok != ';') {
8469 if (is_expr) {
8470 vpop();
8471 gexpr();
8472 } else {
8473 gexpr();
8474 vpop();
8477 skip(';');
8482 /* t is the array or struct type. c is the array or struct
8483 address. cur_index/cur_field is the pointer to the current
8484 value. 'size_only' is true if only size info is needed (only used
8485 in arrays) */
8486 static void decl_designator(CType *type, Section *sec, unsigned long c,
8487 int *cur_index, Sym **cur_field,
8488 int size_only)
8490 Sym *s, *f;
8491 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8492 CType type1;
8494 notfirst = 0;
8495 elem_size = 0;
8496 nb_elems = 1;
8497 if (gnu_ext && (l = is_label()) != 0)
8498 goto struct_field;
8499 while (tok == '[' || tok == '.') {
8500 if (tok == '[') {
8501 if (!(type->t & VT_ARRAY))
8502 expect("array type");
8503 s = type->ref;
8504 next();
8505 index = expr_const();
8506 if (index < 0 || (s->c >= 0 && index >= s->c))
8507 expect("invalid index");
8508 if (tok == TOK_DOTS && gnu_ext) {
8509 next();
8510 index_last = expr_const();
8511 if (index_last < 0 ||
8512 (s->c >= 0 && index_last >= s->c) ||
8513 index_last < index)
8514 expect("invalid index");
8515 } else {
8516 index_last = index;
8518 skip(']');
8519 if (!notfirst)
8520 *cur_index = index_last;
8521 type = pointed_type(type);
8522 elem_size = type_size(type, &align);
8523 c += index * elem_size;
8524 /* NOTE: we only support ranges for last designator */
8525 nb_elems = index_last - index + 1;
8526 if (nb_elems != 1) {
8527 notfirst = 1;
8528 break;
8530 } else {
8531 next();
8532 l = tok;
8533 next();
8534 struct_field:
8535 if ((type->t & VT_BTYPE) != VT_STRUCT)
8536 expect("struct/union type");
8537 s = type->ref;
8538 l |= SYM_FIELD;
8539 f = s->next;
8540 while (f) {
8541 if (f->v == l)
8542 break;
8543 f = f->next;
8545 if (!f)
8546 expect("field");
8547 if (!notfirst)
8548 *cur_field = f;
8549 /* XXX: fix this mess by using explicit storage field */
8550 type1 = f->type;
8551 type1.t |= (type->t & ~VT_TYPE);
8552 type = &type1;
8553 c += f->c;
8555 notfirst = 1;
8557 if (notfirst) {
8558 if (tok == '=') {
8559 next();
8560 } else {
8561 if (!gnu_ext)
8562 expect("=");
8564 } else {
8565 if (type->t & VT_ARRAY) {
8566 index = *cur_index;
8567 type = pointed_type(type);
8568 c += index * type_size(type, &align);
8569 } else {
8570 f = *cur_field;
8571 if (!f)
8572 error("too many field init");
8573 /* XXX: fix this mess by using explicit storage field */
8574 type1 = f->type;
8575 type1.t |= (type->t & ~VT_TYPE);
8576 type = &type1;
8577 c += f->c;
8580 decl_initializer(type, sec, c, 0, size_only);
8582 /* XXX: make it more general */
8583 if (!size_only && nb_elems > 1) {
8584 unsigned long c_end;
8585 uint8_t *src, *dst;
8586 int i;
8588 if (!sec)
8589 error("range init not supported yet for dynamic storage");
8590 c_end = c + nb_elems * elem_size;
8591 if (c_end > sec->data_allocated)
8592 section_realloc(sec, c_end);
8593 src = sec->data + c;
8594 dst = src;
8595 for(i = 1; i < nb_elems; i++) {
8596 dst += elem_size;
8597 memcpy(dst, src, elem_size);
8602 #define EXPR_VAL 0
8603 #define EXPR_CONST 1
8604 #define EXPR_ANY 2
8606 /* store a value or an expression directly in global data or in local array */
8607 static void init_putv(CType *type, Section *sec, unsigned long c,
8608 int v, int expr_type)
8610 int saved_global_expr, bt, bit_pos, bit_size;
8611 void *ptr;
8612 unsigned long long bit_mask;
8613 CType dtype;
8615 switch(expr_type) {
8616 case EXPR_VAL:
8617 vpushi(v);
8618 break;
8619 case EXPR_CONST:
8620 /* compound literals must be allocated globally in this case */
8621 saved_global_expr = global_expr;
8622 global_expr = 1;
8623 expr_const1();
8624 global_expr = saved_global_expr;
8625 /* NOTE: symbols are accepted */
8626 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8627 error("initializer element is not constant");
8628 break;
8629 case EXPR_ANY:
8630 expr_eq();
8631 break;
8634 dtype = *type;
8635 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8637 if (sec) {
8638 /* XXX: not portable */
8639 /* XXX: generate error if incorrect relocation */
8640 gen_assign_cast(&dtype);
8641 bt = type->t & VT_BTYPE;
8642 ptr = sec->data + c;
8643 /* XXX: make code faster ? */
8644 if (!(type->t & VT_BITFIELD)) {
8645 bit_pos = 0;
8646 bit_size = 32;
8647 bit_mask = -1LL;
8648 } else {
8649 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8650 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8651 bit_mask = (1LL << bit_size) - 1;
8653 if ((vtop->r & VT_SYM) &&
8654 (bt == VT_BYTE ||
8655 bt == VT_SHORT ||
8656 bt == VT_DOUBLE ||
8657 bt == VT_LDOUBLE ||
8658 bt == VT_LLONG ||
8659 (bt == VT_INT && bit_size != 32)))
8660 error("initializer element is not computable at load time");
8661 switch(bt) {
8662 case VT_BYTE:
8663 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8664 break;
8665 case VT_SHORT:
8666 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8667 break;
8668 case VT_DOUBLE:
8669 *(double *)ptr = vtop->c.d;
8670 break;
8671 case VT_LDOUBLE:
8672 *(long double *)ptr = vtop->c.ld;
8673 break;
8674 case VT_LLONG:
8675 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8676 break;
8677 default:
8678 if (vtop->r & VT_SYM) {
8679 greloc(sec, vtop->sym, c, R_DATA_32);
8681 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8682 break;
8684 vtop--;
8685 } else {
8686 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8687 vswap();
8688 vstore();
8689 vpop();
8693 /* put zeros for variable based init */
8694 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8696 if (sec) {
8697 /* nothing to do because globals are already set to zero */
8698 } else {
8699 vpush_global_sym(&func_old_type, TOK_memset);
8700 vseti(VT_LOCAL, c);
8701 vpushi(0);
8702 vpushi(size);
8703 gfunc_call(3);
8707 /* 't' contains the type and storage info. 'c' is the offset of the
8708 object in section 'sec'. If 'sec' is NULL, it means stack based
8709 allocation. 'first' is true if array '{' must be read (multi
8710 dimension implicit array init handling). 'size_only' is true if
8711 size only evaluation is wanted (only for arrays). */
8712 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8713 int first, int size_only)
8715 int index, array_length, n, no_oblock, nb, parlevel, i;
8716 int size1, align1, expr_type;
8717 Sym *s, *f;
8718 CType *t1;
8720 if (type->t & VT_ARRAY) {
8721 s = type->ref;
8722 n = s->c;
8723 array_length = 0;
8724 t1 = pointed_type(type);
8725 size1 = type_size(t1, &align1);
8727 no_oblock = 1;
8728 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8729 tok == '{') {
8730 skip('{');
8731 no_oblock = 0;
8734 /* only parse strings here if correct type (otherwise: handle
8735 them as ((w)char *) expressions */
8736 if ((tok == TOK_LSTR &&
8737 #ifdef TCC_TARGET_PE
8738 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8739 #else
8740 (t1->t & VT_BTYPE) == VT_INT
8741 #endif
8742 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8743 while (tok == TOK_STR || tok == TOK_LSTR) {
8744 int cstr_len, ch;
8745 CString *cstr;
8747 cstr = tokc.cstr;
8748 /* compute maximum number of chars wanted */
8749 if (tok == TOK_STR)
8750 cstr_len = cstr->size;
8751 else
8752 cstr_len = cstr->size / sizeof(nwchar_t);
8753 cstr_len--;
8754 nb = cstr_len;
8755 if (n >= 0 && nb > (n - array_length))
8756 nb = n - array_length;
8757 if (!size_only) {
8758 if (cstr_len > nb)
8759 warning("initializer-string for array is too long");
8760 /* in order to go faster for common case (char
8761 string in global variable, we handle it
8762 specifically */
8763 if (sec && tok == TOK_STR && size1 == 1) {
8764 memcpy(sec->data + c + array_length, cstr->data, nb);
8765 } else {
8766 for(i=0;i<nb;i++) {
8767 if (tok == TOK_STR)
8768 ch = ((unsigned char *)cstr->data)[i];
8769 else
8770 ch = ((nwchar_t *)cstr->data)[i];
8771 init_putv(t1, sec, c + (array_length + i) * size1,
8772 ch, EXPR_VAL);
8776 array_length += nb;
8777 next();
8779 /* only add trailing zero if enough storage (no
8780 warning in this case since it is standard) */
8781 if (n < 0 || array_length < n) {
8782 if (!size_only) {
8783 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8785 array_length++;
8787 } else {
8788 index = 0;
8789 while (tok != '}') {
8790 decl_designator(type, sec, c, &index, NULL, size_only);
8791 if (n >= 0 && index >= n)
8792 error("index too large");
8793 /* must put zero in holes (note that doing it that way
8794 ensures that it even works with designators) */
8795 if (!size_only && array_length < index) {
8796 init_putz(t1, sec, c + array_length * size1,
8797 (index - array_length) * size1);
8799 index++;
8800 if (index > array_length)
8801 array_length = index;
8802 /* special test for multi dimensional arrays (may not
8803 be strictly correct if designators are used at the
8804 same time) */
8805 if (index >= n && no_oblock)
8806 break;
8807 if (tok == '}')
8808 break;
8809 skip(',');
8812 if (!no_oblock)
8813 skip('}');
8814 /* put zeros at the end */
8815 if (!size_only && n >= 0 && array_length < n) {
8816 init_putz(t1, sec, c + array_length * size1,
8817 (n - array_length) * size1);
8819 /* patch type size if needed */
8820 if (n < 0)
8821 s->c = array_length;
8822 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8823 (sec || !first || tok == '{')) {
8824 int par_count;
8826 /* NOTE: the previous test is a specific case for automatic
8827 struct/union init */
8828 /* XXX: union needs only one init */
8830 /* XXX: this test is incorrect for local initializers
8831 beginning with ( without {. It would be much more difficult
8832 to do it correctly (ideally, the expression parser should
8833 be used in all cases) */
8834 par_count = 0;
8835 if (tok == '(') {
8836 AttributeDef ad1;
8837 CType type1;
8838 next();
8839 while (tok == '(') {
8840 par_count++;
8841 next();
8843 if (!parse_btype(&type1, &ad1))
8844 expect("cast");
8845 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8846 #if 0
8847 if (!is_assignable_types(type, &type1))
8848 error("invalid type for cast");
8849 #endif
8850 skip(')');
8852 no_oblock = 1;
8853 if (first || tok == '{') {
8854 skip('{');
8855 no_oblock = 0;
8857 s = type->ref;
8858 f = s->next;
8859 array_length = 0;
8860 index = 0;
8861 n = s->c;
8862 while (tok != '}') {
8863 decl_designator(type, sec, c, NULL, &f, size_only);
8864 index = f->c;
8865 if (!size_only && array_length < index) {
8866 init_putz(type, sec, c + array_length,
8867 index - array_length);
8869 index = index + type_size(&f->type, &align1);
8870 if (index > array_length)
8871 array_length = index;
8872 f = f->next;
8873 if (no_oblock && f == NULL)
8874 break;
8875 if (tok == '}')
8876 break;
8877 skip(',');
8879 /* put zeros at the end */
8880 if (!size_only && array_length < n) {
8881 init_putz(type, sec, c + array_length,
8882 n - array_length);
8884 if (!no_oblock)
8885 skip('}');
8886 while (par_count) {
8887 skip(')');
8888 par_count--;
8890 } else if (tok == '{') {
8891 next();
8892 decl_initializer(type, sec, c, first, size_only);
8893 skip('}');
8894 } else if (size_only) {
8895 /* just skip expression */
8896 parlevel = 0;
8897 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8898 tok != -1) {
8899 if (tok == '(')
8900 parlevel++;
8901 else if (tok == ')')
8902 parlevel--;
8903 next();
8905 } else {
8906 /* currently, we always use constant expression for globals
8907 (may change for scripting case) */
8908 expr_type = EXPR_CONST;
8909 if (!sec)
8910 expr_type = EXPR_ANY;
8911 init_putv(type, sec, c, 0, expr_type);
8915 /* parse an initializer for type 't' if 'has_init' is non zero, and
8916 allocate space in local or global data space ('r' is either
8917 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8918 variable 'v' of scope 'scope' is declared before initializers are
8919 parsed. If 'v' is zero, then a reference to the new object is put
8920 in the value stack. If 'has_init' is 2, a special parsing is done
8921 to handle string constants. */
8922 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8923 int has_init, int v, int scope)
8925 int size, align, addr, data_offset;
8926 int level;
8927 ParseState saved_parse_state;
8928 TokenString init_str;
8929 Section *sec;
8931 size = type_size(type, &align);
8932 /* If unknown size, we must evaluate it before
8933 evaluating initializers because
8934 initializers can generate global data too
8935 (e.g. string pointers or ISOC99 compound
8936 literals). It also simplifies local
8937 initializers handling */
8938 tok_str_new(&init_str);
8939 if (size < 0) {
8940 if (!has_init)
8941 error("unknown type size");
8942 /* get all init string */
8943 if (has_init == 2) {
8944 /* only get strings */
8945 while (tok == TOK_STR || tok == TOK_LSTR) {
8946 tok_str_add_tok(&init_str);
8947 next();
8949 } else {
8950 level = 0;
8951 while (level > 0 || (tok != ',' && tok != ';')) {
8952 if (tok < 0)
8953 error("unexpected end of file in initializer");
8954 tok_str_add_tok(&init_str);
8955 if (tok == '{')
8956 level++;
8957 else if (tok == '}') {
8958 if (level == 0)
8959 break;
8960 level--;
8962 next();
8965 tok_str_add(&init_str, -1);
8966 tok_str_add(&init_str, 0);
8968 /* compute size */
8969 save_parse_state(&saved_parse_state);
8971 macro_ptr = init_str.str;
8972 next();
8973 decl_initializer(type, NULL, 0, 1, 1);
8974 /* prepare second initializer parsing */
8975 macro_ptr = init_str.str;
8976 next();
8978 /* if still unknown size, error */
8979 size = type_size(type, &align);
8980 if (size < 0)
8981 error("unknown type size");
8983 /* take into account specified alignment if bigger */
8984 if (ad->aligned) {
8985 if (ad->aligned > align)
8986 align = ad->aligned;
8987 } else if (ad->packed) {
8988 align = 1;
8990 if ((r & VT_VALMASK) == VT_LOCAL) {
8991 sec = NULL;
8992 if (do_bounds_check && (type->t & VT_ARRAY))
8993 loc--;
8994 loc = (loc - size) & -align;
8995 addr = loc;
8996 /* handles bounds */
8997 /* XXX: currently, since we do only one pass, we cannot track
8998 '&' operators, so we add only arrays */
8999 if (do_bounds_check && (type->t & VT_ARRAY)) {
9000 unsigned long *bounds_ptr;
9001 /* add padding between regions */
9002 loc--;
9003 /* then add local bound info */
9004 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9005 bounds_ptr[0] = addr;
9006 bounds_ptr[1] = size;
9008 if (v) {
9009 /* local variable */
9010 sym_push(v, type, r, addr);
9011 } else {
9012 /* push local reference */
9013 vset(type, r, addr);
9015 } else {
9016 Sym *sym;
9018 sym = NULL;
9019 if (v && scope == VT_CONST) {
9020 /* see if the symbol was already defined */
9021 sym = sym_find(v);
9022 if (sym) {
9023 if (!is_compatible_types(&sym->type, type))
9024 error("incompatible types for redefinition of '%s'",
9025 get_tok_str(v, NULL));
9026 if (sym->type.t & VT_EXTERN) {
9027 /* if the variable is extern, it was not allocated */
9028 sym->type.t &= ~VT_EXTERN;
9029 /* set array size if it was ommited in extern
9030 declaration */
9031 if ((sym->type.t & VT_ARRAY) &&
9032 sym->type.ref->c < 0 &&
9033 type->ref->c >= 0)
9034 sym->type.ref->c = type->ref->c;
9035 } else {
9036 /* we accept several definitions of the same
9037 global variable. this is tricky, because we
9038 must play with the SHN_COMMON type of the symbol */
9039 /* XXX: should check if the variable was already
9040 initialized. It is incorrect to initialized it
9041 twice */
9042 /* no init data, we won't add more to the symbol */
9043 if (!has_init)
9044 goto no_alloc;
9049 /* allocate symbol in corresponding section */
9050 sec = ad->section;
9051 if (!sec) {
9052 if (has_init)
9053 sec = data_section;
9054 else if (tcc_state->nocommon)
9055 sec = bss_section;
9057 if (sec) {
9058 data_offset = sec->data_offset;
9059 data_offset = (data_offset + align - 1) & -align;
9060 addr = data_offset;
9061 /* very important to increment global pointer at this time
9062 because initializers themselves can create new initializers */
9063 data_offset += size;
9064 /* add padding if bound check */
9065 if (do_bounds_check)
9066 data_offset++;
9067 sec->data_offset = data_offset;
9068 /* allocate section space to put the data */
9069 if (sec->sh_type != SHT_NOBITS &&
9070 data_offset > sec->data_allocated)
9071 section_realloc(sec, data_offset);
9072 /* align section if needed */
9073 if (align > sec->sh_addralign)
9074 sec->sh_addralign = align;
9075 } else {
9076 addr = 0; /* avoid warning */
9079 if (v) {
9080 if (scope != VT_CONST || !sym) {
9081 sym = sym_push(v, type, r | VT_SYM, 0);
9083 /* update symbol definition */
9084 if (sec) {
9085 put_extern_sym(sym, sec, addr, size);
9086 } else {
9087 Elf32_Sym *esym;
9088 /* put a common area */
9089 put_extern_sym(sym, NULL, align, size);
9090 /* XXX: find a nicer way */
9091 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
9092 esym->st_shndx = SHN_COMMON;
9094 } else {
9095 CValue cval;
9097 /* push global reference */
9098 sym = get_sym_ref(type, sec, addr, size);
9099 cval.ul = 0;
9100 vsetc(type, VT_CONST | VT_SYM, &cval);
9101 vtop->sym = sym;
9104 /* handles bounds now because the symbol must be defined
9105 before for the relocation */
9106 if (do_bounds_check) {
9107 unsigned long *bounds_ptr;
9109 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9110 /* then add global bound info */
9111 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9112 bounds_ptr[0] = 0; /* relocated */
9113 bounds_ptr[1] = size;
9116 if (has_init) {
9117 decl_initializer(type, sec, addr, 1, 0);
9118 /* restore parse state if needed */
9119 if (init_str.str) {
9120 tok_str_free(init_str.str);
9121 restore_parse_state(&saved_parse_state);
9124 no_alloc: ;
9127 void put_func_debug(Sym *sym)
9129 char buf[512];
9131 /* stabs info */
9132 /* XXX: we put here a dummy type */
9133 snprintf(buf, sizeof(buf), "%s:%c1",
9134 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9135 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9136 cur_text_section, sym->c);
9137 last_ind = 0;
9138 last_line_num = 0;
9141 /* parse an old style function declaration list */
9142 /* XXX: check multiple parameter */
9143 static void func_decl_list(Sym *func_sym)
9145 AttributeDef ad;
9146 int v;
9147 Sym *s;
9148 CType btype, type;
9150 /* parse each declaration */
9151 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9152 if (!parse_btype(&btype, &ad))
9153 expect("declaration list");
9154 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9155 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9156 tok == ';') {
9157 /* we accept no variable after */
9158 } else {
9159 for(;;) {
9160 type = btype;
9161 type_decl(&type, &ad, &v, TYPE_DIRECT);
9162 /* find parameter in function parameter list */
9163 s = func_sym->next;
9164 while (s != NULL) {
9165 if ((s->v & ~SYM_FIELD) == v)
9166 goto found;
9167 s = s->next;
9169 error("declaration for parameter '%s' but no such parameter",
9170 get_tok_str(v, NULL));
9171 found:
9172 /* check that no storage specifier except 'register' was given */
9173 if (type.t & VT_STORAGE)
9174 error("storage class specified for '%s'", get_tok_str(v, NULL));
9175 convert_parameter_type(&type);
9176 /* we can add the type (NOTE: it could be local to the function) */
9177 s->type = type;
9178 /* accept other parameters */
9179 if (tok == ',')
9180 next();
9181 else
9182 break;
9185 skip(';');
9189 /* parse a function defined by symbol 'sym' and generate its code in
9190 'cur_text_section' */
9191 static void gen_function(Sym *sym)
9193 int saved_nocode_wanted = nocode_wanted;
9194 nocode_wanted = 0;
9195 ind = cur_text_section->data_offset;
9196 /* NOTE: we patch the symbol size later */
9197 put_extern_sym(sym, cur_text_section, ind, 0);
9198 funcname = get_tok_str(sym->v, NULL);
9199 func_ind = ind;
9200 /* put debug symbol */
9201 if (do_debug)
9202 put_func_debug(sym);
9203 /* push a dummy symbol to enable local sym storage */
9204 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9205 gfunc_prolog(&sym->type);
9206 rsym = 0;
9207 block(NULL, NULL, NULL, NULL, 0, 0);
9208 gsym(rsym);
9209 gfunc_epilog();
9210 cur_text_section->data_offset = ind;
9211 label_pop(&global_label_stack, NULL);
9212 sym_pop(&local_stack, NULL); /* reset local stack */
9213 /* end of function */
9214 /* patch symbol size */
9215 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
9216 ind - func_ind;
9217 if (do_debug) {
9218 put_stabn(N_FUN, 0, 0, ind - func_ind);
9220 funcname = ""; /* for safety */
9221 func_vt.t = VT_VOID; /* for safety */
9222 ind = 0; /* for safety */
9223 nocode_wanted = saved_nocode_wanted;
9226 static void gen_inline_functions(void)
9228 Sym *sym;
9229 CType *type;
9230 int *str, inline_generated;
9232 /* iterate while inline function are referenced */
9233 for(;;) {
9234 inline_generated = 0;
9235 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9236 type = &sym->type;
9237 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9238 (type->t & (VT_STATIC | VT_INLINE)) ==
9239 (VT_STATIC | VT_INLINE) &&
9240 sym->c != 0) {
9241 /* the function was used: generate its code and
9242 convert it to a normal function */
9243 str = INLINE_DEF(sym->r);
9244 sym->r = VT_SYM | VT_CONST;
9245 sym->type.t &= ~VT_INLINE;
9247 macro_ptr = str;
9248 next();
9249 cur_text_section = text_section;
9250 gen_function(sym);
9251 macro_ptr = NULL; /* fail safe */
9253 tok_str_free(str);
9254 inline_generated = 1;
9257 if (!inline_generated)
9258 break;
9261 /* free all remaining inline function tokens */
9262 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9263 type = &sym->type;
9264 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9265 (type->t & (VT_STATIC | VT_INLINE)) ==
9266 (VT_STATIC | VT_INLINE)) {
9267 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9268 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9269 continue;
9270 str = INLINE_DEF(sym->r);
9271 tok_str_free(str);
9272 sym->r = 0; /* fail safe */
9277 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9278 static void decl(int l)
9280 int v, has_init, r;
9281 CType type, btype;
9282 Sym *sym;
9283 AttributeDef ad;
9285 while (1) {
9286 if (!parse_btype(&btype, &ad)) {
9287 /* skip redundant ';' */
9288 /* XXX: find more elegant solution */
9289 if (tok == ';') {
9290 next();
9291 continue;
9293 if (l == VT_CONST &&
9294 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9295 /* global asm block */
9296 asm_global_instr();
9297 continue;
9299 /* special test for old K&R protos without explicit int
9300 type. Only accepted when defining global data */
9301 if (l == VT_LOCAL || tok < TOK_DEFINE)
9302 break;
9303 btype.t = VT_INT;
9305 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9306 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9307 tok == ';') {
9308 /* we accept no variable after */
9309 next();
9310 continue;
9312 while (1) { /* iterate thru each declaration */
9313 type = btype;
9314 type_decl(&type, &ad, &v, TYPE_DIRECT);
9315 #if 0
9317 char buf[500];
9318 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9319 printf("type = '%s'\n", buf);
9321 #endif
9322 if ((type.t & VT_BTYPE) == VT_FUNC) {
9323 /* if old style function prototype, we accept a
9324 declaration list */
9325 sym = type.ref;
9326 if (sym->c == FUNC_OLD)
9327 func_decl_list(sym);
9330 if (tok == '{') {
9331 if (l == VT_LOCAL)
9332 error("cannot use local functions");
9333 if ((type.t & VT_BTYPE) != VT_FUNC)
9334 expect("function definition");
9336 /* reject abstract declarators in function definition */
9337 sym = type.ref;
9338 while ((sym = sym->next) != NULL)
9339 if (!(sym->v & ~SYM_FIELD))
9340 expect("identifier");
9342 /* XXX: cannot do better now: convert extern line to static inline */
9343 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9344 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9346 sym = sym_find(v);
9347 if (sym) {
9348 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9349 goto func_error1;
9350 /* specific case: if not func_call defined, we put
9351 the one of the prototype */
9352 /* XXX: should have default value */
9353 r = sym->type.ref->r;
9354 if (FUNC_CALL(r) != FUNC_CDECL
9355 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9356 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9357 if (FUNC_EXPORT(r))
9358 FUNC_EXPORT(type.ref->r) = 1;
9360 if (!is_compatible_types(&sym->type, &type)) {
9361 func_error1:
9362 error("incompatible types for redefinition of '%s'",
9363 get_tok_str(v, NULL));
9365 /* if symbol is already defined, then put complete type */
9366 sym->type = type;
9367 } else {
9368 /* put function symbol */
9369 sym = global_identifier_push(v, type.t, 0);
9370 sym->type.ref = type.ref;
9373 /* static inline functions are just recorded as a kind
9374 of macro. Their code will be emitted at the end of
9375 the compilation unit only if they are used */
9376 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9377 (VT_INLINE | VT_STATIC)) {
9378 TokenString func_str;
9379 int block_level;
9381 tok_str_new(&func_str);
9383 block_level = 0;
9384 for(;;) {
9385 int t;
9386 if (tok == TOK_EOF)
9387 error("unexpected end of file");
9388 tok_str_add_tok(&func_str);
9389 t = tok;
9390 next();
9391 if (t == '{') {
9392 block_level++;
9393 } else if (t == '}') {
9394 block_level--;
9395 if (block_level == 0)
9396 break;
9399 tok_str_add(&func_str, -1);
9400 tok_str_add(&func_str, 0);
9401 INLINE_DEF(sym->r) = func_str.str;
9402 } else {
9403 /* compute text section */
9404 cur_text_section = ad.section;
9405 if (!cur_text_section)
9406 cur_text_section = text_section;
9407 sym->r = VT_SYM | VT_CONST;
9408 gen_function(sym);
9410 break;
9411 } else {
9412 if (btype.t & VT_TYPEDEF) {
9413 /* save typedefed type */
9414 /* XXX: test storage specifiers ? */
9415 sym = sym_push(v, &type, 0, 0);
9416 sym->type.t |= VT_TYPEDEF;
9417 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9418 /* external function definition */
9419 /* specific case for func_call attribute */
9420 if (ad.func_attr)
9421 type.ref->r = ad.func_attr;
9422 external_sym(v, &type, 0);
9423 } else {
9424 /* not lvalue if array */
9425 r = 0;
9426 if (!(type.t & VT_ARRAY))
9427 r |= lvalue_type(type.t);
9428 has_init = (tok == '=');
9429 if ((btype.t & VT_EXTERN) ||
9430 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9431 !has_init && l == VT_CONST && type.ref->c < 0)) {
9432 /* external variable */
9433 /* NOTE: as GCC, uninitialized global static
9434 arrays of null size are considered as
9435 extern */
9436 external_sym(v, &type, r);
9437 } else {
9438 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9439 if (type.t & VT_STATIC)
9440 r |= VT_CONST;
9441 else
9442 r |= l;
9443 if (has_init)
9444 next();
9445 decl_initializer_alloc(&type, &ad, r,
9446 has_init, v, l);
9449 if (tok != ',') {
9450 skip(';');
9451 break;
9453 next();
9459 /* better than nothing, but needs extension to handle '-E' option
9460 correctly too */
9461 static void preprocess_init(TCCState *s1)
9463 s1->include_stack_ptr = s1->include_stack;
9464 /* XXX: move that before to avoid having to initialize
9465 file->ifdef_stack_ptr ? */
9466 s1->ifdef_stack_ptr = s1->ifdef_stack;
9467 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9469 /* XXX: not ANSI compliant: bound checking says error */
9470 vtop = vstack - 1;
9471 s1->pack_stack[0] = 0;
9472 s1->pack_stack_ptr = s1->pack_stack;
9475 /* compile the C file opened in 'file'. Return non zero if errors. */
9476 static int tcc_compile(TCCState *s1)
9478 Sym *define_start;
9479 char buf[512];
9480 volatile int section_sym;
9482 #ifdef INC_DEBUG
9483 printf("%s: **** new file\n", file->filename);
9484 #endif
9485 preprocess_init(s1);
9487 funcname = "";
9488 anon_sym = SYM_FIRST_ANOM;
9490 /* file info: full path + filename */
9491 section_sym = 0; /* avoid warning */
9492 if (do_debug) {
9493 section_sym = put_elf_sym(symtab_section, 0, 0,
9494 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
9495 text_section->sh_num, NULL);
9496 getcwd(buf, sizeof(buf));
9497 #ifdef _WIN32
9498 normalize_slashes(buf);
9499 #endif
9500 pstrcat(buf, sizeof(buf), "/");
9501 put_stabs_r(buf, N_SO, 0, 0,
9502 text_section->data_offset, text_section, section_sym);
9503 put_stabs_r(file->filename, N_SO, 0, 0,
9504 text_section->data_offset, text_section, section_sym);
9506 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9507 symbols can be safely used */
9508 put_elf_sym(symtab_section, 0, 0,
9509 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
9510 SHN_ABS, file->filename);
9512 /* define some often used types */
9513 int_type.t = VT_INT;
9515 char_pointer_type.t = VT_BYTE;
9516 mk_pointer(&char_pointer_type);
9518 func_old_type.t = VT_FUNC;
9519 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9521 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9522 float_type.t = VT_FLOAT;
9523 double_type.t = VT_DOUBLE;
9525 func_float_type.t = VT_FUNC;
9526 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9527 func_double_type.t = VT_FUNC;
9528 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9529 #endif
9531 #if 0
9532 /* define 'void *alloca(unsigned int)' builtin function */
9534 Sym *s1;
9536 p = anon_sym++;
9537 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9538 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9539 s1->next = NULL;
9540 sym->next = s1;
9541 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9543 #endif
9545 define_start = define_stack;
9546 nocode_wanted = 1;
9548 if (setjmp(s1->error_jmp_buf) == 0) {
9549 s1->nb_errors = 0;
9550 s1->error_set_jmp_enabled = 1;
9552 ch = file->buf_ptr[0];
9553 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9554 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9555 next();
9556 decl(VT_CONST);
9557 if (tok != TOK_EOF)
9558 expect("declaration");
9560 /* end of translation unit info */
9561 if (do_debug) {
9562 put_stabs_r(NULL, N_SO, 0, 0,
9563 text_section->data_offset, text_section, section_sym);
9566 s1->error_set_jmp_enabled = 0;
9568 /* reset define stack, but leave -Dsymbols (may be incorrect if
9569 they are undefined) */
9570 free_defines(define_start);
9572 gen_inline_functions();
9574 sym_pop(&global_stack, NULL);
9576 return s1->nb_errors != 0 ? -1 : 0;
9579 /* Preprocess the current file */
9580 /* XXX: add line and file infos, add options to preserve spaces */
9581 static int tcc_preprocess(TCCState *s1)
9583 Sym *define_start;
9584 int last_is_space;
9586 preprocess_init(s1);
9588 define_start = define_stack;
9590 ch = file->buf_ptr[0];
9591 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9592 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9593 PARSE_FLAG_LINEFEED;
9594 last_is_space = 1;
9595 next();
9596 for(;;) {
9597 if (tok == TOK_EOF) {
9598 break;
9599 } else if (tok == TOK_LINEFEED) {
9600 last_is_space = 1;
9601 } else {
9602 if (!last_is_space)
9603 fputc(' ', s1->outfile);
9604 last_is_space = 0;
9606 fputs(get_tok_str(tok, &tokc), s1->outfile);
9607 next();
9609 free_defines(define_start);
9610 return 0;
9613 #ifdef LIBTCC
9614 int tcc_compile_string(TCCState *s, const char *str)
9616 BufferedFile bf1, *bf = &bf1;
9617 int ret, len;
9618 char *buf;
9620 /* init file structure */
9621 bf->fd = -1;
9622 /* XXX: avoid copying */
9623 len = strlen(str);
9624 buf = tcc_malloc(len + 1);
9625 if (!buf)
9626 return -1;
9627 memcpy(buf, str, len);
9628 buf[len] = CH_EOB;
9629 bf->buf_ptr = buf;
9630 bf->buf_end = buf + len;
9631 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9632 bf->line_num = 1;
9633 file = bf;
9635 ret = tcc_compile(s);
9637 tcc_free(buf);
9639 /* currently, no need to close */
9640 return ret;
9642 #endif
9644 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9645 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9647 BufferedFile bf1, *bf = &bf1;
9649 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9650 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9651 /* default value */
9652 if (!value)
9653 value = "1";
9654 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9656 /* init file structure */
9657 bf->fd = -1;
9658 bf->buf_ptr = bf->buffer;
9659 bf->buf_end = bf->buffer + strlen(bf->buffer);
9660 *bf->buf_end = CH_EOB;
9661 bf->filename[0] = '\0';
9662 bf->line_num = 1;
9663 file = bf;
9665 s1->include_stack_ptr = s1->include_stack;
9667 /* parse with define parser */
9668 ch = file->buf_ptr[0];
9669 next_nomacro();
9670 parse_define();
9671 file = NULL;
9674 /* undefine a preprocessor symbol */
9675 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9677 TokenSym *ts;
9678 Sym *s;
9679 ts = tok_alloc(sym, strlen(sym));
9680 s = define_find(ts->tok);
9681 /* undefine symbol by putting an invalid name */
9682 if (s)
9683 define_undef(s);
9686 #ifdef CONFIG_TCC_ASM
9688 #ifdef TCC_TARGET_I386
9689 #include "i386-asm.c"
9690 #endif
9691 #include "tccasm.c"
9693 #else
9694 static void asm_instr(void)
9696 error("inline asm() not supported");
9698 static void asm_global_instr(void)
9700 error("inline asm() not supported");
9702 #endif
9704 #include "tccelf.c"
9706 #ifdef TCC_TARGET_COFF
9707 #include "tcccoff.c"
9708 #endif
9710 #ifdef TCC_TARGET_PE
9711 #include "tccpe.c"
9712 #endif
9714 /* print the position in the source file of PC value 'pc' by reading
9715 the stabs debug information */
9716 static void rt_printline(unsigned long wanted_pc)
9718 Stab_Sym *sym, *sym_end;
9719 char func_name[128], last_func_name[128];
9720 unsigned long func_addr, last_pc, pc;
9721 const char *incl_files[INCLUDE_STACK_SIZE];
9722 int incl_index, len, last_line_num, i;
9723 const char *str, *p;
9725 fprintf(stderr, "0x%08lx:", wanted_pc);
9727 func_name[0] = '\0';
9728 func_addr = 0;
9729 incl_index = 0;
9730 last_func_name[0] = '\0';
9731 last_pc = 0xffffffff;
9732 last_line_num = 1;
9733 sym = (Stab_Sym *)stab_section->data + 1;
9734 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9735 while (sym < sym_end) {
9736 switch(sym->n_type) {
9737 /* function start or end */
9738 case N_FUN:
9739 if (sym->n_strx == 0) {
9740 /* we test if between last line and end of function */
9741 pc = sym->n_value + func_addr;
9742 if (wanted_pc >= last_pc && wanted_pc < pc)
9743 goto found;
9744 func_name[0] = '\0';
9745 func_addr = 0;
9746 } else {
9747 str = stabstr_section->data + sym->n_strx;
9748 p = strchr(str, ':');
9749 if (!p) {
9750 pstrcpy(func_name, sizeof(func_name), str);
9751 } else {
9752 len = p - str;
9753 if (len > sizeof(func_name) - 1)
9754 len = sizeof(func_name) - 1;
9755 memcpy(func_name, str, len);
9756 func_name[len] = '\0';
9758 func_addr = sym->n_value;
9760 break;
9761 /* line number info */
9762 case N_SLINE:
9763 pc = sym->n_value + func_addr;
9764 if (wanted_pc >= last_pc && wanted_pc < pc)
9765 goto found;
9766 last_pc = pc;
9767 last_line_num = sym->n_desc;
9768 /* XXX: slow! */
9769 strcpy(last_func_name, func_name);
9770 break;
9771 /* include files */
9772 case N_BINCL:
9773 str = stabstr_section->data + sym->n_strx;
9774 add_incl:
9775 if (incl_index < INCLUDE_STACK_SIZE) {
9776 incl_files[incl_index++] = str;
9778 break;
9779 case N_EINCL:
9780 if (incl_index > 1)
9781 incl_index--;
9782 break;
9783 case N_SO:
9784 if (sym->n_strx == 0) {
9785 incl_index = 0; /* end of translation unit */
9786 } else {
9787 str = stabstr_section->data + sym->n_strx;
9788 /* do not add path */
9789 len = strlen(str);
9790 if (len > 0 && str[len - 1] != '/')
9791 goto add_incl;
9793 break;
9795 sym++;
9798 /* second pass: we try symtab symbols (no line number info) */
9799 incl_index = 0;
9801 Elf32_Sym *sym, *sym_end;
9802 int type;
9804 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
9805 for(sym = (Elf32_Sym *)symtab_section->data + 1;
9806 sym < sym_end;
9807 sym++) {
9808 type = ELF32_ST_TYPE(sym->st_info);
9809 if (type == STT_FUNC) {
9810 if (wanted_pc >= sym->st_value &&
9811 wanted_pc < sym->st_value + sym->st_size) {
9812 pstrcpy(last_func_name, sizeof(last_func_name),
9813 strtab_section->data + sym->st_name);
9814 goto found;
9819 /* did not find any info: */
9820 fprintf(stderr, " ???\n");
9821 return;
9822 found:
9823 if (last_func_name[0] != '\0') {
9824 fprintf(stderr, " %s()", last_func_name);
9826 if (incl_index > 0) {
9827 fprintf(stderr, " (%s:%d",
9828 incl_files[incl_index - 1], last_line_num);
9829 for(i = incl_index - 2; i >= 0; i--)
9830 fprintf(stderr, ", included from %s", incl_files[i]);
9831 fprintf(stderr, ")");
9833 fprintf(stderr, "\n");
9836 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
9838 #ifdef __i386__
9840 /* fix for glibc 2.1 */
9841 #ifndef REG_EIP
9842 #define REG_EIP EIP
9843 #define REG_EBP EBP
9844 #endif
9846 /* return the PC at frame level 'level'. Return non zero if not found */
9847 static int rt_get_caller_pc(unsigned long *paddr,
9848 ucontext_t *uc, int level)
9850 unsigned long fp;
9851 int i;
9853 if (level == 0) {
9854 #if defined(__FreeBSD__)
9855 *paddr = uc->uc_mcontext.mc_eip;
9856 #elif defined(__dietlibc__)
9857 *paddr = uc->uc_mcontext.eip;
9858 #else
9859 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9860 #endif
9861 return 0;
9862 } else {
9863 #if defined(__FreeBSD__)
9864 fp = uc->uc_mcontext.mc_ebp;
9865 #elif defined(__dietlibc__)
9866 fp = uc->uc_mcontext.ebp;
9867 #else
9868 fp = uc->uc_mcontext.gregs[REG_EBP];
9869 #endif
9870 for(i=1;i<level;i++) {
9871 /* XXX: check address validity with program info */
9872 if (fp <= 0x1000 || fp >= 0xc0000000)
9873 return -1;
9874 fp = ((unsigned long *)fp)[0];
9876 *paddr = ((unsigned long *)fp)[1];
9877 return 0;
9880 #else
9882 #warning add arch specific rt_get_caller_pc()
9884 static int rt_get_caller_pc(unsigned long *paddr,
9885 ucontext_t *uc, int level)
9887 return -1;
9889 #endif
9891 /* emit a run time error at position 'pc' */
9892 void rt_error(ucontext_t *uc, const char *fmt, ...)
9894 va_list ap;
9895 unsigned long pc;
9896 int i;
9898 va_start(ap, fmt);
9899 fprintf(stderr, "Runtime error: ");
9900 vfprintf(stderr, fmt, ap);
9901 fprintf(stderr, "\n");
9902 for(i=0;i<num_callers;i++) {
9903 if (rt_get_caller_pc(&pc, uc, i) < 0)
9904 break;
9905 if (i == 0)
9906 fprintf(stderr, "at ");
9907 else
9908 fprintf(stderr, "by ");
9909 rt_printline(pc);
9911 exit(255);
9912 va_end(ap);
9915 /* signal handler for fatal errors */
9916 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9918 ucontext_t *uc = puc;
9920 switch(signum) {
9921 case SIGFPE:
9922 switch(siginf->si_code) {
9923 case FPE_INTDIV:
9924 case FPE_FLTDIV:
9925 rt_error(uc, "division by zero");
9926 break;
9927 default:
9928 rt_error(uc, "floating point exception");
9929 break;
9931 break;
9932 case SIGBUS:
9933 case SIGSEGV:
9934 if (rt_bound_error_msg && *rt_bound_error_msg)
9935 rt_error(uc, *rt_bound_error_msg);
9936 else
9937 rt_error(uc, "dereferencing invalid pointer");
9938 break;
9939 case SIGILL:
9940 rt_error(uc, "illegal instruction");
9941 break;
9942 case SIGABRT:
9943 rt_error(uc, "abort() called");
9944 break;
9945 default:
9946 rt_error(uc, "caught signal %d", signum);
9947 break;
9949 exit(255);
9951 #endif
9953 /* do all relocations (needed before using tcc_get_symbol()) */
9954 int tcc_relocate(TCCState *s1)
9956 Section *s;
9957 int i;
9959 s1->nb_errors = 0;
9961 #ifdef TCC_TARGET_PE
9962 pe_add_runtime(s1);
9963 #else
9964 tcc_add_runtime(s1);
9965 #endif
9967 relocate_common_syms();
9969 tcc_add_linker_symbols(s1);
9970 #ifndef TCC_TARGET_PE
9971 build_got_entries(s1);
9972 #endif
9973 /* compute relocation address : section are relocated in place. We
9974 also alloc the bss space */
9975 for(i = 1; i < s1->nb_sections; i++) {
9976 s = s1->sections[i];
9977 if (s->sh_flags & SHF_ALLOC) {
9978 if (s->sh_type == SHT_NOBITS)
9979 s->data = tcc_mallocz(s->data_offset);
9980 s->sh_addr = (unsigned long)s->data;
9984 relocate_syms(s1, 1);
9986 if (s1->nb_errors != 0)
9987 return -1;
9989 /* relocate each section */
9990 for(i = 1; i < s1->nb_sections; i++) {
9991 s = s1->sections[i];
9992 if (s->reloc)
9993 relocate_section(s1, s);
9996 /* mark executable sections as executable in memory */
9997 for(i = 1; i < s1->nb_sections; i++) {
9998 s = s1->sections[i];
9999 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10000 (SHF_ALLOC | SHF_EXECINSTR))
10001 set_pages_executable(s->data, s->data_offset);
10003 return 0;
10006 /* launch the compiled program with the given arguments */
10007 int tcc_run(TCCState *s1, int argc, char **argv)
10009 int (*prog_main)(int, char **);
10011 if (tcc_relocate(s1) < 0)
10012 return -1;
10014 prog_main = tcc_get_symbol_err(s1, "main");
10016 if (do_debug) {
10017 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10018 error("debug mode currently not available for Windows");
10019 #else
10020 struct sigaction sigact;
10021 /* install TCC signal handlers to print debug info on fatal
10022 runtime errors */
10023 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10024 sigact.sa_sigaction = sig_error;
10025 sigemptyset(&sigact.sa_mask);
10026 sigaction(SIGFPE, &sigact, NULL);
10027 sigaction(SIGILL, &sigact, NULL);
10028 sigaction(SIGSEGV, &sigact, NULL);
10029 sigaction(SIGBUS, &sigact, NULL);
10030 sigaction(SIGABRT, &sigact, NULL);
10031 #endif
10034 #ifdef CONFIG_TCC_BCHECK
10035 if (do_bounds_check) {
10036 void (*bound_init)(void);
10038 /* set error function */
10039 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10040 "__bound_error_msg");
10042 /* XXX: use .init section so that it also work in binary ? */
10043 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10044 bound_init();
10046 #endif
10047 return (*prog_main)(argc, argv);
10050 TCCState *tcc_new(void)
10052 const char *p, *r;
10053 TCCState *s;
10054 TokenSym *ts;
10055 int i, c;
10057 s = tcc_mallocz(sizeof(TCCState));
10058 if (!s)
10059 return NULL;
10060 tcc_state = s;
10061 s->output_type = TCC_OUTPUT_MEMORY;
10063 /* init isid table */
10064 for(i=0;i<256;i++)
10065 isidnum_table[i] = isid(i) || isnum(i);
10067 /* add all tokens */
10068 table_ident = NULL;
10069 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10071 tok_ident = TOK_IDENT;
10072 p = tcc_keywords;
10073 while (*p) {
10074 r = p;
10075 for(;;) {
10076 c = *r++;
10077 if (c == '\0')
10078 break;
10080 ts = tok_alloc(p, r - p - 1);
10081 p = r;
10084 /* we add dummy defines for some special macros to speed up tests
10085 and to have working defined() */
10086 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10087 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10088 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10089 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10091 /* standard defines */
10092 tcc_define_symbol(s, "__STDC__", NULL);
10093 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10094 #if defined(TCC_TARGET_I386)
10095 tcc_define_symbol(s, "__i386__", NULL);
10096 #endif
10097 #if defined(TCC_TARGET_ARM)
10098 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10099 tcc_define_symbol(s, "__arm_elf__", NULL);
10100 tcc_define_symbol(s, "__arm_elf", NULL);
10101 tcc_define_symbol(s, "arm_elf", NULL);
10102 tcc_define_symbol(s, "__arm__", NULL);
10103 tcc_define_symbol(s, "__arm", NULL);
10104 tcc_define_symbol(s, "arm", NULL);
10105 tcc_define_symbol(s, "__APCS_32__", NULL);
10106 #endif
10107 #ifdef TCC_TARGET_PE
10108 tcc_define_symbol(s, "_WIN32", NULL);
10109 #else
10110 tcc_define_symbol(s, "__unix__", NULL);
10111 tcc_define_symbol(s, "__unix", NULL);
10112 #if defined(__linux)
10113 tcc_define_symbol(s, "__linux__", NULL);
10114 tcc_define_symbol(s, "__linux", NULL);
10115 #endif
10116 #endif
10117 /* tiny C specific defines */
10118 tcc_define_symbol(s, "__TINYC__", NULL);
10120 /* tiny C & gcc defines */
10121 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10122 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10123 #ifdef TCC_TARGET_PE
10124 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10125 #else
10126 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10127 #endif
10129 #ifndef TCC_TARGET_PE
10130 /* default library paths */
10131 tcc_add_library_path(s, "/usr/local/lib");
10132 tcc_add_library_path(s, "/usr/lib");
10133 tcc_add_library_path(s, "/lib");
10134 #endif
10136 /* no section zero */
10137 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10139 /* create standard sections */
10140 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10141 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10142 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10144 /* symbols are always generated for linking stage */
10145 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10146 ".strtab",
10147 ".hashtab", SHF_PRIVATE);
10148 strtab_section = symtab_section->link;
10150 /* private symbol table for dynamic symbols */
10151 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10152 ".dynstrtab",
10153 ".dynhashtab", SHF_PRIVATE);
10154 s->alacarte_link = 1;
10156 #ifdef CHAR_IS_UNSIGNED
10157 s->char_is_unsigned = 1;
10158 #endif
10159 #if defined(TCC_TARGET_PE) && 0
10160 /* XXX: currently the PE linker is not ready to support that */
10161 s->leading_underscore = 1;
10162 #endif
10163 return s;
10166 void tcc_delete(TCCState *s1)
10168 int i, n;
10170 /* free -D defines */
10171 free_defines(NULL);
10173 /* free tokens */
10174 n = tok_ident - TOK_IDENT;
10175 for(i = 0; i < n; i++)
10176 tcc_free(table_ident[i]);
10177 tcc_free(table_ident);
10179 /* free all sections */
10181 free_section(symtab_section->hash);
10183 free_section(s1->dynsymtab_section->hash);
10184 free_section(s1->dynsymtab_section->link);
10185 free_section(s1->dynsymtab_section);
10187 for(i = 1; i < s1->nb_sections; i++)
10188 free_section(s1->sections[i]);
10189 tcc_free(s1->sections);
10191 /* free loaded dlls array */
10192 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10194 /* free library paths */
10195 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10197 /* free include paths */
10198 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10199 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10200 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10202 tcc_free(s1);
10205 int tcc_add_include_path(TCCState *s1, const char *pathname)
10207 char *pathname1;
10209 pathname1 = tcc_strdup(pathname);
10210 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10211 return 0;
10214 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10216 char *pathname1;
10218 pathname1 = tcc_strdup(pathname);
10219 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10220 return 0;
10223 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10225 const char *ext;
10226 Elf32_Ehdr ehdr;
10227 int fd, ret;
10228 BufferedFile *saved_file;
10230 /* find source file type with extension */
10231 ext = tcc_fileextension(filename);
10232 if (ext[0])
10233 ext++;
10235 /* open the file */
10236 saved_file = file;
10237 file = tcc_open(s1, filename);
10238 if (!file) {
10239 if (flags & AFF_PRINT_ERROR) {
10240 error_noabort("file '%s' not found", filename);
10242 ret = -1;
10243 goto fail1;
10246 if (flags & AFF_PREPROCESS) {
10247 ret = tcc_preprocess(s1);
10248 } else if (!ext[0] || !strcmp(ext, "c")) {
10249 /* C file assumed */
10250 ret = tcc_compile(s1);
10251 } else
10252 #ifdef CONFIG_TCC_ASM
10253 if (!strcmp(ext, "S")) {
10254 /* preprocessed assembler */
10255 ret = tcc_assemble(s1, 1);
10256 } else if (!strcmp(ext, "s")) {
10257 /* non preprocessed assembler */
10258 ret = tcc_assemble(s1, 0);
10259 } else
10260 #endif
10261 #ifdef TCC_TARGET_PE
10262 if (!strcmp(ext, "def")) {
10263 ret = pe_load_def_file(s1, file->fd);
10264 } else
10265 #endif
10267 fd = file->fd;
10268 /* assume executable format: auto guess file type */
10269 ret = read(fd, &ehdr, sizeof(ehdr));
10270 lseek(fd, 0, SEEK_SET);
10271 if (ret <= 0) {
10272 error_noabort("could not read header");
10273 goto fail;
10274 } else if (ret != sizeof(ehdr)) {
10275 goto try_load_script;
10278 if (ehdr.e_ident[0] == ELFMAG0 &&
10279 ehdr.e_ident[1] == ELFMAG1 &&
10280 ehdr.e_ident[2] == ELFMAG2 &&
10281 ehdr.e_ident[3] == ELFMAG3) {
10282 file->line_num = 0; /* do not display line number if error */
10283 if (ehdr.e_type == ET_REL) {
10284 ret = tcc_load_object_file(s1, fd, 0);
10285 } else if (ehdr.e_type == ET_DYN) {
10286 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10287 #ifdef TCC_TARGET_PE
10288 ret = -1;
10289 #else
10290 void *h;
10291 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10292 if (h)
10293 ret = 0;
10294 else
10295 ret = -1;
10296 #endif
10297 } else {
10298 ret = tcc_load_dll(s1, fd, filename,
10299 (flags & AFF_REFERENCED_DLL) != 0);
10301 } else {
10302 error_noabort("unrecognized ELF file");
10303 goto fail;
10305 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10306 file->line_num = 0; /* do not display line number if error */
10307 ret = tcc_load_archive(s1, fd);
10308 } else
10309 #ifdef TCC_TARGET_COFF
10310 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10311 ret = tcc_load_coff(s1, fd);
10312 } else
10313 #endif
10314 #ifdef TCC_TARGET_PE
10315 if (pe_test_res_file(&ehdr, ret)) {
10316 ret = pe_load_res_file(s1, fd);
10317 } else
10318 #endif
10320 /* as GNU ld, consider it is an ld script if not recognized */
10321 try_load_script:
10322 ret = tcc_load_ldscript(s1);
10323 if (ret < 0) {
10324 error_noabort("unrecognized file type");
10325 goto fail;
10329 the_end:
10330 tcc_close(file);
10331 fail1:
10332 file = saved_file;
10333 return ret;
10334 fail:
10335 ret = -1;
10336 goto the_end;
10339 int tcc_add_file(TCCState *s, const char *filename)
10341 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10344 int tcc_add_library_path(TCCState *s, const char *pathname)
10346 char *pathname1;
10348 pathname1 = tcc_strdup(pathname);
10349 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10350 return 0;
10353 /* find and load a dll. Return non zero if not found */
10354 /* XXX: add '-rpath' option support ? */
10355 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10357 char buf[1024];
10358 int i;
10360 for(i = 0; i < s->nb_library_paths; i++) {
10361 snprintf(buf, sizeof(buf), "%s/%s",
10362 s->library_paths[i], filename);
10363 if (tcc_add_file_internal(s, buf, flags) == 0)
10364 return 0;
10366 return -1;
10369 /* the library name is the same as the argument of the '-l' option */
10370 int tcc_add_library(TCCState *s, const char *libraryname)
10372 char buf[1024];
10373 int i;
10375 /* first we look for the dynamic library if not static linking */
10376 if (!s->static_link) {
10377 #ifdef TCC_TARGET_PE
10378 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10379 #else
10380 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10381 #endif
10382 if (tcc_add_dll(s, buf, 0) == 0)
10383 return 0;
10386 /* then we look for the static library */
10387 for(i = 0; i < s->nb_library_paths; i++) {
10388 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10389 s->library_paths[i], libraryname);
10390 if (tcc_add_file_internal(s, buf, 0) == 0)
10391 return 0;
10393 return -1;
10396 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10398 add_elf_sym(symtab_section, val, 0,
10399 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10400 SHN_ABS, name);
10401 return 0;
10404 int tcc_set_output_type(TCCState *s, int output_type)
10406 char buf[1024];
10408 s->output_type = output_type;
10410 if (!s->nostdinc) {
10411 /* default include paths */
10412 /* XXX: reverse order needed if -isystem support */
10413 #ifndef TCC_TARGET_PE
10414 tcc_add_sysinclude_path(s, "/usr/local/include");
10415 tcc_add_sysinclude_path(s, "/usr/include");
10416 #endif
10417 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10418 tcc_add_sysinclude_path(s, buf);
10419 #ifdef TCC_TARGET_PE
10420 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10421 tcc_add_sysinclude_path(s, buf);
10422 #endif
10425 /* if bound checking, then add corresponding sections */
10426 #ifdef CONFIG_TCC_BCHECK
10427 if (do_bounds_check) {
10428 /* define symbol */
10429 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10430 /* create bounds sections */
10431 bounds_section = new_section(s, ".bounds",
10432 SHT_PROGBITS, SHF_ALLOC);
10433 lbounds_section = new_section(s, ".lbounds",
10434 SHT_PROGBITS, SHF_ALLOC);
10436 #endif
10438 if (s->char_is_unsigned) {
10439 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10442 /* add debug sections */
10443 if (do_debug) {
10444 /* stab symbols */
10445 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10446 stab_section->sh_entsize = sizeof(Stab_Sym);
10447 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10448 put_elf_str(stabstr_section, "");
10449 stab_section->link = stabstr_section;
10450 /* put first entry */
10451 put_stabs("", 0, 0, 0, 0);
10454 /* add libc crt1/crti objects */
10455 #ifndef TCC_TARGET_PE
10456 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10457 !s->nostdlib) {
10458 if (output_type != TCC_OUTPUT_DLL)
10459 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10460 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10462 #endif
10464 #ifdef TCC_TARGET_PE
10465 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10466 tcc_add_library_path(s, buf);
10467 #endif
10469 return 0;
10472 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10473 #define FD_INVERT 0x0002 /* invert value before storing */
10475 typedef struct FlagDef {
10476 uint16_t offset;
10477 uint16_t flags;
10478 const char *name;
10479 } FlagDef;
10481 static const FlagDef warning_defs[] = {
10482 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10483 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10484 { offsetof(TCCState, warn_error), 0, "error" },
10485 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10486 "implicit-function-declaration" },
10489 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10490 const char *name, int value)
10492 int i;
10493 const FlagDef *p;
10494 const char *r;
10496 r = name;
10497 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10498 r += 3;
10499 value = !value;
10501 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10502 if (!strcmp(r, p->name))
10503 goto found;
10505 return -1;
10506 found:
10507 if (p->flags & FD_INVERT)
10508 value = !value;
10509 *(int *)((uint8_t *)s + p->offset) = value;
10510 return 0;
10514 /* set/reset a warning */
10515 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10517 int i;
10518 const FlagDef *p;
10520 if (!strcmp(warning_name, "all")) {
10521 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10522 if (p->flags & WD_ALL)
10523 *(int *)((uint8_t *)s + p->offset) = 1;
10525 return 0;
10526 } else {
10527 return set_flag(s, warning_defs, countof(warning_defs),
10528 warning_name, value);
10532 static const FlagDef flag_defs[] = {
10533 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10534 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10535 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10536 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10539 /* set/reset a flag */
10540 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10542 return set_flag(s, flag_defs, countof(flag_defs),
10543 flag_name, value);
10546 #if !defined(LIBTCC)
10548 static int64_t getclock_us(void)
10550 #ifdef _WIN32
10551 struct _timeb tb;
10552 _ftime(&tb);
10553 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10554 #else
10555 struct timeval tv;
10556 gettimeofday(&tv, NULL);
10557 return tv.tv_sec * 1000000LL + tv.tv_usec;
10558 #endif
10561 void help(void)
10563 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10564 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10565 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10566 " [-static] [infile1 infile2...] [-run infile args...]\n"
10567 "\n"
10568 "General options:\n"
10569 " -v display current version\n"
10570 " -c compile only - generate an object file\n"
10571 " -o outfile set output filename\n"
10572 " -Bdir set tcc internal library path\n"
10573 " -bench output compilation statistics\n"
10574 " -run run compiled source\n"
10575 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10576 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10577 " -w disable all warnings\n"
10578 "Preprocessor options:\n"
10579 " -E preprocess only\n"
10580 " -Idir add include path 'dir'\n"
10581 " -Dsym[=val] define 'sym' with value 'val'\n"
10582 " -Usym undefine 'sym'\n"
10583 "Linker options:\n"
10584 " -Ldir add library path 'dir'\n"
10585 " -llib link with dynamic or static library 'lib'\n"
10586 " -shared generate a shared library\n"
10587 " -soname set name for shared library to be used at runtime\n"
10588 " -static static linking\n"
10589 " -rdynamic export all global symbols to dynamic linker\n"
10590 " -r generate (relocatable) object file\n"
10591 "Debugger options:\n"
10592 " -g generate runtime debug info\n"
10593 #ifdef CONFIG_TCC_BCHECK
10594 " -b compile with built-in memory and bounds checker (implies -g)\n"
10595 #endif
10596 " -bt N show N callers in stack traces\n"
10600 #define TCC_OPTION_HAS_ARG 0x0001
10601 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10603 typedef struct TCCOption {
10604 const char *name;
10605 uint16_t index;
10606 uint16_t flags;
10607 } TCCOption;
10609 enum {
10610 TCC_OPTION_HELP,
10611 TCC_OPTION_I,
10612 TCC_OPTION_D,
10613 TCC_OPTION_U,
10614 TCC_OPTION_L,
10615 TCC_OPTION_B,
10616 TCC_OPTION_l,
10617 TCC_OPTION_bench,
10618 TCC_OPTION_bt,
10619 TCC_OPTION_b,
10620 TCC_OPTION_g,
10621 TCC_OPTION_c,
10622 TCC_OPTION_static,
10623 TCC_OPTION_shared,
10624 TCC_OPTION_soname,
10625 TCC_OPTION_o,
10626 TCC_OPTION_r,
10627 TCC_OPTION_Wl,
10628 TCC_OPTION_W,
10629 TCC_OPTION_O,
10630 TCC_OPTION_m,
10631 TCC_OPTION_f,
10632 TCC_OPTION_nostdinc,
10633 TCC_OPTION_nostdlib,
10634 TCC_OPTION_print_search_dirs,
10635 TCC_OPTION_rdynamic,
10636 TCC_OPTION_run,
10637 TCC_OPTION_v,
10638 TCC_OPTION_w,
10639 TCC_OPTION_pipe,
10640 TCC_OPTION_E,
10643 static const TCCOption tcc_options[] = {
10644 { "h", TCC_OPTION_HELP, 0 },
10645 { "?", TCC_OPTION_HELP, 0 },
10646 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10647 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10648 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10649 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10650 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10651 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10652 { "bench", TCC_OPTION_bench, 0 },
10653 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10654 #ifdef CONFIG_TCC_BCHECK
10655 { "b", TCC_OPTION_b, 0 },
10656 #endif
10657 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10658 { "c", TCC_OPTION_c, 0 },
10659 { "static", TCC_OPTION_static, 0 },
10660 { "shared", TCC_OPTION_shared, 0 },
10661 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10662 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10663 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10664 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10665 { "r", TCC_OPTION_r, 0 },
10666 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10667 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10668 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10669 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10670 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10671 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10672 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10673 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10674 { "v", TCC_OPTION_v, 0 },
10675 { "w", TCC_OPTION_w, 0 },
10676 { "pipe", TCC_OPTION_pipe, 0},
10677 { "E", TCC_OPTION_E, 0},
10678 { NULL },
10681 /* convert 'str' into an array of space separated strings */
10682 static int expand_args(char ***pargv, const char *str)
10684 const char *s1;
10685 char **argv, *arg;
10686 int argc, len;
10688 argc = 0;
10689 argv = NULL;
10690 for(;;) {
10691 while (is_space(*str))
10692 str++;
10693 if (*str == '\0')
10694 break;
10695 s1 = str;
10696 while (*str != '\0' && !is_space(*str))
10697 str++;
10698 len = str - s1;
10699 arg = tcc_malloc(len + 1);
10700 memcpy(arg, s1, len);
10701 arg[len] = '\0';
10702 dynarray_add((void ***)&argv, &argc, arg);
10704 *pargv = argv;
10705 return argc;
10708 static char **files;
10709 static int nb_files, nb_libraries;
10710 static int multiple_files;
10711 static int print_search_dirs;
10712 static int output_type;
10713 static int reloc_output;
10714 static const char *outfile;
10716 int parse_args(TCCState *s, int argc, char **argv)
10718 int optind;
10719 const TCCOption *popt;
10720 const char *optarg, *p1, *r1;
10721 char *r;
10723 optind = 0;
10724 while (1) {
10725 if (optind >= argc) {
10726 if (nb_files == 0 && !print_search_dirs)
10727 goto show_help;
10728 else
10729 break;
10731 r = argv[optind++];
10732 if (r[0] != '-' || r[1] == '\0') {
10733 /* add a new file */
10734 dynarray_add((void ***)&files, &nb_files, r);
10735 if (!multiple_files) {
10736 optind--;
10737 /* argv[0] will be this file */
10738 break;
10740 } else {
10741 /* find option in table (match only the first chars */
10742 popt = tcc_options;
10743 for(;;) {
10744 p1 = popt->name;
10745 if (p1 == NULL)
10746 error("invalid option -- '%s'", r);
10747 r1 = r + 1;
10748 for(;;) {
10749 if (*p1 == '\0')
10750 goto option_found;
10751 if (*r1 != *p1)
10752 break;
10753 p1++;
10754 r1++;
10756 popt++;
10758 option_found:
10759 if (popt->flags & TCC_OPTION_HAS_ARG) {
10760 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
10761 optarg = r1;
10762 } else {
10763 if (optind >= argc)
10764 error("argument to '%s' is missing", r);
10765 optarg = argv[optind++];
10767 } else {
10768 if (*r1 != '\0')
10769 goto show_help;
10770 optarg = NULL;
10773 switch(popt->index) {
10774 case TCC_OPTION_HELP:
10775 show_help:
10776 help();
10777 exit(1);
10778 case TCC_OPTION_I:
10779 if (tcc_add_include_path(s, optarg) < 0)
10780 error("too many include paths");
10781 break;
10782 case TCC_OPTION_D:
10784 char *sym, *value;
10785 sym = (char *)optarg;
10786 value = strchr(sym, '=');
10787 if (value) {
10788 *value = '\0';
10789 value++;
10791 tcc_define_symbol(s, sym, value);
10793 break;
10794 case TCC_OPTION_U:
10795 tcc_undefine_symbol(s, optarg);
10796 break;
10797 case TCC_OPTION_L:
10798 tcc_add_library_path(s, optarg);
10799 break;
10800 case TCC_OPTION_B:
10801 /* set tcc utilities path (mainly for tcc development) */
10802 tcc_lib_path = optarg;
10803 break;
10804 case TCC_OPTION_l:
10805 dynarray_add((void ***)&files, &nb_files, r);
10806 nb_libraries++;
10807 break;
10808 case TCC_OPTION_bench:
10809 do_bench = 1;
10810 break;
10811 case TCC_OPTION_bt:
10812 num_callers = atoi(optarg);
10813 break;
10814 #ifdef CONFIG_TCC_BCHECK
10815 case TCC_OPTION_b:
10816 do_bounds_check = 1;
10817 do_debug = 1;
10818 break;
10819 #endif
10820 case TCC_OPTION_g:
10821 do_debug = 1;
10822 break;
10823 case TCC_OPTION_c:
10824 multiple_files = 1;
10825 output_type = TCC_OUTPUT_OBJ;
10826 break;
10827 case TCC_OPTION_static:
10828 s->static_link = 1;
10829 break;
10830 case TCC_OPTION_shared:
10831 output_type = TCC_OUTPUT_DLL;
10832 break;
10833 case TCC_OPTION_soname:
10834 s->soname = optarg;
10835 break;
10836 case TCC_OPTION_o:
10837 multiple_files = 1;
10838 outfile = optarg;
10839 break;
10840 case TCC_OPTION_r:
10841 /* generate a .o merging several output files */
10842 reloc_output = 1;
10843 output_type = TCC_OUTPUT_OBJ;
10844 break;
10845 case TCC_OPTION_nostdinc:
10846 s->nostdinc = 1;
10847 break;
10848 case TCC_OPTION_nostdlib:
10849 s->nostdlib = 1;
10850 break;
10851 case TCC_OPTION_print_search_dirs:
10852 print_search_dirs = 1;
10853 break;
10854 case TCC_OPTION_run:
10856 int argc1;
10857 char **argv1;
10858 argc1 = expand_args(&argv1, optarg);
10859 if (argc1 > 0) {
10860 parse_args(s, argc1, argv1);
10862 multiple_files = 0;
10863 output_type = TCC_OUTPUT_MEMORY;
10865 break;
10866 case TCC_OPTION_v:
10867 printf("tcc version %s\n", TCC_VERSION);
10868 exit(0);
10869 case TCC_OPTION_f:
10870 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
10871 goto unsupported_option;
10872 break;
10873 case TCC_OPTION_W:
10874 if (tcc_set_warning(s, optarg, 1) < 0 &&
10875 s->warn_unsupported)
10876 goto unsupported_option;
10877 break;
10878 case TCC_OPTION_w:
10879 s->warn_none = 1;
10880 break;
10881 case TCC_OPTION_rdynamic:
10882 s->rdynamic = 1;
10883 break;
10884 case TCC_OPTION_Wl:
10886 const char *p;
10887 if (strstart(optarg, "-Ttext,", &p)) {
10888 s->text_addr = strtoul(p, NULL, 16);
10889 s->has_text_addr = 1;
10890 } else if (strstart(optarg, "--oformat,", &p)) {
10891 if (strstart(p, "elf32-", NULL)) {
10892 s->output_format = TCC_OUTPUT_FORMAT_ELF;
10893 } else if (!strcmp(p, "binary")) {
10894 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
10895 } else
10896 #ifdef TCC_TARGET_COFF
10897 if (!strcmp(p, "coff")) {
10898 s->output_format = TCC_OUTPUT_FORMAT_COFF;
10899 } else
10900 #endif
10902 error("target %s not found", p);
10904 } else {
10905 error("unsupported linker option '%s'", optarg);
10908 break;
10909 case TCC_OPTION_E:
10910 output_type = TCC_OUTPUT_PREPROCESS;
10911 break;
10912 default:
10913 if (s->warn_unsupported) {
10914 unsupported_option:
10915 warning("unsupported option '%s'", r);
10917 break;
10921 return optind;
10924 int main(int argc, char **argv)
10926 int i;
10927 TCCState *s;
10928 int nb_objfiles, ret, optind;
10929 char objfilename[1024];
10930 int64_t start_time = 0;
10932 #ifdef _WIN32
10933 tcc_lib_path = w32_tcc_lib_path();
10934 #endif
10936 s = tcc_new();
10937 output_type = TCC_OUTPUT_EXE;
10938 outfile = NULL;
10939 multiple_files = 1;
10940 files = NULL;
10941 nb_files = 0;
10942 nb_libraries = 0;
10943 reloc_output = 0;
10944 print_search_dirs = 0;
10945 ret = 0;
10947 optind = parse_args(s, argc - 1, argv + 1) + 1;
10949 if (print_search_dirs) {
10950 /* enough for Linux kernel */
10951 printf("install: %s/\n", tcc_lib_path);
10952 return 0;
10955 nb_objfiles = nb_files - nb_libraries;
10957 /* if outfile provided without other options, we output an
10958 executable */
10959 if (outfile && output_type == TCC_OUTPUT_MEMORY)
10960 output_type = TCC_OUTPUT_EXE;
10962 /* check -c consistency : only single file handled. XXX: checks file type */
10963 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10964 /* accepts only a single input file */
10965 if (nb_objfiles != 1)
10966 error("cannot specify multiple files with -c");
10967 if (nb_libraries != 0)
10968 error("cannot specify libraries with -c");
10972 if (output_type == TCC_OUTPUT_PREPROCESS) {
10973 if (!outfile) {
10974 s->outfile = stdout;
10975 } else {
10976 s->outfile = fopen(outfile, "w");
10977 if (!s->outfile)
10978 error("could not open '%s", outfile);
10980 } else if (output_type != TCC_OUTPUT_MEMORY) {
10981 if (!outfile) {
10982 /* compute default outfile name */
10983 char *ext;
10984 const char *name =
10985 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
10986 pstrcpy(objfilename, sizeof(objfilename), name);
10987 ext = tcc_fileextension(objfilename);
10988 #ifdef TCC_TARGET_PE
10989 if (output_type == TCC_OUTPUT_DLL)
10990 strcpy(ext, ".dll");
10991 else
10992 if (output_type == TCC_OUTPUT_EXE)
10993 strcpy(ext, ".exe");
10994 else
10995 #endif
10996 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
10997 strcpy(ext, ".o");
10998 else
10999 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11000 outfile = objfilename;
11004 if (do_bench) {
11005 start_time = getclock_us();
11008 tcc_set_output_type(s, output_type);
11010 /* compile or add each files or library */
11011 for(i = 0; i < nb_files && ret == 0; i++) {
11012 const char *filename;
11014 filename = files[i];
11015 if (output_type == TCC_OUTPUT_PREPROCESS) {
11016 if (tcc_add_file_internal(s, filename,
11017 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11018 ret = 1;
11019 } else if (filename[0] == '-' && filename[1]) {
11020 if (tcc_add_library(s, filename + 2) < 0)
11021 error("cannot find %s", filename);
11022 } else {
11023 if (tcc_add_file(s, filename) < 0)
11024 ret = 1;
11028 /* free all files */
11029 tcc_free(files);
11031 if (ret)
11032 goto the_end;
11034 if (do_bench) {
11035 double total_time;
11036 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11037 if (total_time < 0.001)
11038 total_time = 0.001;
11039 if (total_bytes < 1)
11040 total_bytes = 1;
11041 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11042 tok_ident - TOK_IDENT, total_lines, total_bytes,
11043 total_time, (int)(total_lines / total_time),
11044 total_bytes / total_time / 1000000.0);
11047 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11048 if (outfile)
11049 fclose(s->outfile);
11050 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11051 ret = tcc_run(s, argc - optind, argv + optind);
11052 } else
11053 #ifdef TCC_TARGET_PE
11054 if (s->output_type != TCC_OUTPUT_OBJ) {
11055 ret = pe_output_file(s, outfile);
11056 } else
11057 #endif
11059 ret = tcc_output_file(s, outfile) ? 1 : 0;
11061 the_end:
11062 /* XXX: cannot do it with bound checking because of the malloc hooks */
11063 if (!do_bounds_check)
11064 tcc_delete(s);
11066 #ifdef MEM_DEBUG
11067 if (do_bench) {
11068 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11070 #endif
11071 return ret;
11074 #endif