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