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