update manual, changelog
[tinycc.git] / tcc.c
blob9e30eeb074f092be03d0da0248801fac7204243e
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #define _GNU_SOURCE
21 #include "config.h"
23 #ifdef CONFIG_TCCBOOT
25 #include "tccboot.h"
26 #define CONFIG_TCC_STATIC
28 #else
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <math.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <fcntl.h>
39 #include <setjmp.h>
40 #include <time.h>
41 #ifdef _WIN32
42 #include <sys/timeb.h>
43 #include <windows.h>
44 #endif
45 #ifndef _WIN32
46 #include <sys/time.h>
47 #include <sys/ucontext.h>
48 #include <sys/mman.h>
49 #endif
51 #endif /* !CONFIG_TCCBOOT */
53 #ifndef PAGESIZE
54 #define PAGESIZE 4096
55 #endif
57 #include "elf.h"
58 #include "stab.h"
60 #ifndef O_BINARY
61 #define O_BINARY 0
62 #endif
64 #include "libtcc.h"
66 /* parser debug */
67 //#define PARSE_DEBUG
68 /* preprocessor debug */
69 //#define PP_DEBUG
70 /* include file debug */
71 //#define INC_DEBUG
73 //#define MEM_DEBUG
75 /* assembler debug */
76 //#define ASM_DEBUG
78 /* target selection */
79 //#define TCC_TARGET_I386 /* i386 code generator */
80 //#define TCC_TARGET_ARM /* ARMv4 code generator */
81 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
83 /* default target is I386 */
84 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
85 !defined(TCC_TARGET_C67)
86 #define TCC_TARGET_I386
87 #endif
89 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
90 !defined(TCC_TARGET_C67)
91 #define CONFIG_TCC_BCHECK /* enable bound checking code */
92 #endif
94 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
95 #define CONFIG_TCC_STATIC
96 #endif
98 /* define it to include assembler support */
99 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
100 #define CONFIG_TCC_ASM
101 #endif
103 /* object format selection */
104 #if defined(TCC_TARGET_C67)
105 #define TCC_TARGET_COFF
106 #endif
108 #define FALSE 0
109 #define false 0
110 #define TRUE 1
111 #define true 1
112 typedef int BOOL;
114 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
115 executables or dlls */
116 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
118 #define INCLUDE_STACK_SIZE 32
119 #define IFDEF_STACK_SIZE 64
120 #define VSTACK_SIZE 256
121 #define STRING_MAX_SIZE 1024
122 #define PACK_STACK_SIZE 8
124 #define TOK_HASH_SIZE 8192 /* must be a power of two */
125 #define TOK_ALLOC_INCR 512 /* must be a power of two */
126 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
128 /* token symbol management */
129 typedef struct TokenSym {
130 struct TokenSym *hash_next;
131 struct Sym *sym_define; /* direct pointer to define */
132 struct Sym *sym_label; /* direct pointer to label */
133 struct Sym *sym_struct; /* direct pointer to structure */
134 struct Sym *sym_identifier; /* direct pointer to identifier */
135 int tok; /* token number */
136 int len;
137 char str[1];
138 } TokenSym;
140 #ifdef TCC_TARGET_PE
141 typedef unsigned short nwchar_t;
142 #else
143 typedef int nwchar_t;
144 #endif
146 typedef struct CString {
147 int size; /* size in bytes */
148 void *data; /* either 'char *' or 'nwchar_t *' */
149 int size_allocated;
150 void *data_allocated; /* if non NULL, data has been malloced */
151 } CString;
153 /* type definition */
154 typedef struct CType {
155 int t;
156 struct Sym *ref;
157 } CType;
159 /* constant value */
160 typedef union CValue {
161 long double ld;
162 double d;
163 float f;
164 int i;
165 unsigned int ui;
166 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
167 long long ll;
168 unsigned long long ull;
169 struct CString *cstr;
170 void *ptr;
171 int tab[1];
172 } CValue;
174 /* value on stack */
175 typedef struct SValue {
176 CType type; /* type */
177 unsigned short r; /* register + flags */
178 unsigned short r2; /* second register, used for 'long long'
179 type. If not used, set to VT_CONST */
180 CValue c; /* constant, if VT_CONST */
181 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
182 } SValue;
184 /* symbol management */
185 typedef struct Sym {
186 int v; /* symbol token */
187 int r; /* associated register */
188 int c; /* associated number */
189 CType type; /* associated type */
190 struct Sym *next; /* next related symbol */
191 struct Sym *prev; /* prev symbol in stack */
192 struct Sym *prev_tok; /* previous symbol for this token */
193 } Sym;
195 /* section definition */
196 /* XXX: use directly ELF structure for parameters ? */
197 /* special flag to indicate that the section should not be linked to
198 the other ones */
199 #define SHF_PRIVATE 0x80000000
201 typedef struct Section {
202 unsigned long data_offset; /* current data offset */
203 unsigned char *data; /* section data */
204 unsigned long data_allocated; /* used for realloc() handling */
205 int sh_name; /* elf section name (only used during output) */
206 int sh_num; /* elf section number */
207 int sh_type; /* elf section type */
208 int sh_flags; /* elf section flags */
209 int sh_info; /* elf section info */
210 int sh_addralign; /* elf section alignment */
211 int sh_entsize; /* elf entry size */
212 unsigned long sh_size; /* section size (only used during output) */
213 unsigned long sh_addr; /* address at which the section is relocated */
214 unsigned long sh_offset; /* file offset */
215 int nb_hashed_syms; /* used to resize the hash table */
216 struct Section *link; /* link to another section */
217 struct Section *reloc; /* corresponding section for relocation, if any */
218 struct Section *hash; /* hash table for symbols */
219 struct Section *next;
220 char name[1]; /* section name */
221 } Section;
223 typedef struct DLLReference {
224 int level;
225 char name[1];
226 } DLLReference;
228 /* GNUC attribute definition */
229 typedef struct AttributeDef {
230 int aligned;
231 int packed;
232 Section *section;
233 int func_attr; /* calling convention, exports, ... */
234 } AttributeDef;
236 /* -------------------------------------------------- */
237 /* gr: wrappers for casting sym->r for other purposes */
238 typedef struct {
239 unsigned
240 func_call : 8,
241 func_args : 8,
242 func_export : 1;
243 } func_attr_t;
245 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
246 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
247 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
248 #define INLINE_DEF(r) (*(int **)&(r))
249 /* -------------------------------------------------- */
251 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
252 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
253 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
255 /* stored in 'Sym.c' field */
256 #define FUNC_NEW 1 /* ansi function prototype */
257 #define FUNC_OLD 2 /* old function prototype */
258 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
260 /* stored in 'Sym.r' field */
261 #define FUNC_CDECL 0 /* standard c call */
262 #define FUNC_STDCALL 1 /* pascal c call */
263 #define FUNC_FASTCALL1 2 /* first param in %eax */
264 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
265 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
266 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
268 /* field 'Sym.t' for macros */
269 #define MACRO_OBJ 0 /* object like macro */
270 #define MACRO_FUNC 1 /* function like macro */
272 /* field 'Sym.r' for C labels */
273 #define LABEL_DEFINED 0 /* label is defined */
274 #define LABEL_FORWARD 1 /* label is forward defined */
275 #define LABEL_DECLARED 2 /* label is declared but never used */
277 /* type_decl() types */
278 #define TYPE_ABSTRACT 1 /* type without variable */
279 #define TYPE_DIRECT 2 /* type with variable */
281 #define IO_BUF_SIZE 8192
283 typedef struct BufferedFile {
284 uint8_t *buf_ptr;
285 uint8_t *buf_end;
286 int fd;
287 int line_num; /* current line number - here to simplify code */
288 int ifndef_macro; /* #ifndef macro / #endif search */
289 int ifndef_macro_saved; /* saved ifndef_macro */
290 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
291 char inc_type; /* type of include */
292 char inc_filename[512]; /* filename specified by the user */
293 char filename[1024]; /* current filename - here to simplify code */
294 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
295 } BufferedFile;
297 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
298 #define CH_EOF (-1) /* end of file */
300 /* parsing state (used to save parser state to reparse part of the
301 source several times) */
302 typedef struct ParseState {
303 int *macro_ptr;
304 int line_num;
305 int tok;
306 CValue tokc;
307 } ParseState;
309 /* used to record tokens */
310 typedef struct TokenString {
311 int *str;
312 int len;
313 int allocated_len;
314 int last_line_num;
315 } TokenString;
317 /* include file cache, used to find files faster and also to eliminate
318 inclusion if the include file is protected by #ifndef ... #endif */
319 typedef struct CachedInclude {
320 int ifndef_macro;
321 int hash_next; /* -1 if none */
322 char type; /* '"' or '>' to give include type */
323 char filename[1]; /* path specified in #include */
324 } CachedInclude;
326 #define CACHED_INCLUDES_HASH_SIZE 512
328 /* parser */
329 static struct BufferedFile *file;
330 static int ch, tok;
331 static CValue tokc;
332 static CString tokcstr; /* current parsed string, if any */
333 /* additional informations about token */
334 static int tok_flags;
335 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
336 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
337 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
338 #define TOK_FLAG_EOF 0x0008 /* end of file */
340 static int *macro_ptr, *macro_ptr_allocated;
341 static int *unget_saved_macro_ptr;
342 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
343 static int unget_buffer_enabled;
344 static int parse_flags;
345 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
346 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
347 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
348 token. line feed is also
349 returned at eof */
350 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
352 static Section *text_section, *data_section, *bss_section; /* predefined sections */
353 static Section *cur_text_section; /* current section where function code is
354 generated */
355 #ifdef CONFIG_TCC_ASM
356 static Section *last_text_section; /* to handle .previous asm directive */
357 #endif
358 /* bound check related sections */
359 static Section *bounds_section; /* contains global data bound description */
360 static Section *lbounds_section; /* contains local data bound description */
361 /* symbol sections */
362 static Section *symtab_section, *strtab_section;
364 /* debug sections */
365 static Section *stab_section, *stabstr_section;
367 /* loc : local variable index
368 ind : output code index
369 rsym: return symbol
370 anon_sym: anonymous symbol index
372 static int rsym, anon_sym, ind, loc;
373 /* expression generation modifiers */
374 static int const_wanted; /* true if constant wanted */
375 static int nocode_wanted; /* true if no code generation wanted for an expression */
376 static int global_expr; /* true if compound literals must be allocated
377 globally (used during initializers parsing */
378 static CType func_vt; /* current function return type (used by return
379 instruction) */
380 static int func_vc;
381 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
382 static int tok_ident;
383 static TokenSym **table_ident;
384 static TokenSym *hash_ident[TOK_HASH_SIZE];
385 static char token_buf[STRING_MAX_SIZE + 1];
386 static char *funcname;
387 static Sym *global_stack, *local_stack;
388 static Sym *define_stack;
389 static Sym *global_label_stack, *local_label_stack;
390 /* symbol allocator */
391 #define SYM_POOL_NB (8192 / sizeof(Sym))
392 static Sym *sym_free_first;
394 static SValue vstack[VSTACK_SIZE], *vtop;
395 /* some predefined types */
396 static CType char_pointer_type, func_old_type, int_type;
397 /* true if isid(c) || isnum(c) */
398 static unsigned char isidnum_table[256];
400 /* display some information during compilation */
401 static int verbose = 0;
403 /* compile with debug symbol (and use them if error during execution) */
404 static int do_debug = 0;
406 /* compile with built-in memory and bounds checker */
407 static int do_bounds_check = 0;
409 /* display benchmark infos */
410 #if !defined(LIBTCC)
411 static int do_bench = 0;
412 #endif
413 static int total_lines;
414 static int total_bytes;
416 /* use GNU C extensions */
417 static int gnu_ext = 1;
419 /* use Tiny C extensions */
420 static int tcc_ext = 1;
422 /* max number of callers shown if error */
423 static int num_callers = 6;
424 static const char **rt_bound_error_msg;
426 /* XXX: get rid of this ASAP */
427 static struct TCCState *tcc_state;
429 /* give the path of the tcc libraries */
430 static const char *tcc_lib_path = CONFIG_TCCDIR;
432 struct TCCState {
433 int output_type;
435 BufferedFile **include_stack_ptr;
436 int *ifdef_stack_ptr;
438 /* include file handling */
439 char **include_paths;
440 int nb_include_paths;
441 char **sysinclude_paths;
442 int nb_sysinclude_paths;
443 CachedInclude **cached_includes;
444 int nb_cached_includes;
446 char **library_paths;
447 int nb_library_paths;
449 /* array of all loaded dlls (including those referenced by loaded
450 dlls) */
451 DLLReference **loaded_dlls;
452 int nb_loaded_dlls;
454 /* sections */
455 Section **sections;
456 int nb_sections; /* number of sections, including first dummy section */
458 /* got handling */
459 Section *got;
460 Section *plt;
461 unsigned long *got_offsets;
462 int nb_got_offsets;
463 /* give the correspondance from symtab indexes to dynsym indexes */
464 int *symtab_to_dynsym;
466 /* temporary dynamic symbol sections (for dll loading) */
467 Section *dynsymtab_section;
468 /* exported dynamic symbol section */
469 Section *dynsym;
471 int nostdinc; /* if true, no standard headers are added */
472 int nostdlib; /* if true, no standard libraries are added */
474 int nocommon; /* if true, do not use common symbols for .bss data */
476 /* if true, static linking is performed */
477 int static_link;
479 /* soname as specified on the command line (-soname) */
480 const char *soname;
482 /* if true, all symbols are exported */
483 int rdynamic;
485 /* if true, only link in referenced objects from archive */
486 int alacarte_link;
488 /* address of text section */
489 unsigned long text_addr;
490 int has_text_addr;
492 /* output format, see TCC_OUTPUT_FORMAT_xxx */
493 int output_format;
495 /* C language options */
496 int char_is_unsigned;
497 int leading_underscore;
499 /* warning switches */
500 int warn_write_strings;
501 int warn_unsupported;
502 int warn_error;
503 int warn_none;
504 int warn_implicit_function_declaration;
506 /* error handling */
507 void *error_opaque;
508 void (*error_func)(void *opaque, const char *msg);
509 int error_set_jmp_enabled;
510 jmp_buf error_jmp_buf;
511 int nb_errors;
513 /* tiny assembler state */
514 Sym *asm_labels;
516 /* see include_stack_ptr */
517 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
519 /* see ifdef_stack_ptr */
520 int ifdef_stack[IFDEF_STACK_SIZE];
522 /* see cached_includes */
523 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
525 /* pack stack */
526 int pack_stack[PACK_STACK_SIZE];
527 int *pack_stack_ptr;
529 /* output file for preprocessing */
530 FILE *outfile;
533 /* The current value can be: */
534 #define VT_VALMASK 0x00ff
535 #define VT_CONST 0x00f0 /* constant in vc
536 (must be first non register value) */
537 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
538 #define VT_LOCAL 0x00f2 /* offset on stack */
539 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
540 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
541 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
542 #define VT_LVAL 0x0100 /* var is an lvalue */
543 #define VT_SYM 0x0200 /* a symbol value is added */
544 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
545 char/short stored in integer registers) */
546 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
547 dereferencing value */
548 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
549 bounding function call point is in vc */
550 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
551 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
552 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
553 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
555 /* types */
556 #define VT_INT 0 /* integer type */
557 #define VT_BYTE 1 /* signed byte type */
558 #define VT_SHORT 2 /* short type */
559 #define VT_VOID 3 /* void type */
560 #define VT_PTR 4 /* pointer */
561 #define VT_ENUM 5 /* enum definition */
562 #define VT_FUNC 6 /* function type */
563 #define VT_STRUCT 7 /* struct/union definition */
564 #define VT_FLOAT 8 /* IEEE float */
565 #define VT_DOUBLE 9 /* IEEE double */
566 #define VT_LDOUBLE 10 /* IEEE long double */
567 #define VT_BOOL 11 /* ISOC99 boolean type */
568 #define VT_LLONG 12 /* 64 bit integer */
569 #define VT_LONG 13 /* long integer (NEVER USED as type, only
570 during parsing) */
571 #define VT_BTYPE 0x000f /* mask for basic type */
572 #define VT_UNSIGNED 0x0010 /* unsigned type */
573 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
574 #define VT_BITFIELD 0x0040 /* bitfield modifier */
575 #define VT_CONSTANT 0x0800 /* const modifier */
576 #define VT_VOLATILE 0x1000 /* volatile modifier */
577 #define VT_SIGNED 0x2000 /* signed type */
579 /* storage */
580 #define VT_EXTERN 0x00000080 /* extern definition */
581 #define VT_STATIC 0x00000100 /* static variable */
582 #define VT_TYPEDEF 0x00000200 /* typedef definition */
583 #define VT_INLINE 0x00000400 /* inline definition */
585 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
587 /* type mask (except storage) */
588 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
589 #define VT_TYPE (~(VT_STORAGE))
591 /* token values */
593 /* warning: the following compare tokens depend on i386 asm code */
594 #define TOK_ULT 0x92
595 #define TOK_UGE 0x93
596 #define TOK_EQ 0x94
597 #define TOK_NE 0x95
598 #define TOK_ULE 0x96
599 #define TOK_UGT 0x97
600 #define TOK_Nset 0x98
601 #define TOK_Nclear 0x99
602 #define TOK_LT 0x9c
603 #define TOK_GE 0x9d
604 #define TOK_LE 0x9e
605 #define TOK_GT 0x9f
607 #define TOK_LAND 0xa0
608 #define TOK_LOR 0xa1
610 #define TOK_DEC 0xa2
611 #define TOK_MID 0xa3 /* inc/dec, to void constant */
612 #define TOK_INC 0xa4
613 #define TOK_UDIV 0xb0 /* unsigned division */
614 #define TOK_UMOD 0xb1 /* unsigned modulo */
615 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
616 #define TOK_CINT 0xb3 /* number in tokc */
617 #define TOK_CCHAR 0xb4 /* char constant in tokc */
618 #define TOK_STR 0xb5 /* pointer to string in tokc */
619 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
620 #define TOK_LCHAR 0xb7
621 #define TOK_LSTR 0xb8
622 #define TOK_CFLOAT 0xb9 /* float constant */
623 #define TOK_LINENUM 0xba /* line number info */
624 #define TOK_CDOUBLE 0xc0 /* double constant */
625 #define TOK_CLDOUBLE 0xc1 /* long double constant */
626 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
627 #define TOK_ADDC1 0xc3 /* add with carry generation */
628 #define TOK_ADDC2 0xc4 /* add with carry use */
629 #define TOK_SUBC1 0xc5 /* add with carry generation */
630 #define TOK_SUBC2 0xc6 /* add with carry use */
631 #define TOK_CUINT 0xc8 /* unsigned int constant */
632 #define TOK_CLLONG 0xc9 /* long long constant */
633 #define TOK_CULLONG 0xca /* unsigned long long constant */
634 #define TOK_ARROW 0xcb
635 #define TOK_DOTS 0xcc /* three dots */
636 #define TOK_SHR 0xcd /* unsigned shift right */
637 #define TOK_PPNUM 0xce /* preprocessor number */
639 #define TOK_SHL 0x01 /* shift left */
640 #define TOK_SAR 0x02 /* signed shift right */
642 /* assignement operators : normal operator or 0x80 */
643 #define TOK_A_MOD 0xa5
644 #define TOK_A_AND 0xa6
645 #define TOK_A_MUL 0xaa
646 #define TOK_A_ADD 0xab
647 #define TOK_A_SUB 0xad
648 #define TOK_A_DIV 0xaf
649 #define TOK_A_XOR 0xde
650 #define TOK_A_OR 0xfc
651 #define TOK_A_SHL 0x81
652 #define TOK_A_SAR 0x82
654 #ifndef offsetof
655 #define offsetof(type, field) ((size_t) &((type *)0)->field)
656 #endif
658 #ifndef countof
659 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
660 #endif
662 /* WARNING: the content of this string encodes token numbers */
663 static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
665 #define TOK_EOF (-1) /* end of file */
666 #define TOK_LINEFEED 10 /* line feed */
668 /* all identificators and strings have token above that */
669 #define TOK_IDENT 256
671 /* only used for i386 asm opcodes definitions */
672 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
674 #define DEF_BWL(x) \
675 DEF(TOK_ASM_ ## x ## b, #x "b") \
676 DEF(TOK_ASM_ ## x ## w, #x "w") \
677 DEF(TOK_ASM_ ## x ## l, #x "l") \
678 DEF(TOK_ASM_ ## x, #x)
680 #define DEF_WL(x) \
681 DEF(TOK_ASM_ ## x ## w, #x "w") \
682 DEF(TOK_ASM_ ## x ## l, #x "l") \
683 DEF(TOK_ASM_ ## x, #x)
685 #define DEF_FP1(x) \
686 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
687 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
688 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
689 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
691 #define DEF_FP(x) \
692 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
693 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
694 DEF_FP1(x)
696 #define DEF_ASMTEST(x) \
697 DEF_ASM(x ## o) \
698 DEF_ASM(x ## no) \
699 DEF_ASM(x ## b) \
700 DEF_ASM(x ## c) \
701 DEF_ASM(x ## nae) \
702 DEF_ASM(x ## nb) \
703 DEF_ASM(x ## nc) \
704 DEF_ASM(x ## ae) \
705 DEF_ASM(x ## e) \
706 DEF_ASM(x ## z) \
707 DEF_ASM(x ## ne) \
708 DEF_ASM(x ## nz) \
709 DEF_ASM(x ## be) \
710 DEF_ASM(x ## na) \
711 DEF_ASM(x ## nbe) \
712 DEF_ASM(x ## a) \
713 DEF_ASM(x ## s) \
714 DEF_ASM(x ## ns) \
715 DEF_ASM(x ## p) \
716 DEF_ASM(x ## pe) \
717 DEF_ASM(x ## np) \
718 DEF_ASM(x ## po) \
719 DEF_ASM(x ## l) \
720 DEF_ASM(x ## nge) \
721 DEF_ASM(x ## nl) \
722 DEF_ASM(x ## ge) \
723 DEF_ASM(x ## le) \
724 DEF_ASM(x ## ng) \
725 DEF_ASM(x ## nle) \
726 DEF_ASM(x ## g)
728 #define TOK_ASM_int TOK_INT
730 enum tcc_token {
731 TOK_LAST = TOK_IDENT - 1,
732 #define DEF(id, str) id,
733 #include "tcctok.h"
734 #undef DEF
737 static const char tcc_keywords[] =
738 #define DEF(id, str) str "\0"
739 #include "tcctok.h"
740 #undef DEF
743 #define TOK_UIDENT TOK_DEFINE
745 #ifdef _WIN32
746 #define snprintf _snprintf
747 #define vsnprintf _vsnprintf
748 #ifndef __GNUC__
749 #define strtold (long double)strtod
750 #define strtof (float)strtod
751 #define strtoll (long long)strtol
752 #endif
753 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
754 || defined(__OpenBSD__)
755 /* currently incorrect */
756 long double strtold(const char *nptr, char **endptr)
758 return (long double)strtod(nptr, endptr);
760 float strtof(const char *nptr, char **endptr)
762 return (float)strtod(nptr, endptr);
764 #else
765 /* XXX: need to define this to use them in non ISOC99 context */
766 extern float strtof (const char *__nptr, char **__endptr);
767 extern long double strtold (const char *__nptr, char **__endptr);
768 #endif
770 static char *pstrcpy(char *buf, int buf_size, const char *s);
771 static char *pstrcat(char *buf, int buf_size, const char *s);
772 static char *tcc_basename(const char *name);
773 static char *tcc_fileextension (const char *p);
775 static void next(void);
776 static void next_nomacro(void);
777 static void parse_expr_type(CType *type);
778 static void expr_type(CType *type);
779 static void unary_type(CType *type);
780 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
781 int case_reg, int is_expr);
782 static int expr_const(void);
783 static void expr_eq(void);
784 static void gexpr(void);
785 static void gen_inline_functions(void);
786 static void decl(int l);
787 static void decl_initializer(CType *type, Section *sec, unsigned long c,
788 int first, int size_only);
789 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
790 int has_init, int v, int scope);
791 int gv(int rc);
792 void gv2(int rc1, int rc2);
793 void move_reg(int r, int s);
794 void save_regs(int n);
795 void save_reg(int r);
796 void vpop(void);
797 void vswap(void);
798 void vdup(void);
799 int get_reg(int rc);
800 int get_reg_ex(int rc,int rc2);
802 struct macro_level {
803 struct macro_level *prev;
804 int *p;
807 static void macro_subst(TokenString *tok_str, Sym **nested_list,
808 const int *macro_str, struct macro_level **can_read_stream);
809 void gen_op(int op);
810 void force_charshort_cast(int t);
811 static void gen_cast(CType *type);
812 void vstore(void);
813 static Sym *sym_find(int v);
814 static Sym *sym_push(int v, CType *type, int r, int c);
816 /* type handling */
817 static int type_size(CType *type, int *a);
818 static inline CType *pointed_type(CType *type);
819 static int pointed_size(CType *type);
820 static int lvalue_type(int t);
821 static int parse_btype(CType *type, AttributeDef *ad);
822 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
823 static int compare_types(CType *type1, CType *type2, int unqualified);
824 static int is_compatible_types(CType *type1, CType *type2);
825 static int is_compatible_parameter_types(CType *type1, CType *type2);
827 int ieee_finite(double d);
828 void error(const char *fmt, ...);
829 void vpushi(int v);
830 void vrott(int n);
831 void vnrott(int n);
832 void lexpand_nr(void);
833 static void vpush_global_sym(CType *type, int v);
834 void vset(CType *type, int r, int v);
835 void type_to_str(char *buf, int buf_size,
836 CType *type, const char *varstr);
837 char *get_tok_str(int v, CValue *cv);
838 static Sym *get_sym_ref(CType *type, Section *sec,
839 unsigned long offset, unsigned long size);
840 static Sym *external_global_sym(int v, CType *type, int r);
842 /* section generation */
843 static void section_realloc(Section *sec, unsigned long new_size);
844 static void *section_ptr_add(Section *sec, unsigned long size);
845 static void put_extern_sym(Sym *sym, Section *section,
846 unsigned long value, unsigned long size);
847 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
848 static int put_elf_str(Section *s, const char *sym);
849 static int put_elf_sym(Section *s,
850 unsigned long value, unsigned long size,
851 int info, int other, int shndx, const char *name);
852 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
853 int info, int other, int sh_num, const char *name);
854 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
855 int type, int symbol);
856 static void put_stabs(const char *str, int type, int other, int desc,
857 unsigned long value);
858 static void put_stabs_r(const char *str, int type, int other, int desc,
859 unsigned long value, Section *sec, int sym_index);
860 static void put_stabn(int type, int other, int desc, int value);
861 static void put_stabd(int type, int other, int desc);
862 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
864 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
865 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
866 #define AFF_PREPROCESS 0x0004 /* preprocess file */
867 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
869 /* tcccoff.c */
870 int tcc_output_coff(TCCState *s1, FILE *f);
872 /* tccpe.c */
873 void *resolve_sym(TCCState *s1, const char *sym, int type);
874 int pe_load_def_file(struct TCCState *s1, int fd);
875 int pe_test_res_file(void *v, int size);
876 int pe_load_res_file(struct TCCState *s1, int fd);
877 void pe_add_runtime(struct TCCState *s1);
878 void pe_guess_outfile(char *objfilename, int output_type);
879 int pe_output_file(struct TCCState *s1, const char *filename);
881 /* tccasm.c */
883 #ifdef CONFIG_TCC_ASM
885 typedef struct ExprValue {
886 uint32_t v;
887 Sym *sym;
888 } ExprValue;
890 #define MAX_ASM_OPERANDS 30
892 typedef struct ASMOperand {
893 int id; /* GCC 3 optionnal identifier (0 if number only supported */
894 char *constraint;
895 char asm_str[16]; /* computed asm string for operand */
896 SValue *vt; /* C value of the expression */
897 int ref_index; /* if >= 0, gives reference to a output constraint */
898 int input_index; /* if >= 0, gives reference to an input constraint */
899 int priority; /* priority, used to assign registers */
900 int reg; /* if >= 0, register number used for this operand */
901 int is_llong; /* true if double register value */
902 int is_memory; /* true if memory operand */
903 int is_rw; /* for '+' modifier */
904 } ASMOperand;
906 static void asm_expr(TCCState *s1, ExprValue *pe);
907 static int asm_int_expr(TCCState *s1);
908 static int find_constraint(ASMOperand *operands, int nb_operands,
909 const char *name, const char **pp);
911 static int tcc_assemble(TCCState *s1, int do_preprocess);
913 #endif
915 static void asm_instr(void);
916 static void asm_global_instr(void);
918 /* true if float/double/long double type */
919 static inline int is_float(int t)
921 int bt;
922 bt = t & VT_BTYPE;
923 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
926 #ifdef TCC_TARGET_I386
927 #include "i386-gen.c"
928 #endif
930 #ifdef TCC_TARGET_ARM
931 #include "arm-gen.c"
932 #endif
934 #ifdef TCC_TARGET_C67
935 #include "c67-gen.c"
936 #endif
938 #ifdef CONFIG_TCC_STATIC
940 #define RTLD_LAZY 0x001
941 #define RTLD_NOW 0x002
942 #define RTLD_GLOBAL 0x100
943 #define RTLD_DEFAULT NULL
945 /* dummy function for profiling */
946 void *dlopen(const char *filename, int flag)
948 return NULL;
951 const char *dlerror(void)
953 return "error";
956 typedef struct TCCSyms {
957 char *str;
958 void *ptr;
959 } TCCSyms;
961 #define TCCSYM(a) { #a, &a, },
963 /* add the symbol you want here if no dynamic linking is done */
964 static TCCSyms tcc_syms[] = {
965 #if !defined(CONFIG_TCCBOOT)
966 TCCSYM(printf)
967 TCCSYM(fprintf)
968 TCCSYM(fopen)
969 TCCSYM(fclose)
970 #endif
971 { NULL, NULL },
974 void *resolve_sym(TCCState *s1, const char *symbol, int type)
976 TCCSyms *p;
977 p = tcc_syms;
978 while (p->str != NULL) {
979 if (!strcmp(p->str, symbol))
980 return p->ptr;
981 p++;
983 return NULL;
986 #elif !defined(_WIN32)
988 #include <dlfcn.h>
990 void *resolve_sym(TCCState *s1, const char *sym, int type)
992 return dlsym(RTLD_DEFAULT, sym);
995 #endif
997 /********************************************************/
999 /* we use our own 'finite' function to avoid potential problems with
1000 non standard math libs */
1001 /* XXX: endianness dependent */
1002 int ieee_finite(double d)
1004 int *p = (int *)&d;
1005 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
1008 /* copy a string and truncate it. */
1009 static char *pstrcpy(char *buf, int buf_size, const char *s)
1011 char *q, *q_end;
1012 int c;
1014 if (buf_size > 0) {
1015 q = buf;
1016 q_end = buf + buf_size - 1;
1017 while (q < q_end) {
1018 c = *s++;
1019 if (c == '\0')
1020 break;
1021 *q++ = c;
1023 *q = '\0';
1025 return buf;
1028 /* strcat and truncate. */
1029 static char *pstrcat(char *buf, int buf_size, const char *s)
1031 int len;
1032 len = strlen(buf);
1033 if (len < buf_size)
1034 pstrcpy(buf + len, buf_size - len, s);
1035 return buf;
1038 #ifndef LIBTCC
1039 static int strstart(const char *str, const char *val, const char **ptr)
1041 const char *p, *q;
1042 p = str;
1043 q = val;
1044 while (*q != '\0') {
1045 if (*p != *q)
1046 return 0;
1047 p++;
1048 q++;
1050 if (ptr)
1051 *ptr = p;
1052 return 1;
1054 #endif
1056 /* extract the basename of a file */
1057 static char *tcc_basename(const char *name)
1059 char *p = strchr(name, 0);
1060 while (p > name
1061 && p[-1] != '/'
1062 #ifdef _WIN32
1063 && p[-1] != '\\'
1064 #endif
1066 --p;
1067 return p;
1070 static char *tcc_fileextension (const char *name)
1072 char *b = tcc_basename(name);
1073 char *e = strrchr(b, '.');
1074 return e ? e : strchr(b, 0);
1077 #ifdef _WIN32
1078 char *normalize_slashes(char *path)
1080 char *p;
1081 for (p = path; *p; ++p)
1082 if (*p == '\\')
1083 *p = '/';
1084 return path;
1087 char *w32_tcc_lib_path(void)
1089 /* on win32, we suppose the lib and includes are at the location
1090 of 'tcc.exe' */
1091 char path[1024], *p;
1092 GetModuleFileNameA(NULL, path, sizeof path);
1093 p = tcc_basename(normalize_slashes(strlwr(path)));
1094 if (p - 5 > path && 0 == strncmp(p - 5, "/bin/", 5))
1095 p -= 5;
1096 else if (p > path)
1097 p--;
1098 *p = 0;
1099 return strdup(path);
1101 #endif
1103 void set_pages_executable(void *ptr, unsigned long length)
1105 #ifdef _WIN32
1106 unsigned long old_protect;
1107 VirtualProtect(ptr, length, PAGE_EXECUTE_READWRITE, &old_protect);
1108 #else
1109 unsigned long start, end;
1110 start = (unsigned long)ptr & ~(PAGESIZE - 1);
1111 end = (unsigned long)ptr + length;
1112 end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
1113 mprotect((void *)start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC);
1114 #endif
1117 /* memory management */
1118 #ifdef MEM_DEBUG
1119 int mem_cur_size;
1120 int mem_max_size;
1121 #endif
1123 static inline void tcc_free(void *ptr)
1125 #ifdef MEM_DEBUG
1126 mem_cur_size -= malloc_usable_size(ptr);
1127 #endif
1128 free(ptr);
1131 static void *tcc_malloc(unsigned long size)
1133 void *ptr;
1134 ptr = malloc(size);
1135 if (!ptr && size)
1136 error("memory full");
1137 #ifdef MEM_DEBUG
1138 mem_cur_size += malloc_usable_size(ptr);
1139 if (mem_cur_size > mem_max_size)
1140 mem_max_size = mem_cur_size;
1141 #endif
1142 return ptr;
1145 static void *tcc_mallocz(unsigned long size)
1147 void *ptr;
1148 ptr = tcc_malloc(size);
1149 memset(ptr, 0, size);
1150 return ptr;
1153 static inline void *tcc_realloc(void *ptr, unsigned long size)
1155 void *ptr1;
1156 #ifdef MEM_DEBUG
1157 mem_cur_size -= malloc_usable_size(ptr);
1158 #endif
1159 ptr1 = realloc(ptr, size);
1160 #ifdef MEM_DEBUG
1161 /* NOTE: count not correct if alloc error, but not critical */
1162 mem_cur_size += malloc_usable_size(ptr1);
1163 if (mem_cur_size > mem_max_size)
1164 mem_max_size = mem_cur_size;
1165 #endif
1166 return ptr1;
1169 static char *tcc_strdup(const char *str)
1171 char *ptr;
1172 ptr = tcc_malloc(strlen(str) + 1);
1173 strcpy(ptr, str);
1174 return ptr;
1177 #define free(p) use_tcc_free(p)
1178 #define malloc(s) use_tcc_malloc(s)
1179 #define realloc(p, s) use_tcc_realloc(p, s)
1181 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
1183 int nb, nb_alloc;
1184 void **pp;
1186 nb = *nb_ptr;
1187 pp = *ptab;
1188 /* every power of two we double array size */
1189 if ((nb & (nb - 1)) == 0) {
1190 if (!nb)
1191 nb_alloc = 1;
1192 else
1193 nb_alloc = nb * 2;
1194 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
1195 if (!pp)
1196 error("memory full");
1197 *ptab = pp;
1199 pp[nb++] = data;
1200 *nb_ptr = nb;
1203 static void dynarray_reset(void *pp, int *n)
1205 void **p;
1206 for (p = *(void***)pp; *n; ++p, --*n)
1207 if (*p)
1208 tcc_free(*p);
1209 tcc_free(*(void**)pp);
1210 *(void**)pp = NULL;
1213 /* symbol allocator */
1214 static Sym *__sym_malloc(void)
1216 Sym *sym_pool, *sym, *last_sym;
1217 int i;
1219 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
1221 last_sym = sym_free_first;
1222 sym = sym_pool;
1223 for(i = 0; i < SYM_POOL_NB; i++) {
1224 sym->next = last_sym;
1225 last_sym = sym;
1226 sym++;
1228 sym_free_first = last_sym;
1229 return last_sym;
1232 static inline Sym *sym_malloc(void)
1234 Sym *sym;
1235 sym = sym_free_first;
1236 if (!sym)
1237 sym = __sym_malloc();
1238 sym_free_first = sym->next;
1239 return sym;
1242 static inline void sym_free(Sym *sym)
1244 sym->next = sym_free_first;
1245 sym_free_first = sym;
1248 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
1250 Section *sec;
1252 sec = tcc_mallocz(sizeof(Section) + strlen(name));
1253 strcpy(sec->name, name);
1254 sec->sh_type = sh_type;
1255 sec->sh_flags = sh_flags;
1256 switch(sh_type) {
1257 case SHT_HASH:
1258 case SHT_REL:
1259 case SHT_DYNSYM:
1260 case SHT_SYMTAB:
1261 case SHT_DYNAMIC:
1262 sec->sh_addralign = 4;
1263 break;
1264 case SHT_STRTAB:
1265 sec->sh_addralign = 1;
1266 break;
1267 default:
1268 sec->sh_addralign = 32; /* default conservative alignment */
1269 break;
1272 /* only add section if not private */
1273 if (!(sh_flags & SHF_PRIVATE)) {
1274 sec->sh_num = s1->nb_sections;
1275 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
1277 return sec;
1280 static void free_section(Section *s)
1282 tcc_free(s->data);
1283 tcc_free(s);
1286 /* realloc section and set its content to zero */
1287 static void section_realloc(Section *sec, unsigned long new_size)
1289 unsigned long size;
1290 unsigned char *data;
1292 size = sec->data_allocated;
1293 if (size == 0)
1294 size = 1;
1295 while (size < new_size)
1296 size = size * 2;
1297 data = tcc_realloc(sec->data, size);
1298 if (!data)
1299 error("memory full");
1300 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
1301 sec->data = data;
1302 sec->data_allocated = size;
1305 /* reserve at least 'size' bytes in section 'sec' from
1306 sec->data_offset. */
1307 static void *section_ptr_add(Section *sec, unsigned long size)
1309 unsigned long offset, offset1;
1311 offset = sec->data_offset;
1312 offset1 = offset + size;
1313 if (offset1 > sec->data_allocated)
1314 section_realloc(sec, offset1);
1315 sec->data_offset = offset1;
1316 return sec->data + offset;
1319 /* return a reference to a section, and create it if it does not
1320 exists */
1321 Section *find_section(TCCState *s1, const char *name)
1323 Section *sec;
1324 int i;
1325 for(i = 1; i < s1->nb_sections; i++) {
1326 sec = s1->sections[i];
1327 if (!strcmp(name, sec->name))
1328 return sec;
1330 /* sections are created as PROGBITS */
1331 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1334 #define SECTION_ABS ((void *)1)
1336 /* update sym->c so that it points to an external symbol in section
1337 'section' with value 'value' */
1338 static void put_extern_sym2(Sym *sym, Section *section,
1339 unsigned long value, unsigned long size,
1340 int can_add_underscore)
1342 int sym_type, sym_bind, sh_num, info, other, attr;
1343 Elf32_Sym *esym;
1344 const char *name;
1345 char buf1[256];
1347 if (section == NULL)
1348 sh_num = SHN_UNDEF;
1349 else if (section == SECTION_ABS)
1350 sh_num = SHN_ABS;
1351 else
1352 sh_num = section->sh_num;
1354 other = attr = 0;
1356 if ((sym->type.t & VT_BTYPE) == VT_FUNC) {
1357 sym_type = STT_FUNC;
1358 #ifdef TCC_TARGET_PE
1359 if (sym->type.ref)
1360 attr = sym->type.ref->r;
1361 if (FUNC_EXPORT(attr))
1362 other |= 1;
1363 if (FUNC_CALL(attr) == FUNC_STDCALL)
1364 other |= 2;
1365 #endif
1366 } else {
1367 sym_type = STT_OBJECT;
1370 if (sym->type.t & VT_STATIC)
1371 sym_bind = STB_LOCAL;
1372 else
1373 sym_bind = STB_GLOBAL;
1375 if (!sym->c) {
1376 name = get_tok_str(sym->v, NULL);
1377 #ifdef CONFIG_TCC_BCHECK
1378 if (do_bounds_check) {
1379 char buf[32];
1381 /* XXX: avoid doing that for statics ? */
1382 /* if bound checking is activated, we change some function
1383 names by adding the "__bound" prefix */
1384 switch(sym->v) {
1385 #if 0
1386 /* XXX: we rely only on malloc hooks */
1387 case TOK_malloc:
1388 case TOK_free:
1389 case TOK_realloc:
1390 case TOK_memalign:
1391 case TOK_calloc:
1392 #endif
1393 case TOK_memcpy:
1394 case TOK_memmove:
1395 case TOK_memset:
1396 case TOK_strlen:
1397 case TOK_strcpy:
1398 case TOK__alloca:
1399 strcpy(buf, "__bound_");
1400 strcat(buf, name);
1401 name = buf;
1402 break;
1405 #endif
1407 #ifdef TCC_TARGET_PE
1408 if ((other & 2) && can_add_underscore) {
1409 sprintf(buf1, "_%s@%d", name, FUNC_ARGS(attr));
1410 name = buf1;
1411 } else
1412 #endif
1413 if (tcc_state->leading_underscore && can_add_underscore) {
1414 buf1[0] = '_';
1415 pstrcpy(buf1 + 1, sizeof(buf1) - 1, name);
1416 name = buf1;
1418 info = ELF32_ST_INFO(sym_bind, sym_type);
1419 sym->c = add_elf_sym(symtab_section, value, size, info, other, sh_num, name);
1420 } else {
1421 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1422 esym->st_value = value;
1423 esym->st_size = size;
1424 esym->st_shndx = sh_num;
1425 esym->st_other |= other;
1429 static void put_extern_sym(Sym *sym, Section *section,
1430 unsigned long value, unsigned long size)
1432 put_extern_sym2(sym, section, value, size, 1);
1435 /* add a new relocation entry to symbol 'sym' in section 's' */
1436 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1438 if (!sym->c)
1439 put_extern_sym(sym, NULL, 0, 0);
1440 /* now we can add ELF relocation info */
1441 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1444 static inline int isid(int c)
1446 return (c >= 'a' && c <= 'z') ||
1447 (c >= 'A' && c <= 'Z') ||
1448 c == '_';
1451 static inline int isnum(int c)
1453 return c >= '0' && c <= '9';
1456 static inline int isoct(int c)
1458 return c >= '0' && c <= '7';
1461 static inline int toup(int c)
1463 if (c >= 'a' && c <= 'z')
1464 return c - 'a' + 'A';
1465 else
1466 return c;
1469 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1471 int len;
1472 len = strlen(buf);
1473 vsnprintf(buf + len, buf_size - len, fmt, ap);
1476 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1478 va_list ap;
1479 va_start(ap, fmt);
1480 strcat_vprintf(buf, buf_size, fmt, ap);
1481 va_end(ap);
1484 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1486 char buf[2048];
1487 BufferedFile **f;
1489 buf[0] = '\0';
1490 if (file) {
1491 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1492 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1493 (*f)->filename, (*f)->line_num);
1494 if (file->line_num > 0) {
1495 strcat_printf(buf, sizeof(buf),
1496 "%s:%d: ", file->filename, file->line_num);
1497 } else {
1498 strcat_printf(buf, sizeof(buf),
1499 "%s: ", file->filename);
1501 } else {
1502 strcat_printf(buf, sizeof(buf),
1503 "tcc: ");
1505 if (is_warning)
1506 strcat_printf(buf, sizeof(buf), "warning: ");
1507 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1509 if (!s1->error_func) {
1510 /* default case: stderr */
1511 fprintf(stderr, "%s\n", buf);
1512 } else {
1513 s1->error_func(s1->error_opaque, buf);
1515 if (!is_warning || s1->warn_error)
1516 s1->nb_errors++;
1519 #ifdef LIBTCC
1520 void tcc_set_error_func(TCCState *s, void *error_opaque,
1521 void (*error_func)(void *opaque, const char *msg))
1523 s->error_opaque = error_opaque;
1524 s->error_func = error_func;
1526 #endif
1528 /* error without aborting current compilation */
1529 void error_noabort(const char *fmt, ...)
1531 TCCState *s1 = tcc_state;
1532 va_list ap;
1534 va_start(ap, fmt);
1535 error1(s1, 0, fmt, ap);
1536 va_end(ap);
1539 void error(const char *fmt, ...)
1541 TCCState *s1 = tcc_state;
1542 va_list ap;
1544 va_start(ap, fmt);
1545 error1(s1, 0, fmt, ap);
1546 va_end(ap);
1547 /* better than nothing: in some cases, we accept to handle errors */
1548 if (s1->error_set_jmp_enabled) {
1549 longjmp(s1->error_jmp_buf, 1);
1550 } else {
1551 /* XXX: eliminate this someday */
1552 exit(1);
1556 void expect(const char *msg)
1558 error("%s expected", msg);
1561 void warning(const char *fmt, ...)
1563 TCCState *s1 = tcc_state;
1564 va_list ap;
1566 if (s1->warn_none)
1567 return;
1569 va_start(ap, fmt);
1570 error1(s1, 1, fmt, ap);
1571 va_end(ap);
1574 void skip(int c)
1576 if (tok != c)
1577 error("'%c' expected", c);
1578 next();
1581 static void test_lvalue(void)
1583 if (!(vtop->r & VT_LVAL))
1584 expect("lvalue");
1587 /* allocate a new token */
1588 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1590 TokenSym *ts, **ptable;
1591 int i;
1593 if (tok_ident >= SYM_FIRST_ANOM)
1594 error("memory full");
1596 /* expand token table if needed */
1597 i = tok_ident - TOK_IDENT;
1598 if ((i % TOK_ALLOC_INCR) == 0) {
1599 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1600 if (!ptable)
1601 error("memory full");
1602 table_ident = ptable;
1605 ts = tcc_malloc(sizeof(TokenSym) + len);
1606 table_ident[i] = ts;
1607 ts->tok = tok_ident++;
1608 ts->sym_define = NULL;
1609 ts->sym_label = NULL;
1610 ts->sym_struct = NULL;
1611 ts->sym_identifier = NULL;
1612 ts->len = len;
1613 ts->hash_next = NULL;
1614 memcpy(ts->str, str, len);
1615 ts->str[len] = '\0';
1616 *pts = ts;
1617 return ts;
1620 #define TOK_HASH_INIT 1
1621 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1623 /* find a token and add it if not found */
1624 static TokenSym *tok_alloc(const char *str, int len)
1626 TokenSym *ts, **pts;
1627 int i;
1628 unsigned int h;
1630 h = TOK_HASH_INIT;
1631 for(i=0;i<len;i++)
1632 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1633 h &= (TOK_HASH_SIZE - 1);
1635 pts = &hash_ident[h];
1636 for(;;) {
1637 ts = *pts;
1638 if (!ts)
1639 break;
1640 if (ts->len == len && !memcmp(ts->str, str, len))
1641 return ts;
1642 pts = &(ts->hash_next);
1644 return tok_alloc_new(pts, str, len);
1647 /* CString handling */
1649 static void cstr_realloc(CString *cstr, int new_size)
1651 int size;
1652 void *data;
1654 size = cstr->size_allocated;
1655 if (size == 0)
1656 size = 8; /* no need to allocate a too small first string */
1657 while (size < new_size)
1658 size = size * 2;
1659 data = tcc_realloc(cstr->data_allocated, size);
1660 if (!data)
1661 error("memory full");
1662 cstr->data_allocated = data;
1663 cstr->size_allocated = size;
1664 cstr->data = data;
1667 /* add a byte */
1668 static inline void cstr_ccat(CString *cstr, int ch)
1670 int size;
1671 size = cstr->size + 1;
1672 if (size > cstr->size_allocated)
1673 cstr_realloc(cstr, size);
1674 ((unsigned char *)cstr->data)[size - 1] = ch;
1675 cstr->size = size;
1678 static void cstr_cat(CString *cstr, const char *str)
1680 int c;
1681 for(;;) {
1682 c = *str;
1683 if (c == '\0')
1684 break;
1685 cstr_ccat(cstr, c);
1686 str++;
1690 /* add a wide char */
1691 static void cstr_wccat(CString *cstr, int ch)
1693 int size;
1694 size = cstr->size + sizeof(nwchar_t);
1695 if (size > cstr->size_allocated)
1696 cstr_realloc(cstr, size);
1697 *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
1698 cstr->size = size;
1701 static void cstr_new(CString *cstr)
1703 memset(cstr, 0, sizeof(CString));
1706 /* free string and reset it to NULL */
1707 static void cstr_free(CString *cstr)
1709 tcc_free(cstr->data_allocated);
1710 cstr_new(cstr);
1713 #define cstr_reset(cstr) cstr_free(cstr)
1715 /* XXX: unicode ? */
1716 static void add_char(CString *cstr, int c)
1718 if (c == '\'' || c == '\"' || c == '\\') {
1719 /* XXX: could be more precise if char or string */
1720 cstr_ccat(cstr, '\\');
1722 if (c >= 32 && c <= 126) {
1723 cstr_ccat(cstr, c);
1724 } else {
1725 cstr_ccat(cstr, '\\');
1726 if (c == '\n') {
1727 cstr_ccat(cstr, 'n');
1728 } else {
1729 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1730 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1731 cstr_ccat(cstr, '0' + (c & 7));
1736 /* XXX: buffer overflow */
1737 /* XXX: float tokens */
1738 char *get_tok_str(int v, CValue *cv)
1740 static char buf[STRING_MAX_SIZE + 1];
1741 static CString cstr_buf;
1742 CString *cstr;
1743 unsigned char *q;
1744 char *p;
1745 int i, len;
1747 /* NOTE: to go faster, we give a fixed buffer for small strings */
1748 cstr_reset(&cstr_buf);
1749 cstr_buf.data = buf;
1750 cstr_buf.size_allocated = sizeof(buf);
1751 p = buf;
1753 switch(v) {
1754 case TOK_CINT:
1755 case TOK_CUINT:
1756 /* XXX: not quite exact, but only useful for testing */
1757 sprintf(p, "%u", cv->ui);
1758 break;
1759 case TOK_CLLONG:
1760 case TOK_CULLONG:
1761 /* XXX: not quite exact, but only useful for testing */
1762 sprintf(p, "%Lu", cv->ull);
1763 break;
1764 case TOK_CCHAR:
1765 case TOK_LCHAR:
1766 cstr_ccat(&cstr_buf, '\'');
1767 add_char(&cstr_buf, cv->i);
1768 cstr_ccat(&cstr_buf, '\'');
1769 cstr_ccat(&cstr_buf, '\0');
1770 break;
1771 case TOK_PPNUM:
1772 cstr = cv->cstr;
1773 len = cstr->size - 1;
1774 for(i=0;i<len;i++)
1775 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1776 cstr_ccat(&cstr_buf, '\0');
1777 break;
1778 case TOK_STR:
1779 case TOK_LSTR:
1780 cstr = cv->cstr;
1781 cstr_ccat(&cstr_buf, '\"');
1782 if (v == TOK_STR) {
1783 len = cstr->size - 1;
1784 for(i=0;i<len;i++)
1785 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1786 } else {
1787 len = (cstr->size / sizeof(nwchar_t)) - 1;
1788 for(i=0;i<len;i++)
1789 add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
1791 cstr_ccat(&cstr_buf, '\"');
1792 cstr_ccat(&cstr_buf, '\0');
1793 break;
1794 case TOK_LT:
1795 v = '<';
1796 goto addv;
1797 case TOK_GT:
1798 v = '>';
1799 goto addv;
1800 case TOK_DOTS:
1801 return strcpy(p, "...");
1802 case TOK_A_SHL:
1803 return strcpy(p, "<<=");
1804 case TOK_A_SAR:
1805 return strcpy(p, ">>=");
1806 default:
1807 if (v < TOK_IDENT) {
1808 /* search in two bytes table */
1809 q = tok_two_chars;
1810 while (*q) {
1811 if (q[2] == v) {
1812 *p++ = q[0];
1813 *p++ = q[1];
1814 *p = '\0';
1815 return buf;
1817 q += 3;
1819 addv:
1820 *p++ = v;
1821 *p = '\0';
1822 } else if (v < tok_ident) {
1823 return table_ident[v - TOK_IDENT]->str;
1824 } else if (v >= SYM_FIRST_ANOM) {
1825 /* special name for anonymous symbol */
1826 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1827 } else {
1828 /* should never happen */
1829 return NULL;
1831 break;
1833 return cstr_buf.data;
1836 /* push, without hashing */
1837 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1839 Sym *s;
1840 s = sym_malloc();
1841 s->v = v;
1842 s->type.t = t;
1843 s->c = c;
1844 s->next = NULL;
1845 /* add in stack */
1846 s->prev = *ps;
1847 *ps = s;
1848 return s;
1851 /* find a symbol and return its associated structure. 's' is the top
1852 of the symbol stack */
1853 static Sym *sym_find2(Sym *s, int v)
1855 while (s) {
1856 if (s->v == v)
1857 return s;
1858 s = s->prev;
1860 return NULL;
1863 /* structure lookup */
1864 static inline Sym *struct_find(int v)
1866 v -= TOK_IDENT;
1867 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1868 return NULL;
1869 return table_ident[v]->sym_struct;
1872 /* find an identifier */
1873 static inline Sym *sym_find(int v)
1875 v -= TOK_IDENT;
1876 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1877 return NULL;
1878 return table_ident[v]->sym_identifier;
1881 /* push a given symbol on the symbol stack */
1882 static Sym *sym_push(int v, CType *type, int r, int c)
1884 Sym *s, **ps;
1885 TokenSym *ts;
1887 if (local_stack)
1888 ps = &local_stack;
1889 else
1890 ps = &global_stack;
1891 s = sym_push2(ps, v, type->t, c);
1892 s->type.ref = type->ref;
1893 s->r = r;
1894 /* don't record fields or anonymous symbols */
1895 /* XXX: simplify */
1896 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1897 /* record symbol in token array */
1898 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1899 if (v & SYM_STRUCT)
1900 ps = &ts->sym_struct;
1901 else
1902 ps = &ts->sym_identifier;
1903 s->prev_tok = *ps;
1904 *ps = s;
1906 return s;
1909 /* push a global identifier */
1910 static Sym *global_identifier_push(int v, int t, int c)
1912 Sym *s, **ps;
1913 s = sym_push2(&global_stack, v, t, c);
1914 /* don't record anonymous symbol */
1915 if (v < SYM_FIRST_ANOM) {
1916 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1917 /* modify the top most local identifier, so that
1918 sym_identifier will point to 's' when popped */
1919 while (*ps != NULL)
1920 ps = &(*ps)->prev_tok;
1921 s->prev_tok = NULL;
1922 *ps = s;
1924 return s;
1927 /* pop symbols until top reaches 'b' */
1928 static void sym_pop(Sym **ptop, Sym *b)
1930 Sym *s, *ss, **ps;
1931 TokenSym *ts;
1932 int v;
1934 s = *ptop;
1935 while(s != b) {
1936 ss = s->prev;
1937 v = s->v;
1938 /* remove symbol in token array */
1939 /* XXX: simplify */
1940 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1941 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1942 if (v & SYM_STRUCT)
1943 ps = &ts->sym_struct;
1944 else
1945 ps = &ts->sym_identifier;
1946 *ps = s->prev_tok;
1948 sym_free(s);
1949 s = ss;
1951 *ptop = b;
1954 /* I/O layer */
1956 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1958 int fd;
1959 BufferedFile *bf;
1961 if (strcmp(filename, "-") == 0)
1962 fd = 0, filename = "stdin";
1963 else
1964 fd = open(filename, O_RDONLY | O_BINARY);
1965 if ((verbose == 2 && fd >= 0) || verbose == 3)
1966 printf("%s %*s%s\n", fd < 0 ? "nf":"->",
1967 (s1->include_stack_ptr - s1->include_stack), "", filename);
1968 if (fd < 0)
1969 return NULL;
1970 bf = tcc_malloc(sizeof(BufferedFile));
1971 bf->fd = fd;
1972 bf->buf_ptr = bf->buffer;
1973 bf->buf_end = bf->buffer;
1974 bf->buffer[0] = CH_EOB; /* put eob symbol */
1975 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1976 #ifdef _WIN32
1977 normalize_slashes(bf->filename);
1978 #endif
1979 bf->line_num = 1;
1980 bf->ifndef_macro = 0;
1981 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1982 // printf("opening '%s'\n", filename);
1983 return bf;
1986 void tcc_close(BufferedFile *bf)
1988 total_lines += bf->line_num;
1989 close(bf->fd);
1990 tcc_free(bf);
1993 /* fill input buffer and peek next char */
1994 static int tcc_peekc_slow(BufferedFile *bf)
1996 int len;
1997 /* only tries to read if really end of buffer */
1998 if (bf->buf_ptr >= bf->buf_end) {
1999 if (bf->fd != -1) {
2000 #if defined(PARSE_DEBUG)
2001 len = 8;
2002 #else
2003 len = IO_BUF_SIZE;
2004 #endif
2005 len = read(bf->fd, bf->buffer, len);
2006 if (len < 0)
2007 len = 0;
2008 } else {
2009 len = 0;
2011 total_bytes += len;
2012 bf->buf_ptr = bf->buffer;
2013 bf->buf_end = bf->buffer + len;
2014 *bf->buf_end = CH_EOB;
2016 if (bf->buf_ptr < bf->buf_end) {
2017 return bf->buf_ptr[0];
2018 } else {
2019 bf->buf_ptr = bf->buf_end;
2020 return CH_EOF;
2024 /* return the current character, handling end of block if necessary
2025 (but not stray) */
2026 static int handle_eob(void)
2028 return tcc_peekc_slow(file);
2031 /* read next char from current input file and handle end of input buffer */
2032 static inline void inp(void)
2034 ch = *(++(file->buf_ptr));
2035 /* end of buffer/file handling */
2036 if (ch == CH_EOB)
2037 ch = handle_eob();
2040 /* handle '\[\r]\n' */
2041 static int handle_stray_noerror(void)
2043 while (ch == '\\') {
2044 inp();
2045 if (ch == '\n') {
2046 file->line_num++;
2047 inp();
2048 } else if (ch == '\r') {
2049 inp();
2050 if (ch != '\n')
2051 goto fail;
2052 file->line_num++;
2053 inp();
2054 } else {
2055 fail:
2056 return 1;
2059 return 0;
2062 static void handle_stray(void)
2064 if (handle_stray_noerror())
2065 error("stray '\\' in program");
2068 /* skip the stray and handle the \\n case. Output an error if
2069 incorrect char after the stray */
2070 static int handle_stray1(uint8_t *p)
2072 int c;
2074 if (p >= file->buf_end) {
2075 file->buf_ptr = p;
2076 c = handle_eob();
2077 p = file->buf_ptr;
2078 if (c == '\\')
2079 goto parse_stray;
2080 } else {
2081 parse_stray:
2082 file->buf_ptr = p;
2083 ch = *p;
2084 handle_stray();
2085 p = file->buf_ptr;
2086 c = *p;
2088 return c;
2091 /* handle just the EOB case, but not stray */
2092 #define PEEKC_EOB(c, p)\
2094 p++;\
2095 c = *p;\
2096 if (c == '\\') {\
2097 file->buf_ptr = p;\
2098 c = handle_eob();\
2099 p = file->buf_ptr;\
2103 /* handle the complicated stray case */
2104 #define PEEKC(c, p)\
2106 p++;\
2107 c = *p;\
2108 if (c == '\\') {\
2109 c = handle_stray1(p);\
2110 p = file->buf_ptr;\
2114 /* input with '\[\r]\n' handling. Note that this function cannot
2115 handle other characters after '\', so you cannot call it inside
2116 strings or comments */
2117 static void minp(void)
2119 inp();
2120 if (ch == '\\')
2121 handle_stray();
2125 /* single line C++ comments */
2126 static uint8_t *parse_line_comment(uint8_t *p)
2128 int c;
2130 p++;
2131 for(;;) {
2132 c = *p;
2133 redo:
2134 if (c == '\n' || c == CH_EOF) {
2135 break;
2136 } else if (c == '\\') {
2137 file->buf_ptr = p;
2138 c = handle_eob();
2139 p = file->buf_ptr;
2140 if (c == '\\') {
2141 PEEKC_EOB(c, p);
2142 if (c == '\n') {
2143 file->line_num++;
2144 PEEKC_EOB(c, p);
2145 } else if (c == '\r') {
2146 PEEKC_EOB(c, p);
2147 if (c == '\n') {
2148 file->line_num++;
2149 PEEKC_EOB(c, p);
2152 } else {
2153 goto redo;
2155 } else {
2156 p++;
2159 return p;
2162 /* C comments */
2163 static uint8_t *parse_comment(uint8_t *p)
2165 int c;
2167 p++;
2168 for(;;) {
2169 /* fast skip loop */
2170 for(;;) {
2171 c = *p;
2172 if (c == '\n' || c == '*' || c == '\\')
2173 break;
2174 p++;
2175 c = *p;
2176 if (c == '\n' || c == '*' || c == '\\')
2177 break;
2178 p++;
2180 /* now we can handle all the cases */
2181 if (c == '\n') {
2182 file->line_num++;
2183 p++;
2184 } else if (c == '*') {
2185 p++;
2186 for(;;) {
2187 c = *p;
2188 if (c == '*') {
2189 p++;
2190 } else if (c == '/') {
2191 goto end_of_comment;
2192 } else if (c == '\\') {
2193 file->buf_ptr = p;
2194 c = handle_eob();
2195 p = file->buf_ptr;
2196 if (c == '\\') {
2197 /* skip '\[\r]\n', otherwise just skip the stray */
2198 while (c == '\\') {
2199 PEEKC_EOB(c, p);
2200 if (c == '\n') {
2201 file->line_num++;
2202 PEEKC_EOB(c, p);
2203 } else if (c == '\r') {
2204 PEEKC_EOB(c, p);
2205 if (c == '\n') {
2206 file->line_num++;
2207 PEEKC_EOB(c, p);
2209 } else {
2210 goto after_star;
2214 } else {
2215 break;
2218 after_star: ;
2219 } else {
2220 /* stray, eob or eof */
2221 file->buf_ptr = p;
2222 c = handle_eob();
2223 p = file->buf_ptr;
2224 if (c == CH_EOF) {
2225 error("unexpected end of file in comment");
2226 } else if (c == '\\') {
2227 p++;
2231 end_of_comment:
2232 p++;
2233 return p;
2236 #define cinp minp
2238 /* space exlcuding newline */
2239 static inline int is_space(int ch)
2241 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
2244 static inline void skip_spaces(void)
2246 while (is_space(ch))
2247 cinp();
2250 /* parse a string without interpreting escapes */
2251 static uint8_t *parse_pp_string(uint8_t *p,
2252 int sep, CString *str)
2254 int c;
2255 p++;
2256 for(;;) {
2257 c = *p;
2258 if (c == sep) {
2259 break;
2260 } else if (c == '\\') {
2261 file->buf_ptr = p;
2262 c = handle_eob();
2263 p = file->buf_ptr;
2264 if (c == CH_EOF) {
2265 unterminated_string:
2266 /* XXX: indicate line number of start of string */
2267 error("missing terminating %c character", sep);
2268 } else if (c == '\\') {
2269 /* escape : just skip \[\r]\n */
2270 PEEKC_EOB(c, p);
2271 if (c == '\n') {
2272 file->line_num++;
2273 p++;
2274 } else if (c == '\r') {
2275 PEEKC_EOB(c, p);
2276 if (c != '\n')
2277 expect("'\n' after '\r'");
2278 file->line_num++;
2279 p++;
2280 } else if (c == CH_EOF) {
2281 goto unterminated_string;
2282 } else {
2283 if (str) {
2284 cstr_ccat(str, '\\');
2285 cstr_ccat(str, c);
2287 p++;
2290 } else if (c == '\n') {
2291 file->line_num++;
2292 goto add_char;
2293 } else if (c == '\r') {
2294 PEEKC_EOB(c, p);
2295 if (c != '\n') {
2296 if (str)
2297 cstr_ccat(str, '\r');
2298 } else {
2299 file->line_num++;
2300 goto add_char;
2302 } else {
2303 add_char:
2304 if (str)
2305 cstr_ccat(str, c);
2306 p++;
2309 p++;
2310 return p;
2313 /* skip block of text until #else, #elif or #endif. skip also pairs of
2314 #if/#endif */
2315 void preprocess_skip(void)
2317 int a, start_of_line, c, in_warn_or_error;
2318 uint8_t *p;
2320 p = file->buf_ptr;
2321 a = 0;
2322 redo_start:
2323 start_of_line = 1;
2324 in_warn_or_error = 0;
2325 for(;;) {
2326 redo_no_start:
2327 c = *p;
2328 switch(c) {
2329 case ' ':
2330 case '\t':
2331 case '\f':
2332 case '\v':
2333 case '\r':
2334 p++;
2335 goto redo_no_start;
2336 case '\n':
2337 file->line_num++;
2338 p++;
2339 goto redo_start;
2340 case '\\':
2341 file->buf_ptr = p;
2342 c = handle_eob();
2343 if (c == CH_EOF) {
2344 expect("#endif");
2345 } else if (c == '\\') {
2346 ch = file->buf_ptr[0];
2347 handle_stray_noerror();
2349 p = file->buf_ptr;
2350 goto redo_no_start;
2351 /* skip strings */
2352 case '\"':
2353 case '\'':
2354 if (in_warn_or_error)
2355 goto _default;
2356 p = parse_pp_string(p, c, NULL);
2357 break;
2358 /* skip comments */
2359 case '/':
2360 if (in_warn_or_error)
2361 goto _default;
2362 file->buf_ptr = p;
2363 ch = *p;
2364 minp();
2365 p = file->buf_ptr;
2366 if (ch == '*') {
2367 p = parse_comment(p);
2368 } else if (ch == '/') {
2369 p = parse_line_comment(p);
2371 break;
2372 case '#':
2373 p++;
2374 if (start_of_line) {
2375 file->buf_ptr = p;
2376 next_nomacro();
2377 p = file->buf_ptr;
2378 if (a == 0 &&
2379 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2380 goto the_end;
2381 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2382 a++;
2383 else if (tok == TOK_ENDIF)
2384 a--;
2385 else if( tok == TOK_ERROR || tok == TOK_WARNING)
2386 in_warn_or_error = 1;
2388 break;
2389 _default:
2390 default:
2391 p++;
2392 break;
2394 start_of_line = 0;
2396 the_end: ;
2397 file->buf_ptr = p;
2400 /* ParseState handling */
2402 /* XXX: currently, no include file info is stored. Thus, we cannot display
2403 accurate messages if the function or data definition spans multiple
2404 files */
2406 /* save current parse state in 's' */
2407 void save_parse_state(ParseState *s)
2409 s->line_num = file->line_num;
2410 s->macro_ptr = macro_ptr;
2411 s->tok = tok;
2412 s->tokc = tokc;
2415 /* restore parse state from 's' */
2416 void restore_parse_state(ParseState *s)
2418 file->line_num = s->line_num;
2419 macro_ptr = s->macro_ptr;
2420 tok = s->tok;
2421 tokc = s->tokc;
2424 /* return the number of additional 'ints' necessary to store the
2425 token */
2426 static inline int tok_ext_size(int t)
2428 switch(t) {
2429 /* 4 bytes */
2430 case TOK_CINT:
2431 case TOK_CUINT:
2432 case TOK_CCHAR:
2433 case TOK_LCHAR:
2434 case TOK_CFLOAT:
2435 case TOK_LINENUM:
2436 return 1;
2437 case TOK_STR:
2438 case TOK_LSTR:
2439 case TOK_PPNUM:
2440 error("unsupported token");
2441 return 1;
2442 case TOK_CDOUBLE:
2443 case TOK_CLLONG:
2444 case TOK_CULLONG:
2445 return 2;
2446 case TOK_CLDOUBLE:
2447 return LDOUBLE_SIZE / 4;
2448 default:
2449 return 0;
2453 /* token string handling */
2455 static inline void tok_str_new(TokenString *s)
2457 s->str = NULL;
2458 s->len = 0;
2459 s->allocated_len = 0;
2460 s->last_line_num = -1;
2463 static void tok_str_free(int *str)
2465 tcc_free(str);
2468 static int *tok_str_realloc(TokenString *s)
2470 int *str, len;
2472 if (s->allocated_len == 0) {
2473 len = 8;
2474 } else {
2475 len = s->allocated_len * 2;
2477 str = tcc_realloc(s->str, len * sizeof(int));
2478 if (!str)
2479 error("memory full");
2480 s->allocated_len = len;
2481 s->str = str;
2482 return str;
2485 static void tok_str_add(TokenString *s, int t)
2487 int len, *str;
2489 len = s->len;
2490 str = s->str;
2491 if (len >= s->allocated_len)
2492 str = tok_str_realloc(s);
2493 str[len++] = t;
2494 s->len = len;
2497 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2499 int len, *str;
2501 len = s->len;
2502 str = s->str;
2504 /* allocate space for worst case */
2505 if (len + TOK_MAX_SIZE > s->allocated_len)
2506 str = tok_str_realloc(s);
2507 str[len++] = t;
2508 switch(t) {
2509 case TOK_CINT:
2510 case TOK_CUINT:
2511 case TOK_CCHAR:
2512 case TOK_LCHAR:
2513 case TOK_CFLOAT:
2514 case TOK_LINENUM:
2515 str[len++] = cv->tab[0];
2516 break;
2517 case TOK_PPNUM:
2518 case TOK_STR:
2519 case TOK_LSTR:
2521 int nb_words;
2522 CString *cstr;
2524 nb_words = (sizeof(CString) + cv->cstr->size + 3) >> 2;
2525 while ((len + nb_words) > s->allocated_len)
2526 str = tok_str_realloc(s);
2527 cstr = (CString *)(str + len);
2528 cstr->data = NULL;
2529 cstr->size = cv->cstr->size;
2530 cstr->data_allocated = NULL;
2531 cstr->size_allocated = cstr->size;
2532 memcpy((char *)cstr + sizeof(CString),
2533 cv->cstr->data, cstr->size);
2534 len += nb_words;
2536 break;
2537 case TOK_CDOUBLE:
2538 case TOK_CLLONG:
2539 case TOK_CULLONG:
2540 #if LDOUBLE_SIZE == 8
2541 case TOK_CLDOUBLE:
2542 #endif
2543 str[len++] = cv->tab[0];
2544 str[len++] = cv->tab[1];
2545 break;
2546 #if LDOUBLE_SIZE == 12
2547 case TOK_CLDOUBLE:
2548 str[len++] = cv->tab[0];
2549 str[len++] = cv->tab[1];
2550 str[len++] = cv->tab[2];
2551 #elif LDOUBLE_SIZE != 8
2552 #error add long double size support
2553 #endif
2554 break;
2555 default:
2556 break;
2558 s->len = len;
2561 /* add the current parse token in token string 's' */
2562 static void tok_str_add_tok(TokenString *s)
2564 CValue cval;
2566 /* save line number info */
2567 if (file->line_num != s->last_line_num) {
2568 s->last_line_num = file->line_num;
2569 cval.i = s->last_line_num;
2570 tok_str_add2(s, TOK_LINENUM, &cval);
2572 tok_str_add2(s, tok, &tokc);
2575 #if LDOUBLE_SIZE == 12
2576 #define LDOUBLE_GET(p, cv) \
2577 cv.tab[0] = p[0]; \
2578 cv.tab[1] = p[1]; \
2579 cv.tab[2] = p[2];
2580 #elif LDOUBLE_SIZE == 8
2581 #define LDOUBLE_GET(p, cv) \
2582 cv.tab[0] = p[0]; \
2583 cv.tab[1] = p[1];
2584 #else
2585 #error add long double size support
2586 #endif
2589 /* get a token from an integer array and increment pointer
2590 accordingly. we code it as a macro to avoid pointer aliasing. */
2591 #define TOK_GET(t, p, cv) \
2593 t = *p++; \
2594 switch(t) { \
2595 case TOK_CINT: \
2596 case TOK_CUINT: \
2597 case TOK_CCHAR: \
2598 case TOK_LCHAR: \
2599 case TOK_CFLOAT: \
2600 case TOK_LINENUM: \
2601 cv.tab[0] = *p++; \
2602 break; \
2603 case TOK_STR: \
2604 case TOK_LSTR: \
2605 case TOK_PPNUM: \
2606 cv.cstr = (CString *)p; \
2607 cv.cstr->data = (char *)p + sizeof(CString);\
2608 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2609 break; \
2610 case TOK_CDOUBLE: \
2611 case TOK_CLLONG: \
2612 case TOK_CULLONG: \
2613 cv.tab[0] = p[0]; \
2614 cv.tab[1] = p[1]; \
2615 p += 2; \
2616 break; \
2617 case TOK_CLDOUBLE: \
2618 LDOUBLE_GET(p, cv); \
2619 p += LDOUBLE_SIZE / 4; \
2620 break; \
2621 default: \
2622 break; \
2626 /* defines handling */
2627 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2629 Sym *s;
2631 s = sym_push2(&define_stack, v, macro_type, (int)str);
2632 s->next = first_arg;
2633 table_ident[v - TOK_IDENT]->sym_define = s;
2636 /* undefined a define symbol. Its name is just set to zero */
2637 static void define_undef(Sym *s)
2639 int v;
2640 v = s->v;
2641 if (v >= TOK_IDENT && v < tok_ident)
2642 table_ident[v - TOK_IDENT]->sym_define = NULL;
2643 s->v = 0;
2646 static inline Sym *define_find(int v)
2648 v -= TOK_IDENT;
2649 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2650 return NULL;
2651 return table_ident[v]->sym_define;
2654 /* free define stack until top reaches 'b' */
2655 static void free_defines(Sym *b)
2657 Sym *top, *top1;
2658 int v;
2660 top = define_stack;
2661 while (top != b) {
2662 top1 = top->prev;
2663 /* do not free args or predefined defines */
2664 if (top->c)
2665 tok_str_free((int *)top->c);
2666 v = top->v;
2667 if (v >= TOK_IDENT && v < tok_ident)
2668 table_ident[v - TOK_IDENT]->sym_define = NULL;
2669 sym_free(top);
2670 top = top1;
2672 define_stack = b;
2675 /* label lookup */
2676 static Sym *label_find(int v)
2678 v -= TOK_IDENT;
2679 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2680 return NULL;
2681 return table_ident[v]->sym_label;
2684 static Sym *label_push(Sym **ptop, int v, int flags)
2686 Sym *s, **ps;
2687 s = sym_push2(ptop, v, 0, 0);
2688 s->r = flags;
2689 ps = &table_ident[v - TOK_IDENT]->sym_label;
2690 if (ptop == &global_label_stack) {
2691 /* modify the top most local identifier, so that
2692 sym_identifier will point to 's' when popped */
2693 while (*ps != NULL)
2694 ps = &(*ps)->prev_tok;
2696 s->prev_tok = *ps;
2697 *ps = s;
2698 return s;
2701 /* pop labels until element last is reached. Look if any labels are
2702 undefined. Define symbols if '&&label' was used. */
2703 static void label_pop(Sym **ptop, Sym *slast)
2705 Sym *s, *s1;
2706 for(s = *ptop; s != slast; s = s1) {
2707 s1 = s->prev;
2708 if (s->r == LABEL_DECLARED) {
2709 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2710 } else if (s->r == LABEL_FORWARD) {
2711 error("label '%s' used but not defined",
2712 get_tok_str(s->v, NULL));
2713 } else {
2714 if (s->c) {
2715 /* define corresponding symbol. A size of
2716 1 is put. */
2717 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2720 /* remove label */
2721 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2722 sym_free(s);
2724 *ptop = slast;
2727 /* eval an expression for #if/#elif */
2728 static int expr_preprocess(void)
2730 int c, t;
2731 TokenString str;
2733 tok_str_new(&str);
2734 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2735 next(); /* do macro subst */
2736 if (tok == TOK_DEFINED) {
2737 next_nomacro();
2738 t = tok;
2739 if (t == '(')
2740 next_nomacro();
2741 c = define_find(tok) != 0;
2742 if (t == '(')
2743 next_nomacro();
2744 tok = TOK_CINT;
2745 tokc.i = c;
2746 } else if (tok >= TOK_IDENT) {
2747 /* if undefined macro */
2748 tok = TOK_CINT;
2749 tokc.i = 0;
2751 tok_str_add_tok(&str);
2753 tok_str_add(&str, -1); /* simulate end of file */
2754 tok_str_add(&str, 0);
2755 /* now evaluate C constant expression */
2756 macro_ptr = str.str;
2757 next();
2758 c = expr_const();
2759 macro_ptr = NULL;
2760 tok_str_free(str.str);
2761 return c != 0;
2764 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2765 static void tok_print(int *str)
2767 int t;
2768 CValue cval;
2770 while (1) {
2771 TOK_GET(t, str, cval);
2772 if (!t)
2773 break;
2774 printf(" %s", get_tok_str(t, &cval));
2776 printf("\n");
2778 #endif
2780 /* parse after #define */
2781 static void parse_define(void)
2783 Sym *s, *first, **ps;
2784 int v, t, varg, is_vaargs, c;
2785 TokenString str;
2787 v = tok;
2788 if (v < TOK_IDENT)
2789 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2790 /* XXX: should check if same macro (ANSI) */
2791 first = NULL;
2792 t = MACRO_OBJ;
2793 /* '(' must be just after macro definition for MACRO_FUNC */
2794 c = file->buf_ptr[0];
2795 if (c == '\\')
2796 c = handle_stray1(file->buf_ptr);
2797 if (c == '(') {
2798 next_nomacro();
2799 next_nomacro();
2800 ps = &first;
2801 while (tok != ')') {
2802 varg = tok;
2803 next_nomacro();
2804 is_vaargs = 0;
2805 if (varg == TOK_DOTS) {
2806 varg = TOK___VA_ARGS__;
2807 is_vaargs = 1;
2808 } else if (tok == TOK_DOTS && gnu_ext) {
2809 is_vaargs = 1;
2810 next_nomacro();
2812 if (varg < TOK_IDENT)
2813 error("badly punctuated parameter list");
2814 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2815 *ps = s;
2816 ps = &s->next;
2817 if (tok != ',')
2818 break;
2819 next_nomacro();
2821 t = MACRO_FUNC;
2823 tok_str_new(&str);
2824 next_nomacro();
2825 /* EOF testing necessary for '-D' handling */
2826 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2827 tok_str_add2(&str, tok, &tokc);
2828 next_nomacro();
2830 tok_str_add(&str, 0);
2831 #ifdef PP_DEBUG
2832 printf("define %s %d: ", get_tok_str(v, NULL), t);
2833 tok_print(str.str);
2834 #endif
2835 define_push(v, t, str.str, first);
2838 static inline int hash_cached_include(int type, const char *filename)
2840 const unsigned char *s;
2841 unsigned int h;
2843 h = TOK_HASH_INIT;
2844 h = TOK_HASH_FUNC(h, type);
2845 s = filename;
2846 while (*s) {
2847 h = TOK_HASH_FUNC(h, *s);
2848 s++;
2850 h &= (CACHED_INCLUDES_HASH_SIZE - 1);
2851 return h;
2854 /* XXX: use a token or a hash table to accelerate matching ? */
2855 static CachedInclude *search_cached_include(TCCState *s1,
2856 int type, const char *filename)
2858 CachedInclude *e;
2859 int i, h;
2860 h = hash_cached_include(type, filename);
2861 i = s1->cached_includes_hash[h];
2862 for(;;) {
2863 if (i == 0)
2864 break;
2865 e = s1->cached_includes[i - 1];
2866 if (e->type == type && !strcmp(e->filename, filename))
2867 return e;
2868 i = e->hash_next;
2870 return NULL;
2873 static inline void add_cached_include(TCCState *s1, int type,
2874 const char *filename, int ifndef_macro)
2876 CachedInclude *e;
2877 int h;
2879 if (search_cached_include(s1, type, filename))
2880 return;
2881 #ifdef INC_DEBUG
2882 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2883 #endif
2884 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2885 if (!e)
2886 return;
2887 e->type = type;
2888 strcpy(e->filename, filename);
2889 e->ifndef_macro = ifndef_macro;
2890 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2891 /* add in hash table */
2892 h = hash_cached_include(type, filename);
2893 e->hash_next = s1->cached_includes_hash[h];
2894 s1->cached_includes_hash[h] = s1->nb_cached_includes;
2897 static void pragma_parse(TCCState *s1)
2899 int val;
2901 next();
2902 if (tok == TOK_pack) {
2904 This may be:
2905 #pragma pack(1) // set
2906 #pragma pack() // reset to default
2907 #pragma pack(push,1) // push & set
2908 #pragma pack(pop) // restore previous
2910 next();
2911 skip('(');
2912 if (tok == TOK_ASM_pop) {
2913 next();
2914 if (s1->pack_stack_ptr <= s1->pack_stack) {
2915 stk_error:
2916 error("out of pack stack");
2918 s1->pack_stack_ptr--;
2919 } else {
2920 val = 0;
2921 if (tok != ')') {
2922 if (tok == TOK_ASM_push) {
2923 next();
2924 if (s1->pack_stack_ptr >= s1->pack_stack + PACK_STACK_SIZE - 1)
2925 goto stk_error;
2926 s1->pack_stack_ptr++;
2927 skip(',');
2929 if (tok != TOK_CINT) {
2930 pack_error:
2931 error("invalid pack pragma");
2933 val = tokc.i;
2934 if (val < 1 || val > 16 || (val & (val - 1)) != 0)
2935 goto pack_error;
2936 next();
2938 *s1->pack_stack_ptr = val;
2939 skip(')');
2944 /* is_bof is true if first non space token at beginning of file */
2945 static void preprocess(int is_bof)
2947 TCCState *s1 = tcc_state;
2948 int size, i, c, n, saved_parse_flags;
2949 char buf[1024], *q;
2950 char buf1[1024];
2951 BufferedFile *f;
2952 Sym *s;
2953 CachedInclude *e;
2955 saved_parse_flags = parse_flags;
2956 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2957 PARSE_FLAG_LINEFEED;
2958 next_nomacro();
2959 redo:
2960 switch(tok) {
2961 case TOK_DEFINE:
2962 next_nomacro();
2963 parse_define();
2964 break;
2965 case TOK_UNDEF:
2966 next_nomacro();
2967 s = define_find(tok);
2968 /* undefine symbol by putting an invalid name */
2969 if (s)
2970 define_undef(s);
2971 break;
2972 case TOK_INCLUDE:
2973 case TOK_INCLUDE_NEXT:
2974 ch = file->buf_ptr[0];
2975 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2976 skip_spaces();
2977 if (ch == '<') {
2978 c = '>';
2979 goto read_name;
2980 } else if (ch == '\"') {
2981 c = ch;
2982 read_name:
2983 inp();
2984 q = buf;
2985 while (ch != c && ch != '\n' && ch != CH_EOF) {
2986 if ((q - buf) < sizeof(buf) - 1)
2987 *q++ = ch;
2988 if (ch == '\\') {
2989 if (handle_stray_noerror() == 0)
2990 --q;
2991 } else
2992 inp();
2994 *q = '\0';
2995 minp();
2996 #if 0
2997 /* eat all spaces and comments after include */
2998 /* XXX: slightly incorrect */
2999 while (ch1 != '\n' && ch1 != CH_EOF)
3000 inp();
3001 #endif
3002 } else {
3003 /* computed #include : either we have only strings or
3004 we have anything enclosed in '<>' */
3005 next();
3006 buf[0] = '\0';
3007 if (tok == TOK_STR) {
3008 while (tok != TOK_LINEFEED) {
3009 if (tok != TOK_STR) {
3010 include_syntax:
3011 error("'#include' expects \"FILENAME\" or <FILENAME>");
3013 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
3014 next();
3016 c = '\"';
3017 } else {
3018 int len;
3019 while (tok != TOK_LINEFEED) {
3020 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
3021 next();
3023 len = strlen(buf);
3024 /* check syntax and remove '<>' */
3025 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
3026 goto include_syntax;
3027 memmove(buf, buf + 1, len - 2);
3028 buf[len - 2] = '\0';
3029 c = '>';
3033 e = search_cached_include(s1, c, buf);
3034 if (e && define_find(e->ifndef_macro)) {
3035 /* no need to parse the include because the 'ifndef macro'
3036 is defined */
3037 #ifdef INC_DEBUG
3038 printf("%s: skipping %s\n", file->filename, buf);
3039 #endif
3040 } else {
3041 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
3042 error("#include recursion too deep");
3043 /* push current file in stack */
3044 /* XXX: fix current line init */
3045 *s1->include_stack_ptr++ = file;
3046 if (c == '\"') {
3047 /* first search in current dir if "header.h" */
3048 size = tcc_basename(file->filename) - file->filename;
3049 if (size > sizeof(buf1) - 1)
3050 size = sizeof(buf1) - 1;
3051 memcpy(buf1, file->filename, size);
3052 buf1[size] = '\0';
3053 pstrcat(buf1, sizeof(buf1), buf);
3054 f = tcc_open(s1, buf1);
3055 if (f) {
3056 if (tok == TOK_INCLUDE_NEXT)
3057 tok = TOK_INCLUDE;
3058 else
3059 goto found;
3062 /* now search in all the include paths */
3063 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
3064 for(i = 0; i < n; i++) {
3065 const char *path;
3066 if (i < s1->nb_include_paths)
3067 path = s1->include_paths[i];
3068 else
3069 path = s1->sysinclude_paths[i - s1->nb_include_paths];
3070 pstrcpy(buf1, sizeof(buf1), path);
3071 pstrcat(buf1, sizeof(buf1), "/");
3072 pstrcat(buf1, sizeof(buf1), buf);
3073 f = tcc_open(s1, buf1);
3074 if (f) {
3075 if (tok == TOK_INCLUDE_NEXT)
3076 tok = TOK_INCLUDE;
3077 else
3078 goto found;
3081 --s1->include_stack_ptr;
3082 error("include file '%s' not found", buf);
3083 break;
3084 found:
3085 #ifdef INC_DEBUG
3086 printf("%s: including %s\n", file->filename, buf1);
3087 #endif
3088 f->inc_type = c;
3089 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
3090 file = f;
3091 /* add include file debug info */
3092 if (do_debug) {
3093 put_stabs(file->filename, N_BINCL, 0, 0, 0);
3095 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
3096 ch = file->buf_ptr[0];
3097 goto the_end;
3099 break;
3100 case TOK_IFNDEF:
3101 c = 1;
3102 goto do_ifdef;
3103 case TOK_IF:
3104 c = expr_preprocess();
3105 goto do_if;
3106 case TOK_IFDEF:
3107 c = 0;
3108 do_ifdef:
3109 next_nomacro();
3110 if (tok < TOK_IDENT)
3111 error("invalid argument for '#if%sdef'", c ? "n" : "");
3112 if (is_bof) {
3113 if (c) {
3114 #ifdef INC_DEBUG
3115 printf("#ifndef %s\n", get_tok_str(tok, NULL));
3116 #endif
3117 file->ifndef_macro = tok;
3120 c = (define_find(tok) != 0) ^ c;
3121 do_if:
3122 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
3123 error("memory full");
3124 *s1->ifdef_stack_ptr++ = c;
3125 goto test_skip;
3126 case TOK_ELSE:
3127 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3128 error("#else without matching #if");
3129 if (s1->ifdef_stack_ptr[-1] & 2)
3130 error("#else after #else");
3131 c = (s1->ifdef_stack_ptr[-1] ^= 3);
3132 goto test_skip;
3133 case TOK_ELIF:
3134 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
3135 error("#elif without matching #if");
3136 c = s1->ifdef_stack_ptr[-1];
3137 if (c > 1)
3138 error("#elif after #else");
3139 /* last #if/#elif expression was true: we skip */
3140 if (c == 1)
3141 goto skip;
3142 c = expr_preprocess();
3143 s1->ifdef_stack_ptr[-1] = c;
3144 test_skip:
3145 if (!(c & 1)) {
3146 skip:
3147 preprocess_skip();
3148 is_bof = 0;
3149 goto redo;
3151 break;
3152 case TOK_ENDIF:
3153 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
3154 error("#endif without matching #if");
3155 s1->ifdef_stack_ptr--;
3156 /* '#ifndef macro' was at the start of file. Now we check if
3157 an '#endif' is exactly at the end of file */
3158 if (file->ifndef_macro &&
3159 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
3160 file->ifndef_macro_saved = file->ifndef_macro;
3161 /* need to set to zero to avoid false matches if another
3162 #ifndef at middle of file */
3163 file->ifndef_macro = 0;
3164 while (tok != TOK_LINEFEED)
3165 next_nomacro();
3166 tok_flags |= TOK_FLAG_ENDIF;
3167 goto the_end;
3169 break;
3170 case TOK_LINE:
3171 next();
3172 if (tok != TOK_CINT)
3173 error("#line");
3174 file->line_num = tokc.i - 1; /* the line number will be incremented after */
3175 next();
3176 if (tok != TOK_LINEFEED) {
3177 if (tok != TOK_STR)
3178 error("#line");
3179 pstrcpy(file->filename, sizeof(file->filename),
3180 (char *)tokc.cstr->data);
3182 break;
3183 case TOK_ERROR:
3184 case TOK_WARNING:
3185 c = tok;
3186 ch = file->buf_ptr[0];
3187 skip_spaces();
3188 q = buf;
3189 while (ch != '\n' && ch != CH_EOF) {
3190 if ((q - buf) < sizeof(buf) - 1)
3191 *q++ = ch;
3192 if (ch == '\\') {
3193 if (handle_stray_noerror() == 0)
3194 --q;
3195 } else
3196 inp();
3198 *q = '\0';
3199 if (c == TOK_ERROR)
3200 error("#error %s", buf);
3201 else
3202 warning("#warning %s", buf);
3203 break;
3204 case TOK_PRAGMA:
3205 pragma_parse(s1);
3206 break;
3207 default:
3208 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
3209 /* '!' is ignored to allow C scripts. numbers are ignored
3210 to emulate cpp behaviour */
3211 } else {
3212 if (!(saved_parse_flags & PARSE_FLAG_ASM_COMMENTS))
3213 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok, &tokc));
3215 break;
3217 /* ignore other preprocess commands or #! for C scripts */
3218 while (tok != TOK_LINEFEED)
3219 next_nomacro();
3220 the_end:
3221 parse_flags = saved_parse_flags;
3224 /* evaluate escape codes in a string. */
3225 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
3227 int c, n;
3228 const uint8_t *p;
3230 p = buf;
3231 for(;;) {
3232 c = *p;
3233 if (c == '\0')
3234 break;
3235 if (c == '\\') {
3236 p++;
3237 /* escape */
3238 c = *p;
3239 switch(c) {
3240 case '0': case '1': case '2': case '3':
3241 case '4': case '5': case '6': case '7':
3242 /* at most three octal digits */
3243 n = c - '0';
3244 p++;
3245 c = *p;
3246 if (isoct(c)) {
3247 n = n * 8 + c - '0';
3248 p++;
3249 c = *p;
3250 if (isoct(c)) {
3251 n = n * 8 + c - '0';
3252 p++;
3255 c = n;
3256 goto add_char_nonext;
3257 case 'x':
3258 case 'u':
3259 case 'U':
3260 p++;
3261 n = 0;
3262 for(;;) {
3263 c = *p;
3264 if (c >= 'a' && c <= 'f')
3265 c = c - 'a' + 10;
3266 else if (c >= 'A' && c <= 'F')
3267 c = c - 'A' + 10;
3268 else if (isnum(c))
3269 c = c - '0';
3270 else
3271 break;
3272 n = n * 16 + c;
3273 p++;
3275 c = n;
3276 goto add_char_nonext;
3277 case 'a':
3278 c = '\a';
3279 break;
3280 case 'b':
3281 c = '\b';
3282 break;
3283 case 'f':
3284 c = '\f';
3285 break;
3286 case 'n':
3287 c = '\n';
3288 break;
3289 case 'r':
3290 c = '\r';
3291 break;
3292 case 't':
3293 c = '\t';
3294 break;
3295 case 'v':
3296 c = '\v';
3297 break;
3298 case 'e':
3299 if (!gnu_ext)
3300 goto invalid_escape;
3301 c = 27;
3302 break;
3303 case '\'':
3304 case '\"':
3305 case '\\':
3306 case '?':
3307 break;
3308 default:
3309 invalid_escape:
3310 if (c >= '!' && c <= '~')
3311 warning("unknown escape sequence: \'\\%c\'", c);
3312 else
3313 warning("unknown escape sequence: \'\\x%x\'", c);
3314 break;
3317 p++;
3318 add_char_nonext:
3319 if (!is_long)
3320 cstr_ccat(outstr, c);
3321 else
3322 cstr_wccat(outstr, c);
3324 /* add a trailing '\0' */
3325 if (!is_long)
3326 cstr_ccat(outstr, '\0');
3327 else
3328 cstr_wccat(outstr, '\0');
3331 /* we use 64 bit numbers */
3332 #define BN_SIZE 2
3334 /* bn = (bn << shift) | or_val */
3335 void bn_lshift(unsigned int *bn, int shift, int or_val)
3337 int i;
3338 unsigned int v;
3339 for(i=0;i<BN_SIZE;i++) {
3340 v = bn[i];
3341 bn[i] = (v << shift) | or_val;
3342 or_val = v >> (32 - shift);
3346 void bn_zero(unsigned int *bn)
3348 int i;
3349 for(i=0;i<BN_SIZE;i++) {
3350 bn[i] = 0;
3354 /* parse number in null terminated string 'p' and return it in the
3355 current token */
3356 void parse_number(const char *p)
3358 int b, t, shift, frac_bits, s, exp_val, ch;
3359 char *q;
3360 unsigned int bn[BN_SIZE];
3361 double d;
3363 /* number */
3364 q = token_buf;
3365 ch = *p++;
3366 t = ch;
3367 ch = *p++;
3368 *q++ = t;
3369 b = 10;
3370 if (t == '.') {
3371 goto float_frac_parse;
3372 } else if (t == '0') {
3373 if (ch == 'x' || ch == 'X') {
3374 q--;
3375 ch = *p++;
3376 b = 16;
3377 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
3378 q--;
3379 ch = *p++;
3380 b = 2;
3383 /* parse all digits. cannot check octal numbers at this stage
3384 because of floating point constants */
3385 while (1) {
3386 if (ch >= 'a' && ch <= 'f')
3387 t = ch - 'a' + 10;
3388 else if (ch >= 'A' && ch <= 'F')
3389 t = ch - 'A' + 10;
3390 else if (isnum(ch))
3391 t = ch - '0';
3392 else
3393 break;
3394 if (t >= b)
3395 break;
3396 if (q >= token_buf + STRING_MAX_SIZE) {
3397 num_too_long:
3398 error("number too long");
3400 *q++ = ch;
3401 ch = *p++;
3403 if (ch == '.' ||
3404 ((ch == 'e' || ch == 'E') && b == 10) ||
3405 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
3406 if (b != 10) {
3407 /* NOTE: strtox should support that for hexa numbers, but
3408 non ISOC99 libcs do not support it, so we prefer to do
3409 it by hand */
3410 /* hexadecimal or binary floats */
3411 /* XXX: handle overflows */
3412 *q = '\0';
3413 if (b == 16)
3414 shift = 4;
3415 else
3416 shift = 2;
3417 bn_zero(bn);
3418 q = token_buf;
3419 while (1) {
3420 t = *q++;
3421 if (t == '\0') {
3422 break;
3423 } else if (t >= 'a') {
3424 t = t - 'a' + 10;
3425 } else if (t >= 'A') {
3426 t = t - 'A' + 10;
3427 } else {
3428 t = t - '0';
3430 bn_lshift(bn, shift, t);
3432 frac_bits = 0;
3433 if (ch == '.') {
3434 ch = *p++;
3435 while (1) {
3436 t = ch;
3437 if (t >= 'a' && t <= 'f') {
3438 t = t - 'a' + 10;
3439 } else if (t >= 'A' && t <= 'F') {
3440 t = t - 'A' + 10;
3441 } else if (t >= '0' && t <= '9') {
3442 t = t - '0';
3443 } else {
3444 break;
3446 if (t >= b)
3447 error("invalid digit");
3448 bn_lshift(bn, shift, t);
3449 frac_bits += shift;
3450 ch = *p++;
3453 if (ch != 'p' && ch != 'P')
3454 expect("exponent");
3455 ch = *p++;
3456 s = 1;
3457 exp_val = 0;
3458 if (ch == '+') {
3459 ch = *p++;
3460 } else if (ch == '-') {
3461 s = -1;
3462 ch = *p++;
3464 if (ch < '0' || ch > '9')
3465 expect("exponent digits");
3466 while (ch >= '0' && ch <= '9') {
3467 exp_val = exp_val * 10 + ch - '0';
3468 ch = *p++;
3470 exp_val = exp_val * s;
3472 /* now we can generate the number */
3473 /* XXX: should patch directly float number */
3474 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3475 d = ldexp(d, exp_val - frac_bits);
3476 t = toup(ch);
3477 if (t == 'F') {
3478 ch = *p++;
3479 tok = TOK_CFLOAT;
3480 /* float : should handle overflow */
3481 tokc.f = (float)d;
3482 } else if (t == 'L') {
3483 ch = *p++;
3484 tok = TOK_CLDOUBLE;
3485 /* XXX: not large enough */
3486 tokc.ld = (long double)d;
3487 } else {
3488 tok = TOK_CDOUBLE;
3489 tokc.d = d;
3491 } else {
3492 /* decimal floats */
3493 if (ch == '.') {
3494 if (q >= token_buf + STRING_MAX_SIZE)
3495 goto num_too_long;
3496 *q++ = ch;
3497 ch = *p++;
3498 float_frac_parse:
3499 while (ch >= '0' && ch <= '9') {
3500 if (q >= token_buf + STRING_MAX_SIZE)
3501 goto num_too_long;
3502 *q++ = ch;
3503 ch = *p++;
3506 if (ch == 'e' || ch == 'E') {
3507 if (q >= token_buf + STRING_MAX_SIZE)
3508 goto num_too_long;
3509 *q++ = ch;
3510 ch = *p++;
3511 if (ch == '-' || ch == '+') {
3512 if (q >= token_buf + STRING_MAX_SIZE)
3513 goto num_too_long;
3514 *q++ = ch;
3515 ch = *p++;
3517 if (ch < '0' || ch > '9')
3518 expect("exponent digits");
3519 while (ch >= '0' && ch <= '9') {
3520 if (q >= token_buf + STRING_MAX_SIZE)
3521 goto num_too_long;
3522 *q++ = ch;
3523 ch = *p++;
3526 *q = '\0';
3527 t = toup(ch);
3528 errno = 0;
3529 if (t == 'F') {
3530 ch = *p++;
3531 tok = TOK_CFLOAT;
3532 tokc.f = strtof(token_buf, NULL);
3533 } else if (t == 'L') {
3534 ch = *p++;
3535 tok = TOK_CLDOUBLE;
3536 tokc.ld = strtold(token_buf, NULL);
3537 } else {
3538 tok = TOK_CDOUBLE;
3539 tokc.d = strtod(token_buf, NULL);
3542 } else {
3543 unsigned long long n, n1;
3544 int lcount, ucount;
3546 /* integer number */
3547 *q = '\0';
3548 q = token_buf;
3549 if (b == 10 && *q == '0') {
3550 b = 8;
3551 q++;
3553 n = 0;
3554 while(1) {
3555 t = *q++;
3556 /* no need for checks except for base 10 / 8 errors */
3557 if (t == '\0') {
3558 break;
3559 } else if (t >= 'a') {
3560 t = t - 'a' + 10;
3561 } else if (t >= 'A') {
3562 t = t - 'A' + 10;
3563 } else {
3564 t = t - '0';
3565 if (t >= b)
3566 error("invalid digit");
3568 n1 = n;
3569 n = n * b + t;
3570 /* detect overflow */
3571 /* XXX: this test is not reliable */
3572 if (n < n1)
3573 error("integer constant overflow");
3576 /* XXX: not exactly ANSI compliant */
3577 if ((n & 0xffffffff00000000LL) != 0) {
3578 if ((n >> 63) != 0)
3579 tok = TOK_CULLONG;
3580 else
3581 tok = TOK_CLLONG;
3582 } else if (n > 0x7fffffff) {
3583 tok = TOK_CUINT;
3584 } else {
3585 tok = TOK_CINT;
3587 lcount = 0;
3588 ucount = 0;
3589 for(;;) {
3590 t = toup(ch);
3591 if (t == 'L') {
3592 if (lcount >= 2)
3593 error("three 'l's in integer constant");
3594 lcount++;
3595 if (lcount == 2) {
3596 if (tok == TOK_CINT)
3597 tok = TOK_CLLONG;
3598 else if (tok == TOK_CUINT)
3599 tok = TOK_CULLONG;
3601 ch = *p++;
3602 } else if (t == 'U') {
3603 if (ucount >= 1)
3604 error("two 'u's in integer constant");
3605 ucount++;
3606 if (tok == TOK_CINT)
3607 tok = TOK_CUINT;
3608 else if (tok == TOK_CLLONG)
3609 tok = TOK_CULLONG;
3610 ch = *p++;
3611 } else {
3612 break;
3615 if (tok == TOK_CINT || tok == TOK_CUINT)
3616 tokc.ui = n;
3617 else
3618 tokc.ull = n;
3623 #define PARSE2(c1, tok1, c2, tok2) \
3624 case c1: \
3625 PEEKC(c, p); \
3626 if (c == c2) { \
3627 p++; \
3628 tok = tok2; \
3629 } else { \
3630 tok = tok1; \
3632 break;
3634 /* return next token without macro substitution */
3635 static inline void next_nomacro1(void)
3637 int t, c, is_long;
3638 TokenSym *ts;
3639 uint8_t *p, *p1;
3640 unsigned int h;
3642 p = file->buf_ptr;
3643 redo_no_start:
3644 c = *p;
3645 switch(c) {
3646 case ' ':
3647 case '\t':
3648 case '\f':
3649 case '\v':
3650 case '\r':
3651 p++;
3652 goto redo_no_start;
3654 case '\\':
3655 /* first look if it is in fact an end of buffer */
3656 if (p >= file->buf_end) {
3657 file->buf_ptr = p;
3658 handle_eob();
3659 p = file->buf_ptr;
3660 if (p >= file->buf_end)
3661 goto parse_eof;
3662 else
3663 goto redo_no_start;
3664 } else {
3665 file->buf_ptr = p;
3666 ch = *p;
3667 handle_stray();
3668 p = file->buf_ptr;
3669 goto redo_no_start;
3671 parse_eof:
3673 TCCState *s1 = tcc_state;
3674 if ((parse_flags & PARSE_FLAG_LINEFEED)
3675 && !(tok_flags & TOK_FLAG_EOF)) {
3676 tok_flags |= TOK_FLAG_EOF;
3677 tok = TOK_LINEFEED;
3678 goto keep_tok_flags;
3679 } else if (s1->include_stack_ptr == s1->include_stack ||
3680 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3681 /* no include left : end of file. */
3682 tok = TOK_EOF;
3683 } else {
3684 tok_flags &= ~TOK_FLAG_EOF;
3685 /* pop include file */
3687 /* test if previous '#endif' was after a #ifdef at
3688 start of file */
3689 if (tok_flags & TOK_FLAG_ENDIF) {
3690 #ifdef INC_DEBUG
3691 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3692 #endif
3693 add_cached_include(s1, file->inc_type, file->inc_filename,
3694 file->ifndef_macro_saved);
3697 /* add end of include file debug info */
3698 if (do_debug) {
3699 put_stabd(N_EINCL, 0, 0);
3701 /* pop include stack */
3702 tcc_close(file);
3703 s1->include_stack_ptr--;
3704 file = *s1->include_stack_ptr;
3705 p = file->buf_ptr;
3706 goto redo_no_start;
3709 break;
3711 case '\n':
3712 file->line_num++;
3713 tok_flags |= TOK_FLAG_BOL;
3714 p++;
3715 if (0 == (parse_flags & PARSE_FLAG_LINEFEED))
3716 goto redo_no_start;
3717 tok = TOK_LINEFEED;
3718 goto keep_tok_flags;
3720 case '#':
3721 /* XXX: simplify */
3722 PEEKC(c, p);
3723 if ((tok_flags & TOK_FLAG_BOL) &&
3724 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3725 file->buf_ptr = p;
3726 preprocess(tok_flags & TOK_FLAG_BOF);
3727 p = file->buf_ptr;
3728 goto redo_no_start;
3729 } else {
3730 if (c == '#') {
3731 p++;
3732 tok = TOK_TWOSHARPS;
3733 } else {
3734 if (parse_flags & PARSE_FLAG_ASM_COMMENTS) {
3735 p = parse_line_comment(p - 1);
3736 goto redo_no_start;
3737 } else {
3738 tok = '#';
3742 break;
3744 case 'a': case 'b': case 'c': case 'd':
3745 case 'e': case 'f': case 'g': case 'h':
3746 case 'i': case 'j': case 'k': case 'l':
3747 case 'm': case 'n': case 'o': case 'p':
3748 case 'q': case 'r': case 's': case 't':
3749 case 'u': case 'v': case 'w': case 'x':
3750 case 'y': case 'z':
3751 case 'A': case 'B': case 'C': case 'D':
3752 case 'E': case 'F': case 'G': case 'H':
3753 case 'I': case 'J': case 'K':
3754 case 'M': case 'N': case 'O': case 'P':
3755 case 'Q': case 'R': case 'S': case 'T':
3756 case 'U': case 'V': case 'W': case 'X':
3757 case 'Y': case 'Z':
3758 case '_':
3759 parse_ident_fast:
3760 p1 = p;
3761 h = TOK_HASH_INIT;
3762 h = TOK_HASH_FUNC(h, c);
3763 p++;
3764 for(;;) {
3765 c = *p;
3766 if (!isidnum_table[c])
3767 break;
3768 h = TOK_HASH_FUNC(h, c);
3769 p++;
3771 if (c != '\\') {
3772 TokenSym **pts;
3773 int len;
3775 /* fast case : no stray found, so we have the full token
3776 and we have already hashed it */
3777 len = p - p1;
3778 h &= (TOK_HASH_SIZE - 1);
3779 pts = &hash_ident[h];
3780 for(;;) {
3781 ts = *pts;
3782 if (!ts)
3783 break;
3784 if (ts->len == len && !memcmp(ts->str, p1, len))
3785 goto token_found;
3786 pts = &(ts->hash_next);
3788 ts = tok_alloc_new(pts, p1, len);
3789 token_found: ;
3790 } else {
3791 /* slower case */
3792 cstr_reset(&tokcstr);
3794 while (p1 < p) {
3795 cstr_ccat(&tokcstr, *p1);
3796 p1++;
3798 p--;
3799 PEEKC(c, p);
3800 parse_ident_slow:
3801 while (isidnum_table[c]) {
3802 cstr_ccat(&tokcstr, c);
3803 PEEKC(c, p);
3805 ts = tok_alloc(tokcstr.data, tokcstr.size);
3807 tok = ts->tok;
3808 break;
3809 case 'L':
3810 t = p[1];
3811 if (t != '\\' && t != '\'' && t != '\"') {
3812 /* fast case */
3813 goto parse_ident_fast;
3814 } else {
3815 PEEKC(c, p);
3816 if (c == '\'' || c == '\"') {
3817 is_long = 1;
3818 goto str_const;
3819 } else {
3820 cstr_reset(&tokcstr);
3821 cstr_ccat(&tokcstr, 'L');
3822 goto parse_ident_slow;
3825 break;
3826 case '0': case '1': case '2': case '3':
3827 case '4': case '5': case '6': case '7':
3828 case '8': case '9':
3830 cstr_reset(&tokcstr);
3831 /* after the first digit, accept digits, alpha, '.' or sign if
3832 prefixed by 'eEpP' */
3833 parse_num:
3834 for(;;) {
3835 t = c;
3836 cstr_ccat(&tokcstr, c);
3837 PEEKC(c, p);
3838 if (!(isnum(c) || isid(c) || c == '.' ||
3839 ((c == '+' || c == '-') &&
3840 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3841 break;
3843 /* We add a trailing '\0' to ease parsing */
3844 cstr_ccat(&tokcstr, '\0');
3845 tokc.cstr = &tokcstr;
3846 tok = TOK_PPNUM;
3847 break;
3848 case '.':
3849 /* special dot handling because it can also start a number */
3850 PEEKC(c, p);
3851 if (isnum(c)) {
3852 cstr_reset(&tokcstr);
3853 cstr_ccat(&tokcstr, '.');
3854 goto parse_num;
3855 } else if (c == '.') {
3856 PEEKC(c, p);
3857 if (c != '.')
3858 expect("'.'");
3859 PEEKC(c, p);
3860 tok = TOK_DOTS;
3861 } else {
3862 tok = '.';
3864 break;
3865 case '\'':
3866 case '\"':
3867 is_long = 0;
3868 str_const:
3870 CString str;
3871 int sep;
3873 sep = c;
3875 /* parse the string */
3876 cstr_new(&str);
3877 p = parse_pp_string(p, sep, &str);
3878 cstr_ccat(&str, '\0');
3880 /* eval the escape (should be done as TOK_PPNUM) */
3881 cstr_reset(&tokcstr);
3882 parse_escape_string(&tokcstr, str.data, is_long);
3883 cstr_free(&str);
3885 if (sep == '\'') {
3886 int char_size;
3887 /* XXX: make it portable */
3888 if (!is_long)
3889 char_size = 1;
3890 else
3891 char_size = sizeof(nwchar_t);
3892 if (tokcstr.size <= char_size)
3893 error("empty character constant");
3894 if (tokcstr.size > 2 * char_size)
3895 warning("multi-character character constant");
3896 if (!is_long) {
3897 tokc.i = *(int8_t *)tokcstr.data;
3898 tok = TOK_CCHAR;
3899 } else {
3900 tokc.i = *(nwchar_t *)tokcstr.data;
3901 tok = TOK_LCHAR;
3903 } else {
3904 tokc.cstr = &tokcstr;
3905 if (!is_long)
3906 tok = TOK_STR;
3907 else
3908 tok = TOK_LSTR;
3911 break;
3913 case '<':
3914 PEEKC(c, p);
3915 if (c == '=') {
3916 p++;
3917 tok = TOK_LE;
3918 } else if (c == '<') {
3919 PEEKC(c, p);
3920 if (c == '=') {
3921 p++;
3922 tok = TOK_A_SHL;
3923 } else {
3924 tok = TOK_SHL;
3926 } else {
3927 tok = TOK_LT;
3929 break;
3931 case '>':
3932 PEEKC(c, p);
3933 if (c == '=') {
3934 p++;
3935 tok = TOK_GE;
3936 } else if (c == '>') {
3937 PEEKC(c, p);
3938 if (c == '=') {
3939 p++;
3940 tok = TOK_A_SAR;
3941 } else {
3942 tok = TOK_SAR;
3944 } else {
3945 tok = TOK_GT;
3947 break;
3949 case '&':
3950 PEEKC(c, p);
3951 if (c == '&') {
3952 p++;
3953 tok = TOK_LAND;
3954 } else if (c == '=') {
3955 p++;
3956 tok = TOK_A_AND;
3957 } else {
3958 tok = '&';
3960 break;
3962 case '|':
3963 PEEKC(c, p);
3964 if (c == '|') {
3965 p++;
3966 tok = TOK_LOR;
3967 } else if (c == '=') {
3968 p++;
3969 tok = TOK_A_OR;
3970 } else {
3971 tok = '|';
3973 break;
3975 case '+':
3976 PEEKC(c, p);
3977 if (c == '+') {
3978 p++;
3979 tok = TOK_INC;
3980 } else if (c == '=') {
3981 p++;
3982 tok = TOK_A_ADD;
3983 } else {
3984 tok = '+';
3986 break;
3988 case '-':
3989 PEEKC(c, p);
3990 if (c == '-') {
3991 p++;
3992 tok = TOK_DEC;
3993 } else if (c == '=') {
3994 p++;
3995 tok = TOK_A_SUB;
3996 } else if (c == '>') {
3997 p++;
3998 tok = TOK_ARROW;
3999 } else {
4000 tok = '-';
4002 break;
4004 PARSE2('!', '!', '=', TOK_NE)
4005 PARSE2('=', '=', '=', TOK_EQ)
4006 PARSE2('*', '*', '=', TOK_A_MUL)
4007 PARSE2('%', '%', '=', TOK_A_MOD)
4008 PARSE2('^', '^', '=', TOK_A_XOR)
4010 /* comments or operator */
4011 case '/':
4012 PEEKC(c, p);
4013 if (c == '*') {
4014 p = parse_comment(p);
4015 goto redo_no_start;
4016 } else if (c == '/') {
4017 p = parse_line_comment(p);
4018 goto redo_no_start;
4019 } else if (c == '=') {
4020 p++;
4021 tok = TOK_A_DIV;
4022 } else {
4023 tok = '/';
4025 break;
4027 /* simple tokens */
4028 case '(':
4029 case ')':
4030 case '[':
4031 case ']':
4032 case '{':
4033 case '}':
4034 case ',':
4035 case ';':
4036 case ':':
4037 case '?':
4038 case '~':
4039 case '$': /* only used in assembler */
4040 case '@': /* dito */
4041 tok = c;
4042 p++;
4043 break;
4044 default:
4045 error("unrecognized character \\x%02x", c);
4046 break;
4048 tok_flags = 0;
4049 keep_tok_flags:
4050 file->buf_ptr = p;
4051 #if defined(PARSE_DEBUG)
4052 printf("token = %s\n", get_tok_str(tok, &tokc));
4053 #endif
4056 /* return next token without macro substitution. Can read input from
4057 macro_ptr buffer */
4058 static void next_nomacro(void)
4060 if (macro_ptr) {
4061 redo:
4062 tok = *macro_ptr;
4063 if (tok) {
4064 TOK_GET(tok, macro_ptr, tokc);
4065 if (tok == TOK_LINENUM) {
4066 file->line_num = tokc.i;
4067 goto redo;
4070 } else {
4071 next_nomacro1();
4075 /* substitute args in macro_str and return allocated string */
4076 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
4078 int *st, last_tok, t, notfirst;
4079 Sym *s;
4080 CValue cval;
4081 TokenString str;
4082 CString cstr;
4084 tok_str_new(&str);
4085 last_tok = 0;
4086 while(1) {
4087 TOK_GET(t, macro_str, cval);
4088 if (!t)
4089 break;
4090 if (t == '#') {
4091 /* stringize */
4092 TOK_GET(t, macro_str, cval);
4093 if (!t)
4094 break;
4095 s = sym_find2(args, t);
4096 if (s) {
4097 cstr_new(&cstr);
4098 st = (int *)s->c;
4099 notfirst = 0;
4100 while (*st) {
4101 if (notfirst)
4102 cstr_ccat(&cstr, ' ');
4103 TOK_GET(t, st, cval);
4104 cstr_cat(&cstr, get_tok_str(t, &cval));
4105 #ifndef PP_NOSPACES
4106 notfirst = 1;
4107 #endif
4109 cstr_ccat(&cstr, '\0');
4110 #ifdef PP_DEBUG
4111 printf("stringize: %s\n", (char *)cstr.data);
4112 #endif
4113 /* add string */
4114 cval.cstr = &cstr;
4115 tok_str_add2(&str, TOK_STR, &cval);
4116 cstr_free(&cstr);
4117 } else {
4118 tok_str_add2(&str, t, &cval);
4120 } else if (t >= TOK_IDENT) {
4121 s = sym_find2(args, t);
4122 if (s) {
4123 st = (int *)s->c;
4124 /* if '##' is present before or after, no arg substitution */
4125 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
4126 /* special case for var arg macros : ## eats the
4127 ',' if empty VA_ARGS variable. */
4128 /* XXX: test of the ',' is not 100%
4129 reliable. should fix it to avoid security
4130 problems */
4131 if (gnu_ext && s->type.t &&
4132 last_tok == TOK_TWOSHARPS &&
4133 str.len >= 2 && str.str[str.len - 2] == ',') {
4134 if (*st == 0) {
4135 /* suppress ',' '##' */
4136 str.len -= 2;
4137 } else {
4138 /* suppress '##' and add variable */
4139 str.len--;
4140 goto add_var;
4142 } else {
4143 int t1;
4144 add_var:
4145 for(;;) {
4146 TOK_GET(t1, st, cval);
4147 if (!t1)
4148 break;
4149 tok_str_add2(&str, t1, &cval);
4152 } else {
4153 /* NOTE: the stream cannot be read when macro
4154 substituing an argument */
4155 macro_subst(&str, nested_list, st, NULL);
4157 } else {
4158 tok_str_add(&str, t);
4160 } else {
4161 tok_str_add2(&str, t, &cval);
4163 last_tok = t;
4165 tok_str_add(&str, 0);
4166 return str.str;
4169 static char const ab_month_name[12][4] =
4171 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4172 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4175 /* do macro substitution of current token with macro 's' and add
4176 result to (tok_str,tok_len). 'nested_list' is the list of all
4177 macros we got inside to avoid recursing. Return non zero if no
4178 substitution needs to be done */
4179 static int macro_subst_tok(TokenString *tok_str,
4180 Sym **nested_list, Sym *s, struct macro_level **can_read_stream)
4182 Sym *args, *sa, *sa1;
4183 int mstr_allocated, parlevel, *mstr, t, t1;
4184 TokenString str;
4185 char *cstrval;
4186 CValue cval;
4187 CString cstr;
4188 char buf[32];
4190 /* if symbol is a macro, prepare substitution */
4191 /* special macros */
4192 if (tok == TOK___LINE__) {
4193 snprintf(buf, sizeof(buf), "%d", file->line_num);
4194 cstrval = buf;
4195 t1 = TOK_PPNUM;
4196 goto add_cstr1;
4197 } else if (tok == TOK___FILE__) {
4198 cstrval = file->filename;
4199 goto add_cstr;
4200 } else if (tok == TOK___DATE__ || tok == TOK___TIME__) {
4201 time_t ti;
4202 struct tm *tm;
4204 time(&ti);
4205 tm = localtime(&ti);
4206 if (tok == TOK___DATE__) {
4207 snprintf(buf, sizeof(buf), "%s %2d %d",
4208 ab_month_name[tm->tm_mon], tm->tm_mday, tm->tm_year + 1900);
4209 } else {
4210 snprintf(buf, sizeof(buf), "%02d:%02d:%02d",
4211 tm->tm_hour, tm->tm_min, tm->tm_sec);
4213 cstrval = buf;
4214 add_cstr:
4215 t1 = TOK_STR;
4216 add_cstr1:
4217 cstr_new(&cstr);
4218 cstr_cat(&cstr, cstrval);
4219 cstr_ccat(&cstr, '\0');
4220 cval.cstr = &cstr;
4221 tok_str_add2(tok_str, t1, &cval);
4222 cstr_free(&cstr);
4223 } else {
4224 mstr = (int *)s->c;
4225 mstr_allocated = 0;
4226 if (s->type.t == MACRO_FUNC) {
4227 /* NOTE: we do not use next_nomacro to avoid eating the
4228 next token. XXX: find better solution */
4229 redo:
4230 if (macro_ptr) {
4231 t = *macro_ptr;
4232 if (t == 0 && can_read_stream) {
4233 /* end of macro stream: we must look at the token
4234 after in the file */
4235 struct macro_level *ml = *can_read_stream;
4236 macro_ptr = NULL;
4237 if (ml)
4239 macro_ptr = ml->p;
4240 ml->p = NULL;
4241 *can_read_stream = ml -> prev;
4243 goto redo;
4245 } else {
4246 /* XXX: incorrect with comments */
4247 ch = file->buf_ptr[0];
4248 while (is_space(ch) || ch == '\n')
4249 cinp();
4250 t = ch;
4252 if (t != '(') /* no macro subst */
4253 return -1;
4255 /* argument macro */
4256 next_nomacro();
4257 next_nomacro();
4258 args = NULL;
4259 sa = s->next;
4260 /* NOTE: empty args are allowed, except if no args */
4261 for(;;) {
4262 /* handle '()' case */
4263 if (!args && !sa && tok == ')')
4264 break;
4265 if (!sa)
4266 error("macro '%s' used with too many args",
4267 get_tok_str(s->v, 0));
4268 tok_str_new(&str);
4269 parlevel = 0;
4270 /* NOTE: non zero sa->t indicates VA_ARGS */
4271 while ((parlevel > 0 ||
4272 (tok != ')' &&
4273 (tok != ',' || sa->type.t))) &&
4274 tok != -1) {
4275 if (tok == '(')
4276 parlevel++;
4277 else if (tok == ')')
4278 parlevel--;
4279 if (tok != TOK_LINEFEED)
4280 tok_str_add2(&str, tok, &tokc);
4281 next_nomacro();
4283 tok_str_add(&str, 0);
4284 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
4285 sa = sa->next;
4286 if (tok == ')') {
4287 /* special case for gcc var args: add an empty
4288 var arg argument if it is omitted */
4289 if (sa && sa->type.t && gnu_ext)
4290 continue;
4291 else
4292 break;
4294 if (tok != ',')
4295 expect(",");
4296 next_nomacro();
4298 if (sa) {
4299 error("macro '%s' used with too few args",
4300 get_tok_str(s->v, 0));
4303 /* now subst each arg */
4304 mstr = macro_arg_subst(nested_list, mstr, args);
4305 /* free memory */
4306 sa = args;
4307 while (sa) {
4308 sa1 = sa->prev;
4309 tok_str_free((int *)sa->c);
4310 sym_free(sa);
4311 sa = sa1;
4313 mstr_allocated = 1;
4315 sym_push2(nested_list, s->v, 0, 0);
4316 macro_subst(tok_str, nested_list, mstr, can_read_stream);
4317 /* pop nested defined symbol */
4318 sa1 = *nested_list;
4319 *nested_list = sa1->prev;
4320 sym_free(sa1);
4321 if (mstr_allocated)
4322 tok_str_free(mstr);
4324 return 0;
4327 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4328 return the resulting string (which must be freed). */
4329 static inline int *macro_twosharps(const int *macro_str)
4331 TokenSym *ts;
4332 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
4333 int t;
4334 const char *p1, *p2;
4335 CValue cval;
4336 TokenString macro_str1;
4337 CString cstr;
4339 start_macro_ptr = macro_str;
4340 /* we search the first '##' */
4341 for(;;) {
4342 macro_ptr1 = macro_str;
4343 TOK_GET(t, macro_str, cval);
4344 /* nothing more to do if end of string */
4345 if (t == 0)
4346 return NULL;
4347 if (*macro_str == TOK_TWOSHARPS)
4348 break;
4351 /* we saw '##', so we need more processing to handle it */
4352 cstr_new(&cstr);
4353 tok_str_new(&macro_str1);
4354 tok = t;
4355 tokc = cval;
4357 /* add all tokens seen so far */
4358 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
4359 TOK_GET(t, ptr, cval);
4360 tok_str_add2(&macro_str1, t, &cval);
4362 saved_macro_ptr = macro_ptr;
4363 /* XXX: get rid of the use of macro_ptr here */
4364 macro_ptr = (int *)macro_str;
4365 for(;;) {
4366 while (*macro_ptr == TOK_TWOSHARPS) {
4367 macro_ptr++;
4368 macro_ptr1 = macro_ptr;
4369 t = *macro_ptr;
4370 if (t) {
4371 TOK_GET(t, macro_ptr, cval);
4372 /* We concatenate the two tokens if we have an
4373 identifier or a preprocessing number */
4374 cstr_reset(&cstr);
4375 p1 = get_tok_str(tok, &tokc);
4376 cstr_cat(&cstr, p1);
4377 p2 = get_tok_str(t, &cval);
4378 cstr_cat(&cstr, p2);
4379 cstr_ccat(&cstr, '\0');
4381 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
4382 (t >= TOK_IDENT || t == TOK_PPNUM)) {
4383 if (tok == TOK_PPNUM) {
4384 /* if number, then create a number token */
4385 /* NOTE: no need to allocate because
4386 tok_str_add2() does it */
4387 cstr_reset(&tokcstr);
4388 tokcstr = cstr;
4389 cstr_new(&cstr);
4390 tokc.cstr = &tokcstr;
4391 } else {
4392 /* if identifier, we must do a test to
4393 validate we have a correct identifier */
4394 if (t == TOK_PPNUM) {
4395 const char *p;
4396 int c;
4398 p = p2;
4399 for(;;) {
4400 c = *p;
4401 if (c == '\0')
4402 break;
4403 p++;
4404 if (!isnum(c) && !isid(c))
4405 goto error_pasting;
4408 ts = tok_alloc(cstr.data, strlen(cstr.data));
4409 tok = ts->tok; /* modify current token */
4411 } else {
4412 const char *str = cstr.data;
4413 const unsigned char *q;
4415 /* we look for a valid token */
4416 /* XXX: do more extensive checks */
4417 if (!strcmp(str, ">>=")) {
4418 tok = TOK_A_SAR;
4419 } else if (!strcmp(str, "<<=")) {
4420 tok = TOK_A_SHL;
4421 } else if (strlen(str) == 2) {
4422 /* search in two bytes table */
4423 q = tok_two_chars;
4424 for(;;) {
4425 if (!*q)
4426 goto error_pasting;
4427 if (q[0] == str[0] && q[1] == str[1])
4428 break;
4429 q += 3;
4431 tok = q[2];
4432 } else {
4433 error_pasting:
4434 /* NOTE: because get_tok_str use a static buffer,
4435 we must save it */
4436 cstr_reset(&cstr);
4437 p1 = get_tok_str(tok, &tokc);
4438 cstr_cat(&cstr, p1);
4439 cstr_ccat(&cstr, '\0');
4440 p2 = get_tok_str(t, &cval);
4441 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
4442 /* cannot merge tokens: just add them separately */
4443 tok_str_add2(&macro_str1, tok, &tokc);
4444 /* XXX: free associated memory ? */
4445 tok = t;
4446 tokc = cval;
4451 tok_str_add2(&macro_str1, tok, &tokc);
4452 next_nomacro();
4453 if (tok == 0)
4454 break;
4456 macro_ptr = (int *)saved_macro_ptr;
4457 cstr_free(&cstr);
4458 tok_str_add(&macro_str1, 0);
4459 return macro_str1.str;
4463 /* do macro substitution of macro_str and add result to
4464 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4465 inside to avoid recursing. */
4466 static void macro_subst(TokenString *tok_str, Sym **nested_list,
4467 const int *macro_str, struct macro_level ** can_read_stream)
4469 Sym *s;
4470 int *macro_str1;
4471 const int *ptr;
4472 int t, ret;
4473 CValue cval;
4474 struct macro_level ml;
4476 /* first scan for '##' operator handling */
4477 ptr = macro_str;
4478 macro_str1 = macro_twosharps(ptr);
4479 if (macro_str1)
4480 ptr = macro_str1;
4481 while (1) {
4482 /* NOTE: ptr == NULL can only happen if tokens are read from
4483 file stream due to a macro function call */
4484 if (ptr == NULL)
4485 break;
4486 TOK_GET(t, ptr, cval);
4487 if (t == 0)
4488 break;
4489 s = define_find(t);
4490 if (s != NULL) {
4491 /* if nested substitution, do nothing */
4492 if (sym_find2(*nested_list, t))
4493 goto no_subst;
4494 ml.p = macro_ptr;
4495 if (can_read_stream)
4496 ml.prev = *can_read_stream, *can_read_stream = &ml;
4497 macro_ptr = (int *)ptr;
4498 tok = t;
4499 ret = macro_subst_tok(tok_str, nested_list, s, can_read_stream);
4500 ptr = (int *)macro_ptr;
4501 macro_ptr = ml.p;
4502 if (can_read_stream && *can_read_stream == &ml)
4503 *can_read_stream = ml.prev;
4504 if (ret != 0)
4505 goto no_subst;
4506 } else {
4507 no_subst:
4508 tok_str_add2(tok_str, t, &cval);
4511 if (macro_str1)
4512 tok_str_free(macro_str1);
4515 /* return next token with macro substitution */
4516 static void next(void)
4518 Sym *nested_list, *s;
4519 TokenString str;
4520 struct macro_level *ml;
4522 redo:
4523 next_nomacro();
4524 if (!macro_ptr) {
4525 /* if not reading from macro substituted string, then try
4526 to substitute macros */
4527 if (tok >= TOK_IDENT &&
4528 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4529 s = define_find(tok);
4530 if (s) {
4531 /* we have a macro: we try to substitute */
4532 tok_str_new(&str);
4533 nested_list = NULL;
4534 ml = NULL;
4535 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
4536 /* substitution done, NOTE: maybe empty */
4537 tok_str_add(&str, 0);
4538 macro_ptr = str.str;
4539 macro_ptr_allocated = str.str;
4540 goto redo;
4544 } else {
4545 if (tok == 0) {
4546 /* end of macro or end of unget buffer */
4547 if (unget_buffer_enabled) {
4548 macro_ptr = unget_saved_macro_ptr;
4549 unget_buffer_enabled = 0;
4550 } else {
4551 /* end of macro string: free it */
4552 tok_str_free(macro_ptr_allocated);
4553 macro_ptr = NULL;
4555 goto redo;
4559 /* convert preprocessor tokens into C tokens */
4560 if (tok == TOK_PPNUM &&
4561 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4562 parse_number((char *)tokc.cstr->data);
4566 /* push back current token and set current token to 'last_tok'. Only
4567 identifier case handled for labels. */
4568 static inline void unget_tok(int last_tok)
4570 int i, n;
4571 int *q;
4572 unget_saved_macro_ptr = macro_ptr;
4573 unget_buffer_enabled = 1;
4574 q = unget_saved_buffer;
4575 macro_ptr = q;
4576 *q++ = tok;
4577 n = tok_ext_size(tok) - 1;
4578 for(i=0;i<n;i++)
4579 *q++ = tokc.tab[i];
4580 *q = 0; /* end of token string */
4581 tok = last_tok;
4585 void swap(int *p, int *q)
4587 int t;
4588 t = *p;
4589 *p = *q;
4590 *q = t;
4593 void vsetc(CType *type, int r, CValue *vc)
4595 int v;
4597 if (vtop >= vstack + (VSTACK_SIZE - 1))
4598 error("memory full");
4599 /* cannot let cpu flags if other instruction are generated. Also
4600 avoid leaving VT_JMP anywhere except on the top of the stack
4601 because it would complicate the code generator. */
4602 if (vtop >= vstack) {
4603 v = vtop->r & VT_VALMASK;
4604 if (v == VT_CMP || (v & ~1) == VT_JMP)
4605 gv(RC_INT);
4607 vtop++;
4608 vtop->type = *type;
4609 vtop->r = r;
4610 vtop->r2 = VT_CONST;
4611 vtop->c = *vc;
4614 /* push integer constant */
4615 void vpushi(int v)
4617 CValue cval;
4618 cval.i = v;
4619 vsetc(&int_type, VT_CONST, &cval);
4622 /* Return a static symbol pointing to a section */
4623 static Sym *get_sym_ref(CType *type, Section *sec,
4624 unsigned long offset, unsigned long size)
4626 int v;
4627 Sym *sym;
4629 v = anon_sym++;
4630 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4631 sym->type.ref = type->ref;
4632 sym->r = VT_CONST | VT_SYM;
4633 put_extern_sym(sym, sec, offset, size);
4634 return sym;
4637 /* push a reference to a section offset by adding a dummy symbol */
4638 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4640 CValue cval;
4642 cval.ul = 0;
4643 vsetc(type, VT_CONST | VT_SYM, &cval);
4644 vtop->sym = get_sym_ref(type, sec, offset, size);
4647 /* define a new external reference to a symbol 'v' of type 'u' */
4648 static Sym *external_global_sym(int v, CType *type, int r)
4650 Sym *s;
4652 s = sym_find(v);
4653 if (!s) {
4654 /* push forward reference */
4655 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4656 s->type.ref = type->ref;
4657 s->r = r | VT_CONST | VT_SYM;
4659 return s;
4662 /* define a new external reference to a symbol 'v' of type 'u' */
4663 static Sym *external_sym(int v, CType *type, int r)
4665 Sym *s;
4667 s = sym_find(v);
4668 if (!s) {
4669 /* push forward reference */
4670 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4671 s->type.t |= VT_EXTERN;
4672 } else {
4673 if (!is_compatible_types(&s->type, type))
4674 error("incompatible types for redefinition of '%s'",
4675 get_tok_str(v, NULL));
4677 return s;
4680 /* push a reference to global symbol v */
4681 static void vpush_global_sym(CType *type, int v)
4683 Sym *sym;
4684 CValue cval;
4686 sym = external_global_sym(v, type, 0);
4687 cval.ul = 0;
4688 vsetc(type, VT_CONST | VT_SYM, &cval);
4689 vtop->sym = sym;
4692 void vset(CType *type, int r, int v)
4694 CValue cval;
4696 cval.i = v;
4697 vsetc(type, r, &cval);
4700 void vseti(int r, int v)
4702 CType type;
4703 type.t = VT_INT;
4704 vset(&type, r, v);
4707 void vswap(void)
4709 SValue tmp;
4711 tmp = vtop[0];
4712 vtop[0] = vtop[-1];
4713 vtop[-1] = tmp;
4716 void vpushv(SValue *v)
4718 if (vtop >= vstack + (VSTACK_SIZE - 1))
4719 error("memory full");
4720 vtop++;
4721 *vtop = *v;
4724 void vdup(void)
4726 vpushv(vtop);
4729 /* save r to the memory stack, and mark it as being free */
4730 void save_reg(int r)
4732 int l, saved, size, align;
4733 SValue *p, sv;
4734 CType *type;
4736 /* modify all stack values */
4737 saved = 0;
4738 l = 0;
4739 for(p=vstack;p<=vtop;p++) {
4740 if ((p->r & VT_VALMASK) == r ||
4741 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
4742 /* must save value on stack if not already done */
4743 if (!saved) {
4744 /* NOTE: must reload 'r' because r might be equal to r2 */
4745 r = p->r & VT_VALMASK;
4746 /* store register in the stack */
4747 type = &p->type;
4748 if ((p->r & VT_LVAL) ||
4749 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4750 type = &int_type;
4751 size = type_size(type, &align);
4752 loc = (loc - size) & -align;
4753 sv.type.t = type->t;
4754 sv.r = VT_LOCAL | VT_LVAL;
4755 sv.c.ul = loc;
4756 store(r, &sv);
4757 #ifdef TCC_TARGET_I386
4758 /* x86 specific: need to pop fp register ST0 if saved */
4759 if (r == TREG_ST0) {
4760 o(0xd9dd); /* fstp %st(1) */
4762 #endif
4763 /* special long long case */
4764 if ((type->t & VT_BTYPE) == VT_LLONG) {
4765 sv.c.ul += 4;
4766 store(p->r2, &sv);
4768 l = loc;
4769 saved = 1;
4771 /* mark that stack entry as being saved on the stack */
4772 if (p->r & VT_LVAL) {
4773 /* also clear the bounded flag because the
4774 relocation address of the function was stored in
4775 p->c.ul */
4776 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4777 } else {
4778 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4780 p->r2 = VT_CONST;
4781 p->c.ul = l;
4786 /* find a register of class 'rc2' with at most one reference on stack.
4787 * If none, call get_reg(rc) */
4788 int get_reg_ex(int rc, int rc2)
4790 int r;
4791 SValue *p;
4793 for(r=0;r<NB_REGS;r++) {
4794 if (reg_classes[r] & rc2) {
4795 int n;
4796 n=0;
4797 for(p = vstack; p <= vtop; p++) {
4798 if ((p->r & VT_VALMASK) == r ||
4799 (p->r2 & VT_VALMASK) == r)
4800 n++;
4802 if (n <= 1)
4803 return r;
4806 return get_reg(rc);
4809 /* find a free register of class 'rc'. If none, save one register */
4810 int get_reg(int rc)
4812 int r;
4813 SValue *p;
4815 /* find a free register */
4816 for(r=0;r<NB_REGS;r++) {
4817 if (reg_classes[r] & rc) {
4818 for(p=vstack;p<=vtop;p++) {
4819 if ((p->r & VT_VALMASK) == r ||
4820 (p->r2 & VT_VALMASK) == r)
4821 goto notfound;
4823 return r;
4825 notfound: ;
4828 /* no register left : free the first one on the stack (VERY
4829 IMPORTANT to start from the bottom to ensure that we don't
4830 spill registers used in gen_opi()) */
4831 for(p=vstack;p<=vtop;p++) {
4832 r = p->r & VT_VALMASK;
4833 if (r < VT_CONST && (reg_classes[r] & rc))
4834 goto save_found;
4835 /* also look at second register (if long long) */
4836 r = p->r2 & VT_VALMASK;
4837 if (r < VT_CONST && (reg_classes[r] & rc)) {
4838 save_found:
4839 save_reg(r);
4840 return r;
4843 /* Should never comes here */
4844 return -1;
4847 /* save registers up to (vtop - n) stack entry */
4848 void save_regs(int n)
4850 int r;
4851 SValue *p, *p1;
4852 p1 = vtop - n;
4853 for(p = vstack;p <= p1; p++) {
4854 r = p->r & VT_VALMASK;
4855 if (r < VT_CONST) {
4856 save_reg(r);
4861 /* move register 's' to 'r', and flush previous value of r to memory
4862 if needed */
4863 void move_reg(int r, int s)
4865 SValue sv;
4867 if (r != s) {
4868 save_reg(r);
4869 sv.type.t = VT_INT;
4870 sv.r = s;
4871 sv.c.ul = 0;
4872 load(r, &sv);
4876 /* get address of vtop (vtop MUST BE an lvalue) */
4877 void gaddrof(void)
4879 vtop->r &= ~VT_LVAL;
4880 /* tricky: if saved lvalue, then we can go back to lvalue */
4881 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4882 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4885 #ifdef CONFIG_TCC_BCHECK
4886 /* generate lvalue bound code */
4887 void gbound(void)
4889 int lval_type;
4890 CType type1;
4892 vtop->r &= ~VT_MUSTBOUND;
4893 /* if lvalue, then use checking code before dereferencing */
4894 if (vtop->r & VT_LVAL) {
4895 /* if not VT_BOUNDED value, then make one */
4896 if (!(vtop->r & VT_BOUNDED)) {
4897 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4898 /* must save type because we must set it to int to get pointer */
4899 type1 = vtop->type;
4900 vtop->type.t = VT_INT;
4901 gaddrof();
4902 vpushi(0);
4903 gen_bounded_ptr_add();
4904 vtop->r |= lval_type;
4905 vtop->type = type1;
4907 /* then check for dereferencing */
4908 gen_bounded_ptr_deref();
4911 #endif
4913 /* store vtop a register belonging to class 'rc'. lvalues are
4914 converted to values. Cannot be used if cannot be converted to
4915 register value (such as structures). */
4916 int gv(int rc)
4918 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4919 unsigned long long ll;
4921 /* NOTE: get_reg can modify vstack[] */
4922 if (vtop->type.t & VT_BITFIELD) {
4923 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4924 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4925 /* remove bit field info to avoid loops */
4926 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4927 /* generate shifts */
4928 vpushi(32 - (bit_pos + bit_size));
4929 gen_op(TOK_SHL);
4930 vpushi(32 - bit_size);
4931 /* NOTE: transformed to SHR if unsigned */
4932 gen_op(TOK_SAR);
4933 r = gv(rc);
4934 } else {
4935 if (is_float(vtop->type.t) &&
4936 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4937 Sym *sym;
4938 int *ptr;
4939 unsigned long offset;
4940 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4941 CValue check;
4942 #endif
4944 /* XXX: unify with initializers handling ? */
4945 /* CPUs usually cannot use float constants, so we store them
4946 generically in data segment */
4947 size = type_size(&vtop->type, &align);
4948 offset = (data_section->data_offset + align - 1) & -align;
4949 data_section->data_offset = offset;
4950 /* XXX: not portable yet */
4951 #ifdef __i386__
4952 /* Zero pad x87 tenbyte long doubles */
4953 if (size == 12)
4954 vtop->c.tab[2] &= 0xffff;
4955 #endif
4956 ptr = section_ptr_add(data_section, size);
4957 size = size >> 2;
4958 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4959 check.d = 1;
4960 if(check.tab[0])
4961 for(i=0;i<size;i++)
4962 ptr[i] = vtop->c.tab[size-1-i];
4963 else
4964 #endif
4965 for(i=0;i<size;i++)
4966 ptr[i] = vtop->c.tab[i];
4967 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4968 vtop->r |= VT_LVAL | VT_SYM;
4969 vtop->sym = sym;
4970 vtop->c.ul = 0;
4972 #ifdef CONFIG_TCC_BCHECK
4973 if (vtop->r & VT_MUSTBOUND)
4974 gbound();
4975 #endif
4977 r = vtop->r & VT_VALMASK;
4978 /* need to reload if:
4979 - constant
4980 - lvalue (need to dereference pointer)
4981 - already a register, but not in the right class */
4982 if (r >= VT_CONST ||
4983 (vtop->r & VT_LVAL) ||
4984 !(reg_classes[r] & rc) ||
4985 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4986 !(reg_classes[vtop->r2] & rc))) {
4987 r = get_reg(rc);
4988 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4989 /* two register type load : expand to two words
4990 temporarily */
4991 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4992 /* load constant */
4993 ll = vtop->c.ull;
4994 vtop->c.ui = ll; /* first word */
4995 load(r, vtop);
4996 vtop->r = r; /* save register value */
4997 vpushi(ll >> 32); /* second word */
4998 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
4999 (vtop->r & VT_LVAL)) {
5000 /* We do not want to modifier the long long
5001 pointer here, so the safest (and less
5002 efficient) is to save all the other registers
5003 in the stack. XXX: totally inefficient. */
5004 save_regs(1);
5005 /* load from memory */
5006 load(r, vtop);
5007 vdup();
5008 vtop[-1].r = r; /* save register value */
5009 /* increment pointer to get second word */
5010 vtop->type.t = VT_INT;
5011 gaddrof();
5012 vpushi(4);
5013 gen_op('+');
5014 vtop->r |= VT_LVAL;
5015 } else {
5016 /* move registers */
5017 load(r, vtop);
5018 vdup();
5019 vtop[-1].r = r; /* save register value */
5020 vtop->r = vtop[-1].r2;
5022 /* allocate second register */
5023 rc2 = RC_INT;
5024 if (rc == RC_IRET)
5025 rc2 = RC_LRET;
5026 r2 = get_reg(rc2);
5027 load(r2, vtop);
5028 vpop();
5029 /* write second register */
5030 vtop->r2 = r2;
5031 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
5032 int t1, t;
5033 /* lvalue of scalar type : need to use lvalue type
5034 because of possible cast */
5035 t = vtop->type.t;
5036 t1 = t;
5037 /* compute memory access type */
5038 if (vtop->r & VT_LVAL_BYTE)
5039 t = VT_BYTE;
5040 else if (vtop->r & VT_LVAL_SHORT)
5041 t = VT_SHORT;
5042 if (vtop->r & VT_LVAL_UNSIGNED)
5043 t |= VT_UNSIGNED;
5044 vtop->type.t = t;
5045 load(r, vtop);
5046 /* restore wanted type */
5047 vtop->type.t = t1;
5048 } else {
5049 /* one register type load */
5050 load(r, vtop);
5053 vtop->r = r;
5054 #ifdef TCC_TARGET_C67
5055 /* uses register pairs for doubles */
5056 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
5057 vtop->r2 = r+1;
5058 #endif
5060 return r;
5063 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5064 void gv2(int rc1, int rc2)
5066 int v;
5068 /* generate more generic register first. But VT_JMP or VT_CMP
5069 values must be generated first in all cases to avoid possible
5070 reload errors */
5071 v = vtop[0].r & VT_VALMASK;
5072 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
5073 vswap();
5074 gv(rc1);
5075 vswap();
5076 gv(rc2);
5077 /* test if reload is needed for first register */
5078 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
5079 vswap();
5080 gv(rc1);
5081 vswap();
5083 } else {
5084 gv(rc2);
5085 vswap();
5086 gv(rc1);
5087 vswap();
5088 /* test if reload is needed for first register */
5089 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
5090 gv(rc2);
5095 /* expand long long on stack in two int registers */
5096 void lexpand(void)
5098 int u;
5100 u = vtop->type.t & VT_UNSIGNED;
5101 gv(RC_INT);
5102 vdup();
5103 vtop[0].r = vtop[-1].r2;
5104 vtop[0].r2 = VT_CONST;
5105 vtop[-1].r2 = VT_CONST;
5106 vtop[0].type.t = VT_INT | u;
5107 vtop[-1].type.t = VT_INT | u;
5110 #ifdef TCC_TARGET_ARM
5111 /* expand long long on stack */
5112 void lexpand_nr(void)
5114 int u,v;
5116 u = vtop->type.t & VT_UNSIGNED;
5117 vdup();
5118 vtop->r2 = VT_CONST;
5119 vtop->type.t = VT_INT | u;
5120 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
5121 if (v == VT_CONST) {
5122 vtop[-1].c.ui = vtop->c.ull;
5123 vtop->c.ui = vtop->c.ull >> 32;
5124 vtop->r = VT_CONST;
5125 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
5126 vtop->c.ui += 4;
5127 vtop->r = vtop[-1].r;
5128 } else if (v > VT_CONST) {
5129 vtop--;
5130 lexpand();
5131 } else
5132 vtop->r = vtop[-1].r2;
5133 vtop[-1].r2 = VT_CONST;
5134 vtop[-1].type.t = VT_INT | u;
5136 #endif
5138 /* build a long long from two ints */
5139 void lbuild(int t)
5141 gv2(RC_INT, RC_INT);
5142 vtop[-1].r2 = vtop[0].r;
5143 vtop[-1].type.t = t;
5144 vpop();
5147 /* rotate n first stack elements to the bottom
5148 I1 ... In -> I2 ... In I1 [top is right]
5150 void vrotb(int n)
5152 int i;
5153 SValue tmp;
5155 tmp = vtop[-n + 1];
5156 for(i=-n+1;i!=0;i++)
5157 vtop[i] = vtop[i+1];
5158 vtop[0] = tmp;
5161 /* rotate n first stack elements to the top
5162 I1 ... In -> In I1 ... I(n-1) [top is right]
5164 void vrott(int n)
5166 int i;
5167 SValue tmp;
5169 tmp = vtop[0];
5170 for(i = 0;i < n - 1; i++)
5171 vtop[-i] = vtop[-i - 1];
5172 vtop[-n + 1] = tmp;
5175 #ifdef TCC_TARGET_ARM
5176 /* like vrott but in other direction
5177 In ... I1 -> I(n-1) ... I1 In [top is right]
5179 void vnrott(int n)
5181 int i;
5182 SValue tmp;
5184 tmp = vtop[-n + 1];
5185 for(i = n - 1; i > 0; i--)
5186 vtop[-i] = vtop[-i + 1];
5187 vtop[0] = tmp;
5189 #endif
5191 /* pop stack value */
5192 void vpop(void)
5194 int v;
5195 v = vtop->r & VT_VALMASK;
5196 #ifdef TCC_TARGET_I386
5197 /* for x86, we need to pop the FP stack */
5198 if (v == TREG_ST0 && !nocode_wanted) {
5199 o(0xd9dd); /* fstp %st(1) */
5200 } else
5201 #endif
5202 if (v == VT_JMP || v == VT_JMPI) {
5203 /* need to put correct jump if && or || without test */
5204 gsym(vtop->c.ul);
5206 vtop--;
5209 /* convert stack entry to register and duplicate its value in another
5210 register */
5211 void gv_dup(void)
5213 int rc, t, r, r1;
5214 SValue sv;
5216 t = vtop->type.t;
5217 if ((t & VT_BTYPE) == VT_LLONG) {
5218 lexpand();
5219 gv_dup();
5220 vswap();
5221 vrotb(3);
5222 gv_dup();
5223 vrotb(4);
5224 /* stack: H L L1 H1 */
5225 lbuild(t);
5226 vrotb(3);
5227 vrotb(3);
5228 vswap();
5229 lbuild(t);
5230 vswap();
5231 } else {
5232 /* duplicate value */
5233 rc = RC_INT;
5234 sv.type.t = VT_INT;
5235 if (is_float(t)) {
5236 rc = RC_FLOAT;
5237 sv.type.t = t;
5239 r = gv(rc);
5240 r1 = get_reg(rc);
5241 sv.r = r;
5242 sv.c.ul = 0;
5243 load(r1, &sv); /* move r to r1 */
5244 vdup();
5245 /* duplicates value */
5246 vtop->r = r1;
5250 /* generate CPU independent (unsigned) long long operations */
5251 void gen_opl(int op)
5253 int t, a, b, op1, c, i;
5254 int func;
5255 SValue tmp;
5257 switch(op) {
5258 case '/':
5259 case TOK_PDIV:
5260 func = TOK___divdi3;
5261 goto gen_func;
5262 case TOK_UDIV:
5263 func = TOK___udivdi3;
5264 goto gen_func;
5265 case '%':
5266 func = TOK___moddi3;
5267 goto gen_func;
5268 case TOK_UMOD:
5269 func = TOK___umoddi3;
5270 gen_func:
5271 /* call generic long long function */
5272 vpush_global_sym(&func_old_type, func);
5273 vrott(3);
5274 gfunc_call(2);
5275 vpushi(0);
5276 vtop->r = REG_IRET;
5277 vtop->r2 = REG_LRET;
5278 break;
5279 case '^':
5280 case '&':
5281 case '|':
5282 case '*':
5283 case '+':
5284 case '-':
5285 t = vtop->type.t;
5286 vswap();
5287 lexpand();
5288 vrotb(3);
5289 lexpand();
5290 /* stack: L1 H1 L2 H2 */
5291 tmp = vtop[0];
5292 vtop[0] = vtop[-3];
5293 vtop[-3] = tmp;
5294 tmp = vtop[-2];
5295 vtop[-2] = vtop[-3];
5296 vtop[-3] = tmp;
5297 vswap();
5298 /* stack: H1 H2 L1 L2 */
5299 if (op == '*') {
5300 vpushv(vtop - 1);
5301 vpushv(vtop - 1);
5302 gen_op(TOK_UMULL);
5303 lexpand();
5304 /* stack: H1 H2 L1 L2 ML MH */
5305 for(i=0;i<4;i++)
5306 vrotb(6);
5307 /* stack: ML MH H1 H2 L1 L2 */
5308 tmp = vtop[0];
5309 vtop[0] = vtop[-2];
5310 vtop[-2] = tmp;
5311 /* stack: ML MH H1 L2 H2 L1 */
5312 gen_op('*');
5313 vrotb(3);
5314 vrotb(3);
5315 gen_op('*');
5316 /* stack: ML MH M1 M2 */
5317 gen_op('+');
5318 gen_op('+');
5319 } else if (op == '+' || op == '-') {
5320 /* XXX: add non carry method too (for MIPS or alpha) */
5321 if (op == '+')
5322 op1 = TOK_ADDC1;
5323 else
5324 op1 = TOK_SUBC1;
5325 gen_op(op1);
5326 /* stack: H1 H2 (L1 op L2) */
5327 vrotb(3);
5328 vrotb(3);
5329 gen_op(op1 + 1); /* TOK_xxxC2 */
5330 } else {
5331 gen_op(op);
5332 /* stack: H1 H2 (L1 op L2) */
5333 vrotb(3);
5334 vrotb(3);
5335 /* stack: (L1 op L2) H1 H2 */
5336 gen_op(op);
5337 /* stack: (L1 op L2) (H1 op H2) */
5339 /* stack: L H */
5340 lbuild(t);
5341 break;
5342 case TOK_SAR:
5343 case TOK_SHR:
5344 case TOK_SHL:
5345 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
5346 t = vtop[-1].type.t;
5347 vswap();
5348 lexpand();
5349 vrotb(3);
5350 /* stack: L H shift */
5351 c = (int)vtop->c.i;
5352 /* constant: simpler */
5353 /* NOTE: all comments are for SHL. the other cases are
5354 done by swaping words */
5355 vpop();
5356 if (op != TOK_SHL)
5357 vswap();
5358 if (c >= 32) {
5359 /* stack: L H */
5360 vpop();
5361 if (c > 32) {
5362 vpushi(c - 32);
5363 gen_op(op);
5365 if (op != TOK_SAR) {
5366 vpushi(0);
5367 } else {
5368 gv_dup();
5369 vpushi(31);
5370 gen_op(TOK_SAR);
5372 vswap();
5373 } else {
5374 vswap();
5375 gv_dup();
5376 /* stack: H L L */
5377 vpushi(c);
5378 gen_op(op);
5379 vswap();
5380 vpushi(32 - c);
5381 if (op == TOK_SHL)
5382 gen_op(TOK_SHR);
5383 else
5384 gen_op(TOK_SHL);
5385 vrotb(3);
5386 /* stack: L L H */
5387 vpushi(c);
5388 if (op == TOK_SHL)
5389 gen_op(TOK_SHL);
5390 else
5391 gen_op(TOK_SHR);
5392 gen_op('|');
5394 if (op != TOK_SHL)
5395 vswap();
5396 lbuild(t);
5397 } else {
5398 /* XXX: should provide a faster fallback on x86 ? */
5399 switch(op) {
5400 case TOK_SAR:
5401 func = TOK___sardi3;
5402 goto gen_func;
5403 case TOK_SHR:
5404 func = TOK___shrdi3;
5405 goto gen_func;
5406 case TOK_SHL:
5407 func = TOK___shldi3;
5408 goto gen_func;
5411 break;
5412 default:
5413 /* compare operations */
5414 t = vtop->type.t;
5415 vswap();
5416 lexpand();
5417 vrotb(3);
5418 lexpand();
5419 /* stack: L1 H1 L2 H2 */
5420 tmp = vtop[-1];
5421 vtop[-1] = vtop[-2];
5422 vtop[-2] = tmp;
5423 /* stack: L1 L2 H1 H2 */
5424 /* compare high */
5425 op1 = op;
5426 /* when values are equal, we need to compare low words. since
5427 the jump is inverted, we invert the test too. */
5428 if (op1 == TOK_LT)
5429 op1 = TOK_LE;
5430 else if (op1 == TOK_GT)
5431 op1 = TOK_GE;
5432 else if (op1 == TOK_ULT)
5433 op1 = TOK_ULE;
5434 else if (op1 == TOK_UGT)
5435 op1 = TOK_UGE;
5436 a = 0;
5437 b = 0;
5438 gen_op(op1);
5439 if (op1 != TOK_NE) {
5440 a = gtst(1, 0);
5442 if (op != TOK_EQ) {
5443 /* generate non equal test */
5444 /* XXX: NOT PORTABLE yet */
5445 if (a == 0) {
5446 b = gtst(0, 0);
5447 } else {
5448 #if defined(TCC_TARGET_I386)
5449 b = psym(0x850f, 0);
5450 #elif defined(TCC_TARGET_ARM)
5451 b = ind;
5452 o(0x1A000000 | encbranch(ind, 0, 1));
5453 #elif defined(TCC_TARGET_C67)
5454 error("not implemented");
5455 #else
5456 #error not supported
5457 #endif
5460 /* compare low. Always unsigned */
5461 op1 = op;
5462 if (op1 == TOK_LT)
5463 op1 = TOK_ULT;
5464 else if (op1 == TOK_LE)
5465 op1 = TOK_ULE;
5466 else if (op1 == TOK_GT)
5467 op1 = TOK_UGT;
5468 else if (op1 == TOK_GE)
5469 op1 = TOK_UGE;
5470 gen_op(op1);
5471 a = gtst(1, a);
5472 gsym(b);
5473 vseti(VT_JMPI, a);
5474 break;
5478 /* handle integer constant optimizations and various machine
5479 independent opt */
5480 void gen_opic(int op)
5482 int c1, c2, t1, t2, n;
5483 SValue *v1, *v2;
5484 long long l1, l2;
5485 typedef unsigned long long U;
5487 v1 = vtop - 1;
5488 v2 = vtop;
5489 t1 = v1->type.t & VT_BTYPE;
5490 t2 = v2->type.t & VT_BTYPE;
5491 l1 = (t1 == VT_LLONG) ? v1->c.ll : v1->c.i;
5492 l2 = (t2 == VT_LLONG) ? v2->c.ll : v2->c.i;
5494 /* currently, we cannot do computations with forward symbols */
5495 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5496 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5497 if (c1 && c2) {
5498 switch(op) {
5499 case '+': l1 += l2; break;
5500 case '-': l1 -= l2; break;
5501 case '&': l1 &= l2; break;
5502 case '^': l1 ^= l2; break;
5503 case '|': l1 |= l2; break;
5504 case '*': l1 *= l2; break;
5506 case TOK_PDIV:
5507 case '/':
5508 case '%':
5509 case TOK_UDIV:
5510 case TOK_UMOD:
5511 /* if division by zero, generate explicit division */
5512 if (l2 == 0) {
5513 if (const_wanted)
5514 error("division by zero in constant");
5515 goto general_case;
5517 switch(op) {
5518 default: l1 /= l2; break;
5519 case '%': l1 %= l2; break;
5520 case TOK_UDIV: l1 = (U)l1 / l2; break;
5521 case TOK_UMOD: l1 = (U)l1 % l2; break;
5523 break;
5524 case TOK_SHL: l1 <<= l2; break;
5525 case TOK_SHR: l1 = (U)l1 >> l2; break;
5526 case TOK_SAR: l1 >>= l2; break;
5527 /* tests */
5528 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
5529 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
5530 case TOK_EQ: l1 = l1 == l2; break;
5531 case TOK_NE: l1 = l1 != l2; break;
5532 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
5533 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
5534 case TOK_LT: l1 = l1 < l2; break;
5535 case TOK_GE: l1 = l1 >= l2; break;
5536 case TOK_LE: l1 = l1 <= l2; break;
5537 case TOK_GT: l1 = l1 > l2; break;
5538 /* logical */
5539 case TOK_LAND: l1 = l1 && l2; break;
5540 case TOK_LOR: l1 = l1 || l2; break;
5541 default:
5542 goto general_case;
5544 v1->c.ll = l1;
5545 vtop--;
5546 } else {
5547 /* if commutative ops, put c2 as constant */
5548 if (c1 && (op == '+' || op == '&' || op == '^' ||
5549 op == '|' || op == '*')) {
5550 vswap();
5551 c2 = c1; //c = c1, c1 = c2, c2 = c;
5552 l2 = l1; //l = l1, l1 = l2, l2 = l;
5554 /* Filter out NOP operations like x*1, x-0, x&-1... */
5555 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
5556 op == TOK_PDIV) &&
5557 l2 == 1) ||
5558 ((op == '+' || op == '-' || op == '|' || op == '^' ||
5559 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
5560 l2 == 0) ||
5561 (op == '&' &&
5562 l2 == -1))) {
5563 /* nothing to do */
5564 vtop--;
5565 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
5566 /* try to use shifts instead of muls or divs */
5567 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
5568 n = -1;
5569 while (l2) {
5570 l2 >>= 1;
5571 n++;
5573 vtop->c.ll = n;
5574 if (op == '*')
5575 op = TOK_SHL;
5576 else if (op == TOK_PDIV)
5577 op = TOK_SAR;
5578 else
5579 op = TOK_SHR;
5581 goto general_case;
5582 } else if (c2 && (op == '+' || op == '-') &&
5583 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
5584 (VT_CONST | VT_SYM)) {
5585 /* symbol + constant case */
5586 if (op == '-')
5587 l2 = -l2;
5588 vtop--;
5589 vtop->c.ll += l2;
5590 } else {
5591 general_case:
5592 if (!nocode_wanted) {
5593 /* call low level op generator */
5594 if (t1 == VT_LLONG || t2 == VT_LLONG)
5595 gen_opl(op);
5596 else
5597 gen_opi(op);
5598 } else {
5599 vtop--;
5605 /* generate a floating point operation with constant propagation */
5606 void gen_opif(int op)
5608 int c1, c2;
5609 SValue *v1, *v2;
5610 long double f1, f2;
5612 v1 = vtop - 1;
5613 v2 = vtop;
5614 /* currently, we cannot do computations with forward symbols */
5615 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5616 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5617 if (c1 && c2) {
5618 if (v1->type.t == VT_FLOAT) {
5619 f1 = v1->c.f;
5620 f2 = v2->c.f;
5621 } else if (v1->type.t == VT_DOUBLE) {
5622 f1 = v1->c.d;
5623 f2 = v2->c.d;
5624 } else {
5625 f1 = v1->c.ld;
5626 f2 = v2->c.ld;
5629 /* NOTE: we only do constant propagation if finite number (not
5630 NaN or infinity) (ANSI spec) */
5631 if (!ieee_finite(f1) || !ieee_finite(f2))
5632 goto general_case;
5634 switch(op) {
5635 case '+': f1 += f2; break;
5636 case '-': f1 -= f2; break;
5637 case '*': f1 *= f2; break;
5638 case '/':
5639 if (f2 == 0.0) {
5640 if (const_wanted)
5641 error("division by zero in constant");
5642 goto general_case;
5644 f1 /= f2;
5645 break;
5646 /* XXX: also handles tests ? */
5647 default:
5648 goto general_case;
5650 /* XXX: overflow test ? */
5651 if (v1->type.t == VT_FLOAT) {
5652 v1->c.f = f1;
5653 } else if (v1->type.t == VT_DOUBLE) {
5654 v1->c.d = f1;
5655 } else {
5656 v1->c.ld = f1;
5658 vtop--;
5659 } else {
5660 general_case:
5661 if (!nocode_wanted) {
5662 gen_opf(op);
5663 } else {
5664 vtop--;
5669 static int pointed_size(CType *type)
5671 int align;
5672 return type_size(pointed_type(type), &align);
5675 static inline int is_null_pointer(SValue *p)
5677 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
5678 return 0;
5679 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
5680 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
5683 static inline int is_integer_btype(int bt)
5685 return (bt == VT_BYTE || bt == VT_SHORT ||
5686 bt == VT_INT || bt == VT_LLONG);
5689 /* check types for comparison or substraction of pointers */
5690 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
5692 CType *type1, *type2, tmp_type1, tmp_type2;
5693 int bt1, bt2;
5695 /* null pointers are accepted for all comparisons as gcc */
5696 if (is_null_pointer(p1) || is_null_pointer(p2))
5697 return;
5698 type1 = &p1->type;
5699 type2 = &p2->type;
5700 bt1 = type1->t & VT_BTYPE;
5701 bt2 = type2->t & VT_BTYPE;
5702 /* accept comparison between pointer and integer with a warning */
5703 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
5704 if (op != TOK_LOR && op != TOK_LAND )
5705 warning("comparison between pointer and integer");
5706 return;
5709 /* both must be pointers or implicit function pointers */
5710 if (bt1 == VT_PTR) {
5711 type1 = pointed_type(type1);
5712 } else if (bt1 != VT_FUNC)
5713 goto invalid_operands;
5715 if (bt2 == VT_PTR) {
5716 type2 = pointed_type(type2);
5717 } else if (bt2 != VT_FUNC) {
5718 invalid_operands:
5719 error("invalid operands to binary %s", get_tok_str(op, NULL));
5721 if ((type1->t & VT_BTYPE) == VT_VOID ||
5722 (type2->t & VT_BTYPE) == VT_VOID)
5723 return;
5724 tmp_type1 = *type1;
5725 tmp_type2 = *type2;
5726 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5727 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
5728 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
5729 /* gcc-like error if '-' is used */
5730 if (op == '-')
5731 goto invalid_operands;
5732 else
5733 warning("comparison of distinct pointer types lacks a cast");
5737 /* generic gen_op: handles types problems */
5738 void gen_op(int op)
5740 int u, t1, t2, bt1, bt2, t;
5741 CType type1;
5743 t1 = vtop[-1].type.t;
5744 t2 = vtop[0].type.t;
5745 bt1 = t1 & VT_BTYPE;
5746 bt2 = t2 & VT_BTYPE;
5748 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5749 /* at least one operand is a pointer */
5750 /* relationnal op: must be both pointers */
5751 if (op >= TOK_ULT && op <= TOK_LOR) {
5752 check_comparison_pointer_types(vtop - 1, vtop, op);
5753 /* pointers are handled are unsigned */
5754 t = VT_INT | VT_UNSIGNED;
5755 goto std_op;
5757 /* if both pointers, then it must be the '-' op */
5758 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5759 if (op != '-')
5760 error("cannot use pointers here");
5761 check_comparison_pointer_types(vtop - 1, vtop, op);
5762 /* XXX: check that types are compatible */
5763 u = pointed_size(&vtop[-1].type);
5764 gen_opic(op);
5765 /* set to integer type */
5766 vtop->type.t = VT_INT;
5767 vpushi(u);
5768 gen_op(TOK_PDIV);
5769 } else {
5770 /* exactly one pointer : must be '+' or '-'. */
5771 if (op != '-' && op != '+')
5772 error("cannot use pointers here");
5773 /* Put pointer as first operand */
5774 if (bt2 == VT_PTR) {
5775 vswap();
5776 swap(&t1, &t2);
5778 type1 = vtop[-1].type;
5779 /* XXX: cast to int ? (long long case) */
5780 vpushi(pointed_size(&vtop[-1].type));
5781 gen_op('*');
5782 #ifdef CONFIG_TCC_BCHECK
5783 /* if evaluating constant expression, no code should be
5784 generated, so no bound check */
5785 if (do_bounds_check && !const_wanted) {
5786 /* if bounded pointers, we generate a special code to
5787 test bounds */
5788 if (op == '-') {
5789 vpushi(0);
5790 vswap();
5791 gen_op('-');
5793 gen_bounded_ptr_add();
5794 } else
5795 #endif
5797 gen_opic(op);
5799 /* put again type if gen_opic() swaped operands */
5800 vtop->type = type1;
5802 } else if (is_float(bt1) || is_float(bt2)) {
5803 /* compute bigger type and do implicit casts */
5804 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5805 t = VT_LDOUBLE;
5806 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5807 t = VT_DOUBLE;
5808 } else {
5809 t = VT_FLOAT;
5811 /* floats can only be used for a few operations */
5812 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5813 (op < TOK_ULT || op > TOK_GT))
5814 error("invalid operands for binary operation");
5815 goto std_op;
5816 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5817 /* cast to biggest op */
5818 t = VT_LLONG;
5819 /* convert to unsigned if it does not fit in a long long */
5820 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5821 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5822 t |= VT_UNSIGNED;
5823 goto std_op;
5824 } else {
5825 /* integer operations */
5826 t = VT_INT;
5827 /* convert to unsigned if it does not fit in an integer */
5828 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5829 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5830 t |= VT_UNSIGNED;
5831 std_op:
5832 /* XXX: currently, some unsigned operations are explicit, so
5833 we modify them here */
5834 if (t & VT_UNSIGNED) {
5835 if (op == TOK_SAR)
5836 op = TOK_SHR;
5837 else if (op == '/')
5838 op = TOK_UDIV;
5839 else if (op == '%')
5840 op = TOK_UMOD;
5841 else if (op == TOK_LT)
5842 op = TOK_ULT;
5843 else if (op == TOK_GT)
5844 op = TOK_UGT;
5845 else if (op == TOK_LE)
5846 op = TOK_ULE;
5847 else if (op == TOK_GE)
5848 op = TOK_UGE;
5850 vswap();
5851 type1.t = t;
5852 gen_cast(&type1);
5853 vswap();
5854 /* special case for shifts and long long: we keep the shift as
5855 an integer */
5856 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5857 type1.t = VT_INT;
5858 gen_cast(&type1);
5859 if (is_float(t))
5860 gen_opif(op);
5861 else
5862 gen_opic(op);
5863 if (op >= TOK_ULT && op <= TOK_GT) {
5864 /* relationnal op: the result is an int */
5865 vtop->type.t = VT_INT;
5866 } else {
5867 vtop->type.t = t;
5872 /* generic itof for unsigned long long case */
5873 void gen_cvt_itof1(int t)
5875 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5876 (VT_LLONG | VT_UNSIGNED)) {
5878 if (t == VT_FLOAT)
5879 vpush_global_sym(&func_old_type, TOK___ulltof);
5880 else if (t == VT_DOUBLE)
5881 vpush_global_sym(&func_old_type, TOK___ulltod);
5882 else
5883 vpush_global_sym(&func_old_type, TOK___ulltold);
5884 vrott(2);
5885 gfunc_call(1);
5886 vpushi(0);
5887 vtop->r = REG_FRET;
5888 } else {
5889 gen_cvt_itof(t);
5893 /* generic ftoi for unsigned long long case */
5894 void gen_cvt_ftoi1(int t)
5896 int st;
5898 if (t == (VT_LLONG | VT_UNSIGNED)) {
5899 /* not handled natively */
5900 st = vtop->type.t & VT_BTYPE;
5901 if (st == VT_FLOAT)
5902 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5903 else if (st == VT_DOUBLE)
5904 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5905 else
5906 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5907 vrott(2);
5908 gfunc_call(1);
5909 vpushi(0);
5910 vtop->r = REG_IRET;
5911 vtop->r2 = REG_LRET;
5912 } else {
5913 gen_cvt_ftoi(t);
5917 /* force char or short cast */
5918 void force_charshort_cast(int t)
5920 int bits, dbt;
5921 dbt = t & VT_BTYPE;
5922 /* XXX: add optimization if lvalue : just change type and offset */
5923 if (dbt == VT_BYTE)
5924 bits = 8;
5925 else
5926 bits = 16;
5927 if (t & VT_UNSIGNED) {
5928 vpushi((1 << bits) - 1);
5929 gen_op('&');
5930 } else {
5931 bits = 32 - bits;
5932 vpushi(bits);
5933 gen_op(TOK_SHL);
5934 /* result must be signed or the SAR is converted to an SHL
5935 This was not the case when "t" was a signed short
5936 and the last value on the stack was an unsigned int */
5937 vtop->type.t &= ~VT_UNSIGNED;
5938 vpushi(bits);
5939 gen_op(TOK_SAR);
5943 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5944 static void gen_cast(CType *type)
5946 int sbt, dbt, sf, df, c;
5948 /* special delayed cast for char/short */
5949 /* XXX: in some cases (multiple cascaded casts), it may still
5950 be incorrect */
5951 if (vtop->r & VT_MUSTCAST) {
5952 vtop->r &= ~VT_MUSTCAST;
5953 force_charshort_cast(vtop->type.t);
5956 /* bitfields first get cast to ints */
5957 if (vtop->type.t & VT_BITFIELD) {
5958 gv(RC_INT);
5961 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5962 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5964 if (sbt != dbt && !nocode_wanted) {
5965 sf = is_float(sbt);
5966 df = is_float(dbt);
5967 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5968 if (sf && df) {
5969 /* convert from fp to fp */
5970 if (c) {
5971 /* constant case: we can do it now */
5972 /* XXX: in ISOC, cannot do it if error in convert */
5973 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5974 vtop->c.f = (float)vtop->c.d;
5975 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5976 vtop->c.f = (float)vtop->c.ld;
5977 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5978 vtop->c.d = (double)vtop->c.f;
5979 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5980 vtop->c.d = (double)vtop->c.ld;
5981 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5982 vtop->c.ld = (long double)vtop->c.f;
5983 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5984 vtop->c.ld = (long double)vtop->c.d;
5985 } else {
5986 /* non constant case: generate code */
5987 gen_cvt_ftof(dbt);
5989 } else if (df) {
5990 /* convert int to fp */
5991 if (c) {
5992 switch(sbt) {
5993 case VT_LLONG | VT_UNSIGNED:
5994 case VT_LLONG:
5995 /* XXX: add const cases for long long */
5996 goto do_itof;
5997 case VT_INT | VT_UNSIGNED:
5998 switch(dbt) {
5999 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
6000 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
6001 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
6003 break;
6004 default:
6005 switch(dbt) {
6006 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
6007 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
6008 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
6010 break;
6012 } else {
6013 do_itof:
6014 #if !defined(TCC_TARGET_ARM)
6015 gen_cvt_itof1(dbt);
6016 #else
6017 gen_cvt_itof(dbt);
6018 #endif
6020 } else if (sf) {
6021 /* convert fp to int */
6022 if (dbt == VT_BOOL) {
6023 vpushi(0);
6024 gen_op(TOK_NE);
6025 } else {
6026 /* we handle char/short/etc... with generic code */
6027 if (dbt != (VT_INT | VT_UNSIGNED) &&
6028 dbt != (VT_LLONG | VT_UNSIGNED) &&
6029 dbt != VT_LLONG)
6030 dbt = VT_INT;
6031 if (c) {
6032 switch(dbt) {
6033 case VT_LLONG | VT_UNSIGNED:
6034 case VT_LLONG:
6035 /* XXX: add const cases for long long */
6036 goto do_ftoi;
6037 case VT_INT | VT_UNSIGNED:
6038 switch(sbt) {
6039 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
6040 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
6041 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
6043 break;
6044 default:
6045 /* int case */
6046 switch(sbt) {
6047 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
6048 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
6049 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
6051 break;
6053 } else {
6054 do_ftoi:
6055 gen_cvt_ftoi1(dbt);
6057 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
6058 /* additional cast for char/short... */
6059 vtop->type.t = dbt;
6060 gen_cast(type);
6063 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
6064 if ((sbt & VT_BTYPE) != VT_LLONG) {
6065 /* scalar to long long */
6066 if (c) {
6067 if (sbt == (VT_INT | VT_UNSIGNED))
6068 vtop->c.ll = vtop->c.ui;
6069 else
6070 vtop->c.ll = vtop->c.i;
6071 } else {
6072 /* machine independent conversion */
6073 gv(RC_INT);
6074 /* generate high word */
6075 if (sbt == (VT_INT | VT_UNSIGNED)) {
6076 vpushi(0);
6077 gv(RC_INT);
6078 } else {
6079 gv_dup();
6080 vpushi(31);
6081 gen_op(TOK_SAR);
6083 /* patch second register */
6084 vtop[-1].r2 = vtop->r;
6085 vpop();
6088 } else if (dbt == VT_BOOL) {
6089 /* scalar to bool */
6090 vpushi(0);
6091 gen_op(TOK_NE);
6092 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
6093 (dbt & VT_BTYPE) == VT_SHORT) {
6094 if (sbt == VT_PTR) {
6095 vtop->type.t = VT_INT;
6096 warning("nonportable conversion from pointer to char/short");
6098 force_charshort_cast(dbt);
6099 } else if ((dbt & VT_BTYPE) == VT_INT) {
6100 /* scalar to int */
6101 if (sbt == VT_LLONG) {
6102 /* from long long: just take low order word */
6103 lexpand();
6104 vpop();
6106 /* if lvalue and single word type, nothing to do because
6107 the lvalue already contains the real type size (see
6108 VT_LVAL_xxx constants) */
6110 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
6111 /* if we are casting between pointer types,
6112 we must update the VT_LVAL_xxx size */
6113 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
6114 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
6116 vtop->type = *type;
6119 /* return type size. Put alignment at 'a' */
6120 static int type_size(CType *type, int *a)
6122 Sym *s;
6123 int bt;
6125 bt = type->t & VT_BTYPE;
6126 if (bt == VT_STRUCT) {
6127 /* struct/union */
6128 s = type->ref;
6129 *a = s->r;
6130 return s->c;
6131 } else if (bt == VT_PTR) {
6132 if (type->t & VT_ARRAY) {
6133 s = type->ref;
6134 return type_size(&s->type, a) * s->c;
6135 } else {
6136 *a = PTR_SIZE;
6137 return PTR_SIZE;
6139 } else if (bt == VT_LDOUBLE) {
6140 *a = LDOUBLE_ALIGN;
6141 return LDOUBLE_SIZE;
6142 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
6143 #ifdef TCC_TARGET_I386
6144 *a = 4;
6145 #elif defined(TCC_TARGET_ARM)
6146 #ifdef TCC_ARM_EABI
6147 *a = 8;
6148 #else
6149 *a = 4;
6150 #endif
6151 #else
6152 *a = 8;
6153 #endif
6154 return 8;
6155 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
6156 *a = 4;
6157 return 4;
6158 } else if (bt == VT_SHORT) {
6159 *a = 2;
6160 return 2;
6161 } else {
6162 /* char, void, function, _Bool */
6163 *a = 1;
6164 return 1;
6168 /* return the pointed type of t */
6169 static inline CType *pointed_type(CType *type)
6171 return &type->ref->type;
6174 /* modify type so that its it is a pointer to type. */
6175 static void mk_pointer(CType *type)
6177 Sym *s;
6178 s = sym_push(SYM_FIELD, type, 0, -1);
6179 type->t = VT_PTR | (type->t & ~VT_TYPE);
6180 type->ref = s;
6183 /* compare function types. OLD functions match any new functions */
6184 static int is_compatible_func(CType *type1, CType *type2)
6186 Sym *s1, *s2;
6188 s1 = type1->ref;
6189 s2 = type2->ref;
6190 if (!is_compatible_types(&s1->type, &s2->type))
6191 return 0;
6192 /* check func_call */
6193 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
6194 return 0;
6195 /* XXX: not complete */
6196 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
6197 return 1;
6198 if (s1->c != s2->c)
6199 return 0;
6200 while (s1 != NULL) {
6201 if (s2 == NULL)
6202 return 0;
6203 if (!is_compatible_parameter_types(&s1->type, &s2->type))
6204 return 0;
6205 s1 = s1->next;
6206 s2 = s2->next;
6208 if (s2)
6209 return 0;
6210 return 1;
6213 /* return true if type1 and type2 are the same. If unqualified is
6214 true, qualifiers on the types are ignored.
6216 - enums are not checked as gcc __builtin_types_compatible_p ()
6218 static int compare_types(CType *type1, CType *type2, int unqualified)
6220 int bt1, t1, t2;
6222 t1 = type1->t & VT_TYPE;
6223 t2 = type2->t & VT_TYPE;
6224 if (unqualified) {
6225 /* strip qualifiers before comparing */
6226 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
6227 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
6229 /* XXX: bitfields ? */
6230 if (t1 != t2)
6231 return 0;
6232 /* test more complicated cases */
6233 bt1 = t1 & VT_BTYPE;
6234 if (bt1 == VT_PTR) {
6235 type1 = pointed_type(type1);
6236 type2 = pointed_type(type2);
6237 return is_compatible_types(type1, type2);
6238 } else if (bt1 == VT_STRUCT) {
6239 return (type1->ref == type2->ref);
6240 } else if (bt1 == VT_FUNC) {
6241 return is_compatible_func(type1, type2);
6242 } else {
6243 return 1;
6247 /* return true if type1 and type2 are exactly the same (including
6248 qualifiers).
6250 static int is_compatible_types(CType *type1, CType *type2)
6252 return compare_types(type1,type2,0);
6255 /* return true if type1 and type2 are the same (ignoring qualifiers).
6257 static int is_compatible_parameter_types(CType *type1, CType *type2)
6259 return compare_types(type1,type2,1);
6262 /* print a type. If 'varstr' is not NULL, then the variable is also
6263 printed in the type */
6264 /* XXX: union */
6265 /* XXX: add array and function pointers */
6266 void type_to_str(char *buf, int buf_size,
6267 CType *type, const char *varstr)
6269 int bt, v, t;
6270 Sym *s, *sa;
6271 char buf1[256];
6272 const char *tstr;
6274 t = type->t & VT_TYPE;
6275 bt = t & VT_BTYPE;
6276 buf[0] = '\0';
6277 if (t & VT_CONSTANT)
6278 pstrcat(buf, buf_size, "const ");
6279 if (t & VT_VOLATILE)
6280 pstrcat(buf, buf_size, "volatile ");
6281 if (t & VT_UNSIGNED)
6282 pstrcat(buf, buf_size, "unsigned ");
6283 switch(bt) {
6284 case VT_VOID:
6285 tstr = "void";
6286 goto add_tstr;
6287 case VT_BOOL:
6288 tstr = "_Bool";
6289 goto add_tstr;
6290 case VT_BYTE:
6291 tstr = "char";
6292 goto add_tstr;
6293 case VT_SHORT:
6294 tstr = "short";
6295 goto add_tstr;
6296 case VT_INT:
6297 tstr = "int";
6298 goto add_tstr;
6299 case VT_LONG:
6300 tstr = "long";
6301 goto add_tstr;
6302 case VT_LLONG:
6303 tstr = "long long";
6304 goto add_tstr;
6305 case VT_FLOAT:
6306 tstr = "float";
6307 goto add_tstr;
6308 case VT_DOUBLE:
6309 tstr = "double";
6310 goto add_tstr;
6311 case VT_LDOUBLE:
6312 tstr = "long double";
6313 add_tstr:
6314 pstrcat(buf, buf_size, tstr);
6315 break;
6316 case VT_ENUM:
6317 case VT_STRUCT:
6318 if (bt == VT_STRUCT)
6319 tstr = "struct ";
6320 else
6321 tstr = "enum ";
6322 pstrcat(buf, buf_size, tstr);
6323 v = type->ref->v & ~SYM_STRUCT;
6324 if (v >= SYM_FIRST_ANOM)
6325 pstrcat(buf, buf_size, "<anonymous>");
6326 else
6327 pstrcat(buf, buf_size, get_tok_str(v, NULL));
6328 break;
6329 case VT_FUNC:
6330 s = type->ref;
6331 type_to_str(buf, buf_size, &s->type, varstr);
6332 pstrcat(buf, buf_size, "(");
6333 sa = s->next;
6334 while (sa != NULL) {
6335 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
6336 pstrcat(buf, buf_size, buf1);
6337 sa = sa->next;
6338 if (sa)
6339 pstrcat(buf, buf_size, ", ");
6341 pstrcat(buf, buf_size, ")");
6342 goto no_var;
6343 case VT_PTR:
6344 s = type->ref;
6345 pstrcpy(buf1, sizeof(buf1), "*");
6346 if (varstr)
6347 pstrcat(buf1, sizeof(buf1), varstr);
6348 type_to_str(buf, buf_size, &s->type, buf1);
6349 goto no_var;
6351 if (varstr) {
6352 pstrcat(buf, buf_size, " ");
6353 pstrcat(buf, buf_size, varstr);
6355 no_var: ;
6358 /* verify type compatibility to store vtop in 'dt' type, and generate
6359 casts if needed. */
6360 static void gen_assign_cast(CType *dt)
6362 CType *st, *type1, *type2, tmp_type1, tmp_type2;
6363 char buf1[256], buf2[256];
6364 int dbt, sbt;
6366 st = &vtop->type; /* source type */
6367 dbt = dt->t & VT_BTYPE;
6368 sbt = st->t & VT_BTYPE;
6369 if (dt->t & VT_CONSTANT)
6370 warning("assignment of read-only location");
6371 switch(dbt) {
6372 case VT_PTR:
6373 /* special cases for pointers */
6374 /* '0' can also be a pointer */
6375 if (is_null_pointer(vtop))
6376 goto type_ok;
6377 /* accept implicit pointer to integer cast with warning */
6378 if (is_integer_btype(sbt)) {
6379 warning("assignment makes pointer from integer without a cast");
6380 goto type_ok;
6382 type1 = pointed_type(dt);
6383 /* a function is implicitely a function pointer */
6384 if (sbt == VT_FUNC) {
6385 if ((type1->t & VT_BTYPE) != VT_VOID &&
6386 !is_compatible_types(pointed_type(dt), st))
6387 goto error;
6388 else
6389 goto type_ok;
6391 if (sbt != VT_PTR)
6392 goto error;
6393 type2 = pointed_type(st);
6394 if ((type1->t & VT_BTYPE) == VT_VOID ||
6395 (type2->t & VT_BTYPE) == VT_VOID) {
6396 /* void * can match anything */
6397 } else {
6398 /* exact type match, except for unsigned */
6399 tmp_type1 = *type1;
6400 tmp_type2 = *type2;
6401 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6402 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
6403 if (!is_compatible_types(&tmp_type1, &tmp_type2))
6404 warning("assignment from incompatible pointer type");
6406 /* check const and volatile */
6407 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
6408 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
6409 warning("assignment discards qualifiers from pointer target type");
6410 break;
6411 case VT_BYTE:
6412 case VT_SHORT:
6413 case VT_INT:
6414 case VT_LLONG:
6415 if (sbt == VT_PTR || sbt == VT_FUNC) {
6416 warning("assignment makes integer from pointer without a cast");
6418 /* XXX: more tests */
6419 break;
6420 case VT_STRUCT:
6421 tmp_type1 = *dt;
6422 tmp_type2 = *st;
6423 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
6424 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
6425 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
6426 error:
6427 type_to_str(buf1, sizeof(buf1), st, NULL);
6428 type_to_str(buf2, sizeof(buf2), dt, NULL);
6429 error("cannot cast '%s' to '%s'", buf1, buf2);
6431 break;
6433 type_ok:
6434 gen_cast(dt);
6437 /* store vtop in lvalue pushed on stack */
6438 void vstore(void)
6440 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
6442 ft = vtop[-1].type.t;
6443 sbt = vtop->type.t & VT_BTYPE;
6444 dbt = ft & VT_BTYPE;
6445 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
6446 (sbt == VT_INT && dbt == VT_SHORT)) {
6447 /* optimize char/short casts */
6448 delayed_cast = VT_MUSTCAST;
6449 vtop->type.t = ft & VT_TYPE;
6450 /* XXX: factorize */
6451 if (ft & VT_CONSTANT)
6452 warning("assignment of read-only location");
6453 } else {
6454 delayed_cast = 0;
6455 if (!(ft & VT_BITFIELD))
6456 gen_assign_cast(&vtop[-1].type);
6459 if (sbt == VT_STRUCT) {
6460 /* if structure, only generate pointer */
6461 /* structure assignment : generate memcpy */
6462 /* XXX: optimize if small size */
6463 if (!nocode_wanted) {
6464 size = type_size(&vtop->type, &align);
6466 #ifdef TCC_ARM_EABI
6467 if(!(align & 7))
6468 vpush_global_sym(&func_old_type, TOK_memcpy8);
6469 else if(!(align & 3))
6470 vpush_global_sym(&func_old_type, TOK_memcpy4);
6471 else
6472 #endif
6473 vpush_global_sym(&func_old_type, TOK_memcpy);
6475 /* destination */
6476 vpushv(vtop - 2);
6477 vtop->type.t = VT_INT;
6478 gaddrof();
6479 /* source */
6480 vpushv(vtop - 2);
6481 vtop->type.t = VT_INT;
6482 gaddrof();
6483 /* type size */
6484 vpushi(size);
6485 gfunc_call(3);
6487 vswap();
6488 vpop();
6489 } else {
6490 vswap();
6491 vpop();
6493 /* leave source on stack */
6494 } else if (ft & VT_BITFIELD) {
6495 /* bitfield store handling */
6496 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
6497 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
6498 /* remove bit field info to avoid loops */
6499 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
6501 /* duplicate source into other register */
6502 gv_dup();
6503 vswap();
6504 vrott(3);
6506 /* duplicate destination */
6507 vdup();
6508 vtop[-1] = vtop[-2];
6510 /* mask and shift source */
6511 vpushi((1 << bit_size) - 1);
6512 gen_op('&');
6513 vpushi(bit_pos);
6514 gen_op(TOK_SHL);
6515 /* load destination, mask and or with source */
6516 vswap();
6517 vpushi(~(((1 << bit_size) - 1) << bit_pos));
6518 gen_op('&');
6519 gen_op('|');
6520 /* store result */
6521 vstore();
6523 /* pop off shifted source from "duplicate source..." above */
6524 vpop();
6526 } else {
6527 #ifdef CONFIG_TCC_BCHECK
6528 /* bound check case */
6529 if (vtop[-1].r & VT_MUSTBOUND) {
6530 vswap();
6531 gbound();
6532 vswap();
6534 #endif
6535 if (!nocode_wanted) {
6536 rc = RC_INT;
6537 if (is_float(ft))
6538 rc = RC_FLOAT;
6539 r = gv(rc); /* generate value */
6540 /* if lvalue was saved on stack, must read it */
6541 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
6542 SValue sv;
6543 t = get_reg(RC_INT);
6544 sv.type.t = VT_INT;
6545 sv.r = VT_LOCAL | VT_LVAL;
6546 sv.c.ul = vtop[-1].c.ul;
6547 load(t, &sv);
6548 vtop[-1].r = t | VT_LVAL;
6550 store(r, vtop - 1);
6551 /* two word case handling : store second register at word + 4 */
6552 if ((ft & VT_BTYPE) == VT_LLONG) {
6553 vswap();
6554 /* convert to int to increment easily */
6555 vtop->type.t = VT_INT;
6556 gaddrof();
6557 vpushi(4);
6558 gen_op('+');
6559 vtop->r |= VT_LVAL;
6560 vswap();
6561 /* XXX: it works because r2 is spilled last ! */
6562 store(vtop->r2, vtop - 1);
6565 vswap();
6566 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
6567 vtop->r |= delayed_cast;
6571 /* post defines POST/PRE add. c is the token ++ or -- */
6572 void inc(int post, int c)
6574 test_lvalue();
6575 vdup(); /* save lvalue */
6576 if (post) {
6577 gv_dup(); /* duplicate value */
6578 vrotb(3);
6579 vrotb(3);
6581 /* add constant */
6582 vpushi(c - TOK_MID);
6583 gen_op('+');
6584 vstore(); /* store value */
6585 if (post)
6586 vpop(); /* if post op, return saved value */
6589 /* Parse GNUC __attribute__ extension. Currently, the following
6590 extensions are recognized:
6591 - aligned(n) : set data/function alignment.
6592 - packed : force data alignment to 1
6593 - section(x) : generate data/code in this section.
6594 - unused : currently ignored, but may be used someday.
6595 - regparm(n) : pass function parameters in registers (i386 only)
6597 static void parse_attribute(AttributeDef *ad)
6599 int t, n;
6601 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
6602 next();
6603 skip('(');
6604 skip('(');
6605 while (tok != ')') {
6606 if (tok < TOK_IDENT)
6607 expect("attribute name");
6608 t = tok;
6609 next();
6610 switch(t) {
6611 case TOK_SECTION1:
6612 case TOK_SECTION2:
6613 skip('(');
6614 if (tok != TOK_STR)
6615 expect("section name");
6616 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
6617 next();
6618 skip(')');
6619 break;
6620 case TOK_ALIGNED1:
6621 case TOK_ALIGNED2:
6622 if (tok == '(') {
6623 next();
6624 n = expr_const();
6625 if (n <= 0 || (n & (n - 1)) != 0)
6626 error("alignment must be a positive power of two");
6627 skip(')');
6628 } else {
6629 n = MAX_ALIGN;
6631 ad->aligned = n;
6632 break;
6633 case TOK_PACKED1:
6634 case TOK_PACKED2:
6635 ad->packed = 1;
6636 break;
6637 case TOK_UNUSED1:
6638 case TOK_UNUSED2:
6639 /* currently, no need to handle it because tcc does not
6640 track unused objects */
6641 break;
6642 case TOK_NORETURN1:
6643 case TOK_NORETURN2:
6644 /* currently, no need to handle it because tcc does not
6645 track unused objects */
6646 break;
6647 case TOK_CDECL1:
6648 case TOK_CDECL2:
6649 case TOK_CDECL3:
6650 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
6651 break;
6652 case TOK_STDCALL1:
6653 case TOK_STDCALL2:
6654 case TOK_STDCALL3:
6655 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
6656 break;
6657 #ifdef TCC_TARGET_I386
6658 case TOK_REGPARM1:
6659 case TOK_REGPARM2:
6660 skip('(');
6661 n = expr_const();
6662 if (n > 3)
6663 n = 3;
6664 else if (n < 0)
6665 n = 0;
6666 if (n > 0)
6667 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
6668 skip(')');
6669 break;
6670 case TOK_FASTCALL1:
6671 case TOK_FASTCALL2:
6672 case TOK_FASTCALL3:
6673 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
6674 break;
6675 #endif
6676 case TOK_DLLEXPORT:
6677 FUNC_EXPORT(ad->func_attr) = 1;
6678 break;
6679 default:
6680 if (tcc_state->warn_unsupported)
6681 warning("'%s' attribute ignored", get_tok_str(t, NULL));
6682 /* skip parameters */
6683 if (tok == '(') {
6684 int parenthesis = 0;
6685 do {
6686 if (tok == '(')
6687 parenthesis++;
6688 else if (tok == ')')
6689 parenthesis--;
6690 next();
6691 } while (parenthesis && tok != -1);
6693 break;
6695 if (tok != ',')
6696 break;
6697 next();
6699 skip(')');
6700 skip(')');
6704 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6705 static void struct_decl(CType *type, int u)
6707 int a, v, size, align, maxalign, c, offset;
6708 int bit_size, bit_pos, bsize, bt, lbit_pos;
6709 Sym *s, *ss, *ass, **ps;
6710 AttributeDef ad;
6711 CType type1, btype;
6713 a = tok; /* save decl type */
6714 next();
6715 if (tok != '{') {
6716 v = tok;
6717 next();
6718 /* struct already defined ? return it */
6719 if (v < TOK_IDENT)
6720 expect("struct/union/enum name");
6721 s = struct_find(v);
6722 if (s) {
6723 if (s->type.t != a)
6724 error("invalid type");
6725 goto do_decl;
6727 } else {
6728 v = anon_sym++;
6730 type1.t = a;
6731 /* we put an undefined size for struct/union */
6732 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
6733 s->r = 0; /* default alignment is zero as gcc */
6734 /* put struct/union/enum name in type */
6735 do_decl:
6736 type->t = u;
6737 type->ref = s;
6739 if (tok == '{') {
6740 next();
6741 if (s->c != -1)
6742 error("struct/union/enum already defined");
6743 /* cannot be empty */
6744 c = 0;
6745 /* non empty enums are not allowed */
6746 if (a == TOK_ENUM) {
6747 for(;;) {
6748 v = tok;
6749 if (v < TOK_UIDENT)
6750 expect("identifier");
6751 next();
6752 if (tok == '=') {
6753 next();
6754 c = expr_const();
6756 /* enum symbols have static storage */
6757 ss = sym_push(v, &int_type, VT_CONST, c);
6758 ss->type.t |= VT_STATIC;
6759 if (tok != ',')
6760 break;
6761 next();
6762 c++;
6763 /* NOTE: we accept a trailing comma */
6764 if (tok == '}')
6765 break;
6767 skip('}');
6768 } else {
6769 maxalign = 1;
6770 ps = &s->next;
6771 bit_pos = 0;
6772 offset = 0;
6773 while (tok != '}') {
6774 parse_btype(&btype, &ad);
6775 while (1) {
6776 bit_size = -1;
6777 v = 0;
6778 type1 = btype;
6779 if (tok != ':') {
6780 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
6781 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
6782 expect("identifier");
6783 if ((type1.t & VT_BTYPE) == VT_FUNC ||
6784 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
6785 error("invalid type for '%s'",
6786 get_tok_str(v, NULL));
6788 if (tok == ':') {
6789 next();
6790 bit_size = expr_const();
6791 /* XXX: handle v = 0 case for messages */
6792 if (bit_size < 0)
6793 error("negative width in bit-field '%s'",
6794 get_tok_str(v, NULL));
6795 if (v && bit_size == 0)
6796 error("zero width for bit-field '%s'",
6797 get_tok_str(v, NULL));
6799 size = type_size(&type1, &align);
6800 if (ad.aligned) {
6801 if (align < ad.aligned)
6802 align = ad.aligned;
6803 } else if (ad.packed) {
6804 align = 1;
6805 } else if (*tcc_state->pack_stack_ptr) {
6806 if (align > *tcc_state->pack_stack_ptr)
6807 align = *tcc_state->pack_stack_ptr;
6809 lbit_pos = 0;
6810 if (bit_size >= 0) {
6811 bt = type1.t & VT_BTYPE;
6812 if (bt != VT_INT &&
6813 bt != VT_BYTE &&
6814 bt != VT_SHORT &&
6815 bt != VT_BOOL &&
6816 bt != VT_ENUM)
6817 error("bitfields must have scalar type");
6818 bsize = size * 8;
6819 if (bit_size > bsize) {
6820 error("width of '%s' exceeds its type",
6821 get_tok_str(v, NULL));
6822 } else if (bit_size == bsize) {
6823 /* no need for bit fields */
6824 bit_pos = 0;
6825 } else if (bit_size == 0) {
6826 /* XXX: what to do if only padding in a
6827 structure ? */
6828 /* zero size: means to pad */
6829 if (bit_pos > 0)
6830 bit_pos = bsize;
6831 } else {
6832 /* we do not have enough room ? */
6833 if ((bit_pos + bit_size) > bsize)
6834 bit_pos = 0;
6835 lbit_pos = bit_pos;
6836 /* XXX: handle LSB first */
6837 type1.t |= VT_BITFIELD |
6838 (bit_pos << VT_STRUCT_SHIFT) |
6839 (bit_size << (VT_STRUCT_SHIFT + 6));
6840 bit_pos += bit_size;
6842 } else {
6843 bit_pos = 0;
6845 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
6846 /* add new memory data only if starting
6847 bit field */
6848 if (lbit_pos == 0) {
6849 if (a == TOK_STRUCT) {
6850 c = (c + align - 1) & -align;
6851 offset = c;
6852 if (size > 0)
6853 c += size;
6854 } else {
6855 offset = 0;
6856 if (size > c)
6857 c = size;
6859 if (align > maxalign)
6860 maxalign = align;
6862 #if 0
6863 printf("add field %s offset=%d",
6864 get_tok_str(v, NULL), offset);
6865 if (type1.t & VT_BITFIELD) {
6866 printf(" pos=%d size=%d",
6867 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6868 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6870 printf("\n");
6871 #endif
6873 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
6874 ass = type1.ref;
6875 while ((ass = ass->next) != NULL) {
6876 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
6877 *ps = ss;
6878 ps = &ss->next;
6880 } else if (v) {
6881 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6882 *ps = ss;
6883 ps = &ss->next;
6885 if (tok == ';' || tok == TOK_EOF)
6886 break;
6887 skip(',');
6889 skip(';');
6891 skip('}');
6892 /* store size and alignment */
6893 s->c = (c + maxalign - 1) & -maxalign;
6894 s->r = maxalign;
6899 /* return 0 if no type declaration. otherwise, return the basic type
6900 and skip it.
6902 static int parse_btype(CType *type, AttributeDef *ad)
6904 int t, u, type_found, typespec_found, typedef_found;
6905 Sym *s;
6906 CType type1;
6908 memset(ad, 0, sizeof(AttributeDef));
6909 type_found = 0;
6910 typespec_found = 0;
6911 typedef_found = 0;
6912 t = 0;
6913 while(1) {
6914 switch(tok) {
6915 case TOK_EXTENSION:
6916 /* currently, we really ignore extension */
6917 next();
6918 continue;
6920 /* basic types */
6921 case TOK_CHAR:
6922 u = VT_BYTE;
6923 basic_type:
6924 next();
6925 basic_type1:
6926 if ((t & VT_BTYPE) != 0)
6927 error("too many basic types");
6928 t |= u;
6929 typespec_found = 1;
6930 break;
6931 case TOK_VOID:
6932 u = VT_VOID;
6933 goto basic_type;
6934 case TOK_SHORT:
6935 u = VT_SHORT;
6936 goto basic_type;
6937 case TOK_INT:
6938 next();
6939 typespec_found = 1;
6940 break;
6941 case TOK_LONG:
6942 next();
6943 if ((t & VT_BTYPE) == VT_DOUBLE) {
6944 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6945 } else if ((t & VT_BTYPE) == VT_LONG) {
6946 t = (t & ~VT_BTYPE) | VT_LLONG;
6947 } else {
6948 u = VT_LONG;
6949 goto basic_type1;
6951 break;
6952 case TOK_BOOL:
6953 u = VT_BOOL;
6954 goto basic_type;
6955 case TOK_FLOAT:
6956 u = VT_FLOAT;
6957 goto basic_type;
6958 case TOK_DOUBLE:
6959 next();
6960 if ((t & VT_BTYPE) == VT_LONG) {
6961 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6962 } else {
6963 u = VT_DOUBLE;
6964 goto basic_type1;
6966 break;
6967 case TOK_ENUM:
6968 struct_decl(&type1, VT_ENUM);
6969 basic_type2:
6970 u = type1.t;
6971 type->ref = type1.ref;
6972 goto basic_type1;
6973 case TOK_STRUCT:
6974 case TOK_UNION:
6975 struct_decl(&type1, VT_STRUCT);
6976 goto basic_type2;
6978 /* type modifiers */
6979 case TOK_CONST1:
6980 case TOK_CONST2:
6981 case TOK_CONST3:
6982 t |= VT_CONSTANT;
6983 next();
6984 break;
6985 case TOK_VOLATILE1:
6986 case TOK_VOLATILE2:
6987 case TOK_VOLATILE3:
6988 t |= VT_VOLATILE;
6989 next();
6990 break;
6991 case TOK_SIGNED1:
6992 case TOK_SIGNED2:
6993 case TOK_SIGNED3:
6994 typespec_found = 1;
6995 t |= VT_SIGNED;
6996 next();
6997 break;
6998 case TOK_REGISTER:
6999 case TOK_AUTO:
7000 case TOK_RESTRICT1:
7001 case TOK_RESTRICT2:
7002 case TOK_RESTRICT3:
7003 next();
7004 break;
7005 case TOK_UNSIGNED:
7006 t |= VT_UNSIGNED;
7007 next();
7008 typespec_found = 1;
7009 break;
7011 /* storage */
7012 case TOK_EXTERN:
7013 t |= VT_EXTERN;
7014 next();
7015 break;
7016 case TOK_STATIC:
7017 t |= VT_STATIC;
7018 next();
7019 break;
7020 case TOK_TYPEDEF:
7021 t |= VT_TYPEDEF;
7022 next();
7023 break;
7024 case TOK_INLINE1:
7025 case TOK_INLINE2:
7026 case TOK_INLINE3:
7027 t |= VT_INLINE;
7028 next();
7029 break;
7031 /* GNUC attribute */
7032 case TOK_ATTRIBUTE1:
7033 case TOK_ATTRIBUTE2:
7034 parse_attribute(ad);
7035 break;
7036 /* GNUC typeof */
7037 case TOK_TYPEOF1:
7038 case TOK_TYPEOF2:
7039 case TOK_TYPEOF3:
7040 next();
7041 parse_expr_type(&type1);
7042 goto basic_type2;
7043 default:
7044 if (typespec_found || typedef_found)
7045 goto the_end;
7046 s = sym_find(tok);
7047 if (!s || !(s->type.t & VT_TYPEDEF))
7048 goto the_end;
7049 typedef_found = 1;
7050 t |= (s->type.t & ~VT_TYPEDEF);
7051 type->ref = s->type.ref;
7052 next();
7053 typespec_found = 1;
7054 break;
7056 type_found = 1;
7058 the_end:
7059 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
7060 error("signed and unsigned modifier");
7061 if (tcc_state->char_is_unsigned) {
7062 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
7063 t |= VT_UNSIGNED;
7065 t &= ~VT_SIGNED;
7067 /* long is never used as type */
7068 if ((t & VT_BTYPE) == VT_LONG)
7069 t = (t & ~VT_BTYPE) | VT_INT;
7070 type->t = t;
7071 return type_found;
7074 /* convert a function parameter type (array to pointer and function to
7075 function pointer) */
7076 static inline void convert_parameter_type(CType *pt)
7078 /* remove const and volatile qualifiers (XXX: const could be used
7079 to indicate a const function parameter */
7080 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
7081 /* array must be transformed to pointer according to ANSI C */
7082 pt->t &= ~VT_ARRAY;
7083 if ((pt->t & VT_BTYPE) == VT_FUNC) {
7084 mk_pointer(pt);
7088 static void post_type(CType *type, AttributeDef *ad)
7090 int n, l, t1, arg_size, align;
7091 Sym **plast, *s, *first;
7092 AttributeDef ad1;
7093 CType pt;
7095 if (tok == '(') {
7096 /* function declaration */
7097 next();
7098 l = 0;
7099 first = NULL;
7100 plast = &first;
7101 arg_size = 0;
7102 if (tok != ')') {
7103 for(;;) {
7104 /* read param name and compute offset */
7105 if (l != FUNC_OLD) {
7106 if (!parse_btype(&pt, &ad1)) {
7107 if (l) {
7108 error("invalid type");
7109 } else {
7110 l = FUNC_OLD;
7111 goto old_proto;
7114 l = FUNC_NEW;
7115 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
7116 break;
7117 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
7118 if ((pt.t & VT_BTYPE) == VT_VOID)
7119 error("parameter declared as void");
7120 arg_size += (type_size(&pt, &align) + 3) & ~3;
7121 } else {
7122 old_proto:
7123 n = tok;
7124 if (n < TOK_UIDENT)
7125 expect("identifier");
7126 pt.t = VT_INT;
7127 next();
7129 convert_parameter_type(&pt);
7130 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
7131 *plast = s;
7132 plast = &s->next;
7133 if (tok == ')')
7134 break;
7135 skip(',');
7136 if (l == FUNC_NEW && tok == TOK_DOTS) {
7137 l = FUNC_ELLIPSIS;
7138 next();
7139 break;
7143 /* if no parameters, then old type prototype */
7144 if (l == 0)
7145 l = FUNC_OLD;
7146 skip(')');
7147 t1 = type->t & VT_STORAGE;
7148 /* NOTE: const is ignored in returned type as it has a special
7149 meaning in gcc / C++ */
7150 type->t &= ~(VT_STORAGE | VT_CONSTANT);
7151 post_type(type, ad);
7152 /* we push a anonymous symbol which will contain the function prototype */
7153 FUNC_ARGS(ad->func_attr) = arg_size;
7154 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
7155 s->next = first;
7156 type->t = t1 | VT_FUNC;
7157 type->ref = s;
7158 } else if (tok == '[') {
7159 /* array definition */
7160 next();
7161 n = -1;
7162 if (tok != ']') {
7163 n = expr_const();
7164 if (n < 0)
7165 error("invalid array size");
7167 skip(']');
7168 /* parse next post type */
7169 t1 = type->t & VT_STORAGE;
7170 type->t &= ~VT_STORAGE;
7171 post_type(type, ad);
7173 /* we push a anonymous symbol which will contain the array
7174 element type */
7175 s = sym_push(SYM_FIELD, type, 0, n);
7176 type->t = t1 | VT_ARRAY | VT_PTR;
7177 type->ref = s;
7181 /* Parse a type declaration (except basic type), and return the type
7182 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7183 expected. 'type' should contain the basic type. 'ad' is the
7184 attribute definition of the basic type. It can be modified by
7185 type_decl().
7187 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
7189 Sym *s;
7190 CType type1, *type2;
7191 int qualifiers;
7193 while (tok == '*') {
7194 qualifiers = 0;
7195 redo:
7196 next();
7197 switch(tok) {
7198 case TOK_CONST1:
7199 case TOK_CONST2:
7200 case TOK_CONST3:
7201 qualifiers |= VT_CONSTANT;
7202 goto redo;
7203 case TOK_VOLATILE1:
7204 case TOK_VOLATILE2:
7205 case TOK_VOLATILE3:
7206 qualifiers |= VT_VOLATILE;
7207 goto redo;
7208 case TOK_RESTRICT1:
7209 case TOK_RESTRICT2:
7210 case TOK_RESTRICT3:
7211 goto redo;
7213 mk_pointer(type);
7214 type->t |= qualifiers;
7217 /* XXX: clarify attribute handling */
7218 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7219 parse_attribute(ad);
7221 /* recursive type */
7222 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7223 type1.t = 0; /* XXX: same as int */
7224 if (tok == '(') {
7225 next();
7226 /* XXX: this is not correct to modify 'ad' at this point, but
7227 the syntax is not clear */
7228 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7229 parse_attribute(ad);
7230 type_decl(&type1, ad, v, td);
7231 skip(')');
7232 } else {
7233 /* type identifier */
7234 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
7235 *v = tok;
7236 next();
7237 } else {
7238 if (!(td & TYPE_ABSTRACT))
7239 expect("identifier");
7240 *v = 0;
7243 post_type(type, ad);
7244 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
7245 parse_attribute(ad);
7246 if (!type1.t)
7247 return;
7248 /* append type at the end of type1 */
7249 type2 = &type1;
7250 for(;;) {
7251 s = type2->ref;
7252 type2 = &s->type;
7253 if (!type2->t) {
7254 *type2 = *type;
7255 break;
7258 *type = type1;
7261 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7262 static int lvalue_type(int t)
7264 int bt, r;
7265 r = VT_LVAL;
7266 bt = t & VT_BTYPE;
7267 if (bt == VT_BYTE || bt == VT_BOOL)
7268 r |= VT_LVAL_BYTE;
7269 else if (bt == VT_SHORT)
7270 r |= VT_LVAL_SHORT;
7271 else
7272 return r;
7273 if (t & VT_UNSIGNED)
7274 r |= VT_LVAL_UNSIGNED;
7275 return r;
7278 /* indirection with full error checking and bound check */
7279 static void indir(void)
7281 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
7282 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
7283 return;
7284 expect("pointer");
7286 if ((vtop->r & VT_LVAL) && !nocode_wanted)
7287 gv(RC_INT);
7288 vtop->type = *pointed_type(&vtop->type);
7289 /* Arrays and functions are never lvalues */
7290 if (!(vtop->type.t & VT_ARRAY)
7291 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
7292 vtop->r |= lvalue_type(vtop->type.t);
7293 /* if bound checking, the referenced pointer must be checked */
7294 if (do_bounds_check)
7295 vtop->r |= VT_MUSTBOUND;
7299 /* pass a parameter to a function and do type checking and casting */
7300 static void gfunc_param_typed(Sym *func, Sym *arg)
7302 int func_type;
7303 CType type;
7305 func_type = func->c;
7306 if (func_type == FUNC_OLD ||
7307 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
7308 /* default casting : only need to convert float to double */
7309 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
7310 type.t = VT_DOUBLE;
7311 gen_cast(&type);
7313 } else if (arg == NULL) {
7314 error("too many arguments to function");
7315 } else {
7316 type = arg->type;
7317 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
7318 gen_assign_cast(&type);
7322 /* parse an expression of the form '(type)' or '(expr)' and return its
7323 type */
7324 static void parse_expr_type(CType *type)
7326 int n;
7327 AttributeDef ad;
7329 skip('(');
7330 if (parse_btype(type, &ad)) {
7331 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7332 } else {
7333 expr_type(type);
7335 skip(')');
7338 static void parse_type(CType *type)
7340 AttributeDef ad;
7341 int n;
7343 if (!parse_btype(type, &ad)) {
7344 expect("type");
7346 type_decl(type, &ad, &n, TYPE_ABSTRACT);
7349 static void vpush_tokc(int t)
7351 CType type;
7352 type.t = t;
7353 vsetc(&type, VT_CONST, &tokc);
7356 static void unary(void)
7358 int n, t, align, size, r;
7359 CType type;
7360 Sym *s;
7361 AttributeDef ad;
7363 /* XXX: GCC 2.95.3 does not generate a table although it should be
7364 better here */
7365 tok_next:
7366 switch(tok) {
7367 case TOK_EXTENSION:
7368 next();
7369 goto tok_next;
7370 case TOK_CINT:
7371 case TOK_CCHAR:
7372 case TOK_LCHAR:
7373 vpushi(tokc.i);
7374 next();
7375 break;
7376 case TOK_CUINT:
7377 vpush_tokc(VT_INT | VT_UNSIGNED);
7378 next();
7379 break;
7380 case TOK_CLLONG:
7381 vpush_tokc(VT_LLONG);
7382 next();
7383 break;
7384 case TOK_CULLONG:
7385 vpush_tokc(VT_LLONG | VT_UNSIGNED);
7386 next();
7387 break;
7388 case TOK_CFLOAT:
7389 vpush_tokc(VT_FLOAT);
7390 next();
7391 break;
7392 case TOK_CDOUBLE:
7393 vpush_tokc(VT_DOUBLE);
7394 next();
7395 break;
7396 case TOK_CLDOUBLE:
7397 vpush_tokc(VT_LDOUBLE);
7398 next();
7399 break;
7400 case TOK___FUNCTION__:
7401 if (!gnu_ext)
7402 goto tok_identifier;
7403 /* fall thru */
7404 case TOK___FUNC__:
7406 void *ptr;
7407 int len;
7408 /* special function name identifier */
7409 len = strlen(funcname) + 1;
7410 /* generate char[len] type */
7411 type.t = VT_BYTE;
7412 mk_pointer(&type);
7413 type.t |= VT_ARRAY;
7414 type.ref->c = len;
7415 vpush_ref(&type, data_section, data_section->data_offset, len);
7416 ptr = section_ptr_add(data_section, len);
7417 memcpy(ptr, funcname, len);
7418 next();
7420 break;
7421 case TOK_LSTR:
7422 #ifdef TCC_TARGET_PE
7423 t = VT_SHORT | VT_UNSIGNED;
7424 #else
7425 t = VT_INT;
7426 #endif
7427 goto str_init;
7428 case TOK_STR:
7429 /* string parsing */
7430 t = VT_BYTE;
7431 str_init:
7432 if (tcc_state->warn_write_strings)
7433 t |= VT_CONSTANT;
7434 type.t = t;
7435 mk_pointer(&type);
7436 type.t |= VT_ARRAY;
7437 memset(&ad, 0, sizeof(AttributeDef));
7438 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
7439 break;
7440 case '(':
7441 next();
7442 /* cast ? */
7443 if (parse_btype(&type, &ad)) {
7444 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
7445 skip(')');
7446 /* check ISOC99 compound literal */
7447 if (tok == '{') {
7448 /* data is allocated locally by default */
7449 if (global_expr)
7450 r = VT_CONST;
7451 else
7452 r = VT_LOCAL;
7453 /* all except arrays are lvalues */
7454 if (!(type.t & VT_ARRAY))
7455 r |= lvalue_type(type.t);
7456 memset(&ad, 0, sizeof(AttributeDef));
7457 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
7458 } else {
7459 unary();
7460 gen_cast(&type);
7462 } else if (tok == '{') {
7463 /* save all registers */
7464 save_regs(0);
7465 /* statement expression : we do not accept break/continue
7466 inside as GCC does */
7467 block(NULL, NULL, NULL, NULL, 0, 1);
7468 skip(')');
7469 } else {
7470 gexpr();
7471 skip(')');
7473 break;
7474 case '*':
7475 next();
7476 unary();
7477 indir();
7478 break;
7479 case '&':
7480 next();
7481 unary();
7482 /* functions names must be treated as function pointers,
7483 except for unary '&' and sizeof. Since we consider that
7484 functions are not lvalues, we only have to handle it
7485 there and in function calls. */
7486 /* arrays can also be used although they are not lvalues */
7487 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
7488 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
7489 test_lvalue();
7490 mk_pointer(&vtop->type);
7491 gaddrof();
7492 break;
7493 case '!':
7494 next();
7495 unary();
7496 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
7497 vtop->c.i = !vtop->c.i;
7498 else if ((vtop->r & VT_VALMASK) == VT_CMP)
7499 vtop->c.i = vtop->c.i ^ 1;
7500 else {
7501 save_regs(1);
7502 vseti(VT_JMP, gtst(1, 0));
7504 break;
7505 case '~':
7506 next();
7507 unary();
7508 vpushi(-1);
7509 gen_op('^');
7510 break;
7511 case '+':
7512 next();
7513 /* in order to force cast, we add zero */
7514 unary();
7515 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
7516 error("pointer not accepted for unary plus");
7517 vpushi(0);
7518 gen_op('+');
7519 break;
7520 case TOK_SIZEOF:
7521 case TOK_ALIGNOF1:
7522 case TOK_ALIGNOF2:
7523 t = tok;
7524 next();
7525 if (tok == '(') {
7526 parse_expr_type(&type);
7527 } else {
7528 unary_type(&type);
7530 size = type_size(&type, &align);
7531 if (t == TOK_SIZEOF) {
7532 if (size < 0)
7533 error("sizeof applied to an incomplete type");
7534 vpushi(size);
7535 } else {
7536 vpushi(align);
7538 vtop->type.t |= VT_UNSIGNED;
7539 break;
7541 case TOK_builtin_types_compatible_p:
7543 CType type1, type2;
7544 next();
7545 skip('(');
7546 parse_type(&type1);
7547 skip(',');
7548 parse_type(&type2);
7549 skip(')');
7550 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
7551 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
7552 vpushi(is_compatible_types(&type1, &type2));
7554 break;
7555 case TOK_builtin_constant_p:
7557 int saved_nocode_wanted, res;
7558 next();
7559 skip('(');
7560 saved_nocode_wanted = nocode_wanted;
7561 nocode_wanted = 1;
7562 gexpr();
7563 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
7564 vpop();
7565 nocode_wanted = saved_nocode_wanted;
7566 skip(')');
7567 vpushi(res);
7569 break;
7570 case TOK_INC:
7571 case TOK_DEC:
7572 t = tok;
7573 next();
7574 unary();
7575 inc(0, t);
7576 break;
7577 case '-':
7578 next();
7579 vpushi(0);
7580 unary();
7581 gen_op('-');
7582 break;
7583 case TOK_LAND:
7584 if (!gnu_ext)
7585 goto tok_identifier;
7586 next();
7587 /* allow to take the address of a label */
7588 if (tok < TOK_UIDENT)
7589 expect("label identifier");
7590 s = label_find(tok);
7591 if (!s) {
7592 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7593 } else {
7594 if (s->r == LABEL_DECLARED)
7595 s->r = LABEL_FORWARD;
7597 if (!s->type.t) {
7598 s->type.t = VT_VOID;
7599 mk_pointer(&s->type);
7600 s->type.t |= VT_STATIC;
7602 vset(&s->type, VT_CONST | VT_SYM, 0);
7603 vtop->sym = s;
7604 next();
7605 break;
7606 default:
7607 tok_identifier:
7608 t = tok;
7609 next();
7610 if (t < TOK_UIDENT)
7611 expect("identifier");
7612 s = sym_find(t);
7613 if (!s) {
7614 if (tok != '(')
7615 error("'%s' undeclared", get_tok_str(t, NULL));
7616 /* for simple function calls, we tolerate undeclared
7617 external reference to int() function */
7618 if (tcc_state->warn_implicit_function_declaration)
7619 warning("implicit declaration of function '%s'",
7620 get_tok_str(t, NULL));
7621 s = external_global_sym(t, &func_old_type, 0);
7623 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
7624 (VT_STATIC | VT_INLINE | VT_FUNC)) {
7625 /* if referencing an inline function, then we generate a
7626 symbol to it if not already done. It will have the
7627 effect to generate code for it at the end of the
7628 compilation unit. Inline function as always
7629 generated in the text section. */
7630 if (!s->c)
7631 put_extern_sym(s, text_section, 0, 0);
7632 r = VT_SYM | VT_CONST;
7633 } else {
7634 r = s->r;
7636 vset(&s->type, r, s->c);
7637 /* if forward reference, we must point to s */
7638 if (vtop->r & VT_SYM) {
7639 vtop->sym = s;
7640 vtop->c.ul = 0;
7642 break;
7645 /* post operations */
7646 while (1) {
7647 if (tok == TOK_INC || tok == TOK_DEC) {
7648 inc(1, tok);
7649 next();
7650 } else if (tok == '.' || tok == TOK_ARROW) {
7651 /* field */
7652 if (tok == TOK_ARROW)
7653 indir();
7654 test_lvalue();
7655 gaddrof();
7656 next();
7657 /* expect pointer on structure */
7658 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
7659 expect("struct or union");
7660 s = vtop->type.ref;
7661 /* find field */
7662 tok |= SYM_FIELD;
7663 while ((s = s->next) != NULL) {
7664 if (s->v == tok)
7665 break;
7667 if (!s)
7668 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
7669 /* add field offset to pointer */
7670 vtop->type = char_pointer_type; /* change type to 'char *' */
7671 vpushi(s->c);
7672 gen_op('+');
7673 /* change type to field type, and set to lvalue */
7674 vtop->type = s->type;
7675 /* an array is never an lvalue */
7676 if (!(vtop->type.t & VT_ARRAY)) {
7677 vtop->r |= lvalue_type(vtop->type.t);
7678 /* if bound checking, the referenced pointer must be checked */
7679 if (do_bounds_check)
7680 vtop->r |= VT_MUSTBOUND;
7682 next();
7683 } else if (tok == '[') {
7684 next();
7685 gexpr();
7686 gen_op('+');
7687 indir();
7688 skip(']');
7689 } else if (tok == '(') {
7690 SValue ret;
7691 Sym *sa;
7692 int nb_args;
7694 /* function call */
7695 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
7696 /* pointer test (no array accepted) */
7697 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
7698 vtop->type = *pointed_type(&vtop->type);
7699 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
7700 goto error_func;
7701 } else {
7702 error_func:
7703 expect("function pointer");
7705 } else {
7706 vtop->r &= ~VT_LVAL; /* no lvalue */
7708 /* get return type */
7709 s = vtop->type.ref;
7710 next();
7711 sa = s->next; /* first parameter */
7712 nb_args = 0;
7713 ret.r2 = VT_CONST;
7714 /* compute first implicit argument if a structure is returned */
7715 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
7716 /* get some space for the returned structure */
7717 size = type_size(&s->type, &align);
7718 loc = (loc - size) & -align;
7719 ret.type = s->type;
7720 ret.r = VT_LOCAL | VT_LVAL;
7721 /* pass it as 'int' to avoid structure arg passing
7722 problems */
7723 vseti(VT_LOCAL, loc);
7724 ret.c = vtop->c;
7725 nb_args++;
7726 } else {
7727 ret.type = s->type;
7728 /* return in register */
7729 if (is_float(ret.type.t)) {
7730 ret.r = REG_FRET;
7731 } else {
7732 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
7733 ret.r2 = REG_LRET;
7734 ret.r = REG_IRET;
7736 ret.c.i = 0;
7738 if (tok != ')') {
7739 for(;;) {
7740 expr_eq();
7741 gfunc_param_typed(s, sa);
7742 nb_args++;
7743 if (sa)
7744 sa = sa->next;
7745 if (tok == ')')
7746 break;
7747 skip(',');
7750 if (sa)
7751 error("too few arguments to function");
7752 skip(')');
7753 if (!nocode_wanted) {
7754 gfunc_call(nb_args);
7755 } else {
7756 vtop -= (nb_args + 1);
7758 /* return value */
7759 vsetc(&ret.type, ret.r, &ret.c);
7760 vtop->r2 = ret.r2;
7761 } else {
7762 break;
7767 static void uneq(void)
7769 int t;
7771 unary();
7772 if (tok == '=' ||
7773 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
7774 tok == TOK_A_XOR || tok == TOK_A_OR ||
7775 tok == TOK_A_SHL || tok == TOK_A_SAR) {
7776 test_lvalue();
7777 t = tok;
7778 next();
7779 if (t == '=') {
7780 expr_eq();
7781 } else {
7782 vdup();
7783 expr_eq();
7784 gen_op(t & 0x7f);
7786 vstore();
7790 static void expr_prod(void)
7792 int t;
7794 uneq();
7795 while (tok == '*' || tok == '/' || tok == '%') {
7796 t = tok;
7797 next();
7798 uneq();
7799 gen_op(t);
7803 static void expr_sum(void)
7805 int t;
7807 expr_prod();
7808 while (tok == '+' || tok == '-') {
7809 t = tok;
7810 next();
7811 expr_prod();
7812 gen_op(t);
7816 static void expr_shift(void)
7818 int t;
7820 expr_sum();
7821 while (tok == TOK_SHL || tok == TOK_SAR) {
7822 t = tok;
7823 next();
7824 expr_sum();
7825 gen_op(t);
7829 static void expr_cmp(void)
7831 int t;
7833 expr_shift();
7834 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
7835 tok == TOK_ULT || tok == TOK_UGE) {
7836 t = tok;
7837 next();
7838 expr_shift();
7839 gen_op(t);
7843 static void expr_cmpeq(void)
7845 int t;
7847 expr_cmp();
7848 while (tok == TOK_EQ || tok == TOK_NE) {
7849 t = tok;
7850 next();
7851 expr_cmp();
7852 gen_op(t);
7856 static void expr_and(void)
7858 expr_cmpeq();
7859 while (tok == '&') {
7860 next();
7861 expr_cmpeq();
7862 gen_op('&');
7866 static void expr_xor(void)
7868 expr_and();
7869 while (tok == '^') {
7870 next();
7871 expr_and();
7872 gen_op('^');
7876 static void expr_or(void)
7878 expr_xor();
7879 while (tok == '|') {
7880 next();
7881 expr_xor();
7882 gen_op('|');
7886 /* XXX: fix this mess */
7887 static void expr_land_const(void)
7889 expr_or();
7890 while (tok == TOK_LAND) {
7891 next();
7892 expr_or();
7893 gen_op(TOK_LAND);
7897 /* XXX: fix this mess */
7898 static void expr_lor_const(void)
7900 expr_land_const();
7901 while (tok == TOK_LOR) {
7902 next();
7903 expr_land_const();
7904 gen_op(TOK_LOR);
7908 /* only used if non constant */
7909 static void expr_land(void)
7911 int t;
7913 expr_or();
7914 if (tok == TOK_LAND) {
7915 t = 0;
7916 save_regs(1);
7917 for(;;) {
7918 t = gtst(1, t);
7919 if (tok != TOK_LAND) {
7920 vseti(VT_JMPI, t);
7921 break;
7923 next();
7924 expr_or();
7929 static void expr_lor(void)
7931 int t;
7933 expr_land();
7934 if (tok == TOK_LOR) {
7935 t = 0;
7936 save_regs(1);
7937 for(;;) {
7938 t = gtst(0, t);
7939 if (tok != TOK_LOR) {
7940 vseti(VT_JMP, t);
7941 break;
7943 next();
7944 expr_land();
7949 /* XXX: better constant handling */
7950 static void expr_eq(void)
7952 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7953 SValue sv;
7954 CType type, type1, type2;
7956 if (const_wanted) {
7957 int c1, c;
7958 expr_lor_const();
7959 if (tok == '?') {
7960 c = vtop->c.i;
7961 vpop();
7962 next();
7963 if (tok == ':' && gnu_ext) {
7964 c1 = c;
7965 } else {
7966 gexpr();
7967 c1 = vtop->c.i;
7968 vpop();
7970 skip(':');
7971 expr_eq();
7972 if (c)
7973 vtop->c.i = c1;
7975 } else {
7976 expr_lor();
7977 if (tok == '?') {
7978 next();
7979 if (vtop != vstack) {
7980 /* needed to avoid having different registers saved in
7981 each branch */
7982 if (is_float(vtop->type.t))
7983 rc = RC_FLOAT;
7984 else
7985 rc = RC_INT;
7986 gv(rc);
7987 save_regs(1);
7989 if (tok == ':' && gnu_ext) {
7990 gv_dup();
7991 tt = gtst(1, 0);
7992 } else {
7993 tt = gtst(1, 0);
7994 gexpr();
7996 type1 = vtop->type;
7997 sv = *vtop; /* save value to handle it later */
7998 vtop--; /* no vpop so that FP stack is not flushed */
7999 skip(':');
8000 u = gjmp(0);
8001 gsym(tt);
8002 expr_eq();
8003 type2 = vtop->type;
8005 t1 = type1.t;
8006 bt1 = t1 & VT_BTYPE;
8007 t2 = type2.t;
8008 bt2 = t2 & VT_BTYPE;
8009 /* cast operands to correct type according to ISOC rules */
8010 if (is_float(bt1) || is_float(bt2)) {
8011 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
8012 type.t = VT_LDOUBLE;
8013 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
8014 type.t = VT_DOUBLE;
8015 } else {
8016 type.t = VT_FLOAT;
8018 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
8019 /* cast to biggest op */
8020 type.t = VT_LLONG;
8021 /* convert to unsigned if it does not fit in a long long */
8022 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
8023 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
8024 type.t |= VT_UNSIGNED;
8025 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
8026 /* XXX: test pointer compatibility */
8027 type = type1;
8028 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
8029 /* XXX: test function pointer compatibility */
8030 type = type1;
8031 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
8032 /* XXX: test structure compatibility */
8033 type = type1;
8034 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
8035 /* NOTE: as an extension, we accept void on only one side */
8036 type.t = VT_VOID;
8037 } else {
8038 /* integer operations */
8039 type.t = VT_INT;
8040 /* convert to unsigned if it does not fit in an integer */
8041 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
8042 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
8043 type.t |= VT_UNSIGNED;
8046 /* now we convert second operand */
8047 gen_cast(&type);
8048 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8049 gaddrof();
8050 rc = RC_INT;
8051 if (is_float(type.t)) {
8052 rc = RC_FLOAT;
8053 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
8054 /* for long longs, we use fixed registers to avoid having
8055 to handle a complicated move */
8056 rc = RC_IRET;
8059 r2 = gv(rc);
8060 /* this is horrible, but we must also convert first
8061 operand */
8062 tt = gjmp(0);
8063 gsym(u);
8064 /* put again first value and cast it */
8065 *vtop = sv;
8066 gen_cast(&type);
8067 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
8068 gaddrof();
8069 r1 = gv(rc);
8070 move_reg(r2, r1);
8071 vtop->r = r2;
8072 gsym(tt);
8077 static void gexpr(void)
8079 while (1) {
8080 expr_eq();
8081 if (tok != ',')
8082 break;
8083 vpop();
8084 next();
8088 /* parse an expression and return its type without any side effect. */
8089 static void expr_type(CType *type)
8091 int saved_nocode_wanted;
8093 saved_nocode_wanted = nocode_wanted;
8094 nocode_wanted = 1;
8095 gexpr();
8096 *type = vtop->type;
8097 vpop();
8098 nocode_wanted = saved_nocode_wanted;
8101 /* parse a unary expression and return its type without any side
8102 effect. */
8103 static void unary_type(CType *type)
8105 int a;
8107 a = nocode_wanted;
8108 nocode_wanted = 1;
8109 unary();
8110 *type = vtop->type;
8111 vpop();
8112 nocode_wanted = a;
8115 /* parse a constant expression and return value in vtop. */
8116 static void expr_const1(void)
8118 int a;
8119 a = const_wanted;
8120 const_wanted = 1;
8121 expr_eq();
8122 const_wanted = a;
8125 /* parse an integer constant and return its value. */
8126 static int expr_const(void)
8128 int c;
8129 expr_const1();
8130 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
8131 expect("constant expression");
8132 c = vtop->c.i;
8133 vpop();
8134 return c;
8137 /* return the label token if current token is a label, otherwise
8138 return zero */
8139 static int is_label(void)
8141 int last_tok;
8143 /* fast test first */
8144 if (tok < TOK_UIDENT)
8145 return 0;
8146 /* no need to save tokc because tok is an identifier */
8147 last_tok = tok;
8148 next();
8149 if (tok == ':') {
8150 next();
8151 return last_tok;
8152 } else {
8153 unget_tok(last_tok);
8154 return 0;
8158 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
8159 int case_reg, int is_expr)
8161 int a, b, c, d;
8162 Sym *s;
8164 /* generate line number info */
8165 if (do_debug &&
8166 (last_line_num != file->line_num || last_ind != ind)) {
8167 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
8168 last_ind = ind;
8169 last_line_num = file->line_num;
8172 if (is_expr) {
8173 /* default return value is (void) */
8174 vpushi(0);
8175 vtop->type.t = VT_VOID;
8178 if (tok == TOK_IF) {
8179 /* if test */
8180 next();
8181 skip('(');
8182 gexpr();
8183 skip(')');
8184 a = gtst(1, 0);
8185 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8186 c = tok;
8187 if (c == TOK_ELSE) {
8188 next();
8189 d = gjmp(0);
8190 gsym(a);
8191 block(bsym, csym, case_sym, def_sym, case_reg, 0);
8192 gsym(d); /* patch else jmp */
8193 } else
8194 gsym(a);
8195 } else if (tok == TOK_WHILE) {
8196 next();
8197 d = ind;
8198 skip('(');
8199 gexpr();
8200 skip(')');
8201 a = gtst(1, 0);
8202 b = 0;
8203 block(&a, &b, case_sym, def_sym, case_reg, 0);
8204 gjmp_addr(d);
8205 gsym(a);
8206 gsym_addr(b, d);
8207 } else if (tok == '{') {
8208 Sym *llabel;
8210 next();
8211 /* record local declaration stack position */
8212 s = local_stack;
8213 llabel = local_label_stack;
8214 /* handle local labels declarations */
8215 if (tok == TOK_LABEL) {
8216 next();
8217 for(;;) {
8218 if (tok < TOK_UIDENT)
8219 expect("label identifier");
8220 label_push(&local_label_stack, tok, LABEL_DECLARED);
8221 next();
8222 if (tok == ',') {
8223 next();
8224 } else {
8225 skip(';');
8226 break;
8230 while (tok != '}') {
8231 decl(VT_LOCAL);
8232 if (tok != '}') {
8233 if (is_expr)
8234 vpop();
8235 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8238 /* pop locally defined labels */
8239 label_pop(&local_label_stack, llabel);
8240 /* pop locally defined symbols */
8241 sym_pop(&local_stack, s);
8242 next();
8243 } else if (tok == TOK_RETURN) {
8244 next();
8245 if (tok != ';') {
8246 gexpr();
8247 gen_assign_cast(&func_vt);
8248 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
8249 CType type;
8250 /* if returning structure, must copy it to implicit
8251 first pointer arg location */
8252 #ifdef TCC_ARM_EABI
8253 int align, size;
8254 size = type_size(&func_vt,&align);
8255 if(size <= 4)
8257 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
8258 && (align & 3))
8260 int addr;
8261 loc = (loc - size) & -4;
8262 addr = loc;
8263 type = func_vt;
8264 vset(&type, VT_LOCAL | VT_LVAL, addr);
8265 vswap();
8266 vstore();
8267 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
8269 vtop->type = int_type;
8270 gv(RC_IRET);
8271 } else {
8272 #endif
8273 type = func_vt;
8274 mk_pointer(&type);
8275 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
8276 indir();
8277 vswap();
8278 /* copy structure value to pointer */
8279 vstore();
8280 #ifdef TCC_ARM_EABI
8282 #endif
8283 } else if (is_float(func_vt.t)) {
8284 gv(RC_FRET);
8285 } else {
8286 gv(RC_IRET);
8288 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
8290 skip(';');
8291 rsym = gjmp(rsym); /* jmp */
8292 } else if (tok == TOK_BREAK) {
8293 /* compute jump */
8294 if (!bsym)
8295 error("cannot break");
8296 *bsym = gjmp(*bsym);
8297 next();
8298 skip(';');
8299 } else if (tok == TOK_CONTINUE) {
8300 /* compute jump */
8301 if (!csym)
8302 error("cannot continue");
8303 *csym = gjmp(*csym);
8304 next();
8305 skip(';');
8306 } else if (tok == TOK_FOR) {
8307 int e;
8308 next();
8309 skip('(');
8310 if (tok != ';') {
8311 gexpr();
8312 vpop();
8314 skip(';');
8315 d = ind;
8316 c = ind;
8317 a = 0;
8318 b = 0;
8319 if (tok != ';') {
8320 gexpr();
8321 a = gtst(1, 0);
8323 skip(';');
8324 if (tok != ')') {
8325 e = gjmp(0);
8326 c = ind;
8327 gexpr();
8328 vpop();
8329 gjmp_addr(d);
8330 gsym(e);
8332 skip(')');
8333 block(&a, &b, case_sym, def_sym, case_reg, 0);
8334 gjmp_addr(c);
8335 gsym(a);
8336 gsym_addr(b, c);
8337 } else
8338 if (tok == TOK_DO) {
8339 next();
8340 a = 0;
8341 b = 0;
8342 d = ind;
8343 block(&a, &b, case_sym, def_sym, case_reg, 0);
8344 skip(TOK_WHILE);
8345 skip('(');
8346 gsym(b);
8347 gexpr();
8348 c = gtst(0, 0);
8349 gsym_addr(c, d);
8350 skip(')');
8351 gsym(a);
8352 skip(';');
8353 } else
8354 if (tok == TOK_SWITCH) {
8355 next();
8356 skip('(');
8357 gexpr();
8358 /* XXX: other types than integer */
8359 case_reg = gv(RC_INT);
8360 vpop();
8361 skip(')');
8362 a = 0;
8363 b = gjmp(0); /* jump to first case */
8364 c = 0;
8365 block(&a, csym, &b, &c, case_reg, 0);
8366 /* if no default, jmp after switch */
8367 if (c == 0)
8368 c = ind;
8369 /* default label */
8370 gsym_addr(b, c);
8371 /* break label */
8372 gsym(a);
8373 } else
8374 if (tok == TOK_CASE) {
8375 int v1, v2;
8376 if (!case_sym)
8377 expect("switch");
8378 next();
8379 v1 = expr_const();
8380 v2 = v1;
8381 if (gnu_ext && tok == TOK_DOTS) {
8382 next();
8383 v2 = expr_const();
8384 if (v2 < v1)
8385 warning("empty case range");
8387 /* since a case is like a label, we must skip it with a jmp */
8388 b = gjmp(0);
8389 gsym(*case_sym);
8390 vseti(case_reg, 0);
8391 vpushi(v1);
8392 if (v1 == v2) {
8393 gen_op(TOK_EQ);
8394 *case_sym = gtst(1, 0);
8395 } else {
8396 gen_op(TOK_GE);
8397 *case_sym = gtst(1, 0);
8398 vseti(case_reg, 0);
8399 vpushi(v2);
8400 gen_op(TOK_LE);
8401 *case_sym = gtst(1, *case_sym);
8403 gsym(b);
8404 skip(':');
8405 is_expr = 0;
8406 goto block_after_label;
8407 } else
8408 if (tok == TOK_DEFAULT) {
8409 next();
8410 skip(':');
8411 if (!def_sym)
8412 expect("switch");
8413 if (*def_sym)
8414 error("too many 'default'");
8415 *def_sym = ind;
8416 is_expr = 0;
8417 goto block_after_label;
8418 } else
8419 if (tok == TOK_GOTO) {
8420 next();
8421 if (tok == '*' && gnu_ext) {
8422 /* computed goto */
8423 next();
8424 gexpr();
8425 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
8426 expect("pointer");
8427 ggoto();
8428 } else if (tok >= TOK_UIDENT) {
8429 s = label_find(tok);
8430 /* put forward definition if needed */
8431 if (!s) {
8432 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
8433 } else {
8434 if (s->r == LABEL_DECLARED)
8435 s->r = LABEL_FORWARD;
8437 /* label already defined */
8438 if (s->r & LABEL_FORWARD)
8439 s->next = (void *)gjmp((long)s->next);
8440 else
8441 gjmp_addr((long)s->next);
8442 next();
8443 } else {
8444 expect("label identifier");
8446 skip(';');
8447 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
8448 asm_instr();
8449 } else {
8450 b = is_label();
8451 if (b) {
8452 /* label case */
8453 s = label_find(b);
8454 if (s) {
8455 if (s->r == LABEL_DEFINED)
8456 error("duplicate label '%s'", get_tok_str(s->v, NULL));
8457 gsym((long)s->next);
8458 s->r = LABEL_DEFINED;
8459 } else {
8460 s = label_push(&global_label_stack, b, LABEL_DEFINED);
8462 s->next = (void *)ind;
8463 /* we accept this, but it is a mistake */
8464 block_after_label:
8465 if (tok == '}') {
8466 warning("deprecated use of label at end of compound statement");
8467 } else {
8468 if (is_expr)
8469 vpop();
8470 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
8472 } else {
8473 /* expression case */
8474 if (tok != ';') {
8475 if (is_expr) {
8476 vpop();
8477 gexpr();
8478 } else {
8479 gexpr();
8480 vpop();
8483 skip(';');
8488 /* t is the array or struct type. c is the array or struct
8489 address. cur_index/cur_field is the pointer to the current
8490 value. 'size_only' is true if only size info is needed (only used
8491 in arrays) */
8492 static void decl_designator(CType *type, Section *sec, unsigned long c,
8493 int *cur_index, Sym **cur_field,
8494 int size_only)
8496 Sym *s, *f;
8497 int notfirst, index, index_last, align, l, nb_elems, elem_size;
8498 CType type1;
8500 notfirst = 0;
8501 elem_size = 0;
8502 nb_elems = 1;
8503 if (gnu_ext && (l = is_label()) != 0)
8504 goto struct_field;
8505 while (tok == '[' || tok == '.') {
8506 if (tok == '[') {
8507 if (!(type->t & VT_ARRAY))
8508 expect("array type");
8509 s = type->ref;
8510 next();
8511 index = expr_const();
8512 if (index < 0 || (s->c >= 0 && index >= s->c))
8513 expect("invalid index");
8514 if (tok == TOK_DOTS && gnu_ext) {
8515 next();
8516 index_last = expr_const();
8517 if (index_last < 0 ||
8518 (s->c >= 0 && index_last >= s->c) ||
8519 index_last < index)
8520 expect("invalid index");
8521 } else {
8522 index_last = index;
8524 skip(']');
8525 if (!notfirst)
8526 *cur_index = index_last;
8527 type = pointed_type(type);
8528 elem_size = type_size(type, &align);
8529 c += index * elem_size;
8530 /* NOTE: we only support ranges for last designator */
8531 nb_elems = index_last - index + 1;
8532 if (nb_elems != 1) {
8533 notfirst = 1;
8534 break;
8536 } else {
8537 next();
8538 l = tok;
8539 next();
8540 struct_field:
8541 if ((type->t & VT_BTYPE) != VT_STRUCT)
8542 expect("struct/union type");
8543 s = type->ref;
8544 l |= SYM_FIELD;
8545 f = s->next;
8546 while (f) {
8547 if (f->v == l)
8548 break;
8549 f = f->next;
8551 if (!f)
8552 expect("field");
8553 if (!notfirst)
8554 *cur_field = f;
8555 /* XXX: fix this mess by using explicit storage field */
8556 type1 = f->type;
8557 type1.t |= (type->t & ~VT_TYPE);
8558 type = &type1;
8559 c += f->c;
8561 notfirst = 1;
8563 if (notfirst) {
8564 if (tok == '=') {
8565 next();
8566 } else {
8567 if (!gnu_ext)
8568 expect("=");
8570 } else {
8571 if (type->t & VT_ARRAY) {
8572 index = *cur_index;
8573 type = pointed_type(type);
8574 c += index * type_size(type, &align);
8575 } else {
8576 f = *cur_field;
8577 if (!f)
8578 error("too many field init");
8579 /* XXX: fix this mess by using explicit storage field */
8580 type1 = f->type;
8581 type1.t |= (type->t & ~VT_TYPE);
8582 type = &type1;
8583 c += f->c;
8586 decl_initializer(type, sec, c, 0, size_only);
8588 /* XXX: make it more general */
8589 if (!size_only && nb_elems > 1) {
8590 unsigned long c_end;
8591 uint8_t *src, *dst;
8592 int i;
8594 if (!sec)
8595 error("range init not supported yet for dynamic storage");
8596 c_end = c + nb_elems * elem_size;
8597 if (c_end > sec->data_allocated)
8598 section_realloc(sec, c_end);
8599 src = sec->data + c;
8600 dst = src;
8601 for(i = 1; i < nb_elems; i++) {
8602 dst += elem_size;
8603 memcpy(dst, src, elem_size);
8608 #define EXPR_VAL 0
8609 #define EXPR_CONST 1
8610 #define EXPR_ANY 2
8612 /* store a value or an expression directly in global data or in local array */
8613 static void init_putv(CType *type, Section *sec, unsigned long c,
8614 int v, int expr_type)
8616 int saved_global_expr, bt, bit_pos, bit_size;
8617 void *ptr;
8618 unsigned long long bit_mask;
8619 CType dtype;
8621 switch(expr_type) {
8622 case EXPR_VAL:
8623 vpushi(v);
8624 break;
8625 case EXPR_CONST:
8626 /* compound literals must be allocated globally in this case */
8627 saved_global_expr = global_expr;
8628 global_expr = 1;
8629 expr_const1();
8630 global_expr = saved_global_expr;
8631 /* NOTE: symbols are accepted */
8632 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
8633 error("initializer element is not constant");
8634 break;
8635 case EXPR_ANY:
8636 expr_eq();
8637 break;
8640 dtype = *type;
8641 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
8643 if (sec) {
8644 /* XXX: not portable */
8645 /* XXX: generate error if incorrect relocation */
8646 gen_assign_cast(&dtype);
8647 bt = type->t & VT_BTYPE;
8648 ptr = sec->data + c;
8649 /* XXX: make code faster ? */
8650 if (!(type->t & VT_BITFIELD)) {
8651 bit_pos = 0;
8652 bit_size = 32;
8653 bit_mask = -1LL;
8654 } else {
8655 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
8656 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
8657 bit_mask = (1LL << bit_size) - 1;
8659 if ((vtop->r & VT_SYM) &&
8660 (bt == VT_BYTE ||
8661 bt == VT_SHORT ||
8662 bt == VT_DOUBLE ||
8663 bt == VT_LDOUBLE ||
8664 bt == VT_LLONG ||
8665 (bt == VT_INT && bit_size != 32)))
8666 error("initializer element is not computable at load time");
8667 switch(bt) {
8668 case VT_BYTE:
8669 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8670 break;
8671 case VT_SHORT:
8672 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8673 break;
8674 case VT_DOUBLE:
8675 *(double *)ptr = vtop->c.d;
8676 break;
8677 case VT_LDOUBLE:
8678 *(long double *)ptr = vtop->c.ld;
8679 break;
8680 case VT_LLONG:
8681 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
8682 break;
8683 default:
8684 if (vtop->r & VT_SYM) {
8685 greloc(sec, vtop->sym, c, R_DATA_32);
8687 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
8688 break;
8690 vtop--;
8691 } else {
8692 vset(&dtype, VT_LOCAL|VT_LVAL, c);
8693 vswap();
8694 vstore();
8695 vpop();
8699 /* put zeros for variable based init */
8700 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
8702 if (sec) {
8703 /* nothing to do because globals are already set to zero */
8704 } else {
8705 vpush_global_sym(&func_old_type, TOK_memset);
8706 vseti(VT_LOCAL, c);
8707 vpushi(0);
8708 vpushi(size);
8709 gfunc_call(3);
8713 /* 't' contains the type and storage info. 'c' is the offset of the
8714 object in section 'sec'. If 'sec' is NULL, it means stack based
8715 allocation. 'first' is true if array '{' must be read (multi
8716 dimension implicit array init handling). 'size_only' is true if
8717 size only evaluation is wanted (only for arrays). */
8718 static void decl_initializer(CType *type, Section *sec, unsigned long c,
8719 int first, int size_only)
8721 int index, array_length, n, no_oblock, nb, parlevel, i;
8722 int size1, align1, expr_type;
8723 Sym *s, *f;
8724 CType *t1;
8726 if (type->t & VT_ARRAY) {
8727 s = type->ref;
8728 n = s->c;
8729 array_length = 0;
8730 t1 = pointed_type(type);
8731 size1 = type_size(t1, &align1);
8733 no_oblock = 1;
8734 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
8735 tok == '{') {
8736 skip('{');
8737 no_oblock = 0;
8740 /* only parse strings here if correct type (otherwise: handle
8741 them as ((w)char *) expressions */
8742 if ((tok == TOK_LSTR &&
8743 #ifdef TCC_TARGET_PE
8744 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
8745 #else
8746 (t1->t & VT_BTYPE) == VT_INT
8747 #endif
8748 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
8749 while (tok == TOK_STR || tok == TOK_LSTR) {
8750 int cstr_len, ch;
8751 CString *cstr;
8753 cstr = tokc.cstr;
8754 /* compute maximum number of chars wanted */
8755 if (tok == TOK_STR)
8756 cstr_len = cstr->size;
8757 else
8758 cstr_len = cstr->size / sizeof(nwchar_t);
8759 cstr_len--;
8760 nb = cstr_len;
8761 if (n >= 0 && nb > (n - array_length))
8762 nb = n - array_length;
8763 if (!size_only) {
8764 if (cstr_len > nb)
8765 warning("initializer-string for array is too long");
8766 /* in order to go faster for common case (char
8767 string in global variable, we handle it
8768 specifically */
8769 if (sec && tok == TOK_STR && size1 == 1) {
8770 memcpy(sec->data + c + array_length, cstr->data, nb);
8771 } else {
8772 for(i=0;i<nb;i++) {
8773 if (tok == TOK_STR)
8774 ch = ((unsigned char *)cstr->data)[i];
8775 else
8776 ch = ((nwchar_t *)cstr->data)[i];
8777 init_putv(t1, sec, c + (array_length + i) * size1,
8778 ch, EXPR_VAL);
8782 array_length += nb;
8783 next();
8785 /* only add trailing zero if enough storage (no
8786 warning in this case since it is standard) */
8787 if (n < 0 || array_length < n) {
8788 if (!size_only) {
8789 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
8791 array_length++;
8793 } else {
8794 index = 0;
8795 while (tok != '}') {
8796 decl_designator(type, sec, c, &index, NULL, size_only);
8797 if (n >= 0 && index >= n)
8798 error("index too large");
8799 /* must put zero in holes (note that doing it that way
8800 ensures that it even works with designators) */
8801 if (!size_only && array_length < index) {
8802 init_putz(t1, sec, c + array_length * size1,
8803 (index - array_length) * size1);
8805 index++;
8806 if (index > array_length)
8807 array_length = index;
8808 /* special test for multi dimensional arrays (may not
8809 be strictly correct if designators are used at the
8810 same time) */
8811 if (index >= n && no_oblock)
8812 break;
8813 if (tok == '}')
8814 break;
8815 skip(',');
8818 if (!no_oblock)
8819 skip('}');
8820 /* put zeros at the end */
8821 if (!size_only && n >= 0 && array_length < n) {
8822 init_putz(t1, sec, c + array_length * size1,
8823 (n - array_length) * size1);
8825 /* patch type size if needed */
8826 if (n < 0)
8827 s->c = array_length;
8828 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
8829 (sec || !first || tok == '{')) {
8830 int par_count;
8832 /* NOTE: the previous test is a specific case for automatic
8833 struct/union init */
8834 /* XXX: union needs only one init */
8836 /* XXX: this test is incorrect for local initializers
8837 beginning with ( without {. It would be much more difficult
8838 to do it correctly (ideally, the expression parser should
8839 be used in all cases) */
8840 par_count = 0;
8841 if (tok == '(') {
8842 AttributeDef ad1;
8843 CType type1;
8844 next();
8845 while (tok == '(') {
8846 par_count++;
8847 next();
8849 if (!parse_btype(&type1, &ad1))
8850 expect("cast");
8851 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
8852 #if 0
8853 if (!is_assignable_types(type, &type1))
8854 error("invalid type for cast");
8855 #endif
8856 skip(')');
8858 no_oblock = 1;
8859 if (first || tok == '{') {
8860 skip('{');
8861 no_oblock = 0;
8863 s = type->ref;
8864 f = s->next;
8865 array_length = 0;
8866 index = 0;
8867 n = s->c;
8868 while (tok != '}') {
8869 decl_designator(type, sec, c, NULL, &f, size_only);
8870 index = f->c;
8871 if (!size_only && array_length < index) {
8872 init_putz(type, sec, c + array_length,
8873 index - array_length);
8875 index = index + type_size(&f->type, &align1);
8876 if (index > array_length)
8877 array_length = index;
8878 f = f->next;
8879 if (no_oblock && f == NULL)
8880 break;
8881 if (tok == '}')
8882 break;
8883 skip(',');
8885 /* put zeros at the end */
8886 if (!size_only && array_length < n) {
8887 init_putz(type, sec, c + array_length,
8888 n - array_length);
8890 if (!no_oblock)
8891 skip('}');
8892 while (par_count) {
8893 skip(')');
8894 par_count--;
8896 } else if (tok == '{') {
8897 next();
8898 decl_initializer(type, sec, c, first, size_only);
8899 skip('}');
8900 } else if (size_only) {
8901 /* just skip expression */
8902 parlevel = 0;
8903 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
8904 tok != -1) {
8905 if (tok == '(')
8906 parlevel++;
8907 else if (tok == ')')
8908 parlevel--;
8909 next();
8911 } else {
8912 /* currently, we always use constant expression for globals
8913 (may change for scripting case) */
8914 expr_type = EXPR_CONST;
8915 if (!sec)
8916 expr_type = EXPR_ANY;
8917 init_putv(type, sec, c, 0, expr_type);
8921 /* parse an initializer for type 't' if 'has_init' is non zero, and
8922 allocate space in local or global data space ('r' is either
8923 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8924 variable 'v' of scope 'scope' is declared before initializers are
8925 parsed. If 'v' is zero, then a reference to the new object is put
8926 in the value stack. If 'has_init' is 2, a special parsing is done
8927 to handle string constants. */
8928 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
8929 int has_init, int v, int scope)
8931 int size, align, addr, data_offset;
8932 int level;
8933 ParseState saved_parse_state;
8934 TokenString init_str;
8935 Section *sec;
8937 size = type_size(type, &align);
8938 /* If unknown size, we must evaluate it before
8939 evaluating initializers because
8940 initializers can generate global data too
8941 (e.g. string pointers or ISOC99 compound
8942 literals). It also simplifies local
8943 initializers handling */
8944 tok_str_new(&init_str);
8945 if (size < 0) {
8946 if (!has_init)
8947 error("unknown type size");
8948 /* get all init string */
8949 if (has_init == 2) {
8950 /* only get strings */
8951 while (tok == TOK_STR || tok == TOK_LSTR) {
8952 tok_str_add_tok(&init_str);
8953 next();
8955 } else {
8956 level = 0;
8957 while (level > 0 || (tok != ',' && tok != ';')) {
8958 if (tok < 0)
8959 error("unexpected end of file in initializer");
8960 tok_str_add_tok(&init_str);
8961 if (tok == '{')
8962 level++;
8963 else if (tok == '}') {
8964 if (level == 0)
8965 break;
8966 level--;
8968 next();
8971 tok_str_add(&init_str, -1);
8972 tok_str_add(&init_str, 0);
8974 /* compute size */
8975 save_parse_state(&saved_parse_state);
8977 macro_ptr = init_str.str;
8978 next();
8979 decl_initializer(type, NULL, 0, 1, 1);
8980 /* prepare second initializer parsing */
8981 macro_ptr = init_str.str;
8982 next();
8984 /* if still unknown size, error */
8985 size = type_size(type, &align);
8986 if (size < 0)
8987 error("unknown type size");
8989 /* take into account specified alignment if bigger */
8990 if (ad->aligned) {
8991 if (ad->aligned > align)
8992 align = ad->aligned;
8993 } else if (ad->packed) {
8994 align = 1;
8996 if ((r & VT_VALMASK) == VT_LOCAL) {
8997 sec = NULL;
8998 if (do_bounds_check && (type->t & VT_ARRAY))
8999 loc--;
9000 loc = (loc - size) & -align;
9001 addr = loc;
9002 /* handles bounds */
9003 /* XXX: currently, since we do only one pass, we cannot track
9004 '&' operators, so we add only arrays */
9005 if (do_bounds_check && (type->t & VT_ARRAY)) {
9006 unsigned long *bounds_ptr;
9007 /* add padding between regions */
9008 loc--;
9009 /* then add local bound info */
9010 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
9011 bounds_ptr[0] = addr;
9012 bounds_ptr[1] = size;
9014 if (v) {
9015 /* local variable */
9016 sym_push(v, type, r, addr);
9017 } else {
9018 /* push local reference */
9019 vset(type, r, addr);
9021 } else {
9022 Sym *sym;
9024 sym = NULL;
9025 if (v && scope == VT_CONST) {
9026 /* see if the symbol was already defined */
9027 sym = sym_find(v);
9028 if (sym) {
9029 if (!is_compatible_types(&sym->type, type))
9030 error("incompatible types for redefinition of '%s'",
9031 get_tok_str(v, NULL));
9032 if (sym->type.t & VT_EXTERN) {
9033 /* if the variable is extern, it was not allocated */
9034 sym->type.t &= ~VT_EXTERN;
9035 /* set array size if it was ommited in extern
9036 declaration */
9037 if ((sym->type.t & VT_ARRAY) &&
9038 sym->type.ref->c < 0 &&
9039 type->ref->c >= 0)
9040 sym->type.ref->c = type->ref->c;
9041 } else {
9042 /* we accept several definitions of the same
9043 global variable. this is tricky, because we
9044 must play with the SHN_COMMON type of the symbol */
9045 /* XXX: should check if the variable was already
9046 initialized. It is incorrect to initialized it
9047 twice */
9048 /* no init data, we won't add more to the symbol */
9049 if (!has_init)
9050 goto no_alloc;
9055 /* allocate symbol in corresponding section */
9056 sec = ad->section;
9057 if (!sec) {
9058 if (has_init)
9059 sec = data_section;
9060 else if (tcc_state->nocommon)
9061 sec = bss_section;
9063 if (sec) {
9064 data_offset = sec->data_offset;
9065 data_offset = (data_offset + align - 1) & -align;
9066 addr = data_offset;
9067 /* very important to increment global pointer at this time
9068 because initializers themselves can create new initializers */
9069 data_offset += size;
9070 /* add padding if bound check */
9071 if (do_bounds_check)
9072 data_offset++;
9073 sec->data_offset = data_offset;
9074 /* allocate section space to put the data */
9075 if (sec->sh_type != SHT_NOBITS &&
9076 data_offset > sec->data_allocated)
9077 section_realloc(sec, data_offset);
9078 /* align section if needed */
9079 if (align > sec->sh_addralign)
9080 sec->sh_addralign = align;
9081 } else {
9082 addr = 0; /* avoid warning */
9085 if (v) {
9086 if (scope != VT_CONST || !sym) {
9087 sym = sym_push(v, type, r | VT_SYM, 0);
9089 /* update symbol definition */
9090 if (sec) {
9091 put_extern_sym(sym, sec, addr, size);
9092 } else {
9093 Elf32_Sym *esym;
9094 /* put a common area */
9095 put_extern_sym(sym, NULL, align, size);
9096 /* XXX: find a nicer way */
9097 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
9098 esym->st_shndx = SHN_COMMON;
9100 } else {
9101 CValue cval;
9103 /* push global reference */
9104 sym = get_sym_ref(type, sec, addr, size);
9105 cval.ul = 0;
9106 vsetc(type, VT_CONST | VT_SYM, &cval);
9107 vtop->sym = sym;
9110 /* handles bounds now because the symbol must be defined
9111 before for the relocation */
9112 if (do_bounds_check) {
9113 unsigned long *bounds_ptr;
9115 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
9116 /* then add global bound info */
9117 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
9118 bounds_ptr[0] = 0; /* relocated */
9119 bounds_ptr[1] = size;
9122 if (has_init) {
9123 decl_initializer(type, sec, addr, 1, 0);
9124 /* restore parse state if needed */
9125 if (init_str.str) {
9126 tok_str_free(init_str.str);
9127 restore_parse_state(&saved_parse_state);
9130 no_alloc: ;
9133 void put_func_debug(Sym *sym)
9135 char buf[512];
9137 /* stabs info */
9138 /* XXX: we put here a dummy type */
9139 snprintf(buf, sizeof(buf), "%s:%c1",
9140 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
9141 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
9142 cur_text_section, sym->c);
9143 last_ind = 0;
9144 last_line_num = 0;
9147 /* parse an old style function declaration list */
9148 /* XXX: check multiple parameter */
9149 static void func_decl_list(Sym *func_sym)
9151 AttributeDef ad;
9152 int v;
9153 Sym *s;
9154 CType btype, type;
9156 /* parse each declaration */
9157 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
9158 if (!parse_btype(&btype, &ad))
9159 expect("declaration list");
9160 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9161 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9162 tok == ';') {
9163 /* we accept no variable after */
9164 } else {
9165 for(;;) {
9166 type = btype;
9167 type_decl(&type, &ad, &v, TYPE_DIRECT);
9168 /* find parameter in function parameter list */
9169 s = func_sym->next;
9170 while (s != NULL) {
9171 if ((s->v & ~SYM_FIELD) == v)
9172 goto found;
9173 s = s->next;
9175 error("declaration for parameter '%s' but no such parameter",
9176 get_tok_str(v, NULL));
9177 found:
9178 /* check that no storage specifier except 'register' was given */
9179 if (type.t & VT_STORAGE)
9180 error("storage class specified for '%s'", get_tok_str(v, NULL));
9181 convert_parameter_type(&type);
9182 /* we can add the type (NOTE: it could be local to the function) */
9183 s->type = type;
9184 /* accept other parameters */
9185 if (tok == ',')
9186 next();
9187 else
9188 break;
9191 skip(';');
9195 /* parse a function defined by symbol 'sym' and generate its code in
9196 'cur_text_section' */
9197 static void gen_function(Sym *sym)
9199 int saved_nocode_wanted = nocode_wanted;
9200 nocode_wanted = 0;
9201 ind = cur_text_section->data_offset;
9202 /* NOTE: we patch the symbol size later */
9203 put_extern_sym(sym, cur_text_section, ind, 0);
9204 funcname = get_tok_str(sym->v, NULL);
9205 func_ind = ind;
9206 /* put debug symbol */
9207 if (do_debug)
9208 put_func_debug(sym);
9209 /* push a dummy symbol to enable local sym storage */
9210 sym_push2(&local_stack, SYM_FIELD, 0, 0);
9211 gfunc_prolog(&sym->type);
9212 rsym = 0;
9213 block(NULL, NULL, NULL, NULL, 0, 0);
9214 gsym(rsym);
9215 gfunc_epilog();
9216 cur_text_section->data_offset = ind;
9217 label_pop(&global_label_stack, NULL);
9218 sym_pop(&local_stack, NULL); /* reset local stack */
9219 /* end of function */
9220 /* patch symbol size */
9221 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
9222 ind - func_ind;
9223 if (do_debug) {
9224 put_stabn(N_FUN, 0, 0, ind - func_ind);
9226 funcname = ""; /* for safety */
9227 func_vt.t = VT_VOID; /* for safety */
9228 ind = 0; /* for safety */
9229 nocode_wanted = saved_nocode_wanted;
9232 static void gen_inline_functions(void)
9234 Sym *sym;
9235 CType *type;
9236 int *str, inline_generated;
9238 /* iterate while inline function are referenced */
9239 for(;;) {
9240 inline_generated = 0;
9241 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9242 type = &sym->type;
9243 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9244 (type->t & (VT_STATIC | VT_INLINE)) ==
9245 (VT_STATIC | VT_INLINE) &&
9246 sym->c != 0) {
9247 /* the function was used: generate its code and
9248 convert it to a normal function */
9249 str = INLINE_DEF(sym->r);
9250 sym->r = VT_SYM | VT_CONST;
9251 sym->type.t &= ~VT_INLINE;
9253 macro_ptr = str;
9254 next();
9255 cur_text_section = text_section;
9256 gen_function(sym);
9257 macro_ptr = NULL; /* fail safe */
9259 tok_str_free(str);
9260 inline_generated = 1;
9263 if (!inline_generated)
9264 break;
9267 /* free all remaining inline function tokens */
9268 for(sym = global_stack; sym != NULL; sym = sym->prev) {
9269 type = &sym->type;
9270 if (((type->t & VT_BTYPE) == VT_FUNC) &&
9271 (type->t & (VT_STATIC | VT_INLINE)) ==
9272 (VT_STATIC | VT_INLINE)) {
9273 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9274 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
9275 continue;
9276 str = INLINE_DEF(sym->r);
9277 tok_str_free(str);
9278 sym->r = 0; /* fail safe */
9283 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9284 static void decl(int l)
9286 int v, has_init, r;
9287 CType type, btype;
9288 Sym *sym;
9289 AttributeDef ad;
9291 while (1) {
9292 if (!parse_btype(&btype, &ad)) {
9293 /* skip redundant ';' */
9294 /* XXX: find more elegant solution */
9295 if (tok == ';') {
9296 next();
9297 continue;
9299 if (l == VT_CONST &&
9300 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
9301 /* global asm block */
9302 asm_global_instr();
9303 continue;
9305 /* special test for old K&R protos without explicit int
9306 type. Only accepted when defining global data */
9307 if (l == VT_LOCAL || tok < TOK_DEFINE)
9308 break;
9309 btype.t = VT_INT;
9311 if (((btype.t & VT_BTYPE) == VT_ENUM ||
9312 (btype.t & VT_BTYPE) == VT_STRUCT) &&
9313 tok == ';') {
9314 /* we accept no variable after */
9315 next();
9316 continue;
9318 while (1) { /* iterate thru each declaration */
9319 type = btype;
9320 type_decl(&type, &ad, &v, TYPE_DIRECT);
9321 #if 0
9323 char buf[500];
9324 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
9325 printf("type = '%s'\n", buf);
9327 #endif
9328 if ((type.t & VT_BTYPE) == VT_FUNC) {
9329 /* if old style function prototype, we accept a
9330 declaration list */
9331 sym = type.ref;
9332 if (sym->c == FUNC_OLD)
9333 func_decl_list(sym);
9336 if (tok == '{') {
9337 if (l == VT_LOCAL)
9338 error("cannot use local functions");
9339 if ((type.t & VT_BTYPE) != VT_FUNC)
9340 expect("function definition");
9342 /* reject abstract declarators in function definition */
9343 sym = type.ref;
9344 while ((sym = sym->next) != NULL)
9345 if (!(sym->v & ~SYM_FIELD))
9346 expect("identifier");
9348 /* XXX: cannot do better now: convert extern line to static inline */
9349 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
9350 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
9352 sym = sym_find(v);
9353 if (sym) {
9354 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
9355 goto func_error1;
9356 /* specific case: if not func_call defined, we put
9357 the one of the prototype */
9358 /* XXX: should have default value */
9359 r = sym->type.ref->r;
9360 if (FUNC_CALL(r) != FUNC_CDECL
9361 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
9362 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
9363 if (FUNC_EXPORT(r))
9364 FUNC_EXPORT(type.ref->r) = 1;
9366 if (!is_compatible_types(&sym->type, &type)) {
9367 func_error1:
9368 error("incompatible types for redefinition of '%s'",
9369 get_tok_str(v, NULL));
9371 /* if symbol is already defined, then put complete type */
9372 sym->type = type;
9373 } else {
9374 /* put function symbol */
9375 sym = global_identifier_push(v, type.t, 0);
9376 sym->type.ref = type.ref;
9379 /* static inline functions are just recorded as a kind
9380 of macro. Their code will be emitted at the end of
9381 the compilation unit only if they are used */
9382 if ((type.t & (VT_INLINE | VT_STATIC)) ==
9383 (VT_INLINE | VT_STATIC)) {
9384 TokenString func_str;
9385 int block_level;
9387 tok_str_new(&func_str);
9389 block_level = 0;
9390 for(;;) {
9391 int t;
9392 if (tok == TOK_EOF)
9393 error("unexpected end of file");
9394 tok_str_add_tok(&func_str);
9395 t = tok;
9396 next();
9397 if (t == '{') {
9398 block_level++;
9399 } else if (t == '}') {
9400 block_level--;
9401 if (block_level == 0)
9402 break;
9405 tok_str_add(&func_str, -1);
9406 tok_str_add(&func_str, 0);
9407 INLINE_DEF(sym->r) = func_str.str;
9408 } else {
9409 /* compute text section */
9410 cur_text_section = ad.section;
9411 if (!cur_text_section)
9412 cur_text_section = text_section;
9413 sym->r = VT_SYM | VT_CONST;
9414 gen_function(sym);
9416 break;
9417 } else {
9418 if (btype.t & VT_TYPEDEF) {
9419 /* save typedefed type */
9420 /* XXX: test storage specifiers ? */
9421 sym = sym_push(v, &type, 0, 0);
9422 sym->type.t |= VT_TYPEDEF;
9423 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
9424 /* external function definition */
9425 /* specific case for func_call attribute */
9426 if (ad.func_attr)
9427 type.ref->r = ad.func_attr;
9428 external_sym(v, &type, 0);
9429 } else {
9430 /* not lvalue if array */
9431 r = 0;
9432 if (!(type.t & VT_ARRAY))
9433 r |= lvalue_type(type.t);
9434 has_init = (tok == '=');
9435 if ((btype.t & VT_EXTERN) ||
9436 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
9437 !has_init && l == VT_CONST && type.ref->c < 0)) {
9438 /* external variable */
9439 /* NOTE: as GCC, uninitialized global static
9440 arrays of null size are considered as
9441 extern */
9442 external_sym(v, &type, r);
9443 } else {
9444 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
9445 if (type.t & VT_STATIC)
9446 r |= VT_CONST;
9447 else
9448 r |= l;
9449 if (has_init)
9450 next();
9451 decl_initializer_alloc(&type, &ad, r,
9452 has_init, v, l);
9455 if (tok != ',') {
9456 skip(';');
9457 break;
9459 next();
9465 /* better than nothing, but needs extension to handle '-E' option
9466 correctly too */
9467 static void preprocess_init(TCCState *s1)
9469 s1->include_stack_ptr = s1->include_stack;
9470 /* XXX: move that before to avoid having to initialize
9471 file->ifdef_stack_ptr ? */
9472 s1->ifdef_stack_ptr = s1->ifdef_stack;
9473 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
9475 /* XXX: not ANSI compliant: bound checking says error */
9476 vtop = vstack - 1;
9477 s1->pack_stack[0] = 0;
9478 s1->pack_stack_ptr = s1->pack_stack;
9481 /* compile the C file opened in 'file'. Return non zero if errors. */
9482 static int tcc_compile(TCCState *s1)
9484 Sym *define_start;
9485 char buf[512];
9486 volatile int section_sym;
9488 #ifdef INC_DEBUG
9489 printf("%s: **** new file\n", file->filename);
9490 #endif
9491 preprocess_init(s1);
9493 funcname = "";
9494 anon_sym = SYM_FIRST_ANOM;
9496 /* file info: full path + filename */
9497 section_sym = 0; /* avoid warning */
9498 if (do_debug) {
9499 section_sym = put_elf_sym(symtab_section, 0, 0,
9500 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
9501 text_section->sh_num, NULL);
9502 getcwd(buf, sizeof(buf));
9503 #ifdef _WIN32
9504 normalize_slashes(buf);
9505 #endif
9506 pstrcat(buf, sizeof(buf), "/");
9507 put_stabs_r(buf, N_SO, 0, 0,
9508 text_section->data_offset, text_section, section_sym);
9509 put_stabs_r(file->filename, N_SO, 0, 0,
9510 text_section->data_offset, text_section, section_sym);
9512 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9513 symbols can be safely used */
9514 put_elf_sym(symtab_section, 0, 0,
9515 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
9516 SHN_ABS, file->filename);
9518 /* define some often used types */
9519 int_type.t = VT_INT;
9521 char_pointer_type.t = VT_BYTE;
9522 mk_pointer(&char_pointer_type);
9524 func_old_type.t = VT_FUNC;
9525 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
9527 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9528 float_type.t = VT_FLOAT;
9529 double_type.t = VT_DOUBLE;
9531 func_float_type.t = VT_FUNC;
9532 func_float_type.ref = sym_push(SYM_FIELD, &float_type, FUNC_CDECL, FUNC_OLD);
9533 func_double_type.t = VT_FUNC;
9534 func_double_type.ref = sym_push(SYM_FIELD, &double_type, FUNC_CDECL, FUNC_OLD);
9535 #endif
9537 #if 0
9538 /* define 'void *alloca(unsigned int)' builtin function */
9540 Sym *s1;
9542 p = anon_sym++;
9543 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
9544 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
9545 s1->next = NULL;
9546 sym->next = s1;
9547 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
9549 #endif
9551 define_start = define_stack;
9552 nocode_wanted = 1;
9554 if (setjmp(s1->error_jmp_buf) == 0) {
9555 s1->nb_errors = 0;
9556 s1->error_set_jmp_enabled = 1;
9558 ch = file->buf_ptr[0];
9559 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9560 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
9561 next();
9562 decl(VT_CONST);
9563 if (tok != TOK_EOF)
9564 expect("declaration");
9566 /* end of translation unit info */
9567 if (do_debug) {
9568 put_stabs_r(NULL, N_SO, 0, 0,
9569 text_section->data_offset, text_section, section_sym);
9572 s1->error_set_jmp_enabled = 0;
9574 /* reset define stack, but leave -Dsymbols (may be incorrect if
9575 they are undefined) */
9576 free_defines(define_start);
9578 gen_inline_functions();
9580 sym_pop(&global_stack, NULL);
9582 return s1->nb_errors != 0 ? -1 : 0;
9585 /* Preprocess the current file */
9586 /* XXX: add line and file infos, add options to preserve spaces */
9587 static int tcc_preprocess(TCCState *s1)
9589 Sym *define_start;
9590 int last_is_space;
9592 preprocess_init(s1);
9594 define_start = define_stack;
9596 ch = file->buf_ptr[0];
9597 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
9598 parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
9599 PARSE_FLAG_LINEFEED;
9600 last_is_space = 1;
9601 next();
9602 for(;;) {
9603 if (tok == TOK_EOF) {
9604 break;
9605 } else if (tok == TOK_LINEFEED) {
9606 last_is_space = 1;
9607 } else {
9608 if (!last_is_space)
9609 fputc(' ', s1->outfile);
9610 last_is_space = 0;
9612 fputs(get_tok_str(tok, &tokc), s1->outfile);
9613 next();
9615 free_defines(define_start);
9616 return 0;
9619 #ifdef LIBTCC
9620 int tcc_compile_string(TCCState *s, const char *str)
9622 BufferedFile bf1, *bf = &bf1;
9623 int ret, len;
9624 char *buf;
9626 /* init file structure */
9627 bf->fd = -1;
9628 /* XXX: avoid copying */
9629 len = strlen(str);
9630 buf = tcc_malloc(len + 1);
9631 if (!buf)
9632 return -1;
9633 memcpy(buf, str, len);
9634 buf[len] = CH_EOB;
9635 bf->buf_ptr = buf;
9636 bf->buf_end = buf + len;
9637 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
9638 bf->line_num = 1;
9639 file = bf;
9641 ret = tcc_compile(s);
9643 tcc_free(buf);
9645 /* currently, no need to close */
9646 return ret;
9648 #endif
9650 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9651 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
9653 BufferedFile bf1, *bf = &bf1;
9655 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
9656 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
9657 /* default value */
9658 if (!value)
9659 value = "1";
9660 pstrcat(bf->buffer, IO_BUF_SIZE, value);
9662 /* init file structure */
9663 bf->fd = -1;
9664 bf->buf_ptr = bf->buffer;
9665 bf->buf_end = bf->buffer + strlen(bf->buffer);
9666 *bf->buf_end = CH_EOB;
9667 bf->filename[0] = '\0';
9668 bf->line_num = 1;
9669 file = bf;
9671 s1->include_stack_ptr = s1->include_stack;
9673 /* parse with define parser */
9674 ch = file->buf_ptr[0];
9675 next_nomacro();
9676 parse_define();
9677 file = NULL;
9680 /* undefine a preprocessor symbol */
9681 void tcc_undefine_symbol(TCCState *s1, const char *sym)
9683 TokenSym *ts;
9684 Sym *s;
9685 ts = tok_alloc(sym, strlen(sym));
9686 s = define_find(ts->tok);
9687 /* undefine symbol by putting an invalid name */
9688 if (s)
9689 define_undef(s);
9692 #ifdef CONFIG_TCC_ASM
9694 #ifdef TCC_TARGET_I386
9695 #include "i386-asm.c"
9696 #endif
9697 #include "tccasm.c"
9699 #else
9700 static void asm_instr(void)
9702 error("inline asm() not supported");
9704 static void asm_global_instr(void)
9706 error("inline asm() not supported");
9708 #endif
9710 #include "tccelf.c"
9712 #ifdef TCC_TARGET_COFF
9713 #include "tcccoff.c"
9714 #endif
9716 #ifdef TCC_TARGET_PE
9717 #include "tccpe.c"
9718 #endif
9720 /* print the position in the source file of PC value 'pc' by reading
9721 the stabs debug information */
9722 static void rt_printline(unsigned long wanted_pc)
9724 Stab_Sym *sym, *sym_end;
9725 char func_name[128], last_func_name[128];
9726 unsigned long func_addr, last_pc, pc;
9727 const char *incl_files[INCLUDE_STACK_SIZE];
9728 int incl_index, len, last_line_num, i;
9729 const char *str, *p;
9731 fprintf(stderr, "0x%08lx:", wanted_pc);
9733 func_name[0] = '\0';
9734 func_addr = 0;
9735 incl_index = 0;
9736 last_func_name[0] = '\0';
9737 last_pc = 0xffffffff;
9738 last_line_num = 1;
9739 sym = (Stab_Sym *)stab_section->data + 1;
9740 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
9741 while (sym < sym_end) {
9742 switch(sym->n_type) {
9743 /* function start or end */
9744 case N_FUN:
9745 if (sym->n_strx == 0) {
9746 /* we test if between last line and end of function */
9747 pc = sym->n_value + func_addr;
9748 if (wanted_pc >= last_pc && wanted_pc < pc)
9749 goto found;
9750 func_name[0] = '\0';
9751 func_addr = 0;
9752 } else {
9753 str = stabstr_section->data + sym->n_strx;
9754 p = strchr(str, ':');
9755 if (!p) {
9756 pstrcpy(func_name, sizeof(func_name), str);
9757 } else {
9758 len = p - str;
9759 if (len > sizeof(func_name) - 1)
9760 len = sizeof(func_name) - 1;
9761 memcpy(func_name, str, len);
9762 func_name[len] = '\0';
9764 func_addr = sym->n_value;
9766 break;
9767 /* line number info */
9768 case N_SLINE:
9769 pc = sym->n_value + func_addr;
9770 if (wanted_pc >= last_pc && wanted_pc < pc)
9771 goto found;
9772 last_pc = pc;
9773 last_line_num = sym->n_desc;
9774 /* XXX: slow! */
9775 strcpy(last_func_name, func_name);
9776 break;
9777 /* include files */
9778 case N_BINCL:
9779 str = stabstr_section->data + sym->n_strx;
9780 add_incl:
9781 if (incl_index < INCLUDE_STACK_SIZE) {
9782 incl_files[incl_index++] = str;
9784 break;
9785 case N_EINCL:
9786 if (incl_index > 1)
9787 incl_index--;
9788 break;
9789 case N_SO:
9790 if (sym->n_strx == 0) {
9791 incl_index = 0; /* end of translation unit */
9792 } else {
9793 str = stabstr_section->data + sym->n_strx;
9794 /* do not add path */
9795 len = strlen(str);
9796 if (len > 0 && str[len - 1] != '/')
9797 goto add_incl;
9799 break;
9801 sym++;
9804 /* second pass: we try symtab symbols (no line number info) */
9805 incl_index = 0;
9807 Elf32_Sym *sym, *sym_end;
9808 int type;
9810 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
9811 for(sym = (Elf32_Sym *)symtab_section->data + 1;
9812 sym < sym_end;
9813 sym++) {
9814 type = ELF32_ST_TYPE(sym->st_info);
9815 if (type == STT_FUNC) {
9816 if (wanted_pc >= sym->st_value &&
9817 wanted_pc < sym->st_value + sym->st_size) {
9818 pstrcpy(last_func_name, sizeof(last_func_name),
9819 strtab_section->data + sym->st_name);
9820 goto found;
9825 /* did not find any info: */
9826 fprintf(stderr, " ???\n");
9827 return;
9828 found:
9829 if (last_func_name[0] != '\0') {
9830 fprintf(stderr, " %s()", last_func_name);
9832 if (incl_index > 0) {
9833 fprintf(stderr, " (%s:%d",
9834 incl_files[incl_index - 1], last_line_num);
9835 for(i = incl_index - 2; i >= 0; i--)
9836 fprintf(stderr, ", included from %s", incl_files[i]);
9837 fprintf(stderr, ")");
9839 fprintf(stderr, "\n");
9842 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
9844 #ifdef __i386__
9846 /* fix for glibc 2.1 */
9847 #ifndef REG_EIP
9848 #define REG_EIP EIP
9849 #define REG_EBP EBP
9850 #endif
9852 /* return the PC at frame level 'level'. Return non zero if not found */
9853 static int rt_get_caller_pc(unsigned long *paddr,
9854 ucontext_t *uc, int level)
9856 unsigned long fp;
9857 int i;
9859 if (level == 0) {
9860 #if defined(__FreeBSD__)
9861 *paddr = uc->uc_mcontext.mc_eip;
9862 #elif defined(__dietlibc__)
9863 *paddr = uc->uc_mcontext.eip;
9864 #else
9865 *paddr = uc->uc_mcontext.gregs[REG_EIP];
9866 #endif
9867 return 0;
9868 } else {
9869 #if defined(__FreeBSD__)
9870 fp = uc->uc_mcontext.mc_ebp;
9871 #elif defined(__dietlibc__)
9872 fp = uc->uc_mcontext.ebp;
9873 #else
9874 fp = uc->uc_mcontext.gregs[REG_EBP];
9875 #endif
9876 for(i=1;i<level;i++) {
9877 /* XXX: check address validity with program info */
9878 if (fp <= 0x1000 || fp >= 0xc0000000)
9879 return -1;
9880 fp = ((unsigned long *)fp)[0];
9882 *paddr = ((unsigned long *)fp)[1];
9883 return 0;
9886 #else
9888 #warning add arch specific rt_get_caller_pc()
9890 static int rt_get_caller_pc(unsigned long *paddr,
9891 ucontext_t *uc, int level)
9893 return -1;
9895 #endif
9897 /* emit a run time error at position 'pc' */
9898 void rt_error(ucontext_t *uc, const char *fmt, ...)
9900 va_list ap;
9901 unsigned long pc;
9902 int i;
9904 va_start(ap, fmt);
9905 fprintf(stderr, "Runtime error: ");
9906 vfprintf(stderr, fmt, ap);
9907 fprintf(stderr, "\n");
9908 for(i=0;i<num_callers;i++) {
9909 if (rt_get_caller_pc(&pc, uc, i) < 0)
9910 break;
9911 if (i == 0)
9912 fprintf(stderr, "at ");
9913 else
9914 fprintf(stderr, "by ");
9915 rt_printline(pc);
9917 exit(255);
9918 va_end(ap);
9921 /* signal handler for fatal errors */
9922 static void sig_error(int signum, siginfo_t *siginf, void *puc)
9924 ucontext_t *uc = puc;
9926 switch(signum) {
9927 case SIGFPE:
9928 switch(siginf->si_code) {
9929 case FPE_INTDIV:
9930 case FPE_FLTDIV:
9931 rt_error(uc, "division by zero");
9932 break;
9933 default:
9934 rt_error(uc, "floating point exception");
9935 break;
9937 break;
9938 case SIGBUS:
9939 case SIGSEGV:
9940 if (rt_bound_error_msg && *rt_bound_error_msg)
9941 rt_error(uc, *rt_bound_error_msg);
9942 else
9943 rt_error(uc, "dereferencing invalid pointer");
9944 break;
9945 case SIGILL:
9946 rt_error(uc, "illegal instruction");
9947 break;
9948 case SIGABRT:
9949 rt_error(uc, "abort() called");
9950 break;
9951 default:
9952 rt_error(uc, "caught signal %d", signum);
9953 break;
9955 exit(255);
9957 #endif
9959 /* do all relocations (needed before using tcc_get_symbol()) */
9960 int tcc_relocate(TCCState *s1)
9962 Section *s;
9963 int i;
9965 s1->nb_errors = 0;
9967 #ifdef TCC_TARGET_PE
9968 pe_add_runtime(s1);
9969 #else
9970 tcc_add_runtime(s1);
9971 #endif
9973 relocate_common_syms();
9975 tcc_add_linker_symbols(s1);
9976 #ifndef TCC_TARGET_PE
9977 build_got_entries(s1);
9978 #endif
9979 /* compute relocation address : section are relocated in place. We
9980 also alloc the bss space */
9981 for(i = 1; i < s1->nb_sections; i++) {
9982 s = s1->sections[i];
9983 if (s->sh_flags & SHF_ALLOC) {
9984 if (s->sh_type == SHT_NOBITS)
9985 s->data = tcc_mallocz(s->data_offset);
9986 s->sh_addr = (unsigned long)s->data;
9990 relocate_syms(s1, 1);
9992 if (s1->nb_errors != 0)
9993 return -1;
9995 /* relocate each section */
9996 for(i = 1; i < s1->nb_sections; i++) {
9997 s = s1->sections[i];
9998 if (s->reloc)
9999 relocate_section(s1, s);
10002 /* mark executable sections as executable in memory */
10003 for(i = 1; i < s1->nb_sections; i++) {
10004 s = s1->sections[i];
10005 if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
10006 (SHF_ALLOC | SHF_EXECINSTR))
10007 set_pages_executable(s->data, s->data_offset);
10009 return 0;
10012 /* launch the compiled program with the given arguments */
10013 int tcc_run(TCCState *s1, int argc, char **argv)
10015 int (*prog_main)(int, char **);
10017 if (tcc_relocate(s1) < 0)
10018 return -1;
10020 prog_main = tcc_get_symbol_err(s1, "main");
10022 if (do_debug) {
10023 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10024 error("debug mode currently not available for Windows");
10025 #else
10026 struct sigaction sigact;
10027 /* install TCC signal handlers to print debug info on fatal
10028 runtime errors */
10029 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
10030 sigact.sa_sigaction = sig_error;
10031 sigemptyset(&sigact.sa_mask);
10032 sigaction(SIGFPE, &sigact, NULL);
10033 sigaction(SIGILL, &sigact, NULL);
10034 sigaction(SIGSEGV, &sigact, NULL);
10035 sigaction(SIGBUS, &sigact, NULL);
10036 sigaction(SIGABRT, &sigact, NULL);
10037 #endif
10040 #ifdef CONFIG_TCC_BCHECK
10041 if (do_bounds_check) {
10042 void (*bound_init)(void);
10044 /* set error function */
10045 rt_bound_error_msg = (void *)tcc_get_symbol_err(s1,
10046 "__bound_error_msg");
10048 /* XXX: use .init section so that it also work in binary ? */
10049 bound_init = (void *)tcc_get_symbol_err(s1, "__bound_init");
10050 bound_init();
10052 #endif
10053 return (*prog_main)(argc, argv);
10056 TCCState *tcc_new(void)
10058 const char *p, *r;
10059 TCCState *s;
10060 TokenSym *ts;
10061 int i, c;
10063 s = tcc_mallocz(sizeof(TCCState));
10064 if (!s)
10065 return NULL;
10066 tcc_state = s;
10067 s->output_type = TCC_OUTPUT_MEMORY;
10069 /* init isid table */
10070 for(i=0;i<256;i++)
10071 isidnum_table[i] = isid(i) || isnum(i);
10073 /* add all tokens */
10074 table_ident = NULL;
10075 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
10077 tok_ident = TOK_IDENT;
10078 p = tcc_keywords;
10079 while (*p) {
10080 r = p;
10081 for(;;) {
10082 c = *r++;
10083 if (c == '\0')
10084 break;
10086 ts = tok_alloc(p, r - p - 1);
10087 p = r;
10090 /* we add dummy defines for some special macros to speed up tests
10091 and to have working defined() */
10092 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
10093 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
10094 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
10095 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
10097 /* standard defines */
10098 tcc_define_symbol(s, "__STDC__", NULL);
10099 tcc_define_symbol(s, "__STDC_VERSION__", "199901L");
10100 #if defined(TCC_TARGET_I386)
10101 tcc_define_symbol(s, "__i386__", NULL);
10102 #endif
10103 #if defined(TCC_TARGET_ARM)
10104 tcc_define_symbol(s, "__ARM_ARCH_4__", NULL);
10105 tcc_define_symbol(s, "__arm_elf__", NULL);
10106 tcc_define_symbol(s, "__arm_elf", NULL);
10107 tcc_define_symbol(s, "arm_elf", NULL);
10108 tcc_define_symbol(s, "__arm__", NULL);
10109 tcc_define_symbol(s, "__arm", NULL);
10110 tcc_define_symbol(s, "arm", NULL);
10111 tcc_define_symbol(s, "__APCS_32__", NULL);
10112 #endif
10113 #ifdef TCC_TARGET_PE
10114 tcc_define_symbol(s, "_WIN32", NULL);
10115 #else
10116 tcc_define_symbol(s, "__unix__", NULL);
10117 tcc_define_symbol(s, "__unix", NULL);
10118 #if defined(__linux)
10119 tcc_define_symbol(s, "__linux__", NULL);
10120 tcc_define_symbol(s, "__linux", NULL);
10121 #endif
10122 #endif
10123 /* tiny C specific defines */
10124 tcc_define_symbol(s, "__TINYC__", NULL);
10126 /* tiny C & gcc defines */
10127 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
10128 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
10129 #ifdef TCC_TARGET_PE
10130 tcc_define_symbol(s, "__WCHAR_TYPE__", "unsigned short");
10131 #else
10132 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
10133 #endif
10135 #ifndef TCC_TARGET_PE
10136 /* default library paths */
10137 tcc_add_library_path(s, "/usr/local/lib");
10138 tcc_add_library_path(s, "/usr/lib");
10139 tcc_add_library_path(s, "/lib");
10140 #endif
10142 /* no section zero */
10143 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
10145 /* create standard sections */
10146 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
10147 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
10148 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
10150 /* symbols are always generated for linking stage */
10151 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
10152 ".strtab",
10153 ".hashtab", SHF_PRIVATE);
10154 strtab_section = symtab_section->link;
10156 /* private symbol table for dynamic symbols */
10157 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
10158 ".dynstrtab",
10159 ".dynhashtab", SHF_PRIVATE);
10160 s->alacarte_link = 1;
10162 #ifdef CHAR_IS_UNSIGNED
10163 s->char_is_unsigned = 1;
10164 #endif
10165 #if defined(TCC_TARGET_PE) && 0
10166 /* XXX: currently the PE linker is not ready to support that */
10167 s->leading_underscore = 1;
10168 #endif
10169 return s;
10172 void tcc_delete(TCCState *s1)
10174 int i, n;
10176 /* free -D defines */
10177 free_defines(NULL);
10179 /* free tokens */
10180 n = tok_ident - TOK_IDENT;
10181 for(i = 0; i < n; i++)
10182 tcc_free(table_ident[i]);
10183 tcc_free(table_ident);
10185 /* free all sections */
10187 free_section(symtab_section->hash);
10189 free_section(s1->dynsymtab_section->hash);
10190 free_section(s1->dynsymtab_section->link);
10191 free_section(s1->dynsymtab_section);
10193 for(i = 1; i < s1->nb_sections; i++)
10194 free_section(s1->sections[i]);
10195 tcc_free(s1->sections);
10197 /* free loaded dlls array */
10198 dynarray_reset(&s1->loaded_dlls, &s1->nb_loaded_dlls);
10200 /* free library paths */
10201 dynarray_reset(&s1->library_paths, &s1->nb_library_paths);
10203 /* free include paths */
10204 dynarray_reset(&s1->cached_includes, &s1->nb_cached_includes);
10205 dynarray_reset(&s1->include_paths, &s1->nb_include_paths);
10206 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10208 tcc_free(s1);
10211 int tcc_add_include_path(TCCState *s1, const char *pathname)
10213 char *pathname1;
10215 pathname1 = tcc_strdup(pathname);
10216 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
10217 return 0;
10220 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
10222 char *pathname1;
10224 pathname1 = tcc_strdup(pathname);
10225 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
10226 return 0;
10229 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
10231 const char *ext;
10232 Elf32_Ehdr ehdr;
10233 int fd, ret;
10234 BufferedFile *saved_file;
10236 /* find source file type with extension */
10237 ext = tcc_fileextension(filename);
10238 if (ext[0])
10239 ext++;
10241 /* open the file */
10242 saved_file = file;
10243 file = tcc_open(s1, filename);
10244 if (!file) {
10245 if (flags & AFF_PRINT_ERROR) {
10246 error_noabort("file '%s' not found", filename);
10248 ret = -1;
10249 goto fail1;
10252 if (flags & AFF_PREPROCESS) {
10253 ret = tcc_preprocess(s1);
10254 } else if (!ext[0] || !strcmp(ext, "c")) {
10255 /* C file assumed */
10256 ret = tcc_compile(s1);
10257 } else
10258 #ifdef CONFIG_TCC_ASM
10259 if (!strcmp(ext, "S")) {
10260 /* preprocessed assembler */
10261 ret = tcc_assemble(s1, 1);
10262 } else if (!strcmp(ext, "s")) {
10263 /* non preprocessed assembler */
10264 ret = tcc_assemble(s1, 0);
10265 } else
10266 #endif
10267 #ifdef TCC_TARGET_PE
10268 if (!strcmp(ext, "def")) {
10269 ret = pe_load_def_file(s1, file->fd);
10270 } else
10271 #endif
10273 fd = file->fd;
10274 /* assume executable format: auto guess file type */
10275 ret = read(fd, &ehdr, sizeof(ehdr));
10276 lseek(fd, 0, SEEK_SET);
10277 if (ret <= 0) {
10278 error_noabort("could not read header");
10279 goto fail;
10280 } else if (ret != sizeof(ehdr)) {
10281 goto try_load_script;
10284 if (ehdr.e_ident[0] == ELFMAG0 &&
10285 ehdr.e_ident[1] == ELFMAG1 &&
10286 ehdr.e_ident[2] == ELFMAG2 &&
10287 ehdr.e_ident[3] == ELFMAG3) {
10288 file->line_num = 0; /* do not display line number if error */
10289 if (ehdr.e_type == ET_REL) {
10290 ret = tcc_load_object_file(s1, fd, 0);
10291 } else if (ehdr.e_type == ET_DYN) {
10292 if (s1->output_type == TCC_OUTPUT_MEMORY) {
10293 #ifdef TCC_TARGET_PE
10294 ret = -1;
10295 #else
10296 void *h;
10297 h = dlopen(filename, RTLD_GLOBAL | RTLD_LAZY);
10298 if (h)
10299 ret = 0;
10300 else
10301 ret = -1;
10302 #endif
10303 } else {
10304 ret = tcc_load_dll(s1, fd, filename,
10305 (flags & AFF_REFERENCED_DLL) != 0);
10307 } else {
10308 error_noabort("unrecognized ELF file");
10309 goto fail;
10311 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
10312 file->line_num = 0; /* do not display line number if error */
10313 ret = tcc_load_archive(s1, fd);
10314 } else
10315 #ifdef TCC_TARGET_COFF
10316 if (*(uint16_t *)(&ehdr) == COFF_C67_MAGIC) {
10317 ret = tcc_load_coff(s1, fd);
10318 } else
10319 #endif
10320 #ifdef TCC_TARGET_PE
10321 if (pe_test_res_file(&ehdr, ret)) {
10322 ret = pe_load_res_file(s1, fd);
10323 } else
10324 #endif
10326 /* as GNU ld, consider it is an ld script if not recognized */
10327 try_load_script:
10328 ret = tcc_load_ldscript(s1);
10329 if (ret < 0) {
10330 error_noabort("unrecognized file type");
10331 goto fail;
10335 the_end:
10336 tcc_close(file);
10337 fail1:
10338 file = saved_file;
10339 return ret;
10340 fail:
10341 ret = -1;
10342 goto the_end;
10345 int tcc_add_file(TCCState *s, const char *filename)
10347 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
10350 int tcc_add_library_path(TCCState *s, const char *pathname)
10352 char *pathname1;
10354 pathname1 = tcc_strdup(pathname);
10355 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
10356 return 0;
10359 /* find and load a dll. Return non zero if not found */
10360 /* XXX: add '-rpath' option support ? */
10361 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
10363 char buf[1024];
10364 int i;
10366 for(i = 0; i < s->nb_library_paths; i++) {
10367 snprintf(buf, sizeof(buf), "%s/%s",
10368 s->library_paths[i], filename);
10369 if (tcc_add_file_internal(s, buf, flags) == 0)
10370 return 0;
10372 return -1;
10375 /* the library name is the same as the argument of the '-l' option */
10376 int tcc_add_library(TCCState *s, const char *libraryname)
10378 char buf[1024];
10379 int i;
10381 /* first we look for the dynamic library if not static linking */
10382 if (!s->static_link) {
10383 #ifdef TCC_TARGET_PE
10384 snprintf(buf, sizeof(buf), "%s.def", libraryname);
10385 #else
10386 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
10387 #endif
10388 if (tcc_add_dll(s, buf, 0) == 0)
10389 return 0;
10392 /* then we look for the static library */
10393 for(i = 0; i < s->nb_library_paths; i++) {
10394 snprintf(buf, sizeof(buf), "%s/lib%s.a",
10395 s->library_paths[i], libraryname);
10396 if (tcc_add_file_internal(s, buf, 0) == 0)
10397 return 0;
10399 return -1;
10402 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
10404 add_elf_sym(symtab_section, val, 0,
10405 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0,
10406 SHN_ABS, name);
10407 return 0;
10410 int tcc_set_output_type(TCCState *s, int output_type)
10412 char buf[1024];
10414 s->output_type = output_type;
10416 if (!s->nostdinc) {
10417 /* default include paths */
10418 /* XXX: reverse order needed if -isystem support */
10419 #ifndef TCC_TARGET_PE
10420 tcc_add_sysinclude_path(s, "/usr/local/include");
10421 tcc_add_sysinclude_path(s, "/usr/include");
10422 #endif
10423 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
10424 tcc_add_sysinclude_path(s, buf);
10425 #ifdef TCC_TARGET_PE
10426 snprintf(buf, sizeof(buf), "%s/include/winapi", tcc_lib_path);
10427 tcc_add_sysinclude_path(s, buf);
10428 #endif
10431 /* if bound checking, then add corresponding sections */
10432 #ifdef CONFIG_TCC_BCHECK
10433 if (do_bounds_check) {
10434 /* define symbol */
10435 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
10436 /* create bounds sections */
10437 bounds_section = new_section(s, ".bounds",
10438 SHT_PROGBITS, SHF_ALLOC);
10439 lbounds_section = new_section(s, ".lbounds",
10440 SHT_PROGBITS, SHF_ALLOC);
10442 #endif
10444 if (s->char_is_unsigned) {
10445 tcc_define_symbol(s, "__CHAR_UNSIGNED__", NULL);
10448 /* add debug sections */
10449 if (do_debug) {
10450 /* stab symbols */
10451 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
10452 stab_section->sh_entsize = sizeof(Stab_Sym);
10453 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
10454 put_elf_str(stabstr_section, "");
10455 stab_section->link = stabstr_section;
10456 /* put first entry */
10457 put_stabs("", 0, 0, 0, 0);
10460 /* add libc crt1/crti objects */
10461 #ifndef TCC_TARGET_PE
10462 if ((output_type == TCC_OUTPUT_EXE || output_type == TCC_OUTPUT_DLL) &&
10463 !s->nostdlib) {
10464 if (output_type != TCC_OUTPUT_DLL)
10465 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
10466 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
10468 #endif
10470 #ifdef TCC_TARGET_PE
10471 snprintf(buf, sizeof(buf), "%s/lib", tcc_lib_path);
10472 tcc_add_library_path(s, buf);
10473 #endif
10475 return 0;
10478 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10479 #define FD_INVERT 0x0002 /* invert value before storing */
10481 typedef struct FlagDef {
10482 uint16_t offset;
10483 uint16_t flags;
10484 const char *name;
10485 } FlagDef;
10487 static const FlagDef warning_defs[] = {
10488 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
10489 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
10490 { offsetof(TCCState, warn_error), 0, "error" },
10491 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
10492 "implicit-function-declaration" },
10495 static int set_flag(TCCState *s, const FlagDef *flags, int nb_flags,
10496 const char *name, int value)
10498 int i;
10499 const FlagDef *p;
10500 const char *r;
10502 r = name;
10503 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
10504 r += 3;
10505 value = !value;
10507 for(i = 0, p = flags; i < nb_flags; i++, p++) {
10508 if (!strcmp(r, p->name))
10509 goto found;
10511 return -1;
10512 found:
10513 if (p->flags & FD_INVERT)
10514 value = !value;
10515 *(int *)((uint8_t *)s + p->offset) = value;
10516 return 0;
10520 /* set/reset a warning */
10521 int tcc_set_warning(TCCState *s, const char *warning_name, int value)
10523 int i;
10524 const FlagDef *p;
10526 if (!strcmp(warning_name, "all")) {
10527 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
10528 if (p->flags & WD_ALL)
10529 *(int *)((uint8_t *)s + p->offset) = 1;
10531 return 0;
10532 } else {
10533 return set_flag(s, warning_defs, countof(warning_defs),
10534 warning_name, value);
10538 static const FlagDef flag_defs[] = {
10539 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
10540 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
10541 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
10542 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
10545 /* set/reset a flag */
10546 int tcc_set_flag(TCCState *s, const char *flag_name, int value)
10548 return set_flag(s, flag_defs, countof(flag_defs),
10549 flag_name, value);
10552 #if !defined(LIBTCC)
10554 static int64_t getclock_us(void)
10556 #ifdef _WIN32
10557 struct _timeb tb;
10558 _ftime(&tb);
10559 return (tb.time * 1000LL + tb.millitm) * 1000LL;
10560 #else
10561 struct timeval tv;
10562 gettimeofday(&tv, NULL);
10563 return tv.tv_sec * 1000000LL + tv.tv_usec;
10564 #endif
10567 void help(void)
10569 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10570 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10571 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10572 " [-static] [infile1 infile2...] [-run infile args...]\n"
10573 "\n"
10574 "General options:\n"
10575 " -v display current version, increase verbosity\n"
10576 " -c compile only - generate an object file\n"
10577 " -o outfile set output filename\n"
10578 " -Bdir set tcc internal library path\n"
10579 " -bench output compilation statistics\n"
10580 " -run run compiled source\n"
10581 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10582 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10583 " -w disable all warnings\n"
10584 "Preprocessor options:\n"
10585 " -E preprocess only\n"
10586 " -Idir add include path 'dir'\n"
10587 " -Dsym[=val] define 'sym' with value 'val'\n"
10588 " -Usym undefine 'sym'\n"
10589 "Linker options:\n"
10590 " -Ldir add library path 'dir'\n"
10591 " -llib link with dynamic or static library 'lib'\n"
10592 " -shared generate a shared library\n"
10593 " -soname set name for shared library to be used at runtime\n"
10594 " -static static linking\n"
10595 " -rdynamic export all global symbols to dynamic linker\n"
10596 " -r generate (relocatable) object file\n"
10597 "Debugger options:\n"
10598 " -g generate runtime debug info\n"
10599 #ifdef CONFIG_TCC_BCHECK
10600 " -b compile with built-in memory and bounds checker (implies -g)\n"
10601 #endif
10602 " -bt N show N callers in stack traces\n"
10606 #define TCC_OPTION_HAS_ARG 0x0001
10607 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10609 typedef struct TCCOption {
10610 const char *name;
10611 uint16_t index;
10612 uint16_t flags;
10613 } TCCOption;
10615 enum {
10616 TCC_OPTION_HELP,
10617 TCC_OPTION_I,
10618 TCC_OPTION_D,
10619 TCC_OPTION_U,
10620 TCC_OPTION_L,
10621 TCC_OPTION_B,
10622 TCC_OPTION_l,
10623 TCC_OPTION_bench,
10624 TCC_OPTION_bt,
10625 TCC_OPTION_b,
10626 TCC_OPTION_g,
10627 TCC_OPTION_c,
10628 TCC_OPTION_static,
10629 TCC_OPTION_shared,
10630 TCC_OPTION_soname,
10631 TCC_OPTION_o,
10632 TCC_OPTION_r,
10633 TCC_OPTION_Wl,
10634 TCC_OPTION_W,
10635 TCC_OPTION_O,
10636 TCC_OPTION_m,
10637 TCC_OPTION_f,
10638 TCC_OPTION_nostdinc,
10639 TCC_OPTION_nostdlib,
10640 TCC_OPTION_print_search_dirs,
10641 TCC_OPTION_rdynamic,
10642 TCC_OPTION_run,
10643 TCC_OPTION_v,
10644 TCC_OPTION_w,
10645 TCC_OPTION_pipe,
10646 TCC_OPTION_E,
10649 static const TCCOption tcc_options[] = {
10650 { "h", TCC_OPTION_HELP, 0 },
10651 { "?", TCC_OPTION_HELP, 0 },
10652 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
10653 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
10654 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
10655 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
10656 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
10657 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10658 { "bench", TCC_OPTION_bench, 0 },
10659 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
10660 #ifdef CONFIG_TCC_BCHECK
10661 { "b", TCC_OPTION_b, 0 },
10662 #endif
10663 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10664 { "c", TCC_OPTION_c, 0 },
10665 { "static", TCC_OPTION_static, 0 },
10666 { "shared", TCC_OPTION_shared, 0 },
10667 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
10668 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
10669 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10670 { "rdynamic", TCC_OPTION_rdynamic, 0 },
10671 { "r", TCC_OPTION_r, 0 },
10672 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10673 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10674 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10675 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
10676 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10677 { "nostdinc", TCC_OPTION_nostdinc, 0 },
10678 { "nostdlib", TCC_OPTION_nostdlib, 0 },
10679 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
10680 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
10681 { "w", TCC_OPTION_w, 0 },
10682 { "pipe", TCC_OPTION_pipe, 0},
10683 { "E", TCC_OPTION_E, 0},
10684 { NULL },
10687 /* convert 'str' into an array of space separated strings */
10688 static int expand_args(char ***pargv, const char *str)
10690 const char *s1;
10691 char **argv, *arg;
10692 int argc, len;
10694 argc = 0;
10695 argv = NULL;
10696 for(;;) {
10697 while (is_space(*str))
10698 str++;
10699 if (*str == '\0')
10700 break;
10701 s1 = str;
10702 while (*str != '\0' && !is_space(*str))
10703 str++;
10704 len = str - s1;
10705 arg = tcc_malloc(len + 1);
10706 memcpy(arg, s1, len);
10707 arg[len] = '\0';
10708 dynarray_add((void ***)&argv, &argc, arg);
10710 *pargv = argv;
10711 return argc;
10714 static char **files;
10715 static int nb_files, nb_libraries;
10716 static int multiple_files;
10717 static int print_search_dirs;
10718 static int output_type;
10719 static int reloc_output;
10720 static const char *outfile;
10722 int parse_args(TCCState *s, int argc, char **argv)
10724 int optind;
10725 const TCCOption *popt;
10726 const char *optarg, *p1, *r1;
10727 char *r;
10729 optind = 0;
10730 while (1) {
10731 if (optind >= argc) {
10732 if (nb_files == 0 && !print_search_dirs) {
10733 if (verbose)
10734 exit(0);
10735 goto show_help;
10737 break;
10739 r = argv[optind++];
10740 if (r[0] != '-' || r[1] == '\0') {
10741 /* add a new file */
10742 dynarray_add((void ***)&files, &nb_files, r);
10743 if (!multiple_files) {
10744 optind--;
10745 /* argv[0] will be this file */
10746 break;
10748 } else {
10749 /* find option in table (match only the first chars */
10750 popt = tcc_options;
10751 for(;;) {
10752 p1 = popt->name;
10753 if (p1 == NULL)
10754 error("invalid option -- '%s'", r);
10755 r1 = r + 1;
10756 for(;;) {
10757 if (*p1 == '\0')
10758 goto option_found;
10759 if (*r1 != *p1)
10760 break;
10761 p1++;
10762 r1++;
10764 popt++;
10766 option_found:
10767 if (popt->flags & TCC_OPTION_HAS_ARG) {
10768 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
10769 optarg = r1;
10770 } else {
10771 if (optind >= argc)
10772 error("argument to '%s' is missing", r);
10773 optarg = argv[optind++];
10775 } else {
10776 if (*r1 != '\0')
10777 goto show_help;
10778 optarg = NULL;
10781 switch(popt->index) {
10782 case TCC_OPTION_HELP:
10783 show_help:
10784 help();
10785 exit(1);
10786 case TCC_OPTION_I:
10787 if (tcc_add_include_path(s, optarg) < 0)
10788 error("too many include paths");
10789 break;
10790 case TCC_OPTION_D:
10792 char *sym, *value;
10793 sym = (char *)optarg;
10794 value = strchr(sym, '=');
10795 if (value) {
10796 *value = '\0';
10797 value++;
10799 tcc_define_symbol(s, sym, value);
10801 break;
10802 case TCC_OPTION_U:
10803 tcc_undefine_symbol(s, optarg);
10804 break;
10805 case TCC_OPTION_L:
10806 tcc_add_library_path(s, optarg);
10807 break;
10808 case TCC_OPTION_B:
10809 /* set tcc utilities path (mainly for tcc development) */
10810 tcc_lib_path = optarg;
10811 break;
10812 case TCC_OPTION_l:
10813 dynarray_add((void ***)&files, &nb_files, r);
10814 nb_libraries++;
10815 break;
10816 case TCC_OPTION_bench:
10817 do_bench = 1;
10818 break;
10819 case TCC_OPTION_bt:
10820 num_callers = atoi(optarg);
10821 break;
10822 #ifdef CONFIG_TCC_BCHECK
10823 case TCC_OPTION_b:
10824 do_bounds_check = 1;
10825 do_debug = 1;
10826 break;
10827 #endif
10828 case TCC_OPTION_g:
10829 do_debug = 1;
10830 break;
10831 case TCC_OPTION_c:
10832 multiple_files = 1;
10833 output_type = TCC_OUTPUT_OBJ;
10834 break;
10835 case TCC_OPTION_static:
10836 s->static_link = 1;
10837 break;
10838 case TCC_OPTION_shared:
10839 output_type = TCC_OUTPUT_DLL;
10840 break;
10841 case TCC_OPTION_soname:
10842 s->soname = optarg;
10843 break;
10844 case TCC_OPTION_o:
10845 multiple_files = 1;
10846 outfile = optarg;
10847 break;
10848 case TCC_OPTION_r:
10849 /* generate a .o merging several output files */
10850 reloc_output = 1;
10851 output_type = TCC_OUTPUT_OBJ;
10852 break;
10853 case TCC_OPTION_nostdinc:
10854 s->nostdinc = 1;
10855 break;
10856 case TCC_OPTION_nostdlib:
10857 s->nostdlib = 1;
10858 break;
10859 case TCC_OPTION_print_search_dirs:
10860 print_search_dirs = 1;
10861 break;
10862 case TCC_OPTION_run:
10864 int argc1;
10865 char **argv1;
10866 argc1 = expand_args(&argv1, optarg);
10867 if (argc1 > 0) {
10868 parse_args(s, argc1, argv1);
10870 multiple_files = 0;
10871 output_type = TCC_OUTPUT_MEMORY;
10873 break;
10874 case TCC_OPTION_v:
10875 do {
10876 if (0 == verbose++)
10877 printf("tcc version %s\n", TCC_VERSION);
10878 } while (*optarg++ == 'v');
10879 break;
10880 case TCC_OPTION_f:
10881 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
10882 goto unsupported_option;
10883 break;
10884 case TCC_OPTION_W:
10885 if (tcc_set_warning(s, optarg, 1) < 0 &&
10886 s->warn_unsupported)
10887 goto unsupported_option;
10888 break;
10889 case TCC_OPTION_w:
10890 s->warn_none = 1;
10891 break;
10892 case TCC_OPTION_rdynamic:
10893 s->rdynamic = 1;
10894 break;
10895 case TCC_OPTION_Wl:
10897 const char *p;
10898 if (strstart(optarg, "-Ttext,", &p)) {
10899 s->text_addr = strtoul(p, NULL, 16);
10900 s->has_text_addr = 1;
10901 } else if (strstart(optarg, "--oformat,", &p)) {
10902 if (strstart(p, "elf32-", NULL)) {
10903 s->output_format = TCC_OUTPUT_FORMAT_ELF;
10904 } else if (!strcmp(p, "binary")) {
10905 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
10906 } else
10907 #ifdef TCC_TARGET_COFF
10908 if (!strcmp(p, "coff")) {
10909 s->output_format = TCC_OUTPUT_FORMAT_COFF;
10910 } else
10911 #endif
10913 error("target %s not found", p);
10915 } else {
10916 error("unsupported linker option '%s'", optarg);
10919 break;
10920 case TCC_OPTION_E:
10921 output_type = TCC_OUTPUT_PREPROCESS;
10922 break;
10923 default:
10924 if (s->warn_unsupported) {
10925 unsupported_option:
10926 warning("unsupported option '%s'", r);
10928 break;
10932 return optind;
10935 int main(int argc, char **argv)
10937 int i;
10938 TCCState *s;
10939 int nb_objfiles, ret, optind;
10940 char objfilename[1024];
10941 int64_t start_time = 0;
10943 #ifdef _WIN32
10944 tcc_lib_path = w32_tcc_lib_path();
10945 #endif
10947 s = tcc_new();
10948 output_type = TCC_OUTPUT_EXE;
10949 outfile = NULL;
10950 multiple_files = 1;
10951 files = NULL;
10952 nb_files = 0;
10953 nb_libraries = 0;
10954 reloc_output = 0;
10955 print_search_dirs = 0;
10956 ret = 0;
10958 optind = parse_args(s, argc - 1, argv + 1) + 1;
10960 if (print_search_dirs) {
10961 /* enough for Linux kernel */
10962 printf("install: %s/\n", tcc_lib_path);
10963 return 0;
10966 nb_objfiles = nb_files - nb_libraries;
10968 /* if outfile provided without other options, we output an
10969 executable */
10970 if (outfile && output_type == TCC_OUTPUT_MEMORY)
10971 output_type = TCC_OUTPUT_EXE;
10973 /* check -c consistency : only single file handled. XXX: checks file type */
10974 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
10975 /* accepts only a single input file */
10976 if (nb_objfiles != 1)
10977 error("cannot specify multiple files with -c");
10978 if (nb_libraries != 0)
10979 error("cannot specify libraries with -c");
10983 if (output_type == TCC_OUTPUT_PREPROCESS) {
10984 if (!outfile) {
10985 s->outfile = stdout;
10986 } else {
10987 s->outfile = fopen(outfile, "w");
10988 if (!s->outfile)
10989 error("could not open '%s", outfile);
10991 } else if (output_type != TCC_OUTPUT_MEMORY) {
10992 if (!outfile) {
10993 /* compute default outfile name */
10994 char *ext;
10995 const char *name =
10996 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
10997 pstrcpy(objfilename, sizeof(objfilename), name);
10998 ext = tcc_fileextension(objfilename);
10999 #ifdef TCC_TARGET_PE
11000 if (output_type == TCC_OUTPUT_DLL)
11001 strcpy(ext, ".dll");
11002 else
11003 if (output_type == TCC_OUTPUT_EXE)
11004 strcpy(ext, ".exe");
11005 else
11006 #endif
11007 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
11008 strcpy(ext, ".o");
11009 else
11010 pstrcpy(objfilename, sizeof(objfilename), "a.out");
11011 outfile = objfilename;
11015 if (do_bench) {
11016 start_time = getclock_us();
11019 tcc_set_output_type(s, output_type);
11021 /* compile or add each files or library */
11022 for(i = 0; i < nb_files && ret == 0; i++) {
11023 const char *filename;
11025 filename = files[i];
11026 if (output_type == TCC_OUTPUT_PREPROCESS) {
11027 if (tcc_add_file_internal(s, filename,
11028 AFF_PRINT_ERROR | AFF_PREPROCESS) < 0)
11029 ret = 1;
11030 } else if (filename[0] == '-' && filename[1]) {
11031 if (tcc_add_library(s, filename + 2) < 0)
11032 error("cannot find %s", filename);
11033 } else {
11034 if (1 == verbose)
11035 printf("-> %s\n", filename);
11036 if (tcc_add_file(s, filename) < 0)
11037 ret = 1;
11041 /* free all files */
11042 tcc_free(files);
11044 if (ret)
11045 goto the_end;
11047 if (do_bench) {
11048 double total_time;
11049 total_time = (double)(getclock_us() - start_time) / 1000000.0;
11050 if (total_time < 0.001)
11051 total_time = 0.001;
11052 if (total_bytes < 1)
11053 total_bytes = 1;
11054 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11055 tok_ident - TOK_IDENT, total_lines, total_bytes,
11056 total_time, (int)(total_lines / total_time),
11057 total_bytes / total_time / 1000000.0);
11060 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
11061 if (outfile)
11062 fclose(s->outfile);
11063 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
11064 ret = tcc_run(s, argc - optind, argv + optind);
11065 } else
11066 #ifdef TCC_TARGET_PE
11067 if (s->output_type != TCC_OUTPUT_OBJ) {
11068 ret = pe_output_file(s, outfile);
11069 } else
11070 #endif
11072 ret = tcc_output_file(s, outfile) ? 1 : 0;
11074 the_end:
11075 /* XXX: cannot do it with bound checking because of the malloc hooks */
11076 if (!do_bounds_check)
11077 tcc_delete(s);
11079 #ifdef MEM_DEBUG
11080 if (do_bench) {
11081 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
11083 #endif
11084 return ret;
11087 #endif