tcc.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #define _GNU_SOURCE
  21. #include "config.h"
  22. #ifdef CONFIG_TCCBOOT
  23. #include "tccboot.h"
  24. #define CONFIG_TCC_STATIC
  25. #else
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <math.h>
  32. #include <signal.h>
  33. #include <fcntl.h>
  34. #include <setjmp.h>
  35. #include <time.h>
  36. #ifdef _WIN32
  37. #include <windows.h>
  38. #include <sys/timeb.h>
  39. #include <io.h> /* open, close etc. */
  40. #include <direct.h> /* getcwd */
  41. #define inline __inline
  42. #define inp next_inp
  43. #endif
  44. #ifndef _WIN32
  45. #include <unistd.h>
  46. #include <sys/time.h>
  47. #include <sys/ucontext.h>
  48. #include <sys/mman.h>
  49. #endif
  50. #endif /* !CONFIG_TCCBOOT */
  51. #ifndef PAGESIZE
  52. #define PAGESIZE 4096
  53. #endif
  54. #include "elf.h"
  55. #include "stab.h"
  56. #ifndef O_BINARY
  57. #define O_BINARY 0
  58. #endif
  59. #include "libtcc.h"
  60. /* parser debug */
  61. //#define PARSE_DEBUG
  62. /* preprocessor debug */
  63. //#define PP_DEBUG
  64. /* include file debug */
  65. //#define INC_DEBUG
  66. //#define MEM_DEBUG
  67. /* assembler debug */
  68. //#define ASM_DEBUG
  69. /* target selection */
  70. //#define TCC_TARGET_I386 /* i386 code generator */
  71. //#define TCC_TARGET_ARM /* ARMv4 code generator */
  72. //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
  73. //#define TCC_TARGET_X86_64 /* x86-64 code generator */
  74. /* default target is I386 */
  75. #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
  76. !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
  77. #define TCC_TARGET_I386
  78. #endif
  79. #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
  80. !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
  81. #define CONFIG_TCC_BCHECK /* enable bound checking code */
  82. #endif
  83. #if defined(_WIN32) && !defined(TCC_TARGET_PE)
  84. #define CONFIG_TCC_STATIC
  85. #endif
  86. /* define it to include assembler support */
  87. #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67) && \
  88. !defined(TCC_TARGET_X86_64)
  89. #define CONFIG_TCC_ASM
  90. #endif
  91. /* object format selection */
  92. #if defined(TCC_TARGET_C67)
  93. #define TCC_TARGET_COFF
  94. #endif
  95. #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
  96. #define CONFIG_TCC_BACKTRACE
  97. #endif
  98. #define FALSE 0
  99. #define false 0
  100. #define TRUE 1
  101. #define true 1
  102. typedef int BOOL;
  103. /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
  104. executables or dlls */
  105. #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
  106. #define INCLUDE_STACK_SIZE 32
  107. #define IFDEF_STACK_SIZE 64
  108. #define VSTACK_SIZE 256
  109. #define STRING_MAX_SIZE 1024
  110. #define PACK_STACK_SIZE 8
  111. #define TOK_HASH_SIZE 8192 /* must be a power of two */
  112. #define TOK_ALLOC_INCR 512 /* must be a power of two */
  113. #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
  114. /* token symbol management */
  115. typedef struct TokenSym {
  116. struct TokenSym *hash_next;
  117. struct Sym *sym_define; /* direct pointer to define */
  118. struct Sym *sym_label; /* direct pointer to label */
  119. struct Sym *sym_struct; /* direct pointer to structure */
  120. struct Sym *sym_identifier; /* direct pointer to identifier */
  121. int tok; /* token number */
  122. int len;
  123. char str[1];
  124. } TokenSym;
  125. #ifdef TCC_TARGET_PE
  126. typedef unsigned short nwchar_t;
  127. #else
  128. typedef int nwchar_t;
  129. #endif
  130. typedef struct CString {
  131. int size; /* size in bytes */
  132. void *data; /* either 'char *' or 'nwchar_t *' */
  133. int size_allocated;
  134. void *data_allocated; /* if non NULL, data has been malloced */
  135. } CString;
  136. /* type definition */
  137. typedef struct CType {
  138. int t;
  139. struct Sym *ref;
  140. } CType;
  141. /* constant value */
  142. typedef union CValue {
  143. long double ld;
  144. double d;
  145. float f;
  146. int i;
  147. unsigned int ui;
  148. unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
  149. long long ll;
  150. unsigned long long ull;
  151. struct CString *cstr;
  152. void *ptr;
  153. int tab[1];
  154. } CValue;
  155. /* value on stack */
  156. typedef struct SValue {
  157. CType type; /* type */
  158. unsigned short r; /* register + flags */
  159. unsigned short r2; /* second register, used for 'long long'
  160. type. If not used, set to VT_CONST */
  161. CValue c; /* constant, if VT_CONST */
  162. struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
  163. } SValue;
  164. /* symbol management */
  165. typedef struct Sym {
  166. int v; /* symbol token */
  167. long r; /* associated register */
  168. long c; /* associated number */
  169. CType type; /* associated type */
  170. struct Sym *next; /* next related symbol */
  171. struct Sym *prev; /* prev symbol in stack */
  172. struct Sym *prev_tok; /* previous symbol for this token */
  173. } Sym;
  174. /* section definition */
  175. /* XXX: use directly ELF structure for parameters ? */
  176. /* special flag to indicate that the section should not be linked to
  177. the other ones */
  178. #define SHF_PRIVATE 0x80000000
  179. /* special flag, too */
  180. #define SECTION_ABS ((void *)1)
  181. typedef struct Section {
  182. unsigned long data_offset; /* current data offset */
  183. unsigned char *data; /* section data */
  184. unsigned long data_allocated; /* used for realloc() handling */
  185. int sh_name; /* elf section name (only used during output) */
  186. int sh_num; /* elf section number */
  187. int sh_type; /* elf section type */
  188. int sh_flags; /* elf section flags */
  189. int sh_info; /* elf section info */
  190. int sh_addralign; /* elf section alignment */
  191. int sh_entsize; /* elf entry size */
  192. unsigned long sh_size; /* section size (only used during output) */
  193. unsigned long sh_addr; /* address at which the section is relocated */
  194. unsigned long sh_offset; /* file offset */
  195. int nb_hashed_syms; /* used to resize the hash table */
  196. struct Section *link; /* link to another section */
  197. struct Section *reloc; /* corresponding section for relocation, if any */
  198. struct Section *hash; /* hash table for symbols */
  199. struct Section *next;
  200. char name[1]; /* section name */
  201. } Section;
  202. typedef struct DLLReference {
  203. int level;
  204. void *handle;
  205. char name[1];
  206. } DLLReference;
  207. /* GNUC attribute definition */
  208. typedef struct AttributeDef {
  209. int aligned;
  210. int packed;
  211. Section *section;
  212. int func_attr; /* calling convention, exports, ... */
  213. } AttributeDef;
  214. /* -------------------------------------------------- */
  215. /* gr: wrappers for casting sym->r for other purposes */
  216. typedef struct {
  217. unsigned
  218. func_call : 8,
  219. func_args : 8,
  220. func_export : 1;
  221. } func_attr_t;
  222. #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
  223. #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
  224. #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
  225. #define INLINE_DEF(r) (*(int **)&(r))
  226. /* -------------------------------------------------- */
  227. #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
  228. #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
  229. #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
  230. /* stored in 'Sym.c' field */
  231. #define FUNC_NEW 1 /* ansi function prototype */
  232. #define FUNC_OLD 2 /* old function prototype */
  233. #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
  234. /* stored in 'Sym.r' field */
  235. #define FUNC_CDECL 0 /* standard c call */
  236. #define FUNC_STDCALL 1 /* pascal c call */
  237. #define FUNC_FASTCALL1 2 /* first param in %eax */
  238. #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
  239. #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
  240. #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
  241. /* field 'Sym.t' for macros */
  242. #define MACRO_OBJ 0 /* object like macro */
  243. #define MACRO_FUNC 1 /* function like macro */
  244. /* field 'Sym.r' for C labels */
  245. #define LABEL_DEFINED 0 /* label is defined */
  246. #define LABEL_FORWARD 1 /* label is forward defined */
  247. #define LABEL_DECLARED 2 /* label is declared but never used */
  248. /* type_decl() types */
  249. #define TYPE_ABSTRACT 1 /* type without variable */
  250. #define TYPE_DIRECT 2 /* type with variable */
  251. #define IO_BUF_SIZE 8192
  252. typedef struct BufferedFile {
  253. uint8_t *buf_ptr;
  254. uint8_t *buf_end;
  255. int fd;
  256. int line_num; /* current line number - here to simplify code */
  257. int ifndef_macro; /* #ifndef macro / #endif search */
  258. int ifndef_macro_saved; /* saved ifndef_macro */
  259. int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
  260. char inc_type; /* type of include */
  261. char inc_filename[512]; /* filename specified by the user */
  262. char filename[1024]; /* current filename - here to simplify code */
  263. unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
  264. } BufferedFile;
  265. #define CH_EOB '\\' /* end of buffer or '\0' char in file */
  266. #define CH_EOF (-1) /* end of file */
  267. /* parsing state (used to save parser state to reparse part of the
  268. source several times) */
  269. typedef struct ParseState {
  270. int *macro_ptr;
  271. int line_num;
  272. int tok;
  273. CValue tokc;
  274. } ParseState;
  275. /* used to record tokens */
  276. typedef struct TokenString {
  277. int *str;
  278. int len;
  279. int allocated_len;
  280. int last_line_num;
  281. } TokenString;
  282. /* include file cache, used to find files faster and also to eliminate
  283. inclusion if the include file is protected by #ifndef ... #endif */
  284. typedef struct CachedInclude {
  285. int ifndef_macro;
  286. int hash_next; /* -1 if none */
  287. char type; /* '"' or '>' to give include type */
  288. char filename[1]; /* path specified in #include */
  289. } CachedInclude;
  290. #define CACHED_INCLUDES_HASH_SIZE 512
  291. #ifdef CONFIG_TCC_ASM
  292. typedef struct ExprValue {
  293. uint32_t v;
  294. Sym *sym;
  295. } ExprValue;
  296. #define MAX_ASM_OPERANDS 30
  297. typedef struct ASMOperand {
  298. int id; /* GCC 3 optionnal identifier (0 if number only supported */
  299. char *constraint;
  300. char asm_str[16]; /* computed asm string for operand */
  301. SValue *vt; /* C value of the expression */
  302. int ref_index; /* if >= 0, gives reference to a output constraint */
  303. int input_index; /* if >= 0, gives reference to an input constraint */
  304. int priority; /* priority, used to assign registers */
  305. int reg; /* if >= 0, register number used for this operand */
  306. int is_llong; /* true if double register value */
  307. int is_memory; /* true if memory operand */
  308. int is_rw; /* for '+' modifier */
  309. } ASMOperand;
  310. #endif
  311. struct TCCState {
  312. int output_type;
  313. BufferedFile **include_stack_ptr;
  314. int *ifdef_stack_ptr;
  315. /* include file handling */
  316. char **include_paths;
  317. int nb_include_paths;
  318. char **sysinclude_paths;
  319. int nb_sysinclude_paths;
  320. CachedInclude **cached_includes;
  321. int nb_cached_includes;
  322. char **library_paths;
  323. int nb_library_paths;
  324. /* array of all loaded dlls (including those referenced by loaded
  325. dlls) */
  326. DLLReference **loaded_dlls;
  327. int nb_loaded_dlls;
  328. /* sections */
  329. Section **sections;
  330. int nb_sections; /* number of sections, including first dummy section */
  331. Section **priv_sections;
  332. int nb_priv_sections; /* number of private sections */
  333. /* got handling */
  334. Section *got;
  335. Section *plt;
  336. unsigned long *got_offsets;
  337. int nb_got_offsets;
  338. /* give the correspondance from symtab indexes to dynsym indexes */
  339. int *symtab_to_dynsym;
  340. /* temporary dynamic symbol sections (for dll loading) */
  341. Section *dynsymtab_section;
  342. /* exported dynamic symbol section */
  343. Section *dynsym;
  344. int nostdinc; /* if true, no standard headers are added */
  345. int nostdlib; /* if true, no standard libraries are added */
  346. int nocommon; /* if true, do not use common symbols for .bss data */
  347. /* if true, static linking is performed */
  348. int static_link;
  349. /* soname as specified on the command line (-soname) */
  350. const char *soname;
  351. /* if true, all symbols are exported */
  352. int rdynamic;
  353. /* if true, only link in referenced objects from archive */
  354. int alacarte_link;
  355. /* address of text section */
  356. unsigned long text_addr;
  357. int has_text_addr;
  358. /* output format, see TCC_OUTPUT_FORMAT_xxx */
  359. int output_format;
  360. /* C language options */
  361. int char_is_unsigned;
  362. int leading_underscore;
  363. /* warning switches */
  364. int warn_write_strings;
  365. int warn_unsupported;
  366. int warn_error;
  367. int warn_none;
  368. int warn_implicit_function_declaration;
  369. /* display some information during compilation */
  370. int verbose;
  371. /* compile with debug symbol (and use them if error during execution) */
  372. int do_debug;
  373. /* compile with built-in memory and bounds checker */
  374. int do_bounds_check;
  375. /* give the path of the tcc libraries */
  376. const char *tcc_lib_path;
  377. /* error handling */
  378. void *error_opaque;
  379. void (*error_func)(void *opaque, const char *msg);
  380. int error_set_jmp_enabled;
  381. jmp_buf error_jmp_buf;
  382. int nb_errors;
  383. /* tiny assembler state */
  384. Sym *asm_labels;
  385. /* see include_stack_ptr */
  386. BufferedFile *include_stack[INCLUDE_STACK_SIZE];
  387. /* see ifdef_stack_ptr */
  388. int ifdef_stack[IFDEF_STACK_SIZE];
  389. /* see cached_includes */
  390. int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
  391. /* pack stack */
  392. int pack_stack[PACK_STACK_SIZE];
  393. int *pack_stack_ptr;
  394. /* output file for preprocessing */
  395. FILE *outfile;
  396. /* for tcc_relocate */
  397. int runtime_added;
  398. #ifdef TCC_TARGET_X86_64
  399. /* write PLT and GOT here */
  400. char *runtime_plt_and_got;
  401. unsigned int runtime_plt_and_got_offset;
  402. #endif
  403. };
  404. /* The current value can be: */
  405. #define VT_VALMASK 0x00ff
  406. #define VT_CONST 0x00f0 /* constant in vc
  407. (must be first non register value) */
  408. #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
  409. #define VT_LOCAL 0x00f2 /* offset on stack */
  410. #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
  411. #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
  412. #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
  413. #define VT_LVAL 0x0100 /* var is an lvalue */
  414. #define VT_SYM 0x0200 /* a symbol value is added */
  415. #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
  416. char/short stored in integer registers) */
  417. #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
  418. dereferencing value */
  419. #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
  420. bounding function call point is in vc */
  421. #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
  422. #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
  423. #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
  424. #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
  425. /* types */
  426. #define VT_INT 0 /* integer type */
  427. #define VT_BYTE 1 /* signed byte type */
  428. #define VT_SHORT 2 /* short type */
  429. #define VT_VOID 3 /* void type */
  430. #define VT_PTR 4 /* pointer */
  431. #define VT_ENUM 5 /* enum definition */
  432. #define VT_FUNC 6 /* function type */
  433. #define VT_STRUCT 7 /* struct/union definition */
  434. #define VT_FLOAT 8 /* IEEE float */
  435. #define VT_DOUBLE 9 /* IEEE double */
  436. #define VT_LDOUBLE 10 /* IEEE long double */
  437. #define VT_BOOL 11 /* ISOC99 boolean type */
  438. #define VT_LLONG 12 /* 64 bit integer */
  439. #define VT_LONG 13 /* long integer (NEVER USED as type, only
  440. during parsing) */
  441. #define VT_BTYPE 0x000f /* mask for basic type */
  442. #define VT_UNSIGNED 0x0010 /* unsigned type */
  443. #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
  444. #define VT_BITFIELD 0x0040 /* bitfield modifier */
  445. #define VT_CONSTANT 0x0800 /* const modifier */
  446. #define VT_VOLATILE 0x1000 /* volatile modifier */
  447. #define VT_SIGNED 0x2000 /* signed type */
  448. /* storage */
  449. #define VT_EXTERN 0x00000080 /* extern definition */
  450. #define VT_STATIC 0x00000100 /* static variable */
  451. #define VT_TYPEDEF 0x00000200 /* typedef definition */
  452. #define VT_INLINE 0x00000400 /* inline definition */
  453. #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
  454. /* type mask (except storage) */
  455. #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
  456. #define VT_TYPE (~(VT_STORAGE))
  457. /* token values */
  458. /* warning: the following compare tokens depend on i386 asm code */
  459. #define TOK_ULT 0x92
  460. #define TOK_UGE 0x93
  461. #define TOK_EQ 0x94
  462. #define TOK_NE 0x95
  463. #define TOK_ULE 0x96
  464. #define TOK_UGT 0x97
  465. #define TOK_Nset 0x98
  466. #define TOK_Nclear 0x99
  467. #define TOK_LT 0x9c
  468. #define TOK_GE 0x9d
  469. #define TOK_LE 0x9e
  470. #define TOK_GT 0x9f
  471. #define TOK_LAND 0xa0
  472. #define TOK_LOR 0xa1
  473. #define TOK_DEC 0xa2
  474. #define TOK_MID 0xa3 /* inc/dec, to void constant */
  475. #define TOK_INC 0xa4
  476. #define TOK_UDIV 0xb0 /* unsigned division */
  477. #define TOK_UMOD 0xb1 /* unsigned modulo */
  478. #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
  479. #define TOK_CINT 0xb3 /* number in tokc */
  480. #define TOK_CCHAR 0xb4 /* char constant in tokc */
  481. #define TOK_STR 0xb5 /* pointer to string in tokc */
  482. #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
  483. #define TOK_LCHAR 0xb7
  484. #define TOK_LSTR 0xb8
  485. #define TOK_CFLOAT 0xb9 /* float constant */
  486. #define TOK_LINENUM 0xba /* line number info */
  487. #define TOK_CDOUBLE 0xc0 /* double constant */
  488. #define TOK_CLDOUBLE 0xc1 /* long double constant */
  489. #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
  490. #define TOK_ADDC1 0xc3 /* add with carry generation */
  491. #define TOK_ADDC2 0xc4 /* add with carry use */
  492. #define TOK_SUBC1 0xc5 /* add with carry generation */
  493. #define TOK_SUBC2 0xc6 /* add with carry use */
  494. #define TOK_CUINT 0xc8 /* unsigned int constant */
  495. #define TOK_CLLONG 0xc9 /* long long constant */
  496. #define TOK_CULLONG 0xca /* unsigned long long constant */
  497. #define TOK_ARROW 0xcb
  498. #define TOK_DOTS 0xcc /* three dots */
  499. #define TOK_SHR 0xcd /* unsigned shift right */
  500. #define TOK_PPNUM 0xce /* preprocessor number */
  501. #define TOK_SHL 0x01 /* shift left */
  502. #define TOK_SAR 0x02 /* signed shift right */
  503. /* assignement operators : normal operator or 0x80 */
  504. #define TOK_A_MOD 0xa5
  505. #define TOK_A_AND 0xa6
  506. #define TOK_A_MUL 0xaa
  507. #define TOK_A_ADD 0xab
  508. #define TOK_A_SUB 0xad
  509. #define TOK_A_DIV 0xaf
  510. #define TOK_A_XOR 0xde
  511. #define TOK_A_OR 0xfc
  512. #define TOK_A_SHL 0x81
  513. #define TOK_A_SAR 0x82
  514. #ifndef offsetof
  515. #define offsetof(type, field) ((size_t) &((type *)0)->field)
  516. #endif
  517. #ifndef countof
  518. #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
  519. #endif
  520. #define TOK_EOF (-1) /* end of file */
  521. #define TOK_LINEFEED 10 /* line feed */
  522. /* all identificators and strings have token above that */
  523. #define TOK_IDENT 256
  524. /* only used for i386 asm opcodes definitions */
  525. #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
  526. #define DEF_BWL(x) \
  527. DEF(TOK_ASM_ ## x ## b, #x "b") \
  528. DEF(TOK_ASM_ ## x ## w, #x "w") \
  529. DEF(TOK_ASM_ ## x ## l, #x "l") \
  530. DEF(TOK_ASM_ ## x, #x)
  531. #define DEF_WL(x) \
  532. DEF(TOK_ASM_ ## x ## w, #x "w") \
  533. DEF(TOK_ASM_ ## x ## l, #x "l") \
  534. DEF(TOK_ASM_ ## x, #x)
  535. #define DEF_FP1(x) \
  536. DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
  537. DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
  538. DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
  539. DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
  540. #define DEF_FP(x) \
  541. DEF(TOK_ASM_ ## f ## x, "f" #x ) \
  542. DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
  543. DEF_FP1(x)
  544. #define DEF_ASMTEST(x) \
  545. DEF_ASM(x ## o) \
  546. DEF_ASM(x ## no) \
  547. DEF_ASM(x ## b) \
  548. DEF_ASM(x ## c) \
  549. DEF_ASM(x ## nae) \
  550. DEF_ASM(x ## nb) \
  551. DEF_ASM(x ## nc) \
  552. DEF_ASM(x ## ae) \
  553. DEF_ASM(x ## e) \
  554. DEF_ASM(x ## z) \
  555. DEF_ASM(x ## ne) \
  556. DEF_ASM(x ## nz) \
  557. DEF_ASM(x ## be) \
  558. DEF_ASM(x ## na) \
  559. DEF_ASM(x ## nbe) \
  560. DEF_ASM(x ## a) \
  561. DEF_ASM(x ## s) \
  562. DEF_ASM(x ## ns) \
  563. DEF_ASM(x ## p) \
  564. DEF_ASM(x ## pe) \
  565. DEF_ASM(x ## np) \
  566. DEF_ASM(x ## po) \
  567. DEF_ASM(x ## l) \
  568. DEF_ASM(x ## nge) \
  569. DEF_ASM(x ## nl) \
  570. DEF_ASM(x ## ge) \
  571. DEF_ASM(x ## le) \
  572. DEF_ASM(x ## ng) \
  573. DEF_ASM(x ## nle) \
  574. DEF_ASM(x ## g)
  575. #define TOK_ASM_int TOK_INT
  576. enum tcc_token {
  577. TOK_LAST = TOK_IDENT - 1,
  578. #define DEF(id, str) id,
  579. #include "tcctok.h"
  580. #undef DEF
  581. };
  582. #define TOK_UIDENT TOK_DEFINE
  583. #ifdef _WIN32
  584. #define snprintf _snprintf
  585. #define vsnprintf _vsnprintf
  586. #ifndef __GNUC__
  587. #define strtold (long double)strtod
  588. #define strtof (float)strtod
  589. #define strtoll (long long)strtol
  590. #endif
  591. #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
  592. || defined(__OpenBSD__)
  593. /* currently incorrect */
  594. long double strtold(const char *nptr, char **endptr)
  595. {
  596. return (long double)strtod(nptr, endptr);
  597. }
  598. float strtof(const char *nptr, char **endptr)
  599. {
  600. return (float)strtod(nptr, endptr);
  601. }
  602. #else
  603. /* XXX: need to define this to use them in non ISOC99 context */
  604. extern float strtof (const char *__nptr, char **__endptr);
  605. extern long double strtold (const char *__nptr, char **__endptr);
  606. #endif
  607. #ifdef _WIN32
  608. #define IS_PATHSEP(c) (c == '/' || c == '\\')
  609. #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
  610. #define PATHCMP stricmp
  611. #else
  612. #define IS_PATHSEP(c) (c == '/')
  613. #define IS_ABSPATH(p) IS_PATHSEP(p[0])
  614. #define PATHCMP strcmp
  615. #endif
  616. void error(const char *fmt, ...);
  617. void error_noabort(const char *fmt, ...);
  618. void warning(const char *fmt, ...);
  619. void tcc_set_lib_path_w32(TCCState *s);
  620. int tcc_set_flag(TCCState *s, const char *flag_name, int value);
  621. void tcc_print_stats(TCCState *s, int64_t total_time);
  622. void tcc_free(void *ptr);
  623. void *tcc_malloc(unsigned long size);
  624. void *tcc_mallocz(unsigned long size);
  625. void *tcc_realloc(void *ptr, unsigned long size);
  626. char *tcc_strdup(const char *str);
  627. char *tcc_basename(const char *name);
  628. char *tcc_fileextension (const char *name);
  629. char *pstrcpy(char *buf, int buf_size, const char *s);
  630. char *pstrcat(char *buf, int buf_size, const char *s);
  631. void dynarray_add(void ***ptab, int *nb_ptr, void *data);
  632. void dynarray_reset(void *pp, int *n);
  633. #ifdef CONFIG_TCC_BACKTRACE
  634. extern int num_callers;
  635. extern const char **rt_bound_error_msg;
  636. #endif
  637. /* true if float/double/long double type */
  638. static inline int is_float(int t)
  639. {
  640. int bt;
  641. bt = t & VT_BTYPE;
  642. return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
  643. }
  644. /* space exlcuding newline */
  645. static inline int is_space(int ch)
  646. {
  647. return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
  648. }