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