1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #ifndef _TCC_H
22 #define _TCC_H
23
24 #include "r_types.h"
25 #ifndef _GNU_SOURCE
26 #define _GNU_SOURCE
27 #endif
28 #include "tcc_config.h"
29
30 #ifdef CONFIG_TCCBOOT
31 #include "tccboot.h"
32 #define CONFIG_TCC_STATIC
33 #else
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <errno.h>
40 #ifndef _MSC_VER
41 #include <math.h>
42 #endif
43 #include <signal.h>
44 #include <fcntl.h>
45 #include <setjmp.h>
46 #include <time.h>
47
48 #ifdef CONFIG_TCCASSERT
49 #include <assert.h>
50 #define TCC_ASSERT(ex) assert(ex)
51 #else
52 #define TCC_ASSERT(ex)
53 #endif
54
55 #ifndef __WINDOWS__
56 # include <unistd.h>
57 # include <sys/time.h>
58 # ifndef __HAIKU__
59 # endif
60 # include <sys/mman.h>
61 # ifndef CONFIG_TCC_STATIC
62 # include <dlfcn.h>
63 # endif
64 #else
65 # include <windows.h>
66 # include <sys/timeb.h>
67 # include <io.h> /* open, close etc. */
68 # include <direct.h> /* getcwd */
69 # ifdef __GNUC__
70 # include <stdint.h>
71 # else
72 typedef UINT_PTR uintptr_t;
73 # endif
74 # define inline __inline
75 # define inp next_inp
76 # ifdef LIBTCC_AS_DLL
77 # define LIBTCCAPI __declspec(dllexport)
78 # define PUB_FUNC LIBTCCAPI
79 # endif
80 #endif
81
82 #endif /* !CONFIG_TCCBOOT */
83
84 #ifndef O_BINARY
85 # define O_BINARY 0
86 #endif
87
88 #include "stab.h"
89 #include "libtcc.h"
90
91 #ifndef __WINDOWS__
92 #include <inttypes.h>
93 #else
94 typedef unsigned char uint8_t;
95 typedef unsigned short int uint16_t;
96 typedef unsigned int uint32_t;
97 typedef unsigned long long int uint64_t;
98 #ifdef _MSC_VER
99 typedef signed char int8_t;
100 typedef long long int int64_t;
101 #endif
102 #endif
103
104 // TODO: Make it dependable from the r2 asm/anal settings
105 #define LDOUBLE_SIZE 12
106 #define LDOUBLE_ALIGN 4
107 #define MAX_ALIGN 8
108 #define PTR_SIZE 4
109
110 #if !defined (__HAIKU__)
111 typedef uint64_t addr_t;
112 #endif
113
114 /* parser debug */
115 /* #define PARSE_DEBUG */
116 /* preprocessor debug */
117 /* #define PP_DEBUG */
118 /* include file debug */
119 /* #define INC_DEBUG */
120 /* memory leak debug */
121 /* #define MEM_DEBUG */
122 /* assembler debug */
123 /* #define ASM_DEBUG */
124
125 #undef TCC_IS_NATIVE
126
127 /* ------------ path configuration ------------ */
128
129 #ifndef CONFIG_SYSROOT
130 # define CONFIG_SYSROOT ""
131 #endif
132 #ifndef CONFIG_TCCDIR
133 # define CONFIG_TCCDIR "."
134 #endif
135 #ifndef CONFIG_LDDIR
136 # define CONFIG_LDDIR "lib"
137 #endif
138
139 /* path to find crt1.o, crti.o and crtn.o */
140 #ifndef CONFIG_TCC_CRTPREFIX
141 # define CONFIG_TCC_CRTPREFIX CONFIG_SYSROOT "/usr/" CONFIG_LDDIR
142 #endif
143
144 /* Below: {B} is substituted by CONFIG_TCCDIR (rsp. -B option) */
145
146 /* system include paths */
147 #ifndef CONFIG_TCC_SYSINCLUDEPATHS
148 # ifdef TCC_TARGET_PE
149 # define CONFIG_TCC_SYSINCLUDEPATHS "{B}/include;{B}/include/winapi"
150 # elif defined CONFIG_MULTIARCHDIR
151 # define CONFIG_TCC_SYSINCLUDEPATHS \
152 CONFIG_SYSROOT "/usr/local/include" \
153 ":" CONFIG_SYSROOT "/usr/local/include/" CONFIG_MULTIARCHDIR \
154 ":" CONFIG_SYSROOT "/usr/include" \
155 ":" CONFIG_SYSROOT "/usr/include/" CONFIG_MULTIARCHDIR \
156 ":" "{B}/include"
157 # else
158 # define CONFIG_TCC_SYSINCLUDEPATHS \
159 CONFIG_SYSROOT "/usr/local/include" \
160 ":" CONFIG_SYSROOT "/usr/include" \
161 ":" "{B}/include"
162 # endif
163 #endif
164
165 /* library search paths */
166 #ifndef CONFIG_TCC_LIBPATHS
167 # ifdef TCC_TARGET_PE
168 # define CONFIG_TCC_LIBPATHS "{B}/lib;{B}"
169 # else
170 # define CONFIG_TCC_LIBPATHS \
171 CONFIG_SYSROOT "/usr/" CONFIG_LDDIR \
172 ":" CONFIG_SYSROOT "/" CONFIG_LDDIR \
173 ":" CONFIG_SYSROOT "/usr/local/" CONFIG_LDDIR
174 # endif
175 #endif
176
177 /* -------------------------------------------- */
178
179 // TODO: Read this from the configuration variables in r2
180
181 #define STACK_NEW0(type, arg) \
182 type arg; \
183 ZERO_FILL(arg)
184
185 #define INCLUDE_STACK_SIZE 32
186 #define IFDEF_STACK_SIZE 64
187 #define VSTACK_SIZE 1024
188 #define STRING_MAX_SIZE 1024
189 #define PACK_STACK_SIZE 8
190
191 #define TOK_HASH_SIZE 8192 /* must be a power of two */
192 #define TOK_ALLOC_INCR 512 /* must be a power of two */
193 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
194
195 /* token symbol management */
196 typedef struct TokenSym {
197 struct TokenSym *hash_next;
198 struct Sym *sym_define; /* direct pointer to define */
199 struct Sym *sym_label; /* direct pointer to label */
200 struct Sym *sym_struct; /* direct pointer to structure */
201 struct Sym *sym_identifier; /* direct pointer to identifier */
202 int tok; /* token number */
203 int len;
204 char str[1];
205 } TokenSym;
206
207 #ifdef TCC_TARGET_PE
208 typedef unsigned short nwchar_t;
209 #else
210 typedef int nwchar_t;
211 #endif
212
213 typedef struct CString {
214 int size; /* size in bytes */
215 void *data; /* either 'char *' or 'nwchar_t *' */
216 int size_allocated;
217 void *data_allocated; /* if non NULL, data has been malloced */
218 } CString;
219
220 /* type definition */
221 typedef struct CType {
222 int t;
223 struct Sym *ref;
224 } CType;
225
226 /* constant value */
227 typedef union CValue {
228 long double ld;
229 double d;
230 float f;
231 int i;
232 unsigned int ui;
233 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
234 long long ll;
235 unsigned long long ull;
236 struct CString *cstr;
237 void *ptr;
238 int tab[LDOUBLE_SIZE/4];
239 } CValue;
240
241 /* value on stack */
242 typedef struct SValue {
243 CType type; /* type */
244 unsigned short r; /* register + flags */
245 unsigned short r2; /* second register, used for 'long long'
246 type. If not used, set to VT_CONST */
247 CValue c; /* constant, if VT_CONST */
248 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
249 } SValue;
250
251 /* symbol management */
252 typedef struct Sym {
253 int v; /* symbol token */
254 char *asm_label; /* associated asm label */
255 unsigned int r; /* associated register */
256 union {
257 long long c; /* associated number */
258 int *d; /* define token stream */
259 };
260 CType type; /* associated type */
261 union {
262 struct Sym *next; /* next related symbol */
263 long jnext; /* next jump label */
264 };
265 struct Sym *prev; /* prev symbol in stack */
266 struct Sym *prev_tok; /* previous symbol for this token */
267 } Sym;
268
269 /* GNUC attribute definition */
270 typedef struct AttributeDef {
271 unsigned int
272 func_call : 3, /* calling convention (0..5), see below */
273 aligned : 5, /* alignement (0..16) */
274 packed : 1,
275 func_export : 1,
276 func_import : 1,
277 func_args : 5,
278 mode : 4,
279 weak : 1,
280 fill : 11;
281 int alias_target; /* token */
282 } AttributeDef;
283
284 /* gr: wrappers for casting sym->r for other purposes */
285 #define FUNC_CALL(r) (((AttributeDef*)&(r))->func_call)
286 #define FUNC_EXPORT(r) (((AttributeDef*)&(r))->func_export)
287 #define FUNC_IMPORT(r) (((AttributeDef*)&(r))->func_import)
288 #define FUNC_ARGS(r) (((AttributeDef*)&(r))->func_args)
289 #define FUNC_ALIGN(r) (((AttributeDef*)&(r))->aligned)
290 #define FUNC_PACKED(r) (((AttributeDef*)&(r))->packed)
291 #define ATTR_MODE(r) (((AttributeDef*)&(r))->mode)
292 #define INT_ATTR(ad) (*(int*)(ad))
293
294 /* -------------------------------------------------- */
295
296 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
297 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
298 #define SYM_FIRST_ANOM 0x10000000 /* first anonymous sym */
299
300 #define VLA_SP_LOC_SET 0x01 /* Location of SP on stack has been allocated */
301 #define VLA_SP_SAVED 0x02 /* SP has been saved to slot already */
302 #define VLA_NEED_NEW_FRAME 0x04 /* Needs new frame for next VLA */
303 #define VLA_IN_SCOPE 0x08 /* One or more VLAs are in scope */
304 #define VLA_SCOPE_FLAGS (VLA_SP_SAVED|VLA_NEED_NEW_FRAME|VLA_IN_SCOPE) /* Flags which are saved and restored upon entering and exiting a block */
305
306 /* stored in 'Sym.c' field */
307 #define FUNC_NEW 1 /* ansi function prototype */
308 #define FUNC_OLD 2 /* old function prototype */
309 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
310
311 /* stored in 'Sym.r' field */
312 #define FUNC_CDECL 0 /* standard c call */
313 #define FUNC_STDCALL 1 /* pascal c call */
314 #define FUNC_FASTCALL1 2 /* first param in %eax */
315 #define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
316 #define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
317 #define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
318
319 /* field 'Sym.t' for macros */
320 #define MACRO_OBJ 0 /* object like macro */
321 #define MACRO_FUNC 1 /* function like macro */
322
323 /* field 'Sym.r' for C labels */
324 #define LABEL_DEFINED 0 /* label is defined */
325 #define LABEL_FORWARD 1 /* label is forward defined */
326 #define LABEL_DECLARED 2 /* label is declared but never used */
327
328 /* type_decl() types */
329 #define TYPE_ABSTRACT 1 /* type without variable */
330 #define TYPE_DIRECT 2 /* type with variable */
331
332 #define IO_BUF_SIZE 8192
333
334 typedef struct BufferedFile {
335 uint8_t *buf_ptr;
336 uint8_t *buf_end;
337 int fd;
338 struct BufferedFile *prev;
339 int line_num; /* current line number - here to simplify code */
340 int ifndef_macro; /* #ifndef macro / #endif search */
341 int ifndef_macro_saved; /* saved ifndef_macro */
342 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
343 char filename[1024]; /* filename */
344 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
345 } BufferedFile;
346
347 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
348 #define CH_EOF (-1) /* end of file */
349
350 /* parsing state (used to save parser state to reparse part of the
351 source several times) */
352 typedef struct ParseState {
353 const int *macro_ptr;
354 int line_num;
355 int tok;
356 CValue tokc;
357 } ParseState;
358
359 /* used to record tokens */
360 typedef struct TokenString {
361 int *str;
362 int len;
363 int allocated_len;
364 int last_line_num;
365 } TokenString;
366
367 /* inline functions */
368 typedef struct InlineFunc {
369 int *token_str;
370 Sym *sym;
371 char filename[1];
372 } InlineFunc;
373
374 /* include file cache, used to find files faster and also to eliminate
375 inclusion if the include file is protected by #ifndef ... #endif */
376 typedef struct CachedInclude {
377 int ifndef_macro;
378 int hash_next; /* -1 if none */
379 char filename[1]; /* path specified in #include */
380 } CachedInclude;
381
382 #define CACHED_INCLUDES_HASH_SIZE 512
383
384 #ifdef CONFIG_TCC_ASM
385 typedef struct ExprValue {
386 uint32_t v;
387 Sym *sym;
388 } ExprValue;
389
390 #define MAX_ASM_OPERANDS 30
391 typedef struct ASMOperand {
392 int id; /* GCC 3 optionnal identifier (0 if number only supported */
393 char *constraint;
394 char asm_str[16]; /* computed asm string for operand */
395 SValue *vt; /* C value of the expression */
396 int ref_index; /* if >= 0, gives reference to a output constraint */
397 int input_index; /* if >= 0, gives reference to an input constraint */
398 int priority; /* priority, used to assign registers */
399 int reg; /* if >= 0, register number used for this operand */
400 int is_llong; /* true if double register value */
401 int is_memory; /* true if memory operand */
402 int is_rw; /* for '+' modifier */
403 } ASMOperand;
404 #endif
405
406 struct sym_attr {
407 unsigned long got_offset;
408 #ifdef TCC_TARGET_ARM
409 unsigned char plt_thumb_stub:1;
410 #endif
411 };
412
413 struct TCCState {
414
415 int verbose; /* if true, display some information during compilation */
416 int nostdinc; /* if true, no standard headers are added */
417 int nostdlib; /* if true, no standard libraries are added */
418 int nocommon; /* if true, do not use common symbols for .bss data */
419 int static_link; /* if true, static linking is performed */
420 int rdynamic; /* if true, all symbols are exported */
421 int symbolic; /* if true, resolve symbols in the current module first */
422 int alacarte_link; /* if true, only link in referenced objects from archive */
423
424 char *tcc_lib_path; /* CONFIG_TCCDIR or -B option */
425 char *soname; /* as specified on the command line (-soname) */
426 char *rpath; /* as specified on the command line (-Wl,-rpath=) */
427
428 /* output type, see TCC_OUTPUT_XXX */
429 int output_type;
430 /* output format, see TCC_OUTPUT_FORMAT_xxx */
431 int output_format;
432 /* Target system */
433 char *arch;
434 int bits;
435 char *os;
436
437 /* C language options */
438 int char_is_unsigned;
439 int leading_underscore;
440
441 /* warning switches */
442 int warn_write_strings;
443 int warn_unsupported;
444 int warn_error;
445 int warn_none;
446 int warn_implicit_function_declaration;
447
448 /* compile with debug symbol (and use them if error during execution) */
449 int do_debug;
450 #ifdef CONFIG_TCC_BCHECK
451 /* compile with built-in memory and bounds checker */
452 int do_bounds_check;
453 #endif
454
455 addr_t text_addr; /* address of text section */
456 int has_text_addr;
457
458 char *init_symbol; /* symbols to call at load-time (not used currently) */
459 char *fini_symbol; /* symbols to call at unload-time (not used currently) */
460
461 int seg_size; /* 32. Can be 16 with i386 assembler (.code16) */
462
463 /* include paths */
464 char **include_paths;
465 int nb_include_paths;
466
467 char **sysinclude_paths;
468 int nb_sysinclude_paths;
469
470 /* library paths */
471 char **library_paths;
472 int nb_library_paths;
473
474 /* crt?.o object path */
475 char **crt_paths;
476 int nb_crt_paths;
477
478 /* error handling */
479 void *error_opaque;
480 void (*error_func)(void *opaque, const char *msg);
481 int error_set_jmp_enabled;
482 jmp_buf error_jmp_buf;
483 int nb_errors;
484
485 /* output file for preprocessing (-E) */
486 FILE *ppfp;
487
488 /* for -MD/-MF: collected dependencies for this compilation */
489 char **target_deps;
490 int nb_target_deps;
491
492 /* compilation */
493 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
494 BufferedFile **include_stack_ptr;
495
496 int ifdef_stack[IFDEF_STACK_SIZE];
497 int *ifdef_stack_ptr;
498
499 /* included files enclosed with #ifndef MACRO */
500 int cached_includes_hash[CACHED_INCLUDES_HASH_SIZE];
501 CachedInclude **cached_includes;
502 int nb_cached_includes;
503
504 /* #pragma pack stack */
505 int pack_stack[PACK_STACK_SIZE];
506 int *pack_stack_ptr;
507
508 /* inline functions are stored as token lists and compiled last
509 only if referenced */
510 struct InlineFunc **inline_fns;
511 int nb_inline_fns;
512
513 struct sym_attr *sym_attrs;
514 int nb_sym_attrs;
515 /* give the correspondance from symtab indexes to dynsym indexes */
516 int *symtab_to_dynsym;
517
518 /* tiny assembler state */
519 Sym *asm_labels;
520
521 /* used by main and tcc_parse_args only */
522 char **files; /* files seen on command line */
523 int nb_files; /* number thereof */
524 int nb_libraries; /* number of libs thereof */
525 char *outfile; /* output filename */
526 char *option_m; /* only -m32/-m64 handled */
527 int print_search_dirs; /* option */
528 int option_r; /* option -r */
529 int do_bench; /* option -bench */
530 int gen_deps; /* option -MD */
531 char *deps_outfile; /* option -MF */
532 };
533
534 /* The current value can be: */
535 #define VT_VALMASK 0x003f /* mask for value location, register or: */
536 #define VT_CONST 0x0030 /* constant in vc (must be first non register value) */
537 #define VT_LLOCAL 0x0031 /* lvalue, offset on stack */
538 #define VT_LOCAL 0x0032 /* offset on stack */
539 #define VT_CMP 0x0033 /* the value is stored in processor flags (in vc) */
540 #define VT_JMP 0x0034 /* value is the consequence of jmp true (even) */
541 #define VT_JMPI 0x0035 /* value is the consequence of jmp false (odd) */
542 #define VT_REF 0x0040 /* value is pointer to structure rather than address */
543 #define VT_LVAL 0x0100 /* var is an lvalue */
544 #define VT_SYM 0x0200 /* a symbol value is added */
545 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
546 char/short stored in integer registers) */
547 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
548 dereferencing value */
549 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
550 bounding function call point is in vc */
551 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
552 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
553 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
554 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
555
556 /* types */
557 #define VT_BTYPE 0x000f /* mask for basic type */
558 #define VT_INT32 0 /* int32_t integer type */
559 #define VT_INT16 1 /* int16_t short type */
560 #define VT_INT8 2 /* int8_t signed byte type */
561 #define VT_VOID 3 /* void type */
562 #define VT_PTR 4 /* pointer */
563 #define VT_ENUM 5 /* enum definition */
564 #define VT_FUNC 6 /* function type */
565 #define VT_STRUCT 7 /* struct definition */
566 #define VT_UNION 8 /* union definition */
567 #define VT_FLOAT 9 /* IEEE float */
568 #define VT_DOUBLE 10 /* IEEE double */
569 #define VT_LDOUBLE 11 /* IEEE long double */
570 #define VT_BOOL 12 /* ISOC99 boolean type */
571 #define VT_INT64 13 /* int64_t */
572 #define VT_LONG 14 /* long integer (NEVER USED as type, only
573 during parsing) */
574 #define VT_QLONG 15 /* 128-bit integer. Only used for x86-64 ABI */
575 #define VT_QFLOAT 0x0010 /* 128-bit float. Only used for x86-64 ABI */
576 #define VT_UNSIGNED 0x0020 /* unsigned type */
577 #define VT_ARRAY 0x0040 /* array type (also has VT_PTR) */
578 #define VT_BITFIELD 0x0800 /* bitfield modifier */
579 #define VT_CONSTANT 0x1000 /* const modifier */
580 #define VT_VOLATILE 0x2000 /* volatile modifier */
581 #define VT_SIGNED 0x4000 /* signed type */
582 #define VT_CHAR 0x00040000 /* character type - for strings */
583 #define VT_VLA 0x00080000 /* VLA type (also has VT_PTR and VT_ARRAY) */
584
585 /* storage */
586 #define VT_EXTERN 0x00000080 /* extern definition */
587 #define VT_STATIC 0x00000100 /* static variable */
588 #define VT_TYPEDEF 0x00000200 /* typedef definition */
589 #define VT_INLINE 0x00000400 /* inline definition */
590 #define VT_IMPORT 0x00008000 /* win32: extern data imported from dll */
591 #define VT_EXPORT 0x00010000 /* win32: data exported from dll */
592 #define VT_WEAK 0x00020000 /* weak symbol */
593
594 #define VT_STRUCT_SHIFT 18 /* shift for bitfield shift values (max: 32 - 2*6) */
595
596 /* type mask (except storage) */
597 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE | VT_IMPORT | VT_EXPORT | VT_WEAK)
598 #define VT_TYPE (~(VT_STORAGE))
599
600 /* token values */
601
602 /* warning: the following compare tokens depend on i386 asm code */
603 #define TOK_ULT 0x92
604 #define TOK_UGE 0x93
605 #define TOK_EQ 0x94
606 #define TOK_NE 0x95
607 #define TOK_ULE 0x96
608 #define TOK_UGT 0x97
609 #define TOK_Nset 0x98
610 #define TOK_Nclear 0x99
611 #define TOK_LT 0x9c
612 #define TOK_GE 0x9d
613 #define TOK_LE 0x9e
614 #define TOK_GT 0x9f
615
616 #define TOK_LAND 0xa0
617 #define TOK_LOR 0xa1
618
619 #define TOK_DEC 0xa2
620 #define TOK_MID 0xa3 /* inc/dec, to void constant */
621 #define TOK_INC 0xa4
622 #define TOK_UDIV 0xb0 /* unsigned division */
623 #define TOK_UMOD 0xb1 /* unsigned modulo */
624 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
625 #define TOK_CINT 0xb3 /* number in tokc */
626 #define TOK_CCHAR 0xb4 /* char constant in tokc */
627 #define TOK_STR 0xb5 /* pointer to string in tokc */
628 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
629 #define TOK_LCHAR 0xb7
630 #define TOK_LSTR 0xb8
631 #define TOK_CFLOAT 0xb9 /* float constant */
632 #define TOK_LINENUM 0xba /* line number info */
633 #define TOK_CDOUBLE 0xc0 /* double constant */
634 #define TOK_CLDOUBLE 0xc1 /* long double constant */
635 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
636 #define TOK_ADDC1 0xc3 /* add with carry generation */
637 #define TOK_ADDC2 0xc4 /* add with carry use */
638 #define TOK_SUBC1 0xc5 /* add with carry generation */
639 #define TOK_SUBC2 0xc6 /* add with carry use */
640 #define TOK_CUINT 0xc8 /* unsigned int constant */
641 #define TOK_CLLONG 0xc9 /* long long constant */
642 #define TOK_CULLONG 0xca /* unsigned long long constant */
643 #define TOK_ARROW 0xcb
644 #define TOK_DOTS 0xcc /* three dots */
645 #define TOK_SHR 0xcd /* unsigned shift right */
646 #define TOK_PPNUM 0xce /* preprocessor number */
647 #define TOK_NOSUBST 0xcf /* means following token has already been pp'd */
648
649 #define TOK_SHL 0x01 /* shift left */
650 #define TOK_SAR 0x02 /* signed shift right */
651
652 /* assignement operators : normal operator or 0x80 */
653 #define TOK_A_MOD 0xa5
654 #define TOK_A_AND 0xa6
655 #define TOK_A_MUL 0xaa
656 #define TOK_A_ADD 0xab
657 #define TOK_A_SUB 0xad
658 #define TOK_A_DIV 0xaf
659 #define TOK_A_XOR 0xde
660 #define TOK_A_OR 0xfc
661 #define TOK_A_SHL 0x81
662 #define TOK_A_SAR 0x82
663
664 #ifndef offsetof
665 #define offsetof(type, field) ((size_t) &((type *)0)->field)
666 #endif
667
668 #ifndef countof
669 #define countof(tab) (sizeof(tab) / sizeof((tab)[0]))
670 #endif
671
672 #define TOK_EOF (-1) /* end of file */
673 #define TOK_LINEFEED 10 /* line feed */
674
675 /* all identificators and strings have token above that */
676 #define TOK_IDENT 256
677
678 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
679 #define TOK_ASM_int TOK_INT
680 #define TOK_ASM_weak TOK_WEAK1
681
682 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
683 /* only used for i386 asm opcodes definitions */
684 #define DEF_BWL(x) \
685 DEF(TOK_ASM_ ## x ## b, #x "b") \
686 DEF(TOK_ASM_ ## x ## w, #x "w") \
687 DEF(TOK_ASM_ ## x ## l, #x "l") \
688 DEF(TOK_ASM_ ## x, #x)
689 #define DEF_WL(x) \
690 DEF(TOK_ASM_ ## x ## w, #x "w") \
691 DEF(TOK_ASM_ ## x ## l, #x "l") \
692 DEF(TOK_ASM_ ## x, #x)
693 #ifdef TCC_TARGET_X86_64
694 # define DEF_BWLQ(x) \
695 DEF(TOK_ASM_ ## x ## b, #x "b") \
696 DEF(TOK_ASM_ ## x ## w, #x "w") \
697 DEF(TOK_ASM_ ## x ## l, #x "l") \
698 DEF(TOK_ASM_ ## x ## q, #x "q") \
699 DEF(TOK_ASM_ ## x, #x)
700 # define DEF_WLQ(x) \
701 DEF(TOK_ASM_ ## x ## w, #x "w") \
702 DEF(TOK_ASM_ ## x ## l, #x "l") \
703 DEF(TOK_ASM_ ## x ## q, #x "q") \
704 DEF(TOK_ASM_ ## x, #x)
705 # define DEF_BWLX DEF_BWLQ
706 # define DEF_WLX DEF_WLQ
707 /* number of sizes + 1 */
708 # define NBWLX 5
709 #else
710 # define DEF_BWLX DEF_BWL
711 # define DEF_WLX DEF_WL
712 /* number of sizes + 1 */
713 # define NBWLX 4
714 #endif
715
716 #define DEF_FP1(x) \
717 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
718 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
719 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
720 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
721
722 #define DEF_FP(x) \
723 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
724 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
725 DEF_FP1(x)
726
727 #define DEF_ASMTEST(x) \
728 DEF_ASM(x ## o) \
729 DEF_ASM(x ## no) \
730 DEF_ASM(x ## b) \
731 DEF_ASM(x ## c) \
732 DEF_ASM(x ## nae) \
733 DEF_ASM(x ## nb) \
734 DEF_ASM(x ## nc) \
735 DEF_ASM(x ## ae) \
736 DEF_ASM(x ## e) \
737 DEF_ASM(x ## z) \
738 DEF_ASM(x ## ne) \
739 DEF_ASM(x ## nz) \
740 DEF_ASM(x ## be) \
741 DEF_ASM(x ## na) \
742 DEF_ASM(x ## nbe) \
743 DEF_ASM(x ## a) \
744 DEF_ASM(x ## s) \
745 DEF_ASM(x ## ns) \
746 DEF_ASM(x ## p) \
747 DEF_ASM(x ## pe) \
748 DEF_ASM(x ## np) \
749 DEF_ASM(x ## po) \
750 DEF_ASM(x ## l) \
751 DEF_ASM(x ## nge) \
752 DEF_ASM(x ## nl) \
753 DEF_ASM(x ## ge) \
754 DEF_ASM(x ## le) \
755 DEF_ASM(x ## ng) \
756 DEF_ASM(x ## nle) \
757 DEF_ASM(x ## g)
758
759 #endif /* defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64 */
760
761 enum tcc_token {
762 TOK_LAST = TOK_IDENT - 1,
763 #define DEF(id, str) id,
764 #include "tcctok.h"
765 #undef DEF
766 };
767
768 #define TOK_UIDENT TOK_DEFINE
769
770 #ifdef __WINDOWS__
771 #define snprintf _snprintf
772 #define vsnprintf _vsnprintf
773 #ifndef __GNUC__
774 # define strtold (long double)strtod
775 # define strtof (float)strtod
776 # define strtoll _strtoi64
777 # define strtoull _strtoui64
778 #endif
779 #else
780 /* XXX: need to define this to use them in non ISOC99 context */
781 extern float strtof (const char *__nptr, char **__endptr);
782 extern long double strtold (const char *__nptr, char **__endptr);
783 #endif
784
785 #ifdef __WINDOWS__
786 #define IS_DIRSEP(c) (c == '/' || c == '\\')
787 #define IS_ABSPATH(p) (IS_DIRSEP(p[0]) || (p[0] && p[1] == ':' && IS_DIRSEP(p[2])))
788 #define PATHCMP stricmp
789 #else
790 #define IS_DIRSEP(c) (c == '/')
791 #define IS_ABSPATH(p) IS_DIRSEP(p[0])
792 #define PATHCMP strcmp
793 #endif
794
795 #ifdef TCC_TARGET_PE
796 #define PATHSEP ';'
797 #else
798 #define PATHSEP ':'
799 #endif
800
801 /* space exlcuding newline */
is_space(int ch)802 static inline int is_space(int ch)
803 {
804 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
805 }
806
isid(int c)807 static inline int isid(int c)
808 {
809 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
810 }
811
isnum(int c)812 static inline int isnum(int c)
813 {
814 return c >= '0' && c <= '9';
815 }
816
isdot(int c)817 static inline int isdot(int c)
818 {
819 return c == '.';
820 }
821
isoct(int c)822 static inline int isoct(int c)
823 {
824 return c >= '0' && c <= '7';
825 }
826
toup(int c)827 static inline int toup(int c)
828 {
829 return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c;
830 }
831
832 #ifndef PUB_FUNC
833 # define PUB_FUNC
834 #endif
835
836 #ifdef ONE_SOURCE
837 #define ST_INLN static inline
838 #define ST_FUNC static
839 #define ST_DATA static
840 #else
841 #define ST_INLN
842 #define ST_FUNC
843 #define ST_DATA extern
844 #endif
845
846 /* ------------ libtcc.c ------------ */
847
848 /* use GNU C extensions */
849 ST_DATA int gnu_ext;
850 /* use Tiny C extensions */
851 ST_DATA int tcc_ext;
852 /* XXX: get rid of this ASAP */
853 ST_DATA struct TCCState *tcc_state;
854
tcc_nerr(void)855 static inline int tcc_nerr(void) {
856 return tcc_state->nb_errors;
857 }
858
859
860 #ifdef MEM_DEBUG
861 ST_DATA int mem_cur_size;
862 ST_DATA int mem_max_size;
863 #endif
864
865 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
866 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
867 #define AFF_PREPROCESS 0x0004 /* preprocess file */
868
869 /* public functions currently used by the tcc main function */
870 PUB_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
871 PUB_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
872 PUB_FUNC char *pstrncpy(char *out, const char *in, size_t num);
873 PUB_FUNC char *tcc_basename(const char *name);
874 PUB_FUNC char *tcc_fileextension (const char *name);
875 PUB_FUNC void tcc_free(void *ptr);
876 PUB_FUNC void *tcc_malloc(unsigned long size);
877 PUB_FUNC void *tcc_mallocz(unsigned long size);
878 PUB_FUNC void *tcc_realloc(void *ptr, unsigned long size);
879 PUB_FUNC char *tcc_strdup(const char *str);
880 PUB_FUNC void tcc_memstats(void);
881 PUB_FUNC void tcc_error_noabort(const char *fmt, ...);
882 PUB_FUNC void tcc_error(const char *fmt, ...);
883 PUB_FUNC void tcc_warning(const char *fmt, ...);
884 PUB_FUNC void strcat_printf(char *buf, int buf_size, const char *fmt, ...);
885
886 /* other utilities */
887 ST_FUNC void dynarray_add(void ***ptab, int *nb_ptr, void *data);
888 ST_FUNC void dynarray_reset(void *pp, int *n);
889 ST_FUNC void cstr_ccat(CString *cstr, int ch);
890 ST_FUNC void cstr_cat(CString *cstr, const char *str);
891 ST_FUNC void cstr_wccat(CString *cstr, int ch);
892 ST_FUNC void cstr_new(CString *cstr);
893 ST_FUNC void cstr_free(CString *cstr);
894 ST_FUNC void cstr_reset(CString *cstr);
895
896 ST_INLN void sym_free(Sym *sym);
897 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long long c);
898 ST_FUNC Sym *sym_find2(Sym *s, int v);
899 ST_FUNC Sym *sym_push(int v, CType *type, int r, long long c);
900 ST_FUNC void sym_pop(Sym **ptop, Sym *b);
901 ST_INLN Sym *struct_find(int v);
902 ST_INLN Sym *sym_find(int v);
903 ST_FUNC Sym *global_identifier_push(int v, int t, long long c);
904
905 ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
906 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
907 ST_FUNC void tcc_close(void);
908
909 ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
910
911 PUB_FUNC void tcc_print_stats(TCCState *s, int64_t total_time);
912 PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv);
913
914 PUB_FUNC void tcc_set_environment(TCCState *s);
915
916 /* ------------ tccpp.c ------------ */
917
918 ST_DATA struct BufferedFile *file;
919 ST_DATA int ch, tok;
920 ST_DATA CValue tokc;
921 ST_DATA const int *macro_ptr;
922 ST_DATA int parse_flags;
923 ST_DATA int tok_flags;
924 ST_DATA CString tokcstr; /* current parsed string, if any */
925
926 /* display benchmark infos */
927 ST_DATA int total_lines;
928 ST_DATA int total_bytes;
929 ST_DATA int tok_ident;
930 ST_DATA TokenSym **table_ident;
931
932 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
933 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
934 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
935 #define TOK_FLAG_EOF 0x0008 /* end of file */
936
937 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
938 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
939 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
940 token. line feed is also
941 returned at eof */
942 #define PARSE_FLAG_ASM_COMMENTS 0x0008 /* '#' can be used for line comment */
943 #define PARSE_FLAG_SPACES 0x0010 /* next() returns space tokens (for -E) */
944
945 ST_FUNC TokenSym *tok_alloc(const char *str, int len);
946 ST_FUNC char *get_tok_str(int v, CValue *cv);
947 ST_FUNC void save_parse_state(ParseState *s);
948 ST_FUNC void restore_parse_state(ParseState *s);
949 ST_INLN void tok_str_new(TokenString *s);
950 ST_FUNC void tok_str_free(int *str);
951 ST_FUNC void tok_str_add(TokenString *s, int t);
952 ST_FUNC void tok_str_add_tok(TokenString *s);
953 ST_INLN void define_push(int v, int macro_type, int *str, Sym *first_arg);
954 ST_FUNC void define_undef(Sym *s);
955 ST_INLN Sym *define_find(int v);
956 ST_FUNC void free_defines(Sym *b);
957 ST_FUNC Sym *label_find(int v);
958 ST_FUNC Sym *label_push(Sym **ptop, int v, int flags);
959 ST_FUNC void label_pop(Sym **ptop, Sym *slast);
960 ST_FUNC void parse_define(void);
961 ST_FUNC void preprocess(int is_bof);
962 ST_FUNC void next_nomacro(void);
963 ST_FUNC void next(void);
964 ST_INLN void unget_tok(int last_tok);
965 ST_FUNC void preprocess_init(TCCState *s1);
966 ST_FUNC void preprocess_new(void);
967 ST_FUNC int tcc_preprocess(TCCState *s1);
968 ST_FUNC void skip(int c);
969 ST_FUNC void expect(const char *msg);
970
971 /* ------------ tccgen.c ------------ */
972
973 #define RC_INT 0x0001 /* generic integer register */
974 #define RC_FLOAT 0x0002 /* generic float register */
975 #define RC_IRET 0x0004
976 #define RC_LRET 0x0020
977 #define RC_FRET 0x0008
978 #define REG_IRET 0
979 #define REG_LRET 2
980 #define REG_FRET 3
981
982 #define SYM_POOL_NB (8192 / sizeof(Sym))
983 ST_DATA Sym *sym_free_first;
984 ST_DATA void **sym_pools;
985 ST_DATA int nb_sym_pools;
986
987 ST_DATA Sym *global_stack;
988 ST_DATA Sym *local_stack;
989 ST_DATA Sym *local_label_stack;
990 ST_DATA Sym *global_label_stack;
991 ST_DATA Sym *define_stack;
992 ST_DATA CType char_pointer_type, func_old_type;
993 ST_DATA CType int8_type, int16_type, int32_type, int64_type, size_type;
994 ST_DATA SValue __vstack[1+/*to make bcheck happy*/ VSTACK_SIZE], *vtop;
995 #define vstack (__vstack + 1)
996 ST_DATA int rsym, anon_sym, ind, loc;
997
998 ST_DATA int const_wanted; /* true if constant wanted */
999 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
1000 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
1001 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
1002 ST_DATA int func_vc;
1003 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
1004 ST_DATA char *funcname;
1005 ST_DATA char *dir_name;
1006
1007 ST_INLN bool is_structured(CType *t);
1008 ST_INLN bool is_struct(CType *t);
1009 ST_INLN bool is_union(CType *t);
1010 ST_INLN bool is_float(int t);
1011 ST_INLN bool not_structured(CType *t);
1012
1013 ST_FUNC int ieee_finite(double d);
1014 ST_FUNC void test_lvalue(void);
1015 ST_FUNC void swap(int *p, int *q);
1016 ST_FUNC void vpushi(int v);
1017 ST_FUNC Sym *external_global_sym(int v, CType *type, int r);
1018 ST_FUNC void vset(CType *type, int r, int v);
1019 ST_FUNC void vswap(void);
1020 ST_FUNC void vpush_global_sym(CType *type, int v);
1021 ST_FUNC void vrote(SValue *e, int n);
1022 ST_FUNC void vrott(int n);
1023 ST_FUNC void vrotb(int n);
1024 #ifdef TCC_TARGET_ARM
1025 ST_FUNC int get_reg_ex(int rc, int rc2);
1026 ST_FUNC void lexpand_nr(void);
1027 #endif
1028 ST_FUNC void vpushv(SValue *v);
1029 ST_FUNC void save_reg(int r);
1030 ST_FUNC int get_reg(int rc);
1031 ST_FUNC void save_regs(int n);
1032 ST_FUNC int gv(int rc);
1033 ST_FUNC void gv2(int rc1, int rc2);
1034 ST_FUNC void vpop(void);
1035 ST_FUNC void gen_op(int op);
1036 ST_FUNC int type_size(CType *type, int *a);
1037 ST_FUNC void mk_pointer(CType *type);
1038 ST_FUNC void vstore(void);
1039 ST_FUNC void inc(int post, int c);
1040 ST_FUNC void parse_asm_str(CString *astr);
1041 ST_FUNC int lvalue_type(int t);
1042 ST_FUNC void indir(void);
1043 ST_FUNC void unary(void);
1044 ST_FUNC void expr_prod(void);
1045 ST_FUNC void expr_sum(void);
1046 ST_FUNC void gexpr(void);
1047 ST_FUNC long long expr_const(void);
1048 ST_FUNC void gen_inline_functions(void);
1049 ST_FUNC void decl(int l);
1050 #if defined TCC_TARGET_X86_64 && !defined TCC_TARGET_PE
1051 ST_FUNC int classify_x86_64_va_arg(CType *ty);
1052 #endif
1053
1054 /********************************************************/
1055 #undef ST_DATA
1056 #ifdef ONE_SOURCE
1057 #define ST_DATA static
1058 #else
1059 #define ST_DATA
1060 #endif
1061 /********************************************************/
1062 PUB_FUNC void tcc_appendf(const char *fmt, ...);
1063 PUB_FUNC void tcc_typedef_appendf(const char *fmt, ...);
1064 PUB_FUNC void tcc_typedef_alias_fields(const char *alias);
1065
1066 extern void (*tcc_cb)(const char *, char **);
1067
1068 #endif /* _TCC_H */
1069