Don't enable bound check if libgcc is used
[tinycc.git] / tcc.h
blob50642b7815d34e32ae915523a1acc05be5e01aa8
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 #ifdef CONFIG_TCCBOOT
28 #include "tccboot.h"
29 #define CONFIG_TCC_STATIC
30 #else
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <math.h>
38 #include <signal.h>
39 #include <fcntl.h>
40 #include <setjmp.h>
41 #include <time.h>
43 #ifdef CONFIG_TCCASSERT
44 #include <assert.h>
45 #define TCC_ASSERT(ex) assert(ex)
46 #else
47 #define TCC_ASSERT(ex)
48 #endif
50 #ifndef _WIN32
51 # include <unistd.h>
52 # include <sys/time.h>
53 # include <sys/ucontext.h>
54 # include <sys/mman.h>
55 # ifndef CONFIG_TCC_STATIC
56 # include <dlfcn.h>
57 # endif
58 #else
59 # include <windows.h>
60 # include <sys/timeb.h>
61 # include <io.h> /* open, close etc. */
62 # include <direct.h> /* getcwd */
63 # ifdef __GNUC__
64 # include <stdint.h>
65 # else
66 typedef UINT_PTR uintptr_t;
67 # endif
68 # define inline __inline
69 # define inp next_inp
70 # ifdef LIBTCC_AS_DLL
71 # define LIBTCCAPI __declspec(dllexport)
72 # define PUB_FUNC LIBTCCAPI
73 # endif
74 #endif
76 #endif /* !CONFIG_TCCBOOT */
78 #ifndef O_BINARY
79 # define O_BINARY 0
80 #endif
82 #include "elf.h"
83 #ifdef TCC_TARGET_X86_64
84 # define ELFCLASSW ELFCLASS64
85 # define ElfW(type) Elf##64##_##type
86 # define ELFW(type) ELF##64##_##type
87 # define ElfW_Rel ElfW(Rela)
88 # define SHT_RELX SHT_RELA
89 # define REL_SECTION_FMT ".rela%s"
90 /* XXX: DLL with PLT would only work with x86-64 for now */
91 # define TCC_OUTPUT_DLL_WITH_PLT
92 #else
93 # define ELFCLASSW ELFCLASS32
94 # define ElfW(type) Elf##32##_##type
95 # define ELFW(type) ELF##32##_##type
96 # define ElfW_Rel ElfW(Rel)
97 # define SHT_RELX SHT_REL
98 # define REL_SECTION_FMT ".rel%s"
99 #endif
101 /* target address type */
102 #define addr_t ElfW(Addr)
104 #include "stab.h"
105 #include "libtcc.h"
107 /* parser debug */
108 /* #define PARSE_DEBUG */
109 /* preprocessor debug */
110 /* #define PP_DEBUG */
111 /* include file debug */
112 /* #define INC_DEBUG */
113 /* memory leak debug */
114 /* #define MEM_DEBUG */
115 /* assembler debug */
116 /* #define ASM_DEBUG */
118 /* target selection */
119 /* #define TCC_TARGET_I386 *//* i386 code generator */
120 /* #define TCC_TARGET_ARM *//* ARMv4 code generator */
121 /* #define TCC_TARGET_C67 *//* TMS320C67xx code generator */
122 /* #define TCC_TARGET_X86_64 *//* x86-64 code generator */
124 /* default target is I386 */
125 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
126 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
127 #define TCC_TARGET_I386
128 #endif
130 #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
131 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64) && \
132 !defined(CONFIG_USE_LIBGCC)
133 #define CONFIG_TCC_BCHECK /* enable bound checking code */
134 #endif
136 /* define it to include assembler support */
137 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
138 #define CONFIG_TCC_ASM
139 #endif
141 /* object format selection */
142 #if defined(TCC_TARGET_C67)
143 #define TCC_TARGET_COFF
144 #endif
146 /* only native compiler supports -run */
147 #if defined _WIN32 == defined TCC_TARGET_PE
148 # if (defined __i386__ || defined _X86_) && defined TCC_TARGET_I386
149 # define TCC_IS_NATIVE
150 # elif (defined __x86_64__ || defined _AMD64_) && defined TCC_TARGET_X86_64
151 # define TCC_IS_NATIVE
152 # elif defined __arm__ && defined TCC_TARGET_ARM
153 # define TCC_IS_NATIVE
154 # endif
155 #endif
157 #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
158 # define CONFIG_TCC_BACKTRACE
159 #endif
161 /* ------------ path configuration ------------ */
163 #ifndef CONFIG_SYSROOT
164 # define CONFIG_SYSROOT ""
165 #endif
166 #ifndef CONFIG_TCCDIR
167 # define CONFIG_TCCDIR "."
168 #endif
169 #ifndef CONFIG_LDDIR
170 # define CONFIG_LDDIR "lib"
171 #endif
172 #ifndef CONFIG_MULTIARCHDIR
173 #define CONFIG_MULTIARCHDIR
174 #endif
176 /* path to find crt1.o, crti.o and crtn.o */
177 #ifndef CONFIG_TCC_CRTPREFIX
178 # define CONFIG_TCC_CRTPREFIX CONFIG_SYSROOT "/usr/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR
179 #endif
181 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
183 /* system include paths */
184 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
185 # ifdef TCC_TARGET_PE
186 # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi"
187 # else
188 # define CONFIG_TCC_SYSINCLUDEPATHS \
189 CONFIG_SYSROOT "/usr/local/include/" CONFIG_MULTIARCHDIR \
190 ":" CONFIG_SYSROOT "/usr/local/include" \
191 ":" CONFIG_SYSROOT "/usr/include/" CONFIG_MULTIARCHDIR \
192 ":" CONFIG_SYSROOT "/usr/include" \
193 ":" "{B}/include"
194 # endif
195 #endif
197 /* library search paths */
198 #ifndef CONFIG_TCC_LIBPATHS
199 # ifdef TCC_TARGET_PE
200 # define CONFIG_TCC_LIBPATHS "{B}/lib;{B}"
201 # else
202 # define CONFIG_TCC_LIBPATHS \
203 CONFIG_SYSROOT "/usr/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR \
204 ":" CONFIG_SYSROOT "/usr/" CONFIG_LDDIR \
205 ":" CONFIG_SYSROOT "/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR \
206 ":" CONFIG_SYSROOT "/" CONFIG_LDDIR \
207 ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR \
208 ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR
209 # endif
210 #endif
212 /* name of ELF interpreter */
213 #ifndef CONFIG_TCC_ELFINTERP
214 # if defined __FreeBSD__
215 # define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
216 # elif defined __FreeBSD_kernel__
217 # if defined(TCC_TARGET_X86_64)
218 # define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
219 # else
220 # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
221 # endif
222 # elif defined __GNU__
223 # define CONFIG_TCC_ELFINTERP "/lib/ld.so"
224 # elif defined TCC_ARM_HARDFLOAT
225 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-armhf.so.3"
226 # elif defined TCC_ARM_EABI
227 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.3"
228 # elif defined(TCC_TARGET_X86_64)
229 # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
230 # elif defined(TCC_UCLIBC)
231 # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0"
232 # elif defined(TCC_TARGET_PE)
233 # define CONFIG_TCC_ELFINTERP "-"
234 # else
235 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
236 # endif
237 #endif
239 /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
240 #define TCC_LIBGCC CONFIG_SYSROOT "/" CONFIG_LDDIR "/" CONFIG_MULTIARCHDIR "/libgcc_s.so.1"
242 /* -------------------------------------------- */
243 /* include the target specific definitions */
245 #define TARGET_DEFS_ONLY
246 #ifdef TCC_TARGET_I386
247 # include "i386-gen.c"
248 #endif
249 #ifdef TCC_TARGET_X86_64
250 # include "x86_64-gen.c"
251 #endif
252 #ifdef TCC_TARGET_ARM
253 # include "arm-gen.c"
254 #endif
255 #ifdef TCC_TARGET_C67
256 # include "coff.h"
257 # include "c67-gen.c"
258 #endif
259 #undef TARGET_DEFS_ONLY
261 /* -------------------------------------------- */
263 #define INCLUDE_STACK_SIZE 32
264 #define IFDEF_STACK_SIZE 64
265 #define VSTACK_SIZE 256
266 #define STRING_MAX_SIZE 1024
267 #define PACK_STACK_SIZE 8
269 #define TOK_HASH_SIZE 8192 /* must be a power of two */
270 #define TOK_ALLOC_INCR 512 /* must be a power of two */
271 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
273 /* token symbol management */
274 typedef struct TokenSym {
275 struct TokenSym *hash_next;
276 struct Sym *sym_define; /* direct pointer to define */
277 struct Sym *sym_label; /* direct pointer to label */
278 struct Sym *sym_struct; /* direct pointer to structure */
279 struct Sym *sym_identifier; /* direct pointer to identifier */
280 int tok; /* token number */
281 int len;
282 char str[1];
283 } TokenSym;
285 #ifdef TCC_TARGET_PE
286 typedef unsigned short nwchar_t;
287 #else
288 typedef int nwchar_t;
289 #endif
291 typedef struct CString {
292 int size; /* size in bytes */
293 void *data; /* either 'char *' or 'nwchar_t *' */
294 int size_allocated;
295 void *data_allocated; /* if non NULL, data has been malloced */
296 } CString;
298 /* type definition */
299 typedef struct CType {
300 int t;
301 struct Sym *ref;
302 } CType;
304 /* constant value */
305 typedef union CValue {
306 long double ld;
307 double d;
308 float f;
309 int i;
310 unsigned int ui;
311 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
312 long long ll;
313 unsigned long long ull;
314 struct CString *cstr;
315 void *ptr;
316 int tab[LDOUBLE_SIZE/4];
317 } CValue;
319 /* value on stack */
320 typedef struct SValue {
321 CType type; /* type */
322 unsigned short r; /* register + flags */
323 unsigned short r2; /* second register, used for 'long long'
324 type. If not used, set to VT_CONST */
325 CValue c; /* constant, if VT_CONST */
326 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
327 } SValue;
329 /* symbol management */
330 typedef struct Sym {
331 int v; /* symbol token */
332 char *asm_label; /* associated asm label */
333 long r; /* associated register */
334 union {
335 long c; /* associated number */
336 int *d; /* define token stream */
338 CType type; /* associated type */
339 union {
340 struct Sym *next; /* next related symbol */
341 long jnext; /* next jump label */
343 struct Sym *prev; /* prev symbol in stack */
344 struct Sym *prev_tok; /* previous symbol for this token */
345 } Sym;
347 /* section definition */
348 /* XXX: use directly ELF structure for parameters ? */
349 /* special flag to indicate that the section should not be linked to
350 the other ones */
351 #define SHF_PRIVATE 0x80000000
353 /* special flag, too */
354 #define SECTION_ABS ((void *)1)
356 typedef struct Section {
357 unsigned long data_offset; /* current data offset */
358 unsigned char *data; /* section data */
359 unsigned long data_allocated; /* used for realloc() handling */
360 int sh_name; /* elf section name (only used during output) */
361 int sh_num; /* elf section number */
362 int sh_type; /* elf section type */
363 int sh_flags; /* elf section flags */
364 int sh_info; /* elf section info */
365 int sh_addralign; /* elf section alignment */
366 int sh_entsize; /* elf entry size */
367 unsigned long sh_size; /* section size (only used during output) */
368 addr_t sh_addr; /* address at which the section is relocated */
369 unsigned long sh_offset; /* file offset */
370 int nb_hashed_syms; /* used to resize the hash table */
371 struct Section *link; /* link to another section */
372 struct Section *reloc; /* corresponding section for relocation, if any */
373 struct Section *hash; /* hash table for symbols */
374 struct Section *next;
375 char name[1]; /* section name */
376 } Section;
378 typedef struct DLLReference {
379 int level;
380 void *handle;
381 char name[1];
382 } DLLReference;
384 /* GNUC attribute definition */
385 typedef struct AttributeDef {
386 unsigned
387 func_call : 3, /* calling convention (0..5), see below */
388 aligned : 5, /* alignement (0..16) */
389 packed : 1,
390 func_export : 1,
391 func_import : 1,
392 func_args : 5,
393 func_proto : 1,
394 mode : 4,
395 weak : 1,
396 fill : 10;
397 struct Section *section;
398 int alias_target; /* token */
399 } AttributeDef;
401 /* gr: wrappers for casting sym->r for other purposes */
402 #define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call)
403 #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
404 #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
405 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
406 #define FUNC_PROTO(r) (((AttributeDef*)&(r))->func_proto)
407 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
408 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
409 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
410 #define INT_ATTR(ad) (*(int*)(ad))
412 /* -------------------------------------------------- */
414 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
415 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
416 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
418 #define VLA_SP_LOC_SET 0x01 /* Location of SP on stack has been allocated */
419 #define VLA_SP_SAVED 0x02 /* SP has been saved to slot already */
420 #define VLA_NEED_NEW_FRAME 0x04 /* Needs new frame for next VLA */
421 #define VLA_IN_SCOPE 0x08 /* One or more VLAs are in scope */
422 #define VLA_SCOPE_FLAGS (VLA_SP_SAVED|VLA_NEED_NEW_FRAME|VLA_IN_SCOPE) /* Flags which are saved and restored upon entering and exiting a block */
424 /* stored in 'Sym.c' field */
425 #define FUNC_NEW 1 /* ansi function prototype */
426 #define FUNC_OLD 2 /* old function prototype */
427 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
429 /* stored in 'Sym.r' field */
430 #define FUNC_CDECL 0 /* standard c call */
431 #define FUNC_STDCALL 1 /* pascal c call */
432 #define FUNC_FASTCALL1 2 /* first param in %eax */
433 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
434 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
435 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
437 /* field 'Sym.t' for macros */
438 #define MACRO_OBJ 0 /* object like macro */
439 #define MACRO_FUNC 1 /* function like macro */
441 /* field 'Sym.r' for C labels */
442 #define LABEL_DEFINED 0 /* label is defined */
443 #define LABEL_FORWARD 1 /* label is forward defined */
444 #define LABEL_DECLARED 2 /* label is declared but never used */
446 /* type_decl() types */
447 #define TYPE_ABSTRACT 1 /* type without variable */
448 #define TYPE_DIRECT 2 /* type with variable */
450 #define IO_BUF_SIZE 8192
452 typedef struct BufferedFile {
453 uint8_t *buf_ptr;
454 uint8_t *buf_end;
455 int fd;
456 struct BufferedFile *prev;
457 int line_num; /* current line number - here to simplify code */
458 int ifndef_macro; /* #ifndef macro / #endif search */
459 int ifndef_macro_saved; /* saved ifndef_macro */
460 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
461 char filename[1024]; /* filename */
462 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
463 } BufferedFile;
465 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
466 #define CH_EOF (-1) /* end of file */
468 /* parsing state (used to save parser state to reparse part of the
469 source several times) */
470 typedef struct ParseState {
471 const int *macro_ptr;
472 int line_num;
473 int tok;
474 CValue tokc;
475 } ParseState;
477 /* used to record tokens */
478 typedef struct TokenString {
479 int *str;
480 int len;
481 int allocated_len;
482 int last_line_num;
483 } TokenString;
485 /* inline functions */
486 typedef struct InlineFunc {
487 int *token_str;
488 Sym *sym;
489 char filename[1];
490 } InlineFunc;
492 /* include file cache, used to find files faster and also to eliminate
493 inclusion if the include file is protected by #ifndef ... #endif */
494 typedef struct CachedInclude {
495 int ifndef_macro;
496 int hash_next; /* -1 if none */
497 char filename[1]; /* path specified in #include */
498 } CachedInclude;
500 #define CACHED_INCLUDES_HASH_SIZE 512
502 #ifdef CONFIG_TCC_ASM
503 typedef struct ExprValue {
504 uint32_t v;
505 Sym *sym;
506 } ExprValue;
508 #define MAX_ASM_OPERANDS 30
509 typedef struct ASMOperand {
510 int id; /* GCC 3 optionnal identifier (0 if number only supported */
511 char *constraint;
512 char asm_str[16]; /* computed asm string for operand */
513 SValue *vt; /* C value of the expression */
514 int ref_index; /* if >= 0, gives reference to a output constraint */
515 int input_index; /* if >= 0, gives reference to an input constraint */
516 int priority; /* priority, used to assign registers */
517 int reg; /* if >= 0, register number used for this operand */
518 int is_llong; /* true if double register value */
519 int is_memory; /* true if memory operand */
520 int is_rw; /* for '+' modifier */
521 } ASMOperand;
522 #endif
524 struct sym_attr {
525 unsigned long got_offset;
526 #ifdef TCC_TARGET_ARM
527 unsigned char plt_thumb_stub:1;
528 #endif
531 struct TCCState {
533 int verbose; /* if true, display some information during compilation */
534 int nostdinc; /* if true, no standard headers are added */
535 int nostdlib; /* if true, no standard libraries are added */
536 int nocommon; /* if true, do not use common symbols for .bss data */
537 int static_link; /* if true, static linking is performed */
538 int rdynamic; /* if true, all symbols are exported */
539 int symbolic; /* if true, resolve symbols in the current module first */
540 int alacarte_link; /* if true, only link in referenced objects from archive */
542 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
543 char *soname; /* as specified on the command line (-soname) */
544 char *rpath; /* as specified on the command line (-Wl,-rpath=) */
546 /* output type, see TCC_OUTPUT_XXX */
547 int output_type;
548 /* output format, see TCC_OUTPUT_FORMAT_xxx */
549 int output_format;
551 /* C language options */
552 int char_is_unsigned;
553 int leading_underscore;
555 /* warning switches */
556 int warn_write_strings;
557 int warn_unsupported;
558 int warn_error;
559 int warn_none;
560 int warn_implicit_function_declaration;
562 /* compile with debug symbol (and use them if error during execution) */
563 int do_debug;
564 #ifdef CONFIG_TCC_BCHECK
565 /* compile with built-in memory and bounds checker */
566 int do_bounds_check;
567 #endif
569 addr_t text_addr; /* address of text section */
570 int has_text_addr;
572 unsigned long section_align; /* section alignment */
574 char *init_symbol; /* symbols to call at load-time (not used currently) */
575 char *fini_symbol; /* symbols to call at unload-time (not used currently) */
577 #ifdef TCC_TARGET_I386
578 int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
579 #endif
581 /* array of all loaded dlls (including those referenced by loaded dlls) */
582 DLLReference **loaded_dlls;
583 int nb_loaded_dlls;
585 /* include paths */
586 char **include_paths;
587 int nb_include_paths;
589 char **sysinclude_paths;
590 int nb_sysinclude_paths;
592 /* library paths */
593 char **library_paths;
594 int nb_library_paths;
596 /* crt?.o object path */
597 char **crt_paths;
598 int nb_crt_paths;
600 /* error handling */
601 void *error_opaque;
602 void (*error_func)(void *opaque, const char *msg);
603 int error_set_jmp_enabled;
604 jmp_buf error_jmp_buf;
605 int nb_errors;
607 /* output file for preprocessing (-E) */
608 FILE *ppfp;
610 /* for -MD/-MF: collected dependencies for this compilation */
611 char **target_deps;
612 int nb_target_deps;
614 /* compilation */
615 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
616 BufferedFile **include_stack_ptr;
618 int ifdef_stack[IFDEF_STACK_SIZE];
619 int *ifdef_stack_ptr;
621 /* included files enclosed with #ifndef MACRO */
622 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
623 CachedInclude **cached_includes;
624 int nb_cached_includes;
626 /* #pragma pack stack */
627 int pack_stack[PACK_STACK_SIZE];
628 int *pack_stack_ptr;
630 /* inline functions are stored as token lists and compiled last
631 only if referenced */
632 struct InlineFunc **inline_fns;
633 int nb_inline_fns;
635 /* sections */
636 Section **sections;
637 int nb_sections; /* number of sections, including first dummy section */
639 Section **priv_sections;
640 int nb_priv_sections; /* number of private sections */
642 /* got & plt handling */
643 Section *got;
644 Section *plt;
645 struct sym_attr *sym_attrs;
646 int nb_sym_attrs;
647 /* give the correspondance from symtab indexes to dynsym indexes */
648 int *symtab_to_dynsym;
650 /* temporary dynamic symbol sections (for dll loading) */
651 Section *dynsymtab_section;
652 /* exported dynamic symbol section */
653 Section *dynsym;
654 /* copy of the gobal symtab_section variable */
655 Section *symtab;
656 /* tiny assembler state */
657 Sym *asm_labels;
659 #ifdef TCC_TARGET_PE
660 /* PE info */
661 int pe_subsystem;
662 unsigned pe_file_align;
663 unsigned pe_stack_size;
664 # ifdef TCC_TARGET_X86_64
665 Section *uw_pdata;
666 int uw_sym;
667 unsigned uw_offs;
668 # endif
669 #endif
671 #ifdef TCC_IS_NATIVE
672 const char *runtime_main;
673 /* for tcc_relocate */
674 void *runtime_mem;
675 # ifdef HAVE_SELINUX
676 void *write_mem;
677 unsigned long mem_size;
678 # endif
679 # if !defined TCC_TARGET_PE && (defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM)
680 /* write PLT and GOT here */
681 char *runtime_plt_and_got;
682 unsigned runtime_plt_and_got_offset;
683 # define TCC_HAS_RUNTIME_PLTGOT
684 # endif
685 #endif
687 /* used by main and tcc_parse_args only */
688 char **files; /* files seen on command line */
689 int nb_files; /* number thereof */
690 int nb_libraries; /* number of libs thereof */
691 char *outfile; /* output filename */
692 char *option_m; /* only -m32/-m64 handled */
693 int print_search_dirs; /* option */
694 int option_r; /* option -r */
695 int do_bench; /* option -bench */
696 int gen_deps; /* option -MD */
697 char *deps_outfile; /* option -MF */
700 /* The current value can be: */
701 #define VT_VALMASK 0x003f /* mask for value location, register or: */
702 #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
703 #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
704 #define VT_LOCAL 0x0032 /* offset on stack */
705 #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
706 #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
707 #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
708 #define VT_REF 0x0040 /* value is pointer to structure rather than address */
709 #define VT_LVAL 0x0100 /* var is an lvalue */
710 #define VT_SYM 0x0200 /* a symbol value is added */
711 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
712 char/short stored in integer registers) */
713 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
714 dereferencing value */
715 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
716 bounding function call point is in vc */
717 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
718 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
719 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
720 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
722 /* types */
723 #define VT_BTYPE 0x000f /* mask for basic type */
724 #define VT_INT 0 /* integer type */
725 #define VT_BYTE 1 /* signed byte type */
726 #define VT_SHORT 2 /* short type */
727 #define VT_VOID 3 /* void type */
728 #define VT_PTR 4 /* pointer */
729 #define VT_ENUM 5 /* enum definition */
730 #define VT_FUNC 6 /* function type */
731 #define VT_STRUCT 7 /* struct/union definition */
732 #define VT_FLOAT 8 /* IEEE float */
733 #define VT_DOUBLE 9 /* IEEE double */
734 #define VT_LDOUBLE 10 /* IEEE long double */
735 #define VT_BOOL 11 /* ISOC99 boolean type */
736 #define VT_LLONG 12 /* 64 bit integer */
737 #define VT_LONG 13 /* long integer (NEVER USED as type, only
738 during parsing) */
739 #define VT_QLONG 14 /* 128-bit integer. Only used for x86-64 ABI */
740 #define VT_QFLOAT 15 /* 128-bit float. Only used for x86-64 ABI */
741 #define VT_UNSIGNED 0x0010 /* unsigned type */
742 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
743 #define VT_BITFIELD 0x0040 /* bitfield modifier */
744 #define VT_CONSTANT 0x0800 /* const modifier */
745 #define VT_VOLATILE 0x1000 /* volatile modifier */
746 #define VT_SIGNED 0x2000 /* signed type */
747 #define VT_VLA 0x00020000 /* VLA type (also has VT_PTR and VT_ARRAY) */
749 /* storage */
750 #define VT_EXTERN 0x00000080 /* extern definition */
751 #define VT_STATIC 0x00000100 /* static variable */
752 #define VT_TYPEDEF 0x00000200 /* typedef definition */
753 #define VT_INLINE 0x00000400 /* inline definition */
754 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
755 #define VT_EXPORT 0x00008000 /* win32: data exported from dll */
756 #define VT_WEAK 0x00010000 /* weak symbol */
757 #define VT_TLS 0x00040000 /* thread-local storage */
759 #define VT_STRUCT_SHIFT 19 /* shift for bitfield shift values (max: 32 - 2*6) */
761 /* type mask (except storage) */
762 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK)
763 #define VT_TYPE (~(VT_STORAGE))
765 /* token values */
767 /* warning: the following compare tokens depend on i386 asm code */
768 #define TOK_ULT 0x92
769 #define TOK_UGE 0x93
770 #define TOK_EQ 0x94
771 #define TOK_NE 0x95
772 #define TOK_ULE 0x96
773 #define TOK_UGT 0x97
774 #define TOK_Nset 0x98
775 #define TOK_Nclear 0x99
776 #define TOK_LT 0x9c
777 #define TOK_GE 0x9d
778 #define TOK_LE 0x9e
779 #define TOK_GT 0x9f
781 #define TOK_LAND 0xa0
782 #define TOK_LOR 0xa1
784 #define TOK_DEC 0xa2
785 #define TOK_MID 0xa3 /* inc/dec, to void constant */
786 #define TOK_INC 0xa4
787 #define TOK_UDIV 0xb0 /* unsigned division */
788 #define TOK_UMOD 0xb1 /* unsigned modulo */
789 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
790 #define TOK_CINT 0xb3 /* number in tokc */
791 #define TOK_CCHAR 0xb4 /* char constant in tokc */
792 #define TOK_STR 0xb5 /* pointer to string in tokc */
793 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
794 #define TOK_LCHAR 0xb7
795 #define TOK_LSTR 0xb8
796 #define TOK_CFLOAT 0xb9 /* float constant */
797 #define TOK_LINENUM 0xba /* line number info */
798 #define TOK_CDOUBLE 0xc0 /* double constant */
799 #define TOK_CLDOUBLE 0xc1 /* long double constant */
800 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
801 #define TOK_ADDC1 0xc3 /* add with carry generation */
802 #define TOK_ADDC2 0xc4 /* add with carry use */
803 #define TOK_SUBC1 0xc5 /* add with carry generation */
804 #define TOK_SUBC2 0xc6 /* add with carry use */
805 #define TOK_CUINT 0xc8 /* unsigned int constant */
806 #define TOK_CLLONG 0xc9 /* long long constant */
807 #define TOK_CULLONG 0xca /* unsigned long long constant */
808 #define TOK_ARROW 0xcb
809 #define TOK_DOTS 0xcc /* three dots */
810 #define TOK_SHR 0xcd /* unsigned shift right */
811 #define TOK_PPNUM 0xce /* preprocessor number */
812 #define TOK_NOSUBST 0xcf /* means following token has already been pp'd */
814 #define TOK_SHL 0x01 /* shift left */
815 #define TOK_SAR 0x02 /* signed shift right */
817 /* assignement operators : normal operator or 0x80 */
818 #define TOK_A_MOD 0xa5
819 #define TOK_A_AND 0xa6
820 #define TOK_A_MUL 0xaa
821 #define TOK_A_ADD 0xab
822 #define TOK_A_SUB 0xad
823 #define TOK_A_DIV 0xaf
824 #define TOK_A_XOR 0xde
825 #define TOK_A_OR 0xfc
826 #define TOK_A_SHL 0x81
827 #define TOK_A_SAR 0x82
829 #ifndef offsetof
830 #define offsetof(type, field) ((size_t) &((type *)0)->field)
831 #endif
833 #ifndef countof
834 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
835 #endif
837 #define TOK_EOF (-1) /* end of file */
838 #define TOK_LINEFEED 10 /* line feed */
840 /* all identificators and strings have token above that */
841 #define TOK_IDENT 256
843 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
844 #define TOK_ASM_int TOK_INT
845 #define TOK_ASM_weak TOK_WEAK1
847 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
848 /* only used for i386 asm opcodes definitions */
849 #define DEF_BWL(x) \
850 DEF(TOK_ASM_ ## x ## b, #x "b") \
851 DEF(TOK_ASM_ ## x ## w, #x "w") \
852 DEF(TOK_ASM_ ## x ## l, #x "l") \
853 DEF(TOK_ASM_ ## x, #x)
854 #define DEF_WL(x) \
855 DEF(TOK_ASM_ ## x ## w, #x "w") \
856 DEF(TOK_ASM_ ## x ## l, #x "l") \
857 DEF(TOK_ASM_ ## x, #x)
858 #ifdef TCC_TARGET_X86_64
859 # define DEF_BWLQ(x) \
860 DEF(TOK_ASM_ ## x ## b, #x "b") \
861 DEF(TOK_ASM_ ## x ## w, #x "w") \
862 DEF(TOK_ASM_ ## x ## l, #x "l") \
863 DEF(TOK_ASM_ ## x ## q, #x "q") \
864 DEF(TOK_ASM_ ## x, #x)
865 # define DEF_WLQ(x) \
866 DEF(TOK_ASM_ ## x ## w, #x "w") \
867 DEF(TOK_ASM_ ## x ## l, #x "l") \
868 DEF(TOK_ASM_ ## x ## q, #x "q") \
869 DEF(TOK_ASM_ ## x, #x)
870 # define DEF_BWLX DEF_BWLQ
871 # define DEF_WLX DEF_WLQ
872 /* number of sizes + 1 */
873 # define NBWLX 5
874 #else
875 # define DEF_BWLX DEF_BWL
876 # define DEF_WLX DEF_WL
877 /* number of sizes + 1 */
878 # define NBWLX 4
879 #endif
881 #define DEF_FP1(x) \
882 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
883 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
884 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
885 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
887 #define DEF_FP(x) \
888 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
889 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
890 DEF_FP1(x)
892 #define DEF_ASMTEST(x) \
893 DEF_ASM(x ## o) \
894 DEF_ASM(x ## no) \
895 DEF_ASM(x ## b) \
896 DEF_ASM(x ## c) \
897 DEF_ASM(x ## nae) \
898 DEF_ASM(x ## nb) \
899 DEF_ASM(x ## nc) \
900 DEF_ASM(x ## ae) \
901 DEF_ASM(x ## e) \
902 DEF_ASM(x ## z) \
903 DEF_ASM(x ## ne) \
904 DEF_ASM(x ## nz) \
905 DEF_ASM(x ## be) \
906 DEF_ASM(x ## na) \
907 DEF_ASM(x ## nbe) \
908 DEF_ASM(x ## a) \
909 DEF_ASM(x ## s) \
910 DEF_ASM(x ## ns) \
911 DEF_ASM(x ## p) \
912 DEF_ASM(x ## pe) \
913 DEF_ASM(x ## np) \
914 DEF_ASM(x ## po) \
915 DEF_ASM(x ## l) \
916 DEF_ASM(x ## nge) \
917 DEF_ASM(x ## nl) \
918 DEF_ASM(x ## ge) \
919 DEF_ASM(x ## le) \
920 DEF_ASM(x ## ng) \
921 DEF_ASM(x ## nle) \
922 DEF_ASM(x ## g)
924 #endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
926 enum tcc_token {
927 TOK_LAST = TOK_IDENT - 1,
928 #define DEF(id, str) id,
929 #include "tcctok.h"
930 #undef DEF
933 #define TOK_UIDENT TOK_DEFINE
935 #ifdef _WIN32
936 #define snprintf _snprintf
937 #define vsnprintf _vsnprintf
938 #ifndef __GNUC__
939 # define strtold (long double)strtod
940 # define strtof (float)strtod
941 # define strtoll _strtoi64
942 # define strtoull _strtoui64
943 #endif
944 #else
945 /* XXX: need to define this to use them in non ISOC99 context */
946 extern float strtof (const char *__nptr, char **__endptr);
947 extern long double strtold (const char *__nptr, char **__endptr);
948 #endif
950 #ifdef _WIN32
951 #define IS_DIRSEP(c) (c == '/' || c == '\\')
952 #define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
953 #define PATHCMP stricmp
954 #else
955 #define IS_DIRSEP(c) (c == '/')
956 #define IS_ABSPATH(p) IS_DIRSEP(p[0])
957 #define PATHCMP strcmp
958 #endif
960 #ifdef TCC_TARGET_PE
961 #define PATHSEP ';'
962 #else
963 #define PATHSEP ':'
964 #endif
966 /* space exlcuding newline */
967 static inline int is_space(int ch)
969 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
972 static inline int isid(int c)
974 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
977 static inline int isnum(int c)
979 return c >= '0' && c <= '9';
982 static inline int isoct(int c)
984 return c >= '0' && c <= '7';
987 static inline int toup(int c)
989 return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
992 #ifndef PUB_FUNC
993 # define PUB_FUNC
994 #endif
996 #ifdef ONE_SOURCE
997 #define ST_INLN static inline
998 #define ST_FUNC static
999 #define ST_DATA static
1000 #else
1001 #define ST_INLN
1002 #define ST_FUNC
1003 #define ST_DATA extern
1004 #endif
1006 /* ------------ libtcc.c ------------ */
1008 /* use GNU C extensions */
1009 ST_DATA int gnu_ext;
1010 /* use Tiny C extensions */
1011 ST_DATA int tcc_ext;
1012 /* XXX: get rid of this ASAP */
1013 ST_DATA struct TCCState *tcc_state;
1015 #ifdef MEM_DEBUG
1016 ST_DATA int mem_cur_size;
1017 ST_DATA int mem_max_size;
1018 #endif
1020 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
1021 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
1022 #define AFF_PREPROCESS 0x0004 /* preprocess file */
1024 /* public functions currently used by the tcc main function */
1025 PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
1026 PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
1027 PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num);
1028 PUB_FUNC char *tcc_basename(const char *name);
1029 PUB_FUNC char *tcc_fileextension (const char *name);
1030 PUB_FUNC void tcc_free(void *ptr);
1031 PUB_FUNC void *tcc_malloc(unsigned long size);
1032 PUB_FUNC void *tcc_mallocz(unsigned long size);
1033 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
1034 PUB_FUNC char *tcc_strdup(const char *str);
1035 #define free(p) use_tcc_free(p)
1036 #define malloc(s) use_tcc_malloc(s)
1037 #define realloc(p, s) use_tcc_realloc(p, s)
1038 #undef strdup
1039 #define strdup(s) use_tcc_strdup(s)
1040 PUB_FUNC void tcc_memstats(void);
1041 PUB_FUNC void tcc_error_noabort(const char *fmt, ...);
1042 PUB_FUNC void tcc_error(const char *fmt, ...);
1043 PUB_FUNC void tcc_warning(const char *fmt, ...);
1045 /* other utilities */
1046 ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);
1047 ST_FUNC void dynarray_reset(void *pp, int *n);
1048 ST_FUNC void cstr_ccat(CString *cstr, int ch);
1049 ST_FUNC void cstr_cat(CString *cstr, const char *str);
1050 ST_FUNC void cstr_wccat(CString *cstr, int ch);
1051 ST_FUNC void cstr_new(CString *cstr);
1052 ST_FUNC void cstr_free(CString *cstr);
1053 ST_FUNC void cstr_reset(CString *cstr);
1055 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
1056 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
1057 ST_FUNC void *section_ptr_add(Section *sec, unsigned long size);
1058 ST_FUNC void section_reserve(Section *sec, unsigned long size);
1059 ST_FUNC Section *find_section(TCCState *s1, const char *name);
1061 ST_FUNC void put_extern_sym2(Sym *sym, Section *section, addr_t value, unsigned long size, int can_add_underscore);
1062 ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
1063 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
1065 ST_INLN void sym_free(Sym *sym);
1066 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c);
1067 ST_FUNC Sym *sym_find2(Sym *s, int v);
1068 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
1069 ST_FUNC void sym_pop(Sym **ptop, Sym *b);
1070 ST_INLN Sym *struct_find(int v);
1071 ST_INLN Sym *sym_find(int v);
1072 ST_FUNC Sym *global_identifier_push(int v, int t, int c);
1074 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
1075 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
1076 ST_FUNC void tcc_close(void);
1078 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
1079 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
1080 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
1082 PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time);
1083 PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv);
1085 PUB_FUNC void tcc_set_environment(TCCState *s);
1087 /* ------------ tccpp.c ------------ */
1089 ST_DATA struct BufferedFile *file;
1090 ST_DATA int ch, tok;
1091 ST_DATA CValue tokc;
1092 ST_DATA const int *macro_ptr;
1093 ST_DATA int parse_flags;
1094 ST_DATA int tok_flags;
1095 ST_DATA CString tokcstr; /* current parsed string, if any */
1097 /* display benchmark infos */
1098 ST_DATA int total_lines;
1099 ST_DATA int total_bytes;
1100 ST_DATA int tok_ident;
1101 ST_DATA TokenSym **table_ident;
1103 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
1104 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
1105 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
1106 #define TOK_FLAG_EOF 0x0008 /* end of file */
1108 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
1109 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
1110 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
1111 token. line feed is also
1112 returned at eof */
1113 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
1114 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1116 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1117 ST_FUNC char *get_tok_str(int v, CValue *cv);
1118 ST_FUNC void save_parse_state(ParseState *s);
1119 ST_FUNC void restore_parse_state(ParseState *s);
1120 ST_INLN void tok_str_new(TokenString *s);
1121 ST_FUNC void tok_str_free(int *str);
1122 ST_FUNC void tok_str_add(TokenString *s, int t);
1123 ST_FUNC void tok_str_add_tok(TokenString *s);
1124 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
1125 ST_FUNC void define_undef(Sym *s);
1126 ST_INLN Sym *define_find(int v);
1127 ST_FUNC void free_defines(Sym *b);
1128 ST_FUNC Sym *label_find(int v);
1129 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
1130 ST_FUNC void label_pop(Sym **ptop, Sym *slast);
1131 ST_FUNC void parse_define(void);
1132 ST_FUNC void preprocess(int is_bof);
1133 ST_FUNC void next_nomacro(void);
1134 ST_FUNC void next(void);
1135 ST_INLN void unget_tok(int last_tok);
1136 ST_FUNC void preprocess_init(TCCState *s1);
1137 ST_FUNC void preprocess_new(void);
1138 ST_FUNC int tcc_preprocess(TCCState *s1);
1139 ST_FUNC void skip(int c);
1140 ST_FUNC void expect(const char *msg);
1142 /* ------------ tccgen.c ------------ */
1144 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
1145 ST_DATA Section *tdata_section, *tbss_section; /* thread-local storage sections */
1146 ST_DATA Section *cur_text_section; /* current section where function code is generated */
1147 #ifdef CONFIG_TCC_ASM
1148 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
1149 #endif
1150 #ifdef CONFIG_TCC_BCHECK
1151 /* bound check related sections */
1152 ST_DATA Section *bounds_section; /* contains global data bound description */
1153 ST_DATA Section *lbounds_section; /* contains local data bound description */
1154 #endif
1155 /* symbol sections */
1156 ST_DATA Section *symtab_section, *strtab_section;
1157 /* debug sections */
1158 ST_DATA Section *stab_section, *stabstr_section;
1160 #define SYM_POOL_NB (8192 / sizeof(Sym))
1161 ST_DATA Sym *sym_free_first;
1162 ST_DATA void **sym_pools;
1163 ST_DATA int nb_sym_pools;
1165 ST_DATA Sym *global_stack;
1166 ST_DATA Sym *local_stack;
1167 ST_DATA Sym *local_label_stack;
1168 ST_DATA Sym *global_label_stack;
1169 ST_DATA Sym *define_stack;
1170 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
1171 ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop;
1172 #define vstack (__vstack + 1)
1173 ST_DATA int rsym, anon_sym, ind, loc;
1175 ST_DATA int const_wanted; /* true if constant wanted */
1176 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
1177 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
1178 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
1179 ST_DATA int func_vc;
1180 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
1181 ST_DATA char *funcname;
1183 ST_INLN int is_float(int t);
1184 ST_FUNC int ieee_finite(double d);
1185 ST_FUNC void test_lvalue(void);
1186 ST_FUNC void swap(int *p, int *q);
1187 ST_FUNC void vpushi(int v);
1188 ST_FUNC Sym *external_global_sym(int v, CType *type, int r);
1189 ST_FUNC void vset(CType *type, int r, int v);
1190 ST_FUNC void vswap(void);
1191 ST_FUNC void vpush_global_sym(CType *type, int v);
1192 ST_FUNC void vrote(SValue *e, int n);
1193 ST_FUNC void vrott(int n);
1194 ST_FUNC void vrotb(int n);
1195 #ifdef TCC_TARGET_ARM
1196 ST_FUNC int get_reg_ex(int rc, int rc2);
1197 ST_FUNC void lexpand_nr(void);
1198 #endif
1199 ST_FUNC void vpushv(SValue *v);
1200 ST_FUNC void save_reg(int r);
1201 ST_FUNC int get_reg(int rc);
1202 ST_FUNC void save_regs(int n);
1203 ST_FUNC int gv(int rc);
1204 ST_FUNC void gv2(int rc1, int rc2);
1205 ST_FUNC void vpop(void);
1206 ST_FUNC void gen_op(int op);
1207 ST_FUNC int type_size(CType *type, int *a);
1208 ST_FUNC void mk_pointer(CType *type);
1209 ST_FUNC void vstore(void);
1210 ST_FUNC void inc(int post, int c);
1211 ST_FUNC void parse_asm_str(CString *astr);
1212 ST_FUNC int lvalue_type(int t);
1213 ST_FUNC void indir(void);
1214 ST_FUNC void unary(void);
1215 ST_FUNC void expr_prod(void);
1216 ST_FUNC void expr_sum(void);
1217 ST_FUNC void gexpr(void);
1218 ST_FUNC int expr_const(void);
1219 ST_FUNC void gen_inline_functions(void);
1220 ST_FUNC void decl(int l);
1221 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1222 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
1223 #endif
1224 #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
1225 ST_FUNC int classify_x86_64_va_arg(CType *ty);
1226 #endif
1228 /* ------------ tccelf.c ------------ */
1230 #define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
1231 #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
1232 #define TCC_OUTPUT_FORMAT_COFF 2 /* COFF */
1234 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1236 typedef struct {
1237 unsigned int n_strx; /* index into string table of name */
1238 unsigned char n_type; /* type of symbol */
1239 unsigned char n_other; /* misc info (usually empty) */
1240 unsigned short n_desc; /* description field */
1241 unsigned int n_value; /* value of symbol */
1242 } Stab_Sym;
1244 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);
1246 ST_FUNC int put_elf_str(Section *s, const char *sym);
1247 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1248 ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int sh_num, const char *name);
1249 ST_FUNC int find_elf_sym(Section *s, const char *name);
1250 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1252 ST_FUNC void put_stabs(const char *str, int type, int other, int desc, unsigned long value);
1253 ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
1254 ST_FUNC void put_stabn(int type, int other, int desc, int value);
1255 ST_FUNC void put_stabd(int type, int other, int desc);
1257 ST_FUNC void relocate_common_syms(void);
1258 ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);
1259 ST_FUNC void relocate_section(TCCState *s1, Section *s);
1261 ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
1262 ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1263 ST_FUNC int tcc_load_archive(TCCState *s1, int fd);
1264 ST_FUNC void tcc_add_bcheck(TCCState *s1);
1266 ST_FUNC void build_got_entries(TCCState *s1);
1267 ST_FUNC void tcc_add_runtime(TCCState *s1);
1269 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
1270 #ifdef TCC_IS_NATIVE
1271 ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
1272 #endif
1274 #ifndef TCC_TARGET_PE
1275 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1276 ST_FUNC int tcc_load_ldscript(TCCState *s1);
1277 ST_FUNC uint8_t *parse_comment(uint8_t *p);
1278 ST_FUNC void minp(void);
1279 ST_INLN void inp(void);
1280 ST_FUNC int handle_eob(void);
1281 #endif
1283 /* ------------ xxx-gen.c ------------ */
1285 ST_DATA const int reg_classes[NB_REGS];
1287 ST_FUNC void gsym_addr(int t, int a);
1288 ST_FUNC void gsym(int t);
1289 ST_FUNC void load(int r, SValue *sv);
1290 ST_FUNC void store(int r, SValue *v);
1291 ST_FUNC int gfunc_sret(CType *vt, CType *ret, int *align);
1292 ST_FUNC void gfunc_call(int nb_args);
1293 ST_FUNC void gfunc_prolog(CType *func_type);
1294 ST_FUNC void gfunc_epilog(void);
1295 ST_FUNC int gjmp(int t);
1296 ST_FUNC void gjmp_addr(int a);
1297 ST_FUNC int gtst(int inv, int t);
1298 ST_FUNC void gen_opi(int op);
1299 ST_FUNC void gen_opf(int op);
1300 ST_FUNC void gen_cvt_ftoi(int t);
1301 ST_FUNC void gen_cvt_ftof(int t);
1302 ST_FUNC void ggoto(void);
1303 #ifndef TCC_TARGET_C67
1304 ST_FUNC void o(unsigned int c);
1305 #endif
1306 #ifndef TCC_TARGET_ARM
1307 ST_FUNC void gen_cvt_itof(int t);
1308 #endif
1309 ST_FUNC void gen_vla_sp_save(int addr);
1310 ST_FUNC void gen_vla_sp_restore(int addr);
1311 ST_FUNC void gen_vla_alloc(CType *type, int align);
1313 /* ------------ i386-gen.c ------------ */
1314 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1315 ST_FUNC void g(int c);
1316 ST_FUNC int oad(int c, int s);
1317 ST_FUNC void gen_le16(int c);
1318 ST_FUNC void gen_le32(int c);
1319 ST_FUNC void gen_addr32(int r, Sym *sym, int c);
1320 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
1321 #endif
1323 #ifdef CONFIG_TCC_BCHECK
1324 ST_FUNC void gen_bounded_ptr_add(void);
1325 ST_FUNC void gen_bounded_ptr_deref(void);
1326 #endif
1328 /* ------------ x86_64-gen.c ------------ */
1329 #ifdef TCC_TARGET_X86_64
1330 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
1331 ST_FUNC void gen_opl(int op);
1332 #endif
1334 /* ------------ arm-gen.c ------------ */
1335 #ifdef TCC_TARGET_ARM
1336 ST_FUNC void arm_init_types(void);
1337 ST_FUNC uint32_t encbranch(int pos, int addr, int fail);
1338 ST_FUNC void gen_cvt_itof1(int t);
1339 #endif
1341 /* ------------ c67-gen.c ------------ */
1342 #ifdef TCC_TARGET_C67
1343 #endif
1345 /* ------------ tcccoff.c ------------ */
1347 #ifdef TCC_TARGET_COFF
1348 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
1349 ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
1350 #endif
1352 /* ------------ tccasm.c ------------ */
1353 ST_FUNC void asm_instr(void);
1354 ST_FUNC void asm_global_instr(void);
1355 #ifdef CONFIG_TCC_ASM
1356 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
1357 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
1358 ST_FUNC int asm_int_expr(TCCState *s1);
1359 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1360 /* ------------ i386-asm.c ------------ */
1361 ST_FUNC void gen_expr32(ExprValue *pe);
1362 ST_FUNC void asm_opcode(TCCState *s1, int opcode);
1363 ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
1364 ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
1365 ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
1366 ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
1367 #endif
1369 /* ------------ tccpe.c -------------- */
1370 #ifdef TCC_TARGET_PE
1371 ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
1372 ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
1373 ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
1374 ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
1375 #ifdef TCC_TARGET_X86_64
1376 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
1377 #endif
1378 #endif
1380 /* ------------ tccrun.c ----------------- */
1381 #ifdef TCC_IS_NATIVE
1382 #ifdef CONFIG_TCC_STATIC
1383 #define RTLD_LAZY 0x001
1384 #define RTLD_NOW 0x002
1385 #define RTLD_GLOBAL 0x100
1386 #define RTLD_DEFAULT NULL
1387 /* dummy function for profiling */
1388 ST_FUNC void *dlopen(const char *filename, int flag);
1389 ST_FUNC void dlclose(void *p);
1390 ST_FUNC const char *dlerror(void);
1391 ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1392 #elif !defined _WIN32
1393 ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
1394 #endif
1396 #ifdef CONFIG_TCC_BACKTRACE
1397 ST_DATA int rt_num_callers;
1398 ST_DATA const char **rt_bound_error_msg;
1399 ST_DATA void *rt_prog_main;
1400 ST_FUNC void tcc_set_num_callers(int n);
1401 #endif
1402 #endif
1404 /********************************************************/
1405 #undef ST_DATA
1406 #ifdef ONE_SOURCE
1407 #define ST_DATA static
1408 #else
1409 #define ST_DATA
1410 #endif
1411 /********************************************************/
1412 #endif /* _TCC_H */