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