Subversion Repositories Kolibri OS

Rev

Rev 9558 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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. #include "config.h"
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <stdarg.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <math.h>
  33. #include <signal.h>
  34. #include <fcntl.h>
  35. #include <setjmp.h>
  36. #include <time.h>
  37. #include <sys/stat.h>          /* stat() */
  38.  
  39. #ifdef CONFIG_TCCASSERT
  40. #include <assert.h>
  41. #define TCC_ASSERT(ex) assert(ex)
  42. #else
  43. #define TCC_ASSERT(ex)
  44. #endif
  45.  
  46. #ifdef TCC_TARGET_KX
  47. #ifndef TCC_TARGET_MEOS
  48. #define TCC_TARGET_MEOS
  49. #endif
  50. void kx_fix_root_directory(char *buf, size_t size);
  51. #endif
  52.  
  53. #ifndef _WIN32
  54. # include <unistd.h>
  55. # include <sys/time.h>
  56. # ifndef TCC_TARGET_MEOS
  57. # include <sys/ucontext.h>
  58. # include <sys/mman.h>
  59. # endif
  60. # ifndef CONFIG_TCC_STATIC
  61. #  include <dlfcn.h>
  62. # endif
  63. /* XXX: need to define this to use them in non ISOC99 context */
  64.  extern float strtof (const char *__nptr, char **__endptr);
  65.  extern long double strtold (const char *__nptr, char **__endptr);
  66. #else /* on _WIN32: */
  67. # include <windows.h>
  68. # include <sys/timeb.h>
  69. # include <io.h> /* open, close etc. */
  70. # include <direct.h> /* getcwd */
  71. # ifdef __GNUC__
  72. #  include <stdint.h>
  73. # endif
  74. # define inline __inline
  75. # define inp next_inp
  76. # define snprintf _snprintf
  77. # define vsnprintf _vsnprintf
  78. # ifndef __GNUC__
  79. #  define strtold (long double)strtod
  80. #  define strtof (float)strtod
  81. #  define strtoll _strtoi64
  82. #  define strtoull _strtoui64
  83. # endif
  84. # ifdef LIBTCC_AS_DLL
  85. #  define LIBTCCAPI __declspec(dllexport)
  86. #  define PUB_FUNC LIBTCCAPI
  87. # endif
  88. # ifdef _MSC_VER
  89. #  pragma warning (disable : 4244)  // conversion from 'uint64_t' to 'int', possible loss of data
  90. #  pragma warning (disable : 4267)  // conversion from 'size_t' to 'int', possible loss of data
  91. #  pragma warning (disable : 4996)  // The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name
  92. #  pragma warning (disable : 4018)  // signed/unsigned mismatch
  93. #  pragma warning (disable : 4146)  // unary minus operator applied to unsigned type, result still unsigned
  94. # endif
  95. #endif
  96.  
  97. #ifndef O_BINARY
  98. # define O_BINARY 0
  99. #endif
  100.  
  101. #ifdef __GNUC__
  102. # define NORETURN __attribute__ ((noreturn))
  103. #elif defined _MSC_VER
  104. # define NORETURN __declspec(noreturn)
  105. #else
  106. # define NORETURN
  107. #endif
  108.  
  109. #ifdef _WIN32
  110. # define IS_DIRSEP(c) (c == '/' || c == '\\')
  111. # define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
  112. # define PATHCMP stricmp
  113. # define PATH_NOCASE
  114. #else
  115. # define IS_DIRSEP(c) (c == '/')
  116. # define IS_ABSPATH(p) IS_DIRSEP(p[0])
  117. # define PATHCMP strcmp
  118. #endif
  119.  
  120. #if defined(TCC_TARGET_PE)||defined(TCC_TARGET_MEOS)
  121. #define PATHSEP ';'
  122. #else
  123. #define PATHSEP ':'
  124. #endif
  125.  
  126. #include "elf.h"
  127. #if defined(TCC_TARGET_ARM64) || defined(TCC_TARGET_X86_64)
  128. # define ELFCLASSW ELFCLASS64
  129. # define ElfW(type) Elf##64##_##type
  130. # define ELFW(type) ELF##64##_##type
  131. # define ElfW_Rel ElfW(Rela)
  132. # define SHT_RELX SHT_RELA
  133. # define REL_SECTION_FMT ".rela%s"
  134. /* XXX: DLL with PLT would only work with x86-64 for now */
  135. # define TCC_OUTPUT_DLL_WITH_PLT
  136. #else
  137. # define ELFCLASSW ELFCLASS32
  138. # define ElfW(type) Elf##32##_##type
  139. # define ELFW(type) ELF##32##_##type
  140. # define ElfW_Rel ElfW(Rel)
  141. # define SHT_RELX SHT_REL
  142. # define REL_SECTION_FMT ".rel%s"
  143. #endif
  144.  
  145. /* target address type */
  146. #define addr_t ElfW(Addr)
  147.  
  148. #include "stab.h"
  149. #include "libtcc.h"
  150.  
  151. static inline uint16_t read16le(unsigned char *p)
  152. {
  153.     return p[0] | (uint16_t)p[1] << 8;
  154. }
  155.  
  156. static inline void write16le(unsigned char *p, uint16_t x)
  157. {
  158.     p[0] = x & 255;
  159.     p[1] = x >> 8 & 255;
  160. }
  161.  
  162. static inline uint32_t read32le(unsigned char *p)
  163. {
  164.   return (p[0] | (uint32_t)p[1] << 8 |
  165.           (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24);
  166. }
  167.  
  168. static inline void write32le(unsigned char *p, uint32_t x)
  169. {
  170.     p[0] = x & 255;
  171.     p[1] = x >> 8 & 255;
  172.     p[2] = x >> 16 & 255;
  173.     p[3] = x >> 24 & 255;
  174. }
  175.  
  176. static inline uint64_t read64le(unsigned char *p)
  177. {
  178.   return (p[0] | (uint64_t)p[1] << 8 |
  179.           (uint64_t)p[2] << 16 | (uint64_t)p[3] << 24 |
  180.           (uint64_t)p[4] << 32 | (uint64_t)p[5] << 40 |
  181.           (uint64_t)p[6] << 48 | (uint64_t)p[7] << 56);
  182. }
  183.  
  184. static inline void write64le(unsigned char *p, uint64_t x)
  185. {
  186.     p[0] = x & 255;
  187.     p[1] = x >> 8 & 255;
  188.     p[2] = x >> 16 & 255;
  189.     p[3] = x >> 24 & 255;
  190.     p[4] = x >> 32 & 255;
  191.     p[5] = x >> 40 & 255;
  192.     p[6] = x >> 48 & 255;
  193.     p[7] = x >> 56 & 255;
  194. }
  195.  
  196. /* parser debug */
  197. /* #define PARSE_DEBUG */
  198. /* preprocessor debug */
  199. /* #define PP_DEBUG */
  200. /* include file debug */
  201. /* #define INC_DEBUG */
  202. /* memory leak debug */
  203. /* #define MEM_DEBUG */
  204. /* assembler debug */
  205. /* #define ASM_DEBUG */
  206.  
  207. /* target selection */
  208. /* #define TCC_TARGET_I386   *//* i386 code generator */
  209. /* #define TCC_TARGET_ARM    *//* ARMv4 code generator */
  210. /* #define TCC_TARGET_ARM64  *//* ARMv8 code generator */
  211. /* #define TCC_TARGET_C67    *//* TMS320C67xx code generator */
  212. /* #define TCC_TARGET_X86_64 *//* x86-64 code generator */
  213.  
  214. /* default target is I386 */
  215. #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
  216.     !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
  217.     !defined(TCC_TARGET_X86_64)
  218. #define TCC_TARGET_I386
  219. #endif
  220.  
  221. #if !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
  222.     !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_C67) && \
  223.     !defined(CONFIG_USE_LIBGCC)
  224. #define CONFIG_TCC_BCHECK /* enable bound checking code */
  225. #endif
  226.  
  227. /* define it to include assembler support */
  228. #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_ARM64) && \
  229.     !defined(TCC_TARGET_C67)
  230. #define CONFIG_TCC_ASM
  231. #endif
  232.  
  233. /* object format selection */
  234. #if defined(TCC_TARGET_C67)
  235. #define TCC_TARGET_COFF
  236. #endif
  237.  
  238. /* only native compiler supports -run */
  239. #if defined _WIN32 && defined TCC_TARGET_PE
  240. # if (defined __i386__ || defined _X86_) && defined TCC_TARGET_I386
  241. #  define TCC_IS_NATIVE
  242. # elif (defined __x86_64__ || defined _AMD64_) && defined TCC_TARGET_X86_64
  243. #  define TCC_IS_NATIVE
  244. # elif defined __arm__ && defined TCC_TARGET_ARM
  245. #  define TCC_IS_NATIVE
  246. # elif defined __aarch64__ && defined TCC_TARGET_ARM64
  247. #  define TCC_IS_NATIVE
  248. # endif
  249. #endif
  250.  
  251. #if defined TCC_IS_NATIVE && !defined CONFIG_TCCBOOT
  252. # define CONFIG_TCC_BACKTRACE
  253. #endif
  254.  
  255. /* ------------ path configuration ------------ */
  256.  
  257. #ifndef CONFIG_SYSROOT
  258. # define CONFIG_SYSROOT ""
  259. #endif
  260. #ifndef CONFIG_TCCDIR
  261. # define CONFIG_TCCDIR "."
  262. #endif
  263. #ifndef CONFIG_LDDIR
  264. # ifdef TCC_TARGET_X86_64
  265. #   define CONFIG_LDDIR "lib64"
  266. # else
  267. #   define CONFIG_LDDIR "lib"
  268. # endif
  269. #endif
  270.  
  271. #ifdef CONFIG_MULTIARCHDIR
  272. # define USE_MUADIR(s) s "/" CONFIG_MULTIARCHDIR
  273. # define ALSO_MUADIR(s) s "/" CONFIG_MULTIARCHDIR ":" s
  274. #else
  275. # define USE_MUADIR(s) s
  276. # define ALSO_MUADIR(s) s
  277. #endif
  278.  
  279. /* path to find crt1.o, crti.o and crtn.o */
  280. #ifndef CONFIG_TCC_CRTPREFIX
  281. # if defined (TCC_TARGET_MEOS)
  282. #   define CONFIG_TCC_CRTPREFIX "{B}/lib"
  283. # else
  284. #   define CONFIG_TCC_CRTPREFIX USE_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR)
  285. # endif
  286. #endif
  287.  
  288. /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
  289.  
  290. /* system include paths */
  291. #ifndef CONFIG_TCC_SYSINCLUDEPATHS
  292. # ifdef TCC_TARGET_PE
  293. #  define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi"
  294. # elif defined(TCC_TARGET_MEOS)
  295. #  define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include"
  296. # else
  297. #  define CONFIG_TCC_SYSINCLUDEPATHS \
  298.         "{B}/include" \
  299.     ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/include") \
  300.     ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/include")
  301. # endif
  302. #endif
  303.  
  304. /* library search paths */
  305. #ifndef CONFIG_TCC_LIBPATHS
  306. # if defined (TCC_TARGET_PE) || defined (TCC_TARGET_MEOS)
  307. #  define CONFIG_TCC_LIBPATHS "{B}/lib"
  308. # else
  309. #  define CONFIG_TCC_LIBPATHS \
  310.         ALSO_MUADIR(CONFIG_SYSROOT "/usr/" CONFIG_LDDIR) \
  311.     ":" ALSO_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) \
  312.     ":" ALSO_MUADIR(CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR)
  313. # endif
  314. #endif
  315.  
  316. /* name of ELF interpreter */
  317. #ifndef CONFIG_TCC_ELFINTERP
  318. # if defined __FreeBSD__
  319. #  define CONFIG_TCC_ELFINTERP "/libexec/ld-elf.so.1"
  320. # elif defined __FreeBSD_kernel__
  321. #  if defined(TCC_TARGET_X86_64)
  322. #   define CONFIG_TCC_ELFINTERP "/lib/ld-kfreebsd-x86-64.so.1"
  323. #  else
  324. #   define CONFIG_TCC_ELFINTERP "/lib/ld.so.1"
  325. #  endif
  326. # elif defined __DragonFly__
  327. #  define CONFIG_TCC_ELFINTERP "/usr/libexec/ld-elf.so.2"
  328. # elif defined __NetBSD__
  329. #  define CONFIG_TCC_ELFINTERP "/usr/libexec/ld.elf_so"
  330. # elif defined __GNU__
  331. #  define CONFIG_TCC_ELFINTERP "/lib/ld.so"
  332. # elif defined(TCC_TARGET_PE)
  333. #  define CONFIG_TCC_ELFINTERP "-"
  334. # elif defined(TCC_UCLIBC)
  335. #  define CONFIG_TCC_ELFINTERP "/lib/ld-uClibc.so.0" /* is there a uClibc for x86_64 ? */
  336. # elif defined TCC_TARGET_ARM64
  337. #  define CONFIG_TCC_ELFINTERP "/lib/ld-linux-aarch64.so.1"
  338. # elif defined(TCC_TARGET_X86_64)
  339. #  define CONFIG_TCC_ELFINTERP "/lib64/ld-linux-x86-64.so.2"
  340. # elif !defined(TCC_ARM_EABI)
  341. #  define CONFIG_TCC_ELFINTERP "/lib/ld-linux.so.2"
  342. # endif
  343. #endif
  344.  
  345. /* var elf_interp dans *-gen.c */
  346. #ifdef CONFIG_TCC_ELFINTERP
  347. # define DEFAULT_ELFINTERP(s) CONFIG_TCC_ELFINTERP
  348. #else
  349. # define DEFAULT_ELFINTERP(s) default_elfinterp(s)
  350. #endif
  351.  
  352. /* library to use with CONFIG_USE_LIBGCC instead of libtcc1.a */
  353. #define TCC_LIBGCC USE_MUADIR(CONFIG_SYSROOT "/" CONFIG_LDDIR) "/libgcc_s.so.1"
  354.  
  355. /* -------------------------------------------- */
  356. /* include the target specific definitions */
  357.  
  358. #ifdef TCC_TARGET_COFF
  359. # include "coff.h"
  360. #endif
  361.  
  362.  
  363. #define TARGET_DEFS_ONLY
  364. #ifdef TCC_TARGET_I386
  365. # include "i386-gen.c"
  366. #endif
  367. #ifdef TCC_TARGET_X86_64
  368. # include "x86_64-gen.c"
  369. #endif
  370. #ifdef TCC_TARGET_ARM
  371. # include "arm-gen.c"
  372. #endif
  373. #ifdef TCC_TARGET_ARM64
  374. # include "arm64-gen.c"
  375. #endif
  376. #ifdef TCC_TARGET_C67
  377. # include "c67-gen.c"
  378. #endif
  379. #undef TARGET_DEFS_ONLY
  380.  
  381. /* -------------------------------------------- */
  382.  
  383. #define INCLUDE_STACK_SIZE  32
  384. #define IFDEF_STACK_SIZE    64
  385. #define VSTACK_SIZE         256
  386. #define STRING_MAX_SIZE     1024
  387. #define TOKSTR_MAX_SIZE     256
  388. #define PACK_STACK_SIZE     8
  389.  
  390. #define TOK_HASH_SIZE       16384 /* must be a power of two */
  391. #define TOK_ALLOC_INCR      512  /* must be a power of two */
  392. #define TOK_MAX_SIZE        4 /* token max size in int unit when stored in string */
  393. #define TOKSYM_TAL_SIZE     (768 * 1024) /* allocator for tiny TokenSym in table_ident */
  394. #define TOKSTR_TAL_SIZE     (768 * 1024) /* allocator for tiny TokenString instances */
  395. #define CSTR_TAL_SIZE       (256 * 1024) /* allocator for tiny CString instances */
  396. #define TOKSYM_TAL_LIMIT    256 /* prefer unique limits to distinguish allocators debug msgs */
  397. #define TOKSTR_TAL_LIMIT    128 /* 32 * sizeof(int) */
  398. #define CSTR_TAL_LIMIT      1024
  399.  
  400. /* token symbol management */
  401. typedef struct TokenSym {
  402.     struct TokenSym *hash_next;
  403.     struct Sym *sym_define; /* direct pointer to define */
  404.     struct Sym *sym_label; /* direct pointer to label */
  405.     struct Sym *sym_struct; /* direct pointer to structure */
  406.     struct Sym *sym_identifier; /* direct pointer to identifier */
  407.     int tok; /* token number */
  408.     int len;
  409.     char str[1];
  410. } TokenSym;
  411.  
  412. #ifdef TCC_TARGET_PE
  413. typedef unsigned short nwchar_t;
  414. #else
  415. typedef int nwchar_t;
  416. #endif
  417.  
  418. typedef struct CString {
  419.     int size; /* size in bytes */
  420.     void *data; /* either 'char *' or 'nwchar_t *' */
  421.     int size_allocated;
  422.     void *data_allocated; /* if non NULL, data has been malloced */
  423. } CString;
  424.  
  425. /* type definition */
  426. typedef struct CType {
  427.     int t;
  428.     struct Sym *ref;
  429. } CType;
  430.  
  431. /* constant value */
  432. typedef union CValue {
  433.     long double ld;
  434.     double d;
  435.     float f;
  436.     uint64_t i;
  437.     struct {
  438.         int size;
  439.         const void *data;
  440.         void *data_allocated;
  441.     } str;
  442.     int tab[LDOUBLE_SIZE/4];
  443. } CValue;
  444.  
  445. /* value on stack */
  446. typedef struct SValue {
  447.     CType type;      /* type */
  448.     unsigned short r;      /* register + flags */
  449.     unsigned short r2;     /* second register, used for 'long long'
  450.                               type. If not used, set to VT_CONST */
  451.     CValue c;              /* constant, if VT_CONST */
  452.     struct Sym *sym;       /* symbol, if (VT_SYM | VT_CONST) */
  453. } SValue;
  454.  
  455. struct Attribute {
  456.     unsigned
  457.         func_call     : 3, /* calling convention (0..5), see below */
  458.         aligned       : 5, /* alignement (0..16) */
  459.         packed        : 1,
  460.         func_export   : 1,
  461.         func_import   : 1,
  462.         func_args     : 5,
  463.         func_proto    : 1,
  464.         mode          : 4,
  465.         weak          : 1,
  466.         visibility    : 2,
  467.         fill          : 8; // 8 bits left to fit well in union below
  468. };
  469.  
  470. /* GNUC attribute definition */
  471. typedef struct AttributeDef {
  472.     struct Attribute a;
  473.     struct Section *section;
  474.     int alias_target;    /* token */
  475.     int asm_label;    /* associated asm label */
  476. } AttributeDef;
  477.  
  478. /* symbol management */
  479. typedef struct Sym {
  480.     int v;    /* symbol token */
  481.     int asm_label;    /* associated asm label */
  482.     union {
  483.         long r;    /* associated register */
  484.         struct Attribute a;
  485.     };
  486.     union {
  487.         long c;    /* associated number */
  488.         int *d;   /* define token stream */
  489.     };
  490.     CType type;    /* associated type */
  491.     union {
  492.         struct Sym *next; /* next related symbol */
  493.         long jnext; /* next jump label */
  494.     };
  495.     struct Sym *prev; /* prev symbol in stack */
  496.     struct Sym *prev_tok; /* previous symbol for this token */
  497. } Sym;
  498.  
  499. /* section definition */
  500. /* XXX: use directly ELF structure for parameters ? */
  501. /* special flag to indicate that the section should not be linked to
  502.    the other ones */
  503. #define SHF_PRIVATE 0x80000000
  504.  
  505. /* special flag, too */
  506. #define SECTION_ABS ((void *)1)
  507.  
  508. typedef struct Section {
  509.     unsigned long data_offset; /* current data offset */
  510.     unsigned char *data;       /* section data */
  511.     unsigned long data_allocated; /* used for realloc() handling */
  512.     int sh_name;             /* elf section name (only used during output) */
  513.     int sh_num;              /* elf section number */
  514.     int sh_type;             /* elf section type */
  515.     int sh_flags;            /* elf section flags */
  516.     int sh_info;             /* elf section info */
  517.     int sh_addralign;        /* elf section alignment */
  518.     int sh_entsize;          /* elf entry size */
  519.     unsigned long sh_size;   /* section size (only used during output) */
  520.     addr_t sh_addr;          /* address at which the section is relocated */
  521.     unsigned long sh_offset; /* file offset */
  522.     int nb_hashed_syms;      /* used to resize the hash table */
  523.     struct Section *link;    /* link to another section */
  524.     struct Section *reloc;   /* corresponding section for relocation, if any */
  525.     struct Section *hash;     /* hash table for symbols */
  526.     struct Section *next;
  527.     char name[1];           /* section name */
  528. } Section;
  529.  
  530. typedef struct DLLReference {
  531.     int level;
  532.     void *handle;
  533.     char name[1];
  534. } DLLReference;
  535.  
  536. /* -------------------------------------------------- */
  537.  
  538. #define SYM_STRUCT     0x40000000 /* struct/union/enum symbol space */
  539. #define SYM_FIELD      0x20000000 /* struct/union field symbol space */
  540. #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
  541.  
  542. /* stored in 'Sym.c' field */
  543. #define FUNC_NEW       1 /* ansi function prototype */
  544. #define FUNC_OLD       2 /* old function prototype */
  545. #define FUNC_ELLIPSIS  3 /* ansi function prototype with ... */
  546.  
  547. /* stored in 'Sym.r' field */
  548. #define FUNC_CDECL     0 /* standard c call */
  549. #define FUNC_STDCALL   1 /* pascal c call */
  550. #define FUNC_FASTCALL1 2 /* first param in %eax */
  551. #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
  552. #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
  553. #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
  554.  
  555. /* field 'Sym.t' for macros */
  556. #define MACRO_OBJ      0 /* object like macro */
  557. #define MACRO_FUNC     1 /* function like macro */
  558.  
  559. /* field 'Sym.r' for C labels */
  560. #define LABEL_DEFINED  0 /* label is defined */
  561. #define LABEL_FORWARD  1 /* label is forward defined */
  562. #define LABEL_DECLARED 2 /* label is declared but never used */
  563.  
  564. /* type_decl() types */
  565. #define TYPE_ABSTRACT  1 /* type without variable */
  566. #define TYPE_DIRECT    2 /* type with variable */
  567.  
  568. #define IO_BUF_SIZE 8192
  569.  
  570. typedef struct BufferedFile {
  571.     uint8_t *buf_ptr;
  572.     uint8_t *buf_end;
  573.     int fd;
  574.     struct BufferedFile *prev;
  575.     int line_num;    /* current line number - here to simplify code */
  576.     int line_ref;    /* tcc -E: last printed line */
  577.     int ifndef_macro;  /* #ifndef macro / #endif search */
  578.     int ifndef_macro_saved; /* saved ifndef_macro */
  579.     int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
  580.     int include_next_index; /* next search path */
  581.     char filename[1024];    /* filename */
  582.     unsigned char unget[4];
  583.     unsigned char buffer[1]; /* extra size for CH_EOB char */
  584. } BufferedFile;
  585.  
  586. #define CH_EOB   '\\'       /* end of buffer or '\0' char in file */
  587. #define CH_EOF   (-1)   /* end of file */
  588.  
  589. /* parsing state (used to save parser state to reparse part of the
  590.    source several times) */
  591. typedef struct ParseState {
  592.     const int *macro_ptr;
  593.     int line_num;
  594.     int tok;
  595.     CValue tokc;
  596. } ParseState;
  597.  
  598. /* used to record tokens */
  599. typedef struct TokenString {
  600.     int *str;
  601.     int len;
  602.     int allocated_len;
  603.     int last_line_num;
  604.     /* used to chain token-strings with begin/end_macro() */
  605.     struct TokenString *prev;
  606.     const int *prev_ptr;
  607.     char alloc;
  608. } TokenString;
  609.  
  610. /* inline functions */
  611. typedef struct InlineFunc {
  612.     TokenString func_str;
  613.     Sym *sym;
  614.     char filename[1];
  615. } InlineFunc;
  616.  
  617. /* include file cache, used to find files faster and also to eliminate
  618.    inclusion if the include file is protected by #ifndef ... #endif */
  619. typedef struct CachedInclude {
  620.     int ifndef_macro;
  621.     int hash_next; /* -1 if none */
  622.     char filename[1]; /* path specified in #include */
  623. } CachedInclude;
  624.  
  625. #define CACHED_INCLUDES_HASH_SIZE 512
  626.  
  627. #ifdef CONFIG_TCC_ASM
  628. typedef struct ExprValue {
  629.     uint32_t v;
  630.     Sym *sym;
  631. } ExprValue;
  632.  
  633. #define MAX_ASM_OPERANDS 30
  634. typedef struct ASMOperand {
  635.     int id; /* GCC 3 optionnal identifier (0 if number only supported */
  636.     char *constraint;
  637.     char asm_str[16]; /* computed asm string for operand */
  638.     SValue *vt; /* C value of the expression */
  639.     int ref_index; /* if >= 0, gives reference to a output constraint */
  640.     int input_index; /* if >= 0, gives reference to an input constraint */
  641.     int priority; /* priority, used to assign registers */
  642.     int reg; /* if >= 0, register number used for this operand */
  643.     int is_llong; /* true if double register value */
  644.     int is_memory; /* true if memory operand */
  645.     int is_rw;     /* for '+' modifier */
  646. } ASMOperand;
  647. #endif
  648.  
  649. struct sym_attr {
  650.     unsigned long got_offset;
  651.     unsigned long plt_offset;
  652. #ifdef TCC_TARGET_ARM
  653.     unsigned char plt_thumb_stub:1;
  654. #endif
  655. };
  656.  
  657. typedef struct ParseArgsState
  658. {
  659.     int run;
  660.     int pthread;
  661.     int filetype;
  662.     CString linker_arg; /* collect -Wl options for input such as "-Wl,-rpath -Wl,<path>" */
  663. } ParseArgsState;
  664.  
  665. #if !defined(MEM_DEBUG)
  666. #define tal_free(al, p) tal_free_impl(al, p)
  667. #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size)
  668. #define TAL_DEBUG_PARAMS
  669. #else
  670. #define TAL_DEBUG 1
  671. #define tal_free(al, p) tal_free_impl(al, p, __FILE__, __LINE__)
  672. #define tal_realloc(al, p, size) tal_realloc_impl(&al, p, size, __FILE__, __LINE__)
  673. #define TAL_DEBUG_PARAMS , const char *file, int line
  674. #define TAL_DEBUG_FILE_LEN 15
  675. #endif
  676. //#define TAL_INFO 1 /* collect and dump allocators stats */
  677.  
  678. typedef struct TinyAlloc {
  679.     size_t  limit;
  680.     size_t  size;
  681.     uint8_t *buffer;
  682.     uint8_t *p;
  683.     size_t  nb_allocs;
  684.     struct TinyAlloc *next, *top;
  685. #ifdef TAL_INFO
  686.     size_t  nb_peak;
  687.     size_t  nb_total;
  688.     size_t  nb_missed;
  689.     uint8_t *peak_p;
  690. #endif
  691. } TinyAlloc;
  692.  
  693. typedef struct tal_header_t {
  694.     size_t  size;
  695. #ifdef TAL_DEBUG
  696.     int     line_num; /* negative line_num used for double free check */
  697.     char    file_name[TAL_DEBUG_FILE_LEN + 1];
  698. #endif
  699. } tal_header_t;
  700.  
  701. struct TCCState {
  702.  
  703.     int verbose; /* if true, display some information during compilation */
  704.     int nostdinc; /* if true, no standard headers are added */
  705.     int nostdlib; /* if true, no standard libraries are added */
  706.     int nocommon; /* if true, do not use common symbols for .bss data */
  707.     int static_link; /* if true, static linking is performed */
  708.     int rdynamic; /* if true, all symbols are exported */
  709.     int symbolic; /* if true, resolve symbols in the current module first */
  710.     int alacarte_link; /* if true, only link in referenced objects from archive */
  711.  
  712.     char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
  713.     char *soname; /* as specified on the command line (-soname) */
  714.     char *rpath; /* as specified on the command line (-Wl,-rpath=) */
  715.  
  716.     /* output type, see TCC_OUTPUT_XXX */
  717.     int output_type;
  718.     /* output format, see TCC_OUTPUT_FORMAT_xxx */
  719.     int output_format;
  720.  
  721.     /* C language options */
  722.     int char_is_unsigned;
  723.     int leading_underscore;
  724.     int ms_extensions;          /* allow nested named struct w/o identifier behave like unnamed */
  725.     int old_struct_init_code;   /* use old algorithm to init array in struct when there is no '{' used.
  726.                                    Liuux 2.4.26 can't find initrd when compiled with a new algorithm */
  727.     int dollars_in_identifiers; /* allows '$' char in indentifiers */
  728.     int normalize_inc_dirs;     /* remove non-existent or duplicate directories from include paths */
  729.  
  730.     /* warning switches */
  731.     int warn_write_strings;
  732.     int warn_unsupported;
  733.     int warn_error;
  734.     int warn_none;
  735.     int warn_implicit_function_declaration;
  736.  
  737.     /* compile with debug symbol (and use them if error during execution) */
  738.     int do_debug;
  739.     int do_strip;
  740. #ifdef CONFIG_TCC_BCHECK
  741.     /* compile with built-in memory and bounds checker */
  742.     int do_bounds_check;
  743. #endif
  744. #ifdef TCC_TARGET_ARM
  745.     enum float_abi float_abi; /* float ABI of the generated code*/
  746. #endif
  747.  
  748.     addr_t text_addr; /* address of text section */
  749.     int has_text_addr;
  750.  
  751.     unsigned long section_align; /* section alignment */
  752.  
  753.     char *init_symbol; /* symbols to call at load-time (not used currently) */
  754.     char *fini_symbol; /* symbols to call at unload-time (not used currently) */
  755.    
  756. #ifdef TCC_TARGET_I386
  757.     int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
  758. #endif
  759.  
  760.     /* array of all loaded dlls (including those referenced by loaded dlls) */
  761.     DLLReference **loaded_dlls;
  762.     int nb_loaded_dlls;
  763.  
  764.     /* include paths */
  765.     char **include_paths;
  766.     int nb_include_paths;
  767.  
  768.     char **sysinclude_paths;
  769.     int nb_sysinclude_paths;
  770.  
  771.     /* library paths */
  772.     char **library_paths;
  773.     int nb_library_paths;
  774.  
  775.     /* crt?.o object path */
  776.     char **crt_paths;
  777.     int nb_crt_paths;
  778.  
  779.     /* error handling */
  780.     void *error_opaque;
  781.     void (*error_func)(void *opaque, const char *msg);
  782.     int error_set_jmp_enabled;
  783.     jmp_buf error_jmp_buf;
  784.     int nb_errors;
  785.  
  786.     /* output file for preprocessing (-E) */
  787.     FILE *ppfp;
  788.     enum {
  789.         LINE_MACRO_OUTPUT_FORMAT_GCC,
  790.         LINE_MACRO_OUTPUT_FORMAT_NONE,
  791.         LINE_MACRO_OUTPUT_FORMAT_STD
  792.     } Pflag; /* -P switch */
  793.     char dflag; /* -dX value */
  794.     FILE *dffp;
  795.  
  796.     /* for -MD/-MF: collected dependencies for this compilation */
  797.     char **target_deps;
  798.     int nb_target_deps;
  799.  
  800.     /* compilation */
  801.     BufferedFile *include_stack[INCLUDE_STACK_SIZE];
  802.     BufferedFile **include_stack_ptr;
  803.  
  804.     int ifdef_stack[IFDEF_STACK_SIZE];
  805.     int *ifdef_stack_ptr;
  806.  
  807.     /* included files enclosed with #ifndef MACRO */
  808.     int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
  809.     CachedInclude **cached_includes;
  810.     int nb_cached_includes;
  811.  
  812.     /* #pragma pack stack */
  813.     int pack_stack[PACK_STACK_SIZE];
  814.     int *pack_stack_ptr;
  815.     char **pragma_libs;
  816.     int nb_pragma_libs;
  817.  
  818.     /* inline functions are stored as token lists and compiled last
  819.        only if referenced */
  820.     struct InlineFunc **inline_fns;
  821.     int nb_inline_fns;
  822.  
  823.     /* sections */
  824.     Section **sections;
  825.     int nb_sections; /* number of sections, including first dummy section */
  826.  
  827.     Section **priv_sections;
  828.     int nb_priv_sections; /* number of private sections */
  829.  
  830.     /* got & plt handling */
  831.     Section *got;
  832.     Section *plt;
  833.     struct sym_attr *sym_attrs;
  834.     int nb_sym_attrs;
  835.     /* give the correspondance from symtab indexes to dynsym indexes */
  836.     int *symtab_to_dynsym;
  837.  
  838.     /* temporary dynamic symbol sections (for dll loading) */
  839.     Section *dynsymtab_section;
  840.     /* exported dynamic symbol section */
  841.     Section *dynsym;
  842.     /* copy of the gobal symtab_section variable */
  843.     Section *symtab;
  844.     /* tiny assembler state */
  845.     Sym *asm_labels;
  846.  
  847. #if defined (TCC_TARGET_PE) || defined (TCC_TARGET_MEOS)
  848.     /* PE info */
  849.     int pe_subsystem;
  850.     unsigned pe_file_align;
  851.     unsigned pe_stack_size;
  852. # ifdef TCC_TARGET_X86_64
  853.     Section *uw_pdata;
  854.     int uw_sym;
  855.     unsigned uw_offs;
  856. # endif
  857. #endif
  858.  
  859. #ifdef TCC_IS_NATIVE
  860.     const char *runtime_main;
  861.     /* for tcc_relocate */
  862.     void *runtime_mem;
  863. # ifdef HAVE_SELINUX
  864.     void *write_mem;
  865.     unsigned long mem_size;
  866. # endif
  867. #endif
  868.  
  869.     /* used by main and tcc_parse_args only */
  870.     char **files; /* files seen on command line */
  871.     int nb_files; /* number thereof */
  872.     int nb_libraries; /* number of libs thereof */
  873.     char *outfile; /* output filename */
  874.     char *option_m; /* only -m32/-m64 handled */
  875.     int print_search_dirs; /* option */
  876.     int option_r; /* option -r */
  877.     int option_C; /* option -C, keep comments when -E */
  878.     int do_bench; /* option -bench */
  879.     int gen_deps; /* option -MD  */
  880.     char *deps_outfile; /* option -MF */
  881. #if defined(TCC_TARGET_MEOS) && !defined (TCC_TARGET_KX)
  882.     int nobss; /* option -nobss, omit BSS section (KolibriOS-only) */
  883. #endif
  884.     ParseArgsState *parse_args_state;
  885. };
  886.  
  887. /* The current value can be: */
  888. #define VT_VALMASK   0x003f  /* mask for value location, register or: */
  889. #define VT_CONST     0x0030  /* constant in vc (must be first non register value) */
  890. #define VT_LLOCAL    0x0031  /* lvalue, offset on stack */
  891. #define VT_LOCAL     0x0032  /* offset on stack */
  892. #define VT_CMP       0x0033  /* the value is stored in processor flags (in vc) */
  893. #define VT_JMP       0x0034  /* value is the consequence of jmp true (even) */
  894. #define VT_JMPI      0x0035  /* value is the consequence of jmp false (odd) */
  895. #define VT_REF       0x0040  /* value is pointer to structure rather than address */
  896. #define VT_LVAL      0x0100  /* var is an lvalue */
  897. #define VT_SYM       0x0200  /* a symbol value is added */
  898. #define VT_MUSTCAST  0x0400  /* value must be casted to be correct (used for
  899.                                 char/short stored in integer registers) */
  900. #define VT_MUSTBOUND 0x0800  /* bound checking must be done before
  901.                                 dereferencing value */
  902. #define VT_BOUNDED   0x8000  /* value is bounded. The address of the
  903.                                 bounding function call point is in vc */
  904. #define VT_LVAL_BYTE     0x1000  /* lvalue is a byte */
  905. #define VT_LVAL_SHORT    0x2000  /* lvalue is a short */
  906. #define VT_LVAL_UNSIGNED 0x4000  /* lvalue is unsigned */
  907. #define VT_LVAL_TYPE     (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
  908.  
  909. /* types */
  910. #define VT_BTYPE       0x000f  /* mask for basic type */
  911. #define VT_INT              0  /* integer type */
  912. #define VT_BYTE             1  /* signed byte type */
  913. #define VT_SHORT            2  /* short type */
  914. #define VT_VOID             3  /* void type */
  915. #define VT_PTR              4  /* pointer */
  916. #define VT_ENUM             5  /* enum definition */
  917. #define VT_FUNC             6  /* function type */
  918. #define VT_STRUCT           7  /* struct/union definition */
  919. #define VT_FLOAT            8  /* IEEE float */
  920. #define VT_DOUBLE           9  /* IEEE double */
  921. #define VT_LDOUBLE         10  /* IEEE long double */
  922. #define VT_BOOL            11  /* ISOC99 boolean type */
  923. #define VT_LLONG           12  /* 64 bit integer */
  924. #define VT_LONG            13  /* long integer (NEVER USED as type, only
  925.                                   during parsing) */
  926. #define VT_QLONG           14  /* 128-bit integer. Only used for x86-64 ABI */
  927. #define VT_QFLOAT          15  /* 128-bit float. Only used for x86-64 ABI */
  928. #define VT_UNSIGNED    0x0010  /* unsigned type */
  929. #define VT_ARRAY       0x0020  /* array type (also has VT_PTR) */
  930. #define VT_BITFIELD    0x0040  /* bitfield modifier */
  931. #define VT_CONSTANT    0x0800  /* const modifier */
  932. #define VT_VOLATILE    0x1000  /* volatile modifier */
  933. #define VT_DEFSIGN     0x2000  /* signed type */
  934. #define VT_VLA     0x00020000  /* VLA type (also has VT_PTR and VT_ARRAY) */
  935.  
  936. /* storage */
  937. #define VT_EXTERN  0x00000080  /* extern definition */
  938. #define VT_STATIC  0x00000100  /* static variable */
  939. #define VT_TYPEDEF 0x00000200  /* typedef definition */
  940. #define VT_INLINE  0x00000400  /* inline definition */
  941. #define VT_IMPORT  0x00004000  /* win32: extern data imported from dll */
  942. #define VT_EXPORT  0x00008000  /* win32: data exported from dll */
  943. #define VT_WEAK    0x00010000  /* weak symbol */
  944. #define VT_TLS     0x00040000  /* thread-local storage */
  945. #define VT_VIS_SHIFT    19     /* shift for symbol visibility, overlapping
  946.                                   bitfield values, because bitfields never
  947.                                   have linkage and hence never have
  948.                                   visibility.  */
  949. #define VT_VIS_SIZE      2     /* We have four visibilities.  */
  950. #define VT_VIS_MASK (((1 << VT_VIS_SIZE)-1) << VT_VIS_SHIFT)
  951.  
  952. #define VT_STRUCT_SHIFT 19     /* shift for bitfield shift values (max: 32 - 2*6) */
  953.  
  954.  
  955. /* type mask (except storage) */
  956. #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK | VT_VIS_MASK)
  957. #define VT_TYPE (~(VT_STORAGE))
  958.  
  959. /* token values */
  960.  
  961. /* warning: the following compare tokens depend on i386 asm code */
  962. #define TOK_ULT 0x92
  963. #define TOK_UGE 0x93
  964. #define TOK_EQ  0x94
  965. #define TOK_NE  0x95
  966. #define TOK_ULE 0x96
  967. #define TOK_UGT 0x97
  968. #define TOK_Nset 0x98
  969. #define TOK_Nclear 0x99
  970. #define TOK_LT  0x9c
  971. #define TOK_GE  0x9d
  972. #define TOK_LE  0x9e
  973. #define TOK_GT  0x9f
  974.  
  975. #define TOK_LAND  0xa0
  976. #define TOK_LOR   0xa1
  977. #define TOK_DEC   0xa2
  978. #define TOK_MID   0xa3 /* inc/dec, to void constant */
  979. #define TOK_INC   0xa4
  980. #define TOK_UDIV  0xb0 /* unsigned division */
  981. #define TOK_UMOD  0xb1 /* unsigned modulo */
  982. #define TOK_PDIV  0xb2 /* fast division with undefined rounding for pointers */
  983.  
  984. /* tokens that carry values (in additional token string space / tokc) --> */
  985. #define TOK_CCHAR   0xb3 /* char constant in tokc */
  986. #define TOK_LCHAR   0xb4
  987. #define TOK_CINT    0xb5 /* number in tokc */
  988. #define TOK_CUINT   0xb6 /* unsigned int constant */
  989. #define TOK_CLLONG  0xb7 /* long long constant */
  990. #define TOK_CULLONG 0xb8 /* unsigned long long constant */
  991. #define TOK_STR     0xb9 /* pointer to string in tokc */
  992. #define TOK_LSTR    0xba
  993. #define TOK_CFLOAT  0xbb /* float constant */
  994. #define TOK_CDOUBLE 0xbc /* double constant */
  995. #define TOK_CLDOUBLE 0xbd /* long double constant */
  996. #define TOK_PPNUM   0xbe /* preprocessor number */
  997. #define TOK_PPSTR   0xbf /* preprocessor string */
  998. #define TOK_LINENUM 0xc0 /* line number info */
  999. /* <-- */
  1000.  
  1001. #define TOK_UMULL    0xc2 /* unsigned 32x32 -> 64 mul */
  1002. #define TOK_ADDC1    0xc3 /* add with carry generation */
  1003. #define TOK_ADDC2    0xc4 /* add with carry use */
  1004. #define TOK_SUBC1    0xc5 /* add with carry generation */
  1005. #define TOK_SUBC2    0xc6 /* add with carry use */
  1006. #define TOK_ARROW    0xc7
  1007. #define TOK_DOTS     0xc8 /* three dots */
  1008. #define TOK_SHR      0xc9 /* unsigned shift right */
  1009. #define TOK_TWOSHARPS 0xca /* ## preprocessing token */
  1010. #define TOK_PLCHLDR  0xcb /* placeholder token as defined in C99 */
  1011. #define TOK_NOSUBST  0xcc /* means following token has already been pp'd */
  1012.  
  1013. #define TOK_SHL   0x01 /* shift left */
  1014. #define TOK_SAR   0x02 /* signed shift right */
  1015.  
  1016. /* assignement operators : normal operator or 0x80 */
  1017. #define TOK_A_MOD 0xa5
  1018. #define TOK_A_AND 0xa6
  1019. #define TOK_A_MUL 0xaa
  1020. #define TOK_A_ADD 0xab
  1021. #define TOK_A_SUB 0xad
  1022. #define TOK_A_DIV 0xaf
  1023. #define TOK_A_XOR 0xde
  1024. #define TOK_A_OR  0xfc
  1025. #define TOK_A_SHL 0x81
  1026. #define TOK_A_SAR 0x82
  1027.  
  1028. #ifndef offsetof
  1029. #define offsetof(type, field) ((size_t) &((type *)0)->field)
  1030. #endif
  1031.  
  1032. #ifndef countof
  1033. #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
  1034. #endif
  1035.  
  1036. #define TOK_EOF       (-1)  /* end of file */
  1037. #define TOK_LINEFEED  10    /* line feed */
  1038.  
  1039. /* all identificators and strings have token above that */
  1040. #define TOK_IDENT 256
  1041.  
  1042. #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
  1043. #define TOK_ASM_int TOK_INT
  1044. #define DEF_ASMDIR(x) DEF(TOK_ASMDIR_ ## x, "." #x)
  1045. #define TOK_ASMDIR_FIRST TOK_ASMDIR_byte
  1046. #define TOK_ASMDIR_LAST TOK_ASMDIR_section
  1047.  
  1048. #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
  1049. /* only used for i386 asm opcodes definitions */
  1050. #define DEF_BWL(x) \
  1051.  DEF(TOK_ASM_ ## x ## b, #x "b") \
  1052.  DEF(TOK_ASM_ ## x ## w, #x "w") \
  1053.  DEF(TOK_ASM_ ## x ## l, #x "l") \
  1054.  DEF(TOK_ASM_ ## x, #x)
  1055. #define DEF_WL(x) \
  1056.  DEF(TOK_ASM_ ## x ## w, #x "w") \
  1057.  DEF(TOK_ASM_ ## x ## l, #x "l") \
  1058.  DEF(TOK_ASM_ ## x, #x)
  1059. #ifdef TCC_TARGET_X86_64
  1060. # define DEF_BWLQ(x) \
  1061.  DEF(TOK_ASM_ ## x ## b, #x "b") \
  1062.  DEF(TOK_ASM_ ## x ## w, #x "w") \
  1063.  DEF(TOK_ASM_ ## x ## l, #x "l") \
  1064.  DEF(TOK_ASM_ ## x ## q, #x "q") \
  1065.  DEF(TOK_ASM_ ## x, #x)
  1066. # define DEF_WLQ(x) \
  1067.  DEF(TOK_ASM_ ## x ## w, #x "w") \
  1068.  DEF(TOK_ASM_ ## x ## l, #x "l") \
  1069.  DEF(TOK_ASM_ ## x ## q, #x "q") \
  1070.  DEF(TOK_ASM_ ## x, #x)
  1071. # define DEF_BWLX DEF_BWLQ
  1072. # define DEF_WLX DEF_WLQ
  1073. /* number of sizes + 1 */
  1074. # define NBWLX 5
  1075. #else
  1076. # define DEF_BWLX DEF_BWL
  1077. # define DEF_WLX DEF_WL
  1078. /* number of sizes + 1 */
  1079. # define NBWLX 4
  1080. #endif
  1081.  
  1082. #define DEF_FP1(x) \
  1083.  DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
  1084.  DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
  1085.  DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
  1086.  DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
  1087.  
  1088. #define DEF_FP(x) \
  1089.  DEF(TOK_ASM_ ## f ## x, "f" #x ) \
  1090.  DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
  1091.  DEF_FP1(x)
  1092.  
  1093. #define DEF_ASMTEST(x,suffix) \
  1094.  DEF_ASM(x ## o ## suffix) \
  1095.  DEF_ASM(x ## no ## suffix) \
  1096.  DEF_ASM(x ## b ## suffix) \
  1097.  DEF_ASM(x ## c ## suffix) \
  1098.  DEF_ASM(x ## nae ## suffix) \
  1099.  DEF_ASM(x ## nb ## suffix) \
  1100.  DEF_ASM(x ## nc ## suffix) \
  1101.  DEF_ASM(x ## ae ## suffix) \
  1102.  DEF_ASM(x ## e ## suffix) \
  1103.  DEF_ASM(x ## z ## suffix) \
  1104.  DEF_ASM(x ## ne ## suffix) \
  1105.  DEF_ASM(x ## nz ## suffix) \
  1106.  DEF_ASM(x ## be ## suffix) \
  1107.  DEF_ASM(x ## na ## suffix) \
  1108.  DEF_ASM(x ## nbe ## suffix) \
  1109.  DEF_ASM(x ## a ## suffix) \
  1110.  DEF_ASM(x ## s ## suffix) \
  1111.  DEF_ASM(x ## ns ## suffix) \
  1112.  DEF_ASM(x ## p ## suffix) \
  1113.  DEF_ASM(x ## pe ## suffix) \
  1114.  DEF_ASM(x ## np ## suffix) \
  1115.  DEF_ASM(x ## po ## suffix) \
  1116.  DEF_ASM(x ## l ## suffix) \
  1117.  DEF_ASM(x ## nge ## suffix) \
  1118.  DEF_ASM(x ## nl ## suffix) \
  1119.  DEF_ASM(x ## ge ## suffix) \
  1120.  DEF_ASM(x ## le ## suffix) \
  1121.  DEF_ASM(x ## ng ## suffix) \
  1122.  DEF_ASM(x ## nle ## suffix) \
  1123.  DEF_ASM(x ## g ## suffix)
  1124.  
  1125. #endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
  1126.  
  1127. enum tcc_token {
  1128.     TOK_LAST = TOK_IDENT - 1
  1129. #define DEF(id, str) ,id
  1130. #include "tcctok.h"
  1131. #undef DEF
  1132. };
  1133.  
  1134. #define TOK_UIDENT TOK_DEFINE
  1135.  
  1136. /* space exlcuding newline */
  1137. static inline int is_space(int ch)
  1138. {
  1139.     return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
  1140. }
  1141.  
  1142. static inline int isid(int c)
  1143. {
  1144.     return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
  1145. }
  1146.  
  1147. static inline int isnum(int c)
  1148. {
  1149.     return c >= '0' && c <= '9';
  1150. }
  1151.  
  1152. static inline int isoct(int c)
  1153. {
  1154.     return c >= '0' && c <= '7';
  1155. }
  1156.  
  1157. static inline int toup(int c)
  1158. {
  1159.     return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
  1160. }
  1161.  
  1162. #ifndef PUB_FUNC
  1163. # define PUB_FUNC
  1164. #endif
  1165.  
  1166. #ifdef ONE_SOURCE
  1167. #define ST_INLN static inline
  1168. #define ST_FUNC static
  1169. #define ST_DATA static
  1170. #else
  1171. #define ST_INLN
  1172. #define ST_FUNC
  1173. #define ST_DATA extern
  1174. #endif
  1175.  
  1176. /* ------------ libtcc.c ------------ */
  1177.  
  1178. /* use GNU C extensions */
  1179. ST_DATA int gnu_ext;
  1180. /* use Tiny C extensions */
  1181. ST_DATA int tcc_ext;
  1182. /* XXX: get rid of this ASAP */
  1183. ST_DATA struct TCCState *tcc_state;
  1184.  
  1185. #define AFF_PRINT_ERROR     0x0001 /* print error if file not found */
  1186. #define AFF_REFERENCED_DLL  0x0002 /* load a referenced dll from another dll */
  1187. #define AFF_PREPROCESS      0x0004 /* preprocess file */
  1188.  
  1189. /* public functions currently used by the tcc main function */
  1190. PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
  1191. PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
  1192. PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num);
  1193. PUB_FUNC char *tcc_basename(const char *name);
  1194. PUB_FUNC char *tcc_fileextension (const char *name);
  1195.  
  1196. #ifndef MEM_DEBUG
  1197. PUB_FUNC void tcc_free(void *ptr);
  1198. PUB_FUNC void *tcc_malloc(unsigned long size);
  1199. PUB_FUNC void *tcc_mallocz(unsigned long size);
  1200. PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
  1201. PUB_FUNC char *tcc_strdup(const char *str);
  1202. #else
  1203. #define tcc_free(ptr)           tcc_free_debug(ptr)
  1204. #define tcc_malloc(size)        tcc_malloc_debug(size, __FILE__, __LINE__)
  1205. #define tcc_mallocz(size)       tcc_mallocz_debug(size, __FILE__, __LINE__)
  1206. #define tcc_realloc(ptr,size)   tcc_realloc_debug(ptr, size, __FILE__, __LINE__)
  1207. #define tcc_strdup(str)         tcc_strdup_debug(str, __FILE__, __LINE__)
  1208. PUB_FUNC void tcc_free_debug(void *ptr);
  1209. PUB_FUNC void *tcc_malloc_debug(unsigned long size, const char *file, int line);
  1210. PUB_FUNC void *tcc_mallocz_debug(unsigned long size, const char *file, int line);
  1211. PUB_FUNC void *tcc_realloc_debug(void *ptr, unsigned long size, const char *file, int line);
  1212. PUB_FUNC char *tcc_strdup_debug(const char *str, const char *file, int line);
  1213. #endif
  1214.  
  1215. #define free(p) use_tcc_free(p)
  1216. #define malloc(s) use_tcc_malloc(s)
  1217. #define realloc(p, s) use_tcc_realloc(p, s)
  1218. #undef strdup
  1219. #define strdup(s) use_tcc_strdup(s)
  1220. // { Added by Coldy
  1221. #define tcc_abort() tcc_error(0)
  1222. // }
  1223. PUB_FUNC void tcc_memstats(int bench);
  1224. PUB_FUNC void tcc_error_noabort(const char *fmt, ...);
  1225. PUB_FUNC NORETURN void tcc_error(const char *fmt, ...);
  1226. PUB_FUNC void tcc_warning(const char *fmt, ...);
  1227.  
  1228. /* other utilities */
  1229. ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);
  1230. ST_FUNC void dynarray_reset(void *pp, int *n);
  1231. ST_FUNC void cstr_ccat(CString *cstr, int ch);
  1232. ST_FUNC void cstr_cat(CString *cstr, const char *str, int len);
  1233. ST_FUNC void cstr_wccat(CString *cstr, int ch);
  1234. ST_FUNC void cstr_new(CString *cstr);
  1235. ST_FUNC void cstr_free(CString *cstr);
  1236. ST_FUNC void cstr_reset(CString *cstr);
  1237.  
  1238. ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
  1239. ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
  1240. ST_FUNC void *section_ptr_add(Section *sec, addr_t size);
  1241. ST_FUNC void section_reserve(Section *sec, unsigned long size);
  1242. ST_FUNC Section *find_section(TCCState *s1, const char *name);
  1243.  
  1244. ST_FUNC void put_extern_sym2(Sym *sym, Section *section, addr_t value, unsigned long size, int can_add_underscore);
  1245. ST_FUNC void put_extern_sym(Sym *sym, Section *section, addr_t value, unsigned long size);
  1246. ST_FUNC void greloc(Section *s, Sym *sym, unsigned long offset, int type);
  1247. ST_FUNC void greloca(Section *s, Sym *sym, unsigned long offset, int type, addr_t addend);
  1248.  
  1249. ST_INLN void sym_free(Sym *sym);
  1250. ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c);
  1251. ST_FUNC Sym *sym_find2(Sym *s, int v);
  1252. ST_FUNC Sym *sym_push(int v, CType *type, int r, int c);
  1253. ST_FUNC void sym_pop(Sym **ptop, Sym *b);
  1254. ST_INLN Sym *struct_find(int v);
  1255. ST_INLN Sym *sym_find(int v);
  1256. ST_FUNC Sym *global_identifier_push(int v, int t, int c);
  1257.  
  1258. ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
  1259. ST_FUNC int tcc_open(TCCState *s1, const char *filename);
  1260. ST_FUNC void tcc_close(void);
  1261.  
  1262. ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype);
  1263. ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
  1264.  
  1265. #if !defined(TCC_TARGET_PE) && !defined(TCC_TARGET_MEOS)
  1266. ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
  1267. #endif
  1268.  
  1269. ST_FUNC void tcc_add_pragma_libs(TCCState *s1);
  1270. PUB_FUNC int tcc_add_library_err(TCCState *s, const char *f);
  1271.  
  1272. PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time);
  1273. PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv);
  1274. PUB_FUNC void tcc_set_environment(TCCState *s);
  1275.  
  1276. /* ------------ tccpp.c ------------ */
  1277.  
  1278. ST_DATA struct BufferedFile *file;
  1279. ST_DATA int ch, tok;
  1280. ST_DATA CValue tokc;
  1281. ST_DATA const int *macro_ptr;
  1282. ST_DATA int parse_flags;
  1283. ST_DATA int tok_flags;
  1284. ST_DATA CString tokcstr; /* current parsed string, if any */
  1285.  
  1286. /* display benchmark infos */
  1287. ST_DATA int total_lines;
  1288. ST_DATA int total_bytes;
  1289. ST_DATA int tok_ident;
  1290. ST_DATA TokenSym **table_ident;
  1291.  
  1292. #define TOK_FLAG_BOL   0x0001 /* beginning of line before */
  1293. #define TOK_FLAG_BOF   0x0002 /* beginning of file before */
  1294. #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
  1295. #define TOK_FLAG_EOF   0x0008 /* end of file */
  1296.  
  1297. #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
  1298. #define PARSE_FLAG_TOK_NUM    0x0002 /* return numbers instead of TOK_PPNUM */
  1299. #define PARSE_FLAG_LINEFEED   0x0004 /* line feed is returned as a
  1300.                                         token. line feed is also
  1301.                                         returned at eof */
  1302. #define PARSE_FLAG_ASM_FILE 0x0008 /* we processing an asm file: '#' can be used for line comment, etc. */
  1303. #define PARSE_FLAG_SPACES     0x0010 /* next() returns space tokens (for -E) */
  1304. #define PARSE_FLAG_ACCEPT_STRAYS 0x0020 /* next() returns '\\' token */
  1305. #define PARSE_FLAG_TOK_STR    0x0040 /* return parsed strings instead of TOK_PPSTR */
  1306.  
  1307. ST_FUNC TokenSym *tok_alloc(const char *str, int len);
  1308. ST_FUNC const char *get_tok_str(int v, CValue *cv);
  1309. ST_FUNC void begin_macro(TokenString *str, int alloc);
  1310. ST_FUNC void end_macro(void);
  1311. ST_FUNC void save_parse_state(ParseState *s);
  1312. ST_FUNC void restore_parse_state(ParseState *s);
  1313. ST_INLN void tok_str_new(TokenString *s);
  1314. ST_FUNC void tok_str_free(int *str);
  1315. ST_FUNC void tok_str_add(TokenString *s, int t);
  1316. ST_FUNC void tok_str_add_tok(TokenString *s);
  1317. ST_INLN void define_push(int v, int macro_type, TokenString *str, Sym *first_arg);
  1318. ST_FUNC void define_undef(Sym *s);
  1319. ST_INLN Sym *define_find(int v);
  1320. ST_FUNC void free_defines(Sym *b);
  1321. ST_FUNC void print_defines(void);
  1322. ST_FUNC Sym *label_find(int v);
  1323. ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
  1324. ST_FUNC void label_pop(Sym **ptop, Sym *slast);
  1325. ST_FUNC void parse_define(void);
  1326. ST_FUNC void preprocess(int is_bof);
  1327. ST_FUNC void next_nomacro(void);
  1328. ST_FUNC void next(void);
  1329. ST_INLN void unget_tok(int last_tok);
  1330. ST_FUNC void preprocess_init(TCCState *s1);
  1331. ST_FUNC void preprocess_new(void);
  1332. ST_FUNC void preprocess_delete(void);
  1333. ST_FUNC int tcc_preprocess(TCCState *s1);
  1334. ST_FUNC void skip(int c);
  1335. ST_FUNC NORETURN void expect(const char *msg);
  1336. ST_FUNC char *trimfront(char *p);
  1337. ST_FUNC char *trimback(char *a, char *e);
  1338.  
  1339. /* ------------ tccgen.c ------------ */
  1340.  
  1341. ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
  1342. ST_DATA Section *cur_text_section; /* current section where function code is generated */
  1343. #ifdef CONFIG_TCC_ASM
  1344. ST_DATA Section *last_text_section; /* to handle .previous asm directive */
  1345. #endif
  1346. #ifdef CONFIG_TCC_BCHECK
  1347. /* bound check related sections */
  1348. ST_DATA Section *bounds_section; /* contains global data bound description */
  1349. ST_DATA Section *lbounds_section; /* contains local data bound description */
  1350. #endif
  1351. /* symbol sections */
  1352. ST_DATA Section *symtab_section, *strtab_section;
  1353. /* debug sections */
  1354. ST_DATA Section *stab_section, *stabstr_section;
  1355.  
  1356. #define SYM_POOL_NB (8192 / sizeof(Sym))
  1357. ST_DATA Sym *sym_free_first;
  1358. ST_DATA void **sym_pools;
  1359. ST_DATA int nb_sym_pools;
  1360.  
  1361. ST_DATA Sym *global_stack;
  1362. ST_DATA Sym *local_stack;
  1363. ST_DATA Sym *local_label_stack;
  1364. ST_DATA Sym *global_label_stack;
  1365. ST_DATA Sym *define_stack;
  1366. ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
  1367. ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop, *pvtop;
  1368. #define vstack  (__vstack + 1)
  1369. ST_DATA int rsym, anon_sym, ind, loc;
  1370.  
  1371. ST_DATA int const_wanted; /* true if constant wanted */
  1372. ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
  1373. ST_DATA int global_expr;  /* true if compound literals must be allocated globally (used during initializers parsing */
  1374. ST_DATA CType func_vt; /* current function return type (used by return instruction) */
  1375. ST_DATA int func_var; /* true if current function is variadic */
  1376. ST_DATA int func_vc;
  1377. ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
  1378. ST_DATA const char *funcname;
  1379.  
  1380. ST_FUNC void check_vstack(void);
  1381. ST_INLN int is_float(int t);
  1382. ST_FUNC int ieee_finite(double d);
  1383. ST_FUNC void test_lvalue(void);
  1384. ST_FUNC void swap(int *p, int *q);
  1385. ST_FUNC void vpushi(int v);
  1386. ST_FUNC Sym *external_global_sym(int v, CType *type, int r);
  1387. ST_FUNC void vset(CType *type, int r, int v);
  1388. ST_FUNC void vswap(void);
  1389. ST_FUNC void vpush_global_sym(CType *type, int v);
  1390. ST_FUNC void vrote(SValue *e, int n);
  1391. ST_FUNC void vrott(int n);
  1392. ST_FUNC void vrotb(int n);
  1393. #ifdef TCC_TARGET_ARM
  1394. ST_FUNC int get_reg_ex(int rc, int rc2);
  1395. ST_FUNC void lexpand_nr(void);
  1396. #endif
  1397. ST_FUNC void vpushv(SValue *v);
  1398. ST_FUNC void save_reg(int r);
  1399. ST_FUNC int get_reg(int rc);
  1400. ST_FUNC void save_regs(int n);
  1401. ST_FUNC void gaddrof(void);
  1402. ST_FUNC int gv(int rc);
  1403. ST_FUNC void gv2(int rc1, int rc2);
  1404. ST_FUNC void vpop(void);
  1405. ST_FUNC void gen_op(int op);
  1406. ST_FUNC int type_size(CType *type, int *a);
  1407. ST_FUNC void mk_pointer(CType *type);
  1408. ST_FUNC void vstore(void);
  1409. ST_FUNC void inc(int post, int c);
  1410. ST_FUNC void parse_asm_str(CString *astr);
  1411. ST_FUNC int lvalue_type(int t);
  1412. ST_FUNC void indir(void);
  1413. ST_FUNC void unary(void);
  1414. ST_FUNC void expr_prod(void);
  1415. ST_FUNC void expr_sum(void);
  1416. ST_FUNC void gexpr(void);
  1417. ST_FUNC int expr_const(void);
  1418. ST_FUNC void gen_inline_functions(void);
  1419. ST_FUNC void decl(int l);
  1420. #if defined CONFIG_TCC_BCHECK || defined TCC_TARGET_C67
  1421. ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size);
  1422. #endif
  1423. #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
  1424. ST_FUNC int classify_x86_64_va_arg(CType *ty);
  1425. #endif
  1426.  
  1427. /* ------------ tccelf.c ------------ */
  1428.  
  1429. #define TCC_OUTPUT_FORMAT_ELF    0 /* default output format: ELF */
  1430. #define TCC_OUTPUT_FORMAT_BINARY 1 /* binary image output */
  1431. #define TCC_OUTPUT_FORMAT_COFF   2 /* COFF */
  1432.  
  1433. #define ARMAG  "!<arch>\012"    /* For COFF and a.out archives */
  1434.  
  1435. typedef struct {
  1436.     unsigned int n_strx;         /* index into string table of name */
  1437.     unsigned char n_type;         /* type of symbol */
  1438.     unsigned char n_other;        /* misc info (usually empty) */
  1439.     unsigned short n_desc;        /* description field */
  1440.     unsigned int n_value;        /* value of symbol */
  1441. } Stab_Sym;
  1442.  
  1443. 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);
  1444.  
  1445. ST_FUNC int put_elf_str(Section *s, const char *sym);
  1446. ST_FUNC int put_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int shndx, const char *name);
  1447. ST_FUNC int add_elf_sym(Section *s, addr_t value, unsigned long size, int info, int other, int sh_num, const char *name);
  1448. ST_FUNC int find_elf_sym(Section *s, const char *name);
  1449. ST_FUNC void put_elf_reloc(Section *symtab, Section *s, unsigned long offset, int type, int symbol);
  1450. ST_FUNC void put_elf_reloca(Section *symtab, Section *s, unsigned long offset, int type, int symbol, addr_t addend);
  1451.  
  1452. ST_FUNC void put_stabs(const char *str, int type, int other, int desc, unsigned long value);
  1453. ST_FUNC void put_stabs_r(const char *str, int type, int other, int desc, unsigned long value, Section *sec, int sym_index);
  1454. ST_FUNC void put_stabn(int type, int other, int desc, int value);
  1455. ST_FUNC void put_stabd(int type, int other, int desc);
  1456.  
  1457. ST_FUNC void relocate_common_syms(void);
  1458. ST_FUNC void relocate_syms(TCCState *s1, int do_resolve);
  1459. ST_FUNC void relocate_section(TCCState *s1, Section *s);
  1460. ST_FUNC void relocate_plt(TCCState *s1);
  1461.  
  1462. ST_FUNC void tcc_add_linker_symbols(TCCState *s1);
  1463. ST_FUNC int tcc_load_object_file(TCCState *s1, int fd, unsigned long file_offset);
  1464. ST_FUNC int tcc_load_archive(TCCState *s1, int fd);
  1465. ST_FUNC void tcc_add_bcheck(TCCState *s1);
  1466.  
  1467. ST_FUNC void build_got_entries(TCCState *s1);
  1468. ST_FUNC void tcc_add_runtime(TCCState *s1);
  1469.  
  1470. ST_FUNC addr_t get_elf_sym_addr(TCCState *s, const char *name, int err);
  1471. #if defined TCC_IS_NATIVE || defined TCC_TARGET_PE || defined TCC_TARGET_MEOS
  1472. ST_FUNC void *tcc_get_symbol_err(TCCState *s, const char *name);
  1473. #endif
  1474.  
  1475. #ifndef TCC_TARGET_PE
  1476. ST_FUNC int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
  1477. ST_FUNC int tcc_load_ldscript(TCCState *s1);
  1478. ST_FUNC uint8_t *parse_comment(uint8_t *p, int skip);
  1479. ST_FUNC void minp(void);
  1480. ST_INLN void inp(void);
  1481. ST_FUNC int handle_eob(void);
  1482. #endif
  1483.  
  1484. /* ------------ xxx-gen.c ------------ */
  1485.  
  1486. ST_DATA const int reg_classes[NB_REGS];
  1487.  
  1488. ST_FUNC void gsym_addr(int t, int a);
  1489. ST_FUNC void gsym(int t);
  1490. ST_FUNC void load(int r, SValue *sv);
  1491. ST_FUNC void store(int r, SValue *v);
  1492. ST_FUNC int gfunc_sret(CType *vt, int variadic, CType *ret, int *align, int *regsize);
  1493. ST_FUNC void gfunc_call(int nb_args);
  1494. ST_FUNC void gfunc_prolog(CType *func_type);
  1495. ST_FUNC void gfunc_epilog(void);
  1496. ST_FUNC int gjmp(int t);
  1497. ST_FUNC void gjmp_addr(int a);
  1498. ST_FUNC int gtst(int inv, int t);
  1499. ST_FUNC void gen_opi(int op);
  1500. ST_FUNC void gen_opf(int op);
  1501. ST_FUNC void gen_cvt_ftoi(int t);
  1502. ST_FUNC void gen_cvt_ftof(int t);
  1503. ST_FUNC void ggoto(void);
  1504. #ifndef TCC_TARGET_C67
  1505. ST_FUNC void o(unsigned int c);
  1506. #endif
  1507. #ifndef TCC_TARGET_ARM
  1508. ST_FUNC void gen_cvt_itof(int t);
  1509. #endif
  1510. ST_FUNC void gen_vla_sp_save(int addr);
  1511. ST_FUNC void gen_vla_sp_restore(int addr);
  1512. ST_FUNC void gen_vla_alloc(CType *type, int align);
  1513.  
  1514. /* ------------ i386-gen.c ------------ */
  1515. #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
  1516. ST_FUNC void g(int c);
  1517. ST_FUNC int oad(int c, int s);
  1518. ST_FUNC void gen_le16(int c);
  1519. ST_FUNC void gen_le32(int c);
  1520. ST_FUNC void gen_addr32(int r, Sym *sym, int c);
  1521. ST_FUNC void gen_addrpc32(int r, Sym *sym, int c);
  1522. #endif
  1523.  
  1524. #ifdef CONFIG_TCC_BCHECK
  1525. ST_FUNC void gen_bounded_ptr_add(void);
  1526. ST_FUNC void gen_bounded_ptr_deref(void);
  1527. #endif
  1528.  
  1529. /* ------------ x86_64-gen.c ------------ */
  1530. #ifdef TCC_TARGET_X86_64
  1531. ST_FUNC void gen_addr64(int r, Sym *sym, int64_t c);
  1532. ST_FUNC void gen_opl(int op);
  1533. #endif
  1534.  
  1535. /* ------------ arm-gen.c ------------ */
  1536. #ifdef TCC_TARGET_ARM
  1537. #if defined(TCC_ARM_EABI) && !defined(CONFIG_TCC_ELFINTERP)
  1538. ST_FUNC char *default_elfinterp(struct TCCState *s);
  1539. #endif
  1540. ST_FUNC void arm_init(struct TCCState *s);
  1541. ST_FUNC uint32_t encbranch(int pos, int addr, int fail);
  1542. ST_FUNC void gen_cvt_itof1(int t);
  1543. #endif
  1544.  
  1545. /* ------------ arm64-gen.c ------------ */
  1546. #ifdef TCC_TARGET_ARM64
  1547. ST_FUNC void gen_cvt_sxtw(void);
  1548. ST_FUNC void gen_opl(int op);
  1549. ST_FUNC void greturn(void);
  1550. ST_FUNC void gen_va_start(void);
  1551. ST_FUNC void gen_va_arg(CType *t);
  1552. ST_FUNC void gen_clear_cache(void);
  1553. #endif
  1554.  
  1555. /* ------------ c67-gen.c ------------ */
  1556. #ifdef TCC_TARGET_C67
  1557. #endif
  1558.  
  1559. /* ------------ tcccoff.c ------------ */
  1560.  
  1561. #ifdef TCC_TARGET_COFF
  1562. ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f);
  1563. ST_FUNC int tcc_load_coff(TCCState * s1, int fd);
  1564. #endif
  1565.  
  1566. /* ------------ tccasm.c ------------ */
  1567. ST_FUNC void asm_instr(void);
  1568. ST_FUNC void asm_global_instr(void);
  1569. #ifdef CONFIG_TCC_ASM
  1570. ST_FUNC int find_constraint(ASMOperand *operands, int nb_operands, const char *name, const char **pp);
  1571. ST_FUNC void asm_expr(TCCState *s1, ExprValue *pe);
  1572. ST_FUNC int asm_int_expr(TCCState *s1);
  1573. ST_FUNC int tcc_assemble(TCCState *s1, int do_preprocess);
  1574. /* ------------ i386-asm.c ------------ */
  1575. ST_FUNC void gen_expr32(ExprValue *pe);
  1576. ST_FUNC void asm_opcode(TCCState *s1, int opcode);
  1577. ST_FUNC void asm_compute_constraints(ASMOperand *operands, int nb_operands, int nb_outputs, const uint8_t *clobber_regs, int *pout_reg);
  1578. ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier);
  1579. ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, int nb_outputs, int is_output, uint8_t *clobber_regs, int out_reg);
  1580. ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str);
  1581. #endif
  1582.  
  1583. /* ------------ tccme.c -------------- */
  1584. #if defined(TCC_TARGET_MEOS)
  1585. int tcc_output_me(TCCState* s1,const char *filename);
  1586. #endif
  1587. /* ------------ tccpe.c -------------- */
  1588. #if defined(TCC_TARGET_PE) || defined(TCC_TARGET_MEOS)
  1589. ST_FUNC int pe_load_file(struct TCCState *s1, const char *filename, int fd);
  1590. ST_FUNC int pe_output_file(TCCState * s1, const char *filename);
  1591. ST_FUNC int pe_putimport(TCCState *s1, int dllindex, const char *name, addr_t value);
  1592. ST_FUNC SValue *pe_getimport(SValue *sv, SValue *v2);
  1593. #ifdef TCC_TARGET_X86_64
  1594. ST_FUNC void pe_add_unwind_data(unsigned start, unsigned end, unsigned stack);
  1595. #endif
  1596. /* symbol properties stored in Elf32_Sym->st_other */
  1597. # define ST_PE_EXPORT 0x10
  1598. # define ST_PE_IMPORT 0x20
  1599. # define ST_PE_STDCALL 0x40
  1600. #endif
  1601.  
  1602. /* ------------ tccrun.c ----------------- */
  1603. #ifdef TCC_IS_NATIVE
  1604. #ifdef CONFIG_TCC_STATIC
  1605. #define RTLD_LAZY       0x001
  1606. #define RTLD_NOW        0x002
  1607. #define RTLD_GLOBAL     0x100
  1608. #define RTLD_DEFAULT    NULL
  1609. /* dummy function for profiling */
  1610. ST_FUNC void *dlopen(const char *filename, int flag);
  1611. ST_FUNC void dlclose(void *p);
  1612. ST_FUNC const char *dlerror(void);
  1613. ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
  1614. #elif !defined _WIN32
  1615. ST_FUNC void *resolve_sym(TCCState *s1, const char *symbol);
  1616. #endif
  1617.  
  1618. #ifdef CONFIG_TCC_BACKTRACE
  1619. ST_DATA int rt_num_callers;
  1620. ST_DATA const char **rt_bound_error_msg;
  1621. ST_DATA void *rt_prog_main;
  1622. ST_FUNC void tcc_set_num_callers(int n);
  1623. #endif
  1624. #endif
  1625.  
  1626. /********************************************************/
  1627. #undef ST_DATA
  1628. #ifdef ONE_SOURCE
  1629. #define ST_DATA static
  1630. #else
  1631. #define ST_DATA
  1632. #endif
  1633. /********************************************************/
  1634. #endif /* _TCC_H */
  1635.