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 __aligned(n) __declspec(align(n))
56 #define uplong unsigned long long
67 #include <sys/ucontext.h>
71 #endif /* !CONFIG_TCCBOOT */
74 #define uplong unsigned long
78 #define __aligned(n) __attribute__((aligned(n)))
96 /* preprocessor debug */
98 /* include file debug */
103 /* assembler debug */
106 /* target selection */
107 //#define TCC_TARGET_I386 /* i386 code generator */
108 //#define TCC_TARGET_ARM /* ARMv4 code generator */
109 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
110 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
112 /* default target is I386 */
113 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
114 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
115 #define TCC_TARGET_I386
118 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
119 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
120 #define CONFIG_TCC_BCHECK /* enable bound checking code */
123 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
124 #define CONFIG_TCC_STATIC
127 /* define it to include assembler support */
128 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
129 #define CONFIG_TCC_ASM
132 /* object format selection */
133 #if defined(TCC_TARGET_C67)
134 #define TCC_TARGET_COFF
137 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
138 #define CONFIG_TCC_BACKTRACE
147 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
148 executables or dlls */
149 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
151 #define INCLUDE_STACK_SIZE 32
152 #define IFDEF_STACK_SIZE 64
153 #define VSTACK_SIZE 256
154 #define STRING_MAX_SIZE 1024
155 #define PACK_STACK_SIZE 8
157 #define TOK_HASH_SIZE 8192 /* must be a power of two */
158 #define TOK_ALLOC_INCR 512 /* must be a power of two */
159 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
161 /* token symbol management */
162 typedef struct TokenSym
{
163 struct TokenSym
*hash_next
;
164 struct Sym
*sym_define
; /* direct pointer to define */
165 struct Sym
*sym_label
; /* direct pointer to label */
166 struct Sym
*sym_struct
; /* direct pointer to structure */
167 struct Sym
*sym_identifier
; /* direct pointer to identifier */
168 int tok
; /* token number */
174 typedef unsigned short nwchar_t
;
176 typedef int nwchar_t
;
179 typedef struct CString
{
180 int size
; /* size in bytes */
181 void *data
; /* either 'char *' or 'nwchar_t *' */
183 void *data_allocated
; /* if non NULL, data has been malloced */
186 /* type definition */
187 typedef struct CType
{
193 typedef union CValue
{
199 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
201 unsigned long long ull
;
202 struct CString
*cstr
;
208 typedef struct SValue
{
209 CType type
; /* type */
210 unsigned short r
; /* register + flags */
211 unsigned short r2
; /* second register, used for 'long long'
212 type. If not used, set to VT_CONST */
213 CValue c
; /* constant, if VT_CONST */
214 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
217 /* symbol management */
219 int v
; /* symbol token */
220 long r
; /* associated register */
222 long c
; /* associated number */
223 int *d
; /* define token stream */
225 CType type
; /* associated type */
227 struct Sym
*next
; /* next related symbol */
228 long jnext
; /* next jump label */
230 struct Sym
*prev
; /* prev symbol in stack */
231 struct Sym
*prev_tok
; /* previous symbol for this token */
234 /* section definition */
235 /* XXX: use directly ELF structure for parameters ? */
236 /* special flag to indicate that the section should not be linked to
238 #define SHF_PRIVATE 0x80000000
240 /* special flag, too */
241 #define SECTION_ABS ((void *)1)
243 typedef struct Section
{
244 unsigned long data_offset
; /* current data offset */
245 unsigned char *data
; /* section data */
246 unsigned long data_allocated
; /* used for realloc() handling */
247 int sh_name
; /* elf section name (only used during output) */
248 int sh_num
; /* elf section number */
249 int sh_type
; /* elf section type */
250 int sh_flags
; /* elf section flags */
251 int sh_info
; /* elf section info */
252 int sh_addralign
; /* elf section alignment */
253 int sh_entsize
; /* elf entry size */
254 unsigned long sh_size
; /* section size (only used during output) */
255 unsigned long sh_addr
; /* address at which the section is relocated */
256 unsigned long sh_offset
; /* file offset */
257 int nb_hashed_syms
; /* used to resize the hash table */
258 struct Section
*link
; /* link to another section */
259 struct Section
*reloc
; /* corresponding section for relocation, if any */
260 struct Section
*hash
; /* hash table for symbols */
261 struct Section
*next
;
262 char name
[1]; /* section name */
265 typedef struct DLLReference
{
271 /* GNUC attribute definition */
272 typedef struct AttributeDef
{
276 int func_attr
; /* calling convention, exports, ... */
279 /* -------------------------------------------------- */
280 /* gr: wrappers for casting sym->r for other purposes */
289 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
290 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
291 #define FUNC_IMPORT(r) (((func_attr_t*)&(r))->func_import)
292 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
293 /* -------------------------------------------------- */
295 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
296 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
297 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
299 /* stored in 'Sym.c' field */
300 #define FUNC_NEW 1 /* ansi function prototype */
301 #define FUNC_OLD 2 /* old function prototype */
302 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
304 /* stored in 'Sym.r' field */
305 #define FUNC_CDECL 0 /* standard c call */
306 #define FUNC_STDCALL 1 /* pascal c call */
307 #define FUNC_FASTCALL1 2 /* first param in %eax */
308 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
309 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
310 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
312 /* field 'Sym.t' for macros */
313 #define MACRO_OBJ 0 /* object like macro */
314 #define MACRO_FUNC 1 /* function like macro */
316 /* field 'Sym.r' for C labels */
317 #define LABEL_DEFINED 0 /* label is defined */
318 #define LABEL_FORWARD 1 /* label is forward defined */
319 #define LABEL_DECLARED 2 /* label is declared but never used */
321 /* type_decl() types */
322 #define TYPE_ABSTRACT 1 /* type without variable */
323 #define TYPE_DIRECT 2 /* type with variable */
325 #define IO_BUF_SIZE 8192
327 typedef struct BufferedFile
{
331 int line_num
; /* current line number - here to simplify code */
332 int ifndef_macro
; /* #ifndef macro / #endif search */
333 int ifndef_macro_saved
; /* saved ifndef_macro */
334 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
335 char inc_type
; /* type of include */
336 char inc_filename
[512]; /* filename specified by the user */
337 char filename
[1024]; /* current filename - here to simplify code */
338 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
341 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
342 #define CH_EOF (-1) /* end of file */
344 /* parsing state (used to save parser state to reparse part of the
345 source several times) */
346 typedef struct ParseState
{
353 /* used to record tokens */
354 typedef struct TokenString
{
361 /* inline functions */
362 typedef struct InlineFunc
{
368 /* include file cache, used to find files faster and also to eliminate
369 inclusion if the include file is protected by #ifndef ... #endif */
370 typedef struct CachedInclude
{
372 int hash_next
; /* -1 if none */
373 char type
; /* '"' or '>' to give include type */
374 char filename
[1]; /* path specified in #include */
377 #define CACHED_INCLUDES_HASH_SIZE 512
379 #ifdef CONFIG_TCC_ASM
380 typedef struct ExprValue
{
385 #define MAX_ASM_OPERANDS 30
386 typedef struct ASMOperand
{
387 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
389 char asm_str
[16]; /* computed asm string for operand */
390 SValue
*vt
; /* C value of the expression */
391 int ref_index
; /* if >= 0, gives reference to a output constraint */
392 int input_index
; /* if >= 0, gives reference to an input constraint */
393 int priority
; /* priority, used to assign registers */
394 int reg
; /* if >= 0, register number used for this operand */
395 int is_llong
; /* true if double register value */
396 int is_memory
; /* true if memory operand */
397 int is_rw
; /* for '+' modifier */
404 BufferedFile
**include_stack_ptr
;
405 int *ifdef_stack_ptr
;
407 /* include file handling */
408 char **include_paths
;
409 int nb_include_paths
;
410 char **sysinclude_paths
;
411 int nb_sysinclude_paths
;
412 CachedInclude
**cached_includes
;
413 int nb_cached_includes
;
415 char **library_paths
;
416 int nb_library_paths
;
418 /* array of all loaded dlls (including those referenced by loaded
420 DLLReference
**loaded_dlls
;
425 int nb_sections
; /* number of sections, including first dummy section */
427 Section
**priv_sections
;
428 int nb_priv_sections
; /* number of private sections */
433 unsigned long *got_offsets
;
435 /* give the correspondance from symtab indexes to dynsym indexes */
436 int *symtab_to_dynsym
;
438 /* temporary dynamic symbol sections (for dll loading) */
439 Section
*dynsymtab_section
;
440 /* exported dynamic symbol section */
443 int nostdinc
; /* if true, no standard headers are added */
444 int nostdlib
; /* if true, no standard libraries are added */
445 int nocommon
; /* if true, do not use common symbols for .bss data */
447 /* if true, static linking is performed */
450 /* soname as specified on the command line (-soname) */
453 /* if true, all symbols are exported */
456 /* if true, only link in referenced objects from archive */
459 /* address of text section */
460 unsigned long text_addr
;
463 /* output format, see TCC_OUTPUT_FORMAT_xxx */
466 /* C language options */
467 int char_is_unsigned
;
468 int leading_underscore
;
470 /* warning switches */
471 int warn_write_strings
;
472 int warn_unsupported
;
475 int warn_implicit_function_declaration
;
477 /* display some information during compilation */
479 /* compile with debug symbol (and use them if error during execution) */
481 /* compile with built-in memory and bounds checker */
483 /* give the path of the tcc libraries */
488 void (*error_func
)(void *opaque
, const char *msg
);
489 int error_set_jmp_enabled
;
493 jmp_buf error_jmp_buf
;
496 /* tiny assembler state */
499 /* see include_stack_ptr */
500 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
502 /* see ifdef_stack_ptr */
503 int ifdef_stack
[IFDEF_STACK_SIZE
];
505 /* see cached_includes */
506 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
509 int pack_stack
[PACK_STACK_SIZE
];
512 /* output file for preprocessing */
515 /* for tcc_relocate */
518 struct InlineFunc
**inline_fns
;
521 #ifdef TCC_TARGET_I386
525 /* section alignment */
526 unsigned long section_align
;
531 unsigned long pe_file_align
;
534 #ifndef TCC_TARGET_PE
535 #ifdef TCC_TARGET_X86_64
536 /* write PLT and GOT here */
537 char *runtime_plt_and_got
;
538 unsigned int runtime_plt_and_got_offset
;
543 /* The current value can be: */
544 #define VT_VALMASK 0x00ff
545 #define VT_CONST 0x00f0 /* constant in vc
546 (must be first non register value) */
547 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
548 #define VT_LOCAL 0x00f2 /* offset on stack */
549 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
550 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
551 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
552 #define VT_LVAL 0x0100 /* var is an lvalue */
553 #define VT_SYM 0x0200 /* a symbol value is added */
554 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
555 char/short stored in integer registers) */
556 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
557 dereferencing value */
558 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
559 bounding function call point is in vc */
560 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
561 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
562 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
563 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
566 #define VT_INT 0 /* integer type */
567 #define VT_BYTE 1 /* signed byte type */
568 #define VT_SHORT 2 /* short type */
569 #define VT_VOID 3 /* void type */
570 #define VT_PTR 4 /* pointer */
571 #define VT_ENUM 5 /* enum definition */
572 #define VT_FUNC 6 /* function type */
573 #define VT_STRUCT 7 /* struct/union definition */
574 #define VT_FLOAT 8 /* IEEE float */
575 #define VT_DOUBLE 9 /* IEEE double */
576 #define VT_LDOUBLE 10 /* IEEE long double */
577 #define VT_BOOL 11 /* ISOC99 boolean type */
578 #define VT_LLONG 12 /* 64 bit integer */
579 #define VT_LONG 13 /* long integer (NEVER USED as type, only
581 #define VT_BTYPE 0x000f /* mask for basic type */
582 #define VT_UNSIGNED 0x0010 /* unsigned type */
583 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
584 #define VT_BITFIELD 0x0040 /* bitfield modifier */
585 #define VT_CONSTANT 0x0800 /* const modifier */
586 #define VT_VOLATILE 0x1000 /* volatile modifier */
587 #define VT_SIGNED 0x2000 /* signed type */
590 #define VT_EXTERN 0x00000080 /* extern definition */
591 #define VT_STATIC 0x00000100 /* static variable */
592 #define VT_TYPEDEF 0x00000200 /* typedef definition */
593 #define VT_INLINE 0x00000400 /* inline definition */
594 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
596 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
598 /* type mask (except storage) */
599 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT)
600 #define VT_TYPE (~(VT_STORAGE))
604 /* warning: the following compare tokens depend on i386 asm code */
611 #define TOK_Nset 0x98
612 #define TOK_Nclear 0x99
618 #define TOK_LAND 0xa0
622 #define TOK_MID 0xa3 /* inc/dec, to void constant */
624 #define TOK_UDIV 0xb0 /* unsigned division */
625 #define TOK_UMOD 0xb1 /* unsigned modulo */
626 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
627 #define TOK_CINT 0xb3 /* number in tokc */
628 #define TOK_CCHAR 0xb4 /* char constant in tokc */
629 #define TOK_STR 0xb5 /* pointer to string in tokc */
630 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
631 #define TOK_LCHAR 0xb7
632 #define TOK_LSTR 0xb8
633 #define TOK_CFLOAT 0xb9 /* float constant */
634 #define TOK_LINENUM 0xba /* line number info */
635 #define TOK_CDOUBLE 0xc0 /* double constant */
636 #define TOK_CLDOUBLE 0xc1 /* long double constant */
637 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
638 #define TOK_ADDC1 0xc3 /* add with carry generation */
639 #define TOK_ADDC2 0xc4 /* add with carry use */
640 #define TOK_SUBC1 0xc5 /* add with carry generation */
641 #define TOK_SUBC2 0xc6 /* add with carry use */
642 #define TOK_CUINT 0xc8 /* unsigned int constant */
643 #define TOK_CLLONG 0xc9 /* long long constant */
644 #define TOK_CULLONG 0xca /* unsigned long long constant */
645 #define TOK_ARROW 0xcb
646 #define TOK_DOTS 0xcc /* three dots */
647 #define TOK_SHR 0xcd /* unsigned shift right */
648 #define TOK_PPNUM 0xce /* preprocessor number */
650 #define TOK_SHL 0x01 /* shift left */
651 #define TOK_SAR 0x02 /* signed shift right */
653 /* assignement operators : normal operator or 0x80 */
654 #define TOK_A_MOD 0xa5
655 #define TOK_A_AND 0xa6
656 #define TOK_A_MUL 0xaa
657 #define TOK_A_ADD 0xab
658 #define TOK_A_SUB 0xad
659 #define TOK_A_DIV 0xaf
660 #define TOK_A_XOR 0xde
661 #define TOK_A_OR 0xfc
662 #define TOK_A_SHL 0x81
663 #define TOK_A_SAR 0x82
666 #define offsetof(type, field) ((size_t) &((type *)0)->field)
670 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
673 #define TOK_EOF (-1) /* end of file */
674 #define TOK_LINEFEED 10 /* line feed */
676 /* all identificators and strings have token above that */
677 #define TOK_IDENT 256
679 /* only used for i386 asm opcodes definitions */
680 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
683 DEF(TOK_ASM_ ## x ## b, #x "b") \
684 DEF(TOK_ASM_ ## x ## w, #x "w") \
685 DEF(TOK_ASM_ ## x ## l, #x "l") \
686 DEF(TOK_ASM_ ## x, #x)
689 DEF(TOK_ASM_ ## x ## w, #x "w") \
690 DEF(TOK_ASM_ ## x ## l, #x "l") \
691 DEF(TOK_ASM_ ## x, #x)
693 #ifdef TCC_TARGET_X86_64
695 #define DEF_BWLQ(x) \
696 DEF(TOK_ASM_ ## x ## b, #x "b") \
697 DEF(TOK_ASM_ ## x ## w, #x "w") \
698 DEF(TOK_ASM_ ## x ## l, #x "l") \
699 DEF(TOK_ASM_ ## x ## q, #x "q") \
700 DEF(TOK_ASM_ ## x, #x)
703 DEF(TOK_ASM_ ## x ## w, #x "w") \
704 DEF(TOK_ASM_ ## x ## l, #x "l") \
705 DEF(TOK_ASM_ ## x ## q, #x "q") \
706 DEF(TOK_ASM_ ## x, #x)
711 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
712 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
713 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
714 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
717 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
718 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
721 #define DEF_ASMTEST(x) \
753 #define TOK_ASM_int TOK_INT
756 TOK_LAST
= TOK_IDENT
- 1,
757 #define DEF(id, str) id,
762 #define TOK_UIDENT TOK_DEFINE
765 #define snprintf _snprintf
766 #define vsnprintf _vsnprintf
768 #define strtold (long double)strtod
769 #define strtof (float)strtod
770 #define strtoll (long long)strtol
772 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
773 || defined(__OpenBSD__)
774 /* currently incorrect */
775 long double strtold(const char *nptr
, char **endptr
)
777 return (long double)strtod(nptr
, endptr
);
779 float strtof(const char *nptr
, char **endptr
)
781 return (float)strtod(nptr
, endptr
);
784 /* XXX: need to define this to use them in non ISOC99 context */
785 extern float strtof (const char *__nptr
, char **__endptr
);
786 extern long double strtold (const char *__nptr
, char **__endptr
);
790 #define IS_PATHSEP(c) (c == '/' || c == '\\')
791 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
792 #define PATHCMP stricmp
794 #define IS_PATHSEP(c) (c == '/')
795 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
796 #define PATHCMP strcmp
799 void error(const char *fmt
, ...);
800 void error_noabort(const char *fmt
, ...);
801 void warning(const char *fmt
, ...);
803 void tcc_set_lib_path_w32(TCCState
*s
);
804 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
);
805 void tcc_print_stats(TCCState
*s
, int64_t total_time
);
807 void tcc_free(void *ptr
);
808 void *tcc_malloc(unsigned long size
);
809 void *tcc_mallocz(unsigned long size
);
810 void *tcc_realloc(void *ptr
, unsigned long size
);
811 char *tcc_strdup(const char *str
);
813 char *tcc_basename(const char *name
);
814 char *tcc_fileextension (const char *name
);
815 char *pstrcpy(char *buf
, int buf_size
, const char *s
);
816 char *pstrcat(char *buf
, int buf_size
, const char *s
);
817 void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
818 void dynarray_reset(void *pp
, int *n
);
820 /* true if float/double/long double type */
821 static inline int is_float(int t
)
825 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
828 /* space exlcuding newline */
829 static inline int is_space(int ch
)
831 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
834 static inline int isid(int c
)
836 return (c
>= 'a' && c
<= 'z')
837 || (c
>= 'A' && c
<= 'Z')
841 static inline int isnum(int c
)
843 return c
>= '0' && c
<= '9';
846 static inline int isoct(int c
)
848 return c
>= '0' && c
<= '7';
851 static inline int toup(int c
)
853 if (c
>= 'a' && c
<= 'z')
854 return c
- 'a' + 'A';
860 int type_size(CType
*type
, int *a
);
861 inline CType
*pointed_type(CType
*type
);
862 int pointed_size(CType
*type
);
863 int lvalue_type(int t
);
864 int parse_btype(CType
*type
, AttributeDef
*ad
);
865 void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
866 int compare_types(CType
*type1
, CType
*type2
, int unqualified
);
867 int is_compatible_types(CType
*type1
, CType
*type2
);
868 int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
870 int ieee_finite(double d
);
872 void vpushll(long long v
);
875 void lexpand_nr(void);
876 static void vpush_global_sym(CType
*type
, int v
);
877 void vset(CType
*type
, int r
, int v
);
878 void type_to_str(char *buf
, int buf_size
,
879 CType
*type
, const char *varstr
);
880 Sym
*get_sym_ref(CType
*type
, Section
*sec
,
881 unsigned long offset
, unsigned long size
);
882 Sym
*external_global_sym(int v
, CType
*type
, int r
);
884 int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
886 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
887 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
888 #define AFF_PREPROCESS 0x0004 /* preprocess file */
889 int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
892 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
);
893 void free_section(Section
*s
);
894 void section_realloc(Section
*sec
, unsigned long new_size
);
895 void *section_ptr_add(Section
*sec
, unsigned long size
);
896 Section
*find_section(TCCState
*s1
, const char *name
);
897 void put_extern_sym2(
898 Sym
*sym
, Section
*section
,
899 unsigned long value
, unsigned long size
, int can_add_underscore
);
901 Sym
*sym
, Section
*section
,
902 unsigned long value
, unsigned long size
);
903 void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
);
905 void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
);
906 void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...);
908 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
);
909 void tcc_close(BufferedFile
*bf
);
910 int tcc_compile(TCCState
*s1
);
912 void expect(const char *msg
);
914 void test_lvalue(void);
915 void *resolve_sym(TCCState
*s1
, const char *sym
);
917 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
919 int tcc_load_ldscript(TCCState
*s1
);
920 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
921 is referenced by the user (so it should be added as DT_NEEDED in
922 the generated ELF file) */
923 int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
);
925 /********************************************************/
926 /* global variables */
928 /* display benchmark infos */
929 extern int total_lines
;
930 extern int total_bytes
;
933 extern struct BufferedFile
*file
;
936 extern CString tokcstr
; /* current parsed string, if any */
937 /* additional informations about token */
938 extern int tok_flags
;
939 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
940 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
941 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
942 #define TOK_FLAG_EOF 0x0008 /* end of file */
944 extern int *macro_ptr
, *macro_ptr_allocated
;
945 extern int *unget_saved_macro_ptr
;
946 extern int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
947 extern int unget_buffer_enabled
;
948 extern int parse_flags
;
949 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
950 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
951 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
952 token. line feed is also
954 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
955 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
957 extern Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
958 extern Section
*cur_text_section
; /* current section where function code is
960 #ifdef CONFIG_TCC_ASM
961 extern Section
*last_text_section
; /* to handle .previous asm directive */
963 /* bound check related sections */
964 extern Section
*bounds_section
; /* contains global data bound description */
965 extern Section
*lbounds_section
; /* contains local data bound description */
966 /* symbol sections */
967 extern Section
*symtab_section
, *strtab_section
;
970 extern Section
*stab_section
, *stabstr_section
;
972 /* loc : local variable index
973 ind : output code index
975 anon_sym: anonymous symbol index
977 extern int rsym
, anon_sym
, ind
, loc
;
978 /* expression generation modifiers */
979 extern int const_wanted
; /* true if constant wanted */
980 extern int nocode_wanted
; /* true if no code generation wanted for an expression */
981 extern int global_expr
; /* true if compound literals must be allocated
982 globally (used during initializers parsing */
983 extern CType func_vt
; /* current function return type (used by return
986 extern int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
987 extern int tok_ident
;
988 extern TokenSym
**table_ident
;
989 extern TokenSym
*hash_ident
[TOK_HASH_SIZE
];
990 extern char token_buf
[STRING_MAX_SIZE
+ 1];
991 extern char *funcname
;
992 extern Sym
*global_stack
, *local_stack
;
993 extern Sym
*define_stack
;
994 extern Sym
*global_label_stack
, *local_label_stack
;
995 /* symbol allocator */
996 #define SYM_POOL_NB (8192 / sizeof(Sym))
997 extern Sym
*sym_free_first
;
998 extern void **sym_pools
;
999 extern int nb_sym_pools
;
1001 extern SValue vstack
[VSTACK_SIZE
], *vtop
;
1002 /* some predefined types */
1003 extern CType char_pointer_type
, func_old_type
, int_type
;
1005 /* use GNU C extensions */
1008 /* use Tiny C extensions */
1011 /* max number of callers shown if error */
1012 #ifdef CONFIG_TCC_BACKTRACE
1013 extern int num_callers
;
1014 extern const char **rt_bound_error_msg
;
1015 extern unsigned long rt_prog_main
;
1018 /* XXX: get rid of this ASAP */
1019 extern struct TCCState
*tcc_state
;
1022 * Include the target specific definitions.
1024 #ifdef TCC_TARGET_I386
1025 #include "i386-gen.h"
1028 #ifdef TCC_TARGET_ARM
1029 #include "arm-gen.h"
1032 #ifdef TCC_TARGET_C67
1033 #include "c67-gen.h"
1036 #ifdef TCC_TARGET_X86_64
1037 #include "x86_64-gen.h"
1041 #include "tcccoff.h"
1043 #include "tcccstring.h"
1048 #include "tccutil.h"
1049 #include "tccargs.h"
1051 #ifdef TCC_TARGET_PE
1056 #endif /* __TCC_H__ */