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
63 #include <sys/ucontext.h>
67 #endif /* !CONFIG_TCCBOOT */
70 #define uplong unsigned long
74 #define __aligned(n) __attribute__((aligned(n)))
92 /* preprocessor debug */
94 /* include file debug */
102 /* target selection */
103 //#define TCC_TARGET_I386 /* i386 code generator */
104 //#define TCC_TARGET_ARM /* ARMv4 code generator */
105 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
106 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
108 /* default target is I386 */
109 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
110 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
111 #define TCC_TARGET_I386
114 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
115 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
116 #define CONFIG_TCC_BCHECK /* enable bound checking code */
119 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
120 #define CONFIG_TCC_STATIC
123 /* define it to include assembler support */
124 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
125 #define CONFIG_TCC_ASM
128 /* object format selection */
129 #if defined(TCC_TARGET_C67)
130 #define TCC_TARGET_COFF
133 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
134 #define CONFIG_TCC_BACKTRACE
143 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
144 executables or dlls */
145 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
147 #define INCLUDE_STACK_SIZE 32
148 #define IFDEF_STACK_SIZE 64
149 #define VSTACK_SIZE 256
150 #define STRING_MAX_SIZE 1024
151 #define PACK_STACK_SIZE 8
153 #define TOK_HASH_SIZE 8192 /* must be a power of two */
154 #define TOK_ALLOC_INCR 512 /* must be a power of two */
155 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
157 /* token symbol management */
158 typedef struct TokenSym
{
159 struct TokenSym
*hash_next
;
160 struct Sym
*sym_define
; /* direct pointer to define */
161 struct Sym
*sym_label
; /* direct pointer to label */
162 struct Sym
*sym_struct
; /* direct pointer to structure */
163 struct Sym
*sym_identifier
; /* direct pointer to identifier */
164 int tok
; /* token number */
170 typedef unsigned short nwchar_t
;
172 typedef int nwchar_t
;
175 typedef struct CString
{
176 int size
; /* size in bytes */
177 void *data
; /* either 'char *' or 'nwchar_t *' */
179 void *data_allocated
; /* if non NULL, data has been malloced */
182 /* type definition */
183 typedef struct CType
{
189 typedef union CValue
{
195 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
197 unsigned long long ull
;
198 struct CString
*cstr
;
204 typedef struct SValue
{
205 CType type
; /* type */
206 unsigned short r
; /* register + flags */
207 unsigned short r2
; /* second register, used for 'long long'
208 type. If not used, set to VT_CONST */
209 CValue c
; /* constant, if VT_CONST */
210 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
213 /* symbol management */
215 int v
; /* symbol token */
216 long r
; /* associated register */
218 long c
; /* associated number */
219 int *d
; /* define token stream */
221 CType type
; /* associated type */
223 struct Sym
*next
; /* next related symbol */
224 long jnext
; /* next jump label */
226 struct Sym
*prev
; /* prev symbol in stack */
227 struct Sym
*prev_tok
; /* previous symbol for this token */
230 /* section definition */
231 /* XXX: use directly ELF structure for parameters ? */
232 /* special flag to indicate that the section should not be linked to
234 #define SHF_PRIVATE 0x80000000
236 /* special flag, too */
237 #define SECTION_ABS ((void *)1)
239 typedef struct Section
{
240 unsigned long data_offset
; /* current data offset */
241 unsigned char *data
; /* section data */
242 unsigned long data_allocated
; /* used for realloc() handling */
243 int sh_name
; /* elf section name (only used during output) */
244 int sh_num
; /* elf section number */
245 int sh_type
; /* elf section type */
246 int sh_flags
; /* elf section flags */
247 int sh_info
; /* elf section info */
248 int sh_addralign
; /* elf section alignment */
249 int sh_entsize
; /* elf entry size */
250 unsigned long sh_size
; /* section size (only used during output) */
251 unsigned long sh_addr
; /* address at which the section is relocated */
252 unsigned long sh_offset
; /* file offset */
253 int nb_hashed_syms
; /* used to resize the hash table */
254 struct Section
*link
; /* link to another section */
255 struct Section
*reloc
; /* corresponding section for relocation, if any */
256 struct Section
*hash
; /* hash table for symbols */
257 struct Section
*next
;
258 char name
[1]; /* section name */
261 typedef struct DLLReference
{
267 /* GNUC attribute definition */
268 typedef struct AttributeDef
{
272 int func_attr
; /* calling convention, exports, ... */
275 /* -------------------------------------------------- */
276 /* gr: wrappers for casting sym->r for other purposes */
285 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
286 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
287 #define FUNC_IMPORT(r) (((func_attr_t*)&(r))->func_import)
288 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
289 /* -------------------------------------------------- */
291 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
292 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
293 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
295 /* stored in 'Sym.c' field */
296 #define FUNC_NEW 1 /* ansi function prototype */
297 #define FUNC_OLD 2 /* old function prototype */
298 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
300 /* stored in 'Sym.r' field */
301 #define FUNC_CDECL 0 /* standard c call */
302 #define FUNC_STDCALL 1 /* pascal c call */
303 #define FUNC_FASTCALL1 2 /* first param in %eax */
304 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
305 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
306 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
308 /* field 'Sym.t' for macros */
309 #define MACRO_OBJ 0 /* object like macro */
310 #define MACRO_FUNC 1 /* function like macro */
312 /* field 'Sym.r' for C labels */
313 #define LABEL_DEFINED 0 /* label is defined */
314 #define LABEL_FORWARD 1 /* label is forward defined */
315 #define LABEL_DECLARED 2 /* label is declared but never used */
317 /* type_decl() types */
318 #define TYPE_ABSTRACT 1 /* type without variable */
319 #define TYPE_DIRECT 2 /* type with variable */
321 #define IO_BUF_SIZE 8192
323 typedef struct BufferedFile
{
327 int line_num
; /* current line number - here to simplify code */
328 int ifndef_macro
; /* #ifndef macro / #endif search */
329 int ifndef_macro_saved
; /* saved ifndef_macro */
330 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
331 char inc_type
; /* type of include */
332 char inc_filename
[512]; /* filename specified by the user */
333 char filename
[1024]; /* current filename - here to simplify code */
334 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
337 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
338 #define CH_EOF (-1) /* end of file */
340 /* parsing state (used to save parser state to reparse part of the
341 source several times) */
342 typedef struct ParseState
{
349 /* used to record tokens */
350 typedef struct TokenString
{
357 /* inline functions */
358 typedef struct InlineFunc
{
364 /* include file cache, used to find files faster and also to eliminate
365 inclusion if the include file is protected by #ifndef ... #endif */
366 typedef struct CachedInclude
{
368 int hash_next
; /* -1 if none */
369 char type
; /* '"' or '>' to give include type */
370 char filename
[1]; /* path specified in #include */
373 #define CACHED_INCLUDES_HASH_SIZE 512
375 #ifdef CONFIG_TCC_ASM
376 typedef struct ExprValue
{
381 #define MAX_ASM_OPERANDS 30
382 typedef struct ASMOperand
{
383 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
385 char asm_str
[16]; /* computed asm string for operand */
386 SValue
*vt
; /* C value of the expression */
387 int ref_index
; /* if >= 0, gives reference to a output constraint */
388 int input_index
; /* if >= 0, gives reference to an input constraint */
389 int priority
; /* priority, used to assign registers */
390 int reg
; /* if >= 0, register number used for this operand */
391 int is_llong
; /* true if double register value */
392 int is_memory
; /* true if memory operand */
393 int is_rw
; /* for '+' modifier */
400 BufferedFile
**include_stack_ptr
;
401 int *ifdef_stack_ptr
;
403 /* include file handling */
404 char **include_paths
;
405 int nb_include_paths
;
406 char **sysinclude_paths
;
407 int nb_sysinclude_paths
;
408 CachedInclude
**cached_includes
;
409 int nb_cached_includes
;
411 char **library_paths
;
412 int nb_library_paths
;
414 /* array of all loaded dlls (including those referenced by loaded
416 DLLReference
**loaded_dlls
;
421 int nb_sections
; /* number of sections, including first dummy section */
423 Section
**priv_sections
;
424 int nb_priv_sections
; /* number of private sections */
429 unsigned long *got_offsets
;
431 /* give the correspondance from symtab indexes to dynsym indexes */
432 int *symtab_to_dynsym
;
434 /* temporary dynamic symbol sections (for dll loading) */
435 Section
*dynsymtab_section
;
436 /* exported dynamic symbol section */
439 int nostdinc
; /* if true, no standard headers are added */
440 int nostdlib
; /* if true, no standard libraries are added */
441 int nocommon
; /* if true, do not use common symbols for .bss data */
443 /* if true, static linking is performed */
446 /* soname as specified on the command line (-soname) */
449 /* if true, all symbols are exported */
452 /* if true, only link in referenced objects from archive */
455 /* address of text section */
456 unsigned long text_addr
;
459 /* output format, see TCC_OUTPUT_FORMAT_xxx */
462 /* C language options */
463 int char_is_unsigned
;
464 int leading_underscore
;
466 /* warning switches */
467 int warn_write_strings
;
468 int warn_unsupported
;
471 int warn_implicit_function_declaration
;
473 /* display some information during compilation */
475 /* compile with debug symbol (and use them if error during execution) */
477 /* compile with built-in memory and bounds checker */
479 /* give the path of the tcc libraries */
484 void (*error_func
)(void *opaque
, const char *msg
);
485 int error_set_jmp_enabled
;
489 jmp_buf error_jmp_buf
;
492 /* tiny assembler state */
495 /* see include_stack_ptr */
496 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
498 /* see ifdef_stack_ptr */
499 int ifdef_stack
[IFDEF_STACK_SIZE
];
501 /* see cached_includes */
502 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
505 int pack_stack
[PACK_STACK_SIZE
];
508 /* output file for preprocessing */
511 /* for tcc_relocate */
514 struct InlineFunc
**inline_fns
;
517 #ifdef TCC_TARGET_I386
521 /* section alignment */
522 unsigned long section_align
;
527 unsigned long pe_file_align
;
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 */
592 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
594 /* type mask (except storage) */
595 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT)
596 #define VT_TYPE (~(VT_STORAGE))
600 /* warning: the following compare tokens depend on i386 asm code */
607 #define TOK_Nset 0x98
608 #define TOK_Nclear 0x99
614 #define TOK_LAND 0xa0
618 #define TOK_MID 0xa3 /* inc/dec, to void constant */
620 #define TOK_UDIV 0xb0 /* unsigned division */
621 #define TOK_UMOD 0xb1 /* unsigned modulo */
622 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
623 #define TOK_CINT 0xb3 /* number in tokc */
624 #define TOK_CCHAR 0xb4 /* char constant in tokc */
625 #define TOK_STR 0xb5 /* pointer to string in tokc */
626 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
627 #define TOK_LCHAR 0xb7
628 #define TOK_LSTR 0xb8
629 #define TOK_CFLOAT 0xb9 /* float constant */
630 #define TOK_LINENUM 0xba /* line number info */
631 #define TOK_CDOUBLE 0xc0 /* double constant */
632 #define TOK_CLDOUBLE 0xc1 /* long double constant */
633 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
634 #define TOK_ADDC1 0xc3 /* add with carry generation */
635 #define TOK_ADDC2 0xc4 /* add with carry use */
636 #define TOK_SUBC1 0xc5 /* add with carry generation */
637 #define TOK_SUBC2 0xc6 /* add with carry use */
638 #define TOK_CUINT 0xc8 /* unsigned int constant */
639 #define TOK_CLLONG 0xc9 /* long long constant */
640 #define TOK_CULLONG 0xca /* unsigned long long constant */
641 #define TOK_ARROW 0xcb
642 #define TOK_DOTS 0xcc /* three dots */
643 #define TOK_SHR 0xcd /* unsigned shift right */
644 #define TOK_PPNUM 0xce /* preprocessor number */
646 #define TOK_SHL 0x01 /* shift left */
647 #define TOK_SAR 0x02 /* signed shift right */
649 /* assignement operators : normal operator or 0x80 */
650 #define TOK_A_MOD 0xa5
651 #define TOK_A_AND 0xa6
652 #define TOK_A_MUL 0xaa
653 #define TOK_A_ADD 0xab
654 #define TOK_A_SUB 0xad
655 #define TOK_A_DIV 0xaf
656 #define TOK_A_XOR 0xde
657 #define TOK_A_OR 0xfc
658 #define TOK_A_SHL 0x81
659 #define TOK_A_SAR 0x82
662 #define offsetof(type, field) ((size_t) &((type *)0)->field)
666 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
669 #define TOK_EOF (-1) /* end of file */
670 #define TOK_LINEFEED 10 /* line feed */
672 /* all identificators and strings have token above that */
673 #define TOK_IDENT 256
675 /* only used for i386 asm opcodes definitions */
676 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
679 DEF(TOK_ASM_ ## x ## b, #x "b") \
680 DEF(TOK_ASM_ ## x ## w, #x "w") \
681 DEF(TOK_ASM_ ## x ## l, #x "l") \
682 DEF(TOK_ASM_ ## x, #x)
685 DEF(TOK_ASM_ ## x ## w, #x "w") \
686 DEF(TOK_ASM_ ## x ## l, #x "l") \
687 DEF(TOK_ASM_ ## x, #x)
689 #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)
699 DEF(TOK_ASM_ ## x ## w, #x "w") \
700 DEF(TOK_ASM_ ## x ## l, #x "l") \
701 DEF(TOK_ASM_ ## x ## q, #x "q") \
702 DEF(TOK_ASM_ ## x, #x)
707 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
708 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
709 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
710 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
713 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
714 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
717 #define DEF_ASMTEST(x) \
749 #define TOK_ASM_int TOK_INT
752 TOK_LAST
= TOK_IDENT
- 1,
753 #define DEF(id, str) id,
758 #define TOK_UIDENT TOK_DEFINE
761 #define snprintf _snprintf
762 #define vsnprintf _vsnprintf
764 #define strtold (long double)strtod
765 #define strtof (float)strtod
766 #define strtoll (long long)strtol
768 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
769 || defined(__OpenBSD__)
770 /* currently incorrect */
771 long double strtold(const char *nptr
, char **endptr
)
773 return (long double)strtod(nptr
, endptr
);
775 float strtof(const char *nptr
, char **endptr
)
777 return (float)strtod(nptr
, endptr
);
780 /* XXX: need to define this to use them in non ISOC99 context */
781 extern float strtof (const char *__nptr
, char **__endptr
);
782 extern long double strtold (const char *__nptr
, char **__endptr
);
786 #define IS_PATHSEP(c) (c == '/' || c == '\\')
787 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
788 #define PATHCMP stricmp
790 #define IS_PATHSEP(c) (c == '/')
791 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
792 #define PATHCMP strcmp
795 void error(const char *fmt
, ...);
796 void error_noabort(const char *fmt
, ...);
797 void warning(const char *fmt
, ...);
799 void tcc_set_lib_path_w32(TCCState
*s
);
800 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
);
801 void tcc_print_stats(TCCState
*s
, int64_t total_time
);
803 void tcc_free(void *ptr
);
804 void *tcc_malloc(unsigned long size
);
805 void *tcc_mallocz(unsigned long size
);
806 void *tcc_realloc(void *ptr
, unsigned long size
);
807 char *tcc_strdup(const char *str
);
809 char *tcc_basename(const char *name
);
810 char *tcc_fileextension (const char *name
);
811 char *pstrcpy(char *buf
, int buf_size
, const char *s
);
812 char *pstrcat(char *buf
, int buf_size
, const char *s
);
813 void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
814 void dynarray_reset(void *pp
, int *n
);
816 /* true if float/double/long double type */
817 static inline int is_float(int t
)
821 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
824 /* space exlcuding newline */
825 static inline int is_space(int ch
)
827 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
830 static inline int isid(int c
)
832 return (c
>= 'a' && c
<= 'z')
833 || (c
>= 'A' && c
<= 'Z')
837 static inline int isnum(int c
)
839 return c
>= '0' && c
<= '9';
842 static inline int isoct(int c
)
844 return c
>= '0' && c
<= '7';
847 static inline int toup(int c
)
849 if (c
>= 'a' && c
<= 'z')
850 return c
- 'a' + 'A';
856 int type_size(CType
*type
, int *a
);
857 inline CType
*pointed_type(CType
*type
);
858 int pointed_size(CType
*type
);
859 int lvalue_type(int t
);
860 int parse_btype(CType
*type
, AttributeDef
*ad
);
861 void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
862 int compare_types(CType
*type1
, CType
*type2
, int unqualified
);
863 int is_compatible_types(CType
*type1
, CType
*type2
);
864 int is_compatible_parameter_types(CType
*type1
, CType
*type2
);
866 int ieee_finite(double d
);
868 void vpushll(long long v
);
871 void lexpand_nr(void);
872 static void vpush_global_sym(CType
*type
, int v
);
873 void vset(CType
*type
, int r
, int v
);
874 void type_to_str(char *buf
, int buf_size
,
875 CType
*type
, const char *varstr
);
876 Sym
*get_sym_ref(CType
*type
, Section
*sec
,
877 unsigned long offset
, unsigned long size
);
878 Sym
*external_global_sym(int v
, CType
*type
, int r
);
880 int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
882 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
883 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
884 #define AFF_PREPROCESS 0x0004 /* preprocess file */
885 int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
888 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
);
889 void free_section(Section
*s
);
890 void section_realloc(Section
*sec
, unsigned long new_size
);
891 void *section_ptr_add(Section
*sec
, unsigned long size
);
892 Section
*find_section(TCCState
*s1
, const char *name
);
893 void put_extern_sym2(
894 Sym
*sym
, Section
*section
,
895 unsigned long value
, unsigned long size
, int can_add_underscore
);
897 Sym
*sym
, Section
*section
,
898 unsigned long value
, unsigned long size
);
899 void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
);
901 void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
);
902 void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...);
904 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
);
905 void tcc_close(BufferedFile
*bf
);
906 int tcc_compile(TCCState
*s1
);
908 void expect(const char *msg
);
910 void test_lvalue(void);
911 void *resolve_sym(TCCState
*s1
, const char *sym
);
913 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
915 int tcc_load_ldscript(TCCState
*s1
);
916 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
917 is referenced by the user (so it should be added as DT_NEEDED in
918 the generated ELF file) */
919 int tcc_load_dll(TCCState
*s1
, int fd
, const char *filename
, int level
);
921 /********************************************************/
922 /* global variables */
924 /* display benchmark infos */
925 extern int total_lines
;
926 extern int total_bytes
;
929 extern struct BufferedFile
*file
;
932 extern CString tokcstr
; /* current parsed string, if any */
933 /* additional informations about token */
934 extern int tok_flags
;
935 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
936 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
937 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
938 #define TOK_FLAG_EOF 0x0008 /* end of file */
940 extern int *macro_ptr
, *macro_ptr_allocated
;
941 extern int *unget_saved_macro_ptr
;
942 extern int unget_saved_buffer
[TOK_MAX_SIZE
+ 1];
943 extern int unget_buffer_enabled
;
944 extern int parse_flags
;
945 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
946 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
947 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
948 token. line feed is also
950 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
951 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
953 extern Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
954 extern Section
*cur_text_section
; /* current section where function code is
956 #ifdef CONFIG_TCC_ASM
957 extern Section
*last_text_section
; /* to handle .previous asm directive */
959 /* bound check related sections */
960 extern Section
*bounds_section
; /* contains global data bound description */
961 extern Section
*lbounds_section
; /* contains local data bound description */
962 /* symbol sections */
963 extern Section
*symtab_section
, *strtab_section
;
966 extern Section
*stab_section
, *stabstr_section
;
968 /* loc : local variable index
969 ind : output code index
971 anon_sym: anonymous symbol index
973 extern int rsym
, anon_sym
, ind
, loc
;
974 /* expression generation modifiers */
975 extern int const_wanted
; /* true if constant wanted */
976 extern int nocode_wanted
; /* true if no code generation wanted for an expression */
977 extern int global_expr
; /* true if compound literals must be allocated
978 globally (used during initializers parsing */
979 extern CType func_vt
; /* current function return type (used by return
982 extern int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
983 extern int tok_ident
;
984 extern TokenSym
**table_ident
;
985 extern TokenSym
*hash_ident
[TOK_HASH_SIZE
];
986 extern char token_buf
[STRING_MAX_SIZE
+ 1];
987 extern char *funcname
;
988 extern Sym
*global_stack
, *local_stack
;
989 extern Sym
*define_stack
;
990 extern Sym
*global_label_stack
, *local_label_stack
;
991 /* symbol allocator */
992 #define SYM_POOL_NB (8192 / sizeof(Sym))
993 extern Sym
*sym_free_first
;
994 extern void **sym_pools
;
995 extern int nb_sym_pools
;
997 extern SValue vstack
[VSTACK_SIZE
], *vtop
;
998 /* some predefined types */
999 extern CType char_pointer_type
, func_old_type
, int_type
;
1001 /* use GNU C extensions */
1004 /* use Tiny C extensions */
1007 /* max number of callers shown if error */
1008 #ifdef CONFIG_TCC_BACKTRACE
1009 extern int num_callers
;
1010 extern const char **rt_bound_error_msg
;
1011 extern unsigned long rt_prog_main
;
1014 /* XXX: get rid of this ASAP */
1015 extern struct TCCState
*tcc_state
;
1018 * Include the target specific definitions.
1020 #ifdef TCC_TARGET_I386
1021 #include "i386-gen.h"
1024 #ifdef TCC_TARGET_ARM
1025 #include "arm-gen.h"
1028 #ifdef TCC_TARGET_C67
1029 #include "c67-gen.h"
1032 #ifdef TCC_TARGET_X86_64
1033 #include "x86_64-gen.h"
1037 #include "tcccoff.h"
1039 #include "tcccstring.h"
1044 #include "tccutil.h"
1046 #ifdef TCC_TARGET_PE
1051 #endif /* __TCC_H__ */