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
47 #include <sys/timeb.h>
48 #include <io.h> /* open, close etc. */
49 #include <direct.h> /* getcwd */
50 #define inline __inline
53 #define uplong unsigned long long
60 #include <sys/ucontext.h>
65 #endif /* !CONFIG_TCCBOOT */
68 #define uplong unsigned long
83 #define SA_SIGINFO 0x00000004u
90 /* preprocessor debug */
92 /* include file debug */
100 /* target selection */
101 //#define TCC_TARGET_I386 /* i386 code generator */
102 //#define TCC_TARGET_ARM /* ARMv4 code generator */
103 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
104 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
106 /* default target is I386 */
107 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
108 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
109 #define TCC_TARGET_I386
112 #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
113 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
114 #define CONFIG_TCC_BCHECK /* enable bound checking code */
117 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
118 #define CONFIG_TCC_STATIC
121 /* define it to include assembler support */
122 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
123 #define CONFIG_TCC_ASM
126 /* object format selection */
127 #if defined(TCC_TARGET_C67)
128 #define TCC_TARGET_COFF
131 #if !defined(CONFIG_TCCBOOT)
132 #define CONFIG_TCC_BACKTRACE
141 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
142 executables or dlls */
144 #if defined(TCC_TARGET_X86_64_CENTOS)
145 # define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib64"
146 # define CONFIG_TCC_LDDIR "/lib64"
148 # define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
149 # define CONFIG_TCC_LDDIR "/lib"
152 #define INCLUDE_STACK_SIZE 32
153 #define IFDEF_STACK_SIZE 64
154 #define VSTACK_SIZE 256
155 #define STRING_MAX_SIZE 1024
156 #define PACK_STACK_SIZE 8
158 #define TOK_HASH_SIZE 8192 /* must be a power of two */
159 #define TOK_ALLOC_INCR 512 /* must be a power of two */
160 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
162 /* token symbol management */
163 typedef struct TokenSym
{
164 struct TokenSym
*hash_next
;
165 struct Sym
*sym_define
; /* direct pointer to define */
166 struct Sym
*sym_label
; /* direct pointer to label */
167 struct Sym
*sym_struct
; /* direct pointer to structure */
168 struct Sym
*sym_identifier
; /* direct pointer to identifier */
169 int tok
; /* token number */
175 typedef unsigned short nwchar_t
;
177 typedef int nwchar_t
;
180 typedef struct CString
{
181 int size
; /* size in bytes */
182 void *data
; /* either 'char *' or 'nwchar_t *' */
184 void *data_allocated
; /* if non NULL, data has been malloced */
187 /* type definition */
188 typedef struct CType
{
194 typedef union CValue
{
200 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
202 unsigned long long ull
;
203 struct CString
*cstr
;
209 typedef struct SValue
{
210 CType type
; /* type */
211 unsigned short r
; /* register + flags */
212 unsigned short r2
; /* second register, used for 'long long'
213 type. If not used, set to VT_CONST */
214 CValue c
; /* constant, if VT_CONST */
215 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
218 /* symbol management */
220 int v
; /* symbol token */
221 int a
; /* asm symbol token */
222 long r
; /* associated register */
224 long c
; /* associated number */
225 int *d
; /* define token stream */
227 CType type
; /* associated type */
229 struct Sym
*next
; /* next related symbol */
230 long jnext
; /* next jump label */
232 struct Sym
*prev
; /* prev symbol in stack */
233 struct Sym
*prev_tok
; /* previous symbol for this token */
236 /* section definition */
237 /* XXX: use directly ELF structure for parameters ? */
238 /* special flag to indicate that the section should not be linked to
240 #define SHF_PRIVATE 0x80000000
242 /* special flag, too */
243 #define SECTION_ABS ((void *)1)
245 typedef struct Section
{
246 unsigned long data_offset
; /* current data offset */
247 unsigned char *data
; /* section data */
248 unsigned long data_allocated
; /* used for realloc() handling */
249 int sh_name
; /* elf section name (only used during output) */
250 int sh_num
; /* elf section number */
251 int sh_type
; /* elf section type */
252 int sh_flags
; /* elf section flags */
253 int sh_info
; /* elf section info */
254 int sh_addralign
; /* elf section alignment */
255 int sh_entsize
; /* elf entry size */
256 unsigned long sh_size
; /* section size (only used during output) */
257 unsigned long sh_addr
; /* address at which the section is relocated */
258 unsigned long sh_offset
; /* file offset */
259 int nb_hashed_syms
; /* used to resize the hash table */
260 struct Section
*link
; /* link to another section */
261 struct Section
*reloc
; /* corresponding section for relocation, if any */
262 struct Section
*hash
; /* hash table for symbols */
263 struct Section
*next
;
264 char name
[1]; /* section name */
267 typedef struct DLLReference
{
273 /* GNUC attribute definition */
274 typedef struct AttributeDef
{
276 func_call
: 3, /* calling convention (0..5), see below */
277 aligned
: 5, /* alignement (0..16) */
285 struct Section
*section
;
288 /* gr: wrappers for casting sym->r for other purposes */
289 #define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call)
290 #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
291 #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
292 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
293 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
294 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
295 #define FUNC_WEAK(r) (((AttributeDef*)&(r))->weak)
296 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
297 #define INT_ATTR(ad) (*(int*)(ad))
299 /* -------------------------------------------------- */
301 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
302 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
303 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
305 /* stored in 'Sym.c' field */
306 #define FUNC_NEW 1 /* ansi function prototype */
307 #define FUNC_OLD 2 /* old function prototype */
308 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
310 /* stored in 'Sym.r' field */
311 #define FUNC_CDECL 0 /* standard c call */
312 #define FUNC_STDCALL 1 /* pascal c call */
313 #define FUNC_FASTCALL1 2 /* first param in %eax */
314 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
315 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
316 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
318 /* field 'Sym.t' for macros */
319 #define MACRO_OBJ 0 /* object like macro */
320 #define MACRO_FUNC 1 /* function like macro */
322 /* field 'Sym.r' for C labels */
323 #define LABEL_DEFINED 0 /* label is defined */
324 #define LABEL_FORWARD 1 /* label is forward defined */
325 #define LABEL_DECLARED 2 /* label is declared but never used */
327 /* type_decl() types */
328 #define TYPE_ABSTRACT 1 /* type without variable */
329 #define TYPE_DIRECT 2 /* type with variable */
331 #define IO_BUF_SIZE 8192
333 typedef struct BufferedFile
{
337 struct BufferedFile
*prev
;
338 int line_num
; /* current line number - here to simplify code */
339 int ifndef_macro
; /* #ifndef macro / #endif search */
340 int ifndef_macro_saved
; /* saved ifndef_macro */
341 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
342 char inc_type
; /* type of include */
343 char inc_filename
[512]; /* filename specified by the user */
344 char filename
[1024]; /* current filename - here to simplify code */
345 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
348 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
349 #define CH_EOF (-1) /* end of file */
351 /* parsing state (used to save parser state to reparse part of the
352 source several times) */
353 typedef struct ParseState
{
354 const int *macro_ptr
;
360 /* used to record tokens */
361 typedef struct TokenString
{
368 /* inline functions */
369 typedef struct InlineFunc
{
375 /* include file cache, used to find files faster and also to eliminate
376 inclusion if the include file is protected by #ifndef ... #endif */
377 typedef struct CachedInclude
{
379 int hash_next
; /* -1 if none */
380 char type
; /* '"' or '>' to give include type */
381 char filename
[1]; /* path specified in #include */
384 #define CACHED_INCLUDES_HASH_SIZE 512
386 #ifdef CONFIG_TCC_ASM
387 typedef struct ExprValue
{
392 #define MAX_ASM_OPERANDS 30
393 typedef struct ASMOperand
{
394 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
396 char asm_str
[16]; /* computed asm string for operand */
397 SValue
*vt
; /* C value of the expression */
398 int ref_index
; /* if >= 0, gives reference to a output constraint */
399 int input_index
; /* if >= 0, gives reference to an input constraint */
400 int priority
; /* priority, used to assign registers */
401 int reg
; /* if >= 0, register number used for this operand */
402 int is_llong
; /* true if double register value */
403 int is_memory
; /* true if memory operand */
404 int is_rw
; /* for '+' modifier */
409 unsigned output_type
: 8;
410 unsigned reloc_output
: 1;
412 BufferedFile
**include_stack_ptr
;
413 int *ifdef_stack_ptr
;
415 /* include file handling */
416 char **include_paths
;
417 int nb_include_paths
;
418 char **sysinclude_paths
;
419 int nb_sysinclude_paths
;
420 CachedInclude
**cached_includes
;
421 int nb_cached_includes
;
423 char **library_paths
;
424 int nb_library_paths
;
426 /* array of all loaded dlls (including those referenced by loaded
428 DLLReference
**loaded_dlls
;
433 int nb_sections
; /* number of sections, including first dummy section */
435 Section
**priv_sections
;
436 int nb_priv_sections
; /* number of private sections */
441 unsigned long *got_offsets
;
443 /* give the correspondance from symtab indexes to dynsym indexes */
444 int *symtab_to_dynsym
;
446 /* temporary dynamic symbol sections (for dll loading) */
447 Section
*dynsymtab_section
;
448 /* exported dynamic symbol section */
451 int nostdinc
; /* if true, no standard headers are added */
452 int nostdlib
; /* if true, no standard libraries are added */
453 int nocommon
; /* if true, do not use common symbols for .bss data */
455 /* if true, static linking is performed */
458 /* soname as specified on the command line (-soname) */
460 /* rpath as specified on the command line (-Wl,-rpath=) */
463 /* if true, all symbols are exported */
466 /* if true, resolve symbols in the current module first (-Wl,Bsymbolic) */
469 /* if true, only link in referenced objects from archive */
472 /* address of text section */
473 unsigned long text_addr
;
476 /* symbols to call at load-time / unload-time */
477 const char *init_symbol
;
478 const char *fini_symbol
;
480 /* output format, see TCC_OUTPUT_FORMAT_xxx */
483 /* C language options */
484 int char_is_unsigned
;
485 int leading_underscore
;
487 /* warning switches */
488 int warn_write_strings
;
489 int warn_unsupported
;
492 int warn_implicit_function_declaration
;
494 /* display some information during compilation */
496 /* compile with debug symbol (and use them if error during execution) */
498 #ifdef CONFIG_TCC_BCHECK
499 /* compile with built-in memory and bounds checker */
502 /* give the path of the tcc libraries */
507 void (*error_func
)(void *opaque
, const char *msg
);
508 int error_set_jmp_enabled
;
509 jmp_buf error_jmp_buf
;
512 /* tiny assembler state */
515 /* see include_stack_ptr */
516 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
518 /* see ifdef_stack_ptr */
519 int ifdef_stack
[IFDEF_STACK_SIZE
];
521 /* see cached_includes */
522 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
525 int pack_stack
[PACK_STACK_SIZE
];
528 /* output file for preprocessing */
531 /* input files and libraries for this compilation */
537 /* automatically collected dependencies for this compilation */
541 /* for tcc_relocate */
546 unsigned long mem_size
;
549 struct InlineFunc
**inline_fns
;
552 #ifdef TCC_TARGET_I386
556 /* section alignment */
557 unsigned long section_align
;
562 unsigned long pe_file_align
;
565 int sym_1
, sym_2
, offs_1
;
569 #ifndef TCC_TARGET_PE
570 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM
571 /* write PLT and GOT here */
572 char *runtime_plt_and_got
;
573 unsigned int runtime_plt_and_got_offset
;
578 /* The current value can be: */
579 #define VT_VALMASK 0x00ff
580 #define VT_CONST 0x00f0 /* constant in vc
581 (must be first non register value) */
582 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
583 #define VT_LOCAL 0x00f2 /* offset on stack */
584 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
585 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
586 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
587 #define VT_LVAL 0x0100 /* var is an lvalue */
588 #define VT_SYM 0x0200 /* a symbol value is added */
589 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
590 char/short stored in integer registers) */
591 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
592 dereferencing value */
593 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
594 bounding function call point is in vc */
595 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
596 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
597 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
598 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
601 #define VT_INT 0 /* integer type */
602 #define VT_BYTE 1 /* signed byte type */
603 #define VT_SHORT 2 /* short type */
604 #define VT_VOID 3 /* void type */
605 #define VT_PTR 4 /* pointer */
606 #define VT_ENUM 5 /* enum definition */
607 #define VT_FUNC 6 /* function type */
608 #define VT_STRUCT 7 /* struct/union definition */
609 #define VT_FLOAT 8 /* IEEE float */
610 #define VT_DOUBLE 9 /* IEEE double */
611 #define VT_LDOUBLE 10 /* IEEE long double */
612 #define VT_BOOL 11 /* ISOC99 boolean type */
613 #define VT_LLONG 12 /* 64 bit integer */
614 #define VT_LONG 13 /* long integer (NEVER USED as type, only
616 #define VT_BTYPE 0x000f /* mask for basic type */
617 #define VT_UNSIGNED 0x0010 /* unsigned type */
618 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
619 #define VT_BITFIELD 0x0040 /* bitfield modifier */
620 #define VT_CONSTANT 0x0800 /* const modifier */
621 #define VT_VOLATILE 0x1000 /* volatile modifier */
622 #define VT_SIGNED 0x2000 /* signed type */
625 #define VT_EXTERN 0x00000080 /* extern definition */
626 #define VT_STATIC 0x00000100 /* static variable */
627 #define VT_TYPEDEF 0x00000200 /* typedef definition */
628 #define VT_INLINE 0x00000400 /* inline definition */
629 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
630 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
632 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
634 /* type mask (except storage) */
635 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT)
636 #define VT_TYPE (~(VT_STORAGE))
640 /* warning: the following compare tokens depend on i386 asm code */
647 #define TOK_Nset 0x98
648 #define TOK_Nclear 0x99
654 #define TOK_LAND 0xa0
658 #define TOK_MID 0xa3 /* inc/dec, to void constant */
660 #define TOK_UDIV 0xb0 /* unsigned division */
661 #define TOK_UMOD 0xb1 /* unsigned modulo */
662 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
663 #define TOK_CINT 0xb3 /* number in tokc */
664 #define TOK_CCHAR 0xb4 /* char constant in tokc */
665 #define TOK_STR 0xb5 /* pointer to string in tokc */
666 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
667 #define TOK_LCHAR 0xb7
668 #define TOK_LSTR 0xb8
669 #define TOK_CFLOAT 0xb9 /* float constant */
670 #define TOK_LINENUM 0xba /* line number info */
671 #define TOK_CDOUBLE 0xc0 /* double constant */
672 #define TOK_CLDOUBLE 0xc1 /* long double constant */
673 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
674 #define TOK_ADDC1 0xc3 /* add with carry generation */
675 #define TOK_ADDC2 0xc4 /* add with carry use */
676 #define TOK_SUBC1 0xc5 /* add with carry generation */
677 #define TOK_SUBC2 0xc6 /* add with carry use */
678 #define TOK_CUINT 0xc8 /* unsigned int constant */
679 #define TOK_CLLONG 0xc9 /* long long constant */
680 #define TOK_CULLONG 0xca /* unsigned long long constant */
681 #define TOK_ARROW 0xcb
682 #define TOK_DOTS 0xcc /* three dots */
683 #define TOK_SHR 0xcd /* unsigned shift right */
684 #define TOK_PPNUM 0xce /* preprocessor number */
686 #define TOK_SHL 0x01 /* shift left */
687 #define TOK_SAR 0x02 /* signed shift right */
689 /* assignement operators : normal operator or 0x80 */
690 #define TOK_A_MOD 0xa5
691 #define TOK_A_AND 0xa6
692 #define TOK_A_MUL 0xaa
693 #define TOK_A_ADD 0xab
694 #define TOK_A_SUB 0xad
695 #define TOK_A_DIV 0xaf
696 #define TOK_A_XOR 0xde
697 #define TOK_A_OR 0xfc
698 #define TOK_A_SHL 0x81
699 #define TOK_A_SAR 0x82
702 #define offsetof(type, field) ((size_t) &((type *)0)->field)
706 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
709 #define TOK_EOF (-1) /* end of file */
710 #define TOK_LINEFEED 10 /* line feed */
712 /* all identificators and strings have token above that */
713 #define TOK_IDENT 256
715 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
716 #define TOK_ASM_int TOK_INT
718 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
719 /* only used for i386 asm opcodes definitions */
721 DEF(TOK_ASM_ ## x ## b, #x "b") \
722 DEF(TOK_ASM_ ## x ## w, #x "w") \
723 DEF(TOK_ASM_ ## x ## l, #x "l") \
724 DEF(TOK_ASM_ ## x, #x)
726 DEF(TOK_ASM_ ## x ## w, #x "w") \
727 DEF(TOK_ASM_ ## x ## l, #x "l") \
728 DEF(TOK_ASM_ ## x, #x)
729 #ifdef TCC_TARGET_X86_64
730 # define DEF_BWLQ(x) \
731 DEF(TOK_ASM_ ## x ## b, #x "b") \
732 DEF(TOK_ASM_ ## x ## w, #x "w") \
733 DEF(TOK_ASM_ ## x ## l, #x "l") \
734 DEF(TOK_ASM_ ## x ## q, #x "q") \
735 DEF(TOK_ASM_ ## x, #x)
736 # define DEF_WLQ(x) \
737 DEF(TOK_ASM_ ## x ## w, #x "w") \
738 DEF(TOK_ASM_ ## x ## l, #x "l") \
739 DEF(TOK_ASM_ ## x ## q, #x "q") \
740 DEF(TOK_ASM_ ## x, #x)
741 # define DEF_BWLX DEF_BWLQ
742 # define DEF_WLX DEF_WLQ
743 /* number of sizes + 1 */
746 # define DEF_BWLX DEF_BWL
747 # define DEF_WLX DEF_WL
748 /* number of sizes + 1 */
753 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
754 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
755 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
756 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
759 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
760 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
763 #define DEF_ASMTEST(x) \
795 #endif // defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
798 TOK_LAST
= TOK_IDENT
- 1,
799 #define DEF(id, str) id,
804 #define TOK_UIDENT TOK_DEFINE
807 #define snprintf _snprintf
808 #define vsnprintf _vsnprintf
810 #define strtold (long double)strtod
811 #define strtof (float)strtod
812 #define strtoll (long long)strtol
814 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) \
815 || defined(__FreeBSD_kernel__) || defined(__DragonFly__) \
816 || defined(__OpenBSD__)
817 /* currently incorrect */
818 static inline long double strtold(const char *nptr
, char **endptr
)
820 return (long double)strtod(nptr
, endptr
);
822 static inline float strtof(const char *nptr
, char **endptr
)
824 return (float)strtod(nptr
, endptr
);
827 /* XXX: need to define this to use them in non ISOC99 context */
828 extern float strtof (const char *__nptr
, char **__endptr
);
829 extern long double strtold (const char *__nptr
, char **__endptr
);
833 #define IS_PATHSEP(c) (c == '/' || c == '\\')
834 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
835 #define PATHCMP stricmp
837 #define IS_PATHSEP(c) (c == '/')
838 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
839 #define PATHCMP strcmp
842 /* space exlcuding newline */
843 static inline int is_space(int ch
)
845 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
848 static inline int isid(int c
)
850 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') || c
== '_';
853 static inline int isnum(int c
)
855 return c
>= '0' && c
<= '9';
858 static inline int isoct(int c
)
860 return c
>= '0' && c
<= '7';
863 static inline int toup(int c
)
865 return (c
>= 'a' && c
<= 'z') ? c
- 'a' + 'A' : c
;
871 #define ST_INLN static inline
872 #define ST_FUNC static
873 #define ST_DATA static
877 #define ST_DATA extern
880 /* ------------ libtcc.c ------------ */
882 /* use GNU C extensions */
884 /* use Tiny C extensions */
886 /* XXX: get rid of this ASAP */
887 ST_DATA
struct TCCState
*tcc_state
;
889 #ifdef CONFIG_TCC_BACKTRACE
890 ST_DATA
int num_callers
;
891 ST_DATA
const char **rt_bound_error_msg
;
892 ST_DATA
void *rt_prog_main
;
895 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
896 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
897 #define AFF_PREPROCESS 0x0004 /* preprocess file */
899 /* public functions currently used by the tcc main function */
900 PUB_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
);
901 PUB_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
);
902 PUB_FUNC
char *tcc_basename(const char *name
);
903 PUB_FUNC
char *tcc_fileextension (const char *name
);
904 PUB_FUNC
void tcc_free(void *ptr
);
905 PUB_FUNC
void *tcc_malloc(unsigned long size
);
906 PUB_FUNC
void *tcc_mallocz(unsigned long size
);
907 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
);
908 PUB_FUNC
char *tcc_strdup(const char *str
);
909 #define free(p) use_tcc_free(p)
910 #define malloc(s) use_tcc_malloc(s)
911 #define realloc(p, s) use_tcc_realloc(p, s)
913 #define strdup(s) use_tcc_strdup(s)
914 PUB_FUNC
void tcc_memstats(void);
915 PUB_FUNC
void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
916 PUB_FUNC
void dynarray_reset(void *pp
, int *n
);
917 PUB_FUNC
void error_noabort(const char *fmt
, ...);
918 PUB_FUNC
void error(const char *fmt
, ...);
919 PUB_FUNC
void expect(const char *msg
);
920 PUB_FUNC
void warning(const char *fmt
, ...);
922 /* other utilities */
923 ST_INLN
void cstr_ccat(CString
*cstr
, int ch
);
924 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
);
925 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
);
926 ST_FUNC
void cstr_new(CString
*cstr
);
927 ST_FUNC
void cstr_free(CString
*cstr
);
928 ST_FUNC
void add_char(CString
*cstr
, int c
);
929 #define cstr_reset(cstr) cstr_free(cstr)
931 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
);
932 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
);
933 ST_FUNC
void *section_ptr_add(Section
*sec
, unsigned long size
);
934 ST_FUNC
void section_reserve(Section
*sec
, unsigned long size
);
935 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
);
937 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
, unsigned long value
, unsigned long size
, int can_add_underscore
);
938 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
, unsigned long value
, unsigned long size
);
939 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
);
941 ST_INLN
void sym_free(Sym
*sym
);
942 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
);
943 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
);
944 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
945 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
);
946 ST_INLN Sym
*struct_find(int v
);
947 ST_INLN Sym
*sym_find(int v
);
948 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
);
950 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
);
951 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
);
952 ST_FUNC
void tcc_close(void);
954 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
);
955 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
956 PUB_FUNC
int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
);
957 PUB_FUNC
void tcc_print_stats(TCCState
*s
, int64_t total_time
);
958 PUB_FUNC
void set_num_callers(int n
);
960 ST_FUNC
int ieee_finite(double d
);
962 /* ------------ tccpp.c ------------ */
964 ST_DATA
struct BufferedFile
*file
;
967 ST_DATA
const int *macro_ptr
;
968 ST_DATA
int parse_flags
;
969 ST_DATA
int tok_flags
;
970 ST_DATA CString tokcstr
; /* current parsed string, if any */
972 /* display benchmark infos */
973 ST_DATA
int total_lines
;
974 ST_DATA
int total_bytes
;
975 ST_DATA
int tok_ident
;
976 ST_DATA TokenSym
**table_ident
;
978 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
979 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
980 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
981 #define TOK_FLAG_EOF 0x0008 /* end of file */
983 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
984 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
985 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
986 token. line feed is also
988 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
989 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
991 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
);
992 ST_FUNC
char *get_tok_str(int v
, CValue
*cv
);
993 ST_FUNC
void save_parse_state(ParseState
*s
);
994 ST_FUNC
void restore_parse_state(ParseState
*s
);
995 ST_INLN
void tok_str_new(TokenString
*s
);
996 ST_FUNC
void tok_str_free(int *str
);
997 ST_FUNC
void tok_str_add(TokenString
*s
, int t
);
998 ST_FUNC
void tok_str_add_tok(TokenString
*s
);
999 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
);
1000 ST_FUNC
void define_undef(Sym
*s
);
1001 ST_INLN Sym
*define_find(int v
);
1002 ST_FUNC
void free_defines(Sym
*b
);
1003 ST_FUNC Sym
*label_find(int v
);
1004 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
);
1005 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
);
1006 ST_FUNC
void parse_define(void);
1007 ST_FUNC
void preprocess(int is_bof
);
1008 ST_FUNC
void next_nomacro(void);
1009 ST_FUNC
void next(void);
1010 ST_INLN
void unget_tok(int last_tok
);
1011 ST_FUNC
void preprocess_init(TCCState
*s1
);
1012 ST_FUNC
void preprocess_new();
1013 ST_FUNC
int tcc_preprocess(TCCState
*s1
);
1014 ST_FUNC
void skip(int c
);
1016 /* ------------ tccgen.c ------------ */
1018 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
1019 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
1020 #ifdef CONFIG_TCC_ASM
1021 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
1023 #ifdef CONFIG_TCC_BCHECK
1024 /* bound check related sections */
1025 ST_DATA Section
*bounds_section
; /* contains global data bound description */
1026 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
1028 /* symbol sections */
1029 ST_DATA Section
*symtab_section
, *strtab_section
;
1030 /* debug sections */
1031 ST_DATA Section
*stab_section
, *stabstr_section
;
1033 #define SYM_POOL_NB (8192 / sizeof(Sym))
1034 ST_DATA Sym
*sym_free_first
;
1035 ST_DATA
void **sym_pools
;
1036 ST_DATA
int nb_sym_pools
;
1038 ST_DATA Sym
*global_stack
;
1039 ST_DATA Sym
*local_stack
;
1040 ST_DATA Sym
*local_label_stack
;
1041 ST_DATA Sym
*global_label_stack
;
1042 ST_DATA Sym
*define_stack
;
1043 ST_DATA CType char_pointer_type
, func_old_type
, int_type
;
1044 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
1045 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
1047 ST_DATA
int const_wanted
; /* true if constant wanted */
1048 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
1049 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
1050 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
1051 ST_DATA
int func_vc
;
1052 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
1053 ST_DATA
char *funcname
;
1055 ST_INLN
int is_float(int t
);
1056 ST_FUNC
void test_lvalue(void);
1057 ST_FUNC
void swap(int *p
, int *q
);
1058 ST_FUNC
void vpushi(int v
);
1059 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
);
1060 ST_FUNC
void vset(CType
*type
, int r
, int v
);
1061 ST_FUNC
void vswap(void);
1062 ST_FUNC
void vpush_global_sym(CType
*type
, int v
);
1063 ST_FUNC
void vrott(int n
);
1064 #ifdef TCC_TARGET_ARM
1065 ST_FUNC
int get_reg_ex(int rc
, int rc2
);
1066 ST_FUNC
void vnrott(int n
);
1067 ST_FUNC
void lexpand_nr(void);
1069 ST_FUNC
void vpushv(SValue
*v
);
1070 ST_FUNC
void save_reg(int r
);
1071 ST_FUNC
int get_reg(int rc
);
1072 ST_FUNC
void save_regs(int n
);
1073 ST_FUNC
int gv(int rc
);
1074 ST_FUNC
void gv2(int rc1
, int rc2
);
1075 ST_FUNC
void vpop(void);
1076 ST_FUNC
void gen_op(int op
);
1077 ST_FUNC
int type_size(CType
*type
, int *a
);
1078 ST_FUNC
void mk_pointer(CType
*type
);
1079 ST_FUNC
void vstore(void);
1080 ST_FUNC
void inc(int post
, int c
);
1081 ST_FUNC
void parse_asm_str(CString
*astr
);
1082 ST_FUNC
int lvalue_type(int t
);
1083 ST_FUNC
void indir(void);
1084 ST_FUNC
void unary(void);
1085 ST_FUNC
void expr_prod(void);
1086 ST_FUNC
void expr_sum(void);
1087 ST_FUNC
void gexpr(void);
1088 ST_FUNC
int expr_const(void);
1089 ST_FUNC
void gen_inline_functions(void);
1090 ST_FUNC
void decl(int l
);
1091 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1092 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
);
1095 /* ------------ tccelf.c ------------ */
1097 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1100 unsigned int n_strx
; /* index into string table of name */
1101 unsigned char n_type
; /* type of symbol */
1102 unsigned char n_other
; /* misc info (usually empty) */
1103 unsigned short n_desc
; /* description field */
1104 unsigned int n_value
; /* value of symbol */
1107 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
);
1109 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
);
1110 ST_FUNC
int put_elf_sym(Section
*s
, uplong value
, unsigned long size
, int info
, int other
, int shndx
, const char *name
);
1111 ST_FUNC
int add_elf_sym(Section
*s
, uplong value
, unsigned long size
, int info
, int other
, int sh_num
, const char *name
);
1112 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
);
1113 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
, int type
, int symbol
);
1115 ST_FUNC
void put_stabs(const char *str
, int type
, int other
, int desc
, unsigned long value
);
1116 ST_FUNC
void put_stabs_r(const char *str
, int type
, int other
, int desc
, unsigned long value
, Section
*sec
, int sym_index
);
1117 ST_FUNC
void put_stabn(int type
, int other
, int desc
, int value
);
1118 ST_FUNC
void put_stabd(int type
, int other
, int desc
);
1120 ST_FUNC
void relocate_common_syms(void);
1121 ST_FUNC
void relocate_syms(TCCState
*s1
, int do_resolve
);
1122 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
);
1124 ST_FUNC
void tcc_add_linker_symbols(TCCState
*s1
);
1125 ST_FUNC
int tcc_load_object_file(TCCState
*s1
, int fd
, unsigned long file_offset
);
1126 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
);
1127 ST_FUNC
void *tcc_get_symbol_err(TCCState
*s
, const char *name
);
1128 ST_FUNC
void tcc_add_bcheck(TCCState
*s1
);
1130 ST_FUNC
void build_got_entries(TCCState
*s1
);
1131 ST_FUNC
void tcc_add_runtime(TCCState
*s1
);
1133 #ifndef TCC_TARGET_PE
1134 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
);
1135 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
);
1136 ST_FUNC
uint8_t *parse_comment(uint8_t *p
);
1137 ST_FUNC
void minp(void);
1138 ST_INLN
void inp(void);
1139 ST_FUNC
int handle_eob(void);
1142 /* ------------ xxx-gen.c ------------ */
1144 ST_FUNC
void gsym_addr(int t
, int a
);
1145 ST_FUNC
void gsym(int t
);
1146 ST_FUNC
void load(int r
, SValue
*sv
);
1147 ST_FUNC
void store(int r
, SValue
*v
);
1148 ST_FUNC
void gfunc_call(int nb_args
);
1149 ST_FUNC
void gfunc_prolog(CType
*func_type
);
1150 ST_FUNC
void gfunc_epilog(void);
1151 ST_FUNC
int gjmp(int t
);
1152 ST_FUNC
void gjmp_addr(int a
);
1153 ST_FUNC
int gtst(int inv
, int t
);
1154 ST_FUNC
void gen_opi(int op
);
1155 ST_FUNC
void gen_opf(int op
);
1156 ST_FUNC
void gen_cvt_ftoi(int t
);
1157 ST_FUNC
void gen_cvt_ftof(int t
);
1158 ST_FUNC
void ggoto(void);
1159 #ifndef TCC_TARGET_C67
1160 ST_FUNC
void o(unsigned int c
);
1162 #ifndef TCC_TARGET_ARM
1163 ST_FUNC
void gen_cvt_itof(int t
);
1166 /* ------------ i386-gen.c ------------ */
1167 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1168 ST_FUNC
void g(int c
);
1169 ST_FUNC
int oad(int c
, int s
);
1170 ST_FUNC
void gen_le16(int c
);
1171 ST_FUNC
void gen_le32(int c
);
1172 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
);
1173 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
);
1176 #ifdef CONFIG_TCC_BCHECK
1177 ST_FUNC
void gen_bounded_ptr_add(void);
1178 ST_FUNC
void gen_bounded_ptr_deref(void);
1181 /* ------------ x86_64-gen.c ------------ */
1182 #ifdef TCC_TARGET_X86_64
1183 ST_FUNC
void gen_addr64(int r
, Sym
*sym
, int64_t c
);
1186 /* ------------ arm-gen.c ------------ */
1187 #ifdef TCC_TARGET_ARM
1188 ST_FUNC
uint32_t encbranch(int pos
, int addr
, int fail
);
1189 ST_FUNC
void gen_cvt_itof1(int t
);
1192 /* ------------ c67-gen.c ------------ */
1193 #ifdef TCC_TARGET_C67
1196 /* ------------ tcccoff.c ------------ */
1198 #ifdef TCC_TARGET_COFF
1199 ST_FUNC
int tcc_output_coff(TCCState
*s1
, FILE *f
);
1200 ST_FUNC
int tcc_load_coff(TCCState
* s1
, int fd
);
1203 /* ------------ tccasm.c ------------ */
1204 ST_FUNC
void asm_instr(void);
1205 ST_FUNC
void asm_global_instr(void);
1207 #ifdef CONFIG_TCC_ASM
1208 ST_FUNC
int find_constraint(ASMOperand
*operands
, int nb_operands
, const char *name
, const char **pp
);
1209 ST_FUNC
void asm_expr(TCCState
*s1
, ExprValue
*pe
);
1210 ST_FUNC
int asm_int_expr(TCCState
*s1
);
1211 ST_FUNC
int tcc_assemble(TCCState
*s1
, int do_preprocess
);
1212 /* ------------ i386-asm.c ------------ */
1213 ST_FUNC
void gen_expr32(ExprValue
*pe
);
1214 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
);
1215 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, const uint8_t *clobber_regs
, int *pout_reg
);
1216 ST_FUNC
void subst_asm_operand(CString
*add_str
, SValue
*sv
, int modifier
);
1217 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, int is_output
, uint8_t *clobber_regs
, int out_reg
);
1218 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
);
1220 /* ------------ tccpe.c -------------- */
1221 #ifdef TCC_TARGET_PE
1222 ST_FUNC
int pe_load_file(struct TCCState
*s1
, const char *filename
, int fd
);
1223 ST_FUNC
int pe_add_dll(struct TCCState
*s
, const char *libraryname
);
1224 ST_FUNC
int pe_output_file(TCCState
* s1
, const char *filename
);
1225 ST_FUNC
int pe_putimport(TCCState
*s1
, int dllindex
, const char *name
, const void *value
);
1226 ST_FUNC SValue
*pe_getimport(SValue
*sv
, SValue
*v2
);
1228 ST_FUNC
char *get_export_names(FILE *fp
);
1229 #ifdef TCC_TARGET_X86_64
1230 ST_FUNC
void pe_add_unwind_data(unsigned start
, unsigned end
, unsigned stack
);
1234 /* ------------ tccrun.c ----------------- */
1235 #ifdef CONFIG_TCC_STATIC
1236 #define RTLD_LAZY 0x001
1237 #define RTLD_NOW 0x002
1238 #define RTLD_GLOBAL 0x100
1239 #define RTLD_DEFAULT NULL
1240 /* dummy function for profiling */
1241 ST_FUNC
void *dlopen(const char *filename
, int flag
);
1242 ST_FUNC
void dlclose(void *p
);
1243 //ST_FUNC const char *dlerror(void);
1244 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1245 #elif !defined TCC_TARGET_PE || !defined _WIN32
1246 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1248 /********************************************************/
1249 /* include the target specific definitions */
1251 #define TARGET_DEFS_ONLY
1252 #ifdef TCC_TARGET_I386
1253 #include "i386-gen.c"
1255 #ifdef TCC_TARGET_X86_64
1256 #include "x86_64-gen.c"
1258 #ifdef TCC_TARGET_ARM
1259 #include "arm-gen.c"
1261 #ifdef TCC_TARGET_C67
1263 #include "c67-gen.c"
1265 #undef TARGET_DEFS_ONLY
1267 ST_DATA
const int reg_classes
[NB_REGS
];
1269 /********************************************************/
1272 #define ST_DATA static
1276 /********************************************************/