2 * TCC - Tiny C Compiler
4 * Copyright (c) 2001, 2002 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <sys/timeb.h>
36 #include <sys/ucontext.h>
40 #ifndef CONFIG_TCC_STATIC
48 /* preprocessor debug */
50 /* include file debug */
55 /* target selection */
56 //#define TCC_TARGET_I386 /* i386 code generator */
57 //#define TCC_TARGET_IL /* .NET CLI generator */
59 /* default target is I386 */
60 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_IL)
61 #define TCC_TARGET_I386
64 #if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL)
65 #define CONFIG_TCC_BCHECK /* enable bound checking code */
68 #ifndef CONFIG_TCC_PREFIX
69 #define CONFIG_TCC_PREFIX "/usr/local"
72 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
73 executables or dlls */
74 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
76 #define INCLUDE_STACK_SIZE 32
77 #define IFDEF_STACK_SIZE 64
78 #define VSTACK_SIZE 64
79 #define STRING_MAX_SIZE 1024
81 #define TOK_HASH_SIZE 2048 /* must be a power of two */
82 #define TOK_ALLOC_INCR 512 /* must be a power of two */
83 #define TOK_STR_ALLOC_INCR_BITS 6
84 #define TOK_STR_ALLOC_INCR (1 << TOK_STR_ALLOC_INCR_BITS)
85 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
87 /* token symbol management */
88 typedef struct TokenSym
{
89 struct TokenSym
*hash_next
;
90 struct Sym
*sym_define
; /* direct pointer to define */
91 struct Sym
*sym_label
; /* direct pointer to label */
92 struct Sym
*sym_struct
; /* direct pointer to structure */
93 struct Sym
*sym_identifier
; /* direct pointer to identifier */
94 int tok
; /* token number */
99 typedef struct CString
{
100 int size
; /* size in bytes */
101 void *data
; /* either 'char *' or 'int *' */
103 void *data_allocated
; /* if non NULL, data has been malloced */
106 /* type definition */
107 typedef struct CType
{
113 typedef union CValue
{
119 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
121 unsigned long long ull
;
122 struct CString
*cstr
;
128 typedef struct SValue
{
129 CType type
; /* type */
130 unsigned short r
; /* register + flags */
131 unsigned short r2
; /* second register, used for 'long long'
132 type. If not used, set to VT_CONST */
133 CValue c
; /* constant, if VT_CONST */
134 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
137 /* symbol management */
139 int v
; /* symbol token */
140 int r
; /* associated register */
141 int c
; /* associated number */
142 CType type
; /* associated type */
143 struct Sym
*next
; /* next related symbol */
144 struct Sym
*prev
; /* prev symbol in stack */
145 struct Sym
*prev_tok
; /* previous symbol for this token */
148 /* section definition */
149 /* XXX: use directly ELF structure for parameters ? */
150 /* special flag to indicate that the section should not be linked to
152 #define SHF_PRIVATE 0x80000000
154 typedef struct Section
{
155 unsigned long data_offset
; /* current data offset */
156 unsigned char *data
; /* section data */
157 unsigned long data_allocated
; /* used for realloc() handling */
158 int sh_name
; /* elf section name (only used during output) */
159 int sh_num
; /* elf section number */
160 int sh_type
; /* elf section type */
161 int sh_flags
; /* elf section flags */
162 int sh_info
; /* elf section info */
163 int sh_addralign
; /* elf section alignment */
164 int sh_entsize
; /* elf entry size */
165 unsigned long sh_size
; /* section size (only used during output) */
166 unsigned long sh_addr
; /* address at which the section is relocated */
167 unsigned long sh_offset
; /* address at which the section is relocated */
168 int nb_hashed_syms
; /* used to resize the hash table */
169 struct Section
*link
; /* link to another section */
170 struct Section
*reloc
; /* corresponding section for relocation, if any */
171 struct Section
*hash
; /* hash table for symbols */
172 struct Section
*next
;
173 char name
[64]; /* section name */
176 typedef struct DLLReference
{
181 /* GNUC attribute definition */
182 typedef struct AttributeDef
{
185 unsigned char func_call
; /* FUNC_CDECL or FUNC_STDCALL */
188 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
189 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
190 #define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
192 /* stored in 'Sym.c' field */
193 #define FUNC_NEW 1 /* ansi function prototype */
194 #define FUNC_OLD 2 /* old function prototype */
195 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
197 /* stored in 'Sym.r' field */
198 #define FUNC_CDECL 0 /* standard c call */
199 #define FUNC_STDCALL 1 /* pascal c call */
201 /* field 'Sym.t' for macros */
202 #define MACRO_OBJ 0 /* object like macro */
203 #define MACRO_FUNC 1 /* function like macro */
205 /* field 'Sym.r' for labels */
206 #define LABEL_FORWARD 1 /* label is forward defined */
208 /* type_decl() types */
209 #define TYPE_ABSTRACT 1 /* type without variable */
210 #define TYPE_DIRECT 2 /* type with variable */
212 #define IO_BUF_SIZE 8192
214 typedef struct BufferedFile
{
218 int line_num
; /* current line number - here to simply code */
219 int ifndef_macro
; /* #ifndef macro / #endif search */
220 int ifndef_macro_saved
; /* saved ifndef_macro */
221 int *ifdef_stack_ptr
; /* ifdef_stack value at the start of the file */
222 char inc_type
; /* type of include */
223 char inc_filename
[512]; /* filename specified by the user */
224 char filename
[1024]; /* current filename - here to simplify code */
225 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
228 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
229 #define CH_EOF (-1) /* end of file */
231 /* parsing state (used to save parser state to reparse part of the
232 source several times) */
233 typedef struct ParseState
{
240 /* used to record tokens */
241 typedef struct TokenString
{
248 /* include file cache, used to find files faster and also to eliminate
249 inclusion if the include file is protected by #ifndef ... #endif */
250 typedef struct CachedInclude
{
252 char type
; /* '"' or '>' to give include type */
253 char filename
[1]; /* path specified in #include */
257 static struct BufferedFile
*file
;
258 static int ch
, tok
, tok1
;
259 static CValue tokc
, tok1c
;
260 static CString tokcstr
; /* current parsed string, if any */
261 /* additionnal informations about token */
262 static int tok_flags
;
263 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
264 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
265 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
267 /* if true, line feed is returned as a token. line feed is also
269 static int return_linefeed
;
270 static Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
271 static Section
*cur_text_section
; /* current section where function code is
273 /* bound check related sections */
274 static Section
*bounds_section
; /* contains global data bound description */
275 static Section
*lbounds_section
; /* contains local data bound description */
276 /* symbol sections */
277 static Section
*symtab_section
, *strtab_section
;
280 static Section
*stab_section
, *stabstr_section
;
282 /* loc : local variable index
283 ind : output code index
285 anon_sym: anonymous symbol index
287 static int rsym
, anon_sym
, ind
, loc
;
288 /* expression generation modifiers */
289 static int const_wanted
; /* true if constant wanted */
290 static int nocode_wanted
; /* true if no code generation wanted for an expression */
291 static int global_expr
; /* true if compound literals must be allocated
292 globally (used during initializers parsing */
293 static CType func_vt
; /* current function return type (used by return
296 static int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
297 static int tok_ident
;
298 static TokenSym
**table_ident
;
299 static TokenSym
*hash_ident
[TOK_HASH_SIZE
];
300 static char token_buf
[STRING_MAX_SIZE
+ 1];
301 static char *funcname
;
302 static Sym
*global_stack
, *local_stack
;
303 static Sym
*define_stack
;
304 static Sym
*label_stack
;
306 static SValue vstack
[VSTACK_SIZE
], *vtop
;
307 static int *macro_ptr
, *macro_ptr_allocated
;
308 /* some predefined types */
309 static CType char_pointer_type
, func_old_type
, int_type
;
311 /* compile with debug symbol (and use them if error during execution) */
312 static int do_debug
= 0;
314 /* compile with built-in memory and bounds checker */
315 static int do_bounds_check
= 0;
317 /* display benchmark infos */
318 static int do_bench
= 0;
319 static int total_lines
;
320 static int total_bytes
;
322 /* use GNU C extensions */
323 static int gnu_ext
= 1;
325 /* use Tiny C extensions */
326 static int tcc_ext
= 1;
328 /* max number of callers shown if error */
329 static int num_callers
= 6;
330 static const char **rt_bound_error_msg
;
332 /* XXX: suppress that ASAP */
333 static struct TCCState
*tcc_state
;
335 /* give the path of the tcc libraries */
336 static const char *tcc_lib_path
= CONFIG_TCC_PREFIX
"/lib/tcc";
341 BufferedFile
**include_stack_ptr
;
342 int *ifdef_stack_ptr
;
344 /* include file handling */
345 char **include_paths
;
346 int nb_include_paths
;
347 char **sysinclude_paths
;
348 int nb_sysinclude_paths
;
349 CachedInclude
**cached_includes
;
350 int nb_cached_includes
;
352 char **library_paths
;
353 int nb_library_paths
;
355 /* array of all loaded dlls (including those referenced by loaded
357 DLLReference
**loaded_dlls
;
362 int nb_sections
; /* number of sections, including first dummy section */
366 unsigned long *got_offsets
;
369 /* give the correspondance from symtab indexes to dynsym indexes */
370 int *symtab_to_dynsym
;
372 /* temporary dynamic symbol sections (for dll loading) */
373 Section
*dynsymtab_section
;
374 /* exported dynamic symbol section */
377 /* if true, static linking is performed */
382 void (*error_func
)(void *opaque
, const char *msg
);
383 int error_set_jmp_enabled
;
384 jmp_buf error_jmp_buf
;
387 /* see include_stack_ptr */
388 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
];
390 /* see ifdef_stack_ptr */
391 int ifdef_stack
[IFDEF_STACK_SIZE
];
394 /* The current value can be: */
395 #define VT_VALMASK 0x00ff
396 #define VT_CONST 0x00f0 /* constant in vc
397 (must be first non register value) */
398 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
399 #define VT_LOCAL 0x00f2 /* offset on stack */
400 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
401 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
402 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
403 #define VT_LVAL 0x0100 /* var is an lvalue */
404 #define VT_SYM 0x0200 /* a symbol value is added */
405 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
406 char/short stored in integer registers) */
407 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
408 dereferencing value */
409 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
410 bounding function call point is in vc */
411 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
412 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
413 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
414 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
417 #define VT_STRUCT_SHIFT 12 /* structure/enum name shift (20 bits left) */
419 #define VT_INT 0 /* integer type */
420 #define VT_BYTE 1 /* signed byte type */
421 #define VT_SHORT 2 /* short type */
422 #define VT_VOID 3 /* void type */
423 #define VT_PTR 4 /* pointer */
424 #define VT_ENUM 5 /* enum definition */
425 #define VT_FUNC 6 /* function type */
426 #define VT_STRUCT 7 /* struct/union definition */
427 #define VT_FLOAT 8 /* IEEE float */
428 #define VT_DOUBLE 9 /* IEEE double */
429 #define VT_LDOUBLE 10 /* IEEE long double */
430 #define VT_BOOL 11 /* ISOC99 boolean type */
431 #define VT_LLONG 12 /* 64 bit integer */
432 #define VT_LONG 13 /* long integer (NEVER USED as type, only
434 #define VT_BTYPE 0x000f /* mask for basic type */
435 #define VT_UNSIGNED 0x0010 /* unsigned type */
436 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
437 #define VT_BITFIELD 0x0040 /* bitfield modifier */
440 #define VT_EXTERN 0x00000080 /* extern definition */
441 #define VT_STATIC 0x00000100 /* static variable */
442 #define VT_TYPEDEF 0x00000200 /* typedef definition */
444 /* type mask (except storage) */
445 #define VT_TYPE (~(VT_EXTERN | VT_STATIC | VT_TYPEDEF))
449 /* warning: the following compare tokens depend on i386 asm code */
461 #define TOK_LAND 0xa0
465 #define TOK_MID 0xa3 /* inc/dec, to void constant */
467 #define TOK_UDIV 0xb0 /* unsigned division */
468 #define TOK_UMOD 0xb1 /* unsigned modulo */
469 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
470 #define TOK_CINT 0xb3 /* number in tokc */
471 #define TOK_CCHAR 0xb4 /* char constant in tokc */
472 #define TOK_STR 0xb5 /* pointer to string in tokc */
473 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
474 #define TOK_LCHAR 0xb7
475 #define TOK_LSTR 0xb8
476 #define TOK_CFLOAT 0xb9 /* float constant */
477 #define TOK_LINENUM 0xba /* line number info */
478 #define TOK_CDOUBLE 0xc0 /* double constant */
479 #define TOK_CLDOUBLE 0xc1 /* long double constant */
480 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
481 #define TOK_ADDC1 0xc3 /* add with carry generation */
482 #define TOK_ADDC2 0xc4 /* add with carry use */
483 #define TOK_SUBC1 0xc5 /* add with carry generation */
484 #define TOK_SUBC2 0xc6 /* add with carry use */
485 #define TOK_CUINT 0xc8 /* unsigned int constant */
486 #define TOK_CLLONG 0xc9 /* long long constant */
487 #define TOK_CULLONG 0xca /* unsigned long long constant */
488 #define TOK_ARROW 0xcb
489 #define TOK_DOTS 0xcc /* three dots */
490 #define TOK_SHR 0xcd /* unsigned shift right */
491 #define TOK_PPNUM 0xce /* preprocessor number */
493 #define TOK_SHL 0x01 /* shift left */
494 #define TOK_SAR 0x02 /* signed shift right */
496 /* assignement operators : normal operator or 0x80 */
497 #define TOK_A_MOD 0xa5
498 #define TOK_A_AND 0xa6
499 #define TOK_A_MUL 0xaa
500 #define TOK_A_ADD 0xab
501 #define TOK_A_SUB 0xad
502 #define TOK_A_DIV 0xaf
503 #define TOK_A_XOR 0xde
504 #define TOK_A_OR 0xfc
505 #define TOK_A_SHL 0x81
506 #define TOK_A_SAR 0x82
508 /* WARNING: the content of this string encodes token numbers */
509 static char tok_two_chars
[] = "<=\236>=\235!=\225&&\240||\241++\244--\242==\224<<\1>>\2+=\253-=\255*=\252/=\257%=\245&=\246^=\336|=\374->\313..\250##\266";
511 #define TOK_EOF (-1) /* end of file */
512 #define TOK_LINEFEED 10 /* line feed */
514 /* all identificators and strings have token above that */
515 #define TOK_IDENT 256
518 TOK_LAST
= TOK_IDENT
- 1,
519 #define DEF(id, str) id,
524 static const char *tcc_keywords
=
525 #define DEF(id, str) str "\0"
530 #define TOK_UIDENT TOK_DEFINE
533 #define snprintf _snprintf
534 #define vsnprintf _vsnprintf
537 #if defined(WIN32) || defined(TCC_UCLIBC)
538 /* currently incorrect */
539 long double strtold(const char *nptr
, char **endptr
)
541 return (long double)strtod(nptr
, endptr
);
543 float strtof(const char *nptr
, char **endptr
)
545 return (float)strtod(nptr
, endptr
);
548 /* XXX: need to define this to use them in non ISOC99 context */
549 extern float strtof (const char *__nptr
, char **__endptr
);
550 extern long double strtold (const char *__nptr
, char **__endptr
);
553 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
554 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
556 static void next(void);
557 static void next_nomacro(void);
558 static void parse_expr_type(CType
*type
);
559 static void expr_type(CType
*type
);
560 static void unary_type(CType
*type
);
561 static int expr_const(void);
562 static void expr_eq(void);
563 static void gexpr(void);
564 static void decl(int l
);
565 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
566 int first
, int size_only
);
567 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
568 int has_init
, int v
, int scope
);
570 void gv2(int rc1
, int rc2
);
571 void move_reg(int r
, int s
);
572 void save_regs(int n
);
573 void save_reg(int r
);
579 static void macro_subst(TokenString
*tok_str
,
580 Sym
**nested_list
, int *macro_str
);
581 int save_reg_forced(int r
);
583 void force_charshort_cast(int t
);
584 static void gen_cast(CType
*type
);
586 static Sym
*sym_find(int v
);
587 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
);
590 int type_size(CType
*type
, int *a
);
591 static inline CType
*pointed_type(CType
*type
);
592 static int pointed_size(CType
*type
);
593 static int lvalue_type(int t
);
594 static int is_compatible_types(CType
*type1
, CType
*type2
);
595 static int parse_btype(CType
*type
, AttributeDef
*ad
);
596 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
);
598 void error(const char *fmt
, ...);
600 void vset(CType
*type
, int r
, int v
);
601 void type_to_str(char *buf
, int buf_size
,
602 CType
*type
, const char *varstr
);
603 char *get_tok_str(int v
, CValue
*cv
);
604 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
605 unsigned long offset
, unsigned long size
);
606 static Sym
*external_global_sym(int v
, CType
*type
, int r
);
608 /* section generation */
609 static void section_realloc(Section
*sec
, unsigned long new_size
);
610 static void *section_ptr_add(Section
*sec
, unsigned long size
);
611 static void put_extern_sym(Sym
*sym
, Section
*section
,
612 unsigned long value
, unsigned long size
);
613 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
614 static int put_elf_str(Section
*s
, const char *sym
);
615 static int put_elf_sym(Section
*s
,
616 unsigned long value
, unsigned long size
,
617 int info
, int other
, int shndx
, const char *name
);
618 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
619 int info
, int sh_num
, const char *name
);
620 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
621 int type
, int symbol
);
622 static void put_stabs(const char *str
, int type
, int other
, int desc
,
623 unsigned long value
);
624 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
625 unsigned long value
, Section
*sec
, int sym_index
);
626 static void put_stabn(int type
, int other
, int desc
, int value
);
627 static void put_stabd(int type
, int other
, int desc
);
628 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
630 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
631 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
632 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
634 /* true if float/double/long double type */
635 static inline int is_float(int t
)
639 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
642 #ifdef TCC_TARGET_I386
643 #include "i386-gen.c"
649 #ifdef CONFIG_TCC_STATIC
651 #define RTLD_LAZY 0x001
652 #define RTLD_NOW 0x002
653 #define RTLD_GLOBAL 0x100
654 #define RTLD_DEFAULT NULL
656 /* dummy function for profiling */
657 void *dlopen(const char *filename
, int flag
)
662 const char *dlerror(void)
667 typedef struct TCCSyms
{
672 #define TCCSYM(a) { #a, &a, },
674 /* add the symbol you want here if no dynamic linking is done */
675 static TCCSyms tcc_syms
[] = {
683 void *dlsym(void *handle
, const char *symbol
)
687 while (p
->str
!= NULL
) {
688 if (!strcmp(p
->str
, symbol
))
697 /********************************************************/
699 /* we use our own 'finite' function to avoid potential problems with
700 non standard math libs */
701 /* XXX: endianness dependant */
702 int ieee_finite(double d
)
705 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
708 /* copy a string and truncate it. */
709 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
716 q_end
= buf
+ buf_size
- 1;
728 /* strcat and truncate. */
729 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
734 pstrcpy(buf
+ len
, buf_size
- len
, s
);
738 /* memory management */
744 static inline void tcc_free(void *ptr
)
747 mem_cur_size
-= malloc_usable_size(ptr
);
752 static void *tcc_malloc(unsigned long size
)
757 error("memory full");
759 mem_cur_size
+= malloc_usable_size(ptr
);
760 if (mem_cur_size
> mem_max_size
)
761 mem_max_size
= mem_cur_size
;
766 static void *tcc_mallocz(unsigned long size
)
769 ptr
= tcc_malloc(size
);
770 memset(ptr
, 0, size
);
774 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
778 mem_cur_size
-= malloc_usable_size(ptr
);
780 ptr1
= realloc(ptr
, size
);
782 /* NOTE: count not correct if alloc error, but not critical */
783 mem_cur_size
+= malloc_usable_size(ptr1
);
784 if (mem_cur_size
> mem_max_size
)
785 mem_max_size
= mem_cur_size
;
790 static char *tcc_strdup(const char *str
)
793 ptr
= tcc_malloc(strlen(str
) + 1);
798 #define free(p) use_tcc_free(p)
799 #define malloc(s) use_tcc_malloc(s)
800 #define realloc(p, s) use_tcc_realloc(p, s)
802 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
809 /* every power of two we double array size */
810 if ((nb
& (nb
- 1)) == 0) {
815 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
817 error("memory full");
824 Section
*new_section(TCCState
*s1
, const char *name
, int sh_type
, int sh_flags
)
828 sec
= tcc_mallocz(sizeof(Section
));
829 pstrcpy(sec
->name
, sizeof(sec
->name
), name
);
830 sec
->sh_type
= sh_type
;
831 sec
->sh_flags
= sh_flags
;
838 sec
->sh_addralign
= 4;
841 sec
->sh_addralign
= 1;
844 sec
->sh_addralign
= 32; /* default conservative alignment */
848 /* only add section if not private */
849 if (!(sh_flags
& SHF_PRIVATE
)) {
850 sec
->sh_num
= s1
->nb_sections
;
851 dynarray_add((void ***)&s1
->sections
, &s1
->nb_sections
, sec
);
856 static void free_section(Section
*s
)
862 /* realloc section and set its content to zero */
863 static void section_realloc(Section
*sec
, unsigned long new_size
)
868 size
= sec
->data_allocated
;
871 while (size
< new_size
)
873 data
= tcc_realloc(sec
->data
, size
);
875 error("memory full");
876 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
878 sec
->data_allocated
= size
;
881 /* reserve at least 'size' bytes in section 'sec' from
883 static void *section_ptr_add(Section
*sec
, unsigned long size
)
885 unsigned long offset
, offset1
;
887 offset
= sec
->data_offset
;
888 offset1
= offset
+ size
;
889 if (offset1
> sec
->data_allocated
)
890 section_realloc(sec
, offset1
);
891 sec
->data_offset
= offset1
;
892 return sec
->data
+ offset
;
895 /* return a reference to a section, and create it if it does not
897 Section
*find_section(TCCState
*s1
, const char *name
)
901 for(i
= 1; i
< s1
->nb_sections
; i
++) {
902 sec
= s1
->sections
[i
];
903 if (!strcmp(name
, sec
->name
))
906 /* sections are created as PROGBITS */
907 return new_section(s1
, name
, SHT_PROGBITS
, SHF_ALLOC
);
910 /* update sym->c so that it points to an external symbol in section
911 'section' with value 'value' */
912 static void put_extern_sym(Sym
*sym
, Section
*section
,
913 unsigned long value
, unsigned long size
)
915 int sym_type
, sym_bind
, sh_num
, info
;
920 sh_num
= section
->sh_num
;
924 if ((sym
->type
.t
& VT_BTYPE
) == VT_FUNC
)
927 sym_type
= STT_OBJECT
;
928 if (sym
->type
.t
& VT_STATIC
)
929 sym_bind
= STB_LOCAL
;
931 sym_bind
= STB_GLOBAL
;
933 name
= get_tok_str(sym
->v
, NULL
);
934 #ifdef CONFIG_TCC_BCHECK
935 if (do_bounds_check
) {
938 /* XXX: avoid doing that for statics ? */
939 /* if bound checking is activated, we change some function
940 names by adding the "__bound" prefix */
943 /* XXX: we rely only on malloc hooks */
955 strcpy(buf
, "__bound_");
962 info
= ELF32_ST_INFO(sym_bind
, sym_type
);
963 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, sh_num
, name
);
965 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
966 esym
->st_value
= value
;
967 esym
->st_size
= size
;
968 esym
->st_shndx
= sh_num
;
972 /* add a new relocation entry to symbol 'sym' in section 's' */
973 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
976 put_extern_sym(sym
, NULL
, 0, 0);
977 /* now we can add ELF relocation info */
978 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
981 static inline int isid(int c
)
983 return (c
>= 'a' && c
<= 'z') ||
984 (c
>= 'A' && c
<= 'Z') ||
988 static inline int isnum(int c
)
990 return c
>= '0' && c
<= '9';
993 static inline int isoct(int c
)
995 return c
>= '0' && c
<= '7';
998 static inline int toup(int c
)
1000 if (c
>= 'a' && c
<= 'z')
1001 return c
- 'a' + 'A';
1006 static void strcat_vprintf(char *buf
, int buf_size
, const char *fmt
, va_list ap
)
1010 vsnprintf(buf
+ len
, buf_size
- len
, fmt
, ap
);
1013 static void strcat_printf(char *buf
, int buf_size
, const char *fmt
, ...)
1017 strcat_vprintf(buf
, buf_size
, fmt
, ap
);
1021 void error1(TCCState
*s1
, int is_warning
, const char *fmt
, va_list ap
)
1028 for(f
= s1
->include_stack
; f
< s1
->include_stack_ptr
; f
++)
1029 strcat_printf(buf
, sizeof(buf
), "In file included from %s:%d:\n",
1030 (*f
)->filename
, (*f
)->line_num
);
1031 if (file
->line_num
> 0) {
1032 strcat_printf(buf
, sizeof(buf
),
1033 "%s:%d: ", file
->filename
, file
->line_num
);
1035 strcat_printf(buf
, sizeof(buf
),
1036 "%s: ", file
->filename
);
1039 strcat_printf(buf
, sizeof(buf
),
1043 strcat_printf(buf
, sizeof(buf
), "warning: ");
1044 strcat_vprintf(buf
, sizeof(buf
), fmt
, ap
);
1046 if (!s1
->error_func
) {
1047 /* default case: stderr */
1048 fprintf(stderr
, "%s\n", buf
);
1050 s1
->error_func(s1
->error_opaque
, buf
);
1057 void tcc_set_error_func(TCCState
*s
, void *error_opaque
,
1058 void (*error_func
)(void *opaque
, const char *msg
))
1060 s
->error_opaque
= error_opaque
;
1061 s
->error_func
= error_func
;
1065 /* error without aborting current compilation */
1066 void error_noabort(const char *fmt
, ...)
1068 TCCState
*s1
= tcc_state
;
1072 error1(s1
, 0, fmt
, ap
);
1076 void error(const char *fmt
, ...)
1078 TCCState
*s1
= tcc_state
;
1082 error1(s1
, 0, fmt
, ap
);
1084 /* better than nothing: in some cases, we accept to handle errors */
1085 if (s1
->error_set_jmp_enabled
) {
1086 longjmp(s1
->error_jmp_buf
, 1);
1088 /* XXX: suppress it someday */
1093 void expect(const char *msg
)
1095 error("%s expected", msg
);
1098 void warning(const char *fmt
, ...)
1100 TCCState
*s1
= tcc_state
;
1104 error1(s1
, 1, fmt
, ap
);
1111 error("'%c' expected", c
);
1115 void test_lvalue(void)
1117 if (!(vtop
->r
& VT_LVAL
))
1121 /* allocate a new token */
1122 static TokenSym
*tok_alloc_new(TokenSym
**pts
, const char *str
, int len
)
1124 TokenSym
*ts
, **ptable
;
1127 if (tok_ident
>= SYM_FIRST_ANOM
)
1128 error("memory full");
1130 /* expand token table if needed */
1131 i
= tok_ident
- TOK_IDENT
;
1132 if ((i
% TOK_ALLOC_INCR
) == 0) {
1133 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1135 error("memory full");
1136 table_ident
= ptable
;
1139 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1140 table_ident
[i
] = ts
;
1141 ts
->tok
= tok_ident
++;
1142 ts
->sym_define
= NULL
;
1143 ts
->sym_label
= NULL
;
1144 ts
->sym_struct
= NULL
;
1145 ts
->sym_identifier
= NULL
;
1147 ts
->hash_next
= NULL
;
1148 memcpy(ts
->str
, str
, len
);
1149 ts
->str
[len
] = '\0';
1154 #define TOK_HASH_INIT 1
1155 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1157 /* find a token and add it if not found */
1158 static TokenSym
*tok_alloc(const char *str
, int len
)
1160 TokenSym
*ts
, **pts
;
1166 h
= TOK_HASH_FUNC(h
, ((unsigned char *)str
)[i
]);
1167 h
&= (TOK_HASH_SIZE
- 1);
1169 pts
= &hash_ident
[h
];
1174 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1176 pts
= &(ts
->hash_next
);
1178 return tok_alloc_new(pts
, str
, len
);
1181 /* CString handling */
1183 static void cstr_realloc(CString
*cstr
, int new_size
)
1188 size
= cstr
->size_allocated
;
1190 size
= 8; /* no need to allocate a too small first string */
1191 while (size
< new_size
)
1193 data
= tcc_realloc(cstr
->data_allocated
, size
);
1195 error("memory full");
1196 cstr
->data_allocated
= data
;
1197 cstr
->size_allocated
= size
;
1202 static void cstr_ccat(CString
*cstr
, int ch
)
1205 size
= cstr
->size
+ 1;
1206 if (size
> cstr
->size_allocated
)
1207 cstr_realloc(cstr
, size
);
1208 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1212 static void cstr_cat(CString
*cstr
, const char *str
)
1224 /* add a wide char */
1225 static void cstr_wccat(CString
*cstr
, int ch
)
1228 size
= cstr
->size
+ sizeof(int);
1229 if (size
> cstr
->size_allocated
)
1230 cstr_realloc(cstr
, size
);
1231 *(int *)(((unsigned char *)cstr
->data
) + size
- sizeof(int)) = ch
;
1235 static void cstr_new(CString
*cstr
)
1237 memset(cstr
, 0, sizeof(CString
));
1240 /* free string and reset it to NULL */
1241 static void cstr_free(CString
*cstr
)
1243 tcc_free(cstr
->data_allocated
);
1247 #define cstr_reset(cstr) cstr_free(cstr)
1249 static CString
*cstr_dup(CString
*cstr1
)
1254 cstr
= tcc_malloc(sizeof(CString
));
1257 cstr
->size_allocated
= size
;
1258 cstr
->data_allocated
= tcc_malloc(size
);
1259 cstr
->data
= cstr
->data_allocated
;
1260 memcpy(cstr
->data_allocated
, cstr1
->data_allocated
, size
);
1264 /* XXX: unicode ? */
1265 static void add_char(CString
*cstr
, int c
)
1267 if (c
== '\'' || c
== '\"' || c
== '\\') {
1268 /* XXX: could be more precise if char or string */
1269 cstr_ccat(cstr
, '\\');
1271 if (c
>= 32 && c
<= 126) {
1274 cstr_ccat(cstr
, '\\');
1276 cstr_ccat(cstr
, 'n');
1278 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1279 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1280 cstr_ccat(cstr
, '0' + (c
& 7));
1285 /* XXX: buffer overflow */
1286 /* XXX: float tokens */
1287 char *get_tok_str(int v
, CValue
*cv
)
1289 static char buf
[STRING_MAX_SIZE
+ 1];
1290 static CString cstr_buf
;
1296 /* NOTE: to go faster, we give a fixed buffer for small strings */
1297 cstr_reset(&cstr_buf
);
1298 cstr_buf
.data
= buf
;
1299 cstr_buf
.size_allocated
= sizeof(buf
);
1305 /* XXX: not quite exact, but only useful for testing */
1306 sprintf(p
, "%u", cv
->ui
);
1310 /* XXX: not quite exact, but only useful for testing */
1311 sprintf(p
, "%Lu", cv
->ull
);
1315 cstr_ccat(&cstr_buf
, '\'');
1316 add_char(&cstr_buf
, cv
->i
);
1317 cstr_ccat(&cstr_buf
, '\'');
1318 cstr_ccat(&cstr_buf
, '\0');
1322 len
= cstr
->size
- 1;
1324 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1325 cstr_ccat(&cstr_buf
, '\0');
1330 cstr_ccat(&cstr_buf
, '\"');
1332 len
= cstr
->size
- 1;
1334 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1336 len
= (cstr
->size
/ sizeof(int)) - 1;
1338 add_char(&cstr_buf
, ((int *)cstr
->data
)[i
]);
1340 cstr_ccat(&cstr_buf
, '\"');
1341 cstr_ccat(&cstr_buf
, '\0');
1350 return strcpy(p
, "<<=");
1352 return strcpy(p
, ">>=");
1354 if (v
< TOK_IDENT
) {
1355 /* search in two bytes table */
1369 } else if (v
< tok_ident
) {
1370 return table_ident
[v
- TOK_IDENT
]->str
;
1371 } else if (v
>= SYM_FIRST_ANOM
) {
1372 /* special name for anonymous symbol */
1373 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1375 /* should never happen */
1380 return cstr_buf
.data
;
1383 /* push, without hashing */
1384 static Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
1387 s
= tcc_malloc(sizeof(Sym
));
1398 /* find a symbol and return its associated structure. 's' is the top
1399 of the symbol stack */
1400 static Sym
*sym_find2(Sym
*s
, int v
)
1410 /* structure lookup */
1411 static Sym
*struct_find(int v
)
1414 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1416 return table_ident
[v
]->sym_struct
;
1419 /* find an identifier */
1420 static inline Sym
*sym_find(int v
)
1423 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
1425 return table_ident
[v
]->sym_identifier
;
1428 /* push a given symbol on the symbol stack */
1429 static Sym
*sym_push(int v
, CType
*type
, int r
, int c
)
1438 s
= sym_push2(ps
, v
, type
->t
, c
);
1439 s
->type
.ref
= type
->ref
;
1441 /* don't record fields or anonymous symbols */
1443 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1444 /* record symbol in token array */
1445 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1447 ps
= &ts
->sym_struct
;
1449 ps
= &ts
->sym_identifier
;
1456 /* push a global identifier */
1457 static Sym
*global_identifier_push(int v
, int t
, int c
)
1460 s
= sym_push2(&global_stack
, v
, t
, c
);
1461 /* don't record anonymous symbol */
1462 if (v
< SYM_FIRST_ANOM
) {
1463 ps
= &table_ident
[v
- TOK_IDENT
]->sym_identifier
;
1464 /* modify the top most local identifier, so that
1465 sym_identifier will point to 's' when popped */
1467 ps
= &(*ps
)->prev_tok
;
1474 /* pop symbols until top reaches 'b' */
1475 static void sym_pop(Sym
**ptop
, Sym
*b
)
1485 /* remove symbol in token array */
1487 if (!(v
& SYM_FIELD
) && (v
& ~SYM_STRUCT
) < SYM_FIRST_ANOM
) {
1488 ts
= table_ident
[(v
& ~SYM_STRUCT
) - TOK_IDENT
];
1490 ps
= &ts
->sym_struct
;
1492 ps
= &ts
->sym_identifier
;
1503 BufferedFile
*tcc_open(TCCState
*s1
, const char *filename
)
1508 fd
= open(filename
, O_RDONLY
);
1511 bf
= tcc_malloc(sizeof(BufferedFile
));
1517 bf
->buf_ptr
= bf
->buffer
;
1518 bf
->buf_end
= bf
->buffer
;
1519 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
1520 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
1522 bf
->ifndef_macro
= 0;
1523 bf
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
1524 // printf("opening '%s'\n", filename);
1528 void tcc_close(BufferedFile
*bf
)
1530 total_lines
+= bf
->line_num
;
1535 /* fill input buffer and peek next char */
1536 static int tcc_peekc_slow(BufferedFile
*bf
)
1539 /* only tries to read if really end of buffer */
1540 if (bf
->buf_ptr
>= bf
->buf_end
) {
1542 #if defined(PARSE_DEBUG)
1547 len
= read(bf
->fd
, bf
->buffer
, len
);
1554 bf
->buf_ptr
= bf
->buffer
;
1555 bf
->buf_end
= bf
->buffer
+ len
;
1556 *bf
->buf_end
= CH_EOB
;
1558 if (bf
->buf_ptr
< bf
->buf_end
) {
1559 return bf
->buf_ptr
[0];
1561 bf
->buf_ptr
= bf
->buf_end
;
1566 /* return the current character, handling end of block if necessary
1568 static int handle_eob(void)
1570 return tcc_peekc_slow(file
);
1573 /* read next char from current input file and handle end of input buffer */
1574 static inline void inp(void)
1576 ch
= *(++(file
->buf_ptr
));
1577 /* end of buffer/file handling */
1582 /* handle '\[\r]\n' */
1583 static void handle_stray(void)
1585 while (ch
== '\\') {
1590 } else if (ch
== '\r') {
1598 error("stray '\\' in program");
1603 /* skip the stray and handle the \\n case. Output an error if
1604 incorrect char after the stray */
1605 static int handle_stray1(uint8_t *p
)
1609 if (p
>= file
->buf_end
) {
1626 /* handle the complicated stray case */
1627 #define PEEKC(c, p)\
1632 c = handle_stray1(p);\
1637 /* input with '\[\r]\n' handling. Note that this function cannot
1638 handle other characters after '\', so you cannot call it inside
1639 strings or comments */
1640 static void minp(void)
1648 static void parse_line_comment(void)
1650 /* single line C++ comments */
1651 /* XXX: accept '\\\n' ? */
1653 while (ch
!= '\n' && ch
!= CH_EOF
)
1657 static void parse_comment(void)
1666 /* fast skip loop */
1669 if (c
== '\n' || c
== '*' || c
== '\\')
1673 if (c
== '\n' || c
== '*' || c
== '\\')
1677 /* now we can handle all the cases */
1681 } else if (c
== '*') {
1687 } else if (c
== '/') {
1688 goto end_of_comment
;
1689 } else if (c
== '\\') {
1693 /* skip '\\n', but if '\' followed but another
1694 char, behave asif a stray was parsed */
1695 ch
= file
->buf_ptr
[0];
1696 while (ch
== '\\') {
1701 } else if (ch
== '\r') {
1719 /* stray, eob or eof */
1724 error("unexpected end of file in comment");
1725 } else if (c
== '\\') {
1738 /* space exlcuding newline */
1739 static inline int is_space(int ch
)
1741 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
1744 static inline void skip_spaces(void)
1746 while (is_space(ch
))
1750 /* skip block of text until #else, #elif or #endif. skip also pairs of
1752 void preprocess_skip(void)
1754 int a
, start_of_line
, sep
, c
;
1781 } else if (c
== '\\') {
1782 /* XXX: incorrect: should not give an error */
1783 ch
= file
->buf_ptr
[0];
1797 } else if (c
== '\\') {
1802 /* XXX: better error message */
1803 error("unterminated string");
1804 } else if (c
== '\\') {
1805 /* ignore next char */
1815 else if (c
!= CH_EOF
)
1818 } else if (c
== '\n') {
1834 } else if (ch
== '/') {
1835 parse_line_comment();
1842 if (start_of_line
) {
1847 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
1849 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
1851 else if (tok
== TOK_ENDIF
)
1865 /* ParseState handling */
1867 /* XXX: currently, no include file info is stored. Thus, we cannot display
1868 accurate messages if the function or data definition spans multiple
1871 /* save current parse state in 's' */
1872 void save_parse_state(ParseState
*s
)
1874 s
->line_num
= file
->line_num
;
1875 s
->macro_ptr
= macro_ptr
;
1880 /* restore parse state from 's' */
1881 void restore_parse_state(ParseState
*s
)
1883 file
->line_num
= s
->line_num
;
1884 macro_ptr
= s
->macro_ptr
;
1889 /* return the number of additionnal 'ints' necessary to store the
1891 static inline int tok_ext_size(int t
)
1910 return LDOUBLE_SIZE
/ 4;
1916 /* token string handling */
1918 static inline void tok_str_new(TokenString
*s
)
1922 s
->allocated_len
= 0;
1923 s
->last_line_num
= -1;
1926 static void tok_str_free(int *str
)
1935 /* NOTE: we test zero separately so that GCC can generate a
1936 table for the following switch */
1951 /* XXX: use a macro to be portable on 64 bit ? */
1952 cstr
= (CString
*)p
[1];
1963 p
+= 1 + (LDOUBLE_SIZE
/ 4);
1973 static int *tok_str_realloc(TokenString
*s
)
1977 len
= s
->allocated_len
+ TOK_STR_ALLOC_INCR
;
1978 str
= tcc_realloc(s
->str
, len
* sizeof(int));
1980 error("memory full");
1981 s
->allocated_len
= len
;
1986 static void tok_str_add(TokenString
*s
, int t
)
1992 if (len
>= s
->allocated_len
)
1993 str
= tok_str_realloc(s
);
1998 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
2005 /* allocate space for worst case */
2006 if (len
+ TOK_MAX_SIZE
> s
->allocated_len
)
2007 str
= tok_str_realloc(s
);
2016 str
[len
++] = cv
->tab
[0];
2021 str
[len
++] = (int)cstr_dup(cv
->cstr
);
2026 str
[len
++] = cv
->tab
[0];
2027 str
[len
++] = cv
->tab
[1];
2030 #if LDOUBLE_SIZE == 12
2031 str
[len
++] = cv
->tab
[0];
2032 str
[len
++] = cv
->tab
[1];
2033 str
[len
++] = cv
->tab
[2];
2035 #error add long double size support
2044 /* add the current parse token in token string 's' */
2045 static void tok_str_add_tok(TokenString
*s
)
2049 /* save line number info */
2050 if (file
->line_num
!= s
->last_line_num
) {
2051 s
->last_line_num
= file
->line_num
;
2052 cval
.i
= s
->last_line_num
;
2053 tok_str_add2(s
, TOK_LINENUM
, &cval
);
2055 tok_str_add2(s
, tok
, &tokc
);
2058 #if LDOUBLE_SIZE == 12
2059 #define LDOUBLE_GET(p, cv) \
2064 #error add long double size support
2068 /* get a token from an integer array and increment pointer
2069 accordingly. we code it as a macro to avoid pointer aliasing. */
2070 #define TOK_GET(t, p, cv) \
2092 case TOK_CLDOUBLE: \
2093 LDOUBLE_GET(p, cv); \
2094 p += LDOUBLE_SIZE / 4; \
2101 /* defines handling */
2102 static inline void define_push(int v
, int macro_type
, int *str
, Sym
*first_arg
)
2106 s
= sym_push2(&define_stack
, v
, macro_type
, (int)str
);
2107 s
->next
= first_arg
;
2108 table_ident
[v
- TOK_IDENT
]->sym_define
= s
;
2111 /* undefined a define symbol. Its name is just set to zero */
2112 static void define_undef(Sym
*s
)
2116 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2117 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2121 static inline Sym
*define_find(int v
)
2124 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2126 return table_ident
[v
]->sym_define
;
2129 /* free define stack until top reaches 'b' */
2130 static void free_defines(Sym
*b
)
2138 /* do not free args or predefined defines */
2140 tok_str_free((int *)top
->c
);
2142 if (v
>= TOK_IDENT
&& v
< tok_ident
)
2143 table_ident
[v
- TOK_IDENT
]->sym_define
= NULL
;
2151 static Sym
*label_find(int v
)
2154 if ((unsigned)v
>= (unsigned)(tok_ident
- TOK_IDENT
))
2156 return table_ident
[v
]->sym_label
;
2159 static Sym
*label_push(int v
, int flags
)
2162 s
= sym_push2(&label_stack
, v
, 0, 0);
2164 table_ident
[v
- TOK_IDENT
]->sym_label
= s
;
2168 /* eval an expression for #if/#elif */
2169 static int expr_preprocess(void)
2175 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2176 next(); /* do macro subst */
2177 if (tok
== TOK_DEFINED
) {
2182 c
= define_find(tok
) != 0;
2187 } else if (tok
>= TOK_IDENT
) {
2188 /* if undefined macro */
2192 tok_str_add_tok(&str
);
2194 tok_str_add(&str
, -1); /* simulate end of file */
2195 tok_str_add(&str
, 0);
2196 /* now evaluate C constant expression */
2197 macro_ptr
= str
.str
;
2201 tok_str_free(str
.str
);
2205 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2206 static void tok_print(int *str
)
2212 TOK_GET(t
, str
, cval
);
2215 printf(" %s", get_tok_str(t
, &cval
));
2221 /* parse after #define */
2222 static void parse_define(void)
2224 Sym
*s
, *first
, **ps
;
2225 int v
, t
, varg
, is_vaargs
, c
;
2230 error("invalid macro name '%s'", get_tok_str(tok
, &tokc
));
2231 /* XXX: should check if same macro (ANSI) */
2234 /* '(' must be just after macro definition for MACRO_FUNC */
2235 c
= file
->buf_ptr
[0];
2237 c
= handle_stray1(file
->buf_ptr
);
2242 while (tok
!= ')') {
2246 if (varg
== TOK_DOTS
) {
2247 varg
= TOK___VA_ARGS__
;
2249 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
2253 if (varg
< TOK_IDENT
)
2254 error("badly punctuated parameter list");
2255 s
= sym_push2(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
2266 /* EOF testing necessary for '-D' handling */
2267 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
2268 tok_str_add2(&str
, tok
, &tokc
);
2271 tok_str_add(&str
, 0);
2273 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
2276 define_push(v
, t
, str
.str
, first
);
2279 /* XXX: use a token or a hash table to accelerate matching ? */
2280 static CachedInclude
*search_cached_include(TCCState
*s1
,
2281 int type
, const char *filename
)
2286 for(i
= 0;i
< s1
->nb_cached_includes
; i
++) {
2287 e
= s1
->cached_includes
[i
];
2288 if (e
->type
== type
&& !strcmp(e
->filename
, filename
))
2294 static inline void add_cached_include(TCCState
*s1
, int type
,
2295 const char *filename
, int ifndef_macro
)
2299 if (search_cached_include(s1
, type
, filename
))
2302 printf("adding cached '%s' %s\n", filename
, get_tok_str(ifndef_macro
, NULL
));
2304 e
= tcc_malloc(sizeof(CachedInclude
) + strlen(filename
));
2308 strcpy(e
->filename
, filename
);
2309 e
->ifndef_macro
= ifndef_macro
;
2310 dynarray_add((void ***)&s1
->cached_includes
, &s1
->nb_cached_includes
, e
);
2313 /* is_bof is true if first non space token at beginning of file */
2314 static void preprocess(int is_bof
)
2316 TCCState
*s1
= tcc_state
;
2318 char buf
[1024], *q
, *p
;
2324 return_linefeed
= 1; /* linefeed will be returned as a
2325 token. EOF is also returned as line feed */
2335 s
= define_find(tok
);
2336 /* undefine symbol by putting an invalid name */
2341 ch
= file
->buf_ptr
[0];
2342 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2347 } else if (ch
== '\"') {
2350 /* XXX: better stray handling */
2353 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
2354 if ((q
- buf
) < sizeof(buf
) - 1)
2361 /* eat all spaces and comments after include */
2362 /* XXX: slightly incorrect */
2363 while (ch1
!= '\n' && ch1
!= CH_EOF
)
2367 /* computed #include : either we have only strings or
2368 we have anything enclosed in '<>' */
2371 if (tok
== TOK_STR
) {
2372 while (tok
!= TOK_LINEFEED
) {
2373 if (tok
!= TOK_STR
) {
2375 error("'#include' expects \"FILENAME\" or <FILENAME>");
2377 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
2383 while (tok
!= TOK_LINEFEED
) {
2384 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
2388 /* check syntax and remove '<>' */
2389 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
2390 goto include_syntax
;
2391 memmove(buf
, buf
+ 1, len
- 2);
2392 buf
[len
- 2] = '\0';
2397 e
= search_cached_include(s1
, c
, buf
);
2398 if (e
&& define_find(e
->ifndef_macro
)) {
2399 /* no need to parse the include because the 'ifndef macro'
2402 printf("%s: skipping %s\n", file
->filename
, buf
);
2406 /* first search in current dir if "header.h" */
2408 p
= strrchr(file
->filename
, '/');
2410 size
= p
+ 1 - file
->filename
;
2411 if (size
> sizeof(buf1
) - 1)
2412 size
= sizeof(buf1
) - 1;
2413 memcpy(buf1
, file
->filename
, size
);
2415 pstrcat(buf1
, sizeof(buf1
), buf
);
2416 f
= tcc_open(s1
, buf1
);
2420 if (s1
->include_stack_ptr
>= s1
->include_stack
+ INCLUDE_STACK_SIZE
)
2421 error("#include recursion too deep");
2422 /* now search in all the include paths */
2423 n
= s1
->nb_include_paths
+ s1
->nb_sysinclude_paths
;
2424 for(i
= 0; i
< n
; i
++) {
2426 if (i
< s1
->nb_include_paths
)
2427 path
= s1
->include_paths
[i
];
2429 path
= s1
->sysinclude_paths
[i
- s1
->nb_include_paths
];
2430 pstrcpy(buf1
, sizeof(buf1
), path
);
2431 pstrcat(buf1
, sizeof(buf1
), "/");
2432 pstrcat(buf1
, sizeof(buf1
), buf
);
2433 f
= tcc_open(s1
, buf1
);
2437 error("include file '%s' not found", buf
);
2441 printf("%s: including %s\n", file
->filename
, buf1
);
2444 pstrcpy(f
->inc_filename
, sizeof(f
->inc_filename
), buf
);
2445 /* push current file in stack */
2446 /* XXX: fix current line init */
2447 *s1
->include_stack_ptr
++ = file
;
2449 /* add include file debug info */
2451 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
2453 tok_flags
|= TOK_FLAG_BOF
| TOK_FLAG_BOL
;
2454 ch
= file
->buf_ptr
[0];
2462 c
= expr_preprocess();
2468 if (tok
< TOK_IDENT
)
2469 error("invalid argument for '#if%sdef'", c
? "n" : "");
2473 printf("#ifndef %s\n", get_tok_str(tok
, NULL
));
2475 file
->ifndef_macro
= tok
;
2478 c
= (define_find(tok
) != 0) ^ c
;
2480 if (s1
->ifdef_stack_ptr
>= s1
->ifdef_stack
+ IFDEF_STACK_SIZE
)
2481 error("memory full");
2482 *s1
->ifdef_stack_ptr
++ = c
;
2485 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2486 error("#else without matching #if");
2487 if (s1
->ifdef_stack_ptr
[-1] & 2)
2488 error("#else after #else");
2489 c
= (s1
->ifdef_stack_ptr
[-1] ^= 3);
2492 if (s1
->ifdef_stack_ptr
== s1
->ifdef_stack
)
2493 error("#elif without matching #if");
2494 c
= s1
->ifdef_stack_ptr
[-1];
2496 error("#elif after #else");
2497 /* last #if/#elif expression was true: we skip */
2500 c
= expr_preprocess();
2501 s1
->ifdef_stack_ptr
[-1] = c
;
2511 if (s1
->ifdef_stack_ptr
<= file
->ifdef_stack_ptr
)
2512 error("#endif without matching #if");
2513 s1
->ifdef_stack_ptr
--;
2514 /* '#ifndef macro' was at the start of file. Now we check if
2515 an '#endif' is exactly at the end of file */
2516 if (file
->ifndef_macro
&&
2517 s1
->ifdef_stack_ptr
== file
->ifdef_stack_ptr
) {
2518 file
->ifndef_macro_saved
= file
->ifndef_macro
;
2519 /* need to set to zero to avoid false matches if another
2520 #ifndef at middle of file */
2521 file
->ifndef_macro
= 0;
2522 while (tok
!= TOK_LINEFEED
)
2524 tok_flags
|= TOK_FLAG_ENDIF
;
2530 if (tok
!= TOK_CINT
)
2532 file
->line_num
= tokc
.i
- 1; /* the line number will be incremented after */
2534 if (tok
!= TOK_LINEFEED
) {
2537 pstrcpy(file
->filename
, sizeof(file
->filename
),
2538 (char *)tokc
.cstr
->data
);
2546 while (ch
!= '\n' && ch
!= CH_EOF
) {
2547 if ((q
- buf
) < sizeof(buf
) - 1)
2553 error("#error %s", buf
);
2555 warning("#warning %s", buf
);
2558 if (tok
== TOK_LINEFEED
|| tok
== '!' || tok
== TOK_CINT
) {
2559 /* '!' is ignored to allow C scripts. numbers are ignored
2560 to emulate cpp behaviour */
2562 error("invalid preprocessing directive #%s", get_tok_str(tok
, &tokc
));
2566 /* ignore other preprocess commands or #! for C scripts */
2567 while (tok
!= TOK_LINEFEED
)
2570 return_linefeed
= 0;
2573 /* read a number in base b */
2574 static int getn(int b
)
2579 if (ch
>= 'a' && ch
<= 'f')
2581 else if (ch
>= 'A' && ch
<= 'F')
2587 if (t
< 0 || t
>= b
)
2595 /* read a character for string or char constant and eval escape codes */
2596 static int getq(void)
2605 case '0': case '1': case '2': case '3':
2606 case '4': case '5': case '6': case '7':
2607 /* at most three octal digits */
2611 c
= c
* 8 + ch
- '0';
2614 c
= c
* 8 + ch
- '0';
2645 goto invalid_escape
;
2660 goto invalid_escape
;
2665 error("invalid escaped char");
2668 } else if (c
== '\r' && ch
== '\n') {
2675 /* we use 64 bit numbers */
2678 /* bn = (bn << shift) | or_val */
2679 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2683 for(i
=0;i
<BN_SIZE
;i
++) {
2685 bn
[i
] = (v
<< shift
) | or_val
;
2686 or_val
= v
>> (32 - shift
);
2690 void bn_zero(unsigned int *bn
)
2693 for(i
=0;i
<BN_SIZE
;i
++) {
2698 /* parse number in null terminated string 'p' and return it in the
2700 void parse_number(const char *p
)
2702 int b
, t
, shift
, frac_bits
, s
, exp_val
, ch
;
2704 unsigned int bn
[BN_SIZE
];
2715 goto float_frac_parse
;
2716 } else if (t
== '0') {
2717 if (ch
== 'x' || ch
== 'X') {
2721 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2727 /* parse all digits. cannot check octal numbers at this stage
2728 because of floating point constants */
2730 if (ch
>= 'a' && ch
<= 'f')
2732 else if (ch
>= 'A' && ch
<= 'F')
2740 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2742 error("number too long");
2748 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2749 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2751 /* NOTE: strtox should support that for hexa numbers, but
2752 non ISOC99 libcs do not support it, so we prefer to do
2754 /* hexadecimal or binary floats */
2755 /* XXX: handle overflows */
2767 } else if (t
>= 'a') {
2769 } else if (t
>= 'A') {
2774 bn_lshift(bn
, shift
, t
);
2781 if (t
>= 'a' && t
<= 'f') {
2783 } else if (t
>= 'A' && t
<= 'F') {
2785 } else if (t
>= '0' && t
<= '9') {
2791 error("invalid digit");
2792 bn_lshift(bn
, shift
, t
);
2797 if (ch
!= 'p' && ch
!= 'P')
2804 } else if (ch
== '-') {
2808 if (ch
< '0' || ch
> '9')
2809 expect("exponent digits");
2810 while (ch
>= '0' && ch
<= '9') {
2811 exp_val
= exp_val
* 10 + ch
- '0';
2814 exp_val
= exp_val
* s
;
2816 /* now we can generate the number */
2817 /* XXX: should patch directly float number */
2818 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2819 d
= ldexp(d
, exp_val
- frac_bits
);
2824 /* float : should handle overflow */
2826 } else if (t
== 'L') {
2829 /* XXX: not large enough */
2830 tokc
.ld
= (long double)d
;
2836 /* decimal floats */
2838 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2843 while (ch
>= '0' && ch
<= '9') {
2844 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2850 if (ch
== 'e' || ch
== 'E') {
2851 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2855 if (ch
== '-' || ch
== '+') {
2856 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2861 if (ch
< '0' || ch
> '9')
2862 expect("exponent digits");
2863 while (ch
>= '0' && ch
<= '9') {
2864 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2876 tokc
.f
= strtof(token_buf
, NULL
);
2877 } else if (t
== 'L') {
2880 tokc
.ld
= strtold(token_buf
, NULL
);
2883 tokc
.d
= strtod(token_buf
, NULL
);
2887 unsigned long long n
, n1
;
2890 /* integer number */
2893 if (b
== 10 && *q
== '0') {
2900 /* no need for checks except for base 10 / 8 errors */
2903 } else if (t
>= 'a') {
2905 } else if (t
>= 'A') {
2910 error("invalid digit");
2914 /* detect overflow */
2915 /* XXX: this test is not reliable */
2917 error("integer constant overflow");
2920 /* XXX: not exactly ANSI compliant */
2921 if ((n
& 0xffffffff00000000LL
) != 0) {
2926 } else if (n
> 0x7fffffff) {
2937 error("three 'l's in integer constant");
2940 if (tok
== TOK_CINT
)
2942 else if (tok
== TOK_CUINT
)
2946 } else if (t
== 'U') {
2948 error("two 'u's in integer constant");
2950 if (tok
== TOK_CINT
)
2952 else if (tok
== TOK_CLLONG
)
2959 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2967 #define PARSE2(c1, tok1, c2, tok2) \
2978 /* return next token without macro substitution */
2979 static inline void next_nomacro1(void)
2999 /* first look if it is in fact an end of buffer */
3000 if (p
>= file
->buf_end
) {
3004 if (p
>= file
->buf_end
)
3017 TCCState
*s1
= tcc_state
;
3019 if (return_linefeed
) {
3021 } else if (s1
->include_stack_ptr
== s1
->include_stack
) {
3022 /* no include left : end of file */
3025 /* pop include file */
3027 /* test if previous '#endif' was after a #ifdef at
3029 if (tok_flags
& TOK_FLAG_ENDIF
) {
3031 printf("#endif %s\n", get_tok_str(file
->ifndef_macro_saved
, NULL
));
3033 add_cached_include(s1
, file
->inc_type
, file
->inc_filename
,
3034 file
->ifndef_macro_saved
);
3037 /* add end of include file debug info */
3039 put_stabd(N_EINCL
, 0, 0);
3041 /* pop include stack */
3043 s1
->include_stack_ptr
--;
3044 file
= *s1
->include_stack_ptr
;
3052 if (return_linefeed
) {
3056 tok_flags
|= TOK_FLAG_BOL
;
3065 if (tok_flags
& TOK_FLAG_BOL
) {
3067 preprocess(tok_flags
& TOK_FLAG_BOF
);
3073 tok
= TOK_TWOSHARPS
;
3080 case 'a': case 'b': case 'c': case 'd':
3081 case 'e': case 'f': case 'g': case 'h':
3082 case 'i': case 'j': case 'k': case 'l':
3083 case 'm': case 'n': case 'o': case 'p':
3084 case 'q': case 'r': case 's': case 't':
3085 case 'u': case 'v': case 'w': case 'x':
3087 case 'A': case 'B': case 'C': case 'D':
3088 case 'E': case 'F': case 'G': case 'H':
3089 case 'I': case 'J': case 'K':
3090 case 'M': case 'N': case 'O': case 'P':
3091 case 'Q': case 'R': case 'S': case 'T':
3092 case 'U': case 'V': case 'W': case 'X':
3098 h
= TOK_HASH_FUNC(h
, c
);
3102 if (!isid(c
) && !isnum(c
))
3104 h
= TOK_HASH_FUNC(h
, c
);
3111 /* fast case : no stray found, so we have the full token
3112 and we have already hashed it */
3114 h
&= (TOK_HASH_SIZE
- 1);
3115 pts
= &hash_ident
[h
];
3120 if (ts
->len
== len
&& !memcmp(ts
->str
, p1
, len
))
3122 pts
= &(ts
->hash_next
);
3124 ts
= tok_alloc_new(pts
, p1
, len
);
3128 cstr_reset(&tokcstr
);
3131 cstr_ccat(&tokcstr
, *p1
);
3137 while (isid(c
) || isnum(c
)) {
3138 cstr_ccat(&tokcstr
, c
);
3141 ts
= tok_alloc(tokcstr
.data
, tokcstr
.size
);
3147 if (t
!= '\\' && t
!= '\'' && t
!= '\"') {
3149 goto parse_ident_fast
;
3155 } else if (c
== '\"') {
3159 cstr_reset(&tokcstr
);
3160 cstr_ccat(&tokcstr
, 'L');
3161 goto parse_ident_slow
;
3165 case '0': case '1': case '2': case '3':
3166 case '4': case '5': case '6': case '7':
3169 cstr_reset(&tokcstr
);
3170 /* after the first digit, accept digits, alpha, '.' or sign if
3171 prefixed by 'eEpP' */
3175 cstr_ccat(&tokcstr
, c
);
3177 if (!(isnum(c
) || isid(c
) || c
== '.' ||
3178 ((c
== '+' || c
== '-') &&
3179 (t
== 'e' || t
== 'E' || t
== 'p' || t
== 'P'))))
3182 /* We add a trailing '\0' to ease parsing */
3183 cstr_ccat(&tokcstr
, '\0');
3184 tokc
.cstr
= &tokcstr
;
3188 /* special dot handling because it can also start a number */
3191 cstr_reset(&tokcstr
);
3192 cstr_ccat(&tokcstr
, '.');
3194 } else if (c
== '.') {
3210 /* this cast is needed if >= 128 */
3211 if (tok
== TOK_CCHAR
)
3215 error("unterminated character constant");
3224 cstr_reset(&tokcstr
);
3225 while (ch
!= '\"') {
3228 error("unterminated string");
3230 cstr_ccat(&tokcstr
, b
);
3232 cstr_wccat(&tokcstr
, b
);
3235 cstr_ccat(&tokcstr
, '\0');
3237 cstr_wccat(&tokcstr
, '\0');
3238 tokc
.cstr
= &tokcstr
;
3248 } else if (c
== '<') {
3266 } else if (c
== '>') {
3284 } else if (c
== '=') {
3297 } else if (c
== '=') {
3310 } else if (c
== '=') {
3323 } else if (c
== '=') {
3326 } else if (c
== '>') {
3334 PARSE2('!', '!', '=', TOK_NE
)
3335 PARSE2('=', '=', '=', TOK_EQ
)
3336 PARSE2('*', '*', '=', TOK_A_MUL
)
3337 PARSE2('%', '%', '=', TOK_A_MOD
)
3338 PARSE2('^', '^', '=', TOK_A_XOR
)
3340 /* comments or operator */
3348 } else if (c
== '/') {
3350 parse_line_comment();
3353 } else if (c
== '=') {
3377 error("unrecognized character \\x%02x", c
);
3382 #if defined(PARSE_DEBUG)
3383 printf("token = %s\n", get_tok_str(tok
, &tokc
));
3387 /* return next token without macro substitution. Can read input from
3389 static void next_nomacro(void)
3395 TOK_GET(tok
, macro_ptr
, tokc
);
3396 if (tok
== TOK_LINENUM
) {
3397 file
->line_num
= tokc
.i
;
3406 /* substitute args in macro_str and return allocated string */
3407 static int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
3409 int *st
, last_tok
, t
, notfirst
;
3418 TOK_GET(t
, macro_str
, cval
);
3423 TOK_GET(t
, macro_str
, cval
);
3426 s
= sym_find2(args
, t
);
3433 cstr_ccat(&cstr
, ' ');
3434 TOK_GET(t
, st
, cval
);
3435 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
3438 cstr_ccat(&cstr
, '\0');
3440 printf("stringize: %s\n", (char *)cstr
.data
);
3444 tok_str_add2(&str
, TOK_STR
, &cval
);
3447 tok_str_add2(&str
, t
, &cval
);
3449 } else if (t
>= TOK_IDENT
) {
3450 s
= sym_find2(args
, t
);
3453 /* if '##' is present before or after, no arg substitution */
3454 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
3455 /* special case for var arg macros : ## eats the
3456 ',' if empty VA_ARGS variable. */
3457 /* XXX: test of the ',' is not 100%
3458 reliable. should fix it to avoid security
3460 if (gnu_ext
&& s
->type
.t
&&
3461 last_tok
== TOK_TWOSHARPS
&&
3462 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
3464 /* suppress ',' '##' */
3467 /* suppress '##' and add variable */
3475 TOK_GET(t1
, st
, cval
);
3478 tok_str_add2(&str
, t1
, &cval
);
3482 macro_subst(&str
, nested_list
, st
);
3485 tok_str_add(&str
, t
);
3488 tok_str_add2(&str
, t
, &cval
);
3492 tok_str_add(&str
, 0);
3496 /* handle the '##' operator */
3497 static int *macro_twosharps(void)
3502 const char *p1
, *p2
;
3504 TokenString macro_str1
;
3508 tok_str_new(¯o_str1
);
3514 while (*macro_ptr
== TOK_TWOSHARPS
) {
3516 macro_ptr1
= macro_ptr
;
3519 TOK_GET(t
, macro_ptr
, cval
);
3521 /* We concatenate the two tokens if we have an
3522 identifier or a preprocessing number */
3524 p1
= get_tok_str(tok
, &tokc
);
3525 cstr_cat(&cstr
, p1
);
3526 p2
= get_tok_str(t
, &cval
);
3527 cstr_cat(&cstr
, p2
);
3528 cstr_ccat(&cstr
, '\0');
3530 if ((tok
>= TOK_IDENT
|| tok
== TOK_PPNUM
) &&
3531 (t
>= TOK_IDENT
|| t
== TOK_PPNUM
)) {
3532 if (tok
== TOK_PPNUM
) {
3533 /* if number, then create a number token */
3534 /* NOTE: no need to allocate because
3535 tok_str_add2() does it */
3538 /* if identifier, we must do a test to
3539 validate we have a correct identifier */
3540 if (t
== TOK_PPNUM
) {
3550 if (!isnum(c
) && !isid(c
))
3554 ts
= tok_alloc(cstr
.data
, strlen(cstr
.data
));
3555 tok
= ts
->tok
; /* modify current token */
3558 const char *str
= cstr
.data
;
3559 const unsigned char *q
;
3561 /* we look for a valid token */
3562 /* XXX: do more extensive checks */
3563 if (!strcmp(str
, ">>=")) {
3565 } else if (!strcmp(str
, "<<=")) {
3567 } else if (strlen(str
) == 2) {
3568 /* search in two bytes table */
3573 if (q
[0] == str
[0] && q
[1] == str
[1])
3580 /* NOTE: because get_tok_str use a static buffer,
3583 p1
= get_tok_str(tok
, &tokc
);
3584 cstr_cat(&cstr
, p1
);
3585 cstr_ccat(&cstr
, '\0');
3586 p2
= get_tok_str(t
, &cval
);
3587 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr
.data
, p2
);
3588 /* cannot merge tokens: just add them separately */
3589 tok_str_add2(¯o_str1
, tok
, &tokc
);
3590 /* XXX: free associated memory ? */
3597 tok_str_add2(¯o_str1
, tok
, &tokc
);
3600 tok_str_add(¯o_str1
, 0);
3601 return macro_str1
.str
;
3605 /* do macro substitution of current token with macro 's' and add
3606 result to (tok_str,tok_len). 'nested_list' is the list of all
3607 macros we got inside to avoid recursing. Return non zero if no
3608 substitution needs to be done */
3609 static int macro_subst_tok(TokenString
*tok_str
,
3610 Sym
**nested_list
, Sym
*s
)
3612 Sym
*args
, *sa
, *sa1
;
3613 int mstr_allocated
, parlevel
, *mstr
, t
;
3619 /* if symbol is a macro, prepare substitution */
3620 /* if nested substitution, do nothing */
3621 if (sym_find2(*nested_list
, tok
))
3624 /* special macros */
3625 if (tok
== TOK___LINE__
) {
3626 cval
.i
= file
->line_num
;
3627 tok_str_add2(tok_str
, TOK_CINT
, &cval
);
3628 } else if (tok
== TOK___FILE__
) {
3629 cstrval
= file
->filename
;
3631 tok_str_add2(tok_str
, TOK_STR
, &cval
);
3632 } else if (tok
== TOK___DATE__
) {
3633 cstrval
= "Jan 1 2002";
3635 } else if (tok
== TOK___TIME__
) {
3636 cstrval
= "00:00:00";
3639 cstr_cat(&cstr
, cstrval
);
3640 cstr_ccat(&cstr
, '\0');
3642 tok_str_add2(tok_str
, TOK_STR
, &cval
);
3647 if (s
->type
.t
== MACRO_FUNC
) {
3648 /* NOTE: we do not use next_nomacro to avoid eating the
3649 next token. XXX: find better solution */
3653 /* XXX: incorrect with comments */
3654 ch
= file
->buf_ptr
[0];
3655 while (is_space(ch
) || ch
== '\n')
3659 if (t
!= '(') /* no macro subst */
3662 /* argument macro */
3667 /* NOTE: empty args are allowed, except if no args */
3669 /* handle '()' case */
3670 if (!args
&& tok
== ')')
3673 error("macro '%s' used with too many args",
3674 get_tok_str(s
->v
, 0));
3677 /* NOTE: non zero sa->t indicates VA_ARGS */
3678 while ((parlevel
> 0 ||
3680 (tok
!= ',' || sa
->type
.t
))) &&
3684 else if (tok
== ')')
3686 tok_str_add2(&str
, tok
, &tokc
);
3689 tok_str_add(&str
, 0);
3690 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->type
.t
, (int)str
.str
);
3693 /* special case for gcc var args: add an empty
3694 var arg argument if it is omitted */
3695 if (sa
&& sa
->type
.t
&& gnu_ext
)
3705 error("macro '%s' used with too few args",
3706 get_tok_str(s
->v
, 0));
3709 /* now subst each arg */
3710 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
3715 tok_str_free((int *)sa
->c
);
3721 sym_push2(nested_list
, s
->v
, 0, 0);
3722 macro_subst(tok_str
, nested_list
, mstr
);
3723 /* pop nested defined symbol */
3725 *nested_list
= sa1
->prev
;
3733 /* do macro substitution of macro_str and add result to
3734 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3735 inside to avoid recursing. */
3736 static void macro_subst(TokenString
*tok_str
,
3737 Sym
**nested_list
, int *macro_str
)
3740 int *saved_macro_ptr
;
3743 saved_macro_ptr
= macro_ptr
;
3744 macro_ptr
= macro_str
;
3745 /* first scan for '##' operator handling */
3746 macro_str1
= macro_twosharps();
3747 macro_ptr
= macro_str1
;
3753 s
= define_find(tok
);
3755 if (macro_subst_tok(tok_str
, nested_list
, s
) != 0)
3759 tok_str_add2(tok_str
, tok
, &tokc
);
3762 macro_ptr
= saved_macro_ptr
;
3763 tok_str_free(macro_str1
);
3766 /* return next token with macro substitution */
3767 static void next(void)
3769 Sym
*nested_list
, *s
;
3772 /* special 'ungettok' case for label parsing */
3781 /* if not reading from macro substituted string, then try
3782 to substitute macros */
3783 if (tok
>= TOK_IDENT
) {
3784 s
= define_find(tok
);
3786 /* we have a macro: we try to substitute */
3789 if (macro_subst_tok(&str
, &nested_list
, s
) == 0) {
3790 /* substitution done, NOTE: maybe empty */
3791 tok_str_add(&str
, 0);
3792 macro_ptr
= str
.str
;
3793 macro_ptr_allocated
= str
.str
;
3800 /* end of macro string: free it */
3801 tok_str_free(macro_ptr_allocated
);
3807 /* convert preprocessor tokens into C tokens */
3808 if (tok
== TOK_PPNUM
) {
3809 parse_number((char *)tokc
.cstr
->data
);
3814 void swap(int *p
, int *q
)
3822 void vsetc(CType
*type
, int r
, CValue
*vc
)
3826 if (vtop
>= vstack
+ VSTACK_SIZE
)
3827 error("memory full");
3828 /* cannot let cpu flags if other instruction are generated. Also
3829 avoid leaving VT_JMP anywhere except on the top of the stack
3830 because it would complicate the code generator. */
3831 if (vtop
>= vstack
) {
3832 v
= vtop
->r
& VT_VALMASK
;
3833 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
3839 vtop
->r2
= VT_CONST
;
3843 /* push integer constant */
3848 vsetc(&int_type
, VT_CONST
, &cval
);
3851 /* Return a static symbol pointing to a section */
3852 static Sym
*get_sym_ref(CType
*type
, Section
*sec
,
3853 unsigned long offset
, unsigned long size
)
3859 sym
= global_identifier_push(v
, type
->t
| VT_STATIC
, 0);
3860 sym
->type
.ref
= type
->ref
;
3861 sym
->r
= VT_CONST
| VT_SYM
;
3862 put_extern_sym(sym
, sec
, offset
, size
);
3866 /* push a reference to a section offset by adding a dummy symbol */
3867 static void vpush_ref(CType
*type
, Section
*sec
, unsigned long offset
, unsigned long size
)
3872 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
3873 vtop
->sym
= get_sym_ref(type
, sec
, offset
, size
);
3876 /* define a new external reference to a symbol 'v' of type 'u' */
3877 static Sym
*external_global_sym(int v
, CType
*type
, int r
)
3883 /* push forward reference */
3884 s
= global_identifier_push(v
, type
->t
| VT_EXTERN
, 0);
3885 s
->type
.ref
= type
->ref
;
3886 s
->r
= r
| VT_CONST
| VT_SYM
;
3891 /* define a new external reference to a symbol 'v' of type 'u' */
3892 static Sym
*external_sym(int v
, CType
*type
, int r
)
3898 /* push forward reference */
3899 s
= sym_push(v
, type
, r
| VT_CONST
| VT_SYM
, 0);
3900 s
->type
.t
|= VT_EXTERN
;
3905 /* push a reference to global symbol v */
3906 static void vpush_global_sym(CType
*type
, int v
)
3911 sym
= external_global_sym(v
, type
, 0);
3913 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
3917 void vset(CType
*type
, int r
, int v
)
3922 vsetc(type
, r
, &cval
);
3925 void vseti(int r
, int v
)
3941 void vpushv(SValue
*v
)
3943 if (vtop
>= vstack
+ VSTACK_SIZE
)
3944 error("memory full");
3954 /* save r to the memory stack, and mark it as being free */
3955 void save_reg(int r
)
3957 int l
, saved
, size
, align
;
3961 /* modify all stack values */
3964 for(p
=vstack
;p
<=vtop
;p
++) {
3965 if ((p
->r
& VT_VALMASK
) == r
||
3966 (p
->r2
& VT_VALMASK
) == r
) {
3967 /* must save value on stack if not already done */
3969 /* NOTE: must reload 'r' because r might be equal to r2 */
3970 r
= p
->r
& VT_VALMASK
;
3971 /* store register in the stack */
3973 if ((p
->r
& VT_LVAL
) ||
3974 (!is_float(type
->t
) && (type
->t
& VT_BTYPE
) != VT_LLONG
))
3976 size
= type_size(type
, &align
);
3977 loc
= (loc
- size
) & -align
;
3978 sv
.type
.t
= type
->t
;
3979 sv
.r
= VT_LOCAL
| VT_LVAL
;
3982 #ifdef TCC_TARGET_I386
3983 /* x86 specific: need to pop fp register ST0 if saved */
3985 o(0xd9dd); /* fstp %st(1) */
3988 /* special long long case */
3989 if ((type
->t
& VT_BTYPE
) == VT_LLONG
) {
3996 /* mark that stack entry as being saved on the stack */
3997 if (p
->r
& VT_LVAL
) {
3998 /* also suppress the bounded flag because the
3999 relocation address of the function was stored in
4001 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
4003 p
->r
= lvalue_type(p
->type
.t
) | VT_LOCAL
;
4011 /* find a free register of class 'rc'. If none, save one register */
4017 /* find a free register */
4018 for(r
=0;r
<NB_REGS
;r
++) {
4019 if (reg_classes
[r
] & rc
) {
4020 for(p
=vstack
;p
<=vtop
;p
++) {
4021 if ((p
->r
& VT_VALMASK
) == r
||
4022 (p
->r2
& VT_VALMASK
) == r
)
4030 /* no register left : free the first one on the stack (VERY
4031 IMPORTANT to start from the bottom to ensure that we don't
4032 spill registers used in gen_opi()) */
4033 for(p
=vstack
;p
<=vtop
;p
++) {
4034 r
= p
->r
& VT_VALMASK
;
4035 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
))
4037 /* also look at second register (if long long) */
4038 r
= p
->r2
& VT_VALMASK
;
4039 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
4045 /* Should never comes here */
4049 /* save registers up to (vtop - n) stack entry */
4050 void save_regs(int n
)
4055 for(p
= vstack
;p
<= p1
; p
++) {
4056 r
= p
->r
& VT_VALMASK
;
4063 /* move register 's' to 'r', and flush previous value of r to memory
4065 void move_reg(int r
, int s
)
4078 /* get address of vtop (vtop MUST BE an lvalue) */
4081 vtop
->r
&= ~VT_LVAL
;
4082 /* tricky: if saved lvalue, then we can go back to lvalue */
4083 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
4084 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
4087 #ifdef CONFIG_TCC_BCHECK
4088 /* generate lvalue bound code */
4094 vtop
->r
&= ~VT_MUSTBOUND
;
4095 /* if lvalue, then use checking code before dereferencing */
4096 if (vtop
->r
& VT_LVAL
) {
4097 /* if not VT_BOUNDED value, then make one */
4098 if (!(vtop
->r
& VT_BOUNDED
)) {
4099 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
4100 /* must save type because we must set it to int to get pointer */
4102 vtop
->type
.t
= VT_INT
;
4105 gen_bounded_ptr_add();
4106 vtop
->r
|= lval_type
;
4109 /* then check for dereferencing */
4110 gen_bounded_ptr_deref();
4115 /* store vtop a register belonging to class 'rc'. lvalues are
4116 converted to values. Cannot be used if cannot be converted to
4117 register value (such as structures). */
4120 int r
, r2
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
4121 unsigned long long ll
;
4123 /* NOTE: get_reg can modify vstack[] */
4124 if (vtop
->type
.t
& VT_BITFIELD
) {
4125 bit_pos
= (vtop
->type
.t
>> VT_STRUCT_SHIFT
) & 0x3f;
4126 bit_size
= (vtop
->type
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4127 /* remove bit field info to avoid loops */
4128 vtop
->type
.t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4129 /* generate shifts */
4130 vpushi(32 - (bit_pos
+ bit_size
));
4132 vpushi(32 - bit_size
);
4133 /* NOTE: transformed to SHR if unsigned */
4137 if (is_float(vtop
->type
.t
) &&
4138 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4141 unsigned long offset
;
4143 /* XXX: unify with initializers handling ? */
4144 /* CPUs usually cannot use float constants, so we store them
4145 generically in data segment */
4146 size
= type_size(&vtop
->type
, &align
);
4147 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
4148 data_section
->data_offset
= offset
;
4149 /* XXX: not portable yet */
4150 ptr
= section_ptr_add(data_section
, size
);
4153 ptr
[i
] = vtop
->c
.tab
[i
];
4154 sym
= get_sym_ref(&vtop
->type
, data_section
, offset
, size
<< 2);
4155 vtop
->r
|= VT_LVAL
| VT_SYM
;
4159 #ifdef CONFIG_TCC_BCHECK
4160 if (vtop
->r
& VT_MUSTBOUND
)
4164 r
= vtop
->r
& VT_VALMASK
;
4165 /* need to reload if:
4167 - lvalue (need to dereference pointer)
4168 - already a register, but not in the right class */
4169 if (r
>= VT_CONST
||
4170 (vtop
->r
& VT_LVAL
) ||
4171 !(reg_classes
[r
] & rc
) ||
4172 ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
&&
4173 !(reg_classes
[vtop
->r2
] & rc
))) {
4175 if ((vtop
->type
.t
& VT_BTYPE
) == VT_LLONG
) {
4176 /* two register type load : expand to two words
4178 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
4181 vtop
->c
.ui
= ll
; /* first word */
4183 vtop
->r
= r
; /* save register value */
4184 vpushi(ll
>> 32); /* second word */
4185 } else if (r
>= VT_CONST
||
4186 (vtop
->r
& VT_LVAL
)) {
4187 /* load from memory */
4190 vtop
[-1].r
= r
; /* save register value */
4191 /* increment pointer to get second word */
4192 vtop
->type
.t
= VT_INT
;
4198 /* move registers */
4201 vtop
[-1].r
= r
; /* save register value */
4202 vtop
->r
= vtop
[-1].r2
;
4204 /* allocate second register */
4211 /* write second register */
4213 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->type
.t
)) {
4215 /* lvalue of scalar type : need to use lvalue type
4216 because of possible cast */
4219 /* compute memory access type */
4220 if (vtop
->r
& VT_LVAL_BYTE
)
4222 else if (vtop
->r
& VT_LVAL_SHORT
)
4224 if (vtop
->r
& VT_LVAL_UNSIGNED
)
4228 /* restore wanted type */
4231 /* one register type load */
4240 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4241 void gv2(int rc1
, int rc2
)
4245 /* generate more generic register first. But VT_JMP or VT_CMP
4246 values must be generated first in all cases to avoid possible
4248 v
= vtop
[0].r
& VT_VALMASK
;
4249 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
4254 /* test if reload is needed for first register */
4255 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
4265 /* test if reload is needed for first register */
4266 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
4272 /* expand long long on stack in two int registers */
4277 u
= vtop
->type
.t
& VT_UNSIGNED
;
4280 vtop
[0].r
= vtop
[-1].r2
;
4281 vtop
[0].r2
= VT_CONST
;
4282 vtop
[-1].r2
= VT_CONST
;
4283 vtop
[0].type
.t
= VT_INT
| u
;
4284 vtop
[-1].type
.t
= VT_INT
| u
;
4287 /* build a long long from two ints */
4290 gv2(RC_INT
, RC_INT
);
4291 vtop
[-1].r2
= vtop
[0].r
;
4292 vtop
[-1].type
.t
= t
;
4296 /* rotate n first stack elements to the bottom */
4303 for(i
=-n
+1;i
!=0;i
++)
4304 vtop
[i
] = vtop
[i
+1];
4308 /* pop stack value */
4312 v
= vtop
->r
& VT_VALMASK
;
4313 #ifdef TCC_TARGET_I386
4314 /* for x86, we need to pop the FP stack */
4315 if (v
== REG_ST0
&& !nocode_wanted
) {
4316 o(0xd9dd); /* fstp %st(1) */
4319 if (v
== VT_JMP
|| v
== VT_JMPI
) {
4320 /* need to put correct jump if && or || without test */
4326 /* convert stack entry to register and duplicate its value in another
4334 if ((t
& VT_BTYPE
) == VT_LLONG
) {
4341 /* stack: H L L1 H1 */
4349 /* duplicate value */
4360 load(r1
, &sv
); /* move r to r1 */
4362 /* duplicates value */
4367 /* generate CPU independent (unsigned) long long operations */
4368 void gen_opl(int op
)
4370 int t
, a
, b
, op1
, c
, i
;
4378 func
= TOK___divdi3
;
4381 func
= TOK___udivdi3
;
4384 func
= TOK___moddi3
;
4387 func
= TOK___umoddi3
;
4389 /* call generic long long function */
4390 gfunc_start(&gf
, FUNC_CDECL
);
4393 vpush_global_sym(&func_old_type
, func
);
4397 vtop
->r2
= REG_LRET
;
4410 /* stack: L1 H1 L2 H2 */
4415 vtop
[-2] = vtop
[-3];
4418 /* stack: H1 H2 L1 L2 */
4424 /* stack: H1 H2 L1 L2 ML MH */
4427 /* stack: ML MH H1 H2 L1 L2 */
4431 /* stack: ML MH H1 L2 H2 L1 */
4436 /* stack: ML MH M1 M2 */
4439 } else if (op
== '+' || op
== '-') {
4440 /* XXX: add non carry method too (for MIPS or alpha) */
4446 /* stack: H1 H2 (L1 op L2) */
4449 gen_op(op1
+ 1); /* TOK_xxxC2 */
4452 /* stack: H1 H2 (L1 op L2) */
4455 /* stack: (L1 op L2) H1 H2 */
4457 /* stack: (L1 op L2) (H1 op H2) */
4465 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
4466 t
= vtop
[-1].type
.t
;
4470 /* stack: L H shift */
4472 /* constant: simpler */
4473 /* NOTE: all comments are for SHL. the other cases are
4474 done by swaping words */
4485 if (op
!= TOK_SAR
) {
4518 /* XXX: should provide a faster fallback on x86 ? */
4521 func
= TOK___sardi3
;
4524 func
= TOK___shrdi3
;
4527 func
= TOK___shldi3
;
4533 /* compare operations */
4539 /* stack: L1 H1 L2 H2 */
4541 vtop
[-1] = vtop
[-2];
4543 /* stack: L1 L2 H1 H2 */
4546 /* when values are equal, we need to compare low words. since
4547 the jump is inverted, we invert the test too. */
4550 else if (op1
== TOK_GT
)
4552 else if (op1
== TOK_ULT
)
4554 else if (op1
== TOK_UGT
)
4559 if (op1
!= TOK_NE
) {
4563 /* generate non equal test */
4564 /* XXX: NOT PORTABLE yet */
4568 #ifdef TCC_TARGET_I386
4569 b
= psym(0x850f, 0);
4571 error("not implemented");
4575 /* compare low. Always unsigned */
4579 else if (op1
== TOK_LE
)
4581 else if (op1
== TOK_GT
)
4583 else if (op1
== TOK_GE
)
4593 /* handle integer constant optimizations and various machine
4595 void gen_opic(int op
)
4602 /* currently, we cannot do computations with forward symbols */
4603 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4604 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4608 case '+': v1
->c
.i
+= fc
; break;
4609 case '-': v1
->c
.i
-= fc
; break;
4610 case '&': v1
->c
.i
&= fc
; break;
4611 case '^': v1
->c
.i
^= fc
; break;
4612 case '|': v1
->c
.i
|= fc
; break;
4613 case '*': v1
->c
.i
*= fc
; break;
4620 /* if division by zero, generate explicit division */
4623 error("division by zero in constant");
4627 default: v1
->c
.i
/= fc
; break;
4628 case '%': v1
->c
.i
%= fc
; break;
4629 case TOK_UDIV
: v1
->c
.i
= (unsigned)v1
->c
.i
/ fc
; break;
4630 case TOK_UMOD
: v1
->c
.i
= (unsigned)v1
->c
.i
% fc
; break;
4633 case TOK_SHL
: v1
->c
.i
<<= fc
; break;
4634 case TOK_SHR
: v1
->c
.i
= (unsigned)v1
->c
.i
>> fc
; break;
4635 case TOK_SAR
: v1
->c
.i
>>= fc
; break;
4637 case TOK_ULT
: v1
->c
.i
= (unsigned)v1
->c
.i
< (unsigned)fc
; break;
4638 case TOK_UGE
: v1
->c
.i
= (unsigned)v1
->c
.i
>= (unsigned)fc
; break;
4639 case TOK_EQ
: v1
->c
.i
= v1
->c
.i
== fc
; break;
4640 case TOK_NE
: v1
->c
.i
= v1
->c
.i
!= fc
; break;
4641 case TOK_ULE
: v1
->c
.i
= (unsigned)v1
->c
.i
<= (unsigned)fc
; break;
4642 case TOK_UGT
: v1
->c
.i
= (unsigned)v1
->c
.i
> (unsigned)fc
; break;
4643 case TOK_LT
: v1
->c
.i
= v1
->c
.i
< fc
; break;
4644 case TOK_GE
: v1
->c
.i
= v1
->c
.i
>= fc
; break;
4645 case TOK_LE
: v1
->c
.i
= v1
->c
.i
<= fc
; break;
4646 case TOK_GT
: v1
->c
.i
= v1
->c
.i
> fc
; break;
4648 case TOK_LAND
: v1
->c
.i
= v1
->c
.i
&& fc
; break;
4649 case TOK_LOR
: v1
->c
.i
= v1
->c
.i
|| fc
; break;
4655 /* if commutative ops, put c2 as constant */
4656 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
4657 op
== '|' || op
== '*')) {
4662 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
4665 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
4666 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
4672 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
4673 /* try to use shifts instead of muls or divs */
4674 if (fc
> 0 && (fc
& (fc
- 1)) == 0) {
4683 else if (op
== TOK_PDIV
)
4689 } else if (c2
&& (op
== '+' || op
== '-') &&
4690 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
4691 (VT_CONST
| VT_SYM
)) {
4692 /* symbol + constant case */
4699 if (!nocode_wanted
) {
4700 /* call low level op generator */
4709 /* generate a floating point operation with constant propagation */
4710 void gen_opif(int op
)
4718 /* currently, we cannot do computations with forward symbols */
4719 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4720 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4722 if (v1
->type
.t
== VT_FLOAT
) {
4725 } else if (v1
->type
.t
== VT_DOUBLE
) {
4733 /* NOTE: we only do constant propagation if finite number (not
4734 NaN or infinity) (ANSI spec) */
4735 if (!ieee_finite(f1
) || !ieee_finite(f2
))
4739 case '+': f1
+= f2
; break;
4740 case '-': f1
-= f2
; break;
4741 case '*': f1
*= f2
; break;
4745 error("division by zero in constant");
4750 /* XXX: also handles tests ? */
4754 /* XXX: overflow test ? */
4755 if (v1
->type
.t
== VT_FLOAT
) {
4757 } else if (v1
->type
.t
== VT_DOUBLE
) {
4765 if (!nocode_wanted
) {
4773 static int pointed_size(CType
*type
)
4776 return type_size(pointed_type(type
), &align
);
4780 void check_pointer_types(SValue
*p1
, SValue
*p2
)
4782 char buf1
[256], buf2
[256];
4786 if (!is_compatible_types(t1
, t2
)) {
4787 type_to_str(buf1
, sizeof(buf1
), t1
, NULL
);
4788 type_to_str(buf2
, sizeof(buf2
), t2
, NULL
);
4789 error("incompatible pointers '%s' and '%s'", buf1
, buf2
);
4794 /* generic gen_op: handles types problems */
4797 int u
, t1
, t2
, bt1
, bt2
, t
;
4800 t1
= vtop
[-1].type
.t
;
4801 t2
= vtop
[0].type
.t
;
4802 bt1
= t1
& VT_BTYPE
;
4803 bt2
= t2
& VT_BTYPE
;
4805 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
4806 /* at least one operand is a pointer */
4807 /* relationnal op: must be both pointers */
4808 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
4809 // check_pointer_types(vtop, vtop - 1);
4810 /* pointers are handled are unsigned */
4811 t
= VT_INT
| VT_UNSIGNED
;
4814 /* if both pointers, then it must be the '-' op */
4815 if (bt1
== VT_PTR
&& bt2
== VT_PTR
) {
4817 error("cannot use pointers here");
4818 // check_pointer_types(vtop - 1, vtop);
4819 /* XXX: check that types are compatible */
4820 u
= pointed_size(&vtop
[-1].type
);
4822 /* set to integer type */
4823 vtop
->type
.t
= VT_INT
;
4827 /* exactly one pointer : must be '+' or '-'. */
4828 if (op
!= '-' && op
!= '+')
4829 error("cannot use pointers here");
4830 /* Put pointer as first operand */
4831 if (bt2
== VT_PTR
) {
4835 type1
= vtop
[-1].type
;
4836 /* XXX: cast to int ? (long long case) */
4837 vpushi(pointed_size(&vtop
[-1].type
));
4839 #ifdef CONFIG_TCC_BCHECK
4840 /* if evaluating constant expression, no code should be
4841 generated, so no bound check */
4842 if (do_bounds_check
&& !const_wanted
) {
4843 /* if bounded pointers, we generate a special code to
4850 gen_bounded_ptr_add();
4856 /* put again type if gen_opic() swaped operands */
4859 } else if (is_float(bt1
) || is_float(bt2
)) {
4860 /* compute bigger type and do implicit casts */
4861 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
4863 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
4868 /* floats can only be used for a few operations */
4869 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
4870 (op
< TOK_ULT
|| op
> TOK_GT
))
4871 error("invalid operands for binary operation");
4873 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
4874 /* cast to biggest op */
4876 /* convert to unsigned if it does not fit in a long long */
4877 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
4878 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
4882 /* integer operations */
4884 /* convert to unsigned if it does not fit in an integer */
4885 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
4886 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
4889 /* XXX: currently, some unsigned operations are explicit, so
4890 we modify them here */
4891 if (t
& VT_UNSIGNED
) {
4898 else if (op
== TOK_LT
)
4900 else if (op
== TOK_GT
)
4902 else if (op
== TOK_LE
)
4904 else if (op
== TOK_GE
)
4911 /* special case for shifts and long long: we keep the shift as
4913 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
4918 else if ((t
& VT_BTYPE
) == VT_LLONG
)
4922 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
4923 /* relationnal op: the result is an int */
4924 vtop
->type
.t
= VT_INT
;
4931 /* generic itof for unsigned long long case */
4932 void gen_cvt_itof1(int t
)
4936 if ((vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
4937 (VT_LLONG
| VT_UNSIGNED
)) {
4939 gfunc_start(&gf
, FUNC_CDECL
);
4942 vpush_global_sym(&func_old_type
, TOK___ulltof
);
4943 else if (t
== VT_DOUBLE
)
4944 vpush_global_sym(&func_old_type
, TOK___ulltod
);
4946 vpush_global_sym(&func_old_type
, TOK___ulltold
);
4955 /* generic ftoi for unsigned long long case */
4956 void gen_cvt_ftoi1(int t
)
4961 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
4962 /* not handled natively */
4963 gfunc_start(&gf
, FUNC_CDECL
);
4964 st
= vtop
->type
.t
& VT_BTYPE
;
4967 vpush_global_sym(&func_old_type
, TOK___fixunssfdi
);
4968 else if (st
== VT_DOUBLE
)
4969 vpush_global_sym(&func_old_type
, TOK___fixunsdfdi
);
4971 vpush_global_sym(&func_old_type
, TOK___fixunsxfdi
);
4975 vtop
->r2
= REG_LRET
;
4981 /* force char or short cast */
4982 void force_charshort_cast(int t
)
4986 /* XXX: add optimization if lvalue : just change type and offset */
4991 if (t
& VT_UNSIGNED
) {
4992 vpushi((1 << bits
) - 1);
5003 /* cast 'vtop' to 'type' */
5004 static void gen_cast(CType
*type
)
5006 int sbt
, dbt
, sf
, df
, c
;
5008 /* special delayed cast for char/short */
5009 /* XXX: in some cases (multiple cascaded casts), it may still
5011 if (vtop
->r
& VT_MUSTCAST
) {
5012 vtop
->r
&= ~VT_MUSTCAST
;
5013 force_charshort_cast(vtop
->type
.t
);
5016 dbt
= type
->t
& (VT_BTYPE
| VT_UNSIGNED
);
5017 sbt
= vtop
->type
.t
& (VT_BTYPE
| VT_UNSIGNED
);
5019 if (sbt
!= dbt
&& !nocode_wanted
) {
5022 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
5024 /* convert from fp to fp */
5026 /* constant case: we can do it now */
5027 /* XXX: in ISOC, cannot do it if error in convert */
5028 if (dbt
== VT_FLOAT
&& sbt
== VT_DOUBLE
)
5029 vtop
->c
.f
= (float)vtop
->c
.d
;
5030 else if (dbt
== VT_FLOAT
&& sbt
== VT_LDOUBLE
)
5031 vtop
->c
.f
= (float)vtop
->c
.ld
;
5032 else if (dbt
== VT_DOUBLE
&& sbt
== VT_FLOAT
)
5033 vtop
->c
.d
= (double)vtop
->c
.f
;
5034 else if (dbt
== VT_DOUBLE
&& sbt
== VT_LDOUBLE
)
5035 vtop
->c
.d
= (double)vtop
->c
.ld
;
5036 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_FLOAT
)
5037 vtop
->c
.ld
= (long double)vtop
->c
.f
;
5038 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_DOUBLE
)
5039 vtop
->c
.ld
= (long double)vtop
->c
.d
;
5041 /* non constant case: generate code */
5045 /* convert int to fp */
5048 case VT_LLONG
| VT_UNSIGNED
:
5050 /* XXX: add const cases for long long */
5052 case VT_INT
| VT_UNSIGNED
:
5054 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.ui
; break;
5055 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.ui
; break;
5056 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.ui
; break;
5061 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.i
; break;
5062 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.i
; break;
5063 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.i
; break;
5072 /* convert fp to int */
5073 /* we handle char/short/etc... with generic code */
5074 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
5075 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
5080 case VT_LLONG
| VT_UNSIGNED
:
5082 /* XXX: add const cases for long long */
5084 case VT_INT
| VT_UNSIGNED
:
5086 case VT_FLOAT
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5087 case VT_DOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5088 case VT_LDOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
5094 case VT_FLOAT
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5095 case VT_DOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5096 case VT_LDOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
5104 if (dbt
== VT_INT
&& (type
->t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
5105 /* additionnal cast for char/short/bool... */
5109 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
5110 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
5111 /* scalar to long long */
5113 if (sbt
== (VT_INT
| VT_UNSIGNED
))
5114 vtop
->c
.ll
= vtop
->c
.ui
;
5116 vtop
->c
.ll
= vtop
->c
.i
;
5118 /* machine independant conversion */
5120 /* generate high word */
5121 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
5129 /* patch second register */
5130 vtop
[-1].r2
= vtop
->r
;
5134 } else if (dbt
== VT_BOOL
) {
5135 /* scalar to bool */
5138 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
5139 (dbt
& VT_BTYPE
) == VT_SHORT
) {
5140 force_charshort_cast(dbt
);
5141 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
5143 if (sbt
== VT_LLONG
) {
5144 /* from long long: just take low order word */
5148 /* if lvalue and single word type, nothing to do because
5149 the lvalue already contains the real type size (see
5150 VT_LVAL_xxx constants) */
5156 /* return type size. Put alignment at 'a' */
5157 int type_size(CType
*type
, int *a
)
5162 bt
= type
->t
& VT_BTYPE
;
5163 if (bt
== VT_STRUCT
) {
5166 *a
= 4; /* XXX: cannot store it yet. Doing that is safe */
5168 } else if (bt
== VT_PTR
) {
5169 if (type
->t
& VT_ARRAY
) {
5171 return type_size(&s
->type
, a
) * s
->c
;
5176 } else if (bt
== VT_LDOUBLE
) {
5178 return LDOUBLE_SIZE
;
5179 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
5182 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
5185 } else if (bt
== VT_SHORT
) {
5189 /* char, void, function, _Bool */
5195 /* return the pointed type of t */
5196 static inline CType
*pointed_type(CType
*type
)
5198 return &type
->ref
->type
;
5201 /* modify type so that its it is a pointer to type. */
5202 static void mk_pointer(CType
*type
)
5205 s
= sym_push(SYM_FIELD
, type
, 0, -1);
5206 type
->t
= VT_PTR
| (type
->t
& ~VT_TYPE
);
5210 static int is_compatible_types(CType
*type1
, CType
*type2
)
5213 int bt1
, bt2
, t1
, t2
;
5215 t1
= type1
->t
& VT_TYPE
;
5216 t2
= type2
->t
& VT_TYPE
;
5217 bt1
= t1
& VT_BTYPE
;
5218 bt2
= t2
& VT_BTYPE
;
5219 if (bt1
== VT_PTR
) {
5220 type1
= pointed_type(type1
);
5221 /* if function, then convert implicitely to function pointer */
5222 if (bt2
!= VT_FUNC
) {
5225 type2
= pointed_type(type2
);
5227 /* void matches everything */
5228 /* XXX: not fully compliant */
5229 if ((type1
->t
& VT_TYPE
) == VT_VOID
|| (type2
->t
& VT_TYPE
) == VT_VOID
)
5231 return is_compatible_types(type1
, type2
);
5232 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5234 } else if (bt1
== VT_FUNC
) {
5239 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5241 /* XXX: not complete */
5242 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
5246 while (s1
!= NULL
) {
5249 if (!is_compatible_types(&s1
->type
, &s2
->type
))
5258 /* XXX: not complete */
5263 /* print a type. If 'varstr' is not NULL, then the variable is also
5264 printed in the type */
5266 /* XXX: add array and function pointers */
5267 void type_to_str(char *buf
, int buf_size
,
5268 CType
*type
, const char *varstr
)
5275 t
= type
->t
& VT_TYPE
;
5278 if (t
& VT_UNSIGNED
)
5279 pstrcat(buf
, buf_size
, "unsigned ");
5309 tstr
= "long double";
5311 pstrcat(buf
, buf_size
, tstr
);
5315 if (bt
== VT_STRUCT
)
5319 pstrcat(buf
, buf_size
, tstr
);
5321 if (v
>= SYM_FIRST_ANOM
)
5322 pstrcat(buf
, buf_size
, "<anonymous>");
5324 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
5328 type_to_str(buf
, buf_size
, &s
->type
, varstr
);
5329 pstrcat(buf
, buf_size
, "(");
5331 while (sa
!= NULL
) {
5332 type_to_str(buf1
, sizeof(buf1
), &sa
->type
, NULL
);
5333 pstrcat(buf
, buf_size
, buf1
);
5336 pstrcat(buf
, buf_size
, ", ");
5338 pstrcat(buf
, buf_size
, ")");
5342 pstrcpy(buf1
, sizeof(buf1
), "*");
5344 pstrcat(buf1
, sizeof(buf1
), varstr
);
5345 type_to_str(buf
, buf_size
, &s
->type
, buf1
);
5349 pstrcat(buf
, buf_size
, " ");
5350 pstrcat(buf
, buf_size
, varstr
);
5355 /* verify type compatibility to store vtop in 'dt' type, and generate
5357 void gen_assign_cast(CType
*dt
)
5360 char buf1
[256], buf2
[256];
5362 st
= &vtop
->type
; /* source type */
5363 if ((dt
->t
& VT_BTYPE
) == VT_PTR
) {
5364 /* special cases for pointers */
5365 /* a function is implicitely a function pointer */
5366 if ((st
->t
& VT_BTYPE
) == VT_FUNC
) {
5367 if (!is_compatible_types(pointed_type(dt
), st
))
5372 /* '0' can also be a pointer */
5373 if ((st
->t
& VT_BTYPE
) == VT_INT
&&
5374 ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) &&
5378 if (!is_compatible_types(dt
, st
)) {
5380 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
5381 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
5382 error("cannot cast '%s' to '%s'", buf1
, buf2
);
5388 /* store vtop in lvalue pushed on stack */
5391 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
5394 ft
= vtop
[-1].type
.t
;
5395 sbt
= vtop
->type
.t
& VT_BTYPE
;
5396 dbt
= ft
& VT_BTYPE
;
5397 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
5398 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
5399 /* optimize char/short casts */
5400 delayed_cast
= VT_MUSTCAST
;
5401 vtop
->type
.t
= ft
& VT_TYPE
;
5404 gen_assign_cast(&vtop
[-1].type
);
5407 if (sbt
== VT_STRUCT
) {
5408 /* if structure, only generate pointer */
5409 /* structure assignment : generate memcpy */
5410 /* XXX: optimize if small size */
5411 if (!nocode_wanted
) {
5413 gfunc_start(&gf
, FUNC_CDECL
);
5415 size
= type_size(&vtop
->type
, &align
);
5419 vtop
->type
.t
= VT_INT
;
5424 vtop
->type
.t
= VT_INT
;
5429 vpush_global_sym(&func_old_type
, TOK_memcpy
);
5435 /* leave source on stack */
5436 } else if (ft
& VT_BITFIELD
) {
5437 /* bitfield store handling */
5438 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
5439 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
5440 /* remove bit field info to avoid loops */
5441 vtop
[-1].type
.t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
5443 /* duplicate destination */
5445 vtop
[-1] = vtop
[-2];
5447 /* mask and shift source */
5448 vpushi((1 << bit_size
) - 1);
5452 /* load destination, mask and or with source */
5454 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
5460 #ifdef CONFIG_TCC_BCHECK
5461 /* bound check case */
5462 if (vtop
[-1].r
& VT_MUSTBOUND
) {
5468 if (!nocode_wanted
) {
5472 r
= gv(rc
); /* generate value */
5473 /* if lvalue was saved on stack, must read it */
5474 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
5476 t
= get_reg(RC_INT
);
5478 sv
.r
= VT_LOCAL
| VT_LVAL
;
5479 sv
.c
.ul
= vtop
[-1].c
.ul
;
5481 vtop
[-1].r
= t
| VT_LVAL
;
5484 /* two word case handling : store second register at word + 4 */
5485 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
5487 /* convert to int to increment easily */
5488 vtop
->type
.t
= VT_INT
;
5494 /* XXX: it works because r2 is spilled last ! */
5495 store(vtop
->r2
, vtop
- 1);
5499 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5500 vtop
->r
|= delayed_cast
;
5504 /* post defines POST/PRE add. c is the token ++ or -- */
5505 void inc(int post
, int c
)
5508 vdup(); /* save lvalue */
5510 gv_dup(); /* duplicate value */
5515 vpushi(c
- TOK_MID
);
5517 vstore(); /* store value */
5519 vpop(); /* if post op, return saved value */
5522 /* Parse GNUC __attribute__ extension. Currently, the following
5523 extensions are recognized:
5524 - aligned(n) : set data/function alignment.
5525 - section(x) : generate data/code in this section.
5526 - unused : currently ignored, but may be used someday.
5528 void parse_attribute(AttributeDef
*ad
)
5535 while (tok
!= ')') {
5536 if (tok
< TOK_IDENT
)
5537 expect("attribute name");
5542 case TOK___SECTION__
:
5545 expect("section name");
5546 ad
->section
= find_section(tcc_state
, (char *)tokc
.cstr
->data
);
5551 case TOK___ALIGNED__
:
5554 if (n
<= 0 || (n
& (n
- 1)) != 0)
5555 error("alignment must be a positive power of two");
5560 case TOK___UNUSED__
:
5561 /* currently, no need to handle it because tcc does not
5562 track unused objects */
5565 case TOK___NORETURN__
:
5566 /* currently, no need to handle it because tcc does not
5567 track unused objects */
5572 ad
->func_call
= FUNC_CDECL
;
5576 case TOK___STDCALL__
:
5577 ad
->func_call
= FUNC_STDCALL
;
5580 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
5581 /* skip parameters */
5582 /* XXX: skip parenthesis too */
5585 while (tok
!= ')' && tok
!= -1)
5599 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
5600 static void struct_decl(CType
*type
, int u
)
5602 int a
, v
, size
, align
, maxalign
, c
, offset
;
5603 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
;
5608 a
= tok
; /* save decl type */
5613 /* struct already defined ? return it */
5614 /* XXX: check consistency */
5618 error("invalid type");
5625 s
= sym_push(v
| SYM_STRUCT
, &type1
, 0, 0);
5626 /* put struct/union/enum name in type */
5634 error("struct/union/enum already defined");
5635 /* cannot be empty */
5642 if (a
== TOK_ENUM
) {
5649 /* enum symbols have static storage */
5650 ss
= sym_push(v
, &int_type
, VT_CONST
, c
);
5651 ss
->type
.t
|= VT_STATIC
;
5656 parse_btype(&btype
, &ad
);
5662 type_decl(&type1
, &ad
, &v
, TYPE_DIRECT
);
5663 if ((type1
.t
& VT_BTYPE
) == VT_FUNC
||
5664 (type1
.t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
)))
5665 error("invalid type for '%s'",
5666 get_tok_str(v
, NULL
));
5670 bit_size
= expr_const();
5671 /* XXX: handle v = 0 case for messages */
5673 error("negative width in bit-field '%s'",
5674 get_tok_str(v
, NULL
));
5675 if (v
&& bit_size
== 0)
5676 error("zero width for bit-field '%s'",
5677 get_tok_str(v
, NULL
));
5679 size
= type_size(&type1
, &align
);
5681 if (bit_size
>= 0) {
5682 bt
= type1
.t
& VT_BTYPE
;
5686 error("bitfields must have scalar type");
5688 if (bit_size
> bsize
) {
5689 error("width of '%s' exceeds its type",
5690 get_tok_str(v
, NULL
));
5691 } else if (bit_size
== bsize
) {
5692 /* no need for bit fields */
5694 } else if (bit_size
== 0) {
5695 /* XXX: what to do if only padding in a
5697 /* zero size: means to pad */
5701 /* we do not have enough room ? */
5702 if ((bit_pos
+ bit_size
) > bsize
)
5705 /* XXX: handle LSB first */
5706 type1
.t
|= VT_BITFIELD
|
5707 (bit_pos
<< VT_STRUCT_SHIFT
) |
5708 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
5709 bit_pos
+= bit_size
;
5715 /* add new memory data only if starting
5717 if (lbit_pos
== 0) {
5718 if (a
== TOK_STRUCT
) {
5719 c
= (c
+ align
- 1) & -align
;
5727 if (align
> maxalign
)
5731 printf("add field %s offset=%d",
5732 get_tok_str(v
, NULL
), offset
);
5733 if (type1
.t
& VT_BITFIELD
) {
5734 printf(" pos=%d size=%d",
5735 (type1
.t
>> VT_STRUCT_SHIFT
) & 0x3f,
5736 (type1
.t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
5740 ss
= sym_push(v
| SYM_FIELD
, &type1
, 0, offset
);
5744 if (tok
== ';' || tok
== -1)
5754 /* size for struct/union, dummy for enum */
5755 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
5759 /* return 0 if no type declaration. otherwise, return the basic type
5762 static int parse_btype(CType
*type
, AttributeDef
*ad
)
5764 int t
, u
, type_found
;
5768 memset(ad
, 0, sizeof(AttributeDef
));
5779 if ((t
& VT_BTYPE
) != 0)
5780 error("too many basic types");
5794 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
5795 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
5796 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
5797 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
5811 if ((t
& VT_BTYPE
) == VT_LONG
) {
5812 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
5819 struct_decl(&type1
, VT_ENUM
);
5822 type
->ref
= type1
.ref
;
5826 struct_decl(&type1
, VT_STRUCT
);
5829 /* type modifiers */
5834 case TOK___SIGNED__
:
5837 case TOK___INLINE__
:
5859 /* GNUC attribute */
5860 case TOK___ATTRIBUTE__
:
5861 parse_attribute(ad
);
5866 parse_expr_type(&type1
);
5870 if (!s
|| !(s
->type
.t
& VT_TYPEDEF
))
5872 t
|= (s
->type
.t
& ~VT_TYPEDEF
);
5873 type
->ref
= s
->type
.ref
;
5880 /* long is never used as type */
5881 if ((t
& VT_BTYPE
) == VT_LONG
)
5882 t
= (t
& ~VT_BTYPE
) | VT_INT
;
5887 static void post_type(CType
*type
, AttributeDef
*ad
)
5890 Sym
**plast
, *s
, *first
;
5895 /* function declaration */
5900 while (tok
!= ')') {
5901 /* read param name and compute offset */
5902 if (l
!= FUNC_OLD
) {
5903 if (!parse_btype(&pt
, &ad1
)) {
5905 error("invalid type");
5912 if ((pt
.t
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
5914 type_decl(&pt
, &ad1
, &n
, TYPE_DIRECT
| TYPE_ABSTRACT
);
5915 if ((pt
.t
& VT_BTYPE
) == VT_VOID
)
5916 error("parameter declared as void");
5923 /* array must be transformed to pointer according to ANSI C */
5925 s
= sym_push(n
| SYM_FIELD
, &pt
, 0, 0);
5930 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
5937 /* if no parameters, then old type prototype */
5941 t1
= type
->t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
);
5942 type
->t
&= ~(VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
);
5943 post_type(type
, ad
);
5944 /* we push a anonymous symbol which will contain the function prototype */
5945 s
= sym_push(SYM_FIELD
, type
, ad
->func_call
, l
);
5947 type
->t
= t1
| VT_FUNC
;
5949 } else if (tok
== '[') {
5950 /* array definition */
5956 error("invalid array size");
5959 /* parse next post type */
5960 t1
= type
->t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
);
5961 type
->t
&= ~(VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
);
5962 post_type(type
, ad
);
5964 /* we push a anonymous symbol which will contain the array
5966 s
= sym_push(SYM_FIELD
, type
, 0, n
);
5967 type
->t
= t1
| VT_ARRAY
| VT_PTR
;
5972 /* Parse a type declaration (except basic type), and return the type
5973 in 'type'. 'td' is a bitmask indicating which kind of type decl is
5974 expected. 'type' should contain the basic type. 'ad' is the
5975 attribute definition of the basic type. It can be modified by
5978 static void type_decl(CType
*type
, AttributeDef
*ad
, int *v
, int td
)
5981 CType type1
, *type2
;
5983 while (tok
== '*') {
5985 while (tok
== TOK_CONST
|| tok
== TOK_VOLATILE
|| tok
== TOK_RESTRICT
)
5990 /* recursive type */
5991 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
5992 type1
.t
= 0; /* XXX: same as int */
5995 /* XXX: this is not correct to modify 'ad' at this point, but
5996 the syntax is not clear */
5997 if (tok
== TOK___ATTRIBUTE__
)
5998 parse_attribute(ad
);
5999 type_decl(&type1
, ad
, v
, td
);
6002 /* type identifier */
6003 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
6007 if (!(td
& TYPE_ABSTRACT
))
6008 expect("identifier");
6012 post_type(type
, ad
);
6013 if (tok
== TOK___ATTRIBUTE__
)
6014 parse_attribute(ad
);
6017 /* append type at the end of type1 */
6030 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6031 static int lvalue_type(int t
)
6038 else if (bt
== VT_SHORT
)
6042 if (t
& VT_UNSIGNED
)
6043 r
|= VT_LVAL_UNSIGNED
;
6047 /* indirection with full error checking and bound check */
6048 static void indir(void)
6050 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
6052 if ((vtop
->r
& VT_LVAL
) && !nocode_wanted
)
6054 vtop
->type
= *pointed_type(&vtop
->type
);
6055 /* an array is never an lvalue */
6056 if (!(vtop
->type
.t
& VT_ARRAY
)) {
6057 vtop
->r
|= lvalue_type(vtop
->type
.t
);
6058 /* if bound checking, the referenced pointer must be checked */
6059 if (do_bounds_check
)
6060 vtop
->r
|= VT_MUSTBOUND
;
6064 /* pass a parameter to a function and do type checking and casting */
6065 void gfunc_param_typed(GFuncContext
*gf
, Sym
*func
, Sym
*arg
)
6070 func_type
= func
->c
;
6071 if (func_type
== FUNC_OLD
||
6072 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
6073 /* default casting : only need to convert float to double */
6074 if ((vtop
->type
.t
& VT_BTYPE
) == VT_FLOAT
) {
6078 } else if (arg
== NULL
) {
6079 error("too many arguments to function");
6081 gen_assign_cast(&arg
->type
);
6083 if (!nocode_wanted
) {
6090 /* parse an expression of the form '(type)' or '(expr)' and return its
6092 static void parse_expr_type(CType
*type
)
6098 if (parse_btype(type
, &ad
)) {
6099 type_decl(type
, &ad
, &n
, TYPE_ABSTRACT
);
6106 static void vpush_tokc(int t
)
6110 vsetc(&type
, VT_CONST
, &tokc
);
6113 static void unary(void)
6115 int n
, t
, align
, size
, r
;
6121 /* XXX: GCC 2.95.3 does not generate a table although it should be
6131 vpush_tokc(VT_INT
| VT_UNSIGNED
);
6135 vpush_tokc(VT_LLONG
);
6139 vpush_tokc(VT_LLONG
| VT_UNSIGNED
);
6143 vpush_tokc(VT_FLOAT
);
6147 vpush_tokc(VT_DOUBLE
);
6151 vpush_tokc(VT_LDOUBLE
);
6154 case TOK___FUNCTION__
:
6156 goto tok_identifier
;
6162 /* special function name identifier */
6163 len
= strlen(funcname
) + 1;
6164 /* generate char[len] type */
6169 vpush_ref(&type
, data_section
, data_section
->data_offset
, len
);
6170 ptr
= section_ptr_add(data_section
, len
);
6171 memcpy(ptr
, funcname
, len
);
6179 /* string parsing */
6185 memset(&ad
, 0, sizeof(AttributeDef
));
6186 decl_initializer_alloc(&type
, &ad
, VT_CONST
, 2, 0, 0);
6191 if (parse_btype(&type
, &ad
)) {
6192 type_decl(&type
, &ad
, &n
, TYPE_ABSTRACT
);
6194 /* check ISOC99 compound literal */
6196 /* data is allocated locally by default */
6201 /* all except arrays are lvalues */
6202 if (!(type
.t
& VT_ARRAY
))
6203 r
|= lvalue_type(type
.t
);
6204 memset(&ad
, 0, sizeof(AttributeDef
));
6205 decl_initializer_alloc(&type
, &ad
, r
, 1, 0, 0);
6223 /* functions names must be treated as function pointers,
6224 except for unary '&' and sizeof. Since we consider that
6225 functions are not lvalues, we only have to handle it
6226 there and in function calls. */
6227 /* arrays can also be used although they are not lvalues */
6228 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
&&
6229 !(vtop
->type
.t
& VT_ARRAY
))
6231 mk_pointer(&vtop
->type
);
6237 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
)
6238 vtop
->c
.i
= !vtop
->c
.i
;
6239 else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
6240 vtop
->c
.i
= vtop
->c
.i
^ 1;
6242 vseti(VT_JMP
, gtst(1, 0));
6252 /* in order to force cast, we add zero */
6254 if ((vtop
->type
.t
& VT_BTYPE
) == VT_PTR
)
6255 error("pointer not accepted for unary plus");
6264 parse_expr_type(&type
);
6268 size
= type_size(&type
, &align
);
6269 if (t
== TOK_SIZEOF
)
6290 goto tok_identifier
;
6292 /* allow to take the address of a label */
6293 if (tok
< TOK_UIDENT
)
6294 expect("label identifier");
6295 s
= label_find(tok
);
6297 s
= label_push(tok
, LABEL_FORWARD
);
6300 s
->type
.t
= VT_VOID
;
6301 mk_pointer(&s
->type
);
6302 s
->type
.t
|= VT_STATIC
;
6304 vset(&s
->type
, VT_CONST
| VT_SYM
, 0);
6313 expect("identifier");
6317 error("'%s' undeclared", get_tok_str(t
, NULL
));
6318 /* for simple function calls, we tolerate undeclared
6319 external reference to int() function */
6320 s
= external_global_sym(t
, &func_old_type
, 0);
6322 vset(&s
->type
, s
->r
, s
->c
);
6323 /* if forward reference, we must point to s */
6324 if (vtop
->r
& VT_SYM
) {
6331 /* post operations */
6333 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
6336 } else if (tok
== '.' || tok
== TOK_ARROW
) {
6338 if (tok
== TOK_ARROW
)
6343 /* expect pointer on structure */
6344 if ((vtop
->type
.t
& VT_BTYPE
) != VT_STRUCT
)
6345 expect("struct or union");
6349 while ((s
= s
->next
) != NULL
) {
6354 error("field not found");
6355 /* add field offset to pointer */
6356 vtop
->type
= char_pointer_type
; /* change type to 'char *' */
6359 /* change type to field type, and set to lvalue */
6360 vtop
->type
= s
->type
;
6361 /* an array is never an lvalue */
6362 if (!(vtop
->type
.t
& VT_ARRAY
)) {
6363 vtop
->r
|= lvalue_type(vtop
->type
.t
);
6364 /* if bound checking, the referenced pointer must be checked */
6365 if (do_bounds_check
)
6366 vtop
->r
|= VT_MUSTBOUND
;
6369 } else if (tok
== '[') {
6375 } else if (tok
== '(') {
6380 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
) {
6381 /* pointer test (no array accepted) */
6382 if ((vtop
->type
.t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
6383 vtop
->type
= *pointed_type(&vtop
->type
);
6384 if ((vtop
->type
.t
& VT_BTYPE
) != VT_FUNC
)
6388 expect("function pointer");
6391 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
6393 /* get return type */
6395 if (!nocode_wanted
) {
6396 save_regs(0); /* save used temporary registers */
6397 gfunc_start(&gf
, s
->r
);
6400 sa
= s
->next
; /* first parameter */
6401 #ifdef INVERT_FUNC_PARAMS
6405 ParseState saved_parse_state
;
6408 /* read each argument and store it on a stack */
6414 while ((parlevel
> 0 || (tok
!= ')' && tok
!= ',')) &&
6418 else if (tok
== ')')
6420 tok_str_add_tok(&str
);
6423 tok_str_add(&str
, -1); /* end of file added */
6424 tok_str_add(&str
, 0);
6425 s1
= sym_push2(&args
, 0, 0, (int)str
.str
);
6426 s1
->next
= sa
; /* add reference to argument */
6435 /* now generate code in reverse order by reading the stack */
6436 save_parse_state(&saved_parse_state
);
6438 macro_ptr
= (int *)args
->c
;
6442 expect("',' or ')'");
6443 gfunc_param_typed(&gf
, s
, args
->next
);
6445 tok_str_free((int *)args
->c
);
6449 restore_parse_state(&saved_parse_state
);
6452 /* compute first implicit argument if a structure is returned */
6453 if ((s
->type
.t
& VT_BTYPE
) == VT_STRUCT
) {
6454 /* get some space for the returned structure */
6455 size
= type_size(&s
->type
, &align
);
6456 loc
= (loc
- size
) & -align
;
6458 ret
.r
= VT_LOCAL
| VT_LVAL
;
6459 /* pass it as 'int' to avoid structure arg passing
6461 vseti(VT_LOCAL
, loc
);
6470 /* return in register */
6471 if (is_float(ret
.type
.t
)) {
6474 if ((ret
.type
.t
& VT_BTYPE
) == VT_LLONG
)
6480 #ifndef INVERT_FUNC_PARAMS
6484 gfunc_param_typed(&gf
, s
, sa
);
6494 error("too few arguments to function");
6501 vsetc(&ret
.type
, ret
.r
, &ret
.c
);
6509 static void uneq(void)
6515 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
6516 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
6517 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
6532 static void expr_prod(void)
6537 while (tok
== '*' || tok
== '/' || tok
== '%') {
6545 static void expr_sum(void)
6550 while (tok
== '+' || tok
== '-') {
6558 static void expr_shift(void)
6563 while (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
6571 static void expr_cmp(void)
6576 while ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
6577 tok
== TOK_ULT
|| tok
== TOK_UGE
) {
6585 static void expr_cmpeq(void)
6590 while (tok
== TOK_EQ
|| tok
== TOK_NE
) {
6598 static void expr_and(void)
6601 while (tok
== '&') {
6608 static void expr_xor(void)
6611 while (tok
== '^') {
6618 static void expr_or(void)
6621 while (tok
== '|') {
6628 /* XXX: suppress this mess */
6629 static void expr_land_const(void)
6632 while (tok
== TOK_LAND
) {
6639 /* XXX: suppress this mess */
6640 static void expr_lor_const(void)
6643 while (tok
== TOK_LOR
) {
6650 /* only used if non constant */
6651 static void expr_land(void)
6658 if (tok
!= TOK_LAND
) {
6671 static void expr_lor(void)
6678 if (tok
!= TOK_LOR
) {
6691 /* XXX: better constant handling */
6692 static void expr_eq(void)
6694 int tt
, u
, r1
, r2
, rc
, t1
, t2
, bt1
, bt2
;
6696 CType type
, type1
, type2
;
6717 save_regs(1); /* we need to save all registers here except
6718 at the top because it is a branch point */
6722 sv
= *vtop
; /* save value to handle it later */
6723 vtop
--; /* no vpop so that FP stack is not flushed */
6732 bt1
= t1
& VT_BTYPE
;
6734 bt2
= t2
& VT_BTYPE
;
6735 /* cast operands to correct type according to ISOC rules */
6736 if (is_float(bt1
) || is_float(bt2
)) {
6737 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
6738 type
.t
= VT_LDOUBLE
;
6739 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
6744 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
6745 /* cast to biggest op */
6747 /* convert to unsigned if it does not fit in a long long */
6748 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
6749 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
6750 type
.t
|= VT_UNSIGNED
;
6751 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
6752 /* XXX: test pointer compatibility */
6754 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
6755 /* XXX: test structure compatibility */
6757 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
6758 /* NOTE: as an extension, we accept void on only one side */
6761 /* integer operations */
6763 /* convert to unsigned if it does not fit in an integer */
6764 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
6765 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
6766 type
.t
|= VT_UNSIGNED
;
6769 /* now we convert second operand */
6772 if (is_float(type
.t
)) {
6774 } else if ((type
.t
& VT_BTYPE
) == VT_LLONG
) {
6775 /* for long longs, we use fixed registers to avoid having
6776 to handle a complicated move */
6780 /* this is horrible, but we must also convert first
6784 /* put again first value and cast it */
6795 static void gexpr(void)
6806 /* parse an expression and return its type without any side effect. */
6807 static void expr_type(CType
*type
)
6819 /* parse a unary expression and return its type without any side
6821 static void unary_type(CType
*type
)
6833 /* parse a constant expression and return value in vtop. */
6834 static void expr_const1(void)
6843 /* parse an integer constant and return its value. */
6844 static int expr_const(void)
6848 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
6849 expect("constant expression");
6855 /* return the label token if current token is a label, otherwise
6857 static int is_label(void)
6862 /* fast test first */
6863 if (tok
< TOK_UIDENT
)
6865 /* no need to save tokc since we expect an identifier */
6873 /* XXX: may not work in all cases (macros ?) */
6882 static void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
)
6887 /* generate line number info */
6889 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
6890 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
6892 last_line_num
= file
->line_num
;
6895 if (tok
== TOK_IF
) {
6902 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
6904 if (c
== TOK_ELSE
) {
6908 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
6909 gsym(d
); /* patch else jmp */
6912 } else if (tok
== TOK_WHILE
) {
6920 block(&a
, &b
, case_sym
, def_sym
, case_reg
);
6924 } else if (tok
== '{') {
6928 while (tok
!= '}') {
6931 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
6933 /* pop locally defined symbols */
6934 sym_pop(&local_stack
, s
);
6936 } else if (tok
== TOK_RETURN
) {
6940 gen_assign_cast(&func_vt
);
6941 if ((func_vt
.t
& VT_BTYPE
) == VT_STRUCT
) {
6943 /* if returning structure, must copy it to implicit
6944 first pointer arg location */
6947 vset(&type
, VT_LOCAL
| VT_LVAL
, func_vc
);
6950 /* copy structure value to pointer */
6952 } else if (is_float(func_vt
.t
)) {
6957 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
6960 rsym
= gjmp(rsym
); /* jmp */
6961 } else if (tok
== TOK_BREAK
) {
6964 error("cannot break");
6965 *bsym
= gjmp(*bsym
);
6968 } else if (tok
== TOK_CONTINUE
) {
6971 error("cannot continue");
6972 *csym
= gjmp(*csym
);
6975 } else if (tok
== TOK_FOR
) {
7002 block(&a
, &b
, case_sym
, def_sym
, case_reg
);
7007 if (tok
== TOK_DO
) {
7012 block(&a
, &b
, case_sym
, def_sym
, case_reg
);
7023 if (tok
== TOK_SWITCH
) {
7027 /* XXX: other types than integer */
7028 case_reg
= gv(RC_INT
);
7032 b
= gjmp(0); /* jump to first case */
7034 block(&a
, csym
, &b
, &c
, case_reg
);
7035 /* if no default, jmp after switch */
7043 if (tok
== TOK_CASE
) {
7050 if (gnu_ext
&& tok
== TOK_DOTS
) {
7054 warning("empty case range");
7056 /* since a case is like a label, we must skip it with a jmp */
7063 *case_sym
= gtst(1, 0);
7066 *case_sym
= gtst(1, 0);
7070 *case_sym
= gtst(1, *case_sym
);
7074 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
7076 if (tok
== TOK_DEFAULT
) {
7082 error("too many 'default'");
7084 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
7086 if (tok
== TOK_GOTO
) {
7088 if (tok
== '*' && gnu_ext
) {
7092 if ((vtop
->type
.t
& VT_BTYPE
) != VT_PTR
)
7095 } else if (tok
>= TOK_UIDENT
) {
7096 s
= label_find(tok
);
7097 /* put forward definition if needed */
7099 s
= label_push(tok
, LABEL_FORWARD
);
7101 /* label already defined */
7102 if (s
->r
& LABEL_FORWARD
)
7103 s
->next
= (void *)gjmp((long)s
->next
);
7105 gjmp_addr((long)s
->next
);
7108 expect("label identifier");
7117 if (!(s
->r
& LABEL_FORWARD
))
7118 error("multiple defined label");
7119 gsym((long)s
->next
);
7121 s
= label_push(b
, 0);
7123 s
->next
= (void *)ind
;
7125 /* we accept this, but it is a mistake */
7127 warning("deprecated use of label at end of compound statement");
7129 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
7131 /* expression case */
7141 /* t is the array or struct type. c is the array or struct
7142 address. cur_index/cur_field is the pointer to the current
7143 value. 'size_only' is true if only size info is needed (only used
7145 static void decl_designator(CType
*type
, Section
*sec
, unsigned long c
,
7146 int *cur_index
, Sym
**cur_field
,
7150 int notfirst
, index
, align
, l
;
7154 if (gnu_ext
&& (l
= is_label()) != 0)
7157 while (tok
== '[' || tok
== '.') {
7159 if (!(type
->t
& VT_ARRAY
))
7160 expect("array type");
7163 index
= expr_const();
7164 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
7165 expect("invalid index");
7169 type
= pointed_type(type
);
7170 c
+= index
* type_size(type
, &align
);
7176 if ((type
->t
& VT_BTYPE
) != VT_STRUCT
)
7177 expect("struct/union type");
7190 /* XXX: suppress this mess by using explicit storage field */
7192 type1
.t
|= (type
->t
& ~VT_TYPE
);
7206 if (type
->t
& VT_ARRAY
) {
7208 type
= pointed_type(type
);
7209 c
+= index
* type_size(type
, &align
);
7213 error("too many field init");
7214 /* XXX: suppress this mess by using explicit storage field */
7216 type1
.t
|= (type
->t
& ~VT_TYPE
);
7221 decl_initializer(type
, sec
, c
, 0, size_only
);
7225 #define EXPR_CONST 1
7228 /* store a value or an expression directly in global data or in local array */
7229 static void init_putv(CType
*type
, Section
*sec
, unsigned long c
,
7230 int v
, int expr_type
)
7232 int saved_global_expr
, bt
;
7240 /* compound literals must be allocated globally in this case */
7241 saved_global_expr
= global_expr
;
7244 global_expr
= saved_global_expr
;
7245 /* NOTE: symbols are accepted */
7246 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
7247 error("initializer element is not constant");
7255 /* XXX: not portable */
7256 /* XXX: generate error if incorrect relocation */
7257 gen_assign_cast(type
);
7258 bt
= type
->t
& VT_BTYPE
;
7259 ptr
= sec
->data
+ c
;
7260 if ((vtop
->r
& VT_SYM
) &&
7266 error("initializer element is not computable at load time");
7269 *(char *)ptr
= vtop
->c
.i
;
7272 *(short *)ptr
= vtop
->c
.i
;
7275 *(double *)ptr
= vtop
->c
.d
;
7278 *(long double *)ptr
= vtop
->c
.ld
;
7281 *(long long *)ptr
= vtop
->c
.ll
;
7284 if (vtop
->r
& VT_SYM
) {
7285 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
7287 *(int *)ptr
= vtop
->c
.i
;
7292 vset(type
, VT_LOCAL
, c
);
7299 /* put zeros for variable based init */
7300 static void init_putz(CType
*t
, Section
*sec
, unsigned long c
, int size
)
7305 /* nothing to do because globals are already set to zero */
7307 gfunc_start(&gf
, FUNC_CDECL
);
7314 vpush_global_sym(&func_old_type
, TOK_memset
);
7319 /* 't' contains the type and storage info. 'c' is the offset of the
7320 object in section 'sec'. If 'sec' is NULL, it means stack based
7321 allocation. 'first' is true if array '{' must be read (multi
7322 dimension implicit array init handling). 'size_only' is true if
7323 size only evaluation is wanted (only for arrays). */
7324 static void decl_initializer(CType
*type
, Section
*sec
, unsigned long c
,
7325 int first
, int size_only
)
7327 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
7328 int size1
, align1
, expr_type
;
7332 if (type
->t
& VT_ARRAY
) {
7336 t1
= pointed_type(type
);
7337 size1
= type_size(t1
, &align1
);
7340 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
7346 /* only parse strings here if correct type (otherwise: handle
7347 them as ((w)char *) expressions */
7348 if ((tok
== TOK_LSTR
&&
7349 (t1
->t
& VT_BTYPE
) == VT_INT
) ||
7351 (t1
->t
& VT_BTYPE
) == VT_BYTE
)) {
7352 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7357 /* compute maximum number of chars wanted */
7359 cstr_len
= cstr
->size
;
7361 cstr_len
= cstr
->size
/ sizeof(int);
7364 if (n
>= 0 && nb
> (n
- array_length
))
7365 nb
= n
- array_length
;
7368 warning("initializer-string for array is too long");
7369 /* in order to go faster for common case (char
7370 string in global variable, we handle it
7372 if (sec
&& tok
== TOK_STR
&& size1
== 1) {
7373 memcpy(sec
->data
+ c
+ array_length
, cstr
->data
, nb
);
7377 ch
= ((unsigned char *)cstr
->data
)[i
];
7379 ch
= ((int *)cstr
->data
)[i
];
7380 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
7388 /* only add trailing zero if enough storage (no
7389 warning in this case since it is standard) */
7390 if (n
< 0 || array_length
< n
) {
7392 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
7398 while (tok
!= '}') {
7399 decl_designator(type
, sec
, c
, &index
, NULL
, size_only
);
7400 if (n
>= 0 && index
>= n
)
7401 error("index too large");
7402 /* must put zero in holes (note that doing it that way
7403 ensures that it even works with designators) */
7404 if (!size_only
&& array_length
< index
) {
7405 init_putz(t1
, sec
, c
+ array_length
* size1
,
7406 (index
- array_length
) * size1
);
7409 if (index
> array_length
)
7410 array_length
= index
;
7411 /* special test for multi dimensional arrays (may not
7412 be strictly correct if designators are used at the
7414 if (index
>= n
&& no_oblock
)
7423 /* put zeros at the end */
7424 if (!size_only
&& n
>= 0 && array_length
< n
) {
7425 init_putz(t1
, sec
, c
+ array_length
* size1
,
7426 (n
- array_length
) * size1
);
7428 /* patch type size if needed */
7430 s
->c
= array_length
;
7431 } else if ((type
->t
& VT_BTYPE
) == VT_STRUCT
&& tok
== '{') {
7432 /* XXX: union needs only one init */
7439 while (tok
!= '}') {
7440 decl_designator(type
, sec
, c
, NULL
, &f
, size_only
);
7441 /* fill with zero between fields */
7443 if (!size_only
&& array_length
< index
) {
7444 init_putz(type
, sec
, c
+ array_length
,
7445 index
- array_length
);
7447 index
= index
+ type_size(&f
->type
, &align1
);
7448 if (index
> array_length
)
7449 array_length
= index
;
7455 /* put zeros at the end */
7456 if (!size_only
&& array_length
< n
) {
7457 init_putz(type
, sec
, c
+ array_length
,
7461 } else if (tok
== '{') {
7463 decl_initializer(type
, sec
, c
, first
, size_only
);
7465 } else if (size_only
) {
7466 /* just skip expression */
7468 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
7472 else if (tok
== ')')
7477 /* currently, we always use constant expression for globals
7478 (may change for scripting case) */
7479 expr_type
= EXPR_CONST
;
7481 expr_type
= EXPR_ANY
;
7482 init_putv(type
, sec
, c
, 0, expr_type
);
7486 /* parse an initializer for type 't' if 'has_init' is non zero, and
7487 allocate space in local or global data space ('r' is either
7488 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7489 variable 'v' of scope 'scope' is declared before initializers are
7490 parsed. If 'v' is zero, then a reference to the new object is put
7491 in the value stack. If 'has_init' is 2, a special parsing is done
7492 to handle string constants. */
7493 static void decl_initializer_alloc(CType
*type
, AttributeDef
*ad
, int r
,
7494 int has_init
, int v
, int scope
)
7496 int size
, align
, addr
, data_offset
;
7498 ParseState saved_parse_state
;
7499 TokenString init_str
;
7502 size
= type_size(type
, &align
);
7503 /* If unknown size, we must evaluate it before
7504 evaluating initializers because
7505 initializers can generate global data too
7506 (e.g. string pointers or ISOC99 compound
7507 literals). It also simplifies local
7508 initializers handling */
7509 tok_str_new(&init_str
);
7512 error("unknown type size");
7513 /* get all init string */
7514 if (has_init
== 2) {
7515 /* only get strings */
7516 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
7517 tok_str_add_tok(&init_str
);
7522 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
7524 error("unexpected end of file in initializer");
7525 tok_str_add_tok(&init_str
);
7528 else if (tok
== '}') {
7536 tok_str_add(&init_str
, -1);
7537 tok_str_add(&init_str
, 0);
7540 save_parse_state(&saved_parse_state
);
7542 macro_ptr
= init_str
.str
;
7544 decl_initializer(type
, NULL
, 0, 1, 1);
7545 /* prepare second initializer parsing */
7546 macro_ptr
= init_str
.str
;
7549 /* if still unknown size, error */
7550 size
= type_size(type
, &align
);
7552 error("unknown type size");
7554 /* take into account specified alignment if bigger */
7555 if (ad
->aligned
> align
)
7556 align
= ad
->aligned
;
7557 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
7559 if (do_bounds_check
&& (type
->t
& VT_ARRAY
))
7561 #ifdef TCC_TARGET_IL
7562 /* XXX: ugly patch to allocate local variables for IL, just
7567 loc
= (loc
- size
) & -align
;
7570 /* handles bounds */
7571 /* XXX: currently, since we do only one pass, we cannot track
7572 '&' operators, so we add only arrays */
7573 if (do_bounds_check
&& (type
->t
& VT_ARRAY
)) {
7574 unsigned long *bounds_ptr
;
7575 /* add padding between regions */
7577 /* then add local bound info */
7578 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
7579 bounds_ptr
[0] = addr
;
7580 bounds_ptr
[1] = size
;
7583 /* compute section */
7591 data_offset
= sec
->data_offset
;
7592 data_offset
= (data_offset
+ align
- 1) & -align
;
7594 /* very important to increment global pointer at this time
7595 because initializers themselves can create new initializers */
7596 data_offset
+= size
;
7597 /* add padding if bound check */
7598 if (do_bounds_check
)
7600 sec
->data_offset
= data_offset
;
7601 /* allocate section space to put the data */
7602 if (sec
->sh_type
!= SHT_NOBITS
&&
7603 data_offset
> sec
->data_allocated
)
7604 section_realloc(sec
, data_offset
);
7608 /* local variable */
7609 sym_push(v
, type
, r
, addr
);
7611 /* push local reference */
7612 vset(type
, r
, addr
);
7618 if (scope
== VT_CONST
) {
7619 /* global scope: see if already defined */
7623 if (!is_compatible_types(&sym
->type
, type
))
7624 error("incompatible types for redefinition of '%s'",
7625 get_tok_str(v
, NULL
));
7626 if (!(sym
->type
.t
& VT_EXTERN
))
7627 error("redefinition of '%s'", get_tok_str(v
, NULL
));
7628 sym
->type
.t
&= ~VT_EXTERN
;
7631 sym
= sym_push(v
, type
, r
| VT_SYM
, 0);
7633 put_extern_sym(sym
, sec
, addr
, size
);
7637 /* push global reference */
7638 sym
= get_sym_ref(type
, sec
, addr
, size
);
7640 vsetc(type
, VT_CONST
| VT_SYM
, &cval
);
7644 /* handles bounds now because the symbol must be defined
7645 before for the relocation */
7646 if (do_bounds_check
) {
7647 unsigned long *bounds_ptr
;
7649 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
7650 /* then add global bound info */
7651 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
7652 bounds_ptr
[0] = 0; /* relocated */
7653 bounds_ptr
[1] = size
;
7657 decl_initializer(type
, sec
, addr
, 1, 0);
7658 /* restore parse state if needed */
7660 tok_str_free(init_str
.str
);
7661 restore_parse_state(&saved_parse_state
);
7666 void put_func_debug(Sym
*sym
)
7671 /* XXX: we put here a dummy type */
7672 snprintf(buf
, sizeof(buf
), "%s:%c1",
7673 funcname
, sym
->type
.t
& VT_STATIC
? 'f' : 'F');
7674 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
7675 cur_text_section
, sym
->c
);
7680 /* not finished : try to put some local vars in registers */
7681 //#define CONFIG_REG_VARS
7683 #ifdef CONFIG_REG_VARS
7684 void add_var_ref(int t
)
7686 printf("%s:%d: &%s\n",
7687 file
->filename
, file
->line_num
,
7688 get_tok_str(t
, NULL
));
7691 /* first pass on a function with heuristic to extract variable usage
7692 and pointer references to local variables for register allocation */
7693 void analyse_function(void)
7700 /* any symbol coming after '&' is considered as being a
7701 variable whose reference is taken. It is highly unaccurate
7702 but it is difficult to do better without a complete parse */
7705 /* if '& number', then no need to examine next tokens */
7706 if (tok
== TOK_CINT
||
7708 tok
== TOK_CLLONG
||
7709 tok
== TOK_CULLONG
) {
7711 } else if (tok
>= TOK_UIDENT
) {
7712 /* if '& ident [' or '& ident ->', then ident address
7716 if (tok
!= '[' && tok
!= TOK_ARROW
)
7720 while (tok
!= '}' && tok
!= ';' &&
7721 !((tok
== ',' || tok
== ')') && level
== 0)) {
7722 if (tok
>= TOK_UIDENT
) {
7724 } else if (tok
== '(') {
7726 } else if (tok
== ')') {
7739 /* parse an old style function declaration list */
7740 /* XXX: check multiple parameter */
7741 static void func_decl_list(Sym
*func_sym
)
7748 /* parse each declaration */
7749 while (tok
!= '{' && tok
!= ';' && tok
!= TOK_EOF
) {
7750 if (!parse_btype(&btype
, &ad
))
7751 expect("declaration list");
7752 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
7753 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
7755 /* we accept no variable after */
7759 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7760 /* find parameter in function parameter list */
7763 if ((s
->v
& ~SYM_FIELD
) == v
)
7767 error("declaration for parameter '%s' but no such parameter",
7768 get_tok_str(v
, NULL
));
7770 /* check that no storage specifier except 'register' was given */
7771 if (type
.t
& (VT_EXTERN
| VT_STATIC
| VT_TYPEDEF
))
7772 error("storage class specified for '%s'", get_tok_str(v
, NULL
));
7773 /* we can add the type (NOTE: it could be local to the function) */
7775 /* accept other parameters */
7786 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
7787 static void decl(int l
)
7795 if (!parse_btype(&btype
, &ad
)) {
7796 /* skip redundant ';' */
7797 /* XXX: find more elegant solution */
7802 /* special test for old K&R protos without explicit int
7803 type. Only accepted when defining global data */
7804 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
7808 if (((btype
.t
& VT_BTYPE
) == VT_ENUM
||
7809 (btype
.t
& VT_BTYPE
) == VT_STRUCT
) &&
7811 /* we accept no variable after */
7815 while (1) { /* iterate thru each declaration */
7817 type_decl(&type
, &ad
, &v
, TYPE_DIRECT
);
7821 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
7822 printf("type = '%s'\n", buf
);
7825 if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7826 /* if old style function prototype, we accept a
7829 if (sym
->c
== FUNC_OLD
)
7830 func_decl_list(sym
);
7834 #ifdef CONFIG_REG_VARS
7835 TokenString func_str
;
7836 ParseState saved_parse_state
;
7841 error("cannot use local functions");
7842 if (!(type
.t
& VT_FUNC
))
7843 expect("function definition");
7845 #ifdef CONFIG_REG_VARS
7846 /* parse all function code and record it */
7848 tok_str_new(&func_str
);
7854 error("unexpected end of file");
7855 tok_str_add_tok(&func_str
);
7860 } else if (t
== '}') {
7862 if (block_level
== 0)
7866 tok_str_add(&func_str
, -1);
7867 tok_str_add(&func_str
, 0);
7869 save_parse_state(&saved_parse_state
);
7871 macro_ptr
= func_str
.str
;
7876 /* compute text section */
7877 cur_text_section
= ad
.section
;
7878 if (!cur_text_section
)
7879 cur_text_section
= text_section
;
7880 ind
= cur_text_section
->data_offset
;
7881 funcname
= get_tok_str(v
, NULL
);
7884 /* if symbol is already defined, then put complete type */
7887 /* put function symbol */
7888 sym
= global_identifier_push(v
, type
.t
, 0);
7889 sym
->type
.ref
= type
.ref
;
7891 /* NOTE: we patch the symbol size later */
7892 put_extern_sym(sym
, cur_text_section
, ind
, 0);
7894 sym
->r
= VT_SYM
| VT_CONST
;
7895 /* put debug symbol */
7897 put_func_debug(sym
);
7898 /* push a dummy symbol to enable local sym storage */
7899 sym_push2(&local_stack
, SYM_FIELD
, 0, 0);
7900 gfunc_prolog(&type
);
7903 #ifdef CONFIG_REG_VARS
7904 macro_ptr
= func_str
.str
;
7907 block(NULL
, NULL
, NULL
, NULL
, 0);
7910 cur_text_section
->data_offset
= ind
;
7911 /* look if any labels are undefined. Define symbols if
7912 '&&label' was used. */
7915 for(s
= label_stack
; s
!= NULL
; s
= s1
) {
7917 if (s
->r
& LABEL_FORWARD
) {
7918 error("label '%s' used but not defined",
7919 get_tok_str(s
->v
, NULL
));
7922 /* define corresponding symbol. A size of
7924 put_extern_sym(s
, cur_text_section
, (long)s
->next
, 1);
7927 table_ident
[s
->v
- TOK_IDENT
]->sym_label
= NULL
;
7932 sym_pop(&local_stack
, NULL
); /* reset local stack */
7933 /* end of function */
7934 /* patch symbol size */
7935 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_size
=
7938 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
7940 funcname
= ""; /* for safety */
7941 func_vt
.t
= VT_VOID
; /* for safety */
7942 ind
= 0; /* for safety */
7944 #ifdef CONFIG_REG_VARS
7945 tok_str_free(func_str
.str
);
7946 restore_parse_state(&saved_parse_state
);
7950 if (btype
.t
& VT_TYPEDEF
) {
7951 /* save typedefed type */
7952 /* XXX: test storage specifiers ? */
7953 sym
= sym_push(v
, &type
, 0, 0);
7954 sym
->type
.t
|= VT_TYPEDEF
;
7955 } else if ((type
.t
& VT_BTYPE
) == VT_FUNC
) {
7956 /* external function definition */
7957 external_sym(v
, &type
, 0);
7959 /* not lvalue if array */
7961 if (!(type
.t
& VT_ARRAY
))
7962 r
|= lvalue_type(type
.t
);
7963 if (btype
.t
& VT_EXTERN
) {
7964 /* external variable */
7965 external_sym(v
, &type
, r
);
7967 if (type
.t
& VT_STATIC
)
7971 has_init
= (tok
== '=');
7974 decl_initializer_alloc(&type
, &ad
, r
,
7988 /* compile the C file opened in 'file'. Return non zero if errors. */
7989 static int tcc_compile(TCCState
*s1
)
7993 volatile int section_sym
;
7996 printf("%s: **** new file\n", file
->filename
);
7999 s1
->include_stack_ptr
= s1
->include_stack
;
8000 /* XXX: move that before to avoid having to initialize
8001 file->ifdef_stack_ptr ? */
8002 s1
->ifdef_stack_ptr
= s1
->ifdef_stack
;
8003 file
->ifdef_stack_ptr
= s1
->ifdef_stack_ptr
;
8005 /* XXX: not ANSI compliant: bound checking says error */
8007 anon_sym
= SYM_FIRST_ANOM
;
8009 /* file info: full path + filename */
8010 section_sym
= 0; /* avoid warning */
8012 section_sym
= put_elf_sym(symtab_section
, 0, 0,
8013 ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
), 0,
8014 text_section
->sh_num
, NULL
);
8015 getcwd(buf
, sizeof(buf
));
8016 pstrcat(buf
, sizeof(buf
), "/");
8017 put_stabs_r(buf
, N_SO
, 0, 0,
8018 text_section
->data_offset
, text_section
, section_sym
);
8019 put_stabs_r(file
->filename
, N_SO
, 0, 0,
8020 text_section
->data_offset
, text_section
, section_sym
);
8022 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8023 symbols can be safely used */
8024 put_elf_sym(symtab_section
, 0, 0,
8025 ELF32_ST_INFO(STB_LOCAL
, STT_FILE
), 0,
8026 SHN_ABS
, file
->filename
);
8028 /* define some often used types */
8029 int_type
.t
= VT_INT
;
8031 char_pointer_type
.t
= VT_BYTE
;
8032 mk_pointer(&char_pointer_type
);
8034 func_old_type
.t
= VT_FUNC
;
8035 func_old_type
.ref
= sym_push(SYM_FIELD
, &int_type
, FUNC_CDECL
, FUNC_OLD
);
8038 /* define 'void *alloca(unsigned int)' builtin function */
8043 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
8044 s1
= sym_push(SYM_FIELD
, VT_UNSIGNED
| VT_INT
, 0, 0);
8047 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
8051 define_start
= define_stack
;
8053 if (setjmp(s1
->error_jmp_buf
) == 0) {
8055 s1
->error_set_jmp_enabled
= 1;
8057 ch
= file
->buf_ptr
[0];
8058 tok_flags
= TOK_FLAG_BOL
| TOK_FLAG_BOF
;
8062 expect("declaration");
8064 /* end of translation unit info */
8066 put_stabs_r(NULL
, N_SO
, 0, 0,
8067 text_section
->data_offset
, text_section
, section_sym
);
8070 s1
->error_set_jmp_enabled
= 0;
8072 /* reset define stack, but leave -Dsymbols (may be incorrect if
8073 they are undefined) */
8074 free_defines(define_start
);
8076 sym_pop(&global_stack
, NULL
);
8078 return s1
->nb_errors
!= 0 ? -1 : 0;
8082 int tcc_compile_string(TCCState
*s
, const char *str
)
8084 BufferedFile bf1
, *bf
= &bf1
;
8088 /* init file structure */
8090 /* XXX: avoid copying */
8092 buf
= tcc_malloc(len
+ 1);
8097 bf
->buf_end
= buf
+ len
;
8098 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
8102 ret
= tcc_compile(s
);
8106 /* currently, no need to close */
8111 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8112 void tcc_define_symbol(TCCState
*s1
, const char *sym
, const char *value
)
8114 BufferedFile bf1
, *bf
= &bf1
;
8116 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
8117 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
8121 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
8123 /* init file structure */
8125 bf
->buf_ptr
= bf
->buffer
;
8126 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
8127 *bf
->buf_end
= CH_EOB
;
8128 bf
->filename
[0] = '\0';
8132 s1
->include_stack_ptr
= s1
->include_stack
;
8134 /* parse with define parser */
8135 ch
= file
->buf_ptr
[0];
8141 /* undefine a preprocessor symbol */
8142 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
8146 ts
= tok_alloc(sym
, strlen(sym
));
8147 s
= define_find(ts
->tok
);
8148 /* undefine symbol by putting an invalid name */
8155 /* print the position in the source file of PC value 'pc' by reading
8156 the stabs debug information */
8157 static void rt_printline(unsigned long wanted_pc
)
8159 Stab_Sym
*sym
, *sym_end
;
8160 char func_name
[128], last_func_name
[128];
8161 unsigned long func_addr
, last_pc
, pc
;
8162 const char *incl_files
[INCLUDE_STACK_SIZE
];
8163 int incl_index
, len
, last_line_num
, i
;
8164 const char *str
, *p
;
8166 fprintf(stderr
, "0x%08lx:", wanted_pc
);
8168 func_name
[0] = '\0';
8171 last_func_name
[0] = '\0';
8172 last_pc
= 0xffffffff;
8174 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
8175 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
8176 while (sym
< sym_end
) {
8177 switch(sym
->n_type
) {
8178 /* function start or end */
8180 if (sym
->n_strx
== 0) {
8181 /* we test if between last line and end of function */
8182 pc
= sym
->n_value
+ func_addr
;
8183 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
8185 func_name
[0] = '\0';
8188 str
= stabstr_section
->data
+ sym
->n_strx
;
8189 p
= strchr(str
, ':');
8191 pstrcpy(func_name
, sizeof(func_name
), str
);
8194 if (len
> sizeof(func_name
) - 1)
8195 len
= sizeof(func_name
) - 1;
8196 memcpy(func_name
, str
, len
);
8197 func_name
[len
] = '\0';
8199 func_addr
= sym
->n_value
;
8202 /* line number info */
8204 pc
= sym
->n_value
+ func_addr
;
8205 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
8208 last_line_num
= sym
->n_desc
;
8210 strcpy(last_func_name
, func_name
);
8214 str
= stabstr_section
->data
+ sym
->n_strx
;
8216 if (incl_index
< INCLUDE_STACK_SIZE
) {
8217 incl_files
[incl_index
++] = str
;
8225 if (sym
->n_strx
== 0) {
8226 incl_index
= 0; /* end of translation unit */
8228 str
= stabstr_section
->data
+ sym
->n_strx
;
8229 /* do not add path */
8231 if (len
> 0 && str
[len
- 1] != '/')
8239 /* second pass: we try symtab symbols (no line number info) */
8242 Elf32_Sym
*sym
, *sym_end
;
8245 sym_end
= (Elf32_Sym
*)(symtab_section
->data
+ symtab_section
->data_offset
);
8246 for(sym
= (Elf32_Sym
*)symtab_section
->data
+ 1;
8249 type
= ELF32_ST_TYPE(sym
->st_info
);
8250 if (type
== STT_FUNC
) {
8251 if (wanted_pc
>= sym
->st_value
&&
8252 wanted_pc
< sym
->st_value
+ sym
->st_size
) {
8253 pstrcpy(last_func_name
, sizeof(last_func_name
),
8254 strtab_section
->data
+ sym
->st_name
);
8260 /* did not find any info: */
8261 fprintf(stderr
, " ???\n");
8264 if (last_func_name
[0] != '\0') {
8265 fprintf(stderr
, " %s()", last_func_name
);
8267 if (incl_index
> 0) {
8268 fprintf(stderr
, " (%s:%d",
8269 incl_files
[incl_index
- 1], last_line_num
);
8270 for(i
= incl_index
- 2; i
>= 0; i
--)
8271 fprintf(stderr
, ", included from %s", incl_files
[i
]);
8272 fprintf(stderr
, ")");
8274 fprintf(stderr
, "\n");
8286 /* return the PC at frame level 'level'. Return non zero if not found */
8287 static int rt_get_caller_pc(unsigned long *paddr
,
8288 struct ucontext
*uc
, int level
)
8294 *paddr
= uc
->uc_mcontext
.gregs
[EIP
];
8297 fp
= uc
->uc_mcontext
.gregs
[EBP
];
8298 for(i
=1;i
<level
;i
++) {
8299 /* XXX: check address validity with program info */
8300 if (fp
<= 0x1000 || fp
>= 0xc0000000)
8302 fp
= ((unsigned long *)fp
)[0];
8304 *paddr
= ((unsigned long *)fp
)[1];
8309 #error add arch specific rt_get_caller_pc()
8312 /* emit a run time error at position 'pc' */
8313 void rt_error(struct ucontext
*uc
, const char *fmt
, ...)
8320 fprintf(stderr
, "Runtime error: ");
8321 vfprintf(stderr
, fmt
, ap
);
8322 fprintf(stderr
, "\n");
8323 for(i
=0;i
<num_callers
;i
++) {
8324 if (rt_get_caller_pc(&pc
, uc
, i
) < 0)
8327 fprintf(stderr
, "at ");
8329 fprintf(stderr
, "by ");
8336 /* signal handler for fatal errors */
8337 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
8339 struct ucontext
*uc
= puc
;
8343 switch(siginf
->si_code
) {
8346 rt_error(uc
, "division by zero");
8349 rt_error(uc
, "floating point exception");
8355 if (rt_bound_error_msg
&& *rt_bound_error_msg
)
8356 rt_error(uc
, *rt_bound_error_msg
);
8358 rt_error(uc
, "dereferencing invalid pointer");
8361 rt_error(uc
, "illegal instruction");
8364 rt_error(uc
, "abort() called");
8367 rt_error(uc
, "caught signal %d", signum
);
8374 /* do all relocations (needed before using tcc_get_symbol()) */
8375 int tcc_relocate(TCCState
*s1
)
8382 tcc_add_runtime(s1
);
8384 relocate_common_syms();
8386 /* compute relocation address : section are relocated in place. We
8387 also alloc the bss space */
8388 for(i
= 1; i
< s1
->nb_sections
; i
++) {
8389 s
= s1
->sections
[i
];
8390 if (s
->sh_flags
& SHF_ALLOC
) {
8391 if (s
->sh_type
== SHT_NOBITS
)
8392 s
->data
= tcc_mallocz(s
->data_offset
);
8393 s
->sh_addr
= (unsigned long)s
->data
;
8397 relocate_syms(s1
, 1);
8399 if (s1
->nb_errors
!= 0)
8402 /* relocate each section */
8403 for(i
= 1; i
< s1
->nb_sections
; i
++) {
8404 s
= s1
->sections
[i
];
8406 relocate_section(s1
, s
);
8411 /* launch the compiled program with the given arguments */
8412 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
8414 int (*prog_main
)(int, char **);
8416 if (tcc_relocate(s1
) < 0)
8419 prog_main
= tcc_get_symbol(s1
, "main");
8423 error("debug mode currently not available for Windows");
8425 struct sigaction sigact
;
8426 /* install TCC signal handlers to print debug info on fatal
8428 sigact
.sa_flags
= SA_SIGINFO
| SA_ONESHOT
;
8429 sigact
.sa_sigaction
= sig_error
;
8430 sigemptyset(&sigact
.sa_mask
);
8431 sigaction(SIGFPE
, &sigact
, NULL
);
8432 sigaction(SIGILL
, &sigact
, NULL
);
8433 sigaction(SIGSEGV
, &sigact
, NULL
);
8434 sigaction(SIGBUS
, &sigact
, NULL
);
8435 sigaction(SIGABRT
, &sigact
, NULL
);
8439 #ifdef CONFIG_TCC_BCHECK
8440 if (do_bounds_check
) {
8441 void (*bound_init
)(void);
8443 /* set error function */
8444 rt_bound_error_msg
= (void *)tcc_get_symbol(s1
, "__bound_error_msg");
8446 /* XXX: use .init section so that it also work in binary ? */
8447 bound_init
= (void *)tcc_get_symbol(s1
, "__bound_init");
8451 return (*prog_main
)(argc
, argv
);
8454 TCCState
*tcc_new(void)
8459 s
= tcc_mallocz(sizeof(TCCState
));
8463 s
->output_type
= TCC_OUTPUT_MEMORY
;
8465 /* default include paths */
8466 tcc_add_sysinclude_path(s
, "/usr/local/include");
8467 tcc_add_sysinclude_path(s
, "/usr/include");
8468 tcc_add_sysinclude_path(s
, CONFIG_TCC_PREFIX
"/lib/tcc/include");
8470 /* add all tokens */
8472 memset(hash_ident
, 0, TOK_HASH_SIZE
* sizeof(TokenSym
*));
8474 tok_ident
= TOK_IDENT
;
8479 tok_alloc(p
, r
- p
- 1);
8483 /* we add dummy defines for some special macros to speed up tests
8484 and to have working defined() */
8485 define_push(TOK___LINE__
, MACRO_OBJ
, NULL
, NULL
);
8486 define_push(TOK___FILE__
, MACRO_OBJ
, NULL
, NULL
);
8487 define_push(TOK___DATE__
, MACRO_OBJ
, NULL
, NULL
);
8488 define_push(TOK___TIME__
, MACRO_OBJ
, NULL
, NULL
);
8490 /* standard defines */
8491 tcc_define_symbol(s
, "__STDC__", NULL
);
8492 #if defined(TCC_TARGET_I386)
8493 tcc_define_symbol(s
, "__i386__", NULL
);
8496 tcc_define_symbol(s
, "linux", NULL
);
8498 /* tiny C specific defines */
8499 tcc_define_symbol(s
, "__TINYC__", NULL
);
8501 /* default library paths */
8502 tcc_add_library_path(s
, "/usr/local/lib");
8503 tcc_add_library_path(s
, "/usr/lib");
8504 tcc_add_library_path(s
, "/lib");
8506 /* no section zero */
8507 dynarray_add((void ***)&s
->sections
, &s
->nb_sections
, NULL
);
8509 /* create standard sections */
8510 text_section
= new_section(s
, ".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
8511 data_section
= new_section(s
, ".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
8512 bss_section
= new_section(s
, ".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
8514 /* symbols are always generated for linking stage */
8515 symtab_section
= new_symtab(s
, ".symtab", SHT_SYMTAB
, 0,
8517 ".hashtab", SHF_PRIVATE
);
8518 strtab_section
= symtab_section
->link
;
8520 /* private symbol table for dynamic symbols */
8521 s
->dynsymtab_section
= new_symtab(s
, ".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
8523 ".dynhashtab", SHF_PRIVATE
);
8527 void tcc_delete(TCCState
*s1
)
8531 /* free -D defines */
8535 n
= tok_ident
- TOK_IDENT
;
8536 for(i
= 0; i
< n
; i
++)
8537 tcc_free(table_ident
[i
]);
8538 tcc_free(table_ident
);
8540 /* free all sections */
8542 free_section(symtab_section
->hash
);
8544 free_section(s1
->dynsymtab_section
->hash
);
8545 free_section(s1
->dynsymtab_section
->link
);
8546 free_section(s1
->dynsymtab_section
);
8548 for(i
= 1; i
< s1
->nb_sections
; i
++)
8549 free_section(s1
->sections
[i
]);
8550 tcc_free(s1
->sections
);
8552 /* free loaded dlls array */
8553 for(i
= 0; i
< s1
->nb_loaded_dlls
; i
++)
8554 tcc_free(s1
->loaded_dlls
[i
]);
8555 tcc_free(s1
->loaded_dlls
);
8558 for(i
= 0; i
< s1
->nb_library_paths
; i
++)
8559 tcc_free(s1
->library_paths
[i
]);
8560 tcc_free(s1
->library_paths
);
8562 /* cached includes */
8563 for(i
= 0; i
< s1
->nb_cached_includes
; i
++)
8564 tcc_free(s1
->cached_includes
[i
]);
8565 tcc_free(s1
->cached_includes
);
8567 for(i
= 0; i
< s1
->nb_include_paths
; i
++)
8568 tcc_free(s1
->include_paths
[i
]);
8569 tcc_free(s1
->include_paths
);
8571 for(i
= 0; i
< s1
->nb_sysinclude_paths
; i
++)
8572 tcc_free(s1
->sysinclude_paths
[i
]);
8573 tcc_free(s1
->sysinclude_paths
);
8578 int tcc_add_include_path(TCCState
*s1
, const char *pathname
)
8582 pathname1
= tcc_strdup(pathname
);
8583 dynarray_add((void ***)&s1
->include_paths
, &s1
->nb_include_paths
, pathname1
);
8587 int tcc_add_sysinclude_path(TCCState
*s1
, const char *pathname
)
8591 pathname1
= tcc_strdup(pathname
);
8592 dynarray_add((void ***)&s1
->sysinclude_paths
, &s1
->nb_sysinclude_paths
, pathname1
);
8596 static int tcc_add_file_internal(TCCState
*s1
, const char *filename
, int flags
)
8601 BufferedFile
*saved_file
;
8603 /* find source file type with extension */
8604 ext
= strrchr(filename
, '.');
8610 file
= tcc_open(s1
, filename
);
8612 if (flags
& AFF_PRINT_ERROR
) {
8613 error_noabort("file '%s' not found", filename
);
8619 if (!ext
|| !strcmp(ext
, "c")) {
8620 /* C file assumed */
8621 ret
= tcc_compile(s1
);
8624 /* assume executable format: auto guess file type */
8625 if (read(fd
, &ehdr
, sizeof(ehdr
)) != sizeof(ehdr
)) {
8626 error_noabort("could not read header");
8629 lseek(fd
, 0, SEEK_SET
);
8631 if (ehdr
.e_ident
[0] == ELFMAG0
&&
8632 ehdr
.e_ident
[1] == ELFMAG1
&&
8633 ehdr
.e_ident
[2] == ELFMAG2
&&
8634 ehdr
.e_ident
[3] == ELFMAG3
) {
8635 file
->line_num
= 0; /* do not display line number if error */
8636 if (ehdr
.e_type
== ET_REL
) {
8637 ret
= tcc_load_object_file(s1
, fd
, 0);
8638 } else if (ehdr
.e_type
== ET_DYN
) {
8639 ret
= tcc_load_dll(s1
, fd
, filename
,
8640 (flags
& AFF_REFERENCED_DLL
) != 0);
8642 error_noabort("unrecognized ELF file");
8645 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
8646 file
->line_num
= 0; /* do not display line number if error */
8647 ret
= tcc_load_archive(s1
, fd
);
8649 /* as GNU ld, consider it is an ld script if not recognized */
8650 ret
= tcc_load_ldscript(s1
);
8652 error_noabort("unrecognized file type");
8667 int tcc_add_file(TCCState
*s
, const char *filename
)
8669 return tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
8672 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
8676 pathname1
= tcc_strdup(pathname
);
8677 dynarray_add((void ***)&s
->library_paths
, &s
->nb_library_paths
, pathname1
);
8681 /* find and load a dll. Return non zero if not found */
8682 /* XXX: add '-rpath' option support ? */
8683 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
8688 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
8689 snprintf(buf
, sizeof(buf
), "%s/%s",
8690 s
->library_paths
[i
], filename
);
8691 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
8697 /* the library name is the same as the argument of the '-l' option */
8698 int tcc_add_library(TCCState
*s
, const char *libraryname
)
8704 /* first we look for the dynamic library if not static linking */
8705 if (!s
->static_link
) {
8706 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
8707 /* if we output to memory, then we simply we dlopen(). */
8708 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
8709 /* Since the libc is already loaded, we don't need to load it again */
8710 if (!strcmp(libraryname
, "c"))
8712 h
= dlopen(buf
, RTLD_GLOBAL
| RTLD_LAZY
);
8716 if (tcc_add_dll(s
, buf
, 0) == 0)
8721 /* then we look for the static library */
8722 for(i
= 0; i
< s
->nb_library_paths
; i
++) {
8723 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
8724 s
->library_paths
[i
], libraryname
);
8725 if (tcc_add_file_internal(s
, buf
, 0) == 0)
8731 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
8733 add_elf_sym(symtab_section
, val
, 0,
8734 ELF32_ST_INFO(STB_GLOBAL
, STT_NOTYPE
),
8739 int tcc_set_output_type(TCCState
*s
, int output_type
)
8741 s
->output_type
= output_type
;
8743 /* if bound checking, then add corresponding sections */
8744 #ifdef CONFIG_TCC_BCHECK
8745 if (do_bounds_check
) {
8747 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
8748 /* create bounds sections */
8749 bounds_section
= new_section(s
, ".bounds",
8750 SHT_PROGBITS
, SHF_ALLOC
);
8751 lbounds_section
= new_section(s
, ".lbounds",
8752 SHT_PROGBITS
, SHF_ALLOC
);
8756 /* add debug sections */
8759 stab_section
= new_section(s
, ".stab", SHT_PROGBITS
, 0);
8760 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
8761 stabstr_section
= new_section(s
, ".stabstr", SHT_STRTAB
, 0);
8762 put_elf_str(stabstr_section
, "");
8763 stab_section
->link
= stabstr_section
;
8764 /* put first entry */
8765 put_stabs("", 0, 0, 0, 0);
8768 /* add libc crt1/crti objects */
8769 if (output_type
== TCC_OUTPUT_EXE
||
8770 output_type
== TCC_OUTPUT_DLL
) {
8771 if (output_type
!= TCC_OUTPUT_DLL
)
8772 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
8773 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
8778 #if !defined(LIBTCC)
8780 static int64_t getclock_us(void)
8785 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
8788 gettimeofday(&tv
, NULL
);
8789 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
8795 printf("tcc version 0.9.13 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
8796 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
8797 " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
8798 " [--] infile1 [infile2... --] [infile_args...]\n"
8800 "General options:\n"
8801 " -c compile only - generate an object file\n"
8802 " -o outfile set output filename\n"
8803 " -- allows multiples input files if no -o option given. Also\n"
8804 " separate input files from runtime arguments\n"
8805 " -Bdir set tcc internal library path\n"
8806 " -bench output compilation statistics\n"
8807 "Preprocessor options:\n"
8808 " -Idir add include path 'dir'\n"
8809 " -Dsym[=val] define 'sym' with value 'val'\n"
8810 " -Usym undefine 'sym'\n"
8812 " -Ldir add library path 'dir'\n"
8813 " -llib link with dynamic or static library 'lib'\n"
8814 " -shared generate a shared library\n"
8815 " -static static linking\n"
8816 " -r relocatable output\n"
8817 "Debugger options:\n"
8818 " -g generate runtime debug info\n"
8819 #ifdef CONFIG_TCC_BCHECK
8820 " -b compile with built-in memory and bounds checker (implies -g)\n"
8822 " -bt N show N callers in stack traces\n"
8826 int main(int argc
, char **argv
)
8829 int optind
, output_type
, multiple_files
, i
, reloc_output
;
8832 int nb_files
, nb_libraries
, nb_objfiles
, dminus
, ret
;
8833 char objfilename
[1024];
8834 int64_t start_time
= 0;
8837 output_type
= TCC_OUTPUT_MEMORY
;
8848 if (optind
>= argc
) {
8856 /* add a new file */
8857 dynarray_add((void ***)&files
, &nb_files
, r
);
8858 if (!multiple_files
) {
8860 /* argv[0] will be this file */
8863 } else if (r
[1] == '-') {
8864 /* '--' enables multiple files input and also ends several file input */
8865 if (dminus
&& multiple_files
) {
8866 optind
--; /* argv[0] will be '--' */
8871 } else if (r
[1] == 'h' || r
[1] == '?') {
8875 } else if (r
[1] == 'I') {
8876 if (tcc_add_include_path(s
, r
+ 2) < 0)
8877 error("too many include paths");
8878 } else if (r
[1] == 'D') {
8881 value
= strchr(sym
, '=');
8886 tcc_define_symbol(s
, sym
, value
);
8887 } else if (r
[1] == 'U') {
8888 tcc_undefine_symbol(s
, r
+ 2);
8889 } else if (r
[1] == 'L') {
8890 tcc_add_library_path(s
, r
+ 2);
8891 } else if (r
[1] == 'B') {
8892 /* set tcc utilities path (mainly for tcc development) */
8893 tcc_lib_path
= r
+ 2;
8894 } else if (r
[1] == 'l') {
8895 dynarray_add((void ***)&files
, &nb_files
, r
);
8897 } else if (!strcmp(r
+ 1, "bench")) {
8899 } else if (!strcmp(r
+ 1, "bt")) {
8900 num_callers
= atoi(argv
[optind
++]);
8902 #ifdef CONFIG_TCC_BCHECK
8904 do_bounds_check
= 1;
8910 } else if (r
[1] == 'c') {
8912 output_type
= TCC_OUTPUT_OBJ
;
8913 } else if (!strcmp(r
+ 1, "static")) {
8915 } else if (!strcmp(r
+ 1, "shared")) {
8916 output_type
= TCC_OUTPUT_DLL
;
8917 } else if (r
[1] == 'o') {
8921 outfile
= argv
[optind
++];
8922 } else if (r
[1] == 'r') {
8923 /* generate a .o merging several output files */
8925 output_type
= TCC_OUTPUT_OBJ
;
8926 } else if (r
[1] == 'W' || r
[1] == 'O' || r
[1] == 'm' || r
[1] == 'f') {
8927 /* ignore those options to be a drop-in replacement for gcc */
8929 error("invalid option -- '%s'", r
);
8933 nb_objfiles
= nb_files
- nb_libraries
;
8935 /* if outfile provided without other options, we output an
8937 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
8938 output_type
= TCC_OUTPUT_EXE
;
8940 /* check -c consistency : only single file handled. XXX: checks file type */
8941 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
8942 /* accepts only a single input file */
8943 if (nb_objfiles
!= 1)
8944 error("cannot specify multiple files with -c");
8945 if (nb_libraries
!= 0)
8946 error("cannot specify libraries with -c");
8949 /* compute default outfile name */
8950 if (output_type
!= TCC_OUTPUT_MEMORY
&& !outfile
) {
8951 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
8953 /* add .o extension */
8954 pstrcpy(objfilename
, sizeof(objfilename
) - 1, files
[0]);
8955 ext
= strrchr(objfilename
, '.');
8957 goto default_outfile
;
8958 strcpy(ext
+ 1, "o");
8961 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
8963 outfile
= objfilename
;
8967 start_time
= getclock_us();
8970 tcc_set_output_type(s
, output_type
);
8972 /* compile or add each files or library */
8973 for(i
= 0;i
< nb_files
; i
++) {
8974 const char *filename
;
8976 filename
= files
[i
];
8977 if (filename
[0] == '-') {
8978 if (tcc_add_library(s
, filename
+ 2) < 0)
8979 error("cannot find %s", filename
);
8981 if (tcc_add_file(s
, filename
) < 0) {
8988 /* free all files */
8993 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
8994 if (total_time
< 0.001)
8996 if (total_bytes
< 1)
8998 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
8999 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
9000 total_time
, (int)(total_lines
/ total_time
),
9001 total_bytes
/ total_time
/ 1000000.0);
9004 if (s
->output_type
!= TCC_OUTPUT_MEMORY
) {
9005 tcc_output_file(s
, outfile
);
9008 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
9011 /* XXX: cannot do it with bound checking because of the malloc hooks */
9012 if (!do_bounds_check
)
9017 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);