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
, ...);
847 void vpushll(long long v
);
850 void lexpand_nr(void);
851 static void vpush_global_sym(CType
*type
, int v
);
852 void vset(CType
*type
, int r
, int v
);
853 void type_to_str(char *buf
, int buf_size
,
854 CType
*type
, const char *varstr
);
855 char *get_tok_str(int v
, CValue
*cv
);
856 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
857 unsigned long offset
, unsigned long size
);
858 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
860 /* section generation */
861 static void section_realloc(Section
*sec
, unsigned long new_size
);
862 static void *section_ptr_add(Section
*sec
, unsigned long size
);
863 static void put_extern_sym(Sym
*sym
, Section
*section
,
864 unsigned long value
, unsigned long size
);
865 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
866 static int put_elf_str(Section
*s
, const char *sym
);
867 static int put_elf_sym(Section
*s
,
868 unsigned long value
, unsigned long size
,
869 int info
, int other
, int shndx
, const char *name
);
870 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
871 int info
, int other
, int sh_num
, const char *name
);
872 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
873 int type
, int symbol
);
874 static void put_stabs(const char *str
, int type
, int other
, int desc
,
875 unsigned long value
);
876 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
877 unsigned long value
, Section
*sec
, int sym_index
);
878 static void put_stabn(int type
, int other
, int desc
, int value
);
879 static void put_stabd(int type
, int other
, int desc
);
880 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
882 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
883 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
884 #define AFF_PREPROCESS 0x0004 /* preprocess file */
885 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
888 int tcc_output_coff(TCCState
*s1
, FILE *f
);
891 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
);
892 int pe_load_def_file(struct TCCState
*s1
, int fd
);
893 int pe_test_res_file(void *v
, int size
);
894 int pe_load_res_file(struct TCCState
*s1
, int fd
);
895 void pe_add_runtime(struct TCCState
*s1
);
896 void pe_guess_outfile(char *objfilename
, int output_type
);
897 int pe_output_file(struct TCCState
*s1
, const char *filename
);
901 #ifdef CONFIG_TCC_ASM
903 typedef struct ExprValue
{
908 #define MAX_ASM_OPERANDS 30
910 typedef struct ASMOperand
{
911 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
913 char asm_str
[16]; /* computed asm string for operand */
914 SValue
*vt
; /* C value of the expression */
915 int ref_index
; /* if >= 0, gives reference to a output constraint */
916 int input_index
; /* if >= 0, gives reference to an input constraint */
917 int priority
; /* priority, used to assign registers */
918 int reg
; /* if >= 0, register number used for this operand */
919 int is_llong
; /* true if double register value */
920 int is_memory
; /* true if memory operand */
921 int is_rw
; /* for '+' modifier */
924 static void asm_expr(TCCState
*s1
, ExprValue
*pe
);
925 static int asm_int_expr(TCCState
*s1
);
926 static int find_constraint(ASMOperand
*operands
, int nb_operands
,
927 const char *name
, const char **pp
);
929 static int tcc_assemble(TCCState
*s1
, int do_preprocess
);
933 static void asm_instr(void);
934 static void asm_global_instr(void);
936 /* true if float/double/long double type */
937 static inline int is_float(int t
)
941 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
944 #ifdef TCC_TARGET_I386
945 #include "i386-gen.c"
948 #ifdef TCC_TARGET_ARM
952 #ifdef TCC_TARGET_C67
956 #ifdef TCC_TARGET_X86_64
957 #include "x86_64-gen.c"
960 #ifdef CONFIG_TCC_STATIC
962 #define RTLD_LAZY 0x001
963 #define RTLD_NOW 0x002
964 #define RTLD_GLOBAL 0x100
965 #define RTLD_DEFAULT NULL
967 /* dummy function for profiling */
968 void *dlopen(const char *filename
, int flag
)
973 const char *dlerror(void)
978 typedef struct TCCSyms
{
983 #define TCCSYM(a) { #a, &a, },
985 /* add the symbol you want here if no dynamic linking is done */
986 static TCCSyms tcc_syms
[] = {
987 #if !defined(CONFIG_TCCBOOT)
996 void *resolve_sym(TCCState
*s1
, const char *symbol
, int type
)
1000 while (p
->str
!= NULL
) {
1001 if (!strcmp(p
->str
, symbol
))
1008 #elif !defined(_WIN32)
1012 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
)
1014 return dlsym(RTLD_DEFAULT
, sym
);
1019 /********************************************************/
1021 /* we use our own 'finite' function to avoid potential problems with
1022 non standard math libs */
1023 /* XXX: endianness dependent */
1024 int ieee_finite(double d
)
1027 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
1030 /* copy a string and truncate it. */
1031 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
1038 q_end
= buf
+ buf_size
- 1;
1050 /* strcat and truncate. */
1051 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
1056 pstrcpy(buf
+ len
, buf_size
- len
, s
);
1061 static int strstart(const char *str
, const char *val
, const char **ptr
)
1066 while (*q
!= '\0') {
1078 /* extract the basename of a file */
1079 static char *tcc_basename(const char *name
)
1081 char *p
= strchr(name
, 0);
1092 static char *tcc_fileextension (const char *name
)
1094 char *b
= tcc_basename(name
);
1095 char *e
= strrchr(b
, '.');
1096 return e
? e
: strchr(b
, 0);
1100 char *normalize_slashes(char *path
)
1103 for (p
= path
; *p
; ++p
)
1109 char *w32_tcc_lib_path(void)
1111 /* on win32, we suppose the lib and includes are at the location
1113 char path
[1024], *p
;
1114 GetModuleFileNameA(NULL
, path
, sizeof path
);
1115 p
= tcc_basename(normalize_slashes(strlwr(path
)));
1116 if (p
- 5 > path
&& 0 == strncmp(p
- 5, "/bin/", 5))
1121 return strdup(path
);
1125 void set_pages_executable(void *ptr
, unsigned long length
)
1128 unsigned long old_protect
;
1129 VirtualProtect(ptr
, length
, PAGE_EXECUTE_READWRITE
, &old_protect
);
1131 unsigned long start
, end
;
1132 start
= (unsigned long)ptr
& ~(PAGESIZE
- 1);
1133 end
= (unsigned long)ptr
+ length
;
1134 end
= (end
+ PAGESIZE
- 1) & ~(PAGESIZE
- 1);
1135 mprotect((void *)start
, end
- start
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1139 /* memory management */
1143 unsigned malloc_usable_size(void*);
1146 static inline void tcc_free(void *ptr
)
1149 mem_cur_size
-= malloc_usable_size(ptr
);
1154 static void *tcc_malloc(unsigned long size
)
1159 error("memory full");
1161 mem_cur_size
+= malloc_usable_size(ptr
);
1162 if (mem_cur_size
> mem_max_size
)
1163 mem_max_size
= mem_cur_size
;
1168 static void *tcc_mallocz(unsigned long size
)
1171 ptr
= tcc_malloc(size
);
1172 memset(ptr
, 0, size
);
1176 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
1180 mem_cur_size
-= malloc_usable_size(ptr
);
1182 ptr1
= realloc(ptr
, size
);
1184 /* NOTE: count not correct if alloc error, but not critical */
1185 mem_cur_size
+= malloc_usable_size(ptr1
);
1186 if (mem_cur_size
> mem_max_size
)
1187 mem_max_size
= mem_cur_size
;
1192 static char *tcc_strdup(const char *str
)
1195 ptr
= tcc_malloc(strlen(str
) + 1);
1200 #define free(p) use_tcc_free(p)
1201 #define malloc(s) use_tcc_malloc(s)
1202 #define realloc(p, s) use_tcc_realloc(p, s)
1204 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
1211 /* every power of two we double array size */
1212 if ((nb
& (nb
- 1)) == 0) {
1217 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
1219 error("memory full");
1226 static void dynarray_reset(void *pp
, int *n
)
1229 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
1232 tcc_free(*(void**)pp
);
1236 /* symbol allocator */
1237 static Sym
*__sym_malloc(void)
1239 Sym
*sym_pool
, *sym
, *last_sym
;
1242 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
1243 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
1245 last_sym
= sym_free_first
;
1247 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
1248 sym
->next
= last_sym
;
1252 sym_free_first
= last_sym
;
1256 static inline Sym
*sym_malloc(void)
1259 sym
= sym_free_first
;
1261 sym
= __sym_malloc();
1262 sym_free_first
= sym
->next
;
1266 static inline void sym_free(Sym
*sym
)
1268 sym
->next
= sym_free_first
;
1269 sym_free_first
= sym
;
1272 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
1276 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
1277 strcpy(sec
->name
, name
);
1278 sec
->sh_type
= sh_type
;
1279 sec
->sh_flags
= sh_flags
;
1287 sec
->sh_addralign
= 4;
1290 sec
->sh_addralign
= 1;
1293 sec
->sh_addralign
= 32; /* default conservative alignment */
1297 /* only add section if not private */
1298 if (!(sh_flags
& SHF_PRIVATE
)) {
1299 sec
->sh_num
= s1
->nb_sections
;
1300 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
1305 static void free_section(Section
*s
)
1307 if (s
->link
&& (s
->link
->sh_flags
& SHF_PRIVATE
))
1308 free_section(s
->link
);
1309 if (s
->hash
&& (s
->hash
->sh_flags
& SHF_PRIVATE
))
1310 s
->hash
->link
= NULL
, free_section(s
->hash
);
1315 /* realloc section and set its content to zero */
1316 static void section_realloc(Section
*sec
, unsigned long new_size
)
1319 unsigned char *data
;
1321 size
= sec
->data_allocated
;
1324 while (size
< new_size
)
1326 data
= tcc_realloc(sec
->data
, size
);
1328 error("memory full");
1329 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
1331 sec
->data_allocated
= size
;
1334 /* reserve at least 'size' bytes in section 'sec' from
1335 sec->data_offset. */
1336 static void *section_ptr_add(Section
*sec
, unsigned long size
)
1338 unsigned long offset
, offset1
;
1340 offset
= sec
->data_offset
;
1341 offset1
= offset
+ size
;
1342 if (offset1
> sec
->data_allocated
)
1343 section_realloc(sec
, offset1
);
1344 sec
->data_offset
= offset1
;
1345 return sec
->data
+ offset
;
1348 /* return a reference to a section, and create it if it does not
1350 Section
*find_section(TCCState
*s1
, const char *name
)
1354 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1355 sec
= s1
->sections
[i
];
1356 if (!strcmp(name
, sec
->name
))
1359 /* sections are created as PROGBITS */
1360 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
1363 #define SECTION_ABS ((void *)1)
1365 /* update sym->c so that it points to an external symbol in section
1366 'section' with value 'value' */
1367 static void put_extern_sym2(Sym
*sym
, Section
*section
,
1368 unsigned long value
, unsigned long size
,
1369 int can_add_underscore
)
1371 int sym_type
, sym_bind
, sh_num
, info
, other
, attr
;
1376 if (section
== NULL
)
1378 else if (section
== SECTION_ABS
)
1381 sh_num
= section
->sh_num
;
1385 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
1386 sym_type
= STT_FUNC
;
1387 #ifdef TCC_TARGET_PE
1389 attr
= sym
->type
.ref
->r
;
1390 if (FUNC_EXPORT(attr
))
1392 if (FUNC_CALL(attr
) == FUNC_STDCALL
)
1396 sym_type
= STT_OBJECT
;
1399 if (sym
->type
.t
& VT_STATIC
)
1400 sym_bind
= STB_LOCAL
;
1402 sym_bind
= STB_GLOBAL
;
1405 name
= get_tok_str(sym
->v
, NULL
);
1406 #ifdef CONFIG_TCC_BCHECK
1407 if (do_bounds_check
) {
1410 /* XXX: avoid doing that for statics ? */
1411 /* if bound checking is activated, we change some function
1412 names by adding the "__bound" prefix */
1415 /* XXX: we rely only on malloc hooks */
1428 strcpy(buf
, "__bound_");
1436 #ifdef TCC_TARGET_PE
1437 if ((other
& 2) && can_add_underscore
) {
1438 sprintf(buf1
, "_%s@%d", name
, FUNC_ARGS(attr
));
1442 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
1444 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
1447 info
= ELFW(ST_INFO
)(sym_bind
, sym_type
);
1448 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
1450 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
1451 esym
->st_value
= value
;
1452 esym
->st_size
= size
;
1453 esym
->st_shndx
= sh_num
;
1454 esym
->st_other
|= other
;
1458 static void put_extern_sym(Sym
*sym
, Section
*section
,
1459 unsigned long value
, unsigned long size
)
1461 put_extern_sym2(sym
, section
, value
, size
, 1);
1464 /* add a new relocation entry to symbol 'sym' in section 's' */
1465 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
1468 put_extern_sym(sym
, NULL
, 0, 0);
1469 /* now we can add ELF relocation info */
1470 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
1473 static inline int isid(int c
)
1475 return (c
>= 'a' && c
<= 'z') ||
1476 (c
>= 'A' && c
<= 'Z') ||
1480 static inline int isnum(int c
)
1482 return c
>= '0' && c
<= '9';
1485 static inline int isoct(int c
)
1487 return c
>= '0' && c
<= '7';
1490 static inline int toup(int c
)
1492 if (c
>= 'a' && c
<= 'z')
1493 return c
- 'a' + 'A';
1498 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1502 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1505 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1509 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1513 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1520 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1521 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1522 (*f
)->filename
, (*f
)->line_num
);
1523 if (file
->line_num
> 0) {
1524 strcat_printf(buf
, sizeof(buf
),
1525 "%s:%d: ", file
->filename
, file
->line_num
);
1527 strcat_printf(buf
, sizeof(buf
),
1528 "%s: ", file
->filename
);
1531 strcat_printf(buf
, sizeof(buf
),
1535 strcat_printf(buf
, sizeof(buf
), "warning: ");
1536 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1538 if (!s1
->error_func
) {
1539 /* default case: stderr */
1540 fprintf(stderr
, "%s\n", buf
);
1542 s1
->error_func(s1
->error_opaque
, buf
);
1544 if (!is_warning
|| s1
->warn_error
)
1549 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1550 void (*error_func
)(void *opaque
, const char *msg
))
1552 s
->error_opaque
= error_opaque
;
1553 s
->error_func
= error_func
;
1557 /* error without aborting current compilation */
1558 void error_noabort(const char *fmt
, ...)
1560 TCCState
*s1
= tcc_state
;
1564 error1(s1
, 0, fmt
, ap
);
1568 void error(const char *fmt
, ...)
1570 TCCState
*s1
= tcc_state
;
1574 error1(s1
, 0, fmt
, ap
);
1576 /* better than nothing: in some cases, we accept to handle errors */
1577 if (s1
->error_set_jmp_enabled
) {
1578 longjmp(s1
->error_jmp_buf
, 1);
1580 /* XXX: eliminate this someday */
1585 void expect(const char *msg
)
1587 error("%s expected", msg
);
1590 void warning(const char *fmt
, ...)
1592 TCCState
*s1
= tcc_state
;
1599 error1(s1
, 1, fmt
, ap
);
1606 error("'%c' expected", c
);
1610 static void test_lvalue(void)
1612 if (!(vtop
->r
& VT_LVAL
))
1616 /* allocate a new token */
1617 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1619 TokenSym
*ts
, **ptable
;
1622 if (tok_ident
>= SYM_FIRST_ANOM
)
1623 error("memory full");
1625 /* expand token table if needed */
1626 i
= tok_ident
- TOK_IDENT
;
1627 if ((i
% TOK_ALLOC_INCR
) == 0) {
1628 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1630 error("memory full");
1631 table_ident
= ptable
;
1634 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1635 table_ident
[i
] = ts
;
1636 ts
->tok
= tok_ident
++;
1637 ts
->sym_define
= NULL
;
1638 ts
->sym_label
= NULL
;
1639 ts
->sym_struct
= NULL
;
1640 ts
->sym_identifier
= NULL
;
1642 ts
->hash_next
= NULL
;
1643 memcpy(ts
->str
, str
, len
);
1644 ts
->str
[len
] = '\0';
1649 #define TOK_HASH_INIT 1
1650 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1652 /* find a token and add it if not found */
1653 static TokenSym
*tok_alloc(const char *str
, int len
)
1655 TokenSym
*ts
, **pts
;
1661 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1662 h
&= (TOK_HASH_SIZE
- 1);
1664 pts
= &hash_ident
[h
];
1669 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1671 pts
= &(ts
->hash_next
);
1673 return tok_alloc_new(pts
, str
, len
);
1676 /* CString handling */
1678 static void cstr_realloc(CString
*cstr
, int new_size
)
1683 size
= cstr
->size_allocated
;
1685 size
= 8; /* no need to allocate a too small first string */
1686 while (size
< new_size
)
1688 data
= tcc_realloc(cstr
->data_allocated
, size
);
1690 error("memory full");
1691 cstr
->data_allocated
= data
;
1692 cstr
->size_allocated
= size
;
1697 static inline void cstr_ccat(CString
*cstr
, int ch
)
1700 size
= cstr
->size
+ 1;
1701 if (size
> cstr
->size_allocated
)
1702 cstr_realloc(cstr
, size
);
1703 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1707 static void cstr_cat(CString
*cstr
, const char *str
)
1719 /* add a wide char */
1720 static void cstr_wccat(CString
*cstr
, int ch
)
1723 size
= cstr
->size
+ sizeof(nwchar_t
);
1724 if (size
> cstr
->size_allocated
)
1725 cstr_realloc(cstr
, size
);
1726 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
1730 static void cstr_new(CString
*cstr
)
1732 memset(cstr
, 0, sizeof(CString
));
1735 /* free string and reset it to NULL */
1736 static void cstr_free(CString
*cstr
)
1738 tcc_free(cstr
->data_allocated
);
1742 #define cstr_reset(cstr) cstr_free(cstr)
1744 /* XXX: unicode ? */
1745 static void add_char(CString
*cstr
, int c
)
1747 if (c
== '\'' || c
== '\"' || c
== '\\') {
1748 /* XXX: could be more precise if char or string */
1749 cstr_ccat(cstr
, '\\');
1751 if (c
>= 32 && c
<= 126) {
1754 cstr_ccat(cstr
, '\\');
1756 cstr_ccat(cstr
, 'n');
1758 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1759 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1760 cstr_ccat(cstr
, '0' + (c
& 7));
1765 /* XXX: buffer overflow */
1766 /* XXX: float tokens */
1767 char *get_tok_str(int v
, CValue
*cv
)
1769 static char buf
[STRING_MAX_SIZE
+ 1];
1770 static CString cstr_buf
;
1776 /* NOTE: to go faster, we give a fixed buffer for small strings */
1777 cstr_reset(&cstr_buf
);
1778 cstr_buf
.data
= buf
;
1779 cstr_buf
.size_allocated
= sizeof(buf
);
1785 /* XXX: not quite exact, but only useful for testing */
1786 sprintf(p
, "%u", cv
->ui
);
1790 /* XXX: not quite exact, but only useful for testing */
1791 sprintf(p
, "%Lu", cv
->ull
);
1794 cstr_ccat(&cstr_buf
, 'L');
1796 cstr_ccat(&cstr_buf
, '\'');
1797 add_char(&cstr_buf
, cv
->i
);
1798 cstr_ccat(&cstr_buf
, '\'');
1799 cstr_ccat(&cstr_buf
, '\0');
1803 len
= cstr
->size
- 1;
1805 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1806 cstr_ccat(&cstr_buf
, '\0');
1809 cstr_ccat(&cstr_buf
, 'L');
1812 cstr_ccat(&cstr_buf
, '\"');
1814 len
= cstr
->size
- 1;
1816 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1818 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
1820 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
1822 cstr_ccat(&cstr_buf
, '\"');
1823 cstr_ccat(&cstr_buf
, '\0');
1832 return strcpy(p
, "...");
1834 return strcpy(p
, "<<=");
1836 return strcpy(p
, ">>=");
1838 if (v
< TOK_IDENT
) {
1839 /* search in two bytes table */
1853 } else if (v
< tok_ident
) {
1854 return table_ident
[v
- TOK_IDENT
]->str
;
1855 } else if (v
>= SYM_FIRST_ANOM
) {
1856 /* special name for anonymous symbol */
1857 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1859 /* should never happen */
1864 return cstr_buf
.data
;
1867 /* push, without hashing */
1868 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
)
1882 /* find a symbol and return its associated structure. 's' is the top
1883 of the symbol stack */
1884 static Sym
*sym_find2(Sym
*s
, int v
)
1894 /* structure lookup */
1895 static inline Sym
*struct_find(int v
)
1898 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1900 return table_ident
[v
]->sym_struct
;
1903 /* find an identifier */
1904 static inline Sym
*sym_find(int v
)
1907 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1909 return table_ident
[v
]->sym_identifier
;
1912 /* push a given symbol on the symbol stack */
1913 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1922 s
= sym_push2(ps
, v
, type
->t
, c
);
1923 s
->type
.ref
= type
->ref
;
1925 /* don't record fields or anonymous symbols */
1927 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1928 /* record symbol in token array */
1929 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1931 ps
= &ts
->sym_struct
;
1933 ps
= &ts
->sym_identifier
;
1940 /* push a global identifier */
1941 static Sym
*global_identifier_push(int v
, int t
, int c
)
1944 s
= sym_push2(&global_stack
, v
, t
, c
);
1945 /* don't record anonymous symbol */
1946 if (v
< SYM_FIRST_ANOM
) {
1947 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1948 /* modify the top most local identifier, so that
1949 sym_identifier will point to 's' when popped */
1951 ps
= &(*ps
)->prev_tok
;
1958 /* pop symbols until top reaches 'b' */
1959 static void sym_pop(Sym
**ptop
, Sym
*b
)
1969 /* remove symbol in token array */
1971 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1972 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1974 ps
= &ts
->sym_struct
;
1976 ps
= &ts
->sym_identifier
;
1987 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1992 if (strcmp(filename
, "-") == 0)
1993 fd
= 0, filename
= "stdin";
1995 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1996 if ((verbose
== 2 && fd
>= 0) || verbose
== 3)
1997 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
1998 (s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
2001 bf
= tcc_malloc(sizeof(BufferedFile
));
2003 bf
->buf_ptr
= bf
->buffer
;
2004 bf
->buf_end
= bf
->buffer
;
2005 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
2006 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
2008 normalize_slashes(bf
->filename
);
2011 bf
->ifndef_macro
= 0;
2012 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
2013 // printf("opening '%s'\n", filename);
2017 void tcc_close(BufferedFile
*bf
)
2019 total_lines
+= bf
->line_num
;
2024 /* fill input buffer and peek next char */
2025 static int tcc_peekc_slow(BufferedFile
*bf
)
2028 /* only tries to read if really end of buffer */
2029 if (bf
->buf_ptr
>= bf
->buf_end
) {
2031 #if defined(PARSE_DEBUG)
2036 len
= read(bf
->fd
, bf
->buffer
, len
);
2043 bf
->buf_ptr
= bf
->buffer
;
2044 bf
->buf_end
= bf
->buffer
+ len
;
2045 *bf
->buf_end
= CH_EOB
;
2047 if (bf
->buf_ptr
< bf
->buf_end
) {
2048 return bf
->buf_ptr
[0];
2050 bf
->buf_ptr
= bf
->buf_end
;
2055 /* return the current character, handling end of block if necessary
2057 static int handle_eob(void)
2059 return tcc_peekc_slow(file
);
2062 /* read next char from current input file and handle end of input buffer */
2063 static inline void inp(void)
2065 ch
= *(++(file
->buf_ptr
));
2066 /* end of buffer/file handling */
2071 /* handle '\[\r]\n' */
2072 static int handle_stray_noerror(void)
2074 while (ch
== '\\') {
2079 } else if (ch
== '\r') {
2093 static void handle_stray(void)
2095 if (handle_stray_noerror())
2096 error("stray '\\' in program");
2099 /* skip the stray and handle the \\n case. Output an error if
2100 incorrect char after the stray */
2101 static int handle_stray1(uint8_t *p
)
2105 if (p
>= file
->buf_end
) {
2122 /* handle just the EOB case, but not stray */
2123 #define PEEKC_EOB(c, p)\
2134 /* handle the complicated stray case */
2135 #define PEEKC(c, p)\
2140 c = handle_stray1(p);\
2145 /* input with '\[\r]\n' handling. Note that this function cannot
2146 handle other characters after '\', so you cannot call it inside
2147 strings or comments */
2148 static void minp(void)
2156 /* single line C++ comments */
2157 static uint8_t *parse_line_comment(uint8_t *p
)
2165 if (c
== '\n' || c
== CH_EOF
) {
2167 } else if (c
== '\\') {
2176 } else if (c
== '\r') {
2194 static uint8_t *parse_comment(uint8_t *p
)
2200 /* fast skip loop */
2203 if (c
== '\n' || c
== '*' || c
== '\\')
2207 if (c
== '\n' || c
== '*' || c
== '\\')
2211 /* now we can handle all the cases */
2215 } else if (c
== '*') {
2221 } else if (c
== '/') {
2222 goto end_of_comment
;
2223 } else if (c
== '\\') {
2228 /* skip '\[\r]\n', otherwise just skip the stray */
2234 } else if (c
== '\r') {
2251 /* stray, eob or eof */
2256 error("unexpected end of file in comment");
2257 } else if (c
== '\\') {
2269 /* space exlcuding newline */
2270 static inline int is_space(int ch
)
2272 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
2275 static inline void skip_spaces(void)
2277 while (is_space(ch
))
2281 /* parse a string without interpreting escapes */
2282 static uint8_t *parse_pp_string(uint8_t *p
,
2283 int sep
, CString
*str
)
2291 } else if (c
== '\\') {
2296 unterminated_string
:
2297 /* XXX: indicate line number of start of string */
2298 error("missing terminating %c character", sep
);
2299 } else if (c
== '\\') {
2300 /* escape : just skip \[\r]\n */
2305 } else if (c
== '\r') {
2308 expect("'\n' after '\r'");
2311 } else if (c
== CH_EOF
) {
2312 goto unterminated_string
;
2315 cstr_ccat(str
, '\\');
2321 } else if (c
== '\n') {
2324 } else if (c
== '\r') {
2328 cstr_ccat(str
, '\r');
2344 /* skip block of text until #else, #elif or #endif. skip also pairs of
2346 void preprocess_skip(void)
2348 int a
, start_of_line
, c
, in_warn_or_error
;
2355 in_warn_or_error
= 0;
2376 } else if (c
== '\\') {
2377 ch
= file
->buf_ptr
[0];
2378 handle_stray_noerror();
2385 if (in_warn_or_error
)
2387 p
= parse_pp_string(p
, c
, NULL
);
2391 if (in_warn_or_error
)
2398 p
= parse_comment(p
);
2399 } else if (ch
== '/') {
2400 p
= parse_line_comment(p
);
2405 if (start_of_line
) {
2410 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
2412 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
2414 else if (tok
== TOK_ENDIF
)
2416 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
2417 in_warn_or_error
= 1;
2431 /* ParseState handling */
2433 /* XXX: currently, no include file info is stored. Thus, we cannot display
2434 accurate messages if the function or data definition spans multiple
2437 /* save current parse state in 's' */
2438 void save_parse_state(ParseState
*s
)
2440 s
->line_num
= file
->line_num
;
2441 s
->macro_ptr
= macro_ptr
;
2446 /* restore parse state from 's' */
2447 void restore_parse_state(ParseState
*s
)
2449 file
->line_num
= s
->line_num
;
2450 macro_ptr
= s
->macro_ptr
;
2455 /* return the number of additional 'ints' necessary to store the
2457 static inline int tok_ext_size(int t
)
2471 error("unsupported token");
2478 return LDOUBLE_SIZE
/ 4;
2484 /* token string handling */
2486 static inline void tok_str_new(TokenString
*s
)
2490 s
->allocated_len
= 0;
2491 s
->last_line_num
= -1;
2494 static void tok_str_free(int *str
)
2499 static int *tok_str_realloc(TokenString
*s
)
2503 if (s
->allocated_len
== 0) {
2506 len
= s
->allocated_len
* 2;
2508 str
= tcc_realloc(s
->str
, len
* sizeof(int));
2510 error("memory full");
2511 s
->allocated_len
= len
;
2516 static void tok_str_add(TokenString
*s
, int t
)
2522 if (len
>= s
->allocated_len
)
2523 str
= tok_str_realloc(s
);
2528 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2535 /* allocate space for worst case */
2536 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2537 str
= tok_str_realloc(s
);
2546 str
[len
++] = cv
->tab
[0];
2555 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
2556 while ((len
+ nb_words
) > s
->allocated_len
)
2557 str
= tok_str_realloc(s
);
2558 cstr
= (CString
*)(str
+ len
);
2560 cstr
->size
= cv
->cstr
->size
;
2561 cstr
->data_allocated
= NULL
;
2562 cstr
->size_allocated
= cstr
->size
;
2563 memcpy((char *)cstr
+ sizeof(CString
),
2564 cv
->cstr
->data
, cstr
->size
);
2571 #if LDOUBLE_SIZE == 8
2574 str
[len
++] = cv
->tab
[0];
2575 str
[len
++] = cv
->tab
[1];
2577 #if LDOUBLE_SIZE == 12
2579 str
[len
++] = cv
->tab
[0];
2580 str
[len
++] = cv
->tab
[1];
2581 str
[len
++] = cv
->tab
[2];
2582 #elif LDOUBLE_SIZE == 16
2584 str
[len
++] = cv
->tab
[0];
2585 str
[len
++] = cv
->tab
[1];
2586 str
[len
++] = cv
->tab
[2];
2587 str
[len
++] = cv
->tab
[3];
2588 #elif LDOUBLE_SIZE != 8
2589 #error add long double size support
2598 /* add the current parse token in token string 's' */
2599 static void tok_str_add_tok(TokenString
*s
)
2603 /* save line number info */
2604 if (file
->line_num
!= s
->last_line_num
) {
2605 s
->last_line_num
= file
->line_num
;
2606 cval
.i
= s
->last_line_num
;
2607 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2609 tok_str_add2(s
, tok
, &tokc
);
2612 #if LDOUBLE_SIZE == 16
2613 #define LDOUBLE_GET(p, cv) \
2618 #elif LDOUBLE_SIZE == 12
2619 #define LDOUBLE_GET(p, cv) \
2623 #elif LDOUBLE_SIZE == 8
2624 #define LDOUBLE_GET(p, cv) \
2628 #error add long double size support
2632 /* get a token from an integer array and increment pointer
2633 accordingly. we code it as a macro to avoid pointer aliasing. */
2634 #define TOK_GET(t, p, cv) \
2649 cv.cstr = (CString *)p; \
2650 cv.cstr->data = (char *)p + sizeof(CString);\
2651 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2660 case TOK_CLDOUBLE: \
2661 LDOUBLE_GET(p, cv); \
2662 p += LDOUBLE_SIZE / 4; \
2669 /* defines handling */
2670 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2674 s
= sym_push2(&define_stack
, v
, macro_type
, (long)str
);
2675 s
->next
= first_arg
;
2676 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2679 /* undefined a define symbol. Its name is just set to zero */
2680 static void define_undef(Sym
*s
)
2684 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2685 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2689 static inline Sym
*define_find(int v
)
2692 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2694 return table_ident
[v
]->sym_define
;
2697 /* free define stack until top reaches 'b' */
2698 static void free_defines(Sym
*b
)
2706 /* do not free args or predefined defines */
2708 tok_str_free((int *)top
->c
);
2710 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2711 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2719 static Sym
*label_find(int v
)
2722 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2724 return table_ident
[v
]->sym_label
;
2727 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
2730 s
= sym_push2(ptop
, v
, 0, 0);
2732 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
2733 if (ptop
== &global_label_stack
) {
2734 /* modify the top most local identifier, so that
2735 sym_identifier will point to 's' when popped */
2737 ps
= &(*ps
)->prev_tok
;
2744 /* pop labels until element last is reached. Look if any labels are
2745 undefined. Define symbols if '&&label' was used. */
2746 static void label_pop(Sym
**ptop
, Sym
*slast
)
2749 for(s
= *ptop
; s
!= slast
; s
= s1
) {
2751 if (s
->r
== LABEL_DECLARED
) {
2752 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
2753 } else if (s
->r
== LABEL_FORWARD
) {
2754 error("label '%s' used but not defined",
2755 get_tok_str(s
->v
, NULL
));
2758 /* define corresponding symbol. A size of
2760 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
2764 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
2770 /* eval an expression for #if/#elif */
2771 static int expr_preprocess(void)
2777 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2778 next(); /* do macro subst */
2779 if (tok
== TOK_DEFINED
) {
2784 c
= define_find(tok
) != 0;
2789 } else if (tok
>= TOK_IDENT
) {
2790 /* if undefined macro */
2794 tok_str_add_tok(&str
);
2796 tok_str_add(&str
, -1); /* simulate end of file */
2797 tok_str_add(&str
, 0);
2798 /* now evaluate C constant expression */
2799 macro_ptr
= str
.str
;
2803 tok_str_free(str
.str
);
2807 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2808 static void tok_print(int *str
)
2814 TOK_GET(t
, str
, cval
);
2817 printf(" %s", get_tok_str(t
, &cval
));
2823 /* parse after #define */
2824 static void parse_define(void)
2826 Sym
*s
, *first
, **ps
;
2827 int v
, t
, varg
, is_vaargs
, c
;
2832 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2833 /* XXX: should check if same macro (ANSI) */
2836 /* '(' must be just after macro definition for MACRO_FUNC */
2837 c
= file
->buf_ptr
[0];
2839 c
= handle_stray1(file
->buf_ptr
);
2844 while (tok
!= ')') {
2848 if (varg
== TOK_DOTS
) {
2849 varg
= TOK___VA_ARGS__
;
2851 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2855 if (varg
< TOK_IDENT
)
2856 error("badly punctuated parameter list");
2857 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2868 /* EOF testing necessary for '-D' handling */
2869 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2870 tok_str_add2(&str
, tok
, &tokc
);
2873 tok_str_add(&str
, 0);
2875 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2878 define_push(v
, t
, str
.str
, first
);
2881 static inline int hash_cached_include(int type
, const char *filename
)
2883 const unsigned char *s
;
2887 h
= TOK_HASH_FUNC(h
, type
);
2890 h
= TOK_HASH_FUNC(h
, *s
);
2893 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
2897 /* XXX: use a token or a hash table to accelerate matching ? */
2898 static CachedInclude
*search_cached_include(TCCState
*s1
,
2899 int type
, const char *filename
)
2903 h
= hash_cached_include(type
, filename
);
2904 i
= s1
->cached_includes_hash
[h
];
2908 e
= s1
->cached_includes
[i
- 1];
2909 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2916 static inline void add_cached_include(TCCState
*s1
, int type
,
2917 const char *filename
, int ifndef_macro
)
2922 if (search_cached_include(s1
, type
, filename
))
2925 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2927 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2931 strcpy(e
->filename
, filename
);
2932 e
->ifndef_macro
= ifndef_macro
;
2933 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2934 /* add in hash table */
2935 h
= hash_cached_include(type
, filename
);
2936 e
->hash_next
= s1
->cached_includes_hash
[h
];
2937 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
2940 static void pragma_parse(TCCState
*s1
)
2945 if (tok
== TOK_pack
) {
2948 #pragma pack(1) // set
2949 #pragma pack() // reset to default
2950 #pragma pack(push,1) // push & set
2951 #pragma pack(pop) // restore previous
2955 if (tok
== TOK_ASM_pop
) {
2957 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
2959 error("out of pack stack");
2961 s1
->pack_stack_ptr
--;
2965 if (tok
== TOK_ASM_push
) {
2967 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
2969 s1
->pack_stack_ptr
++;
2972 if (tok
!= TOK_CINT
) {
2974 error("invalid pack pragma");
2977 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
2981 *s1
->pack_stack_ptr
= val
;
2987 /* is_bof is true if first non space token at beginning of file */
2988 static void preprocess(int is_bof
)
2990 TCCState
*s1
= tcc_state
;
2991 int size
, i
, c
, n
, saved_parse_flags
;
2998 saved_parse_flags
= parse_flags
;
2999 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
3000 PARSE_FLAG_LINEFEED
;
3010 s
= define_find(tok
);
3011 /* undefine symbol by putting an invalid name */
3016 case TOK_INCLUDE_NEXT
:
3017 ch
= file
->buf_ptr
[0];
3018 /* XXX: incorrect if comments : use next_nomacro with a special mode */
3023 } else if (ch
== '\"') {
3028 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
3029 if ((q
- buf
) < sizeof(buf
) - 1)
3032 if (handle_stray_noerror() == 0)
3040 /* eat all spaces and comments after include */
3041 /* XXX: slightly incorrect */
3042 while (ch1
!= '\n' && ch1
!= CH_EOF
)
3046 /* computed #include : either we have only strings or
3047 we have anything enclosed in '<>' */
3050 if (tok
== TOK_STR
) {
3051 while (tok
!= TOK_LINEFEED
) {
3052 if (tok
!= TOK_STR
) {
3054 error("'#include' expects \"FILENAME\" or <FILENAME>");
3056 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
3062 while (tok
!= TOK_LINEFEED
) {
3063 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
3067 /* check syntax and remove '<>' */
3068 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
3069 goto include_syntax
;
3070 memmove(buf
, buf
+ 1, len
- 2);
3071 buf
[len
- 2] = '\0';
3076 e
= search_cached_include(s1
, c
, buf
);
3077 if (e
&& define_find(e
->ifndef_macro
)) {
3078 /* no need to parse the include because the 'ifndef macro'
3081 printf("%s: skipping %s\n", file
->filename
, buf
);
3084 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
3085 error("#include recursion too deep");
3086 /* push current file in stack */
3087 /* XXX: fix current line init */
3088 *s1
->include_stack_ptr
++ = file
;
3090 /* first search in current dir if "header.h" */
3091 size
= tcc_basename(file
->filename
) - file
->filename
;
3092 if (size
> sizeof(buf1
) - 1)
3093 size
= sizeof(buf1
) - 1;
3094 memcpy(buf1
, file
->filename
, size
);
3096 pstrcat(buf1
, sizeof(buf1
), buf
);
3097 f
= tcc_open(s1
, buf1
);
3099 if (tok
== TOK_INCLUDE_NEXT
)
3105 /* now search in all the include paths */
3106 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
3107 for(i
= 0; i
< n
; i
++) {
3109 if (i
< s1
->nb_include_paths
)
3110 path
= s1
->include_paths
[i
];
3112 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
3113 pstrcpy(buf1
, sizeof(buf1
), path
);
3114 pstrcat(buf1
, sizeof(buf1
), "/");
3115 pstrcat(buf1
, sizeof(buf1
), buf
);
3116 f
= tcc_open(s1
, buf1
);
3118 if (tok
== TOK_INCLUDE_NEXT
)
3124 --s1
->include_stack_ptr
;
3125 error("include file '%s' not found", buf
);
3129 printf("%s: including %s\n", file
->filename
, buf1
);
3132 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
3134 /* add include file debug info */
3136 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
3138 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
3139 ch
= file
->buf_ptr
[0];
3147 c
= expr_preprocess();
3153 if (tok
< TOK_IDENT
)
3154 error("invalid argument for '#if%sdef'", c
? "n" : "");
3158 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
3160 file
->ifndef_macro
= tok
;
3163 c
= (define_find(tok
) != 0) ^ c
;
3165 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
3166 error("memory full");
3167 *s1
->ifdef_stack_ptr
++ = c
;
3170 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3171 error("#else without matching #if");
3172 if (s1
->ifdef_stack_ptr
[-1] & 2)
3173 error("#else after #else");
3174 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
3177 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3178 error("#elif without matching #if");
3179 c
= s1
->ifdef_stack_ptr
[-1];
3181 error("#elif after #else");
3182 /* last #if/#elif expression was true: we skip */
3185 c
= expr_preprocess();
3186 s1
->ifdef_stack_ptr
[-1] = c
;
3196 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
3197 error("#endif without matching #if");
3198 s1
->ifdef_stack_ptr
--;
3199 /* '#ifndef macro' was at the start of file. Now we check if
3200 an '#endif' is exactly at the end of file */
3201 if (file
->ifndef_macro
&&
3202 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
3203 file
->ifndef_macro_saved
= file
->ifndef_macro
;
3204 /* need to set to zero to avoid false matches if another
3205 #ifndef at middle of file */
3206 file
->ifndef_macro
= 0;
3207 while (tok
!= TOK_LINEFEED
)
3209 tok_flags
|= TOK_FLAG_ENDIF
;
3215 if (tok
!= TOK_CINT
)
3217 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
3219 if (tok
!= TOK_LINEFEED
) {
3222 pstrcpy(file
->filename
, sizeof(file
->filename
),
3223 (char *)tokc
.cstr
->data
);
3229 ch
= file
->buf_ptr
[0];
3232 while (ch
!= '\n' && ch
!= CH_EOF
) {
3233 if ((q
- buf
) < sizeof(buf
) - 1)
3236 if (handle_stray_noerror() == 0)
3243 error("#error %s", buf
);
3245 warning("#warning %s", buf
);
3251 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
3252 /* '!' is ignored to allow C scripts. numbers are ignored
3253 to emulate cpp behaviour */
3255 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
3256 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
3260 /* ignore other preprocess commands or #! for C scripts */
3261 while (tok
!= TOK_LINEFEED
)
3264 parse_flags
= saved_parse_flags
;
3267 /* evaluate escape codes in a string. */
3268 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
3283 case '0': case '1': case '2': case '3':
3284 case '4': case '5': case '6': case '7':
3285 /* at most three octal digits */
3290 n
= n
* 8 + c
- '0';
3294 n
= n
* 8 + c
- '0';
3299 goto add_char_nonext
;
3307 if (c
>= 'a' && c
<= 'f')
3309 else if (c
>= 'A' && c
<= 'F')
3319 goto add_char_nonext
;
3343 goto invalid_escape
;
3353 if (c
>= '!' && c
<= '~')
3354 warning("unknown escape sequence: \'\\%c\'", c
);
3356 warning("unknown escape sequence: \'\\x%x\'", c
);
3363 cstr_ccat(outstr
, c
);
3365 cstr_wccat(outstr
, c
);
3367 /* add a trailing '\0' */
3369 cstr_ccat(outstr
, '\0');
3371 cstr_wccat(outstr
, '\0');
3374 /* we use 64 bit numbers */
3377 /* bn = (bn << shift) | or_val */
3378 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
3382 for(i
=0;i
<BN_SIZE
;i
++) {
3384 bn
[i
] = (v
<< shift
) | or_val
;
3385 or_val
= v
>> (32 - shift
);
3389 void bn_zero(unsigned int *bn
)
3392 for(i
=0;i
<BN_SIZE
;i
++) {
3397 /* parse number in null terminated string 'p' and return it in the
3399 void parse_number(const char *p
)
3401 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
3403 unsigned int bn
[BN_SIZE
];
3414 goto float_frac_parse
;
3415 } else if (t
== '0') {
3416 if (ch
== 'x' || ch
== 'X') {
3420 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
3426 /* parse all digits. cannot check octal numbers at this stage
3427 because of floating point constants */
3429 if (ch
>= 'a' && ch
<= 'f')
3431 else if (ch
>= 'A' && ch
<= 'F')
3439 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
3441 error("number too long");
3447 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
3448 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
3450 /* NOTE: strtox should support that for hexa numbers, but
3451 non ISOC99 libcs do not support it, so we prefer to do
3453 /* hexadecimal or binary floats */
3454 /* XXX: handle overflows */
3466 } else if (t
>= 'a') {
3468 } else if (t
>= 'A') {
3473 bn_lshift(bn
, shift
, t
);
3480 if (t
>= 'a' && t
<= 'f') {
3482 } else if (t
>= 'A' && t
<= 'F') {
3484 } else if (t
>= '0' && t
<= '9') {
3490 error("invalid digit");
3491 bn_lshift(bn
, shift
, t
);
3496 if (ch
!= 'p' && ch
!= 'P')
3503 } else if (ch
== '-') {
3507 if (ch
< '0' || ch
> '9')
3508 expect("exponent digits");
3509 while (ch
>= '0' && ch
<= '9') {
3510 exp_val
= exp_val
* 10 + ch
- '0';
3513 exp_val
= exp_val
* s
;
3515 /* now we can generate the number */
3516 /* XXX: should patch directly float number */
3517 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
3518 d
= ldexp(d
, exp_val
- frac_bits
);
3523 /* float : should handle overflow */
3525 } else if (t
== 'L') {
3528 /* XXX: not large enough */
3529 tokc
.ld
= (long double)d
;
3535 /* decimal floats */
3537 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3542 while (ch
>= '0' && ch
<= '9') {
3543 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3549 if (ch
== 'e' || ch
== 'E') {
3550 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3554 if (ch
== '-' || ch
== '+') {
3555 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3560 if (ch
< '0' || ch
> '9')
3561 expect("exponent digits");
3562 while (ch
>= '0' && ch
<= '9') {
3563 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3575 tokc
.f
= strtof(token_buf
, NULL
);
3576 } else if (t
== 'L') {
3579 tokc
.ld
= strtold(token_buf
, NULL
);
3582 tokc
.d
= strtod(token_buf
, NULL
);
3586 unsigned long long n
, n1
;
3589 /* integer number */
3592 if (b
== 10 && *q
== '0') {
3599 /* no need for checks except for base 10 / 8 errors */
3602 } else if (t
>= 'a') {
3604 } else if (t
>= 'A') {
3609 error("invalid digit");
3613 /* detect overflow */
3614 /* XXX: this test is not reliable */
3616 error("integer constant overflow");
3619 /* XXX: not exactly ANSI compliant */
3620 if ((n
& 0xffffffff00000000LL
) != 0) {
3625 } else if (n
> 0x7fffffff) {
3636 error("three 'l's in integer constant");
3639 if (tok
== TOK_CINT
)
3641 else if (tok
== TOK_CUINT
)
3645 } else if (t
== 'U') {
3647 error("two 'u's in integer constant");
3649 if (tok
== TOK_CINT
)
3651 else if (tok
== TOK_CLLONG
)
3658 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
3664 error("invalid number\n");
3668 #define PARSE2(c1, tok1, c2, tok2) \
3679 /* return next token without macro substitution */
3680 static inline void next_nomacro1(void)
3687 cstr_reset(&tok_spaces
);
3697 cstr_ccat(&tok_spaces
, c
);
3702 /* first look if it is in fact an end of buffer */
3703 if (p
>= file
->buf_end
) {
3707 if (p
>= file
->buf_end
)
3720 TCCState
*s1
= tcc_state
;
3721 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
3722 && !(tok_flags
& TOK_FLAG_EOF
)) {
3723 tok_flags
|= TOK_FLAG_EOF
;
3725 goto keep_tok_flags
;
3726 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
3727 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3728 /* no include left : end of file. */
3731 tok_flags
&= ~TOK_FLAG_EOF
;
3732 /* pop include file */
3734 /* test if previous '#endif' was after a #ifdef at
3736 if (tok_flags
& TOK_FLAG_ENDIF
) {
3738 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3740 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3741 file
->ifndef_macro_saved
);
3744 /* add end of include file debug info */
3746 put_stabd(N_EINCL
, 0, 0);
3748 /* pop include stack */
3750 s1
->include_stack_ptr
--;
3751 file
= *s1
->include_stack_ptr
;
3760 tok_flags
|= TOK_FLAG_BOL
;
3762 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
3765 goto keep_tok_flags
;
3770 if ((tok_flags
& TOK_FLAG_BOL
) &&
3771 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3773 preprocess(tok_flags
& TOK_FLAG_BOF
);
3779 tok
= TOK_TWOSHARPS
;
3781 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
3782 p
= parse_line_comment(p
- 1);
3791 case 'a': case 'b': case 'c': case 'd':
3792 case 'e': case 'f': case 'g': case 'h':
3793 case 'i': case 'j': case 'k': case 'l':
3794 case 'm': case 'n': case 'o': case 'p':
3795 case 'q': case 'r': case 's': case 't':
3796 case 'u': case 'v': case 'w': case 'x':
3798 case 'A': case 'B': case 'C': case 'D':
3799 case 'E': case 'F': case 'G': case 'H':
3800 case 'I': case 'J': case 'K':
3801 case 'M': case 'N': case 'O': case 'P':
3802 case 'Q': case 'R': case 'S': case 'T':
3803 case 'U': case 'V': case 'W': case 'X':
3809 h
= TOK_HASH_FUNC(h
, c
);
3813 if (!isidnum_table
[c
-CH_EOF
])
3815 h
= TOK_HASH_FUNC(h
, c
);
3822 /* fast case : no stray found, so we have the full token
3823 and we have already hashed it */
3825 h
&= (TOK_HASH_SIZE
- 1);
3826 pts
= &hash_ident
[h
];
3831 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3833 pts
= &(ts
->hash_next
);
3835 ts
= tok_alloc_new(pts
, p1
, len
);
3839 cstr_reset(&tokcstr
);
3842 cstr_ccat(&tokcstr
, *p1
);
3848 while (isidnum_table
[c
-CH_EOF
]) {
3849 cstr_ccat(&tokcstr
, c
);
3852 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3858 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3860 goto parse_ident_fast
;
3863 if (c
== '\'' || c
== '\"') {
3867 cstr_reset(&tokcstr
);
3868 cstr_ccat(&tokcstr
, 'L');
3869 goto parse_ident_slow
;
3873 case '0': case '1': case '2': case '3':
3874 case '4': case '5': case '6': case '7':
3877 cstr_reset(&tokcstr
);
3878 /* after the first digit, accept digits, alpha, '.' or sign if
3879 prefixed by 'eEpP' */
3883 cstr_ccat(&tokcstr
, c
);
3885 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3886 ((c
== '+' || c
== '-') &&
3887 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3890 /* We add a trailing '\0' to ease parsing */
3891 cstr_ccat(&tokcstr
, '\0');
3892 tokc
.cstr
= &tokcstr
;
3896 /* special dot handling because it can also start a number */
3899 cstr_reset(&tokcstr
);
3900 cstr_ccat(&tokcstr
, '.');
3902 } else if (c
== '.') {
3922 /* parse the string */
3924 p
= parse_pp_string(p
, sep
, &str
);
3925 cstr_ccat(&str
, '\0');
3927 /* eval the escape (should be done as TOK_PPNUM) */
3928 cstr_reset(&tokcstr
);
3929 parse_escape_string(&tokcstr
, str
.data
, is_long
);
3934 /* XXX: make it portable */
3938 char_size
= sizeof(nwchar_t
);
3939 if (tokcstr
.size
<= char_size
)
3940 error("empty character constant");
3941 if (tokcstr
.size
> 2 * char_size
)
3942 warning("multi-character character constant");
3944 tokc
.i
= *(int8_t *)tokcstr
.data
;
3947 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
3951 tokc
.cstr
= &tokcstr
;
3965 } else if (c
== '<') {
3983 } else if (c
== '>') {
4001 } else if (c
== '=') {
4014 } else if (c
== '=') {
4027 } else if (c
== '=') {
4040 } else if (c
== '=') {
4043 } else if (c
== '>') {
4051 PARSE2('!', '!', '=', TOK_NE
)
4052 PARSE2('=', '=', '=', TOK_EQ
)
4053 PARSE2('*', '*', '=', TOK_A_MUL
)
4054 PARSE2('%', '%', '=', TOK_A_MOD
)
4055 PARSE2('^', '^', '=', TOK_A_XOR
)
4057 /* comments or operator */
4061 p
= parse_comment(p
);
4063 } else if (c
== '/') {
4064 p
= parse_line_comment(p
);
4066 } else if (c
== '=') {
4086 case '$': /* only used in assembler */
4087 case '@': /* dito */
4092 error("unrecognized character \\x%02x", c
);
4098 #if defined(PARSE_DEBUG)
4099 printf("token = %s\n", get_tok_str(tok
, &tokc
));
4103 /* return next token without macro substitution. Can read input from
4105 static void next_nomacro(void)
4111 TOK_GET(tok
, macro_ptr
, tokc
);
4112 if (tok
== TOK_LINENUM
) {
4113 file
->line_num
= tokc
.i
;
4122 /* substitute args in macro_str and return allocated string */
4123 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
4125 int *st
, last_tok
, t
, notfirst
;
4134 TOK_GET(t
, macro_str
, cval
);
4139 TOK_GET(t
, macro_str
, cval
);
4142 s
= sym_find2(args
, t
);
4149 cstr_ccat(&cstr
, ' ');
4150 TOK_GET(t
, st
, cval
);
4151 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
4156 cstr_ccat(&cstr
, '\0');
4158 printf("stringize: %s\n", (char *)cstr
.data
);
4162 tok_str_add2(&str
, TOK_STR
, &cval
);
4165 tok_str_add2(&str
, t
, &cval
);
4167 } else if (t
>= TOK_IDENT
) {
4168 s
= sym_find2(args
, t
);
4171 /* if '##' is present before or after, no arg substitution */
4172 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
4173 /* special case for var arg macros : ## eats the
4174 ',' if empty VA_ARGS variable. */
4175 /* XXX: test of the ',' is not 100%
4176 reliable. should fix it to avoid security
4178 if (gnu_ext
&& s
->type
.t
&&
4179 last_tok
== TOK_TWOSHARPS
&&
4180 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
4182 /* suppress ',' '##' */
4185 /* suppress '##' and add variable */
4193 TOK_GET(t1
, st
, cval
);
4196 tok_str_add2(&str
, t1
, &cval
);
4200 /* NOTE: the stream cannot be read when macro
4201 substituing an argument */
4202 macro_subst(&str
, nested_list
, st
, NULL
);
4205 tok_str_add(&str
, t
);
4208 tok_str_add2(&str
, t
, &cval
);
4212 tok_str_add(&str
, 0);
4216 static char const ab_month_name
[12][4] =
4218 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4219 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4222 /* do macro substitution of current token with macro 's' and add
4223 result to (tok_str,tok_len). 'nested_list' is the list of all
4224 macros we got inside to avoid recursing. Return non zero if no
4225 substitution needs to be done */
4226 static int macro_subst_tok(TokenString
*tok_str
,
4227 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
4229 Sym
*args
, *sa
, *sa1
;
4230 int mstr_allocated
, parlevel
, *mstr
, t
, t1
;
4237 /* if symbol is a macro, prepare substitution */
4238 /* special macros */
4239 if (tok
== TOK___LINE__
) {
4240 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
4244 } else if (tok
== TOK___FILE__
) {
4245 cstrval
= file
->filename
;
4247 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
4252 tm
= localtime(&ti
);
4253 if (tok
== TOK___DATE__
) {
4254 snprintf(buf
, sizeof(buf
), "%s %2d %d",
4255 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
4257 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
4258 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
4265 cstr_cat(&cstr
, cstrval
);
4266 cstr_ccat(&cstr
, '\0');
4268 tok_str_add2(tok_str
, t1
, &cval
);
4273 if (s
->type
.t
== MACRO_FUNC
) {
4274 /* NOTE: we do not use next_nomacro to avoid eating the
4275 next token. XXX: find better solution */
4279 if (t
== 0 && can_read_stream
) {
4280 /* end of macro stream: we must look at the token
4281 after in the file */
4282 struct macro_level
*ml
= *can_read_stream
;
4288 *can_read_stream
= ml
-> prev
;
4293 /* XXX: incorrect with comments */
4294 ch
= file
->buf_ptr
[0];
4295 while (is_space(ch
) || ch
== '\n')
4299 if (t
!= '(') /* no macro subst */
4302 /* argument macro */
4307 /* NOTE: empty args are allowed, except if no args */
4309 /* handle '()' case */
4310 if (!args
&& !sa
&& tok
== ')')
4313 error("macro '%s' used with too many args",
4314 get_tok_str(s
->v
, 0));
4317 /* NOTE: non zero sa->t indicates VA_ARGS */
4318 while ((parlevel
> 0 ||
4320 (tok
!= ',' || sa
->type
.t
))) &&
4324 else if (tok
== ')')
4326 if (tok
!= TOK_LINEFEED
)
4327 tok_str_add2(&str
, tok
, &tokc
);
4330 tok_str_add(&str
, 0);
4331 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (long)str
.str
);
4334 /* special case for gcc var args: add an empty
4335 var arg argument if it is omitted */
4336 if (sa
&& sa
->type
.t
&& gnu_ext
)
4346 error("macro '%s' used with too few args",
4347 get_tok_str(s
->v
, 0));
4350 /* now subst each arg */
4351 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
4356 tok_str_free((int *)sa
->c
);
4362 sym_push2(nested_list
, s
->v
, 0, 0);
4363 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
4364 /* pop nested defined symbol */
4366 *nested_list
= sa1
->prev
;
4374 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4375 return the resulting string (which must be freed). */
4376 static inline int *macro_twosharps(const int *macro_str
)
4379 const int *macro_ptr1
, *start_macro_ptr
, *ptr
, *saved_macro_ptr
;
4381 const char *p1
, *p2
;
4383 TokenString macro_str1
;
4386 start_macro_ptr
= macro_str
;
4387 /* we search the first '##' */
4389 macro_ptr1
= macro_str
;
4390 TOK_GET(t
, macro_str
, cval
);
4391 /* nothing more to do if end of string */
4394 if (*macro_str
== TOK_TWOSHARPS
)
4398 /* we saw '##', so we need more processing to handle it */
4400 tok_str_new(¯o_str1
);
4404 /* add all tokens seen so far */
4405 for(ptr
= start_macro_ptr
; ptr
< macro_ptr1
;) {
4406 TOK_GET(t
, ptr
, cval
);
4407 tok_str_add2(¯o_str1
, t
, &cval
);
4409 saved_macro_ptr
= macro_ptr
;
4410 /* XXX: get rid of the use of macro_ptr here */
4411 macro_ptr
= (int *)macro_str
;
4413 while (*macro_ptr
== TOK_TWOSHARPS
) {
4415 macro_ptr1
= macro_ptr
;
4418 TOK_GET(t
, macro_ptr
, cval
);
4419 /* We concatenate the two tokens if we have an
4420 identifier or a preprocessing number */
4422 p1
= get_tok_str(tok
, &tokc
);
4423 cstr_cat(&cstr
, p1
);
4424 p2
= get_tok_str(t
, &cval
);
4425 cstr_cat(&cstr
, p2
);
4426 cstr_ccat(&cstr
, '\0');
4428 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
4429 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
4430 if (tok
== TOK_PPNUM
) {
4431 /* if number, then create a number token */
4432 /* NOTE: no need to allocate because
4433 tok_str_add2() does it */
4434 cstr_reset(&tokcstr
);
4437 tokc
.cstr
= &tokcstr
;
4439 /* if identifier, we must do a test to
4440 validate we have a correct identifier */
4441 if (t
== TOK_PPNUM
) {
4451 if (!isnum(c
) && !isid(c
))
4455 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
4456 tok
= ts
->tok
; /* modify current token */
4459 const char *str
= cstr
.data
;
4460 const unsigned char *q
;
4462 /* we look for a valid token */
4463 /* XXX: do more extensive checks */
4464 if (!strcmp(str
, ">>=")) {
4466 } else if (!strcmp(str
, "<<=")) {
4468 } else if (strlen(str
) == 2) {
4469 /* search in two bytes table */
4474 if (q
[0] == str
[0] && q
[1] == str
[1])
4481 /* NOTE: because get_tok_str use a static buffer,
4484 p1
= get_tok_str(tok
, &tokc
);
4485 cstr_cat(&cstr
, p1
);
4486 cstr_ccat(&cstr
, '\0');
4487 p2
= get_tok_str(t
, &cval
);
4488 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
4489 /* cannot merge tokens: just add them separately */
4490 tok_str_add2(¯o_str1
, tok
, &tokc
);
4491 /* XXX: free associated memory ? */
4498 tok_str_add2(¯o_str1
, tok
, &tokc
);
4503 macro_ptr
= (int *)saved_macro_ptr
;
4505 tok_str_add(¯o_str1
, 0);
4506 return macro_str1
.str
;
4510 /* do macro substitution of macro_str and add result to
4511 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4512 inside to avoid recursing. */
4513 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
4514 const int *macro_str
, struct macro_level
** can_read_stream
)
4521 struct macro_level ml
;
4523 /* first scan for '##' operator handling */
4525 macro_str1
= macro_twosharps(ptr
);
4529 /* NOTE: ptr == NULL can only happen if tokens are read from
4530 file stream due to a macro function call */
4533 TOK_GET(t
, ptr
, cval
);
4538 /* if nested substitution, do nothing */
4539 if (sym_find2(*nested_list
, t
))
4542 if (can_read_stream
)
4543 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
4544 macro_ptr
= (int *)ptr
;
4546 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
4547 ptr
= (int *)macro_ptr
;
4549 if (can_read_stream
&& *can_read_stream
== &ml
)
4550 *can_read_stream
= ml
.prev
;
4555 tok_str_add2(tok_str
, t
, &cval
);
4559 tok_str_free(macro_str1
);
4562 /* return next token with macro substitution */
4563 static void next(void)
4565 Sym
*nested_list
, *s
;
4567 struct macro_level
*ml
;
4572 /* if not reading from macro substituted string, then try
4573 to substitute macros */
4574 if (tok
>= TOK_IDENT
&&
4575 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
4576 s
= define_find(tok
);
4578 /* we have a macro: we try to substitute */
4582 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
4583 /* substitution done, NOTE: maybe empty */
4584 tok_str_add(&str
, 0);
4585 macro_ptr
= str
.str
;
4586 macro_ptr_allocated
= str
.str
;
4593 /* end of macro or end of unget buffer */
4594 if (unget_buffer_enabled
) {
4595 macro_ptr
= unget_saved_macro_ptr
;
4596 unget_buffer_enabled
= 0;
4598 /* end of macro string: free it */
4599 tok_str_free(macro_ptr_allocated
);
4606 /* convert preprocessor tokens into C tokens */
4607 if (tok
== TOK_PPNUM
&&
4608 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
4609 parse_number((char *)tokc
.cstr
->data
);
4613 /* push back current token and set current token to 'last_tok'. Only
4614 identifier case handled for labels. */
4615 static inline void unget_tok(int last_tok
)
4619 unget_saved_macro_ptr
= macro_ptr
;
4620 unget_buffer_enabled
= 1;
4621 q
= unget_saved_buffer
;
4624 n
= tok_ext_size(tok
) - 1;
4627 *q
= 0; /* end of token string */
4632 void swap(int *p
, int *q
)
4640 void vsetc(CType
*type
, int r
, CValue
*vc
)
4644 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4645 error("memory full");
4646 /* cannot let cpu flags if other instruction are generated. Also
4647 avoid leaving VT_JMP anywhere except on the top of the stack
4648 because it would complicate the code generator. */
4649 if (vtop
>= vstack
) {
4650 v
= vtop
->r
& VT_VALMASK
;
4651 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
4657 vtop
->r2
= VT_CONST
;
4661 /* push integer constant */
4666 vsetc(&int_type
, VT_CONST
, &cval
);
4669 /* push long long constant */
4670 void vpushll(long long v
)
4676 vsetc(&ctype
, VT_CONST
, &cval
);
4679 /* Return a static symbol pointing to a section */
4680 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
4681 unsigned long offset
, unsigned long size
)
4687 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
4688 sym
->type
.ref
= type
->ref
;
4689 sym
->r
= VT_CONST
| VT_SYM
;
4690 put_extern_sym(sym
, sec
, offset
, size
);
4694 /* push a reference to a section offset by adding a dummy symbol */
4695 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
4700 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4701 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
4704 /* define a new external reference to a symbol 'v' of type 'u' */
4705 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
4711 /* push forward reference */
4712 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
4713 s
->type
.ref
= type
->ref
;
4714 s
->r
= r
| VT_CONST
| VT_SYM
;
4719 /* define a new external reference to a symbol 'v' of type 'u' */
4720 static Sym
*external_sym(int v
, CType
*type
, int r
)
4726 /* push forward reference */
4727 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
4728 s
->type
.t
|= VT_EXTERN
;
4730 if (!is_compatible_types(&s
->type
, type
))
4731 error("incompatible types for redefinition of '%s'",
4732 get_tok_str(v
, NULL
));
4737 /* push a reference to global symbol v */
4738 static void vpush_global_sym(CType
*type
, int v
)
4743 sym
= external_global_sym(v
, type
, 0);
4745 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4749 void vset(CType
*type
, int r
, int v
)
4754 vsetc(type
, r
, &cval
);
4757 void vseti(int r
, int v
)
4773 void vpushv(SValue
*v
)
4775 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4776 error("memory full");
4786 /* save r to the memory stack, and mark it as being free */
4787 void save_reg(int r
)
4789 int l
, saved
, size
, align
;
4793 /* modify all stack values */
4796 for(p
=vstack
;p
<=vtop
;p
++) {
4797 if ((p
->r
& VT_VALMASK
) == r
||
4798 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
4799 /* must save value on stack if not already done */
4801 /* NOTE: must reload 'r' because r might be equal to r2 */
4802 r
= p
->r
& VT_VALMASK
;
4803 /* store register in the stack */
4805 if ((p
->r
& VT_LVAL
) ||
4806 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
4807 #ifdef TCC_TARGET_X86_64
4808 type
= &char_pointer_type
;
4812 size
= type_size(type
, &align
);
4813 loc
= (loc
- size
) & -align
;
4814 sv
.type
.t
= type
->t
;
4815 sv
.r
= VT_LOCAL
| VT_LVAL
;
4818 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
4819 /* x86 specific: need to pop fp register ST0 if saved */
4820 if (r
== TREG_ST0
) {
4821 o(0xd9dd); /* fstp %st(1) */
4824 #ifndef TCC_TARGET_X86_64
4825 /* special long long case */
4826 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
4834 /* mark that stack entry as being saved on the stack */
4835 if (p
->r
& VT_LVAL
) {
4836 /* also clear the bounded flag because the
4837 relocation address of the function was stored in
4839 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4841 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4849 /* find a register of class 'rc2' with at most one reference on stack.
4850 * If none, call get_reg(rc) */
4851 int get_reg_ex(int rc
, int rc2
)
4856 for(r
=0;r
<NB_REGS
;r
++) {
4857 if (reg_classes
[r
] & rc2
) {
4860 for(p
= vstack
; p
<= vtop
; p
++) {
4861 if ((p
->r
& VT_VALMASK
) == r
||
4862 (p
->r2
& VT_VALMASK
) == r
)
4872 /* find a free register of class 'rc'. If none, save one register */
4878 /* find a free register */
4879 for(r
=0;r
<NB_REGS
;r
++) {
4880 if (reg_classes
[r
] & rc
) {
4881 for(p
=vstack
;p
<=vtop
;p
++) {
4882 if ((p
->r
& VT_VALMASK
) == r
||
4883 (p
->r2
& VT_VALMASK
) == r
)
4891 /* no register left : free the first one on the stack (VERY
4892 IMPORTANT to start from the bottom to ensure that we don't
4893 spill registers used in gen_opi()) */
4894 for(p
=vstack
;p
<=vtop
;p
++) {
4895 r
= p
->r
& VT_VALMASK
;
4896 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4898 /* also look at second register (if long long) */
4899 r
= p
->r2
& VT_VALMASK
;
4900 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4906 /* Should never comes here */
4910 /* save registers up to (vtop - n) stack entry */
4911 void save_regs(int n
)
4916 for(p
= vstack
;p
<= p1
; p
++) {
4917 r
= p
->r
& VT_VALMASK
;
4924 /* move register 's' to 'r', and flush previous value of r to memory
4926 void move_reg(int r
, int s
)
4939 /* get address of vtop (vtop MUST BE an lvalue) */
4942 vtop
->r
&= ~VT_LVAL
;
4943 /* tricky: if saved lvalue, then we can go back to lvalue */
4944 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4945 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4948 #ifdef CONFIG_TCC_BCHECK
4949 /* generate lvalue bound code */
4955 vtop
->r
&= ~VT_MUSTBOUND
;
4956 /* if lvalue, then use checking code before dereferencing */
4957 if (vtop
->r
& VT_LVAL
) {
4958 /* if not VT_BOUNDED value, then make one */
4959 if (!(vtop
->r
& VT_BOUNDED
)) {
4960 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4961 /* must save type because we must set it to int to get pointer */
4963 vtop
->type
.t
= VT_INT
;
4966 gen_bounded_ptr_add();
4967 vtop
->r
|= lval_type
;
4970 /* then check for dereferencing */
4971 gen_bounded_ptr_deref();
4976 /* store vtop a register belonging to class 'rc'. lvalues are
4977 converted to values. Cannot be used if cannot be converted to
4978 register value (such as structures). */
4981 int r
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4983 /* NOTE: get_reg can modify vstack[] */
4984 if (vtop
->type
.t
& VT_BITFIELD
) {
4987 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4988 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4989 /* remove bit field info to avoid loops */
4990 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4991 /* cast to int to propagate signedness in following ops */
4992 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
4997 if((vtop
->type
.t
& VT_UNSIGNED
) ||
4998 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
4999 type
.t
|= VT_UNSIGNED
;
5001 /* generate shifts */
5002 vpushi(bits
- (bit_pos
+ bit_size
));
5004 vpushi(bits
- bit_size
);
5005 /* NOTE: transformed to SHR if unsigned */
5009 if (is_float(vtop
->type
.t
) &&
5010 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
5013 unsigned long offset
;
5014 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5018 /* XXX: unify with initializers handling ? */
5019 /* CPUs usually cannot use float constants, so we store them
5020 generically in data segment */
5021 size
= type_size(&vtop
->type
, &align
);
5022 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
5023 data_section
->data_offset
= offset
;
5024 /* XXX: not portable yet */
5025 #if defined(__i386__) || defined(__x86_64__)
5026 /* Zero pad x87 tenbyte long doubles */
5027 if (size
== LDOUBLE_SIZE
)
5028 vtop
->c
.tab
[2] &= 0xffff;
5030 ptr
= section_ptr_add(data_section
, size
);
5032 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
5036 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
5040 ptr
[i
] = vtop
->c
.tab
[i
];
5041 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
5042 vtop
->r
|= VT_LVAL
| VT_SYM
;
5046 #ifdef CONFIG_TCC_BCHECK
5047 if (vtop
->r
& VT_MUSTBOUND
)
5051 r
= vtop
->r
& VT_VALMASK
;
5055 /* need to reload if:
5057 - lvalue (need to dereference pointer)
5058 - already a register, but not in the right class */
5059 if (r
>= VT_CONST
||
5060 (vtop
->r
& VT_LVAL
) ||
5061 !(reg_classes
[r
] & rc
) ||
5062 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
5063 !(reg_classes
[vtop
->r2
] & rc2
))) {
5065 #ifndef TCC_TARGET_X86_64
5066 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
5068 unsigned long long ll
;
5069 /* two register type load : expand to two words
5071 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
5074 vtop
->c
.ui
= ll
; /* first word */
5076 vtop
->r
= r
; /* save register value */
5077 vpushi(ll
>> 32); /* second word */
5078 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
5079 (vtop
->r
& VT_LVAL
)) {
5080 /* We do not want to modifier the long long
5081 pointer here, so the safest (and less
5082 efficient) is to save all the other registers
5083 in the stack. XXX: totally inefficient. */
5085 /* load from memory */
5088 vtop
[-1].r
= r
; /* save register value */
5089 /* increment pointer to get second word */
5090 vtop
->type
.t
= VT_INT
;
5096 /* move registers */
5099 vtop
[-1].r
= r
; /* save register value */
5100 vtop
->r
= vtop
[-1].r2
;
5102 /* allocate second register */
5106 /* write second register */
5110 if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
5112 /* lvalue of scalar type : need to use lvalue type
5113 because of possible cast */
5116 /* compute memory access type */
5117 if (vtop
->r
& VT_LVAL_BYTE
)
5119 else if (vtop
->r
& VT_LVAL_SHORT
)
5121 if (vtop
->r
& VT_LVAL_UNSIGNED
)
5125 /* restore wanted type */
5128 /* one register type load */
5133 #ifdef TCC_TARGET_C67
5134 /* uses register pairs for doubles */
5135 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
5142 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5143 void gv2(int rc1
, int rc2
)
5147 /* generate more generic register first. But VT_JMP or VT_CMP
5148 values must be generated first in all cases to avoid possible
5150 v
= vtop
[0].r
& VT_VALMASK
;
5151 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
5156 /* test if reload is needed for first register */
5157 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
5167 /* test if reload is needed for first register */
5168 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
5174 /* expand long long on stack in two int registers */
5179 u
= vtop
->type
.t
& VT_UNSIGNED
;
5182 vtop
[0].r
= vtop
[-1].r2
;
5183 vtop
[0].r2
= VT_CONST
;
5184 vtop
[-1].r2
= VT_CONST
;
5185 vtop
[0].type
.t
= VT_INT
| u
;
5186 vtop
[-1].type
.t
= VT_INT
| u
;
5189 #ifdef TCC_TARGET_ARM
5190 /* expand long long on stack */
5191 void lexpand_nr(void)
5195 u
= vtop
->type
.t
& VT_UNSIGNED
;
5197 vtop
->r2
= VT_CONST
;
5198 vtop
->type
.t
= VT_INT
| u
;
5199 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
5200 if (v
== VT_CONST
) {
5201 vtop
[-1].c
.ui
= vtop
->c
.ull
;
5202 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
5204 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
5206 vtop
->r
= vtop
[-1].r
;
5207 } else if (v
> VT_CONST
) {
5211 vtop
->r
= vtop
[-1].r2
;
5212 vtop
[-1].r2
= VT_CONST
;
5213 vtop
[-1].type
.t
= VT_INT
| u
;
5217 /* build a long long from two ints */
5220 gv2(RC_INT
, RC_INT
);
5221 vtop
[-1].r2
= vtop
[0].r
;
5222 vtop
[-1].type
.t
= t
;
5226 /* rotate n first stack elements to the bottom
5227 I1 ... In -> I2 ... In I1 [top is right]
5235 for(i
=-n
+1;i
!=0;i
++)
5236 vtop
[i
] = vtop
[i
+1];
5240 /* rotate n first stack elements to the top
5241 I1 ... In -> In I1 ... I(n-1) [top is right]
5249 for(i
= 0;i
< n
- 1; i
++)
5250 vtop
[-i
] = vtop
[-i
- 1];
5254 #ifdef TCC_TARGET_ARM
5255 /* like vrott but in other direction
5256 In ... I1 -> I(n-1) ... I1 In [top is right]
5264 for(i
= n
- 1; i
> 0; i
--)
5265 vtop
[-i
] = vtop
[-i
+ 1];
5270 /* pop stack value */
5274 v
= vtop
->r
& VT_VALMASK
;
5275 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
5276 /* for x86, we need to pop the FP stack */
5277 if (v
== TREG_ST0
&& !nocode_wanted
) {
5278 o(0xd9dd); /* fstp %st(1) */
5281 if (v
== VT_JMP
|| v
== VT_JMPI
) {
5282 /* need to put correct jump if && or || without test */
5288 /* convert stack entry to register and duplicate its value in another
5296 if ((t
& VT_BTYPE
) == VT_LLONG
) {
5303 /* stack: H L L1 H1 */
5311 /* duplicate value */
5316 #ifdef TCC_TARGET_X86_64
5317 if ((t
& VT_BTYPE
) == VT_LDOUBLE
) {
5327 load(r1
, &sv
); /* move r to r1 */
5329 /* duplicates value */
5334 #ifndef TCC_TARGET_X86_64
5335 /* generate CPU independent (unsigned) long long operations */
5336 void gen_opl(int op
)
5338 int t
, a
, b
, op1
, c
, i
;
5340 unsigned short reg_iret
= REG_IRET
;
5341 unsigned short reg_lret
= REG_LRET
;
5347 func
= TOK___divdi3
;
5350 func
= TOK___udivdi3
;
5353 func
= TOK___moddi3
;
5356 func
= TOK___umoddi3
;
5363 /* call generic long long function */
5364 vpush_global_sym(&func_old_type
, func
);
5369 vtop
->r2
= reg_lret
;
5382 /* stack: L1 H1 L2 H2 */
5387 vtop
[-2] = vtop
[-3];
5390 /* stack: H1 H2 L1 L2 */
5396 /* stack: H1 H2 L1 L2 ML MH */
5399 /* stack: ML MH H1 H2 L1 L2 */
5403 /* stack: ML MH H1 L2 H2 L1 */
5408 /* stack: ML MH M1 M2 */
5411 } else if (op
== '+' || op
== '-') {
5412 /* XXX: add non carry method too (for MIPS or alpha) */
5418 /* stack: H1 H2 (L1 op L2) */
5421 gen_op(op1
+ 1); /* TOK_xxxC2 */
5424 /* stack: H1 H2 (L1 op L2) */
5427 /* stack: (L1 op L2) H1 H2 */
5429 /* stack: (L1 op L2) (H1 op H2) */
5437 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5438 t
= vtop
[-1].type
.t
;
5442 /* stack: L H shift */
5444 /* constant: simpler */
5445 /* NOTE: all comments are for SHL. the other cases are
5446 done by swaping words */
5457 if (op
!= TOK_SAR
) {
5490 /* XXX: should provide a faster fallback on x86 ? */
5493 func
= TOK___ashrdi3
;
5496 func
= TOK___lshrdi3
;
5499 func
= TOK___ashldi3
;
5505 /* compare operations */
5511 /* stack: L1 H1 L2 H2 */
5513 vtop
[-1] = vtop
[-2];
5515 /* stack: L1 L2 H1 H2 */
5518 /* when values are equal, we need to compare low words. since
5519 the jump is inverted, we invert the test too. */
5522 else if (op1
== TOK_GT
)
5524 else if (op1
== TOK_ULT
)
5526 else if (op1
== TOK_UGT
)
5531 if (op1
!= TOK_NE
) {
5535 /* generate non equal test */
5536 /* XXX: NOT PORTABLE yet */
5540 #if defined(TCC_TARGET_I386)
5541 b
= psym(0x850f, 0);
5542 #elif defined(TCC_TARGET_ARM)
5544 o(0x1A000000 | encbranch(ind
, 0, 1));
5545 #elif defined(TCC_TARGET_C67)
5546 error("not implemented");
5548 #error not supported
5552 /* compare low. Always unsigned */
5556 else if (op1
== TOK_LE
)
5558 else if (op1
== TOK_GT
)
5560 else if (op1
== TOK_GE
)
5571 /* handle integer constant optimizations and various machine
5573 void gen_opic(int op
)
5575 int c1
, c2
, t1
, t2
, n
;
5578 typedef unsigned long long U
;
5582 t1
= v1
->type
.t
& VT_BTYPE
;
5583 t2
= v2
->type
.t
& VT_BTYPE
;
5587 else if (v1
->type
.t
& VT_UNSIGNED
)
5594 else if (v2
->type
.t
& VT_UNSIGNED
)
5599 /* currently, we cannot do computations with forward symbols */
5600 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5601 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5604 case '+': l1
+= l2
; break;
5605 case '-': l1
-= l2
; break;
5606 case '&': l1
&= l2
; break;
5607 case '^': l1
^= l2
; break;
5608 case '|': l1
|= l2
; break;
5609 case '*': l1
*= l2
; break;
5616 /* if division by zero, generate explicit division */
5619 error("division by zero in constant");
5623 default: l1
/= l2
; break;
5624 case '%': l1
%= l2
; break;
5625 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
5626 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
5629 case TOK_SHL
: l1
<<= l2
; break;
5630 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
5631 case TOK_SAR
: l1
>>= l2
; break;
5633 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
5634 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
5635 case TOK_EQ
: l1
= l1
== l2
; break;
5636 case TOK_NE
: l1
= l1
!= l2
; break;
5637 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
5638 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
5639 case TOK_LT
: l1
= l1
< l2
; break;
5640 case TOK_GE
: l1
= l1
>= l2
; break;
5641 case TOK_LE
: l1
= l1
<= l2
; break;
5642 case TOK_GT
: l1
= l1
> l2
; break;
5644 case TOK_LAND
: l1
= l1
&& l2
; break;
5645 case TOK_LOR
: l1
= l1
|| l2
; break;
5652 /* if commutative ops, put c2 as constant */
5653 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
5654 op
== '|' || op
== '*')) {
5656 c2
= c1
; //c = c1, c1 = c2, c2 = c;
5657 l2
= l1
; //l = l1, l1 = l2, l2 = l;
5659 /* Filter out NOP operations like x*1, x-0, x&-1... */
5660 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
5663 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
5664 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
5670 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
5671 /* try to use shifts instead of muls or divs */
5672 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
5681 else if (op
== TOK_PDIV
)
5687 } else if (c2
&& (op
== '+' || op
== '-') &&
5688 ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
5689 (VT_CONST
| VT_SYM
) ||
5690 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
5691 /* symbol + constant case */
5698 if (!nocode_wanted
) {
5699 /* call low level op generator */
5700 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
5711 /* generate a floating point operation with constant propagation */
5712 void gen_opif(int op
)
5720 /* currently, we cannot do computations with forward symbols */
5721 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5722 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5724 if (v1
->type
.t
== VT_FLOAT
) {
5727 } else if (v1
->type
.t
== VT_DOUBLE
) {
5735 /* NOTE: we only do constant propagation if finite number (not
5736 NaN or infinity) (ANSI spec) */
5737 if (!ieee_finite(f1
) || !ieee_finite(f2
))
5741 case '+': f1
+= f2
; break;
5742 case '-': f1
-= f2
; break;
5743 case '*': f1
*= f2
; break;
5747 error("division by zero in constant");
5752 /* XXX: also handles tests ? */
5756 /* XXX: overflow test ? */
5757 if (v1
->type
.t
== VT_FLOAT
) {
5759 } else if (v1
->type
.t
== VT_DOUBLE
) {
5767 if (!nocode_wanted
) {
5775 static int pointed_size(CType
*type
)
5778 return type_size(pointed_type(type
), &align
);
5781 static inline int is_null_pointer(SValue
*p
)
5783 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5785 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
5786 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
5789 static inline int is_integer_btype(int bt
)
5791 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
5792 bt
== VT_INT
|| bt
== VT_LLONG
);
5795 /* check types for comparison or substraction of pointers */
5796 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
5798 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
5801 /* null pointers are accepted for all comparisons as gcc */
5802 if (is_null_pointer(p1
) || is_null_pointer(p2
))
5806 bt1
= type1
->t
& VT_BTYPE
;
5807 bt2
= type2
->t
& VT_BTYPE
;
5808 /* accept comparison between pointer and integer with a warning */
5809 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
5810 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
5811 warning("comparison between pointer and integer");
5815 /* both must be pointers or implicit function pointers */
5816 if (bt1
== VT_PTR
) {
5817 type1
= pointed_type(type1
);
5818 } else if (bt1
!= VT_FUNC
)
5819 goto invalid_operands
;
5821 if (bt2
== VT_PTR
) {
5822 type2
= pointed_type(type2
);
5823 } else if (bt2
!= VT_FUNC
) {
5825 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
5827 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5828 (type2
->t
& VT_BTYPE
) == VT_VOID
)
5832 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5833 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5834 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
5835 /* gcc-like error if '-' is used */
5837 goto invalid_operands
;
5839 warning("comparison of distinct pointer types lacks a cast");
5843 /* generic gen_op: handles types problems */
5846 int u
, t1
, t2
, bt1
, bt2
, t
;
5849 t1
= vtop
[-1].type
.t
;
5850 t2
= vtop
[0].type
.t
;
5851 bt1
= t1
& VT_BTYPE
;
5852 bt2
= t2
& VT_BTYPE
;
5854 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5855 /* at least one operand is a pointer */
5856 /* relationnal op: must be both pointers */
5857 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
5858 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5859 /* pointers are handled are unsigned */
5860 #ifdef TCC_TARGET_X86_64
5861 t
= VT_LLONG
| VT_UNSIGNED
;
5863 t
= VT_INT
| VT_UNSIGNED
;
5867 /* if both pointers, then it must be the '-' op */
5868 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
5870 error("cannot use pointers here");
5871 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5872 /* XXX: check that types are compatible */
5873 u
= pointed_size(&vtop
[-1].type
);
5875 /* set to integer type */
5876 #ifdef TCC_TARGET_X86_64
5877 vtop
->type
.t
= VT_LLONG
;
5879 vtop
->type
.t
= VT_INT
;
5884 /* exactly one pointer : must be '+' or '-'. */
5885 if (op
!= '-' && op
!= '+')
5886 error("cannot use pointers here");
5887 /* Put pointer as first operand */
5888 if (bt2
== VT_PTR
) {
5892 type1
= vtop
[-1].type
;
5893 #ifdef TCC_TARGET_X86_64
5894 vpushll(pointed_size(&vtop
[-1].type
));
5896 /* XXX: cast to int ? (long long case) */
5897 vpushi(pointed_size(&vtop
[-1].type
));
5900 #ifdef CONFIG_TCC_BCHECK
5901 /* if evaluating constant expression, no code should be
5902 generated, so no bound check */
5903 if (do_bounds_check
&& !const_wanted
) {
5904 /* if bounded pointers, we generate a special code to
5911 gen_bounded_ptr_add();
5917 /* put again type if gen_opic() swaped operands */
5920 } else if (is_float(bt1
) || is_float(bt2
)) {
5921 /* compute bigger type and do implicit casts */
5922 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5924 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5929 /* floats can only be used for a few operations */
5930 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
5931 (op
< TOK_ULT
|| op
> TOK_GT
))
5932 error("invalid operands for binary operation");
5934 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5935 /* cast to biggest op */
5937 /* convert to unsigned if it does not fit in a long long */
5938 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5939 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5943 /* integer operations */
5945 /* convert to unsigned if it does not fit in an integer */
5946 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5947 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5950 /* XXX: currently, some unsigned operations are explicit, so
5951 we modify them here */
5952 if (t
& VT_UNSIGNED
) {
5959 else if (op
== TOK_LT
)
5961 else if (op
== TOK_GT
)
5963 else if (op
== TOK_LE
)
5965 else if (op
== TOK_GE
)
5972 /* special case for shifts and long long: we keep the shift as
5974 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
5981 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5982 /* relationnal op: the result is an int */
5983 vtop
->type
.t
= VT_INT
;
5990 #ifndef TCC_TARGET_ARM
5991 /* generic itof for unsigned long long case */
5992 void gen_cvt_itof1(int t
)
5994 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
5995 (VT_LLONG
| VT_UNSIGNED
)) {
5998 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
5999 #if LDOUBLE_SIZE != 8
6000 else if (t
== VT_LDOUBLE
)
6001 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
6004 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
6015 /* generic ftoi for unsigned long long case */
6016 void gen_cvt_ftoi1(int t
)
6020 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
6021 /* not handled natively */
6022 st
= vtop
->type
.t
& VT_BTYPE
;
6024 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
6025 #if LDOUBLE_SIZE != 8
6026 else if (st
== VT_LDOUBLE
)
6027 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
6030 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
6035 vtop
->r2
= REG_LRET
;
6041 /* force char or short cast */
6042 void force_charshort_cast(int t
)
6046 /* XXX: add optimization if lvalue : just change type and offset */
6051 if (t
& VT_UNSIGNED
) {
6052 vpushi((1 << bits
) - 1);
6058 /* result must be signed or the SAR is converted to an SHL
6059 This was not the case when "t" was a signed short
6060 and the last value on the stack was an unsigned int */
6061 vtop
->type
.t
&= ~VT_UNSIGNED
;
6067 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
6068 static void gen_cast(CType
*type
)
6070 int sbt
, dbt
, sf
, df
, c
, p
;
6072 /* special delayed cast for char/short */
6073 /* XXX: in some cases (multiple cascaded casts), it may still
6075 if (vtop
->r
& VT_MUSTCAST
) {
6076 vtop
->r
&= ~VT_MUSTCAST
;
6077 force_charshort_cast(vtop
->type
.t
);
6080 /* bitfields first get cast to ints */
6081 if (vtop
->type
.t
& VT_BITFIELD
) {
6085 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
6086 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
6091 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
6092 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
6094 /* constant case: we can do it now */
6095 /* XXX: in ISOC, cannot do it if error in convert */
6096 if (sbt
== VT_FLOAT
)
6097 vtop
->c
.ld
= vtop
->c
.f
;
6098 else if (sbt
== VT_DOUBLE
)
6099 vtop
->c
.ld
= vtop
->c
.d
;
6102 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
6103 if (sbt
& VT_UNSIGNED
)
6104 vtop
->c
.ld
= vtop
->c
.ull
;
6106 vtop
->c
.ld
= vtop
->c
.ll
;
6108 if (sbt
& VT_UNSIGNED
)
6109 vtop
->c
.ld
= vtop
->c
.ui
;
6111 vtop
->c
.ld
= vtop
->c
.i
;
6114 if (dbt
== VT_FLOAT
)
6115 vtop
->c
.f
= (float)vtop
->c
.ld
;
6116 else if (dbt
== VT_DOUBLE
)
6117 vtop
->c
.d
= (double)vtop
->c
.ld
;
6118 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
6119 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
6120 } else if (sf
&& dbt
== VT_BOOL
) {
6121 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
6124 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
6125 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
6126 vtop
->c
.ll
= vtop
->c
.ull
;
6127 else if (sbt
& VT_UNSIGNED
)
6128 vtop
->c
.ll
= vtop
->c
.ui
;
6129 else if (sbt
!= VT_LLONG
)
6130 vtop
->c
.ll
= vtop
->c
.i
;
6132 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
6133 vtop
->c
.ull
= vtop
->c
.ll
;
6134 else if (dbt
== VT_BOOL
)
6135 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
6136 else if (dbt
!= VT_LLONG
) {
6138 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
6140 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
6143 if(dbt
& VT_UNSIGNED
)
6144 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
6146 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
6149 } else if (p
&& dbt
== VT_BOOL
) {
6152 } else if (!nocode_wanted
) {
6153 /* non constant case: generate code */
6155 /* convert from fp to fp */
6158 /* convert int to fp */
6161 /* convert fp to int */
6162 if (dbt
== VT_BOOL
) {
6166 /* we handle char/short/etc... with generic code */
6167 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
6168 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
6172 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
6173 /* additional cast for char/short... */
6178 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
6179 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
6180 /* scalar to long long */
6181 #ifndef TCC_TARGET_X86_64
6182 /* machine independent conversion */
6184 /* generate high word */
6185 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
6193 /* patch second register */
6194 vtop
[-1].r2
= vtop
->r
;
6198 if (sbt
!= (VT_INT
| VT_UNSIGNED
)) {
6199 /* x86_64 specific: movslq */
6201 o(0xc0 + (REG_VALUE(r
) << 3) + REG_VALUE(r
));
6205 } else if (dbt
== VT_BOOL
) {
6206 /* scalar to bool */
6209 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
6210 (dbt
& VT_BTYPE
) == VT_SHORT
) {
6211 if (sbt
== VT_PTR
) {
6212 vtop
->type
.t
= VT_INT
;
6213 warning("nonportable conversion from pointer to char/short");
6215 force_charshort_cast(dbt
);
6216 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
6218 if (sbt
== VT_LLONG
) {
6219 /* from long long: just take low order word */
6223 /* if lvalue and single word type, nothing to do because
6224 the lvalue already contains the real type size (see
6225 VT_LVAL_xxx constants) */
6228 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
6229 /* if we are casting between pointer types,
6230 we must update the VT_LVAL_xxx size */
6231 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
6232 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
6237 /* return type size. Put alignment at 'a' */
6238 static int type_size(CType
*type
, int *a
)
6243 bt
= type
->t
& VT_BTYPE
;
6244 if (bt
== VT_STRUCT
) {
6249 } else if (bt
== VT_PTR
) {
6250 if (type
->t
& VT_ARRAY
) {
6254 ts
= type_size(&s
->type
, a
);
6256 if (ts
< 0 && s
->c
< 0)
6264 } else if (bt
== VT_LDOUBLE
) {
6266 return LDOUBLE_SIZE
;
6267 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
6268 #ifdef TCC_TARGET_I386
6270 #elif defined(TCC_TARGET_ARM)
6280 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
6283 } else if (bt
== VT_SHORT
) {
6287 /* char, void, function, _Bool */
6293 /* return the pointed type of t */
6294 static inline CType
*pointed_type(CType
*type
)
6296 return &type
->ref
->type
;
6299 /* modify type so that its it is a pointer to type. */
6300 static void mk_pointer(CType
*type
)
6303 s
= sym_push(SYM_FIELD
, type
, 0, -1);
6304 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
6308 /* compare function types. OLD functions match any new functions */
6309 static int is_compatible_func(CType
*type1
, CType
*type2
)
6315 if (!is_compatible_types(&s1
->type
, &s2
->type
))
6317 /* check func_call */
6318 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
6320 /* XXX: not complete */
6321 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
6325 while (s1
!= NULL
) {
6328 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
6338 /* return true if type1 and type2 are the same. If unqualified is
6339 true, qualifiers on the types are ignored.
6341 - enums are not checked as gcc __builtin_types_compatible_p ()
6343 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
6347 t1
= type1
->t
& VT_TYPE
;
6348 t2
= type2
->t
& VT_TYPE
;
6350 /* strip qualifiers before comparing */
6351 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6352 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6354 /* XXX: bitfields ? */
6357 /* test more complicated cases */
6358 bt1
= t1
& VT_BTYPE
;
6359 if (bt1
== VT_PTR
) {
6360 type1
= pointed_type(type1
);
6361 type2
= pointed_type(type2
);
6362 return is_compatible_types(type1
, type2
);
6363 } else if (bt1
== VT_STRUCT
) {
6364 return (type1
->ref
== type2
->ref
);
6365 } else if (bt1
== VT_FUNC
) {
6366 return is_compatible_func(type1
, type2
);
6372 /* return true if type1 and type2 are exactly the same (including
6375 static int is_compatible_types(CType
*type1
, CType
*type2
)
6377 return compare_types(type1
,type2
,0);
6380 /* return true if type1 and type2 are the same (ignoring qualifiers).
6382 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
6384 return compare_types(type1
,type2
,1);
6387 /* print a type. If 'varstr' is not NULL, then the variable is also
6388 printed in the type */
6390 /* XXX: add array and function pointers */
6391 void type_to_str(char *buf
, int buf_size
,
6392 CType
*type
, const char *varstr
)
6399 t
= type
->t
& VT_TYPE
;
6402 if (t
& VT_CONSTANT
)
6403 pstrcat(buf
, buf_size
, "const ");
6404 if (t
& VT_VOLATILE
)
6405 pstrcat(buf
, buf_size
, "volatile ");
6406 if (t
& VT_UNSIGNED
)
6407 pstrcat(buf
, buf_size
, "unsigned ");
6437 tstr
= "long double";
6439 pstrcat(buf
, buf_size
, tstr
);
6443 if (bt
== VT_STRUCT
)
6447 pstrcat(buf
, buf_size
, tstr
);
6448 v
= type
->ref
->v
& ~SYM_STRUCT
;
6449 if (v
>= SYM_FIRST_ANOM
)
6450 pstrcat(buf
, buf_size
, "<anonymous>");
6452 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
6456 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
6457 pstrcat(buf
, buf_size
, "(");
6459 while (sa
!= NULL
) {
6460 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
6461 pstrcat(buf
, buf_size
, buf1
);
6464 pstrcat(buf
, buf_size
, ", ");
6466 pstrcat(buf
, buf_size
, ")");
6470 pstrcpy(buf1
, sizeof(buf1
), "*");
6472 pstrcat(buf1
, sizeof(buf1
), varstr
);
6473 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
6477 pstrcat(buf
, buf_size
, " ");
6478 pstrcat(buf
, buf_size
, varstr
);
6483 /* verify type compatibility to store vtop in 'dt' type, and generate
6485 static void gen_assign_cast(CType
*dt
)
6487 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
6488 char buf1
[256], buf2
[256];
6491 st
= &vtop
->type
; /* source type */
6492 dbt
= dt
->t
& VT_BTYPE
;
6493 sbt
= st
->t
& VT_BTYPE
;
6494 if (dt
->t
& VT_CONSTANT
)
6495 warning("assignment of read-only location");
6498 /* special cases for pointers */
6499 /* '0' can also be a pointer */
6500 if (is_null_pointer(vtop
))
6502 /* accept implicit pointer to integer cast with warning */
6503 if (is_integer_btype(sbt
)) {
6504 warning("assignment makes pointer from integer without a cast");
6507 type1
= pointed_type(dt
);
6508 /* a function is implicitely a function pointer */
6509 if (sbt
== VT_FUNC
) {
6510 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
6511 !is_compatible_types(pointed_type(dt
), st
))
6518 type2
= pointed_type(st
);
6519 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
6520 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
6521 /* void * can match anything */
6523 /* exact type match, except for unsigned */
6526 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6527 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6528 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
6529 warning("assignment from incompatible pointer type");
6531 /* check const and volatile */
6532 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
6533 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
6534 warning("assignment discards qualifiers from pointer target type");
6540 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
6541 warning("assignment makes integer from pointer without a cast");
6543 /* XXX: more tests */
6548 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6549 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6550 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
6552 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
6553 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
6554 error("cannot cast '%s' to '%s'", buf1
, buf2
);
6562 /* store vtop in lvalue pushed on stack */
6565 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
6567 ft
= vtop
[-1].type
.t
;
6568 sbt
= vtop
->type
.t
& VT_BTYPE
;
6569 dbt
= ft
& VT_BTYPE
;
6570 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
6571 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
6572 /* optimize char/short casts */
6573 delayed_cast
= VT_MUSTCAST
;
6574 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
6575 /* XXX: factorize */
6576 if (ft
& VT_CONSTANT
)
6577 warning("assignment of read-only location");
6580 if (!(ft
& VT_BITFIELD
))
6581 gen_assign_cast(&vtop
[-1].type
);
6584 if (sbt
== VT_STRUCT
) {
6585 /* if structure, only generate pointer */
6586 /* structure assignment : generate memcpy */
6587 /* XXX: optimize if small size */
6588 if (!nocode_wanted
) {
6589 size
= type_size(&vtop
->type
, &align
);
6593 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
6594 else if(!(align
& 3))
6595 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
6598 vpush_global_sym(&func_old_type
, TOK_memcpy
);
6602 vtop
->type
.t
= VT_INT
;
6606 vtop
->type
.t
= VT_INT
;
6618 /* leave source on stack */
6619 } else if (ft
& VT_BITFIELD
) {
6620 /* bitfield store handling */
6621 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
6622 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6623 /* remove bit field info to avoid loops */
6624 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
6626 /* duplicate source into other register */
6631 if((ft
& VT_BTYPE
) == VT_BOOL
) {
6632 gen_cast(&vtop
[-1].type
);
6633 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
6636 /* duplicate destination */
6638 vtop
[-1] = vtop
[-2];
6640 /* mask and shift source */
6641 if((ft
& VT_BTYPE
) != VT_BOOL
) {
6642 if((ft
& VT_BTYPE
) == VT_LLONG
) {
6643 vpushll((1ULL << bit_size
) - 1ULL);
6645 vpushi((1 << bit_size
) - 1);
6651 /* load destination, mask and or with source */
6653 if((ft
& VT_BTYPE
) == VT_LLONG
) {
6654 vpushll(~(((1ULL << bit_size
) - 1ULL) << bit_pos
));
6656 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
6663 /* pop off shifted source from "duplicate source..." above */
6667 #ifdef CONFIG_TCC_BCHECK
6668 /* bound check case */
6669 if (vtop
[-1].r
& VT_MUSTBOUND
) {
6675 if (!nocode_wanted
) {
6679 #ifdef TCC_TARGET_X86_64
6680 if ((ft
& VT_BTYPE
) == VT_LDOUBLE
) {
6685 r
= gv(rc
); /* generate value */
6686 /* if lvalue was saved on stack, must read it */
6687 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
6689 t
= get_reg(RC_INT
);
6690 #ifdef TCC_TARGET_X86_64
6695 sv
.r
= VT_LOCAL
| VT_LVAL
;
6696 sv
.c
.ul
= vtop
[-1].c
.ul
;
6698 vtop
[-1].r
= t
| VT_LVAL
;
6701 #ifndef TCC_TARGET_X86_64
6702 /* two word case handling : store second register at word + 4 */
6703 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
6705 /* convert to int to increment easily */
6706 vtop
->type
.t
= VT_INT
;
6712 /* XXX: it works because r2 is spilled last ! */
6713 store(vtop
->r2
, vtop
- 1);
6718 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6719 vtop
->r
|= delayed_cast
;
6723 /* post defines POST/PRE add. c is the token ++ or -- */
6724 void inc(int post
, int c
)
6727 vdup(); /* save lvalue */
6729 gv_dup(); /* duplicate value */
6734 vpushi(c
- TOK_MID
);
6736 vstore(); /* store value */
6738 vpop(); /* if post op, return saved value */
6741 /* Parse GNUC __attribute__ extension. Currently, the following
6742 extensions are recognized:
6743 - aligned(n) : set data/function alignment.
6744 - packed : force data alignment to 1
6745 - section(x) : generate data/code in this section.
6746 - unused : currently ignored, but may be used someday.
6747 - regparm(n) : pass function parameters in registers (i386 only)
6749 static void parse_attribute(AttributeDef
*ad
)
6753 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
6757 while (tok
!= ')') {
6758 if (tok
< TOK_IDENT
)
6759 expect("attribute name");
6767 expect("section name");
6768 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
6777 if (n
<= 0 || (n
& (n
- 1)) != 0)
6778 error("alignment must be a positive power of two");
6791 /* currently, no need to handle it because tcc does not
6792 track unused objects */
6796 /* currently, no need to handle it because tcc does not
6797 track unused objects */
6802 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
6807 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
6809 #ifdef TCC_TARGET_I386
6819 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
6825 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
6829 FUNC_EXPORT(ad
->func_attr
) = 1;
6832 if (tcc_state
->warn_unsupported
)
6833 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
6834 /* skip parameters */
6836 int parenthesis
= 0;
6840 else if (tok
== ')')
6843 } while (parenthesis
&& tok
!= -1);
6856 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6857 static void struct_decl(CType
*type
, int u
)
6859 int a
, v
, size
, align
, maxalign
, c
, offset
;
6860 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
6861 Sym
*s
, *ss
, *ass
, **ps
;
6865 a
= tok
; /* save decl type */
6870 /* struct already defined ? return it */
6872 expect("struct/union/enum name");
6876 error("invalid type");
6883 /* we put an undefined size for struct/union */
6884 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
6885 s
->r
= 0; /* default alignment is zero as gcc */
6886 /* put struct/union/enum name in type */
6894 error("struct/union/enum already defined");
6895 /* cannot be empty */
6897 /* non empty enums are not allowed */
6898 if (a
== TOK_ENUM
) {
6902 expect("identifier");
6908 /* enum symbols have static storage */
6909 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
6910 ss
->type
.t
|= VT_STATIC
;
6915 /* NOTE: we accept a trailing comma */
6926 while (tok
!= '}') {
6927 parse_btype(&btype
, &ad
);
6933 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
6934 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
6935 expect("identifier");
6936 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
6937 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
6938 error("invalid type for '%s'",
6939 get_tok_str(v
, NULL
));
6943 bit_size
= expr_const();
6944 /* XXX: handle v = 0 case for messages */
6946 error("negative width in bit-field '%s'",
6947 get_tok_str(v
, NULL
));
6948 if (v
&& bit_size
== 0)
6949 error("zero width for bit-field '%s'",
6950 get_tok_str(v
, NULL
));
6952 size
= type_size(&type1
, &align
);
6954 if (align
< ad
.aligned
)
6956 } else if (ad
.packed
) {
6958 } else if (*tcc_state
->pack_stack_ptr
) {
6959 if (align
> *tcc_state
->pack_stack_ptr
)
6960 align
= *tcc_state
->pack_stack_ptr
;
6963 if (bit_size
>= 0) {
6964 bt
= type1
.t
& VT_BTYPE
;
6971 error("bitfields must have scalar type");
6973 if (bit_size
> bsize
) {
6974 error("width of '%s' exceeds its type",
6975 get_tok_str(v
, NULL
));
6976 } else if (bit_size
== bsize
) {
6977 /* no need for bit fields */
6979 } else if (bit_size
== 0) {
6980 /* XXX: what to do if only padding in a
6982 /* zero size: means to pad */
6985 /* we do not have enough room ?
6986 did the type change?
6988 if ((bit_pos
+ bit_size
) > bsize
||
6989 bt
!= prevbt
|| a
== TOK_UNION
)
6992 /* XXX: handle LSB first */
6993 type1
.t
|= VT_BITFIELD
|
6994 (bit_pos
<< VT_STRUCT_SHIFT
) |
6995 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
6996 bit_pos
+= bit_size
;
7002 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
7003 /* add new memory data only if starting
7005 if (lbit_pos
== 0) {
7006 if (a
== TOK_STRUCT
) {
7007 c
= (c
+ align
- 1) & -align
;
7016 if (align
> maxalign
)
7020 printf("add field %s offset=%d",
7021 get_tok_str(v
, NULL
), offset
);
7022 if (type1
.t
& VT_BITFIELD
) {
7023 printf(" pos=%d size=%d",
7024 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
7025 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
7030 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
7032 while ((ass
= ass
->next
) != NULL
) {
7033 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
7038 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
7042 if (tok
== ';' || tok
== TOK_EOF
)
7049 /* store size and alignment */
7050 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
7056 /* return 0 if no type declaration. otherwise, return the basic type
7059 static int parse_btype(CType
*type
, AttributeDef
*ad
)
7061 int t
, u
, type_found
, typespec_found
, typedef_found
;
7065 memset(ad
, 0, sizeof(AttributeDef
));
7073 /* currently, we really ignore extension */
7083 if ((t
& VT_BTYPE
) != 0)
7084 error("too many basic types");
7100 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
7101 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
7102 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
7103 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
7117 if ((t
& VT_BTYPE
) == VT_LONG
) {
7118 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
7125 struct_decl(&type1
, VT_ENUM
);
7128 type
->ref
= type1
.ref
;
7132 struct_decl(&type1
, VT_STRUCT
);
7135 /* type modifiers */
7188 /* GNUC attribute */
7189 case TOK_ATTRIBUTE1
:
7190 case TOK_ATTRIBUTE2
:
7191 parse_attribute(ad
);
7198 parse_expr_type(&type1
);
7201 if (typespec_found
|| typedef_found
)
7204 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
7207 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
7208 type
->ref
= s
->type
.ref
;
7216 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
7217 error("signed and unsigned modifier");
7218 if (tcc_state
->char_is_unsigned
) {
7219 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
7224 /* long is never used as type */
7225 if ((t
& VT_BTYPE
) == VT_LONG
)
7226 #ifndef TCC_TARGET_X86_64
7227 t
= (t
& ~VT_BTYPE
) | VT_INT
;
7229 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
7235 /* convert a function parameter type (array to pointer and function to
7236 function pointer) */
7237 static inline void convert_parameter_type(CType
*pt
)
7239 /* remove const and volatile qualifiers (XXX: const could be used
7240 to indicate a const function parameter */
7241 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7242 /* array must be transformed to pointer according to ANSI C */
7244 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
7249 static void post_type(CType
*type
, AttributeDef
*ad
)
7251 int n
, l
, t1
, arg_size
, align
;
7252 Sym
**plast
, *s
, *first
;
7257 /* function declaration */
7265 /* read param name and compute offset */
7266 if (l
!= FUNC_OLD
) {
7267 if (!parse_btype(&pt
, &ad1
)) {
7269 error("invalid type");
7276 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
7278 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
7279 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
7280 error("parameter declared as void");
7281 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
7286 expect("identifier");
7290 convert_parameter_type(&pt
);
7291 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
7297 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
7304 /* if no parameters, then old type prototype */
7308 t1
= type
->t
& VT_STORAGE
;
7309 /* NOTE: const is ignored in returned type as it has a special
7310 meaning in gcc / C++ */
7311 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
7312 post_type(type
, ad
);
7313 /* we push a anonymous symbol which will contain the function prototype */
7314 FUNC_ARGS(ad
->func_attr
) = arg_size
;
7315 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
7317 type
->t
= t1
| VT_FUNC
;
7319 } else if (tok
== '[') {
7320 /* array definition */
7326 error("invalid array size");
7329 /* parse next post type */
7330 t1
= type
->t
& VT_STORAGE
;
7331 type
->t
&= ~VT_STORAGE
;
7332 post_type(type
, ad
);
7334 /* we push a anonymous symbol which will contain the array
7336 s
= sym_push(SYM_FIELD
, type
, 0, n
);
7337 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
7342 /* Parse a type declaration (except basic type), and return the type
7343 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7344 expected. 'type' should contain the basic type. 'ad' is the
7345 attribute definition of the basic type. It can be modified by
7348 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
7351 CType type1
, *type2
;
7354 while (tok
== '*') {
7362 qualifiers
|= VT_CONSTANT
;
7367 qualifiers
|= VT_VOLATILE
;
7375 type
->t
|= qualifiers
;
7378 /* XXX: clarify attribute handling */
7379 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7380 parse_attribute(ad
);
7382 /* recursive type */
7383 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7384 type1
.t
= 0; /* XXX: same as int */
7387 /* XXX: this is not correct to modify 'ad' at this point, but
7388 the syntax is not clear */
7389 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7390 parse_attribute(ad
);
7391 type_decl(&type1
, ad
, v
, td
);
7394 /* type identifier */
7395 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
7399 if (!(td
& TYPE_ABSTRACT
))
7400 expect("identifier");
7404 post_type(type
, ad
);
7405 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7406 parse_attribute(ad
);
7409 /* append type at the end of type1 */
7422 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7423 static int lvalue_type(int t
)
7428 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
7430 else if (bt
== VT_SHORT
)
7434 if (t
& VT_UNSIGNED
)
7435 r
|= VT_LVAL_UNSIGNED
;
7439 /* indirection with full error checking and bound check */
7440 static void indir(void)
7442 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
7443 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
7447 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
7449 vtop
->type
= *pointed_type(&vtop
->type
);
7450 /* Arrays and functions are never lvalues */
7451 if (!(vtop
->type
.t
& VT_ARRAY
)
7452 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7453 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7454 /* if bound checking, the referenced pointer must be checked */
7455 if (do_bounds_check
)
7456 vtop
->r
|= VT_MUSTBOUND
;
7460 /* pass a parameter to a function and do type checking and casting */
7461 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
7466 func_type
= func
->c
;
7467 if (func_type
== FUNC_OLD
||
7468 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
7469 /* default casting : only need to convert float to double */
7470 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
7474 } else if (arg
== NULL
) {
7475 error("too many arguments to function");
7478 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
7479 gen_assign_cast(&type
);
7483 /* parse an expression of the form '(type)' or '(expr)' and return its
7485 static void parse_expr_type(CType
*type
)
7491 if (parse_btype(type
, &ad
)) {
7492 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7499 static void parse_type(CType
*type
)
7504 if (!parse_btype(type
, &ad
)) {
7507 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7510 static void vpush_tokc(int t
)
7514 vsetc(&type
, VT_CONST
, &tokc
);
7517 static void unary(void)
7519 int n
, t
, align
, size
, r
;
7524 /* XXX: GCC 2.95.3 does not generate a table although it should be
7538 vpush_tokc(VT_INT
| VT_UNSIGNED
);
7542 vpush_tokc(VT_LLONG
);
7546 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
7550 vpush_tokc(VT_FLOAT
);
7554 vpush_tokc(VT_DOUBLE
);
7558 vpush_tokc(VT_LDOUBLE
);
7561 case TOK___FUNCTION__
:
7563 goto tok_identifier
;
7569 /* special function name identifier */
7570 len
= strlen(funcname
) + 1;
7571 /* generate char[len] type */
7576 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
7577 ptr
= section_ptr_add(data_section
, len
);
7578 memcpy(ptr
, funcname
, len
);
7583 #ifdef TCC_TARGET_PE
7584 t
= VT_SHORT
| VT_UNSIGNED
;
7590 /* string parsing */
7593 if (tcc_state
->warn_write_strings
)
7598 memset(&ad
, 0, sizeof(AttributeDef
));
7599 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
7604 if (parse_btype(&type
, &ad
)) {
7605 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
7607 /* check ISOC99 compound literal */
7609 /* data is allocated locally by default */
7614 /* all except arrays are lvalues */
7615 if (!(type
.t
& VT_ARRAY
))
7616 r
|= lvalue_type(type
.t
);
7617 memset(&ad
, 0, sizeof(AttributeDef
));
7618 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
7623 } else if (tok
== '{') {
7624 /* save all registers */
7626 /* statement expression : we do not accept break/continue
7627 inside as GCC does */
7628 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
7643 /* functions names must be treated as function pointers,
7644 except for unary '&' and sizeof. Since we consider that
7645 functions are not lvalues, we only have to handle it
7646 there and in function calls. */
7647 /* arrays can also be used although they are not lvalues */
7648 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
7649 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
7651 mk_pointer(&vtop
->type
);
7657 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
7659 boolean
.t
= VT_BOOL
;
7661 vtop
->c
.i
= !vtop
->c
.i
;
7662 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
7663 vtop
->c
.i
= vtop
->c
.i
^ 1;
7666 vseti(VT_JMP
, gtst(1, 0));
7677 /* in order to force cast, we add zero */
7679 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
7680 error("pointer not accepted for unary plus");
7690 parse_expr_type(&type
);
7694 size
= type_size(&type
, &align
);
7695 if (t
== TOK_SIZEOF
) {
7697 error("sizeof applied to an incomplete type");
7702 vtop
->type
.t
|= VT_UNSIGNED
;
7705 case TOK_builtin_types_compatible_p
:
7714 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7715 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7716 vpushi(is_compatible_types(&type1
, &type2
));
7719 case TOK_builtin_constant_p
:
7721 int saved_nocode_wanted
, res
;
7724 saved_nocode_wanted
= nocode_wanted
;
7727 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
7729 nocode_wanted
= saved_nocode_wanted
;
7734 case TOK_builtin_frame_address
:
7739 if (tok
!= TOK_CINT
) {
7740 error("__builtin_frame_address only takes integers");
7743 error("TCC only supports __builtin_frame_address(0)");
7749 vset(&type
, VT_LOCAL
, 0);
7752 #ifdef TCC_TARGET_X86_64
7753 case TOK_builtin_malloc
:
7755 char *p
= file
->buf_ptr
;
7756 file
->buf_ptr
= "malloc";
7759 goto tok_identifier
;
7761 case TOK_builtin_free
:
7763 char *p
= file
->buf_ptr
;
7764 file
->buf_ptr
= "free";
7767 goto tok_identifier
;
7785 goto tok_identifier
;
7787 /* allow to take the address of a label */
7788 if (tok
< TOK_UIDENT
)
7789 expect("label identifier");
7790 s
= label_find(tok
);
7792 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
7794 if (s
->r
== LABEL_DECLARED
)
7795 s
->r
= LABEL_FORWARD
;
7798 s
->type
.t
= VT_VOID
;
7799 mk_pointer(&s
->type
);
7800 s
->type
.t
|= VT_STATIC
;
7802 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
7811 expect("identifier");
7815 error("'%s' undeclared", get_tok_str(t
, NULL
));
7816 /* for simple function calls, we tolerate undeclared
7817 external reference to int() function */
7818 if (tcc_state
->warn_implicit_function_declaration
)
7819 warning("implicit declaration of function '%s'",
7820 get_tok_str(t
, NULL
));
7821 s
= external_global_sym(t
, &func_old_type
, 0);
7823 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
7824 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
7825 /* if referencing an inline function, then we generate a
7826 symbol to it if not already done. It will have the
7827 effect to generate code for it at the end of the
7828 compilation unit. Inline function as always
7829 generated in the text section. */
7831 put_extern_sym(s
, text_section
, 0, 0);
7832 r
= VT_SYM
| VT_CONST
;
7836 vset(&s
->type
, r
, s
->c
);
7837 /* if forward reference, we must point to s */
7838 if (vtop
->r
& VT_SYM
) {
7845 /* post operations */
7847 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
7850 } else if (tok
== '.' || tok
== TOK_ARROW
) {
7852 if (tok
== TOK_ARROW
)
7857 /* expect pointer on structure */
7858 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
7859 expect("struct or union");
7863 while ((s
= s
->next
) != NULL
) {
7868 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
7869 /* add field offset to pointer */
7870 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
7873 /* change type to field type, and set to lvalue */
7874 vtop
->type
= s
->type
;
7875 /* an array is never an lvalue */
7876 if (!(vtop
->type
.t
& VT_ARRAY
)) {
7877 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7878 /* if bound checking, the referenced pointer must be checked */
7879 if (do_bounds_check
)
7880 vtop
->r
|= VT_MUSTBOUND
;
7883 } else if (tok
== '[') {
7889 } else if (tok
== '(') {
7895 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7896 /* pointer test (no array accepted) */
7897 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
7898 vtop
->type
= *pointed_type(&vtop
->type
);
7899 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7903 expect("function pointer");
7906 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
7908 /* get return type */
7911 sa
= s
->next
; /* first parameter */
7914 /* compute first implicit argument if a structure is returned */
7915 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
7916 /* get some space for the returned structure */
7917 size
= type_size(&s
->type
, &align
);
7918 loc
= (loc
- size
) & -align
;
7920 ret
.r
= VT_LOCAL
| VT_LVAL
;
7921 /* pass it as 'int' to avoid structure arg passing
7923 vseti(VT_LOCAL
, loc
);
7928 /* return in register */
7929 if (is_float(ret
.type
.t
)) {
7932 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
7941 gfunc_param_typed(s
, sa
);
7951 error("too few arguments to function");
7953 if (!nocode_wanted
) {
7954 gfunc_call(nb_args
);
7956 vtop
-= (nb_args
+ 1);
7959 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
7967 static void uneq(void)
7973 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
7974 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
7975 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
7990 static void expr_prod(void)
7995 while (tok
== '*' || tok
== '/' || tok
== '%') {
8003 static void expr_sum(void)
8008 while (tok
== '+' || tok
== '-') {
8016 static void expr_shift(void)
8021 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
8029 static void expr_cmp(void)
8034 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
8035 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
8043 static void expr_cmpeq(void)
8048 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
8056 static void expr_and(void)
8059 while (tok
== '&') {
8066 static void expr_xor(void)
8069 while (tok
== '^') {
8076 static void expr_or(void)
8079 while (tok
== '|') {
8086 /* XXX: fix this mess */
8087 static void expr_land_const(void)
8090 while (tok
== TOK_LAND
) {
8097 /* XXX: fix this mess */
8098 static void expr_lor_const(void)
8101 while (tok
== TOK_LOR
) {
8108 /* only used if non constant */
8109 static void expr_land(void)
8114 if (tok
== TOK_LAND
) {
8119 if (tok
!= TOK_LAND
) {
8129 static void expr_lor(void)
8134 if (tok
== TOK_LOR
) {
8139 if (tok
!= TOK_LOR
) {
8149 /* XXX: better constant handling */
8150 static void expr_eq(void)
8152 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
8154 CType type
, type1
, type2
;
8161 boolean
.t
= VT_BOOL
;
8167 if (tok
!= ':' || !gnu_ext
) {
8182 if (vtop
!= vstack
) {
8183 /* needed to avoid having different registers saved in
8185 if (is_float(vtop
->type
.t
)) {
8187 #ifdef TCC_TARGET_X86_64
8188 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
8198 if (tok
== ':' && gnu_ext
) {
8206 sv
= *vtop
; /* save value to handle it later */
8207 vtop
--; /* no vpop so that FP stack is not flushed */
8215 bt1
= t1
& VT_BTYPE
;
8217 bt2
= t2
& VT_BTYPE
;
8218 /* cast operands to correct type according to ISOC rules */
8219 if (is_float(bt1
) || is_float(bt2
)) {
8220 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
8221 type
.t
= VT_LDOUBLE
;
8222 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
8227 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
8228 /* cast to biggest op */
8230 /* convert to unsigned if it does not fit in a long long */
8231 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
8232 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
8233 type
.t
|= VT_UNSIGNED
;
8234 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
8235 /* XXX: test pointer compatibility */
8237 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
8238 /* XXX: test function pointer compatibility */
8240 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
8241 /* XXX: test structure compatibility */
8243 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
8244 /* NOTE: as an extension, we accept void on only one side */
8247 /* integer operations */
8249 /* convert to unsigned if it does not fit in an integer */
8250 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
8251 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
8252 type
.t
|= VT_UNSIGNED
;
8255 /* now we convert second operand */
8257 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8260 if (is_float(type
.t
)) {
8262 #ifdef TCC_TARGET_X86_64
8263 if ((type
.t
& VT_BTYPE
) == VT_LDOUBLE
) {
8267 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
8268 /* for long longs, we use fixed registers to avoid having
8269 to handle a complicated move */
8274 /* this is horrible, but we must also convert first
8278 /* put again first value and cast it */
8281 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8291 static void gexpr(void)
8302 /* parse an expression and return its type without any side effect. */
8303 static void expr_type(CType
*type
)
8305 int saved_nocode_wanted
;
8307 saved_nocode_wanted
= nocode_wanted
;
8312 nocode_wanted
= saved_nocode_wanted
;
8315 /* parse a unary expression and return its type without any side
8317 static void unary_type(CType
*type
)
8329 /* parse a constant expression and return value in vtop. */
8330 static void expr_const1(void)
8339 /* parse an integer constant and return its value. */
8340 static int expr_const(void)
8344 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
8345 expect("constant expression");
8351 /* return the label token if current token is a label, otherwise
8353 static int is_label(void)
8357 /* fast test first */
8358 if (tok
< TOK_UIDENT
)
8360 /* no need to save tokc because tok is an identifier */
8367 unget_tok(last_tok
);
8372 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
8373 int case_reg
, int is_expr
)
8378 /* generate line number info */
8380 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
8381 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
8383 last_line_num
= file
->line_num
;
8387 /* default return value is (void) */
8389 vtop
->type
.t
= VT_VOID
;
8392 if (tok
== TOK_IF
) {
8399 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8401 if (c
== TOK_ELSE
) {
8405 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8406 gsym(d
); /* patch else jmp */
8409 } else if (tok
== TOK_WHILE
) {
8417 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8421 } else if (tok
== '{') {
8425 /* record local declaration stack position */
8427 llabel
= local_label_stack
;
8428 /* handle local labels declarations */
8429 if (tok
== TOK_LABEL
) {
8432 if (tok
< TOK_UIDENT
)
8433 expect("label identifier");
8434 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
8444 while (tok
!= '}') {
8449 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8452 /* pop locally defined labels */
8453 label_pop(&local_label_stack
, llabel
);
8454 /* pop locally defined symbols */
8456 /* XXX: this solution makes only valgrind happy...
8457 triggered by gcc.c-torture/execute/20000917-1.c */
8459 switch(vtop
->type
.t
& VT_BTYPE
) {
8464 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
8466 error("unsupported expression type");
8469 sym_pop(&local_stack
, s
);
8471 } else if (tok
== TOK_RETURN
) {
8475 gen_assign_cast(&func_vt
);
8476 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
8478 /* if returning structure, must copy it to implicit
8479 first pointer arg location */
8482 size
= type_size(&func_vt
,&align
);
8485 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
8489 loc
= (loc
- size
) & -4;
8492 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
8495 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
8497 vtop
->type
= int_type
;
8503 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
8506 /* copy structure value to pointer */
8511 } else if (is_float(func_vt
.t
)) {
8516 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
8519 rsym
= gjmp(rsym
); /* jmp */
8520 } else if (tok
== TOK_BREAK
) {
8523 error("cannot break");
8524 *bsym
= gjmp(*bsym
);
8527 } else if (tok
== TOK_CONTINUE
) {
8530 error("cannot continue");
8531 *csym
= gjmp(*csym
);
8534 } else if (tok
== TOK_FOR
) {
8561 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8566 if (tok
== TOK_DO
) {
8571 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8582 if (tok
== TOK_SWITCH
) {
8586 /* XXX: other types than integer */
8587 case_reg
= gv(RC_INT
);
8591 b
= gjmp(0); /* jump to first case */
8593 block(&a
, csym
, &b
, &c
, case_reg
, 0);
8594 /* if no default, jmp after switch */
8602 if (tok
== TOK_CASE
) {
8609 if (gnu_ext
&& tok
== TOK_DOTS
) {
8613 warning("empty case range");
8615 /* since a case is like a label, we must skip it with a jmp */
8622 *case_sym
= gtst(1, 0);
8625 *case_sym
= gtst(1, 0);
8629 *case_sym
= gtst(1, *case_sym
);
8634 goto block_after_label
;
8636 if (tok
== TOK_DEFAULT
) {
8642 error("too many 'default'");
8645 goto block_after_label
;
8647 if (tok
== TOK_GOTO
) {
8649 if (tok
== '*' && gnu_ext
) {
8653 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
8656 } else if (tok
>= TOK_UIDENT
) {
8657 s
= label_find(tok
);
8658 /* put forward definition if needed */
8660 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
8662 if (s
->r
== LABEL_DECLARED
)
8663 s
->r
= LABEL_FORWARD
;
8665 /* label already defined */
8666 if (s
->r
& LABEL_FORWARD
)
8667 s
->next
= (void *)gjmp((long)s
->next
);
8669 gjmp_addr((long)s
->next
);
8672 expect("label identifier");
8675 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
8683 if (s
->r
== LABEL_DEFINED
)
8684 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
8685 gsym((long)s
->next
);
8686 s
->r
= LABEL_DEFINED
;
8688 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
8690 s
->next
= (void *)ind
;
8691 /* we accept this, but it is a mistake */
8694 warning("deprecated use of label at end of compound statement");
8698 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8701 /* expression case */
8716 /* t is the array or struct type. c is the array or struct
8717 address. cur_index/cur_field is the pointer to the current
8718 value. 'size_only' is true if only size info is needed (only used
8720 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
8721 int *cur_index
, Sym
**cur_field
,
8725 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
8731 if (gnu_ext
&& (l
= is_label()) != 0)
8733 while (tok
== '[' || tok
== '.') {
8735 if (!(type
->t
& VT_ARRAY
))
8736 expect("array type");
8739 index
= expr_const();
8740 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
8741 expect("invalid index");
8742 if (tok
== TOK_DOTS
&& gnu_ext
) {
8744 index_last
= expr_const();
8745 if (index_last
< 0 ||
8746 (s
->c
>= 0 && index_last
>= s
->c
) ||
8748 expect("invalid index");
8754 *cur_index
= index_last
;
8755 type
= pointed_type(type
);
8756 elem_size
= type_size(type
, &align
);
8757 c
+= index
* elem_size
;
8758 /* NOTE: we only support ranges for last designator */
8759 nb_elems
= index_last
- index
+ 1;
8760 if (nb_elems
!= 1) {
8769 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
8770 expect("struct/union type");
8783 /* XXX: fix this mess by using explicit storage field */
8785 type1
.t
|= (type
->t
& ~VT_TYPE
);
8799 if (type
->t
& VT_ARRAY
) {
8801 type
= pointed_type(type
);
8802 c
+= index
* type_size(type
, &align
);
8806 error("too many field init");
8807 /* XXX: fix this mess by using explicit storage field */
8809 type1
.t
|= (type
->t
& ~VT_TYPE
);
8814 decl_initializer(type
, sec
, c
, 0, size_only
);
8816 /* XXX: make it more general */
8817 if (!size_only
&& nb_elems
> 1) {
8818 unsigned long c_end
;
8823 error("range init not supported yet for dynamic storage");
8824 c_end
= c
+ nb_elems
* elem_size
;
8825 if (c_end
> sec
->data_allocated
)
8826 section_realloc(sec
, c_end
);
8827 src
= sec
->data
+ c
;
8829 for(i
= 1; i
< nb_elems
; i
++) {
8831 memcpy(dst
, src
, elem_size
);
8837 #define EXPR_CONST 1
8840 /* store a value or an expression directly in global data or in local array */
8841 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
8842 int v
, int expr_type
)
8844 int saved_global_expr
, bt
, bit_pos
, bit_size
;
8846 unsigned long long bit_mask
;
8854 /* compound literals must be allocated globally in this case */
8855 saved_global_expr
= global_expr
;
8858 global_expr
= saved_global_expr
;
8859 /* NOTE: symbols are accepted */
8860 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
8861 error("initializer element is not constant");
8869 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
8872 /* XXX: not portable */
8873 /* XXX: generate error if incorrect relocation */
8874 gen_assign_cast(&dtype
);
8875 bt
= type
->t
& VT_BTYPE
;
8876 /* we'll write at most 12 bytes */
8877 if (c
+ 12 > sec
->data_allocated
) {
8878 section_realloc(sec
, c
+ 12);
8880 ptr
= sec
->data
+ c
;
8881 /* XXX: make code faster ? */
8882 if (!(type
->t
& VT_BITFIELD
)) {
8887 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
8888 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
8889 bit_mask
= (1LL << bit_size
) - 1;
8891 if ((vtop
->r
& VT_SYM
) &&
8897 (bt
== VT_INT
&& bit_size
!= 32)))
8898 error("initializer element is not computable at load time");
8901 vtop
->c
.i
= (vtop
->c
.i
!= 0);
8903 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8906 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8909 *(double *)ptr
= vtop
->c
.d
;
8912 *(long double *)ptr
= vtop
->c
.ld
;
8915 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
8918 if (vtop
->r
& VT_SYM
) {
8919 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
8921 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8926 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
8933 /* put zeros for variable based init */
8934 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
8937 /* nothing to do because globals are already set to zero */
8939 vpush_global_sym(&func_old_type
, TOK_memset
);
8947 /* 't' contains the type and storage info. 'c' is the offset of the
8948 object in section 'sec'. If 'sec' is NULL, it means stack based
8949 allocation. 'first' is true if array '{' must be read (multi
8950 dimension implicit array init handling). 'size_only' is true if
8951 size only evaluation is wanted (only for arrays). */
8952 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
8953 int first
, int size_only
)
8955 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
8956 int size1
, align1
, expr_type
;
8960 if (type
->t
& VT_ARRAY
) {
8964 t1
= pointed_type(type
);
8965 size1
= type_size(t1
, &align1
);
8968 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
8974 /* only parse strings here if correct type (otherwise: handle
8975 them as ((w)char *) expressions */
8976 if ((tok
== TOK_LSTR
&&
8977 #ifdef TCC_TARGET_PE
8978 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
8980 (t1
->t
& VT_BTYPE
) == VT_INT
8982 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
8983 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8988 /* compute maximum number of chars wanted */
8990 cstr_len
= cstr
->size
;
8992 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
8995 if (n
>= 0 && nb
> (n
- array_length
))
8996 nb
= n
- array_length
;
8999 warning("initializer-string for array is too long");
9000 /* in order to go faster for common case (char
9001 string in global variable, we handle it
9003 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
9004 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
9008 ch
= ((unsigned char *)cstr
->data
)[i
];
9010 ch
= ((nwchar_t
*)cstr
->data
)[i
];
9011 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
9019 /* only add trailing zero if enough storage (no
9020 warning in this case since it is standard) */
9021 if (n
< 0 || array_length
< n
) {
9023 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
9029 while (tok
!= '}') {
9030 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
9031 if (n
>= 0 && index
>= n
)
9032 error("index too large");
9033 /* must put zero in holes (note that doing it that way
9034 ensures that it even works with designators) */
9035 if (!size_only
&& array_length
< index
) {
9036 init_putz(t1
, sec
, c
+ array_length
* size1
,
9037 (index
- array_length
) * size1
);
9040 if (index
> array_length
)
9041 array_length
= index
;
9042 /* special test for multi dimensional arrays (may not
9043 be strictly correct if designators are used at the
9045 if (index
>= n
&& no_oblock
)
9054 /* put zeros at the end */
9055 if (!size_only
&& n
>= 0 && array_length
< n
) {
9056 init_putz(t1
, sec
, c
+ array_length
* size1
,
9057 (n
- array_length
) * size1
);
9059 /* patch type size if needed */
9061 s
->c
= array_length
;
9062 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
9063 (sec
|| !first
|| tok
== '{')) {
9066 /* NOTE: the previous test is a specific case for automatic
9067 struct/union init */
9068 /* XXX: union needs only one init */
9070 /* XXX: this test is incorrect for local initializers
9071 beginning with ( without {. It would be much more difficult
9072 to do it correctly (ideally, the expression parser should
9073 be used in all cases) */
9079 while (tok
== '(') {
9083 if (!parse_btype(&type1
, &ad1
))
9085 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
9087 if (!is_assignable_types(type
, &type1
))
9088 error("invalid type for cast");
9093 if (first
|| tok
== '{') {
9102 while (tok
!= '}') {
9103 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
9105 if (!size_only
&& array_length
< index
) {
9106 init_putz(type
, sec
, c
+ array_length
,
9107 index
- array_length
);
9109 index
= index
+ type_size(&f
->type
, &align1
);
9110 if (index
> array_length
)
9111 array_length
= index
;
9113 if (no_oblock
&& f
== NULL
)
9119 /* put zeros at the end */
9120 if (!size_only
&& array_length
< n
) {
9121 init_putz(type
, sec
, c
+ array_length
,
9130 } else if (tok
== '{') {
9132 decl_initializer(type
, sec
, c
, first
, size_only
);
9134 } else if (size_only
) {
9135 /* just skip expression */
9137 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
9141 else if (tok
== ')')
9146 /* currently, we always use constant expression for globals
9147 (may change for scripting case) */
9148 expr_type
= EXPR_CONST
;
9150 expr_type
= EXPR_ANY
;
9151 init_putv(type
, sec
, c
, 0, expr_type
);
9155 /* parse an initializer for type 't' if 'has_init' is non zero, and
9156 allocate space in local or global data space ('r' is either
9157 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
9158 variable 'v' of scope 'scope' is declared before initializers are
9159 parsed. If 'v' is zero, then a reference to the new object is put
9160 in the value stack. If 'has_init' is 2, a special parsing is done
9161 to handle string constants. */
9162 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
9163 int has_init
, int v
, int scope
)
9165 int size
, align
, addr
, data_offset
;
9167 ParseState saved_parse_state
;
9168 TokenString init_str
;
9171 size
= type_size(type
, &align
);
9172 /* If unknown size, we must evaluate it before
9173 evaluating initializers because
9174 initializers can generate global data too
9175 (e.g. string pointers or ISOC99 compound
9176 literals). It also simplifies local
9177 initializers handling */
9178 tok_str_new(&init_str
);
9181 error("unknown type size");
9182 /* get all init string */
9183 if (has_init
== 2) {
9184 /* only get strings */
9185 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
9186 tok_str_add_tok(&init_str
);
9191 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
9193 error("unexpected end of file in initializer");
9194 tok_str_add_tok(&init_str
);
9197 else if (tok
== '}') {
9207 tok_str_add(&init_str
, -1);
9208 tok_str_add(&init_str
, 0);
9211 save_parse_state(&saved_parse_state
);
9213 macro_ptr
= init_str
.str
;
9215 decl_initializer(type
, NULL
, 0, 1, 1);
9216 /* prepare second initializer parsing */
9217 macro_ptr
= init_str
.str
;
9220 /* if still unknown size, error */
9221 size
= type_size(type
, &align
);
9223 error("unknown type size");
9225 /* take into account specified alignment if bigger */
9227 if (ad
->aligned
> align
)
9228 align
= ad
->aligned
;
9229 } else if (ad
->packed
) {
9232 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
9234 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
9236 loc
= (loc
- size
) & -align
;
9238 /* handles bounds */
9239 /* XXX: currently, since we do only one pass, we cannot track
9240 '&' operators, so we add only arrays */
9241 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
9242 unsigned long *bounds_ptr
;
9243 /* add padding between regions */
9245 /* then add local bound info */
9246 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
9247 bounds_ptr
[0] = addr
;
9248 bounds_ptr
[1] = size
;
9251 /* local variable */
9252 sym_push(v
, type
, r
, addr
);
9254 /* push local reference */
9255 vset(type
, r
, addr
);
9261 if (v
&& scope
== VT_CONST
) {
9262 /* see if the symbol was already defined */
9265 if (!is_compatible_types(&sym
->type
, type
))
9266 error("incompatible types for redefinition of '%s'",
9267 get_tok_str(v
, NULL
));
9268 if (sym
->type
.t
& VT_EXTERN
) {
9269 /* if the variable is extern, it was not allocated */
9270 sym
->type
.t
&= ~VT_EXTERN
;
9271 /* set array size if it was ommited in extern
9273 if ((sym
->type
.t
& VT_ARRAY
) &&
9274 sym
->type
.ref
->c
< 0 &&
9276 sym
->type
.ref
->c
= type
->ref
->c
;
9278 /* we accept several definitions of the same
9279 global variable. this is tricky, because we
9280 must play with the SHN_COMMON type of the symbol */
9281 /* XXX: should check if the variable was already
9282 initialized. It is incorrect to initialized it
9284 /* no init data, we won't add more to the symbol */
9291 /* allocate symbol in corresponding section */
9296 else if (tcc_state
->nocommon
)
9300 data_offset
= sec
->data_offset
;
9301 data_offset
= (data_offset
+ align
- 1) & -align
;
9303 /* very important to increment global pointer at this time
9304 because initializers themselves can create new initializers */
9305 data_offset
+= size
;
9306 /* add padding if bound check */
9307 if (do_bounds_check
)
9309 sec
->data_offset
= data_offset
;
9310 /* allocate section space to put the data */
9311 if (sec
->sh_type
!= SHT_NOBITS
&&
9312 data_offset
> sec
->data_allocated
)
9313 section_realloc(sec
, data_offset
);
9314 /* align section if needed */
9315 if (align
> sec
->sh_addralign
)
9316 sec
->sh_addralign
= align
;
9318 addr
= 0; /* avoid warning */
9322 if (scope
!= VT_CONST
|| !sym
) {
9323 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
9325 /* update symbol definition */
9327 put_extern_sym(sym
, sec
, addr
, size
);
9330 /* put a common area */
9331 put_extern_sym(sym
, NULL
, align
, size
);
9332 /* XXX: find a nicer way */
9333 esym
= &((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
];
9334 esym
->st_shndx
= SHN_COMMON
;
9339 /* push global reference */
9340 sym
= get_sym_ref(type
, sec
, addr
, size
);
9342 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
9346 /* handles bounds now because the symbol must be defined
9347 before for the relocation */
9348 if (do_bounds_check
) {
9349 unsigned long *bounds_ptr
;
9351 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
9352 /* then add global bound info */
9353 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
9354 bounds_ptr
[0] = 0; /* relocated */
9355 bounds_ptr
[1] = size
;
9359 decl_initializer(type
, sec
, addr
, 1, 0);
9360 /* restore parse state if needed */
9362 tok_str_free(init_str
.str
);
9363 restore_parse_state(&saved_parse_state
);
9369 void put_func_debug(Sym
*sym
)
9374 /* XXX: we put here a dummy type */
9375 snprintf(buf
, sizeof(buf
), "%s:%c1",
9376 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
9377 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
9378 cur_text_section
, sym
->c
);
9379 /* //gr gdb wants a line at the function */
9380 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
9385 /* parse an old style function declaration list */
9386 /* XXX: check multiple parameter */
9387 static void func_decl_list(Sym
*func_sym
)
9394 /* parse each declaration */
9395 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
9396 if (!parse_btype(&btype
, &ad
))
9397 expect("declaration list");
9398 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9399 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9401 /* we accept no variable after */
9405 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9406 /* find parameter in function parameter list */
9409 if ((s
->v
& ~SYM_FIELD
) == v
)
9413 error("declaration for parameter '%s' but no such parameter",
9414 get_tok_str(v
, NULL
));
9416 /* check that no storage specifier except 'register' was given */
9417 if (type
.t
& VT_STORAGE
)
9418 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
9419 convert_parameter_type(&type
);
9420 /* we can add the type (NOTE: it could be local to the function) */
9422 /* accept other parameters */
9433 /* parse a function defined by symbol 'sym' and generate its code in
9434 'cur_text_section' */
9435 static void gen_function(Sym
*sym
)
9437 int saved_nocode_wanted
= nocode_wanted
;
9439 ind
= cur_text_section
->data_offset
;
9440 /* NOTE: we patch the symbol size later */
9441 put_extern_sym(sym
, cur_text_section
, ind
, 0);
9442 funcname
= get_tok_str(sym
->v
, NULL
);
9444 /* put debug symbol */
9446 put_func_debug(sym
);
9447 /* push a dummy symbol to enable local sym storage */
9448 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
9449 gfunc_prolog(&sym
->type
);
9451 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
9454 cur_text_section
->data_offset
= ind
;
9455 label_pop(&global_label_stack
, NULL
);
9456 sym_pop(&local_stack
, NULL
); /* reset local stack */
9457 /* end of function */
9458 /* patch symbol size */
9459 ((ElfW(Sym
) *)symtab_section
->data
)[sym
->c
].st_size
=
9462 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
9464 /* It's better to crash than to generate wrong code */
9465 cur_text_section
= NULL
;
9466 funcname
= ""; /* for safety */
9467 func_vt
.t
= VT_VOID
; /* for safety */
9468 ind
= 0; /* for safety */
9469 nocode_wanted
= saved_nocode_wanted
;
9472 static void gen_inline_functions(void)
9476 int *str
, inline_generated
;
9478 /* iterate while inline function are referenced */
9480 inline_generated
= 0;
9481 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9483 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9484 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9485 (VT_STATIC
| VT_INLINE
) &&
9487 /* the function was used: generate its code and
9488 convert it to a normal function */
9489 str
= INLINE_DEF(sym
->r
);
9490 sym
->r
= VT_SYM
| VT_CONST
;
9491 sym
->type
.t
&= ~VT_INLINE
;
9495 cur_text_section
= text_section
;
9497 macro_ptr
= NULL
; /* fail safe */
9500 inline_generated
= 1;
9503 if (!inline_generated
)
9507 /* free all remaining inline function tokens */
9508 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9510 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9511 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9512 (VT_STATIC
| VT_INLINE
)) {
9513 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9514 if (sym
->r
== (VT_SYM
| VT_CONST
)) //gr beware!
9516 str
= INLINE_DEF(sym
->r
);
9518 sym
->r
= 0; /* fail safe */
9523 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9524 static void decl(int l
)
9532 if (!parse_btype(&btype
, &ad
)) {
9533 /* skip redundant ';' */
9534 /* XXX: find more elegant solution */
9539 if (l
== VT_CONST
&&
9540 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
9541 /* global asm block */
9545 /* special test for old K&R protos without explicit int
9546 type. Only accepted when defining global data */
9547 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
9551 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9552 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9554 /* we accept no variable after */
9558 while (1) { /* iterate thru each declaration */
9560 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9564 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
9565 printf("type = '%s'\n", buf
);
9568 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9569 /* if old style function prototype, we accept a
9572 if (sym
->c
== FUNC_OLD
)
9573 func_decl_list(sym
);
9578 error("cannot use local functions");
9579 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
9580 expect("function definition");
9582 /* reject abstract declarators in function definition */
9584 while ((sym
= sym
->next
) != NULL
)
9585 if (!(sym
->v
& ~SYM_FIELD
))
9586 expect("identifier");
9588 /* XXX: cannot do better now: convert extern line to static inline */
9589 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
9590 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
9594 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
9596 /* specific case: if not func_call defined, we put
9597 the one of the prototype */
9598 /* XXX: should have default value */
9599 r
= sym
->type
.ref
->r
;
9600 if (FUNC_CALL(r
) != FUNC_CDECL
9601 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
9602 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
9604 FUNC_EXPORT(type
.ref
->r
) = 1;
9606 if (!is_compatible_types(&sym
->type
, &type
)) {
9608 error("incompatible types for redefinition of '%s'",
9609 get_tok_str(v
, NULL
));
9611 /* if symbol is already defined, then put complete type */
9614 /* put function symbol */
9615 sym
= global_identifier_push(v
, type
.t
, 0);
9616 sym
->type
.ref
= type
.ref
;
9619 /* static inline functions are just recorded as a kind
9620 of macro. Their code will be emitted at the end of
9621 the compilation unit only if they are used */
9622 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
9623 (VT_INLINE
| VT_STATIC
)) {
9624 TokenString func_str
;
9627 tok_str_new(&func_str
);
9633 error("unexpected end of file");
9634 tok_str_add_tok(&func_str
);
9639 } else if (t
== '}') {
9641 if (block_level
== 0)
9645 tok_str_add(&func_str
, -1);
9646 tok_str_add(&func_str
, 0);
9647 INLINE_DEF(sym
->r
) = func_str
.str
;
9649 /* compute text section */
9650 cur_text_section
= ad
.section
;
9651 if (!cur_text_section
)
9652 cur_text_section
= text_section
;
9653 sym
->r
= VT_SYM
| VT_CONST
;
9658 if (btype
.t
& VT_TYPEDEF
) {
9659 /* save typedefed type */
9660 /* XXX: test storage specifiers ? */
9661 sym
= sym_push(v
, &type
, 0, 0);
9662 sym
->type
.t
|= VT_TYPEDEF
;
9663 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9664 /* external function definition */
9665 /* specific case for func_call attribute */
9667 type
.ref
->r
= ad
.func_attr
;
9668 external_sym(v
, &type
, 0);
9670 /* not lvalue if array */
9672 if (!(type
.t
& VT_ARRAY
))
9673 r
|= lvalue_type(type
.t
);
9674 has_init
= (tok
== '=');
9675 if ((btype
.t
& VT_EXTERN
) ||
9676 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
9677 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
9678 /* external variable */
9679 /* NOTE: as GCC, uninitialized global static
9680 arrays of null size are considered as
9682 external_sym(v
, &type
, r
);
9684 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
9685 if (type
.t
& VT_STATIC
)
9691 decl_initializer_alloc(&type
, &ad
, r
,
9705 /* better than nothing, but needs extension to handle '-E' option
9707 static void preprocess_init(TCCState
*s1
)
9709 s1
->include_stack_ptr
= s1
->include_stack
;
9710 /* XXX: move that before to avoid having to initialize
9711 file->ifdef_stack_ptr ? */
9712 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
9713 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
9715 /* XXX: not ANSI compliant: bound checking says error */
9717 s1
->pack_stack
[0] = 0;
9718 s1
->pack_stack_ptr
= s1
->pack_stack
;
9721 /* compile the C file opened in 'file'. Return non zero if errors. */
9722 static int tcc_compile(TCCState
*s1
)
9726 volatile int section_sym
;
9729 printf("%s: **** new file\n", file
->filename
);
9731 preprocess_init(s1
);
9733 cur_text_section
= NULL
;
9735 anon_sym
= SYM_FIRST_ANOM
;
9737 /* file info: full path + filename */
9738 section_sym
= 0; /* avoid warning */
9740 section_sym
= put_elf_sym(symtab_section
, 0, 0,
9741 ELFW(ST_INFO
)(STB_LOCAL
, STT_SECTION
), 0,
9742 text_section
->sh_num
, NULL
);
9743 getcwd(buf
, sizeof(buf
));
9745 normalize_slashes(buf
);
9747 pstrcat(buf
, sizeof(buf
), "/");
9748 put_stabs_r(buf
, N_SO
, 0, 0,
9749 text_section
->data_offset
, text_section
, section_sym
);
9750 put_stabs_r(file
->filename
, N_SO
, 0, 0,
9751 text_section
->data_offset
, text_section
, section_sym
);
9753 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9754 symbols can be safely used */
9755 put_elf_sym(symtab_section
, 0, 0,
9756 ELFW(ST_INFO
)(STB_LOCAL
, STT_FILE
), 0,
9757 SHN_ABS
, file
->filename
);
9759 /* define some often used types */
9760 int_type
.t
= VT_INT
;
9762 char_pointer_type
.t
= VT_BYTE
;
9763 mk_pointer(&char_pointer_type
);
9765 func_old_type
.t
= VT_FUNC
;
9766 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
9768 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9769 float_type
.t
= VT_FLOAT
;
9770 double_type
.t
= VT_DOUBLE
;
9772 func_float_type
.t
= VT_FUNC
;
9773 func_float_type
.ref
= sym_push(SYM_FIELD
, &float_type
, FUNC_CDECL
, FUNC_OLD
);
9774 func_double_type
.t
= VT_FUNC
;
9775 func_double_type
.ref
= sym_push(SYM_FIELD
, &double_type
, FUNC_CDECL
, FUNC_OLD
);
9779 /* define 'void *alloca(unsigned int)' builtin function */
9784 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
9785 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
9788 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
9792 define_start
= define_stack
;
9795 if (setjmp(s1
->error_jmp_buf
) == 0) {
9797 s1
->error_set_jmp_enabled
= 1;
9799 ch
= file
->buf_ptr
[0];
9800 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9801 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
;
9805 expect("declaration");
9807 /* end of translation unit info */
9809 put_stabs_r(NULL
, N_SO
, 0, 0,
9810 text_section
->data_offset
, text_section
, section_sym
);
9813 s1
->error_set_jmp_enabled
= 0;
9815 /* reset define stack, but leave -Dsymbols (may be incorrect if
9816 they are undefined) */
9817 free_defines(define_start
);
9819 gen_inline_functions();
9821 sym_pop(&global_stack
, NULL
);
9822 sym_pop(&local_stack
, NULL
);
9824 return s1
->nb_errors
!= 0 ? -1 : 0;
9827 /* Preprocess the current file */
9828 /* XXX: add line and file infos,
9829 * XXX: add options to preserve spaces (partly done, only spaces in macro are
9832 static int tcc_preprocess(TCCState
*s1
)
9835 BufferedFile
*file_ref
;
9836 int token_seen
, line_ref
;
9838 preprocess_init(s1
);
9839 define_start
= define_stack
;
9840 ch
= file
->buf_ptr
[0];
9842 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9843 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
9844 PARSE_FLAG_LINEFEED
;
9852 if (tok
== TOK_EOF
) {
9854 } else if (tok
== TOK_LINEFEED
) {
9859 } else if (token_seen
) {
9860 fwrite(tok_spaces
.data
, tok_spaces
.size
, 1, s1
->outfile
);
9862 int d
= file
->line_num
- line_ref
;
9863 if (file
!= file_ref
|| d
< 0 || d
>= 8)
9864 fprintf(s1
->outfile
, "# %d \"%s\"\n", file
->line_num
, file
->filename
);
9867 fputs("\n", s1
->outfile
), --d
;
9868 line_ref
= (file_ref
= file
)->line_num
;
9871 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
9873 free_defines(define_start
);
9878 int tcc_compile_string(TCCState
*s
, const char *str
)
9880 BufferedFile bf1
, *bf
= &bf1
;
9884 /* init file structure */
9886 /* XXX: avoid copying */
9888 buf
= tcc_malloc(len
+ 1);
9891 memcpy(buf
, str
, len
);
9894 bf
->buf_end
= buf
+ len
;
9895 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
9898 ret
= tcc_compile(s
);
9902 /* currently, no need to close */
9907 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9908 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
9910 BufferedFile bf1
, *bf
= &bf1
;
9912 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
9913 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
9917 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
9919 /* init file structure */
9921 bf
->buf_ptr
= bf
->buffer
;
9922 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
9923 *bf
->buf_end
= CH_EOB
;
9924 bf
->filename
[0] = '\0';
9928 s1
->include_stack_ptr
= s1
->include_stack
;
9930 /* parse with define parser */
9931 ch
= file
->buf_ptr
[0];
9937 /* undefine a preprocessor symbol */
9938 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
9942 ts
= tok_alloc(sym
, strlen(sym
));
9943 s
= define_find(ts
->tok
);
9944 /* undefine symbol by putting an invalid name */
9949 #ifdef CONFIG_TCC_ASM
9951 #ifdef TCC_TARGET_I386
9952 #include "i386-asm.c"
9957 static void asm_instr(void)
9959 error("inline asm() not supported");
9961 static void asm_global_instr(void)
9963 error("inline asm() not supported");
9969 #ifdef TCC_TARGET_COFF
9970 #include "tcccoff.c"
9973 #ifdef TCC_TARGET_PE
9977 /* print the position in the source file of PC value 'pc' by reading
9978 the stabs debug information */
9979 static void rt_printline(unsigned long wanted_pc
)
9981 Stab_Sym
*sym
, *sym_end
;
9982 char func_name
[128], last_func_name
[128];
9983 unsigned long func_addr
, last_pc
, pc
;
9984 const char *incl_files
[INCLUDE_STACK_SIZE
];
9985 int incl_index
, len
, last_line_num
, i
;
9986 const char *str
, *p
;
9988 fprintf(stderr
, "0x%08lx:", wanted_pc
);
9990 func_name
[0] = '\0';
9993 last_func_name
[0] = '\0';
9994 last_pc
= 0xffffffff;
9996 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
9997 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
9998 while (sym
< sym_end
) {
9999 switch(sym
->n_type
) {
10000 /* function start or end */
10002 if (sym
->n_strx
== 0) {
10003 /* we test if between last line and end of function */
10004 pc
= sym
->n_value
+ func_addr
;
10005 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
10007 func_name
[0] = '\0';
10010 str
= stabstr_section
->data
+ sym
->n_strx
;
10011 p
= strchr(str
, ':');
10013 pstrcpy(func_name
, sizeof(func_name
), str
);
10016 if (len
> sizeof(func_name
) - 1)
10017 len
= sizeof(func_name
) - 1;
10018 memcpy(func_name
, str
, len
);
10019 func_name
[len
] = '\0';
10021 func_addr
= sym
->n_value
;
10024 /* line number info */
10026 pc
= sym
->n_value
+ func_addr
;
10027 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
10030 last_line_num
= sym
->n_desc
;
10032 strcpy(last_func_name
, func_name
);
10034 /* include files */
10036 str
= stabstr_section
->data
+ sym
->n_strx
;
10038 if (incl_index
< INCLUDE_STACK_SIZE
) {
10039 incl_files
[incl_index
++] = str
;
10043 if (incl_index
> 1)
10047 if (sym
->n_strx
== 0) {
10048 incl_index
= 0; /* end of translation unit */
10050 str
= stabstr_section
->data
+ sym
->n_strx
;
10051 /* do not add path */
10053 if (len
> 0 && str
[len
- 1] != '/')
10061 /* second pass: we try symtab symbols (no line number info) */
10064 ElfW(Sym
) *sym
, *sym_end
;
10067 sym_end
= (ElfW(Sym
) *)(symtab_section
->data
+ symtab_section
->data_offset
);
10068 for(sym
= (ElfW(Sym
) *)symtab_section
->data
+ 1;
10071 type
= ELFW(ST_TYPE
)(sym
->st_info
);
10072 if (type
== STT_FUNC
) {
10073 if (wanted_pc
>= sym
->st_value
&&
10074 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
10075 pstrcpy(last_func_name
, sizeof(last_func_name
),
10076 strtab_section
->data
+ sym
->st_name
);
10082 /* did not find any info: */
10083 fprintf(stderr
, " ???\n");
10086 if (last_func_name
[0] != '\0') {
10087 fprintf(stderr
, " %s()", last_func_name
);
10089 if (incl_index
> 0) {
10090 fprintf(stderr
, " (%s:%d",
10091 incl_files
[incl_index
- 1], last_line_num
);
10092 for(i
= incl_index
- 2; i
>= 0; i
--)
10093 fprintf(stderr
, ", included from %s", incl_files
[i
]);
10094 fprintf(stderr
, ")");
10096 fprintf(stderr
, "\n");
10099 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
10103 /* fix for glibc 2.1 */
10105 #define REG_EIP EIP
10106 #define REG_EBP EBP
10109 /* return the PC at frame level 'level'. Return non zero if not found */
10110 static int rt_get_caller_pc(unsigned long *paddr
,
10111 ucontext_t
*uc
, int level
)
10117 #if defined(__FreeBSD__)
10118 *paddr
= uc
->uc_mcontext
.mc_eip
;
10119 #elif defined(__dietlibc__)
10120 *paddr
= uc
->uc_mcontext
.eip
;
10122 *paddr
= uc
->uc_mcontext
.gregs
[REG_EIP
];
10126 #if defined(__FreeBSD__)
10127 fp
= uc
->uc_mcontext
.mc_ebp
;
10128 #elif defined(__dietlibc__)
10129 fp
= uc
->uc_mcontext
.ebp
;
10131 fp
= uc
->uc_mcontext
.gregs
[REG_EBP
];
10133 for(i
=1;i
<level
;i
++) {
10134 /* XXX: check address validity with program info */
10135 if (fp
<= 0x1000 || fp
>= 0xc0000000)
10137 fp
= ((unsigned long *)fp
)[0];
10139 *paddr
= ((unsigned long *)fp
)[1];
10143 #elif defined(__x86_64__)
10144 /* return the PC at frame level 'level'. Return non zero if not found */
10145 static int rt_get_caller_pc(unsigned long *paddr
,
10146 ucontext_t
*uc
, int level
)
10152 /* XXX: only support linux */
10153 *paddr
= uc
->uc_mcontext
.gregs
[REG_RIP
];
10156 fp
= uc
->uc_mcontext
.gregs
[REG_RBP
];
10157 for(i
=1;i
<level
;i
++) {
10158 /* XXX: check address validity with program info */
10159 if (fp
<= 0x1000 || fp
>= 0xc0000000)
10161 fp
= ((unsigned long *)fp
)[0];
10163 *paddr
= ((unsigned long *)fp
)[1];
10169 #warning add arch specific rt_get_caller_pc()
10171 static int rt_get_caller_pc(unsigned long *paddr
,
10172 ucontext_t
*uc
, int level
)
10178 /* emit a run time error at position 'pc' */
10179 void rt_error(ucontext_t
*uc
, const char *fmt
, ...)
10186 fprintf(stderr
, "Runtime error: ");
10187 vfprintf(stderr
, fmt
, ap
);
10188 fprintf(stderr
, "\n");
10189 for(i
=0;i
<num_callers
;i
++) {
10190 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
10193 fprintf(stderr
, "at ");
10195 fprintf(stderr
, "by ");
10202 /* signal handler for fatal errors */
10203 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
10205 ucontext_t
*uc
= puc
;
10209 switch(siginf
->si_code
) {
10212 rt_error(uc
, "division by zero");
10215 rt_error(uc
, "floating point exception");
10221 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
10222 rt_error(uc
, *rt_bound_error_msg
);
10224 rt_error(uc
, "dereferencing invalid pointer");
10227 rt_error(uc
, "illegal instruction");
10230 rt_error(uc
, "abort() called");
10233 rt_error(uc
, "caught signal %d", signum
);
10240 /* do all relocations (needed before using tcc_get_symbol()) */
10241 int tcc_relocate(TCCState
*s1
)
10248 #ifdef TCC_TARGET_PE
10249 pe_add_runtime(s1
);
10251 tcc_add_runtime(s1
);
10254 relocate_common_syms();
10256 tcc_add_linker_symbols(s1
);
10257 #ifndef TCC_TARGET_PE
10258 build_got_entries(s1
);
10260 /* compute relocation address : section are relocated in place. We
10261 also alloc the bss space */
10262 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10263 s
= s1
->sections
[i
];
10264 if (s
->sh_flags
& SHF_ALLOC
) {
10265 if (s
->sh_type
== SHT_NOBITS
)
10266 s
->data
= tcc_mallocz(s
->data_offset
);
10267 s
->sh_addr
= (unsigned long)s
->data
;
10271 relocate_syms(s1
, 1);
10273 if (s1
->nb_errors
!= 0)
10276 /* relocate each section */
10277 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10278 s
= s1
->sections
[i
];
10280 relocate_section(s1
, s
);
10283 /* mark executable sections as executable in memory */
10284 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10285 s
= s1
->sections
[i
];
10286 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_EXECINSTR
)) ==
10287 (SHF_ALLOC
| SHF_EXECINSTR
))
10288 set_pages_executable(s
->data
, s
->data_offset
);
10293 /* launch the compiled program with the given arguments */
10294 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
10296 int (*prog_main
)(int, char **);
10298 if (tcc_relocate(s1
) < 0)
10301 prog_main
= tcc_get_symbol_err(s1
, "main");
10304 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10305 error("debug mode currently not available for Windows");
10307 struct sigaction sigact
;
10308 /* install TCC signal handlers to print debug info on fatal
10310 sigact
.sa_flags
= SA_SIGINFO
| SA_RESETHAND
;
10311 sigact
.sa_sigaction
= sig_error
;
10312 sigemptyset(&sigact
.sa_mask
);
10313 sigaction(SIGFPE
, &sigact
, NULL
);
10314 sigaction(SIGILL
, &sigact
, NULL
);
10315 sigaction(SIGSEGV
, &sigact
, NULL
);
10316 sigaction(SIGBUS
, &sigact
, NULL
);
10317 sigaction(SIGABRT
, &sigact
, NULL
);
10321 #ifdef CONFIG_TCC_BCHECK
10322 if (do_bounds_check
) {
10323 void (*bound_init
)(void);
10325 /* set error function */
10326 rt_bound_error_msg
= (void *)tcc_get_symbol_err(s1
,
10327 "__bound_error_msg");
10329 /* XXX: use .init section so that it also work in binary ? */
10330 bound_init
= (void *)tcc_get_symbol_err(s1
, "__bound_init");
10334 return (*prog_main
)(argc
, argv
);
10337 void tcc_memstats(void)
10340 printf("memory in use: %d\n", mem_cur_size
);
10344 static void tcc_cleanup(void)
10348 if (NULL
== tcc_state
)
10352 /* free -D defines */
10353 free_defines(NULL
);
10356 n
= tok_ident
- TOK_IDENT
;
10357 for(i
= 0; i
< n
; i
++)
10358 tcc_free(table_ident
[i
]);
10359 tcc_free(table_ident
);
10361 /* free sym_pools */
10362 dynarray_reset(&sym_pools
, &nb_sym_pools
);
10363 /* string buffer */
10364 cstr_free(&tokcstr
);
10365 /* reset symbol stack */
10366 sym_free_first
= NULL
;
10367 /* cleanup from error/setjmp */
10371 TCCState
*tcc_new(void)
10380 s
= tcc_mallocz(sizeof(TCCState
));
10384 s
->output_type
= TCC_OUTPUT_MEMORY
;
10386 /* init isid table */
10387 for(i
=CH_EOF
;i
<256;i
++)
10388 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
10390 /* add all tokens */
10391 table_ident
= NULL
;
10392 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
10394 tok_ident
= TOK_IDENT
;
10403 ts
= tok_alloc(p
, r
- p
- 1);
10407 /* we add dummy defines for some special macros to speed up tests
10408 and to have working defined() */
10409 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
10410 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
10411 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
10412 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
10414 /* standard defines */
10415 tcc_define_symbol(s
, "__STDC__", NULL
);
10416 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
10417 #if defined(TCC_TARGET_I386)
10418 tcc_define_symbol(s
, "__i386__", NULL
);
10420 #if defined(TCC_TARGET_X86_64)
10421 tcc_define_symbol(s
, "__x86_64__", NULL
);
10423 #if defined(TCC_TARGET_ARM)
10424 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
10425 tcc_define_symbol(s
, "__arm_elf__", NULL
);
10426 tcc_define_symbol(s
, "__arm_elf", NULL
);
10427 tcc_define_symbol(s
, "arm_elf", NULL
);
10428 tcc_define_symbol(s
, "__arm__", NULL
);
10429 tcc_define_symbol(s
, "__arm", NULL
);
10430 tcc_define_symbol(s
, "arm", NULL
);
10431 tcc_define_symbol(s
, "__APCS_32__", NULL
);
10433 #ifdef TCC_TARGET_PE
10434 tcc_define_symbol(s
, "_WIN32", NULL
);
10436 tcc_define_symbol(s
, "__unix__", NULL
);
10437 tcc_define_symbol(s
, "__unix", NULL
);
10438 #if defined(__linux)
10439 tcc_define_symbol(s
, "__linux__", NULL
);
10440 tcc_define_symbol(s
, "__linux", NULL
);
10443 /* tiny C specific defines */
10444 tcc_define_symbol(s
, "__TINYC__", NULL
);
10446 /* tiny C & gcc defines */
10447 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
10448 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
10449 #ifdef TCC_TARGET_PE
10450 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
10452 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
10455 #ifndef TCC_TARGET_PE
10456 /* default library paths */
10457 tcc_add_library_path(s
, CONFIG_SYSROOT
"/usr/local/lib");
10458 tcc_add_library_path(s
, CONFIG_SYSROOT
"/usr/lib");
10459 tcc_add_library_path(s
, CONFIG_SYSROOT
"/lib");
10462 /* no section zero */
10463 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
10465 /* create standard sections */
10466 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
10467 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
10468 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
10470 /* symbols are always generated for linking stage */
10471 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
10473 ".hashtab", SHF_PRIVATE
);
10474 strtab_section
= symtab_section
->link
;
10476 /* private symbol table for dynamic symbols */
10477 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
10479 ".dynhashtab", SHF_PRIVATE
);
10480 s
->alacarte_link
= 1;
10482 #ifdef CHAR_IS_UNSIGNED
10483 s
->char_is_unsigned
= 1;
10485 #if defined(TCC_TARGET_PE) && 0
10486 /* XXX: currently the PE linker is not ready to support that */
10487 s
->leading_underscore
= 1;
10490 #ifdef TCC_TARGET_X86_64
10491 s
->jmp_table
= NULL
;
10496 void tcc_delete(TCCState
*s1
)
10502 /* free all sections */
10503 free_section(s1
->dynsymtab_section
);
10505 for(i
= 1; i
< s1
->nb_sections
; i
++)
10506 free_section(s1
->sections
[i
]);
10507 tcc_free(s1
->sections
);
10509 /* free any loaded DLLs */
10510 for ( i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
10512 DLLReference
*ref
= s1
->loaded_dlls
[i
];
10514 dlclose(ref
->handle
);
10517 /* free loaded dlls array */
10518 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
10520 /* free library paths */
10521 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
10523 /* free include paths */
10524 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
10525 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
10526 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
10528 #ifdef TCC_TARGET_X86_64
10529 tcc_free(s1
->jmp_table
);
10534 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
10538 pathname1
= tcc_strdup(pathname
);
10539 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
10543 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
10547 pathname1
= tcc_strdup(pathname
);
10548 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
10552 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
10557 BufferedFile
*saved_file
;
10559 /* find source file type with extension */
10560 ext
= tcc_fileextension(filename
);
10564 /* open the file */
10566 file
= tcc_open(s1
, filename
);
10568 if (flags
& AFF_PRINT_ERROR
) {
10569 error_noabort("file '%s' not found", filename
);
10575 if (flags
& AFF_PREPROCESS
) {
10576 ret
= tcc_preprocess(s1
);
10577 } else if (!ext
[0] || !strcmp(ext
, "c")) {
10578 /* C file assumed */
10579 ret
= tcc_compile(s1
);
10581 #ifdef CONFIG_TCC_ASM
10582 if (!strcmp(ext
, "S")) {
10583 /* preprocessed assembler */
10584 ret
= tcc_assemble(s1
, 1);
10585 } else if (!strcmp(ext
, "s")) {
10586 /* non preprocessed assembler */
10587 ret
= tcc_assemble(s1
, 0);
10590 #ifdef TCC_TARGET_PE
10591 if (!strcmp(ext
, "def")) {
10592 ret
= pe_load_def_file(s1
, file
->fd
);
10597 /* assume executable format: auto guess file type */
10598 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
10599 lseek(fd
, 0, SEEK_SET
);
10601 error_noabort("could not read header");
10603 } else if (ret
!= sizeof(ehdr
)) {
10604 goto try_load_script
;
10607 if (ehdr
.e_ident
[0] == ELFMAG0
&&
10608 ehdr
.e_ident
[1] == ELFMAG1
&&
10609 ehdr
.e_ident
[2] == ELFMAG2
&&
10610 ehdr
.e_ident
[3] == ELFMAG3
) {
10611 file
->line_num
= 0; /* do not display line number if error */
10612 if (ehdr
.e_type
== ET_REL
) {
10613 ret
= tcc_load_object_file(s1
, fd
, 0);
10614 } else if (ehdr
.e_type
== ET_DYN
) {
10615 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
10616 #ifdef TCC_TARGET_PE
10620 h
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
10627 ret
= tcc_load_dll(s1
, fd
, filename
,
10628 (flags
& AFF_REFERENCED_DLL
) != 0);
10631 error_noabort("unrecognized ELF file");
10634 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
10635 file
->line_num
= 0; /* do not display line number if error */
10636 ret
= tcc_load_archive(s1
, fd
);
10638 #ifdef TCC_TARGET_COFF
10639 if (*(uint16_t *)(&ehdr
) == COFF_C67_MAGIC
) {
10640 ret
= tcc_load_coff(s1
, fd
);
10643 #ifdef TCC_TARGET_PE
10644 if (pe_test_res_file(&ehdr
, ret
)) {
10645 ret
= pe_load_res_file(s1
, fd
);
10649 /* as GNU ld, consider it is an ld script if not recognized */
10651 ret
= tcc_load_ldscript(s1
);
10653 error_noabort("unrecognized file type");
10668 int tcc_add_file(TCCState
*s
, const char *filename
)
10670 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
10673 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
10677 pathname1
= tcc_strdup(pathname
);
10678 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
10682 /* find and load a dll. Return non zero if not found */
10683 /* XXX: add '-rpath' option support ? */
10684 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
10689 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10690 snprintf(buf
, sizeof(buf
), "%s/%s",
10691 s
->library_paths
[i
], filename
);
10692 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
10698 /* the library name is the same as the argument of the '-l' option */
10699 int tcc_add_library(TCCState
*s
, const char *libraryname
)
10704 /* first we look for the dynamic library if not static linking */
10705 if (!s
->static_link
) {
10706 #ifdef TCC_TARGET_PE
10707 snprintf(buf
, sizeof(buf
), "%s.def", libraryname
);
10709 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
10711 if (tcc_add_dll(s
, buf
, 0) == 0)
10715 /* then we look for the static library */
10716 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10717 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
10718 s
->library_paths
[i
], libraryname
);
10719 if (tcc_add_file_internal(s
, buf
, 0) == 0)
10725 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
10727 add_elf_sym(symtab_section
, val
, 0,
10728 ELFW(ST_INFO
)(STB_GLOBAL
, STT_NOTYPE
), 0,
10733 int tcc_set_output_type(TCCState
*s
, int output_type
)
10737 s
->output_type
= output_type
;
10739 if (!s
->nostdinc
) {
10740 /* default include paths */
10741 /* XXX: reverse order needed if -isystem support */
10742 #ifndef TCC_TARGET_PE
10743 tcc_add_sysinclude_path(s
, CONFIG_SYSROOT
"/usr/local/include");
10744 tcc_add_sysinclude_path(s
, CONFIG_SYSROOT
"/usr/include");
10746 snprintf(buf
, sizeof(buf
), "%s/include", tcc_lib_path
);
10747 tcc_add_sysinclude_path(s
, buf
);
10748 #ifdef TCC_TARGET_PE
10749 snprintf(buf
, sizeof(buf
), "%s/include/winapi", tcc_lib_path
);
10750 tcc_add_sysinclude_path(s
, buf
);
10754 /* if bound checking, then add corresponding sections */
10755 #ifdef CONFIG_TCC_BCHECK
10756 if (do_bounds_check
) {
10757 /* define symbol */
10758 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
10759 /* create bounds sections */
10760 bounds_section
= new_section(s
, ".bounds",
10761 SHT_PROGBITS
, SHF_ALLOC
);
10762 lbounds_section
= new_section(s
, ".lbounds",
10763 SHT_PROGBITS
, SHF_ALLOC
);
10767 if (s
->char_is_unsigned
) {
10768 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
10771 /* add debug sections */
10774 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
10775 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
10776 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
10777 put_elf_str(stabstr_section
, "");
10778 stab_section
->link
= stabstr_section
;
10779 /* put first entry */
10780 put_stabs("", 0, 0, 0, 0);
10783 /* add libc crt1/crti objects */
10784 #ifndef TCC_TARGET_PE
10785 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
10787 if (output_type
!= TCC_OUTPUT_DLL
)
10788 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
10789 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
10793 #ifdef TCC_TARGET_PE
10794 snprintf(buf
, sizeof(buf
), "%s/lib", tcc_lib_path
);
10795 tcc_add_library_path(s
, buf
);
10801 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10802 #define FD_INVERT 0x0002 /* invert value before storing */
10804 typedef struct FlagDef
{
10810 static const FlagDef warning_defs
[] = {
10811 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
10812 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
10813 { offsetof(TCCState
, warn_error
), 0, "error" },
10814 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
10815 "implicit-function-declaration" },
10818 static int set_flag(TCCState
*s
, const FlagDef
*flags
, int nb_flags
,
10819 const char *name
, int value
)
10826 if (r
[0] == 'n' && r
[1] == 'o' && r
[2] == '-') {
10830 for(i
= 0, p
= flags
; i
< nb_flags
; i
++, p
++) {
10831 if (!strcmp(r
, p
->name
))
10836 if (p
->flags
& FD_INVERT
)
10838 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
10843 /* set/reset a warning */
10844 int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
10849 if (!strcmp(warning_name
, "all")) {
10850 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
10851 if (p
->flags
& WD_ALL
)
10852 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
10856 return set_flag(s
, warning_defs
, countof(warning_defs
),
10857 warning_name
, value
);
10861 static const FlagDef flag_defs
[] = {
10862 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
10863 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
10864 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
10865 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
10868 /* set/reset a flag */
10869 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
)
10871 return set_flag(s
, flag_defs
, countof(flag_defs
),
10875 #if !defined(LIBTCC)
10877 static int64_t getclock_us(void)
10882 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
10885 gettimeofday(&tv
, NULL
);
10886 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
10892 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10893 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10894 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10895 " [-static] [infile1 infile2...] [-run infile args...]\n"
10897 "General options:\n"
10898 " -v display current version, increase verbosity\n"
10899 " -c compile only - generate an object file\n"
10900 " -o outfile set output filename\n"
10901 " -Bdir set tcc internal library path\n"
10902 " -bench output compilation statistics\n"
10903 " -run run compiled source\n"
10904 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10905 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10906 " -w disable all warnings\n"
10907 "Preprocessor options:\n"
10908 " -E preprocess only\n"
10909 " -Idir add include path 'dir'\n"
10910 " -Dsym[=val] define 'sym' with value 'val'\n"
10911 " -Usym undefine 'sym'\n"
10912 "Linker options:\n"
10913 " -Ldir add library path 'dir'\n"
10914 " -llib link with dynamic or static library 'lib'\n"
10915 " -shared generate a shared library\n"
10916 " -soname set name for shared library to be used at runtime\n"
10917 " -static static linking\n"
10918 " -rdynamic export all global symbols to dynamic linker\n"
10919 " -r generate (relocatable) object file\n"
10920 "Debugger options:\n"
10921 " -g generate runtime debug info\n"
10922 #ifdef CONFIG_TCC_BCHECK
10923 " -b compile with built-in memory and bounds checker (implies -g)\n"
10925 " -bt N show N callers in stack traces\n"
10929 #define TCC_OPTION_HAS_ARG 0x0001
10930 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10932 typedef struct TCCOption
{
10961 TCC_OPTION_nostdinc
,
10962 TCC_OPTION_nostdlib
,
10963 TCC_OPTION_print_search_dirs
,
10964 TCC_OPTION_rdynamic
,
10972 static const TCCOption tcc_options
[] = {
10973 { "h", TCC_OPTION_HELP
, 0 },
10974 { "?", TCC_OPTION_HELP
, 0 },
10975 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
10976 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
10977 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
10978 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
10979 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
10980 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10981 { "bench", TCC_OPTION_bench
, 0 },
10982 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
10983 #ifdef CONFIG_TCC_BCHECK
10984 { "b", TCC_OPTION_b
, 0 },
10986 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10987 { "c", TCC_OPTION_c
, 0 },
10988 { "static", TCC_OPTION_static
, 0 },
10989 { "shared", TCC_OPTION_shared
, 0 },
10990 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
10991 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
10992 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10993 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
10994 { "r", TCC_OPTION_r
, 0 },
10995 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10996 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10997 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10998 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
10999 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
11000 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
11001 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
11002 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
11003 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
11004 { "w", TCC_OPTION_w
, 0 },
11005 { "pipe", TCC_OPTION_pipe
, 0},
11006 { "E", TCC_OPTION_E
, 0},
11010 /* convert 'str' into an array of space separated strings */
11011 static int expand_args(char ***pargv
, const char *str
)
11020 while (is_space(*str
))
11025 while (*str
!= '\0' && !is_space(*str
))
11028 arg
= tcc_malloc(len
+ 1);
11029 memcpy(arg
, s1
, len
);
11031 dynarray_add((void ***)&argv
, &argc
, arg
);
11037 static char **files
;
11038 static int nb_files
, nb_libraries
;
11039 static int multiple_files
;
11040 static int print_search_dirs
;
11041 static int output_type
;
11042 static int reloc_output
;
11043 static const char *outfile
;
11045 int parse_args(TCCState
*s
, int argc
, char **argv
)
11048 const TCCOption
*popt
;
11049 const char *optarg
, *p1
, *r1
;
11053 while (optind
< argc
) {
11055 r
= argv
[optind
++];
11056 if (r
[0] != '-' || r
[1] == '\0') {
11057 /* add a new file */
11058 dynarray_add((void ***)&files
, &nb_files
, r
);
11059 if (!multiple_files
) {
11061 /* argv[0] will be this file */
11065 /* find option in table (match only the first chars */
11066 popt
= tcc_options
;
11070 error("invalid option -- '%s'", r
);
11083 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
11084 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
11087 if (optind
>= argc
)
11088 error("argument to '%s' is missing", r
);
11089 optarg
= argv
[optind
++];
11097 switch(popt
->index
) {
11098 case TCC_OPTION_HELP
:
11102 if (tcc_add_include_path(s
, optarg
) < 0)
11103 error("too many include paths");
11108 sym
= (char *)optarg
;
11109 value
= strchr(sym
, '=');
11114 tcc_define_symbol(s
, sym
, value
);
11118 tcc_undefine_symbol(s
, optarg
);
11121 tcc_add_library_path(s
, optarg
);
11124 /* set tcc utilities path (mainly for tcc development) */
11125 tcc_lib_path
= optarg
;
11128 dynarray_add((void ***)&files
, &nb_files
, r
);
11131 case TCC_OPTION_bench
:
11134 case TCC_OPTION_bt
:
11135 num_callers
= atoi(optarg
);
11137 #ifdef CONFIG_TCC_BCHECK
11139 do_bounds_check
= 1;
11147 multiple_files
= 1;
11148 output_type
= TCC_OUTPUT_OBJ
;
11150 case TCC_OPTION_static
:
11151 s
->static_link
= 1;
11153 case TCC_OPTION_shared
:
11154 output_type
= TCC_OUTPUT_DLL
;
11156 case TCC_OPTION_soname
:
11157 s
->soname
= optarg
;
11160 multiple_files
= 1;
11164 /* generate a .o merging several output files */
11166 output_type
= TCC_OUTPUT_OBJ
;
11168 case TCC_OPTION_nostdinc
:
11171 case TCC_OPTION_nostdlib
:
11174 case TCC_OPTION_print_search_dirs
:
11175 print_search_dirs
= 1;
11177 case TCC_OPTION_run
:
11181 argc1
= expand_args(&argv1
, optarg
);
11183 parse_args(s
, argc1
, argv1
);
11185 multiple_files
= 0;
11186 output_type
= TCC_OUTPUT_MEMORY
;
11191 if (0 == verbose
++)
11192 printf("tcc version %s\n", TCC_VERSION
);
11193 } while (*optarg
++ == 'v');
11196 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
11197 goto unsupported_option
;
11200 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
11201 s
->warn_unsupported
)
11202 goto unsupported_option
;
11207 case TCC_OPTION_rdynamic
:
11210 case TCC_OPTION_Wl
:
11213 if (strstart(optarg
, "-Ttext,", &p
)) {
11214 s
->text_addr
= strtoul(p
, NULL
, 16);
11215 s
->has_text_addr
= 1;
11216 } else if (strstart(optarg
, "--oformat,", &p
)) {
11217 if (strstart(p
, "elf32-", NULL
)) {
11218 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
11219 } else if (!strcmp(p
, "binary")) {
11220 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
11222 #ifdef TCC_TARGET_COFF
11223 if (!strcmp(p
, "coff")) {
11224 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
11228 error("target %s not found", p
);
11231 error("unsupported linker option '%s'", optarg
);
11236 output_type
= TCC_OUTPUT_PREPROCESS
;
11239 if (s
->warn_unsupported
) {
11240 unsupported_option
:
11241 warning("unsupported option '%s'", r
);
11250 int main(int argc
, char **argv
)
11254 int nb_objfiles
, ret
, optind
;
11255 char objfilename
[1024];
11256 int64_t start_time
= 0;
11259 tcc_lib_path
= w32_tcc_lib_path();
11263 output_type
= TCC_OUTPUT_EXE
;
11265 multiple_files
= 1;
11270 print_search_dirs
= 0;
11273 optind
= parse_args(s
, argc
- 1, argv
+ 1);
11274 if (print_search_dirs
) {
11275 /* enough for Linux kernel */
11276 printf("install: %s/\n", tcc_lib_path
);
11279 if (optind
== 0 || nb_files
== 0) {
11280 if (optind
&& verbose
)
11286 nb_objfiles
= nb_files
- nb_libraries
;
11288 /* if outfile provided without other options, we output an
11290 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
11291 output_type
= TCC_OUTPUT_EXE
;
11293 /* check -c consistency : only single file handled. XXX: checks file type */
11294 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
11295 /* accepts only a single input file */
11296 if (nb_objfiles
!= 1)
11297 error("cannot specify multiple files with -c");
11298 if (nb_libraries
!= 0)
11299 error("cannot specify libraries with -c");
11303 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
11305 s
->outfile
= stdout
;
11307 s
->outfile
= fopen(outfile
, "w");
11309 error("could not open '%s", outfile
);
11311 } else if (output_type
!= TCC_OUTPUT_MEMORY
) {
11313 /* compute default outfile name */
11316 strcmp(files
[0], "-") == 0 ? "a" : tcc_basename(files
[0]);
11317 pstrcpy(objfilename
, sizeof(objfilename
), name
);
11318 ext
= tcc_fileextension(objfilename
);
11319 #ifdef TCC_TARGET_PE
11320 if (output_type
== TCC_OUTPUT_DLL
)
11321 strcpy(ext
, ".dll");
11323 if (output_type
== TCC_OUTPUT_EXE
)
11324 strcpy(ext
, ".exe");
11327 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
&& *ext
)
11330 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
11331 outfile
= objfilename
;
11336 start_time
= getclock_us();
11339 tcc_set_output_type(s
, output_type
);
11341 /* compile or add each files or library */
11342 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
11343 const char *filename
;
11345 filename
= files
[i
];
11346 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
11347 if (tcc_add_file_internal(s
, filename
,
11348 AFF_PRINT_ERROR
| AFF_PREPROCESS
) < 0)
11350 } else if (filename
[0] == '-' && filename
[1]) {
11351 if (tcc_add_library(s
, filename
+ 2) < 0)
11352 error("cannot find %s", filename
);
11355 printf("-> %s\n", filename
);
11356 if (tcc_add_file(s
, filename
) < 0)
11361 /* free all files */
11369 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
11370 if (total_time
< 0.001)
11371 total_time
= 0.001;
11372 if (total_bytes
< 1)
11374 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11375 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
11376 total_time
, (int)(total_lines
/ total_time
),
11377 total_bytes
/ total_time
/ 1000000.0);
11380 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
11382 fclose(s
->outfile
);
11383 } else if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
11384 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
11386 ret
= tcc_output_file(s
, outfile
) ? 1 : 0;
11388 /* XXX: cannot do it with bound checking because of the malloc hooks */
11389 if (!do_bounds_check
)
11394 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);