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