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