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