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