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