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