2 * TCC - Tiny C Compiler
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
26 #define CONFIG_TCC_STATIC
43 #include <sys/timeb.h>
45 #define inline __inline
52 #include <sys/ucontext.h>
56 #endif /* !CONFIG_TCCBOOT */
73 /* preprocessor debug */
75 /* include file debug */
83 /* target selection */
84 //#define TCC_TARGET_I386 /* i386 code generator */
85 //#define TCC_TARGET_ARM /* ARMv4 code generator */
86 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
87 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
89 /* default target is I386 */
90 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
91 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
92 #define TCC_TARGET_I386
95 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
96 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
97 #define CONFIG_TCC_BCHECK /* enable bound checking code */
100 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
101 #define CONFIG_TCC_STATIC
104 /* define it to include assembler support */
105 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67) && \
106 !defined(TCC_TARGET_X86_64)
107 #define CONFIG_TCC_ASM
110 /* object format selection */
111 #if defined(TCC_TARGET_C67)
112 #define TCC_TARGET_COFF
121 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
122 executables or dlls */
123 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
125 #define INCLUDE_STACK_SIZE 32
126 #define IFDEF_STACK_SIZE 64
127 #define VSTACK_SIZE 256
128 #define STRING_MAX_SIZE 1024
129 #define PACK_STACK_SIZE 8
131 #define TOK_HASH_SIZE 8192 /* must be a power of two */
132 #define TOK_ALLOC_INCR 512 /* must be a power of two */
133 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
135 /* token symbol management */
136 typedef struct TokenSym
{
137 struct TokenSym
*hash_next
;
138 struct Sym
*sym_define
; /* direct pointer to define */
139 struct Sym
*sym_label
; /* direct pointer to label */
140 struct Sym
*sym_struct
; /* direct pointer to structure */
141 struct Sym
*sym_identifier
; /* direct pointer to identifier */
142 int tok
; /* token number */
148 typedef unsigned short nwchar_t
;
150 typedef int nwchar_t
;
153 typedef struct CString
{
154 int size
; /* size in bytes */
155 void *data
; /* either 'char *' or 'nwchar_t *' */
157 void *data_allocated
; /* if non NULL, data has been malloced */
160 /* type definition */
161 typedef struct CType
{
167 typedef union CValue
{
173 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
175 unsigned long long ull
;
176 struct CString
*cstr
;
182 typedef struct SValue
{
183 CType type
; /* type */
184 unsigned short r
; /* register + flags */
185 unsigned short r2
; /* second register, used for 'long long'
186 type. If not used, set to VT_CONST */
187 CValue c
; /* constant, if VT_CONST */
188 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
191 /* symbol management */
193 int v
; /* symbol token */
194 long r
; /* associated register */
195 long c
; /* associated number */
196 CType type
; /* associated type */
197 struct Sym
*next
; /* next related symbol */
198 struct Sym
*prev
; /* prev symbol in stack */
199 struct Sym
*prev_tok
; /* previous symbol for this token */
202 /* section definition */
203 /* XXX: use directly ELF structure for parameters ? */
204 /* special flag to indicate that the section should not be linked to
206 #define SHF_PRIVATE 0x80000000
208 typedef struct Section
{
209 unsigned long data_offset
; /* current data offset */
210 unsigned char *data
; /* section data */
211 unsigned long data_allocated
; /* used for realloc() handling */
212 int sh_name
; /* elf section name (only used during output) */
213 int sh_num
; /* elf section number */
214 int sh_type
; /* elf section type */
215 int sh_flags
; /* elf section flags */
216 int sh_info
; /* elf section info */
217 int sh_addralign
; /* elf section alignment */
218 int sh_entsize
; /* elf entry size */
219 unsigned long sh_size
; /* section size (only used during output) */
220 unsigned long sh_addr
; /* address at which the section is relocated */
221 unsigned long sh_offset
; /* file offset */
222 int nb_hashed_syms
; /* used to resize the hash table */
223 struct Section
*link
; /* link to another section */
224 struct Section
*reloc
; /* corresponding section for relocation, if any */
225 struct Section
*hash
; /* hash table for symbols */
226 struct Section
*next
;
227 char name
[1]; /* section name */
230 typedef struct DLLReference
{
236 /* GNUC attribute definition */
237 typedef struct AttributeDef
{
241 int func_attr
; /* calling convention, exports, ... */
244 /* -------------------------------------------------- */
245 /* gr: wrappers for casting sym->r for other purposes */
253 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
254 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
255 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
256 #define INLINE_DEF(r) (*(int **)&(r))
257 /* -------------------------------------------------- */
259 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
260 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
261 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
263 /* stored in 'Sym.c' field */
264 #define FUNC_NEW 1 /* ansi function prototype */
265 #define FUNC_OLD 2 /* old function prototype */
266 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
268 /* stored in 'Sym.r' field */
269 #define FUNC_CDECL 0 /* standard c call */
270 #define FUNC_STDCALL 1 /* pascal c call */
271 #define FUNC_FASTCALL1 2 /* first param in %eax */
272 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
273 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
274 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
276 /* field 'Sym.t' for macros */
277 #define MACRO_OBJ 0 /* object like macro */
278 #define MACRO_FUNC 1 /* function like macro */
280 /* field 'Sym.r' for C labels */
281 #define LABEL_DEFINED 0 /* label is defined */
282 #define LABEL_FORWARD 1 /* label is forward defined */
283 #define LABEL_DECLARED 2 /* label is declared but never used */
285 /* type_decl() types */
286 #define TYPE_ABSTRACT 1 /* type without variable */
287 #define TYPE_DIRECT 2 /* type with variable */
289 #define IO_BUF_SIZE 8192
291 typedef struct BufferedFile
{
295 int line_num
; /* current line number - here to simplify code */
296 int ifndef_macro
; /* #ifndef macro / #endif search */
297 int ifndef_macro_saved
; /* saved ifndef_macro */
298 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
299 char inc_type
; /* type of include */
300 char inc_filename
[512]; /* filename specified by the user */
301 char filename
[1024]; /* current filename - here to simplify code */
302 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
305 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
306 #define CH_EOF (-1) /* end of file */
308 /* parsing state (used to save parser state to reparse part of the
309 source several times) */
310 typedef struct ParseState
{
317 /* used to record tokens */
318 typedef struct TokenString
{
325 /* include file cache, used to find files faster and also to eliminate
326 inclusion if the include file is protected by #ifndef ... #endif */
327 typedef struct CachedInclude
{
329 int hash_next
; /* -1 if none */
330 char type
; /* '"' or '>' to give include type */
331 char filename
[1]; /* path specified in #include */
334 #define CACHED_INCLUDES_HASH_SIZE 512
337 static struct BufferedFile
*file
;
339 static CString tok_spaces
; /* spaces before current token */
341 static CString tokcstr
; /* current parsed string, if any */
342 /* additional informations about token */
343 static int tok_flags
;
344 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
345 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
346 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
347 #define TOK_FLAG_EOF 0x0008 /* end of file */
349 static int *macro_ptr
, *macro_ptr_allocated
;
350 static int *unget_saved_macro_ptr
;
351 static int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
352 static int unget_buffer_enabled
;
353 static int parse_flags
;
354 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
355 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
356 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
357 token. line feed is also
359 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
361 static Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
362 static Section
*cur_text_section
; /* current section where function code is
364 #ifdef CONFIG_TCC_ASM
365 static Section
*last_text_section
; /* to handle .previous asm directive */
367 /* bound check related sections */
368 static Section
*bounds_section
; /* contains global data bound description */
369 static Section
*lbounds_section
; /* contains local data bound description */
370 /* symbol sections */
371 static Section
*symtab_section
, *strtab_section
;
374 static Section
*stab_section
, *stabstr_section
;
376 /* loc : local variable index
377 ind : output code index
379 anon_sym: anonymous symbol index
381 static int rsym
, anon_sym
, ind
, loc
;
382 /* expression generation modifiers */
383 static int const_wanted
; /* true if constant wanted */
384 static int nocode_wanted
; /* true if no code generation wanted for an expression */
385 static int global_expr
; /* true if compound literals must be allocated
386 globally (used during initializers parsing */
387 static CType func_vt
; /* current function return type (used by return
390 static int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
391 static int tok_ident
;
392 static TokenSym
**table_ident
;
393 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
394 static char token_buf
[STRING_MAX_SIZE
+ 1];
395 static char *funcname
;
396 static Sym
*global_stack
, *local_stack
;
397 static Sym
*define_stack
;
398 static Sym
*global_label_stack
, *local_label_stack
;
399 /* symbol allocator */
400 #define SYM_POOL_NB (8192 / sizeof(Sym))
401 static Sym
*sym_free_first
;
402 static void **sym_pools
;
403 static int nb_sym_pools
;
405 static SValue vstack
[VSTACK_SIZE
], *vtop
;
406 /* some predefined types */
407 static CType char_pointer_type
, func_old_type
, int_type
;
408 /* true if isid(c) || isnum(c) */
409 static unsigned char isidnum_table
[256-CH_EOF
];
411 /* display some information during compilation */
412 static int verbose
= 0;
414 /* compile with debug symbol (and use them if error during execution) */
415 static int do_debug
= 0;
417 /* compile with built-in memory and bounds checker */
418 static int do_bounds_check
= 0;
420 /* display benchmark infos */
422 static int do_bench
= 0;
424 static int total_lines
;
425 static int total_bytes
;
427 /* use GNU C extensions */
428 static int gnu_ext
= 1;
430 /* use Tiny C extensions */
431 static int tcc_ext
= 1;
433 /* max number of callers shown if error */
434 static int num_callers
= 6;
435 static const char **rt_bound_error_msg
;
437 /* XXX: get rid of this ASAP */
438 static struct TCCState
*tcc_state
;
440 /* give the path of the tcc libraries */
441 static const char *tcc_lib_path
= CONFIG_TCCDIR
;
446 BufferedFile
**include_stack_ptr
;
447 int *ifdef_stack_ptr
;
449 /* include file handling */
450 char **include_paths
;
451 int nb_include_paths
;
452 char **sysinclude_paths
;
453 int nb_sysinclude_paths
;
454 CachedInclude
**cached_includes
;
455 int nb_cached_includes
;
457 char **library_paths
;
458 int nb_library_paths
;
460 /* array of all loaded dlls (including those referenced by loaded
462 DLLReference
**loaded_dlls
;
467 int nb_sections
; /* number of sections, including first dummy section */
472 unsigned long *got_offsets
;
474 /* give the correspondance from symtab indexes to dynsym indexes */
475 int *symtab_to_dynsym
;
477 /* temporary dynamic symbol sections (for dll loading) */
478 Section
*dynsymtab_section
;
479 /* exported dynamic symbol section */
482 int nostdinc
; /* if true, no standard headers are added */
483 int nostdlib
; /* if true, no standard libraries are added */
485 int nocommon
; /* if true, do not use common symbols for .bss data */
487 /* if true, static linking is performed */
490 /* soname as specified on the command line (-soname) */
493 /* if true, all symbols are exported */
496 /* if true, only link in referenced objects from archive */
499 /* address of text section */
500 unsigned long text_addr
;
503 /* output format, see TCC_OUTPUT_FORMAT_xxx */
506 /* C language options */
507 int char_is_unsigned
;
508 int leading_underscore
;
510 /* warning switches */
511 int warn_write_strings
;
512 int warn_unsupported
;
515 int warn_implicit_function_declaration
;
519 void (*error_func
)(void *opaque
, const char *msg
);
520 int error_set_jmp_enabled
;
521 jmp_buf error_jmp_buf
;
524 /* tiny assembler state */
527 /* see include_stack_ptr */
528 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
530 /* see ifdef_stack_ptr */
531 int ifdef_stack
[IFDEF_STACK_SIZE
];
533 /* see cached_includes */
534 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
537 int pack_stack
[PACK_STACK_SIZE
];
540 /* output file for preprocessing */
543 #ifdef TCC_TARGET_X86_64
544 /* buffer to store jump tables */
550 /* The current value can be: */
551 #define VT_VALMASK 0x00ff
552 #define VT_CONST 0x00f0 /* constant in vc
553 (must be first non register value) */
554 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
555 #define VT_LOCAL 0x00f2 /* offset on stack */
556 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
557 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
558 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
559 #define VT_LVAL 0x0100 /* var is an lvalue */
560 #define VT_SYM 0x0200 /* a symbol value is added */
561 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
562 char/short stored in integer registers) */
563 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
564 dereferencing value */
565 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
566 bounding function call point is in vc */
567 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
568 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
569 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
570 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
573 #define VT_INT 0 /* integer type */
574 #define VT_BYTE 1 /* signed byte type */
575 #define VT_SHORT 2 /* short type */
576 #define VT_VOID 3 /* void type */
577 #define VT_PTR 4 /* pointer */
578 #define VT_ENUM 5 /* enum definition */
579 #define VT_FUNC 6 /* function type */
580 #define VT_STRUCT 7 /* struct/union definition */
581 #define VT_FLOAT 8 /* IEEE float */
582 #define VT_DOUBLE 9 /* IEEE double */
583 #define VT_LDOUBLE 10 /* IEEE long double */
584 #define VT_BOOL 11 /* ISOC99 boolean type */
585 #define VT_LLONG 12 /* 64 bit integer */
586 #define VT_LONG 13 /* long integer (NEVER USED as type, only
588 #define VT_BTYPE 0x000f /* mask for basic type */
589 #define VT_UNSIGNED 0x0010 /* unsigned type */
590 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
591 #define VT_BITFIELD 0x0040 /* bitfield modifier */
592 #define VT_CONSTANT 0x0800 /* const modifier */
593 #define VT_VOLATILE 0x1000 /* volatile modifier */
594 #define VT_SIGNED 0x2000 /* signed type */
597 #define VT_EXTERN 0x00000080 /* extern definition */
598 #define VT_STATIC 0x00000100 /* static variable */
599 #define VT_TYPEDEF 0x00000200 /* typedef definition */
600 #define VT_INLINE 0x00000400 /* inline definition */
602 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
604 /* type mask (except storage) */
605 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
606 #define VT_TYPE (~(VT_STORAGE))
610 /* warning: the following compare tokens depend on i386 asm code */
617 #define TOK_Nset 0x98
618 #define TOK_Nclear 0x99
624 #define TOK_LAND 0xa0
628 #define TOK_MID 0xa3 /* inc/dec, to void constant */
630 #define TOK_UDIV 0xb0 /* unsigned division */
631 #define TOK_UMOD 0xb1 /* unsigned modulo */
632 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
633 #define TOK_CINT 0xb3 /* number in tokc */
634 #define TOK_CCHAR 0xb4 /* char constant in tokc */
635 #define TOK_STR 0xb5 /* pointer to string in tokc */
636 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
637 #define TOK_LCHAR 0xb7
638 #define TOK_LSTR 0xb8
639 #define TOK_CFLOAT 0xb9 /* float constant */
640 #define TOK_LINENUM 0xba /* line number info */
641 #define TOK_CDOUBLE 0xc0 /* double constant */
642 #define TOK_CLDOUBLE 0xc1 /* long double constant */
643 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
644 #define TOK_ADDC1 0xc3 /* add with carry generation */
645 #define TOK_ADDC2 0xc4 /* add with carry use */
646 #define TOK_SUBC1 0xc5 /* add with carry generation */
647 #define TOK_SUBC2 0xc6 /* add with carry use */
648 #define TOK_CUINT 0xc8 /* unsigned int constant */
649 #define TOK_CLLONG 0xc9 /* long long constant */
650 #define TOK_CULLONG 0xca /* unsigned long long constant */
651 #define TOK_ARROW 0xcb
652 #define TOK_DOTS 0xcc /* three dots */
653 #define TOK_SHR 0xcd /* unsigned shift right */
654 #define TOK_PPNUM 0xce /* preprocessor number */
656 #define TOK_SHL 0x01 /* shift left */
657 #define TOK_SAR 0x02 /* signed shift right */
659 /* assignement operators : normal operator or 0x80 */
660 #define TOK_A_MOD 0xa5
661 #define TOK_A_AND 0xa6
662 #define TOK_A_MUL 0xaa
663 #define TOK_A_ADD 0xab
664 #define TOK_A_SUB 0xad
665 #define TOK_A_DIV 0xaf
666 #define TOK_A_XOR 0xde
667 #define TOK_A_OR 0xfc
668 #define TOK_A_SHL 0x81
669 #define TOK_A_SAR 0x82
672 #define offsetof(type, field) ((size_t) &((type *)0)->field)
676 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
679 /* WARNING: the content of this string encodes token numbers */
680 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";
682 #define TOK_EOF (-1) /* end of file */
683 #define TOK_LINEFEED 10 /* line feed */
685 /* all identificators and strings have token above that */
686 #define TOK_IDENT 256
688 /* only used for i386 asm opcodes definitions */
689 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
692 DEF(TOK_ASM_ ## x ## b, #x "b") \
693 DEF(TOK_ASM_ ## x ## w, #x "w") \
694 DEF(TOK_ASM_ ## x ## l, #x "l") \
695 DEF(TOK_ASM_ ## x, #x)
698 DEF(TOK_ASM_ ## x ## w, #x "w") \
699 DEF(TOK_ASM_ ## x ## l, #x "l") \
700 DEF(TOK_ASM_ ## x, #x)
703 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
704 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
705 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
706 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
709 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
710 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
713 #define DEF_ASMTEST(x) \
745 #define TOK_ASM_int TOK_INT
748 TOK_LAST
= TOK_IDENT
- 1,
749 #define DEF(id, str) id,
754 static const char tcc_keywords
[] =
755 #define DEF(id, str) str "\0"
760 #define TOK_UIDENT TOK_DEFINE
763 #define snprintf _snprintf
764 #define vsnprintf _vsnprintf
766 #define strtold (long double)strtod
767 #define strtof (float)strtod
768 #define strtoll (long long)strtol
770 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
771 || defined(__OpenBSD__)
772 /* currently incorrect */
773 long double strtold(const char *nptr
, char **endptr
)
775 return (long double)strtod(nptr
, endptr
);
777 float strtof(const char *nptr
, char **endptr
)
779 return (float)strtod(nptr
, endptr
);
782 /* XXX: need to define this to use them in non ISOC99 context */
783 extern float strtof (const char *__nptr
, char **__endptr
);
784 extern long double strtold (const char *__nptr
, char **__endptr
);
787 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
788 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
789 static char *tcc_basename(const char *name
);
790 static char *tcc_fileextension (const char *p
);
792 static void next(void);
793 static void next_nomacro(void);
794 static void parse_expr_type(CType
*type
);
795 static void expr_type(CType
*type
);
796 static void unary_type(CType
*type
);
797 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
798 int case_reg
, int is_expr
);
799 static int expr_const(void);
800 static void expr_eq(void);
801 static void gexpr(void);
802 static void gen_inline_functions(void);
803 static void decl(int l
);
804 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
805 int first
, int size_only
);
806 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
807 int has_init
, int v
, int scope
);
809 void gv2(int rc1
, int rc2
);
810 void move_reg(int r
, int s
);
811 void save_regs(int n
);
812 void save_reg(int r
);
817 int get_reg_ex(int rc
,int rc2
);
820 struct macro_level
*prev
;
824 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
825 const int *macro_str
, struct macro_level
**can_read_stream
);
827 void force_charshort_cast(int t
);
828 static void gen_cast(CType
*type
);
830 static Sym
*sym_find(int v
);
831 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
834 static int type_size(CType
*type
, int *a
);
835 static inline CType
*pointed_type(CType
*type
);
836 static int pointed_size(CType
*type
);
837 static int lvalue_type(int t
);
838 static int parse_btype(CType
*type
, AttributeDef
*ad
);
839 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
840 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
);
841 static int is_compatible_types(CType
*type1
, CType
*type2
);
842 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
844 int ieee_finite(double d
);
845 void error(const char *fmt
, ...);
849 void lexpand_nr(void);
850 static void vpush_global_sym(CType
*type
, int v
);
851 void vset(CType
*type
, int r
, int v
);
852 void type_to_str(char *buf
, int buf_size
,
853 CType
*type
, const char *varstr
);
854 char *get_tok_str(int v
, CValue
*cv
);
855 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
856 unsigned long offset
, unsigned long size
);
857 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
859 /* section generation */
860 static void section_realloc(Section
*sec
, unsigned long new_size
);
861 static void *section_ptr_add(Section
*sec
, unsigned long size
);
862 static void put_extern_sym(Sym
*sym
, Section
*section
,
863 unsigned long value
, unsigned long size
);
864 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
865 static int put_elf_str(Section
*s
, const char *sym
);
866 static int put_elf_sym(Section
*s
,
867 unsigned long value
, unsigned long size
,
868 int info
, int other
, int shndx
, const char *name
);
869 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
870 int info
, int other
, int sh_num
, const char *name
);
871 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
872 int type
, int symbol
);
873 static void put_stabs(const char *str
, int type
, int other
, int desc
,
874 unsigned long value
);
875 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
876 unsigned long value
, Section
*sec
, int sym_index
);
877 static void put_stabn(int type
, int other
, int desc
, int value
);
878 static void put_stabd(int type
, int other
, int desc
);
879 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
881 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
882 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
883 #define AFF_PREPROCESS 0x0004 /* preprocess file */
884 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
887 int tcc_output_coff(TCCState
*s1
, FILE *f
);
890 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
);
891 int pe_load_def_file(struct TCCState
*s1
, int fd
);
892 int pe_test_res_file(void *v
, int size
);
893 int pe_load_res_file(struct TCCState
*s1
, int fd
);
894 void pe_add_runtime(struct TCCState
*s1
);
895 void pe_guess_outfile(char *objfilename
, int output_type
);
896 int pe_output_file(struct TCCState
*s1
, const char *filename
);
900 #ifdef CONFIG_TCC_ASM
902 typedef struct ExprValue
{
907 #define MAX_ASM_OPERANDS 30
909 typedef struct ASMOperand
{
910 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
912 char asm_str
[16]; /* computed asm string for operand */
913 SValue
*vt
; /* C value of the expression */
914 int ref_index
; /* if >= 0, gives reference to a output constraint */
915 int input_index
; /* if >= 0, gives reference to an input constraint */
916 int priority
; /* priority, used to assign registers */
917 int reg
; /* if >= 0, register number used for this operand */
918 int is_llong
; /* true if double register value */
919 int is_memory
; /* true if memory operand */
920 int is_rw
; /* for '+' modifier */
923 static void asm_expr(TCCState
*s1
, ExprValue
*pe
);
924 static int asm_int_expr(TCCState
*s1
);
925 static int find_constraint(ASMOperand
*operands
, int nb_operands
,
926 const char *name
, const char **pp
);
928 static int tcc_assemble(TCCState
*s1
, int do_preprocess
);
932 static void asm_instr(void);
933 static void asm_global_instr(void);
935 /* true if float/double/long double type */
936 static inline int is_float(int t
)
940 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
943 #ifdef TCC_TARGET_I386
944 #include "i386-gen.c"
947 #ifdef TCC_TARGET_ARM
951 #ifdef TCC_TARGET_C67
955 #ifdef TCC_TARGET_X86_64
956 #include "x86_64-gen.c"
959 #ifdef CONFIG_TCC_STATIC
961 #define RTLD_LAZY 0x001
962 #define RTLD_NOW 0x002
963 #define RTLD_GLOBAL 0x100
964 #define RTLD_DEFAULT NULL
966 /* dummy function for profiling */
967 void *dlopen(const char *filename
, int flag
)
972 const char *dlerror(void)
977 typedef struct TCCSyms
{
982 #define TCCSYM(a) { #a, &a, },
984 /* add the symbol you want here if no dynamic linking is done */
985 static TCCSyms tcc_syms
[] = {
986 #if !defined(CONFIG_TCCBOOT)
995 void *resolve_sym(TCCState
*s1
, const char *symbol
, int type
)
999 while (p
->str
!= NULL
) {
1000 if (!strcmp(p
->str
, symbol
))
1007 #elif !defined(_WIN32)
1011 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
)
1013 return dlsym(RTLD_DEFAULT
, sym
);
1018 /********************************************************/
1020 /* we use our own 'finite' function to avoid potential problems with
1021 non standard math libs */
1022 /* XXX: endianness dependent */
1023 int ieee_finite(double d
)
1026 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
1029 /* copy a string and truncate it. */
1030 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
1037 q_end
= buf
+ buf_size
- 1;
1049 /* strcat and truncate. */
1050 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
1055 pstrcpy(buf
+ len
, buf_size
- len
, s
);
1060 static int strstart(const char *str
, const char *val
, const char **ptr
)
1065 while (*q
!= '\0') {
1077 /* extract the basename of a file */
1078 static char *tcc_basename(const char *name
)
1080 char *p
= strchr(name
, 0);
1091 static char *tcc_fileextension (const char *name
)
1093 char *b
= tcc_basename(name
);
1094 char *e
= strrchr(b
, '.');
1095 return e
? e
: strchr(b
, 0);
1099 char *normalize_slashes(char *path
)
1102 for (p
= path
; *p
; ++p
)
1108 char *w32_tcc_lib_path(void)
1110 /* on win32, we suppose the lib and includes are at the location
1112 char path
[1024], *p
;
1113 GetModuleFileNameA(NULL
, path
, sizeof path
);
1114 p
= tcc_basename(normalize_slashes(strlwr(path
)));
1115 if (p
- 5 > path
&& 0 == strncmp(p
- 5, "/bin/", 5))
1120 return strdup(path
);
1124 void set_pages_executable(void *ptr
, unsigned long length
)
1127 unsigned long old_protect
;
1128 VirtualProtect(ptr
, length
, PAGE_EXECUTE_READWRITE
, &old_protect
);
1130 unsigned long start
, end
;
1131 start
= (unsigned long)ptr
& ~(PAGESIZE
- 1);
1132 end
= (unsigned long)ptr
+ length
;
1133 end
= (end
+ PAGESIZE
- 1) & ~(PAGESIZE
- 1);
1134 mprotect((void *)start
, end
- start
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1138 /* memory management */
1142 unsigned malloc_usable_size(void*);
1145 static inline void tcc_free(void *ptr
)
1148 mem_cur_size
-= malloc_usable_size(ptr
);
1153 static void *tcc_malloc(unsigned long size
)
1158 error("memory full");
1160 mem_cur_size
+= malloc_usable_size(ptr
);
1161 if (mem_cur_size
> mem_max_size
)
1162 mem_max_size
= mem_cur_size
;
1167 static void *tcc_mallocz(unsigned long size
)
1170 ptr
= tcc_malloc(size
);
1171 memset(ptr
, 0, size
);
1175 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
1179 mem_cur_size
-= malloc_usable_size(ptr
);
1181 ptr1
= realloc(ptr
, size
);
1183 /* NOTE: count not correct if alloc error, but not critical */
1184 mem_cur_size
+= malloc_usable_size(ptr1
);
1185 if (mem_cur_size
> mem_max_size
)
1186 mem_max_size
= mem_cur_size
;
1191 static char *tcc_strdup(const char *str
)
1194 ptr
= tcc_malloc(strlen(str
) + 1);
1199 #define free(p) use_tcc_free(p)
1200 #define malloc(s) use_tcc_malloc(s)
1201 #define realloc(p, s) use_tcc_realloc(p, s)
1203 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
1210 /* every power of two we double array size */
1211 if ((nb
& (nb
- 1)) == 0) {
1216 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
1218 error("memory full");
1225 static void dynarray_reset(void *pp
, int *n
)
1228 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
1231 tcc_free(*(void**)pp
);
1235 /* symbol allocator */
1236 static Sym
*__sym_malloc(void)
1238 Sym
*sym_pool
, *sym
, *last_sym
;
1241 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
1242 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
1244 last_sym
= sym_free_first
;
1246 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
1247 sym
->next
= last_sym
;
1251 sym_free_first
= last_sym
;
1255 static inline Sym
*sym_malloc(void)
1258 sym
= sym_free_first
;
1260 sym
= __sym_malloc();
1261 sym_free_first
= sym
->next
;
1265 static inline void sym_free(Sym
*sym
)
1267 sym
->next
= sym_free_first
;
1268 sym_free_first
= sym
;
1271 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
1275 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
1276 strcpy(sec
->name
, name
);
1277 sec
->sh_type
= sh_type
;
1278 sec
->sh_flags
= sh_flags
;
1286 sec
->sh_addralign
= 4;
1289 sec
->sh_addralign
= 1;
1292 sec
->sh_addralign
= 32; /* default conservative alignment */
1296 /* only add section if not private */
1297 if (!(sh_flags
& SHF_PRIVATE
)) {
1298 sec
->sh_num
= s1
->nb_sections
;
1299 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
1304 static void free_section(Section
*s
)
1306 if (s
->link
&& (s
->link
->sh_flags
& SHF_PRIVATE
))
1307 free_section(s
->link
);
1308 if (s
->hash
&& (s
->hash
->sh_flags
& SHF_PRIVATE
))
1309 s
->hash
->link
= NULL
, free_section(s
->hash
);
1314 /* realloc section and set its content to zero */
1315 static void section_realloc(Section
*sec
, unsigned long new_size
)
1318 unsigned char *data
;
1320 size
= sec
->data_allocated
;
1323 while (size
< new_size
)
1325 data
= tcc_realloc(sec
->data
, size
);
1327 error("memory full");
1328 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
1330 sec
->data_allocated
= size
;
1333 /* reserve at least 'size' bytes in section 'sec' from
1334 sec->data_offset. */
1335 static void *section_ptr_add(Section
*sec
, unsigned long size
)
1337 unsigned long offset
, offset1
;
1339 offset
= sec
->data_offset
;
1340 offset1
= offset
+ size
;
1341 if (offset1
> sec
->data_allocated
)
1342 section_realloc(sec
, offset1
);
1343 sec
->data_offset
= offset1
;
1344 return sec
->data
+ offset
;
1347 /* return a reference to a section, and create it if it does not
1349 Section
*find_section(TCCState
*s1
, const char *name
)
1353 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1354 sec
= s1
->sections
[i
];
1355 if (!strcmp(name
, sec
->name
))
1358 /* sections are created as PROGBITS */
1359 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
1362 #define SECTION_ABS ((void *)1)
1364 /* update sym->c so that it points to an external symbol in section
1365 'section' with value 'value' */
1366 static void put_extern_sym2(Sym
*sym
, Section
*section
,
1367 unsigned long value
, unsigned long size
,
1368 int can_add_underscore
)
1370 int sym_type
, sym_bind
, sh_num
, info
, other
, attr
;
1375 if (section
== NULL
)
1377 else if (section
== SECTION_ABS
)
1380 sh_num
= section
->sh_num
;
1384 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
1385 sym_type
= STT_FUNC
;
1386 #ifdef TCC_TARGET_PE
1388 attr
= sym
->type
.ref
->r
;
1389 if (FUNC_EXPORT(attr
))
1391 if (FUNC_CALL(attr
) == FUNC_STDCALL
)
1395 sym_type
= STT_OBJECT
;
1398 if (sym
->type
.t
& VT_STATIC
)
1399 sym_bind
= STB_LOCAL
;
1401 sym_bind
= STB_GLOBAL
;
1404 name
= get_tok_str(sym
->v
, NULL
);
1405 #ifdef CONFIG_TCC_BCHECK
1406 if (do_bounds_check
) {
1409 /* XXX: avoid doing that for statics ? */
1410 /* if bound checking is activated, we change some function
1411 names by adding the "__bound" prefix */
1414 /* XXX: we rely only on malloc hooks */
1427 strcpy(buf
, "__bound_");
1435 #ifdef TCC_TARGET_PE
1436 if ((other
& 2) && can_add_underscore
) {
1437 sprintf(buf1
, "_%s@%d", name
, FUNC_ARGS(attr
));
1441 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
1443 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
1446 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
1447 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
1449 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
1450 esym
->st_value
= value
;
1451 esym
->st_size
= size
;
1452 esym
->st_shndx
= sh_num
;
1453 esym
->st_other
|= other
;
1457 static void put_extern_sym(Sym
*sym
, Section
*section
,
1458 unsigned long value
, unsigned long size
)
1460 put_extern_sym2(sym
, section
, value
, size
, 1);
1463 /* add a new relocation entry to symbol 'sym' in section 's' */
1464 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
1467 put_extern_sym(sym
, NULL
, 0, 0);
1468 /* now we can add ELF relocation info */
1469 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
1472 static inline int isid(int c
)
1474 return (c
>= 'a' && c
<= 'z') ||
1475 (c
>= 'A' && c
<= 'Z') ||
1479 static inline int isnum(int c
)
1481 return c
>= '0' && c
<= '9';
1484 static inline int isoct(int c
)
1486 return c
>= '0' && c
<= '7';
1489 static inline int toup(int c
)
1491 if (c
>= 'a' && c
<= 'z')
1492 return c
- 'a' + 'A';
1497 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1501 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1504 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1508 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1512 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1519 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1520 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1521 (*f
)->filename
, (*f
)->line_num
);
1522 if (file
->line_num
> 0) {
1523 strcat_printf(buf
, sizeof(buf
),
1524 "%s:%d: ", file
->filename
, file
->line_num
);
1526 strcat_printf(buf
, sizeof(buf
),
1527 "%s: ", file
->filename
);
1530 strcat_printf(buf
, sizeof(buf
),
1534 strcat_printf(buf
, sizeof(buf
), "warning: ");
1535 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1537 if (!s1
->error_func
) {
1538 /* default case: stderr */
1539 fprintf(stderr
, "%s\n", buf
);
1541 s1
->error_func(s1
->error_opaque
, buf
);
1543 if (!is_warning
|| s1
->warn_error
)
1548 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1549 void (*error_func
)(void *opaque
, const char *msg
))
1551 s
->error_opaque
= error_opaque
;
1552 s
->error_func
= error_func
;
1556 /* error without aborting current compilation */
1557 void error_noabort(const char *fmt
, ...)
1559 TCCState
*s1
= tcc_state
;
1563 error1(s1
, 0, fmt
, ap
);
1567 void error(const char *fmt
, ...)
1569 TCCState
*s1
= tcc_state
;
1573 error1(s1
, 0, fmt
, ap
);
1575 /* better than nothing: in some cases, we accept to handle errors */
1576 if (s1
->error_set_jmp_enabled
) {
1577 longjmp(s1
->error_jmp_buf
, 1);
1579 /* XXX: eliminate this someday */
1584 void expect(const char *msg
)
1586 error("%s expected", msg
);
1589 void warning(const char *fmt
, ...)
1591 TCCState
*s1
= tcc_state
;
1598 error1(s1
, 1, fmt
, ap
);
1605 error("'%c' expected", c
);
1609 static void test_lvalue(void)
1611 if (!(vtop
->r
& VT_LVAL
))
1615 /* allocate a new token */
1616 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1618 TokenSym
*ts
, **ptable
;
1621 if (tok_ident
>= SYM_FIRST_ANOM
)
1622 error("memory full");
1624 /* expand token table if needed */
1625 i
= tok_ident
- TOK_IDENT
;
1626 if ((i
% TOK_ALLOC_INCR
) == 0) {
1627 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1629 error("memory full");
1630 table_ident
= ptable
;
1633 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1634 table_ident
[i
] = ts
;
1635 ts
->tok
= tok_ident
++;
1636 ts
->sym_define
= NULL
;
1637 ts
->sym_label
= NULL
;
1638 ts
->sym_struct
= NULL
;
1639 ts
->sym_identifier
= NULL
;
1641 ts
->hash_next
= NULL
;
1642 memcpy(ts
->str
, str
, len
);
1643 ts
->str
[len
] = '\0';
1648 #define TOK_HASH_INIT 1
1649 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1651 /* find a token and add it if not found */
1652 static TokenSym
*tok_alloc(const char *str
, int len
)
1654 TokenSym
*ts
, **pts
;
1660 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1661 h
&= (TOK_HASH_SIZE
- 1);
1663 pts
= &hash_ident
[h
];
1668 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1670 pts
= &(ts
->hash_next
);
1672 return tok_alloc_new(pts
, str
, len
);
1675 /* CString handling */
1677 static void cstr_realloc(CString
*cstr
, int new_size
)
1682 size
= cstr
->size_allocated
;
1684 size
= 8; /* no need to allocate a too small first string */
1685 while (size
< new_size
)
1687 data
= tcc_realloc(cstr
->data_allocated
, size
);
1689 error("memory full");
1690 cstr
->data_allocated
= data
;
1691 cstr
->size_allocated
= size
;
1696 static inline void cstr_ccat(CString
*cstr
, int ch
)
1699 size
= cstr
->size
+ 1;
1700 if (size
> cstr
->size_allocated
)
1701 cstr_realloc(cstr
, size
);
1702 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1706 static void cstr_cat(CString
*cstr
, const char *str
)
1718 /* add a wide char */
1719 static void cstr_wccat(CString
*cstr
, int ch
)
1722 size
= cstr
->size
+ sizeof(nwchar_t
);
1723 if (size
> cstr
->size_allocated
)
1724 cstr_realloc(cstr
, size
);
1725 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
1729 static void cstr_new(CString
*cstr
)
1731 memset(cstr
, 0, sizeof(CString
));
1734 /* free string and reset it to NULL */
1735 static void cstr_free(CString
*cstr
)
1737 tcc_free(cstr
->data_allocated
);
1741 #define cstr_reset(cstr) cstr_free(cstr)
1743 /* XXX: unicode ? */
1744 static void add_char(CString
*cstr
, int c
)
1746 if (c
== '\'' || c
== '\"' || c
== '\\') {
1747 /* XXX: could be more precise if char or string */
1748 cstr_ccat(cstr
, '\\');
1750 if (c
>= 32 && c
<= 126) {
1753 cstr_ccat(cstr
, '\\');
1755 cstr_ccat(cstr
, 'n');
1757 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1758 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1759 cstr_ccat(cstr
, '0' + (c
& 7));
1764 /* XXX: buffer overflow */
1765 /* XXX: float tokens */
1766 char *get_tok_str(int v
, CValue
*cv
)
1768 static char buf
[STRING_MAX_SIZE
+ 1];
1769 static CString cstr_buf
;
1775 /* NOTE: to go faster, we give a fixed buffer for small strings */
1776 cstr_reset(&cstr_buf
);
1777 cstr_buf
.data
= buf
;
1778 cstr_buf
.size_allocated
= sizeof(buf
);
1784 /* XXX: not quite exact, but only useful for testing */
1785 sprintf(p
, "%u", cv
->ui
);
1789 /* XXX: not quite exact, but only useful for testing */
1790 sprintf(p
, "%Lu", cv
->ull
);
1793 cstr_ccat(&cstr_buf
, 'L');
1795 cstr_ccat(&cstr_buf
, '\'');
1796 add_char(&cstr_buf
, cv
->i
);
1797 cstr_ccat(&cstr_buf
, '\'');
1798 cstr_ccat(&cstr_buf
, '\0');
1802 len
= cstr
->size
- 1;
1804 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1805 cstr_ccat(&cstr_buf
, '\0');
1808 cstr_ccat(&cstr_buf
, 'L');
1811 cstr_ccat(&cstr_buf
, '\"');
1813 len
= cstr
->size
- 1;
1815 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1817 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
1819 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
1821 cstr_ccat(&cstr_buf
, '\"');
1822 cstr_ccat(&cstr_buf
, '\0');
1831 return strcpy(p
, "...");
1833 return strcpy(p
, "<<=");
1835 return strcpy(p
, ">>=");
1837 if (v
< TOK_IDENT
) {
1838 /* search in two bytes table */
1852 } else if (v
< tok_ident
) {
1853 return table_ident
[v
- TOK_IDENT
]->str
;
1854 } else if (v
>= SYM_FIRST_ANOM
) {
1855 /* special name for anonymous symbol */
1856 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1858 /* should never happen */
1863 return cstr_buf
.data
;
1866 /* push, without hashing */
1867 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
1881 /* find a symbol and return its associated structure. 's' is the top
1882 of the symbol stack */
1883 static Sym
*sym_find2(Sym
*s
, int v
)
1893 /* structure lookup */
1894 static inline Sym
*struct_find(int v
)
1897 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1899 return table_ident
[v
]->sym_struct
;
1902 /* find an identifier */
1903 static inline Sym
*sym_find(int v
)
1906 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1908 return table_ident
[v
]->sym_identifier
;
1911 /* push a given symbol on the symbol stack */
1912 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1921 s
= sym_push2(ps
, v
, type
->t
, c
);
1922 s
->type
.ref
= type
->ref
;
1924 /* don't record fields or anonymous symbols */
1926 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1927 /* record symbol in token array */
1928 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1930 ps
= &ts
->sym_struct
;
1932 ps
= &ts
->sym_identifier
;
1939 /* push a global identifier */
1940 static Sym
*global_identifier_push(int v
, int t
, int c
)
1943 s
= sym_push2(&global_stack
, v
, t
, c
);
1944 /* don't record anonymous symbol */
1945 if (v
< SYM_FIRST_ANOM
) {
1946 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1947 /* modify the top most local identifier, so that
1948 sym_identifier will point to 's' when popped */
1950 ps
= &(*ps
)->prev_tok
;
1957 /* pop symbols until top reaches 'b' */
1958 static void sym_pop(Sym
**ptop
, Sym
*b
)
1968 /* remove symbol in token array */
1970 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1971 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1973 ps
= &ts
->sym_struct
;
1975 ps
= &ts
->sym_identifier
;
1986 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1991 if (strcmp(filename
, "-") == 0)
1992 fd
= 0, filename
= "stdin";
1994 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1995 if ((verbose
== 2 && fd
>= 0) || verbose
== 3)
1996 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
1997 (s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
2000 bf
= tcc_malloc(sizeof(BufferedFile
));
2002 bf
->buf_ptr
= bf
->buffer
;
2003 bf
->buf_end
= bf
->buffer
;
2004 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
2005 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
2007 normalize_slashes(bf
->filename
);
2010 bf
->ifndef_macro
= 0;
2011 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2012 // printf("opening '%s'\n", filename);
2016 void tcc_close(BufferedFile
*bf
)
2018 total_lines
+= bf
->line_num
;
2023 /* fill input buffer and peek next char */
2024 static int tcc_peekc_slow(BufferedFile
*bf
)
2027 /* only tries to read if really end of buffer */
2028 if (bf
->buf_ptr
>= bf
->buf_end
) {
2030 #if defined(PARSE_DEBUG)
2035 len
= read(bf
->fd
, bf
->buffer
, len
);
2042 bf
->buf_ptr
= bf
->buffer
;
2043 bf
->buf_end
= bf
->buffer
+ len
;
2044 *bf
->buf_end
= CH_EOB
;
2046 if (bf
->buf_ptr
< bf
->buf_end
) {
2047 return bf
->buf_ptr
[0];
2049 bf
->buf_ptr
= bf
->buf_end
;
2054 /* return the current character, handling end of block if necessary
2056 static int handle_eob(void)
2058 return tcc_peekc_slow(file
);
2061 /* read next char from current input file and handle end of input buffer */
2062 static inline void inp(void)
2064 ch
= *(++(file
->buf_ptr
));
2065 /* end of buffer/file handling */
2070 /* handle '\[\r]\n' */
2071 static int handle_stray_noerror(void)
2073 while (ch
== '\\') {
2078 } else if (ch
== '\r') {
2092 static void handle_stray(void)
2094 if (handle_stray_noerror())
2095 error("stray '\\' in program");
2098 /* skip the stray and handle the \\n case. Output an error if
2099 incorrect char after the stray */
2100 static int handle_stray1(uint8_t *p
)
2104 if (p
>= file
->buf_end
) {
2121 /* handle just the EOB case, but not stray */
2122 #define PEEKC_EOB(c, p)\
2133 /* handle the complicated stray case */
2134 #define PEEKC(c, p)\
2139 c = handle_stray1(p);\
2144 /* input with '\[\r]\n' handling. Note that this function cannot
2145 handle other characters after '\', so you cannot call it inside
2146 strings or comments */
2147 static void minp(void)
2155 /* single line C++ comments */
2156 static uint8_t *parse_line_comment(uint8_t *p
)
2164 if (c
== '\n' || c
== CH_EOF
) {
2166 } else if (c
== '\\') {
2175 } else if (c
== '\r') {
2193 static uint8_t *parse_comment(uint8_t *p
)
2199 /* fast skip loop */
2202 if (c
== '\n' || c
== '*' || c
== '\\')
2206 if (c
== '\n' || c
== '*' || c
== '\\')
2210 /* now we can handle all the cases */
2214 } else if (c
== '*') {
2220 } else if (c
== '/') {
2221 goto end_of_comment
;
2222 } else if (c
== '\\') {
2227 /* skip '\[\r]\n', otherwise just skip the stray */
2233 } else if (c
== '\r') {
2250 /* stray, eob or eof */
2255 error("unexpected end of file in comment");
2256 } else if (c
== '\\') {
2268 /* space exlcuding newline */
2269 static inline int is_space(int ch
)
2271 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
2274 static inline void skip_spaces(void)
2276 while (is_space(ch
))
2280 /* parse a string without interpreting escapes */
2281 static uint8_t *parse_pp_string(uint8_t *p
,
2282 int sep
, CString
*str
)
2290 } else if (c
== '\\') {
2295 unterminated_string
:
2296 /* XXX: indicate line number of start of string */
2297 error("missing terminating %c character", sep
);
2298 } else if (c
== '\\') {
2299 /* escape : just skip \[\r]\n */
2304 } else if (c
== '\r') {
2307 expect("'\n' after '\r'");
2310 } else if (c
== CH_EOF
) {
2311 goto unterminated_string
;
2314 cstr_ccat(str
, '\\');
2320 } else if (c
== '\n') {
2323 } else if (c
== '\r') {
2327 cstr_ccat(str
, '\r');
2343 /* skip block of text until #else, #elif or #endif. skip also pairs of
2345 void preprocess_skip(void)
2347 int a
, start_of_line
, c
, in_warn_or_error
;
2354 in_warn_or_error
= 0;
2375 } else if (c
== '\\') {
2376 ch
= file
->buf_ptr
[0];
2377 handle_stray_noerror();
2384 if (in_warn_or_error
)
2386 p
= parse_pp_string(p
, c
, NULL
);
2390 if (in_warn_or_error
)
2397 p
= parse_comment(p
);
2398 } else if (ch
== '/') {
2399 p
= parse_line_comment(p
);
2404 if (start_of_line
) {
2409 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
2411 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
2413 else if (tok
== TOK_ENDIF
)
2415 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
2416 in_warn_or_error
= 1;
2430 /* ParseState handling */
2432 /* XXX: currently, no include file info is stored. Thus, we cannot display
2433 accurate messages if the function or data definition spans multiple
2436 /* save current parse state in 's' */
2437 void save_parse_state(ParseState
*s
)
2439 s
->line_num
= file
->line_num
;
2440 s
->macro_ptr
= macro_ptr
;
2445 /* restore parse state from 's' */
2446 void restore_parse_state(ParseState
*s
)
2448 file
->line_num
= s
->line_num
;
2449 macro_ptr
= s
->macro_ptr
;
2454 /* return the number of additional 'ints' necessary to store the
2456 static inline int tok_ext_size(int t
)
2470 error("unsupported token");
2477 return LDOUBLE_SIZE
/ 4;
2483 /* token string handling */
2485 static inline void tok_str_new(TokenString
*s
)
2489 s
->allocated_len
= 0;
2490 s
->last_line_num
= -1;
2493 static void tok_str_free(int *str
)
2498 static int *tok_str_realloc(TokenString
*s
)
2502 if (s
->allocated_len
== 0) {
2505 len
= s
->allocated_len
* 2;
2507 str
= tcc_realloc(s
->str
, len
* sizeof(int));
2509 error("memory full");
2510 s
->allocated_len
= len
;
2515 static void tok_str_add(TokenString
*s
, int t
)
2521 if (len
>= s
->allocated_len
)
2522 str
= tok_str_realloc(s
);
2527 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2534 /* allocate space for worst case */
2535 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2536 str
= tok_str_realloc(s
);
2545 str
[len
++] = cv
->tab
[0];
2554 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
2555 while ((len
+ nb_words
) > s
->allocated_len
)
2556 str
= tok_str_realloc(s
);
2557 cstr
= (CString
*)(str
+ len
);
2559 cstr
->size
= cv
->cstr
->size
;
2560 cstr
->data_allocated
= NULL
;
2561 cstr
->size_allocated
= cstr
->size
;
2562 memcpy((char *)cstr
+ sizeof(CString
),
2563 cv
->cstr
->data
, cstr
->size
);
2570 #if LDOUBLE_SIZE == 8
2573 str
[len
++] = cv
->tab
[0];
2574 str
[len
++] = cv
->tab
[1];
2576 #if LDOUBLE_SIZE == 12
2578 str
[len
++] = cv
->tab
[0];
2579 str
[len
++] = cv
->tab
[1];
2580 str
[len
++] = cv
->tab
[2];
2581 #elif LDOUBLE_SIZE == 16
2583 str
[len
++] = cv
->tab
[0];
2584 str
[len
++] = cv
->tab
[1];
2585 str
[len
++] = cv
->tab
[2];
2586 str
[len
++] = cv
->tab
[3];
2587 #elif LDOUBLE_SIZE != 8
2588 #error add long double size support
2597 /* add the current parse token in token string 's' */
2598 static void tok_str_add_tok(TokenString
*s
)
2602 /* save line number info */
2603 if (file
->line_num
!= s
->last_line_num
) {
2604 s
->last_line_num
= file
->line_num
;
2605 cval
.i
= s
->last_line_num
;
2606 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2608 tok_str_add2(s
, tok
, &tokc
);
2611 #if LDOUBLE_SIZE == 16
2612 #define LDOUBLE_GET(p, cv) \
2617 #elif LDOUBLE_SIZE == 12
2618 #define LDOUBLE_GET(p, cv) \
2622 #elif LDOUBLE_SIZE == 8
2623 #define LDOUBLE_GET(p, cv) \
2627 #error add long double size support
2631 /* get a token from an integer array and increment pointer
2632 accordingly. we code it as a macro to avoid pointer aliasing. */
2633 #define TOK_GET(t, p, cv) \
2648 cv.cstr = (CString *)p; \
2649 cv.cstr->data = (char *)p + sizeof(CString);\
2650 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2659 case TOK_CLDOUBLE: \
2660 LDOUBLE_GET(p, cv); \
2661 p += LDOUBLE_SIZE / 4; \
2668 /* defines handling */
2669 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2673 s
= sym_push2(&define_stack
, v
, macro_type
, (long)str
);
2674 s
->next
= first_arg
;
2675 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2678 /* undefined a define symbol. Its name is just set to zero */
2679 static void define_undef(Sym
*s
)
2683 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2684 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2688 static inline Sym
*define_find(int v
)
2691 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2693 return table_ident
[v
]->sym_define
;
2696 /* free define stack until top reaches 'b' */
2697 static void free_defines(Sym
*b
)
2705 /* do not free args or predefined defines */
2707 tok_str_free((int *)top
->c
);
2709 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2710 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2718 static Sym
*label_find(int v
)
2721 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2723 return table_ident
[v
]->sym_label
;
2726 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
2729 s
= sym_push2(ptop
, v
, 0, 0);
2731 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
2732 if (ptop
== &global_label_stack
) {
2733 /* modify the top most local identifier, so that
2734 sym_identifier will point to 's' when popped */
2736 ps
= &(*ps
)->prev_tok
;
2743 /* pop labels until element last is reached. Look if any labels are
2744 undefined. Define symbols if '&&label' was used. */
2745 static void label_pop(Sym
**ptop
, Sym
*slast
)
2748 for(s
= *ptop
; s
!= slast
; s
= s1
) {
2750 if (s
->r
== LABEL_DECLARED
) {
2751 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
2752 } else if (s
->r
== LABEL_FORWARD
) {
2753 error("label '%s' used but not defined",
2754 get_tok_str(s
->v
, NULL
));
2757 /* define corresponding symbol. A size of
2759 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
2763 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
2769 /* eval an expression for #if/#elif */
2770 static int expr_preprocess(void)
2776 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2777 next(); /* do macro subst */
2778 if (tok
== TOK_DEFINED
) {
2783 c
= define_find(tok
) != 0;
2788 } else if (tok
>= TOK_IDENT
) {
2789 /* if undefined macro */
2793 tok_str_add_tok(&str
);
2795 tok_str_add(&str
, -1); /* simulate end of file */
2796 tok_str_add(&str
, 0);
2797 /* now evaluate C constant expression */
2798 macro_ptr
= str
.str
;
2802 tok_str_free(str
.str
);
2806 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2807 static void tok_print(int *str
)
2813 TOK_GET(t
, str
, cval
);
2816 printf(" %s", get_tok_str(t
, &cval
));
2822 /* parse after #define */
2823 static void parse_define(void)
2825 Sym
*s
, *first
, **ps
;
2826 int v
, t
, varg
, is_vaargs
, c
;
2831 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2832 /* XXX: should check if same macro (ANSI) */
2835 /* '(' must be just after macro definition for MACRO_FUNC */
2836 c
= file
->buf_ptr
[0];
2838 c
= handle_stray1(file
->buf_ptr
);
2843 while (tok
!= ')') {
2847 if (varg
== TOK_DOTS
) {
2848 varg
= TOK___VA_ARGS__
;
2850 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2854 if (varg
< TOK_IDENT
)
2855 error("badly punctuated parameter list");
2856 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2867 /* EOF testing necessary for '-D' handling */
2868 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2869 tok_str_add2(&str
, tok
, &tokc
);
2872 tok_str_add(&str
, 0);
2874 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2877 define_push(v
, t
, str
.str
, first
);
2880 static inline int hash_cached_include(int type
, const char *filename
)
2882 const unsigned char *s
;
2886 h
= TOK_HASH_FUNC(h
, type
);
2889 h
= TOK_HASH_FUNC(h
, *s
);
2892 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
2896 /* XXX: use a token or a hash table to accelerate matching ? */
2897 static CachedInclude
*search_cached_include(TCCState
*s1
,
2898 int type
, const char *filename
)
2902 h
= hash_cached_include(type
, filename
);
2903 i
= s1
->cached_includes_hash
[h
];
2907 e
= s1
->cached_includes
[i
- 1];
2908 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2915 static inline void add_cached_include(TCCState
*s1
, int type
,
2916 const char *filename
, int ifndef_macro
)
2921 if (search_cached_include(s1
, type
, filename
))
2924 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2926 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2930 strcpy(e
->filename
, filename
);
2931 e
->ifndef_macro
= ifndef_macro
;
2932 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2933 /* add in hash table */
2934 h
= hash_cached_include(type
, filename
);
2935 e
->hash_next
= s1
->cached_includes_hash
[h
];
2936 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
2939 static void pragma_parse(TCCState
*s1
)
2944 if (tok
== TOK_pack
) {
2947 #pragma pack(1) // set
2948 #pragma pack() // reset to default
2949 #pragma pack(push,1) // push & set
2950 #pragma pack(pop) // restore previous
2954 if (tok
== TOK_ASM_pop
) {
2956 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
2958 error("out of pack stack");
2960 s1
->pack_stack_ptr
--;
2964 if (tok
== TOK_ASM_push
) {
2966 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
2968 s1
->pack_stack_ptr
++;
2971 if (tok
!= TOK_CINT
) {
2973 error("invalid pack pragma");
2976 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
2980 *s1
->pack_stack_ptr
= val
;
2986 /* is_bof is true if first non space token at beginning of file */
2987 static void preprocess(int is_bof
)
2989 TCCState
*s1
= tcc_state
;
2990 int size
, i
, c
, n
, saved_parse_flags
;
2997 saved_parse_flags
= parse_flags
;
2998 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
2999 PARSE_FLAG_LINEFEED
;
3009 s
= define_find(tok
);
3010 /* undefine symbol by putting an invalid name */
3015 case TOK_INCLUDE_NEXT
:
3016 ch
= file
->buf_ptr
[0];
3017 /* XXX: incorrect if comments : use next_nomacro with a special mode */
3022 } else if (ch
== '\"') {
3027 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
3028 if ((q
- buf
) < sizeof(buf
) - 1)
3031 if (handle_stray_noerror() == 0)
3039 /* eat all spaces and comments after include */
3040 /* XXX: slightly incorrect */
3041 while (ch1
!= '\n' && ch1
!= CH_EOF
)
3045 /* computed #include : either we have only strings or
3046 we have anything enclosed in '<>' */
3049 if (tok
== TOK_STR
) {
3050 while (tok
!= TOK_LINEFEED
) {
3051 if (tok
!= TOK_STR
) {
3053 error("'#include' expects \"FILENAME\" or <FILENAME>");
3055 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
3061 while (tok
!= TOK_LINEFEED
) {
3062 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
3066 /* check syntax and remove '<>' */
3067 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
3068 goto include_syntax
;
3069 memmove(buf
, buf
+ 1, len
- 2);
3070 buf
[len
- 2] = '\0';
3075 e
= search_cached_include(s1
, c
, buf
);
3076 if (e
&& define_find(e
->ifndef_macro
)) {
3077 /* no need to parse the include because the 'ifndef macro'
3080 printf("%s: skipping %s\n", file
->filename
, buf
);
3083 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
3084 error("#include recursion too deep");
3085 /* push current file in stack */
3086 /* XXX: fix current line init */
3087 *s1
->include_stack_ptr
++ = file
;
3089 /* first search in current dir if "header.h" */
3090 size
= tcc_basename(file
->filename
) - file
->filename
;
3091 if (size
> sizeof(buf1
) - 1)
3092 size
= sizeof(buf1
) - 1;
3093 memcpy(buf1
, file
->filename
, size
);
3095 pstrcat(buf1
, sizeof(buf1
), buf
);
3096 f
= tcc_open(s1
, buf1
);
3098 if (tok
== TOK_INCLUDE_NEXT
)
3104 /* now search in all the include paths */
3105 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
3106 for(i
= 0; i
< n
; i
++) {
3108 if (i
< s1
->nb_include_paths
)
3109 path
= s1
->include_paths
[i
];
3111 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
3112 pstrcpy(buf1
, sizeof(buf1
), path
);
3113 pstrcat(buf1
, sizeof(buf1
), "/");
3114 pstrcat(buf1
, sizeof(buf1
), buf
);
3115 f
= tcc_open(s1
, buf1
);
3117 if (tok
== TOK_INCLUDE_NEXT
)
3123 --s1
->include_stack_ptr
;
3124 error("include file '%s' not found", buf
);
3128 printf("%s: including %s\n", file
->filename
, buf1
);
3131 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
3133 /* add include file debug info */
3135 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
3137 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
3138 ch
= file
->buf_ptr
[0];
3146 c
= expr_preprocess();
3152 if (tok
< TOK_IDENT
)
3153 error("invalid argument for '#if%sdef'", c
? "n" : "");
3157 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
3159 file
->ifndef_macro
= tok
;
3162 c
= (define_find(tok
) != 0) ^ c
;
3164 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
3165 error("memory full");
3166 *s1
->ifdef_stack_ptr
++ = c
;
3169 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3170 error("#else without matching #if");
3171 if (s1
->ifdef_stack_ptr
[-1] & 2)
3172 error("#else after #else");
3173 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
3176 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3177 error("#elif without matching #if");
3178 c
= s1
->ifdef_stack_ptr
[-1];
3180 error("#elif after #else");
3181 /* last #if/#elif expression was true: we skip */
3184 c
= expr_preprocess();
3185 s1
->ifdef_stack_ptr
[-1] = c
;
3195 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
3196 error("#endif without matching #if");
3197 s1
->ifdef_stack_ptr
--;
3198 /* '#ifndef macro' was at the start of file. Now we check if
3199 an '#endif' is exactly at the end of file */
3200 if (file
->ifndef_macro
&&
3201 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
3202 file
->ifndef_macro_saved
= file
->ifndef_macro
;
3203 /* need to set to zero to avoid false matches if another
3204 #ifndef at middle of file */
3205 file
->ifndef_macro
= 0;
3206 while (tok
!= TOK_LINEFEED
)
3208 tok_flags
|= TOK_FLAG_ENDIF
;
3214 if (tok
!= TOK_CINT
)
3216 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
3218 if (tok
!= TOK_LINEFEED
) {
3221 pstrcpy(file
->filename
, sizeof(file
->filename
),
3222 (char *)tokc
.cstr
->data
);
3228 ch
= file
->buf_ptr
[0];
3231 while (ch
!= '\n' && ch
!= CH_EOF
) {
3232 if ((q
- buf
) < sizeof(buf
) - 1)
3235 if (handle_stray_noerror() == 0)
3242 error("#error %s", buf
);
3244 warning("#warning %s", buf
);
3250 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
3251 /* '!' is ignored to allow C scripts. numbers are ignored
3252 to emulate cpp behaviour */
3254 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
3255 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
3259 /* ignore other preprocess commands or #! for C scripts */
3260 while (tok
!= TOK_LINEFEED
)
3263 parse_flags
= saved_parse_flags
;
3266 /* evaluate escape codes in a string. */
3267 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
3282 case '0': case '1': case '2': case '3':
3283 case '4': case '5': case '6': case '7':
3284 /* at most three octal digits */
3289 n
= n
* 8 + c
- '0';
3293 n
= n
* 8 + c
- '0';
3298 goto add_char_nonext
;
3306 if (c
>= 'a' && c
<= 'f')
3308 else if (c
>= 'A' && c
<= 'F')
3318 goto add_char_nonext
;
3342 goto invalid_escape
;
3352 if (c
>= '!' && c
<= '~')
3353 warning("unknown escape sequence: \'\\%c\'", c
);
3355 warning("unknown escape sequence: \'\\x%x\'", c
);
3362 cstr_ccat(outstr
, c
);
3364 cstr_wccat(outstr
, c
);
3366 /* add a trailing '\0' */
3368 cstr_ccat(outstr
, '\0');
3370 cstr_wccat(outstr
, '\0');
3373 /* we use 64 bit numbers */
3376 /* bn = (bn << shift) | or_val */
3377 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
3381 for(i
=0;i
<BN_SIZE
;i
++) {
3383 bn
[i
] = (v
<< shift
) | or_val
;
3384 or_val
= v
>> (32 - shift
);
3388 void bn_zero(unsigned int *bn
)
3391 for(i
=0;i
<BN_SIZE
;i
++) {
3396 /* parse number in null terminated string 'p' and return it in the
3398 void parse_number(const char *p
)
3400 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
3402 unsigned int bn
[BN_SIZE
];
3413 goto float_frac_parse
;
3414 } else if (t
== '0') {
3415 if (ch
== 'x' || ch
== 'X') {
3419 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
3425 /* parse all digits. cannot check octal numbers at this stage
3426 because of floating point constants */
3428 if (ch
>= 'a' && ch
<= 'f')
3430 else if (ch
>= 'A' && ch
<= 'F')
3438 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
3440 error("number too long");
3446 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
3447 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
3449 /* NOTE: strtox should support that for hexa numbers, but
3450 non ISOC99 libcs do not support it, so we prefer to do
3452 /* hexadecimal or binary floats */
3453 /* XXX: handle overflows */
3465 } else if (t
>= 'a') {
3467 } else if (t
>= 'A') {
3472 bn_lshift(bn
, shift
, t
);
3479 if (t
>= 'a' && t
<= 'f') {
3481 } else if (t
>= 'A' && t
<= 'F') {
3483 } else if (t
>= '0' && t
<= '9') {
3489 error("invalid digit");
3490 bn_lshift(bn
, shift
, t
);
3495 if (ch
!= 'p' && ch
!= 'P')
3502 } else if (ch
== '-') {
3506 if (ch
< '0' || ch
> '9')
3507 expect("exponent digits");
3508 while (ch
>= '0' && ch
<= '9') {
3509 exp_val
= exp_val
* 10 + ch
- '0';
3512 exp_val
= exp_val
* s
;
3514 /* now we can generate the number */
3515 /* XXX: should patch directly float number */
3516 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
3517 d
= ldexp(d
, exp_val
- frac_bits
);
3522 /* float : should handle overflow */
3524 } else if (t
== 'L') {
3527 /* XXX: not large enough */
3528 tokc
.ld
= (long double)d
;
3534 /* decimal floats */
3536 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3541 while (ch
>= '0' && ch
<= '9') {
3542 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3548 if (ch
== 'e' || ch
== 'E') {
3549 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3553 if (ch
== '-' || ch
== '+') {
3554 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3559 if (ch
< '0' || ch
> '9')
3560 expect("exponent digits");
3561 while (ch
>= '0' && ch
<= '9') {
3562 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3574 tokc
.f
= strtof(token_buf
, NULL
);
3575 } else if (t
== 'L') {
3578 tokc
.ld
= strtold(token_buf
, NULL
);
3581 tokc
.d
= strtod(token_buf
, NULL
);
3585 unsigned long long n
, n1
;
3588 /* integer number */
3591 if (b
== 10 && *q
== '0') {
3598 /* no need for checks except for base 10 / 8 errors */
3601 } else if (t
>= 'a') {
3603 } else if (t
>= 'A') {
3608 error("invalid digit");
3612 /* detect overflow */
3613 /* XXX: this test is not reliable */
3615 error("integer constant overflow");
3618 /* XXX: not exactly ANSI compliant */
3619 if ((n
& 0xffffffff00000000LL
) != 0) {
3624 } else if (n
> 0x7fffffff) {
3635 error("three 'l's in integer constant");
3638 if (tok
== TOK_CINT
)
3640 else if (tok
== TOK_CUINT
)
3644 } else if (t
== 'U') {
3646 error("two 'u's in integer constant");
3648 if (tok
== TOK_CINT
)
3650 else if (tok
== TOK_CLLONG
)
3657 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
3665 #define PARSE2(c1, tok1, c2, tok2) \
3676 /* return next token without macro substitution */
3677 static inline void next_nomacro1(void)
3684 cstr_reset(&tok_spaces
);
3694 cstr_ccat(&tok_spaces
, c
);
3699 /* first look if it is in fact an end of buffer */
3700 if (p
>= file
->buf_end
) {
3704 if (p
>= file
->buf_end
)
3717 TCCState
*s1
= tcc_state
;
3718 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
3719 && !(tok_flags
& TOK_FLAG_EOF
)) {
3720 tok_flags
|= TOK_FLAG_EOF
;
3722 goto keep_tok_flags
;
3723 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
3724 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3725 /* no include left : end of file. */
3728 tok_flags
&= ~TOK_FLAG_EOF
;
3729 /* pop include file */
3731 /* test if previous '#endif' was after a #ifdef at
3733 if (tok_flags
& TOK_FLAG_ENDIF
) {
3735 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3737 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3738 file
->ifndef_macro_saved
);
3741 /* add end of include file debug info */
3743 put_stabd(N_EINCL
, 0, 0);
3745 /* pop include stack */
3747 s1
->include_stack_ptr
--;
3748 file
= *s1
->include_stack_ptr
;
3757 tok_flags
|= TOK_FLAG_BOL
;
3759 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
3762 goto keep_tok_flags
;
3767 if ((tok_flags
& TOK_FLAG_BOL
) &&
3768 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3770 preprocess(tok_flags
& TOK_FLAG_BOF
);
3776 tok
= TOK_TWOSHARPS
;
3778 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
3779 p
= parse_line_comment(p
- 1);
3788 case 'a': case 'b': case 'c': case 'd':
3789 case 'e': case 'f': case 'g': case 'h':
3790 case 'i': case 'j': case 'k': case 'l':
3791 case 'm': case 'n': case 'o': case 'p':
3792 case 'q': case 'r': case 's': case 't':
3793 case 'u': case 'v': case 'w': case 'x':
3795 case 'A': case 'B': case 'C': case 'D':
3796 case 'E': case 'F': case 'G': case 'H':
3797 case 'I': case 'J': case 'K':
3798 case 'M': case 'N': case 'O': case 'P':
3799 case 'Q': case 'R': case 'S': case 'T':
3800 case 'U': case 'V': case 'W': case 'X':
3806 h
= TOK_HASH_FUNC(h
, c
);
3810 if (!isidnum_table
[c
-CH_EOF
])
3812 h
= TOK_HASH_FUNC(h
, c
);
3819 /* fast case : no stray found, so we have the full token
3820 and we have already hashed it */
3822 h
&= (TOK_HASH_SIZE
- 1);
3823 pts
= &hash_ident
[h
];
3828 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3830 pts
= &(ts
->hash_next
);
3832 ts
= tok_alloc_new(pts
, p1
, len
);
3836 cstr_reset(&tokcstr
);
3839 cstr_ccat(&tokcstr
, *p1
);
3845 while (isidnum_table
[c
-CH_EOF
]) {
3846 cstr_ccat(&tokcstr
, c
);
3849 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3855 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3857 goto parse_ident_fast
;
3860 if (c
== '\'' || c
== '\"') {
3864 cstr_reset(&tokcstr
);
3865 cstr_ccat(&tokcstr
, 'L');
3866 goto parse_ident_slow
;
3870 case '0': case '1': case '2': case '3':
3871 case '4': case '5': case '6': case '7':
3874 cstr_reset(&tokcstr
);
3875 /* after the first digit, accept digits, alpha, '.' or sign if
3876 prefixed by 'eEpP' */
3880 cstr_ccat(&tokcstr
, c
);
3882 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3883 ((c
== '+' || c
== '-') &&
3884 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3887 /* We add a trailing '\0' to ease parsing */
3888 cstr_ccat(&tokcstr
, '\0');
3889 tokc
.cstr
= &tokcstr
;
3893 /* special dot handling because it can also start a number */
3896 cstr_reset(&tokcstr
);
3897 cstr_ccat(&tokcstr
, '.');
3899 } else if (c
== '.') {
3919 /* parse the string */
3921 p
= parse_pp_string(p
, sep
, &str
);
3922 cstr_ccat(&str
, '\0');
3924 /* eval the escape (should be done as TOK_PPNUM) */
3925 cstr_reset(&tokcstr
);
3926 parse_escape_string(&tokcstr
, str
.data
, is_long
);
3931 /* XXX: make it portable */
3935 char_size
= sizeof(nwchar_t
);
3936 if (tokcstr
.size
<= char_size
)
3937 error("empty character constant");
3938 if (tokcstr
.size
> 2 * char_size
)
3939 warning("multi-character character constant");
3941 tokc
.i
= *(int8_t *)tokcstr
.data
;
3944 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
3948 tokc
.cstr
= &tokcstr
;
3962 } else if (c
== '<') {
3980 } else if (c
== '>') {
3998 } else if (c
== '=') {
4011 } else if (c
== '=') {
4024 } else if (c
== '=') {
4037 } else if (c
== '=') {
4040 } else if (c
== '>') {
4048 PARSE2('!', '!', '=', TOK_NE
)
4049 PARSE2('=', '=', '=', TOK_EQ
)
4050 PARSE2('*', '*', '=', TOK_A_MUL
)
4051 PARSE2('%', '%', '=', TOK_A_MOD
)
4052 PARSE2('^', '^', '=', TOK_A_XOR
)
4054 /* comments or operator */
4058 p
= parse_comment(p
);
4060 } else if (c
== '/') {
4061 p
= parse_line_comment(p
);
4063 } else if (c
== '=') {
4083 case '$': /* only used in assembler */
4084 case '@': /* dito */
4089 error("unrecognized character \\x%02x", c
);
4095 #if defined(PARSE_DEBUG)
4096 printf("token = %s\n", get_tok_str(tok
, &tokc
));
4100 /* return next token without macro substitution. Can read input from
4102 static void next_nomacro(void)
4108 TOK_GET(tok
, macro_ptr
, tokc
);
4109 if (tok
== TOK_LINENUM
) {
4110 file
->line_num
= tokc
.i
;
4119 /* substitute args in macro_str and return allocated string */
4120 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
4122 int *st
, last_tok
, t
, notfirst
;
4131 TOK_GET(t
, macro_str
, cval
);
4136 TOK_GET(t
, macro_str
, cval
);
4139 s
= sym_find2(args
, t
);
4146 cstr_ccat(&cstr
, ' ');
4147 TOK_GET(t
, st
, cval
);
4148 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
4153 cstr_ccat(&cstr
, '\0');
4155 printf("stringize: %s\n", (char *)cstr
.data
);
4159 tok_str_add2(&str
, TOK_STR
, &cval
);
4162 tok_str_add2(&str
, t
, &cval
);
4164 } else if (t
>= TOK_IDENT
) {
4165 s
= sym_find2(args
, t
);
4168 /* if '##' is present before or after, no arg substitution */
4169 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
4170 /* special case for var arg macros : ## eats the
4171 ',' if empty VA_ARGS variable. */
4172 /* XXX: test of the ',' is not 100%
4173 reliable. should fix it to avoid security
4175 if (gnu_ext
&& s
->type
.t
&&
4176 last_tok
== TOK_TWOSHARPS
&&
4177 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
4179 /* suppress ',' '##' */
4182 /* suppress '##' and add variable */
4190 TOK_GET(t1
, st
, cval
);
4193 tok_str_add2(&str
, t1
, &cval
);
4197 /* NOTE: the stream cannot be read when macro
4198 substituing an argument */
4199 macro_subst(&str
, nested_list
, st
, NULL
);
4202 tok_str_add(&str
, t
);
4205 tok_str_add2(&str
, t
, &cval
);
4209 tok_str_add(&str
, 0);
4213 static char const ab_month_name
[12][4] =
4215 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4216 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4219 /* do macro substitution of current token with macro 's' and add
4220 result to (tok_str,tok_len). 'nested_list' is the list of all
4221 macros we got inside to avoid recursing. Return non zero if no
4222 substitution needs to be done */
4223 static int macro_subst_tok(TokenString
*tok_str
,
4224 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
4226 Sym
*args
, *sa
, *sa1
;
4227 int mstr_allocated
, parlevel
, *mstr
, t
, t1
;
4234 /* if symbol is a macro, prepare substitution */
4235 /* special macros */
4236 if (tok
== TOK___LINE__
) {
4237 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
4241 } else if (tok
== TOK___FILE__
) {
4242 cstrval
= file
->filename
;
4244 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
4249 tm
= localtime(&ti
);
4250 if (tok
== TOK___DATE__
) {
4251 snprintf(buf
, sizeof(buf
), "%s %2d %d",
4252 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
4254 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
4255 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
4262 cstr_cat(&cstr
, cstrval
);
4263 cstr_ccat(&cstr
, '\0');
4265 tok_str_add2(tok_str
, t1
, &cval
);
4270 if (s
->type
.t
== MACRO_FUNC
) {
4271 /* NOTE: we do not use next_nomacro to avoid eating the
4272 next token. XXX: find better solution */
4276 if (t
== 0 && can_read_stream
) {
4277 /* end of macro stream: we must look at the token
4278 after in the file */
4279 struct macro_level
*ml
= *can_read_stream
;
4285 *can_read_stream
= ml
-> prev
;
4290 /* XXX: incorrect with comments */
4291 ch
= file
->buf_ptr
[0];
4292 while (is_space(ch
) || ch
== '\n')
4296 if (t
!= '(') /* no macro subst */
4299 /* argument macro */
4304 /* NOTE: empty args are allowed, except if no args */
4306 /* handle '()' case */
4307 if (!args
&& !sa
&& tok
== ')')
4310 error("macro '%s' used with too many args",
4311 get_tok_str(s
->v
, 0));
4314 /* NOTE: non zero sa->t indicates VA_ARGS */
4315 while ((parlevel
> 0 ||
4317 (tok
!= ',' || sa
->type
.t
))) &&
4321 else if (tok
== ')')
4323 if (tok
!= TOK_LINEFEED
)
4324 tok_str_add2(&str
, tok
, &tokc
);
4327 tok_str_add(&str
, 0);
4328 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (long)str
.str
);
4331 /* special case for gcc var args: add an empty
4332 var arg argument if it is omitted */
4333 if (sa
&& sa
->type
.t
&& gnu_ext
)
4343 error("macro '%s' used with too few args",
4344 get_tok_str(s
->v
, 0));
4347 /* now subst each arg */
4348 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
4353 tok_str_free((int *)sa
->c
);
4359 sym_push2(nested_list
, s
->v
, 0, 0);
4360 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
4361 /* pop nested defined symbol */
4363 *nested_list
= sa1
->prev
;
4371 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4372 return the resulting string (which must be freed). */
4373 static inline int *macro_twosharps(const int *macro_str
)
4376 const int *macro_ptr1
, *start_macro_ptr
, *ptr
, *saved_macro_ptr
;
4378 const char *p1
, *p2
;
4380 TokenString macro_str1
;
4383 start_macro_ptr
= macro_str
;
4384 /* we search the first '##' */
4386 macro_ptr1
= macro_str
;
4387 TOK_GET(t
, macro_str
, cval
);
4388 /* nothing more to do if end of string */
4391 if (*macro_str
== TOK_TWOSHARPS
)
4395 /* we saw '##', so we need more processing to handle it */
4397 tok_str_new(¯o_str1
);
4401 /* add all tokens seen so far */
4402 for(ptr
= start_macro_ptr
; ptr
< macro_ptr1
;) {
4403 TOK_GET(t
, ptr
, cval
);
4404 tok_str_add2(¯o_str1
, t
, &cval
);
4406 saved_macro_ptr
= macro_ptr
;
4407 /* XXX: get rid of the use of macro_ptr here */
4408 macro_ptr
= (int *)macro_str
;
4410 while (*macro_ptr
== TOK_TWOSHARPS
) {
4412 macro_ptr1
= macro_ptr
;
4415 TOK_GET(t
, macro_ptr
, cval
);
4416 /* We concatenate the two tokens if we have an
4417 identifier or a preprocessing number */
4419 p1
= get_tok_str(tok
, &tokc
);
4420 cstr_cat(&cstr
, p1
);
4421 p2
= get_tok_str(t
, &cval
);
4422 cstr_cat(&cstr
, p2
);
4423 cstr_ccat(&cstr
, '\0');
4425 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
4426 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
4427 if (tok
== TOK_PPNUM
) {
4428 /* if number, then create a number token */
4429 /* NOTE: no need to allocate because
4430 tok_str_add2() does it */
4431 cstr_reset(&tokcstr
);
4434 tokc
.cstr
= &tokcstr
;
4436 /* if identifier, we must do a test to
4437 validate we have a correct identifier */
4438 if (t
== TOK_PPNUM
) {
4448 if (!isnum(c
) && !isid(c
))
4452 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
4453 tok
= ts
->tok
; /* modify current token */
4456 const char *str
= cstr
.data
;
4457 const unsigned char *q
;
4459 /* we look for a valid token */
4460 /* XXX: do more extensive checks */
4461 if (!strcmp(str
, ">>=")) {
4463 } else if (!strcmp(str
, "<<=")) {
4465 } else if (strlen(str
) == 2) {
4466 /* search in two bytes table */
4471 if (q
[0] == str
[0] && q
[1] == str
[1])
4478 /* NOTE: because get_tok_str use a static buffer,
4481 p1
= get_tok_str(tok
, &tokc
);
4482 cstr_cat(&cstr
, p1
);
4483 cstr_ccat(&cstr
, '\0');
4484 p2
= get_tok_str(t
, &cval
);
4485 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
4486 /* cannot merge tokens: just add them separately */
4487 tok_str_add2(¯o_str1
, tok
, &tokc
);
4488 /* XXX: free associated memory ? */
4495 tok_str_add2(¯o_str1
, tok
, &tokc
);
4500 macro_ptr
= (int *)saved_macro_ptr
;
4502 tok_str_add(¯o_str1
, 0);
4503 return macro_str1
.str
;
4507 /* do macro substitution of macro_str and add result to
4508 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4509 inside to avoid recursing. */
4510 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
4511 const int *macro_str
, struct macro_level
** can_read_stream
)
4518 struct macro_level ml
;
4520 /* first scan for '##' operator handling */
4522 macro_str1
= macro_twosharps(ptr
);
4526 /* NOTE: ptr == NULL can only happen if tokens are read from
4527 file stream due to a macro function call */
4530 TOK_GET(t
, ptr
, cval
);
4535 /* if nested substitution, do nothing */
4536 if (sym_find2(*nested_list
, t
))
4539 if (can_read_stream
)
4540 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
4541 macro_ptr
= (int *)ptr
;
4543 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
4544 ptr
= (int *)macro_ptr
;
4546 if (can_read_stream
&& *can_read_stream
== &ml
)
4547 *can_read_stream
= ml
.prev
;
4552 tok_str_add2(tok_str
, t
, &cval
);
4556 tok_str_free(macro_str1
);
4559 /* return next token with macro substitution */
4560 static void next(void)
4562 Sym
*nested_list
, *s
;
4564 struct macro_level
*ml
;
4569 /* if not reading from macro substituted string, then try
4570 to substitute macros */
4571 if (tok
>= TOK_IDENT
&&
4572 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
4573 s
= define_find(tok
);
4575 /* we have a macro: we try to substitute */
4579 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
4580 /* substitution done, NOTE: maybe empty */
4581 tok_str_add(&str
, 0);
4582 macro_ptr
= str
.str
;
4583 macro_ptr_allocated
= str
.str
;
4590 /* end of macro or end of unget buffer */
4591 if (unget_buffer_enabled
) {
4592 macro_ptr
= unget_saved_macro_ptr
;
4593 unget_buffer_enabled
= 0;
4595 /* end of macro string: free it */
4596 tok_str_free(macro_ptr_allocated
);
4603 /* convert preprocessor tokens into C tokens */
4604 if (tok
== TOK_PPNUM
&&
4605 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
4606 parse_number((char *)tokc
.cstr
->data
);
4610 /* push back current token and set current token to 'last_tok'. Only
4611 identifier case handled for labels. */
4612 static inline void unget_tok(int last_tok
)
4616 unget_saved_macro_ptr
= macro_ptr
;
4617 unget_buffer_enabled
= 1;
4618 q
= unget_saved_buffer
;
4621 n
= tok_ext_size(tok
) - 1;
4624 *q
= 0; /* end of token string */
4629 void swap(int *p
, int *q
)
4637 void vsetc(CType
*type
, int r
, CValue
*vc
)
4641 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4642 error("memory full");
4643 /* cannot let cpu flags if other instruction are generated. Also
4644 avoid leaving VT_JMP anywhere except on the top of the stack
4645 because it would complicate the code generator. */
4646 if (vtop
>= vstack
) {
4647 v
= vtop
->r
& VT_VALMASK
;
4648 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
4654 vtop
->r2
= VT_CONST
;
4658 /* push integer constant */
4663 vsetc(&int_type
, VT_CONST
, &cval
);
4666 /* Return a static symbol pointing to a section */
4667 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
4668 unsigned long offset
, unsigned long size
)
4674 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
4675 sym
->type
.ref
= type
->ref
;
4676 sym
->r
= VT_CONST
| VT_SYM
;
4677 put_extern_sym(sym
, sec
, offset
, size
);
4681 /* push a reference to a section offset by adding a dummy symbol */
4682 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
4687 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4688 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
4691 /* define a new external reference to a symbol 'v' of type 'u' */
4692 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
4698 /* push forward reference */
4699 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
4700 s
->type
.ref
= type
->ref
;
4701 s
->r
= r
| VT_CONST
| VT_SYM
;
4706 /* define a new external reference to a symbol 'v' of type 'u' */
4707 static Sym
*external_sym(int v
, CType
*type
, int r
)
4713 /* push forward reference */
4714 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
4715 s
->type
.t
|= VT_EXTERN
;
4717 if (!is_compatible_types(&s
->type
, type
))
4718 error("incompatible types for redefinition of '%s'",
4719 get_tok_str(v
, NULL
));
4724 /* push a reference to global symbol v */
4725 static void vpush_global_sym(CType
*type
, int v
)
4730 sym
= external_global_sym(v
, type
, 0);
4732 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4736 void vset(CType
*type
, int r
, int v
)
4741 vsetc(type
, r
, &cval
);
4744 void vseti(int r
, int v
)
4760 void vpushv(SValue
*v
)
4762 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4763 error("memory full");
4773 /* save r to the memory stack, and mark it as being free */
4774 void save_reg(int r
)
4776 int l
, saved
, size
, align
;
4780 /* modify all stack values */
4783 for(p
=vstack
;p
<=vtop
;p
++) {
4784 if ((p
->r
& VT_VALMASK
) == r
||
4785 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
4786 /* must save value on stack if not already done */
4788 /* NOTE: must reload 'r' because r might be equal to r2 */
4789 r
= p
->r
& VT_VALMASK
;
4790 /* store register in the stack */
4792 #ifndef TCC_TARGET_X86_64
4793 if ((p
->r
& VT_LVAL
) ||
4794 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
4798 type
= &char_pointer_type
;
4800 size
= type_size(type
, &align
);
4801 loc
= (loc
- size
) & -align
;
4802 sv
.type
.t
= type
->t
;
4803 sv
.r
= VT_LOCAL
| VT_LVAL
;
4806 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
4807 /* x86 specific: need to pop fp register ST0 if saved */
4808 if (r
== TREG_ST0
) {
4809 o(0xd9dd); /* fstp %st(1) */
4812 #ifndef TCC_TARGET_X86_64
4813 /* special long long case */
4814 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
4822 /* mark that stack entry as being saved on the stack */
4823 if (p
->r
& VT_LVAL
) {
4824 /* also clear the bounded flag because the
4825 relocation address of the function was stored in
4827 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4829 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4837 /* find a register of class 'rc2' with at most one reference on stack.
4838 * If none, call get_reg(rc) */
4839 int get_reg_ex(int rc
, int rc2
)
4844 for(r
=0;r
<NB_REGS
;r
++) {
4845 if (reg_classes
[r
] & rc2
) {
4848 for(p
= vstack
; p
<= vtop
; p
++) {
4849 if ((p
->r
& VT_VALMASK
) == r
||
4850 (p
->r2
& VT_VALMASK
) == r
)
4860 /* find a free register of class 'rc'. If none, save one register */
4866 /* find a free register */
4867 for(r
=0;r
<NB_REGS
;r
++) {
4868 if (reg_classes
[r
] & rc
) {
4869 for(p
=vstack
;p
<=vtop
;p
++) {
4870 if ((p
->r
& VT_VALMASK
) == r
||
4871 (p
->r2
& VT_VALMASK
) == r
)
4879 /* no register left : free the first one on the stack (VERY
4880 IMPORTANT to start from the bottom to ensure that we don't
4881 spill registers used in gen_opi()) */
4882 for(p
=vstack
;p
<=vtop
;p
++) {
4883 r
= p
->r
& VT_VALMASK
;
4884 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4886 /* also look at second register (if long long) */
4887 r
= p
->r2
& VT_VALMASK
;
4888 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4894 /* Should never comes here */
4898 /* save registers up to (vtop - n) stack entry */
4899 void save_regs(int n
)
4904 for(p
= vstack
;p
<= p1
; p
++) {
4905 r
= p
->r
& VT_VALMASK
;
4912 /* move register 's' to 'r', and flush previous value of r to memory
4914 void move_reg(int r
, int s
)
4927 /* get address of vtop (vtop MUST BE an lvalue) */
4930 vtop
->r
&= ~VT_LVAL
;
4931 /* tricky: if saved lvalue, then we can go back to lvalue */
4932 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4933 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4936 #ifdef CONFIG_TCC_BCHECK
4937 /* generate lvalue bound code */
4943 vtop
->r
&= ~VT_MUSTBOUND
;
4944 /* if lvalue, then use checking code before dereferencing */
4945 if (vtop
->r
& VT_LVAL
) {
4946 /* if not VT_BOUNDED value, then make one */
4947 if (!(vtop
->r
& VT_BOUNDED
)) {
4948 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4949 /* must save type because we must set it to int to get pointer */
4951 vtop
->type
.t
= VT_INT
;
4954 gen_bounded_ptr_add();
4955 vtop
->r
|= lval_type
;
4958 /* then check for dereferencing */
4959 gen_bounded_ptr_deref();
4964 /* store vtop a register belonging to class 'rc'. lvalues are
4965 converted to values. Cannot be used if cannot be converted to
4966 register value (such as structures). */
4969 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4971 /* NOTE: get_reg can modify vstack[] */
4972 if (vtop
->type
.t
& VT_BITFIELD
) {
4974 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4975 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4976 /* remove bit field info to avoid loops */
4977 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4978 /* cast to int to propagate signedness in following ops */
4980 if((vtop
->type
.t
& VT_UNSIGNED
) ||
4981 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
4982 type
.t
|= VT_UNSIGNED
;
4984 /* generate shifts */
4985 vpushi(32 - (bit_pos
+ bit_size
));
4987 vpushi(32 - bit_size
);
4988 /* NOTE: transformed to SHR if unsigned */
4992 if (is_float(vtop
->type
.t
) &&
4993 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4996 unsigned long offset
;
4997 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5001 /* XXX: unify with initializers handling ? */
5002 /* CPUs usually cannot use float constants, so we store them
5003 generically in data segment */
5004 size
= type_size(&vtop
->type
, &align
);
5005 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
5006 data_section
->data_offset
= offset
;
5007 /* XXX: not portable yet */
5008 #if defined(__i386__) || defined(__x86_64__)
5009 /* Zero pad x87 tenbyte long doubles */
5010 if (size
== LDOUBLE_SIZE
)
5011 vtop
->c
.tab
[2] &= 0xffff;
5013 ptr
= section_ptr_add(data_section
, size
);
5015 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5019 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
5023 ptr
[i
] = vtop
->c
.tab
[i
];
5024 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
5025 vtop
->r
|= VT_LVAL
| VT_SYM
;
5029 #ifdef CONFIG_TCC_BCHECK
5030 if (vtop
->r
& VT_MUSTBOUND
)
5034 r
= vtop
->r
& VT_VALMASK
;
5038 /* need to reload if:
5040 - lvalue (need to dereference pointer)
5041 - already a register, but not in the right class */
5042 if (r
>= VT_CONST
||
5043 (vtop
->r
& VT_LVAL
) ||
5044 !(reg_classes
[r
] & rc
) ||
5045 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
5046 !(reg_classes
[vtop
->r2
] & rc2
))) {
5048 #ifndef TCC_TARGET_X86_64
5049 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
5051 unsigned long long ll
;
5052 /* two register type load : expand to two words
5054 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
5057 vtop
->c
.ui
= ll
; /* first word */
5059 vtop
->r
= r
; /* save register value */
5060 vpushi(ll
>> 32); /* second word */
5061 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
5062 (vtop
->r
& VT_LVAL
)) {
5063 /* We do not want to modifier the long long
5064 pointer here, so the safest (and less
5065 efficient) is to save all the other registers
5066 in the stack. XXX: totally inefficient. */
5068 /* load from memory */
5071 vtop
[-1].r
= r
; /* save register value */
5072 /* increment pointer to get second word */
5073 vtop
->type
.t
= VT_INT
;
5079 /* move registers */
5082 vtop
[-1].r
= r
; /* save register value */
5083 vtop
->r
= vtop
[-1].r2
;
5085 /* allocate second register */
5089 /* write second register */
5093 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
5095 /* lvalue of scalar type : need to use lvalue type
5096 because of possible cast */
5099 /* compute memory access type */
5100 if (vtop
->r
& VT_LVAL_BYTE
)
5102 else if (vtop
->r
& VT_LVAL_SHORT
)
5104 if (vtop
->r
& VT_LVAL_UNSIGNED
)
5108 /* restore wanted type */
5111 /* one register type load */
5116 #ifdef TCC_TARGET_C67
5117 /* uses register pairs for doubles */
5118 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
5125 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5126 void gv2(int rc1
, int rc2
)
5130 /* generate more generic register first. But VT_JMP or VT_CMP
5131 values must be generated first in all cases to avoid possible
5133 v
= vtop
[0].r
& VT_VALMASK
;
5134 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
5139 /* test if reload is needed for first register */
5140 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
5150 /* test if reload is needed for first register */
5151 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
5157 /* expand long long on stack in two int registers */
5162 u
= vtop
->type
.t
& VT_UNSIGNED
;
5165 vtop
[0].r
= vtop
[-1].r2
;
5166 vtop
[0].r2
= VT_CONST
;
5167 vtop
[-1].r2
= VT_CONST
;
5168 vtop
[0].type
.t
= VT_INT
| u
;
5169 vtop
[-1].type
.t
= VT_INT
| u
;
5172 #ifdef TCC_TARGET_ARM
5173 /* expand long long on stack */
5174 void lexpand_nr(void)
5178 u
= vtop
->type
.t
& VT_UNSIGNED
;
5180 vtop
->r2
= VT_CONST
;
5181 vtop
->type
.t
= VT_INT
| u
;
5182 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
5183 if (v
== VT_CONST
) {
5184 vtop
[-1].c
.ui
= vtop
->c
.ull
;
5185 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
5187 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
5189 vtop
->r
= vtop
[-1].r
;
5190 } else if (v
> VT_CONST
) {
5194 vtop
->r
= vtop
[-1].r2
;
5195 vtop
[-1].r2
= VT_CONST
;
5196 vtop
[-1].type
.t
= VT_INT
| u
;
5200 /* build a long long from two ints */
5203 gv2(RC_INT
, RC_INT
);
5204 vtop
[-1].r2
= vtop
[0].r
;
5205 vtop
[-1].type
.t
= t
;
5209 /* rotate n first stack elements to the bottom
5210 I1 ... In -> I2 ... In I1 [top is right]
5218 for(i
=-n
+1;i
!=0;i
++)
5219 vtop
[i
] = vtop
[i
+1];
5223 /* rotate n first stack elements to the top
5224 I1 ... In -> In I1 ... I(n-1) [top is right]
5232 for(i
= 0;i
< n
- 1; i
++)
5233 vtop
[-i
] = vtop
[-i
- 1];
5237 #ifdef TCC_TARGET_ARM
5238 /* like vrott but in other direction
5239 In ... I1 -> I(n-1) ... I1 In [top is right]
5247 for(i
= n
- 1; i
> 0; i
--)
5248 vtop
[-i
] = vtop
[-i
+ 1];
5253 /* pop stack value */
5257 v
= vtop
->r
& VT_VALMASK
;
5258 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
5259 /* for x86, we need to pop the FP stack */
5260 if (v
== TREG_ST0
&& !nocode_wanted
) {
5261 o(0xd9dd); /* fstp %st(1) */
5264 if (v
== VT_JMP
|| v
== VT_JMPI
) {
5265 /* need to put correct jump if && or || without test */
5271 /* convert stack entry to register and duplicate its value in another
5279 if ((t
& VT_BTYPE
) == VT_LLONG
) {
5286 /* stack: H L L1 H1 */
5294 /* duplicate value */
5299 #ifdef TCC_TARGET_X86_64
5300 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
5310 load(r1
, &sv
); /* move r to r1 */
5312 /* duplicates value */
5317 #ifndef TCC_TARGET_X86_64
5318 /* generate CPU independent (unsigned) long long operations */
5319 void gen_opl(int op
)
5321 int t
, a
, b
, op1
, c
, i
;
5323 unsigned short reg_iret
= REG_IRET
;
5324 unsigned short reg_lret
= REG_LRET
;
5330 func
= TOK___divdi3
;
5333 func
= TOK___udivdi3
;
5336 func
= TOK___moddi3
;
5339 func
= TOK___umoddi3
;
5346 /* call generic long long function */
5347 vpush_global_sym(&func_old_type
, func
);
5352 vtop
->r2
= reg_lret
;
5365 /* stack: L1 H1 L2 H2 */
5370 vtop
[-2] = vtop
[-3];
5373 /* stack: H1 H2 L1 L2 */
5379 /* stack: H1 H2 L1 L2 ML MH */
5382 /* stack: ML MH H1 H2 L1 L2 */
5386 /* stack: ML MH H1 L2 H2 L1 */
5391 /* stack: ML MH M1 M2 */
5394 } else if (op
== '+' || op
== '-') {
5395 /* XXX: add non carry method too (for MIPS or alpha) */
5401 /* stack: H1 H2 (L1 op L2) */
5404 gen_op(op1
+ 1); /* TOK_xxxC2 */
5407 /* stack: H1 H2 (L1 op L2) */
5410 /* stack: (L1 op L2) H1 H2 */
5412 /* stack: (L1 op L2) (H1 op H2) */
5420 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5421 t
= vtop
[-1].type
.t
;
5425 /* stack: L H shift */
5427 /* constant: simpler */
5428 /* NOTE: all comments are for SHL. the other cases are
5429 done by swaping words */
5440 if (op
!= TOK_SAR
) {
5473 /* XXX: should provide a faster fallback on x86 ? */
5476 func
= TOK___ashrdi3
;
5479 func
= TOK___lshrdi3
;
5482 func
= TOK___ashldi3
;
5488 /* compare operations */
5494 /* stack: L1 H1 L2 H2 */
5496 vtop
[-1] = vtop
[-2];
5498 /* stack: L1 L2 H1 H2 */
5501 /* when values are equal, we need to compare low words. since
5502 the jump is inverted, we invert the test too. */
5505 else if (op1
== TOK_GT
)
5507 else if (op1
== TOK_ULT
)
5509 else if (op1
== TOK_UGT
)
5514 if (op1
!= TOK_NE
) {
5518 /* generate non equal test */
5519 /* XXX: NOT PORTABLE yet */
5523 #if defined(TCC_TARGET_I386)
5524 b
= psym(0x850f, 0);
5525 #elif defined(TCC_TARGET_ARM)
5527 o(0x1A000000 | encbranch(ind
, 0, 1));
5528 #elif defined(TCC_TARGET_C67)
5529 error("not implemented");
5531 #error not supported
5535 /* compare low. Always unsigned */
5539 else if (op1
== TOK_LE
)
5541 else if (op1
== TOK_GT
)
5543 else if (op1
== TOK_GE
)
5554 /* handle integer constant optimizations and various machine
5556 void gen_opic(int op
)
5558 int c1
, c2
, t1
, t2
, n
;
5561 typedef unsigned long long U
;
5565 t1
= v1
->type
.t
& VT_BTYPE
;
5566 t2
= v2
->type
.t
& VT_BTYPE
;
5570 else if (v1
->type
.t
& VT_UNSIGNED
)
5577 else if (v2
->type
.t
& VT_UNSIGNED
)
5582 /* currently, we cannot do computations with forward symbols */
5583 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5584 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5587 case '+': l1
+= l2
; break;
5588 case '-': l1
-= l2
; break;
5589 case '&': l1
&= l2
; break;
5590 case '^': l1
^= l2
; break;
5591 case '|': l1
|= l2
; break;
5592 case '*': l1
*= l2
; break;
5599 /* if division by zero, generate explicit division */
5602 error("division by zero in constant");
5606 default: l1
/= l2
; break;
5607 case '%': l1
%= l2
; break;
5608 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
5609 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
5612 case TOK_SHL
: l1
<<= l2
; break;
5613 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
5614 case TOK_SAR
: l1
>>= l2
; break;
5616 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
5617 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
5618 case TOK_EQ
: l1
= l1
== l2
; break;
5619 case TOK_NE
: l1
= l1
!= l2
; break;
5620 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
5621 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
5622 case TOK_LT
: l1
= l1
< l2
; break;
5623 case TOK_GE
: l1
= l1
>= l2
; break;
5624 case TOK_LE
: l1
= l1
<= l2
; break;
5625 case TOK_GT
: l1
= l1
> l2
; break;
5627 case TOK_LAND
: l1
= l1
&& l2
; break;
5628 case TOK_LOR
: l1
= l1
|| l2
; break;
5635 /* if commutative ops, put c2 as constant */
5636 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
5637 op
== '|' || op
== '*')) {
5639 c2
= c1
; //c = c1, c1 = c2, c2 = c;
5640 l2
= l1
; //l = l1, l1 = l2, l2 = l;
5642 /* Filter out NOP operations like x*1, x-0, x&-1... */
5643 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
5646 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
5647 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
5653 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
5654 /* try to use shifts instead of muls or divs */
5655 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
5664 else if (op
== TOK_PDIV
)
5670 } else if (c2
&& (op
== '+' || op
== '-') &&
5671 ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
5672 (VT_CONST
| VT_SYM
) ||
5673 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
5674 /* symbol + constant case */
5681 if (!nocode_wanted
) {
5682 /* call low level op generator */
5683 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
5694 /* generate a floating point operation with constant propagation */
5695 void gen_opif(int op
)
5703 /* currently, we cannot do computations with forward symbols */
5704 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5705 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5707 if (v1
->type
.t
== VT_FLOAT
) {
5710 } else if (v1
->type
.t
== VT_DOUBLE
) {
5718 /* NOTE: we only do constant propagation if finite number (not
5719 NaN or infinity) (ANSI spec) */
5720 if (!ieee_finite(f1
) || !ieee_finite(f2
))
5724 case '+': f1
+= f2
; break;
5725 case '-': f1
-= f2
; break;
5726 case '*': f1
*= f2
; break;
5730 error("division by zero in constant");
5735 /* XXX: also handles tests ? */
5739 /* XXX: overflow test ? */
5740 if (v1
->type
.t
== VT_FLOAT
) {
5742 } else if (v1
->type
.t
== VT_DOUBLE
) {
5750 if (!nocode_wanted
) {
5758 static int pointed_size(CType
*type
)
5761 return type_size(pointed_type(type
), &align
);
5764 static inline int is_null_pointer(SValue
*p
)
5766 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5768 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
5769 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
5772 static inline int is_integer_btype(int bt
)
5774 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
5775 bt
== VT_INT
|| bt
== VT_LLONG
);
5778 /* check types for comparison or substraction of pointers */
5779 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
5781 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
5784 /* null pointers are accepted for all comparisons as gcc */
5785 if (is_null_pointer(p1
) || is_null_pointer(p2
))
5789 bt1
= type1
->t
& VT_BTYPE
;
5790 bt2
= type2
->t
& VT_BTYPE
;
5791 /* accept comparison between pointer and integer with a warning */
5792 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
5793 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
5794 warning("comparison between pointer and integer");
5798 /* both must be pointers or implicit function pointers */
5799 if (bt1
== VT_PTR
) {
5800 type1
= pointed_type(type1
);
5801 } else if (bt1
!= VT_FUNC
)
5802 goto invalid_operands
;
5804 if (bt2
== VT_PTR
) {
5805 type2
= pointed_type(type2
);
5806 } else if (bt2
!= VT_FUNC
) {
5808 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
5810 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5811 (type2
->t
& VT_BTYPE
) == VT_VOID
)
5815 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5816 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5817 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
5818 /* gcc-like error if '-' is used */
5820 goto invalid_operands
;
5822 warning("comparison of distinct pointer types lacks a cast");
5826 /* generic gen_op: handles types problems */
5829 int u
, t1
, t2
, bt1
, bt2
, t
;
5832 t1
= vtop
[-1].type
.t
;
5833 t2
= vtop
[0].type
.t
;
5834 bt1
= t1
& VT_BTYPE
;
5835 bt2
= t2
& VT_BTYPE
;
5837 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5838 /* at least one operand is a pointer */
5839 /* relationnal op: must be both pointers */
5840 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
5841 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5842 /* pointers are handled are unsigned */
5843 #ifdef TCC_TARGET_X86_64
5844 t
= VT_LLONG
| VT_UNSIGNED
;
5846 t
= VT_INT
| VT_UNSIGNED
;
5850 /* if both pointers, then it must be the '-' op */
5851 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
5853 error("cannot use pointers here");
5854 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5855 /* XXX: check that types are compatible */
5856 u
= pointed_size(&vtop
[-1].type
);
5858 /* set to integer type */
5859 #ifdef TCC_TARGET_X86_64
5860 vtop
->type
.t
= VT_LLONG
;
5862 vtop
->type
.t
= VT_INT
;
5867 /* exactly one pointer : must be '+' or '-'. */
5868 if (op
!= '-' && op
!= '+')
5869 error("cannot use pointers here");
5870 /* Put pointer as first operand */
5871 if (bt2
== VT_PTR
) {
5875 type1
= vtop
[-1].type
;
5876 #ifdef TCC_TARGET_X86_64
5881 cval
.ull
= pointed_size(&vtop
[-1].type
);
5882 vsetc(&ctype
, VT_CONST
, &cval
);
5885 /* XXX: cast to int ? (long long case) */
5886 vpushi(pointed_size(&vtop
[-1].type
));
5889 #ifdef CONFIG_TCC_BCHECK
5890 /* if evaluating constant expression, no code should be
5891 generated, so no bound check */
5892 if (do_bounds_check
&& !const_wanted
) {
5893 /* if bounded pointers, we generate a special code to
5900 gen_bounded_ptr_add();
5906 /* put again type if gen_opic() swaped operands */
5909 } else if (is_float(bt1
) || is_float(bt2
)) {
5910 /* compute bigger type and do implicit casts */
5911 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5913 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5918 /* floats can only be used for a few operations */
5919 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
5920 (op
< TOK_ULT
|| op
> TOK_GT
))
5921 error("invalid operands for binary operation");
5923 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5924 /* cast to biggest op */
5926 /* convert to unsigned if it does not fit in a long long */
5927 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5928 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5932 /* integer operations */
5934 /* convert to unsigned if it does not fit in an integer */
5935 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5936 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5939 /* XXX: currently, some unsigned operations are explicit, so
5940 we modify them here */
5941 if (t
& VT_UNSIGNED
) {
5948 else if (op
== TOK_LT
)
5950 else if (op
== TOK_GT
)
5952 else if (op
== TOK_LE
)
5954 else if (op
== TOK_GE
)
5961 /* special case for shifts and long long: we keep the shift as
5963 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
5970 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5971 /* relationnal op: the result is an int */
5972 vtop
->type
.t
= VT_INT
;
5979 #ifndef TCC_TARGET_ARM
5980 /* generic itof for unsigned long long case */
5981 void gen_cvt_itof1(int t
)
5983 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
5984 (VT_LLONG
| VT_UNSIGNED
)) {
5987 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
5988 #if LDOUBLE_SIZE != 8
5989 else if (t
== VT_LDOUBLE
)
5990 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
5993 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
6004 /* generic ftoi for unsigned long long case */
6005 void gen_cvt_ftoi1(int t
)
6009 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
6010 /* not handled natively */
6011 st
= vtop
->type
.t
& VT_BTYPE
;
6013 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
6014 #if LDOUBLE_SIZE != 8
6015 else if (st
== VT_LDOUBLE
)
6016 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
6019 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
6024 vtop
->r2
= REG_LRET
;
6030 /* force char or short cast */
6031 void force_charshort_cast(int t
)
6035 /* XXX: add optimization if lvalue : just change type and offset */
6040 if (t
& VT_UNSIGNED
) {
6041 vpushi((1 << bits
) - 1);
6047 /* result must be signed or the SAR is converted to an SHL
6048 This was not the case when "t" was a signed short
6049 and the last value on the stack was an unsigned int */
6050 vtop
->type
.t
&= ~VT_UNSIGNED
;
6056 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
6057 static void gen_cast(CType
*type
)
6059 int sbt
, dbt
, sf
, df
, c
, p
;
6061 /* special delayed cast for char/short */
6062 /* XXX: in some cases (multiple cascaded casts), it may still
6064 if (vtop
->r
& VT_MUSTCAST
) {
6065 vtop
->r
&= ~VT_MUSTCAST
;
6066 force_charshort_cast(vtop
->type
.t
);
6069 /* bitfields first get cast to ints */
6070 if (vtop
->type
.t
& VT_BITFIELD
) {
6074 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
6075 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
6080 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
6081 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
6083 /* constant case: we can do it now */
6084 /* XXX: in ISOC, cannot do it if error in convert */
6085 if (sbt
== VT_FLOAT
)
6086 vtop
->c
.ld
= vtop
->c
.f
;
6087 else if (sbt
== VT_DOUBLE
)
6088 vtop
->c
.ld
= vtop
->c
.d
;
6091 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
6092 if (sbt
& VT_UNSIGNED
)
6093 vtop
->c
.ld
= vtop
->c
.ull
;
6095 vtop
->c
.ld
= vtop
->c
.ll
;
6097 if (sbt
& VT_UNSIGNED
)
6098 vtop
->c
.ld
= vtop
->c
.ui
;
6100 vtop
->c
.ld
= vtop
->c
.i
;
6103 if (dbt
== VT_FLOAT
)
6104 vtop
->c
.f
= (float)vtop
->c
.ld
;
6105 else if (dbt
== VT_DOUBLE
)
6106 vtop
->c
.d
= (double)vtop
->c
.ld
;
6107 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
6108 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
6109 } else if (sf
&& dbt
== VT_BOOL
) {
6110 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
6113 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
6114 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
6115 vtop
->c
.ll
= vtop
->c
.ull
;
6116 else if (sbt
& VT_UNSIGNED
)
6117 vtop
->c
.ll
= vtop
->c
.ui
;
6118 else if (sbt
!= VT_LLONG
)
6119 vtop
->c
.ll
= vtop
->c
.i
;
6121 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
6122 vtop
->c
.ull
= vtop
->c
.ll
;
6123 else if (dbt
== VT_BOOL
)
6124 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
6125 else if (dbt
!= VT_LLONG
) {
6127 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
6129 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
6132 if(dbt
& VT_UNSIGNED
)
6133 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
6135 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
6138 } else if (p
&& dbt
== VT_BOOL
) {
6141 } else if (!nocode_wanted
) {
6142 /* non constant case: generate code */
6144 /* convert from fp to fp */
6147 /* convert int to fp */
6150 /* convert fp to int */
6151 if (dbt
== VT_BOOL
) {
6155 /* we handle char/short/etc... with generic code */
6156 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
6157 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
6161 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
6162 /* additional cast for char/short... */
6167 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
6168 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
6169 /* scalar to long long */
6170 #ifndef TCC_TARGET_X86_64
6171 /* machine independent conversion */
6173 /* generate high word */
6174 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
6182 /* patch second register */
6183 vtop
[-1].r2
= vtop
->r
;
6187 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
6188 /* x86_64 specific: movslq */
6190 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
6194 } else if (dbt
== VT_BOOL
) {
6195 /* scalar to bool */
6198 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
6199 (dbt
& VT_BTYPE
) == VT_SHORT
) {
6200 if (sbt
== VT_PTR
) {
6201 vtop
->type
.t
= VT_INT
;
6202 warning("nonportable conversion from pointer to char/short");
6204 force_charshort_cast(dbt
);
6205 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
6207 if (sbt
== VT_LLONG
) {
6208 /* from long long: just take low order word */
6212 /* if lvalue and single word type, nothing to do because
6213 the lvalue already contains the real type size (see
6214 VT_LVAL_xxx constants) */
6217 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
6218 /* if we are casting between pointer types,
6219 we must update the VT_LVAL_xxx size */
6220 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
6221 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
6226 /* return type size. Put alignment at 'a' */
6227 static int type_size(CType
*type
, int *a
)
6232 bt
= type
->t
& VT_BTYPE
;
6233 if (bt
== VT_STRUCT
) {
6238 } else if (bt
== VT_PTR
) {
6239 if (type
->t
& VT_ARRAY
) {
6243 ts
= type_size(&s
->type
, a
);
6245 if (ts
< 0 && s
->c
< 0)
6253 } else if (bt
== VT_LDOUBLE
) {
6255 return LDOUBLE_SIZE
;
6256 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
6257 #ifdef TCC_TARGET_I386
6259 #elif defined(TCC_TARGET_ARM)
6269 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
6272 } else if (bt
== VT_SHORT
) {
6276 /* char, void, function, _Bool */
6282 /* return the pointed type of t */
6283 static inline CType
*pointed_type(CType
*type
)
6285 return &type
->ref
->type
;
6288 /* modify type so that its it is a pointer to type. */
6289 static void mk_pointer(CType
*type
)
6292 s
= sym_push(SYM_FIELD
, type
, 0, -1);
6293 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
6297 /* compare function types. OLD functions match any new functions */
6298 static int is_compatible_func(CType
*type1
, CType
*type2
)
6304 if (!is_compatible_types(&s1
->type
, &s2
->type
))
6306 /* check func_call */
6307 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
6309 /* XXX: not complete */
6310 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
6314 while (s1
!= NULL
) {
6317 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
6327 /* return true if type1 and type2 are the same. If unqualified is
6328 true, qualifiers on the types are ignored.
6330 - enums are not checked as gcc __builtin_types_compatible_p ()
6332 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
6336 t1
= type1
->t
& VT_TYPE
;
6337 t2
= type2
->t
& VT_TYPE
;
6339 /* strip qualifiers before comparing */
6340 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6341 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6343 /* XXX: bitfields ? */
6346 /* test more complicated cases */
6347 bt1
= t1
& VT_BTYPE
;
6348 if (bt1
== VT_PTR
) {
6349 type1
= pointed_type(type1
);
6350 type2
= pointed_type(type2
);
6351 return is_compatible_types(type1
, type2
);
6352 } else if (bt1
== VT_STRUCT
) {
6353 return (type1
->ref
== type2
->ref
);
6354 } else if (bt1
== VT_FUNC
) {
6355 return is_compatible_func(type1
, type2
);
6361 /* return true if type1 and type2 are exactly the same (including
6364 static int is_compatible_types(CType
*type1
, CType
*type2
)
6366 return compare_types(type1
,type2
,0);
6369 /* return true if type1 and type2 are the same (ignoring qualifiers).
6371 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
6373 return compare_types(type1
,type2
,1);
6376 /* print a type. If 'varstr' is not NULL, then the variable is also
6377 printed in the type */
6379 /* XXX: add array and function pointers */
6380 void type_to_str(char *buf
, int buf_size
,
6381 CType
*type
, const char *varstr
)
6388 t
= type
->t
& VT_TYPE
;
6391 if (t
& VT_CONSTANT
)
6392 pstrcat(buf
, buf_size
, "const ");
6393 if (t
& VT_VOLATILE
)
6394 pstrcat(buf
, buf_size
, "volatile ");
6395 if (t
& VT_UNSIGNED
)
6396 pstrcat(buf
, buf_size
, "unsigned ");
6426 tstr
= "long double";
6428 pstrcat(buf
, buf_size
, tstr
);
6432 if (bt
== VT_STRUCT
)
6436 pstrcat(buf
, buf_size
, tstr
);
6437 v
= type
->ref
->v
& ~SYM_STRUCT
;
6438 if (v
>= SYM_FIRST_ANOM
)
6439 pstrcat(buf
, buf_size
, "<anonymous>");
6441 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
6445 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
6446 pstrcat(buf
, buf_size
, "(");
6448 while (sa
!= NULL
) {
6449 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
6450 pstrcat(buf
, buf_size
, buf1
);
6453 pstrcat(buf
, buf_size
, ", ");
6455 pstrcat(buf
, buf_size
, ")");
6459 pstrcpy(buf1
, sizeof(buf1
), "*");
6461 pstrcat(buf1
, sizeof(buf1
), varstr
);
6462 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
6466 pstrcat(buf
, buf_size
, " ");
6467 pstrcat(buf
, buf_size
, varstr
);
6472 /* verify type compatibility to store vtop in 'dt' type, and generate
6474 static void gen_assign_cast(CType
*dt
)
6476 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
6477 char buf1
[256], buf2
[256];
6480 st
= &vtop
->type
; /* source type */
6481 dbt
= dt
->t
& VT_BTYPE
;
6482 sbt
= st
->t
& VT_BTYPE
;
6483 if (dt
->t
& VT_CONSTANT
)
6484 warning("assignment of read-only location");
6487 /* special cases for pointers */
6488 /* '0' can also be a pointer */
6489 if (is_null_pointer(vtop
))
6491 /* accept implicit pointer to integer cast with warning */
6492 if (is_integer_btype(sbt
)) {
6493 warning("assignment makes pointer from integer without a cast");
6496 type1
= pointed_type(dt
);
6497 /* a function is implicitely a function pointer */
6498 if (sbt
== VT_FUNC
) {
6499 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
6500 !is_compatible_types(pointed_type(dt
), st
))
6507 type2
= pointed_type(st
);
6508 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
6509 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
6510 /* void * can match anything */
6512 /* exact type match, except for unsigned */
6515 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6516 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6517 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
6518 warning("assignment from incompatible pointer type");
6520 /* check const and volatile */
6521 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
6522 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
6523 warning("assignment discards qualifiers from pointer target type");
6529 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
6530 warning("assignment makes integer from pointer without a cast");
6532 /* XXX: more tests */
6537 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6538 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6539 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
6541 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
6542 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
6543 error("cannot cast '%s' to '%s'", buf1
, buf2
);
6551 /* store vtop in lvalue pushed on stack */
6554 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
6556 ft
= vtop
[-1].type
.t
;
6557 sbt
= vtop
->type
.t
& VT_BTYPE
;
6558 dbt
= ft
& VT_BTYPE
;
6559 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
6560 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
6561 /* optimize char/short casts */
6562 delayed_cast
= VT_MUSTCAST
;
6563 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
6564 /* XXX: factorize */
6565 if (ft
& VT_CONSTANT
)
6566 warning("assignment of read-only location");
6569 if (!(ft
& VT_BITFIELD
))
6570 gen_assign_cast(&vtop
[-1].type
);
6573 if (sbt
== VT_STRUCT
) {
6574 /* if structure, only generate pointer */
6575 /* structure assignment : generate memcpy */
6576 /* XXX: optimize if small size */
6577 if (!nocode_wanted
) {
6578 size
= type_size(&vtop
->type
, &align
);
6582 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
6583 else if(!(align
& 3))
6584 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
6587 vpush_global_sym(&func_old_type
, TOK_memcpy
);
6591 vtop
->type
.t
= VT_INT
;
6595 vtop
->type
.t
= VT_INT
;
6607 /* leave source on stack */
6608 } else if (ft
& VT_BITFIELD
) {
6609 /* bitfield store handling */
6610 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
6611 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6612 /* remove bit field info to avoid loops */
6613 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
6615 /* duplicate source into other register */
6620 if((ft
& VT_BTYPE
) == VT_BOOL
) {
6621 gen_cast(&vtop
[-1].type
);
6622 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
6625 /* duplicate destination */
6627 vtop
[-1] = vtop
[-2];
6629 /* mask and shift source */
6630 if((ft
& VT_BTYPE
) != VT_BOOL
) {
6631 vpushi((1 << bit_size
) - 1);
6636 /* load destination, mask and or with source */
6638 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
6644 /* pop off shifted source from "duplicate source..." above */
6648 #ifdef CONFIG_TCC_BCHECK
6649 /* bound check case */
6650 if (vtop
[-1].r
& VT_MUSTBOUND
) {
6656 if (!nocode_wanted
) {
6660 #ifdef TCC_TARGET_X86_64
6661 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
6666 r
= gv(rc
); /* generate value */
6667 /* if lvalue was saved on stack, must read it */
6668 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
6670 t
= get_reg(RC_INT
);
6671 #ifdef TCC_TARGET_X86_64
6676 sv
.r
= VT_LOCAL
| VT_LVAL
;
6677 sv
.c
.ul
= vtop
[-1].c
.ul
;
6679 vtop
[-1].r
= t
| VT_LVAL
;
6682 #ifndef TCC_TARGET_X86_64
6683 /* two word case handling : store second register at word + 4 */
6684 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
6686 /* convert to int to increment easily */
6687 vtop
->type
.t
= VT_INT
;
6693 /* XXX: it works because r2 is spilled last ! */
6694 store(vtop
->r2
, vtop
- 1);
6699 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6700 vtop
->r
|= delayed_cast
;
6704 /* post defines POST/PRE add. c is the token ++ or -- */
6705 void inc(int post
, int c
)
6708 vdup(); /* save lvalue */
6710 gv_dup(); /* duplicate value */
6715 vpushi(c
- TOK_MID
);
6717 vstore(); /* store value */
6719 vpop(); /* if post op, return saved value */
6722 /* Parse GNUC __attribute__ extension. Currently, the following
6723 extensions are recognized:
6724 - aligned(n) : set data/function alignment.
6725 - packed : force data alignment to 1
6726 - section(x) : generate data/code in this section.
6727 - unused : currently ignored, but may be used someday.
6728 - regparm(n) : pass function parameters in registers (i386 only)
6730 static void parse_attribute(AttributeDef
*ad
)
6734 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
6738 while (tok
!= ')') {
6739 if (tok
< TOK_IDENT
)
6740 expect("attribute name");
6748 expect("section name");
6749 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
6758 if (n
<= 0 || (n
& (n
- 1)) != 0)
6759 error("alignment must be a positive power of two");
6772 /* currently, no need to handle it because tcc does not
6773 track unused objects */
6777 /* currently, no need to handle it because tcc does not
6778 track unused objects */
6783 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
6788 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
6790 #ifdef TCC_TARGET_I386
6800 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
6806 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
6810 FUNC_EXPORT(ad
->func_attr
) = 1;
6813 if (tcc_state
->warn_unsupported
)
6814 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
6815 /* skip parameters */
6817 int parenthesis
= 0;
6821 else if (tok
== ')')
6824 } while (parenthesis
&& tok
!= -1);
6837 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6838 static void struct_decl(CType
*type
, int u
)
6840 int a
, v
, size
, align
, maxalign
, c
, offset
;
6841 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
6842 Sym
*s
, *ss
, *ass
, **ps
;
6846 a
= tok
; /* save decl type */
6851 /* struct already defined ? return it */
6853 expect("struct/union/enum name");
6857 error("invalid type");
6864 /* we put an undefined size for struct/union */
6865 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
6866 s
->r
= 0; /* default alignment is zero as gcc */
6867 /* put struct/union/enum name in type */
6875 error("struct/union/enum already defined");
6876 /* cannot be empty */
6878 /* non empty enums are not allowed */
6879 if (a
== TOK_ENUM
) {
6883 expect("identifier");
6889 /* enum symbols have static storage */
6890 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
6891 ss
->type
.t
|= VT_STATIC
;
6896 /* NOTE: we accept a trailing comma */
6907 while (tok
!= '}') {
6908 parse_btype(&btype
, &ad
);
6914 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
6915 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
6916 expect("identifier");
6917 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
6918 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
6919 error("invalid type for '%s'",
6920 get_tok_str(v
, NULL
));
6924 bit_size
= expr_const();
6925 /* XXX: handle v = 0 case for messages */
6927 error("negative width in bit-field '%s'",
6928 get_tok_str(v
, NULL
));
6929 if (v
&& bit_size
== 0)
6930 error("zero width for bit-field '%s'",
6931 get_tok_str(v
, NULL
));
6933 size
= type_size(&type1
, &align
);
6935 if (align
< ad
.aligned
)
6937 } else if (ad
.packed
) {
6939 } else if (*tcc_state
->pack_stack_ptr
) {
6940 if (align
> *tcc_state
->pack_stack_ptr
)
6941 align
= *tcc_state
->pack_stack_ptr
;
6944 if (bit_size
>= 0) {
6945 bt
= type1
.t
& VT_BTYPE
;
6951 error("bitfields must have scalar type");
6953 if (bit_size
> bsize
) {
6954 error("width of '%s' exceeds its type",
6955 get_tok_str(v
, NULL
));
6956 } else if (bit_size
== bsize
) {
6957 /* no need for bit fields */
6959 } else if (bit_size
== 0) {
6960 /* XXX: what to do if only padding in a
6962 /* zero size: means to pad */
6965 /* we do not have enough room ?
6966 did the type change?
6968 if ((bit_pos
+ bit_size
) > bsize
||
6969 bt
!= prevbt
|| a
== TOK_UNION
)
6972 /* XXX: handle LSB first */
6973 type1
.t
|= VT_BITFIELD
|
6974 (bit_pos
<< VT_STRUCT_SHIFT
) |
6975 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
6976 bit_pos
+= bit_size
;
6982 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
6983 /* add new memory data only if starting
6985 if (lbit_pos
== 0) {
6986 if (a
== TOK_STRUCT
) {
6987 c
= (c
+ align
- 1) & -align
;
6996 if (align
> maxalign
)
7000 printf("add field %s offset=%d",
7001 get_tok_str(v
, NULL
), offset
);
7002 if (type1
.t
& VT_BITFIELD
) {
7003 printf(" pos=%d size=%d",
7004 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
7005 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
7010 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
7012 while ((ass
= ass
->next
) != NULL
) {
7013 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
7018 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
7022 if (tok
== ';' || tok
== TOK_EOF
)
7029 /* store size and alignment */
7030 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
7036 /* return 0 if no type declaration. otherwise, return the basic type
7039 static int parse_btype(CType
*type
, AttributeDef
*ad
)
7041 int t
, u
, type_found
, typespec_found
, typedef_found
;
7045 memset(ad
, 0, sizeof(AttributeDef
));
7053 /* currently, we really ignore extension */
7063 if ((t
& VT_BTYPE
) != 0)
7064 error("too many basic types");
7080 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
7081 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
7082 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
7083 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
7097 if ((t
& VT_BTYPE
) == VT_LONG
) {
7098 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
7105 struct_decl(&type1
, VT_ENUM
);
7108 type
->ref
= type1
.ref
;
7112 struct_decl(&type1
, VT_STRUCT
);
7115 /* type modifiers */
7168 /* GNUC attribute */
7169 case TOK_ATTRIBUTE1
:
7170 case TOK_ATTRIBUTE2
:
7171 parse_attribute(ad
);
7178 parse_expr_type(&type1
);
7181 if (typespec_found
|| typedef_found
)
7184 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
7187 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
7188 type
->ref
= s
->type
.ref
;
7196 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
7197 error("signed and unsigned modifier");
7198 if (tcc_state
->char_is_unsigned
) {
7199 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
7204 /* long is never used as type */
7205 if ((t
& VT_BTYPE
) == VT_LONG
)
7206 #ifndef TCC_TARGET_X86_64
7207 t
= (t
& ~VT_BTYPE
) | VT_INT
;
7209 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
7215 /* convert a function parameter type (array to pointer and function to
7216 function pointer) */
7217 static inline void convert_parameter_type(CType
*pt
)
7219 /* remove const and volatile qualifiers (XXX: const could be used
7220 to indicate a const function parameter */
7221 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7222 /* array must be transformed to pointer according to ANSI C */
7224 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
7229 static void post_type(CType
*type
, AttributeDef
*ad
)
7231 int n
, l
, t1
, arg_size
, align
;
7232 Sym
**plast
, *s
, *first
;
7237 /* function declaration */
7245 /* read param name and compute offset */
7246 if (l
!= FUNC_OLD
) {
7247 if (!parse_btype(&pt
, &ad1
)) {
7249 error("invalid type");
7256 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
7258 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
7259 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
7260 error("parameter declared as void");
7261 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
7266 expect("identifier");
7270 convert_parameter_type(&pt
);
7271 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
7277 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
7284 /* if no parameters, then old type prototype */
7288 t1
= type
->t
& VT_STORAGE
;
7289 /* NOTE: const is ignored in returned type as it has a special
7290 meaning in gcc / C++ */
7291 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
7292 post_type(type
, ad
);
7293 /* we push a anonymous symbol which will contain the function prototype */
7294 FUNC_ARGS(ad
->func_attr
) = arg_size
;
7295 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
7297 type
->t
= t1
| VT_FUNC
;
7299 } else if (tok
== '[') {
7300 /* array definition */
7306 error("invalid array size");
7309 /* parse next post type */
7310 t1
= type
->t
& VT_STORAGE
;
7311 type
->t
&= ~VT_STORAGE
;
7312 post_type(type
, ad
);
7314 /* we push a anonymous symbol which will contain the array
7316 s
= sym_push(SYM_FIELD
, type
, 0, n
);
7317 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
7322 /* Parse a type declaration (except basic type), and return the type
7323 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7324 expected. 'type' should contain the basic type. 'ad' is the
7325 attribute definition of the basic type. It can be modified by
7328 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
7331 CType type1
, *type2
;
7334 while (tok
== '*') {
7342 qualifiers
|= VT_CONSTANT
;
7347 qualifiers
|= VT_VOLATILE
;
7355 type
->t
|= qualifiers
;
7358 /* XXX: clarify attribute handling */
7359 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7360 parse_attribute(ad
);
7362 /* recursive type */
7363 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7364 type1
.t
= 0; /* XXX: same as int */
7367 /* XXX: this is not correct to modify 'ad' at this point, but
7368 the syntax is not clear */
7369 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7370 parse_attribute(ad
);
7371 type_decl(&type1
, ad
, v
, td
);
7374 /* type identifier */
7375 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
7379 if (!(td
& TYPE_ABSTRACT
))
7380 expect("identifier");
7384 post_type(type
, ad
);
7385 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7386 parse_attribute(ad
);
7389 /* append type at the end of type1 */
7402 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7403 static int lvalue_type(int t
)
7408 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
7410 else if (bt
== VT_SHORT
)
7414 if (t
& VT_UNSIGNED
)
7415 r
|= VT_LVAL_UNSIGNED
;
7419 /* indirection with full error checking and bound check */
7420 static void indir(void)
7422 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
7423 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
7427 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
7429 vtop
->type
= *pointed_type(&vtop
->type
);
7430 /* Arrays and functions are never lvalues */
7431 if (!(vtop
->type
.t
& VT_ARRAY
)
7432 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7433 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7434 /* if bound checking, the referenced pointer must be checked */
7435 if (do_bounds_check
)
7436 vtop
->r
|= VT_MUSTBOUND
;
7440 /* pass a parameter to a function and do type checking and casting */
7441 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
7446 func_type
= func
->c
;
7447 if (func_type
== FUNC_OLD
||
7448 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
7449 /* default casting : only need to convert float to double */
7450 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
7454 } else if (arg
== NULL
) {
7455 error("too many arguments to function");
7458 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
7459 gen_assign_cast(&type
);
7463 /* parse an expression of the form '(type)' or '(expr)' and return its
7465 static void parse_expr_type(CType
*type
)
7471 if (parse_btype(type
, &ad
)) {
7472 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7479 static void parse_type(CType
*type
)
7484 if (!parse_btype(type
, &ad
)) {
7487 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7490 static void vpush_tokc(int t
)
7494 vsetc(&type
, VT_CONST
, &tokc
);
7497 static void unary(void)
7499 int n
, t
, align
, size
, r
;
7504 /* XXX: GCC 2.95.3 does not generate a table although it should be
7518 vpush_tokc(VT_INT
| VT_UNSIGNED
);
7522 vpush_tokc(VT_LLONG
);
7526 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
7530 vpush_tokc(VT_FLOAT
);
7534 vpush_tokc(VT_DOUBLE
);
7538 vpush_tokc(VT_LDOUBLE
);
7541 case TOK___FUNCTION__
:
7543 goto tok_identifier
;
7549 /* special function name identifier */
7550 len
= strlen(funcname
) + 1;
7551 /* generate char[len] type */
7556 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
7557 ptr
= section_ptr_add(data_section
, len
);
7558 memcpy(ptr
, funcname
, len
);
7563 #ifdef TCC_TARGET_PE
7564 t
= VT_SHORT
| VT_UNSIGNED
;
7570 /* string parsing */
7573 if (tcc_state
->warn_write_strings
)
7578 memset(&ad
, 0, sizeof(AttributeDef
));
7579 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
7584 if (parse_btype(&type
, &ad
)) {
7585 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
7587 /* check ISOC99 compound literal */
7589 /* data is allocated locally by default */
7594 /* all except arrays are lvalues */
7595 if (!(type
.t
& VT_ARRAY
))
7596 r
|= lvalue_type(type
.t
);
7597 memset(&ad
, 0, sizeof(AttributeDef
));
7598 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
7603 } else if (tok
== '{') {
7604 /* save all registers */
7606 /* statement expression : we do not accept break/continue
7607 inside as GCC does */
7608 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
7623 /* functions names must be treated as function pointers,
7624 except for unary '&' and sizeof. Since we consider that
7625 functions are not lvalues, we only have to handle it
7626 there and in function calls. */
7627 /* arrays can also be used although they are not lvalues */
7628 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
7629 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
7631 mk_pointer(&vtop
->type
);
7637 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
7639 boolean
.t
= VT_BOOL
;
7641 vtop
->c
.i
= !vtop
->c
.i
;
7642 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
7643 vtop
->c
.i
= vtop
->c
.i
^ 1;
7646 vseti(VT_JMP
, gtst(1, 0));
7657 /* in order to force cast, we add zero */
7659 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
7660 error("pointer not accepted for unary plus");
7670 parse_expr_type(&type
);
7674 size
= type_size(&type
, &align
);
7675 if (t
== TOK_SIZEOF
) {
7677 error("sizeof applied to an incomplete type");
7682 vtop
->type
.t
|= VT_UNSIGNED
;
7685 case TOK_builtin_types_compatible_p
:
7694 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7695 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7696 vpushi(is_compatible_types(&type1
, &type2
));
7699 case TOK_builtin_constant_p
:
7701 int saved_nocode_wanted
, res
;
7704 saved_nocode_wanted
= nocode_wanted
;
7707 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
7709 nocode_wanted
= saved_nocode_wanted
;
7714 case TOK_builtin_frame_address
:
7719 if (tok
!= TOK_CINT
) {
7720 error("__builtin_frame_address only takes integers");
7723 error("TCC only supports __builtin_frame_address(0)");
7729 vset(&type
, VT_LOCAL
, 0);
7747 goto tok_identifier
;
7749 /* allow to take the address of a label */
7750 if (tok
< TOK_UIDENT
)
7751 expect("label identifier");
7752 s
= label_find(tok
);
7754 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
7756 if (s
->r
== LABEL_DECLARED
)
7757 s
->r
= LABEL_FORWARD
;
7760 s
->type
.t
= VT_VOID
;
7761 mk_pointer(&s
->type
);
7762 s
->type
.t
|= VT_STATIC
;
7764 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
7773 expect("identifier");
7777 error("'%s' undeclared", get_tok_str(t
, NULL
));
7778 /* for simple function calls, we tolerate undeclared
7779 external reference to int() function */
7780 if (tcc_state
->warn_implicit_function_declaration
)
7781 warning("implicit declaration of function '%s'",
7782 get_tok_str(t
, NULL
));
7783 s
= external_global_sym(t
, &func_old_type
, 0);
7785 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
7786 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
7787 /* if referencing an inline function, then we generate a
7788 symbol to it if not already done. It will have the
7789 effect to generate code for it at the end of the
7790 compilation unit. Inline function as always
7791 generated in the text section. */
7793 put_extern_sym(s
, text_section
, 0, 0);
7794 r
= VT_SYM
| VT_CONST
;
7798 vset(&s
->type
, r
, s
->c
);
7799 /* if forward reference, we must point to s */
7800 if (vtop
->r
& VT_SYM
) {
7807 /* post operations */
7809 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
7812 } else if (tok
== '.' || tok
== TOK_ARROW
) {
7814 if (tok
== TOK_ARROW
)
7819 /* expect pointer on structure */
7820 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
7821 expect("struct or union");
7825 while ((s
= s
->next
) != NULL
) {
7830 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
7831 /* add field offset to pointer */
7832 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
7835 /* change type to field type, and set to lvalue */
7836 vtop
->type
= s
->type
;
7837 /* an array is never an lvalue */
7838 if (!(vtop
->type
.t
& VT_ARRAY
)) {
7839 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7840 /* if bound checking, the referenced pointer must be checked */
7841 if (do_bounds_check
)
7842 vtop
->r
|= VT_MUSTBOUND
;
7845 } else if (tok
== '[') {
7851 } else if (tok
== '(') {
7857 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7858 /* pointer test (no array accepted) */
7859 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
7860 vtop
->type
= *pointed_type(&vtop
->type
);
7861 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7865 expect("function pointer");
7868 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
7870 /* get return type */
7873 sa
= s
->next
; /* first parameter */
7876 /* compute first implicit argument if a structure is returned */
7877 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
7878 /* get some space for the returned structure */
7879 size
= type_size(&s
->type
, &align
);
7880 loc
= (loc
- size
) & -align
;
7882 ret
.r
= VT_LOCAL
| VT_LVAL
;
7883 /* pass it as 'int' to avoid structure arg passing
7885 vseti(VT_LOCAL
, loc
);
7890 /* return in register */
7891 if (is_float(ret
.type
.t
)) {
7894 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
7903 gfunc_param_typed(s
, sa
);
7913 error("too few arguments to function");
7915 if (!nocode_wanted
) {
7916 gfunc_call(nb_args
);
7918 vtop
-= (nb_args
+ 1);
7921 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
7929 static void uneq(void)
7935 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
7936 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
7937 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
7952 static void expr_prod(void)
7957 while (tok
== '*' || tok
== '/' || tok
== '%') {
7965 static void expr_sum(void)
7970 while (tok
== '+' || tok
== '-') {
7978 static void expr_shift(void)
7983 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
7991 static void expr_cmp(void)
7996 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
7997 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
8005 static void expr_cmpeq(void)
8010 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
8018 static void expr_and(void)
8021 while (tok
== '&') {
8028 static void expr_xor(void)
8031 while (tok
== '^') {
8038 static void expr_or(void)
8041 while (tok
== '|') {
8048 /* XXX: fix this mess */
8049 static void expr_land_const(void)
8052 while (tok
== TOK_LAND
) {
8059 /* XXX: fix this mess */
8060 static void expr_lor_const(void)
8063 while (tok
== TOK_LOR
) {
8070 /* only used if non constant */
8071 static void expr_land(void)
8076 if (tok
== TOK_LAND
) {
8081 if (tok
!= TOK_LAND
) {
8091 static void expr_lor(void)
8096 if (tok
== TOK_LOR
) {
8101 if (tok
!= TOK_LOR
) {
8111 /* XXX: better constant handling */
8112 static void expr_eq(void)
8114 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
8116 CType type
, type1
, type2
;
8123 boolean
.t
= VT_BOOL
;
8129 if (tok
!= ':' || !gnu_ext
) {
8144 if (vtop
!= vstack
) {
8145 /* needed to avoid having different registers saved in
8147 if (is_float(vtop
->type
.t
)) {
8149 #ifdef TCC_TARGET_X86_64
8150 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
8160 if (tok
== ':' && gnu_ext
) {
8168 sv
= *vtop
; /* save value to handle it later */
8169 vtop
--; /* no vpop so that FP stack is not flushed */
8177 bt1
= t1
& VT_BTYPE
;
8179 bt2
= t2
& VT_BTYPE
;
8180 /* cast operands to correct type according to ISOC rules */
8181 if (is_float(bt1
) || is_float(bt2
)) {
8182 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
8183 type
.t
= VT_LDOUBLE
;
8184 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
8189 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
8190 /* cast to biggest op */
8192 /* convert to unsigned if it does not fit in a long long */
8193 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
8194 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
8195 type
.t
|= VT_UNSIGNED
;
8196 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
8197 /* XXX: test pointer compatibility */
8199 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
8200 /* XXX: test function pointer compatibility */
8202 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
8203 /* XXX: test structure compatibility */
8205 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
8206 /* NOTE: as an extension, we accept void on only one side */
8209 /* integer operations */
8211 /* convert to unsigned if it does not fit in an integer */
8212 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
8213 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
8214 type
.t
|= VT_UNSIGNED
;
8217 /* now we convert second operand */
8219 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8222 if (is_float(type
.t
)) {
8224 #ifdef TCC_TARGET_X86_64
8225 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
8229 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
8230 /* for long longs, we use fixed registers to avoid having
8231 to handle a complicated move */
8236 /* this is horrible, but we must also convert first
8240 /* put again first value and cast it */
8243 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8253 static void gexpr(void)
8264 /* parse an expression and return its type without any side effect. */
8265 static void expr_type(CType
*type
)
8267 int saved_nocode_wanted
;
8269 saved_nocode_wanted
= nocode_wanted
;
8274 nocode_wanted
= saved_nocode_wanted
;
8277 /* parse a unary expression and return its type without any side
8279 static void unary_type(CType
*type
)
8291 /* parse a constant expression and return value in vtop. */
8292 static void expr_const1(void)
8301 /* parse an integer constant and return its value. */
8302 static int expr_const(void)
8306 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
8307 expect("constant expression");
8313 /* return the label token if current token is a label, otherwise
8315 static int is_label(void)
8319 /* fast test first */
8320 if (tok
< TOK_UIDENT
)
8322 /* no need to save tokc because tok is an identifier */
8329 unget_tok(last_tok
);
8334 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
8335 int case_reg
, int is_expr
)
8340 /* generate line number info */
8342 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
8343 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
8345 last_line_num
= file
->line_num
;
8349 /* default return value is (void) */
8351 vtop
->type
.t
= VT_VOID
;
8354 if (tok
== TOK_IF
) {
8361 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8363 if (c
== TOK_ELSE
) {
8367 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8368 gsym(d
); /* patch else jmp */
8371 } else if (tok
== TOK_WHILE
) {
8379 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8383 } else if (tok
== '{') {
8387 /* record local declaration stack position */
8389 llabel
= local_label_stack
;
8390 /* handle local labels declarations */
8391 if (tok
== TOK_LABEL
) {
8394 if (tok
< TOK_UIDENT
)
8395 expect("label identifier");
8396 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
8406 while (tok
!= '}') {
8411 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8414 /* pop locally defined labels */
8415 label_pop(&local_label_stack
, llabel
);
8416 /* pop locally defined symbols */
8418 /* XXX: this solution makes only valgrind happy...
8419 triggered by gcc.c-torture/execute/20000917-1.c */
8421 switch(vtop
->type
.t
& VT_BTYPE
) {
8426 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
8428 error("unsupported expression type");
8431 sym_pop(&local_stack
, s
);
8433 } else if (tok
== TOK_RETURN
) {
8437 gen_assign_cast(&func_vt
);
8438 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
8440 /* if returning structure, must copy it to implicit
8441 first pointer arg location */
8444 size
= type_size(&func_vt
,&align
);
8447 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
8451 loc
= (loc
- size
) & -4;
8454 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
8457 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
8459 vtop
->type
= int_type
;
8465 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
8468 /* copy structure value to pointer */
8473 } else if (is_float(func_vt
.t
)) {
8478 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
8481 rsym
= gjmp(rsym
); /* jmp */
8482 } else if (tok
== TOK_BREAK
) {
8485 error("cannot break");
8486 *bsym
= gjmp(*bsym
);
8489 } else if (tok
== TOK_CONTINUE
) {
8492 error("cannot continue");
8493 *csym
= gjmp(*csym
);
8496 } else if (tok
== TOK_FOR
) {
8523 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8528 if (tok
== TOK_DO
) {
8533 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8544 if (tok
== TOK_SWITCH
) {
8548 /* XXX: other types than integer */
8549 case_reg
= gv(RC_INT
);
8553 b
= gjmp(0); /* jump to first case */
8555 block(&a
, csym
, &b
, &c
, case_reg
, 0);
8556 /* if no default, jmp after switch */
8564 if (tok
== TOK_CASE
) {
8571 if (gnu_ext
&& tok
== TOK_DOTS
) {
8575 warning("empty case range");
8577 /* since a case is like a label, we must skip it with a jmp */
8584 *case_sym
= gtst(1, 0);
8587 *case_sym
= gtst(1, 0);
8591 *case_sym
= gtst(1, *case_sym
);
8596 goto block_after_label
;
8598 if (tok
== TOK_DEFAULT
) {
8604 error("too many 'default'");
8607 goto block_after_label
;
8609 if (tok
== TOK_GOTO
) {
8611 if (tok
== '*' && gnu_ext
) {
8615 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
8618 } else if (tok
>= TOK_UIDENT
) {
8619 s
= label_find(tok
);
8620 /* put forward definition if needed */
8622 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
8624 if (s
->r
== LABEL_DECLARED
)
8625 s
->r
= LABEL_FORWARD
;
8627 /* label already defined */
8628 if (s
->r
& LABEL_FORWARD
)
8629 s
->next
= (void *)gjmp((long)s
->next
);
8631 gjmp_addr((long)s
->next
);
8634 expect("label identifier");
8637 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
8645 if (s
->r
== LABEL_DEFINED
)
8646 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
8647 gsym((long)s
->next
);
8648 s
->r
= LABEL_DEFINED
;
8650 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
8652 s
->next
= (void *)ind
;
8653 /* we accept this, but it is a mistake */
8656 warning("deprecated use of label at end of compound statement");
8660 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8663 /* expression case */
8678 /* t is the array or struct type. c is the array or struct
8679 address. cur_index/cur_field is the pointer to the current
8680 value. 'size_only' is true if only size info is needed (only used
8682 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
8683 int *cur_index
, Sym
**cur_field
,
8687 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
8693 if (gnu_ext
&& (l
= is_label()) != 0)
8695 while (tok
== '[' || tok
== '.') {
8697 if (!(type
->t
& VT_ARRAY
))
8698 expect("array type");
8701 index
= expr_const();
8702 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
8703 expect("invalid index");
8704 if (tok
== TOK_DOTS
&& gnu_ext
) {
8706 index_last
= expr_const();
8707 if (index_last
< 0 ||
8708 (s
->c
>= 0 && index_last
>= s
->c
) ||
8710 expect("invalid index");
8716 *cur_index
= index_last
;
8717 type
= pointed_type(type
);
8718 elem_size
= type_size(type
, &align
);
8719 c
+= index
* elem_size
;
8720 /* NOTE: we only support ranges for last designator */
8721 nb_elems
= index_last
- index
+ 1;
8722 if (nb_elems
!= 1) {
8731 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
8732 expect("struct/union type");
8745 /* XXX: fix this mess by using explicit storage field */
8747 type1
.t
|= (type
->t
& ~VT_TYPE
);
8761 if (type
->t
& VT_ARRAY
) {
8763 type
= pointed_type(type
);
8764 c
+= index
* type_size(type
, &align
);
8768 error("too many field init");
8769 /* XXX: fix this mess by using explicit storage field */
8771 type1
.t
|= (type
->t
& ~VT_TYPE
);
8776 decl_initializer(type
, sec
, c
, 0, size_only
);
8778 /* XXX: make it more general */
8779 if (!size_only
&& nb_elems
> 1) {
8780 unsigned long c_end
;
8785 error("range init not supported yet for dynamic storage");
8786 c_end
= c
+ nb_elems
* elem_size
;
8787 if (c_end
> sec
->data_allocated
)
8788 section_realloc(sec
, c_end
);
8789 src
= sec
->data
+ c
;
8791 for(i
= 1; i
< nb_elems
; i
++) {
8793 memcpy(dst
, src
, elem_size
);
8799 #define EXPR_CONST 1
8802 /* store a value or an expression directly in global data or in local array */
8803 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
8804 int v
, int expr_type
)
8806 int saved_global_expr
, bt
, bit_pos
, bit_size
;
8808 unsigned long long bit_mask
;
8816 /* compound literals must be allocated globally in this case */
8817 saved_global_expr
= global_expr
;
8820 global_expr
= saved_global_expr
;
8821 /* NOTE: symbols are accepted */
8822 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
8823 error("initializer element is not constant");
8831 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
8834 /* XXX: not portable */
8835 /* XXX: generate error if incorrect relocation */
8836 gen_assign_cast(&dtype
);
8837 bt
= type
->t
& VT_BTYPE
;
8838 /* we'll write at most 12 bytes */
8839 if (c
+ 12 > sec
->data_allocated
) {
8840 section_realloc(sec
, c
+ 12);
8842 ptr
= sec
->data
+ c
;
8843 /* XXX: make code faster ? */
8844 if (!(type
->t
& VT_BITFIELD
)) {
8849 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
8850 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
8851 bit_mask
= (1LL << bit_size
) - 1;
8853 if ((vtop
->r
& VT_SYM
) &&
8859 (bt
== VT_INT
&& bit_size
!= 32)))
8860 error("initializer element is not computable at load time");
8863 vtop
->c
.i
= (vtop
->c
.i
!= 0);
8865 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8868 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8871 *(double *)ptr
= vtop
->c
.d
;
8874 *(long double *)ptr
= vtop
->c
.ld
;
8877 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
8880 if (vtop
->r
& VT_SYM
) {
8881 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
8883 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8888 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
8895 /* put zeros for variable based init */
8896 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
8899 /* nothing to do because globals are already set to zero */
8901 vpush_global_sym(&func_old_type
, TOK_memset
);
8909 /* 't' contains the type and storage info. 'c' is the offset of the
8910 object in section 'sec'. If 'sec' is NULL, it means stack based
8911 allocation. 'first' is true if array '{' must be read (multi
8912 dimension implicit array init handling). 'size_only' is true if
8913 size only evaluation is wanted (only for arrays). */
8914 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
8915 int first
, int size_only
)
8917 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
8918 int size1
, align1
, expr_type
;
8922 if (type
->t
& VT_ARRAY
) {
8926 t1
= pointed_type(type
);
8927 size1
= type_size(t1
, &align1
);
8930 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
8936 /* only parse strings here if correct type (otherwise: handle
8937 them as ((w)char *) expressions */
8938 if ((tok
== TOK_LSTR
&&
8939 #ifdef TCC_TARGET_PE
8940 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
8942 (t1
->t
& VT_BTYPE
) == VT_INT
8944 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
8945 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8950 /* compute maximum number of chars wanted */
8952 cstr_len
= cstr
->size
;
8954 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
8957 if (n
>= 0 && nb
> (n
- array_length
))
8958 nb
= n
- array_length
;
8961 warning("initializer-string for array is too long");
8962 /* in order to go faster for common case (char
8963 string in global variable, we handle it
8965 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
8966 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
8970 ch
= ((unsigned char *)cstr
->data
)[i
];
8972 ch
= ((nwchar_t
*)cstr
->data
)[i
];
8973 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
8981 /* only add trailing zero if enough storage (no
8982 warning in this case since it is standard) */
8983 if (n
< 0 || array_length
< n
) {
8985 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
8991 while (tok
!= '}') {
8992 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
8993 if (n
>= 0 && index
>= n
)
8994 error("index too large");
8995 /* must put zero in holes (note that doing it that way
8996 ensures that it even works with designators) */
8997 if (!size_only
&& array_length
< index
) {
8998 init_putz(t1
, sec
, c
+ array_length
* size1
,
8999 (index
- array_length
) * size1
);
9002 if (index
> array_length
)
9003 array_length
= index
;
9004 /* special test for multi dimensional arrays (may not
9005 be strictly correct if designators are used at the
9007 if (index
>= n
&& no_oblock
)
9016 /* put zeros at the end */
9017 if (!size_only
&& n
>= 0 && array_length
< n
) {
9018 init_putz(t1
, sec
, c
+ array_length
* size1
,
9019 (n
- array_length
) * size1
);
9021 /* patch type size if needed */
9023 s
->c
= array_length
;
9024 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
9025 (sec
|| !first
|| tok
== '{')) {
9028 /* NOTE: the previous test is a specific case for automatic
9029 struct/union init */
9030 /* XXX: union needs only one init */
9032 /* XXX: this test is incorrect for local initializers
9033 beginning with ( without {. It would be much more difficult
9034 to do it correctly (ideally, the expression parser should
9035 be used in all cases) */
9041 while (tok
== '(') {
9045 if (!parse_btype(&type1
, &ad1
))
9047 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
9049 if (!is_assignable_types(type
, &type1
))
9050 error("invalid type for cast");
9055 if (first
|| tok
== '{') {
9064 while (tok
!= '}') {
9065 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
9067 if (!size_only
&& array_length
< index
) {
9068 init_putz(type
, sec
, c
+ array_length
,
9069 index
- array_length
);
9071 index
= index
+ type_size(&f
->type
, &align1
);
9072 if (index
> array_length
)
9073 array_length
= index
;
9075 if (no_oblock
&& f
== NULL
)
9081 /* put zeros at the end */
9082 if (!size_only
&& array_length
< n
) {
9083 init_putz(type
, sec
, c
+ array_length
,
9092 } else if (tok
== '{') {
9094 decl_initializer(type
, sec
, c
, first
, size_only
);
9096 } else if (size_only
) {
9097 /* just skip expression */
9099 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
9103 else if (tok
== ')')
9108 /* currently, we always use constant expression for globals
9109 (may change for scripting case) */
9110 expr_type
= EXPR_CONST
;
9112 expr_type
= EXPR_ANY
;
9113 init_putv(type
, sec
, c
, 0, expr_type
);
9117 /* parse an initializer for type 't' if 'has_init' is non zero, and
9118 allocate space in local or global data space ('r' is either
9119 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
9120 variable 'v' of scope 'scope' is declared before initializers are
9121 parsed. If 'v' is zero, then a reference to the new object is put
9122 in the value stack. If 'has_init' is 2, a special parsing is done
9123 to handle string constants. */
9124 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
9125 int has_init
, int v
, int scope
)
9127 int size
, align
, addr
, data_offset
;
9129 ParseState saved_parse_state
;
9130 TokenString init_str
;
9133 size
= type_size(type
, &align
);
9134 /* If unknown size, we must evaluate it before
9135 evaluating initializers because
9136 initializers can generate global data too
9137 (e.g. string pointers or ISOC99 compound
9138 literals). It also simplifies local
9139 initializers handling */
9140 tok_str_new(&init_str
);
9143 error("unknown type size");
9144 /* get all init string */
9145 if (has_init
== 2) {
9146 /* only get strings */
9147 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
9148 tok_str_add_tok(&init_str
);
9153 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
9155 error("unexpected end of file in initializer");
9156 tok_str_add_tok(&init_str
);
9159 else if (tok
== '}') {
9167 tok_str_add(&init_str
, -1);
9168 tok_str_add(&init_str
, 0);
9171 save_parse_state(&saved_parse_state
);
9173 macro_ptr
= init_str
.str
;
9175 decl_initializer(type
, NULL
, 0, 1, 1);
9176 /* prepare second initializer parsing */
9177 macro_ptr
= init_str
.str
;
9180 /* if still unknown size, error */
9181 size
= type_size(type
, &align
);
9183 error("unknown type size");
9185 /* take into account specified alignment if bigger */
9187 if (ad
->aligned
> align
)
9188 align
= ad
->aligned
;
9189 } else if (ad
->packed
) {
9192 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
9194 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
9196 loc
= (loc
- size
) & -align
;
9198 /* handles bounds */
9199 /* XXX: currently, since we do only one pass, we cannot track
9200 '&' operators, so we add only arrays */
9201 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
9202 unsigned long *bounds_ptr
;
9203 /* add padding between regions */
9205 /* then add local bound info */
9206 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
9207 bounds_ptr
[0] = addr
;
9208 bounds_ptr
[1] = size
;
9211 /* local variable */
9212 sym_push(v
, type
, r
, addr
);
9214 /* push local reference */
9215 vset(type
, r
, addr
);
9221 if (v
&& scope
== VT_CONST
) {
9222 /* see if the symbol was already defined */
9225 if (!is_compatible_types(&sym
->type
, type
))
9226 error("incompatible types for redefinition of '%s'",
9227 get_tok_str(v
, NULL
));
9228 if (sym
->type
.t
& VT_EXTERN
) {
9229 /* if the variable is extern, it was not allocated */
9230 sym
->type
.t
&= ~VT_EXTERN
;
9231 /* set array size if it was ommited in extern
9233 if ((sym
->type
.t
& VT_ARRAY
) &&
9234 sym
->type
.ref
->c
< 0 &&
9236 sym
->type
.ref
->c
= type
->ref
->c
;
9238 /* we accept several definitions of the same
9239 global variable. this is tricky, because we
9240 must play with the SHN_COMMON type of the symbol */
9241 /* XXX: should check if the variable was already
9242 initialized. It is incorrect to initialized it
9244 /* no init data, we won't add more to the symbol */
9251 /* allocate symbol in corresponding section */
9256 else if (tcc_state
->nocommon
)
9260 data_offset
= sec
->data_offset
;
9261 data_offset
= (data_offset
+ align
- 1) & -align
;
9263 /* very important to increment global pointer at this time
9264 because initializers themselves can create new initializers */
9265 data_offset
+= size
;
9266 /* add padding if bound check */
9267 if (do_bounds_check
)
9269 sec
->data_offset
= data_offset
;
9270 /* allocate section space to put the data */
9271 if (sec
->sh_type
!= SHT_NOBITS
&&
9272 data_offset
> sec
->data_allocated
)
9273 section_realloc(sec
, data_offset
);
9274 /* align section if needed */
9275 if (align
> sec
->sh_addralign
)
9276 sec
->sh_addralign
= align
;
9278 addr
= 0; /* avoid warning */
9282 if (scope
!= VT_CONST
|| !sym
) {
9283 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
9285 /* update symbol definition */
9287 put_extern_sym(sym
, sec
, addr
, size
);
9290 /* put a common area */
9291 put_extern_sym(sym
, NULL
, align
, size
);
9292 /* XXX: find a nicer way */
9293 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
9294 esym
->st_shndx
= SHN_COMMON
;
9299 /* push global reference */
9300 sym
= get_sym_ref(type
, sec
, addr
, size
);
9302 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
9306 /* handles bounds now because the symbol must be defined
9307 before for the relocation */
9308 if (do_bounds_check
) {
9309 unsigned long *bounds_ptr
;
9311 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
9312 /* then add global bound info */
9313 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
9314 bounds_ptr
[0] = 0; /* relocated */
9315 bounds_ptr
[1] = size
;
9319 decl_initializer(type
, sec
, addr
, 1, 0);
9320 /* restore parse state if needed */
9322 tok_str_free(init_str
.str
);
9323 restore_parse_state(&saved_parse_state
);
9329 void put_func_debug(Sym
*sym
)
9334 /* XXX: we put here a dummy type */
9335 snprintf(buf
, sizeof(buf
), "%s:%c1",
9336 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
9337 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
9338 cur_text_section
, sym
->c
);
9339 /* //gr gdb wants a line at the function */
9340 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
9345 /* parse an old style function declaration list */
9346 /* XXX: check multiple parameter */
9347 static void func_decl_list(Sym
*func_sym
)
9354 /* parse each declaration */
9355 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
9356 if (!parse_btype(&btype
, &ad
))
9357 expect("declaration list");
9358 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9359 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9361 /* we accept no variable after */
9365 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9366 /* find parameter in function parameter list */
9369 if ((s
->v
& ~SYM_FIELD
) == v
)
9373 error("declaration for parameter '%s' but no such parameter",
9374 get_tok_str(v
, NULL
));
9376 /* check that no storage specifier except 'register' was given */
9377 if (type
.t
& VT_STORAGE
)
9378 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
9379 convert_parameter_type(&type
);
9380 /* we can add the type (NOTE: it could be local to the function) */
9382 /* accept other parameters */
9393 /* parse a function defined by symbol 'sym' and generate its code in
9394 'cur_text_section' */
9395 static void gen_function(Sym
*sym
)
9397 int saved_nocode_wanted
= nocode_wanted
;
9399 ind
= cur_text_section
->data_offset
;
9400 /* NOTE: we patch the symbol size later */
9401 put_extern_sym(sym
, cur_text_section
, ind
, 0);
9402 funcname
= get_tok_str(sym
->v
, NULL
);
9404 /* put debug symbol */
9406 put_func_debug(sym
);
9407 /* push a dummy symbol to enable local sym storage */
9408 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
9409 gfunc_prolog(&sym
->type
);
9411 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
9414 cur_text_section
->data_offset
= ind
;
9415 label_pop(&global_label_stack
, NULL
);
9416 sym_pop(&local_stack
, NULL
); /* reset local stack */
9417 /* end of function */
9418 /* patch symbol size */
9419 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
9422 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
9424 /* It's better to crash than to generate wrong code */
9425 cur_text_section
= NULL
;
9426 funcname
= ""; /* for safety */
9427 func_vt
.t
= VT_VOID
; /* for safety */
9428 ind
= 0; /* for safety */
9429 nocode_wanted
= saved_nocode_wanted
;
9432 static void gen_inline_functions(void)
9436 int *str
, inline_generated
;
9438 /* iterate while inline function are referenced */
9440 inline_generated
= 0;
9441 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9443 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9444 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9445 (VT_STATIC
| VT_INLINE
) &&
9447 /* the function was used: generate its code and
9448 convert it to a normal function */
9449 str
= INLINE_DEF(sym
->r
);
9450 sym
->r
= VT_SYM
| VT_CONST
;
9451 sym
->type
.t
&= ~VT_INLINE
;
9455 cur_text_section
= text_section
;
9457 macro_ptr
= NULL
; /* fail safe */
9460 inline_generated
= 1;
9463 if (!inline_generated
)
9467 /* free all remaining inline function tokens */
9468 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9470 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9471 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9472 (VT_STATIC
| VT_INLINE
)) {
9473 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9474 if (sym
->r
== (VT_SYM
| VT_CONST
)) //gr beware!
9476 str
= INLINE_DEF(sym
->r
);
9478 sym
->r
= 0; /* fail safe */
9483 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9484 static void decl(int l
)
9492 if (!parse_btype(&btype
, &ad
)) {
9493 /* skip redundant ';' */
9494 /* XXX: find more elegant solution */
9499 if (l
== VT_CONST
&&
9500 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
9501 /* global asm block */
9505 /* special test for old K&R protos without explicit int
9506 type. Only accepted when defining global data */
9507 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
9511 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9512 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9514 /* we accept no variable after */
9518 while (1) { /* iterate thru each declaration */
9520 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9524 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
9525 printf("type = '%s'\n", buf
);
9528 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9529 /* if old style function prototype, we accept a
9532 if (sym
->c
== FUNC_OLD
)
9533 func_decl_list(sym
);
9538 error("cannot use local functions");
9539 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
9540 expect("function definition");
9542 /* reject abstract declarators in function definition */
9544 while ((sym
= sym
->next
) != NULL
)
9545 if (!(sym
->v
& ~SYM_FIELD
))
9546 expect("identifier");
9548 /* XXX: cannot do better now: convert extern line to static inline */
9549 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
9550 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
9554 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
9556 /* specific case: if not func_call defined, we put
9557 the one of the prototype */
9558 /* XXX: should have default value */
9559 r
= sym
->type
.ref
->r
;
9560 if (FUNC_CALL(r
) != FUNC_CDECL
9561 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
9562 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
9564 FUNC_EXPORT(type
.ref
->r
) = 1;
9566 if (!is_compatible_types(&sym
->type
, &type
)) {
9568 error("incompatible types for redefinition of '%s'",
9569 get_tok_str(v
, NULL
));
9571 /* if symbol is already defined, then put complete type */
9574 /* put function symbol */
9575 sym
= global_identifier_push(v
, type
.t
, 0);
9576 sym
->type
.ref
= type
.ref
;
9579 /* static inline functions are just recorded as a kind
9580 of macro. Their code will be emitted at the end of
9581 the compilation unit only if they are used */
9582 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
9583 (VT_INLINE
| VT_STATIC
)) {
9584 TokenString func_str
;
9587 tok_str_new(&func_str
);
9593 error("unexpected end of file");
9594 tok_str_add_tok(&func_str
);
9599 } else if (t
== '}') {
9601 if (block_level
== 0)
9605 tok_str_add(&func_str
, -1);
9606 tok_str_add(&func_str
, 0);
9607 INLINE_DEF(sym
->r
) = func_str
.str
;
9609 /* compute text section */
9610 cur_text_section
= ad
.section
;
9611 if (!cur_text_section
)
9612 cur_text_section
= text_section
;
9613 sym
->r
= VT_SYM
| VT_CONST
;
9618 if (btype
.t
& VT_TYPEDEF
) {
9619 /* save typedefed type */
9620 /* XXX: test storage specifiers ? */
9621 sym
= sym_push(v
, &type
, 0, 0);
9622 sym
->type
.t
|= VT_TYPEDEF
;
9623 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9624 /* external function definition */
9625 /* specific case for func_call attribute */
9627 type
.ref
->r
= ad
.func_attr
;
9628 external_sym(v
, &type
, 0);
9630 /* not lvalue if array */
9632 if (!(type
.t
& VT_ARRAY
))
9633 r
|= lvalue_type(type
.t
);
9634 has_init
= (tok
== '=');
9635 if ((btype
.t
& VT_EXTERN
) ||
9636 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
9637 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
9638 /* external variable */
9639 /* NOTE: as GCC, uninitialized global static
9640 arrays of null size are considered as
9642 external_sym(v
, &type
, r
);
9644 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
9645 if (type
.t
& VT_STATIC
)
9651 decl_initializer_alloc(&type
, &ad
, r
,
9665 /* better than nothing, but needs extension to handle '-E' option
9667 static void preprocess_init(TCCState
*s1
)
9669 s1
->include_stack_ptr
= s1
->include_stack
;
9670 /* XXX: move that before to avoid having to initialize
9671 file->ifdef_stack_ptr ? */
9672 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
9673 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
9675 /* XXX: not ANSI compliant: bound checking says error */
9677 s1
->pack_stack
[0] = 0;
9678 s1
->pack_stack_ptr
= s1
->pack_stack
;
9681 /* compile the C file opened in 'file'. Return non zero if errors. */
9682 static int tcc_compile(TCCState
*s1
)
9686 volatile int section_sym
;
9689 printf("%s: **** new file\n", file
->filename
);
9691 preprocess_init(s1
);
9693 cur_text_section
= NULL
;
9695 anon_sym
= SYM_FIRST_ANOM
;
9697 /* file info: full path + filename */
9698 section_sym
= 0; /* avoid warning */
9700 section_sym
= put_elf_sym(symtab_section
, 0, 0,
9701 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
9702 text_section
->sh_num
, NULL
);
9703 getcwd(buf
, sizeof(buf
));
9705 normalize_slashes(buf
);
9707 pstrcat(buf
, sizeof(buf
), "/");
9708 put_stabs_r(buf
, N_SO
, 0, 0,
9709 text_section
->data_offset
, text_section
, section_sym
);
9710 put_stabs_r(file
->filename
, N_SO
, 0, 0,
9711 text_section
->data_offset
, text_section
, section_sym
);
9713 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9714 symbols can be safely used */
9715 put_elf_sym(symtab_section
, 0, 0,
9716 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
9717 SHN_ABS
, file
->filename
);
9719 /* define some often used types */
9720 int_type
.t
= VT_INT
;
9722 char_pointer_type
.t
= VT_BYTE
;
9723 mk_pointer(&char_pointer_type
);
9725 func_old_type
.t
= VT_FUNC
;
9726 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
9728 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9729 float_type
.t
= VT_FLOAT
;
9730 double_type
.t
= VT_DOUBLE
;
9732 func_float_type
.t
= VT_FUNC
;
9733 func_float_type
.ref
= sym_push(SYM_FIELD
, &float_type
, FUNC_CDECL
, FUNC_OLD
);
9734 func_double_type
.t
= VT_FUNC
;
9735 func_double_type
.ref
= sym_push(SYM_FIELD
, &double_type
, FUNC_CDECL
, FUNC_OLD
);
9739 /* define 'void *alloca(unsigned int)' builtin function */
9744 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
9745 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
9748 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
9752 define_start
= define_stack
;
9755 if (setjmp(s1
->error_jmp_buf
) == 0) {
9757 s1
->error_set_jmp_enabled
= 1;
9759 ch
= file
->buf_ptr
[0];
9760 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9761 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
;
9765 expect("declaration");
9767 /* end of translation unit info */
9769 put_stabs_r(NULL
, N_SO
, 0, 0,
9770 text_section
->data_offset
, text_section
, section_sym
);
9773 s1
->error_set_jmp_enabled
= 0;
9775 /* reset define stack, but leave -Dsymbols (may be incorrect if
9776 they are undefined) */
9777 free_defines(define_start
);
9779 gen_inline_functions();
9781 sym_pop(&global_stack
, NULL
);
9782 sym_pop(&local_stack
, NULL
);
9784 return s1
->nb_errors
!= 0 ? -1 : 0;
9787 /* Preprocess the current file */
9788 /* XXX: add line and file infos,
9789 * XXX: add options to preserve spaces (partly done, only spaces in macro are
9792 static int tcc_preprocess(TCCState
*s1
)
9795 BufferedFile
*file_ref
;
9796 int token_seen
, line_ref
;
9798 preprocess_init(s1
);
9799 define_start
= define_stack
;
9800 ch
= file
->buf_ptr
[0];
9802 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9803 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
9804 PARSE_FLAG_LINEFEED
;
9812 if (tok
== TOK_EOF
) {
9814 } else if (tok
== TOK_LINEFEED
) {
9819 } else if (token_seen
) {
9820 fwrite(tok_spaces
.data
, tok_spaces
.size
, 1, s1
->outfile
);
9822 int d
= file
->line_num
- line_ref
;
9823 if (file
!= file_ref
|| d
< 0 || d
>= 8)
9824 fprintf(s1
->outfile
, "# %d \"%s\"\n", file
->line_num
, file
->filename
);
9827 fputs("\n", s1
->outfile
), --d
;
9828 line_ref
= (file_ref
= file
)->line_num
;
9831 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
9833 free_defines(define_start
);
9838 int tcc_compile_string(TCCState
*s
, const char *str
)
9840 BufferedFile bf1
, *bf
= &bf1
;
9844 /* init file structure */
9846 /* XXX: avoid copying */
9848 buf
= tcc_malloc(len
+ 1);
9851 memcpy(buf
, str
, len
);
9854 bf
->buf_end
= buf
+ len
;
9855 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
9858 ret
= tcc_compile(s
);
9862 /* currently, no need to close */
9867 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9868 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
9870 BufferedFile bf1
, *bf
= &bf1
;
9872 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
9873 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
9877 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
9879 /* init file structure */
9881 bf
->buf_ptr
= bf
->buffer
;
9882 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
9883 *bf
->buf_end
= CH_EOB
;
9884 bf
->filename
[0] = '\0';
9888 s1
->include_stack_ptr
= s1
->include_stack
;
9890 /* parse with define parser */
9891 ch
= file
->buf_ptr
[0];
9897 /* undefine a preprocessor symbol */
9898 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
9902 ts
= tok_alloc(sym
, strlen(sym
));
9903 s
= define_find(ts
->tok
);
9904 /* undefine symbol by putting an invalid name */
9909 #ifdef CONFIG_TCC_ASM
9911 #ifdef TCC_TARGET_I386
9912 #include "i386-asm.c"
9917 static void asm_instr(void)
9919 error("inline asm() not supported");
9921 static void asm_global_instr(void)
9923 error("inline asm() not supported");
9929 #ifdef TCC_TARGET_COFF
9930 #include "tcccoff.c"
9933 #ifdef TCC_TARGET_PE
9937 /* print the position in the source file of PC value 'pc' by reading
9938 the stabs debug information */
9939 static void rt_printline(unsigned long wanted_pc
)
9941 Stab_Sym
*sym
, *sym_end
;
9942 char func_name
[128], last_func_name
[128];
9943 unsigned long func_addr
, last_pc
, pc
;
9944 const char *incl_files
[INCLUDE_STACK_SIZE
];
9945 int incl_index
, len
, last_line_num
, i
;
9946 const char *str
, *p
;
9948 fprintf(stderr
, "0x%08lx:", wanted_pc
);
9950 func_name
[0] = '\0';
9953 last_func_name
[0] = '\0';
9954 last_pc
= 0xffffffff;
9956 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
9957 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
9958 while (sym
< sym_end
) {
9959 switch(sym
->n_type
) {
9960 /* function start or end */
9962 if (sym
->n_strx
== 0) {
9963 /* we test if between last line and end of function */
9964 pc
= sym
->n_value
+ func_addr
;
9965 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9967 func_name
[0] = '\0';
9970 str
= stabstr_section
->data
+ sym
->n_strx
;
9971 p
= strchr(str
, ':');
9973 pstrcpy(func_name
, sizeof(func_name
), str
);
9976 if (len
> sizeof(func_name
) - 1)
9977 len
= sizeof(func_name
) - 1;
9978 memcpy(func_name
, str
, len
);
9979 func_name
[len
] = '\0';
9981 func_addr
= sym
->n_value
;
9984 /* line number info */
9986 pc
= sym
->n_value
+ func_addr
;
9987 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9990 last_line_num
= sym
->n_desc
;
9992 strcpy(last_func_name
, func_name
);
9996 str
= stabstr_section
->data
+ sym
->n_strx
;
9998 if (incl_index
< INCLUDE_STACK_SIZE
) {
9999 incl_files
[incl_index
++] = str
;
10003 if (incl_index
> 1)
10007 if (sym
->n_strx
== 0) {
10008 incl_index
= 0; /* end of translation unit */
10010 str
= stabstr_section
->data
+ sym
->n_strx
;
10011 /* do not add path */
10013 if (len
> 0 && str
[len
- 1] != '/')
10021 /* second pass: we try symtab symbols (no line number info) */
10024 ElfW(Sym
) *sym
, *sym_end
;
10027 sym_end
= (ElfW(Sym
) *)(symtab_section
->data
+ symtab_section
->data_offset
);
10028 for(sym
= (ElfW(Sym
) *)symtab_section
->data
+ 1;
10031 type
= ELFW(ST_TYPE
)(sym
->st_info
);
10032 if (type
== STT_FUNC
) {
10033 if (wanted_pc
>= sym
->st_value
&&
10034 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
10035 pstrcpy(last_func_name
, sizeof(last_func_name
),
10036 strtab_section
->data
+ sym
->st_name
);
10042 /* did not find any info: */
10043 fprintf(stderr
, " ???\n");
10046 if (last_func_name
[0] != '\0') {
10047 fprintf(stderr
, " %s()", last_func_name
);
10049 if (incl_index
> 0) {
10050 fprintf(stderr
, " (%s:%d",
10051 incl_files
[incl_index
- 1], last_line_num
);
10052 for(i
= incl_index
- 2; i
>= 0; i
--)
10053 fprintf(stderr
, ", included from %s", incl_files
[i
]);
10054 fprintf(stderr
, ")");
10056 fprintf(stderr
, "\n");
10059 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
10063 /* fix for glibc 2.1 */
10065 #define REG_EIP EIP
10066 #define REG_EBP EBP
10069 /* return the PC at frame level 'level'. Return non zero if not found */
10070 static int rt_get_caller_pc(unsigned long *paddr
,
10071 ucontext_t
*uc
, int level
)
10077 #if defined(__FreeBSD__)
10078 *paddr
= uc
->uc_mcontext
.mc_eip
;
10079 #elif defined(__dietlibc__)
10080 *paddr
= uc
->uc_mcontext
.eip
;
10082 *paddr
= uc
->uc_mcontext
.gregs
[REG_EIP
];
10086 #if defined(__FreeBSD__)
10087 fp
= uc
->uc_mcontext
.mc_ebp
;
10088 #elif defined(__dietlibc__)
10089 fp
= uc
->uc_mcontext
.ebp
;
10091 fp
= uc
->uc_mcontext
.gregs
[REG_EBP
];
10093 for(i
=1;i
<level
;i
++) {
10094 /* XXX: check address validity with program info */
10095 if (fp
<= 0x1000 || fp
>= 0xc0000000)
10097 fp
= ((unsigned long *)fp
)[0];
10099 *paddr
= ((unsigned long *)fp
)[1];
10103 #elif defined(__x86_64__)
10104 /* return the PC at frame level 'level'. Return non zero if not found */
10105 static int rt_get_caller_pc(unsigned long *paddr
,
10106 ucontext_t
*uc
, int level
)
10112 /* XXX: only support linux */
10113 *paddr
= uc
->uc_mcontext
.gregs
[REG_RIP
];
10116 fp
= uc
->uc_mcontext
.gregs
[REG_RBP
];
10117 for(i
=1;i
<level
;i
++) {
10118 /* XXX: check address validity with program info */
10119 if (fp
<= 0x1000 || fp
>= 0xc0000000)
10121 fp
= ((unsigned long *)fp
)[0];
10123 *paddr
= ((unsigned long *)fp
)[1];
10129 #warning add arch specific rt_get_caller_pc()
10131 static int rt_get_caller_pc(unsigned long *paddr
,
10132 ucontext_t
*uc
, int level
)
10138 /* emit a run time error at position 'pc' */
10139 void rt_error(ucontext_t
*uc
, const char *fmt
, ...)
10146 fprintf(stderr
, "Runtime error: ");
10147 vfprintf(stderr
, fmt
, ap
);
10148 fprintf(stderr
, "\n");
10149 for(i
=0;i
<num_callers
;i
++) {
10150 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
10153 fprintf(stderr
, "at ");
10155 fprintf(stderr
, "by ");
10162 /* signal handler for fatal errors */
10163 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
10165 ucontext_t
*uc
= puc
;
10169 switch(siginf
->si_code
) {
10172 rt_error(uc
, "division by zero");
10175 rt_error(uc
, "floating point exception");
10181 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
10182 rt_error(uc
, *rt_bound_error_msg
);
10184 rt_error(uc
, "dereferencing invalid pointer");
10187 rt_error(uc
, "illegal instruction");
10190 rt_error(uc
, "abort() called");
10193 rt_error(uc
, "caught signal %d", signum
);
10200 /* do all relocations (needed before using tcc_get_symbol()) */
10201 int tcc_relocate(TCCState
*s1
)
10208 #ifdef TCC_TARGET_PE
10209 pe_add_runtime(s1
);
10211 tcc_add_runtime(s1
);
10214 relocate_common_syms();
10216 tcc_add_linker_symbols(s1
);
10217 #ifndef TCC_TARGET_PE
10218 build_got_entries(s1
);
10220 /* compute relocation address : section are relocated in place. We
10221 also alloc the bss space */
10222 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10223 s
= s1
->sections
[i
];
10224 if (s
->sh_flags
& SHF_ALLOC
) {
10225 if (s
->sh_type
== SHT_NOBITS
)
10226 s
->data
= tcc_mallocz(s
->data_offset
);
10227 s
->sh_addr
= (unsigned long)s
->data
;
10231 relocate_syms(s1
, 1);
10233 if (s1
->nb_errors
!= 0)
10236 /* relocate each section */
10237 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10238 s
= s1
->sections
[i
];
10240 relocate_section(s1
, s
);
10243 /* mark executable sections as executable in memory */
10244 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10245 s
= s1
->sections
[i
];
10246 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_EXECINSTR
)) ==
10247 (SHF_ALLOC
| SHF_EXECINSTR
))
10248 set_pages_executable(s
->data
, s
->data_offset
);
10253 /* launch the compiled program with the given arguments */
10254 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
10256 int (*prog_main
)(int, char **);
10258 if (tcc_relocate(s1
) < 0)
10261 prog_main
= tcc_get_symbol_err(s1
, "main");
10264 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10265 error("debug mode currently not available for Windows");
10267 struct sigaction sigact
;
10268 /* install TCC signal handlers to print debug info on fatal
10270 sigact
.sa_flags
= SA_SIGINFO
| SA_RESETHAND
;
10271 sigact
.sa_sigaction
= sig_error
;
10272 sigemptyset(&sigact
.sa_mask
);
10273 sigaction(SIGFPE
, &sigact
, NULL
);
10274 sigaction(SIGILL
, &sigact
, NULL
);
10275 sigaction(SIGSEGV
, &sigact
, NULL
);
10276 sigaction(SIGBUS
, &sigact
, NULL
);
10277 sigaction(SIGABRT
, &sigact
, NULL
);
10281 #ifdef CONFIG_TCC_BCHECK
10282 if (do_bounds_check
) {
10283 void (*bound_init
)(void);
10285 /* set error function */
10286 rt_bound_error_msg
= (void *)tcc_get_symbol_err(s1
,
10287 "__bound_error_msg");
10289 /* XXX: use .init section so that it also work in binary ? */
10290 bound_init
= (void *)tcc_get_symbol_err(s1
, "__bound_init");
10294 return (*prog_main
)(argc
, argv
);
10297 void tcc_memstats(void)
10300 printf("memory in use: %d\n", mem_cur_size
);
10304 static void tcc_cleanup(void)
10308 if (NULL
== tcc_state
)
10312 /* free -D defines */
10313 free_defines(NULL
);
10316 n
= tok_ident
- TOK_IDENT
;
10317 for(i
= 0; i
< n
; i
++)
10318 tcc_free(table_ident
[i
]);
10319 tcc_free(table_ident
);
10321 /* free sym_pools */
10322 dynarray_reset(&sym_pools
, &nb_sym_pools
);
10323 /* string buffer */
10324 cstr_free(&tokcstr
);
10325 /* reset symbol stack */
10326 sym_free_first
= NULL
;
10327 /* cleanup from error/setjmp */
10331 TCCState
*tcc_new(void)
10340 s
= tcc_mallocz(sizeof(TCCState
));
10344 s
->output_type
= TCC_OUTPUT_MEMORY
;
10346 /* init isid table */
10347 for(i
=CH_EOF
;i
<256;i
++)
10348 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
10350 /* add all tokens */
10351 table_ident
= NULL
;
10352 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
10354 tok_ident
= TOK_IDENT
;
10363 ts
= tok_alloc(p
, r
- p
- 1);
10367 /* we add dummy defines for some special macros to speed up tests
10368 and to have working defined() */
10369 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
10370 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
10371 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
10372 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
10374 /* standard defines */
10375 tcc_define_symbol(s
, "__STDC__", NULL
);
10376 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
10377 #if defined(TCC_TARGET_I386)
10378 tcc_define_symbol(s
, "__i386__", NULL
);
10380 #if defined(TCC_TARGET_X86_64)
10381 tcc_define_symbol(s
, "__x86_64__", NULL
);
10383 #if defined(TCC_TARGET_ARM)
10384 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
10385 tcc_define_symbol(s
, "__arm_elf__", NULL
);
10386 tcc_define_symbol(s
, "__arm_elf", NULL
);
10387 tcc_define_symbol(s
, "arm_elf", NULL
);
10388 tcc_define_symbol(s
, "__arm__", NULL
);
10389 tcc_define_symbol(s
, "__arm", NULL
);
10390 tcc_define_symbol(s
, "arm", NULL
);
10391 tcc_define_symbol(s
, "__APCS_32__", NULL
);
10393 #ifdef TCC_TARGET_PE
10394 tcc_define_symbol(s
, "_WIN32", NULL
);
10396 tcc_define_symbol(s
, "__unix__", NULL
);
10397 tcc_define_symbol(s
, "__unix", NULL
);
10398 #if defined(__linux)
10399 tcc_define_symbol(s
, "__linux__", NULL
);
10400 tcc_define_symbol(s
, "__linux", NULL
);
10403 /* tiny C specific defines */
10404 tcc_define_symbol(s
, "__TINYC__", NULL
);
10406 /* tiny C & gcc defines */
10407 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
10408 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
10409 #ifdef TCC_TARGET_PE
10410 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
10412 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
10415 #ifndef TCC_TARGET_PE
10416 /* default library paths */
10417 tcc_add_library_path(s
, CONFIG_SYSROOT
"/usr/local/lib");
10418 tcc_add_library_path(s
, CONFIG_SYSROOT
"/usr/lib");
10419 tcc_add_library_path(s
, CONFIG_SYSROOT
"/lib");
10422 /* no section zero */
10423 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
10425 /* create standard sections */
10426 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
10427 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
10428 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
10430 /* symbols are always generated for linking stage */
10431 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
10433 ".hashtab", SHF_PRIVATE
);
10434 strtab_section
= symtab_section
->link
;
10436 /* private symbol table for dynamic symbols */
10437 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
10439 ".dynhashtab", SHF_PRIVATE
);
10440 s
->alacarte_link
= 1;
10442 #ifdef CHAR_IS_UNSIGNED
10443 s
->char_is_unsigned
= 1;
10445 #if defined(TCC_TARGET_PE) && 0
10446 /* XXX: currently the PE linker is not ready to support that */
10447 s
->leading_underscore
= 1;
10450 #ifdef TCC_TARGET_X86_64
10451 s
->jmp_table
= NULL
;
10456 void tcc_delete(TCCState
*s1
)
10462 /* free all sections */
10463 free_section(s1
->dynsymtab_section
);
10465 for(i
= 1; i
< s1
->nb_sections
; i
++)
10466 free_section(s1
->sections
[i
]);
10467 tcc_free(s1
->sections
);
10469 /* free any loaded DLLs */
10470 for ( i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
10472 DLLReference
*ref
= s1
->loaded_dlls
[i
];
10474 dlclose(ref
->handle
);
10477 /* free loaded dlls array */
10478 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
10480 /* free library paths */
10481 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
10483 /* free include paths */
10484 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
10485 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
10486 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
10488 #ifdef TCC_TARGET_X86_64
10489 tcc_free(s1
->jmp_table
);
10494 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
10498 pathname1
= tcc_strdup(pathname
);
10499 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
10503 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
10507 pathname1
= tcc_strdup(pathname
);
10508 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
10512 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
10517 BufferedFile
*saved_file
;
10519 /* find source file type with extension */
10520 ext
= tcc_fileextension(filename
);
10524 /* open the file */
10526 file
= tcc_open(s1
, filename
);
10528 if (flags
& AFF_PRINT_ERROR
) {
10529 error_noabort("file '%s' not found", filename
);
10535 if (flags
& AFF_PREPROCESS
) {
10536 ret
= tcc_preprocess(s1
);
10537 } else if (!ext
[0] || !strcmp(ext
, "c")) {
10538 /* C file assumed */
10539 ret
= tcc_compile(s1
);
10541 #ifdef CONFIG_TCC_ASM
10542 if (!strcmp(ext
, "S")) {
10543 /* preprocessed assembler */
10544 ret
= tcc_assemble(s1
, 1);
10545 } else if (!strcmp(ext
, "s")) {
10546 /* non preprocessed assembler */
10547 ret
= tcc_assemble(s1
, 0);
10550 #ifdef TCC_TARGET_PE
10551 if (!strcmp(ext
, "def")) {
10552 ret
= pe_load_def_file(s1
, file
->fd
);
10557 /* assume executable format: auto guess file type */
10558 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
10559 lseek(fd
, 0, SEEK_SET
);
10561 error_noabort("could not read header");
10563 } else if (ret
!= sizeof(ehdr
)) {
10564 goto try_load_script
;
10567 if (ehdr
.e_ident
[0] == ELFMAG0
&&
10568 ehdr
.e_ident
[1] == ELFMAG1
&&
10569 ehdr
.e_ident
[2] == ELFMAG2
&&
10570 ehdr
.e_ident
[3] == ELFMAG3
) {
10571 file
->line_num
= 0; /* do not display line number if error */
10572 if (ehdr
.e_type
== ET_REL
) {
10573 ret
= tcc_load_object_file(s1
, fd
, 0);
10574 } else if (ehdr
.e_type
== ET_DYN
) {
10575 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
10576 #ifdef TCC_TARGET_PE
10580 h
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
10587 ret
= tcc_load_dll(s1
, fd
, filename
,
10588 (flags
& AFF_REFERENCED_DLL
) != 0);
10591 error_noabort("unrecognized ELF file");
10594 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
10595 file
->line_num
= 0; /* do not display line number if error */
10596 ret
= tcc_load_archive(s1
, fd
);
10598 #ifdef TCC_TARGET_COFF
10599 if (*(uint16_t *)(&ehdr
) == COFF_C67_MAGIC
) {
10600 ret
= tcc_load_coff(s1
, fd
);
10603 #ifdef TCC_TARGET_PE
10604 if (pe_test_res_file(&ehdr
, ret
)) {
10605 ret
= pe_load_res_file(s1
, fd
);
10609 /* as GNU ld, consider it is an ld script if not recognized */
10611 ret
= tcc_load_ldscript(s1
);
10613 error_noabort("unrecognized file type");
10628 int tcc_add_file(TCCState
*s
, const char *filename
)
10630 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
10633 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
10637 pathname1
= tcc_strdup(pathname
);
10638 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
10642 /* find and load a dll. Return non zero if not found */
10643 /* XXX: add '-rpath' option support ? */
10644 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
10649 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10650 snprintf(buf
, sizeof(buf
), "%s/%s",
10651 s
->library_paths
[i
], filename
);
10652 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
10658 /* the library name is the same as the argument of the '-l' option */
10659 int tcc_add_library(TCCState
*s
, const char *libraryname
)
10664 /* first we look for the dynamic library if not static linking */
10665 if (!s
->static_link
) {
10666 #ifdef TCC_TARGET_PE
10667 snprintf(buf
, sizeof(buf
), "%s.def", libraryname
);
10669 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
10671 if (tcc_add_dll(s
, buf
, 0) == 0)
10675 /* then we look for the static library */
10676 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10677 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
10678 s
->library_paths
[i
], libraryname
);
10679 if (tcc_add_file_internal(s
, buf
, 0) == 0)
10685 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
10687 add_elf_sym(symtab_section
, val
, 0,
10688 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
10693 int tcc_set_output_type(TCCState
*s
, int output_type
)
10697 s
->output_type
= output_type
;
10699 if (!s
->nostdinc
) {
10700 /* default include paths */
10701 /* XXX: reverse order needed if -isystem support */
10702 #ifndef TCC_TARGET_PE
10703 tcc_add_sysinclude_path(s
, CONFIG_SYSROOT
"/usr/local/include");
10704 tcc_add_sysinclude_path(s
, CONFIG_SYSROOT
"/usr/include");
10706 snprintf(buf
, sizeof(buf
), "%s/include", tcc_lib_path
);
10707 tcc_add_sysinclude_path(s
, buf
);
10708 #ifdef TCC_TARGET_PE
10709 snprintf(buf
, sizeof(buf
), "%s/include/winapi", tcc_lib_path
);
10710 tcc_add_sysinclude_path(s
, buf
);
10714 /* if bound checking, then add corresponding sections */
10715 #ifdef CONFIG_TCC_BCHECK
10716 if (do_bounds_check
) {
10717 /* define symbol */
10718 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
10719 /* create bounds sections */
10720 bounds_section
= new_section(s
, ".bounds",
10721 SHT_PROGBITS
, SHF_ALLOC
);
10722 lbounds_section
= new_section(s
, ".lbounds",
10723 SHT_PROGBITS
, SHF_ALLOC
);
10727 if (s
->char_is_unsigned
) {
10728 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
10731 /* add debug sections */
10734 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
10735 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
10736 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
10737 put_elf_str(stabstr_section
, "");
10738 stab_section
->link
= stabstr_section
;
10739 /* put first entry */
10740 put_stabs("", 0, 0, 0, 0);
10743 /* add libc crt1/crti objects */
10744 #ifndef TCC_TARGET_PE
10745 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
10747 if (output_type
!= TCC_OUTPUT_DLL
)
10748 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
10749 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
10753 #ifdef TCC_TARGET_PE
10754 snprintf(buf
, sizeof(buf
), "%s/lib", tcc_lib_path
);
10755 tcc_add_library_path(s
, buf
);
10761 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10762 #define FD_INVERT 0x0002 /* invert value before storing */
10764 typedef struct FlagDef
{
10770 static const FlagDef warning_defs
[] = {
10771 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
10772 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
10773 { offsetof(TCCState
, warn_error
), 0, "error" },
10774 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
10775 "implicit-function-declaration" },
10778 static int set_flag(TCCState
*s
, const FlagDef
*flags
, int nb_flags
,
10779 const char *name
, int value
)
10786 if (r
[0] == 'n' && r
[1] == 'o' && r
[2] == '-') {
10790 for(i
= 0, p
= flags
; i
< nb_flags
; i
++, p
++) {
10791 if (!strcmp(r
, p
->name
))
10796 if (p
->flags
& FD_INVERT
)
10798 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
10803 /* set/reset a warning */
10804 int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
10809 if (!strcmp(warning_name
, "all")) {
10810 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
10811 if (p
->flags
& WD_ALL
)
10812 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
10816 return set_flag(s
, warning_defs
, countof(warning_defs
),
10817 warning_name
, value
);
10821 static const FlagDef flag_defs
[] = {
10822 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
10823 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
10824 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
10825 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
10828 /* set/reset a flag */
10829 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
)
10831 return set_flag(s
, flag_defs
, countof(flag_defs
),
10835 #if !defined(LIBTCC)
10837 static int64_t getclock_us(void)
10842 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
10845 gettimeofday(&tv
, NULL
);
10846 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
10852 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10853 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10854 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10855 " [-static] [infile1 infile2...] [-run infile args...]\n"
10857 "General options:\n"
10858 " -v display current version, increase verbosity\n"
10859 " -c compile only - generate an object file\n"
10860 " -o outfile set output filename\n"
10861 " -Bdir set tcc internal library path\n"
10862 " -bench output compilation statistics\n"
10863 " -run run compiled source\n"
10864 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10865 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10866 " -w disable all warnings\n"
10867 "Preprocessor options:\n"
10868 " -E preprocess only\n"
10869 " -Idir add include path 'dir'\n"
10870 " -Dsym[=val] define 'sym' with value 'val'\n"
10871 " -Usym undefine 'sym'\n"
10872 "Linker options:\n"
10873 " -Ldir add library path 'dir'\n"
10874 " -llib link with dynamic or static library 'lib'\n"
10875 " -shared generate a shared library\n"
10876 " -soname set name for shared library to be used at runtime\n"
10877 " -static static linking\n"
10878 " -rdynamic export all global symbols to dynamic linker\n"
10879 " -r generate (relocatable) object file\n"
10880 "Debugger options:\n"
10881 " -g generate runtime debug info\n"
10882 #ifdef CONFIG_TCC_BCHECK
10883 " -b compile with built-in memory and bounds checker (implies -g)\n"
10885 " -bt N show N callers in stack traces\n"
10889 #define TCC_OPTION_HAS_ARG 0x0001
10890 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10892 typedef struct TCCOption
{
10921 TCC_OPTION_nostdinc
,
10922 TCC_OPTION_nostdlib
,
10923 TCC_OPTION_print_search_dirs
,
10924 TCC_OPTION_rdynamic
,
10932 static const TCCOption tcc_options
[] = {
10933 { "h", TCC_OPTION_HELP
, 0 },
10934 { "?", TCC_OPTION_HELP
, 0 },
10935 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
10936 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
10937 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
10938 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
10939 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
10940 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10941 { "bench", TCC_OPTION_bench
, 0 },
10942 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
10943 #ifdef CONFIG_TCC_BCHECK
10944 { "b", TCC_OPTION_b
, 0 },
10946 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10947 { "c", TCC_OPTION_c
, 0 },
10948 { "static", TCC_OPTION_static
, 0 },
10949 { "shared", TCC_OPTION_shared
, 0 },
10950 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
10951 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
10952 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10953 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
10954 { "r", TCC_OPTION_r
, 0 },
10955 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10956 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10957 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10958 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
10959 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10960 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
10961 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
10962 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
10963 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10964 { "w", TCC_OPTION_w
, 0 },
10965 { "pipe", TCC_OPTION_pipe
, 0},
10966 { "E", TCC_OPTION_E
, 0},
10970 /* convert 'str' into an array of space separated strings */
10971 static int expand_args(char ***pargv
, const char *str
)
10980 while (is_space(*str
))
10985 while (*str
!= '\0' && !is_space(*str
))
10988 arg
= tcc_malloc(len
+ 1);
10989 memcpy(arg
, s1
, len
);
10991 dynarray_add((void ***)&argv
, &argc
, arg
);
10997 static char **files
;
10998 static int nb_files
, nb_libraries
;
10999 static int multiple_files
;
11000 static int print_search_dirs
;
11001 static int output_type
;
11002 static int reloc_output
;
11003 static const char *outfile
;
11005 int parse_args(TCCState
*s
, int argc
, char **argv
)
11008 const TCCOption
*popt
;
11009 const char *optarg
, *p1
, *r1
;
11013 while (optind
< argc
) {
11015 r
= argv
[optind
++];
11016 if (r
[0] != '-' || r
[1] == '\0') {
11017 /* add a new file */
11018 dynarray_add((void ***)&files
, &nb_files
, r
);
11019 if (!multiple_files
) {
11021 /* argv[0] will be this file */
11025 /* find option in table (match only the first chars */
11026 popt
= tcc_options
;
11030 error("invalid option -- '%s'", r
);
11043 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
11044 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
11047 if (optind
>= argc
)
11048 error("argument to '%s' is missing", r
);
11049 optarg
= argv
[optind
++];
11057 switch(popt
->index
) {
11058 case TCC_OPTION_HELP
:
11062 if (tcc_add_include_path(s
, optarg
) < 0)
11063 error("too many include paths");
11068 sym
= (char *)optarg
;
11069 value
= strchr(sym
, '=');
11074 tcc_define_symbol(s
, sym
, value
);
11078 tcc_undefine_symbol(s
, optarg
);
11081 tcc_add_library_path(s
, optarg
);
11084 /* set tcc utilities path (mainly for tcc development) */
11085 tcc_lib_path
= optarg
;
11088 dynarray_add((void ***)&files
, &nb_files
, r
);
11091 case TCC_OPTION_bench
:
11094 case TCC_OPTION_bt
:
11095 num_callers
= atoi(optarg
);
11097 #ifdef CONFIG_TCC_BCHECK
11099 do_bounds_check
= 1;
11107 multiple_files
= 1;
11108 output_type
= TCC_OUTPUT_OBJ
;
11110 case TCC_OPTION_static
:
11111 s
->static_link
= 1;
11113 case TCC_OPTION_shared
:
11114 output_type
= TCC_OUTPUT_DLL
;
11116 case TCC_OPTION_soname
:
11117 s
->soname
= optarg
;
11120 multiple_files
= 1;
11124 /* generate a .o merging several output files */
11126 output_type
= TCC_OUTPUT_OBJ
;
11128 case TCC_OPTION_nostdinc
:
11131 case TCC_OPTION_nostdlib
:
11134 case TCC_OPTION_print_search_dirs
:
11135 print_search_dirs
= 1;
11137 case TCC_OPTION_run
:
11141 argc1
= expand_args(&argv1
, optarg
);
11143 parse_args(s
, argc1
, argv1
);
11145 multiple_files
= 0;
11146 output_type
= TCC_OUTPUT_MEMORY
;
11151 if (0 == verbose
++)
11152 printf("tcc version %s\n", TCC_VERSION
);
11153 } while (*optarg
++ == 'v');
11156 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
11157 goto unsupported_option
;
11160 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
11161 s
->warn_unsupported
)
11162 goto unsupported_option
;
11167 case TCC_OPTION_rdynamic
:
11170 case TCC_OPTION_Wl
:
11173 if (strstart(optarg
, "-Ttext,", &p
)) {
11174 s
->text_addr
= strtoul(p
, NULL
, 16);
11175 s
->has_text_addr
= 1;
11176 } else if (strstart(optarg
, "--oformat,", &p
)) {
11177 if (strstart(p
, "elf32-", NULL
)) {
11178 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
11179 } else if (!strcmp(p
, "binary")) {
11180 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
11182 #ifdef TCC_TARGET_COFF
11183 if (!strcmp(p
, "coff")) {
11184 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
11188 error("target %s not found", p
);
11191 error("unsupported linker option '%s'", optarg
);
11196 output_type
= TCC_OUTPUT_PREPROCESS
;
11199 if (s
->warn_unsupported
) {
11200 unsupported_option
:
11201 warning("unsupported option '%s'", r
);
11210 int main(int argc
, char **argv
)
11214 int nb_objfiles
, ret
, optind
;
11215 char objfilename
[1024];
11216 int64_t start_time
= 0;
11219 tcc_lib_path
= w32_tcc_lib_path();
11223 output_type
= TCC_OUTPUT_EXE
;
11225 multiple_files
= 1;
11230 print_search_dirs
= 0;
11233 optind
= parse_args(s
, argc
- 1, argv
+ 1);
11234 if (print_search_dirs
) {
11235 /* enough for Linux kernel */
11236 printf("install: %s/\n", tcc_lib_path
);
11239 if (optind
== 0 || nb_files
== 0) {
11240 if (optind
&& verbose
)
11246 nb_objfiles
= nb_files
- nb_libraries
;
11248 /* if outfile provided without other options, we output an
11250 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
11251 output_type
= TCC_OUTPUT_EXE
;
11253 /* check -c consistency : only single file handled. XXX: checks file type */
11254 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
11255 /* accepts only a single input file */
11256 if (nb_objfiles
!= 1)
11257 error("cannot specify multiple files with -c");
11258 if (nb_libraries
!= 0)
11259 error("cannot specify libraries with -c");
11263 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
11265 s
->outfile
= stdout
;
11267 s
->outfile
= fopen(outfile
, "w");
11269 error("could not open '%s", outfile
);
11271 } else if (output_type
!= TCC_OUTPUT_MEMORY
) {
11273 /* compute default outfile name */
11276 strcmp(files
[0], "-") == 0 ? "a" : tcc_basename(files
[0]);
11277 pstrcpy(objfilename
, sizeof(objfilename
), name
);
11278 ext
= tcc_fileextension(objfilename
);
11279 #ifdef TCC_TARGET_PE
11280 if (output_type
== TCC_OUTPUT_DLL
)
11281 strcpy(ext
, ".dll");
11283 if (output_type
== TCC_OUTPUT_EXE
)
11284 strcpy(ext
, ".exe");
11287 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
&& *ext
)
11290 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
11291 outfile
= objfilename
;
11296 start_time
= getclock_us();
11299 tcc_set_output_type(s
, output_type
);
11301 /* compile or add each files or library */
11302 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
11303 const char *filename
;
11305 filename
= files
[i
];
11306 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
11307 if (tcc_add_file_internal(s
, filename
,
11308 AFF_PRINT_ERROR
| AFF_PREPROCESS
) < 0)
11310 } else if (filename
[0] == '-' && filename
[1]) {
11311 if (tcc_add_library(s
, filename
+ 2) < 0)
11312 error("cannot find %s", filename
);
11315 printf("-> %s\n", filename
);
11316 if (tcc_add_file(s
, filename
) < 0)
11321 /* free all files */
11329 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
11330 if (total_time
< 0.001)
11331 total_time
= 0.001;
11332 if (total_bytes
< 1)
11334 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11335 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
11336 total_time
, (int)(total_lines
/ total_time
),
11337 total_bytes
/ total_time
/ 1000000.0);
11340 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
11342 fclose(s
->outfile
);
11343 } else if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
11344 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
11346 ret
= tcc_output_file(s
, outfile
) ? 1 : 0;
11348 /* XXX: cannot do it with bound checking because of the malloc hooks */
11349 if (!do_bounds_check
)
11354 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);