tcc.h 55 KB

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