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 "/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
{
228 /* GNUC attribute definition */
229 typedef struct AttributeDef
{
233 int func_attr
; /* calling convention, exports, ... */
236 /* -------------------------------------------------- */
237 /* gr: wrappers for casting sym->r for other purposes */
245 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
246 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
247 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
248 #define INLINE_DEF(r) (*(int **)&(r))
249 /* -------------------------------------------------- */
251 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
252 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
253 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
255 /* stored in 'Sym.c' field */
256 #define FUNC_NEW 1 /* ansi function prototype */
257 #define FUNC_OLD 2 /* old function prototype */
258 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
260 /* stored in 'Sym.r' field */
261 #define FUNC_CDECL 0 /* standard c call */
262 #define FUNC_STDCALL 1 /* pascal c call */
263 #define FUNC_FASTCALL1 2 /* first param in %eax */
264 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
265 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
266 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
268 /* field 'Sym.t' for macros */
269 #define MACRO_OBJ 0 /* object like macro */
270 #define MACRO_FUNC 1 /* function like macro */
272 /* field 'Sym.r' for C labels */
273 #define LABEL_DEFINED 0 /* label is defined */
274 #define LABEL_FORWARD 1 /* label is forward defined */
275 #define LABEL_DECLARED 2 /* label is declared but never used */
277 /* type_decl() types */
278 #define TYPE_ABSTRACT 1 /* type without variable */
279 #define TYPE_DIRECT 2 /* type with variable */
281 #define IO_BUF_SIZE 8192
283 typedef struct BufferedFile
{
287 int line_num
; /* current line number - here to simplify code */
288 int ifndef_macro
; /* #ifndef macro / #endif search */
289 int ifndef_macro_saved
; /* saved ifndef_macro */
290 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
291 char inc_type
; /* type of include */
292 char inc_filename
[512]; /* filename specified by the user */
293 char filename
[1024]; /* current filename - here to simplify code */
294 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
297 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
298 #define CH_EOF (-1) /* end of file */
300 /* parsing state (used to save parser state to reparse part of the
301 source several times) */
302 typedef struct ParseState
{
309 /* used to record tokens */
310 typedef struct TokenString
{
317 /* include file cache, used to find files faster and also to eliminate
318 inclusion if the include file is protected by #ifndef ... #endif */
319 typedef struct CachedInclude
{
321 int hash_next
; /* -1 if none */
322 char type
; /* '"' or '>' to give include type */
323 char filename
[1]; /* path specified in #include */
326 #define CACHED_INCLUDES_HASH_SIZE 512
329 static struct BufferedFile
*file
;
332 static CString tokcstr
; /* current parsed string, if any */
333 /* additional informations about token */
334 static int tok_flags
;
335 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
336 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
337 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
338 #define TOK_FLAG_EOF 0x0008 /* end of file */
340 static int *macro_ptr
, *macro_ptr_allocated
;
341 static int *unget_saved_macro_ptr
;
342 static int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
343 static int unget_buffer_enabled
;
344 static int parse_flags
;
345 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
346 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
347 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
348 token. line feed is also
350 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
352 static Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
353 static Section
*cur_text_section
; /* current section where function code is
355 #ifdef CONFIG_TCC_ASM
356 static Section
*last_text_section
; /* to handle .previous asm directive */
358 /* bound check related sections */
359 static Section
*bounds_section
; /* contains global data bound description */
360 static Section
*lbounds_section
; /* contains local data bound description */
361 /* symbol sections */
362 static Section
*symtab_section
, *strtab_section
;
365 static Section
*stab_section
, *stabstr_section
;
367 /* loc : local variable index
368 ind : output code index
370 anon_sym: anonymous symbol index
372 static int rsym
, anon_sym
, ind
, loc
;
373 /* expression generation modifiers */
374 static int const_wanted
; /* true if constant wanted */
375 static int nocode_wanted
; /* true if no code generation wanted for an expression */
376 static int global_expr
; /* true if compound literals must be allocated
377 globally (used during initializers parsing */
378 static CType func_vt
; /* current function return type (used by return
381 static int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
382 static int tok_ident
;
383 static TokenSym
**table_ident
;
384 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
385 static char token_buf
[STRING_MAX_SIZE
+ 1];
386 static char *funcname
;
387 static Sym
*global_stack
, *local_stack
;
388 static Sym
*define_stack
;
389 static Sym
*global_label_stack
, *local_label_stack
;
390 /* symbol allocator */
391 #define SYM_POOL_NB (8192 / sizeof(Sym))
392 static Sym
*sym_free_first
;
394 static SValue vstack
[VSTACK_SIZE
], *vtop
;
395 /* some predefined types */
396 static CType char_pointer_type
, func_old_type
, int_type
;
397 /* true if isid(c) || isnum(c) */
398 static unsigned char isidnum_table
[256];
400 /* display some information during compilation */
401 static int verbose
= 0;
403 /* compile with debug symbol (and use them if error during execution) */
404 static int do_debug
= 0;
406 /* compile with built-in memory and bounds checker */
407 static int do_bounds_check
= 0;
409 /* display benchmark infos */
411 static int do_bench
= 0;
413 static int total_lines
;
414 static int total_bytes
;
416 /* use GNU C extensions */
417 static int gnu_ext
= 1;
419 /* use Tiny C extensions */
420 static int tcc_ext
= 1;
422 /* max number of callers shown if error */
423 static int num_callers
= 6;
424 static const char **rt_bound_error_msg
;
426 /* XXX: get rid of this ASAP */
427 static struct TCCState
*tcc_state
;
429 /* give the path of the tcc libraries */
430 static const char *tcc_lib_path
= CONFIG_TCCDIR
;
435 BufferedFile
**include_stack_ptr
;
436 int *ifdef_stack_ptr
;
438 /* include file handling */
439 char **include_paths
;
440 int nb_include_paths
;
441 char **sysinclude_paths
;
442 int nb_sysinclude_paths
;
443 CachedInclude
**cached_includes
;
444 int nb_cached_includes
;
446 char **library_paths
;
447 int nb_library_paths
;
449 /* array of all loaded dlls (including those referenced by loaded
451 DLLReference
**loaded_dlls
;
456 int nb_sections
; /* number of sections, including first dummy section */
461 unsigned long *got_offsets
;
463 /* give the correspondance from symtab indexes to dynsym indexes */
464 int *symtab_to_dynsym
;
466 /* temporary dynamic symbol sections (for dll loading) */
467 Section
*dynsymtab_section
;
468 /* exported dynamic symbol section */
471 int nostdinc
; /* if true, no standard headers are added */
472 int nostdlib
; /* if true, no standard libraries are added */
474 int nocommon
; /* if true, do not use common symbols for .bss data */
476 /* if true, static linking is performed */
479 /* soname as specified on the command line (-soname) */
482 /* if true, all symbols are exported */
485 /* if true, only link in referenced objects from archive */
488 /* address of text section */
489 unsigned long text_addr
;
492 /* output format, see TCC_OUTPUT_FORMAT_xxx */
495 /* C language options */
496 int char_is_unsigned
;
497 int leading_underscore
;
499 /* warning switches */
500 int warn_write_strings
;
501 int warn_unsupported
;
504 int warn_implicit_function_declaration
;
508 void (*error_func
)(void *opaque
, const char *msg
);
509 int error_set_jmp_enabled
;
510 jmp_buf error_jmp_buf
;
513 /* tiny assembler state */
516 /* see include_stack_ptr */
517 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
519 /* see ifdef_stack_ptr */
520 int ifdef_stack
[IFDEF_STACK_SIZE
];
522 /* see cached_includes */
523 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
526 int pack_stack
[PACK_STACK_SIZE
];
529 /* output file for preprocessing */
533 /* The current value can be: */
534 #define VT_VALMASK 0x00ff
535 #define VT_CONST 0x00f0 /* constant in vc
536 (must be first non register value) */
537 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
538 #define VT_LOCAL 0x00f2 /* offset on stack */
539 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
540 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
541 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
542 #define VT_LVAL 0x0100 /* var is an lvalue */
543 #define VT_SYM 0x0200 /* a symbol value is added */
544 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
545 char/short stored in integer registers) */
546 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
547 dereferencing value */
548 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
549 bounding function call point is in vc */
550 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
551 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
552 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
553 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
556 #define VT_INT 0 /* integer type */
557 #define VT_BYTE 1 /* signed byte type */
558 #define VT_SHORT 2 /* short type */
559 #define VT_VOID 3 /* void type */
560 #define VT_PTR 4 /* pointer */
561 #define VT_ENUM 5 /* enum definition */
562 #define VT_FUNC 6 /* function type */
563 #define VT_STRUCT 7 /* struct/union definition */
564 #define VT_FLOAT 8 /* IEEE float */
565 #define VT_DOUBLE 9 /* IEEE double */
566 #define VT_LDOUBLE 10 /* IEEE long double */
567 #define VT_BOOL 11 /* ISOC99 boolean type */
568 #define VT_LLONG 12 /* 64 bit integer */
569 #define VT_LONG 13 /* long integer (NEVER USED as type, only
571 #define VT_BTYPE 0x000f /* mask for basic type */
572 #define VT_UNSIGNED 0x0010 /* unsigned type */
573 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
574 #define VT_BITFIELD 0x0040 /* bitfield modifier */
575 #define VT_CONSTANT 0x0800 /* const modifier */
576 #define VT_VOLATILE 0x1000 /* volatile modifier */
577 #define VT_SIGNED 0x2000 /* signed type */
580 #define VT_EXTERN 0x00000080 /* extern definition */
581 #define VT_STATIC 0x00000100 /* static variable */
582 #define VT_TYPEDEF 0x00000200 /* typedef definition */
583 #define VT_INLINE 0x00000400 /* inline definition */
585 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
587 /* type mask (except storage) */
588 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
589 #define VT_TYPE (~(VT_STORAGE))
593 /* warning: the following compare tokens depend on i386 asm code */
600 #define TOK_Nset 0x98
601 #define TOK_Nclear 0x99
607 #define TOK_LAND 0xa0
611 #define TOK_MID 0xa3 /* inc/dec, to void constant */
613 #define TOK_UDIV 0xb0 /* unsigned division */
614 #define TOK_UMOD 0xb1 /* unsigned modulo */
615 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
616 #define TOK_CINT 0xb3 /* number in tokc */
617 #define TOK_CCHAR 0xb4 /* char constant in tokc */
618 #define TOK_STR 0xb5 /* pointer to string in tokc */
619 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
620 #define TOK_LCHAR 0xb7
621 #define TOK_LSTR 0xb8
622 #define TOK_CFLOAT 0xb9 /* float constant */
623 #define TOK_LINENUM 0xba /* line number info */
624 #define TOK_CDOUBLE 0xc0 /* double constant */
625 #define TOK_CLDOUBLE 0xc1 /* long double constant */
626 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
627 #define TOK_ADDC1 0xc3 /* add with carry generation */
628 #define TOK_ADDC2 0xc4 /* add with carry use */
629 #define TOK_SUBC1 0xc5 /* add with carry generation */
630 #define TOK_SUBC2 0xc6 /* add with carry use */
631 #define TOK_CUINT 0xc8 /* unsigned int constant */
632 #define TOK_CLLONG 0xc9 /* long long constant */
633 #define TOK_CULLONG 0xca /* unsigned long long constant */
634 #define TOK_ARROW 0xcb
635 #define TOK_DOTS 0xcc /* three dots */
636 #define TOK_SHR 0xcd /* unsigned shift right */
637 #define TOK_PPNUM 0xce /* preprocessor number */
639 #define TOK_SHL 0x01 /* shift left */
640 #define TOK_SAR 0x02 /* signed shift right */
642 /* assignement operators : normal operator or 0x80 */
643 #define TOK_A_MOD 0xa5
644 #define TOK_A_AND 0xa6
645 #define TOK_A_MUL 0xaa
646 #define TOK_A_ADD 0xab
647 #define TOK_A_SUB 0xad
648 #define TOK_A_DIV 0xaf
649 #define TOK_A_XOR 0xde
650 #define TOK_A_OR 0xfc
651 #define TOK_A_SHL 0x81
652 #define TOK_A_SAR 0x82
655 #define offsetof(type, field) ((size_t) &((type *)0)->field)
659 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
662 /* WARNING: the content of this string encodes token numbers */
663 static char tok_two_chars
[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
665 #define TOK_EOF (-1) /* end of file */
666 #define TOK_LINEFEED 10 /* line feed */
668 /* all identificators and strings have token above that */
669 #define TOK_IDENT 256
671 /* only used for i386 asm opcodes definitions */
672 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
675 DEF(TOK_ASM_ ## x ## b, #x "b") \
676 DEF(TOK_ASM_ ## x ## w, #x "w") \
677 DEF(TOK_ASM_ ## x ## l, #x "l") \
678 DEF(TOK_ASM_ ## x, #x)
681 DEF(TOK_ASM_ ## x ## w, #x "w") \
682 DEF(TOK_ASM_ ## x ## l, #x "l") \
683 DEF(TOK_ASM_ ## x, #x)
686 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
687 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
688 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
689 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
692 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
693 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
696 #define DEF_ASMTEST(x) \
728 #define TOK_ASM_int TOK_INT
731 TOK_LAST
= TOK_IDENT
- 1,
732 #define DEF(id, str) id,
737 static const char tcc_keywords
[] =
738 #define DEF(id, str) str "\0"
743 #define TOK_UIDENT TOK_DEFINE
746 #define snprintf _snprintf
747 #define vsnprintf _vsnprintf
749 #define strtold (long double)strtod
750 #define strtof (float)strtod
751 #define strtoll (long long)strtol
753 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
754 || defined(__OpenBSD__)
755 /* currently incorrect */
756 long double strtold(const char *nptr
, char **endptr
)
758 return (long double)strtod(nptr
, endptr
);
760 float strtof(const char *nptr
, char **endptr
)
762 return (float)strtod(nptr
, endptr
);
765 /* XXX: need to define this to use them in non ISOC99 context */
766 extern float strtof (const char *__nptr
, char **__endptr
);
767 extern long double strtold (const char *__nptr
, char **__endptr
);
770 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
771 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
772 static char *tcc_basename(const char *name
);
773 static char *tcc_fileextension (const char *p
);
775 static void next(void);
776 static void next_nomacro(void);
777 static void parse_expr_type(CType
*type
);
778 static void expr_type(CType
*type
);
779 static void unary_type(CType
*type
);
780 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
781 int case_reg
, int is_expr
);
782 static int expr_const(void);
783 static void expr_eq(void);
784 static void gexpr(void);
785 static void gen_inline_functions(void);
786 static void decl(int l
);
787 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
788 int first
, int size_only
);
789 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
790 int has_init
, int v
, int scope
);
792 void gv2(int rc1
, int rc2
);
793 void move_reg(int r
, int s
);
794 void save_regs(int n
);
795 void save_reg(int r
);
800 int get_reg_ex(int rc
,int rc2
);
803 struct macro_level
*prev
;
807 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
808 const int *macro_str
, struct macro_level
**can_read_stream
);
810 void force_charshort_cast(int t
);
811 static void gen_cast(CType
*type
);
813 static Sym
*sym_find(int v
);
814 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
817 static int type_size(CType
*type
, int *a
);
818 static inline CType
*pointed_type(CType
*type
);
819 static int pointed_size(CType
*type
);
820 static int lvalue_type(int t
);
821 static int parse_btype(CType
*type
, AttributeDef
*ad
);
822 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
823 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
);
824 static int is_compatible_types(CType
*type1
, CType
*type2
);
825 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
827 int ieee_finite(double d
);
828 void error(const char *fmt
, ...);
832 void lexpand_nr(void);
833 static void vpush_global_sym(CType
*type
, int v
);
834 void vset(CType
*type
, int r
, int v
);
835 void type_to_str(char *buf
, int buf_size
,
836 CType
*type
, const char *varstr
);
837 char *get_tok_str(int v
, CValue
*cv
);
838 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
839 unsigned long offset
, unsigned long size
);
840 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
842 /* section generation */
843 static void section_realloc(Section
*sec
, unsigned long new_size
);
844 static void *section_ptr_add(Section
*sec
, unsigned long size
);
845 static void put_extern_sym(Sym
*sym
, Section
*section
,
846 unsigned long value
, unsigned long size
);
847 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
848 static int put_elf_str(Section
*s
, const char *sym
);
849 static int put_elf_sym(Section
*s
,
850 unsigned long value
, unsigned long size
,
851 int info
, int other
, int shndx
, const char *name
);
852 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
853 int info
, int other
, int sh_num
, const char *name
);
854 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
855 int type
, int symbol
);
856 static void put_stabs(const char *str
, int type
, int other
, int desc
,
857 unsigned long value
);
858 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
859 unsigned long value
, Section
*sec
, int sym_index
);
860 static void put_stabn(int type
, int other
, int desc
, int value
);
861 static void put_stabd(int type
, int other
, int desc
);
862 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
864 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
865 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
866 #define AFF_PREPROCESS 0x0004 /* preprocess file */
867 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
870 int tcc_output_coff(TCCState
*s1
, FILE *f
);
873 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
);
874 int pe_load_def_file(struct TCCState
*s1
, int fd
);
875 int pe_test_res_file(void *v
, int size
);
876 int pe_load_res_file(struct TCCState
*s1
, int fd
);
877 void pe_add_runtime(struct TCCState
*s1
);
878 void pe_guess_outfile(char *objfilename
, int output_type
);
879 int pe_output_file(struct TCCState
*s1
, const char *filename
);
883 #ifdef CONFIG_TCC_ASM
885 typedef struct ExprValue
{
890 #define MAX_ASM_OPERANDS 30
892 typedef struct ASMOperand
{
893 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
895 char asm_str
[16]; /* computed asm string for operand */
896 SValue
*vt
; /* C value of the expression */
897 int ref_index
; /* if >= 0, gives reference to a output constraint */
898 int input_index
; /* if >= 0, gives reference to an input constraint */
899 int priority
; /* priority, used to assign registers */
900 int reg
; /* if >= 0, register number used for this operand */
901 int is_llong
; /* true if double register value */
902 int is_memory
; /* true if memory operand */
903 int is_rw
; /* for '+' modifier */
906 static void asm_expr(TCCState
*s1
, ExprValue
*pe
);
907 static int asm_int_expr(TCCState
*s1
);
908 static int find_constraint(ASMOperand
*operands
, int nb_operands
,
909 const char *name
, const char **pp
);
911 static int tcc_assemble(TCCState
*s1
, int do_preprocess
);
915 static void asm_instr(void);
916 static void asm_global_instr(void);
918 /* true if float/double/long double type */
919 static inline int is_float(int t
)
923 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
926 #ifdef TCC_TARGET_I386
927 #include "i386-gen.c"
930 #ifdef TCC_TARGET_ARM
934 #ifdef TCC_TARGET_C67
938 #ifdef CONFIG_TCC_STATIC
940 #define RTLD_LAZY 0x001
941 #define RTLD_NOW 0x002
942 #define RTLD_GLOBAL 0x100
943 #define RTLD_DEFAULT NULL
945 /* dummy function for profiling */
946 void *dlopen(const char *filename
, int flag
)
951 const char *dlerror(void)
956 typedef struct TCCSyms
{
961 #define TCCSYM(a) { #a, &a, },
963 /* add the symbol you want here if no dynamic linking is done */
964 static TCCSyms tcc_syms
[] = {
965 #if !defined(CONFIG_TCCBOOT)
974 void *resolve_sym(TCCState
*s1
, const char *symbol
, int type
)
978 while (p
->str
!= NULL
) {
979 if (!strcmp(p
->str
, symbol
))
986 #elif !defined(_WIN32)
990 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
)
992 return dlsym(RTLD_DEFAULT
, sym
);
997 /********************************************************/
999 /* we use our own 'finite' function to avoid potential problems with
1000 non standard math libs */
1001 /* XXX: endianness dependent */
1002 int ieee_finite(double d
)
1005 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
1008 /* copy a string and truncate it. */
1009 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
1016 q_end
= buf
+ buf_size
- 1;
1028 /* strcat and truncate. */
1029 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
1034 pstrcpy(buf
+ len
, buf_size
- len
, s
);
1039 static int strstart(const char *str
, const char *val
, const char **ptr
)
1044 while (*q
!= '\0') {
1056 /* extract the basename of a file */
1057 static char *tcc_basename(const char *name
)
1059 char *p
= strchr(name
, 0);
1070 static char *tcc_fileextension (const char *name
)
1072 char *b
= tcc_basename(name
);
1073 char *e
= strrchr(b
, '.');
1074 return e
? e
: strchr(b
, 0);
1078 char *normalize_slashes(char *path
)
1081 for (p
= path
; *p
; ++p
)
1087 char *w32_tcc_lib_path(void)
1089 /* on win32, we suppose the lib and includes are at the location
1091 char path
[1024], *p
;
1092 GetModuleFileNameA(NULL
, path
, sizeof path
);
1093 p
= tcc_basename(normalize_slashes(strlwr(path
)));
1094 if (p
- 5 > path
&& 0 == strncmp(p
- 5, "/bin/", 5))
1099 return strdup(path
);
1103 void set_pages_executable(void *ptr
, unsigned long length
)
1106 unsigned long old_protect
;
1107 VirtualProtect(ptr
, length
, PAGE_EXECUTE_READWRITE
, &old_protect
);
1109 unsigned long start
, end
;
1110 start
= (unsigned long)ptr
& ~(PAGESIZE
- 1);
1111 end
= (unsigned long)ptr
+ length
;
1112 end
= (end
+ PAGESIZE
- 1) & ~(PAGESIZE
- 1);
1113 mprotect((void *)start
, end
- start
, PROT_READ
| PROT_WRITE
| PROT_EXEC
);
1117 /* memory management */
1123 static inline void tcc_free(void *ptr
)
1126 mem_cur_size
-= malloc_usable_size(ptr
);
1131 static void *tcc_malloc(unsigned long size
)
1136 error("memory full");
1138 mem_cur_size
+= malloc_usable_size(ptr
);
1139 if (mem_cur_size
> mem_max_size
)
1140 mem_max_size
= mem_cur_size
;
1145 static void *tcc_mallocz(unsigned long size
)
1148 ptr
= tcc_malloc(size
);
1149 memset(ptr
, 0, size
);
1153 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
1157 mem_cur_size
-= malloc_usable_size(ptr
);
1159 ptr1
= realloc(ptr
, size
);
1161 /* NOTE: count not correct if alloc error, but not critical */
1162 mem_cur_size
+= malloc_usable_size(ptr1
);
1163 if (mem_cur_size
> mem_max_size
)
1164 mem_max_size
= mem_cur_size
;
1169 static char *tcc_strdup(const char *str
)
1172 ptr
= tcc_malloc(strlen(str
) + 1);
1177 #define free(p) use_tcc_free(p)
1178 #define malloc(s) use_tcc_malloc(s)
1179 #define realloc(p, s) use_tcc_realloc(p, s)
1181 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
1188 /* every power of two we double array size */
1189 if ((nb
& (nb
- 1)) == 0) {
1194 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
1196 error("memory full");
1203 static void dynarray_reset(void *pp
, int *n
)
1206 for (p
= *(void***)pp
; *n
; ++p
, --*n
)
1209 tcc_free(*(void**)pp
);
1213 /* symbol allocator */
1214 static Sym
*__sym_malloc(void)
1216 Sym
*sym_pool
, *sym
, *last_sym
;
1219 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
1221 last_sym
= sym_free_first
;
1223 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
1224 sym
->next
= last_sym
;
1228 sym_free_first
= last_sym
;
1232 static inline Sym
*sym_malloc(void)
1235 sym
= sym_free_first
;
1237 sym
= __sym_malloc();
1238 sym_free_first
= sym
->next
;
1242 static inline void sym_free(Sym
*sym
)
1244 sym
->next
= sym_free_first
;
1245 sym_free_first
= sym
;
1248 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
1252 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
1253 strcpy(sec
->name
, name
);
1254 sec
->sh_type
= sh_type
;
1255 sec
->sh_flags
= sh_flags
;
1262 sec
->sh_addralign
= 4;
1265 sec
->sh_addralign
= 1;
1268 sec
->sh_addralign
= 32; /* default conservative alignment */
1272 /* only add section if not private */
1273 if (!(sh_flags
& SHF_PRIVATE
)) {
1274 sec
->sh_num
= s1
->nb_sections
;
1275 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
1280 static void free_section(Section
*s
)
1286 /* realloc section and set its content to zero */
1287 static void section_realloc(Section
*sec
, unsigned long new_size
)
1290 unsigned char *data
;
1292 size
= sec
->data_allocated
;
1295 while (size
< new_size
)
1297 data
= tcc_realloc(sec
->data
, size
);
1299 error("memory full");
1300 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
1302 sec
->data_allocated
= size
;
1305 /* reserve at least 'size' bytes in section 'sec' from
1306 sec->data_offset. */
1307 static void *section_ptr_add(Section
*sec
, unsigned long size
)
1309 unsigned long offset
, offset1
;
1311 offset
= sec
->data_offset
;
1312 offset1
= offset
+ size
;
1313 if (offset1
> sec
->data_allocated
)
1314 section_realloc(sec
, offset1
);
1315 sec
->data_offset
= offset1
;
1316 return sec
->data
+ offset
;
1319 /* return a reference to a section, and create it if it does not
1321 Section
*find_section(TCCState
*s1
, const char *name
)
1325 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1326 sec
= s1
->sections
[i
];
1327 if (!strcmp(name
, sec
->name
))
1330 /* sections are created as PROGBITS */
1331 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
1334 #define SECTION_ABS ((void *)1)
1336 /* update sym->c so that it points to an external symbol in section
1337 'section' with value 'value' */
1338 static void put_extern_sym2(Sym
*sym
, Section
*section
,
1339 unsigned long value
, unsigned long size
,
1340 int can_add_underscore
)
1342 int sym_type
, sym_bind
, sh_num
, info
, other
, attr
;
1347 if (section
== NULL
)
1349 else if (section
== SECTION_ABS
)
1352 sh_num
= section
->sh_num
;
1356 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
) {
1357 sym_type
= STT_FUNC
;
1358 #ifdef TCC_TARGET_PE
1360 attr
= sym
->type
.ref
->r
;
1361 if (FUNC_EXPORT(attr
))
1363 if (FUNC_CALL(attr
) == FUNC_STDCALL
)
1367 sym_type
= STT_OBJECT
;
1370 if (sym
->type
.t
& VT_STATIC
)
1371 sym_bind
= STB_LOCAL
;
1373 sym_bind
= STB_GLOBAL
;
1376 name
= get_tok_str(sym
->v
, NULL
);
1377 #ifdef CONFIG_TCC_BCHECK
1378 if (do_bounds_check
) {
1381 /* XXX: avoid doing that for statics ? */
1382 /* if bound checking is activated, we change some function
1383 names by adding the "__bound" prefix */
1386 /* XXX: we rely only on malloc hooks */
1399 strcpy(buf
, "__bound_");
1407 #ifdef TCC_TARGET_PE
1408 if ((other
& 2) && can_add_underscore
) {
1409 sprintf(buf1
, "_%s@%d", name
, FUNC_ARGS(attr
));
1413 if (tcc_state
->leading_underscore
&& can_add_underscore
) {
1415 pstrcpy(buf1
+ 1, sizeof(buf1
) - 1, name
);
1418 info
= ELF32_ST_INFO(sym_bind
, sym_type
);
1419 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, other
, sh_num
, name
);
1421 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
1422 esym
->st_value
= value
;
1423 esym
->st_size
= size
;
1424 esym
->st_shndx
= sh_num
;
1425 esym
->st_other
|= other
;
1429 static void put_extern_sym(Sym
*sym
, Section
*section
,
1430 unsigned long value
, unsigned long size
)
1432 put_extern_sym2(sym
, section
, value
, size
, 1);
1435 /* add a new relocation entry to symbol 'sym' in section 's' */
1436 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
1439 put_extern_sym(sym
, NULL
, 0, 0);
1440 /* now we can add ELF relocation info */
1441 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
1444 static inline int isid(int c
)
1446 return (c
>= 'a' && c
<= 'z') ||
1447 (c
>= 'A' && c
<= 'Z') ||
1451 static inline int isnum(int c
)
1453 return c
>= '0' && c
<= '9';
1456 static inline int isoct(int c
)
1458 return c
>= '0' && c
<= '7';
1461 static inline int toup(int c
)
1463 if (c
>= 'a' && c
<= 'z')
1464 return c
- 'a' + 'A';
1469 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1473 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1476 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1480 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1484 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1491 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1492 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1493 (*f
)->filename
, (*f
)->line_num
);
1494 if (file
->line_num
> 0) {
1495 strcat_printf(buf
, sizeof(buf
),
1496 "%s:%d: ", file
->filename
, file
->line_num
);
1498 strcat_printf(buf
, sizeof(buf
),
1499 "%s: ", file
->filename
);
1502 strcat_printf(buf
, sizeof(buf
),
1506 strcat_printf(buf
, sizeof(buf
), "warning: ");
1507 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1509 if (!s1
->error_func
) {
1510 /* default case: stderr */
1511 fprintf(stderr
, "%s\n", buf
);
1513 s1
->error_func(s1
->error_opaque
, buf
);
1515 if (!is_warning
|| s1
->warn_error
)
1520 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1521 void (*error_func
)(void *opaque
, const char *msg
))
1523 s
->error_opaque
= error_opaque
;
1524 s
->error_func
= error_func
;
1528 /* error without aborting current compilation */
1529 void error_noabort(const char *fmt
, ...)
1531 TCCState
*s1
= tcc_state
;
1535 error1(s1
, 0, fmt
, ap
);
1539 void error(const char *fmt
, ...)
1541 TCCState
*s1
= tcc_state
;
1545 error1(s1
, 0, fmt
, ap
);
1547 /* better than nothing: in some cases, we accept to handle errors */
1548 if (s1
->error_set_jmp_enabled
) {
1549 longjmp(s1
->error_jmp_buf
, 1);
1551 /* XXX: eliminate this someday */
1556 void expect(const char *msg
)
1558 error("%s expected", msg
);
1561 void warning(const char *fmt
, ...)
1563 TCCState
*s1
= tcc_state
;
1570 error1(s1
, 1, fmt
, ap
);
1577 error("'%c' expected", c
);
1581 static void test_lvalue(void)
1583 if (!(vtop
->r
& VT_LVAL
))
1587 /* allocate a new token */
1588 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1590 TokenSym
*ts
, **ptable
;
1593 if (tok_ident
>= SYM_FIRST_ANOM
)
1594 error("memory full");
1596 /* expand token table if needed */
1597 i
= tok_ident
- TOK_IDENT
;
1598 if ((i
% TOK_ALLOC_INCR
) == 0) {
1599 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1601 error("memory full");
1602 table_ident
= ptable
;
1605 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1606 table_ident
[i
] = ts
;
1607 ts
->tok
= tok_ident
++;
1608 ts
->sym_define
= NULL
;
1609 ts
->sym_label
= NULL
;
1610 ts
->sym_struct
= NULL
;
1611 ts
->sym_identifier
= NULL
;
1613 ts
->hash_next
= NULL
;
1614 memcpy(ts
->str
, str
, len
);
1615 ts
->str
[len
] = '\0';
1620 #define TOK_HASH_INIT 1
1621 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1623 /* find a token and add it if not found */
1624 static TokenSym
*tok_alloc(const char *str
, int len
)
1626 TokenSym
*ts
, **pts
;
1632 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1633 h
&= (TOK_HASH_SIZE
- 1);
1635 pts
= &hash_ident
[h
];
1640 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1642 pts
= &(ts
->hash_next
);
1644 return tok_alloc_new(pts
, str
, len
);
1647 /* CString handling */
1649 static void cstr_realloc(CString
*cstr
, int new_size
)
1654 size
= cstr
->size_allocated
;
1656 size
= 8; /* no need to allocate a too small first string */
1657 while (size
< new_size
)
1659 data
= tcc_realloc(cstr
->data_allocated
, size
);
1661 error("memory full");
1662 cstr
->data_allocated
= data
;
1663 cstr
->size_allocated
= size
;
1668 static inline void cstr_ccat(CString
*cstr
, int ch
)
1671 size
= cstr
->size
+ 1;
1672 if (size
> cstr
->size_allocated
)
1673 cstr_realloc(cstr
, size
);
1674 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1678 static void cstr_cat(CString
*cstr
, const char *str
)
1690 /* add a wide char */
1691 static void cstr_wccat(CString
*cstr
, int ch
)
1694 size
= cstr
->size
+ sizeof(nwchar_t
);
1695 if (size
> cstr
->size_allocated
)
1696 cstr_realloc(cstr
, size
);
1697 *(nwchar_t
*)(((unsigned char *)cstr
->data
) + size
- sizeof(nwchar_t
)) = ch
;
1701 static void cstr_new(CString
*cstr
)
1703 memset(cstr
, 0, sizeof(CString
));
1706 /* free string and reset it to NULL */
1707 static void cstr_free(CString
*cstr
)
1709 tcc_free(cstr
->data_allocated
);
1713 #define cstr_reset(cstr) cstr_free(cstr)
1715 /* XXX: unicode ? */
1716 static void add_char(CString
*cstr
, int c
)
1718 if (c
== '\'' || c
== '\"' || c
== '\\') {
1719 /* XXX: could be more precise if char or string */
1720 cstr_ccat(cstr
, '\\');
1722 if (c
>= 32 && c
<= 126) {
1725 cstr_ccat(cstr
, '\\');
1727 cstr_ccat(cstr
, 'n');
1729 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1730 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1731 cstr_ccat(cstr
, '0' + (c
& 7));
1736 /* XXX: buffer overflow */
1737 /* XXX: float tokens */
1738 char *get_tok_str(int v
, CValue
*cv
)
1740 static char buf
[STRING_MAX_SIZE
+ 1];
1741 static CString cstr_buf
;
1747 /* NOTE: to go faster, we give a fixed buffer for small strings */
1748 cstr_reset(&cstr_buf
);
1749 cstr_buf
.data
= buf
;
1750 cstr_buf
.size_allocated
= sizeof(buf
);
1756 /* XXX: not quite exact, but only useful for testing */
1757 sprintf(p
, "%u", cv
->ui
);
1761 /* XXX: not quite exact, but only useful for testing */
1762 sprintf(p
, "%Lu", cv
->ull
);
1766 cstr_ccat(&cstr_buf
, '\'');
1767 add_char(&cstr_buf
, cv
->i
);
1768 cstr_ccat(&cstr_buf
, '\'');
1769 cstr_ccat(&cstr_buf
, '\0');
1773 len
= cstr
->size
- 1;
1775 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1776 cstr_ccat(&cstr_buf
, '\0');
1781 cstr_ccat(&cstr_buf
, '\"');
1783 len
= cstr
->size
- 1;
1785 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1787 len
= (cstr
->size
/ sizeof(nwchar_t
)) - 1;
1789 add_char(&cstr_buf
, ((nwchar_t
*)cstr
->data
)[i
]);
1791 cstr_ccat(&cstr_buf
, '\"');
1792 cstr_ccat(&cstr_buf
, '\0');
1801 return strcpy(p
, "...");
1803 return strcpy(p
, "<<=");
1805 return strcpy(p
, ">>=");
1807 if (v
< TOK_IDENT
) {
1808 /* search in two bytes table */
1822 } else if (v
< tok_ident
) {
1823 return table_ident
[v
- TOK_IDENT
]->str
;
1824 } else if (v
>= SYM_FIRST_ANOM
) {
1825 /* special name for anonymous symbol */
1826 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1828 /* should never happen */
1833 return cstr_buf
.data
;
1836 /* push, without hashing */
1837 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
1851 /* find a symbol and return its associated structure. 's' is the top
1852 of the symbol stack */
1853 static Sym
*sym_find2(Sym
*s
, int v
)
1863 /* structure lookup */
1864 static inline Sym
*struct_find(int v
)
1867 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1869 return table_ident
[v
]->sym_struct
;
1872 /* find an identifier */
1873 static inline Sym
*sym_find(int v
)
1876 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1878 return table_ident
[v
]->sym_identifier
;
1881 /* push a given symbol on the symbol stack */
1882 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1891 s
= sym_push2(ps
, v
, type
->t
, c
);
1892 s
->type
.ref
= type
->ref
;
1894 /* don't record fields or anonymous symbols */
1896 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1897 /* record symbol in token array */
1898 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1900 ps
= &ts
->sym_struct
;
1902 ps
= &ts
->sym_identifier
;
1909 /* push a global identifier */
1910 static Sym
*global_identifier_push(int v
, int t
, int c
)
1913 s
= sym_push2(&global_stack
, v
, t
, c
);
1914 /* don't record anonymous symbol */
1915 if (v
< SYM_FIRST_ANOM
) {
1916 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1917 /* modify the top most local identifier, so that
1918 sym_identifier will point to 's' when popped */
1920 ps
= &(*ps
)->prev_tok
;
1927 /* pop symbols until top reaches 'b' */
1928 static void sym_pop(Sym
**ptop
, Sym
*b
)
1938 /* remove symbol in token array */
1940 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1941 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1943 ps
= &ts
->sym_struct
;
1945 ps
= &ts
->sym_identifier
;
1956 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1961 if (strcmp(filename
, "-") == 0)
1962 fd
= 0, filename
= "stdin";
1964 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1965 if ((verbose
== 2 && fd
>= 0) || verbose
== 3)
1966 printf("%s %*s%s\n", fd
< 0 ? "nf":"->",
1967 (s1
->include_stack_ptr
- s1
->include_stack
), "", filename
);
1970 bf
= tcc_malloc(sizeof(BufferedFile
));
1972 bf
->buf_ptr
= bf
->buffer
;
1973 bf
->buf_end
= bf
->buffer
;
1974 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
1975 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
1977 normalize_slashes(bf
->filename
);
1980 bf
->ifndef_macro
= 0;
1981 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
1982 // printf("opening '%s'\n", filename);
1986 void tcc_close(BufferedFile
*bf
)
1988 total_lines
+= bf
->line_num
;
1993 /* fill input buffer and peek next char */
1994 static int tcc_peekc_slow(BufferedFile
*bf
)
1997 /* only tries to read if really end of buffer */
1998 if (bf
->buf_ptr
>= bf
->buf_end
) {
2000 #if defined(PARSE_DEBUG)
2005 len
= read(bf
->fd
, bf
->buffer
, len
);
2012 bf
->buf_ptr
= bf
->buffer
;
2013 bf
->buf_end
= bf
->buffer
+ len
;
2014 *bf
->buf_end
= CH_EOB
;
2016 if (bf
->buf_ptr
< bf
->buf_end
) {
2017 return bf
->buf_ptr
[0];
2019 bf
->buf_ptr
= bf
->buf_end
;
2024 /* return the current character, handling end of block if necessary
2026 static int handle_eob(void)
2028 return tcc_peekc_slow(file
);
2031 /* read next char from current input file and handle end of input buffer */
2032 static inline void inp(void)
2034 ch
= *(++(file
->buf_ptr
));
2035 /* end of buffer/file handling */
2040 /* handle '\[\r]\n' */
2041 static int handle_stray_noerror(void)
2043 while (ch
== '\\') {
2048 } else if (ch
== '\r') {
2062 static void handle_stray(void)
2064 if (handle_stray_noerror())
2065 error("stray '\\' in program");
2068 /* skip the stray and handle the \\n case. Output an error if
2069 incorrect char after the stray */
2070 static int handle_stray1(uint8_t *p
)
2074 if (p
>= file
->buf_end
) {
2091 /* handle just the EOB case, but not stray */
2092 #define PEEKC_EOB(c, p)\
2103 /* handle the complicated stray case */
2104 #define PEEKC(c, p)\
2109 c = handle_stray1(p);\
2114 /* input with '\[\r]\n' handling. Note that this function cannot
2115 handle other characters after '\', so you cannot call it inside
2116 strings or comments */
2117 static void minp(void)
2125 /* single line C++ comments */
2126 static uint8_t *parse_line_comment(uint8_t *p
)
2134 if (c
== '\n' || c
== CH_EOF
) {
2136 } else if (c
== '\\') {
2145 } else if (c
== '\r') {
2163 static uint8_t *parse_comment(uint8_t *p
)
2169 /* fast skip loop */
2172 if (c
== '\n' || c
== '*' || c
== '\\')
2176 if (c
== '\n' || c
== '*' || c
== '\\')
2180 /* now we can handle all the cases */
2184 } else if (c
== '*') {
2190 } else if (c
== '/') {
2191 goto end_of_comment
;
2192 } else if (c
== '\\') {
2197 /* skip '\[\r]\n', otherwise just skip the stray */
2203 } else if (c
== '\r') {
2220 /* stray, eob or eof */
2225 error("unexpected end of file in comment");
2226 } else if (c
== '\\') {
2238 /* space exlcuding newline */
2239 static inline int is_space(int ch
)
2241 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
2244 static inline void skip_spaces(void)
2246 while (is_space(ch
))
2250 /* parse a string without interpreting escapes */
2251 static uint8_t *parse_pp_string(uint8_t *p
,
2252 int sep
, CString
*str
)
2260 } else if (c
== '\\') {
2265 unterminated_string
:
2266 /* XXX: indicate line number of start of string */
2267 error("missing terminating %c character", sep
);
2268 } else if (c
== '\\') {
2269 /* escape : just skip \[\r]\n */
2274 } else if (c
== '\r') {
2277 expect("'\n' after '\r'");
2280 } else if (c
== CH_EOF
) {
2281 goto unterminated_string
;
2284 cstr_ccat(str
, '\\');
2290 } else if (c
== '\n') {
2293 } else if (c
== '\r') {
2297 cstr_ccat(str
, '\r');
2313 /* skip block of text until #else, #elif or #endif. skip also pairs of
2315 void preprocess_skip(void)
2317 int a
, start_of_line
, c
, in_warn_or_error
;
2324 in_warn_or_error
= 0;
2345 } else if (c
== '\\') {
2346 ch
= file
->buf_ptr
[0];
2347 handle_stray_noerror();
2354 if (in_warn_or_error
)
2356 p
= parse_pp_string(p
, c
, NULL
);
2360 if (in_warn_or_error
)
2367 p
= parse_comment(p
);
2368 } else if (ch
== '/') {
2369 p
= parse_line_comment(p
);
2374 if (start_of_line
) {
2379 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
2381 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
2383 else if (tok
== TOK_ENDIF
)
2385 else if( tok
== TOK_ERROR
|| tok
== TOK_WARNING
)
2386 in_warn_or_error
= 1;
2400 /* ParseState handling */
2402 /* XXX: currently, no include file info is stored. Thus, we cannot display
2403 accurate messages if the function or data definition spans multiple
2406 /* save current parse state in 's' */
2407 void save_parse_state(ParseState
*s
)
2409 s
->line_num
= file
->line_num
;
2410 s
->macro_ptr
= macro_ptr
;
2415 /* restore parse state from 's' */
2416 void restore_parse_state(ParseState
*s
)
2418 file
->line_num
= s
->line_num
;
2419 macro_ptr
= s
->macro_ptr
;
2424 /* return the number of additional 'ints' necessary to store the
2426 static inline int tok_ext_size(int t
)
2440 error("unsupported token");
2447 return LDOUBLE_SIZE
/ 4;
2453 /* token string handling */
2455 static inline void tok_str_new(TokenString
*s
)
2459 s
->allocated_len
= 0;
2460 s
->last_line_num
= -1;
2463 static void tok_str_free(int *str
)
2468 static int *tok_str_realloc(TokenString
*s
)
2472 if (s
->allocated_len
== 0) {
2475 len
= s
->allocated_len
* 2;
2477 str
= tcc_realloc(s
->str
, len
* sizeof(int));
2479 error("memory full");
2480 s
->allocated_len
= len
;
2485 static void tok_str_add(TokenString
*s
, int t
)
2491 if (len
>= s
->allocated_len
)
2492 str
= tok_str_realloc(s
);
2497 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2504 /* allocate space for worst case */
2505 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2506 str
= tok_str_realloc(s
);
2515 str
[len
++] = cv
->tab
[0];
2524 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
2525 while ((len
+ nb_words
) > s
->allocated_len
)
2526 str
= tok_str_realloc(s
);
2527 cstr
= (CString
*)(str
+ len
);
2529 cstr
->size
= cv
->cstr
->size
;
2530 cstr
->data_allocated
= NULL
;
2531 cstr
->size_allocated
= cstr
->size
;
2532 memcpy((char *)cstr
+ sizeof(CString
),
2533 cv
->cstr
->data
, cstr
->size
);
2540 #if LDOUBLE_SIZE == 8
2543 str
[len
++] = cv
->tab
[0];
2544 str
[len
++] = cv
->tab
[1];
2546 #if LDOUBLE_SIZE == 12
2548 str
[len
++] = cv
->tab
[0];
2549 str
[len
++] = cv
->tab
[1];
2550 str
[len
++] = cv
->tab
[2];
2551 #elif LDOUBLE_SIZE != 8
2552 #error add long double size support
2561 /* add the current parse token in token string 's' */
2562 static void tok_str_add_tok(TokenString
*s
)
2566 /* save line number info */
2567 if (file
->line_num
!= s
->last_line_num
) {
2568 s
->last_line_num
= file
->line_num
;
2569 cval
.i
= s
->last_line_num
;
2570 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2572 tok_str_add2(s
, tok
, &tokc
);
2575 #if LDOUBLE_SIZE == 12
2576 #define LDOUBLE_GET(p, cv) \
2580 #elif LDOUBLE_SIZE == 8
2581 #define LDOUBLE_GET(p, cv) \
2585 #error add long double size support
2589 /* get a token from an integer array and increment pointer
2590 accordingly. we code it as a macro to avoid pointer aliasing. */
2591 #define TOK_GET(t, p, cv) \
2606 cv.cstr = (CString *)p; \
2607 cv.cstr->data = (char *)p + sizeof(CString);\
2608 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2617 case TOK_CLDOUBLE: \
2618 LDOUBLE_GET(p, cv); \
2619 p += LDOUBLE_SIZE / 4; \
2626 /* defines handling */
2627 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2631 s
= sym_push2(&define_stack
, v
, macro_type
, (int)str
);
2632 s
->next
= first_arg
;
2633 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2636 /* undefined a define symbol. Its name is just set to zero */
2637 static void define_undef(Sym
*s
)
2641 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2642 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2646 static inline Sym
*define_find(int v
)
2649 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2651 return table_ident
[v
]->sym_define
;
2654 /* free define stack until top reaches 'b' */
2655 static void free_defines(Sym
*b
)
2663 /* do not free args or predefined defines */
2665 tok_str_free((int *)top
->c
);
2667 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2668 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2676 static Sym
*label_find(int v
)
2679 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2681 return table_ident
[v
]->sym_label
;
2684 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
2687 s
= sym_push2(ptop
, v
, 0, 0);
2689 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
2690 if (ptop
== &global_label_stack
) {
2691 /* modify the top most local identifier, so that
2692 sym_identifier will point to 's' when popped */
2694 ps
= &(*ps
)->prev_tok
;
2701 /* pop labels until element last is reached. Look if any labels are
2702 undefined. Define symbols if '&&label' was used. */
2703 static void label_pop(Sym
**ptop
, Sym
*slast
)
2706 for(s
= *ptop
; s
!= slast
; s
= s1
) {
2708 if (s
->r
== LABEL_DECLARED
) {
2709 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
2710 } else if (s
->r
== LABEL_FORWARD
) {
2711 error("label '%s' used but not defined",
2712 get_tok_str(s
->v
, NULL
));
2715 /* define corresponding symbol. A size of
2717 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
2721 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
2727 /* eval an expression for #if/#elif */
2728 static int expr_preprocess(void)
2734 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2735 next(); /* do macro subst */
2736 if (tok
== TOK_DEFINED
) {
2741 c
= define_find(tok
) != 0;
2746 } else if (tok
>= TOK_IDENT
) {
2747 /* if undefined macro */
2751 tok_str_add_tok(&str
);
2753 tok_str_add(&str
, -1); /* simulate end of file */
2754 tok_str_add(&str
, 0);
2755 /* now evaluate C constant expression */
2756 macro_ptr
= str
.str
;
2760 tok_str_free(str
.str
);
2764 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2765 static void tok_print(int *str
)
2771 TOK_GET(t
, str
, cval
);
2774 printf(" %s", get_tok_str(t
, &cval
));
2780 /* parse after #define */
2781 static void parse_define(void)
2783 Sym
*s
, *first
, **ps
;
2784 int v
, t
, varg
, is_vaargs
, c
;
2789 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2790 /* XXX: should check if same macro (ANSI) */
2793 /* '(' must be just after macro definition for MACRO_FUNC */
2794 c
= file
->buf_ptr
[0];
2796 c
= handle_stray1(file
->buf_ptr
);
2801 while (tok
!= ')') {
2805 if (varg
== TOK_DOTS
) {
2806 varg
= TOK___VA_ARGS__
;
2808 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2812 if (varg
< TOK_IDENT
)
2813 error("badly punctuated parameter list");
2814 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2825 /* EOF testing necessary for '-D' handling */
2826 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2827 tok_str_add2(&str
, tok
, &tokc
);
2830 tok_str_add(&str
, 0);
2832 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2835 define_push(v
, t
, str
.str
, first
);
2838 static inline int hash_cached_include(int type
, const char *filename
)
2840 const unsigned char *s
;
2844 h
= TOK_HASH_FUNC(h
, type
);
2847 h
= TOK_HASH_FUNC(h
, *s
);
2850 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
2854 /* XXX: use a token or a hash table to accelerate matching ? */
2855 static CachedInclude
*search_cached_include(TCCState
*s1
,
2856 int type
, const char *filename
)
2860 h
= hash_cached_include(type
, filename
);
2861 i
= s1
->cached_includes_hash
[h
];
2865 e
= s1
->cached_includes
[i
- 1];
2866 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2873 static inline void add_cached_include(TCCState
*s1
, int type
,
2874 const char *filename
, int ifndef_macro
)
2879 if (search_cached_include(s1
, type
, filename
))
2882 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2884 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2888 strcpy(e
->filename
, filename
);
2889 e
->ifndef_macro
= ifndef_macro
;
2890 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2891 /* add in hash table */
2892 h
= hash_cached_include(type
, filename
);
2893 e
->hash_next
= s1
->cached_includes_hash
[h
];
2894 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
2897 static void pragma_parse(TCCState
*s1
)
2902 if (tok
== TOK_pack
) {
2905 #pragma pack(1) // set
2906 #pragma pack() // reset to default
2907 #pragma pack(push,1) // push & set
2908 #pragma pack(pop) // restore previous
2912 if (tok
== TOK_ASM_pop
) {
2914 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
2916 error("out of pack stack");
2918 s1
->pack_stack_ptr
--;
2922 if (tok
== TOK_ASM_push
) {
2924 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
2926 s1
->pack_stack_ptr
++;
2929 if (tok
!= TOK_CINT
) {
2931 error("invalid pack pragma");
2934 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
2938 *s1
->pack_stack_ptr
= val
;
2944 /* is_bof is true if first non space token at beginning of file */
2945 static void preprocess(int is_bof
)
2947 TCCState
*s1
= tcc_state
;
2948 int size
, i
, c
, n
, saved_parse_flags
;
2955 saved_parse_flags
= parse_flags
;
2956 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
2957 PARSE_FLAG_LINEFEED
;
2967 s
= define_find(tok
);
2968 /* undefine symbol by putting an invalid name */
2973 case TOK_INCLUDE_NEXT
:
2974 ch
= file
->buf_ptr
[0];
2975 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2980 } else if (ch
== '\"') {
2985 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
2986 if ((q
- buf
) < sizeof(buf
) - 1)
2989 if (handle_stray_noerror() == 0)
2997 /* eat all spaces and comments after include */
2998 /* XXX: slightly incorrect */
2999 while (ch1
!= '\n' && ch1
!= CH_EOF
)
3003 /* computed #include : either we have only strings or
3004 we have anything enclosed in '<>' */
3007 if (tok
== TOK_STR
) {
3008 while (tok
!= TOK_LINEFEED
) {
3009 if (tok
!= TOK_STR
) {
3011 error("'#include' expects \"FILENAME\" or <FILENAME>");
3013 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
3019 while (tok
!= TOK_LINEFEED
) {
3020 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
3024 /* check syntax and remove '<>' */
3025 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
3026 goto include_syntax
;
3027 memmove(buf
, buf
+ 1, len
- 2);
3028 buf
[len
- 2] = '\0';
3033 e
= search_cached_include(s1
, c
, buf
);
3034 if (e
&& define_find(e
->ifndef_macro
)) {
3035 /* no need to parse the include because the 'ifndef macro'
3038 printf("%s: skipping %s\n", file
->filename
, buf
);
3041 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
3042 error("#include recursion too deep");
3043 /* push current file in stack */
3044 /* XXX: fix current line init */
3045 *s1
->include_stack_ptr
++ = file
;
3047 /* first search in current dir if "header.h" */
3048 size
= tcc_basename(file
->filename
) - file
->filename
;
3049 if (size
> sizeof(buf1
) - 1)
3050 size
= sizeof(buf1
) - 1;
3051 memcpy(buf1
, file
->filename
, size
);
3053 pstrcat(buf1
, sizeof(buf1
), buf
);
3054 f
= tcc_open(s1
, buf1
);
3056 if (tok
== TOK_INCLUDE_NEXT
)
3062 /* now search in all the include paths */
3063 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
3064 for(i
= 0; i
< n
; i
++) {
3066 if (i
< s1
->nb_include_paths
)
3067 path
= s1
->include_paths
[i
];
3069 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
3070 pstrcpy(buf1
, sizeof(buf1
), path
);
3071 pstrcat(buf1
, sizeof(buf1
), "/");
3072 pstrcat(buf1
, sizeof(buf1
), buf
);
3073 f
= tcc_open(s1
, buf1
);
3075 if (tok
== TOK_INCLUDE_NEXT
)
3081 --s1
->include_stack_ptr
;
3082 error("include file '%s' not found", buf
);
3086 printf("%s: including %s\n", file
->filename
, buf1
);
3089 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
3091 /* add include file debug info */
3093 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
3095 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
3096 ch
= file
->buf_ptr
[0];
3104 c
= expr_preprocess();
3110 if (tok
< TOK_IDENT
)
3111 error("invalid argument for '#if%sdef'", c
? "n" : "");
3115 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
3117 file
->ifndef_macro
= tok
;
3120 c
= (define_find(tok
) != 0) ^ c
;
3122 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
3123 error("memory full");
3124 *s1
->ifdef_stack_ptr
++ = c
;
3127 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3128 error("#else without matching #if");
3129 if (s1
->ifdef_stack_ptr
[-1] & 2)
3130 error("#else after #else");
3131 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
3134 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
3135 error("#elif without matching #if");
3136 c
= s1
->ifdef_stack_ptr
[-1];
3138 error("#elif after #else");
3139 /* last #if/#elif expression was true: we skip */
3142 c
= expr_preprocess();
3143 s1
->ifdef_stack_ptr
[-1] = c
;
3153 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
3154 error("#endif without matching #if");
3155 s1
->ifdef_stack_ptr
--;
3156 /* '#ifndef macro' was at the start of file. Now we check if
3157 an '#endif' is exactly at the end of file */
3158 if (file
->ifndef_macro
&&
3159 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
3160 file
->ifndef_macro_saved
= file
->ifndef_macro
;
3161 /* need to set to zero to avoid false matches if another
3162 #ifndef at middle of file */
3163 file
->ifndef_macro
= 0;
3164 while (tok
!= TOK_LINEFEED
)
3166 tok_flags
|= TOK_FLAG_ENDIF
;
3172 if (tok
!= TOK_CINT
)
3174 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
3176 if (tok
!= TOK_LINEFEED
) {
3179 pstrcpy(file
->filename
, sizeof(file
->filename
),
3180 (char *)tokc
.cstr
->data
);
3186 ch
= file
->buf_ptr
[0];
3189 while (ch
!= '\n' && ch
!= CH_EOF
) {
3190 if ((q
- buf
) < sizeof(buf
) - 1)
3193 if (handle_stray_noerror() == 0)
3200 error("#error %s", buf
);
3202 warning("#warning %s", buf
);
3208 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
3209 /* '!' is ignored to allow C scripts. numbers are ignored
3210 to emulate cpp behaviour */
3212 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
3213 warning("Ignoring unknown preprocessing directive #%s", get_tok_str(tok
, &tokc
));
3217 /* ignore other preprocess commands or #! for C scripts */
3218 while (tok
!= TOK_LINEFEED
)
3221 parse_flags
= saved_parse_flags
;
3224 /* evaluate escape codes in a string. */
3225 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
3240 case '0': case '1': case '2': case '3':
3241 case '4': case '5': case '6': case '7':
3242 /* at most three octal digits */
3247 n
= n
* 8 + c
- '0';
3251 n
= n
* 8 + c
- '0';
3256 goto add_char_nonext
;
3264 if (c
>= 'a' && c
<= 'f')
3266 else if (c
>= 'A' && c
<= 'F')
3276 goto add_char_nonext
;
3300 goto invalid_escape
;
3310 if (c
>= '!' && c
<= '~')
3311 warning("unknown escape sequence: \'\\%c\'", c
);
3313 warning("unknown escape sequence: \'\\x%x\'", c
);
3320 cstr_ccat(outstr
, c
);
3322 cstr_wccat(outstr
, c
);
3324 /* add a trailing '\0' */
3326 cstr_ccat(outstr
, '\0');
3328 cstr_wccat(outstr
, '\0');
3331 /* we use 64 bit numbers */
3334 /* bn = (bn << shift) | or_val */
3335 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
3339 for(i
=0;i
<BN_SIZE
;i
++) {
3341 bn
[i
] = (v
<< shift
) | or_val
;
3342 or_val
= v
>> (32 - shift
);
3346 void bn_zero(unsigned int *bn
)
3349 for(i
=0;i
<BN_SIZE
;i
++) {
3354 /* parse number in null terminated string 'p' and return it in the
3356 void parse_number(const char *p
)
3358 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
3360 unsigned int bn
[BN_SIZE
];
3371 goto float_frac_parse
;
3372 } else if (t
== '0') {
3373 if (ch
== 'x' || ch
== 'X') {
3377 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
3383 /* parse all digits. cannot check octal numbers at this stage
3384 because of floating point constants */
3386 if (ch
>= 'a' && ch
<= 'f')
3388 else if (ch
>= 'A' && ch
<= 'F')
3396 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
3398 error("number too long");
3404 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
3405 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
3407 /* NOTE: strtox should support that for hexa numbers, but
3408 non ISOC99 libcs do not support it, so we prefer to do
3410 /* hexadecimal or binary floats */
3411 /* XXX: handle overflows */
3423 } else if (t
>= 'a') {
3425 } else if (t
>= 'A') {
3430 bn_lshift(bn
, shift
, t
);
3437 if (t
>= 'a' && t
<= 'f') {
3439 } else if (t
>= 'A' && t
<= 'F') {
3441 } else if (t
>= '0' && t
<= '9') {
3447 error("invalid digit");
3448 bn_lshift(bn
, shift
, t
);
3453 if (ch
!= 'p' && ch
!= 'P')
3460 } else if (ch
== '-') {
3464 if (ch
< '0' || ch
> '9')
3465 expect("exponent digits");
3466 while (ch
>= '0' && ch
<= '9') {
3467 exp_val
= exp_val
* 10 + ch
- '0';
3470 exp_val
= exp_val
* s
;
3472 /* now we can generate the number */
3473 /* XXX: should patch directly float number */
3474 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
3475 d
= ldexp(d
, exp_val
- frac_bits
);
3480 /* float : should handle overflow */
3482 } else if (t
== 'L') {
3485 /* XXX: not large enough */
3486 tokc
.ld
= (long double)d
;
3492 /* decimal floats */
3494 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3499 while (ch
>= '0' && ch
<= '9') {
3500 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3506 if (ch
== 'e' || ch
== 'E') {
3507 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3511 if (ch
== '-' || ch
== '+') {
3512 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3517 if (ch
< '0' || ch
> '9')
3518 expect("exponent digits");
3519 while (ch
>= '0' && ch
<= '9') {
3520 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3532 tokc
.f
= strtof(token_buf
, NULL
);
3533 } else if (t
== 'L') {
3536 tokc
.ld
= strtold(token_buf
, NULL
);
3539 tokc
.d
= strtod(token_buf
, NULL
);
3543 unsigned long long n
, n1
;
3546 /* integer number */
3549 if (b
== 10 && *q
== '0') {
3556 /* no need for checks except for base 10 / 8 errors */
3559 } else if (t
>= 'a') {
3561 } else if (t
>= 'A') {
3566 error("invalid digit");
3570 /* detect overflow */
3571 /* XXX: this test is not reliable */
3573 error("integer constant overflow");
3576 /* XXX: not exactly ANSI compliant */
3577 if ((n
& 0xffffffff00000000LL
) != 0) {
3582 } else if (n
> 0x7fffffff) {
3593 error("three 'l's in integer constant");
3596 if (tok
== TOK_CINT
)
3598 else if (tok
== TOK_CUINT
)
3602 } else if (t
== 'U') {
3604 error("two 'u's in integer constant");
3606 if (tok
== TOK_CINT
)
3608 else if (tok
== TOK_CLLONG
)
3615 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
3623 #define PARSE2(c1, tok1, c2, tok2) \
3634 /* return next token without macro substitution */
3635 static inline void next_nomacro1(void)
3655 /* first look if it is in fact an end of buffer */
3656 if (p
>= file
->buf_end
) {
3660 if (p
>= file
->buf_end
)
3673 TCCState
*s1
= tcc_state
;
3674 if ((parse_flags
& PARSE_FLAG_LINEFEED
)
3675 && !(tok_flags
& TOK_FLAG_EOF
)) {
3676 tok_flags
|= TOK_FLAG_EOF
;
3678 goto keep_tok_flags
;
3679 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
3680 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3681 /* no include left : end of file. */
3684 tok_flags
&= ~TOK_FLAG_EOF
;
3685 /* pop include file */
3687 /* test if previous '#endif' was after a #ifdef at
3689 if (tok_flags
& TOK_FLAG_ENDIF
) {
3691 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3693 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3694 file
->ifndef_macro_saved
);
3697 /* add end of include file debug info */
3699 put_stabd(N_EINCL
, 0, 0);
3701 /* pop include stack */
3703 s1
->include_stack_ptr
--;
3704 file
= *s1
->include_stack_ptr
;
3713 tok_flags
|= TOK_FLAG_BOL
;
3715 if (0 == (parse_flags
& PARSE_FLAG_LINEFEED
))
3718 goto keep_tok_flags
;
3723 if ((tok_flags
& TOK_FLAG_BOL
) &&
3724 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3726 preprocess(tok_flags
& TOK_FLAG_BOF
);
3732 tok
= TOK_TWOSHARPS
;
3734 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
3735 p
= parse_line_comment(p
- 1);
3744 case 'a': case 'b': case 'c': case 'd':
3745 case 'e': case 'f': case 'g': case 'h':
3746 case 'i': case 'j': case 'k': case 'l':
3747 case 'm': case 'n': case 'o': case 'p':
3748 case 'q': case 'r': case 's': case 't':
3749 case 'u': case 'v': case 'w': case 'x':
3751 case 'A': case 'B': case 'C': case 'D':
3752 case 'E': case 'F': case 'G': case 'H':
3753 case 'I': case 'J': case 'K':
3754 case 'M': case 'N': case 'O': case 'P':
3755 case 'Q': case 'R': case 'S': case 'T':
3756 case 'U': case 'V': case 'W': case 'X':
3762 h
= TOK_HASH_FUNC(h
, c
);
3766 if (!isidnum_table
[c
])
3768 h
= TOK_HASH_FUNC(h
, c
);
3775 /* fast case : no stray found, so we have the full token
3776 and we have already hashed it */
3778 h
&= (TOK_HASH_SIZE
- 1);
3779 pts
= &hash_ident
[h
];
3784 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3786 pts
= &(ts
->hash_next
);
3788 ts
= tok_alloc_new(pts
, p1
, len
);
3792 cstr_reset(&tokcstr
);
3795 cstr_ccat(&tokcstr
, *p1
);
3801 while (isidnum_table
[c
]) {
3802 cstr_ccat(&tokcstr
, c
);
3805 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3811 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3813 goto parse_ident_fast
;
3816 if (c
== '\'' || c
== '\"') {
3820 cstr_reset(&tokcstr
);
3821 cstr_ccat(&tokcstr
, 'L');
3822 goto parse_ident_slow
;
3826 case '0': case '1': case '2': case '3':
3827 case '4': case '5': case '6': case '7':
3830 cstr_reset(&tokcstr
);
3831 /* after the first digit, accept digits, alpha, '.' or sign if
3832 prefixed by 'eEpP' */
3836 cstr_ccat(&tokcstr
, c
);
3838 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3839 ((c
== '+' || c
== '-') &&
3840 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3843 /* We add a trailing '\0' to ease parsing */
3844 cstr_ccat(&tokcstr
, '\0');
3845 tokc
.cstr
= &tokcstr
;
3849 /* special dot handling because it can also start a number */
3852 cstr_reset(&tokcstr
);
3853 cstr_ccat(&tokcstr
, '.');
3855 } else if (c
== '.') {
3875 /* parse the string */
3877 p
= parse_pp_string(p
, sep
, &str
);
3878 cstr_ccat(&str
, '\0');
3880 /* eval the escape (should be done as TOK_PPNUM) */
3881 cstr_reset(&tokcstr
);
3882 parse_escape_string(&tokcstr
, str
.data
, is_long
);
3887 /* XXX: make it portable */
3891 char_size
= sizeof(nwchar_t
);
3892 if (tokcstr
.size
<= char_size
)
3893 error("empty character constant");
3894 if (tokcstr
.size
> 2 * char_size
)
3895 warning("multi-character character constant");
3897 tokc
.i
= *(int8_t *)tokcstr
.data
;
3900 tokc
.i
= *(nwchar_t
*)tokcstr
.data
;
3904 tokc
.cstr
= &tokcstr
;
3918 } else if (c
== '<') {
3936 } else if (c
== '>') {
3954 } else if (c
== '=') {
3967 } else if (c
== '=') {
3980 } else if (c
== '=') {
3993 } else if (c
== '=') {
3996 } else if (c
== '>') {
4004 PARSE2('!', '!', '=', TOK_NE
)
4005 PARSE2('=', '=', '=', TOK_EQ
)
4006 PARSE2('*', '*', '=', TOK_A_MUL
)
4007 PARSE2('%', '%', '=', TOK_A_MOD
)
4008 PARSE2('^', '^', '=', TOK_A_XOR
)
4010 /* comments or operator */
4014 p
= parse_comment(p
);
4016 } else if (c
== '/') {
4017 p
= parse_line_comment(p
);
4019 } else if (c
== '=') {
4039 case '$': /* only used in assembler */
4040 case '@': /* dito */
4045 error("unrecognized character \\x%02x", c
);
4051 #if defined(PARSE_DEBUG)
4052 printf("token = %s\n", get_tok_str(tok
, &tokc
));
4056 /* return next token without macro substitution. Can read input from
4058 static void next_nomacro(void)
4064 TOK_GET(tok
, macro_ptr
, tokc
);
4065 if (tok
== TOK_LINENUM
) {
4066 file
->line_num
= tokc
.i
;
4075 /* substitute args in macro_str and return allocated string */
4076 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
4078 int *st
, last_tok
, t
, notfirst
;
4087 TOK_GET(t
, macro_str
, cval
);
4092 TOK_GET(t
, macro_str
, cval
);
4095 s
= sym_find2(args
, t
);
4102 cstr_ccat(&cstr
, ' ');
4103 TOK_GET(t
, st
, cval
);
4104 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
4109 cstr_ccat(&cstr
, '\0');
4111 printf("stringize: %s\n", (char *)cstr
.data
);
4115 tok_str_add2(&str
, TOK_STR
, &cval
);
4118 tok_str_add2(&str
, t
, &cval
);
4120 } else if (t
>= TOK_IDENT
) {
4121 s
= sym_find2(args
, t
);
4124 /* if '##' is present before or after, no arg substitution */
4125 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
4126 /* special case for var arg macros : ## eats the
4127 ',' if empty VA_ARGS variable. */
4128 /* XXX: test of the ',' is not 100%
4129 reliable. should fix it to avoid security
4131 if (gnu_ext
&& s
->type
.t
&&
4132 last_tok
== TOK_TWOSHARPS
&&
4133 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
4135 /* suppress ',' '##' */
4138 /* suppress '##' and add variable */
4146 TOK_GET(t1
, st
, cval
);
4149 tok_str_add2(&str
, t1
, &cval
);
4153 /* NOTE: the stream cannot be read when macro
4154 substituing an argument */
4155 macro_subst(&str
, nested_list
, st
, NULL
);
4158 tok_str_add(&str
, t
);
4161 tok_str_add2(&str
, t
, &cval
);
4165 tok_str_add(&str
, 0);
4169 static char const ab_month_name
[12][4] =
4171 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
4172 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
4175 /* do macro substitution of current token with macro 's' and add
4176 result to (tok_str,tok_len). 'nested_list' is the list of all
4177 macros we got inside to avoid recursing. Return non zero if no
4178 substitution needs to be done */
4179 static int macro_subst_tok(TokenString
*tok_str
,
4180 Sym
**nested_list
, Sym
*s
, struct macro_level
**can_read_stream
)
4182 Sym
*args
, *sa
, *sa1
;
4183 int mstr_allocated
, parlevel
, *mstr
, t
, t1
;
4190 /* if symbol is a macro, prepare substitution */
4191 /* special macros */
4192 if (tok
== TOK___LINE__
) {
4193 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
4197 } else if (tok
== TOK___FILE__
) {
4198 cstrval
= file
->filename
;
4200 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
4205 tm
= localtime(&ti
);
4206 if (tok
== TOK___DATE__
) {
4207 snprintf(buf
, sizeof(buf
), "%s %2d %d",
4208 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
4210 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
4211 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
4218 cstr_cat(&cstr
, cstrval
);
4219 cstr_ccat(&cstr
, '\0');
4221 tok_str_add2(tok_str
, t1
, &cval
);
4226 if (s
->type
.t
== MACRO_FUNC
) {
4227 /* NOTE: we do not use next_nomacro to avoid eating the
4228 next token. XXX: find better solution */
4232 if (t
== 0 && can_read_stream
) {
4233 /* end of macro stream: we must look at the token
4234 after in the file */
4235 struct macro_level
*ml
= *can_read_stream
;
4241 *can_read_stream
= ml
-> prev
;
4246 /* XXX: incorrect with comments */
4247 ch
= file
->buf_ptr
[0];
4248 while (is_space(ch
) || ch
== '\n')
4252 if (t
!= '(') /* no macro subst */
4255 /* argument macro */
4260 /* NOTE: empty args are allowed, except if no args */
4262 /* handle '()' case */
4263 if (!args
&& !sa
&& tok
== ')')
4266 error("macro '%s' used with too many args",
4267 get_tok_str(s
->v
, 0));
4270 /* NOTE: non zero sa->t indicates VA_ARGS */
4271 while ((parlevel
> 0 ||
4273 (tok
!= ',' || sa
->type
.t
))) &&
4277 else if (tok
== ')')
4279 if (tok
!= TOK_LINEFEED
)
4280 tok_str_add2(&str
, tok
, &tokc
);
4283 tok_str_add(&str
, 0);
4284 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (int)str
.str
);
4287 /* special case for gcc var args: add an empty
4288 var arg argument if it is omitted */
4289 if (sa
&& sa
->type
.t
&& gnu_ext
)
4299 error("macro '%s' used with too few args",
4300 get_tok_str(s
->v
, 0));
4303 /* now subst each arg */
4304 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
4309 tok_str_free((int *)sa
->c
);
4315 sym_push2(nested_list
, s
->v
, 0, 0);
4316 macro_subst(tok_str
, nested_list
, mstr
, can_read_stream
);
4317 /* pop nested defined symbol */
4319 *nested_list
= sa1
->prev
;
4327 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4328 return the resulting string (which must be freed). */
4329 static inline int *macro_twosharps(const int *macro_str
)
4332 const int *macro_ptr1
, *start_macro_ptr
, *ptr
, *saved_macro_ptr
;
4334 const char *p1
, *p2
;
4336 TokenString macro_str1
;
4339 start_macro_ptr
= macro_str
;
4340 /* we search the first '##' */
4342 macro_ptr1
= macro_str
;
4343 TOK_GET(t
, macro_str
, cval
);
4344 /* nothing more to do if end of string */
4347 if (*macro_str
== TOK_TWOSHARPS
)
4351 /* we saw '##', so we need more processing to handle it */
4353 tok_str_new(¯o_str1
);
4357 /* add all tokens seen so far */
4358 for(ptr
= start_macro_ptr
; ptr
< macro_ptr1
;) {
4359 TOK_GET(t
, ptr
, cval
);
4360 tok_str_add2(¯o_str1
, t
, &cval
);
4362 saved_macro_ptr
= macro_ptr
;
4363 /* XXX: get rid of the use of macro_ptr here */
4364 macro_ptr
= (int *)macro_str
;
4366 while (*macro_ptr
== TOK_TWOSHARPS
) {
4368 macro_ptr1
= macro_ptr
;
4371 TOK_GET(t
, macro_ptr
, cval
);
4372 /* We concatenate the two tokens if we have an
4373 identifier or a preprocessing number */
4375 p1
= get_tok_str(tok
, &tokc
);
4376 cstr_cat(&cstr
, p1
);
4377 p2
= get_tok_str(t
, &cval
);
4378 cstr_cat(&cstr
, p2
);
4379 cstr_ccat(&cstr
, '\0');
4381 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
4382 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
4383 if (tok
== TOK_PPNUM
) {
4384 /* if number, then create a number token */
4385 /* NOTE: no need to allocate because
4386 tok_str_add2() does it */
4387 cstr_reset(&tokcstr
);
4390 tokc
.cstr
= &tokcstr
;
4392 /* if identifier, we must do a test to
4393 validate we have a correct identifier */
4394 if (t
== TOK_PPNUM
) {
4404 if (!isnum(c
) && !isid(c
))
4408 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
4409 tok
= ts
->tok
; /* modify current token */
4412 const char *str
= cstr
.data
;
4413 const unsigned char *q
;
4415 /* we look for a valid token */
4416 /* XXX: do more extensive checks */
4417 if (!strcmp(str
, ">>=")) {
4419 } else if (!strcmp(str
, "<<=")) {
4421 } else if (strlen(str
) == 2) {
4422 /* search in two bytes table */
4427 if (q
[0] == str
[0] && q
[1] == str
[1])
4434 /* NOTE: because get_tok_str use a static buffer,
4437 p1
= get_tok_str(tok
, &tokc
);
4438 cstr_cat(&cstr
, p1
);
4439 cstr_ccat(&cstr
, '\0');
4440 p2
= get_tok_str(t
, &cval
);
4441 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
4442 /* cannot merge tokens: just add them separately */
4443 tok_str_add2(¯o_str1
, tok
, &tokc
);
4444 /* XXX: free associated memory ? */
4451 tok_str_add2(¯o_str1
, tok
, &tokc
);
4456 macro_ptr
= (int *)saved_macro_ptr
;
4458 tok_str_add(¯o_str1
, 0);
4459 return macro_str1
.str
;
4463 /* do macro substitution of macro_str and add result to
4464 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4465 inside to avoid recursing. */
4466 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
4467 const int *macro_str
, struct macro_level
** can_read_stream
)
4474 struct macro_level ml
;
4476 /* first scan for '##' operator handling */
4478 macro_str1
= macro_twosharps(ptr
);
4482 /* NOTE: ptr == NULL can only happen if tokens are read from
4483 file stream due to a macro function call */
4486 TOK_GET(t
, ptr
, cval
);
4491 /* if nested substitution, do nothing */
4492 if (sym_find2(*nested_list
, t
))
4495 if (can_read_stream
)
4496 ml
.prev
= *can_read_stream
, *can_read_stream
= &ml
;
4497 macro_ptr
= (int *)ptr
;
4499 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
4500 ptr
= (int *)macro_ptr
;
4502 if (can_read_stream
&& *can_read_stream
== &ml
)
4503 *can_read_stream
= ml
.prev
;
4508 tok_str_add2(tok_str
, t
, &cval
);
4512 tok_str_free(macro_str1
);
4515 /* return next token with macro substitution */
4516 static void next(void)
4518 Sym
*nested_list
, *s
;
4520 struct macro_level
*ml
;
4525 /* if not reading from macro substituted string, then try
4526 to substitute macros */
4527 if (tok
>= TOK_IDENT
&&
4528 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
4529 s
= define_find(tok
);
4531 /* we have a macro: we try to substitute */
4535 if (macro_subst_tok(&str
, &nested_list
, s
, &ml
) == 0) {
4536 /* substitution done, NOTE: maybe empty */
4537 tok_str_add(&str
, 0);
4538 macro_ptr
= str
.str
;
4539 macro_ptr_allocated
= str
.str
;
4546 /* end of macro or end of unget buffer */
4547 if (unget_buffer_enabled
) {
4548 macro_ptr
= unget_saved_macro_ptr
;
4549 unget_buffer_enabled
= 0;
4551 /* end of macro string: free it */
4552 tok_str_free(macro_ptr_allocated
);
4559 /* convert preprocessor tokens into C tokens */
4560 if (tok
== TOK_PPNUM
&&
4561 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
4562 parse_number((char *)tokc
.cstr
->data
);
4566 /* push back current token and set current token to 'last_tok'. Only
4567 identifier case handled for labels. */
4568 static inline void unget_tok(int last_tok
)
4572 unget_saved_macro_ptr
= macro_ptr
;
4573 unget_buffer_enabled
= 1;
4574 q
= unget_saved_buffer
;
4577 n
= tok_ext_size(tok
) - 1;
4580 *q
= 0; /* end of token string */
4585 void swap(int *p
, int *q
)
4593 void vsetc(CType
*type
, int r
, CValue
*vc
)
4597 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4598 error("memory full");
4599 /* cannot let cpu flags if other instruction are generated. Also
4600 avoid leaving VT_JMP anywhere except on the top of the stack
4601 because it would complicate the code generator. */
4602 if (vtop
>= vstack
) {
4603 v
= vtop
->r
& VT_VALMASK
;
4604 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
4610 vtop
->r2
= VT_CONST
;
4614 /* push integer constant */
4619 vsetc(&int_type
, VT_CONST
, &cval
);
4622 /* Return a static symbol pointing to a section */
4623 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
4624 unsigned long offset
, unsigned long size
)
4630 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
4631 sym
->type
.ref
= type
->ref
;
4632 sym
->r
= VT_CONST
| VT_SYM
;
4633 put_extern_sym(sym
, sec
, offset
, size
);
4637 /* push a reference to a section offset by adding a dummy symbol */
4638 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
4643 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4644 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
4647 /* define a new external reference to a symbol 'v' of type 'u' */
4648 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
4654 /* push forward reference */
4655 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
4656 s
->type
.ref
= type
->ref
;
4657 s
->r
= r
| VT_CONST
| VT_SYM
;
4662 /* define a new external reference to a symbol 'v' of type 'u' */
4663 static Sym
*external_sym(int v
, CType
*type
, int r
)
4669 /* push forward reference */
4670 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
4671 s
->type
.t
|= VT_EXTERN
;
4673 if (!is_compatible_types(&s
->type
, type
))
4674 error("incompatible types for redefinition of '%s'",
4675 get_tok_str(v
, NULL
));
4680 /* push a reference to global symbol v */
4681 static void vpush_global_sym(CType
*type
, int v
)
4686 sym
= external_global_sym(v
, type
, 0);
4688 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4692 void vset(CType
*type
, int r
, int v
)
4697 vsetc(type
, r
, &cval
);
4700 void vseti(int r
, int v
)
4716 void vpushv(SValue
*v
)
4718 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4719 error("memory full");
4729 /* save r to the memory stack, and mark it as being free */
4730 void save_reg(int r
)
4732 int l
, saved
, size
, align
;
4736 /* modify all stack values */
4739 for(p
=vstack
;p
<=vtop
;p
++) {
4740 if ((p
->r
& VT_VALMASK
) == r
||
4741 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& (p
->r2
& VT_VALMASK
) == r
)) {
4742 /* must save value on stack if not already done */
4744 /* NOTE: must reload 'r' because r might be equal to r2 */
4745 r
= p
->r
& VT_VALMASK
;
4746 /* store register in the stack */
4748 if ((p
->r
& VT_LVAL
) ||
4749 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
4751 size
= type_size(type
, &align
);
4752 loc
= (loc
- size
) & -align
;
4753 sv
.type
.t
= type
->t
;
4754 sv
.r
= VT_LOCAL
| VT_LVAL
;
4757 #ifdef TCC_TARGET_I386
4758 /* x86 specific: need to pop fp register ST0 if saved */
4759 if (r
== TREG_ST0
) {
4760 o(0xd9dd); /* fstp %st(1) */
4763 /* special long long case */
4764 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
4771 /* mark that stack entry as being saved on the stack */
4772 if (p
->r
& VT_LVAL
) {
4773 /* also clear the bounded flag because the
4774 relocation address of the function was stored in
4776 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4778 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4786 /* find a register of class 'rc2' with at most one reference on stack.
4787 * If none, call get_reg(rc) */
4788 int get_reg_ex(int rc
, int rc2
)
4793 for(r
=0;r
<NB_REGS
;r
++) {
4794 if (reg_classes
[r
] & rc2
) {
4797 for(p
= vstack
; p
<= vtop
; p
++) {
4798 if ((p
->r
& VT_VALMASK
) == r
||
4799 (p
->r2
& VT_VALMASK
) == r
)
4809 /* find a free register of class 'rc'. If none, save one register */
4815 /* find a free register */
4816 for(r
=0;r
<NB_REGS
;r
++) {
4817 if (reg_classes
[r
] & rc
) {
4818 for(p
=vstack
;p
<=vtop
;p
++) {
4819 if ((p
->r
& VT_VALMASK
) == r
||
4820 (p
->r2
& VT_VALMASK
) == r
)
4828 /* no register left : free the first one on the stack (VERY
4829 IMPORTANT to start from the bottom to ensure that we don't
4830 spill registers used in gen_opi()) */
4831 for(p
=vstack
;p
<=vtop
;p
++) {
4832 r
= p
->r
& VT_VALMASK
;
4833 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4835 /* also look at second register (if long long) */
4836 r
= p
->r2
& VT_VALMASK
;
4837 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4843 /* Should never comes here */
4847 /* save registers up to (vtop - n) stack entry */
4848 void save_regs(int n
)
4853 for(p
= vstack
;p
<= p1
; p
++) {
4854 r
= p
->r
& VT_VALMASK
;
4861 /* move register 's' to 'r', and flush previous value of r to memory
4863 void move_reg(int r
, int s
)
4876 /* get address of vtop (vtop MUST BE an lvalue) */
4879 vtop
->r
&= ~VT_LVAL
;
4880 /* tricky: if saved lvalue, then we can go back to lvalue */
4881 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4882 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4885 #ifdef CONFIG_TCC_BCHECK
4886 /* generate lvalue bound code */
4892 vtop
->r
&= ~VT_MUSTBOUND
;
4893 /* if lvalue, then use checking code before dereferencing */
4894 if (vtop
->r
& VT_LVAL
) {
4895 /* if not VT_BOUNDED value, then make one */
4896 if (!(vtop
->r
& VT_BOUNDED
)) {
4897 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4898 /* must save type because we must set it to int to get pointer */
4900 vtop
->type
.t
= VT_INT
;
4903 gen_bounded_ptr_add();
4904 vtop
->r
|= lval_type
;
4907 /* then check for dereferencing */
4908 gen_bounded_ptr_deref();
4913 /* store vtop a register belonging to class 'rc'. lvalues are
4914 converted to values. Cannot be used if cannot be converted to
4915 register value (such as structures). */
4918 int r
, r2
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4919 unsigned long long ll
;
4921 /* NOTE: get_reg can modify vstack[] */
4922 if (vtop
->type
.t
& VT_BITFIELD
) {
4923 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4924 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4925 /* remove bit field info to avoid loops */
4926 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4927 /* generate shifts */
4928 vpushi(32 - (bit_pos
+ bit_size
));
4930 vpushi(32 - bit_size
);
4931 /* NOTE: transformed to SHR if unsigned */
4935 if (is_float(vtop
->type
.t
) &&
4936 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4939 unsigned long offset
;
4940 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4944 /* XXX: unify with initializers handling ? */
4945 /* CPUs usually cannot use float constants, so we store them
4946 generically in data segment */
4947 size
= type_size(&vtop
->type
, &align
);
4948 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
4949 data_section
->data_offset
= offset
;
4950 /* XXX: not portable yet */
4952 /* Zero pad x87 tenbyte long doubles */
4954 vtop
->c
.tab
[2] &= 0xffff;
4956 ptr
= section_ptr_add(data_section
, size
);
4958 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
4962 ptr
[i
] = vtop
->c
.tab
[size
-1-i
];
4966 ptr
[i
] = vtop
->c
.tab
[i
];
4967 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
4968 vtop
->r
|= VT_LVAL
| VT_SYM
;
4972 #ifdef CONFIG_TCC_BCHECK
4973 if (vtop
->r
& VT_MUSTBOUND
)
4977 r
= vtop
->r
& VT_VALMASK
;
4978 /* need to reload if:
4980 - lvalue (need to dereference pointer)
4981 - already a register, but not in the right class */
4982 if (r
>= VT_CONST
||
4983 (vtop
->r
& VT_LVAL
) ||
4984 !(reg_classes
[r
] & rc
) ||
4985 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
4986 !(reg_classes
[vtop
->r2
] & rc
))) {
4988 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
4989 /* two register type load : expand to two words
4991 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4994 vtop
->c
.ui
= ll
; /* first word */
4996 vtop
->r
= r
; /* save register value */
4997 vpushi(ll
>> 32); /* second word */
4998 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
4999 (vtop
->r
& VT_LVAL
)) {
5000 /* We do not want to modifier the long long
5001 pointer here, so the safest (and less
5002 efficient) is to save all the other registers
5003 in the stack. XXX: totally inefficient. */
5005 /* load from memory */
5008 vtop
[-1].r
= r
; /* save register value */
5009 /* increment pointer to get second word */
5010 vtop
->type
.t
= VT_INT
;
5016 /* move registers */
5019 vtop
[-1].r
= r
; /* save register value */
5020 vtop
->r
= vtop
[-1].r2
;
5022 /* allocate second register */
5029 /* write second register */
5031 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
5033 /* lvalue of scalar type : need to use lvalue type
5034 because of possible cast */
5037 /* compute memory access type */
5038 if (vtop
->r
& VT_LVAL_BYTE
)
5040 else if (vtop
->r
& VT_LVAL_SHORT
)
5042 if (vtop
->r
& VT_LVAL_UNSIGNED
)
5046 /* restore wanted type */
5049 /* one register type load */
5054 #ifdef TCC_TARGET_C67
5055 /* uses register pairs for doubles */
5056 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
5063 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
5064 void gv2(int rc1
, int rc2
)
5068 /* generate more generic register first. But VT_JMP or VT_CMP
5069 values must be generated first in all cases to avoid possible
5071 v
= vtop
[0].r
& VT_VALMASK
;
5072 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
5077 /* test if reload is needed for first register */
5078 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
5088 /* test if reload is needed for first register */
5089 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
5095 /* expand long long on stack in two int registers */
5100 u
= vtop
->type
.t
& VT_UNSIGNED
;
5103 vtop
[0].r
= vtop
[-1].r2
;
5104 vtop
[0].r2
= VT_CONST
;
5105 vtop
[-1].r2
= VT_CONST
;
5106 vtop
[0].type
.t
= VT_INT
| u
;
5107 vtop
[-1].type
.t
= VT_INT
| u
;
5110 #ifdef TCC_TARGET_ARM
5111 /* expand long long on stack */
5112 void lexpand_nr(void)
5116 u
= vtop
->type
.t
& VT_UNSIGNED
;
5118 vtop
->r2
= VT_CONST
;
5119 vtop
->type
.t
= VT_INT
| u
;
5120 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
5121 if (v
== VT_CONST
) {
5122 vtop
[-1].c
.ui
= vtop
->c
.ull
;
5123 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
5125 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
5127 vtop
->r
= vtop
[-1].r
;
5128 } else if (v
> VT_CONST
) {
5132 vtop
->r
= vtop
[-1].r2
;
5133 vtop
[-1].r2
= VT_CONST
;
5134 vtop
[-1].type
.t
= VT_INT
| u
;
5138 /* build a long long from two ints */
5141 gv2(RC_INT
, RC_INT
);
5142 vtop
[-1].r2
= vtop
[0].r
;
5143 vtop
[-1].type
.t
= t
;
5147 /* rotate n first stack elements to the bottom
5148 I1 ... In -> I2 ... In I1 [top is right]
5156 for(i
=-n
+1;i
!=0;i
++)
5157 vtop
[i
] = vtop
[i
+1];
5161 /* rotate n first stack elements to the top
5162 I1 ... In -> In I1 ... I(n-1) [top is right]
5170 for(i
= 0;i
< n
- 1; i
++)
5171 vtop
[-i
] = vtop
[-i
- 1];
5175 #ifdef TCC_TARGET_ARM
5176 /* like vrott but in other direction
5177 In ... I1 -> I(n-1) ... I1 In [top is right]
5185 for(i
= n
- 1; i
> 0; i
--)
5186 vtop
[-i
] = vtop
[-i
+ 1];
5191 /* pop stack value */
5195 v
= vtop
->r
& VT_VALMASK
;
5196 #ifdef TCC_TARGET_I386
5197 /* for x86, we need to pop the FP stack */
5198 if (v
== TREG_ST0
&& !nocode_wanted
) {
5199 o(0xd9dd); /* fstp %st(1) */
5202 if (v
== VT_JMP
|| v
== VT_JMPI
) {
5203 /* need to put correct jump if && or || without test */
5209 /* convert stack entry to register and duplicate its value in another
5217 if ((t
& VT_BTYPE
) == VT_LLONG
) {
5224 /* stack: H L L1 H1 */
5232 /* duplicate value */
5243 load(r1
, &sv
); /* move r to r1 */
5245 /* duplicates value */
5250 /* generate CPU independent (unsigned) long long operations */
5251 void gen_opl(int op
)
5253 int t
, a
, b
, op1
, c
, i
;
5260 func
= TOK___divdi3
;
5263 func
= TOK___udivdi3
;
5266 func
= TOK___moddi3
;
5269 func
= TOK___umoddi3
;
5271 /* call generic long long function */
5272 vpush_global_sym(&func_old_type
, func
);
5277 vtop
->r2
= REG_LRET
;
5290 /* stack: L1 H1 L2 H2 */
5295 vtop
[-2] = vtop
[-3];
5298 /* stack: H1 H2 L1 L2 */
5304 /* stack: H1 H2 L1 L2 ML MH */
5307 /* stack: ML MH H1 H2 L1 L2 */
5311 /* stack: ML MH H1 L2 H2 L1 */
5316 /* stack: ML MH M1 M2 */
5319 } else if (op
== '+' || op
== '-') {
5320 /* XXX: add non carry method too (for MIPS or alpha) */
5326 /* stack: H1 H2 (L1 op L2) */
5329 gen_op(op1
+ 1); /* TOK_xxxC2 */
5332 /* stack: H1 H2 (L1 op L2) */
5335 /* stack: (L1 op L2) H1 H2 */
5337 /* stack: (L1 op L2) (H1 op H2) */
5345 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5346 t
= vtop
[-1].type
.t
;
5350 /* stack: L H shift */
5352 /* constant: simpler */
5353 /* NOTE: all comments are for SHL. the other cases are
5354 done by swaping words */
5365 if (op
!= TOK_SAR
) {
5398 /* XXX: should provide a faster fallback on x86 ? */
5401 func
= TOK___sardi3
;
5404 func
= TOK___shrdi3
;
5407 func
= TOK___shldi3
;
5413 /* compare operations */
5419 /* stack: L1 H1 L2 H2 */
5421 vtop
[-1] = vtop
[-2];
5423 /* stack: L1 L2 H1 H2 */
5426 /* when values are equal, we need to compare low words. since
5427 the jump is inverted, we invert the test too. */
5430 else if (op1
== TOK_GT
)
5432 else if (op1
== TOK_ULT
)
5434 else if (op1
== TOK_UGT
)
5439 if (op1
!= TOK_NE
) {
5443 /* generate non equal test */
5444 /* XXX: NOT PORTABLE yet */
5448 #if defined(TCC_TARGET_I386)
5449 b
= psym(0x850f, 0);
5450 #elif defined(TCC_TARGET_ARM)
5452 o(0x1A000000 | encbranch(ind
, 0, 1));
5453 #elif defined(TCC_TARGET_C67)
5454 error("not implemented");
5456 #error not supported
5460 /* compare low. Always unsigned */
5464 else if (op1
== TOK_LE
)
5466 else if (op1
== TOK_GT
)
5468 else if (op1
== TOK_GE
)
5478 /* handle integer constant optimizations and various machine
5480 void gen_opic(int op
)
5482 int c1
, c2
, t1
, t2
, n
;
5485 typedef unsigned long long U
;
5489 t1
= v1
->type
.t
& VT_BTYPE
;
5490 t2
= v2
->type
.t
& VT_BTYPE
;
5491 l1
= (t1
== VT_LLONG
) ? v1
->c
.ll
: v1
->c
.i
;
5492 l2
= (t2
== VT_LLONG
) ? v2
->c
.ll
: v2
->c
.i
;
5494 /* currently, we cannot do computations with forward symbols */
5495 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5496 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5499 case '+': l1
+= l2
; break;
5500 case '-': l1
-= l2
; break;
5501 case '&': l1
&= l2
; break;
5502 case '^': l1
^= l2
; break;
5503 case '|': l1
|= l2
; break;
5504 case '*': l1
*= l2
; break;
5511 /* if division by zero, generate explicit division */
5514 error("division by zero in constant");
5518 default: l1
/= l2
; break;
5519 case '%': l1
%= l2
; break;
5520 case TOK_UDIV
: l1
= (U
)l1
/ l2
; break;
5521 case TOK_UMOD
: l1
= (U
)l1
% l2
; break;
5524 case TOK_SHL
: l1
<<= l2
; break;
5525 case TOK_SHR
: l1
= (U
)l1
>> l2
; break;
5526 case TOK_SAR
: l1
>>= l2
; break;
5528 case TOK_ULT
: l1
= (U
)l1
< (U
)l2
; break;
5529 case TOK_UGE
: l1
= (U
)l1
>= (U
)l2
; break;
5530 case TOK_EQ
: l1
= l1
== l2
; break;
5531 case TOK_NE
: l1
= l1
!= l2
; break;
5532 case TOK_ULE
: l1
= (U
)l1
<= (U
)l2
; break;
5533 case TOK_UGT
: l1
= (U
)l1
> (U
)l2
; break;
5534 case TOK_LT
: l1
= l1
< l2
; break;
5535 case TOK_GE
: l1
= l1
>= l2
; break;
5536 case TOK_LE
: l1
= l1
<= l2
; break;
5537 case TOK_GT
: l1
= l1
> l2
; break;
5539 case TOK_LAND
: l1
= l1
&& l2
; break;
5540 case TOK_LOR
: l1
= l1
|| l2
; break;
5547 /* if commutative ops, put c2 as constant */
5548 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
5549 op
== '|' || op
== '*')) {
5551 c2
= c1
; //c = c1, c1 = c2, c2 = c;
5552 l2
= l1
; //l = l1, l1 = l2, l2 = l;
5554 /* Filter out NOP operations like x*1, x-0, x&-1... */
5555 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
5558 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
5559 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
5565 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
5566 /* try to use shifts instead of muls or divs */
5567 if (l2
> 0 && (l2
& (l2
- 1)) == 0) {
5576 else if (op
== TOK_PDIV
)
5582 } else if (c2
&& (op
== '+' || op
== '-') &&
5583 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
5584 (VT_CONST
| VT_SYM
)) {
5585 /* symbol + constant case */
5592 if (!nocode_wanted
) {
5593 /* call low level op generator */
5594 if (t1
== VT_LLONG
|| t2
== VT_LLONG
)
5605 /* generate a floating point operation with constant propagation */
5606 void gen_opif(int op
)
5614 /* currently, we cannot do computations with forward symbols */
5615 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5616 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5618 if (v1
->type
.t
== VT_FLOAT
) {
5621 } else if (v1
->type
.t
== VT_DOUBLE
) {
5629 /* NOTE: we only do constant propagation if finite number (not
5630 NaN or infinity) (ANSI spec) */
5631 if (!ieee_finite(f1
) || !ieee_finite(f2
))
5635 case '+': f1
+= f2
; break;
5636 case '-': f1
-= f2
; break;
5637 case '*': f1
*= f2
; break;
5641 error("division by zero in constant");
5646 /* XXX: also handles tests ? */
5650 /* XXX: overflow test ? */
5651 if (v1
->type
.t
== VT_FLOAT
) {
5653 } else if (v1
->type
.t
== VT_DOUBLE
) {
5661 if (!nocode_wanted
) {
5669 static int pointed_size(CType
*type
)
5672 return type_size(pointed_type(type
), &align
);
5675 static inline int is_null_pointer(SValue
*p
)
5677 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5679 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
5680 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
5683 static inline int is_integer_btype(int bt
)
5685 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
5686 bt
== VT_INT
|| bt
== VT_LLONG
);
5689 /* check types for comparison or substraction of pointers */
5690 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
5692 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
5695 /* null pointers are accepted for all comparisons as gcc */
5696 if (is_null_pointer(p1
) || is_null_pointer(p2
))
5700 bt1
= type1
->t
& VT_BTYPE
;
5701 bt2
= type2
->t
& VT_BTYPE
;
5702 /* accept comparison between pointer and integer with a warning */
5703 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
5704 if (op
!= TOK_LOR
&& op
!= TOK_LAND
)
5705 warning("comparison between pointer and integer");
5709 /* both must be pointers or implicit function pointers */
5710 if (bt1
== VT_PTR
) {
5711 type1
= pointed_type(type1
);
5712 } else if (bt1
!= VT_FUNC
)
5713 goto invalid_operands
;
5715 if (bt2
== VT_PTR
) {
5716 type2
= pointed_type(type2
);
5717 } else if (bt2
!= VT_FUNC
) {
5719 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
5721 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5722 (type2
->t
& VT_BTYPE
) == VT_VOID
)
5726 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5727 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5728 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
5729 /* gcc-like error if '-' is used */
5731 goto invalid_operands
;
5733 warning("comparison of distinct pointer types lacks a cast");
5737 /* generic gen_op: handles types problems */
5740 int u
, t1
, t2
, bt1
, bt2
, t
;
5743 t1
= vtop
[-1].type
.t
;
5744 t2
= vtop
[0].type
.t
;
5745 bt1
= t1
& VT_BTYPE
;
5746 bt2
= t2
& VT_BTYPE
;
5748 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5749 /* at least one operand is a pointer */
5750 /* relationnal op: must be both pointers */
5751 if (op
>= TOK_ULT
&& op
<= TOK_LOR
) {
5752 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5753 /* pointers are handled are unsigned */
5754 t
= VT_INT
| VT_UNSIGNED
;
5757 /* if both pointers, then it must be the '-' op */
5758 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
5760 error("cannot use pointers here");
5761 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5762 /* XXX: check that types are compatible */
5763 u
= pointed_size(&vtop
[-1].type
);
5765 /* set to integer type */
5766 vtop
->type
.t
= VT_INT
;
5770 /* exactly one pointer : must be '+' or '-'. */
5771 if (op
!= '-' && op
!= '+')
5772 error("cannot use pointers here");
5773 /* Put pointer as first operand */
5774 if (bt2
== VT_PTR
) {
5778 type1
= vtop
[-1].type
;
5779 /* XXX: cast to int ? (long long case) */
5780 vpushi(pointed_size(&vtop
[-1].type
));
5782 #ifdef CONFIG_TCC_BCHECK
5783 /* if evaluating constant expression, no code should be
5784 generated, so no bound check */
5785 if (do_bounds_check
&& !const_wanted
) {
5786 /* if bounded pointers, we generate a special code to
5793 gen_bounded_ptr_add();
5799 /* put again type if gen_opic() swaped operands */
5802 } else if (is_float(bt1
) || is_float(bt2
)) {
5803 /* compute bigger type and do implicit casts */
5804 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5806 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5811 /* floats can only be used for a few operations */
5812 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
5813 (op
< TOK_ULT
|| op
> TOK_GT
))
5814 error("invalid operands for binary operation");
5816 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5817 /* cast to biggest op */
5819 /* convert to unsigned if it does not fit in a long long */
5820 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5821 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5825 /* integer operations */
5827 /* convert to unsigned if it does not fit in an integer */
5828 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5829 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5832 /* XXX: currently, some unsigned operations are explicit, so
5833 we modify them here */
5834 if (t
& VT_UNSIGNED
) {
5841 else if (op
== TOK_LT
)
5843 else if (op
== TOK_GT
)
5845 else if (op
== TOK_LE
)
5847 else if (op
== TOK_GE
)
5854 /* special case for shifts and long long: we keep the shift as
5856 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
5863 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5864 /* relationnal op: the result is an int */
5865 vtop
->type
.t
= VT_INT
;
5872 /* generic itof for unsigned long long case */
5873 void gen_cvt_itof1(int t
)
5875 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
5876 (VT_LLONG
| VT_UNSIGNED
)) {
5879 vpush_global_sym(&func_old_type
, TOK___ulltof
);
5880 else if (t
== VT_DOUBLE
)
5881 vpush_global_sym(&func_old_type
, TOK___ulltod
);
5883 vpush_global_sym(&func_old_type
, TOK___ulltold
);
5893 /* generic ftoi for unsigned long long case */
5894 void gen_cvt_ftoi1(int t
)
5898 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
5899 /* not handled natively */
5900 st
= vtop
->type
.t
& VT_BTYPE
;
5902 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
5903 else if (st
== VT_DOUBLE
)
5904 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
5906 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
5911 vtop
->r2
= REG_LRET
;
5917 /* force char or short cast */
5918 void force_charshort_cast(int t
)
5922 /* XXX: add optimization if lvalue : just change type and offset */
5927 if (t
& VT_UNSIGNED
) {
5928 vpushi((1 << bits
) - 1);
5934 /* result must be signed or the SAR is converted to an SHL
5935 This was not the case when "t" was a signed short
5936 and the last value on the stack was an unsigned int */
5937 vtop
->type
.t
&= ~VT_UNSIGNED
;
5943 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5944 static void gen_cast(CType
*type
)
5946 int sbt
, dbt
, sf
, df
, c
;
5948 /* special delayed cast for char/short */
5949 /* XXX: in some cases (multiple cascaded casts), it may still
5951 if (vtop
->r
& VT_MUSTCAST
) {
5952 vtop
->r
&= ~VT_MUSTCAST
;
5953 force_charshort_cast(vtop
->type
.t
);
5956 /* bitfields first get cast to ints */
5957 if (vtop
->type
.t
& VT_BITFIELD
) {
5961 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
5962 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
5964 if (sbt
!= dbt
&& !nocode_wanted
) {
5967 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5969 /* convert from fp to fp */
5971 /* constant case: we can do it now */
5972 /* XXX: in ISOC, cannot do it if error in convert */
5973 if (dbt
== VT_FLOAT
&& sbt
== VT_DOUBLE
)
5974 vtop
->c
.f
= (float)vtop
->c
.d
;
5975 else if (dbt
== VT_FLOAT
&& sbt
== VT_LDOUBLE
)
5976 vtop
->c
.f
= (float)vtop
->c
.ld
;
5977 else if (dbt
== VT_DOUBLE
&& sbt
== VT_FLOAT
)
5978 vtop
->c
.d
= (double)vtop
->c
.f
;
5979 else if (dbt
== VT_DOUBLE
&& sbt
== VT_LDOUBLE
)
5980 vtop
->c
.d
= (double)vtop
->c
.ld
;
5981 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_FLOAT
)
5982 vtop
->c
.ld
= (long double)vtop
->c
.f
;
5983 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_DOUBLE
)
5984 vtop
->c
.ld
= (long double)vtop
->c
.d
;
5986 /* non constant case: generate code */
5990 /* convert int to fp */
5993 case VT_LLONG
| VT_UNSIGNED
:
5995 /* XXX: add const cases for long long */
5997 case VT_INT
| VT_UNSIGNED
:
5999 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.ui
; break;
6000 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.ui
; break;
6001 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.ui
; break;
6006 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.i
; break;
6007 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.i
; break;
6008 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.i
; break;
6014 #if !defined(TCC_TARGET_ARM)
6021 /* convert fp to int */
6022 if (dbt
== VT_BOOL
) {
6026 /* we handle char/short/etc... with generic code */
6027 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
6028 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
6033 case VT_LLONG
| VT_UNSIGNED
:
6035 /* XXX: add const cases for long long */
6037 case VT_INT
| VT_UNSIGNED
:
6039 case VT_FLOAT
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
6040 case VT_DOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
6041 case VT_LDOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
6047 case VT_FLOAT
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
6048 case VT_DOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
6049 case VT_LDOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
6057 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
6058 /* additional cast for char/short... */
6063 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
6064 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
6065 /* scalar to long long */
6067 if (sbt
== (VT_INT
| VT_UNSIGNED
))
6068 vtop
->c
.ll
= vtop
->c
.ui
;
6070 vtop
->c
.ll
= vtop
->c
.i
;
6072 /* machine independent conversion */
6074 /* generate high word */
6075 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
6083 /* patch second register */
6084 vtop
[-1].r2
= vtop
->r
;
6088 } else if (dbt
== VT_BOOL
) {
6089 /* scalar to bool */
6092 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
6093 (dbt
& VT_BTYPE
) == VT_SHORT
) {
6094 if (sbt
== VT_PTR
) {
6095 vtop
->type
.t
= VT_INT
;
6096 warning("nonportable conversion from pointer to char/short");
6098 force_charshort_cast(dbt
);
6099 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
6101 if (sbt
== VT_LLONG
) {
6102 /* from long long: just take low order word */
6106 /* if lvalue and single word type, nothing to do because
6107 the lvalue already contains the real type size (see
6108 VT_LVAL_xxx constants) */
6110 } else if ((dbt
& VT_BTYPE
) == VT_PTR
&& !(vtop
->r
& VT_LVAL
)) {
6111 /* if we are casting between pointer types,
6112 we must update the VT_LVAL_xxx size */
6113 vtop
->r
= (vtop
->r
& ~VT_LVAL_TYPE
)
6114 | (lvalue_type(type
->ref
->type
.t
) & VT_LVAL_TYPE
);
6119 /* return type size. Put alignment at 'a' */
6120 static int type_size(CType
*type
, int *a
)
6125 bt
= type
->t
& VT_BTYPE
;
6126 if (bt
== VT_STRUCT
) {
6131 } else if (bt
== VT_PTR
) {
6132 if (type
->t
& VT_ARRAY
) {
6134 return type_size(&s
->type
, a
) * s
->c
;
6139 } else if (bt
== VT_LDOUBLE
) {
6141 return LDOUBLE_SIZE
;
6142 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
6143 #ifdef TCC_TARGET_I386
6145 #elif defined(TCC_TARGET_ARM)
6155 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
6158 } else if (bt
== VT_SHORT
) {
6162 /* char, void, function, _Bool */
6168 /* return the pointed type of t */
6169 static inline CType
*pointed_type(CType
*type
)
6171 return &type
->ref
->type
;
6174 /* modify type so that its it is a pointer to type. */
6175 static void mk_pointer(CType
*type
)
6178 s
= sym_push(SYM_FIELD
, type
, 0, -1);
6179 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
6183 /* compare function types. OLD functions match any new functions */
6184 static int is_compatible_func(CType
*type1
, CType
*type2
)
6190 if (!is_compatible_types(&s1
->type
, &s2
->type
))
6192 /* check func_call */
6193 if (FUNC_CALL(s1
->r
) != FUNC_CALL(s2
->r
))
6195 /* XXX: not complete */
6196 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
6200 while (s1
!= NULL
) {
6203 if (!is_compatible_parameter_types(&s1
->type
, &s2
->type
))
6213 /* return true if type1 and type2 are the same. If unqualified is
6214 true, qualifiers on the types are ignored.
6216 - enums are not checked as gcc __builtin_types_compatible_p ()
6218 static int compare_types(CType
*type1
, CType
*type2
, int unqualified
)
6222 t1
= type1
->t
& VT_TYPE
;
6223 t2
= type2
->t
& VT_TYPE
;
6225 /* strip qualifiers before comparing */
6226 t1
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6227 t2
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6229 /* XXX: bitfields ? */
6232 /* test more complicated cases */
6233 bt1
= t1
& VT_BTYPE
;
6234 if (bt1
== VT_PTR
) {
6235 type1
= pointed_type(type1
);
6236 type2
= pointed_type(type2
);
6237 return is_compatible_types(type1
, type2
);
6238 } else if (bt1
== VT_STRUCT
) {
6239 return (type1
->ref
== type2
->ref
);
6240 } else if (bt1
== VT_FUNC
) {
6241 return is_compatible_func(type1
, type2
);
6247 /* return true if type1 and type2 are exactly the same (including
6250 static int is_compatible_types(CType
*type1
, CType
*type2
)
6252 return compare_types(type1
,type2
,0);
6255 /* return true if type1 and type2 are the same (ignoring qualifiers).
6257 static int is_compatible_parameter_types(CType
*type1
, CType
*type2
)
6259 return compare_types(type1
,type2
,1);
6262 /* print a type. If 'varstr' is not NULL, then the variable is also
6263 printed in the type */
6265 /* XXX: add array and function pointers */
6266 void type_to_str(char *buf
, int buf_size
,
6267 CType
*type
, const char *varstr
)
6274 t
= type
->t
& VT_TYPE
;
6277 if (t
& VT_CONSTANT
)
6278 pstrcat(buf
, buf_size
, "const ");
6279 if (t
& VT_VOLATILE
)
6280 pstrcat(buf
, buf_size
, "volatile ");
6281 if (t
& VT_UNSIGNED
)
6282 pstrcat(buf
, buf_size
, "unsigned ");
6312 tstr
= "long double";
6314 pstrcat(buf
, buf_size
, tstr
);
6318 if (bt
== VT_STRUCT
)
6322 pstrcat(buf
, buf_size
, tstr
);
6323 v
= type
->ref
->v
& ~SYM_STRUCT
;
6324 if (v
>= SYM_FIRST_ANOM
)
6325 pstrcat(buf
, buf_size
, "<anonymous>");
6327 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
6331 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
6332 pstrcat(buf
, buf_size
, "(");
6334 while (sa
!= NULL
) {
6335 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
6336 pstrcat(buf
, buf_size
, buf1
);
6339 pstrcat(buf
, buf_size
, ", ");
6341 pstrcat(buf
, buf_size
, ")");
6345 pstrcpy(buf1
, sizeof(buf1
), "*");
6347 pstrcat(buf1
, sizeof(buf1
), varstr
);
6348 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
6352 pstrcat(buf
, buf_size
, " ");
6353 pstrcat(buf
, buf_size
, varstr
);
6358 /* verify type compatibility to store vtop in 'dt' type, and generate
6360 static void gen_assign_cast(CType
*dt
)
6362 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
6363 char buf1
[256], buf2
[256];
6366 st
= &vtop
->type
; /* source type */
6367 dbt
= dt
->t
& VT_BTYPE
;
6368 sbt
= st
->t
& VT_BTYPE
;
6369 if (dt
->t
& VT_CONSTANT
)
6370 warning("assignment of read-only location");
6373 /* special cases for pointers */
6374 /* '0' can also be a pointer */
6375 if (is_null_pointer(vtop
))
6377 /* accept implicit pointer to integer cast with warning */
6378 if (is_integer_btype(sbt
)) {
6379 warning("assignment makes pointer from integer without a cast");
6382 type1
= pointed_type(dt
);
6383 /* a function is implicitely a function pointer */
6384 if (sbt
== VT_FUNC
) {
6385 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
6386 !is_compatible_types(pointed_type(dt
), st
))
6393 type2
= pointed_type(st
);
6394 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
6395 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
6396 /* void * can match anything */
6398 /* exact type match, except for unsigned */
6401 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6402 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6403 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
6404 warning("assignment from incompatible pointer type");
6406 /* check const and volatile */
6407 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
6408 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
6409 warning("assignment discards qualifiers from pointer target type");
6415 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
6416 warning("assignment makes integer from pointer without a cast");
6418 /* XXX: more tests */
6423 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6424 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6425 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
6427 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
6428 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
6429 error("cannot cast '%s' to '%s'", buf1
, buf2
);
6437 /* store vtop in lvalue pushed on stack */
6440 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
6442 ft
= vtop
[-1].type
.t
;
6443 sbt
= vtop
->type
.t
& VT_BTYPE
;
6444 dbt
= ft
& VT_BTYPE
;
6445 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
6446 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
6447 /* optimize char/short casts */
6448 delayed_cast
= VT_MUSTCAST
;
6449 vtop
->type
.t
= ft
& VT_TYPE
;
6450 /* XXX: factorize */
6451 if (ft
& VT_CONSTANT
)
6452 warning("assignment of read-only location");
6455 if (!(ft
& VT_BITFIELD
))
6456 gen_assign_cast(&vtop
[-1].type
);
6459 if (sbt
== VT_STRUCT
) {
6460 /* if structure, only generate pointer */
6461 /* structure assignment : generate memcpy */
6462 /* XXX: optimize if small size */
6463 if (!nocode_wanted
) {
6464 size
= type_size(&vtop
->type
, &align
);
6468 vpush_global_sym(&func_old_type
, TOK_memcpy8
);
6469 else if(!(align
& 3))
6470 vpush_global_sym(&func_old_type
, TOK_memcpy4
);
6473 vpush_global_sym(&func_old_type
, TOK_memcpy
);
6477 vtop
->type
.t
= VT_INT
;
6481 vtop
->type
.t
= VT_INT
;
6493 /* leave source on stack */
6494 } else if (ft
& VT_BITFIELD
) {
6495 /* bitfield store handling */
6496 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
6497 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6498 /* remove bit field info to avoid loops */
6499 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
6501 /* duplicate source into other register */
6506 /* duplicate destination */
6508 vtop
[-1] = vtop
[-2];
6510 /* mask and shift source */
6511 vpushi((1 << bit_size
) - 1);
6515 /* load destination, mask and or with source */
6517 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
6523 /* pop off shifted source from "duplicate source..." above */
6527 #ifdef CONFIG_TCC_BCHECK
6528 /* bound check case */
6529 if (vtop
[-1].r
& VT_MUSTBOUND
) {
6535 if (!nocode_wanted
) {
6539 r
= gv(rc
); /* generate value */
6540 /* if lvalue was saved on stack, must read it */
6541 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
6543 t
= get_reg(RC_INT
);
6545 sv
.r
= VT_LOCAL
| VT_LVAL
;
6546 sv
.c
.ul
= vtop
[-1].c
.ul
;
6548 vtop
[-1].r
= t
| VT_LVAL
;
6551 /* two word case handling : store second register at word + 4 */
6552 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
6554 /* convert to int to increment easily */
6555 vtop
->type
.t
= VT_INT
;
6561 /* XXX: it works because r2 is spilled last ! */
6562 store(vtop
->r2
, vtop
- 1);
6566 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6567 vtop
->r
|= delayed_cast
;
6571 /* post defines POST/PRE add. c is the token ++ or -- */
6572 void inc(int post
, int c
)
6575 vdup(); /* save lvalue */
6577 gv_dup(); /* duplicate value */
6582 vpushi(c
- TOK_MID
);
6584 vstore(); /* store value */
6586 vpop(); /* if post op, return saved value */
6589 /* Parse GNUC __attribute__ extension. Currently, the following
6590 extensions are recognized:
6591 - aligned(n) : set data/function alignment.
6592 - packed : force data alignment to 1
6593 - section(x) : generate data/code in this section.
6594 - unused : currently ignored, but may be used someday.
6595 - regparm(n) : pass function parameters in registers (i386 only)
6597 static void parse_attribute(AttributeDef
*ad
)
6601 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
6605 while (tok
!= ')') {
6606 if (tok
< TOK_IDENT
)
6607 expect("attribute name");
6615 expect("section name");
6616 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
6625 if (n
<= 0 || (n
& (n
- 1)) != 0)
6626 error("alignment must be a positive power of two");
6639 /* currently, no need to handle it because tcc does not
6640 track unused objects */
6644 /* currently, no need to handle it because tcc does not
6645 track unused objects */
6650 FUNC_CALL(ad
->func_attr
) = FUNC_CDECL
;
6655 FUNC_CALL(ad
->func_attr
) = FUNC_STDCALL
;
6657 #ifdef TCC_TARGET_I386
6667 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALL1
+ n
- 1;
6673 FUNC_CALL(ad
->func_attr
) = FUNC_FASTCALLW
;
6677 FUNC_EXPORT(ad
->func_attr
) = 1;
6680 if (tcc_state
->warn_unsupported
)
6681 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
6682 /* skip parameters */
6684 int parenthesis
= 0;
6688 else if (tok
== ')')
6691 } while (parenthesis
&& tok
!= -1);
6704 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6705 static void struct_decl(CType
*type
, int u
)
6707 int a
, v
, size
, align
, maxalign
, c
, offset
;
6708 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
;
6709 Sym
*s
, *ss
, *ass
, **ps
;
6713 a
= tok
; /* save decl type */
6718 /* struct already defined ? return it */
6720 expect("struct/union/enum name");
6724 error("invalid type");
6731 /* we put an undefined size for struct/union */
6732 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
6733 s
->r
= 0; /* default alignment is zero as gcc */
6734 /* put struct/union/enum name in type */
6742 error("struct/union/enum already defined");
6743 /* cannot be empty */
6745 /* non empty enums are not allowed */
6746 if (a
== TOK_ENUM
) {
6750 expect("identifier");
6756 /* enum symbols have static storage */
6757 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
6758 ss
->type
.t
|= VT_STATIC
;
6763 /* NOTE: we accept a trailing comma */
6773 while (tok
!= '}') {
6774 parse_btype(&btype
, &ad
);
6780 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
| TYPE_ABSTRACT
);
6781 if (v
== 0 && (type1
.t
& VT_BTYPE
) != VT_STRUCT
)
6782 expect("identifier");
6783 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
6784 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
6785 error("invalid type for '%s'",
6786 get_tok_str(v
, NULL
));
6790 bit_size
= expr_const();
6791 /* XXX: handle v = 0 case for messages */
6793 error("negative width in bit-field '%s'",
6794 get_tok_str(v
, NULL
));
6795 if (v
&& bit_size
== 0)
6796 error("zero width for bit-field '%s'",
6797 get_tok_str(v
, NULL
));
6799 size
= type_size(&type1
, &align
);
6801 if (align
< ad
.aligned
)
6803 } else if (ad
.packed
) {
6805 } else if (*tcc_state
->pack_stack_ptr
) {
6806 if (align
> *tcc_state
->pack_stack_ptr
)
6807 align
= *tcc_state
->pack_stack_ptr
;
6810 if (bit_size
>= 0) {
6811 bt
= type1
.t
& VT_BTYPE
;
6817 error("bitfields must have scalar type");
6819 if (bit_size
> bsize
) {
6820 error("width of '%s' exceeds its type",
6821 get_tok_str(v
, NULL
));
6822 } else if (bit_size
== bsize
) {
6823 /* no need for bit fields */
6825 } else if (bit_size
== 0) {
6826 /* XXX: what to do if only padding in a
6828 /* zero size: means to pad */
6832 /* we do not have enough room ? */
6833 if ((bit_pos
+ bit_size
) > bsize
)
6836 /* XXX: handle LSB first */
6837 type1
.t
|= VT_BITFIELD
|
6838 (bit_pos
<< VT_STRUCT_SHIFT
) |
6839 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
6840 bit_pos
+= bit_size
;
6845 if (v
!= 0 || (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
6846 /* add new memory data only if starting
6848 if (lbit_pos
== 0) {
6849 if (a
== TOK_STRUCT
) {
6850 c
= (c
+ align
- 1) & -align
;
6859 if (align
> maxalign
)
6863 printf("add field %s offset=%d",
6864 get_tok_str(v
, NULL
), offset
);
6865 if (type1
.t
& VT_BITFIELD
) {
6866 printf(" pos=%d size=%d",
6867 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
6868 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
6873 if (v
== 0 && (type1
.t
& VT_BTYPE
) == VT_STRUCT
) {
6875 while ((ass
= ass
->next
) != NULL
) {
6876 ss
= sym_push(ass
->v
, &ass
->type
, 0, offset
+ ass
->c
);
6881 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
6885 if (tok
== ';' || tok
== TOK_EOF
)
6892 /* store size and alignment */
6893 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
6899 /* return 0 if no type declaration. otherwise, return the basic type
6902 static int parse_btype(CType
*type
, AttributeDef
*ad
)
6904 int t
, u
, type_found
, typespec_found
, typedef_found
;
6908 memset(ad
, 0, sizeof(AttributeDef
));
6916 /* currently, we really ignore extension */
6926 if ((t
& VT_BTYPE
) != 0)
6927 error("too many basic types");
6943 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
6944 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6945 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
6946 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
6960 if ((t
& VT_BTYPE
) == VT_LONG
) {
6961 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6968 struct_decl(&type1
, VT_ENUM
);
6971 type
->ref
= type1
.ref
;
6975 struct_decl(&type1
, VT_STRUCT
);
6978 /* type modifiers */
7031 /* GNUC attribute */
7032 case TOK_ATTRIBUTE1
:
7033 case TOK_ATTRIBUTE2
:
7034 parse_attribute(ad
);
7041 parse_expr_type(&type1
);
7044 if (typespec_found
|| typedef_found
)
7047 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
7050 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
7051 type
->ref
= s
->type
.ref
;
7059 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
7060 error("signed and unsigned modifier");
7061 if (tcc_state
->char_is_unsigned
) {
7062 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
7067 /* long is never used as type */
7068 if ((t
& VT_BTYPE
) == VT_LONG
)
7069 t
= (t
& ~VT_BTYPE
) | VT_INT
;
7074 /* convert a function parameter type (array to pointer and function to
7075 function pointer) */
7076 static inline void convert_parameter_type(CType
*pt
)
7078 /* remove const and volatile qualifiers (XXX: const could be used
7079 to indicate a const function parameter */
7080 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7081 /* array must be transformed to pointer according to ANSI C */
7083 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
7088 static void post_type(CType
*type
, AttributeDef
*ad
)
7090 int n
, l
, t1
, arg_size
, align
;
7091 Sym
**plast
, *s
, *first
;
7096 /* function declaration */
7104 /* read param name and compute offset */
7105 if (l
!= FUNC_OLD
) {
7106 if (!parse_btype(&pt
, &ad1
)) {
7108 error("invalid type");
7115 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
7117 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
7118 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
7119 error("parameter declared as void");
7120 arg_size
+= (type_size(&pt
, &align
) + 3) & ~3;
7125 expect("identifier");
7129 convert_parameter_type(&pt
);
7130 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
7136 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
7143 /* if no parameters, then old type prototype */
7147 t1
= type
->t
& VT_STORAGE
;
7148 /* NOTE: const is ignored in returned type as it has a special
7149 meaning in gcc / C++ */
7150 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
7151 post_type(type
, ad
);
7152 /* we push a anonymous symbol which will contain the function prototype */
7153 FUNC_ARGS(ad
->func_attr
) = arg_size
;
7154 s
= sym_push(SYM_FIELD
, type
, ad
->func_attr
, l
);
7156 type
->t
= t1
| VT_FUNC
;
7158 } else if (tok
== '[') {
7159 /* array definition */
7165 error("invalid array size");
7168 /* parse next post type */
7169 t1
= type
->t
& VT_STORAGE
;
7170 type
->t
&= ~VT_STORAGE
;
7171 post_type(type
, ad
);
7173 /* we push a anonymous symbol which will contain the array
7175 s
= sym_push(SYM_FIELD
, type
, 0, n
);
7176 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
7181 /* Parse a type declaration (except basic type), and return the type
7182 in 'type'. 'td' is a bitmask indicating which kind of type decl is
7183 expected. 'type' should contain the basic type. 'ad' is the
7184 attribute definition of the basic type. It can be modified by
7187 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
7190 CType type1
, *type2
;
7193 while (tok
== '*') {
7201 qualifiers
|= VT_CONSTANT
;
7206 qualifiers
|= VT_VOLATILE
;
7214 type
->t
|= qualifiers
;
7217 /* XXX: clarify attribute handling */
7218 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7219 parse_attribute(ad
);
7221 /* recursive type */
7222 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
7223 type1
.t
= 0; /* XXX: same as int */
7226 /* XXX: this is not correct to modify 'ad' at this point, but
7227 the syntax is not clear */
7228 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7229 parse_attribute(ad
);
7230 type_decl(&type1
, ad
, v
, td
);
7233 /* type identifier */
7234 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
7238 if (!(td
& TYPE_ABSTRACT
))
7239 expect("identifier");
7243 post_type(type
, ad
);
7244 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
7245 parse_attribute(ad
);
7248 /* append type at the end of type1 */
7261 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
7262 static int lvalue_type(int t
)
7267 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
7269 else if (bt
== VT_SHORT
)
7273 if (t
& VT_UNSIGNED
)
7274 r
|= VT_LVAL_UNSIGNED
;
7278 /* indirection with full error checking and bound check */
7279 static void indir(void)
7281 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
) {
7282 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FUNC
)
7286 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
7288 vtop
->type
= *pointed_type(&vtop
->type
);
7289 /* Arrays and functions are never lvalues */
7290 if (!(vtop
->type
.t
& VT_ARRAY
)
7291 && (vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7292 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7293 /* if bound checking, the referenced pointer must be checked */
7294 if (do_bounds_check
)
7295 vtop
->r
|= VT_MUSTBOUND
;
7299 /* pass a parameter to a function and do type checking and casting */
7300 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
7305 func_type
= func
->c
;
7306 if (func_type
== FUNC_OLD
||
7307 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
7308 /* default casting : only need to convert float to double */
7309 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
7313 } else if (arg
== NULL
) {
7314 error("too many arguments to function");
7317 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
7318 gen_assign_cast(&type
);
7322 /* parse an expression of the form '(type)' or '(expr)' and return its
7324 static void parse_expr_type(CType
*type
)
7330 if (parse_btype(type
, &ad
)) {
7331 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7338 static void parse_type(CType
*type
)
7343 if (!parse_btype(type
, &ad
)) {
7346 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7349 static void vpush_tokc(int t
)
7353 vsetc(&type
, VT_CONST
, &tokc
);
7356 static void unary(void)
7358 int n
, t
, align
, size
, r
;
7363 /* XXX: GCC 2.95.3 does not generate a table although it should be
7377 vpush_tokc(VT_INT
| VT_UNSIGNED
);
7381 vpush_tokc(VT_LLONG
);
7385 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
7389 vpush_tokc(VT_FLOAT
);
7393 vpush_tokc(VT_DOUBLE
);
7397 vpush_tokc(VT_LDOUBLE
);
7400 case TOK___FUNCTION__
:
7402 goto tok_identifier
;
7408 /* special function name identifier */
7409 len
= strlen(funcname
) + 1;
7410 /* generate char[len] type */
7415 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
7416 ptr
= section_ptr_add(data_section
, len
);
7417 memcpy(ptr
, funcname
, len
);
7422 #ifdef TCC_TARGET_PE
7423 t
= VT_SHORT
| VT_UNSIGNED
;
7429 /* string parsing */
7432 if (tcc_state
->warn_write_strings
)
7437 memset(&ad
, 0, sizeof(AttributeDef
));
7438 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
7443 if (parse_btype(&type
, &ad
)) {
7444 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
7446 /* check ISOC99 compound literal */
7448 /* data is allocated locally by default */
7453 /* all except arrays are lvalues */
7454 if (!(type
.t
& VT_ARRAY
))
7455 r
|= lvalue_type(type
.t
);
7456 memset(&ad
, 0, sizeof(AttributeDef
));
7457 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
7462 } else if (tok
== '{') {
7463 /* save all registers */
7465 /* statement expression : we do not accept break/continue
7466 inside as GCC does */
7467 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
7482 /* functions names must be treated as function pointers,
7483 except for unary '&' and sizeof. Since we consider that
7484 functions are not lvalues, we only have to handle it
7485 there and in function calls. */
7486 /* arrays can also be used although they are not lvalues */
7487 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
7488 !(vtop
->type
.t
& VT_ARRAY
) && !(vtop
->type
.t
& VT_LLOCAL
))
7490 mk_pointer(&vtop
->type
);
7496 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
)
7497 vtop
->c
.i
= !vtop
->c
.i
;
7498 else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
7499 vtop
->c
.i
= vtop
->c
.i
^ 1;
7502 vseti(VT_JMP
, gtst(1, 0));
7513 /* in order to force cast, we add zero */
7515 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
7516 error("pointer not accepted for unary plus");
7526 parse_expr_type(&type
);
7530 size
= type_size(&type
, &align
);
7531 if (t
== TOK_SIZEOF
) {
7533 error("sizeof applied to an incomplete type");
7538 vtop
->type
.t
|= VT_UNSIGNED
;
7541 case TOK_builtin_types_compatible_p
:
7550 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7551 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7552 vpushi(is_compatible_types(&type1
, &type2
));
7555 case TOK_builtin_constant_p
:
7557 int saved_nocode_wanted
, res
;
7560 saved_nocode_wanted
= nocode_wanted
;
7563 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
7565 nocode_wanted
= saved_nocode_wanted
;
7585 goto tok_identifier
;
7587 /* allow to take the address of a label */
7588 if (tok
< TOK_UIDENT
)
7589 expect("label identifier");
7590 s
= label_find(tok
);
7592 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
7594 if (s
->r
== LABEL_DECLARED
)
7595 s
->r
= LABEL_FORWARD
;
7598 s
->type
.t
= VT_VOID
;
7599 mk_pointer(&s
->type
);
7600 s
->type
.t
|= VT_STATIC
;
7602 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
7611 expect("identifier");
7615 error("'%s' undeclared", get_tok_str(t
, NULL
));
7616 /* for simple function calls, we tolerate undeclared
7617 external reference to int() function */
7618 if (tcc_state
->warn_implicit_function_declaration
)
7619 warning("implicit declaration of function '%s'",
7620 get_tok_str(t
, NULL
));
7621 s
= external_global_sym(t
, &func_old_type
, 0);
7623 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
7624 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
7625 /* if referencing an inline function, then we generate a
7626 symbol to it if not already done. It will have the
7627 effect to generate code for it at the end of the
7628 compilation unit. Inline function as always
7629 generated in the text section. */
7631 put_extern_sym(s
, text_section
, 0, 0);
7632 r
= VT_SYM
| VT_CONST
;
7636 vset(&s
->type
, r
, s
->c
);
7637 /* if forward reference, we must point to s */
7638 if (vtop
->r
& VT_SYM
) {
7645 /* post operations */
7647 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
7650 } else if (tok
== '.' || tok
== TOK_ARROW
) {
7652 if (tok
== TOK_ARROW
)
7657 /* expect pointer on structure */
7658 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
7659 expect("struct or union");
7663 while ((s
= s
->next
) != NULL
) {
7668 error("field not found: %s", get_tok_str(tok
& ~SYM_FIELD
, NULL
));
7669 /* add field offset to pointer */
7670 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
7673 /* change type to field type, and set to lvalue */
7674 vtop
->type
= s
->type
;
7675 /* an array is never an lvalue */
7676 if (!(vtop
->type
.t
& VT_ARRAY
)) {
7677 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7678 /* if bound checking, the referenced pointer must be checked */
7679 if (do_bounds_check
)
7680 vtop
->r
|= VT_MUSTBOUND
;
7683 } else if (tok
== '[') {
7689 } else if (tok
== '(') {
7695 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7696 /* pointer test (no array accepted) */
7697 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
7698 vtop
->type
= *pointed_type(&vtop
->type
);
7699 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7703 expect("function pointer");
7706 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
7708 /* get return type */
7711 sa
= s
->next
; /* first parameter */
7714 /* compute first implicit argument if a structure is returned */
7715 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
7716 /* get some space for the returned structure */
7717 size
= type_size(&s
->type
, &align
);
7718 loc
= (loc
- size
) & -align
;
7720 ret
.r
= VT_LOCAL
| VT_LVAL
;
7721 /* pass it as 'int' to avoid structure arg passing
7723 vseti(VT_LOCAL
, loc
);
7728 /* return in register */
7729 if (is_float(ret
.type
.t
)) {
7732 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
7741 gfunc_param_typed(s
, sa
);
7751 error("too few arguments to function");
7753 if (!nocode_wanted
) {
7754 gfunc_call(nb_args
);
7756 vtop
-= (nb_args
+ 1);
7759 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
7767 static void uneq(void)
7773 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
7774 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
7775 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
7790 static void expr_prod(void)
7795 while (tok
== '*' || tok
== '/' || tok
== '%') {
7803 static void expr_sum(void)
7808 while (tok
== '+' || tok
== '-') {
7816 static void expr_shift(void)
7821 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
7829 static void expr_cmp(void)
7834 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
7835 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
7843 static void expr_cmpeq(void)
7848 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
7856 static void expr_and(void)
7859 while (tok
== '&') {
7866 static void expr_xor(void)
7869 while (tok
== '^') {
7876 static void expr_or(void)
7879 while (tok
== '|') {
7886 /* XXX: fix this mess */
7887 static void expr_land_const(void)
7890 while (tok
== TOK_LAND
) {
7897 /* XXX: fix this mess */
7898 static void expr_lor_const(void)
7901 while (tok
== TOK_LOR
) {
7908 /* only used if non constant */
7909 static void expr_land(void)
7914 if (tok
== TOK_LAND
) {
7919 if (tok
!= TOK_LAND
) {
7929 static void expr_lor(void)
7934 if (tok
== TOK_LOR
) {
7939 if (tok
!= TOK_LOR
) {
7949 /* XXX: better constant handling */
7950 static void expr_eq(void)
7952 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
7954 CType type
, type1
, type2
;
7963 if (tok
== ':' && gnu_ext
) {
7979 if (vtop
!= vstack
) {
7980 /* needed to avoid having different registers saved in
7982 if (is_float(vtop
->type
.t
))
7989 if (tok
== ':' && gnu_ext
) {
7997 sv
= *vtop
; /* save value to handle it later */
7998 vtop
--; /* no vpop so that FP stack is not flushed */
8006 bt1
= t1
& VT_BTYPE
;
8008 bt2
= t2
& VT_BTYPE
;
8009 /* cast operands to correct type according to ISOC rules */
8010 if (is_float(bt1
) || is_float(bt2
)) {
8011 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
8012 type
.t
= VT_LDOUBLE
;
8013 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
8018 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
8019 /* cast to biggest op */
8021 /* convert to unsigned if it does not fit in a long long */
8022 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
8023 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
8024 type
.t
|= VT_UNSIGNED
;
8025 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
8026 /* XXX: test pointer compatibility */
8028 } else if (bt1
== VT_FUNC
|| bt2
== VT_FUNC
) {
8029 /* XXX: test function pointer compatibility */
8031 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
8032 /* XXX: test structure compatibility */
8034 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
8035 /* NOTE: as an extension, we accept void on only one side */
8038 /* integer operations */
8040 /* convert to unsigned if it does not fit in an integer */
8041 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
8042 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
8043 type
.t
|= VT_UNSIGNED
;
8046 /* now we convert second operand */
8048 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8051 if (is_float(type
.t
)) {
8053 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
8054 /* for long longs, we use fixed registers to avoid having
8055 to handle a complicated move */
8060 /* this is horrible, but we must also convert first
8064 /* put again first value and cast it */
8067 if (VT_STRUCT
== (vtop
->type
.t
& VT_BTYPE
))
8077 static void gexpr(void)
8088 /* parse an expression and return its type without any side effect. */
8089 static void expr_type(CType
*type
)
8091 int saved_nocode_wanted
;
8093 saved_nocode_wanted
= nocode_wanted
;
8098 nocode_wanted
= saved_nocode_wanted
;
8101 /* parse a unary expression and return its type without any side
8103 static void unary_type(CType
*type
)
8115 /* parse a constant expression and return value in vtop. */
8116 static void expr_const1(void)
8125 /* parse an integer constant and return its value. */
8126 static int expr_const(void)
8130 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
8131 expect("constant expression");
8137 /* return the label token if current token is a label, otherwise
8139 static int is_label(void)
8143 /* fast test first */
8144 if (tok
< TOK_UIDENT
)
8146 /* no need to save tokc because tok is an identifier */
8153 unget_tok(last_tok
);
8158 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
8159 int case_reg
, int is_expr
)
8164 /* generate line number info */
8166 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
8167 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
8169 last_line_num
= file
->line_num
;
8173 /* default return value is (void) */
8175 vtop
->type
.t
= VT_VOID
;
8178 if (tok
== TOK_IF
) {
8185 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8187 if (c
== TOK_ELSE
) {
8191 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
8192 gsym(d
); /* patch else jmp */
8195 } else if (tok
== TOK_WHILE
) {
8203 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8207 } else if (tok
== '{') {
8211 /* record local declaration stack position */
8213 llabel
= local_label_stack
;
8214 /* handle local labels declarations */
8215 if (tok
== TOK_LABEL
) {
8218 if (tok
< TOK_UIDENT
)
8219 expect("label identifier");
8220 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
8230 while (tok
!= '}') {
8235 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8238 /* pop locally defined labels */
8239 label_pop(&local_label_stack
, llabel
);
8240 /* pop locally defined symbols */
8241 sym_pop(&local_stack
, s
);
8243 } else if (tok
== TOK_RETURN
) {
8247 gen_assign_cast(&func_vt
);
8248 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
8250 /* if returning structure, must copy it to implicit
8251 first pointer arg location */
8254 size
= type_size(&func_vt
,&align
);
8257 if((vtop
->r
!= (VT_LOCAL
| VT_LVAL
) || (vtop
->c
.i
& 3))
8261 loc
= (loc
- size
) & -4;
8264 vset(&type
, VT_LOCAL
| VT_LVAL
, addr
);
8267 vset(&int_type
, VT_LOCAL
| VT_LVAL
, addr
);
8269 vtop
->type
= int_type
;
8275 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
8278 /* copy structure value to pointer */
8283 } else if (is_float(func_vt
.t
)) {
8288 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
8291 rsym
= gjmp(rsym
); /* jmp */
8292 } else if (tok
== TOK_BREAK
) {
8295 error("cannot break");
8296 *bsym
= gjmp(*bsym
);
8299 } else if (tok
== TOK_CONTINUE
) {
8302 error("cannot continue");
8303 *csym
= gjmp(*csym
);
8306 } else if (tok
== TOK_FOR
) {
8333 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8338 if (tok
== TOK_DO
) {
8343 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
8354 if (tok
== TOK_SWITCH
) {
8358 /* XXX: other types than integer */
8359 case_reg
= gv(RC_INT
);
8363 b
= gjmp(0); /* jump to first case */
8365 block(&a
, csym
, &b
, &c
, case_reg
, 0);
8366 /* if no default, jmp after switch */
8374 if (tok
== TOK_CASE
) {
8381 if (gnu_ext
&& tok
== TOK_DOTS
) {
8385 warning("empty case range");
8387 /* since a case is like a label, we must skip it with a jmp */
8394 *case_sym
= gtst(1, 0);
8397 *case_sym
= gtst(1, 0);
8401 *case_sym
= gtst(1, *case_sym
);
8406 goto block_after_label
;
8408 if (tok
== TOK_DEFAULT
) {
8414 error("too many 'default'");
8417 goto block_after_label
;
8419 if (tok
== TOK_GOTO
) {
8421 if (tok
== '*' && gnu_ext
) {
8425 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
8428 } else if (tok
>= TOK_UIDENT
) {
8429 s
= label_find(tok
);
8430 /* put forward definition if needed */
8432 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
8434 if (s
->r
== LABEL_DECLARED
)
8435 s
->r
= LABEL_FORWARD
;
8437 /* label already defined */
8438 if (s
->r
& LABEL_FORWARD
)
8439 s
->next
= (void *)gjmp((long)s
->next
);
8441 gjmp_addr((long)s
->next
);
8444 expect("label identifier");
8447 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
8455 if (s
->r
== LABEL_DEFINED
)
8456 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
8457 gsym((long)s
->next
);
8458 s
->r
= LABEL_DEFINED
;
8460 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
8462 s
->next
= (void *)ind
;
8463 /* we accept this, but it is a mistake */
8466 warning("deprecated use of label at end of compound statement");
8470 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8473 /* expression case */
8488 /* t is the array or struct type. c is the array or struct
8489 address. cur_index/cur_field is the pointer to the current
8490 value. 'size_only' is true if only size info is needed (only used
8492 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
8493 int *cur_index
, Sym
**cur_field
,
8497 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
8503 if (gnu_ext
&& (l
= is_label()) != 0)
8505 while (tok
== '[' || tok
== '.') {
8507 if (!(type
->t
& VT_ARRAY
))
8508 expect("array type");
8511 index
= expr_const();
8512 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
8513 expect("invalid index");
8514 if (tok
== TOK_DOTS
&& gnu_ext
) {
8516 index_last
= expr_const();
8517 if (index_last
< 0 ||
8518 (s
->c
>= 0 && index_last
>= s
->c
) ||
8520 expect("invalid index");
8526 *cur_index
= index_last
;
8527 type
= pointed_type(type
);
8528 elem_size
= type_size(type
, &align
);
8529 c
+= index
* elem_size
;
8530 /* NOTE: we only support ranges for last designator */
8531 nb_elems
= index_last
- index
+ 1;
8532 if (nb_elems
!= 1) {
8541 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
8542 expect("struct/union type");
8555 /* XXX: fix this mess by using explicit storage field */
8557 type1
.t
|= (type
->t
& ~VT_TYPE
);
8571 if (type
->t
& VT_ARRAY
) {
8573 type
= pointed_type(type
);
8574 c
+= index
* type_size(type
, &align
);
8578 error("too many field init");
8579 /* XXX: fix this mess by using explicit storage field */
8581 type1
.t
|= (type
->t
& ~VT_TYPE
);
8586 decl_initializer(type
, sec
, c
, 0, size_only
);
8588 /* XXX: make it more general */
8589 if (!size_only
&& nb_elems
> 1) {
8590 unsigned long c_end
;
8595 error("range init not supported yet for dynamic storage");
8596 c_end
= c
+ nb_elems
* elem_size
;
8597 if (c_end
> sec
->data_allocated
)
8598 section_realloc(sec
, c_end
);
8599 src
= sec
->data
+ c
;
8601 for(i
= 1; i
< nb_elems
; i
++) {
8603 memcpy(dst
, src
, elem_size
);
8609 #define EXPR_CONST 1
8612 /* store a value or an expression directly in global data or in local array */
8613 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
8614 int v
, int expr_type
)
8616 int saved_global_expr
, bt
, bit_pos
, bit_size
;
8618 unsigned long long bit_mask
;
8626 /* compound literals must be allocated globally in this case */
8627 saved_global_expr
= global_expr
;
8630 global_expr
= saved_global_expr
;
8631 /* NOTE: symbols are accepted */
8632 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
8633 error("initializer element is not constant");
8641 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
8644 /* XXX: not portable */
8645 /* XXX: generate error if incorrect relocation */
8646 gen_assign_cast(&dtype
);
8647 bt
= type
->t
& VT_BTYPE
;
8648 ptr
= sec
->data
+ c
;
8649 /* XXX: make code faster ? */
8650 if (!(type
->t
& VT_BITFIELD
)) {
8655 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
8656 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
8657 bit_mask
= (1LL << bit_size
) - 1;
8659 if ((vtop
->r
& VT_SYM
) &&
8665 (bt
== VT_INT
&& bit_size
!= 32)))
8666 error("initializer element is not computable at load time");
8669 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8672 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8675 *(double *)ptr
= vtop
->c
.d
;
8678 *(long double *)ptr
= vtop
->c
.ld
;
8681 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
8684 if (vtop
->r
& VT_SYM
) {
8685 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
8687 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8692 vset(&dtype
, VT_LOCAL
|VT_LVAL
, c
);
8699 /* put zeros for variable based init */
8700 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
8703 /* nothing to do because globals are already set to zero */
8705 vpush_global_sym(&func_old_type
, TOK_memset
);
8713 /* 't' contains the type and storage info. 'c' is the offset of the
8714 object in section 'sec'. If 'sec' is NULL, it means stack based
8715 allocation. 'first' is true if array '{' must be read (multi
8716 dimension implicit array init handling). 'size_only' is true if
8717 size only evaluation is wanted (only for arrays). */
8718 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
8719 int first
, int size_only
)
8721 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
8722 int size1
, align1
, expr_type
;
8726 if (type
->t
& VT_ARRAY
) {
8730 t1
= pointed_type(type
);
8731 size1
= type_size(t1
, &align1
);
8734 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
8740 /* only parse strings here if correct type (otherwise: handle
8741 them as ((w)char *) expressions */
8742 if ((tok
== TOK_LSTR
&&
8743 #ifdef TCC_TARGET_PE
8744 (t1
->t
& VT_BTYPE
) == VT_SHORT
&& (t1
->t
& VT_UNSIGNED
)
8746 (t1
->t
& VT_BTYPE
) == VT_INT
8748 ) || (tok
== TOK_STR
&& (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
8749 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8754 /* compute maximum number of chars wanted */
8756 cstr_len
= cstr
->size
;
8758 cstr_len
= cstr
->size
/ sizeof(nwchar_t
);
8761 if (n
>= 0 && nb
> (n
- array_length
))
8762 nb
= n
- array_length
;
8765 warning("initializer-string for array is too long");
8766 /* in order to go faster for common case (char
8767 string in global variable, we handle it
8769 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
8770 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
8774 ch
= ((unsigned char *)cstr
->data
)[i
];
8776 ch
= ((nwchar_t
*)cstr
->data
)[i
];
8777 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
8785 /* only add trailing zero if enough storage (no
8786 warning in this case since it is standard) */
8787 if (n
< 0 || array_length
< n
) {
8789 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
8795 while (tok
!= '}') {
8796 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
8797 if (n
>= 0 && index
>= n
)
8798 error("index too large");
8799 /* must put zero in holes (note that doing it that way
8800 ensures that it even works with designators) */
8801 if (!size_only
&& array_length
< index
) {
8802 init_putz(t1
, sec
, c
+ array_length
* size1
,
8803 (index
- array_length
) * size1
);
8806 if (index
> array_length
)
8807 array_length
= index
;
8808 /* special test for multi dimensional arrays (may not
8809 be strictly correct if designators are used at the
8811 if (index
>= n
&& no_oblock
)
8820 /* put zeros at the end */
8821 if (!size_only
&& n
>= 0 && array_length
< n
) {
8822 init_putz(t1
, sec
, c
+ array_length
* size1
,
8823 (n
- array_length
) * size1
);
8825 /* patch type size if needed */
8827 s
->c
= array_length
;
8828 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
8829 (sec
|| !first
|| tok
== '{')) {
8832 /* NOTE: the previous test is a specific case for automatic
8833 struct/union init */
8834 /* XXX: union needs only one init */
8836 /* XXX: this test is incorrect for local initializers
8837 beginning with ( without {. It would be much more difficult
8838 to do it correctly (ideally, the expression parser should
8839 be used in all cases) */
8845 while (tok
== '(') {
8849 if (!parse_btype(&type1
, &ad1
))
8851 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
8853 if (!is_assignable_types(type
, &type1
))
8854 error("invalid type for cast");
8859 if (first
|| tok
== '{') {
8868 while (tok
!= '}') {
8869 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
8871 if (!size_only
&& array_length
< index
) {
8872 init_putz(type
, sec
, c
+ array_length
,
8873 index
- array_length
);
8875 index
= index
+ type_size(&f
->type
, &align1
);
8876 if (index
> array_length
)
8877 array_length
= index
;
8879 if (no_oblock
&& f
== NULL
)
8885 /* put zeros at the end */
8886 if (!size_only
&& array_length
< n
) {
8887 init_putz(type
, sec
, c
+ array_length
,
8896 } else if (tok
== '{') {
8898 decl_initializer(type
, sec
, c
, first
, size_only
);
8900 } else if (size_only
) {
8901 /* just skip expression */
8903 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
8907 else if (tok
== ')')
8912 /* currently, we always use constant expression for globals
8913 (may change for scripting case) */
8914 expr_type
= EXPR_CONST
;
8916 expr_type
= EXPR_ANY
;
8917 init_putv(type
, sec
, c
, 0, expr_type
);
8921 /* parse an initializer for type 't' if 'has_init' is non zero, and
8922 allocate space in local or global data space ('r' is either
8923 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8924 variable 'v' of scope 'scope' is declared before initializers are
8925 parsed. If 'v' is zero, then a reference to the new object is put
8926 in the value stack. If 'has_init' is 2, a special parsing is done
8927 to handle string constants. */
8928 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
8929 int has_init
, int v
, int scope
)
8931 int size
, align
, addr
, data_offset
;
8933 ParseState saved_parse_state
;
8934 TokenString init_str
;
8937 size
= type_size(type
, &align
);
8938 /* If unknown size, we must evaluate it before
8939 evaluating initializers because
8940 initializers can generate global data too
8941 (e.g. string pointers or ISOC99 compound
8942 literals). It also simplifies local
8943 initializers handling */
8944 tok_str_new(&init_str
);
8947 error("unknown type size");
8948 /* get all init string */
8949 if (has_init
== 2) {
8950 /* only get strings */
8951 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8952 tok_str_add_tok(&init_str
);
8957 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
8959 error("unexpected end of file in initializer");
8960 tok_str_add_tok(&init_str
);
8963 else if (tok
== '}') {
8971 tok_str_add(&init_str
, -1);
8972 tok_str_add(&init_str
, 0);
8975 save_parse_state(&saved_parse_state
);
8977 macro_ptr
= init_str
.str
;
8979 decl_initializer(type
, NULL
, 0, 1, 1);
8980 /* prepare second initializer parsing */
8981 macro_ptr
= init_str
.str
;
8984 /* if still unknown size, error */
8985 size
= type_size(type
, &align
);
8987 error("unknown type size");
8989 /* take into account specified alignment if bigger */
8991 if (ad
->aligned
> align
)
8992 align
= ad
->aligned
;
8993 } else if (ad
->packed
) {
8996 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
8998 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
9000 loc
= (loc
- size
) & -align
;
9002 /* handles bounds */
9003 /* XXX: currently, since we do only one pass, we cannot track
9004 '&' operators, so we add only arrays */
9005 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
9006 unsigned long *bounds_ptr
;
9007 /* add padding between regions */
9009 /* then add local bound info */
9010 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
9011 bounds_ptr
[0] = addr
;
9012 bounds_ptr
[1] = size
;
9015 /* local variable */
9016 sym_push(v
, type
, r
, addr
);
9018 /* push local reference */
9019 vset(type
, r
, addr
);
9025 if (v
&& scope
== VT_CONST
) {
9026 /* see if the symbol was already defined */
9029 if (!is_compatible_types(&sym
->type
, type
))
9030 error("incompatible types for redefinition of '%s'",
9031 get_tok_str(v
, NULL
));
9032 if (sym
->type
.t
& VT_EXTERN
) {
9033 /* if the variable is extern, it was not allocated */
9034 sym
->type
.t
&= ~VT_EXTERN
;
9035 /* set array size if it was ommited in extern
9037 if ((sym
->type
.t
& VT_ARRAY
) &&
9038 sym
->type
.ref
->c
< 0 &&
9040 sym
->type
.ref
->c
= type
->ref
->c
;
9042 /* we accept several definitions of the same
9043 global variable. this is tricky, because we
9044 must play with the SHN_COMMON type of the symbol */
9045 /* XXX: should check if the variable was already
9046 initialized. It is incorrect to initialized it
9048 /* no init data, we won't add more to the symbol */
9055 /* allocate symbol in corresponding section */
9060 else if (tcc_state
->nocommon
)
9064 data_offset
= sec
->data_offset
;
9065 data_offset
= (data_offset
+ align
- 1) & -align
;
9067 /* very important to increment global pointer at this time
9068 because initializers themselves can create new initializers */
9069 data_offset
+= size
;
9070 /* add padding if bound check */
9071 if (do_bounds_check
)
9073 sec
->data_offset
= data_offset
;
9074 /* allocate section space to put the data */
9075 if (sec
->sh_type
!= SHT_NOBITS
&&
9076 data_offset
> sec
->data_allocated
)
9077 section_realloc(sec
, data_offset
);
9078 /* align section if needed */
9079 if (align
> sec
->sh_addralign
)
9080 sec
->sh_addralign
= align
;
9082 addr
= 0; /* avoid warning */
9086 if (scope
!= VT_CONST
|| !sym
) {
9087 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
9089 /* update symbol definition */
9091 put_extern_sym(sym
, sec
, addr
, size
);
9094 /* put a common area */
9095 put_extern_sym(sym
, NULL
, align
, size
);
9096 /* XXX: find a nicer way */
9097 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
9098 esym
->st_shndx
= SHN_COMMON
;
9103 /* push global reference */
9104 sym
= get_sym_ref(type
, sec
, addr
, size
);
9106 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
9110 /* handles bounds now because the symbol must be defined
9111 before for the relocation */
9112 if (do_bounds_check
) {
9113 unsigned long *bounds_ptr
;
9115 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
9116 /* then add global bound info */
9117 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
9118 bounds_ptr
[0] = 0; /* relocated */
9119 bounds_ptr
[1] = size
;
9123 decl_initializer(type
, sec
, addr
, 1, 0);
9124 /* restore parse state if needed */
9126 tok_str_free(init_str
.str
);
9127 restore_parse_state(&saved_parse_state
);
9133 void put_func_debug(Sym
*sym
)
9138 /* XXX: we put here a dummy type */
9139 snprintf(buf
, sizeof(buf
), "%s:%c1",
9140 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
9141 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
9142 cur_text_section
, sym
->c
);
9147 /* parse an old style function declaration list */
9148 /* XXX: check multiple parameter */
9149 static void func_decl_list(Sym
*func_sym
)
9156 /* parse each declaration */
9157 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
9158 if (!parse_btype(&btype
, &ad
))
9159 expect("declaration list");
9160 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9161 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9163 /* we accept no variable after */
9167 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9168 /* find parameter in function parameter list */
9171 if ((s
->v
& ~SYM_FIELD
) == v
)
9175 error("declaration for parameter '%s' but no such parameter",
9176 get_tok_str(v
, NULL
));
9178 /* check that no storage specifier except 'register' was given */
9179 if (type
.t
& VT_STORAGE
)
9180 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
9181 convert_parameter_type(&type
);
9182 /* we can add the type (NOTE: it could be local to the function) */
9184 /* accept other parameters */
9195 /* parse a function defined by symbol 'sym' and generate its code in
9196 'cur_text_section' */
9197 static void gen_function(Sym
*sym
)
9199 int saved_nocode_wanted
= nocode_wanted
;
9201 ind
= cur_text_section
->data_offset
;
9202 /* NOTE: we patch the symbol size later */
9203 put_extern_sym(sym
, cur_text_section
, ind
, 0);
9204 funcname
= get_tok_str(sym
->v
, NULL
);
9206 /* put debug symbol */
9208 put_func_debug(sym
);
9209 /* push a dummy symbol to enable local sym storage */
9210 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
9211 gfunc_prolog(&sym
->type
);
9213 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
9216 cur_text_section
->data_offset
= ind
;
9217 label_pop(&global_label_stack
, NULL
);
9218 sym_pop(&local_stack
, NULL
); /* reset local stack */
9219 /* end of function */
9220 /* patch symbol size */
9221 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_size
=
9224 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
9226 funcname
= ""; /* for safety */
9227 func_vt
.t
= VT_VOID
; /* for safety */
9228 ind
= 0; /* for safety */
9229 nocode_wanted
= saved_nocode_wanted
;
9232 static void gen_inline_functions(void)
9236 int *str
, inline_generated
;
9238 /* iterate while inline function are referenced */
9240 inline_generated
= 0;
9241 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9243 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9244 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9245 (VT_STATIC
| VT_INLINE
) &&
9247 /* the function was used: generate its code and
9248 convert it to a normal function */
9249 str
= INLINE_DEF(sym
->r
);
9250 sym
->r
= VT_SYM
| VT_CONST
;
9251 sym
->type
.t
&= ~VT_INLINE
;
9255 cur_text_section
= text_section
;
9257 macro_ptr
= NULL
; /* fail safe */
9260 inline_generated
= 1;
9263 if (!inline_generated
)
9267 /* free all remaining inline function tokens */
9268 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
9270 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
9271 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
9272 (VT_STATIC
| VT_INLINE
)) {
9273 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
9274 if (sym
->r
== (VT_SYM
| VT_CONST
)) //gr beware!
9276 str
= INLINE_DEF(sym
->r
);
9278 sym
->r
= 0; /* fail safe */
9283 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
9284 static void decl(int l
)
9292 if (!parse_btype(&btype
, &ad
)) {
9293 /* skip redundant ';' */
9294 /* XXX: find more elegant solution */
9299 if (l
== VT_CONST
&&
9300 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
9301 /* global asm block */
9305 /* special test for old K&R protos without explicit int
9306 type. Only accepted when defining global data */
9307 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
9311 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
9312 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
9314 /* we accept no variable after */
9318 while (1) { /* iterate thru each declaration */
9320 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
9324 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
9325 printf("type = '%s'\n", buf
);
9328 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9329 /* if old style function prototype, we accept a
9332 if (sym
->c
== FUNC_OLD
)
9333 func_decl_list(sym
);
9338 error("cannot use local functions");
9339 if ((type
.t
& VT_BTYPE
) != VT_FUNC
)
9340 expect("function definition");
9342 /* reject abstract declarators in function definition */
9344 while ((sym
= sym
->next
) != NULL
)
9345 if (!(sym
->v
& ~SYM_FIELD
))
9346 expect("identifier");
9348 /* XXX: cannot do better now: convert extern line to static inline */
9349 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
9350 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
9354 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
9356 /* specific case: if not func_call defined, we put
9357 the one of the prototype */
9358 /* XXX: should have default value */
9359 r
= sym
->type
.ref
->r
;
9360 if (FUNC_CALL(r
) != FUNC_CDECL
9361 && FUNC_CALL(type
.ref
->r
) == FUNC_CDECL
)
9362 FUNC_CALL(type
.ref
->r
) = FUNC_CALL(r
);
9364 FUNC_EXPORT(type
.ref
->r
) = 1;
9366 if (!is_compatible_types(&sym
->type
, &type
)) {
9368 error("incompatible types for redefinition of '%s'",
9369 get_tok_str(v
, NULL
));
9371 /* if symbol is already defined, then put complete type */
9374 /* put function symbol */
9375 sym
= global_identifier_push(v
, type
.t
, 0);
9376 sym
->type
.ref
= type
.ref
;
9379 /* static inline functions are just recorded as a kind
9380 of macro. Their code will be emitted at the end of
9381 the compilation unit only if they are used */
9382 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
9383 (VT_INLINE
| VT_STATIC
)) {
9384 TokenString func_str
;
9387 tok_str_new(&func_str
);
9393 error("unexpected end of file");
9394 tok_str_add_tok(&func_str
);
9399 } else if (t
== '}') {
9401 if (block_level
== 0)
9405 tok_str_add(&func_str
, -1);
9406 tok_str_add(&func_str
, 0);
9407 INLINE_DEF(sym
->r
) = func_str
.str
;
9409 /* compute text section */
9410 cur_text_section
= ad
.section
;
9411 if (!cur_text_section
)
9412 cur_text_section
= text_section
;
9413 sym
->r
= VT_SYM
| VT_CONST
;
9418 if (btype
.t
& VT_TYPEDEF
) {
9419 /* save typedefed type */
9420 /* XXX: test storage specifiers ? */
9421 sym
= sym_push(v
, &type
, 0, 0);
9422 sym
->type
.t
|= VT_TYPEDEF
;
9423 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9424 /* external function definition */
9425 /* specific case for func_call attribute */
9427 type
.ref
->r
= ad
.func_attr
;
9428 external_sym(v
, &type
, 0);
9430 /* not lvalue if array */
9432 if (!(type
.t
& VT_ARRAY
))
9433 r
|= lvalue_type(type
.t
);
9434 has_init
= (tok
== '=');
9435 if ((btype
.t
& VT_EXTERN
) ||
9436 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
9437 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
9438 /* external variable */
9439 /* NOTE: as GCC, uninitialized global static
9440 arrays of null size are considered as
9442 external_sym(v
, &type
, r
);
9444 type
.t
|= (btype
.t
& VT_STATIC
); /* Retain "static". */
9445 if (type
.t
& VT_STATIC
)
9451 decl_initializer_alloc(&type
, &ad
, r
,
9465 /* better than nothing, but needs extension to handle '-E' option
9467 static void preprocess_init(TCCState
*s1
)
9469 s1
->include_stack_ptr
= s1
->include_stack
;
9470 /* XXX: move that before to avoid having to initialize
9471 file->ifdef_stack_ptr ? */
9472 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
9473 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
9475 /* XXX: not ANSI compliant: bound checking says error */
9477 s1
->pack_stack
[0] = 0;
9478 s1
->pack_stack_ptr
= s1
->pack_stack
;
9481 /* compile the C file opened in 'file'. Return non zero if errors. */
9482 static int tcc_compile(TCCState
*s1
)
9486 volatile int section_sym
;
9489 printf("%s: **** new file\n", file
->filename
);
9491 preprocess_init(s1
);
9494 anon_sym
= SYM_FIRST_ANOM
;
9496 /* file info: full path + filename */
9497 section_sym
= 0; /* avoid warning */
9499 section_sym
= put_elf_sym(symtab_section
, 0, 0,
9500 ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
), 0,
9501 text_section
->sh_num
, NULL
);
9502 getcwd(buf
, sizeof(buf
));
9504 normalize_slashes(buf
);
9506 pstrcat(buf
, sizeof(buf
), "/");
9507 put_stabs_r(buf
, N_SO
, 0, 0,
9508 text_section
->data_offset
, text_section
, section_sym
);
9509 put_stabs_r(file
->filename
, N_SO
, 0, 0,
9510 text_section
->data_offset
, text_section
, section_sym
);
9512 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9513 symbols can be safely used */
9514 put_elf_sym(symtab_section
, 0, 0,
9515 ELF32_ST_INFO(STB_LOCAL
, STT_FILE
), 0,
9516 SHN_ABS
, file
->filename
);
9518 /* define some often used types */
9519 int_type
.t
= VT_INT
;
9521 char_pointer_type
.t
= VT_BYTE
;
9522 mk_pointer(&char_pointer_type
);
9524 func_old_type
.t
= VT_FUNC
;
9525 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
9527 #if defined(TCC_ARM_EABI) && defined(TCC_ARM_VFP)
9528 float_type
.t
= VT_FLOAT
;
9529 double_type
.t
= VT_DOUBLE
;
9531 func_float_type
.t
= VT_FUNC
;
9532 func_float_type
.ref
= sym_push(SYM_FIELD
, &float_type
, FUNC_CDECL
, FUNC_OLD
);
9533 func_double_type
.t
= VT_FUNC
;
9534 func_double_type
.ref
= sym_push(SYM_FIELD
, &double_type
, FUNC_CDECL
, FUNC_OLD
);
9538 /* define 'void *alloca(unsigned int)' builtin function */
9543 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
9544 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
9547 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
9551 define_start
= define_stack
;
9554 if (setjmp(s1
->error_jmp_buf
) == 0) {
9556 s1
->error_set_jmp_enabled
= 1;
9558 ch
= file
->buf_ptr
[0];
9559 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9560 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
;
9564 expect("declaration");
9566 /* end of translation unit info */
9568 put_stabs_r(NULL
, N_SO
, 0, 0,
9569 text_section
->data_offset
, text_section
, section_sym
);
9572 s1
->error_set_jmp_enabled
= 0;
9574 /* reset define stack, but leave -Dsymbols (may be incorrect if
9575 they are undefined) */
9576 free_defines(define_start
);
9578 gen_inline_functions();
9580 sym_pop(&global_stack
, NULL
);
9582 return s1
->nb_errors
!= 0 ? -1 : 0;
9585 /* Preprocess the current file */
9586 /* XXX: add line and file infos, add options to preserve spaces */
9587 static int tcc_preprocess(TCCState
*s1
)
9592 preprocess_init(s1
);
9594 define_start
= define_stack
;
9596 ch
= file
->buf_ptr
[0];
9597 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9598 parse_flags
= PARSE_FLAG_ASM_COMMENTS
| PARSE_FLAG_PREPROCESS
|
9599 PARSE_FLAG_LINEFEED
;
9603 if (tok
== TOK_EOF
) {
9605 } else if (tok
== TOK_LINEFEED
) {
9609 fputc(' ', s1
->outfile
);
9612 fputs(get_tok_str(tok
, &tokc
), s1
->outfile
);
9615 free_defines(define_start
);
9620 int tcc_compile_string(TCCState
*s
, const char *str
)
9622 BufferedFile bf1
, *bf
= &bf1
;
9626 /* init file structure */
9628 /* XXX: avoid copying */
9630 buf
= tcc_malloc(len
+ 1);
9633 memcpy(buf
, str
, len
);
9636 bf
->buf_end
= buf
+ len
;
9637 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
9641 ret
= tcc_compile(s
);
9645 /* currently, no need to close */
9650 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9651 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
9653 BufferedFile bf1
, *bf
= &bf1
;
9655 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
9656 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
9660 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
9662 /* init file structure */
9664 bf
->buf_ptr
= bf
->buffer
;
9665 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
9666 *bf
->buf_end
= CH_EOB
;
9667 bf
->filename
[0] = '\0';
9671 s1
->include_stack_ptr
= s1
->include_stack
;
9673 /* parse with define parser */
9674 ch
= file
->buf_ptr
[0];
9680 /* undefine a preprocessor symbol */
9681 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
9685 ts
= tok_alloc(sym
, strlen(sym
));
9686 s
= define_find(ts
->tok
);
9687 /* undefine symbol by putting an invalid name */
9692 #ifdef CONFIG_TCC_ASM
9694 #ifdef TCC_TARGET_I386
9695 #include "i386-asm.c"
9700 static void asm_instr(void)
9702 error("inline asm() not supported");
9704 static void asm_global_instr(void)
9706 error("inline asm() not supported");
9712 #ifdef TCC_TARGET_COFF
9713 #include "tcccoff.c"
9716 #ifdef TCC_TARGET_PE
9720 /* print the position in the source file of PC value 'pc' by reading
9721 the stabs debug information */
9722 static void rt_printline(unsigned long wanted_pc
)
9724 Stab_Sym
*sym
, *sym_end
;
9725 char func_name
[128], last_func_name
[128];
9726 unsigned long func_addr
, last_pc
, pc
;
9727 const char *incl_files
[INCLUDE_STACK_SIZE
];
9728 int incl_index
, len
, last_line_num
, i
;
9729 const char *str
, *p
;
9731 fprintf(stderr
, "0x%08lx:", wanted_pc
);
9733 func_name
[0] = '\0';
9736 last_func_name
[0] = '\0';
9737 last_pc
= 0xffffffff;
9739 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
9740 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
9741 while (sym
< sym_end
) {
9742 switch(sym
->n_type
) {
9743 /* function start or end */
9745 if (sym
->n_strx
== 0) {
9746 /* we test if between last line and end of function */
9747 pc
= sym
->n_value
+ func_addr
;
9748 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9750 func_name
[0] = '\0';
9753 str
= stabstr_section
->data
+ sym
->n_strx
;
9754 p
= strchr(str
, ':');
9756 pstrcpy(func_name
, sizeof(func_name
), str
);
9759 if (len
> sizeof(func_name
) - 1)
9760 len
= sizeof(func_name
) - 1;
9761 memcpy(func_name
, str
, len
);
9762 func_name
[len
] = '\0';
9764 func_addr
= sym
->n_value
;
9767 /* line number info */
9769 pc
= sym
->n_value
+ func_addr
;
9770 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9773 last_line_num
= sym
->n_desc
;
9775 strcpy(last_func_name
, func_name
);
9779 str
= stabstr_section
->data
+ sym
->n_strx
;
9781 if (incl_index
< INCLUDE_STACK_SIZE
) {
9782 incl_files
[incl_index
++] = str
;
9790 if (sym
->n_strx
== 0) {
9791 incl_index
= 0; /* end of translation unit */
9793 str
= stabstr_section
->data
+ sym
->n_strx
;
9794 /* do not add path */
9796 if (len
> 0 && str
[len
- 1] != '/')
9804 /* second pass: we try symtab symbols (no line number info) */
9807 Elf32_Sym
*sym
, *sym_end
;
9810 sym_end
= (Elf32_Sym
*)(symtab_section
->data
+ symtab_section
->data_offset
);
9811 for(sym
= (Elf32_Sym
*)symtab_section
->data
+ 1;
9814 type
= ELF32_ST_TYPE(sym
->st_info
);
9815 if (type
== STT_FUNC
) {
9816 if (wanted_pc
>= sym
->st_value
&&
9817 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
9818 pstrcpy(last_func_name
, sizeof(last_func_name
),
9819 strtab_section
->data
+ sym
->st_name
);
9825 /* did not find any info: */
9826 fprintf(stderr
, " ???\n");
9829 if (last_func_name
[0] != '\0') {
9830 fprintf(stderr
, " %s()", last_func_name
);
9832 if (incl_index
> 0) {
9833 fprintf(stderr
, " (%s:%d",
9834 incl_files
[incl_index
- 1], last_line_num
);
9835 for(i
= incl_index
- 2; i
>= 0; i
--)
9836 fprintf(stderr
, ", included from %s", incl_files
[i
]);
9837 fprintf(stderr
, ")");
9839 fprintf(stderr
, "\n");
9842 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
9846 /* fix for glibc 2.1 */
9852 /* return the PC at frame level 'level'. Return non zero if not found */
9853 static int rt_get_caller_pc(unsigned long *paddr
,
9854 ucontext_t
*uc
, int level
)
9860 #if defined(__FreeBSD__)
9861 *paddr
= uc
->uc_mcontext
.mc_eip
;
9862 #elif defined(__dietlibc__)
9863 *paddr
= uc
->uc_mcontext
.eip
;
9865 *paddr
= uc
->uc_mcontext
.gregs
[REG_EIP
];
9869 #if defined(__FreeBSD__)
9870 fp
= uc
->uc_mcontext
.mc_ebp
;
9871 #elif defined(__dietlibc__)
9872 fp
= uc
->uc_mcontext
.ebp
;
9874 fp
= uc
->uc_mcontext
.gregs
[REG_EBP
];
9876 for(i
=1;i
<level
;i
++) {
9877 /* XXX: check address validity with program info */
9878 if (fp
<= 0x1000 || fp
>= 0xc0000000)
9880 fp
= ((unsigned long *)fp
)[0];
9882 *paddr
= ((unsigned long *)fp
)[1];
9888 #warning add arch specific rt_get_caller_pc()
9890 static int rt_get_caller_pc(unsigned long *paddr
,
9891 ucontext_t
*uc
, int level
)
9897 /* emit a run time error at position 'pc' */
9898 void rt_error(ucontext_t
*uc
, const char *fmt
, ...)
9905 fprintf(stderr
, "Runtime error: ");
9906 vfprintf(stderr
, fmt
, ap
);
9907 fprintf(stderr
, "\n");
9908 for(i
=0;i
<num_callers
;i
++) {
9909 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
9912 fprintf(stderr
, "at ");
9914 fprintf(stderr
, "by ");
9921 /* signal handler for fatal errors */
9922 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
9924 ucontext_t
*uc
= puc
;
9928 switch(siginf
->si_code
) {
9931 rt_error(uc
, "division by zero");
9934 rt_error(uc
, "floating point exception");
9940 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
9941 rt_error(uc
, *rt_bound_error_msg
);
9943 rt_error(uc
, "dereferencing invalid pointer");
9946 rt_error(uc
, "illegal instruction");
9949 rt_error(uc
, "abort() called");
9952 rt_error(uc
, "caught signal %d", signum
);
9959 /* do all relocations (needed before using tcc_get_symbol()) */
9960 int tcc_relocate(TCCState
*s1
)
9967 #ifdef TCC_TARGET_PE
9970 tcc_add_runtime(s1
);
9973 relocate_common_syms();
9975 tcc_add_linker_symbols(s1
);
9976 #ifndef TCC_TARGET_PE
9977 build_got_entries(s1
);
9979 /* compute relocation address : section are relocated in place. We
9980 also alloc the bss space */
9981 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9982 s
= s1
->sections
[i
];
9983 if (s
->sh_flags
& SHF_ALLOC
) {
9984 if (s
->sh_type
== SHT_NOBITS
)
9985 s
->data
= tcc_mallocz(s
->data_offset
);
9986 s
->sh_addr
= (unsigned long)s
->data
;
9990 relocate_syms(s1
, 1);
9992 if (s1
->nb_errors
!= 0)
9995 /* relocate each section */
9996 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9997 s
= s1
->sections
[i
];
9999 relocate_section(s1
, s
);
10002 /* mark executable sections as executable in memory */
10003 for(i
= 1; i
< s1
->nb_sections
; i
++) {
10004 s
= s1
->sections
[i
];
10005 if ((s
->sh_flags
& (SHF_ALLOC
| SHF_EXECINSTR
)) ==
10006 (SHF_ALLOC
| SHF_EXECINSTR
))
10007 set_pages_executable(s
->data
, s
->data_offset
);
10012 /* launch the compiled program with the given arguments */
10013 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
10015 int (*prog_main
)(int, char **);
10017 if (tcc_relocate(s1
) < 0)
10020 prog_main
= tcc_get_symbol_err(s1
, "main");
10023 #if defined(_WIN32) || defined(CONFIG_TCCBOOT)
10024 error("debug mode currently not available for Windows");
10026 struct sigaction sigact
;
10027 /* install TCC signal handlers to print debug info on fatal
10029 sigact
.sa_flags
= SA_SIGINFO
| SA_RESETHAND
;
10030 sigact
.sa_sigaction
= sig_error
;
10031 sigemptyset(&sigact
.sa_mask
);
10032 sigaction(SIGFPE
, &sigact
, NULL
);
10033 sigaction(SIGILL
, &sigact
, NULL
);
10034 sigaction(SIGSEGV
, &sigact
, NULL
);
10035 sigaction(SIGBUS
, &sigact
, NULL
);
10036 sigaction(SIGABRT
, &sigact
, NULL
);
10040 #ifdef CONFIG_TCC_BCHECK
10041 if (do_bounds_check
) {
10042 void (*bound_init
)(void);
10044 /* set error function */
10045 rt_bound_error_msg
= (void *)tcc_get_symbol_err(s1
,
10046 "__bound_error_msg");
10048 /* XXX: use .init section so that it also work in binary ? */
10049 bound_init
= (void *)tcc_get_symbol_err(s1
, "__bound_init");
10053 return (*prog_main
)(argc
, argv
);
10056 TCCState
*tcc_new(void)
10063 s
= tcc_mallocz(sizeof(TCCState
));
10067 s
->output_type
= TCC_OUTPUT_MEMORY
;
10069 /* init isid table */
10071 isidnum_table
[i
] = isid(i
) || isnum(i
);
10073 /* add all tokens */
10074 table_ident
= NULL
;
10075 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
10077 tok_ident
= TOK_IDENT
;
10086 ts
= tok_alloc(p
, r
- p
- 1);
10090 /* we add dummy defines for some special macros to speed up tests
10091 and to have working defined() */
10092 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
10093 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
10094 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
10095 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
10097 /* standard defines */
10098 tcc_define_symbol(s
, "__STDC__", NULL
);
10099 tcc_define_symbol(s
, "__STDC_VERSION__", "199901L");
10100 #if defined(TCC_TARGET_I386)
10101 tcc_define_symbol(s
, "__i386__", NULL
);
10103 #if defined(TCC_TARGET_ARM)
10104 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
10105 tcc_define_symbol(s
, "__arm_elf__", NULL
);
10106 tcc_define_symbol(s
, "__arm_elf", NULL
);
10107 tcc_define_symbol(s
, "arm_elf", NULL
);
10108 tcc_define_symbol(s
, "__arm__", NULL
);
10109 tcc_define_symbol(s
, "__arm", NULL
);
10110 tcc_define_symbol(s
, "arm", NULL
);
10111 tcc_define_symbol(s
, "__APCS_32__", NULL
);
10113 #ifdef TCC_TARGET_PE
10114 tcc_define_symbol(s
, "_WIN32", NULL
);
10116 tcc_define_symbol(s
, "__unix__", NULL
);
10117 tcc_define_symbol(s
, "__unix", NULL
);
10118 #if defined(__linux)
10119 tcc_define_symbol(s
, "__linux__", NULL
);
10120 tcc_define_symbol(s
, "__linux", NULL
);
10123 /* tiny C specific defines */
10124 tcc_define_symbol(s
, "__TINYC__", NULL
);
10126 /* tiny C & gcc defines */
10127 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
10128 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
10129 #ifdef TCC_TARGET_PE
10130 tcc_define_symbol(s
, "__WCHAR_TYPE__", "unsigned short");
10132 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
10135 #ifndef TCC_TARGET_PE
10136 /* default library paths */
10137 tcc_add_library_path(s
, "/usr/local/lib");
10138 tcc_add_library_path(s
, "/usr/lib");
10139 tcc_add_library_path(s
, "/lib");
10142 /* no section zero */
10143 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
10145 /* create standard sections */
10146 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
10147 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
10148 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
10150 /* symbols are always generated for linking stage */
10151 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
10153 ".hashtab", SHF_PRIVATE
);
10154 strtab_section
= symtab_section
->link
;
10156 /* private symbol table for dynamic symbols */
10157 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
10159 ".dynhashtab", SHF_PRIVATE
);
10160 s
->alacarte_link
= 1;
10162 #ifdef CHAR_IS_UNSIGNED
10163 s
->char_is_unsigned
= 1;
10165 #if defined(TCC_TARGET_PE) && 0
10166 /* XXX: currently the PE linker is not ready to support that */
10167 s
->leading_underscore
= 1;
10172 void tcc_delete(TCCState
*s1
)
10176 /* free -D defines */
10177 free_defines(NULL
);
10180 n
= tok_ident
- TOK_IDENT
;
10181 for(i
= 0; i
< n
; i
++)
10182 tcc_free(table_ident
[i
]);
10183 tcc_free(table_ident
);
10185 /* free all sections */
10187 free_section(symtab_section
->hash
);
10189 free_section(s1
->dynsymtab_section
->hash
);
10190 free_section(s1
->dynsymtab_section
->link
);
10191 free_section(s1
->dynsymtab_section
);
10193 for(i
= 1; i
< s1
->nb_sections
; i
++)
10194 free_section(s1
->sections
[i
]);
10195 tcc_free(s1
->sections
);
10197 /* free loaded dlls array */
10198 dynarray_reset(&s1
->loaded_dlls
, &s1
->nb_loaded_dlls
);
10200 /* free library paths */
10201 dynarray_reset(&s1
->library_paths
, &s1
->nb_library_paths
);
10203 /* free include paths */
10204 dynarray_reset(&s1
->cached_includes
, &s1
->nb_cached_includes
);
10205 dynarray_reset(&s1
->include_paths
, &s1
->nb_include_paths
);
10206 dynarray_reset(&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
);
10211 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
10215 pathname1
= tcc_strdup(pathname
);
10216 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
10220 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
10224 pathname1
= tcc_strdup(pathname
);
10225 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
10229 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
10234 BufferedFile
*saved_file
;
10236 /* find source file type with extension */
10237 ext
= tcc_fileextension(filename
);
10241 /* open the file */
10243 file
= tcc_open(s1
, filename
);
10245 if (flags
& AFF_PRINT_ERROR
) {
10246 error_noabort("file '%s' not found", filename
);
10252 if (flags
& AFF_PREPROCESS
) {
10253 ret
= tcc_preprocess(s1
);
10254 } else if (!ext
[0] || !strcmp(ext
, "c")) {
10255 /* C file assumed */
10256 ret
= tcc_compile(s1
);
10258 #ifdef CONFIG_TCC_ASM
10259 if (!strcmp(ext
, "S")) {
10260 /* preprocessed assembler */
10261 ret
= tcc_assemble(s1
, 1);
10262 } else if (!strcmp(ext
, "s")) {
10263 /* non preprocessed assembler */
10264 ret
= tcc_assemble(s1
, 0);
10267 #ifdef TCC_TARGET_PE
10268 if (!strcmp(ext
, "def")) {
10269 ret
= pe_load_def_file(s1
, file
->fd
);
10274 /* assume executable format: auto guess file type */
10275 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
10276 lseek(fd
, 0, SEEK_SET
);
10278 error_noabort("could not read header");
10280 } else if (ret
!= sizeof(ehdr
)) {
10281 goto try_load_script
;
10284 if (ehdr
.e_ident
[0] == ELFMAG0
&&
10285 ehdr
.e_ident
[1] == ELFMAG1
&&
10286 ehdr
.e_ident
[2] == ELFMAG2
&&
10287 ehdr
.e_ident
[3] == ELFMAG3
) {
10288 file
->line_num
= 0; /* do not display line number if error */
10289 if (ehdr
.e_type
== ET_REL
) {
10290 ret
= tcc_load_object_file(s1
, fd
, 0);
10291 } else if (ehdr
.e_type
== ET_DYN
) {
10292 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
10293 #ifdef TCC_TARGET_PE
10297 h
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
10304 ret
= tcc_load_dll(s1
, fd
, filename
,
10305 (flags
& AFF_REFERENCED_DLL
) != 0);
10308 error_noabort("unrecognized ELF file");
10311 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
10312 file
->line_num
= 0; /* do not display line number if error */
10313 ret
= tcc_load_archive(s1
, fd
);
10315 #ifdef TCC_TARGET_COFF
10316 if (*(uint16_t *)(&ehdr
) == COFF_C67_MAGIC
) {
10317 ret
= tcc_load_coff(s1
, fd
);
10320 #ifdef TCC_TARGET_PE
10321 if (pe_test_res_file(&ehdr
, ret
)) {
10322 ret
= pe_load_res_file(s1
, fd
);
10326 /* as GNU ld, consider it is an ld script if not recognized */
10328 ret
= tcc_load_ldscript(s1
);
10330 error_noabort("unrecognized file type");
10345 int tcc_add_file(TCCState
*s
, const char *filename
)
10347 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
10350 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
10354 pathname1
= tcc_strdup(pathname
);
10355 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
10359 /* find and load a dll. Return non zero if not found */
10360 /* XXX: add '-rpath' option support ? */
10361 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
10366 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10367 snprintf(buf
, sizeof(buf
), "%s/%s",
10368 s
->library_paths
[i
], filename
);
10369 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
10375 /* the library name is the same as the argument of the '-l' option */
10376 int tcc_add_library(TCCState
*s
, const char *libraryname
)
10381 /* first we look for the dynamic library if not static linking */
10382 if (!s
->static_link
) {
10383 #ifdef TCC_TARGET_PE
10384 snprintf(buf
, sizeof(buf
), "%s.def", libraryname
);
10386 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
10388 if (tcc_add_dll(s
, buf
, 0) == 0)
10392 /* then we look for the static library */
10393 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
10394 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
10395 s
->library_paths
[i
], libraryname
);
10396 if (tcc_add_file_internal(s
, buf
, 0) == 0)
10402 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
10404 add_elf_sym(symtab_section
, val
, 0,
10405 ELF32_ST_INFO(STB_GLOBAL
, STT_NOTYPE
), 0,
10410 int tcc_set_output_type(TCCState
*s
, int output_type
)
10414 s
->output_type
= output_type
;
10416 if (!s
->nostdinc
) {
10417 /* default include paths */
10418 /* XXX: reverse order needed if -isystem support */
10419 #ifndef TCC_TARGET_PE
10420 tcc_add_sysinclude_path(s
, "/usr/local/include");
10421 tcc_add_sysinclude_path(s
, "/usr/include");
10423 snprintf(buf
, sizeof(buf
), "%s/include", tcc_lib_path
);
10424 tcc_add_sysinclude_path(s
, buf
);
10425 #ifdef TCC_TARGET_PE
10426 snprintf(buf
, sizeof(buf
), "%s/include/winapi", tcc_lib_path
);
10427 tcc_add_sysinclude_path(s
, buf
);
10431 /* if bound checking, then add corresponding sections */
10432 #ifdef CONFIG_TCC_BCHECK
10433 if (do_bounds_check
) {
10434 /* define symbol */
10435 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
10436 /* create bounds sections */
10437 bounds_section
= new_section(s
, ".bounds",
10438 SHT_PROGBITS
, SHF_ALLOC
);
10439 lbounds_section
= new_section(s
, ".lbounds",
10440 SHT_PROGBITS
, SHF_ALLOC
);
10444 if (s
->char_is_unsigned
) {
10445 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
10448 /* add debug sections */
10451 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
10452 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
10453 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
10454 put_elf_str(stabstr_section
, "");
10455 stab_section
->link
= stabstr_section
;
10456 /* put first entry */
10457 put_stabs("", 0, 0, 0, 0);
10460 /* add libc crt1/crti objects */
10461 #ifndef TCC_TARGET_PE
10462 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
10464 if (output_type
!= TCC_OUTPUT_DLL
)
10465 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
10466 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
10470 #ifdef TCC_TARGET_PE
10471 snprintf(buf
, sizeof(buf
), "%s/lib", tcc_lib_path
);
10472 tcc_add_library_path(s
, buf
);
10478 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10479 #define FD_INVERT 0x0002 /* invert value before storing */
10481 typedef struct FlagDef
{
10487 static const FlagDef warning_defs
[] = {
10488 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
10489 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
10490 { offsetof(TCCState
, warn_error
), 0, "error" },
10491 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
10492 "implicit-function-declaration" },
10495 static int set_flag(TCCState
*s
, const FlagDef
*flags
, int nb_flags
,
10496 const char *name
, int value
)
10503 if (r
[0] == 'n' && r
[1] == 'o' && r
[2] == '-') {
10507 for(i
= 0, p
= flags
; i
< nb_flags
; i
++, p
++) {
10508 if (!strcmp(r
, p
->name
))
10513 if (p
->flags
& FD_INVERT
)
10515 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
10520 /* set/reset a warning */
10521 int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
10526 if (!strcmp(warning_name
, "all")) {
10527 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
10528 if (p
->flags
& WD_ALL
)
10529 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
10533 return set_flag(s
, warning_defs
, countof(warning_defs
),
10534 warning_name
, value
);
10538 static const FlagDef flag_defs
[] = {
10539 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
10540 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
10541 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
10542 { offsetof(TCCState
, leading_underscore
), 0, "leading-underscore" },
10545 /* set/reset a flag */
10546 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
)
10548 return set_flag(s
, flag_defs
, countof(flag_defs
),
10552 #if !defined(LIBTCC)
10554 static int64_t getclock_us(void)
10559 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
10562 gettimeofday(&tv
, NULL
);
10563 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
10569 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
10570 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10571 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
10572 " [-static] [infile1 infile2...] [-run infile args...]\n"
10574 "General options:\n"
10575 " -v display current version, increase verbosity\n"
10576 " -c compile only - generate an object file\n"
10577 " -o outfile set output filename\n"
10578 " -Bdir set tcc internal library path\n"
10579 " -bench output compilation statistics\n"
10580 " -run run compiled source\n"
10581 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10582 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10583 " -w disable all warnings\n"
10584 "Preprocessor options:\n"
10585 " -E preprocess only\n"
10586 " -Idir add include path 'dir'\n"
10587 " -Dsym[=val] define 'sym' with value 'val'\n"
10588 " -Usym undefine 'sym'\n"
10589 "Linker options:\n"
10590 " -Ldir add library path 'dir'\n"
10591 " -llib link with dynamic or static library 'lib'\n"
10592 " -shared generate a shared library\n"
10593 " -soname set name for shared library to be used at runtime\n"
10594 " -static static linking\n"
10595 " -rdynamic export all global symbols to dynamic linker\n"
10596 " -r generate (relocatable) object file\n"
10597 "Debugger options:\n"
10598 " -g generate runtime debug info\n"
10599 #ifdef CONFIG_TCC_BCHECK
10600 " -b compile with built-in memory and bounds checker (implies -g)\n"
10602 " -bt N show N callers in stack traces\n"
10606 #define TCC_OPTION_HAS_ARG 0x0001
10607 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10609 typedef struct TCCOption
{
10638 TCC_OPTION_nostdinc
,
10639 TCC_OPTION_nostdlib
,
10640 TCC_OPTION_print_search_dirs
,
10641 TCC_OPTION_rdynamic
,
10649 static const TCCOption tcc_options
[] = {
10650 { "h", TCC_OPTION_HELP
, 0 },
10651 { "?", TCC_OPTION_HELP
, 0 },
10652 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
10653 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
10654 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
10655 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
10656 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
10657 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10658 { "bench", TCC_OPTION_bench
, 0 },
10659 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
10660 #ifdef CONFIG_TCC_BCHECK
10661 { "b", TCC_OPTION_b
, 0 },
10663 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10664 { "c", TCC_OPTION_c
, 0 },
10665 { "static", TCC_OPTION_static
, 0 },
10666 { "shared", TCC_OPTION_shared
, 0 },
10667 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
10668 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
10669 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10670 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
10671 { "r", TCC_OPTION_r
, 0 },
10672 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10673 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10674 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10675 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
10676 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10677 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
10678 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
10679 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
10680 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10681 { "w", TCC_OPTION_w
, 0 },
10682 { "pipe", TCC_OPTION_pipe
, 0},
10683 { "E", TCC_OPTION_E
, 0},
10687 /* convert 'str' into an array of space separated strings */
10688 static int expand_args(char ***pargv
, const char *str
)
10697 while (is_space(*str
))
10702 while (*str
!= '\0' && !is_space(*str
))
10705 arg
= tcc_malloc(len
+ 1);
10706 memcpy(arg
, s1
, len
);
10708 dynarray_add((void ***)&argv
, &argc
, arg
);
10714 static char **files
;
10715 static int nb_files
, nb_libraries
;
10716 static int multiple_files
;
10717 static int print_search_dirs
;
10718 static int output_type
;
10719 static int reloc_output
;
10720 static const char *outfile
;
10722 int parse_args(TCCState
*s
, int argc
, char **argv
)
10725 const TCCOption
*popt
;
10726 const char *optarg
, *p1
, *r1
;
10731 if (optind
>= argc
) {
10732 if (nb_files
== 0 && !print_search_dirs
) {
10739 r
= argv
[optind
++];
10740 if (r
[0] != '-' || r
[1] == '\0') {
10741 /* add a new file */
10742 dynarray_add((void ***)&files
, &nb_files
, r
);
10743 if (!multiple_files
) {
10745 /* argv[0] will be this file */
10749 /* find option in table (match only the first chars */
10750 popt
= tcc_options
;
10754 error("invalid option -- '%s'", r
);
10767 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
10768 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
10771 if (optind
>= argc
)
10772 error("argument to '%s' is missing", r
);
10773 optarg
= argv
[optind
++];
10781 switch(popt
->index
) {
10782 case TCC_OPTION_HELP
:
10787 if (tcc_add_include_path(s
, optarg
) < 0)
10788 error("too many include paths");
10793 sym
= (char *)optarg
;
10794 value
= strchr(sym
, '=');
10799 tcc_define_symbol(s
, sym
, value
);
10803 tcc_undefine_symbol(s
, optarg
);
10806 tcc_add_library_path(s
, optarg
);
10809 /* set tcc utilities path (mainly for tcc development) */
10810 tcc_lib_path
= optarg
;
10813 dynarray_add((void ***)&files
, &nb_files
, r
);
10816 case TCC_OPTION_bench
:
10819 case TCC_OPTION_bt
:
10820 num_callers
= atoi(optarg
);
10822 #ifdef CONFIG_TCC_BCHECK
10824 do_bounds_check
= 1;
10832 multiple_files
= 1;
10833 output_type
= TCC_OUTPUT_OBJ
;
10835 case TCC_OPTION_static
:
10836 s
->static_link
= 1;
10838 case TCC_OPTION_shared
:
10839 output_type
= TCC_OUTPUT_DLL
;
10841 case TCC_OPTION_soname
:
10842 s
->soname
= optarg
;
10845 multiple_files
= 1;
10849 /* generate a .o merging several output files */
10851 output_type
= TCC_OUTPUT_OBJ
;
10853 case TCC_OPTION_nostdinc
:
10856 case TCC_OPTION_nostdlib
:
10859 case TCC_OPTION_print_search_dirs
:
10860 print_search_dirs
= 1;
10862 case TCC_OPTION_run
:
10866 argc1
= expand_args(&argv1
, optarg
);
10868 parse_args(s
, argc1
, argv1
);
10870 multiple_files
= 0;
10871 output_type
= TCC_OUTPUT_MEMORY
;
10876 if (0 == verbose
++)
10877 printf("tcc version %s\n", TCC_VERSION
);
10878 } while (*optarg
++ == 'v');
10881 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
10882 goto unsupported_option
;
10885 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
10886 s
->warn_unsupported
)
10887 goto unsupported_option
;
10892 case TCC_OPTION_rdynamic
:
10895 case TCC_OPTION_Wl
:
10898 if (strstart(optarg
, "-Ttext,", &p
)) {
10899 s
->text_addr
= strtoul(p
, NULL
, 16);
10900 s
->has_text_addr
= 1;
10901 } else if (strstart(optarg
, "--oformat,", &p
)) {
10902 if (strstart(p
, "elf32-", NULL
)) {
10903 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
10904 } else if (!strcmp(p
, "binary")) {
10905 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
10907 #ifdef TCC_TARGET_COFF
10908 if (!strcmp(p
, "coff")) {
10909 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
10913 error("target %s not found", p
);
10916 error("unsupported linker option '%s'", optarg
);
10921 output_type
= TCC_OUTPUT_PREPROCESS
;
10924 if (s
->warn_unsupported
) {
10925 unsupported_option
:
10926 warning("unsupported option '%s'", r
);
10935 int main(int argc
, char **argv
)
10939 int nb_objfiles
, ret
, optind
;
10940 char objfilename
[1024];
10941 int64_t start_time
= 0;
10944 tcc_lib_path
= w32_tcc_lib_path();
10948 output_type
= TCC_OUTPUT_EXE
;
10950 multiple_files
= 1;
10955 print_search_dirs
= 0;
10958 optind
= parse_args(s
, argc
- 1, argv
+ 1) + 1;
10960 if (print_search_dirs
) {
10961 /* enough for Linux kernel */
10962 printf("install: %s/\n", tcc_lib_path
);
10966 nb_objfiles
= nb_files
- nb_libraries
;
10968 /* if outfile provided without other options, we output an
10970 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
10971 output_type
= TCC_OUTPUT_EXE
;
10973 /* check -c consistency : only single file handled. XXX: checks file type */
10974 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
10975 /* accepts only a single input file */
10976 if (nb_objfiles
!= 1)
10977 error("cannot specify multiple files with -c");
10978 if (nb_libraries
!= 0)
10979 error("cannot specify libraries with -c");
10983 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
10985 s
->outfile
= stdout
;
10987 s
->outfile
= fopen(outfile
, "w");
10989 error("could not open '%s", outfile
);
10991 } else if (output_type
!= TCC_OUTPUT_MEMORY
) {
10993 /* compute default outfile name */
10996 strcmp(files
[0], "-") == 0 ? "a" : tcc_basename(files
[0]);
10997 pstrcpy(objfilename
, sizeof(objfilename
), name
);
10998 ext
= tcc_fileextension(objfilename
);
10999 #ifdef TCC_TARGET_PE
11000 if (output_type
== TCC_OUTPUT_DLL
)
11001 strcpy(ext
, ".dll");
11003 if (output_type
== TCC_OUTPUT_EXE
)
11004 strcpy(ext
, ".exe");
11007 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
&& *ext
)
11010 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
11011 outfile
= objfilename
;
11016 start_time
= getclock_us();
11019 tcc_set_output_type(s
, output_type
);
11021 /* compile or add each files or library */
11022 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
11023 const char *filename
;
11025 filename
= files
[i
];
11026 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
11027 if (tcc_add_file_internal(s
, filename
,
11028 AFF_PRINT_ERROR
| AFF_PREPROCESS
) < 0)
11030 } else if (filename
[0] == '-' && filename
[1]) {
11031 if (tcc_add_library(s
, filename
+ 2) < 0)
11032 error("cannot find %s", filename
);
11035 printf("-> %s\n", filename
);
11036 if (tcc_add_file(s
, filename
) < 0)
11041 /* free all files */
11049 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
11050 if (total_time
< 0.001)
11051 total_time
= 0.001;
11052 if (total_bytes
< 1)
11054 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
11055 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
11056 total_time
, (int)(total_lines
/ total_time
),
11057 total_bytes
/ total_time
/ 1000000.0);
11060 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
11062 fclose(s
->outfile
);
11063 } else if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
11064 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
11066 #ifdef TCC_TARGET_PE
11067 if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
11068 ret
= pe_output_file(s
, outfile
);
11072 ret
= tcc_output_file(s
, outfile
) ? 1 : 0;
11075 /* XXX: cannot do it with bound checking because of the malloc hooks */
11076 if (!do_bounds_check
)
11081 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);