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