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