more precise C type check support (const warnings) - added generic -W option support
[tinycc.git] / tcc.c
blob1091a56ea279491490fc3bb0e5c13239e302b1e1
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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[64]; /* 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 /* if true, no standard headers are added */
396 int nostdinc;
398 /* if true, static linking is performed */
399 int static_link;
401 /* warning switches */
402 int warn_write_strings;
403 int warn_unsupported;
404 int warn_error;
406 /* error handling */
407 void *error_opaque;
408 void (*error_func)(void *opaque, const char *msg);
409 int error_set_jmp_enabled;
410 jmp_buf error_jmp_buf;
411 int nb_errors;
413 /* tiny assembler state */
414 Sym *asm_labels;
416 /* see include_stack_ptr */
417 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
419 /* see ifdef_stack_ptr */
420 int ifdef_stack[IFDEF_STACK_SIZE];
423 /* The current value can be: */
424 #define VT_VALMASK 0x00ff
425 #define VT_CONST 0x00f0 /* constant in vc
426 (must be first non register value) */
427 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
428 #define VT_LOCAL 0x00f2 /* offset on stack */
429 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
430 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
431 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
432 #define VT_LVAL 0x0100 /* var is an lvalue */
433 #define VT_SYM 0x0200 /* a symbol value is added */
434 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
435 char/short stored in integer registers) */
436 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
437 dereferencing value */
438 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
439 bounding function call point is in vc */
440 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
441 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
442 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
443 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
445 /* types */
446 #define VT_INT 0 /* integer type */
447 #define VT_BYTE 1 /* signed byte type */
448 #define VT_SHORT 2 /* short type */
449 #define VT_VOID 3 /* void type */
450 #define VT_PTR 4 /* pointer */
451 #define VT_ENUM 5 /* enum definition */
452 #define VT_FUNC 6 /* function type */
453 #define VT_STRUCT 7 /* struct/union definition */
454 #define VT_FLOAT 8 /* IEEE float */
455 #define VT_DOUBLE 9 /* IEEE double */
456 #define VT_LDOUBLE 10 /* IEEE long double */
457 #define VT_BOOL 11 /* ISOC99 boolean type */
458 #define VT_LLONG 12 /* 64 bit integer */
459 #define VT_LONG 13 /* long integer (NEVER USED as type, only
460 during parsing) */
461 #define VT_BTYPE 0x000f /* mask for basic type */
462 #define VT_UNSIGNED 0x0010 /* unsigned type */
463 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
464 #define VT_BITFIELD 0x0040 /* bitfield modifier */
465 #define VT_CONSTANT 0x0800 /* const modifier */
466 #define VT_VOLATILE 0x1000 /* volatile modifier */
468 /* storage */
469 #define VT_EXTERN 0x00000080 /* extern definition */
470 #define VT_STATIC 0x00000100 /* static variable */
471 #define VT_TYPEDEF 0x00000200 /* typedef definition */
472 #define VT_INLINE 0x00000400 /* inline definition */
474 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
476 /* type mask (except storage) */
477 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
478 #define VT_TYPE (~(VT_STORAGE))
480 /* token values */
482 /* warning: the following compare tokens depend on i386 asm code */
483 #define TOK_ULT 0x92
484 #define TOK_UGE 0x93
485 #define TOK_EQ 0x94
486 #define TOK_NE 0x95
487 #define TOK_ULE 0x96
488 #define TOK_UGT 0x97
489 #define TOK_LT 0x9c
490 #define TOK_GE 0x9d
491 #define TOK_LE 0x9e
492 #define TOK_GT 0x9f
494 #define TOK_LAND 0xa0
495 #define TOK_LOR 0xa1
497 #define TOK_DEC 0xa2
498 #define TOK_MID 0xa3 /* inc/dec, to void constant */
499 #define TOK_INC 0xa4
500 #define TOK_UDIV 0xb0 /* unsigned division */
501 #define TOK_UMOD 0xb1 /* unsigned modulo */
502 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
503 #define TOK_CINT 0xb3 /* number in tokc */
504 #define TOK_CCHAR 0xb4 /* char constant in tokc */
505 #define TOK_STR 0xb5 /* pointer to string in tokc */
506 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
507 #define TOK_LCHAR 0xb7
508 #define TOK_LSTR 0xb8
509 #define TOK_CFLOAT 0xb9 /* float constant */
510 #define TOK_LINENUM 0xba /* line number info */
511 #define TOK_CDOUBLE 0xc0 /* double constant */
512 #define TOK_CLDOUBLE 0xc1 /* long double constant */
513 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
514 #define TOK_ADDC1 0xc3 /* add with carry generation */
515 #define TOK_ADDC2 0xc4 /* add with carry use */
516 #define TOK_SUBC1 0xc5 /* add with carry generation */
517 #define TOK_SUBC2 0xc6 /* add with carry use */
518 #define TOK_CUINT 0xc8 /* unsigned int constant */
519 #define TOK_CLLONG 0xc9 /* long long constant */
520 #define TOK_CULLONG 0xca /* unsigned long long constant */
521 #define TOK_ARROW 0xcb
522 #define TOK_DOTS 0xcc /* three dots */
523 #define TOK_SHR 0xcd /* unsigned shift right */
524 #define TOK_PPNUM 0xce /* preprocessor number */
526 #define TOK_SHL 0x01 /* shift left */
527 #define TOK_SAR 0x02 /* signed shift right */
529 /* assignement operators : normal operator or 0x80 */
530 #define TOK_A_MOD 0xa5
531 #define TOK_A_AND 0xa6
532 #define TOK_A_MUL 0xaa
533 #define TOK_A_ADD 0xab
534 #define TOK_A_SUB 0xad
535 #define TOK_A_DIV 0xaf
536 #define TOK_A_XOR 0xde
537 #define TOK_A_OR 0xfc
538 #define TOK_A_SHL 0x81
539 #define TOK_A_SAR 0x82
541 #ifndef offsetof
542 #define offsetof(type, field) ((size_t) &((type *)0)->field)
543 #endif
545 #ifndef countof
546 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
547 #endif
549 /* WARNING: the content of this string encodes token numbers */
550 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";
552 #define TOK_EOF (-1) /* end of file */
553 #define TOK_LINEFEED 10 /* line feed */
555 /* all identificators and strings have token above that */
556 #define TOK_IDENT 256
558 /* only used for i386 asm opcodes definitions */
559 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
561 #define DEF_BWL(x) \
562 DEF(TOK_ASM_ ## x ## b, #x "b") \
563 DEF(TOK_ASM_ ## x ## w, #x "w") \
564 DEF(TOK_ASM_ ## x ## l, #x "l") \
565 DEF(TOK_ASM_ ## x, #x)
567 #define DEF_WL(x) \
568 DEF(TOK_ASM_ ## x ## w, #x "w") \
569 DEF(TOK_ASM_ ## x ## l, #x "l") \
570 DEF(TOK_ASM_ ## x, #x)
572 #define DEF_FP1(x) \
573 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
574 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
575 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
576 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
578 #define DEF_FP(x) \
579 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
580 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
581 DEF_FP1(x)
583 #define DEF_ASMTEST(x) \
584 DEF_ASM(x ## o) \
585 DEF_ASM(x ## no) \
586 DEF_ASM(x ## b) \
587 DEF_ASM(x ## c) \
588 DEF_ASM(x ## nae) \
589 DEF_ASM(x ## nb) \
590 DEF_ASM(x ## nc) \
591 DEF_ASM(x ## ae) \
592 DEF_ASM(x ## e) \
593 DEF_ASM(x ## z) \
594 DEF_ASM(x ## ne) \
595 DEF_ASM(x ## nz) \
596 DEF_ASM(x ## be) \
597 DEF_ASM(x ## na) \
598 DEF_ASM(x ## nbe) \
599 DEF_ASM(x ## a) \
600 DEF_ASM(x ## s) \
601 DEF_ASM(x ## ns) \
602 DEF_ASM(x ## p) \
603 DEF_ASM(x ## pe) \
604 DEF_ASM(x ## np) \
605 DEF_ASM(x ## po) \
606 DEF_ASM(x ## l) \
607 DEF_ASM(x ## nge) \
608 DEF_ASM(x ## nl) \
609 DEF_ASM(x ## ge) \
610 DEF_ASM(x ## le) \
611 DEF_ASM(x ## ng) \
612 DEF_ASM(x ## nle) \
613 DEF_ASM(x ## g)
615 #define TOK_ASM_int TOK_INT
617 enum {
618 TOK_LAST = TOK_IDENT - 1,
619 #define DEF(id, str) id,
620 #include "tcctok.h"
621 #undef DEF
624 static const char tcc_keywords[] =
625 #define DEF(id, str) str "\0"
626 #include "tcctok.h"
627 #undef DEF
630 #define TOK_UIDENT TOK_DEFINE
632 #ifdef WIN32
633 #define snprintf _snprintf
634 #define vsnprintf _vsnprintf
635 #endif
637 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
638 /* currently incorrect */
639 long double strtold(const char *nptr, char **endptr)
641 return (long double)strtod(nptr, endptr);
643 float strtof(const char *nptr, char **endptr)
645 return (float)strtod(nptr, endptr);
647 #else
648 /* XXX: need to define this to use them in non ISOC99 context */
649 extern float strtof (const char *__nptr, char **__endptr);
650 extern long double strtold (const char *__nptr, char **__endptr);
651 #endif
653 static char *pstrcpy(char *buf, int buf_size, const char *s);
654 static char *pstrcat(char *buf, int buf_size, const char *s);
656 static void next(void);
657 static void next_nomacro(void);
658 static void parse_expr_type(CType *type);
659 static void expr_type(CType *type);
660 static void unary_type(CType *type);
661 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
662 int case_reg, int is_expr);
663 static int expr_const(void);
664 static void expr_eq(void);
665 static void gexpr(void);
666 static void decl(int l);
667 static void decl_initializer(CType *type, Section *sec, unsigned long c,
668 int first, int size_only);
669 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
670 int has_init, int v, int scope);
671 int gv(int rc);
672 void gv2(int rc1, int rc2);
673 void move_reg(int r, int s);
674 void save_regs(int n);
675 void save_reg(int r);
676 void vpop(void);
677 void vswap(void);
678 void vdup(void);
679 int get_reg(int rc);
681 static void macro_subst(TokenString *tok_str,
682 Sym **nested_list, const int *macro_str);
683 int save_reg_forced(int r);
684 void gen_op(int op);
685 void force_charshort_cast(int t);
686 static void gen_cast(CType *type);
687 void vstore(void);
688 static Sym *sym_find(int v);
689 static Sym *sym_push(int v, CType *type, int r, int c);
691 /* type handling */
692 static int type_size(CType *type, int *a);
693 static inline CType *pointed_type(CType *type);
694 static int pointed_size(CType *type);
695 static int lvalue_type(int t);
696 static int parse_btype(CType *type, AttributeDef *ad);
697 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
698 static int is_compatible_types(CType *type1, CType *type2);
700 void error(const char *fmt, ...);
701 void vpushi(int v);
702 void vset(CType *type, int r, int v);
703 void type_to_str(char *buf, int buf_size,
704 CType *type, const char *varstr);
705 char *get_tok_str(int v, CValue *cv);
706 static Sym *get_sym_ref(CType *type, Section *sec,
707 unsigned long offset, unsigned long size);
708 static Sym *external_global_sym(int v, CType *type, int r);
710 /* section generation */
711 static void section_realloc(Section *sec, unsigned long new_size);
712 static void *section_ptr_add(Section *sec, unsigned long size);
713 static void put_extern_sym(Sym *sym, Section *section,
714 unsigned long value, unsigned long size);
715 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
716 static int put_elf_str(Section *s, const char *sym);
717 static int put_elf_sym(Section *s,
718 unsigned long value, unsigned long size,
719 int info, int other, int shndx, const char *name);
720 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
721 int info, int sh_num, const char *name);
722 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
723 int type, int symbol);
724 static void put_stabs(const char *str, int type, int other, int desc,
725 unsigned long value);
726 static void put_stabs_r(const char *str, int type, int other, int desc,
727 unsigned long value, Section *sec, int sym_index);
728 static void put_stabn(int type, int other, int desc, int value);
729 static void put_stabd(int type, int other, int desc);
730 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
732 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
733 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
734 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
736 /* tccasm.c */
738 #ifdef CONFIG_TCC_ASM
740 typedef struct ExprValue {
741 uint32_t v;
742 Sym *sym;
743 } ExprValue;
745 #define MAX_ASM_OPERANDS 30
747 typedef struct ASMOperand {
748 int id; /* GCC 3 optionnal identifier (0 if number only supported */
749 char *constraint;
750 char asm_str[16]; /* computed asm string for operand */
751 SValue *vt; /* C value of the expression */
752 int ref_index; /* if >= 0, gives reference to a output constraint */
753 int priority; /* priority, used to assign registers */
754 int reg; /* if >= 0, register number used for this operand */
755 int is_llong; /* true if double register value */
756 } ASMOperand;
758 static void asm_expr(TCCState *s1, ExprValue *pe);
759 static int asm_int_expr(TCCState *s1);
760 static int find_constraint(ASMOperand *operands, int nb_operands,
761 const char *name, const char **pp);
763 static int tcc_assemble(TCCState *s1, int do_preprocess);
765 #endif
767 static void asm_instr(void);
769 /* true if float/double/long double type */
770 static inline int is_float(int t)
772 int bt;
773 bt = t & VT_BTYPE;
774 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
777 #ifdef TCC_TARGET_I386
778 #include "i386-gen.c"
779 #endif
781 #ifdef CONFIG_TCC_STATIC
783 #define RTLD_LAZY 0x001
784 #define RTLD_NOW 0x002
785 #define RTLD_GLOBAL 0x100
786 #define RTLD_DEFAULT NULL
788 /* dummy function for profiling */
789 void *dlopen(const char *filename, int flag)
791 return NULL;
794 const char *dlerror(void)
796 return "error";
799 typedef struct TCCSyms {
800 char *str;
801 void *ptr;
802 } TCCSyms;
804 #define TCCSYM(a) { #a, &a, },
806 /* add the symbol you want here if no dynamic linking is done */
807 static TCCSyms tcc_syms[] = {
808 TCCSYM(printf)
809 TCCSYM(fprintf)
810 TCCSYM(fopen)
811 TCCSYM(fclose)
812 { NULL, NULL },
815 void *dlsym(void *handle, const char *symbol)
817 TCCSyms *p;
818 p = tcc_syms;
819 while (p->str != NULL) {
820 if (!strcmp(p->str, symbol))
821 return p->ptr;
822 p++;
824 return NULL;
827 #endif
829 /********************************************************/
831 /* we use our own 'finite' function to avoid potential problems with
832 non standard math libs */
833 /* XXX: endianness dependent */
834 int ieee_finite(double d)
836 int *p = (int *)&d;
837 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
840 /* copy a string and truncate it. */
841 static char *pstrcpy(char *buf, int buf_size, const char *s)
843 char *q, *q_end;
844 int c;
846 if (buf_size > 0) {
847 q = buf;
848 q_end = buf + buf_size - 1;
849 while (q < q_end) {
850 c = *s++;
851 if (c == '\0')
852 break;
853 *q++ = c;
855 *q = '\0';
857 return buf;
860 /* strcat and truncate. */
861 static char *pstrcat(char *buf, int buf_size, const char *s)
863 int len;
864 len = strlen(buf);
865 if (len < buf_size)
866 pstrcpy(buf + len, buf_size - len, s);
867 return buf;
870 /* memory management */
871 #ifdef MEM_DEBUG
872 int mem_cur_size;
873 int mem_max_size;
874 #endif
876 static inline void tcc_free(void *ptr)
878 #ifdef MEM_DEBUG
879 mem_cur_size -= malloc_usable_size(ptr);
880 #endif
881 free(ptr);
884 static void *tcc_malloc(unsigned long size)
886 void *ptr;
887 ptr = malloc(size);
888 if (!ptr && size)
889 error("memory full");
890 #ifdef MEM_DEBUG
891 mem_cur_size += malloc_usable_size(ptr);
892 if (mem_cur_size > mem_max_size)
893 mem_max_size = mem_cur_size;
894 #endif
895 return ptr;
898 static void *tcc_mallocz(unsigned long size)
900 void *ptr;
901 ptr = tcc_malloc(size);
902 memset(ptr, 0, size);
903 return ptr;
906 static inline void *tcc_realloc(void *ptr, unsigned long size)
908 void *ptr1;
909 #ifdef MEM_DEBUG
910 mem_cur_size -= malloc_usable_size(ptr);
911 #endif
912 ptr1 = realloc(ptr, size);
913 #ifdef MEM_DEBUG
914 /* NOTE: count not correct if alloc error, but not critical */
915 mem_cur_size += malloc_usable_size(ptr1);
916 if (mem_cur_size > mem_max_size)
917 mem_max_size = mem_cur_size;
918 #endif
919 return ptr1;
922 static char *tcc_strdup(const char *str)
924 char *ptr;
925 ptr = tcc_malloc(strlen(str) + 1);
926 strcpy(ptr, str);
927 return ptr;
930 #define free(p) use_tcc_free(p)
931 #define malloc(s) use_tcc_malloc(s)
932 #define realloc(p, s) use_tcc_realloc(p, s)
934 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
936 int nb, nb_alloc;
937 void **pp;
939 nb = *nb_ptr;
940 pp = *ptab;
941 /* every power of two we double array size */
942 if ((nb & (nb - 1)) == 0) {
943 if (!nb)
944 nb_alloc = 1;
945 else
946 nb_alloc = nb * 2;
947 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
948 if (!pp)
949 error("memory full");
950 *ptab = pp;
952 pp[nb++] = data;
953 *nb_ptr = nb;
956 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
958 Section *sec;
960 sec = tcc_mallocz(sizeof(Section));
961 pstrcpy(sec->name, sizeof(sec->name), name);
962 sec->sh_type = sh_type;
963 sec->sh_flags = sh_flags;
964 switch(sh_type) {
965 case SHT_HASH:
966 case SHT_REL:
967 case SHT_DYNSYM:
968 case SHT_SYMTAB:
969 case SHT_DYNAMIC:
970 sec->sh_addralign = 4;
971 break;
972 case SHT_STRTAB:
973 sec->sh_addralign = 1;
974 break;
975 default:
976 sec->sh_addralign = 32; /* default conservative alignment */
977 break;
980 /* only add section if not private */
981 if (!(sh_flags & SHF_PRIVATE)) {
982 sec->sh_num = s1->nb_sections;
983 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
985 return sec;
988 static void free_section(Section *s)
990 tcc_free(s->data);
991 tcc_free(s);
994 /* realloc section and set its content to zero */
995 static void section_realloc(Section *sec, unsigned long new_size)
997 unsigned long size;
998 unsigned char *data;
1000 size = sec->data_allocated;
1001 if (size == 0)
1002 size = 1;
1003 while (size < new_size)
1004 size = size * 2;
1005 data = tcc_realloc(sec->data, size);
1006 if (!data)
1007 error("memory full");
1008 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1009 sec->data = data;
1010 sec->data_allocated = size;
1013 /* reserve at least 'size' bytes in section 'sec' from
1014 sec->data_offset. */
1015 static void *section_ptr_add(Section *sec, unsigned long size)
1017 unsigned long offset, offset1;
1019 offset = sec->data_offset;
1020 offset1 = offset + size;
1021 if (offset1 > sec->data_allocated)
1022 section_realloc(sec, offset1);
1023 sec->data_offset = offset1;
1024 return sec->data + offset;
1027 /* return a reference to a section, and create it if it does not
1028 exists */
1029 Section *find_section(TCCState *s1, const char *name)
1031 Section *sec;
1032 int i;
1033 for(i = 1; i < s1->nb_sections; i++) {
1034 sec = s1->sections[i];
1035 if (!strcmp(name, sec->name))
1036 return sec;
1038 /* sections are created as PROGBITS */
1039 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1042 /* update sym->c so that it points to an external symbol in section
1043 'section' with value 'value' */
1044 static void put_extern_sym(Sym *sym, Section *section,
1045 unsigned long value, unsigned long size)
1047 int sym_type, sym_bind, sh_num, info;
1048 Elf32_Sym *esym;
1049 const char *name;
1051 if (section)
1052 sh_num = section->sh_num;
1053 else
1054 sh_num = SHN_UNDEF;
1055 if (!sym->c) {
1056 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1057 sym_type = STT_FUNC;
1058 else
1059 sym_type = STT_OBJECT;
1060 if (sym->type.t & VT_STATIC)
1061 sym_bind = STB_LOCAL;
1062 else
1063 sym_bind = STB_GLOBAL;
1065 name = get_tok_str(sym->v, NULL);
1066 #ifdef CONFIG_TCC_BCHECK
1067 if (do_bounds_check) {
1068 char buf[32];
1070 /* XXX: avoid doing that for statics ? */
1071 /* if bound checking is activated, we change some function
1072 names by adding the "__bound" prefix */
1073 switch(sym->v) {
1074 #if 0
1075 /* XXX: we rely only on malloc hooks */
1076 case TOK_malloc:
1077 case TOK_free:
1078 case TOK_realloc:
1079 case TOK_memalign:
1080 case TOK_calloc:
1081 #endif
1082 case TOK_memcpy:
1083 case TOK_memmove:
1084 case TOK_memset:
1085 case TOK_strlen:
1086 case TOK_strcpy:
1087 strcpy(buf, "__bound_");
1088 strcat(buf, name);
1089 name = buf;
1090 break;
1093 #endif
1094 info = ELF32_ST_INFO(sym_bind, sym_type);
1095 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1096 } else {
1097 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1098 esym->st_value = value;
1099 esym->st_size = size;
1100 esym->st_shndx = sh_num;
1104 /* add a new relocation entry to symbol 'sym' in section 's' */
1105 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1107 if (!sym->c)
1108 put_extern_sym(sym, NULL, 0, 0);
1109 /* now we can add ELF relocation info */
1110 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1113 static inline int isid(int c)
1115 return (c >= 'a' && c <= 'z') ||
1116 (c >= 'A' && c <= 'Z') ||
1117 c == '_';
1120 static inline int isnum(int c)
1122 return c >= '0' && c <= '9';
1125 static inline int isoct(int c)
1127 return c >= '0' && c <= '7';
1130 static inline int toup(int c)
1132 if (c >= 'a' && c <= 'z')
1133 return c - 'a' + 'A';
1134 else
1135 return c;
1138 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1140 int len;
1141 len = strlen(buf);
1142 vsnprintf(buf + len, buf_size - len, fmt, ap);
1145 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1147 va_list ap;
1148 va_start(ap, fmt);
1149 strcat_vprintf(buf, buf_size, fmt, ap);
1150 va_end(ap);
1153 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1155 char buf[2048];
1156 BufferedFile **f;
1158 buf[0] = '\0';
1159 if (file) {
1160 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1161 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1162 (*f)->filename, (*f)->line_num);
1163 if (file->line_num > 0) {
1164 strcat_printf(buf, sizeof(buf),
1165 "%s:%d: ", file->filename, file->line_num);
1166 } else {
1167 strcat_printf(buf, sizeof(buf),
1168 "%s: ", file->filename);
1170 } else {
1171 strcat_printf(buf, sizeof(buf),
1172 "tcc: ");
1174 if (is_warning)
1175 strcat_printf(buf, sizeof(buf), "warning: ");
1176 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1178 if (!s1->error_func) {
1179 /* default case: stderr */
1180 fprintf(stderr, "%s\n", buf);
1181 } else {
1182 s1->error_func(s1->error_opaque, buf);
1184 if (!is_warning || s1->warn_error)
1185 s1->nb_errors++;
1188 #ifdef LIBTCC
1189 void tcc_set_error_func(TCCState *s, void *error_opaque,
1190 void (*error_func)(void *opaque, const char *msg))
1192 s->error_opaque = error_opaque;
1193 s->error_func = error_func;
1195 #endif
1197 /* error without aborting current compilation */
1198 void error_noabort(const char *fmt, ...)
1200 TCCState *s1 = tcc_state;
1201 va_list ap;
1203 va_start(ap, fmt);
1204 error1(s1, 0, fmt, ap);
1205 va_end(ap);
1208 void error(const char *fmt, ...)
1210 TCCState *s1 = tcc_state;
1211 va_list ap;
1213 va_start(ap, fmt);
1214 error1(s1, 0, fmt, ap);
1215 va_end(ap);
1216 /* better than nothing: in some cases, we accept to handle errors */
1217 if (s1->error_set_jmp_enabled) {
1218 longjmp(s1->error_jmp_buf, 1);
1219 } else {
1220 /* XXX: eliminate this someday */
1221 exit(1);
1225 void expect(const char *msg)
1227 error("%s expected", msg);
1230 void warning(const char *fmt, ...)
1232 TCCState *s1 = tcc_state;
1233 va_list ap;
1235 va_start(ap, fmt);
1236 error1(s1, 1, fmt, ap);
1237 va_end(ap);
1240 void skip(int c)
1242 if (tok != c)
1243 error("'%c' expected", c);
1244 next();
1247 static void test_lvalue(void)
1249 if (!(vtop->r & VT_LVAL))
1250 expect("lvalue");
1253 /* allocate a new token */
1254 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1256 TokenSym *ts, **ptable;
1257 int i;
1259 if (tok_ident >= SYM_FIRST_ANOM)
1260 error("memory full");
1262 /* expand token table if needed */
1263 i = tok_ident - TOK_IDENT;
1264 if ((i % TOK_ALLOC_INCR) == 0) {
1265 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1266 if (!ptable)
1267 error("memory full");
1268 table_ident = ptable;
1271 ts = tcc_malloc(sizeof(TokenSym) + len);
1272 table_ident[i] = ts;
1273 ts->tok = tok_ident++;
1274 ts->sym_define = NULL;
1275 ts->sym_label = NULL;
1276 ts->sym_struct = NULL;
1277 ts->sym_identifier = NULL;
1278 ts->len = len;
1279 ts->hash_next = NULL;
1280 memcpy(ts->str, str, len);
1281 ts->str[len] = '\0';
1282 *pts = ts;
1283 return ts;
1286 #define TOK_HASH_INIT 1
1287 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1289 /* find a token and add it if not found */
1290 static TokenSym *tok_alloc(const char *str, int len)
1292 TokenSym *ts, **pts;
1293 int i;
1294 unsigned int h;
1296 h = TOK_HASH_INIT;
1297 for(i=0;i<len;i++)
1298 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1299 h &= (TOK_HASH_SIZE - 1);
1301 pts = &hash_ident[h];
1302 for(;;) {
1303 ts = *pts;
1304 if (!ts)
1305 break;
1306 if (ts->len == len && !memcmp(ts->str, str, len))
1307 return ts;
1308 pts = &(ts->hash_next);
1310 return tok_alloc_new(pts, str, len);
1313 /* CString handling */
1315 static void cstr_realloc(CString *cstr, int new_size)
1317 int size;
1318 void *data;
1320 size = cstr->size_allocated;
1321 if (size == 0)
1322 size = 8; /* no need to allocate a too small first string */
1323 while (size < new_size)
1324 size = size * 2;
1325 data = tcc_realloc(cstr->data_allocated, size);
1326 if (!data)
1327 error("memory full");
1328 cstr->data_allocated = data;
1329 cstr->size_allocated = size;
1330 cstr->data = data;
1333 /* add a byte */
1334 static void cstr_ccat(CString *cstr, int ch)
1336 int size;
1337 size = cstr->size + 1;
1338 if (size > cstr->size_allocated)
1339 cstr_realloc(cstr, size);
1340 ((unsigned char *)cstr->data)[size - 1] = ch;
1341 cstr->size = size;
1344 static void cstr_cat(CString *cstr, const char *str)
1346 int c;
1347 for(;;) {
1348 c = *str;
1349 if (c == '\0')
1350 break;
1351 cstr_ccat(cstr, c);
1352 str++;
1356 /* add a wide char */
1357 static void cstr_wccat(CString *cstr, int ch)
1359 int size;
1360 size = cstr->size + sizeof(int);
1361 if (size > cstr->size_allocated)
1362 cstr_realloc(cstr, size);
1363 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1364 cstr->size = size;
1367 static void cstr_new(CString *cstr)
1369 memset(cstr, 0, sizeof(CString));
1372 /* free string and reset it to NULL */
1373 static void cstr_free(CString *cstr)
1375 tcc_free(cstr->data_allocated);
1376 cstr_new(cstr);
1379 #define cstr_reset(cstr) cstr_free(cstr)
1381 static CString *cstr_dup(CString *cstr1)
1383 CString *cstr;
1384 int size;
1386 cstr = tcc_malloc(sizeof(CString));
1387 size = cstr1->size;
1388 cstr->size = size;
1389 cstr->size_allocated = size;
1390 cstr->data_allocated = tcc_malloc(size);
1391 cstr->data = cstr->data_allocated;
1392 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1393 return cstr;
1396 /* XXX: unicode ? */
1397 static void add_char(CString *cstr, int c)
1399 if (c == '\'' || c == '\"' || c == '\\') {
1400 /* XXX: could be more precise if char or string */
1401 cstr_ccat(cstr, '\\');
1403 if (c >= 32 && c <= 126) {
1404 cstr_ccat(cstr, c);
1405 } else {
1406 cstr_ccat(cstr, '\\');
1407 if (c == '\n') {
1408 cstr_ccat(cstr, 'n');
1409 } else {
1410 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1411 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1412 cstr_ccat(cstr, '0' + (c & 7));
1417 /* XXX: buffer overflow */
1418 /* XXX: float tokens */
1419 char *get_tok_str(int v, CValue *cv)
1421 static char buf[STRING_MAX_SIZE + 1];
1422 static CString cstr_buf;
1423 CString *cstr;
1424 unsigned char *q;
1425 char *p;
1426 int i, len;
1428 /* NOTE: to go faster, we give a fixed buffer for small strings */
1429 cstr_reset(&cstr_buf);
1430 cstr_buf.data = buf;
1431 cstr_buf.size_allocated = sizeof(buf);
1432 p = buf;
1434 switch(v) {
1435 case TOK_CINT:
1436 case TOK_CUINT:
1437 /* XXX: not quite exact, but only useful for testing */
1438 sprintf(p, "%u", cv->ui);
1439 break;
1440 case TOK_CLLONG:
1441 case TOK_CULLONG:
1442 /* XXX: not quite exact, but only useful for testing */
1443 sprintf(p, "%Lu", cv->ull);
1444 break;
1445 case TOK_CCHAR:
1446 case TOK_LCHAR:
1447 cstr_ccat(&cstr_buf, '\'');
1448 add_char(&cstr_buf, cv->i);
1449 cstr_ccat(&cstr_buf, '\'');
1450 cstr_ccat(&cstr_buf, '\0');
1451 break;
1452 case TOK_PPNUM:
1453 cstr = cv->cstr;
1454 len = cstr->size - 1;
1455 for(i=0;i<len;i++)
1456 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1457 cstr_ccat(&cstr_buf, '\0');
1458 break;
1459 case TOK_STR:
1460 case TOK_LSTR:
1461 cstr = cv->cstr;
1462 cstr_ccat(&cstr_buf, '\"');
1463 if (v == TOK_STR) {
1464 len = cstr->size - 1;
1465 for(i=0;i<len;i++)
1466 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1467 } else {
1468 len = (cstr->size / sizeof(int)) - 1;
1469 for(i=0;i<len;i++)
1470 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1472 cstr_ccat(&cstr_buf, '\"');
1473 cstr_ccat(&cstr_buf, '\0');
1474 break;
1475 case TOK_LT:
1476 v = '<';
1477 goto addv;
1478 case TOK_GT:
1479 v = '>';
1480 goto addv;
1481 case TOK_A_SHL:
1482 return strcpy(p, "<<=");
1483 case TOK_A_SAR:
1484 return strcpy(p, ">>=");
1485 default:
1486 if (v < TOK_IDENT) {
1487 /* search in two bytes table */
1488 q = tok_two_chars;
1489 while (*q) {
1490 if (q[2] == v) {
1491 *p++ = q[0];
1492 *p++ = q[1];
1493 *p = '\0';
1494 return buf;
1496 q += 3;
1498 addv:
1499 *p++ = v;
1500 *p = '\0';
1501 } else if (v < tok_ident) {
1502 return table_ident[v - TOK_IDENT]->str;
1503 } else if (v >= SYM_FIRST_ANOM) {
1504 /* special name for anonymous symbol */
1505 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1506 } else {
1507 /* should never happen */
1508 return NULL;
1510 break;
1512 return cstr_buf.data;
1515 /* push, without hashing */
1516 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1518 Sym *s;
1519 s = tcc_malloc(sizeof(Sym));
1520 s->v = v;
1521 s->type.t = t;
1522 s->c = c;
1523 s->next = NULL;
1524 /* add in stack */
1525 s->prev = *ps;
1526 *ps = s;
1527 return s;
1530 /* find a symbol and return its associated structure. 's' is the top
1531 of the symbol stack */
1532 static Sym *sym_find2(Sym *s, int v)
1534 while (s) {
1535 if (s->v == v)
1536 return s;
1537 s = s->prev;
1539 return NULL;
1542 /* structure lookup */
1543 static inline Sym *struct_find(int v)
1545 v -= TOK_IDENT;
1546 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1547 return NULL;
1548 return table_ident[v]->sym_struct;
1551 /* find an identifier */
1552 static inline Sym *sym_find(int v)
1554 v -= TOK_IDENT;
1555 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1556 return NULL;
1557 return table_ident[v]->sym_identifier;
1560 /* push a given symbol on the symbol stack */
1561 static Sym *sym_push(int v, CType *type, int r, int c)
1563 Sym *s, **ps;
1564 TokenSym *ts;
1566 if (local_stack)
1567 ps = &local_stack;
1568 else
1569 ps = &global_stack;
1570 s = sym_push2(ps, v, type->t, c);
1571 s->type.ref = type->ref;
1572 s->r = r;
1573 /* don't record fields or anonymous symbols */
1574 /* XXX: simplify */
1575 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1576 /* record symbol in token array */
1577 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1578 if (v & SYM_STRUCT)
1579 ps = &ts->sym_struct;
1580 else
1581 ps = &ts->sym_identifier;
1582 s->prev_tok = *ps;
1583 *ps = s;
1585 return s;
1588 /* push a global identifier */
1589 static Sym *global_identifier_push(int v, int t, int c)
1591 Sym *s, **ps;
1592 s = sym_push2(&global_stack, v, t, c);
1593 /* don't record anonymous symbol */
1594 if (v < SYM_FIRST_ANOM) {
1595 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1596 /* modify the top most local identifier, so that
1597 sym_identifier will point to 's' when popped */
1598 while (*ps != NULL)
1599 ps = &(*ps)->prev_tok;
1600 s->prev_tok = NULL;
1601 *ps = s;
1603 return s;
1606 /* pop symbols until top reaches 'b' */
1607 static void sym_pop(Sym **ptop, Sym *b)
1609 Sym *s, *ss, **ps;
1610 TokenSym *ts;
1611 int v;
1613 s = *ptop;
1614 while(s != b) {
1615 ss = s->prev;
1616 v = s->v;
1617 /* remove symbol in token array */
1618 /* XXX: simplify */
1619 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1620 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1621 if (v & SYM_STRUCT)
1622 ps = &ts->sym_struct;
1623 else
1624 ps = &ts->sym_identifier;
1625 *ps = s->prev_tok;
1627 tcc_free(s);
1628 s = ss;
1630 *ptop = b;
1633 /* I/O layer */
1635 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1637 int fd;
1638 BufferedFile *bf;
1640 fd = open(filename, O_RDONLY);
1641 if (fd < 0)
1642 return NULL;
1643 bf = tcc_malloc(sizeof(BufferedFile));
1644 if (!bf) {
1645 close(fd);
1646 return NULL;
1648 bf->fd = fd;
1649 bf->buf_ptr = bf->buffer;
1650 bf->buf_end = bf->buffer;
1651 bf->buffer[0] = CH_EOB; /* put eob symbol */
1652 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1653 bf->line_num = 1;
1654 bf->ifndef_macro = 0;
1655 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1656 // printf("opening '%s'\n", filename);
1657 return bf;
1660 void tcc_close(BufferedFile *bf)
1662 total_lines += bf->line_num;
1663 close(bf->fd);
1664 tcc_free(bf);
1667 /* fill input buffer and peek next char */
1668 static int tcc_peekc_slow(BufferedFile *bf)
1670 int len;
1671 /* only tries to read if really end of buffer */
1672 if (bf->buf_ptr >= bf->buf_end) {
1673 if (bf->fd != -1) {
1674 #if defined(PARSE_DEBUG)
1675 len = 8;
1676 #else
1677 len = IO_BUF_SIZE;
1678 #endif
1679 len = read(bf->fd, bf->buffer, len);
1680 if (len < 0)
1681 len = 0;
1682 } else {
1683 len = 0;
1685 total_bytes += len;
1686 bf->buf_ptr = bf->buffer;
1687 bf->buf_end = bf->buffer + len;
1688 *bf->buf_end = CH_EOB;
1690 if (bf->buf_ptr < bf->buf_end) {
1691 return bf->buf_ptr[0];
1692 } else {
1693 bf->buf_ptr = bf->buf_end;
1694 return CH_EOF;
1698 /* return the current character, handling end of block if necessary
1699 (but not stray) */
1700 static int handle_eob(void)
1702 return tcc_peekc_slow(file);
1705 /* read next char from current input file and handle end of input buffer */
1706 static inline void inp(void)
1708 ch = *(++(file->buf_ptr));
1709 /* end of buffer/file handling */
1710 if (ch == CH_EOB)
1711 ch = handle_eob();
1714 /* handle '\[\r]\n' */
1715 static void handle_stray(void)
1717 while (ch == '\\') {
1718 inp();
1719 if (ch == '\n') {
1720 file->line_num++;
1721 inp();
1722 } else if (ch == '\r') {
1723 inp();
1724 if (ch != '\n')
1725 goto fail;
1726 file->line_num++;
1727 inp();
1728 } else {
1729 fail:
1730 error("stray '\\' in program");
1735 /* skip the stray and handle the \\n case. Output an error if
1736 incorrect char after the stray */
1737 static int handle_stray1(uint8_t *p)
1739 int c;
1741 if (p >= file->buf_end) {
1742 file->buf_ptr = p;
1743 c = handle_eob();
1744 p = file->buf_ptr;
1745 if (c == '\\')
1746 goto parse_stray;
1747 } else {
1748 parse_stray:
1749 file->buf_ptr = p;
1750 ch = *p;
1751 handle_stray();
1752 p = file->buf_ptr;
1753 c = *p;
1755 return c;
1758 /* handle just the EOB case, but not stray */
1759 #define PEEKC_EOB(c, p)\
1761 p++;\
1762 c = *p;\
1763 if (c == '\\') {\
1764 file->buf_ptr = p;\
1765 c = handle_eob();\
1766 p = file->buf_ptr;\
1770 /* handle the complicated stray case */
1771 #define PEEKC(c, p)\
1773 p++;\
1774 c = *p;\
1775 if (c == '\\') {\
1776 c = handle_stray1(p);\
1777 p = file->buf_ptr;\
1781 /* input with '\[\r]\n' handling. Note that this function cannot
1782 handle other characters after '\', so you cannot call it inside
1783 strings or comments */
1784 static void minp(void)
1786 inp();
1787 if (ch == '\\')
1788 handle_stray();
1792 /* single line C++ comments */
1793 static uint8_t *parse_line_comment(uint8_t *p)
1795 int c;
1797 p++;
1798 for(;;) {
1799 c = *p;
1800 if (c == '\n' || c == CH_EOF) {
1801 break;
1802 } else if (c == '\\') {
1803 PEEKC_EOB(c, p);
1804 if (c == '\n') {
1805 file->line_num++;
1806 PEEKC_EOB(c, p);
1807 } else if (c == '\r') {
1808 PEEKC_EOB(c, p);
1809 if (c == '\n') {
1810 file->line_num++;
1811 PEEKC_EOB(c, p);
1814 } else {
1815 p++;
1818 return p;
1821 /* C comments */
1822 static uint8_t *parse_comment(uint8_t *p)
1824 int c;
1826 p++;
1827 for(;;) {
1828 /* fast skip loop */
1829 for(;;) {
1830 c = *p;
1831 if (c == '\n' || c == '*' || c == '\\')
1832 break;
1833 p++;
1834 c = *p;
1835 if (c == '\n' || c == '*' || c == '\\')
1836 break;
1837 p++;
1839 /* now we can handle all the cases */
1840 if (c == '\n') {
1841 file->line_num++;
1842 p++;
1843 } else if (c == '*') {
1844 p++;
1845 for(;;) {
1846 c = *p;
1847 if (c == '*') {
1848 p++;
1849 } else if (c == '/') {
1850 goto end_of_comment;
1851 } else if (c == '\\') {
1852 file->buf_ptr = p;
1853 c = handle_eob();
1854 p = file->buf_ptr;
1855 if (c == '\\') {
1856 /* skip '\[\r]\n', otherwise just skip the stray */
1857 while (c == '\\') {
1858 PEEKC_EOB(c, p);
1859 if (c == '\n') {
1860 file->line_num++;
1861 PEEKC_EOB(c, p);
1862 } else if (c == '\r') {
1863 PEEKC_EOB(c, p);
1864 if (c == '\n') {
1865 file->line_num++;
1866 PEEKC_EOB(c, p);
1868 } else {
1869 goto after_star;
1873 } else {
1874 break;
1877 after_star: ;
1878 } else {
1879 /* stray, eob or eof */
1880 file->buf_ptr = p;
1881 c = handle_eob();
1882 p = file->buf_ptr;
1883 if (c == CH_EOF) {
1884 error("unexpected end of file in comment");
1885 } else if (c == '\\') {
1886 p++;
1890 end_of_comment:
1891 p++;
1892 return p;
1895 #define cinp minp
1897 /* space exlcuding newline */
1898 static inline int is_space(int ch)
1900 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1903 static inline void skip_spaces(void)
1905 while (is_space(ch))
1906 cinp();
1909 /* parse a string without interpreting escapes */
1910 static uint8_t *parse_pp_string(uint8_t *p,
1911 int sep, CString *str)
1913 int c;
1914 p++;
1915 for(;;) {
1916 c = *p;
1917 if (c == sep) {
1918 break;
1919 } else if (c == '\\') {
1920 file->buf_ptr = p;
1921 c = handle_eob();
1922 p = file->buf_ptr;
1923 if (c == CH_EOF) {
1924 unterminated_string:
1925 /* XXX: indicate line number of start of string */
1926 error("missing terminating %c character", sep);
1927 } else if (c == '\\') {
1928 /* escape : just skip \[\r]\n */
1929 PEEKC_EOB(c, p);
1930 if (c == '\n') {
1931 file->line_num++;
1932 p++;
1933 } else if (c == '\r') {
1934 PEEKC_EOB(c, p);
1935 if (c != '\n')
1936 expect("'\n' after '\r'");
1937 file->line_num++;
1938 p++;
1939 } else if (c == CH_EOF) {
1940 goto unterminated_string;
1941 } else {
1942 if (str) {
1943 cstr_ccat(str, '\\');
1944 cstr_ccat(str, c);
1946 p++;
1949 } else if (c == '\n') {
1950 file->line_num++;
1951 goto add_char;
1952 } else if (c == '\r') {
1953 PEEKC_EOB(c, p);
1954 if (c != '\n') {
1955 cstr_ccat(str, '\r');
1956 } else {
1957 file->line_num++;
1958 goto add_char;
1960 } else {
1961 add_char:
1962 if (str)
1963 cstr_ccat(str, c);
1964 p++;
1967 p++;
1968 return p;
1971 /* skip block of text until #else, #elif or #endif. skip also pairs of
1972 #if/#endif */
1973 void preprocess_skip(void)
1975 int a, start_of_line, c;
1976 uint8_t *p;
1978 p = file->buf_ptr;
1979 start_of_line = 1;
1980 a = 0;
1981 for(;;) {
1982 redo_no_start:
1983 c = *p;
1984 switch(c) {
1985 case ' ':
1986 case '\t':
1987 case '\f':
1988 case '\v':
1989 case '\r':
1990 p++;
1991 goto redo_no_start;
1992 case '\n':
1993 start_of_line = 1;
1994 file->line_num++;
1995 p++;
1996 goto redo_no_start;
1997 case '\\':
1998 file->buf_ptr = p;
1999 c = handle_eob();
2000 if (c == CH_EOF) {
2001 expect("#endif");
2002 } else if (c == '\\') {
2003 /* XXX: incorrect: should not give an error */
2004 ch = file->buf_ptr[0];
2005 handle_stray();
2007 p = file->buf_ptr;
2008 goto redo_no_start;
2009 /* skip strings */
2010 case '\"':
2011 case '\'':
2012 p = parse_pp_string(p, c, NULL);
2013 break;
2014 /* skip comments */
2015 case '/':
2016 file->buf_ptr = p;
2017 ch = *p;
2018 minp();
2019 p = file->buf_ptr;
2020 if (ch == '*') {
2021 p = parse_comment(p);
2022 } else if (ch == '/') {
2023 p = parse_line_comment(p);
2025 break;
2027 case '#':
2028 p++;
2029 if (start_of_line) {
2030 file->buf_ptr = p;
2031 next_nomacro();
2032 p = file->buf_ptr;
2033 if (a == 0 &&
2034 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2035 goto the_end;
2036 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2037 a++;
2038 else if (tok == TOK_ENDIF)
2039 a--;
2041 break;
2042 default:
2043 p++;
2044 break;
2046 start_of_line = 0;
2048 the_end: ;
2049 file->buf_ptr = p;
2052 /* ParseState handling */
2054 /* XXX: currently, no include file info is stored. Thus, we cannot display
2055 accurate messages if the function or data definition spans multiple
2056 files */
2058 /* save current parse state in 's' */
2059 void save_parse_state(ParseState *s)
2061 s->line_num = file->line_num;
2062 s->macro_ptr = macro_ptr;
2063 s->tok = tok;
2064 s->tokc = tokc;
2067 /* restore parse state from 's' */
2068 void restore_parse_state(ParseState *s)
2070 file->line_num = s->line_num;
2071 macro_ptr = s->macro_ptr;
2072 tok = s->tok;
2073 tokc = s->tokc;
2076 /* return the number of additional 'ints' necessary to store the
2077 token */
2078 static inline int tok_ext_size(int t)
2080 switch(t) {
2081 /* 4 bytes */
2082 case TOK_CINT:
2083 case TOK_CUINT:
2084 case TOK_CCHAR:
2085 case TOK_LCHAR:
2086 case TOK_STR:
2087 case TOK_LSTR:
2088 case TOK_CFLOAT:
2089 case TOK_LINENUM:
2090 case TOK_PPNUM:
2091 return 1;
2092 case TOK_CDOUBLE:
2093 case TOK_CLLONG:
2094 case TOK_CULLONG:
2095 return 2;
2096 case TOK_CLDOUBLE:
2097 return LDOUBLE_SIZE / 4;
2098 default:
2099 return 0;
2103 /* token string handling */
2105 static inline void tok_str_new(TokenString *s)
2107 s->str = NULL;
2108 s->len = 0;
2109 s->allocated_len = 0;
2110 s->last_line_num = -1;
2113 static void tok_str_free(int *str)
2115 const int *p;
2116 CString *cstr;
2117 int t;
2119 p = str;
2120 for(;;) {
2121 t = *p;
2122 /* NOTE: we test zero separately so that GCC can generate a
2123 table for the following switch */
2124 if (t == 0)
2125 break;
2126 switch(t) {
2127 case TOK_CINT:
2128 case TOK_CUINT:
2129 case TOK_CCHAR:
2130 case TOK_LCHAR:
2131 case TOK_CFLOAT:
2132 case TOK_LINENUM:
2133 p += 2;
2134 break;
2135 case TOK_PPNUM:
2136 case TOK_STR:
2137 case TOK_LSTR:
2138 /* XXX: use a macro to be portable on 64 bit ? */
2139 cstr = (CString *)p[1];
2140 cstr_free(cstr);
2141 tcc_free(cstr);
2142 p += 2;
2143 break;
2144 case TOK_CDOUBLE:
2145 case TOK_CLLONG:
2146 case TOK_CULLONG:
2147 p += 3;
2148 break;
2149 case TOK_CLDOUBLE:
2150 p += 1 + (LDOUBLE_SIZE / 4);
2151 break;
2152 default:
2153 p++;
2154 break;
2157 tcc_free(str);
2160 static int *tok_str_realloc(TokenString *s)
2162 int *str, len;
2164 len = s->allocated_len + TOK_STR_ALLOC_INCR;
2165 str = tcc_realloc(s->str, len * sizeof(int));
2166 if (!str)
2167 error("memory full");
2168 s->allocated_len = len;
2169 s->str = str;
2170 return str;
2173 static void tok_str_add(TokenString *s, int t)
2175 int len, *str;
2177 len = s->len;
2178 str = s->str;
2179 if (len >= s->allocated_len)
2180 str = tok_str_realloc(s);
2181 str[len++] = t;
2182 s->len = len;
2185 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2187 int len, *str;
2189 len = s->len;
2190 str = s->str;
2192 /* allocate space for worst case */
2193 if (len + TOK_MAX_SIZE > s->allocated_len)
2194 str = tok_str_realloc(s);
2195 str[len++] = t;
2196 switch(t) {
2197 case TOK_CINT:
2198 case TOK_CUINT:
2199 case TOK_CCHAR:
2200 case TOK_LCHAR:
2201 case TOK_CFLOAT:
2202 case TOK_LINENUM:
2203 str[len++] = cv->tab[0];
2204 break;
2205 case TOK_PPNUM:
2206 case TOK_STR:
2207 case TOK_LSTR:
2208 str[len++] = (int)cstr_dup(cv->cstr);
2209 break;
2210 case TOK_CDOUBLE:
2211 case TOK_CLLONG:
2212 case TOK_CULLONG:
2213 str[len++] = cv->tab[0];
2214 str[len++] = cv->tab[1];
2215 break;
2216 case TOK_CLDOUBLE:
2217 #if LDOUBLE_SIZE == 12
2218 str[len++] = cv->tab[0];
2219 str[len++] = cv->tab[1];
2220 str[len++] = cv->tab[2];
2221 #else
2222 #error add long double size support
2223 #endif
2224 break;
2225 default:
2226 break;
2228 s->len = len;
2231 /* add the current parse token in token string 's' */
2232 static void tok_str_add_tok(TokenString *s)
2234 CValue cval;
2236 /* save line number info */
2237 if (file->line_num != s->last_line_num) {
2238 s->last_line_num = file->line_num;
2239 cval.i = s->last_line_num;
2240 tok_str_add2(s, TOK_LINENUM, &cval);
2242 tok_str_add2(s, tok, &tokc);
2245 #if LDOUBLE_SIZE == 12
2246 #define LDOUBLE_GET(p, cv) \
2247 cv.tab[0] = p[0]; \
2248 cv.tab[1] = p[1]; \
2249 cv.tab[2] = p[2];
2250 #else
2251 #error add long double size support
2252 #endif
2255 /* get a token from an integer array and increment pointer
2256 accordingly. we code it as a macro to avoid pointer aliasing. */
2257 #define TOK_GET(t, p, cv) \
2259 t = *p++; \
2260 switch(t) { \
2261 case TOK_CINT: \
2262 case TOK_CUINT: \
2263 case TOK_CCHAR: \
2264 case TOK_LCHAR: \
2265 case TOK_CFLOAT: \
2266 case TOK_LINENUM: \
2267 case TOK_STR: \
2268 case TOK_LSTR: \
2269 case TOK_PPNUM: \
2270 cv.tab[0] = *p++; \
2271 break; \
2272 case TOK_CDOUBLE: \
2273 case TOK_CLLONG: \
2274 case TOK_CULLONG: \
2275 cv.tab[0] = p[0]; \
2276 cv.tab[1] = p[1]; \
2277 p += 2; \
2278 break; \
2279 case TOK_CLDOUBLE: \
2280 LDOUBLE_GET(p, cv); \
2281 p += LDOUBLE_SIZE / 4; \
2282 break; \
2283 default: \
2284 break; \
2288 /* defines handling */
2289 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2291 Sym *s;
2293 s = sym_push2(&define_stack, v, macro_type, (int)str);
2294 s->next = first_arg;
2295 table_ident[v - TOK_IDENT]->sym_define = s;
2298 /* undefined a define symbol. Its name is just set to zero */
2299 static void define_undef(Sym *s)
2301 int v;
2302 v = s->v;
2303 if (v >= TOK_IDENT && v < tok_ident)
2304 table_ident[v - TOK_IDENT]->sym_define = NULL;
2305 s->v = 0;
2308 static inline Sym *define_find(int v)
2310 v -= TOK_IDENT;
2311 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2312 return NULL;
2313 return table_ident[v]->sym_define;
2316 /* free define stack until top reaches 'b' */
2317 static void free_defines(Sym *b)
2319 Sym *top, *top1;
2320 int v;
2322 top = define_stack;
2323 while (top != b) {
2324 top1 = top->prev;
2325 /* do not free args or predefined defines */
2326 if (top->c)
2327 tok_str_free((int *)top->c);
2328 v = top->v;
2329 if (v >= TOK_IDENT && v < tok_ident)
2330 table_ident[v - TOK_IDENT]->sym_define = NULL;
2331 tcc_free(top);
2332 top = top1;
2334 define_stack = b;
2337 /* label lookup */
2338 static Sym *label_find(int v)
2340 v -= TOK_IDENT;
2341 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2342 return NULL;
2343 return table_ident[v]->sym_label;
2346 static Sym *label_push(Sym **ptop, int v, int flags)
2348 Sym *s, **ps;
2349 s = sym_push2(ptop, v, 0, 0);
2350 s->r = flags;
2351 ps = &table_ident[v - TOK_IDENT]->sym_label;
2352 if (ptop == &global_label_stack) {
2353 /* modify the top most local identifier, so that
2354 sym_identifier will point to 's' when popped */
2355 while (*ps != NULL)
2356 ps = &(*ps)->prev_tok;
2358 s->prev_tok = *ps;
2359 *ps = s;
2360 return s;
2363 /* pop labels until element last is reached. Look if any labels are
2364 undefined. Define symbols if '&&label' was used. */
2365 static void label_pop(Sym **ptop, Sym *slast)
2367 Sym *s, *s1;
2368 for(s = *ptop; s != slast; s = s1) {
2369 s1 = s->prev;
2370 if (s->r == LABEL_DECLARED) {
2371 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2372 } else if (s->r == LABEL_FORWARD) {
2373 error("label '%s' used but not defined",
2374 get_tok_str(s->v, NULL));
2375 } else {
2376 if (s->c) {
2377 /* define corresponding symbol. A size of
2378 1 is put. */
2379 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2382 /* remove label */
2383 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2384 tcc_free(s);
2386 *ptop = slast;
2389 /* eval an expression for #if/#elif */
2390 static int expr_preprocess(void)
2392 int c, t;
2393 TokenString str;
2395 tok_str_new(&str);
2396 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2397 next(); /* do macro subst */
2398 if (tok == TOK_DEFINED) {
2399 next_nomacro();
2400 t = tok;
2401 if (t == '(')
2402 next_nomacro();
2403 c = define_find(tok) != 0;
2404 if (t == '(')
2405 next_nomacro();
2406 tok = TOK_CINT;
2407 tokc.i = c;
2408 } else if (tok >= TOK_IDENT) {
2409 /* if undefined macro */
2410 tok = TOK_CINT;
2411 tokc.i = 0;
2413 tok_str_add_tok(&str);
2415 tok_str_add(&str, -1); /* simulate end of file */
2416 tok_str_add(&str, 0);
2417 /* now evaluate C constant expression */
2418 macro_ptr = str.str;
2419 next();
2420 c = expr_const();
2421 macro_ptr = NULL;
2422 tok_str_free(str.str);
2423 return c != 0;
2426 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2427 static void tok_print(int *str)
2429 int t;
2430 CValue cval;
2432 while (1) {
2433 TOK_GET(t, str, cval);
2434 if (!t)
2435 break;
2436 printf(" %s", get_tok_str(t, &cval));
2438 printf("\n");
2440 #endif
2442 /* parse after #define */
2443 static void parse_define(void)
2445 Sym *s, *first, **ps;
2446 int v, t, varg, is_vaargs, c;
2447 TokenString str;
2449 v = tok;
2450 if (v < TOK_IDENT)
2451 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2452 /* XXX: should check if same macro (ANSI) */
2453 first = NULL;
2454 t = MACRO_OBJ;
2455 /* '(' must be just after macro definition for MACRO_FUNC */
2456 c = file->buf_ptr[0];
2457 if (c == '\\')
2458 c = handle_stray1(file->buf_ptr);
2459 if (c == '(') {
2460 next_nomacro();
2461 next_nomacro();
2462 ps = &first;
2463 while (tok != ')') {
2464 varg = tok;
2465 next_nomacro();
2466 is_vaargs = 0;
2467 if (varg == TOK_DOTS) {
2468 varg = TOK___VA_ARGS__;
2469 is_vaargs = 1;
2470 } else if (tok == TOK_DOTS && gnu_ext) {
2471 is_vaargs = 1;
2472 next_nomacro();
2474 if (varg < TOK_IDENT)
2475 error("badly punctuated parameter list");
2476 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2477 *ps = s;
2478 ps = &s->next;
2479 if (tok != ',')
2480 break;
2481 next_nomacro();
2483 t = MACRO_FUNC;
2485 tok_str_new(&str);
2486 next_nomacro();
2487 /* EOF testing necessary for '-D' handling */
2488 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2489 tok_str_add2(&str, tok, &tokc);
2490 next_nomacro();
2492 tok_str_add(&str, 0);
2493 #ifdef PP_DEBUG
2494 printf("define %s %d: ", get_tok_str(v, NULL), t);
2495 tok_print(str.str);
2496 #endif
2497 define_push(v, t, str.str, first);
2500 /* XXX: use a token or a hash table to accelerate matching ? */
2501 static CachedInclude *search_cached_include(TCCState *s1,
2502 int type, const char *filename)
2504 CachedInclude *e;
2505 int i;
2507 for(i = 0;i < s1->nb_cached_includes; i++) {
2508 e = s1->cached_includes[i];
2509 if (e->type == type && !strcmp(e->filename, filename))
2510 return e;
2512 return NULL;
2515 static inline void add_cached_include(TCCState *s1, int type,
2516 const char *filename, int ifndef_macro)
2518 CachedInclude *e;
2520 if (search_cached_include(s1, type, filename))
2521 return;
2522 #ifdef INC_DEBUG
2523 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2524 #endif
2525 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2526 if (!e)
2527 return;
2528 e->type = type;
2529 strcpy(e->filename, filename);
2530 e->ifndef_macro = ifndef_macro;
2531 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2534 /* is_bof is true if first non space token at beginning of file */
2535 static void preprocess(int is_bof)
2537 TCCState *s1 = tcc_state;
2538 int size, i, c, n, saved_parse_flags;
2539 char buf[1024], *q, *p;
2540 char buf1[1024];
2541 BufferedFile *f;
2542 Sym *s;
2543 CachedInclude *e;
2545 saved_parse_flags = parse_flags;
2546 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2547 PARSE_FLAG_LINEFEED;
2548 next_nomacro();
2549 redo:
2550 switch(tok) {
2551 case TOK_DEFINE:
2552 next_nomacro();
2553 parse_define();
2554 break;
2555 case TOK_UNDEF:
2556 next_nomacro();
2557 s = define_find(tok);
2558 /* undefine symbol by putting an invalid name */
2559 if (s)
2560 define_undef(s);
2561 break;
2562 case TOK_INCLUDE:
2563 ch = file->buf_ptr[0];
2564 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2565 skip_spaces();
2566 if (ch == '<') {
2567 c = '>';
2568 goto read_name;
2569 } else if (ch == '\"') {
2570 c = ch;
2571 read_name:
2572 /* XXX: better stray handling */
2573 minp();
2574 q = buf;
2575 while (ch != c && ch != '\n' && ch != CH_EOF) {
2576 if ((q - buf) < sizeof(buf) - 1)
2577 *q++ = ch;
2578 minp();
2580 *q = '\0';
2581 minp();
2582 #if 0
2583 /* eat all spaces and comments after include */
2584 /* XXX: slightly incorrect */
2585 while (ch1 != '\n' && ch1 != CH_EOF)
2586 inp();
2587 #endif
2588 } else {
2589 /* computed #include : either we have only strings or
2590 we have anything enclosed in '<>' */
2591 next();
2592 buf[0] = '\0';
2593 if (tok == TOK_STR) {
2594 while (tok != TOK_LINEFEED) {
2595 if (tok != TOK_STR) {
2596 include_syntax:
2597 error("'#include' expects \"FILENAME\" or <FILENAME>");
2599 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2600 next();
2602 c = '\"';
2603 } else {
2604 int len;
2605 while (tok != TOK_LINEFEED) {
2606 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2607 next();
2609 len = strlen(buf);
2610 /* check syntax and remove '<>' */
2611 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2612 goto include_syntax;
2613 memmove(buf, buf + 1, len - 2);
2614 buf[len - 2] = '\0';
2615 c = '>';
2619 e = search_cached_include(s1, c, buf);
2620 if (e && define_find(e->ifndef_macro)) {
2621 /* no need to parse the include because the 'ifndef macro'
2622 is defined */
2623 #ifdef INC_DEBUG
2624 printf("%s: skipping %s\n", file->filename, buf);
2625 #endif
2626 } else {
2627 if (c == '\"') {
2628 /* first search in current dir if "header.h" */
2629 size = 0;
2630 p = strrchr(file->filename, '/');
2631 if (p)
2632 size = p + 1 - file->filename;
2633 if (size > sizeof(buf1) - 1)
2634 size = sizeof(buf1) - 1;
2635 memcpy(buf1, file->filename, size);
2636 buf1[size] = '\0';
2637 pstrcat(buf1, sizeof(buf1), buf);
2638 f = tcc_open(s1, buf1);
2639 if (f)
2640 goto found;
2642 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2643 error("#include recursion too deep");
2644 /* now search in all the include paths */
2645 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2646 for(i = 0; i < n; i++) {
2647 const char *path;
2648 if (i < s1->nb_include_paths)
2649 path = s1->include_paths[i];
2650 else
2651 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2652 pstrcpy(buf1, sizeof(buf1), path);
2653 pstrcat(buf1, sizeof(buf1), "/");
2654 pstrcat(buf1, sizeof(buf1), buf);
2655 f = tcc_open(s1, buf1);
2656 if (f)
2657 goto found;
2659 error("include file '%s' not found", buf);
2660 f = NULL;
2661 found:
2662 #ifdef INC_DEBUG
2663 printf("%s: including %s\n", file->filename, buf1);
2664 #endif
2665 f->inc_type = c;
2666 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2667 /* push current file in stack */
2668 /* XXX: fix current line init */
2669 *s1->include_stack_ptr++ = file;
2670 file = f;
2671 /* add include file debug info */
2672 if (do_debug) {
2673 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2675 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2676 ch = file->buf_ptr[0];
2677 goto the_end;
2679 break;
2680 case TOK_IFNDEF:
2681 c = 1;
2682 goto do_ifdef;
2683 case TOK_IF:
2684 c = expr_preprocess();
2685 goto do_if;
2686 case TOK_IFDEF:
2687 c = 0;
2688 do_ifdef:
2689 next_nomacro();
2690 if (tok < TOK_IDENT)
2691 error("invalid argument for '#if%sdef'", c ? "n" : "");
2692 if (is_bof) {
2693 if (c) {
2694 #ifdef INC_DEBUG
2695 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2696 #endif
2697 file->ifndef_macro = tok;
2700 c = (define_find(tok) != 0) ^ c;
2701 do_if:
2702 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2703 error("memory full");
2704 *s1->ifdef_stack_ptr++ = c;
2705 goto test_skip;
2706 case TOK_ELSE:
2707 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2708 error("#else without matching #if");
2709 if (s1->ifdef_stack_ptr[-1] & 2)
2710 error("#else after #else");
2711 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2712 goto test_skip;
2713 case TOK_ELIF:
2714 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2715 error("#elif without matching #if");
2716 c = s1->ifdef_stack_ptr[-1];
2717 if (c > 1)
2718 error("#elif after #else");
2719 /* last #if/#elif expression was true: we skip */
2720 if (c == 1)
2721 goto skip;
2722 c = expr_preprocess();
2723 s1->ifdef_stack_ptr[-1] = c;
2724 test_skip:
2725 if (!(c & 1)) {
2726 skip:
2727 preprocess_skip();
2728 is_bof = 0;
2729 goto redo;
2731 break;
2732 case TOK_ENDIF:
2733 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2734 error("#endif without matching #if");
2735 s1->ifdef_stack_ptr--;
2736 /* '#ifndef macro' was at the start of file. Now we check if
2737 an '#endif' is exactly at the end of file */
2738 if (file->ifndef_macro &&
2739 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2740 file->ifndef_macro_saved = file->ifndef_macro;
2741 /* need to set to zero to avoid false matches if another
2742 #ifndef at middle of file */
2743 file->ifndef_macro = 0;
2744 while (tok != TOK_LINEFEED)
2745 next_nomacro();
2746 tok_flags |= TOK_FLAG_ENDIF;
2747 goto the_end;
2749 break;
2750 case TOK_LINE:
2751 next();
2752 if (tok != TOK_CINT)
2753 error("#line");
2754 file->line_num = tokc.i - 1; /* the line number will be incremented after */
2755 next();
2756 if (tok != TOK_LINEFEED) {
2757 if (tok != TOK_STR)
2758 error("#line");
2759 pstrcpy(file->filename, sizeof(file->filename),
2760 (char *)tokc.cstr->data);
2762 break;
2763 case TOK_ERROR:
2764 case TOK_WARNING:
2765 c = tok;
2766 ch = file->buf_ptr[0];
2767 skip_spaces();
2768 q = buf;
2769 while (ch != '\n' && ch != CH_EOF) {
2770 if ((q - buf) < sizeof(buf) - 1)
2771 *q++ = ch;
2772 minp();
2774 *q = '\0';
2775 if (c == TOK_ERROR)
2776 error("#error %s", buf);
2777 else
2778 warning("#warning %s", buf);
2779 break;
2780 case TOK_PRAGMA:
2781 /* ignored */
2782 break;
2783 default:
2784 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
2785 /* '!' is ignored to allow C scripts. numbers are ignored
2786 to emulate cpp behaviour */
2787 } else {
2788 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
2790 break;
2792 /* ignore other preprocess commands or #! for C scripts */
2793 while (tok != TOK_LINEFEED)
2794 next_nomacro();
2795 the_end:
2796 parse_flags = saved_parse_flags;
2799 /* evaluate escape codes in a string. */
2800 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2802 int c, n;
2803 const char *p;
2805 p = buf;
2806 for(;;) {
2807 c = *p;
2808 if (c == '\0')
2809 break;
2810 if (c == '\\') {
2811 p++;
2812 /* escape */
2813 c = *p;
2814 switch(c) {
2815 case '0': case '1': case '2': case '3':
2816 case '4': case '5': case '6': case '7':
2817 /* at most three octal digits */
2818 n = c - '0';
2819 p++;
2820 c = *p;
2821 if (isoct(c)) {
2822 n = n * 8 + c - '0';
2823 p++;
2824 c = *p;
2825 if (isoct(c)) {
2826 n = n * 8 + c - '0';
2827 p++;
2830 c = n;
2831 goto add_char_nonext;
2832 case 'x':
2833 p++;
2834 n = 0;
2835 for(;;) {
2836 c = *p;
2837 if (c >= 'a' && c <= 'f')
2838 c = c - 'a' + 10;
2839 else if (c >= 'A' && c <= 'F')
2840 c = c - 'A' + 10;
2841 else if (isnum(c))
2842 c = c - '0';
2843 else
2844 break;
2845 n = n * 16 + c;
2846 p++;
2848 c = n;
2849 goto add_char_nonext;
2850 case 'a':
2851 c = '\a';
2852 break;
2853 case 'b':
2854 c = '\b';
2855 break;
2856 case 'f':
2857 c = '\f';
2858 break;
2859 case 'n':
2860 c = '\n';
2861 break;
2862 case 'r':
2863 c = '\r';
2864 break;
2865 case 't':
2866 c = '\t';
2867 break;
2868 case 'v':
2869 c = '\v';
2870 break;
2871 case 'e':
2872 if (!gnu_ext)
2873 goto invalid_escape;
2874 c = 27;
2875 break;
2876 case '\'':
2877 case '\"':
2878 case '\\':
2879 case '?':
2880 break;
2881 default:
2882 invalid_escape:
2883 error("invalid escaped char");
2886 p++;
2887 add_char_nonext:
2888 if (!is_long)
2889 cstr_ccat(outstr, c);
2890 else
2891 cstr_wccat(outstr, c);
2893 /* add a trailing '\0' */
2894 if (!is_long)
2895 cstr_ccat(outstr, '\0');
2896 else
2897 cstr_wccat(outstr, '\0');
2900 /* we use 64 bit numbers */
2901 #define BN_SIZE 2
2903 /* bn = (bn << shift) | or_val */
2904 void bn_lshift(unsigned int *bn, int shift, int or_val)
2906 int i;
2907 unsigned int v;
2908 for(i=0;i<BN_SIZE;i++) {
2909 v = bn[i];
2910 bn[i] = (v << shift) | or_val;
2911 or_val = v >> (32 - shift);
2915 void bn_zero(unsigned int *bn)
2917 int i;
2918 for(i=0;i<BN_SIZE;i++) {
2919 bn[i] = 0;
2923 /* parse number in null terminated string 'p' and return it in the
2924 current token */
2925 void parse_number(const char *p)
2927 int b, t, shift, frac_bits, s, exp_val, ch;
2928 char *q;
2929 unsigned int bn[BN_SIZE];
2930 double d;
2932 /* number */
2933 q = token_buf;
2934 ch = *p++;
2935 t = ch;
2936 ch = *p++;
2937 *q++ = t;
2938 b = 10;
2939 if (t == '.') {
2940 goto float_frac_parse;
2941 } else if (t == '0') {
2942 if (ch == 'x' || ch == 'X') {
2943 q--;
2944 ch = *p++;
2945 b = 16;
2946 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2947 q--;
2948 ch = *p++;
2949 b = 2;
2952 /* parse all digits. cannot check octal numbers at this stage
2953 because of floating point constants */
2954 while (1) {
2955 if (ch >= 'a' && ch <= 'f')
2956 t = ch - 'a' + 10;
2957 else if (ch >= 'A' && ch <= 'F')
2958 t = ch - 'A' + 10;
2959 else if (isnum(ch))
2960 t = ch - '0';
2961 else
2962 break;
2963 if (t >= b)
2964 break;
2965 if (q >= token_buf + STRING_MAX_SIZE) {
2966 num_too_long:
2967 error("number too long");
2969 *q++ = ch;
2970 ch = *p++;
2972 if (ch == '.' ||
2973 ((ch == 'e' || ch == 'E') && b == 10) ||
2974 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2975 if (b != 10) {
2976 /* NOTE: strtox should support that for hexa numbers, but
2977 non ISOC99 libcs do not support it, so we prefer to do
2978 it by hand */
2979 /* hexadecimal or binary floats */
2980 /* XXX: handle overflows */
2981 *q = '\0';
2982 if (b == 16)
2983 shift = 4;
2984 else
2985 shift = 2;
2986 bn_zero(bn);
2987 q = token_buf;
2988 while (1) {
2989 t = *q++;
2990 if (t == '\0') {
2991 break;
2992 } else if (t >= 'a') {
2993 t = t - 'a' + 10;
2994 } else if (t >= 'A') {
2995 t = t - 'A' + 10;
2996 } else {
2997 t = t - '0';
2999 bn_lshift(bn, shift, t);
3001 frac_bits = 0;
3002 if (ch == '.') {
3003 ch = *p++;
3004 while (1) {
3005 t = ch;
3006 if (t >= 'a' && t <= 'f') {
3007 t = t - 'a' + 10;
3008 } else if (t >= 'A' && t <= 'F') {
3009 t = t - 'A' + 10;
3010 } else if (t >= '0' && t <= '9') {
3011 t = t - '0';
3012 } else {
3013 break;
3015 if (t >= b)
3016 error("invalid digit");
3017 bn_lshift(bn, shift, t);
3018 frac_bits += shift;
3019 ch = *p++;
3022 if (ch != 'p' && ch != 'P')
3023 expect("exponent");
3024 ch = *p++;
3025 s = 1;
3026 exp_val = 0;
3027 if (ch == '+') {
3028 ch = *p++;
3029 } else if (ch == '-') {
3030 s = -1;
3031 ch = *p++;
3033 if (ch < '0' || ch > '9')
3034 expect("exponent digits");
3035 while (ch >= '0' && ch <= '9') {
3036 exp_val = exp_val * 10 + ch - '0';
3037 ch = *p++;
3039 exp_val = exp_val * s;
3041 /* now we can generate the number */
3042 /* XXX: should patch directly float number */
3043 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3044 d = ldexp(d, exp_val - frac_bits);
3045 t = toup(ch);
3046 if (t == 'F') {
3047 ch = *p++;
3048 tok = TOK_CFLOAT;
3049 /* float : should handle overflow */
3050 tokc.f = (float)d;
3051 } else if (t == 'L') {
3052 ch = *p++;
3053 tok = TOK_CLDOUBLE;
3054 /* XXX: not large enough */
3055 tokc.ld = (long double)d;
3056 } else {
3057 tok = TOK_CDOUBLE;
3058 tokc.d = d;
3060 } else {
3061 /* decimal floats */
3062 if (ch == '.') {
3063 if (q >= token_buf + STRING_MAX_SIZE)
3064 goto num_too_long;
3065 *q++ = ch;
3066 ch = *p++;
3067 float_frac_parse:
3068 while (ch >= '0' && ch <= '9') {
3069 if (q >= token_buf + STRING_MAX_SIZE)
3070 goto num_too_long;
3071 *q++ = ch;
3072 ch = *p++;
3075 if (ch == 'e' || ch == 'E') {
3076 if (q >= token_buf + STRING_MAX_SIZE)
3077 goto num_too_long;
3078 *q++ = ch;
3079 ch = *p++;
3080 if (ch == '-' || ch == '+') {
3081 if (q >= token_buf + STRING_MAX_SIZE)
3082 goto num_too_long;
3083 *q++ = ch;
3084 ch = *p++;
3086 if (ch < '0' || ch > '9')
3087 expect("exponent digits");
3088 while (ch >= '0' && ch <= '9') {
3089 if (q >= token_buf + STRING_MAX_SIZE)
3090 goto num_too_long;
3091 *q++ = ch;
3092 ch = *p++;
3095 *q = '\0';
3096 t = toup(ch);
3097 errno = 0;
3098 if (t == 'F') {
3099 ch = *p++;
3100 tok = TOK_CFLOAT;
3101 tokc.f = strtof(token_buf, NULL);
3102 } else if (t == 'L') {
3103 ch = *p++;
3104 tok = TOK_CLDOUBLE;
3105 tokc.ld = strtold(token_buf, NULL);
3106 } else {
3107 tok = TOK_CDOUBLE;
3108 tokc.d = strtod(token_buf, NULL);
3111 } else {
3112 unsigned long long n, n1;
3113 int lcount, ucount;
3115 /* integer number */
3116 *q = '\0';
3117 q = token_buf;
3118 if (b == 10 && *q == '0') {
3119 b = 8;
3120 q++;
3122 n = 0;
3123 while(1) {
3124 t = *q++;
3125 /* no need for checks except for base 10 / 8 errors */
3126 if (t == '\0') {
3127 break;
3128 } else if (t >= 'a') {
3129 t = t - 'a' + 10;
3130 } else if (t >= 'A') {
3131 t = t - 'A' + 10;
3132 } else {
3133 t = t - '0';
3134 if (t >= b)
3135 error("invalid digit");
3137 n1 = n;
3138 n = n * b + t;
3139 /* detect overflow */
3140 /* XXX: this test is not reliable */
3141 if (n < n1)
3142 error("integer constant overflow");
3145 /* XXX: not exactly ANSI compliant */
3146 if ((n & 0xffffffff00000000LL) != 0) {
3147 if ((n >> 63) != 0)
3148 tok = TOK_CULLONG;
3149 else
3150 tok = TOK_CLLONG;
3151 } else if (n > 0x7fffffff) {
3152 tok = TOK_CUINT;
3153 } else {
3154 tok = TOK_CINT;
3156 lcount = 0;
3157 ucount = 0;
3158 for(;;) {
3159 t = toup(ch);
3160 if (t == 'L') {
3161 if (lcount >= 2)
3162 error("three 'l's in integer constant");
3163 lcount++;
3164 if (lcount == 2) {
3165 if (tok == TOK_CINT)
3166 tok = TOK_CLLONG;
3167 else if (tok == TOK_CUINT)
3168 tok = TOK_CULLONG;
3170 ch = *p++;
3171 } else if (t == 'U') {
3172 if (ucount >= 1)
3173 error("two 'u's in integer constant");
3174 ucount++;
3175 if (tok == TOK_CINT)
3176 tok = TOK_CUINT;
3177 else if (tok == TOK_CLLONG)
3178 tok = TOK_CULLONG;
3179 ch = *p++;
3180 } else {
3181 break;
3184 if (tok == TOK_CINT || tok == TOK_CUINT)
3185 tokc.ui = n;
3186 else
3187 tokc.ull = n;
3192 #define PARSE2(c1, tok1, c2, tok2) \
3193 case c1: \
3194 PEEKC(c, p); \
3195 if (c == c2) { \
3196 p++; \
3197 tok = tok2; \
3198 } else { \
3199 tok = tok1; \
3201 break;
3203 /* return next token without macro substitution */
3204 static inline void next_nomacro1(void)
3206 int t, c, is_long;
3207 TokenSym *ts;
3208 uint8_t *p, *p1;
3209 unsigned int h;
3211 p = file->buf_ptr;
3212 redo_no_start:
3213 c = *p;
3214 switch(c) {
3215 case ' ':
3216 case '\t':
3217 case '\f':
3218 case '\v':
3219 case '\r':
3220 p++;
3221 goto redo_no_start;
3223 case '\\':
3224 /* first look if it is in fact an end of buffer */
3225 if (p >= file->buf_end) {
3226 file->buf_ptr = p;
3227 handle_eob();
3228 p = file->buf_ptr;
3229 if (p >= file->buf_end)
3230 goto parse_eof;
3231 else
3232 goto redo_no_start;
3233 } else {
3234 file->buf_ptr = p;
3235 ch = *p;
3236 handle_stray();
3237 p = file->buf_ptr;
3238 goto redo_no_start;
3240 parse_eof:
3242 TCCState *s1 = tcc_state;
3243 if (parse_flags & PARSE_FLAG_LINEFEED) {
3244 tok = TOK_LINEFEED;
3245 } else if (s1->include_stack_ptr == s1->include_stack ||
3246 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3247 /* no include left : end of file. */
3248 tok = TOK_EOF;
3249 } else {
3250 /* pop include file */
3252 /* test if previous '#endif' was after a #ifdef at
3253 start of file */
3254 if (tok_flags & TOK_FLAG_ENDIF) {
3255 #ifdef INC_DEBUG
3256 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3257 #endif
3258 add_cached_include(s1, file->inc_type, file->inc_filename,
3259 file->ifndef_macro_saved);
3262 /* add end of include file debug info */
3263 if (do_debug) {
3264 put_stabd(N_EINCL, 0, 0);
3266 /* pop include stack */
3267 tcc_close(file);
3268 s1->include_stack_ptr--;
3269 file = *s1->include_stack_ptr;
3270 p = file->buf_ptr;
3271 goto redo_no_start;
3274 break;
3276 case '\n':
3277 if (parse_flags & PARSE_FLAG_LINEFEED) {
3278 tok = TOK_LINEFEED;
3279 } else {
3280 file->line_num++;
3281 tok_flags |= TOK_FLAG_BOL;
3282 p++;
3283 goto redo_no_start;
3285 break;
3287 case '#':
3288 /* XXX: simplify */
3289 PEEKC(c, p);
3290 if ((tok_flags & TOK_FLAG_BOL) &&
3291 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3292 file->buf_ptr = p;
3293 preprocess(tok_flags & TOK_FLAG_BOF);
3294 p = file->buf_ptr;
3295 goto redo_no_start;
3296 } else {
3297 if (c == '#') {
3298 p++;
3299 tok = TOK_TWOSHARPS;
3300 } else {
3301 tok = '#';
3304 break;
3306 case 'a': case 'b': case 'c': case 'd':
3307 case 'e': case 'f': case 'g': case 'h':
3308 case 'i': case 'j': case 'k': case 'l':
3309 case 'm': case 'n': case 'o': case 'p':
3310 case 'q': case 'r': case 's': case 't':
3311 case 'u': case 'v': case 'w': case 'x':
3312 case 'y': case 'z':
3313 case 'A': case 'B': case 'C': case 'D':
3314 case 'E': case 'F': case 'G': case 'H':
3315 case 'I': case 'J': case 'K':
3316 case 'M': case 'N': case 'O': case 'P':
3317 case 'Q': case 'R': case 'S': case 'T':
3318 case 'U': case 'V': case 'W': case 'X':
3319 case 'Y': case 'Z':
3320 case '_':
3321 parse_ident_fast:
3322 p1 = p;
3323 h = TOK_HASH_INIT;
3324 h = TOK_HASH_FUNC(h, c);
3325 p++;
3326 for(;;) {
3327 c = *p;
3328 if (!isidnum_table[c])
3329 break;
3330 h = TOK_HASH_FUNC(h, c);
3331 p++;
3333 if (c != '\\') {
3334 TokenSym **pts;
3335 int len;
3337 /* fast case : no stray found, so we have the full token
3338 and we have already hashed it */
3339 len = p - p1;
3340 h &= (TOK_HASH_SIZE - 1);
3341 pts = &hash_ident[h];
3342 for(;;) {
3343 ts = *pts;
3344 if (!ts)
3345 break;
3346 if (ts->len == len && !memcmp(ts->str, p1, len))
3347 goto token_found;
3348 pts = &(ts->hash_next);
3350 ts = tok_alloc_new(pts, p1, len);
3351 token_found: ;
3352 } else {
3353 /* slower case */
3354 cstr_reset(&tokcstr);
3356 while (p1 < p) {
3357 cstr_ccat(&tokcstr, *p1);
3358 p1++;
3360 p--;
3361 PEEKC(c, p);
3362 parse_ident_slow:
3363 while (isidnum_table[c]) {
3364 cstr_ccat(&tokcstr, c);
3365 PEEKC(c, p);
3367 ts = tok_alloc(tokcstr.data, tokcstr.size);
3369 tok = ts->tok;
3370 break;
3371 case 'L':
3372 t = p[1];
3373 if (t != '\\' && t != '\'' && t != '\"') {
3374 /* fast case */
3375 goto parse_ident_fast;
3376 } else {
3377 PEEKC(c, p);
3378 if (c == '\'' || c == '\"') {
3379 is_long = 1;
3380 goto str_const;
3381 } else {
3382 cstr_reset(&tokcstr);
3383 cstr_ccat(&tokcstr, 'L');
3384 goto parse_ident_slow;
3387 break;
3388 case '0': case '1': case '2': case '3':
3389 case '4': case '5': case '6': case '7':
3390 case '8': case '9':
3392 cstr_reset(&tokcstr);
3393 /* after the first digit, accept digits, alpha, '.' or sign if
3394 prefixed by 'eEpP' */
3395 parse_num:
3396 for(;;) {
3397 t = c;
3398 cstr_ccat(&tokcstr, c);
3399 PEEKC(c, p);
3400 if (!(isnum(c) || isid(c) || c == '.' ||
3401 ((c == '+' || c == '-') &&
3402 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3403 break;
3405 /* We add a trailing '\0' to ease parsing */
3406 cstr_ccat(&tokcstr, '\0');
3407 tokc.cstr = &tokcstr;
3408 tok = TOK_PPNUM;
3409 break;
3410 case '.':
3411 /* special dot handling because it can also start a number */
3412 PEEKC(c, p);
3413 if (isnum(c)) {
3414 cstr_reset(&tokcstr);
3415 cstr_ccat(&tokcstr, '.');
3416 goto parse_num;
3417 } else if (c == '.') {
3418 PEEKC(c, p);
3419 if (c != '.')
3420 expect("'.'");
3421 PEEKC(c, p);
3422 tok = TOK_DOTS;
3423 } else {
3424 tok = '.';
3426 break;
3427 case '\'':
3428 case '\"':
3429 is_long = 0;
3430 str_const:
3432 CString str;
3433 int sep;
3435 sep = c;
3437 /* parse the string */
3438 cstr_new(&str);
3439 p = parse_pp_string(p, sep, &str);
3440 cstr_ccat(&str, '\0');
3442 /* eval the escape (should be done as TOK_PPNUM) */
3443 cstr_reset(&tokcstr);
3444 parse_escape_string(&tokcstr, str.data, is_long);
3445 cstr_free(&str);
3447 if (sep == '\'') {
3448 int char_size;
3449 /* XXX: make it portable */
3450 if (!is_long)
3451 char_size = 1;
3452 else
3453 char_size = sizeof(int);
3454 if (tokcstr.size <= char_size)
3455 error("empty character constant");
3456 if (tokcstr.size > 2 * char_size)
3457 warning("multi-character character constant");
3458 if (!is_long) {
3459 tokc.i = *(int8_t *)tokcstr.data;
3460 tok = TOK_CCHAR;
3461 } else {
3462 tokc.i = *(int *)tokcstr.data;
3463 tok = TOK_LCHAR;
3465 } else {
3466 tokc.cstr = &tokcstr;
3467 if (!is_long)
3468 tok = TOK_STR;
3469 else
3470 tok = TOK_LSTR;
3473 break;
3475 case '<':
3476 PEEKC(c, p);
3477 if (c == '=') {
3478 p++;
3479 tok = TOK_LE;
3480 } else if (c == '<') {
3481 PEEKC(c, p);
3482 if (c == '=') {
3483 p++;
3484 tok = TOK_A_SHL;
3485 } else {
3486 tok = TOK_SHL;
3488 } else {
3489 tok = TOK_LT;
3491 break;
3493 case '>':
3494 PEEKC(c, p);
3495 if (c == '=') {
3496 p++;
3497 tok = TOK_GE;
3498 } else if (c == '>') {
3499 PEEKC(c, p);
3500 if (c == '=') {
3501 p++;
3502 tok = TOK_A_SAR;
3503 } else {
3504 tok = TOK_SAR;
3506 } else {
3507 tok = TOK_GT;
3509 break;
3511 case '&':
3512 PEEKC(c, p);
3513 if (c == '&') {
3514 p++;
3515 tok = TOK_LAND;
3516 } else if (c == '=') {
3517 p++;
3518 tok = TOK_A_AND;
3519 } else {
3520 tok = '&';
3522 break;
3524 case '|':
3525 PEEKC(c, p);
3526 if (c == '|') {
3527 p++;
3528 tok = TOK_LOR;
3529 } else if (c == '=') {
3530 p++;
3531 tok = TOK_A_OR;
3532 } else {
3533 tok = '|';
3535 break;
3537 case '+':
3538 PEEKC(c, p);
3539 if (c == '+') {
3540 p++;
3541 tok = TOK_INC;
3542 } else if (c == '=') {
3543 p++;
3544 tok = TOK_A_ADD;
3545 } else {
3546 tok = '+';
3548 break;
3550 case '-':
3551 PEEKC(c, p);
3552 if (c == '-') {
3553 p++;
3554 tok = TOK_DEC;
3555 } else if (c == '=') {
3556 p++;
3557 tok = TOK_A_SUB;
3558 } else if (c == '>') {
3559 p++;
3560 tok = TOK_ARROW;
3561 } else {
3562 tok = '-';
3564 break;
3566 PARSE2('!', '!', '=', TOK_NE)
3567 PARSE2('=', '=', '=', TOK_EQ)
3568 PARSE2('*', '*', '=', TOK_A_MUL)
3569 PARSE2('%', '%', '=', TOK_A_MOD)
3570 PARSE2('^', '^', '=', TOK_A_XOR)
3572 /* comments or operator */
3573 case '/':
3574 PEEKC(c, p);
3575 if (c == '*') {
3576 p = parse_comment(p);
3577 goto redo_no_start;
3578 } else if (c == '/') {
3579 p = parse_line_comment(p);
3580 goto redo_no_start;
3581 } else if (c == '=') {
3582 p++;
3583 tok = TOK_A_DIV;
3584 } else {
3585 tok = '/';
3587 break;
3589 /* simple tokens */
3590 case '(':
3591 case ')':
3592 case '[':
3593 case ']':
3594 case '{':
3595 case '}':
3596 case ',':
3597 case ';':
3598 case ':':
3599 case '?':
3600 case '~':
3601 case '$': /* only used in assembler */
3602 tok = c;
3603 p++;
3604 break;
3605 default:
3606 error("unrecognized character \\x%02x", c);
3607 break;
3609 file->buf_ptr = p;
3610 tok_flags = 0;
3611 #if defined(PARSE_DEBUG)
3612 printf("token = %s\n", get_tok_str(tok, &tokc));
3613 #endif
3616 /* return next token without macro substitution. Can read input from
3617 macro_ptr buffer */
3618 static void next_nomacro(void)
3620 if (macro_ptr) {
3621 redo:
3622 tok = *macro_ptr;
3623 if (tok) {
3624 TOK_GET(tok, macro_ptr, tokc);
3625 if (tok == TOK_LINENUM) {
3626 file->line_num = tokc.i;
3627 goto redo;
3630 } else {
3631 next_nomacro1();
3635 /* substitute args in macro_str and return allocated string */
3636 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3638 int *st, last_tok, t, notfirst;
3639 Sym *s;
3640 CValue cval;
3641 TokenString str;
3642 CString cstr;
3644 tok_str_new(&str);
3645 last_tok = 0;
3646 while(1) {
3647 TOK_GET(t, macro_str, cval);
3648 if (!t)
3649 break;
3650 if (t == '#') {
3651 /* stringize */
3652 TOK_GET(t, macro_str, cval);
3653 if (!t)
3654 break;
3655 s = sym_find2(args, t);
3656 if (s) {
3657 cstr_new(&cstr);
3658 st = (int *)s->c;
3659 notfirst = 0;
3660 while (*st) {
3661 if (notfirst)
3662 cstr_ccat(&cstr, ' ');
3663 TOK_GET(t, st, cval);
3664 cstr_cat(&cstr, get_tok_str(t, &cval));
3665 notfirst = 1;
3667 cstr_ccat(&cstr, '\0');
3668 #ifdef PP_DEBUG
3669 printf("stringize: %s\n", (char *)cstr.data);
3670 #endif
3671 /* add string */
3672 cval.cstr = &cstr;
3673 tok_str_add2(&str, TOK_STR, &cval);
3674 cstr_free(&cstr);
3675 } else {
3676 tok_str_add2(&str, t, &cval);
3678 } else if (t >= TOK_IDENT) {
3679 s = sym_find2(args, t);
3680 if (s) {
3681 st = (int *)s->c;
3682 /* if '##' is present before or after, no arg substitution */
3683 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3684 /* special case for var arg macros : ## eats the
3685 ',' if empty VA_ARGS variable. */
3686 /* XXX: test of the ',' is not 100%
3687 reliable. should fix it to avoid security
3688 problems */
3689 if (gnu_ext && s->type.t &&
3690 last_tok == TOK_TWOSHARPS &&
3691 str.len >= 2 && str.str[str.len - 2] == ',') {
3692 if (*st == 0) {
3693 /* suppress ',' '##' */
3694 str.len -= 2;
3695 } else {
3696 /* suppress '##' and add variable */
3697 str.len--;
3698 goto add_var;
3700 } else {
3701 int t1;
3702 add_var:
3703 for(;;) {
3704 TOK_GET(t1, st, cval);
3705 if (!t1)
3706 break;
3707 tok_str_add2(&str, t1, &cval);
3710 } else {
3711 macro_subst(&str, nested_list, st);
3713 } else {
3714 tok_str_add(&str, t);
3716 } else {
3717 tok_str_add2(&str, t, &cval);
3719 last_tok = t;
3721 tok_str_add(&str, 0);
3722 return str.str;
3725 static char const ab_month_name[12][4] =
3727 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3728 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3731 /* do macro substitution of current token with macro 's' and add
3732 result to (tok_str,tok_len). 'nested_list' is the list of all
3733 macros we got inside to avoid recursing. Return non zero if no
3734 substitution needs to be done */
3735 static int macro_subst_tok(TokenString *tok_str,
3736 Sym **nested_list, Sym *s)
3738 Sym *args, *sa, *sa1;
3739 int mstr_allocated, parlevel, *mstr, t;
3740 TokenString str;
3741 char *cstrval;
3742 CValue cval;
3743 CString cstr;
3745 /* if symbol is a macro, prepare substitution */
3747 /* special macros */
3748 if (tok == TOK___LINE__) {
3749 cval.i = file->line_num;
3750 tok_str_add2(tok_str, TOK_CINT, &cval);
3751 } else if (tok == TOK___FILE__) {
3752 cstrval = file->filename;
3753 goto add_cstr;
3754 tok_str_add2(tok_str, TOK_STR, &cval);
3755 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
3756 time_t ti;
3757 struct tm *tm;
3758 char buf[64];
3760 time(&ti);
3761 tm = localtime(&ti);
3762 if (tok == TOK___DATE__) {
3763 snprintf(buf, sizeof(buf), "%s %2d %d",
3764 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
3765 } else {
3766 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
3767 tm->tm_hour, tm->tm_min, tm->tm_sec);
3769 cstrval = buf;
3770 add_cstr:
3771 cstr_new(&cstr);
3772 cstr_cat(&cstr, cstrval);
3773 cstr_ccat(&cstr, '\0');
3774 cval.cstr = &cstr;
3775 tok_str_add2(tok_str, TOK_STR, &cval);
3776 cstr_free(&cstr);
3777 } else {
3778 mstr = (int *)s->c;
3779 mstr_allocated = 0;
3780 if (s->type.t == MACRO_FUNC) {
3781 /* NOTE: we do not use next_nomacro to avoid eating the
3782 next token. XXX: find better solution */
3783 if (macro_ptr) {
3784 t = *macro_ptr;
3785 if (t == 0) {
3786 /* end of macro stream: we must look at the token
3787 after in the file */
3788 macro_ptr = NULL;
3789 goto parse_stream;
3791 } else {
3792 parse_stream:
3793 /* XXX: incorrect with comments */
3794 ch = file->buf_ptr[0];
3795 while (is_space(ch) || ch == '\n')
3796 cinp();
3797 t = ch;
3799 if (t != '(') /* no macro subst */
3800 return -1;
3802 /* argument macro */
3803 next_nomacro();
3804 next_nomacro();
3805 args = NULL;
3806 sa = s->next;
3807 /* NOTE: empty args are allowed, except if no args */
3808 for(;;) {
3809 /* handle '()' case */
3810 if (!args && tok == ')')
3811 break;
3812 if (!sa)
3813 error("macro '%s' used with too many args",
3814 get_tok_str(s->v, 0));
3815 tok_str_new(&str);
3816 parlevel = 0;
3817 /* NOTE: non zero sa->t indicates VA_ARGS */
3818 while ((parlevel > 0 ||
3819 (tok != ')' &&
3820 (tok != ',' || sa->type.t))) &&
3821 tok != -1) {
3822 if (tok == '(')
3823 parlevel++;
3824 else if (tok == ')')
3825 parlevel--;
3826 tok_str_add2(&str, tok, &tokc);
3827 next_nomacro();
3829 tok_str_add(&str, 0);
3830 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
3831 sa = sa->next;
3832 if (tok == ')') {
3833 /* special case for gcc var args: add an empty
3834 var arg argument if it is omitted */
3835 if (sa && sa->type.t && gnu_ext)
3836 continue;
3837 else
3838 break;
3840 if (tok != ',')
3841 expect(",");
3842 next_nomacro();
3844 if (sa) {
3845 error("macro '%s' used with too few args",
3846 get_tok_str(s->v, 0));
3849 /* now subst each arg */
3850 mstr = macro_arg_subst(nested_list, mstr, args);
3851 /* free memory */
3852 sa = args;
3853 while (sa) {
3854 sa1 = sa->prev;
3855 tok_str_free((int *)sa->c);
3856 tcc_free(sa);
3857 sa = sa1;
3859 mstr_allocated = 1;
3861 sym_push2(nested_list, s->v, 0, 0);
3862 macro_subst(tok_str, nested_list, mstr);
3863 /* pop nested defined symbol */
3864 sa1 = *nested_list;
3865 *nested_list = sa1->prev;
3866 tcc_free(sa1);
3867 if (mstr_allocated)
3868 tok_str_free(mstr);
3870 return 0;
3873 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3874 return the resulting string (which must be freed). */
3875 static inline int *macro_twosharps(const int *macro_str)
3877 TokenSym *ts;
3878 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
3879 int t;
3880 const char *p1, *p2;
3881 CValue cval;
3882 TokenString macro_str1;
3883 CString cstr;
3885 start_macro_ptr = macro_str;
3886 /* we search the first '##' */
3887 for(;;) {
3888 macro_ptr1 = macro_str;
3889 TOK_GET(t, macro_str, cval);
3890 /* nothing more to do if end of string */
3891 if (t == 0)
3892 return NULL;
3893 if (*macro_str == TOK_TWOSHARPS)
3894 break;
3897 /* we saw '##', so we need more processing to handle it */
3898 cstr_new(&cstr);
3899 tok_str_new(&macro_str1);
3900 tok = t;
3901 tokc = cval;
3903 /* add all tokens seen so far */
3904 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
3905 TOK_GET(t, ptr, cval);
3906 tok_str_add2(&macro_str1, t, &cval);
3908 saved_macro_ptr = macro_ptr;
3909 /* XXX: get rid of the use of macro_ptr here */
3910 macro_ptr = (int *)macro_str;
3911 for(;;) {
3912 while (*macro_ptr == TOK_TWOSHARPS) {
3913 macro_ptr++;
3914 macro_ptr1 = macro_ptr;
3915 t = *macro_ptr;
3916 if (t) {
3917 TOK_GET(t, macro_ptr, cval);
3918 /* We concatenate the two tokens if we have an
3919 identifier or a preprocessing number */
3920 cstr_reset(&cstr);
3921 p1 = get_tok_str(tok, &tokc);
3922 cstr_cat(&cstr, p1);
3923 p2 = get_tok_str(t, &cval);
3924 cstr_cat(&cstr, p2);
3925 cstr_ccat(&cstr, '\0');
3927 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
3928 (t >= TOK_IDENT || t == TOK_PPNUM)) {
3929 if (tok == TOK_PPNUM) {
3930 /* if number, then create a number token */
3931 /* NOTE: no need to allocate because
3932 tok_str_add2() does it */
3933 tokc.cstr = &cstr;
3934 } else {
3935 /* if identifier, we must do a test to
3936 validate we have a correct identifier */
3937 if (t == TOK_PPNUM) {
3938 const char *p;
3939 int c;
3941 p = p2;
3942 for(;;) {
3943 c = *p;
3944 if (c == '\0')
3945 break;
3946 p++;
3947 if (!isnum(c) && !isid(c))
3948 goto error_pasting;
3951 ts = tok_alloc(cstr.data, strlen(cstr.data));
3952 tok = ts->tok; /* modify current token */
3954 } else {
3955 const char *str = cstr.data;
3956 const unsigned char *q;
3958 /* we look for a valid token */
3959 /* XXX: do more extensive checks */
3960 if (!strcmp(str, ">>=")) {
3961 tok = TOK_A_SAR;
3962 } else if (!strcmp(str, "<<=")) {
3963 tok = TOK_A_SHL;
3964 } else if (strlen(str) == 2) {
3965 /* search in two bytes table */
3966 q = tok_two_chars;
3967 for(;;) {
3968 if (!*q)
3969 goto error_pasting;
3970 if (q[0] == str[0] && q[1] == str[1])
3971 break;
3972 q += 3;
3974 tok = q[2];
3975 } else {
3976 error_pasting:
3977 /* NOTE: because get_tok_str use a static buffer,
3978 we must save it */
3979 cstr_reset(&cstr);
3980 p1 = get_tok_str(tok, &tokc);
3981 cstr_cat(&cstr, p1);
3982 cstr_ccat(&cstr, '\0');
3983 p2 = get_tok_str(t, &cval);
3984 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
3985 /* cannot merge tokens: just add them separately */
3986 tok_str_add2(&macro_str1, tok, &tokc);
3987 /* XXX: free associated memory ? */
3988 tok = t;
3989 tokc = cval;
3994 tok_str_add2(&macro_str1, tok, &tokc);
3995 next_nomacro();
3996 if (tok == 0)
3997 break;
3999 macro_ptr = (int *)saved_macro_ptr;
4000 cstr_free(&cstr);
4001 tok_str_add(&macro_str1, 0);
4002 return macro_str1.str;
4006 /* do macro substitution of macro_str and add result to
4007 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4008 inside to avoid recursing. */
4009 static void macro_subst(TokenString *tok_str,
4010 Sym **nested_list, const int *macro_str)
4012 Sym *s;
4013 int *saved_macro_ptr, *macro_str1;
4014 const int *ptr;
4015 int t, ret;
4016 CValue cval;
4018 /* first scan for '##' operator handling */
4019 ptr = macro_str;
4020 macro_str1 = macro_twosharps(ptr);
4021 if (macro_str1)
4022 ptr = macro_str1;
4023 while (1) {
4024 /* NOTE: ptr == NULL can only happen if tokens are read from
4025 file stream due to a macro function call */
4026 if (ptr == NULL)
4027 break;
4028 TOK_GET(t, ptr, cval);
4029 if (t == 0)
4030 break;
4031 s = define_find(t);
4032 if (s != NULL) {
4033 /* if nested substitution, do nothing */
4034 if (sym_find2(*nested_list, t))
4035 goto no_subst;
4036 saved_macro_ptr = macro_ptr;
4037 macro_ptr = (int *)ptr;
4038 tok = t;
4039 ret = macro_subst_tok(tok_str, nested_list, s);
4040 ptr = (int *)macro_ptr;
4041 macro_ptr = saved_macro_ptr;
4042 if (ret != 0)
4043 goto no_subst;
4044 } else {
4045 no_subst:
4046 tok_str_add2(tok_str, t, &cval);
4049 if (macro_str1)
4050 tok_str_free(macro_str1);
4053 /* return next token with macro substitution */
4054 static void next(void)
4056 Sym *nested_list, *s;
4057 TokenString str;
4059 redo:
4060 next_nomacro();
4061 if (!macro_ptr) {
4062 /* if not reading from macro substituted string, then try
4063 to substitute macros */
4064 if (tok >= TOK_IDENT &&
4065 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4066 s = define_find(tok);
4067 if (s) {
4068 /* we have a macro: we try to substitute */
4069 tok_str_new(&str);
4070 nested_list = NULL;
4071 if (macro_subst_tok(&str, &nested_list, s) == 0) {
4072 /* substitution done, NOTE: maybe empty */
4073 tok_str_add(&str, 0);
4074 macro_ptr = str.str;
4075 macro_ptr_allocated = str.str;
4076 goto redo;
4080 } else {
4081 if (tok == 0) {
4082 /* end of macro or end of unget buffer */
4083 if (unget_buffer_enabled) {
4084 macro_ptr = unget_saved_macro_ptr;
4085 unget_buffer_enabled = 0;
4086 } else {
4087 /* end of macro string: free it */
4088 tok_str_free(macro_ptr_allocated);
4089 macro_ptr = NULL;
4091 goto redo;
4095 /* convert preprocessor tokens into C tokens */
4096 if (tok == TOK_PPNUM &&
4097 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4098 parse_number((char *)tokc.cstr->data);
4102 /* push back current token and set current token to 'last_tok'. Only
4103 identifier case handled for labels. */
4104 static inline void unget_tok(int last_tok)
4106 int i, n;
4107 int *q;
4108 unget_saved_macro_ptr = macro_ptr;
4109 unget_buffer_enabled = 1;
4110 q = unget_saved_buffer;
4111 macro_ptr = q;
4112 *q++ = tok;
4113 n = tok_ext_size(tok) - 1;
4114 for(i=0;i<n;i++)
4115 *q++ = tokc.tab[i];
4116 *q = 0; /* end of token string */
4117 tok = last_tok;
4121 void swap(int *p, int *q)
4123 int t;
4124 t = *p;
4125 *p = *q;
4126 *q = t;
4129 void vsetc(CType *type, int r, CValue *vc)
4131 int v;
4133 if (vtop >= vstack + VSTACK_SIZE)
4134 error("memory full");
4135 /* cannot let cpu flags if other instruction are generated. Also
4136 avoid leaving VT_JMP anywhere except on the top of the stack
4137 because it would complicate the code generator. */
4138 if (vtop >= vstack) {
4139 v = vtop->r & VT_VALMASK;
4140 if (v == VT_CMP || (v & ~1) == VT_JMP)
4141 gv(RC_INT);
4143 vtop++;
4144 vtop->type = *type;
4145 vtop->r = r;
4146 vtop->r2 = VT_CONST;
4147 vtop->c = *vc;
4150 /* push integer constant */
4151 void vpushi(int v)
4153 CValue cval;
4154 cval.i = v;
4155 vsetc(&int_type, VT_CONST, &cval);
4158 /* Return a static symbol pointing to a section */
4159 static Sym *get_sym_ref(CType *type, Section *sec,
4160 unsigned long offset, unsigned long size)
4162 int v;
4163 Sym *sym;
4165 v = anon_sym++;
4166 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4167 sym->type.ref = type->ref;
4168 sym->r = VT_CONST | VT_SYM;
4169 put_extern_sym(sym, sec, offset, size);
4170 return sym;
4173 /* push a reference to a section offset by adding a dummy symbol */
4174 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4176 CValue cval;
4178 cval.ul = 0;
4179 vsetc(type, VT_CONST | VT_SYM, &cval);
4180 vtop->sym = get_sym_ref(type, sec, offset, size);
4183 /* define a new external reference to a symbol 'v' of type 'u' */
4184 static Sym *external_global_sym(int v, CType *type, int r)
4186 Sym *s;
4188 s = sym_find(v);
4189 if (!s) {
4190 /* push forward reference */
4191 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4192 s->type.ref = type->ref;
4193 s->r = r | VT_CONST | VT_SYM;
4195 return s;
4198 /* define a new external reference to a symbol 'v' of type 'u' */
4199 static Sym *external_sym(int v, CType *type, int r)
4201 Sym *s;
4203 s = sym_find(v);
4204 if (!s) {
4205 /* push forward reference */
4206 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4207 s->type.t |= VT_EXTERN;
4208 } else {
4209 if (!is_compatible_types(&s->type, type))
4210 error("incompatible types for redefinition of '%s'",
4211 get_tok_str(v, NULL));
4213 return s;
4216 /* push a reference to global symbol v */
4217 static void vpush_global_sym(CType *type, int v)
4219 Sym *sym;
4220 CValue cval;
4222 sym = external_global_sym(v, type, 0);
4223 cval.ul = 0;
4224 vsetc(type, VT_CONST | VT_SYM, &cval);
4225 vtop->sym = sym;
4228 void vset(CType *type, int r, int v)
4230 CValue cval;
4232 cval.i = v;
4233 vsetc(type, r, &cval);
4236 void vseti(int r, int v)
4238 CType type;
4239 type.t = VT_INT;
4240 vset(&type, r, v);
4243 void vswap(void)
4245 SValue tmp;
4247 tmp = vtop[0];
4248 vtop[0] = vtop[-1];
4249 vtop[-1] = tmp;
4252 void vpushv(SValue *v)
4254 if (vtop >= vstack + VSTACK_SIZE)
4255 error("memory full");
4256 vtop++;
4257 *vtop = *v;
4260 void vdup(void)
4262 vpushv(vtop);
4265 /* save r to the memory stack, and mark it as being free */
4266 void save_reg(int r)
4268 int l, saved, size, align;
4269 SValue *p, sv;
4270 CType *type;
4272 /* modify all stack values */
4273 saved = 0;
4274 l = 0;
4275 for(p=vstack;p<=vtop;p++) {
4276 if ((p->r & VT_VALMASK) == r ||
4277 (p->r2 & VT_VALMASK) == r) {
4278 /* must save value on stack if not already done */
4279 if (!saved) {
4280 /* NOTE: must reload 'r' because r might be equal to r2 */
4281 r = p->r & VT_VALMASK;
4282 /* store register in the stack */
4283 type = &p->type;
4284 if ((p->r & VT_LVAL) ||
4285 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4286 type = &int_type;
4287 size = type_size(type, &align);
4288 loc = (loc - size) & -align;
4289 sv.type.t = type->t;
4290 sv.r = VT_LOCAL | VT_LVAL;
4291 sv.c.ul = loc;
4292 store(r, &sv);
4293 #ifdef TCC_TARGET_I386
4294 /* x86 specific: need to pop fp register ST0 if saved */
4295 if (r == TREG_ST0) {
4296 o(0xd9dd); /* fstp %st(1) */
4298 #endif
4299 /* special long long case */
4300 if ((type->t & VT_BTYPE) == VT_LLONG) {
4301 sv.c.ul += 4;
4302 store(p->r2, &sv);
4304 l = loc;
4305 saved = 1;
4307 /* mark that stack entry as being saved on the stack */
4308 if (p->r & VT_LVAL) {
4309 /* also clear the bounded flag because the
4310 relocation address of the function was stored in
4311 p->c.ul */
4312 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4313 } else {
4314 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4316 p->r2 = VT_CONST;
4317 p->c.ul = l;
4322 /* find a free register of class 'rc'. If none, save one register */
4323 int get_reg(int rc)
4325 int r;
4326 SValue *p;
4328 /* find a free register */
4329 for(r=0;r<NB_REGS;r++) {
4330 if (reg_classes[r] & rc) {
4331 for(p=vstack;p<=vtop;p++) {
4332 if ((p->r & VT_VALMASK) == r ||
4333 (p->r2 & VT_VALMASK) == r)
4334 goto notfound;
4336 return r;
4338 notfound: ;
4341 /* no register left : free the first one on the stack (VERY
4342 IMPORTANT to start from the bottom to ensure that we don't
4343 spill registers used in gen_opi()) */
4344 for(p=vstack;p<=vtop;p++) {
4345 r = p->r & VT_VALMASK;
4346 if (r < VT_CONST && (reg_classes[r] & rc))
4347 goto save_found;
4348 /* also look at second register (if long long) */
4349 r = p->r2 & VT_VALMASK;
4350 if (r < VT_CONST && (reg_classes[r] & rc)) {
4351 save_found:
4352 save_reg(r);
4353 return r;
4356 /* Should never comes here */
4357 return -1;
4360 /* save registers up to (vtop - n) stack entry */
4361 void save_regs(int n)
4363 int r;
4364 SValue *p, *p1;
4365 p1 = vtop - n;
4366 for(p = vstack;p <= p1; p++) {
4367 r = p->r & VT_VALMASK;
4368 if (r < VT_CONST) {
4369 save_reg(r);
4374 /* move register 's' to 'r', and flush previous value of r to memory
4375 if needed */
4376 void move_reg(int r, int s)
4378 SValue sv;
4380 if (r != s) {
4381 save_reg(r);
4382 sv.type.t = VT_INT;
4383 sv.r = s;
4384 sv.c.ul = 0;
4385 load(r, &sv);
4389 /* get address of vtop (vtop MUST BE an lvalue) */
4390 void gaddrof(void)
4392 vtop->r &= ~VT_LVAL;
4393 /* tricky: if saved lvalue, then we can go back to lvalue */
4394 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4395 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4398 #ifdef CONFIG_TCC_BCHECK
4399 /* generate lvalue bound code */
4400 void gbound(void)
4402 int lval_type;
4403 CType type1;
4405 vtop->r &= ~VT_MUSTBOUND;
4406 /* if lvalue, then use checking code before dereferencing */
4407 if (vtop->r & VT_LVAL) {
4408 /* if not VT_BOUNDED value, then make one */
4409 if (!(vtop->r & VT_BOUNDED)) {
4410 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4411 /* must save type because we must set it to int to get pointer */
4412 type1 = vtop->type;
4413 vtop->type.t = VT_INT;
4414 gaddrof();
4415 vpushi(0);
4416 gen_bounded_ptr_add();
4417 vtop->r |= lval_type;
4418 vtop->type = type1;
4420 /* then check for dereferencing */
4421 gen_bounded_ptr_deref();
4424 #endif
4426 /* store vtop a register belonging to class 'rc'. lvalues are
4427 converted to values. Cannot be used if cannot be converted to
4428 register value (such as structures). */
4429 int gv(int rc)
4431 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4432 unsigned long long ll;
4434 /* NOTE: get_reg can modify vstack[] */
4435 if (vtop->type.t & VT_BITFIELD) {
4436 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4437 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4438 /* remove bit field info to avoid loops */
4439 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4440 /* generate shifts */
4441 vpushi(32 - (bit_pos + bit_size));
4442 gen_op(TOK_SHL);
4443 vpushi(32 - bit_size);
4444 /* NOTE: transformed to SHR if unsigned */
4445 gen_op(TOK_SAR);
4446 r = gv(rc);
4447 } else {
4448 if (is_float(vtop->type.t) &&
4449 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4450 Sym *sym;
4451 int *ptr;
4452 unsigned long offset;
4454 /* XXX: unify with initializers handling ? */
4455 /* CPUs usually cannot use float constants, so we store them
4456 generically in data segment */
4457 size = type_size(&vtop->type, &align);
4458 offset = (data_section->data_offset + align - 1) & -align;
4459 data_section->data_offset = offset;
4460 /* XXX: not portable yet */
4461 ptr = section_ptr_add(data_section, size);
4462 size = size >> 2;
4463 for(i=0;i<size;i++)
4464 ptr[i] = vtop->c.tab[i];
4465 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4466 vtop->r |= VT_LVAL | VT_SYM;
4467 vtop->sym = sym;
4468 vtop->c.ul = 0;
4470 #ifdef CONFIG_TCC_BCHECK
4471 if (vtop->r & VT_MUSTBOUND)
4472 gbound();
4473 #endif
4475 r = vtop->r & VT_VALMASK;
4476 /* need to reload if:
4477 - constant
4478 - lvalue (need to dereference pointer)
4479 - already a register, but not in the right class */
4480 if (r >= VT_CONST ||
4481 (vtop->r & VT_LVAL) ||
4482 !(reg_classes[r] & rc) ||
4483 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4484 !(reg_classes[vtop->r2] & rc))) {
4485 r = get_reg(rc);
4486 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4487 /* two register type load : expand to two words
4488 temporarily */
4489 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4490 /* load constant */
4491 ll = vtop->c.ull;
4492 vtop->c.ui = ll; /* first word */
4493 load(r, vtop);
4494 vtop->r = r; /* save register value */
4495 vpushi(ll >> 32); /* second word */
4496 } else if (r >= VT_CONST ||
4497 (vtop->r & VT_LVAL)) {
4498 /* load from memory */
4499 load(r, vtop);
4500 vdup();
4501 vtop[-1].r = r; /* save register value */
4502 /* increment pointer to get second word */
4503 vtop->type.t = VT_INT;
4504 gaddrof();
4505 vpushi(4);
4506 gen_op('+');
4507 vtop->r |= VT_LVAL;
4508 } else {
4509 /* move registers */
4510 load(r, vtop);
4511 vdup();
4512 vtop[-1].r = r; /* save register value */
4513 vtop->r = vtop[-1].r2;
4515 /* allocate second register */
4516 rc2 = RC_INT;
4517 if (rc == RC_IRET)
4518 rc2 = RC_LRET;
4519 r2 = get_reg(rc2);
4520 load(r2, vtop);
4521 vpop();
4522 /* write second register */
4523 vtop->r2 = r2;
4524 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4525 int t1, t;
4526 /* lvalue of scalar type : need to use lvalue type
4527 because of possible cast */
4528 t = vtop->type.t;
4529 t1 = t;
4530 /* compute memory access type */
4531 if (vtop->r & VT_LVAL_BYTE)
4532 t = VT_BYTE;
4533 else if (vtop->r & VT_LVAL_SHORT)
4534 t = VT_SHORT;
4535 if (vtop->r & VT_LVAL_UNSIGNED)
4536 t |= VT_UNSIGNED;
4537 vtop->type.t = t;
4538 load(r, vtop);
4539 /* restore wanted type */
4540 vtop->type.t = t1;
4541 } else {
4542 /* one register type load */
4543 load(r, vtop);
4546 vtop->r = r;
4548 return r;
4551 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4552 void gv2(int rc1, int rc2)
4554 int v;
4556 /* generate more generic register first. But VT_JMP or VT_CMP
4557 values must be generated first in all cases to avoid possible
4558 reload errors */
4559 v = vtop[0].r & VT_VALMASK;
4560 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4561 vswap();
4562 gv(rc1);
4563 vswap();
4564 gv(rc2);
4565 /* test if reload is needed for first register */
4566 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4567 vswap();
4568 gv(rc1);
4569 vswap();
4571 } else {
4572 gv(rc2);
4573 vswap();
4574 gv(rc1);
4575 vswap();
4576 /* test if reload is needed for first register */
4577 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4578 gv(rc2);
4583 /* expand long long on stack in two int registers */
4584 void lexpand(void)
4586 int u;
4588 u = vtop->type.t & VT_UNSIGNED;
4589 gv(RC_INT);
4590 vdup();
4591 vtop[0].r = vtop[-1].r2;
4592 vtop[0].r2 = VT_CONST;
4593 vtop[-1].r2 = VT_CONST;
4594 vtop[0].type.t = VT_INT | u;
4595 vtop[-1].type.t = VT_INT | u;
4598 /* build a long long from two ints */
4599 void lbuild(int t)
4601 gv2(RC_INT, RC_INT);
4602 vtop[-1].r2 = vtop[0].r;
4603 vtop[-1].type.t = t;
4604 vpop();
4607 /* rotate n first stack elements to the bottom
4608 I1 ... In -> I2 ... In I1 [top is right]
4610 void vrotb(int n)
4612 int i;
4613 SValue tmp;
4615 tmp = vtop[-n + 1];
4616 for(i=-n+1;i!=0;i++)
4617 vtop[i] = vtop[i+1];
4618 vtop[0] = tmp;
4621 /* rotate n first stack elements to the top
4622 I1 ... In -> In I1 ... I(n-1) [top is right]
4624 void vrott(int n)
4626 int i;
4627 SValue tmp;
4629 tmp = vtop[0];
4630 for(i = 0;i < n - 1; i++)
4631 vtop[-i] = vtop[-i - 1];
4632 vtop[-n + 1] = tmp;
4635 /* pop stack value */
4636 void vpop(void)
4638 int v;
4639 v = vtop->r & VT_VALMASK;
4640 #ifdef TCC_TARGET_I386
4641 /* for x86, we need to pop the FP stack */
4642 if (v == TREG_ST0 && !nocode_wanted) {
4643 o(0xd9dd); /* fstp %st(1) */
4644 } else
4645 #endif
4646 if (v == VT_JMP || v == VT_JMPI) {
4647 /* need to put correct jump if && or || without test */
4648 gsym(vtop->c.ul);
4650 vtop--;
4653 /* convert stack entry to register and duplicate its value in another
4654 register */
4655 void gv_dup(void)
4657 int rc, t, r, r1;
4658 SValue sv;
4660 t = vtop->type.t;
4661 if ((t & VT_BTYPE) == VT_LLONG) {
4662 lexpand();
4663 gv_dup();
4664 vswap();
4665 vrotb(3);
4666 gv_dup();
4667 vrotb(4);
4668 /* stack: H L L1 H1 */
4669 lbuild(t);
4670 vrotb(3);
4671 vrotb(3);
4672 vswap();
4673 lbuild(t);
4674 vswap();
4675 } else {
4676 /* duplicate value */
4677 rc = RC_INT;
4678 sv.type.t = VT_INT;
4679 if (is_float(t)) {
4680 rc = RC_FLOAT;
4681 sv.type.t = t;
4683 r = gv(rc);
4684 r1 = get_reg(rc);
4685 sv.r = r;
4686 sv.c.ul = 0;
4687 load(r1, &sv); /* move r to r1 */
4688 vdup();
4689 /* duplicates value */
4690 vtop->r = r1;
4694 /* generate CPU independent (unsigned) long long operations */
4695 void gen_opl(int op)
4697 int t, a, b, op1, c, i;
4698 int func;
4699 SValue tmp;
4701 switch(op) {
4702 case '/':
4703 case TOK_PDIV:
4704 func = TOK___divdi3;
4705 goto gen_func;
4706 case TOK_UDIV:
4707 func = TOK___udivdi3;
4708 goto gen_func;
4709 case '%':
4710 func = TOK___moddi3;
4711 goto gen_func;
4712 case TOK_UMOD:
4713 func = TOK___umoddi3;
4714 gen_func:
4715 /* call generic long long function */
4716 vpush_global_sym(&func_old_type, func);
4717 vrott(3);
4718 gfunc_call(2);
4719 vpushi(0);
4720 vtop->r = REG_IRET;
4721 vtop->r2 = REG_LRET;
4722 break;
4723 case '^':
4724 case '&':
4725 case '|':
4726 case '*':
4727 case '+':
4728 case '-':
4729 t = vtop->type.t;
4730 vswap();
4731 lexpand();
4732 vrotb(3);
4733 lexpand();
4734 /* stack: L1 H1 L2 H2 */
4735 tmp = vtop[0];
4736 vtop[0] = vtop[-3];
4737 vtop[-3] = tmp;
4738 tmp = vtop[-2];
4739 vtop[-2] = vtop[-3];
4740 vtop[-3] = tmp;
4741 vswap();
4742 /* stack: H1 H2 L1 L2 */
4743 if (op == '*') {
4744 vpushv(vtop - 1);
4745 vpushv(vtop - 1);
4746 gen_op(TOK_UMULL);
4747 lexpand();
4748 /* stack: H1 H2 L1 L2 ML MH */
4749 for(i=0;i<4;i++)
4750 vrotb(6);
4751 /* stack: ML MH H1 H2 L1 L2 */
4752 tmp = vtop[0];
4753 vtop[0] = vtop[-2];
4754 vtop[-2] = tmp;
4755 /* stack: ML MH H1 L2 H2 L1 */
4756 gen_op('*');
4757 vrotb(3);
4758 vrotb(3);
4759 gen_op('*');
4760 /* stack: ML MH M1 M2 */
4761 gen_op('+');
4762 gen_op('+');
4763 } else if (op == '+' || op == '-') {
4764 /* XXX: add non carry method too (for MIPS or alpha) */
4765 if (op == '+')
4766 op1 = TOK_ADDC1;
4767 else
4768 op1 = TOK_SUBC1;
4769 gen_op(op1);
4770 /* stack: H1 H2 (L1 op L2) */
4771 vrotb(3);
4772 vrotb(3);
4773 gen_op(op1 + 1); /* TOK_xxxC2 */
4774 } else {
4775 gen_op(op);
4776 /* stack: H1 H2 (L1 op L2) */
4777 vrotb(3);
4778 vrotb(3);
4779 /* stack: (L1 op L2) H1 H2 */
4780 gen_op(op);
4781 /* stack: (L1 op L2) (H1 op H2) */
4783 /* stack: L H */
4784 lbuild(t);
4785 break;
4786 case TOK_SAR:
4787 case TOK_SHR:
4788 case TOK_SHL:
4789 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4790 t = vtop[-1].type.t;
4791 vswap();
4792 lexpand();
4793 vrotb(3);
4794 /* stack: L H shift */
4795 c = (int)vtop->c.i;
4796 /* constant: simpler */
4797 /* NOTE: all comments are for SHL. the other cases are
4798 done by swaping words */
4799 vpop();
4800 if (op != TOK_SHL)
4801 vswap();
4802 if (c >= 32) {
4803 /* stack: L H */
4804 vpop();
4805 if (c > 32) {
4806 vpushi(c - 32);
4807 gen_op(op);
4809 if (op != TOK_SAR) {
4810 vpushi(0);
4811 } else {
4812 gv_dup();
4813 vpushi(31);
4814 gen_op(TOK_SAR);
4816 vswap();
4817 } else {
4818 vswap();
4819 gv_dup();
4820 /* stack: H L L */
4821 vpushi(c);
4822 gen_op(op);
4823 vswap();
4824 vpushi(32 - c);
4825 if (op == TOK_SHL)
4826 gen_op(TOK_SHR);
4827 else
4828 gen_op(TOK_SHL);
4829 vrotb(3);
4830 /* stack: L L H */
4831 vpushi(c);
4832 if (op == TOK_SHL)
4833 gen_op(TOK_SHL);
4834 else
4835 gen_op(TOK_SHR);
4836 gen_op('|');
4838 if (op != TOK_SHL)
4839 vswap();
4840 lbuild(t);
4841 } else {
4842 /* XXX: should provide a faster fallback on x86 ? */
4843 switch(op) {
4844 case TOK_SAR:
4845 func = TOK___sardi3;
4846 goto gen_func;
4847 case TOK_SHR:
4848 func = TOK___shrdi3;
4849 goto gen_func;
4850 case TOK_SHL:
4851 func = TOK___shldi3;
4852 goto gen_func;
4855 break;
4856 default:
4857 /* compare operations */
4858 t = vtop->type.t;
4859 vswap();
4860 lexpand();
4861 vrotb(3);
4862 lexpand();
4863 /* stack: L1 H1 L2 H2 */
4864 tmp = vtop[-1];
4865 vtop[-1] = vtop[-2];
4866 vtop[-2] = tmp;
4867 /* stack: L1 L2 H1 H2 */
4868 /* compare high */
4869 op1 = op;
4870 /* when values are equal, we need to compare low words. since
4871 the jump is inverted, we invert the test too. */
4872 if (op1 == TOK_LT)
4873 op1 = TOK_LE;
4874 else if (op1 == TOK_GT)
4875 op1 = TOK_GE;
4876 else if (op1 == TOK_ULT)
4877 op1 = TOK_ULE;
4878 else if (op1 == TOK_UGT)
4879 op1 = TOK_UGE;
4880 a = 0;
4881 b = 0;
4882 gen_op(op1);
4883 if (op1 != TOK_NE) {
4884 a = gtst(1, 0);
4886 if (op != TOK_EQ) {
4887 /* generate non equal test */
4888 /* XXX: NOT PORTABLE yet */
4889 if (a == 0) {
4890 b = gtst(0, 0);
4891 } else {
4892 #ifdef TCC_TARGET_I386
4893 b = psym(0x850f, 0);
4894 #else
4895 error("not implemented");
4896 #endif
4899 /* compare low. Always unsigned */
4900 op1 = op;
4901 if (op1 == TOK_LT)
4902 op1 = TOK_ULT;
4903 else if (op1 == TOK_LE)
4904 op1 = TOK_ULE;
4905 else if (op1 == TOK_GT)
4906 op1 = TOK_UGT;
4907 else if (op1 == TOK_GE)
4908 op1 = TOK_UGE;
4909 gen_op(op1);
4910 a = gtst(1, a);
4911 gsym(b);
4912 vseti(VT_JMPI, a);
4913 break;
4917 /* handle integer constant optimizations and various machine
4918 independent opt */
4919 void gen_opic(int op)
4921 int fc, c1, c2, n;
4922 SValue *v1, *v2;
4924 v1 = vtop - 1;
4925 v2 = vtop;
4926 /* currently, we cannot do computations with forward symbols */
4927 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4928 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4929 if (c1 && c2) {
4930 fc = v2->c.i;
4931 switch(op) {
4932 case '+': v1->c.i += fc; break;
4933 case '-': v1->c.i -= fc; break;
4934 case '&': v1->c.i &= fc; break;
4935 case '^': v1->c.i ^= fc; break;
4936 case '|': v1->c.i |= fc; break;
4937 case '*': v1->c.i *= fc; break;
4939 case TOK_PDIV:
4940 case '/':
4941 case '%':
4942 case TOK_UDIV:
4943 case TOK_UMOD:
4944 /* if division by zero, generate explicit division */
4945 if (fc == 0) {
4946 if (const_wanted)
4947 error("division by zero in constant");
4948 goto general_case;
4950 switch(op) {
4951 default: v1->c.i /= fc; break;
4952 case '%': v1->c.i %= fc; break;
4953 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
4954 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
4956 break;
4957 case TOK_SHL: v1->c.i <<= fc; break;
4958 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
4959 case TOK_SAR: v1->c.i >>= fc; break;
4960 /* tests */
4961 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
4962 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
4963 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
4964 case TOK_NE: v1->c.i = v1->c.i != fc; break;
4965 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
4966 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
4967 case TOK_LT: v1->c.i = v1->c.i < fc; break;
4968 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
4969 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
4970 case TOK_GT: v1->c.i = v1->c.i > fc; break;
4971 /* logical */
4972 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
4973 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
4974 default:
4975 goto general_case;
4977 vtop--;
4978 } else {
4979 /* if commutative ops, put c2 as constant */
4980 if (c1 && (op == '+' || op == '&' || op == '^' ||
4981 op == '|' || op == '*')) {
4982 vswap();
4983 swap(&c1, &c2);
4985 fc = vtop->c.i;
4986 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
4987 op == TOK_PDIV) &&
4988 fc == 1) ||
4989 ((op == '+' || op == '-' || op == '|' || op == '^' ||
4990 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
4991 fc == 0) ||
4992 (op == '&' &&
4993 fc == -1))) {
4994 /* nothing to do */
4995 vtop--;
4996 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
4997 /* try to use shifts instead of muls or divs */
4998 if (fc > 0 && (fc & (fc - 1)) == 0) {
4999 n = -1;
5000 while (fc) {
5001 fc >>= 1;
5002 n++;
5004 vtop->c.i = n;
5005 if (op == '*')
5006 op = TOK_SHL;
5007 else if (op == TOK_PDIV)
5008 op = TOK_SAR;
5009 else
5010 op = TOK_SHR;
5012 goto general_case;
5013 } else if (c2 && (op == '+' || op == '-') &&
5014 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5015 (VT_CONST | VT_SYM)) {
5016 /* symbol + constant case */
5017 if (op == '-')
5018 fc = -fc;
5019 vtop--;
5020 vtop->c.i += fc;
5021 } else {
5022 general_case:
5023 if (!nocode_wanted) {
5024 /* call low level op generator */
5025 gen_opi(op);
5026 } else {
5027 vtop--;
5033 /* generate a floating point operation with constant propagation */
5034 void gen_opif(int op)
5036 int c1, c2;
5037 SValue *v1, *v2;
5038 long double f1, f2;
5040 v1 = vtop - 1;
5041 v2 = vtop;
5042 /* currently, we cannot do computations with forward symbols */
5043 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5044 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5045 if (c1 && c2) {
5046 if (v1->type.t == VT_FLOAT) {
5047 f1 = v1->c.f;
5048 f2 = v2->c.f;
5049 } else if (v1->type.t == VT_DOUBLE) {
5050 f1 = v1->c.d;
5051 f2 = v2->c.d;
5052 } else {
5053 f1 = v1->c.ld;
5054 f2 = v2->c.ld;
5057 /* NOTE: we only do constant propagation if finite number (not
5058 NaN or infinity) (ANSI spec) */
5059 if (!ieee_finite(f1) || !ieee_finite(f2))
5060 goto general_case;
5062 switch(op) {
5063 case '+': f1 += f2; break;
5064 case '-': f1 -= f2; break;
5065 case '*': f1 *= f2; break;
5066 case '/':
5067 if (f2 == 0.0) {
5068 if (const_wanted)
5069 error("division by zero in constant");
5070 goto general_case;
5072 f1 /= f2;
5073 break;
5074 /* XXX: also handles tests ? */
5075 default:
5076 goto general_case;
5078 /* XXX: overflow test ? */
5079 if (v1->type.t == VT_FLOAT) {
5080 v1->c.f = f1;
5081 } else if (v1->type.t == VT_DOUBLE) {
5082 v1->c.d = f1;
5083 } else {
5084 v1->c.ld = f1;
5086 vtop--;
5087 } else {
5088 general_case:
5089 if (!nocode_wanted) {
5090 gen_opf(op);
5091 } else {
5092 vtop--;
5097 static int pointed_size(CType *type)
5099 int align;
5100 return type_size(pointed_type(type), &align);
5103 static inline int is_null_pointer(SValue *p)
5105 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5106 return 0;
5107 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5108 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5111 static inline int is_integer_btype(int bt)
5113 return (bt == VT_BYTE || bt == VT_SHORT ||
5114 bt == VT_INT || bt == VT_LLONG);
5117 /* check types for comparison or substraction of pointers */
5118 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5120 CType *type1, *type2, tmp_type1, tmp_type2;
5121 int bt1, bt2;
5123 /* null pointers are accepted for all comparisons as gcc */
5124 if (is_null_pointer(p1) || is_null_pointer(p2))
5125 return;
5126 type1 = &p1->type;
5127 type2 = &p2->type;
5128 bt1 = type1->t & VT_BTYPE;
5129 bt2 = type2->t & VT_BTYPE;
5130 /* accept comparison between pointer and integer with a warning */
5131 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5132 warning("comparison between pointer and integer");
5133 return;
5136 /* both must be pointers or implicit function pointers */
5137 if (bt1 == VT_PTR) {
5138 type1 = pointed_type(type1);
5139 } else if (bt1 != VT_FUNC)
5140 goto invalid_operands;
5142 if (bt2 == VT_PTR) {
5143 type2 = pointed_type(type2);
5144 } else if (bt2 != VT_FUNC) {
5145 invalid_operands:
5146 error("invalid operands to binary %s", get_tok_str(op, NULL));
5148 if ((type1->t & VT_BTYPE) == VT_VOID ||
5149 (type2->t & VT_BTYPE) == VT_VOID)
5150 return;
5151 tmp_type1 = *type1;
5152 tmp_type2 = *type2;
5153 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5154 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5155 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5156 /* gcc-like error if '-' is used */
5157 if (op == '-')
5158 goto invalid_operands;
5159 else
5160 warning("comparison of distinct pointer types lacks a cast");
5164 /* generic gen_op: handles types problems */
5165 void gen_op(int op)
5167 int u, t1, t2, bt1, bt2, t;
5168 CType type1;
5170 t1 = vtop[-1].type.t;
5171 t2 = vtop[0].type.t;
5172 bt1 = t1 & VT_BTYPE;
5173 bt2 = t2 & VT_BTYPE;
5175 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5176 /* at least one operand is a pointer */
5177 /* relationnal op: must be both pointers */
5178 if (op >= TOK_ULT && op <= TOK_GT) {
5179 check_comparison_pointer_types(vtop - 1, vtop, op);
5180 /* pointers are handled are unsigned */
5181 t = VT_INT | VT_UNSIGNED;
5182 goto std_op;
5184 /* if both pointers, then it must be the '-' op */
5185 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5186 if (op != '-')
5187 error("cannot use pointers here");
5188 check_comparison_pointer_types(vtop - 1, vtop, op);
5189 /* XXX: check that types are compatible */
5190 u = pointed_size(&vtop[-1].type);
5191 gen_opic(op);
5192 /* set to integer type */
5193 vtop->type.t = VT_INT;
5194 vpushi(u);
5195 gen_op(TOK_PDIV);
5196 } else {
5197 /* exactly one pointer : must be '+' or '-'. */
5198 if (op != '-' && op != '+')
5199 error("cannot use pointers here");
5200 /* Put pointer as first operand */
5201 if (bt2 == VT_PTR) {
5202 vswap();
5203 swap(&t1, &t2);
5205 type1 = vtop[-1].type;
5206 /* XXX: cast to int ? (long long case) */
5207 vpushi(pointed_size(&vtop[-1].type));
5208 gen_op('*');
5209 #ifdef CONFIG_TCC_BCHECK
5210 /* if evaluating constant expression, no code should be
5211 generated, so no bound check */
5212 if (do_bounds_check && !const_wanted) {
5213 /* if bounded pointers, we generate a special code to
5214 test bounds */
5215 if (op == '-') {
5216 vpushi(0);
5217 vswap();
5218 gen_op('-');
5220 gen_bounded_ptr_add();
5221 } else
5222 #endif
5224 gen_opic(op);
5226 /* put again type if gen_opic() swaped operands */
5227 vtop->type = type1;
5229 } else if (is_float(bt1) || is_float(bt2)) {
5230 /* compute bigger type and do implicit casts */
5231 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5232 t = VT_LDOUBLE;
5233 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5234 t = VT_DOUBLE;
5235 } else {
5236 t = VT_FLOAT;
5238 /* floats can only be used for a few operations */
5239 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5240 (op < TOK_ULT || op > TOK_GT))
5241 error("invalid operands for binary operation");
5242 goto std_op;
5243 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5244 /* cast to biggest op */
5245 t = VT_LLONG;
5246 /* convert to unsigned if it does not fit in a long long */
5247 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5248 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5249 t |= VT_UNSIGNED;
5250 goto std_op;
5251 } else {
5252 /* integer operations */
5253 t = VT_INT;
5254 /* convert to unsigned if it does not fit in an integer */
5255 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5256 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5257 t |= VT_UNSIGNED;
5258 std_op:
5259 /* XXX: currently, some unsigned operations are explicit, so
5260 we modify them here */
5261 if (t & VT_UNSIGNED) {
5262 if (op == TOK_SAR)
5263 op = TOK_SHR;
5264 else if (op == '/')
5265 op = TOK_UDIV;
5266 else if (op == '%')
5267 op = TOK_UMOD;
5268 else if (op == TOK_LT)
5269 op = TOK_ULT;
5270 else if (op == TOK_GT)
5271 op = TOK_UGT;
5272 else if (op == TOK_LE)
5273 op = TOK_ULE;
5274 else if (op == TOK_GE)
5275 op = TOK_UGE;
5277 vswap();
5278 type1.t = t;
5279 gen_cast(&type1);
5280 vswap();
5281 /* special case for shifts and long long: we keep the shift as
5282 an integer */
5283 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5284 type1.t = VT_INT;
5285 gen_cast(&type1);
5286 if (is_float(t))
5287 gen_opif(op);
5288 else if ((t & VT_BTYPE) == VT_LLONG)
5289 gen_opl(op);
5290 else
5291 gen_opic(op);
5292 if (op >= TOK_ULT && op <= TOK_GT) {
5293 /* relationnal op: the result is an int */
5294 vtop->type.t = VT_INT;
5295 } else {
5296 vtop->type.t = t;
5301 /* generic itof for unsigned long long case */
5302 void gen_cvt_itof1(int t)
5304 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5305 (VT_LLONG | VT_UNSIGNED)) {
5307 if (t == VT_FLOAT)
5308 vpush_global_sym(&func_old_type, TOK___ulltof);
5309 else if (t == VT_DOUBLE)
5310 vpush_global_sym(&func_old_type, TOK___ulltod);
5311 else
5312 vpush_global_sym(&func_old_type, TOK___ulltold);
5313 vrott(2);
5314 gfunc_call(1);
5315 vpushi(0);
5316 vtop->r = REG_FRET;
5317 } else {
5318 gen_cvt_itof(t);
5322 /* generic ftoi for unsigned long long case */
5323 void gen_cvt_ftoi1(int t)
5325 int st;
5327 if (t == (VT_LLONG | VT_UNSIGNED)) {
5328 /* not handled natively */
5329 st = vtop->type.t & VT_BTYPE;
5330 if (st == VT_FLOAT)
5331 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5332 else if (st == VT_DOUBLE)
5333 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5334 else
5335 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5336 vrott(2);
5337 gfunc_call(1);
5338 vpushi(0);
5339 vtop->r = REG_IRET;
5340 vtop->r2 = REG_LRET;
5341 } else {
5342 gen_cvt_ftoi(t);
5346 /* force char or short cast */
5347 void force_charshort_cast(int t)
5349 int bits, dbt;
5350 dbt = t & VT_BTYPE;
5351 /* XXX: add optimization if lvalue : just change type and offset */
5352 if (dbt == VT_BYTE)
5353 bits = 8;
5354 else
5355 bits = 16;
5356 if (t & VT_UNSIGNED) {
5357 vpushi((1 << bits) - 1);
5358 gen_op('&');
5359 } else {
5360 bits = 32 - bits;
5361 vpushi(bits);
5362 gen_op(TOK_SHL);
5363 vpushi(bits);
5364 gen_op(TOK_SAR);
5368 /* cast 'vtop' to 'type' */
5369 static void gen_cast(CType *type)
5371 int sbt, dbt, sf, df, c;
5373 /* special delayed cast for char/short */
5374 /* XXX: in some cases (multiple cascaded casts), it may still
5375 be incorrect */
5376 if (vtop->r & VT_MUSTCAST) {
5377 vtop->r &= ~VT_MUSTCAST;
5378 force_charshort_cast(vtop->type.t);
5381 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5382 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5384 if (sbt != dbt && !nocode_wanted) {
5385 sf = is_float(sbt);
5386 df = is_float(dbt);
5387 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5388 if (sf && df) {
5389 /* convert from fp to fp */
5390 if (c) {
5391 /* constant case: we can do it now */
5392 /* XXX: in ISOC, cannot do it if error in convert */
5393 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5394 vtop->c.f = (float)vtop->c.d;
5395 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5396 vtop->c.f = (float)vtop->c.ld;
5397 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5398 vtop->c.d = (double)vtop->c.f;
5399 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5400 vtop->c.d = (double)vtop->c.ld;
5401 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5402 vtop->c.ld = (long double)vtop->c.f;
5403 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5404 vtop->c.ld = (long double)vtop->c.d;
5405 } else {
5406 /* non constant case: generate code */
5407 gen_cvt_ftof(dbt);
5409 } else if (df) {
5410 /* convert int to fp */
5411 if (c) {
5412 switch(sbt) {
5413 case VT_LLONG | VT_UNSIGNED:
5414 case VT_LLONG:
5415 /* XXX: add const cases for long long */
5416 goto do_itof;
5417 case VT_INT | VT_UNSIGNED:
5418 switch(dbt) {
5419 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5420 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5421 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5423 break;
5424 default:
5425 switch(dbt) {
5426 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5427 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5428 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5430 break;
5432 } else {
5433 do_itof:
5434 gen_cvt_itof1(dbt);
5436 } else if (sf) {
5437 /* convert fp to int */
5438 /* we handle char/short/etc... with generic code */
5439 if (dbt != (VT_INT | VT_UNSIGNED) &&
5440 dbt != (VT_LLONG | VT_UNSIGNED) &&
5441 dbt != VT_LLONG)
5442 dbt = VT_INT;
5443 if (c) {
5444 switch(dbt) {
5445 case VT_LLONG | VT_UNSIGNED:
5446 case VT_LLONG:
5447 /* XXX: add const cases for long long */
5448 goto do_ftoi;
5449 case VT_INT | VT_UNSIGNED:
5450 switch(sbt) {
5451 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5452 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5453 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5455 break;
5456 default:
5457 /* int case */
5458 switch(sbt) {
5459 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5460 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5461 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5463 break;
5465 } else {
5466 do_ftoi:
5467 gen_cvt_ftoi1(dbt);
5469 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5470 /* additional cast for char/short/bool... */
5471 vtop->type.t = dbt;
5472 gen_cast(type);
5474 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5475 if ((sbt & VT_BTYPE) != VT_LLONG) {
5476 /* scalar to long long */
5477 if (c) {
5478 if (sbt == (VT_INT | VT_UNSIGNED))
5479 vtop->c.ll = vtop->c.ui;
5480 else
5481 vtop->c.ll = vtop->c.i;
5482 } else {
5483 /* machine independent conversion */
5484 gv(RC_INT);
5485 /* generate high word */
5486 if (sbt == (VT_INT | VT_UNSIGNED)) {
5487 vpushi(0);
5488 gv(RC_INT);
5489 } else {
5490 gv_dup();
5491 vpushi(31);
5492 gen_op(TOK_SAR);
5494 /* patch second register */
5495 vtop[-1].r2 = vtop->r;
5496 vpop();
5499 } else if (dbt == VT_BOOL) {
5500 /* scalar to bool */
5501 vpushi(0);
5502 gen_op(TOK_NE);
5503 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5504 (dbt & VT_BTYPE) == VT_SHORT) {
5505 force_charshort_cast(dbt);
5506 } else if ((dbt & VT_BTYPE) == VT_INT) {
5507 /* scalar to int */
5508 if (sbt == VT_LLONG) {
5509 /* from long long: just take low order word */
5510 lexpand();
5511 vpop();
5513 /* if lvalue and single word type, nothing to do because
5514 the lvalue already contains the real type size (see
5515 VT_LVAL_xxx constants) */
5518 vtop->type = *type;
5521 /* return type size. Put alignment at 'a' */
5522 static int type_size(CType *type, int *a)
5524 Sym *s;
5525 int bt;
5527 bt = type->t & VT_BTYPE;
5528 if (bt == VT_STRUCT) {
5529 /* struct/union */
5530 s = type->ref;
5531 *a = s->r;
5532 return s->c;
5533 } else if (bt == VT_PTR) {
5534 if (type->t & VT_ARRAY) {
5535 s = type->ref;
5536 return type_size(&s->type, a) * s->c;
5537 } else {
5538 *a = PTR_SIZE;
5539 return PTR_SIZE;
5541 } else if (bt == VT_LDOUBLE) {
5542 *a = LDOUBLE_ALIGN;
5543 return LDOUBLE_SIZE;
5544 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5545 *a = 4; /* XXX: i386 specific */
5546 return 8;
5547 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5548 *a = 4;
5549 return 4;
5550 } else if (bt == VT_SHORT) {
5551 *a = 2;
5552 return 2;
5553 } else {
5554 /* char, void, function, _Bool */
5555 *a = 1;
5556 return 1;
5560 /* return the pointed type of t */
5561 static inline CType *pointed_type(CType *type)
5563 return &type->ref->type;
5566 /* modify type so that its it is a pointer to type. */
5567 static void mk_pointer(CType *type)
5569 Sym *s;
5570 s = sym_push(SYM_FIELD, type, 0, -1);
5571 type->t = VT_PTR | (type->t & ~VT_TYPE);
5572 type->ref = s;
5575 /* compare function types. OLD functions match any new functions */
5576 static int is_compatible_func(CType *type1, CType *type2)
5578 Sym *s1, *s2;
5580 s1 = type1->ref;
5581 s2 = type2->ref;
5582 if (!is_compatible_types(&s1->type, &s2->type))
5583 return 0;
5584 /* XXX: not complete */
5585 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5586 return 1;
5587 if (s1->c != s2->c)
5588 return 0;
5589 while (s1 != NULL) {
5590 if (s2 == NULL)
5591 return 0;
5592 if (!is_compatible_types(&s1->type, &s2->type))
5593 return 0;
5594 s1 = s1->next;
5595 s2 = s2->next;
5597 if (s2)
5598 return 0;
5599 return 1;
5602 /* return true if type1 and type2 are exactly the same (including
5603 qualifiers).
5605 - enums are not checked as gcc __builtin_types_compatible_p ()
5607 static int is_compatible_types(CType *type1, CType *type2)
5609 int bt1, t1, t2;
5611 t1 = type1->t & VT_TYPE;
5612 t2 = type2->t & VT_TYPE;
5613 /* XXX: bitfields ? */
5614 if (t1 != t2)
5615 return 0;
5616 /* test more complicated cases */
5617 bt1 = t1 & VT_BTYPE;
5618 if (bt1 == VT_PTR) {
5619 type1 = pointed_type(type1);
5620 type2 = pointed_type(type2);
5621 return is_compatible_types(type1, type2);
5622 } else if (bt1 == VT_STRUCT) {
5623 return (type1->ref == type2->ref);
5624 } else if (bt1 == VT_FUNC) {
5625 return is_compatible_func(type1, type2);
5626 } else {
5627 return 1;
5631 /* print a type. If 'varstr' is not NULL, then the variable is also
5632 printed in the type */
5633 /* XXX: union */
5634 /* XXX: add array and function pointers */
5635 void type_to_str(char *buf, int buf_size,
5636 CType *type, const char *varstr)
5638 int bt, v, t;
5639 Sym *s, *sa;
5640 char buf1[256];
5641 const char *tstr;
5643 t = type->t & VT_TYPE;
5644 bt = t & VT_BTYPE;
5645 buf[0] = '\0';
5646 if (t & VT_CONSTANT)
5647 pstrcat(buf, buf_size, "const ");
5648 if (t & VT_VOLATILE)
5649 pstrcat(buf, buf_size, "volatile ");
5650 if (t & VT_UNSIGNED)
5651 pstrcat(buf, buf_size, "unsigned ");
5652 switch(bt) {
5653 case VT_VOID:
5654 tstr = "void";
5655 goto add_tstr;
5656 case VT_BOOL:
5657 tstr = "_Bool";
5658 goto add_tstr;
5659 case VT_BYTE:
5660 tstr = "char";
5661 goto add_tstr;
5662 case VT_SHORT:
5663 tstr = "short";
5664 goto add_tstr;
5665 case VT_INT:
5666 tstr = "int";
5667 goto add_tstr;
5668 case VT_LONG:
5669 tstr = "long";
5670 goto add_tstr;
5671 case VT_LLONG:
5672 tstr = "long long";
5673 goto add_tstr;
5674 case VT_FLOAT:
5675 tstr = "float";
5676 goto add_tstr;
5677 case VT_DOUBLE:
5678 tstr = "double";
5679 goto add_tstr;
5680 case VT_LDOUBLE:
5681 tstr = "long double";
5682 add_tstr:
5683 pstrcat(buf, buf_size, tstr);
5684 break;
5685 case VT_ENUM:
5686 case VT_STRUCT:
5687 if (bt == VT_STRUCT)
5688 tstr = "struct ";
5689 else
5690 tstr = "enum ";
5691 pstrcat(buf, buf_size, tstr);
5692 v = type->ref->v & ~SYM_STRUCT;
5693 if (v >= SYM_FIRST_ANOM)
5694 pstrcat(buf, buf_size, "<anonymous>");
5695 else
5696 pstrcat(buf, buf_size, get_tok_str(v, NULL));
5697 break;
5698 case VT_FUNC:
5699 s = type->ref;
5700 type_to_str(buf, buf_size, &s->type, varstr);
5701 pstrcat(buf, buf_size, "(");
5702 sa = s->next;
5703 while (sa != NULL) {
5704 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
5705 pstrcat(buf, buf_size, buf1);
5706 sa = sa->next;
5707 if (sa)
5708 pstrcat(buf, buf_size, ", ");
5710 pstrcat(buf, buf_size, ")");
5711 goto no_var;
5712 case VT_PTR:
5713 s = type->ref;
5714 pstrcpy(buf1, sizeof(buf1), "*");
5715 if (varstr)
5716 pstrcat(buf1, sizeof(buf1), varstr);
5717 type_to_str(buf, buf_size, &s->type, buf1);
5718 goto no_var;
5720 if (varstr) {
5721 pstrcat(buf, buf_size, " ");
5722 pstrcat(buf, buf_size, varstr);
5724 no_var: ;
5727 /* verify type compatibility to store vtop in 'dt' type, and generate
5728 casts if needed. */
5729 static void gen_assign_cast(CType *dt)
5731 CType *st, *type1, *type2, tmp_type1, tmp_type2;
5732 char buf1[256], buf2[256];
5733 int dbt, sbt;
5735 st = &vtop->type; /* source type */
5736 dbt = dt->t & VT_BTYPE;
5737 sbt = st->t & VT_BTYPE;
5738 if (dt->t & VT_CONSTANT)
5739 warning("assignment of read-only location");
5740 switch(dbt) {
5741 case VT_PTR:
5742 /* special cases for pointers */
5743 /* '0' can also be a pointer */
5744 if (is_null_pointer(vtop))
5745 goto type_ok;
5746 /* accept implicit pointer to integer cast with warning */
5747 if (is_integer_btype(sbt)) {
5748 warning("assignment makes pointer from integer without a cast");
5749 goto type_ok;
5751 type1 = pointed_type(dt);
5752 /* a function is implicitely a function pointer */
5753 if (sbt == VT_FUNC) {
5754 if ((type1->t & VT_BTYPE) != VT_VOID &&
5755 !is_compatible_types(pointed_type(dt), st))
5756 goto error;
5757 else
5758 goto type_ok;
5760 if (sbt != VT_PTR)
5761 goto error;
5762 type2 = pointed_type(st);
5763 if ((type1->t & VT_BTYPE) == VT_VOID ||
5764 (type2->t & VT_BTYPE) == VT_VOID) {
5765 /* void * can match anything */
5766 } else {
5767 /* exact type match, except for unsigned */
5768 tmp_type1 = *type1;
5769 tmp_type2 = *type2;
5770 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5771 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5772 if (!is_compatible_types(&tmp_type1, &tmp_type2))
5773 goto error;
5775 /* check const and volatile */
5776 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
5777 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
5778 warning("assignment discards qualifiers from pointer target type");
5779 break;
5780 case VT_BYTE:
5781 case VT_SHORT:
5782 case VT_INT:
5783 case VT_LLONG:
5784 if (sbt == VT_PTR || sbt == VT_FUNC) {
5785 warning("assignment makes integer from pointer without a cast");
5787 /* XXX: more tests */
5788 break;
5789 case VT_STRUCT:
5790 if (!is_compatible_types(dt, st)) {
5791 error:
5792 type_to_str(buf1, sizeof(buf1), st, NULL);
5793 type_to_str(buf2, sizeof(buf2), dt, NULL);
5794 error("cannot cast '%s' to '%s'", buf1, buf2);
5796 break;
5798 type_ok:
5799 gen_cast(dt);
5802 /* store vtop in lvalue pushed on stack */
5803 void vstore(void)
5805 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5807 ft = vtop[-1].type.t;
5808 sbt = vtop->type.t & VT_BTYPE;
5809 dbt = ft & VT_BTYPE;
5810 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
5811 (sbt == VT_INT && dbt == VT_SHORT)) {
5812 /* optimize char/short casts */
5813 delayed_cast = VT_MUSTCAST;
5814 vtop->type.t = ft & VT_TYPE;
5815 /* XXX: factorize */
5816 if (ft & VT_CONSTANT)
5817 warning("assignment of read-only location");
5818 } else {
5819 delayed_cast = 0;
5820 gen_assign_cast(&vtop[-1].type);
5823 if (sbt == VT_STRUCT) {
5824 /* if structure, only generate pointer */
5825 /* structure assignment : generate memcpy */
5826 /* XXX: optimize if small size */
5827 if (!nocode_wanted) {
5828 size = type_size(&vtop->type, &align);
5830 vpush_global_sym(&func_old_type, TOK_memcpy);
5832 /* destination */
5833 vpushv(vtop - 2);
5834 vtop->type.t = VT_INT;
5835 gaddrof();
5836 /* source */
5837 vpushv(vtop - 2);
5838 vtop->type.t = VT_INT;
5839 gaddrof();
5840 /* type size */
5841 vpushi(size);
5842 gfunc_call(3);
5844 vswap();
5845 vpop();
5846 } else {
5847 vswap();
5848 vpop();
5850 /* leave source on stack */
5851 } else if (ft & VT_BITFIELD) {
5852 /* bitfield store handling */
5853 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
5854 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5855 /* remove bit field info to avoid loops */
5856 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
5858 /* duplicate destination */
5859 vdup();
5860 vtop[-1] = vtop[-2];
5862 /* mask and shift source */
5863 vpushi((1 << bit_size) - 1);
5864 gen_op('&');
5865 vpushi(bit_pos);
5866 gen_op(TOK_SHL);
5867 /* load destination, mask and or with source */
5868 vswap();
5869 vpushi(~(((1 << bit_size) - 1) << bit_pos));
5870 gen_op('&');
5871 gen_op('|');
5872 /* store result */
5873 vstore();
5874 } else {
5875 #ifdef CONFIG_TCC_BCHECK
5876 /* bound check case */
5877 if (vtop[-1].r & VT_MUSTBOUND) {
5878 vswap();
5879 gbound();
5880 vswap();
5882 #endif
5883 if (!nocode_wanted) {
5884 rc = RC_INT;
5885 if (is_float(ft))
5886 rc = RC_FLOAT;
5887 r = gv(rc); /* generate value */
5888 /* if lvalue was saved on stack, must read it */
5889 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
5890 SValue sv;
5891 t = get_reg(RC_INT);
5892 sv.type.t = VT_INT;
5893 sv.r = VT_LOCAL | VT_LVAL;
5894 sv.c.ul = vtop[-1].c.ul;
5895 load(t, &sv);
5896 vtop[-1].r = t | VT_LVAL;
5898 store(r, vtop - 1);
5899 /* two word case handling : store second register at word + 4 */
5900 if ((ft & VT_BTYPE) == VT_LLONG) {
5901 vswap();
5902 /* convert to int to increment easily */
5903 vtop->type.t = VT_INT;
5904 gaddrof();
5905 vpushi(4);
5906 gen_op('+');
5907 vtop->r |= VT_LVAL;
5908 vswap();
5909 /* XXX: it works because r2 is spilled last ! */
5910 store(vtop->r2, vtop - 1);
5913 vswap();
5914 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5915 vtop->r |= delayed_cast;
5919 /* post defines POST/PRE add. c is the token ++ or -- */
5920 void inc(int post, int c)
5922 test_lvalue();
5923 vdup(); /* save lvalue */
5924 if (post) {
5925 gv_dup(); /* duplicate value */
5926 vrotb(3);
5927 vrotb(3);
5929 /* add constant */
5930 vpushi(c - TOK_MID);
5931 gen_op('+');
5932 vstore(); /* store value */
5933 if (post)
5934 vpop(); /* if post op, return saved value */
5937 /* Parse GNUC __attribute__ extension. Currently, the following
5938 extensions are recognized:
5939 - aligned(n) : set data/function alignment.
5940 - section(x) : generate data/code in this section.
5941 - unused : currently ignored, but may be used someday.
5943 static void parse_attribute(AttributeDef *ad)
5945 int t, n;
5947 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
5948 next();
5949 skip('(');
5950 skip('(');
5951 while (tok != ')') {
5952 if (tok < TOK_IDENT)
5953 expect("attribute name");
5954 t = tok;
5955 next();
5956 switch(t) {
5957 case TOK_SECTION1:
5958 case TOK_SECTION2:
5959 skip('(');
5960 if (tok != TOK_STR)
5961 expect("section name");
5962 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
5963 next();
5964 skip(')');
5965 break;
5966 case TOK_ALIGNED1:
5967 case TOK_ALIGNED2:
5968 if (tok == '(') {
5969 next();
5970 n = expr_const();
5971 if (n <= 0 || (n & (n - 1)) != 0)
5972 error("alignment must be a positive power of two");
5973 skip(')');
5974 } else {
5975 n = MAX_ALIGN;
5977 ad->aligned = n;
5978 break;
5979 case TOK_UNUSED1:
5980 case TOK_UNUSED2:
5981 /* currently, no need to handle it because tcc does not
5982 track unused objects */
5983 break;
5984 case TOK_NORETURN1:
5985 case TOK_NORETURN2:
5986 /* currently, no need to handle it because tcc does not
5987 track unused objects */
5988 break;
5989 case TOK_CDECL1:
5990 case TOK_CDECL2:
5991 case TOK_CDECL3:
5992 ad->func_call = FUNC_CDECL;
5993 break;
5994 case TOK_STDCALL1:
5995 case TOK_STDCALL2:
5996 case TOK_STDCALL3:
5997 ad->func_call = FUNC_STDCALL;
5998 break;
5999 default:
6000 if (tcc_state->warn_unsupported)
6001 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6002 /* skip parameters */
6003 /* XXX: skip parenthesis too */
6004 if (tok == '(') {
6005 next();
6006 while (tok != ')' && tok != -1)
6007 next();
6008 next();
6010 break;
6012 if (tok != ',')
6013 break;
6014 next();
6016 skip(')');
6017 skip(')');
6021 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6022 static void struct_decl(CType *type, int u)
6024 int a, v, size, align, maxalign, c, offset;
6025 int bit_size, bit_pos, bsize, bt, lbit_pos;
6026 Sym *s, *ss, **ps;
6027 AttributeDef ad;
6028 CType type1, btype;
6030 a = tok; /* save decl type */
6031 next();
6032 if (tok != '{') {
6033 v = tok;
6034 next();
6035 /* struct already defined ? return it */
6036 if (v < TOK_IDENT)
6037 expect("struct/union/enum name");
6038 s = struct_find(v);
6039 if (s) {
6040 if (s->type.t != a)
6041 error("invalid type");
6042 goto do_decl;
6044 } else {
6045 v = anon_sym++;
6047 type1.t = a;
6048 /* we put an undefined size for struct/union */
6049 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6050 s->r = 0; /* default alignment is zero as gcc */
6051 /* put struct/union/enum name in type */
6052 do_decl:
6053 type->t = u;
6054 type->ref = s;
6056 if (tok == '{') {
6057 next();
6058 if (s->c != -1)
6059 error("struct/union/enum already defined");
6060 /* cannot be empty */
6061 c = 0;
6062 /* non empty enums are not allowed */
6063 if (a == TOK_ENUM) {
6064 for(;;) {
6065 v = tok;
6066 if (v < TOK_UIDENT)
6067 expect("identifier");
6068 next();
6069 if (tok == '=') {
6070 next();
6071 c = expr_const();
6073 /* enum symbols have static storage */
6074 ss = sym_push(v, &int_type, VT_CONST, c);
6075 ss->type.t |= VT_STATIC;
6076 if (tok != ',')
6077 break;
6078 next();
6079 c++;
6080 /* NOTE: we accept a trailing comma */
6081 if (tok == '}')
6082 break;
6084 skip('}');
6085 } else {
6086 maxalign = 1;
6087 ps = &s->next;
6088 bit_pos = 0;
6089 offset = 0;
6090 while (tok != '}') {
6091 parse_btype(&btype, &ad);
6092 while (1) {
6093 bit_size = -1;
6094 v = 0;
6095 type1 = btype;
6096 if (tok != ':') {
6097 type_decl(&type1, &ad, &v, TYPE_DIRECT);
6098 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6099 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6100 error("invalid type for '%s'",
6101 get_tok_str(v, NULL));
6103 if (tok == ':') {
6104 next();
6105 bit_size = expr_const();
6106 /* XXX: handle v = 0 case for messages */
6107 if (bit_size < 0)
6108 error("negative width in bit-field '%s'",
6109 get_tok_str(v, NULL));
6110 if (v && bit_size == 0)
6111 error("zero width for bit-field '%s'",
6112 get_tok_str(v, NULL));
6114 size = type_size(&type1, &align);
6115 lbit_pos = 0;
6116 if (bit_size >= 0) {
6117 bt = type1.t & VT_BTYPE;
6118 if (bt != VT_INT &&
6119 bt != VT_BYTE &&
6120 bt != VT_SHORT &&
6121 bt != VT_ENUM)
6122 error("bitfields must have scalar type");
6123 bsize = size * 8;
6124 if (bit_size > bsize) {
6125 error("width of '%s' exceeds its type",
6126 get_tok_str(v, NULL));
6127 } else if (bit_size == bsize) {
6128 /* no need for bit fields */
6129 bit_pos = 0;
6130 } else if (bit_size == 0) {
6131 /* XXX: what to do if only padding in a
6132 structure ? */
6133 /* zero size: means to pad */
6134 if (bit_pos > 0)
6135 bit_pos = bsize;
6136 } else {
6137 /* we do not have enough room ? */
6138 if ((bit_pos + bit_size) > bsize)
6139 bit_pos = 0;
6140 lbit_pos = bit_pos;
6141 /* XXX: handle LSB first */
6142 type1.t |= VT_BITFIELD |
6143 (bit_pos << VT_STRUCT_SHIFT) |
6144 (bit_size << (VT_STRUCT_SHIFT + 6));
6145 bit_pos += bit_size;
6147 } else {
6148 bit_pos = 0;
6150 if (v) {
6151 /* add new memory data only if starting
6152 bit field */
6153 if (lbit_pos == 0) {
6154 if (a == TOK_STRUCT) {
6155 c = (c + align - 1) & -align;
6156 offset = c;
6157 c += size;
6158 } else {
6159 offset = 0;
6160 if (size > c)
6161 c = size;
6163 if (align > maxalign)
6164 maxalign = align;
6166 #if 0
6167 printf("add field %s offset=%d",
6168 get_tok_str(v, NULL), offset);
6169 if (type1.t & VT_BITFIELD) {
6170 printf(" pos=%d size=%d",
6171 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6172 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6174 printf("\n");
6175 #endif
6176 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6177 *ps = ss;
6178 ps = &ss->next;
6180 if (tok == ';' || tok == TOK_EOF)
6181 break;
6182 skip(',');
6184 skip(';');
6186 skip('}');
6187 /* store size and alignment */
6188 s->c = (c + maxalign - 1) & -maxalign;
6189 s->r = maxalign;
6194 /* return 0 if no type declaration. otherwise, return the basic type
6195 and skip it.
6197 static int parse_btype(CType *type, AttributeDef *ad)
6199 int t, u, type_found;
6200 Sym *s;
6201 CType type1;
6203 memset(ad, 0, sizeof(AttributeDef));
6204 type_found = 0;
6205 t = 0;
6206 while(1) {
6207 switch(tok) {
6208 case TOK_EXTENSION:
6209 /* currently, we really ignore extension */
6210 next();
6211 continue;
6213 /* basic types */
6214 case TOK_CHAR:
6215 u = VT_BYTE;
6216 basic_type:
6217 next();
6218 basic_type1:
6219 if ((t & VT_BTYPE) != 0)
6220 error("too many basic types");
6221 t |= u;
6222 break;
6223 case TOK_VOID:
6224 u = VT_VOID;
6225 goto basic_type;
6226 case TOK_SHORT:
6227 u = VT_SHORT;
6228 goto basic_type;
6229 case TOK_INT:
6230 next();
6231 break;
6232 case TOK_LONG:
6233 next();
6234 if ((t & VT_BTYPE) == VT_DOUBLE) {
6235 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6236 } else if ((t & VT_BTYPE) == VT_LONG) {
6237 t = (t & ~VT_BTYPE) | VT_LLONG;
6238 } else {
6239 u = VT_LONG;
6240 goto basic_type1;
6242 break;
6243 case TOK_BOOL:
6244 u = VT_BOOL;
6245 goto basic_type;
6246 case TOK_FLOAT:
6247 u = VT_FLOAT;
6248 goto basic_type;
6249 case TOK_DOUBLE:
6250 next();
6251 if ((t & VT_BTYPE) == VT_LONG) {
6252 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6253 } else {
6254 u = VT_DOUBLE;
6255 goto basic_type1;
6257 break;
6258 case TOK_ENUM:
6259 struct_decl(&type1, VT_ENUM);
6260 basic_type2:
6261 u = type1.t;
6262 type->ref = type1.ref;
6263 goto basic_type1;
6264 case TOK_STRUCT:
6265 case TOK_UNION:
6266 struct_decl(&type1, VT_STRUCT);
6267 goto basic_type2;
6269 /* type modifiers */
6270 case TOK_CONST1:
6271 case TOK_CONST2:
6272 case TOK_CONST3:
6273 t |= VT_CONSTANT;
6274 next();
6275 break;
6276 case TOK_VOLATILE1:
6277 case TOK_VOLATILE2:
6278 case TOK_VOLATILE3:
6279 t |= VT_VOLATILE;
6280 next();
6281 break;
6282 case TOK_REGISTER:
6283 case TOK_SIGNED1:
6284 case TOK_SIGNED2:
6285 case TOK_SIGNED3:
6286 case TOK_AUTO:
6287 case TOK_RESTRICT1:
6288 case TOK_RESTRICT2:
6289 case TOK_RESTRICT3:
6290 next();
6291 break;
6292 case TOK_UNSIGNED:
6293 t |= VT_UNSIGNED;
6294 next();
6295 break;
6297 /* storage */
6298 case TOK_EXTERN:
6299 t |= VT_EXTERN;
6300 next();
6301 break;
6302 case TOK_STATIC:
6303 t |= VT_STATIC;
6304 next();
6305 break;
6306 case TOK_TYPEDEF:
6307 t |= VT_TYPEDEF;
6308 next();
6309 break;
6310 case TOK_INLINE1:
6311 case TOK_INLINE2:
6312 case TOK_INLINE3:
6313 t |= VT_INLINE;
6314 next();
6315 break;
6317 /* GNUC attribute */
6318 case TOK_ATTRIBUTE1:
6319 case TOK_ATTRIBUTE2:
6320 parse_attribute(ad);
6321 break;
6322 /* GNUC typeof */
6323 case TOK_TYPEOF1:
6324 case TOK_TYPEOF2:
6325 case TOK_TYPEOF3:
6326 next();
6327 parse_expr_type(&type1);
6328 goto basic_type2;
6329 default:
6330 s = sym_find(tok);
6331 if (!s || !(s->type.t & VT_TYPEDEF))
6332 goto the_end;
6333 t |= (s->type.t & ~VT_TYPEDEF);
6334 type->ref = s->type.ref;
6335 next();
6336 break;
6338 type_found = 1;
6340 the_end:
6341 /* long is never used as type */
6342 if ((t & VT_BTYPE) == VT_LONG)
6343 t = (t & ~VT_BTYPE) | VT_INT;
6344 type->t = t;
6345 return type_found;
6348 /* convert a function parameter type (array to pointer and function to
6349 function pointer) */
6350 static inline void convert_parameter_type(CType *pt)
6352 /* array must be transformed to pointer according to ANSI C */
6353 pt->t &= ~VT_ARRAY;
6354 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6355 mk_pointer(pt);
6359 static void post_type(CType *type, AttributeDef *ad)
6361 int n, l, t1;
6362 Sym **plast, *s, *first;
6363 AttributeDef ad1;
6364 CType pt;
6366 if (tok == '(') {
6367 /* function declaration */
6368 next();
6369 l = 0;
6370 first = NULL;
6371 plast = &first;
6372 while (tok != ')') {
6373 /* read param name and compute offset */
6374 if (l != FUNC_OLD) {
6375 if (!parse_btype(&pt, &ad1)) {
6376 if (l) {
6377 error("invalid type");
6378 } else {
6379 l = FUNC_OLD;
6380 goto old_proto;
6383 l = FUNC_NEW;
6384 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6385 break;
6386 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6387 if ((pt.t & VT_BTYPE) == VT_VOID)
6388 error("parameter declared as void");
6389 } else {
6390 old_proto:
6391 n = tok;
6392 pt.t = VT_INT;
6393 next();
6395 convert_parameter_type(&pt);
6396 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6397 *plast = s;
6398 plast = &s->next;
6399 if (tok == ',') {
6400 next();
6401 if (l == FUNC_NEW && tok == TOK_DOTS) {
6402 l = FUNC_ELLIPSIS;
6403 next();
6404 break;
6408 /* if no parameters, then old type prototype */
6409 if (l == 0)
6410 l = FUNC_OLD;
6411 skip(')');
6412 t1 = type->t & VT_STORAGE;
6413 /* NOTE: const is ignored in returned type as it has a special
6414 meaning in gcc / C++ */
6415 type->t &= ~(VT_STORAGE | VT_CONSTANT);
6416 post_type(type, ad);
6417 /* we push a anonymous symbol which will contain the function prototype */
6418 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6419 s->next = first;
6420 type->t = t1 | VT_FUNC;
6421 type->ref = s;
6422 } else if (tok == '[') {
6423 /* array definition */
6424 next();
6425 n = -1;
6426 if (tok != ']') {
6427 n = expr_const();
6428 if (n < 0)
6429 error("invalid array size");
6431 skip(']');
6432 /* parse next post type */
6433 t1 = type->t & VT_STORAGE;
6434 type->t &= ~VT_STORAGE;
6435 post_type(type, ad);
6437 /* we push a anonymous symbol which will contain the array
6438 element type */
6439 s = sym_push(SYM_FIELD, type, 0, n);
6440 type->t = t1 | VT_ARRAY | VT_PTR;
6441 type->ref = s;
6445 /* Parse a type declaration (except basic type), and return the type
6446 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6447 expected. 'type' should contain the basic type. 'ad' is the
6448 attribute definition of the basic type. It can be modified by
6449 type_decl().
6451 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6453 Sym *s;
6454 CType type1, *type2;
6455 int qualifiers;
6457 while (tok == '*') {
6458 qualifiers = 0;
6459 redo:
6460 next();
6461 switch(tok) {
6462 case TOK_CONST1:
6463 case TOK_CONST2:
6464 case TOK_CONST3:
6465 qualifiers |= VT_CONSTANT;
6466 goto redo;
6467 case TOK_VOLATILE1:
6468 case TOK_VOLATILE2:
6469 case TOK_VOLATILE3:
6470 qualifiers |= VT_VOLATILE;
6471 goto redo;
6472 case TOK_RESTRICT1:
6473 case TOK_RESTRICT2:
6474 case TOK_RESTRICT3:
6475 goto redo;
6477 mk_pointer(type);
6478 type->t |= qualifiers;
6481 /* XXX: clarify attribute handling */
6482 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6483 parse_attribute(ad);
6485 /* recursive type */
6486 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6487 type1.t = 0; /* XXX: same as int */
6488 if (tok == '(') {
6489 next();
6490 /* XXX: this is not correct to modify 'ad' at this point, but
6491 the syntax is not clear */
6492 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6493 parse_attribute(ad);
6494 type_decl(&type1, ad, v, td);
6495 skip(')');
6496 } else {
6497 /* type identifier */
6498 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6499 *v = tok;
6500 next();
6501 } else {
6502 if (!(td & TYPE_ABSTRACT))
6503 expect("identifier");
6504 *v = 0;
6507 post_type(type, ad);
6508 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6509 parse_attribute(ad);
6510 if (!type1.t)
6511 return;
6512 /* append type at the end of type1 */
6513 type2 = &type1;
6514 for(;;) {
6515 s = type2->ref;
6516 type2 = &s->type;
6517 if (!type2->t) {
6518 *type2 = *type;
6519 break;
6522 *type = type1;
6525 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6526 static int lvalue_type(int t)
6528 int bt, r;
6529 r = VT_LVAL;
6530 bt = t & VT_BTYPE;
6531 if (bt == VT_BYTE || bt == VT_BOOL)
6532 r |= VT_LVAL_BYTE;
6533 else if (bt == VT_SHORT)
6534 r |= VT_LVAL_SHORT;
6535 else
6536 return r;
6537 if (t & VT_UNSIGNED)
6538 r |= VT_LVAL_UNSIGNED;
6539 return r;
6542 /* indirection with full error checking and bound check */
6543 static void indir(void)
6545 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6546 expect("pointer");
6547 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6548 gv(RC_INT);
6549 vtop->type = *pointed_type(&vtop->type);
6550 /* an array is never an lvalue */
6551 if (!(vtop->type.t & VT_ARRAY)) {
6552 vtop->r |= lvalue_type(vtop->type.t);
6553 /* if bound checking, the referenced pointer must be checked */
6554 if (do_bounds_check)
6555 vtop->r |= VT_MUSTBOUND;
6559 /* pass a parameter to a function and do type checking and casting */
6560 static void gfunc_param_typed(Sym *func, Sym *arg)
6562 int func_type;
6563 CType type;
6565 func_type = func->c;
6566 if (func_type == FUNC_OLD ||
6567 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6568 /* default casting : only need to convert float to double */
6569 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6570 type.t = VT_DOUBLE;
6571 gen_cast(&type);
6573 } else if (arg == NULL) {
6574 error("too many arguments to function");
6575 } else {
6576 gen_assign_cast(&arg->type);
6580 /* parse an expression of the form '(type)' or '(expr)' and return its
6581 type */
6582 static void parse_expr_type(CType *type)
6584 int n;
6585 AttributeDef ad;
6587 skip('(');
6588 if (parse_btype(type, &ad)) {
6589 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6590 } else {
6591 expr_type(type);
6593 skip(')');
6596 static void vpush_tokc(int t)
6598 CType type;
6599 type.t = t;
6600 vsetc(&type, VT_CONST, &tokc);
6603 static void unary(void)
6605 int n, t, align, size, r;
6606 CType type;
6607 Sym *s;
6608 AttributeDef ad;
6610 /* XXX: GCC 2.95.3 does not generate a table although it should be
6611 better here */
6612 tok_next:
6613 switch(tok) {
6614 case TOK_EXTENSION:
6615 next();
6616 goto tok_next;
6617 case TOK_CINT:
6618 case TOK_CCHAR:
6619 case TOK_LCHAR:
6620 vpushi(tokc.i);
6621 next();
6622 break;
6623 case TOK_CUINT:
6624 vpush_tokc(VT_INT | VT_UNSIGNED);
6625 next();
6626 break;
6627 case TOK_CLLONG:
6628 vpush_tokc(VT_LLONG);
6629 next();
6630 break;
6631 case TOK_CULLONG:
6632 vpush_tokc(VT_LLONG | VT_UNSIGNED);
6633 next();
6634 break;
6635 case TOK_CFLOAT:
6636 vpush_tokc(VT_FLOAT);
6637 next();
6638 break;
6639 case TOK_CDOUBLE:
6640 vpush_tokc(VT_DOUBLE);
6641 next();
6642 break;
6643 case TOK_CLDOUBLE:
6644 vpush_tokc(VT_LDOUBLE);
6645 next();
6646 break;
6647 case TOK___FUNCTION__:
6648 if (!gnu_ext)
6649 goto tok_identifier;
6650 /* fall thru */
6651 case TOK___FUNC__:
6653 void *ptr;
6654 int len;
6655 /* special function name identifier */
6656 len = strlen(funcname) + 1;
6657 /* generate char[len] type */
6658 type.t = VT_BYTE;
6659 mk_pointer(&type);
6660 type.t |= VT_ARRAY;
6661 type.ref->c = len;
6662 vpush_ref(&type, data_section, data_section->data_offset, len);
6663 ptr = section_ptr_add(data_section, len);
6664 memcpy(ptr, funcname, len);
6665 next();
6667 break;
6668 case TOK_LSTR:
6669 t = VT_INT;
6670 goto str_init;
6671 case TOK_STR:
6672 /* string parsing */
6673 t = VT_BYTE;
6674 str_init:
6675 if (tcc_state->warn_write_strings)
6676 t |= VT_CONSTANT;
6677 type.t = t;
6678 mk_pointer(&type);
6679 type.t |= VT_ARRAY;
6680 memset(&ad, 0, sizeof(AttributeDef));
6681 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
6682 break;
6683 case '(':
6684 next();
6685 /* cast ? */
6686 if (parse_btype(&type, &ad)) {
6687 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
6688 skip(')');
6689 /* check ISOC99 compound literal */
6690 if (tok == '{') {
6691 /* data is allocated locally by default */
6692 if (global_expr)
6693 r = VT_CONST;
6694 else
6695 r = VT_LOCAL;
6696 /* all except arrays are lvalues */
6697 if (!(type.t & VT_ARRAY))
6698 r |= lvalue_type(type.t);
6699 memset(&ad, 0, sizeof(AttributeDef));
6700 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
6701 } else {
6702 unary();
6703 gen_cast(&type);
6705 } else if (tok == '{') {
6706 /* save all registers */
6707 save_regs(0);
6708 /* statement expression : we do not accept break/continue
6709 inside as GCC does */
6710 block(NULL, NULL, NULL, NULL, 0, 1);
6711 skip(')');
6712 } else {
6713 gexpr();
6714 skip(')');
6716 break;
6717 case '*':
6718 next();
6719 unary();
6720 indir();
6721 break;
6722 case '&':
6723 next();
6724 unary();
6725 /* functions names must be treated as function pointers,
6726 except for unary '&' and sizeof. Since we consider that
6727 functions are not lvalues, we only have to handle it
6728 there and in function calls. */
6729 /* arrays can also be used although they are not lvalues */
6730 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
6731 !(vtop->type.t & VT_ARRAY))
6732 test_lvalue();
6733 mk_pointer(&vtop->type);
6734 gaddrof();
6735 break;
6736 case '!':
6737 next();
6738 unary();
6739 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
6740 vtop->c.i = !vtop->c.i;
6741 else if ((vtop->r & VT_VALMASK) == VT_CMP)
6742 vtop->c.i = vtop->c.i ^ 1;
6743 else
6744 vseti(VT_JMP, gtst(1, 0));
6745 break;
6746 case '~':
6747 next();
6748 unary();
6749 vpushi(-1);
6750 gen_op('^');
6751 break;
6752 case '+':
6753 next();
6754 /* in order to force cast, we add zero */
6755 unary();
6756 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
6757 error("pointer not accepted for unary plus");
6758 vpushi(0);
6759 gen_op('+');
6760 break;
6761 case TOK_SIZEOF:
6762 case TOK_ALIGNOF1:
6763 case TOK_ALIGNOF2:
6764 t = tok;
6765 next();
6766 if (tok == '(') {
6767 parse_expr_type(&type);
6768 } else {
6769 unary_type(&type);
6771 size = type_size(&type, &align);
6772 if (t == TOK_SIZEOF) {
6773 if (size < 0)
6774 error("sizeof applied to an incomplete type");
6775 vpushi(size);
6776 } else {
6777 vpushi(align);
6779 break;
6781 case TOK_INC:
6782 case TOK_DEC:
6783 t = tok;
6784 next();
6785 unary();
6786 inc(0, t);
6787 break;
6788 case '-':
6789 next();
6790 vpushi(0);
6791 unary();
6792 gen_op('-');
6793 break;
6794 case TOK_LAND:
6795 if (!gnu_ext)
6796 goto tok_identifier;
6797 next();
6798 /* allow to take the address of a label */
6799 if (tok < TOK_UIDENT)
6800 expect("label identifier");
6801 s = label_find(tok);
6802 if (!s) {
6803 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
6804 } else {
6805 if (s->r == LABEL_DECLARED)
6806 s->r = LABEL_FORWARD;
6808 if (!s->type.t) {
6809 s->type.t = VT_VOID;
6810 mk_pointer(&s->type);
6811 s->type.t |= VT_STATIC;
6813 vset(&s->type, VT_CONST | VT_SYM, 0);
6814 vtop->sym = s;
6815 next();
6816 break;
6817 default:
6818 tok_identifier:
6819 t = tok;
6820 next();
6821 if (t < TOK_UIDENT)
6822 expect("identifier");
6823 s = sym_find(t);
6824 if (!s) {
6825 if (tok != '(')
6826 error("'%s' undeclared", get_tok_str(t, NULL));
6827 /* for simple function calls, we tolerate undeclared
6828 external reference to int() function */
6829 s = external_global_sym(t, &func_old_type, 0);
6831 vset(&s->type, s->r, s->c);
6832 /* if forward reference, we must point to s */
6833 if (vtop->r & VT_SYM) {
6834 vtop->sym = s;
6835 vtop->c.ul = 0;
6837 break;
6840 /* post operations */
6841 while (1) {
6842 if (tok == TOK_INC || tok == TOK_DEC) {
6843 inc(1, tok);
6844 next();
6845 } else if (tok == '.' || tok == TOK_ARROW) {
6846 /* field */
6847 if (tok == TOK_ARROW)
6848 indir();
6849 test_lvalue();
6850 gaddrof();
6851 next();
6852 /* expect pointer on structure */
6853 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
6854 expect("struct or union");
6855 s = vtop->type.ref;
6856 /* find field */
6857 tok |= SYM_FIELD;
6858 while ((s = s->next) != NULL) {
6859 if (s->v == tok)
6860 break;
6862 if (!s)
6863 error("field not found");
6864 /* add field offset to pointer */
6865 vtop->type = char_pointer_type; /* change type to 'char *' */
6866 vpushi(s->c);
6867 gen_op('+');
6868 /* change type to field type, and set to lvalue */
6869 vtop->type = s->type;
6870 /* an array is never an lvalue */
6871 if (!(vtop->type.t & VT_ARRAY)) {
6872 vtop->r |= lvalue_type(vtop->type.t);
6873 /* if bound checking, the referenced pointer must be checked */
6874 if (do_bounds_check)
6875 vtop->r |= VT_MUSTBOUND;
6877 next();
6878 } else if (tok == '[') {
6879 next();
6880 gexpr();
6881 gen_op('+');
6882 indir();
6883 skip(']');
6884 } else if (tok == '(') {
6885 SValue ret;
6886 Sym *sa;
6887 int nb_args;
6889 /* function call */
6890 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
6891 /* pointer test (no array accepted) */
6892 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
6893 vtop->type = *pointed_type(&vtop->type);
6894 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
6895 goto error_func;
6896 } else {
6897 error_func:
6898 expect("function pointer");
6900 } else {
6901 vtop->r &= ~VT_LVAL; /* no lvalue */
6903 /* get return type */
6904 s = vtop->type.ref;
6905 next();
6906 sa = s->next; /* first parameter */
6907 nb_args = 0;
6908 /* compute first implicit argument if a structure is returned */
6909 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
6910 /* get some space for the returned structure */
6911 size = type_size(&s->type, &align);
6912 loc = (loc - size) & -align;
6913 ret.type = s->type;
6914 ret.r = VT_LOCAL | VT_LVAL;
6915 /* pass it as 'int' to avoid structure arg passing
6916 problems */
6917 vseti(VT_LOCAL, loc);
6918 ret.c = vtop->c;
6919 nb_args++;
6920 } else {
6921 ret.type = s->type;
6922 ret.r2 = VT_CONST;
6923 /* return in register */
6924 if (is_float(ret.type.t)) {
6925 ret.r = REG_FRET;
6926 } else {
6927 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
6928 ret.r2 = REG_LRET;
6929 ret.r = REG_IRET;
6931 ret.c.i = 0;
6933 if (tok != ')') {
6934 for(;;) {
6935 expr_eq();
6936 gfunc_param_typed(s, sa);
6937 nb_args++;
6938 if (sa)
6939 sa = sa->next;
6940 if (tok == ')')
6941 break;
6942 skip(',');
6945 if (sa)
6946 error("too few arguments to function");
6947 skip(')');
6948 if (!nocode_wanted) {
6949 gfunc_call(nb_args);
6950 } else {
6951 vtop -= (nb_args + 1);
6953 /* return value */
6954 vsetc(&ret.type, ret.r, &ret.c);
6955 vtop->r2 = ret.r2;
6956 } else {
6957 break;
6962 static void uneq(void)
6964 int t;
6966 unary();
6967 if (tok == '=' ||
6968 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
6969 tok == TOK_A_XOR || tok == TOK_A_OR ||
6970 tok == TOK_A_SHL || tok == TOK_A_SAR) {
6971 test_lvalue();
6972 t = tok;
6973 next();
6974 if (t == '=') {
6975 expr_eq();
6976 } else {
6977 vdup();
6978 expr_eq();
6979 gen_op(t & 0x7f);
6981 vstore();
6985 static void expr_prod(void)
6987 int t;
6989 uneq();
6990 while (tok == '*' || tok == '/' || tok == '%') {
6991 t = tok;
6992 next();
6993 uneq();
6994 gen_op(t);
6998 static void expr_sum(void)
7000 int t;
7002 expr_prod();
7003 while (tok == '+' || tok == '-') {
7004 t = tok;
7005 next();
7006 expr_prod();
7007 gen_op(t);
7011 static void expr_shift(void)
7013 int t;
7015 expr_sum();
7016 while (tok == TOK_SHL || tok == TOK_SAR) {
7017 t = tok;
7018 next();
7019 expr_sum();
7020 gen_op(t);
7024 static void expr_cmp(void)
7026 int t;
7028 expr_shift();
7029 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7030 tok == TOK_ULT || tok == TOK_UGE) {
7031 t = tok;
7032 next();
7033 expr_shift();
7034 gen_op(t);
7038 static void expr_cmpeq(void)
7040 int t;
7042 expr_cmp();
7043 while (tok == TOK_EQ || tok == TOK_NE) {
7044 t = tok;
7045 next();
7046 expr_cmp();
7047 gen_op(t);
7051 static void expr_and(void)
7053 expr_cmpeq();
7054 while (tok == '&') {
7055 next();
7056 expr_cmpeq();
7057 gen_op('&');
7061 static void expr_xor(void)
7063 expr_and();
7064 while (tok == '^') {
7065 next();
7066 expr_and();
7067 gen_op('^');
7071 static void expr_or(void)
7073 expr_xor();
7074 while (tok == '|') {
7075 next();
7076 expr_xor();
7077 gen_op('|');
7081 /* XXX: fix this mess */
7082 static void expr_land_const(void)
7084 expr_or();
7085 while (tok == TOK_LAND) {
7086 next();
7087 expr_or();
7088 gen_op(TOK_LAND);
7092 /* XXX: fix this mess */
7093 static void expr_lor_const(void)
7095 expr_land_const();
7096 while (tok == TOK_LOR) {
7097 next();
7098 expr_land_const();
7099 gen_op(TOK_LOR);
7103 /* only used if non constant */
7104 static void expr_land(void)
7106 int t;
7108 expr_or();
7109 if (tok == TOK_LAND) {
7110 t = 0;
7111 for(;;) {
7112 t = gtst(1, t);
7113 if (tok != TOK_LAND) {
7114 vseti(VT_JMPI, t);
7115 break;
7117 next();
7118 expr_or();
7123 static void expr_lor(void)
7125 int t;
7127 expr_land();
7128 if (tok == TOK_LOR) {
7129 t = 0;
7130 for(;;) {
7131 t = gtst(0, t);
7132 if (tok != TOK_LOR) {
7133 vseti(VT_JMP, t);
7134 break;
7136 next();
7137 expr_land();
7142 /* XXX: better constant handling */
7143 static void expr_eq(void)
7145 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7146 SValue sv;
7147 CType type, type1, type2;
7149 if (const_wanted) {
7150 int c1, c;
7151 expr_lor_const();
7152 if (tok == '?') {
7153 c = vtop->c.i;
7154 vpop();
7155 next();
7156 if (tok == ':' && gnu_ext) {
7157 c1 = c;
7158 } else {
7159 gexpr();
7160 c1 = vtop->c.i;
7161 vpop();
7163 skip(':');
7164 expr_eq();
7165 if (c)
7166 vtop->c.i = c1;
7168 } else {
7169 expr_lor();
7170 if (tok == '?') {
7171 next();
7172 if (vtop != vstack) {
7173 /* needed to avoid having different registers saved in
7174 each branch */
7175 if (is_float(vtop->type.t))
7176 rc = RC_FLOAT;
7177 else
7178 rc = RC_INT;
7179 gv(rc);
7180 save_regs(1);
7182 if (tok == ':' && gnu_ext) {
7183 gv_dup();
7184 tt = gtst(1, 0);
7185 } else {
7186 tt = gtst(1, 0);
7187 gexpr();
7189 type1 = vtop->type;
7190 sv = *vtop; /* save value to handle it later */
7191 vtop--; /* no vpop so that FP stack is not flushed */
7192 skip(':');
7193 u = gjmp(0);
7194 gsym(tt);
7195 expr_eq();
7196 type2 = vtop->type;
7198 t1 = type1.t;
7199 bt1 = t1 & VT_BTYPE;
7200 t2 = type2.t;
7201 bt2 = t2 & VT_BTYPE;
7202 /* cast operands to correct type according to ISOC rules */
7203 if (is_float(bt1) || is_float(bt2)) {
7204 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7205 type.t = VT_LDOUBLE;
7206 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7207 type.t = VT_DOUBLE;
7208 } else {
7209 type.t = VT_FLOAT;
7211 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7212 /* cast to biggest op */
7213 type.t = VT_LLONG;
7214 /* convert to unsigned if it does not fit in a long long */
7215 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7216 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7217 type.t |= VT_UNSIGNED;
7218 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7219 /* XXX: test pointer compatibility */
7220 type = type1;
7221 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7222 /* XXX: test structure compatibility */
7223 type = type1;
7224 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7225 /* NOTE: as an extension, we accept void on only one side */
7226 type.t = VT_VOID;
7227 } else {
7228 /* integer operations */
7229 type.t = VT_INT;
7230 /* convert to unsigned if it does not fit in an integer */
7231 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7232 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7233 type.t |= VT_UNSIGNED;
7236 /* now we convert second operand */
7237 gen_cast(&type);
7238 rc = RC_INT;
7239 if (is_float(type.t)) {
7240 rc = RC_FLOAT;
7241 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7242 /* for long longs, we use fixed registers to avoid having
7243 to handle a complicated move */
7244 rc = RC_IRET;
7247 r2 = gv(rc);
7248 /* this is horrible, but we must also convert first
7249 operand */
7250 tt = gjmp(0);
7251 gsym(u);
7252 /* put again first value and cast it */
7253 *vtop = sv;
7254 gen_cast(&type);
7255 r1 = gv(rc);
7256 move_reg(r2, r1);
7257 vtop->r = r2;
7258 gsym(tt);
7263 static void gexpr(void)
7265 while (1) {
7266 expr_eq();
7267 if (tok != ',')
7268 break;
7269 vpop();
7270 next();
7274 /* parse an expression and return its type without any side effect. */
7275 static void expr_type(CType *type)
7277 int a;
7279 a = nocode_wanted;
7280 nocode_wanted = 1;
7281 gexpr();
7282 *type = vtop->type;
7283 vpop();
7284 nocode_wanted = a;
7287 /* parse a unary expression and return its type without any side
7288 effect. */
7289 static void unary_type(CType *type)
7291 int a;
7293 a = nocode_wanted;
7294 nocode_wanted = 1;
7295 unary();
7296 *type = vtop->type;
7297 vpop();
7298 nocode_wanted = a;
7301 /* parse a constant expression and return value in vtop. */
7302 static void expr_const1(void)
7304 int a;
7305 a = const_wanted;
7306 const_wanted = 1;
7307 expr_eq();
7308 const_wanted = a;
7311 /* parse an integer constant and return its value. */
7312 static int expr_const(void)
7314 int c;
7315 expr_const1();
7316 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7317 expect("constant expression");
7318 c = vtop->c.i;
7319 vpop();
7320 return c;
7323 /* return the label token if current token is a label, otherwise
7324 return zero */
7325 static int is_label(void)
7327 int last_tok;
7329 /* fast test first */
7330 if (tok < TOK_UIDENT)
7331 return 0;
7332 /* no need to save tokc because tok is an identifier */
7333 last_tok = tok;
7334 next();
7335 if (tok == ':') {
7336 next();
7337 return last_tok;
7338 } else {
7339 unget_tok(last_tok);
7340 return 0;
7344 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7345 int case_reg, int is_expr)
7347 int a, b, c, d;
7348 Sym *s;
7350 /* generate line number info */
7351 if (do_debug &&
7352 (last_line_num != file->line_num || last_ind != ind)) {
7353 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7354 last_ind = ind;
7355 last_line_num = file->line_num;
7358 if (is_expr) {
7359 /* default return value is (void) */
7360 vpushi(0);
7361 vtop->type.t = VT_VOID;
7364 if (tok == TOK_IF) {
7365 /* if test */
7366 next();
7367 skip('(');
7368 gexpr();
7369 skip(')');
7370 a = gtst(1, 0);
7371 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7372 c = tok;
7373 if (c == TOK_ELSE) {
7374 next();
7375 d = gjmp(0);
7376 gsym(a);
7377 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7378 gsym(d); /* patch else jmp */
7379 } else
7380 gsym(a);
7381 } else if (tok == TOK_WHILE) {
7382 next();
7383 d = ind;
7384 skip('(');
7385 gexpr();
7386 skip(')');
7387 a = gtst(1, 0);
7388 b = 0;
7389 block(&a, &b, case_sym, def_sym, case_reg, 0);
7390 gjmp_addr(d);
7391 gsym(a);
7392 gsym_addr(b, d);
7393 } else if (tok == '{') {
7394 Sym *llabel;
7396 next();
7397 /* record local declaration stack position */
7398 s = local_stack;
7399 llabel = local_label_stack;
7400 /* handle local labels declarations */
7401 if (tok == TOK_LABEL) {
7402 next();
7403 for(;;) {
7404 if (tok < TOK_UIDENT)
7405 expect("label identifier");
7406 label_push(&local_label_stack, tok, LABEL_DECLARED);
7407 next();
7408 if (tok == ',') {
7409 next();
7410 } else {
7411 skip(';');
7412 break;
7416 while (tok != '}') {
7417 decl(VT_LOCAL);
7418 if (tok != '}') {
7419 if (is_expr)
7420 vpop();
7421 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7424 /* pop locally defined labels */
7425 label_pop(&local_label_stack, llabel);
7426 /* pop locally defined symbols */
7427 sym_pop(&local_stack, s);
7428 next();
7429 } else if (tok == TOK_RETURN) {
7430 next();
7431 if (tok != ';') {
7432 gexpr();
7433 gen_assign_cast(&func_vt);
7434 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7435 CType type;
7436 /* if returning structure, must copy it to implicit
7437 first pointer arg location */
7438 type = func_vt;
7439 mk_pointer(&type);
7440 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7441 indir();
7442 vswap();
7443 /* copy structure value to pointer */
7444 vstore();
7445 } else if (is_float(func_vt.t)) {
7446 gv(RC_FRET);
7447 } else {
7448 gv(RC_IRET);
7450 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7452 skip(';');
7453 rsym = gjmp(rsym); /* jmp */
7454 } else if (tok == TOK_BREAK) {
7455 /* compute jump */
7456 if (!bsym)
7457 error("cannot break");
7458 *bsym = gjmp(*bsym);
7459 next();
7460 skip(';');
7461 } else if (tok == TOK_CONTINUE) {
7462 /* compute jump */
7463 if (!csym)
7464 error("cannot continue");
7465 *csym = gjmp(*csym);
7466 next();
7467 skip(';');
7468 } else if (tok == TOK_FOR) {
7469 int e;
7470 next();
7471 skip('(');
7472 if (tok != ';') {
7473 gexpr();
7474 vpop();
7476 skip(';');
7477 d = ind;
7478 c = ind;
7479 a = 0;
7480 b = 0;
7481 if (tok != ';') {
7482 gexpr();
7483 a = gtst(1, 0);
7485 skip(';');
7486 if (tok != ')') {
7487 e = gjmp(0);
7488 c = ind;
7489 gexpr();
7490 vpop();
7491 gjmp_addr(d);
7492 gsym(e);
7494 skip(')');
7495 block(&a, &b, case_sym, def_sym, case_reg, 0);
7496 gjmp_addr(c);
7497 gsym(a);
7498 gsym_addr(b, c);
7499 } else
7500 if (tok == TOK_DO) {
7501 next();
7502 a = 0;
7503 b = 0;
7504 d = ind;
7505 block(&a, &b, case_sym, def_sym, case_reg, 0);
7506 skip(TOK_WHILE);
7507 skip('(');
7508 gsym(b);
7509 gexpr();
7510 c = gtst(0, 0);
7511 gsym_addr(c, d);
7512 skip(')');
7513 gsym(a);
7514 skip(';');
7515 } else
7516 if (tok == TOK_SWITCH) {
7517 next();
7518 skip('(');
7519 gexpr();
7520 /* XXX: other types than integer */
7521 case_reg = gv(RC_INT);
7522 vpop();
7523 skip(')');
7524 a = 0;
7525 b = gjmp(0); /* jump to first case */
7526 c = 0;
7527 block(&a, csym, &b, &c, case_reg, 0);
7528 /* if no default, jmp after switch */
7529 if (c == 0)
7530 c = ind;
7531 /* default label */
7532 gsym_addr(b, c);
7533 /* break label */
7534 gsym(a);
7535 } else
7536 if (tok == TOK_CASE) {
7537 int v1, v2;
7538 if (!case_sym)
7539 expect("switch");
7540 next();
7541 v1 = expr_const();
7542 v2 = v1;
7543 if (gnu_ext && tok == TOK_DOTS) {
7544 next();
7545 v2 = expr_const();
7546 if (v2 < v1)
7547 warning("empty case range");
7549 /* since a case is like a label, we must skip it with a jmp */
7550 b = gjmp(0);
7551 gsym(*case_sym);
7552 vseti(case_reg, 0);
7553 vpushi(v1);
7554 if (v1 == v2) {
7555 gen_op(TOK_EQ);
7556 *case_sym = gtst(1, 0);
7557 } else {
7558 gen_op(TOK_GE);
7559 *case_sym = gtst(1, 0);
7560 vseti(case_reg, 0);
7561 vpushi(v2);
7562 gen_op(TOK_LE);
7563 *case_sym = gtst(1, *case_sym);
7565 gsym(b);
7566 skip(':');
7567 is_expr = 0;
7568 goto block_after_label;
7569 } else
7570 if (tok == TOK_DEFAULT) {
7571 next();
7572 skip(':');
7573 if (!def_sym)
7574 expect("switch");
7575 if (*def_sym)
7576 error("too many 'default'");
7577 *def_sym = ind;
7578 is_expr = 0;
7579 goto block_after_label;
7580 } else
7581 if (tok == TOK_GOTO) {
7582 next();
7583 if (tok == '*' && gnu_ext) {
7584 /* computed goto */
7585 next();
7586 gexpr();
7587 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7588 expect("pointer");
7589 ggoto();
7590 } else if (tok >= TOK_UIDENT) {
7591 s = label_find(tok);
7592 /* put forward definition if needed */
7593 if (!s) {
7594 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7595 } else {
7596 if (s->r == LABEL_DECLARED)
7597 s->r = LABEL_FORWARD;
7599 /* label already defined */
7600 if (s->r & LABEL_FORWARD)
7601 s->next = (void *)gjmp((long)s->next);
7602 else
7603 gjmp_addr((long)s->next);
7604 next();
7605 } else {
7606 expect("label identifier");
7608 skip(';');
7609 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7610 asm_instr();
7611 } else {
7612 b = is_label();
7613 if (b) {
7614 /* label case */
7615 s = label_find(b);
7616 if (s) {
7617 if (s->r == LABEL_DEFINED)
7618 error("duplicate label '%s'", get_tok_str(s->v, NULL));
7619 gsym((long)s->next);
7620 s->r = LABEL_DEFINED;
7621 } else {
7622 s = label_push(&global_label_stack, b, LABEL_DEFINED);
7624 s->next = (void *)ind;
7625 /* we accept this, but it is a mistake */
7626 block_after_label:
7627 if (tok == '}') {
7628 warning("deprecated use of label at end of compound statement");
7629 } else {
7630 if (is_expr)
7631 vpop();
7632 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7634 } else {
7635 /* expression case */
7636 if (tok != ';') {
7637 if (is_expr) {
7638 vpop();
7639 gexpr();
7640 } else {
7641 gexpr();
7642 vpop();
7645 skip(';');
7650 /* t is the array or struct type. c is the array or struct
7651 address. cur_index/cur_field is the pointer to the current
7652 value. 'size_only' is true if only size info is needed (only used
7653 in arrays) */
7654 static void decl_designator(CType *type, Section *sec, unsigned long c,
7655 int *cur_index, Sym **cur_field,
7656 int size_only)
7658 Sym *s, *f;
7659 int notfirst, index, index_last, align, l, nb_elems, elem_size;
7660 CType type1;
7662 notfirst = 0;
7663 elem_size = 0;
7664 nb_elems = 1;
7665 if (gnu_ext && (l = is_label()) != 0)
7666 goto struct_field;
7667 while (tok == '[' || tok == '.') {
7668 if (tok == '[') {
7669 if (!(type->t & VT_ARRAY))
7670 expect("array type");
7671 s = type->ref;
7672 next();
7673 index = expr_const();
7674 if (index < 0 || (s->c >= 0 && index >= s->c))
7675 expect("invalid index");
7676 if (tok == TOK_DOTS && gnu_ext) {
7677 next();
7678 index_last = expr_const();
7679 if (index_last < 0 ||
7680 (s->c >= 0 && index_last >= s->c) ||
7681 index_last < index)
7682 expect("invalid index");
7683 } else {
7684 index_last = index;
7686 skip(']');
7687 if (!notfirst)
7688 *cur_index = index_last;
7689 type = pointed_type(type);
7690 elem_size = type_size(type, &align);
7691 c += index * elem_size;
7692 /* NOTE: we only support ranges for last designator */
7693 nb_elems = index_last - index + 1;
7694 if (nb_elems != 1) {
7695 notfirst = 1;
7696 break;
7698 } else {
7699 next();
7700 l = tok;
7701 next();
7702 struct_field:
7703 if ((type->t & VT_BTYPE) != VT_STRUCT)
7704 expect("struct/union type");
7705 s = type->ref;
7706 l |= SYM_FIELD;
7707 f = s->next;
7708 while (f) {
7709 if (f->v == l)
7710 break;
7711 f = f->next;
7713 if (!f)
7714 expect("field");
7715 if (!notfirst)
7716 *cur_field = f;
7717 /* XXX: fix this mess by using explicit storage field */
7718 type1 = f->type;
7719 type1.t |= (type->t & ~VT_TYPE);
7720 type = &type1;
7721 c += f->c;
7723 notfirst = 1;
7725 if (notfirst) {
7726 if (tok == '=') {
7727 next();
7728 } else {
7729 if (!gnu_ext)
7730 expect("=");
7732 } else {
7733 if (type->t & VT_ARRAY) {
7734 index = *cur_index;
7735 type = pointed_type(type);
7736 c += index * type_size(type, &align);
7737 } else {
7738 f = *cur_field;
7739 if (!f)
7740 error("too many field init");
7741 /* XXX: fix this mess by using explicit storage field */
7742 type1 = f->type;
7743 type1.t |= (type->t & ~VT_TYPE);
7744 type = &type1;
7745 c += f->c;
7748 decl_initializer(type, sec, c, 0, size_only);
7750 /* XXX: make it more general */
7751 if (!size_only && nb_elems > 1) {
7752 unsigned long c_end;
7753 uint8_t *src, *dst;
7754 int i;
7756 if (!sec)
7757 error("range init not supported yet for dynamic storage");
7758 c_end = c + nb_elems * elem_size;
7759 if (c_end > sec->data_allocated)
7760 section_realloc(sec, c_end);
7761 src = sec->data + c;
7762 dst = src;
7763 for(i = 1; i < nb_elems; i++) {
7764 dst += elem_size;
7765 memcpy(dst, src, elem_size);
7770 #define EXPR_VAL 0
7771 #define EXPR_CONST 1
7772 #define EXPR_ANY 2
7774 /* store a value or an expression directly in global data or in local array */
7775 static void init_putv(CType *type, Section *sec, unsigned long c,
7776 int v, int expr_type)
7778 int saved_global_expr, bt, bit_pos, bit_size;
7779 void *ptr;
7780 unsigned long long bit_mask;
7781 CType dtype;
7783 switch(expr_type) {
7784 case EXPR_VAL:
7785 vpushi(v);
7786 break;
7787 case EXPR_CONST:
7788 /* compound literals must be allocated globally in this case */
7789 saved_global_expr = global_expr;
7790 global_expr = 1;
7791 expr_const1();
7792 global_expr = saved_global_expr;
7793 /* NOTE: symbols are accepted */
7794 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
7795 error("initializer element is not constant");
7796 break;
7797 case EXPR_ANY:
7798 expr_eq();
7799 break;
7802 dtype = *type;
7803 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7805 if (sec) {
7806 /* XXX: not portable */
7807 /* XXX: generate error if incorrect relocation */
7808 gen_assign_cast(&dtype);
7809 bt = type->t & VT_BTYPE;
7810 ptr = sec->data + c;
7811 /* XXX: make code faster ? */
7812 if (!(type->t & VT_BITFIELD)) {
7813 bit_pos = 0;
7814 bit_size = 32;
7815 bit_mask = -1LL;
7816 } else {
7817 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
7818 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
7819 bit_mask = (1LL << bit_size) - 1;
7821 if ((vtop->r & VT_SYM) &&
7822 (bt == VT_BYTE ||
7823 bt == VT_SHORT ||
7824 bt == VT_DOUBLE ||
7825 bt == VT_LDOUBLE ||
7826 bt == VT_LLONG ||
7827 (bt == VT_INT && bit_size != 32)))
7828 error("initializer element is not computable at load time");
7829 switch(bt) {
7830 case VT_BYTE:
7831 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7832 break;
7833 case VT_SHORT:
7834 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7835 break;
7836 case VT_DOUBLE:
7837 *(double *)ptr = vtop->c.d;
7838 break;
7839 case VT_LDOUBLE:
7840 *(long double *)ptr = vtop->c.ld;
7841 break;
7842 case VT_LLONG:
7843 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
7844 break;
7845 default:
7846 if (vtop->r & VT_SYM) {
7847 greloc(sec, vtop->sym, c, R_DATA_32);
7849 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7850 break;
7852 vtop--;
7853 } else {
7854 vset(&dtype, VT_LOCAL, c);
7855 vswap();
7856 vstore();
7857 vpop();
7861 /* put zeros for variable based init */
7862 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
7864 if (sec) {
7865 /* nothing to do because globals are already set to zero */
7866 } else {
7867 vpush_global_sym(&func_old_type, TOK_memset);
7868 vseti(VT_LOCAL, c);
7869 vpushi(0);
7870 vpushi(size);
7871 gfunc_call(3);
7875 /* 't' contains the type and storage info. 'c' is the offset of the
7876 object in section 'sec'. If 'sec' is NULL, it means stack based
7877 allocation. 'first' is true if array '{' must be read (multi
7878 dimension implicit array init handling). 'size_only' is true if
7879 size only evaluation is wanted (only for arrays). */
7880 static void decl_initializer(CType *type, Section *sec, unsigned long c,
7881 int first, int size_only)
7883 int index, array_length, n, no_oblock, nb, parlevel, i;
7884 int size1, align1, expr_type;
7885 Sym *s, *f;
7886 CType *t1;
7888 if (type->t & VT_ARRAY) {
7889 s = type->ref;
7890 n = s->c;
7891 array_length = 0;
7892 t1 = pointed_type(type);
7893 size1 = type_size(t1, &align1);
7895 no_oblock = 1;
7896 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
7897 tok == '{') {
7898 skip('{');
7899 no_oblock = 0;
7902 /* only parse strings here if correct type (otherwise: handle
7903 them as ((w)char *) expressions */
7904 if ((tok == TOK_LSTR &&
7905 (t1->t & VT_BTYPE) == VT_INT) ||
7906 (tok == TOK_STR &&
7907 (t1->t & VT_BTYPE) == VT_BYTE)) {
7908 while (tok == TOK_STR || tok == TOK_LSTR) {
7909 int cstr_len, ch;
7910 CString *cstr;
7912 cstr = tokc.cstr;
7913 /* compute maximum number of chars wanted */
7914 if (tok == TOK_STR)
7915 cstr_len = cstr->size;
7916 else
7917 cstr_len = cstr->size / sizeof(int);
7918 cstr_len--;
7919 nb = cstr_len;
7920 if (n >= 0 && nb > (n - array_length))
7921 nb = n - array_length;
7922 if (!size_only) {
7923 if (cstr_len > nb)
7924 warning("initializer-string for array is too long");
7925 /* in order to go faster for common case (char
7926 string in global variable, we handle it
7927 specifically */
7928 if (sec && tok == TOK_STR && size1 == 1) {
7929 memcpy(sec->data + c + array_length, cstr->data, nb);
7930 } else {
7931 for(i=0;i<nb;i++) {
7932 if (tok == TOK_STR)
7933 ch = ((unsigned char *)cstr->data)[i];
7934 else
7935 ch = ((int *)cstr->data)[i];
7936 init_putv(t1, sec, c + (array_length + i) * size1,
7937 ch, EXPR_VAL);
7941 array_length += nb;
7942 next();
7944 /* only add trailing zero if enough storage (no
7945 warning in this case since it is standard) */
7946 if (n < 0 || array_length < n) {
7947 if (!size_only) {
7948 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
7950 array_length++;
7952 } else {
7953 index = 0;
7954 while (tok != '}') {
7955 decl_designator(type, sec, c, &index, NULL, size_only);
7956 if (n >= 0 && index >= n)
7957 error("index too large");
7958 /* must put zero in holes (note that doing it that way
7959 ensures that it even works with designators) */
7960 if (!size_only && array_length < index) {
7961 init_putz(t1, sec, c + array_length * size1,
7962 (index - array_length) * size1);
7964 index++;
7965 if (index > array_length)
7966 array_length = index;
7967 /* special test for multi dimensional arrays (may not
7968 be strictly correct if designators are used at the
7969 same time) */
7970 if (index >= n && no_oblock)
7971 break;
7972 if (tok == '}')
7973 break;
7974 skip(',');
7977 if (!no_oblock)
7978 skip('}');
7979 /* put zeros at the end */
7980 if (!size_only && n >= 0 && array_length < n) {
7981 init_putz(t1, sec, c + array_length * size1,
7982 (n - array_length) * size1);
7984 /* patch type size if needed */
7985 if (n < 0)
7986 s->c = array_length;
7987 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
7988 (sec || !first || tok == '{')) {
7989 int par_count;
7991 /* NOTE: the previous test is a specific case for automatic
7992 struct/union init */
7993 /* XXX: union needs only one init */
7995 /* XXX: this test is incorrect for local initializers
7996 beginning with ( without {. It would be much more difficult
7997 to do it correctly (ideally, the expression parser should
7998 be used in all cases) */
7999 par_count = 0;
8000 if (tok == '(') {
8001 AttributeDef ad1;
8002 CType type1;
8003 next();
8004 while (tok == '(') {
8005 par_count++;
8006 next();
8008 if (!parse_btype(&type1, &ad1))
8009 expect("cast");
8010 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8011 #if 0
8012 if (!is_assignable_types(type, &type1))
8013 error("invalid type for cast");
8014 #endif
8015 skip(')');
8017 no_oblock = 1;
8018 if (first || tok == '{') {
8019 skip('{');
8020 no_oblock = 0;
8022 s = type->ref;
8023 f = s->next;
8024 array_length = 0;
8025 index = 0;
8026 n = s->c;
8027 while (tok != '}') {
8028 decl_designator(type, sec, c, NULL, &f, size_only);
8029 index = f->c;
8030 if (!size_only && array_length < index) {
8031 init_putz(type, sec, c + array_length,
8032 index - array_length);
8034 index = index + type_size(&f->type, &align1);
8035 if (index > array_length)
8036 array_length = index;
8037 f = f->next;
8038 if (no_oblock && f == NULL)
8039 break;
8040 if (tok == '}')
8041 break;
8042 skip(',');
8044 /* put zeros at the end */
8045 if (!size_only && array_length < n) {
8046 init_putz(type, sec, c + array_length,
8047 n - array_length);
8049 if (!no_oblock)
8050 skip('}');
8051 while (par_count) {
8052 skip(')');
8053 par_count--;
8055 } else if (tok == '{') {
8056 next();
8057 decl_initializer(type, sec, c, first, size_only);
8058 skip('}');
8059 } else if (size_only) {
8060 /* just skip expression */
8061 parlevel = 0;
8062 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8063 tok != -1) {
8064 if (tok == '(')
8065 parlevel++;
8066 else if (tok == ')')
8067 parlevel--;
8068 next();
8070 } else {
8071 /* currently, we always use constant expression for globals
8072 (may change for scripting case) */
8073 expr_type = EXPR_CONST;
8074 if (!sec)
8075 expr_type = EXPR_ANY;
8076 init_putv(type, sec, c, 0, expr_type);
8080 /* parse an initializer for type 't' if 'has_init' is non zero, and
8081 allocate space in local or global data space ('r' is either
8082 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8083 variable 'v' of scope 'scope' is declared before initializers are
8084 parsed. If 'v' is zero, then a reference to the new object is put
8085 in the value stack. If 'has_init' is 2, a special parsing is done
8086 to handle string constants. */
8087 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8088 int has_init, int v, int scope)
8090 int size, align, addr, data_offset;
8091 int level;
8092 ParseState saved_parse_state;
8093 TokenString init_str;
8094 Section *sec;
8096 size = type_size(type, &align);
8097 /* If unknown size, we must evaluate it before
8098 evaluating initializers because
8099 initializers can generate global data too
8100 (e.g. string pointers or ISOC99 compound
8101 literals). It also simplifies local
8102 initializers handling */
8103 tok_str_new(&init_str);
8104 if (size < 0) {
8105 if (!has_init)
8106 error("unknown type size");
8107 /* get all init string */
8108 if (has_init == 2) {
8109 /* only get strings */
8110 while (tok == TOK_STR || tok == TOK_LSTR) {
8111 tok_str_add_tok(&init_str);
8112 next();
8114 } else {
8115 level = 0;
8116 while (level > 0 || (tok != ',' && tok != ';')) {
8117 if (tok < 0)
8118 error("unexpected end of file in initializer");
8119 tok_str_add_tok(&init_str);
8120 if (tok == '{')
8121 level++;
8122 else if (tok == '}') {
8123 if (level == 0)
8124 break;
8125 level--;
8127 next();
8130 tok_str_add(&init_str, -1);
8131 tok_str_add(&init_str, 0);
8133 /* compute size */
8134 save_parse_state(&saved_parse_state);
8136 macro_ptr = init_str.str;
8137 next();
8138 decl_initializer(type, NULL, 0, 1, 1);
8139 /* prepare second initializer parsing */
8140 macro_ptr = init_str.str;
8141 next();
8143 /* if still unknown size, error */
8144 size = type_size(type, &align);
8145 if (size < 0)
8146 error("unknown type size");
8148 /* take into account specified alignment if bigger */
8149 if (ad->aligned > align)
8150 align = ad->aligned;
8151 if ((r & VT_VALMASK) == VT_LOCAL) {
8152 sec = NULL;
8153 if (do_bounds_check && (type->t & VT_ARRAY))
8154 loc--;
8155 loc = (loc - size) & -align;
8156 addr = loc;
8157 /* handles bounds */
8158 /* XXX: currently, since we do only one pass, we cannot track
8159 '&' operators, so we add only arrays */
8160 if (do_bounds_check && (type->t & VT_ARRAY)) {
8161 unsigned long *bounds_ptr;
8162 /* add padding between regions */
8163 loc--;
8164 /* then add local bound info */
8165 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8166 bounds_ptr[0] = addr;
8167 bounds_ptr[1] = size;
8169 if (v) {
8170 /* local variable */
8171 sym_push(v, type, r, addr);
8172 } else {
8173 /* push local reference */
8174 vset(type, r, addr);
8176 } else {
8177 Sym *sym;
8179 sym = NULL;
8180 if (v && scope == VT_CONST) {
8181 /* see if the symbol was already defined */
8182 sym = sym_find(v);
8183 if (sym) {
8184 if (!is_compatible_types(&sym->type, type))
8185 error("incompatible types for redefinition of '%s'",
8186 get_tok_str(v, NULL));
8187 if (sym->type.t & VT_EXTERN) {
8188 /* if the variable is extern, it was not allocated */
8189 sym->type.t &= ~VT_EXTERN;
8190 } else {
8191 /* we accept several definitions of the same
8192 global variable. this is tricky, because we
8193 must play with the SHN_COMMON type of the symbol */
8194 /* XXX: should check if the variable was already
8195 initialized. It is incorrect to initialized it
8196 twice */
8197 /* no init data, we won't add more to the symbol */
8198 if (!has_init)
8199 goto no_alloc;
8204 /* allocate symbol in corresponding section */
8205 sec = ad->section;
8206 if (!sec) {
8207 if (has_init)
8208 sec = data_section;
8210 if (sec) {
8211 data_offset = sec->data_offset;
8212 data_offset = (data_offset + align - 1) & -align;
8213 addr = data_offset;
8214 /* very important to increment global pointer at this time
8215 because initializers themselves can create new initializers */
8216 data_offset += size;
8217 /* add padding if bound check */
8218 if (do_bounds_check)
8219 data_offset++;
8220 sec->data_offset = data_offset;
8221 /* allocate section space to put the data */
8222 if (sec->sh_type != SHT_NOBITS &&
8223 data_offset > sec->data_allocated)
8224 section_realloc(sec, data_offset);
8225 } else {
8226 addr = 0; /* avoid warning */
8229 if (v) {
8230 if (scope == VT_CONST) {
8231 if (!sym)
8232 goto do_def;
8233 } else {
8234 do_def:
8235 sym = sym_push(v, type, r | VT_SYM, 0);
8237 /* update symbol definition */
8238 if (sec) {
8239 put_extern_sym(sym, sec, addr, size);
8240 } else {
8241 Elf32_Sym *esym;
8242 /* put a common area */
8243 put_extern_sym(sym, NULL, align, size);
8244 /* XXX: find a nicer way */
8245 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8246 esym->st_shndx = SHN_COMMON;
8248 } else {
8249 CValue cval;
8251 /* push global reference */
8252 sym = get_sym_ref(type, sec, addr, size);
8253 cval.ul = 0;
8254 vsetc(type, VT_CONST | VT_SYM, &cval);
8255 vtop->sym = sym;
8258 /* handles bounds now because the symbol must be defined
8259 before for the relocation */
8260 if (do_bounds_check) {
8261 unsigned long *bounds_ptr;
8263 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8264 /* then add global bound info */
8265 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8266 bounds_ptr[0] = 0; /* relocated */
8267 bounds_ptr[1] = size;
8270 if (has_init) {
8271 decl_initializer(type, sec, addr, 1, 0);
8272 /* restore parse state if needed */
8273 if (init_str.str) {
8274 tok_str_free(init_str.str);
8275 restore_parse_state(&saved_parse_state);
8278 no_alloc: ;
8281 void put_func_debug(Sym *sym)
8283 char buf[512];
8285 /* stabs info */
8286 /* XXX: we put here a dummy type */
8287 snprintf(buf, sizeof(buf), "%s:%c1",
8288 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8289 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8290 cur_text_section, sym->c);
8291 last_ind = 0;
8292 last_line_num = 0;
8295 /* not finished : try to put some local vars in registers */
8296 //#define CONFIG_REG_VARS
8298 #ifdef CONFIG_REG_VARS
8299 void add_var_ref(int t)
8301 printf("%s:%d: &%s\n",
8302 file->filename, file->line_num,
8303 get_tok_str(t, NULL));
8306 /* first pass on a function with heuristic to extract variable usage
8307 and pointer references to local variables for register allocation */
8308 void analyse_function(void)
8310 int level, t;
8312 for(;;) {
8313 if (tok == -1)
8314 break;
8315 /* any symbol coming after '&' is considered as being a
8316 variable whose reference is taken. It is highly unaccurate
8317 but it is difficult to do better without a complete parse */
8318 if (tok == '&') {
8319 next();
8320 /* if '& number', then no need to examine next tokens */
8321 if (tok == TOK_CINT ||
8322 tok == TOK_CUINT ||
8323 tok == TOK_CLLONG ||
8324 tok == TOK_CULLONG) {
8325 continue;
8326 } else if (tok >= TOK_UIDENT) {
8327 /* if '& ident [' or '& ident ->', then ident address
8328 is not needed */
8329 t = tok;
8330 next();
8331 if (tok != '[' && tok != TOK_ARROW)
8332 add_var_ref(t);
8333 } else {
8334 level = 0;
8335 while (tok != '}' && tok != ';' &&
8336 !((tok == ',' || tok == ')') && level == 0)) {
8337 if (tok >= TOK_UIDENT) {
8338 add_var_ref(tok);
8339 } else if (tok == '(') {
8340 level++;
8341 } else if (tok == ')') {
8342 level--;
8344 next();
8347 } else {
8348 next();
8352 #endif
8354 /* parse an old style function declaration list */
8355 /* XXX: check multiple parameter */
8356 static void func_decl_list(Sym *func_sym)
8358 AttributeDef ad;
8359 int v;
8360 Sym *s;
8361 CType btype, type;
8363 /* parse each declaration */
8364 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8365 if (!parse_btype(&btype, &ad))
8366 expect("declaration list");
8367 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8368 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8369 tok == ';') {
8370 /* we accept no variable after */
8371 } else {
8372 for(;;) {
8373 type = btype;
8374 type_decl(&type, &ad, &v, TYPE_DIRECT);
8375 /* find parameter in function parameter list */
8376 s = func_sym->next;
8377 while (s != NULL) {
8378 if ((s->v & ~SYM_FIELD) == v)
8379 goto found;
8380 s = s->next;
8382 error("declaration for parameter '%s' but no such parameter",
8383 get_tok_str(v, NULL));
8384 found:
8385 /* check that no storage specifier except 'register' was given */
8386 if (type.t & VT_STORAGE)
8387 error("storage class specified for '%s'", get_tok_str(v, NULL));
8388 convert_parameter_type(&type);
8389 /* we can add the type (NOTE: it could be local to the function) */
8390 s->type = type;
8391 /* accept other parameters */
8392 if (tok == ',')
8393 next();
8394 else
8395 break;
8398 skip(';');
8402 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8403 static void decl(int l)
8405 int v, has_init, r;
8406 CType type, btype;
8407 Sym *sym;
8408 AttributeDef ad;
8410 while (1) {
8411 if (!parse_btype(&btype, &ad)) {
8412 /* skip redundant ';' */
8413 /* XXX: find more elegant solution */
8414 if (tok == ';') {
8415 next();
8416 continue;
8418 /* special test for old K&R protos without explicit int
8419 type. Only accepted when defining global data */
8420 if (l == VT_LOCAL || tok < TOK_DEFINE)
8421 break;
8422 btype.t = VT_INT;
8424 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8425 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8426 tok == ';') {
8427 /* we accept no variable after */
8428 next();
8429 continue;
8431 while (1) { /* iterate thru each declaration */
8432 type = btype;
8433 type_decl(&type, &ad, &v, TYPE_DIRECT);
8434 #if 0
8436 char buf[500];
8437 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8438 printf("type = '%s'\n", buf);
8440 #endif
8441 if ((type.t & VT_BTYPE) == VT_FUNC) {
8442 /* if old style function prototype, we accept a
8443 declaration list */
8444 sym = type.ref;
8445 if (sym->c == FUNC_OLD)
8446 func_decl_list(sym);
8449 if (tok == '{') {
8450 #ifdef CONFIG_REG_VARS
8451 TokenString func_str;
8452 ParseState saved_parse_state;
8453 int block_level;
8454 #endif
8456 if (l == VT_LOCAL)
8457 error("cannot use local functions");
8458 if (!(type.t & VT_FUNC))
8459 expect("function definition");
8460 /* XXX: cannot do better now: convert extern line to static inline */
8461 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8462 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8464 #ifdef CONFIG_REG_VARS
8465 /* parse all function code and record it */
8467 tok_str_new(&func_str);
8469 block_level = 0;
8470 for(;;) {
8471 int t;
8472 if (tok == -1)
8473 error("unexpected end of file");
8474 tok_str_add_tok(&func_str);
8475 t = tok;
8476 next();
8477 if (t == '{') {
8478 block_level++;
8479 } else if (t == '}') {
8480 block_level--;
8481 if (block_level == 0)
8482 break;
8485 tok_str_add(&func_str, -1);
8486 tok_str_add(&func_str, 0);
8488 save_parse_state(&saved_parse_state);
8490 macro_ptr = func_str.str;
8491 next();
8492 analyse_function();
8493 #endif
8495 /* compute text section */
8496 cur_text_section = ad.section;
8497 if (!cur_text_section)
8498 cur_text_section = text_section;
8499 ind = cur_text_section->data_offset;
8500 funcname = get_tok_str(v, NULL);
8501 sym = sym_find(v);
8502 if (sym) {
8503 /* if symbol is already defined, then put complete type */
8504 sym->type = type;
8505 } else {
8506 /* put function symbol */
8507 sym = global_identifier_push(v, type.t, 0);
8508 sym->type.ref = type.ref;
8510 /* NOTE: we patch the symbol size later */
8511 put_extern_sym(sym, cur_text_section, ind, 0);
8512 func_ind = ind;
8513 sym->r = VT_SYM | VT_CONST;
8514 /* put debug symbol */
8515 if (do_debug)
8516 put_func_debug(sym);
8517 /* push a dummy symbol to enable local sym storage */
8518 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8519 gfunc_prolog(&type);
8520 loc = 0;
8521 rsym = 0;
8522 #ifdef CONFIG_REG_VARS
8523 macro_ptr = func_str.str;
8524 next();
8525 #endif
8526 block(NULL, NULL, NULL, NULL, 0, 0);
8527 gsym(rsym);
8528 gfunc_epilog();
8529 cur_text_section->data_offset = ind;
8530 label_pop(&global_label_stack, NULL);
8531 sym_pop(&local_stack, NULL); /* reset local stack */
8532 /* end of function */
8533 /* patch symbol size */
8534 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8535 ind - func_ind;
8536 if (do_debug) {
8537 put_stabn(N_FUN, 0, 0, ind - func_ind);
8539 funcname = ""; /* for safety */
8540 func_vt.t = VT_VOID; /* for safety */
8541 ind = 0; /* for safety */
8543 #ifdef CONFIG_REG_VARS
8544 tok_str_free(func_str.str);
8545 restore_parse_state(&saved_parse_state);
8546 #endif
8547 break;
8548 } else {
8549 if (btype.t & VT_TYPEDEF) {
8550 /* save typedefed type */
8551 /* XXX: test storage specifiers ? */
8552 sym = sym_push(v, &type, 0, 0);
8553 sym->type.t |= VT_TYPEDEF;
8554 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
8555 /* external function definition */
8556 external_sym(v, &type, 0);
8557 } else {
8558 /* not lvalue if array */
8559 r = 0;
8560 if (!(type.t & VT_ARRAY))
8561 r |= lvalue_type(type.t);
8562 has_init = (tok == '=');
8563 if ((btype.t & VT_EXTERN) ||
8564 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
8565 !has_init && l == VT_CONST && type.ref->c < 0)) {
8566 /* external variable */
8567 /* NOTE: as GCC, uninitialized global static
8568 arrays of null size are considered as
8569 extern */
8570 external_sym(v, &type, r);
8571 } else {
8572 if (type.t & VT_STATIC)
8573 r |= VT_CONST;
8574 else
8575 r |= l;
8576 if (has_init)
8577 next();
8578 decl_initializer_alloc(&type, &ad, r,
8579 has_init, v, l);
8582 if (tok != ',') {
8583 skip(';');
8584 break;
8586 next();
8592 /* better than nothing, but needs extension to handle '-E' option
8593 correctly too */
8594 static void preprocess_init(TCCState *s1)
8596 s1->include_stack_ptr = s1->include_stack;
8597 /* XXX: move that before to avoid having to initialize
8598 file->ifdef_stack_ptr ? */
8599 s1->ifdef_stack_ptr = s1->ifdef_stack;
8600 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
8602 /* XXX: not ANSI compliant: bound checking says error */
8603 vtop = vstack - 1;
8606 /* compile the C file opened in 'file'. Return non zero if errors. */
8607 static int tcc_compile(TCCState *s1)
8609 Sym *define_start;
8610 char buf[512];
8611 volatile int section_sym;
8613 #ifdef INC_DEBUG
8614 printf("%s: **** new file\n", file->filename);
8615 #endif
8616 preprocess_init(s1);
8618 funcname = "";
8619 anon_sym = SYM_FIRST_ANOM;
8621 /* file info: full path + filename */
8622 section_sym = 0; /* avoid warning */
8623 if (do_debug) {
8624 section_sym = put_elf_sym(symtab_section, 0, 0,
8625 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
8626 text_section->sh_num, NULL);
8627 getcwd(buf, sizeof(buf));
8628 pstrcat(buf, sizeof(buf), "/");
8629 put_stabs_r(buf, N_SO, 0, 0,
8630 text_section->data_offset, text_section, section_sym);
8631 put_stabs_r(file->filename, N_SO, 0, 0,
8632 text_section->data_offset, text_section, section_sym);
8634 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8635 symbols can be safely used */
8636 put_elf_sym(symtab_section, 0, 0,
8637 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
8638 SHN_ABS, file->filename);
8640 /* define some often used types */
8641 int_type.t = VT_INT;
8643 char_pointer_type.t = VT_BYTE;
8644 mk_pointer(&char_pointer_type);
8646 func_old_type.t = VT_FUNC;
8647 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
8649 #if 0
8650 /* define 'void *alloca(unsigned int)' builtin function */
8652 Sym *s1;
8654 p = anon_sym++;
8655 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
8656 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
8657 s1->next = NULL;
8658 sym->next = s1;
8659 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
8661 #endif
8663 define_start = define_stack;
8665 if (setjmp(s1->error_jmp_buf) == 0) {
8666 s1->nb_errors = 0;
8667 s1->error_set_jmp_enabled = 1;
8669 ch = file->buf_ptr[0];
8670 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
8671 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
8672 next();
8673 decl(VT_CONST);
8674 if (tok != TOK_EOF)
8675 expect("declaration");
8677 /* end of translation unit info */
8678 if (do_debug) {
8679 put_stabs_r(NULL, N_SO, 0, 0,
8680 text_section->data_offset, text_section, section_sym);
8683 s1->error_set_jmp_enabled = 0;
8685 /* reset define stack, but leave -Dsymbols (may be incorrect if
8686 they are undefined) */
8687 free_defines(define_start);
8689 sym_pop(&global_stack, NULL);
8691 return s1->nb_errors != 0 ? -1 : 0;
8694 #ifdef LIBTCC
8695 int tcc_compile_string(TCCState *s, const char *str)
8697 BufferedFile bf1, *bf = &bf1;
8698 int ret, len;
8699 char *buf;
8701 /* init file structure */
8702 bf->fd = -1;
8703 /* XXX: avoid copying */
8704 len = strlen(str);
8705 buf = tcc_malloc(len + 1);
8706 if (!buf)
8707 return -1;
8708 memcpy(buf, str, len);
8709 buf[len] = CH_EOB;
8710 bf->buf_ptr = buf;
8711 bf->buf_end = buf + len;
8712 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
8713 bf->line_num = 1;
8714 file = bf;
8716 ret = tcc_compile(s);
8718 tcc_free(buf);
8720 /* currently, no need to close */
8721 return ret;
8723 #endif
8725 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8726 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
8728 BufferedFile bf1, *bf = &bf1;
8730 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
8731 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
8732 /* default value */
8733 if (!value)
8734 value = "1";
8735 pstrcat(bf->buffer, IO_BUF_SIZE, value);
8737 /* init file structure */
8738 bf->fd = -1;
8739 bf->buf_ptr = bf->buffer;
8740 bf->buf_end = bf->buffer + strlen(bf->buffer);
8741 *bf->buf_end = CH_EOB;
8742 bf->filename[0] = '\0';
8743 bf->line_num = 1;
8744 file = bf;
8746 s1->include_stack_ptr = s1->include_stack;
8748 /* parse with define parser */
8749 ch = file->buf_ptr[0];
8750 next_nomacro();
8751 parse_define();
8752 file = NULL;
8755 /* undefine a preprocessor symbol */
8756 void tcc_undefine_symbol(TCCState *s1, const char *sym)
8758 TokenSym *ts;
8759 Sym *s;
8760 ts = tok_alloc(sym, strlen(sym));
8761 s = define_find(ts->tok);
8762 /* undefine symbol by putting an invalid name */
8763 if (s)
8764 define_undef(s);
8767 #ifdef CONFIG_TCC_ASM
8769 #include "i386-asm.c"
8770 #include "tccasm.c"
8772 #else
8773 static void asm_instr(void)
8775 error("inline asm() not supported");
8777 #endif
8779 #include "tccelf.c"
8781 /* print the position in the source file of PC value 'pc' by reading
8782 the stabs debug information */
8783 static void rt_printline(unsigned long wanted_pc)
8785 Stab_Sym *sym, *sym_end;
8786 char func_name[128], last_func_name[128];
8787 unsigned long func_addr, last_pc, pc;
8788 const char *incl_files[INCLUDE_STACK_SIZE];
8789 int incl_index, len, last_line_num, i;
8790 const char *str, *p;
8792 fprintf(stderr, "0x%08lx:", wanted_pc);
8794 func_name[0] = '\0';
8795 func_addr = 0;
8796 incl_index = 0;
8797 last_func_name[0] = '\0';
8798 last_pc = 0xffffffff;
8799 last_line_num = 1;
8800 sym = (Stab_Sym *)stab_section->data + 1;
8801 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
8802 while (sym < sym_end) {
8803 switch(sym->n_type) {
8804 /* function start or end */
8805 case N_FUN:
8806 if (sym->n_strx == 0) {
8807 /* we test if between last line and end of function */
8808 pc = sym->n_value + func_addr;
8809 if (wanted_pc >= last_pc && wanted_pc < pc)
8810 goto found;
8811 func_name[0] = '\0';
8812 func_addr = 0;
8813 } else {
8814 str = stabstr_section->data + sym->n_strx;
8815 p = strchr(str, ':');
8816 if (!p) {
8817 pstrcpy(func_name, sizeof(func_name), str);
8818 } else {
8819 len = p - str;
8820 if (len > sizeof(func_name) - 1)
8821 len = sizeof(func_name) - 1;
8822 memcpy(func_name, str, len);
8823 func_name[len] = '\0';
8825 func_addr = sym->n_value;
8827 break;
8828 /* line number info */
8829 case N_SLINE:
8830 pc = sym->n_value + func_addr;
8831 if (wanted_pc >= last_pc && wanted_pc < pc)
8832 goto found;
8833 last_pc = pc;
8834 last_line_num = sym->n_desc;
8835 /* XXX: slow! */
8836 strcpy(last_func_name, func_name);
8837 break;
8838 /* include files */
8839 case N_BINCL:
8840 str = stabstr_section->data + sym->n_strx;
8841 add_incl:
8842 if (incl_index < INCLUDE_STACK_SIZE) {
8843 incl_files[incl_index++] = str;
8845 break;
8846 case N_EINCL:
8847 if (incl_index > 1)
8848 incl_index--;
8849 break;
8850 case N_SO:
8851 if (sym->n_strx == 0) {
8852 incl_index = 0; /* end of translation unit */
8853 } else {
8854 str = stabstr_section->data + sym->n_strx;
8855 /* do not add path */
8856 len = strlen(str);
8857 if (len > 0 && str[len - 1] != '/')
8858 goto add_incl;
8860 break;
8862 sym++;
8865 /* second pass: we try symtab symbols (no line number info) */
8866 incl_index = 0;
8868 Elf32_Sym *sym, *sym_end;
8869 int type;
8871 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
8872 for(sym = (Elf32_Sym *)symtab_section->data + 1;
8873 sym < sym_end;
8874 sym++) {
8875 type = ELF32_ST_TYPE(sym->st_info);
8876 if (type == STT_FUNC) {
8877 if (wanted_pc >= sym->st_value &&
8878 wanted_pc < sym->st_value + sym->st_size) {
8879 pstrcpy(last_func_name, sizeof(last_func_name),
8880 strtab_section->data + sym->st_name);
8881 goto found;
8886 /* did not find any info: */
8887 fprintf(stderr, " ???\n");
8888 return;
8889 found:
8890 if (last_func_name[0] != '\0') {
8891 fprintf(stderr, " %s()", last_func_name);
8893 if (incl_index > 0) {
8894 fprintf(stderr, " (%s:%d",
8895 incl_files[incl_index - 1], last_line_num);
8896 for(i = incl_index - 2; i >= 0; i--)
8897 fprintf(stderr, ", included from %s", incl_files[i]);
8898 fprintf(stderr, ")");
8900 fprintf(stderr, "\n");
8903 #ifndef WIN32
8905 #ifdef __i386__
8907 /* fix for glibc 2.1 */
8908 #ifndef REG_EIP
8909 #define REG_EIP EIP
8910 #define REG_EBP EBP
8911 #endif
8913 /* return the PC at frame level 'level'. Return non zero if not found */
8914 static int rt_get_caller_pc(unsigned long *paddr,
8915 ucontext_t *uc, int level)
8917 unsigned long fp;
8918 int i;
8920 if (level == 0) {
8921 #ifdef __FreeBSD__
8922 *paddr = uc->uc_mcontext.mc_eip;
8923 #else
8924 *paddr = uc->uc_mcontext.gregs[REG_EIP];
8925 #endif
8926 return 0;
8927 } else {
8928 #ifdef __FreeBSD__
8929 fp = uc->uc_mcontext.mc_ebp;
8930 #else
8931 fp = uc->uc_mcontext.gregs[REG_EBP];
8932 #endif
8933 for(i=1;i<level;i++) {
8934 /* XXX: check address validity with program info */
8935 if (fp <= 0x1000 || fp >= 0xc0000000)
8936 return -1;
8937 fp = ((unsigned long *)fp)[0];
8939 *paddr = ((unsigned long *)fp)[1];
8940 return 0;
8943 #else
8944 #error add arch specific rt_get_caller_pc()
8945 #endif
8947 /* emit a run time error at position 'pc' */
8948 void rt_error(ucontext_t *uc, const char *fmt, ...)
8950 va_list ap;
8951 unsigned long pc;
8952 int i;
8954 va_start(ap, fmt);
8955 fprintf(stderr, "Runtime error: ");
8956 vfprintf(stderr, fmt, ap);
8957 fprintf(stderr, "\n");
8958 for(i=0;i<num_callers;i++) {
8959 if (rt_get_caller_pc(&pc, uc, i) < 0)
8960 break;
8961 if (i == 0)
8962 fprintf(stderr, "at ");
8963 else
8964 fprintf(stderr, "by ");
8965 rt_printline(pc);
8967 exit(255);
8968 va_end(ap);
8971 /* signal handler for fatal errors */
8972 static void sig_error(int signum, siginfo_t *siginf, void *puc)
8974 ucontext_t *uc = puc;
8976 switch(signum) {
8977 case SIGFPE:
8978 switch(siginf->si_code) {
8979 case FPE_INTDIV:
8980 case FPE_FLTDIV:
8981 rt_error(uc, "division by zero");
8982 break;
8983 default:
8984 rt_error(uc, "floating point exception");
8985 break;
8987 break;
8988 case SIGBUS:
8989 case SIGSEGV:
8990 if (rt_bound_error_msg && *rt_bound_error_msg)
8991 rt_error(uc, *rt_bound_error_msg);
8992 else
8993 rt_error(uc, "dereferencing invalid pointer");
8994 break;
8995 case SIGILL:
8996 rt_error(uc, "illegal instruction");
8997 break;
8998 case SIGABRT:
8999 rt_error(uc, "abort() called");
9000 break;
9001 default:
9002 rt_error(uc, "caught signal %d", signum);
9003 break;
9005 exit(255);
9007 #endif
9009 /* do all relocations (needed before using tcc_get_symbol()) */
9010 int tcc_relocate(TCCState *s1)
9012 Section *s;
9013 int i;
9015 s1->nb_errors = 0;
9017 tcc_add_runtime(s1);
9019 relocate_common_syms();
9021 /* compute relocation address : section are relocated in place. We
9022 also alloc the bss space */
9023 for(i = 1; i < s1->nb_sections; i++) {
9024 s = s1->sections[i];
9025 if (s->sh_flags & SHF_ALLOC) {
9026 if (s->sh_type == SHT_NOBITS)
9027 s->data = tcc_mallocz(s->data_offset);
9028 s->sh_addr = (unsigned long)s->data;
9032 relocate_syms(s1, 1);
9034 if (s1->nb_errors != 0)
9035 return -1;
9037 /* relocate each section */
9038 for(i = 1; i < s1->nb_sections; i++) {
9039 s = s1->sections[i];
9040 if (s->reloc)
9041 relocate_section(s1, s);
9043 return 0;
9046 /* launch the compiled program with the given arguments */
9047 int tcc_run(TCCState *s1, int argc, char **argv)
9049 int (*prog_main)(int, char **);
9051 if (tcc_relocate(s1) < 0)
9052 return -1;
9054 prog_main = tcc_get_symbol(s1, "main");
9056 if (do_debug) {
9057 #ifdef WIN32
9058 error("debug mode currently not available for Windows");
9059 #else
9060 struct sigaction sigact;
9061 /* install TCC signal handlers to print debug info on fatal
9062 runtime errors */
9063 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
9064 sigact.sa_sigaction = sig_error;
9065 sigemptyset(&sigact.sa_mask);
9066 sigaction(SIGFPE, &sigact, NULL);
9067 sigaction(SIGILL, &sigact, NULL);
9068 sigaction(SIGSEGV, &sigact, NULL);
9069 sigaction(SIGBUS, &sigact, NULL);
9070 sigaction(SIGABRT, &sigact, NULL);
9071 #endif
9074 #ifdef CONFIG_TCC_BCHECK
9075 if (do_bounds_check) {
9076 void (*bound_init)(void);
9078 /* set error function */
9079 rt_bound_error_msg = (void *)tcc_get_symbol(s1, "__bound_error_msg");
9081 /* XXX: use .init section so that it also work in binary ? */
9082 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
9083 bound_init();
9085 #endif
9086 return (*prog_main)(argc, argv);
9089 TCCState *tcc_new(void)
9091 const char *p, *r;
9092 TCCState *s;
9093 TokenSym *ts;
9094 int i, c;
9096 s = tcc_mallocz(sizeof(TCCState));
9097 if (!s)
9098 return NULL;
9099 tcc_state = s;
9100 s->output_type = TCC_OUTPUT_MEMORY;
9102 /* init isid table */
9103 for(i=0;i<256;i++)
9104 isidnum_table[i] = isid(i) || isnum(i);
9106 /* add all tokens */
9107 table_ident = NULL;
9108 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9110 tok_ident = TOK_IDENT;
9111 p = tcc_keywords;
9112 while (*p) {
9113 r = p;
9114 for(;;) {
9115 c = *r++;
9116 if (c == '\0')
9117 break;
9119 ts = tok_alloc(p, r - p - 1);
9120 p = r;
9123 /* we add dummy defines for some special macros to speed up tests
9124 and to have working defined() */
9125 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9126 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9127 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9128 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9130 /* standard defines */
9131 tcc_define_symbol(s, "__STDC__", NULL);
9132 #if defined(TCC_TARGET_I386)
9133 tcc_define_symbol(s, "__i386__", NULL);
9134 #endif
9135 #if defined(linux)
9136 tcc_define_symbol(s, "__linux__", NULL);
9137 tcc_define_symbol(s, "linux", NULL);
9138 #endif
9139 /* tiny C specific defines */
9140 tcc_define_symbol(s, "__TINYC__", NULL);
9142 /* tiny C & gcc defines */
9143 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9144 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9145 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9147 /* default library paths */
9148 tcc_add_library_path(s, "/usr/local/lib");
9149 tcc_add_library_path(s, "/usr/lib");
9150 tcc_add_library_path(s, "/lib");
9152 /* no section zero */
9153 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9155 /* create standard sections */
9156 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9157 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9158 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9160 /* symbols are always generated for linking stage */
9161 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9162 ".strtab",
9163 ".hashtab", SHF_PRIVATE);
9164 strtab_section = symtab_section->link;
9166 /* private symbol table for dynamic symbols */
9167 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9168 ".dynstrtab",
9169 ".dynhashtab", SHF_PRIVATE);
9170 return s;
9173 void tcc_delete(TCCState *s1)
9175 int i, n;
9177 /* free -D defines */
9178 free_defines(NULL);
9180 /* free tokens */
9181 n = tok_ident - TOK_IDENT;
9182 for(i = 0; i < n; i++)
9183 tcc_free(table_ident[i]);
9184 tcc_free(table_ident);
9186 /* free all sections */
9188 free_section(symtab_section->hash);
9190 free_section(s1->dynsymtab_section->hash);
9191 free_section(s1->dynsymtab_section->link);
9192 free_section(s1->dynsymtab_section);
9194 for(i = 1; i < s1->nb_sections; i++)
9195 free_section(s1->sections[i]);
9196 tcc_free(s1->sections);
9198 /* free loaded dlls array */
9199 for(i = 0; i < s1->nb_loaded_dlls; i++)
9200 tcc_free(s1->loaded_dlls[i]);
9201 tcc_free(s1->loaded_dlls);
9203 /* library paths */
9204 for(i = 0; i < s1->nb_library_paths; i++)
9205 tcc_free(s1->library_paths[i]);
9206 tcc_free(s1->library_paths);
9208 /* cached includes */
9209 for(i = 0; i < s1->nb_cached_includes; i++)
9210 tcc_free(s1->cached_includes[i]);
9211 tcc_free(s1->cached_includes);
9213 for(i = 0; i < s1->nb_include_paths; i++)
9214 tcc_free(s1->include_paths[i]);
9215 tcc_free(s1->include_paths);
9217 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9218 tcc_free(s1->sysinclude_paths[i]);
9219 tcc_free(s1->sysinclude_paths);
9221 tcc_free(s1);
9224 int tcc_add_include_path(TCCState *s1, const char *pathname)
9226 char *pathname1;
9228 pathname1 = tcc_strdup(pathname);
9229 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9230 return 0;
9233 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9235 char *pathname1;
9237 pathname1 = tcc_strdup(pathname);
9238 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9239 return 0;
9242 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9244 const char *ext, *filename1;
9245 Elf32_Ehdr ehdr;
9246 int fd, ret;
9247 BufferedFile *saved_file;
9249 /* find source file type with extension */
9250 filename1 = strrchr(filename, '/');
9251 if (filename1)
9252 filename1++;
9253 else
9254 filename1 = filename;
9255 ext = strrchr(filename1, '.');
9256 if (ext)
9257 ext++;
9259 /* open the file */
9260 saved_file = file;
9261 file = tcc_open(s1, filename);
9262 if (!file) {
9263 if (flags & AFF_PRINT_ERROR) {
9264 error_noabort("file '%s' not found", filename);
9266 ret = -1;
9267 goto fail1;
9270 if (!ext || !strcmp(ext, "c")) {
9271 /* C file assumed */
9272 ret = tcc_compile(s1);
9273 } else
9274 #ifdef CONFIG_TCC_ASM
9275 if (!strcmp(ext, "S")) {
9276 /* preprocessed assembler */
9277 ret = tcc_assemble(s1, 1);
9278 } else if (!strcmp(ext, "s")) {
9279 /* non preprocessed assembler */
9280 ret = tcc_assemble(s1, 0);
9281 } else
9282 #endif
9284 fd = file->fd;
9285 /* assume executable format: auto guess file type */
9286 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
9287 error_noabort("could not read header");
9288 goto fail;
9290 lseek(fd, 0, SEEK_SET);
9292 if (ehdr.e_ident[0] == ELFMAG0 &&
9293 ehdr.e_ident[1] == ELFMAG1 &&
9294 ehdr.e_ident[2] == ELFMAG2 &&
9295 ehdr.e_ident[3] == ELFMAG3) {
9296 file->line_num = 0; /* do not display line number if error */
9297 if (ehdr.e_type == ET_REL) {
9298 ret = tcc_load_object_file(s1, fd, 0);
9299 } else if (ehdr.e_type == ET_DYN) {
9300 ret = tcc_load_dll(s1, fd, filename,
9301 (flags & AFF_REFERENCED_DLL) != 0);
9302 } else {
9303 error_noabort("unrecognized ELF file");
9304 goto fail;
9306 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9307 file->line_num = 0; /* do not display line number if error */
9308 ret = tcc_load_archive(s1, fd);
9309 } else {
9310 /* as GNU ld, consider it is an ld script if not recognized */
9311 ret = tcc_load_ldscript(s1);
9312 if (ret < 0) {
9313 error_noabort("unrecognized file type");
9314 goto fail;
9318 the_end:
9319 tcc_close(file);
9320 fail1:
9321 file = saved_file;
9322 return ret;
9323 fail:
9324 ret = -1;
9325 goto the_end;
9328 int tcc_add_file(TCCState *s, const char *filename)
9330 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9333 int tcc_add_library_path(TCCState *s, const char *pathname)
9335 char *pathname1;
9337 pathname1 = tcc_strdup(pathname);
9338 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9339 return 0;
9342 /* find and load a dll. Return non zero if not found */
9343 /* XXX: add '-rpath' option support ? */
9344 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9346 char buf[1024];
9347 int i;
9349 for(i = 0; i < s->nb_library_paths; i++) {
9350 snprintf(buf, sizeof(buf), "%s/%s",
9351 s->library_paths[i], filename);
9352 if (tcc_add_file_internal(s, buf, flags) == 0)
9353 return 0;
9355 return -1;
9358 /* the library name is the same as the argument of the '-l' option */
9359 int tcc_add_library(TCCState *s, const char *libraryname)
9361 char buf[1024];
9362 int i;
9363 void *h;
9365 /* first we look for the dynamic library if not static linking */
9366 if (!s->static_link) {
9367 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9368 /* if we output to memory, then we simply we dlopen(). */
9369 if (s->output_type == TCC_OUTPUT_MEMORY) {
9370 /* Since the libc is already loaded, we don't need to load it again */
9371 if (!strcmp(libraryname, "c"))
9372 return 0;
9373 h = dlopen(buf, RTLD_GLOBAL | RTLD_LAZY);
9374 if (h)
9375 return 0;
9376 } else {
9377 if (tcc_add_dll(s, buf, 0) == 0)
9378 return 0;
9382 /* then we look for the static library */
9383 for(i = 0; i < s->nb_library_paths; i++) {
9384 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9385 s->library_paths[i], libraryname);
9386 if (tcc_add_file_internal(s, buf, 0) == 0)
9387 return 0;
9389 return -1;
9392 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9394 add_elf_sym(symtab_section, val, 0,
9395 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9396 SHN_ABS, name);
9397 return 0;
9400 int tcc_set_output_type(TCCState *s, int output_type)
9402 char buf[1024];
9404 s->output_type = output_type;
9406 if (!s->nostdinc) {
9407 /* default include paths */
9408 /* XXX: reverse order needed if -isystem support */
9409 tcc_add_sysinclude_path(s, "/usr/local/include");
9410 tcc_add_sysinclude_path(s, "/usr/include");
9411 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
9412 tcc_add_sysinclude_path(s, buf);
9415 /* if bound checking, then add corresponding sections */
9416 #ifdef CONFIG_TCC_BCHECK
9417 if (do_bounds_check) {
9418 /* define symbol */
9419 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
9420 /* create bounds sections */
9421 bounds_section = new_section(s, ".bounds",
9422 SHT_PROGBITS, SHF_ALLOC);
9423 lbounds_section = new_section(s, ".lbounds",
9424 SHT_PROGBITS, SHF_ALLOC);
9426 #endif
9428 /* add debug sections */
9429 if (do_debug) {
9430 /* stab symbols */
9431 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
9432 stab_section->sh_entsize = sizeof(Stab_Sym);
9433 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
9434 put_elf_str(stabstr_section, "");
9435 stab_section->link = stabstr_section;
9436 /* put first entry */
9437 put_stabs("", 0, 0, 0, 0);
9440 /* add libc crt1/crti objects */
9441 if (output_type == TCC_OUTPUT_EXE ||
9442 output_type == TCC_OUTPUT_DLL) {
9443 if (output_type != TCC_OUTPUT_DLL)
9444 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
9445 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
9447 return 0;
9450 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
9452 typedef struct WarningDef {
9453 int offset;
9454 int flags;
9455 const char *name;
9456 } WarningDef;
9458 static const WarningDef warning_defs[] = {
9459 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
9460 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
9461 { offsetof(TCCState, warn_error), 0, "error" },
9464 /* set/reset a warning */
9465 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
9467 int i;
9468 const WarningDef *p;
9469 if (!strcmp(warning_name, "all")) {
9470 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9471 if (p->flags & WD_ALL)
9472 *(int *)((uint8_t *)s + p->offset) = 1;
9474 } else {
9475 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
9476 if (!strcmp(warning_name, p->name))
9477 goto found;
9479 return -1;
9480 found:
9481 *(int *)((uint8_t *)s + p->offset) = value;
9483 return 0;
9487 #if !defined(LIBTCC)
9489 /* extract the basename of a file */
9490 static const char *tcc_basename(const char *name)
9492 const char *p;
9493 p = strrchr(name, '/');
9494 #ifdef WIN32
9495 if (!p)
9496 p = strrchr(name, '\\');
9497 #endif
9498 if (!p)
9499 p = name;
9500 else
9501 p++;
9502 return p;
9505 static int64_t getclock_us(void)
9507 #ifdef WIN32
9508 struct _timeb tb;
9509 _ftime(&tb);
9510 return (tb.time * 1000LL + tb.millitm) * 1000LL;
9511 #else
9512 struct timeval tv;
9513 gettimeofday(&tv, NULL);
9514 return tv.tv_sec * 1000000LL + tv.tv_usec;
9515 #endif
9518 void help(void)
9520 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2003 Fabrice Bellard\n"
9521 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9522 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9523 " [infile1 infile2...] [-run infile args...]\n"
9524 "\n"
9525 "General options:\n"
9526 " -v display current version\n"
9527 " -c compile only - generate an object file\n"
9528 " -o outfile set output filename\n"
9529 " -Bdir set tcc internal library path\n"
9530 " -bench output compilation statistics\n"
9531 " -run run compiled source\n"
9532 " -Wwarning set or reset (with 'no-' prefix) 'warning'\n"
9533 "Preprocessor options:\n"
9534 " -Idir add include path 'dir'\n"
9535 " -Dsym[=val] define 'sym' with value 'val'\n"
9536 " -Usym undefine 'sym'\n"
9537 "Linker options:\n"
9538 " -Ldir add library path 'dir'\n"
9539 " -llib link with dynamic or static library 'lib'\n"
9540 " -shared generate a shared library\n"
9541 " -static static linking\n"
9542 " -r relocatable output\n"
9543 "Debugger options:\n"
9544 " -g generate runtime debug info\n"
9545 #ifdef CONFIG_TCC_BCHECK
9546 " -b compile with built-in memory and bounds checker (implies -g)\n"
9547 #endif
9548 " -bt N show N callers in stack traces\n"
9552 #define TCC_OPTION_HAS_ARG 0x0001
9553 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9555 typedef struct TCCOption {
9556 const char *name;
9557 uint16_t index;
9558 uint16_t flags;
9559 } TCCOption;
9561 enum {
9562 TCC_OPTION_HELP,
9563 TCC_OPTION_I,
9564 TCC_OPTION_D,
9565 TCC_OPTION_U,
9566 TCC_OPTION_L,
9567 TCC_OPTION_B,
9568 TCC_OPTION_l,
9569 TCC_OPTION_bench,
9570 TCC_OPTION_bt,
9571 TCC_OPTION_b,
9572 TCC_OPTION_g,
9573 TCC_OPTION_c,
9574 TCC_OPTION_static,
9575 TCC_OPTION_shared,
9576 TCC_OPTION_o,
9577 TCC_OPTION_r,
9578 TCC_OPTION_W,
9579 TCC_OPTION_O,
9580 TCC_OPTION_m,
9581 TCC_OPTION_f,
9582 TCC_OPTION_nostdinc,
9583 TCC_OPTION_print_search_dirs,
9584 TCC_OPTION_rdynamic,
9585 TCC_OPTION_run,
9586 TCC_OPTION_v,
9589 static const TCCOption tcc_options[] = {
9590 { "h", TCC_OPTION_HELP, 0 },
9591 { "?", TCC_OPTION_HELP, 0 },
9592 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
9593 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
9594 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
9595 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
9596 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
9597 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9598 { "bench", TCC_OPTION_bench, 0 },
9599 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
9600 #ifdef CONFIG_TCC_BCHECK
9601 { "b", TCC_OPTION_b, 0 },
9602 #endif
9603 { "g", TCC_OPTION_g, 0 },
9604 { "c", TCC_OPTION_c, 0 },
9605 { "static", TCC_OPTION_static, 0 },
9606 { "shared", TCC_OPTION_shared, 0 },
9607 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
9608 { "run", TCC_OPTION_run, 0 },
9609 { "rdynamic", TCC_OPTION_rdynamic, 0 }, /* currently ignored */
9610 { "r", TCC_OPTION_r, 0 },
9611 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9612 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9613 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
9614 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9615 { "nostdinc", TCC_OPTION_nostdinc, 0 },
9616 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
9617 { "v", TCC_OPTION_v, 0 },
9618 { NULL },
9621 int main(int argc, char **argv)
9623 char *r;
9624 int optind, output_type, multiple_files, i, reloc_output;
9625 TCCState *s;
9626 char **files;
9627 int nb_files, nb_libraries, nb_objfiles, dminus, ret;
9628 char objfilename[1024];
9629 int64_t start_time = 0;
9630 const TCCOption *popt;
9631 const char *optarg, *p1, *r1, *outfile;
9632 int print_search_dirs;
9634 s = tcc_new();
9635 output_type = TCC_OUTPUT_EXE;
9637 optind = 1;
9638 outfile = NULL;
9639 multiple_files = 1;
9640 dminus = 0;
9641 files = NULL;
9642 nb_files = 0;
9643 nb_libraries = 0;
9644 reloc_output = 0;
9645 print_search_dirs = 0;
9646 while (1) {
9647 if (optind >= argc) {
9648 if (nb_files == 0 && !print_search_dirs)
9649 goto show_help;
9650 else
9651 break;
9653 r = argv[optind++];
9654 if (r[0] != '-') {
9655 /* add a new file */
9656 dynarray_add((void ***)&files, &nb_files, r);
9657 if (!multiple_files) {
9658 optind--;
9659 /* argv[0] will be this file */
9660 break;
9662 } else {
9663 /* find option in table (match only the first chars */
9664 popt = tcc_options;
9665 for(;;) {
9666 p1 = popt->name;
9667 if (p1 == NULL)
9668 error("invalid option -- '%s'", r);
9669 r1 = r + 1;
9670 for(;;) {
9671 if (*p1 == '\0')
9672 goto option_found;
9673 if (*r1 != *p1)
9674 break;
9675 p1++;
9676 r1++;
9678 popt++;
9680 option_found:
9681 if (popt->flags & TCC_OPTION_HAS_ARG) {
9682 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
9683 optarg = r1;
9684 } else {
9685 if (optind >= argc)
9686 error("argument to '%s' is missing", r);
9687 optarg = argv[optind++];
9689 } else {
9690 if (*r1 != '\0')
9691 goto show_help;
9692 optarg = NULL;
9695 switch(popt->index) {
9696 case TCC_OPTION_HELP:
9697 show_help:
9698 help();
9699 return 1;
9700 case TCC_OPTION_I:
9701 if (tcc_add_include_path(s, optarg) < 0)
9702 error("too many include paths");
9703 break;
9704 case TCC_OPTION_D:
9706 char *sym, *value;
9707 sym = (char *)optarg;
9708 value = strchr(sym, '=');
9709 if (value) {
9710 *value = '\0';
9711 value++;
9713 tcc_define_symbol(s, sym, value);
9715 break;
9716 case TCC_OPTION_U:
9717 tcc_undefine_symbol(s, optarg);
9718 break;
9719 case TCC_OPTION_L:
9720 tcc_add_library_path(s, optarg);
9721 break;
9722 case TCC_OPTION_B:
9723 /* set tcc utilities path (mainly for tcc development) */
9724 tcc_lib_path = optarg;
9725 break;
9726 case TCC_OPTION_l:
9727 dynarray_add((void ***)&files, &nb_files, r);
9728 nb_libraries++;
9729 break;
9730 case TCC_OPTION_bench:
9731 do_bench = 1;
9732 break;
9733 case TCC_OPTION_bt:
9734 num_callers = atoi(optarg);
9735 break;
9736 #ifdef CONFIG_TCC_BCHECK
9737 case TCC_OPTION_b:
9738 do_bounds_check = 1;
9739 do_debug = 1;
9740 break;
9741 #endif
9742 case TCC_OPTION_g:
9743 do_debug = 1;
9744 break;
9745 case TCC_OPTION_c:
9746 multiple_files = 1;
9747 output_type = TCC_OUTPUT_OBJ;
9748 break;
9749 case TCC_OPTION_static:
9750 s->static_link = 1;
9751 break;
9752 case TCC_OPTION_shared:
9753 output_type = TCC_OUTPUT_DLL;
9754 break;
9755 case TCC_OPTION_o:
9756 multiple_files = 1;
9757 outfile = optarg;
9758 break;
9759 case TCC_OPTION_r:
9760 /* generate a .o merging several output files */
9761 reloc_output = 1;
9762 output_type = TCC_OUTPUT_OBJ;
9763 break;
9764 case TCC_OPTION_nostdinc:
9765 s->nostdinc = 1;
9766 break;
9767 case TCC_OPTION_print_search_dirs:
9768 print_search_dirs = 1;
9769 break;
9770 case TCC_OPTION_run:
9771 multiple_files = 0;
9772 output_type = TCC_OUTPUT_MEMORY;
9773 break;
9774 case TCC_OPTION_v:
9775 printf("tcc version %s\n", TCC_VERSION);
9776 return 0;
9777 case TCC_OPTION_W:
9779 const char *p = optarg;
9780 int value;
9781 value = 1;
9782 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-') {
9783 p += 2;
9784 value = 0;
9786 if (tcc_set_warning(s, p, value) < 0 && s->warn_unsupported)
9787 goto unsupported_option;
9789 break;
9790 default:
9791 if (s->warn_unsupported) {
9792 unsupported_option:
9793 warning("unsupported option '%s'", r);
9795 break;
9799 if (print_search_dirs) {
9800 /* enough for Linux kernel */
9801 printf("install: %s/\n", tcc_lib_path);
9802 return 0;
9805 nb_objfiles = nb_files - nb_libraries;
9807 /* if outfile provided without other options, we output an
9808 executable */
9809 if (outfile && output_type == TCC_OUTPUT_MEMORY)
9810 output_type = TCC_OUTPUT_EXE;
9812 /* check -c consistency : only single file handled. XXX: checks file type */
9813 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9814 /* accepts only a single input file */
9815 if (nb_objfiles != 1)
9816 error("cannot specify multiple files with -c");
9817 if (nb_libraries != 0)
9818 error("cannot specify libraries with -c");
9821 /* compute default outfile name */
9822 if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
9823 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9824 char *ext;
9825 /* strip path */
9826 pstrcpy(objfilename, sizeof(objfilename) - 1,
9827 tcc_basename(files[0]));
9828 /* add .o extension */
9829 ext = strrchr(objfilename, '.');
9830 if (!ext)
9831 goto default_outfile;
9832 strcpy(ext + 1, "o");
9833 } else {
9834 default_outfile:
9835 pstrcpy(objfilename, sizeof(objfilename), "a.out");
9837 outfile = objfilename;
9840 if (do_bench) {
9841 start_time = getclock_us();
9844 tcc_set_output_type(s, output_type);
9846 /* compile or add each files or library */
9847 for(i = 0;i < nb_files; i++) {
9848 const char *filename;
9850 filename = files[i];
9851 if (filename[0] == '-') {
9852 if (tcc_add_library(s, filename + 2) < 0)
9853 error("cannot find %s", filename);
9854 } else {
9855 if (tcc_add_file(s, filename) < 0) {
9856 ret = 1;
9857 goto the_end;
9862 /* free all files */
9863 tcc_free(files);
9865 if (do_bench) {
9866 double total_time;
9867 total_time = (double)(getclock_us() - start_time) / 1000000.0;
9868 if (total_time < 0.001)
9869 total_time = 0.001;
9870 if (total_bytes < 1)
9871 total_bytes = 1;
9872 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
9873 tok_ident - TOK_IDENT, total_lines, total_bytes,
9874 total_time, (int)(total_lines / total_time),
9875 total_bytes / total_time / 1000000.0);
9878 if (s->output_type != TCC_OUTPUT_MEMORY) {
9879 tcc_output_file(s, outfile);
9880 ret = 0;
9881 } else {
9882 ret = tcc_run(s, argc - optind, argv + optind);
9884 the_end:
9885 /* XXX: cannot do it with bound checking because of the malloc hooks */
9886 if (!do_bounds_check)
9887 tcc_delete(s);
9889 #ifdef MEM_DEBUG
9890 if (do_bench) {
9891 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
9893 #endif
9894 return ret;
9897 #endif