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
86 /* preprocessor debug */
88 /* include file debug */
96 /* target selection */
97 //#define TCC_TARGET_I386 /* i386 code generator */
98 //#define TCC_TARGET_ARM /* ARMv4 code generator */
99 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
100 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
102 /* default target is I386 */
103 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
104 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
105 #define TCC_TARGET_I386
108 #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
109 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
110 #define CONFIG_TCC_BCHECK /* enable bound checking code */
113 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
114 #define CONFIG_TCC_STATIC
117 /* define it to include assembler support */
118 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
119 #define CONFIG_TCC_ASM
122 /* object format selection */
123 #if defined(TCC_TARGET_C67)
124 #define TCC_TARGET_COFF
127 #if !defined(CONFIG_TCCBOOT)
128 #define CONFIG_TCC_BACKTRACE
137 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
138 executables or dlls */
139 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
141 #define INCLUDE_STACK_SIZE 32
142 #define IFDEF_STACK_SIZE 64
143 #define VSTACK_SIZE 256
144 #define STRING_MAX_SIZE 1024
145 #define PACK_STACK_SIZE 8
147 #define TOK_HASH_SIZE 8192 /* must be a power of two */
148 #define TOK_ALLOC_INCR 512 /* must be a power of two */
149 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
151 /* token symbol management */
152 typedef struct TokenSym
{
153 struct TokenSym
*hash_next
;
154 struct Sym
*sym_define
; /* direct pointer to define */
155 struct Sym
*sym_label
; /* direct pointer to label */
156 struct Sym
*sym_struct
; /* direct pointer to structure */
157 struct Sym
*sym_identifier
; /* direct pointer to identifier */
158 int tok
; /* token number */
164 typedef unsigned short nwchar_t
;
166 typedef int nwchar_t
;
169 typedef struct CString
{
170 int size
; /* size in bytes */
171 void *data
; /* either 'char *' or 'nwchar_t *' */
173 void *data_allocated
; /* if non NULL, data has been malloced */
176 /* type definition */
177 typedef struct CType
{
183 typedef union CValue
{
189 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
191 unsigned long long ull
;
192 struct CString
*cstr
;
198 typedef struct SValue
{
199 CType type
; /* type */
200 unsigned short r
; /* register + flags */
201 unsigned short r2
; /* second register, used for 'long long'
202 type. If not used, set to VT_CONST */
203 CValue c
; /* constant, if VT_CONST */
204 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
207 /* symbol management */
209 int v
; /* symbol token */
210 long r
; /* associated register */
212 long c
; /* associated number */
213 int *d
; /* define token stream */
215 CType type
; /* associated type */
217 struct Sym
*next
; /* next related symbol */
218 long jnext
; /* next jump label */
220 struct Sym
*prev
; /* prev symbol in stack */
221 struct Sym
*prev_tok
; /* previous symbol for this token */
224 /* section definition */
225 /* XXX: use directly ELF structure for parameters ? */
226 /* special flag to indicate that the section should not be linked to
228 #define SHF_PRIVATE 0x80000000
230 /* special flag, too */
231 #define SECTION_ABS ((void *)1)
233 typedef struct Section
{
234 unsigned long data_offset
; /* current data offset */
235 unsigned char *data
; /* section data */
236 unsigned long data_allocated
; /* used for realloc() handling */
237 int sh_name
; /* elf section name (only used during output) */
238 int sh_num
; /* elf section number */
239 int sh_type
; /* elf section type */
240 int sh_flags
; /* elf section flags */
241 int sh_info
; /* elf section info */
242 int sh_addralign
; /* elf section alignment */
243 int sh_entsize
; /* elf entry size */
244 unsigned long sh_size
; /* section size (only used during output) */
245 unsigned long sh_addr
; /* address at which the section is relocated */
246 unsigned long sh_offset
; /* file offset */
247 int nb_hashed_syms
; /* used to resize the hash table */
248 struct Section
*link
; /* link to another section */
249 struct Section
*reloc
; /* corresponding section for relocation, if any */
250 struct Section
*hash
; /* hash table for symbols */
251 struct Section
*next
;
252 char name
[1]; /* section name */
255 typedef struct DLLReference
{
261 /* GNUC attribute definition */
262 typedef struct AttributeDef
{
264 func_call
: 3, /* calling convention (0..5), see below */
265 aligned
: 5, /* alignement (0..16) */
271 struct Section
*section
;
274 /* gr: wrappers for casting sym->r for other purposes */
275 #define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call)
276 #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
277 #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
278 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
279 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
280 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
281 #define INT_ATTR(ad) (*(int*)(ad))
283 /* -------------------------------------------------- */
285 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
286 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
287 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
289 /* stored in 'Sym.c' field */
290 #define FUNC_NEW 1 /* ansi function prototype */
291 #define FUNC_OLD 2 /* old function prototype */
292 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
294 /* stored in 'Sym.r' field */
295 #define FUNC_CDECL 0 /* standard c call */
296 #define FUNC_STDCALL 1 /* pascal c call */
297 #define FUNC_FASTCALL1 2 /* first param in %eax */
298 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
299 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
300 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
302 /* field 'Sym.t' for macros */
303 #define MACRO_OBJ 0 /* object like macro */
304 #define MACRO_FUNC 1 /* function like macro */
306 /* field 'Sym.r' for C labels */
307 #define LABEL_DEFINED 0 /* label is defined */
308 #define LABEL_FORWARD 1 /* label is forward defined */
309 #define LABEL_DECLARED 2 /* label is declared but never used */
311 /* type_decl() types */
312 #define TYPE_ABSTRACT 1 /* type without variable */
313 #define TYPE_DIRECT 2 /* type with variable */
315 #define IO_BUF_SIZE 8192
317 typedef struct BufferedFile
{
321 int line_num
; /* current line number - here to simplify code */
322 int ifndef_macro
; /* #ifndef macro / #endif search */
323 int ifndef_macro_saved
; /* saved ifndef_macro */
324 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
325 char inc_type
; /* type of include */
326 char inc_filename
[512]; /* filename specified by the user */
327 char filename
[1024]; /* current filename - here to simplify code */
328 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
331 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
332 #define CH_EOF (-1) /* end of file */
334 /* parsing state (used to save parser state to reparse part of the
335 source several times) */
336 typedef struct ParseState
{
337 const int *macro_ptr
;
343 /* used to record tokens */
344 typedef struct TokenString
{
351 /* inline functions */
352 typedef struct InlineFunc
{
358 /* include file cache, used to find files faster and also to eliminate
359 inclusion if the include file is protected by #ifndef ... #endif */
360 typedef struct CachedInclude
{
362 int hash_next
; /* -1 if none */
363 char type
; /* '"' or '>' to give include type */
364 char filename
[1]; /* path specified in #include */
367 #define CACHED_INCLUDES_HASH_SIZE 512
369 #ifdef CONFIG_TCC_ASM
370 typedef struct ExprValue
{
375 #define MAX_ASM_OPERANDS 30
376 typedef struct ASMOperand
{
377 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
379 char asm_str
[16]; /* computed asm string for operand */
380 SValue
*vt
; /* C value of the expression */
381 int ref_index
; /* if >= 0, gives reference to a output constraint */
382 int input_index
; /* if >= 0, gives reference to an input constraint */
383 int priority
; /* priority, used to assign registers */
384 int reg
; /* if >= 0, register number used for this operand */
385 int is_llong
; /* true if double register value */
386 int is_memory
; /* true if memory operand */
387 int is_rw
; /* for '+' modifier */
394 BufferedFile
**include_stack_ptr
;
395 int *ifdef_stack_ptr
;
397 /* include file handling */
398 char **include_paths
;
399 int nb_include_paths
;
400 char **sysinclude_paths
;
401 int nb_sysinclude_paths
;
402 CachedInclude
**cached_includes
;
403 int nb_cached_includes
;
405 char **library_paths
;
406 int nb_library_paths
;
408 /* array of all loaded dlls (including those referenced by loaded
410 DLLReference
**loaded_dlls
;
415 int nb_sections
; /* number of sections, including first dummy section */
417 Section
**priv_sections
;
418 int nb_priv_sections
; /* number of private sections */
423 unsigned long *got_offsets
;
425 /* give the correspondance from symtab indexes to dynsym indexes */
426 int *symtab_to_dynsym
;
428 /* temporary dynamic symbol sections (for dll loading) */
429 Section
*dynsymtab_section
;
430 /* exported dynamic symbol section */
433 int nostdinc
; /* if true, no standard headers are added */
434 int nostdlib
; /* if true, no standard libraries are added */
435 int nocommon
; /* if true, do not use common symbols for .bss data */
437 /* if true, static linking is performed */
440 /* soname as specified on the command line (-soname) */
442 /* rpath as specified on the command line (-Wl,-rpath=) */
445 /* if true, all symbols are exported */
448 /* if true, only link in referenced objects from archive */
451 /* address of text section */
452 unsigned long text_addr
;
455 /* output format, see TCC_OUTPUT_FORMAT_xxx */
458 /* C language options */
459 int char_is_unsigned
;
460 int leading_underscore
;
462 /* warning switches */
463 int warn_write_strings
;
464 int warn_unsupported
;
467 int warn_implicit_function_declaration
;
469 /* display some information during compilation */
471 /* compile with debug symbol (and use them if error during execution) */
473 #ifdef CONFIG_TCC_BCHECK
474 /* compile with built-in memory and bounds checker */
477 /* give the path of the tcc libraries */
482 void (*error_func
)(void *opaque
, const char *msg
);
483 int error_set_jmp_enabled
;
484 jmp_buf error_jmp_buf
;
487 /* tiny assembler state */
490 /* see include_stack_ptr */
491 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
493 /* see ifdef_stack_ptr */
494 int ifdef_stack
[IFDEF_STACK_SIZE
];
496 /* see cached_includes */
497 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
500 int pack_stack
[PACK_STACK_SIZE
];
503 /* output file for preprocessing */
506 /* for tcc_relocate */
510 struct InlineFunc
**inline_fns
;
513 #ifdef TCC_TARGET_I386
517 /* section alignment */
518 unsigned long section_align
;
523 unsigned long pe_file_align
;
526 int sym_1
, sym_2
, offs_1
;
530 #ifndef TCC_TARGET_PE
531 #ifdef TCC_TARGET_X86_64
532 /* write PLT and GOT here */
533 char *runtime_plt_and_got
;
534 unsigned int runtime_plt_and_got_offset
;
539 /* The current value can be: */
540 #define VT_VALMASK 0x00ff
541 #define VT_CONST 0x00f0 /* constant in vc
542 (must be first non register value) */
543 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
544 #define VT_LOCAL 0x00f2 /* offset on stack */
545 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
546 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
547 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
548 #define VT_LVAL 0x0100 /* var is an lvalue */
549 #define VT_SYM 0x0200 /* a symbol value is added */
550 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
551 char/short stored in integer registers) */
552 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
553 dereferencing value */
554 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
555 bounding function call point is in vc */
556 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
557 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
558 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
559 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
562 #define VT_INT 0 /* integer type */
563 #define VT_BYTE 1 /* signed byte type */
564 #define VT_SHORT 2 /* short type */
565 #define VT_VOID 3 /* void type */
566 #define VT_PTR 4 /* pointer */
567 #define VT_ENUM 5 /* enum definition */
568 #define VT_FUNC 6 /* function type */
569 #define VT_STRUCT 7 /* struct/union definition */
570 #define VT_FLOAT 8 /* IEEE float */
571 #define VT_DOUBLE 9 /* IEEE double */
572 #define VT_LDOUBLE 10 /* IEEE long double */
573 #define VT_BOOL 11 /* ISOC99 boolean type */
574 #define VT_LLONG 12 /* 64 bit integer */
575 #define VT_LONG 13 /* long integer (NEVER USED as type, only
577 #define VT_BTYPE 0x000f /* mask for basic type */
578 #define VT_UNSIGNED 0x0010 /* unsigned type */
579 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
580 #define VT_BITFIELD 0x0040 /* bitfield modifier */
581 #define VT_CONSTANT 0x0800 /* const modifier */
582 #define VT_VOLATILE 0x1000 /* volatile modifier */
583 #define VT_SIGNED 0x2000 /* signed type */
586 #define VT_EXTERN 0x00000080 /* extern definition */
587 #define VT_STATIC 0x00000100 /* static variable */
588 #define VT_TYPEDEF 0x00000200 /* typedef definition */
589 #define VT_INLINE 0x00000400 /* inline definition */
590 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
591 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
593 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
595 /* type mask (except storage) */
596 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT)
597 #define VT_TYPE (~(VT_STORAGE))
601 /* warning: the following compare tokens depend on i386 asm code */
608 #define TOK_Nset 0x98
609 #define TOK_Nclear 0x99
615 #define TOK_LAND 0xa0
619 #define TOK_MID 0xa3 /* inc/dec, to void constant */
621 #define TOK_UDIV 0xb0 /* unsigned division */
622 #define TOK_UMOD 0xb1 /* unsigned modulo */
623 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
624 #define TOK_CINT 0xb3 /* number in tokc */
625 #define TOK_CCHAR 0xb4 /* char constant in tokc */
626 #define TOK_STR 0xb5 /* pointer to string in tokc */
627 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
628 #define TOK_LCHAR 0xb7
629 #define TOK_LSTR 0xb8
630 #define TOK_CFLOAT 0xb9 /* float constant */
631 #define TOK_LINENUM 0xba /* line number info */
632 #define TOK_CDOUBLE 0xc0 /* double constant */
633 #define TOK_CLDOUBLE 0xc1 /* long double constant */
634 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
635 #define TOK_ADDC1 0xc3 /* add with carry generation */
636 #define TOK_ADDC2 0xc4 /* add with carry use */
637 #define TOK_SUBC1 0xc5 /* add with carry generation */
638 #define TOK_SUBC2 0xc6 /* add with carry use */
639 #define TOK_CUINT 0xc8 /* unsigned int constant */
640 #define TOK_CLLONG 0xc9 /* long long constant */
641 #define TOK_CULLONG 0xca /* unsigned long long constant */
642 #define TOK_ARROW 0xcb
643 #define TOK_DOTS 0xcc /* three dots */
644 #define TOK_SHR 0xcd /* unsigned shift right */
645 #define TOK_PPNUM 0xce /* preprocessor number */
647 #define TOK_SHL 0x01 /* shift left */
648 #define TOK_SAR 0x02 /* signed shift right */
650 /* assignement operators : normal operator or 0x80 */
651 #define TOK_A_MOD 0xa5
652 #define TOK_A_AND 0xa6
653 #define TOK_A_MUL 0xaa
654 #define TOK_A_ADD 0xab
655 #define TOK_A_SUB 0xad
656 #define TOK_A_DIV 0xaf
657 #define TOK_A_XOR 0xde
658 #define TOK_A_OR 0xfc
659 #define TOK_A_SHL 0x81
660 #define TOK_A_SAR 0x82
663 #define offsetof(type, field) ((size_t) &((type *)0)->field)
667 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
670 #define TOK_EOF (-1) /* end of file */
671 #define TOK_LINEFEED 10 /* line feed */
673 /* all identificators and strings have token above that */
674 #define TOK_IDENT 256
676 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
677 #define TOK_ASM_int TOK_INT
679 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
680 /* only used for i386 asm opcodes definitions */
682 DEF(TOK_ASM_ ## x ## b, #x "b") \
683 DEF(TOK_ASM_ ## x ## w, #x "w") \
684 DEF(TOK_ASM_ ## x ## l, #x "l") \
685 DEF(TOK_ASM_ ## x, #x)
687 DEF(TOK_ASM_ ## x ## w, #x "w") \
688 DEF(TOK_ASM_ ## x ## l, #x "l") \
689 DEF(TOK_ASM_ ## x, #x)
690 #ifdef TCC_TARGET_X86_64
691 # define DEF_BWLQ(x) \
692 DEF(TOK_ASM_ ## x ## b, #x "b") \
693 DEF(TOK_ASM_ ## x ## w, #x "w") \
694 DEF(TOK_ASM_ ## x ## l, #x "l") \
695 DEF(TOK_ASM_ ## x ## q, #x "q") \
696 DEF(TOK_ASM_ ## x, #x)
697 # define DEF_WLQ(x) \
698 DEF(TOK_ASM_ ## x ## w, #x "w") \
699 DEF(TOK_ASM_ ## x ## l, #x "l") \
700 DEF(TOK_ASM_ ## x ## q, #x "q") \
701 DEF(TOK_ASM_ ## x, #x)
702 # define DEF_BWLX DEF_BWLQ
703 # define DEF_WLX DEF_WLQ
704 /* number of sizes + 1 */
707 # define DEF_BWLX DEF_BWL
708 # define DEF_WLX DEF_WL
709 /* number of sizes + 1 */
714 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
715 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
716 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
717 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
720 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
721 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
724 #define DEF_ASMTEST(x) \
756 #endif // defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
759 TOK_LAST
= TOK_IDENT
- 1,
760 #define DEF(id, str) id,
765 #define TOK_UIDENT TOK_DEFINE
768 #define snprintf _snprintf
769 #define vsnprintf _vsnprintf
771 #define strtold (long double)strtod
772 #define strtof (float)strtod
773 #define strtoll (long long)strtol
775 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
776 || defined(__OpenBSD__)
777 /* currently incorrect */
778 static inline long double strtold(const char *nptr
, char **endptr
)
780 return (long double)strtod(nptr
, endptr
);
782 static inline float strtof(const char *nptr
, char **endptr
)
784 return (float)strtod(nptr
, endptr
);
787 /* XXX: need to define this to use them in non ISOC99 context */
788 extern float strtof (const char *__nptr
, char **__endptr
);
789 extern long double strtold (const char *__nptr
, char **__endptr
);
793 #define IS_PATHSEP(c) (c == '/' || c == '\\')
794 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
795 #define PATHCMP stricmp
797 #define IS_PATHSEP(c) (c == '/')
798 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
799 #define PATHCMP strcmp
802 /* space exlcuding newline */
803 static inline int is_space(int ch
)
805 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
808 static inline int isid(int c
)
810 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') || c
== '_';
813 static inline int isnum(int c
)
815 return c
>= '0' && c
<= '9';
818 static inline int isoct(int c
)
820 return c
>= '0' && c
<= '7';
823 static inline int toup(int c
)
825 return (c
>= 'a' && c
<= 'z') ? c
- 'a' + 'A' : c
;
831 #define ST_INLN static inline
832 #define ST_FUNC static
833 #define ST_DATA static
837 #define ST_DATA extern
840 /* ------------ libtcc.c ------------ */
842 /* use GNU C extensions */
844 /* use Tiny C extensions */
846 /* XXX: get rid of this ASAP */
847 ST_DATA
struct TCCState
*tcc_state
;
849 #ifdef CONFIG_TCC_BACKTRACE
850 ST_DATA
int num_callers
;
851 ST_DATA
const char **rt_bound_error_msg
;
852 ST_DATA
void *rt_prog_main
;
855 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
856 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
857 #define AFF_PREPROCESS 0x0004 /* preprocess file */
859 /* public functions currently used by the tcc main function */
860 PUB_FUNC
char *pstrcpy(char *buf
, int buf_size
, const char *s
);
861 PUB_FUNC
char *pstrcat(char *buf
, int buf_size
, const char *s
);
862 PUB_FUNC
char *tcc_basename(const char *name
);
863 PUB_FUNC
char *tcc_fileextension (const char *name
);
864 PUB_FUNC
void tcc_free(void *ptr
);
865 PUB_FUNC
void *tcc_malloc(unsigned long size
);
866 PUB_FUNC
void *tcc_mallocz(unsigned long size
);
867 PUB_FUNC
void *tcc_realloc(void *ptr
, unsigned long size
);
868 PUB_FUNC
char *tcc_strdup(const char *str
);
869 #define free(p) use_tcc_free(p)
870 #define malloc(s) use_tcc_malloc(s)
871 #define realloc(p, s) use_tcc_realloc(p, s)
873 #define strdup(s) use_tcc_strdup(s)
874 PUB_FUNC
void tcc_memstats(void);
875 PUB_FUNC
void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
876 PUB_FUNC
void dynarray_reset(void *pp
, int *n
);
877 PUB_FUNC
void error_noabort(const char *fmt
, ...);
878 PUB_FUNC
void error(const char *fmt
, ...);
879 PUB_FUNC
void expect(const char *msg
);
880 PUB_FUNC
void warning(const char *fmt
, ...);
882 /* other utilities */
883 ST_INLN
void cstr_ccat(CString
*cstr
, int ch
);
884 ST_FUNC
void cstr_cat(CString
*cstr
, const char *str
);
885 ST_FUNC
void cstr_wccat(CString
*cstr
, int ch
);
886 ST_FUNC
void cstr_new(CString
*cstr
);
887 ST_FUNC
void cstr_free(CString
*cstr
);
888 ST_FUNC
void add_char(CString
*cstr
, int c
);
889 #define cstr_reset(cstr) cstr_free(cstr)
891 ST_FUNC Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
);
892 ST_FUNC
void section_realloc(Section
*sec
, unsigned long new_size
);
893 ST_FUNC
void *section_ptr_add(Section
*sec
, unsigned long size
);
894 ST_FUNC Section
*find_section(TCCState
*s1
, const char *name
);
896 ST_FUNC
void put_extern_sym2(Sym
*sym
, Section
*section
, unsigned long value
, unsigned long size
, int can_add_underscore
);
897 ST_FUNC
void put_extern_sym(Sym
*sym
, Section
*section
, unsigned long value
, unsigned long size
);
898 ST_FUNC
void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
);
900 ST_INLN
void sym_free(Sym
*sym
);
901 ST_FUNC Sym
*sym_push2(Sym
**ps
, int v
, int t
, long c
);
902 ST_FUNC Sym
*sym_find2(Sym
*s
, int v
);
903 ST_FUNC Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
904 ST_FUNC
void sym_pop(Sym
**ptop
, Sym
*b
);
905 ST_INLN Sym
*struct_find(int v
);
906 ST_INLN Sym
*sym_find(int v
);
907 ST_FUNC Sym
*global_identifier_push(int v
, int t
, int c
);
909 ST_FUNC BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
);
910 ST_FUNC
void tcc_close(BufferedFile
*bf
);
911 ST_FUNC
int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
912 PUB_FUNC
int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
);
913 PUB_FUNC
void tcc_print_stats(TCCState
*s
, int64_t total_time
);
914 PUB_FUNC
void set_num_callers(int n
);
916 ST_FUNC
int ieee_finite(double d
);
918 /* ------------ tccpp.c ------------ */
920 ST_DATA
struct BufferedFile
*file
;
923 ST_DATA
const int *macro_ptr
;
924 ST_DATA
int parse_flags
;
925 ST_DATA
int tok_flags
;
926 ST_DATA CString tokcstr
; /* current parsed string, if any */
928 /* display benchmark infos */
929 ST_DATA
int total_lines
;
930 ST_DATA
int total_bytes
;
931 ST_DATA
int tok_ident
;
932 ST_DATA TokenSym
**table_ident
;
934 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
935 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
936 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
937 #define TOK_FLAG_EOF 0x0008 /* end of file */
939 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
940 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
941 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
942 token. line feed is also
944 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
945 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
947 ST_FUNC TokenSym
*tok_alloc(const char *str
, int len
);
948 ST_FUNC
char *get_tok_str(int v
, CValue
*cv
);
949 ST_FUNC
void save_parse_state(ParseState
*s
);
950 ST_FUNC
void restore_parse_state(ParseState
*s
);
951 ST_INLN
void tok_str_new(TokenString
*s
);
952 ST_FUNC
void tok_str_free(int *str
);
953 ST_FUNC
void tok_str_add(TokenString
*s
, int t
);
954 ST_FUNC
void tok_str_add_tok(TokenString
*s
);
955 ST_INLN
void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
);
956 ST_FUNC
void define_undef(Sym
*s
);
957 ST_INLN Sym
*define_find(int v
);
958 ST_FUNC
void free_defines(Sym
*b
);
959 ST_FUNC Sym
*label_find(int v
);
960 ST_FUNC Sym
*label_push(Sym
**ptop
, int v
, int flags
);
961 ST_FUNC
void label_pop(Sym
**ptop
, Sym
*slast
);
962 ST_FUNC
void parse_define(void);
963 ST_FUNC
void preprocess(int is_bof
);
964 ST_FUNC
void next_nomacro(void);
965 ST_FUNC
void next(void);
966 ST_INLN
void unget_tok(int last_tok
);
967 ST_FUNC
void preprocess_init(TCCState
*s1
);
968 ST_FUNC
void preprocess_new();
969 ST_FUNC
int tcc_preprocess(TCCState
*s1
);
970 ST_FUNC
void skip(int c
);
972 /* ------------ tccgen.c ------------ */
974 ST_DATA Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
975 ST_DATA Section
*cur_text_section
; /* current section where function code is generated */
976 #ifdef CONFIG_TCC_ASM
977 ST_DATA Section
*last_text_section
; /* to handle .previous asm directive */
979 #ifdef CONFIG_TCC_BCHECK
980 /* bound check related sections */
981 ST_DATA Section
*bounds_section
; /* contains global data bound description */
982 ST_DATA Section
*lbounds_section
; /* contains local data bound description */
984 /* symbol sections */
985 ST_DATA Section
*symtab_section
, *strtab_section
;
987 ST_DATA Section
*stab_section
, *stabstr_section
;
989 #define SYM_POOL_NB (8192 / sizeof(Sym))
990 ST_DATA Sym
*sym_free_first
;
991 ST_DATA
void **sym_pools
;
992 ST_DATA
int nb_sym_pools
;
994 ST_DATA Sym
*global_stack
;
995 ST_DATA Sym
*local_stack
;
996 ST_DATA Sym
*local_label_stack
;
997 ST_DATA Sym
*global_label_stack
;
998 ST_DATA Sym
*define_stack
;
999 ST_DATA CType char_pointer_type
, func_old_type
, int_type
;
1000 ST_DATA SValue vstack
[VSTACK_SIZE
], *vtop
;
1001 ST_DATA
int rsym
, anon_sym
, ind
, loc
;
1003 ST_DATA
int const_wanted
; /* true if constant wanted */
1004 ST_DATA
int nocode_wanted
; /* true if no code generation wanted for an expression */
1005 ST_DATA
int global_expr
; /* true if compound literals must be allocated globally (used during initializers parsing */
1006 ST_DATA CType func_vt
; /* current function return type (used by return instruction) */
1007 ST_DATA
int func_vc
;
1008 ST_DATA
int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
1009 ST_DATA
char *funcname
;
1011 ST_INLN
int is_float(int t
);
1012 ST_FUNC
void test_lvalue(void);
1013 ST_FUNC
void swap(int *p
, int *q
);
1014 ST_FUNC
void vpushi(int v
);
1015 ST_FUNC Sym
*external_global_sym(int v
, CType
*type
, int r
);
1016 ST_FUNC
void vset(CType
*type
, int r
, int v
);
1017 ST_FUNC
void vswap(void);
1018 ST_FUNC
void vpush_global_sym(CType
*type
, int v
);
1019 ST_FUNC
void vrott(int n
);
1020 #ifdef TCC_TARGET_ARM
1021 ST_FUNC
int get_reg_ex(int rc
, int rc2
);
1022 ST_FUNC
void vnrott(int n
);
1023 ST_FUNC
void lexpand_nr(void);
1025 ST_FUNC
void vpushv(SValue
*v
);
1026 ST_FUNC
void save_reg(int r
);
1027 ST_FUNC
int get_reg(int rc
);
1028 ST_FUNC
void save_regs(int n
);
1029 ST_FUNC
int gv(int rc
);
1030 ST_FUNC
void gv2(int rc1
, int rc2
);
1031 ST_FUNC
void vpop(void);
1032 ST_FUNC
void gen_op(int op
);
1033 ST_FUNC
int type_size(CType
*type
, int *a
);
1034 ST_FUNC
void mk_pointer(CType
*type
);
1035 ST_FUNC
void vstore(void);
1036 ST_FUNC
void inc(int post
, int c
);
1037 ST_FUNC
int lvalue_type(int t
);
1038 ST_FUNC
void indir(void);
1039 ST_FUNC
void unary(void);
1040 ST_FUNC
void expr_prod(void);
1041 ST_FUNC
void expr_sum(void);
1042 ST_FUNC
void gexpr(void);
1043 ST_FUNC
int expr_const(void);
1044 ST_FUNC
void gen_inline_functions(void);
1045 ST_FUNC
void decl(int l
);
1046 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1047 ST_FUNC Sym
*get_sym_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
);
1050 /* ------------ tccelf.c ------------ */
1052 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1055 unsigned int n_strx
; /* index into string table of name */
1056 unsigned char n_type
; /* type of symbol */
1057 unsigned char n_other
; /* misc info (usually empty) */
1058 unsigned short n_desc
; /* description field */
1059 unsigned int n_value
; /* value of symbol */
1062 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
);
1064 ST_FUNC
int put_elf_str(Section
*s
, const char *sym
);
1065 ST_FUNC
int put_elf_sym(Section
*s
, uplong value
, unsigned long size
, int info
, int other
, int shndx
, const char *name
);
1066 ST_FUNC
int add_elf_sym(Section
*s
, uplong value
, unsigned long size
, int info
, int other
, int sh_num
, const char *name
);
1067 ST_FUNC
int find_elf_sym(Section
*s
, const char *name
);
1068 ST_FUNC
void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
, int type
, int symbol
);
1070 ST_FUNC
void put_stabs(const char *str
, int type
, int other
, int desc
, unsigned long value
);
1071 ST_FUNC
void put_stabs_r(const char *str
, int type
, int other
, int desc
, unsigned long value
, Section
*sec
, int sym_index
);
1072 ST_FUNC
void put_stabn(int type
, int other
, int desc
, int value
);
1073 ST_FUNC
void put_stabd(int type
, int other
, int desc
);
1075 ST_FUNC
void relocate_common_syms(void);
1076 ST_FUNC
void relocate_syms(TCCState
*s1
, int do_resolve
);
1077 ST_FUNC
void relocate_section(TCCState
*s1
, Section
*s
);
1079 ST_FUNC
void tcc_add_linker_symbols(TCCState
*s1
);
1080 ST_FUNC
int tcc_load_object_file(TCCState
*s1
, int fd
, unsigned long file_offset
);
1081 ST_FUNC
int tcc_load_archive(TCCState
*s1
, int fd
);
1082 ST_FUNC
void *tcc_get_symbol_err(TCCState
*s
, const char *name
);
1083 ST_FUNC
void tcc_add_bcheck(TCCState
*s1
);
1085 ST_FUNC
void build_got_entries(TCCState
*s1
);
1086 ST_FUNC
void tcc_add_runtime(TCCState
*s1
);
1088 #ifndef TCC_TARGET_PE
1089 ST_FUNC
int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
);
1090 ST_FUNC
int tcc_load_ldscript(TCCState
*s1
);
1091 ST_FUNC
uint8_t *parse_comment(uint8_t *p
);
1092 ST_FUNC
void minp(void);
1093 ST_INLN
void inp(void);
1094 ST_FUNC
int handle_eob(void);
1097 /* ------------ xxx-gen.c ------------ */
1099 ST_FUNC
void gsym_addr(int t
, int a
);
1100 ST_FUNC
void gsym(int t
);
1101 ST_FUNC
void load(int r
, SValue
*sv
);
1102 ST_FUNC
void store(int r
, SValue
*v
);
1103 ST_FUNC
void gfunc_call(int nb_args
);
1104 ST_FUNC
void gfunc_prolog(CType
*func_type
);
1105 ST_FUNC
void gfunc_epilog(void);
1106 ST_FUNC
int gjmp(int t
);
1107 ST_FUNC
void gjmp_addr(int a
);
1108 ST_FUNC
int gtst(int inv
, int t
);
1109 ST_FUNC
void gen_opi(int op
);
1110 ST_FUNC
void gen_opf(int op
);
1111 ST_FUNC
void gen_cvt_ftoi(int t
);
1112 ST_FUNC
void gen_cvt_ftof(int t
);
1113 ST_FUNC
void ggoto(void);
1114 #ifndef TCC_TARGET_C67
1115 ST_FUNC
void o(unsigned int c
);
1117 #ifndef TCC_TARGET_ARM
1118 ST_FUNC
void gen_cvt_itof(int t
);
1121 /* ------------ i386-gen.c ------------ */
1122 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1123 ST_FUNC
void g(int c
);
1124 ST_FUNC
int oad(int c
, int s
);
1125 ST_FUNC
void gen_le16(int c
);
1126 ST_FUNC
void gen_le32(int c
);
1127 ST_FUNC
void gen_addr32(int r
, Sym
*sym
, int c
);
1128 ST_FUNC
void gen_addrpc32(int r
, Sym
*sym
, int c
);
1131 #ifdef CONFIG_TCC_BCHECK
1132 ST_FUNC
void gen_bounded_ptr_add(void);
1133 ST_FUNC
void gen_bounded_ptr_deref(void);
1136 /* ------------ x86_64-gen.c ------------ */
1137 #ifdef TCC_TARGET_X86_64
1138 ST_FUNC
void gen_addr64(int r
, Sym
*sym
, int64_t c
);
1141 /* ------------ arm-gen.c ------------ */
1142 #ifdef TCC_TARGET_ARM
1143 ST_FUNC
unsigned long encbranch(int pos
,int addr
,int fail
);
1144 ST_FUNC
void gen_cvt_itof1(int t
);
1147 /* ------------ c67-gen.c ------------ */
1148 #ifdef TCC_TARGET_C67
1151 /* ------------ tcccoff.c ------------ */
1153 #ifdef TCC_TARGET_COFF
1154 ST_FUNC
int tcc_output_coff(TCCState
*s1
, FILE *f
);
1155 ST_FUNC
int tcc_load_coff(TCCState
* s1
, int fd
);
1158 /* ------------ tccasm.c ------------ */
1159 ST_FUNC
void asm_instr(void);
1160 ST_FUNC
void asm_global_instr(void);
1162 #ifdef CONFIG_TCC_ASM
1163 ST_FUNC
int find_constraint(ASMOperand
*operands
, int nb_operands
, const char *name
, const char **pp
);
1164 ST_FUNC
void asm_expr(TCCState
*s1
, ExprValue
*pe
);
1165 ST_FUNC
int asm_int_expr(TCCState
*s1
);
1166 ST_FUNC
int tcc_assemble(TCCState
*s1
, int do_preprocess
);
1167 /* ------------ i386-asm.c ------------ */
1168 ST_FUNC
void gen_expr32(ExprValue
*pe
);
1169 ST_FUNC
void asm_opcode(TCCState
*s1
, int opcode
);
1170 ST_FUNC
void asm_compute_constraints(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, const uint8_t *clobber_regs
, int *pout_reg
);
1171 ST_FUNC
void subst_asm_operand(CString
*add_str
, SValue
*sv
, int modifier
);
1172 ST_FUNC
void asm_gen_code(ASMOperand
*operands
, int nb_operands
, int nb_outputs
, int is_output
, uint8_t *clobber_regs
, int out_reg
);
1173 ST_FUNC
void asm_clobber(uint8_t *clobber_regs
, const char *str
);
1175 /* ------------ tccpe.c -------------- */
1176 #ifdef TCC_TARGET_PE
1177 ST_FUNC
int pe_load_file(struct TCCState
*s1
, const char *filename
, int fd
);
1178 ST_FUNC
int pe_add_dll(struct TCCState
*s
, const char *libraryname
);
1179 ST_FUNC
int pe_output_file(TCCState
* s1
, const char *filename
);
1180 ST_FUNC
int pe_putimport(TCCState
*s1
, int dllindex
, const char *name
, const void *value
);
1181 ST_FUNC SValue
*pe_getimport(SValue
*sv
, SValue
*v2
);
1183 ST_FUNC
char *get_export_names(FILE *fp
);
1184 #ifdef TCC_TARGET_X86_64
1185 ST_FUNC
void pe_add_unwind_data(unsigned start
, unsigned end
, unsigned stack
);
1189 /* ------------ tccrun.c ----------------- */
1190 #ifdef CONFIG_TCC_STATIC
1191 #define RTLD_LAZY 0x001
1192 #define RTLD_NOW 0x002
1193 #define RTLD_GLOBAL 0x100
1194 #define RTLD_DEFAULT NULL
1195 /* dummy function for profiling */
1196 ST_FUNC
void *dlopen(const char *filename
, int flag
);
1197 ST_FUNC
void dlclose(void *p
);
1198 //ST_FUNC const char *dlerror(void);
1199 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1200 #elif !defined TCC_TARGET_PE || !defined _WIN32
1201 ST_FUNC
void *resolve_sym(TCCState
*s1
, const char *symbol
);
1203 /********************************************************/
1204 /* include the target specific definitions */
1206 #define TARGET_DEFS_ONLY
1207 #ifdef TCC_TARGET_I386
1208 #include "i386-gen.c"
1210 #ifdef TCC_TARGET_X86_64
1211 #include "x86_64-gen.c"
1213 #ifdef TCC_TARGET_ARM
1214 #include "arm-gen.c"
1216 #ifdef TCC_TARGET_C67
1218 #include "c67-gen.c"
1220 #undef TARGET_DEFS_ONLY
1222 ST_DATA
const int reg_classes
[NB_REGS
];
1224 /********************************************************/
1227 #define ST_DATA static
1231 /********************************************************/