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