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.
31 #include <sys/ucontext.h>
35 #ifndef CONFIG_TCC_STATIC
42 /* preprocessor debug */
47 /* target selection */
48 //#define TCC_TARGET_I386 /* i386 code generator */
49 //#define TCC_TARGET_IL /* .NET CLI generator */
51 /* default target is I386 */
52 #if !defined(TCC_TARGET_I386) && !defined(TCC_TARGET_IL)
53 #define TCC_TARGET_I386
56 #if !defined(WIN32) && !defined(TCC_UCLIBC) && !defined(TCC_TARGET_IL)
57 #define CONFIG_TCC_BCHECK /* enable bound checking code */
60 #ifndef CONFIG_TCC_PREFIX
61 #define CONFIG_TCC_PREFIX "/usr/local"
64 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
65 executables or dlls */
66 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
68 #define INCLUDE_STACK_SIZE 32
69 #define IFDEF_STACK_SIZE 64
70 #define VSTACK_SIZE 64
71 #define STRING_MAX_SIZE 1024
73 #define TOK_HASH_SIZE 2048 /* must be a power of two */
74 #define TOK_ALLOC_INCR 512 /* must be a power of two */
75 #define SYM_HASH_SIZE 1031
77 /* token symbol management */
78 typedef struct TokenSym
{
79 struct TokenSym
*hash_next
;
80 int tok
; /* token number */
85 typedef struct CString
{
86 int size
; /* size in bytes */
87 void *data
; /* either 'char *' or 'int *' */
89 void *data_allocated
; /* if non NULL, data has been malloced */
93 typedef union CValue
{
99 unsigned int ul
; /* address (should be unsigned long on 64 bit cpu) */
101 unsigned long long ull
;
102 struct CString
*cstr
;
108 typedef struct SValue
{
110 unsigned short r
; /* register + flags */
111 unsigned short r2
; /* second register, used for 'long long'
112 type. If not used, set to VT_CONST */
113 CValue c
; /* constant, if VT_CONST */
114 struct Sym
*sym
; /* symbol, if (VT_SYM | VT_CONST) */
117 /* symbol management */
119 int v
; /* symbol token */
120 int t
; /* associated type */
121 int r
; /* associated register */
122 int c
; /* associated number */
123 struct Sym
*next
; /* next related symbol */
124 struct Sym
*prev
; /* prev symbol in stack */
125 struct Sym
*hash_next
; /* next symbol in hash table */
128 typedef struct SymStack
{
130 struct Sym
*hash
[SYM_HASH_SIZE
];
133 /* section definition */
134 /* XXX: use directly ELF structure for parameters ? */
135 /* special flag to indicate that the section should not be linked to
137 #define SHF_PRIVATE 0x80000000
139 typedef struct Section
{
140 unsigned long data_offset
; /* current data offset */
141 unsigned char *data
; /* section data */
142 unsigned long data_allocated
; /* used for realloc() handling */
143 int sh_name
; /* elf section name (only used during output) */
144 int sh_num
; /* elf section number */
145 int sh_type
; /* elf section type */
146 int sh_flags
; /* elf section flags */
147 int sh_info
; /* elf section info */
148 int sh_addralign
; /* elf section alignment */
149 int sh_entsize
; /* elf entry size */
150 unsigned long sh_size
; /* section size (only used during output) */
151 unsigned long sh_addr
; /* address at which the section is relocated */
152 unsigned long sh_offset
; /* address at which the section is relocated */
153 int nb_hashed_syms
; /* used to resize the hash table */
154 struct Section
*link
; /* link to another section */
155 struct Section
*reloc
; /* corresponding section for relocation, if any */
156 struct Section
*hash
; /* hash table for symbols */
157 struct Section
*next
;
158 char name
[64]; /* section name */
161 typedef struct DLLReference
{
166 /* GNUC attribute definition */
167 typedef struct AttributeDef
{
170 unsigned char func_call
; /* FUNC_CDECL or FUNC_STDCALL */
173 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
174 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
175 #define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
177 /* stored in 'Sym.c' field */
178 #define FUNC_NEW 1 /* ansi function prototype */
179 #define FUNC_OLD 2 /* old function prototype */
180 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
182 /* stored in 'Sym.r' field */
183 #define FUNC_CDECL 0 /* standard c call */
184 #define FUNC_STDCALL 1 /* pascal c call */
186 /* field 'Sym.t' for macros */
187 #define MACRO_OBJ 0 /* object like macro */
188 #define MACRO_FUNC 1 /* function like macro */
190 /* field 'Sym.t' for labels */
191 #define LABEL_FORWARD 1 /* label is forward defined */
193 /* type_decl() types */
194 #define TYPE_ABSTRACT 1 /* type without variable */
195 #define TYPE_DIRECT 2 /* type with variable */
197 #define IO_BUF_SIZE 8192
199 typedef struct BufferedFile
{
200 unsigned char *buf_ptr
;
201 unsigned char *buf_end
;
203 int line_num
; /* current line number - here to simply code */
204 char filename
[1024]; /* current filename - here to simplify code */
205 unsigned char buffer
[IO_BUF_SIZE
+ 1]; /* extra size for CH_EOB char */
208 #define CH_EOB 0 /* end of buffer or '\0' char in file */
209 #define CH_EOF (-1) /* end of file */
211 /* parsing state (used to save parser state to reparse part of the
212 source several times) */
213 typedef struct ParseState
{
220 /* used to record tokens */
221 typedef struct TokenString
{
228 struct BufferedFile
*file
;
229 int ch
, ch1
, tok
, tok1
;
231 CString tokcstr
; /* current parsed string, if any */
232 int return_linefeed
; /* if true, line feed is returned as a token */
236 int nb_sections
; /* number of sections, including first dummy section */
237 Section
*text_section
, *data_section
, *bss_section
; /* predefined sections */
238 Section
*cur_text_section
; /* current section where function code is
240 /* bound check related sections */
241 Section
*bounds_section
; /* contains global data bound description */
242 Section
*lbounds_section
; /* contains local data bound description */
243 /* symbol sections */
244 Section
*symtab_section
, *strtab_section
;
245 /* temporary dynamic symbol sections (for dll loading) */
246 Section
*dynsymtab_section
;
247 /* exported dynamic symbol section */
251 unsigned long *got_offsets
;
254 /* give the correspondance from symtab indexes to dynsym indexes */
255 int *symtab_to_dynsym
;
257 /* array of all loaded dlls (including those referenced by loaded
259 DLLReference
**loaded_dlls
;
263 Section
*stab_section
, *stabstr_section
;
265 char **library_paths
;
266 int nb_library_paths
;
268 /* loc : local variable index
269 ind : output code index
271 anon_sym: anonymous symbol index
275 /* expression generation modifiers */
276 int const_wanted
; /* true if constant wanted */
277 int global_expr
; /* true if compound literals must be allocated
278 globally (used during initializers parsing */
279 int func_vt
, func_vc
; /* current function return type (used by
280 return instruction) */
281 int last_line_num
, last_ind
, func_ind
; /* debug last line number and pc */
283 TokenSym
**table_ident
;
284 TokenSym
*hash_ident
[TOK_HASH_SIZE
];
285 char token_buf
[STRING_MAX_SIZE
+ 1];
287 SymStack define_stack
, global_stack
, local_stack
, label_stack
;
289 SValue vstack
[VSTACK_SIZE
], *vtop
;
290 int *macro_ptr
, *macro_ptr_allocated
;
291 BufferedFile
*include_stack
[INCLUDE_STACK_SIZE
], **include_stack_ptr
;
292 int ifdef_stack
[IFDEF_STACK_SIZE
], *ifdef_stack_ptr
;
293 char **include_paths
;
294 int nb_include_paths
;
295 char **sysinclude_paths
;
296 int nb_sysinclude_paths
;
297 int char_pointer_type
;
300 /* compile with debug symbol (and use them if error during execution) */
303 /* compile with built-in memory and bounds checker */
304 int do_bounds_check
= 0;
306 /* display benchmark infos */
311 /* use GNU C extensions */
314 /* use Tiny C extensions */
317 /* if true, static linking is performed */
320 /* give the path of the tcc libraries */
321 static const char *tcc_lib_path
= CONFIG_TCC_PREFIX
"/lib/tcc";
327 /* The current value can be: */
328 #define VT_VALMASK 0x00ff
329 #define VT_CONST 0x00f0 /* constant in vc
330 (must be first non register value) */
331 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
332 #define VT_LOCAL 0x00f2 /* offset on stack */
333 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
334 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
335 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
336 #define VT_LVAL 0x0100 /* var is an lvalue */
337 #define VT_SYM 0x0200 /* a symbol value is added */
338 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
339 char/short stored in integer registers) */
340 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
341 dereferencing value */
342 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
343 bounding function call point is in vc */
344 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
345 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
346 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
347 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
350 #define VT_STRUCT_SHIFT 12 /* structure/enum name shift (20 bits left) */
352 #define VT_INT 0 /* integer type */
353 #define VT_BYTE 1 /* signed byte type */
354 #define VT_SHORT 2 /* short type */
355 #define VT_VOID 3 /* void type */
356 #define VT_PTR 4 /* pointer */
357 #define VT_ENUM 5 /* enum definition */
358 #define VT_FUNC 6 /* function type */
359 #define VT_STRUCT 7 /* struct/union definition */
360 #define VT_FLOAT 8 /* IEEE float */
361 #define VT_DOUBLE 9 /* IEEE double */
362 #define VT_LDOUBLE 10 /* IEEE long double */
363 #define VT_BOOL 11 /* ISOC99 boolean type */
364 #define VT_LLONG 12 /* 64 bit integer */
365 #define VT_LONG 13 /* long integer (NEVER USED as type, only
367 #define VT_BTYPE 0x000f /* mask for basic type */
368 #define VT_UNSIGNED 0x0010 /* unsigned type */
369 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
370 #define VT_BITFIELD 0x0040 /* bitfield modifier */
373 #define VT_EXTERN 0x00000080 /* extern definition */
374 #define VT_STATIC 0x00000100 /* static variable */
375 #define VT_TYPEDEF 0x00000200 /* typedef definition */
377 /* type mask (except storage) */
378 #define VT_TYPE (~(VT_EXTERN | VT_STATIC | VT_TYPEDEF))
382 /* warning: the following compare tokens depend on i386 asm code */
394 #define TOK_LAND 0xa0
398 #define TOK_MID 0xa3 /* inc/dec, to void constant */
400 #define TOK_UDIV 0xb0 /* unsigned division */
401 #define TOK_UMOD 0xb1 /* unsigned modulo */
402 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
403 #define TOK_CINT 0xb3 /* number in tokc */
404 #define TOK_CCHAR 0xb4 /* char constant in tokc */
405 #define TOK_STR 0xb5 /* pointer to string in tokc */
406 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
407 #define TOK_LCHAR 0xb7
408 #define TOK_LSTR 0xb8
409 #define TOK_CFLOAT 0xb9 /* float constant */
410 #define TOK_LINENUM 0xba /* line number info */
411 #define TOK_CDOUBLE 0xc0 /* double constant */
412 #define TOK_CLDOUBLE 0xc1 /* long double constant */
413 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
414 #define TOK_ADDC1 0xc3 /* add with carry generation */
415 #define TOK_ADDC2 0xc4 /* add with carry use */
416 #define TOK_SUBC1 0xc5 /* add with carry generation */
417 #define TOK_SUBC2 0xc6 /* add with carry use */
418 #define TOK_CUINT 0xc8 /* unsigned int constant */
419 #define TOK_CLLONG 0xc9 /* long long constant */
420 #define TOK_CULLONG 0xca /* unsigned long long constant */
421 #define TOK_ARROW 0xcb
422 #define TOK_DOTS 0xcc /* three dots */
423 #define TOK_SHR 0xcd /* unsigned shift right */
425 #define TOK_SHL 0x01 /* shift left */
426 #define TOK_SAR 0x02 /* signed shift right */
428 /* assignement operators : normal operator or 0x80 */
429 #define TOK_A_MOD 0xa5
430 #define TOK_A_AND 0xa6
431 #define TOK_A_MUL 0xaa
432 #define TOK_A_ADD 0xab
433 #define TOK_A_SUB 0xad
434 #define TOK_A_DIV 0xaf
435 #define TOK_A_XOR 0xde
436 #define TOK_A_OR 0xfc
437 #define TOK_A_SHL 0x81
438 #define TOK_A_SAR 0x82
440 /* WARNING: the content of this string encodes token numbers */
441 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";
443 #define TOK_EOF (-1) /* end of file */
444 #define TOK_LINEFEED 10 /* line feed */
446 /* all identificators and strings have token above that */
447 #define TOK_IDENT 256
468 /* ignored types Must have contiguous values */
474 TOK___SIGNED__
, /* gcc keyword */
477 TOK___INLINE__
, /* gcc keyword */
480 /* unsupported type */
494 /* preprocessor only */
495 TOK_UIDENT
, /* first "user" ident (not keyword) */
496 TOK_DEFINE
= TOK_UIDENT
,
507 #define DEF(id, str) id,
513 "int\0void\0char\0if\0else\0while\0break\0return\0for\0extern\0static\0"
514 "unsigned\0goto\0do\0continue\0switch\0case\0const\0volatile\0long\0"
515 "register\0signed\0__signed__\0auto\0inline\0__inline__\0restrict\0"
516 "float\0double\0_Bool\0short\0struct\0union\0typedef\0default\0enum\0"
517 "sizeof\0__attribute__\0"
518 /* the following are not keywords. They are included to ease parsing */
519 "define\0include\0ifdef\0ifndef\0elif\0endif\0"
520 "defined\0undef\0error\0line\0"
521 /* builtin functions */
522 #define DEF(id, str) str "\0"
528 #define snprintf _snprintf
531 #if defined(WIN32) || defined(TCC_UCLIBC)
532 /* currently incorrect */
533 long double strtold(const char *nptr
, char **endptr
)
535 return (long double)strtod(nptr
, endptr
);
537 float strtof(const char *nptr
, char **endptr
)
539 return (float)strtod(nptr
, endptr
);
542 /* XXX: need to define this to use them in non ISOC99 context */
543 extern float strtof (const char *__nptr
, char **__endptr
);
544 extern long double strtold (const char *__nptr
, char **__endptr
);
547 static char *pstrcpy(char *buf
, int buf_size
, const char *s
);
548 static char *pstrcat(char *buf
, int buf_size
, const char *s
);
552 void next_nomacro(void);
553 static int expr_const(void);
557 static void decl_initializer(int t
, Section
*sec
, unsigned long c
,
558 int first
, int size_only
);
559 static void decl_initializer_alloc(int t
, AttributeDef
*ad
, int r
,
560 int has_init
, int v
, int scope
);
562 void gv2(int rc1
, int rc2
);
563 void move_reg(int r
, int s
);
564 void save_regs(int n
);
565 void save_reg(int r
);
571 void macro_subst(TokenString
*tok_str
,
572 Sym
**nested_list
, int *macro_str
);
573 int save_reg_forced(int r
);
575 void force_charshort_cast(int t
);
576 void gen_cast(int t
);
578 Sym
*sym_find(int v
);
579 Sym
*sym_push(int v
, int t
, int r
, int c
);
582 int type_size(int t
, int *a
);
583 int pointed_type(int t
);
584 int pointed_size(int t
);
585 static int lvalue_type(int t
);
586 int is_compatible_types(int t1
, int t2
);
587 int parse_btype(int *type_ptr
, AttributeDef
*ad
);
588 int type_decl(AttributeDef
*ad
, int *v
, int t
, int td
);
590 void error(const char *fmt
, ...);
591 void rt_error(unsigned long pc
, const char *fmt
, ...);
593 void vset(int t
, int r
, int v
);
594 void type_to_str(char *buf
, int buf_size
,
595 int t
, const char *varstr
);
596 char *get_tok_str(int v
, CValue
*cv
);
597 static Sym
*get_sym_ref(int t
, Section
*sec
,
598 unsigned long offset
, unsigned long size
);
599 static Sym
*external_global_sym(int v
, int u
, int r
);
601 /* section generation */
602 static void section_realloc(Section
*sec
, unsigned long new_size
);
603 static void *section_ptr_add(Section
*sec
, unsigned long size
);
604 static void put_extern_sym(Sym
*sym
, Section
*section
,
605 unsigned long value
, unsigned long size
);
606 static void greloc(Section
*s
, Sym
*sym
, unsigned long addr
, int type
);
607 static int put_elf_str(Section
*s
, const char *sym
);
608 static int put_elf_sym(Section
*s
,
609 unsigned long value
, unsigned long size
,
610 int info
, int other
, int shndx
, const char *name
);
611 static int add_elf_sym(Section
*s
, unsigned long value
, unsigned long size
,
612 int info
, int sh_num
, const char *name
);
613 static void put_elf_reloc(Section
*symtab
, Section
*s
, unsigned long offset
,
614 int type
, int symbol
);
615 static void put_stabs(const char *str
, int type
, int other
, int desc
,
616 unsigned long value
);
617 static void put_stabs_r(const char *str
, int type
, int other
, int desc
,
618 unsigned long value
, Section
*sec
, int sym_index
);
619 static void put_stabn(int type
, int other
, int desc
, int value
);
620 static void put_stabd(int type
, int other
, int desc
);
621 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
);
623 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
624 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
625 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
);
627 /* true if float/double/long double type */
628 static inline int is_float(int t
)
632 return bt
== VT_LDOUBLE
|| bt
== VT_DOUBLE
|| bt
== VT_FLOAT
;
635 #ifdef TCC_TARGET_I386
636 #include "i386-gen.c"
642 #ifdef CONFIG_TCC_STATIC
644 #define RTLD_LAZY 0x001
645 #define RTLD_NOW 0x002
646 #define RTLD_GLOBAL 0x100
648 /* dummy function for profiling */
649 void *dlopen(const char *filename
, int flag
)
654 const char *dlerror(void)
659 typedef struct TCCSyms
{
664 #define TCCSYM(a) { #a, &a, },
666 /* add the symbol you want here if no dynamic linking is done */
667 static TCCSyms tcc_syms
[] = {
675 void *dlsym(void *handle
, const char *symbol
)
679 while (p
->str
!= NULL
) {
680 if (!strcmp(p
->str
, symbol
))
689 /********************************************************/
691 /* we use our own 'finite' function to avoid potential problems with
692 non standard math libs */
693 /* XXX: endianness dependant */
694 int ieee_finite(double d
)
697 return ((unsigned)((p
[1] | 0x800fffff) + 1)) >> 31;
700 /* copy a string and truncate it. */
701 static char *pstrcpy(char *buf
, int buf_size
, const char *s
)
708 q_end
= buf
+ buf_size
- 1;
720 /* strcat and truncate. */
721 static char *pstrcat(char *buf
, int buf_size
, const char *s
)
726 pstrcpy(buf
+ len
, buf_size
- len
, s
);
730 /* memory management */
736 static inline void tcc_free(void *ptr
)
739 mem_cur_size
-= malloc_usable_size(ptr
);
744 static void *tcc_malloc(unsigned long size
)
749 error("memory full");
751 mem_cur_size
+= malloc_usable_size(ptr
);
752 if (mem_cur_size
> mem_max_size
)
753 mem_max_size
= mem_cur_size
;
758 static void *tcc_mallocz(unsigned long size
)
761 ptr
= tcc_malloc(size
);
762 memset(ptr
, 0, size
);
766 static inline void *tcc_realloc(void *ptr
, unsigned long size
)
770 mem_cur_size
-= malloc_usable_size(ptr
);
772 ptr1
= realloc(ptr
, size
);
774 /* NOTE: count not correct if alloc error, but not critical */
775 mem_cur_size
+= malloc_usable_size(ptr1
);
776 if (mem_cur_size
> mem_max_size
)
777 mem_max_size
= mem_cur_size
;
782 static char *tcc_strdup(const char *str
)
785 ptr
= tcc_malloc(strlen(str
) + 1);
790 #define free(p) use_tcc_free(p)
791 #define malloc(s) use_tcc_malloc(s)
792 #define realloc(p, s) use_tcc_realloc(p, s)
794 static void dynarray_add(void ***ptab
, int *nb_ptr
, void *data
)
801 /* every power of two we double array size */
802 if ((nb
& (nb
- 1)) == 0) {
807 pp
= tcc_realloc(pp
, nb_alloc
* sizeof(void *));
809 error("memory full");
816 Section
*new_section(const char *name
, int sh_type
, int sh_flags
)
820 sec
= tcc_mallocz(sizeof(Section
));
821 pstrcpy(sec
->name
, sizeof(sec
->name
), name
);
822 sec
->sh_type
= sh_type
;
823 sec
->sh_flags
= sh_flags
;
830 sec
->sh_addralign
= 4;
833 sec
->sh_addralign
= 1;
836 sec
->sh_addralign
= 32; /* default conservative alignment */
840 /* only add section if not private */
841 if (!(sh_flags
& SHF_PRIVATE
)) {
842 sec
->sh_num
= nb_sections
;
843 dynarray_add((void ***)§ions
, &nb_sections
, sec
);
848 /* realloc section and set its content to zero */
849 static void section_realloc(Section
*sec
, unsigned long new_size
)
854 size
= sec
->data_allocated
;
857 while (size
< new_size
)
859 data
= tcc_realloc(sec
->data
, size
);
861 error("memory full");
862 memset(data
+ sec
->data_allocated
, 0, size
- sec
->data_allocated
);
864 sec
->data_allocated
= size
;
867 /* reserve at least 'size' bytes in section 'sec' from
869 static void *section_ptr_add(Section
*sec
, unsigned long size
)
871 unsigned long offset
, offset1
;
873 offset
= sec
->data_offset
;
874 offset1
= offset
+ size
;
875 if (offset1
> sec
->data_allocated
)
876 section_realloc(sec
, offset1
);
877 sec
->data_offset
= offset1
;
878 return sec
->data
+ offset
;
881 /* return a reference to a section, and create it if it does not
883 Section
*find_section(const char *name
)
887 for(i
= 1; i
< nb_sections
; i
++) {
889 if (!strcmp(name
, sec
->name
))
892 /* sections are created as PROGBITS */
893 return new_section(name
, SHT_PROGBITS
, SHF_ALLOC
);
896 /* update sym->c so that it points to an external symbol in section
897 'section' with value 'value' */
898 static void put_extern_sym(Sym
*sym
, Section
*section
,
899 unsigned long value
, unsigned long size
)
901 int sym_type
, sym_bind
, sh_num
, info
;
907 sh_num
= section
->sh_num
;
911 if ((sym
->t
& VT_BTYPE
) == VT_FUNC
)
914 sym_type
= STT_OBJECT
;
915 if (sym
->t
& VT_STATIC
)
916 sym_bind
= STB_LOCAL
;
918 sym_bind
= STB_GLOBAL
;
920 name
= get_tok_str(sym
->v
, NULL
);
921 #ifdef CONFIG_TCC_BCHECK
922 if (do_bounds_check
) {
923 /* if bound checking is activated, we change some function
924 names by adding the "__bound" prefix */
927 /* XXX: we rely only on malloc hooks */
939 strcpy(buf
, "__bound_");
946 info
= ELF32_ST_INFO(sym_bind
, sym_type
);
947 sym
->c
= add_elf_sym(symtab_section
, value
, size
, info
, sh_num
, name
);
949 esym
= &((Elf32_Sym
*)symtab_section
->data
)[sym
->c
];
950 esym
->st_value
= value
;
951 esym
->st_size
= size
;
952 esym
->st_shndx
= sh_num
;
956 /* add a new relocation entry to symbol 'sym' in section 's' */
957 static void greloc(Section
*s
, Sym
*sym
, unsigned long offset
, int type
)
960 put_extern_sym(sym
, NULL
, 0, 0);
961 /* now we can add ELF relocation info */
962 put_elf_reloc(symtab_section
, s
, offset
, type
, sym
->c
);
965 static inline int isid(int c
)
967 return (c
>= 'a' && c
<= 'z') ||
968 (c
>= 'A' && c
<= 'Z') ||
972 static inline int isnum(int c
)
974 return c
>= '0' && c
<= '9';
977 static inline int isoct(int c
)
979 return c
>= '0' && c
<= '7';
982 static inline int toup(int c
)
984 if (ch
>= 'a' && ch
<= 'z')
985 return ch
- 'a' + 'A';
995 for(f
= include_stack
; f
< include_stack_ptr
; f
++)
996 fprintf(stderr
, "In file included from %s:%d:\n",
997 (*f
)->filename
, (*f
)->line_num
);
998 if (file
->line_num
> 0) {
999 fprintf(stderr
, "%s:%d: ", file
->filename
, file
->line_num
);
1001 fprintf(stderr
, "%s: ", file
->filename
);
1004 fprintf(stderr
, "tcc: ");
1008 void error(const char *fmt
, ...)
1013 vfprintf(stderr
, fmt
, ap
);
1014 fprintf(stderr
, "\n");
1019 void expect(const char *msg
)
1021 error("%s expected", msg
);
1024 void warning(const char *fmt
, ...)
1030 fprintf(stderr
, "warning: ");
1031 vfprintf(stderr
, fmt
, ap
);
1032 fprintf(stderr
, "\n");
1039 error("'%c' expected", c
);
1043 void test_lvalue(void)
1045 if (!(vtop
->r
& VT_LVAL
))
1049 TokenSym
*tok_alloc(const char *str
, int len
)
1051 TokenSym
*ts
, **pts
, **ptable
;
1058 h
= (h
* 263 + ((unsigned char *)str
)[i
]) & (TOK_HASH_SIZE
- 1);
1060 pts
= &hash_ident
[h
];
1065 if (ts
->len
== len
&& !memcmp(ts
->str
, str
, len
))
1067 pts
= &(ts
->hash_next
);
1070 if (tok_ident
>= SYM_FIRST_ANOM
)
1071 error("memory full");
1073 /* expand token table if needed */
1074 i
= tok_ident
- TOK_IDENT
;
1075 if ((i
% TOK_ALLOC_INCR
) == 0) {
1076 ptable
= tcc_realloc(table_ident
, (i
+ TOK_ALLOC_INCR
) * sizeof(TokenSym
*));
1078 error("memory full");
1079 table_ident
= ptable
;
1082 ts
= tcc_malloc(sizeof(TokenSym
) + len
);
1083 table_ident
[i
] = ts
;
1084 ts
->tok
= tok_ident
++;
1086 ts
->hash_next
= NULL
;
1087 memcpy(ts
->str
, str
, len
+ 1);
1092 /* CString handling */
1094 static void cstr_realloc(CString
*cstr
, int new_size
)
1099 size
= cstr
->size_allocated
;
1101 size
= 8; /* no need to allocate a too small first string */
1102 while (size
< new_size
)
1104 data
= tcc_realloc(cstr
->data_allocated
, size
);
1106 error("memory full");
1107 cstr
->data_allocated
= data
;
1108 cstr
->size_allocated
= size
;
1113 static void cstr_ccat(CString
*cstr
, int ch
)
1116 size
= cstr
->size
+ 1;
1117 if (size
> cstr
->size_allocated
)
1118 cstr_realloc(cstr
, size
);
1119 ((unsigned char *)cstr
->data
)[size
- 1] = ch
;
1123 static void cstr_cat(CString
*cstr
, const char *str
)
1135 /* add a wide char */
1136 static void cstr_wccat(CString
*cstr
, int ch
)
1139 size
= cstr
->size
+ sizeof(int);
1140 if (size
> cstr
->size_allocated
)
1141 cstr_realloc(cstr
, size
);
1142 *(int *)(((unsigned char *)cstr
->data
) + size
- sizeof(int)) = ch
;
1146 static void cstr_new(CString
*cstr
)
1148 memset(cstr
, 0, sizeof(CString
));
1151 /* free string and reset it to NULL */
1152 static void cstr_free(CString
*cstr
)
1154 tcc_free(cstr
->data_allocated
);
1158 #define cstr_reset(cstr) cstr_free(cstr)
1160 /* XXX: unicode ? */
1161 static void add_char(CString
*cstr
, int c
)
1163 if (c
== '\'' || c
== '\"' || c
== '\\') {
1164 /* XXX: could be more precise if char or string */
1165 cstr_ccat(cstr
, '\\');
1167 if (c
>= 32 && c
<= 126) {
1170 cstr_ccat(cstr
, '\\');
1172 cstr_ccat(cstr
, 'n');
1174 cstr_ccat(cstr
, '0' + ((c
>> 6) & 7));
1175 cstr_ccat(cstr
, '0' + ((c
>> 3) & 7));
1176 cstr_ccat(cstr
, '0' + (c
& 7));
1181 /* XXX: buffer overflow */
1182 /* XXX: float tokens */
1183 char *get_tok_str(int v
, CValue
*cv
)
1185 static char buf
[STRING_MAX_SIZE
+ 1];
1186 static CString cstr_buf
;
1192 /* NOTE: to go faster, we give a fixed buffer for small strings */
1193 cstr_reset(&cstr_buf
);
1194 cstr_buf
.data
= buf
;
1195 cstr_buf
.size_allocated
= sizeof(buf
);
1201 /* XXX: not exact */
1202 sprintf(p
, "%u", cv
->ui
);
1206 cstr_ccat(&cstr_buf
, '\'');
1207 add_char(&cstr_buf
, cv
->i
);
1208 cstr_ccat(&cstr_buf
, '\'');
1209 cstr_ccat(&cstr_buf
, '\0');
1214 cstr_ccat(&cstr_buf
, '\"');
1216 len
= cstr
->size
- 1;
1218 add_char(&cstr_buf
, ((unsigned char *)cstr
->data
)[i
]);
1220 len
= (cstr
->size
/ sizeof(int)) - 1;
1222 add_char(&cstr_buf
, ((int *)cstr
->data
)[i
]);
1224 cstr_ccat(&cstr_buf
, '\"');
1225 cstr_ccat(&cstr_buf
, '\0');
1234 return strcpy(p
, "<<=");
1236 return strcpy(p
, ">>=");
1238 if (v
< TOK_IDENT
) {
1239 /* search in two bytes table */
1253 } else if (v
< tok_ident
) {
1254 return table_ident
[v
- TOK_IDENT
]->str
;
1255 } else if (v
>= SYM_FIRST_ANOM
) {
1256 /* special name for anonymous symbol */
1257 sprintf(p
, "L.%u", v
- SYM_FIRST_ANOM
);
1259 /* should never happen */
1264 return cstr_buf
.data
;
1267 /* push, without hashing */
1268 Sym
*sym_push2(Sym
**ps
, int v
, int t
, int c
)
1271 s
= tcc_malloc(sizeof(Sym
));
1282 /* find a symbol and return its associated structure. 's' is the top
1283 of the symbol stack */
1284 Sym
*sym_find2(Sym
*s
, int v
)
1294 #define HASH_SYM(v) ((unsigned)(v) % SYM_HASH_SIZE)
1296 /* find a symbol and return its associated structure. 'st' is the
1298 Sym
*sym_find1(SymStack
*st
, int v
)
1302 s
= st
->hash
[HASH_SYM(v
)];
1311 Sym
*sym_push1(SymStack
*st
, int v
, int t
, int c
)
1314 s
= sym_push2(&st
->top
, v
, t
, c
);
1315 /* add in hash table */
1317 ps
= &st
->hash
[HASH_SYM(v
)];
1324 /* find a symbol in the right symbol space */
1325 Sym
*sym_find(int v
)
1328 s
= sym_find1(&local_stack
, v
);
1330 s
= sym_find1(&global_stack
, v
);
1334 /* push a given symbol on the symbol stack */
1335 Sym
*sym_push(int v
, int t
, int r
, int c
)
1338 if (local_stack
.top
)
1339 s
= sym_push1(&local_stack
, v
, t
, c
);
1341 s
= sym_push1(&global_stack
, v
, t
, c
);
1346 /* pop symbols until top reaches 'b' */
1347 void sym_pop(SymStack
*st
, Sym
*b
)
1354 /* free hash table entry, except if symbol was freed (only
1355 used for #undef symbols) */
1357 st
->hash
[HASH_SYM(s
->v
)] = s
->hash_next
;
1364 /* undefined a hashed symbol (used for #undef). Its name is set to
1366 void sym_undef(SymStack
*st
, Sym
*s
)
1369 ss
= &st
->hash
[HASH_SYM(s
->v
)];
1370 while (*ss
!= NULL
) {
1373 ss
= &(*ss
)->hash_next
;
1381 BufferedFile
*tcc_open(const char *filename
)
1386 fd
= open(filename
, O_RDONLY
);
1389 bf
= tcc_malloc(sizeof(BufferedFile
));
1395 bf
->buf_ptr
= bf
->buffer
;
1396 bf
->buf_end
= bf
->buffer
;
1397 bf
->buffer
[0] = CH_EOB
; /* put eob symbol */
1398 pstrcpy(bf
->filename
, sizeof(bf
->filename
), filename
);
1400 // printf("opening '%s'\n", filename);
1404 void tcc_close(BufferedFile
*bf
)
1406 total_lines
+= bf
->line_num
;
1411 /* read one char. MUST call tcc_fillbuf if CH_EOB is read */
1412 #define TCC_GETC(bf) (*(bf)->buf_ptr++)
1414 /* fill input buffer and return next char */
1415 int tcc_getc_slow(BufferedFile
*bf
)
1418 /* only tries to read if really end of buffer */
1419 if (bf
->buf_ptr
>= bf
->buf_end
) {
1421 len
= read(bf
->fd
, bf
->buffer
, IO_BUF_SIZE
);
1428 bf
->buf_ptr
= bf
->buffer
;
1429 bf
->buf_end
= bf
->buffer
+ len
;
1430 *bf
->buf_end
= CH_EOB
;
1432 if (bf
->buf_ptr
< bf
->buf_end
) {
1433 return *bf
->buf_ptr
++;
1435 bf
->buf_ptr
= bf
->buf_end
;
1440 /* no need to put that inline */
1441 void handle_eob(void)
1444 ch1
= tcc_getc_slow(file
);
1448 if (include_stack_ptr
== include_stack
)
1450 /* add end of include file debug info */
1452 put_stabd(N_EINCL
, 0, 0);
1454 /* pop include stack */
1456 include_stack_ptr
--;
1457 file
= *include_stack_ptr
;
1461 /* read next char from current input file */
1462 static inline void inp(void)
1464 ch1
= TCC_GETC(file
);
1465 /* end of buffer/file handling */
1470 // printf("ch1=%c 0x%x\n", ch1, ch1);
1473 /* handle '\\n' and '\\r\n' */
1474 static void handle_stray(void)
1479 } else if (ch1
== '\r') {
1482 error("invalid character after '\\'");
1489 } while (ch
== '\\');
1492 /* input with '\\n' handling. Also supports '\\r\n' for horrible MSDOS
1494 static inline void minp(void)
1503 /* same as minp, but also skip comments */
1504 static void cinp(void)
1511 /* single line C++ comments */
1513 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1515 ch
= ' '; /* return space */
1516 } else if (ch1
== '*') {
1519 while (ch1
!= CH_EOF
) {
1522 if (c
== '*' && ch1
== '/') {
1524 ch
= ' '; /* return space */
1536 /* space exlcuding newline */
1537 static inline int is_space(int ch
)
1539 return ch
== ' ' || ch
== '\t' || ch
== '\v' || ch
== '\f' || ch
== '\r';
1542 static inline void skip_spaces(void)
1544 while (is_space(ch
))
1548 /* skip block of text until #else, #elif or #endif. skip also pairs of
1550 void preprocess_skip(void)
1555 while (ch
!= '\n') {
1566 (tok
== TOK_ELSE
|| tok
== TOK_ELIF
|| tok
== TOK_ENDIF
))
1568 if (tok
== TOK_IF
|| tok
== TOK_IFDEF
|| tok
== TOK_IFNDEF
)
1570 else if (tok
== TOK_ENDIF
)
1576 /* ParseState handling */
1578 /* XXX: currently, no include file info is stored. Thus, we cannot display
1579 accurate messages if the function or data definition spans multiple
1582 /* save current parse state in 's' */
1583 void save_parse_state(ParseState
*s
)
1585 s
->line_num
= file
->line_num
;
1586 s
->macro_ptr
= macro_ptr
;
1591 /* restore parse state from 's' */
1592 void restore_parse_state(ParseState
*s
)
1594 file
->line_num
= s
->line_num
;
1595 macro_ptr
= s
->macro_ptr
;
1600 /* return the number of additionnal 'ints' necessary to store the
1602 static inline int tok_ext_size(int t
)
1620 return LDOUBLE_SIZE
/ 4;
1626 /* token string handling */
1628 static inline void tok_str_new(TokenString
*s
)
1632 s
->last_line_num
= -1;
1635 static void tok_str_free(int *str
)
1646 if (t
== TOK_STR
|| t
== TOK_LSTR
) {
1647 /* XXX: use a macro to be portable on 64 bit ? */
1648 cstr
= (CString
*)(*p
++);
1652 p
+= tok_ext_size(t
);
1658 static void tok_str_add(TokenString
*s
, int t
)
1664 if ((len
& 63) == 0) {
1665 str
= tcc_realloc(str
, (len
+ 64) * sizeof(int));
1674 static void tok_str_add2(TokenString
*s
, int t
, CValue
*cv
)
1677 CString
*cstr
, *cstr1
;
1681 if (t
== TOK_STR
|| t
== TOK_LSTR
) {
1682 /* special case: need to duplicate string */
1684 cstr
= tcc_malloc(sizeof(CString
));
1687 cstr
->size_allocated
= size
;
1688 cstr
->data_allocated
= tcc_malloc(size
);
1689 cstr
->data
= cstr
->data_allocated
;
1690 memcpy(cstr
->data_allocated
, cstr1
->data_allocated
, size
);
1692 tok_str_add(s
, cv1
.tab
[0]);
1694 n
= tok_ext_size(t
);
1696 tok_str_add(s
, cv
->tab
[i
]);
1700 /* add the current parse token in token string 's' */
1701 static void tok_str_add_tok(TokenString
*s
)
1705 /* save line number info */
1706 if (file
->line_num
!= s
->last_line_num
) {
1707 s
->last_line_num
= file
->line_num
;
1708 cval
.i
= s
->last_line_num
;
1709 tok_str_add2(s
, TOK_LINENUM
, &cval
);
1711 tok_str_add2(s
, tok
, &tokc
);
1714 /* get a token from an integer array and increment pointer accordingly */
1715 static int tok_get(int **tok_str
, CValue
*cv
)
1721 n
= tok_ext_size(t
);
1728 /* eval an expression for #if/#elif */
1729 int expr_preprocess(void)
1735 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
) {
1736 next(); /* do macro subst */
1737 if (tok
== TOK_DEFINED
) {
1742 c
= sym_find1(&define_stack
, tok
) != 0;
1747 } else if (tok
>= TOK_IDENT
) {
1748 /* if undefined macro */
1752 tok_str_add_tok(&str
);
1754 tok_str_add(&str
, -1); /* simulate end of file */
1755 tok_str_add(&str
, 0);
1756 /* now evaluate C constant expression */
1757 macro_ptr
= str
.str
;
1761 tok_str_free(str
.str
);
1765 #if defined(DEBUG) || defined(PP_DEBUG)
1766 void tok_print(int *str
)
1772 t
= tok_get(&str
, &cval
);
1775 printf(" %s", get_tok_str(t
, &cval
));
1781 /* parse after #define */
1782 void parse_define(void)
1784 Sym
*s
, *first
, **ps
;
1785 int v
, t
, varg
, is_vaargs
;
1789 /* XXX: should check if same macro (ANSI) */
1792 /* '(' must be just after macro definition for MACRO_FUNC */
1797 while (tok
!= ')') {
1801 if (varg
== TOK_DOTS
) {
1802 varg
= TOK___VA_ARGS__
;
1804 } else if (tok
== TOK_DOTS
&& gnu_ext
) {
1808 if (varg
< TOK_IDENT
)
1809 error("badly punctuated parameter list");
1810 s
= sym_push1(&define_stack
, varg
| SYM_FIELD
, is_vaargs
, 0);
1822 if (ch
== '\n' || ch
== CH_EOF
)
1825 tok_str_add2(&str
, tok
, &tokc
);
1827 tok_str_add(&str
, 0);
1829 printf("define %s %d: ", get_tok_str(v
, NULL
), t
);
1832 s
= sym_push1(&define_stack
, v
, t
, (int)str
.str
);
1836 void preprocess(void)
1839 char buf
[1024], *q
, *p
;
1844 return_linefeed
= 1; /* linefeed will be returned as a token */
1848 if (tok
== TOK_DEFINE
) {
1851 } else if (tok
== TOK_UNDEF
) {
1853 s
= sym_find1(&define_stack
, tok
);
1854 /* undefine symbol by putting an invalid name */
1856 sym_undef(&define_stack
, s
);
1857 } else if (tok
== TOK_INCLUDE
) {
1862 } else if (ch
== '\"') {
1867 while (ch
!= c
&& ch
!= '\n' && ch
!= CH_EOF
) {
1868 if ((q
- buf
) < sizeof(buf
) - 1)
1873 /* eat all spaces and comments after include */
1874 /* XXX: slightly incorrect */
1875 while (ch1
!= '\n' && ch1
!= CH_EOF
)
1878 /* computed #include : either we have only strings or
1879 we have anything enclosed in '<>' */
1882 if (tok
== TOK_STR
) {
1883 while (tok
!= TOK_LINEFEED
) {
1884 if (tok
!= TOK_STR
) {
1886 error("'#include' expects \"FILENAME\" or <FILENAME>");
1888 pstrcat(buf
, sizeof(buf
), (char *)tokc
.cstr
->data
);
1894 while (tok
!= TOK_LINEFEED
) {
1895 pstrcat(buf
, sizeof(buf
), get_tok_str(tok
, &tokc
));
1899 /* check syntax and remove '<>' */
1900 if (len
< 2 || buf
[0] != '<' || buf
[len
- 1] != '>')
1901 goto include_syntax
;
1902 memmove(buf
, buf
+ 1, len
- 2);
1903 buf
[len
- 2] = '\0';
1908 if (include_stack_ptr
>= include_stack
+ INCLUDE_STACK_SIZE
)
1909 error("#include recursion too deep");
1911 /* first search in current dir if "header.h" */
1913 p
= strrchr(file
->filename
, '/');
1915 size
= p
+ 1 - file
->filename
;
1916 if (size
> sizeof(buf1
) - 1)
1917 size
= sizeof(buf1
) - 1;
1918 memcpy(buf1
, file
->filename
, size
);
1920 pstrcat(buf1
, sizeof(buf1
), buf
);
1925 /* now search in all the include paths */
1926 n
= nb_include_paths
+ nb_sysinclude_paths
;
1927 for(i
= 0; i
< n
; i
++) {
1929 if (i
< nb_include_paths
)
1930 path
= include_paths
[i
];
1932 path
= sysinclude_paths
[i
- nb_include_paths
];
1933 pstrcpy(buf1
, sizeof(buf1
), path
);
1934 pstrcat(buf1
, sizeof(buf1
), "/");
1935 pstrcat(buf1
, sizeof(buf1
), buf
);
1940 error("include file '%s' not found", buf
);
1943 /* push current file in stack */
1944 /* XXX: fix current line init */
1945 *include_stack_ptr
++ = file
;
1947 /* add include file debug info */
1949 put_stabs(file
->filename
, N_BINCL
, 0, 0, 0);
1953 } else if (tok
== TOK_IFNDEF
) {
1956 } else if (tok
== TOK_IF
) {
1957 c
= expr_preprocess();
1959 } else if (tok
== TOK_IFDEF
) {
1963 c
= (sym_find1(&define_stack
, tok
) != 0) ^ c
;
1965 if (ifdef_stack_ptr
>= ifdef_stack
+ IFDEF_STACK_SIZE
)
1966 error("memory full");
1967 *ifdef_stack_ptr
++ = c
;
1969 } else if (tok
== TOK_ELSE
) {
1970 if (ifdef_stack_ptr
== ifdef_stack
)
1971 error("#else without matching #if");
1972 if (ifdef_stack_ptr
[-1] & 2)
1973 error("#else after #else");
1974 c
= (ifdef_stack_ptr
[-1] ^= 3);
1976 } else if (tok
== TOK_ELIF
) {
1977 if (ifdef_stack_ptr
== ifdef_stack
)
1978 error("#elif without matching #if");
1979 c
= ifdef_stack_ptr
[-1];
1981 error("#elif after #else");
1982 /* last #if/#elif expression was true: we skip */
1985 c
= expr_preprocess();
1986 ifdef_stack_ptr
[-1] = c
;
1993 } else if (tok
== TOK_ENDIF
) {
1994 if (ifdef_stack_ptr
== ifdef_stack
)
1995 error("#endif without matching #if");
1997 } else if (tok
== TOK_LINE
) {
2000 if (tok
!= TOK_CINT
)
2004 if (tok
!= TOK_LINEFEED
) {
2007 pstrcpy(file
->filename
, sizeof(file
->filename
),
2008 (char *)tokc
.cstr
->data
);
2010 /* NOTE: we do it there to avoid problems with linefeed */
2011 file
->line_num
= line_num
;
2012 } else if (tok
== TOK_ERROR
) {
2015 /* ignore other preprocess commands or #! for C scripts */
2016 while (tok
!= TOK_LINEFEED
&& tok
!= TOK_EOF
)
2019 return_linefeed
= 0;
2022 /* read a number in base b */
2023 static int getn(int b
)
2028 if (ch
>= 'a' && ch
<= 'f')
2030 else if (ch
>= 'A' && ch
<= 'F')
2036 if (t
< 0 || t
>= b
)
2044 /* read a character for string or char constant and eval escape codes */
2045 static int getq(void)
2053 /* at most three octal digits */
2057 c
= c
* 8 + ch
- '0';
2060 c
= c
* 8 + ch
- '0';
2065 } else if (ch
== 'x') {
2083 else if (ch
== 'e' && gnu_ext
)
2085 else if (ch
== '\'' || ch
== '\"' || ch
== '\\' || ch
== '?')
2088 error("invalid escaped char");
2091 } else if (c
== '\r' && ch
== '\n') {
2098 /* we use 64 bit numbers */
2101 /* bn = (bn << shift) | or_val */
2102 void bn_lshift(unsigned int *bn
, int shift
, int or_val
)
2106 for(i
=0;i
<BN_SIZE
;i
++) {
2108 bn
[i
] = (v
<< shift
) | or_val
;
2109 or_val
= v
>> (32 - shift
);
2113 void bn_zero(unsigned int *bn
)
2116 for(i
=0;i
<BN_SIZE
;i
++) {
2121 void parse_number(void)
2123 int b
, t
, shift
, frac_bits
, s
, exp_val
;
2125 unsigned int bn
[BN_SIZE
];
2135 /* special dot handling */
2136 if (ch
>= '0' && ch
<= '9') {
2137 goto float_frac_parse
;
2138 } else if (ch
== '.') {
2149 } else if (t
== '0') {
2150 if (ch
== 'x' || ch
== 'X') {
2154 } else if (tcc_ext
&& (ch
== 'b' || ch
== 'B')) {
2160 /* parse all digits. cannot check octal numbers at this stage
2161 because of floating point constants */
2163 if (ch
>= 'a' && ch
<= 'f')
2165 else if (ch
>= 'A' && ch
<= 'F')
2173 if (q
>= token_buf
+ STRING_MAX_SIZE
) {
2175 error("number too long");
2181 ((ch
== 'e' || ch
== 'E') && b
== 10) ||
2182 ((ch
== 'p' || ch
== 'P') && (b
== 16 || b
== 2))) {
2184 /* NOTE: strtox should support that for hexa numbers, but
2185 non ISOC99 libcs do not support it, so we prefer to do
2187 /* hexadecimal or binary floats */
2188 /* XXX: handle overflows */
2200 } else if (t
>= 'a') {
2202 } else if (t
>= 'A') {
2207 bn_lshift(bn
, shift
, t
);
2214 if (t
>= 'a' && t
<= 'f') {
2216 } else if (t
>= 'A' && t
<= 'F') {
2218 } else if (t
>= '0' && t
<= '9') {
2224 error("invalid digit");
2225 bn_lshift(bn
, shift
, t
);
2230 if (ch
!= 'p' && ch
!= 'P')
2231 error("exponent expected");
2237 } else if (ch
== '-') {
2241 if (ch
< '0' || ch
> '9')
2242 error("exponent digits expected");
2243 while (ch
>= '0' && ch
<= '9') {
2244 exp_val
= exp_val
* 10 + ch
- '0';
2247 exp_val
= exp_val
* s
;
2249 /* now we can generate the number */
2250 /* XXX: should patch directly float number */
2251 d
= (double)bn
[1] * 4294967296.0 + (double)bn
[0];
2252 d
= ldexp(d
, exp_val
- frac_bits
);
2257 /* float : should handle overflow */
2259 } else if (t
== 'L') {
2262 /* XXX: not large enough */
2263 tokc
.ld
= (long double)d
;
2269 /* decimal floats */
2271 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2276 while (ch
>= '0' && ch
<= '9') {
2277 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2283 if (ch
== 'e' || ch
== 'E') {
2284 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2288 if (ch
== '-' || ch
== '+') {
2289 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2294 if (ch
< '0' || ch
> '9')
2295 error("exponent digits expected");
2296 while (ch
>= '0' && ch
<= '9') {
2297 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2309 tokc
.f
= strtof(token_buf
, NULL
);
2310 } else if (t
== 'L') {
2313 tokc
.ld
= strtold(token_buf
, NULL
);
2316 tokc
.d
= strtod(token_buf
, NULL
);
2320 unsigned long long n
, n1
;
2323 /* integer number */
2326 if (b
== 10 && *q
== '0') {
2333 /* no need for checks except for base 10 / 8 errors */
2336 } else if (t
>= 'a') {
2338 } else if (t
>= 'A') {
2343 error("invalid digit");
2347 /* detect overflow */
2349 error("integer constant overflow");
2352 /* XXX: not exactly ANSI compliant */
2353 if ((n
& 0xffffffff00000000LL
) != 0) {
2358 } else if (n
> 0x7fffffff) {
2368 error("three 'l' in integer constant");
2371 if (tok
== TOK_CINT
)
2373 else if (tok
== TOK_CUINT
)
2377 } else if (t
== 'U') {
2378 if (tok
== TOK_CINT
)
2380 else if (tok
== TOK_CLLONG
)
2387 if (tok
== TOK_CINT
|| tok
== TOK_CUINT
)
2395 /* return next token without macro substitution */
2396 void next_nomacro1(void)
2404 while (ch
== '\n') {
2405 /* during preprocessor parsing, '\n' is a token */
2406 if (return_linefeed
) {
2413 /* preprocessor command if # at start of line after
2436 while (isid(ch
) || isnum(ch
)) {
2437 if (q
>= token_buf
+ STRING_MAX_SIZE
)
2438 error("ident too long");
2443 ts
= tok_alloc(token_buf
, q
- token_buf
);
2445 } else if (isnum(ch
) || ch
== '.') {
2447 } else if (ch
== '\'') {
2452 /* this cast is needed if >= 128 */
2453 if (tok
== TOK_CCHAR
)
2459 } else if (ch
== '\"') {
2463 cstr_reset(&tokcstr
);
2464 while (ch
!= '\"') {
2467 error("unterminated string");
2469 cstr_ccat(&tokcstr
, b
);
2471 cstr_wccat(&tokcstr
, b
);
2474 cstr_ccat(&tokcstr
, '\0');
2476 cstr_wccat(&tokcstr
, '\0');
2477 tokc
.cstr
= &tokcstr
;
2485 if (*q
== tok
&& q
[1] == ch
) {
2488 /* three chars tests */
2489 if (tok
== TOK_SHL
|| tok
== TOK_SAR
) {
2494 } else if (tok
== TOK_DOTS
) {
2496 error("parse error");
2503 /* single char substitutions */
2506 else if (tok
== '>')
2511 /* return next token without macro substitution. Can read input from
2519 tok
= tok_get(¯o_ptr
, &tokc
);
2520 if (tok
== TOK_LINENUM
) {
2521 file
->line_num
= tokc
.i
;
2530 /* substitute args in macro_str and return allocated string */
2531 int *macro_arg_subst(Sym
**nested_list
, int *macro_str
, Sym
*args
)
2533 int *st
, last_tok
, t
, notfirst
;
2542 t
= tok_get(¯o_str
, &cval
);
2547 t
= tok_get(¯o_str
, &cval
);
2550 s
= sym_find2(args
, t
);
2557 cstr_ccat(&cstr
, ' ');
2558 t
= tok_get(&st
, &cval
);
2559 cstr_cat(&cstr
, get_tok_str(t
, &cval
));
2562 cstr_ccat(&cstr
, '\0');
2564 printf("stringize: %s\n", (char *)cstr
.data
);
2568 tok_str_add2(&str
, TOK_STR
, &cval
);
2571 tok_str_add2(&str
, t
, &cval
);
2573 } else if (t
>= TOK_IDENT
) {
2574 s
= sym_find2(args
, t
);
2577 /* if '##' is present before or after, no arg substitution */
2578 if (*macro_str
== TOK_TWOSHARPS
|| last_tok
== TOK_TWOSHARPS
) {
2579 /* special case for var arg macros : ## eats the
2580 ',' if empty VA_ARGS riable. */
2581 /* XXX: test of the ',' is not 100%
2582 reliable. should fix it to avoid security
2584 if (gnu_ext
&& s
->t
&& *st
== 0 &&
2585 last_tok
== TOK_TWOSHARPS
&&
2586 str
.len
>= 2 && str
.str
[str
.len
- 2] == ',') {
2587 /* suppress ',' '##' */
2592 t1
= tok_get(&st
, &cval
);
2595 tok_str_add2(&str
, t1
, &cval
);
2599 macro_subst(&str
, nested_list
, st
);
2602 tok_str_add(&str
, t
);
2605 tok_str_add2(&str
, t
, &cval
);
2609 tok_str_add(&str
, 0);
2613 /* handle the '##' operator */
2614 int *macro_twosharps(int *macro_str
)
2621 TokenString macro_str1
;
2623 tok_str_new(¯o_str1
);
2629 while (*macro_ptr
== TOK_TWOSHARPS
) {
2631 macro_ptr1
= macro_ptr
;
2634 t
= tok_get(¯o_ptr
, &cval
);
2635 /* XXX: we handle only most common cases:
2636 ident + ident or ident + number */
2637 if (tok
>= TOK_IDENT
&&
2638 (t
>= TOK_IDENT
|| t
== TOK_CINT
)) {
2639 p
= get_tok_str(tok
, &tokc
);
2640 pstrcpy(token_buf
, sizeof(token_buf
), p
);
2641 p
= get_tok_str(t
, &cval
);
2642 pstrcat(token_buf
, sizeof(token_buf
), p
);
2643 ts
= tok_alloc(token_buf
, 0);
2644 tok
= ts
->tok
; /* modify current token */
2646 /* cannot merge tokens: skip '##' */
2647 macro_ptr
= macro_ptr1
;
2652 tok_str_add2(¯o_str1
, tok
, &tokc
);
2654 tok_str_add(¯o_str1
, 0);
2655 return macro_str1
.str
;
2658 /* do macro substitution of macro_str and add result to
2659 (tok_str,tok_len). If macro_str is NULL, then input stream token is
2660 substituted. 'nested_list' is the list of all macros we got inside
2661 to avoid recursing. */
2662 void macro_subst(TokenString
*tok_str
,
2663 Sym
**nested_list
, int *macro_str
)
2665 Sym
*s
, *args
, *sa
, *sa1
;
2666 int parlevel
, *mstr
, t
, *saved_macro_ptr
;
2667 int mstr_allocated
, *macro_str1
;
2673 saved_macro_ptr
= macro_ptr
;
2674 macro_ptr
= macro_str
;
2677 /* first scan for '##' operator handling */
2678 macro_str1
= macro_twosharps(macro_str
);
2679 macro_ptr
= macro_str1
;
2686 if ((s
= sym_find1(&define_stack
, tok
)) != NULL
) {
2687 /* if symbol is a macro, prepare substitution */
2688 /* if nested substitution, do nothing */
2689 if (sym_find2(*nested_list
, tok
))
2692 /* special macros */
2693 if (tok
== TOK___LINE__
) {
2694 cval
.i
= file
->line_num
;
2695 tok_str_add2(tok_str
, TOK_CINT
, &cval
);
2696 } else if (tok
== TOK___FILE__
) {
2697 cstrval
= file
->filename
;
2699 tok_str_add2(tok_str
, TOK_STR
, &cval
);
2700 } else if (tok
== TOK___DATE__
) {
2701 cstrval
= "Jan 1 2002";
2703 } else if (tok
== TOK___TIME__
) {
2704 cstrval
= "00:00:00";
2707 cstr_cat(&cstr
, cstrval
);
2708 cstr_ccat(&cstr
, '\0');
2710 tok_str_add2(tok_str
, TOK_STR
, &cval
);
2715 if (s
->t
== MACRO_FUNC
) {
2716 /* NOTE: we do not use next_nomacro to avoid eating the
2717 next token. XXX: find better solution */
2721 while (is_space(ch
) || ch
== '\n')
2725 if (t
!= '(') /* no macro subst */
2728 /* argument macro */
2733 /* NOTE: empty args are allowed, except if no args */
2735 /* handle '()' case */
2736 if (!args
&& tok
== ')')
2739 error("macro '%s' used with too many args",
2740 get_tok_str(s
->v
, 0));
2743 /* NOTE: non zero sa->t indicates VA_ARGS */
2744 while ((parlevel
> 0 ||
2746 (tok
!= ',' || sa
->t
))) &&
2750 else if (tok
== ')')
2752 tok_str_add2(&str
, tok
, &tokc
);
2755 tok_str_add(&str
, 0);
2756 sym_push2(&args
, sa
->v
& ~SYM_FIELD
, sa
->t
, (int)str
.str
);
2759 /* special case for gcc var args: add an empty
2760 var arg argument if it is omitted */
2761 if (sa
&& sa
->t
&& gnu_ext
)
2771 error("macro '%s' used with too few args",
2772 get_tok_str(s
->v
, 0));
2775 /* now subst each arg */
2776 mstr
= macro_arg_subst(nested_list
, mstr
, args
);
2781 tok_str_free((int *)sa
->c
);
2787 sym_push2(nested_list
, s
->v
, 0, 0);
2788 macro_subst(tok_str
, nested_list
, mstr
);
2789 /* pop nested defined symbol */
2791 *nested_list
= sa1
->prev
;
2798 /* no need to add if reading input stream */
2801 tok_str_add2(tok_str
, tok
, &tokc
);
2803 /* only replace one macro while parsing input stream */
2807 macro_ptr
= saved_macro_ptr
;
2809 tok_str_free(macro_str1
);
2812 /* return next token with macro substitution */
2818 /* special 'ungettok' case for label parsing */
2826 /* if not reading from macro substituted string, then try
2828 /* XXX: optimize non macro case */
2831 macro_subst(&str
, &nested_list
, NULL
);
2833 tok_str_add(&str
, 0);
2834 macro_ptr
= str
.str
;
2835 macro_ptr_allocated
= str
.str
;
2843 /* end of macro string: free it */
2844 tok_str_free(macro_ptr_allocated
);
2851 printf("token = %s\n", get_tok_str(tok
, &tokc
));
2855 void swap(int *p
, int *q
)
2863 void vsetc(int t
, int r
, CValue
*vc
)
2867 if (vtop
>= vstack
+ VSTACK_SIZE
)
2868 error("memory full");
2869 /* cannot let cpu flags if other instruction are generated. Also
2870 avoid leaving VT_JMP anywhere except on the top of the stack
2871 because it would complicate the code generator. */
2872 if (vtop
>= vstack
) {
2873 v
= vtop
->r
& VT_VALMASK
;
2874 if (v
== VT_CMP
|| (v
& ~1) == VT_JMP
)
2880 vtop
->r2
= VT_CONST
;
2884 /* push integer constant */
2889 vsetc(VT_INT
, VT_CONST
, &cval
);
2892 /* Return a static symbol pointing to a section */
2893 static Sym
*get_sym_ref(int t
, Section
*sec
,
2894 unsigned long offset
, unsigned long size
)
2900 sym
= sym_push1(&global_stack
, v
, t
| VT_STATIC
, 0);
2901 sym
->r
= VT_CONST
| VT_SYM
;
2902 put_extern_sym(sym
, sec
, offset
, size
);
2906 /* push a reference to a section offset by adding a dummy symbol */
2907 static void vpush_ref(int t
, Section
*sec
, unsigned long offset
, unsigned long size
)
2912 vsetc(t
, VT_CONST
| VT_SYM
, &cval
);
2913 vtop
->sym
= get_sym_ref(t
, sec
, offset
, size
);
2916 /* define a new external reference to a symbol 'v' of type 'u' */
2917 static Sym
*external_global_sym(int v
, int u
, int r
)
2923 /* push forward reference */
2924 s
= sym_push1(&global_stack
,
2925 v
, u
| VT_EXTERN
, 0);
2926 s
->r
= r
| VT_CONST
| VT_SYM
;
2931 /* define a new external reference to a symbol 'v' of type 'u' */
2932 static Sym
*external_sym(int v
, int u
, int r
)
2938 /* push forward reference */
2939 s
= sym_push(v
, u
| VT_EXTERN
, r
| VT_CONST
| VT_SYM
, 0);
2944 /* push a reference to global symbol v */
2945 static void vpush_global_sym(int t
, int v
)
2950 sym
= external_global_sym(v
, t
, 0);
2952 vsetc(t
, VT_CONST
| VT_SYM
, &cval
);
2956 void vset(int t
, int r
, int v
)
2973 void vpushv(SValue
*v
)
2975 if (vtop
>= vstack
+ VSTACK_SIZE
)
2976 error("memory full");
2986 /* save r to the memory stack, and mark it as being free */
2987 void save_reg(int r
)
2989 int l
, saved
, t
, size
, align
;
2992 /* modify all stack values */
2995 for(p
=vstack
;p
<=vtop
;p
++) {
2996 if ((p
->r
& VT_VALMASK
) == r
||
2997 (p
->r2
& VT_VALMASK
) == r
) {
2998 /* must save value on stack if not already done */
3000 /* store register in the stack */
3002 if ((p
->r
& VT_LVAL
) ||
3003 (!is_float(t
) && (t
& VT_BTYPE
) != VT_LLONG
))
3005 size
= type_size(t
, &align
);
3006 loc
= (loc
- size
) & -align
;
3008 sv
.r
= VT_LOCAL
| VT_LVAL
;
3011 #ifdef TCC_TARGET_I386
3012 /* x86 specific: need to pop fp register ST0 if saved */
3014 o(0xd9dd); /* fstp %st(1) */
3017 /* special long long case */
3018 if ((t
& VT_BTYPE
) == VT_LLONG
) {
3025 /* mark that stack entry as being saved on the stack */
3026 if (p
->r
& VT_LVAL
) {
3027 /* also suppress the bounded flag because the
3028 relocation address of the function was stored in
3030 p
->r
= (p
->r
& ~(VT_VALMASK
| VT_BOUNDED
)) | VT_LLOCAL
;
3032 p
->r
= lvalue_type(p
->t
) | VT_LOCAL
;
3040 /* find a free register of class 'rc'. If none, save one register */
3046 /* find a free register */
3047 for(r
=0;r
<NB_REGS
;r
++) {
3048 if (reg_classes
[r
] & rc
) {
3049 for(p
=vstack
;p
<=vtop
;p
++) {
3050 if ((p
->r
& VT_VALMASK
) == r
||
3051 (p
->r2
& VT_VALMASK
) == r
)
3059 /* no register left : free the first one on the stack (VERY
3060 IMPORTANT to start from the bottom to ensure that we don't
3061 spill registers used in gen_opi()) */
3062 for(p
=vstack
;p
<=vtop
;p
++) {
3063 r
= p
->r
& VT_VALMASK
;
3064 if (r
< VT_CONST
&& (reg_classes
[r
] & rc
)) {
3072 /* save registers up to (vtop - n) stack entry */
3073 void save_regs(int n
)
3078 for(p
= vstack
;p
<= p1
; p
++) {
3079 r
= p
->r
& VT_VALMASK
;
3086 /* move register 's' to 'r', and flush previous value of r to memory
3088 void move_reg(int r
, int s
)
3101 /* get address of vtop (vtop MUST BE an lvalue) */
3104 vtop
->r
&= ~VT_LVAL
;
3105 /* tricky: if saved lvalue, then we can go back to lvalue */
3106 if ((vtop
->r
& VT_VALMASK
) == VT_LLOCAL
)
3107 vtop
->r
= (vtop
->r
& ~(VT_VALMASK
| VT_LVAL_TYPE
)) | VT_LOCAL
| VT_LVAL
;
3110 #ifdef CONFIG_TCC_BCHECK
3111 /* generate lvalue bound code */
3116 vtop
->r
&= ~VT_MUSTBOUND
;
3117 /* if lvalue, then use checking code before dereferencing */
3118 if (vtop
->r
& VT_LVAL
) {
3119 /* if not VT_BOUNDED value, then make one */
3120 if (!(vtop
->r
& VT_BOUNDED
)) {
3121 lval_type
= vtop
->r
& (VT_LVAL_TYPE
| VT_LVAL
);
3122 /* must save type because we must set it to int to get pointer */
3127 gen_bounded_ptr_add();
3128 vtop
->r
|= lval_type
;
3131 /* then check for dereferencing */
3132 gen_bounded_ptr_deref();
3137 /* store vtop a register belonging to class 'rc'. lvalues are
3138 converted to values. Cannot be used if cannot be converted to
3139 register value (such as structures). */
3142 int r
, r2
, rc2
, bit_pos
, bit_size
, size
, align
, i
;
3143 unsigned long long ll
;
3145 /* NOTE: get_reg can modify vstack[] */
3146 if (vtop
->t
& VT_BITFIELD
) {
3147 bit_pos
= (vtop
->t
>> VT_STRUCT_SHIFT
) & 0x3f;
3148 bit_size
= (vtop
->t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
3149 /* remove bit field info to avoid loops */
3150 vtop
->t
&= ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
3151 /* generate shifts */
3152 vpushi(32 - (bit_pos
+ bit_size
));
3154 vpushi(32 - bit_size
);
3155 /* NOTE: transformed to SHR if unsigned */
3159 if (is_float(vtop
->t
) &&
3160 (vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
3163 unsigned long offset
;
3165 /* XXX: unify with initializers handling ? */
3166 /* CPUs usually cannot use float constants, so we store them
3167 generically in data segment */
3168 size
= type_size(vtop
->t
, &align
);
3169 offset
= (data_section
->data_offset
+ align
- 1) & -align
;
3170 data_section
->data_offset
= offset
;
3171 /* XXX: not portable yet */
3172 ptr
= section_ptr_add(data_section
, size
);
3175 ptr
[i
] = vtop
->c
.tab
[i
];
3176 sym
= get_sym_ref(vtop
->t
, data_section
, offset
, size
<< 2);
3177 vtop
->r
|= VT_LVAL
| VT_SYM
;
3181 #ifdef CONFIG_TCC_BCHECK
3182 if (vtop
->r
& VT_MUSTBOUND
)
3186 r
= vtop
->r
& VT_VALMASK
;
3187 /* need to reload if:
3189 - lvalue (need to dereference pointer)
3190 - already a register, but not in the right class */
3191 if (r
>= VT_CONST
||
3192 (vtop
->r
& VT_LVAL
) ||
3193 !(reg_classes
[r
] & rc
) ||
3194 ((vtop
->t
& VT_BTYPE
) == VT_LLONG
&&
3195 !(reg_classes
[vtop
->r2
] & rc
))) {
3197 if ((vtop
->t
& VT_BTYPE
) == VT_LLONG
) {
3198 /* two register type load : expand to two words
3200 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) == VT_CONST
) {
3203 vtop
->c
.ui
= ll
; /* first word */
3205 vtop
->r
= r
; /* save register value */
3206 vpushi(ll
>> 32); /* second word */
3207 } else if (r
>= VT_CONST
||
3208 (vtop
->r
& VT_LVAL
)) {
3209 /* load from memory */
3212 vtop
[-1].r
= r
; /* save register value */
3213 /* increment pointer to get second word */
3220 /* move registers */
3223 vtop
[-1].r
= r
; /* save register value */
3224 vtop
->r
= vtop
[-1].r2
;
3226 /* allocate second register */
3233 /* write second register */
3235 } else if ((vtop
->r
& VT_LVAL
) && !is_float(vtop
->t
)) {
3237 /* lvalue of scalar type : need to use lvalue type
3238 because of possible cast */
3241 /* compute memory access type */
3242 if (vtop
->r
& VT_LVAL_BYTE
)
3244 else if (vtop
->r
& VT_LVAL_SHORT
)
3246 if (vtop
->r
& VT_LVAL_UNSIGNED
)
3250 /* restore wanted type */
3253 /* one register type load */
3262 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
3263 void gv2(int rc1
, int rc2
)
3267 /* generate more generic register first. But VT_JMP or VT_CMP
3268 values must be generated first in all cases to avoid possible
3270 v
= vtop
[0].r
& VT_VALMASK
;
3271 if (v
!= VT_CMP
&& (v
& ~1) != VT_JMP
&& rc1
<= rc2
) {
3276 /* test if reload is needed for first register */
3277 if ((vtop
[-1].r
& VT_VALMASK
) >= VT_CONST
) {
3287 /* test if reload is needed for first register */
3288 if ((vtop
[0].r
& VT_VALMASK
) >= VT_CONST
) {
3294 /* expand long long on stack in two int registers */
3299 u
= vtop
->t
& VT_UNSIGNED
;
3302 vtop
[0].r
= vtop
[-1].r2
;
3303 vtop
[0].r2
= VT_CONST
;
3304 vtop
[-1].r2
= VT_CONST
;
3305 vtop
[0].t
= VT_INT
| u
;
3306 vtop
[-1].t
= VT_INT
| u
;
3309 /* build a long long from two ints */
3312 gv2(RC_INT
, RC_INT
);
3313 vtop
[-1].r2
= vtop
[0].r
;
3318 /* rotate n first stack elements to the bottom */
3325 for(i
=-n
+1;i
!=0;i
++)
3326 vtop
[i
] = vtop
[i
+1];
3330 /* pop stack value */
3334 v
= vtop
->r
& VT_VALMASK
;
3335 #ifdef TCC_TARGET_I386
3336 /* for x86, we need to pop the FP stack */
3338 o(0xd9dd); /* fstp %st(1) */
3341 if (v
== VT_JMP
|| v
== VT_JMPI
) {
3342 /* need to put correct jump if && or || without test */
3348 /* convert stack entry to register and duplicate its value in another
3356 if ((t
& VT_BTYPE
) == VT_LLONG
) {
3363 /* stack: H L L1 H1 */
3371 /* duplicate value */
3382 load(r1
, &sv
); /* move r to r1 */
3384 /* duplicates value */
3389 /* generate CPU independent (unsigned) long long operations */
3390 void gen_opl(int op
)
3392 int t
, a
, b
, op1
, c
, i
;
3400 func
= TOK___divdi3
;
3403 func
= TOK___udivdi3
;
3406 func
= TOK___moddi3
;
3409 func
= TOK___umoddi3
;
3411 /* call generic long long function */
3412 gfunc_start(&gf
, FUNC_CDECL
);
3415 vpush_global_sym(func_old_type
, func
);
3419 vtop
->r2
= REG_LRET
;
3432 /* stack: L1 H1 L2 H2 */
3437 vtop
[-2] = vtop
[-3];
3440 /* stack: H1 H2 L1 L2 */
3446 /* stack: H1 H2 L1 L2 ML MH */
3449 /* stack: ML MH H1 H2 L1 L2 */
3453 /* stack: ML MH H1 L2 H2 L1 */
3458 /* stack: ML MH M1 M2 */
3461 } else if (op
== '+' || op
== '-') {
3462 /* XXX: add non carry method too (for MIPS or alpha) */
3468 /* stack: H1 H2 (L1 op L2) */
3471 gen_op(op1
+ 1); /* TOK_xxxC2 */
3474 /* stack: H1 H2 (L1 op L2) */
3477 /* stack: (L1 op L2) H1 H2 */
3479 /* stack: (L1 op L2) (H1 op H2) */
3487 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) {
3492 /* stack: L H shift */
3494 /* constant: simpler */
3495 /* NOTE: all comments are for SHL. the other cases are
3496 done by swaping words */
3507 if (op
!= TOK_SAR
) {
3537 /* XXX: should provide a faster fallback on x86 ? */
3540 func
= TOK___sardi3
;
3543 func
= TOK___shrdi3
;
3546 func
= TOK___shldi3
;
3552 /* compare operations */
3558 /* stack: L1 H1 L2 H2 */
3560 vtop
[-1] = vtop
[-2];
3562 /* stack: L1 L2 H1 H2 */
3565 /* when values are equal, we need to compare low words. since
3566 the jump is inverted, we invert the test too. */
3569 else if (op1
== TOK_GT
)
3571 else if (op1
== TOK_ULT
)
3573 else if (op1
== TOK_UGT
)
3578 if (op1
!= TOK_NE
) {
3582 /* generate non equal test */
3583 /* XXX: NOT PORTABLE yet */
3587 #ifdef TCC_TARGET_I386
3588 b
= psym(0x850f, 0);
3590 error("not implemented");
3598 vset(VT_INT
, VT_JMPI
, a
);
3603 /* handle integer constant optimizations and various machine
3605 void gen_opic(int op
)
3612 /* currently, we cannot do computations with forward symbols */
3613 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3614 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3618 case '+': v1
->c
.i
+= fc
; break;
3619 case '-': v1
->c
.i
-= fc
; break;
3620 case '&': v1
->c
.i
&= fc
; break;
3621 case '^': v1
->c
.i
^= fc
; break;
3622 case '|': v1
->c
.i
|= fc
; break;
3623 case '*': v1
->c
.i
*= fc
; break;
3630 /* if division by zero, generate explicit division */
3633 error("division by zero in constant");
3637 default: v1
->c
.i
/= fc
; break;
3638 case '%': v1
->c
.i
%= fc
; break;
3639 case TOK_UDIV
: v1
->c
.i
= (unsigned)v1
->c
.i
/ fc
; break;
3640 case TOK_UMOD
: v1
->c
.i
= (unsigned)v1
->c
.i
% fc
; break;
3643 case TOK_SHL
: v1
->c
.i
<<= fc
; break;
3644 case TOK_SHR
: v1
->c
.i
= (unsigned)v1
->c
.i
>> fc
; break;
3645 case TOK_SAR
: v1
->c
.i
>>= fc
; break;
3647 case TOK_ULT
: v1
->c
.i
= (unsigned)v1
->c
.i
< (unsigned)fc
; break;
3648 case TOK_UGE
: v1
->c
.i
= (unsigned)v1
->c
.i
>= (unsigned)fc
; break;
3649 case TOK_EQ
: v1
->c
.i
= v1
->c
.i
== fc
; break;
3650 case TOK_NE
: v1
->c
.i
= v1
->c
.i
!= fc
; break;
3651 case TOK_ULE
: v1
->c
.i
= (unsigned)v1
->c
.i
<= (unsigned)fc
; break;
3652 case TOK_UGT
: v1
->c
.i
= (unsigned)v1
->c
.i
> (unsigned)fc
; break;
3653 case TOK_LT
: v1
->c
.i
= v1
->c
.i
< fc
; break;
3654 case TOK_GE
: v1
->c
.i
= v1
->c
.i
>= fc
; break;
3655 case TOK_LE
: v1
->c
.i
= v1
->c
.i
<= fc
; break;
3656 case TOK_GT
: v1
->c
.i
= v1
->c
.i
> fc
; break;
3658 case TOK_LAND
: v1
->c
.i
= v1
->c
.i
&& fc
; break;
3659 case TOK_LOR
: v1
->c
.i
= v1
->c
.i
|| fc
; break;
3665 /* if commutative ops, put c2 as constant */
3666 if (c1
&& (op
== '+' || op
== '&' || op
== '^' ||
3667 op
== '|' || op
== '*')) {
3672 if (c2
&& (((op
== '*' || op
== '/' || op
== TOK_UDIV
||
3675 ((op
== '+' || op
== '-' || op
== '|' || op
== '^' ||
3676 op
== TOK_SHL
|| op
== TOK_SHR
|| op
== TOK_SAR
) &&
3682 } else if (c2
&& (op
== '*' || op
== TOK_PDIV
|| op
== TOK_UDIV
)) {
3683 /* try to use shifts instead of muls or divs */
3684 if (fc
> 0 && (fc
& (fc
- 1)) == 0) {
3693 else if (op
== TOK_PDIV
)
3699 } else if (c2
&& (op
== '+' || op
== '-') &&
3700 (vtop
[-1].r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) ==
3701 (VT_CONST
| VT_SYM
)) {
3702 /* symbol + constant case */
3709 /* call low level op generator */
3715 /* generate a floating point operation with constant propagation */
3716 void gen_opif(int op
)
3724 /* currently, we cannot do computations with forward symbols */
3725 c1
= (v1
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3726 c2
= (v2
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
3728 if (v1
->t
== VT_FLOAT
) {
3731 } else if (v1
->t
== VT_DOUBLE
) {
3739 /* NOTE: we only do constant propagation if finite number (not
3740 NaN or infinity) (ANSI spec) */
3741 if (!ieee_finite(f1
) || !ieee_finite(f2
))
3745 case '+': f1
+= f2
; break;
3746 case '-': f1
-= f2
; break;
3747 case '*': f1
*= f2
; break;
3751 error("division by zero in constant");
3756 /* XXX: also handles tests ? */
3760 /* XXX: overflow test ? */
3761 if (v1
->t
== VT_FLOAT
) {
3763 } else if (v1
->t
== VT_DOUBLE
) {
3775 int pointed_size(int t
)
3777 return type_size(pointed_type(t
), &t
);
3781 void check_pointer_types(SValue
*p1
, SValue
*p2
)
3783 char buf1
[256], buf2
[256];
3787 if (!is_compatible_types(t1
, t2
)) {
3788 type_to_str(buf1
, sizeof(buf1
), t1
, NULL
);
3789 type_to_str(buf2
, sizeof(buf2
), t2
, NULL
);
3790 error("incompatible pointers '%s' and '%s'", buf1
, buf2
);
3795 /* generic gen_op: handles types problems */
3798 int u
, t1
, t2
, bt1
, bt2
, t
;
3802 bt1
= t1
& VT_BTYPE
;
3803 bt2
= t2
& VT_BTYPE
;
3805 if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
3806 /* at least one operand is a pointer */
3807 /* relationnal op: must be both pointers */
3808 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
3809 // check_pointer_types(vtop, vtop - 1);
3810 /* pointers are handled are unsigned */
3811 t
= VT_INT
| VT_UNSIGNED
;
3814 /* if both pointers, then it must be the '-' op */
3815 if ((t1
& VT_BTYPE
) == VT_PTR
&&
3816 (t2
& VT_BTYPE
) == VT_PTR
) {
3818 error("cannot use pointers here");
3819 // check_pointer_types(vtop - 1, vtop);
3820 /* XXX: check that types are compatible */
3821 u
= pointed_size(t1
);
3823 /* set to integer type */
3828 /* exactly one pointer : must be '+' or '-'. */
3829 if (op
!= '-' && op
!= '+')
3830 error("cannot use pointers here");
3831 /* Put pointer as first operand */
3832 if ((t2
& VT_BTYPE
) == VT_PTR
) {
3836 /* XXX: cast to int ? (long long case) */
3837 vpushi(pointed_size(vtop
[-1].t
));
3839 #ifdef CONFIG_TCC_BCHECK
3840 /* if evaluating constant expression, no code should be
3841 generated, so no bound check */
3842 if (do_bounds_check
&& !const_wanted
) {
3843 /* if bounded pointers, we generate a special code to
3850 gen_bounded_ptr_add();
3856 /* put again type if gen_opic() swaped operands */
3859 } else if (is_float(bt1
) || is_float(bt2
)) {
3860 /* compute bigger type and do implicit casts */
3861 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
3863 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
3868 /* floats can only be used for a few operations */
3869 if (op
!= '+' && op
!= '-' && op
!= '*' && op
!= '/' &&
3870 (op
< TOK_ULT
|| op
> TOK_GT
))
3871 error("invalid operands for binary operation");
3873 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
3874 /* cast to biggest op */
3876 /* convert to unsigned if it does not fit in a long long */
3877 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
3878 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
3882 /* integer operations */
3884 /* convert to unsigned if it does not fit in an integer */
3885 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
3886 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
3889 /* XXX: currently, some unsigned operations are explicit, so
3890 we modify them here */
3891 if (t
& VT_UNSIGNED
) {
3898 else if (op
== TOK_LT
)
3900 else if (op
== TOK_GT
)
3902 else if (op
== TOK_LE
)
3904 else if (op
== TOK_GE
)
3910 /* special case for shifts and long long: we keep the shift as
3912 if (op
== TOK_SHR
|| op
== TOK_SAR
|| op
== TOK_SHL
)
3918 else if ((t
& VT_BTYPE
) == VT_LLONG
)
3922 if (op
>= TOK_ULT
&& op
<= TOK_GT
) {
3923 /* relationnal op: the result is an int */
3931 /* generic itof for unsigned long long case */
3932 void gen_cvt_itof1(int t
)
3936 if ((vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
)) ==
3937 (VT_LLONG
| VT_UNSIGNED
)) {
3939 gfunc_start(&gf
, FUNC_CDECL
);
3942 vpush_global_sym(func_old_type
, TOK___ulltof
);
3943 else if (t
== VT_DOUBLE
)
3944 vpush_global_sym(func_old_type
, TOK___ulltod
);
3946 vpush_global_sym(func_old_type
, TOK___ulltold
);
3955 /* generic ftoi for unsigned long long case */
3956 void gen_cvt_ftoi1(int t
)
3961 if (t
== (VT_LLONG
| VT_UNSIGNED
)) {
3962 /* not handled natively */
3963 gfunc_start(&gf
, FUNC_CDECL
);
3964 st
= vtop
->t
& VT_BTYPE
;
3967 vpush_global_sym(func_old_type
, TOK___fixunssfdi
);
3968 else if (st
== VT_DOUBLE
)
3969 vpush_global_sym(func_old_type
, TOK___fixunsdfdi
);
3971 vpush_global_sym(func_old_type
, TOK___fixunsxfdi
);
3975 vtop
->r2
= REG_LRET
;
3981 /* force char or short cast */
3982 void force_charshort_cast(int t
)
3986 /* XXX: add optimization if lvalue : just change type and offset */
3991 if (t
& VT_UNSIGNED
) {
3992 vpushi((1 << bits
) - 1);
4003 /* cast 'vtop' to 't' type */
4004 void gen_cast(int t
)
4006 int sbt
, dbt
, sf
, df
, c
;
4008 /* special delayed cast for char/short */
4009 /* XXX: in some cases (multiple cascaded casts), it may still
4011 if (vtop
->r
& VT_MUSTCAST
) {
4012 vtop
->r
&= ~VT_MUSTCAST
;
4013 force_charshort_cast(vtop
->t
);
4016 dbt
= t
& (VT_BTYPE
| VT_UNSIGNED
);
4017 sbt
= vtop
->t
& (VT_BTYPE
| VT_UNSIGNED
);
4022 c
= (vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
;
4024 /* convert from fp to fp */
4026 /* constant case: we can do it now */
4027 /* XXX: in ISOC, cannot do it if error in convert */
4028 if (dbt
== VT_FLOAT
&& sbt
== VT_DOUBLE
)
4029 vtop
->c
.f
= (float)vtop
->c
.d
;
4030 else if (dbt
== VT_FLOAT
&& sbt
== VT_LDOUBLE
)
4031 vtop
->c
.f
= (float)vtop
->c
.ld
;
4032 else if (dbt
== VT_DOUBLE
&& sbt
== VT_FLOAT
)
4033 vtop
->c
.d
= (double)vtop
->c
.f
;
4034 else if (dbt
== VT_DOUBLE
&& sbt
== VT_LDOUBLE
)
4035 vtop
->c
.d
= (double)vtop
->c
.ld
;
4036 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_FLOAT
)
4037 vtop
->c
.ld
= (long double)vtop
->c
.f
;
4038 else if (dbt
== VT_LDOUBLE
&& sbt
== VT_DOUBLE
)
4039 vtop
->c
.ld
= (long double)vtop
->c
.d
;
4041 /* non constant case: generate code */
4045 /* convert int to fp */
4048 case VT_LLONG
| VT_UNSIGNED
:
4050 /* XXX: add const cases for long long */
4052 case VT_INT
| VT_UNSIGNED
:
4054 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.ui
; break;
4055 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.ui
; break;
4056 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.ui
; break;
4061 case VT_FLOAT
: vtop
->c
.f
= (float)vtop
->c
.i
; break;
4062 case VT_DOUBLE
: vtop
->c
.d
= (double)vtop
->c
.i
; break;
4063 case VT_LDOUBLE
: vtop
->c
.ld
= (long double)vtop
->c
.i
; break;
4072 /* convert fp to int */
4073 /* we handle char/short/etc... with generic code */
4074 if (dbt
!= (VT_INT
| VT_UNSIGNED
) &&
4075 dbt
!= (VT_LLONG
| VT_UNSIGNED
) &&
4080 case VT_LLONG
| VT_UNSIGNED
:
4082 /* XXX: add const cases for long long */
4084 case VT_INT
| VT_UNSIGNED
:
4086 case VT_FLOAT
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
4087 case VT_DOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
4088 case VT_LDOUBLE
: vtop
->c
.ui
= (unsigned int)vtop
->c
.d
; break;
4094 case VT_FLOAT
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
4095 case VT_DOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
4096 case VT_LDOUBLE
: vtop
->c
.i
= (int)vtop
->c
.d
; break;
4104 if (dbt
== VT_INT
&& (t
& (VT_BTYPE
| VT_UNSIGNED
)) != dbt
) {
4105 /* additionnal cast for char/short/bool... */
4109 } else if ((dbt
& VT_BTYPE
) == VT_LLONG
) {
4110 if ((sbt
& VT_BTYPE
) != VT_LLONG
) {
4111 /* scalar to long long */
4113 if (sbt
== (VT_INT
| VT_UNSIGNED
))
4114 vtop
->c
.ll
= vtop
->c
.ui
;
4116 vtop
->c
.ll
= vtop
->c
.i
;
4118 /* machine independant conversion */
4120 /* generate high word */
4121 if (sbt
== (VT_INT
| VT_UNSIGNED
)) {
4129 /* patch second register */
4130 vtop
[-1].r2
= vtop
->r
;
4134 } else if (dbt
== VT_BOOL
) {
4135 /* scalar to bool */
4138 } else if ((dbt
& VT_BTYPE
) == VT_BYTE
||
4139 (dbt
& VT_BTYPE
) == VT_SHORT
) {
4140 force_charshort_cast(t
);
4141 } else if ((dbt
& VT_BTYPE
) == VT_INT
) {
4143 if (sbt
== VT_LLONG
) {
4144 /* from long long: just take low order word */
4148 /* if lvalue and single word type, nothing to do because
4149 the lvalue already contains the real type size (see
4150 VT_LVAL_xxx constants) */
4156 /* return type size. Put alignment at 'a' */
4157 int type_size(int t
, int *a
)
4163 if (bt
== VT_STRUCT
) {
4165 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
) | SYM_STRUCT
);
4166 *a
= 4; /* XXX: cannot store it yet. Doing that is safe */
4168 } else if (bt
== VT_PTR
) {
4170 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
));
4171 return type_size(s
->t
, a
) * s
->c
;
4176 } else if (bt
== VT_LDOUBLE
) {
4178 return LDOUBLE_SIZE
;
4179 } else if (bt
== VT_DOUBLE
|| bt
== VT_LLONG
) {
4182 } else if (bt
== VT_INT
|| bt
== VT_ENUM
|| bt
== VT_FLOAT
) {
4185 } else if (bt
== VT_SHORT
) {
4189 /* char, void, function, _Bool */
4195 /* return the pointed type of t */
4196 int pointed_type(int t
)
4199 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
));
4200 return s
->t
| (t
& ~VT_TYPE
);
4203 int mk_pointer(int t
)
4207 sym_push(p
, t
, 0, -1);
4208 return VT_PTR
| (p
<< VT_STRUCT_SHIFT
) | (t
& ~VT_TYPE
);
4211 int is_compatible_types(int t1
, int t2
)
4218 bt1
= t1
& VT_BTYPE
;
4219 bt2
= t2
& VT_BTYPE
;
4220 if (bt1
== VT_PTR
) {
4221 t1
= pointed_type(t1
);
4222 /* if function, then convert implicitely to function pointer */
4223 if (bt2
!= VT_FUNC
) {
4226 t2
= pointed_type(t2
);
4228 /* void matches everything */
4231 if (t1
== VT_VOID
|| t2
== VT_VOID
)
4233 return is_compatible_types(t1
, t2
);
4234 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
4236 } else if (bt1
== VT_FUNC
) {
4239 s1
= sym_find(((unsigned)t1
>> VT_STRUCT_SHIFT
));
4240 s2
= sym_find(((unsigned)t2
>> VT_STRUCT_SHIFT
));
4241 if (!is_compatible_types(s1
->t
, s2
->t
))
4243 /* XXX: not complete */
4244 if (s1
->c
== FUNC_OLD
|| s2
->c
== FUNC_OLD
)
4248 while (s1
!= NULL
) {
4251 if (!is_compatible_types(s1
->t
, s2
->t
))
4260 /* XXX: not complete */
4265 /* print a type. If 'varstr' is not NULL, then the variable is also
4266 printed in the type */
4268 /* XXX: add array and function pointers */
4269 void type_to_str(char *buf
, int buf_size
,
4270 int t
, const char *varstr
)
4280 if (t
& VT_UNSIGNED
)
4281 pstrcat(buf
, buf_size
, "unsigned ");
4311 tstr
= "long double";
4313 pstrcat(buf
, buf_size
, tstr
);
4317 if (bt
== VT_STRUCT
)
4321 pstrcat(buf
, buf_size
, tstr
);
4322 v
= (unsigned)t
>> VT_STRUCT_SHIFT
;
4323 if (v
>= SYM_FIRST_ANOM
)
4324 pstrcat(buf
, buf_size
, "<anonymous>");
4326 pstrcat(buf
, buf_size
, get_tok_str(v
, NULL
));
4329 s
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
4330 type_to_str(buf
, buf_size
, s
->t
, varstr
);
4331 pstrcat(buf
, buf_size
, "(");
4333 while (sa
!= NULL
) {
4334 type_to_str(buf1
, sizeof(buf1
), sa
->t
, NULL
);
4335 pstrcat(buf
, buf_size
, buf1
);
4338 pstrcat(buf
, buf_size
, ", ");
4340 pstrcat(buf
, buf_size
, ")");
4343 s
= sym_find((unsigned)t
>> VT_STRUCT_SHIFT
);
4344 pstrcpy(buf1
, sizeof(buf1
), "*");
4346 pstrcat(buf1
, sizeof(buf1
), varstr
);
4347 type_to_str(buf
, buf_size
, s
->t
, buf1
);
4351 pstrcat(buf
, buf_size
, " ");
4352 pstrcat(buf
, buf_size
, varstr
);
4357 /* verify type compatibility to store vtop in 'dt' type, and generate
4359 void gen_assign_cast(int dt
)
4362 char buf1
[256], buf2
[256];
4364 st
= vtop
->t
; /* source type */
4365 if ((dt
& VT_BTYPE
) == VT_PTR
) {
4366 /* special cases for pointers */
4367 /* a function is implicitely a function pointer */
4368 if ((st
& VT_BTYPE
) == VT_FUNC
) {
4369 if (!is_compatible_types(pointed_type(dt
), st
))
4374 /* '0' can also be a pointer */
4375 if ((st
& VT_BTYPE
) == VT_INT
&&
4376 ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
) &&
4380 if (!is_compatible_types(dt
, st
)) {
4382 type_to_str(buf1
, sizeof(buf1
), st
, NULL
);
4383 type_to_str(buf2
, sizeof(buf2
), dt
, NULL
);
4384 error("cannot cast '%s' to '%s'", buf1
, buf2
);
4390 /* store vtop in lvalue pushed on stack */
4393 int sbt
, dbt
, ft
, r
, t
, size
, align
, bit_size
, bit_pos
, rc
, delayed_cast
;
4397 sbt
= vtop
->t
& VT_BTYPE
;
4398 dbt
= ft
& VT_BTYPE
;
4399 if (((sbt
== VT_INT
|| sbt
== VT_SHORT
) && dbt
== VT_BYTE
) ||
4400 (sbt
== VT_INT
&& dbt
== VT_SHORT
)) {
4401 /* optimize char/short casts */
4402 delayed_cast
= VT_MUSTCAST
;
4403 vtop
->t
= ft
& VT_TYPE
;
4406 gen_assign_cast(ft
& VT_TYPE
);
4409 if (sbt
== VT_STRUCT
) {
4410 /* if structure, only generate pointer */
4411 /* structure assignment : generate memcpy */
4412 /* XXX: optimize if small size */
4414 gfunc_start(&gf
, FUNC_CDECL
);
4416 size
= type_size(vtop
->t
, &align
);
4430 vpush_global_sym(func_old_type
, TOK_memcpy
);
4432 /* leave source on stack */
4433 } else if (ft
& VT_BITFIELD
) {
4434 /* bitfield store handling */
4435 bit_pos
= (ft
>> VT_STRUCT_SHIFT
) & 0x3f;
4436 bit_size
= (ft
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f;
4437 /* remove bit field info to avoid loops */
4438 vtop
[-1].t
= ft
& ~(VT_BITFIELD
| (-1 << VT_STRUCT_SHIFT
));
4440 /* duplicate destination */
4442 vtop
[-1] = vtop
[-2];
4444 /* mask and shift source */
4445 vpushi((1 << bit_size
) - 1);
4449 /* load destination, mask and or with source */
4451 vpushi(~(((1 << bit_size
) - 1) << bit_pos
));
4457 #ifdef CONFIG_TCC_BCHECK
4458 /* bound check case */
4459 if (vtop
[-1].r
& VT_MUSTBOUND
) {
4468 r
= gv(rc
); /* generate value */
4469 /* if lvalue was saved on stack, must read it */
4470 if ((vtop
[-1].r
& VT_VALMASK
) == VT_LLOCAL
) {
4472 t
= get_reg(RC_INT
);
4474 sv
.r
= VT_LOCAL
| VT_LVAL
;
4475 sv
.c
.ul
= vtop
[-1].c
.ul
;
4477 vtop
[-1].r
= t
| VT_LVAL
;
4480 /* two word case handling : store second register at word + 4 */
4481 if ((ft
& VT_BTYPE
) == VT_LLONG
) {
4483 /* convert to int to increment easily */
4490 /* XXX: it works because r2 is spilled last ! */
4491 store(vtop
->r2
, vtop
- 1);
4494 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
4495 vtop
->r
|= delayed_cast
;
4499 /* post defines POST/PRE add. c is the token ++ or -- */
4500 void inc(int post
, int c
)
4503 vdup(); /* save lvalue */
4505 gv_dup(); /* duplicate value */
4510 vpushi(c
- TOK_MID
);
4512 vstore(); /* store value */
4514 vpop(); /* if post op, return saved value */
4517 /* Parse GNUC __attribute__ extension. Currently, the following
4518 extensions are recognized:
4519 - aligned(n) : set data/function alignment.
4520 - section(x) : generate data/code in this section.
4521 - unused : currently ignored, but may be used someday.
4523 void parse_attribute(AttributeDef
*ad
)
4530 while (tok
!= ')') {
4531 if (tok
< TOK_IDENT
)
4532 expect("attribute name");
4537 case TOK___SECTION__
:
4540 expect("section name");
4541 ad
->section
= find_section((char *)tokc
.cstr
->data
);
4546 case TOK___ALIGNED__
:
4549 if (n
<= 0 || (n
& (n
- 1)) != 0)
4550 error("alignment must be a positive power of two");
4555 case TOK___UNUSED__
:
4556 /* currently, no need to handle it because tcc does not
4557 track unused objects */
4560 case TOK___NORETURN__
:
4561 /* currently, no need to handle it because tcc does not
4562 track unused objects */
4567 ad
->func_call
= FUNC_CDECL
;
4571 case TOK___STDCALL__
:
4572 ad
->func_call
= FUNC_STDCALL
;
4575 warning("'%s' attribute ignored", get_tok_str(t
, NULL
));
4576 /* skip parameters */
4577 /* XXX: skip parenthesis too */
4580 while (tok
!= ')' && tok
!= -1)
4594 /* enum/struct/union declaration */
4595 int struct_decl(int u
)
4597 int a
, t
, b
, v
, size
, align
, maxalign
, c
, offset
;
4598 int bit_size
, bit_pos
, bsize
, bt
, lbit_pos
;
4602 a
= tok
; /* save decl type */
4607 /* struct already defined ? return it */
4608 /* XXX: check consistency */
4609 s
= sym_find(v
| SYM_STRUCT
);
4612 error("invalid type");
4618 s
= sym_push(v
| SYM_STRUCT
, a
, 0, 0);
4619 /* put struct/union/enum name in type */
4621 u
= u
| (v
<< VT_STRUCT_SHIFT
);
4626 error("struct/union/enum already defined");
4627 /* cannot be empty */
4634 if (a
== TOK_ENUM
) {
4641 /* enum symbols have static storage */
4642 sym_push(v
, VT_STATIC
| VT_INT
, VT_CONST
, c
);
4647 parse_btype(&b
, &ad
);
4652 t
= type_decl(&ad
, &v
, b
, TYPE_DIRECT
);
4653 if ((t
& VT_BTYPE
) == VT_FUNC
||
4654 (t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
)))
4655 error("invalid type for '%s'",
4656 get_tok_str(v
, NULL
));
4662 bit_size
= expr_const();
4663 /* XXX: handle v = 0 case for messages */
4665 error("negative width in bit-field '%s'",
4666 get_tok_str(v
, NULL
));
4667 if (v
&& bit_size
== 0)
4668 error("zero width for bit-field '%s'",
4669 get_tok_str(v
, NULL
));
4671 size
= type_size(t
, &align
);
4673 if (bit_size
>= 0) {
4678 error("bitfields must have scalar type");
4680 if (bit_size
> bsize
) {
4681 error("width of '%s' exceeds its type",
4682 get_tok_str(v
, NULL
));
4683 } else if (bit_size
== bsize
) {
4684 /* no need for bit fields */
4686 } else if (bit_size
== 0) {
4687 /* XXX: what to do if only padding in a
4689 /* zero size: means to pad */
4693 /* we do not have enough room ? */
4694 if ((bit_pos
+ bit_size
) > bsize
)
4697 /* XXX: handle LSB first */
4699 (bit_pos
<< VT_STRUCT_SHIFT
) |
4700 (bit_size
<< (VT_STRUCT_SHIFT
+ 6));
4701 bit_pos
+= bit_size
;
4707 /* add new memory data only if starting
4709 if (lbit_pos
== 0) {
4710 if (a
== TOK_STRUCT
) {
4711 c
= (c
+ align
- 1) & -align
;
4719 if (align
> maxalign
)
4723 printf("add field %s offset=%d",
4724 get_tok_str(v
, NULL
), offset
);
4725 if (t
& VT_BITFIELD
) {
4726 printf(" pos=%d size=%d",
4727 (t
>> VT_STRUCT_SHIFT
) & 0x3f,
4728 (t
>> (VT_STRUCT_SHIFT
+ 6)) & 0x3f);
4732 ss
= sym_push(v
| SYM_FIELD
, t
, 0, offset
);
4736 if (tok
== ';' || tok
== -1)
4746 /* size for struct/union, dummy for enum */
4747 s
->c
= (c
+ maxalign
- 1) & -maxalign
;
4752 /* return 0 if no type declaration. otherwise, return the basic type
4755 int parse_btype(int *type_ptr
, AttributeDef
*ad
)
4757 int t
, u
, type_found
;
4760 memset(ad
, 0, sizeof(AttributeDef
));
4771 if ((t
& VT_BTYPE
) != 0)
4772 error("too many basic types");
4786 if ((t
& VT_BTYPE
) == VT_DOUBLE
) {
4787 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
4788 } else if ((t
& VT_BTYPE
) == VT_LONG
) {
4789 t
= (t
& ~VT_BTYPE
) | VT_LLONG
;
4803 if ((t
& VT_BTYPE
) == VT_LONG
) {
4804 t
= (t
& ~VT_BTYPE
) | VT_LDOUBLE
;
4811 u
= struct_decl(VT_ENUM
);
4815 u
= struct_decl(VT_STRUCT
);
4818 /* type modifiers */
4823 case TOK___SIGNED__
:
4826 case TOK___INLINE__
:
4848 /* GNUC attribute */
4849 case TOK___ATTRIBUTE__
:
4850 parse_attribute(ad
);
4854 if (!s
|| !(s
->t
& VT_TYPEDEF
))
4856 t
|= (s
->t
& ~VT_TYPEDEF
);
4863 /* long is never used as type */
4864 if ((t
& VT_BTYPE
) == VT_LONG
)
4865 t
= (t
& ~VT_BTYPE
) | VT_INT
;
4870 int post_type(int t
, AttributeDef
*ad
)
4872 int p
, n
, pt
, l
, t1
;
4873 Sym
**plast
, *s
, *first
;
4877 /* function declaration */
4882 while (tok
!= ')') {
4883 /* read param name and compute offset */
4884 if (l
!= FUNC_OLD
) {
4885 if (!parse_btype(&pt
, &ad1
)) {
4887 error("invalid type");
4894 if ((pt
& VT_BTYPE
) == VT_VOID
&& tok
== ')')
4896 pt
= type_decl(&ad1
, &n
, pt
, TYPE_DIRECT
| TYPE_ABSTRACT
);
4897 if ((pt
& VT_BTYPE
) == VT_VOID
)
4898 error("parameter declared as void");
4905 /* array must be transformed to pointer according to ANSI C */
4907 s
= sym_push(n
| SYM_FIELD
, pt
, 0, 0);
4912 if (l
== FUNC_NEW
&& tok
== TOK_DOTS
) {
4919 /* if no parameters, then old type prototype */
4923 t1
= t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
);
4924 t
= post_type(t
& ~(VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
), ad
);
4925 /* we push a anonymous symbol which will contain the function prototype */
4927 s
= sym_push(p
, t
, ad
->func_call
, l
);
4929 t
= t1
| VT_FUNC
| (p
<< VT_STRUCT_SHIFT
);
4930 } else if (tok
== '[') {
4931 /* array definition */
4937 error("invalid array size");
4940 /* parse next post type */
4941 t1
= t
& (VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
);
4942 t
= post_type(t
& ~(VT_TYPEDEF
| VT_STATIC
| VT_EXTERN
), ad
);
4944 /* we push a anonymous symbol which will contain the array
4947 sym_push(p
, t
, 0, n
);
4948 t
= t1
| VT_ARRAY
| VT_PTR
| (p
<< VT_STRUCT_SHIFT
);
4953 /* Read a type declaration (except basic type), and return the
4954 type. 'td' is a bitmask indicating which kind of type decl is
4955 expected. 't' should contain the basic type. 'ad' is the attribute
4956 definition of the basic type. It can be modified by type_decl(). */
4957 int type_decl(AttributeDef
*ad
, int *v
, int t
, int td
)
4962 while (tok
== '*') {
4964 while (tok
== TOK_CONST
|| tok
== TOK_VOLATILE
|| tok
== TOK_RESTRICT
)
4969 /* recursive type */
4970 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
4973 /* XXX: this is not correct to modify 'ad' at this point, but
4974 the syntax is not clear */
4975 if (tok
== TOK___ATTRIBUTE__
)
4976 parse_attribute(ad
);
4977 u
= type_decl(ad
, v
, 0, td
);
4981 /* type identifier */
4982 if (tok
>= TOK_IDENT
&& (td
& TYPE_DIRECT
)) {
4986 if (!(td
& TYPE_ABSTRACT
))
4987 expect("identifier");
4991 /* append t at the end of u */
4992 t
= post_type(t
, ad
);
4993 if (tok
== TOK___ATTRIBUTE__
)
4994 parse_attribute(ad
);
4999 s
= sym_find((unsigned)p
>> VT_STRUCT_SHIFT
);
5009 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
5010 static int lvalue_type(int t
)
5017 else if (bt
== VT_SHORT
)
5021 if (t
& VT_UNSIGNED
)
5022 r
|= VT_LVAL_UNSIGNED
;
5026 /* indirection with full error checking and bound check */
5027 static void indir(void)
5029 if ((vtop
->t
& VT_BTYPE
) != VT_PTR
)
5031 if (vtop
->r
& VT_LVAL
)
5033 vtop
->t
= pointed_type(vtop
->t
);
5034 /* an array is never an lvalue */
5035 if (!(vtop
->t
& VT_ARRAY
)) {
5036 vtop
->r
|= lvalue_type(vtop
->t
);
5037 /* if bound checking, the referenced pointer must be checked */
5038 if (do_bounds_check
)
5039 vtop
->r
|= VT_MUSTBOUND
;
5043 /* pass a parameter to a function and do type checking and casting */
5044 void gfunc_param_typed(GFuncContext
*gf
, Sym
*func
, Sym
*arg
)
5047 func_type
= func
->c
;
5048 if (func_type
== FUNC_OLD
||
5049 (func_type
== FUNC_ELLIPSIS
&& arg
== NULL
)) {
5050 /* default casting : only need to convert float to double */
5051 if ((vtop
->t
& VT_BTYPE
) == VT_FLOAT
)
5052 gen_cast(VT_DOUBLE
);
5053 } else if (arg
== NULL
) {
5054 error("too many arguments to function");
5056 gen_assign_cast(arg
->t
);
5063 int n
, t
, ft
, align
, size
, r
;
5068 if (tok
== TOK_CINT
|| tok
== TOK_CCHAR
|| tok
== TOK_LCHAR
) {
5071 } else if (tok
== TOK_CUINT
) {
5072 vsetc(VT_INT
| VT_UNSIGNED
, VT_CONST
, &tokc
);
5074 } else if (tok
== TOK_CLLONG
) {
5075 vsetc(VT_LLONG
, VT_CONST
, &tokc
);
5077 } else if (tok
== TOK_CULLONG
) {
5078 vsetc(VT_LLONG
| VT_UNSIGNED
, VT_CONST
, &tokc
);
5080 } else if (tok
== TOK_CFLOAT
) {
5081 vsetc(VT_FLOAT
, VT_CONST
, &tokc
);
5083 } else if (tok
== TOK_CDOUBLE
) {
5084 vsetc(VT_DOUBLE
, VT_CONST
, &tokc
);
5086 } else if (tok
== TOK_CLDOUBLE
) {
5087 vsetc(VT_LDOUBLE
, VT_CONST
, &tokc
);
5089 } else if (tok
== TOK___FUNC__
|| (tok
== TOK___FUNCTION__
&& gnu_ext
)) {
5092 /* special function name identifier */
5094 len
= strlen(funcname
) + 1;
5095 /* generate char[len] type */
5096 t
= VT_ARRAY
| mk_pointer(VT_BYTE
);
5097 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
));
5099 vpush_ref(t
, data_section
, data_section
->data_offset
, len
);
5100 ptr
= section_ptr_add(data_section
, len
);
5101 memcpy(ptr
, funcname
, len
);
5103 } else if (tok
== TOK_LSTR
) {
5106 } else if (tok
== TOK_STR
) {
5107 /* string parsing */
5110 t
= VT_ARRAY
| mk_pointer(t
);
5111 memset(&ad
, 0, sizeof(AttributeDef
));
5112 decl_initializer_alloc(t
, &ad
, VT_CONST
, 2, 0, 0);
5118 if (parse_btype(&t
, &ad
)) {
5119 ft
= type_decl(&ad
, &n
, t
, TYPE_ABSTRACT
);
5121 /* check ISOC99 compound literal */
5123 /* data is allocated locally by default */
5128 /* all except arrays are lvalues */
5129 if (!(ft
& VT_ARRAY
))
5130 r
|= lvalue_type(ft
);
5131 memset(&ad
, 0, sizeof(AttributeDef
));
5132 decl_initializer_alloc(ft
, &ad
, r
, 1, 0, 0);
5141 } else if (t
== '*') {
5144 } else if (t
== '&') {
5146 /* functions names must be treated as function pointers,
5147 except for unary '&' and sizeof. Since we consider that
5148 functions are not lvalues, we only have to handle it
5149 there and in function calls. */
5150 /* arrays can also be used although they are not lvalues */
5151 if ((vtop
->t
& VT_BTYPE
) != VT_FUNC
&&
5152 !(vtop
->t
& VT_ARRAY
))
5154 vtop
->t
= mk_pointer(vtop
->t
);
5159 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) == VT_CONST
)
5160 vtop
->c
.i
= !vtop
->c
.i
;
5161 else if ((vtop
->r
& VT_VALMASK
) == VT_CMP
)
5162 vtop
->c
.i
= vtop
->c
.i
^ 1;
5164 vset(VT_INT
, VT_JMP
, gtst(1, 0));
5172 /* in order to force cast, we add zero */
5174 if ((vtop
->t
& VT_BTYPE
) == VT_PTR
)
5175 error("pointer not accepted for unary plus");
5179 if (t
== TOK_SIZEOF
) {
5182 if (parse_btype(&t
, &ad
)) {
5183 t
= type_decl(&ad
, &n
, t
, TYPE_ABSTRACT
);
5185 /* XXX: some code could be generated: add eval
5197 vpushi(type_size(t
, &t
));
5199 if (t
== TOK_INC
|| t
== TOK_DEC
) {
5202 } else if (t
== '-') {
5209 expect("identifier");
5213 error("'%s' undeclared", get_tok_str(t
, NULL
));
5214 /* for simple function calls, we tolerate undeclared
5215 external reference to int() function */
5216 s
= external_global_sym(t
, func_old_type
, 0);
5218 vset(s
->t
, s
->r
, s
->c
);
5219 /* if forward reference, we must point to s */
5220 if (vtop
->r
& VT_SYM
) {
5227 /* post operations */
5229 if (tok
== TOK_INC
|| tok
== TOK_DEC
) {
5232 } else if (tok
== '.' || tok
== TOK_ARROW
) {
5234 if (tok
== TOK_ARROW
)
5239 /* expect pointer on structure */
5240 if ((vtop
->t
& VT_BTYPE
) != VT_STRUCT
)
5241 expect("struct or union");
5242 s
= sym_find(((unsigned)vtop
->t
>> VT_STRUCT_SHIFT
) | SYM_STRUCT
);
5245 while ((s
= s
->next
) != NULL
) {
5250 error("field not found");
5251 /* add field offset to pointer */
5252 vtop
->t
= char_pointer_type
; /* change type to 'char *' */
5255 /* change type to field type, and set to lvalue */
5257 /* an array is never an lvalue */
5258 if (!(vtop
->t
& VT_ARRAY
)) {
5259 vtop
->r
|= lvalue_type(vtop
->t
);
5260 /* if bound checking, the referenced pointer must be checked */
5261 if (do_bounds_check
)
5262 vtop
->r
|= VT_MUSTBOUND
;
5265 } else if (tok
== '[') {
5271 } else if (tok
== '(') {
5276 if ((vtop
->t
& VT_BTYPE
) != VT_FUNC
) {
5277 /* pointer test (no array accepted) */
5278 if ((vtop
->t
& (VT_BTYPE
| VT_ARRAY
)) == VT_PTR
) {
5279 vtop
->t
= pointed_type(vtop
->t
);
5280 if ((vtop
->t
& VT_BTYPE
) != VT_FUNC
)
5284 expect("function pointer");
5287 vtop
->r
&= ~VT_LVAL
; /* no lvalue */
5289 /* get return type */
5290 s
= sym_find((unsigned)vtop
->t
>> VT_STRUCT_SHIFT
);
5291 save_regs(0); /* save used temporary registers */
5292 gfunc_start(&gf
, s
->r
);
5294 sa
= s
->next
; /* first parameter */
5295 #ifdef INVERT_FUNC_PARAMS
5299 ParseState saved_parse_state
;
5302 /* read each argument and store it on a stack */
5308 while ((parlevel
> 0 || (tok
!= ')' && tok
!= ',')) &&
5312 else if (tok
== ')')
5314 tok_str_add_tok(&str
);
5317 tok_str_add(&str
, -1); /* end of file added */
5318 tok_str_add(&str
, 0);
5319 s1
= sym_push2(&args
, 0, 0, (int)str
.str
);
5320 s1
->next
= sa
; /* add reference to argument */
5329 /* now generate code in reverse order by reading the stack */
5330 save_parse_state(&saved_parse_state
);
5332 macro_ptr
= (int *)args
->c
;
5336 expect("',' or ')'");
5337 gfunc_param_typed(&gf
, s
, args
->next
);
5339 tok_str_free((int *)args
->c
);
5343 restore_parse_state(&saved_parse_state
);
5346 /* compute first implicit argument if a structure is returned */
5347 if ((s
->t
& VT_BTYPE
) == VT_STRUCT
) {
5348 /* get some space for the returned structure */
5349 size
= type_size(s
->t
, &align
);
5350 loc
= (loc
- size
) & -align
;
5352 ret
.r
= VT_LOCAL
| VT_LVAL
;
5353 /* pass it as 'int' to avoid structure arg passing
5355 vset(VT_INT
, VT_LOCAL
, loc
);
5361 /* return in register */
5362 if (is_float(ret
.t
)) {
5365 if ((ret
.t
& VT_BTYPE
) == VT_LLONG
)
5371 #ifndef INVERT_FUNC_PARAMS
5375 gfunc_param_typed(&gf
, s
, sa
);
5385 error("too few arguments to function");
5389 vsetc(ret
.t
, ret
.r
, &ret
.c
);
5403 (tok
>= TOK_A_MOD
&& tok
<= TOK_A_DIV
) ||
5404 tok
== TOK_A_XOR
|| tok
== TOK_A_OR
||
5405 tok
== TOK_A_SHL
|| tok
== TOK_A_SAR
) {
5428 while ((l
== 0 && (tok
== '*' || tok
== '/' || tok
== '%')) ||
5429 (l
== 1 && (tok
== '+' || tok
== '-')) ||
5430 (l
== 2 && (tok
== TOK_SHL
|| tok
== TOK_SAR
)) ||
5431 (l
== 3 && ((tok
>= TOK_ULE
&& tok
<= TOK_GT
) ||
5432 tok
== TOK_ULT
|| tok
== TOK_UGE
)) ||
5433 (l
== 4 && (tok
== TOK_EQ
|| tok
== TOK_NE
)) ||
5434 (l
== 5 && tok
== '&') ||
5435 (l
== 6 && tok
== '^') ||
5436 (l
== 7 && tok
== '|') ||
5437 (l
== 8 && tok
== TOK_LAND
) ||
5438 (l
== 9 && tok
== TOK_LOR
)) {
5447 /* only used if non constant */
5455 if (tok
!= TOK_LAND
) {
5458 vset(VT_INT
, VT_JMPI
, t
);
5475 if (tok
!= TOK_LOR
) {
5478 vset(VT_INT
, VT_JMP
, t
);
5488 /* XXX: better constant handling */
5491 int tt
, u
, r1
, r2
, rc
, t1
, t2
, t
, bt1
, bt2
;
5513 save_regs(1); /* we need to save all registers here except
5514 at the top because it is a branch point */
5518 bt1
= t1
& VT_BTYPE
;
5519 sv
= *vtop
; /* save value to handle it later */
5520 vtop
--; /* no vpop so that FP stack is not flushed */
5528 bt2
= t2
& VT_BTYPE
;
5529 /* cast operands to correct type according to ISOC rules */
5530 if (is_float(bt1
) || is_float(bt2
)) {
5531 if (bt1
== VT_LDOUBLE
|| bt2
== VT_LDOUBLE
) {
5533 } else if (bt1
== VT_DOUBLE
|| bt2
== VT_DOUBLE
) {
5538 } else if (bt1
== VT_LLONG
|| bt2
== VT_LLONG
) {
5539 /* cast to biggest op */
5541 /* convert to unsigned if it does not fit in a long long */
5542 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
) ||
5543 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_LLONG
| VT_UNSIGNED
))
5545 } else if (bt1
== VT_PTR
|| bt2
== VT_PTR
) {
5546 /* XXX: test pointer compatibility */
5548 } else if (bt1
== VT_STRUCT
|| bt2
== VT_STRUCT
) {
5549 /* XXX: test structure compatibility */
5551 } else if (bt1
== VT_VOID
|| bt2
== VT_VOID
) {
5552 /* NOTE: as an extension, we accept void on only one side */
5555 /* integer operations */
5557 /* convert to unsigned if it does not fit in an integer */
5558 if ((t1
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
) ||
5559 (t2
& (VT_BTYPE
| VT_UNSIGNED
)) == (VT_INT
| VT_UNSIGNED
))
5563 /* now we convert second operand */
5568 } else if ((t
& VT_BTYPE
) == VT_LLONG
) {
5569 /* for long longs, we use fixed registers to avoid having
5570 to handle a complicated move */
5574 /* this is horrible, but we must also convert first
5578 /* put again first value and cast it */
5600 /* parse a constant expression and return value in vtop. */
5601 static void expr_const1(void)
5610 /* parse an integer constant and return its value. */
5611 static int expr_const(void)
5615 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
| VT_SYM
)) != VT_CONST
)
5616 expect("constant expression");
5622 /* return the label token if current token is a label, otherwise
5629 /* fast test first */
5630 if (tok
< TOK_UIDENT
)
5632 /* no need to save tokc since we expect an identifier */
5640 /* XXX: may not work in all cases (macros ?) */
5649 void block(int *bsym
, int *csym
, int *case_sym
, int *def_sym
, int case_reg
)
5654 /* generate line number info */
5656 (last_line_num
!= file
->line_num
|| last_ind
!= ind
)) {
5657 put_stabn(N_SLINE
, 0, file
->line_num
, ind
- func_ind
);
5659 last_line_num
= file
->line_num
;
5662 if (tok
== TOK_IF
) {
5669 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
5671 if (c
== TOK_ELSE
) {
5675 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
5676 gsym(d
); /* patch else jmp */
5679 } else if (tok
== TOK_WHILE
) {
5687 block(&a
, &b
, case_sym
, def_sym
, case_reg
);
5691 } else if (tok
== '{') {
5694 s
= local_stack
.top
;
5695 while (tok
!= '}') {
5698 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
5700 /* pop locally defined symbols */
5701 sym_pop(&local_stack
, s
);
5703 } else if (tok
== TOK_RETURN
) {
5707 gen_assign_cast(func_vt
);
5708 if ((func_vt
& VT_BTYPE
) == VT_STRUCT
) {
5709 /* if returning structure, must copy it to implicit
5710 first pointer arg location */
5711 vset(mk_pointer(func_vt
), VT_LOCAL
| VT_LVAL
, func_vc
);
5714 /* copy structure value to pointer */
5716 } else if (is_float(func_vt
)) {
5721 vtop
--; /* NOT vpop() because on x86 it would flush the fp stack */
5724 rsym
= gjmp(rsym
); /* jmp */
5725 } else if (tok
== TOK_BREAK
) {
5728 error("cannot break");
5729 *bsym
= gjmp(*bsym
);
5732 } else if (tok
== TOK_CONTINUE
) {
5735 error("cannot continue");
5736 *csym
= gjmp(*csym
);
5739 } else if (tok
== TOK_FOR
) {
5766 block(&a
, &b
, case_sym
, def_sym
, case_reg
);
5771 if (tok
== TOK_DO
) {
5776 block(&a
, &b
, case_sym
, def_sym
, case_reg
);
5787 if (tok
== TOK_SWITCH
) {
5791 /* XXX: other types than integer */
5792 case_reg
= gv(RC_INT
);
5796 b
= gjmp(0); /* jump to first case */
5798 block(&a
, csym
, &b
, &c
, case_reg
);
5799 /* if no default, jmp after switch */
5807 if (tok
== TOK_CASE
) {
5814 if (gnu_ext
&& tok
== TOK_DOTS
) {
5818 warning("empty case range");
5820 /* since a case is like a label, we must skip it with a jmp */
5823 vset(VT_INT
, case_reg
, 0);
5827 *case_sym
= gtst(1, 0);
5830 *case_sym
= gtst(1, 0);
5831 vset(VT_INT
, case_reg
, 0);
5834 *case_sym
= gtst(1, *case_sym
);
5838 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
5840 if (tok
== TOK_DEFAULT
) {
5846 error("too many 'default'");
5848 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
5850 if (tok
== TOK_GOTO
) {
5852 s
= sym_find1(&label_stack
, tok
);
5853 /* put forward definition if needed */
5855 s
= sym_push1(&label_stack
, tok
, LABEL_FORWARD
, 0);
5856 /* label already defined */
5857 if (s
->t
& LABEL_FORWARD
)
5867 s
= sym_find1(&label_stack
, b
);
5869 if (!(s
->t
& LABEL_FORWARD
))
5870 error("multiple defined label");
5875 sym_push1(&label_stack
, b
, 0, ind
);
5877 /* we accept this, but it is a mistake */
5879 warning("deprecated use of label at end of compound statement");
5881 block(bsym
, csym
, case_sym
, def_sym
, case_reg
);
5883 /* expression case */
5893 /* t is the array or struct type. c is the array or struct
5894 address. cur_index/cur_field is the pointer to the current
5895 value. 'size_only' is true if only size info is needed (only used
5897 static void decl_designator(int t
, Section
*sec
, unsigned long c
,
5898 int *cur_index
, Sym
**cur_field
,
5902 int notfirst
, index
, align
, l
;
5905 if (gnu_ext
&& (l
= is_label()) != 0)
5908 while (tok
== '[' || tok
== '.') {
5910 if (!(t
& VT_ARRAY
))
5911 expect("array type");
5912 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
));
5914 index
= expr_const();
5915 if (index
< 0 || (s
->c
>= 0 && index
>= s
->c
))
5916 expect("invalid index");
5920 t
= pointed_type(t
);
5921 c
+= index
* type_size(t
, &align
);
5927 if ((t
& VT_BTYPE
) != VT_STRUCT
)
5928 expect("struct/union type");
5929 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
) | SYM_STRUCT
);
5941 t
= f
->t
| (t
& ~VT_TYPE
);
5956 t
= pointed_type(t
);
5957 c
+= index
* type_size(t
, &align
);
5961 error("too many field init");
5962 t
= f
->t
| (t
& ~VT_TYPE
);
5966 decl_initializer(t
, sec
, c
, 0, size_only
);
5970 #define EXPR_CONST 1
5973 /* store a value or an expression directly in global data or in local array */
5974 static void init_putv(int t
, Section
*sec
, unsigned long c
,
5975 int v
, int expr_type
)
5977 int saved_global_expr
, bt
;
5985 /* compound literals must be allocated globally in this case */
5986 saved_global_expr
= global_expr
;
5989 global_expr
= saved_global_expr
;
5990 /* NOTE: symbols are accepted */
5991 if ((vtop
->r
& (VT_VALMASK
| VT_LVAL
)) != VT_CONST
)
5992 error("initializer element is not constant");
6000 /* XXX: not portable */
6001 /* XXX: generate error if incorrect relocation */
6004 ptr
= sec
->data
+ c
;
6005 if ((vtop
->r
& VT_SYM
) &&
6011 error("initializer element is not computable at load time");
6014 *(char *)ptr
= vtop
->c
.i
;
6017 *(short *)ptr
= vtop
->c
.i
;
6020 *(double *)ptr
= vtop
->c
.d
;
6023 *(long double *)ptr
= vtop
->c
.ld
;
6026 *(long long *)ptr
= vtop
->c
.ll
;
6029 if (vtop
->r
& VT_SYM
) {
6030 greloc(sec
, vtop
->sym
, c
, R_DATA_32
);
6032 *(int *)ptr
= vtop
->c
.i
;
6037 vset(t
, VT_LOCAL
, c
);
6044 /* put zeros for variable based init */
6045 static void init_putz(int t
, Section
*sec
, unsigned long c
, int size
)
6050 /* nothing to do because globals are already set to zero */
6052 gfunc_start(&gf
, FUNC_CDECL
);
6057 vset(VT_INT
, VT_LOCAL
, c
);
6059 vpush_global_sym(func_old_type
, TOK_memset
);
6064 /* 't' contains the type and storage info. 'c' is the offset of the
6065 object in section 'sec'. If 'sec' is NULL, it means stack based
6066 allocation. 'first' is true if array '{' must be read (multi
6067 dimension implicit array init handling). 'size_only' is true if
6068 size only evaluation is wanted (only for arrays). */
6069 static void decl_initializer(int t
, Section
*sec
, unsigned long c
,
6070 int first
, int size_only
)
6072 int index
, array_length
, n
, no_oblock
, nb
, parlevel
, i
;
6073 int t1
, size1
, align1
, expr_type
;
6077 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
));
6080 t1
= pointed_type(t
);
6081 size1
= type_size(t1
, &align1
);
6084 if ((first
&& tok
!= TOK_LSTR
&& tok
!= TOK_STR
) ||
6090 /* only parse strings here if correct type (otherwise: handle
6091 them as ((w)char *) expressions */
6092 if ((tok
== TOK_LSTR
&&
6093 (t1
& VT_BTYPE
) == VT_INT
) ||
6095 (t1
& VT_BTYPE
) == VT_BYTE
)) {
6096 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6101 /* compute maximum number of chars wanted */
6103 cstr_len
= cstr
->size
;
6105 cstr_len
= cstr
->size
/ sizeof(int);
6108 if (n
>= 0 && nb
> (n
- array_length
))
6109 nb
= n
- array_length
;
6112 warning("initializer-string for array is too long");
6115 ch
= ((unsigned char *)cstr
->data
)[i
];
6117 ch
= ((int *)cstr
->data
)[i
];
6118 init_putv(t1
, sec
, c
+ (array_length
+ i
) * size1
,
6125 /* only add trailing zero if enough storage (no
6126 warning in this case since it is standard) */
6127 if (n
< 0 || array_length
< n
) {
6129 init_putv(t1
, sec
, c
+ (array_length
* size1
), 0, EXPR_VAL
);
6135 while (tok
!= '}') {
6136 decl_designator(t
, sec
, c
, &index
, NULL
, size_only
);
6137 if (n
>= 0 && index
>= n
)
6138 error("index too large");
6139 /* must put zero in holes (note that doing it that way
6140 ensures that it even works with designators) */
6141 if (!size_only
&& array_length
< index
) {
6142 init_putz(t1
, sec
, c
+ array_length
* size1
,
6143 (index
- array_length
) * size1
);
6146 if (index
> array_length
)
6147 array_length
= index
;
6148 /* special test for multi dimensional arrays (may not
6149 be strictly correct if designators are used at the
6151 if (index
>= n
&& no_oblock
)
6160 /* put zeros at the end */
6161 if (!size_only
&& n
>= 0 && array_length
< n
) {
6162 init_putz(t1
, sec
, c
+ array_length
* size1
,
6163 (n
- array_length
) * size1
);
6165 /* patch type size if needed */
6167 s
->c
= array_length
;
6168 } else if ((t
& VT_BTYPE
) == VT_STRUCT
&& tok
== '{') {
6169 /* XXX: union needs only one init */
6171 s
= sym_find(((unsigned)t
>> VT_STRUCT_SHIFT
) | SYM_STRUCT
);
6176 while (tok
!= '}') {
6177 decl_designator(t
, sec
, c
, NULL
, &f
, size_only
);
6178 /* fill with zero between fields */
6180 if (!size_only
&& array_length
< index
) {
6181 init_putz(t
, sec
, c
+ array_length
,
6182 index
- array_length
);
6184 index
= index
+ type_size(f
->t
, &align1
);
6185 if (index
> array_length
)
6186 array_length
= index
;
6192 /* put zeros at the end */
6193 if (!size_only
&& array_length
< n
) {
6194 init_putz(t
, sec
, c
+ array_length
,
6198 } else if (tok
== '{') {
6200 decl_initializer(t
, sec
, c
, first
, size_only
);
6202 } else if (size_only
) {
6203 /* just skip expression */
6205 while ((parlevel
> 0 || (tok
!= '}' && tok
!= ',')) &&
6209 else if (tok
== ')')
6214 /* currently, we always use constant expression for globals
6215 (may change for scripting case) */
6216 expr_type
= EXPR_CONST
;
6218 expr_type
= EXPR_ANY
;
6219 init_putv(t
, sec
, c
, 0, expr_type
);
6223 /* parse an initializer for type 't' if 'has_init' is non zero, and
6224 allocate space in local or global data space ('r' is either
6225 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
6226 variable 'v' of scope 'scope' is declared before initializers are
6227 parsed. If 'v' is zero, then a reference to the new object is put
6228 in the value stack. If 'has_init' is 2, a special parsing is done
6229 to handle string constants. */
6230 static void decl_initializer_alloc(int t
, AttributeDef
*ad
, int r
,
6231 int has_init
, int v
, int scope
)
6233 int size
, align
, addr
, data_offset
;
6235 ParseState saved_parse_state
;
6236 TokenString init_str
;
6239 size
= type_size(t
, &align
);
6240 /* If unknown size, we must evaluate it before
6241 evaluating initializers because
6242 initializers can generate global data too
6243 (e.g. string pointers or ISOC99 compound
6244 literals). It also simplifies local
6245 initializers handling */
6246 tok_str_new(&init_str
);
6249 error("unknown type size");
6250 /* get all init string */
6251 if (has_init
== 2) {
6252 /* only get strings */
6253 while (tok
== TOK_STR
|| tok
== TOK_LSTR
) {
6254 tok_str_add_tok(&init_str
);
6259 while (level
> 0 || (tok
!= ',' && tok
!= ';')) {
6261 error("unexpected end of file in initializer");
6262 tok_str_add_tok(&init_str
);
6265 else if (tok
== '}') {
6273 tok_str_add(&init_str
, -1);
6274 tok_str_add(&init_str
, 0);
6277 save_parse_state(&saved_parse_state
);
6279 macro_ptr
= init_str
.str
;
6281 decl_initializer(t
, NULL
, 0, 1, 1);
6282 /* prepare second initializer parsing */
6283 macro_ptr
= init_str
.str
;
6286 /* if still unknown size, error */
6287 size
= type_size(t
, &align
);
6289 error("unknown type size");
6291 /* take into account specified alignment if bigger */
6292 if (ad
->aligned
> align
)
6293 align
= ad
->aligned
;
6294 if ((r
& VT_VALMASK
) == VT_LOCAL
) {
6296 if (do_bounds_check
&& (t
& VT_ARRAY
))
6298 #ifdef TCC_TARGET_IL
6299 /* XXX: ugly patch to allocate local variables for IL, just
6304 loc
= (loc
- size
) & -align
;
6307 /* handles bounds */
6308 /* XXX: currently, since we do only one pass, we cannot track
6309 '&' operators, so we add only arrays */
6310 if (do_bounds_check
&& (t
& VT_ARRAY
)) {
6311 unsigned long *bounds_ptr
;
6312 /* add padding between regions */
6314 /* then add local bound info */
6315 bounds_ptr
= section_ptr_add(lbounds_section
, 2 * sizeof(unsigned long));
6316 bounds_ptr
[0] = addr
;
6317 bounds_ptr
[1] = size
;
6320 /* compute section */
6328 data_offset
= sec
->data_offset
;
6329 data_offset
= (data_offset
+ align
- 1) & -align
;
6331 /* very important to increment global pointer at this time
6332 because initializers themselves can create new initializers */
6333 data_offset
+= size
;
6334 /* add padding if bound check */
6335 if (do_bounds_check
)
6337 sec
->data_offset
= data_offset
;
6338 /* allocate section space to put the data */
6339 if (sec
->sh_type
!= SHT_NOBITS
&&
6340 data_offset
> sec
->data_allocated
)
6341 section_realloc(sec
, data_offset
);
6345 /* local variable */
6346 sym_push(v
, t
, r
, addr
);
6348 /* push local reference */
6355 if (scope
== VT_CONST
) {
6356 /* global scope: see if already defined */
6360 if (!is_compatible_types(sym
->t
, t
))
6361 error("incompatible types for redefinition of '%s'",
6362 get_tok_str(v
, NULL
));
6363 if (!(sym
->t
& VT_EXTERN
))
6364 error("redefinition of '%s'", get_tok_str(v
, NULL
));
6365 sym
->t
&= ~VT_EXTERN
;
6368 sym
= sym_push(v
, t
, r
| VT_SYM
, 0);
6370 put_extern_sym(sym
, sec
, addr
, size
);
6374 /* push global reference */
6375 sym
= get_sym_ref(t
, sec
, addr
, size
);
6377 vsetc(t
, VT_CONST
| VT_SYM
, &cval
);
6381 /* handles bounds now because the symbol must be defined
6382 before for the relocation */
6383 if (do_bounds_check
) {
6384 unsigned long *bounds_ptr
;
6386 greloc(bounds_section
, sym
, bounds_section
->data_offset
, R_DATA_32
);
6387 /* then add global bound info */
6388 bounds_ptr
= section_ptr_add(bounds_section
, 2 * sizeof(long));
6389 bounds_ptr
[0] = 0; /* relocated */
6390 bounds_ptr
[1] = size
;
6394 decl_initializer(t
, sec
, addr
, 1, 0);
6395 /* restore parse state if needed */
6397 tok_str_free(init_str
.str
);
6398 restore_parse_state(&saved_parse_state
);
6403 void put_func_debug(Sym
*sym
)
6408 /* XXX: we put here a dummy type */
6409 snprintf(buf
, sizeof(buf
), "%s:%c1",
6410 funcname
, sym
->t
& VT_STATIC
? 'f' : 'F');
6411 put_stabs_r(buf
, N_FUN
, 0, file
->line_num
, 0,
6412 cur_text_section
, sym
->c
);
6417 /* not finished : try to put some local vars in registers */
6418 //#define CONFIG_REG_VARS
6420 #ifdef CONFIG_REG_VARS
6421 void add_var_ref(int t
)
6423 printf("%s:%d: &%s\n",
6424 file
->filename
, file
->line_num
,
6425 get_tok_str(t
, NULL
));
6428 /* first pass on a function with heuristic to extract variable usage
6429 and pointer references to local variables for register allocation */
6430 void analyse_function(void)
6437 /* any symbol coming after '&' is considered as being a
6438 variable whose reference is taken. It is highly unaccurate
6439 but it is difficult to do better without a complete parse */
6442 /* if '& number', then no need to examine next tokens */
6443 if (tok
== TOK_CINT
||
6445 tok
== TOK_CLLONG
||
6446 tok
== TOK_CULLONG
) {
6448 } else if (tok
>= TOK_UIDENT
) {
6449 /* if '& ident [' or '& ident ->', then ident address
6453 if (tok
!= '[' && tok
!= TOK_ARROW
)
6457 while (tok
!= '}' && tok
!= ';' &&
6458 !((tok
== ',' || tok
== ')') && level
== 0)) {
6459 if (tok
>= TOK_UIDENT
) {
6461 } else if (tok
== '(') {
6463 } else if (tok
== ')') {
6476 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6479 int t
, b
, v
, has_init
, r
;
6484 if (!parse_btype(&b
, &ad
)) {
6485 /* skip redundant ';' */
6486 /* XXX: find more elegant solution */
6491 /* special test for old K&R protos without explicit int
6492 type. Only accepted when defining global data */
6493 if (l
== VT_LOCAL
|| tok
< TOK_DEFINE
)
6497 if (((b
& VT_BTYPE
) == VT_ENUM
||
6498 (b
& VT_BTYPE
) == VT_STRUCT
) &&
6500 /* we accept no variable after */
6504 while (1) { /* iterate thru each declaration */
6505 t
= type_decl(&ad
, &v
, b
, TYPE_DIRECT
);
6509 type_to_str(buf
, sizeof(buf
), t
, get_tok_str(v
, NULL
));
6510 printf("type = '%s'\n", buf
);
6514 #ifdef CONFIG_REG_VARS
6515 TokenString func_str
;
6516 ParseState saved_parse_state
;
6521 error("cannot use local functions");
6523 expect("function definition");
6525 #ifdef CONFIG_REG_VARS
6526 /* parse all function code and record it */
6528 tok_str_new(&func_str
);
6534 error("unexpected end of file");
6535 tok_str_add_tok(&func_str
);
6540 } else if (t
== '}') {
6542 if (block_level
== 0)
6546 tok_str_add(&func_str
, -1);
6547 tok_str_add(&func_str
, 0);
6549 save_parse_state(&saved_parse_state
);
6551 macro_ptr
= func_str
.str
;
6556 /* compute text section */
6557 cur_text_section
= ad
.section
;
6558 if (!cur_text_section
)
6559 cur_text_section
= text_section
;
6560 ind
= cur_text_section
->data_offset
;
6561 funcname
= get_tok_str(v
, NULL
);
6564 /* if symbol is already defined, then put complete type */
6567 /* put function symbol */
6568 sym
= sym_push1(&global_stack
, v
, t
, 0);
6570 /* NOTE: we patch the symbol size later */
6571 put_extern_sym(sym
, cur_text_section
, ind
, 0);
6573 sym
->r
= VT_SYM
| VT_CONST
;
6574 /* put debug symbol */
6576 put_func_debug(sym
);
6577 /* push a dummy symbol to enable local sym storage */
6578 sym_push1(&local_stack
, 0, 0, 0);
6582 #ifdef CONFIG_REG_VARS
6583 macro_ptr
= func_str
.str
;
6586 block(NULL
, NULL
, NULL
, NULL
, 0);
6589 cur_text_section
->data_offset
= ind
;
6590 sym_pop(&label_stack
, NULL
); /* reset label stack */
6591 sym_pop(&local_stack
, NULL
); /* reset local stack */
6592 /* end of function */
6593 /* patch symbol size */
6594 ((Elf32_Sym
*)symtab_section
->data
)[sym
->c
].st_size
=
6597 put_stabn(N_FUN
, 0, 0, ind
- func_ind
);
6599 funcname
= ""; /* for safety */
6600 func_vt
= VT_VOID
; /* for safety */
6601 ind
= 0; /* for safety */
6603 #ifdef CONFIG_REG_VARS
6604 tok_str_free(func_str
.str
);
6605 restore_parse_state(&saved_parse_state
);
6609 if (b
& VT_TYPEDEF
) {
6610 /* save typedefed type */
6611 /* XXX: test storage specifiers ? */
6612 sym_push(v
, t
| VT_TYPEDEF
, 0, 0);
6613 } else if ((t
& VT_BTYPE
) == VT_FUNC
) {
6614 /* external function definition */
6615 external_sym(v
, t
, 0);
6617 /* not lvalue if array */
6619 if (!(t
& VT_ARRAY
))
6620 r
|= lvalue_type(t
);
6621 if (b
& VT_EXTERN
) {
6622 /* external variable */
6623 external_sym(v
, t
, r
);
6629 has_init
= (tok
== '=');
6632 decl_initializer_alloc(t
, &ad
, r
,
6646 /* compile the C file opened in 'file'. Return non zero if errors. */
6647 static int tcc_compile(TCCState
*s
)
6649 Sym
*define_start
, *sym
;
6654 include_stack_ptr
= include_stack
;
6655 ifdef_stack_ptr
= ifdef_stack
;
6657 /* XXX: not ANSI compliant: bound checking says error */
6659 anon_sym
= SYM_FIRST_ANOM
;
6661 /* file info: full path + filename */
6662 section_sym
= 0; /* avoid warning */
6664 section_sym
= put_elf_sym(symtab_section
, 0, 0,
6665 ELF32_ST_INFO(STB_LOCAL
, STT_SECTION
), 0,
6666 text_section
->sh_num
, NULL
);
6667 getcwd(buf
, sizeof(buf
));
6668 pstrcat(buf
, sizeof(buf
), "/");
6669 put_stabs_r(buf
, N_SO
, 0, 0,
6670 text_section
->data_offset
, text_section
, section_sym
);
6671 put_stabs_r(file
->filename
, N_SO
, 0, 0,
6672 text_section
->data_offset
, text_section
, section_sym
);
6674 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
6675 symbols can be safely used */
6676 put_elf_sym(symtab_section
, 0, 0,
6677 ELF32_ST_INFO(STB_LOCAL
, STT_FILE
), 0,
6678 SHN_ABS
, file
->filename
);
6680 /* define common 'char *' type because it is often used internally
6681 for arrays and struct dereference */
6682 char_pointer_type
= mk_pointer(VT_BYTE
);
6683 /* define an old type function 'int func()' */
6685 sym
= sym_push(p
, 0, FUNC_CDECL
, FUNC_OLD
);
6686 func_old_type
= VT_FUNC
| (p
<< VT_STRUCT_SHIFT
);
6688 /* define 'void *alloca(unsigned int)' builtin function */
6693 sym
= sym_push(p
, mk_pointer(VT_VOID
), FUNC_CDECL
, FUNC_NEW
);
6694 s1
= sym_push(0, VT_UNSIGNED
| VT_INT
, 0, 0);
6697 sym_push(TOK_alloca
, VT_FUNC
| (p
<< VT_STRUCT_SHIFT
), VT_CONST
, 0);
6701 define_start
= define_stack
.top
;
6703 ch
= '\n'; /* needed to parse correctly first preprocessor command */
6707 expect("declaration");
6709 /* end of translation unit info */
6711 put_stabs_r(NULL
, N_SO
, 0, 0,
6712 text_section
->data_offset
, text_section
, section_sym
);
6715 /* reset define stack, but leave -Dsymbols (may be incorrect if
6716 they are undefined) */
6717 sym_pop(&define_stack
, define_start
);
6719 sym_pop(&global_stack
, NULL
);
6724 int tcc_compile_string(TCCState
*s
, const char *str
)
6726 BufferedFile bf1
, *bf
= &bf1
;
6729 /* init file structure */
6731 bf
->buf_ptr
= (char *)str
;
6732 bf
->buf_end
= (char *)str
+ strlen(bf
->buffer
);
6733 pstrcpy(bf
->filename
, sizeof(bf
->filename
), "<string>");
6737 ret
= tcc_compile(s
);
6739 /* currently, no need to close */
6743 /* define a symbol. A value can also be provided with the '=' operator */
6744 void tcc_define_symbol(TCCState
*s
, const char *sym
, const char *value
)
6746 BufferedFile bf1
, *bf
= &bf1
;
6748 pstrcpy(bf
->buffer
, IO_BUF_SIZE
, sym
);
6749 pstrcat(bf
->buffer
, IO_BUF_SIZE
, " ");
6753 pstrcat(bf
->buffer
, IO_BUF_SIZE
, value
);
6755 /* init file structure */
6757 bf
->buf_ptr
= bf
->buffer
;
6758 bf
->buf_end
= bf
->buffer
+ strlen(bf
->buffer
);
6759 bf
->filename
[0] = '\0';
6763 include_stack_ptr
= include_stack
;
6765 /* parse with define parser */
6767 ch
= '\n'; /* needed to parse correctly first preprocessor command */
6773 void tcc_undefine_symbol(TCCState
*s1
, const char *sym
)
6777 ts
= tok_alloc(sym
, 0);
6778 s
= sym_find1(&define_stack
, tok
);
6779 /* undefine symbol by putting an invalid name */
6781 sym_undef(&define_stack
, s
);
6786 /* print the position in the source file of PC value 'pc' by reading
6787 the stabs debug information */
6788 static void rt_printline(unsigned long wanted_pc
)
6790 Stab_Sym
*sym
, *sym_end
;
6791 char func_name
[128], last_func_name
[128];
6792 unsigned long func_addr
, last_pc
, pc
;
6793 const char *incl_files
[INCLUDE_STACK_SIZE
];
6794 int incl_index
, len
, last_line_num
, i
;
6795 const char *str
, *p
;
6797 func_name
[0] = '\0';
6800 last_func_name
[0] = '\0';
6801 last_pc
= 0xffffffff;
6803 sym
= (Stab_Sym
*)stab_section
->data
+ 1;
6804 sym_end
= (Stab_Sym
*)(stab_section
->data
+ stab_section
->data_offset
);
6805 while (sym
< sym_end
) {
6806 switch(sym
->n_type
) {
6807 /* function start or end */
6809 if (sym
->n_strx
== 0) {
6810 func_name
[0] = '\0';
6813 str
= stabstr_section
->data
+ sym
->n_strx
;
6814 p
= strchr(str
, ':');
6816 pstrcpy(func_name
, sizeof(func_name
), str
);
6819 if (len
> sizeof(func_name
) - 1)
6820 len
= sizeof(func_name
) - 1;
6821 memcpy(func_name
, str
, len
);
6822 func_name
[len
] = '\0';
6824 func_addr
= sym
->n_value
;
6827 /* line number info */
6829 pc
= sym
->n_value
+ func_addr
;
6830 if (wanted_pc
>= last_pc
&& wanted_pc
< pc
)
6833 last_line_num
= sym
->n_desc
;
6835 strcpy(last_func_name
, func_name
);
6839 str
= stabstr_section
->data
+ sym
->n_strx
;
6841 if (incl_index
< INCLUDE_STACK_SIZE
) {
6842 incl_files
[incl_index
++] = str
;
6850 if (sym
->n_strx
== 0) {
6851 incl_index
= 0; /* end of translation unit */
6853 str
= stabstr_section
->data
+ sym
->n_strx
;
6854 /* do not add path */
6856 if (len
> 0 && str
[len
- 1] != '/')
6863 /* did not find line number info: */
6864 fprintf(stderr
, "(no debug info, pc=0x%08lx): ", wanted_pc
);
6867 for(i
= 0; i
< incl_index
- 1; i
++)
6868 fprintf(stderr
, "In file included from %s\n",
6870 if (incl_index
> 0) {
6871 fprintf(stderr
, "%s:%d: ",
6872 incl_files
[incl_index
- 1], last_line_num
);
6874 if (last_func_name
[0] != '\0') {
6875 fprintf(stderr
, "in function '%s()': ", last_func_name
);
6879 /* emit a run time error at position 'pc' */
6880 void rt_error(unsigned long pc
, const char *fmt
, ...)
6886 vfprintf(stderr
, fmt
, ap
);
6887 fprintf(stderr
, "\n");
6893 /* signal handler for fatal errors */
6894 static void sig_error(int signum
, siginfo_t
*siginf
, void *puc
)
6896 struct ucontext
*uc
= puc
;
6900 pc
= uc
->uc_mcontext
.gregs
[14];
6902 #error please put the right sigcontext field
6907 switch(siginf
->si_code
) {
6910 rt_error(pc
, "division by zero");
6913 rt_error(pc
, "floating point exception");
6919 rt_error(pc
, "dereferencing invalid pointer");
6922 rt_error(pc
, "illegal instruction");
6925 rt_error(pc
, "abort() called");
6928 rt_error(pc
, "caught signal %d", signum
);
6935 /* launch the compiled program with the given arguments */
6936 int tcc_run(TCCState
*s1
, int argc
, char **argv
)
6939 int (*prog_main
)(int, char **);
6942 tcc_add_runtime(s1
);
6944 relocate_common_syms();
6946 /* compute relocation address : section are relocated in place. We
6947 also alloc the bss space */
6948 for(i
= 1; i
< nb_sections
; i
++) {
6950 if (s
->sh_flags
& SHF_ALLOC
) {
6952 if (s
->sh_type
== SHT_NOBITS
) {
6953 data
= tcc_mallocz(s
->data_offset
);
6957 s
->sh_addr
= (unsigned long)data
;
6963 /* relocate each section */
6964 for(i
= 1; i
< nb_sections
; i
++) {
6967 relocate_section(s1
, s
);
6970 prog_main
= (void *)get_elf_sym_val("main");
6974 error("debug mode currently not available for Windows");
6976 struct sigaction sigact
;
6977 /* install TCC signal handlers to print debug info on fatal
6979 sigact
.sa_flags
= SA_SIGINFO
| SA_ONESHOT
;
6980 sigact
.sa_sigaction
= sig_error
;
6981 sigemptyset(&sigact
.sa_mask
);
6982 sigaction(SIGFPE
, &sigact
, NULL
);
6983 sigaction(SIGILL
, &sigact
, NULL
);
6984 sigaction(SIGSEGV
, &sigact
, NULL
);
6985 sigaction(SIGBUS
, &sigact
, NULL
);
6986 sigaction(SIGABRT
, &sigact
, NULL
);
6990 #ifdef CONFIG_TCC_BCHECK
6991 if (do_bounds_check
) {
6992 void (*bound_init
)(void);
6993 void **bound_error_func
;
6995 /* set error function */
6996 bound_error_func
= (void **)get_elf_sym_val("__bound_error_func");
6997 *bound_error_func
= rt_error
;
6999 /* XXX: use .init section so that it also work in binary ? */
7000 bound_init
= (void *)get_elf_sym_val("__bound_init");
7004 return (*prog_main
)(argc
, argv
);
7007 TCCState
*tcc_new(void)
7012 s
= tcc_malloc(sizeof(TCCState
));
7015 s
->output_type
= TCC_OUTPUT_MEMORY
;
7017 /* default include paths */
7018 tcc_add_sysinclude_path(s
, "/usr/local/include");
7019 tcc_add_sysinclude_path(s
, "/usr/include");
7020 tcc_add_sysinclude_path(s
, CONFIG_TCC_PREFIX
"/lib/tcc/include");
7022 /* add all tokens */
7023 tok_ident
= TOK_IDENT
;
7028 tok_alloc(p
, r
- p
- 1);
7032 /* we add dummy defines for some special macros to speed up tests
7033 and to have working defined() */
7034 sym_push1(&define_stack
, TOK___LINE__
, MACRO_OBJ
, 0);
7035 sym_push1(&define_stack
, TOK___FILE__
, MACRO_OBJ
, 0);
7036 sym_push1(&define_stack
, TOK___DATE__
, MACRO_OBJ
, 0);
7037 sym_push1(&define_stack
, TOK___TIME__
, MACRO_OBJ
, 0);
7039 /* standard defines */
7040 tcc_define_symbol(s
, "__STDC__", NULL
);
7041 #if defined(TCC_TARGET_I386)
7042 tcc_define_symbol(s
, "__i386__", NULL
);
7045 tcc_define_symbol(s
, "linux", NULL
);
7047 /* tiny C specific defines */
7048 tcc_define_symbol(s
, "__TINYC__", NULL
);
7050 /* default library paths */
7051 tcc_add_library_path(s
, "/usr/local/lib");
7052 tcc_add_library_path(s
, "/usr/lib");
7053 tcc_add_library_path(s
, "/lib");
7055 /* no section zero */
7056 dynarray_add((void ***)§ions
, &nb_sections
, NULL
);
7058 /* create standard sections */
7059 text_section
= new_section(".text", SHT_PROGBITS
, SHF_ALLOC
| SHF_EXECINSTR
);
7060 data_section
= new_section(".data", SHT_PROGBITS
, SHF_ALLOC
| SHF_WRITE
);
7061 bss_section
= new_section(".bss", SHT_NOBITS
, SHF_ALLOC
| SHF_WRITE
);
7063 /* symbols are always generated for linking stage */
7064 symtab_section
= new_symtab(".symtab", SHT_SYMTAB
, 0,
7066 ".hashtab", SHF_PRIVATE
);
7067 strtab_section
= symtab_section
->link
;
7069 /* private symbol table for dynamic symbols */
7070 dynsymtab_section
= new_symtab(".dynsymtab", SHT_SYMTAB
, SHF_PRIVATE
,
7072 ".dynhashtab", SHF_PRIVATE
);
7076 void tcc_delete(TCCState
*s
)
7081 int tcc_add_include_path(TCCState
*s
, const char *pathname
)
7085 pathname1
= tcc_strdup(pathname
);
7086 dynarray_add((void ***)&include_paths
, &nb_include_paths
, pathname1
);
7090 int tcc_add_sysinclude_path(TCCState
*s
, const char *pathname
)
7094 pathname1
= tcc_strdup(pathname
);
7095 dynarray_add((void ***)&sysinclude_paths
, &nb_sysinclude_paths
, pathname1
);
7099 static int tcc_add_file_internal(TCCState
*s
, const char *filename
, int flags
)
7104 BufferedFile
*saved_file
;
7106 /* find source file type with extension */
7107 ext
= strrchr(filename
, '.');
7113 file
= tcc_open(filename
);
7115 if (flags
& AFF_PRINT_ERROR
) {
7116 error("file '%s' not found", filename
);
7123 if (!ext
|| !strcmp(ext
, "c")) {
7124 /* C file assumed */
7128 /* assume executable format: auto guess file type */
7129 if (read(fd
, &ehdr
, sizeof(ehdr
)) != sizeof(ehdr
))
7130 error("could not read header");
7131 lseek(fd
, 0, SEEK_SET
);
7133 if (ehdr
.e_ident
[0] == ELFMAG0
&&
7134 ehdr
.e_ident
[1] == ELFMAG1
&&
7135 ehdr
.e_ident
[2] == ELFMAG2
&&
7136 ehdr
.e_ident
[3] == ELFMAG3
) {
7137 file
->line_num
= 0; /* do not display line number if error */
7138 if (ehdr
.e_type
== ET_REL
) {
7139 tcc_load_object_file(s
, fd
, 0);
7140 } else if (ehdr
.e_type
== ET_DYN
) {
7141 tcc_load_dll(s
, fd
, filename
, (flags
& AFF_REFERENCED_DLL
) != 0);
7143 error("unrecognized ELF file");
7145 } else if (memcmp((char *)&ehdr
, ARMAG
, 8) == 0) {
7146 file
->line_num
= 0; /* do not display line number if error */
7147 tcc_load_archive(s
, fd
);
7149 /* as GNU ld, consider it is an ld script if not recognized */
7150 if (tcc_load_ldscript(s
) < 0)
7151 error("unrecognized file type");
7159 void tcc_add_file(TCCState
*s
, const char *filename
)
7161 tcc_add_file_internal(s
, filename
, AFF_PRINT_ERROR
);
7164 int tcc_add_library_path(TCCState
*s
, const char *pathname
)
7168 pathname1
= tcc_strdup(pathname
);
7169 dynarray_add((void ***)&library_paths
, &nb_library_paths
, pathname1
);
7173 /* find and load a dll. Return non zero if not found */
7174 /* XXX: add '-rpath' option support ? */
7175 static int tcc_add_dll(TCCState
*s
, const char *filename
, int flags
)
7180 for(i
= 0; i
< nb_library_paths
; i
++) {
7181 snprintf(buf
, sizeof(buf
), "%s/%s",
7182 library_paths
[i
], filename
);
7183 if (tcc_add_file_internal(s
, buf
, flags
) == 0)
7189 /* the library name is the same as the argument of the '-l' option */
7190 int tcc_add_library(TCCState
*s
, const char *libraryname
)
7196 /* first we look for the dynamic library if not static linking */
7198 snprintf(buf
, sizeof(buf
), "lib%s.so", libraryname
);
7199 /* if we output to memory, then we simply we dlopen(). */
7200 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
7201 /* Since the libc is already loaded, we don't need to load it again */
7202 if (!strcmp(libraryname
, "c"))
7204 h
= dlopen(buf
, RTLD_GLOBAL
| RTLD_LAZY
);
7208 if (tcc_add_dll(s
, buf
, 0) == 0)
7213 /* then we look for the static library */
7214 for(i
= 0; i
< nb_library_paths
; i
++) {
7215 snprintf(buf
, sizeof(buf
), "%s/lib%s.a",
7216 library_paths
[i
], libraryname
);
7217 if (tcc_add_file_internal(s
, buf
, 0) == 0)
7223 int tcc_add_symbol(TCCState
*s
, const char *name
, unsigned long val
)
7225 add_elf_sym(symtab_section
, val
, 0,
7226 ELF32_ST_INFO(STB_GLOBAL
, STT_NOTYPE
),
7231 int tcc_set_output_type(TCCState
*s
, int output_type
)
7233 s
->output_type
= output_type
;
7235 /* if bound checking, then add corresponding sections */
7236 #ifdef CONFIG_TCC_BCHECK
7237 if (do_bounds_check
) {
7239 tcc_define_symbol(s
, "__BOUNDS_CHECKING_ON", NULL
);
7240 /* create bounds sections */
7241 bounds_section
= new_section(".bounds",
7242 SHT_PROGBITS
, SHF_ALLOC
);
7243 lbounds_section
= new_section(".lbounds",
7244 SHT_PROGBITS
, SHF_ALLOC
);
7248 /* add debug sections */
7251 stab_section
= new_section(".stab", SHT_PROGBITS
, 0);
7252 stab_section
->sh_entsize
= sizeof(Stab_Sym
);
7253 stabstr_section
= new_section(".stabstr", SHT_STRTAB
, 0);
7254 put_elf_str(stabstr_section
, "");
7255 stab_section
->link
= stabstr_section
;
7256 /* put first entry */
7257 put_stabs("", 0, 0, 0, 0);
7260 /* add libc crt1/crti objects */
7261 if (output_type
== TCC_OUTPUT_EXE
||
7262 output_type
== TCC_OUTPUT_DLL
) {
7263 if (output_type
!= TCC_OUTPUT_DLL
)
7264 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crt1.o");
7265 tcc_add_file(s
, CONFIG_TCC_CRT_PREFIX
"/crti.o");
7270 #if !defined(LIBTCC)
7274 printf("tcc version 0.9.12 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
7275 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
7276 " [-g] [-b] [-Ldir] [-llib] [-shared] [-static]\n"
7277 " [--] infile1 [infile2... --] [infile_args...]\n"
7279 "General options:\n"
7280 " -c compile only - generate an object file\n"
7281 " -o outfile set output filename\n"
7282 " -- allows multiples input files if no -o option given. Also\n"
7283 " separate input files from runtime arguments\n"
7284 " -Bdir set tcc internal library path\n"
7285 " -bench output compilation statistics\n"
7286 "Preprocessor options:\n"
7287 " -Idir add include path 'dir'\n"
7288 " -Dsym[=val] define 'sym' with value 'val'\n"
7289 " -Usym undefine 'sym'\n"
7290 "C compiler options:\n"
7291 " -g generate runtime debug info\n"
7292 #ifdef CONFIG_TCC_BCHECK
7293 " -b compile with built-in memory and bounds checker (implies -g)\n"
7296 " -Ldir add library path 'dir'\n"
7297 " -llib link with dynamic or static library 'lib'\n"
7298 " -shared generate a shared library\n"
7299 " -static static linking\n"
7300 " -r relocatable output\n"
7304 int main(int argc
, char **argv
)
7307 int optind
, output_type
, multiple_files
, i
, reloc_output
;
7310 int nb_files
, nb_libraries
, nb_objfiles
, dminus
;
7311 char objfilename
[1024];
7314 output_type
= TCC_OUTPUT_MEMORY
;
7325 if (optind
>= argc
) {
7333 /* add a new file */
7334 dynarray_add((void ***)&files
, &nb_files
, r
);
7335 if (!multiple_files
) {
7337 /* argv[0] will be this file */
7340 } else if (r
[1] == '-') {
7341 /* '--' enables multiple files input and also ends several file input */
7342 if (dminus
&& multiple_files
) {
7343 optind
--; /* argv[0] will be '--' */
7348 } else if (r
[1] == 'h' || r
[1] == '?') {
7352 } else if (r
[1] == 'I') {
7353 if (tcc_add_include_path(s
, r
+ 2) < 0)
7354 error("too many include paths");
7355 } else if (r
[1] == 'D') {
7358 value
= strchr(sym
, '=');
7363 tcc_define_symbol(s
, sym
, value
);
7364 } else if (r
[1] == 'U') {
7365 tcc_undefine_symbol(s
, r
+ 2);
7366 } else if (r
[1] == 'L') {
7367 tcc_add_library_path(s
, r
+ 2);
7368 } else if (r
[1] == 'B') {
7369 /* set tcc utilities path (mainly for tcc development) */
7370 tcc_lib_path
= r
+ 2;
7371 } else if (r
[1] == 'l') {
7372 dynarray_add((void ***)&files
, &nb_files
, r
);
7374 } else if (!strcmp(r
+ 1, "bench")) {
7377 #ifdef CONFIG_TCC_BCHECK
7379 do_bounds_check
= 1;
7385 } else if (r
[1] == 'c') {
7387 output_type
= TCC_OUTPUT_OBJ
;
7388 } else if (!strcmp(r
+ 1, "static")) {
7390 } else if (!strcmp(r
+ 1, "shared")) {
7391 output_type
= TCC_OUTPUT_DLL
;
7392 } else if (r
[1] == 'o') {
7396 outfile
= argv
[optind
++];
7397 } else if (r
[1] == 'r') {
7398 /* generate a .o merging several output files */
7400 output_type
= TCC_OUTPUT_OBJ
;
7402 error("invalid option -- '%s'", r
);
7406 nb_objfiles
= nb_files
- nb_libraries
;
7408 /* if outfile provided without other options, we output an
7410 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
7411 output_type
= TCC_OUTPUT_EXE
;
7413 /* check -c consistency : only single file handled. XXX: checks file type */
7414 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
7415 /* accepts only a single input file */
7416 if (nb_objfiles
!= 1)
7417 error("cannot specify multiple files with -c");
7418 if (nb_libraries
!= 0)
7419 error("cannot specify libraries with -c");
7422 /* compute default outfile name */
7423 if (output_type
!= TCC_OUTPUT_MEMORY
&& !outfile
) {
7424 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
7426 /* add .o extension */
7427 pstrcpy(objfilename
, sizeof(objfilename
) - 1, files
[0]);
7428 ext
= strrchr(objfilename
, '.');
7430 goto default_outfile
;
7431 strcpy(ext
+ 1, "o");
7434 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
7436 outfile
= objfilename
;
7439 tcc_set_output_type(s
, output_type
);
7441 /* compile or add each files or library */
7442 for(i
= 0;i
< nb_files
; i
++) {
7443 const char *filename
;
7445 filename
= files
[i
];
7446 if (filename
[0] == '-') {
7447 if (tcc_add_library(s
, filename
+ 2) < 0)
7448 error("cannot find %s", filename
);
7450 tcc_add_file(s
, filename
);
7455 printf("total: %d idents, %d lines, %d bytes\n",
7456 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
);
7458 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);
7462 if (s
->output_type
!= TCC_OUTPUT_MEMORY
) {
7463 tcc_output_file(s
, outfile
);
7466 return tcc_run(s
, argc
- optind
, argv
+ optind
);