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