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