History

move declarations to tcc.h

grischka committed 17 years ago

Commit ae37bd51 parent a93bcdf2 files · +938 −935Browse files

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t