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