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>
46 #include <sys/ucontext.h>
49 #endif /* !CONFIG_TCCBOOT */
62 /* preprocessor debug */
64 /* include file debug */
72 /* target selection */
73 //#define TCC_TARGET_I386 /* i386 code generator */
74 //#define TCC_TARGET_ARM /* ARMv4 code generator */
75 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
77 /* default target is I386 */
78 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
79 !defined(TCC_TARGET_C67)
80 #define TCC_TARGET_I386
83 #if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
84 !defined(TCC_TARGET_C67)
85 #define CONFIG_TCC_BCHECK /* enable bound checking code */
88 #if defined(WIN32) && !defined(TCC_TARGET_PE)
89 #define CONFIG_TCC_STATIC
92 /* define it to include assembler support */
93 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
94 #define CONFIG_TCC_ASM
97 /* object format selection */
98 #if defined(TCC_TARGET_C67)
99 #define TCC_TARGET_COFF
108 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
109 executables or dlls */
110 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
112 #define INCLUDE_STACK_SIZE 32
113 #define IFDEF_STACK_SIZE 64
114 #define VSTACK_SIZE 256
115 #define STRING_MAX_SIZE 1024
116 #define PACK_STACK_SIZE 8
118 #define TOK_HASH_SIZE 8192 /* must be a power of two */
119 #define TOK_ALLOC_INCR 512 /* must be a power of two */
120 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
122 /* token symbol management */
123 typedef struct TokenSym
{
124 struct TokenSym
*hash_next
;
125 struct Sym
*sym_define
; /* direct pointer to define */
126 struct Sym
*sym_label
; /* direct pointer to label */
127 struct Sym
*sym_struct
; /* direct pointer to structure */
128 struct Sym
*sym_identifier
; /* direct pointer to identifier */
129 int tok
; /* token number */
134 typedef struct CString
{
135 int size
; /* size in bytes */
136 void *data
; /* either 'char *' or 'int *' */
138 void *data_allocated
; /* if non NULL, data has been malloced */
141 /* type definition */
142 typedef struct CType
{
148 typedef union CValue
{
154 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
156 unsigned long long ull
;
157 struct CString
*cstr
;
163 typedef struct SValue
{
164 CType type
; /* type */
165 unsigned short r
; /* register + flags */
166 unsigned short r2
; /* second register, used for 'long long'
167 type. If not used, set to VT_CONST */
168 CValue c
; /* constant, if VT_CONST */
169 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
172 /* symbol management */
174 int v
; /* symbol token */
175 int r
; /* associated register */
176 int c
; /* associated number */
177 CType type
; /* associated type */
178 struct Sym
*next
; /* next related symbol */
179 struct Sym
*prev
; /* prev symbol in stack */
180 struct Sym
*prev_tok
; /* previous symbol for this token */
183 /* section definition */
184 /* XXX: use directly ELF structure for parameters ? */
185 /* special flag to indicate that the section should not be linked to
187 #define SHF_PRIVATE 0x80000000
189 typedef struct Section
{
190 unsigned long data_offset
; /* current data offset */
191 unsigned char *data
; /* section data */
192 unsigned long data_allocated
; /* used for realloc() handling */
193 int sh_name
; /* elf section name (only used during output) */
194 int sh_num
; /* elf section number */
195 int sh_type
; /* elf section type */
196 int sh_flags
; /* elf section flags */
197 int sh_info
; /* elf section info */
198 int sh_addralign
; /* elf section alignment */
199 int sh_entsize
; /* elf entry size */
200 unsigned long sh_size
; /* section size (only used during output) */
201 unsigned long sh_addr
; /* address at which the section is relocated */
202 unsigned long sh_offset
; /* address at which the section is relocated */
203 int nb_hashed_syms
; /* used to resize the hash table */
204 struct Section
*link
; /* link to another section */
205 struct Section
*reloc
; /* corresponding section for relocation, if any */
206 struct Section
*hash
; /* hash table for symbols */
207 struct Section
*next
;
208 char name
[1]; /* section name */
211 typedef struct DLLReference
{
216 /* GNUC attribute definition */
217 typedef struct AttributeDef
{
221 unsigned char func_call
; /* FUNC_CDECL, FUNC_STDCALL, FUNC_FASTCALLx */
222 unsigned char dllexport
;
225 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
226 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
227 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
229 /* stored in 'Sym.c' field */
230 #define FUNC_NEW 1 /* ansi function prototype */
231 #define FUNC_OLD 2 /* old function prototype */
232 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
234 /* stored in 'Sym.r' field */
235 #define FUNC_CDECL 0 /* standard c call */
236 #define FUNC_STDCALL 1 /* pascal c call */
237 #define FUNC_FASTCALL1 2 /* first param in %eax */
238 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
239 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
241 /* field 'Sym.t' for macros */
242 #define MACRO_OBJ 0 /* object like macro */
243 #define MACRO_FUNC 1 /* function like macro */
245 /* field 'Sym.r' for C labels */
246 #define LABEL_DEFINED 0 /* label is defined */
247 #define LABEL_FORWARD 1 /* label is forward defined */
248 #define LABEL_DECLARED 2 /* label is declared but never used */
250 /* type_decl() types */
251 #define TYPE_ABSTRACT 1 /* type without variable */
252 #define TYPE_DIRECT 2 /* type with variable */
254 #define IO_BUF_SIZE 8192
256 typedef struct BufferedFile
{
260 int line_num
; /* current line number - here to simplify code */
261 int ifndef_macro
; /* #ifndef macro / #endif search */
262 int ifndef_macro_saved
; /* saved ifndef_macro */
263 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
264 char inc_type
; /* type of include */
265 char inc_filename
[512]; /* filename specified by the user */
266 char filename
[1024]; /* current filename - here to simplify code */
267 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
270 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
271 #define CH_EOF (-1) /* end of file */
273 /* parsing state (used to save parser state to reparse part of the
274 source several times) */
275 typedef struct ParseState
{
282 /* used to record tokens */
283 typedef struct TokenString
{
290 /* include file cache, used to find files faster and also to eliminate
291 inclusion if the include file is protected by #ifndef ... #endif */
292 typedef struct CachedInclude
{
294 int hash_next
; /* -1 if none */
295 char type
; /* '"' or '>' to give include type */
296 char filename
[1]; /* path specified in #include */
299 #define CACHED_INCLUDES_HASH_SIZE 512
302 static struct BufferedFile
*file
;
305 static CString tokcstr
; /* current parsed string, if any */
306 /* additional informations about token */
307 static int tok_flags
;
308 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
309 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
310 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
312 static int *macro_ptr
, *macro_ptr_allocated
;
313 static int *unget_saved_macro_ptr
;
314 static int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
315 static int unget_buffer_enabled
;
316 static int parse_flags
;
317 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
318 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
319 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
320 token. line feed is also
322 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
324 static Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
325 static Section
*cur_text_section
; /* current section where function code is
327 #ifdef CONFIG_TCC_ASM
328 static Section
*last_text_section
; /* to handle .previous asm directive */
330 /* bound check related sections */
331 static Section
*bounds_section
; /* contains global data bound description */
332 static Section
*lbounds_section
; /* contains local data bound description */
333 /* symbol sections */
334 static Section
*symtab_section
, *strtab_section
;
337 static Section
*stab_section
, *stabstr_section
;
339 /* loc : local variable index
340 ind : output code index
342 anon_sym: anonymous symbol index
344 static int rsym
, anon_sym
, ind
, loc
;
345 /* expression generation modifiers */
346 static int const_wanted
; /* true if constant wanted */
347 static int nocode_wanted
; /* true if no code generation wanted for an expression */
348 static int global_expr
; /* true if compound literals must be allocated
349 globally (used during initializers parsing */
350 static CType func_vt
; /* current function return type (used by return
353 static int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
354 static int tok_ident
;
355 static TokenSym
**table_ident
;
356 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
357 static char token_buf
[STRING_MAX_SIZE
+ 1];
358 static char *funcname
;
359 static Sym
*global_stack
, *local_stack
;
360 static Sym
*define_stack
;
361 static Sym
*global_label_stack
, *local_label_stack
;
362 /* symbol allocator */
363 #define SYM_POOL_NB (8192 / sizeof(Sym))
364 static Sym
*sym_free_first
;
366 static SValue vstack
[VSTACK_SIZE
], *vtop
;
367 /* some predefined types */
368 static CType char_pointer_type
, func_old_type
, int_type
;
369 /* true if isid(c) || isnum(c) */
370 static unsigned char isidnum_table
[256];
372 /* compile with debug symbol (and use them if error during execution) */
373 static int do_debug
= 0;
375 /* compile with built-in memory and bounds checker */
376 static int do_bounds_check
= 0;
378 /* display benchmark infos */
380 static int do_bench
= 0;
382 static int total_lines
;
383 static int total_bytes
;
385 /* use GNU C extensions */
386 static int gnu_ext
= 1;
388 /* use Tiny C extensions */
389 static int tcc_ext
= 1;
391 /* max number of callers shown if error */
392 static int num_callers
= 6;
393 static const char **rt_bound_error_msg
;
395 /* XXX: get rid of this ASAP */
396 static struct TCCState
*tcc_state
;
398 /* give the path of the tcc libraries */
399 static const char *tcc_lib_path
= CONFIG_TCCDIR
;
404 BufferedFile
**include_stack_ptr
;
405 int *ifdef_stack_ptr
;
407 /* include file handling */
408 char **include_paths
;
409 int nb_include_paths
;
410 char **sysinclude_paths
;
411 int nb_sysinclude_paths
;
412 CachedInclude
**cached_includes
;
413 int nb_cached_includes
;
415 char **library_paths
;
416 int nb_library_paths
;
418 /* array of all loaded dlls (including those referenced by loaded
420 DLLReference
**loaded_dlls
;
425 int nb_sections
; /* number of sections, including first dummy section */
430 unsigned long *got_offsets
;
432 /* give the correspondance from symtab indexes to dynsym indexes */
433 int *symtab_to_dynsym
;
435 /* temporary dynamic symbol sections (for dll loading) */
436 Section
*dynsymtab_section
;
437 /* exported dynamic symbol section */
440 int nostdinc
; /* if true, no standard headers are added */
441 int nostdlib
; /* if true, no standard libraries are added */
443 int nocommon
; /* if true, do not use common symbols for .bss data */
445 /* if true, static linking is performed */
448 /* if true, all symbols are exported */
451 /* if true, only link in referenced objects from archive */
454 /* address of text section */
455 unsigned long text_addr
;
458 /* output format, see TCC_OUTPUT_FORMAT_xxx */
461 /* C language options */
462 int char_is_unsigned
;
464 /* warning switches */
465 int warn_write_strings
;
466 int warn_unsupported
;
469 int warn_implicit_function_declaration
;
473 void (*error_func
)(void *opaque
, const char *msg
);
474 int error_set_jmp_enabled
;
475 jmp_buf error_jmp_buf
;
478 /* tiny assembler state */
481 /* see include_stack_ptr */
482 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
484 /* see ifdef_stack_ptr */
485 int ifdef_stack
[IFDEF_STACK_SIZE
];
487 /* see cached_includes */
488 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
491 int pack_stack
[PACK_STACK_SIZE
];
495 /* The current value can be: */
496 #define VT_VALMASK 0x00ff
497 #define VT_CONST 0x00f0 /* constant in vc
498 (must be first non register value) */
499 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
500 #define VT_LOCAL 0x00f2 /* offset on stack */
501 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
502 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
503 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
504 #define VT_LVAL 0x0100 /* var is an lvalue */
505 #define VT_SYM 0x0200 /* a symbol value is added */
506 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
507 char/short stored in integer registers) */
508 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
509 dereferencing value */
510 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
511 bounding function call point is in vc */
512 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
513 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
514 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
515 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
518 #define VT_INT 0 /* integer type */
519 #define VT_BYTE 1 /* signed byte type */
520 #define VT_SHORT 2 /* short type */
521 #define VT_VOID 3 /* void type */
522 #define VT_PTR 4 /* pointer */
523 #define VT_ENUM 5 /* enum definition */
524 #define VT_FUNC 6 /* function type */
525 #define VT_STRUCT 7 /* struct/union definition */
526 #define VT_FLOAT 8 /* IEEE float */
527 #define VT_DOUBLE 9 /* IEEE double */
528 #define VT_LDOUBLE 10 /* IEEE long double */
529 #define VT_BOOL 11 /* ISOC99 boolean type */
530 #define VT_LLONG 12 /* 64 bit integer */
531 #define VT_LONG 13 /* long integer (NEVER USED as type, only
533 #define VT_BTYPE 0x000f /* mask for basic type */
534 #define VT_UNSIGNED 0x0010 /* unsigned type */
535 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
536 #define VT_BITFIELD 0x0040 /* bitfield modifier */
537 #define VT_CONSTANT 0x0800 /* const modifier */
538 #define VT_VOLATILE 0x1000 /* volatile modifier */
539 #define VT_SIGNED 0x2000 /* signed type */
542 #define VT_EXTERN 0x00000080 /* extern definition */
543 #define VT_STATIC 0x00000100 /* static variable */
544 #define VT_TYPEDEF 0x00000200 /* typedef definition */
545 #define VT_INLINE 0x00000400 /* inline definition */
547 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
549 /* type mask (except storage) */
550 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
551 #define VT_TYPE (~(VT_STORAGE))
555 /* warning: the following compare tokens depend on i386 asm code */
567 #define TOK_LAND 0xa0
571 #define TOK_MID 0xa3 /* inc/dec, to void constant */
573 #define TOK_UDIV 0xb0 /* unsigned division */
574 #define TOK_UMOD 0xb1 /* unsigned modulo */
575 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
576 #define TOK_CINT 0xb3 /* number in tokc */
577 #define TOK_CCHAR 0xb4 /* char constant in tokc */
578 #define TOK_STR 0xb5 /* pointer to string in tokc */
579 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
580 #define TOK_LCHAR 0xb7
581 #define TOK_LSTR 0xb8
582 #define TOK_CFLOAT 0xb9 /* float constant */
583 #define TOK_LINENUM 0xba /* line number info */
584 #define TOK_CDOUBLE 0xc0 /* double constant */
585 #define TOK_CLDOUBLE 0xc1 /* long double constant */
586 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
587 #define TOK_ADDC1 0xc3 /* add with carry generation */
588 #define TOK_ADDC2 0xc4 /* add with carry use */
589 #define TOK_SUBC1 0xc5 /* add with carry generation */
590 #define TOK_SUBC2 0xc6 /* add with carry use */
591 #define TOK_CUINT 0xc8 /* unsigned int constant */
592 #define TOK_CLLONG 0xc9 /* long long constant */
593 #define TOK_CULLONG 0xca /* unsigned long long constant */
594 #define TOK_ARROW 0xcb
595 #define TOK_DOTS 0xcc /* three dots */
596 #define TOK_SHR 0xcd /* unsigned shift right */
597 #define TOK_PPNUM 0xce /* preprocessor number */
599 #define TOK_SHL 0x01 /* shift left */
600 #define TOK_SAR 0x02 /* signed shift right */
602 /* assignement operators : normal operator or 0x80 */
603 #define TOK_A_MOD 0xa5
604 #define TOK_A_AND 0xa6
605 #define TOK_A_MUL 0xaa
606 #define TOK_A_ADD 0xab
607 #define TOK_A_SUB 0xad
608 #define TOK_A_DIV 0xaf
609 #define TOK_A_XOR 0xde
610 #define TOK_A_OR 0xfc
611 #define TOK_A_SHL 0x81
612 #define TOK_A_SAR 0x82
615 #define offsetof(type, field) ((size_t) &((type *)0)->field)
619 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
622 /* WARNING: the content of this string encodes token numbers */
623 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";
625 #define TOK_EOF (-1) /* end of file */
626 #define TOK_LINEFEED 10 /* line feed */
628 /* all identificators and strings have token above that */
629 #define TOK_IDENT 256
631 /* only used for i386 asm opcodes definitions */
632 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
635 DEF(TOK_ASM_ ## x ## b, #x "b") \
636 DEF(TOK_ASM_ ## x ## w, #x "w") \
637 DEF(TOK_ASM_ ## x ## l, #x "l") \
638 DEF(TOK_ASM_ ## x, #x)
641 DEF(TOK_ASM_ ## x ## w, #x "w") \
642 DEF(TOK_ASM_ ## x ## l, #x "l") \
643 DEF(TOK_ASM_ ## x, #x)
646 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
647 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
648 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
649 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
652 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
653 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
656 #define DEF_ASMTEST(x) \
688 #define TOK_ASM_int TOK_INT
691 TOK_LAST
= TOK_IDENT
- 1,
692 #define DEF(id, str) id,
697 static const char tcc_keywords
[] =
698 #define DEF(id, str) str "\0"
703 #define TOK_UIDENT TOK_DEFINE
706 int __stdcall
GetModuleFileNameA(void *, char *, int);
707 void *__stdcall
GetProcAddress(void *, const char *);
708 void *__stdcall
GetModuleHandleA(const char *);
709 void *__stdcall
LoadLibraryA(const char *);
710 int __stdcall
FreeConsole(void);
712 #define snprintf _snprintf
713 #define vsnprintf _vsnprintf
715 #define strtold (long double)strtod
716 #define strtof (float)strtod
717 #define strtoll (long long)strtol
719 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__)
720 /* currently incorrect */
721 long double strtold(const char *nptr
, char **endptr
)
723 return (long double)strtod(nptr
, endptr
);
725 float strtof(const char *nptr
, char **endptr
)
727 return (float)strtod(nptr
, endptr
);
730 /* XXX: need to define this to use them in non ISOC99 context */
731 extern float strtof (const char *__nptr
, char **__endptr
);
732 extern long double strtold (const char *__nptr
, char **__endptr
);
735 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
736 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
737 static const char *tcc_basename(const char *name
);
739 static void next(void);
740 static void next_nomacro(void);
741 static void parse_expr_type(CType
*type
);
742 static void expr_type(CType
*type
);
743 static void unary_type(CType
*type
);
744 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
745 int case_reg
, int is_expr
);
746 static int expr_const(void);
747 static void expr_eq(void);
748 static void gexpr(void);
749 static void gen_inline_functions(void);
750 static void decl(int l
);
751 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
752 int first
, int size_only
);
753 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
754 int has_init
, int v
, int scope
);
756 void gv2(int rc1
, int rc2
);
757 void move_reg(int r
, int s
);
758 void save_regs(int n
);
759 void save_reg(int r
);
764 int get_reg_ex(int rc
,int rc2
);
766 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
767 const int *macro_str
, int can_read_stream
);
769 void force_charshort_cast(int t
);
770 static void gen_cast(CType
*type
);
772 static Sym
*sym_find(int v
);
773 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
776 static int type_size(CType
*type
, int *a
);
777 static inline CType
*pointed_type(CType
*type
);
778 static int pointed_size(CType
*type
);
779 static int lvalue_type(int t
);
780 static int parse_btype(CType
*type
, AttributeDef
*ad
);
781 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
782 static int is_compatible_types(CType
*type1
, CType
*type2
);
784 int ieee_finite(double d
);
785 void error(const char *fmt
, ...);
789 void lexpand_nr(void);
790 static void vpush_global_sym(CType
*type
, int v
);
791 void vset(CType
*type
, int r
, int v
);
792 void type_to_str(char *buf
, int buf_size
,
793 CType
*type
, const char *varstr
);
794 char *get_tok_str(int v
, CValue
*cv
);
795 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
796 unsigned long offset
, unsigned long size
);
797 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
799 /* section generation */
800 static void section_realloc(Section
*sec
, unsigned long new_size
);
801 static void *section_ptr_add(Section
*sec
, unsigned long size
);
802 static void put_extern_sym(Sym
*sym
, Section
*section
,
803 unsigned long value
, unsigned long size
);
804 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
805 static int put_elf_str(Section
*s
, const char *sym
);
806 static int put_elf_sym(Section
*s
,
807 unsigned long value
, unsigned long size
,
808 int info
, int other
, int shndx
, const char *name
);
809 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
810 int info
, int other
, int sh_num
, const char *name
);
811 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
812 int type
, int symbol
);
813 static void put_stabs(const char *str
, int type
, int other
, int desc
,
814 unsigned long value
);
815 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
816 unsigned long value
, Section
*sec
, int sym_index
);
817 static void put_stabn(int type
, int other
, int desc
, int value
);
818 static void put_stabd(int type
, int other
, int desc
);
819 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
821 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
822 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
823 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
826 int tcc_output_coff(TCCState
*s1
, FILE *f
);
829 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
);
830 int pe_load_def_file(struct TCCState
*s1
, FILE *fp
);
831 void pe_setup_paths(struct TCCState
*s1
, int *p_output_type
, const char **p_outfile
, char *first_file
);
832 unsigned long pe_add_runtime(struct TCCState
*s1
);
833 int tcc_output_pe(struct TCCState
*s1
, const char *filename
);
837 #ifdef CONFIG_TCC_ASM
839 typedef struct ExprValue
{
844 #define MAX_ASM_OPERANDS 30
846 typedef struct ASMOperand
{
847 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
849 char asm_str
[16]; /* computed asm string for operand */
850 SValue
*vt
; /* C value of the expression */
851 int ref_index
; /* if >= 0, gives reference to a output constraint */
852 int input_index
; /* if >= 0, gives reference to an input constraint */
853 int priority
; /* priority, used to assign registers */
854 int reg
; /* if >= 0, register number used for this operand */
855 int is_llong
; /* true if double register value */
856 int is_memory
; /* true if memory operand */
857 int is_rw
; /* for '+' modifier */
860 static void asm_expr(TCCState
*s1
, ExprValue
*pe
);
861 static int asm_int_expr(TCCState
*s1
);
862 static int find_constraint(ASMOperand
*operands
, int nb_operands
,
863 const char *name
, const char **pp
);
865 static int tcc_assemble(TCCState
*s1
, int do_preprocess
);
869 static void asm_instr(void);
870 static void asm_global_instr(void);
872 /* true if float/double/long double type */
873 static inline int is_float(int t
)
877 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
880 #ifdef TCC_TARGET_I386
881 #include "i386-gen.c"
884 #ifdef TCC_TARGET_ARM
888 #ifdef TCC_TARGET_C67
892 #ifdef CONFIG_TCC_STATIC
894 #define RTLD_LAZY 0x001
895 #define RTLD_NOW 0x002
896 #define RTLD_GLOBAL 0x100
897 #define RTLD_DEFAULT NULL
899 /* dummy function for profiling */
900 void *dlopen(const char *filename
, int flag
)
905 const char *dlerror(void)
910 typedef struct TCCSyms
{
915 #define TCCSYM(a) { #a, &a, },
917 /* add the symbol you want here if no dynamic linking is done */
918 static TCCSyms tcc_syms
[] = {
919 #if !defined(CONFIG_TCCBOOT)
928 void *resolve_sym(TCCState
*s1
, const char *symbol
, int type
)
932 while (p
->str
!= NULL
) {
933 if (!strcmp(p
->str
, symbol
))
940 #elif !defined(WIN32)
944 void *resolve_sym(TCCState
*s1
, const char *sym
, int type
)
946 return dlsym(RTLD_DEFAULT
, sym
);
951 /********************************************************/
953 /* we use our own 'finite' function to avoid potential problems with
954 non standard math libs */
955 /* XXX: endianness dependent */
956 int ieee_finite(double d
)
959 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
962 /* copy a string and truncate it. */
963 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
970 q_end
= buf
+ buf_size
- 1;
982 /* strcat and truncate. */
983 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
988 pstrcpy(buf
+ len
, buf_size
- len
, s
);
992 static int strstart(const char *str
, const char *val
, const char **ptr
)
1008 /* memory management */
1014 static inline void tcc_free(void *ptr
)
1017 mem_cur_size
-= malloc_usable_size(ptr
);
1022 static void *tcc_malloc(unsigned long size
)
1027 error("memory full");
1029 mem_cur_size
+= malloc_usable_size(ptr
);
1030 if (mem_cur_size
> mem_max_size
)
1031 mem_max_size
= mem_cur_size
;
1036 static void *tcc_mallocz(unsigned long size
)
1039 ptr
= tcc_malloc(size
);
1040 memset(ptr
, 0, size
);
1044 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
1048 mem_cur_size
-= malloc_usable_size(ptr
);
1050 ptr1
= realloc(ptr
, size
);
1052 /* NOTE: count not correct if alloc error, but not critical */
1053 mem_cur_size
+= malloc_usable_size(ptr1
);
1054 if (mem_cur_size
> mem_max_size
)
1055 mem_max_size
= mem_cur_size
;
1060 static char *tcc_strdup(const char *str
)
1063 ptr
= tcc_malloc(strlen(str
) + 1);
1068 #define free(p) use_tcc_free(p)
1069 #define malloc(s) use_tcc_malloc(s)
1070 #define realloc(p, s) use_tcc_realloc(p, s)
1072 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
1079 /* every power of two we double array size */
1080 if ((nb
& (nb
- 1)) == 0) {
1085 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
1087 error("memory full");
1094 /* symbol allocator */
1095 static Sym
*__sym_malloc(void)
1097 Sym
*sym_pool
, *sym
, *last_sym
;
1100 sym_pool
= tcc_malloc(SYM_POOL_NB
* sizeof(Sym
));
1102 last_sym
= sym_free_first
;
1104 for(i
= 0; i
< SYM_POOL_NB
; i
++) {
1105 sym
->next
= last_sym
;
1109 sym_free_first
= last_sym
;
1113 static inline Sym
*sym_malloc(void)
1116 sym
= sym_free_first
;
1118 sym
= __sym_malloc();
1119 sym_free_first
= sym
->next
;
1123 static inline void sym_free(Sym
*sym
)
1125 sym
->next
= sym_free_first
;
1126 sym_free_first
= sym
;
1129 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
1133 sec
= tcc_mallocz(sizeof(Section
) + strlen(name
));
1134 strcpy(sec
->name
, name
);
1135 sec
->sh_type
= sh_type
;
1136 sec
->sh_flags
= sh_flags
;
1143 sec
->sh_addralign
= 4;
1146 sec
->sh_addralign
= 1;
1149 sec
->sh_addralign
= 32; /* default conservative alignment */
1153 /* only add section if not private */
1154 if (!(sh_flags
& SHF_PRIVATE
)) {
1155 sec
->sh_num
= s1
->nb_sections
;
1156 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
1161 static void free_section(Section
*s
)
1167 /* realloc section and set its content to zero */
1168 static void section_realloc(Section
*sec
, unsigned long new_size
)
1171 unsigned char *data
;
1173 size
= sec
->data_allocated
;
1176 while (size
< new_size
)
1178 data
= tcc_realloc(sec
->data
, size
);
1180 error("memory full");
1181 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
1183 sec
->data_allocated
= size
;
1186 /* reserve at least 'size' bytes in section 'sec' from
1187 sec->data_offset. */
1188 static void *section_ptr_add(Section
*sec
, unsigned long size
)
1190 unsigned long offset
, offset1
;
1192 offset
= sec
->data_offset
;
1193 offset1
= offset
+ size
;
1194 if (offset1
> sec
->data_allocated
)
1195 section_realloc(sec
, offset1
);
1196 sec
->data_offset
= offset1
;
1197 return sec
->data
+ offset
;
1200 /* return a reference to a section, and create it if it does not
1202 Section
*find_section(TCCState
*s1
, const char *name
)
1206 for(i
= 1; i
< s1
->nb_sections
; i
++) {
1207 sec
= s1
->sections
[i
];
1208 if (!strcmp(name
, sec
->name
))
1211 /* sections are created as PROGBITS */
1212 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
1215 #define SECTION_ABS ((void *)1)
1217 /* update sym->c so that it points to an external symbol in section
1218 'section' with value 'value' */
1219 static void put_extern_sym(Sym
*sym
, Section
*section
,
1220 unsigned long value
, unsigned long size
)
1222 int sym_type
, sym_bind
, sh_num
, info
;
1226 if (section
== NULL
)
1228 else if (section
== SECTION_ABS
)
1231 sh_num
= section
->sh_num
;
1233 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
)
1234 sym_type
= STT_FUNC
;
1236 sym_type
= STT_OBJECT
;
1237 if (sym
->type
.t
& VT_STATIC
)
1238 sym_bind
= STB_LOCAL
;
1240 sym_bind
= STB_GLOBAL
;
1242 name
= get_tok_str(sym
->v
, NULL
);
1243 #ifdef CONFIG_TCC_BCHECK
1244 if (do_bounds_check
) {
1247 /* XXX: avoid doing that for statics ? */
1248 /* if bound checking is activated, we change some function
1249 names by adding the "__bound" prefix */
1252 /* XXX: we rely only on malloc hooks */
1264 strcpy(buf
, "__bound_");
1271 info
= ELF32_ST_INFO(sym_bind
, sym_type
);
1272 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, 0, sh_num
, name
);
1274 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
1275 esym
->st_value
= value
;
1276 esym
->st_size
= size
;
1277 esym
->st_shndx
= sh_num
;
1281 /* add a new relocation entry to symbol 'sym' in section 's' */
1282 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
1285 put_extern_sym(sym
, NULL
, 0, 0);
1286 /* now we can add ELF relocation info */
1287 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
1290 static inline int isid(int c
)
1292 return (c
>= 'a' && c
<= 'z') ||
1293 (c
>= 'A' && c
<= 'Z') ||
1297 static inline int isnum(int c
)
1299 return c
>= '0' && c
<= '9';
1302 static inline int isoct(int c
)
1304 return c
>= '0' && c
<= '7';
1307 static inline int toup(int c
)
1309 if (c
>= 'a' && c
<= 'z')
1310 return c
- 'a' + 'A';
1315 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1319 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1322 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1326 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1330 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1337 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1338 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1339 (*f
)->filename
, (*f
)->line_num
);
1340 if (file
->line_num
> 0) {
1341 strcat_printf(buf
, sizeof(buf
),
1342 "%s:%d: ", file
->filename
, file
->line_num
);
1344 strcat_printf(buf
, sizeof(buf
),
1345 "%s: ", file
->filename
);
1348 strcat_printf(buf
, sizeof(buf
),
1352 strcat_printf(buf
, sizeof(buf
), "warning: ");
1353 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1355 if (!s1
->error_func
) {
1356 /* default case: stderr */
1357 fprintf(stderr
, "%s\n", buf
);
1359 s1
->error_func(s1
->error_opaque
, buf
);
1361 if (!is_warning
|| s1
->warn_error
)
1366 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1367 void (*error_func
)(void *opaque
, const char *msg
))
1369 s
->error_opaque
= error_opaque
;
1370 s
->error_func
= error_func
;
1374 /* error without aborting current compilation */
1375 void error_noabort(const char *fmt
, ...)
1377 TCCState
*s1
= tcc_state
;
1381 error1(s1
, 0, fmt
, ap
);
1385 void error(const char *fmt
, ...)
1387 TCCState
*s1
= tcc_state
;
1391 error1(s1
, 0, fmt
, ap
);
1393 /* better than nothing: in some cases, we accept to handle errors */
1394 if (s1
->error_set_jmp_enabled
) {
1395 longjmp(s1
->error_jmp_buf
, 1);
1397 /* XXX: eliminate this someday */
1402 void expect(const char *msg
)
1404 error("%s expected", msg
);
1407 void warning(const char *fmt
, ...)
1409 TCCState
*s1
= tcc_state
;
1416 error1(s1
, 1, fmt
, ap
);
1423 error("'%c' expected", c
);
1427 static void test_lvalue(void)
1429 if (!(vtop
->r
& VT_LVAL
))
1433 /* allocate a new token */
1434 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1436 TokenSym
*ts
, **ptable
;
1439 if (tok_ident
>= SYM_FIRST_ANOM
)
1440 error("memory full");
1442 /* expand token table if needed */
1443 i
= tok_ident
- TOK_IDENT
;
1444 if ((i
% TOK_ALLOC_INCR
) == 0) {
1445 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1447 error("memory full");
1448 table_ident
= ptable
;
1451 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1452 table_ident
[i
] = ts
;
1453 ts
->tok
= tok_ident
++;
1454 ts
->sym_define
= NULL
;
1455 ts
->sym_label
= NULL
;
1456 ts
->sym_struct
= NULL
;
1457 ts
->sym_identifier
= NULL
;
1459 ts
->hash_next
= NULL
;
1460 memcpy(ts
->str
, str
, len
);
1461 ts
->str
[len
] = '\0';
1466 #define TOK_HASH_INIT 1
1467 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1469 /* find a token and add it if not found */
1470 static TokenSym
*tok_alloc(const char *str
, int len
)
1472 TokenSym
*ts
, **pts
;
1478 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1479 h
&= (TOK_HASH_SIZE
- 1);
1481 pts
= &hash_ident
[h
];
1486 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1488 pts
= &(ts
->hash_next
);
1490 return tok_alloc_new(pts
, str
, len
);
1493 /* CString handling */
1495 static void cstr_realloc(CString
*cstr
, int new_size
)
1500 size
= cstr
->size_allocated
;
1502 size
= 8; /* no need to allocate a too small first string */
1503 while (size
< new_size
)
1505 data
= tcc_realloc(cstr
->data_allocated
, size
);
1507 error("memory full");
1508 cstr
->data_allocated
= data
;
1509 cstr
->size_allocated
= size
;
1514 static inline void cstr_ccat(CString
*cstr
, int ch
)
1517 size
= cstr
->size
+ 1;
1518 if (size
> cstr
->size_allocated
)
1519 cstr_realloc(cstr
, size
);
1520 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1524 static void cstr_cat(CString
*cstr
, const char *str
)
1536 /* add a wide char */
1537 static void cstr_wccat(CString
*cstr
, int ch
)
1540 size
= cstr
->size
+ sizeof(int);
1541 if (size
> cstr
->size_allocated
)
1542 cstr_realloc(cstr
, size
);
1543 *(int *)(((unsigned char *)cstr
->data
) + size
- sizeof(int)) = ch
;
1547 static void cstr_new(CString
*cstr
)
1549 memset(cstr
, 0, sizeof(CString
));
1552 /* free string and reset it to NULL */
1553 static void cstr_free(CString
*cstr
)
1555 tcc_free(cstr
->data_allocated
);
1559 #define cstr_reset(cstr) cstr_free(cstr)
1561 /* XXX: unicode ? */
1562 static void add_char(CString
*cstr
, int c
)
1564 if (c
== '\'' || c
== '\"' || c
== '\\') {
1565 /* XXX: could be more precise if char or string */
1566 cstr_ccat(cstr
, '\\');
1568 if (c
>= 32 && c
<= 126) {
1571 cstr_ccat(cstr
, '\\');
1573 cstr_ccat(cstr
, 'n');
1575 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1576 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1577 cstr_ccat(cstr
, '0' + (c
& 7));
1582 /* XXX: buffer overflow */
1583 /* XXX: float tokens */
1584 char *get_tok_str(int v
, CValue
*cv
)
1586 static char buf
[STRING_MAX_SIZE
+ 1];
1587 static CString cstr_buf
;
1593 /* NOTE: to go faster, we give a fixed buffer for small strings */
1594 cstr_reset(&cstr_buf
);
1595 cstr_buf
.data
= buf
;
1596 cstr_buf
.size_allocated
= sizeof(buf
);
1602 /* XXX: not quite exact, but only useful for testing */
1603 sprintf(p
, "%u", cv
->ui
);
1607 /* XXX: not quite exact, but only useful for testing */
1608 sprintf(p
, "%Lu", cv
->ull
);
1612 cstr_ccat(&cstr_buf
, '\'');
1613 add_char(&cstr_buf
, cv
->i
);
1614 cstr_ccat(&cstr_buf
, '\'');
1615 cstr_ccat(&cstr_buf
, '\0');
1619 len
= cstr
->size
- 1;
1621 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1622 cstr_ccat(&cstr_buf
, '\0');
1627 cstr_ccat(&cstr_buf
, '\"');
1629 len
= cstr
->size
- 1;
1631 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1633 len
= (cstr
->size
/ sizeof(int)) - 1;
1635 add_char(&cstr_buf
, ((int *)cstr
->data
)[i
]);
1637 cstr_ccat(&cstr_buf
, '\"');
1638 cstr_ccat(&cstr_buf
, '\0');
1647 return strcpy(p
, "<<=");
1649 return strcpy(p
, ">>=");
1651 if (v
< TOK_IDENT
) {
1652 /* search in two bytes table */
1666 } else if (v
< tok_ident
) {
1667 return table_ident
[v
- TOK_IDENT
]->str
;
1668 } else if (v
>= SYM_FIRST_ANOM
) {
1669 /* special name for anonymous symbol */
1670 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1672 /* should never happen */
1677 return cstr_buf
.data
;
1680 /* push, without hashing */
1681 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
1695 /* find a symbol and return its associated structure. 's' is the top
1696 of the symbol stack */
1697 static Sym
*sym_find2(Sym
*s
, int v
)
1707 /* structure lookup */
1708 static inline Sym
*struct_find(int v
)
1711 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1713 return table_ident
[v
]->sym_struct
;
1716 /* find an identifier */
1717 static inline Sym
*sym_find(int v
)
1720 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1722 return table_ident
[v
]->sym_identifier
;
1725 /* push a given symbol on the symbol stack */
1726 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1735 s
= sym_push2(ps
, v
, type
->t
, c
);
1736 s
->type
.ref
= type
->ref
;
1738 /* don't record fields or anonymous symbols */
1740 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1741 /* record symbol in token array */
1742 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1744 ps
= &ts
->sym_struct
;
1746 ps
= &ts
->sym_identifier
;
1753 /* push a global identifier */
1754 static Sym
*global_identifier_push(int v
, int t
, int c
)
1757 s
= sym_push2(&global_stack
, v
, t
, c
);
1758 /* don't record anonymous symbol */
1759 if (v
< SYM_FIRST_ANOM
) {
1760 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1761 /* modify the top most local identifier, so that
1762 sym_identifier will point to 's' when popped */
1764 ps
= &(*ps
)->prev_tok
;
1771 /* pop symbols until top reaches 'b' */
1772 static void sym_pop(Sym
**ptop
, Sym
*b
)
1782 /* remove symbol in token array */
1784 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1785 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1787 ps
= &ts
->sym_struct
;
1789 ps
= &ts
->sym_identifier
;
1800 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1805 fd
= open(filename
, O_RDONLY
| O_BINARY
);
1808 bf
= tcc_malloc(sizeof(BufferedFile
));
1814 bf
->buf_ptr
= bf
->buffer
;
1815 bf
->buf_end
= bf
->buffer
;
1816 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
1817 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
1819 bf
->ifndef_macro
= 0;
1820 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
1821 // printf("opening '%s'\n", filename);
1825 void tcc_close(BufferedFile
*bf
)
1827 total_lines
+= bf
->line_num
;
1832 /* fill input buffer and peek next char */
1833 static int tcc_peekc_slow(BufferedFile
*bf
)
1836 /* only tries to read if really end of buffer */
1837 if (bf
->buf_ptr
>= bf
->buf_end
) {
1839 #if defined(PARSE_DEBUG)
1844 len
= read(bf
->fd
, bf
->buffer
, len
);
1851 bf
->buf_ptr
= bf
->buffer
;
1852 bf
->buf_end
= bf
->buffer
+ len
;
1853 *bf
->buf_end
= CH_EOB
;
1855 if (bf
->buf_ptr
< bf
->buf_end
) {
1856 return bf
->buf_ptr
[0];
1858 bf
->buf_ptr
= bf
->buf_end
;
1863 /* return the current character, handling end of block if necessary
1865 static int handle_eob(void)
1867 return tcc_peekc_slow(file
);
1870 /* read next char from current input file and handle end of input buffer */
1871 static inline void inp(void)
1873 ch
= *(++(file
->buf_ptr
));
1874 /* end of buffer/file handling */
1879 /* handle '\[\r]\n' */
1880 static void handle_stray(void)
1882 while (ch
== '\\') {
1887 } else if (ch
== '\r') {
1895 error("stray '\\' in program");
1900 /* skip the stray and handle the \\n case. Output an error if
1901 incorrect char after the stray */
1902 static int handle_stray1(uint8_t *p
)
1906 if (p
>= file
->buf_end
) {
1923 /* handle just the EOB case, but not stray */
1924 #define PEEKC_EOB(c, p)\
1935 /* handle the complicated stray case */
1936 #define PEEKC(c, p)\
1941 c = handle_stray1(p);\
1946 /* input with '\[\r]\n' handling. Note that this function cannot
1947 handle other characters after '\', so you cannot call it inside
1948 strings or comments */
1949 static void minp(void)
1957 /* single line C++ comments */
1958 static uint8_t *parse_line_comment(uint8_t *p
)
1966 if (c
== '\n' || c
== CH_EOF
) {
1968 } else if (c
== '\\') {
1977 } else if (c
== '\r') {
1995 static uint8_t *parse_comment(uint8_t *p
)
2001 /* fast skip loop */
2004 if (c
== '\n' || c
== '*' || c
== '\\')
2008 if (c
== '\n' || c
== '*' || c
== '\\')
2012 /* now we can handle all the cases */
2016 } else if (c
== '*') {
2022 } else if (c
== '/') {
2023 goto end_of_comment
;
2024 } else if (c
== '\\') {
2029 /* skip '\[\r]\n', otherwise just skip the stray */
2035 } else if (c
== '\r') {
2052 /* stray, eob or eof */
2057 error("unexpected end of file in comment");
2058 } else if (c
== '\\') {
2070 /* space exlcuding newline */
2071 static inline int is_space(int ch
)
2073 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
2076 static inline void skip_spaces(void)
2078 while (is_space(ch
))
2082 /* parse a string without interpreting escapes */
2083 static uint8_t *parse_pp_string(uint8_t *p
,
2084 int sep
, CString
*str
)
2092 } else if (c
== '\\') {
2097 unterminated_string
:
2098 /* XXX: indicate line number of start of string */
2099 error("missing terminating %c character", sep
);
2100 } else if (c
== '\\') {
2101 /* escape : just skip \[\r]\n */
2106 } else if (c
== '\r') {
2109 expect("'\n' after '\r'");
2112 } else if (c
== CH_EOF
) {
2113 goto unterminated_string
;
2116 cstr_ccat(str
, '\\');
2122 } else if (c
== '\n') {
2125 } else if (c
== '\r') {
2129 cstr_ccat(str
, '\r');
2145 /* skip block of text until #else, #elif or #endif. skip also pairs of
2147 void preprocess_skip(void)
2149 int a
, start_of_line
, c
;
2176 } else if (c
== '\\') {
2177 /* XXX: incorrect: should not give an error */
2178 ch
= file
->buf_ptr
[0];
2186 p
= parse_pp_string(p
, c
, NULL
);
2195 p
= parse_comment(p
);
2196 } else if (ch
== '/') {
2197 p
= parse_line_comment(p
);
2203 if (start_of_line
) {
2208 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
2210 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
2212 else if (tok
== TOK_ENDIF
)
2226 /* ParseState handling */
2228 /* XXX: currently, no include file info is stored. Thus, we cannot display
2229 accurate messages if the function or data definition spans multiple
2232 /* save current parse state in 's' */
2233 void save_parse_state(ParseState
*s
)
2235 s
->line_num
= file
->line_num
;
2236 s
->macro_ptr
= macro_ptr
;
2241 /* restore parse state from 's' */
2242 void restore_parse_state(ParseState
*s
)
2244 file
->line_num
= s
->line_num
;
2245 macro_ptr
= s
->macro_ptr
;
2250 /* return the number of additional 'ints' necessary to store the
2252 static inline int tok_ext_size(int t
)
2266 error("unsupported token");
2273 return LDOUBLE_SIZE
/ 4;
2279 /* token string handling */
2281 static inline void tok_str_new(TokenString
*s
)
2285 s
->allocated_len
= 0;
2286 s
->last_line_num
= -1;
2289 static void tok_str_free(int *str
)
2294 static int *tok_str_realloc(TokenString
*s
)
2298 if (s
->allocated_len
== 0) {
2301 len
= s
->allocated_len
* 2;
2303 str
= tcc_realloc(s
->str
, len
* sizeof(int));
2305 error("memory full");
2306 s
->allocated_len
= len
;
2311 static void tok_str_add(TokenString
*s
, int t
)
2317 if (len
>= s
->allocated_len
)
2318 str
= tok_str_realloc(s
);
2323 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2330 /* allocate space for worst case */
2331 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2332 str
= tok_str_realloc(s
);
2341 str
[len
++] = cv
->tab
[0];
2350 nb_words
= (sizeof(CString
) + cv
->cstr
->size
+ 3) >> 2;
2351 while ((len
+ nb_words
) > s
->allocated_len
)
2352 str
= tok_str_realloc(s
);
2353 cstr
= (CString
*)(str
+ len
);
2355 cstr
->size
= cv
->cstr
->size
;
2356 cstr
->data_allocated
= NULL
;
2357 cstr
->size_allocated
= cstr
->size
;
2358 memcpy((char *)cstr
+ sizeof(CString
),
2359 cv
->cstr
->data
, cstr
->size
);
2366 #if LDOUBLE_SIZE == 8
2369 str
[len
++] = cv
->tab
[0];
2370 str
[len
++] = cv
->tab
[1];
2372 #if LDOUBLE_SIZE == 12
2374 str
[len
++] = cv
->tab
[0];
2375 str
[len
++] = cv
->tab
[1];
2376 str
[len
++] = cv
->tab
[2];
2377 #elif LDOUBLE_SIZE != 8
2378 #error add long double size support
2387 /* add the current parse token in token string 's' */
2388 static void tok_str_add_tok(TokenString
*s
)
2392 /* save line number info */
2393 if (file
->line_num
!= s
->last_line_num
) {
2394 s
->last_line_num
= file
->line_num
;
2395 cval
.i
= s
->last_line_num
;
2396 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2398 tok_str_add2(s
, tok
, &tokc
);
2401 #if LDOUBLE_SIZE == 12
2402 #define LDOUBLE_GET(p, cv) \
2406 #elif LDOUBLE_SIZE == 8
2407 #define LDOUBLE_GET(p, cv) \
2411 #error add long double size support
2415 /* get a token from an integer array and increment pointer
2416 accordingly. we code it as a macro to avoid pointer aliasing. */
2417 #define TOK_GET(t, p, cv) \
2432 cv.cstr = (CString *)p; \
2433 cv.cstr->data = (char *)p + sizeof(CString);\
2434 p += (sizeof(CString) + cv.cstr->size + 3) >> 2;\
2443 case TOK_CLDOUBLE: \
2444 LDOUBLE_GET(p, cv); \
2445 p += LDOUBLE_SIZE / 4; \
2452 /* defines handling */
2453 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2457 s
= sym_push2(&define_stack
, v
, macro_type
, (int)str
);
2458 s
->next
= first_arg
;
2459 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2462 /* undefined a define symbol. Its name is just set to zero */
2463 static void define_undef(Sym
*s
)
2467 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2468 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2472 static inline Sym
*define_find(int v
)
2475 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2477 return table_ident
[v
]->sym_define
;
2480 /* free define stack until top reaches 'b' */
2481 static void free_defines(Sym
*b
)
2489 /* do not free args or predefined defines */
2491 tok_str_free((int *)top
->c
);
2493 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2494 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2502 static Sym
*label_find(int v
)
2505 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2507 return table_ident
[v
]->sym_label
;
2510 static Sym
*label_push(Sym
**ptop
, int v
, int flags
)
2513 s
= sym_push2(ptop
, v
, 0, 0);
2515 ps
= &table_ident
[v
- TOK_IDENT
]->sym_label
;
2516 if (ptop
== &global_label_stack
) {
2517 /* modify the top most local identifier, so that
2518 sym_identifier will point to 's' when popped */
2520 ps
= &(*ps
)->prev_tok
;
2527 /* pop labels until element last is reached. Look if any labels are
2528 undefined. Define symbols if '&&label' was used. */
2529 static void label_pop(Sym
**ptop
, Sym
*slast
)
2532 for(s
= *ptop
; s
!= slast
; s
= s1
) {
2534 if (s
->r
== LABEL_DECLARED
) {
2535 warning("label '%s' declared but not used", get_tok_str(s
->v
, NULL
));
2536 } else if (s
->r
== LABEL_FORWARD
) {
2537 error("label '%s' used but not defined",
2538 get_tok_str(s
->v
, NULL
));
2541 /* define corresponding symbol. A size of
2543 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
2547 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= s
->prev_tok
;
2553 /* eval an expression for #if/#elif */
2554 static int expr_preprocess(void)
2560 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2561 next(); /* do macro subst */
2562 if (tok
== TOK_DEFINED
) {
2567 c
= define_find(tok
) != 0;
2572 } else if (tok
>= TOK_IDENT
) {
2573 /* if undefined macro */
2577 tok_str_add_tok(&str
);
2579 tok_str_add(&str
, -1); /* simulate end of file */
2580 tok_str_add(&str
, 0);
2581 /* now evaluate C constant expression */
2582 macro_ptr
= str
.str
;
2586 tok_str_free(str
.str
);
2590 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2591 static void tok_print(int *str
)
2597 TOK_GET(t
, str
, cval
);
2600 printf(" %s", get_tok_str(t
, &cval
));
2606 /* parse after #define */
2607 static void parse_define(void)
2609 Sym
*s
, *first
, **ps
;
2610 int v
, t
, varg
, is_vaargs
, c
;
2615 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2616 /* XXX: should check if same macro (ANSI) */
2619 /* '(' must be just after macro definition for MACRO_FUNC */
2620 c
= file
->buf_ptr
[0];
2622 c
= handle_stray1(file
->buf_ptr
);
2627 while (tok
!= ')') {
2631 if (varg
== TOK_DOTS
) {
2632 varg
= TOK___VA_ARGS__
;
2634 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2638 if (varg
< TOK_IDENT
)
2639 error("badly punctuated parameter list");
2640 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2651 /* EOF testing necessary for '-D' handling */
2652 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2653 tok_str_add2(&str
, tok
, &tokc
);
2656 tok_str_add(&str
, 0);
2658 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2661 define_push(v
, t
, str
.str
, first
);
2664 static inline int hash_cached_include(int type
, const char *filename
)
2666 const unsigned char *s
;
2670 h
= TOK_HASH_FUNC(h
, type
);
2673 h
= TOK_HASH_FUNC(h
, *s
);
2676 h
&= (CACHED_INCLUDES_HASH_SIZE
- 1);
2680 /* XXX: use a token or a hash table to accelerate matching ? */
2681 static CachedInclude
*search_cached_include(TCCState
*s1
,
2682 int type
, const char *filename
)
2686 h
= hash_cached_include(type
, filename
);
2687 i
= s1
->cached_includes_hash
[h
];
2691 e
= s1
->cached_includes
[i
- 1];
2692 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2699 static inline void add_cached_include(TCCState
*s1
, int type
,
2700 const char *filename
, int ifndef_macro
)
2705 if (search_cached_include(s1
, type
, filename
))
2708 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2710 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2714 strcpy(e
->filename
, filename
);
2715 e
->ifndef_macro
= ifndef_macro
;
2716 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2717 /* add in hash table */
2718 h
= hash_cached_include(type
, filename
);
2719 e
->hash_next
= s1
->cached_includes_hash
[h
];
2720 s1
->cached_includes_hash
[h
] = s1
->nb_cached_includes
;
2723 static void pragma_parse(TCCState
*s1
)
2728 if (tok
== TOK_pack
) {
2731 #pragma pack(1) // set
2732 #pragma pack() // reset to default
2733 #pragma pack(push,1) // push & set
2734 #pragma pack(pop) // restore previous
2738 if (tok
== TOK_ASM_pop
) {
2740 if (s1
->pack_stack_ptr
<= s1
->pack_stack
) {
2742 error("out of pack stack");
2744 s1
->pack_stack_ptr
--;
2748 if (tok
== TOK_ASM_push
) {
2750 if (s1
->pack_stack_ptr
>= s1
->pack_stack
+ PACK_STACK_SIZE
- 1)
2752 s1
->pack_stack_ptr
++;
2755 if (tok
!= TOK_CINT
) {
2757 error("invalid pack pragma");
2760 if (val
< 1 || val
> 16 || (val
& (val
- 1)) != 0)
2764 *s1
->pack_stack_ptr
= val
;
2770 /* is_bof is true if first non space token at beginning of file */
2771 static void preprocess(int is_bof
)
2773 TCCState
*s1
= tcc_state
;
2774 int size
, i
, c
, n
, saved_parse_flags
;
2775 char buf
[1024], *q
, *p
;
2781 saved_parse_flags
= parse_flags
;
2782 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
|
2783 PARSE_FLAG_LINEFEED
;
2793 s
= define_find(tok
);
2794 /* undefine symbol by putting an invalid name */
2799 case TOK_INCLUDE_NEXT
:
2800 ch
= file
->buf_ptr
[0];
2801 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2806 } else if (ch
== '\"') {
2809 /* XXX: better stray handling */
2812 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
2813 if ((q
- buf
) < sizeof(buf
) - 1)
2820 /* eat all spaces and comments after include */
2821 /* XXX: slightly incorrect */
2822 while (ch1
!= '\n' && ch1
!= CH_EOF
)
2826 /* computed #include : either we have only strings or
2827 we have anything enclosed in '<>' */
2830 if (tok
== TOK_STR
) {
2831 while (tok
!= TOK_LINEFEED
) {
2832 if (tok
!= TOK_STR
) {
2834 error("'#include' expects \"FILENAME\" or <FILENAME>");
2836 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
2842 while (tok
!= TOK_LINEFEED
) {
2843 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
2847 /* check syntax and remove '<>' */
2848 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
2849 goto include_syntax
;
2850 memmove(buf
, buf
+ 1, len
- 2);
2851 buf
[len
- 2] = '\0';
2856 e
= search_cached_include(s1
, c
, buf
);
2857 if (e
&& define_find(e
->ifndef_macro
)) {
2858 /* no need to parse the include because the 'ifndef macro'
2861 printf("%s: skipping %s\n", file
->filename
, buf
);
2865 /* first search in current dir if "header.h" */
2867 p
= strrchr(file
->filename
, '/');
2869 size
= p
+ 1 - file
->filename
;
2870 if (size
> sizeof(buf1
) - 1)
2871 size
= sizeof(buf1
) - 1;
2872 memcpy(buf1
, file
->filename
, size
);
2874 pstrcat(buf1
, sizeof(buf1
), buf
);
2875 f
= tcc_open(s1
, buf1
);
2877 if (tok
== TOK_INCLUDE_NEXT
)
2883 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
2884 error("#include recursion too deep");
2885 /* now search in all the include paths */
2886 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
2887 for(i
= 0; i
< n
; i
++) {
2889 if (i
< s1
->nb_include_paths
)
2890 path
= s1
->include_paths
[i
];
2892 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
2893 pstrcpy(buf1
, sizeof(buf1
), path
);
2894 pstrcat(buf1
, sizeof(buf1
), "/");
2895 pstrcat(buf1
, sizeof(buf1
), buf
);
2896 f
= tcc_open(s1
, buf1
);
2898 if (tok
== TOK_INCLUDE_NEXT
)
2904 error("include file '%s' not found", buf
);
2908 printf("%s: including %s\n", file
->filename
, buf1
);
2911 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
2912 /* push current file in stack */
2913 /* XXX: fix current line init */
2914 *s1
->include_stack_ptr
++ = file
;
2916 /* add include file debug info */
2918 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
2920 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
2921 ch
= file
->buf_ptr
[0];
2929 c
= expr_preprocess();
2935 if (tok
< TOK_IDENT
)
2936 error("invalid argument for '#if%sdef'", c
? "n" : "");
2940 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
2942 file
->ifndef_macro
= tok
;
2945 c
= (define_find(tok
) != 0) ^ c
;
2947 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
2948 error("memory full");
2949 *s1
->ifdef_stack_ptr
++ = c
;
2952 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2953 error("#else without matching #if");
2954 if (s1
->ifdef_stack_ptr
[-1] & 2)
2955 error("#else after #else");
2956 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
2959 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2960 error("#elif without matching #if");
2961 c
= s1
->ifdef_stack_ptr
[-1];
2963 error("#elif after #else");
2964 /* last #if/#elif expression was true: we skip */
2967 c
= expr_preprocess();
2968 s1
->ifdef_stack_ptr
[-1] = c
;
2978 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
2979 error("#endif without matching #if");
2980 s1
->ifdef_stack_ptr
--;
2981 /* '#ifndef macro' was at the start of file. Now we check if
2982 an '#endif' is exactly at the end of file */
2983 if (file
->ifndef_macro
&&
2984 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
2985 file
->ifndef_macro_saved
= file
->ifndef_macro
;
2986 /* need to set to zero to avoid false matches if another
2987 #ifndef at middle of file */
2988 file
->ifndef_macro
= 0;
2989 while (tok
!= TOK_LINEFEED
)
2991 tok_flags
|= TOK_FLAG_ENDIF
;
2997 if (tok
!= TOK_CINT
)
2999 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
3001 if (tok
!= TOK_LINEFEED
) {
3004 pstrcpy(file
->filename
, sizeof(file
->filename
),
3005 (char *)tokc
.cstr
->data
);
3011 ch
= file
->buf_ptr
[0];
3014 while (ch
!= '\n' && ch
!= CH_EOF
) {
3015 if ((q
- buf
) < sizeof(buf
) - 1)
3021 error("#error %s", buf
);
3023 warning("#warning %s", buf
);
3029 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
3030 /* '!' is ignored to allow C scripts. numbers are ignored
3031 to emulate cpp behaviour */
3033 if (!(saved_parse_flags
& PARSE_FLAG_ASM_COMMENTS
))
3034 error("invalid preprocessing directive #%s", get_tok_str(tok
, &tokc
));
3038 /* ignore other preprocess commands or #! for C scripts */
3039 while (tok
!= TOK_LINEFEED
)
3042 parse_flags
= saved_parse_flags
;
3045 /* evaluate escape codes in a string. */
3046 static void parse_escape_string(CString
*outstr
, const uint8_t *buf
, int is_long
)
3061 case '0': case '1': case '2': case '3':
3062 case '4': case '5': case '6': case '7':
3063 /* at most three octal digits */
3068 n
= n
* 8 + c
- '0';
3072 n
= n
* 8 + c
- '0';
3077 goto add_char_nonext
;
3083 if (c
>= 'a' && c
<= 'f')
3085 else if (c
>= 'A' && c
<= 'F')
3095 goto add_char_nonext
;
3119 goto invalid_escape
;
3129 if (c
>= '!' && c
<= '~')
3130 warning("unknown escape sequence: \'\\%c\'", c
);
3132 warning("unknown escape sequence: \'\\x%x\'", c
);
3139 cstr_ccat(outstr
, c
);
3141 cstr_wccat(outstr
, c
);
3143 /* add a trailing '\0' */
3145 cstr_ccat(outstr
, '\0');
3147 cstr_wccat(outstr
, '\0');
3150 /* we use 64 bit numbers */
3153 /* bn = (bn << shift) | or_val */
3154 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
3158 for(i
=0;i
<BN_SIZE
;i
++) {
3160 bn
[i
] = (v
<< shift
) | or_val
;
3161 or_val
= v
>> (32 - shift
);
3165 void bn_zero(unsigned int *bn
)
3168 for(i
=0;i
<BN_SIZE
;i
++) {
3173 /* parse number in null terminated string 'p' and return it in the
3175 void parse_number(const char *p
)
3177 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
3179 unsigned int bn
[BN_SIZE
];
3190 goto float_frac_parse
;
3191 } else if (t
== '0') {
3192 if (ch
== 'x' || ch
== 'X') {
3196 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
3202 /* parse all digits. cannot check octal numbers at this stage
3203 because of floating point constants */
3205 if (ch
>= 'a' && ch
<= 'f')
3207 else if (ch
>= 'A' && ch
<= 'F')
3215 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
3217 error("number too long");
3223 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
3224 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
3226 /* NOTE: strtox should support that for hexa numbers, but
3227 non ISOC99 libcs do not support it, so we prefer to do
3229 /* hexadecimal or binary floats */
3230 /* XXX: handle overflows */
3242 } else if (t
>= 'a') {
3244 } else if (t
>= 'A') {
3249 bn_lshift(bn
, shift
, t
);
3256 if (t
>= 'a' && t
<= 'f') {
3258 } else if (t
>= 'A' && t
<= 'F') {
3260 } else if (t
>= '0' && t
<= '9') {
3266 error("invalid digit");
3267 bn_lshift(bn
, shift
, t
);
3272 if (ch
!= 'p' && ch
!= 'P')
3279 } else if (ch
== '-') {
3283 if (ch
< '0' || ch
> '9')
3284 expect("exponent digits");
3285 while (ch
>= '0' && ch
<= '9') {
3286 exp_val
= exp_val
* 10 + ch
- '0';
3289 exp_val
= exp_val
* s
;
3291 /* now we can generate the number */
3292 /* XXX: should patch directly float number */
3293 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
3294 d
= ldexp(d
, exp_val
- frac_bits
);
3299 /* float : should handle overflow */
3301 } else if (t
== 'L') {
3304 /* XXX: not large enough */
3305 tokc
.ld
= (long double)d
;
3311 /* decimal floats */
3313 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3318 while (ch
>= '0' && ch
<= '9') {
3319 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3325 if (ch
== 'e' || ch
== 'E') {
3326 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3330 if (ch
== '-' || ch
== '+') {
3331 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3336 if (ch
< '0' || ch
> '9')
3337 expect("exponent digits");
3338 while (ch
>= '0' && ch
<= '9') {
3339 if (q
>= token_buf
+ STRING_MAX_SIZE
)
3351 tokc
.f
= strtof(token_buf
, NULL
);
3352 } else if (t
== 'L') {
3355 tokc
.ld
= strtold(token_buf
, NULL
);
3358 tokc
.d
= strtod(token_buf
, NULL
);
3362 unsigned long long n
, n1
;
3365 /* integer number */
3368 if (b
== 10 && *q
== '0') {
3375 /* no need for checks except for base 10 / 8 errors */
3378 } else if (t
>= 'a') {
3380 } else if (t
>= 'A') {
3385 error("invalid digit");
3389 /* detect overflow */
3390 /* XXX: this test is not reliable */
3392 error("integer constant overflow");
3395 /* XXX: not exactly ANSI compliant */
3396 if ((n
& 0xffffffff00000000LL
) != 0) {
3401 } else if (n
> 0x7fffffff) {
3412 error("three 'l's in integer constant");
3415 if (tok
== TOK_CINT
)
3417 else if (tok
== TOK_CUINT
)
3421 } else if (t
== 'U') {
3423 error("two 'u's in integer constant");
3425 if (tok
== TOK_CINT
)
3427 else if (tok
== TOK_CLLONG
)
3434 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
3442 #define PARSE2(c1, tok1, c2, tok2) \
3453 /* return next token without macro substitution */
3454 static inline void next_nomacro1(void)
3474 /* first look if it is in fact an end of buffer */
3475 if (p
>= file
->buf_end
) {
3479 if (p
>= file
->buf_end
)
3492 TCCState
*s1
= tcc_state
;
3493 if (parse_flags
& PARSE_FLAG_LINEFEED
) {
3495 } else if (s1
->include_stack_ptr
== s1
->include_stack
||
3496 !(parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3497 /* no include left : end of file. */
3500 /* pop include file */
3502 /* test if previous '#endif' was after a #ifdef at
3504 if (tok_flags
& TOK_FLAG_ENDIF
) {
3506 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3508 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3509 file
->ifndef_macro_saved
);
3512 /* add end of include file debug info */
3514 put_stabd(N_EINCL
, 0, 0);
3516 /* pop include stack */
3518 s1
->include_stack_ptr
--;
3519 file
= *s1
->include_stack_ptr
;
3527 if (parse_flags
& PARSE_FLAG_LINEFEED
) {
3531 tok_flags
|= TOK_FLAG_BOL
;
3540 if ((tok_flags
& TOK_FLAG_BOL
) &&
3541 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
3543 preprocess(tok_flags
& TOK_FLAG_BOF
);
3549 tok
= TOK_TWOSHARPS
;
3551 if (parse_flags
& PARSE_FLAG_ASM_COMMENTS
) {
3552 p
= parse_line_comment(p
- 1);
3561 case 'a': case 'b': case 'c': case 'd':
3562 case 'e': case 'f': case 'g': case 'h':
3563 case 'i': case 'j': case 'k': case 'l':
3564 case 'm': case 'n': case 'o': case 'p':
3565 case 'q': case 'r': case 's': case 't':
3566 case 'u': case 'v': case 'w': case 'x':
3568 case 'A': case 'B': case 'C': case 'D':
3569 case 'E': case 'F': case 'G': case 'H':
3570 case 'I': case 'J': case 'K':
3571 case 'M': case 'N': case 'O': case 'P':
3572 case 'Q': case 'R': case 'S': case 'T':
3573 case 'U': case 'V': case 'W': case 'X':
3579 h
= TOK_HASH_FUNC(h
, c
);
3583 if (!isidnum_table
[c
])
3585 h
= TOK_HASH_FUNC(h
, c
);
3592 /* fast case : no stray found, so we have the full token
3593 and we have already hashed it */
3595 h
&= (TOK_HASH_SIZE
- 1);
3596 pts
= &hash_ident
[h
];
3601 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3603 pts
= &(ts
->hash_next
);
3605 ts
= tok_alloc_new(pts
, p1
, len
);
3609 cstr_reset(&tokcstr
);
3612 cstr_ccat(&tokcstr
, *p1
);
3618 while (isidnum_table
[c
]) {
3619 cstr_ccat(&tokcstr
, c
);
3622 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3628 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3630 goto parse_ident_fast
;
3633 if (c
== '\'' || c
== '\"') {
3637 cstr_reset(&tokcstr
);
3638 cstr_ccat(&tokcstr
, 'L');
3639 goto parse_ident_slow
;
3643 case '0': case '1': case '2': case '3':
3644 case '4': case '5': case '6': case '7':
3647 cstr_reset(&tokcstr
);
3648 /* after the first digit, accept digits, alpha, '.' or sign if
3649 prefixed by 'eEpP' */
3653 cstr_ccat(&tokcstr
, c
);
3655 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3656 ((c
== '+' || c
== '-') &&
3657 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3660 /* We add a trailing '\0' to ease parsing */
3661 cstr_ccat(&tokcstr
, '\0');
3662 tokc
.cstr
= &tokcstr
;
3666 /* special dot handling because it can also start a number */
3669 cstr_reset(&tokcstr
);
3670 cstr_ccat(&tokcstr
, '.');
3672 } else if (c
== '.') {
3692 /* parse the string */
3694 p
= parse_pp_string(p
, sep
, &str
);
3695 cstr_ccat(&str
, '\0');
3697 /* eval the escape (should be done as TOK_PPNUM) */
3698 cstr_reset(&tokcstr
);
3699 parse_escape_string(&tokcstr
, str
.data
, is_long
);
3704 /* XXX: make it portable */
3708 char_size
= sizeof(int);
3709 if (tokcstr
.size
<= char_size
)
3710 error("empty character constant");
3711 if (tokcstr
.size
> 2 * char_size
)
3712 warning("multi-character character constant");
3714 tokc
.i
= *(int8_t *)tokcstr
.data
;
3717 tokc
.i
= *(int *)tokcstr
.data
;
3721 tokc
.cstr
= &tokcstr
;
3735 } else if (c
== '<') {
3753 } else if (c
== '>') {
3771 } else if (c
== '=') {
3784 } else if (c
== '=') {
3797 } else if (c
== '=') {
3810 } else if (c
== '=') {
3813 } else if (c
== '>') {
3821 PARSE2('!', '!', '=', TOK_NE
)
3822 PARSE2('=', '=', '=', TOK_EQ
)
3823 PARSE2('*', '*', '=', TOK_A_MUL
)
3824 PARSE2('%', '%', '=', TOK_A_MOD
)
3825 PARSE2('^', '^', '=', TOK_A_XOR
)
3827 /* comments or operator */
3831 p
= parse_comment(p
);
3833 } else if (c
== '/') {
3834 p
= parse_line_comment(p
);
3836 } else if (c
== '=') {
3856 case '$': /* only used in assembler */
3861 error("unrecognized character \\x%02x", c
);
3866 #if defined(PARSE_DEBUG)
3867 printf("token = %s\n", get_tok_str(tok
, &tokc
));
3871 /* return next token without macro substitution. Can read input from
3873 static void next_nomacro(void)
3879 TOK_GET(tok
, macro_ptr
, tokc
);
3880 if (tok
== TOK_LINENUM
) {
3881 file
->line_num
= tokc
.i
;
3890 /* substitute args in macro_str and return allocated string */
3891 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
3893 int *st
, last_tok
, t
, notfirst
;
3902 TOK_GET(t
, macro_str
, cval
);
3907 TOK_GET(t
, macro_str
, cval
);
3910 s
= sym_find2(args
, t
);
3917 cstr_ccat(&cstr
, ' ');
3918 TOK_GET(t
, st
, cval
);
3919 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
3922 cstr_ccat(&cstr
, '\0');
3924 printf("stringize: %s\n", (char *)cstr
.data
);
3928 tok_str_add2(&str
, TOK_STR
, &cval
);
3931 tok_str_add2(&str
, t
, &cval
);
3933 } else if (t
>= TOK_IDENT
) {
3934 s
= sym_find2(args
, t
);
3937 /* if '##' is present before or after, no arg substitution */
3938 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
3939 /* special case for var arg macros : ## eats the
3940 ',' if empty VA_ARGS variable. */
3941 /* XXX: test of the ',' is not 100%
3942 reliable. should fix it to avoid security
3944 if (gnu_ext
&& s
->type
.t
&&
3945 last_tok
== TOK_TWOSHARPS
&&
3946 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
3948 /* suppress ',' '##' */
3951 /* suppress '##' and add variable */
3959 TOK_GET(t1
, st
, cval
);
3962 tok_str_add2(&str
, t1
, &cval
);
3966 /* NOTE: the stream cannot be read when macro
3967 substituing an argument */
3968 macro_subst(&str
, nested_list
, st
, 0);
3971 tok_str_add(&str
, t
);
3974 tok_str_add2(&str
, t
, &cval
);
3978 tok_str_add(&str
, 0);
3982 static char const ab_month_name
[12][4] =
3984 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
3985 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
3988 /* do macro substitution of current token with macro 's' and add
3989 result to (tok_str,tok_len). 'nested_list' is the list of all
3990 macros we got inside to avoid recursing. Return non zero if no
3991 substitution needs to be done */
3992 static int macro_subst_tok(TokenString
*tok_str
,
3993 Sym
**nested_list
, Sym
*s
, int can_read_stream
)
3995 Sym
*args
, *sa
, *sa1
;
3996 int mstr_allocated
, parlevel
, *mstr
, t
, t1
;
4003 /* if symbol is a macro, prepare substitution */
4004 /* special macros */
4005 if (tok
== TOK___LINE__
) {
4006 snprintf(buf
, sizeof(buf
), "%d", file
->line_num
);
4010 } else if (tok
== TOK___FILE__
) {
4011 cstrval
= file
->filename
;
4013 } else if (tok
== TOK___DATE__
|| tok
== TOK___TIME__
) {
4018 tm
= localtime(&ti
);
4019 if (tok
== TOK___DATE__
) {
4020 snprintf(buf
, sizeof(buf
), "%s %2d %d",
4021 ab_month_name
[tm
->tm_mon
], tm
->tm_mday
, tm
->tm_year
+ 1900);
4023 snprintf(buf
, sizeof(buf
), "%02d:%02d:%02d",
4024 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
4031 cstr_cat(&cstr
, cstrval
);
4032 cstr_ccat(&cstr
, '\0');
4034 tok_str_add2(tok_str
, t1
, &cval
);
4039 if (s
->type
.t
== MACRO_FUNC
) {
4040 /* NOTE: we do not use next_nomacro to avoid eating the
4041 next token. XXX: find better solution */
4044 if (t
== 0 && can_read_stream
) {
4045 /* end of macro stream: we must look at the token
4046 after in the file */
4052 /* XXX: incorrect with comments */
4053 ch
= file
->buf_ptr
[0];
4054 while (is_space(ch
) || ch
== '\n')
4058 if (t
!= '(') /* no macro subst */
4061 /* argument macro */
4066 /* NOTE: empty args are allowed, except if no args */
4068 /* handle '()' case */
4069 if (!args
&& !sa
&& tok
== ')')
4072 error("macro '%s' used with too many args",
4073 get_tok_str(s
->v
, 0));
4076 /* NOTE: non zero sa->t indicates VA_ARGS */
4077 while ((parlevel
> 0 ||
4079 (tok
!= ',' || sa
->type
.t
))) &&
4083 else if (tok
== ')')
4085 tok_str_add2(&str
, tok
, &tokc
);
4088 tok_str_add(&str
, 0);
4089 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (int)str
.str
);
4092 /* special case for gcc var args: add an empty
4093 var arg argument if it is omitted */
4094 if (sa
&& sa
->type
.t
&& gnu_ext
)
4104 error("macro '%s' used with too few args",
4105 get_tok_str(s
->v
, 0));
4108 /* now subst each arg */
4109 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
4114 tok_str_free((int *)sa
->c
);
4120 sym_push2(nested_list
, s
->v
, 0, 0);
4121 macro_subst(tok_str
, nested_list
, mstr
, 1);
4122 /* pop nested defined symbol */
4124 *nested_list
= sa1
->prev
;
4132 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
4133 return the resulting string (which must be freed). */
4134 static inline int *macro_twosharps(const int *macro_str
)
4137 const int *macro_ptr1
, *start_macro_ptr
, *ptr
, *saved_macro_ptr
;
4139 const char *p1
, *p2
;
4141 TokenString macro_str1
;
4144 start_macro_ptr
= macro_str
;
4145 /* we search the first '##' */
4147 macro_ptr1
= macro_str
;
4148 TOK_GET(t
, macro_str
, cval
);
4149 /* nothing more to do if end of string */
4152 if (*macro_str
== TOK_TWOSHARPS
)
4156 /* we saw '##', so we need more processing to handle it */
4158 tok_str_new(¯o_str1
);
4162 /* add all tokens seen so far */
4163 for(ptr
= start_macro_ptr
; ptr
< macro_ptr1
;) {
4164 TOK_GET(t
, ptr
, cval
);
4165 tok_str_add2(¯o_str1
, t
, &cval
);
4167 saved_macro_ptr
= macro_ptr
;
4168 /* XXX: get rid of the use of macro_ptr here */
4169 macro_ptr
= (int *)macro_str
;
4171 while (*macro_ptr
== TOK_TWOSHARPS
) {
4173 macro_ptr1
= macro_ptr
;
4176 TOK_GET(t
, macro_ptr
, cval
);
4177 /* We concatenate the two tokens if we have an
4178 identifier or a preprocessing number */
4180 p1
= get_tok_str(tok
, &tokc
);
4181 cstr_cat(&cstr
, p1
);
4182 p2
= get_tok_str(t
, &cval
);
4183 cstr_cat(&cstr
, p2
);
4184 cstr_ccat(&cstr
, '\0');
4186 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
4187 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
4188 if (tok
== TOK_PPNUM
) {
4189 /* if number, then create a number token */
4190 /* NOTE: no need to allocate because
4191 tok_str_add2() does it */
4194 /* if identifier, we must do a test to
4195 validate we have a correct identifier */
4196 if (t
== TOK_PPNUM
) {
4206 if (!isnum(c
) && !isid(c
))
4210 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
4211 tok
= ts
->tok
; /* modify current token */
4214 const char *str
= cstr
.data
;
4215 const unsigned char *q
;
4217 /* we look for a valid token */
4218 /* XXX: do more extensive checks */
4219 if (!strcmp(str
, ">>=")) {
4221 } else if (!strcmp(str
, "<<=")) {
4223 } else if (strlen(str
) == 2) {
4224 /* search in two bytes table */
4229 if (q
[0] == str
[0] && q
[1] == str
[1])
4236 /* NOTE: because get_tok_str use a static buffer,
4239 p1
= get_tok_str(tok
, &tokc
);
4240 cstr_cat(&cstr
, p1
);
4241 cstr_ccat(&cstr
, '\0');
4242 p2
= get_tok_str(t
, &cval
);
4243 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
4244 /* cannot merge tokens: just add them separately */
4245 tok_str_add2(¯o_str1
, tok
, &tokc
);
4246 /* XXX: free associated memory ? */
4253 tok_str_add2(¯o_str1
, tok
, &tokc
);
4258 macro_ptr
= (int *)saved_macro_ptr
;
4260 tok_str_add(¯o_str1
, 0);
4261 return macro_str1
.str
;
4265 /* do macro substitution of macro_str and add result to
4266 (tok_str,tok_len). 'nested_list' is the list of all macros we got
4267 inside to avoid recursing. */
4268 static void macro_subst(TokenString
*tok_str
, Sym
**nested_list
,
4269 const int *macro_str
, int can_read_stream
)
4272 int *saved_macro_ptr
, *macro_str1
;
4277 /* first scan for '##' operator handling */
4279 macro_str1
= macro_twosharps(ptr
);
4283 /* NOTE: ptr == NULL can only happen if tokens are read from
4284 file stream due to a macro function call */
4287 TOK_GET(t
, ptr
, cval
);
4292 /* if nested substitution, do nothing */
4293 if (sym_find2(*nested_list
, t
))
4295 saved_macro_ptr
= macro_ptr
;
4296 macro_ptr
= (int *)ptr
;
4298 ret
= macro_subst_tok(tok_str
, nested_list
, s
, can_read_stream
);
4299 ptr
= (int *)macro_ptr
;
4300 macro_ptr
= saved_macro_ptr
;
4305 tok_str_add2(tok_str
, t
, &cval
);
4309 tok_str_free(macro_str1
);
4312 /* return next token with macro substitution */
4313 static void next(void)
4315 Sym
*nested_list
, *s
;
4321 /* if not reading from macro substituted string, then try
4322 to substitute macros */
4323 if (tok
>= TOK_IDENT
&&
4324 (parse_flags
& PARSE_FLAG_PREPROCESS
)) {
4325 s
= define_find(tok
);
4327 /* we have a macro: we try to substitute */
4330 if (macro_subst_tok(&str
, &nested_list
, s
, 1) == 0) {
4331 /* substitution done, NOTE: maybe empty */
4332 tok_str_add(&str
, 0);
4333 macro_ptr
= str
.str
;
4334 macro_ptr_allocated
= str
.str
;
4341 /* end of macro or end of unget buffer */
4342 if (unget_buffer_enabled
) {
4343 macro_ptr
= unget_saved_macro_ptr
;
4344 unget_buffer_enabled
= 0;
4346 /* end of macro string: free it */
4347 tok_str_free(macro_ptr_allocated
);
4354 /* convert preprocessor tokens into C tokens */
4355 if (tok
== TOK_PPNUM
&&
4356 (parse_flags
& PARSE_FLAG_TOK_NUM
)) {
4357 parse_number((char *)tokc
.cstr
->data
);
4361 /* push back current token and set current token to 'last_tok'. Only
4362 identifier case handled for labels. */
4363 static inline void unget_tok(int last_tok
)
4367 unget_saved_macro_ptr
= macro_ptr
;
4368 unget_buffer_enabled
= 1;
4369 q
= unget_saved_buffer
;
4372 n
= tok_ext_size(tok
) - 1;
4375 *q
= 0; /* end of token string */
4380 void swap(int *p
, int *q
)
4388 void vsetc(CType
*type
, int r
, CValue
*vc
)
4392 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4393 error("memory full");
4394 /* cannot let cpu flags if other instruction are generated. Also
4395 avoid leaving VT_JMP anywhere except on the top of the stack
4396 because it would complicate the code generator. */
4397 if (vtop
>= vstack
) {
4398 v
= vtop
->r
& VT_VALMASK
;
4399 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
4405 vtop
->r2
= VT_CONST
;
4409 /* push integer constant */
4414 vsetc(&int_type
, VT_CONST
, &cval
);
4417 /* Return a static symbol pointing to a section */
4418 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
4419 unsigned long offset
, unsigned long size
)
4425 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
4426 sym
->type
.ref
= type
->ref
;
4427 sym
->r
= VT_CONST
| VT_SYM
;
4428 put_extern_sym(sym
, sec
, offset
, size
);
4432 /* push a reference to a section offset by adding a dummy symbol */
4433 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
4438 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4439 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
4442 /* define a new external reference to a symbol 'v' of type 'u' */
4443 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
4449 /* push forward reference */
4450 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
4451 s
->type
.ref
= type
->ref
;
4452 s
->r
= r
| VT_CONST
| VT_SYM
;
4457 /* define a new external reference to a symbol 'v' of type 'u' */
4458 static Sym
*external_sym(int v
, CType
*type
, int r
)
4464 /* push forward reference */
4465 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
4466 s
->type
.t
|= VT_EXTERN
;
4468 if (!is_compatible_types(&s
->type
, type
))
4469 error("incompatible types for redefinition of '%s'",
4470 get_tok_str(v
, NULL
));
4475 /* push a reference to global symbol v */
4476 static void vpush_global_sym(CType
*type
, int v
)
4481 sym
= external_global_sym(v
, type
, 0);
4483 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
4487 void vset(CType
*type
, int r
, int v
)
4492 vsetc(type
, r
, &cval
);
4495 void vseti(int r
, int v
)
4511 void vpushv(SValue
*v
)
4513 if (vtop
>= vstack
+ (VSTACK_SIZE
- 1))
4514 error("memory full");
4524 /* save r to the memory stack, and mark it as being free */
4525 void save_reg(int r
)
4527 int l
, saved
, size
, align
;
4531 /* modify all stack values */
4534 for(p
=vstack
;p
<=vtop
;p
++) {
4535 if ((p
->r
& VT_VALMASK
) == r
||
4536 (p
->r2
& VT_VALMASK
) == r
) {
4537 /* must save value on stack if not already done */
4539 /* NOTE: must reload 'r' because r might be equal to r2 */
4540 r
= p
->r
& VT_VALMASK
;
4541 /* store register in the stack */
4543 if ((p
->r
& VT_LVAL
) ||
4544 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
4546 size
= type_size(type
, &align
);
4547 loc
= (loc
- size
) & -align
;
4548 sv
.type
.t
= type
->t
;
4549 sv
.r
= VT_LOCAL
| VT_LVAL
;
4552 #ifdef TCC_TARGET_I386
4553 /* x86 specific: need to pop fp register ST0 if saved */
4554 if (r
== TREG_ST0
) {
4555 o(0xd9dd); /* fstp %st(1) */
4558 /* special long long case */
4559 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
4566 /* mark that stack entry as being saved on the stack */
4567 if (p
->r
& VT_LVAL
) {
4568 /* also clear the bounded flag because the
4569 relocation address of the function was stored in
4571 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4573 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4581 /* find a register of class 'rc2' with at most one reference on stack.
4582 * If none, call get_reg(rc) */
4583 int get_reg_ex(int rc
, int rc2
)
4588 for(r
=0;r
<NB_REGS
;r
++) {
4589 if (reg_classes
[r
] & rc2
) {
4592 for(p
= vstack
; p
<= vtop
; p
++) {
4593 if ((p
->r
& VT_VALMASK
) == r
||
4594 (p
->r2
& VT_VALMASK
) == r
)
4604 /* find a free register of class 'rc'. If none, save one register */
4610 /* find a free register */
4611 for(r
=0;r
<NB_REGS
;r
++) {
4612 if (reg_classes
[r
] & rc
) {
4613 for(p
=vstack
;p
<=vtop
;p
++) {
4614 if ((p
->r
& VT_VALMASK
) == r
||
4615 (p
->r2
& VT_VALMASK
) == r
)
4623 /* no register left : free the first one on the stack (VERY
4624 IMPORTANT to start from the bottom to ensure that we don't
4625 spill registers used in gen_opi()) */
4626 for(p
=vstack
;p
<=vtop
;p
++) {
4627 r
= p
->r
& VT_VALMASK
;
4628 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4630 /* also look at second register (if long long) */
4631 r
= p
->r2
& VT_VALMASK
;
4632 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4638 /* Should never comes here */
4642 /* save registers up to (vtop - n) stack entry */
4643 void save_regs(int n
)
4648 for(p
= vstack
;p
<= p1
; p
++) {
4649 r
= p
->r
& VT_VALMASK
;
4656 /* move register 's' to 'r', and flush previous value of r to memory
4658 void move_reg(int r
, int s
)
4671 /* get address of vtop (vtop MUST BE an lvalue) */
4674 vtop
->r
&= ~VT_LVAL
;
4675 /* tricky: if saved lvalue, then we can go back to lvalue */
4676 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4677 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4680 #ifdef CONFIG_TCC_BCHECK
4681 /* generate lvalue bound code */
4687 vtop
->r
&= ~VT_MUSTBOUND
;
4688 /* if lvalue, then use checking code before dereferencing */
4689 if (vtop
->r
& VT_LVAL
) {
4690 /* if not VT_BOUNDED value, then make one */
4691 if (!(vtop
->r
& VT_BOUNDED
)) {
4692 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4693 /* must save type because we must set it to int to get pointer */
4695 vtop
->type
.t
= VT_INT
;
4698 gen_bounded_ptr_add();
4699 vtop
->r
|= lval_type
;
4702 /* then check for dereferencing */
4703 gen_bounded_ptr_deref();
4708 /* store vtop a register belonging to class 'rc'. lvalues are
4709 converted to values. Cannot be used if cannot be converted to
4710 register value (such as structures). */
4713 int r
, r2
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4714 unsigned long long ll
;
4716 /* NOTE: get_reg can modify vstack[] */
4717 if (vtop
->type
.t
& VT_BITFIELD
) {
4718 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4719 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4720 /* remove bit field info to avoid loops */
4721 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4722 /* generate shifts */
4723 vpushi(32 - (bit_pos
+ bit_size
));
4725 vpushi(32 - bit_size
);
4726 /* NOTE: transformed to SHR if unsigned */
4730 if (is_float(vtop
->type
.t
) &&
4731 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4734 unsigned long offset
;
4736 /* XXX: unify with initializers handling ? */
4737 /* CPUs usually cannot use float constants, so we store them
4738 generically in data segment */
4739 size
= type_size(&vtop
->type
, &align
);
4740 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
4741 data_section
->data_offset
= offset
;
4742 /* XXX: not portable yet */
4743 ptr
= section_ptr_add(data_section
, size
);
4746 ptr
[i
] = vtop
->c
.tab
[i
];
4747 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
4748 vtop
->r
|= VT_LVAL
| VT_SYM
;
4752 #ifdef CONFIG_TCC_BCHECK
4753 if (vtop
->r
& VT_MUSTBOUND
)
4757 r
= vtop
->r
& VT_VALMASK
;
4758 /* need to reload if:
4760 - lvalue (need to dereference pointer)
4761 - already a register, but not in the right class */
4762 if (r
>= VT_CONST
||
4763 (vtop
->r
& VT_LVAL
) ||
4764 !(reg_classes
[r
] & rc
) ||
4765 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
4766 !(reg_classes
[vtop
->r2
] & rc
))) {
4768 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
4769 /* two register type load : expand to two words
4771 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4774 vtop
->c
.ui
= ll
; /* first word */
4776 vtop
->r
= r
; /* save register value */
4777 vpushi(ll
>> 32); /* second word */
4778 } else if (r
>= VT_CONST
|| /* XXX: test to VT_CONST incorrect ? */
4779 (vtop
->r
& VT_LVAL
)) {
4780 /* We do not want to modifier the long long
4781 pointer here, so the safest (and less
4782 efficient) is to save all the other registers
4783 in the stack. XXX: totally inefficient. */
4785 /* load from memory */
4788 vtop
[-1].r
= r
; /* save register value */
4789 /* increment pointer to get second word */
4790 vtop
->type
.t
= VT_INT
;
4796 /* move registers */
4799 vtop
[-1].r
= r
; /* save register value */
4800 vtop
->r
= vtop
[-1].r2
;
4802 /* allocate second register */
4809 /* write second register */
4811 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
4813 /* lvalue of scalar type : need to use lvalue type
4814 because of possible cast */
4817 /* compute memory access type */
4818 if (vtop
->r
& VT_LVAL_BYTE
)
4820 else if (vtop
->r
& VT_LVAL_SHORT
)
4822 if (vtop
->r
& VT_LVAL_UNSIGNED
)
4826 /* restore wanted type */
4829 /* one register type load */
4834 #ifdef TCC_TARGET_C67
4835 /* uses register pairs for doubles */
4836 if ((vtop
->type
.t
& VT_BTYPE
) == VT_DOUBLE
)
4843 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4844 void gv2(int rc1
, int rc2
)
4848 /* generate more generic register first. But VT_JMP or VT_CMP
4849 values must be generated first in all cases to avoid possible
4851 v
= vtop
[0].r
& VT_VALMASK
;
4852 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
4857 /* test if reload is needed for first register */
4858 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
4868 /* test if reload is needed for first register */
4869 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
4875 /* expand long long on stack in two int registers */
4880 u
= vtop
->type
.t
& VT_UNSIGNED
;
4883 vtop
[0].r
= vtop
[-1].r2
;
4884 vtop
[0].r2
= VT_CONST
;
4885 vtop
[-1].r2
= VT_CONST
;
4886 vtop
[0].type
.t
= VT_INT
| u
;
4887 vtop
[-1].type
.t
= VT_INT
| u
;
4890 #ifdef TCC_TARGET_ARM
4891 /* expand long long on stack */
4892 void lexpand_nr(void)
4896 u
= vtop
->type
.t
& VT_UNSIGNED
;
4898 vtop
->r2
= VT_CONST
;
4899 vtop
->type
.t
= VT_INT
| u
;
4900 v
=vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
);
4901 if (v
== VT_CONST
) {
4902 vtop
[-1].c
.ui
= vtop
->c
.ull
;
4903 vtop
->c
.ui
= vtop
->c
.ull
>> 32;
4905 } else if (v
== (VT_LVAL
|VT_CONST
) || v
== (VT_LVAL
|VT_LOCAL
)) {
4907 vtop
->r
= vtop
[-1].r
;
4908 } else if (v
> VT_CONST
) {
4912 vtop
->r
= vtop
[-1].r2
;
4913 vtop
[-1].r2
= VT_CONST
;
4914 vtop
[-1].type
.t
= VT_INT
| u
;
4918 /* build a long long from two ints */
4921 gv2(RC_INT
, RC_INT
);
4922 vtop
[-1].r2
= vtop
[0].r
;
4923 vtop
[-1].type
.t
= t
;
4927 /* rotate n first stack elements to the bottom
4928 I1 ... In -> I2 ... In I1 [top is right]
4936 for(i
=-n
+1;i
!=0;i
++)
4937 vtop
[i
] = vtop
[i
+1];
4941 /* rotate n first stack elements to the top
4942 I1 ... In -> In I1 ... I(n-1) [top is right]
4950 for(i
= 0;i
< n
- 1; i
++)
4951 vtop
[-i
] = vtop
[-i
- 1];
4955 #ifdef TCC_TARGET_ARM
4956 /* like vrott but in other direction
4957 In ... I1 -> I(n-1) ... I1 In [top is right]
4965 for(i
= n
- 1; i
> 0; i
--)
4966 vtop
[-i
] = vtop
[-i
+ 1];
4971 /* pop stack value */
4975 v
= vtop
->r
& VT_VALMASK
;
4976 #ifdef TCC_TARGET_I386
4977 /* for x86, we need to pop the FP stack */
4978 if (v
== TREG_ST0
&& !nocode_wanted
) {
4979 o(0xd9dd); /* fstp %st(1) */
4982 if (v
== VT_JMP
|| v
== VT_JMPI
) {
4983 /* need to put correct jump if && or || without test */
4989 /* convert stack entry to register and duplicate its value in another
4997 if ((t
& VT_BTYPE
) == VT_LLONG
) {
5004 /* stack: H L L1 H1 */
5012 /* duplicate value */
5023 load(r1
, &sv
); /* move r to r1 */
5025 /* duplicates value */
5030 /* generate CPU independent (unsigned) long long operations */
5031 void gen_opl(int op
)
5033 int t
, a
, b
, op1
, c
, i
;
5040 func
= TOK___divdi3
;
5043 func
= TOK___udivdi3
;
5046 func
= TOK___moddi3
;
5049 func
= TOK___umoddi3
;
5051 /* call generic long long function */
5052 vpush_global_sym(&func_old_type
, func
);
5057 vtop
->r2
= REG_LRET
;
5070 /* stack: L1 H1 L2 H2 */
5075 vtop
[-2] = vtop
[-3];
5078 /* stack: H1 H2 L1 L2 */
5084 /* stack: H1 H2 L1 L2 ML MH */
5087 /* stack: ML MH H1 H2 L1 L2 */
5091 /* stack: ML MH H1 L2 H2 L1 */
5096 /* stack: ML MH M1 M2 */
5099 } else if (op
== '+' || op
== '-') {
5100 /* XXX: add non carry method too (for MIPS or alpha) */
5106 /* stack: H1 H2 (L1 op L2) */
5109 gen_op(op1
+ 1); /* TOK_xxxC2 */
5112 /* stack: H1 H2 (L1 op L2) */
5115 /* stack: (L1 op L2) H1 H2 */
5117 /* stack: (L1 op L2) (H1 op H2) */
5125 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
5126 t
= vtop
[-1].type
.t
;
5130 /* stack: L H shift */
5132 /* constant: simpler */
5133 /* NOTE: all comments are for SHL. the other cases are
5134 done by swaping words */
5145 if (op
!= TOK_SAR
) {
5178 /* XXX: should provide a faster fallback on x86 ? */
5181 func
= TOK___sardi3
;
5184 func
= TOK___shrdi3
;
5187 func
= TOK___shldi3
;
5193 /* compare operations */
5199 /* stack: L1 H1 L2 H2 */
5201 vtop
[-1] = vtop
[-2];
5203 /* stack: L1 L2 H1 H2 */
5206 /* when values are equal, we need to compare low words. since
5207 the jump is inverted, we invert the test too. */
5210 else if (op1
== TOK_GT
)
5212 else if (op1
== TOK_ULT
)
5214 else if (op1
== TOK_UGT
)
5219 if (op1
!= TOK_NE
) {
5223 /* generate non equal test */
5224 /* XXX: NOT PORTABLE yet */
5228 #if defined(TCC_TARGET_I386)
5229 b
= psym(0x850f, 0);
5230 #elif defined(TCC_TARGET_ARM)
5232 o(0x1A000000 | encbranch(ind
, 0, 1));
5233 #elif defined(TCC_TARGET_C67)
5234 error("not implemented");
5236 #error not supported
5240 /* compare low. Always unsigned */
5244 else if (op1
== TOK_LE
)
5246 else if (op1
== TOK_GT
)
5248 else if (op1
== TOK_GE
)
5258 /* handle integer constant optimizations and various machine
5260 void gen_opic(int op
)
5267 /* currently, we cannot do computations with forward symbols */
5268 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5269 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5273 case '+': v1
->c
.i
+= fc
; break;
5274 case '-': v1
->c
.i
-= fc
; break;
5275 case '&': v1
->c
.i
&= fc
; break;
5276 case '^': v1
->c
.i
^= fc
; break;
5277 case '|': v1
->c
.i
|= fc
; break;
5278 case '*': v1
->c
.i
*= fc
; break;
5285 /* if division by zero, generate explicit division */
5288 error("division by zero in constant");
5292 default: v1
->c
.i
/= fc
; break;
5293 case '%': v1
->c
.i
%= fc
; break;
5294 case TOK_UDIV
: v1
->c
.i
= (unsigned)v1
->c
.i
/ fc
; break;
5295 case TOK_UMOD
: v1
->c
.i
= (unsigned)v1
->c
.i
% fc
; break;
5298 case TOK_SHL
: v1
->c
.i
<<= fc
; break;
5299 case TOK_SHR
: v1
->c
.i
= (unsigned)v1
->c
.i
>> fc
; break;
5300 case TOK_SAR
: v1
->c
.i
>>= fc
; break;
5302 case TOK_ULT
: v1
->c
.i
= (unsigned)v1
->c
.i
< (unsigned)fc
; break;
5303 case TOK_UGE
: v1
->c
.i
= (unsigned)v1
->c
.i
>= (unsigned)fc
; break;
5304 case TOK_EQ
: v1
->c
.i
= v1
->c
.i
== fc
; break;
5305 case TOK_NE
: v1
->c
.i
= v1
->c
.i
!= fc
; break;
5306 case TOK_ULE
: v1
->c
.i
= (unsigned)v1
->c
.i
<= (unsigned)fc
; break;
5307 case TOK_UGT
: v1
->c
.i
= (unsigned)v1
->c
.i
> (unsigned)fc
; break;
5308 case TOK_LT
: v1
->c
.i
= v1
->c
.i
< fc
; break;
5309 case TOK_GE
: v1
->c
.i
= v1
->c
.i
>= fc
; break;
5310 case TOK_LE
: v1
->c
.i
= v1
->c
.i
<= fc
; break;
5311 case TOK_GT
: v1
->c
.i
= v1
->c
.i
> fc
; break;
5313 case TOK_LAND
: v1
->c
.i
= v1
->c
.i
&& fc
; break;
5314 case TOK_LOR
: v1
->c
.i
= v1
->c
.i
|| fc
; break;
5320 /* if commutative ops, put c2 as constant */
5321 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
5322 op
== '|' || op
== '*')) {
5327 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
5330 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
5331 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
5337 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
5338 /* try to use shifts instead of muls or divs */
5339 if (fc
> 0 && (fc
& (fc
- 1)) == 0) {
5348 else if (op
== TOK_PDIV
)
5354 } else if (c2
&& (op
== '+' || op
== '-') &&
5355 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
5356 (VT_CONST
| VT_SYM
)) {
5357 /* symbol + constant case */
5364 if (!nocode_wanted
) {
5365 /* call low level op generator */
5374 /* generate a floating point operation with constant propagation */
5375 void gen_opif(int op
)
5383 /* currently, we cannot do computations with forward symbols */
5384 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5385 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5387 if (v1
->type
.t
== VT_FLOAT
) {
5390 } else if (v1
->type
.t
== VT_DOUBLE
) {
5398 /* NOTE: we only do constant propagation if finite number (not
5399 NaN or infinity) (ANSI spec) */
5400 if (!ieee_finite(f1
) || !ieee_finite(f2
))
5404 case '+': f1
+= f2
; break;
5405 case '-': f1
-= f2
; break;
5406 case '*': f1
*= f2
; break;
5410 error("division by zero in constant");
5415 /* XXX: also handles tests ? */
5419 /* XXX: overflow test ? */
5420 if (v1
->type
.t
== VT_FLOAT
) {
5422 } else if (v1
->type
.t
== VT_DOUBLE
) {
5430 if (!nocode_wanted
) {
5438 static int pointed_size(CType
*type
)
5441 return type_size(pointed_type(type
), &align
);
5444 static inline int is_null_pointer(SValue
*p
)
5446 if ((p
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5448 return ((p
->type
.t
& VT_BTYPE
) == VT_INT
&& p
->c
.i
== 0) ||
5449 ((p
->type
.t
& VT_BTYPE
) == VT_LLONG
&& p
->c
.ll
== 0);
5452 static inline int is_integer_btype(int bt
)
5454 return (bt
== VT_BYTE
|| bt
== VT_SHORT
||
5455 bt
== VT_INT
|| bt
== VT_LLONG
);
5458 /* check types for comparison or substraction of pointers */
5459 static void check_comparison_pointer_types(SValue
*p1
, SValue
*p2
, int op
)
5461 CType
*type1
, *type2
, tmp_type1
, tmp_type2
;
5464 /* null pointers are accepted for all comparisons as gcc */
5465 if (is_null_pointer(p1
) || is_null_pointer(p2
))
5469 bt1
= type1
->t
& VT_BTYPE
;
5470 bt2
= type2
->t
& VT_BTYPE
;
5471 /* accept comparison between pointer and integer with a warning */
5472 if ((is_integer_btype(bt1
) || is_integer_btype(bt2
)) && op
!= '-') {
5473 warning("comparison between pointer and integer");
5477 /* both must be pointers or implicit function pointers */
5478 if (bt1
== VT_PTR
) {
5479 type1
= pointed_type(type1
);
5480 } else if (bt1
!= VT_FUNC
)
5481 goto invalid_operands
;
5483 if (bt2
== VT_PTR
) {
5484 type2
= pointed_type(type2
);
5485 } else if (bt2
!= VT_FUNC
) {
5487 error("invalid operands to binary %s", get_tok_str(op
, NULL
));
5489 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
5490 (type2
->t
& VT_BTYPE
) == VT_VOID
)
5494 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5495 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
5496 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
5497 /* gcc-like error if '-' is used */
5499 goto invalid_operands
;
5501 warning("comparison of distinct pointer types lacks a cast");
5505 /* generic gen_op: handles types problems */
5508 int u
, t1
, t2
, bt1
, bt2
, t
;
5511 t1
= vtop
[-1].type
.t
;
5512 t2
= vtop
[0].type
.t
;
5513 bt1
= t1
& VT_BTYPE
;
5514 bt2
= t2
& VT_BTYPE
;
5516 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5517 /* at least one operand is a pointer */
5518 /* relationnal op: must be both pointers */
5519 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5520 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5521 /* pointers are handled are unsigned */
5522 t
= VT_INT
| VT_UNSIGNED
;
5525 /* if both pointers, then it must be the '-' op */
5526 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
5528 error("cannot use pointers here");
5529 check_comparison_pointer_types(vtop
- 1, vtop
, op
);
5530 /* XXX: check that types are compatible */
5531 u
= pointed_size(&vtop
[-1].type
);
5533 /* set to integer type */
5534 vtop
->type
.t
= VT_INT
;
5538 /* exactly one pointer : must be '+' or '-'. */
5539 if (op
!= '-' && op
!= '+')
5540 error("cannot use pointers here");
5541 /* Put pointer as first operand */
5542 if (bt2
== VT_PTR
) {
5546 type1
= vtop
[-1].type
;
5547 /* XXX: cast to int ? (long long case) */
5548 vpushi(pointed_size(&vtop
[-1].type
));
5550 #ifdef CONFIG_TCC_BCHECK
5551 /* if evaluating constant expression, no code should be
5552 generated, so no bound check */
5553 if (do_bounds_check
&& !const_wanted
) {
5554 /* if bounded pointers, we generate a special code to
5561 gen_bounded_ptr_add();
5567 /* put again type if gen_opic() swaped operands */
5570 } else if (is_float(bt1
) || is_float(bt2
)) {
5571 /* compute bigger type and do implicit casts */
5572 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5574 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5579 /* floats can only be used for a few operations */
5580 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
5581 (op
< TOK_ULT
|| op
> TOK_GT
))
5582 error("invalid operands for binary operation");
5584 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5585 /* cast to biggest op */
5587 /* convert to unsigned if it does not fit in a long long */
5588 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5589 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5593 /* integer operations */
5595 /* convert to unsigned if it does not fit in an integer */
5596 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5597 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5600 /* XXX: currently, some unsigned operations are explicit, so
5601 we modify them here */
5602 if (t
& VT_UNSIGNED
) {
5609 else if (op
== TOK_LT
)
5611 else if (op
== TOK_GT
)
5613 else if (op
== TOK_LE
)
5615 else if (op
== TOK_GE
)
5622 /* special case for shifts and long long: we keep the shift as
5624 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
5629 else if ((t
& VT_BTYPE
) == VT_LLONG
)
5633 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
5634 /* relationnal op: the result is an int */
5635 vtop
->type
.t
= VT_INT
;
5642 /* generic itof for unsigned long long case */
5643 void gen_cvt_itof1(int t
)
5645 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
5646 (VT_LLONG
| VT_UNSIGNED
)) {
5649 vpush_global_sym(&func_old_type
, TOK___ulltof
);
5650 else if (t
== VT_DOUBLE
)
5651 vpush_global_sym(&func_old_type
, TOK___ulltod
);
5653 vpush_global_sym(&func_old_type
, TOK___ulltold
);
5663 /* generic ftoi for unsigned long long case */
5664 void gen_cvt_ftoi1(int t
)
5668 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
5669 /* not handled natively */
5670 st
= vtop
->type
.t
& VT_BTYPE
;
5672 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
5673 else if (st
== VT_DOUBLE
)
5674 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
5676 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
5681 vtop
->r2
= REG_LRET
;
5687 /* force char or short cast */
5688 void force_charshort_cast(int t
)
5692 /* XXX: add optimization if lvalue : just change type and offset */
5697 if (t
& VT_UNSIGNED
) {
5698 vpushi((1 << bits
) - 1);
5709 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
5710 static void gen_cast(CType
*type
)
5712 int sbt
, dbt
, sf
, df
, c
;
5714 /* special delayed cast for char/short */
5715 /* XXX: in some cases (multiple cascaded casts), it may still
5717 if (vtop
->r
& VT_MUSTCAST
) {
5718 vtop
->r
&= ~VT_MUSTCAST
;
5719 force_charshort_cast(vtop
->type
.t
);
5722 /* bitfields first get cast to ints */
5723 if (vtop
->type
.t
& VT_BITFIELD
) {
5727 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
5728 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
5730 if (sbt
!= dbt
&& !nocode_wanted
) {
5733 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5735 /* convert from fp to fp */
5737 /* constant case: we can do it now */
5738 /* XXX: in ISOC, cannot do it if error in convert */
5739 if (dbt
== VT_FLOAT
&& sbt
== VT_DOUBLE
)
5740 vtop
->c
.f
= (float)vtop
->c
.d
;
5741 else if (dbt
== VT_FLOAT
&& sbt
== VT_LDOUBLE
)
5742 vtop
->c
.f
= (float)vtop
->c
.ld
;
5743 else if (dbt
== VT_DOUBLE
&& sbt
== VT_FLOAT
)
5744 vtop
->c
.d
= (double)vtop
->c
.f
;
5745 else if (dbt
== VT_DOUBLE
&& sbt
== VT_LDOUBLE
)
5746 vtop
->c
.d
= (double)vtop
->c
.ld
;
5747 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_FLOAT
)
5748 vtop
->c
.ld
= (long double)vtop
->c
.f
;
5749 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_DOUBLE
)
5750 vtop
->c
.ld
= (long double)vtop
->c
.d
;
5752 /* non constant case: generate code */
5756 /* convert int to fp */
5759 case VT_LLONG
| VT_UNSIGNED
:
5761 /* XXX: add const cases for long long */
5763 case VT_INT
| VT_UNSIGNED
:
5765 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.ui
; break;
5766 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.ui
; break;
5767 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.ui
; break;
5772 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.i
; break;
5773 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.i
; break;
5774 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.i
; break;
5780 #if !defined(TCC_TARGET_ARM)
5787 /* convert fp to int */
5788 /* we handle char/short/etc... with generic code */
5789 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
5790 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
5795 case VT_LLONG
| VT_UNSIGNED
:
5797 /* XXX: add const cases for long long */
5799 case VT_INT
| VT_UNSIGNED
:
5801 case VT_FLOAT
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5802 case VT_DOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5803 case VT_LDOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5809 case VT_FLOAT
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5810 case VT_DOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5811 case VT_LDOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5819 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
5820 /* additional cast for char/short/bool... */
5824 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
5825 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
5826 /* scalar to long long */
5828 if (sbt
== (VT_INT
| VT_UNSIGNED
))
5829 vtop
->c
.ll
= vtop
->c
.ui
;
5831 vtop
->c
.ll
= vtop
->c
.i
;
5833 /* machine independent conversion */
5835 /* generate high word */
5836 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
5844 /* patch second register */
5845 vtop
[-1].r2
= vtop
->r
;
5849 } else if (dbt
== VT_BOOL
) {
5850 /* scalar to bool */
5853 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
5854 (dbt
& VT_BTYPE
) == VT_SHORT
) {
5855 force_charshort_cast(dbt
);
5856 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
5858 if (sbt
== VT_LLONG
) {
5859 /* from long long: just take low order word */
5863 /* if lvalue and single word type, nothing to do because
5864 the lvalue already contains the real type size (see
5865 VT_LVAL_xxx constants) */
5871 /* return type size. Put alignment at 'a' */
5872 static int type_size(CType
*type
, int *a
)
5877 bt
= type
->t
& VT_BTYPE
;
5878 if (bt
== VT_STRUCT
) {
5883 } else if (bt
== VT_PTR
) {
5884 if (type
->t
& VT_ARRAY
) {
5886 return type_size(&s
->type
, a
) * s
->c
;
5891 } else if (bt
== VT_LDOUBLE
) {
5893 return LDOUBLE_SIZE
;
5894 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
5895 #ifdef TCC_TARGET_I386
5901 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
5904 } else if (bt
== VT_SHORT
) {
5908 /* char, void, function, _Bool */
5914 /* return the pointed type of t */
5915 static inline CType
*pointed_type(CType
*type
)
5917 return &type
->ref
->type
;
5920 /* modify type so that its it is a pointer to type. */
5921 static void mk_pointer(CType
*type
)
5924 s
= sym_push(SYM_FIELD
, type
, 0, -1);
5925 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
5929 /* compare function types. OLD functions match any new functions */
5930 static int is_compatible_func(CType
*type1
, CType
*type2
)
5936 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5938 /* check func_call */
5941 /* XXX: not complete */
5942 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
5946 while (s1
!= NULL
) {
5949 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5959 /* return true if type1 and type2 are exactly the same (including
5962 - enums are not checked as gcc __builtin_types_compatible_p ()
5964 static int is_compatible_types(CType
*type1
, CType
*type2
)
5968 t1
= type1
->t
& VT_TYPE
;
5969 t2
= type2
->t
& VT_TYPE
;
5970 /* XXX: bitfields ? */
5973 /* test more complicated cases */
5974 bt1
= t1
& VT_BTYPE
;
5975 if (bt1
== VT_PTR
) {
5976 type1
= pointed_type(type1
);
5977 type2
= pointed_type(type2
);
5978 return is_compatible_types(type1
, type2
);
5979 } else if (bt1
== VT_STRUCT
) {
5980 return (type1
->ref
== type2
->ref
);
5981 } else if (bt1
== VT_FUNC
) {
5982 return is_compatible_func(type1
, type2
);
5988 /* print a type. If 'varstr' is not NULL, then the variable is also
5989 printed in the type */
5991 /* XXX: add array and function pointers */
5992 void type_to_str(char *buf
, int buf_size
,
5993 CType
*type
, const char *varstr
)
6000 t
= type
->t
& VT_TYPE
;
6003 if (t
& VT_CONSTANT
)
6004 pstrcat(buf
, buf_size
, "const ");
6005 if (t
& VT_VOLATILE
)
6006 pstrcat(buf
, buf_size
, "volatile ");
6007 if (t
& VT_UNSIGNED
)
6008 pstrcat(buf
, buf_size
, "unsigned ");
6038 tstr
= "long double";
6040 pstrcat(buf
, buf_size
, tstr
);
6044 if (bt
== VT_STRUCT
)
6048 pstrcat(buf
, buf_size
, tstr
);
6049 v
= type
->ref
->v
& ~SYM_STRUCT
;
6050 if (v
>= SYM_FIRST_ANOM
)
6051 pstrcat(buf
, buf_size
, "<anonymous>");
6053 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
6057 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
6058 pstrcat(buf
, buf_size
, "(");
6060 while (sa
!= NULL
) {
6061 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
6062 pstrcat(buf
, buf_size
, buf1
);
6065 pstrcat(buf
, buf_size
, ", ");
6067 pstrcat(buf
, buf_size
, ")");
6071 pstrcpy(buf1
, sizeof(buf1
), "*");
6073 pstrcat(buf1
, sizeof(buf1
), varstr
);
6074 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
6078 pstrcat(buf
, buf_size
, " ");
6079 pstrcat(buf
, buf_size
, varstr
);
6084 /* verify type compatibility to store vtop in 'dt' type, and generate
6086 static void gen_assign_cast(CType
*dt
)
6088 CType
*st
, *type1
, *type2
, tmp_type1
, tmp_type2
;
6089 char buf1
[256], buf2
[256];
6092 st
= &vtop
->type
; /* source type */
6093 dbt
= dt
->t
& VT_BTYPE
;
6094 sbt
= st
->t
& VT_BTYPE
;
6095 if (dt
->t
& VT_CONSTANT
)
6096 warning("assignment of read-only location");
6099 /* special cases for pointers */
6100 /* '0' can also be a pointer */
6101 if (is_null_pointer(vtop
))
6103 /* accept implicit pointer to integer cast with warning */
6104 if (is_integer_btype(sbt
)) {
6105 warning("assignment makes pointer from integer without a cast");
6108 type1
= pointed_type(dt
);
6109 /* a function is implicitely a function pointer */
6110 if (sbt
== VT_FUNC
) {
6111 if ((type1
->t
& VT_BTYPE
) != VT_VOID
&&
6112 !is_compatible_types(pointed_type(dt
), st
))
6119 type2
= pointed_type(st
);
6120 if ((type1
->t
& VT_BTYPE
) == VT_VOID
||
6121 (type2
->t
& VT_BTYPE
) == VT_VOID
) {
6122 /* void * can match anything */
6124 /* exact type match, except for unsigned */
6127 tmp_type1
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6128 tmp_type2
.t
&= ~(VT_UNSIGNED
| VT_CONSTANT
| VT_VOLATILE
);
6129 if (!is_compatible_types(&tmp_type1
, &tmp_type2
))
6132 /* check const and volatile */
6133 if ((!(type1
->t
& VT_CONSTANT
) && (type2
->t
& VT_CONSTANT
)) ||
6134 (!(type1
->t
& VT_VOLATILE
) && (type2
->t
& VT_VOLATILE
)))
6135 warning("assignment discards qualifiers from pointer target type");
6141 if (sbt
== VT_PTR
|| sbt
== VT_FUNC
) {
6142 warning("assignment makes integer from pointer without a cast");
6144 /* XXX: more tests */
6149 tmp_type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6150 tmp_type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6151 if (!is_compatible_types(&tmp_type1
, &tmp_type2
)) {
6153 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
6154 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
6155 error("cannot cast '%s' to '%s'", buf1
, buf2
);
6163 /* store vtop in lvalue pushed on stack */
6166 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
6168 ft
= vtop
[-1].type
.t
;
6169 sbt
= vtop
->type
.t
& VT_BTYPE
;
6170 dbt
= ft
& VT_BTYPE
;
6171 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
6172 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
6173 /* optimize char/short casts */
6174 delayed_cast
= VT_MUSTCAST
;
6175 vtop
->type
.t
= ft
& VT_TYPE
;
6176 /* XXX: factorize */
6177 if (ft
& VT_CONSTANT
)
6178 warning("assignment of read-only location");
6181 if (!(ft
& VT_BITFIELD
))
6182 gen_assign_cast(&vtop
[-1].type
);
6185 if (sbt
== VT_STRUCT
) {
6186 /* if structure, only generate pointer */
6187 /* structure assignment : generate memcpy */
6188 /* XXX: optimize if small size */
6189 if (!nocode_wanted
) {
6190 size
= type_size(&vtop
->type
, &align
);
6192 vpush_global_sym(&func_old_type
, TOK_memcpy
);
6196 vtop
->type
.t
= VT_INT
;
6200 vtop
->type
.t
= VT_INT
;
6212 /* leave source on stack */
6213 } else if (ft
& VT_BITFIELD
) {
6214 /* bitfield store handling */
6215 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
6216 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
6217 /* remove bit field info to avoid loops */
6218 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
6220 /* duplicate destination */
6222 vtop
[-1] = vtop
[-2];
6224 /* mask and shift source */
6225 vpushi((1 << bit_size
) - 1);
6229 /* load destination, mask and or with source */
6231 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
6237 #ifdef CONFIG_TCC_BCHECK
6238 /* bound check case */
6239 if (vtop
[-1].r
& VT_MUSTBOUND
) {
6245 if (!nocode_wanted
) {
6249 r
= gv(rc
); /* generate value */
6250 /* if lvalue was saved on stack, must read it */
6251 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
6253 t
= get_reg(RC_INT
);
6255 sv
.r
= VT_LOCAL
| VT_LVAL
;
6256 sv
.c
.ul
= vtop
[-1].c
.ul
;
6258 vtop
[-1].r
= t
| VT_LVAL
;
6261 /* two word case handling : store second register at word + 4 */
6262 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
6264 /* convert to int to increment easily */
6265 vtop
->type
.t
= VT_INT
;
6271 /* XXX: it works because r2 is spilled last ! */
6272 store(vtop
->r2
, vtop
- 1);
6276 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6277 vtop
->r
|= delayed_cast
;
6281 /* post defines POST/PRE add. c is the token ++ or -- */
6282 void inc(int post
, int c
)
6285 vdup(); /* save lvalue */
6287 gv_dup(); /* duplicate value */
6292 vpushi(c
- TOK_MID
);
6294 vstore(); /* store value */
6296 vpop(); /* if post op, return saved value */
6299 /* Parse GNUC __attribute__ extension. Currently, the following
6300 extensions are recognized:
6301 - aligned(n) : set data/function alignment.
6302 - packed : force data alignment to 1
6303 - section(x) : generate data/code in this section.
6304 - unused : currently ignored, but may be used someday.
6305 - regparm(n) : pass function parameters in registers (i386 only)
6307 static void parse_attribute(AttributeDef
*ad
)
6311 while (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
) {
6315 while (tok
!= ')') {
6316 if (tok
< TOK_IDENT
)
6317 expect("attribute name");
6325 expect("section name");
6326 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
6335 if (n
<= 0 || (n
& (n
- 1)) != 0)
6336 error("alignment must be a positive power of two");
6349 /* currently, no need to handle it because tcc does not
6350 track unused objects */
6354 /* currently, no need to handle it because tcc does not
6355 track unused objects */
6360 ad
->func_call
= FUNC_CDECL
;
6365 ad
->func_call
= FUNC_STDCALL
;
6367 #ifdef TCC_TARGET_I386
6377 ad
->func_call
= FUNC_FASTCALL1
+ n
- 1;
6385 if (tcc_state
->warn_unsupported
)
6386 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
6387 /* skip parameters */
6388 /* XXX: skip parenthesis too */
6391 while (tok
!= ')' && tok
!= -1)
6406 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
6407 static void struct_decl(CType
*type
, int u
)
6409 int a
, v
, size
, align
, maxalign
, c
, offset
;
6410 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
;
6415 a
= tok
; /* save decl type */
6420 /* struct already defined ? return it */
6422 expect("struct/union/enum name");
6426 error("invalid type");
6433 /* we put an undefined size for struct/union */
6434 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, -1);
6435 s
->r
= 0; /* default alignment is zero as gcc */
6436 /* put struct/union/enum name in type */
6444 error("struct/union/enum already defined");
6445 /* cannot be empty */
6447 /* non empty enums are not allowed */
6448 if (a
== TOK_ENUM
) {
6452 expect("identifier");
6458 /* enum symbols have static storage */
6459 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
6460 ss
->type
.t
|= VT_STATIC
;
6465 /* NOTE: we accept a trailing comma */
6475 while (tok
!= '}') {
6476 parse_btype(&btype
, &ad
);
6482 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
);
6483 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
6484 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
| VT_INLINE
)))
6485 error("invalid type for '%s'",
6486 get_tok_str(v
, NULL
));
6490 bit_size
= expr_const();
6491 /* XXX: handle v = 0 case for messages */
6493 error("negative width in bit-field '%s'",
6494 get_tok_str(v
, NULL
));
6495 if (v
&& bit_size
== 0)
6496 error("zero width for bit-field '%s'",
6497 get_tok_str(v
, NULL
));
6499 size
= type_size(&type1
, &align
);
6501 if (align
< ad
.aligned
)
6503 } else if (ad
.packed
) {
6505 } else if (*tcc_state
->pack_stack_ptr
) {
6506 if (align
> *tcc_state
->pack_stack_ptr
)
6507 align
= *tcc_state
->pack_stack_ptr
;
6510 if (bit_size
>= 0) {
6511 bt
= type1
.t
& VT_BTYPE
;
6517 error("bitfields must have scalar type");
6519 if (bit_size
> bsize
) {
6520 error("width of '%s' exceeds its type",
6521 get_tok_str(v
, NULL
));
6522 } else if (bit_size
== bsize
) {
6523 /* no need for bit fields */
6525 } else if (bit_size
== 0) {
6526 /* XXX: what to do if only padding in a
6528 /* zero size: means to pad */
6532 /* we do not have enough room ? */
6533 if ((bit_pos
+ bit_size
) > bsize
)
6536 /* XXX: handle LSB first */
6537 type1
.t
|= VT_BITFIELD
|
6538 (bit_pos
<< VT_STRUCT_SHIFT
) |
6539 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
6540 bit_pos
+= bit_size
;
6546 /* add new memory data only if starting
6548 if (lbit_pos
== 0) {
6549 if (a
== TOK_STRUCT
) {
6550 c
= (c
+ align
- 1) & -align
;
6558 if (align
> maxalign
)
6562 printf("add field %s offset=%d",
6563 get_tok_str(v
, NULL
), offset
);
6564 if (type1
.t
& VT_BITFIELD
) {
6565 printf(" pos=%d size=%d",
6566 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
6567 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
6571 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
6575 if (tok
== ';' || tok
== TOK_EOF
)
6582 /* store size and alignment */
6583 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
6589 /* return 0 if no type declaration. otherwise, return the basic type
6592 static int parse_btype(CType
*type
, AttributeDef
*ad
)
6594 int t
, u
, type_found
, typespec_found
;
6598 memset(ad
, 0, sizeof(AttributeDef
));
6605 /* currently, we really ignore extension */
6615 if ((t
& VT_BTYPE
) != 0)
6616 error("too many basic types");
6632 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
6633 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6634 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
6635 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
6649 if ((t
& VT_BTYPE
) == VT_LONG
) {
6650 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
6657 struct_decl(&type1
, VT_ENUM
);
6660 type
->ref
= type1
.ref
;
6664 struct_decl(&type1
, VT_STRUCT
);
6667 /* type modifiers */
6720 /* GNUC attribute */
6721 case TOK_ATTRIBUTE1
:
6722 case TOK_ATTRIBUTE2
:
6723 parse_attribute(ad
);
6730 parse_expr_type(&type1
);
6736 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
6738 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
6739 type
->ref
= s
->type
.ref
;
6746 if ((t
& (VT_SIGNED
|VT_UNSIGNED
)) == (VT_SIGNED
|VT_UNSIGNED
))
6747 error("signed and unsigned modifier");
6748 if (tcc_state
->char_is_unsigned
) {
6749 if ((t
& (VT_SIGNED
|VT_UNSIGNED
|VT_BTYPE
)) == VT_BYTE
)
6754 /* long is never used as type */
6755 if ((t
& VT_BTYPE
) == VT_LONG
)
6756 t
= (t
& ~VT_BTYPE
) | VT_INT
;
6761 /* convert a function parameter type (array to pointer and function to
6762 function pointer) */
6763 static inline void convert_parameter_type(CType
*pt
)
6765 /* remove const and volatile qualifiers (XXX: const could be used
6766 to indicate a const function parameter */
6767 pt
->t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
6768 /* array must be transformed to pointer according to ANSI C */
6770 if ((pt
->t
& VT_BTYPE
) == VT_FUNC
) {
6775 static void post_type(CType
*type
, AttributeDef
*ad
)
6778 Sym
**plast
, *s
, *first
;
6783 /* function declaration */
6788 while (tok
!= ')') {
6789 /* read param name and compute offset */
6790 if (l
!= FUNC_OLD
) {
6791 if (!parse_btype(&pt
, &ad1
)) {
6793 error("invalid type");
6800 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
6802 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
6803 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
6804 error("parameter declared as void");
6811 convert_parameter_type(&pt
);
6812 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
6817 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
6824 /* if no parameters, then old type prototype */
6828 t1
= type
->t
& VT_STORAGE
;
6829 /* NOTE: const is ignored in returned type as it has a special
6830 meaning in gcc / C++ */
6831 type
->t
&= ~(VT_STORAGE
| VT_CONSTANT
);
6832 post_type(type
, ad
);
6833 /* we push a anonymous symbol which will contain the function prototype */
6834 s
= sym_push(SYM_FIELD
, type
, ad
->func_call
, l
);
6836 type
->t
= t1
| VT_FUNC
;
6838 } else if (tok
== '[') {
6839 /* array definition */
6845 error("invalid array size");
6848 /* parse next post type */
6849 t1
= type
->t
& VT_STORAGE
;
6850 type
->t
&= ~VT_STORAGE
;
6851 post_type(type
, ad
);
6853 /* we push a anonymous symbol which will contain the array
6855 s
= sym_push(SYM_FIELD
, type
, 0, n
);
6856 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
6861 /* Parse a type declaration (except basic type), and return the type
6862 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6863 expected. 'type' should contain the basic type. 'ad' is the
6864 attribute definition of the basic type. It can be modified by
6867 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
6870 CType type1
, *type2
;
6873 while (tok
== '*') {
6881 qualifiers
|= VT_CONSTANT
;
6886 qualifiers
|= VT_VOLATILE
;
6894 type
->t
|= qualifiers
;
6897 /* XXX: clarify attribute handling */
6898 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6899 parse_attribute(ad
);
6901 /* recursive type */
6902 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6903 type1
.t
= 0; /* XXX: same as int */
6906 /* XXX: this is not correct to modify 'ad' at this point, but
6907 the syntax is not clear */
6908 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6909 parse_attribute(ad
);
6910 type_decl(&type1
, ad
, v
, td
);
6913 /* type identifier */
6914 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
6918 if (!(td
& TYPE_ABSTRACT
))
6919 expect("identifier");
6923 post_type(type
, ad
);
6924 if (tok
== TOK_ATTRIBUTE1
|| tok
== TOK_ATTRIBUTE2
)
6925 parse_attribute(ad
);
6928 /* append type at the end of type1 */
6941 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6942 static int lvalue_type(int t
)
6947 if (bt
== VT_BYTE
|| bt
== VT_BOOL
)
6949 else if (bt
== VT_SHORT
)
6953 if (t
& VT_UNSIGNED
)
6954 r
|= VT_LVAL_UNSIGNED
;
6958 /* indirection with full error checking and bound check */
6959 static void indir(void)
6961 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6963 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
6965 vtop
->type
= *pointed_type(&vtop
->type
);
6966 /* an array is never an lvalue */
6967 if (!(vtop
->type
.t
& VT_ARRAY
)) {
6968 vtop
->r
|= lvalue_type(vtop
->type
.t
);
6969 /* if bound checking, the referenced pointer must be checked */
6970 if (do_bounds_check
)
6971 vtop
->r
|= VT_MUSTBOUND
;
6975 /* pass a parameter to a function and do type checking and casting */
6976 static void gfunc_param_typed(Sym
*func
, Sym
*arg
)
6981 func_type
= func
->c
;
6982 if (func_type
== FUNC_OLD
||
6983 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
6984 /* default casting : only need to convert float to double */
6985 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
6989 } else if (arg
== NULL
) {
6990 error("too many arguments to function");
6993 type
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
6994 gen_assign_cast(&type
);
6998 /* parse an expression of the form '(type)' or '(expr)' and return its
7000 static void parse_expr_type(CType
*type
)
7006 if (parse_btype(type
, &ad
)) {
7007 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7014 static void parse_type(CType
*type
)
7019 if (!parse_btype(type
, &ad
)) {
7022 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
7025 static void vpush_tokc(int t
)
7029 vsetc(&type
, VT_CONST
, &tokc
);
7032 static void unary(void)
7034 int n
, t
, align
, size
, r
;
7039 /* XXX: GCC 2.95.3 does not generate a table although it should be
7053 vpush_tokc(VT_INT
| VT_UNSIGNED
);
7057 vpush_tokc(VT_LLONG
);
7061 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
7065 vpush_tokc(VT_FLOAT
);
7069 vpush_tokc(VT_DOUBLE
);
7073 vpush_tokc(VT_LDOUBLE
);
7076 case TOK___FUNCTION__
:
7078 goto tok_identifier
;
7084 /* special function name identifier */
7085 len
= strlen(funcname
) + 1;
7086 /* generate char[len] type */
7091 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
7092 ptr
= section_ptr_add(data_section
, len
);
7093 memcpy(ptr
, funcname
, len
);
7101 /* string parsing */
7104 if (tcc_state
->warn_write_strings
)
7109 memset(&ad
, 0, sizeof(AttributeDef
));
7110 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
7115 if (parse_btype(&type
, &ad
)) {
7116 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
7118 /* check ISOC99 compound literal */
7120 /* data is allocated locally by default */
7125 /* all except arrays are lvalues */
7126 if (!(type
.t
& VT_ARRAY
))
7127 r
|= lvalue_type(type
.t
);
7128 memset(&ad
, 0, sizeof(AttributeDef
));
7129 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
7134 } else if (tok
== '{') {
7135 /* save all registers */
7137 /* statement expression : we do not accept break/continue
7138 inside as GCC does */
7139 block(NULL
, NULL
, NULL
, NULL
, 0, 1);
7154 /* functions names must be treated as function pointers,
7155 except for unary '&' and sizeof. Since we consider that
7156 functions are not lvalues, we only have to handle it
7157 there and in function calls. */
7158 /* arrays can also be used although they are not lvalues */
7159 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
7160 !(vtop
->type
.t
& VT_ARRAY
))
7162 mk_pointer(&vtop
->type
);
7168 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
)
7169 vtop
->c
.i
= !vtop
->c
.i
;
7170 else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
7171 vtop
->c
.i
= vtop
->c
.i
^ 1;
7173 vseti(VT_JMP
, gtst(1, 0));
7183 /* in order to force cast, we add zero */
7185 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
7186 error("pointer not accepted for unary plus");
7196 parse_expr_type(&type
);
7200 size
= type_size(&type
, &align
);
7201 if (t
== TOK_SIZEOF
) {
7203 error("sizeof applied to an incomplete type");
7210 case TOK_builtin_types_compatible_p
:
7219 type1
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7220 type2
.t
&= ~(VT_CONSTANT
| VT_VOLATILE
);
7221 vpushi(is_compatible_types(&type1
, &type2
));
7224 case TOK_builtin_constant_p
:
7226 int saved_nocode_wanted
, res
;
7229 saved_nocode_wanted
= nocode_wanted
;
7232 res
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
7234 nocode_wanted
= saved_nocode_wanted
;
7254 goto tok_identifier
;
7256 /* allow to take the address of a label */
7257 if (tok
< TOK_UIDENT
)
7258 expect("label identifier");
7259 s
= label_find(tok
);
7261 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
7263 if (s
->r
== LABEL_DECLARED
)
7264 s
->r
= LABEL_FORWARD
;
7267 s
->type
.t
= VT_VOID
;
7268 mk_pointer(&s
->type
);
7269 s
->type
.t
|= VT_STATIC
;
7271 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
7280 expect("identifier");
7284 error("'%s' undeclared", get_tok_str(t
, NULL
));
7285 /* for simple function calls, we tolerate undeclared
7286 external reference to int() function */
7287 if (tcc_state
->warn_implicit_function_declaration
)
7288 warning("implicit declaration of function '%s'",
7289 get_tok_str(t
, NULL
));
7290 s
= external_global_sym(t
, &func_old_type
, 0);
7292 if ((s
->type
.t
& (VT_STATIC
| VT_INLINE
| VT_BTYPE
)) ==
7293 (VT_STATIC
| VT_INLINE
| VT_FUNC
)) {
7294 /* if referencing an inline function, then we generate a
7295 symbol to it if not already done. It will have the
7296 effect to generate code for it at the end of the
7297 compilation unit. Inline function as always
7298 generated in the text section. */
7300 put_extern_sym(s
, text_section
, 0, 0);
7301 r
= VT_SYM
| VT_CONST
;
7305 vset(&s
->type
, r
, s
->c
);
7306 /* if forward reference, we must point to s */
7307 if (vtop
->r
& VT_SYM
) {
7314 /* post operations */
7316 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
7319 } else if (tok
== '.' || tok
== TOK_ARROW
) {
7321 if (tok
== TOK_ARROW
)
7326 /* expect pointer on structure */
7327 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
7328 expect("struct or union");
7332 while ((s
= s
->next
) != NULL
) {
7337 error("field not found");
7338 /* add field offset to pointer */
7339 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
7342 /* change type to field type, and set to lvalue */
7343 vtop
->type
= s
->type
;
7344 /* an array is never an lvalue */
7345 if (!(vtop
->type
.t
& VT_ARRAY
)) {
7346 vtop
->r
|= lvalue_type(vtop
->type
.t
);
7347 /* if bound checking, the referenced pointer must be checked */
7348 if (do_bounds_check
)
7349 vtop
->r
|= VT_MUSTBOUND
;
7352 } else if (tok
== '[') {
7358 } else if (tok
== '(') {
7364 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
7365 /* pointer test (no array accepted) */
7366 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
7367 vtop
->type
= *pointed_type(&vtop
->type
);
7368 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
7372 expect("function pointer");
7375 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
7377 /* get return type */
7380 sa
= s
->next
; /* first parameter */
7382 /* compute first implicit argument if a structure is returned */
7383 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
7384 /* get some space for the returned structure */
7385 size
= type_size(&s
->type
, &align
);
7386 loc
= (loc
- size
) & -align
;
7388 ret
.r
= VT_LOCAL
| VT_LVAL
;
7389 /* pass it as 'int' to avoid structure arg passing
7391 vseti(VT_LOCAL
, loc
);
7397 /* return in register */
7398 if (is_float(ret
.type
.t
)) {
7401 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
7410 gfunc_param_typed(s
, sa
);
7420 error("too few arguments to function");
7422 if (!nocode_wanted
) {
7423 gfunc_call(nb_args
);
7425 vtop
-= (nb_args
+ 1);
7428 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
7436 static void uneq(void)
7442 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
7443 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
7444 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
7459 static void expr_prod(void)
7464 while (tok
== '*' || tok
== '/' || tok
== '%') {
7472 static void expr_sum(void)
7477 while (tok
== '+' || tok
== '-') {
7485 static void expr_shift(void)
7490 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
7498 static void expr_cmp(void)
7503 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
7504 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
7512 static void expr_cmpeq(void)
7517 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
7525 static void expr_and(void)
7528 while (tok
== '&') {
7535 static void expr_xor(void)
7538 while (tok
== '^') {
7545 static void expr_or(void)
7548 while (tok
== '|') {
7555 /* XXX: fix this mess */
7556 static void expr_land_const(void)
7559 while (tok
== TOK_LAND
) {
7566 /* XXX: fix this mess */
7567 static void expr_lor_const(void)
7570 while (tok
== TOK_LOR
) {
7577 /* only used if non constant */
7578 static void expr_land(void)
7583 if (tok
== TOK_LAND
) {
7587 if (tok
!= TOK_LAND
) {
7597 static void expr_lor(void)
7602 if (tok
== TOK_LOR
) {
7606 if (tok
!= TOK_LOR
) {
7616 /* XXX: better constant handling */
7617 static void expr_eq(void)
7619 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
7621 CType type
, type1
, type2
;
7630 if (tok
== ':' && gnu_ext
) {
7646 if (vtop
!= vstack
) {
7647 /* needed to avoid having different registers saved in
7649 if (is_float(vtop
->type
.t
))
7656 if (tok
== ':' && gnu_ext
) {
7664 sv
= *vtop
; /* save value to handle it later */
7665 vtop
--; /* no vpop so that FP stack is not flushed */
7673 bt1
= t1
& VT_BTYPE
;
7675 bt2
= t2
& VT_BTYPE
;
7676 /* cast operands to correct type according to ISOC rules */
7677 if (is_float(bt1
) || is_float(bt2
)) {
7678 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
7679 type
.t
= VT_LDOUBLE
;
7680 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
7685 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
7686 /* cast to biggest op */
7688 /* convert to unsigned if it does not fit in a long long */
7689 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
7690 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
7691 type
.t
|= VT_UNSIGNED
;
7692 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
7693 /* XXX: test pointer compatibility */
7695 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
7696 /* XXX: test structure compatibility */
7698 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
7699 /* NOTE: as an extension, we accept void on only one side */
7702 /* integer operations */
7704 /* convert to unsigned if it does not fit in an integer */
7705 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
7706 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
7707 type
.t
|= VT_UNSIGNED
;
7710 /* now we convert second operand */
7713 if (is_float(type
.t
)) {
7715 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
7716 /* for long longs, we use fixed registers to avoid having
7717 to handle a complicated move */
7722 /* this is horrible, but we must also convert first
7726 /* put again first value and cast it */
7737 static void gexpr(void)
7748 /* parse an expression and return its type without any side effect. */
7749 static void expr_type(CType
*type
)
7751 int saved_nocode_wanted
;
7753 saved_nocode_wanted
= nocode_wanted
;
7758 nocode_wanted
= saved_nocode_wanted
;
7761 /* parse a unary expression and return its type without any side
7763 static void unary_type(CType
*type
)
7775 /* parse a constant expression and return value in vtop. */
7776 static void expr_const1(void)
7785 /* parse an integer constant and return its value. */
7786 static int expr_const(void)
7790 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
7791 expect("constant expression");
7797 /* return the label token if current token is a label, otherwise
7799 static int is_label(void)
7803 /* fast test first */
7804 if (tok
< TOK_UIDENT
)
7806 /* no need to save tokc because tok is an identifier */
7813 unget_tok(last_tok
);
7818 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
,
7819 int case_reg
, int is_expr
)
7824 /* generate line number info */
7826 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
7827 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
7829 last_line_num
= file
->line_num
;
7833 /* default return value is (void) */
7835 vtop
->type
.t
= VT_VOID
;
7838 if (tok
== TOK_IF
) {
7845 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
7847 if (c
== TOK_ELSE
) {
7851 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, 0);
7852 gsym(d
); /* patch else jmp */
7855 } else if (tok
== TOK_WHILE
) {
7863 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7867 } else if (tok
== '{') {
7871 /* record local declaration stack position */
7873 llabel
= local_label_stack
;
7874 /* handle local labels declarations */
7875 if (tok
== TOK_LABEL
) {
7878 if (tok
< TOK_UIDENT
)
7879 expect("label identifier");
7880 label_push(&local_label_stack
, tok
, LABEL_DECLARED
);
7890 while (tok
!= '}') {
7895 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
7898 /* pop locally defined labels */
7899 label_pop(&local_label_stack
, llabel
);
7900 /* pop locally defined symbols */
7901 sym_pop(&local_stack
, s
);
7903 } else if (tok
== TOK_RETURN
) {
7907 gen_assign_cast(&func_vt
);
7908 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
7910 /* if returning structure, must copy it to implicit
7911 first pointer arg location */
7914 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
7917 /* copy structure value to pointer */
7919 } else if (is_float(func_vt
.t
)) {
7924 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
7927 rsym
= gjmp(rsym
); /* jmp */
7928 } else if (tok
== TOK_BREAK
) {
7931 error("cannot break");
7932 *bsym
= gjmp(*bsym
);
7935 } else if (tok
== TOK_CONTINUE
) {
7938 error("cannot continue");
7939 *csym
= gjmp(*csym
);
7942 } else if (tok
== TOK_FOR
) {
7969 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7974 if (tok
== TOK_DO
) {
7979 block(&a
, &b
, case_sym
, def_sym
, case_reg
, 0);
7990 if (tok
== TOK_SWITCH
) {
7994 /* XXX: other types than integer */
7995 case_reg
= gv(RC_INT
);
7999 b
= gjmp(0); /* jump to first case */
8001 block(&a
, csym
, &b
, &c
, case_reg
, 0);
8002 /* if no default, jmp after switch */
8010 if (tok
== TOK_CASE
) {
8017 if (gnu_ext
&& tok
== TOK_DOTS
) {
8021 warning("empty case range");
8023 /* since a case is like a label, we must skip it with a jmp */
8030 *case_sym
= gtst(1, 0);
8033 *case_sym
= gtst(1, 0);
8037 *case_sym
= gtst(1, *case_sym
);
8042 goto block_after_label
;
8044 if (tok
== TOK_DEFAULT
) {
8050 error("too many 'default'");
8053 goto block_after_label
;
8055 if (tok
== TOK_GOTO
) {
8057 if (tok
== '*' && gnu_ext
) {
8061 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
8064 } else if (tok
>= TOK_UIDENT
) {
8065 s
= label_find(tok
);
8066 /* put forward definition if needed */
8068 s
= label_push(&global_label_stack
, tok
, LABEL_FORWARD
);
8070 if (s
->r
== LABEL_DECLARED
)
8071 s
->r
= LABEL_FORWARD
;
8073 /* label already defined */
8074 if (s
->r
& LABEL_FORWARD
)
8075 s
->next
= (void *)gjmp((long)s
->next
);
8077 gjmp_addr((long)s
->next
);
8080 expect("label identifier");
8083 } else if (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
) {
8091 if (s
->r
== LABEL_DEFINED
)
8092 error("duplicate label '%s'", get_tok_str(s
->v
, NULL
));
8093 gsym((long)s
->next
);
8094 s
->r
= LABEL_DEFINED
;
8096 s
= label_push(&global_label_stack
, b
, LABEL_DEFINED
);
8098 s
->next
= (void *)ind
;
8099 /* we accept this, but it is a mistake */
8102 warning("deprecated use of label at end of compound statement");
8106 block(bsym
, csym
, case_sym
, def_sym
, case_reg
, is_expr
);
8109 /* expression case */
8124 /* t is the array or struct type. c is the array or struct
8125 address. cur_index/cur_field is the pointer to the current
8126 value. 'size_only' is true if only size info is needed (only used
8128 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
8129 int *cur_index
, Sym
**cur_field
,
8133 int notfirst
, index
, index_last
, align
, l
, nb_elems
, elem_size
;
8139 if (gnu_ext
&& (l
= is_label()) != 0)
8141 while (tok
== '[' || tok
== '.') {
8143 if (!(type
->t
& VT_ARRAY
))
8144 expect("array type");
8147 index
= expr_const();
8148 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
8149 expect("invalid index");
8150 if (tok
== TOK_DOTS
&& gnu_ext
) {
8152 index_last
= expr_const();
8153 if (index_last
< 0 ||
8154 (s
->c
>= 0 && index_last
>= s
->c
) ||
8156 expect("invalid index");
8162 *cur_index
= index_last
;
8163 type
= pointed_type(type
);
8164 elem_size
= type_size(type
, &align
);
8165 c
+= index
* elem_size
;
8166 /* NOTE: we only support ranges for last designator */
8167 nb_elems
= index_last
- index
+ 1;
8168 if (nb_elems
!= 1) {
8177 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
8178 expect("struct/union type");
8191 /* XXX: fix this mess by using explicit storage field */
8193 type1
.t
|= (type
->t
& ~VT_TYPE
);
8207 if (type
->t
& VT_ARRAY
) {
8209 type
= pointed_type(type
);
8210 c
+= index
* type_size(type
, &align
);
8214 error("too many field init");
8215 /* XXX: fix this mess by using explicit storage field */
8217 type1
.t
|= (type
->t
& ~VT_TYPE
);
8222 decl_initializer(type
, sec
, c
, 0, size_only
);
8224 /* XXX: make it more general */
8225 if (!size_only
&& nb_elems
> 1) {
8226 unsigned long c_end
;
8231 error("range init not supported yet for dynamic storage");
8232 c_end
= c
+ nb_elems
* elem_size
;
8233 if (c_end
> sec
->data_allocated
)
8234 section_realloc(sec
, c_end
);
8235 src
= sec
->data
+ c
;
8237 for(i
= 1; i
< nb_elems
; i
++) {
8239 memcpy(dst
, src
, elem_size
);
8245 #define EXPR_CONST 1
8248 /* store a value or an expression directly in global data or in local array */
8249 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
8250 int v
, int expr_type
)
8252 int saved_global_expr
, bt
, bit_pos
, bit_size
;
8254 unsigned long long bit_mask
;
8262 /* compound literals must be allocated globally in this case */
8263 saved_global_expr
= global_expr
;
8266 global_expr
= saved_global_expr
;
8267 /* NOTE: symbols are accepted */
8268 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
8269 error("initializer element is not constant");
8277 dtype
.t
&= ~VT_CONSTANT
; /* need to do that to avoid false warning */
8280 /* XXX: not portable */
8281 /* XXX: generate error if incorrect relocation */
8282 gen_assign_cast(&dtype
);
8283 bt
= type
->t
& VT_BTYPE
;
8284 ptr
= sec
->data
+ c
;
8285 /* XXX: make code faster ? */
8286 if (!(type
->t
& VT_BITFIELD
)) {
8291 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
8292 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
8293 bit_mask
= (1LL << bit_size
) - 1;
8295 if ((vtop
->r
& VT_SYM
) &&
8301 (bt
== VT_INT
&& bit_size
!= 32)))
8302 error("initializer element is not computable at load time");
8305 *(char *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8308 *(short *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8311 *(double *)ptr
= vtop
->c
.d
;
8314 *(long double *)ptr
= vtop
->c
.ld
;
8317 *(long long *)ptr
|= (vtop
->c
.ll
& bit_mask
) << bit_pos
;
8320 if (vtop
->r
& VT_SYM
) {
8321 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
8323 *(int *)ptr
|= (vtop
->c
.i
& bit_mask
) << bit_pos
;
8328 vset(&dtype
, VT_LOCAL
, c
);
8335 /* put zeros for variable based init */
8336 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
8339 /* nothing to do because globals are already set to zero */
8341 vpush_global_sym(&func_old_type
, TOK_memset
);
8349 /* 't' contains the type and storage info. 'c' is the offset of the
8350 object in section 'sec'. If 'sec' is NULL, it means stack based
8351 allocation. 'first' is true if array '{' must be read (multi
8352 dimension implicit array init handling). 'size_only' is true if
8353 size only evaluation is wanted (only for arrays). */
8354 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
8355 int first
, int size_only
)
8357 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
8358 int size1
, align1
, expr_type
;
8362 if (type
->t
& VT_ARRAY
) {
8366 t1
= pointed_type(type
);
8367 size1
= type_size(t1
, &align1
);
8370 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
8376 /* only parse strings here if correct type (otherwise: handle
8377 them as ((w)char *) expressions */
8378 if ((tok
== TOK_LSTR
&&
8379 (t1
->t
& VT_BTYPE
) == VT_INT
) ||
8381 (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
8382 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8387 /* compute maximum number of chars wanted */
8389 cstr_len
= cstr
->size
;
8391 cstr_len
= cstr
->size
/ sizeof(int);
8394 if (n
>= 0 && nb
> (n
- array_length
))
8395 nb
= n
- array_length
;
8398 warning("initializer-string for array is too long");
8399 /* in order to go faster for common case (char
8400 string in global variable, we handle it
8402 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
8403 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
8407 ch
= ((unsigned char *)cstr
->data
)[i
];
8409 ch
= ((int *)cstr
->data
)[i
];
8410 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
8418 /* only add trailing zero if enough storage (no
8419 warning in this case since it is standard) */
8420 if (n
< 0 || array_length
< n
) {
8422 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
8428 while (tok
!= '}') {
8429 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
8430 if (n
>= 0 && index
>= n
)
8431 error("index too large");
8432 /* must put zero in holes (note that doing it that way
8433 ensures that it even works with designators) */
8434 if (!size_only
&& array_length
< index
) {
8435 init_putz(t1
, sec
, c
+ array_length
* size1
,
8436 (index
- array_length
) * size1
);
8439 if (index
> array_length
)
8440 array_length
= index
;
8441 /* special test for multi dimensional arrays (may not
8442 be strictly correct if designators are used at the
8444 if (index
>= n
&& no_oblock
)
8453 /* put zeros at the end */
8454 if (!size_only
&& n
>= 0 && array_length
< n
) {
8455 init_putz(t1
, sec
, c
+ array_length
* size1
,
8456 (n
- array_length
) * size1
);
8458 /* patch type size if needed */
8460 s
->c
= array_length
;
8461 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&&
8462 (sec
|| !first
|| tok
== '{')) {
8465 /* NOTE: the previous test is a specific case for automatic
8466 struct/union init */
8467 /* XXX: union needs only one init */
8469 /* XXX: this test is incorrect for local initializers
8470 beginning with ( without {. It would be much more difficult
8471 to do it correctly (ideally, the expression parser should
8472 be used in all cases) */
8478 while (tok
== '(') {
8482 if (!parse_btype(&type1
, &ad1
))
8484 type_decl(&type1
, &ad1
, &n
, TYPE_ABSTRACT
);
8486 if (!is_assignable_types(type
, &type1
))
8487 error("invalid type for cast");
8492 if (first
|| tok
== '{') {
8501 while (tok
!= '}') {
8502 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
8504 if (!size_only
&& array_length
< index
) {
8505 init_putz(type
, sec
, c
+ array_length
,
8506 index
- array_length
);
8508 index
= index
+ type_size(&f
->type
, &align1
);
8509 if (index
> array_length
)
8510 array_length
= index
;
8512 if (no_oblock
&& f
== NULL
)
8518 /* put zeros at the end */
8519 if (!size_only
&& array_length
< n
) {
8520 init_putz(type
, sec
, c
+ array_length
,
8529 } else if (tok
== '{') {
8531 decl_initializer(type
, sec
, c
, first
, size_only
);
8533 } else if (size_only
) {
8534 /* just skip expression */
8536 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
8540 else if (tok
== ')')
8545 /* currently, we always use constant expression for globals
8546 (may change for scripting case) */
8547 expr_type
= EXPR_CONST
;
8549 expr_type
= EXPR_ANY
;
8550 init_putv(type
, sec
, c
, 0, expr_type
);
8554 /* parse an initializer for type 't' if 'has_init' is non zero, and
8555 allocate space in local or global data space ('r' is either
8556 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
8557 variable 'v' of scope 'scope' is declared before initializers are
8558 parsed. If 'v' is zero, then a reference to the new object is put
8559 in the value stack. If 'has_init' is 2, a special parsing is done
8560 to handle string constants. */
8561 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
8562 int has_init
, int v
, int scope
)
8564 int size
, align
, addr
, data_offset
;
8566 ParseState saved_parse_state
;
8567 TokenString init_str
;
8570 size
= type_size(type
, &align
);
8571 /* If unknown size, we must evaluate it before
8572 evaluating initializers because
8573 initializers can generate global data too
8574 (e.g. string pointers or ISOC99 compound
8575 literals). It also simplifies local
8576 initializers handling */
8577 tok_str_new(&init_str
);
8580 error("unknown type size");
8581 /* get all init string */
8582 if (has_init
== 2) {
8583 /* only get strings */
8584 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
8585 tok_str_add_tok(&init_str
);
8590 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
8592 error("unexpected end of file in initializer");
8593 tok_str_add_tok(&init_str
);
8596 else if (tok
== '}') {
8604 tok_str_add(&init_str
, -1);
8605 tok_str_add(&init_str
, 0);
8608 save_parse_state(&saved_parse_state
);
8610 macro_ptr
= init_str
.str
;
8612 decl_initializer(type
, NULL
, 0, 1, 1);
8613 /* prepare second initializer parsing */
8614 macro_ptr
= init_str
.str
;
8617 /* if still unknown size, error */
8618 size
= type_size(type
, &align
);
8620 error("unknown type size");
8622 /* take into account specified alignment if bigger */
8624 if (ad
->aligned
> align
)
8625 align
= ad
->aligned
;
8626 } else if (ad
->packed
) {
8629 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
8631 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
8633 loc
= (loc
- size
) & -align
;
8635 /* handles bounds */
8636 /* XXX: currently, since we do only one pass, we cannot track
8637 '&' operators, so we add only arrays */
8638 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
8639 unsigned long *bounds_ptr
;
8640 /* add padding between regions */
8642 /* then add local bound info */
8643 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
8644 bounds_ptr
[0] = addr
;
8645 bounds_ptr
[1] = size
;
8648 /* local variable */
8649 sym_push(v
, type
, r
, addr
);
8651 /* push local reference */
8652 vset(type
, r
, addr
);
8658 if (v
&& scope
== VT_CONST
) {
8659 /* see if the symbol was already defined */
8662 if (!is_compatible_types(&sym
->type
, type
))
8663 error("incompatible types for redefinition of '%s'",
8664 get_tok_str(v
, NULL
));
8665 if (sym
->type
.t
& VT_EXTERN
) {
8666 /* if the variable is extern, it was not allocated */
8667 sym
->type
.t
&= ~VT_EXTERN
;
8668 /* set array size if it was ommited in extern
8670 if ((sym
->type
.t
& VT_ARRAY
) &&
8671 sym
->type
.ref
->c
< 0 &&
8673 sym
->type
.ref
->c
= type
->ref
->c
;
8675 /* we accept several definitions of the same
8676 global variable. this is tricky, because we
8677 must play with the SHN_COMMON type of the symbol */
8678 /* XXX: should check if the variable was already
8679 initialized. It is incorrect to initialized it
8681 /* no init data, we won't add more to the symbol */
8688 /* allocate symbol in corresponding section */
8693 else if (tcc_state
->nocommon
)
8697 data_offset
= sec
->data_offset
;
8698 data_offset
= (data_offset
+ align
- 1) & -align
;
8700 /* very important to increment global pointer at this time
8701 because initializers themselves can create new initializers */
8702 data_offset
+= size
;
8703 /* add padding if bound check */
8704 if (do_bounds_check
)
8706 sec
->data_offset
= data_offset
;
8707 /* allocate section space to put the data */
8708 if (sec
->sh_type
!= SHT_NOBITS
&&
8709 data_offset
> sec
->data_allocated
)
8710 section_realloc(sec
, data_offset
);
8711 /* align section if needed */
8712 if (align
> sec
->sh_addralign
)
8713 sec
->sh_addralign
= align
;
8715 addr
= 0; /* avoid warning */
8719 if (scope
== VT_CONST
) {
8724 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
8726 /* update symbol definition */
8728 put_extern_sym(sym
, sec
, addr
, size
);
8731 /* put a common area */
8732 put_extern_sym(sym
, NULL
, align
, size
);
8733 /* XXX: find a nicer way */
8734 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
8735 esym
->st_shndx
= SHN_COMMON
;
8740 /* push global reference */
8741 sym
= get_sym_ref(type
, sec
, addr
, size
);
8743 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
8747 /* handles bounds now because the symbol must be defined
8748 before for the relocation */
8749 if (do_bounds_check
) {
8750 unsigned long *bounds_ptr
;
8752 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
8753 /* then add global bound info */
8754 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
8755 bounds_ptr
[0] = 0; /* relocated */
8756 bounds_ptr
[1] = size
;
8760 decl_initializer(type
, sec
, addr
, 1, 0);
8761 /* restore parse state if needed */
8763 tok_str_free(init_str
.str
);
8764 restore_parse_state(&saved_parse_state
);
8770 void put_func_debug(Sym
*sym
)
8775 /* XXX: we put here a dummy type */
8776 snprintf(buf
, sizeof(buf
), "%s:%c1",
8777 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
8778 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
8779 cur_text_section
, sym
->c
);
8784 /* parse an old style function declaration list */
8785 /* XXX: check multiple parameter */
8786 static void func_decl_list(Sym
*func_sym
)
8793 /* parse each declaration */
8794 while (tok
!= '{' && tok
!= ';' && tok
!= ',' && tok
!= TOK_EOF
) {
8795 if (!parse_btype(&btype
, &ad
))
8796 expect("declaration list");
8797 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
8798 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
8800 /* we accept no variable after */
8804 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
8805 /* find parameter in function parameter list */
8808 if ((s
->v
& ~SYM_FIELD
) == v
)
8812 error("declaration for parameter '%s' but no such parameter",
8813 get_tok_str(v
, NULL
));
8815 /* check that no storage specifier except 'register' was given */
8816 if (type
.t
& VT_STORAGE
)
8817 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
8818 convert_parameter_type(&type
);
8819 /* we can add the type (NOTE: it could be local to the function) */
8821 /* accept other parameters */
8832 /* parse a function defined by symbol 'sym' and generate its code in
8833 'cur_text_section' */
8834 static void gen_function(Sym
*sym
)
8836 ind
= cur_text_section
->data_offset
;
8837 /* NOTE: we patch the symbol size later */
8838 put_extern_sym(sym
, cur_text_section
, ind
, 0);
8839 funcname
= get_tok_str(sym
->v
, NULL
);
8841 /* put debug symbol */
8843 put_func_debug(sym
);
8844 /* push a dummy symbol to enable local sym storage */
8845 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
8846 gfunc_prolog(&sym
->type
);
8848 block(NULL
, NULL
, NULL
, NULL
, 0, 0);
8851 cur_text_section
->data_offset
= ind
;
8852 label_pop(&global_label_stack
, NULL
);
8853 sym_pop(&local_stack
, NULL
); /* reset local stack */
8854 /* end of function */
8855 /* patch symbol size */
8856 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_size
=
8859 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
8861 funcname
= ""; /* for safety */
8862 func_vt
.t
= VT_VOID
; /* for safety */
8863 ind
= 0; /* for safety */
8866 static void gen_inline_functions(void)
8870 int *str
, inline_generated
;
8872 /* iterate while inline function are referenced */
8874 inline_generated
= 0;
8875 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
8877 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
8878 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
8879 (VT_STATIC
| VT_INLINE
) &&
8881 /* the function was used: generate its code and
8882 convert it to a normal function */
8883 str
= (int *)sym
->r
;
8884 sym
->r
= VT_SYM
| VT_CONST
;
8885 type
->t
&= ~VT_INLINE
;
8889 cur_text_section
= text_section
;
8891 macro_ptr
= NULL
; /* fail safe */
8894 inline_generated
= 1;
8897 if (!inline_generated
)
8901 /* free all remaining inline function tokens */
8902 for(sym
= global_stack
; sym
!= NULL
; sym
= sym
->prev
) {
8904 if (((type
->t
& VT_BTYPE
) == VT_FUNC
) &&
8905 (type
->t
& (VT_STATIC
| VT_INLINE
)) ==
8906 (VT_STATIC
| VT_INLINE
)) {
8907 str
= (int *)sym
->r
;
8909 sym
->r
= 0; /* fail safe */
8914 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8915 static void decl(int l
)
8923 if (!parse_btype(&btype
, &ad
)) {
8924 /* skip redundant ';' */
8925 /* XXX: find more elegant solution */
8930 if (l
== VT_CONST
&&
8931 (tok
== TOK_ASM1
|| tok
== TOK_ASM2
|| tok
== TOK_ASM3
)) {
8932 /* global asm block */
8936 /* special test for old K&R protos without explicit int
8937 type. Only accepted when defining global data */
8938 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
8942 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
8943 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
8945 /* we accept no variable after */
8949 while (1) { /* iterate thru each declaration */
8951 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
8955 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
8956 printf("type = '%s'\n", buf
);
8959 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
8960 /* if old style function prototype, we accept a
8963 if (sym
->c
== FUNC_OLD
)
8964 func_decl_list(sym
);
8969 error("cannot use local functions");
8970 if (!(type
.t
& VT_FUNC
))
8971 expect("function definition");
8973 /* reject abstract declarators in function definition */
8975 while ((sym
= sym
->next
) != NULL
)
8976 if (!(sym
->v
& ~SYM_FIELD
))
8977 expect("identifier");
8979 /* XXX: cannot do better now: convert extern line to static inline */
8980 if ((type
.t
& (VT_EXTERN
| VT_INLINE
)) == (VT_EXTERN
| VT_INLINE
))
8981 type
.t
= (type
.t
& ~VT_EXTERN
) | VT_STATIC
;
8985 if ((sym
->type
.t
& VT_BTYPE
) != VT_FUNC
)
8987 /* specific case: if not func_call defined, we put
8988 the one of the prototype */
8989 /* XXX: should have default value */
8990 if (sym
->type
.ref
->r
!= FUNC_CDECL
&&
8991 type
.ref
->r
== FUNC_CDECL
)
8992 type
.ref
->r
= sym
->type
.ref
->r
;
8993 if (!is_compatible_types(&sym
->type
, &type
)) {
8995 error("incompatible types for redefinition of '%s'",
8996 get_tok_str(v
, NULL
));
8998 /* if symbol is already defined, then put complete type */
9001 /* put function symbol */
9002 sym
= global_identifier_push(v
, type
.t
, 0);
9003 sym
->type
.ref
= type
.ref
;
9006 /* static inline functions are just recorded as a kind
9007 of macro. Their code will be emitted at the end of
9008 the compilation unit only if they are used */
9009 if ((type
.t
& (VT_INLINE
| VT_STATIC
)) ==
9010 (VT_INLINE
| VT_STATIC
)) {
9011 TokenString func_str
;
9014 tok_str_new(&func_str
);
9020 error("unexpected end of file");
9021 tok_str_add_tok(&func_str
);
9026 } else if (t
== '}') {
9028 if (block_level
== 0)
9032 tok_str_add(&func_str
, -1);
9033 tok_str_add(&func_str
, 0);
9034 sym
->r
= (int)func_str
.str
;
9036 /* compute text section */
9037 cur_text_section
= ad
.section
;
9038 if (!cur_text_section
)
9039 cur_text_section
= text_section
;
9040 sym
->r
= VT_SYM
| VT_CONST
;
9042 #ifdef TCC_TARGET_PE
9044 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_other
|= 1;
9050 if (btype
.t
& VT_TYPEDEF
) {
9051 /* save typedefed type */
9052 /* XXX: test storage specifiers ? */
9053 sym
= sym_push(v
, &type
, 0, 0);
9054 sym
->type
.t
|= VT_TYPEDEF
;
9055 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
9056 /* external function definition */
9057 /* specific case for func_call attribute */
9059 type
.ref
->r
= ad
.func_call
;
9060 external_sym(v
, &type
, 0);
9062 /* not lvalue if array */
9064 if (!(type
.t
& VT_ARRAY
))
9065 r
|= lvalue_type(type
.t
);
9066 has_init
= (tok
== '=');
9067 if ((btype
.t
& VT_EXTERN
) ||
9068 ((type
.t
& VT_ARRAY
) && (type
.t
& VT_STATIC
) &&
9069 !has_init
&& l
== VT_CONST
&& type
.ref
->c
< 0)) {
9070 /* external variable */
9071 /* NOTE: as GCC, uninitialized global static
9072 arrays of null size are considered as
9074 external_sym(v
, &type
, r
);
9076 if (type
.t
& VT_STATIC
)
9082 decl_initializer_alloc(&type
, &ad
, r
,
9096 /* better than nothing, but needs extension to handle '-E' option
9098 static void preprocess_init(TCCState
*s1
)
9100 s1
->include_stack_ptr
= s1
->include_stack
;
9101 /* XXX: move that before to avoid having to initialize
9102 file->ifdef_stack_ptr ? */
9103 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
9104 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
9106 /* XXX: not ANSI compliant: bound checking says error */
9108 s1
->pack_stack
[0] = 0;
9109 s1
->pack_stack_ptr
= s1
->pack_stack
;
9112 /* compile the C file opened in 'file'. Return non zero if errors. */
9113 static int tcc_compile(TCCState
*s1
)
9117 volatile int section_sym
;
9120 printf("%s: **** new file\n", file
->filename
);
9122 preprocess_init(s1
);
9125 anon_sym
= SYM_FIRST_ANOM
;
9127 /* file info: full path + filename */
9128 section_sym
= 0; /* avoid warning */
9130 section_sym
= put_elf_sym(symtab_section
, 0, 0,
9131 ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
), 0,
9132 text_section
->sh_num
, NULL
);
9133 getcwd(buf
, sizeof(buf
));
9134 pstrcat(buf
, sizeof(buf
), "/");
9135 put_stabs_r(buf
, N_SO
, 0, 0,
9136 text_section
->data_offset
, text_section
, section_sym
);
9137 put_stabs_r(file
->filename
, N_SO
, 0, 0,
9138 text_section
->data_offset
, text_section
, section_sym
);
9140 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
9141 symbols can be safely used */
9142 put_elf_sym(symtab_section
, 0, 0,
9143 ELF32_ST_INFO(STB_LOCAL
, STT_FILE
), 0,
9144 SHN_ABS
, file
->filename
);
9146 /* define some often used types */
9147 int_type
.t
= VT_INT
;
9149 char_pointer_type
.t
= VT_BYTE
;
9150 mk_pointer(&char_pointer_type
);
9152 func_old_type
.t
= VT_FUNC
;
9153 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
9156 /* define 'void *alloca(unsigned int)' builtin function */
9161 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
9162 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
9165 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
9169 define_start
= define_stack
;
9171 if (setjmp(s1
->error_jmp_buf
) == 0) {
9173 s1
->error_set_jmp_enabled
= 1;
9175 ch
= file
->buf_ptr
[0];
9176 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
9177 parse_flags
= PARSE_FLAG_PREPROCESS
| PARSE_FLAG_TOK_NUM
;
9181 expect("declaration");
9183 /* end of translation unit info */
9185 put_stabs_r(NULL
, N_SO
, 0, 0,
9186 text_section
->data_offset
, text_section
, section_sym
);
9189 s1
->error_set_jmp_enabled
= 0;
9191 /* reset define stack, but leave -Dsymbols (may be incorrect if
9192 they are undefined) */
9193 free_defines(define_start
);
9195 gen_inline_functions();
9197 sym_pop(&global_stack
, NULL
);
9199 return s1
->nb_errors
!= 0 ? -1 : 0;
9203 int tcc_compile_string(TCCState
*s
, const char *str
)
9205 BufferedFile bf1
, *bf
= &bf1
;
9209 /* init file structure */
9211 /* XXX: avoid copying */
9213 buf
= tcc_malloc(len
+ 1);
9216 memcpy(buf
, str
, len
);
9219 bf
->buf_end
= buf
+ len
;
9220 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
9224 ret
= tcc_compile(s
);
9228 /* currently, no need to close */
9233 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
9234 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
9236 BufferedFile bf1
, *bf
= &bf1
;
9238 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
9239 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
9243 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
9245 /* init file structure */
9247 bf
->buf_ptr
= bf
->buffer
;
9248 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
9249 *bf
->buf_end
= CH_EOB
;
9250 bf
->filename
[0] = '\0';
9254 s1
->include_stack_ptr
= s1
->include_stack
;
9256 /* parse with define parser */
9257 ch
= file
->buf_ptr
[0];
9263 /* undefine a preprocessor symbol */
9264 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
9268 ts
= tok_alloc(sym
, strlen(sym
));
9269 s
= define_find(ts
->tok
);
9270 /* undefine symbol by putting an invalid name */
9275 #ifdef CONFIG_TCC_ASM
9277 #ifdef TCC_TARGET_I386
9278 #include "i386-asm.c"
9283 static void asm_instr(void)
9285 error("inline asm() not supported");
9287 static void asm_global_instr(void)
9289 error("inline asm() not supported");
9295 #ifdef TCC_TARGET_COFF
9296 #include "tcccoff.c"
9299 #ifdef TCC_TARGET_PE
9303 /* print the position in the source file of PC value 'pc' by reading
9304 the stabs debug information */
9305 static void rt_printline(unsigned long wanted_pc
)
9307 Stab_Sym
*sym
, *sym_end
;
9308 char func_name
[128], last_func_name
[128];
9309 unsigned long func_addr
, last_pc
, pc
;
9310 const char *incl_files
[INCLUDE_STACK_SIZE
];
9311 int incl_index
, len
, last_line_num
, i
;
9312 const char *str
, *p
;
9314 fprintf(stderr
, "0x%08lx:", wanted_pc
);
9316 func_name
[0] = '\0';
9319 last_func_name
[0] = '\0';
9320 last_pc
= 0xffffffff;
9322 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
9323 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
9324 while (sym
< sym_end
) {
9325 switch(sym
->n_type
) {
9326 /* function start or end */
9328 if (sym
->n_strx
== 0) {
9329 /* we test if between last line and end of function */
9330 pc
= sym
->n_value
+ func_addr
;
9331 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9333 func_name
[0] = '\0';
9336 str
= stabstr_section
->data
+ sym
->n_strx
;
9337 p
= strchr(str
, ':');
9339 pstrcpy(func_name
, sizeof(func_name
), str
);
9342 if (len
> sizeof(func_name
) - 1)
9343 len
= sizeof(func_name
) - 1;
9344 memcpy(func_name
, str
, len
);
9345 func_name
[len
] = '\0';
9347 func_addr
= sym
->n_value
;
9350 /* line number info */
9352 pc
= sym
->n_value
+ func_addr
;
9353 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
9356 last_line_num
= sym
->n_desc
;
9358 strcpy(last_func_name
, func_name
);
9362 str
= stabstr_section
->data
+ sym
->n_strx
;
9364 if (incl_index
< INCLUDE_STACK_SIZE
) {
9365 incl_files
[incl_index
++] = str
;
9373 if (sym
->n_strx
== 0) {
9374 incl_index
= 0; /* end of translation unit */
9376 str
= stabstr_section
->data
+ sym
->n_strx
;
9377 /* do not add path */
9379 if (len
> 0 && str
[len
- 1] != '/')
9387 /* second pass: we try symtab symbols (no line number info) */
9390 Elf32_Sym
*sym
, *sym_end
;
9393 sym_end
= (Elf32_Sym
*)(symtab_section
->data
+ symtab_section
->data_offset
);
9394 for(sym
= (Elf32_Sym
*)symtab_section
->data
+ 1;
9397 type
= ELF32_ST_TYPE(sym
->st_info
);
9398 if (type
== STT_FUNC
) {
9399 if (wanted_pc
>= sym
->st_value
&&
9400 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
9401 pstrcpy(last_func_name
, sizeof(last_func_name
),
9402 strtab_section
->data
+ sym
->st_name
);
9408 /* did not find any info: */
9409 fprintf(stderr
, " ???\n");
9412 if (last_func_name
[0] != '\0') {
9413 fprintf(stderr
, " %s()", last_func_name
);
9415 if (incl_index
> 0) {
9416 fprintf(stderr
, " (%s:%d",
9417 incl_files
[incl_index
- 1], last_line_num
);
9418 for(i
= incl_index
- 2; i
>= 0; i
--)
9419 fprintf(stderr
, ", included from %s", incl_files
[i
]);
9420 fprintf(stderr
, ")");
9422 fprintf(stderr
, "\n");
9425 #if !defined(WIN32) && !defined(CONFIG_TCCBOOT)
9429 /* fix for glibc 2.1 */
9435 /* return the PC at frame level 'level'. Return non zero if not found */
9436 static int rt_get_caller_pc(unsigned long *paddr
,
9437 ucontext_t
*uc
, int level
)
9443 #if defined(__FreeBSD__)
9444 *paddr
= uc
->uc_mcontext
.mc_eip
;
9445 #elif defined(__dietlibc__)
9446 *paddr
= uc
->uc_mcontext
.eip
;
9448 *paddr
= uc
->uc_mcontext
.gregs
[REG_EIP
];
9452 #if defined(__FreeBSD__)
9453 fp
= uc
->uc_mcontext
.mc_ebp
;
9454 #elif defined(__dietlibc__)
9455 fp
= uc
->uc_mcontext
.ebp
;
9457 fp
= uc
->uc_mcontext
.gregs
[REG_EBP
];
9459 for(i
=1;i
<level
;i
++) {
9460 /* XXX: check address validity with program info */
9461 if (fp
<= 0x1000 || fp
>= 0xc0000000)
9463 fp
= ((unsigned long *)fp
)[0];
9465 *paddr
= ((unsigned long *)fp
)[1];
9471 #warning add arch specific rt_get_caller_pc()
9473 static int rt_get_caller_pc(unsigned long *paddr
,
9474 ucontext_t
*uc
, int level
)
9480 /* emit a run time error at position 'pc' */
9481 void rt_error(ucontext_t
*uc
, const char *fmt
, ...)
9488 fprintf(stderr
, "Runtime error: ");
9489 vfprintf(stderr
, fmt
, ap
);
9490 fprintf(stderr
, "\n");
9491 for(i
=0;i
<num_callers
;i
++) {
9492 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
9495 fprintf(stderr
, "at ");
9497 fprintf(stderr
, "by ");
9504 /* signal handler for fatal errors */
9505 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
9507 ucontext_t
*uc
= puc
;
9511 switch(siginf
->si_code
) {
9514 rt_error(uc
, "division by zero");
9517 rt_error(uc
, "floating point exception");
9523 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
9524 rt_error(uc
, *rt_bound_error_msg
);
9526 rt_error(uc
, "dereferencing invalid pointer");
9529 rt_error(uc
, "illegal instruction");
9532 rt_error(uc
, "abort() called");
9535 rt_error(uc
, "caught signal %d", signum
);
9542 /* do all relocations (needed before using tcc_get_symbol()) */
9543 int tcc_relocate(TCCState
*s1
)
9550 #ifdef TCC_TARGET_PE
9553 tcc_add_runtime(s1
);
9556 relocate_common_syms();
9558 tcc_add_linker_symbols(s1
);
9560 build_got_entries(s1
);
9562 /* compute relocation address : section are relocated in place. We
9563 also alloc the bss space */
9564 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9565 s
= s1
->sections
[i
];
9566 if (s
->sh_flags
& SHF_ALLOC
) {
9567 if (s
->sh_type
== SHT_NOBITS
)
9568 s
->data
= tcc_mallocz(s
->data_offset
);
9569 s
->sh_addr
= (unsigned long)s
->data
;
9573 relocate_syms(s1
, 1);
9575 if (s1
->nb_errors
!= 0)
9578 /* relocate each section */
9579 for(i
= 1; i
< s1
->nb_sections
; i
++) {
9580 s
= s1
->sections
[i
];
9582 relocate_section(s1
, s
);
9587 /* launch the compiled program with the given arguments */
9588 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
9590 int (*prog_main
)(int, char **);
9592 if (tcc_relocate(s1
) < 0)
9595 prog_main
= tcc_get_symbol_err(s1
, "main");
9598 #if defined(WIN32) || defined(CONFIG_TCCBOOT)
9599 error("debug mode currently not available for Windows");
9601 struct sigaction sigact
;
9602 /* install TCC signal handlers to print debug info on fatal
9604 sigact
.sa_flags
= SA_SIGINFO
| SA_RESETHAND
;
9605 sigact
.sa_sigaction
= sig_error
;
9606 sigemptyset(&sigact
.sa_mask
);
9607 sigaction(SIGFPE
, &sigact
, NULL
);
9608 sigaction(SIGILL
, &sigact
, NULL
);
9609 sigaction(SIGSEGV
, &sigact
, NULL
);
9610 sigaction(SIGBUS
, &sigact
, NULL
);
9611 sigaction(SIGABRT
, &sigact
, NULL
);
9615 #ifdef CONFIG_TCC_BCHECK
9616 if (do_bounds_check
) {
9617 void (*bound_init
)(void);
9619 /* set error function */
9620 rt_bound_error_msg
= (void *)tcc_get_symbol_err(s1
,
9621 "__bound_error_msg");
9623 /* XXX: use .init section so that it also work in binary ? */
9624 bound_init
= (void *)tcc_get_symbol_err(s1
, "__bound_init");
9628 return (*prog_main
)(argc
, argv
);
9631 TCCState
*tcc_new(void)
9638 s
= tcc_mallocz(sizeof(TCCState
));
9642 s
->output_type
= TCC_OUTPUT_MEMORY
;
9644 /* init isid table */
9646 isidnum_table
[i
] = isid(i
) || isnum(i
);
9648 /* add all tokens */
9650 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
9652 tok_ident
= TOK_IDENT
;
9661 ts
= tok_alloc(p
, r
- p
- 1);
9665 /* we add dummy defines for some special macros to speed up tests
9666 and to have working defined() */
9667 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
9668 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
9669 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
9670 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
9672 /* standard defines */
9673 tcc_define_symbol(s
, "__STDC__", NULL
);
9674 #if defined(TCC_TARGET_I386)
9675 tcc_define_symbol(s
, "__i386__", NULL
);
9677 #if defined(TCC_TARGET_ARM)
9678 tcc_define_symbol(s
, "__ARM_ARCH_4__", NULL
);
9679 tcc_define_symbol(s
, "__arm_elf__", NULL
);
9680 tcc_define_symbol(s
, "__arm_elf", NULL
);
9681 tcc_define_symbol(s
, "arm_elf", NULL
);
9682 tcc_define_symbol(s
, "__arm__", NULL
);
9683 tcc_define_symbol(s
, "__arm", NULL
);
9684 tcc_define_symbol(s
, "arm", NULL
);
9685 tcc_define_symbol(s
, "__APCS_32__", NULL
);
9688 tcc_define_symbol(s
, "__linux__", NULL
);
9689 tcc_define_symbol(s
, "linux", NULL
);
9691 /* tiny C specific defines */
9692 tcc_define_symbol(s
, "__TINYC__", NULL
);
9694 /* tiny C & gcc defines */
9695 tcc_define_symbol(s
, "__SIZE_TYPE__", "unsigned int");
9696 tcc_define_symbol(s
, "__PTRDIFF_TYPE__", "int");
9697 tcc_define_symbol(s
, "__WCHAR_TYPE__", "int");
9699 /* default library paths */
9700 #ifdef TCC_TARGET_PE
9703 snprintf(buf
, sizeof(buf
), "%s/lib", tcc_lib_path
);
9704 tcc_add_library_path(s
, buf
);
9707 tcc_add_library_path(s
, "/usr/local/lib");
9708 tcc_add_library_path(s
, "/usr/lib");
9709 tcc_add_library_path(s
, "/lib");
9712 /* no section zero */
9713 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
9715 /* create standard sections */
9716 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
9717 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
9718 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
9720 /* symbols are always generated for linking stage */
9721 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
9723 ".hashtab", SHF_PRIVATE
);
9724 strtab_section
= symtab_section
->link
;
9726 /* private symbol table for dynamic symbols */
9727 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
9729 ".dynhashtab", SHF_PRIVATE
);
9730 s
->alacarte_link
= 1;
9732 #ifdef CHAR_IS_UNSIGNED
9733 s
->char_is_unsigned
= 1;
9738 void tcc_delete(TCCState
*s1
)
9742 /* free -D defines */
9746 n
= tok_ident
- TOK_IDENT
;
9747 for(i
= 0; i
< n
; i
++)
9748 tcc_free(table_ident
[i
]);
9749 tcc_free(table_ident
);
9751 /* free all sections */
9753 free_section(symtab_section
->hash
);
9755 free_section(s1
->dynsymtab_section
->hash
);
9756 free_section(s1
->dynsymtab_section
->link
);
9757 free_section(s1
->dynsymtab_section
);
9759 for(i
= 1; i
< s1
->nb_sections
; i
++)
9760 free_section(s1
->sections
[i
]);
9761 tcc_free(s1
->sections
);
9763 /* free loaded dlls array */
9764 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
9765 tcc_free(s1
->loaded_dlls
[i
]);
9766 tcc_free(s1
->loaded_dlls
);
9769 for(i
= 0; i
< s1
->nb_library_paths
; i
++)
9770 tcc_free(s1
->library_paths
[i
]);
9771 tcc_free(s1
->library_paths
);
9773 /* cached includes */
9774 for(i
= 0; i
< s1
->nb_cached_includes
; i
++)
9775 tcc_free(s1
->cached_includes
[i
]);
9776 tcc_free(s1
->cached_includes
);
9778 for(i
= 0; i
< s1
->nb_include_paths
; i
++)
9779 tcc_free(s1
->include_paths
[i
]);
9780 tcc_free(s1
->include_paths
);
9782 for(i
= 0; i
< s1
->nb_sysinclude_paths
; i
++)
9783 tcc_free(s1
->sysinclude_paths
[i
]);
9784 tcc_free(s1
->sysinclude_paths
);
9789 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
9793 pathname1
= tcc_strdup(pathname
);
9794 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
9798 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
9802 pathname1
= tcc_strdup(pathname
);
9803 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
9807 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
9809 const char *ext
, *filename1
;
9812 BufferedFile
*saved_file
;
9814 /* find source file type with extension */
9815 filename1
= strrchr(filename
, '/');
9819 filename1
= filename
;
9820 ext
= strrchr(filename1
, '.');
9826 file
= tcc_open(s1
, filename
);
9828 if (flags
& AFF_PRINT_ERROR
) {
9829 error_noabort("file '%s' not found", filename
);
9835 if (!ext
|| !strcmp(ext
, "c")) {
9836 /* C file assumed */
9837 ret
= tcc_compile(s1
);
9839 #ifdef CONFIG_TCC_ASM
9840 if (!strcmp(ext
, "S")) {
9841 /* preprocessed assembler */
9842 ret
= tcc_assemble(s1
, 1);
9843 } else if (!strcmp(ext
, "s")) {
9844 /* non preprocessed assembler */
9845 ret
= tcc_assemble(s1
, 0);
9848 #ifdef TCC_TARGET_PE
9849 if (!strcmp(ext
, "def")) {
9850 ret
= pe_load_def_file(s1
, fdopen(file
->fd
, "rb"));
9855 /* assume executable format: auto guess file type */
9856 ret
= read(fd
, &ehdr
, sizeof(ehdr
));
9857 lseek(fd
, 0, SEEK_SET
);
9859 error_noabort("could not read header");
9861 } else if (ret
!= sizeof(ehdr
)) {
9862 goto try_load_script
;
9865 if (ehdr
.e_ident
[0] == ELFMAG0
&&
9866 ehdr
.e_ident
[1] == ELFMAG1
&&
9867 ehdr
.e_ident
[2] == ELFMAG2
&&
9868 ehdr
.e_ident
[3] == ELFMAG3
) {
9869 file
->line_num
= 0; /* do not display line number if error */
9870 if (ehdr
.e_type
== ET_REL
) {
9871 ret
= tcc_load_object_file(s1
, fd
, 0);
9872 } else if (ehdr
.e_type
== ET_DYN
) {
9873 if (s1
->output_type
== TCC_OUTPUT_MEMORY
) {
9874 #ifdef TCC_TARGET_PE
9878 h
= dlopen(filename
, RTLD_GLOBAL
| RTLD_LAZY
);
9885 ret
= tcc_load_dll(s1
, fd
, filename
,
9886 (flags
& AFF_REFERENCED_DLL
) != 0);
9889 error_noabort("unrecognized ELF file");
9892 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
9893 file
->line_num
= 0; /* do not display line number if error */
9894 ret
= tcc_load_archive(s1
, fd
);
9896 #ifdef TCC_TARGET_COFF
9897 if (*(uint16_t *)(&ehdr
) == COFF_C67_MAGIC
) {
9898 ret
= tcc_load_coff(s1
, fd
);
9902 /* as GNU ld, consider it is an ld script if not recognized */
9904 ret
= tcc_load_ldscript(s1
);
9906 error_noabort("unrecognized file type");
9921 int tcc_add_file(TCCState
*s
, const char *filename
)
9923 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
9926 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
9930 pathname1
= tcc_strdup(pathname
);
9931 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
9935 /* find and load a dll. Return non zero if not found */
9936 /* XXX: add '-rpath' option support ? */
9937 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
9942 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
9943 snprintf(buf
, sizeof(buf
), "%s/%s",
9944 s
->library_paths
[i
], filename
);
9945 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
9951 /* the library name is the same as the argument of the '-l' option */
9952 int tcc_add_library(TCCState
*s
, const char *libraryname
)
9957 /* first we look for the dynamic library if not static linking */
9958 if (!s
->static_link
) {
9959 #ifdef TCC_TARGET_PE
9960 snprintf(buf
, sizeof(buf
), "%s.def", libraryname
);
9962 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
9964 if (tcc_add_dll(s
, buf
, 0) == 0)
9968 /* then we look for the static library */
9969 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
9970 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
9971 s
->library_paths
[i
], libraryname
);
9972 if (tcc_add_file_internal(s
, buf
, 0) == 0)
9978 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
9980 add_elf_sym(symtab_section
, val
, 0,
9981 ELF32_ST_INFO(STB_GLOBAL
, STT_NOTYPE
), 0,
9986 int tcc_set_output_type(TCCState
*s
, int output_type
)
9988 s
->output_type
= output_type
;
9993 /* default include paths */
9994 /* XXX: reverse order needed if -isystem support */
9995 #ifndef TCC_TARGET_PE
9996 tcc_add_sysinclude_path(s
, "/usr/local/include");
9997 tcc_add_sysinclude_path(s
, "/usr/include");
9999 snprintf(buf
, sizeof(buf
), "%s/include", tcc_lib_path
);
10000 tcc_add_sysinclude_path(s
, buf
);
10001 #ifdef TCC_TARGET_PE
10002 snprintf(buf
, sizeof(buf
), "%s/include/winapi", tcc_lib_path
);
10003 tcc_add_sysinclude_path(s
, buf
);
10007 /* if bound checking, then add corresponding sections */
10008 #ifdef CONFIG_TCC_BCHECK
10009 if (do_bounds_check
) {
10010 /* define symbol */
10011 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
10012 /* create bounds sections */
10013 bounds_section
= new_section(s
, ".bounds",
10014 SHT_PROGBITS
, SHF_ALLOC
);
10015 lbounds_section
= new_section(s
, ".lbounds",
10016 SHT_PROGBITS
, SHF_ALLOC
);
10020 if (s
->char_is_unsigned
) {
10021 tcc_define_symbol(s
, "__CHAR_UNSIGNED__", NULL
);
10024 /* add debug sections */
10027 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
10028 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
10029 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
10030 put_elf_str(stabstr_section
, "");
10031 stab_section
->link
= stabstr_section
;
10032 /* put first entry */
10033 put_stabs("", 0, 0, 0, 0);
10036 /* add libc crt1/crti objects */
10037 #ifndef TCC_TARGET_PE
10038 if ((output_type
== TCC_OUTPUT_EXE
|| output_type
== TCC_OUTPUT_DLL
) &&
10040 if (output_type
!= TCC_OUTPUT_DLL
)
10041 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
10042 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
10048 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
10049 #define FD_INVERT 0x0002 /* invert value before storing */
10051 typedef struct FlagDef
{
10057 static const FlagDef warning_defs
[] = {
10058 { offsetof(TCCState
, warn_unsupported
), 0, "unsupported" },
10059 { offsetof(TCCState
, warn_write_strings
), 0, "write-strings" },
10060 { offsetof(TCCState
, warn_error
), 0, "error" },
10061 { offsetof(TCCState
, warn_implicit_function_declaration
), WD_ALL
,
10062 "implicit-function-declaration" },
10065 static int set_flag(TCCState
*s
, const FlagDef
*flags
, int nb_flags
,
10066 const char *name
, int value
)
10073 if (r
[0] == 'n' && r
[1] == 'o' && r
[2] == '-') {
10077 for(i
= 0, p
= flags
; i
< nb_flags
; i
++, p
++) {
10078 if (!strcmp(r
, p
->name
))
10083 if (p
->flags
& FD_INVERT
)
10085 *(int *)((uint8_t *)s
+ p
->offset
) = value
;
10090 /* set/reset a warning */
10091 int tcc_set_warning(TCCState
*s
, const char *warning_name
, int value
)
10096 if (!strcmp(warning_name
, "all")) {
10097 for(i
= 0, p
= warning_defs
; i
< countof(warning_defs
); i
++, p
++) {
10098 if (p
->flags
& WD_ALL
)
10099 *(int *)((uint8_t *)s
+ p
->offset
) = 1;
10103 return set_flag(s
, warning_defs
, countof(warning_defs
),
10104 warning_name
, value
);
10108 static const FlagDef flag_defs
[] = {
10109 { offsetof(TCCState
, char_is_unsigned
), 0, "unsigned-char" },
10110 { offsetof(TCCState
, char_is_unsigned
), FD_INVERT
, "signed-char" },
10111 { offsetof(TCCState
, nocommon
), FD_INVERT
, "common" },
10114 /* set/reset a flag */
10115 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
)
10117 return set_flag(s
, flag_defs
, countof(flag_defs
),
10121 #if !defined(LIBTCC)
10123 /* extract the basename of a file */
10124 static const char *tcc_basename(const char *name
)
10127 p
= strrchr(name
, '/');
10130 p
= strrchr(name
, '\\');
10139 static int64_t getclock_us(void)
10144 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
10147 gettimeofday(&tv
, NULL
);
10148 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
10154 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2005 Fabrice Bellard\n"
10155 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
10156 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
10157 " [infile1 infile2...] [-run infile args...]\n"
10159 "General options:\n"
10160 " -v display current version\n"
10161 " -c compile only - generate an object file\n"
10162 " -o outfile set output filename\n"
10163 " -Bdir set tcc internal library path\n"
10164 " -bench output compilation statistics\n"
10165 " -run run compiled source\n"
10166 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
10167 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
10168 " -w disable all warnings\n"
10169 "Preprocessor options:\n"
10170 " -Idir add include path 'dir'\n"
10171 " -Dsym[=val] define 'sym' with value 'val'\n"
10172 " -Usym undefine 'sym'\n"
10173 "Linker options:\n"
10174 " -Ldir add library path 'dir'\n"
10175 " -llib link with dynamic or static library 'lib'\n"
10176 " -shared generate a shared library\n"
10177 " -static static linking\n"
10178 " -rdynamic export all global symbols to dynamic linker\n"
10179 " -r relocatable output\n"
10180 "Debugger options:\n"
10181 " -g generate runtime debug info\n"
10182 #ifdef CONFIG_TCC_BCHECK
10183 " -b compile with built-in memory and bounds checker (implies -g)\n"
10185 " -bt N show N callers in stack traces\n"
10189 #define TCC_OPTION_HAS_ARG 0x0001
10190 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
10192 typedef struct TCCOption
{
10220 TCC_OPTION_nostdinc
,
10221 TCC_OPTION_nostdlib
,
10222 TCC_OPTION_print_search_dirs
,
10223 TCC_OPTION_rdynamic
,
10230 static const TCCOption tcc_options
[] = {
10231 { "h", TCC_OPTION_HELP
, 0 },
10232 { "?", TCC_OPTION_HELP
, 0 },
10233 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
10234 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
10235 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
10236 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
10237 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
10238 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10239 { "bench", TCC_OPTION_bench
, 0 },
10240 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
10241 #ifdef CONFIG_TCC_BCHECK
10242 { "b", TCC_OPTION_b
, 0 },
10244 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10245 { "c", TCC_OPTION_c
, 0 },
10246 { "static", TCC_OPTION_static
, 0 },
10247 { "shared", TCC_OPTION_shared
, 0 },
10248 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
10249 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10250 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
10251 { "r", TCC_OPTION_r
, 0 },
10252 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10253 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10254 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10255 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
10256 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
10257 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
10258 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
10259 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
10260 { "v", TCC_OPTION_v
, 0 },
10261 { "w", TCC_OPTION_w
, 0 },
10262 { "pipe", TCC_OPTION_pipe
, 0},
10266 /* convert 'str' into an array of space separated strings */
10267 static int expand_args(char ***pargv
, const char *str
)
10276 while (is_space(*str
))
10281 while (*str
!= '\0' && !is_space(*str
))
10284 arg
= tcc_malloc(len
+ 1);
10285 memcpy(arg
, s1
, len
);
10287 dynarray_add((void ***)&argv
, &argc
, arg
);
10293 static char **files
;
10294 static int nb_files
, nb_libraries
;
10295 static int multiple_files
;
10296 static int print_search_dirs
;
10297 static int output_type
;
10298 static int reloc_output
;
10299 static const char *outfile
;
10301 int parse_args(TCCState
*s
, int argc
, char **argv
)
10304 const TCCOption
*popt
;
10305 const char *optarg
, *p1
, *r1
;
10310 if (optind
>= argc
) {
10311 if (nb_files
== 0 && !print_search_dirs
)
10316 r
= argv
[optind
++];
10318 /* add a new file */
10319 dynarray_add((void ***)&files
, &nb_files
, r
);
10320 if (!multiple_files
) {
10322 /* argv[0] will be this file */
10326 /* find option in table (match only the first chars */
10327 popt
= tcc_options
;
10331 error("invalid option -- '%s'", r
);
10344 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
10345 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
10348 if (optind
>= argc
)
10349 error("argument to '%s' is missing", r
);
10350 optarg
= argv
[optind
++];
10358 switch(popt
->index
) {
10359 case TCC_OPTION_HELP
:
10364 if (tcc_add_include_path(s
, optarg
) < 0)
10365 error("too many include paths");
10370 sym
= (char *)optarg
;
10371 value
= strchr(sym
, '=');
10376 tcc_define_symbol(s
, sym
, value
);
10380 tcc_undefine_symbol(s
, optarg
);
10383 tcc_add_library_path(s
, optarg
);
10386 /* set tcc utilities path (mainly for tcc development) */
10387 tcc_lib_path
= optarg
;
10390 dynarray_add((void ***)&files
, &nb_files
, r
);
10393 case TCC_OPTION_bench
:
10396 case TCC_OPTION_bt
:
10397 num_callers
= atoi(optarg
);
10399 #ifdef CONFIG_TCC_BCHECK
10401 do_bounds_check
= 1;
10409 multiple_files
= 1;
10410 output_type
= TCC_OUTPUT_OBJ
;
10412 case TCC_OPTION_static
:
10413 s
->static_link
= 1;
10415 case TCC_OPTION_shared
:
10416 output_type
= TCC_OUTPUT_DLL
;
10419 multiple_files
= 1;
10423 /* generate a .o merging several output files */
10425 output_type
= TCC_OUTPUT_OBJ
;
10427 case TCC_OPTION_nostdinc
:
10430 case TCC_OPTION_nostdlib
:
10433 case TCC_OPTION_print_search_dirs
:
10434 print_search_dirs
= 1;
10436 case TCC_OPTION_run
:
10440 argc1
= expand_args(&argv1
, optarg
);
10442 parse_args(s
, argc1
, argv1
);
10444 multiple_files
= 0;
10445 output_type
= TCC_OUTPUT_MEMORY
;
10449 printf("tcc version %s\n", TCC_VERSION
);
10452 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
10453 goto unsupported_option
;
10456 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
10457 s
->warn_unsupported
)
10458 goto unsupported_option
;
10463 case TCC_OPTION_rdynamic
:
10466 case TCC_OPTION_Wl
:
10469 if (strstart(optarg
, "-Ttext,", &p
)) {
10470 s
->text_addr
= strtoul(p
, NULL
, 16);
10471 s
->has_text_addr
= 1;
10472 } else if (strstart(optarg
, "--oformat,", &p
)) {
10473 if (strstart(p
, "elf32-", NULL
)) {
10474 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
10475 } else if (!strcmp(p
, "binary")) {
10476 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
10478 #ifdef TCC_TARGET_COFF
10479 if (!strcmp(p
, "coff")) {
10480 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
10484 error("target %s not found", p
);
10487 error("unsupported linker option '%s'", optarg
);
10492 if (s
->warn_unsupported
) {
10493 unsupported_option
:
10494 warning("unsupported option '%s'", r
);
10503 int main(int argc
, char **argv
)
10507 int nb_objfiles
, ret
, optind
;
10508 char objfilename
[1024];
10509 int64_t start_time
= 0;
10512 /* on win32, we suppose the lib and includes are at the location
10515 static char path
[1024];
10518 GetModuleFileNameA(NULL
, path
, sizeof path
);
10519 p
= d
= strlwr(path
);
10522 if (*d
== '\\') *d
= '/', p
= d
;
10526 tcc_lib_path
= path
;
10531 output_type
= TCC_OUTPUT_EXE
;
10533 multiple_files
= 1;
10538 print_search_dirs
= 0;
10540 optind
= parse_args(s
, argc
- 1, argv
+ 1) + 1;
10542 if (print_search_dirs
) {
10543 /* enough for Linux kernel */
10544 printf("install: %s/\n", tcc_lib_path
);
10548 nb_objfiles
= nb_files
- nb_libraries
;
10550 /* if outfile provided without other options, we output an
10552 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
10553 output_type
= TCC_OUTPUT_EXE
;
10555 /* check -c consistency : only single file handled. XXX: checks file type */
10556 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
10557 /* accepts only a single input file */
10558 if (nb_objfiles
!= 1)
10559 error("cannot specify multiple files with -c");
10560 if (nb_libraries
!= 0)
10561 error("cannot specify libraries with -c");
10564 if (output_type
!= TCC_OUTPUT_MEMORY
) {
10566 /* compute default outfile name */
10567 pstrcpy(objfilename
, sizeof(objfilename
) - 1,
10569 tcc_basename(files
[0]));
10570 #ifdef TCC_TARGET_PE
10571 pe_guess_outfile(objfilename
, output_type
);
10573 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
10574 char *ext
= strrchr(objfilename
, '.');
10576 goto default_outfile
;
10577 /* add .o extension */
10578 strcpy(ext
+ 1, "o");
10581 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
10584 outfile
= objfilename
;
10589 start_time
= getclock_us();
10592 tcc_set_output_type(s
, output_type
);
10594 /* compile or add each files or library */
10595 for(i
= 0;i
< nb_files
; i
++) {
10596 const char *filename
;
10598 filename
= files
[i
];
10599 if (filename
[0] == '-') {
10600 if (tcc_add_library(s
, filename
+ 2) < 0)
10601 error("cannot find %s", filename
);
10603 if (tcc_add_file(s
, filename
) < 0) {
10610 /* free all files */
10615 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
10616 if (total_time
< 0.001)
10617 total_time
= 0.001;
10618 if (total_bytes
< 1)
10620 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
10621 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
10622 total_time
, (int)(total_lines
/ total_time
),
10623 total_bytes
/ total_time
/ 1000000.0);
10626 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
10627 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
10629 #ifdef TCC_TARGET_PE
10630 if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
10631 ret
= tcc_output_pe(s
, outfile
);
10635 tcc_output_file(s
, outfile
);
10639 /* XXX: cannot do it with bound checking because of the malloc hooks */
10640 if (!do_bounds_check
)
10645 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);