Checkpoint. More refactoring.
[tinycc/k1w1.git] / tcc.h
blob7de4725f19403880fe2baf1e1021fbfed4989192
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 #ifndef __TCC_H__
22 #define __TCC_H__
24 #define _GNU_SOURCE
25 #include "config.h"
27 #ifdef CONFIG_TCCBOOT
29 #include "tccboot.h"
30 #define CONFIG_TCC_STATIC
32 #else
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <math.h>
40 #include <signal.h>
41 #include <fcntl.h>
42 #include <setjmp.h>
43 #include <time.h>
45 #ifdef _WIN32
46 #include <windows.h>
47 #include <sys/timeb.h>
48 #include <io.h> /* open, close etc. */
49 #include <direct.h> /* getcwd */
50 #define inline __inline
51 #define inp next_inp
52 #ifdef _MSC_VER
53 #define __aligned(n) __declspec(align(n))
54 #endif
55 #ifdef _WIN64
56 #define uplong unsigned long long
57 #endif
58 #endif
60 #ifndef _WIN32
61 #include <unistd.h>
62 #include <sys/time.h>
63 #include <sys/ucontext.h>
64 #include <sys/mman.h>
65 #endif
67 #endif /* !CONFIG_TCCBOOT */
69 #ifndef uplong
70 #define uplong unsigned long
71 #endif
73 #ifndef __aligned
74 #define __aligned(n) __attribute__((aligned(n)))
75 #endif
77 #ifndef PAGESIZE
78 #define PAGESIZE 4096
79 #endif
81 #include "elf.h"
82 #include "stab.h"
84 #ifndef O_BINARY
85 #define O_BINARY 0
86 #endif
88 #include "libtcc.h"
90 /* parser debug */
91 //#define PARSE_DEBUG
92 /* preprocessor debug */
93 //#define PP_DEBUG
94 /* include file debug */
95 //#define INC_DEBUG
97 //#define MEM_DEBUG
99 /* assembler debug */
100 //#define ASM_DEBUG
102 /* target selection */
103 //#define TCC_TARGET_I386 /* i386 code generator */
104 //#define TCC_TARGET_ARM /* ARMv4 code generator */
105 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
106 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
108 /* default target is I386 */
109 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
110 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
111 #define TCC_TARGET_I386
112 #endif
114 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
115 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
116 #define CONFIG_TCC_BCHECK /* enable bound checking code */
117 #endif
119 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
120 #define CONFIG_TCC_STATIC
121 #endif
123 /* define it to include assembler support */
124 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67)
125 #define CONFIG_TCC_ASM
126 #endif
128 /* object format selection */
129 #if defined(TCC_TARGET_C67)
130 #define TCC_TARGET_COFF
131 #endif
133 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
134 #define CONFIG_TCC_BACKTRACE
135 #endif
137 #define FALSE 0
138 #define false 0
139 #define TRUE 1
140 #define true 1
141 typedef int BOOL;
143 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
144 executables or dlls */
145 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
147 #define INCLUDE_STACK_SIZE 32
148 #define IFDEF_STACK_SIZE 64
149 #define VSTACK_SIZE 256
150 #define STRING_MAX_SIZE 1024
151 #define PACK_STACK_SIZE 8
153 #define TOK_HASH_SIZE 8192 /* must be a power of two */
154 #define TOK_ALLOC_INCR 512 /* must be a power of two */
155 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
157 /* token symbol management */
158 typedef struct TokenSym {
159 struct TokenSym *hash_next;
160 struct Sym *sym_define; /* direct pointer to define */
161 struct Sym *sym_label; /* direct pointer to label */
162 struct Sym *sym_struct; /* direct pointer to structure */
163 struct Sym *sym_identifier; /* direct pointer to identifier */
164 int tok; /* token number */
165 int len;
166 char str[1];
167 } TokenSym;
169 #ifdef TCC_TARGET_PE
170 typedef unsigned short nwchar_t;
171 #else
172 typedef int nwchar_t;
173 #endif
175 typedef struct CString {
176 int size; /* size in bytes */
177 void *data; /* either 'char *' or 'nwchar_t *' */
178 int size_allocated;
179 void *data_allocated; /* if non NULL, data has been malloced */
180 } CString;
182 /* type definition */
183 typedef struct CType {
184 int t;
185 struct Sym *ref;
186 } CType;
188 /* constant value */
189 typedef union CValue {
190 long double ld;
191 double d;
192 float f;
193 int i;
194 unsigned int ui;
195 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
196 long long ll;
197 unsigned long long ull;
198 struct CString *cstr;
199 void *ptr;
200 int tab[1];
201 } CValue;
203 /* value on stack */
204 typedef struct SValue {
205 CType type; /* type */
206 unsigned short r; /* register + flags */
207 unsigned short r2; /* second register, used for 'long long'
208 type. If not used, set to VT_CONST */
209 CValue c; /* constant, if VT_CONST */
210 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
211 } SValue;
213 /* symbol management */
214 typedef struct Sym {
215 int v; /* symbol token */
216 long r; /* associated register */
217 union {
218 long c; /* associated number */
219 int *d; /* define token stream */
221 CType type; /* associated type */
222 union {
223 struct Sym *next; /* next related symbol */
224 long jnext; /* next jump label */
226 struct Sym *prev; /* prev symbol in stack */
227 struct Sym *prev_tok; /* previous symbol for this token */
228 } Sym;
230 /* section definition */
231 /* XXX: use directly ELF structure for parameters ? */
232 /* special flag to indicate that the section should not be linked to
233 the other ones */
234 #define SHF_PRIVATE 0x80000000
236 /* special flag, too */
237 #define SECTION_ABS ((void *)1)
239 typedef struct Section {
240 unsigned long data_offset; /* current data offset */
241 unsigned char *data; /* section data */
242 unsigned long data_allocated; /* used for realloc() handling */
243 int sh_name; /* elf section name (only used during output) */
244 int sh_num; /* elf section number */
245 int sh_type; /* elf section type */
246 int sh_flags; /* elf section flags */
247 int sh_info; /* elf section info */
248 int sh_addralign; /* elf section alignment */
249 int sh_entsize; /* elf entry size */
250 unsigned long sh_size; /* section size (only used during output) */
251 unsigned long sh_addr; /* address at which the section is relocated */
252 unsigned long sh_offset; /* file offset */
253 int nb_hashed_syms; /* used to resize the hash table */
254 struct Section *link; /* link to another section */
255 struct Section *reloc; /* corresponding section for relocation, if any */
256 struct Section *hash; /* hash table for symbols */
257 struct Section *next;
258 char name[1]; /* section name */
259 } Section;
261 typedef struct DLLReference {
262 int level;
263 void *handle;
264 char name[1];
265 } DLLReference;
267 /* GNUC attribute definition */
268 typedef struct AttributeDef {
269 int aligned;
270 int packed;
271 Section *section;
272 int func_attr; /* calling convention, exports, ... */
273 } AttributeDef;
275 /* -------------------------------------------------- */
276 /* gr: wrappers for casting sym->r for other purposes */
277 typedef struct {
278 unsigned
279 func_call : 8,
280 func_args : 8,
281 func_export : 1,
282 func_import : 1;
283 } func_attr_t;
285 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
286 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
287 #define FUNC_IMPORT(r) (((func_attr_t*)&(r))->func_import)
288 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
289 /* -------------------------------------------------- */
291 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
292 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
293 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
295 /* stored in 'Sym.c' field */
296 #define FUNC_NEW 1 /* ansi function prototype */
297 #define FUNC_OLD 2 /* old function prototype */
298 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
300 /* stored in 'Sym.r' field */
301 #define FUNC_CDECL 0 /* standard c call */
302 #define FUNC_STDCALL 1 /* pascal c call */
303 #define FUNC_FASTCALL1 2 /* first param in %eax */
304 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
305 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
306 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
308 /* field 'Sym.t' for macros */
309 #define MACRO_OBJ 0 /* object like macro */
310 #define MACRO_FUNC 1 /* function like macro */
312 /* field 'Sym.r' for C labels */
313 #define LABEL_DEFINED 0 /* label is defined */
314 #define LABEL_FORWARD 1 /* label is forward defined */
315 #define LABEL_DECLARED 2 /* label is declared but never used */
317 /* type_decl() types */
318 #define TYPE_ABSTRACT 1 /* type without variable */
319 #define TYPE_DIRECT 2 /* type with variable */
321 #define IO_BUF_SIZE 8192
323 typedef struct BufferedFile {
324 uint8_t *buf_ptr;
325 uint8_t *buf_end;
326 int fd;
327 int line_num; /* current line number - here to simplify code */
328 int ifndef_macro; /* #ifndef macro / #endif search */
329 int ifndef_macro_saved; /* saved ifndef_macro */
330 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
331 char inc_type; /* type of include */
332 char inc_filename[512]; /* filename specified by the user */
333 char filename[1024]; /* current filename - here to simplify code */
334 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
335 } BufferedFile;
337 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
338 #define CH_EOF (-1) /* end of file */
340 /* parsing state (used to save parser state to reparse part of the
341 source several times) */
342 typedef struct ParseState {
343 int *macro_ptr;
344 int line_num;
345 int tok;
346 CValue tokc;
347 } ParseState;
349 /* used to record tokens */
350 typedef struct TokenString {
351 int *str;
352 int len;
353 int allocated_len;
354 int last_line_num;
355 } TokenString;
357 /* inline functions */
358 typedef struct InlineFunc {
359 int *token_str;
360 Sym *sym;
361 char filename[1];
362 } InlineFunc;
364 /* include file cache, used to find files faster and also to eliminate
365 inclusion if the include file is protected by #ifndef ... #endif */
366 typedef struct CachedInclude {
367 int ifndef_macro;
368 int hash_next; /* -1 if none */
369 char type; /* '"' or '>' to give include type */
370 char filename[1]; /* path specified in #include */
371 } CachedInclude;
373 #define CACHED_INCLUDES_HASH_SIZE 512
375 #ifdef CONFIG_TCC_ASM
376 typedef struct ExprValue {
377 uint32_t v;
378 Sym *sym;
379 } ExprValue;
381 #define MAX_ASM_OPERANDS 30
382 typedef struct ASMOperand {
383 int id; /* GCC 3 optionnal identifier (0 if number only supported */
384 char *constraint;
385 char asm_str[16]; /* computed asm string for operand */
386 SValue *vt; /* C value of the expression */
387 int ref_index; /* if >= 0, gives reference to a output constraint */
388 int input_index; /* if >= 0, gives reference to an input constraint */
389 int priority; /* priority, used to assign registers */
390 int reg; /* if >= 0, register number used for this operand */
391 int is_llong; /* true if double register value */
392 int is_memory; /* true if memory operand */
393 int is_rw; /* for '+' modifier */
394 } ASMOperand;
395 #endif
397 struct TCCState {
398 int output_type;
400 BufferedFile **include_stack_ptr;
401 int *ifdef_stack_ptr;
403 /* include file handling */
404 char **include_paths;
405 int nb_include_paths;
406 char **sysinclude_paths;
407 int nb_sysinclude_paths;
408 CachedInclude **cached_includes;
409 int nb_cached_includes;
411 char **library_paths;
412 int nb_library_paths;
414 /* array of all loaded dlls (including those referenced by loaded
415 dlls) */
416 DLLReference **loaded_dlls;
417 int nb_loaded_dlls;
419 /* sections */
420 Section **sections;
421 int nb_sections; /* number of sections, including first dummy section */
423 Section **priv_sections;
424 int nb_priv_sections; /* number of private sections */
426 /* got handling */
427 Section *got;
428 Section *plt;
429 unsigned long *got_offsets;
430 int nb_got_offsets;
431 /* give the correspondance from symtab indexes to dynsym indexes */
432 int *symtab_to_dynsym;
434 /* temporary dynamic symbol sections (for dll loading) */
435 Section *dynsymtab_section;
436 /* exported dynamic symbol section */
437 Section *dynsym;
439 int nostdinc; /* if true, no standard headers are added */
440 int nostdlib; /* if true, no standard libraries are added */
441 int nocommon; /* if true, do not use common symbols for .bss data */
443 /* if true, static linking is performed */
444 int static_link;
446 /* soname as specified on the command line (-soname) */
447 const char *soname;
449 /* if true, all symbols are exported */
450 int rdynamic;
452 /* if true, only link in referenced objects from archive */
453 int alacarte_link;
455 /* address of text section */
456 unsigned long text_addr;
457 int has_text_addr;
459 /* output format, see TCC_OUTPUT_FORMAT_xxx */
460 int output_format;
462 /* C language options */
463 int char_is_unsigned;
464 int leading_underscore;
466 /* warning switches */
467 int warn_write_strings;
468 int warn_unsupported;
469 int warn_error;
470 int warn_none;
471 int warn_implicit_function_declaration;
473 /* display some information during compilation */
474 int verbose;
475 /* compile with debug symbol (and use them if error during execution) */
476 int do_debug;
477 /* compile with built-in memory and bounds checker */
478 int do_bounds_check;
479 /* give the path of the tcc libraries */
480 char *tcc_lib_path;
482 /* error handling */
483 void *error_opaque;
484 void (*error_func)(void *opaque, const char *msg);
485 int error_set_jmp_enabled;
486 #ifdef _WIN64
487 __aligned(16)
488 #endif
489 jmp_buf error_jmp_buf;
490 int nb_errors;
492 /* tiny assembler state */
493 Sym *asm_labels;
495 /* see include_stack_ptr */
496 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
498 /* see ifdef_stack_ptr */
499 int ifdef_stack[IFDEF_STACK_SIZE];
501 /* see cached_includes */
502 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
504 /* pack stack */
505 int pack_stack[PACK_STACK_SIZE];
506 int *pack_stack_ptr;
508 /* output file for preprocessing */
509 FILE *outfile;
511 /* for tcc_relocate */
512 int runtime_added;
514 struct InlineFunc **inline_fns;
515 int nb_inline_fns;
517 #ifdef TCC_TARGET_I386
518 int seg_size;
519 #endif
521 /* section alignment */
522 unsigned long section_align;
524 #ifdef TCC_TARGET_PE
525 /* PE info */
526 int pe_subsystem;
527 unsigned long pe_file_align;
528 #endif
530 #ifndef TCC_TARGET_PE
531 #ifdef TCC_TARGET_X86_64
532 /* write PLT and GOT here */
533 char *runtime_plt_and_got;
534 unsigned int runtime_plt_and_got_offset;
535 #endif
536 #endif
539 /* The current value can be: */
540 #define VT_VALMASK 0x00ff
541 #define VT_CONST 0x00f0 /* constant in vc
542 (must be first non register value) */
543 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
544 #define VT_LOCAL 0x00f2 /* offset on stack */
545 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
546 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
547 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
548 #define VT_LVAL 0x0100 /* var is an lvalue */
549 #define VT_SYM 0x0200 /* a symbol value is added */
550 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
551 char/short stored in integer registers) */
552 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
553 dereferencing value */
554 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
555 bounding function call point is in vc */
556 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
557 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
558 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
559 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
561 /* types */
562 #define VT_INT 0 /* integer type */
563 #define VT_BYTE 1 /* signed byte type */
564 #define VT_SHORT 2 /* short type */
565 #define VT_VOID 3 /* void type */
566 #define VT_PTR 4 /* pointer */
567 #define VT_ENUM 5 /* enum definition */
568 #define VT_FUNC 6 /* function type */
569 #define VT_STRUCT 7 /* struct/union definition */
570 #define VT_FLOAT 8 /* IEEE float */
571 #define VT_DOUBLE 9 /* IEEE double */
572 #define VT_LDOUBLE 10 /* IEEE long double */
573 #define VT_BOOL 11 /* ISOC99 boolean type */
574 #define VT_LLONG 12 /* 64 bit integer */
575 #define VT_LONG 13 /* long integer (NEVER USED as type, only
576 during parsing) */
577 #define VT_BTYPE 0x000f /* mask for basic type */
578 #define VT_UNSIGNED 0x0010 /* unsigned type */
579 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
580 #define VT_BITFIELD 0x0040 /* bitfield modifier */
581 #define VT_CONSTANT 0x0800 /* const modifier */
582 #define VT_VOLATILE 0x1000 /* volatile modifier */
583 #define VT_SIGNED 0x2000 /* signed type */
585 /* storage */
586 #define VT_EXTERN 0x00000080 /* extern definition */
587 #define VT_STATIC 0x00000100 /* static variable */
588 #define VT_TYPEDEF 0x00000200 /* typedef definition */
589 #define VT_INLINE 0x00000400 /* inline definition */
590 #define VT_IMPORT 0x00004000 /* win32: extern data imported from dll */
592 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
594 /* type mask (except storage) */
595 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT)
596 #define VT_TYPE (~(VT_STORAGE))
598 /* token values */
600 /* warning: the following compare tokens depend on i386 asm code */
601 #define TOK_ULT 0x92
602 #define TOK_UGE 0x93
603 #define TOK_EQ 0x94
604 #define TOK_NE 0x95
605 #define TOK_ULE 0x96
606 #define TOK_UGT 0x97
607 #define TOK_Nset 0x98
608 #define TOK_Nclear 0x99
609 #define TOK_LT 0x9c
610 #define TOK_GE 0x9d
611 #define TOK_LE 0x9e
612 #define TOK_GT 0x9f
614 #define TOK_LAND 0xa0
615 #define TOK_LOR 0xa1
617 #define TOK_DEC 0xa2
618 #define TOK_MID 0xa3 /* inc/dec, to void constant */
619 #define TOK_INC 0xa4
620 #define TOK_UDIV 0xb0 /* unsigned division */
621 #define TOK_UMOD 0xb1 /* unsigned modulo */
622 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
623 #define TOK_CINT 0xb3 /* number in tokc */
624 #define TOK_CCHAR 0xb4 /* char constant in tokc */
625 #define TOK_STR 0xb5 /* pointer to string in tokc */
626 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
627 #define TOK_LCHAR 0xb7
628 #define TOK_LSTR 0xb8
629 #define TOK_CFLOAT 0xb9 /* float constant */
630 #define TOK_LINENUM 0xba /* line number info */
631 #define TOK_CDOUBLE 0xc0 /* double constant */
632 #define TOK_CLDOUBLE 0xc1 /* long double constant */
633 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
634 #define TOK_ADDC1 0xc3 /* add with carry generation */
635 #define TOK_ADDC2 0xc4 /* add with carry use */
636 #define TOK_SUBC1 0xc5 /* add with carry generation */
637 #define TOK_SUBC2 0xc6 /* add with carry use */
638 #define TOK_CUINT 0xc8 /* unsigned int constant */
639 #define TOK_CLLONG 0xc9 /* long long constant */
640 #define TOK_CULLONG 0xca /* unsigned long long constant */
641 #define TOK_ARROW 0xcb
642 #define TOK_DOTS 0xcc /* three dots */
643 #define TOK_SHR 0xcd /* unsigned shift right */
644 #define TOK_PPNUM 0xce /* preprocessor number */
646 #define TOK_SHL 0x01 /* shift left */
647 #define TOK_SAR 0x02 /* signed shift right */
649 /* assignement operators : normal operator or 0x80 */
650 #define TOK_A_MOD 0xa5
651 #define TOK_A_AND 0xa6
652 #define TOK_A_MUL 0xaa
653 #define TOK_A_ADD 0xab
654 #define TOK_A_SUB 0xad
655 #define TOK_A_DIV 0xaf
656 #define TOK_A_XOR 0xde
657 #define TOK_A_OR 0xfc
658 #define TOK_A_SHL 0x81
659 #define TOK_A_SAR 0x82
661 #ifndef offsetof
662 #define offsetof(type, field) ((size_t) &((type *)0)->field)
663 #endif
665 #ifndef countof
666 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
667 #endif
669 #define TOK_EOF (-1) /* end of file */
670 #define TOK_LINEFEED 10 /* line feed */
672 /* all identificators and strings have token above that */
673 #define TOK_IDENT 256
675 /* only used for i386 asm opcodes definitions */
676 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
678 #define DEF_BWL(x) \
679 DEF(TOK_ASM_ ## x ## b, #x "b") \
680 DEF(TOK_ASM_ ## x ## w, #x "w") \
681 DEF(TOK_ASM_ ## x ## l, #x "l") \
682 DEF(TOK_ASM_ ## x, #x)
684 #define DEF_WL(x) \
685 DEF(TOK_ASM_ ## x ## w, #x "w") \
686 DEF(TOK_ASM_ ## x ## l, #x "l") \
687 DEF(TOK_ASM_ ## x, #x)
689 #ifdef TCC_TARGET_X86_64
691 #define DEF_BWLQ(x) \
692 DEF(TOK_ASM_ ## x ## b, #x "b") \
693 DEF(TOK_ASM_ ## x ## w, #x "w") \
694 DEF(TOK_ASM_ ## x ## l, #x "l") \
695 DEF(TOK_ASM_ ## x ## q, #x "q") \
696 DEF(TOK_ASM_ ## x, #x)
698 #define DEF_WLQ(x) \
699 DEF(TOK_ASM_ ## x ## w, #x "w") \
700 DEF(TOK_ASM_ ## x ## l, #x "l") \
701 DEF(TOK_ASM_ ## x ## q, #x "q") \
702 DEF(TOK_ASM_ ## x, #x)
704 #endif
706 #define DEF_FP1(x) \
707 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
708 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
709 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
710 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
712 #define DEF_FP(x) \
713 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
714 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
715 DEF_FP1(x)
717 #define DEF_ASMTEST(x) \
718 DEF_ASM(x ## o) \
719 DEF_ASM(x ## no) \
720 DEF_ASM(x ## b) \
721 DEF_ASM(x ## c) \
722 DEF_ASM(x ## nae) \
723 DEF_ASM(x ## nb) \
724 DEF_ASM(x ## nc) \
725 DEF_ASM(x ## ae) \
726 DEF_ASM(x ## e) \
727 DEF_ASM(x ## z) \
728 DEF_ASM(x ## ne) \
729 DEF_ASM(x ## nz) \
730 DEF_ASM(x ## be) \
731 DEF_ASM(x ## na) \
732 DEF_ASM(x ## nbe) \
733 DEF_ASM(x ## a) \
734 DEF_ASM(x ## s) \
735 DEF_ASM(x ## ns) \
736 DEF_ASM(x ## p) \
737 DEF_ASM(x ## pe) \
738 DEF_ASM(x ## np) \
739 DEF_ASM(x ## po) \
740 DEF_ASM(x ## l) \
741 DEF_ASM(x ## nge) \
742 DEF_ASM(x ## nl) \
743 DEF_ASM(x ## ge) \
744 DEF_ASM(x ## le) \
745 DEF_ASM(x ## ng) \
746 DEF_ASM(x ## nle) \
747 DEF_ASM(x ## g)
749 #define TOK_ASM_int TOK_INT
751 enum tcc_token {
752 TOK_LAST = TOK_IDENT - 1,
753 #define DEF(id, str) id,
754 #include "tcctok.h"
755 #undef DEF
758 #define TOK_UIDENT TOK_DEFINE
760 #ifdef _WIN32
761 #define snprintf _snprintf
762 #define vsnprintf _vsnprintf
763 #ifndef __GNUC__
764 #define strtold (long double)strtod
765 #define strtof (float)strtod
766 #define strtoll (long long)strtol
767 #endif
768 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
769 || defined(__OpenBSD__)
770 /* currently incorrect */
771 long double strtold(const char *nptr, char **endptr)
773 return (long double)strtod(nptr, endptr);
775 float strtof(const char *nptr, char **endptr)
777 return (float)strtod(nptr, endptr);
779 #else
780 /* XXX: need to define this to use them in non ISOC99 context */
781 extern float strtof (const char *__nptr, char **__endptr);
782 extern long double strtold (const char *__nptr, char **__endptr);
783 #endif
785 #ifdef _WIN32
786 #define IS_PATHSEP(c) (c == '/' || c == '\\')
787 #define IS_ABSPATH(p) (IS_PATHSEP(p[0]) || (p[0] && p[1] == ':' && IS_PATHSEP(p[2])))
788 #define PATHCMP stricmp
789 #else
790 #define IS_PATHSEP(c) (c == '/')
791 #define IS_ABSPATH(p) IS_PATHSEP(p[0])
792 #define PATHCMP strcmp
793 #endif
795 void error(const char *fmt, ...);
796 void error_noabort(const char *fmt, ...);
797 void warning(const char *fmt, ...);
799 void tcc_set_lib_path_w32(TCCState *s);
800 int tcc_set_flag(TCCState *s, const char *flag_name, int value);
801 void tcc_print_stats(TCCState *s, int64_t total_time);
803 void tcc_free(void *ptr);
804 void *tcc_malloc(unsigned long size);
805 void *tcc_mallocz(unsigned long size);
806 void *tcc_realloc(void *ptr, unsigned long size);
807 char *tcc_strdup(const char *str);
809 char *tcc_basename(const char *name);
810 char *tcc_fileextension (const char *name);
811 char *pstrcpy(char *buf, int buf_size, const char *s);
812 char *pstrcat(char *buf, int buf_size, const char *s);
813 void dynarray_add(void ***ptab, int *nb_ptr, void *data);
814 void dynarray_reset(void *pp, int *n);
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';
830 static inline int isid(int c)
832 return (c >= 'a' && c <= 'z')
833 || (c >= 'A' && c <= 'Z')
834 || c == '_';
837 static inline int isnum(int c)
839 return c >= '0' && c <= '9';
842 static inline int isoct(int c)
844 return c >= '0' && c <= '7';
847 static inline int toup(int c)
849 if (c >= 'a' && c <= 'z')
850 return c - 'a' + 'A';
851 else
852 return c;
855 /* type handling */
856 int type_size(CType *type, int *a);
857 inline CType *pointed_type(CType *type);
858 int pointed_size(CType *type);
859 int lvalue_type(int t);
860 int parse_btype(CType *type, AttributeDef *ad);
861 void type_decl(CType *type, AttributeDef *ad, int *v, int td);
862 int compare_types(CType *type1, CType *type2, int unqualified);
863 int is_compatible_types(CType *type1, CType *type2);
864 int is_compatible_parameter_types(CType *type1, CType *type2);
866 int ieee_finite(double d);
867 void vpushi(int v);
868 void vpushll(long long v);
869 void vrott(int n);
870 void vnrott(int n);
871 void lexpand_nr(void);
872 static void vpush_global_sym(CType *type, int v);
873 void vset(CType *type, int r, int v);
874 void type_to_str(char *buf, int buf_size,
875 CType *type, const char *varstr);
876 Sym *get_sym_ref(CType *type, Section *sec,
877 unsigned long offset, unsigned long size);
878 Sym *external_global_sym(int v, CType *type, int r);
880 int tcc_add_dll(TCCState *s, const char *filename, int flags);
882 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
883 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
884 #define AFF_PREPROCESS 0x0004 /* preprocess file */
885 int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
888 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
889 void free_section(Section *s);
890 void section_realloc(Section *sec, unsigned long new_size);
891 void *section_ptr_add(Section *sec, unsigned long size);
892 Section *find_section(TCCState *s1, const char *name);
893 void put_extern_sym2(
894 Sym *sym, Section *section,
895 unsigned long value, unsigned long size, int can_add_underscore);
896 void put_extern_sym(
897 Sym *sym, Section *section,
898 unsigned long value, unsigned long size);
899 void greloc(Section *s, Sym *sym, unsigned long offset, int type);
901 void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap);
902 void strcat_printf(char *buf, int buf_size, const char *fmt, ...);
904 BufferedFile *tcc_open(TCCState *s1, const char *filename);
905 void tcc_close(BufferedFile *bf);
906 int tcc_compile(TCCState *s1);
908 void expect(const char *msg);
909 void skip(int c);
910 void test_lvalue(void);
911 void *resolve_sym(TCCState *s1, const char *sym);
913 /* interpret a subset of GNU ldscripts to handle the dummy libc.so
914 files */
915 int tcc_load_ldscript(TCCState *s1);
916 /* load a DLL and all referenced DLLs. 'level = 0' means that the DLL
917 is referenced by the user (so it should be added as DT_NEEDED in
918 the generated ELF file) */
919 int tcc_load_dll(TCCState *s1, int fd, const char *filename, int level);
921 /********************************************************/
922 /* global variables */
924 /* display benchmark infos */
925 extern int total_lines;
926 extern int total_bytes;
928 /* parser */
929 extern struct BufferedFile *file;
930 extern int ch, tok;
931 extern CValue tokc;
932 extern CString tokcstr; /* current parsed string, if any */
933 /* additional informations about token */
934 extern int tok_flags;
935 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
936 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
937 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
938 #define TOK_FLAG_EOF 0x0008 /* end of file */
940 extern int *macro_ptr, *macro_ptr_allocated;
941 extern int *unget_saved_macro_ptr;
942 extern int unget_saved_buffer[TOK_MAX_SIZE + 1];
943 extern int unget_buffer_enabled;
944 extern int parse_flags;
945 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
946 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
947 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
948 token. line feed is also
949 returned at eof */
950 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
951 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
953 extern Section *text_section, *data_section, *bss_section; /* predefined sections */
954 extern Section *cur_text_section; /* current section where function code is
955 generated */
956 #ifdef CONFIG_TCC_ASM
957 extern Section *last_text_section; /* to handle .previous asm directive */
958 #endif
959 /* bound check related sections */
960 extern Section *bounds_section; /* contains global data bound description */
961 extern Section *lbounds_section; /* contains local data bound description */
962 /* symbol sections */
963 extern Section *symtab_section, *strtab_section;
965 /* debug sections */
966 extern Section *stab_section, *stabstr_section;
968 /* loc : local variable index
969 ind : output code index
970 rsym: return symbol
971 anon_sym: anonymous symbol index
973 extern int rsym, anon_sym, ind, loc;
974 /* expression generation modifiers */
975 extern int const_wanted; /* true if constant wanted */
976 extern int nocode_wanted; /* true if no code generation wanted for an expression */
977 extern int global_expr; /* true if compound literals must be allocated
978 globally (used during initializers parsing */
979 extern CType func_vt; /* current function return type (used by return
980 instruction) */
981 extern int func_vc;
982 extern int last_line_num, last_ind, func_ind; /* debug last line number and pc */
983 extern int tok_ident;
984 extern TokenSym **table_ident;
985 extern TokenSym *hash_ident[TOK_HASH_SIZE];
986 extern char token_buf[STRING_MAX_SIZE + 1];
987 extern char *funcname;
988 extern Sym *global_stack, *local_stack;
989 extern Sym *define_stack;
990 extern Sym *global_label_stack, *local_label_stack;
991 /* symbol allocator */
992 #define SYM_POOL_NB (8192 / sizeof(Sym))
993 extern Sym *sym_free_first;
994 extern void **sym_pools;
995 extern int nb_sym_pools;
997 extern SValue vstack[VSTACK_SIZE], *vtop;
998 /* some predefined types */
999 extern CType char_pointer_type, func_old_type, int_type;
1001 /* use GNU C extensions */
1002 extern int gnu_ext;
1004 /* use Tiny C extensions */
1005 extern int tcc_ext;
1007 /* max number of callers shown if error */
1008 #ifdef CONFIG_TCC_BACKTRACE
1009 extern int num_callers;
1010 extern const char **rt_bound_error_msg;
1011 extern unsigned long rt_prog_main;
1012 #endif
1014 /* XXX: get rid of this ASAP */
1015 extern struct TCCState *tcc_state;
1018 * Include the target specific definitions.
1020 #ifdef TCC_TARGET_I386
1021 #include "i386-gen.h"
1022 #endif
1024 #ifdef TCC_TARGET_ARM
1025 #include "arm-gen.h"
1026 #endif
1028 #ifdef TCC_TARGET_C67
1029 #include "c67-gen.h"
1030 #endif
1032 #ifdef TCC_TARGET_X86_64
1033 #include "x86_64-gen.h"
1034 #endif
1036 #include "tccpp.h"
1037 #include "tcccoff.h"
1038 #include "tccelf.h"
1039 #include "tcccstring.h"
1040 #include "tccgen.h"
1041 #include "tccasm.h"
1042 #include "tccld.h"
1043 #include "tccsym.h"
1044 #include "tccutil.h"
1046 #ifdef TCC_TARGET_PE
1047 #include "tccpe.h"
1048 #endif
1051 #endif /* __TCC_H__ */