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
40 #include <sys/timeb.h>
41 #include <io.h> /* open, close etc. */
42 #include <direct.h> /* getcwd */
43 #define inline __inline
46 #define __aligned(n) __declspec(align(n))
49 #define uplong unsigned long long
64 #define uplong unsigned long
68 #define __aligned(n) __attribute__((aligned(n)))
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 #define CONFIG_TCC_BACKTRACE
135 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
136 executables or dlls */
137 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
139 #define INCLUDE_STACK_SIZE 32
140 #define IFDEF_STACK_SIZE 64
141 #define VSTACK_SIZE 256
142 #define STRING_MAX_SIZE 1024
143 #define PACK_STACK_SIZE 8
145 #define TOK_HASH_SIZE 8192 /* must be a power of two */
146 #define TOK_ALLOC_INCR 512 /* must be a power of two */
147 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
149 /* token symbol management */
150 typedef struct TokenSym
{
151 struct TokenSym
*hash_next
;
152 struct Sym
*sym_define
; /* direct pointer to define */
153 struct Sym
*sym_label
; /* direct pointer to label */
154 struct Sym
*sym_struct
; /* direct pointer to structure */
155 struct Sym
*sym_identifier
; /* direct pointer to identifier */
156 int tok
; /* token number */
162 typedef unsigned short nwchar_t
;
164 typedef int nwchar_t
;
167 typedef struct CString
{
168 int size
; /* size in bytes */
169 void *data
; /* either 'char *' or 'nwchar_t *' */
171 void *data_allocated
; /* if non NULL, data has been malloced */
174 /* type definition */
175 typedef struct CType
{
181 typedef union CValue
{
187 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
189 unsigned long long ull
;
190 struct CString
*cstr
;
196 typedef struct SValue
{
197 CType type
; /* type */
198 unsigned short r
; /* register + flags */
199 unsigned short r2
; /* second register, used for 'long long'
200 type. If not used, set to VT_CONST */
201 CValue c
; /* constant, if VT_CONST */
202 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
205 /* symbol management */
207 int v
; /* symbol token */
208 long r
; /* associated register */
210 long c
; /* associated number */
211 int *d
; /* define token stream */
213 CType type
; /* associated type */
215 struct Sym
*next
; /* next related symbol */
216 long jnext
; /* next jump label */
218 struct Sym
*prev
; /* prev symbol in stack */
219 struct Sym
*prev_tok
; /* previous symbol for this token */
222 /* section definition */
223 /* XXX: use directly ELF structure for parameters ? */
224 /* special flag to indicate that the section should not be linked to
226 #define SHF_PRIVATE 0x80000000
228 /* special flag, too */
229 #define SECTION_ABS ((void *)1)
231 typedef struct Section
{
232 unsigned long data_offset
; /* current data offset */
233 unsigned char *data
; /* section data */
234 unsigned long data_allocated
; /* used for realloc() handling */
235 int sh_name
; /* elf section name (only used during output) */
236 int sh_num
; /* elf section number */
237 int sh_type
; /* elf section type */
238 int sh_flags
; /* elf section flags */
239 int sh_info
; /* elf section info */
240 int sh_addralign
; /* elf section alignment */
241 int sh_entsize
; /* elf entry size */
242 unsigned long sh_size
; /* section size (only used during output) */
243 unsigned long sh_addr
; /* address at which the section is relocated */
244 unsigned long sh_offset
; /* file offset */
245 int nb_hashed_syms
; /* used to resize the hash table */
246 struct Section
*link
; /* link to another section */
247 struct Section
*reloc
; /* corresponding section for relocation, if any */
248 struct Section
*hash
; /* hash table for symbols */
249 struct Section
*next
;
250 char name
[1]; /* section name */
253 typedef struct DLLReference
{
259 /* GNUC attribute definition */
260 typedef struct AttributeDef
{
264 int func_attr
; /* calling convention, exports, ... */
267 /* -------------------------------------------------- */
268 /* gr: wrappers for casting sym->r for other purposes */
277 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
278 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
279 #define FUNC_IMPORT(r) (((func_attr_t*)&(r))->func_import)
280 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
281 /* -------------------------------------------------- */
283 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
284 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
285 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
287 /* stored in 'Sym.c' field */
288 #define FUNC_NEW 1 /* ansi function prototype */
289 #define FUNC_OLD 2 /* old function prototype */
290 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
292 /* stored in 'Sym.r' field */
293 #define FUNC_CDECL 0 /* standard c call */
294 #define FUNC_STDCALL 1 /* pascal c call */
295 #define FUNC_FASTCALL1 2 /* first param in %eax */
296 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
297 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
298 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
300 /* field 'Sym.t' for macros */
301 #define MACRO_OBJ 0 /* object like macro */
302 #define MACRO_FUNC 1 /* function like macro */
304 /* field 'Sym.r' for C labels */
305 #define LABEL_DEFINED 0 /* label is defined */
306 #define LABEL_FORWARD 1 /* label is forward defined */
307 #define LABEL_DECLARED 2 /* label is declared but never used */
309 /* type_decl() types */
310 #define TYPE_ABSTRACT 1 /* type without variable */
311 #define TYPE_DIRECT 2 /* type with variable */
313 #define IO_BUF_SIZE 8192
315 typedef struct BufferedFile
{
319 int line_num
; /* current line number - here to simplify code */
320 int ifndef_macro
; /* #ifndef macro / #endif search */
321 int ifndef_macro_saved
; /* saved ifndef_macro */
322 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
323 char inc_type
; /* type of include */
324 char inc_filename
[512]; /* filename specified by the user */
325 char filename
[1024]; /* current filename - here to simplify code */
326 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
329 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
330 #define CH_EOF (-1) /* end of file */
332 /* parsing state (used to save parser state to reparse part of the
333 source several times) */
334 typedef struct ParseState
{
341 /* used to record tokens */
342 typedef struct TokenString
{
349 /* inline functions */
350 typedef struct InlineFunc
{
356 /* include file cache, used to find files faster and also to eliminate
357 inclusion if the include file is protected by #ifndef ... #endif */
358 typedef struct CachedInclude
{
360 int hash_next
; /* -1 if none */
361 char type
; /* '"' or '>' to give include type */
362 char filename
[1]; /* path specified in #include */
365 #define CACHED_INCLUDES_HASH_SIZE 512
367 #ifdef CONFIG_TCC_ASM
368 typedef struct ExprValue
{
373 #define MAX_ASM_OPERANDS 30
374 typedef struct ASMOperand
{
375 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
377 char asm_str
[16]; /* computed asm string for operand */
378 SValue
*vt
; /* C value of the expression */
379 int ref_index
; /* if >= 0, gives reference to a output constraint */
380 int input_index
; /* if >= 0, gives reference to an input constraint */
381 int priority
; /* priority, used to assign registers */
382 int reg
; /* if >= 0, register number used for this operand */
383 int is_llong
; /* true if double register value */
384 int is_memory
; /* true if memory operand */
385 int is_rw
; /* for '+' modifier */
392 BufferedFile
**include_stack_ptr
;
393 int *ifdef_stack_ptr
;
395 /* include file handling */
396 char **include_paths
;
397 int nb_include_paths
;
398 char **sysinclude_paths
;
399 int nb_sysinclude_paths
;
400 CachedInclude
**cached_includes
;
401 int nb_cached_includes
;
403 char **library_paths
;
404 int nb_library_paths
;
406 /* array of all loaded dlls (including those referenced by loaded
408 DLLReference
**loaded_dlls
;
413 int nb_sections
; /* number of sections, including first dummy section */
415 Section
**priv_sections
;
416 int nb_priv_sections
; /* number of private sections */
421 unsigned long *got_offsets
;
423 /* give the correspondance from symtab indexes to dynsym indexes */
424 int *symtab_to_dynsym
;
426 /* temporary dynamic symbol sections (for dll loading) */
427 Section
*dynsymtab_section
;
428 /* exported dynamic symbol section */
431 int nostdinc
; /* if true, no standard headers are added */
432 int nostdlib
; /* if true, no standard libraries are added */
433 int nocommon
; /* if true, do not use common symbols for .bss data */
435 /* if true, static linking is performed */
438 /* soname as specified on the command line (-soname) */
441 /* if true, all symbols are exported */
444 /* if true, only link in referenced objects from archive */
447 /* address of text section */
448 unsigned long text_addr
;
451 /* output format, see TCC_OUTPUT_FORMAT_xxx */
454 /* C language options */
455 int char_is_unsigned
;
456 int leading_underscore
;
458 /* warning switches */
459 int warn_write_strings
;
460 int warn_unsupported
;
463 int warn_implicit_function_declaration
;
465 /* display some information during compilation */
467 /* compile with debug symbol (and use them if error during execution) */
469 /* compile with built-in memory and bounds checker */
471 /* give the path of the tcc libraries */
476 void (*error_func
)(void *opaque
, const char *msg
);
477 int error_set_jmp_enabled
;
481 jmp_buf error_jmp_buf
;
484 /* tiny assembler state */
487 /* see include_stack_ptr */
488 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
490 /* see ifdef_stack_ptr */
491 int ifdef_stack
[IFDEF_STACK_SIZE
];
493 /* see cached_includes */
494 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
497 int pack_stack
[PACK_STACK_SIZE
];
500 /* output file for preprocessing */
503 /* for tcc_relocate */
506 struct InlineFunc
**inline_fns
;
509 #ifdef TCC_TARGET_I386
513 /* section alignment */
514 unsigned long section_align
;
519 unsigned long pe_file_align
;
522 #ifndef TCC_TARGET_PE
523 #ifdef TCC_TARGET_X86_64
524 /* write PLT and GOT here */
525 char *runtime_plt_and_got
;
526 unsigned int runtime_plt_and_got_offset
;
531 /* The current value can be: */
532 #define VT_VALMASK 0x00ff
533 #define VT_CONST 0x00f0 /* constant in vc
534 (must be first non register value) */
535 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
536 #define VT_LOCAL 0x00f2 /* offset on stack */
537 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
538 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
539 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
540 #define VT_LVAL 0x0100 /* var is an lvalue */
541 #define VT_SYM 0x0200 /* a symbol value is added */
542 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
543 char/short stored in integer registers) */
544 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
545 dereferencing value */
546 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
547 bounding function call point is in vc */
548 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
549 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
550 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
551 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
554 #define VT_INT 0 /* integer type */
555 #define VT_BYTE 1 /* signed byte type */
556 #define VT_SHORT 2 /* short type */
557 #define VT_VOID 3 /* void type */
558 #define VT_PTR 4 /* pointer */
559 #define VT_ENUM 5 /* enum definition */
560 #define VT_FUNC 6 /* function type */
561 #define VT_STRUCT 7 /* struct/union definition */
562 #define VT_FLOAT 8 /* IEEE float */
563 #define VT_DOUBLE 9 /* IEEE double */
564 #define VT_LDOUBLE 10 /* IEEE long double */
565 #define VT_BOOL 11 /* ISOC99 boolean type */
566 #define VT_LLONG 12 /* 64 bit integer */
567 #define VT_LONG 13 /* long integer (NEVER USED as type, only
569 #define VT_BTYPE 0x000f /* mask for basic type */
570 #define VT_UNSIGNED 0x0010 /* unsigned type */
571 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
572 #define VT_BITFIELD 0x0040 /* bitfield modifier */
573 #define VT_CONSTANT 0x0800 /* const modifier */
574 #define VT_VOLATILE 0x1000 /* volatile modifier */
575 #define VT_SIGNED 0x2000 /* signed type */
578 #define VT_EXTERN 0x00000080 /* extern definition */
579 #define VT_STATIC 0x00000100 /* static variable */
580 #define VT_TYPEDEF 0x00000200 /* typedef definition */
581 #define VT_INLINE 0x00000400 /* inline definition */
582 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
584 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
586 /* type mask (except storage) */
587 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT)
588 #define VT_TYPE (~(VT_STORAGE))
592 /* warning: the following compare tokens depend on i386 asm code */
599 #define TOK_Nset 0x98
600 #define TOK_Nclear 0x99
606 #define TOK_LAND 0xa0
610 #define TOK_MID 0xa3 /* inc/dec, to void constant */
612 #define TOK_UDIV 0xb0 /* unsigned division */
613 #define TOK_UMOD 0xb1 /* unsigned modulo */
614 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
615 #define TOK_CINT 0xb3 /* number in tokc */
616 #define TOK_CCHAR 0xb4 /* char constant in tokc */
617 #define TOK_STR 0xb5 /* pointer to string in tokc */
618 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
619 #define TOK_LCHAR 0xb7
620 #define TOK_LSTR 0xb8
621 #define TOK_CFLOAT 0xb9 /* float constant */
622 #define TOK_LINENUM 0xba /* line number info */
623 #define TOK_CDOUBLE 0xc0 /* double constant */
624 #define TOK_CLDOUBLE 0xc1 /* long double constant */
625 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
626 #define TOK_ADDC1 0xc3 /* add with carry generation */
627 #define TOK_ADDC2 0xc4 /* add with carry use */
628 #define TOK_SUBC1 0xc5 /* add with carry generation */
629 #define TOK_SUBC2 0xc6 /* add with carry use */
630 #define TOK_CUINT 0xc8 /* unsigned int constant */
631 #define TOK_CLLONG 0xc9 /* long long constant */
632 #define TOK_CULLONG 0xca /* unsigned long long constant */
633 #define TOK_ARROW 0xcb
634 #define TOK_DOTS 0xcc /* three dots */
635 #define TOK_SHR 0xcd /* unsigned shift right */
636 #define TOK_PPNUM 0xce /* preprocessor number */
638 #define TOK_SHL 0x01 /* shift left */
639 #define TOK_SAR 0x02 /* signed shift right */
641 /* assignement operators : normal operator or 0x80 */
642 #define TOK_A_MOD 0xa5
643 #define TOK_A_AND 0xa6
644 #define TOK_A_MUL 0xaa
645 #define TOK_A_ADD 0xab
646 #define TOK_A_SUB 0xad
647 #define TOK_A_DIV 0xaf
648 #define TOK_A_XOR 0xde
649 #define TOK_A_OR 0xfc
650 #define TOK_A_SHL 0x81
651 #define TOK_A_SAR 0x82
654 #define offsetof(type, field) ((size_t) &((type *)0)->field)
658 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
661 #define TOK_EOF (-1) /* end of file */
662 #define TOK_LINEFEED 10 /* line feed */
664 /* all identificators and strings have token above that */
665 #define TOK_IDENT 256
667 /* only used for i386 asm opcodes definitions */
668 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
671 DEF(TOK_ASM_ ## x ## b, #x "b") \
672 DEF(TOK_ASM_ ## x ## w, #x "w") \
673 DEF(TOK_ASM_ ## x ## l, #x "l") \
674 DEF(TOK_ASM_ ## x, #x)
677 DEF(TOK_ASM_ ## x ## w, #x "w") \
678 DEF(TOK_ASM_ ## x ## l, #x "l") \
679 DEF(TOK_ASM_ ## x, #x)
681 #ifdef TCC_TARGET_X86_64
683 #define DEF_BWLQ(x) \
684 DEF(TOK_ASM_ ## x ## b, #x "b") \
685 DEF(TOK_ASM_ ## x ## w, #x "w") \
686 DEF(TOK_ASM_ ## x ## l, #x "l") \
687 DEF(TOK_ASM_ ## x ## q, #x "q") \
688 DEF(TOK_ASM_ ## x, #x)
691 DEF(TOK_ASM_ ## x ## w, #x "w") \
692 DEF(TOK_ASM_ ## x ## l, #x "l") \
693 DEF(TOK_ASM_ ## x ## q, #x "q") \
694 DEF(TOK_ASM_ ## x, #x)
699 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
700 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
701 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
702 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
705 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
706 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
709 #define DEF_ASMTEST(x) \
741 #define TOK_ASM_int TOK_INT
744 TOK_LAST
= TOK_IDENT
- 1,
745 #define DEF(id, str) id,
750 #define TOK_UIDENT TOK_DEFINE
753 #define snprintf _snprintf
754 #define vsnprintf _vsnprintf
756 #define strtold (long double)strtod
757 #define strtof (float)strtod
758 #define strtoll (long long)strtol
760 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
761 || defined(__OpenBSD__) || defined(__CYGWIN__)
762 /* currently incorrect */
763 long double strtold(const char *nptr
, char **endptr
);
764 float strtof(const char *nptr
, char **endptr
);
766 /* XXX: need to define this to use them in non ISOC99 context */
767 extern float strtof (const char *__nptr
, char **__endptr
);
768 extern long double strtold (const char *__nptr
, char **__endptr
);
772 #define IS_PATHSEP(c) (c == '/' || c == '\\')
773 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
774 #define PATHCMP stricmp
776 #define IS_PATHSEP(c) (c == '/')
777 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
778 #define PATHCMP strcmp
781 void error(const char *fmt
, ...);
782 void error_noabort(const char *fmt
, ...);
783 void warning(const char *fmt
, ...);
785 void tcc_set_lib_path_w32(TCCState
*s
);
786 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
);
787 void tcc_print_stats(TCCState
*s
, int64_t total_time
);
789 void tcc_free(void *ptr
);
790 void *tcc_malloc(unsigned long size
);
791 void *tcc_mallocz(unsigned long size
);
792 void *tcc_realloc(void *ptr
, unsigned long size
);
793 char *tcc_strdup(const char *str
);
795 char *tcc_basename(const char *name
);
796 char *tcc_fileextension (const char *name
);
797 char *pstrcpy(char *buf
, int buf_size
, const char *s
);
798 char *pstrcat(char *buf
, int buf_size
, const char *s
);
799 void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
800 void dynarray_reset(void *pp
, int *n
);
802 /* true if float/double/long double type */
803 static inline int is_float(int t
)
807 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
810 /* space exlcuding newline */
811 static inline int is_space(int ch
)
813 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
816 static inline int isid(int c
)
818 return (c
>= 'a' && c
<= 'z')
819 || (c
>= 'A' && c
<= 'Z')
823 static inline int isnum(int c
)
825 return c
>= '0' && c
<= '9';
828 static inline int isoct(int c
)
830 return c
>= '0' && c
<= '7';
833 static inline int toup(int c
)
835 if (c
>= 'a' && c
<= 'z')
836 return c
- 'a' + 'A';
842 int type_size(CType
*type
, int *a
);
843 inline CType
*pointed_type(CType
*type
);
844 int pointed_size(CType
*type
);
845 int lvalue_type(int t
);
846 int parse_btype(CType
*type
, AttributeDef
*ad
);
847 void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
848 int compare_types(CType
*type1
, CType
*type2
, int unqualified
);
849 int is_compatible_types(CType
*type1
, CType
*type2
);
850 int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
852 int ieee_finite(double d
);
854 void vpushll(long long v
);
857 void lexpand_nr(void);
858 void vpush_global_sym(CType
*type
, int v
);
859 void vset(CType
*type
, int r
, int v
);
860 void type_to_str(char *buf
, int buf_size
,
861 CType
*type
, const char *varstr
);
862 Sym
*get_sym_ref(CType
*type
, Section
*sec
,
863 unsigned long offset
, unsigned long size
);
864 Sym
*external_global_sym(int v
, CType
*type
, int r
);
866 int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
868 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
869 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
870 #define AFF_PREPROCESS 0x0004 /* preprocess file */
871 int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
874 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
);
875 void free_section(Section
*s
);
876 void section_realloc(Section
*sec
, unsigned long new_size
);
877 void *section_ptr_add(Section
*sec
, unsigned long size
);
878 Section
*find_section(TCCState
*s1
, const char *name
);
879 void put_extern_sym2(
880 Sym
*sym
, Section
*section
,
881 unsigned long value
, unsigned long size
, int can_add_underscore
);
883 Sym
*sym
, Section
*section
,
884 unsigned long value
, unsigned long size
);
885 void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
);
887 void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
);
888 void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...);
890 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
);
891 void tcc_close(BufferedFile
*bf
);
892 int tcc_compile(TCCState
*s1
);
894 void expect(const char *msg
);
896 void test_lvalue(void);
897 void *resolve_sym(TCCState
*s1
, const char *sym
);
899 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
901 int tcc_load_ldscript(TCCState
*s1
);
902 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
903 is referenced by the user (so it should be added as DT_NEEDED in
904 the generated ELF file) */
905 int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
);
906 unsigned long rt_printline(unsigned long wanted_pc
);
908 /********************************************************/
909 /* global variables */
911 /* display benchmark infos */
912 extern int total_lines
;
913 extern int total_bytes
;
916 extern struct BufferedFile
*file
;
919 extern CString tokcstr
; /* current parsed string, if any */
920 /* additional informations about token */
921 extern int tok_flags
;
922 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
923 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
924 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
925 #define TOK_FLAG_EOF 0x0008 /* end of file */
927 extern int *macro_ptr
, *macro_ptr_allocated
;
928 extern int *unget_saved_macro_ptr
;
929 extern int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
930 extern int unget_buffer_enabled
;
931 extern int parse_flags
;
932 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
933 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
934 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
935 token. line feed is also
937 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
938 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
940 extern Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
941 extern Section
*cur_text_section
; /* current section where function code is
943 #ifdef CONFIG_TCC_ASM
944 extern Section
*last_text_section
; /* to handle .previous asm directive */
946 /* bound check related sections */
947 extern Section
*bounds_section
; /* contains global data bound description */
948 extern Section
*lbounds_section
; /* contains local data bound description */
949 /* symbol sections */
950 extern Section
*symtab_section
, *strtab_section
;
953 extern Section
*stab_section
, *stabstr_section
;
955 /* loc : local variable index
956 ind : output code index
958 anon_sym: anonymous symbol index
960 extern int rsym
, anon_sym
, ind
, loc
;
961 /* expression generation modifiers */
962 extern int const_wanted
; /* true if constant wanted */
963 extern int nocode_wanted
; /* true if no code generation wanted for an expression */
964 extern int global_expr
; /* true if compound literals must be allocated
965 globally (used during initializers parsing */
966 extern CType func_vt
; /* current function return type (used by return
969 extern int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
970 extern int tok_ident
;
971 extern TokenSym
**table_ident
;
972 extern TokenSym
*hash_ident
[TOK_HASH_SIZE
];
973 extern char token_buf
[STRING_MAX_SIZE
+ 1];
974 extern char *funcname
;
975 extern Sym
*global_stack
, *local_stack
;
976 extern Sym
*define_stack
;
977 extern Sym
*global_label_stack
, *local_label_stack
;
978 /* symbol allocator */
979 #define SYM_POOL_NB (8192 / sizeof(Sym))
980 extern Sym
*sym_free_first
;
981 extern void **sym_pools
;
982 extern int nb_sym_pools
;
984 extern SValue vstack
[VSTACK_SIZE
], *vtop
;
985 /* some predefined types */
986 extern CType char_pointer_type
, func_old_type
, int_type
;
988 /* use GNU C extensions */
991 /* use Tiny C extensions */
994 /* max number of callers shown if error */
995 #ifdef CONFIG_TCC_BACKTRACE
996 extern int num_callers
;
997 extern const char **rt_bound_error_msg
;
998 extern unsigned long rt_prog_main
;
1001 /* XXX: get rid of this ASAP */
1002 extern struct TCCState
*tcc_state
;
1005 * Include the target specific definitions.
1007 #ifdef TCC_TARGET_I386
1008 #include "i386-gen.h"
1011 #ifdef TCC_TARGET_ARM
1012 #include "arm-gen.h"
1015 #ifdef TCC_TARGET_C67
1016 #include "c67-gen.h"
1019 #ifdef TCC_TARGET_X86_64
1020 #include "x86_64-gen.h"
1024 #include "tcccoff.h"
1026 #include "tcccstring.h"
1031 #include "tccutil.h"
1032 #include "tccargs.h"
1035 #ifdef TCC_TARGET_PE
1040 #endif /* __TCC_H__ */