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
37 #include <sys/stat.h> /* stat() */
39 #ifdef CONFIG_TCCASSERT
41 #define TCC_ASSERT(ex) assert(ex)
43 #define TCC_ASSERT(ex)
48 # include <sys/time.h>
49 # include <sys/ucontext.h>
50 # include <sys/mman.h>
51 # ifndef CONFIG_TCC_STATIC
54 /* XXX: need to define this to use them in non ISOC99 context */
55 extern float strtof (const char *__nptr
, char **__endptr
);
56 extern long double strtold (const char *__nptr
, char **__endptr
);
57 #else /* on _WIN32: */
59 # include <sys/timeb.h>
60 # include <io.h> /* open, close etc. */
61 # include <direct.h> /* getcwd */
65 # define inline __inline
67 # define snprintf _snprintf
68 # define vsnprintf _vsnprintf
70 # define strtold (long double)strtod
71 # define strtof (float)strtod
72 # define strtoll _strtoi64
73 # define strtoull _strtoui64
76 # define LIBTCCAPI __declspec(dllexport)
77 # define PUB_FUNC LIBTCCAPI
80 # pragma warning (disable : 4244) // conversion from 'uint64_t' to 'int', possible loss of data
81 # pragma warning (disable : 4267) // conversion from 'size_t' to 'int', possible loss of data
82 # pragma warning (disable : 4996) // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
83 # pragma warning (disable : 4018) // signed/unsigned mismatch
84 # pragma warning (disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
93 # define NORETURN __attribute__ ((noreturn))
94 #elif defined _MSC_VER
95 # define NORETURN __declspec(noreturn)
101 # define IS_DIRSEP(c) (c == '/' || c == '\\')
102 # define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
103 # define PATHCMP stricmp
106 # define IS_DIRSEP(c) (c == '/')
107 # define IS_ABSPATH(p) IS_DIRSEP(p[0])
108 # define PATHCMP strcmp
118 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
119 # define ELFCLASSW ELFCLASS64
120 # define ElfW(type) Elf##64##_##type
121 # define ELFW(type) ELF##64##_##type
122 # define ElfW_Rel ElfW(Rela)
123 # define SHT_RELX SHT_RELA
124 # define REL_SECTION_FMT ".rela%s"
125 /* XXX: DLL with PLT would only work with x86-64 for now */
126 # define TCC_OUTPUT_DLL_WITH_PLT
128 # define ELFCLASSW ELFCLASS32
129 # define ElfW(type) Elf##32##_##type
130 # define ELFW(type) ELF##32##_##type
131 # define ElfW_Rel ElfW(Rel)
132 # define SHT_RELX SHT_REL
133 # define REL_SECTION_FMT ".rel%s"
136 /* target address type */
137 #define addr_t ElfW(Addr)
142 static inline uint16_t read16le(unsigned char *p
)
144 return p
[0] | (uint16_t)p
[1] << 8;
147 static inline void write16le(unsigned char *p
, uint16_t x
)
153 static inline uint32_t read32le(unsigned char *p
)
155 return (p
[0] | (uint32_t)p
[1] << 8 |
156 (uint32_t)p
[2] << 16 | (uint32_t)p
[3] << 24);
159 static inline void write32le(unsigned char *p
, uint32_t x
)
163 p
[2] = x
>> 16 & 255;
164 p
[3] = x
>> 24 & 255;
167 static inline uint64_t read64le(unsigned char *p
)
169 return (p
[0] | (uint64_t)p
[1] << 8 |
170 (uint64_t)p
[2] << 16 | (uint64_t)p
[3] << 24 |
171 (uint64_t)p
[4] << 32 | (uint64_t)p
[5] << 40 |
172 (uint64_t)p
[6] << 48 | (uint64_t)p
[7] << 56);
175 static inline void write64le(unsigned char *p
, uint64_t x
)
179 p
[2] = x
>> 16 & 255;
180 p
[3] = x
>> 24 & 255;
181 p
[4] = x
>> 32 & 255;
182 p
[5] = x
>> 40 & 255;
183 p
[6] = x
>> 48 & 255;
184 p
[7] = x
>> 56 & 255;
188 /* #define PARSE_DEBUG */
189 /* preprocessor debug */
190 /* #define PP_DEBUG */
191 /* include file debug */
192 /* #define INC_DEBUG */
193 /* memory leak debug */
194 /* #define MEM_DEBUG */
195 /* assembler debug */
196 /* #define ASM_DEBUG */
198 /* target selection */
199 /* #define TCC_TARGET_I386 *//* i386 code generator */
200 /* #define TCC_TARGET_ARM *//* ARMv4 code generator */
201 /* #define TCC_TARGET_ARM64 *//* ARMv8 code generator */
202 /* #define TCC_TARGET_C67 *//* TMS320C67xx code generator */
203 /* #define TCC_TARGET_X86_64 *//* x86-64 code generator */
205 /* default target is I386 */
206 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
207 !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
208 !defined(TCC_TARGET_X86_64)
209 #define TCC_TARGET_I386
212 #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
213 !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
214 !defined(CONFIG_USE_LIBGCC)
215 #define CONFIG_TCC_BCHECK /* enable bound checking code */
218 /* define it to include assembler support */
219 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_ARM64) && \
220 !defined(TCC_TARGET_C67)
221 #define CONFIG_TCC_ASM
224 /* object format selection */
225 #if defined(TCC_TARGET_C67)
226 #define TCC_TARGET_COFF
229 /* only native compiler supports -run */
230 #if defined _WIN32 == defined TCC_TARGET_PE
231 # if (defined __i386__ || defined _X86_) && defined TCC_TARGET_I386
232 # define TCC_IS_NATIVE
233 # elif (defined __x86_64__ || defined _AMD64_) && defined TCC_TARGET_X86_64
234 # define TCC_IS_NATIVE
235 # elif defined __arm__ && defined TCC_TARGET_ARM
236 # define TCC_IS_NATIVE
237 # elif defined __aarch64__ && defined TCC_TARGET_ARM64
238 # define TCC_IS_NATIVE
242 #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
243 # define CONFIG_TCC_BACKTRACE
246 /* ------------ path configuration ------------ */
248 #ifndef CONFIG_SYSROOT
249 # define CONFIG_SYSROOT ""
251 #ifndef CONFIG_TCCDIR
252 # define CONFIG_TCCDIR "."
255 # ifdef TCC_TARGET_X86_64
256 # define CONFIG_LDDIR "lib64"
258 # define CONFIG_LDDIR "lib"
262 #ifdef CONFIG_MULTIARCHDIR
263 # define USE_MUADIR(s) s "/" CONFIG_MULTIARCHDIR
264 # define ALSO_MUADIR(s) s "/" CONFIG_MULTIARCHDIR ":" s
266 # define USE_MUADIR(s) s
267 # define ALSO_MUADIR(s) s
270 /* path to find crt1.o, crti.o and crtn.o */
271 #ifndef CONFIG_TCC_CRTPREFIX
272 # define CONFIG_TCC_CRTPREFIX USE_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
275 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
277 /* system include paths */
278 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
279 # ifdef TCC_TARGET_PE
280 # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi"
282 # define CONFIG_TCC_SYSINCLUDEPATHS \
284 ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/include") \
285 ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/include")
289 /* library search paths */
290 #ifndef CONFIG_TCC_LIBPATHS
291 # ifdef TCC_TARGET_PE
292 # define CONFIG_TCC_LIBPATHS "{B}/lib"
294 # define CONFIG_TCC_LIBPATHS \
295 ALSO_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
296 ":" ALSO_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
297 ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
301 /* name of ELF interpreter */
302 #ifndef CONFIG_TCC_ELFINTERP
303 # if defined __FreeBSD__
304 # define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
305 # elif defined __FreeBSD_kernel__
306 # if defined(TCC_TARGET_X86_64)
307 # define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
309 # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
311 # elif defined __DragonFly__
312 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2"
313 # elif defined __NetBSD__
314 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
315 # elif defined __GNU__
316 # define CONFIG_TCC_ELFINTERP "/lib/ld.so"
317 # elif defined(TCC_TARGET_PE)
318 # define CONFIG_TCC_ELFINTERP "-"
319 # elif defined(TCC_UCLIBC)
320 # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */
321 # elif defined TCC_TARGET_ARM64
322 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
323 # elif defined(TCC_TARGET_X86_64)
324 # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
325 # elif !defined(TCC_ARM_EABI)
326 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
330 /* var elf_interp dans *-gen.c */
331 #ifdef CONFIG_TCC_ELFINTERP
332 # define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP
334 # define DEFAULT_ELFINTERP(s) default_elfinterp(s)
337 /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
338 #define TCC_LIBGCC USE_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
340 /* -------------------------------------------- */
341 /* include the target specific definitions */
343 #define TARGET_DEFS_ONLY
344 #ifdef TCC_TARGET_I386
345 # include "i386-gen.c"
347 #ifdef TCC_TARGET_X86_64
348 # include "x86_64-gen.c"
350 #ifdef TCC_TARGET_ARM
351 # include "arm-gen.c"
353 #ifdef TCC_TARGET_ARM64
354 # include "arm64-gen.c"
356 #ifdef TCC_TARGET_C67
358 # include "c67-gen.c"
360 #undef TARGET_DEFS_ONLY
362 /* -------------------------------------------- */
364 #define INCLUDE_STACK_SIZE 32
365 #define IFDEF_STACK_SIZE 64
366 #define VSTACK_SIZE 256
367 #define STRING_MAX_SIZE 1024
368 #define TOKSTR_MAX_SIZE 256
369 #define PACK_STACK_SIZE 8
371 #define TOK_HASH_SIZE 16384 /* must be a power of two */
372 #define TOK_ALLOC_INCR 512 /* must be a power of two */
373 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
374 #define TOKSYM_TAL_SIZE (768 * 1024) /* allocator for tiny TokenSym in table_ident */
375 #define TOKSTR_TAL_SIZE (768 * 1024) /* allocator for tiny TokenString instances */
376 #define CSTR_TAL_SIZE (256 * 1024) /* allocator for tiny CString instances */
377 #define TOKSYM_TAL_LIMIT 256 /* prefer unique limits to distinguish allocators debug msgs */
378 #define TOKSTR_TAL_LIMIT 128 /* 32 * sizeof(int) */
379 #define CSTR_TAL_LIMIT 1024
381 /* token symbol management */
382 typedef struct TokenSym
{
383 struct TokenSym
*hash_next
;
384 struct Sym
*sym_define
; /* direct pointer to define */
385 struct Sym
*sym_label
; /* direct pointer to label */
386 struct Sym
*sym_struct
; /* direct pointer to structure */
387 struct Sym
*sym_identifier
; /* direct pointer to identifier */
388 int tok
; /* token number */
394 typedef unsigned short nwchar_t
;
396 typedef int nwchar_t
;
399 typedef struct CString
{
400 int size
; /* size in bytes */
401 void *data
; /* either 'char *' or 'nwchar_t *' */
403 void *data_allocated
; /* if non NULL, data has been malloced */
406 /* type definition */
407 typedef struct CType
{
413 typedef union CValue
{
421 void *data_allocated
;
423 int tab
[LDOUBLE_SIZE
/4];
427 typedef struct SValue
{
428 CType type
; /* type */
429 unsigned short r
; /* register + flags */
430 unsigned short r2
; /* second register, used for 'long long'
431 type. If not used, set to VT_CONST */
432 CValue c
; /* constant, if VT_CONST */
433 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
438 func_call
: 3, /* calling convention (0..5), see below */
439 aligned
: 5, /* alignement (0..16) */
448 fill
: 8; // 8 bits left to fit well in union below
451 /* GNUC attribute definition */
452 typedef struct AttributeDef
{
454 struct Section
*section
;
455 int alias_target
; /* token */
456 int asm_label
; /* associated asm label */
459 /* symbol management */
461 int v
; /* symbol token */
463 int asm_label
; /* associated asm label */
464 int scope
; /* scope level for locals */
467 long r
; /* associated register */
471 long c
; /* associated number */
472 int *d
; /* define token stream */
474 CType type
; /* associated type */
476 struct Sym
*next
; /* next related symbol */
477 long jnext
; /* next jump label */
479 struct Sym
*prev
; /* prev symbol in stack */
480 struct Sym
*prev_tok
; /* previous symbol for this token */
483 /* section definition */
484 /* XXX: use directly ELF structure for parameters ? */
485 /* special flag to indicate that the section should not be linked to
487 #define SHF_PRIVATE 0x80000000
489 /* special flag, too */
490 #define SECTION_ABS ((void *)1)
492 typedef struct Section
{
493 unsigned long data_offset
; /* current data offset */
494 unsigned char *data
; /* section data */
495 unsigned long data_allocated
; /* used for realloc() handling */
496 int sh_name
; /* elf section name (only used during output) */
497 int sh_num
; /* elf section number */
498 int sh_type
; /* elf section type */
499 int sh_flags
; /* elf section flags */
500 int sh_info
; /* elf section info */
501 int sh_addralign
; /* elf section alignment */
502 int sh_entsize
; /* elf entry size */
503 unsigned long sh_size
; /* section size (only used during output) */
504 addr_t sh_addr
; /* address at which the section is relocated */
505 unsigned long sh_offset
; /* file offset */
506 int nb_hashed_syms
; /* used to resize the hash table */
507 struct Section
*link
; /* link to another section */
508 struct Section
*reloc
; /* corresponding section for relocation, if any */
509 struct Section
*hash
; /* hash table for symbols */
510 struct Section
*next
;
511 char name
[1]; /* section name */
514 typedef struct DLLReference
{
520 /* -------------------------------------------------- */
522 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
523 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
524 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
526 /* stored in 'Sym.c' field */
527 #define FUNC_NEW 1 /* ansi function prototype */
528 #define FUNC_OLD 2 /* old function prototype */
529 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
531 /* stored in 'Sym.r' field */
532 #define FUNC_CDECL 0 /* standard c call */
533 #define FUNC_STDCALL 1 /* pascal c call */
534 #define FUNC_FASTCALL1 2 /* first param in %eax */
535 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
536 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
537 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
539 /* field 'Sym.t' for macros */
540 #define MACRO_OBJ 0 /* object like macro */
541 #define MACRO_FUNC 1 /* function like macro */
543 /* field 'Sym.r' for C labels */
544 #define LABEL_DEFINED 0 /* label is defined */
545 #define LABEL_FORWARD 1 /* label is forward defined */
546 #define LABEL_DECLARED 2 /* label is declared but never used */
548 /* type_decl() types */
549 #define TYPE_ABSTRACT 1 /* type without variable */
550 #define TYPE_DIRECT 2 /* type with variable */
552 #define IO_BUF_SIZE 8192
554 typedef struct BufferedFile
{
558 struct BufferedFile
*prev
;
559 int line_num
; /* current line number - here to simplify code */
560 int line_ref
; /* tcc -E: last printed line */
561 int ifndef_macro
; /* #ifndef macro / #endif search */
562 int ifndef_macro_saved
; /* saved ifndef_macro */
563 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
564 int include_next_index
; /* next search path */
565 char filename
[1024]; /* filename */
566 unsigned char unget
[4];
567 unsigned char buffer
[1]; /* extra size for CH_EOB char */
570 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
571 #define CH_EOF (-1) /* end of file */
573 /* parsing state (used to save parser state to reparse part of the
574 source several times) */
575 typedef struct ParseState
{
576 const int *macro_ptr
;
582 /* used to record tokens */
583 typedef struct TokenString
{
588 /* used to chain token-strings with begin/end_macro() */
589 struct TokenString
*prev
;
594 /* inline functions */
595 typedef struct InlineFunc
{
596 TokenString func_str
;
601 /* include file cache, used to find files faster and also to eliminate
602 inclusion if the include file is protected by #ifndef ... #endif */
603 typedef struct CachedInclude
{
605 int hash_next
; /* -1 if none */
606 char filename
[1]; /* path specified in #include */
609 #define CACHED_INCLUDES_HASH_SIZE 512
611 #ifdef CONFIG_TCC_ASM
612 typedef struct ExprValue
{
617 #define MAX_ASM_OPERANDS 30
618 typedef struct ASMOperand
{
619 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
621 char asm_str
[16]; /* computed asm string for operand */
622 SValue
*vt
; /* C value of the expression */
623 int ref_index
; /* if >= 0, gives reference to a output constraint */
624 int input_index
; /* if >= 0, gives reference to an input constraint */
625 int priority
; /* priority, used to assign registers */
626 int reg
; /* if >= 0, register number used for this operand */
627 int is_llong
; /* true if double register value */
628 int is_memory
; /* true if memory operand */
629 int is_rw
; /* for '+' modifier */
634 unsigned long got_offset
;
635 unsigned long plt_offset
;
636 #ifdef TCC_TARGET_ARM
637 unsigned char plt_thumb_stub
:1;
641 typedef struct ParseArgsState
646 CString linker_arg
; /* collect -Wl options for input such as "-Wl,-rpath -Wl,<path>" */
649 #if !defined(MEM_DEBUG)
650 #define tal_free(al, p) tal_free_impl(al, p)
651 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
652 #define TAL_DEBUG_PARAMS
655 #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
656 #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
657 #define TAL_DEBUG_PARAMS , const char *file, int line
658 #define TAL_DEBUG_FILE_LEN 15
660 //#define TAL_INFO 1 /* collect and dump allocators stats */
662 typedef struct TinyAlloc
{
668 struct TinyAlloc
*next
, *top
;
677 typedef struct tal_header_t
{
680 int line_num
; /* negative line_num used for double free check */
681 char file_name
[TAL_DEBUG_FILE_LEN
+ 1];
687 int verbose
; /* if true, display some information during compilation */
688 int nostdinc
; /* if true, no standard headers are added */
689 int nostdlib
; /* if true, no standard libraries are added */
690 int nocommon
; /* if true, do not use common symbols for .bss data */
691 int static_link
; /* if true, static linking is performed */
692 int rdynamic
; /* if true, all symbols are exported */
693 int symbolic
; /* if true, resolve symbols in the current module first */
694 int alacarte_link
; /* if true, only link in referenced objects from archive */
696 char *tcc_lib_path
; /* CONFIG_TCCDIR or -B option */
697 char *soname
; /* as specified on the command line (-soname) */
698 char *rpath
; /* as specified on the command line (-Wl,-rpath=) */
700 /* output type, see TCC_OUTPUT_XXX */
702 /* output format, see TCC_OUTPUT_FORMAT_xxx */
705 /* C language options */
706 int char_is_unsigned
;
707 int leading_underscore
;
708 int ms_extensions
; /* allow nested named struct w/o identifier behave like unnamed */
709 int old_struct_init_code
; /* use old algorithm to init array in struct when there is no '{' used.
710 Liuux 2.4.26 can't find initrd when compiled with a new algorithm */
711 int dollars_in_identifiers
; /* allows '$' char in indentifiers */
712 int normalize_inc_dirs
; /* remove non-existent or duplicate directories from include paths */
714 /* warning switches */
715 int warn_write_strings
;
716 int warn_unsupported
;
719 int warn_implicit_function_declaration
;
721 /* compile with debug symbol (and use them if error during execution) */
724 #ifdef CONFIG_TCC_BCHECK
725 /* compile with built-in memory and bounds checker */
728 #ifdef TCC_TARGET_ARM
729 enum float_abi float_abi
; /* float ABI of the generated code*/
732 addr_t text_addr
; /* address of text section */
735 unsigned long section_align
; /* section alignment */
737 char *init_symbol
; /* symbols to call at load-time (not used currently) */
738 char *fini_symbol
; /* symbols to call at unload-time (not used currently) */
740 #ifdef TCC_TARGET_I386
741 int seg_size
; /* 32. Can be 16 with i386 assembler (.code16) */
744 /* array of all loaded dlls (including those referenced by loaded dlls) */
745 DLLReference
**loaded_dlls
;
749 char **include_paths
;
750 int nb_include_paths
;
752 char **sysinclude_paths
;
753 int nb_sysinclude_paths
;
756 char **library_paths
;
757 int nb_library_paths
;
759 /* crt?.o object path */
765 void (*error_func
)(void *opaque
, const char *msg
);
766 int error_set_jmp_enabled
;
767 jmp_buf error_jmp_buf
;
770 /* output file for preprocessing (-E) */
773 LINE_MACRO_OUTPUT_FORMAT_GCC
,
774 LINE_MACRO_OUTPUT_FORMAT_NONE
,
775 LINE_MACRO_OUTPUT_FORMAT_STD
776 } Pflag
; /* -P switch */
777 char dflag
; /* -dX value */
779 /* for -MD/-MF: collected dependencies for this compilation */
784 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
785 BufferedFile
**include_stack_ptr
;
787 int ifdef_stack
[IFDEF_STACK_SIZE
];
788 int *ifdef_stack_ptr
;
790 /* included files enclosed with #ifndef MACRO */
791 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
792 CachedInclude
**cached_includes
;
793 int nb_cached_includes
;
795 /* #pragma pack stack */
796 int pack_stack
[PACK_STACK_SIZE
];
801 /* inline functions are stored as token lists and compiled last
802 only if referenced */
803 struct InlineFunc
**inline_fns
;
808 int nb_sections
; /* number of sections, including first dummy section */
810 Section
**priv_sections
;
811 int nb_priv_sections
; /* number of private sections */
813 /* got & plt handling */
816 struct sym_attr
*sym_attrs
;
818 /* give the correspondance from symtab indexes to dynsym indexes */
819 int *symtab_to_dynsym
;
821 /* temporary dynamic symbol sections (for dll loading) */
822 Section
*dynsymtab_section
;
823 /* exported dynamic symbol section */
825 /* copy of the gobal symtab_section variable */
827 /* tiny assembler state */
833 unsigned pe_file_align
;
834 unsigned pe_stack_size
;
835 # ifdef TCC_TARGET_X86_64
843 const char *runtime_main
;
844 /* for tcc_relocate */
848 unsigned long mem_size
;
852 /* used by main and tcc_parse_args only */
853 char **files
; /* files seen on command line */
854 int nb_files
; /* number thereof */
855 int nb_libraries
; /* number of libs thereof */
856 char *outfile
; /* output filename */
857 char *option_m
; /* only -m32/-m64 handled */
858 int print_search_dirs
; /* option */
859 int option_r
; /* option -r */
860 int do_bench
; /* option -bench */
861 int gen_deps
; /* option -MD */
862 char *deps_outfile
; /* option -MF */
863 ParseArgsState
*parse_args_state
;
866 /* The current value can be: */
867 #define VT_VALMASK 0x003f /* mask for value location, register or: */
868 #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
869 #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
870 #define VT_LOCAL 0x0032 /* offset on stack */
871 #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
872 #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
873 #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
874 #define VT_REF 0x0040 /* value is pointer to structure rather than address */
875 #define VT_LVAL 0x0100 /* var is an lvalue */
876 #define VT_SYM 0x0200 /* a symbol value is added */
877 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
878 char/short stored in integer registers) */
879 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
880 dereferencing value */
881 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
882 bounding function call point is in vc */
883 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
884 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
885 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
886 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
889 #define VT_BTYPE 0x000f /* mask for basic type */
890 #define VT_INT 0 /* integer type */
891 #define VT_BYTE 1 /* signed byte type */
892 #define VT_SHORT 2 /* short type */
893 #define VT_VOID 3 /* void type */
894 #define VT_PTR 4 /* pointer */
895 #define VT_ENUM 5 /* enum definition */
896 #define VT_FUNC 6 /* function type */
897 #define VT_STRUCT 7 /* struct/union definition */
898 #define VT_FLOAT 8 /* IEEE float */
899 #define VT_DOUBLE 9 /* IEEE double */
900 #define VT_LDOUBLE 10 /* IEEE long double */
901 #define VT_BOOL 11 /* ISOC99 boolean type */
902 #define VT_LLONG 12 /* 64 bit integer */
903 #define VT_LONG 13 /* long integer (NEVER USED as type, only
905 #define VT_QLONG 14 /* 128-bit integer. Only used for x86-64 ABI */
906 #define VT_QFLOAT 15 /* 128-bit float. Only used for x86-64 ABI */
907 #define VT_UNSIGNED 0x0010 /* unsigned type */
908 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
909 #define VT_BITFIELD 0x0040 /* bitfield modifier */
910 #define VT_CONSTANT 0x0800 /* const modifier */
911 #define VT_VOLATILE 0x1000 /* volatile modifier */
912 #define VT_DEFSIGN 0x2000 /* signed type */
913 #define VT_VLA 0x00020000 /* VLA type (also has VT_PTR and VT_ARRAY) */
916 #define VT_EXTERN 0x00000080 /* extern definition */
917 #define VT_STATIC 0x00000100 /* static variable */
918 #define VT_TYPEDEF 0x00000200 /* typedef definition */
919 #define VT_INLINE 0x00000400 /* inline definition */
920 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
921 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
922 #define VT_WEAK 0x00010000 /* weak symbol */
923 #define VT_TLS 0x00040000 /* thread-local storage */
924 #define VT_VIS_SHIFT 19 /* shift for symbol visibility, overlapping
925 bitfield values, because bitfields never
926 have linkage and hence never have
928 #define VT_VIS_SIZE 2 /* We have four visibilities. */
929 #define VT_VIS_MASK (((1 << VT_VIS_SIZE)-1) << VT_VIS_SHIFT)
931 #define VT_STRUCT_SHIFT 19 /* shift for bitfield shift values (max: 32 - 2*6) */
934 /* type mask (except storage) */
935 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK | VT_VIS_MASK)
936 #define VT_TYPE (~(VT_STORAGE))
940 /* warning: the following compare tokens depend on i386 asm code */
947 #define TOK_Nset 0x98
948 #define TOK_Nclear 0x99
954 #define TOK_LAND 0xa0
957 #define TOK_MID 0xa3 /* inc/dec, to void constant */
959 #define TOK_UDIV 0xb0 /* unsigned division */
960 #define TOK_UMOD 0xb1 /* unsigned modulo */
961 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
963 /* tokens that carry values (in additional token string space / tokc) --> */
964 #define TOK_CCHAR 0xb3 /* char constant in tokc */
965 #define TOK_LCHAR 0xb4
966 #define TOK_CINT 0xb5 /* number in tokc */
967 #define TOK_CUINT 0xb6 /* unsigned int constant */
968 #define TOK_CLLONG 0xb7 /* long long constant */
969 #define TOK_CULLONG 0xb8 /* unsigned long long constant */
970 #define TOK_STR 0xb9 /* pointer to string in tokc */
971 #define TOK_LSTR 0xba
972 #define TOK_CFLOAT 0xbb /* float constant */
973 #define TOK_CDOUBLE 0xbc /* double constant */
974 #define TOK_CLDOUBLE 0xbd /* long double constant */
975 #define TOK_PPNUM 0xbe /* preprocessor number */
976 #define TOK_PPSTR 0xbf /* preprocessor string */
977 #define TOK_LINENUM 0xc0 /* line number info */
980 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
981 #define TOK_ADDC1 0xc3 /* add with carry generation */
982 #define TOK_ADDC2 0xc4 /* add with carry use */
983 #define TOK_SUBC1 0xc5 /* add with carry generation */
984 #define TOK_SUBC2 0xc6 /* add with carry use */
985 #define TOK_ARROW 0xc7
986 #define TOK_DOTS 0xc8 /* three dots */
987 #define TOK_SHR 0xc9 /* unsigned shift right */
988 #define TOK_TWOSHARPS 0xca /* ## preprocessing token */
989 #define TOK_PLCHLDR 0xcb /* placeholder token as defined in C99 */
990 #define TOK_NOSUBST 0xcc /* means following token has already been pp'd */
992 #define TOK_SHL 0x01 /* shift left */
993 #define TOK_SAR 0x02 /* signed shift right */
995 /* assignement operators : normal operator or 0x80 */
996 #define TOK_A_MOD 0xa5
997 #define TOK_A_AND 0xa6
998 #define TOK_A_MUL 0xaa
999 #define TOK_A_ADD 0xab
1000 #define TOK_A_SUB 0xad
1001 #define TOK_A_DIV 0xaf
1002 #define TOK_A_XOR 0xde
1003 #define TOK_A_OR 0xfc
1004 #define TOK_A_SHL 0x81
1005 #define TOK_A_SAR 0x82
1008 #define offsetof(type, field) ((size_t) &((type *)0)->field)
1012 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
1015 #define TOK_EOF (-1) /* end of file */
1016 #define TOK_LINEFEED 10 /* line feed */
1018 /* all identificators and strings have token above that */
1019 #define TOK_IDENT 256
1021 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
1022 #define TOK_ASM_int TOK_INT
1023 #define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
1024 #define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
1025 #define TOK_ASMDIR_LAST TOK_ASMDIR_section
1027 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1028 /* only used for i386 asm opcodes definitions */
1029 #define DEF_BWL(x) \
1030 DEF(TOK_ASM_ ## x ## b, #x "b") \
1031 DEF(TOK_ASM_ ## x ## w, #x "w") \
1032 DEF(TOK_ASM_ ## x ## l, #x "l") \
1033 DEF(TOK_ASM_ ## x, #x)
1035 DEF(TOK_ASM_ ## x ## w, #x "w") \
1036 DEF(TOK_ASM_ ## x ## l, #x "l") \
1037 DEF(TOK_ASM_ ## x, #x)
1038 #ifdef TCC_TARGET_X86_64
1039 # define DEF_BWLQ(x) \
1040 DEF(TOK_ASM_ ## x ## b, #x "b") \
1041 DEF(TOK_ASM_ ## x ## w, #x "w") \
1042 DEF(TOK_ASM_ ## x ## l, #x "l") \
1043 DEF(TOK_ASM_ ## x ## q, #x "q") \
1044 DEF(TOK_ASM_ ## x, #x)
1045 # define DEF_WLQ(x) \
1046 DEF(TOK_ASM_ ## x ## w, #x "w") \
1047 DEF(TOK_ASM_ ## x ## l, #x "l") \
1048 DEF(TOK_ASM_ ## x ## q, #x "q") \
1049 DEF(TOK_ASM_ ## x, #x)
1050 # define DEF_BWLX DEF_BWLQ
1051 # define DEF_WLX DEF_WLQ
1052 /* number of sizes + 1 */
1055 # define DEF_BWLX DEF_BWL
1056 # define DEF_WLX DEF_WL
1057 /* number of sizes + 1 */
1061 #define DEF_FP1(x) \
1062 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
1063 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
1064 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
1065 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
1068 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
1069 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
1072 #define DEF_ASMTEST(x,suffix) \
1073 DEF_ASM(x ## o ## suffix) \
1074 DEF_ASM(x ## no ## suffix) \
1075 DEF_ASM(x ## b ## suffix) \
1076 DEF_ASM(x ## c ## suffix) \
1077 DEF_ASM(x ## nae ## suffix) \
1078 DEF_ASM(x ## nb ## suffix) \
1079 DEF_ASM(x ## nc ## suffix) \
1080 DEF_ASM(x ## ae ## suffix) \
1081 DEF_ASM(x ## e ## suffix) \
1082 DEF_ASM(x ## z ## suffix) \
1083 DEF_ASM(x ## ne ## suffix) \
1084 DEF_ASM(x ## nz ## suffix) \
1085 DEF_ASM(x ## be ## suffix) \
1086 DEF_ASM(x ## na ## suffix) \
1087 DEF_ASM(x ## nbe ## suffix) \
1088 DEF_ASM(x ## a ## suffix) \
1089 DEF_ASM(x ## s ## suffix) \
1090 DEF_ASM(x ## ns ## suffix) \
1091 DEF_ASM(x ## p ## suffix) \
1092 DEF_ASM(x ## pe ## suffix) \
1093 DEF_ASM(x ## np ## suffix) \
1094 DEF_ASM(x ## po ## suffix) \
1095 DEF_ASM(x ## l ## suffix) \
1096 DEF_ASM(x ## nge ## suffix) \
1097 DEF_ASM(x ## nl ## suffix) \
1098 DEF_ASM(x ## ge ## suffix) \
1099 DEF_ASM(x ## le ## suffix) \
1100 DEF_ASM(x ## ng ## suffix) \
1101 DEF_ASM(x ## nle ## suffix) \
1102 DEF_ASM(x ## g ## suffix)
1104 #endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
1107 TOK_LAST
= TOK_IDENT
- 1
1108 #define DEF(id, str) ,id
1113 #define TOK_UIDENT TOK_DEFINE
1115 /* space exlcuding newline */
1116 static inline int is_space(int ch
)
1118 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
1121 static inline int isid(int c
)
1123 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') || c
== '_';
1126 static inline int isnum(int c
)
1128 return c
>= '0' && c
<= '9';
1131 static inline int isoct(int c
)
1133 return c
>= '0' && c
<= '7';
1136 static inline int toup(int c
)
1138 return (c
>= 'a' && c
<= 'z') ? c
- 'a' + 'A' : c
;
1146 #define ST_INLN static inline
1147 #define ST_FUNC static
1148 #define ST_DATA static
1152 #define ST_DATA extern
1155 /* ------------ libtcc.c ------------ */
1157 /* use GNU C extensions */
1158 ST_DATA
int gnu_ext
;
1159 /* use Tiny C extensions */
1160 ST_DATA
int tcc_ext
;
1161 /* XXX: get rid of this ASAP */
1162 ST_DATA
struct TCCState
*tcc_state
;
1164 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
1165 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
1166 #define AFF_PREPROCESS 0x0004 /* preprocess file */
1168 /* public functions currently used by the tcc main function */
1169 PUB_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
);
1170 PUB_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
);
1171 PUB_FUNC
char *pstrncpy(char *out
, const char *in
, size_t num
);
1172 PUB_FUNC
char *tcc_basename(const char *name
);
1173 PUB_FUNC
char *tcc_fileextension (const char *name
);
1176 PUB_FUNC
void tcc_free(void *ptr
);
1177 PUB_FUNC
void *tcc_malloc(unsigned long size
);
1178 PUB_FUNC
void *tcc_mallocz(unsigned long size
);
1179 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
);
1180 PUB_FUNC
char *tcc_strdup(const char *str
);
1182 #define tcc_free(ptr) tcc_free_debug(ptr)
1183 #define tcc_malloc(size) tcc_malloc_debug(size, __FILE__, __LINE__)
1184 #define tcc_mallocz(size) tcc_mallocz_debug(size, __FILE__, __LINE__)
1185 #define tcc_realloc(ptr,size) tcc_realloc_debug(ptr, size, __FILE__, __LINE__)
1186 #define tcc_strdup(str) tcc_strdup_debug(str, __FILE__, __LINE__)
1187 PUB_FUNC
void tcc_free_debug(void *ptr
);
1188 PUB_FUNC
void *tcc_malloc_debug(unsigned long size
, const char *file
, int line
);
1189 PUB_FUNC
void *tcc_mallocz_debug(unsigned long size
, const char *file
, int line
);
1190 PUB_FUNC
void *tcc_realloc_debug(void *ptr
, unsigned long size
, const char *file
, int line
);
1191 PUB_FUNC
char *tcc_strdup_debug(const char *str
, const char *file
, int line
);
1194 #define free(p) use_tcc_free(p)
1195 #define malloc(s) use_tcc_malloc(s)
1196 #define realloc(p, s) use_tcc_realloc(p, s)
1198 #define strdup(s) use_tcc_strdup(s)
1199 PUB_FUNC
void tcc_memstats(int bench
);
1200 PUB_FUNC
void tcc_error_noabort(const char *fmt
, ...);
1201 PUB_FUNC NORETURN
void tcc_error(const char *fmt
, ...);
1202 PUB_FUNC
void tcc_warning(const char *fmt
, ...);
1204 /* other utilities */
1205 ST_FUNC
void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
1206 ST_FUNC
void dynarray_reset(void *pp
, int *n
);
1207 ST_FUNC
void cstr_ccat(CString
*cstr
, int ch
);
1208 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
, int len
);
1209 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
);
1210 ST_FUNC
void cstr_new(CString
*cstr
);
1211 ST_FUNC
void cstr_free(CString
*cstr
);
1212 ST_FUNC
void cstr_reset(CString
*cstr
);
1214 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
);
1215 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
);
1216 ST_FUNC
void *section_ptr_add(Section
*sec
, addr_t size
);
1217 ST_FUNC
void section_reserve(Section
*sec
, unsigned long size
);
1218 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
);
1220 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
, addr_t value
, unsigned long size
, int can_add_underscore
);
1221 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
, addr_t value
, unsigned long size
);
1222 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
);
1223 ST_FUNC
void greloca(Section
*s
, Sym
*sym
, unsigned long offset
, int type
, addr_t addend
);
1225 ST_INLN
void sym_free(Sym
*sym
);
1226 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
);
1227 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
);
1228 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
1229 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
);
1230 ST_INLN Sym
*struct_find(int v
);
1231 ST_INLN Sym
*sym_find(int v
);
1232 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
);
1234 ST_FUNC
void tcc_open_bf(TCCState
*s1
, const char *filename
, int initlen
);
1235 ST_FUNC
int tcc_open(TCCState
*s1
, const char *filename
);
1236 ST_FUNC
void tcc_close(void);
1238 ST_FUNC
int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
, int filetype
);
1239 ST_FUNC
int tcc_add_crt(TCCState
*s
, const char *filename
);
1241 #ifndef TCC_TARGET_PE
1242 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
1245 ST_FUNC
void tcc_add_pragma_libs(TCCState
*s1
);
1246 PUB_FUNC
int tcc_add_library_err(TCCState
*s
, const char *f
);
1248 PUB_FUNC
void tcc_print_stats(TCCState
*s
, int64_t total_time
);
1249 PUB_FUNC
int tcc_parse_args(TCCState
*s
, int argc
, char **argv
);
1250 PUB_FUNC
void tcc_set_environment(TCCState
*s
);
1252 /* ------------ tccpp.c ------------ */
1254 ST_DATA
struct BufferedFile
*file
;
1255 ST_DATA
int ch
, tok
;
1256 ST_DATA CValue tokc
;
1257 ST_DATA
const int *macro_ptr
;
1258 ST_DATA
int parse_flags
;
1259 ST_DATA
int tok_flags
;
1260 ST_DATA CString tokcstr
; /* current parsed string, if any */
1262 /* display benchmark infos */
1263 ST_DATA
int total_lines
;
1264 ST_DATA
int total_bytes
;
1265 ST_DATA
int tok_ident
;
1266 ST_DATA TokenSym
**table_ident
;
1268 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
1269 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
1270 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
1271 #define TOK_FLAG_EOF 0x0008 /* end of file */
1273 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
1274 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
1275 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
1276 token. line feed is also
1278 #define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */
1279 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1280 #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
1281 #define PARSE_FLAG_TOK_STR 0x0040 /* return parsed strings instead of TOK_PPSTR */
1283 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
);
1284 ST_FUNC
const char *get_tok_str(int v
, CValue
*cv
);
1285 ST_FUNC
void begin_macro(TokenString
*str
, int alloc
);
1286 ST_FUNC
void end_macro(void);
1287 ST_FUNC
void save_parse_state(ParseState
*s
);
1288 ST_FUNC
void restore_parse_state(ParseState
*s
);
1289 ST_INLN
void tok_str_new(TokenString
*s
);
1290 ST_FUNC
void tok_str_free(int *str
);
1291 ST_FUNC
void tok_str_add(TokenString
*s
, int t
);
1292 ST_FUNC
void tok_str_add_tok(TokenString
*s
);
1293 ST_INLN
void define_push(int v
, int macro_type
, TokenString
*str
, Sym
*first_arg
);
1294 ST_FUNC
void define_undef(Sym
*s
);
1295 ST_INLN Sym
*define_find(int v
);
1296 ST_FUNC
void free_defines(Sym
*b
);
1297 ST_FUNC Sym
*label_find(int v
);
1298 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
);
1299 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
);
1300 ST_FUNC
void parse_define(void);
1301 ST_FUNC
void preprocess(int is_bof
);
1302 ST_FUNC
void next_nomacro(void);
1303 ST_FUNC
void next(void);
1304 ST_INLN
void unget_tok(int last_tok
);
1305 ST_FUNC
void preprocess_init(TCCState
*s1
);
1306 ST_FUNC
void preprocess_new(void);
1307 ST_FUNC
void preprocess_delete(void);
1308 ST_FUNC
int tcc_preprocess(TCCState
*s1
);
1309 ST_FUNC
void skip(int c
);
1310 ST_FUNC NORETURN
void expect(const char *msg
);
1311 ST_FUNC
char *trimfront(char *p
);
1312 ST_FUNC
char *trimback(char *a
, char *e
);
1314 /* ------------ tccgen.c ------------ */
1316 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
1317 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
1318 #ifdef CONFIG_TCC_ASM
1319 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
1321 #ifdef CONFIG_TCC_BCHECK
1322 /* bound check related sections */
1323 ST_DATA Section
*bounds_section
; /* contains global data bound description */
1324 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
1326 /* symbol sections */
1327 ST_DATA Section
*symtab_section
, *strtab_section
;
1328 /* debug sections */
1329 ST_DATA Section
*stab_section
, *stabstr_section
;
1331 #define SYM_POOL_NB (8192 / sizeof(Sym))
1332 ST_DATA Sym
*sym_free_first
;
1333 ST_DATA
void **sym_pools
;
1334 ST_DATA
int nb_sym_pools
;
1336 ST_DATA Sym
*global_stack
;
1337 ST_DATA Sym
*local_stack
;
1338 ST_DATA Sym
*local_label_stack
;
1339 ST_DATA Sym
*global_label_stack
;
1340 ST_DATA Sym
*define_stack
;
1341 ST_DATA CType char_pointer_type
, func_old_type
, int_type
, size_type
;
1342 ST_DATA SValue __vstack
[1+/*to make bcheck happy*/ VSTACK_SIZE
], *vtop
, *pvtop
;
1343 #define vstack (__vstack + 1)
1344 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
1346 ST_DATA
int const_wanted
; /* true if constant wanted */
1347 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
1348 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
1349 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
1350 ST_DATA
int func_var
; /* true if current function is variadic */
1351 ST_DATA
int func_vc
;
1352 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
1353 ST_DATA
const char *funcname
;
1355 ST_FUNC
void check_vstack(void);
1356 ST_INLN
int is_float(int t
);
1357 ST_FUNC
int ieee_finite(double d
);
1358 ST_FUNC
void test_lvalue(void);
1359 ST_FUNC
void swap(int *p
, int *q
);
1360 ST_FUNC
void vpushi(int v
);
1361 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
);
1362 ST_FUNC
void vset(CType
*type
, int r
, int v
);
1363 ST_FUNC
void vswap(void);
1364 ST_FUNC
void vpush_global_sym(CType
*type
, int v
);
1365 ST_FUNC
void vrote(SValue
*e
, int n
);
1366 ST_FUNC
void vrott(int n
);
1367 ST_FUNC
void vrotb(int n
);
1368 #ifdef TCC_TARGET_ARM
1369 ST_FUNC
int get_reg_ex(int rc
, int rc2
);
1370 ST_FUNC
void lexpand_nr(void);
1372 ST_FUNC
void vpushv(SValue
*v
);
1373 ST_FUNC
void save_reg(int r
);
1374 ST_FUNC
int get_reg(int rc
);
1375 ST_FUNC
void save_regs(int n
);
1376 ST_FUNC
void gaddrof(void);
1377 ST_FUNC
int gv(int rc
);
1378 ST_FUNC
void gv2(int rc1
, int rc2
);
1379 ST_FUNC
void vpop(void);
1380 ST_FUNC
void gen_op(int op
);
1381 ST_FUNC
int type_size(CType
*type
, int *a
);
1382 ST_FUNC
void mk_pointer(CType
*type
);
1383 ST_FUNC
void vstore(void);
1384 ST_FUNC
void inc(int post
, int c
);
1385 ST_FUNC
void parse_asm_str(CString
*astr
);
1386 ST_FUNC
int lvalue_type(int t
);
1387 ST_FUNC
void indir(void);
1388 ST_FUNC
void unary(void);
1389 ST_FUNC
void expr_prod(void);
1390 ST_FUNC
void expr_sum(void);
1391 ST_FUNC
void gexpr(void);
1392 ST_FUNC
int expr_const(void);
1393 ST_FUNC
void gen_inline_functions(void);
1394 ST_FUNC
void decl(int l
);
1395 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1396 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
);
1398 #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
1399 ST_FUNC
int classify_x86_64_va_arg(CType
*ty
);
1402 /* ------------ tccelf.c ------------ */
1404 #define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
1405 #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
1406 #define TCC_OUTPUT_FORMAT_COFF 2 /* COFF */
1408 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1411 unsigned int n_strx
; /* index into string table of name */
1412 unsigned char n_type
; /* type of symbol */
1413 unsigned char n_other
; /* misc info (usually empty) */
1414 unsigned short n_desc
; /* description field */
1415 unsigned int n_value
; /* value of symbol */
1418 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
);
1420 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
);
1421 ST_FUNC
int put_elf_sym(Section
*s
, addr_t value
, unsigned long size
, int info
, int other
, int shndx
, const char *name
);
1422 ST_FUNC
int add_elf_sym(Section
*s
, addr_t value
, unsigned long size
, int info
, int other
, int sh_num
, const char *name
);
1423 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
);
1424 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
, int type
, int symbol
);
1425 ST_FUNC
void put_elf_reloca(Section
*symtab
, Section
*s
, unsigned long offset
, int type
, int symbol
, addr_t addend
);
1427 ST_FUNC
void put_stabs(const char *str
, int type
, int other
, int desc
, unsigned long value
);
1428 ST_FUNC
void put_stabs_r(const char *str
, int type
, int other
, int desc
, unsigned long value
, Section
*sec
, int sym_index
);
1429 ST_FUNC
void put_stabn(int type
, int other
, int desc
, int value
);
1430 ST_FUNC
void put_stabd(int type
, int other
, int desc
);
1432 ST_FUNC
void relocate_common_syms(void);
1433 ST_FUNC
void relocate_syms(TCCState
*s1
, int do_resolve
);
1434 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
);
1435 ST_FUNC
void relocate_plt(TCCState
*s1
);
1437 ST_FUNC
void tcc_add_linker_symbols(TCCState
*s1
);
1438 ST_FUNC
int tcc_load_object_file(TCCState
*s1
, int fd
, unsigned long file_offset
);
1439 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
);
1440 ST_FUNC
void tcc_add_bcheck(TCCState
*s1
);
1442 ST_FUNC
void build_got_entries(TCCState
*s1
);
1443 ST_FUNC
void tcc_add_runtime(TCCState
*s1
);
1445 ST_FUNC addr_t
get_elf_sym_addr(TCCState
*s
, const char *name
, int err
);
1446 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
1447 ST_FUNC
void *tcc_get_symbol_err(TCCState
*s
, const char *name
);
1450 #ifndef TCC_TARGET_PE
1451 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
);
1452 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
);
1453 ST_FUNC
uint8_t *parse_comment(uint8_t *p
);
1454 ST_FUNC
void minp(void);
1455 ST_INLN
void inp(void);
1456 ST_FUNC
int handle_eob(void);
1459 /* ------------ xxx-gen.c ------------ */
1461 ST_DATA
const int reg_classes
[NB_REGS
];
1463 ST_FUNC
void gsym_addr(int t
, int a
);
1464 ST_FUNC
void gsym(int t
);
1465 ST_FUNC
void load(int r
, SValue
*sv
);
1466 ST_FUNC
void store(int r
, SValue
*v
);
1467 ST_FUNC
int gfunc_sret(CType
*vt
, int variadic
, CType
*ret
, int *align
, int *regsize
);
1468 ST_FUNC
void gfunc_call(int nb_args
);
1469 ST_FUNC
void gfunc_prolog(CType
*func_type
);
1470 ST_FUNC
void gfunc_epilog(void);
1471 ST_FUNC
int gjmp(int t
);
1472 ST_FUNC
void gjmp_addr(int a
);
1473 ST_FUNC
int gtst(int inv
, int t
);
1474 ST_FUNC
void gen_opi(int op
);
1475 ST_FUNC
void gen_opf(int op
);
1476 ST_FUNC
void gen_cvt_ftoi(int t
);
1477 ST_FUNC
void gen_cvt_ftof(int t
);
1478 ST_FUNC
void ggoto(void);
1479 #ifndef TCC_TARGET_C67
1480 ST_FUNC
void o(unsigned int c
);
1482 #ifndef TCC_TARGET_ARM
1483 ST_FUNC
void gen_cvt_itof(int t
);
1485 ST_FUNC
void gen_vla_sp_save(int addr
);
1486 ST_FUNC
void gen_vla_sp_restore(int addr
);
1487 ST_FUNC
void gen_vla_alloc(CType
*type
, int align
);
1489 /* ------------ i386-gen.c ------------ */
1490 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1491 ST_FUNC
void g(int c
);
1492 ST_FUNC
int oad(int c
, int s
);
1493 ST_FUNC
void gen_le16(int c
);
1494 ST_FUNC
void gen_le32(int c
);
1495 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
);
1496 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
);
1499 #ifdef CONFIG_TCC_BCHECK
1500 ST_FUNC
void gen_bounded_ptr_add(void);
1501 ST_FUNC
void gen_bounded_ptr_deref(void);
1504 /* ------------ x86_64-gen.c ------------ */
1505 #ifdef TCC_TARGET_X86_64
1506 ST_FUNC
void gen_addr64(int r
, Sym
*sym
, int64_t c
);
1507 ST_FUNC
void gen_opl(int op
);
1510 /* ------------ arm-gen.c ------------ */
1511 #ifdef TCC_TARGET_ARM
1512 #if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP)
1513 ST_FUNC
char *default_elfinterp(struct TCCState
*s
);
1515 ST_FUNC
void arm_init(struct TCCState
*s
);
1516 ST_FUNC
uint32_t encbranch(int pos
, int addr
, int fail
);
1517 ST_FUNC
void gen_cvt_itof1(int t
);
1520 /* ------------ arm64-gen.c ------------ */
1521 #ifdef TCC_TARGET_ARM64
1522 ST_FUNC
void gen_cvt_sxtw(void);
1523 ST_FUNC
void gen_opl(int op
);
1524 ST_FUNC
void greturn(void);
1525 ST_FUNC
void gen_va_start(void);
1526 ST_FUNC
void gen_va_arg(CType
*t
);
1527 ST_FUNC
void gen_clear_cache(void);
1530 /* ------------ c67-gen.c ------------ */
1531 #ifdef TCC_TARGET_C67
1534 /* ------------ tcccoff.c ------------ */
1536 #ifdef TCC_TARGET_COFF
1537 ST_FUNC
int tcc_output_coff(TCCState
*s1
, FILE *f
);
1538 ST_FUNC
int tcc_load_coff(TCCState
* s1
, int fd
);
1541 /* ------------ tccasm.c ------------ */
1542 ST_FUNC
void asm_instr(void);
1543 ST_FUNC
void asm_global_instr(void);
1544 #ifdef CONFIG_TCC_ASM
1545 ST_FUNC
int find_constraint(ASMOperand
*operands
, int nb_operands
, const char *name
, const char **pp
);
1546 ST_FUNC
void asm_expr(TCCState
*s1
, ExprValue
*pe
);
1547 ST_FUNC
int asm_int_expr(TCCState
*s1
);
1548 ST_FUNC
int tcc_assemble(TCCState
*s1
, int do_preprocess
);
1549 /* ------------ i386-asm.c ------------ */
1550 ST_FUNC
void gen_expr32(ExprValue
*pe
);
1551 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
);
1552 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, const uint8_t *clobber_regs
, int *pout_reg
);
1553 ST_FUNC
void subst_asm_operand(CString
*add_str
, SValue
*sv
, int modifier
);
1554 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, int is_output
, uint8_t *clobber_regs
, int out_reg
);
1555 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
);
1558 /* ------------ tccpe.c -------------- */
1559 #ifdef TCC_TARGET_PE
1560 ST_FUNC
int pe_load_file(struct TCCState
*s1
, const char *filename
, int fd
);
1561 ST_FUNC
int pe_output_file(TCCState
* s1
, const char *filename
);
1562 ST_FUNC
int pe_putimport(TCCState
*s1
, int dllindex
, const char *name
, addr_t value
);
1563 ST_FUNC SValue
*pe_getimport(SValue
*sv
, SValue
*v2
);
1564 #ifdef TCC_TARGET_X86_64
1565 ST_FUNC
void pe_add_unwind_data(unsigned start
, unsigned end
, unsigned stack
);
1567 /* symbol properties stored in Elf32_Sym->st_other */
1568 # define ST_PE_EXPORT 0x10
1569 # define ST_PE_IMPORT 0x20
1570 # define ST_PE_STDCALL 0x40
1573 /* ------------ tccrun.c ----------------- */
1574 #ifdef TCC_IS_NATIVE
1575 #ifdef CONFIG_TCC_STATIC
1576 #define RTLD_LAZY 0x001
1577 #define RTLD_NOW 0x002
1578 #define RTLD_GLOBAL 0x100
1579 #define RTLD_DEFAULT NULL
1580 /* dummy function for profiling */
1581 ST_FUNC
void *dlopen(const char *filename
, int flag
);
1582 ST_FUNC
void dlclose(void *p
);
1583 ST_FUNC
const char *dlerror(void);
1584 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1585 #elif !defined _WIN32
1586 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1589 #ifdef CONFIG_TCC_BACKTRACE
1590 ST_DATA
int rt_num_callers
;
1591 ST_DATA
const char **rt_bound_error_msg
;
1592 ST_DATA
void *rt_prog_main
;
1593 ST_FUNC
void tcc_set_num_callers(int n
);
1597 /********************************************************/
1600 #define ST_DATA static
1604 /********************************************************/