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
27 #define CONFIG_TCC_STATIC
44 #include <sys/timeb.h>
45 #include <io.h> /* open, close etc. */
46 #include <direct.h> /* getcwd */
47 #define inline __inline
50 #define __aligned(n) __declspec(align(n))
53 #define uplong unsigned long long
60 #include <sys/ucontext.h>
64 #endif /* !CONFIG_TCCBOOT */
67 #define uplong unsigned long
71 #define __aligned(n) __attribute__((aligned(n)))
89 /* preprocessor debug */
91 /* include file debug */
99 /* target selection */
100 //#define TCC_TARGET_I386 /* i386 code generator */
101 //#define TCC_TARGET_ARM /* ARMv4 code generator */
102 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
103 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
105 /* default target is I386 */
106 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
107 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
108 #define TCC_TARGET_I386
111 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
112 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
113 #define CONFIG_TCC_BCHECK /* enable bound checking code */
116 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
117 #define CONFIG_TCC_STATIC
120 /* define it to include assembler support */
121 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67) && \
122 !defined(TCC_TARGET_X86_64)
123 #define CONFIG_TCC_ASM
126 /* object format selection */
127 #if defined(TCC_TARGET_C67)
128 #define TCC_TARGET_COFF
131 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
132 #define CONFIG_TCC_BACKTRACE
141 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
142 executables or dlls */
143 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
145 #define INCLUDE_STACK_SIZE 32
146 #define IFDEF_STACK_SIZE 64
147 #define VSTACK_SIZE 256
148 #define STRING_MAX_SIZE 1024
149 #define PACK_STACK_SIZE 8
151 #define TOK_HASH_SIZE 8192 /* must be a power of two */
152 #define TOK_ALLOC_INCR 512 /* must be a power of two */
153 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
155 /* token symbol management */
156 typedef struct TokenSym
{
157 struct TokenSym
*hash_next
;
158 struct Sym
*sym_define
; /* direct pointer to define */
159 struct Sym
*sym_label
; /* direct pointer to label */
160 struct Sym
*sym_struct
; /* direct pointer to structure */
161 struct Sym
*sym_identifier
; /* direct pointer to identifier */
162 int tok
; /* token number */
168 typedef unsigned short nwchar_t
;
170 typedef int nwchar_t
;
173 typedef struct CString
{
174 int size
; /* size in bytes */
175 void *data
; /* either 'char *' or 'nwchar_t *' */
177 void *data_allocated
; /* if non NULL, data has been malloced */
180 /* type definition */
181 typedef struct CType
{
187 typedef union CValue
{
193 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
195 unsigned long long ull
;
196 struct CString
*cstr
;
202 typedef struct SValue
{
203 CType type
; /* type */
204 unsigned short r
; /* register + flags */
205 unsigned short r2
; /* second register, used for 'long long'
206 type. If not used, set to VT_CONST */
207 CValue c
; /* constant, if VT_CONST */
208 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
211 /* symbol management */
213 int v
; /* symbol token */
214 long r
; /* associated register */
216 long c
; /* associated number */
217 int *d
; /* define token stream */
219 CType type
; /* associated type */
221 struct Sym
*next
; /* next related symbol */
222 long jnext
; /* next jump label */
224 struct Sym
*prev
; /* prev symbol in stack */
225 struct Sym
*prev_tok
; /* previous symbol for this token */
228 /* section definition */
229 /* XXX: use directly ELF structure for parameters ? */
230 /* special flag to indicate that the section should not be linked to
232 #define SHF_PRIVATE 0x80000000
234 /* special flag, too */
235 #define SECTION_ABS ((void *)1)
237 typedef struct Section
{
238 unsigned long data_offset
; /* current data offset */
239 unsigned char *data
; /* section data */
240 unsigned long data_allocated
; /* used for realloc() handling */
241 int sh_name
; /* elf section name (only used during output) */
242 int sh_num
; /* elf section number */
243 int sh_type
; /* elf section type */
244 int sh_flags
; /* elf section flags */
245 int sh_info
; /* elf section info */
246 int sh_addralign
; /* elf section alignment */
247 int sh_entsize
; /* elf entry size */
248 unsigned long sh_size
; /* section size (only used during output) */
249 unsigned long sh_addr
; /* address at which the section is relocated */
250 unsigned long sh_offset
; /* file offset */
251 int nb_hashed_syms
; /* used to resize the hash table */
252 struct Section
*link
; /* link to another section */
253 struct Section
*reloc
; /* corresponding section for relocation, if any */
254 struct Section
*hash
; /* hash table for symbols */
255 struct Section
*next
;
256 char name
[1]; /* section name */
259 typedef struct DLLReference
{
265 /* GNUC attribute definition */
266 typedef struct AttributeDef
{
270 int func_attr
; /* calling convention, exports, ... */
273 /* -------------------------------------------------- */
274 /* gr: wrappers for casting sym->r for other purposes */
282 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
283 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
284 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
285 /* -------------------------------------------------- */
287 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
288 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
289 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
291 /* stored in 'Sym.c' field */
292 #define FUNC_NEW 1 /* ansi function prototype */
293 #define FUNC_OLD 2 /* old function prototype */
294 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
296 /* stored in 'Sym.r' field */
297 #define FUNC_CDECL 0 /* standard c call */
298 #define FUNC_STDCALL 1 /* pascal c call */
299 #define FUNC_FASTCALL1 2 /* first param in %eax */
300 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
301 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
302 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
304 /* field 'Sym.t' for macros */
305 #define MACRO_OBJ 0 /* object like macro */
306 #define MACRO_FUNC 1 /* function like macro */
308 /* field 'Sym.r' for C labels */
309 #define LABEL_DEFINED 0 /* label is defined */
310 #define LABEL_FORWARD 1 /* label is forward defined */
311 #define LABEL_DECLARED 2 /* label is declared but never used */
313 /* type_decl() types */
314 #define TYPE_ABSTRACT 1 /* type without variable */
315 #define TYPE_DIRECT 2 /* type with variable */
317 #define IO_BUF_SIZE 8192
319 typedef struct BufferedFile
{
323 int line_num
; /* current line number - here to simplify code */
324 int ifndef_macro
; /* #ifndef macro / #endif search */
325 int ifndef_macro_saved
; /* saved ifndef_macro */
326 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
327 char inc_type
; /* type of include */
328 char inc_filename
[512]; /* filename specified by the user */
329 char filename
[1024]; /* current filename - here to simplify code */
330 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
333 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
334 #define CH_EOF (-1) /* end of file */
336 /* parsing state (used to save parser state to reparse part of the
337 source several times) */
338 typedef struct ParseState
{
345 /* used to record tokens */
346 typedef struct TokenString
{
353 /* inline functions */
354 typedef struct InlineFunc
{
360 /* include file cache, used to find files faster and also to eliminate
361 inclusion if the include file is protected by #ifndef ... #endif */
362 typedef struct CachedInclude
{
364 int hash_next
; /* -1 if none */
365 char type
; /* '"' or '>' to give include type */
366 char filename
[1]; /* path specified in #include */
369 #define CACHED_INCLUDES_HASH_SIZE 512
371 #ifdef CONFIG_TCC_ASM
372 typedef struct ExprValue
{
377 #define MAX_ASM_OPERANDS 30
378 typedef struct ASMOperand
{
379 int id
; /* GCC 3 optionnal identifier (0 if number only supported */
381 char asm_str
[16]; /* computed asm string for operand */
382 SValue
*vt
; /* C value of the expression */
383 int ref_index
; /* if >= 0, gives reference to a output constraint */
384 int input_index
; /* if >= 0, gives reference to an input constraint */
385 int priority
; /* priority, used to assign registers */
386 int reg
; /* if >= 0, register number used for this operand */
387 int is_llong
; /* true if double register value */
388 int is_memory
; /* true if memory operand */
389 int is_rw
; /* for '+' modifier */
396 BufferedFile
**include_stack_ptr
;
397 int *ifdef_stack_ptr
;
399 /* include file handling */
400 char **include_paths
;
401 int nb_include_paths
;
402 char **sysinclude_paths
;
403 int nb_sysinclude_paths
;
404 CachedInclude
**cached_includes
;
405 int nb_cached_includes
;
407 char **library_paths
;
408 int nb_library_paths
;
410 /* array of all loaded dlls (including those referenced by loaded
412 DLLReference
**loaded_dlls
;
417 int nb_sections
; /* number of sections, including first dummy section */
419 Section
**priv_sections
;
420 int nb_priv_sections
; /* number of private sections */
425 unsigned long *got_offsets
;
427 /* give the correspondance from symtab indexes to dynsym indexes */
428 int *symtab_to_dynsym
;
430 /* temporary dynamic symbol sections (for dll loading) */
431 Section
*dynsymtab_section
;
432 /* exported dynamic symbol section */
435 int nostdinc
; /* if true, no standard headers are added */
436 int nostdlib
; /* if true, no standard libraries are added */
437 int nocommon
; /* if true, do not use common symbols for .bss data */
439 /* if true, static linking is performed */
442 /* soname as specified on the command line (-soname) */
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 /* compile with built-in memory and bounds checker */
475 /* give the path of the tcc libraries */
480 void (*error_func
)(void *opaque
, const char *msg
);
481 int error_set_jmp_enabled
;
485 jmp_buf error_jmp_buf
;
488 /* tiny assembler state */
491 /* see include_stack_ptr */
492 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
494 /* see ifdef_stack_ptr */
495 int ifdef_stack
[IFDEF_STACK_SIZE
];
497 /* see cached_includes */
498 int cached_includes_hash
[CACHED_INCLUDES_HASH_SIZE
];
501 int pack_stack
[PACK_STACK_SIZE
];
504 /* output file for preprocessing */
507 /* for tcc_relocate */
510 struct InlineFunc
**inline_fns
;
513 #ifndef TCC_TARGET_PE
514 #ifdef TCC_TARGET_X86_64
515 /* write PLT and GOT here */
516 char *runtime_plt_and_got
;
517 unsigned int runtime_plt_and_got_offset
;
522 /* The current value can be: */
523 #define VT_VALMASK 0x00ff
524 #define VT_CONST 0x00f0 /* constant in vc
525 (must be first non register value) */
526 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
527 #define VT_LOCAL 0x00f2 /* offset on stack */
528 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
529 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
530 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
531 #define VT_LVAL 0x0100 /* var is an lvalue */
532 #define VT_SYM 0x0200 /* a symbol value is added */
533 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
534 char/short stored in integer registers) */
535 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
536 dereferencing value */
537 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
538 bounding function call point is in vc */
539 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
540 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
541 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
542 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
545 #define VT_INT 0 /* integer type */
546 #define VT_BYTE 1 /* signed byte type */
547 #define VT_SHORT 2 /* short type */
548 #define VT_VOID 3 /* void type */
549 #define VT_PTR 4 /* pointer */
550 #define VT_ENUM 5 /* enum definition */
551 #define VT_FUNC 6 /* function type */
552 #define VT_STRUCT 7 /* struct/union definition */
553 #define VT_FLOAT 8 /* IEEE float */
554 #define VT_DOUBLE 9 /* IEEE double */
555 #define VT_LDOUBLE 10 /* IEEE long double */
556 #define VT_BOOL 11 /* ISOC99 boolean type */
557 #define VT_LLONG 12 /* 64 bit integer */
558 #define VT_LONG 13 /* long integer (NEVER USED as type, only
560 #define VT_BTYPE 0x000f /* mask for basic type */
561 #define VT_UNSIGNED 0x0010 /* unsigned type */
562 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
563 #define VT_BITFIELD 0x0040 /* bitfield modifier */
564 #define VT_CONSTANT 0x0800 /* const modifier */
565 #define VT_VOLATILE 0x1000 /* volatile modifier */
566 #define VT_SIGNED 0x2000 /* signed type */
569 #define VT_EXTERN 0x00000080 /* extern definition */
570 #define VT_STATIC 0x00000100 /* static variable */
571 #define VT_TYPEDEF 0x00000200 /* typedef definition */
572 #define VT_INLINE 0x00000400 /* inline definition */
574 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
576 /* type mask (except storage) */
577 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
578 #define VT_TYPE (~(VT_STORAGE))
582 /* warning: the following compare tokens depend on i386 asm code */
589 #define TOK_Nset 0x98
590 #define TOK_Nclear 0x99
596 #define TOK_LAND 0xa0
600 #define TOK_MID 0xa3 /* inc/dec, to void constant */
602 #define TOK_UDIV 0xb0 /* unsigned division */
603 #define TOK_UMOD 0xb1 /* unsigned modulo */
604 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
605 #define TOK_CINT 0xb3 /* number in tokc */
606 #define TOK_CCHAR 0xb4 /* char constant in tokc */
607 #define TOK_STR 0xb5 /* pointer to string in tokc */
608 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
609 #define TOK_LCHAR 0xb7
610 #define TOK_LSTR 0xb8
611 #define TOK_CFLOAT 0xb9 /* float constant */
612 #define TOK_LINENUM 0xba /* line number info */
613 #define TOK_CDOUBLE 0xc0 /* double constant */
614 #define TOK_CLDOUBLE 0xc1 /* long double constant */
615 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
616 #define TOK_ADDC1 0xc3 /* add with carry generation */
617 #define TOK_ADDC2 0xc4 /* add with carry use */
618 #define TOK_SUBC1 0xc5 /* add with carry generation */
619 #define TOK_SUBC2 0xc6 /* add with carry use */
620 #define TOK_CUINT 0xc8 /* unsigned int constant */
621 #define TOK_CLLONG 0xc9 /* long long constant */
622 #define TOK_CULLONG 0xca /* unsigned long long constant */
623 #define TOK_ARROW 0xcb
624 #define TOK_DOTS 0xcc /* three dots */
625 #define TOK_SHR 0xcd /* unsigned shift right */
626 #define TOK_PPNUM 0xce /* preprocessor number */
628 #define TOK_SHL 0x01 /* shift left */
629 #define TOK_SAR 0x02 /* signed shift right */
631 /* assignement operators : normal operator or 0x80 */
632 #define TOK_A_MOD 0xa5
633 #define TOK_A_AND 0xa6
634 #define TOK_A_MUL 0xaa
635 #define TOK_A_ADD 0xab
636 #define TOK_A_SUB 0xad
637 #define TOK_A_DIV 0xaf
638 #define TOK_A_XOR 0xde
639 #define TOK_A_OR 0xfc
640 #define TOK_A_SHL 0x81
641 #define TOK_A_SAR 0x82
644 #define offsetof(type, field) ((size_t) &((type *)0)->field)
648 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
651 #define TOK_EOF (-1) /* end of file */
652 #define TOK_LINEFEED 10 /* line feed */
654 /* all identificators and strings have token above that */
655 #define TOK_IDENT 256
657 /* only used for i386 asm opcodes definitions */
658 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
661 DEF(TOK_ASM_ ## x ## b, #x "b") \
662 DEF(TOK_ASM_ ## x ## w, #x "w") \
663 DEF(TOK_ASM_ ## x ## l, #x "l") \
664 DEF(TOK_ASM_ ## x, #x)
667 DEF(TOK_ASM_ ## x ## w, #x "w") \
668 DEF(TOK_ASM_ ## x ## l, #x "l") \
669 DEF(TOK_ASM_ ## x, #x)
672 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
673 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
674 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
675 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
678 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
679 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
682 #define DEF_ASMTEST(x) \
714 #define TOK_ASM_int TOK_INT
717 TOK_LAST
= TOK_IDENT
- 1,
718 #define DEF(id, str) id,
723 #define TOK_UIDENT TOK_DEFINE
726 #define snprintf _snprintf
727 #define vsnprintf _vsnprintf
729 #define strtold (long double)strtod
730 #define strtof (float)strtod
731 #define strtoll (long long)strtol
733 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
734 || defined(__OpenBSD__)
735 /* currently incorrect */
736 long double strtold(const char *nptr
, char **endptr
)
738 return (long double)strtod(nptr
, endptr
);
740 float strtof(const char *nptr
, char **endptr
)
742 return (float)strtod(nptr
, endptr
);
745 /* XXX: need to define this to use them in non ISOC99 context */
746 extern float strtof (const char *__nptr
, char **__endptr
);
747 extern long double strtold (const char *__nptr
, char **__endptr
);
751 #define IS_PATHSEP(c) (c == '/' || c == '\\')
752 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
753 #define PATHCMP stricmp
755 #define IS_PATHSEP(c) (c == '/')
756 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
757 #define PATHCMP strcmp
760 void error(const char *fmt
, ...);
761 void error_noabort(const char *fmt
, ...);
762 void warning(const char *fmt
, ...);
764 void tcc_set_lib_path_w32(TCCState
*s
);
765 int tcc_set_flag(TCCState
*s
, const char *flag_name
, int value
);
766 void tcc_print_stats(TCCState
*s
, int64_t total_time
);
768 void tcc_free(void *ptr
);
769 void *tcc_malloc(unsigned long size
);
770 void *tcc_mallocz(unsigned long size
);
771 void *tcc_realloc(void *ptr
, unsigned long size
);
772 char *tcc_strdup(const char *str
);
774 char *tcc_basename(const char *name
);
775 char *tcc_fileextension (const char *name
);
776 char *pstrcpy(char *buf
, int buf_size
, const char *s
);
777 char *pstrcat(char *buf
, int buf_size
, const char *s
);
778 void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
);
779 void dynarray_reset(void *pp
, int *n
);
781 #ifdef CONFIG_TCC_BACKTRACE
782 extern int num_callers
;
783 extern const char **rt_bound_error_msg
;
786 /* true if float/double/long double type */
787 static inline int is_float(int t
)
791 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
794 /* space exlcuding newline */
795 static inline int is_space(int ch
)
797 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';