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