Improve hash performance
[tinycc.git] / tcc.h
blob6fc3775373da6a02b088c612b6e79df25455bfa3
1 /*
2 * TCC - Tiny C Compiler
3 *
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
21 #ifndef _TCC_H
22 #define _TCC_H
24 #define _GNU_SOURCE
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <math.h>
33 #include <signal.h>
34 #include <fcntl.h>
35 #include <setjmp.h>
36 #include <time.h>
37 #include <sys/stat.h> /* stat() */
39 #ifdef CONFIG_TCCASSERT
40 #include <assert.h>
41 #define TCC_ASSERT(ex) assert(ex)
42 #else
43 #define TCC_ASSERT(ex)
44 #endif
46 #ifndef _WIN32
47 # include <unistd.h>
48 # include <sys/time.h>
49 # include <sys/ucontext.h>
50 # include <sys/mman.h>
51 # ifndef CONFIG_TCC_STATIC
52 # include <dlfcn.h>
53 # endif
54 /* XXX: need to define this to use them in non ISOC99 context */
55 extern float strtof (const char *__nptr, char **__endptr);
56 extern long double strtold (const char *__nptr, char **__endptr);
57 #else /* on _WIN32: */
58 # include <windows.h>
59 # include <sys/timeb.h>
60 # include <io.h> /* open, close etc. */
61 # include <direct.h> /* getcwd */
62 # ifdef __GNUC__
63 # include <stdint.h>
64 # endif
65 # define inline __inline
66 # define inp next_inp
67 # define snprintf _snprintf
68 # define vsnprintf _vsnprintf
69 # ifndef __GNUC__
70 # define strtold (long double)strtod
71 # define strtof (float)strtod
72 # define strtoll _strtoi64
73 # define strtoull _strtoui64
74 # endif
75 # ifdef LIBTCC_AS_DLL
76 # define LIBTCCAPI __declspec(dllexport)
77 # define PUB_FUNC LIBTCCAPI
78 # endif
79 # ifdef _MSC_VER
80 # pragma warning (disable : 4244) // conversion from 'uint64_t' to 'int', possible loss of data
81 # pragma warning (disable : 4267) // conversion from 'size_t' to 'int', possible loss of data
82 # pragma warning (disable : 4996) // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
83 # pragma warning (disable : 4018) // signed/unsigned mismatch
84 # pragma warning (disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
85 # endif
86 #endif
88 #ifndef O_BINARY
89 # define O_BINARY 0
90 #endif
92 #ifdef __GNUC__
93 # define NORETURN __attribute__ ((noreturn))
94 #elif defined _MSC_VER
95 # define NORETURN __declspec(noreturn)
96 #else
97 # define NORETURN
98 #endif
100 #ifdef _WIN32
101 # define IS_DIRSEP(c) (c == '/' || c == '\\')
102 # define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
103 # define PATHCMP stricmp
104 # define PATH_NOCASE
105 #else
106 # define IS_DIRSEP(c) (c == '/')
107 # define IS_ABSPATH(p) IS_DIRSEP(p[0])
108 # define PATHCMP strcmp
109 #endif
111 #ifdef TCC_TARGET_PE
112 #define PATHSEP ';'
113 #else
114 #define PATHSEP ':'
115 #endif
117 #include "elf.h"
118 #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
119 # define ELFCLASSW ELFCLASS64
120 # define ElfW(type) Elf##64##_##type
121 # define ELFW(type) ELF##64##_##type
122 # define ElfW_Rel ElfW(Rela)
123 # define SHT_RELX SHT_RELA
124 # define REL_SECTION_FMT ".rela%s"
125 /* XXX: DLL with PLT would only work with x86-64 for now */
126 # define TCC_OUTPUT_DLL_WITH_PLT
127 #else
128 # define ELFCLASSW ELFCLASS32
129 # define ElfW(type) Elf##32##_##type
130 # define ELFW(type) ELF##32##_##type
131 # define ElfW_Rel ElfW(Rel)
132 # define SHT_RELX SHT_REL
133 # define REL_SECTION_FMT ".rel%s"
134 #endif
136 /* target address type */
137 #define addr_t ElfW(Addr)
139 #include "stab.h"
140 #include "libtcc.h"
142 static inline uint16_t read16le(unsigned char *p)
144 return p[0] | (uint16_t)p[1] << 8;
147 static inline void write16le(unsigned char *p, uint16_t x)
149 p[0] = x & 255;
150 p[1] = x >> 8 & 255;
153 static inline uint32_t read32le(unsigned char *p)
155 return (p[0] | (uint32_t)p[1] << 8 |
156 (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24);
159 static inline void write32le(unsigned char *p, uint32_t x)
161 p[0] = x & 255;
162 p[1] = x >> 8 & 255;
163 p[2] = x >> 16 & 255;
164 p[3] = x >> 24 & 255;
167 static inline uint64_t read64le(unsigned char *p)
169 return (p[0] | (uint64_t)p[1] << 8 |
170 (uint64_t)p[2] << 16 | (uint64_t)p[3] << 24 |
171 (uint64_t)p[4] << 32 | (uint64_t)p[5] << 40 |
172 (uint64_t)p[6] << 48 | (uint64_t)p[7] << 56);
175 static inline void write64le(unsigned char *p, uint64_t x)
177 p[0] = x & 255;
178 p[1] = x >> 8 & 255;
179 p[2] = x >> 16 & 255;
180 p[3] = x >> 24 & 255;
181 p[4] = x >> 32 & 255;
182 p[5] = x >> 40 & 255;
183 p[6] = x >> 48 & 255;
184 p[7] = x >> 56 & 255;
187 /* parser debug */
188 /* #define PARSE_DEBUG */
189 /* preprocessor debug */
190 /* #define PP_DEBUG */
191 /* include file debug */
192 /* #define INC_DEBUG */
193 /* memory leak debug */
194 /* #define MEM_DEBUG */
195 /* assembler debug */
196 /* #define ASM_DEBUG */
198 /* target selection */
199 /* #define TCC_TARGET_I386 *//* i386 code generator */
200 /* #define TCC_TARGET_ARM *//* ARMv4 code generator */
201 /* #define TCC_TARGET_ARM64 *//* ARMv8 code generator */
202 /* #define TCC_TARGET_C67 *//* TMS320C67xx code generator */
203 /* #define TCC_TARGET_X86_64 *//* x86-64 code generator */
205 /* default target is I386 */
206 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
207 !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
208 !defined(TCC_TARGET_X86_64)
209 #define TCC_TARGET_I386
210 #endif
212 #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
213 !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
214 !defined(CONFIG_USE_LIBGCC)
215 #define CONFIG_TCC_BCHECK /* enable bound checking code */
216 #endif
218 /* define it to include assembler support */
219 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_ARM64) && \
220 !defined(TCC_TARGET_C67)
221 #define CONFIG_TCC_ASM
222 #endif
224 /* object format selection */
225 #if defined(TCC_TARGET_C67)
226 #define TCC_TARGET_COFF
227 #endif
229 /* only native compiler supports -run */
230 #if defined _WIN32 == defined TCC_TARGET_PE
231 # if (defined __i386__ || defined _X86_) && defined TCC_TARGET_I386
232 # define TCC_IS_NATIVE
233 # elif (defined __x86_64__ || defined _AMD64_) && defined TCC_TARGET_X86_64
234 # define TCC_IS_NATIVE
235 # elif defined __arm__ && defined TCC_TARGET_ARM
236 # define TCC_IS_NATIVE
237 # elif defined __aarch64__ && defined TCC_TARGET_ARM64
238 # define TCC_IS_NATIVE
239 # endif
240 #endif
242 #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
243 # define CONFIG_TCC_BACKTRACE
244 #endif
246 /* ------------ path configuration ------------ */
248 #ifndef CONFIG_SYSROOT
249 # define CONFIG_SYSROOT ""
250 #endif
251 #ifndef CONFIG_TCCDIR
252 # define CONFIG_TCCDIR "."
253 #endif
254 #ifndef CONFIG_LDDIR
255 # ifdef TCC_TARGET_X86_64
256 # define CONFIG_LDDIR "lib64"
257 # else
258 # define CONFIG_LDDIR "lib"
259 # endif
260 #endif
262 #ifdef CONFIG_MULTIARCHDIR
263 # define USE_MUADIR(s) s "/" CONFIG_MULTIARCHDIR
264 # define ALSO_MUADIR(s) s "/" CONFIG_MULTIARCHDIR ":" s
265 #else
266 # define USE_MUADIR(s) s
267 # define ALSO_MUADIR(s) s
268 #endif
270 /* path to find crt1.o, crti.o and crtn.o */
271 #ifndef CONFIG_TCC_CRTPREFIX
272 # define CONFIG_TCC_CRTPREFIX USE_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
273 #endif
275 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
277 /* system include paths */
278 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
279 # ifdef TCC_TARGET_PE
280 # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi"
281 # else
282 # define CONFIG_TCC_SYSINCLUDEPATHS \
283 "{B}/include" \
284 ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/include") \
285 ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/include")
286 # endif
287 #endif
289 /* library search paths */
290 #ifndef CONFIG_TCC_LIBPATHS
291 # ifdef TCC_TARGET_PE
292 # define CONFIG_TCC_LIBPATHS "{B}/lib"
293 # else
294 # define CONFIG_TCC_LIBPATHS \
295 ALSO_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
296 ":" ALSO_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
297 ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
298 # endif
299 #endif
301 /* name of ELF interpreter */
302 #ifndef CONFIG_TCC_ELFINTERP
303 # if defined __FreeBSD__
304 # define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
305 # elif defined __FreeBSD_kernel__
306 # if defined(TCC_TARGET_X86_64)
307 # define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
308 # else
309 # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
310 # endif
311 # elif defined __DragonFly__
312 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2"
313 # elif defined __NetBSD__
314 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
315 # elif defined __GNU__
316 # define CONFIG_TCC_ELFINTERP "/lib/ld.so"
317 # elif defined(TCC_TARGET_PE)
318 # define CONFIG_TCC_ELFINTERP "-"
319 # elif defined(TCC_UCLIBC)
320 # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */
321 # elif defined TCC_TARGET_ARM64
322 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
323 # elif defined(TCC_TARGET_X86_64)
324 # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
325 # elif !defined(TCC_ARM_EABI)
326 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
327 # endif
328 #endif
330 /* var elf_interp dans *-gen.c */
331 #ifdef CONFIG_TCC_ELFINTERP
332 # define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP
333 #else
334 # define DEFAULT_ELFINTERP(s) default_elfinterp(s)
335 #endif
337 /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
338 #define TCC_LIBGCC USE_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
340 /* -------------------------------------------- */
341 /* include the target specific definitions */
343 #define TARGET_DEFS_ONLY
344 #ifdef TCC_TARGET_I386
345 # include "i386-gen.c"
346 #endif
347 #ifdef TCC_TARGET_X86_64
348 # include "x86_64-gen.c"
349 #endif
350 #ifdef TCC_TARGET_ARM
351 # include "arm-gen.c"
352 #endif
353 #ifdef TCC_TARGET_ARM64
354 # include "arm64-gen.c"
355 #endif
356 #ifdef TCC_TARGET_C67
357 # include "coff.h"
358 # include "c67-gen.c"
359 #endif
360 #undef TARGET_DEFS_ONLY
362 /* -------------------------------------------- */
364 #define INCLUDE_STACK_SIZE 32
365 #define IFDEF_STACK_SIZE 64
366 #define VSTACK_SIZE 256
367 #define STRING_MAX_SIZE 1024
368 #define TOKSTR_MAX_SIZE 256
369 #define PACK_STACK_SIZE 8
371 #define TOK_HASH_SIZE 16384 /* must be a power of two */
372 #define TOK_ALLOC_INCR 512 /* must be a power of two */
373 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
375 /* token symbol management */
376 typedef struct TokenSym {
377 struct TokenSym *hash_next;
378 struct Sym *sym_define; /* direct pointer to define */
379 struct Sym *sym_label; /* direct pointer to label */
380 struct Sym *sym_struct; /* direct pointer to structure */
381 struct Sym *sym_identifier; /* direct pointer to identifier */
382 int tok; /* token number */
383 int len;
384 char str[1];
385 } TokenSym;
387 #ifdef TCC_TARGET_PE
388 typedef unsigned short nwchar_t;
389 #else
390 typedef int nwchar_t;
391 #endif
393 typedef struct CString {
394 int size; /* size in bytes */
395 void *data; /* either 'char *' or 'nwchar_t *' */
396 int size_allocated;
397 void *data_allocated; /* if non NULL, data has been malloced */
398 } CString;
400 /* type definition */
401 typedef struct CType {
402 int t;
403 struct Sym *ref;
404 } CType;
406 /* constant value */
407 typedef union CValue {
408 long double ld;
409 double d;
410 float f;
411 uint64_t i;
412 struct {
413 int size;
414 const void *data;
415 void *data_allocated;
416 } str;
417 int tab[LDOUBLE_SIZE/4];
418 } CValue;
420 /* value on stack */
421 typedef struct SValue {
422 CType type; /* type */
423 unsigned short r; /* register + flags */
424 unsigned short r2; /* second register, used for 'long long'
425 type. If not used, set to VT_CONST */
426 CValue c; /* constant, if VT_CONST */
427 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
428 } SValue;
430 struct Attribute {
431 unsigned
432 func_call : 3, /* calling convention (0..5), see below */
433 aligned : 5, /* alignement (0..16) */
434 packed : 1,
435 func_export : 1,
436 func_import : 1,
437 func_args : 5,
438 func_proto : 1,
439 mode : 4,
440 weak : 1,
441 visibility : 2,
442 fill : 8; // 8 bits left to fit well in union below
445 /* GNUC attribute definition */
446 typedef struct AttributeDef {
447 struct Attribute a;
448 struct Section *section;
449 int alias_target; /* token */
450 int asm_label; /* associated asm label */
451 } AttributeDef;
453 /* symbol management */
454 typedef struct Sym {
455 int v; /* symbol token */
456 int asm_label; /* associated asm label */
457 union {
458 long r; /* associated register */
459 struct Attribute a;
461 union {
462 long c; /* associated number */
463 int *d; /* define token stream */
465 CType type; /* associated type */
466 union {
467 struct Sym *next; /* next related symbol */
468 long jnext; /* next jump label */
470 struct Sym *prev; /* prev symbol in stack */
471 struct Sym *prev_tok; /* previous symbol for this token */
472 } Sym;
474 /* section definition */
475 /* XXX: use directly ELF structure for parameters ? */
476 /* special flag to indicate that the section should not be linked to
477 the other ones */
478 #define SHF_PRIVATE 0x80000000
480 /* special flag, too */
481 #define SECTION_ABS ((void *)1)
483 typedef struct Section {
484 unsigned long data_offset; /* current data offset */
485 unsigned char *data; /* section data */
486 unsigned long data_allocated; /* used for realloc() handling */
487 int sh_name; /* elf section name (only used during output) */
488 int sh_num; /* elf section number */
489 int sh_type; /* elf section type */
490 int sh_flags; /* elf section flags */
491 int sh_info; /* elf section info */
492 int sh_addralign; /* elf section alignment */
493 int sh_entsize; /* elf entry size */
494 unsigned long sh_size; /* section size (only used during output) */
495 addr_t sh_addr; /* address at which the section is relocated */
496 unsigned long sh_offset; /* file offset */
497 int nb_hashed_syms; /* used to resize the hash table */
498 struct Section *link; /* link to another section */
499 struct Section *reloc; /* corresponding section for relocation, if any */
500 struct Section *hash; /* hash table for symbols */
501 struct Section *next;
502 char name[1]; /* section name */
503 } Section;
505 typedef struct DLLReference {
506 int level;
507 void *handle;
508 char name[1];
509 } DLLReference;
511 /* -------------------------------------------------- */
513 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
514 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
515 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
517 /* stored in 'Sym.c' field */
518 #define FUNC_NEW 1 /* ansi function prototype */
519 #define FUNC_OLD 2 /* old function prototype */
520 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
522 /* stored in 'Sym.r' field */
523 #define FUNC_CDECL 0 /* standard c call */
524 #define FUNC_STDCALL 1 /* pascal c call */
525 #define FUNC_FASTCALL1 2 /* first param in %eax */
526 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
527 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
528 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
530 /* field 'Sym.t' for macros */
531 #define MACRO_OBJ 0 /* object like macro */
532 #define MACRO_FUNC 1 /* function like macro */
534 /* field 'Sym.r' for C labels */
535 #define LABEL_DEFINED 0 /* label is defined */
536 #define LABEL_FORWARD 1 /* label is forward defined */
537 #define LABEL_DECLARED 2 /* label is declared but never used */
539 /* type_decl() types */
540 #define TYPE_ABSTRACT 1 /* type without variable */
541 #define TYPE_DIRECT 2 /* type with variable */
543 #define IO_BUF_SIZE 8192
545 typedef struct BufferedFile {
546 uint8_t *buf_ptr;
547 uint8_t *buf_end;
548 int fd;
549 struct BufferedFile *prev;
550 int line_num; /* current line number - here to simplify code */
551 int line_ref; /* tcc -E: last printed line */
552 int ifndef_macro; /* #ifndef macro / #endif search */
553 int ifndef_macro_saved; /* saved ifndef_macro */
554 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
555 int include_next_index; /* next search path */
556 char filename[1024]; /* filename */
557 unsigned char unget[4];
558 unsigned char buffer[1]; /* extra size for CH_EOB char */
559 } BufferedFile;
561 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
562 #define CH_EOF (-1) /* end of file */
564 /* parsing state (used to save parser state to reparse part of the
565 source several times) */
566 typedef struct ParseState {
567 const int *macro_ptr;
568 int line_num;
569 int tok;
570 CValue tokc;
571 } ParseState;
573 /* used to record tokens */
574 typedef struct TokenString {
575 int *str;
576 int len;
577 int allocated_len;
578 int last_line_num;
579 /* used to chain token-strings with begin/end_macro() */
580 struct TokenString *prev;
581 const int *prev_ptr;
582 char alloc;
583 } TokenString;
585 /* inline functions */
586 typedef struct InlineFunc {
587 TokenString func_str;
588 Sym *sym;
589 char filename[1];
590 } InlineFunc;
592 /* include file cache, used to find files faster and also to eliminate
593 inclusion if the include file is protected by #ifndef ... #endif */
594 typedef struct CachedInclude {
595 int ifndef_macro;
596 int hash_next; /* -1 if none */
597 char filename[1]; /* path specified in #include */
598 } CachedInclude;
600 #define CACHED_INCLUDES_HASH_SIZE 512
602 #ifdef CONFIG_TCC_ASM
603 typedef struct ExprValue {
604 uint32_t v;
605 Sym *sym;
606 } ExprValue;
608 #define MAX_ASM_OPERANDS 30
609 typedef struct ASMOperand {
610 int id; /* GCC 3 optionnal identifier (0 if number only supported */
611 char *constraint;
612 char asm_str[16]; /* computed asm string for operand */
613 SValue *vt; /* C value of the expression */
614 int ref_index; /* if >= 0, gives reference to a output constraint */
615 int input_index; /* if >= 0, gives reference to an input constraint */
616 int priority; /* priority, used to assign registers */
617 int reg; /* if >= 0, register number used for this operand */
618 int is_llong; /* true if double register value */
619 int is_memory; /* true if memory operand */
620 int is_rw; /* for '+' modifier */
621 } ASMOperand;
622 #endif
624 struct sym_attr {
625 unsigned long got_offset;
626 unsigned long plt_offset;
627 #ifdef TCC_TARGET_ARM
628 unsigned char plt_thumb_stub:1;
629 #endif
632 typedef struct ParseArgsState
634 int run;
635 int pthread;
636 int filetype;
637 CString linker_arg; /* collect -Wl options for input such as "-Wl,-rpath -Wl,<path>" */
638 } ParseArgsState;
640 struct TCCState {
642 int verbose; /* if true, display some information during compilation */
643 int nostdinc; /* if true, no standard headers are added */
644 int nostdlib; /* if true, no standard libraries are added */
645 int nocommon; /* if true, do not use common symbols for .bss data */
646 int static_link; /* if true, static linking is performed */
647 int rdynamic; /* if true, all symbols are exported */
648 int symbolic; /* if true, resolve symbols in the current module first */
649 int alacarte_link; /* if true, only link in referenced objects from archive */
651 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
652 char *soname; /* as specified on the command line (-soname) */
653 char *rpath; /* as specified on the command line (-Wl,-rpath=) */
655 /* output type, see TCC_OUTPUT_XXX */
656 int output_type;
657 /* output format, see TCC_OUTPUT_FORMAT_xxx */
658 int output_format;
660 /* C language options */
661 int char_is_unsigned;
662 int leading_underscore;
663 int ms_extensions; /* allow nested named struct w/o identifier behave like unnamed */
664 int old_struct_init_code; /* use old algorithm to init array in struct when there is no '{' used.
665 Liuux 2.4.26 can't find initrd when compiled with a new algorithm */
666 int dollars_in_identifiers; /* allows '$' char in indentifiers */
667 int normalize_inc_dirs; /* remove non-existent or duplicate directories from include paths */
669 /* warning switches */
670 int warn_write_strings;
671 int warn_unsupported;
672 int warn_error;
673 int warn_none;
674 int warn_implicit_function_declaration;
676 /* compile with debug symbol (and use them if error during execution) */
677 int do_debug;
678 int do_strip;
679 #ifdef CONFIG_TCC_BCHECK
680 /* compile with built-in memory and bounds checker */
681 int do_bounds_check;
682 #endif
683 #ifdef TCC_TARGET_ARM
684 enum float_abi float_abi; /* float ABI of the generated code*/
685 #endif
687 addr_t text_addr; /* address of text section */
688 int has_text_addr;
690 unsigned long section_align; /* section alignment */
692 char *init_symbol; /* symbols to call at load-time (not used currently) */
693 char *fini_symbol; /* symbols to call at unload-time (not used currently) */
695 #ifdef TCC_TARGET_I386
696 int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
697 #endif
699 /* array of all loaded dlls (including those referenced by loaded dlls) */
700 DLLReference **loaded_dlls;
701 int nb_loaded_dlls;
703 /* include paths */
704 char **include_paths;
705 int nb_include_paths;
707 char **sysinclude_paths;
708 int nb_sysinclude_paths;
710 /* library paths */
711 char **library_paths;
712 int nb_library_paths;
714 /* crt?.o object path */
715 char **crt_paths;
716 int nb_crt_paths;
718 /* error handling */
719 void *error_opaque;
720 void (*error_func)(void *opaque, const char *msg);
721 int error_set_jmp_enabled;
722 jmp_buf error_jmp_buf;
723 int nb_errors;
725 /* output file for preprocessing (-E) */
726 FILE *ppfp;
727 enum {
728 LINE_MACRO_OUTPUT_FORMAT_GCC,
729 LINE_MACRO_OUTPUT_FORMAT_NONE,
730 LINE_MACRO_OUTPUT_FORMAT_STD
731 } Pflag; /* -P switch */
732 char dflag; /* -dX value */
733 FILE *dffp;
735 /* for -MD/-MF: collected dependencies for this compilation */
736 char **target_deps;
737 int nb_target_deps;
739 /* compilation */
740 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
741 BufferedFile **include_stack_ptr;
743 int ifdef_stack[IFDEF_STACK_SIZE];
744 int *ifdef_stack_ptr;
746 /* included files enclosed with #ifndef MACRO */
747 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
748 CachedInclude **cached_includes;
749 int nb_cached_includes;
751 /* #pragma pack stack */
752 int pack_stack[PACK_STACK_SIZE];
753 int *pack_stack_ptr;
754 char **pragma_libs;
755 int nb_pragma_libs;
757 /* inline functions are stored as token lists and compiled last
758 only if referenced */
759 struct InlineFunc **inline_fns;
760 int nb_inline_fns;
762 /* sections */
763 Section **sections;
764 int nb_sections; /* number of sections, including first dummy section */
766 Section **priv_sections;
767 int nb_priv_sections; /* number of private sections */
769 /* got & plt handling */
770 Section *got;
771 Section *plt;
772 struct sym_attr *sym_attrs;
773 int nb_sym_attrs;
774 /* give the correspondance from symtab indexes to dynsym indexes */
775 int *symtab_to_dynsym;
777 /* temporary dynamic symbol sections (for dll loading) */
778 Section *dynsymtab_section;
779 /* exported dynamic symbol section */
780 Section *dynsym;
781 /* copy of the gobal symtab_section variable */
782 Section *symtab;
783 /* tiny assembler state */
784 Sym *asm_labels;
786 #ifdef TCC_TARGET_PE
787 /* PE info */
788 int pe_subsystem;
789 unsigned pe_file_align;
790 unsigned pe_stack_size;
791 # ifdef TCC_TARGET_X86_64
792 Section *uw_pdata;
793 int uw_sym;
794 unsigned uw_offs;
795 # endif
796 #endif
798 #ifdef TCC_IS_NATIVE
799 const char *runtime_main;
800 /* for tcc_relocate */
801 void *runtime_mem;
802 # ifdef HAVE_SELINUX
803 void *write_mem;
804 unsigned long mem_size;
805 # endif
806 #endif
808 /* used by main and tcc_parse_args only */
809 char **files; /* files seen on command line */
810 int nb_files; /* number thereof */
811 int nb_libraries; /* number of libs thereof */
812 char *outfile; /* output filename */
813 char *option_m; /* only -m32/-m64 handled */
814 int print_search_dirs; /* option */
815 int option_r; /* option -r */
816 int option_C; /* option -C, keep comments when -E */
817 int do_bench; /* option -bench */
818 int gen_deps; /* option -MD */
819 char *deps_outfile; /* option -MF */
820 ParseArgsState *parse_args_state;
823 /* The current value can be: */
824 #define VT_VALMASK 0x003f /* mask for value location, register or: */
825 #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
826 #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
827 #define VT_LOCAL 0x0032 /* offset on stack */
828 #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
829 #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
830 #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
831 #define VT_REF 0x0040 /* value is pointer to structure rather than address */
832 #define VT_LVAL 0x0100 /* var is an lvalue */
833 #define VT_SYM 0x0200 /* a symbol value is added */
834 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
835 char/short stored in integer registers) */
836 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
837 dereferencing value */
838 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
839 bounding function call point is in vc */
840 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
841 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
842 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
843 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
845 /* types */
846 #define VT_BTYPE 0x000f /* mask for basic type */
847 #define VT_INT 0 /* integer type */
848 #define VT_BYTE 1 /* signed byte type */
849 #define VT_SHORT 2 /* short type */
850 #define VT_VOID 3 /* void type */
851 #define VT_PTR 4 /* pointer */
852 #define VT_ENUM 5 /* enum definition */
853 #define VT_FUNC 6 /* function type */
854 #define VT_STRUCT 7 /* struct/union definition */
855 #define VT_FLOAT 8 /* IEEE float */
856 #define VT_DOUBLE 9 /* IEEE double */
857 #define VT_LDOUBLE 10 /* IEEE long double */
858 #define VT_BOOL 11 /* ISOC99 boolean type */
859 #define VT_LLONG 12 /* 64 bit integer */
860 #define VT_LONG 13 /* long integer (NEVER USED as type, only
861 during parsing) */
862 #define VT_QLONG 14 /* 128-bit integer. Only used for x86-64 ABI */
863 #define VT_QFLOAT 15 /* 128-bit float. Only used for x86-64 ABI */
864 #define VT_UNSIGNED 0x0010 /* unsigned type */
865 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
866 #define VT_BITFIELD 0x0040 /* bitfield modifier */
867 #define VT_CONSTANT 0x0800 /* const modifier */
868 #define VT_VOLATILE 0x1000 /* volatile modifier */
869 #define VT_DEFSIGN 0x2000 /* signed type */
870 #define VT_VLA 0x00020000 /* VLA type (also has VT_PTR and VT_ARRAY) */
872 /* storage */
873 #define VT_EXTERN 0x00000080 /* extern definition */
874 #define VT_STATIC 0x00000100 /* static variable */
875 #define VT_TYPEDEF 0x00000200 /* typedef definition */
876 #define VT_INLINE 0x00000400 /* inline definition */
877 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
878 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
879 #define VT_WEAK 0x00010000 /* weak symbol */
880 #define VT_TLS 0x00040000 /* thread-local storage */
881 #define VT_VIS_SHIFT 19 /* shift for symbol visibility, overlapping
882 bitfield values, because bitfields never
883 have linkage and hence never have
884 visibility. */
885 #define VT_VIS_SIZE 2 /* We have four visibilities. */
886 #define VT_VIS_MASK (((1 << VT_VIS_SIZE)-1) << VT_VIS_SHIFT)
888 #define VT_STRUCT_SHIFT 19 /* shift for bitfield shift values (max: 32 - 2*6) */
891 /* type mask (except storage) */
892 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK | VT_VIS_MASK)
893 #define VT_TYPE (~(VT_STORAGE))
895 /* token values */
897 /* warning: the following compare tokens depend on i386 asm code */
898 #define TOK_ULT 0x92
899 #define TOK_UGE 0x93
900 #define TOK_EQ 0x94
901 #define TOK_NE 0x95
902 #define TOK_ULE 0x96
903 #define TOK_UGT 0x97
904 #define TOK_Nset 0x98
905 #define TOK_Nclear 0x99
906 #define TOK_LT 0x9c
907 #define TOK_GE 0x9d
908 #define TOK_LE 0x9e
909 #define TOK_GT 0x9f
911 #define TOK_LAND 0xa0
912 #define TOK_LOR 0xa1
913 #define TOK_DEC 0xa2
914 #define TOK_MID 0xa3 /* inc/dec, to void constant */
915 #define TOK_INC 0xa4
916 #define TOK_UDIV 0xb0 /* unsigned division */
917 #define TOK_UMOD 0xb1 /* unsigned modulo */
918 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
920 /* tokens that carry values (in additional token string space / tokc) --> */
921 #define TOK_CCHAR 0xb3 /* char constant in tokc */
922 #define TOK_LCHAR 0xb4
923 #define TOK_CINT 0xb5 /* number in tokc */
924 #define TOK_CUINT 0xb6 /* unsigned int constant */
925 #define TOK_CLLONG 0xb7 /* long long constant */
926 #define TOK_CULLONG 0xb8 /* unsigned long long constant */
927 #define TOK_STR 0xb9 /* pointer to string in tokc */
928 #define TOK_LSTR 0xba
929 #define TOK_CFLOAT 0xbb /* float constant */
930 #define TOK_CDOUBLE 0xbc /* double constant */
931 #define TOK_CLDOUBLE 0xbd /* long double constant */
932 #define TOK_PPNUM 0xbe /* preprocessor number */
933 #define TOK_PPSTR 0xbf /* preprocessor string */
934 #define TOK_LINENUM 0xc0 /* line number info */
935 /* <-- */
937 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
938 #define TOK_ADDC1 0xc3 /* add with carry generation */
939 #define TOK_ADDC2 0xc4 /* add with carry use */
940 #define TOK_SUBC1 0xc5 /* add with carry generation */
941 #define TOK_SUBC2 0xc6 /* add with carry use */
942 #define TOK_ARROW 0xc7
943 #define TOK_DOTS 0xc8 /* three dots */
944 #define TOK_SHR 0xc9 /* unsigned shift right */
945 #define TOK_TWOSHARPS 0xca /* ## preprocessing token */
946 #define TOK_PLCHLDR 0xcb /* placeholder token as defined in C99 */
947 #define TOK_NOSUBST 0xcc /* means following token has already been pp'd */
949 #define TOK_SHL 0x01 /* shift left */
950 #define TOK_SAR 0x02 /* signed shift right */
952 /* assignement operators : normal operator or 0x80 */
953 #define TOK_A_MOD 0xa5
954 #define TOK_A_AND 0xa6
955 #define TOK_A_MUL 0xaa
956 #define TOK_A_ADD 0xab
957 #define TOK_A_SUB 0xad
958 #define TOK_A_DIV 0xaf
959 #define TOK_A_XOR 0xde
960 #define TOK_A_OR 0xfc
961 #define TOK_A_SHL 0x81
962 #define TOK_A_SAR 0x82
964 #ifndef offsetof
965 #define offsetof(type, field) ((size_t) &((type *)0)->field)
966 #endif
968 #ifndef countof
969 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
970 #endif
972 #define TOK_EOF (-1) /* end of file */
973 #define TOK_LINEFEED 10 /* line feed */
975 /* all identificators and strings have token above that */
976 #define TOK_IDENT 256
978 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
979 #define TOK_ASM_int TOK_INT
980 #define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
981 #define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
982 #define TOK_ASMDIR_LAST TOK_ASMDIR_section
984 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
985 /* only used for i386 asm opcodes definitions */
986 #define DEF_BWL(x) \
987 DEF(TOK_ASM_ ## x ## b, #x "b") \
988 DEF(TOK_ASM_ ## x ## w, #x "w") \
989 DEF(TOK_ASM_ ## x ## l, #x "l") \
990 DEF(TOK_ASM_ ## x, #x)
991 #define DEF_WL(x) \
992 DEF(TOK_ASM_ ## x ## w, #x "w") \
993 DEF(TOK_ASM_ ## x ## l, #x "l") \
994 DEF(TOK_ASM_ ## x, #x)
995 #ifdef TCC_TARGET_X86_64
996 # define DEF_BWLQ(x) \
997 DEF(TOK_ASM_ ## x ## b, #x "b") \
998 DEF(TOK_ASM_ ## x ## w, #x "w") \
999 DEF(TOK_ASM_ ## x ## l, #x "l") \
1000 DEF(TOK_ASM_ ## x ## q, #x "q") \
1001 DEF(TOK_ASM_ ## x, #x)
1002 # define DEF_WLQ(x) \
1003 DEF(TOK_ASM_ ## x ## w, #x "w") \
1004 DEF(TOK_ASM_ ## x ## l, #x "l") \
1005 DEF(TOK_ASM_ ## x ## q, #x "q") \
1006 DEF(TOK_ASM_ ## x, #x)
1007 # define DEF_BWLX DEF_BWLQ
1008 # define DEF_WLX DEF_WLQ
1009 /* number of sizes + 1 */
1010 # define NBWLX 5
1011 #else
1012 # define DEF_BWLX DEF_BWL
1013 # define DEF_WLX DEF_WL
1014 /* number of sizes + 1 */
1015 # define NBWLX 4
1016 #endif
1018 #define DEF_FP1(x) \
1019 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
1020 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
1021 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
1022 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
1024 #define DEF_FP(x) \
1025 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
1026 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
1027 DEF_FP1(x)
1029 #define DEF_ASMTEST(x,suffix) \
1030 DEF_ASM(x ## o ## suffix) \
1031 DEF_ASM(x ## no ## suffix) \
1032 DEF_ASM(x ## b ## suffix) \
1033 DEF_ASM(x ## c ## suffix) \
1034 DEF_ASM(x ## nae ## suffix) \
1035 DEF_ASM(x ## nb ## suffix) \
1036 DEF_ASM(x ## nc ## suffix) \
1037 DEF_ASM(x ## ae ## suffix) \
1038 DEF_ASM(x ## e ## suffix) \
1039 DEF_ASM(x ## z ## suffix) \
1040 DEF_ASM(x ## ne ## suffix) \
1041 DEF_ASM(x ## nz ## suffix) \
1042 DEF_ASM(x ## be ## suffix) \
1043 DEF_ASM(x ## na ## suffix) \
1044 DEF_ASM(x ## nbe ## suffix) \
1045 DEF_ASM(x ## a ## suffix) \
1046 DEF_ASM(x ## s ## suffix) \
1047 DEF_ASM(x ## ns ## suffix) \
1048 DEF_ASM(x ## p ## suffix) \
1049 DEF_ASM(x ## pe ## suffix) \
1050 DEF_ASM(x ## np ## suffix) \
1051 DEF_ASM(x ## po ## suffix) \
1052 DEF_ASM(x ## l ## suffix) \
1053 DEF_ASM(x ## nge ## suffix) \
1054 DEF_ASM(x ## nl ## suffix) \
1055 DEF_ASM(x ## ge ## suffix) \
1056 DEF_ASM(x ## le ## suffix) \
1057 DEF_ASM(x ## ng ## suffix) \
1058 DEF_ASM(x ## nle ## suffix) \
1059 DEF_ASM(x ## g ## suffix)
1061 #endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
1063 enum tcc_token {
1064 TOK_LAST = TOK_IDENT - 1
1065 #define DEF(id, str) ,id
1066 #include "tcctok.h"
1067 #undef DEF
1070 #define TOK_UIDENT TOK_DEFINE
1072 /* space exlcuding newline */
1073 static inline int is_space(int ch)
1075 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1078 static inline int isid(int c)
1080 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
1083 static inline int isnum(int c)
1085 return c >= '0' && c <= '9';
1088 static inline int isoct(int c)
1090 return c >= '0' && c <= '7';
1093 static inline int toup(int c)
1095 return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
1098 #ifndef PUB_FUNC
1099 # define PUB_FUNC
1100 #endif
1102 #ifdef ONE_SOURCE
1103 #define ST_INLN static inline
1104 #define ST_FUNC static
1105 #define ST_DATA static
1106 #else
1107 #define ST_INLN
1108 #define ST_FUNC
1109 #define ST_DATA extern
1110 #endif
1112 /* ------------ libtcc.c ------------ */
1114 /* use GNU C extensions */
1115 ST_DATA int gnu_ext;
1116 /* use Tiny C extensions */
1117 ST_DATA int tcc_ext;
1118 /* XXX: get rid of this ASAP */
1119 ST_DATA struct TCCState *tcc_state;
1121 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
1122 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
1123 #define AFF_PREPROCESS 0x0004 /* preprocess file */
1125 /* public functions currently used by the tcc main function */
1126 PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
1127 PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
1128 PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num);
1129 PUB_FUNC char *tcc_basename(const char *name);
1130 PUB_FUNC char *tcc_fileextension (const char *name);
1132 #ifndef MEM_DEBUG
1133 PUB_FUNC void tcc_free(void *ptr);
1134 PUB_FUNC void *tcc_malloc(unsigned long size);
1135 PUB_FUNC void *tcc_mallocz(unsigned long size);
1136 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
1137 PUB_FUNC char *tcc_strdup(const char *str);
1138 #else
1139 #define tcc_free(ptr) tcc_free_debug(ptr)
1140 #define tcc_malloc(size) tcc_malloc_debug(size, __FILE__, __LINE__)
1141 #define tcc_mallocz(size) tcc_mallocz_debug(size, __FILE__, __LINE__)
1142 #define tcc_realloc(ptr,size) tcc_realloc_debug(ptr, size, __FILE__, __LINE__)
1143 #define tcc_strdup(str) tcc_strdup_debug(str, __FILE__, __LINE__)
1144 PUB_FUNC void tcc_free_debug(void *ptr);
1145 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line);
1146 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line);
1147 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line);
1148 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line);
1149 #endif
1151 #define free(p) use_tcc_free(p)
1152 #define malloc(s) use_tcc_malloc(s)
1153 #define realloc(p, s) use_tcc_realloc(p, s)
1154 #undef strdup
1155 #define strdup(s) use_tcc_strdup(s)
1156 PUB_FUNC void tcc_memstats(int bench);
1157 PUB_FUNC void tcc_error_noabort(const char *fmt, ...);
1158 PUB_FUNC NORETURN void tcc_error(const char *fmt, ...);
1159 PUB_FUNC void tcc_warning(const char *fmt, ...);
1161 /* other utilities */
1162 ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);
1163 ST_FUNC void dynarray_reset(void *pp, int *n);
1164 ST_FUNC void cstr_ccat(CString *cstr, int ch);
1165 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len);
1166 ST_FUNC void cstr_wccat(CString *cstr, int ch);
1167 ST_FUNC void cstr_new(CString *cstr);
1168 ST_FUNC void cstr_free(CString *cstr);
1169 ST_FUNC void cstr_reset(CString *cstr);
1171 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
1172 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
1173 ST_FUNC void *section_ptr_add(Section *sec, addr_t size);
1174 ST_FUNC void section_reserve(Section *sec, unsigned long size);
1175 ST_FUNC Section *find_section(TCCState *s1, const char *name);
1177 ST_FUNC void put_extern_sym2(Sym *sym, Section *section, addr_t value, unsigned long size, int can_add_underscore);
1178 ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
1179 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
1180 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, addr_t addend);
1182 ST_INLN void sym_free(Sym *sym);
1183 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c);
1184 ST_FUNC Sym *sym_find2(Sym *s, int v);
1185 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
1186 ST_FUNC void sym_pop(Sym **ptop, Sym *b);
1187 ST_INLN Sym *struct_find(int v);
1188 ST_INLN Sym *sym_find(int v);
1189 ST_FUNC Sym *global_identifier_push(int v, int t, int c);
1191 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
1192 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
1193 ST_FUNC void tcc_close(void);
1195 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype);
1196 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
1198 #ifndef TCC_TARGET_PE
1199 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
1200 #endif
1202 ST_FUNC void tcc_add_pragma_libs(TCCState *s1);
1203 PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f);
1205 PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time);
1206 PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv);
1207 PUB_FUNC void tcc_set_environment(TCCState *s);
1209 /* ------------ tccpp.c ------------ */
1211 ST_DATA struct BufferedFile *file;
1212 ST_DATA int ch, tok;
1213 ST_DATA CValue tokc;
1214 ST_DATA const int *macro_ptr;
1215 ST_DATA int parse_flags;
1216 ST_DATA int tok_flags;
1217 ST_DATA CString tokcstr; /* current parsed string, if any */
1219 /* display benchmark infos */
1220 ST_DATA int total_lines;
1221 ST_DATA int total_bytes;
1222 ST_DATA int tok_ident;
1223 ST_DATA TokenSym **table_ident;
1225 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
1226 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
1227 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
1228 #define TOK_FLAG_EOF 0x0008 /* end of file */
1230 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
1231 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
1232 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
1233 token. line feed is also
1234 returned at eof */
1235 #define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */
1236 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1237 #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
1238 #define PARSE_FLAG_TOK_STR 0x0040 /* return parsed strings instead of TOK_PPSTR */
1240 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1241 ST_FUNC const char *get_tok_str(int v, CValue *cv);
1242 ST_FUNC void begin_macro(TokenString *str, int alloc);
1243 ST_FUNC void end_macro(void);
1244 ST_FUNC void save_parse_state(ParseState *s);
1245 ST_FUNC void restore_parse_state(ParseState *s);
1246 ST_INLN void tok_str_new(TokenString *s);
1247 ST_FUNC void tok_str_free(int *str);
1248 ST_FUNC void tok_str_add(TokenString *s, int t);
1249 ST_FUNC void tok_str_add_tok(TokenString *s);
1250 ST_INLN void define_push(int v, int macro_type, TokenString *str, Sym *first_arg);
1251 ST_FUNC void define_undef(Sym *s);
1252 ST_INLN Sym *define_find(int v);
1253 ST_FUNC void free_defines(Sym *b);
1254 ST_FUNC void print_defines(void);
1255 ST_FUNC Sym *label_find(int v);
1256 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
1257 ST_FUNC void label_pop(Sym **ptop, Sym *slast);
1258 ST_FUNC void parse_define(void);
1259 ST_FUNC void preprocess(int is_bof);
1260 ST_FUNC void next_nomacro(void);
1261 ST_FUNC void next(void);
1262 ST_INLN void unget_tok(int last_tok);
1263 ST_FUNC void preprocess_init(TCCState *s1);
1264 ST_FUNC void preprocess_new(void);
1265 ST_FUNC void preprocess_delete(void);
1266 ST_FUNC int tcc_preprocess(TCCState *s1);
1267 ST_FUNC void skip(int c);
1268 ST_FUNC NORETURN void expect(const char *msg);
1269 ST_FUNC char *trimfront(char *p);
1270 ST_FUNC char *trimback(char *a, char *e);
1272 /* ------------ tccgen.c ------------ */
1274 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
1275 ST_DATA Section *cur_text_section; /* current section where function code is generated */
1276 #ifdef CONFIG_TCC_ASM
1277 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
1278 #endif
1279 #ifdef CONFIG_TCC_BCHECK
1280 /* bound check related sections */
1281 ST_DATA Section *bounds_section; /* contains global data bound description */
1282 ST_DATA Section *lbounds_section; /* contains local data bound description */
1283 #endif
1284 /* symbol sections */
1285 ST_DATA Section *symtab_section, *strtab_section;
1286 /* debug sections */
1287 ST_DATA Section *stab_section, *stabstr_section;
1289 #define SYM_POOL_NB (8192 / sizeof(Sym))
1290 ST_DATA Sym *sym_free_first;
1291 ST_DATA void **sym_pools;
1292 ST_DATA int nb_sym_pools;
1294 ST_DATA Sym *global_stack;
1295 ST_DATA Sym *local_stack;
1296 ST_DATA Sym *local_label_stack;
1297 ST_DATA Sym *global_label_stack;
1298 ST_DATA Sym *define_stack;
1299 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
1300 ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop, *pvtop;
1301 #define vstack (__vstack + 1)
1302 ST_DATA int rsym, anon_sym, ind, loc;
1304 ST_DATA int const_wanted; /* true if constant wanted */
1305 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
1306 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
1307 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
1308 ST_DATA int func_var; /* true if current function is variadic */
1309 ST_DATA int func_vc;
1310 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
1311 ST_DATA const char *funcname;
1313 ST_FUNC void check_vstack(void);
1314 ST_INLN int is_float(int t);
1315 ST_FUNC int ieee_finite(double d);
1316 ST_FUNC void test_lvalue(void);
1317 ST_FUNC void swap(int *p, int *q);
1318 ST_FUNC void vpushi(int v);
1319 ST_FUNC Sym *external_global_sym(int v, CType *type, int r);
1320 ST_FUNC void vset(CType *type, int r, int v);
1321 ST_FUNC void vswap(void);
1322 ST_FUNC void vpush_global_sym(CType *type, int v);
1323 ST_FUNC void vrote(SValue *e, int n);
1324 ST_FUNC void vrott(int n);
1325 ST_FUNC void vrotb(int n);
1326 #ifdef TCC_TARGET_ARM
1327 ST_FUNC int get_reg_ex(int rc, int rc2);
1328 ST_FUNC void lexpand_nr(void);
1329 #endif
1330 ST_FUNC void vpushv(SValue *v);
1331 ST_FUNC void save_reg(int r);
1332 ST_FUNC int get_reg(int rc);
1333 ST_FUNC void save_regs(int n);
1334 ST_FUNC void gaddrof(void);
1335 ST_FUNC int gv(int rc);
1336 ST_FUNC void gv2(int rc1, int rc2);
1337 ST_FUNC void vpop(void);
1338 ST_FUNC void gen_op(int op);
1339 ST_FUNC int type_size(CType *type, int *a);
1340 ST_FUNC void mk_pointer(CType *type);
1341 ST_FUNC void vstore(void);
1342 ST_FUNC void inc(int post, int c);
1343 ST_FUNC void parse_asm_str(CString *astr);
1344 ST_FUNC int lvalue_type(int t);
1345 ST_FUNC void indir(void);
1346 ST_FUNC void unary(void);
1347 ST_FUNC void expr_prod(void);
1348 ST_FUNC void expr_sum(void);
1349 ST_FUNC void gexpr(void);
1350 ST_FUNC int expr_const(void);
1351 ST_FUNC void gen_inline_functions(void);
1352 ST_FUNC void decl(int l);
1353 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1354 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
1355 #endif
1356 #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
1357 ST_FUNC int classify_x86_64_va_arg(CType *ty);
1358 #endif
1360 /* ------------ tccelf.c ------------ */
1362 #define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
1363 #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
1364 #define TCC_OUTPUT_FORMAT_COFF 2 /* COFF */
1366 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1368 typedef struct {
1369 unsigned int n_strx; /* index into string table of name */
1370 unsigned char n_type; /* type of symbol */
1371 unsigned char n_other; /* misc info (usually empty) */
1372 unsigned short n_desc; /* description field */
1373 unsigned int n_value; /* value of symbol */
1374 } Stab_Sym;
1376 ST_FUNC Section *new_symtab(TCCState *s1, const char *symtab_name, int sh_type, int sh_flags, const char *strtab_name, const char *hash_name, int hash_sh_flags);
1378 ST_FUNC int put_elf_str(Section *s, const char *sym);
1379 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1380 ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int sh_num, const char *name);
1381 ST_FUNC int find_elf_sym(Section *s, const char *name);
1382 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1383 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);
1385 ST_FUNC void put_stabs(const char *str, int type, int other, int desc, unsigned long value);
1386 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
1387 ST_FUNC void put_stabn(int type, int other, int desc, int value);
1388 ST_FUNC void put_stabd(int type, int other, int desc);
1390 ST_FUNC void relocate_common_syms(void);
1391 ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);
1392 ST_FUNC void relocate_section(TCCState *s1, Section *s);
1393 ST_FUNC void relocate_plt(TCCState *s1);
1395 ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
1396 ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1397 ST_FUNC int tcc_load_archive(TCCState *s1, int fd);
1398 ST_FUNC void tcc_add_bcheck(TCCState *s1);
1400 ST_FUNC void build_got_entries(TCCState *s1);
1401 ST_FUNC void tcc_add_runtime(TCCState *s1);
1403 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
1404 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
1405 ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
1406 #endif
1408 #ifndef TCC_TARGET_PE
1409 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1410 ST_FUNC int tcc_load_ldscript(TCCState *s1);
1411 ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip);
1412 ST_FUNC void minp(void);
1413 ST_INLN void inp(void);
1414 ST_FUNC int handle_eob(void);
1415 #endif
1417 /* ------------ xxx-gen.c ------------ */
1419 ST_DATA const int reg_classes[NB_REGS];
1421 ST_FUNC void gsym_addr(int t, int a);
1422 ST_FUNC void gsym(int t);
1423 ST_FUNC void load(int r, SValue *sv);
1424 ST_FUNC void store(int r, SValue *v);
1425 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align, int *regsize);
1426 ST_FUNC void gfunc_call(int nb_args);
1427 ST_FUNC void gfunc_prolog(CType *func_type);
1428 ST_FUNC void gfunc_epilog(void);
1429 ST_FUNC int gjmp(int t);
1430 ST_FUNC void gjmp_addr(int a);
1431 ST_FUNC int gtst(int inv, int t);
1432 ST_FUNC void gen_opi(int op);
1433 ST_FUNC void gen_opf(int op);
1434 ST_FUNC void gen_cvt_ftoi(int t);
1435 ST_FUNC void gen_cvt_ftof(int t);
1436 ST_FUNC void ggoto(void);
1437 #ifndef TCC_TARGET_C67
1438 ST_FUNC void o(unsigned int c);
1439 #endif
1440 #ifndef TCC_TARGET_ARM
1441 ST_FUNC void gen_cvt_itof(int t);
1442 #endif
1443 ST_FUNC void gen_vla_sp_save(int addr);
1444 ST_FUNC void gen_vla_sp_restore(int addr);
1445 ST_FUNC void gen_vla_alloc(CType *type, int align);
1447 /* ------------ i386-gen.c ------------ */
1448 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1449 ST_FUNC void g(int c);
1450 ST_FUNC int oad(int c, int s);
1451 ST_FUNC void gen_le16(int c);
1452 ST_FUNC void gen_le32(int c);
1453 ST_FUNC void gen_addr32(int r, Sym *sym, int c);
1454 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
1455 #endif
1457 #ifdef CONFIG_TCC_BCHECK
1458 ST_FUNC void gen_bounded_ptr_add(void);
1459 ST_FUNC void gen_bounded_ptr_deref(void);
1460 #endif
1462 /* ------------ x86_64-gen.c ------------ */
1463 #ifdef TCC_TARGET_X86_64
1464 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
1465 ST_FUNC void gen_opl(int op);
1466 #endif
1468 /* ------------ arm-gen.c ------------ */
1469 #ifdef TCC_TARGET_ARM
1470 #if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP)
1471 ST_FUNC char *default_elfinterp(struct TCCState *s);
1472 #endif
1473 ST_FUNC void arm_init(struct TCCState *s);
1474 ST_FUNC uint32_t encbranch(int pos, int addr, int fail);
1475 ST_FUNC void gen_cvt_itof1(int t);
1476 #endif
1478 /* ------------ arm64-gen.c ------------ */
1479 #ifdef TCC_TARGET_ARM64
1480 ST_FUNC void gen_cvt_sxtw(void);
1481 ST_FUNC void gen_opl(int op);
1482 ST_FUNC void greturn(void);
1483 ST_FUNC void gen_va_start(void);
1484 ST_FUNC void gen_va_arg(CType *t);
1485 ST_FUNC void gen_clear_cache(void);
1486 #endif
1488 /* ------------ c67-gen.c ------------ */
1489 #ifdef TCC_TARGET_C67
1490 #endif
1492 /* ------------ tcccoff.c ------------ */
1494 #ifdef TCC_TARGET_COFF
1495 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
1496 ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
1497 #endif
1499 /* ------------ tccasm.c ------------ */
1500 ST_FUNC void asm_instr(void);
1501 ST_FUNC void asm_global_instr(void);
1502 #ifdef CONFIG_TCC_ASM
1503 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
1504 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
1505 ST_FUNC int asm_int_expr(TCCState *s1);
1506 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1507 /* ------------ i386-asm.c ------------ */
1508 ST_FUNC void gen_expr32(ExprValue *pe);
1509 ST_FUNC void asm_opcode(TCCState *s1, int opcode);
1510 ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
1511 ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
1512 ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
1513 ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
1514 #endif
1516 /* ------------ tccpe.c -------------- */
1517 #ifdef TCC_TARGET_PE
1518 ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
1519 ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
1520 ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
1521 ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
1522 #ifdef TCC_TARGET_X86_64
1523 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
1524 #endif
1525 /* symbol properties stored in Elf32_Sym->st_other */
1526 # define ST_PE_EXPORT 0x10
1527 # define ST_PE_IMPORT 0x20
1528 # define ST_PE_STDCALL 0x40
1529 #endif
1531 /* ------------ tccrun.c ----------------- */
1532 #ifdef TCC_IS_NATIVE
1533 #ifdef CONFIG_TCC_STATIC
1534 #define RTLD_LAZY 0x001
1535 #define RTLD_NOW 0x002
1536 #define RTLD_GLOBAL 0x100
1537 #define RTLD_DEFAULT NULL
1538 /* dummy function for profiling */
1539 ST_FUNC void *dlopen(const char *filename, int flag);
1540 ST_FUNC void dlclose(void *p);
1541 ST_FUNC const char *dlerror(void);
1542 ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1543 #elif !defined _WIN32
1544 ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1545 #endif
1547 #ifdef CONFIG_TCC_BACKTRACE
1548 ST_DATA int rt_num_callers;
1549 ST_DATA const char **rt_bound_error_msg;
1550 ST_DATA void *rt_prog_main;
1551 ST_FUNC void tcc_set_num_callers(int n);
1552 #endif
1553 #endif
1555 /********************************************************/
1556 #undef ST_DATA
1557 #ifdef ONE_SOURCE
1558 #define ST_DATA static
1559 #else
1560 #define ST_DATA
1561 #endif
1562 /********************************************************/
1563 #endif /* _TCC_H */