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
42 #include <sys/timeb.h>
47 #include <sys/ucontext.h>
51 #endif /* !CONFIG_TCCBOOT */
68 /* preprocessor debug */
70 /* include file debug */
78 /* target selection */
79 //#define TCC_TARGET_I386 /* i386 code generator */
80 //#define TCC_TARGET_ARM /* ARMv4 code generator */
81 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
83 /* default target is I386 */
84 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
85 !defined(TCC_TARGET_C67)
86 #define TCC_TARGET_I386
89 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
90 !defined(TCC_TARGET_C67)
91 #define CONFIG_TCC_BCHECK /* enable bound checking code */
94 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
95 #define CONFIG_TCC_STATIC
98 /* define it to include assembler support */
99 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
100 #define CONFIG_TCC_ASM
103 /* object format selection */
104 #if defined(TCC_TARGET_C67)
105 #define TCC_TARGET_COFF
114 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
115 executables or dlls */
116 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
118 #define INCLUDE_STACK_SIZE 32
119 #define IFDEF_STACK_SIZE 64
120 #define VSTACK_SIZE 256
121 #define STRING_MAX_SIZE 1024
122 #define PACK_STACK_SIZE 8
124 #define TOK_HASH_SIZE 8192 /* must be a power of two */
125 #define TOK_ALLOC_INCR 512 /* must be a power of two */
126 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
128 /* token symbol management */
129 typedef struct TokenSym
{
130 struct TokenSym
*hash_next
;
131 struct Sym
*sym_define
; /* direct pointer to define */
132 struct Sym
*sym_label
; /* direct pointer to label */
133 struct Sym
*sym_struct
; /* direct pointer to structure */
134 struct Sym
*sym_identifier
; /* direct pointer to identifier */
135 int tok
; /* token number */
141 typedef unsigned short nwchar_t
;
143 typedef int nwchar_t
;
146 typedef struct CString
{
147 int size
; /* size in bytes */
148 void *data
; /* either 'char *' or 'nwchar_t *' */
150 void *data_allocated
; /* if non NULL, data has been malloced */
153 /* type definition */
154 typedef struct CType
{
160 typedef union CValue
{
166 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
168 unsigned long long ull
;
169 struct CString
*cstr
;
175 typedef struct SValue
{
176 CType type
; /* type */
177 unsigned short r
; /* register + flags */
178 unsigned short r2
; /* second register, used for 'long long'
179 type. If not used, set to VT_CONST */
180 CValue c
; /* constant, if VT_CONST */
181 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
184 /* symbol management */
186 int v
; /* symbol token */
187 int r
; /* associated register */
188 int c
; /* associated number */
189 CType type
; /* associated type */
190 struct Sym
*next
; /* next related symbol */
191 struct Sym
*prev
; /* prev symbol in stack */
192 struct Sym
*prev_tok
; /* previous symbol for this token */
195 /* section definition */
196 /* XXX: use directly ELF structure for parameters ? */
197 /* special flag to indicate that the section should not be linked to
199 #define SHF_PRIVATE 0x80000000
201 typedef struct Section
{
202 unsigned long data_offset
; /* current data offset */
203 unsigned char *data
; /* section data */
204 unsigned long data_allocated
; /* used for realloc() handling */
205 int sh_name
; /* elf section name (only used during output) */
206 int sh_num
; /* elf section number */
207 int sh_type
; /* elf section type */
208 int sh_flags
; /* elf section flags */
209 int sh_info
; /* elf section info */
210 int sh_addralign
; /* elf section alignment */
211 int sh_entsize
; /* elf entry size */
212 unsigned long sh_size
; /* section size (only used during output) */
213 unsigned long sh_addr
; /* address at which the section is relocated */
214 unsigned long sh_offset
; /* file offset */
215 int nb_hashed_syms
; /* used to resize the hash table */
216 struct Section
*link
; /* link to another section */
217 struct Section
*reloc
; /* corresponding section for relocation, if any */
218 struct Section
*hash
; /* hash table for symbols */
219 struct Section
*next
;
220 char name
[1]; /* section name */
223 typedef struct DLLReference
{
229 /* GNUC attribute definition */
230 typedef struct AttributeDef
{
234 int func_attr
; /* calling convention, exports, ... */
237 /* -------------------------------------------------- */
238 /* gr: wrappers for casting sym->r for other purposes */
246 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
247 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
248 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
249 #define INLINE_DEF(r) (*(int **)&(r))
250 /* -------------------------------------------------- */
252 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
253 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
254 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
256 /* stored in 'Sym.c' field */
257 #define FUNC_NEW 1 /* ansi function prototype */
258 #define FUNC_OLD 2 /* old function prototype */
259 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
261 /* stored in 'Sym.r' field */
262 #define FUNC_CDECL 0 /* standard c call */
263 #define FUNC_STDCALL 1 /* pascal c call */
264 #define FUNC_FASTCALL1 2 /* first param in %eax */
265 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
266 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
267 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
269 /* field 'Sym.t' for macros */
270 #define MACRO_OBJ 0 /* object like macro */
271 #define MACRO_FUNC 1 /* function like macro */
273 /* field 'Sym.r' for C labels */
274 #define LABEL_DEFINED 0 /* label is defined */
275 #define LABEL_FORWARD 1 /* label is forward defined */
276 #define LABEL_DECLARED 2 /* label is declared but never used */
278 /* type_decl() types */
279 #define TYPE_ABSTRACT 1 /* type without variable */
280 #define TYPE_DIRECT 2 /* type with variable */
282 #define IO_BUF_SIZE 8192
284 typedef struct BufferedFile
{
288 int line_num
; /* current line number - here to simplify code */
289 int ifndef_macro
; /* #ifndef macro / #endif search */
290 int ifndef_macro_saved
; /* saved ifndef_macro */
291 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
292 char inc_type
; /* type of include */
293 char inc_filename
[512]; /* filename specified by the user */
294 char filename
[1024]; /* current filename - here to simplify code */
295 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
298 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
299 #define CH_EOF (-1) /* end of file */
301 /* parsing state (used to save parser state to reparse part of the
302 source several times) */
303 typedef struct ParseState
{
310 /* used to record tokens */
311 typedef struct TokenString
{
318 /* include file cache, used to find files faster and also to eliminate
319 inclusion if the include file is protected by #ifndef ... #endif */
320 typedef struct CachedInclude
{
322 int hash_next
; /* -1 if none */
323 char type
; /* '"' or '>' to give include type */
324 char filename
[1]; /* path specified in #include */
327 #define CACHED_INCLUDES_HASH_SIZE 512
330 static struct BufferedFile
*file
;
333 static CString tokcstr
; /* current parsed string, if any */
334 /* additional informations about token */
335 static int tok_flags
;
336 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
337 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
338 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
339 #define TOK_FLAG_EOF 0x0008 /* end of file */
341 static int *macro_ptr
, *macro_ptr_allocated
;
342 static int *unget_saved_macro_ptr
;
343 static int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
344 static int unget_buffer_enabled
;
345 static int parse_flags
;
346 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
347 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
348 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
349 token. line feed is also
351 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
353 static Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
354 static Section
*cur_text_section
; /* current section where function code is
356 #ifdef CONFIG_TCC_ASM
357 static Section
*last_text_section
; /* to handle .previous asm directive */
359 /* bound check related sections */
360 static Section
*bounds_section
; /* contains global data bound description */
361 static Section
*lbounds_section
; /* contains local data bound description */
362 /* symbol sections */
363 static Section
*symtab_section
, *strtab_section
;
366 static Section
*stab_section
, *stabstr_section
;
368 /* loc : local variable index
369 ind : output code index
371 anon_sym: anonymous symbol index
373 static int rsym
, anon_sym
, ind
, loc
;
374 /* expression generation modifiers */
375 static int const_wanted
; /* true if constant wanted */
376 static int nocode_wanted
; /* true if no code generation wanted for an expression */
377 static int global_expr
; /* true if compound literals must be allocated
378 globally (used during initializers parsing */
379 static CType func_vt
; /* current function return type (used by return
382 static int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
383 static int tok_ident
;
384 static TokenSym
**table_ident
;
385 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
386 static char token_buf
[STRING_MAX_SIZE
+ 1];
387 static char *funcname
;
388 static Sym
*global_stack
, *local_stack
;
389 static Sym
*define_stack
;
390 static Sym
*global_label_stack
, *local_label_stack
;
391 /* symbol allocator */
392 #define SYM_POOL_NB (8192 / sizeof(Sym))
393 static Sym
*sym_free_first
;
394 static void **sym_pools
;
395 static int nb_sym_pools
;
397 static SValue vstack
[VSTACK_SIZE
], *vtop
;
398 /* some predefined types */
399 static CType char_pointer_type
, func_old_type
, int_type
;
400 /* true if isid(c) || isnum(c) */
401 static unsigned char isidnum_table
[256-CH_EOF
];
403 /* display some information during compilation */
404 static int verbose
= 0;
406 /* compile with debug symbol (and use them if error during execution) */
407 static int do_debug
= 0;
409 /* compile with built-in memory and bounds checker */
410 static int do_bounds_check
= 0;
412 /* display benchmark infos */
414 static int do_bench
= 0;
416 static int total_lines
;
417 static int total_bytes
;
419 /* use GNU C extensions */
420 static int gnu_ext
= 1;
422 /* use Tiny C extensions */
423 static int tcc_ext
= 1;
425 /* max number of callers shown if error */
426 static int num_callers
= 6;
427 static const char **rt_bound_error_msg
;
429 /* XXX: get rid of this ASAP */
430 static struct TCCState
*tcc_state
;
432 /* give the path of the tcc libraries */
433 static const char *tcc_lib_path
= CONFIG_TCCDIR
;
438 BufferedFile
**include_stack_ptr
;
439 int *ifdef_stack_ptr
;
441 /* include file handling */
442 char **include_paths
;
443 int nb_include_paths
;
444 char **sysinclude_paths
;
445 int nb_sysinclude_paths
;
446 CachedInclude
**cached_includes
;
447 int nb_cached_includes
;
449 char **library_paths
;
450 int nb_library_paths
;
452 /* array of all loaded dlls (including those referenced by loaded
454 DLLReference
**loaded_dlls
;
459 int nb_sections
; /* number of sections, including first dummy section */
464 unsigned long *got_offsets
;
466 /* give the correspondance from symtab indexes to dynsym indexes */
467 int *symtab_to_dynsym
;
469 /* temporary dynamic symbol sections (for dll loading) */
470 Section
*dynsymtab_section
;
471 /* exported dynamic symbol section */
474 int nostdinc
; /* if true, no standard headers are added */
475 int nostdlib
; /* if true, no standard libraries are added */
477 int nocommon
; /* if true, do not use common symbols for .bss data */
479 /* if true, static linking is performed */
482 /* soname as specified on the command line (-soname) */
485 /* if true, all symbols are exported */
488 /* if true, only link in referenced objects from archive */
491 /* address of text section */
492 unsigned long text_addr
;
495 /* output format, see TCC_OUTPUT_FORMAT_xxx */
498 /* C language options */
499 int char_is_unsigned
;
500 int leading_underscore
;
502 /* warning switches */
503 int warn_write_strings
;
504 int warn_unsupported
;
507 int warn_implicit_function_declaration
;
511 void (*error_func
)(void *opaque
, const char *msg
);
512 int error_set_jmp_enabled
;
513 jmp_buf error_jmp_buf
;
516 /* tiny assembler state */
519 /* see include_stack_ptr */
520 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
522 /* see ifdef_stack_ptr */
523 int ifdef_stack
[IFDEF_STACK_SIZE
];
525 /* see cached_includes */
526 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
529 int pack_stack
[PACK_STACK_SIZE
];
532 /* output file for preprocessing */
536 /* The current value can be: */
537 #define VT_VALMASK 0x00ff
538 #define VT_CONST 0x00f0 /* constant in vc
539 (must be first non register value) */
540 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
541 #define VT_LOCAL 0x00f2 /* offset on stack */
542 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
543 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
544 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
545 #define VT_LVAL 0x0100 /* var is an lvalue */
546 #define VT_SYM 0x0200 /* a symbol value is added */
547 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
548 char/short stored in integer registers) */
549 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
550 dereferencing value */
551 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
552 bounding function call point is in vc */
553 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
554 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
555 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
556 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
559 #define VT_INT 0 /* integer type */
560 #define VT_BYTE 1 /* signed byte type */
561 #define VT_SHORT 2 /* short type */
562 #define VT_VOID 3 /* void type */
563 #define VT_PTR 4 /* pointer */
564 #define VT_ENUM 5 /* enum definition */
565 #define VT_FUNC 6 /* function type */
566 #define VT_STRUCT 7 /* struct/union definition */
567 #define VT_FLOAT 8 /* IEEE float */
568 #define VT_DOUBLE 9 /* IEEE double */
569 #define VT_LDOUBLE 10 /* IEEE long double */
570 #define VT_BOOL 11 /* ISOC99 boolean type */
571 #define VT_LLONG 12 /* 64 bit integer */
572 #define VT_LONG 13 /* long integer (NEVER USED as type, only
574 #define VT_BTYPE 0x000f /* mask for basic type */
575 #define VT_UNSIGNED 0x0010 /* unsigned type */
576 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
577 #define VT_BITFIELD 0x0040 /* bitfield modifier */
578 #define VT_CONSTANT 0x0800 /* const modifier */
579 #define VT_VOLATILE 0x1000 /* volatile modifier */
580 #define VT_SIGNED 0x2000 /* signed type */
583 #define VT_EXTERN 0x00000080 /* extern definition */
584 #define VT_STATIC 0x00000100 /* static variable */
585 #define VT_TYPEDEF 0x00000200 /* typedef definition */
586 #define VT_INLINE 0x00000400 /* inline definition */
588 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
590 /* type mask (except storage) */
591 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
592 #define VT_TYPE (~(VT_STORAGE))
596 /* warning: the following compare tokens depend on i386 asm code */
603 #define TOK_Nset 0x98
604 #define TOK_Nclear 0x99
610 #define TOK_LAND 0xa0
614 #define TOK_MID 0xa3 /* inc/dec, to void constant */
616 #define TOK_UDIV 0xb0 /* unsigned division */
617 #define TOK_UMOD 0xb1 /* unsigned modulo */
618 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
619 #define TOK_CINT 0xb3 /* number in tokc */
620 #define TOK_CCHAR 0xb4 /* char constant in tokc */
621 #define TOK_STR 0xb5 /* pointer to string in tokc */
622 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
623 #define TOK_LCHAR 0xb7
624 #define TOK_LSTR 0xb8
625 #define TOK_CFLOAT 0xb9 /* float constant */
626 #define TOK_LINENUM 0xba /* line number info */
627 #define TOK_CDOUBLE 0xc0 /* double constant */
628 #define TOK_CLDOUBLE 0xc1 /* long double constant */
629 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
630 #define TOK_ADDC1 0xc3 /* add with carry generation */
631 #define TOK_ADDC2 0xc4 /* add with carry use */
632 #define TOK_SUBC1 0xc5 /* add with carry generation */
633 #define TOK_SUBC2 0xc6 /* add with carry use */
634 #define TOK_CUINT 0xc8 /* unsigned int constant */
635 #define TOK_CLLONG 0xc9 /* long long constant */
636 #define TOK_CULLONG 0xca /* unsigned long long constant */
637 #define TOK_ARROW 0xcb
638 #define TOK_DOTS 0xcc /* three dots */
639 #define TOK_SHR 0xcd /* unsigned shift right */
640 #define TOK_PPNUM 0xce /* preprocessor number */
642 #define TOK_SHL 0x01 /* shift left */
643 #define TOK_SAR 0x02 /* signed shift right */
645 /* assignement operators : normal operator or 0x80 */
646 #define TOK_A_MOD 0xa5
647 #define TOK_A_AND 0xa6
648 #define TOK_A_MUL 0xaa
649 #define TOK_A_ADD 0xab
650 #define TOK_A_SUB 0xad
651 #define TOK_A_DIV 0xaf
652 #define TOK_A_XOR 0xde
653 #define TOK_A_OR 0xfc
654 #define TOK_A_SHL 0x81
655 #define TOK_A_SAR 0x82
658 #define offsetof(type, field) ((size_t) &((type *)0)->field)
662 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
665 /* WARNING: the content of this string encodes token numbers */
666 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";
668 #define TOK_EOF (-1) /* end of file */
669 #define TOK_LINEFEED 10 /* line feed */
671 /* all identificators and strings have token above that */
672 #define TOK_IDENT 256
674 /* only used for i386 asm opcodes definitions */
675 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
678 DEF(TOK_ASM_ ## x ## b, #x "b") \
679 DEF(TOK_ASM_ ## x ## w, #x "w") \
680 DEF(TOK_ASM_ ## x ## l, #x "l") \
681 DEF(TOK_ASM_ ## x, #x)
684 DEF(TOK_ASM_ ## x ## w, #x "w") \
685 DEF(TOK_ASM_ ## x ## l, #x "l") \
686 DEF(TOK_ASM_ ## x, #x)
689 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
690 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
691 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
692 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
695 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
696 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
699 #define DEF_ASMTEST(x) \
731 #define TOK_ASM_int TOK_INT
734 TOK_LAST
= TOK_IDENT
- 1,
735 #define DEF(id, str) id,
740 static const char tcc_keywords
[] =
741 #define DEF(id, str) str "\0"
746 #define TOK_UIDENT TOK_DEFINE
749 #define snprintf _snprintf
750 #define vsnprintf _vsnprintf
752 #define strtold (long double)strtod
753 #define strtof (float)strtod
754 #define strtoll (long long)strtol
756 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
757 || defined(__OpenBSD__)
758 /* currently incorrect */
759 long double strtold(const char *nptr
, char **endptr
)
761 return (long double)strtod(nptr
, endptr
);
763 float strtof(const char *nptr
, char **endptr
)
765 return (float)strtod(nptr
, endptr
);
768 /* XXX: need to define this to use them in non ISOC99 context */
769 extern float strtof (const char *__nptr
, char **__endptr
);
770 extern long double strtold (const char *__nptr
, char **__endptr
);
773 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
774 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
775 static char *tcc_basename(const char *name
);
776 static char *tcc_fileextension (const char *p
);
778 static void next(void);
779 static void next_nomacro(void);
780 static void parse_expr_type(CType
*type
);
781 static void expr_type(CType
*type
);
782 static void unary_type(CType
*type
);
783 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
784 int case_reg
, int is_expr
);
785 static int expr_const(void);
786 static void expr_eq(void);
787 static void gexpr(void);
788 static void gen_inline_functions(void);
789 static void decl(int l
);
790 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
791 int first
, int size_only
);
792 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
793 int has_init
, int v
, int scope
);
795 void gv2(int rc1
, int rc2
);
796 void move_reg(int r
, int s
);
797 void save_regs(int n
);
798 void save_reg(int r
);
803 int get_reg_ex(int rc
,int rc2
);
806 struct macro_level
*prev
;
810 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
811 const int *macro_str
, struct macro_level
**can_read_stream
);
813 void force_charshort_cast(int t
);
814 static void gen_cast(CType
*type
);
816 static Sym
*sym_find(int v
);
817 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
820 static int type_size(CType
*type
, int *a
);
821 static inline CType
*pointed_type(CType
*type
);
822 static int pointed_size(CType
*type
);
823 static int lvalue_type(int t
);
824 static int parse_btype(CType
*type
, AttributeDef
*ad
);
825 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
826 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
);
827 static int is_compatible_types(CType
*type1
, CType
*type2
);
828 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
830 int ieee_finite(double d
);
831 void error(const char *fmt
, ...);
835 void lexpand_nr(void);
836 static void vpush_global_sym(CType
*type
, int v
);
837 void vset(CType
*type
, int r
, int v
);
838 void type_to_str(char *buf
, int buf_size
,
839 CType
*type
, const char *varstr
);
840 char *get_tok_str(int v
, CValue
*cv
);
841 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
842 unsigned long offset
, unsigned long size
);
843 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
845 /* section generation */
846 static void section_realloc(Section
*sec
, unsigned long new_size
);
847 static void *section_ptr_add(Section
*sec
, unsigned long size
);
848 static void put_extern_sym(Sym
*sym
, Section
*section
,
849 unsigned long value
, unsigned long size
);
850 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
851 static int put_elf_str(Section
*s
, const char *sym
);
852 static int put_elf_sym(Section
*s
,
853 unsigned long value
, unsigned long size
,
854 int info
, int other
, int shndx
, const char *name
);
855 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
856 int info
, int other
, int sh_num
, const char *name
);
857 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
858 int type
, int symbol
);
859 static void put_stabs(const char *str
, int type
, int other
, int desc
,
860 unsigned long value
);
861 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
862 unsigned long value
, Section
*sec
, int sym_index
);
863 static void put_stabn(int type
, int other
, int desc
, int value
);
864 static void put_stabd(int type
, int other
, int desc
);
865 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
867 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
868 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
869 #define AFF_PREPROCESS 0x0004 /* preprocess file */
870 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
873 int tcc_output_coff(TCCState
*s1
, FILE *f
);
876 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
);
877 int pe_load_def_file(struct TCCState
*s1
, int fd
);
878 int pe_test_res_file(void *v
, int size
);
879 int pe_load_res_file(struct TCCState
*s1
, int fd
);
880 void pe_add_runtime(struct TCCState
*s1
);
881 void pe_guess_outfile(char *objfilename
, int output_type
);
882 int pe_output_file(struct TCCState
*s1
, const char *filename
);
886 #ifdef CONFIG_TCC_ASM
888 typedef struct ExprValue
{
893 #define MAX_ASM_OPERANDS 30
895 typedef struct ASMOperand
{
896 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
898 char asm_str
[16]; /* computed asm string for operand */
899 SValue
*vt
; /* C value of the expression */
900 int ref_index
; /* if >= 0, gives reference to a output constraint */
901 int input_index
; /* if >= 0, gives reference to an input constraint */
902 int priority
; /* priority, used to assign registers */
903 int reg
; /* if >= 0, register number used for this operand */
904 int is_llong
; /* true if double register value */
905 int is_memory
; /* true if memory operand */
906 int is_rw
; /* for '+' modifier */
909 static void asm_expr(TCCState
*s1
, ExprValue
*pe
);
910 static int asm_int_expr(TCCState
*s1
);
911 static int find_constraint(ASMOperand
*operands
, int nb_operands
,
912 const char *name
, const char **pp
);
914 static int tcc_assemble(TCCState
*s1
, int do_preprocess
);
918 static void asm_instr(void);
919 static void asm_global_instr(void);
921 /* true if float/double/long double type */
922 static inline int is_float(int t
)
926 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
929 #ifdef TCC_TARGET_I386
930 #include "i386-gen.c"
933 #ifdef TCC_TARGET_ARM
937 #ifdef TCC_TARGET_C67
941 #ifdef CONFIG_TCC_STATIC
943 #define RTLD_LAZY 0x001
944 #define RTLD_NOW 0x002
945 #define RTLD_GLOBAL 0x100
946 #define RTLD_DEFAULT NULL
948 /* dummy function for profiling */
949 void *dlopen(const char *filename
, int flag
)
954 const char *dlerror(void)
959 typedef struct TCCSyms
{
964 #define TCCSYM(a) { #a, &a, },
966 /* add the symbol you want here if no dynamic linking is done */
967 static TCCSyms tcc_syms
[] = {
968 #if !defined(CONFIG_TCCBOOT)
977 void *resolve_sym(TCCState
*s1
, const char *symbol
, int type
)
981 while (p
->str
!= NULL
) {
982 if (!strcmp(p
->str
, symbol
))
989 #elif !defined(_WIN32)
993 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
)
995 return dlsym(RTLD_DEFAULT
, sym
);
1000 /********************************************************/
1002 /* we use our own 'finite' function to avoid potential problems with
1003 non standard math libs */
1004 /* XXX: endianness dependent */
1005 int ieee_finite(double d
)
1008 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
1011 /* copy a string and truncate it. */
1012 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
1019 q_end
= buf
+ buf_size
- 1;
1031 /* strcat and truncate. */
1032 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
1037 pstrcpy(buf
+ len
, buf_size
- len
, s
);
1042 static int strstart(const char *str
, const char *val
, const char **ptr
)
1047 while (*q
!= '\0') {
1059 /* extract the basename of a file */
1060 static char *tcc_basename(const char *name
)
1062 char *p
= strchr(name
, 0);
1073 static char *tcc_fileextension (const char *name
)
1075 char *b
= tcc_basename(name
);
1076 char *e
= strrchr(b
, '.');
1077 return e
? e
: strchr(b
, 0);
1081 char *normalize_slashes(char *path
)
1084 for (p
= path
; *p
; ++p
)
1090 char *w32_tcc_lib_path(void)
1092 /* on win32, we suppose the lib and includes are at the location
1094 char path
[1024], *p
;
1095 GetModuleFileNameA(NULL
, path
, sizeof path
);
1096 p
= tcc_basename(normalize_slashes(strlwr(path
)));
1097 if (p
- 5 > path
&& 0 == strncmp(p
- 5, "/bin/", 5))
1102 return strdup(path
);
1106 void set_pages_executable(void *ptr
, unsigned long length
)
1109 unsigned long old_protect
;
1110 VirtualProtect(ptr
, length
, PAGE_EXECUTE_READWRITE
, &old_protect
);
1112 unsigned long start
, end
;
1113 start
= (unsigned long)ptr
& ~(PAGESIZE
- 1);
1114 end
= (unsigned long)ptr
+ length
;
1115 end
= (end
+ PAGESIZE
- 1) & ~(PAGESIZE
- 1);
1116 mprotect((void *)start
, end
- start
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1120 /* memory management */
1124 unsigned malloc_usable_size(void*);
1127 static inline void tcc_free(void *ptr
)
1130 mem_cur_size
-= malloc_usable_size(ptr
);
1135 static void *tcc_malloc(unsigned long size
)
1140 error("memory full");
1142 mem_cur_size
+= malloc_usable_size(ptr
);
1143 if (mem_cur_size
> mem_max_size
)
1144 mem_max_size
= mem_cur_size
;
1149 static void *tcc_mallocz(unsigned long size
)
1152 ptr
= tcc_malloc(size
);
1153 memset(ptr
, 0, size
);
1157 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
1161 mem_cur_size
-= malloc_usable_size(ptr
);
1163 ptr1
= realloc(ptr
, size
);
1165 /* NOTE: count not correct if alloc error, but not critical */
1166 mem_cur_size
+= malloc_usable_size(ptr1
);
1167 if (mem_cur_size
> mem_max_size
)
1168 mem_max_size
= mem_cur_size
;
1173 static char *tcc_strdup(const char *str
)
1176 ptr
= tcc_malloc(strlen(str
) + 1);
1181 #define free(p) use_tcc_free(p)
1182 #define malloc(s) use_tcc_malloc(s)
1183 #define realloc(p, s) use_tcc_realloc(p, s)
1185 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
1192 /* every power of two we double array size */
1193 if ((nb
& (nb
- 1)) == 0) {
1198 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
1200 error("memory full");
1207 static void dynarray_reset(void *pp
, int *n
)
1210 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
1213 tcc_free(*(void**)pp
);
1217 /* symbol allocator */
1218 static Sym
*__sym_malloc(void)
1220 Sym
*sym_pool
, *sym
, *last_sym
;
1223 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
1224 dynarray_add(&sym_pools
, &nb_sym_pools
, sym_pool
);
1226 last_sym
= sym_free_first
;
1228 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
1229 sym
->next
= last_sym
;
1233 sym_free_first
= last_sym
;
1237 static inline Sym
*sym_malloc(void)
1240 sym
= sym_free_first
;
1242 sym
= __sym_malloc();
1243 sym_free_first
= sym
->next
;
1247 static inline void sym_free(Sym
*sym
)
1249 sym
->next
= sym_free_first
;
1250 sym_free_first
= sym
;
1253 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
1257 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
1258 strcpy(sec
->name
, name
);
1259 sec
->sh_type
= sh_type
;
1260 sec
->sh_flags
= sh_flags
;
1267 sec
->sh_addralign
= 4;
1270 sec
->sh_addralign
= 1;
1273 sec
->sh_addralign
= 32; /* default conservative alignment */
1277 /* only add section if not private */
1278 if (!(sh_flags
& SHF_PRIVATE
)) {
1279 sec
->sh_num
= s1
->nb_sections
;
1280 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
1285 static void free_section(Section
*s
)
1287 if (s
->link
&& (s
->link
->sh_flags
& SHF_PRIVATE
))
1288 free_section(s
->link
);
1289 if (s
->hash
&& (s
->hash
->sh_flags
& SHF_PRIVATE
))
1290 s
->hash
->link
= NULL
, free_section(s
->hash
);
1295 /* realloc section and set its content to zero */
1296 static void section_realloc(Section
*sec
, unsigned long new_size
)
1299 unsigned char *data
;
1301 size
= sec
->data_allocated
;
1304 while (size
< new_size
)
1306 data
= tcc_realloc(sec
->data
, size
);
1308 error("memory full");
1309 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
1311 sec
->data_allocated
= size
;
1314 /* reserve at least 'size' bytes in section 'sec' from
1315 sec->data_offset. */
1316 static void *section_ptr_add(Section
*sec
, unsigned long size
)
1318 unsigned long offset
, offset1
;
1320 offset
= sec
->data_offset
;
1321 offset1
= offset
+ size
;
1322 if (offset1
> sec
->data_allocated
)
1323 section_realloc(sec
, offset1
);
1324 sec
->data_offset
= offset1
;
1325 return sec
->data
+ offset
;
1328 /* return a reference to a section, and create it if it does not
1330 Section
*find_section(TCCState
*s1
, const char *name
)
1334 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1335 sec
= s1
->sections
[i
];
1336 if (!strcmp(name
, sec
->name
))
1339 /* sections are created as PROGBITS */
1340 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
1343 #define SECTION_ABS ((void *)1)
1345 /* update sym->c so that it points to an external symbol in section
1346 'section' with value 'value' */
1347 static void put_extern_sym2(Sym
*sym
, Section
*section
,
1348 unsigned long value
, unsigned long size
,
1349 int can_add_underscore
)
1351 int sym_type
, sym_bind
, sh_num
, info
, other
, attr
;
1356 if (section
== NULL
)
1358 else if (section
== SECTION_ABS
)
1361 sh_num
= section
->sh_num
;
1365 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
1366 sym_type
= STT_FUNC
;
1367 #ifdef TCC_TARGET_PE
1369 attr
= sym
->type
.ref
->r
;
1370 if (FUNC_EXPORT(attr
))
1372 if (FUNC_CALL(attr
) == FUNC_STDCALL
)
1376 sym_type
= STT_OBJECT
;
1379 if (sym
->type
.t
& VT_STATIC
)
1380 sym_bind
= STB_LOCAL
;
1382 sym_bind
= STB_GLOBAL
;
1385 name
= get_tok_str(sym
->v
, NULL
);
1386 #ifdef CONFIG_TCC_BCHECK
1387 if (do_bounds_check
) {
1390 /* XXX: avoid doing that for statics ? */
1391 /* if bound checking is activated, we change some function
1392 names by adding the "__bound" prefix */
1395 /* XXX: we rely only on malloc hooks */
1408 strcpy(buf
, "__bound_");
1416 #ifdef TCC_TARGET_PE
1417 if ((other
& 2) && can_add_underscore
) {
1418 sprintf(buf1
, "_%s@%d", name
, FUNC_ARGS(attr
));
1422 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
1424 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
1427 info
= ELF32_ST_INFO(sym_bind
, sym_type
);
1428 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
1430 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
1431 esym
->st_value
= value
;
1432 esym
->st_size
= size
;
1433 esym
->st_shndx
= sh_num
;
1434 esym
->st_other
|= other
;
1438 static void put_extern_sym(Sym
*sym
, Section
*section
,
1439 unsigned long value
, unsigned long size
)
1441 put_extern_sym2(sym
, section
, value
, size
, 1);
1444 /* add a new relocation entry to symbol 'sym' in section 's' */
1445 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
1448 put_extern_sym(sym
, NULL
, 0, 0);
1449 /* now we can add ELF relocation info */
1450 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
1453 static inline int isid(int c
)
1455 return (c
>= 'a' && c
<= 'z') ||
1456 (c
>= 'A' && c
<= 'Z') ||
1460 static inline int isnum(int c
)
1462 return c
>= '0' && c
<= '9';
1465 static inline int isoct(int c
)
1467 return c
>= '0' && c
<= '7';
1470 static inline int toup(int c
)
1472 if (c
>= 'a' && c
<= 'z')
1473 return c
- 'a' + 'A';
1478 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1482 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1485 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1489 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1493 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1500 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1501 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1502 (*f
)->filename
, (*f
)->line_num
);
1503 if (file
->line_num
> 0) {
1504 strcat_printf(buf
, sizeof(buf
),
1505 "%s:%d: ", file
->filename
, file
->line_num
);
1507 strcat_printf(buf
, sizeof(buf
),
1508 "%s: ", file
->filename
);
1511 strcat_printf(buf
, sizeof(buf
),
1515 strcat_printf(buf
, sizeof(buf
), "warning: ");
1516 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1518 if (!s1
->error_func
) {
1519 /* default case: stderr */
1520 fprintf(stderr
, "%s\n", buf
);
1522 s1
->error_func(s1
->error_opaque
, buf
);
1524 if (!is_warning
|| s1
->warn_error
)
1529 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1530 void (*error_func
)(void *opaque
, const char *msg
))
1532 s
->error_opaque
= error_opaque
;
1533 s
->error_func
= error_func
;
1537 /* error without aborting current compilation */
1538 void error_noabort(const char *fmt
, ...)
1540 TCCState
*s1
= tcc_state
;
1544 error1(s1
, 0, fmt
, ap
);
1548 void error(const char *fmt
, ...)
1550 TCCState
*s1
= tcc_state
;
1554 error1(s1
, 0, fmt
, ap
);
1556 /* better than nothing: in some cases, we accept to handle errors */
1557 if (s1
->error_set_jmp_enabled
) {
1558 longjmp(s1
->error_jmp_buf
, 1);
1560 /* XXX: eliminate this someday */
1565 void expect(const char *msg
)
1567 error("%s expected", msg
);
1570 void warning(const char *fmt
, ...)
1572 TCCState
*s1
= tcc_state
;
1579 error1(s1
, 1, fmt
, ap
);
1586 error("'%c' expected", c
);
1590 static void test_lvalue(void)
1592 if (!(vtop
->r
& VT_LVAL
))
1596 /* allocate a new token */
1597 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1599 TokenSym
*ts
, **ptable
;
1602 if (tok_ident
>= SYM_FIRST_ANOM
)
1603 error("memory full");
1605 /* expand token table if needed */
1606 i
= tok_ident
- TOK_IDENT
;
1607 if ((i
% TOK_ALLOC_INCR
) == 0) {
1608 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1610 error("memory full");
1611 table_ident
= ptable
;
1614 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1615 table_ident
[i
] = ts
;
1616 ts
->tok
= tok_ident
++;
1617 ts
->sym_define
= NULL
;
1618 ts
->sym_label
= NULL
;
1619 ts
->sym_struct
= NULL
;
1620 ts
->sym_identifier
= NULL
;
1622 ts
->hash_next
= NULL
;
1623 memcpy(ts
->str
, str
, len
);
1624 ts
->str
[len
] = '\0';
1629 #define TOK_HASH_INIT 1
1630 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1632 /* find a token and add it if not found */
1633 static TokenSym
*tok_alloc(const char *str
, int len
)
1635 TokenSym
*ts
, **pts
;
1641 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1642 h
&= (TOK_HASH_SIZE
- 1);
1644 pts
= &hash_ident
[h
];
1649 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1651 pts
= &(ts
->hash_next
);
1653 return tok_alloc_new(pts
, str
, len
);
1656 /* CString handling */
1658 static void cstr_realloc(CString
*cstr
, int new_size
)
1663 size
= cstr
->size_allocated
;
1665 size
= 8; /* no need to allocate a too small first string */
1666 while (size
< new_size
)
1668 data
= tcc_realloc(cstr
->data_allocated
, size
);
1670 error("memory full");
1671 cstr
->data_allocated
= data
;
1672 cstr
->size_allocated
= size
;
1677 static inline void cstr_ccat(CString
*cstr
, int ch
)
1680 size
= cstr
->size
+ 1;
1681 if (size
> cstr
->size_allocated
)
1682 cstr_realloc(cstr
, size
);
1683 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1687 static void cstr_cat(CString
*cstr
, const char *str
)
1699 /* add a wide char */
1700 static void cstr_wccat(CString
*cstr
, int ch
)
1703 size
= cstr
->size
+ sizeof(nwchar_t
);
1704 if (size
> cstr
->size_allocated
)
1705 cstr_realloc(cstr
, size
);
1706 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
1710 static void cstr_new(CString
*cstr
)
1712 memset(cstr
, 0, sizeof(CString
));
1715 /* free string and reset it to NULL */
1716 static void cstr_free(CString
*cstr
)
1718 tcc_free(cstr
->data_allocated
);
1722 #define cstr_reset(cstr) cstr_free(cstr)
1724 /* XXX: unicode ? */
1725 static void add_char(CString
*cstr
, int c
)
1727 if (c
== '\'' || c
== '\"' || c
== '\\') {
1728 /* XXX: could be more precise if char or string */
1729 cstr_ccat(cstr
, '\\');
1731 if (c
>= 32 && c
<= 126) {
1734 cstr_ccat(cstr
, '\\');
1736 cstr_ccat(cstr
, 'n');
1738 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1739 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1740 cstr_ccat(cstr
, '0' + (c
& 7));
1745 /* XXX: buffer overflow */
1746 /* XXX: float tokens */
1747 char *get_tok_str(int v
, CValue
*cv
)
1749 static char buf
[STRING_MAX_SIZE
+ 1];
1750 static CString cstr_buf
;
1756 /* NOTE: to go faster, we give a fixed buffer for small strings */
1757 cstr_reset(&cstr_buf
);
1758 cstr_buf
.data
= buf
;
1759 cstr_buf
.size_allocated
= sizeof(buf
);
1765 /* XXX: not quite exact, but only useful for testing */
1766 sprintf(p
, "%u", cv
->ui
);
1770 /* XXX: not quite exact, but only useful for testing */
1771 sprintf(p
, "%Lu", cv
->ull
);
1774 cstr_ccat(&cstr_buf
, 'L');
1776 cstr_ccat(&cstr_buf
, '\'');
1777 add_char(&cstr_buf
, cv
->i
);
1778 cstr_ccat(&cstr_buf
, '\'');
1779 cstr_ccat(&cstr_buf
, '\0');
1783 len
= cstr
->size
- 1;
1785 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1786 cstr_ccat(&cstr_buf
, '\0');
1789 cstr_ccat(&cstr_buf
, 'L');
1792 cstr_ccat(&cstr_buf
, '\"');
1794 len
= cstr
->size
- 1;
1796 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1798 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
1800 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
1802 cstr_ccat(&cstr_buf
, '\"');
1803 cstr_ccat(&cstr_buf
, '\0');
1812 return strcpy(p
, "...");
1814 return strcpy(p
, "<<=");
1816 return strcpy(p
, ">>=");
1818 if (v
< TOK_IDENT
) {
1819 /* search in two bytes table */
1833 } else if (v
< tok_ident
) {
1834 return table_ident
[v
- TOK_IDENT
]->str
;
1835 } else if (v
>= SYM_FIRST_ANOM
) {
1836 /* special name for anonymous symbol */
1837 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1839 /* should never happen */
1844 return cstr_buf
.data
;
1847 /* push, without hashing */
1848 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
1862 /* find a symbol and return its associated structure. 's' is the top
1863 of the symbol stack */
1864 static Sym
*sym_find2(Sym
*s
, int v
)
1874 /* structure lookup */
1875 static inline Sym
*struct_find(int v
)
1878 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1880 return table_ident
[v
]->sym_struct
;
1883 /* find an identifier */
1884 static inline Sym
*sym_find(int v
)
1887 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1889 return table_ident
[v
]->sym_identifier
;
1892 /* push a given symbol on the symbol stack */
1893 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1902 s
= sym_push2(ps
, v
, type
->t
, c
);
1903 s
->type
.ref
= type
->ref
;
1905 /* don't record fields or anonymous symbols */
1907 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1908 /* record symbol in token array */
1909 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1911 ps
= &ts
->sym_struct
;
1913 ps
= &ts
->sym_identifier
;
1920 /* push a global identifier */
1921 static Sym
*global_identifier_push(int v
, int t
, int c
)
1924 s
= sym_push2(&global_stack
, v
, t
, c
);
1925 /* don't record anonymous symbol */
1926 if (v
< SYM_FIRST_ANOM
) {
1927 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1928 /* modify the top most local identifier, so that
1929 sym_identifier will point to 's' when popped */
1931 ps
= &(*ps
)->prev_tok
;
1938 /* pop symbols until top reaches 'b' */
1939 static void sym_pop(Sym
**ptop
, Sym
*b
)
1949 /* remove symbol in token array */
1951 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1952 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1954 ps
= &ts
->sym_struct
;
1956 ps
= &ts
->sym_identifier
;
1967 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1972 if (strcmp(filename
, "-") == 0)
1973 fd
= 0, filename
= "stdin";
1975 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1976 if ((verbose
== 2 && fd
>= 0) || verbose
== 3)
1977 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
1978 (s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
1981 bf
= tcc_malloc(sizeof(BufferedFile
));
1983 bf
->buf_ptr
= bf
->buffer
;
1984 bf
->buf_end
= bf
->buffer
;
1985 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
1986 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
1988 normalize_slashes(bf
->filename
);
1991 bf
->ifndef_macro
= 0;
1992 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
1993 // printf("opening '%s'\n", filename);
1997 void tcc_close(BufferedFile
*bf
)
1999 total_lines
+= bf
->line_num
;
2004 /* fill input buffer and peek next char */
2005 static int tcc_peekc_slow(BufferedFile
*bf
)
2008 /* only tries to read if really end of buffer */
2009 if (bf
->buf_ptr
>= bf
->buf_end
) {
2011 #if defined(PARSE_DEBUG)
2016 len
= read(bf
->fd
, bf
->buffer
, len
);
2023 bf
->buf_ptr
= bf
->buffer
;
2024 bf
->buf_end
= bf
->buffer
+ len
;
2025 *bf
->buf_end
= CH_EOB
;
2027 if (bf
->buf_ptr
< bf
->buf_end
) {
2028 return bf
->buf_ptr
[0];
2030 bf
->buf_ptr
= bf
->buf_end
;
2035 /* return the current character, handling end of block if necessary
2037 static int handle_eob(void)
2039 return tcc_peekc_slow(file
);
2042 /* read next char from current input file and handle end of input buffer */
2043 static inline void inp(void)
2045 ch
= *(++(file
->buf_ptr
));
2046 /* end of buffer/file handling */
2051 /* handle '\[\r]\n' */
2052 static int handle_stray_noerror(void)
2054 while (ch
== '\\') {
2059 } else if (ch
== '\r') {
2073 static void handle_stray(void)
2075 if (handle_stray_noerror())
2076 error("stray '\\' in program");
2079 /* skip the stray and handle the \\n case. Output an error if
2080 incorrect char after the stray */
2081 static int handle_stray1(uint8_t *p
)
2085 if (p
>= file
->buf_end
) {
2102 /* handle just the EOB case, but not stray */
2103 #define PEEKC_EOB(c, p)\
2114 /* handle the complicated stray case */
2115 #define PEEKC(c, p)\
2120 c = handle_stray1(p);\
2125 /* input with '\[\r]\n' handling. Note that this function cannot
2126 handle other characters after '\', so you cannot call it inside
2127 strings or comments */
2128 static void minp(void)
2136 /* single line C++ comments */
2137 static uint8_t *parse_line_comment(uint8_t *p
)
2145 if (c
== '\n' || c
== CH_EOF
) {
2147 } else if (c
== '\\') {
2156 } else if (c
== '\r') {
2174 static uint8_t *parse_comment(uint8_t *p
)
2180 /* fast skip loop */
2183 if (c
== '\n' || c
== '*' || c
== '\\')
2187 if (c
== '\n' || c
== '*' || c
== '\\')
2191 /* now we can handle all the cases */
2195 } else if (c
== '*') {
2201 } else if (c
== '/') {
2202 goto end_of_comment
;
2203 } else if (c
== '\\') {
2208 /* skip '\[\r]\n', otherwise just skip the stray */
2214 } else if (c
== '\r') {
2231 /* stray, eob or eof */
2236 error("unexpected end of file in comment");
2237 } else if (c
== '\\') {
2249 /* space exlcuding newline */
2250 static inline int is_space(int ch
)
2252 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
2255 static inline void skip_spaces(void)
2257 while (is_space(ch
))
2261 /* parse a string without interpreting escapes */
2262 static uint8_t *parse_pp_string(uint8_t *p
,
2263 int sep
, CString
*str
)
2271 } else if (c
== '\\') {
2276 unterminated_string
:
2277 /* XXX: indicate line number of start of string */
2278 error("missing terminating %c character", sep
);
2279 } else if (c
== '\\') {
2280 /* escape : just skip \[\r]\n */
2285 } else if (c
== '\r') {
2288 expect("'\n' after '\r'");
2291 } else if (c
== CH_EOF
) {
2292 goto unterminated_string
;
2295 cstr_ccat(str
, '\\');
2301 } else if (c
== '\n') {
2304 } else if (c
== '\r') {
2308 cstr_ccat(str
, '\r');
2324 /* skip block of text until #else, #elif or #endif. skip also pairs of
2326 void preprocess_skip(void)
2328 int a
, start_of_line
, c
, in_warn_or_error
;
2335 in_warn_or_error
= 0;
2356 } else if (c
== '\\') {
2357 ch
= file
->buf_ptr
[0];
2358 handle_stray_noerror();
2365 if (in_warn_or_error
)
2367 p
= parse_pp_string(p
, c
, NULL
);
2371 if (in_warn_or_error
)
2378 p
= parse_comment(p
);
2379 } else if (ch
== '/') {
2380 p
= parse_line_comment(p
);
2385 if (start_of_line
) {
2390 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
2392 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
2394 else if (tok
== TOK_ENDIF
)
2396 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
2397 in_warn_or_error
= 1;
2411 /* ParseState handling */
2413 /* XXX: currently, no include file info is stored. Thus, we cannot display
2414 accurate messages if the function or data definition spans multiple
2417 /* save current parse state in 's' */
2418 void save_parse_state(ParseState
*s
)
2420 s
->line_num
= file
->line_num
;
2421 s
->macro_ptr
= macro_ptr
;
2426 /* restore parse state from 's' */
2427 void restore_parse_state(ParseState
*s
)
2429 file
->line_num
= s
->line_num
;
2430 macro_ptr
= s
->macro_ptr
;
2435 /* return the number of additional 'ints' necessary to store the
2437 static inline int tok_ext_size(int t
)
2451 error("unsupported token");
2458 return LDOUBLE_SIZE
/ 4;
2464 /* token string handling */
2466 static inline void tok_str_new(TokenString
*s
)
2470 s
->allocated_len
= 0;
2471 s
->last_line_num
= -1;
2474 static void tok_str_free(int *str
)
2479 static int *tok_str_realloc(TokenString
*s
)
2483 if (s
->allocated_len
== 0) {
2486 len
= s
->allocated_len
* 2;
2488 str
= tcc_realloc(s
->str
, len
* sizeof(int));
2490 error("memory full");
2491 s
->allocated_len
= len
;
2496 static void tok_str_add(TokenString
*s
, int t
)
2502 if (len
>= s
->allocated_len
)
2503 str
= tok_str_realloc(s
);
2508 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2515 /* allocate space for worst case */
2516 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2517 str
= tok_str_realloc(s
);
2526 str
[len
++] = cv
->tab
[0];
2535 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
2536 while ((len
+ nb_words
) > s
->allocated_len
)
2537 str
= tok_str_realloc(s
);
2538 cstr
= (CString
*)(str
+ len
);
2540 cstr
->size
= cv
->cstr
->size
;
2541 cstr
->data_allocated
= NULL
;
2542 cstr
->size_allocated
= cstr
->size
;
2543 memcpy((char *)cstr
+ sizeof(CString
),
2544 cv
->cstr
->data
, cstr
->size
);
2551 #if LDOUBLE_SIZE == 8
2554 str
[len
++] = cv
->tab
[0];
2555 str
[len
++] = cv
->tab
[1];
2557 #if LDOUBLE_SIZE == 12
2559 str
[len
++] = cv
->tab
[0];
2560 str
[len
++] = cv
->tab
[1];
2561 str
[len
++] = cv
->tab
[2];
2562 #elif LDOUBLE_SIZE != 8
2563 #error add long double size support
2572 /* add the current parse token in token string 's' */
2573 static void tok_str_add_tok(TokenString
*s
)
2577 /* save line number info */
2578 if (file
->line_num
!= s
->last_line_num
) {
2579 s
->last_line_num
= file
->line_num
;
2580 cval
.i
= s
->last_line_num
;
2581 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2583 tok_str_add2(s
, tok
, &tokc
);
2586 #if LDOUBLE_SIZE == 12
2587 #define LDOUBLE_GET(p, cv) \
2591 #elif LDOUBLE_SIZE == 8
2592 #define LDOUBLE_GET(p, cv) \
2596 #error add long double size support
2600 /* get a token from an integer array and increment pointer
2601 accordingly. we code it as a macro to avoid pointer aliasing. */
2602 #define TOK_GET(t, p, cv) \
2617 cv.cstr = (CString *)p; \
2618 cv.cstr->data = (char *)p + sizeof(CString);\
2619 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2628 case TOK_CLDOUBLE: \
2629 LDOUBLE_GET(p, cv); \
2630 p += LDOUBLE_SIZE / 4; \
2637 /* defines handling */
2638 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2642 s
= sym_push2(&define_stack
, v
, macro_type
, (int)str
);
2643 s
->next
= first_arg
;
2644 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2647 /* undefined a define symbol. Its name is just set to zero */
2648 static void define_undef(Sym
*s
)
2652 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2653 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2657 static inline Sym
*define_find(int v
)
2660 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2662 return table_ident
[v
]->sym_define
;
2665 /* free define stack until top reaches 'b' */
2666 static void free_defines(Sym
*b
)
2674 /* do not free args or predefined defines */
2676 tok_str_free((int *)top
->c
);
2678 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2679 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2687 static Sym
*label_find(int v
)
2690 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2692 return table_ident
[v
]->sym_label
;
2695 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
2698 s
= sym_push2(ptop
, v
, 0, 0);
2700 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
2701 if (ptop
== &global_label_stack
) {
2702 /* modify the top most local identifier, so that
2703 sym_identifier will point to 's' when popped */
2705 ps
= &(*ps
)->prev_tok
;
2712 /* pop labels until element last is reached. Look if any labels are
2713 undefined. Define symbols if '&&label' was used. */
2714 static void label_pop(Sym
**ptop
, Sym
*slast
)
2717 for(s
= *ptop
; s
!= slast
; s
= s1
) {
2719 if (s
->r
== LABEL_DECLARED
) {
2720 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
2721 } else if (s
->r
== LABEL_FORWARD
) {
2722 error("label '%s' used but not defined",
2723 get_tok_str(s
->v
, NULL
));
2726 /* define corresponding symbol. A size of
2728 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
2732 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
2738 /* eval an expression for #if/#elif */
2739 static int expr_preprocess(void)
2745 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2746 next(); /* do macro subst */
2747 if (tok
== TOK_DEFINED
) {
2752 c
= define_find(tok
) != 0;
2757 } else if (tok
>= TOK_IDENT
) {
2758 /* if undefined macro */
2762 tok_str_add_tok(&str
);
2764 tok_str_add(&str
, -1); /* simulate end of file */
2765 tok_str_add(&str
, 0);
2766 /* now evaluate C constant expression */
2767 macro_ptr
= str
.str
;
2771 tok_str_free(str
.str
);
2775 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2776 static void tok_print(int *str
)
2782 TOK_GET(t
, str
, cval
);
2785 printf(" %s", get_tok_str(t
, &cval
));
2791 /* parse after #define */
2792 static void parse_define(void)
2794 Sym
*s
, *first
, **ps
;
2795 int v
, t
, varg
, is_vaargs
, c
;
2800 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2801 /* XXX: should check if same macro (ANSI) */
2804 /* '(' must be just after macro definition for MACRO_FUNC */
2805 c
= file
->buf_ptr
[0];
2807 c
= handle_stray1(file
->buf_ptr
);
2812 while (tok
!= ')') {
2816 if (varg
== TOK_DOTS
) {
2817 varg
= TOK___VA_ARGS__
;
2819 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2823 if (varg
< TOK_IDENT
)
2824 error("badly punctuated parameter list");
2825 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2836 /* EOF testing necessary for '-D' handling */
2837 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2838 tok_str_add2(&str
, tok
, &tokc
);
2841 tok_str_add(&str
, 0);
2843 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2846 define_push(v
, t
, str
.str
, first
);
2849 static inline int hash_cached_include(int type
, const char *filename
)
2851 const unsigned char *s
;
2855 h
= TOK_HASH_FUNC(h
, type
);
2858 h
= TOK_HASH_FUNC(h
, *s
);
2861 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
2865 /* XXX: use a token or a hash table to accelerate matching ? */
2866 static CachedInclude
*search_cached_include(TCCState
*s1
,
2867 int type
, const char *filename
)
2871 h
= hash_cached_include(type
, filename
);
2872 i
= s1
->cached_includes_hash
[h
];
2876 e
= s1
->cached_includes
[i
- 1];
2877 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2884 static inline void add_cached_include(TCCState
*s1
, int type
,
2885 const char *filename
, int ifndef_macro
)
2890 if (search_cached_include(s1
, type
, filename
))
2893 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2895 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2899 strcpy(e
->filename
, filename
);
2900 e
->ifndef_macro
= ifndef_macro
;
2901 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2902 /* add in hash table */
2903 h
= hash_cached_include(type
, filename
);
2904 e
->hash_next
= s1
->cached_includes_hash
[h
];
2905 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
2908 static void pragma_parse(TCCState
*s1
)
2913 if (tok
== TOK_pack
) {
2916 #pragma pack(1) // set
2917 #pragma pack() // reset to default
2918 #pragma pack(push,1) // push & set
2919 #pragma pack(pop) // restore previous
2923 if (tok
== TOK_ASM_pop
) {
2925 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
2927 error("out of pack stack");
2929 s1
->pack_stack_ptr
--;
2933 if (tok
== TOK_ASM_push
) {
2935 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
2937 s1
->pack_stack_ptr
++;
2940 if (tok
!= TOK_CINT
) {
2942 error("invalid pack pragma");
2945 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
2949 *s1
->pack_stack_ptr
= val
;
2955 /* is_bof is true if first non space token at beginning of file */
2956 static void preprocess(int is_bof
)
2958 TCCState
*s1
= tcc_state
;
2959 int size
, i
, c
, n
, saved_parse_flags
;
2966 saved_parse_flags
= parse_flags
;
2967 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
2968 PARSE_FLAG_LINEFEED
;
2978 s
= define_find(tok
);
2979 /* undefine symbol by putting an invalid name */
2984 case TOK_INCLUDE_NEXT
:
2985 ch
= file
->buf_ptr
[0];
2986 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2991 } else if (ch
== '\"') {
2996 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
2997 if ((q
- buf
) < sizeof(buf
) - 1)
3000 if (handle_stray_noerror() == 0)
3008 /* eat all spaces and comments after include */
3009 /* XXX: slightly incorrect */
3010 while (ch1
!= '\n' && ch1
!= CH_EOF
)
3014 /* computed #include : either we have only strings or
3015 we have anything enclosed in '<>' */
3018 if (tok
== TOK_STR
) {
3019 while (tok
!= TOK_LINEFEED
) {
3020 if (tok
!= TOK_STR
) {
3022 error("'#include' expects \"FILENAME\" or <FILENAME>");
3024 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
3030 while (tok
!= TOK_LINEFEED
) {
3031 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
3035 /* check syntax and remove '<>' */
3036 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
3037 goto include_syntax
;
3038 memmove(buf
, buf
+ 1, len
- 2);
3039 buf
[len
- 2] = '\0';
3044 e
= search_cached_include(s1
, c
, buf
);
3045 if (e
&& define_find(e
->ifndef_macro
)) {
3046 /* no need to parse the include because the 'ifndef macro'
3049 printf("%s: skipping %s\n", file
->filename
, buf
);
3052 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
3053 error("#include recursion too deep");
3054 /* push current file in stack */
3055 /* XXX: fix current line init */
3056 *s1
->include_stack_ptr
++ = file
;
3058 /* first search in current dir if "header.h" */
3059 size
= tcc_basename(file
->filename
) - file
->filename
;
3060 if (size
> sizeof(buf1
) - 1)
3061 size
= sizeof(buf1
) - 1;
3062 memcpy(buf1
, file
->filename
, size
);
3064 pstrcat(buf1
, sizeof(buf1
), buf
);
3065 f
= tcc_open(s1
, buf1
);
3067 if (tok
== TOK_INCLUDE_NEXT
)
3073 /* now search in all the include paths */
3074 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
3075 for(i
= 0; i
< n
; i
++) {
3077 if (i
< s1
->nb_include_paths
)
3078 path
= s1
->include_paths
[i
];
3080 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
3081 pstrcpy(buf1
, sizeof(buf1
), path
);
3082 pstrcat(buf1
, sizeof(buf1
), "/");
3083 pstrcat(buf1
, sizeof(buf1
), buf
);
3084 f
= tcc_open(s1
, buf1
);
3086 if (tok
== TOK_INCLUDE_NEXT
)
3092 --s1
->include_stack_ptr
;
3093 error("include file '%s' not found", buf
);
3097 printf("%s: including %s\n", file
->filename
, buf1
);
3100 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
3102 /* add include file debug info */
3104 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
3106 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
3107 ch
= file
->buf_ptr
[0];
3115 c
= expr_preprocess();
3121 if (tok
< TOK_IDENT
)
3122 error("invalid argument for '#if%sdef'", c
? "n" : "");
3126 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
3128 file
->ifndef_macro
= tok
;
3131 c
= (define_find(tok
) != 0) ^ c
;
3133 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
3134 error("memory full");
3135 *s1
->ifdef_stack_ptr
++ = c
;
3138 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3139 error("#else without matching #if");
3140 if (s1
->ifdef_stack_ptr
[-1] & 2)
3141 error("#else after #else");
3142 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
3145 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3146 error("#elif without matching #if");
3147 c
= s1
->ifdef_stack_ptr
[-1];
3149 error("#elif after #else");
3150 /* last #if/#elif expression was true: we skip */
3153 c
= expr_preprocess();
3154 s1
->ifdef_stack_ptr
[-1] = c
;
3164 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
3165 error("#endif without matching #if");
3166 s1
->ifdef_stack_ptr
--;
3167 /* '#ifndef macro' was at the start of file. Now we check if
3168 an '#endif' is exactly at the end of file */
3169 if (file
->ifndef_macro
&&
3170 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
3171 file
->ifndef_macro_saved
= file
->ifndef_macro
;
3172 /* need to set to zero to avoid false matches if another
3173 #ifndef at middle of file */
3174 file
->ifndef_macro
= 0;
3175 while (tok
!= TOK_LINEFEED
)
3177 tok_flags
|= TOK_FLAG_ENDIF
;
3183 if (tok
!= TOK_CINT
)
3185 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
3187 if (tok
!= TOK_LINEFEED
) {
3190 pstrcpy(file
->filename
, sizeof(file
->filename
),
3191 (char *)tokc
.cstr
->data
);
3197 ch
= file
->buf_ptr
[0];
3200 while (ch
!= '\n' && ch
!= CH_EOF
) {
3201 if ((q
- buf
) < sizeof(buf
) - 1)
3204 if (handle_stray_noerror() == 0)
3211 error("#error %s", buf
);
3213 warning("#warning %s", buf
);
3219 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
3220 /* '!' is ignored to allow C scripts. numbers are ignored
3221 to emulate cpp behaviour */
3223 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
3224 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
3228 /* ignore other preprocess commands or #! for C scripts */
3229 while (tok
!= TOK_LINEFEED
)
3232 parse_flags
= saved_parse_flags
;
3235 /* evaluate escape codes in a string. */
3236 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
3251 case '0': case '1': case '2': case '3':
3252 case '4': case '5': case '6': case '7':
3253 /* at most three octal digits */
3258 n
= n
* 8 + c
- '0';
3262 n
= n
* 8 + c
- '0';
3267 goto add_char_nonext
;
3275 if (c
>= 'a' && c
<= 'f')
3277 else if (c
>= 'A' && c
<= 'F')
3287 goto add_char_nonext
;
3311 goto invalid_escape
;
3321 if (c
>= '!' && c
<= '~')
3322 warning("unknown escape sequence: \'\\%c\'", c
);
3324 warning("unknown escape sequence: \'\\x%x\'", c
);
3331 cstr_ccat(outstr
, c
);
3333 cstr_wccat(outstr
, c
);
3335 /* add a trailing '\0' */
3337 cstr_ccat(outstr
, '\0');
3339 cstr_wccat(outstr
, '\0');
3342 /* we use 64 bit numbers */
3345 /* bn = (bn << shift) | or_val */
3346 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
3350 for(i
=0;i
<BN_SIZE
;i
++) {
3352 bn
[i
] = (v
<< shift
) | or_val
;
3353 or_val
= v
>> (32 - shift
);
3357 void bn_zero(unsigned int *bn
)
3360 for(i
=0;i
<BN_SIZE
;i
++) {
3365 /* parse number in null terminated string 'p' and return it in the
3367 void parse_number(const char *p
)
3369 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
3371 unsigned int bn
[BN_SIZE
];
3382 goto float_frac_parse
;
3383 } else if (t
== '0') {
3384 if (ch
== 'x' || ch
== 'X') {
3388 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
3394 /* parse all digits. cannot check octal numbers at this stage
3395 because of floating point constants */
3397 if (ch
>= 'a' && ch
<= 'f')
3399 else if (ch
>= 'A' && ch
<= 'F')
3407 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
3409 error("number too long");
3415 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
3416 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
3418 /* NOTE: strtox should support that for hexa numbers, but
3419 non ISOC99 libcs do not support it, so we prefer to do
3421 /* hexadecimal or binary floats */
3422 /* XXX: handle overflows */
3434 } else if (t
>= 'a') {
3436 } else if (t
>= 'A') {
3441 bn_lshift(bn
, shift
, t
);
3448 if (t
>= 'a' && t
<= 'f') {
3450 } else if (t
>= 'A' && t
<= 'F') {
3452 } else if (t
>= '0' && t
<= '9') {
3458 error("invalid digit");
3459 bn_lshift(bn
, shift
, t
);
3464 if (ch
!= 'p' && ch
!= 'P')
3471 } else if (ch
== '-') {
3475 if (ch
< '0' || ch
> '9')
3476 expect("exponent digits");
3477 while (ch
>= '0' && ch
<= '9') {
3478 exp_val
= exp_val
* 10 + ch
- '0';
3481 exp_val
= exp_val
* s
;
3483 /* now we can generate the number */
3484 /* XXX: should patch directly float number */
3485 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
3486 d
= ldexp(d
, exp_val
- frac_bits
);
3491 /* float : should handle overflow */
3493 } else if (t
== 'L') {
3496 /* XXX: not large enough */
3497 tokc
.ld
= (long double)d
;
3503 /* decimal floats */
3505 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3510 while (ch
>= '0' && ch
<= '9') {
3511 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3517 if (ch
== 'e' || ch
== 'E') {
3518 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3522 if (ch
== '-' || ch
== '+') {
3523 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3528 if (ch
< '0' || ch
> '9')
3529 expect("exponent digits");
3530 while (ch
>= '0' && ch
<= '9') {
3531 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3543 tokc
.f
= strtof(token_buf
, NULL
);
3544 } else if (t
== 'L') {
3547 tokc
.ld
= strtold(token_buf
, NULL
);
3550 tokc
.d
= strtod(token_buf
, NULL
);
3554 unsigned long long n
, n1
;
3557 /* integer number */
3560 if (b
== 10 && *q
== '0') {
3567 /* no need for checks except for base 10 / 8 errors */
3570 } else if (t
>= 'a') {
3572 } else if (t
>= 'A') {
3577 error("invalid digit");
3581 /* detect overflow */
3582 /* XXX: this test is not reliable */
3584 error("integer constant overflow");
3587 /* XXX: not exactly ANSI compliant */
3588 if ((n
& 0xffffffff00000000LL
) != 0) {
3593 } else if (n
> 0x7fffffff) {
3604 error("three 'l's in integer constant");
3607 if (tok
== TOK_CINT
)
3609 else if (tok
== TOK_CUINT
)
3613 } else if (t
== 'U') {
3615 error("two 'u's in integer constant");
3617 if (tok
== TOK_CINT
)
3619 else if (tok
== TOK_CLLONG
)
3626 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
3634 #define PARSE2(c1, tok1, c2, tok2) \
3645 /* return next token without macro substitution */
3646 static inline void next_nomacro1(void)
3666 /* first look if it is in fact an end of buffer */
3667 if (p
>= file
->buf_end
) {
3671 if (p
>= file
->buf_end
)
3684 TCCState
*s1
= tcc_state
;
3685 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
3686 && !(tok_flags
& TOK_FLAG_EOF
)) {
3687 tok_flags
|= TOK_FLAG_EOF
;
3689 goto keep_tok_flags
;
3690 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
3691 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3692 /* no include left : end of file. */
3695 tok_flags
&= ~TOK_FLAG_EOF
;
3696 /* pop include file */
3698 /* test if previous '#endif' was after a #ifdef at
3700 if (tok_flags
& TOK_FLAG_ENDIF
) {
3702 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3704 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3705 file
->ifndef_macro_saved
);
3708 /* add end of include file debug info */
3710 put_stabd(N_EINCL
, 0, 0);
3712 /* pop include stack */
3714 s1
->include_stack_ptr
--;
3715 file
= *s1
->include_stack_ptr
;
3724 tok_flags
|= TOK_FLAG_BOL
;
3726 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
3729 goto keep_tok_flags
;
3734 if ((tok_flags
& TOK_FLAG_BOL
) &&
3735 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3737 preprocess(tok_flags
& TOK_FLAG_BOF
);
3743 tok
= TOK_TWOSHARPS
;
3745 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
3746 p
= parse_line_comment(p
- 1);
3755 case 'a': case 'b': case 'c': case 'd':
3756 case 'e': case 'f': case 'g': case 'h':
3757 case 'i': case 'j': case 'k': case 'l':
3758 case 'm': case 'n': case 'o': case 'p':
3759 case 'q': case 'r': case 's': case 't':
3760 case 'u': case 'v': case 'w': case 'x':
3762 case 'A': case 'B': case 'C': case 'D':
3763 case 'E': case 'F': case 'G': case 'H':
3764 case 'I': case 'J': case 'K':
3765 case 'M': case 'N': case 'O': case 'P':
3766 case 'Q': case 'R': case 'S': case 'T':
3767 case 'U': case 'V': case 'W': case 'X':
3773 h
= TOK_HASH_FUNC(h
, c
);
3777 if (!isidnum_table
[c
-CH_EOF
])
3779 h
= TOK_HASH_FUNC(h
, c
);
3786 /* fast case : no stray found, so we have the full token
3787 and we have already hashed it */
3789 h
&= (TOK_HASH_SIZE
- 1);
3790 pts
= &hash_ident
[h
];
3795 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3797 pts
= &(ts
->hash_next
);
3799 ts
= tok_alloc_new(pts
, p1
, len
);
3803 cstr_reset(&tokcstr
);
3806 cstr_ccat(&tokcstr
, *p1
);
3812 while (isidnum_table
[c
-CH_EOF
]) {
3813 cstr_ccat(&tokcstr
, c
);
3816 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3822 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3824 goto parse_ident_fast
;
3827 if (c
== '\'' || c
== '\"') {
3831 cstr_reset(&tokcstr
);
3832 cstr_ccat(&tokcstr
, 'L');
3833 goto parse_ident_slow
;
3837 case '0': case '1': case '2': case '3':
3838 case '4': case '5': case '6': case '7':
3841 cstr_reset(&tokcstr
);
3842 /* after the first digit, accept digits, alpha, '.' or sign if
3843 prefixed by 'eEpP' */
3847 cstr_ccat(&tokcstr
, c
);
3849 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3850 ((c
== '+' || c
== '-') &&
3851 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3854 /* We add a trailing '\0' to ease parsing */
3855 cstr_ccat(&tokcstr
, '\0');
3856 tokc
.cstr
= &tokcstr
;
3860 /* special dot handling because it can also start a number */
3863 cstr_reset(&tokcstr
);
3864 cstr_ccat(&tokcstr
, '.');
3866 } else if (c
== '.') {
3886 /* parse the string */
3888 p
= parse_pp_string(p
, sep
, &str
);
3889 cstr_ccat(&str
, '\0');
3891 /* eval the escape (should be done as TOK_PPNUM) */
3892 cstr_reset(&tokcstr
);
3893 parse_escape_string(&tokcstr
, str
.data
, is_long
);
3898 /* XXX: make it portable */
3902 char_size
= sizeof(nwchar_t
);
3903 if (tokcstr
.size
<= char_size
)
3904 error("empty character constant");
3905 if (tokcstr
.size
> 2 * char_size
)
3906 warning("multi-character character constant");
3908 tokc
.i
= *(int8_t *)tokcstr
.data
;
3911 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
3915 tokc
.cstr
= &tokcstr
;
3929 } else if (c
== '<') {
3947 } else if (c
== '>') {
3965 } else if (c
== '=') {
3978 } else if (c
== '=') {
3991 } else if (c
== '=') {
4004 } else if (c
== '=') {
4007 } else if (c
== '>') {
4015 PARSE2('!', '!', '=', TOK_NE
)
4016 PARSE2('=', '=', '=', TOK_EQ
)
4017 PARSE2('*', '*', '=', TOK_A_MUL
)
4018 PARSE2('%', '%', '=', TOK_A_MOD
)
4019 PARSE2('^', '^', '=', TOK_A_XOR
)
4021 /* comments or operator */
4025 p
= parse_comment(p
);
4027 } else if (c
== '/') {
4028 p
= parse_line_comment(p
);
4030 } else if (c
== '=') {
4050 case '$': /* only used in assembler */
4051 case '@': /* dito */
4056 error("unrecognized character \\x%02x", c
);
4062 #if defined(PARSE_DEBUG)
4063 printf("token = %s\n", get_tok_str(tok
, &tokc
));
4067 /* return next token without macro substitution. Can read input from
4069 static void next_nomacro(void)
4075 TOK_GET(tok
, macro_ptr
, tokc
);
4076 if (tok
== TOK_LINENUM
) {
4077 file
->line_num
= tokc
.i
;
4086 /* substitute args in macro_str and return allocated string */
4087 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
4089 int *st
, last_tok
, t
, notfirst
;
4098 TOK_GET(t
, macro_str
, cval
);
4103 TOK_GET(t
, macro_str
, cval
);
4106 s
= sym_find2(args
, t
);
4113 cstr_ccat(&cstr
, ' ');
4114 TOK_GET(t
, st
, cval
);
4115 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
4120 cstr_ccat(&cstr
, '\0');
4122 printf("stringize: %s\n", (char *)cstr
.data
);
4126 tok_str_add2(&str
, TOK_STR
, &cval
);
4129 tok_str_add2(&str
, t
, &cval
);
4131 } else if (t
>= TOK_IDENT
) {
4132 s
= sym_find2(args
, t
);
4135 /* if '##' is present before or after, no arg substitution */
4136 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
4137 /* special case for var arg macros : ## eats the
4138 ',' if empty VA_ARGS variable. */
4139 /* XXX: test of the ',' is not 100%
4140 reliable. should fix it to avoid security
4142 if (gnu_ext
&& s
->type
.t
&&
4143 last_tok
== TOK_TWOSHARPS
&&
4144 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
4146 /* suppress ',' '##' */
4149 /* suppress '##' and add variable */
4157 TOK_GET(t1
, st
, cval
);
4160 tok_str_add2(&str
, t1
, &cval
);
4164 /* NOTE: the stream cannot be read when macro
4165 substituing an argument */
4166 macro_subst(&str
, nested_list
, st
, NULL
);
4169 tok_str_add(&str
, t
);
4172 tok_str_add2(&str
, t
, &cval
);
4176 tok_str_add(&str
, 0);
4180 static char const ab_month_name
[12][4] =
4182 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4183 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4186 /* do macro substitution of current token with macro 's' and add
4187 result to (tok_str,tok_len). 'nested_list' is the list of all
4188 macros we got inside to avoid recursing. Return non zero if no
4189 substitution needs to be done */
4190 static int macro_subst_tok(TokenString
*tok_str
,
4191 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
4193 Sym
*args
, *sa
, *sa1
;
4194 int mstr_allocated
, parlevel
, *mstr
, t
, t1
;
4201 /* if symbol is a macro, prepare substitution */
4202 /* special macros */
4203 if (tok
== TOK___LINE__
) {
4204 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
4208 } else if (tok
== TOK___FILE__
) {
4209 cstrval
= file
->filename
;
4211 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
4216 tm
= localtime(&ti
);
4217 if (tok
== TOK___DATE__
) {
4218 snprintf(buf
, sizeof(buf
), "%s %2d %d",
4219 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
4221 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
4222 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
4229 cstr_cat(&cstr
, cstrval
);
4230 cstr_ccat(&cstr
, '\0');
4232 tok_str_add2(tok_str
, t1
, &cval
);
4237 if (s
->type
.t
== MACRO_FUNC
) {
4238 /* NOTE: we do not use next_nomacro to avoid eating the
4239 next token. XXX: find better solution */
4243 if (t
== 0 && can_read_stream
) {
4244 /* end of macro stream: we must look at the token
4245 after in the file */
4246 struct macro_level
*ml
= *can_read_stream
;
4252 *can_read_stream
= ml
-> prev
;
4257 /* XXX: incorrect with comments */
4258 ch
= file
->buf_ptr
[0];
4259 while (is_space(ch
) || ch
== '\n')
4263 if (t
!= '(') /* no macro subst */
4266 /* argument macro */
4271 /* NOTE: empty args are allowed, except if no args */
4273 /* handle '()' case */
4274 if (!args
&& !sa
&& tok
== ')')
4277 error("macro '%s' used with too many args",
4278 get_tok_str(s
->v
, 0));
4281 /* NOTE: non zero sa->t indicates VA_ARGS */
4282 while ((parlevel
> 0 ||
4284 (tok
!= ',' || sa
->type
.t
))) &&
4288 else if (tok
== ')')
4290 if (tok
!= TOK_LINEFEED
)
4291 tok_str_add2(&str
, tok
, &tokc
);
4294 tok_str_add(&str
, 0);
4295 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (int)str
.str
);
4298 /* special case for gcc var args: add an empty
4299 var arg argument if it is omitted */
4300 if (sa
&& sa
->type
.t
&& gnu_ext
)
4310 error("macro '%s' used with too few args",
4311 get_tok_str(s
->v
, 0));
4314 /* now subst each arg */
4315 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
4320 tok_str_free((int *)sa
->c
);
4326 sym_push2(nested_list
, s
->v
, 0, 0);
4327 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
4328 /* pop nested defined symbol */
4330 *nested_list
= sa1
->prev
;
4338 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4339 return the resulting string (which must be freed). */
4340 static inline int *macro_twosharps(const int *macro_str
)
4343 const int *macro_ptr1
, *start_macro_ptr
, *ptr
, *saved_macro_ptr
;
4345 const char *p1
, *p2
;
4347 TokenString macro_str1
;
4350 start_macro_ptr
= macro_str
;
4351 /* we search the first '##' */
4353 macro_ptr1
= macro_str
;
4354 TOK_GET(t
, macro_str
, cval
);
4355 /* nothing more to do if end of string */
4358 if (*macro_str
== TOK_TWOSHARPS
)
4362 /* we saw '##', so we need more processing to handle it */
4364 tok_str_new(¯o_str1
);
4368 /* add all tokens seen so far */
4369 for(ptr
= start_macro_ptr
; ptr
< macro_ptr1
;) {
4370 TOK_GET(t
, ptr
, cval
);
4371 tok_str_add2(¯o_str1
, t
, &cval
);
4373 saved_macro_ptr
= macro_ptr
;
4374 /* XXX: get rid of the use of macro_ptr here */
4375 macro_ptr
= (int *)macro_str
;
4377 while (*macro_ptr
== TOK_TWOSHARPS
) {
4379 macro_ptr1
= macro_ptr
;
4382 TOK_GET(t
, macro_ptr
, cval
);
4383 /* We concatenate the two tokens if we have an
4384 identifier or a preprocessing number */
4386 p1
= get_tok_str(tok
, &tokc
);
4387 cstr_cat(&cstr
, p1
);
4388 p2
= get_tok_str(t
, &cval
);
4389 cstr_cat(&cstr
, p2
);
4390 cstr_ccat(&cstr
, '\0');
4392 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
4393 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
4394 if (tok
== TOK_PPNUM
) {
4395 /* if number, then create a number token */
4396 /* NOTE: no need to allocate because
4397 tok_str_add2() does it */
4398 cstr_reset(&tokcstr
);
4401 tokc
.cstr
= &tokcstr
;
4403 /* if identifier, we must do a test to
4404 validate we have a correct identifier */
4405 if (t
== TOK_PPNUM
) {
4415 if (!isnum(c
) && !isid(c
))
4419 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
4420 tok
= ts
->tok
; /* modify current token */
4423 const char *str
= cstr
.data
;
4424 const unsigned char *q
;
4426 /* we look for a valid token */
4427 /* XXX: do more extensive checks */
4428 if (!strcmp(str
, ">>=")) {
4430 } else if (!strcmp(str
, "<<=")) {
4432 } else if (strlen(str
) == 2) {
4433 /* search in two bytes table */
4438 if (q
[0] == str
[0] && q
[1] == str
[1])
4445 /* NOTE: because get_tok_str use a static buffer,
4448 p1
= get_tok_str(tok
, &tokc
);
4449 cstr_cat(&cstr
, p1
);
4450 cstr_ccat(&cstr
, '\0');
4451 p2
= get_tok_str(t
, &cval
);
4452 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
4453 /* cannot merge tokens: just add them separately */
4454 tok_str_add2(¯o_str1
, tok
, &tokc
);
4455 /* XXX: free associated memory ? */
4462 tok_str_add2(¯o_str1
, tok
, &tokc
);
4467 macro_ptr
= (int *)saved_macro_ptr
;
4469 tok_str_add(¯o_str1
, 0);
4470 return macro_str1
.str
;
4474 /* do macro substitution of macro_str and add result to
4475 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4476 inside to avoid recursing. */
4477 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
4478 const int *macro_str
, struct macro_level
** can_read_stream
)
4485 struct macro_level ml
;
4487 /* first scan for '##' operator handling */
4489 macro_str1
= macro_twosharps(ptr
);
4493 /* NOTE: ptr == NULL can only happen if tokens are read from
4494 file stream due to a macro function call */
4497 TOK_GET(t
, ptr
, cval
);
4502 /* if nested substitution, do nothing */
4503 if (sym_find2(*nested_list
, t
))
4506 if (can_read_stream
)
4507 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
4508 macro_ptr
= (int *)ptr
;
4510 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
4511 ptr
= (int *)macro_ptr
;
4513 if (can_read_stream
&& *can_read_stream
== &ml
)
4514 *can_read_stream
= ml
.prev
;
4519 tok_str_add2(tok_str
, t
, &cval
);
4523 tok_str_free(macro_str1
);
4526 /* return next token with macro substitution */
4527 static void next(void)
4529 Sym
*nested_list
, *s
;
4531 struct macro_level
*ml
;
4536 /* if not reading from macro substituted string, then try
4537 to substitute macros */
4538 if (tok
>= TOK_IDENT
&&
4539 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
4540 s
= define_find(tok
);
4542 /* we have a macro: we try to substitute */
4546 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
4547 /* substitution done, NOTE: maybe empty */
4548 tok_str_add(&str
, 0);
4549 macro_ptr
= str
.str
;
4550 macro_ptr_allocated
= str
.str
;
4557 /* end of macro or end of unget buffer */
4558 if (unget_buffer_enabled
) {
4559 macro_ptr
= unget_saved_macro_ptr
;
4560 unget_buffer_enabled
= 0;
4562 /* end of macro string: free it */
4563 tok_str_free(macro_ptr_allocated
);
4570 /* convert preprocessor tokens into C tokens */
4571 if (tok
== TOK_PPNUM
&&
4572 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
4573 parse_number((char *)tokc
.cstr
->data
);
4577 /* push back current token and set current token to 'last_tok'. Only
4578 identifier case handled for labels. */
4579 static inline void unget_tok(int last_tok
)
4583 unget_saved_macro_ptr
= macro_ptr
;
4584 unget_buffer_enabled
= 1;
4585 q
= unget_saved_buffer
;
4588 n
= tok_ext_size(tok
) - 1;
4591 *q
= 0; /* end of token string */
4596 void swap(int *p
, int *q
)
4604 void vsetc(CType
*type
, int r
, CValue
*vc
)
4608 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4609 error("memory full");
4610 /* cannot let cpu flags if other instruction are generated. Also
4611 avoid leaving VT_JMP anywhere except on the top of the stack
4612 because it would complicate the code generator. */
4613 if (vtop
>= vstack
) {
4614 v
= vtop
->r
& VT_VALMASK
;
4615 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
4621 vtop
->r2
= VT_CONST
;
4625 /* push integer constant */
4630 vsetc(&int_type
, VT_CONST
, &cval
);
4633 /* Return a static symbol pointing to a section */
4634 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
4635 unsigned long offset
, unsigned long size
)
4641 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
4642 sym
->type
.ref
= type
->ref
;
4643 sym
->r
= VT_CONST
| VT_SYM
;
4644 put_extern_sym(sym
, sec
, offset
, size
);
4648 /* push a reference to a section offset by adding a dummy symbol */
4649 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
4654 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4655 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
4658 /* define a new external reference to a symbol 'v' of type 'u' */
4659 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
4665 /* push forward reference */
4666 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
4667 s
->type
.ref
= type
->ref
;
4668 s
->r
= r
| VT_CONST
| VT_SYM
;
4673 /* define a new external reference to a symbol 'v' of type 'u' */
4674 static Sym
*external_sym(int v
, CType
*type
, int r
)
4680 /* push forward reference */
4681 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
4682 s
->type
.t
|= VT_EXTERN
;
4684 if (!is_compatible_types(&s
->type
, type
))
4685 error("incompatible types for redefinition of '%s'",
4686 get_tok_str(v
, NULL
));
4691 /* push a reference to global symbol v */
4692 static void vpush_global_sym(CType
*type
, int v
)
4697 sym
= external_global_sym(v
, type
, 0);
4699 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4703 void vset(CType
*type
, int r
, int v
)
4708 vsetc(type
, r
, &cval
);
4711 void vseti(int r
, int v
)
4727 void vpushv(SValue
*v
)
4729 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4730 error("memory full");
4740 /* save r to the memory stack, and mark it as being free */
4741 void save_reg(int r
)
4743 int l
, saved
, size
, align
;
4747 /* modify all stack values */
4750 for(p
=vstack
;p
<=vtop
;p
++) {
4751 if ((p
->r
& VT_VALMASK
) == r
||
4752 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
4753 /* must save value on stack if not already done */
4755 /* NOTE: must reload 'r' because r might be equal to r2 */
4756 r
= p
->r
& VT_VALMASK
;
4757 /* store register in the stack */
4759 if ((p
->r
& VT_LVAL
) ||
4760 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
4762 size
= type_size(type
, &align
);
4763 loc
= (loc
- size
) & -align
;
4764 sv
.type
.t
= type
->t
;
4765 sv
.r
= VT_LOCAL
| VT_LVAL
;
4768 #ifdef TCC_TARGET_I386
4769 /* x86 specific: need to pop fp register ST0 if saved */
4770 if (r
== TREG_ST0
) {
4771 o(0xd9dd); /* fstp %st(1) */
4774 /* special long long case */
4775 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
4782 /* mark that stack entry as being saved on the stack */
4783 if (p
->r
& VT_LVAL
) {
4784 /* also clear the bounded flag because the
4785 relocation address of the function was stored in
4787 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4789 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4797 /* find a register of class 'rc2' with at most one reference on stack.
4798 * If none, call get_reg(rc) */
4799 int get_reg_ex(int rc
, int rc2
)
4804 for(r
=0;r
<NB_REGS
;r
++) {
4805 if (reg_classes
[r
] & rc2
) {
4808 for(p
= vstack
; p
<= vtop
; p
++) {
4809 if ((p
->r
& VT_VALMASK
) == r
||
4810 (p
->r2
& VT_VALMASK
) == r
)
4820 /* find a free register of class 'rc'. If none, save one register */
4826 /* find a free register */
4827 for(r
=0;r
<NB_REGS
;r
++) {
4828 if (reg_classes
[r
] & rc
) {
4829 for(p
=vstack
;p
<=vtop
;p
++) {
4830 if ((p
->r
& VT_VALMASK
) == r
||
4831 (p
->r2
& VT_VALMASK
) == r
)
4839 /* no register left : free the first one on the stack (VERY
4840 IMPORTANT to start from the bottom to ensure that we don't
4841 spill registers used in gen_opi()) */
4842 for(p
=vstack
;p
<=vtop
;p
++) {
4843 r
= p
->r
& VT_VALMASK
;
4844 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4846 /* also look at second register (if long long) */
4847 r
= p
->r2
& VT_VALMASK
;
4848 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4854 /* Should never comes here */
4858 /* save registers up to (vtop - n) stack entry */
4859 void save_regs(int n
)
4864 for(p
= vstack
;p
<= p1
; p
++) {
4865 r
= p
->r
& VT_VALMASK
;
4872 /* move register 's' to 'r', and flush previous value of r to memory
4874 void move_reg(int r
, int s
)
4887 /* get address of vtop (vtop MUST BE an lvalue) */
4890 vtop
->r
&= ~VT_LVAL
;
4891 /* tricky: if saved lvalue, then we can go back to lvalue */
4892 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4893 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4896 #ifdef CONFIG_TCC_BCHECK
4897 /* generate lvalue bound code */
4903 vtop
->r
&= ~VT_MUSTBOUND
;
4904 /* if lvalue, then use checking code before dereferencing */
4905 if (vtop
->r
& VT_LVAL
) {
4906 /* if not VT_BOUNDED value, then make one */
4907 if (!(vtop
->r
& VT_BOUNDED
)) {
4908 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4909 /* must save type because we must set it to int to get pointer */
4911 vtop
->type
.t
= VT_INT
;
4914 gen_bounded_ptr_add();
4915 vtop
->r
|= lval_type
;
4918 /* then check for dereferencing */
4919 gen_bounded_ptr_deref();
4924 /* store vtop a register belonging to class 'rc'. lvalues are
4925 converted to values. Cannot be used if cannot be converted to
4926 register value (such as structures). */
4929 int r
, r2
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4930 unsigned long long ll
;
4932 /* NOTE: get_reg can modify vstack[] */
4933 if (vtop
->type
.t
& VT_BITFIELD
) {
4935 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4936 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4937 /* remove bit field info to avoid loops */
4938 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4939 /* cast to int to propagate signedness in following ops */
4941 if((vtop
->type
.t
& VT_UNSIGNED
) ||
4942 (vtop
->type
.t
& VT_BTYPE
) == VT_BOOL
)
4943 type
.t
|= VT_UNSIGNED
;
4945 /* generate shifts */
4946 vpushi(32 - (bit_pos
+ bit_size
));
4948 vpushi(32 - bit_size
);
4949 /* NOTE: transformed to SHR if unsigned */
4953 if (is_float(vtop
->type
.t
) &&
4954 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4957 unsigned long offset
;
4958 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4962 /* XXX: unify with initializers handling ? */
4963 /* CPUs usually cannot use float constants, so we store them
4964 generically in data segment */
4965 size
= type_size(&vtop
->type
, &align
);
4966 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
4967 data_section
->data_offset
= offset
;
4968 /* XXX: not portable yet */
4970 /* Zero pad x87 tenbyte long doubles */
4972 vtop
->c
.tab
[2] &= 0xffff;
4974 ptr
= section_ptr_add(data_section
, size
);
4976 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4980 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
4984 ptr
[i
] = vtop
->c
.tab
[i
];
4985 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
4986 vtop
->r
|= VT_LVAL
| VT_SYM
;
4990 #ifdef CONFIG_TCC_BCHECK
4991 if (vtop
->r
& VT_MUSTBOUND
)
4995 r
= vtop
->r
& VT_VALMASK
;
4999 /* need to reload if:
5001 - lvalue (need to dereference pointer)
5002 - already a register, but not in the right class */
5003 if (r
>= VT_CONST
||
5004 (vtop
->r
& VT_LVAL
) ||
5005 !(reg_classes
[r
] & rc
) ||
5006 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
5007 !(reg_classes
[vtop
->r2
] & rc2
))) {
5009 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
5010 /* two register type load : expand to two words
5012 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
5015 vtop
->c
.ui
= ll
; /* first word */
5017 vtop
->r
= r
; /* save register value */
5018 vpushi(ll
>> 32); /* second word */
5019 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
5020 (vtop
->r
& VT_LVAL
)) {
5021 /* We do not want to modifier the long long
5022 pointer here, so the safest (and less
5023 efficient) is to save all the other registers
5024 in the stack. XXX: totally inefficient. */
5026 /* load from memory */
5029 vtop
[-1].r
= r
; /* save register value */
5030 /* increment pointer to get second word */
5031 vtop
->type
.t
= VT_INT
;
5037 /* move registers */
5040 vtop
[-1].r
= r
; /* save register value */
5041 vtop
->r
= vtop
[-1].r2
;
5043 /* allocate second register */
5047 /* write second register */
5049 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
5051 /* lvalue of scalar type : need to use lvalue type
5052 because of possible cast */
5055 /* compute memory access type */
5056 if (vtop
->r
& VT_LVAL_BYTE
)
5058 else if (vtop
->r
& VT_LVAL_SHORT
)
5060 if (vtop
->r
& VT_LVAL_UNSIGNED
)
5064 /* restore wanted type */
5067 /* one register type load */
5072 #ifdef TCC_TARGET_C67
5073 /* uses register pairs for doubles */
5074 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
5081 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5082 void gv2(int rc1
, int rc2
)
5086 /* generate more generic register first. But VT_JMP or VT_CMP
5087 values must be generated first in all cases to avoid possible
5089 v
= vtop
[0].r
& VT_VALMASK
;
5090 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
5095 /* test if reload is needed for first register */
5096 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
5106 /* test if reload is needed for first register */
5107 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
5113 /* expand long long on stack in two int registers */
5118 u
= vtop
->type
.t
& VT_UNSIGNED
;
5121 vtop
[0].r
= vtop
[-1].r2
;
5122 vtop
[0].r2
= VT_CONST
;
5123 vtop
[-1].r2
= VT_CONST
;
5124 vtop
[0].type
.t
= VT_INT
| u
;
5125 vtop
[-1].type
.t
= VT_INT
| u
;
5128 #ifdef TCC_TARGET_ARM
5129 /* expand long long on stack */
5130 void lexpand_nr(void)
5134 u
= vtop
->type
.t
& VT_UNSIGNED
;
5136 vtop
->r2
= VT_CONST
;
5137 vtop
->type
.t
= VT_INT
| u
;
5138 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
5139 if (v
== VT_CONST
) {
5140 vtop
[-1].c
.ui
= vtop
->c
.ull
;
5141 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
5143 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
5145 vtop
->r
= vtop
[-1].r
;
5146 } else if (v
> VT_CONST
) {
5150 vtop
->r
= vtop
[-1].r2
;
5151 vtop
[-1].r2
= VT_CONST
;
5152 vtop
[-1].type
.t
= VT_INT
| u
;
5156 /* build a long long from two ints */
5159 gv2(RC_INT
, RC_INT
);
5160 vtop
[-1].r2
= vtop
[0].r
;
5161 vtop
[-1].type
.t
= t
;
5165 /* rotate n first stack elements to the bottom
5166 I1 ... In -> I2 ... In I1 [top is right]
5174 for(i
=-n
+1;i
!=0;i
++)
5175 vtop
[i
] = vtop
[i
+1];
5179 /* rotate n first stack elements to the top
5180 I1 ... In -> In I1 ... I(n-1) [top is right]
5188 for(i
= 0;i
< n
- 1; i
++)
5189 vtop
[-i
] = vtop
[-i
- 1];
5193 #ifdef TCC_TARGET_ARM
5194 /* like vrott but in other direction
5195 In ... I1 -> I(n-1) ... I1 In [top is right]
5203 for(i
= n
- 1; i
> 0; i
--)
5204 vtop
[-i
] = vtop
[-i
+ 1];
5209 /* pop stack value */
5213 v
= vtop
->r
& VT_VALMASK
;
5214 #ifdef TCC_TARGET_I386
5215 /* for x86, we need to pop the FP stack */
5216 if (v
== TREG_ST0
&& !nocode_wanted
) {
5217 o(0xd9dd); /* fstp %st(1) */
5220 if (v
== VT_JMP
|| v
== VT_JMPI
) {
5221 /* need to put correct jump if && or || without test */
5227 /* convert stack entry to register and duplicate its value in another
5235 if ((t
& VT_BTYPE
) == VT_LLONG
) {
5242 /* stack: H L L1 H1 */
5250 /* duplicate value */
5261 load(r1
, &sv
); /* move r to r1 */
5263 /* duplicates value */
5268 /* generate CPU independent (unsigned) long long operations */
5269 void gen_opl(int op
)
5271 int t
, a
, b
, op1
, c
, i
;
5273 unsigned short reg_iret
= REG_IRET
;
5274 unsigned short reg_lret
= REG_LRET
;
5280 func
= TOK___divdi3
;
5283 func
= TOK___udivdi3
;
5286 func
= TOK___moddi3
;
5289 func
= TOK___umoddi3
;
5296 /* call generic long long function */
5297 vpush_global_sym(&func_old_type
, func
);
5302 vtop
->r2
= reg_lret
;
5315 /* stack: L1 H1 L2 H2 */
5320 vtop
[-2] = vtop
[-3];
5323 /* stack: H1 H2 L1 L2 */
5329 /* stack: H1 H2 L1 L2 ML MH */
5332 /* stack: ML MH H1 H2 L1 L2 */
5336 /* stack: ML MH H1 L2 H2 L1 */
5341 /* stack: ML MH M1 M2 */
5344 } else if (op
== '+' || op
== '-') {
5345 /* XXX: add non carry method too (for MIPS or alpha) */
5351 /* stack: H1 H2 (L1 op L2) */
5354 gen_op(op1
+ 1); /* TOK_xxxC2 */
5357 /* stack: H1 H2 (L1 op L2) */
5360 /* stack: (L1 op L2) H1 H2 */
5362 /* stack: (L1 op L2) (H1 op H2) */
5370 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5371 t
= vtop
[-1].type
.t
;
5375 /* stack: L H shift */
5377 /* constant: simpler */
5378 /* NOTE: all comments are for SHL. the other cases are
5379 done by swaping words */
5390 if (op
!= TOK_SAR
) {
5423 /* XXX: should provide a faster fallback on x86 ? */
5426 func
= TOK___ashrdi3
;
5429 func
= TOK___lshrdi3
;
5432 func
= TOK___ashldi3
;
5438 /* compare operations */
5444 /* stack: L1 H1 L2 H2 */
5446 vtop
[-1] = vtop
[-2];
5448 /* stack: L1 L2 H1 H2 */
5451 /* when values are equal, we need to compare low words. since
5452 the jump is inverted, we invert the test too. */
5455 else if (op1
== TOK_GT
)
5457 else if (op1
== TOK_ULT
)
5459 else if (op1
== TOK_UGT
)
5464 if (op1
!= TOK_NE
) {
5468 /* generate non equal test */
5469 /* XXX: NOT PORTABLE yet */
5473 #if defined(TCC_TARGET_I386)
5474 b
= psym(0x850f, 0);
5475 #elif defined(TCC_TARGET_ARM)
5477 o(0x1A000000 | encbranch(ind
, 0, 1));
5478 #elif defined(TCC_TARGET_C67)
5479 error("not implemented");
5481 #error not supported
5485 /* compare low. Always unsigned */
5489 else if (op1
== TOK_LE
)
5491 else if (op1
== TOK_GT
)
5493 else if (op1
== TOK_GE
)
5503 /* handle integer constant optimizations and various machine
5505 void gen_opic(int op
)
5507 int c1
, c2
, t1
, t2
, n
;
5510 typedef unsigned long long U
;
5514 t1
= v1
->type
.t
& VT_BTYPE
;
5515 t2
= v2
->type
.t
& VT_BTYPE
;
5516 l1
= (t1
== VT_LLONG
) ? v1
->c
.ll
: v1
->c
.i
;
5517 l2
= (t2
== VT_LLONG
) ? v2
->c
.ll
: v2
->c
.i
;
5519 /* currently, we cannot do computations with forward symbols */
5520 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5521 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5524 case '+': l1
+= l2
; break;
5525 case '-': l1
-= l2
; break;
5526 case '&': l1
&= l2
; break;
5527 case '^': l1
^= l2
; break;
5528 case '|': l1
|= l2
; break;
5529 case '*': l1
*= l2
; break;
5536 /* if division by zero, generate explicit division */
5539 error("division by zero in constant");
5543 default: l1
/= l2
; break;
5544 case '%': l1
%= l2
; break;
5545 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
5546 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
5549 case TOK_SHL
: l1
<<= l2
; break;
5550 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
5551 case TOK_SAR
: l1
>>= l2
; break;
5553 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
5554 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
5555 case TOK_EQ
: l1
= l1
== l2
; break;
5556 case TOK_NE
: l1
= l1
!= l2
; break;
5557 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
5558 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
5559 case TOK_LT
: l1
= l1
< l2
; break;
5560 case TOK_GE
: l1
= l1
>= l2
; break;
5561 case TOK_LE
: l1
= l1
<= l2
; break;
5562 case TOK_GT
: l1
= l1
> l2
; break;
5564 case TOK_LAND
: l1
= l1
&& l2
; break;
5565 case TOK_LOR
: l1
= l1
|| l2
; break;
5572 /* if commutative ops, put c2 as constant */
5573 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
5574 op
== '|' || op
== '*')) {
5576 c2
= c1
; //c = c1, c1 = c2, c2 = c;
5577 l2
= l1
; //l = l1, l1 = l2, l2 = l;
5579 /* Filter out NOP operations like x*1, x-0, x&-1... */
5580 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
5583 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
5584 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
5590 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
5591 /* try to use shifts instead of muls or divs */
5592 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
5601 else if (op
== TOK_PDIV
)
5607 } else if (c2
&& (op
== '+' || op
== '-') &&
5608 ((vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
5609 (VT_CONST
| VT_SYM
) ||
5610 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
)) == VT_LOCAL
)) {
5611 /* symbol + constant case */
5618 if (!nocode_wanted
) {
5619 /* call low level op generator */
5620 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
5631 /* generate a floating point operation with constant propagation */
5632 void gen_opif(int op
)
5640 /* currently, we cannot do computations with forward symbols */
5641 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5642 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5644 if (v1
->type
.t
== VT_FLOAT
) {
5647 } else if (v1
->type
.t
== VT_DOUBLE
) {
5655 /* NOTE: we only do constant propagation if finite number (not
5656 NaN or infinity) (ANSI spec) */
5657 if (!ieee_finite(f1
) || !ieee_finite(f2
))
5661 case '+': f1
+= f2
; break;
5662 case '-': f1
-= f2
; break;
5663 case '*': f1
*= f2
; break;
5667 error("division by zero in constant");
5672 /* XXX: also handles tests ? */
5676 /* XXX: overflow test ? */
5677 if (v1
->type
.t
== VT_FLOAT
) {
5679 } else if (v1
->type
.t
== VT_DOUBLE
) {
5687 if (!nocode_wanted
) {
5695 static int pointed_size(CType
*type
)
5698 return type_size(pointed_type(type
), &align
);
5701 static inline int is_null_pointer(SValue
*p
)
5703 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5705 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
5706 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
5709 static inline int is_integer_btype(int bt
)
5711 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
5712 bt
== VT_INT
|| bt
== VT_LLONG
);
5715 /* check types for comparison or substraction of pointers */
5716 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
5718 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
5721 /* null pointers are accepted for all comparisons as gcc */
5722 if (is_null_pointer(p1
) || is_null_pointer(p2
))
5726 bt1
= type1
->t
& VT_BTYPE
;
5727 bt2
= type2
->t
& VT_BTYPE
;
5728 /* accept comparison between pointer and integer with a warning */
5729 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
5730 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
5731 warning("comparison between pointer and integer");
5735 /* both must be pointers or implicit function pointers */
5736 if (bt1
== VT_PTR
) {
5737 type1
= pointed_type(type1
);
5738 } else if (bt1
!= VT_FUNC
)
5739 goto invalid_operands
;
5741 if (bt2
== VT_PTR
) {
5742 type2
= pointed_type(type2
);
5743 } else if (bt2
!= VT_FUNC
) {
5745 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
5747 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5748 (type2
->t
& VT_BTYPE
) == VT_VOID
)
5752 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5753 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5754 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
5755 /* gcc-like error if '-' is used */
5757 goto invalid_operands
;
5759 warning("comparison of distinct pointer types lacks a cast");
5763 /* generic gen_op: handles types problems */
5766 int u
, t1
, t2
, bt1
, bt2
, t
;
5769 t1
= vtop
[-1].type
.t
;
5770 t2
= vtop
[0].type
.t
;
5771 bt1
= t1
& VT_BTYPE
;
5772 bt2
= t2
& VT_BTYPE
;
5774 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5775 /* at least one operand is a pointer */
5776 /* relationnal op: must be both pointers */
5777 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
5778 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5779 /* pointers are handled are unsigned */
5780 t
= VT_INT
| VT_UNSIGNED
;
5783 /* if both pointers, then it must be the '-' op */
5784 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
5786 error("cannot use pointers here");
5787 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5788 /* XXX: check that types are compatible */
5789 u
= pointed_size(&vtop
[-1].type
);
5791 /* set to integer type */
5792 vtop
->type
.t
= VT_INT
;
5796 /* exactly one pointer : must be '+' or '-'. */
5797 if (op
!= '-' && op
!= '+')
5798 error("cannot use pointers here");
5799 /* Put pointer as first operand */
5800 if (bt2
== VT_PTR
) {
5804 type1
= vtop
[-1].type
;
5805 /* XXX: cast to int ? (long long case) */
5806 vpushi(pointed_size(&vtop
[-1].type
));
5808 #ifdef CONFIG_TCC_BCHECK
5809 /* if evaluating constant expression, no code should be
5810 generated, so no bound check */
5811 if (do_bounds_check
&& !const_wanted
) {
5812 /* if bounded pointers, we generate a special code to
5819 gen_bounded_ptr_add();
5825 /* put again type if gen_opic() swaped operands */
5828 } else if (is_float(bt1
) || is_float(bt2
)) {
5829 /* compute bigger type and do implicit casts */
5830 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5832 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5837 /* floats can only be used for a few operations */
5838 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
5839 (op
< TOK_ULT
|| op
> TOK_GT
))
5840 error("invalid operands for binary operation");
5842 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5843 /* cast to biggest op */
5845 /* convert to unsigned if it does not fit in a long long */
5846 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5847 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5851 /* integer operations */
5853 /* convert to unsigned if it does not fit in an integer */
5854 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5855 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5858 /* XXX: currently, some unsigned operations are explicit, so
5859 we modify them here */
5860 if (t
& VT_UNSIGNED
) {
5867 else if (op
== TOK_LT
)
5869 else if (op
== TOK_GT
)
5871 else if (op
== TOK_LE
)
5873 else if (op
== TOK_GE
)
5880 /* special case for shifts and long long: we keep the shift as
5882 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
5889 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5890 /* relationnal op: the result is an int */
5891 vtop
->type
.t
= VT_INT
;
5898 #ifndef TCC_TARGET_ARM
5899 /* generic itof for unsigned long long case */
5900 void gen_cvt_itof1(int t
)
5902 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
5903 (VT_LLONG
| VT_UNSIGNED
)) {
5906 vpush_global_sym(&func_old_type
, TOK___floatundisf
);
5907 #if LDOUBLE_SIZE != 8
5908 else if (t
== VT_LDOUBLE
)
5909 vpush_global_sym(&func_old_type
, TOK___floatundixf
);
5912 vpush_global_sym(&func_old_type
, TOK___floatundidf
);
5923 /* generic ftoi for unsigned long long case */
5924 void gen_cvt_ftoi1(int t
)
5928 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
5929 /* not handled natively */
5930 st
= vtop
->type
.t
& VT_BTYPE
;
5932 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
5933 #if LDOUBLE_SIZE != 8
5934 else if (st
== VT_LDOUBLE
)
5935 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
5938 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
5943 vtop
->r2
= REG_LRET
;
5949 /* force char or short cast */
5950 void force_charshort_cast(int t
)
5954 /* XXX: add optimization if lvalue : just change type and offset */
5959 if (t
& VT_UNSIGNED
) {
5960 vpushi((1 << bits
) - 1);
5966 /* result must be signed or the SAR is converted to an SHL
5967 This was not the case when "t" was a signed short
5968 and the last value on the stack was an unsigned int */
5969 vtop
->type
.t
&= ~VT_UNSIGNED
;
5975 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5976 static void gen_cast(CType
*type
)
5978 int sbt
, dbt
, sf
, df
, c
, p
;
5980 /* special delayed cast for char/short */
5981 /* XXX: in some cases (multiple cascaded casts), it may still
5983 if (vtop
->r
& VT_MUSTCAST
) {
5984 vtop
->r
&= ~VT_MUSTCAST
;
5985 force_charshort_cast(vtop
->type
.t
);
5988 /* bitfields first get cast to ints */
5989 if (vtop
->type
.t
& VT_BITFIELD
) {
5993 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
5994 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
5999 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
6000 p
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == (VT_CONST
| VT_SYM
);
6002 /* constant case: we can do it now */
6003 /* XXX: in ISOC, cannot do it if error in convert */
6004 if (sbt
== VT_FLOAT
)
6005 vtop
->c
.ld
= vtop
->c
.f
;
6006 else if (sbt
== VT_DOUBLE
)
6007 vtop
->c
.ld
= vtop
->c
.d
;
6010 if ((sbt
& VT_BTYPE
) == VT_LLONG
) {
6011 if (sbt
& VT_UNSIGNED
)
6012 vtop
->c
.ld
= vtop
->c
.ull
;
6014 vtop
->c
.ld
= vtop
->c
.ll
;
6016 if (sbt
& VT_UNSIGNED
)
6017 vtop
->c
.ld
= vtop
->c
.ui
;
6019 vtop
->c
.ld
= vtop
->c
.i
;
6022 if (dbt
== VT_FLOAT
)
6023 vtop
->c
.f
= (float)vtop
->c
.ld
;
6024 else if (dbt
== VT_DOUBLE
)
6025 vtop
->c
.d
= (double)vtop
->c
.ld
;
6026 } else if (sf
&& dbt
== (VT_LLONG
|VT_UNSIGNED
)) {
6027 vtop
->c
.ull
= (unsigned long long)vtop
->c
.ld
;
6028 } else if (sf
&& dbt
== VT_BOOL
) {
6029 vtop
->c
.i
= (vtop
->c
.ld
!= 0);
6032 vtop
->c
.ll
= (long long)vtop
->c
.ld
;
6033 else if (sbt
== (VT_LLONG
|VT_UNSIGNED
))
6034 vtop
->c
.ll
= vtop
->c
.ull
;
6035 else if (sbt
& VT_UNSIGNED
)
6036 vtop
->c
.ll
= vtop
->c
.ui
;
6037 else if (sbt
!= VT_LLONG
)
6038 vtop
->c
.ll
= vtop
->c
.i
;
6040 if (dbt
== (VT_LLONG
|VT_UNSIGNED
))
6041 vtop
->c
.ull
= vtop
->c
.ll
;
6042 else if (dbt
== VT_BOOL
)
6043 vtop
->c
.i
= (vtop
->c
.ll
!= 0);
6044 else if (dbt
!= VT_LLONG
) {
6046 if ((dbt
& VT_BTYPE
) == VT_BYTE
)
6048 else if ((dbt
& VT_BTYPE
) == VT_SHORT
)
6051 if(dbt
& VT_UNSIGNED
)
6052 vtop
->c
.ui
= ((unsigned int)vtop
->c
.ll
<< s
) >> s
;
6054 vtop
->c
.i
= ((int)vtop
->c
.ll
<< s
) >> s
;
6057 } else if (p
&& dbt
== VT_BOOL
) {
6060 } else if (!nocode_wanted
) {
6061 /* non constant case: generate code */
6063 /* convert from fp to fp */
6066 /* convert int to fp */
6069 /* convert fp to int */
6070 if (dbt
== VT_BOOL
) {
6074 /* we handle char/short/etc... with generic code */
6075 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
6076 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
6080 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
6081 /* additional cast for char/short... */
6086 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
6087 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
6088 /* scalar to long long */
6089 /* machine independent conversion */
6091 /* generate high word */
6092 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
6100 /* patch second register */
6101 vtop
[-1].r2
= vtop
->r
;
6104 } else if (dbt
== VT_BOOL
) {
6105 /* scalar to bool */
6108 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
6109 (dbt
& VT_BTYPE
) == VT_SHORT
) {
6110 if (sbt
== VT_PTR
) {
6111 vtop
->type
.t
= VT_INT
;
6112 warning("nonportable conversion from pointer to char/short");
6114 force_charshort_cast(dbt
);
6115 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
6117 if (sbt
== VT_LLONG
) {
6118 /* from long long: just take low order word */
6122 /* if lvalue and single word type, nothing to do because
6123 the lvalue already contains the real type size (see
6124 VT_LVAL_xxx constants) */
6127 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
6128 /* if we are casting between pointer types,
6129 we must update the VT_LVAL_xxx size */
6130 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
6131 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
6136 /* return type size. Put alignment at 'a' */
6137 static int type_size(CType
*type
, int *a
)
6142 bt
= type
->t
& VT_BTYPE
;
6143 if (bt
== VT_STRUCT
) {
6148 } else if (bt
== VT_PTR
) {
6149 if (type
->t
& VT_ARRAY
) {
6151 return type_size(&s
->type
, a
) * s
->c
;
6156 } else if (bt
== VT_LDOUBLE
) {
6158 return LDOUBLE_SIZE
;
6159 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
6160 #ifdef TCC_TARGET_I386
6162 #elif defined(TCC_TARGET_ARM)
6172 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
6175 } else if (bt
== VT_SHORT
) {
6179 /* char, void, function, _Bool */
6185 /* return the pointed type of t */
6186 static inline CType
*pointed_type(CType
*type
)
6188 return &type
->ref
->type
;
6191 /* modify type so that its it is a pointer to type. */
6192 static void mk_pointer(CType
*type
)
6195 s
= sym_push(SYM_FIELD
, type
, 0, -1);
6196 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
6200 /* compare function types. OLD functions match any new functions */
6201 static int is_compatible_func(CType
*type1
, CType
*type2
)
6207 if (!is_compatible_types(&s1
->type
, &s2
->type
))
6209 /* check func_call */
6210 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
6212 /* XXX: not complete */
6213 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
6217 while (s1
!= NULL
) {
6220 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
6230 /* return true if type1 and type2 are the same. If unqualified is
6231 true, qualifiers on the types are ignored.
6233 - enums are not checked as gcc __builtin_types_compatible_p ()
6235 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
6239 t1
= type1
->t
& VT_TYPE
;
6240 t2
= type2
->t
& VT_TYPE
;
6242 /* strip qualifiers before comparing */
6243 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6244 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6246 /* XXX: bitfields ? */
6249 /* test more complicated cases */
6250 bt1
= t1
& VT_BTYPE
;
6251 if (bt1
== VT_PTR
) {
6252 type1
= pointed_type(type1
);
6253 type2
= pointed_type(type2
);
6254 return is_compatible_types(type1
, type2
);
6255 } else if (bt1
== VT_STRUCT
) {
6256 return (type1
->ref
== type2
->ref
);
6257 } else if (bt1
== VT_FUNC
) {
6258 return is_compatible_func(type1
, type2
);
6264 /* return true if type1 and type2 are exactly the same (including
6267 static int is_compatible_types(CType
*type1
, CType
*type2
)
6269 return compare_types(type1
,type2
,0);
6272 /* return true if type1 and type2 are the same (ignoring qualifiers).
6274 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
6276 return compare_types(type1
,type2
,1);
6279 /* print a type. If 'varstr' is not NULL, then the variable is also
6280 printed in the type */
6282 /* XXX: add array and function pointers */
6283 void type_to_str(char *buf
, int buf_size
,
6284 CType
*type
, const char *varstr
)
6291 t
= type
->t
& VT_TYPE
;
6294 if (t
& VT_CONSTANT
)
6295 pstrcat(buf
, buf_size
, "const ");
6296 if (t
& VT_VOLATILE
)
6297 pstrcat(buf
, buf_size
, "volatile ");
6298 if (t
& VT_UNSIGNED
)
6299 pstrcat(buf
, buf_size
, "unsigned ");
6329 tstr
= "long double";
6331 pstrcat(buf
, buf_size
, tstr
);
6335 if (bt
== VT_STRUCT
)
6339 pstrcat(buf
, buf_size
, tstr
);
6340 v
= type
->ref
->v
& ~SYM_STRUCT
;
6341 if (v
>= SYM_FIRST_ANOM
)
6342 pstrcat(buf
, buf_size
, "<anonymous>");
6344 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
6348 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
6349 pstrcat(buf
, buf_size
, "(");
6351 while (sa
!= NULL
) {
6352 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
6353 pstrcat(buf
, buf_size
, buf1
);
6356 pstrcat(buf
, buf_size
, ", ");
6358 pstrcat(buf
, buf_size
, ")");
6362 pstrcpy(buf1
, sizeof(buf1
), "*");
6364 pstrcat(buf1
, sizeof(buf1
), varstr
);
6365 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
6369 pstrcat(buf
, buf_size
, " ");
6370 pstrcat(buf
, buf_size
, varstr
);
6375 /* verify type compatibility to store vtop in 'dt' type, and generate
6377 static void gen_assign_cast(CType
*dt
)
6379 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
6380 char buf1
[256], buf2
[256];
6383 st
= &vtop
->type
; /* source type */
6384 dbt
= dt
->t
& VT_BTYPE
;
6385 sbt
= st
->t
& VT_BTYPE
;
6386 if (dt
->t
& VT_CONSTANT
)
6387 warning("assignment of read-only location");
6390 /* special cases for pointers */
6391 /* '0' can also be a pointer */
6392 if (is_null_pointer(vtop
))
6394 /* accept implicit pointer to integer cast with warning */
6395 if (is_integer_btype(sbt
)) {
6396 warning("assignment makes pointer from integer without a cast");
6399 type1
= pointed_type(dt
);
6400 /* a function is implicitely a function pointer */
6401 if (sbt
== VT_FUNC
) {
6402 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
6403 !is_compatible_types(pointed_type(dt
), st
))
6410 type2
= pointed_type(st
);
6411 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
6412 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
6413 /* void * can match anything */
6415 /* exact type match, except for unsigned */
6418 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6419 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6420 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
6421 warning("assignment from incompatible pointer type");
6423 /* check const and volatile */
6424 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
6425 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
6426 warning("assignment discards qualifiers from pointer target type");
6432 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
6433 warning("assignment makes integer from pointer without a cast");
6435 /* XXX: more tests */
6440 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6441 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6442 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
6444 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
6445 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
6446 error("cannot cast '%s' to '%s'", buf1
, buf2
);
6454 /* store vtop in lvalue pushed on stack */
6457 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
6459 ft
= vtop
[-1].type
.t
;
6460 sbt
= vtop
->type
.t
& VT_BTYPE
;
6461 dbt
= ft
& VT_BTYPE
;
6462 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
6463 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
6464 /* optimize char/short casts */
6465 delayed_cast
= VT_MUSTCAST
;
6466 vtop
->type
.t
= ft
& (VT_TYPE
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
)));
6467 /* XXX: factorize */
6468 if (ft
& VT_CONSTANT
)
6469 warning("assignment of read-only location");
6472 if (!(ft
& VT_BITFIELD
))
6473 gen_assign_cast(&vtop
[-1].type
);
6476 if (sbt
== VT_STRUCT
) {
6477 /* if structure, only generate pointer */
6478 /* structure assignment : generate memcpy */
6479 /* XXX: optimize if small size */
6480 if (!nocode_wanted
) {
6481 size
= type_size(&vtop
->type
, &align
);
6485 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
6486 else if(!(align
& 3))
6487 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
6490 vpush_global_sym(&func_old_type
, TOK_memcpy
);
6494 vtop
->type
.t
= VT_INT
;
6498 vtop
->type
.t
= VT_INT
;
6510 /* leave source on stack */
6511 } else if (ft
& VT_BITFIELD
) {
6512 /* bitfield store handling */
6513 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
6514 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6515 /* remove bit field info to avoid loops */
6516 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
6518 /* duplicate source into other register */
6523 if((ft
& VT_BTYPE
) == VT_BOOL
) {
6524 gen_cast(&vtop
[-1].type
);
6525 vtop
[-1].type
.t
= (vtop
[-1].type
.t
& ~VT_BTYPE
) | (VT_BYTE
| VT_UNSIGNED
);
6528 /* duplicate destination */
6530 vtop
[-1] = vtop
[-2];
6532 /* mask and shift source */
6533 if((ft
& VT_BTYPE
) != VT_BOOL
) {
6534 vpushi((1 << bit_size
) - 1);
6539 /* load destination, mask and or with source */
6541 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
6547 /* pop off shifted source from "duplicate source..." above */
6551 #ifdef CONFIG_TCC_BCHECK
6552 /* bound check case */
6553 if (vtop
[-1].r
& VT_MUSTBOUND
) {
6559 if (!nocode_wanted
) {
6563 r
= gv(rc
); /* generate value */
6564 /* if lvalue was saved on stack, must read it */
6565 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
6567 t
= get_reg(RC_INT
);
6569 sv
.r
= VT_LOCAL
| VT_LVAL
;
6570 sv
.c
.ul
= vtop
[-1].c
.ul
;
6572 vtop
[-1].r
= t
| VT_LVAL
;
6575 /* two word case handling : store second register at word + 4 */
6576 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
6578 /* convert to int to increment easily */
6579 vtop
->type
.t
= VT_INT
;
6585 /* XXX: it works because r2 is spilled last ! */
6586 store(vtop
->r2
, vtop
- 1);
6590 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6591 vtop
->r
|= delayed_cast
;
6595 /* post defines POST/PRE add. c is the token ++ or -- */
6596 void inc(int post
, int c
)
6599 vdup(); /* save lvalue */
6601 gv_dup(); /* duplicate value */
6606 vpushi(c
- TOK_MID
);
6608 vstore(); /* store value */
6610 vpop(); /* if post op, return saved value */
6613 /* Parse GNUC __attribute__ extension. Currently, the following
6614 extensions are recognized:
6615 - aligned(n) : set data/function alignment.
6616 - packed : force data alignment to 1
6617 - section(x) : generate data/code in this section.
6618 - unused : currently ignored, but may be used someday.
6619 - regparm(n) : pass function parameters in registers (i386 only)
6621 static void parse_attribute(AttributeDef
*ad
)
6625 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
6629 while (tok
!= ')') {
6630 if (tok
< TOK_IDENT
)
6631 expect("attribute name");
6639 expect("section name");
6640 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
6649 if (n
<= 0 || (n
& (n
- 1)) != 0)
6650 error("alignment must be a positive power of two");
6663 /* currently, no need to handle it because tcc does not
6664 track unused objects */
6668 /* currently, no need to handle it because tcc does not
6669 track unused objects */
6674 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
6679 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
6681 #ifdef TCC_TARGET_I386
6691 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
6697 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
6701 FUNC_EXPORT(ad
->func_attr
) = 1;
6704 if (tcc_state
->warn_unsupported
)
6705 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
6706 /* skip parameters */
6708 int parenthesis
= 0;
6712 else if (tok
== ')')
6715 } while (parenthesis
&& tok
!= -1);
6728 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6729 static void struct_decl(CType
*type
, int u
)
6731 int a
, v
, size
, align
, maxalign
, c
, offset
;
6732 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
, prevbt
;
6733 Sym
*s
, *ss
, *ass
, **ps
;
6737 a
= tok
; /* save decl type */
6742 /* struct already defined ? return it */
6744 expect("struct/union/enum name");
6748 error("invalid type");
6755 /* we put an undefined size for struct/union */
6756 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
6757 s
->r
= 0; /* default alignment is zero as gcc */
6758 /* put struct/union/enum name in type */
6766 error("struct/union/enum already defined");
6767 /* cannot be empty */
6769 /* non empty enums are not allowed */
6770 if (a
== TOK_ENUM
) {
6774 expect("identifier");
6780 /* enum symbols have static storage */
6781 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
6782 ss
->type
.t
|= VT_STATIC
;
6787 /* NOTE: we accept a trailing comma */
6798 while (tok
!= '}') {
6799 parse_btype(&btype
, &ad
);
6805 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
6806 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
6807 expect("identifier");
6808 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
6809 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
6810 error("invalid type for '%s'",
6811 get_tok_str(v
, NULL
));
6815 bit_size
= expr_const();
6816 /* XXX: handle v = 0 case for messages */
6818 error("negative width in bit-field '%s'",
6819 get_tok_str(v
, NULL
));
6820 if (v
&& bit_size
== 0)
6821 error("zero width for bit-field '%s'",
6822 get_tok_str(v
, NULL
));
6824 size
= type_size(&type1
, &align
);
6826 if (align
< ad
.aligned
)
6828 } else if (ad
.packed
) {
6830 } else if (*tcc_state
->pack_stack_ptr
) {
6831 if (align
> *tcc_state
->pack_stack_ptr
)
6832 align
= *tcc_state
->pack_stack_ptr
;
6835 if (bit_size
>= 0) {
6836 bt
= type1
.t
& VT_BTYPE
;
6842 error("bitfields must have scalar type");
6844 if (bit_size
> bsize
) {
6845 error("width of '%s' exceeds its type",
6846 get_tok_str(v
, NULL
));
6847 } else if (bit_size
== bsize
) {
6848 /* no need for bit fields */
6850 } else if (bit_size
== 0) {
6851 /* XXX: what to do if only padding in a
6853 /* zero size: means to pad */
6856 /* we do not have enough room ?
6857 did the type change?
6859 if ((bit_pos
+ bit_size
) > bsize
||
6860 bt
!= prevbt
|| a
== TOK_UNION
)
6863 /* XXX: handle LSB first */
6864 type1
.t
|= VT_BITFIELD
|
6865 (bit_pos
<< VT_STRUCT_SHIFT
) |
6866 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
6867 bit_pos
+= bit_size
;
6873 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
6874 /* add new memory data only if starting
6876 if (lbit_pos
== 0) {
6877 if (a
== TOK_STRUCT
) {
6878 c
= (c
+ align
- 1) & -align
;
6887 if (align
> maxalign
)
6891 printf("add field %s offset=%d",
6892 get_tok_str(v
, NULL
), offset
);
6893 if (type1
.t
& VT_BITFIELD
) {
6894 printf(" pos=%d size=%d",
6895 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
6896 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
6901 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
6903 while ((ass
= ass
->next
) != NULL
) {
6904 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
6909 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
6913 if (tok
== ';' || tok
== TOK_EOF
)
6920 /* store size and alignment */
6921 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
6927 /* return 0 if no type declaration. otherwise, return the basic type
6930 static int parse_btype(CType
*type
, AttributeDef
*ad
)
6932 int t
, u
, type_found
, typespec_found
, typedef_found
;
6936 memset(ad
, 0, sizeof(AttributeDef
));
6944 /* currently, we really ignore extension */
6954 if ((t
& VT_BTYPE
) != 0)
6955 error("too many basic types");
6971 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
6972 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6973 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
6974 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
6988 if ((t
& VT_BTYPE
) == VT_LONG
) {
6989 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6996 struct_decl(&type1
, VT_ENUM
);
6999 type
->ref
= type1
.ref
;
7003 struct_decl(&type1
, VT_STRUCT
);
7006 /* type modifiers */
7059 /* GNUC attribute */
7060 case TOK_ATTRIBUTE1
:
7061 case TOK_ATTRIBUTE2
:
7062 parse_attribute(ad
);
7069 parse_expr_type(&type1
);
7072 if (typespec_found
|| typedef_found
)
7075 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
7078 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
7079 type
->ref
= s
->type
.ref
;
7087 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
7088 error("signed and unsigned modifier");
7089 if (tcc_state
->char_is_unsigned
) {
7090 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
7095 /* long is never used as type */
7096 if ((t
& VT_BTYPE
) == VT_LONG
)
7097 t
= (t
& ~VT_BTYPE
) | VT_INT
;
7102 /* convert a function parameter type (array to pointer and function to
7103 function pointer) */
7104 static inline void convert_parameter_type(CType
*pt
)
7106 /* remove const and volatile qualifiers (XXX: const could be used
7107 to indicate a const function parameter */
7108 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7109 /* array must be transformed to pointer according to ANSI C */
7111 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
7116 static void post_type(CType
*type
, AttributeDef
*ad
)
7118 int n
, l
, t1
, arg_size
, align
;
7119 Sym
**plast
, *s
, *first
;
7124 /* function declaration */
7132 /* read param name and compute offset */
7133 if (l
!= FUNC_OLD
) {
7134 if (!parse_btype(&pt
, &ad1
)) {
7136 error("invalid type");
7143 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
7145 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
7146 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
7147 error("parameter declared as void");
7148 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
7153 expect("identifier");
7157 convert_parameter_type(&pt
);
7158 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
7164 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
7171 /* if no parameters, then old type prototype */
7175 t1
= type
->t
& VT_STORAGE
;
7176 /* NOTE: const is ignored in returned type as it has a special
7177 meaning in gcc / C++ */
7178 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
7179 post_type(type
, ad
);
7180 /* we push a anonymous symbol which will contain the function prototype */
7181 FUNC_ARGS(ad
->func_attr
) = arg_size
;
7182 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
7184 type
->t
= t1
| VT_FUNC
;
7186 } else if (tok
== '[') {
7187 /* array definition */
7193 error("invalid array size");
7196 /* parse next post type */
7197 t1
= type
->t
& VT_STORAGE
;
7198 type
->t
&= ~VT_STORAGE
;
7199 post_type(type
, ad
);
7201 /* we push a anonymous symbol which will contain the array
7203 s
= sym_push(SYM_FIELD
, type
, 0, n
);
7204 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
7209 /* Parse a type declaration (except basic type), and return the type
7210 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7211 expected. 'type' should contain the basic type. 'ad' is the
7212 attribute definition of the basic type. It can be modified by
7215 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
7218 CType type1
, *type2
;
7221 while (tok
== '*') {
7229 qualifiers
|= VT_CONSTANT
;
7234 qualifiers
|= VT_VOLATILE
;
7242 type
->t
|= qualifiers
;
7245 /* XXX: clarify attribute handling */
7246 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7247 parse_attribute(ad
);
7249 /* recursive type */
7250 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7251 type1
.t
= 0; /* XXX: same as int */
7254 /* XXX: this is not correct to modify 'ad' at this point, but
7255 the syntax is not clear */
7256 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7257 parse_attribute(ad
);
7258 type_decl(&type1
, ad
, v
, td
);
7261 /* type identifier */
7262 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
7266 if (!(td
& TYPE_ABSTRACT
))
7267 expect("identifier");
7271 post_type(type
, ad
);
7272 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7273 parse_attribute(ad
);
7276 /* append type at the end of type1 */
7289 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7290 static int lvalue_type(int t
)
7295 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
7297 else if (bt
== VT_SHORT
)
7301 if (t
& VT_UNSIGNED
)
7302 r
|= VT_LVAL_UNSIGNED
;
7306 /* indirection with full error checking and bound check */
7307 static void indir(void)
7309 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
7310 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
7314 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
7316 vtop
->type
= *pointed_type(&vtop
->type
);
7317 /* Arrays and functions are never lvalues */
7318 if (!(vtop
->type
.t
& VT_ARRAY
)
7319 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7320 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7321 /* if bound checking, the referenced pointer must be checked */
7322 if (do_bounds_check
)
7323 vtop
->r
|= VT_MUSTBOUND
;
7327 /* pass a parameter to a function and do type checking and casting */
7328 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
7333 func_type
= func
->c
;
7334 if (func_type
== FUNC_OLD
||
7335 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
7336 /* default casting : only need to convert float to double */
7337 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
7341 } else if (arg
== NULL
) {
7342 error("too many arguments to function");
7345 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
7346 gen_assign_cast(&type
);
7350 /* parse an expression of the form '(type)' or '(expr)' and return its
7352 static void parse_expr_type(CType
*type
)
7358 if (parse_btype(type
, &ad
)) {
7359 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7366 static void parse_type(CType
*type
)
7371 if (!parse_btype(type
, &ad
)) {
7374 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7377 static void vpush_tokc(int t
)
7381 vsetc(&type
, VT_CONST
, &tokc
);
7384 static void unary(void)
7386 int n
, t
, align
, size
, r
;
7391 /* XXX: GCC 2.95.3 does not generate a table although it should be
7405 vpush_tokc(VT_INT
| VT_UNSIGNED
);
7409 vpush_tokc(VT_LLONG
);
7413 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
7417 vpush_tokc(VT_FLOAT
);
7421 vpush_tokc(VT_DOUBLE
);
7425 vpush_tokc(VT_LDOUBLE
);
7428 case TOK___FUNCTION__
:
7430 goto tok_identifier
;
7436 /* special function name identifier */
7437 len
= strlen(funcname
) + 1;
7438 /* generate char[len] type */
7443 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
7444 ptr
= section_ptr_add(data_section
, len
);
7445 memcpy(ptr
, funcname
, len
);
7450 #ifdef TCC_TARGET_PE
7451 t
= VT_SHORT
| VT_UNSIGNED
;
7457 /* string parsing */
7460 if (tcc_state
->warn_write_strings
)
7465 memset(&ad
, 0, sizeof(AttributeDef
));
7466 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
7471 if (parse_btype(&type
, &ad
)) {
7472 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
7474 /* check ISOC99 compound literal */
7476 /* data is allocated locally by default */
7481 /* all except arrays are lvalues */
7482 if (!(type
.t
& VT_ARRAY
))
7483 r
|= lvalue_type(type
.t
);
7484 memset(&ad
, 0, sizeof(AttributeDef
));
7485 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
7490 } else if (tok
== '{') {
7491 /* save all registers */
7493 /* statement expression : we do not accept break/continue
7494 inside as GCC does */
7495 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
7510 /* functions names must be treated as function pointers,
7511 except for unary '&' and sizeof. Since we consider that
7512 functions are not lvalues, we only have to handle it
7513 there and in function calls. */
7514 /* arrays can also be used although they are not lvalues */
7515 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
7516 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
7518 mk_pointer(&vtop
->type
);
7524 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
7526 boolean
.t
= VT_BOOL
;
7528 vtop
->c
.i
= !vtop
->c
.i
;
7529 } else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
7530 vtop
->c
.i
= vtop
->c
.i
^ 1;
7533 vseti(VT_JMP
, gtst(1, 0));
7544 /* in order to force cast, we add zero */
7546 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
7547 error("pointer not accepted for unary plus");
7557 parse_expr_type(&type
);
7561 size
= type_size(&type
, &align
);
7562 if (t
== TOK_SIZEOF
) {
7564 error("sizeof applied to an incomplete type");
7569 vtop
->type
.t
|= VT_UNSIGNED
;
7572 case TOK_builtin_types_compatible_p
:
7581 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7582 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7583 vpushi(is_compatible_types(&type1
, &type2
));
7586 case TOK_builtin_constant_p
:
7588 int saved_nocode_wanted
, res
;
7591 saved_nocode_wanted
= nocode_wanted
;
7594 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
7596 nocode_wanted
= saved_nocode_wanted
;
7616 goto tok_identifier
;
7618 /* allow to take the address of a label */
7619 if (tok
< TOK_UIDENT
)
7620 expect("label identifier");
7621 s
= label_find(tok
);
7623 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
7625 if (s
->r
== LABEL_DECLARED
)
7626 s
->r
= LABEL_FORWARD
;
7629 s
->type
.t
= VT_VOID
;
7630 mk_pointer(&s
->type
);
7631 s
->type
.t
|= VT_STATIC
;
7633 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
7642 expect("identifier");
7646 error("'%s' undeclared", get_tok_str(t
, NULL
));
7647 /* for simple function calls, we tolerate undeclared
7648 external reference to int() function */
7649 if (tcc_state
->warn_implicit_function_declaration
)
7650 warning("implicit declaration of function '%s'",
7651 get_tok_str(t
, NULL
));
7652 s
= external_global_sym(t
, &func_old_type
, 0);
7654 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
7655 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
7656 /* if referencing an inline function, then we generate a
7657 symbol to it if not already done. It will have the
7658 effect to generate code for it at the end of the
7659 compilation unit. Inline function as always
7660 generated in the text section. */
7662 put_extern_sym(s
, text_section
, 0, 0);
7663 r
= VT_SYM
| VT_CONST
;
7667 vset(&s
->type
, r
, s
->c
);
7668 /* if forward reference, we must point to s */
7669 if (vtop
->r
& VT_SYM
) {
7676 /* post operations */
7678 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
7681 } else if (tok
== '.' || tok
== TOK_ARROW
) {
7683 if (tok
== TOK_ARROW
)
7688 /* expect pointer on structure */
7689 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
7690 expect("struct or union");
7694 while ((s
= s
->next
) != NULL
) {
7699 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
7700 /* add field offset to pointer */
7701 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
7704 /* change type to field type, and set to lvalue */
7705 vtop
->type
= s
->type
;
7706 /* an array is never an lvalue */
7707 if (!(vtop
->type
.t
& VT_ARRAY
)) {
7708 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7709 /* if bound checking, the referenced pointer must be checked */
7710 if (do_bounds_check
)
7711 vtop
->r
|= VT_MUSTBOUND
;
7714 } else if (tok
== '[') {
7720 } else if (tok
== '(') {
7726 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7727 /* pointer test (no array accepted) */
7728 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
7729 vtop
->type
= *pointed_type(&vtop
->type
);
7730 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7734 expect("function pointer");
7737 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
7739 /* get return type */
7742 sa
= s
->next
; /* first parameter */
7745 /* compute first implicit argument if a structure is returned */
7746 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
7747 /* get some space for the returned structure */
7748 size
= type_size(&s
->type
, &align
);
7749 loc
= (loc
- size
) & -align
;
7751 ret
.r
= VT_LOCAL
| VT_LVAL
;
7752 /* pass it as 'int' to avoid structure arg passing
7754 vseti(VT_LOCAL
, loc
);
7759 /* return in register */
7760 if (is_float(ret
.type
.t
)) {
7763 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
7772 gfunc_param_typed(s
, sa
);
7782 error("too few arguments to function");
7784 if (!nocode_wanted
) {
7785 gfunc_call(nb_args
);
7787 vtop
-= (nb_args
+ 1);
7790 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
7798 static void uneq(void)
7804 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
7805 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
7806 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
7821 static void expr_prod(void)
7826 while (tok
== '*' || tok
== '/' || tok
== '%') {
7834 static void expr_sum(void)
7839 while (tok
== '+' || tok
== '-') {
7847 static void expr_shift(void)
7852 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
7860 static void expr_cmp(void)
7865 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
7866 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
7874 static void expr_cmpeq(void)
7879 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
7887 static void expr_and(void)
7890 while (tok
== '&') {
7897 static void expr_xor(void)
7900 while (tok
== '^') {
7907 static void expr_or(void)
7910 while (tok
== '|') {
7917 /* XXX: fix this mess */
7918 static void expr_land_const(void)
7921 while (tok
== TOK_LAND
) {
7928 /* XXX: fix this mess */
7929 static void expr_lor_const(void)
7932 while (tok
== TOK_LOR
) {
7939 /* only used if non constant */
7940 static void expr_land(void)
7945 if (tok
== TOK_LAND
) {
7950 if (tok
!= TOK_LAND
) {
7960 static void expr_lor(void)
7965 if (tok
== TOK_LOR
) {
7970 if (tok
!= TOK_LOR
) {
7980 /* XXX: better constant handling */
7981 static void expr_eq(void)
7983 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
7985 CType type
, type1
, type2
;
7992 boolean
.t
= VT_BOOL
;
7998 if (tok
!= ':' || !gnu_ext
) {
8013 if (vtop
!= vstack
) {
8014 /* needed to avoid having different registers saved in
8016 if (is_float(vtop
->type
.t
))
8023 if (tok
== ':' && gnu_ext
) {
8031 sv
= *vtop
; /* save value to handle it later */
8032 vtop
--; /* no vpop so that FP stack is not flushed */
8040 bt1
= t1
& VT_BTYPE
;
8042 bt2
= t2
& VT_BTYPE
;
8043 /* cast operands to correct type according to ISOC rules */
8044 if (is_float(bt1
) || is_float(bt2
)) {
8045 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
8046 type
.t
= VT_LDOUBLE
;
8047 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
8052 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
8053 /* cast to biggest op */
8055 /* convert to unsigned if it does not fit in a long long */
8056 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
8057 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
8058 type
.t
|= VT_UNSIGNED
;
8059 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
8060 /* XXX: test pointer compatibility */
8062 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
8063 /* XXX: test function pointer compatibility */
8065 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
8066 /* XXX: test structure compatibility */
8068 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
8069 /* NOTE: as an extension, we accept void on only one side */
8072 /* integer operations */
8074 /* convert to unsigned if it does not fit in an integer */
8075 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
8076 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
8077 type
.t
|= VT_UNSIGNED
;
8080 /* now we convert second operand */
8082 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8085 if (is_float(type
.t
)) {
8087 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
8088 /* for long longs, we use fixed registers to avoid having
8089 to handle a complicated move */
8094 /* this is horrible, but we must also convert first
8098 /* put again first value and cast it */
8101 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8111 static void gexpr(void)
8122 /* parse an expression and return its type without any side effect. */
8123 static void expr_type(CType
*type
)
8125 int saved_nocode_wanted
;
8127 saved_nocode_wanted
= nocode_wanted
;
8132 nocode_wanted
= saved_nocode_wanted
;
8135 /* parse a unary expression and return its type without any side
8137 static void unary_type(CType
*type
)
8149 /* parse a constant expression and return value in vtop. */
8150 static void expr_const1(void)
8159 /* parse an integer constant and return its value. */
8160 static int expr_const(void)
8164 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
8165 expect("constant expression");
8171 /* return the label token if current token is a label, otherwise
8173 static int is_label(void)
8177 /* fast test first */
8178 if (tok
< TOK_UIDENT
)
8180 /* no need to save tokc because tok is an identifier */
8187 unget_tok(last_tok
);
8192 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
8193 int case_reg
, int is_expr
)
8198 /* generate line number info */
8200 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
8201 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
8203 last_line_num
= file
->line_num
;
8207 /* default return value is (void) */
8209 vtop
->type
.t
= VT_VOID
;
8212 if (tok
== TOK_IF
) {
8219 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8221 if (c
== TOK_ELSE
) {
8225 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8226 gsym(d
); /* patch else jmp */
8229 } else if (tok
== TOK_WHILE
) {
8237 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8241 } else if (tok
== '{') {
8245 /* record local declaration stack position */
8247 llabel
= local_label_stack
;
8248 /* handle local labels declarations */
8249 if (tok
== TOK_LABEL
) {
8252 if (tok
< TOK_UIDENT
)
8253 expect("label identifier");
8254 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
8264 while (tok
!= '}') {
8269 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8272 /* pop locally defined labels */
8273 label_pop(&local_label_stack
, llabel
);
8274 /* pop locally defined symbols */
8276 /* XXX: this solution makes only valgrind happy...
8277 triggered by gcc.c-torture/execute/20000917-1.c */
8279 switch(vtop
->type
.t
& VT_BTYPE
) {
8284 for(p
=vtop
->type
.ref
;p
;p
=p
->prev
)
8286 error("unsupported expression type");
8289 sym_pop(&local_stack
, s
);
8291 } else if (tok
== TOK_RETURN
) {
8295 gen_assign_cast(&func_vt
);
8296 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
8298 /* if returning structure, must copy it to implicit
8299 first pointer arg location */
8302 size
= type_size(&func_vt
,&align
);
8305 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
8309 loc
= (loc
- size
) & -4;
8312 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
8315 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
8317 vtop
->type
= int_type
;
8323 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
8326 /* copy structure value to pointer */
8331 } else if (is_float(func_vt
.t
)) {
8336 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
8339 rsym
= gjmp(rsym
); /* jmp */
8340 } else if (tok
== TOK_BREAK
) {
8343 error("cannot break");
8344 *bsym
= gjmp(*bsym
);
8347 } else if (tok
== TOK_CONTINUE
) {
8350 error("cannot continue");
8351 *csym
= gjmp(*csym
);
8354 } else if (tok
== TOK_FOR
) {
8381 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8386 if (tok
== TOK_DO
) {
8391 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8402 if (tok
== TOK_SWITCH
) {
8406 /* XXX: other types than integer */
8407 case_reg
= gv(RC_INT
);
8411 b
= gjmp(0); /* jump to first case */
8413 block(&a
, csym
, &b
, &c
, case_reg
, 0);
8414 /* if no default, jmp after switch */
8422 if (tok
== TOK_CASE
) {
8429 if (gnu_ext
&& tok
== TOK_DOTS
) {
8433 warning("empty case range");
8435 /* since a case is like a label, we must skip it with a jmp */
8442 *case_sym
= gtst(1, 0);
8445 *case_sym
= gtst(1, 0);
8449 *case_sym
= gtst(1, *case_sym
);
8454 goto block_after_label
;
8456 if (tok
== TOK_DEFAULT
) {
8462 error("too many 'default'");
8465 goto block_after_label
;
8467 if (tok
== TOK_GOTO
) {
8469 if (tok
== '*' && gnu_ext
) {
8473 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
8476 } else if (tok
>= TOK_UIDENT
) {
8477 s
= label_find(tok
);
8478 /* put forward definition if needed */
8480 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
8482 if (s
->r
== LABEL_DECLARED
)
8483 s
->r
= LABEL_FORWARD
;
8485 /* label already defined */
8486 if (s
->r
& LABEL_FORWARD
)
8487 s
->next
= (void *)gjmp((long)s
->next
);
8489 gjmp_addr((long)s
->next
);
8492 expect("label identifier");
8495 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
8503 if (s
->r
== LABEL_DEFINED
)
8504 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
8505 gsym((long)s
->next
);
8506 s
->r
= LABEL_DEFINED
;
8508 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
8510 s
->next
= (void *)ind
;
8511 /* we accept this, but it is a mistake */
8514 warning("deprecated use of label at end of compound statement");
8518 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8521 /* expression case */
8536 /* t is the array or struct type. c is the array or struct
8537 address. cur_index/cur_field is the pointer to the current
8538 value. 'size_only' is true if only size info is needed (only used
8540 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
8541 int *cur_index
, Sym
**cur_field
,
8545 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
8551 if (gnu_ext
&& (l
= is_label()) != 0)
8553 while (tok
== '[' || tok
== '.') {
8555 if (!(type
->t
& VT_ARRAY
))
8556 expect("array type");
8559 index
= expr_const();
8560 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
8561 expect("invalid index");
8562 if (tok
== TOK_DOTS
&& gnu_ext
) {
8564 index_last
= expr_const();
8565 if (index_last
< 0 ||
8566 (s
->c
>= 0 && index_last
>= s
->c
) ||
8568 expect("invalid index");
8574 *cur_index
= index_last
;
8575 type
= pointed_type(type
);
8576 elem_size
= type_size(type
, &align
);
8577 c
+= index
* elem_size
;
8578 /* NOTE: we only support ranges for last designator */
8579 nb_elems
= index_last
- index
+ 1;
8580 if (nb_elems
!= 1) {
8589 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
8590 expect("struct/union type");
8603 /* XXX: fix this mess by using explicit storage field */
8605 type1
.t
|= (type
->t
& ~VT_TYPE
);
8619 if (type
->t
& VT_ARRAY
) {
8621 type
= pointed_type(type
);
8622 c
+= index
* type_size(type
, &align
);
8626 error("too many field init");
8627 /* XXX: fix this mess by using explicit storage field */
8629 type1
.t
|= (type
->t
& ~VT_TYPE
);
8634 decl_initializer(type
, sec
, c
, 0, size_only
);
8636 /* XXX: make it more general */
8637 if (!size_only
&& nb_elems
> 1) {
8638 unsigned long c_end
;
8643 error("range init not supported yet for dynamic storage");
8644 c_end
= c
+ nb_elems
* elem_size
;
8645 if (c_end
> sec
->data_allocated
)
8646 section_realloc(sec
, c_end
);
8647 src
= sec
->data
+ c
;
8649 for(i
= 1; i
< nb_elems
; i
++) {
8651 memcpy(dst
, src
, elem_size
);
8657 #define EXPR_CONST 1
8660 /* store a value or an expression directly in global data or in local array */
8661 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
8662 int v
, int expr_type
)
8664 int saved_global_expr
, bt
, bit_pos
, bit_size
;
8666 unsigned long long bit_mask
;
8674 /* compound literals must be allocated globally in this case */
8675 saved_global_expr
= global_expr
;
8678 global_expr
= saved_global_expr
;
8679 /* NOTE: symbols are accepted */
8680 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
8681 error("initializer element is not constant");
8689 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
8692 /* XXX: not portable */
8693 /* XXX: generate error if incorrect relocation */
8694 gen_assign_cast(&dtype
);
8695 bt
= type
->t
& VT_BTYPE
;
8696 ptr
= sec
->data
+ c
;
8697 /* XXX: make code faster ? */
8698 if (!(type
->t
& VT_BITFIELD
)) {
8703 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
8704 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
8705 bit_mask
= (1LL << bit_size
) - 1;
8707 if ((vtop
->r
& VT_SYM
) &&
8713 (bt
== VT_INT
&& bit_size
!= 32)))
8714 error("initializer element is not computable at load time");
8717 vtop
->c
.i
= (vtop
->c
.i
!= 0);
8719 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8722 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8725 *(double *)ptr
= vtop
->c
.d
;
8728 *(long double *)ptr
= vtop
->c
.ld
;
8731 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
8734 if (vtop
->r
& VT_SYM
) {
8735 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
8737 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8742 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
8749 /* put zeros for variable based init */
8750 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
8753 /* nothing to do because globals are already set to zero */
8755 vpush_global_sym(&func_old_type
, TOK_memset
);
8763 /* 't' contains the type and storage info. 'c' is the offset of the
8764 object in section 'sec'. If 'sec' is NULL, it means stack based
8765 allocation. 'first' is true if array '{' must be read (multi
8766 dimension implicit array init handling). 'size_only' is true if
8767 size only evaluation is wanted (only for arrays). */
8768 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
8769 int first
, int size_only
)
8771 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
8772 int size1
, align1
, expr_type
;
8776 if (type
->t
& VT_ARRAY
) {
8780 t1
= pointed_type(type
);
8781 size1
= type_size(t1
, &align1
);
8784 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
8790 /* only parse strings here if correct type (otherwise: handle
8791 them as ((w)char *) expressions */
8792 if ((tok
== TOK_LSTR
&&
8793 #ifdef TCC_TARGET_PE
8794 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
8796 (t1
->t
& VT_BTYPE
) == VT_INT
8798 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
8799 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8804 /* compute maximum number of chars wanted */
8806 cstr_len
= cstr
->size
;
8808 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
8811 if (n
>= 0 && nb
> (n
- array_length
))
8812 nb
= n
- array_length
;
8815 warning("initializer-string for array is too long");
8816 /* in order to go faster for common case (char
8817 string in global variable, we handle it
8819 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
8820 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
8824 ch
= ((unsigned char *)cstr
->data
)[i
];
8826 ch
= ((nwchar_t
*)cstr
->data
)[i
];
8827 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
8835 /* only add trailing zero if enough storage (no
8836 warning in this case since it is standard) */
8837 if (n
< 0 || array_length
< n
) {
8839 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
8845 while (tok
!= '}') {
8846 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
8847 if (n
>= 0 && index
>= n
)
8848 error("index too large");
8849 /* must put zero in holes (note that doing it that way
8850 ensures that it even works with designators) */
8851 if (!size_only
&& array_length
< index
) {
8852 init_putz(t1
, sec
, c
+ array_length
* size1
,
8853 (index
- array_length
) * size1
);
8856 if (index
> array_length
)
8857 array_length
= index
;
8858 /* special test for multi dimensional arrays (may not
8859 be strictly correct if designators are used at the
8861 if (index
>= n
&& no_oblock
)
8870 /* put zeros at the end */
8871 if (!size_only
&& n
>= 0 && array_length
< n
) {
8872 init_putz(t1
, sec
, c
+ array_length
* size1
,
8873 (n
- array_length
) * size1
);
8875 /* patch type size if needed */
8877 s
->c
= array_length
;
8878 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
8879 (sec
|| !first
|| tok
== '{')) {
8882 /* NOTE: the previous test is a specific case for automatic
8883 struct/union init */
8884 /* XXX: union needs only one init */
8886 /* XXX: this test is incorrect for local initializers
8887 beginning with ( without {. It would be much more difficult
8888 to do it correctly (ideally, the expression parser should
8889 be used in all cases) */
8895 while (tok
== '(') {
8899 if (!parse_btype(&type1
, &ad1
))
8901 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
8903 if (!is_assignable_types(type
, &type1
))
8904 error("invalid type for cast");
8909 if (first
|| tok
== '{') {
8918 while (tok
!= '}') {
8919 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
8921 if (!size_only
&& array_length
< index
) {
8922 init_putz(type
, sec
, c
+ array_length
,
8923 index
- array_length
);
8925 index
= index
+ type_size(&f
->type
, &align1
);
8926 if (index
> array_length
)
8927 array_length
= index
;
8929 if (no_oblock
&& f
== NULL
)
8935 /* put zeros at the end */
8936 if (!size_only
&& array_length
< n
) {
8937 init_putz(type
, sec
, c
+ array_length
,
8946 } else if (tok
== '{') {
8948 decl_initializer(type
, sec
, c
, first
, size_only
);
8950 } else if (size_only
) {
8951 /* just skip expression */
8953 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
8957 else if (tok
== ')')
8962 /* currently, we always use constant expression for globals
8963 (may change for scripting case) */
8964 expr_type
= EXPR_CONST
;
8966 expr_type
= EXPR_ANY
;
8967 init_putv(type
, sec
, c
, 0, expr_type
);
8971 /* parse an initializer for type 't' if 'has_init' is non zero, and
8972 allocate space in local or global data space ('r' is either
8973 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8974 variable 'v' of scope 'scope' is declared before initializers are
8975 parsed. If 'v' is zero, then a reference to the new object is put
8976 in the value stack. If 'has_init' is 2, a special parsing is done
8977 to handle string constants. */
8978 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
8979 int has_init
, int v
, int scope
)
8981 int size
, align
, addr
, data_offset
;
8983 ParseState saved_parse_state
;
8984 TokenString init_str
;
8987 size
= type_size(type
, &align
);
8988 /* If unknown size, we must evaluate it before
8989 evaluating initializers because
8990 initializers can generate global data too
8991 (e.g. string pointers or ISOC99 compound
8992 literals). It also simplifies local
8993 initializers handling */
8994 tok_str_new(&init_str
);
8997 error("unknown type size");
8998 /* get all init string */
8999 if (has_init
== 2) {
9000 /* only get strings */
9001 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
9002 tok_str_add_tok(&init_str
);
9007 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
9009 error("unexpected end of file in initializer");
9010 tok_str_add_tok(&init_str
);
9013 else if (tok
== '}') {
9021 tok_str_add(&init_str
, -1);
9022 tok_str_add(&init_str
, 0);
9025 save_parse_state(&saved_parse_state
);
9027 macro_ptr
= init_str
.str
;
9029 decl_initializer(type
, NULL
, 0, 1, 1);
9030 /* prepare second initializer parsing */
9031 macro_ptr
= init_str
.str
;
9034 /* if still unknown size, error */
9035 size
= type_size(type
, &align
);
9037 error("unknown type size");
9039 /* take into account specified alignment if bigger */
9041 if (ad
->aligned
> align
)
9042 align
= ad
->aligned
;
9043 } else if (ad
->packed
) {
9046 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
9048 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
9050 loc
= (loc
- size
) & -align
;
9052 /* handles bounds */
9053 /* XXX: currently, since we do only one pass, we cannot track
9054 '&' operators, so we add only arrays */
9055 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
9056 unsigned long *bounds_ptr
;
9057 /* add padding between regions */
9059 /* then add local bound info */
9060 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
9061 bounds_ptr
[0] = addr
;
9062 bounds_ptr
[1] = size
;
9065 /* local variable */
9066 sym_push(v
, type
, r
, addr
);
9068 /* push local reference */
9069 vset(type
, r
, addr
);
9075 if (v
&& scope
== VT_CONST
) {
9076 /* see if the symbol was already defined */
9079 if (!is_compatible_types(&sym
->type
, type
))
9080 error("incompatible types for redefinition of '%s'",
9081 get_tok_str(v
, NULL
));
9082 if (sym
->type
.t
& VT_EXTERN
) {
9083 /* if the variable is extern, it was not allocated */
9084 sym
->type
.t
&= ~VT_EXTERN
;
9085 /* set array size if it was ommited in extern
9087 if ((sym
->type
.t
& VT_ARRAY
) &&
9088 sym
->type
.ref
->c
< 0 &&
9090 sym
->type
.ref
->c
= type
->ref
->c
;
9092 /* we accept several definitions of the same
9093 global variable. this is tricky, because we
9094 must play with the SHN_COMMON type of the symbol */
9095 /* XXX: should check if the variable was already
9096 initialized. It is incorrect to initialized it
9098 /* no init data, we won't add more to the symbol */
9105 /* allocate symbol in corresponding section */
9110 else if (tcc_state
->nocommon
)
9114 data_offset
= sec
->data_offset
;
9115 data_offset
= (data_offset
+ align
- 1) & -align
;
9117 /* very important to increment global pointer at this time
9118 because initializers themselves can create new initializers */
9119 data_offset
+= size
;
9120 /* add padding if bound check */
9121 if (do_bounds_check
)
9123 sec
->data_offset
= data_offset
;
9124 /* allocate section space to put the data */
9125 if (sec
->sh_type
!= SHT_NOBITS
&&
9126 data_offset
> sec
->data_allocated
)
9127 section_realloc(sec
, data_offset
);
9128 /* align section if needed */
9129 if (align
> sec
->sh_addralign
)
9130 sec
->sh_addralign
= align
;
9132 addr
= 0; /* avoid warning */
9136 if (scope
!= VT_CONST
|| !sym
) {
9137 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
9139 /* update symbol definition */
9141 put_extern_sym(sym
, sec
, addr
, size
);
9144 /* put a common area */
9145 put_extern_sym(sym
, NULL
, align
, size
);
9146 /* XXX: find a nicer way */
9147 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
9148 esym
->st_shndx
= SHN_COMMON
;
9153 /* push global reference */
9154 sym
= get_sym_ref(type
, sec
, addr
, size
);
9156 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
9160 /* handles bounds now because the symbol must be defined
9161 before for the relocation */
9162 if (do_bounds_check
) {
9163 unsigned long *bounds_ptr
;
9165 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
9166 /* then add global bound info */
9167 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
9168 bounds_ptr
[0] = 0; /* relocated */
9169 bounds_ptr
[1] = size
;
9173 decl_initializer(type
, sec
, addr
, 1, 0);
9174 /* restore parse state if needed */
9176 tok_str_free(init_str
.str
);
9177 restore_parse_state(&saved_parse_state
);
9183 void put_func_debug(Sym
*sym
)
9188 /* XXX: we put here a dummy type */
9189 snprintf(buf
, sizeof(buf
), "%s:%c1",
9190 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
9191 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
9192 cur_text_section
, sym
->c
);
9193 /* //gr gdb wants a line at the function */
9194 put_stabn(N_SLINE
, 0, file
->line_num
, 0);
9199 /* parse an old style function declaration list */
9200 /* XXX: check multiple parameter */
9201 static void func_decl_list(Sym
*func_sym
)
9208 /* parse each declaration */
9209 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
9210 if (!parse_btype(&btype
, &ad
))
9211 expect("declaration list");
9212 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9213 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9215 /* we accept no variable after */
9219 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9220 /* find parameter in function parameter list */
9223 if ((s
->v
& ~SYM_FIELD
) == v
)
9227 error("declaration for parameter '%s' but no such parameter",
9228 get_tok_str(v
, NULL
));
9230 /* check that no storage specifier except 'register' was given */
9231 if (type
.t
& VT_STORAGE
)
9232 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
9233 convert_parameter_type(&type
);
9234 /* we can add the type (NOTE: it could be local to the function) */
9236 /* accept other parameters */
9247 /* parse a function defined by symbol 'sym' and generate its code in
9248 'cur_text_section' */
9249 static void gen_function(Sym
*sym
)
9251 int saved_nocode_wanted
= nocode_wanted
;
9253 ind
= cur_text_section
->data_offset
;
9254 /* NOTE: we patch the symbol size later */
9255 put_extern_sym(sym
, cur_text_section
, ind
, 0);
9256 funcname
= get_tok_str(sym
->v
, NULL
);
9258 /* put debug symbol */
9260 put_func_debug(sym
);
9261 /* push a dummy symbol to enable local sym storage */
9262 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
9263 gfunc_prolog(&sym
->type
);
9265 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
9268 cur_text_section
->data_offset
= ind
;
9269 label_pop(&global_label_stack
, NULL
);
9270 sym_pop(&local_stack
, NULL
); /* reset local stack */
9271 /* end of function */
9272 /* patch symbol size */
9273 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_size
=
9276 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
9278 /* It's better to crash than to generate wrong code */
9279 cur_text_section
= NULL
;
9280 funcname
= ""; /* for safety */
9281 func_vt
.t
= VT_VOID
; /* for safety */
9282 ind
= 0; /* for safety */
9283 nocode_wanted
= saved_nocode_wanted
;
9286 static void gen_inline_functions(void)
9290 int *str
, inline_generated
;
9292 /* iterate while inline function are referenced */
9294 inline_generated
= 0;
9295 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9297 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9298 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9299 (VT_STATIC
| VT_INLINE
) &&
9301 /* the function was used: generate its code and
9302 convert it to a normal function */
9303 str
= INLINE_DEF(sym
->r
);
9304 sym
->r
= VT_SYM
| VT_CONST
;
9305 sym
->type
.t
&= ~VT_INLINE
;
9309 cur_text_section
= text_section
;
9311 macro_ptr
= NULL
; /* fail safe */
9314 inline_generated
= 1;
9317 if (!inline_generated
)
9321 /* free all remaining inline function tokens */
9322 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9324 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9325 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9326 (VT_STATIC
| VT_INLINE
)) {
9327 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9328 if (sym
->r
== (VT_SYM
| VT_CONST
)) //gr beware!
9330 str
= INLINE_DEF(sym
->r
);
9332 sym
->r
= 0; /* fail safe */
9337 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9338 static void decl(int l
)
9346 if (!parse_btype(&btype
, &ad
)) {
9347 /* skip redundant ';' */
9348 /* XXX: find more elegant solution */
9353 if (l
== VT_CONST
&&
9354 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
9355 /* global asm block */
9359 /* special test for old K&R protos without explicit int
9360 type. Only accepted when defining global data */
9361 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
9365 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9366 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9368 /* we accept no variable after */
9372 while (1) { /* iterate thru each declaration */
9374 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9378 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
9379 printf("type = '%s'\n", buf
);
9382 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9383 /* if old style function prototype, we accept a
9386 if (sym
->c
== FUNC_OLD
)
9387 func_decl_list(sym
);
9392 error("cannot use local functions");
9393 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
9394 expect("function definition");
9396 /* reject abstract declarators in function definition */
9398 while ((sym
= sym
->next
) != NULL
)
9399 if (!(sym
->v
& ~SYM_FIELD
))
9400 expect("identifier");
9402 /* XXX: cannot do better now: convert extern line to static inline */
9403 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
9404 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
9408 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
9410 /* specific case: if not func_call defined, we put
9411 the one of the prototype */
9412 /* XXX: should have default value */
9413 r
= sym
->type
.ref
->r
;
9414 if (FUNC_CALL(r
) != FUNC_CDECL
9415 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
9416 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
9418 FUNC_EXPORT(type
.ref
->r
) = 1;
9420 if (!is_compatible_types(&sym
->type
, &type
)) {
9422 error("incompatible types for redefinition of '%s'",
9423 get_tok_str(v
, NULL
));
9425 /* if symbol is already defined, then put complete type */
9428 /* put function symbol */
9429 sym
= global_identifier_push(v
, type
.t
, 0);
9430 sym
->type
.ref
= type
.ref
;
9433 /* static inline functions are just recorded as a kind
9434 of macro. Their code will be emitted at the end of
9435 the compilation unit only if they are used */
9436 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
9437 (VT_INLINE
| VT_STATIC
)) {
9438 TokenString func_str
;
9441 tok_str_new(&func_str
);
9447 error("unexpected end of file");
9448 tok_str_add_tok(&func_str
);
9453 } else if (t
== '}') {
9455 if (block_level
== 0)
9459 tok_str_add(&func_str
, -1);
9460 tok_str_add(&func_str
, 0);
9461 INLINE_DEF(sym
->r
) = func_str
.str
;
9463 /* compute text section */
9464 cur_text_section
= ad
.section
;
9465 if (!cur_text_section
)
9466 cur_text_section
= text_section
;
9467 sym
->r
= VT_SYM
| VT_CONST
;
9472 if (btype
.t
& VT_TYPEDEF
) {
9473 /* save typedefed type */
9474 /* XXX: test storage specifiers ? */
9475 sym
= sym_push(v
, &type
, 0, 0);
9476 sym
->type
.t
|= VT_TYPEDEF
;
9477 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9478 /* external function definition */
9479 /* specific case for func_call attribute */
9481 type
.ref
->r
= ad
.func_attr
;
9482 external_sym(v
, &type
, 0);
9484 /* not lvalue if array */
9486 if (!(type
.t
& VT_ARRAY
))
9487 r
|= lvalue_type(type
.t
);
9488 has_init
= (tok
== '=');
9489 if ((btype
.t
& VT_EXTERN
) ||
9490 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
9491 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
9492 /* external variable */
9493 /* NOTE: as GCC, uninitialized global static
9494 arrays of null size are considered as
9496 external_sym(v
, &type
, r
);
9498 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
9499 if (type
.t
& VT_STATIC
)
9505 decl_initializer_alloc(&type
, &ad
, r
,
9519 /* better than nothing, but needs extension to handle '-E' option
9521 static void preprocess_init(TCCState
*s1
)
9523 s1
->include_stack_ptr
= s1
->include_stack
;
9524 /* XXX: move that before to avoid having to initialize
9525 file->ifdef_stack_ptr ? */
9526 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
9527 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
9529 /* XXX: not ANSI compliant: bound checking says error */
9531 s1
->pack_stack
[0] = 0;
9532 s1
->pack_stack_ptr
= s1
->pack_stack
;
9535 /* compile the C file opened in 'file'. Return non zero if errors. */
9536 static int tcc_compile(TCCState
*s1
)
9540 volatile int section_sym
;
9543 printf("%s: **** new file\n", file
->filename
);
9545 preprocess_init(s1
);
9547 cur_text_section
= NULL
;
9549 anon_sym
= SYM_FIRST_ANOM
;
9551 /* file info: full path + filename */
9552 section_sym
= 0; /* avoid warning */
9554 section_sym
= put_elf_sym(symtab_section
, 0, 0,
9555 ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
), 0,
9556 text_section
->sh_num
, NULL
);
9557 getcwd(buf
, sizeof(buf
));
9559 normalize_slashes(buf
);
9561 pstrcat(buf
, sizeof(buf
), "/");
9562 put_stabs_r(buf
, N_SO
, 0, 0,
9563 text_section
->data_offset
, text_section
, section_sym
);
9564 put_stabs_r(file
->filename
, N_SO
, 0, 0,
9565 text_section
->data_offset
, text_section
, section_sym
);
9567 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9568 symbols can be safely used */
9569 put_elf_sym(symtab_section
, 0, 0,
9570 ELF32_ST_INFO(STB_LOCAL
, STT_FILE
), 0,
9571 SHN_ABS
, file
->filename
);
9573 /* define some often used types */
9574 int_type
.t
= VT_INT
;
9576 char_pointer_type
.t
= VT_BYTE
;
9577 mk_pointer(&char_pointer_type
);
9579 func_old_type
.t
= VT_FUNC
;
9580 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
9582 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9583 float_type
.t
= VT_FLOAT
;
9584 double_type
.t
= VT_DOUBLE
;
9586 func_float_type
.t
= VT_FUNC
;
9587 func_float_type
.ref
= sym_push(SYM_FIELD
, &float_type
, FUNC_CDECL
, FUNC_OLD
);
9588 func_double_type
.t
= VT_FUNC
;
9589 func_double_type
.ref
= sym_push(SYM_FIELD
, &double_type
, FUNC_CDECL
, FUNC_OLD
);
9593 /* define 'void *alloca(unsigned int)' builtin function */
9598 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
9599 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
9602 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
9606 define_start
= define_stack
;
9609 if (setjmp(s1
->error_jmp_buf
) == 0) {
9611 s1
->error_set_jmp_enabled
= 1;
9613 ch
= file
->buf_ptr
[0];
9614 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9615 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
;
9619 expect("declaration");
9621 /* end of translation unit info */
9623 put_stabs_r(NULL
, N_SO
, 0, 0,
9624 text_section
->data_offset
, text_section
, section_sym
);
9627 s1
->error_set_jmp_enabled
= 0;
9629 /* reset define stack, but leave -Dsymbols (may be incorrect if
9630 they are undefined) */
9631 free_defines(define_start
);
9633 gen_inline_functions();
9635 sym_pop(&global_stack
, NULL
);
9636 sym_pop(&local_stack
, NULL
);
9638 return s1
->nb_errors
!= 0 ? -1 : 0;
9641 /* Preprocess the current file */
9642 /* XXX: add line and file infos, add options to preserve spaces */
9643 static int tcc_preprocess(TCCState
*s1
)
9648 preprocess_init(s1
);
9650 define_start
= define_stack
;
9652 ch
= file
->buf_ptr
[0];
9653 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9654 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
9655 PARSE_FLAG_LINEFEED
;
9659 if (tok
== TOK_EOF
) {
9661 } else if (tok
== TOK_LINEFEED
) {
9665 fputc(' ', s1
->outfile
);
9668 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
9671 free_defines(define_start
);
9676 int tcc_compile_string(TCCState
*s
, const char *str
)
9678 BufferedFile bf1
, *bf
= &bf1
;
9682 /* init file structure */
9684 /* XXX: avoid copying */
9686 buf
= tcc_malloc(len
+ 1);
9689 memcpy(buf
, str
, len
);
9692 bf
->buf_end
= buf
+ len
;
9693 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
9696 ret
= tcc_compile(s
);
9700 /* currently, no need to close */
9705 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9706 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
9708 BufferedFile bf1
, *bf
= &bf1
;
9710 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
9711 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
9715 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
9717 /* init file structure */
9719 bf
->buf_ptr
= bf
->buffer
;
9720 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
9721 *bf
->buf_end
= CH_EOB
;
9722 bf
->filename
[0] = '\0';
9726 s1
->include_stack_ptr
= s1
->include_stack
;
9728 /* parse with define parser */
9729 ch
= file
->buf_ptr
[0];
9735 /* undefine a preprocessor symbol */
9736 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
9740 ts
= tok_alloc(sym
, strlen(sym
));
9741 s
= define_find(ts
->tok
);
9742 /* undefine symbol by putting an invalid name */
9747 #ifdef CONFIG_TCC_ASM
9749 #ifdef TCC_TARGET_I386
9750 #include "i386-asm.c"
9755 static void asm_instr(void)
9757 error("inline asm() not supported");
9759 static void asm_global_instr(void)
9761 error("inline asm() not supported");
9767 #ifdef TCC_TARGET_COFF
9768 #include "tcccoff.c"
9771 #ifdef TCC_TARGET_PE
9775 /* print the position in the source file of PC value 'pc' by reading
9776 the stabs debug information */
9777 static void rt_printline(unsigned long wanted_pc
)
9779 Stab_Sym
*sym
, *sym_end
;
9780 char func_name
[128], last_func_name
[128];
9781 unsigned long func_addr
, last_pc
, pc
;
9782 const char *incl_files
[INCLUDE_STACK_SIZE
];
9783 int incl_index
, len
, last_line_num
, i
;
9784 const char *str
, *p
;
9786 fprintf(stderr
, "0x%08lx:", wanted_pc
);
9788 func_name
[0] = '\0';
9791 last_func_name
[0] = '\0';
9792 last_pc
= 0xffffffff;
9794 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
9795 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
9796 while (sym
< sym_end
) {
9797 switch(sym
->n_type
) {
9798 /* function start or end */
9800 if (sym
->n_strx
== 0) {
9801 /* we test if between last line and end of function */
9802 pc
= sym
->n_value
+ func_addr
;
9803 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9805 func_name
[0] = '\0';
9808 str
= stabstr_section
->data
+ sym
->n_strx
;
9809 p
= strchr(str
, ':');
9811 pstrcpy(func_name
, sizeof(func_name
), str
);
9814 if (len
> sizeof(func_name
) - 1)
9815 len
= sizeof(func_name
) - 1;
9816 memcpy(func_name
, str
, len
);
9817 func_name
[len
] = '\0';
9819 func_addr
= sym
->n_value
;
9822 /* line number info */
9824 pc
= sym
->n_value
+ func_addr
;
9825 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9828 last_line_num
= sym
->n_desc
;
9830 strcpy(last_func_name
, func_name
);
9834 str
= stabstr_section
->data
+ sym
->n_strx
;
9836 if (incl_index
< INCLUDE_STACK_SIZE
) {
9837 incl_files
[incl_index
++] = str
;
9845 if (sym
->n_strx
== 0) {
9846 incl_index
= 0; /* end of translation unit */
9848 str
= stabstr_section
->data
+ sym
->n_strx
;
9849 /* do not add path */
9851 if (len
> 0 && str
[len
- 1] != '/')
9859 /* second pass: we try symtab symbols (no line number info) */
9862 Elf32_Sym
*sym
, *sym_end
;
9865 sym_end
= (Elf32_Sym
*)(symtab_section
->data
+ symtab_section
->data_offset
);
9866 for(sym
= (Elf32_Sym
*)symtab_section
->data
+ 1;
9869 type
= ELF32_ST_TYPE(sym
->st_info
);
9870 if (type
== STT_FUNC
) {
9871 if (wanted_pc
>= sym
->st_value
&&
9872 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
9873 pstrcpy(last_func_name
, sizeof(last_func_name
),
9874 strtab_section
->data
+ sym
->st_name
);
9880 /* did not find any info: */
9881 fprintf(stderr
, " ???\n");
9884 if (last_func_name
[0] != '\0') {
9885 fprintf(stderr
, " %s()", last_func_name
);
9887 if (incl_index
> 0) {
9888 fprintf(stderr
, " (%s:%d",
9889 incl_files
[incl_index
- 1], last_line_num
);
9890 for(i
= incl_index
- 2; i
>= 0; i
--)
9891 fprintf(stderr
, ", included from %s", incl_files
[i
]);
9892 fprintf(stderr
, ")");
9894 fprintf(stderr
, "\n");
9897 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
9901 /* fix for glibc 2.1 */
9907 /* return the PC at frame level 'level'. Return non zero if not found */
9908 static int rt_get_caller_pc(unsigned long *paddr
,
9909 ucontext_t
*uc
, int level
)
9915 #if defined(__FreeBSD__)
9916 *paddr
= uc
->uc_mcontext
.mc_eip
;
9917 #elif defined(__dietlibc__)
9918 *paddr
= uc
->uc_mcontext
.eip
;
9920 *paddr
= uc
->uc_mcontext
.gregs
[REG_EIP
];
9924 #if defined(__FreeBSD__)
9925 fp
= uc
->uc_mcontext
.mc_ebp
;
9926 #elif defined(__dietlibc__)
9927 fp
= uc
->uc_mcontext
.ebp
;
9929 fp
= uc
->uc_mcontext
.gregs
[REG_EBP
];
9931 for(i
=1;i
<level
;i
++) {
9932 /* XXX: check address validity with program info */
9933 if (fp
<= 0x1000 || fp
>= 0xc0000000)
9935 fp
= ((unsigned long *)fp
)[0];
9937 *paddr
= ((unsigned long *)fp
)[1];
9943 #warning add arch specific rt_get_caller_pc()
9945 static int rt_get_caller_pc(unsigned long *paddr
,
9946 ucontext_t
*uc
, int level
)
9952 /* emit a run time error at position 'pc' */
9953 void rt_error(ucontext_t
*uc
, const char *fmt
, ...)
9960 fprintf(stderr
, "Runtime error: ");
9961 vfprintf(stderr
, fmt
, ap
);
9962 fprintf(stderr
, "\n");
9963 for(i
=0;i
<num_callers
;i
++) {
9964 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
9967 fprintf(stderr
, "at ");
9969 fprintf(stderr
, "by ");
9976 /* signal handler for fatal errors */
9977 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
9979 ucontext_t
*uc
= puc
;
9983 switch(siginf
->si_code
) {
9986 rt_error(uc
, "division by zero");
9989 rt_error(uc
, "floating point exception");
9995 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
9996 rt_error(uc
, *rt_bound_error_msg
);
9998 rt_error(uc
, "dereferencing invalid pointer");
10001 rt_error(uc
, "illegal instruction");
10004 rt_error(uc
, "abort() called");
10007 rt_error(uc
, "caught signal %d", signum
);
10014 /* do all relocations (needed before using tcc_get_symbol()) */
10015 int tcc_relocate(TCCState
*s1
)
10022 #ifdef TCC_TARGET_PE
10023 pe_add_runtime(s1
);
10025 tcc_add_runtime(s1
);
10028 relocate_common_syms();
10030 tcc_add_linker_symbols(s1
);
10031 #ifndef TCC_TARGET_PE
10032 build_got_entries(s1
);
10034 /* compute relocation address : section are relocated in place. We
10035 also alloc the bss space */
10036 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10037 s
= s1
->sections
[i
];
10038 if (s
->sh_flags
& SHF_ALLOC
) {
10039 if (s
->sh_type
== SHT_NOBITS
)
10040 s
->data
= tcc_mallocz(s
->data_offset
);
10041 s
->sh_addr
= (unsigned long)s
->data
;
10045 relocate_syms(s1
, 1);
10047 if (s1
->nb_errors
!= 0)
10050 /* relocate each section */
10051 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10052 s
= s1
->sections
[i
];
10054 relocate_section(s1
, s
);
10057 /* mark executable sections as executable in memory */
10058 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10059 s
= s1
->sections
[i
];
10060 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_EXECINSTR
)) ==
10061 (SHF_ALLOC
| SHF_EXECINSTR
))
10062 set_pages_executable(s
->data
, s
->data_offset
);
10067 /* launch the compiled program with the given arguments */
10068 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
10070 int (*prog_main
)(int, char **);
10072 if (tcc_relocate(s1
) < 0)
10075 prog_main
= tcc_get_symbol_err(s1
, "main");
10078 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10079 error("debug mode currently not available for Windows");
10081 struct sigaction sigact
;
10082 /* install TCC signal handlers to print debug info on fatal
10084 sigact
.sa_flags
= SA_SIGINFO
| SA_RESETHAND
;
10085 sigact
.sa_sigaction
= sig_error
;
10086 sigemptyset(&sigact
.sa_mask
);
10087 sigaction(SIGFPE
, &sigact
, NULL
);
10088 sigaction(SIGILL
, &sigact
, NULL
);
10089 sigaction(SIGSEGV
, &sigact
, NULL
);
10090 sigaction(SIGBUS
, &sigact
, NULL
);
10091 sigaction(SIGABRT
, &sigact
, NULL
);
10095 #ifdef CONFIG_TCC_BCHECK
10096 if (do_bounds_check
) {
10097 void (*bound_init
)(void);
10099 /* set error function */
10100 rt_bound_error_msg
= (void *)tcc_get_symbol_err(s1
,
10101 "__bound_error_msg");
10103 /* XXX: use .init section so that it also work in binary ? */
10104 bound_init
= (void *)tcc_get_symbol_err(s1
, "__bound_init");
10108 return (*prog_main
)(argc
, argv
);
10111 void tcc_memstats(void)
10114 printf("memory in use: %d\n", mem_cur_size
);
10118 static void tcc_cleanup(void)
10122 if (NULL
== tcc_state
)
10126 /* free -D defines */
10127 free_defines(NULL
);
10130 n
= tok_ident
- TOK_IDENT
;
10131 for(i
= 0; i
< n
; i
++)
10132 tcc_free(table_ident
[i
]);
10133 tcc_free(table_ident
);
10135 /* free sym_pools */
10136 dynarray_reset(&sym_pools
, &nb_sym_pools
);
10137 /* string buffer */
10138 cstr_free(&tokcstr
);
10139 /* reset symbol stack */
10140 sym_free_first
= NULL
;
10141 /* cleanup from error/setjmp */
10145 TCCState
*tcc_new(void)
10154 s
= tcc_mallocz(sizeof(TCCState
));
10158 s
->output_type
= TCC_OUTPUT_MEMORY
;
10160 /* init isid table */
10161 for(i
=CH_EOF
;i
<256;i
++)
10162 isidnum_table
[i
-CH_EOF
] = isid(i
) || isnum(i
);
10164 /* add all tokens */
10165 table_ident
= NULL
;
10166 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
10168 tok_ident
= TOK_IDENT
;
10177 ts
= tok_alloc(p
, r
- p
- 1);
10181 /* we add dummy defines for some special macros to speed up tests
10182 and to have working defined() */
10183 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
10184 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
10185 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
10186 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
10188 /* standard defines */
10189 tcc_define_symbol(s
, "__STDC__", NULL
);
10190 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
10191 #if defined(TCC_TARGET_I386)
10192 tcc_define_symbol(s
, "__i386__", NULL
);
10194 #if defined(TCC_TARGET_ARM)
10195 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
10196 tcc_define_symbol(s
, "__arm_elf__", NULL
);
10197 tcc_define_symbol(s
, "__arm_elf", NULL
);
10198 tcc_define_symbol(s
, "arm_elf", NULL
);
10199 tcc_define_symbol(s
, "__arm__", NULL
);
10200 tcc_define_symbol(s
, "__arm", NULL
);
10201 tcc_define_symbol(s
, "arm", NULL
);
10202 tcc_define_symbol(s
, "__APCS_32__", NULL
);
10204 #ifdef TCC_TARGET_PE
10205 tcc_define_symbol(s
, "_WIN32", NULL
);
10207 tcc_define_symbol(s
, "__unix__", NULL
);
10208 tcc_define_symbol(s
, "__unix", NULL
);
10209 #if defined(__linux)
10210 tcc_define_symbol(s
, "__linux__", NULL
);
10211 tcc_define_symbol(s
, "__linux", NULL
);
10214 /* tiny C specific defines */
10215 tcc_define_symbol(s
, "__TINYC__", NULL
);
10217 /* tiny C & gcc defines */
10218 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
10219 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
10220 #ifdef TCC_TARGET_PE
10221 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
10223 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
10226 #ifndef TCC_TARGET_PE
10227 /* default library paths */
10228 tcc_add_library_path(s
, CONFIG_SYSROOT
"/usr/local/lib");
10229 tcc_add_library_path(s
, CONFIG_SYSROOT
"/usr/lib");
10230 tcc_add_library_path(s
, CONFIG_SYSROOT
"/lib");
10233 /* no section zero */
10234 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
10236 /* create standard sections */
10237 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
10238 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
10239 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
10241 /* symbols are always generated for linking stage */
10242 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
10244 ".hashtab", SHF_PRIVATE
);
10245 strtab_section
= symtab_section
->link
;
10247 /* private symbol table for dynamic symbols */
10248 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
10250 ".dynhashtab", SHF_PRIVATE
);
10251 s
->alacarte_link
= 1;
10253 #ifdef CHAR_IS_UNSIGNED
10254 s
->char_is_unsigned
= 1;
10256 #if defined(TCC_TARGET_PE) && 0
10257 /* XXX: currently the PE linker is not ready to support that */
10258 s
->leading_underscore
= 1;
10263 void tcc_delete(TCCState
*s1
)
10269 /* free all sections */
10270 free_section(s1
->dynsymtab_section
);
10272 for(i
= 1; i
< s1
->nb_sections
; i
++)
10273 free_section(s1
->sections
[i
]);
10274 tcc_free(s1
->sections
);
10276 /* free any loaded DLLs */
10277 for ( i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
10279 DLLReference
*ref
= s1
->loaded_dlls
[i
];
10281 dlclose(ref
->handle
);
10284 /* free loaded dlls array */
10285 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
10287 /* free library paths */
10288 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
10290 /* free include paths */
10291 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
10292 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
10293 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
10298 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
10302 pathname1
= tcc_strdup(pathname
);
10303 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
10307 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
10311 pathname1
= tcc_strdup(pathname
);
10312 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
10316 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
10321 BufferedFile
*saved_file
;
10323 /* find source file type with extension */
10324 ext
= tcc_fileextension(filename
);
10328 /* open the file */
10330 file
= tcc_open(s1
, filename
);
10332 if (flags
& AFF_PRINT_ERROR
) {
10333 error_noabort("file '%s' not found", filename
);
10339 if (flags
& AFF_PREPROCESS
) {
10340 ret
= tcc_preprocess(s1
);
10341 } else if (!ext
[0] || !strcmp(ext
, "c")) {
10342 /* C file assumed */
10343 ret
= tcc_compile(s1
);
10345 #ifdef CONFIG_TCC_ASM
10346 if (!strcmp(ext
, "S")) {
10347 /* preprocessed assembler */
10348 ret
= tcc_assemble(s1
, 1);
10349 } else if (!strcmp(ext
, "s")) {
10350 /* non preprocessed assembler */
10351 ret
= tcc_assemble(s1
, 0);
10354 #ifdef TCC_TARGET_PE
10355 if (!strcmp(ext
, "def")) {
10356 ret
= pe_load_def_file(s1
, file
->fd
);
10361 /* assume executable format: auto guess file type */
10362 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
10363 lseek(fd
, 0, SEEK_SET
);
10365 error_noabort("could not read header");
10367 } else if (ret
!= sizeof(ehdr
)) {
10368 goto try_load_script
;
10371 if (ehdr
.e_ident
[0] == ELFMAG0
&&
10372 ehdr
.e_ident
[1] == ELFMAG1
&&
10373 ehdr
.e_ident
[2] == ELFMAG2
&&
10374 ehdr
.e_ident
[3] == ELFMAG3
) {
10375 file
->line_num
= 0; /* do not display line number if error */
10376 if (ehdr
.e_type
== ET_REL
) {
10377 ret
= tcc_load_object_file(s1
, fd
, 0);
10378 } else if (ehdr
.e_type
== ET_DYN
) {
10379 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
10380 #ifdef TCC_TARGET_PE
10384 h
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
10391 ret
= tcc_load_dll(s1
, fd
, filename
,
10392 (flags
& AFF_REFERENCED_DLL
) != 0);
10395 error_noabort("unrecognized ELF file");
10398 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
10399 file
->line_num
= 0; /* do not display line number if error */
10400 ret
= tcc_load_archive(s1
, fd
);
10402 #ifdef TCC_TARGET_COFF
10403 if (*(uint16_t *)(&ehdr
) == COFF_C67_MAGIC
) {
10404 ret
= tcc_load_coff(s1
, fd
);
10407 #ifdef TCC_TARGET_PE
10408 if (pe_test_res_file(&ehdr
, ret
)) {
10409 ret
= pe_load_res_file(s1
, fd
);
10413 /* as GNU ld, consider it is an ld script if not recognized */
10415 ret
= tcc_load_ldscript(s1
);
10417 error_noabort("unrecognized file type");
10432 int tcc_add_file(TCCState
*s
, const char *filename
)
10434 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
10437 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
10441 pathname1
= tcc_strdup(pathname
);
10442 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
10446 /* find and load a dll. Return non zero if not found */
10447 /* XXX: add '-rpath' option support ? */
10448 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
10453 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10454 snprintf(buf
, sizeof(buf
), "%s/%s",
10455 s
->library_paths
[i
], filename
);
10456 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
10462 /* the library name is the same as the argument of the '-l' option */
10463 int tcc_add_library(TCCState
*s
, const char *libraryname
)
10468 /* first we look for the dynamic library if not static linking */
10469 if (!s
->static_link
) {
10470 #ifdef TCC_TARGET_PE
10471 snprintf(buf
, sizeof(buf
), "%s.def", libraryname
);
10473 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
10475 if (tcc_add_dll(s
, buf
, 0) == 0)
10479 /* then we look for the static library */
10480 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10481 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
10482 s
->library_paths
[i
], libraryname
);
10483 if (tcc_add_file_internal(s
, buf
, 0) == 0)
10489 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
10491 add_elf_sym(symtab_section
, val
, 0,
10492 ELF32_ST_INFO(STB_GLOBAL
, STT_NOTYPE
), 0,
10497 int tcc_set_output_type(TCCState
*s
, int output_type
)
10501 s
->output_type
= output_type
;
10503 if (!s
->nostdinc
) {
10504 /* default include paths */
10505 /* XXX: reverse order needed if -isystem support */
10506 #ifndef TCC_TARGET_PE
10507 tcc_add_sysinclude_path(s
, CONFIG_SYSROOT
"/usr/local/include");
10508 tcc_add_sysinclude_path(s
, CONFIG_SYSROOT
"/usr/include");
10510 snprintf(buf
, sizeof(buf
), "%s/include", tcc_lib_path
);
10511 tcc_add_sysinclude_path(s
, buf
);
10512 #ifdef TCC_TARGET_PE
10513 snprintf(buf
, sizeof(buf
), "%s/include/winapi", tcc_lib_path
);
10514 tcc_add_sysinclude_path(s
, buf
);
10518 /* if bound checking, then add corresponding sections */
10519 #ifdef CONFIG_TCC_BCHECK
10520 if (do_bounds_check
) {
10521 /* define symbol */
10522 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
10523 /* create bounds sections */
10524 bounds_section
= new_section(s
, ".bounds",
10525 SHT_PROGBITS
, SHF_ALLOC
);
10526 lbounds_section
= new_section(s
, ".lbounds",
10527 SHT_PROGBITS
, SHF_ALLOC
);
10531 if (s
->char_is_unsigned
) {
10532 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
10535 /* add debug sections */
10538 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
10539 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
10540 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
10541 put_elf_str(stabstr_section
, "");
10542 stab_section
->link
= stabstr_section
;
10543 /* put first entry */
10544 put_stabs("", 0, 0, 0, 0);
10547 /* add libc crt1/crti objects */
10548 #ifndef TCC_TARGET_PE
10549 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
10551 if (output_type
!= TCC_OUTPUT_DLL
)
10552 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
10553 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
10557 #ifdef TCC_TARGET_PE
10558 snprintf(buf
, sizeof(buf
), "%s/lib", tcc_lib_path
);
10559 tcc_add_library_path(s
, buf
);
10565 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10566 #define FD_INVERT 0x0002 /* invert value before storing */
10568 typedef struct FlagDef
{
10574 static const FlagDef warning_defs
[] = {
10575 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
10576 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
10577 { offsetof(TCCState
, warn_error
), 0, "error" },
10578 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
10579 "implicit-function-declaration" },
10582 static int set_flag(TCCState
*s
, const FlagDef
*flags
, int nb_flags
,
10583 const char *name
, int value
)
10590 if (r
[0] == 'n' && r
[1] == 'o' && r
[2] == '-') {
10594 for(i
= 0, p
= flags
; i
< nb_flags
; i
++, p
++) {
10595 if (!strcmp(r
, p
->name
))
10600 if (p
->flags
& FD_INVERT
)
10602 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
10607 /* set/reset a warning */
10608 int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
10613 if (!strcmp(warning_name
, "all")) {
10614 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
10615 if (p
->flags
& WD_ALL
)
10616 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
10620 return set_flag(s
, warning_defs
, countof(warning_defs
),
10621 warning_name
, value
);
10625 static const FlagDef flag_defs
[] = {
10626 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
10627 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
10628 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
10629 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
10632 /* set/reset a flag */
10633 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
)
10635 return set_flag(s
, flag_defs
, countof(flag_defs
),
10639 #if !defined(LIBTCC)
10641 static int64_t getclock_us(void)
10646 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
10649 gettimeofday(&tv
, NULL
);
10650 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
10656 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10657 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10658 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10659 " [-static] [infile1 infile2...] [-run infile args...]\n"
10661 "General options:\n"
10662 " -v display current version, increase verbosity\n"
10663 " -c compile only - generate an object file\n"
10664 " -o outfile set output filename\n"
10665 " -Bdir set tcc internal library path\n"
10666 " -bench output compilation statistics\n"
10667 " -run run compiled source\n"
10668 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10669 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10670 " -w disable all warnings\n"
10671 "Preprocessor options:\n"
10672 " -E preprocess only\n"
10673 " -Idir add include path 'dir'\n"
10674 " -Dsym[=val] define 'sym' with value 'val'\n"
10675 " -Usym undefine 'sym'\n"
10676 "Linker options:\n"
10677 " -Ldir add library path 'dir'\n"
10678 " -llib link with dynamic or static library 'lib'\n"
10679 " -shared generate a shared library\n"
10680 " -soname set name for shared library to be used at runtime\n"
10681 " -static static linking\n"
10682 " -rdynamic export all global symbols to dynamic linker\n"
10683 " -r generate (relocatable) object file\n"
10684 "Debugger options:\n"
10685 " -g generate runtime debug info\n"
10686 #ifdef CONFIG_TCC_BCHECK
10687 " -b compile with built-in memory and bounds checker (implies -g)\n"
10689 " -bt N show N callers in stack traces\n"
10693 #define TCC_OPTION_HAS_ARG 0x0001
10694 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10696 typedef struct TCCOption
{
10725 TCC_OPTION_nostdinc
,
10726 TCC_OPTION_nostdlib
,
10727 TCC_OPTION_print_search_dirs
,
10728 TCC_OPTION_rdynamic
,
10736 static const TCCOption tcc_options
[] = {
10737 { "h", TCC_OPTION_HELP
, 0 },
10738 { "?", TCC_OPTION_HELP
, 0 },
10739 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
10740 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
10741 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
10742 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
10743 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
10744 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10745 { "bench", TCC_OPTION_bench
, 0 },
10746 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
10747 #ifdef CONFIG_TCC_BCHECK
10748 { "b", TCC_OPTION_b
, 0 },
10750 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10751 { "c", TCC_OPTION_c
, 0 },
10752 { "static", TCC_OPTION_static
, 0 },
10753 { "shared", TCC_OPTION_shared
, 0 },
10754 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
10755 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
10756 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10757 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
10758 { "r", TCC_OPTION_r
, 0 },
10759 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10760 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10761 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10762 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
10763 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10764 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
10765 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
10766 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
10767 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10768 { "w", TCC_OPTION_w
, 0 },
10769 { "pipe", TCC_OPTION_pipe
, 0},
10770 { "E", TCC_OPTION_E
, 0},
10774 /* convert 'str' into an array of space separated strings */
10775 static int expand_args(char ***pargv
, const char *str
)
10784 while (is_space(*str
))
10789 while (*str
!= '\0' && !is_space(*str
))
10792 arg
= tcc_malloc(len
+ 1);
10793 memcpy(arg
, s1
, len
);
10795 dynarray_add((void ***)&argv
, &argc
, arg
);
10801 static char **files
;
10802 static int nb_files
, nb_libraries
;
10803 static int multiple_files
;
10804 static int print_search_dirs
;
10805 static int output_type
;
10806 static int reloc_output
;
10807 static const char *outfile
;
10809 int parse_args(TCCState
*s
, int argc
, char **argv
)
10812 const TCCOption
*popt
;
10813 const char *optarg
, *p1
, *r1
;
10817 while (optind
< argc
) {
10819 r
= argv
[optind
++];
10820 if (r
[0] != '-' || r
[1] == '\0') {
10821 /* add a new file */
10822 dynarray_add((void ***)&files
, &nb_files
, r
);
10823 if (!multiple_files
) {
10825 /* argv[0] will be this file */
10829 /* find option in table (match only the first chars */
10830 popt
= tcc_options
;
10834 error("invalid option -- '%s'", r
);
10847 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
10848 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
10851 if (optind
>= argc
)
10852 error("argument to '%s' is missing", r
);
10853 optarg
= argv
[optind
++];
10861 switch(popt
->index
) {
10862 case TCC_OPTION_HELP
:
10866 if (tcc_add_include_path(s
, optarg
) < 0)
10867 error("too many include paths");
10872 sym
= (char *)optarg
;
10873 value
= strchr(sym
, '=');
10878 tcc_define_symbol(s
, sym
, value
);
10882 tcc_undefine_symbol(s
, optarg
);
10885 tcc_add_library_path(s
, optarg
);
10888 /* set tcc utilities path (mainly for tcc development) */
10889 tcc_lib_path
= optarg
;
10892 dynarray_add((void ***)&files
, &nb_files
, r
);
10895 case TCC_OPTION_bench
:
10898 case TCC_OPTION_bt
:
10899 num_callers
= atoi(optarg
);
10901 #ifdef CONFIG_TCC_BCHECK
10903 do_bounds_check
= 1;
10911 multiple_files
= 1;
10912 output_type
= TCC_OUTPUT_OBJ
;
10914 case TCC_OPTION_static
:
10915 s
->static_link
= 1;
10917 case TCC_OPTION_shared
:
10918 output_type
= TCC_OUTPUT_DLL
;
10920 case TCC_OPTION_soname
:
10921 s
->soname
= optarg
;
10924 multiple_files
= 1;
10928 /* generate a .o merging several output files */
10930 output_type
= TCC_OUTPUT_OBJ
;
10932 case TCC_OPTION_nostdinc
:
10935 case TCC_OPTION_nostdlib
:
10938 case TCC_OPTION_print_search_dirs
:
10939 print_search_dirs
= 1;
10941 case TCC_OPTION_run
:
10945 argc1
= expand_args(&argv1
, optarg
);
10947 parse_args(s
, argc1
, argv1
);
10949 multiple_files
= 0;
10950 output_type
= TCC_OUTPUT_MEMORY
;
10955 if (0 == verbose
++)
10956 printf("tcc version %s\n", TCC_VERSION
);
10957 } while (*optarg
++ == 'v');
10960 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
10961 goto unsupported_option
;
10964 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
10965 s
->warn_unsupported
)
10966 goto unsupported_option
;
10971 case TCC_OPTION_rdynamic
:
10974 case TCC_OPTION_Wl
:
10977 if (strstart(optarg
, "-Ttext,", &p
)) {
10978 s
->text_addr
= strtoul(p
, NULL
, 16);
10979 s
->has_text_addr
= 1;
10980 } else if (strstart(optarg
, "--oformat,", &p
)) {
10981 if (strstart(p
, "elf32-", NULL
)) {
10982 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
10983 } else if (!strcmp(p
, "binary")) {
10984 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
10986 #ifdef TCC_TARGET_COFF
10987 if (!strcmp(p
, "coff")) {
10988 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
10992 error("target %s not found", p
);
10995 error("unsupported linker option '%s'", optarg
);
11000 output_type
= TCC_OUTPUT_PREPROCESS
;
11003 if (s
->warn_unsupported
) {
11004 unsupported_option
:
11005 warning("unsupported option '%s'", r
);
11014 int main(int argc
, char **argv
)
11018 int nb_objfiles
, ret
, optind
;
11019 char objfilename
[1024];
11020 int64_t start_time
= 0;
11023 tcc_lib_path
= w32_tcc_lib_path();
11027 output_type
= TCC_OUTPUT_EXE
;
11029 multiple_files
= 1;
11034 print_search_dirs
= 0;
11037 optind
= parse_args(s
, argc
- 1, argv
+ 1);
11038 if (print_search_dirs
) {
11039 /* enough for Linux kernel */
11040 printf("install: %s/\n", tcc_lib_path
);
11043 if (optind
== 0 || nb_files
== 0) {
11044 if (optind
&& verbose
)
11050 nb_objfiles
= nb_files
- nb_libraries
;
11052 /* if outfile provided without other options, we output an
11054 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
11055 output_type
= TCC_OUTPUT_EXE
;
11057 /* check -c consistency : only single file handled. XXX: checks file type */
11058 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
11059 /* accepts only a single input file */
11060 if (nb_objfiles
!= 1)
11061 error("cannot specify multiple files with -c");
11062 if (nb_libraries
!= 0)
11063 error("cannot specify libraries with -c");
11067 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
11069 s
->outfile
= stdout
;
11071 s
->outfile
= fopen(outfile
, "w");
11073 error("could not open '%s", outfile
);
11075 } else if (output_type
!= TCC_OUTPUT_MEMORY
) {
11077 /* compute default outfile name */
11080 strcmp(files
[0], "-") == 0 ? "a" : tcc_basename(files
[0]);
11081 pstrcpy(objfilename
, sizeof(objfilename
), name
);
11082 ext
= tcc_fileextension(objfilename
);
11083 #ifdef TCC_TARGET_PE
11084 if (output_type
== TCC_OUTPUT_DLL
)
11085 strcpy(ext
, ".dll");
11087 if (output_type
== TCC_OUTPUT_EXE
)
11088 strcpy(ext
, ".exe");
11091 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
&& *ext
)
11094 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
11095 outfile
= objfilename
;
11100 start_time
= getclock_us();
11103 tcc_set_output_type(s
, output_type
);
11105 /* compile or add each files or library */
11106 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
11107 const char *filename
;
11109 filename
= files
[i
];
11110 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
11111 if (tcc_add_file_internal(s
, filename
,
11112 AFF_PRINT_ERROR
| AFF_PREPROCESS
) < 0)
11114 } else if (filename
[0] == '-' && filename
[1]) {
11115 if (tcc_add_library(s
, filename
+ 2) < 0)
11116 error("cannot find %s", filename
);
11119 printf("-> %s\n", filename
);
11120 if (tcc_add_file(s
, filename
) < 0)
11125 /* free all files */
11133 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
11134 if (total_time
< 0.001)
11135 total_time
= 0.001;
11136 if (total_bytes
< 1)
11138 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11139 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
11140 total_time
, (int)(total_lines
/ total_time
),
11141 total_bytes
/ total_time
/ 1000000.0);
11144 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
11146 fclose(s
->outfile
);
11147 } else if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
11148 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
11150 ret
= tcc_output_file(s
, outfile
) ? 1 : 0;
11152 /* XXX: cannot do it with bound checking because of the malloc hooks */
11153 if (!do_bounds_check
)
11158 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);