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
30 #define CONFIG_TCC_STATIC
48 #include <sys/timeb.h>
49 #include <io.h> /* open, close etc. */
50 #include <direct.h> /* getcwd */
51 #define inline __inline
54 #define uplong unsigned long long
61 #include <sys/ucontext.h>
66 #endif /* !CONFIG_TCCBOOT */
69 #define uplong unsigned long
84 #define SA_SIGINFO 0x00000004u
91 /* preprocessor debug */
93 /* include file debug */
101 /* target selection */
102 //#define TCC_TARGET_I386 /* i386 code generator */
103 //#define TCC_TARGET_ARM /* ARMv4 code generator */
104 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
105 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
107 /* default target is I386 */
108 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
109 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
110 #define TCC_TARGET_I386
113 #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
114 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
115 #define CONFIG_TCC_BCHECK /* enable bound checking code */
118 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
119 #define CONFIG_TCC_STATIC
122 /* define it to include assembler support */
123 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
124 #define CONFIG_TCC_ASM
127 /* object format selection */
128 #if defined(TCC_TARGET_C67)
129 #define TCC_TARGET_COFF
132 #if !defined(CONFIG_TCCBOOT)
133 #define CONFIG_TCC_BACKTRACE
142 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
143 executables or dlls */
145 #ifndef CONFIG_TCC_LDDIR
146 #if defined(TCC_TARGET_X86_64_CENTOS)
147 #define CONFIG_TCC_LDDIR "/lib64"
149 #define CONFIG_TCC_LDDIR "/lib"
152 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr" CONFIG_TCC_LDDIR
153 #ifndef CONFIG_TCC_INCSUBDIR
154 #define CONFIG_TCC_INCSUBDIR ""
157 #define INCLUDE_STACK_SIZE 32
158 #define IFDEF_STACK_SIZE 64
159 #define VSTACK_SIZE 256
160 #define STRING_MAX_SIZE 1024
161 #define PACK_STACK_SIZE 8
163 #define TOK_HASH_SIZE 8192 /* must be a power of two */
164 #define TOK_ALLOC_INCR 512 /* must be a power of two */
165 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
167 /* token symbol management */
168 typedef struct TokenSym
{
169 struct TokenSym
*hash_next
;
170 struct Sym
*sym_define
; /* direct pointer to define */
171 struct Sym
*sym_label
; /* direct pointer to label */
172 struct Sym
*sym_struct
; /* direct pointer to structure */
173 struct Sym
*sym_identifier
; /* direct pointer to identifier */
174 int tok
; /* token number */
180 typedef unsigned short nwchar_t
;
182 typedef int nwchar_t
;
185 typedef struct CString
{
186 int size
; /* size in bytes */
187 void *data
; /* either 'char *' or 'nwchar_t *' */
189 void *data_allocated
; /* if non NULL, data has been malloced */
192 /* type definition */
193 typedef struct CType
{
199 typedef union CValue
{
205 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
207 unsigned long long ull
;
208 struct CString
*cstr
;
214 typedef struct SValue
{
215 CType type
; /* type */
216 unsigned short r
; /* register + flags */
217 unsigned short r2
; /* second register, used for 'long long'
218 type. If not used, set to VT_CONST */
219 CValue c
; /* constant, if VT_CONST */
220 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
223 /* symbol management */
225 int v
; /* symbol token */
226 char *asm_label
; /* associated asm label */
227 long r
; /* associated register */
229 long c
; /* associated number */
230 int *d
; /* define token stream */
232 CType type
; /* associated type */
234 struct Sym
*next
; /* next related symbol */
235 long jnext
; /* next jump label */
237 struct Sym
*prev
; /* prev symbol in stack */
238 struct Sym
*prev_tok
; /* previous symbol for this token */
241 /* section definition */
242 /* XXX: use directly ELF structure for parameters ? */
243 /* special flag to indicate that the section should not be linked to
245 #define SHF_PRIVATE 0x80000000
247 /* special flag, too */
248 #define SECTION_ABS ((void *)1)
250 typedef struct Section
{
251 unsigned long data_offset
; /* current data offset */
252 unsigned char *data
; /* section data */
253 unsigned long data_allocated
; /* used for realloc() handling */
254 int sh_name
; /* elf section name (only used during output) */
255 int sh_num
; /* elf section number */
256 int sh_type
; /* elf section type */
257 int sh_flags
; /* elf section flags */
258 int sh_info
; /* elf section info */
259 int sh_addralign
; /* elf section alignment */
260 int sh_entsize
; /* elf entry size */
261 unsigned long sh_size
; /* section size (only used during output) */
262 unsigned long sh_addr
; /* address at which the section is relocated */
263 unsigned long sh_offset
; /* file offset */
264 int nb_hashed_syms
; /* used to resize the hash table */
265 struct Section
*link
; /* link to another section */
266 struct Section
*reloc
; /* corresponding section for relocation, if any */
267 struct Section
*hash
; /* hash table for symbols */
268 struct Section
*next
;
269 char name
[1]; /* section name */
272 typedef struct DLLReference
{
278 /* GNUC attribute definition */
279 typedef struct AttributeDef
{
281 func_call
: 3, /* calling convention (0..5), see below */
282 aligned
: 5, /* alignement (0..16) */
290 struct Section
*section
;
291 int alias_target
; /* token */
294 /* gr: wrappers for casting sym->r for other purposes */
295 #define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call)
296 #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
297 #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
298 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
299 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
300 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
301 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
302 #define INT_ATTR(ad) (*(int*)(ad))
304 /* -------------------------------------------------- */
306 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
307 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
308 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
310 /* stored in 'Sym.c' field */
311 #define FUNC_NEW 1 /* ansi function prototype */
312 #define FUNC_OLD 2 /* old function prototype */
313 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
315 /* stored in 'Sym.r' field */
316 #define FUNC_CDECL 0 /* standard c call */
317 #define FUNC_STDCALL 1 /* pascal c call */
318 #define FUNC_FASTCALL1 2 /* first param in %eax */
319 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
320 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
321 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
323 /* field 'Sym.t' for macros */
324 #define MACRO_OBJ 0 /* object like macro */
325 #define MACRO_FUNC 1 /* function like macro */
327 /* field 'Sym.r' for C labels */
328 #define LABEL_DEFINED 0 /* label is defined */
329 #define LABEL_FORWARD 1 /* label is forward defined */
330 #define LABEL_DECLARED 2 /* label is declared but never used */
332 /* type_decl() types */
333 #define TYPE_ABSTRACT 1 /* type without variable */
334 #define TYPE_DIRECT 2 /* type with variable */
336 #define IO_BUF_SIZE 8192
338 typedef struct BufferedFile
{
342 struct BufferedFile
*prev
;
343 int line_num
; /* current line number - here to simplify code */
344 int ifndef_macro
; /* #ifndef macro / #endif search */
345 int ifndef_macro_saved
; /* saved ifndef_macro */
346 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
347 char inc_type
; /* type of include */
348 char inc_filename
[512]; /* filename specified by the user */
349 char filename
[1024]; /* current filename - here to simplify code */
350 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
353 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
354 #define CH_EOF (-1) /* end of file */
356 /* parsing state (used to save parser state to reparse part of the
357 source several times) */
358 typedef struct ParseState
{
359 const int *macro_ptr
;
365 /* used to record tokens */
366 typedef struct TokenString
{
373 /* inline functions */
374 typedef struct InlineFunc
{
380 /* include file cache, used to find files faster and also to eliminate
381 inclusion if the include file is protected by #ifndef ... #endif */
382 typedef struct CachedInclude
{
384 int hash_next
; /* -1 if none */
385 char type
; /* '"' or '>' to give include type */
386 char filename
[1]; /* path specified in #include */
389 #define CACHED_INCLUDES_HASH_SIZE 512
391 #ifdef CONFIG_TCC_ASM
392 typedef struct ExprValue
{
397 #define MAX_ASM_OPERANDS 30
398 typedef struct ASMOperand
{
399 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
401 char asm_str
[16]; /* computed asm string for operand */
402 SValue
*vt
; /* C value of the expression */
403 int ref_index
; /* if >= 0, gives reference to a output constraint */
404 int input_index
; /* if >= 0, gives reference to an input constraint */
405 int priority
; /* priority, used to assign registers */
406 int reg
; /* if >= 0, register number used for this operand */
407 int is_llong
; /* true if double register value */
408 int is_memory
; /* true if memory operand */
409 int is_rw
; /* for '+' modifier */
414 unsigned output_type
: 8;
415 unsigned reloc_output
: 1;
417 BufferedFile
**include_stack_ptr
;
418 int *ifdef_stack_ptr
;
420 /* include file handling */
421 char **include_paths
;
422 int nb_include_paths
;
423 char **sysinclude_paths
;
424 int nb_sysinclude_paths
;
425 CachedInclude
**cached_includes
;
426 int nb_cached_includes
;
428 char **library_paths
;
429 int nb_library_paths
;
431 /* array of all loaded dlls (including those referenced by loaded
433 DLLReference
**loaded_dlls
;
438 int nb_sections
; /* number of sections, including first dummy section */
440 Section
**priv_sections
;
441 int nb_priv_sections
; /* number of private sections */
446 unsigned long *got_offsets
;
448 /* give the correspondance from symtab indexes to dynsym indexes */
449 int *symtab_to_dynsym
;
451 /* temporary dynamic symbol sections (for dll loading) */
452 Section
*dynsymtab_section
;
453 /* exported dynamic symbol section */
456 int nostdinc
; /* if true, no standard headers are added */
457 int nostdlib
; /* if true, no standard libraries are added */
458 int nocommon
; /* if true, do not use common symbols for .bss data */
460 /* if true, static linking is performed */
463 /* soname as specified on the command line (-soname) */
465 /* rpath as specified on the command line (-Wl,-rpath=) */
468 /* if true, all symbols are exported */
471 /* if true, resolve symbols in the current module first (-Wl,Bsymbolic) */
474 /* if true, only link in referenced objects from archive */
477 /* address of text section */
478 unsigned long text_addr
;
481 /* symbols to call at load-time / unload-time */
482 const char *init_symbol
;
483 const char *fini_symbol
;
485 /* output format, see TCC_OUTPUT_FORMAT_xxx */
488 /* C language options */
489 int char_is_unsigned
;
490 int leading_underscore
;
492 /* warning switches */
493 int warn_write_strings
;
494 int warn_unsupported
;
497 int warn_implicit_function_declaration
;
499 /* display some information during compilation */
501 /* compile with debug symbol (and use them if error during execution) */
503 #ifdef CONFIG_TCC_BCHECK
504 /* compile with built-in memory and bounds checker */
507 /* give the path of the tcc libraries */
512 void (*error_func
)(void *opaque
, const char *msg
);
513 int error_set_jmp_enabled
;
514 jmp_buf error_jmp_buf
;
517 /* tiny assembler state */
520 /* see include_stack_ptr */
521 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
523 /* see ifdef_stack_ptr */
524 int ifdef_stack
[IFDEF_STACK_SIZE
];
526 /* see cached_includes */
527 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
530 int pack_stack
[PACK_STACK_SIZE
];
533 /* output file for preprocessing */
536 /* input files and libraries for this compilation */
542 /* automatically collected dependencies for this compilation */
546 /* for tcc_relocate */
551 unsigned long mem_size
;
554 struct InlineFunc
**inline_fns
;
557 #ifdef TCC_TARGET_I386
561 /* section alignment */
562 unsigned long section_align
;
567 unsigned long pe_file_align
;
568 unsigned long pe_stack_size
;
569 #ifdef TCC_TARGET_X86_64
576 #ifndef TCC_TARGET_PE
577 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM
578 /* write PLT and GOT here */
579 char *runtime_plt_and_got
;
580 unsigned int runtime_plt_and_got_offset
;
585 /* The current value can be: */
586 #define VT_VALMASK 0x001f
587 #define VT_CONST 0x0010 /* constant in vc
588 (must be first non register value) */
589 #define VT_LLOCAL 0x0011 /* lvalue, offset on stack */
590 #define VT_LOCAL 0x0012 /* offset on stack */
591 #define VT_CMP 0x0013 /* the value is stored in processor flags (in vc) */
592 #define VT_JMP 0x0014 /* value is the consequence of jmp true (even) */
593 #define VT_JMPI 0x0015 /* value is the consequence of jmp false (odd) */
594 #define VT_LVAL 0x0100 /* var is an lvalue */
595 #define VT_SYM 0x0200 /* a symbol value is added */
596 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
597 char/short stored in integer registers) */
598 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
599 dereferencing value */
600 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
601 bounding function call point is in vc */
602 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
603 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
604 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
605 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
607 #define VT_REF 0x0020
610 #define VT_INT 0 /* integer type */
611 #define VT_BYTE 1 /* signed byte type */
612 #define VT_SHORT 2 /* short type */
613 #define VT_VOID 3 /* void type */
614 #define VT_PTR 4 /* pointer */
615 #define VT_ENUM 5 /* enum definition */
616 #define VT_FUNC 6 /* function type */
617 #define VT_STRUCT 7 /* struct/union definition */
618 #define VT_FLOAT 8 /* IEEE float */
619 #define VT_DOUBLE 9 /* IEEE double */
620 #define VT_LDOUBLE 10 /* IEEE long double */
621 #define VT_BOOL 11 /* ISOC99 boolean type */
622 #define VT_LLONG 12 /* 64 bit integer */
623 #define VT_LONG 13 /* long integer (NEVER USED as type, only
625 #define VT_BTYPE 0x000f /* mask for basic type */
626 #define VT_UNSIGNED 0x0010 /* unsigned type */
627 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
628 #define VT_VLA 0x20000 /* VLA type (also has VT_PTR and VT_ARRAY) */
629 #define VT_BITFIELD 0x0040 /* bitfield modifier */
630 #define VT_CONSTANT 0x0800 /* const modifier */
631 #define VT_VOLATILE 0x1000 /* volatile modifier */
632 #define VT_SIGNED 0x2000 /* signed type */
635 #define VT_EXTERN 0x00000080 /* extern definition */
636 #define VT_STATIC 0x00000100 /* static variable */
637 #define VT_TYPEDEF 0x00000200 /* typedef definition */
638 #define VT_INLINE 0x00000400 /* inline definition */
639 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
640 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
641 #define VT_WEAK 0x00010000 /* win32: data exported from dll */
643 #define VT_STRUCT_SHIFT 18 /* shift for bitfield shift values */
645 /* type mask (except storage) */
646 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK)
647 #define VT_TYPE (~(VT_STORAGE))
651 /* warning: the following compare tokens depend on i386 asm code */
658 #define TOK_Nset 0x98
659 #define TOK_Nclear 0x99
665 #define TOK_LAND 0xa0
669 #define TOK_MID 0xa3 /* inc/dec, to void constant */
671 #define TOK_UDIV 0xb0 /* unsigned division */
672 #define TOK_UMOD 0xb1 /* unsigned modulo */
673 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
674 #define TOK_CINT 0xb3 /* number in tokc */
675 #define TOK_CCHAR 0xb4 /* char constant in tokc */
676 #define TOK_STR 0xb5 /* pointer to string in tokc */
677 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
678 #define TOK_LCHAR 0xb7
679 #define TOK_LSTR 0xb8
680 #define TOK_CFLOAT 0xb9 /* float constant */
681 #define TOK_LINENUM 0xba /* line number info */
682 #define TOK_CDOUBLE 0xc0 /* double constant */
683 #define TOK_CLDOUBLE 0xc1 /* long double constant */
684 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
685 #define TOK_ADDC1 0xc3 /* add with carry generation */
686 #define TOK_ADDC2 0xc4 /* add with carry use */
687 #define TOK_SUBC1 0xc5 /* add with carry generation */
688 #define TOK_SUBC2 0xc6 /* add with carry use */
689 #define TOK_CUINT 0xc8 /* unsigned int constant */
690 #define TOK_CLLONG 0xc9 /* long long constant */
691 #define TOK_CULLONG 0xca /* unsigned long long constant */
692 #define TOK_ARROW 0xcb
693 #define TOK_DOTS 0xcc /* three dots */
694 #define TOK_SHR 0xcd /* unsigned shift right */
695 #define TOK_PPNUM 0xce /* preprocessor number */
696 #define TOK_NOSUBST 0xcf /* means following token has already been pp'd */
698 #define TOK_SHL 0x01 /* shift left */
699 #define TOK_SAR 0x02 /* signed shift right */
701 /* assignement operators : normal operator or 0x80 */
702 #define TOK_A_MOD 0xa5
703 #define TOK_A_AND 0xa6
704 #define TOK_A_MUL 0xaa
705 #define TOK_A_ADD 0xab
706 #define TOK_A_SUB 0xad
707 #define TOK_A_DIV 0xaf
708 #define TOK_A_XOR 0xde
709 #define TOK_A_OR 0xfc
710 #define TOK_A_SHL 0x81
711 #define TOK_A_SAR 0x82
714 #define offsetof(type, field) ((size_t) &((type *)0)->field)
718 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
721 #define TOK_EOF (-1) /* end of file */
722 #define TOK_LINEFEED 10 /* line feed */
724 /* all identificators and strings have token above that */
725 #define TOK_IDENT 256
727 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
728 #define TOK_ASM_int TOK_INT
729 #define TOK_ASM_weak TOK_WEAK1
731 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
732 /* only used for i386 asm opcodes definitions */
734 DEF(TOK_ASM_ ## x ## b, #x "b") \
735 DEF(TOK_ASM_ ## x ## w, #x "w") \
736 DEF(TOK_ASM_ ## x ## l, #x "l") \
737 DEF(TOK_ASM_ ## x, #x)
739 DEF(TOK_ASM_ ## x ## w, #x "w") \
740 DEF(TOK_ASM_ ## x ## l, #x "l") \
741 DEF(TOK_ASM_ ## x, #x)
742 #ifdef TCC_TARGET_X86_64
743 # define DEF_BWLQ(x) \
744 DEF(TOK_ASM_ ## x ## b, #x "b") \
745 DEF(TOK_ASM_ ## x ## w, #x "w") \
746 DEF(TOK_ASM_ ## x ## l, #x "l") \
747 DEF(TOK_ASM_ ## x ## q, #x "q") \
748 DEF(TOK_ASM_ ## x, #x)
749 # define DEF_WLQ(x) \
750 DEF(TOK_ASM_ ## x ## w, #x "w") \
751 DEF(TOK_ASM_ ## x ## l, #x "l") \
752 DEF(TOK_ASM_ ## x ## q, #x "q") \
753 DEF(TOK_ASM_ ## x, #x)
754 # define DEF_BWLX DEF_BWLQ
755 # define DEF_WLX DEF_WLQ
756 /* number of sizes + 1 */
759 # define DEF_BWLX DEF_BWL
760 # define DEF_WLX DEF_WL
761 /* number of sizes + 1 */
766 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
767 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
768 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
769 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
772 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
773 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
776 #define DEF_ASMTEST(x) \
808 #endif // defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
811 TOK_LAST
= TOK_IDENT
- 1,
812 #define DEF(id, str) id,
817 #define TOK_UIDENT TOK_DEFINE
820 #define snprintf _snprintf
821 #define vsnprintf _vsnprintf
823 #define strtold (long double)strtod
824 #define strtof (float)strtod
825 #define strtoll (long long)strtol
827 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) \
828 || defined(__FreeBSD_kernel__) || defined(__DragonFly__) \
829 || defined(__OpenBSD__)
830 /* currently incorrect */
831 static inline long double strtold(const char *nptr
, char **endptr
)
833 return (long double)strtod(nptr
, endptr
);
835 static inline float strtof(const char *nptr
, char **endptr
)
837 return (float)strtod(nptr
, endptr
);
840 /* XXX: need to define this to use them in non ISOC99 context */
841 extern float strtof (const char *__nptr
, char **__endptr
);
842 extern long double strtold (const char *__nptr
, char **__endptr
);
846 #define IS_PATHSEP(c) (c == '/' || c == '\\')
847 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
848 #define PATHCMP stricmp
850 #define IS_PATHSEP(c) (c == '/')
851 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
852 #define PATHCMP strcmp
855 /* space exlcuding newline */
856 static inline int is_space(int ch
)
858 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
861 static inline int isid(int c
)
863 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') || c
== '_';
866 static inline int isnum(int c
)
868 return c
>= '0' && c
<= '9';
871 static inline int isoct(int c
)
873 return c
>= '0' && c
<= '7';
876 static inline int toup(int c
)
878 return (c
>= 'a' && c
<= 'z') ? c
- 'a' + 'A' : c
;
884 #define ST_INLN static inline
885 #define ST_FUNC static
886 #define ST_DATA static
890 #define ST_DATA extern
893 /* ------------ libtcc.c ------------ */
895 /* use GNU C extensions */
897 /* use Tiny C extensions */
899 /* XXX: get rid of this ASAP */
900 ST_DATA
struct TCCState
*tcc_state
;
902 #ifdef CONFIG_TCC_BACKTRACE
903 ST_DATA
int num_callers
;
904 ST_DATA
const char **rt_bound_error_msg
;
905 ST_DATA
void *rt_prog_main
;
908 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
909 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
910 #define AFF_PREPROCESS 0x0004 /* preprocess file */
912 /* public functions currently used by the tcc main function */
913 PUB_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
);
914 PUB_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
);
915 PUB_FUNC
char *tcc_basename(const char *name
);
916 PUB_FUNC
char *tcc_fileextension (const char *name
);
917 PUB_FUNC
void tcc_free(void *ptr
);
918 PUB_FUNC
void *tcc_malloc(unsigned long size
);
919 PUB_FUNC
void *tcc_mallocz(unsigned long size
);
920 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
);
921 PUB_FUNC
char *tcc_strdup(const char *str
);
922 #define free(p) use_tcc_free(p)
923 #define malloc(s) use_tcc_malloc(s)
924 #define realloc(p, s) use_tcc_realloc(p, s)
926 #define strdup(s) use_tcc_strdup(s)
927 PUB_FUNC
void tcc_memstats(void);
928 PUB_FUNC
void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
929 PUB_FUNC
void dynarray_reset(void *pp
, int *n
);
930 PUB_FUNC
void error_noabort(const char *fmt
, ...);
931 PUB_FUNC
void error(const char *fmt
, ...);
932 PUB_FUNC
void expect(const char *msg
);
933 PUB_FUNC
void warning(const char *fmt
, ...);
935 /* other utilities */
936 ST_INLN
void cstr_ccat(CString
*cstr
, int ch
);
937 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
);
938 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
);
939 ST_FUNC
void cstr_new(CString
*cstr
);
940 ST_FUNC
void cstr_free(CString
*cstr
);
941 ST_FUNC
void add_char(CString
*cstr
, int c
);
942 #define cstr_reset(cstr) cstr_free(cstr)
944 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
);
945 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
);
946 ST_FUNC
void *section_ptr_add(Section
*sec
, unsigned long size
);
947 ST_FUNC
void section_reserve(Section
*sec
, unsigned long size
);
948 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
);
950 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
, unsigned long value
, unsigned long size
, int can_add_underscore
);
951 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
, unsigned long value
, unsigned long size
);
952 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
);
954 ST_INLN
void sym_free(Sym
*sym
);
955 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
);
956 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
);
957 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
958 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
);
959 ST_INLN Sym
*struct_find(int v
);
960 ST_INLN Sym
*sym_find(int v
);
961 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
);
963 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
);
964 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
);
965 ST_FUNC
void tcc_close(void);
967 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
);
968 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
969 PUB_FUNC
int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
);
970 PUB_FUNC
void tcc_print_stats(TCCState
*s
, int64_t total_time
);
971 PUB_FUNC
void set_num_callers(int n
);
973 ST_FUNC
int ieee_finite(double d
);
975 /* ------------ tccpp.c ------------ */
977 ST_DATA
struct BufferedFile
*file
;
980 ST_DATA
const int *macro_ptr
;
981 ST_DATA
int parse_flags
;
982 ST_DATA
int tok_flags
;
983 ST_DATA CString tokcstr
; /* current parsed string, if any */
985 /* display benchmark infos */
986 ST_DATA
int total_lines
;
987 ST_DATA
int total_bytes
;
988 ST_DATA
int tok_ident
;
989 ST_DATA TokenSym
**table_ident
;
991 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
992 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
993 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
994 #define TOK_FLAG_EOF 0x0008 /* end of file */
996 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
997 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
998 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
999 token. line feed is also
1001 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
1002 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1004 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
);
1005 ST_FUNC
char *get_tok_str(int v
, CValue
*cv
);
1006 ST_FUNC
void save_parse_state(ParseState
*s
);
1007 ST_FUNC
void restore_parse_state(ParseState
*s
);
1008 ST_INLN
void tok_str_new(TokenString
*s
);
1009 ST_FUNC
void tok_str_free(int *str
);
1010 ST_FUNC
void tok_str_add(TokenString
*s
, int t
);
1011 ST_FUNC
void tok_str_add_tok(TokenString
*s
);
1012 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
);
1013 ST_FUNC
void define_undef(Sym
*s
);
1014 ST_INLN Sym
*define_find(int v
);
1015 ST_FUNC
void free_defines(Sym
*b
);
1016 ST_FUNC Sym
*label_find(int v
);
1017 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
);
1018 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
);
1019 ST_FUNC
void parse_define(void);
1020 ST_FUNC
void preprocess(int is_bof
);
1021 ST_FUNC
void next_nomacro(void);
1022 ST_FUNC
void next(void);
1023 ST_INLN
void unget_tok(int last_tok
);
1024 ST_FUNC
void preprocess_init(TCCState
*s1
);
1025 ST_FUNC
void preprocess_new();
1026 ST_FUNC
int tcc_preprocess(TCCState
*s1
);
1027 ST_FUNC
void skip(int c
);
1029 /* ------------ tccgen.c ------------ */
1031 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
1032 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
1033 #ifdef CONFIG_TCC_ASM
1034 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
1036 #ifdef CONFIG_TCC_BCHECK
1037 /* bound check related sections */
1038 ST_DATA Section
*bounds_section
; /* contains global data bound description */
1039 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
1041 /* symbol sections */
1042 ST_DATA Section
*symtab_section
, *strtab_section
;
1043 /* debug sections */
1044 ST_DATA Section
*stab_section
, *stabstr_section
;
1046 #define SYM_POOL_NB (8192 / sizeof(Sym))
1047 ST_DATA Sym
*sym_free_first
;
1048 ST_DATA
void **sym_pools
;
1049 ST_DATA
int nb_sym_pools
;
1051 ST_DATA Sym
*global_stack
;
1052 ST_DATA Sym
*local_stack
;
1053 ST_DATA Sym
*local_label_stack
;
1054 ST_DATA Sym
*global_label_stack
;
1055 ST_DATA Sym
*define_stack
;
1056 ST_DATA CType char_pointer_type
, func_old_type
, int_type
;
1057 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
1058 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
1060 ST_DATA
int const_wanted
; /* true if constant wanted */
1061 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
1062 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
1063 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
1064 ST_DATA
int func_vc
;
1065 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
1066 ST_DATA
char *funcname
;
1068 ST_INLN
int is_float(int t
);
1069 ST_FUNC
void test_lvalue(void);
1070 ST_FUNC
void swap(int *p
, int *q
);
1071 ST_FUNC
void vpushi(int v
);
1072 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
);
1073 ST_FUNC
void vset(CType
*type
, int r
, int v
);
1074 ST_FUNC
void vswap(void);
1075 ST_FUNC
void vpush_global_sym(CType
*type
, int v
);
1076 ST_FUNC
void vrott(int n
);
1077 #ifdef TCC_TARGET_ARM
1078 ST_FUNC
int get_reg_ex(int rc
, int rc2
);
1079 ST_FUNC
void vnrott(int n
);
1080 ST_FUNC
void lexpand_nr(void);
1082 ST_FUNC
void vpushv(SValue
*v
);
1083 ST_FUNC
void save_reg(int r
);
1084 ST_FUNC
int get_reg(int rc
);
1085 ST_FUNC
void save_regs(int n
);
1086 ST_FUNC
int gv(int rc
);
1087 ST_FUNC
void gv2(int rc1
, int rc2
);
1088 ST_FUNC
void vpop(void);
1089 ST_FUNC
void gen_op(int op
);
1090 ST_FUNC
int type_size(CType
*type
, int *a
);
1091 ST_FUNC
void mk_pointer(CType
*type
);
1092 ST_FUNC
void vstore(void);
1093 ST_FUNC
void inc(int post
, int c
);
1094 ST_FUNC
void parse_asm_str(CString
*astr
);
1095 ST_FUNC
int lvalue_type(int t
);
1096 ST_FUNC
void indir(void);
1097 ST_FUNC
void unary(void);
1098 ST_FUNC
void expr_prod(void);
1099 ST_FUNC
void expr_sum(void);
1100 ST_FUNC
void gexpr(void);
1101 ST_FUNC
int expr_const(void);
1102 ST_FUNC
void gen_inline_functions(void);
1103 ST_FUNC
void decl(int l
);
1104 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1105 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
);
1108 /* ------------ tccelf.c ------------ */
1110 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1113 unsigned int n_strx
; /* index into string table of name */
1114 unsigned char n_type
; /* type of symbol */
1115 unsigned char n_other
; /* misc info (usually empty) */
1116 unsigned short n_desc
; /* description field */
1117 unsigned int n_value
; /* value of symbol */
1120 ST_FUNC Section
*new_symtab(TCCState
*s1
, const char *symtab_name
, int sh_type
, int sh_flags
, const char *strtab_name
, const char *hash_name
, int hash_sh_flags
);
1122 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
);
1123 ST_FUNC
int put_elf_sym(Section
*s
, uplong value
, unsigned long size
, int info
, int other
, int shndx
, const char *name
);
1124 ST_FUNC
int add_elf_sym(Section
*s
, uplong value
, unsigned long size
, int info
, int other
, int sh_num
, const char *name
);
1125 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
);
1126 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
, int type
, int symbol
);
1128 ST_FUNC
void put_stabs(const char *str
, int type
, int other
, int desc
, unsigned long value
);
1129 ST_FUNC
void put_stabs_r(const char *str
, int type
, int other
, int desc
, unsigned long value
, Section
*sec
, int sym_index
);
1130 ST_FUNC
void put_stabn(int type
, int other
, int desc
, int value
);
1131 ST_FUNC
void put_stabd(int type
, int other
, int desc
);
1133 ST_FUNC
void relocate_common_syms(void);
1134 ST_FUNC
void relocate_syms(TCCState
*s1
, int do_resolve
);
1135 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
);
1137 ST_FUNC
void tcc_add_linker_symbols(TCCState
*s1
);
1138 ST_FUNC
int tcc_load_object_file(TCCState
*s1
, int fd
, unsigned long file_offset
);
1139 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
);
1140 ST_FUNC
void *tcc_get_symbol_err(TCCState
*s
, const char *name
);
1141 ST_FUNC
void tcc_add_bcheck(TCCState
*s1
);
1143 ST_FUNC
void build_got_entries(TCCState
*s1
);
1144 ST_FUNC
void tcc_add_runtime(TCCState
*s1
);
1146 #ifndef TCC_TARGET_PE
1147 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
);
1148 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
);
1149 ST_FUNC
uint8_t *parse_comment(uint8_t *p
);
1150 ST_FUNC
void minp(void);
1151 ST_INLN
void inp(void);
1152 ST_FUNC
int handle_eob(void);
1155 #ifdef TCC_TARGET_X86_64
1156 # define ELFCLASSW ELFCLASS64
1157 # define ElfW(type) Elf##64##_##type
1158 # define ELFW(type) ELF##64##_##type
1159 # define ElfW_Rel ElfW(Rela)
1160 # define SHT_RELX SHT_RELA
1161 # define REL_SECTION_FMT ".rela%s"
1162 /* XXX: DLL with PLT would only work with x86-64 for now */
1163 # define TCC_OUTPUT_DLL_WITH_PLT
1165 # define ELFCLASSW ELFCLASS32
1166 # define ElfW(type) Elf##32##_##type
1167 # define ELFW(type) ELF##32##_##type
1168 # define ElfW_Rel ElfW(Rel)
1169 # define SHT_RELX SHT_REL
1170 # define REL_SECTION_FMT ".rel%s"
1173 /* ------------ xxx-gen.c ------------ */
1175 ST_FUNC
void gsym_addr(int t
, int a
);
1176 ST_FUNC
void gsym(int t
);
1177 ST_FUNC
void load(int r
, SValue
*sv
);
1178 ST_FUNC
void store(int r
, SValue
*v
);
1179 ST_FUNC
void gfunc_call(int nb_args
);
1180 ST_FUNC
void gfunc_prolog(CType
*func_type
);
1181 ST_FUNC
void gfunc_epilog(void);
1182 ST_FUNC
int gjmp(int t
);
1183 ST_FUNC
void gjmp_addr(int a
);
1184 ST_FUNC
int gtst(int inv
, int t
);
1185 ST_FUNC
void gen_opi(int op
);
1186 ST_FUNC
void gen_opf(int op
);
1187 ST_FUNC
void gen_cvt_ftoi(int t
);
1188 ST_FUNC
void gen_cvt_ftof(int t
);
1189 ST_FUNC
void ggoto(void);
1190 #ifndef TCC_TARGET_C67
1191 ST_FUNC
void o(unsigned int c
);
1193 #ifndef TCC_TARGET_ARM
1194 ST_FUNC
void gen_cvt_itof(int t
);
1197 /* ------------ i386-gen.c ------------ */
1198 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1199 ST_FUNC
void g(int c
);
1200 ST_FUNC
int oad(int c
, int s
);
1201 ST_FUNC
void gen_le16(int c
);
1202 ST_FUNC
void gen_le32(int c
);
1203 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
);
1204 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
);
1207 #ifdef CONFIG_TCC_BCHECK
1208 ST_FUNC
void gen_bounded_ptr_add(void);
1209 ST_FUNC
void gen_bounded_ptr_deref(void);
1212 /* ------------ x86_64-gen.c ------------ */
1213 #ifdef TCC_TARGET_X86_64
1214 ST_FUNC
void gen_addr64(int r
, Sym
*sym
, int64_t c
);
1217 /* ------------ arm-gen.c ------------ */
1218 #ifdef TCC_TARGET_ARM
1219 ST_FUNC
uint32_t encbranch(int pos
, int addr
, int fail
);
1220 ST_FUNC
void gen_cvt_itof1(int t
);
1223 /* ------------ c67-gen.c ------------ */
1224 #ifdef TCC_TARGET_C67
1227 /* ------------ tcccoff.c ------------ */
1229 #ifdef TCC_TARGET_COFF
1230 ST_FUNC
int tcc_output_coff(TCCState
*s1
, FILE *f
);
1231 ST_FUNC
int tcc_load_coff(TCCState
* s1
, int fd
);
1234 /* ------------ tccasm.c ------------ */
1235 ST_FUNC
void asm_instr(void);
1236 ST_FUNC
void asm_global_instr(void);
1238 #ifdef CONFIG_TCC_ASM
1239 ST_FUNC
int find_constraint(ASMOperand
*operands
, int nb_operands
, const char *name
, const char **pp
);
1240 ST_FUNC
void asm_expr(TCCState
*s1
, ExprValue
*pe
);
1241 ST_FUNC
int asm_int_expr(TCCState
*s1
);
1242 ST_FUNC
int tcc_assemble(TCCState
*s1
, int do_preprocess
);
1243 /* ------------ i386-asm.c ------------ */
1244 ST_FUNC
void gen_expr32(ExprValue
*pe
);
1245 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
);
1246 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, const uint8_t *clobber_regs
, int *pout_reg
);
1247 ST_FUNC
void subst_asm_operand(CString
*add_str
, SValue
*sv
, int modifier
);
1248 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, int is_output
, uint8_t *clobber_regs
, int out_reg
);
1249 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
);
1251 /* ------------ tccpe.c -------------- */
1252 #ifdef TCC_TARGET_PE
1253 ST_FUNC
int pe_load_file(struct TCCState
*s1
, const char *filename
, int fd
);
1254 ST_FUNC
int pe_add_dll(struct TCCState
*s
, const char *libraryname
);
1255 ST_FUNC
int pe_output_file(TCCState
* s1
, const char *filename
);
1256 ST_FUNC
int pe_putimport(TCCState
*s1
, int dllindex
, const char *name
, const void *value
);
1257 ST_FUNC SValue
*pe_getimport(SValue
*sv
, SValue
*v2
);
1259 ST_FUNC
char *get_export_names(FILE *fp
);
1260 #ifdef TCC_TARGET_X86_64
1261 ST_FUNC
void pe_add_unwind_data(unsigned start
, unsigned end
, unsigned stack
);
1265 /* ------------ tccrun.c ----------------- */
1266 #ifdef CONFIG_TCC_STATIC
1267 #define RTLD_LAZY 0x001
1268 #define RTLD_NOW 0x002
1269 #define RTLD_GLOBAL 0x100
1270 #define RTLD_DEFAULT NULL
1271 /* dummy function for profiling */
1272 ST_FUNC
void *dlopen(const char *filename
, int flag
);
1273 ST_FUNC
void dlclose(void *p
);
1274 //ST_FUNC const char *dlerror(void);
1275 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1276 #elif !defined TCC_TARGET_PE || !defined _WIN32
1277 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1279 /********************************************************/
1280 /* include the target specific definitions */
1282 #define TARGET_DEFS_ONLY
1283 #ifdef TCC_TARGET_I386
1284 #include "i386-gen.c"
1286 #ifdef TCC_TARGET_X86_64
1287 #include "x86_64-gen.c"
1289 #ifdef TCC_TARGET_ARM
1290 #include "arm-gen.c"
1292 #ifdef TCC_TARGET_C67
1294 #include "c67-gen.c"
1296 #undef TARGET_DEFS_ONLY
1298 ST_DATA
const int reg_classes
[NB_REGS
];
1300 /********************************************************/
1303 #define ST_DATA static
1307 /********************************************************/