move declarations to tcc.h
[tinycc.git] / tcc.h
blobe3041cbc9d8bd1d242482c5a237ef5ed73ed4b2d
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define _GNU_SOURCE
22 #include "config.h"
24 #ifdef CONFIG_TCCBOOT
26 #include "tccboot.h"
27 #define CONFIG_TCC_STATIC
29 #else
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <math.h>
37 #include <signal.h>
38 #include <fcntl.h>
39 #include <setjmp.h>
40 #include <time.h>
42 #ifdef _WIN32
43 #include <windows.h>
44 #include <sys/timeb.h>
45 #ifdef _MSC_VER
46 #define inline __inline
47 #endif
48 #endif
50 #ifndef _WIN32
51 #include <unistd.h>
52 #include <sys/time.h>
53 #include <sys/ucontext.h>
54 #include <sys/mman.h>
55 #endif
57 #endif /* !CONFIG_TCCBOOT */
59 #ifndef PAGESIZE
60 #define PAGESIZE 4096
61 #endif
63 #include "elf.h"
64 #include "stab.h"
66 #ifndef O_BINARY
67 #define O_BINARY 0
68 #endif
70 #include "libtcc.h"
72 /* parser debug */
73 //#define PARSE_DEBUG
74 /* preprocessor debug */
75 //#define PP_DEBUG
76 /* include file debug */
77 //#define INC_DEBUG
79 //#define MEM_DEBUG
81 /* assembler debug */
82 //#define ASM_DEBUG
84 /* target selection */
85 //#define TCC_TARGET_I386 /* i386 code generator */
86 //#define TCC_TARGET_ARM /* ARMv4 code generator */
87 //#define TCC_TARGET_C67 /* TMS320C67xx code generator */
88 //#define TCC_TARGET_X86_64 /* x86-64 code generator */
90 /* default target is I386 */
91 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_ARM) && \
92 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
93 #define TCC_TARGET_I386
94 #endif
96 #if !defined(_WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_ARM) && \
97 !defined(TCC_TARGET_C67) && !defined(TCC_TARGET_X86_64)
98 #define CONFIG_TCC_BCHECK /* enable bound checking code */
99 #endif
101 #if defined(_WIN32) && !defined(TCC_TARGET_PE)
102 #define CONFIG_TCC_STATIC
103 #endif
105 /* define it to include assembler support */
106 #if !defined(TCC_TARGET_ARM) && !defined(TCC_TARGET_C67) && \
107 !defined(TCC_TARGET_X86_64)
108 #define CONFIG_TCC_ASM
109 #endif
111 /* object format selection */
112 #if defined(TCC_TARGET_C67)
113 #define TCC_TARGET_COFF
114 #endif
116 #if !defined(_WIN32) && !defined(CONFIG_TCCBOOT)
117 #define CONFIG_TCC_BACKTRACE
118 #endif
120 #define FALSE 0
121 #define false 0
122 #define TRUE 1
123 #define true 1
124 typedef int BOOL;
126 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
127 executables or dlls */
128 #define CONFIG_TCC_CRT_PREFIX CONFIG_SYSROOT "/usr/lib"
130 #define INCLUDE_STACK_SIZE 32
131 #define IFDEF_STACK_SIZE 64
132 #define VSTACK_SIZE 256
133 #define STRING_MAX_SIZE 1024
134 #define PACK_STACK_SIZE 8
136 #define TOK_HASH_SIZE 8192 /* must be a power of two */
137 #define TOK_ALLOC_INCR 512 /* must be a power of two */
138 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
140 /* token symbol management */
141 typedef struct TokenSym {
142 struct TokenSym *hash_next;
143 struct Sym *sym_define; /* direct pointer to define */
144 struct Sym *sym_label; /* direct pointer to label */
145 struct Sym *sym_struct; /* direct pointer to structure */
146 struct Sym *sym_identifier; /* direct pointer to identifier */
147 int tok; /* token number */
148 int len;
149 char str[1];
150 } TokenSym;
152 #ifdef TCC_TARGET_PE
153 typedef unsigned short nwchar_t;
154 #else
155 typedef int nwchar_t;
156 #endif
158 typedef struct CString {
159 int size; /* size in bytes */
160 void *data; /* either 'char *' or 'nwchar_t *' */
161 int size_allocated;
162 void *data_allocated; /* if non NULL, data has been malloced */
163 } CString;
165 /* type definition */
166 typedef struct CType {
167 int t;
168 struct Sym *ref;
169 } CType;
171 /* constant value */
172 typedef union CValue {
173 long double ld;
174 double d;
175 float f;
176 int i;
177 unsigned int ui;
178 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
179 long long ll;
180 unsigned long long ull;
181 struct CString *cstr;
182 void *ptr;
183 int tab[1];
184 } CValue;
186 /* value on stack */
187 typedef struct SValue {
188 CType type; /* type */
189 unsigned short r; /* register + flags */
190 unsigned short r2; /* second register, used for 'long long'
191 type. If not used, set to VT_CONST */
192 CValue c; /* constant, if VT_CONST */
193 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
194 } SValue;
196 /* symbol management */
197 typedef struct Sym {
198 int v; /* symbol token */
199 long r; /* associated register */
200 long c; /* associated number */
201 CType type; /* associated type */
202 struct Sym *next; /* next related symbol */
203 struct Sym *prev; /* prev symbol in stack */
204 struct Sym *prev_tok; /* previous symbol for this token */
205 } Sym;
207 /* section definition */
208 /* XXX: use directly ELF structure for parameters ? */
209 /* special flag to indicate that the section should not be linked to
210 the other ones */
211 #define SHF_PRIVATE 0x80000000
213 typedef struct Section {
214 unsigned long data_offset; /* current data offset */
215 unsigned char *data; /* section data */
216 unsigned long data_allocated; /* used for realloc() handling */
217 int sh_name; /* elf section name (only used during output) */
218 int sh_num; /* elf section number */
219 int sh_type; /* elf section type */
220 int sh_flags; /* elf section flags */
221 int sh_info; /* elf section info */
222 int sh_addralign; /* elf section alignment */
223 int sh_entsize; /* elf entry size */
224 unsigned long sh_size; /* section size (only used during output) */
225 unsigned long sh_addr; /* address at which the section is relocated */
226 unsigned long sh_offset; /* file offset */
227 int nb_hashed_syms; /* used to resize the hash table */
228 struct Section *link; /* link to another section */
229 struct Section *reloc; /* corresponding section for relocation, if any */
230 struct Section *hash; /* hash table for symbols */
231 struct Section *next;
232 char name[1]; /* section name */
233 } Section;
235 typedef struct DLLReference {
236 int level;
237 void *handle;
238 char name[1];
239 } DLLReference;
241 /* GNUC attribute definition */
242 typedef struct AttributeDef {
243 int aligned;
244 int packed;
245 Section *section;
246 int func_attr; /* calling convention, exports, ... */
247 } AttributeDef;
249 /* -------------------------------------------------- */
250 /* gr: wrappers for casting sym->r for other purposes */
251 typedef struct {
252 unsigned
253 func_call : 8,
254 func_args : 8,
255 func_export : 1;
256 } func_attr_t;
258 #define FUNC_CALL(r) (((func_attr_t*)&(r))->func_call)
259 #define FUNC_EXPORT(r) (((func_attr_t*)&(r))->func_export)
260 #define FUNC_ARGS(r) (((func_attr_t*)&(r))->func_args)
261 #define INLINE_DEF(r) (*(int **)&(r))
262 /* -------------------------------------------------- */
264 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
265 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
266 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
268 /* stored in 'Sym.c' field */
269 #define FUNC_NEW 1 /* ansi function prototype */
270 #define FUNC_OLD 2 /* old function prototype */
271 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
273 /* stored in 'Sym.r' field */
274 #define FUNC_CDECL 0 /* standard c call */
275 #define FUNC_STDCALL 1 /* pascal c call */
276 #define FUNC_FASTCALL1 2 /* first param in %eax */
277 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
278 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
279 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
281 /* field 'Sym.t' for macros */
282 #define MACRO_OBJ 0 /* object like macro */
283 #define MACRO_FUNC 1 /* function like macro */
285 /* field 'Sym.r' for C labels */
286 #define LABEL_DEFINED 0 /* label is defined */
287 #define LABEL_FORWARD 1 /* label is forward defined */
288 #define LABEL_DECLARED 2 /* label is declared but never used */
290 /* type_decl() types */
291 #define TYPE_ABSTRACT 1 /* type without variable */
292 #define TYPE_DIRECT 2 /* type with variable */
294 #define IO_BUF_SIZE 8192
296 typedef struct BufferedFile {
297 uint8_t *buf_ptr;
298 uint8_t *buf_end;
299 int fd;
300 int line_num; /* current line number - here to simplify code */
301 int ifndef_macro; /* #ifndef macro / #endif search */
302 int ifndef_macro_saved; /* saved ifndef_macro */
303 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
304 char inc_type; /* type of include */
305 char inc_filename[512]; /* filename specified by the user */
306 char filename[1024]; /* current filename - here to simplify code */
307 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
308 } BufferedFile;
310 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
311 #define CH_EOF (-1) /* end of file */
313 /* parsing state (used to save parser state to reparse part of the
314 source several times) */
315 typedef struct ParseState {
316 int *macro_ptr;
317 int line_num;
318 int tok;
319 CValue tokc;
320 } ParseState;
322 /* used to record tokens */
323 typedef struct TokenString {
324 int *str;
325 int len;
326 int allocated_len;
327 int last_line_num;
328 } TokenString;
330 /* include file cache, used to find files faster and also to eliminate
331 inclusion if the include file is protected by #ifndef ... #endif */
332 typedef struct CachedInclude {
333 int ifndef_macro;
334 int hash_next; /* -1 if none */
335 char type; /* '"' or '>' to give include type */
336 char filename[1]; /* path specified in #include */
337 } CachedInclude;
339 #define CACHED_INCLUDES_HASH_SIZE 512
341 /* parser */
342 static struct BufferedFile *file;
343 static int ch, tok;
344 static CValue tokc;
345 static CString tokcstr; /* current parsed string, if any */
346 /* additional informations about token */
347 static int tok_flags;
348 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
349 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
350 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
351 #define TOK_FLAG_EOF 0x0008 /* end of file */
353 static int *macro_ptr, *macro_ptr_allocated;
354 static int *unget_saved_macro_ptr;
355 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
356 static int unget_buffer_enabled;
357 static int parse_flags;
358 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
359 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
360 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
361 token. line feed is also
362 returned at eof */
363 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
364 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
366 static Section *text_section, *data_section, *bss_section; /* predefined sections */
367 static Section *cur_text_section; /* current section where function code is
368 generated */
369 #ifdef CONFIG_TCC_ASM
370 static Section *last_text_section; /* to handle .previous asm directive */
371 #endif
372 /* bound check related sections */
373 static Section *bounds_section; /* contains global data bound description */
374 static Section *lbounds_section; /* contains local data bound description */
375 /* symbol sections */
376 static Section *symtab_section, *strtab_section;
378 /* debug sections */
379 static Section *stab_section, *stabstr_section;
381 /* loc : local variable index
382 ind : output code index
383 rsym: return symbol
384 anon_sym: anonymous symbol index
386 static int rsym, anon_sym, ind, loc;
387 /* expression generation modifiers */
388 static int const_wanted; /* true if constant wanted */
389 static int nocode_wanted; /* true if no code generation wanted for an expression */
390 static int global_expr; /* true if compound literals must be allocated
391 globally (used during initializers parsing */
392 static CType func_vt; /* current function return type (used by return
393 instruction) */
394 static int func_vc;
395 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
396 static int tok_ident;
397 static TokenSym **table_ident;
398 static TokenSym *hash_ident[TOK_HASH_SIZE];
399 static char token_buf[STRING_MAX_SIZE + 1];
400 static char *funcname;
401 static Sym *global_stack, *local_stack;
402 static Sym *define_stack;
403 static Sym *global_label_stack, *local_label_stack;
404 /* symbol allocator */
405 #define SYM_POOL_NB (8192 / sizeof(Sym))
406 static Sym *sym_free_first;
407 static void **sym_pools;
408 static int nb_sym_pools;
410 static SValue vstack[VSTACK_SIZE], *vtop;
411 /* some predefined types */
412 static CType char_pointer_type, func_old_type, int_type;
413 /* true if isid(c) || isnum(c) */
414 static unsigned char isidnum_table[256-CH_EOF];
416 /* display some information during compilation */
417 static int verbose = 0;
419 /* compile with debug symbol (and use them if error during execution) */
420 static int do_debug = 0;
422 /* compile with built-in memory and bounds checker */
423 static int do_bounds_check = 0;
425 /* display benchmark infos */
426 #if !defined(LIBTCC)
427 static int do_bench = 0;
428 #endif
429 static int total_lines;
430 static int total_bytes;
432 /* use GNU C extensions */
433 static int gnu_ext = 1;
435 /* use Tiny C extensions */
436 static int tcc_ext = 1;
438 /* max number of callers shown if error */
439 #ifdef CONFIG_TCC_BACKTRACE
440 static int num_callers = 6;
441 static const char **rt_bound_error_msg;
442 #endif
444 /* XXX: get rid of this ASAP */
445 static struct TCCState *tcc_state;
447 /* give the path of the tcc libraries */
448 static const char *tcc_lib_path = CONFIG_TCCDIR;
450 struct TCCState {
451 int output_type;
453 BufferedFile **include_stack_ptr;
454 int *ifdef_stack_ptr;
456 /* include file handling */
457 char **include_paths;
458 int nb_include_paths;
459 char **sysinclude_paths;
460 int nb_sysinclude_paths;
461 CachedInclude **cached_includes;
462 int nb_cached_includes;
464 char **library_paths;
465 int nb_library_paths;
467 /* array of all loaded dlls (including those referenced by loaded
468 dlls) */
469 DLLReference **loaded_dlls;
470 int nb_loaded_dlls;
472 /* sections */
473 Section **sections;
474 int nb_sections; /* number of sections, including first dummy section */
476 Section **priv_sections;
477 int nb_priv_sections; /* number of private sections */
479 /* got handling */
480 Section *got;
481 Section *plt;
482 unsigned long *got_offsets;
483 int nb_got_offsets;
484 /* give the correspondance from symtab indexes to dynsym indexes */
485 int *symtab_to_dynsym;
487 /* temporary dynamic symbol sections (for dll loading) */
488 Section *dynsymtab_section;
489 /* exported dynamic symbol section */
490 Section *dynsym;
492 int nostdinc; /* if true, no standard headers are added */
493 int nostdlib; /* if true, no standard libraries are added */
495 int nocommon; /* if true, do not use common symbols for .bss data */
497 /* if true, static linking is performed */
498 int static_link;
500 /* soname as specified on the command line (-soname) */
501 const char *soname;
503 /* if true, all symbols are exported */
504 int rdynamic;
506 /* if true, only link in referenced objects from archive */
507 int alacarte_link;
509 /* address of text section */
510 unsigned long text_addr;
511 int has_text_addr;
513 /* output format, see TCC_OUTPUT_FORMAT_xxx */
514 int output_format;
516 /* C language options */
517 int char_is_unsigned;
518 int leading_underscore;
520 /* warning switches */
521 int warn_write_strings;
522 int warn_unsupported;
523 int warn_error;
524 int warn_none;
525 int warn_implicit_function_declaration;
527 /* error handling */
528 void *error_opaque;
529 void (*error_func)(void *opaque, const char *msg);
530 int error_set_jmp_enabled;
531 jmp_buf error_jmp_buf;
532 int nb_errors;
534 /* tiny assembler state */
535 Sym *asm_labels;
537 /* see include_stack_ptr */
538 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
540 /* see ifdef_stack_ptr */
541 int ifdef_stack[IFDEF_STACK_SIZE];
543 /* see cached_includes */
544 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
546 /* pack stack */
547 int pack_stack[PACK_STACK_SIZE];
548 int *pack_stack_ptr;
550 /* output file for preprocessing */
551 FILE *outfile;
553 /* for tcc_relocate */
554 int runtime_added;
556 #ifdef TCC_TARGET_X86_64
557 /* write PLT and GOT here */
558 char *runtime_plt_and_got;
559 unsigned int runtime_plt_and_got_offset;
560 #endif
563 /* The current value can be: */
564 #define VT_VALMASK 0x00ff
565 #define VT_CONST 0x00f0 /* constant in vc
566 (must be first non register value) */
567 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
568 #define VT_LOCAL 0x00f2 /* offset on stack */
569 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
570 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
571 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
572 #define VT_LVAL 0x0100 /* var is an lvalue */
573 #define VT_SYM 0x0200 /* a symbol value is added */
574 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
575 char/short stored in integer registers) */
576 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
577 dereferencing value */
578 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
579 bounding function call point is in vc */
580 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
581 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
582 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
583 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
585 /* types */
586 #define VT_INT 0 /* integer type */
587 #define VT_BYTE 1 /* signed byte type */
588 #define VT_SHORT 2 /* short type */
589 #define VT_VOID 3 /* void type */
590 #define VT_PTR 4 /* pointer */
591 #define VT_ENUM 5 /* enum definition */
592 #define VT_FUNC 6 /* function type */
593 #define VT_STRUCT 7 /* struct/union definition */
594 #define VT_FLOAT 8 /* IEEE float */
595 #define VT_DOUBLE 9 /* IEEE double */
596 #define VT_LDOUBLE 10 /* IEEE long double */
597 #define VT_BOOL 11 /* ISOC99 boolean type */
598 #define VT_LLONG 12 /* 64 bit integer */
599 #define VT_LONG 13 /* long integer (NEVER USED as type, only
600 during parsing) */
601 #define VT_BTYPE 0x000f /* mask for basic type */
602 #define VT_UNSIGNED 0x0010 /* unsigned type */
603 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
604 #define VT_BITFIELD 0x0040 /* bitfield modifier */
605 #define VT_CONSTANT 0x0800 /* const modifier */
606 #define VT_VOLATILE 0x1000 /* volatile modifier */
607 #define VT_SIGNED 0x2000 /* signed type */
609 /* storage */
610 #define VT_EXTERN 0x00000080 /* extern definition */
611 #define VT_STATIC 0x00000100 /* static variable */
612 #define VT_TYPEDEF 0x00000200 /* typedef definition */
613 #define VT_INLINE 0x00000400 /* inline definition */
615 #define VT_STRUCT_SHIFT 16 /* shift for bitfield shift values */
617 /* type mask (except storage) */
618 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
619 #define VT_TYPE (~(VT_STORAGE))
621 /* token values */
623 /* warning: the following compare tokens depend on i386 asm code */
624 #define TOK_ULT 0x92
625 #define TOK_UGE 0x93
626 #define TOK_EQ 0x94
627 #define TOK_NE 0x95
628 #define TOK_ULE 0x96
629 #define TOK_UGT 0x97
630 #define TOK_Nset 0x98
631 #define TOK_Nclear 0x99
632 #define TOK_LT 0x9c
633 #define TOK_GE 0x9d
634 #define TOK_LE 0x9e
635 #define TOK_GT 0x9f
637 #define TOK_LAND 0xa0
638 #define TOK_LOR 0xa1
640 #define TOK_DEC 0xa2
641 #define TOK_MID 0xa3 /* inc/dec, to void constant */
642 #define TOK_INC 0xa4
643 #define TOK_UDIV 0xb0 /* unsigned division */
644 #define TOK_UMOD 0xb1 /* unsigned modulo */
645 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
646 #define TOK_CINT 0xb3 /* number in tokc */
647 #define TOK_CCHAR 0xb4 /* char constant in tokc */
648 #define TOK_STR 0xb5 /* pointer to string in tokc */
649 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
650 #define TOK_LCHAR 0xb7
651 #define TOK_LSTR 0xb8
652 #define TOK_CFLOAT 0xb9 /* float constant */
653 #define TOK_LINENUM 0xba /* line number info */
654 #define TOK_CDOUBLE 0xc0 /* double constant */
655 #define TOK_CLDOUBLE 0xc1 /* long double constant */
656 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
657 #define TOK_ADDC1 0xc3 /* add with carry generation */
658 #define TOK_ADDC2 0xc4 /* add with carry use */
659 #define TOK_SUBC1 0xc5 /* add with carry generation */
660 #define TOK_SUBC2 0xc6 /* add with carry use */
661 #define TOK_CUINT 0xc8 /* unsigned int constant */
662 #define TOK_CLLONG 0xc9 /* long long constant */
663 #define TOK_CULLONG 0xca /* unsigned long long constant */
664 #define TOK_ARROW 0xcb
665 #define TOK_DOTS 0xcc /* three dots */
666 #define TOK_SHR 0xcd /* unsigned shift right */
667 #define TOK_PPNUM 0xce /* preprocessor number */
669 #define TOK_SHL 0x01 /* shift left */
670 #define TOK_SAR 0x02 /* signed shift right */
672 /* assignement operators : normal operator or 0x80 */
673 #define TOK_A_MOD 0xa5
674 #define TOK_A_AND 0xa6
675 #define TOK_A_MUL 0xaa
676 #define TOK_A_ADD 0xab
677 #define TOK_A_SUB 0xad
678 #define TOK_A_DIV 0xaf
679 #define TOK_A_XOR 0xde
680 #define TOK_A_OR 0xfc
681 #define TOK_A_SHL 0x81
682 #define TOK_A_SAR 0x82
684 #ifndef offsetof
685 #define offsetof(type, field) ((size_t) &((type *)0)->field)
686 #endif
688 #ifndef countof
689 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
690 #endif
692 /* WARNING: the content of this string encodes token numbers */
693 static char tok_two_chars[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
695 #define TOK_EOF (-1) /* end of file */
696 #define TOK_LINEFEED 10 /* line feed */
698 /* all identificators and strings have token above that */
699 #define TOK_IDENT 256
701 /* only used for i386 asm opcodes definitions */
702 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
704 #define DEF_BWL(x) \
705 DEF(TOK_ASM_ ## x ## b, #x "b") \
706 DEF(TOK_ASM_ ## x ## w, #x "w") \
707 DEF(TOK_ASM_ ## x ## l, #x "l") \
708 DEF(TOK_ASM_ ## x, #x)
710 #define DEF_WL(x) \
711 DEF(TOK_ASM_ ## x ## w, #x "w") \
712 DEF(TOK_ASM_ ## x ## l, #x "l") \
713 DEF(TOK_ASM_ ## x, #x)
715 #define DEF_FP1(x) \
716 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
717 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
718 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
719 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
721 #define DEF_FP(x) \
722 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
723 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
724 DEF_FP1(x)
726 #define DEF_ASMTEST(x) \
727 DEF_ASM(x ## o) \
728 DEF_ASM(x ## no) \
729 DEF_ASM(x ## b) \
730 DEF_ASM(x ## c) \
731 DEF_ASM(x ## nae) \
732 DEF_ASM(x ## nb) \
733 DEF_ASM(x ## nc) \
734 DEF_ASM(x ## ae) \
735 DEF_ASM(x ## e) \
736 DEF_ASM(x ## z) \
737 DEF_ASM(x ## ne) \
738 DEF_ASM(x ## nz) \
739 DEF_ASM(x ## be) \
740 DEF_ASM(x ## na) \
741 DEF_ASM(x ## nbe) \
742 DEF_ASM(x ## a) \
743 DEF_ASM(x ## s) \
744 DEF_ASM(x ## ns) \
745 DEF_ASM(x ## p) \
746 DEF_ASM(x ## pe) \
747 DEF_ASM(x ## np) \
748 DEF_ASM(x ## po) \
749 DEF_ASM(x ## l) \
750 DEF_ASM(x ## nge) \
751 DEF_ASM(x ## nl) \
752 DEF_ASM(x ## ge) \
753 DEF_ASM(x ## le) \
754 DEF_ASM(x ## ng) \
755 DEF_ASM(x ## nle) \
756 DEF_ASM(x ## g)
758 #define TOK_ASM_int TOK_INT
760 enum tcc_token {
761 TOK_LAST = TOK_IDENT - 1,
762 #define DEF(id, str) id,
763 #include "tcctok.h"
764 #undef DEF
767 static const char tcc_keywords[] =
768 #define DEF(id, str) str "\0"
769 #include "tcctok.h"
770 #undef DEF
773 #define TOK_UIDENT TOK_DEFINE
775 #ifdef _WIN32
776 #define snprintf _snprintf
777 #define vsnprintf _vsnprintf
778 #ifndef __GNUC__
779 #define strtold (long double)strtod
780 #define strtof (float)strtod
781 #define strtoll (long long)strtol
782 #endif
783 #elif defined(TCC_UCLIBC) || defined(__FreeBSD__) || defined(__DragonFly__) \
784 || defined(__OpenBSD__)
785 /* currently incorrect */
786 long double strtold(const char *nptr, char **endptr)
788 return (long double)strtod(nptr, endptr);
790 float strtof(const char *nptr, char **endptr)
792 return (float)strtod(nptr, endptr);
794 #else
795 /* XXX: need to define this to use them in non ISOC99 context */
796 extern float strtof (const char *__nptr, char **__endptr);
797 extern long double strtold (const char *__nptr, char **__endptr);
798 #endif
800 static char *pstrcpy(char *buf, int buf_size, const char *s);
801 static char *pstrcat(char *buf, int buf_size, const char *s);
802 static char *tcc_basename(const char *name);
803 static char *tcc_fileextension (const char *p);
805 static void next(void);
806 static void next_nomacro(void);
807 static void next_nomacro_spc(void);
808 static void parse_expr_type(CType *type);
809 static void expr_type(CType *type);
810 static void unary_type(CType *type);
811 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
812 int case_reg, int is_expr);
813 static int expr_const(void);
814 static void expr_eq(void);
815 static void gexpr(void);
816 static void gen_inline_functions(void);
817 static void decl(int l);
818 static void decl_initializer(CType *type, Section *sec, unsigned long c,
819 int first, int size_only);
820 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
821 int has_init, int v, int scope);
822 int gv(int rc);
823 void gv2(int rc1, int rc2);
824 void move_reg(int r, int s);
825 void save_regs(int n);
826 void save_reg(int r);
827 void vpop(void);
828 void vswap(void);
829 void vdup(void);
830 int get_reg(int rc);
831 int get_reg_ex(int rc,int rc2);
833 struct macro_level {
834 struct macro_level *prev;
835 int *p;
838 static void macro_subst(TokenString *tok_str, Sym **nested_list,
839 const int *macro_str, struct macro_level **can_read_stream);
840 void gen_op(int op);
841 void force_charshort_cast(int t);
842 static void gen_cast(CType *type);
843 void vstore(void);
844 static Sym *sym_find(int v);
845 static Sym *sym_push(int v, CType *type, int r, int c);
847 /* type handling */
848 static int type_size(CType *type, int *a);
849 static inline CType *pointed_type(CType *type);
850 static int pointed_size(CType *type);
851 static int lvalue_type(int t);
852 static int parse_btype(CType *type, AttributeDef *ad);
853 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
854 static int compare_types(CType *type1, CType *type2, int unqualified);
855 static int is_compatible_types(CType *type1, CType *type2);
856 static int is_compatible_parameter_types(CType *type1, CType *type2);
858 int ieee_finite(double d);
859 void error(const char *fmt, ...);
860 void vpushi(int v);
861 void vpushll(long long v);
862 void vrott(int n);
863 void vnrott(int n);
864 void lexpand_nr(void);
865 static void vpush_global_sym(CType *type, int v);
866 void vset(CType *type, int r, int v);
867 void type_to_str(char *buf, int buf_size,
868 CType *type, const char *varstr);
869 char *get_tok_str(int v, CValue *cv);
870 static Sym *get_sym_ref(CType *type, Section *sec,
871 unsigned long offset, unsigned long size);
872 static Sym *external_global_sym(int v, CType *type, int r);
874 /* section generation */
875 static void section_realloc(Section *sec, unsigned long new_size);
876 static void *section_ptr_add(Section *sec, unsigned long size);
877 static void put_extern_sym(Sym *sym, Section *section,
878 unsigned long value, unsigned long size);
879 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
880 static int put_elf_str(Section *s, const char *sym);
881 static int put_elf_sym(Section *s,
882 unsigned long value, unsigned long size,
883 int info, int other, int shndx, const char *name);
884 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
885 int info, int other, int sh_num, const char *name);
886 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
887 int type, int symbol);
888 static void put_stabs(const char *str, int type, int other, int desc,
889 unsigned long value);
890 static void put_stabs_r(const char *str, int type, int other, int desc,
891 unsigned long value, Section *sec, int sym_index);
892 static void put_stabn(int type, int other, int desc, int value);
893 static void put_stabd(int type, int other, int desc);
894 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
896 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
897 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
898 #define AFF_PREPROCESS 0x0004 /* preprocess file */
899 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
901 /* tcccoff.c */
902 int tcc_output_coff(TCCState *s1, FILE *f);
904 /* tccpe.c */
905 void *resolve_sym(TCCState *s1, const char *sym, int type);
906 int pe_load_def_file(struct TCCState *s1, int fd);
907 int pe_test_res_file(void *v, int size);
908 int pe_load_res_file(struct TCCState *s1, int fd);
909 void pe_add_runtime(struct TCCState *s1);
910 void pe_guess_outfile(char *objfilename, int output_type);
911 int pe_output_file(struct TCCState *s1, const char *filename);
913 /* tccasm.c */
915 #ifdef CONFIG_TCC_ASM
917 typedef struct ExprValue {
918 uint32_t v;
919 Sym *sym;
920 } ExprValue;
922 #define MAX_ASM_OPERANDS 30
924 typedef struct ASMOperand {
925 int id; /* GCC 3 optionnal identifier (0 if number only supported */
926 char *constraint;
927 char asm_str[16]; /* computed asm string for operand */
928 SValue *vt; /* C value of the expression */
929 int ref_index; /* if >= 0, gives reference to a output constraint */
930 int input_index; /* if >= 0, gives reference to an input constraint */
931 int priority; /* priority, used to assign registers */
932 int reg; /* if >= 0, register number used for this operand */
933 int is_llong; /* true if double register value */
934 int is_memory; /* true if memory operand */
935 int is_rw; /* for '+' modifier */
936 } ASMOperand;
938 static void asm_expr(TCCState *s1, ExprValue *pe);
939 static int asm_int_expr(TCCState *s1);
940 static int find_constraint(ASMOperand *operands, int nb_operands,
941 const char *name, const char **pp);
943 static int tcc_assemble(TCCState *s1, int do_preprocess);
945 #endif
947 static void asm_instr(void);
948 static void asm_global_instr(void);
950 /* true if float/double/long double type */
951 static inline int is_float(int t)
953 int bt;
954 bt = t & VT_BTYPE;
955 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;