Merge branch 'mob' of git://repo.or.cz/tinycc into mypatch
[tinycc.git] / tcc.h
blob9013e87d5ac61db86dfdb117074396f7093f9e80
1 /*
2 * TCC - Tiny C Compiler
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 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <math.h>
33 #include <fcntl.h>
34 #include <setjmp.h>
35 #include <time.h>
37 #ifndef _WIN32
38 # include <unistd.h>
39 # include <sys/time.h>
40 # ifndef CONFIG_TCC_STATIC
41 # include <dlfcn.h>
42 # endif
43 /* XXX: need to define this to use them in non ISOC99 context */
44 extern float strtof (const char *__nptr, char **__endptr);
45 extern long double strtold (const char *__nptr, char **__endptr);
46 #endif
48 #ifdef _WIN32
49 # include <windows.h>
50 # include <io.h> /* open, close etc. */
51 # include <direct.h> /* getcwd */
52 # ifdef __GNUC__
53 # include <stdint.h>
54 # endif
55 # define inline __inline
56 # define snprintf _snprintf
57 # define vsnprintf _vsnprintf
58 # ifndef __GNUC__
59 # define strtold (long double)strtod
60 # define strtof (float)strtod
61 # define strtoll _strtoi64
62 # define strtoull _strtoui64
63 # endif
64 # ifdef LIBTCC_AS_DLL
65 # define LIBTCCAPI __declspec(dllexport)
66 # define PUB_FUNC LIBTCCAPI
67 # endif
68 # define inp next_inp /* inp is an intrinsic on msvc/mingw */
69 # ifdef _MSC_VER
70 # pragma warning (disable : 4244) // conversion from 'uint64_t' to 'int', possible loss of data
71 # pragma warning (disable : 4267) // conversion from 'size_t' to 'int', possible loss of data
72 # pragma warning (disable : 4996) // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
73 # pragma warning (disable : 4018) // signed/unsigned mismatch
74 # pragma warning (disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
75 # define ssize_t intptr_t
76 # ifdef _X86_
77 # define __i386__ 1
78 # endif
79 # ifdef _AMD64_
80 # define __x86_64__ 1
81 # endif
82 # endif
83 # undef CONFIG_TCC_STATIC
84 #endif
86 #ifndef O_BINARY
87 # define O_BINARY 0
88 #endif
90 #ifndef offsetof
91 #define offsetof(type, field) ((size_t) &((type *)0)->field)
92 #endif
94 #ifndef countof
95 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
96 #endif
98 #ifdef _MSC_VER
99 # define NORETURN __declspec(noreturn)
100 # define ALIGNED(x) __declspec(align(x))
101 #else
102 # define NORETURN __attribute__((noreturn))
103 # define ALIGNED(x) __attribute__((aligned(x)))
104 #endif
106 /* gnu headers use to #define __attribute__ to empty for non-gcc compilers */
107 #ifdef __TINYC__
108 # undef __attribute__
109 #endif
111 #ifdef _WIN32
112 # define IS_DIRSEP(c) (c == '/' || c == '\\')
113 # define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
114 # define PATHCMP stricmp
115 # define PATHSEP ";"
116 #else
117 # define IS_DIRSEP(c) (c == '/')
118 # define IS_ABSPATH(p) IS_DIRSEP(p[0])
119 # define PATHCMP strcmp
120 # define PATHSEP ":"
121 #endif
123 /* -------------------------------------------- */
125 /* parser debug */
126 /* #define PARSE_DEBUG */
127 /* preprocessor debug */
128 /* #define PP_DEBUG */
129 /* include file debug */
130 /* #define INC_DEBUG */
131 /* memory leak debug (only for single threaded usage) */
132 /* #define MEM_DEBUG */
133 /* assembler debug */
134 /* #define ASM_DEBUG */
136 /* target selection */
137 /* #define TCC_TARGET_I386 *//* i386 code generator */
138 /* #define TCC_TARGET_X86_64 *//* x86-64 code generator */
139 /* #define TCC_TARGET_ARM *//* ARMv4 code generator */
140 /* #define TCC_TARGET_ARM64 *//* ARMv8 code generator */
141 /* #define TCC_TARGET_C67 *//* TMS320C67xx code generator */
142 /* #define TCC_TARGET_RISCV64 *//* risc-v code generator */
144 /* default target is I386 */
145 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
146 !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
147 !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_RISCV64)
148 # if defined __x86_64__
149 # define TCC_TARGET_X86_64
150 # elif defined __arm__
151 # define TCC_TARGET_ARM
152 # define TCC_ARM_EABI
153 # define TCC_ARM_HARDFLOAT
154 # elif defined __aarch64__
155 # define TCC_TARGET_ARM64
156 # else
157 # define TCC_TARGET_I386
158 # endif
159 # ifdef _WIN32
160 # define TCC_TARGET_PE 1
161 # endif
162 #endif
164 /* only native compiler supports -run */
165 #if defined _WIN32 == defined TCC_TARGET_PE
166 # if defined __i386__ && defined TCC_TARGET_I386
167 # define TCC_IS_NATIVE
168 # elif defined __x86_64__ && defined TCC_TARGET_X86_64
169 # define TCC_IS_NATIVE
170 # elif defined __arm__ && defined TCC_TARGET_ARM
171 # define TCC_IS_NATIVE
172 # elif defined __aarch64__ && defined TCC_TARGET_ARM64
173 # define TCC_IS_NATIVE
174 # elif defined __riscv && defined __LP64__ && defined TCC_TARGET_RISCV64
175 # define TCC_IS_NATIVE
176 # endif
177 #endif
179 #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
180 # define CONFIG_TCC_BACKTRACE
181 # if (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64) \
182 && !defined TCC_UCLIBC && !defined TCC_MUSL
183 # define CONFIG_TCC_BCHECK /* enable bound checking code */
184 # endif
185 #endif
187 /* ------------ path configuration ------------ */
189 #ifndef CONFIG_SYSROOT
190 # define CONFIG_SYSROOT ""
191 #endif
192 #ifndef CONFIG_TCCDIR
193 # define CONFIG_TCCDIR "/usr/local/lib/tcc"
194 #endif
195 #ifndef CONFIG_LDDIR
196 # define CONFIG_LDDIR "lib"
197 #endif
198 #ifdef CONFIG_TRIPLET
199 # define USE_TRIPLET(s) s "/" CONFIG_TRIPLET
200 # define ALSO_TRIPLET(s) USE_TRIPLET(s) ":" s
201 #else
202 # define USE_TRIPLET(s) s
203 # define ALSO_TRIPLET(s) s
204 #endif
206 /* path to find crt1.o, crti.o and crtn.o */
207 #ifndef CONFIG_TCC_CRTPREFIX
208 # define CONFIG_TCC_CRTPREFIX USE_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
209 #endif
211 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
213 /* system include paths */
214 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
215 # ifdef TCC_TARGET_PE
216 # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include"PATHSEP"{B}/include/winapi"
217 # else
218 # define CONFIG_TCC_SYSINCLUDEPATHS \
219 "{B}/include" \
220 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/include") \
221 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/include")
222 # endif
223 #endif
225 /* library search paths */
226 #ifndef CONFIG_TCC_LIBPATHS
227 # ifdef TCC_TARGET_PE
228 # define CONFIG_TCC_LIBPATHS "{B}/lib"
229 # else
230 # define CONFIG_TCC_LIBPATHS \
231 ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
232 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
233 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
234 # endif
235 #endif
237 /* name of ELF interpreter */
238 #ifndef CONFIG_TCC_ELFINTERP
239 # if defined __FreeBSD__
240 # define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
241 # elif defined __FreeBSD_kernel__
242 # if defined(TCC_TARGET_X86_64)
243 # define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
244 # else
245 # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
246 # endif
247 # elif defined __DragonFly__
248 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2"
249 # elif defined __NetBSD__
250 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
251 # elif defined __GNU__
252 # define CONFIG_TCC_ELFINTERP "/lib/ld.so"
253 # elif defined(TCC_TARGET_PE)
254 # define CONFIG_TCC_ELFINTERP "-"
255 # elif defined(TCC_UCLIBC)
256 # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */
257 # elif defined TCC_TARGET_ARM64
258 # if defined(TCC_MUSL)
259 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-aarch64.so.1"
260 # else
261 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
262 # endif
263 # elif defined(TCC_TARGET_X86_64)
264 # if defined(TCC_MUSL)
265 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-x86_64.so.1"
266 # else
267 # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
268 # endif
269 # elif defined(TCC_TARGET_RISCV64)
270 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-riscv64-lp64d.so.1"
271 # elif !defined(TCC_ARM_EABI)
272 # if defined(TCC_MUSL)
273 # if defined(TCC_TARGET_I386)
274 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-i386.so.1"
275 # else
276 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-arm.so.1"
277 # endif
278 # else
279 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
280 # endif
281 # endif
282 #endif
284 /* var elf_interp dans *-gen.c */
285 #ifdef CONFIG_TCC_ELFINTERP
286 # define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP
287 #else
288 # define DEFAULT_ELFINTERP(s) default_elfinterp(s)
289 #endif
291 /* (target specific) libtcc1.a */
292 #ifndef TCC_LIBTCC1
293 # define TCC_LIBTCC1 "libtcc1.a"
294 #endif
295 #ifndef TCC_LIBBCHECK
296 # define TCC_LIBBCHECK "bcheck.o"
297 #endif
299 /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
300 #if defined CONFIG_USE_LIBGCC && !defined TCC_LIBGCC
301 #define TCC_LIBGCC USE_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
302 #endif
304 /* -------------------------------------------- */
306 #include "libtcc.h"
307 #include "elf.h"
308 #include "stab.h"
310 /* -------------------------------------------- */
312 #ifndef PUB_FUNC /* functions used by tcc.c but not in libtcc.h */
313 # define PUB_FUNC
314 #endif
316 #ifndef ONE_SOURCE
317 # define ONE_SOURCE 1
318 #endif
320 /* support using libtcc from threads */
321 #define CONFIG_TCC_SEMLOCK
323 #if ONE_SOURCE
324 #define ST_INLN static inline
325 #define ST_FUNC static
326 #define ST_DATA static
327 #else
328 #define ST_INLN
329 #define ST_FUNC
330 #define ST_DATA extern
331 #endif
333 #ifdef TCC_PROFILE /* profile all functions */
334 # define static
335 #endif
337 /* -------------------------------------------- */
338 /* include the target specific definitions */
340 #define TARGET_DEFS_ONLY
341 #ifdef TCC_TARGET_I386
342 # include "i386-gen.c"
343 # include "i386-link.c"
344 #elif defined TCC_TARGET_X86_64
345 # include "x86_64-gen.c"
346 # include "x86_64-link.c"
347 #elif defined TCC_TARGET_ARM
348 # include "arm-gen.c"
349 # include "arm-link.c"
350 # include "arm-asm.c"
351 #elif defined TCC_TARGET_ARM64
352 # include "arm64-gen.c"
353 # include "arm64-link.c"
354 #elif defined TCC_TARGET_C67
355 # define TCC_TARGET_COFF
356 # include "coff.h"
357 # include "c67-gen.c"
358 # include "c67-link.c"
359 #elif defined(TCC_TARGET_RISCV64)
360 # include "riscv64-gen.c"
361 # include "riscv64-link.c"
362 #else
363 #error unknown target
364 #endif
365 #undef TARGET_DEFS_ONLY
367 /* -------------------------------------------- */
369 #if PTR_SIZE == 8
370 # define ELFCLASSW ELFCLASS64
371 # define ElfW(type) Elf##64##_##type
372 # define ELFW(type) ELF##64##_##type
373 # define ElfW_Rel ElfW(Rela)
374 # define SHT_RELX SHT_RELA
375 # define REL_SECTION_FMT ".rela%s"
376 #else
377 # define ELFCLASSW ELFCLASS32
378 # define ElfW(type) Elf##32##_##type
379 # define ELFW(type) ELF##32##_##type
380 # define ElfW_Rel ElfW(Rel)
381 # define SHT_RELX SHT_REL
382 # define REL_SECTION_FMT ".rel%s"
383 #endif
384 /* target address type */
385 #define addr_t ElfW(Addr)
386 #define ElfSym ElfW(Sym)
388 #if PTR_SIZE == 8 && !defined TCC_TARGET_PE
389 # define LONG_SIZE 8
390 #else
391 # define LONG_SIZE 4
392 #endif
394 /* -------------------------------------------- */
396 #define INCLUDE_STACK_SIZE 32
397 #define IFDEF_STACK_SIZE 64
398 #define VSTACK_SIZE 256
399 #define STRING_MAX_SIZE 1024
400 #define TOKSTR_MAX_SIZE 256
401 #define PACK_STACK_SIZE 8
403 #define TOK_HASH_SIZE 16384 /* must be a power of two */
404 #define TOK_ALLOC_INCR 512 /* must be a power of two */
405 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
407 /* token symbol management */
408 typedef struct TokenSym {
409 struct TokenSym *hash_next;
410 struct Sym *sym_define; /* direct pointer to define */
411 struct Sym *sym_label; /* direct pointer to label */
412 struct Sym *sym_struct; /* direct pointer to structure */
413 struct Sym *sym_identifier; /* direct pointer to identifier */
414 int tok; /* token number */
415 int len;
416 char str[1];
417 } TokenSym;
419 #ifdef TCC_TARGET_PE
420 typedef unsigned short nwchar_t;
421 #else
422 typedef int nwchar_t;
423 #endif
425 typedef struct CString {
426 int size; /* size in bytes */
427 void *data; /* either 'char *' or 'nwchar_t *' */
428 int size_allocated;
429 } CString;
431 /* type definition */
432 typedef struct CType {
433 int t;
434 struct Sym *ref;
435 } CType;
437 /* constant value */
438 typedef union CValue {
439 long double ld;
440 double d;
441 float f;
442 uint64_t i;
443 struct {
444 int size;
445 const void *data;
446 } str;
447 int tab[LDOUBLE_SIZE/4];
448 } CValue;
450 /* value on stack */
451 typedef struct SValue {
452 CType type; /* type */
453 unsigned short r; /* register + flags */
454 unsigned short r2; /* second register, used for 'long long'
455 type. If not used, set to VT_CONST */
456 union {
457 struct { int jtrue, jfalse; }; /* forward jmps */
458 CValue c; /* constant, if VT_CONST */
460 union {
461 struct { unsigned short cmp_op, cmp_r; }; /* VT_CMP operation */
462 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST), or if */
463 }; /* result of unary() for an identifier. */
465 } SValue;
467 /* symbol attributes */
468 struct SymAttr {
469 unsigned short
470 aligned : 5, /* alignment as log2+1 (0 == unspecified) */
471 packed : 1,
472 weak : 1,
473 visibility : 2,
474 dllexport : 1,
475 nodecorate : 1,
476 dllimport : 1,
477 addrtaken : 1,
478 unused : 3;
481 /* function attributes or temporary attributes for parsing */
482 struct FuncAttr {
483 unsigned
484 func_call : 3, /* calling convention (0..5), see below */
485 func_type : 2, /* FUNC_OLD/NEW/ELLIPSIS */
486 func_noreturn : 1, /* attribute((noreturn)) */
487 xxxx : 2,
488 func_args : 8; /* PE __stdcall args */
491 /* symbol management */
492 typedef struct Sym {
493 int v; /* symbol token */
494 unsigned short r; /* associated register or VT_CONST/VT_LOCAL and LVAL type */
495 struct SymAttr a; /* symbol attributes */
496 union {
497 struct {
498 int c; /* associated number or Elf symbol index */
499 union {
500 int sym_scope; /* scope level for locals */
501 int jnext; /* next jump label */
502 struct FuncAttr f; /* function attributes */
503 int auxtype; /* bitfield access type */
506 long long enum_val; /* enum constant if IS_ENUM_VAL */
507 int *d; /* define token stream */
508 struct Sym *ncl; /* next cleanup */
510 CType type; /* associated type */
511 union {
512 struct Sym *next; /* next related symbol (for fields and anoms) */
513 struct Sym *cleanupstate; /* in defined labels */
514 int asm_label; /* associated asm label */
516 struct Sym *prev; /* prev symbol in stack */
517 struct Sym *prev_tok; /* previous symbol for this token */
518 } Sym;
520 /* section definition */
521 typedef struct Section {
522 unsigned long data_offset; /* current data offset */
523 unsigned char *data; /* section data */
524 unsigned long data_allocated; /* used for realloc() handling */
525 TCCState *s1;
526 int sh_name; /* elf section name (only used during output) */
527 int sh_num; /* elf section number */
528 int sh_type; /* elf section type */
529 int sh_flags; /* elf section flags */
530 int sh_info; /* elf section info */
531 int sh_addralign; /* elf section alignment */
532 int sh_entsize; /* elf entry size */
533 unsigned long sh_size; /* section size (only used during output) */
534 addr_t sh_addr; /* address at which the section is relocated */
535 unsigned long sh_offset; /* file offset */
536 int nb_hashed_syms; /* used to resize the hash table */
537 struct Section *link; /* link to another section */
538 struct Section *reloc; /* corresponding section for relocation, if any */
539 struct Section *hash; /* hash table for symbols */
540 struct Section *prev; /* previous section on section stack */
541 char name[1]; /* section name */
542 } Section;
544 typedef struct DLLReference {
545 int level;
546 void *handle;
547 char name[1];
548 } DLLReference;
550 /* -------------------------------------------------- */
552 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
553 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
554 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
556 /* stored in 'Sym->f.func_type' field */
557 #define FUNC_NEW 1 /* ansi function prototype */
558 #define FUNC_OLD 2 /* old function prototype */
559 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
561 /* stored in 'Sym->f.func_call' field */
562 #define FUNC_CDECL 0 /* standard c call */
563 #define FUNC_STDCALL 1 /* pascal c call */
564 #define FUNC_FASTCALL1 2 /* first param in %eax */
565 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
566 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
567 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
569 /* field 'Sym.t' for macros */
570 #define MACRO_OBJ 0 /* object like macro */
571 #define MACRO_FUNC 1 /* function like macro */
573 /* field 'Sym.r' for C labels */
574 #define LABEL_DEFINED 0 /* label is defined */
575 #define LABEL_FORWARD 1 /* label is forward defined */
576 #define LABEL_DECLARED 2 /* label is declared but never used */
578 /* type_decl() types */
579 #define TYPE_ABSTRACT 1 /* type without variable */
580 #define TYPE_DIRECT 2 /* type with variable */
582 #define IO_BUF_SIZE 8192
584 typedef struct BufferedFile {
585 uint8_t *buf_ptr;
586 uint8_t *buf_end;
587 int fd;
588 struct BufferedFile *prev;
589 int line_num; /* current line number - here to simplify code */
590 int line_ref; /* tcc -E: last printed line */
591 int ifndef_macro; /* #ifndef macro / #endif search */
592 int ifndef_macro_saved; /* saved ifndef_macro */
593 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
594 int include_next_index; /* next search path */
595 char filename[1024]; /* filename */
596 char *true_filename; /* filename not modified by # line directive */
597 unsigned char unget[4];
598 unsigned char buffer[1]; /* extra size for CH_EOB char */
599 } BufferedFile;
601 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
602 #define CH_EOF (-1) /* end of file */
604 /* used to record tokens */
605 typedef struct TokenString {
606 int *str;
607 int len;
608 int lastlen;
609 int allocated_len;
610 int last_line_num;
611 int save_line_num;
612 /* used to chain token-strings with begin/end_macro() */
613 struct TokenString *prev;
614 const int *prev_ptr;
615 char alloc;
616 } TokenString;
618 /* GNUC attribute definition */
619 typedef struct AttributeDef {
620 struct SymAttr a;
621 struct FuncAttr f;
622 unsigned short constructor:1;
623 unsigned short destructor:1;
624 struct Section *section;
625 Sym *cleanup_func;
626 int alias_target; /* token */
627 int asm_label; /* associated asm label */
628 char attr_mode; /* __attribute__((__mode__(...))) */
629 } AttributeDef;
631 /* inline functions */
632 typedef struct InlineFunc {
633 TokenString *func_str;
634 Sym *sym;
635 char filename[1];
636 } InlineFunc;
638 /* include file cache, used to find files faster and also to eliminate
639 inclusion if the include file is protected by #ifndef ... #endif */
640 typedef struct CachedInclude {
641 int ifndef_macro;
642 int once;
643 int hash_next; /* -1 if none */
644 char filename[1]; /* path specified in #include */
645 } CachedInclude;
647 #define CACHED_INCLUDES_HASH_SIZE 32
649 #ifdef CONFIG_TCC_ASM
650 typedef struct ExprValue {
651 uint64_t v;
652 Sym *sym;
653 int pcrel;
654 } ExprValue;
656 #define MAX_ASM_OPERANDS 30
657 typedef struct ASMOperand {
658 int id; /* GCC 3 optional identifier (0 if number only supported */
659 char *constraint;
660 char asm_str[16]; /* computed asm string for operand */
661 SValue *vt; /* C value of the expression */
662 int ref_index; /* if >= 0, gives reference to a output constraint */
663 int input_index; /* if >= 0, gives reference to an input constraint */
664 int priority; /* priority, used to assign registers */
665 int reg; /* if >= 0, register number used for this operand */
666 int is_llong; /* true if double register value */
667 int is_memory; /* true if memory operand */
668 int is_rw; /* for '+' modifier */
669 } ASMOperand;
670 #endif
672 /* extra symbol attributes (not in symbol table) */
673 struct sym_attr {
674 unsigned got_offset;
675 unsigned plt_offset;
676 int plt_sym;
677 int dyn_index;
678 #ifdef TCC_TARGET_ARM
679 unsigned char plt_thumb_stub:1;
680 #endif
683 struct TCCState {
684 unsigned char verbose; /* if true, display some information during compilation */
685 unsigned char nostdinc; /* if true, no standard headers are added */
686 unsigned char nostdlib; /* if true, no standard libraries are added */
687 unsigned char nocommon; /* if true, do not use common symbols for .bss data */
688 unsigned char static_link; /* if true, static linking is performed */
689 unsigned char rdynamic; /* if true, all symbols are exported */
690 unsigned char symbolic; /* if true, resolve symbols in the current module first */
691 unsigned char filetype; /* file type for compilation (NONE,C,ASM) */
692 unsigned int cversion; /* supported C ISO version, 199901 (the default), 201112, ... */
694 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
695 char *soname; /* as specified on the command line (-soname) */
696 char *rpath; /* as specified on the command line (-Wl,-rpath=) */
697 unsigned char enable_new_dtags; /* ditto, (-Wl,--enable-new-dtags) */
699 /* output type, see TCC_OUTPUT_XXX */
700 int output_type;
701 /* output format, see TCC_OUTPUT_FORMAT_xxx */
702 int output_format;
704 /* C language options */
705 unsigned char char_is_unsigned;
706 unsigned char leading_underscore;
707 unsigned char ms_extensions; /* allow nested named struct w/o identifier behave like unnamed */
708 unsigned char dollars_in_identifiers; /* allows '$' char in identifiers */
709 unsigned char ms_bitfields; /* if true, emulate MS algorithm for aligning bitfields */
711 /* warning switches */
712 unsigned char warn_write_strings;
713 unsigned char warn_unsupported;
714 unsigned char warn_error;
715 unsigned char warn_none;
716 unsigned char warn_implicit_function_declaration;
717 unsigned char warn_gcc_compat;
719 /* compile with debug symbol (and use them if error during execution) */
720 unsigned char do_debug;
721 #ifdef CONFIG_TCC_BCHECK
722 /* compile with built-in memory and bounds checker */
723 unsigned char do_bounds_check;
724 #endif
725 #ifdef TCC_TARGET_ARM
726 enum float_abi float_abi; /* float ABI of the generated code*/
727 #endif
728 int run_test; /* nth test to run with -dt -run */
730 addr_t text_addr; /* address of text section */
731 unsigned char has_text_addr;
733 unsigned section_align; /* section alignment */
735 /* use GNU C extensions */
736 unsigned char gnu_ext;
737 /* use TinyCC extensions */
738 unsigned char tcc_ext;
740 char *init_symbol; /* symbols to call at load-time (not used currently) */
741 char *fini_symbol; /* symbols to call at unload-time (not used currently) */
743 #ifdef TCC_TARGET_I386
744 int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
745 #endif
746 #ifdef TCC_TARGET_X86_64
747 unsigned char nosse; /* For -mno-sse support. */
748 #endif
750 /* array of all loaded dlls (including those referenced by loaded dlls) */
751 DLLReference **loaded_dlls;
752 int nb_loaded_dlls;
754 /* include paths */
755 char **include_paths;
756 int nb_include_paths;
758 char **sysinclude_paths;
759 int nb_sysinclude_paths;
761 /* library paths */
762 char **library_paths;
763 int nb_library_paths;
765 /* crt?.o object path */
766 char **crt_paths;
767 int nb_crt_paths;
769 /* -D / -U options */
770 CString cmdline_defs;
771 /* -include options */
772 CString cmdline_incl;
774 /* error handling */
775 void *error_opaque;
776 void (*error_func)(void *opaque, const char *msg);
777 int error_set_jmp_enabled;
778 jmp_buf error_jmp_buf;
779 int nb_errors;
781 /* output file for preprocessing (-E) */
782 FILE *ppfp;
783 enum {
784 LINE_MACRO_OUTPUT_FORMAT_GCC,
785 LINE_MACRO_OUTPUT_FORMAT_NONE,
786 LINE_MACRO_OUTPUT_FORMAT_STD,
787 LINE_MACRO_OUTPUT_FORMAT_P10 = 11
788 } Pflag; /* -P switch */
789 char dflag; /* -dX value */
791 /* for -MD/-MF: collected dependencies for this compilation */
792 char **target_deps;
793 int nb_target_deps;
795 /* compilation */
796 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
797 BufferedFile **include_stack_ptr;
799 int ifdef_stack[IFDEF_STACK_SIZE];
800 int *ifdef_stack_ptr;
802 /* included files enclosed with #ifndef MACRO */
803 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
804 CachedInclude **cached_includes;
805 int nb_cached_includes;
807 /* #pragma pack stack */
808 int pack_stack[PACK_STACK_SIZE];
809 int *pack_stack_ptr;
810 char **pragma_libs;
811 int nb_pragma_libs;
813 /* inline functions are stored as token lists and compiled last
814 only if referenced */
815 struct InlineFunc **inline_fns;
816 int nb_inline_fns;
818 /* sections */
819 Section **sections;
820 int nb_sections; /* number of sections, including first dummy section */
822 Section **priv_sections;
823 int nb_priv_sections; /* number of private sections */
825 /* got & plt handling */
826 Section *got;
827 Section *plt;
829 /* predefined sections */
830 Section *text_section, *data_section, *bss_section;
831 Section *common_section;
832 Section *cur_text_section; /* current section where function code is generated */
833 #ifdef CONFIG_TCC_BCHECK
834 /* bound check related sections */
835 Section *bounds_section; /* contains global data bound description */
836 Section *lbounds_section; /* contains local data bound description */
837 #endif
838 /* symbol sections */
839 Section *symtab_section;
840 /* debug sections */
841 Section *stab_section;
842 /* Is there a new undefined sym since last new_undef_sym() */
843 int new_undef_sym;
845 /* temporary dynamic symbol sections (for dll loading) */
846 Section *dynsymtab_section;
847 /* exported dynamic symbol section */
848 Section *dynsym;
849 /* copy of the global symtab_section variable */
850 Section *symtab;
851 /* extra attributes (eg. GOT/PLT value) for symtab symbols */
852 struct sym_attr *sym_attrs;
853 int nb_sym_attrs;
854 /* ptr to next reloc entry reused */
855 ElfW_Rel *qrel;
856 # define qrel s1->qrel
858 #ifdef TCC_TARGET_PE
859 /* PE info */
860 int pe_subsystem;
861 unsigned pe_characteristics;
862 unsigned pe_file_align;
863 unsigned pe_stack_size;
864 addr_t pe_imagebase;
865 # ifdef TCC_TARGET_X86_64
866 Section *uw_pdata;
867 int uw_sym;
868 unsigned uw_offs;
869 # endif
870 # define ELF_OBJ_ONLY
871 #endif
873 #ifndef ELF_OBJ_ONLY
874 int nb_sym_versions;
875 struct sym_version *sym_versions;
876 int nb_sym_to_version;
877 int *sym_to_version;
878 int dt_verneednum;
879 Section *versym_section;
880 Section *verneed_section;
881 #endif
883 #ifdef TCC_IS_NATIVE
884 const char *runtime_main;
885 void **runtime_mem;
886 int nb_runtime_mem;
887 #ifdef CONFIG_TCC_BACKTRACE
888 int rt_num_callers;
889 const char **rt_bound_error_msg;
890 void *rt_prog_main;
891 #endif
892 #endif
894 int fd, cc; /* used by tcc_load_ldscript */
896 /* benchmark info */
897 int total_idents;
898 int total_lines;
899 int total_bytes;
901 /* option -dnum (for general development purposes) */
902 int g_debug;
904 /* used by main and tcc_parse_args only */
905 struct filespec **files; /* files seen on command line */
906 int nb_files; /* number thereof */
907 int nb_libraries; /* number of libs thereof */
908 char *outfile; /* output filename */
909 unsigned char option_r; /* option -r */
910 unsigned char do_bench; /* option -bench */
911 int gen_deps; /* option -MD */
912 char *deps_outfile; /* option -MF */
913 unsigned char option_pthread; /* -pthread option */
914 int argc;
915 char **argv;
918 struct filespec {
919 char type;
920 char name[1];
923 /* The current value can be: */
924 #define VT_VALMASK 0x003f /* mask for value location, register or: */
925 #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
926 #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
927 #define VT_LOCAL 0x0032 /* offset on stack */
928 #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
929 #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
930 #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
931 #define VT_LVAL 0x0100 /* var is an lvalue */
932 #define VT_SYM 0x0200 /* a symbol value is added */
933 #define VT_MUSTCAST 0x0C00 /* value must be casted to be correct (used for
934 char/short stored in integer registers) */
935 #define VT_MUSTBOUND 0x4000 /* bound checking must be done before
936 dereferencing value */
937 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
938 bounding function call point is in vc */
939 /* types */
940 #define VT_BTYPE 0x000f /* mask for basic type */
941 #define VT_VOID 0 /* void type */
942 #define VT_BYTE 1 /* signed byte type */
943 #define VT_SHORT 2 /* short type */
944 #define VT_INT 3 /* integer type */
945 #define VT_LLONG 4 /* 64 bit integer */
946 #define VT_PTR 5 /* pointer */
947 #define VT_FUNC 6 /* function type */
948 #define VT_STRUCT 7 /* struct/union definition */
949 #define VT_FLOAT 8 /* IEEE float */
950 #define VT_DOUBLE 9 /* IEEE double */
951 #define VT_LDOUBLE 10 /* IEEE long double */
952 #define VT_BOOL 11 /* ISOC99 boolean type */
953 #define VT_QLONG 13 /* 128-bit integer. Only used for x86-64 ABI */
954 #define VT_QFLOAT 14 /* 128-bit float. Only used for x86-64 ABI */
956 #define VT_UNSIGNED 0x0010 /* unsigned type */
957 #define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */
958 #define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */
959 #define VT_BITFIELD 0x0080 /* bitfield modifier */
960 #define VT_CONSTANT 0x0100 /* const modifier */
961 #define VT_VOLATILE 0x0200 /* volatile modifier */
962 #define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */
963 #define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */
965 /* storage */
966 #define VT_EXTERN 0x00001000 /* extern definition */
967 #define VT_STATIC 0x00002000 /* static variable */
968 #define VT_TYPEDEF 0x00004000 /* typedef definition */
969 #define VT_INLINE 0x00008000 /* inline definition */
970 /* currently unused: 0x000[1248]0000 */
972 #define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values (32 - 2*6) */
973 #define VT_STRUCT_MASK (((1U << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD)
974 #define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f)
975 #define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f)
977 #define VT_UNION (1 << VT_STRUCT_SHIFT | VT_STRUCT)
978 #define VT_ENUM (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */
979 #define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */
981 #define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM)
982 #define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL)
983 #define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION)
985 /* type mask (except storage) */
986 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
987 #define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK))
989 /* symbol was created by tccasm.c first */
990 #define VT_ASM (VT_VOID | VT_UNSIGNED)
991 #define IS_ASM_SYM(sym) (((sym)->type.t & (VT_BTYPE | VT_ASM)) == VT_ASM)
993 /* general: set/get the pseudo-bitfield value for bit-mask M */
994 #define BFVAL(M,N) ((unsigned)((M) & ~((M) << 1)) * (N))
995 #define BFGET(X,M) (((X) & (M)) / BFVAL(M,1))
996 #define BFSET(X,M,N) ((X) = ((X) & ~(M)) | BFVAL(M,N))
998 /* token values */
1000 /* warning: the following compare tokens depend on i386 asm code */
1001 #define TOK_ULT 0x92
1002 #define TOK_UGE 0x93
1003 #define TOK_EQ 0x94
1004 #define TOK_NE 0x95
1005 #define TOK_ULE 0x96
1006 #define TOK_UGT 0x97
1007 #define TOK_Nset 0x98
1008 #define TOK_Nclear 0x99
1009 #define TOK_LT 0x9c
1010 #define TOK_GE 0x9d
1011 #define TOK_LE 0x9e
1012 #define TOK_GT 0x9f
1014 #define TOK_LAND 0xa0
1015 #define TOK_LOR 0xa1
1016 #define TOK_DEC 0xa2
1017 #define TOK_MID 0xa3 /* inc/dec, to void constant */
1018 #define TOK_INC 0xa4
1019 #define TOK_UDIV 0xb0 /* unsigned division */
1020 #define TOK_UMOD 0xb1 /* unsigned modulo */
1021 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
1023 /* tokens that carry values (in additional token string space / tokc) --> */
1024 #define TOK_CCHAR 0xb3 /* char constant in tokc */
1025 #define TOK_LCHAR 0xb4
1026 #define TOK_CINT 0xb5 /* number in tokc */
1027 #define TOK_CUINT 0xb6 /* unsigned int constant */
1028 #define TOK_CLLONG 0xb7 /* long long constant */
1029 #define TOK_CULLONG 0xb8 /* unsigned long long constant */
1030 #define TOK_STR 0xb9 /* pointer to string in tokc */
1031 #define TOK_LSTR 0xba
1032 #define TOK_CFLOAT 0xbb /* float constant */
1033 #define TOK_CDOUBLE 0xbc /* double constant */
1034 #define TOK_CLDOUBLE 0xbd /* long double constant */
1035 #define TOK_PPNUM 0xbe /* preprocessor number */
1036 #define TOK_PPSTR 0xbf /* preprocessor string */
1037 #define TOK_LINENUM 0xc0 /* line number info */
1038 #define TOK_TWODOTS 0xa8 /* C++ token ? */
1039 /* <-- */
1041 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
1042 #define TOK_ADDC1 0xc3 /* add with carry generation */
1043 #define TOK_ADDC2 0xc4 /* add with carry use */
1044 #define TOK_SUBC1 0xc5 /* add with carry generation */
1045 #define TOK_SUBC2 0xc6 /* add with carry use */
1046 #define TOK_ARROW 0xc7
1047 #define TOK_DOTS 0xc8 /* three dots */
1048 #define TOK_SHR 0xc9 /* unsigned shift right */
1049 #define TOK_TWOSHARPS 0xca /* ## preprocessing token */
1050 #define TOK_PLCHLDR 0xcb /* placeholder token as defined in C99 */
1051 #define TOK_NOSUBST 0xcc /* means following token has already been pp'd */
1052 #define TOK_PPJOIN 0xcd /* A '##' in the right position to mean pasting */
1053 #define TOK_CLONG 0xce /* long constant */
1054 #define TOK_CULONG 0xcf /* unsigned long constant */
1056 #define TOK_SHL 0x01 /* shift left */
1057 #define TOK_SAR 0x02 /* signed shift right */
1059 /* assignment operators : normal operator or 0x80 */
1060 #define TOK_A_MOD 0xa5
1061 #define TOK_A_AND 0xa6
1062 #define TOK_A_MUL 0xaa
1063 #define TOK_A_ADD 0xab
1064 #define TOK_A_SUB 0xad
1065 #define TOK_A_DIV 0xaf
1066 #define TOK_A_XOR 0xde
1067 #define TOK_A_OR 0xfc
1068 #define TOK_A_SHL 0x81
1069 #define TOK_A_SAR 0x82
1071 #define TOK_EOF (-1) /* end of file */
1072 #define TOK_LINEFEED 10 /* line feed */
1074 /* all identifiers and strings have token above that */
1075 #define TOK_IDENT 256
1077 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
1078 #define TOK_ASM_int TOK_INT
1079 #define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
1080 #define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
1081 #define TOK_ASMDIR_LAST TOK_ASMDIR_section
1083 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1084 /* only used for i386 asm opcodes definitions */
1085 #define DEF_BWL(x) \
1086 DEF(TOK_ASM_ ## x ## b, #x "b") \
1087 DEF(TOK_ASM_ ## x ## w, #x "w") \
1088 DEF(TOK_ASM_ ## x ## l, #x "l") \
1089 DEF(TOK_ASM_ ## x, #x)
1090 #define DEF_WL(x) \
1091 DEF(TOK_ASM_ ## x ## w, #x "w") \
1092 DEF(TOK_ASM_ ## x ## l, #x "l") \
1093 DEF(TOK_ASM_ ## x, #x)
1094 #ifdef TCC_TARGET_X86_64
1095 # define DEF_BWLQ(x) \
1096 DEF(TOK_ASM_ ## x ## b, #x "b") \
1097 DEF(TOK_ASM_ ## x ## w, #x "w") \
1098 DEF(TOK_ASM_ ## x ## l, #x "l") \
1099 DEF(TOK_ASM_ ## x ## q, #x "q") \
1100 DEF(TOK_ASM_ ## x, #x)
1101 # define DEF_WLQ(x) \
1102 DEF(TOK_ASM_ ## x ## w, #x "w") \
1103 DEF(TOK_ASM_ ## x ## l, #x "l") \
1104 DEF(TOK_ASM_ ## x ## q, #x "q") \
1105 DEF(TOK_ASM_ ## x, #x)
1106 # define DEF_BWLX DEF_BWLQ
1107 # define DEF_WLX DEF_WLQ
1108 /* number of sizes + 1 */
1109 # define NBWLX 5
1110 #else
1111 # define DEF_BWLX DEF_BWL
1112 # define DEF_WLX DEF_WL
1113 /* number of sizes + 1 */
1114 # define NBWLX 4
1115 #endif
1117 #define DEF_FP1(x) \
1118 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
1119 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
1120 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
1121 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
1123 #define DEF_FP(x) \
1124 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
1125 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
1126 DEF_FP1(x)
1128 #define DEF_ASMTEST(x,suffix) \
1129 DEF_ASM(x ## o ## suffix) \
1130 DEF_ASM(x ## no ## suffix) \
1131 DEF_ASM(x ## b ## suffix) \
1132 DEF_ASM(x ## c ## suffix) \
1133 DEF_ASM(x ## nae ## suffix) \
1134 DEF_ASM(x ## nb ## suffix) \
1135 DEF_ASM(x ## nc ## suffix) \
1136 DEF_ASM(x ## ae ## suffix) \
1137 DEF_ASM(x ## e ## suffix) \
1138 DEF_ASM(x ## z ## suffix) \
1139 DEF_ASM(x ## ne ## suffix) \
1140 DEF_ASM(x ## nz ## suffix) \
1141 DEF_ASM(x ## be ## suffix) \
1142 DEF_ASM(x ## na ## suffix) \
1143 DEF_ASM(x ## nbe ## suffix) \
1144 DEF_ASM(x ## a ## suffix) \
1145 DEF_ASM(x ## s ## suffix) \
1146 DEF_ASM(x ## ns ## suffix) \
1147 DEF_ASM(x ## p ## suffix) \
1148 DEF_ASM(x ## pe ## suffix) \
1149 DEF_ASM(x ## np ## suffix) \
1150 DEF_ASM(x ## po ## suffix) \
1151 DEF_ASM(x ## l ## suffix) \
1152 DEF_ASM(x ## nge ## suffix) \
1153 DEF_ASM(x ## nl ## suffix) \
1154 DEF_ASM(x ## ge ## suffix) \
1155 DEF_ASM(x ## le ## suffix) \
1156 DEF_ASM(x ## ng ## suffix) \
1157 DEF_ASM(x ## nle ## suffix) \
1158 DEF_ASM(x ## g ## suffix)
1160 #endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
1162 enum tcc_token {
1163 TOK_LAST = TOK_IDENT - 1
1164 #define DEF(id, str) ,id
1165 #include "tcctok.h"
1166 #undef DEF
1169 /* keywords: tok >= TOK_IDENT && tok < TOK_UIDENT */
1170 #define TOK_UIDENT TOK_DEFINE
1172 /* ------------ libtcc.c ------------ */
1174 ST_DATA struct TCCState *tcc_state;
1176 /* public functions currently used by the tcc main function */
1177 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s);
1178 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s);
1179 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num);
1180 PUB_FUNC char *tcc_basename(const char *name);
1181 PUB_FUNC char *tcc_fileextension (const char *name);
1183 #ifndef MEM_DEBUG
1184 PUB_FUNC void tcc_free(void *ptr);
1185 PUB_FUNC void *tcc_malloc(unsigned long size);
1186 PUB_FUNC void *tcc_mallocz(unsigned long size);
1187 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
1188 PUB_FUNC char *tcc_strdup(const char *str);
1189 #else
1190 #define tcc_free(ptr) tcc_free_debug(ptr)
1191 #define tcc_malloc(size) tcc_malloc_debug(size, __FILE__, __LINE__)
1192 #define tcc_mallocz(size) tcc_mallocz_debug(size, __FILE__, __LINE__)
1193 #define tcc_realloc(ptr,size) tcc_realloc_debug(ptr, size, __FILE__, __LINE__)
1194 #define tcc_strdup(str) tcc_strdup_debug(str, __FILE__, __LINE__)
1195 PUB_FUNC void tcc_free_debug(void *ptr);
1196 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line);
1197 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line);
1198 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line);
1199 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line);
1200 #endif
1202 #define free(p) use_tcc_free(p)
1203 #define malloc(s) use_tcc_malloc(s)
1204 #define realloc(p, s) use_tcc_realloc(p, s)
1205 #undef strdup
1206 #define strdup(s) use_tcc_strdup(s)
1207 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...);
1208 PUB_FUNC NORETURN void _tcc_error(const char *fmt, ...);
1209 PUB_FUNC void _tcc_warning(const char *fmt, ...);
1211 /* other utilities */
1212 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data);
1213 ST_FUNC void dynarray_reset(void *pp, int *n);
1214 ST_INLN void cstr_ccat(CString *cstr, int ch);
1215 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len);
1216 ST_FUNC void cstr_wccat(CString *cstr, int ch);
1217 ST_FUNC void cstr_new(CString *cstr);
1218 ST_FUNC void cstr_free(CString *cstr);
1219 ST_FUNC int cstr_printf(CString *cs, const char *fmt, ...);
1220 ST_FUNC void cstr_reset(CString *cstr);
1222 ST_INLN void sym_free(Sym *sym);
1223 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c);
1224 ST_FUNC Sym *sym_find2(Sym *s, int v);
1225 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
1226 ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep);
1227 ST_INLN Sym *struct_find(int v);
1228 ST_INLN Sym *sym_find(int v);
1229 ST_FUNC Sym *global_identifier_push(int v, int t, int c);
1231 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
1232 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
1233 ST_FUNC void tcc_close(void);
1235 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
1236 /* flags: */
1237 #define AFF_PRINT_ERROR 0x10 /* print error if file not found */
1238 #define AFF_REFERENCED_DLL 0x20 /* load a referenced dll from another dll */
1239 #define AFF_TYPE_BIN 0x40 /* file to add is binary */
1240 #define AFF_WHOLE_ARCHIVE 0x80 /* load all objects from archive */
1241 /* s->filetype: */
1242 #define AFF_TYPE_NONE 0
1243 #define AFF_TYPE_C 1
1244 #define AFF_TYPE_ASM 2
1245 #define AFF_TYPE_ASMPP 4
1246 #define AFF_TYPE_LIB 8
1247 #define AFF_TYPE_MASK (15 | AFF_TYPE_BIN)
1248 /* values from tcc_object_type(...) */
1249 #define AFF_BINTYPE_REL 1
1250 #define AFF_BINTYPE_DYN 2
1251 #define AFF_BINTYPE_AR 3
1252 #define AFF_BINTYPE_C67 4
1255 #ifndef TCC_TARGET_PE
1256 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
1257 #endif
1258 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
1259 #ifdef CONFIG_TCC_BCHECK
1260 ST_FUNC void tcc_add_bcheck(TCCState *s1);
1261 #endif
1262 ST_FUNC void tcc_add_pragma_libs(TCCState *s1);
1263 PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f);
1264 PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time);
1265 PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv, int optind);
1266 #ifdef _WIN32
1267 ST_FUNC char *normalize_slashes(char *path);
1268 #endif
1270 /* tcc_parse_args return codes: */
1271 #define OPT_HELP 1
1272 #define OPT_HELP2 2
1273 #define OPT_V 3
1274 #define OPT_PRINT_DIRS 4
1275 #define OPT_AR 5
1276 #define OPT_IMPDEF 6
1277 #define OPT_M32 32
1278 #define OPT_M64 64
1280 /* ------------ tccpp.c ------------ */
1282 ST_DATA struct BufferedFile *file;
1283 ST_DATA int ch, tok;
1284 ST_DATA CValue tokc;
1285 ST_DATA const int *macro_ptr;
1286 ST_DATA int parse_flags;
1287 ST_DATA int tok_flags;
1288 ST_DATA CString tokcstr; /* current parsed string, if any */
1290 /* display benchmark infos */
1291 ST_DATA int tok_ident;
1292 ST_DATA TokenSym **table_ident;
1294 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
1295 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
1296 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
1297 #define TOK_FLAG_EOF 0x0008 /* end of file */
1299 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
1300 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
1301 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
1302 token. line feed is also
1303 returned at eof */
1304 #define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */
1305 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1306 #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
1307 #define PARSE_FLAG_TOK_STR 0x0040 /* return parsed strings instead of TOK_PPSTR */
1309 /* isidnum_table flags: */
1310 #define IS_SPC 1
1311 #define IS_ID 2
1312 #define IS_NUM 4
1314 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1315 ST_FUNC const char *get_tok_str(int v, CValue *cv);
1316 ST_FUNC void begin_macro(TokenString *str, int alloc);
1317 ST_FUNC void end_macro(void);
1318 ST_FUNC int set_idnum(int c, int val);
1319 ST_INLN void tok_str_new(TokenString *s);
1320 ST_FUNC TokenString *tok_str_alloc(void);
1321 ST_FUNC void tok_str_free(TokenString *s);
1322 ST_FUNC void tok_str_free_str(int *str);
1323 ST_FUNC void tok_str_add(TokenString *s, int t);
1324 ST_FUNC void tok_str_add_tok(TokenString *s);
1325 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
1326 ST_FUNC void define_undef(Sym *s);
1327 ST_INLN Sym *define_find(int v);
1328 ST_FUNC void free_defines(Sym *b);
1329 ST_FUNC Sym *label_find(int v);
1330 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
1331 ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep);
1332 ST_FUNC void parse_define(void);
1333 ST_FUNC void preprocess(int is_bof);
1334 ST_FUNC void next_nomacro(void);
1335 ST_FUNC void next(void);
1336 ST_INLN void unget_tok(int last_tok);
1337 ST_FUNC void preprocess_start(TCCState *s1, int is_asm);
1338 ST_FUNC void preprocess_end(TCCState *s1);
1339 ST_FUNC void tccpp_new(TCCState *s);
1340 ST_FUNC void tccpp_delete(TCCState *s);
1341 ST_FUNC int tcc_preprocess(TCCState *s1);
1342 ST_FUNC void skip(int c);
1343 ST_FUNC NORETURN void expect(const char *msg);
1345 /* space excluding newline */
1346 static inline int is_space(int ch) {
1347 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1349 static inline int isid(int c) {
1350 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
1352 static inline int isnum(int c) {
1353 return c >= '0' && c <= '9';
1355 static inline int isoct(int c) {
1356 return c >= '0' && c <= '7';
1358 static inline int toup(int c) {
1359 return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
1362 /* ------------ tccgen.c ------------ */
1364 #define SYM_POOL_NB (8192 / sizeof(Sym))
1366 ST_DATA Sym *global_stack;
1367 ST_DATA Sym *local_stack;
1368 ST_DATA Sym *local_label_stack;
1369 ST_DATA Sym *global_label_stack;
1370 ST_DATA Sym *define_stack;
1371 ST_DATA CType int_type, func_old_type, char_pointer_type;
1372 ST_DATA SValue *vtop;
1373 ST_DATA int rsym, anon_sym, ind, loc;
1375 ST_DATA int const_wanted; /* true if constant wanted */
1376 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
1377 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
1378 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
1379 ST_DATA int func_var; /* true if current function is variadic */
1380 ST_DATA int func_vc;
1381 ST_DATA const char *funcname;
1383 ST_FUNC void tcc_debug_start(TCCState *s1);
1384 ST_FUNC void tcc_debug_end(TCCState *s1);
1385 ST_FUNC void tcc_debug_bincl(TCCState *s1);
1386 ST_FUNC void tcc_debug_eincl(TCCState *s1);
1387 ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename);
1388 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym);
1389 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size);
1390 ST_FUNC void tcc_debug_line(TCCState *s1);
1392 ST_FUNC void tccgen_init(TCCState *s1);
1393 ST_FUNC int tccgen_compile(TCCState *s1);
1394 ST_FUNC void tccgen_finish(TCCState *s1);
1395 ST_FUNC void check_vstack(void);
1397 ST_INLN int is_float(int t);
1398 ST_FUNC int ieee_finite(double d);
1399 ST_FUNC void test_lvalue(void);
1400 ST_FUNC void vpushi(int v);
1401 ST_FUNC ElfSym *elfsym(Sym *);
1402 ST_FUNC void update_storage(Sym *sym);
1403 ST_FUNC Sym *external_global_sym(int v, CType *type);
1404 ST_FUNC void vset(CType *type, int r, int v);
1405 ST_FUNC void vset_VT_CMP(int op);
1406 ST_FUNC void vswap(void);
1407 ST_FUNC void vpush_global_sym(CType *type, int v);
1408 ST_FUNC void vrote(SValue *e, int n);
1409 ST_FUNC void vrott(int n);
1410 ST_FUNC void vrotb(int n);
1411 #if PTR_SIZE == 4
1412 ST_FUNC void lexpand(void);
1413 #endif
1414 #ifdef TCC_TARGET_ARM
1415 ST_FUNC int get_reg_ex(int rc, int rc2);
1416 #endif
1417 ST_FUNC void vpushv(SValue *v);
1418 ST_FUNC void save_reg(int r);
1419 ST_FUNC void save_reg_upstack(int r, int n);
1420 ST_FUNC int get_reg(int rc);
1421 ST_FUNC void save_regs(int n);
1422 ST_FUNC void gaddrof(void);
1423 ST_FUNC int gv(int rc);
1424 ST_FUNC void gv2(int rc1, int rc2);
1425 ST_FUNC void vpop(void);
1426 ST_FUNC void gen_op(int op);
1427 ST_FUNC int type_size(CType *type, int *a);
1428 ST_FUNC void mk_pointer(CType *type);
1429 ST_FUNC void vstore(void);
1430 ST_FUNC void inc(int post, int c);
1431 ST_FUNC void parse_mult_str (CString *astr, const char *msg);
1432 ST_FUNC void parse_asm_str(CString *astr);
1433 ST_FUNC void indir(void);
1434 ST_FUNC void unary(void);
1435 ST_FUNC void expr_prod(void);
1436 ST_FUNC void expr_sum(void);
1437 ST_FUNC void gexpr(void);
1438 ST_FUNC int expr_const(void);
1439 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1440 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
1441 #endif
1442 #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
1443 ST_FUNC int classify_x86_64_va_arg(CType *ty);
1444 #endif
1445 #ifdef CONFIG_TCC_BCHECK
1446 ST_FUNC void gbound_args(int nb_args);
1447 #endif
1449 /* ------------ tccelf.c ------------ */
1451 #define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
1452 #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
1453 #define TCC_OUTPUT_FORMAT_COFF 2 /* COFF */
1455 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1457 typedef struct {
1458 unsigned int n_strx; /* index into string table of name */
1459 unsigned char n_type; /* type of symbol */
1460 unsigned char n_other; /* misc info (usually empty) */
1461 unsigned short n_desc; /* description field */
1462 unsigned int n_value; /* value of symbol */
1463 } Stab_Sym;
1465 ST_FUNC void tccelf_new(TCCState *s);
1466 ST_FUNC void tccelf_delete(TCCState *s);
1467 ST_FUNC void tccelf_stab_new(TCCState *s);
1468 ST_FUNC void tccelf_begin_file(TCCState *s1);
1469 ST_FUNC void tccelf_end_file(TCCState *s1);
1470 #ifdef CONFIG_TCC_BCHECK
1471 ST_FUNC void tccelf_bounds_new(TCCState *s);
1472 #endif
1473 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
1474 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
1475 ST_FUNC size_t section_add(Section *sec, addr_t size, int align);
1476 ST_FUNC void *section_ptr_add(Section *sec, addr_t size);
1477 ST_FUNC void section_reserve(Section *sec, unsigned long size);
1478 ST_FUNC Section *find_section(TCCState *s1, const char *name);
1479 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);
1481 ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, addr_t value, unsigned long size, int can_add_underscore);
1482 ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
1483 #if PTR_SIZE == 4
1484 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
1485 #endif
1486 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, addr_t addend);
1488 ST_FUNC void add_init_array (TCCState *s1, Sym *sym);
1489 ST_FUNC void add_fini_array (TCCState *s1, Sym *sym);
1491 ST_FUNC int put_elf_str(Section *s, const char *sym);
1492 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1493 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1494 ST_FUNC int find_elf_sym(Section *s, const char *name);
1495 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1496 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);
1498 ST_FUNC void put_stabs(TCCState *s1, const char *str, int type, int other, int desc, unsigned long value);
1499 ST_FUNC void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
1500 ST_FUNC void put_stabn(TCCState *s1, int type, int other, int desc, int value);
1502 ST_FUNC void resolve_common_syms(TCCState *s1);
1503 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve);
1504 ST_FUNC void relocate_section(TCCState *s1, Section *s);
1506 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h);
1507 ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1508 ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte);
1509 ST_FUNC void tcc_add_runtime(TCCState *s1);
1511 #ifndef ELF_OBJ_ONLY
1512 ST_FUNC void build_got_entries(TCCState *s1);
1513 #endif
1514 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc);
1515 ST_FUNC void squeeze_multi_relocs(Section *sec, size_t oldrelocoffset);
1517 ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
1518 ST_FUNC void list_elf_symbols(TCCState *s, void *ctx,
1519 void (*symbol_cb)(void *ctx, const char *name, const void *val));
1520 #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE
1521 ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
1522 #endif
1524 #ifndef TCC_TARGET_PE
1525 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1526 ST_FUNC int tcc_load_ldscript(TCCState *s1, int fd);
1527 #endif
1529 /* ------------ xxx-link.c ------------ */
1531 /* Whether to generate a GOT/PLT entry and when. NO_GOTPLT_ENTRY is first so
1532 that unknown relocation don't create a GOT or PLT entry */
1533 enum gotplt_entry {
1534 NO_GOTPLT_ENTRY, /* never generate (eg. GLOB_DAT & JMP_SLOT relocs) */
1535 BUILD_GOT_ONLY, /* only build GOT (eg. TPOFF relocs) */
1536 AUTO_GOTPLT_ENTRY, /* generate if sym is UNDEF */
1537 ALWAYS_GOTPLT_ENTRY /* always generate (eg. PLTOFF relocs) */
1540 #ifndef ELF_OBJ_ONLY
1541 ST_FUNC int code_reloc (int reloc_type);
1542 ST_FUNC int gotplt_entry_type (int reloc_type);
1543 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr);
1544 ST_FUNC void relocate_plt(TCCState *s1);
1545 #endif
1546 ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val);
1548 /* ------------ xxx-gen.c ------------ */
1550 ST_DATA const int reg_classes[NB_REGS];
1552 ST_FUNC void gsym_addr(int t, int a);
1553 ST_FUNC void gsym(int t);
1554 ST_FUNC void load(int r, SValue *sv);
1555 ST_FUNC void store(int r, SValue *v);
1556 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align, int *regsize);
1557 ST_FUNC void gfunc_call(int nb_args);
1558 ST_FUNC void gfunc_prolog(Sym *func_sym);
1559 ST_FUNC void gfunc_epilog(void);
1560 ST_FUNC void gen_fill_nops(int);
1561 ST_FUNC int gjmp(int t);
1562 ST_FUNC void gjmp_addr(int a);
1563 ST_FUNC int gjmp_cond(int op, int t);
1564 ST_FUNC int gjmp_append(int n, int t);
1565 ST_FUNC void gen_opi(int op);
1566 ST_FUNC void gen_opf(int op);
1567 ST_FUNC void gen_cvt_ftoi(int t);
1568 ST_FUNC void gen_cvt_itof(int t);
1569 ST_FUNC void gen_cvt_ftof(int t);
1570 ST_FUNC void ggoto(void);
1571 #ifndef TCC_TARGET_C67
1572 ST_FUNC void o(unsigned int c);
1573 #endif
1574 ST_FUNC void gen_vla_sp_save(int addr);
1575 ST_FUNC void gen_vla_sp_restore(int addr);
1576 ST_FUNC void gen_vla_alloc(CType *type, int align);
1578 static inline uint16_t read16le(unsigned char *p) {
1579 return p[0] | (uint16_t)p[1] << 8;
1581 static inline void write16le(unsigned char *p, uint16_t x) {
1582 p[0] = x & 255; p[1] = x >> 8 & 255;
1584 static inline uint32_t read32le(unsigned char *p) {
1585 return read16le(p) | (uint32_t)read16le(p + 2) << 16;
1587 static inline void write32le(unsigned char *p, uint32_t x) {
1588 write16le(p, x); write16le(p + 2, x >> 16);
1590 static inline void add32le(unsigned char *p, int32_t x) {
1591 write32le(p, read32le(p) + x);
1593 static inline uint64_t read64le(unsigned char *p) {
1594 return read32le(p) | (uint64_t)read32le(p + 4) << 32;
1596 static inline void write64le(unsigned char *p, uint64_t x) {
1597 write32le(p, x); write32le(p + 4, x >> 32);
1599 static inline void add64le(unsigned char *p, int64_t x) {
1600 write64le(p, read64le(p) + x);
1603 /* ------------ i386-gen.c ------------ */
1604 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1605 ST_FUNC void g(int c);
1606 ST_FUNC void gen_le16(int c);
1607 ST_FUNC void gen_le32(int c);
1608 ST_FUNC void gen_addr32(int r, Sym *sym, int c);
1609 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
1610 ST_FUNC void gen_cvt_csti(int t);
1611 #endif
1613 #ifdef CONFIG_TCC_BCHECK
1614 ST_FUNC void gen_bounded_ptr_add(void);
1615 ST_FUNC void gen_bounded_ptr_deref(void);
1616 #endif
1618 /* ------------ x86_64-gen.c ------------ */
1619 #ifdef TCC_TARGET_X86_64
1620 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
1621 ST_FUNC void gen_opl(int op);
1622 #ifdef TCC_TARGET_PE
1623 ST_FUNC void gen_vla_result(int addr);
1624 #endif
1625 ST_FUNC void gen_cvt_sxtw(void);
1626 ST_FUNC void gen_cvt_csti(int t);
1627 #endif
1629 /* ------------ arm-gen.c ------------ */
1630 #ifdef TCC_TARGET_ARM
1631 #if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP)
1632 PUB_FUNC const char *default_elfinterp(struct TCCState *s);
1633 #endif
1634 ST_FUNC void arm_init(struct TCCState *s);
1635 #endif
1637 /* ------------ arm64-gen.c ------------ */
1638 #ifdef TCC_TARGET_ARM64
1639 ST_FUNC void gen_opl(int op);
1640 ST_FUNC void gfunc_return(CType *func_type);
1641 ST_FUNC void gen_va_start(void);
1642 ST_FUNC void gen_va_arg(CType *t);
1643 ST_FUNC void gen_clear_cache(void);
1644 ST_FUNC void gen_cvt_sxtw(void);
1645 ST_FUNC void gen_cvt_csti(int t);
1646 #endif
1648 /* ------------ riscv64-gen.c ------------ */
1649 #ifdef TCC_TARGET_RISCV64
1650 ST_FUNC void gen_opl(int op);
1651 //ST_FUNC void gfunc_return(CType *func_type);
1652 ST_FUNC void gen_va_start(void);
1653 ST_FUNC void arch_transfer_ret_regs(int);
1654 ST_FUNC void gen_cvt_sxtw(void);
1655 #endif
1657 /* ------------ c67-gen.c ------------ */
1658 #ifdef TCC_TARGET_C67
1659 #endif
1661 /* ------------ tcccoff.c ------------ */
1663 #ifdef TCC_TARGET_COFF
1664 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
1665 ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
1666 #endif
1668 /* ------------ tccasm.c ------------ */
1669 ST_FUNC void asm_instr(void);
1670 ST_FUNC void asm_global_instr(void);
1671 #ifdef CONFIG_TCC_ASM
1672 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
1673 ST_FUNC Sym* get_asm_sym(int name, Sym *csym);
1674 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
1675 ST_FUNC int asm_int_expr(TCCState *s1);
1676 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1677 /* ------------ i386-asm.c ------------ */
1678 ST_FUNC void gen_expr32(ExprValue *pe);
1679 #ifdef TCC_TARGET_X86_64
1680 ST_FUNC void gen_expr64(ExprValue *pe);
1681 #endif
1682 ST_FUNC void asm_opcode(TCCState *s1, int opcode);
1683 ST_FUNC int asm_parse_regvar(int t);
1684 ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
1685 ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
1686 ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
1687 ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
1688 #endif
1690 /* ------------ tccpe.c -------------- */
1691 #ifdef TCC_TARGET_PE
1692 ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
1693 ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
1694 ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
1695 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1696 ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
1697 #endif
1698 #ifdef TCC_TARGET_X86_64
1699 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
1700 #endif
1701 PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp);
1702 /* symbol properties stored in Elf32_Sym->st_other */
1703 # define ST_PE_EXPORT 0x10
1704 # define ST_PE_IMPORT 0x20
1705 # define ST_PE_STDCALL 0x40
1706 #endif
1707 #define ST_ASM_SET 0x04
1709 /* ------------ tccrun.c ----------------- */
1710 #ifdef TCC_IS_NATIVE
1711 #ifdef CONFIG_TCC_STATIC
1712 #define RTLD_LAZY 0x001
1713 #define RTLD_NOW 0x002
1714 #define RTLD_GLOBAL 0x100
1715 #define RTLD_DEFAULT NULL
1716 /* dummy function for profiling */
1717 ST_FUNC void *dlopen(const char *filename, int flag);
1718 ST_FUNC void dlclose(void *p);
1719 ST_FUNC const char *dlerror(void);
1720 ST_FUNC void *dlsym(void *handle, const char *symbol);
1721 #endif
1722 ST_FUNC void tcc_run_free(TCCState *s1);
1723 #endif
1725 /* ------------ tcctools.c ----------------- */
1726 #if 0 /* included in tcc.c */
1727 ST_FUNC int tcc_tool_ar(TCCState *s, int argc, char **argv);
1728 #ifdef TCC_TARGET_PE
1729 ST_FUNC int tcc_tool_impdef(TCCState *s, int argc, char **argv);
1730 #endif
1731 ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option);
1732 ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename);
1733 #endif
1735 /********************************************************/
1736 #undef ST_DATA
1737 #if ONE_SOURCE
1738 #define ST_DATA static
1739 #else
1740 #define ST_DATA
1741 #endif
1742 /********************************************************/
1744 #define text_section TCC_STATE_VAR(text_section)
1745 #define data_section TCC_STATE_VAR(data_section)
1746 #define bss_section TCC_STATE_VAR(bss_section)
1747 #define common_section TCC_STATE_VAR(common_section)
1748 #define cur_text_section TCC_STATE_VAR(cur_text_section)
1749 #define bounds_section TCC_STATE_VAR(bounds_section)
1750 #define lbounds_section TCC_STATE_VAR(lbounds_section)
1751 #define symtab_section TCC_STATE_VAR(symtab_section)
1752 #define stab_section TCC_STATE_VAR(stab_section)
1753 #define stabstr_section stab_section->link
1754 #define gnu_ext TCC_STATE_VAR(gnu_ext)
1755 #define tcc_error_noabort TCC_SET_STATE(_tcc_error_noabort)
1756 #define tcc_error TCC_SET_STATE(_tcc_error)
1757 #define tcc_warning TCC_SET_STATE(_tcc_warning)
1759 #define total_idents TCC_STATE_VAR(total_idents)
1760 #define total_lines TCC_STATE_VAR(total_lines)
1761 #define total_bytes TCC_STATE_VAR(total_bytes)
1763 PUB_FUNC void tcc_enter_state(TCCState *s1);
1765 /********************************************************/
1766 #endif /* _TCC_H */
1768 #undef TCC_STATE_VAR
1769 #undef TCC_SET_STATE
1771 #ifdef USING_GLOBALS
1772 # define TCC_STATE_VAR(sym) tcc_state->sym
1773 # define TCC_SET_STATE(fn) fn
1774 # undef USING_GLOBALS
1775 #else
1776 # define TCC_STATE_VAR(sym) s1->sym
1777 # define TCC_SET_STATE(fn) (tcc_enter_state(s1),fn)
1778 /* actually we could avoid the tcc_enter_state(s1) hack by using
1779 __VA_ARGS__ except that some compiler doesn't support it. */
1780 #endif