Fix macos warning for create_gnu_hash
[tinycc.git] / tcc.h
blobaa04404a620bd48f69c210fc7c4705b1a39b3017
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 #define _DARWIN_C_SOURCE
26 #include "config.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 /* gnu headers use to #define __attribute__ to empty for non-gcc compilers */
32 #ifdef __TINYC__
33 # undef __attribute__
34 #endif
35 #include <string.h>
36 #include <errno.h>
37 #include <math.h>
38 #include <fcntl.h>
39 #include <setjmp.h>
40 #include <time.h>
42 #ifndef _WIN32
43 # include <unistd.h>
44 # include <sys/time.h>
45 # ifndef CONFIG_TCC_STATIC
46 # include <dlfcn.h>
47 # endif
48 /* XXX: need to define this to use them in non ISOC99 context */
49 extern float strtof (const char *__nptr, char **__endptr);
50 extern long double strtold (const char *__nptr, char **__endptr);
51 #endif
53 #ifdef _WIN32
54 # define WIN32_LEAN_AND_MEAN 1
55 # include <windows.h>
56 # include <io.h> /* open, close etc. */
57 # include <direct.h> /* getcwd */
58 # include <malloc.h> /* alloca */
59 # ifdef __GNUC__
60 # include <stdint.h>
61 # endif
62 # define inline __inline
63 # define snprintf _snprintf
64 # define vsnprintf _vsnprintf
65 # ifndef __GNUC__
66 # define strtold (long double)strtod
67 # define strtof (float)strtod
68 # ifdef _WIN64
69 # define strtoll _strtoi64
70 # define strtoull _strtoui64
71 # else
72 # define strtoll strtol
73 # define strtoull strtoul
74 # endif
75 # endif
76 # ifdef LIBTCC_AS_DLL
77 # define LIBTCCAPI __declspec(dllexport)
78 # define PUB_FUNC LIBTCCAPI
79 # endif
80 # ifdef _MSC_VER
81 # pragma warning (disable : 4244) // conversion from 'uint64_t' to 'int', possible loss of data
82 # pragma warning (disable : 4267) // conversion from 'size_t' to 'int', possible loss of data
83 # pragma warning (disable : 4996) // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
84 # pragma warning (disable : 4018) // signed/unsigned mismatch
85 # pragma warning (disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
86 # define ssize_t intptr_t
87 # ifdef _X86_
88 # define __i386__ 1
89 # endif
90 # ifdef _AMD64_
91 # define __x86_64__ 1
92 # endif
93 # endif
94 # ifndef va_copy
95 # define va_copy(a,b) a = b
96 # endif
97 # undef CONFIG_TCC_STATIC
98 #endif
100 #ifndef PAGESIZE
101 # ifdef _SC_PAGESIZE
102 # define PAGESIZE sysconf(_SC_PAGESIZE)
103 # else
104 # define PAGESIZE 4096
105 # endif
106 #endif
108 #ifndef O_BINARY
109 # define O_BINARY 0
110 #endif
112 #ifndef offsetof
113 #define offsetof(type, field) ((size_t) &((type *)0)->field)
114 #endif
116 #ifndef countof
117 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
118 #endif
120 #ifdef _MSC_VER
121 # define NORETURN __declspec(noreturn)
122 # define ALIGNED(x) __declspec(align(x))
123 # define PRINTF_LIKE(x,y)
124 #else
125 # define NORETURN __attribute__((noreturn))
126 # define ALIGNED(x) __attribute__((aligned(x)))
127 # define PRINTF_LIKE(x,y) __attribute__ ((format (printf, (x), (y))))
128 #endif
130 #ifdef _WIN32
131 # define IS_DIRSEP(c) (c == '/' || c == '\\')
132 # define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
133 # define PATHCMP stricmp
134 # define PATHSEP ";"
135 #else
136 # define IS_DIRSEP(c) (c == '/')
137 # define IS_ABSPATH(p) IS_DIRSEP(p[0])
138 # define PATHCMP strcmp
139 # define PATHSEP ":"
140 #endif
142 /* -------------------------------------------- */
144 /* parser debug */
145 /* #define PARSE_DEBUG */
146 /* preprocessor debug */
147 /* #define PP_DEBUG */
148 /* include file debug */
149 /* #define INC_DEBUG */
150 /* memory leak debug (only for single threaded usage) */
151 /* #define MEM_DEBUG */
152 /* assembler debug */
153 /* #define ASM_DEBUG */
155 /* target selection */
156 /* #define TCC_TARGET_I386 *//* i386 code generator */
157 /* #define TCC_TARGET_X86_64 *//* x86-64 code generator */
158 /* #define TCC_TARGET_ARM *//* ARMv4 code generator */
159 /* #define TCC_TARGET_ARM64 *//* ARMv8 code generator */
160 /* #define TCC_TARGET_C67 *//* TMS320C67xx code generator */
161 /* #define TCC_TARGET_RISCV64 *//* risc-v code generator */
163 /* default target is I386 */
164 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
165 !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
166 !defined(TCC_TARGET_X86_64) && !defined(TCC_TARGET_RISCV64)
167 # if defined __x86_64__
168 # define TCC_TARGET_X86_64
169 # elif defined __arm__
170 # define TCC_TARGET_ARM
171 # define TCC_ARM_EABI
172 # define TCC_ARM_VFP
173 # define TCC_ARM_HARDFLOAT
174 # elif defined __aarch64__
175 # define TCC_TARGET_ARM64
176 # elif defined __riscv
177 # define TCC_TARGET_RISCV64
178 # else
179 # define TCC_TARGET_I386
180 # endif
181 # ifdef _WIN32
182 # define TCC_TARGET_PE 1
183 # endif
184 # ifdef __APPLE__
185 # define TCC_TARGET_MACHO 1
186 # endif
187 #endif
189 /* only native compiler supports -run */
190 #if defined _WIN32 == defined TCC_TARGET_PE \
191 && defined __APPLE__ == defined TCC_TARGET_MACHO
192 # if defined __i386__ && defined TCC_TARGET_I386
193 # define TCC_IS_NATIVE
194 # elif defined __x86_64__ && defined TCC_TARGET_X86_64
195 # define TCC_IS_NATIVE
196 # elif defined __arm__ && defined TCC_TARGET_ARM
197 # define TCC_IS_NATIVE
198 # elif defined __aarch64__ && defined TCC_TARGET_ARM64
199 # define TCC_IS_NATIVE
200 # elif defined __riscv && defined __LP64__ && defined TCC_TARGET_RISCV64
201 # define TCC_IS_NATIVE
202 # endif
203 #endif
205 #if defined CONFIG_TCC_BACKTRACE && CONFIG_TCC_BACKTRACE==0
206 # undef CONFIG_TCC_BACKTRACE
207 #else
208 # define CONFIG_TCC_BACKTRACE 1 /* enable builtin stack backtraces */
209 #endif
211 #if defined CONFIG_TCC_BCHECK && CONFIG_TCC_BCHECK==0
212 # undef CONFIG_TCC_BCHECK
213 #else
214 # define CONFIG_TCC_BCHECK 1 /* enable bound checking code */
215 #endif
217 #if defined TARGETOS_OpenBSD \
218 || defined TARGETOS_FreeBSD \
219 || defined TARGETOS_NetBSD \
220 || defined TARGETOS_FreeBSD_kernel
221 # define TARGETOS_BSD 1
222 #elif !(defined TCC_TARGET_PE || defined TCC_TARGET_MACHO)
223 # define TARGETOS_Linux 1
224 #endif
226 #if defined TCC_TARGET_PE || defined TCC_TARGET_MACHO
227 # define ELF_OBJ_ONLY /* create elf .o but native executables */
228 #endif
230 /* No ten-byte long doubles on window and macos except in
231 cross-compilers made by a mingw-GCC */
232 #if defined TCC_TARGET_PE \
233 || (defined TCC_TARGET_MACHO && defined TCC_TARGET_ARM64) \
234 || (defined _WIN32 && !defined __GNUC__)
235 # define TCC_USING_DOUBLE_FOR_LDOUBLE 1
236 #endif
238 #ifdef CONFIG_TCC_PIE
239 # define CONFIG_TCC_PIC 1
240 #endif
242 /* ------------ path configuration ------------ */
244 #ifndef CONFIG_SYSROOT
245 # define CONFIG_SYSROOT ""
246 #endif
247 #if !defined CONFIG_TCCDIR && !defined _WIN32
248 # define CONFIG_TCCDIR "/usr/local/lib/tcc"
249 #endif
250 #ifndef CONFIG_LDDIR
251 # define CONFIG_LDDIR "lib"
252 #endif
253 #ifdef CONFIG_TRIPLET
254 # define USE_TRIPLET(s) s "/" CONFIG_TRIPLET
255 # define ALSO_TRIPLET(s) USE_TRIPLET(s) ":" s
256 #else
257 # define USE_TRIPLET(s) s
258 # define ALSO_TRIPLET(s) s
259 #endif
261 /* path to find crt1.o, crti.o and crtn.o */
262 #ifndef CONFIG_TCC_CRTPREFIX
263 # define CONFIG_TCC_CRTPREFIX USE_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
264 #endif
266 #ifndef CONFIG_USR_INCLUDE
267 # define CONFIG_USR_INCLUDE "/usr/include"
268 #endif
270 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
272 /* system include paths */
273 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
274 # if defined TCC_TARGET_PE || defined _WIN32
275 # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include"PATHSEP"{B}/include/winapi"
276 # else
277 # define CONFIG_TCC_SYSINCLUDEPATHS \
278 "{B}/include" \
279 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/include") \
280 ":" ALSO_TRIPLET(CONFIG_SYSROOT CONFIG_USR_INCLUDE)
281 # endif
282 #endif
284 /* library search paths */
285 #ifndef CONFIG_TCC_LIBPATHS
286 # ifdef TCC_TARGET_PE
287 # define CONFIG_TCC_LIBPATHS "{B}/lib"
288 # else
289 # define CONFIG_TCC_LIBPATHS \
290 "{B}" \
291 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
292 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
293 ":" ALSO_TRIPLET(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
294 # endif
295 #endif
297 /* name of ELF interpreter */
298 #ifndef CONFIG_TCC_ELFINTERP
299 # if TARGETOS_FreeBSD
300 # define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
301 # elif TARGETOS_FreeBSD_kernel
302 # if defined(TCC_TARGET_X86_64)
303 # define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
304 # else
305 # define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
306 # endif
307 # elif TARGETOS_DragonFly
308 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2"
309 # elif TARGETOS_NetBSD
310 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
311 # elif TARGETOS_OpenBSD
312 # define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.so"
313 # elif defined __GNU__
314 # define CONFIG_TCC_ELFINTERP "/lib/ld.so"
315 # elif defined(TCC_TARGET_PE)
316 # define CONFIG_TCC_ELFINTERP "-"
317 # elif defined(TCC_UCLIBC)
318 # define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */
319 # elif defined TCC_TARGET_ARM64
320 # if defined(TCC_MUSL)
321 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-aarch64.so.1"
322 # else
323 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
324 # endif
325 # elif defined(TCC_TARGET_X86_64)
326 # if defined(TCC_MUSL)
327 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-x86_64.so.1"
328 # else
329 # define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
330 # endif
331 # elif defined(TCC_TARGET_RISCV64)
332 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux-riscv64-lp64d.so.1"
333 # elif !defined(TCC_ARM_EABI)
334 # if defined(TCC_MUSL)
335 # if defined(TCC_TARGET_I386)
336 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-i386.so.1"
337 # else
338 # define CONFIG_TCC_ELFINTERP "/lib/ld-musl-arm.so.1"
339 # endif
340 # else
341 # define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
342 # endif
343 # endif
344 #endif
346 /* var elf_interp dans *-gen.c */
347 #ifdef CONFIG_TCC_ELFINTERP
348 # define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP
349 #else
350 # define DEFAULT_ELFINTERP(s) default_elfinterp(s)
351 #endif
353 /* (target specific) libtcc1.a */
354 #ifndef TCC_LIBTCC1
355 # define TCC_LIBTCC1 "libtcc1.a"
356 #endif
358 #ifndef CONFIG_TCC_CROSSPREFIX
359 # define CONFIG_TCC_CROSSPREFIX ""
360 #endif
362 /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
363 #if defined CONFIG_USE_LIBGCC && !defined TCC_LIBGCC
364 #define TCC_LIBGCC USE_TRIPLET(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
365 #endif
367 /* -------------------------------------------- */
369 #include "libtcc.h"
370 #include "elf.h"
371 #include "stab.h"
372 #include "dwarf.h"
374 /* -------------------------------------------- */
376 #ifndef PUB_FUNC /* functions used by tcc.c but not in libtcc.h */
377 # define PUB_FUNC
378 #endif
380 #ifndef ONE_SOURCE
381 # define ONE_SOURCE 1
382 #endif
384 /* support using libtcc from threads */
385 #ifndef CONFIG_TCC_SEMLOCK
386 # define CONFIG_TCC_SEMLOCK 1
387 #endif
389 #if ONE_SOURCE
390 #define ST_INLN static inline
391 #define ST_FUNC static
392 #define ST_DATA static
393 #else
394 #define ST_INLN
395 #define ST_FUNC
396 #define ST_DATA extern
397 #endif
399 #ifdef TCC_PROFILE /* profile all functions */
400 # define static
401 #endif
403 /* -------------------------------------------- */
404 /* include the target specific definitions */
406 #define TARGET_DEFS_ONLY
407 #ifdef TCC_TARGET_I386
408 # include "i386-gen.c"
409 # include "i386-link.c"
410 #elif defined TCC_TARGET_X86_64
411 # include "x86_64-gen.c"
412 # include "x86_64-link.c"
413 #elif defined TCC_TARGET_ARM
414 # include "arm-gen.c"
415 # include "arm-link.c"
416 # include "arm-asm.c"
417 #elif defined TCC_TARGET_ARM64
418 # include "arm64-gen.c"
419 # include "arm64-link.c"
420 # include "arm-asm.c"
421 #elif defined TCC_TARGET_C67
422 # define TCC_TARGET_COFF
423 # include "coff.h"
424 # include "c67-gen.c"
425 # include "c67-link.c"
426 #elif defined(TCC_TARGET_RISCV64)
427 # include "riscv64-gen.c"
428 # include "riscv64-link.c"
429 # include "riscv64-asm.c"
430 #else
431 #error unknown target
432 #endif
433 #undef TARGET_DEFS_ONLY
435 /* -------------------------------------------- */
437 #if PTR_SIZE == 8
438 # define ELFCLASSW ELFCLASS64
439 # define ElfW(type) Elf##64##_##type
440 # define ELFW(type) ELF##64##_##type
441 # define ElfW_Rel ElfW(Rela)
442 # define SHT_RELX SHT_RELA
443 # define REL_SECTION_FMT ".rela%s"
444 #else
445 # define ELFCLASSW ELFCLASS32
446 # define ElfW(type) Elf##32##_##type
447 # define ELFW(type) ELF##32##_##type
448 # define ElfW_Rel ElfW(Rel)
449 # define SHT_RELX SHT_REL
450 # define REL_SECTION_FMT ".rel%s"
451 #endif
452 /* target address type */
453 #define addr_t ElfW(Addr)
454 #define ElfSym ElfW(Sym)
456 #if PTR_SIZE == 8 && !defined TCC_TARGET_PE
457 # define LONG_SIZE 8
458 #else
459 # define LONG_SIZE 4
460 #endif
462 /* -------------------------------------------- */
464 #define INCLUDE_STACK_SIZE 32
465 #define IFDEF_STACK_SIZE 64
466 #define VSTACK_SIZE 256
467 #define STRING_MAX_SIZE 1024
468 #define TOKSTR_MAX_SIZE 256
469 #define PACK_STACK_SIZE 8
471 #define TOK_HASH_SIZE 16384 /* must be a power of two */
472 #define TOK_ALLOC_INCR 512 /* must be a power of two */
473 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
475 /* token symbol management */
476 typedef struct TokenSym {
477 struct TokenSym *hash_next;
478 struct Sym *sym_define; /* direct pointer to define */
479 struct Sym *sym_label; /* direct pointer to label */
480 struct Sym *sym_struct; /* direct pointer to structure */
481 struct Sym *sym_identifier; /* direct pointer to identifier */
482 int tok; /* token number */
483 int len;
484 char str[1];
485 } TokenSym;
487 #ifdef TCC_TARGET_PE
488 typedef unsigned short nwchar_t;
489 #else
490 typedef int nwchar_t;
491 #endif
493 typedef struct CString {
494 int size; /* size in bytes */
495 void *data; /* either 'char *' or 'nwchar_t *' */
496 int size_allocated;
497 } CString;
499 /* type definition */
500 typedef struct CType {
501 int t;
502 struct Sym *ref;
503 } CType;
505 /* constant value */
506 typedef union CValue {
507 long double ld;
508 double d;
509 float f;
510 uint64_t i;
511 struct {
512 const void *data;
513 int size;
514 } str;
515 int tab[LDOUBLE_SIZE/4];
516 } CValue;
518 /* value on stack */
519 typedef struct SValue {
520 CType type; /* type */
521 unsigned short r; /* register + flags */
522 unsigned short r2; /* second register, used for 'long long'
523 type. If not used, set to VT_CONST */
524 union {
525 struct { int jtrue, jfalse; }; /* forward jmps */
526 CValue c; /* constant, if VT_CONST */
528 union {
529 struct { unsigned short cmp_op, cmp_r; }; /* VT_CMP operation */
530 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST), or if */
531 }; /* result of unary() for an identifier. */
533 } SValue;
535 /* symbol attributes */
536 struct SymAttr {
537 unsigned short
538 aligned : 5, /* alignment as log2+1 (0 == unspecified) */
539 packed : 1,
540 weak : 1,
541 visibility : 2,
542 dllexport : 1,
543 nodecorate : 1,
544 dllimport : 1,
545 addrtaken : 1,
546 xxxx : 3; /* not used */
549 /* function attributes or temporary attributes for parsing */
550 struct FuncAttr {
551 unsigned
552 func_call : 3, /* calling convention (0..5), see below */
553 func_type : 2, /* FUNC_OLD/NEW/ELLIPSIS */
554 func_noreturn : 1, /* attribute((noreturn)) */
555 func_ctor : 1, /* attribute((constructor)) */
556 func_dtor : 1, /* attribute((destructor)) */
557 func_args : 8, /* PE __stdcall args */
558 func_alwinl : 1, /* always_inline */
559 xxxx : 15;
562 /* symbol management */
563 typedef struct Sym {
564 int v; /* symbol token */
565 unsigned short r; /* associated register or VT_CONST/VT_LOCAL and LVAL type */
566 struct SymAttr a; /* symbol attributes */
567 union {
568 struct {
569 int c; /* associated number or Elf symbol index */
570 union {
571 int sym_scope; /* scope level for locals */
572 int jnext; /* next jump label */
573 struct FuncAttr f; /* function attributes */
574 int auxtype; /* bitfield access type */
577 long long enum_val; /* enum constant if IS_ENUM_VAL */
578 int *d; /* define token stream */
579 struct Sym *ncl; /* next cleanup */
581 CType type; /* associated type */
582 union {
583 int *vla_array_str; /* vla array code */
584 struct Sym *next; /* next related symbol (for fields and anoms) */
585 struct Sym *cleanupstate; /* in defined labels */
586 int asm_label; /* associated asm label */
588 struct Sym *prev; /* prev symbol in stack */
589 struct Sym *prev_tok; /* previous symbol for this token */
590 } Sym;
592 /* section definition */
593 typedef struct Section {
594 unsigned long data_offset; /* current data offset */
595 unsigned char *data; /* section data */
596 unsigned long data_allocated; /* used for realloc() handling */
597 TCCState *s1;
598 int sh_name; /* elf section name (only used during output) */
599 int sh_num; /* elf section number */
600 int sh_type; /* elf section type */
601 int sh_flags; /* elf section flags */
602 int sh_info; /* elf section info */
603 int sh_addralign; /* elf section alignment */
604 int sh_entsize; /* elf entry size */
605 unsigned long sh_size; /* section size (only used during output) */
606 addr_t sh_addr; /* address at which the section is relocated */
607 unsigned long sh_offset; /* file offset */
608 int nb_hashed_syms; /* used to resize the hash table */
609 struct Section *link; /* link to another section */
610 struct Section *reloc; /* corresponding section for relocation, if any */
611 struct Section *hash; /* hash table for symbols */
612 struct Section *prev; /* previous section on section stack */
613 char name[1]; /* section name */
614 } Section;
616 typedef struct DLLReference {
617 int level;
618 void *handle;
619 unsigned char found, index;
620 char name[1];
621 } DLLReference;
623 /* -------------------------------------------------- */
625 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
626 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
627 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
629 /* stored in 'Sym->f.func_type' field */
630 #define FUNC_NEW 1 /* ansi function prototype */
631 #define FUNC_OLD 2 /* old function prototype */
632 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
634 /* stored in 'Sym->f.func_call' field */
635 #define FUNC_CDECL 0 /* standard c call */
636 #define FUNC_STDCALL 1 /* pascal c call */
637 #define FUNC_FASTCALL1 2 /* first param in %eax */
638 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
639 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
640 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
642 /* field 'Sym.t' for macros */
643 #define MACRO_OBJ 0 /* object like macro */
644 #define MACRO_FUNC 1 /* function like macro */
646 /* field 'Sym.r' for C labels */
647 #define LABEL_DEFINED 0 /* label is defined */
648 #define LABEL_FORWARD 1 /* label is forward defined */
649 #define LABEL_DECLARED 2 /* label is declared but never used */
650 #define LABEL_GONE 3 /* label isn't in scope, but not yet popped
651 from local_label_stack (stmt exprs) */
653 /* type_decl() types */
654 #define TYPE_ABSTRACT 1 /* type without variable */
655 #define TYPE_DIRECT 2 /* type with variable */
656 #define TYPE_PARAM 4 /* type declares function parameter */
657 #define TYPE_NEST 8 /* nested call to post_type */
659 #define IO_BUF_SIZE 8192
661 typedef struct BufferedFile {
662 uint8_t *buf_ptr;
663 uint8_t *buf_end;
664 int fd;
665 struct BufferedFile *prev;
666 int line_num; /* current line number - here to simplify code */
667 int line_ref; /* tcc -E: last printed line */
668 int ifndef_macro; /* #ifndef macro / #endif search */
669 int ifndef_macro_saved; /* saved ifndef_macro */
670 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
671 int include_next_index; /* next search path */
672 char filename[1024]; /* filename */
673 char *true_filename; /* filename not modified by # line directive */
674 unsigned char unget[4];
675 unsigned char buffer[1]; /* extra size for CH_EOB char */
676 } BufferedFile;
678 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
679 #define CH_EOF (-1) /* end of file */
681 /* used to record tokens */
682 typedef struct TokenString {
683 int *str;
684 int len;
685 int lastlen;
686 int allocated_len;
687 int last_line_num;
688 int save_line_num;
689 /* used to chain token-strings with begin/end_macro() */
690 struct TokenString *prev;
691 const int *prev_ptr;
692 char alloc;
693 } TokenString;
695 /* GNUC attribute definition */
696 typedef struct AttributeDef {
697 struct SymAttr a;
698 struct FuncAttr f;
699 struct Section *section;
700 Sym *cleanup_func;
701 int alias_target; /* token */
702 int asm_label; /* associated asm label */
703 char attr_mode; /* __attribute__((__mode__(...))) */
704 } AttributeDef;
706 /* inline functions */
707 typedef struct InlineFunc {
708 TokenString *func_str;
709 Sym *sym;
710 char filename[1];
711 } InlineFunc;
713 /* include file cache, used to find files faster and also to eliminate
714 inclusion if the include file is protected by #ifndef ... #endif */
715 typedef struct CachedInclude {
716 int ifndef_macro;
717 int once;
718 int hash_next; /* -1 if none */
719 char filename[1]; /* path specified in #include */
720 } CachedInclude;
722 #define CACHED_INCLUDES_HASH_SIZE 32
724 #ifdef CONFIG_TCC_ASM
725 typedef struct ExprValue {
726 uint64_t v;
727 Sym *sym;
728 int pcrel;
729 } ExprValue;
731 #define MAX_ASM_OPERANDS 30
732 typedef struct ASMOperand {
733 int id; /* GCC 3 optional identifier (0 if number only supported) */
734 char *constraint;
735 char asm_str[16]; /* computed asm string for operand */
736 SValue *vt; /* C value of the expression */
737 int ref_index; /* if >= 0, gives reference to a output constraint */
738 int input_index; /* if >= 0, gives reference to an input constraint */
739 int priority; /* priority, used to assign registers */
740 int reg; /* if >= 0, register number used for this operand */
741 int is_llong; /* true if double register value */
742 int is_memory; /* true if memory operand */
743 int is_rw; /* for '+' modifier */
744 int is_label; /* for asm goto */
745 } ASMOperand;
746 #endif
748 /* extra symbol attributes (not in symbol table) */
749 struct sym_attr {
750 unsigned got_offset;
751 unsigned plt_offset;
752 int plt_sym;
753 int dyn_index;
754 #ifdef TCC_TARGET_ARM
755 unsigned char plt_thumb_stub:1;
756 #endif
759 struct TCCState {
760 unsigned char verbose; /* if true, display some information during compilation */
761 unsigned char nostdinc; /* if true, no standard headers are added */
762 unsigned char nostdlib; /* if true, no standard libraries are added */
763 unsigned char nocommon; /* if true, do not use common symbols for .bss data */
764 unsigned char static_link; /* if true, static linking is performed */
765 unsigned char rdynamic; /* if true, all symbols are exported */
766 unsigned char symbolic; /* if true, resolve symbols in the current module first */
767 unsigned char filetype; /* file type for compilation (NONE,C,ASM) */
768 unsigned char optimize; /* only to #define __OPTIMIZE__ */
769 unsigned char option_pthread; /* -pthread option */
770 unsigned char enable_new_dtags; /* -Wl,--enable-new-dtags */
771 unsigned int cversion; /* supported C ISO version, 199901 (the default), 201112, ... */
773 /* C language options */
774 unsigned char char_is_unsigned;
775 unsigned char leading_underscore;
776 unsigned char ms_extensions; /* allow nested named struct w/o identifier behave like unnamed */
777 unsigned char dollars_in_identifiers; /* allows '$' char in identifiers */
778 unsigned char ms_bitfields; /* if true, emulate MS algorithm for aligning bitfields */
780 /* warning switches */
781 unsigned char warn_none;
782 unsigned char warn_all;
783 unsigned char warn_error;
784 unsigned char warn_write_strings;
785 unsigned char warn_unsupported;
786 unsigned char warn_implicit_function_declaration;
787 unsigned char warn_discarded_qualifiers;
788 #define WARN_ON 1 /* warning is on (-Woption) */
789 unsigned char warn_num; /* temp var for tcc_warning_c() */
791 unsigned char option_r; /* option -r */
792 unsigned char do_bench; /* option -bench */
793 unsigned char just_deps; /* option -M */
794 unsigned char gen_deps; /* option -MD */
795 unsigned char include_sys_deps; /* option -MD */
797 /* compile with debug symbol (and use them if error during execution) */
798 unsigned char do_debug;
799 unsigned char dwarf;
800 unsigned char do_backtrace;
801 #ifdef CONFIG_TCC_BCHECK
802 /* compile with built-in memory and bounds checker */
803 unsigned char do_bounds_check;
804 #endif
805 unsigned char test_coverage; /* generate test coverage code */
807 /* use GNU C extensions */
808 unsigned char gnu_ext;
809 /* use TinyCC extensions */
810 unsigned char tcc_ext;
812 unsigned char dflag; /* -dX value */
813 unsigned char Pflag; /* -P switch (LINE_MACRO_OUTPUT_FORMAT) */
815 #ifdef TCC_TARGET_X86_64
816 unsigned char nosse; /* For -mno-sse support. */
817 #endif
818 #ifdef TCC_TARGET_ARM
819 unsigned char float_abi; /* float ABI of the generated code*/
820 #endif
822 unsigned char has_text_addr;
823 addr_t text_addr; /* address of text section */
824 unsigned section_align; /* section alignment */
825 #ifdef TCC_TARGET_I386
826 int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
827 #endif
829 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
830 char *soname; /* as specified on the command line (-soname) */
831 char *rpath; /* as specified on the command line (-Wl,-rpath=) */
832 char *elf_entryname; /* "_start" unless set */
833 char *init_symbol; /* symbols to call at load-time (not used currently) */
834 char *fini_symbol; /* symbols to call at unload-time (not used currently) */
835 char *mapfile; /* create a mapfile (not used currently) */
837 /* output type, see TCC_OUTPUT_XXX */
838 int output_type;
839 /* output format, see TCC_OUTPUT_FORMAT_xxx */
840 int output_format;
841 /* nth test to run with -dt -run */
842 int run_test;
844 /* array of all loaded dlls (including those referenced by loaded dlls) */
845 DLLReference **loaded_dlls;
846 int nb_loaded_dlls;
848 /* include paths */
849 char **include_paths;
850 int nb_include_paths;
852 char **sysinclude_paths;
853 int nb_sysinclude_paths;
855 /* library paths */
856 char **library_paths;
857 int nb_library_paths;
859 /* crt?.o object path */
860 char **crt_paths;
861 int nb_crt_paths;
863 /* -D / -U options */
864 CString cmdline_defs;
865 /* -include options */
866 CString cmdline_incl;
868 /* error handling */
869 void *error_opaque;
870 void (*error_func)(void *opaque, const char *msg);
871 int error_set_jmp_enabled;
872 jmp_buf error_jmp_buf;
873 int nb_errors;
875 /* output file for preprocessing (-E) */
876 FILE *ppfp;
878 /* for -MD/-MF: collected dependencies for this compilation */
879 char **target_deps;
880 int nb_target_deps;
882 /* compilation */
883 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
884 BufferedFile **include_stack_ptr;
886 int ifdef_stack[IFDEF_STACK_SIZE];
887 int *ifdef_stack_ptr;
889 /* included files enclosed with #ifndef MACRO */
890 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
891 CachedInclude **cached_includes;
892 int nb_cached_includes;
894 /* #pragma pack stack */
895 int pack_stack[PACK_STACK_SIZE];
896 int *pack_stack_ptr;
897 char **pragma_libs;
898 int nb_pragma_libs;
900 /* inline functions are stored as token lists and compiled last
901 only if referenced */
902 struct InlineFunc **inline_fns;
903 int nb_inline_fns;
905 /* sections */
906 Section **sections;
907 int nb_sections; /* number of sections, including first dummy section */
909 Section **priv_sections;
910 int nb_priv_sections; /* number of private sections */
912 /* predefined sections */
913 Section *text_section, *data_section, *rodata_section, *bss_section;
914 Section *common_section;
915 Section *cur_text_section; /* current section where function code is generated */
916 #ifdef CONFIG_TCC_BCHECK
917 /* bound check related sections */
918 Section *bounds_section; /* contains global data bound description */
919 Section *lbounds_section; /* contains local data bound description */
920 #endif
921 /* symbol section */
922 Section *symtab_section;
923 /* temporary dynamic symbol sections (for dll loading) */
924 Section *dynsymtab_section;
925 /* exported dynamic symbol section */
926 Section *dynsym;
927 /* copy of the global symtab_section variable */
928 Section *symtab;
929 /* got & plt handling */
930 Section *got, *plt;
931 /* debug sections */
932 Section *stab_section;
933 Section *dwarf_info_section;
934 Section *dwarf_abbrev_section;
935 Section *dwarf_line_section;
936 Section *dwarf_aranges_section;
937 Section *dwarf_str_section;
938 Section *dwarf_line_str_section;
939 int dwlo, dwhi; /* dwarf section range */
940 /* test coverage */
941 Section *tcov_section;
942 /* debug state */
943 struct _tccdbg *dState;
945 /* Is there a new undefined sym since last new_undef_sym() */
946 int new_undef_sym;
947 /* extra attributes (eg. GOT/PLT value) for symtab symbols */
948 struct sym_attr *sym_attrs;
949 int nb_sym_attrs;
950 /* ptr to next reloc entry reused */
951 ElfW_Rel *qrel;
952 #define qrel s1->qrel
954 #ifdef TCC_TARGET_RISCV64
955 struct pcrel_hi { addr_t addr, val; } last_hi;
956 #define last_hi s1->last_hi
957 #endif
959 #ifdef TCC_TARGET_PE
960 /* PE info */
961 int pe_subsystem;
962 unsigned pe_characteristics;
963 unsigned pe_file_align;
964 unsigned pe_stack_size;
965 addr_t pe_imagebase;
966 # ifdef TCC_TARGET_X86_64
967 Section *uw_pdata;
968 int uw_sym;
969 unsigned uw_offs;
970 # endif
971 #endif
973 #ifndef ELF_OBJ_ONLY
974 int nb_sym_versions;
975 struct sym_version *sym_versions;
976 int nb_sym_to_version;
977 int *sym_to_version;
978 int dt_verneednum;
979 Section *versym_section;
980 Section *verneed_section;
981 #endif
983 #ifdef TCC_IS_NATIVE
984 const char *runtime_main;
985 void **runtime_mem;
986 int nb_runtime_mem;
987 #endif
989 #ifdef CONFIG_TCC_BACKTRACE
990 int rt_num_callers;
991 #endif
993 /* benchmark info */
994 int total_idents;
995 int total_lines;
996 int total_bytes;
997 int total_output[4];
999 /* option -dnum (for general development purposes) */
1000 int g_debug;
1002 /* used by tcc_load_ldscript */
1003 int fd, cc;
1005 /* for warnings/errors for object files */
1006 const char *current_filename;
1008 /* used by main and tcc_parse_args only */
1009 struct filespec **files; /* files seen on command line */
1010 int nb_files; /* number thereof */
1011 int nb_libraries; /* number of libs thereof */
1012 char *outfile; /* output filename */
1013 char *deps_outfile; /* option -MF */
1014 int argc;
1015 char **argv;
1018 struct filespec {
1019 char type;
1020 char name[1];
1023 /* The current value can be: */
1024 #define VT_VALMASK 0x003f /* mask for value location, register or: */
1025 #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
1026 #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
1027 #define VT_LOCAL 0x0032 /* offset on stack */
1028 #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
1029 #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
1030 #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
1031 #define VT_LVAL 0x0100 /* var is an lvalue */
1032 #define VT_SYM 0x0200 /* a symbol value is added */
1033 #define VT_MUSTCAST 0x0C00 /* value must be casted to be correct (used for
1034 char/short stored in integer registers) */
1035 #define VT_NONCONST 0x1000 /* VT_CONST, but not an (C standard) integer
1036 constant expression */
1037 #define VT_MUSTBOUND 0x4000 /* bound checking must be done before
1038 dereferencing value */
1039 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
1040 bounding function call point is in vc */
1041 /* types */
1042 #define VT_BTYPE 0x000f /* mask for basic type */
1043 #define VT_VOID 0 /* void type */
1044 #define VT_BYTE 1 /* signed byte type */
1045 #define VT_SHORT 2 /* short type */
1046 #define VT_INT 3 /* integer type */
1047 #define VT_LLONG 4 /* 64 bit integer */
1048 #define VT_PTR 5 /* pointer */
1049 #define VT_FUNC 6 /* function type */
1050 #define VT_STRUCT 7 /* struct/union definition */
1051 #define VT_FLOAT 8 /* IEEE float */
1052 #define VT_DOUBLE 9 /* IEEE double */
1053 #define VT_LDOUBLE 10 /* IEEE long double */
1054 #define VT_BOOL 11 /* ISOC99 boolean type */
1055 #define VT_QLONG 13 /* 128-bit integer. Only used for x86-64 ABI */
1056 #define VT_QFLOAT 14 /* 128-bit float. Only used for x86-64 ABI */
1058 #define VT_UNSIGNED 0x0010 /* unsigned type */
1059 #define VT_DEFSIGN 0x0020 /* explicitly signed or unsigned */
1060 #define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */
1061 #define VT_BITFIELD 0x0080 /* bitfield modifier */
1062 #define VT_CONSTANT 0x0100 /* const modifier */
1063 #define VT_VOLATILE 0x0200 /* volatile modifier */
1064 #define VT_VLA 0x0400 /* VLA type (also has VT_PTR and VT_ARRAY) */
1065 #define VT_LONG 0x0800 /* long type (also has VT_INT rsp. VT_LLONG) */
1067 /* storage */
1068 #define VT_EXTERN 0x00001000 /* extern definition */
1069 #define VT_STATIC 0x00002000 /* static variable */
1070 #define VT_TYPEDEF 0x00004000 /* typedef definition */
1071 #define VT_INLINE 0x00008000 /* inline definition */
1072 /* currently unused: 0x000[1248]0000 */
1074 #define VT_STRUCT_SHIFT 20 /* shift for bitfield shift values (32 - 2*6) */
1075 #define VT_STRUCT_MASK (((1U << (6+6)) - 1) << VT_STRUCT_SHIFT | VT_BITFIELD)
1076 #define BIT_POS(t) (((t) >> VT_STRUCT_SHIFT) & 0x3f)
1077 #define BIT_SIZE(t) (((t) >> (VT_STRUCT_SHIFT + 6)) & 0x3f)
1079 #define VT_UNION (1 << VT_STRUCT_SHIFT | VT_STRUCT)
1080 #define VT_ENUM (2 << VT_STRUCT_SHIFT) /* integral type is an enum really */
1081 #define VT_ENUM_VAL (3 << VT_STRUCT_SHIFT) /* integral type is an enum constant really */
1083 #define IS_ENUM(t) ((t & VT_STRUCT_MASK) == VT_ENUM)
1084 #define IS_ENUM_VAL(t) ((t & VT_STRUCT_MASK) == VT_ENUM_VAL)
1085 #define IS_UNION(t) ((t & (VT_STRUCT_MASK|VT_BTYPE)) == VT_UNION)
1087 #define VT_ATOMIC VT_VOLATILE
1089 /* type mask (except storage) */
1090 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
1091 #define VT_TYPE (~(VT_STORAGE|VT_STRUCT_MASK))
1093 /* symbol was created by tccasm.c first */
1094 #define VT_ASM (VT_VOID | 1 << VT_STRUCT_SHIFT)
1095 #define VT_ASM_FUNC (VT_ASM | 2 << VT_STRUCT_SHIFT)
1096 #define IS_ASM_SYM(sym) (((sym)->type.t & (VT_BTYPE | VT_ASM)) == VT_ASM)
1098 /* general: set/get the pseudo-bitfield value for bit-mask M */
1099 #define BFVAL(M,N) ((unsigned)((M) & ~((M) << 1)) * (N))
1100 #define BFGET(X,M) (((X) & (M)) / BFVAL(M,1))
1101 #define BFSET(X,M,N) ((X) = ((X) & ~(M)) | BFVAL(M,N))
1103 /* token values */
1105 /* conditional ops */
1106 #define TOK_LAND 0x90
1107 #define TOK_LOR 0x91
1108 /* warning: the following compare tokens depend on i386 asm code */
1109 #define TOK_ULT 0x92
1110 #define TOK_UGE 0x93
1111 #define TOK_EQ 0x94
1112 #define TOK_NE 0x95
1113 #define TOK_ULE 0x96
1114 #define TOK_UGT 0x97
1115 #define TOK_Nset 0x98
1116 #define TOK_Nclear 0x99
1117 #define TOK_LT 0x9c
1118 #define TOK_GE 0x9d
1119 #define TOK_LE 0x9e
1120 #define TOK_GT 0x9f
1122 #define TOK_ISCOND(t) (t >= TOK_LAND && t <= TOK_GT)
1124 #define TOK_DEC 0x80 /* -- */
1125 #define TOK_MID 0x81 /* inc/dec, to void constant */
1126 #define TOK_INC 0x82 /* ++ */
1127 #define TOK_UDIV 0x83 /* unsigned division */
1128 #define TOK_UMOD 0x84 /* unsigned modulo */
1129 #define TOK_PDIV 0x85 /* fast division with undefined rounding for pointers */
1130 #define TOK_UMULL 0x86 /* unsigned 32x32 -> 64 mul */
1131 #define TOK_ADDC1 0x87 /* add with carry generation */
1132 #define TOK_ADDC2 0x88 /* add with carry use */
1133 #define TOK_SUBC1 0x89 /* add with carry generation */
1134 #define TOK_SUBC2 0x8a /* add with carry use */
1135 #define TOK_SHL '<' /* shift left */
1136 #define TOK_SAR '>' /* signed shift right */
1137 #define TOK_SHR 0x8b /* unsigned shift right */
1138 #define TOK_NEG TOK_MID /* unary minus operation (for floats) */
1140 #define TOK_ARROW 0xa0 /* -> */
1141 #define TOK_DOTS 0xa1 /* three dots */
1142 #define TOK_TWODOTS 0xa2 /* C++ token ? */
1143 #define TOK_TWOSHARPS 0xa3 /* ## preprocessing token */
1144 #define TOK_PLCHLDR 0xa4 /* placeholder token as defined in C99 */
1145 #define TOK_NOSUBST 0xa5 /* means following token has already been pp'd */
1146 #define TOK_PPJOIN 0xa6 /* A '##' in the right position to mean pasting */
1148 /* assignment operators */
1149 #define TOK_A_ADD 0xb0
1150 #define TOK_A_SUB 0xb1
1151 #define TOK_A_MUL 0xb2
1152 #define TOK_A_DIV 0xb3
1153 #define TOK_A_MOD 0xb4
1154 #define TOK_A_AND 0xb5
1155 #define TOK_A_OR 0xb6
1156 #define TOK_A_XOR 0xb7
1157 #define TOK_A_SHL 0xb8
1158 #define TOK_A_SAR 0xb9
1160 #define TOK_ASSIGN(t) (t >= TOK_A_ADD && t <= TOK_A_SAR)
1161 #define TOK_ASSIGN_OP(t) ("+-*/%&|^<>"[t - TOK_A_ADD])
1163 /* tokens that carry values (in additional token string space / tokc) --> */
1164 #define TOK_CCHAR 0xc0 /* char constant in tokc */
1165 #define TOK_LCHAR 0xc1
1166 #define TOK_CINT 0xc2 /* number in tokc */
1167 #define TOK_CUINT 0xc3 /* unsigned int constant */
1168 #define TOK_CLLONG 0xc4 /* long long constant */
1169 #define TOK_CULLONG 0xc5 /* unsigned long long constant */
1170 #define TOK_CLONG 0xc6 /* long constant */
1171 #define TOK_CULONG 0xc7 /* unsigned long constant */
1172 #define TOK_STR 0xc8 /* pointer to string in tokc */
1173 #define TOK_LSTR 0xc9
1174 #define TOK_CFLOAT 0xca /* float constant */
1175 #define TOK_CDOUBLE 0xcb /* double constant */
1176 #define TOK_CLDOUBLE 0xcc /* long double constant */
1177 #define TOK_PPNUM 0xcd /* preprocessor number */
1178 #define TOK_PPSTR 0xce /* preprocessor string */
1179 #define TOK_LINENUM 0xcf /* line number info */
1181 #define TOK_HAS_VALUE(t) (t >= TOK_CCHAR && t <= TOK_LINENUM)
1183 #define TOK_EOF (-1) /* end of file */
1184 #define TOK_LINEFEED 10 /* line feed */
1186 /* all identifiers and strings have token above that */
1187 #define TOK_IDENT 256
1189 enum tcc_token {
1190 TOK_LAST = TOK_IDENT - 1
1191 #define DEF(id, str) ,id
1192 #include "tcctok.h"
1193 #undef DEF
1196 /* keywords: tok >= TOK_IDENT && tok < TOK_UIDENT */
1197 #define TOK_UIDENT TOK_DEFINE
1199 /* ------------ libtcc.c ------------ */
1201 ST_DATA struct TCCState *tcc_state;
1203 /* public functions currently used by the tcc main function */
1204 ST_FUNC char *pstrcpy(char *buf, size_t buf_size, const char *s);
1205 ST_FUNC char *pstrcat(char *buf, size_t buf_size, const char *s);
1206 ST_FUNC char *pstrncpy(char *out, const char *in, size_t num);
1207 PUB_FUNC char *tcc_basename(const char *name);
1208 PUB_FUNC char *tcc_fileextension (const char *name);
1210 #ifndef MEM_DEBUG
1211 PUB_FUNC void tcc_free(void *ptr);
1212 PUB_FUNC void *tcc_malloc(unsigned long size);
1213 PUB_FUNC void *tcc_mallocz(unsigned long size);
1214 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
1215 PUB_FUNC char *tcc_strdup(const char *str);
1216 #else
1217 #define tcc_free(ptr) tcc_free_debug(ptr)
1218 #define tcc_malloc(size) tcc_malloc_debug(size, __FILE__, __LINE__)
1219 #define tcc_mallocz(size) tcc_mallocz_debug(size, __FILE__, __LINE__)
1220 #define tcc_realloc(ptr,size) tcc_realloc_debug(ptr, size, __FILE__, __LINE__)
1221 #define tcc_strdup(str) tcc_strdup_debug(str, __FILE__, __LINE__)
1222 PUB_FUNC void tcc_free_debug(void *ptr);
1223 PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line);
1224 PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line);
1225 PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line);
1226 PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line);
1227 #endif
1229 #define free(p) use_tcc_free(p)
1230 #define malloc(s) use_tcc_malloc(s)
1231 #define realloc(p, s) use_tcc_realloc(p, s)
1232 #undef strdup
1233 #define strdup(s) use_tcc_strdup(s)
1234 PUB_FUNC void _tcc_error_noabort(const char *fmt, ...) PRINTF_LIKE(1,2);
1235 PUB_FUNC NORETURN void _tcc_error(const char *fmt, ...) PRINTF_LIKE(1,2);
1236 PUB_FUNC void _tcc_warning(const char *fmt, ...) PRINTF_LIKE(1,2);
1237 #define tcc_internal_error(msg) tcc_error("internal compiler error\n"\
1238 "%s:%d: in %s(): " msg, __FILE__,__LINE__,__FUNCTION__)
1240 /* other utilities */
1241 ST_FUNC void dynarray_add(void *ptab, int *nb_ptr, void *data);
1242 ST_FUNC void dynarray_reset(void *pp, int *n);
1243 ST_INLN void cstr_ccat(CString *cstr, int ch);
1244 ST_FUNC void cstr_cat(CString *cstr, const char *str, int len);
1245 ST_FUNC void cstr_wccat(CString *cstr, int ch);
1246 ST_FUNC void cstr_new(CString *cstr);
1247 ST_FUNC void cstr_free(CString *cstr);
1248 ST_FUNC int cstr_printf(CString *cs, const char *fmt, ...) PRINTF_LIKE(2,3);
1249 ST_FUNC int cstr_vprintf(CString *cstr, const char *fmt, va_list ap);
1250 ST_FUNC void cstr_reset(CString *cstr);
1252 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
1253 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
1254 ST_FUNC void tcc_close(void);
1256 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
1257 /* flags: */
1258 #define AFF_PRINT_ERROR 0x10 /* print error if file not found */
1259 #define AFF_REFERENCED_DLL 0x20 /* load a referenced dll from another dll */
1260 #define AFF_TYPE_BIN 0x40 /* file to add is binary */
1261 #define AFF_WHOLE_ARCHIVE 0x80 /* load all objects from archive */
1262 /* s->filetype: */
1263 #define AFF_TYPE_NONE 0
1264 #define AFF_TYPE_C 1
1265 #define AFF_TYPE_ASM 2
1266 #define AFF_TYPE_ASMPP 4
1267 #define AFF_TYPE_LIB 8
1268 #define AFF_TYPE_MASK (15 | AFF_TYPE_BIN)
1269 /* values from tcc_object_type(...) */
1270 #define AFF_BINTYPE_REL 1
1271 #define AFF_BINTYPE_DYN 2
1272 #define AFF_BINTYPE_AR 3
1273 #define AFF_BINTYPE_C67 4
1275 #ifndef ELF_OBJ_ONLY
1276 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
1277 #endif
1278 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
1279 ST_FUNC void tcc_add_support(TCCState *s1, const char *filename);
1280 #ifdef CONFIG_TCC_BCHECK
1281 ST_FUNC void tcc_add_bcheck(TCCState *s1);
1282 #endif
1283 #ifdef CONFIG_TCC_BACKTRACE
1284 ST_FUNC void tcc_add_btstub(TCCState *s1);
1285 #endif
1286 ST_FUNC void tcc_add_pragma_libs(TCCState *s1);
1287 PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f);
1288 PUB_FUNC void tcc_print_stats(TCCState *s, unsigned total_time);
1289 PUB_FUNC int tcc_parse_args(TCCState *s, int *argc, char ***argv, int optind);
1290 #ifdef _WIN32
1291 ST_FUNC char *normalize_slashes(char *path);
1292 #endif
1293 ST_FUNC DLLReference *tcc_add_dllref(TCCState *s1, const char *dllname, int level);
1294 ST_FUNC char *tcc_load_text(int fd);
1296 /* tcc_parse_args return codes: */
1297 #define OPT_HELP 1
1298 #define OPT_HELP2 2
1299 #define OPT_V 3
1300 #define OPT_PRINT_DIRS 4
1301 #define OPT_AR 5
1302 #define OPT_IMPDEF 6
1303 #define OPT_M32 32
1304 #define OPT_M64 64
1306 /* ------------ tccpp.c ------------ */
1308 ST_DATA struct BufferedFile *file;
1309 ST_DATA int tok;
1310 ST_DATA CValue tokc;
1311 ST_DATA const int *macro_ptr;
1312 ST_DATA int parse_flags;
1313 ST_DATA int tok_flags;
1314 ST_DATA CString tokcstr; /* current parsed string, if any */
1316 /* display benchmark infos */
1317 ST_DATA int tok_ident;
1318 ST_DATA TokenSym **table_ident;
1320 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
1321 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
1322 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
1323 #define TOK_FLAG_EOF 0x0008 /* end of file */
1325 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
1326 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
1327 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
1328 token. line feed is also
1329 returned at eof */
1330 #define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */
1331 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
1332 #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
1333 #define PARSE_FLAG_TOK_STR 0x0040 /* return parsed strings instead of TOK_PPSTR */
1335 /* isidnum_table flags: */
1336 #define IS_SPC 1
1337 #define IS_ID 2
1338 #define IS_NUM 4
1340 enum line_macro_output_format {
1341 LINE_MACRO_OUTPUT_FORMAT_GCC,
1342 LINE_MACRO_OUTPUT_FORMAT_NONE,
1343 LINE_MACRO_OUTPUT_FORMAT_STD,
1344 LINE_MACRO_OUTPUT_FORMAT_P10 = 11
1347 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
1348 ST_FUNC int tok_alloc_const(const char *str);
1349 ST_FUNC const char *get_tok_str(int v, CValue *cv);
1350 ST_FUNC void begin_macro(TokenString *str, int alloc);
1351 ST_FUNC void end_macro(void);
1352 ST_FUNC int set_idnum(int c, int val);
1353 ST_INLN void tok_str_new(TokenString *s);
1354 ST_FUNC TokenString *tok_str_alloc(void);
1355 ST_FUNC void tok_str_free(TokenString *s);
1356 ST_FUNC void tok_str_free_str(int *str);
1357 ST_FUNC void tok_str_add(TokenString *s, int t);
1358 ST_FUNC void tok_str_add_tok(TokenString *s);
1359 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
1360 ST_FUNC void define_undef(Sym *s);
1361 ST_INLN Sym *define_find(int v);
1362 ST_FUNC void free_defines(Sym *b);
1363 ST_FUNC Sym *label_find(int v);
1364 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
1365 ST_FUNC void label_pop(Sym **ptop, Sym *slast, int keep);
1366 ST_FUNC void parse_define(void);
1367 ST_FUNC void preprocess(int is_bof);
1368 ST_FUNC void next(void);
1369 ST_INLN void unget_tok(int last_tok);
1370 ST_FUNC void preprocess_start(TCCState *s1, int filetype);
1371 ST_FUNC void preprocess_end(TCCState *s1);
1372 ST_FUNC void tccpp_new(TCCState *s);
1373 ST_FUNC void tccpp_delete(TCCState *s);
1374 ST_FUNC int tcc_preprocess(TCCState *s1);
1375 ST_FUNC void skip(int c);
1376 ST_FUNC NORETURN void expect(const char *msg);
1378 /* space excluding newline */
1379 static inline int is_space(int ch) {
1380 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1382 static inline int isid(int c) {
1383 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
1385 static inline int isnum(int c) {
1386 return c >= '0' && c <= '9';
1388 static inline int isoct(int c) {
1389 return c >= '0' && c <= '7';
1391 static inline int toup(int c) {
1392 return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
1395 /* ------------ tccgen.c ------------ */
1397 #define SYM_POOL_NB (8192 / sizeof(Sym))
1399 ST_DATA Sym *global_stack;
1400 ST_DATA Sym *local_stack;
1401 ST_DATA Sym *local_label_stack;
1402 ST_DATA Sym *global_label_stack;
1403 ST_DATA Sym *define_stack;
1404 ST_DATA CType int_type, func_old_type, char_pointer_type;
1405 ST_DATA SValue *vtop;
1406 ST_DATA int rsym, anon_sym, ind, loc;
1407 ST_DATA char debug_modes;
1409 ST_DATA int const_wanted; /* true if constant wanted */
1410 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
1411 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
1412 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
1413 ST_DATA int func_var; /* true if current function is variadic */
1414 ST_DATA int func_vc;
1415 ST_DATA int func_ind;
1416 ST_DATA const char *funcname;
1418 ST_FUNC void tccgen_init(TCCState *s1);
1419 ST_FUNC int tccgen_compile(TCCState *s1);
1420 ST_FUNC void tccgen_finish(TCCState *s1);
1421 ST_FUNC void check_vstack(void);
1423 ST_INLN int is_float(int t);
1424 ST_FUNC int ieee_finite(double d);
1425 ST_FUNC int exact_log2p1(int i);
1426 ST_FUNC void test_lvalue(void);
1428 ST_FUNC ElfSym *elfsym(Sym *);
1429 ST_FUNC void update_storage(Sym *sym);
1430 ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, addr_t value, unsigned long size, int can_add_underscore);
1431 ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
1432 #if PTR_SIZE == 4
1433 ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
1434 #endif
1435 ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, addr_t addend);
1437 ST_INLN void sym_free(Sym *sym);
1438 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
1439 ST_FUNC void sym_pop(Sym **ptop, Sym *b, int keep);
1440 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, int c);
1441 ST_FUNC Sym *sym_find2(Sym *s, int v);
1442 ST_INLN Sym *sym_find(int v);
1443 ST_INLN Sym *struct_find(int v);
1445 ST_FUNC Sym *global_identifier_push(int v, int t, int c);
1446 ST_FUNC Sym *external_global_sym(int v, CType *type);
1447 ST_FUNC Sym *external_helper_sym(int v);
1448 ST_FUNC void vpush_helper_func(int v);
1449 ST_FUNC void vset(CType *type, int r, int v);
1450 ST_FUNC void vset_VT_CMP(int op);
1451 ST_FUNC void vpushi(int v);
1452 ST_FUNC void vpushv(SValue *v);
1453 ST_FUNC void vpushsym(CType *type, Sym *sym);
1454 ST_FUNC void vswap(void);
1455 ST_FUNC void vrote(SValue *e, int n);
1456 ST_FUNC void vrott(int n);
1457 ST_FUNC void vrotb(int n);
1458 ST_FUNC void vpop(void);
1459 #if PTR_SIZE == 4
1460 ST_FUNC void lexpand(void);
1461 #endif
1462 #ifdef TCC_TARGET_ARM
1463 ST_FUNC int get_reg_ex(int rc, int rc2);
1464 #endif
1465 ST_FUNC void save_reg(int r);
1466 ST_FUNC void save_reg_upstack(int r, int n);
1467 ST_FUNC int get_reg(int rc);
1468 ST_FUNC void save_regs(int n);
1469 ST_FUNC void gaddrof(void);
1470 ST_FUNC int gv(int rc);
1471 ST_FUNC void gv2(int rc1, int rc2);
1472 ST_FUNC void gen_op(int op);
1473 ST_FUNC int type_size(CType *type, int *a);
1474 ST_FUNC void mk_pointer(CType *type);
1475 ST_FUNC void vstore(void);
1476 ST_FUNC void inc(int post, int c);
1477 ST_FUNC void parse_mult_str (CString *astr, const char *msg);
1478 ST_FUNC void parse_asm_str(CString *astr);
1479 ST_FUNC void indir(void);
1480 ST_FUNC void unary(void);
1481 ST_FUNC void gexpr(void);
1482 ST_FUNC int expr_const(void);
1483 #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
1484 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
1485 #endif
1486 #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
1487 ST_FUNC int classify_x86_64_va_arg(CType *ty);
1488 #endif
1489 #ifdef CONFIG_TCC_BCHECK
1490 ST_FUNC void gbound_args(int nb_args);
1491 ST_DATA int func_bound_add_epilog;
1492 #endif
1494 /* ------------ tccelf.c ------------ */
1496 #define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
1497 #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
1498 #define TCC_OUTPUT_FORMAT_COFF 2 /* COFF */
1499 #define TCC_OUTPUT_DYN TCC_OUTPUT_DLL
1501 #define ARMAG "!<arch>\012" /* For COFF and a.out archives */
1503 typedef struct {
1504 unsigned int n_strx; /* index into string table of name */
1505 unsigned char n_type; /* type of symbol */
1506 unsigned char n_other; /* misc info (usually empty) */
1507 unsigned short n_desc; /* description field */
1508 unsigned int n_value; /* value of symbol */
1509 } Stab_Sym;
1511 ST_FUNC void tccelf_new(TCCState *s);
1512 ST_FUNC void tccelf_delete(TCCState *s);
1513 ST_FUNC void tccelf_begin_file(TCCState *s1);
1514 ST_FUNC void tccelf_end_file(TCCState *s1);
1515 #ifdef CONFIG_TCC_BCHECK
1516 ST_FUNC void tccelf_bounds_new(TCCState *s);
1517 #endif
1518 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
1519 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
1520 ST_FUNC size_t section_add(Section *sec, addr_t size, int align);
1521 ST_FUNC void *section_ptr_add(Section *sec, addr_t size);
1522 ST_FUNC Section *find_section(TCCState *s1, const char *name);
1523 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);
1525 ST_FUNC int put_elf_str(Section *s, const char *sym);
1526 ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1527 ST_FUNC int set_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
1528 ST_FUNC int find_elf_sym(Section *s, const char *name);
1529 ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
1530 ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);
1532 ST_FUNC void resolve_common_syms(TCCState *s1);
1533 ST_FUNC void relocate_syms(TCCState *s1, Section *symtab, int do_resolve);
1534 ST_FUNC void relocate_sections(TCCState *s1);
1536 ST_FUNC ssize_t full_read(int fd, void *buf, size_t count);
1537 ST_FUNC void *load_data(int fd, unsigned long file_offset, unsigned long size);
1538 ST_FUNC int tcc_object_type(int fd, ElfW(Ehdr) *h);
1539 ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
1540 ST_FUNC int tcc_load_archive(TCCState *s1, int fd, int alacarte);
1541 ST_FUNC void add_array(TCCState *s1, const char *sec, int c);
1543 ST_FUNC struct sym_attr *get_sym_attr(TCCState *s1, int index, int alloc);
1544 ST_FUNC addr_t get_sym_addr(TCCState *s, const char *name, int err, int forc);
1545 ST_FUNC void list_elf_symbols(TCCState *s, void *ctx,
1546 void (*symbol_cb)(void *ctx, const char *name, const void *val));
1547 ST_FUNC int set_global_sym(TCCState *s1, const char *name, Section *sec, addr_t offs);
1549 /* Browse each elem of type <type> in section <sec> starting at elem <startoff>
1550 using variable <elem> */
1551 #define for_each_elem(sec, startoff, elem, type) \
1552 for (elem = (type *) sec->data + startoff; \
1553 elem < (type *) (sec->data + sec->data_offset); elem++)
1555 #ifndef ELF_OBJ_ONLY
1556 ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
1557 ST_FUNC int tcc_load_ldscript(TCCState *s1, int fd);
1558 #endif
1559 #ifndef TCC_TARGET_PE
1560 ST_FUNC void tcc_add_runtime(TCCState *s1);
1561 #endif
1563 /* ------------ xxx-link.c ------------ */
1565 #if !defined ELF_OBJ_ONLY || defined TCC_TARGET_MACHO
1566 ST_FUNC int code_reloc (int reloc_type);
1567 ST_FUNC int gotplt_entry_type (int reloc_type);
1568 /* Whether to generate a GOT/PLT entry and when. NO_GOTPLT_ENTRY is first so
1569 that unknown relocation don't create a GOT or PLT entry */
1570 enum gotplt_entry {
1571 NO_GOTPLT_ENTRY, /* never generate (eg. GLOB_DAT & JMP_SLOT relocs) */
1572 BUILD_GOT_ONLY, /* only build GOT (eg. TPOFF relocs) */
1573 AUTO_GOTPLT_ENTRY, /* generate if sym is UNDEF */
1574 ALWAYS_GOTPLT_ENTRY /* always generate (eg. PLTOFF relocs) */
1576 #define NEED_RELOC_TYPE
1578 #if !defined TCC_TARGET_MACHO || defined TCC_IS_NATIVE
1579 ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr);
1580 ST_FUNC void relocate_plt(TCCState *s1);
1581 ST_FUNC void build_got_entries(TCCState *s1, int got_sym); /* in tccelf.c */
1582 #define NEED_BUILD_GOT
1584 #endif
1585 #endif
1587 ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val);
1589 /* ------------ xxx-gen.c ------------ */
1590 ST_DATA const char * const target_machine_defs;
1591 ST_DATA const int reg_classes[NB_REGS];
1593 ST_FUNC void gsym_addr(int t, int a);
1594 ST_FUNC void gsym(int t);
1595 ST_FUNC void load(int r, SValue *sv);
1596 ST_FUNC void store(int r, SValue *v);
1597 ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align, int *regsize);
1598 ST_FUNC void gfunc_call(int nb_args);
1599 ST_FUNC void gfunc_prolog(Sym *func_sym);
1600 ST_FUNC void gfunc_epilog(void);
1601 ST_FUNC void gen_fill_nops(int);
1602 ST_FUNC int gjmp(int t);
1603 ST_FUNC void gjmp_addr(int a);
1604 ST_FUNC int gjmp_cond(int op, int t);
1605 ST_FUNC int gjmp_append(int n, int t);
1606 ST_FUNC void gen_opi(int op);
1607 ST_FUNC void gen_opf(int op);
1608 ST_FUNC void gen_cvt_ftoi(int t);
1609 ST_FUNC void gen_cvt_itof(int t);
1610 ST_FUNC void gen_cvt_ftof(int t);
1611 ST_FUNC void ggoto(void);
1612 #ifndef TCC_TARGET_C67
1613 ST_FUNC void o(unsigned int c);
1614 #endif
1615 ST_FUNC void gen_vla_sp_save(int addr);
1616 ST_FUNC void gen_vla_sp_restore(int addr);
1617 ST_FUNC void gen_vla_alloc(CType *type, int align);
1619 static inline uint16_t read16le(unsigned char *p) {
1620 return p[0] | (uint16_t)p[1] << 8;
1622 static inline void write16le(unsigned char *p, uint16_t x) {
1623 p[0] = x & 255; p[1] = x >> 8 & 255;
1625 static inline uint32_t read32le(unsigned char *p) {
1626 return read16le(p) | (uint32_t)read16le(p + 2) << 16;
1628 static inline void write32le(unsigned char *p, uint32_t x) {
1629 write16le(p, x); write16le(p + 2, x >> 16);
1631 static inline void add32le(unsigned char *p, int32_t x) {
1632 write32le(p, read32le(p) + x);
1634 static inline uint64_t read64le(unsigned char *p) {
1635 return read32le(p) | (uint64_t)read32le(p + 4) << 32;
1637 static inline void write64le(unsigned char *p, uint64_t x) {
1638 write32le(p, x); write32le(p + 4, x >> 32);
1640 static inline void add64le(unsigned char *p, int64_t x) {
1641 write64le(p, read64le(p) + x);
1644 /* ------------ i386-gen.c ------------ */
1645 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 || defined TCC_TARGET_ARM
1646 ST_FUNC void g(int c);
1647 ST_FUNC void gen_le16(int c);
1648 ST_FUNC void gen_le32(int c);
1649 #endif
1650 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1651 ST_FUNC void gen_addr32(int r, Sym *sym, int c);
1652 ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
1653 ST_FUNC void gen_cvt_csti(int t);
1654 ST_FUNC void gen_increment_tcov (SValue *sv);
1655 #endif
1657 /* ------------ x86_64-gen.c ------------ */
1658 #ifdef TCC_TARGET_X86_64
1659 ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
1660 ST_FUNC void gen_opl(int op);
1661 #ifdef TCC_TARGET_PE
1662 ST_FUNC void gen_vla_result(int addr);
1663 #endif
1664 ST_FUNC void gen_cvt_sxtw(void);
1665 ST_FUNC void gen_cvt_csti(int t);
1666 #endif
1668 /* ------------ arm-gen.c ------------ */
1669 #ifdef TCC_TARGET_ARM
1670 #if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP)
1671 PUB_FUNC const char *default_elfinterp(struct TCCState *s);
1672 #endif
1673 ST_FUNC void arm_init(struct TCCState *s);
1674 ST_FUNC void gen_increment_tcov (SValue *sv);
1675 #endif
1677 /* ------------ arm64-gen.c ------------ */
1678 #ifdef TCC_TARGET_ARM64
1679 ST_FUNC void gen_opl(int op);
1680 ST_FUNC void gfunc_return(CType *func_type);
1681 ST_FUNC void gen_va_start(void);
1682 ST_FUNC void gen_va_arg(CType *t);
1683 ST_FUNC void gen_clear_cache(void);
1684 ST_FUNC void gen_cvt_sxtw(void);
1685 ST_FUNC void gen_cvt_csti(int t);
1686 ST_FUNC void gen_increment_tcov (SValue *sv);
1687 #endif
1689 /* ------------ riscv64-gen.c ------------ */
1690 #ifdef TCC_TARGET_RISCV64
1691 ST_FUNC void gen_opl(int op);
1692 //ST_FUNC void gfunc_return(CType *func_type);
1693 ST_FUNC void gen_va_start(void);
1694 ST_FUNC void arch_transfer_ret_regs(int);
1695 ST_FUNC void gen_cvt_sxtw(void);
1696 ST_FUNC void gen_increment_tcov (SValue *sv);
1697 #endif
1699 /* ------------ c67-gen.c ------------ */
1700 #ifdef TCC_TARGET_C67
1701 #endif
1703 /* ------------ tcccoff.c ------------ */
1704 #ifdef TCC_TARGET_COFF
1705 ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
1706 ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
1707 #endif
1709 /* ------------ tccasm.c ------------ */
1710 ST_FUNC void asm_instr(void);
1711 ST_FUNC void asm_global_instr(void);
1712 ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
1713 #ifdef CONFIG_TCC_ASM
1714 ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
1715 ST_FUNC Sym* get_asm_sym(int name, Sym *csym);
1716 ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
1717 ST_FUNC int asm_int_expr(TCCState *s1);
1718 /* ------------ i386-asm.c ------------ */
1719 ST_FUNC void gen_expr32(ExprValue *pe);
1720 #ifdef TCC_TARGET_X86_64
1721 ST_FUNC void gen_expr64(ExprValue *pe);
1722 #endif
1723 ST_FUNC void asm_opcode(TCCState *s1, int opcode);
1724 ST_FUNC int asm_parse_regvar(int t);
1725 ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
1726 ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
1727 ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
1728 ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
1729 #endif
1731 /* ------------ tccpe.c -------------- */
1732 #ifdef TCC_TARGET_PE
1733 ST_FUNC int pe_load_file(struct TCCState *s1, int fd, const char *filename);
1734 ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
1735 ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
1736 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
1737 ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
1738 #endif
1739 #ifdef TCC_TARGET_X86_64
1740 ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
1741 #endif
1742 PUB_FUNC int tcc_get_dllexports(const char *filename, char **pp);
1743 /* symbol properties stored in Elf32_Sym->st_other */
1744 # define ST_PE_EXPORT 0x10
1745 # define ST_PE_IMPORT 0x20
1746 # define ST_PE_STDCALL 0x40
1747 #endif
1748 #define ST_ASM_SET 0x04
1750 /* ------------ tccmacho.c ----------------- */
1751 #ifdef TCC_TARGET_MACHO
1752 ST_FUNC int macho_output_file(TCCState * s1, const char *filename);
1753 ST_FUNC int macho_load_dll(TCCState *s1, int fd, const char *filename, int lev);
1754 ST_FUNC int macho_load_tbd(TCCState *s1, int fd, const char *filename, int lev);
1755 #ifdef TCC_IS_NATIVE
1756 ST_FUNC void tcc_add_macos_sdkpath(TCCState* s);
1757 ST_FUNC const char* macho_tbd_soname(const char* filename);
1758 #endif
1759 #endif
1760 /* ------------ tccrun.c ----------------- */
1761 #ifdef TCC_IS_NATIVE
1762 #ifdef CONFIG_TCC_STATIC
1763 #define RTLD_LAZY 0x001
1764 #define RTLD_NOW 0x002
1765 #define RTLD_GLOBAL 0x100
1766 #define RTLD_DEFAULT NULL
1767 /* dummy function for profiling */
1768 ST_FUNC void *dlopen(const char *filename, int flag);
1769 ST_FUNC void dlclose(void *p);
1770 ST_FUNC const char *dlerror(void);
1771 ST_FUNC void *dlsym(void *handle, const char *symbol);
1772 #endif
1773 ST_FUNC void tcc_run_free(TCCState *s1);
1774 #endif
1776 /* ------------ tcctools.c ----------------- */
1777 #if 0 /* included in tcc.c */
1778 ST_FUNC int tcc_tool_ar(TCCState *s, int argc, char **argv);
1779 #ifdef TCC_TARGET_PE
1780 ST_FUNC int tcc_tool_impdef(TCCState *s, int argc, char **argv);
1781 #endif
1782 ST_FUNC void tcc_tool_cross(TCCState *s, char **argv, int option);
1783 ST_FUNC void gen_makedeps(TCCState *s, const char *target, const char *filename);
1784 #endif
1786 /* ------------ tccdbg.c ------------ */
1788 ST_FUNC void tcc_debug_new(TCCState *s);
1790 ST_FUNC void tcc_debug_start(TCCState *s1);
1791 ST_FUNC void tcc_debug_end(TCCState *s1);
1792 ST_FUNC void tcc_debug_bincl(TCCState *s1);
1793 ST_FUNC void tcc_debug_eincl(TCCState *s1);
1794 ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename);
1796 ST_FUNC void tcc_debug_line(TCCState *s1);
1797 ST_FUNC void tcc_add_debug_info(TCCState *s1, int param, Sym *s, Sym *e);
1798 ST_FUNC void tcc_debug_funcstart(TCCState *s1, Sym *sym);
1799 ST_FUNC void tcc_debug_funcend(TCCState *s1, int size);
1800 ST_FUNC void tcc_debug_extern_sym(TCCState *s1, Sym *sym, int sh_num, int sym_bind, int sym_type);
1801 ST_FUNC void tcc_debug_typedef(TCCState *s1, Sym *sym);
1802 ST_FUNC void tcc_debug_stabn(TCCState *s1, int type, int value);
1803 ST_FUNC void tcc_debug_fix_anon(TCCState *s1, CType *t);
1805 ST_FUNC void tcc_tcov_start(TCCState *s1);
1806 ST_FUNC void tcc_tcov_end(TCCState *s1);
1807 ST_FUNC void tcc_tcov_check_line(TCCState *s1, int start);
1808 ST_FUNC void tcc_tcov_block_end(TCCState *s1, int line);
1809 ST_FUNC void tcc_tcov_block_begin(TCCState *s1);
1810 ST_FUNC void tcc_tcov_reset_ind(TCCState *s1);
1812 #define stab_section s1->stab_section
1813 #define stabstr_section stab_section->link
1814 #define tcov_section s1->tcov_section
1815 #define dwarf_info_section s1->dwarf_info_section
1816 #define dwarf_abbrev_section s1->dwarf_abbrev_section
1817 #define dwarf_line_section s1->dwarf_line_section
1818 #define dwarf_aranges_section s1->dwarf_aranges_section
1819 #define dwarf_str_section s1->dwarf_str_section
1820 #define dwarf_line_str_section s1->dwarf_line_str_section
1822 /* default dwarf version for "-g". use 0 to emit stab debug infos */
1823 #ifndef DWARF_VERSION
1824 # define DWARF_VERSION 0
1825 #endif
1827 /* default dwarf version for "-gdwarf" */
1828 #ifdef TCC_TARGET_MACHO
1829 # define DEFAULT_DWARF_VERSION 2
1830 #else
1831 # define DEFAULT_DWARF_VERSION 5
1832 #endif
1834 #if defined TCC_TARGET_PE
1835 # define R_DATA_32DW 'Z' /* fake code to avoid DLL relocs */
1836 #elif defined TCC_TARGET_X86_64
1837 # define R_DATA_32DW R_X86_64_32
1838 #else
1839 # define R_DATA_32DW R_DATA_32
1840 #endif
1842 /********************************************************/
1843 #if CONFIG_TCC_SEMLOCK
1844 #if defined _WIN32
1845 typedef struct { int init; CRITICAL_SECTION cr; } TCCSem;
1846 #elif defined __APPLE__
1847 #include <dispatch/dispatch.h>
1848 typedef struct { int init; dispatch_semaphore_t sem; } TCCSem;
1849 #else
1850 #include <semaphore.h>
1851 typedef struct { int init; sem_t sem; } TCCSem;
1852 #endif
1853 ST_FUNC void wait_sem(TCCSem *p);
1854 ST_FUNC void post_sem(TCCSem *p);
1855 #define TCC_SEM(s) TCCSem s
1856 #define WAIT_SEM wait_sem
1857 #define POST_SEM post_sem
1858 #else
1859 #define TCC_SEM(s)
1860 #define WAIT_SEM(p)
1861 #define POST_SEM(p)
1862 #endif
1864 /********************************************************/
1865 #undef ST_DATA
1866 #if ONE_SOURCE
1867 #define ST_DATA static
1868 #else
1869 #define ST_DATA
1870 #endif
1871 /********************************************************/
1873 #define text_section TCC_STATE_VAR(text_section)
1874 #define data_section TCC_STATE_VAR(data_section)
1875 #define rodata_section TCC_STATE_VAR(rodata_section)
1876 #define bss_section TCC_STATE_VAR(bss_section)
1877 #define common_section TCC_STATE_VAR(common_section)
1878 #define cur_text_section TCC_STATE_VAR(cur_text_section)
1879 #define bounds_section TCC_STATE_VAR(bounds_section)
1880 #define lbounds_section TCC_STATE_VAR(lbounds_section)
1881 #define symtab_section TCC_STATE_VAR(symtab_section)
1882 #define gnu_ext TCC_STATE_VAR(gnu_ext)
1883 #define tcc_error_noabort TCC_SET_STATE(_tcc_error_noabort)
1884 #define tcc_error TCC_SET_STATE(_tcc_error)
1885 #define tcc_warning TCC_SET_STATE(_tcc_warning)
1887 #define total_idents TCC_STATE_VAR(total_idents)
1888 #define total_lines TCC_STATE_VAR(total_lines)
1889 #define total_bytes TCC_STATE_VAR(total_bytes)
1891 PUB_FUNC void tcc_enter_state(TCCState *s1);
1892 PUB_FUNC void tcc_exit_state(TCCState *s1);
1894 /* conditional warning depending on switch */
1895 #define tcc_warning_c(sw) TCC_SET_STATE((\
1896 tcc_state->warn_num = offsetof(TCCState, sw) \
1897 - offsetof(TCCState, warn_none), _tcc_warning))
1899 /********************************************************/
1900 #endif /* _TCC_H */
1902 #undef TCC_STATE_VAR
1903 #undef TCC_SET_STATE
1905 #ifdef USING_GLOBALS
1906 # define TCC_STATE_VAR(sym) tcc_state->sym
1907 # define TCC_SET_STATE(fn) fn
1908 # undef USING_GLOBALS
1909 #else
1910 # define TCC_STATE_VAR(sym) s1->sym
1911 # define TCC_SET_STATE(fn) (tcc_enter_state(s1),fn)
1912 #endif