tcc.h 55 KB

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