update
[tinycc.git] / tcc.c
blobcda15727c62952e3a54400b5e50bb32240b9f9e1
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001, 2002, 2003 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 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <setjmp.h>
34 #include <time.h>
35 #ifdef WIN32
36 #include <sys/timeb.h>
37 #endif
38 #ifndef WIN32
39 #include <sys/time.h>
40 #include <sys/ucontext.h>
41 #endif
42 #include "elf.h"
43 #include "stab.h"
44 #ifndef CONFIG_TCC_STATIC
45 #include <dlfcn.h>
46 #endif
48 #include "libtcc.h"
50 /* parser debug */
51 //#define PARSE_DEBUG
52 /* preprocessor debug */
53 //#define PP_DEBUG
54 /* include file debug */
55 //#define INC_DEBUG
57 //#define MEM_DEBUG
59 /* assembler debug */
60 //#define ASM_DEBUG
62 /* target selection */
63 //#define TCC_TARGET_I386 /* i386 code generator */
65 /* default target is I386 */
66 #if !defined(TCC_TARGET_I386)
67 #define TCC_TARGET_I386
68 #endif
70 #if !defined(WIN32) && !defined(TCC_UCLIBC)
71 #define CONFIG_TCC_BCHECK /* enable bound checking code */
72 #endif
74 /* define it to include assembler support */
75 #define CONFIG_TCC_ASM
77 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
78 executables or dlls */
79 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
81 #define INCLUDE_STACK_SIZE 32
82 #define IFDEF_STACK_SIZE 64
83 #define VSTACK_SIZE 64
84 #define STRING_MAX_SIZE 1024
86 #define TOK_HASH_SIZE 2048 /* must be a power of two */
87 #define TOK_ALLOC_INCR 512 /* must be a power of two */
88 #define TOK_STR_ALLOC_INCR_BITS 6
89 #define TOK_STR_ALLOC_INCR (1 << TOK_STR_ALLOC_INCR_BITS)
90 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
92 /* token symbol management */
93 typedef struct TokenSym {
94 struct TokenSym *hash_next;
95 struct Sym *sym_define; /* direct pointer to define */
96 struct Sym *sym_label; /* direct pointer to label */
97 struct Sym *sym_struct; /* direct pointer to structure */
98 struct Sym *sym_identifier; /* direct pointer to identifier */
99 int tok; /* token number */
100 int len;
101 char str[1];
102 } TokenSym;
104 typedef struct CString {
105 int size; /* size in bytes */
106 void *data; /* either 'char *' or 'int *' */
107 int size_allocated;
108 void *data_allocated; /* if non NULL, data has been malloced */
109 } CString;
111 /* type definition */
112 typedef struct CType {
113 int t;
114 struct Sym *ref;
115 } CType;
117 /* constant value */
118 typedef union CValue {
119 long double ld;
120 double d;
121 float f;
122 int i;
123 unsigned int ui;
124 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
125 long long ll;
126 unsigned long long ull;
127 struct CString *cstr;
128 void *ptr;
129 int tab[1];
130 } CValue;
132 /* value on stack */
133 typedef struct SValue {
134 CType type; /* type */
135 unsigned short r; /* register + flags */
136 unsigned short r2; /* second register, used for 'long long'
137 type. If not used, set to VT_CONST */
138 CValue c; /* constant, if VT_CONST */
139 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
140 } SValue;
142 /* symbol management */
143 typedef struct Sym {
144 int v; /* symbol token */
145 int r; /* associated register */
146 int c; /* associated number */
147 CType type; /* associated type */
148 struct Sym *next; /* next related symbol */
149 struct Sym *prev; /* prev symbol in stack */
150 struct Sym *prev_tok; /* previous symbol for this token */
151 } Sym;
153 /* section definition */
154 /* XXX: use directly ELF structure for parameters ? */
155 /* special flag to indicate that the section should not be linked to
156 the other ones */
157 #define SHF_PRIVATE 0x80000000
159 typedef struct Section {
160 unsigned long data_offset; /* current data offset */
161 unsigned char *data; /* section data */
162 unsigned long data_allocated; /* used for realloc() handling */
163 int sh_name; /* elf section name (only used during output) */
164 int sh_num; /* elf section number */
165 int sh_type; /* elf section type */
166 int sh_flags; /* elf section flags */
167 int sh_info; /* elf section info */
168 int sh_addralign; /* elf section alignment */
169 int sh_entsize; /* elf entry size */
170 unsigned long sh_size; /* section size (only used during output) */
171 unsigned long sh_addr; /* address at which the section is relocated */
172 unsigned long sh_offset; /* address at which the section is relocated */
173 int nb_hashed_syms; /* used to resize the hash table */
174 struct Section *link; /* link to another section */
175 struct Section *reloc; /* corresponding section for relocation, if any */
176 struct Section *hash; /* hash table for symbols */
177 struct Section *next;
178 char name[1]; /* section name */
179 } Section;
181 typedef struct DLLReference {
182 int level;
183 char name[1];
184 } DLLReference;
186 /* GNUC attribute definition */
187 typedef struct AttributeDef {
188 int aligned;
189 Section *section;
190 unsigned char func_call; /* FUNC_CDECL or FUNC_STDCALL */
191 } AttributeDef;
193 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
194 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
195 #define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
197 /* stored in 'Sym.c' field */
198 #define FUNC_NEW 1 /* ansi function prototype */
199 #define FUNC_OLD 2 /* old function prototype */
200 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
202 /* stored in 'Sym.r' field */
203 #define FUNC_CDECL 0 /* standard c call */
204 #define FUNC_STDCALL 1 /* pascal c call */
206 /* field 'Sym.t' for macros */
207 #define MACRO_OBJ 0 /* object like macro */
208 #define MACRO_FUNC 1 /* function like macro */
210 /* field 'Sym.r' for C labels */
211 #define LABEL_DEFINED 0 /* label is defined */
212 #define LABEL_FORWARD 1 /* label is forward defined */
213 #define LABEL_DECLARED 2 /* label is declared but never used */
215 /* type_decl() types */
216 #define TYPE_ABSTRACT 1 /* type without variable */
217 #define TYPE_DIRECT 2 /* type with variable */
219 #define IO_BUF_SIZE 8192
221 typedef struct BufferedFile {
222 uint8_t *buf_ptr;
223 uint8_t *buf_end;
224 int fd;
225 int line_num; /* current line number - here to simplify code */
226 int ifndef_macro; /* #ifndef macro / #endif search */
227 int ifndef_macro_saved; /* saved ifndef_macro */
228 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
229 char inc_type; /* type of include */
230 char inc_filename[512]; /* filename specified by the user */
231 char filename[1024]; /* current filename - here to simplify code */
232 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
233 } BufferedFile;
235 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
236 #define CH_EOF (-1) /* end of file */
238 /* parsing state (used to save parser state to reparse part of the
239 source several times) */
240 typedef struct ParseState {
241 int *macro_ptr;
242 int line_num;
243 int tok;
244 CValue tokc;
245 } ParseState;
247 /* used to record tokens */
248 typedef struct TokenString {
249 int *str;
250 int len;
251 int allocated_len;
252 int last_line_num;
253 } TokenString;
255 /* include file cache, used to find files faster and also to eliminate
256 inclusion if the include file is protected by #ifndef ... #endif */
257 typedef struct CachedInclude {
258 int ifndef_macro;
259 char type; /* '"' or '>' to give include type */
260 char filename[1]; /* path specified in #include */
261 } CachedInclude;
263 /* parser */
264 static struct BufferedFile *file;
265 static int ch, tok;
266 static CValue tokc;
267 static CString tokcstr; /* current parsed string, if any */
268 /* additional informations about token */
269 static int tok_flags;
270 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
271 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
272 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
274 static int *macro_ptr, *macro_ptr_allocated;
275 static int *unget_saved_macro_ptr;
276 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
277 static int unget_buffer_enabled;
278 static int parse_flags;
279 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
280 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
281 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
282 token. line feed is also
283 returned at eof */
285 static Section *text_section, *data_section, *bss_section; /* predefined sections */
286 static Section *cur_text_section; /* current section where function code is
287 generated */
288 /* bound check related sections */
289 static Section *bounds_section; /* contains global data bound description */
290 static Section *lbounds_section; /* contains local data bound description */
291 /* symbol sections */
292 static Section *symtab_section, *strtab_section;
294 /* debug sections */
295 static Section *stab_section, *stabstr_section;
297 /* loc : local variable index
298 ind : output code index
299 rsym: return symbol
300 anon_sym: anonymous symbol index
302 static int rsym, anon_sym, ind, loc;
303 /* expression generation modifiers */
304 static int const_wanted; /* true if constant wanted */
305 static int nocode_wanted; /* true if no code generation wanted for an expression */
306 static int global_expr; /* true if compound literals must be allocated
307 globally (used during initializers parsing */
308 static CType func_vt; /* current function return type (used by return
309 instruction) */
310 static int func_vc;
311 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
312 static int tok_ident;
313 static TokenSym **table_ident;
314 static TokenSym *hash_ident[TOK_HASH_SIZE];
315 static char token_buf[STRING_MAX_SIZE + 1];
316 static char *funcname;
317 static Sym *global_stack, *local_stack;
318 static Sym *define_stack;
319 static Sym *global_label_stack, *local_label_stack;
321 static SValue vstack[VSTACK_SIZE], *vtop;
322 /* some predefined types */
323 static CType char_pointer_type, func_old_type, int_type;
324 /* true if isid(c) || isnum(c) */
325 static unsigned char isidnum_table[256];
327 /* compile with debug symbol (and use them if error during execution) */
328 static int do_debug = 0;
330 /* compile with built-in memory and bounds checker */
331 static int do_bounds_check = 0;
333 /* display benchmark infos */
334 #if !defined(LIBTCC)
335 static int do_bench = 0;
336 #endif
337 static int total_lines;
338 static int total_bytes;
340 /* use GNU C extensions */
341 static int gnu_ext = 1;
343 /* use Tiny C extensions */
344 static int tcc_ext = 1;
346 /* max number of callers shown if error */
347 static int num_callers = 6;
348 static const char **rt_bound_error_msg;
350 /* XXX: get rid of this ASAP */
351 static struct TCCState *tcc_state;
353 /* give the path of the tcc libraries */
354 static const char *tcc_lib_path = CONFIG_TCC_LIBDIR "/tcc";
356 struct TCCState {
357 int output_type;
359 BufferedFile **include_stack_ptr;
360 int *ifdef_stack_ptr;
362 /* include file handling */
363 char **include_paths;
364 int nb_include_paths;
365 char **sysinclude_paths;
366 int nb_sysinclude_paths;
367 CachedInclude **cached_includes;
368 int nb_cached_includes;
370 char **library_paths;
371 int nb_library_paths;
373 /* array of all loaded dlls (including those referenced by loaded
374 dlls) */
375 DLLReference **loaded_dlls;
376 int nb_loaded_dlls;
378 /* sections */
379 Section **sections;
380 int nb_sections; /* number of sections, including first dummy section */
382 /* got handling */
383 Section *got;
384 Section *plt;
385 unsigned long *got_offsets;
386 int nb_got_offsets;
387 /* give the correspondance from symtab indexes to dynsym indexes */
388 int *symtab_to_dynsym;
390 /* temporary dynamic symbol sections (for dll loading) */
391 Section *dynsymtab_section;
392 /* exported dynamic symbol section */
393 Section *dynsym;
395 int nostdinc; /* if true, no standard headers are added */
396 int nostdlib; /* if true, no standard libraries are added */
398 /* if true, static linking is performed */
399 int static_link;
401 /* if true, all symbols are exported */
402 int rdynamic;
404 /* if true, only link in referenced objects from archive */
405 int alacarte_link;
407 /* warning switches */
408 int warn_write_strings;
409 int warn_unsupported;
410 int warn_error;
411 int warn_none;
413 /* error handling */
414 void *error_opaque;
415 void (*error_func)(void *opaque, const char *msg);
416 int error_set_jmp_enabled;
417 jmp_buf error_jmp_buf;
418 int nb_errors;
420 /* tiny assembler state */
421 Sym *asm_labels;
423 /* see include_stack_ptr */
424 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
426 /* see ifdef_stack_ptr */
427 int ifdef_stack[IFDEF_STACK_SIZE];
430 /* The current value can be: */
431 #define VT_VALMASK 0x00ff
432 #define VT_CONST 0x00f0 /* constant in vc
433 (must be first non register value) */
434 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
435 #define VT_LOCAL 0x00f2 /* offset on stack */
436 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
437 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
438 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
439 #define VT_LVAL 0x0100 /* var is an lvalue */
440 #define VT_SYM 0x0200 /* a symbol value is added */
441 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
442 char/short stored in integer registers) */
443 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
444 dereferencing value */
445 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
446 bounding function call point is in vc */
447 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
448 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
449 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
450 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
452 /* types */
453 #define VT_INT 0 /* integer type */
454 #define VT_BYTE 1 /* signed byte type */
455 #define VT_SHORT 2 /* short type */
456 #define VT_VOID 3 /* void type */
457 #define VT_PTR 4 /* pointer */
458 #define VT_ENUM 5 /* enum definition */
459 #define VT_FUNC 6 /* function type */
460 #define VT_STRUCT 7 /* struct/union definition */
461 #define VT_FLOAT 8 /* IEEE float */
462 #define VT_DOUBLE 9 /* IEEE double */
463 #define VT_LDOUBLE 10 /* IEEE long double */
464 #define VT_BOOL 11 /* ISOC99 boolean type */
465 #define VT_LLONG 12 /* 64 bit integer */
466 #define VT_LONG 13 /* long integer (NEVER USED as type, only
467 during parsing) */
468 #define VT_BTYPE 0x000f /* mask for basic type */
469 #define VT_UNSIGNED 0x0010 /* unsigned type */
470 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
471 #define VT_BITFIELD 0x0040 /* bitfield modifier */
472 #define VT_CONSTANT 0x0800 /* const modifier */
473 #define VT_VOLATILE 0x1000 /* volatile modifier */
475 /* storage */
476 #define VT_EXTERN 0x00000080 /* extern definition */
477 #define VT_STATIC 0x00000100 /* static variable */
478 #define VT_TYPEDEF 0x00000200 /* typedef definition */
479 #define VT_INLINE 0x00000400 /* inline definition */
481 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
483 /* type mask (except storage) */
484 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
485 #define VT_TYPE (~(VT_STORAGE))
487 /* token values */
489 /* warning: the following compare tokens depend on i386 asm code */
490 #define TOK_ULT 0x92
491 #define TOK_UGE 0x93
492 #define TOK_EQ 0x94
493 #define TOK_NE 0x95
494 #define TOK_ULE 0x96
495 #define TOK_UGT 0x97
496 #define TOK_LT 0x9c
497 #define TOK_GE 0x9d
498 #define TOK_LE 0x9e
499 #define TOK_GT 0x9f
501 #define TOK_LAND 0xa0
502 #define TOK_LOR 0xa1
504 #define TOK_DEC 0xa2
505 #define TOK_MID 0xa3 /* inc/dec, to void constant */
506 #define TOK_INC 0xa4
507 #define TOK_UDIV 0xb0 /* unsigned division */
508 #define TOK_UMOD 0xb1 /* unsigned modulo */
509 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
510 #define TOK_CINT 0xb3 /* number in tokc */
511 #define TOK_CCHAR 0xb4 /* char constant in tokc */
512 #define TOK_STR 0xb5 /* pointer to string in tokc */
513 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
514 #define TOK_LCHAR 0xb7
515 #define TOK_LSTR 0xb8
516 #define TOK_CFLOAT 0xb9 /* float constant */
517 #define TOK_LINENUM 0xba /* line number info */
518 #define TOK_CDOUBLE 0xc0 /* double constant */
519 #define TOK_CLDOUBLE 0xc1 /* long double constant */
520 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
521 #define TOK_ADDC1 0xc3 /* add with carry generation */
522 #define TOK_ADDC2 0xc4 /* add with carry use */
523 #define TOK_SUBC1 0xc5 /* add with carry generation */
524 #define TOK_SUBC2 0xc6 /* add with carry use */
525 #define TOK_CUINT 0xc8 /* unsigned int constant */
526 #define TOK_CLLONG 0xc9 /* long long constant */
527 #define TOK_CULLONG 0xca /* unsigned long long constant */
528 #define TOK_ARROW 0xcb
529 #define TOK_DOTS 0xcc /* three dots */
530 #define TOK_SHR 0xcd /* unsigned shift right */
531 #define TOK_PPNUM 0xce /* preprocessor number */
533 #define TOK_SHL 0x01 /* shift left */
534 #define TOK_SAR 0x02 /* signed shift right */
536 /* assignement operators : normal operator or 0x80 */
537 #define TOK_A_MOD 0xa5
538 #define TOK_A_AND 0xa6
539 #define TOK_A_MUL 0xaa
540 #define TOK_A_ADD 0xab
541 #define TOK_A_SUB 0xad
542 #define TOK_A_DIV 0xaf
543 #define TOK_A_XOR 0xde
544 #define TOK_A_OR 0xfc
545 #define TOK_A_SHL 0x81
546 #define TOK_A_SAR 0x82
548 #ifndef offsetof
549 #define offsetof(type, field) ((size_t) &((type *)0)->field)
550 #endif
552 #ifndef countof
553 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
554 #endif
556 /* WARNING: the content of this string encodes token numbers */
557 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";
559 #define TOK_EOF (-1) /* end of file */
560 #define TOK_LINEFEED 10 /* line feed */
562 /* all identificators and strings have token above that */
563 #define TOK_IDENT 256
565 /* only used for i386 asm opcodes definitions */
566 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
568 #define DEF_BWL(x) \
569 DEF(TOK_ASM_ ## x ## b, #x "b") \
570 DEF(TOK_ASM_ ## x ## w, #x "w") \
571 DEF(TOK_ASM_ ## x ## l, #x "l") \
572 DEF(TOK_ASM_ ## x, #x)
574 #define DEF_WL(x) \
575 DEF(TOK_ASM_ ## x ## w, #x "w") \
576 DEF(TOK_ASM_ ## x ## l, #x "l") \
577 DEF(TOK_ASM_ ## x, #x)
579 #define DEF_FP1(x) \
580 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
581 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
582 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
583 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
585 #define DEF_FP(x) \
586 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
587 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
588 DEF_FP1(x)
590 #define DEF_ASMTEST(x) \
591 DEF_ASM(x ## o) \
592 DEF_ASM(x ## no) \
593 DEF_ASM(x ## b) \
594 DEF_ASM(x ## c) \
595 DEF_ASM(x ## nae) \
596 DEF_ASM(x ## nb) \
597 DEF_ASM(x ## nc) \
598 DEF_ASM(x ## ae) \
599 DEF_ASM(x ## e) \
600 DEF_ASM(x ## z) \
601 DEF_ASM(x ## ne) \
602 DEF_ASM(x ## nz) \
603 DEF_ASM(x ## be) \
604 DEF_ASM(x ## na) \
605 DEF_ASM(x ## nbe) \
606 DEF_ASM(x ## a) \
607 DEF_ASM(x ## s) \
608 DEF_ASM(x ## ns) \
609 DEF_ASM(x ## p) \
610 DEF_ASM(x ## pe) \
611 DEF_ASM(x ## np) \
612 DEF_ASM(x ## po) \
613 DEF_ASM(x ## l) \
614 DEF_ASM(x ## nge) \
615 DEF_ASM(x ## nl) \
616 DEF_ASM(x ## ge) \
617 DEF_ASM(x ## le) \
618 DEF_ASM(x ## ng) \
619 DEF_ASM(x ## nle) \
620 DEF_ASM(x ## g)
622 #define TOK_ASM_int TOK_INT
624 enum {
625 TOK_LAST = TOK_IDENT - 1,
626 #define DEF(id, str) id,
627 #include "tcctok.h"
628 #undef DEF
631 static const char tcc_keywords[] =
632 #define DEF(id, str) str "\0"
633 #include "tcctok.h"
634 #undef DEF
637 #define TOK_UIDENT TOK_DEFINE
639 #ifdef WIN32
640 #define snprintf _snprintf
641 #define vsnprintf _vsnprintf
642 #endif
644 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
645 /* currently incorrect */
646 long double strtold(const char *nptr, char **endptr)
648 return (long double)strtod(nptr, endptr);
650 float strtof(const char *nptr, char **endptr)
652 return (float)strtod(nptr, endptr);
654 #else
655 /* XXX: need to define this to use them in non ISOC99 context */
656 extern float strtof (const char *__nptr, char **__endptr);
657 extern long double strtold (const char *__nptr, char **__endptr);
658 #endif
660 static char *pstrcpy(char *buf, int buf_size, const char *s);
661 static char *pstrcat(char *buf, int buf_size, const char *s);
663 static void next(void);
664 static void next_nomacro(void);
665 static void parse_expr_type(CType *type);
666 static void expr_type(CType *type);
667 static void unary_type(CType *type);
668 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
669 int case_reg, int is_expr);
670 static int expr_const(void);
671 static void expr_eq(void);
672 static void gexpr(void);
673 static void decl(int l);
674 static void decl_initializer(CType *type, Section *sec, unsigned long c,
675 int first, int size_only);
676 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
677 int has_init, int v, int scope);
678 int gv(int rc);
679 void gv2(int rc1, int rc2);
680 void move_reg(int r, int s);
681 void save_regs(int n);
682 void save_reg(int r);
683 void vpop(void);
684 void vswap(void);
685 void vdup(void);
686 int get_reg(int rc);
688 static void macro_subst(TokenString *tok_str, Sym **nested_list,
689 const int *macro_str, int can_read_stream);
690 int save_reg_forced(int r);
691 void gen_op(int op);
692 void force_charshort_cast(int t);
693 static void gen_cast(CType *type);
694 void vstore(void);
695 static Sym *sym_find(int v);
696 static Sym *sym_push(int v, CType *type, int r, int c);
698 /* type handling */
699 static int type_size(CType *type, int *a);
700 static inline CType *pointed_type(CType *type);
701 static int pointed_size(CType *type);
702 static int lvalue_type(int t);
703 static int parse_btype(CType *type, AttributeDef *ad);
704 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
705 static int is_compatible_types(CType *type1, CType *type2);
707 void error(const char *fmt, ...);
708 void vpushi(int v);
709 void vset(CType *type, int r, int v);
710 void type_to_str(char *buf, int buf_size,
711 CType *type, const char *varstr);
712 char *get_tok_str(int v, CValue *cv);
713 static Sym *get_sym_ref(CType *type, Section *sec,
714 unsigned long offset, unsigned long size);
715 static Sym *external_global_sym(int v, CType *type, int r);
717 /* section generation */
718 static void section_realloc(Section *sec, unsigned long new_size);
719 static void *section_ptr_add(Section *sec, unsigned long size);
720 static void put_extern_sym(Sym *sym, Section *section,
721 unsigned long value, unsigned long size);
722 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
723 static int put_elf_str(Section *s, const char *sym);
724 static int put_elf_sym(Section *s,
725 unsigned long value, unsigned long size,
726 int info, int other, int shndx, const char *name);
727 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
728 int info, int sh_num, const char *name);
729 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
730 int type, int symbol);
731 static void put_stabs(const char *str, int type, int other, int desc,
732 unsigned long value);
733 static void put_stabs_r(const char *str, int type, int other, int desc,
734 unsigned long value, Section *sec, int sym_index);
735 static void put_stabn(int type, int other, int desc, int value);
736 static void put_stabd(int type, int other, int desc);
737 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
739 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
740 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
741 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
743 /* tccasm.c */
745 #ifdef CONFIG_TCC_ASM
747 typedef struct ExprValue {
748 uint32_t v;
749 Sym *sym;
750 } ExprValue;
752 #define MAX_ASM_OPERANDS 30
754 typedef struct ASMOperand {
755 int id; /* GCC 3 optionnal identifier (0 if number only supported */
756 char *constraint;
757 char asm_str[16]; /* computed asm string for operand */
758 SValue *vt; /* C value of the expression */
759 int ref_index; /* if >= 0, gives reference to a output constraint */
760 int priority; /* priority, used to assign registers */
761 int reg; /* if >= 0, register number used for this operand */
762 int is_llong; /* true if double register value */
763 } ASMOperand;
765 static void asm_expr(TCCState *s1, ExprValue *pe);
766 static int asm_int_expr(TCCState *s1);
767 static int find_constraint(ASMOperand *operands, int nb_operands,
768 const char *name, const char **pp);
770 static int tcc_assemble(TCCState *s1, int do_preprocess);
772 #endif
774 static void asm_instr(void);
776 /* true if float/double/long double type */
777 static inline int is_float(int t)
779 int bt;
780 bt = t & VT_BTYPE;
781 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
784 #ifdef TCC_TARGET_I386
785 #include "i386-gen.c"
786 #endif
788 #ifdef CONFIG_TCC_STATIC
790 #define RTLD_LAZY 0x001
791 #define RTLD_NOW 0x002
792 #define RTLD_GLOBAL 0x100
793 #define RTLD_DEFAULT NULL
795 /* dummy function for profiling */
796 void *dlopen(const char *filename, int flag)
798 return NULL;
801 const char *dlerror(void)
803 return "error";
806 typedef struct TCCSyms {
807 char *str;
808 void *ptr;
809 } TCCSyms;
811 #define TCCSYM(a) { #a, &a, },
813 /* add the symbol you want here if no dynamic linking is done */
814 static TCCSyms tcc_syms[] = {
815 TCCSYM(printf)
816 TCCSYM(fprintf)
817 TCCSYM(fopen)
818 TCCSYM(fclose)
819 { NULL, NULL },
822 void *dlsym(void *handle, const char *symbol)
824 TCCSyms *p;
825 p = tcc_syms;
826 while (p->str != NULL) {
827 if (!strcmp(p->str, symbol))
828 return p->ptr;
829 p++;
831 return NULL;
834 #endif
836 /********************************************************/
838 /* we use our own 'finite' function to avoid potential problems with
839 non standard math libs */
840 /* XXX: endianness dependent */
841 int ieee_finite(double d)
843 int *p = (int *)&d;
844 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
847 /* copy a string and truncate it. */
848 static char *pstrcpy(char *buf, int buf_size, const char *s)
850 char *q, *q_end;
851 int c;
853 if (buf_size > 0) {
854 q = buf;
855 q_end = buf + buf_size - 1;
856 while (q < q_end) {
857 c = *s++;
858 if (c == '\0')
859 break;
860 *q++ = c;
862 *q = '\0';
864 return buf;
867 /* strcat and truncate. */
868 static char *pstrcat(char *buf, int buf_size, const char *s)
870 int len;
871 len = strlen(buf);
872 if (len < buf_size)
873 pstrcpy(buf + len, buf_size - len, s);
874 return buf;
877 /* memory management */
878 #ifdef MEM_DEBUG
879 int mem_cur_size;
880 int mem_max_size;
881 #endif
883 static inline void tcc_free(void *ptr)
885 #ifdef MEM_DEBUG
886 mem_cur_size -= malloc_usable_size(ptr);
887 #endif
888 free(ptr);
891 static void *tcc_malloc(unsigned long size)
893 void *ptr;
894 ptr = malloc(size);
895 if (!ptr && size)
896 error("memory full");
897 #ifdef MEM_DEBUG
898 mem_cur_size += malloc_usable_size(ptr);
899 if (mem_cur_size > mem_max_size)
900 mem_max_size = mem_cur_size;
901 #endif
902 return ptr;
905 static void *tcc_mallocz(unsigned long size)
907 void *ptr;
908 ptr = tcc_malloc(size);
909 memset(ptr, 0, size);
910 return ptr;
913 static inline void *tcc_realloc(void *ptr, unsigned long size)
915 void *ptr1;
916 #ifdef MEM_DEBUG
917 mem_cur_size -= malloc_usable_size(ptr);
918 #endif
919 ptr1 = realloc(ptr, size);
920 #ifdef MEM_DEBUG
921 /* NOTE: count not correct if alloc error, but not critical */
922 mem_cur_size += malloc_usable_size(ptr1);
923 if (mem_cur_size > mem_max_size)
924 mem_max_size = mem_cur_size;
925 #endif
926 return ptr1;
929 static char *tcc_strdup(const char *str)
931 char *ptr;
932 ptr = tcc_malloc(strlen(str) + 1);
933 strcpy(ptr, str);
934 return ptr;
937 #define free(p) use_tcc_free(p)
938 #define malloc(s) use_tcc_malloc(s)
939 #define realloc(p, s) use_tcc_realloc(p, s)
941 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
943 int nb, nb_alloc;
944 void **pp;
946 nb = *nb_ptr;
947 pp = *ptab;
948 /* every power of two we double array size */
949 if ((nb & (nb - 1)) == 0) {
950 if (!nb)
951 nb_alloc = 1;
952 else
953 nb_alloc = nb * 2;
954 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
955 if (!pp)
956 error("memory full");
957 *ptab = pp;
959 pp[nb++] = data;
960 *nb_ptr = nb;
963 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
965 Section *sec;
967 sec = tcc_mallocz(sizeof(Section) + strlen(name));
968 strcpy(sec->name, name);
969 sec->sh_type = sh_type;
970 sec->sh_flags = sh_flags;
971 switch(sh_type) {
972 case SHT_HASH:
973 case SHT_REL:
974 case SHT_DYNSYM:
975 case SHT_SYMTAB:
976 case SHT_DYNAMIC:
977 sec->sh_addralign = 4;
978 break;
979 case SHT_STRTAB:
980 sec->sh_addralign = 1;
981 break;
982 default:
983 sec->sh_addralign = 32; /* default conservative alignment */
984 break;
987 /* only add section if not private */
988 if (!(sh_flags & SHF_PRIVATE)) {
989 sec->sh_num = s1->nb_sections;
990 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
992 return sec;
995 static void free_section(Section *s)
997 tcc_free(s->data);
998 tcc_free(s);
1001 /* realloc section and set its content to zero */
1002 static void section_realloc(Section *sec, unsigned long new_size)
1004 unsigned long size;
1005 unsigned char *data;
1007 size = sec->data_allocated;
1008 if (size == 0)
1009 size = 1;
1010 while (size < new_size)
1011 size = size * 2;
1012 data = tcc_realloc(sec->data, size);
1013 if (!data)
1014 error("memory full");
1015 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1016 sec->data = data;
1017 sec->data_allocated = size;
1020 /* reserve at least 'size' bytes in section 'sec' from
1021 sec->data_offset. */
1022 static void *section_ptr_add(Section *sec, unsigned long size)
1024 unsigned long offset, offset1;
1026 offset = sec->data_offset;
1027 offset1 = offset + size;
1028 if (offset1 > sec->data_allocated)
1029 section_realloc(sec, offset1);
1030 sec->data_offset = offset1;
1031 return sec->data + offset;
1034 /* return a reference to a section, and create it if it does not
1035 exists */
1036 Section *find_section(TCCState *s1, const char *name)
1038 Section *sec;
1039 int i;
1040 for(i = 1; i < s1->nb_sections; i++) {
1041 sec = s1->sections[i];
1042 if (!strcmp(name, sec->name))
1043 return sec;
1045 /* sections are created as PROGBITS */
1046 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1049 /* update sym->c so that it points to an external symbol in section
1050 'section' with value 'value' */
1051 static void put_extern_sym(Sym *sym, Section *section,
1052 unsigned long value, unsigned long size)
1054 int sym_type, sym_bind, sh_num, info;
1055 Elf32_Sym *esym;
1056 const char *name;
1058 if (section)
1059 sh_num = section->sh_num;
1060 else
1061 sh_num = SHN_UNDEF;
1062 if (!sym->c) {
1063 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1064 sym_type = STT_FUNC;
1065 else
1066 sym_type = STT_OBJECT;
1067 if (sym->type.t & VT_STATIC)
1068 sym_bind = STB_LOCAL;
1069 else
1070 sym_bind = STB_GLOBAL;
1072 name = get_tok_str(sym->v, NULL);
1073 #ifdef CONFIG_TCC_BCHECK
1074 if (do_bounds_check) {
1075 char buf[32];
1077 /* XXX: avoid doing that for statics ? */
1078 /* if bound checking is activated, we change some function
1079 names by adding the "__bound" prefix */
1080 switch(sym->v) {
1081 #if 0
1082 /* XXX: we rely only on malloc hooks */
1083 case TOK_malloc:
1084 case TOK_free:
1085 case TOK_realloc:
1086 case TOK_memalign:
1087 case TOK_calloc:
1088 #endif
1089 case TOK_memcpy:
1090 case TOK_memmove:
1091 case TOK_memset:
1092 case TOK_strlen:
1093 case TOK_strcpy:
1094 strcpy(buf, "__bound_");
1095 strcat(buf, name);
1096 name = buf;
1097 break;
1100 #endif
1101 info = ELF32_ST_INFO(sym_bind, sym_type);
1102 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1103 } else {
1104 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1105 esym->st_value = value;
1106 esym->st_size = size;
1107 esym->st_shndx = sh_num;
1111 /* add a new relocation entry to symbol 'sym' in section 's' */
1112 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1114 if (!sym->c)
1115 put_extern_sym(sym, NULL, 0, 0);
1116 /* now we can add ELF relocation info */
1117 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1120 static inline int isid(int c)
1122 return (c >= 'a' && c <= 'z') ||
1123 (c >= 'A' && c <= 'Z') ||
1124 c == '_';
1127 static inline int isnum(int c)
1129 return c >= '0' && c <= '9';
1132 static inline int isoct(int c)
1134 return c >= '0' && c <= '7';
1137 static inline int toup(int c)
1139 if (c >= 'a' && c <= 'z')
1140 return c - 'a' + 'A';
1141 else
1142 return c;
1145 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1147 int len;
1148 len = strlen(buf);
1149 vsnprintf(buf + len, buf_size - len, fmt, ap);
1152 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1154 va_list ap;
1155 va_start(ap, fmt);
1156 strcat_vprintf(buf, buf_size, fmt, ap);
1157 va_end(ap);
1160 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1162 char buf[2048];
1163 BufferedFile **f;
1165 buf[0] = '\0';
1166 if (file) {
1167 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1168 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1169 (*f)->filename, (*f)->line_num);
1170 if (file->line_num > 0) {
1171 strcat_printf(buf, sizeof(buf),
1172 "%s:%d: ", file->filename, file->line_num);
1173 } else {
1174 strcat_printf(buf, sizeof(buf),
1175 "%s: ", file->filename);
1177 } else {
1178 strcat_printf(buf, sizeof(buf),
1179 "tcc: ");
1181 if (is_warning)
1182 strcat_printf(buf, sizeof(buf), "warning: ");
1183 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1185 if (!s1->error_func) {
1186 /* default case: stderr */
1187 fprintf(stderr, "%s\n", buf);
1188 } else {
1189 s1->error_func(s1->error_opaque, buf);
1191 if (!is_warning || s1->warn_error)
1192 s1->nb_errors++;
1195 #ifdef LIBTCC
1196 void tcc_set_error_func(TCCState *s, void *error_opaque,
1197 void (*error_func)(void *opaque, const char *msg))
1199 s->error_opaque = error_opaque;
1200 s->error_func = error_func;
1202 #endif
1204 /* error without aborting current compilation */
1205 void error_noabort(const char *fmt, ...)
1207 TCCState *s1 = tcc_state;
1208 va_list ap;
1210 va_start(ap, fmt);
1211 error1(s1, 0, fmt, ap);
1212 va_end(ap);
1215 void error(const char *fmt, ...)
1217 TCCState *s1 = tcc_state;
1218 va_list ap;
1220 va_start(ap, fmt);
1221 error1(s1, 0, fmt, ap);
1222 va_end(ap);
1223 /* better than nothing: in some cases, we accept to handle errors */
1224 if (s1->error_set_jmp_enabled) {
1225 longjmp(s1->error_jmp_buf, 1);
1226 } else {
1227 /* XXX: eliminate this someday */
1228 exit(1);
1232 void expect(const char *msg)
1234 error("%s expected", msg);
1237 void warning(const char *fmt, ...)
1239 TCCState *s1 = tcc_state;
1240 va_list ap;
1242 if (s1->warn_none)
1243 return;
1245 va_start(ap, fmt);
1246 error1(s1, 1, fmt, ap);
1247 va_end(ap);
1250 void skip(int c)
1252 if (tok != c)
1253 error("'%c' expected", c);
1254 next();
1257 static void test_lvalue(void)
1259 if (!(vtop->r & VT_LVAL))
1260 expect("lvalue");
1263 /* allocate a new token */
1264 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1266 TokenSym *ts, **ptable;
1267 int i;
1269 if (tok_ident >= SYM_FIRST_ANOM)
1270 error("memory full");
1272 /* expand token table if needed */
1273 i = tok_ident - TOK_IDENT;
1274 if ((i % TOK_ALLOC_INCR) == 0) {
1275 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1276 if (!ptable)
1277 error("memory full");
1278 table_ident = ptable;
1281 ts = tcc_malloc(sizeof(TokenSym) + len);
1282 table_ident[i] = ts;
1283 ts->tok = tok_ident++;
1284 ts->sym_define = NULL;
1285 ts->sym_label = NULL;
1286 ts->sym_struct = NULL;
1287 ts->sym_identifier = NULL;
1288 ts->len = len;
1289 ts->hash_next = NULL;
1290 memcpy(ts->str, str, len);
1291 ts->str[len] = '\0';
1292 *pts = ts;
1293 return ts;
1296 #define TOK_HASH_INIT 1
1297 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1299 /* find a token and add it if not found */
1300 static TokenSym *tok_alloc(const char *str, int len)
1302 TokenSym *ts, **pts;
1303 int i;
1304 unsigned int h;
1306 h = TOK_HASH_INIT;
1307 for(i=0;i<len;i++)
1308 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1309 h &= (TOK_HASH_SIZE - 1);
1311 pts = &hash_ident[h];
1312 for(;;) {
1313 ts = *pts;
1314 if (!ts)
1315 break;
1316 if (ts->len == len && !memcmp(ts->str, str, len))
1317 return ts;
1318 pts = &(ts->hash_next);
1320 return tok_alloc_new(pts, str, len);
1323 /* CString handling */
1325 static void cstr_realloc(CString *cstr, int new_size)
1327 int size;
1328 void *data;
1330 size = cstr->size_allocated;
1331 if (size == 0)
1332 size = 8; /* no need to allocate a too small first string */
1333 while (size < new_size)
1334 size = size * 2;
1335 data = tcc_realloc(cstr->data_allocated, size);
1336 if (!data)
1337 error("memory full");
1338 cstr->data_allocated = data;
1339 cstr->size_allocated = size;
1340 cstr->data = data;
1343 /* add a byte */
1344 static void cstr_ccat(CString *cstr, int ch)
1346 int size;
1347 size = cstr->size + 1;
1348 if (size > cstr->size_allocated)
1349 cstr_realloc(cstr, size);
1350 ((unsigned char *)cstr->data)[size - 1] = ch;
1351 cstr->size = size;
1354 static void cstr_cat(CString *cstr, const char *str)
1356 int c;
1357 for(;;) {
1358 c = *str;
1359 if (c == '\0')
1360 break;
1361 cstr_ccat(cstr, c);
1362 str++;
1366 /* add a wide char */
1367 static void cstr_wccat(CString *cstr, int ch)
1369 int size;
1370 size = cstr->size + sizeof(int);
1371 if (size > cstr->size_allocated)
1372 cstr_realloc(cstr, size);
1373 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1374 cstr->size = size;
1377 static void cstr_new(CString *cstr)
1379 memset(cstr, 0, sizeof(CString));
1382 /* free string and reset it to NULL */
1383 static void cstr_free(CString *cstr)
1385 tcc_free(cstr->data_allocated);
1386 cstr_new(cstr);
1389 #define cstr_reset(cstr) cstr_free(cstr)
1391 static CString *cstr_dup(CString *cstr1)
1393 CString *cstr;
1394 int size;
1396 cstr = tcc_malloc(sizeof(CString));
1397 size = cstr1->size;
1398 cstr->size = size;
1399 cstr->size_allocated = size;
1400 cstr->data_allocated = tcc_malloc(size);
1401 cstr->data = cstr->data_allocated;
1402 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1403 return cstr;
1406 /* XXX: unicode ? */
1407 static void add_char(CString *cstr, int c)
1409 if (c == '\'' || c == '\"' || c == '\\') {
1410 /* XXX: could be more precise if char or string */
1411 cstr_ccat(cstr, '\\');
1413 if (c >= 32 && c <= 126) {
1414 cstr_ccat(cstr, c);
1415 } else {
1416 cstr_ccat(cstr, '\\');
1417 if (c == '\n') {
1418 cstr_ccat(cstr, 'n');
1419 } else {
1420 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1421 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1422 cstr_ccat(cstr, '0' + (c & 7));
1427 /* XXX: buffer overflow */
1428 /* XXX: float tokens */
1429 char *get_tok_str(int v, CValue *cv)
1431 static char buf[STRING_MAX_SIZE + 1];
1432 static CString cstr_buf;
1433 CString *cstr;
1434 unsigned char *q;
1435 char *p;
1436 int i, len;
1438 /* NOTE: to go faster, we give a fixed buffer for small strings */
1439 cstr_reset(&cstr_buf);
1440 cstr_buf.data = buf;
1441 cstr_buf.size_allocated = sizeof(buf);
1442 p = buf;
1444 switch(v) {
1445 case TOK_CINT:
1446 case TOK_CUINT:
1447 /* XXX: not quite exact, but only useful for testing */
1448 sprintf(p, "%u", cv->ui);
1449 break;
1450 case TOK_CLLONG:
1451 case TOK_CULLONG:
1452 /* XXX: not quite exact, but only useful for testing */
1453 sprintf(p, "%Lu", cv->ull);
1454 break;
1455 case TOK_CCHAR:
1456 case TOK_LCHAR:
1457 cstr_ccat(&cstr_buf, '\'');
1458 add_char(&cstr_buf, cv->i);
1459 cstr_ccat(&cstr_buf, '\'');
1460 cstr_ccat(&cstr_buf, '\0');
1461 break;
1462 case TOK_PPNUM:
1463 cstr = cv->cstr;
1464 len = cstr->size - 1;
1465 for(i=0;i<len;i++)
1466 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1467 cstr_ccat(&cstr_buf, '\0');
1468 break;
1469 case TOK_STR:
1470 case TOK_LSTR:
1471 cstr = cv->cstr;
1472 cstr_ccat(&cstr_buf, '\"');
1473 if (v == TOK_STR) {
1474 len = cstr->size - 1;
1475 for(i=0;i<len;i++)
1476 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1477 } else {
1478 len = (cstr->size / sizeof(int)) - 1;
1479 for(i=0;i<len;i++)
1480 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1482 cstr_ccat(&cstr_buf, '\"');
1483 cstr_ccat(&cstr_buf, '\0');
1484 break;
1485 case TOK_LT:
1486 v = '<';
1487 goto addv;
1488 case TOK_GT:
1489 v = '>';
1490 goto addv;
1491 case TOK_A_SHL:
1492 return strcpy(p, "<<=");
1493 case TOK_A_SAR:
1494 return strcpy(p, ">>=");
1495 default:
1496 if (v < TOK_IDENT) {
1497 /* search in two bytes table */
1498 q = tok_two_chars;
1499 while (*q) {
1500 if (q[2] == v) {
1501 *p++ = q[0];
1502 *p++ = q[1];
1503 *p = '\0';
1504 return buf;
1506 q += 3;
1508 addv:
1509 *p++ = v;
1510 *p = '\0';
1511 } else if (v < tok_ident) {
1512 return table_ident[v - TOK_IDENT]->str;
1513 } else if (v >= SYM_FIRST_ANOM) {
1514 /* special name for anonymous symbol */
1515 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1516 } else {
1517 /* should never happen */
1518 return NULL;
1520 break;
1522 return cstr_buf.data;
1525 /* push, without hashing */
1526 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1528 Sym *s;
1529 s = tcc_malloc(sizeof(Sym));
1530 s->v = v;
1531 s->type.t = t;
1532 s->c = c;
1533 s->next = NULL;
1534 /* add in stack */
1535 s->prev = *ps;
1536 *ps = s;
1537 return s;
1540 /* find a symbol and return its associated structure. 's' is the top
1541 of the symbol stack */
1542 static Sym *sym_find2(Sym *s, int v)
1544 while (s) {
1545 if (s->v == v)
1546 return s;
1547 s = s->prev;
1549 return NULL;
1552 /* structure lookup */
1553 static inline Sym *struct_find(int v)
1555 v -= TOK_IDENT;
1556 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1557 return NULL;
1558 return table_ident[v]->sym_struct;
1561 /* find an identifier */
1562 static inline Sym *sym_find(int v)
1564 v -= TOK_IDENT;
1565 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1566 return NULL;
1567 return table_ident[v]->sym_identifier;
1570 /* push a given symbol on the symbol stack */
1571 static Sym *sym_push(int v, CType *type, int r, int c)
1573 Sym *s, **ps;
1574 TokenSym *ts;
1576 if (local_stack)
1577 ps = &local_stack;
1578 else
1579 ps = &global_stack;
1580 s = sym_push2(ps, v, type->t, c);
1581 s->type.ref = type->ref;
1582 s->r = r;
1583 /* don't record fields or anonymous symbols */
1584 /* XXX: simplify */
1585 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1586 /* record symbol in token array */
1587 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1588 if (v & SYM_STRUCT)
1589 ps = &ts->sym_struct;
1590 else
1591 ps = &ts->sym_identifier;
1592 s->prev_tok = *ps;
1593 *ps = s;
1595 return s;
1598 /* push a global identifier */
1599 static Sym *global_identifier_push(int v, int t, int c)
1601 Sym *s, **ps;
1602 s = sym_push2(&global_stack, v, t, c);
1603 /* don't record anonymous symbol */
1604 if (v < SYM_FIRST_ANOM) {
1605 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1606 /* modify the top most local identifier, so that
1607 sym_identifier will point to 's' when popped */
1608 while (*ps != NULL)
1609 ps = &(*ps)->prev_tok;
1610 s->prev_tok = NULL;
1611 *ps = s;
1613 return s;
1616 /* pop symbols until top reaches 'b' */
1617 static void sym_pop(Sym **ptop, Sym *b)
1619 Sym *s, *ss, **ps;
1620 TokenSym *ts;
1621 int v;
1623 s = *ptop;
1624 while(s != b) {
1625 ss = s->prev;
1626 v = s->v;
1627 /* remove symbol in token array */
1628 /* XXX: simplify */
1629 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1630 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1631 if (v & SYM_STRUCT)
1632 ps = &ts->sym_struct;
1633 else
1634 ps = &ts->sym_identifier;
1635 *ps = s->prev_tok;
1637 tcc_free(s);
1638 s = ss;
1640 *ptop = b;
1643 /* I/O layer */
1645 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1647 int fd;
1648 BufferedFile *bf;
1650 fd = open(filename, O_RDONLY);
1651 if (fd < 0)
1652 return NULL;
1653 bf = tcc_malloc(sizeof(BufferedFile));
1654 if (!bf) {
1655 close(fd);
1656 return NULL;
1658 bf->fd = fd;
1659 bf->buf_ptr = bf->buffer;
1660 bf->buf_end = bf->buffer;
1661 bf->buffer[0] = CH_EOB; /* put eob symbol */
1662 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1663 bf->line_num = 1;
1664 bf->ifndef_macro = 0;
1665 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1666 // printf("opening '%s'\n", filename);
1667 return bf;
1670 void tcc_close(BufferedFile *bf)
1672 total_lines += bf->line_num;
1673 close(bf->fd);
1674 tcc_free(bf);
1677 /* fill input buffer and peek next char */
1678 static int tcc_peekc_slow(BufferedFile *bf)
1680 int len;
1681 /* only tries to read if really end of buffer */
1682 if (bf->buf_ptr >= bf->buf_end) {
1683 if (bf->fd != -1) {
1684 #if defined(PARSE_DEBUG)
1685 len = 8;
1686 #else
1687 len = IO_BUF_SIZE;
1688 #endif
1689 len = read(bf->fd, bf->buffer, len);
1690 if (len < 0)
1691 len = 0;
1692 } else {
1693 len = 0;
1695 total_bytes += len;
1696 bf->buf_ptr = bf->buffer;
1697 bf->buf_end = bf->buffer + len;
1698 *bf->buf_end = CH_EOB;
1700 if (bf->buf_ptr < bf->buf_end) {
1701 return bf->buf_ptr[0];
1702 } else {
1703 bf->buf_ptr = bf->buf_end;
1704 return CH_EOF;
1708 /* return the current character, handling end of block if necessary
1709 (but not stray) */
1710 static int handle_eob(void)
1712 return tcc_peekc_slow(file);
1715 /* read next char from current input file and handle end of input buffer */
1716 static inline void inp(void)
1718 ch = *(++(file->buf_ptr));
1719 /* end of buffer/file handling */
1720 if (ch == CH_EOB)
1721 ch = handle_eob();
1724 /* handle '\[\r]\n' */
1725 static void handle_stray(void)
1727 while (ch == '\\') {
1728 inp();
1729 if (ch == '\n') {
1730 file->line_num++;
1731 inp();
1732 } else if (ch == '\r') {
1733 inp();
1734 if (ch != '\n')
1735 goto fail;
1736 file->line_num++;
1737 inp();
1738 } else {
1739 fail:
1740 error("stray '\\' in program");
1745 /* skip the stray and handle the \\n case. Output an error if
1746 incorrect char after the stray */
1747 static int handle_stray1(uint8_t *p)
1749 int c;
1751 if (p >= file->buf_end) {
1752 file->buf_ptr = p;
1753 c = handle_eob();
1754 p = file->buf_ptr;
1755 if (c == '\\')
1756 goto parse_stray;
1757 } else {
1758 parse_stray:
1759 file->buf_ptr = p;
1760 ch = *p;
1761 handle_stray();
1762 p = file->buf_ptr;
1763 c = *p;
1765 return c;
1768 /* handle just the EOB case, but not stray */
1769 #define PEEKC_EOB(c, p)\
1771 p++;\
1772 c = *p;\
1773 if (c == '\\') {\
1774 file->buf_ptr = p;\
1775 c = handle_eob();\
1776 p = file->buf_ptr;\
1780 /* handle the complicated stray case */
1781 #define PEEKC(c, p)\
1783 p++;\
1784 c = *p;\
1785 if (c == '\\') {\
1786 c = handle_stray1(p);\
1787 p = file->buf_ptr;\
1791 /* input with '\[\r]\n' handling. Note that this function cannot
1792 handle other characters after '\', so you cannot call it inside
1793 strings or comments */
1794 static void minp(void)
1796 inp();
1797 if (ch == '\\')
1798 handle_stray();
1802 /* single line C++ comments */
1803 static uint8_t *parse_line_comment(uint8_t *p)
1805 int c;
1807 p++;
1808 for(;;) {
1809 c = *p;
1810 if (c == '\n' || c == CH_EOF) {
1811 break;
1812 } else if (c == '\\') {
1813 PEEKC_EOB(c, p);
1814 if (c == '\n') {
1815 file->line_num++;
1816 PEEKC_EOB(c, p);
1817 } else if (c == '\r') {
1818 PEEKC_EOB(c, p);
1819 if (c == '\n') {
1820 file->line_num++;
1821 PEEKC_EOB(c, p);
1824 } else {
1825 p++;
1828 return p;
1831 /* C comments */
1832 static uint8_t *parse_comment(uint8_t *p)
1834 int c;
1836 p++;
1837 for(;;) {
1838 /* fast skip loop */
1839 for(;;) {
1840 c = *p;
1841 if (c == '\n' || c == '*' || c == '\\')
1842 break;
1843 p++;
1844 c = *p;
1845 if (c == '\n' || c == '*' || c == '\\')
1846 break;
1847 p++;
1849 /* now we can handle all the cases */
1850 if (c == '\n') {
1851 file->line_num++;
1852 p++;
1853 } else if (c == '*') {
1854 p++;
1855 for(;;) {
1856 c = *p;
1857 if (c == '*') {
1858 p++;
1859 } else if (c == '/') {
1860 goto end_of_comment;
1861 } else if (c == '\\') {
1862 file->buf_ptr = p;
1863 c = handle_eob();
1864 p = file->buf_ptr;
1865 if (c == '\\') {
1866 /* skip '\[\r]\n', otherwise just skip the stray */
1867 while (c == '\\') {
1868 PEEKC_EOB(c, p);
1869 if (c == '\n') {
1870 file->line_num++;
1871 PEEKC_EOB(c, p);
1872 } else if (c == '\r') {
1873 PEEKC_EOB(c, p);
1874 if (c == '\n') {
1875 file->line_num++;
1876 PEEKC_EOB(c, p);
1878 } else {
1879 goto after_star;
1883 } else {
1884 break;
1887 after_star: ;
1888 } else {
1889 /* stray, eob or eof */
1890 file->buf_ptr = p;
1891 c = handle_eob();
1892 p = file->buf_ptr;
1893 if (c == CH_EOF) {
1894 error("unexpected end of file in comment");
1895 } else if (c == '\\') {
1896 p++;
1900 end_of_comment:
1901 p++;
1902 return p;
1905 #define cinp minp
1907 /* space exlcuding newline */
1908 static inline int is_space(int ch)
1910 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1913 static inline void skip_spaces(void)
1915 while (is_space(ch))
1916 cinp();
1919 /* parse a string without interpreting escapes */
1920 static uint8_t *parse_pp_string(uint8_t *p,
1921 int sep, CString *str)
1923 int c;
1924 p++;
1925 for(;;) {
1926 c = *p;
1927 if (c == sep) {
1928 break;
1929 } else if (c == '\\') {
1930 file->buf_ptr = p;
1931 c = handle_eob();
1932 p = file->buf_ptr;
1933 if (c == CH_EOF) {
1934 unterminated_string:
1935 /* XXX: indicate line number of start of string */
1936 error("missing terminating %c character", sep);
1937 } else if (c == '\\') {
1938 /* escape : just skip \[\r]\n */
1939 PEEKC_EOB(c, p);
1940 if (c == '\n') {
1941 file->line_num++;
1942 p++;
1943 } else if (c == '\r') {
1944 PEEKC_EOB(c, p);
1945 if (c != '\n')
1946 expect("'\n' after '\r'");
1947 file->line_num++;
1948 p++;
1949 } else if (c == CH_EOF) {
1950 goto unterminated_string;
1951 } else {
1952 if (str) {
1953 cstr_ccat(str, '\\');
1954 cstr_ccat(str, c);
1956 p++;
1959 } else if (c == '\n') {
1960 file->line_num++;
1961 goto add_char;
1962 } else if (c == '\r') {
1963 PEEKC_EOB(c, p);
1964 if (c != '\n') {
1965 cstr_ccat(str, '\r');
1966 } else {
1967 file->line_num++;
1968 goto add_char;
1970 } else {
1971 add_char:
1972 if (str)
1973 cstr_ccat(str, c);
1974 p++;
1977 p++;
1978 return p;
1981 /* skip block of text until #else, #elif or #endif. skip also pairs of
1982 #if/#endif */
1983 void preprocess_skip(void)
1985 int a, start_of_line, c;
1986 uint8_t *p;
1988 p = file->buf_ptr;
1989 start_of_line = 1;
1990 a = 0;
1991 for(;;) {
1992 redo_no_start:
1993 c = *p;
1994 switch(c) {
1995 case ' ':
1996 case '\t':
1997 case '\f':
1998 case '\v':
1999 case '\r':
2000 p++;
2001 goto redo_no_start;
2002 case '\n':
2003 start_of_line = 1;
2004 file->line_num++;
2005 p++;
2006 goto redo_no_start;
2007 case '\\':
2008 file->buf_ptr = p;
2009 c = handle_eob();
2010 if (c == CH_EOF) {
2011 expect("#endif");
2012 } else if (c == '\\') {
2013 /* XXX: incorrect: should not give an error */
2014 ch = file->buf_ptr[0];
2015 handle_stray();
2017 p = file->buf_ptr;
2018 goto redo_no_start;
2019 /* skip strings */
2020 case '\"':
2021 case '\'':
2022 p = parse_pp_string(p, c, NULL);
2023 break;
2024 /* skip comments */
2025 case '/':
2026 file->buf_ptr = p;
2027 ch = *p;
2028 minp();
2029 p = file->buf_ptr;
2030 if (ch == '*') {
2031 p = parse_comment(p);
2032 } else if (ch == '/') {
2033 p = parse_line_comment(p);
2035 break;
2037 case '#':
2038 p++;
2039 if (start_of_line) {
2040 file->buf_ptr = p;
2041 next_nomacro();
2042 p = file->buf_ptr;
2043 if (a == 0 &&
2044 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2045 goto the_end;
2046 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2047 a++;
2048 else if (tok == TOK_ENDIF)
2049 a--;
2051 break;
2052 default:
2053 p++;
2054 break;
2056 start_of_line = 0;
2058 the_end: ;
2059 file->buf_ptr = p;
2062 /* ParseState handling */
2064 /* XXX: currently, no include file info is stored. Thus, we cannot display
2065 accurate messages if the function or data definition spans multiple
2066 files */
2068 /* save current parse state in 's' */
2069 void save_parse_state(ParseState *s)
2071 s->line_num = file->line_num;
2072 s->macro_ptr = macro_ptr;
2073 s->tok = tok;
2074 s->tokc = tokc;
2077 /* restore parse state from 's' */
2078 void restore_parse_state(ParseState *s)
2080 file->line_num = s->line_num;
2081 macro_ptr = s->macro_ptr;
2082 tok = s->tok;
2083 tokc = s->tokc;
2086 /* return the number of additional 'ints' necessary to store the
2087 token */
2088 static inline int tok_ext_size(int t)
2090 switch(t) {
2091 /* 4 bytes */
2092 case TOK_CINT:
2093 case TOK_CUINT:
2094 case TOK_CCHAR:
2095 case TOK_LCHAR:
2096 case TOK_STR:
2097 case TOK_LSTR:
2098 case TOK_CFLOAT:
2099 case TOK_LINENUM:
2100 case TOK_PPNUM:
2101 return 1;
2102 case TOK_CDOUBLE:
2103 case TOK_CLLONG:
2104 case TOK_CULLONG:
2105 return 2;
2106 case TOK_CLDOUBLE:
2107 return LDOUBLE_SIZE / 4;
2108 default:
2109 return 0;
2113 /* token string handling */
2115 static inline void tok_str_new(TokenString *s)
2117 s->str = NULL;
2118 s->len = 0;
2119 s->allocated_len = 0;
2120 s->last_line_num = -1;
2123 static void tok_str_free(int *str)
2125 const int *p;
2126 CString *cstr;
2127 int t;
2129 p = str;
2130 for(;;) {
2131 t = *p;
2132 /* NOTE: we test zero separately so that GCC can generate a
2133 table for the following switch */
2134 if (t == 0)
2135 break;
2136 switch(t) {
2137 case TOK_CINT:
2138 case TOK_CUINT:
2139 case TOK_CCHAR:
2140 case TOK_LCHAR:
2141 case TOK_CFLOAT:
2142 case TOK_LINENUM:
2143 p += 2;
2144 break;
2145 case TOK_PPNUM:
2146 case TOK_STR:
2147 case TOK_LSTR:
2148 /* XXX: use a macro to be portable on 64 bit ? */
2149 cstr = (CString *)p[1];
2150 cstr_free(cstr);
2151 tcc_free(cstr);
2152 p += 2;
2153 break;
2154 case TOK_CDOUBLE:
2155 case TOK_CLLONG:
2156 case TOK_CULLONG:
2157 p += 3;
2158 break;
2159 case TOK_CLDOUBLE:
2160 p += 1 + (LDOUBLE_SIZE / 4);
2161 break;
2162 default:
2163 p++;
2164 break;
2167 tcc_free(str);
2170 static int *tok_str_realloc(TokenString *s)
2172 int *str, len;
2174 len = s->allocated_len + TOK_STR_ALLOC_INCR;
2175 str = tcc_realloc(s->str, len * sizeof(int));
2176 if (!str)
2177 error("memory full");
2178 s->allocated_len = len;
2179 s->str = str;
2180 return str;
2183 static void tok_str_add(TokenString *s, int t)
2185 int len, *str;
2187 len = s->len;
2188 str = s->str;
2189 if (len >= s->allocated_len)
2190 str = tok_str_realloc(s);
2191 str[len++] = t;
2192 s->len = len;
2195 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2197 int len, *str;
2199 len = s->len;
2200 str = s->str;
2202 /* allocate space for worst case */
2203 if (len + TOK_MAX_SIZE > s->allocated_len)
2204 str = tok_str_realloc(s);
2205 str[len++] = t;
2206 switch(t) {
2207 case TOK_CINT:
2208 case TOK_CUINT:
2209 case TOK_CCHAR:
2210 case TOK_LCHAR:
2211 case TOK_CFLOAT:
2212 case TOK_LINENUM:
2213 str[len++] = cv->tab[0];
2214 break;
2215 case TOK_PPNUM:
2216 case TOK_STR:
2217 case TOK_LSTR:
2218 str[len++] = (int)cstr_dup(cv->cstr);
2219 break;
2220 case TOK_CDOUBLE:
2221 case TOK_CLLONG:
2222 case TOK_CULLONG:
2223 str[len++] = cv->tab[0];
2224 str[len++] = cv->tab[1];
2225 break;
2226 case TOK_CLDOUBLE:
2227 #if LDOUBLE_SIZE == 12
2228 str[len++] = cv->tab[0];
2229 str[len++] = cv->tab[1];
2230 str[len++] = cv->tab[2];
2231 #else
2232 #error add long double size support
2233 #endif
2234 break;
2235 default:
2236 break;
2238 s->len = len;
2241 /* add the current parse token in token string 's' */
2242 static void tok_str_add_tok(TokenString *s)
2244 CValue cval;
2246 /* save line number info */
2247 if (file->line_num != s->last_line_num) {
2248 s->last_line_num = file->line_num;
2249 cval.i = s->last_line_num;
2250 tok_str_add2(s, TOK_LINENUM, &cval);
2252 tok_str_add2(s, tok, &tokc);
2255 #if LDOUBLE_SIZE == 12
2256 #define LDOUBLE_GET(p, cv) \
2257 cv.tab[0] = p[0]; \
2258 cv.tab[1] = p[1]; \
2259 cv.tab[2] = p[2];
2260 #else
2261 #error add long double size support
2262 #endif
2265 /* get a token from an integer array and increment pointer
2266 accordingly. we code it as a macro to avoid pointer aliasing. */
2267 #define TOK_GET(t, p, cv) \
2269 t = *p++; \
2270 switch(t) { \
2271 case TOK_CINT: \
2272 case TOK_CUINT: \
2273 case TOK_CCHAR: \
2274 case TOK_LCHAR: \
2275 case TOK_CFLOAT: \
2276 case TOK_LINENUM: \
2277 case TOK_STR: \
2278 case TOK_LSTR: \
2279 case TOK_PPNUM: \
2280 cv.tab[0] = *p++; \
2281 break; \
2282 case TOK_CDOUBLE: \
2283 case TOK_CLLONG: \
2284 case TOK_CULLONG: \
2285 cv.tab[0] = p[0]; \
2286 cv.tab[1] = p[1]; \
2287 p += 2; \
2288 break; \
2289 case TOK_CLDOUBLE: \
2290 LDOUBLE_GET(p, cv); \
2291 p += LDOUBLE_SIZE / 4; \
2292 break; \
2293 default: \
2294 break; \
2298 /* defines handling */
2299 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2301 Sym *s;
2303 s = sym_push2(&define_stack, v, macro_type, (int)str);
2304 s->next = first_arg;
2305 table_ident[v - TOK_IDENT]->sym_define = s;
2308 /* undefined a define symbol. Its name is just set to zero */
2309 static void define_undef(Sym *s)
2311 int v;
2312 v = s->v;
2313 if (v >= TOK_IDENT && v < tok_ident)
2314 table_ident[v - TOK_IDENT]->sym_define = NULL;
2315 s->v = 0;
2318 static inline Sym *define_find(int v)
2320 v -= TOK_IDENT;
2321 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2322 return NULL;
2323 return table_ident[v]->sym_define;
2326 /* free define stack until top reaches 'b' */
2327 static void free_defines(Sym *b)
2329 Sym *top, *top1;
2330 int v;
2332 top = define_stack;
2333 while (top != b) {
2334 top1 = top->prev;
2335 /* do not free args or predefined defines */
2336 if (top->c)
2337 tok_str_free((int *)top->c);
2338 v = top->v;
2339 if (v >= TOK_IDENT && v < tok_ident)
2340 table_ident[v - TOK_IDENT]->sym_define = NULL;
2341 tcc_free(top);
2342 top = top1;
2344 define_stack = b;
2347 /* label lookup */
2348 static Sym *label_find(int v)
2350 v -= TOK_IDENT;
2351 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2352 return NULL;
2353 return table_ident[v]->sym_label;
2356 static Sym *label_push(Sym **ptop, int v, int flags)
2358 Sym *s, **ps;
2359 s = sym_push2(ptop, v, 0, 0);
2360 s->r = flags;
2361 ps = &table_ident[v - TOK_IDENT]->sym_label;
2362 if (ptop == &global_label_stack) {
2363 /* modify the top most local identifier, so that
2364 sym_identifier will point to 's' when popped */
2365 while (*ps != NULL)
2366 ps = &(*ps)->prev_tok;
2368 s->prev_tok = *ps;
2369 *ps = s;
2370 return s;
2373 /* pop labels until element last is reached. Look if any labels are
2374 undefined. Define symbols if '&&label' was used. */
2375 static void label_pop(Sym **ptop, Sym *slast)
2377 Sym *s, *s1;
2378 for(s = *ptop; s != slast; s = s1) {
2379 s1 = s->prev;
2380 if (s->r == LABEL_DECLARED) {
2381 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2382 } else if (s->r == LABEL_FORWARD) {
2383 error("label '%s' used but not defined",
2384 get_tok_str(s->v, NULL));
2385 } else {
2386 if (s->c) {
2387 /* define corresponding symbol. A size of
2388 1 is put. */
2389 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2392 /* remove label */
2393 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2394 tcc_free(s);
2396 *ptop = slast;
2399 /* eval an expression for #if/#elif */
2400 static int expr_preprocess(void)
2402 int c, t;
2403 TokenString str;
2405 tok_str_new(&str);
2406 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2407 next(); /* do macro subst */
2408 if (tok == TOK_DEFINED) {
2409 next_nomacro();
2410 t = tok;
2411 if (t == '(')
2412 next_nomacro();
2413 c = define_find(tok) != 0;
2414 if (t == '(')
2415 next_nomacro();
2416 tok = TOK_CINT;
2417 tokc.i = c;
2418 } else if (tok >= TOK_IDENT) {
2419 /* if undefined macro */
2420 tok = TOK_CINT;
2421 tokc.i = 0;
2423 tok_str_add_tok(&str);
2425 tok_str_add(&str, -1); /* simulate end of file */
2426 tok_str_add(&str, 0);
2427 /* now evaluate C constant expression */
2428 macro_ptr = str.str;
2429 next();
2430 c = expr_const();
2431 macro_ptr = NULL;
2432 tok_str_free(str.str);
2433 return c != 0;
2436 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2437 static void tok_print(int *str)
2439 int t;
2440 CValue cval;
2442 while (1) {
2443 TOK_GET(t, str, cval);
2444 if (!t)
2445 break;
2446 printf(" %s", get_tok_str(t, &cval));
2448 printf("\n");
2450 #endif
2452 /* parse after #define */
2453 static void parse_define(void)
2455 Sym *s, *first, **ps;
2456 int v, t, varg, is_vaargs, c;
2457 TokenString str;
2459 v = tok;
2460 if (v < TOK_IDENT)
2461 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2462 /* XXX: should check if same macro (ANSI) */
2463 first = NULL;
2464 t = MACRO_OBJ;
2465 /* '(' must be just after macro definition for MACRO_FUNC */
2466 c = file->buf_ptr[0];
2467 if (c == '\\')
2468 c = handle_stray1(file->buf_ptr);
2469 if (c == '(') {
2470 next_nomacro();
2471 next_nomacro();
2472 ps = &first;
2473 while (tok != ')') {
2474 varg = tok;
2475 next_nomacro();
2476 is_vaargs = 0;
2477 if (varg == TOK_DOTS) {
2478 varg = TOK___VA_ARGS__;
2479 is_vaargs = 1;
2480 } else if (tok == TOK_DOTS && gnu_ext) {
2481 is_vaargs = 1;
2482 next_nomacro();
2484 if (varg < TOK_IDENT)
2485 error("badly punctuated parameter list");
2486 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2487 *ps = s;
2488 ps = &s->next;
2489 if (tok != ',')
2490 break;
2491 next_nomacro();
2493 t = MACRO_FUNC;
2495 tok_str_new(&str);
2496 next_nomacro();
2497 /* EOF testing necessary for '-D' handling */
2498 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2499 tok_str_add2(&str, tok, &tokc);
2500 next_nomacro();
2502 tok_str_add(&str, 0);
2503 #ifdef PP_DEBUG
2504 printf("define %s %d: ", get_tok_str(v, NULL), t);
2505 tok_print(str.str);
2506 #endif
2507 define_push(v, t, str.str, first);
2510 /* XXX: use a token or a hash table to accelerate matching ? */
2511 static CachedInclude *search_cached_include(TCCState *s1,
2512 int type, const char *filename)
2514 CachedInclude *e;
2515 int i;
2517 for(i = 0;i < s1->nb_cached_includes; i++) {
2518 e = s1->cached_includes[i];
2519 if (e->type == type && !strcmp(e->filename, filename))
2520 return e;
2522 return NULL;
2525 static inline void add_cached_include(TCCState *s1, int type,
2526 const char *filename, int ifndef_macro)
2528 CachedInclude *e;
2530 if (search_cached_include(s1, type, filename))
2531 return;
2532 #ifdef INC_DEBUG
2533 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2534 #endif
2535 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2536 if (!e)
2537 return;
2538 e->type = type;
2539 strcpy(e->filename, filename);
2540 e->ifndef_macro = ifndef_macro;
2541 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2544 /* is_bof is true if first non space token at beginning of file */
2545 static void preprocess(int is_bof)
2547 TCCState *s1 = tcc_state;
2548 int size, i, c, n, saved_parse_flags;
2549 char buf[1024], *q, *p;
2550 char buf1[1024];
2551 BufferedFile *f;
2552 Sym *s;
2553 CachedInclude *e;
2555 saved_parse_flags = parse_flags;
2556 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2557 PARSE_FLAG_LINEFEED;
2558 next_nomacro();
2559 redo:
2560 switch(tok) {
2561 case TOK_DEFINE:
2562 next_nomacro();
2563 parse_define();
2564 break;
2565 case TOK_UNDEF:
2566 next_nomacro();
2567 s = define_find(tok);
2568 /* undefine symbol by putting an invalid name */
2569 if (s)
2570 define_undef(s);
2571 break;
2572 case TOK_INCLUDE:
2573 ch = file->buf_ptr[0];
2574 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2575 skip_spaces();
2576 if (ch == '<') {
2577 c = '>';
2578 goto read_name;
2579 } else if (ch == '\"') {
2580 c = ch;
2581 read_name:
2582 /* XXX: better stray handling */
2583 minp();
2584 q = buf;
2585 while (ch != c && ch != '\n' && ch != CH_EOF) {
2586 if ((q - buf) < sizeof(buf) - 1)
2587 *q++ = ch;
2588 minp();
2590 *q = '\0';
2591 minp();
2592 #if 0
2593 /* eat all spaces and comments after include */
2594 /* XXX: slightly incorrect */
2595 while (ch1 != '\n' && ch1 != CH_EOF)
2596 inp();
2597 #endif
2598 } else {
2599 /* computed #include : either we have only strings or
2600 we have anything enclosed in '<>' */
2601 next();
2602 buf[0] = '\0';
2603 if (tok == TOK_STR) {
2604 while (tok != TOK_LINEFEED) {
2605 if (tok != TOK_STR) {
2606 include_syntax:
2607 error("'#include' expects \"FILENAME\" or <FILENAME>");
2609 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2610 next();
2612 c = '\"';
2613 } else {
2614 int len;
2615 while (tok != TOK_LINEFEED) {
2616 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2617 next();
2619 len = strlen(buf);
2620 /* check syntax and remove '<>' */
2621 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2622 goto include_syntax;
2623 memmove(buf, buf + 1, len - 2);
2624 buf[len - 2] = '\0';
2625 c = '>';
2629 e = search_cached_include(s1, c, buf);
2630 if (e && define_find(e->ifndef_macro)) {
2631 /* no need to parse the include because the 'ifndef macro'
2632 is defined */
2633 #ifdef INC_DEBUG
2634 printf("%s: skipping %s\n", file->filename, buf);
2635 #endif
2636 } else {
2637 if (c == '\"') {
2638 /* first search in current dir if "header.h" */
2639 size = 0;
2640 p = strrchr(file->filename, '/');
2641 if (p)
2642 size = p + 1 - file->filename;
2643 if (size > sizeof(buf1) - 1)
2644 size = sizeof(buf1) - 1;
2645 memcpy(buf1, file->filename, size);
2646 buf1[size] = '\0';
2647 pstrcat(buf1, sizeof(buf1), buf);
2648 f = tcc_open(s1, buf1);
2649 if (f)
2650 goto found;
2652 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2653 error("#include recursion too deep");
2654 /* now search in all the include paths */
2655 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2656 for(i = 0; i < n; i++) {
2657 const char *path;
2658 if (i < s1->nb_include_paths)
2659 path = s1->include_paths[i];
2660 else
2661 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2662 pstrcpy(buf1, sizeof(buf1), path);
2663 pstrcat(buf1, sizeof(buf1), "/");
2664 pstrcat(buf1, sizeof(buf1), buf);
2665 f = tcc_open(s1, buf1);
2666 if (f)
2667 goto found;
2669 error("include file '%s' not found", buf);
2670 f = NULL;
2671 found:
2672 #ifdef INC_DEBUG
2673 printf("%s: including %s\n", file->filename, buf1);
2674 #endif
2675 f->inc_type = c;
2676 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2677 /* push current file in stack */
2678 /* XXX: fix current line init */
2679 *s1->include_stack_ptr++ = file;
2680 file = f;
2681 /* add include file debug info */
2682 if (do_debug) {
2683 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2685 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2686 ch = file->buf_ptr[0];
2687 goto the_end;
2689 break;
2690 case TOK_IFNDEF:
2691 c = 1;
2692 goto do_ifdef;
2693 case TOK_IF:
2694 c = expr_preprocess();
2695 goto do_if;
2696 case TOK_IFDEF:
2697 c = 0;
2698 do_ifdef:
2699 next_nomacro();
2700 if (tok < TOK_IDENT)
2701 error("invalid argument for '#if%sdef'", c ? "n" : "");
2702 if (is_bof) {
2703 if (c) {
2704 #ifdef INC_DEBUG
2705 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2706 #endif
2707 file->ifndef_macro = tok;
2710 c = (define_find(tok) != 0) ^ c;
2711 do_if:
2712 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2713 error("memory full");
2714 *s1->ifdef_stack_ptr++ = c;
2715 goto test_skip;
2716 case TOK_ELSE:
2717 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2718 error("#else without matching #if");
2719 if (s1->ifdef_stack_ptr[-1] & 2)
2720 error("#else after #else");
2721 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2722 goto test_skip;
2723 case TOK_ELIF:
2724 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2725 error("#elif without matching #if");
2726 c = s1->ifdef_stack_ptr[-1];
2727 if (c > 1)
2728 error("#elif after #else");
2729 /* last #if/#elif expression was true: we skip */
2730 if (c == 1)
2731 goto skip;
2732 c = expr_preprocess();
2733 s1->ifdef_stack_ptr[-1] = c;
2734 test_skip:
2735 if (!(c & 1)) {
2736 skip:
2737 preprocess_skip();
2738 is_bof = 0;
2739 goto redo;
2741 break;
2742 case TOK_ENDIF:
2743 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2744 error("#endif without matching #if");
2745 s1->ifdef_stack_ptr--;
2746 /* '#ifndef macro' was at the start of file. Now we check if
2747 an '#endif' is exactly at the end of file */
2748 if (file->ifndef_macro &&
2749 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2750 file->ifndef_macro_saved = file->ifndef_macro;
2751 /* need to set to zero to avoid false matches if another
2752 #ifndef at middle of file */
2753 file->ifndef_macro = 0;
2754 while (tok != TOK_LINEFEED)
2755 next_nomacro();
2756 tok_flags |= TOK_FLAG_ENDIF;
2757 goto the_end;
2759 break;
2760 case TOK_LINE:
2761 next();
2762 if (tok != TOK_CINT)
2763 error("#line");
2764 file->line_num = tokc.i - 1; /* the line number will be incremented after */
2765 next();
2766 if (tok != TOK_LINEFEED) {
2767 if (tok != TOK_STR)
2768 error("#line");
2769 pstrcpy(file->filename, sizeof(file->filename),
2770 (char *)tokc.cstr->data);
2772 break;
2773 case TOK_ERROR:
2774 case TOK_WARNING:
2775 c = tok;
2776 ch = file->buf_ptr[0];
2777 skip_spaces();
2778 q = buf;
2779 while (ch != '\n' && ch != CH_EOF) {
2780 if ((q - buf) < sizeof(buf) - 1)
2781 *q++ = ch;
2782 minp();
2784 *q = '\0';
2785 if (c == TOK_ERROR)
2786 error("#error %s", buf);
2787 else
2788 warning("#warning %s", buf);
2789 break;
2790 case TOK_PRAGMA:
2791 /* ignored */
2792 break;
2793 default:
2794 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
2795 /* '!' is ignored to allow C scripts. numbers are ignored
2796 to emulate cpp behaviour */
2797 } else {
2798 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
2800 break;
2802 /* ignore other preprocess commands or #! for C scripts */
2803 while (tok != TOK_LINEFEED)
2804 next_nomacro();
2805 the_end:
2806 parse_flags = saved_parse_flags;
2809 /* evaluate escape codes in a string. */
2810 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2812 int c, n;
2813 const uint8_t *p;
2815 p = buf;
2816 for(;;) {
2817 c = *p;
2818 if (c == '\0')
2819 break;
2820 if (c == '\\') {
2821 p++;
2822 /* escape */
2823 c = *p;
2824 switch(c) {
2825 case '0': case '1': case '2': case '3':
2826 case '4': case '5': case '6': case '7':
2827 /* at most three octal digits */
2828 n = c - '0';
2829 p++;
2830 c = *p;
2831 if (isoct(c)) {
2832 n = n * 8 + c - '0';
2833 p++;
2834 c = *p;
2835 if (isoct(c)) {
2836 n = n * 8 + c - '0';
2837 p++;
2840 c = n;
2841 goto add_char_nonext;
2842 case 'x':
2843 p++;
2844 n = 0;
2845 for(;;) {
2846 c = *p;
2847 if (c >= 'a' && c <= 'f')
2848 c = c - 'a' + 10;
2849 else if (c >= 'A' && c <= 'F')
2850 c = c - 'A' + 10;
2851 else if (isnum(c))
2852 c = c - '0';
2853 else
2854 break;
2855 n = n * 16 + c;
2856 p++;
2858 c = n;
2859 goto add_char_nonext;
2860 case 'a':
2861 c = '\a';
2862 break;
2863 case 'b':
2864 c = '\b';
2865 break;
2866 case 'f':
2867 c = '\f';
2868 break;
2869 case 'n':
2870 c = '\n';
2871 break;
2872 case 'r':
2873 c = '\r';
2874 break;
2875 case 't':
2876 c = '\t';
2877 break;
2878 case 'v':
2879 c = '\v';
2880 break;
2881 case 'e':
2882 if (!gnu_ext)
2883 goto invalid_escape;
2884 c = 27;
2885 break;
2886 case '\'':
2887 case '\"':
2888 case '\\':
2889 case '?':
2890 break;
2891 default:
2892 invalid_escape:
2893 if (c >= '!' && c <= '~')
2894 warning("unknown escape sequence: \'\\%c\'", c);
2895 else
2896 warning("unknown escape sequence: \'\\x%x\'", c);
2897 break;
2900 p++;
2901 add_char_nonext:
2902 if (!is_long)
2903 cstr_ccat(outstr, c);
2904 else
2905 cstr_wccat(outstr, c);
2907 /* add a trailing '\0' */
2908 if (!is_long)
2909 cstr_ccat(outstr, '\0');
2910 else
2911 cstr_wccat(outstr, '\0');
2914 /* we use 64 bit numbers */
2915 #define BN_SIZE 2
2917 /* bn = (bn << shift) | or_val */
2918 void bn_lshift(unsigned int *bn, int shift, int or_val)
2920 int i;
2921 unsigned int v;
2922 for(i=0;i<BN_SIZE;i++) {
2923 v = bn[i];
2924 bn[i] = (v << shift) | or_val;
2925 or_val = v >> (32 - shift);
2929 void bn_zero(unsigned int *bn)
2931 int i;
2932 for(i=0;i<BN_SIZE;i++) {
2933 bn[i] = 0;
2937 /* parse number in null terminated string 'p' and return it in the
2938 current token */
2939 void parse_number(const char *p)
2941 int b, t, shift, frac_bits, s, exp_val, ch;
2942 char *q;
2943 unsigned int bn[BN_SIZE];
2944 double d;
2946 /* number */
2947 q = token_buf;
2948 ch = *p++;
2949 t = ch;
2950 ch = *p++;
2951 *q++ = t;
2952 b = 10;
2953 if (t == '.') {
2954 goto float_frac_parse;
2955 } else if (t == '0') {
2956 if (ch == 'x' || ch == 'X') {
2957 q--;
2958 ch = *p++;
2959 b = 16;
2960 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2961 q--;
2962 ch = *p++;
2963 b = 2;
2966 /* parse all digits. cannot check octal numbers at this stage
2967 because of floating point constants */
2968 while (1) {
2969 if (ch >= 'a' && ch <= 'f')
2970 t = ch - 'a' + 10;
2971 else if (ch >= 'A' && ch <= 'F')
2972 t = ch - 'A' + 10;
2973 else if (isnum(ch))
2974 t = ch - '0';
2975 else
2976 break;
2977 if (t >= b)
2978 break;
2979 if (q >= token_buf + STRING_MAX_SIZE) {
2980 num_too_long:
2981 error("number too long");
2983 *q++ = ch;
2984 ch = *p++;
2986 if (ch == '.' ||
2987 ((ch == 'e' || ch == 'E') && b == 10) ||
2988 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2989 if (b != 10) {
2990 /* NOTE: strtox should support that for hexa numbers, but
2991 non ISOC99 libcs do not support it, so we prefer to do
2992 it by hand */
2993 /* hexadecimal or binary floats */
2994 /* XXX: handle overflows */
2995 *q = '\0';
2996 if (b == 16)
2997 shift = 4;
2998 else
2999 shift = 2;
3000 bn_zero(bn);
3001 q = token_buf;
3002 while (1) {
3003 t = *q++;
3004 if (t == '\0') {
3005 break;
3006 } else if (t >= 'a') {
3007 t = t - 'a' + 10;
3008 } else if (t >= 'A') {
3009 t = t - 'A' + 10;
3010 } else {
3011 t = t - '0';
3013 bn_lshift(bn, shift, t);
3015 frac_bits = 0;
3016 if (ch == '.') {
3017 ch = *p++;
3018 while (1) {
3019 t = ch;
3020 if (t >= 'a' && t <= 'f') {
3021 t = t - 'a' + 10;
3022 } else if (t >= 'A' && t <= 'F') {
3023 t = t - 'A' + 10;
3024 } else if (t >= '0' && t <= '9') {
3025 t = t - '0';
3026 } else {
3027 break;
3029 if (t >= b)
3030 error("invalid digit");
3031 bn_lshift(bn, shift, t);
3032 frac_bits += shift;
3033 ch = *p++;
3036 if (ch != 'p' && ch != 'P')
3037 expect("exponent");
3038 ch = *p++;
3039 s = 1;
3040 exp_val = 0;
3041 if (ch == '+') {
3042 ch = *p++;
3043 } else if (ch == '-') {
3044 s = -1;
3045 ch = *p++;
3047 if (ch < '0' || ch > '9')
3048 expect("exponent digits");
3049 while (ch >= '0' && ch <= '9') {
3050 exp_val = exp_val * 10 + ch - '0';
3051 ch = *p++;
3053 exp_val = exp_val * s;
3055 /* now we can generate the number */
3056 /* XXX: should patch directly float number */
3057 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3058 d = ldexp(d, exp_val - frac_bits);
3059 t = toup(ch);
3060 if (t == 'F') {
3061 ch = *p++;
3062 tok = TOK_CFLOAT;
3063 /* float : should handle overflow */
3064 tokc.f = (float)d;
3065 } else if (t == 'L') {
3066 ch = *p++;
3067 tok = TOK_CLDOUBLE;
3068 /* XXX: not large enough */
3069 tokc.ld = (long double)d;
3070 } else {
3071 tok = TOK_CDOUBLE;
3072 tokc.d = d;
3074 } else {
3075 /* decimal floats */
3076 if (ch == '.') {
3077 if (q >= token_buf + STRING_MAX_SIZE)
3078 goto num_too_long;
3079 *q++ = ch;
3080 ch = *p++;
3081 float_frac_parse:
3082 while (ch >= '0' && ch <= '9') {
3083 if (q >= token_buf + STRING_MAX_SIZE)
3084 goto num_too_long;
3085 *q++ = ch;
3086 ch = *p++;
3089 if (ch == 'e' || ch == 'E') {
3090 if (q >= token_buf + STRING_MAX_SIZE)
3091 goto num_too_long;
3092 *q++ = ch;
3093 ch = *p++;
3094 if (ch == '-' || ch == '+') {
3095 if (q >= token_buf + STRING_MAX_SIZE)
3096 goto num_too_long;
3097 *q++ = ch;
3098 ch = *p++;
3100 if (ch < '0' || ch > '9')
3101 expect("exponent digits");
3102 while (ch >= '0' && ch <= '9') {
3103 if (q >= token_buf + STRING_MAX_SIZE)
3104 goto num_too_long;
3105 *q++ = ch;
3106 ch = *p++;
3109 *q = '\0';
3110 t = toup(ch);
3111 errno = 0;
3112 if (t == 'F') {
3113 ch = *p++;
3114 tok = TOK_CFLOAT;
3115 tokc.f = strtof(token_buf, NULL);
3116 } else if (t == 'L') {
3117 ch = *p++;
3118 tok = TOK_CLDOUBLE;
3119 tokc.ld = strtold(token_buf, NULL);
3120 } else {
3121 tok = TOK_CDOUBLE;
3122 tokc.d = strtod(token_buf, NULL);
3125 } else {
3126 unsigned long long n, n1;
3127 int lcount, ucount;
3129 /* integer number */
3130 *q = '\0';
3131 q = token_buf;
3132 if (b == 10 && *q == '0') {
3133 b = 8;
3134 q++;
3136 n = 0;
3137 while(1) {
3138 t = *q++;
3139 /* no need for checks except for base 10 / 8 errors */
3140 if (t == '\0') {
3141 break;
3142 } else if (t >= 'a') {
3143 t = t - 'a' + 10;
3144 } else if (t >= 'A') {
3145 t = t - 'A' + 10;
3146 } else {
3147 t = t - '0';
3148 if (t >= b)
3149 error("invalid digit");
3151 n1 = n;
3152 n = n * b + t;
3153 /* detect overflow */
3154 /* XXX: this test is not reliable */
3155 if (n < n1)
3156 error("integer constant overflow");
3159 /* XXX: not exactly ANSI compliant */
3160 if ((n & 0xffffffff00000000LL) != 0) {
3161 if ((n >> 63) != 0)
3162 tok = TOK_CULLONG;
3163 else
3164 tok = TOK_CLLONG;
3165 } else if (n > 0x7fffffff) {
3166 tok = TOK_CUINT;
3167 } else {
3168 tok = TOK_CINT;
3170 lcount = 0;
3171 ucount = 0;
3172 for(;;) {
3173 t = toup(ch);
3174 if (t == 'L') {
3175 if (lcount >= 2)
3176 error("three 'l's in integer constant");
3177 lcount++;
3178 if (lcount == 2) {
3179 if (tok == TOK_CINT)
3180 tok = TOK_CLLONG;
3181 else if (tok == TOK_CUINT)
3182 tok = TOK_CULLONG;
3184 ch = *p++;
3185 } else if (t == 'U') {
3186 if (ucount >= 1)
3187 error("two 'u's in integer constant");
3188 ucount++;
3189 if (tok == TOK_CINT)
3190 tok = TOK_CUINT;
3191 else if (tok == TOK_CLLONG)
3192 tok = TOK_CULLONG;
3193 ch = *p++;
3194 } else {
3195 break;
3198 if (tok == TOK_CINT || tok == TOK_CUINT)
3199 tokc.ui = n;
3200 else
3201 tokc.ull = n;
3206 #define PARSE2(c1, tok1, c2, tok2) \
3207 case c1: \
3208 PEEKC(c, p); \
3209 if (c == c2) { \
3210 p++; \
3211 tok = tok2; \
3212 } else { \
3213 tok = tok1; \
3215 break;
3217 /* return next token without macro substitution */
3218 static inline void next_nomacro1(void)
3220 int t, c, is_long;
3221 TokenSym *ts;
3222 uint8_t *p, *p1;
3223 unsigned int h;
3225 p = file->buf_ptr;
3226 redo_no_start:
3227 c = *p;
3228 switch(c) {
3229 case ' ':
3230 case '\t':
3231 case '\f':
3232 case '\v':
3233 case '\r':
3234 p++;
3235 goto redo_no_start;
3237 case '\\':
3238 /* first look if it is in fact an end of buffer */
3239 if (p >= file->buf_end) {
3240 file->buf_ptr = p;
3241 handle_eob();
3242 p = file->buf_ptr;
3243 if (p >= file->buf_end)
3244 goto parse_eof;
3245 else
3246 goto redo_no_start;
3247 } else {
3248 file->buf_ptr = p;
3249 ch = *p;
3250 handle_stray();
3251 p = file->buf_ptr;
3252 goto redo_no_start;
3254 parse_eof:
3256 TCCState *s1 = tcc_state;
3257 if (parse_flags & PARSE_FLAG_LINEFEED) {
3258 tok = TOK_LINEFEED;
3259 } else if (s1->include_stack_ptr == s1->include_stack ||
3260 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3261 /* no include left : end of file. */
3262 tok = TOK_EOF;
3263 } else {
3264 /* pop include file */
3266 /* test if previous '#endif' was after a #ifdef at
3267 start of file */
3268 if (tok_flags & TOK_FLAG_ENDIF) {
3269 #ifdef INC_DEBUG
3270 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3271 #endif
3272 add_cached_include(s1, file->inc_type, file->inc_filename,
3273 file->ifndef_macro_saved);
3276 /* add end of include file debug info */
3277 if (do_debug) {
3278 put_stabd(N_EINCL, 0, 0);
3280 /* pop include stack */
3281 tcc_close(file);
3282 s1->include_stack_ptr--;
3283 file = *s1->include_stack_ptr;
3284 p = file->buf_ptr;
3285 goto redo_no_start;
3288 break;
3290 case '\n':
3291 if (parse_flags & PARSE_FLAG_LINEFEED) {
3292 tok = TOK_LINEFEED;
3293 } else {
3294 file->line_num++;
3295 tok_flags |= TOK_FLAG_BOL;
3296 p++;
3297 goto redo_no_start;
3299 break;
3301 case '#':
3302 /* XXX: simplify */
3303 PEEKC(c, p);
3304 if ((tok_flags & TOK_FLAG_BOL) &&
3305 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3306 file->buf_ptr = p;
3307 preprocess(tok_flags & TOK_FLAG_BOF);
3308 p = file->buf_ptr;
3309 goto redo_no_start;
3310 } else {
3311 if (c == '#') {
3312 p++;
3313 tok = TOK_TWOSHARPS;
3314 } else {
3315 tok = '#';
3318 break;
3320 case 'a': case 'b': case 'c': case 'd':
3321 case 'e': case 'f': case 'g': case 'h':
3322 case 'i': case 'j': case 'k': case 'l':
3323 case 'm': case 'n': case 'o': case 'p':
3324 case 'q': case 'r': case 's': case 't':
3325 case 'u': case 'v': case 'w': case 'x':
3326 case 'y': case 'z':
3327 case 'A': case 'B': case 'C': case 'D':
3328 case 'E': case 'F': case 'G': case 'H':
3329 case 'I': case 'J': case 'K':
3330 case 'M': case 'N': case 'O': case 'P':
3331 case 'Q': case 'R': case 'S': case 'T':
3332 case 'U': case 'V': case 'W': case 'X':
3333 case 'Y': case 'Z':
3334 case '_':
3335 parse_ident_fast:
3336 p1 = p;
3337 h = TOK_HASH_INIT;
3338 h = TOK_HASH_FUNC(h, c);
3339 p++;
3340 for(;;) {
3341 c = *p;
3342 if (!isidnum_table[c])
3343 break;
3344 h = TOK_HASH_FUNC(h, c);
3345 p++;
3347 if (c != '\\') {
3348 TokenSym **pts;
3349 int len;
3351 /* fast case : no stray found, so we have the full token
3352 and we have already hashed it */
3353 len = p - p1;
3354 h &= (TOK_HASH_SIZE - 1);
3355 pts = &hash_ident[h];
3356 for(;;) {
3357 ts = *pts;
3358 if (!ts)
3359 break;
3360 if (ts->len == len && !memcmp(ts->str, p1, len))
3361 goto token_found;
3362 pts = &(ts->hash_next);
3364 ts = tok_alloc_new(pts, p1, len);
3365 token_found: ;
3366 } else {
3367 /* slower case */
3368 cstr_reset(&tokcstr);
3370 while (p1 < p) {
3371 cstr_ccat(&tokcstr, *p1);
3372 p1++;
3374 p--;
3375 PEEKC(c, p);
3376 parse_ident_slow:
3377 while (isidnum_table[c]) {
3378 cstr_ccat(&tokcstr, c);
3379 PEEKC(c, p);
3381 ts = tok_alloc(tokcstr.data, tokcstr.size);
3383 tok = ts->tok;
3384 break;
3385 case 'L':
3386 t = p[1];
3387 if (t != '\\' && t != '\'' && t != '\"') {
3388 /* fast case */
3389 goto parse_ident_fast;
3390 } else {
3391 PEEKC(c, p);
3392 if (c == '\'' || c == '\"') {
3393 is_long = 1;
3394 goto str_const;
3395 } else {
3396 cstr_reset(&tokcstr);
3397 cstr_ccat(&tokcstr, 'L');
3398 goto parse_ident_slow;
3401 break;
3402 case '0': case '1': case '2': case '3':
3403 case '4': case '5': case '6': case '7':
3404 case '8': case '9':
3406 cstr_reset(&tokcstr);
3407 /* after the first digit, accept digits, alpha, '.' or sign if
3408 prefixed by 'eEpP' */
3409 parse_num:
3410 for(;;) {
3411 t = c;
3412 cstr_ccat(&tokcstr, c);
3413 PEEKC(c, p);
3414 if (!(isnum(c) || isid(c) || c == '.' ||
3415 ((c == '+' || c == '-') &&
3416 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3417 break;
3419 /* We add a trailing '\0' to ease parsing */
3420 cstr_ccat(&tokcstr, '\0');
3421 tokc.cstr = &tokcstr;
3422 tok = TOK_PPNUM;
3423 break;
3424 case '.':
3425 /* special dot handling because it can also start a number */
3426 PEEKC(c, p);
3427 if (isnum(c)) {
3428 cstr_reset(&tokcstr);
3429 cstr_ccat(&tokcstr, '.');
3430 goto parse_num;
3431 } else if (c == '.') {
3432 PEEKC(c, p);
3433 if (c != '.')
3434 expect("'.'");
3435 PEEKC(c, p);
3436 tok = TOK_DOTS;
3437 } else {
3438 tok = '.';
3440 break;
3441 case '\'':
3442 case '\"':
3443 is_long = 0;
3444 str_const:
3446 CString str;
3447 int sep;
3449 sep = c;
3451 /* parse the string */
3452 cstr_new(&str);
3453 p = parse_pp_string(p, sep, &str);
3454 cstr_ccat(&str, '\0');
3456 /* eval the escape (should be done as TOK_PPNUM) */
3457 cstr_reset(&tokcstr);
3458 parse_escape_string(&tokcstr, str.data, is_long);
3459 cstr_free(&str);
3461 if (sep == '\'') {
3462 int char_size;
3463 /* XXX: make it portable */
3464 if (!is_long)
3465 char_size = 1;
3466 else
3467 char_size = sizeof(int);
3468 if (tokcstr.size <= char_size)
3469 error("empty character constant");
3470 if (tokcstr.size > 2 * char_size)
3471 warning("multi-character character constant");
3472 if (!is_long) {
3473 tokc.i = *(int8_t *)tokcstr.data;
3474 tok = TOK_CCHAR;
3475 } else {
3476 tokc.i = *(int *)tokcstr.data;
3477 tok = TOK_LCHAR;
3479 } else {
3480 tokc.cstr = &tokcstr;
3481 if (!is_long)
3482 tok = TOK_STR;
3483 else
3484 tok = TOK_LSTR;
3487 break;
3489 case '<':
3490 PEEKC(c, p);
3491 if (c == '=') {
3492 p++;
3493 tok = TOK_LE;
3494 } else if (c == '<') {
3495 PEEKC(c, p);
3496 if (c == '=') {
3497 p++;
3498 tok = TOK_A_SHL;
3499 } else {
3500 tok = TOK_SHL;
3502 } else {
3503 tok = TOK_LT;
3505 break;
3507 case '>':
3508 PEEKC(c, p);
3509 if (c == '=') {
3510 p++;
3511 tok = TOK_GE;
3512 } else if (c == '>') {
3513 PEEKC(c, p);
3514 if (c == '=') {
3515 p++;
3516 tok = TOK_A_SAR;
3517 } else {
3518 tok = TOK_SAR;
3520 } else {
3521 tok = TOK_GT;
3523 break;
3525 case '&':
3526 PEEKC(c, p);
3527 if (c == '&') {
3528 p++;
3529 tok = TOK_LAND;
3530 } else if (c == '=') {
3531 p++;
3532 tok = TOK_A_AND;
3533 } else {
3534 tok = '&';
3536 break;
3538 case '|':
3539 PEEKC(c, p);
3540 if (c == '|') {
3541 p++;
3542 tok = TOK_LOR;
3543 } else if (c == '=') {
3544 p++;
3545 tok = TOK_A_OR;
3546 } else {
3547 tok = '|';
3549 break;
3551 case '+':
3552 PEEKC(c, p);
3553 if (c == '+') {
3554 p++;
3555 tok = TOK_INC;
3556 } else if (c == '=') {
3557 p++;
3558 tok = TOK_A_ADD;
3559 } else {
3560 tok = '+';
3562 break;
3564 case '-':
3565 PEEKC(c, p);
3566 if (c == '-') {
3567 p++;
3568 tok = TOK_DEC;
3569 } else if (c == '=') {
3570 p++;
3571 tok = TOK_A_SUB;
3572 } else if (c == '>') {
3573 p++;
3574 tok = TOK_ARROW;
3575 } else {
3576 tok = '-';
3578 break;
3580 PARSE2('!', '!', '=', TOK_NE)
3581 PARSE2('=', '=', '=', TOK_EQ)
3582 PARSE2('*', '*', '=', TOK_A_MUL)
3583 PARSE2('%', '%', '=', TOK_A_MOD)
3584 PARSE2('^', '^', '=', TOK_A_XOR)
3586 /* comments or operator */
3587 case '/':
3588 PEEKC(c, p);
3589 if (c == '*') {
3590 p = parse_comment(p);
3591 goto redo_no_start;
3592 } else if (c == '/') {
3593 p = parse_line_comment(p);
3594 goto redo_no_start;
3595 } else if (c == '=') {
3596 p++;
3597 tok = TOK_A_DIV;
3598 } else {
3599 tok = '/';
3601 break;
3603 /* simple tokens */
3604 case '(':
3605 case ')':
3606 case '[':
3607 case ']':
3608 case '{':
3609 case '}':
3610 case ',':
3611 case ';':
3612 case ':':
3613 case '?':
3614 case '~':
3615 case '$': /* only used in assembler */
3616 tok = c;
3617 p++;
3618 break;
3619 default:
3620 error("unrecognized character \\x%02x", c);
3621 break;
3623 file->buf_ptr = p;
3624 tok_flags = 0;
3625 #if defined(PARSE_DEBUG)
3626 printf("token = %s\n", get_tok_str(tok, &tokc));
3627 #endif
3630 /* return next token without macro substitution. Can read input from
3631 macro_ptr buffer */
3632 static void next_nomacro(void)
3634 if (macro_ptr) {
3635 redo:
3636 tok = *macro_ptr;
3637 if (tok) {
3638 TOK_GET(tok, macro_ptr, tokc);
3639 if (tok == TOK_LINENUM) {
3640 file->line_num = tokc.i;
3641 goto redo;
3644 } else {
3645 next_nomacro1();
3649 /* substitute args in macro_str and return allocated string */
3650 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3652 int *st, last_tok, t, notfirst;
3653 Sym *s;
3654 CValue cval;
3655 TokenString str;
3656 CString cstr;
3658 tok_str_new(&str);
3659 last_tok = 0;
3660 while(1) {
3661 TOK_GET(t, macro_str, cval);
3662 if (!t)
3663 break;
3664 if (t == '#') {
3665 /* stringize */
3666 TOK_GET(t, macro_str, cval);
3667 if (!t)
3668 break;
3669 s = sym_find2(args, t);
3670 if (s) {
3671 cstr_new(&cstr);
3672 st = (int *)s->c;
3673 notfirst = 0;
3674 while (*st) {
3675 if (notfirst)
3676 cstr_ccat(&cstr, ' ');
3677 TOK_GET(t, st, cval);
3678 cstr_cat(&cstr, get_tok_str(t, &cval));
3679 notfirst = 1;
3681 cstr_ccat(&cstr, '\0');
3682 #ifdef PP_DEBUG
3683 printf("stringize: %s\n", (char *)cstr.data);
3684 #endif
3685 /* add string */
3686 cval.cstr = &cstr;
3687 tok_str_add2(&str, TOK_STR, &cval);
3688 cstr_free(&cstr);
3689 } else {
3690 tok_str_add2(&str, t, &cval);
3692 } else if (t >= TOK_IDENT) {
3693 s = sym_find2(args, t);
3694 if (s) {
3695 st = (int *)s->c;
3696 /* if '##' is present before or after, no arg substitution */
3697 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3698 /* special case for var arg macros : ## eats the
3699 ',' if empty VA_ARGS variable. */
3700 /* XXX: test of the ',' is not 100%
3701 reliable. should fix it to avoid security
3702 problems */
3703 if (gnu_ext && s->type.t &&
3704 last_tok == TOK_TWOSHARPS &&
3705 str.len >= 2 && str.str[str.len - 2] == ',') {
3706 if (*st == 0) {
3707 /* suppress ',' '##' */
3708 str.len -= 2;
3709 } else {
3710 /* suppress '##' and add variable */
3711 str.len--;
3712 goto add_var;
3714 } else {
3715 int t1;
3716 add_var:
3717 for(;;) {
3718 TOK_GET(t1, st, cval);
3719 if (!t1)
3720 break;
3721 tok_str_add2(&str, t1, &cval);
3724 } else {
3725 /* NOTE: the stream cannot be read when macro
3726 substituing an argument */
3727 macro_subst(&str, nested_list, st, 0);
3729 } else {
3730 tok_str_add(&str, t);
3732 } else {
3733 tok_str_add2(&str, t, &cval);
3735 last_tok = t;
3737 tok_str_add(&str, 0);
3738 return str.str;
3741 static char const ab_month_name[12][4] =
3743 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3744 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3747 /* do macro substitution of current token with macro 's' and add
3748 result to (tok_str,tok_len). 'nested_list' is the list of all
3749 macros we got inside to avoid recursing. Return non zero if no
3750 substitution needs to be done */
3751 static int macro_subst_tok(TokenString *tok_str,
3752 Sym **nested_list, Sym *s, int can_read_stream)
3754 Sym *args, *sa, *sa1;
3755 int mstr_allocated, parlevel, *mstr, t;
3756 TokenString str;
3757 char *cstrval;
3758 CValue cval;
3759 CString cstr;
3761 /* if symbol is a macro, prepare substitution */
3763 /* special macros */
3764 if (tok == TOK___LINE__) {
3765 cval.i = file->line_num;
3766 tok_str_add2(tok_str, TOK_CINT, &cval);
3767 } else if (tok == TOK___FILE__) {
3768 cstrval = file->filename;
3769 goto add_cstr;
3770 tok_str_add2(tok_str, TOK_STR, &cval);
3771 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3772 time_t ti;
3773 struct tm *tm;
3774 char buf[64];
3776 time(&ti);
3777 tm = localtime(&ti);
3778 if (tok == TOK___DATE__) {
3779 snprintf(buf, sizeof(buf), "%s %2d %d",
3780 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3781 } else {
3782 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3783 tm->tm_hour, tm->tm_min, tm->tm_sec);
3785 cstrval = buf;
3786 add_cstr:
3787 cstr_new(&cstr);
3788 cstr_cat(&cstr, cstrval);
3789 cstr_ccat(&cstr, '\0');
3790 cval.cstr = &cstr;
3791 tok_str_add2(tok_str, TOK_STR, &cval);
3792 cstr_free(&cstr);
3793 } else {
3794 mstr = (int *)s->c;
3795 mstr_allocated = 0;
3796 if (s->type.t == MACRO_FUNC) {
3797 /* NOTE: we do not use next_nomacro to avoid eating the
3798 next token. XXX: find better solution */
3799 if (macro_ptr) {
3800 t = *macro_ptr;
3801 if (t == 0 && can_read_stream) {
3802 /* end of macro stream: we must look at the token
3803 after in the file */
3804 macro_ptr = NULL;
3805 goto parse_stream;
3807 } else {
3808 parse_stream:
3809 /* XXX: incorrect with comments */
3810 ch = file->buf_ptr[0];
3811 while (is_space(ch) || ch == '\n')
3812 cinp();
3813 t = ch;
3815 if (t != '(') /* no macro subst */
3816 return -1;
3818 /* argument macro */
3819 next_nomacro();
3820 next_nomacro();
3821 args = NULL;
3822 sa = s->next;
3823 /* NOTE: empty args are allowed, except if no args */
3824 for(;;) {
3825 /* handle '()' case */
3826 if (!args && !sa && tok == ')')
3827 break;
3828 if (!sa)
3829 error("macro '%s' used with too many args",
3830 get_tok_str(s->v, 0));
3831 tok_str_new(&str);
3832 parlevel = 0;
3833 /* NOTE: non zero sa->t indicates VA_ARGS */
3834 while ((parlevel > 0 ||
3835 (tok != ')' &&
3836 (tok != ',' || sa->type.t))) &&
3837 tok != -1) {
3838 if (tok == '(')
3839 parlevel++;
3840 else if (tok == ')')
3841 parlevel--;
3842 tok_str_add2(&str, tok, &tokc);
3843 next_nomacro();
3845 tok_str_add(&str, 0);
3846 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
3847 sa = sa->next;
3848 if (tok == ')') {
3849 /* special case for gcc var args: add an empty
3850 var arg argument if it is omitted */
3851 if (sa && sa->type.t && gnu_ext)
3852 continue;
3853 else
3854 break;
3856 if (tok != ',')
3857 expect(",");
3858 next_nomacro();
3860 if (sa) {
3861 error("macro '%s' used with too few args",
3862 get_tok_str(s->v, 0));
3865 /* now subst each arg */
3866 mstr = macro_arg_subst(nested_list, mstr, args);
3867 /* free memory */
3868 sa = args;
3869 while (sa) {
3870 sa1 = sa->prev;
3871 tok_str_free((int *)sa->c);
3872 tcc_free(sa);
3873 sa = sa1;
3875 mstr_allocated = 1;
3877 sym_push2(nested_list, s->v, 0, 0);
3878 macro_subst(tok_str, nested_list, mstr, 1);
3879 /* pop nested defined symbol */
3880 sa1 = *nested_list;
3881 *nested_list = sa1->prev;
3882 tcc_free(sa1);
3883 if (mstr_allocated)
3884 tok_str_free(mstr);
3886 return 0;
3889 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3890 return the resulting string (which must be freed). */
3891 static inline int *macro_twosharps(const int *macro_str)
3893 TokenSym *ts;
3894 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
3895 int t;
3896 const char *p1, *p2;
3897 CValue cval;
3898 TokenString macro_str1;
3899 CString cstr;
3901 start_macro_ptr = macro_str;
3902 /* we search the first '##' */
3903 for(;;) {
3904 macro_ptr1 = macro_str;
3905 TOK_GET(t, macro_str, cval);
3906 /* nothing more to do if end of string */
3907 if (t == 0)
3908 return NULL;
3909 if (*macro_str == TOK_TWOSHARPS)
3910 break;
3913 /* we saw '##', so we need more processing to handle it */
3914 cstr_new(&cstr);
3915 tok_str_new(&macro_str1);
3916 tok = t;
3917 tokc = cval;
3919 /* add all tokens seen so far */
3920 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
3921 TOK_GET(t, ptr, cval);
3922 tok_str_add2(&macro_str1, t, &cval);
3924 saved_macro_ptr = macro_ptr;
3925 /* XXX: get rid of the use of macro_ptr here */
3926 macro_ptr = (int *)macro_str;
3927 for(;;) {
3928 while (*macro_ptr == TOK_TWOSHARPS) {
3929 macro_ptr++;
3930 macro_ptr1 = macro_ptr;
3931 t = *macro_ptr;
3932 if (t) {
3933 TOK_GET(t, macro_ptr, cval);
3934 /* We concatenate the two tokens if we have an
3935 identifier or a preprocessing number */
3936 cstr_reset(&cstr);
3937 p1 = get_tok_str(tok, &tokc);
3938 cstr_cat(&cstr, p1);
3939 p2 = get_tok_str(t, &cval);
3940 cstr_cat(&cstr, p2);
3941 cstr_ccat(&cstr, '\0');
3943 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
3944 (t >= TOK_IDENT || t == TOK_PPNUM)) {
3945 if (tok == TOK_PPNUM) {
3946 /* if number, then create a number token */
3947 /* NOTE: no need to allocate because
3948 tok_str_add2() does it */
3949 tokc.cstr = &cstr;
3950 } else {
3951 /* if identifier, we must do a test to
3952 validate we have a correct identifier */
3953 if (t == TOK_PPNUM) {
3954 const char *p;
3955 int c;
3957 p = p2;
3958 for(;;) {
3959 c = *p;
3960 if (c == '\0')
3961 break;
3962 p++;
3963 if (!isnum(c) && !isid(c))
3964 goto error_pasting;
3967 ts = tok_alloc(cstr.data, strlen(cstr.data));
3968 tok = ts->tok; /* modify current token */
3970 } else {
3971 const char *str = cstr.data;
3972 const unsigned char *q;
3974 /* we look for a valid token */
3975 /* XXX: do more extensive checks */
3976 if (!strcmp(str, ">>=")) {
3977 tok = TOK_A_SAR;
3978 } else if (!strcmp(str, "<<=")) {
3979 tok = TOK_A_SHL;
3980 } else if (strlen(str) == 2) {
3981 /* search in two bytes table */
3982 q = tok_two_chars;
3983 for(;;) {
3984 if (!*q)
3985 goto error_pasting;
3986 if (q[0] == str[0] && q[1] == str[1])
3987 break;
3988 q += 3;
3990 tok = q[2];
3991 } else {
3992 error_pasting:
3993 /* NOTE: because get_tok_str use a static buffer,
3994 we must save it */
3995 cstr_reset(&cstr);
3996 p1 = get_tok_str(tok, &tokc);
3997 cstr_cat(&cstr, p1);
3998 cstr_ccat(&cstr, '\0');
3999 p2 = get_tok_str(t, &cval);
4000 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4001 /* cannot merge tokens: just add them separately */
4002 tok_str_add2(&macro_str1, tok, &tokc);
4003 /* XXX: free associated memory ? */
4004 tok = t;
4005 tokc = cval;
4010 tok_str_add2(&macro_str1, tok, &tokc);
4011 next_nomacro();
4012 if (tok == 0)
4013 break;
4015 macro_ptr = (int *)saved_macro_ptr;
4016 cstr_free(&cstr);
4017 tok_str_add(&macro_str1, 0);
4018 return macro_str1.str;
4022 /* do macro substitution of macro_str and add result to
4023 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4024 inside to avoid recursing. */
4025 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4026 const int *macro_str, int can_read_stream)
4028 Sym *s;
4029 int *saved_macro_ptr, *macro_str1;
4030 const int *ptr;
4031 int t, ret;
4032 CValue cval;
4034 /* first scan for '##' operator handling */
4035 ptr = macro_str;
4036 macro_str1 = macro_twosharps(ptr);
4037 if (macro_str1)
4038 ptr = macro_str1;
4039 while (1) {
4040 /* NOTE: ptr == NULL can only happen if tokens are read from
4041 file stream due to a macro function call */
4042 if (ptr == NULL)
4043 break;
4044 TOK_GET(t, ptr, cval);
4045 if (t == 0)
4046 break;
4047 s = define_find(t);
4048 if (s != NULL) {
4049 /* if nested substitution, do nothing */
4050 if (sym_find2(*nested_list, t))
4051 goto no_subst;
4052 saved_macro_ptr = macro_ptr;
4053 macro_ptr = (int *)ptr;
4054 tok = t;
4055 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4056 ptr = (int *)macro_ptr;
4057 macro_ptr = saved_macro_ptr;
4058 if (ret != 0)
4059 goto no_subst;
4060 } else {
4061 no_subst:
4062 tok_str_add2(tok_str, t, &cval);
4065 if (macro_str1)
4066 tok_str_free(macro_str1);
4069 /* return next token with macro substitution */
4070 static void next(void)
4072 Sym *nested_list, *s;
4073 TokenString str;
4075 redo:
4076 next_nomacro();
4077 if (!macro_ptr) {
4078 /* if not reading from macro substituted string, then try
4079 to substitute macros */
4080 if (tok >= TOK_IDENT &&
4081 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4082 s = define_find(tok);
4083 if (s) {
4084 /* we have a macro: we try to substitute */
4085 tok_str_new(&str);
4086 nested_list = NULL;
4087 if (macro_subst_tok(&str, &nested_list, s, 1) == 0) {
4088 /* substitution done, NOTE: maybe empty */
4089 tok_str_add(&str, 0);
4090 macro_ptr = str.str;
4091 macro_ptr_allocated = str.str;
4092 goto redo;
4096 } else {
4097 if (tok == 0) {
4098 /* end of macro or end of unget buffer */
4099 if (unget_buffer_enabled) {
4100 macro_ptr = unget_saved_macro_ptr;
4101 unget_buffer_enabled = 0;
4102 } else {
4103 /* end of macro string: free it */
4104 tok_str_free(macro_ptr_allocated);
4105 macro_ptr = NULL;
4107 goto redo;
4111 /* convert preprocessor tokens into C tokens */
4112 if (tok == TOK_PPNUM &&
4113 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4114 parse_number((char *)tokc.cstr->data);
4118 /* push back current token and set current token to 'last_tok'. Only
4119 identifier case handled for labels. */
4120 static inline void unget_tok(int last_tok)
4122 int i, n;
4123 int *q;
4124 unget_saved_macro_ptr = macro_ptr;
4125 unget_buffer_enabled = 1;
4126 q = unget_saved_buffer;
4127 macro_ptr = q;
4128 *q++ = tok;
4129 n = tok_ext_size(tok) - 1;
4130 for(i=0;i<n;i++)
4131 *q++ = tokc.tab[i];
4132 *q = 0; /* end of token string */
4133 tok = last_tok;
4137 void swap(int *p, int *q)
4139 int t;
4140 t = *p;
4141 *p = *q;
4142 *q = t;
4145 void vsetc(CType *type, int r, CValue *vc)
4147 int v;
4149 if (vtop >= vstack + VSTACK_SIZE)
4150 error("memory full");
4151 /* cannot let cpu flags if other instruction are generated. Also
4152 avoid leaving VT_JMP anywhere except on the top of the stack
4153 because it would complicate the code generator. */
4154 if (vtop >= vstack) {
4155 v = vtop->r & VT_VALMASK;
4156 if (v == VT_CMP || (v & ~1) == VT_JMP)
4157 gv(RC_INT);
4159 vtop++;
4160 vtop->type = *type;
4161 vtop->r = r;
4162 vtop->r2 = VT_CONST;
4163 vtop->c = *vc;
4166 /* push integer constant */
4167 void vpushi(int v)
4169 CValue cval;
4170 cval.i = v;
4171 vsetc(&int_type, VT_CONST, &cval);
4174 /* Return a static symbol pointing to a section */
4175 static Sym *get_sym_ref(CType *type, Section *sec,
4176 unsigned long offset, unsigned long size)
4178 int v;
4179 Sym *sym;
4181 v = anon_sym++;
4182 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4183 sym->type.ref = type->ref;
4184 sym->r = VT_CONST | VT_SYM;
4185 put_extern_sym(sym, sec, offset, size);
4186 return sym;
4189 /* push a reference to a section offset by adding a dummy symbol */
4190 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4192 CValue cval;
4194 cval.ul = 0;
4195 vsetc(type, VT_CONST | VT_SYM, &cval);
4196 vtop->sym = get_sym_ref(type, sec, offset, size);
4199 /* define a new external reference to a symbol 'v' of type 'u' */
4200 static Sym *external_global_sym(int v, CType *type, int r)
4202 Sym *s;
4204 s = sym_find(v);
4205 if (!s) {
4206 /* push forward reference */
4207 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4208 s->type.ref = type->ref;
4209 s->r = r | VT_CONST | VT_SYM;
4211 return s;
4214 /* define a new external reference to a symbol 'v' of type 'u' */
4215 static Sym *external_sym(int v, CType *type, int r)
4217 Sym *s;
4219 s = sym_find(v);
4220 if (!s) {
4221 /* push forward reference */
4222 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4223 s->type.t |= VT_EXTERN;
4224 } else {
4225 if (!is_compatible_types(&s->type, type))
4226 error("incompatible types for redefinition of '%s'",
4227 get_tok_str(v, NULL));
4229 return s;
4232 /* push a reference to global symbol v */
4233 static void vpush_global_sym(CType *type, int v)
4235 Sym *sym;
4236 CValue cval;
4238 sym = external_global_sym(v, type, 0);
4239 cval.ul = 0;
4240 vsetc(type, VT_CONST | VT_SYM, &cval);
4241 vtop->sym = sym;
4244 void vset(CType *type, int r, int v)
4246 CValue cval;
4248 cval.i = v;
4249 vsetc(type, r, &cval);
4252 void vseti(int r, int v)
4254 CType type;
4255 type.t = VT_INT;
4256 vset(&type, r, v);
4259 void vswap(void)
4261 SValue tmp;
4263 tmp = vtop[0];
4264 vtop[0] = vtop[-1];
4265 vtop[-1] = tmp;
4268 void vpushv(SValue *v)
4270 if (vtop >= vstack + VSTACK_SIZE)
4271 error("memory full");
4272 vtop++;
4273 *vtop = *v;
4276 void vdup(void)
4278 vpushv(vtop);
4281 /* save r to the memory stack, and mark it as being free */
4282 void save_reg(int r)
4284 int l, saved, size, align;
4285 SValue *p, sv;
4286 CType *type;
4288 /* modify all stack values */
4289 saved = 0;
4290 l = 0;
4291 for(p=vstack;p<=vtop;p++) {
4292 if ((p->r & VT_VALMASK) == r ||
4293 (p->r2 & VT_VALMASK) == r) {
4294 /* must save value on stack if not already done */
4295 if (!saved) {
4296 /* NOTE: must reload 'r' because r might be equal to r2 */
4297 r = p->r & VT_VALMASK;
4298 /* store register in the stack */
4299 type = &p->type;
4300 if ((p->r & VT_LVAL) ||
4301 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4302 type = &int_type;
4303 size = type_size(type, &align);
4304 loc = (loc - size) & -align;
4305 sv.type.t = type->t;
4306 sv.r = VT_LOCAL | VT_LVAL;
4307 sv.c.ul = loc;
4308 store(r, &sv);
4309 #ifdef TCC_TARGET_I386
4310 /* x86 specific: need to pop fp register ST0 if saved */
4311 if (r == TREG_ST0) {
4312 o(0xd9dd); /* fstp %st(1) */
4314 #endif
4315 /* special long long case */
4316 if ((type->t & VT_BTYPE) == VT_LLONG) {
4317 sv.c.ul += 4;
4318 store(p->r2, &sv);
4320 l = loc;
4321 saved = 1;
4323 /* mark that stack entry as being saved on the stack */
4324 if (p->r & VT_LVAL) {
4325 /* also clear the bounded flag because the
4326 relocation address of the function was stored in
4327 p->c.ul */
4328 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4329 } else {
4330 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4332 p->r2 = VT_CONST;
4333 p->c.ul = l;
4338 /* find a free register of class 'rc'. If none, save one register */
4339 int get_reg(int rc)
4341 int r;
4342 SValue *p;
4344 /* find a free register */
4345 for(r=0;r<NB_REGS;r++) {
4346 if (reg_classes[r] & rc) {
4347 for(p=vstack;p<=vtop;p++) {
4348 if ((p->r & VT_VALMASK) == r ||
4349 (p->r2 & VT_VALMASK) == r)
4350 goto notfound;
4352 return r;
4354 notfound: ;
4357 /* no register left : free the first one on the stack (VERY
4358 IMPORTANT to start from the bottom to ensure that we don't
4359 spill registers used in gen_opi()) */
4360 for(p=vstack;p<=vtop;p++) {
4361 r = p->r & VT_VALMASK;
4362 if (r < VT_CONST && (reg_classes[r] & rc))
4363 goto save_found;
4364 /* also look at second register (if long long) */
4365 r = p->r2 & VT_VALMASK;
4366 if (r < VT_CONST && (reg_classes[r] & rc)) {
4367 save_found:
4368 save_reg(r);
4369 return r;
4372 /* Should never comes here */
4373 return -1;
4376 /* save registers up to (vtop - n) stack entry */
4377 void save_regs(int n)
4379 int r;
4380 SValue *p, *p1;
4381 p1 = vtop - n;
4382 for(p = vstack;p <= p1; p++) {
4383 r = p->r & VT_VALMASK;
4384 if (r < VT_CONST) {
4385 save_reg(r);
4390 /* move register 's' to 'r', and flush previous value of r to memory
4391 if needed */
4392 void move_reg(int r, int s)
4394 SValue sv;
4396 if (r != s) {
4397 save_reg(r);
4398 sv.type.t = VT_INT;
4399 sv.r = s;
4400 sv.c.ul = 0;
4401 load(r, &sv);
4405 /* get address of vtop (vtop MUST BE an lvalue) */
4406 void gaddrof(void)
4408 vtop->r &= ~VT_LVAL;
4409 /* tricky: if saved lvalue, then we can go back to lvalue */
4410 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4411 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4414 #ifdef CONFIG_TCC_BCHECK
4415 /* generate lvalue bound code */
4416 void gbound(void)
4418 int lval_type;
4419 CType type1;
4421 vtop->r &= ~VT_MUSTBOUND;
4422 /* if lvalue, then use checking code before dereferencing */
4423 if (vtop->r & VT_LVAL) {
4424 /* if not VT_BOUNDED value, then make one */
4425 if (!(vtop->r & VT_BOUNDED)) {
4426 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4427 /* must save type because we must set it to int to get pointer */
4428 type1 = vtop->type;
4429 vtop->type.t = VT_INT;
4430 gaddrof();
4431 vpushi(0);
4432 gen_bounded_ptr_add();
4433 vtop->r |= lval_type;
4434 vtop->type = type1;
4436 /* then check for dereferencing */
4437 gen_bounded_ptr_deref();
4440 #endif
4442 /* store vtop a register belonging to class 'rc'. lvalues are
4443 converted to values. Cannot be used if cannot be converted to
4444 register value (such as structures). */
4445 int gv(int rc)
4447 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4448 unsigned long long ll;
4450 /* NOTE: get_reg can modify vstack[] */
4451 if (vtop->type.t & VT_BITFIELD) {
4452 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4453 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4454 /* remove bit field info to avoid loops */
4455 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4456 /* generate shifts */
4457 vpushi(32 - (bit_pos + bit_size));
4458 gen_op(TOK_SHL);
4459 vpushi(32 - bit_size);
4460 /* NOTE: transformed to SHR if unsigned */
4461 gen_op(TOK_SAR);
4462 r = gv(rc);
4463 } else {
4464 if (is_float(vtop->type.t) &&
4465 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4466 Sym *sym;
4467 int *ptr;
4468 unsigned long offset;
4470 /* XXX: unify with initializers handling ? */
4471 /* CPUs usually cannot use float constants, so we store them
4472 generically in data segment */
4473 size = type_size(&vtop->type, &align);
4474 offset = (data_section->data_offset + align - 1) & -align;
4475 data_section->data_offset = offset;
4476 /* XXX: not portable yet */
4477 ptr = section_ptr_add(data_section, size);
4478 size = size >> 2;
4479 for(i=0;i<size;i++)
4480 ptr[i] = vtop->c.tab[i];
4481 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4482 vtop->r |= VT_LVAL | VT_SYM;
4483 vtop->sym = sym;
4484 vtop->c.ul = 0;
4486 #ifdef CONFIG_TCC_BCHECK
4487 if (vtop->r & VT_MUSTBOUND)
4488 gbound();
4489 #endif
4491 r = vtop->r & VT_VALMASK;
4492 /* need to reload if:
4493 - constant
4494 - lvalue (need to dereference pointer)
4495 - already a register, but not in the right class */
4496 if (r >= VT_CONST ||
4497 (vtop->r & VT_LVAL) ||
4498 !(reg_classes[r] & rc) ||
4499 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4500 !(reg_classes[vtop->r2] & rc))) {
4501 r = get_reg(rc);
4502 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4503 /* two register type load : expand to two words
4504 temporarily */
4505 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4506 /* load constant */
4507 ll = vtop->c.ull;
4508 vtop->c.ui = ll; /* first word */
4509 load(r, vtop);
4510 vtop->r = r; /* save register value */
4511 vpushi(ll >> 32); /* second word */
4512 } else if (r >= VT_CONST ||
4513 (vtop->r & VT_LVAL)) {
4514 /* load from memory */
4515 load(r, vtop);
4516 vdup();
4517 vtop[-1].r = r; /* save register value */
4518 /* increment pointer to get second word */
4519 vtop->type.t = VT_INT;
4520 gaddrof();
4521 vpushi(4);
4522 gen_op('+');
4523 vtop->r |= VT_LVAL;
4524 } else {
4525 /* move registers */
4526 load(r, vtop);
4527 vdup();
4528 vtop[-1].r = r; /* save register value */
4529 vtop->r = vtop[-1].r2;
4531 /* allocate second register */
4532 rc2 = RC_INT;
4533 if (rc == RC_IRET)
4534 rc2 = RC_LRET;
4535 r2 = get_reg(rc2);
4536 load(r2, vtop);
4537 vpop();
4538 /* write second register */
4539 vtop->r2 = r2;
4540 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4541 int t1, t;
4542 /* lvalue of scalar type : need to use lvalue type
4543 because of possible cast */
4544 t = vtop->type.t;
4545 t1 = t;
4546 /* compute memory access type */
4547 if (vtop->r & VT_LVAL_BYTE)
4548 t = VT_BYTE;
4549 else if (vtop->r & VT_LVAL_SHORT)
4550 t = VT_SHORT;
4551 if (vtop->r & VT_LVAL_UNSIGNED)
4552 t |= VT_UNSIGNED;
4553 vtop->type.t = t;
4554 load(r, vtop);
4555 /* restore wanted type */
4556 vtop->type.t = t1;
4557 } else {
4558 /* one register type load */
4559 load(r, vtop);
4562 vtop->r = r;
4564 return r;
4567 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4568 void gv2(int rc1, int rc2)
4570 int v;
4572 /* generate more generic register first. But VT_JMP or VT_CMP
4573 values must be generated first in all cases to avoid possible
4574 reload errors */
4575 v = vtop[0].r & VT_VALMASK;
4576 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4577 vswap();
4578 gv(rc1);
4579 vswap();
4580 gv(rc2);
4581 /* test if reload is needed for first register */
4582 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4583 vswap();
4584 gv(rc1);
4585 vswap();
4587 } else {
4588 gv(rc2);
4589 vswap();
4590 gv(rc1);
4591 vswap();
4592 /* test if reload is needed for first register */
4593 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4594 gv(rc2);
4599 /* expand long long on stack in two int registers */
4600 void lexpand(void)
4602 int u;
4604 u = vtop->type.t & VT_UNSIGNED;
4605 gv(RC_INT);
4606 vdup();
4607 vtop[0].r = vtop[-1].r2;
4608 vtop[0].r2 = VT_CONST;
4609 vtop[-1].r2 = VT_CONST;
4610 vtop[0].type.t = VT_INT | u;
4611 vtop[-1].type.t = VT_INT | u;
4614 /* build a long long from two ints */
4615 void lbuild(int t)
4617 gv2(RC_INT, RC_INT);
4618 vtop[-1].r2 = vtop[0].r;
4619 vtop[-1].type.t = t;
4620 vpop();
4623 /* rotate n first stack elements to the bottom
4624 I1 ... In -> I2 ... In I1 [top is right]
4626 void vrotb(int n)
4628 int i;
4629 SValue tmp;
4631 tmp = vtop[-n + 1];
4632 for(i=-n+1;i!=0;i++)
4633 vtop[i] = vtop[i+1];
4634 vtop[0] = tmp;
4637 /* rotate n first stack elements to the top
4638 I1 ... In -> In I1 ... I(n-1) [top is right]
4640 void vrott(int n)
4642 int i;
4643 SValue tmp;
4645 tmp = vtop[0];
4646 for(i = 0;i < n - 1; i++)
4647 vtop[-i] = vtop[-i - 1];
4648 vtop[-n + 1] = tmp;
4651 /* pop stack value */
4652 void vpop(void)
4654 int v;
4655 v = vtop->r & VT_VALMASK;
4656 #ifdef TCC_TARGET_I386
4657 /* for x86, we need to pop the FP stack */
4658 if (v == TREG_ST0 && !nocode_wanted) {
4659 o(0xd9dd); /* fstp %st(1) */
4660 } else
4661 #endif
4662 if (v == VT_JMP || v == VT_JMPI) {
4663 /* need to put correct jump if && or || without test */
4664 gsym(vtop->c.ul);
4666 vtop--;
4669 /* convert stack entry to register and duplicate its value in another
4670 register */
4671 void gv_dup(void)
4673 int rc, t, r, r1;
4674 SValue sv;
4676 t = vtop->type.t;
4677 if ((t & VT_BTYPE) == VT_LLONG) {
4678 lexpand();
4679 gv_dup();
4680 vswap();
4681 vrotb(3);
4682 gv_dup();
4683 vrotb(4);
4684 /* stack: H L L1 H1 */
4685 lbuild(t);
4686 vrotb(3);
4687 vrotb(3);
4688 vswap();
4689 lbuild(t);
4690 vswap();
4691 } else {
4692 /* duplicate value */
4693 rc = RC_INT;
4694 sv.type.t = VT_INT;
4695 if (is_float(t)) {
4696 rc = RC_FLOAT;
4697 sv.type.t = t;
4699 r = gv(rc);
4700 r1 = get_reg(rc);
4701 sv.r = r;
4702 sv.c.ul = 0;
4703 load(r1, &sv); /* move r to r1 */
4704 vdup();
4705 /* duplicates value */
4706 vtop->r = r1;
4710 /* generate CPU independent (unsigned) long long operations */
4711 void gen_opl(int op)
4713 int t, a, b, op1, c, i;
4714 int func;
4715 SValue tmp;
4717 switch(op) {
4718 case '/':
4719 case TOK_PDIV:
4720 func = TOK___divdi3;
4721 goto gen_func;
4722 case TOK_UDIV:
4723 func = TOK___udivdi3;
4724 goto gen_func;
4725 case '%':
4726 func = TOK___moddi3;
4727 goto gen_func;
4728 case TOK_UMOD:
4729 func = TOK___umoddi3;
4730 gen_func:
4731 /* call generic long long function */
4732 vpush_global_sym(&func_old_type, func);
4733 vrott(3);
4734 gfunc_call(2);
4735 vpushi(0);
4736 vtop->r = REG_IRET;
4737 vtop->r2 = REG_LRET;
4738 break;
4739 case '^':
4740 case '&':
4741 case '|':
4742 case '*':
4743 case '+':
4744 case '-':
4745 t = vtop->type.t;
4746 vswap();
4747 lexpand();
4748 vrotb(3);
4749 lexpand();
4750 /* stack: L1 H1 L2 H2 */
4751 tmp = vtop[0];
4752 vtop[0] = vtop[-3];
4753 vtop[-3] = tmp;
4754 tmp = vtop[-2];
4755 vtop[-2] = vtop[-3];
4756 vtop[-3] = tmp;
4757 vswap();
4758 /* stack: H1 H2 L1 L2 */
4759 if (op == '*') {
4760 vpushv(vtop - 1);
4761 vpushv(vtop - 1);
4762 gen_op(TOK_UMULL);
4763 lexpand();
4764 /* stack: H1 H2 L1 L2 ML MH */
4765 for(i=0;i<4;i++)
4766 vrotb(6);
4767 /* stack: ML MH H1 H2 L1 L2 */
4768 tmp = vtop[0];
4769 vtop[0] = vtop[-2];
4770 vtop[-2] = tmp;
4771 /* stack: ML MH H1 L2 H2 L1 */
4772 gen_op('*');
4773 vrotb(3);
4774 vrotb(3);
4775 gen_op('*');
4776 /* stack: ML MH M1 M2 */
4777 gen_op('+');
4778 gen_op('+');
4779 } else if (op == '+' || op == '-') {
4780 /* XXX: add non carry method too (for MIPS or alpha) */
4781 if (op == '+')
4782 op1 = TOK_ADDC1;
4783 else
4784 op1 = TOK_SUBC1;
4785 gen_op(op1);
4786 /* stack: H1 H2 (L1 op L2) */
4787 vrotb(3);
4788 vrotb(3);
4789 gen_op(op1 + 1); /* TOK_xxxC2 */
4790 } else {
4791 gen_op(op);
4792 /* stack: H1 H2 (L1 op L2) */
4793 vrotb(3);
4794 vrotb(3);
4795 /* stack: (L1 op L2) H1 H2 */
4796 gen_op(op);
4797 /* stack: (L1 op L2) (H1 op H2) */
4799 /* stack: L H */
4800 lbuild(t);
4801 break;
4802 case TOK_SAR:
4803 case TOK_SHR:
4804 case TOK_SHL:
4805 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4806 t = vtop[-1].type.t;
4807 vswap();
4808 lexpand();
4809 vrotb(3);
4810 /* stack: L H shift */
4811 c = (int)vtop->c.i;
4812 /* constant: simpler */
4813 /* NOTE: all comments are for SHL. the other cases are
4814 done by swaping words */
4815 vpop();
4816 if (op != TOK_SHL)
4817 vswap();
4818 if (c >= 32) {
4819 /* stack: L H */
4820 vpop();
4821 if (c > 32) {
4822 vpushi(c - 32);
4823 gen_op(op);
4825 if (op != TOK_SAR) {
4826 vpushi(0);
4827 } else {
4828 gv_dup();
4829 vpushi(31);
4830 gen_op(TOK_SAR);
4832 vswap();
4833 } else {
4834 vswap();
4835 gv_dup();
4836 /* stack: H L L */
4837 vpushi(c);
4838 gen_op(op);
4839 vswap();
4840 vpushi(32 - c);
4841 if (op == TOK_SHL)
4842 gen_op(TOK_SHR);
4843 else
4844 gen_op(TOK_SHL);
4845 vrotb(3);
4846 /* stack: L L H */
4847 vpushi(c);
4848 if (op == TOK_SHL)
4849 gen_op(TOK_SHL);
4850 else
4851 gen_op(TOK_SHR);
4852 gen_op('|');
4854 if (op != TOK_SHL)
4855 vswap();
4856 lbuild(t);
4857 } else {
4858 /* XXX: should provide a faster fallback on x86 ? */
4859 switch(op) {
4860 case TOK_SAR:
4861 func = TOK___sardi3;
4862 goto gen_func;
4863 case TOK_SHR:
4864 func = TOK___shrdi3;
4865 goto gen_func;
4866 case TOK_SHL:
4867 func = TOK___shldi3;
4868 goto gen_func;
4871 break;
4872 default:
4873 /* compare operations */
4874 t = vtop->type.t;
4875 vswap();
4876 lexpand();
4877 vrotb(3);
4878 lexpand();
4879 /* stack: L1 H1 L2 H2 */
4880 tmp = vtop[-1];
4881 vtop[-1] = vtop[-2];
4882 vtop[-2] = tmp;
4883 /* stack: L1 L2 H1 H2 */
4884 /* compare high */
4885 op1 = op;
4886 /* when values are equal, we need to compare low words. since
4887 the jump is inverted, we invert the test too. */
4888 if (op1 == TOK_LT)
4889 op1 = TOK_LE;
4890 else if (op1 == TOK_GT)
4891 op1 = TOK_GE;
4892 else if (op1 == TOK_ULT)
4893 op1 = TOK_ULE;
4894 else if (op1 == TOK_UGT)
4895 op1 = TOK_UGE;
4896 a = 0;
4897 b = 0;
4898 gen_op(op1);
4899 if (op1 != TOK_NE) {
4900 a = gtst(1, 0);
4902 if (op != TOK_EQ) {
4903 /* generate non equal test */
4904 /* XXX: NOT PORTABLE yet */
4905 if (a == 0) {
4906 b = gtst(0, 0);
4907 } else {
4908 #ifdef TCC_TARGET_I386
4909 b = psym(0x850f, 0);
4910 #else
4911 error("not implemented");
4912 #endif
4915 /* compare low. Always unsigned */
4916 op1 = op;
4917 if (op1 == TOK_LT)
4918 op1 = TOK_ULT;
4919 else if (op1 == TOK_LE)
4920 op1 = TOK_ULE;
4921 else if (op1 == TOK_GT)
4922 op1 = TOK_UGT;
4923 else if (op1 == TOK_GE)
4924 op1 = TOK_UGE;
4925 gen_op(op1);
4926 a = gtst(1, a);
4927 gsym(b);
4928 vseti(VT_JMPI, a);
4929 break;
4933 /* handle integer constant optimizations and various machine
4934 independent opt */
4935 void gen_opic(int op)
4937 int fc, c1, c2, n;
4938 SValue *v1, *v2;
4940 v1 = vtop - 1;
4941 v2 = vtop;
4942 /* currently, we cannot do computations with forward symbols */
4943 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4944 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4945 if (c1 && c2) {
4946 fc = v2->c.i;
4947 switch(op) {
4948 case '+': v1->c.i += fc; break;
4949 case '-': v1->c.i -= fc; break;
4950 case '&': v1->c.i &= fc; break;
4951 case '^': v1->c.i ^= fc; break;
4952 case '|': v1->c.i |= fc; break;
4953 case '*': v1->c.i *= fc; break;
4955 case TOK_PDIV:
4956 case '/':
4957 case '%':
4958 case TOK_UDIV:
4959 case TOK_UMOD:
4960 /* if division by zero, generate explicit division */
4961 if (fc == 0) {
4962 if (const_wanted)
4963 error("division by zero in constant");
4964 goto general_case;
4966 switch(op) {
4967 default: v1->c.i /= fc; break;
4968 case '%': v1->c.i %= fc; break;
4969 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
4970 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
4972 break;
4973 case TOK_SHL: v1->c.i <<= fc; break;
4974 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
4975 case TOK_SAR: v1->c.i >>= fc; break;
4976 /* tests */
4977 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
4978 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
4979 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
4980 case TOK_NE: v1->c.i = v1->c.i != fc; break;
4981 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
4982 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
4983 case TOK_LT: v1->c.i = v1->c.i < fc; break;
4984 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
4985 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
4986 case TOK_GT: v1->c.i = v1->c.i > fc; break;
4987 /* logical */
4988 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
4989 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
4990 default:
4991 goto general_case;
4993 vtop--;
4994 } else {
4995 /* if commutative ops, put c2 as constant */
4996 if (c1 && (op == '+' || op == '&' || op == '^' ||
4997 op == '|' || op == '*')) {
4998 vswap();
4999 swap(&c1, &c2);
5001 fc = vtop->c.i;
5002 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5003 op == TOK_PDIV) &&
5004 fc == 1) ||
5005 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5006 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5007 fc == 0) ||
5008 (op == '&' &&
5009 fc == -1))) {
5010 /* nothing to do */
5011 vtop--;
5012 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5013 /* try to use shifts instead of muls or divs */
5014 if (fc > 0 && (fc & (fc - 1)) == 0) {
5015 n = -1;
5016 while (fc) {
5017 fc >>= 1;
5018 n++;
5020 vtop->c.i = n;
5021 if (op == '*')
5022 op = TOK_SHL;
5023 else if (op == TOK_PDIV)
5024 op = TOK_SAR;
5025 else
5026 op = TOK_SHR;
5028 goto general_case;
5029 } else if (c2 && (op == '+' || op == '-') &&
5030 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5031 (VT_CONST | VT_SYM)) {
5032 /* symbol + constant case */
5033 if (op == '-')
5034 fc = -fc;
5035 vtop--;
5036 vtop->c.i += fc;
5037 } else {
5038 general_case:
5039 if (!nocode_wanted) {
5040 /* call low level op generator */
5041 gen_opi(op);
5042 } else {
5043 vtop--;
5049 /* generate a floating point operation with constant propagation */
5050 void gen_opif(int op)
5052 int c1, c2;
5053 SValue *v1, *v2;
5054 long double f1, f2;
5056 v1 = vtop - 1;
5057 v2 = vtop;
5058 /* currently, we cannot do computations with forward symbols */
5059 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5060 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5061 if (c1 && c2) {
5062 if (v1->type.t == VT_FLOAT) {
5063 f1 = v1->c.f;
5064 f2 = v2->c.f;
5065 } else if (v1->type.t == VT_DOUBLE) {
5066 f1 = v1->c.d;
5067 f2 = v2->c.d;
5068 } else {
5069 f1 = v1->c.ld;
5070 f2 = v2->c.ld;
5073 /* NOTE: we only do constant propagation if finite number (not
5074 NaN or infinity) (ANSI spec) */
5075 if (!ieee_finite(f1) || !ieee_finite(f2))
5076 goto general_case;
5078 switch(op) {
5079 case '+': f1 += f2; break;
5080 case '-': f1 -= f2; break;
5081 case '*': f1 *= f2; break;
5082 case '/':
5083 if (f2 == 0.0) {
5084 if (const_wanted)
5085 error("division by zero in constant");
5086 goto general_case;
5088 f1 /= f2;
5089 break;
5090 /* XXX: also handles tests ? */
5091 default:
5092 goto general_case;
5094 /* XXX: overflow test ? */
5095 if (v1->type.t == VT_FLOAT) {
5096 v1->c.f = f1;
5097 } else if (v1->type.t == VT_DOUBLE) {
5098 v1->c.d = f1;
5099 } else {
5100 v1->c.ld = f1;
5102 vtop--;
5103 } else {
5104 general_case:
5105 if (!nocode_wanted) {
5106 gen_opf(op);
5107 } else {
5108 vtop--;
5113 static int pointed_size(CType *type)
5115 int align;
5116 return type_size(pointed_type(type), &align);
5119 static inline int is_null_pointer(SValue *p)
5121 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5122 return 0;
5123 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5124 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5127 static inline int is_integer_btype(int bt)
5129 return (bt == VT_BYTE || bt == VT_SHORT ||
5130 bt == VT_INT || bt == VT_LLONG);
5133 /* check types for comparison or substraction of pointers */
5134 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5136 CType *type1, *type2, tmp_type1, tmp_type2;
5137 int bt1, bt2;
5139 /* null pointers are accepted for all comparisons as gcc */
5140 if (is_null_pointer(p1) || is_null_pointer(p2))
5141 return;
5142 type1 = &p1->type;
5143 type2 = &p2->type;
5144 bt1 = type1->t & VT_BTYPE;
5145 bt2 = type2->t & VT_BTYPE;
5146 /* accept comparison between pointer and integer with a warning */
5147 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5148 warning("comparison between pointer and integer");
5149 return;
5152 /* both must be pointers or implicit function pointers */
5153 if (bt1 == VT_PTR) {
5154 type1 = pointed_type(type1);
5155 } else if (bt1 != VT_FUNC)
5156 goto invalid_operands;
5158 if (bt2 == VT_PTR) {
5159 type2 = pointed_type(type2);
5160 } else if (bt2 != VT_FUNC) {
5161 invalid_operands:
5162 error("invalid operands to binary %s", get_tok_str(op, NULL));
5164 if ((type1->t & VT_BTYPE) == VT_VOID ||
5165 (type2->t & VT_BTYPE) == VT_VOID)
5166 return;
5167 tmp_type1 = *type1;
5168 tmp_type2 = *type2;
5169 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5170 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5171 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5172 /* gcc-like error if '-' is used */
5173 if (op == '-')
5174 goto invalid_operands;
5175 else
5176 warning("comparison of distinct pointer types lacks a cast");
5180 /* generic gen_op: handles types problems */
5181 void gen_op(int op)
5183 int u, t1, t2, bt1, bt2, t;
5184 CType type1;
5186 t1 = vtop[-1].type.t;
5187 t2 = vtop[0].type.t;
5188 bt1 = t1 & VT_BTYPE;
5189 bt2 = t2 & VT_BTYPE;
5191 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5192 /* at least one operand is a pointer */
5193 /* relationnal op: must be both pointers */
5194 if (op >= TOK_ULT && op <= TOK_GT) {
5195 check_comparison_pointer_types(vtop - 1, vtop, op);
5196 /* pointers are handled are unsigned */
5197 t = VT_INT | VT_UNSIGNED;
5198 goto std_op;
5200 /* if both pointers, then it must be the '-' op */
5201 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5202 if (op != '-')
5203 error("cannot use pointers here");
5204 check_comparison_pointer_types(vtop - 1, vtop, op);
5205 /* XXX: check that types are compatible */
5206 u = pointed_size(&vtop[-1].type);
5207 gen_opic(op);
5208 /* set to integer type */
5209 vtop->type.t = VT_INT;
5210 vpushi(u);
5211 gen_op(TOK_PDIV);
5212 } else {
5213 /* exactly one pointer : must be '+' or '-'. */
5214 if (op != '-' && op != '+')
5215 error("cannot use pointers here");
5216 /* Put pointer as first operand */
5217 if (bt2 == VT_PTR) {
5218 vswap();
5219 swap(&t1, &t2);
5221 type1 = vtop[-1].type;
5222 /* XXX: cast to int ? (long long case) */
5223 vpushi(pointed_size(&vtop[-1].type));
5224 gen_op('*');
5225 #ifdef CONFIG_TCC_BCHECK
5226 /* if evaluating constant expression, no code should be
5227 generated, so no bound check */
5228 if (do_bounds_check && !const_wanted) {
5229 /* if bounded pointers, we generate a special code to
5230 test bounds */
5231 if (op == '-') {
5232 vpushi(0);
5233 vswap();
5234 gen_op('-');
5236 gen_bounded_ptr_add();
5237 } else
5238 #endif
5240 gen_opic(op);
5242 /* put again type if gen_opic() swaped operands */
5243 vtop->type = type1;
5245 } else if (is_float(bt1) || is_float(bt2)) {
5246 /* compute bigger type and do implicit casts */
5247 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5248 t = VT_LDOUBLE;
5249 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5250 t = VT_DOUBLE;
5251 } else {
5252 t = VT_FLOAT;
5254 /* floats can only be used for a few operations */
5255 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5256 (op < TOK_ULT || op > TOK_GT))
5257 error("invalid operands for binary operation");
5258 goto std_op;
5259 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5260 /* cast to biggest op */
5261 t = VT_LLONG;
5262 /* convert to unsigned if it does not fit in a long long */
5263 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5264 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5265 t |= VT_UNSIGNED;
5266 goto std_op;
5267 } else {
5268 /* integer operations */
5269 t = VT_INT;
5270 /* convert to unsigned if it does not fit in an integer */
5271 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5272 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5273 t |= VT_UNSIGNED;
5274 std_op:
5275 /* XXX: currently, some unsigned operations are explicit, so
5276 we modify them here */
5277 if (t & VT_UNSIGNED) {
5278 if (op == TOK_SAR)
5279 op = TOK_SHR;
5280 else if (op == '/')
5281 op = TOK_UDIV;
5282 else if (op == '%')
5283 op = TOK_UMOD;
5284 else if (op == TOK_LT)
5285 op = TOK_ULT;
5286 else if (op == TOK_GT)
5287 op = TOK_UGT;
5288 else if (op == TOK_LE)
5289 op = TOK_ULE;
5290 else if (op == TOK_GE)
5291 op = TOK_UGE;
5293 vswap();
5294 type1.t = t;
5295 gen_cast(&type1);
5296 vswap();
5297 /* special case for shifts and long long: we keep the shift as
5298 an integer */
5299 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5300 type1.t = VT_INT;
5301 gen_cast(&type1);
5302 if (is_float(t))
5303 gen_opif(op);
5304 else if ((t & VT_BTYPE) == VT_LLONG)
5305 gen_opl(op);
5306 else
5307 gen_opic(op);
5308 if (op >= TOK_ULT && op <= TOK_GT) {
5309 /* relationnal op: the result is an int */
5310 vtop->type.t = VT_INT;
5311 } else {
5312 vtop->type.t = t;
5317 /* generic itof for unsigned long long case */
5318 void gen_cvt_itof1(int t)
5320 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5321 (VT_LLONG | VT_UNSIGNED)) {
5323 if (t == VT_FLOAT)
5324 vpush_global_sym(&func_old_type, TOK___ulltof);
5325 else if (t == VT_DOUBLE)
5326 vpush_global_sym(&func_old_type, TOK___ulltod);
5327 else
5328 vpush_global_sym(&func_old_type, TOK___ulltold);
5329 vrott(2);
5330 gfunc_call(1);
5331 vpushi(0);
5332 vtop->r = REG_FRET;
5333 } else {
5334 gen_cvt_itof(t);
5338 /* generic ftoi for unsigned long long case */
5339 void gen_cvt_ftoi1(int t)
5341 int st;
5343 if (t == (VT_LLONG | VT_UNSIGNED)) {
5344 /* not handled natively */
5345 st = vtop->type.t & VT_BTYPE;
5346 if (st == VT_FLOAT)
5347 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5348 else if (st == VT_DOUBLE)
5349 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5350 else
5351 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5352 vrott(2);
5353 gfunc_call(1);
5354 vpushi(0);
5355 vtop->r = REG_IRET;
5356 vtop->r2 = REG_LRET;
5357 } else {
5358 gen_cvt_ftoi(t);
5362 /* force char or short cast */
5363 void force_charshort_cast(int t)
5365 int bits, dbt;
5366 dbt = t & VT_BTYPE;
5367 /* XXX: add optimization if lvalue : just change type and offset */
5368 if (dbt == VT_BYTE)
5369 bits = 8;
5370 else
5371 bits = 16;
5372 if (t & VT_UNSIGNED) {
5373 vpushi((1 << bits) - 1);
5374 gen_op('&');
5375 } else {
5376 bits = 32 - bits;
5377 vpushi(bits);
5378 gen_op(TOK_SHL);
5379 vpushi(bits);
5380 gen_op(TOK_SAR);
5384 /* cast 'vtop' to 'type' */
5385 static void gen_cast(CType *type)
5387 int sbt, dbt, sf, df, c;
5389 /* special delayed cast for char/short */
5390 /* XXX: in some cases (multiple cascaded casts), it may still
5391 be incorrect */
5392 if (vtop->r & VT_MUSTCAST) {
5393 vtop->r &= ~VT_MUSTCAST;
5394 force_charshort_cast(vtop->type.t);
5397 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5398 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5400 if (sbt != dbt && !nocode_wanted) {
5401 sf = is_float(sbt);
5402 df = is_float(dbt);
5403 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5404 if (sf && df) {
5405 /* convert from fp to fp */
5406 if (c) {
5407 /* constant case: we can do it now */
5408 /* XXX: in ISOC, cannot do it if error in convert */
5409 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5410 vtop->c.f = (float)vtop->c.d;
5411 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5412 vtop->c.f = (float)vtop->c.ld;
5413 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5414 vtop->c.d = (double)vtop->c.f;
5415 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5416 vtop->c.d = (double)vtop->c.ld;
5417 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5418 vtop->c.ld = (long double)vtop->c.f;
5419 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5420 vtop->c.ld = (long double)vtop->c.d;
5421 } else {
5422 /* non constant case: generate code */
5423 gen_cvt_ftof(dbt);
5425 } else if (df) {
5426 /* convert int to fp */
5427 if (c) {
5428 switch(sbt) {
5429 case VT_LLONG | VT_UNSIGNED:
5430 case VT_LLONG:
5431 /* XXX: add const cases for long long */
5432 goto do_itof;
5433 case VT_INT | VT_UNSIGNED:
5434 switch(dbt) {
5435 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5436 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5437 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5439 break;
5440 default:
5441 switch(dbt) {
5442 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5443 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5444 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5446 break;
5448 } else {
5449 do_itof:
5450 gen_cvt_itof1(dbt);
5452 } else if (sf) {
5453 /* convert fp to int */
5454 /* we handle char/short/etc... with generic code */
5455 if (dbt != (VT_INT | VT_UNSIGNED) &&
5456 dbt != (VT_LLONG | VT_UNSIGNED) &&
5457 dbt != VT_LLONG)
5458 dbt = VT_INT;
5459 if (c) {
5460 switch(dbt) {
5461 case VT_LLONG | VT_UNSIGNED:
5462 case VT_LLONG:
5463 /* XXX: add const cases for long long */
5464 goto do_ftoi;
5465 case VT_INT | VT_UNSIGNED:
5466 switch(sbt) {
5467 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5468 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5469 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5471 break;
5472 default:
5473 /* int case */
5474 switch(sbt) {
5475 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5476 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5477 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5479 break;
5481 } else {
5482 do_ftoi:
5483 gen_cvt_ftoi1(dbt);
5485 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5486 /* additional cast for char/short/bool... */
5487 vtop->type.t = dbt;
5488 gen_cast(type);
5490 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5491 if ((sbt & VT_BTYPE) != VT_LLONG) {
5492 /* scalar to long long */
5493 if (c) {
5494 if (sbt == (VT_INT | VT_UNSIGNED))
5495 vtop->c.ll = vtop->c.ui;
5496 else
5497 vtop->c.ll = vtop->c.i;
5498 } else {
5499 /* machine independent conversion */
5500 gv(RC_INT);
5501 /* generate high word */
5502 if (sbt == (VT_INT | VT_UNSIGNED)) {
5503 vpushi(0);
5504 gv(RC_INT);
5505 } else {
5506 gv_dup();
5507 vpushi(31);
5508 gen_op(TOK_SAR);
5510 /* patch second register */
5511 vtop[-1].r2 = vtop->r;
5512 vpop();
5515 } else if (dbt == VT_BOOL) {
5516 /* scalar to bool */
5517 vpushi(0);
5518 gen_op(TOK_NE);
5519 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5520 (dbt & VT_BTYPE) == VT_SHORT) {
5521 force_charshort_cast(dbt);
5522 } else if ((dbt & VT_BTYPE) == VT_INT) {
5523 /* scalar to int */
5524 if (sbt == VT_LLONG) {
5525 /* from long long: just take low order word */
5526 lexpand();
5527 vpop();
5529 /* if lvalue and single word type, nothing to do because
5530 the lvalue already contains the real type size (see
5531 VT_LVAL_xxx constants) */
5534 vtop->type = *type;
5537 /* return type size. Put alignment at 'a' */
5538 static int type_size(CType *type, int *a)
5540 Sym *s;
5541 int bt;
5543 bt = type->t & VT_BTYPE;
5544 if (bt == VT_STRUCT) {
5545 /* struct/union */
5546 s = type->ref;
5547 *a = s->r;
5548 return s->c;
5549 } else if (bt == VT_PTR) {
5550 if (type->t & VT_ARRAY) {
5551 s = type->ref;
5552 return type_size(&s->type, a) * s->c;
5553 } else {
5554 *a = PTR_SIZE;
5555 return PTR_SIZE;
5557 } else if (bt == VT_LDOUBLE) {
5558 *a = LDOUBLE_ALIGN;
5559 return LDOUBLE_SIZE;
5560 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5561 *a = 4; /* XXX: i386 specific */
5562 return 8;
5563 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5564 *a = 4;
5565 return 4;
5566 } else if (bt == VT_SHORT) {
5567 *a = 2;
5568 return 2;
5569 } else {
5570 /* char, void, function, _Bool */
5571 *a = 1;
5572 return 1;
5576 /* return the pointed type of t */
5577 static inline CType *pointed_type(CType *type)
5579 return &type->ref->type;
5582 /* modify type so that its it is a pointer to type. */
5583 static void mk_pointer(CType *type)
5585 Sym *s;
5586 s = sym_push(SYM_FIELD, type, 0, -1);
5587 type->t = VT_PTR | (type->t & ~VT_TYPE);
5588 type->ref = s;
5591 /* compare function types. OLD functions match any new functions */
5592 static int is_compatible_func(CType *type1, CType *type2)
5594 Sym *s1, *s2;
5596 s1 = type1->ref;
5597 s2 = type2->ref;
5598 if (!is_compatible_types(&s1->type, &s2->type))
5599 return 0;
5600 /* XXX: not complete */
5601 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5602 return 1;
5603 if (s1->c != s2->c)
5604 return 0;
5605 while (s1 != NULL) {
5606 if (s2 == NULL)
5607 return 0;
5608 if (!is_compatible_types(&s1->type, &s2->type))
5609 return 0;
5610 s1 = s1->next;
5611 s2 = s2->next;
5613 if (s2)
5614 return 0;
5615 return 1;
5618 /* return true if type1 and type2 are exactly the same (including
5619 qualifiers).
5621 - enums are not checked as gcc __builtin_types_compatible_p ()
5623 static int is_compatible_types(CType *type1, CType *type2)
5625 int bt1, t1, t2;
5627 t1 = type1->t & VT_TYPE;
5628 t2 = type2->t & VT_TYPE;
5629 /* XXX: bitfields ? */
5630 if (t1 != t2)
5631 return 0;
5632 /* test more complicated cases */
5633 bt1 = t1 & VT_BTYPE;
5634 if (bt1 == VT_PTR) {
5635 type1 = pointed_type(type1);
5636 type2 = pointed_type(type2);
5637 return is_compatible_types(type1, type2);
5638 } else if (bt1 == VT_STRUCT) {
5639 return (type1->ref == type2->ref);
5640 } else if (bt1 == VT_FUNC) {
5641 return is_compatible_func(type1, type2);
5642 } else {
5643 return 1;
5647 /* print a type. If 'varstr' is not NULL, then the variable is also
5648 printed in the type */
5649 /* XXX: union */
5650 /* XXX: add array and function pointers */
5651 void type_to_str(char *buf, int buf_size,
5652 CType *type, const char *varstr)
5654 int bt, v, t;
5655 Sym *s, *sa;
5656 char buf1[256];
5657 const char *tstr;
5659 t = type->t & VT_TYPE;
5660 bt = t & VT_BTYPE;
5661 buf[0] = '\0';
5662 if (t & VT_CONSTANT)
5663 pstrcat(buf, buf_size, "const ");
5664 if (t & VT_VOLATILE)
5665 pstrcat(buf, buf_size, "volatile ");
5666 if (t & VT_UNSIGNED)
5667 pstrcat(buf, buf_size, "unsigned ");
5668 switch(bt) {
5669 case VT_VOID:
5670 tstr = "void";
5671 goto add_tstr;
5672 case VT_BOOL:
5673 tstr = "_Bool";
5674 goto add_tstr;
5675 case VT_BYTE:
5676 tstr = "char";
5677 goto add_tstr;
5678 case VT_SHORT:
5679 tstr = "short";
5680 goto add_tstr;
5681 case VT_INT:
5682 tstr = "int";
5683 goto add_tstr;
5684 case VT_LONG:
5685 tstr = "long";
5686 goto add_tstr;
5687 case VT_LLONG:
5688 tstr = "long long";
5689 goto add_tstr;
5690 case VT_FLOAT:
5691 tstr = "float";
5692 goto add_tstr;
5693 case VT_DOUBLE:
5694 tstr = "double";
5695 goto add_tstr;
5696 case VT_LDOUBLE:
5697 tstr = "long double";
5698 add_tstr:
5699 pstrcat(buf, buf_size, tstr);
5700 break;
5701 case VT_ENUM:
5702 case VT_STRUCT:
5703 if (bt == VT_STRUCT)
5704 tstr = "struct ";
5705 else
5706 tstr = "enum ";
5707 pstrcat(buf, buf_size, tstr);
5708 v = type->ref->v & ~SYM_STRUCT;
5709 if (v >= SYM_FIRST_ANOM)
5710 pstrcat(buf, buf_size, "<anonymous>");
5711 else
5712 pstrcat(buf, buf_size, get_tok_str(v, NULL));
5713 break;
5714 case VT_FUNC:
5715 s = type->ref;
5716 type_to_str(buf, buf_size, &s->type, varstr);
5717 pstrcat(buf, buf_size, "(");
5718 sa = s->next;
5719 while (sa != NULL) {
5720 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
5721 pstrcat(buf, buf_size, buf1);
5722 sa = sa->next;
5723 if (sa)
5724 pstrcat(buf, buf_size, ", ");
5726 pstrcat(buf, buf_size, ")");
5727 goto no_var;
5728 case VT_PTR:
5729 s = type->ref;
5730 pstrcpy(buf1, sizeof(buf1), "*");
5731 if (varstr)
5732 pstrcat(buf1, sizeof(buf1), varstr);
5733 type_to_str(buf, buf_size, &s->type, buf1);
5734 goto no_var;
5736 if (varstr) {
5737 pstrcat(buf, buf_size, " ");
5738 pstrcat(buf, buf_size, varstr);
5740 no_var: ;
5743 /* verify type compatibility to store vtop in 'dt' type, and generate
5744 casts if needed. */
5745 static void gen_assign_cast(CType *dt)
5747 CType *st, *type1, *type2, tmp_type1, tmp_type2;
5748 char buf1[256], buf2[256];
5749 int dbt, sbt;
5751 st = &vtop->type; /* source type */
5752 dbt = dt->t & VT_BTYPE;
5753 sbt = st->t & VT_BTYPE;
5754 if (dt->t & VT_CONSTANT)
5755 warning("assignment of read-only location");
5756 switch(dbt) {
5757 case VT_PTR:
5758 /* special cases for pointers */
5759 /* '0' can also be a pointer */
5760 if (is_null_pointer(vtop))
5761 goto type_ok;
5762 /* accept implicit pointer to integer cast with warning */
5763 if (is_integer_btype(sbt)) {
5764 warning("assignment makes pointer from integer without a cast");
5765 goto type_ok;
5767 type1 = pointed_type(dt);
5768 /* a function is implicitely a function pointer */
5769 if (sbt == VT_FUNC) {
5770 if ((type1->t & VT_BTYPE) != VT_VOID &&
5771 !is_compatible_types(pointed_type(dt), st))
5772 goto error;
5773 else
5774 goto type_ok;
5776 if (sbt != VT_PTR)
5777 goto error;
5778 type2 = pointed_type(st);
5779 if ((type1->t & VT_BTYPE) == VT_VOID ||
5780 (type2->t & VT_BTYPE) == VT_VOID) {
5781 /* void * can match anything */
5782 } else {
5783 /* exact type match, except for unsigned */
5784 tmp_type1 = *type1;
5785 tmp_type2 = *type2;
5786 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5787 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5788 if (!is_compatible_types(&tmp_type1, &tmp_type2))
5789 goto error;
5791 /* check const and volatile */
5792 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
5793 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
5794 warning("assignment discards qualifiers from pointer target type");
5795 break;
5796 case VT_BYTE:
5797 case VT_SHORT:
5798 case VT_INT:
5799 case VT_LLONG:
5800 if (sbt == VT_PTR || sbt == VT_FUNC) {
5801 warning("assignment makes integer from pointer without a cast");
5803 /* XXX: more tests */
5804 break;
5805 case VT_STRUCT:
5806 if (!is_compatible_types(dt, st)) {
5807 error:
5808 type_to_str(buf1, sizeof(buf1), st, NULL);
5809 type_to_str(buf2, sizeof(buf2), dt, NULL);
5810 error("cannot cast '%s' to '%s'", buf1, buf2);
5812 break;
5814 type_ok:
5815 gen_cast(dt);
5818 /* store vtop in lvalue pushed on stack */
5819 void vstore(void)
5821 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5823 ft = vtop[-1].type.t;
5824 sbt = vtop->type.t & VT_BTYPE;
5825 dbt = ft & VT_BTYPE;
5826 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
5827 (sbt == VT_INT && dbt == VT_SHORT)) {
5828 /* optimize char/short casts */
5829 delayed_cast = VT_MUSTCAST;
5830 vtop->type.t = ft & VT_TYPE;
5831 /* XXX: factorize */
5832 if (ft & VT_CONSTANT)
5833 warning("assignment of read-only location");
5834 } else {
5835 delayed_cast = 0;
5836 gen_assign_cast(&vtop[-1].type);
5839 if (sbt == VT_STRUCT) {
5840 /* if structure, only generate pointer */
5841 /* structure assignment : generate memcpy */
5842 /* XXX: optimize if small size */
5843 if (!nocode_wanted) {
5844 size = type_size(&vtop->type, &align);
5846 vpush_global_sym(&func_old_type, TOK_memcpy);
5848 /* destination */
5849 vpushv(vtop - 2);
5850 vtop->type.t = VT_INT;
5851 gaddrof();
5852 /* source */
5853 vpushv(vtop - 2);
5854 vtop->type.t = VT_INT;
5855 gaddrof();
5856 /* type size */
5857 vpushi(size);
5858 gfunc_call(3);
5860 vswap();
5861 vpop();
5862 } else {
5863 vswap();
5864 vpop();
5866 /* leave source on stack */
5867 } else if (ft & VT_BITFIELD) {
5868 /* bitfield store handling */
5869 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
5870 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5871 /* remove bit field info to avoid loops */
5872 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
5874 /* duplicate destination */
5875 vdup();
5876 vtop[-1] = vtop[-2];
5878 /* mask and shift source */
5879 vpushi((1 << bit_size) - 1);
5880 gen_op('&');
5881 vpushi(bit_pos);
5882 gen_op(TOK_SHL);
5883 /* load destination, mask and or with source */
5884 vswap();
5885 vpushi(~(((1 << bit_size) - 1) << bit_pos));
5886 gen_op('&');
5887 gen_op('|');
5888 /* store result */
5889 vstore();
5890 } else {
5891 #ifdef CONFIG_TCC_BCHECK
5892 /* bound check case */
5893 if (vtop[-1].r & VT_MUSTBOUND) {
5894 vswap();
5895 gbound();
5896 vswap();
5898 #endif
5899 if (!nocode_wanted) {
5900 rc = RC_INT;
5901 if (is_float(ft))
5902 rc = RC_FLOAT;
5903 r = gv(rc); /* generate value */
5904 /* if lvalue was saved on stack, must read it */
5905 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
5906 SValue sv;
5907 t = get_reg(RC_INT);
5908 sv.type.t = VT_INT;
5909 sv.r = VT_LOCAL | VT_LVAL;
5910 sv.c.ul = vtop[-1].c.ul;
5911 load(t, &sv);
5912 vtop[-1].r = t | VT_LVAL;
5914 store(r, vtop - 1);
5915 /* two word case handling : store second register at word + 4 */
5916 if ((ft & VT_BTYPE) == VT_LLONG) {
5917 vswap();
5918 /* convert to int to increment easily */
5919 vtop->type.t = VT_INT;
5920 gaddrof();
5921 vpushi(4);
5922 gen_op('+');
5923 vtop->r |= VT_LVAL;
5924 vswap();
5925 /* XXX: it works because r2 is spilled last ! */
5926 store(vtop->r2, vtop - 1);
5929 vswap();
5930 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5931 vtop->r |= delayed_cast;
5935 /* post defines POST/PRE add. c is the token ++ or -- */
5936 void inc(int post, int c)
5938 test_lvalue();
5939 vdup(); /* save lvalue */
5940 if (post) {
5941 gv_dup(); /* duplicate value */
5942 vrotb(3);
5943 vrotb(3);
5945 /* add constant */
5946 vpushi(c - TOK_MID);
5947 gen_op('+');
5948 vstore(); /* store value */
5949 if (post)
5950 vpop(); /* if post op, return saved value */
5953 /* Parse GNUC __attribute__ extension. Currently, the following
5954 extensions are recognized:
5955 - aligned(n) : set data/function alignment.
5956 - section(x) : generate data/code in this section.
5957 - unused : currently ignored, but may be used someday.
5959 static void parse_attribute(AttributeDef *ad)
5961 int t, n;
5963 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
5964 next();
5965 skip('(');
5966 skip('(');
5967 while (tok != ')') {
5968 if (tok < TOK_IDENT)
5969 expect("attribute name");
5970 t = tok;
5971 next();
5972 switch(t) {
5973 case TOK_SECTION1:
5974 case TOK_SECTION2:
5975 skip('(');
5976 if (tok != TOK_STR)
5977 expect("section name");
5978 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
5979 next();
5980 skip(')');
5981 break;
5982 case TOK_ALIGNED1:
5983 case TOK_ALIGNED2:
5984 if (tok == '(') {
5985 next();
5986 n = expr_const();
5987 if (n <= 0 || (n & (n - 1)) != 0)
5988 error("alignment must be a positive power of two");
5989 skip(')');
5990 } else {
5991 n = MAX_ALIGN;
5993 ad->aligned = n;
5994 break;
5995 case TOK_UNUSED1:
5996 case TOK_UNUSED2:
5997 /* currently, no need to handle it because tcc does not
5998 track unused objects */
5999 break;
6000 case TOK_NORETURN1:
6001 case TOK_NORETURN2:
6002 /* currently, no need to handle it because tcc does not
6003 track unused objects */
6004 break;
6005 case TOK_CDECL1:
6006 case TOK_CDECL2:
6007 case TOK_CDECL3:
6008 ad->func_call = FUNC_CDECL;
6009 break;
6010 case TOK_STDCALL1:
6011 case TOK_STDCALL2:
6012 case TOK_STDCALL3:
6013 ad->func_call = FUNC_STDCALL;
6014 break;
6015 default:
6016 if (tcc_state->warn_unsupported)
6017 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6018 /* skip parameters */
6019 /* XXX: skip parenthesis too */
6020 if (tok == '(') {
6021 next();
6022 while (tok != ')' && tok != -1)
6023 next();
6024 next();
6026 break;
6028 if (tok != ',')
6029 break;
6030 next();
6032 skip(')');
6033 skip(')');
6037 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6038 static void struct_decl(CType *type, int u)
6040 int a, v, size, align, maxalign, c, offset;
6041 int bit_size, bit_pos, bsize, bt, lbit_pos;
6042 Sym *s, *ss, **ps;
6043 AttributeDef ad;
6044 CType type1, btype;
6046 a = tok; /* save decl type */
6047 next();
6048 if (tok != '{') {
6049 v = tok;
6050 next();
6051 /* struct already defined ? return it */
6052 if (v < TOK_IDENT)
6053 expect("struct/union/enum name");
6054 s = struct_find(v);
6055 if (s) {
6056 if (s->type.t != a)
6057 error("invalid type");
6058 goto do_decl;
6060 } else {
6061 v = anon_sym++;
6063 type1.t = a;
6064 /* we put an undefined size for struct/union */
6065 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6066 s->r = 0; /* default alignment is zero as gcc */
6067 /* put struct/union/enum name in type */
6068 do_decl:
6069 type->t = u;
6070 type->ref = s;
6072 if (tok == '{') {
6073 next();
6074 if (s->c != -1)
6075 error("struct/union/enum already defined");
6076 /* cannot be empty */
6077 c = 0;
6078 /* non empty enums are not allowed */
6079 if (a == TOK_ENUM) {
6080 for(;;) {
6081 v = tok;
6082 if (v < TOK_UIDENT)
6083 expect("identifier");
6084 next();
6085 if (tok == '=') {
6086 next();
6087 c = expr_const();
6089 /* enum symbols have static storage */
6090 ss = sym_push(v, &int_type, VT_CONST, c);
6091 ss->type.t |= VT_STATIC;
6092 if (tok != ',')
6093 break;
6094 next();
6095 c++;
6096 /* NOTE: we accept a trailing comma */
6097 if (tok == '}')
6098 break;
6100 skip('}');
6101 } else {
6102 maxalign = 1;
6103 ps = &s->next;
6104 bit_pos = 0;
6105 offset = 0;
6106 while (tok != '}') {
6107 parse_btype(&btype, &ad);
6108 while (1) {
6109 bit_size = -1;
6110 v = 0;
6111 type1 = btype;
6112 if (tok != ':') {
6113 type_decl(&type1, &ad, &v, TYPE_DIRECT);
6114 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6115 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6116 error("invalid type for '%s'",
6117 get_tok_str(v, NULL));
6119 if (tok == ':') {
6120 next();
6121 bit_size = expr_const();
6122 /* XXX: handle v = 0 case for messages */
6123 if (bit_size < 0)
6124 error("negative width in bit-field '%s'",
6125 get_tok_str(v, NULL));
6126 if (v && bit_size == 0)
6127 error("zero width for bit-field '%s'",
6128 get_tok_str(v, NULL));
6130 size = type_size(&type1, &align);
6131 lbit_pos = 0;
6132 if (bit_size >= 0) {
6133 bt = type1.t & VT_BTYPE;
6134 if (bt != VT_INT &&
6135 bt != VT_BYTE &&
6136 bt != VT_SHORT &&
6137 bt != VT_ENUM)
6138 error("bitfields must have scalar type");
6139 bsize = size * 8;
6140 if (bit_size > bsize) {
6141 error("width of '%s' exceeds its type",
6142 get_tok_str(v, NULL));
6143 } else if (bit_size == bsize) {
6144 /* no need for bit fields */
6145 bit_pos = 0;
6146 } else if (bit_size == 0) {
6147 /* XXX: what to do if only padding in a
6148 structure ? */
6149 /* zero size: means to pad */
6150 if (bit_pos > 0)
6151 bit_pos = bsize;
6152 } else {
6153 /* we do not have enough room ? */
6154 if ((bit_pos + bit_size) > bsize)
6155 bit_pos = 0;
6156 lbit_pos = bit_pos;
6157 /* XXX: handle LSB first */
6158 type1.t |= VT_BITFIELD |
6159 (bit_pos << VT_STRUCT_SHIFT) |
6160 (bit_size << (VT_STRUCT_SHIFT + 6));
6161 bit_pos += bit_size;
6163 } else {
6164 bit_pos = 0;
6166 if (v) {
6167 /* add new memory data only if starting
6168 bit field */
6169 if (lbit_pos == 0) {
6170 if (a == TOK_STRUCT) {
6171 c = (c + align - 1) & -align;
6172 offset = c;
6173 c += size;
6174 } else {
6175 offset = 0;
6176 if (size > c)
6177 c = size;
6179 if (align > maxalign)
6180 maxalign = align;
6182 #if 0
6183 printf("add field %s offset=%d",
6184 get_tok_str(v, NULL), offset);
6185 if (type1.t & VT_BITFIELD) {
6186 printf(" pos=%d size=%d",
6187 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6188 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6190 printf("\n");
6191 #endif
6192 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6193 *ps = ss;
6194 ps = &ss->next;
6196 if (tok == ';' || tok == TOK_EOF)
6197 break;
6198 skip(',');
6200 skip(';');
6202 skip('}');
6203 /* store size and alignment */
6204 s->c = (c + maxalign - 1) & -maxalign;
6205 s->r = maxalign;
6210 /* return 0 if no type declaration. otherwise, return the basic type
6211 and skip it.
6213 static int parse_btype(CType *type, AttributeDef *ad)
6215 int t, u, type_found, typespec_found;
6216 Sym *s;
6217 CType type1;
6219 memset(ad, 0, sizeof(AttributeDef));
6220 type_found = 0;
6221 typespec_found = 0;
6222 t = 0;
6223 while(1) {
6224 switch(tok) {
6225 case TOK_EXTENSION:
6226 /* currently, we really ignore extension */
6227 next();
6228 continue;
6230 /* basic types */
6231 case TOK_CHAR:
6232 u = VT_BYTE;
6233 basic_type:
6234 next();
6235 basic_type1:
6236 if ((t & VT_BTYPE) != 0)
6237 error("too many basic types");
6238 t |= u;
6239 typespec_found = 1;
6240 break;
6241 case TOK_VOID:
6242 u = VT_VOID;
6243 goto basic_type;
6244 case TOK_SHORT:
6245 u = VT_SHORT;
6246 goto basic_type;
6247 case TOK_INT:
6248 next();
6249 typespec_found = 1;
6250 break;
6251 case TOK_LONG:
6252 next();
6253 if ((t & VT_BTYPE) == VT_DOUBLE) {
6254 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6255 } else if ((t & VT_BTYPE) == VT_LONG) {
6256 t = (t & ~VT_BTYPE) | VT_LLONG;
6257 } else {
6258 u = VT_LONG;
6259 goto basic_type1;
6261 break;
6262 case TOK_BOOL:
6263 u = VT_BOOL;
6264 goto basic_type;
6265 case TOK_FLOAT:
6266 u = VT_FLOAT;
6267 goto basic_type;
6268 case TOK_DOUBLE:
6269 next();
6270 if ((t & VT_BTYPE) == VT_LONG) {
6271 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6272 } else {
6273 u = VT_DOUBLE;
6274 goto basic_type1;
6276 break;
6277 case TOK_ENUM:
6278 struct_decl(&type1, VT_ENUM);
6279 basic_type2:
6280 u = type1.t;
6281 type->ref = type1.ref;
6282 goto basic_type1;
6283 case TOK_STRUCT:
6284 case TOK_UNION:
6285 struct_decl(&type1, VT_STRUCT);
6286 goto basic_type2;
6288 /* type modifiers */
6289 case TOK_CONST1:
6290 case TOK_CONST2:
6291 case TOK_CONST3:
6292 t |= VT_CONSTANT;
6293 next();
6294 break;
6295 case TOK_VOLATILE1:
6296 case TOK_VOLATILE2:
6297 case TOK_VOLATILE3:
6298 t |= VT_VOLATILE;
6299 next();
6300 break;
6301 case TOK_SIGNED1:
6302 case TOK_SIGNED2:
6303 case TOK_SIGNED3:
6304 typespec_found = 1;
6305 case TOK_REGISTER:
6306 case TOK_AUTO:
6307 case TOK_RESTRICT1:
6308 case TOK_RESTRICT2:
6309 case TOK_RESTRICT3:
6310 next();
6311 break;
6312 case TOK_UNSIGNED:
6313 t |= VT_UNSIGNED;
6314 next();
6315 typespec_found = 1;
6316 break;
6318 /* storage */
6319 case TOK_EXTERN:
6320 t |= VT_EXTERN;
6321 next();
6322 break;
6323 case TOK_STATIC:
6324 t |= VT_STATIC;
6325 next();
6326 break;
6327 case TOK_TYPEDEF:
6328 t |= VT_TYPEDEF;
6329 next();
6330 break;
6331 case TOK_INLINE1:
6332 case TOK_INLINE2:
6333 case TOK_INLINE3:
6334 t |= VT_INLINE;
6335 next();
6336 break;
6338 /* GNUC attribute */
6339 case TOK_ATTRIBUTE1:
6340 case TOK_ATTRIBUTE2:
6341 parse_attribute(ad);
6342 break;
6343 /* GNUC typeof */
6344 case TOK_TYPEOF1:
6345 case TOK_TYPEOF2:
6346 case TOK_TYPEOF3:
6347 next();
6348 parse_expr_type(&type1);
6349 goto basic_type2;
6350 default:
6351 if (typespec_found)
6352 goto the_end;
6353 s = sym_find(tok);
6354 if (!s || !(s->type.t & VT_TYPEDEF))
6355 goto the_end;
6356 t |= (s->type.t & ~VT_TYPEDEF);
6357 type->ref = s->type.ref;
6358 next();
6359 break;
6361 type_found = 1;
6363 the_end:
6364 /* long is never used as type */
6365 if ((t & VT_BTYPE) == VT_LONG)
6366 t = (t & ~VT_BTYPE) | VT_INT;
6367 type->t = t;
6368 return type_found;
6371 /* convert a function parameter type (array to pointer and function to
6372 function pointer) */
6373 static inline void convert_parameter_type(CType *pt)
6375 /* array must be transformed to pointer according to ANSI C */
6376 pt->t &= ~VT_ARRAY;
6377 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6378 mk_pointer(pt);
6382 static void post_type(CType *type, AttributeDef *ad)
6384 int n, l, t1;
6385 Sym **plast, *s, *first;
6386 AttributeDef ad1;
6387 CType pt;
6389 if (tok == '(') {
6390 /* function declaration */
6391 next();
6392 l = 0;
6393 first = NULL;
6394 plast = &first;
6395 while (tok != ')') {
6396 /* read param name and compute offset */
6397 if (l != FUNC_OLD) {
6398 if (!parse_btype(&pt, &ad1)) {
6399 if (l) {
6400 error("invalid type");
6401 } else {
6402 l = FUNC_OLD;
6403 goto old_proto;
6406 l = FUNC_NEW;
6407 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6408 break;
6409 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6410 if ((pt.t & VT_BTYPE) == VT_VOID)
6411 error("parameter declared as void");
6412 } else {
6413 old_proto:
6414 n = tok;
6415 pt.t = VT_INT;
6416 next();
6418 convert_parameter_type(&pt);
6419 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6420 *plast = s;
6421 plast = &s->next;
6422 if (tok == ',') {
6423 next();
6424 if (l == FUNC_NEW && tok == TOK_DOTS) {
6425 l = FUNC_ELLIPSIS;
6426 next();
6427 break;
6431 /* if no parameters, then old type prototype */
6432 if (l == 0)
6433 l = FUNC_OLD;
6434 skip(')');
6435 t1 = type->t & VT_STORAGE;
6436 /* NOTE: const is ignored in returned type as it has a special
6437 meaning in gcc / C++ */
6438 type->t &= ~(VT_STORAGE | VT_CONSTANT);
6439 post_type(type, ad);
6440 /* we push a anonymous symbol which will contain the function prototype */
6441 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6442 s->next = first;
6443 type->t = t1 | VT_FUNC;
6444 type->ref = s;
6445 } else if (tok == '[') {
6446 /* array definition */
6447 next();
6448 n = -1;
6449 if (tok != ']') {
6450 n = expr_const();
6451 if (n < 0)
6452 error("invalid array size");
6454 skip(']');
6455 /* parse next post type */
6456 t1 = type->t & VT_STORAGE;
6457 type->t &= ~VT_STORAGE;
6458 post_type(type, ad);
6460 /* we push a anonymous symbol which will contain the array
6461 element type */
6462 s = sym_push(SYM_FIELD, type, 0, n);
6463 type->t = t1 | VT_ARRAY | VT_PTR;
6464 type->ref = s;
6468 /* Parse a type declaration (except basic type), and return the type
6469 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6470 expected. 'type' should contain the basic type. 'ad' is the
6471 attribute definition of the basic type. It can be modified by
6472 type_decl().
6474 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6476 Sym *s;
6477 CType type1, *type2;
6478 int qualifiers;
6480 while (tok == '*') {
6481 qualifiers = 0;
6482 redo:
6483 next();
6484 switch(tok) {
6485 case TOK_CONST1:
6486 case TOK_CONST2:
6487 case TOK_CONST3:
6488 qualifiers |= VT_CONSTANT;
6489 goto redo;
6490 case TOK_VOLATILE1:
6491 case TOK_VOLATILE2:
6492 case TOK_VOLATILE3:
6493 qualifiers |= VT_VOLATILE;
6494 goto redo;
6495 case TOK_RESTRICT1:
6496 case TOK_RESTRICT2:
6497 case TOK_RESTRICT3:
6498 goto redo;
6500 mk_pointer(type);
6501 type->t |= qualifiers;
6504 /* XXX: clarify attribute handling */
6505 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6506 parse_attribute(ad);
6508 /* recursive type */
6509 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6510 type1.t = 0; /* XXX: same as int */
6511 if (tok == '(') {
6512 next();
6513 /* XXX: this is not correct to modify 'ad' at this point, but
6514 the syntax is not clear */
6515 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6516 parse_attribute(ad);
6517 type_decl(&type1, ad, v, td);
6518 skip(')');
6519 } else {
6520 /* type identifier */
6521 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6522 *v = tok;
6523 next();
6524 } else {
6525 if (!(td & TYPE_ABSTRACT))
6526 expect("identifier");
6527 *v = 0;
6530 post_type(type, ad);
6531 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6532 parse_attribute(ad);
6533 if (!type1.t)
6534 return;
6535 /* append type at the end of type1 */
6536 type2 = &type1;
6537 for(;;) {
6538 s = type2->ref;
6539 type2 = &s->type;
6540 if (!type2->t) {
6541 *type2 = *type;
6542 break;
6545 *type = type1;
6548 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6549 static int lvalue_type(int t)
6551 int bt, r;
6552 r = VT_LVAL;
6553 bt = t & VT_BTYPE;
6554 if (bt == VT_BYTE || bt == VT_BOOL)
6555 r |= VT_LVAL_BYTE;
6556 else if (bt == VT_SHORT)
6557 r |= VT_LVAL_SHORT;
6558 else
6559 return r;
6560 if (t & VT_UNSIGNED)
6561 r |= VT_LVAL_UNSIGNED;
6562 return r;
6565 /* indirection with full error checking and bound check */
6566 static void indir(void)
6568 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6569 expect("pointer");
6570 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6571 gv(RC_INT);
6572 vtop->type = *pointed_type(&vtop->type);
6573 /* an array is never an lvalue */
6574 if (!(vtop->type.t & VT_ARRAY)) {
6575 vtop->r |= lvalue_type(vtop->type.t);
6576 /* if bound checking, the referenced pointer must be checked */
6577 if (do_bounds_check)
6578 vtop->r |= VT_MUSTBOUND;
6582 /* pass a parameter to a function and do type checking and casting */
6583 static void gfunc_param_typed(Sym *func, Sym *arg)
6585 int func_type;
6586 CType type;
6588 func_type = func->c;
6589 if (func_type == FUNC_OLD ||
6590 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6591 /* default casting : only need to convert float to double */
6592 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6593 type.t = VT_DOUBLE;
6594 gen_cast(&type);
6596 } else if (arg == NULL) {
6597 error("too many arguments to function");
6598 } else {
6599 type = arg->type;
6600 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
6601 gen_assign_cast(&type);
6605 /* parse an expression of the form '(type)' or '(expr)' and return its
6606 type */
6607 static void parse_expr_type(CType *type)
6609 int n;
6610 AttributeDef ad;
6612 skip('(');
6613 if (parse_btype(type, &ad)) {
6614 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6615 } else {
6616 expr_type(type);
6618 skip(')');
6621 static void parse_type(CType *type)
6623 AttributeDef ad;
6624 int n;
6626 if (!parse_btype(type, &ad)) {
6627 expect("type");
6629 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6632 static void vpush_tokc(int t)
6634 CType type;
6635 type.t = t;
6636 vsetc(&type, VT_CONST, &tokc);
6639 static void unary(void)
6641 int n, t, align, size, r;
6642 CType type;
6643 Sym *s;
6644 AttributeDef ad;
6646 /* XXX: GCC 2.95.3 does not generate a table although it should be
6647 better here */
6648 tok_next:
6649 switch(tok) {
6650 case TOK_EXTENSION:
6651 next();
6652 goto tok_next;
6653 case TOK_CINT:
6654 case TOK_CCHAR:
6655 case TOK_LCHAR:
6656 vpushi(tokc.i);
6657 next();
6658 break;
6659 case TOK_CUINT:
6660 vpush_tokc(VT_INT | VT_UNSIGNED);
6661 next();
6662 break;
6663 case TOK_CLLONG:
6664 vpush_tokc(VT_LLONG);
6665 next();
6666 break;
6667 case TOK_CULLONG:
6668 vpush_tokc(VT_LLONG | VT_UNSIGNED);
6669 next();
6670 break;
6671 case TOK_CFLOAT:
6672 vpush_tokc(VT_FLOAT);
6673 next();
6674 break;
6675 case TOK_CDOUBLE:
6676 vpush_tokc(VT_DOUBLE);
6677 next();
6678 break;
6679 case TOK_CLDOUBLE:
6680 vpush_tokc(VT_LDOUBLE);
6681 next();
6682 break;
6683 case TOK___FUNCTION__:
6684 if (!gnu_ext)
6685 goto tok_identifier;
6686 /* fall thru */
6687 case TOK___FUNC__:
6689 void *ptr;
6690 int len;
6691 /* special function name identifier */
6692 len = strlen(funcname) + 1;
6693 /* generate char[len] type */
6694 type.t = VT_BYTE;
6695 mk_pointer(&type);
6696 type.t |= VT_ARRAY;
6697 type.ref->c = len;
6698 vpush_ref(&type, data_section, data_section->data_offset, len);
6699 ptr = section_ptr_add(data_section, len);
6700 memcpy(ptr, funcname, len);
6701 next();
6703 break;
6704 case TOK_LSTR:
6705 t = VT_INT;
6706 goto str_init;
6707 case TOK_STR:
6708 /* string parsing */
6709 t = VT_BYTE;
6710 str_init:
6711 if (tcc_state->warn_write_strings)
6712 t |= VT_CONSTANT;
6713 type.t = t;
6714 mk_pointer(&type);
6715 type.t |= VT_ARRAY;
6716 memset(&ad, 0, sizeof(AttributeDef));
6717 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
6718 break;
6719 case '(':
6720 next();
6721 /* cast ? */
6722 if (parse_btype(&type, &ad)) {
6723 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
6724 skip(')');
6725 /* check ISOC99 compound literal */
6726 if (tok == '{') {
6727 /* data is allocated locally by default */
6728 if (global_expr)
6729 r = VT_CONST;
6730 else
6731 r = VT_LOCAL;
6732 /* all except arrays are lvalues */
6733 if (!(type.t & VT_ARRAY))
6734 r |= lvalue_type(type.t);
6735 memset(&ad, 0, sizeof(AttributeDef));
6736 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
6737 } else {
6738 unary();
6739 gen_cast(&type);
6741 } else if (tok == '{') {
6742 /* save all registers */
6743 save_regs(0);
6744 /* statement expression : we do not accept break/continue
6745 inside as GCC does */
6746 block(NULL, NULL, NULL, NULL, 0, 1);
6747 skip(')');
6748 } else {
6749 gexpr();
6750 skip(')');
6752 break;
6753 case '*':
6754 next();
6755 unary();
6756 indir();
6757 break;
6758 case '&':
6759 next();
6760 unary();
6761 /* functions names must be treated as function pointers,
6762 except for unary '&' and sizeof. Since we consider that
6763 functions are not lvalues, we only have to handle it
6764 there and in function calls. */
6765 /* arrays can also be used although they are not lvalues */
6766 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
6767 !(vtop->type.t & VT_ARRAY))
6768 test_lvalue();
6769 mk_pointer(&vtop->type);
6770 gaddrof();
6771 break;
6772 case '!':
6773 next();
6774 unary();
6775 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
6776 vtop->c.i = !vtop->c.i;
6777 else if ((vtop->r & VT_VALMASK) == VT_CMP)
6778 vtop->c.i = vtop->c.i ^ 1;
6779 else
6780 vseti(VT_JMP, gtst(1, 0));
6781 break;
6782 case '~':
6783 next();
6784 unary();
6785 vpushi(-1);
6786 gen_op('^');
6787 break;
6788 case '+':
6789 next();
6790 /* in order to force cast, we add zero */
6791 unary();
6792 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
6793 error("pointer not accepted for unary plus");
6794 vpushi(0);
6795 gen_op('+');
6796 break;
6797 case TOK_SIZEOF:
6798 case TOK_ALIGNOF1:
6799 case TOK_ALIGNOF2:
6800 t = tok;
6801 next();
6802 if (tok == '(') {
6803 parse_expr_type(&type);
6804 } else {
6805 unary_type(&type);
6807 size = type_size(&type, &align);
6808 if (t == TOK_SIZEOF) {
6809 if (size < 0)
6810 error("sizeof applied to an incomplete type");
6811 vpushi(size);
6812 } else {
6813 vpushi(align);
6815 break;
6817 case TOK_builtin_types_compatible_p:
6819 CType type1, type2;
6820 next();
6821 skip('(');
6822 parse_type(&type1);
6823 skip(',');
6824 parse_type(&type2);
6825 skip(')');
6826 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6827 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6828 vpushi(is_compatible_types(&type1, &type2));
6830 break;
6831 case TOK_builtin_constant_p:
6833 int saved_nocode_wanted, res;
6834 next();
6835 skip('(');
6836 saved_nocode_wanted = nocode_wanted;
6837 nocode_wanted = 1;
6838 gexpr();
6839 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
6840 vpop();
6841 nocode_wanted = saved_nocode_wanted;
6842 skip(')');
6843 vpushi(res);
6845 break;
6846 case TOK_INC:
6847 case TOK_DEC:
6848 t = tok;
6849 next();
6850 unary();
6851 inc(0, t);
6852 break;
6853 case '-':
6854 next();
6855 vpushi(0);
6856 unary();
6857 gen_op('-');
6858 break;
6859 case TOK_LAND:
6860 if (!gnu_ext)
6861 goto tok_identifier;
6862 next();
6863 /* allow to take the address of a label */
6864 if (tok < TOK_UIDENT)
6865 expect("label identifier");
6866 s = label_find(tok);
6867 if (!s) {
6868 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
6869 } else {
6870 if (s->r == LABEL_DECLARED)
6871 s->r = LABEL_FORWARD;
6873 if (!s->type.t) {
6874 s->type.t = VT_VOID;
6875 mk_pointer(&s->type);
6876 s->type.t |= VT_STATIC;
6878 vset(&s->type, VT_CONST | VT_SYM, 0);
6879 vtop->sym = s;
6880 next();
6881 break;
6882 default:
6883 tok_identifier:
6884 t = tok;
6885 next();
6886 if (t < TOK_UIDENT)
6887 expect("identifier");
6888 s = sym_find(t);
6889 if (!s) {
6890 if (tok != '(')
6891 error("'%s' undeclared", get_tok_str(t, NULL));
6892 /* for simple function calls, we tolerate undeclared
6893 external reference to int() function */
6894 s = external_global_sym(t, &func_old_type, 0);
6896 vset(&s->type, s->r, s->c);
6897 /* if forward reference, we must point to s */
6898 if (vtop->r & VT_SYM) {
6899 vtop->sym = s;
6900 vtop->c.ul = 0;
6902 break;
6905 /* post operations */
6906 while (1) {
6907 if (tok == TOK_INC || tok == TOK_DEC) {
6908 inc(1, tok);
6909 next();
6910 } else if (tok == '.' || tok == TOK_ARROW) {
6911 /* field */
6912 if (tok == TOK_ARROW)
6913 indir();
6914 test_lvalue();
6915 gaddrof();
6916 next();
6917 /* expect pointer on structure */
6918 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
6919 expect("struct or union");
6920 s = vtop->type.ref;
6921 /* find field */
6922 tok |= SYM_FIELD;
6923 while ((s = s->next) != NULL) {
6924 if (s->v == tok)
6925 break;
6927 if (!s)
6928 error("field not found");
6929 /* add field offset to pointer */
6930 vtop->type = char_pointer_type; /* change type to 'char *' */
6931 vpushi(s->c);
6932 gen_op('+');
6933 /* change type to field type, and set to lvalue */
6934 vtop->type = s->type;
6935 /* an array is never an lvalue */
6936 if (!(vtop->type.t & VT_ARRAY)) {
6937 vtop->r |= lvalue_type(vtop->type.t);
6938 /* if bound checking, the referenced pointer must be checked */
6939 if (do_bounds_check)
6940 vtop->r |= VT_MUSTBOUND;
6942 next();
6943 } else if (tok == '[') {
6944 next();
6945 gexpr();
6946 gen_op('+');
6947 indir();
6948 skip(']');
6949 } else if (tok == '(') {
6950 SValue ret;
6951 Sym *sa;
6952 int nb_args;
6954 /* function call */
6955 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
6956 /* pointer test (no array accepted) */
6957 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
6958 vtop->type = *pointed_type(&vtop->type);
6959 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
6960 goto error_func;
6961 } else {
6962 error_func:
6963 expect("function pointer");
6965 } else {
6966 vtop->r &= ~VT_LVAL; /* no lvalue */
6968 /* get return type */
6969 s = vtop->type.ref;
6970 next();
6971 sa = s->next; /* first parameter */
6972 nb_args = 0;
6973 /* compute first implicit argument if a structure is returned */
6974 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
6975 /* get some space for the returned structure */
6976 size = type_size(&s->type, &align);
6977 loc = (loc - size) & -align;
6978 ret.type = s->type;
6979 ret.r = VT_LOCAL | VT_LVAL;
6980 /* pass it as 'int' to avoid structure arg passing
6981 problems */
6982 vseti(VT_LOCAL, loc);
6983 ret.c = vtop->c;
6984 nb_args++;
6985 } else {
6986 ret.type = s->type;
6987 ret.r2 = VT_CONST;
6988 /* return in register */
6989 if (is_float(ret.type.t)) {
6990 ret.r = REG_FRET;
6991 } else {
6992 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
6993 ret.r2 = REG_LRET;
6994 ret.r = REG_IRET;
6996 ret.c.i = 0;
6998 if (tok != ')') {
6999 for(;;) {
7000 expr_eq();
7001 gfunc_param_typed(s, sa);
7002 nb_args++;
7003 if (sa)
7004 sa = sa->next;
7005 if (tok == ')')
7006 break;
7007 skip(',');
7010 if (sa)
7011 error("too few arguments to function");
7012 skip(')');
7013 if (!nocode_wanted) {
7014 gfunc_call(nb_args);
7015 } else {
7016 vtop -= (nb_args + 1);
7018 /* return value */
7019 vsetc(&ret.type, ret.r, &ret.c);
7020 vtop->r2 = ret.r2;
7021 } else {
7022 break;
7027 static void uneq(void)
7029 int t;
7031 unary();
7032 if (tok == '=' ||
7033 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7034 tok == TOK_A_XOR || tok == TOK_A_OR ||
7035 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7036 test_lvalue();
7037 t = tok;
7038 next();
7039 if (t == '=') {
7040 expr_eq();
7041 } else {
7042 vdup();
7043 expr_eq();
7044 gen_op(t & 0x7f);
7046 vstore();
7050 static void expr_prod(void)
7052 int t;
7054 uneq();
7055 while (tok == '*' || tok == '/' || tok == '%') {
7056 t = tok;
7057 next();
7058 uneq();
7059 gen_op(t);
7063 static void expr_sum(void)
7065 int t;
7067 expr_prod();
7068 while (tok == '+' || tok == '-') {
7069 t = tok;
7070 next();
7071 expr_prod();
7072 gen_op(t);
7076 static void expr_shift(void)
7078 int t;
7080 expr_sum();
7081 while (tok == TOK_SHL || tok == TOK_SAR) {
7082 t = tok;
7083 next();
7084 expr_sum();
7085 gen_op(t);
7089 static void expr_cmp(void)
7091 int t;
7093 expr_shift();
7094 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7095 tok == TOK_ULT || tok == TOK_UGE) {
7096 t = tok;
7097 next();
7098 expr_shift();
7099 gen_op(t);
7103 static void expr_cmpeq(void)
7105 int t;
7107 expr_cmp();
7108 while (tok == TOK_EQ || tok == TOK_NE) {
7109 t = tok;
7110 next();
7111 expr_cmp();
7112 gen_op(t);
7116 static void expr_and(void)
7118 expr_cmpeq();
7119 while (tok == '&') {
7120 next();
7121 expr_cmpeq();
7122 gen_op('&');
7126 static void expr_xor(void)
7128 expr_and();
7129 while (tok == '^') {
7130 next();
7131 expr_and();
7132 gen_op('^');
7136 static void expr_or(void)
7138 expr_xor();
7139 while (tok == '|') {
7140 next();
7141 expr_xor();
7142 gen_op('|');
7146 /* XXX: fix this mess */
7147 static void expr_land_const(void)
7149 expr_or();
7150 while (tok == TOK_LAND) {
7151 next();
7152 expr_or();
7153 gen_op(TOK_LAND);
7157 /* XXX: fix this mess */
7158 static void expr_lor_const(void)
7160 expr_land_const();
7161 while (tok == TOK_LOR) {
7162 next();
7163 expr_land_const();
7164 gen_op(TOK_LOR);
7168 /* only used if non constant */
7169 static void expr_land(void)
7171 int t;
7173 expr_or();
7174 if (tok == TOK_LAND) {
7175 t = 0;
7176 for(;;) {
7177 t = gtst(1, t);
7178 if (tok != TOK_LAND) {
7179 vseti(VT_JMPI, t);
7180 break;
7182 next();
7183 expr_or();
7188 static void expr_lor(void)
7190 int t;
7192 expr_land();
7193 if (tok == TOK_LOR) {
7194 t = 0;
7195 for(;;) {
7196 t = gtst(0, t);
7197 if (tok != TOK_LOR) {
7198 vseti(VT_JMP, t);
7199 break;
7201 next();
7202 expr_land();
7207 /* XXX: better constant handling */
7208 static void expr_eq(void)
7210 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7211 SValue sv;
7212 CType type, type1, type2;
7214 if (const_wanted) {
7215 int c1, c;
7216 expr_lor_const();
7217 if (tok == '?') {
7218 c = vtop->c.i;
7219 vpop();
7220 next();
7221 if (tok == ':' && gnu_ext) {
7222 c1 = c;
7223 } else {
7224 gexpr();
7225 c1 = vtop->c.i;
7226 vpop();
7228 skip(':');
7229 expr_eq();
7230 if (c)
7231 vtop->c.i = c1;
7233 } else {
7234 expr_lor();
7235 if (tok == '?') {
7236 next();
7237 if (vtop != vstack) {
7238 /* needed to avoid having different registers saved in
7239 each branch */
7240 if (is_float(vtop->type.t))
7241 rc = RC_FLOAT;
7242 else
7243 rc = RC_INT;
7244 gv(rc);
7245 save_regs(1);
7247 if (tok == ':' && gnu_ext) {
7248 gv_dup();
7249 tt = gtst(1, 0);
7250 } else {
7251 tt = gtst(1, 0);
7252 gexpr();
7254 type1 = vtop->type;
7255 sv = *vtop; /* save value to handle it later */
7256 vtop--; /* no vpop so that FP stack is not flushed */
7257 skip(':');
7258 u = gjmp(0);
7259 gsym(tt);
7260 expr_eq();
7261 type2 = vtop->type;
7263 t1 = type1.t;
7264 bt1 = t1 & VT_BTYPE;
7265 t2 = type2.t;
7266 bt2 = t2 & VT_BTYPE;
7267 /* cast operands to correct type according to ISOC rules */
7268 if (is_float(bt1) || is_float(bt2)) {
7269 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7270 type.t = VT_LDOUBLE;
7271 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7272 type.t = VT_DOUBLE;
7273 } else {
7274 type.t = VT_FLOAT;
7276 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7277 /* cast to biggest op */
7278 type.t = VT_LLONG;
7279 /* convert to unsigned if it does not fit in a long long */
7280 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7281 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7282 type.t |= VT_UNSIGNED;
7283 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7284 /* XXX: test pointer compatibility */
7285 type = type1;
7286 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7287 /* XXX: test structure compatibility */
7288 type = type1;
7289 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7290 /* NOTE: as an extension, we accept void on only one side */
7291 type.t = VT_VOID;
7292 } else {
7293 /* integer operations */
7294 type.t = VT_INT;
7295 /* convert to unsigned if it does not fit in an integer */
7296 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7297 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7298 type.t |= VT_UNSIGNED;
7301 /* now we convert second operand */
7302 gen_cast(&type);
7303 rc = RC_INT;
7304 if (is_float(type.t)) {
7305 rc = RC_FLOAT;
7306 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7307 /* for long longs, we use fixed registers to avoid having
7308 to handle a complicated move */
7309 rc = RC_IRET;
7312 r2 = gv(rc);
7313 /* this is horrible, but we must also convert first
7314 operand */
7315 tt = gjmp(0);
7316 gsym(u);
7317 /* put again first value and cast it */
7318 *vtop = sv;
7319 gen_cast(&type);
7320 r1 = gv(rc);
7321 move_reg(r2, r1);
7322 vtop->r = r2;
7323 gsym(tt);
7328 static void gexpr(void)
7330 while (1) {
7331 expr_eq();
7332 if (tok != ',')
7333 break;
7334 vpop();
7335 next();
7339 /* parse an expression and return its type without any side effect. */
7340 static void expr_type(CType *type)
7342 int saved_nocode_wanted;
7344 saved_nocode_wanted = nocode_wanted;
7345 nocode_wanted = 1;
7346 gexpr();
7347 *type = vtop->type;
7348 vpop();
7349 nocode_wanted = saved_nocode_wanted;
7352 /* parse a unary expression and return its type without any side
7353 effect. */
7354 static void unary_type(CType *type)
7356 int a;
7358 a = nocode_wanted;
7359 nocode_wanted = 1;
7360 unary();
7361 *type = vtop->type;
7362 vpop();
7363 nocode_wanted = a;
7366 /* parse a constant expression and return value in vtop. */
7367 static void expr_const1(void)
7369 int a;
7370 a = const_wanted;
7371 const_wanted = 1;
7372 expr_eq();
7373 const_wanted = a;
7376 /* parse an integer constant and return its value. */
7377 static int expr_const(void)
7379 int c;
7380 expr_const1();
7381 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7382 expect("constant expression");
7383 c = vtop->c.i;
7384 vpop();
7385 return c;
7388 /* return the label token if current token is a label, otherwise
7389 return zero */
7390 static int is_label(void)
7392 int last_tok;
7394 /* fast test first */
7395 if (tok < TOK_UIDENT)
7396 return 0;
7397 /* no need to save tokc because tok is an identifier */
7398 last_tok = tok;
7399 next();
7400 if (tok == ':') {
7401 next();
7402 return last_tok;
7403 } else {
7404 unget_tok(last_tok);
7405 return 0;
7409 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7410 int case_reg, int is_expr)
7412 int a, b, c, d;
7413 Sym *s;
7415 /* generate line number info */
7416 if (do_debug &&
7417 (last_line_num != file->line_num || last_ind != ind)) {
7418 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7419 last_ind = ind;
7420 last_line_num = file->line_num;
7423 if (is_expr) {
7424 /* default return value is (void) */
7425 vpushi(0);
7426 vtop->type.t = VT_VOID;
7429 if (tok == TOK_IF) {
7430 /* if test */
7431 next();
7432 skip('(');
7433 gexpr();
7434 skip(')');
7435 a = gtst(1, 0);
7436 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7437 c = tok;
7438 if (c == TOK_ELSE) {
7439 next();
7440 d = gjmp(0);
7441 gsym(a);
7442 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7443 gsym(d); /* patch else jmp */
7444 } else
7445 gsym(a);
7446 } else if (tok == TOK_WHILE) {
7447 next();
7448 d = ind;
7449 skip('(');
7450 gexpr();
7451 skip(')');
7452 a = gtst(1, 0);
7453 b = 0;
7454 block(&a, &b, case_sym, def_sym, case_reg, 0);
7455 gjmp_addr(d);
7456 gsym(a);
7457 gsym_addr(b, d);
7458 } else if (tok == '{') {
7459 Sym *llabel;
7461 next();
7462 /* record local declaration stack position */
7463 s = local_stack;
7464 llabel = local_label_stack;
7465 /* handle local labels declarations */
7466 if (tok == TOK_LABEL) {
7467 next();
7468 for(;;) {
7469 if (tok < TOK_UIDENT)
7470 expect("label identifier");
7471 label_push(&local_label_stack, tok, LABEL_DECLARED);
7472 next();
7473 if (tok == ',') {
7474 next();
7475 } else {
7476 skip(';');
7477 break;
7481 while (tok != '}') {
7482 decl(VT_LOCAL);
7483 if (tok != '}') {
7484 if (is_expr)
7485 vpop();
7486 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7489 /* pop locally defined labels */
7490 label_pop(&local_label_stack, llabel);
7491 /* pop locally defined symbols */
7492 sym_pop(&local_stack, s);
7493 next();
7494 } else if (tok == TOK_RETURN) {
7495 next();
7496 if (tok != ';') {
7497 gexpr();
7498 gen_assign_cast(&func_vt);
7499 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7500 CType type;
7501 /* if returning structure, must copy it to implicit
7502 first pointer arg location */
7503 type = func_vt;
7504 mk_pointer(&type);
7505 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7506 indir();
7507 vswap();
7508 /* copy structure value to pointer */
7509 vstore();
7510 } else if (is_float(func_vt.t)) {
7511 gv(RC_FRET);
7512 } else {
7513 gv(RC_IRET);
7515 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7517 skip(';');
7518 rsym = gjmp(rsym); /* jmp */
7519 } else if (tok == TOK_BREAK) {
7520 /* compute jump */
7521 if (!bsym)
7522 error("cannot break");
7523 *bsym = gjmp(*bsym);
7524 next();
7525 skip(';');
7526 } else if (tok == TOK_CONTINUE) {
7527 /* compute jump */
7528 if (!csym)
7529 error("cannot continue");
7530 *csym = gjmp(*csym);
7531 next();
7532 skip(';');
7533 } else if (tok == TOK_FOR) {
7534 int e;
7535 next();
7536 skip('(');
7537 if (tok != ';') {
7538 gexpr();
7539 vpop();
7541 skip(';');
7542 d = ind;
7543 c = ind;
7544 a = 0;
7545 b = 0;
7546 if (tok != ';') {
7547 gexpr();
7548 a = gtst(1, 0);
7550 skip(';');
7551 if (tok != ')') {
7552 e = gjmp(0);
7553 c = ind;
7554 gexpr();
7555 vpop();
7556 gjmp_addr(d);
7557 gsym(e);
7559 skip(')');
7560 block(&a, &b, case_sym, def_sym, case_reg, 0);
7561 gjmp_addr(c);
7562 gsym(a);
7563 gsym_addr(b, c);
7564 } else
7565 if (tok == TOK_DO) {
7566 next();
7567 a = 0;
7568 b = 0;
7569 d = ind;
7570 block(&a, &b, case_sym, def_sym, case_reg, 0);
7571 skip(TOK_WHILE);
7572 skip('(');
7573 gsym(b);
7574 gexpr();
7575 c = gtst(0, 0);
7576 gsym_addr(c, d);
7577 skip(')');
7578 gsym(a);
7579 skip(';');
7580 } else
7581 if (tok == TOK_SWITCH) {
7582 next();
7583 skip('(');
7584 gexpr();
7585 /* XXX: other types than integer */
7586 case_reg = gv(RC_INT);
7587 vpop();
7588 skip(')');
7589 a = 0;
7590 b = gjmp(0); /* jump to first case */
7591 c = 0;
7592 block(&a, csym, &b, &c, case_reg, 0);
7593 /* if no default, jmp after switch */
7594 if (c == 0)
7595 c = ind;
7596 /* default label */
7597 gsym_addr(b, c);
7598 /* break label */
7599 gsym(a);
7600 } else
7601 if (tok == TOK_CASE) {
7602 int v1, v2;
7603 if (!case_sym)
7604 expect("switch");
7605 next();
7606 v1 = expr_const();
7607 v2 = v1;
7608 if (gnu_ext && tok == TOK_DOTS) {
7609 next();
7610 v2 = expr_const();
7611 if (v2 < v1)
7612 warning("empty case range");
7614 /* since a case is like a label, we must skip it with a jmp */
7615 b = gjmp(0);
7616 gsym(*case_sym);
7617 vseti(case_reg, 0);
7618 vpushi(v1);
7619 if (v1 == v2) {
7620 gen_op(TOK_EQ);
7621 *case_sym = gtst(1, 0);
7622 } else {
7623 gen_op(TOK_GE);
7624 *case_sym = gtst(1, 0);
7625 vseti(case_reg, 0);
7626 vpushi(v2);
7627 gen_op(TOK_LE);
7628 *case_sym = gtst(1, *case_sym);
7630 gsym(b);
7631 skip(':');
7632 is_expr = 0;
7633 goto block_after_label;
7634 } else
7635 if (tok == TOK_DEFAULT) {
7636 next();
7637 skip(':');
7638 if (!def_sym)
7639 expect("switch");
7640 if (*def_sym)
7641 error("too many 'default'");
7642 *def_sym = ind;
7643 is_expr = 0;
7644 goto block_after_label;
7645 } else
7646 if (tok == TOK_GOTO) {
7647 next();
7648 if (tok == '*' && gnu_ext) {
7649 /* computed goto */
7650 next();
7651 gexpr();
7652 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7653 expect("pointer");
7654 ggoto();
7655 } else if (tok >= TOK_UIDENT) {
7656 s = label_find(tok);
7657 /* put forward definition if needed */
7658 if (!s) {
7659 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7660 } else {
7661 if (s->r == LABEL_DECLARED)
7662 s->r = LABEL_FORWARD;
7664 /* label already defined */
7665 if (s->r & LABEL_FORWARD)
7666 s->next = (void *)gjmp((long)s->next);
7667 else
7668 gjmp_addr((long)s->next);
7669 next();
7670 } else {
7671 expect("label identifier");
7673 skip(';');
7674 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7675 asm_instr();
7676 } else {
7677 b = is_label();
7678 if (b) {
7679 /* label case */
7680 s = label_find(b);
7681 if (s) {
7682 if (s->r == LABEL_DEFINED)
7683 error("duplicate label '%s'", get_tok_str(s->v, NULL));
7684 gsym((long)s->next);
7685 s->r = LABEL_DEFINED;
7686 } else {
7687 s = label_push(&global_label_stack, b, LABEL_DEFINED);
7689 s->next = (void *)ind;
7690 /* we accept this, but it is a mistake */
7691 block_after_label:
7692 if (tok == '}') {
7693 warning("deprecated use of label at end of compound statement");
7694 } else {
7695 if (is_expr)
7696 vpop();
7697 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7699 } else {
7700 /* expression case */
7701 if (tok != ';') {
7702 if (is_expr) {
7703 vpop();
7704 gexpr();
7705 } else {
7706 gexpr();
7707 vpop();
7710 skip(';');
7715 /* t is the array or struct type. c is the array or struct
7716 address. cur_index/cur_field is the pointer to the current
7717 value. 'size_only' is true if only size info is needed (only used
7718 in arrays) */
7719 static void decl_designator(CType *type, Section *sec, unsigned long c,
7720 int *cur_index, Sym **cur_field,
7721 int size_only)
7723 Sym *s, *f;
7724 int notfirst, index, index_last, align, l, nb_elems, elem_size;
7725 CType type1;
7727 notfirst = 0;
7728 elem_size = 0;
7729 nb_elems = 1;
7730 if (gnu_ext && (l = is_label()) != 0)
7731 goto struct_field;
7732 while (tok == '[' || tok == '.') {
7733 if (tok == '[') {
7734 if (!(type->t & VT_ARRAY))
7735 expect("array type");
7736 s = type->ref;
7737 next();
7738 index = expr_const();
7739 if (index < 0 || (s->c >= 0 && index >= s->c))
7740 expect("invalid index");
7741 if (tok == TOK_DOTS && gnu_ext) {
7742 next();
7743 index_last = expr_const();
7744 if (index_last < 0 ||
7745 (s->c >= 0 && index_last >= s->c) ||
7746 index_last < index)
7747 expect("invalid index");
7748 } else {
7749 index_last = index;
7751 skip(']');
7752 if (!notfirst)
7753 *cur_index = index_last;
7754 type = pointed_type(type);
7755 elem_size = type_size(type, &align);
7756 c += index * elem_size;
7757 /* NOTE: we only support ranges for last designator */
7758 nb_elems = index_last - index + 1;
7759 if (nb_elems != 1) {
7760 notfirst = 1;
7761 break;
7763 } else {
7764 next();
7765 l = tok;
7766 next();
7767 struct_field:
7768 if ((type->t & VT_BTYPE) != VT_STRUCT)
7769 expect("struct/union type");
7770 s = type->ref;
7771 l |= SYM_FIELD;
7772 f = s->next;
7773 while (f) {
7774 if (f->v == l)
7775 break;
7776 f = f->next;
7778 if (!f)
7779 expect("field");
7780 if (!notfirst)
7781 *cur_field = f;
7782 /* XXX: fix this mess by using explicit storage field */
7783 type1 = f->type;
7784 type1.t |= (type->t & ~VT_TYPE);
7785 type = &type1;
7786 c += f->c;
7788 notfirst = 1;
7790 if (notfirst) {
7791 if (tok == '=') {
7792 next();
7793 } else {
7794 if (!gnu_ext)
7795 expect("=");
7797 } else {
7798 if (type->t & VT_ARRAY) {
7799 index = *cur_index;
7800 type = pointed_type(type);
7801 c += index * type_size(type, &align);
7802 } else {
7803 f = *cur_field;
7804 if (!f)
7805 error("too many field init");
7806 /* XXX: fix this mess by using explicit storage field */
7807 type1 = f->type;
7808 type1.t |= (type->t & ~VT_TYPE);
7809 type = &type1;
7810 c += f->c;
7813 decl_initializer(type, sec, c, 0, size_only);
7815 /* XXX: make it more general */
7816 if (!size_only && nb_elems > 1) {
7817 unsigned long c_end;
7818 uint8_t *src, *dst;
7819 int i;
7821 if (!sec)
7822 error("range init not supported yet for dynamic storage");
7823 c_end = c + nb_elems * elem_size;
7824 if (c_end > sec->data_allocated)
7825 section_realloc(sec, c_end);
7826 src = sec->data + c;
7827 dst = src;
7828 for(i = 1; i < nb_elems; i++) {
7829 dst += elem_size;
7830 memcpy(dst, src, elem_size);
7835 #define EXPR_VAL 0
7836 #define EXPR_CONST 1
7837 #define EXPR_ANY 2
7839 /* store a value or an expression directly in global data or in local array */
7840 static void init_putv(CType *type, Section *sec, unsigned long c,
7841 int v, int expr_type)
7843 int saved_global_expr, bt, bit_pos, bit_size;
7844 void *ptr;
7845 unsigned long long bit_mask;
7846 CType dtype;
7848 switch(expr_type) {
7849 case EXPR_VAL:
7850 vpushi(v);
7851 break;
7852 case EXPR_CONST:
7853 /* compound literals must be allocated globally in this case */
7854 saved_global_expr = global_expr;
7855 global_expr = 1;
7856 expr_const1();
7857 global_expr = saved_global_expr;
7858 /* NOTE: symbols are accepted */
7859 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
7860 error("initializer element is not constant");
7861 break;
7862 case EXPR_ANY:
7863 expr_eq();
7864 break;
7867 dtype = *type;
7868 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7870 if (sec) {
7871 /* XXX: not portable */
7872 /* XXX: generate error if incorrect relocation */
7873 gen_assign_cast(&dtype);
7874 bt = type->t & VT_BTYPE;
7875 ptr = sec->data + c;
7876 /* XXX: make code faster ? */
7877 if (!(type->t & VT_BITFIELD)) {
7878 bit_pos = 0;
7879 bit_size = 32;
7880 bit_mask = -1LL;
7881 } else {
7882 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
7883 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
7884 bit_mask = (1LL << bit_size) - 1;
7886 if ((vtop->r & VT_SYM) &&
7887 (bt == VT_BYTE ||
7888 bt == VT_SHORT ||
7889 bt == VT_DOUBLE ||
7890 bt == VT_LDOUBLE ||
7891 bt == VT_LLONG ||
7892 (bt == VT_INT && bit_size != 32)))
7893 error("initializer element is not computable at load time");
7894 switch(bt) {
7895 case VT_BYTE:
7896 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7897 break;
7898 case VT_SHORT:
7899 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7900 break;
7901 case VT_DOUBLE:
7902 *(double *)ptr = vtop->c.d;
7903 break;
7904 case VT_LDOUBLE:
7905 *(long double *)ptr = vtop->c.ld;
7906 break;
7907 case VT_LLONG:
7908 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
7909 break;
7910 default:
7911 if (vtop->r & VT_SYM) {
7912 greloc(sec, vtop->sym, c, R_DATA_32);
7914 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7915 break;
7917 vtop--;
7918 } else {
7919 vset(&dtype, VT_LOCAL, c);
7920 vswap();
7921 vstore();
7922 vpop();
7926 /* put zeros for variable based init */
7927 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
7929 if (sec) {
7930 /* nothing to do because globals are already set to zero */
7931 } else {
7932 vpush_global_sym(&func_old_type, TOK_memset);
7933 vseti(VT_LOCAL, c);
7934 vpushi(0);
7935 vpushi(size);
7936 gfunc_call(3);
7940 /* 't' contains the type and storage info. 'c' is the offset of the
7941 object in section 'sec'. If 'sec' is NULL, it means stack based
7942 allocation. 'first' is true if array '{' must be read (multi
7943 dimension implicit array init handling). 'size_only' is true if
7944 size only evaluation is wanted (only for arrays). */
7945 static void decl_initializer(CType *type, Section *sec, unsigned long c,
7946 int first, int size_only)
7948 int index, array_length, n, no_oblock, nb, parlevel, i;
7949 int size1, align1, expr_type;
7950 Sym *s, *f;
7951 CType *t1;
7953 if (type->t & VT_ARRAY) {
7954 s = type->ref;
7955 n = s->c;
7956 array_length = 0;
7957 t1 = pointed_type(type);
7958 size1 = type_size(t1, &align1);
7960 no_oblock = 1;
7961 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
7962 tok == '{') {
7963 skip('{');
7964 no_oblock = 0;
7967 /* only parse strings here if correct type (otherwise: handle
7968 them as ((w)char *) expressions */
7969 if ((tok == TOK_LSTR &&
7970 (t1->t & VT_BTYPE) == VT_INT) ||
7971 (tok == TOK_STR &&
7972 (t1->t & VT_BTYPE) == VT_BYTE)) {
7973 while (tok == TOK_STR || tok == TOK_LSTR) {
7974 int cstr_len, ch;
7975 CString *cstr;
7977 cstr = tokc.cstr;
7978 /* compute maximum number of chars wanted */
7979 if (tok == TOK_STR)
7980 cstr_len = cstr->size;
7981 else
7982 cstr_len = cstr->size / sizeof(int);
7983 cstr_len--;
7984 nb = cstr_len;
7985 if (n >= 0 && nb > (n - array_length))
7986 nb = n - array_length;
7987 if (!size_only) {
7988 if (cstr_len > nb)
7989 warning("initializer-string for array is too long");
7990 /* in order to go faster for common case (char
7991 string in global variable, we handle it
7992 specifically */
7993 if (sec && tok == TOK_STR && size1 == 1) {
7994 memcpy(sec->data + c + array_length, cstr->data, nb);
7995 } else {
7996 for(i=0;i<nb;i++) {
7997 if (tok == TOK_STR)
7998 ch = ((unsigned char *)cstr->data)[i];
7999 else
8000 ch = ((int *)cstr->data)[i];
8001 init_putv(t1, sec, c + (array_length + i) * size1,
8002 ch, EXPR_VAL);
8006 array_length += nb;
8007 next();
8009 /* only add trailing zero if enough storage (no
8010 warning in this case since it is standard) */
8011 if (n < 0 || array_length < n) {
8012 if (!size_only) {
8013 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8015 array_length++;
8017 } else {
8018 index = 0;
8019 while (tok != '}') {
8020 decl_designator(type, sec, c, &index, NULL, size_only);
8021 if (n >= 0 && index >= n)
8022 error("index too large");
8023 /* must put zero in holes (note that doing it that way
8024 ensures that it even works with designators) */
8025 if (!size_only && array_length < index) {
8026 init_putz(t1, sec, c + array_length * size1,
8027 (index - array_length) * size1);
8029 index++;
8030 if (index > array_length)
8031 array_length = index;
8032 /* special test for multi dimensional arrays (may not
8033 be strictly correct if designators are used at the
8034 same time) */
8035 if (index >= n && no_oblock)
8036 break;
8037 if (tok == '}')
8038 break;
8039 skip(',');
8042 if (!no_oblock)
8043 skip('}');
8044 /* put zeros at the end */
8045 if (!size_only && n >= 0 && array_length < n) {
8046 init_putz(t1, sec, c + array_length * size1,
8047 (n - array_length) * size1);
8049 /* patch type size if needed */
8050 if (n < 0)
8051 s->c = array_length;
8052 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8053 (sec || !first || tok == '{')) {
8054 int par_count;
8056 /* NOTE: the previous test is a specific case for automatic
8057 struct/union init */
8058 /* XXX: union needs only one init */
8060 /* XXX: this test is incorrect for local initializers
8061 beginning with ( without {. It would be much more difficult
8062 to do it correctly (ideally, the expression parser should
8063 be used in all cases) */
8064 par_count = 0;
8065 if (tok == '(') {
8066 AttributeDef ad1;
8067 CType type1;
8068 next();
8069 while (tok == '(') {
8070 par_count++;
8071 next();
8073 if (!parse_btype(&type1, &ad1))
8074 expect("cast");
8075 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8076 #if 0
8077 if (!is_assignable_types(type, &type1))
8078 error("invalid type for cast");
8079 #endif
8080 skip(')');
8082 no_oblock = 1;
8083 if (first || tok == '{') {
8084 skip('{');
8085 no_oblock = 0;
8087 s = type->ref;
8088 f = s->next;
8089 array_length = 0;
8090 index = 0;
8091 n = s->c;
8092 while (tok != '}') {
8093 decl_designator(type, sec, c, NULL, &f, size_only);
8094 index = f->c;
8095 if (!size_only && array_length < index) {
8096 init_putz(type, sec, c + array_length,
8097 index - array_length);
8099 index = index + type_size(&f->type, &align1);
8100 if (index > array_length)
8101 array_length = index;
8102 f = f->next;
8103 if (no_oblock && f == NULL)
8104 break;
8105 if (tok == '}')
8106 break;
8107 skip(',');
8109 /* put zeros at the end */
8110 if (!size_only && array_length < n) {
8111 init_putz(type, sec, c + array_length,
8112 n - array_length);
8114 if (!no_oblock)
8115 skip('}');
8116 while (par_count) {
8117 skip(')');
8118 par_count--;
8120 } else if (tok == '{') {
8121 next();
8122 decl_initializer(type, sec, c, first, size_only);
8123 skip('}');
8124 } else if (size_only) {
8125 /* just skip expression */
8126 parlevel = 0;
8127 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8128 tok != -1) {
8129 if (tok == '(')
8130 parlevel++;
8131 else if (tok == ')')
8132 parlevel--;
8133 next();
8135 } else {
8136 /* currently, we always use constant expression for globals
8137 (may change for scripting case) */
8138 expr_type = EXPR_CONST;
8139 if (!sec)
8140 expr_type = EXPR_ANY;
8141 init_putv(type, sec, c, 0, expr_type);
8145 /* parse an initializer for type 't' if 'has_init' is non zero, and
8146 allocate space in local or global data space ('r' is either
8147 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8148 variable 'v' of scope 'scope' is declared before initializers are
8149 parsed. If 'v' is zero, then a reference to the new object is put
8150 in the value stack. If 'has_init' is 2, a special parsing is done
8151 to handle string constants. */
8152 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8153 int has_init, int v, int scope)
8155 int size, align, addr, data_offset;
8156 int level;
8157 ParseState saved_parse_state;
8158 TokenString init_str;
8159 Section *sec;
8161 size = type_size(type, &align);
8162 /* If unknown size, we must evaluate it before
8163 evaluating initializers because
8164 initializers can generate global data too
8165 (e.g. string pointers or ISOC99 compound
8166 literals). It also simplifies local
8167 initializers handling */
8168 tok_str_new(&init_str);
8169 if (size < 0) {
8170 if (!has_init)
8171 error("unknown type size");
8172 /* get all init string */
8173 if (has_init == 2) {
8174 /* only get strings */
8175 while (tok == TOK_STR || tok == TOK_LSTR) {
8176 tok_str_add_tok(&init_str);
8177 next();
8179 } else {
8180 level = 0;
8181 while (level > 0 || (tok != ',' && tok != ';')) {
8182 if (tok < 0)
8183 error("unexpected end of file in initializer");
8184 tok_str_add_tok(&init_str);
8185 if (tok == '{')
8186 level++;
8187 else if (tok == '}') {
8188 if (level == 0)
8189 break;
8190 level--;
8192 next();
8195 tok_str_add(&init_str, -1);
8196 tok_str_add(&init_str, 0);
8198 /* compute size */
8199 save_parse_state(&saved_parse_state);
8201 macro_ptr = init_str.str;
8202 next();
8203 decl_initializer(type, NULL, 0, 1, 1);
8204 /* prepare second initializer parsing */
8205 macro_ptr = init_str.str;
8206 next();
8208 /* if still unknown size, error */
8209 size = type_size(type, &align);
8210 if (size < 0)
8211 error("unknown type size");
8213 /* take into account specified alignment if bigger */
8214 if (ad->aligned > align)
8215 align = ad->aligned;
8216 if ((r & VT_VALMASK) == VT_LOCAL) {
8217 sec = NULL;
8218 if (do_bounds_check && (type->t & VT_ARRAY))
8219 loc--;
8220 loc = (loc - size) & -align;
8221 addr = loc;
8222 /* handles bounds */
8223 /* XXX: currently, since we do only one pass, we cannot track
8224 '&' operators, so we add only arrays */
8225 if (do_bounds_check && (type->t & VT_ARRAY)) {
8226 unsigned long *bounds_ptr;
8227 /* add padding between regions */
8228 loc--;
8229 /* then add local bound info */
8230 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8231 bounds_ptr[0] = addr;
8232 bounds_ptr[1] = size;
8234 if (v) {
8235 /* local variable */
8236 sym_push(v, type, r, addr);
8237 } else {
8238 /* push local reference */
8239 vset(type, r, addr);
8241 } else {
8242 Sym *sym;
8244 sym = NULL;
8245 if (v && scope == VT_CONST) {
8246 /* see if the symbol was already defined */
8247 sym = sym_find(v);
8248 if (sym) {
8249 if (!is_compatible_types(&sym->type, type))
8250 error("incompatible types for redefinition of '%s'",
8251 get_tok_str(v, NULL));
8252 if (sym->type.t & VT_EXTERN) {
8253 /* if the variable is extern, it was not allocated */
8254 sym->type.t &= ~VT_EXTERN;
8255 } else {
8256 /* we accept several definitions of the same
8257 global variable. this is tricky, because we
8258 must play with the SHN_COMMON type of the symbol */
8259 /* XXX: should check if the variable was already
8260 initialized. It is incorrect to initialized it
8261 twice */
8262 /* no init data, we won't add more to the symbol */
8263 if (!has_init)
8264 goto no_alloc;
8269 /* allocate symbol in corresponding section */
8270 sec = ad->section;
8271 if (!sec) {
8272 if (has_init)
8273 sec = data_section;
8275 if (sec) {
8276 data_offset = sec->data_offset;
8277 data_offset = (data_offset + align - 1) & -align;
8278 addr = data_offset;
8279 /* very important to increment global pointer at this time
8280 because initializers themselves can create new initializers */
8281 data_offset += size;
8282 /* add padding if bound check */
8283 if (do_bounds_check)
8284 data_offset++;
8285 sec->data_offset = data_offset;
8286 /* allocate section space to put the data */
8287 if (sec->sh_type != SHT_NOBITS &&
8288 data_offset > sec->data_allocated)
8289 section_realloc(sec, data_offset);
8290 } else {
8291 addr = 0; /* avoid warning */
8294 if (v) {
8295 if (scope == VT_CONST) {
8296 if (!sym)
8297 goto do_def;
8298 } else {
8299 do_def:
8300 sym = sym_push(v, type, r | VT_SYM, 0);
8302 /* update symbol definition */
8303 if (sec) {
8304 put_extern_sym(sym, sec, addr, size);
8305 } else {
8306 Elf32_Sym *esym;
8307 /* put a common area */
8308 put_extern_sym(sym, NULL, align, size);
8309 /* XXX: find a nicer way */
8310 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8311 esym->st_shndx = SHN_COMMON;
8313 } else {
8314 CValue cval;
8316 /* push global reference */
8317 sym = get_sym_ref(type, sec, addr, size);
8318 cval.ul = 0;
8319 vsetc(type, VT_CONST | VT_SYM, &cval);
8320 vtop->sym = sym;
8323 /* handles bounds now because the symbol must be defined
8324 before for the relocation */
8325 if (do_bounds_check) {
8326 unsigned long *bounds_ptr;
8328 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8329 /* then add global bound info */
8330 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8331 bounds_ptr[0] = 0; /* relocated */
8332 bounds_ptr[1] = size;
8335 if (has_init) {
8336 decl_initializer(type, sec, addr, 1, 0);
8337 /* restore parse state if needed */
8338 if (init_str.str) {
8339 tok_str_free(init_str.str);
8340 restore_parse_state(&saved_parse_state);
8343 no_alloc: ;
8346 void put_func_debug(Sym *sym)
8348 char buf[512];
8350 /* stabs info */
8351 /* XXX: we put here a dummy type */
8352 snprintf(buf, sizeof(buf), "%s:%c1",
8353 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8354 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8355 cur_text_section, sym->c);
8356 last_ind = 0;
8357 last_line_num = 0;
8360 /* not finished : try to put some local vars in registers */
8361 //#define CONFIG_REG_VARS
8363 #ifdef CONFIG_REG_VARS
8364 void add_var_ref(int t)
8366 printf("%s:%d: &%s\n",
8367 file->filename, file->line_num,
8368 get_tok_str(t, NULL));
8371 /* first pass on a function with heuristic to extract variable usage
8372 and pointer references to local variables for register allocation */
8373 void analyse_function(void)
8375 int level, t;
8377 for(;;) {
8378 if (tok == -1)
8379 break;
8380 /* any symbol coming after '&' is considered as being a
8381 variable whose reference is taken. It is highly unaccurate
8382 but it is difficult to do better without a complete parse */
8383 if (tok == '&') {
8384 next();
8385 /* if '& number', then no need to examine next tokens */
8386 if (tok == TOK_CINT ||
8387 tok == TOK_CUINT ||
8388 tok == TOK_CLLONG ||
8389 tok == TOK_CULLONG) {
8390 continue;
8391 } else if (tok >= TOK_UIDENT) {
8392 /* if '& ident [' or '& ident ->', then ident address
8393 is not needed */
8394 t = tok;
8395 next();
8396 if (tok != '[' && tok != TOK_ARROW)
8397 add_var_ref(t);
8398 } else {
8399 level = 0;
8400 while (tok != '}' && tok != ';' &&
8401 !((tok == ',' || tok == ')') && level == 0)) {
8402 if (tok >= TOK_UIDENT) {
8403 add_var_ref(tok);
8404 } else if (tok == '(') {
8405 level++;
8406 } else if (tok == ')') {
8407 level--;
8409 next();
8412 } else {
8413 next();
8417 #endif
8419 /* parse an old style function declaration list */
8420 /* XXX: check multiple parameter */
8421 static void func_decl_list(Sym *func_sym)
8423 AttributeDef ad;
8424 int v;
8425 Sym *s;
8426 CType btype, type;
8428 /* parse each declaration */
8429 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8430 if (!parse_btype(&btype, &ad))
8431 expect("declaration list");
8432 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8433 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8434 tok == ';') {
8435 /* we accept no variable after */
8436 } else {
8437 for(;;) {
8438 type = btype;
8439 type_decl(&type, &ad, &v, TYPE_DIRECT);
8440 /* find parameter in function parameter list */
8441 s = func_sym->next;
8442 while (s != NULL) {
8443 if ((s->v & ~SYM_FIELD) == v)
8444 goto found;
8445 s = s->next;
8447 error("declaration for parameter '%s' but no such parameter",
8448 get_tok_str(v, NULL));
8449 found:
8450 /* check that no storage specifier except 'register' was given */
8451 if (type.t & VT_STORAGE)
8452 error("storage class specified for '%s'", get_tok_str(v, NULL));
8453 convert_parameter_type(&type);
8454 /* we can add the type (NOTE: it could be local to the function) */
8455 s->type = type;
8456 /* accept other parameters */
8457 if (tok == ',')
8458 next();
8459 else
8460 break;
8463 skip(';');
8467 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8468 static void decl(int l)
8470 int v, has_init, r;
8471 CType type, btype;
8472 Sym *sym;
8473 AttributeDef ad;
8475 while (1) {
8476 if (!parse_btype(&btype, &ad)) {
8477 /* skip redundant ';' */
8478 /* XXX: find more elegant solution */
8479 if (tok == ';') {
8480 next();
8481 continue;
8483 /* special test for old K&R protos without explicit int
8484 type. Only accepted when defining global data */
8485 if (l == VT_LOCAL || tok < TOK_DEFINE)
8486 break;
8487 btype.t = VT_INT;
8489 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8490 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8491 tok == ';') {
8492 /* we accept no variable after */
8493 next();
8494 continue;
8496 while (1) { /* iterate thru each declaration */
8497 type = btype;
8498 type_decl(&type, &ad, &v, TYPE_DIRECT);
8499 #if 0
8501 char buf[500];
8502 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8503 printf("type = '%s'\n", buf);
8505 #endif
8506 if ((type.t & VT_BTYPE) == VT_FUNC) {
8507 /* if old style function prototype, we accept a
8508 declaration list */
8509 sym = type.ref;
8510 if (sym->c == FUNC_OLD)
8511 func_decl_list(sym);
8514 if (tok == '{') {
8515 #ifdef CONFIG_REG_VARS
8516 TokenString func_str;
8517 ParseState saved_parse_state;
8518 int block_level;
8519 #endif
8521 if (l == VT_LOCAL)
8522 error("cannot use local functions");
8523 if (!(type.t & VT_FUNC))
8524 expect("function definition");
8526 /* reject abstract declarators in function definition */
8527 sym = type.ref;
8528 while ((sym = sym->next) != NULL)
8529 if (!(sym->v & ~SYM_FIELD))
8530 expect("identifier");
8532 /* XXX: cannot do better now: convert extern line to static inline */
8533 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8534 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8536 #ifdef CONFIG_REG_VARS
8537 /* parse all function code and record it */
8539 tok_str_new(&func_str);
8541 block_level = 0;
8542 for(;;) {
8543 int t;
8544 if (tok == -1)
8545 error("unexpected end of file");
8546 tok_str_add_tok(&func_str);
8547 t = tok;
8548 next();
8549 if (t == '{') {
8550 block_level++;
8551 } else if (t == '}') {
8552 block_level--;
8553 if (block_level == 0)
8554 break;
8557 tok_str_add(&func_str, -1);
8558 tok_str_add(&func_str, 0);
8560 save_parse_state(&saved_parse_state);
8562 macro_ptr = func_str.str;
8563 next();
8564 analyse_function();
8565 #endif
8567 /* compute text section */
8568 cur_text_section = ad.section;
8569 if (!cur_text_section)
8570 cur_text_section = text_section;
8571 ind = cur_text_section->data_offset;
8572 funcname = get_tok_str(v, NULL);
8573 sym = sym_find(v);
8574 if (sym) {
8575 /* if symbol is already defined, then put complete type */
8576 sym->type = type;
8577 } else {
8578 /* put function symbol */
8579 sym = global_identifier_push(v, type.t, 0);
8580 sym->type.ref = type.ref;
8582 /* NOTE: we patch the symbol size later */
8583 put_extern_sym(sym, cur_text_section, ind, 0);
8584 func_ind = ind;
8585 sym->r = VT_SYM | VT_CONST;
8586 /* put debug symbol */
8587 if (do_debug)
8588 put_func_debug(sym);
8589 /* push a dummy symbol to enable local sym storage */
8590 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8591 gfunc_prolog(&type);
8592 loc = 0;
8593 rsym = 0;
8594 #ifdef CONFIG_REG_VARS
8595 macro_ptr = func_str.str;
8596 next();
8597 #endif
8598 block(NULL, NULL, NULL, NULL, 0, 0);
8599 gsym(rsym);
8600 gfunc_epilog();
8601 cur_text_section->data_offset = ind;
8602 label_pop(&global_label_stack, NULL);
8603 sym_pop(&local_stack, NULL); /* reset local stack */
8604 /* end of function */
8605 /* patch symbol size */
8606 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8607 ind - func_ind;
8608 if (do_debug) {
8609 put_stabn(N_FUN, 0, 0, ind - func_ind);
8611 funcname = ""; /* for safety */
8612 func_vt.t = VT_VOID; /* for safety */
8613 ind = 0; /* for safety */
8615 #ifdef CONFIG_REG_VARS
8616 tok_str_free(func_str.str);
8617 restore_parse_state(&saved_parse_state);
8618 #endif
8619 break;
8620 } else {
8621 if (btype.t & VT_TYPEDEF) {
8622 /* save typedefed type */
8623 /* XXX: test storage specifiers ? */
8624 sym = sym_push(v, &type, 0, 0);
8625 sym->type.t |= VT_TYPEDEF;
8626 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
8627 /* external function definition */
8628 external_sym(v, &type, 0);
8629 } else {
8630 /* not lvalue if array */
8631 r = 0;
8632 if (!(type.t & VT_ARRAY))
8633 r |= lvalue_type(type.t);
8634 has_init = (tok == '=');
8635 if ((btype.t & VT_EXTERN) ||
8636 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
8637 !has_init && l == VT_CONST && type.ref->c < 0)) {
8638 /* external variable */
8639 /* NOTE: as GCC, uninitialized global static
8640 arrays of null size are considered as
8641 extern */
8642 external_sym(v, &type, r);
8643 } else {
8644 if (type.t & VT_STATIC)
8645 r |= VT_CONST;
8646 else
8647 r |= l;
8648 if (has_init)
8649 next();
8650 decl_initializer_alloc(&type, &ad, r,
8651 has_init, v, l);
8654 if (tok != ',') {
8655 skip(';');
8656 break;
8658 next();
8664 /* better than nothing, but needs extension to handle '-E' option
8665 correctly too */
8666 static void preprocess_init(TCCState *s1)
8668 s1->include_stack_ptr = s1->include_stack;
8669 /* XXX: move that before to avoid having to initialize
8670 file->ifdef_stack_ptr ? */
8671 s1->ifdef_stack_ptr = s1->ifdef_stack;
8672 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
8674 /* XXX: not ANSI compliant: bound checking says error */
8675 vtop = vstack - 1;
8678 /* compile the C file opened in 'file'. Return non zero if errors. */
8679 static int tcc_compile(TCCState *s1)
8681 Sym *define_start;
8682 char buf[512];
8683 volatile int section_sym;
8685 #ifdef INC_DEBUG
8686 printf("%s: **** new file\n", file->filename);
8687 #endif
8688 preprocess_init(s1);
8690 funcname = "";
8691 anon_sym = SYM_FIRST_ANOM;
8693 /* file info: full path + filename */
8694 section_sym = 0; /* avoid warning */
8695 if (do_debug) {
8696 section_sym = put_elf_sym(symtab_section, 0, 0,
8697 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
8698 text_section->sh_num, NULL);
8699 getcwd(buf, sizeof(buf));
8700 pstrcat(buf, sizeof(buf), "/");
8701 put_stabs_r(buf, N_SO, 0, 0,
8702 text_section->data_offset, text_section, section_sym);
8703 put_stabs_r(file->filename, N_SO, 0, 0,
8704 text_section->data_offset, text_section, section_sym);
8706 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8707 symbols can be safely used */
8708 put_elf_sym(symtab_section, 0, 0,
8709 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
8710 SHN_ABS, file->filename);
8712 /* define some often used types */
8713 int_type.t = VT_INT;
8715 char_pointer_type.t = VT_BYTE;
8716 mk_pointer(&char_pointer_type);
8718 func_old_type.t = VT_FUNC;
8719 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
8721 #if 0
8722 /* define 'void *alloca(unsigned int)' builtin function */
8724 Sym *s1;
8726 p = anon_sym++;
8727 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
8728 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
8729 s1->next = NULL;
8730 sym->next = s1;
8731 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
8733 #endif
8735 define_start = define_stack;
8737 if (setjmp(s1->error_jmp_buf) == 0) {
8738 s1->nb_errors = 0;
8739 s1->error_set_jmp_enabled = 1;
8741 ch = file->buf_ptr[0];
8742 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
8743 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
8744 next();
8745 decl(VT_CONST);
8746 if (tok != TOK_EOF)
8747 expect("declaration");
8749 /* end of translation unit info */
8750 if (do_debug) {
8751 put_stabs_r(NULL, N_SO, 0, 0,
8752 text_section->data_offset, text_section, section_sym);
8755 s1->error_set_jmp_enabled = 0;
8757 /* reset define stack, but leave -Dsymbols (may be incorrect if
8758 they are undefined) */
8759 free_defines(define_start);
8761 sym_pop(&global_stack, NULL);
8763 return s1->nb_errors != 0 ? -1 : 0;
8766 #ifdef LIBTCC
8767 int tcc_compile_string(TCCState *s, const char *str)
8769 BufferedFile bf1, *bf = &bf1;
8770 int ret, len;
8771 char *buf;
8773 /* init file structure */
8774 bf->fd = -1;
8775 /* XXX: avoid copying */
8776 len = strlen(str);
8777 buf = tcc_malloc(len + 1);
8778 if (!buf)
8779 return -1;
8780 memcpy(buf, str, len);
8781 buf[len] = CH_EOB;
8782 bf->buf_ptr = buf;
8783 bf->buf_end = buf + len;
8784 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
8785 bf->line_num = 1;
8786 file = bf;
8788 ret = tcc_compile(s);
8790 tcc_free(buf);
8792 /* currently, no need to close */
8793 return ret;
8795 #endif
8797 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8798 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
8800 BufferedFile bf1, *bf = &bf1;
8802 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
8803 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
8804 /* default value */
8805 if (!value)
8806 value = "1";
8807 pstrcat(bf->buffer, IO_BUF_SIZE, value);
8809 /* init file structure */
8810 bf->fd = -1;
8811 bf->buf_ptr = bf->buffer;
8812 bf->buf_end = bf->buffer + strlen(bf->buffer);
8813 *bf->buf_end = CH_EOB;
8814 bf->filename[0] = '\0';
8815 bf->line_num = 1;
8816 file = bf;
8818 s1->include_stack_ptr = s1->include_stack;
8820 /* parse with define parser */
8821 ch = file->buf_ptr[0];
8822 next_nomacro();
8823 parse_define();
8824 file = NULL;
8827 /* undefine a preprocessor symbol */
8828 void tcc_undefine_symbol(TCCState *s1, const char *sym)
8830 TokenSym *ts;
8831 Sym *s;
8832 ts = tok_alloc(sym, strlen(sym));
8833 s = define_find(ts->tok);
8834 /* undefine symbol by putting an invalid name */
8835 if (s)
8836 define_undef(s);
8839 #ifdef CONFIG_TCC_ASM
8841 #include "i386-asm.c"
8842 #include "tccasm.c"
8844 #else
8845 static void asm_instr(void)
8847 error("inline asm() not supported");
8849 #endif
8851 #include "tccelf.c"
8853 /* print the position in the source file of PC value 'pc' by reading
8854 the stabs debug information */
8855 static void rt_printline(unsigned long wanted_pc)
8857 Stab_Sym *sym, *sym_end;
8858 char func_name[128], last_func_name[128];
8859 unsigned long func_addr, last_pc, pc;
8860 const char *incl_files[INCLUDE_STACK_SIZE];
8861 int incl_index, len, last_line_num, i;
8862 const char *str, *p;
8864 fprintf(stderr, "0x%08lx:", wanted_pc);
8866 func_name[0] = '\0';
8867 func_addr = 0;
8868 incl_index = 0;
8869 last_func_name[0] = '\0';
8870 last_pc = 0xffffffff;
8871 last_line_num = 1;
8872 sym = (Stab_Sym *)stab_section->data + 1;
8873 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
8874 while (sym < sym_end) {
8875 switch(sym->n_type) {
8876 /* function start or end */
8877 case N_FUN:
8878 if (sym->n_strx == 0) {
8879 /* we test if between last line and end of function */
8880 pc = sym->n_value + func_addr;
8881 if (wanted_pc >= last_pc && wanted_pc < pc)
8882 goto found;
8883 func_name[0] = '\0';
8884 func_addr = 0;
8885 } else {
8886 str = stabstr_section->data + sym->n_strx;
8887 p = strchr(str, ':');
8888 if (!p) {
8889 pstrcpy(func_name, sizeof(func_name), str);
8890 } else {
8891 len = p - str;
8892 if (len > sizeof(func_name) - 1)
8893 len = sizeof(func_name) - 1;
8894 memcpy(func_name, str, len);
8895 func_name[len] = '\0';
8897 func_addr = sym->n_value;
8899 break;
8900 /* line number info */
8901 case N_SLINE:
8902 pc = sym->n_value + func_addr;
8903 if (wanted_pc >= last_pc && wanted_pc < pc)
8904 goto found;
8905 last_pc = pc;
8906 last_line_num = sym->n_desc;
8907 /* XXX: slow! */
8908 strcpy(last_func_name, func_name);
8909 break;
8910 /* include files */
8911 case N_BINCL:
8912 str = stabstr_section->data + sym->n_strx;
8913 add_incl:
8914 if (incl_index < INCLUDE_STACK_SIZE) {
8915 incl_files[incl_index++] = str;
8917 break;
8918 case N_EINCL:
8919 if (incl_index > 1)
8920 incl_index--;
8921 break;
8922 case N_SO:
8923 if (sym->n_strx == 0) {
8924 incl_index = 0; /* end of translation unit */
8925 } else {
8926 str = stabstr_section->data + sym->n_strx;
8927 /* do not add path */
8928 len = strlen(str);
8929 if (len > 0 && str[len - 1] != '/')
8930 goto add_incl;
8932 break;
8934 sym++;
8937 /* second pass: we try symtab symbols (no line number info) */
8938 incl_index = 0;
8940 Elf32_Sym *sym, *sym_end;
8941 int type;
8943 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
8944 for(sym = (Elf32_Sym *)symtab_section->data + 1;
8945 sym < sym_end;
8946 sym++) {
8947 type = ELF32_ST_TYPE(sym->st_info);
8948 if (type == STT_FUNC) {
8949 if (wanted_pc >= sym->st_value &&
8950 wanted_pc < sym->st_value + sym->st_size) {
8951 pstrcpy(last_func_name, sizeof(last_func_name),
8952 strtab_section->data + sym->st_name);
8953 goto found;
8958 /* did not find any info: */
8959 fprintf(stderr, " ???\n");
8960 return;
8961 found:
8962 if (last_func_name[0] != '\0') {
8963 fprintf(stderr, " %s()", last_func_name);
8965 if (incl_index > 0) {
8966 fprintf(stderr, " (%s:%d",
8967 incl_files[incl_index - 1], last_line_num);
8968 for(i = incl_index - 2; i >= 0; i--)
8969 fprintf(stderr, ", included from %s", incl_files[i]);
8970 fprintf(stderr, ")");
8972 fprintf(stderr, "\n");
8975 #ifndef WIN32
8977 #ifdef __i386__
8979 /* fix for glibc 2.1 */
8980 #ifndef REG_EIP
8981 #define REG_EIP EIP
8982 #define REG_EBP EBP
8983 #endif
8985 /* return the PC at frame level 'level'. Return non zero if not found */
8986 static int rt_get_caller_pc(unsigned long *paddr,
8987 ucontext_t *uc, int level)
8989 unsigned long fp;
8990 int i;
8992 if (level == 0) {
8993 #ifdef __FreeBSD__
8994 *paddr = uc->uc_mcontext.mc_eip;
8995 #else
8996 *paddr = uc->uc_mcontext.gregs[REG_EIP];
8997 #endif
8998 return 0;
8999 } else {
9000 #ifdef __FreeBSD__
9001 fp = uc->uc_mcontext.mc_ebp;
9002 #else
9003 fp = uc->uc_mcontext.gregs[REG_EBP];
9004 #endif
9005 for(i=1;i<level;i++) {
9006 /* XXX: check address validity with program info */
9007 if (fp <= 0x1000 || fp >= 0xc0000000)
9008 return -1;
9009 fp = ((unsigned long *)fp)[0];
9011 *paddr = ((unsigned long *)fp)[1];
9012 return 0;
9015 #else
9016 #error add arch specific rt_get_caller_pc()
9017 #endif
9019 /* emit a run time error at position 'pc' */
9020 void rt_error(ucontext_t *uc, const char *fmt, ...)
9022 va_list ap;
9023 unsigned long pc;
9024 int i;
9026 va_start(ap, fmt);
9027 fprintf(stderr, "Runtime error: ");
9028 vfprintf(stderr, fmt, ap);
9029 fprintf(stderr, "\n");
9030 for(i=0;i<num_callers;i++) {
9031 if (rt_get_caller_pc(&pc, uc, i) < 0)
9032 break;
9033 if (i == 0)
9034 fprintf(stderr, "at ");
9035 else
9036 fprintf(stderr, "by ");
9037 rt_printline(pc);
9039 exit(255);
9040 va_end(ap);
9043 /* signal handler for fatal errors */
9044 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9046 ucontext_t *uc = puc;
9048 switch(signum) {
9049 case SIGFPE:
9050 switch(siginf->si_code) {
9051 case FPE_INTDIV:
9052 case FPE_FLTDIV:
9053 rt_error(uc, "division by zero");
9054 break;
9055 default:
9056 rt_error(uc, "floating point exception");
9057 break;
9059 break;
9060 case SIGBUS:
9061 case SIGSEGV:
9062 if (rt_bound_error_msg && *rt_bound_error_msg)
9063 rt_error(uc, *rt_bound_error_msg);
9064 else
9065 rt_error(uc, "dereferencing invalid pointer");
9066 break;
9067 case SIGILL:
9068 rt_error(uc, "illegal instruction");
9069 break;
9070 case SIGABRT:
9071 rt_error(uc, "abort() called");
9072 break;
9073 default:
9074 rt_error(uc, "caught signal %d", signum);
9075 break;
9077 exit(255);
9079 #endif
9081 /* do all relocations (needed before using tcc_get_symbol()) */
9082 int tcc_relocate(TCCState *s1)
9084 Section *s;
9085 int i;
9087 s1->nb_errors = 0;
9089 tcc_add_runtime(s1);
9091 build_got_entries(s1);
9093 relocate_common_syms();
9095 /* compute relocation address : section are relocated in place. We
9096 also alloc the bss space */
9097 for(i = 1; i < s1->nb_sections; i++) {
9098 s = s1->sections[i];
9099 if (s->sh_flags & SHF_ALLOC) {
9100 if (s->sh_type == SHT_NOBITS)
9101 s->data = tcc_mallocz(s->data_offset);
9102 s->sh_addr = (unsigned long)s->data;
9106 relocate_syms(s1, 1);
9108 if (s1->nb_errors != 0)
9109 return -1;
9111 /* relocate each section */
9112 for(i = 1; i < s1->nb_sections; i++) {
9113 s = s1->sections[i];
9114 if (s->reloc)
9115 relocate_section(s1, s);
9117 return 0;
9120 /* launch the compiled program with the given arguments */
9121 int tcc_run(TCCState *s1, int argc, char **argv)
9123 int (*prog_main)(int, char **);
9125 if (tcc_relocate(s1) < 0)
9126 return -1;
9128 prog_main = tcc_get_symbol_err(s1, "main");
9130 if (do_debug) {
9131 #ifdef WIN32
9132 error("debug mode currently not available for Windows");
9133 #else
9134 struct sigaction sigact;
9135 /* install TCC signal handlers to print debug info on fatal
9136 runtime errors */
9137 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
9138 sigact.sa_sigaction = sig_error;
9139 sigemptyset(&sigact.sa_mask);
9140 sigaction(SIGFPE, &sigact, NULL);
9141 sigaction(SIGILL, &sigact, NULL);
9142 sigaction(SIGSEGV, &sigact, NULL);
9143 sigaction(SIGBUS, &sigact, NULL);
9144 sigaction(SIGABRT, &sigact, NULL);
9145 #endif
9148 #ifdef CONFIG_TCC_BCHECK
9149 if (do_bounds_check) {
9150 void (*bound_init)(void);
9152 /* set error function */
9153 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
9154 "__bound_error_msg");
9156 /* XXX: use .init section so that it also work in binary ? */
9157 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
9158 bound_init();
9160 #endif
9161 return (*prog_main)(argc, argv);
9164 TCCState *tcc_new(void)
9166 const char *p, *r;
9167 TCCState *s;
9168 TokenSym *ts;
9169 int i, c;
9171 s = tcc_mallocz(sizeof(TCCState));
9172 if (!s)
9173 return NULL;
9174 tcc_state = s;
9175 s->output_type = TCC_OUTPUT_MEMORY;
9177 /* init isid table */
9178 for(i=0;i<256;i++)
9179 isidnum_table[i] = isid(i) || isnum(i);
9181 /* add all tokens */
9182 table_ident = NULL;
9183 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9185 tok_ident = TOK_IDENT;
9186 p = tcc_keywords;
9187 while (*p) {
9188 r = p;
9189 for(;;) {
9190 c = *r++;
9191 if (c == '\0')
9192 break;
9194 ts = tok_alloc(p, r - p - 1);
9195 p = r;
9198 /* we add dummy defines for some special macros to speed up tests
9199 and to have working defined() */
9200 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9201 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9202 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9203 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9205 /* standard defines */
9206 tcc_define_symbol(s, "__STDC__", NULL);
9207 #if defined(TCC_TARGET_I386)
9208 tcc_define_symbol(s, "__i386__", NULL);
9209 #endif
9210 #if defined(linux)
9211 tcc_define_symbol(s, "__linux__", NULL);
9212 tcc_define_symbol(s, "linux", NULL);
9213 #endif
9214 /* tiny C specific defines */
9215 tcc_define_symbol(s, "__TINYC__", NULL);
9217 /* tiny C & gcc defines */
9218 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9219 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9220 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9222 /* default library paths */
9223 tcc_add_library_path(s, "/usr/local/lib");
9224 tcc_add_library_path(s, "/usr/lib");
9225 tcc_add_library_path(s, "/lib");
9227 /* no section zero */
9228 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9230 /* create standard sections */
9231 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9232 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9233 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9235 /* symbols are always generated for linking stage */
9236 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9237 ".strtab",
9238 ".hashtab", SHF_PRIVATE);
9239 strtab_section = symtab_section->link;
9241 /* private symbol table for dynamic symbols */
9242 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9243 ".dynstrtab",
9244 ".dynhashtab", SHF_PRIVATE);
9245 s->alacarte_link = 1;
9246 return s;
9249 void tcc_delete(TCCState *s1)
9251 int i, n;
9253 /* free -D defines */
9254 free_defines(NULL);
9256 /* free tokens */
9257 n = tok_ident - TOK_IDENT;
9258 for(i = 0; i < n; i++)
9259 tcc_free(table_ident[i]);
9260 tcc_free(table_ident);
9262 /* free all sections */
9264 free_section(symtab_section->hash);
9266 free_section(s1->dynsymtab_section->hash);
9267 free_section(s1->dynsymtab_section->link);
9268 free_section(s1->dynsymtab_section);
9270 for(i = 1; i < s1->nb_sections; i++)
9271 free_section(s1->sections[i]);
9272 tcc_free(s1->sections);
9274 /* free loaded dlls array */
9275 for(i = 0; i < s1->nb_loaded_dlls; i++)
9276 tcc_free(s1->loaded_dlls[i]);
9277 tcc_free(s1->loaded_dlls);
9279 /* library paths */
9280 for(i = 0; i < s1->nb_library_paths; i++)
9281 tcc_free(s1->library_paths[i]);
9282 tcc_free(s1->library_paths);
9284 /* cached includes */
9285 for(i = 0; i < s1->nb_cached_includes; i++)
9286 tcc_free(s1->cached_includes[i]);
9287 tcc_free(s1->cached_includes);
9289 for(i = 0; i < s1->nb_include_paths; i++)
9290 tcc_free(s1->include_paths[i]);
9291 tcc_free(s1->include_paths);
9293 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9294 tcc_free(s1->sysinclude_paths[i]);
9295 tcc_free(s1->sysinclude_paths);
9297 tcc_free(s1);
9300 int tcc_add_include_path(TCCState *s1, const char *pathname)
9302 char *pathname1;
9304 pathname1 = tcc_strdup(pathname);
9305 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9306 return 0;
9309 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9311 char *pathname1;
9313 pathname1 = tcc_strdup(pathname);
9314 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9315 return 0;
9318 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9320 const char *ext, *filename1;
9321 Elf32_Ehdr ehdr;
9322 int fd, ret;
9323 BufferedFile *saved_file;
9325 /* find source file type with extension */
9326 filename1 = strrchr(filename, '/');
9327 if (filename1)
9328 filename1++;
9329 else
9330 filename1 = filename;
9331 ext = strrchr(filename1, '.');
9332 if (ext)
9333 ext++;
9335 /* open the file */
9336 saved_file = file;
9337 file = tcc_open(s1, filename);
9338 if (!file) {
9339 if (flags & AFF_PRINT_ERROR) {
9340 error_noabort("file '%s' not found", filename);
9342 ret = -1;
9343 goto fail1;
9346 if (!ext || !strcmp(ext, "c")) {
9347 /* C file assumed */
9348 ret = tcc_compile(s1);
9349 } else
9350 #ifdef CONFIG_TCC_ASM
9351 if (!strcmp(ext, "S")) {
9352 /* preprocessed assembler */
9353 ret = tcc_assemble(s1, 1);
9354 } else if (!strcmp(ext, "s")) {
9355 /* non preprocessed assembler */
9356 ret = tcc_assemble(s1, 0);
9357 } else
9358 #endif
9360 fd = file->fd;
9361 /* assume executable format: auto guess file type */
9362 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
9363 error_noabort("could not read header");
9364 goto fail;
9366 lseek(fd, 0, SEEK_SET);
9368 if (ehdr.e_ident[0] == ELFMAG0 &&
9369 ehdr.e_ident[1] == ELFMAG1 &&
9370 ehdr.e_ident[2] == ELFMAG2 &&
9371 ehdr.e_ident[3] == ELFMAG3) {
9372 file->line_num = 0; /* do not display line number if error */
9373 if (ehdr.e_type == ET_REL) {
9374 ret = tcc_load_object_file(s1, fd, 0);
9375 } else if (ehdr.e_type == ET_DYN) {
9376 if (s1->output_type == TCC_OUTPUT_MEMORY) {
9377 void *h;
9378 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
9379 if (h)
9380 ret = 0;
9381 else
9382 ret = -1;
9383 } else {
9384 ret = tcc_load_dll(s1, fd, filename,
9385 (flags & AFF_REFERENCED_DLL) != 0);
9387 } else {
9388 error_noabort("unrecognized ELF file");
9389 goto fail;
9391 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9392 file->line_num = 0; /* do not display line number if error */
9393 ret = tcc_load_archive(s1, fd);
9394 } else {
9395 /* as GNU ld, consider it is an ld script if not recognized */
9396 ret = tcc_load_ldscript(s1);
9397 if (ret < 0) {
9398 error_noabort("unrecognized file type");
9399 goto fail;
9403 the_end:
9404 tcc_close(file);
9405 fail1:
9406 file = saved_file;
9407 return ret;
9408 fail:
9409 ret = -1;
9410 goto the_end;
9413 int tcc_add_file(TCCState *s, const char *filename)
9415 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9418 int tcc_add_library_path(TCCState *s, const char *pathname)
9420 char *pathname1;
9422 pathname1 = tcc_strdup(pathname);
9423 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9424 return 0;
9427 /* find and load a dll. Return non zero if not found */
9428 /* XXX: add '-rpath' option support ? */
9429 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9431 char buf[1024];
9432 int i;
9434 for(i = 0; i < s->nb_library_paths; i++) {
9435 snprintf(buf, sizeof(buf), "%s/%s",
9436 s->library_paths[i], filename);
9437 if (tcc_add_file_internal(s, buf, flags) == 0)
9438 return 0;
9440 return -1;
9443 /* the library name is the same as the argument of the '-l' option */
9444 int tcc_add_library(TCCState *s, const char *libraryname)
9446 char buf[1024];
9447 int i;
9449 /* first we look for the dynamic library if not static linking */
9450 if (!s->static_link) {
9451 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9452 if (tcc_add_dll(s, buf, 0) == 0)
9453 return 0;
9456 /* then we look for the static library */
9457 for(i = 0; i < s->nb_library_paths; i++) {
9458 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9459 s->library_paths[i], libraryname);
9460 if (tcc_add_file_internal(s, buf, 0) == 0)
9461 return 0;
9463 return -1;
9466 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9468 add_elf_sym(symtab_section, val, 0,
9469 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9470 SHN_ABS, name);
9471 return 0;
9474 int tcc_set_output_type(TCCState *s, int output_type)
9476 char buf[1024];
9478 s->output_type = output_type;
9480 if (!s->nostdinc) {
9481 /* default include paths */
9482 /* XXX: reverse order needed if -isystem support */
9483 tcc_add_sysinclude_path(s, "/usr/local/include");
9484 tcc_add_sysinclude_path(s, "/usr/include");
9485 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
9486 tcc_add_sysinclude_path(s, buf);
9489 /* if bound checking, then add corresponding sections */
9490 #ifdef CONFIG_TCC_BCHECK
9491 if (do_bounds_check) {
9492 /* define symbol */
9493 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
9494 /* create bounds sections */
9495 bounds_section = new_section(s, ".bounds",
9496 SHT_PROGBITS, SHF_ALLOC);
9497 lbounds_section = new_section(s, ".lbounds",
9498 SHT_PROGBITS, SHF_ALLOC);
9500 #endif
9502 /* add debug sections */
9503 if (do_debug) {
9504 /* stab symbols */
9505 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
9506 stab_section->sh_entsize = sizeof(Stab_Sym);
9507 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
9508 put_elf_str(stabstr_section, "");
9509 stab_section->link = stabstr_section;
9510 /* put first entry */
9511 put_stabs("", 0, 0, 0, 0);
9514 /* add libc crt1/crti objects */
9515 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
9516 !s->nostdlib) {
9517 if (output_type != TCC_OUTPUT_DLL)
9518 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
9519 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
9521 return 0;
9524 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
9526 typedef struct WarningDef {
9527 int offset;
9528 int flags;
9529 const char *name;
9530 } WarningDef;
9532 static const WarningDef warning_defs[] = {
9533 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
9534 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
9535 { offsetof(TCCState, warn_error), 0, "error" },
9538 /* set/reset a warning */
9539 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
9541 int i;
9542 const WarningDef *p;
9543 if (!strcmp(warning_name, "all")) {
9544 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9545 if (p->flags & WD_ALL)
9546 *(int *)((uint8_t *)s + p->offset) = 1;
9548 } else {
9549 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9550 if (!strcmp(warning_name, p->name))
9551 goto found;
9553 return -1;
9554 found:
9555 *(int *)((uint8_t *)s + p->offset) = value;
9557 return 0;
9560 #if !defined(LIBTCC)
9562 /* extract the basename of a file */
9563 static const char *tcc_basename(const char *name)
9565 const char *p;
9566 p = strrchr(name, '/');
9567 #ifdef WIN32
9568 if (!p)
9569 p = strrchr(name, '\\');
9570 #endif
9571 if (!p)
9572 p = name;
9573 else
9574 p++;
9575 return p;
9578 static int64_t getclock_us(void)
9580 #ifdef WIN32
9581 struct _timeb tb;
9582 _ftime(&tb);
9583 return (tb.time * 1000LL + tb.millitm) * 1000LL;
9584 #else
9585 struct timeval tv;
9586 gettimeofday(&tv, NULL);
9587 return tv.tv_sec * 1000000LL + tv.tv_usec;
9588 #endif
9591 void help(void)
9593 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2003 Fabrice Bellard\n"
9594 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9595 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9596 " [infile1 infile2...] [-run infile args...]\n"
9597 "\n"
9598 "General options:\n"
9599 " -v display current version\n"
9600 " -c compile only - generate an object file\n"
9601 " -o outfile set output filename\n"
9602 " -Bdir set tcc internal library path\n"
9603 " -bench output compilation statistics\n"
9604 " -run run compiled source\n"
9605 " -Wwarning set or reset (with 'no-' prefix) 'warning'\n"
9606 " -w disable all warnings\n"
9607 "Preprocessor options:\n"
9608 " -Idir add include path 'dir'\n"
9609 " -Dsym[=val] define 'sym' with value 'val'\n"
9610 " -Usym undefine 'sym'\n"
9611 "Linker options:\n"
9612 " -Ldir add library path 'dir'\n"
9613 " -llib link with dynamic or static library 'lib'\n"
9614 " -shared generate a shared library\n"
9615 " -static static linking\n"
9616 " -rdynamic export all global symbols to dynamic linker\n"
9617 " -r relocatable output\n"
9618 "Debugger options:\n"
9619 " -g generate runtime debug info\n"
9620 #ifdef CONFIG_TCC_BCHECK
9621 " -b compile with built-in memory and bounds checker (implies -g)\n"
9622 #endif
9623 " -bt N show N callers in stack traces\n"
9627 #define TCC_OPTION_HAS_ARG 0x0001
9628 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9630 typedef struct TCCOption {
9631 const char *name;
9632 uint16_t index;
9633 uint16_t flags;
9634 } TCCOption;
9636 enum {
9637 TCC_OPTION_HELP,
9638 TCC_OPTION_I,
9639 TCC_OPTION_D,
9640 TCC_OPTION_U,
9641 TCC_OPTION_L,
9642 TCC_OPTION_B,
9643 TCC_OPTION_l,
9644 TCC_OPTION_bench,
9645 TCC_OPTION_bt,
9646 TCC_OPTION_b,
9647 TCC_OPTION_g,
9648 TCC_OPTION_c,
9649 TCC_OPTION_static,
9650 TCC_OPTION_shared,
9651 TCC_OPTION_o,
9652 TCC_OPTION_r,
9653 TCC_OPTION_W,
9654 TCC_OPTION_O,
9655 TCC_OPTION_m,
9656 TCC_OPTION_f,
9657 TCC_OPTION_nostdinc,
9658 TCC_OPTION_nostdlib,
9659 TCC_OPTION_print_search_dirs,
9660 TCC_OPTION_rdynamic,
9661 TCC_OPTION_run,
9662 TCC_OPTION_v,
9663 TCC_OPTION_w,
9666 static const TCCOption tcc_options[] = {
9667 { "h", TCC_OPTION_HELP, 0 },
9668 { "?", TCC_OPTION_HELP, 0 },
9669 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
9670 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
9671 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
9672 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
9673 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
9674 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9675 { "bench", TCC_OPTION_bench, 0 },
9676 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
9677 #ifdef CONFIG_TCC_BCHECK
9678 { "b", TCC_OPTION_b, 0 },
9679 #endif
9680 { "g", TCC_OPTION_g, 0 },
9681 { "c", TCC_OPTION_c, 0 },
9682 { "static", TCC_OPTION_static, 0 },
9683 { "shared", TCC_OPTION_shared, 0 },
9684 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
9685 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9686 { "rdynamic", TCC_OPTION_rdynamic, 0 },
9687 { "r", TCC_OPTION_r, 0 },
9688 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9689 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9690 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
9691 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9692 { "nostdinc", TCC_OPTION_nostdinc, 0 },
9693 { "nostdlib", TCC_OPTION_nostdlib, 0 },
9694 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
9695 { "v", TCC_OPTION_v, 0 },
9696 { "w", TCC_OPTION_w, 0 },
9697 { NULL },
9700 /* convert 'str' into an array of space separated strings */
9701 static int expand_args(char ***pargv, const char *str)
9703 const char *s1;
9704 char **argv, *arg;
9705 int argc, len;
9707 argc = 0;
9708 argv = NULL;
9709 for(;;) {
9710 while (is_space(*str))
9711 str++;
9712 if (*str == '\0')
9713 break;
9714 s1 = str;
9715 while (*str != '\0' && !is_space(*str))
9716 str++;
9717 len = str - s1;
9718 arg = tcc_malloc(len + 1);
9719 memcpy(arg, s1, len);
9720 arg[len] = '\0';
9721 dynarray_add((void ***)&argv, &argc, arg);
9723 *pargv = argv;
9724 return argc;
9727 static char **files;
9728 static int nb_files, nb_libraries;
9729 static int multiple_files;
9730 static int print_search_dirs;
9731 static int output_type;
9732 static int reloc_output;
9733 static const char *outfile;
9735 int parse_args(TCCState *s, int argc, char **argv)
9737 int optind;
9738 const TCCOption *popt;
9739 const char *optarg, *p1, *r1;
9740 char *r;
9742 optind = 0;
9743 while (1) {
9744 if (optind >= argc) {
9745 if (nb_files == 0 && !print_search_dirs)
9746 goto show_help;
9747 else
9748 break;
9750 r = argv[optind++];
9751 if (r[0] != '-') {
9752 /* add a new file */
9753 dynarray_add((void ***)&files, &nb_files, r);
9754 if (!multiple_files) {
9755 optind--;
9756 /* argv[0] will be this file */
9757 break;
9759 } else {
9760 /* find option in table (match only the first chars */
9761 popt = tcc_options;
9762 for(;;) {
9763 p1 = popt->name;
9764 if (p1 == NULL)
9765 error("invalid option -- '%s'", r);
9766 r1 = r + 1;
9767 for(;;) {
9768 if (*p1 == '\0')
9769 goto option_found;
9770 if (*r1 != *p1)
9771 break;
9772 p1++;
9773 r1++;
9775 popt++;
9777 option_found:
9778 if (popt->flags & TCC_OPTION_HAS_ARG) {
9779 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
9780 optarg = r1;
9781 } else {
9782 if (optind >= argc)
9783 error("argument to '%s' is missing", r);
9784 optarg = argv[optind++];
9786 } else {
9787 if (*r1 != '\0')
9788 goto show_help;
9789 optarg = NULL;
9792 switch(popt->index) {
9793 case TCC_OPTION_HELP:
9794 show_help:
9795 help();
9796 exit(1);
9797 case TCC_OPTION_I:
9798 if (tcc_add_include_path(s, optarg) < 0)
9799 error("too many include paths");
9800 break;
9801 case TCC_OPTION_D:
9803 char *sym, *value;
9804 sym = (char *)optarg;
9805 value = strchr(sym, '=');
9806 if (value) {
9807 *value = '\0';
9808 value++;
9810 tcc_define_symbol(s, sym, value);
9812 break;
9813 case TCC_OPTION_U:
9814 tcc_undefine_symbol(s, optarg);
9815 break;
9816 case TCC_OPTION_L:
9817 tcc_add_library_path(s, optarg);
9818 break;
9819 case TCC_OPTION_B:
9820 /* set tcc utilities path (mainly for tcc development) */
9821 tcc_lib_path = optarg;
9822 break;
9823 case TCC_OPTION_l:
9824 dynarray_add((void ***)&files, &nb_files, r);
9825 nb_libraries++;
9826 break;
9827 case TCC_OPTION_bench:
9828 do_bench = 1;
9829 break;
9830 case TCC_OPTION_bt:
9831 num_callers = atoi(optarg);
9832 break;
9833 #ifdef CONFIG_TCC_BCHECK
9834 case TCC_OPTION_b:
9835 do_bounds_check = 1;
9836 do_debug = 1;
9837 break;
9838 #endif
9839 case TCC_OPTION_g:
9840 do_debug = 1;
9841 break;
9842 case TCC_OPTION_c:
9843 multiple_files = 1;
9844 output_type = TCC_OUTPUT_OBJ;
9845 break;
9846 case TCC_OPTION_static:
9847 s->static_link = 1;
9848 break;
9849 case TCC_OPTION_shared:
9850 output_type = TCC_OUTPUT_DLL;
9851 break;
9852 case TCC_OPTION_o:
9853 multiple_files = 1;
9854 outfile = optarg;
9855 break;
9856 case TCC_OPTION_r:
9857 /* generate a .o merging several output files */
9858 reloc_output = 1;
9859 output_type = TCC_OUTPUT_OBJ;
9860 break;
9861 case TCC_OPTION_nostdinc:
9862 s->nostdinc = 1;
9863 break;
9864 case TCC_OPTION_nostdlib:
9865 s->nostdlib = 1;
9866 break;
9867 case TCC_OPTION_print_search_dirs:
9868 print_search_dirs = 1;
9869 break;
9870 case TCC_OPTION_run:
9872 int argc1;
9873 char **argv1;
9874 argc1 = expand_args(&argv1, optarg);
9875 if (argc1 > 0) {
9876 parse_args(s, argc1, argv1);
9878 multiple_files = 0;
9879 output_type = TCC_OUTPUT_MEMORY;
9881 break;
9882 case TCC_OPTION_v:
9883 printf("tcc version %s\n", TCC_VERSION);
9884 exit(0);
9885 case TCC_OPTION_W:
9887 const char *p = optarg;
9888 int value;
9889 value = 1;
9890 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-') {
9891 p += 2;
9892 value = 0;
9894 if (tcc_set_warning(s, p, value) < 0 && s->warn_unsupported)
9895 goto unsupported_option;
9897 break;
9898 case TCC_OPTION_w:
9899 s->warn_none = 1;
9900 break;
9901 case TCC_OPTION_rdynamic:
9902 s->rdynamic = 1;
9903 break;
9904 default:
9905 if (s->warn_unsupported) {
9906 unsupported_option:
9907 warning("unsupported option '%s'", r);
9909 break;
9913 return optind;
9916 int main(int argc, char **argv)
9918 int i;
9919 TCCState *s;
9920 int nb_objfiles, ret, optind;
9921 char objfilename[1024];
9922 int64_t start_time = 0;
9924 s = tcc_new();
9925 output_type = TCC_OUTPUT_EXE;
9926 outfile = NULL;
9927 multiple_files = 1;
9928 files = NULL;
9929 nb_files = 0;
9930 nb_libraries = 0;
9931 reloc_output = 0;
9932 print_search_dirs = 0;
9934 optind = parse_args(s, argc - 1, argv + 1) + 1;
9936 if (print_search_dirs) {
9937 /* enough for Linux kernel */
9938 printf("install: %s/\n", tcc_lib_path);
9939 return 0;
9942 nb_objfiles = nb_files - nb_libraries;
9944 /* if outfile provided without other options, we output an
9945 executable */
9946 if (outfile && output_type == TCC_OUTPUT_MEMORY)
9947 output_type = TCC_OUTPUT_EXE;
9949 /* check -c consistency : only single file handled. XXX: checks file type */
9950 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9951 /* accepts only a single input file */
9952 if (nb_objfiles != 1)
9953 error("cannot specify multiple files with -c");
9954 if (nb_libraries != 0)
9955 error("cannot specify libraries with -c");
9958 /* compute default outfile name */
9959 if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
9960 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9961 char *ext;
9962 /* strip path */
9963 pstrcpy(objfilename, sizeof(objfilename) - 1,
9964 tcc_basename(files[0]));
9965 /* add .o extension */
9966 ext = strrchr(objfilename, '.');
9967 if (!ext)
9968 goto default_outfile;
9969 strcpy(ext + 1, "o");
9970 } else {
9971 default_outfile:
9972 pstrcpy(objfilename, sizeof(objfilename), "a.out");
9974 outfile = objfilename;
9977 if (do_bench) {
9978 start_time = getclock_us();
9981 tcc_set_output_type(s, output_type);
9983 /* compile or add each files or library */
9984 for(i = 0;i < nb_files; i++) {
9985 const char *filename;
9987 filename = files[i];
9988 if (filename[0] == '-') {
9989 if (tcc_add_library(s, filename + 2) < 0)
9990 error("cannot find %s", filename);
9991 } else {
9992 if (tcc_add_file(s, filename) < 0) {
9993 ret = 1;
9994 goto the_end;
9999 /* free all files */
10000 tcc_free(files);
10002 if (do_bench) {
10003 double total_time;
10004 total_time = (double)(getclock_us() - start_time) / 1000000.0;
10005 if (total_time < 0.001)
10006 total_time = 0.001;
10007 if (total_bytes < 1)
10008 total_bytes = 1;
10009 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
10010 tok_ident - TOK_IDENT, total_lines, total_bytes,
10011 total_time, (int)(total_lines / total_time),
10012 total_bytes / total_time / 1000000.0);
10015 if (s->output_type != TCC_OUTPUT_MEMORY) {
10016 tcc_output_file(s, outfile);
10017 ret = 0;
10018 } else {
10019 ret = tcc_run(s, argc - optind, argv + optind);
10021 the_end:
10022 /* XXX: cannot do it with bound checking because of the malloc hooks */
10023 if (!do_bounds_check)
10024 tcc_delete(s);
10026 #ifdef MEM_DEBUG
10027 if (do_bench) {
10028 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
10030 #endif
10031 return ret;
10034 #endif