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