update
[tinycc.git] / tcc.c
blob26851f9df3ab9092b2277c59f88d856f4188e948
1 /*
2 * TCC - Tiny C Compiler
3 *
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.
20 #define _GNU_SOURCE
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <math.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <setjmp.h>
32 #ifdef WIN32
33 #include <sys/timeb.h>
34 #endif
35 #ifndef WIN32
36 #include <sys/time.h>
37 #include <sys/ucontext.h>
38 #endif
39 #include "elf.h"
40 #include "stab.h"
41 #ifndef CONFIG_TCC_STATIC
42 #include <dlfcn.h>
43 #endif
45 #include "libtcc.h"
47 /* parser debug */
48 //#define PARSE_DEBUG
49 /* preprocessor debug */
50 //#define PP_DEBUG
51 /* include file debug */
52 //#define INC_DEBUG
54 //#define MEM_DEBUG
56 /* assembler debug */
57 //#define ASM_DEBUG
59 /* target selection */
60 //#define TCC_TARGET_I386 /* i386 code generator */
62 /* default target is I386 */
63 #if !defined(TCC_TARGET_I386)
64 #define TCC_TARGET_I386
65 #endif
67 #if !defined(WIN32) && !defined(TCC_UCLIBC)
68 #define CONFIG_TCC_BCHECK /* enable bound checking code */
69 #endif
71 /* define it to include assembler support */
72 #define CONFIG_TCC_ASM
74 #ifndef CONFIG_TCC_PREFIX
75 #define CONFIG_TCC_PREFIX "/usr/local"
76 #endif
78 /* path to find crt1.o, crti.o and crtn.o. Only needed when generating
79 executables or dlls */
80 #define CONFIG_TCC_CRT_PREFIX "/usr/lib"
82 #define INCLUDE_STACK_SIZE 32
83 #define IFDEF_STACK_SIZE 64
84 #define VSTACK_SIZE 64
85 #define STRING_MAX_SIZE 1024
87 #define TOK_HASH_SIZE 2048 /* must be a power of two */
88 #define TOK_ALLOC_INCR 512 /* must be a power of two */
89 #define TOK_STR_ALLOC_INCR_BITS 6
90 #define TOK_STR_ALLOC_INCR (1 << TOK_STR_ALLOC_INCR_BITS)
91 #define TOK_MAX_SIZE 4 /* token max size in int unit when stored in string */
93 /* token symbol management */
94 typedef struct TokenSym {
95 struct TokenSym *hash_next;
96 struct Sym *sym_define; /* direct pointer to define */
97 struct Sym *sym_label; /* direct pointer to label */
98 struct Sym *sym_struct; /* direct pointer to structure */
99 struct Sym *sym_identifier; /* direct pointer to identifier */
100 int tok; /* token number */
101 int len;
102 char str[1];
103 } TokenSym;
105 typedef struct CString {
106 int size; /* size in bytes */
107 void *data; /* either 'char *' or 'int *' */
108 int size_allocated;
109 void *data_allocated; /* if non NULL, data has been malloced */
110 } CString;
112 /* type definition */
113 typedef struct CType {
114 int t;
115 struct Sym *ref;
116 } CType;
118 /* constant value */
119 typedef union CValue {
120 long double ld;
121 double d;
122 float f;
123 int i;
124 unsigned int ui;
125 unsigned int ul; /* address (should be unsigned long on 64 bit cpu) */
126 long long ll;
127 unsigned long long ull;
128 struct CString *cstr;
129 void *ptr;
130 int tab[1];
131 } CValue;
133 /* value on stack */
134 typedef struct SValue {
135 CType type; /* type */
136 unsigned short r; /* register + flags */
137 unsigned short r2; /* second register, used for 'long long'
138 type. If not used, set to VT_CONST */
139 CValue c; /* constant, if VT_CONST */
140 struct Sym *sym; /* symbol, if (VT_SYM | VT_CONST) */
141 } SValue;
143 /* symbol management */
144 typedef struct Sym {
145 int v; /* symbol token */
146 int r; /* associated register */
147 int c; /* associated number */
148 CType type; /* associated type */
149 struct Sym *next; /* next related symbol */
150 struct Sym *prev; /* prev symbol in stack */
151 struct Sym *prev_tok; /* previous symbol for this token */
152 } Sym;
154 /* section definition */
155 /* XXX: use directly ELF structure for parameters ? */
156 /* special flag to indicate that the section should not be linked to
157 the other ones */
158 #define SHF_PRIVATE 0x80000000
160 typedef struct Section {
161 unsigned long data_offset; /* current data offset */
162 unsigned char *data; /* section data */
163 unsigned long data_allocated; /* used for realloc() handling */
164 int sh_name; /* elf section name (only used during output) */
165 int sh_num; /* elf section number */
166 int sh_type; /* elf section type */
167 int sh_flags; /* elf section flags */
168 int sh_info; /* elf section info */
169 int sh_addralign; /* elf section alignment */
170 int sh_entsize; /* elf entry size */
171 unsigned long sh_size; /* section size (only used during output) */
172 unsigned long sh_addr; /* address at which the section is relocated */
173 unsigned long sh_offset; /* address at which the section is relocated */
174 int nb_hashed_syms; /* used to resize the hash table */
175 struct Section *link; /* link to another section */
176 struct Section *reloc; /* corresponding section for relocation, if any */
177 struct Section *hash; /* hash table for symbols */
178 struct Section *next;
179 char name[64]; /* section name */
180 } Section;
182 typedef struct DLLReference {
183 int level;
184 char name[1];
185 } DLLReference;
187 /* GNUC attribute definition */
188 typedef struct AttributeDef {
189 int aligned;
190 Section *section;
191 unsigned char func_call; /* FUNC_CDECL or FUNC_STDCALL */
192 } AttributeDef;
194 #define SYM_STRUCT 0x40000000 /* struct/union/enum symbol space */
195 #define SYM_FIELD 0x20000000 /* struct/union field symbol space */
196 #define SYM_FIRST_ANOM (1 << (31 - VT_STRUCT_SHIFT)) /* first anonymous sym */
198 /* stored in 'Sym.c' field */
199 #define FUNC_NEW 1 /* ansi function prototype */
200 #define FUNC_OLD 2 /* old function prototype */
201 #define FUNC_ELLIPSIS 3 /* ansi function prototype with ... */
203 /* stored in 'Sym.r' field */
204 #define FUNC_CDECL 0 /* standard c call */
205 #define FUNC_STDCALL 1 /* pascal c call */
207 /* field 'Sym.t' for macros */
208 #define MACRO_OBJ 0 /* object like macro */
209 #define MACRO_FUNC 1 /* function like macro */
211 /* field 'Sym.r' for C labels */
212 #define LABEL_DEFINED 0 /* label is defined */
213 #define LABEL_FORWARD 1 /* label is forward defined */
214 #define LABEL_DECLARED 2 /* label is declared but never used */
216 /* type_decl() types */
217 #define TYPE_ABSTRACT 1 /* type without variable */
218 #define TYPE_DIRECT 2 /* type with variable */
220 #define IO_BUF_SIZE 8192
222 typedef struct BufferedFile {
223 uint8_t *buf_ptr;
224 uint8_t *buf_end;
225 int fd;
226 int line_num; /* current line number - here to simplify code */
227 int ifndef_macro; /* #ifndef macro / #endif search */
228 int ifndef_macro_saved; /* saved ifndef_macro */
229 int *ifdef_stack_ptr; /* ifdef_stack value at the start of the file */
230 char inc_type; /* type of include */
231 char inc_filename[512]; /* filename specified by the user */
232 char filename[1024]; /* current filename - here to simplify code */
233 unsigned char buffer[IO_BUF_SIZE + 1]; /* extra size for CH_EOB char */
234 } BufferedFile;
236 #define CH_EOB '\\' /* end of buffer or '\0' char in file */
237 #define CH_EOF (-1) /* end of file */
239 /* parsing state (used to save parser state to reparse part of the
240 source several times) */
241 typedef struct ParseState {
242 int *macro_ptr;
243 int line_num;
244 int tok;
245 CValue tokc;
246 } ParseState;
248 /* used to record tokens */
249 typedef struct TokenString {
250 int *str;
251 int len;
252 int allocated_len;
253 int last_line_num;
254 } TokenString;
256 /* include file cache, used to find files faster and also to eliminate
257 inclusion if the include file is protected by #ifndef ... #endif */
258 typedef struct CachedInclude {
259 int ifndef_macro;
260 char type; /* '"' or '>' to give include type */
261 char filename[1]; /* path specified in #include */
262 } CachedInclude;
264 /* parser */
265 static struct BufferedFile *file;
266 static int ch, tok;
267 static CValue tokc;
268 static CString tokcstr; /* current parsed string, if any */
269 /* additional informations about token */
270 static int tok_flags;
271 #define TOK_FLAG_BOL 0x0001 /* beginning of line before */
272 #define TOK_FLAG_BOF 0x0002 /* beginning of file before */
273 #define TOK_FLAG_ENDIF 0x0004 /* a endif was found matching starting #ifdef */
275 static int *macro_ptr, *macro_ptr_allocated;
276 static int *unget_saved_macro_ptr;
277 static int unget_saved_buffer[TOK_MAX_SIZE + 1];
278 static int unget_buffer_enabled;
279 static int parse_flags;
280 #define PARSE_FLAG_PREPROCESS 0x0001 /* activate preprocessing */
281 #define PARSE_FLAG_TOK_NUM 0x0002 /* return numbers instead of TOK_PPNUM */
282 #define PARSE_FLAG_LINEFEED 0x0004 /* line feed is returned as a
283 token. line feed is also
284 returned at eof */
286 static Section *text_section, *data_section, *bss_section; /* predefined sections */
287 static Section *cur_text_section; /* current section where function code is
288 generated */
289 /* bound check related sections */
290 static Section *bounds_section; /* contains global data bound description */
291 static Section *lbounds_section; /* contains local data bound description */
292 /* symbol sections */
293 static Section *symtab_section, *strtab_section;
295 /* debug sections */
296 static Section *stab_section, *stabstr_section;
298 /* loc : local variable index
299 ind : output code index
300 rsym: return symbol
301 anon_sym: anonymous symbol index
303 static int rsym, anon_sym, ind, loc;
304 /* expression generation modifiers */
305 static int const_wanted; /* true if constant wanted */
306 static int nocode_wanted; /* true if no code generation wanted for an expression */
307 static int global_expr; /* true if compound literals must be allocated
308 globally (used during initializers parsing */
309 static CType func_vt; /* current function return type (used by return
310 instruction) */
311 static int func_vc;
312 static int last_line_num, last_ind, func_ind; /* debug last line number and pc */
313 static int tok_ident;
314 static TokenSym **table_ident;
315 static TokenSym *hash_ident[TOK_HASH_SIZE];
316 static char token_buf[STRING_MAX_SIZE + 1];
317 static char *funcname;
318 static Sym *global_stack, *local_stack;
319 static Sym *define_stack;
320 static Sym *global_label_stack, *local_label_stack;
322 static SValue vstack[VSTACK_SIZE], *vtop;
323 /* some predefined types */
324 static CType char_pointer_type, func_old_type, int_type;
325 /* true if isid(c) || isnum(c) */
326 static unsigned char isidnum_table[256];
328 /* compile with debug symbol (and use them if error during execution) */
329 static int do_debug = 0;
331 /* compile with built-in memory and bounds checker */
332 static int do_bounds_check = 0;
334 /* display benchmark infos */
335 static int do_bench = 0;
336 static int total_lines;
337 static int total_bytes;
339 /* use GNU C extensions */
340 static int gnu_ext = 1;
342 /* use Tiny C extensions */
343 static int tcc_ext = 1;
345 /* max number of callers shown if error */
346 static int num_callers = 6;
347 static const char **rt_bound_error_msg;
349 /* XXX: get rid of this ASAP */
350 static struct TCCState *tcc_state;
352 /* give the path of the tcc libraries */
353 static const char *tcc_lib_path = CONFIG_TCC_PREFIX "/lib/tcc";
355 struct TCCState {
356 int output_type;
358 BufferedFile **include_stack_ptr;
359 int *ifdef_stack_ptr;
361 /* include file handling */
362 char **include_paths;
363 int nb_include_paths;
364 char **sysinclude_paths;
365 int nb_sysinclude_paths;
366 CachedInclude **cached_includes;
367 int nb_cached_includes;
369 char **library_paths;
370 int nb_library_paths;
372 /* array of all loaded dlls (including those referenced by loaded
373 dlls) */
374 DLLReference **loaded_dlls;
375 int nb_loaded_dlls;
377 /* sections */
378 Section **sections;
379 int nb_sections; /* number of sections, including first dummy section */
381 /* got handling */
382 Section *got;
383 Section *plt;
384 unsigned long *got_offsets;
385 int nb_got_offsets;
386 /* give the correspondance from symtab indexes to dynsym indexes */
387 int *symtab_to_dynsym;
389 /* temporary dynamic symbol sections (for dll loading) */
390 Section *dynsymtab_section;
391 /* exported dynamic symbol section */
392 Section *dynsym;
394 /* if true, no standard headers are added */
395 int nostdinc;
397 /* if true, static linking is performed */
398 int static_link;
400 /* error handling */
401 void *error_opaque;
402 void (*error_func)(void *opaque, const char *msg);
403 int error_set_jmp_enabled;
404 jmp_buf error_jmp_buf;
405 int nb_errors;
407 /* tiny assembler state */
408 Sym *asm_labels;
410 /* see include_stack_ptr */
411 BufferedFile *include_stack[INCLUDE_STACK_SIZE];
413 /* see ifdef_stack_ptr */
414 int ifdef_stack[IFDEF_STACK_SIZE];
417 /* The current value can be: */
418 #define VT_VALMASK 0x00ff
419 #define VT_CONST 0x00f0 /* constant in vc
420 (must be first non register value) */
421 #define VT_LLOCAL 0x00f1 /* lvalue, offset on stack */
422 #define VT_LOCAL 0x00f2 /* offset on stack */
423 #define VT_CMP 0x00f3 /* the value is stored in processor flags (in vc) */
424 #define VT_JMP 0x00f4 /* value is the consequence of jmp true (even) */
425 #define VT_JMPI 0x00f5 /* value is the consequence of jmp false (odd) */
426 #define VT_LVAL 0x0100 /* var is an lvalue */
427 #define VT_SYM 0x0200 /* a symbol value is added */
428 #define VT_MUSTCAST 0x0400 /* value must be casted to be correct (used for
429 char/short stored in integer registers) */
430 #define VT_MUSTBOUND 0x0800 /* bound checking must be done before
431 dereferencing value */
432 #define VT_BOUNDED 0x8000 /* value is bounded. The address of the
433 bounding function call point is in vc */
434 #define VT_LVAL_BYTE 0x1000 /* lvalue is a byte */
435 #define VT_LVAL_SHORT 0x2000 /* lvalue is a short */
436 #define VT_LVAL_UNSIGNED 0x4000 /* lvalue is unsigned */
437 #define VT_LVAL_TYPE (VT_LVAL_BYTE | VT_LVAL_SHORT | VT_LVAL_UNSIGNED)
439 /* types */
440 #define VT_STRUCT_SHIFT 12 /* structure/enum name shift (20 bits left) */
442 #define VT_INT 0 /* integer type */
443 #define VT_BYTE 1 /* signed byte type */
444 #define VT_SHORT 2 /* short type */
445 #define VT_VOID 3 /* void type */
446 #define VT_PTR 4 /* pointer */
447 #define VT_ENUM 5 /* enum definition */
448 #define VT_FUNC 6 /* function type */
449 #define VT_STRUCT 7 /* struct/union definition */
450 #define VT_FLOAT 8 /* IEEE float */
451 #define VT_DOUBLE 9 /* IEEE double */
452 #define VT_LDOUBLE 10 /* IEEE long double */
453 #define VT_BOOL 11 /* ISOC99 boolean type */
454 #define VT_LLONG 12 /* 64 bit integer */
455 #define VT_LONG 13 /* long integer (NEVER USED as type, only
456 during parsing) */
457 #define VT_BTYPE 0x000f /* mask for basic type */
458 #define VT_UNSIGNED 0x0010 /* unsigned type */
459 #define VT_ARRAY 0x0020 /* array type (also has VT_PTR) */
460 #define VT_BITFIELD 0x0040 /* bitfield modifier */
462 /* storage */
463 #define VT_EXTERN 0x00000080 /* extern definition */
464 #define VT_STATIC 0x00000100 /* static variable */
465 #define VT_TYPEDEF 0x00000200 /* typedef definition */
466 #define VT_INLINE 0x00000400 /* inline definition */
468 /* type mask (except storage) */
469 #define VT_STORAGE (VT_EXTERN | VT_STATIC | VT_TYPEDEF | VT_INLINE)
470 #define VT_TYPE (~(VT_STORAGE))
472 /* token values */
474 /* warning: the following compare tokens depend on i386 asm code */
475 #define TOK_ULT 0x92
476 #define TOK_UGE 0x93
477 #define TOK_EQ 0x94
478 #define TOK_NE 0x95
479 #define TOK_ULE 0x96
480 #define TOK_UGT 0x97
481 #define TOK_LT 0x9c
482 #define TOK_GE 0x9d
483 #define TOK_LE 0x9e
484 #define TOK_GT 0x9f
486 #define TOK_LAND 0xa0
487 #define TOK_LOR 0xa1
489 #define TOK_DEC 0xa2
490 #define TOK_MID 0xa3 /* inc/dec, to void constant */
491 #define TOK_INC 0xa4
492 #define TOK_UDIV 0xb0 /* unsigned division */
493 #define TOK_UMOD 0xb1 /* unsigned modulo */
494 #define TOK_PDIV 0xb2 /* fast division with undefined rounding for pointers */
495 #define TOK_CINT 0xb3 /* number in tokc */
496 #define TOK_CCHAR 0xb4 /* char constant in tokc */
497 #define TOK_STR 0xb5 /* pointer to string in tokc */
498 #define TOK_TWOSHARPS 0xb6 /* ## preprocessing token */
499 #define TOK_LCHAR 0xb7
500 #define TOK_LSTR 0xb8
501 #define TOK_CFLOAT 0xb9 /* float constant */
502 #define TOK_LINENUM 0xba /* line number info */
503 #define TOK_CDOUBLE 0xc0 /* double constant */
504 #define TOK_CLDOUBLE 0xc1 /* long double constant */
505 #define TOK_UMULL 0xc2 /* unsigned 32x32 -> 64 mul */
506 #define TOK_ADDC1 0xc3 /* add with carry generation */
507 #define TOK_ADDC2 0xc4 /* add with carry use */
508 #define TOK_SUBC1 0xc5 /* add with carry generation */
509 #define TOK_SUBC2 0xc6 /* add with carry use */
510 #define TOK_CUINT 0xc8 /* unsigned int constant */
511 #define TOK_CLLONG 0xc9 /* long long constant */
512 #define TOK_CULLONG 0xca /* unsigned long long constant */
513 #define TOK_ARROW 0xcb
514 #define TOK_DOTS 0xcc /* three dots */
515 #define TOK_SHR 0xcd /* unsigned shift right */
516 #define TOK_PPNUM 0xce /* preprocessor number */
518 #define TOK_SHL 0x01 /* shift left */
519 #define TOK_SAR 0x02 /* signed shift right */
521 /* assignement operators : normal operator or 0x80 */
522 #define TOK_A_MOD 0xa5
523 #define TOK_A_AND 0xa6
524 #define TOK_A_MUL 0xaa
525 #define TOK_A_ADD 0xab
526 #define TOK_A_SUB 0xad
527 #define TOK_A_DIV 0xaf
528 #define TOK_A_XOR 0xde
529 #define TOK_A_OR 0xfc
530 #define TOK_A_SHL 0x81
531 #define TOK_A_SAR 0x82
533 /* WARNING: the content of this string encodes token numbers */
534 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";
536 #define TOK_EOF (-1) /* end of file */
537 #define TOK_LINEFEED 10 /* line feed */
539 /* all identificators and strings have token above that */
540 #define TOK_IDENT 256
542 /* only used for i386 asm opcodes definitions */
543 #define DEF_ASM(x) DEF(TOK_ASM_ ## x, #x)
545 #define DEF_BWL(x) \
546 DEF(TOK_ASM_ ## x ## b, #x "b") \
547 DEF(TOK_ASM_ ## x ## w, #x "w") \
548 DEF(TOK_ASM_ ## x ## l, #x "l") \
549 DEF(TOK_ASM_ ## x, #x)
551 #define DEF_WL(x) \
552 DEF(TOK_ASM_ ## x ## w, #x "w") \
553 DEF(TOK_ASM_ ## x ## l, #x "l") \
554 DEF(TOK_ASM_ ## x, #x)
556 #define DEF_FP1(x) \
557 DEF(TOK_ASM_ ## f ## x ## s, "f" #x "s") \
558 DEF(TOK_ASM_ ## fi ## x ## l, "fi" #x "l") \
559 DEF(TOK_ASM_ ## f ## x ## l, "f" #x "l") \
560 DEF(TOK_ASM_ ## fi ## x ## s, "fi" #x "s")
562 #define DEF_FP(x) \
563 DEF(TOK_ASM_ ## f ## x, "f" #x ) \
564 DEF(TOK_ASM_ ## f ## x ## p, "f" #x "p") \
565 DEF_FP1(x)
567 #define DEF_ASMTEST(x) \
568 DEF_ASM(x ## o) \
569 DEF_ASM(x ## no) \
570 DEF_ASM(x ## b) \
571 DEF_ASM(x ## c) \
572 DEF_ASM(x ## nae) \
573 DEF_ASM(x ## nb) \
574 DEF_ASM(x ## nc) \
575 DEF_ASM(x ## ae) \
576 DEF_ASM(x ## e) \
577 DEF_ASM(x ## z) \
578 DEF_ASM(x ## ne) \
579 DEF_ASM(x ## nz) \
580 DEF_ASM(x ## be) \
581 DEF_ASM(x ## na) \
582 DEF_ASM(x ## nbe) \
583 DEF_ASM(x ## a) \
584 DEF_ASM(x ## s) \
585 DEF_ASM(x ## ns) \
586 DEF_ASM(x ## p) \
587 DEF_ASM(x ## pe) \
588 DEF_ASM(x ## np) \
589 DEF_ASM(x ## po) \
590 DEF_ASM(x ## l) \
591 DEF_ASM(x ## nge) \
592 DEF_ASM(x ## nl) \
593 DEF_ASM(x ## ge) \
594 DEF_ASM(x ## le) \
595 DEF_ASM(x ## ng) \
596 DEF_ASM(x ## nle) \
597 DEF_ASM(x ## g)
599 #define TOK_ASM_int TOK_INT
601 enum {
602 TOK_LAST = TOK_IDENT - 1,
603 #define DEF(id, str) id,
604 #include "tcctok.h"
605 #undef DEF
608 static const char tcc_keywords[] =
609 #define DEF(id, str) str "\0"
610 #include "tcctok.h"
611 #undef DEF
614 #define TOK_UIDENT TOK_DEFINE
616 #ifdef WIN32
617 #define snprintf _snprintf
618 #define vsnprintf _vsnprintf
619 #endif
621 #if defined(WIN32) || defined(TCC_UCLIBC) || defined(__FreeBSD__)
622 /* currently incorrect */
623 long double strtold(const char *nptr, char **endptr)
625 return (long double)strtod(nptr, endptr);
627 float strtof(const char *nptr, char **endptr)
629 return (float)strtod(nptr, endptr);
631 #else
632 /* XXX: need to define this to use them in non ISOC99 context */
633 extern float strtof (const char *__nptr, char **__endptr);
634 extern long double strtold (const char *__nptr, char **__endptr);
635 #endif
637 static char *pstrcpy(char *buf, int buf_size, const char *s);
638 static char *pstrcat(char *buf, int buf_size, const char *s);
640 static void next(void);
641 static void next_nomacro(void);
642 static void parse_expr_type(CType *type);
643 static void expr_type(CType *type);
644 static void unary_type(CType *type);
645 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
646 int case_reg, int is_expr);
647 static int expr_const(void);
648 static void expr_eq(void);
649 static void gexpr(void);
650 static void decl(int l);
651 static void decl_initializer(CType *type, Section *sec, unsigned long c,
652 int first, int size_only);
653 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
654 int has_init, int v, int scope);
655 int gv(int rc);
656 void gv2(int rc1, int rc2);
657 void move_reg(int r, int s);
658 void save_regs(int n);
659 void save_reg(int r);
660 void vpop(void);
661 void vswap(void);
662 void vdup(void);
663 int get_reg(int rc);
665 static void macro_subst(TokenString *tok_str,
666 Sym **nested_list, const int *macro_str);
667 int save_reg_forced(int r);
668 void gen_op(int op);
669 void force_charshort_cast(int t);
670 static void gen_cast(CType *type);
671 void vstore(void);
672 static Sym *sym_find(int v);
673 static Sym *sym_push(int v, CType *type, int r, int c);
675 /* type handling */
676 static int type_size(CType *type, int *a);
677 static inline CType *pointed_type(CType *type);
678 static int pointed_size(CType *type);
679 static int lvalue_type(int t);
680 static int is_compatible_types(CType *type1, CType *type2);
681 static int parse_btype(CType *type, AttributeDef *ad);
682 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
684 void error(const char *fmt, ...);
685 void vpushi(int v);
686 void vset(CType *type, int r, int v);
687 void type_to_str(char *buf, int buf_size,
688 CType *type, const char *varstr);
689 char *get_tok_str(int v, CValue *cv);
690 static Sym *get_sym_ref(CType *type, Section *sec,
691 unsigned long offset, unsigned long size);
692 static Sym *external_global_sym(int v, CType *type, int r);
694 /* section generation */
695 static void section_realloc(Section *sec, unsigned long new_size);
696 static void *section_ptr_add(Section *sec, unsigned long size);
697 static void put_extern_sym(Sym *sym, Section *section,
698 unsigned long value, unsigned long size);
699 static void greloc(Section *s, Sym *sym, unsigned long addr, int type);
700 static int put_elf_str(Section *s, const char *sym);
701 static int put_elf_sym(Section *s,
702 unsigned long value, unsigned long size,
703 int info, int other, int shndx, const char *name);
704 static int add_elf_sym(Section *s, unsigned long value, unsigned long size,
705 int info, int sh_num, const char *name);
706 static void put_elf_reloc(Section *symtab, Section *s, unsigned long offset,
707 int type, int symbol);
708 static void put_stabs(const char *str, int type, int other, int desc,
709 unsigned long value);
710 static void put_stabs_r(const char *str, int type, int other, int desc,
711 unsigned long value, Section *sec, int sym_index);
712 static void put_stabn(int type, int other, int desc, int value);
713 static void put_stabd(int type, int other, int desc);
714 static int tcc_add_dll(TCCState *s, const char *filename, int flags);
716 #define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
717 #define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
718 static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
720 /* tccasm.c */
722 #ifdef CONFIG_TCC_ASM
724 typedef struct ExprValue {
725 uint32_t v;
726 Sym *sym;
727 } ExprValue;
729 #define MAX_ASM_OPERANDS 30
731 typedef struct ASMOperand {
732 int id; /* GCC 3 optionnal identifier (0 if number only supported */
733 char *constraint;
734 char asm_str[16]; /* computed asm string for operand */
735 SValue *vt; /* C value of the expression */
736 int ref_index; /* if >= 0, gives reference to a output constraint */
737 int priority; /* priority, used to assign registers */
738 int reg; /* if >= 0, register number used for this operand */
739 } ASMOperand;
741 static void asm_expr(TCCState *s1, ExprValue *pe);
742 static int asm_int_expr(TCCState *s1);
743 static int find_constraint(ASMOperand *operands, int nb_operands,
744 const char *name, const char **pp);
746 static int tcc_assemble(TCCState *s1, int do_preprocess);
748 #endif
750 static void asm_instr(void);
752 /* true if float/double/long double type */
753 static inline int is_float(int t)
755 int bt;
756 bt = t & VT_BTYPE;
757 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT;
760 #ifdef TCC_TARGET_I386
761 #include "i386-gen.c"
762 #endif
764 #ifdef CONFIG_TCC_STATIC
766 #define RTLD_LAZY 0x001
767 #define RTLD_NOW 0x002
768 #define RTLD_GLOBAL 0x100
769 #define RTLD_DEFAULT NULL
771 /* dummy function for profiling */
772 void *dlopen(const char *filename, int flag)
774 return NULL;
777 const char *dlerror(void)
779 return "error";
782 typedef struct TCCSyms {
783 char *str;
784 void *ptr;
785 } TCCSyms;
787 #define TCCSYM(a) { #a, &a, },
789 /* add the symbol you want here if no dynamic linking is done */
790 static TCCSyms tcc_syms[] = {
791 TCCSYM(printf)
792 TCCSYM(fprintf)
793 TCCSYM(fopen)
794 TCCSYM(fclose)
795 { NULL, NULL },
798 void *dlsym(void *handle, const char *symbol)
800 TCCSyms *p;
801 p = tcc_syms;
802 while (p->str != NULL) {
803 if (!strcmp(p->str, symbol))
804 return p->ptr;
805 p++;
807 return NULL;
810 #endif
812 /********************************************************/
814 /* we use our own 'finite' function to avoid potential problems with
815 non standard math libs */
816 /* XXX: endianness dependent */
817 int ieee_finite(double d)
819 int *p = (int *)&d;
820 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
823 /* copy a string and truncate it. */
824 static char *pstrcpy(char *buf, int buf_size, const char *s)
826 char *q, *q_end;
827 int c;
829 if (buf_size > 0) {
830 q = buf;
831 q_end = buf + buf_size - 1;
832 while (q < q_end) {
833 c = *s++;
834 if (c == '\0')
835 break;
836 *q++ = c;
838 *q = '\0';
840 return buf;
843 /* strcat and truncate. */
844 static char *pstrcat(char *buf, int buf_size, const char *s)
846 int len;
847 len = strlen(buf);
848 if (len < buf_size)
849 pstrcpy(buf + len, buf_size - len, s);
850 return buf;
853 /* memory management */
854 #ifdef MEM_DEBUG
855 int mem_cur_size;
856 int mem_max_size;
857 #endif
859 static inline void tcc_free(void *ptr)
861 #ifdef MEM_DEBUG
862 mem_cur_size -= malloc_usable_size(ptr);
863 #endif
864 free(ptr);
867 static void *tcc_malloc(unsigned long size)
869 void *ptr;
870 ptr = malloc(size);
871 if (!ptr && size)
872 error("memory full");
873 #ifdef MEM_DEBUG
874 mem_cur_size += malloc_usable_size(ptr);
875 if (mem_cur_size > mem_max_size)
876 mem_max_size = mem_cur_size;
877 #endif
878 return ptr;
881 static void *tcc_mallocz(unsigned long size)
883 void *ptr;
884 ptr = tcc_malloc(size);
885 memset(ptr, 0, size);
886 return ptr;
889 static inline void *tcc_realloc(void *ptr, unsigned long size)
891 void *ptr1;
892 #ifdef MEM_DEBUG
893 mem_cur_size -= malloc_usable_size(ptr);
894 #endif
895 ptr1 = realloc(ptr, size);
896 #ifdef MEM_DEBUG
897 /* NOTE: count not correct if alloc error, but not critical */
898 mem_cur_size += malloc_usable_size(ptr1);
899 if (mem_cur_size > mem_max_size)
900 mem_max_size = mem_cur_size;
901 #endif
902 return ptr1;
905 static char *tcc_strdup(const char *str)
907 char *ptr;
908 ptr = tcc_malloc(strlen(str) + 1);
909 strcpy(ptr, str);
910 return ptr;
913 #define free(p) use_tcc_free(p)
914 #define malloc(s) use_tcc_malloc(s)
915 #define realloc(p, s) use_tcc_realloc(p, s)
917 static void dynarray_add(void ***ptab, int *nb_ptr, void *data)
919 int nb, nb_alloc;
920 void **pp;
922 nb = *nb_ptr;
923 pp = *ptab;
924 /* every power of two we double array size */
925 if ((nb & (nb - 1)) == 0) {
926 if (!nb)
927 nb_alloc = 1;
928 else
929 nb_alloc = nb * 2;
930 pp = tcc_realloc(pp, nb_alloc * sizeof(void *));
931 if (!pp)
932 error("memory full");
933 *ptab = pp;
935 pp[nb++] = data;
936 *nb_ptr = nb;
939 Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags)
941 Section *sec;
943 sec = tcc_mallocz(sizeof(Section));
944 pstrcpy(sec->name, sizeof(sec->name), name);
945 sec->sh_type = sh_type;
946 sec->sh_flags = sh_flags;
947 switch(sh_type) {
948 case SHT_HASH:
949 case SHT_REL:
950 case SHT_DYNSYM:
951 case SHT_SYMTAB:
952 case SHT_DYNAMIC:
953 sec->sh_addralign = 4;
954 break;
955 case SHT_STRTAB:
956 sec->sh_addralign = 1;
957 break;
958 default:
959 sec->sh_addralign = 32; /* default conservative alignment */
960 break;
963 /* only add section if not private */
964 if (!(sh_flags & SHF_PRIVATE)) {
965 sec->sh_num = s1->nb_sections;
966 dynarray_add((void ***)&s1->sections, &s1->nb_sections, sec);
968 return sec;
971 static void free_section(Section *s)
973 tcc_free(s->data);
974 tcc_free(s);
977 /* realloc section and set its content to zero */
978 static void section_realloc(Section *sec, unsigned long new_size)
980 unsigned long size;
981 unsigned char *data;
983 size = sec->data_allocated;
984 if (size == 0)
985 size = 1;
986 while (size < new_size)
987 size = size * 2;
988 data = tcc_realloc(sec->data, size);
989 if (!data)
990 error("memory full");
991 memset(data + sec->data_allocated, 0, size - sec->data_allocated);
992 sec->data = data;
993 sec->data_allocated = size;
996 /* reserve at least 'size' bytes in section 'sec' from
997 sec->data_offset. */
998 static void *section_ptr_add(Section *sec, unsigned long size)
1000 unsigned long offset, offset1;
1002 offset = sec->data_offset;
1003 offset1 = offset + size;
1004 if (offset1 > sec->data_allocated)
1005 section_realloc(sec, offset1);
1006 sec->data_offset = offset1;
1007 return sec->data + offset;
1010 /* return a reference to a section, and create it if it does not
1011 exists */
1012 Section *find_section(TCCState *s1, const char *name)
1014 Section *sec;
1015 int i;
1016 for(i = 1; i < s1->nb_sections; i++) {
1017 sec = s1->sections[i];
1018 if (!strcmp(name, sec->name))
1019 return sec;
1021 /* sections are created as PROGBITS */
1022 return new_section(s1, name, SHT_PROGBITS, SHF_ALLOC);
1025 /* update sym->c so that it points to an external symbol in section
1026 'section' with value 'value' */
1027 static void put_extern_sym(Sym *sym, Section *section,
1028 unsigned long value, unsigned long size)
1030 int sym_type, sym_bind, sh_num, info;
1031 Elf32_Sym *esym;
1032 const char *name;
1034 if (section)
1035 sh_num = section->sh_num;
1036 else
1037 sh_num = SHN_UNDEF;
1038 if (!sym->c) {
1039 if ((sym->type.t & VT_BTYPE) == VT_FUNC)
1040 sym_type = STT_FUNC;
1041 else
1042 sym_type = STT_OBJECT;
1043 if (sym->type.t & VT_STATIC)
1044 sym_bind = STB_LOCAL;
1045 else
1046 sym_bind = STB_GLOBAL;
1048 name = get_tok_str(sym->v, NULL);
1049 #ifdef CONFIG_TCC_BCHECK
1050 if (do_bounds_check) {
1051 char buf[32];
1053 /* XXX: avoid doing that for statics ? */
1054 /* if bound checking is activated, we change some function
1055 names by adding the "__bound" prefix */
1056 switch(sym->v) {
1057 #if 0
1058 /* XXX: we rely only on malloc hooks */
1059 case TOK_malloc:
1060 case TOK_free:
1061 case TOK_realloc:
1062 case TOK_memalign:
1063 case TOK_calloc:
1064 #endif
1065 case TOK_memcpy:
1066 case TOK_memmove:
1067 case TOK_memset:
1068 case TOK_strlen:
1069 case TOK_strcpy:
1070 strcpy(buf, "__bound_");
1071 strcat(buf, name);
1072 name = buf;
1073 break;
1076 #endif
1077 info = ELF32_ST_INFO(sym_bind, sym_type);
1078 sym->c = add_elf_sym(symtab_section, value, size, info, sh_num, name);
1079 } else {
1080 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
1081 esym->st_value = value;
1082 esym->st_size = size;
1083 esym->st_shndx = sh_num;
1087 /* add a new relocation entry to symbol 'sym' in section 's' */
1088 static void greloc(Section *s, Sym *sym, unsigned long offset, int type)
1090 if (!sym->c)
1091 put_extern_sym(sym, NULL, 0, 0);
1092 /* now we can add ELF relocation info */
1093 put_elf_reloc(symtab_section, s, offset, type, sym->c);
1096 static inline int isid(int c)
1098 return (c >= 'a' && c <= 'z') ||
1099 (c >= 'A' && c <= 'Z') ||
1100 c == '_';
1103 static inline int isnum(int c)
1105 return c >= '0' && c <= '9';
1108 static inline int isoct(int c)
1110 return c >= '0' && c <= '7';
1113 static inline int toup(int c)
1115 if (c >= 'a' && c <= 'z')
1116 return c - 'a' + 'A';
1117 else
1118 return c;
1121 static void strcat_vprintf(char *buf, int buf_size, const char *fmt, va_list ap)
1123 int len;
1124 len = strlen(buf);
1125 vsnprintf(buf + len, buf_size - len, fmt, ap);
1128 static void strcat_printf(char *buf, int buf_size, const char *fmt, ...)
1130 va_list ap;
1131 va_start(ap, fmt);
1132 strcat_vprintf(buf, buf_size, fmt, ap);
1133 va_end(ap);
1136 void error1(TCCState *s1, int is_warning, const char *fmt, va_list ap)
1138 char buf[2048];
1139 BufferedFile **f;
1141 buf[0] = '\0';
1142 if (file) {
1143 for(f = s1->include_stack; f < s1->include_stack_ptr; f++)
1144 strcat_printf(buf, sizeof(buf), "In file included from %s:%d:\n",
1145 (*f)->filename, (*f)->line_num);
1146 if (file->line_num > 0) {
1147 strcat_printf(buf, sizeof(buf),
1148 "%s:%d: ", file->filename, file->line_num);
1149 } else {
1150 strcat_printf(buf, sizeof(buf),
1151 "%s: ", file->filename);
1153 } else {
1154 strcat_printf(buf, sizeof(buf),
1155 "tcc: ");
1157 if (is_warning)
1158 strcat_printf(buf, sizeof(buf), "warning: ");
1159 strcat_vprintf(buf, sizeof(buf), fmt, ap);
1161 if (!s1->error_func) {
1162 /* default case: stderr */
1163 fprintf(stderr, "%s\n", buf);
1164 } else {
1165 s1->error_func(s1->error_opaque, buf);
1167 if (!is_warning)
1168 s1->nb_errors++;
1171 #ifdef LIBTCC
1172 void tcc_set_error_func(TCCState *s, void *error_opaque,
1173 void (*error_func)(void *opaque, const char *msg))
1175 s->error_opaque = error_opaque;
1176 s->error_func = error_func;
1178 #endif
1180 /* error without aborting current compilation */
1181 void error_noabort(const char *fmt, ...)
1183 TCCState *s1 = tcc_state;
1184 va_list ap;
1186 va_start(ap, fmt);
1187 error1(s1, 0, fmt, ap);
1188 va_end(ap);
1191 void error(const char *fmt, ...)
1193 TCCState *s1 = tcc_state;
1194 va_list ap;
1196 va_start(ap, fmt);
1197 error1(s1, 0, fmt, ap);
1198 va_end(ap);
1199 /* better than nothing: in some cases, we accept to handle errors */
1200 if (s1->error_set_jmp_enabled) {
1201 longjmp(s1->error_jmp_buf, 1);
1202 } else {
1203 /* XXX: eliminate this someday */
1204 exit(1);
1208 void expect(const char *msg)
1210 error("%s expected", msg);
1213 void warning(const char *fmt, ...)
1215 TCCState *s1 = tcc_state;
1216 va_list ap;
1218 va_start(ap, fmt);
1219 error1(s1, 1, fmt, ap);
1220 va_end(ap);
1223 void skip(int c)
1225 if (tok != c)
1226 error("'%c' expected", c);
1227 next();
1230 static void test_lvalue(void)
1232 if (!(vtop->r & VT_LVAL))
1233 expect("lvalue");
1236 /* allocate a new token */
1237 static TokenSym *tok_alloc_new(TokenSym **pts, const char *str, int len)
1239 TokenSym *ts, **ptable;
1240 int i;
1242 if (tok_ident >= SYM_FIRST_ANOM)
1243 error("memory full");
1245 /* expand token table if needed */
1246 i = tok_ident - TOK_IDENT;
1247 if ((i % TOK_ALLOC_INCR) == 0) {
1248 ptable = tcc_realloc(table_ident, (i + TOK_ALLOC_INCR) * sizeof(TokenSym *));
1249 if (!ptable)
1250 error("memory full");
1251 table_ident = ptable;
1254 ts = tcc_malloc(sizeof(TokenSym) + len);
1255 table_ident[i] = ts;
1256 ts->tok = tok_ident++;
1257 ts->sym_define = NULL;
1258 ts->sym_label = NULL;
1259 ts->sym_struct = NULL;
1260 ts->sym_identifier = NULL;
1261 ts->len = len;
1262 ts->hash_next = NULL;
1263 memcpy(ts->str, str, len);
1264 ts->str[len] = '\0';
1265 *pts = ts;
1266 return ts;
1269 #define TOK_HASH_INIT 1
1270 #define TOK_HASH_FUNC(h, c) ((h) * 263 + (c))
1272 /* find a token and add it if not found */
1273 static TokenSym *tok_alloc(const char *str, int len)
1275 TokenSym *ts, **pts;
1276 int i;
1277 unsigned int h;
1279 h = TOK_HASH_INIT;
1280 for(i=0;i<len;i++)
1281 h = TOK_HASH_FUNC(h, ((unsigned char *)str)[i]);
1282 h &= (TOK_HASH_SIZE - 1);
1284 pts = &hash_ident[h];
1285 for(;;) {
1286 ts = *pts;
1287 if (!ts)
1288 break;
1289 if (ts->len == len && !memcmp(ts->str, str, len))
1290 return ts;
1291 pts = &(ts->hash_next);
1293 return tok_alloc_new(pts, str, len);
1296 /* CString handling */
1298 static void cstr_realloc(CString *cstr, int new_size)
1300 int size;
1301 void *data;
1303 size = cstr->size_allocated;
1304 if (size == 0)
1305 size = 8; /* no need to allocate a too small first string */
1306 while (size < new_size)
1307 size = size * 2;
1308 data = tcc_realloc(cstr->data_allocated, size);
1309 if (!data)
1310 error("memory full");
1311 cstr->data_allocated = data;
1312 cstr->size_allocated = size;
1313 cstr->data = data;
1316 /* add a byte */
1317 static void cstr_ccat(CString *cstr, int ch)
1319 int size;
1320 size = cstr->size + 1;
1321 if (size > cstr->size_allocated)
1322 cstr_realloc(cstr, size);
1323 ((unsigned char *)cstr->data)[size - 1] = ch;
1324 cstr->size = size;
1327 static void cstr_cat(CString *cstr, const char *str)
1329 int c;
1330 for(;;) {
1331 c = *str;
1332 if (c == '\0')
1333 break;
1334 cstr_ccat(cstr, c);
1335 str++;
1339 /* add a wide char */
1340 static void cstr_wccat(CString *cstr, int ch)
1342 int size;
1343 size = cstr->size + sizeof(int);
1344 if (size > cstr->size_allocated)
1345 cstr_realloc(cstr, size);
1346 *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
1347 cstr->size = size;
1350 static void cstr_new(CString *cstr)
1352 memset(cstr, 0, sizeof(CString));
1355 /* free string and reset it to NULL */
1356 static void cstr_free(CString *cstr)
1358 tcc_free(cstr->data_allocated);
1359 cstr_new(cstr);
1362 #define cstr_reset(cstr) cstr_free(cstr)
1364 static CString *cstr_dup(CString *cstr1)
1366 CString *cstr;
1367 int size;
1369 cstr = tcc_malloc(sizeof(CString));
1370 size = cstr1->size;
1371 cstr->size = size;
1372 cstr->size_allocated = size;
1373 cstr->data_allocated = tcc_malloc(size);
1374 cstr->data = cstr->data_allocated;
1375 memcpy(cstr->data_allocated, cstr1->data_allocated, size);
1376 return cstr;
1379 /* XXX: unicode ? */
1380 static void add_char(CString *cstr, int c)
1382 if (c == '\'' || c == '\"' || c == '\\') {
1383 /* XXX: could be more precise if char or string */
1384 cstr_ccat(cstr, '\\');
1386 if (c >= 32 && c <= 126) {
1387 cstr_ccat(cstr, c);
1388 } else {
1389 cstr_ccat(cstr, '\\');
1390 if (c == '\n') {
1391 cstr_ccat(cstr, 'n');
1392 } else {
1393 cstr_ccat(cstr, '0' + ((c >> 6) & 7));
1394 cstr_ccat(cstr, '0' + ((c >> 3) & 7));
1395 cstr_ccat(cstr, '0' + (c & 7));
1400 /* XXX: buffer overflow */
1401 /* XXX: float tokens */
1402 char *get_tok_str(int v, CValue *cv)
1404 static char buf[STRING_MAX_SIZE + 1];
1405 static CString cstr_buf;
1406 CString *cstr;
1407 unsigned char *q;
1408 char *p;
1409 int i, len;
1411 /* NOTE: to go faster, we give a fixed buffer for small strings */
1412 cstr_reset(&cstr_buf);
1413 cstr_buf.data = buf;
1414 cstr_buf.size_allocated = sizeof(buf);
1415 p = buf;
1417 switch(v) {
1418 case TOK_CINT:
1419 case TOK_CUINT:
1420 /* XXX: not quite exact, but only useful for testing */
1421 sprintf(p, "%u", cv->ui);
1422 break;
1423 case TOK_CLLONG:
1424 case TOK_CULLONG:
1425 /* XXX: not quite exact, but only useful for testing */
1426 sprintf(p, "%Lu", cv->ull);
1427 break;
1428 case TOK_CCHAR:
1429 case TOK_LCHAR:
1430 cstr_ccat(&cstr_buf, '\'');
1431 add_char(&cstr_buf, cv->i);
1432 cstr_ccat(&cstr_buf, '\'');
1433 cstr_ccat(&cstr_buf, '\0');
1434 break;
1435 case TOK_PPNUM:
1436 cstr = cv->cstr;
1437 len = cstr->size - 1;
1438 for(i=0;i<len;i++)
1439 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1440 cstr_ccat(&cstr_buf, '\0');
1441 break;
1442 case TOK_STR:
1443 case TOK_LSTR:
1444 cstr = cv->cstr;
1445 cstr_ccat(&cstr_buf, '\"');
1446 if (v == TOK_STR) {
1447 len = cstr->size - 1;
1448 for(i=0;i<len;i++)
1449 add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
1450 } else {
1451 len = (cstr->size / sizeof(int)) - 1;
1452 for(i=0;i<len;i++)
1453 add_char(&cstr_buf, ((int *)cstr->data)[i]);
1455 cstr_ccat(&cstr_buf, '\"');
1456 cstr_ccat(&cstr_buf, '\0');
1457 break;
1458 case TOK_LT:
1459 v = '<';
1460 goto addv;
1461 case TOK_GT:
1462 v = '>';
1463 goto addv;
1464 case TOK_A_SHL:
1465 return strcpy(p, "<<=");
1466 case TOK_A_SAR:
1467 return strcpy(p, ">>=");
1468 default:
1469 if (v < TOK_IDENT) {
1470 /* search in two bytes table */
1471 q = tok_two_chars;
1472 while (*q) {
1473 if (q[2] == v) {
1474 *p++ = q[0];
1475 *p++ = q[1];
1476 *p = '\0';
1477 return buf;
1479 q += 3;
1481 addv:
1482 *p++ = v;
1483 *p = '\0';
1484 } else if (v < tok_ident) {
1485 return table_ident[v - TOK_IDENT]->str;
1486 } else if (v >= SYM_FIRST_ANOM) {
1487 /* special name for anonymous symbol */
1488 sprintf(p, "L.%u", v - SYM_FIRST_ANOM);
1489 } else {
1490 /* should never happen */
1491 return NULL;
1493 break;
1495 return cstr_buf.data;
1498 /* push, without hashing */
1499 static Sym *sym_push2(Sym **ps, int v, int t, int c)
1501 Sym *s;
1502 s = tcc_malloc(sizeof(Sym));
1503 s->v = v;
1504 s->type.t = t;
1505 s->c = c;
1506 s->next = NULL;
1507 /* add in stack */
1508 s->prev = *ps;
1509 *ps = s;
1510 return s;
1513 /* find a symbol and return its associated structure. 's' is the top
1514 of the symbol stack */
1515 static Sym *sym_find2(Sym *s, int v)
1517 while (s) {
1518 if (s->v == v)
1519 return s;
1520 s = s->prev;
1522 return NULL;
1525 /* structure lookup */
1526 static inline Sym *struct_find(int v)
1528 v -= TOK_IDENT;
1529 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1530 return NULL;
1531 return table_ident[v]->sym_struct;
1534 /* find an identifier */
1535 static inline Sym *sym_find(int v)
1537 v -= TOK_IDENT;
1538 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
1539 return NULL;
1540 return table_ident[v]->sym_identifier;
1543 /* push a given symbol on the symbol stack */
1544 static Sym *sym_push(int v, CType *type, int r, int c)
1546 Sym *s, **ps;
1547 TokenSym *ts;
1549 if (local_stack)
1550 ps = &local_stack;
1551 else
1552 ps = &global_stack;
1553 s = sym_push2(ps, v, type->t, c);
1554 s->type.ref = type->ref;
1555 s->r = r;
1556 /* don't record fields or anonymous symbols */
1557 /* XXX: simplify */
1558 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1559 /* record symbol in token array */
1560 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1561 if (v & SYM_STRUCT)
1562 ps = &ts->sym_struct;
1563 else
1564 ps = &ts->sym_identifier;
1565 s->prev_tok = *ps;
1566 *ps = s;
1568 return s;
1571 /* push a global identifier */
1572 static Sym *global_identifier_push(int v, int t, int c)
1574 Sym *s, **ps;
1575 s = sym_push2(&global_stack, v, t, c);
1576 /* don't record anonymous symbol */
1577 if (v < SYM_FIRST_ANOM) {
1578 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
1579 /* modify the top most local identifier, so that
1580 sym_identifier will point to 's' when popped */
1581 while (*ps != NULL)
1582 ps = &(*ps)->prev_tok;
1583 s->prev_tok = NULL;
1584 *ps = s;
1586 return s;
1589 /* pop symbols until top reaches 'b' */
1590 static void sym_pop(Sym **ptop, Sym *b)
1592 Sym *s, *ss, **ps;
1593 TokenSym *ts;
1594 int v;
1596 s = *ptop;
1597 while(s != b) {
1598 ss = s->prev;
1599 v = s->v;
1600 /* remove symbol in token array */
1601 /* XXX: simplify */
1602 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
1603 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
1604 if (v & SYM_STRUCT)
1605 ps = &ts->sym_struct;
1606 else
1607 ps = &ts->sym_identifier;
1608 *ps = s->prev_tok;
1610 tcc_free(s);
1611 s = ss;
1613 *ptop = b;
1616 /* I/O layer */
1618 BufferedFile *tcc_open(TCCState *s1, const char *filename)
1620 int fd;
1621 BufferedFile *bf;
1623 fd = open(filename, O_RDONLY);
1624 if (fd < 0)
1625 return NULL;
1626 bf = tcc_malloc(sizeof(BufferedFile));
1627 if (!bf) {
1628 close(fd);
1629 return NULL;
1631 bf->fd = fd;
1632 bf->buf_ptr = bf->buffer;
1633 bf->buf_end = bf->buffer;
1634 bf->buffer[0] = CH_EOB; /* put eob symbol */
1635 pstrcpy(bf->filename, sizeof(bf->filename), filename);
1636 bf->line_num = 1;
1637 bf->ifndef_macro = 0;
1638 bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
1639 // printf("opening '%s'\n", filename);
1640 return bf;
1643 void tcc_close(BufferedFile *bf)
1645 total_lines += bf->line_num;
1646 close(bf->fd);
1647 tcc_free(bf);
1650 /* fill input buffer and peek next char */
1651 static int tcc_peekc_slow(BufferedFile *bf)
1653 int len;
1654 /* only tries to read if really end of buffer */
1655 if (bf->buf_ptr >= bf->buf_end) {
1656 if (bf->fd != -1) {
1657 #if defined(PARSE_DEBUG)
1658 len = 8;
1659 #else
1660 len = IO_BUF_SIZE;
1661 #endif
1662 len = read(bf->fd, bf->buffer, len);
1663 if (len < 0)
1664 len = 0;
1665 } else {
1666 len = 0;
1668 total_bytes += len;
1669 bf->buf_ptr = bf->buffer;
1670 bf->buf_end = bf->buffer + len;
1671 *bf->buf_end = CH_EOB;
1673 if (bf->buf_ptr < bf->buf_end) {
1674 return bf->buf_ptr[0];
1675 } else {
1676 bf->buf_ptr = bf->buf_end;
1677 return CH_EOF;
1681 /* return the current character, handling end of block if necessary
1682 (but not stray) */
1683 static int handle_eob(void)
1685 return tcc_peekc_slow(file);
1688 /* read next char from current input file and handle end of input buffer */
1689 static inline void inp(void)
1691 ch = *(++(file->buf_ptr));
1692 /* end of buffer/file handling */
1693 if (ch == CH_EOB)
1694 ch = handle_eob();
1697 /* handle '\[\r]\n' */
1698 static void handle_stray(void)
1700 while (ch == '\\') {
1701 inp();
1702 if (ch == '\n') {
1703 file->line_num++;
1704 inp();
1705 } else if (ch == '\r') {
1706 inp();
1707 if (ch != '\n')
1708 goto fail;
1709 file->line_num++;
1710 inp();
1711 } else {
1712 fail:
1713 error("stray '\\' in program");
1718 /* skip the stray and handle the \\n case. Output an error if
1719 incorrect char after the stray */
1720 static int handle_stray1(uint8_t *p)
1722 int c;
1724 if (p >= file->buf_end) {
1725 file->buf_ptr = p;
1726 c = handle_eob();
1727 p = file->buf_ptr;
1728 if (c == '\\')
1729 goto parse_stray;
1730 } else {
1731 parse_stray:
1732 file->buf_ptr = p;
1733 ch = *p;
1734 handle_stray();
1735 p = file->buf_ptr;
1736 c = *p;
1738 return c;
1741 /* handle just the EOB case, but not stray */
1742 #define PEEKC_EOB(c, p)\
1744 p++;\
1745 c = *p;\
1746 if (c == '\\') {\
1747 file->buf_ptr = p;\
1748 c = handle_eob();\
1749 p = file->buf_ptr;\
1753 /* handle the complicated stray case */
1754 #define PEEKC(c, p)\
1756 p++;\
1757 c = *p;\
1758 if (c == '\\') {\
1759 c = handle_stray1(p);\
1760 p = file->buf_ptr;\
1764 /* input with '\[\r]\n' handling. Note that this function cannot
1765 handle other characters after '\', so you cannot call it inside
1766 strings or comments */
1767 static void minp(void)
1769 inp();
1770 if (ch == '\\')
1771 handle_stray();
1775 /* single line C++ comments */
1776 static uint8_t *parse_line_comment(uint8_t *p)
1778 int c;
1780 p++;
1781 for(;;) {
1782 c = *p;
1783 if (c == '\n' || c == CH_EOF) {
1784 break;
1785 } else if (c == '\\') {
1786 PEEKC_EOB(c, p);
1787 if (c == '\n') {
1788 file->line_num++;
1789 PEEKC_EOB(c, p);
1790 } else if (c == '\r') {
1791 PEEKC_EOB(c, p);
1792 if (c == '\n') {
1793 file->line_num++;
1794 PEEKC_EOB(c, p);
1797 } else {
1798 p++;
1801 return p;
1804 /* C comments */
1805 static uint8_t *parse_comment(uint8_t *p)
1807 int c;
1809 p++;
1810 for(;;) {
1811 /* fast skip loop */
1812 for(;;) {
1813 c = *p;
1814 if (c == '\n' || c == '*' || c == '\\')
1815 break;
1816 p++;
1817 c = *p;
1818 if (c == '\n' || c == '*' || c == '\\')
1819 break;
1820 p++;
1822 /* now we can handle all the cases */
1823 if (c == '\n') {
1824 file->line_num++;
1825 p++;
1826 } else if (c == '*') {
1827 p++;
1828 for(;;) {
1829 c = *p;
1830 if (c == '*') {
1831 p++;
1832 } else if (c == '/') {
1833 goto end_of_comment;
1834 } else if (c == '\\') {
1835 file->buf_ptr = p;
1836 c = handle_eob();
1837 p = file->buf_ptr;
1838 if (c == '\\') {
1839 /* skip '\[\r]\n', otherwise just skip the stray */
1840 while (c == '\\') {
1841 PEEKC_EOB(c, p);
1842 if (c == '\n') {
1843 file->line_num++;
1844 PEEKC_EOB(c, p);
1845 } else if (c == '\r') {
1846 PEEKC_EOB(c, p);
1847 if (c == '\n') {
1848 file->line_num++;
1849 PEEKC_EOB(c, p);
1851 } else {
1852 goto after_star;
1856 } else {
1857 break;
1860 after_star: ;
1861 } else {
1862 /* stray, eob or eof */
1863 file->buf_ptr = p;
1864 c = handle_eob();
1865 p = file->buf_ptr;
1866 if (c == CH_EOF) {
1867 error("unexpected end of file in comment");
1868 } else if (c == '\\') {
1869 p++;
1873 end_of_comment:
1874 p++;
1875 return p;
1878 #define cinp minp
1880 /* space exlcuding newline */
1881 static inline int is_space(int ch)
1883 return ch == ' ' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r';
1886 static inline void skip_spaces(void)
1888 while (is_space(ch))
1889 cinp();
1892 /* parse a string without interpreting escapes */
1893 static uint8_t *parse_pp_string(uint8_t *p,
1894 int sep, CString *str)
1896 int c;
1897 p++;
1898 for(;;) {
1899 c = *p;
1900 if (c == sep) {
1901 break;
1902 } else if (c == '\\') {
1903 file->buf_ptr = p;
1904 c = handle_eob();
1905 p = file->buf_ptr;
1906 if (c == CH_EOF) {
1907 unterminated_string:
1908 /* XXX: indicate line number of start of string */
1909 error("missing terminating %c character", sep);
1910 } else if (c == '\\') {
1911 /* escape : just skip \[\r]\n */
1912 PEEKC_EOB(c, p);
1913 if (c == '\n') {
1914 file->line_num++;
1915 p++;
1916 } else if (c == '\r') {
1917 PEEKC_EOB(c, p);
1918 if (c != '\n')
1919 expect("'\n' after '\r'");
1920 file->line_num++;
1921 p++;
1922 } else if (c == CH_EOF) {
1923 goto unterminated_string;
1924 } else {
1925 if (str) {
1926 cstr_ccat(str, '\\');
1927 cstr_ccat(str, c);
1929 p++;
1932 } else if (c == '\n') {
1933 file->line_num++;
1934 goto add_char;
1935 } else if (c == '\r') {
1936 PEEKC_EOB(c, p);
1937 if (c != '\n') {
1938 cstr_ccat(str, '\r');
1939 } else {
1940 file->line_num++;
1941 goto add_char;
1943 } else {
1944 add_char:
1945 if (str)
1946 cstr_ccat(str, c);
1947 p++;
1950 p++;
1951 return p;
1954 /* skip block of text until #else, #elif or #endif. skip also pairs of
1955 #if/#endif */
1956 void preprocess_skip(void)
1958 int a, start_of_line, c;
1959 uint8_t *p;
1961 p = file->buf_ptr;
1962 start_of_line = 1;
1963 a = 0;
1964 for(;;) {
1965 redo_no_start:
1966 c = *p;
1967 switch(c) {
1968 case ' ':
1969 case '\t':
1970 case '\f':
1971 case '\v':
1972 case '\r':
1973 p++;
1974 goto redo_no_start;
1975 case '\n':
1976 start_of_line = 1;
1977 file->line_num++;
1978 p++;
1979 goto redo_no_start;
1980 case '\\':
1981 file->buf_ptr = p;
1982 c = handle_eob();
1983 if (c == CH_EOF) {
1984 expect("#endif");
1985 } else if (c == '\\') {
1986 /* XXX: incorrect: should not give an error */
1987 ch = file->buf_ptr[0];
1988 handle_stray();
1990 p = file->buf_ptr;
1991 goto redo_no_start;
1992 /* skip strings */
1993 case '\"':
1994 case '\'':
1995 p = parse_pp_string(p, c, NULL);
1996 break;
1997 /* skip comments */
1998 case '/':
1999 file->buf_ptr = p;
2000 ch = *p;
2001 minp();
2002 p = file->buf_ptr;
2003 if (ch == '*') {
2004 p = parse_comment(p);
2005 } else if (ch == '/') {
2006 p = parse_line_comment(p);
2008 break;
2010 case '#':
2011 p++;
2012 if (start_of_line) {
2013 file->buf_ptr = p;
2014 next_nomacro();
2015 p = file->buf_ptr;
2016 if (a == 0 &&
2017 (tok == TOK_ELSE || tok == TOK_ELIF || tok == TOK_ENDIF))
2018 goto the_end;
2019 if (tok == TOK_IF || tok == TOK_IFDEF || tok == TOK_IFNDEF)
2020 a++;
2021 else if (tok == TOK_ENDIF)
2022 a--;
2024 break;
2025 default:
2026 p++;
2027 break;
2029 start_of_line = 0;
2031 the_end: ;
2032 file->buf_ptr = p;
2035 /* ParseState handling */
2037 /* XXX: currently, no include file info is stored. Thus, we cannot display
2038 accurate messages if the function or data definition spans multiple
2039 files */
2041 /* save current parse state in 's' */
2042 void save_parse_state(ParseState *s)
2044 s->line_num = file->line_num;
2045 s->macro_ptr = macro_ptr;
2046 s->tok = tok;
2047 s->tokc = tokc;
2050 /* restore parse state from 's' */
2051 void restore_parse_state(ParseState *s)
2053 file->line_num = s->line_num;
2054 macro_ptr = s->macro_ptr;
2055 tok = s->tok;
2056 tokc = s->tokc;
2059 /* return the number of additional 'ints' necessary to store the
2060 token */
2061 static inline int tok_ext_size(int t)
2063 switch(t) {
2064 /* 4 bytes */
2065 case TOK_CINT:
2066 case TOK_CUINT:
2067 case TOK_CCHAR:
2068 case TOK_LCHAR:
2069 case TOK_STR:
2070 case TOK_LSTR:
2071 case TOK_CFLOAT:
2072 case TOK_LINENUM:
2073 case TOK_PPNUM:
2074 return 1;
2075 case TOK_CDOUBLE:
2076 case TOK_CLLONG:
2077 case TOK_CULLONG:
2078 return 2;
2079 case TOK_CLDOUBLE:
2080 return LDOUBLE_SIZE / 4;
2081 default:
2082 return 0;
2086 /* token string handling */
2088 static inline void tok_str_new(TokenString *s)
2090 s->str = NULL;
2091 s->len = 0;
2092 s->allocated_len = 0;
2093 s->last_line_num = -1;
2096 static void tok_str_free(int *str)
2098 const int *p;
2099 CString *cstr;
2100 int t;
2102 p = str;
2103 for(;;) {
2104 t = *p;
2105 /* NOTE: we test zero separately so that GCC can generate a
2106 table for the following switch */
2107 if (t == 0)
2108 break;
2109 switch(t) {
2110 case TOK_CINT:
2111 case TOK_CUINT:
2112 case TOK_CCHAR:
2113 case TOK_LCHAR:
2114 case TOK_CFLOAT:
2115 case TOK_LINENUM:
2116 p += 2;
2117 break;
2118 case TOK_PPNUM:
2119 case TOK_STR:
2120 case TOK_LSTR:
2121 /* XXX: use a macro to be portable on 64 bit ? */
2122 cstr = (CString *)p[1];
2123 cstr_free(cstr);
2124 tcc_free(cstr);
2125 p += 2;
2126 break;
2127 case TOK_CDOUBLE:
2128 case TOK_CLLONG:
2129 case TOK_CULLONG:
2130 p += 3;
2131 break;
2132 case TOK_CLDOUBLE:
2133 p += 1 + (LDOUBLE_SIZE / 4);
2134 break;
2135 default:
2136 p++;
2137 break;
2140 tcc_free(str);
2143 static int *tok_str_realloc(TokenString *s)
2145 int *str, len;
2147 len = s->allocated_len + TOK_STR_ALLOC_INCR;
2148 str = tcc_realloc(s->str, len * sizeof(int));
2149 if (!str)
2150 error("memory full");
2151 s->allocated_len = len;
2152 s->str = str;
2153 return str;
2156 static void tok_str_add(TokenString *s, int t)
2158 int len, *str;
2160 len = s->len;
2161 str = s->str;
2162 if (len >= s->allocated_len)
2163 str = tok_str_realloc(s);
2164 str[len++] = t;
2165 s->len = len;
2168 static void tok_str_add2(TokenString *s, int t, CValue *cv)
2170 int len, *str;
2172 len = s->len;
2173 str = s->str;
2175 /* allocate space for worst case */
2176 if (len + TOK_MAX_SIZE > s->allocated_len)
2177 str = tok_str_realloc(s);
2178 str[len++] = t;
2179 switch(t) {
2180 case TOK_CINT:
2181 case TOK_CUINT:
2182 case TOK_CCHAR:
2183 case TOK_LCHAR:
2184 case TOK_CFLOAT:
2185 case TOK_LINENUM:
2186 str[len++] = cv->tab[0];
2187 break;
2188 case TOK_PPNUM:
2189 case TOK_STR:
2190 case TOK_LSTR:
2191 str[len++] = (int)cstr_dup(cv->cstr);
2192 break;
2193 case TOK_CDOUBLE:
2194 case TOK_CLLONG:
2195 case TOK_CULLONG:
2196 str[len++] = cv->tab[0];
2197 str[len++] = cv->tab[1];
2198 break;
2199 case TOK_CLDOUBLE:
2200 #if LDOUBLE_SIZE == 12
2201 str[len++] = cv->tab[0];
2202 str[len++] = cv->tab[1];
2203 str[len++] = cv->tab[2];
2204 #else
2205 #error add long double size support
2206 #endif
2207 break;
2208 default:
2209 break;
2211 s->len = len;
2214 /* add the current parse token in token string 's' */
2215 static void tok_str_add_tok(TokenString *s)
2217 CValue cval;
2219 /* save line number info */
2220 if (file->line_num != s->last_line_num) {
2221 s->last_line_num = file->line_num;
2222 cval.i = s->last_line_num;
2223 tok_str_add2(s, TOK_LINENUM, &cval);
2225 tok_str_add2(s, tok, &tokc);
2228 #if LDOUBLE_SIZE == 12
2229 #define LDOUBLE_GET(p, cv) \
2230 cv.tab[0] = p[0]; \
2231 cv.tab[1] = p[1]; \
2232 cv.tab[2] = p[2];
2233 #else
2234 #error add long double size support
2235 #endif
2238 /* get a token from an integer array and increment pointer
2239 accordingly. we code it as a macro to avoid pointer aliasing. */
2240 #define TOK_GET(t, p, cv) \
2242 t = *p++; \
2243 switch(t) { \
2244 case TOK_CINT: \
2245 case TOK_CUINT: \
2246 case TOK_CCHAR: \
2247 case TOK_LCHAR: \
2248 case TOK_CFLOAT: \
2249 case TOK_LINENUM: \
2250 case TOK_STR: \
2251 case TOK_LSTR: \
2252 case TOK_PPNUM: \
2253 cv.tab[0] = *p++; \
2254 break; \
2255 case TOK_CDOUBLE: \
2256 case TOK_CLLONG: \
2257 case TOK_CULLONG: \
2258 cv.tab[0] = p[0]; \
2259 cv.tab[1] = p[1]; \
2260 p += 2; \
2261 break; \
2262 case TOK_CLDOUBLE: \
2263 LDOUBLE_GET(p, cv); \
2264 p += LDOUBLE_SIZE / 4; \
2265 break; \
2266 default: \
2267 break; \
2271 /* defines handling */
2272 static inline void define_push(int v, int macro_type, int *str, Sym *first_arg)
2274 Sym *s;
2276 s = sym_push2(&define_stack, v, macro_type, (int)str);
2277 s->next = first_arg;
2278 table_ident[v - TOK_IDENT]->sym_define = s;
2281 /* undefined a define symbol. Its name is just set to zero */
2282 static void define_undef(Sym *s)
2284 int v;
2285 v = s->v;
2286 if (v >= TOK_IDENT && v < tok_ident)
2287 table_ident[v - TOK_IDENT]->sym_define = NULL;
2288 s->v = 0;
2291 static inline Sym *define_find(int v)
2293 v -= TOK_IDENT;
2294 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2295 return NULL;
2296 return table_ident[v]->sym_define;
2299 /* free define stack until top reaches 'b' */
2300 static void free_defines(Sym *b)
2302 Sym *top, *top1;
2303 int v;
2305 top = define_stack;
2306 while (top != b) {
2307 top1 = top->prev;
2308 /* do not free args or predefined defines */
2309 if (top->c)
2310 tok_str_free((int *)top->c);
2311 v = top->v;
2312 if (v >= TOK_IDENT && v < tok_ident)
2313 table_ident[v - TOK_IDENT]->sym_define = NULL;
2314 tcc_free(top);
2315 top = top1;
2317 define_stack = b;
2320 /* label lookup */
2321 static Sym *label_find(int v)
2323 v -= TOK_IDENT;
2324 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
2325 return NULL;
2326 return table_ident[v]->sym_label;
2329 static Sym *label_push(Sym **ptop, int v, int flags)
2331 Sym *s, **ps;
2332 s = sym_push2(ptop, v, 0, 0);
2333 s->r = flags;
2334 ps = &table_ident[v - TOK_IDENT]->sym_label;
2335 if (ptop == &global_label_stack) {
2336 /* modify the top most local identifier, so that
2337 sym_identifier will point to 's' when popped */
2338 while (*ps != NULL)
2339 ps = &(*ps)->prev_tok;
2341 s->prev_tok = *ps;
2342 *ps = s;
2343 return s;
2346 /* pop labels until element last is reached. Look if any labels are
2347 undefined. Define symbols if '&&label' was used. */
2348 static void label_pop(Sym **ptop, Sym *slast)
2350 Sym *s, *s1;
2351 for(s = *ptop; s != slast; s = s1) {
2352 s1 = s->prev;
2353 if (s->r == LABEL_DECLARED) {
2354 warning("label '%s' declared but not used", get_tok_str(s->v, NULL));
2355 } else if (s->r == LABEL_FORWARD) {
2356 error("label '%s' used but not defined",
2357 get_tok_str(s->v, NULL));
2358 } else {
2359 if (s->c) {
2360 /* define corresponding symbol. A size of
2361 1 is put. */
2362 put_extern_sym(s, cur_text_section, (long)s->next, 1);
2365 /* remove label */
2366 table_ident[s->v - TOK_IDENT]->sym_label = s->prev_tok;
2367 tcc_free(s);
2369 *ptop = slast;
2372 /* eval an expression for #if/#elif */
2373 static int expr_preprocess(void)
2375 int c, t;
2376 TokenString str;
2378 tok_str_new(&str);
2379 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2380 next(); /* do macro subst */
2381 if (tok == TOK_DEFINED) {
2382 next_nomacro();
2383 t = tok;
2384 if (t == '(')
2385 next_nomacro();
2386 c = define_find(tok) != 0;
2387 if (t == '(')
2388 next_nomacro();
2389 tok = TOK_CINT;
2390 tokc.i = c;
2391 } else if (tok >= TOK_IDENT) {
2392 /* if undefined macro */
2393 tok = TOK_CINT;
2394 tokc.i = 0;
2396 tok_str_add_tok(&str);
2398 tok_str_add(&str, -1); /* simulate end of file */
2399 tok_str_add(&str, 0);
2400 /* now evaluate C constant expression */
2401 macro_ptr = str.str;
2402 next();
2403 c = expr_const();
2404 macro_ptr = NULL;
2405 tok_str_free(str.str);
2406 return c != 0;
2409 #if defined(PARSE_DEBUG) || defined(PP_DEBUG)
2410 static void tok_print(int *str)
2412 int t;
2413 CValue cval;
2415 while (1) {
2416 TOK_GET(t, str, cval);
2417 if (!t)
2418 break;
2419 printf(" %s", get_tok_str(t, &cval));
2421 printf("\n");
2423 #endif
2425 /* parse after #define */
2426 static void parse_define(void)
2428 Sym *s, *first, **ps;
2429 int v, t, varg, is_vaargs, c;
2430 TokenString str;
2432 v = tok;
2433 if (v < TOK_IDENT)
2434 error("invalid macro name '%s'", get_tok_str(tok, &tokc));
2435 /* XXX: should check if same macro (ANSI) */
2436 first = NULL;
2437 t = MACRO_OBJ;
2438 /* '(' must be just after macro definition for MACRO_FUNC */
2439 c = file->buf_ptr[0];
2440 if (c == '\\')
2441 c = handle_stray1(file->buf_ptr);
2442 if (c == '(') {
2443 next_nomacro();
2444 next_nomacro();
2445 ps = &first;
2446 while (tok != ')') {
2447 varg = tok;
2448 next_nomacro();
2449 is_vaargs = 0;
2450 if (varg == TOK_DOTS) {
2451 varg = TOK___VA_ARGS__;
2452 is_vaargs = 1;
2453 } else if (tok == TOK_DOTS && gnu_ext) {
2454 is_vaargs = 1;
2455 next_nomacro();
2457 if (varg < TOK_IDENT)
2458 error("badly punctuated parameter list");
2459 s = sym_push2(&define_stack, varg | SYM_FIELD, is_vaargs, 0);
2460 *ps = s;
2461 ps = &s->next;
2462 if (tok != ',')
2463 break;
2464 next_nomacro();
2466 t = MACRO_FUNC;
2468 tok_str_new(&str);
2469 next_nomacro();
2470 /* EOF testing necessary for '-D' handling */
2471 while (tok != TOK_LINEFEED && tok != TOK_EOF) {
2472 tok_str_add2(&str, tok, &tokc);
2473 next_nomacro();
2475 tok_str_add(&str, 0);
2476 #ifdef PP_DEBUG
2477 printf("define %s %d: ", get_tok_str(v, NULL), t);
2478 tok_print(str.str);
2479 #endif
2480 define_push(v, t, str.str, first);
2483 /* XXX: use a token or a hash table to accelerate matching ? */
2484 static CachedInclude *search_cached_include(TCCState *s1,
2485 int type, const char *filename)
2487 CachedInclude *e;
2488 int i;
2490 for(i = 0;i < s1->nb_cached_includes; i++) {
2491 e = s1->cached_includes[i];
2492 if (e->type == type && !strcmp(e->filename, filename))
2493 return e;
2495 return NULL;
2498 static inline void add_cached_include(TCCState *s1, int type,
2499 const char *filename, int ifndef_macro)
2501 CachedInclude *e;
2503 if (search_cached_include(s1, type, filename))
2504 return;
2505 #ifdef INC_DEBUG
2506 printf("adding cached '%s' %s\n", filename, get_tok_str(ifndef_macro, NULL));
2507 #endif
2508 e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
2509 if (!e)
2510 return;
2511 e->type = type;
2512 strcpy(e->filename, filename);
2513 e->ifndef_macro = ifndef_macro;
2514 dynarray_add((void ***)&s1->cached_includes, &s1->nb_cached_includes, e);
2517 /* is_bof is true if first non space token at beginning of file */
2518 static void preprocess(int is_bof)
2520 TCCState *s1 = tcc_state;
2521 int size, i, c, n, saved_parse_flags;
2522 char buf[1024], *q, *p;
2523 char buf1[1024];
2524 BufferedFile *f;
2525 Sym *s;
2526 CachedInclude *e;
2528 saved_parse_flags = parse_flags;
2529 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM |
2530 PARSE_FLAG_LINEFEED;
2531 next_nomacro();
2532 redo:
2533 switch(tok) {
2534 case TOK_DEFINE:
2535 next_nomacro();
2536 parse_define();
2537 break;
2538 case TOK_UNDEF:
2539 next_nomacro();
2540 s = define_find(tok);
2541 /* undefine symbol by putting an invalid name */
2542 if (s)
2543 define_undef(s);
2544 break;
2545 case TOK_INCLUDE:
2546 ch = file->buf_ptr[0];
2547 /* XXX: incorrect if comments : use next_nomacro with a special mode */
2548 skip_spaces();
2549 if (ch == '<') {
2550 c = '>';
2551 goto read_name;
2552 } else if (ch == '\"') {
2553 c = ch;
2554 read_name:
2555 /* XXX: better stray handling */
2556 minp();
2557 q = buf;
2558 while (ch != c && ch != '\n' && ch != CH_EOF) {
2559 if ((q - buf) < sizeof(buf) - 1)
2560 *q++ = ch;
2561 minp();
2563 *q = '\0';
2564 minp();
2565 #if 0
2566 /* eat all spaces and comments after include */
2567 /* XXX: slightly incorrect */
2568 while (ch1 != '\n' && ch1 != CH_EOF)
2569 inp();
2570 #endif
2571 } else {
2572 /* computed #include : either we have only strings or
2573 we have anything enclosed in '<>' */
2574 next();
2575 buf[0] = '\0';
2576 if (tok == TOK_STR) {
2577 while (tok != TOK_LINEFEED) {
2578 if (tok != TOK_STR) {
2579 include_syntax:
2580 error("'#include' expects \"FILENAME\" or <FILENAME>");
2582 pstrcat(buf, sizeof(buf), (char *)tokc.cstr->data);
2583 next();
2585 c = '\"';
2586 } else {
2587 int len;
2588 while (tok != TOK_LINEFEED) {
2589 pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
2590 next();
2592 len = strlen(buf);
2593 /* check syntax and remove '<>' */
2594 if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
2595 goto include_syntax;
2596 memmove(buf, buf + 1, len - 2);
2597 buf[len - 2] = '\0';
2598 c = '>';
2602 e = search_cached_include(s1, c, buf);
2603 if (e && define_find(e->ifndef_macro)) {
2604 /* no need to parse the include because the 'ifndef macro'
2605 is defined */
2606 #ifdef INC_DEBUG
2607 printf("%s: skipping %s\n", file->filename, buf);
2608 #endif
2609 } else {
2610 if (c == '\"') {
2611 /* first search in current dir if "header.h" */
2612 size = 0;
2613 p = strrchr(file->filename, '/');
2614 if (p)
2615 size = p + 1 - file->filename;
2616 if (size > sizeof(buf1) - 1)
2617 size = sizeof(buf1) - 1;
2618 memcpy(buf1, file->filename, size);
2619 buf1[size] = '\0';
2620 pstrcat(buf1, sizeof(buf1), buf);
2621 f = tcc_open(s1, buf1);
2622 if (f)
2623 goto found;
2625 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
2626 error("#include recursion too deep");
2627 /* now search in all the include paths */
2628 n = s1->nb_include_paths + s1->nb_sysinclude_paths;
2629 for(i = 0; i < n; i++) {
2630 const char *path;
2631 if (i < s1->nb_include_paths)
2632 path = s1->include_paths[i];
2633 else
2634 path = s1->sysinclude_paths[i - s1->nb_include_paths];
2635 pstrcpy(buf1, sizeof(buf1), path);
2636 pstrcat(buf1, sizeof(buf1), "/");
2637 pstrcat(buf1, sizeof(buf1), buf);
2638 f = tcc_open(s1, buf1);
2639 if (f)
2640 goto found;
2642 error("include file '%s' not found", buf);
2643 f = NULL;
2644 found:
2645 #ifdef INC_DEBUG
2646 printf("%s: including %s\n", file->filename, buf1);
2647 #endif
2648 f->inc_type = c;
2649 pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
2650 /* push current file in stack */
2651 /* XXX: fix current line init */
2652 *s1->include_stack_ptr++ = file;
2653 file = f;
2654 /* add include file debug info */
2655 if (do_debug) {
2656 put_stabs(file->filename, N_BINCL, 0, 0, 0);
2658 tok_flags |= TOK_FLAG_BOF | TOK_FLAG_BOL;
2659 ch = file->buf_ptr[0];
2660 goto the_end;
2662 break;
2663 case TOK_IFNDEF:
2664 c = 1;
2665 goto do_ifdef;
2666 case TOK_IF:
2667 c = expr_preprocess();
2668 goto do_if;
2669 case TOK_IFDEF:
2670 c = 0;
2671 do_ifdef:
2672 next_nomacro();
2673 if (tok < TOK_IDENT)
2674 error("invalid argument for '#if%sdef'", c ? "n" : "");
2675 if (is_bof) {
2676 if (c) {
2677 #ifdef INC_DEBUG
2678 printf("#ifndef %s\n", get_tok_str(tok, NULL));
2679 #endif
2680 file->ifndef_macro = tok;
2683 c = (define_find(tok) != 0) ^ c;
2684 do_if:
2685 if (s1->ifdef_stack_ptr >= s1->ifdef_stack + IFDEF_STACK_SIZE)
2686 error("memory full");
2687 *s1->ifdef_stack_ptr++ = c;
2688 goto test_skip;
2689 case TOK_ELSE:
2690 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2691 error("#else without matching #if");
2692 if (s1->ifdef_stack_ptr[-1] & 2)
2693 error("#else after #else");
2694 c = (s1->ifdef_stack_ptr[-1] ^= 3);
2695 goto test_skip;
2696 case TOK_ELIF:
2697 if (s1->ifdef_stack_ptr == s1->ifdef_stack)
2698 error("#elif without matching #if");
2699 c = s1->ifdef_stack_ptr[-1];
2700 if (c > 1)
2701 error("#elif after #else");
2702 /* last #if/#elif expression was true: we skip */
2703 if (c == 1)
2704 goto skip;
2705 c = expr_preprocess();
2706 s1->ifdef_stack_ptr[-1] = c;
2707 test_skip:
2708 if (!(c & 1)) {
2709 skip:
2710 preprocess_skip();
2711 is_bof = 0;
2712 goto redo;
2714 break;
2715 case TOK_ENDIF:
2716 if (s1->ifdef_stack_ptr <= file->ifdef_stack_ptr)
2717 error("#endif without matching #if");
2718 s1->ifdef_stack_ptr--;
2719 /* '#ifndef macro' was at the start of file. Now we check if
2720 an '#endif' is exactly at the end of file */
2721 if (file->ifndef_macro &&
2722 s1->ifdef_stack_ptr == file->ifdef_stack_ptr) {
2723 file->ifndef_macro_saved = file->ifndef_macro;
2724 /* need to set to zero to avoid false matches if another
2725 #ifndef at middle of file */
2726 file->ifndef_macro = 0;
2727 while (tok != TOK_LINEFEED)
2728 next_nomacro();
2729 tok_flags |= TOK_FLAG_ENDIF;
2730 goto the_end;
2732 break;
2733 case TOK_LINE:
2734 next();
2735 if (tok != TOK_CINT)
2736 error("#line");
2737 file->line_num = tokc.i - 1; /* the line number will be incremented after */
2738 next();
2739 if (tok != TOK_LINEFEED) {
2740 if (tok != TOK_STR)
2741 error("#line");
2742 pstrcpy(file->filename, sizeof(file->filename),
2743 (char *)tokc.cstr->data);
2745 break;
2746 case TOK_ERROR:
2747 case TOK_WARNING:
2748 c = tok;
2749 ch = file->buf_ptr[0];
2750 skip_spaces();
2751 q = buf;
2752 while (ch != '\n' && ch != CH_EOF) {
2753 if ((q - buf) < sizeof(buf) - 1)
2754 *q++ = ch;
2755 minp();
2757 *q = '\0';
2758 if (c == TOK_ERROR)
2759 error("#error %s", buf);
2760 else
2761 warning("#warning %s", buf);
2762 break;
2763 case TOK_PRAGMA:
2764 /* ignored */
2765 break;
2766 default:
2767 if (tok == TOK_LINEFEED || tok == '!' || tok == TOK_CINT) {
2768 /* '!' is ignored to allow C scripts. numbers are ignored
2769 to emulate cpp behaviour */
2770 } else {
2771 error("invalid preprocessing directive #%s", get_tok_str(tok, &tokc));
2773 break;
2775 /* ignore other preprocess commands or #! for C scripts */
2776 while (tok != TOK_LINEFEED)
2777 next_nomacro();
2778 the_end:
2779 parse_flags = saved_parse_flags;
2782 /* evaluate escape codes in a string. */
2783 static void parse_escape_string(CString *outstr, const uint8_t *buf, int is_long)
2785 int c, n;
2786 const char *p;
2788 p = buf;
2789 for(;;) {
2790 c = *p;
2791 if (c == '\0')
2792 break;
2793 if (c == '\\') {
2794 p++;
2795 /* escape */
2796 c = *p;
2797 switch(c) {
2798 case '0': case '1': case '2': case '3':
2799 case '4': case '5': case '6': case '7':
2800 /* at most three octal digits */
2801 n = c - '0';
2802 p++;
2803 c = *p;
2804 if (isoct(c)) {
2805 n = n * 8 + c - '0';
2806 p++;
2807 c = *p;
2808 if (isoct(c)) {
2809 n = n * 8 + c - '0';
2810 p++;
2813 c = n;
2814 goto add_char_nonext;
2815 case 'x':
2816 p++;
2817 n = 0;
2818 for(;;) {
2819 c = *p;
2820 if (c >= 'a' && c <= 'f')
2821 c = c - 'a' + 10;
2822 else if (c >= 'A' && c <= 'F')
2823 c = c - 'A' + 10;
2824 else if (isnum(c))
2825 c = c - '0';
2826 else
2827 break;
2828 n = n * 16 + c;
2829 p++;
2831 c = n;
2832 goto add_char_nonext;
2833 case 'a':
2834 c = '\a';
2835 break;
2836 case 'b':
2837 c = '\b';
2838 break;
2839 case 'f':
2840 c = '\f';
2841 break;
2842 case 'n':
2843 c = '\n';
2844 break;
2845 case 'r':
2846 c = '\r';
2847 break;
2848 case 't':
2849 c = '\t';
2850 break;
2851 case 'v':
2852 c = '\v';
2853 break;
2854 case 'e':
2855 if (!gnu_ext)
2856 goto invalid_escape;
2857 c = 27;
2858 break;
2859 case '\'':
2860 case '\"':
2861 case '\\':
2862 case '?':
2863 break;
2864 default:
2865 invalid_escape:
2866 error("invalid escaped char");
2869 p++;
2870 add_char_nonext:
2871 if (!is_long)
2872 cstr_ccat(outstr, c);
2873 else
2874 cstr_wccat(outstr, c);
2876 /* add a trailing '\0' */
2877 if (!is_long)
2878 cstr_ccat(outstr, '\0');
2879 else
2880 cstr_wccat(outstr, '\0');
2883 /* we use 64 bit numbers */
2884 #define BN_SIZE 2
2886 /* bn = (bn << shift) | or_val */
2887 void bn_lshift(unsigned int *bn, int shift, int or_val)
2889 int i;
2890 unsigned int v;
2891 for(i=0;i<BN_SIZE;i++) {
2892 v = bn[i];
2893 bn[i] = (v << shift) | or_val;
2894 or_val = v >> (32 - shift);
2898 void bn_zero(unsigned int *bn)
2900 int i;
2901 for(i=0;i<BN_SIZE;i++) {
2902 bn[i] = 0;
2906 /* parse number in null terminated string 'p' and return it in the
2907 current token */
2908 void parse_number(const char *p)
2910 int b, t, shift, frac_bits, s, exp_val, ch;
2911 char *q;
2912 unsigned int bn[BN_SIZE];
2913 double d;
2915 /* number */
2916 q = token_buf;
2917 ch = *p++;
2918 t = ch;
2919 ch = *p++;
2920 *q++ = t;
2921 b = 10;
2922 if (t == '.') {
2923 goto float_frac_parse;
2924 } else if (t == '0') {
2925 if (ch == 'x' || ch == 'X') {
2926 q--;
2927 ch = *p++;
2928 b = 16;
2929 } else if (tcc_ext && (ch == 'b' || ch == 'B')) {
2930 q--;
2931 ch = *p++;
2932 b = 2;
2935 /* parse all digits. cannot check octal numbers at this stage
2936 because of floating point constants */
2937 while (1) {
2938 if (ch >= 'a' && ch <= 'f')
2939 t = ch - 'a' + 10;
2940 else if (ch >= 'A' && ch <= 'F')
2941 t = ch - 'A' + 10;
2942 else if (isnum(ch))
2943 t = ch - '0';
2944 else
2945 break;
2946 if (t >= b)
2947 break;
2948 if (q >= token_buf + STRING_MAX_SIZE) {
2949 num_too_long:
2950 error("number too long");
2952 *q++ = ch;
2953 ch = *p++;
2955 if (ch == '.' ||
2956 ((ch == 'e' || ch == 'E') && b == 10) ||
2957 ((ch == 'p' || ch == 'P') && (b == 16 || b == 2))) {
2958 if (b != 10) {
2959 /* NOTE: strtox should support that for hexa numbers, but
2960 non ISOC99 libcs do not support it, so we prefer to do
2961 it by hand */
2962 /* hexadecimal or binary floats */
2963 /* XXX: handle overflows */
2964 *q = '\0';
2965 if (b == 16)
2966 shift = 4;
2967 else
2968 shift = 2;
2969 bn_zero(bn);
2970 q = token_buf;
2971 while (1) {
2972 t = *q++;
2973 if (t == '\0') {
2974 break;
2975 } else if (t >= 'a') {
2976 t = t - 'a' + 10;
2977 } else if (t >= 'A') {
2978 t = t - 'A' + 10;
2979 } else {
2980 t = t - '0';
2982 bn_lshift(bn, shift, t);
2984 frac_bits = 0;
2985 if (ch == '.') {
2986 ch = *p++;
2987 while (1) {
2988 t = ch;
2989 if (t >= 'a' && t <= 'f') {
2990 t = t - 'a' + 10;
2991 } else if (t >= 'A' && t <= 'F') {
2992 t = t - 'A' + 10;
2993 } else if (t >= '0' && t <= '9') {
2994 t = t - '0';
2995 } else {
2996 break;
2998 if (t >= b)
2999 error("invalid digit");
3000 bn_lshift(bn, shift, t);
3001 frac_bits += shift;
3002 ch = *p++;
3005 if (ch != 'p' && ch != 'P')
3006 expect("exponent");
3007 ch = *p++;
3008 s = 1;
3009 exp_val = 0;
3010 if (ch == '+') {
3011 ch = *p++;
3012 } else if (ch == '-') {
3013 s = -1;
3014 ch = *p++;
3016 if (ch < '0' || ch > '9')
3017 expect("exponent digits");
3018 while (ch >= '0' && ch <= '9') {
3019 exp_val = exp_val * 10 + ch - '0';
3020 ch = *p++;
3022 exp_val = exp_val * s;
3024 /* now we can generate the number */
3025 /* XXX: should patch directly float number */
3026 d = (double)bn[1] * 4294967296.0 + (double)bn[0];
3027 d = ldexp(d, exp_val - frac_bits);
3028 t = toup(ch);
3029 if (t == 'F') {
3030 ch = *p++;
3031 tok = TOK_CFLOAT;
3032 /* float : should handle overflow */
3033 tokc.f = (float)d;
3034 } else if (t == 'L') {
3035 ch = *p++;
3036 tok = TOK_CLDOUBLE;
3037 /* XXX: not large enough */
3038 tokc.ld = (long double)d;
3039 } else {
3040 tok = TOK_CDOUBLE;
3041 tokc.d = d;
3043 } else {
3044 /* decimal floats */
3045 if (ch == '.') {
3046 if (q >= token_buf + STRING_MAX_SIZE)
3047 goto num_too_long;
3048 *q++ = ch;
3049 ch = *p++;
3050 float_frac_parse:
3051 while (ch >= '0' && ch <= '9') {
3052 if (q >= token_buf + STRING_MAX_SIZE)
3053 goto num_too_long;
3054 *q++ = ch;
3055 ch = *p++;
3058 if (ch == 'e' || ch == 'E') {
3059 if (q >= token_buf + STRING_MAX_SIZE)
3060 goto num_too_long;
3061 *q++ = ch;
3062 ch = *p++;
3063 if (ch == '-' || ch == '+') {
3064 if (q >= token_buf + STRING_MAX_SIZE)
3065 goto num_too_long;
3066 *q++ = ch;
3067 ch = *p++;
3069 if (ch < '0' || ch > '9')
3070 expect("exponent digits");
3071 while (ch >= '0' && ch <= '9') {
3072 if (q >= token_buf + STRING_MAX_SIZE)
3073 goto num_too_long;
3074 *q++ = ch;
3075 ch = *p++;
3078 *q = '\0';
3079 t = toup(ch);
3080 errno = 0;
3081 if (t == 'F') {
3082 ch = *p++;
3083 tok = TOK_CFLOAT;
3084 tokc.f = strtof(token_buf, NULL);
3085 } else if (t == 'L') {
3086 ch = *p++;
3087 tok = TOK_CLDOUBLE;
3088 tokc.ld = strtold(token_buf, NULL);
3089 } else {
3090 tok = TOK_CDOUBLE;
3091 tokc.d = strtod(token_buf, NULL);
3094 } else {
3095 unsigned long long n, n1;
3096 int lcount, ucount;
3098 /* integer number */
3099 *q = '\0';
3100 q = token_buf;
3101 if (b == 10 && *q == '0') {
3102 b = 8;
3103 q++;
3105 n = 0;
3106 while(1) {
3107 t = *q++;
3108 /* no need for checks except for base 10 / 8 errors */
3109 if (t == '\0') {
3110 break;
3111 } else if (t >= 'a') {
3112 t = t - 'a' + 10;
3113 } else if (t >= 'A') {
3114 t = t - 'A' + 10;
3115 } else {
3116 t = t - '0';
3117 if (t >= b)
3118 error("invalid digit");
3120 n1 = n;
3121 n = n * b + t;
3122 /* detect overflow */
3123 /* XXX: this test is not reliable */
3124 if (n < n1)
3125 error("integer constant overflow");
3128 /* XXX: not exactly ANSI compliant */
3129 if ((n & 0xffffffff00000000LL) != 0) {
3130 if ((n >> 63) != 0)
3131 tok = TOK_CULLONG;
3132 else
3133 tok = TOK_CLLONG;
3134 } else if (n > 0x7fffffff) {
3135 tok = TOK_CUINT;
3136 } else {
3137 tok = TOK_CINT;
3139 lcount = 0;
3140 ucount = 0;
3141 for(;;) {
3142 t = toup(ch);
3143 if (t == 'L') {
3144 if (lcount >= 2)
3145 error("three 'l's in integer constant");
3146 lcount++;
3147 if (lcount == 2) {
3148 if (tok == TOK_CINT)
3149 tok = TOK_CLLONG;
3150 else if (tok == TOK_CUINT)
3151 tok = TOK_CULLONG;
3153 ch = *p++;
3154 } else if (t == 'U') {
3155 if (ucount >= 1)
3156 error("two 'u's in integer constant");
3157 ucount++;
3158 if (tok == TOK_CINT)
3159 tok = TOK_CUINT;
3160 else if (tok == TOK_CLLONG)
3161 tok = TOK_CULLONG;
3162 ch = *p++;
3163 } else {
3164 break;
3167 if (tok == TOK_CINT || tok == TOK_CUINT)
3168 tokc.ui = n;
3169 else
3170 tokc.ull = n;
3175 #define PARSE2(c1, tok1, c2, tok2) \
3176 case c1: \
3177 PEEKC(c, p); \
3178 if (c == c2) { \
3179 p++; \
3180 tok = tok2; \
3181 } else { \
3182 tok = tok1; \
3184 break;
3186 /* return next token without macro substitution */
3187 static inline void next_nomacro1(void)
3189 int t, c, is_long;
3190 TokenSym *ts;
3191 uint8_t *p, *p1;
3192 unsigned int h;
3194 p = file->buf_ptr;
3195 redo_no_start:
3196 c = *p;
3197 switch(c) {
3198 case ' ':
3199 case '\t':
3200 case '\f':
3201 case '\v':
3202 case '\r':
3203 p++;
3204 goto redo_no_start;
3206 case '\\':
3207 /* first look if it is in fact an end of buffer */
3208 if (p >= file->buf_end) {
3209 file->buf_ptr = p;
3210 handle_eob();
3211 p = file->buf_ptr;
3212 if (p >= file->buf_end)
3213 goto parse_eof;
3214 else
3215 goto redo_no_start;
3216 } else {
3217 file->buf_ptr = p;
3218 ch = *p;
3219 handle_stray();
3220 p = file->buf_ptr;
3221 goto redo_no_start;
3223 parse_eof:
3225 TCCState *s1 = tcc_state;
3226 if (parse_flags & PARSE_FLAG_LINEFEED) {
3227 tok = TOK_LINEFEED;
3228 } else if (s1->include_stack_ptr == s1->include_stack ||
3229 !(parse_flags & PARSE_FLAG_PREPROCESS)) {
3230 /* no include left : end of file. */
3231 tok = TOK_EOF;
3232 } else {
3233 /* pop include file */
3235 /* test if previous '#endif' was after a #ifdef at
3236 start of file */
3237 if (tok_flags & TOK_FLAG_ENDIF) {
3238 #ifdef INC_DEBUG
3239 printf("#endif %s\n", get_tok_str(file->ifndef_macro_saved, NULL));
3240 #endif
3241 add_cached_include(s1, file->inc_type, file->inc_filename,
3242 file->ifndef_macro_saved);
3245 /* add end of include file debug info */
3246 if (do_debug) {
3247 put_stabd(N_EINCL, 0, 0);
3249 /* pop include stack */
3250 tcc_close(file);
3251 s1->include_stack_ptr--;
3252 file = *s1->include_stack_ptr;
3253 p = file->buf_ptr;
3254 goto redo_no_start;
3257 break;
3259 case '\n':
3260 if (parse_flags & PARSE_FLAG_LINEFEED) {
3261 tok = TOK_LINEFEED;
3262 } else {
3263 file->line_num++;
3264 tok_flags |= TOK_FLAG_BOL;
3265 p++;
3266 goto redo_no_start;
3268 break;
3270 case '#':
3271 /* XXX: simplify */
3272 PEEKC(c, p);
3273 if ((tok_flags & TOK_FLAG_BOL) &&
3274 (parse_flags & PARSE_FLAG_PREPROCESS)) {
3275 file->buf_ptr = p;
3276 preprocess(tok_flags & TOK_FLAG_BOF);
3277 p = file->buf_ptr;
3278 goto redo_no_start;
3279 } else {
3280 if (c == '#') {
3281 p++;
3282 tok = TOK_TWOSHARPS;
3283 } else {
3284 tok = '#';
3287 break;
3289 case 'a': case 'b': case 'c': case 'd':
3290 case 'e': case 'f': case 'g': case 'h':
3291 case 'i': case 'j': case 'k': case 'l':
3292 case 'm': case 'n': case 'o': case 'p':
3293 case 'q': case 'r': case 's': case 't':
3294 case 'u': case 'v': case 'w': case 'x':
3295 case 'y': case 'z':
3296 case 'A': case 'B': case 'C': case 'D':
3297 case 'E': case 'F': case 'G': case 'H':
3298 case 'I': case 'J': case 'K':
3299 case 'M': case 'N': case 'O': case 'P':
3300 case 'Q': case 'R': case 'S': case 'T':
3301 case 'U': case 'V': case 'W': case 'X':
3302 case 'Y': case 'Z':
3303 case '_':
3304 parse_ident_fast:
3305 p1 = p;
3306 h = TOK_HASH_INIT;
3307 h = TOK_HASH_FUNC(h, c);
3308 p++;
3309 for(;;) {
3310 c = *p;
3311 if (!isidnum_table[c])
3312 break;
3313 h = TOK_HASH_FUNC(h, c);
3314 p++;
3316 if (c != '\\') {
3317 TokenSym **pts;
3318 int len;
3320 /* fast case : no stray found, so we have the full token
3321 and we have already hashed it */
3322 len = p - p1;
3323 h &= (TOK_HASH_SIZE - 1);
3324 pts = &hash_ident[h];
3325 for(;;) {
3326 ts = *pts;
3327 if (!ts)
3328 break;
3329 if (ts->len == len && !memcmp(ts->str, p1, len))
3330 goto token_found;
3331 pts = &(ts->hash_next);
3333 ts = tok_alloc_new(pts, p1, len);
3334 token_found: ;
3335 } else {
3336 /* slower case */
3337 cstr_reset(&tokcstr);
3339 while (p1 < p) {
3340 cstr_ccat(&tokcstr, *p1);
3341 p1++;
3343 p--;
3344 PEEKC(c, p);
3345 parse_ident_slow:
3346 while (isidnum_table[c]) {
3347 cstr_ccat(&tokcstr, c);
3348 PEEKC(c, p);
3350 ts = tok_alloc(tokcstr.data, tokcstr.size);
3352 tok = ts->tok;
3353 break;
3354 case 'L':
3355 t = p[1];
3356 if (t != '\\' && t != '\'' && t != '\"') {
3357 /* fast case */
3358 goto parse_ident_fast;
3359 } else {
3360 PEEKC(c, p);
3361 if (c == '\'' || c == '\"') {
3362 is_long = 1;
3363 goto str_const;
3364 } else {
3365 cstr_reset(&tokcstr);
3366 cstr_ccat(&tokcstr, 'L');
3367 goto parse_ident_slow;
3370 break;
3371 case '0': case '1': case '2': case '3':
3372 case '4': case '5': case '6': case '7':
3373 case '8': case '9':
3375 cstr_reset(&tokcstr);
3376 /* after the first digit, accept digits, alpha, '.' or sign if
3377 prefixed by 'eEpP' */
3378 parse_num:
3379 for(;;) {
3380 t = c;
3381 cstr_ccat(&tokcstr, c);
3382 PEEKC(c, p);
3383 if (!(isnum(c) || isid(c) || c == '.' ||
3384 ((c == '+' || c == '-') &&
3385 (t == 'e' || t == 'E' || t == 'p' || t == 'P'))))
3386 break;
3388 /* We add a trailing '\0' to ease parsing */
3389 cstr_ccat(&tokcstr, '\0');
3390 tokc.cstr = &tokcstr;
3391 tok = TOK_PPNUM;
3392 break;
3393 case '.':
3394 /* special dot handling because it can also start a number */
3395 PEEKC(c, p);
3396 if (isnum(c)) {
3397 cstr_reset(&tokcstr);
3398 cstr_ccat(&tokcstr, '.');
3399 goto parse_num;
3400 } else if (c == '.') {
3401 PEEKC(c, p);
3402 if (c != '.')
3403 expect("'.'");
3404 PEEKC(c, p);
3405 tok = TOK_DOTS;
3406 } else {
3407 tok = '.';
3409 break;
3410 case '\'':
3411 case '\"':
3412 is_long = 0;
3413 str_const:
3415 CString str;
3416 int sep;
3418 sep = c;
3420 /* parse the string */
3421 cstr_new(&str);
3422 p = parse_pp_string(p, sep, &str);
3423 cstr_ccat(&str, '\0');
3425 /* eval the escape (should be done as TOK_PPNUM) */
3426 cstr_reset(&tokcstr);
3427 parse_escape_string(&tokcstr, str.data, is_long);
3428 cstr_free(&str);
3430 if (sep == '\'') {
3431 int char_size;
3432 /* XXX: make it portable */
3433 if (!is_long)
3434 char_size = 1;
3435 else
3436 char_size = sizeof(int);
3437 if (tokcstr.size <= char_size)
3438 error("empty character constant");
3439 if (tokcstr.size > 2 * char_size)
3440 warning("multi-character character constant");
3441 if (!is_long) {
3442 tokc.i = *(int8_t *)tokcstr.data;
3443 tok = TOK_CCHAR;
3444 } else {
3445 tokc.i = *(int *)tokcstr.data;
3446 tok = TOK_LCHAR;
3448 } else {
3449 tokc.cstr = &tokcstr;
3450 if (!is_long)
3451 tok = TOK_STR;
3452 else
3453 tok = TOK_LSTR;
3456 break;
3458 case '<':
3459 PEEKC(c, p);
3460 if (c == '=') {
3461 p++;
3462 tok = TOK_LE;
3463 } else if (c == '<') {
3464 PEEKC(c, p);
3465 if (c == '=') {
3466 p++;
3467 tok = TOK_A_SHL;
3468 } else {
3469 tok = TOK_SHL;
3471 } else {
3472 tok = TOK_LT;
3474 break;
3476 case '>':
3477 PEEKC(c, p);
3478 if (c == '=') {
3479 p++;
3480 tok = TOK_GE;
3481 } else if (c == '>') {
3482 PEEKC(c, p);
3483 if (c == '=') {
3484 p++;
3485 tok = TOK_A_SAR;
3486 } else {
3487 tok = TOK_SAR;
3489 } else {
3490 tok = TOK_GT;
3492 break;
3494 case '&':
3495 PEEKC(c, p);
3496 if (c == '&') {
3497 p++;
3498 tok = TOK_LAND;
3499 } else if (c == '=') {
3500 p++;
3501 tok = TOK_A_AND;
3502 } else {
3503 tok = '&';
3505 break;
3507 case '|':
3508 PEEKC(c, p);
3509 if (c == '|') {
3510 p++;
3511 tok = TOK_LOR;
3512 } else if (c == '=') {
3513 p++;
3514 tok = TOK_A_OR;
3515 } else {
3516 tok = '|';
3518 break;
3520 case '+':
3521 PEEKC(c, p);
3522 if (c == '+') {
3523 p++;
3524 tok = TOK_INC;
3525 } else if (c == '=') {
3526 p++;
3527 tok = TOK_A_ADD;
3528 } else {
3529 tok = '+';
3531 break;
3533 case '-':
3534 PEEKC(c, p);
3535 if (c == '-') {
3536 p++;
3537 tok = TOK_DEC;
3538 } else if (c == '=') {
3539 p++;
3540 tok = TOK_A_SUB;
3541 } else if (c == '>') {
3542 p++;
3543 tok = TOK_ARROW;
3544 } else {
3545 tok = '-';
3547 break;
3549 PARSE2('!', '!', '=', TOK_NE)
3550 PARSE2('=', '=', '=', TOK_EQ)
3551 PARSE2('*', '*', '=', TOK_A_MUL)
3552 PARSE2('%', '%', '=', TOK_A_MOD)
3553 PARSE2('^', '^', '=', TOK_A_XOR)
3555 /* comments or operator */
3556 case '/':
3557 PEEKC(c, p);
3558 if (c == '*') {
3559 p = parse_comment(p);
3560 goto redo_no_start;
3561 } else if (c == '/') {
3562 p = parse_line_comment(p);
3563 goto redo_no_start;
3564 } else if (c == '=') {
3565 p++;
3566 tok = TOK_A_DIV;
3567 } else {
3568 tok = '/';
3570 break;
3572 /* simple tokens */
3573 case '(':
3574 case ')':
3575 case '[':
3576 case ']':
3577 case '{':
3578 case '}':
3579 case ',':
3580 case ';':
3581 case ':':
3582 case '?':
3583 case '~':
3584 case '$': /* only used in assembler */
3585 tok = c;
3586 p++;
3587 break;
3588 default:
3589 error("unrecognized character \\x%02x", c);
3590 break;
3592 file->buf_ptr = p;
3593 tok_flags = 0;
3594 #if defined(PARSE_DEBUG)
3595 printf("token = %s\n", get_tok_str(tok, &tokc));
3596 #endif
3599 /* return next token without macro substitution. Can read input from
3600 macro_ptr buffer */
3601 static void next_nomacro(void)
3603 if (macro_ptr) {
3604 redo:
3605 tok = *macro_ptr;
3606 if (tok) {
3607 TOK_GET(tok, macro_ptr, tokc);
3608 if (tok == TOK_LINENUM) {
3609 file->line_num = tokc.i;
3610 goto redo;
3613 } else {
3614 next_nomacro1();
3618 /* substitute args in macro_str and return allocated string */
3619 static int *macro_arg_subst(Sym **nested_list, int *macro_str, Sym *args)
3621 int *st, last_tok, t, notfirst;
3622 Sym *s;
3623 CValue cval;
3624 TokenString str;
3625 CString cstr;
3627 tok_str_new(&str);
3628 last_tok = 0;
3629 while(1) {
3630 TOK_GET(t, macro_str, cval);
3631 if (!t)
3632 break;
3633 if (t == '#') {
3634 /* stringize */
3635 TOK_GET(t, macro_str, cval);
3636 if (!t)
3637 break;
3638 s = sym_find2(args, t);
3639 if (s) {
3640 cstr_new(&cstr);
3641 st = (int *)s->c;
3642 notfirst = 0;
3643 while (*st) {
3644 if (notfirst)
3645 cstr_ccat(&cstr, ' ');
3646 TOK_GET(t, st, cval);
3647 cstr_cat(&cstr, get_tok_str(t, &cval));
3648 notfirst = 1;
3650 cstr_ccat(&cstr, '\0');
3651 #ifdef PP_DEBUG
3652 printf("stringize: %s\n", (char *)cstr.data);
3653 #endif
3654 /* add string */
3655 cval.cstr = &cstr;
3656 tok_str_add2(&str, TOK_STR, &cval);
3657 cstr_free(&cstr);
3658 } else {
3659 tok_str_add2(&str, t, &cval);
3661 } else if (t >= TOK_IDENT) {
3662 s = sym_find2(args, t);
3663 if (s) {
3664 st = (int *)s->c;
3665 /* if '##' is present before or after, no arg substitution */
3666 if (*macro_str == TOK_TWOSHARPS || last_tok == TOK_TWOSHARPS) {
3667 /* special case for var arg macros : ## eats the
3668 ',' if empty VA_ARGS variable. */
3669 /* XXX: test of the ',' is not 100%
3670 reliable. should fix it to avoid security
3671 problems */
3672 if (gnu_ext && s->type.t &&
3673 last_tok == TOK_TWOSHARPS &&
3674 str.len >= 2 && str.str[str.len - 2] == ',') {
3675 if (*st == 0) {
3676 /* suppress ',' '##' */
3677 str.len -= 2;
3678 } else {
3679 /* suppress '##' and add variable */
3680 str.len--;
3681 goto add_var;
3683 } else {
3684 int t1;
3685 add_var:
3686 for(;;) {
3687 TOK_GET(t1, st, cval);
3688 if (!t1)
3689 break;
3690 tok_str_add2(&str, t1, &cval);
3693 } else {
3694 macro_subst(&str, nested_list, st);
3696 } else {
3697 tok_str_add(&str, t);
3699 } else {
3700 tok_str_add2(&str, t, &cval);
3702 last_tok = t;
3704 tok_str_add(&str, 0);
3705 return str.str;
3708 /* do macro substitution of current token with macro 's' and add
3709 result to (tok_str,tok_len). 'nested_list' is the list of all
3710 macros we got inside to avoid recursing. Return non zero if no
3711 substitution needs to be done */
3712 static int macro_subst_tok(TokenString *tok_str,
3713 Sym **nested_list, Sym *s)
3715 Sym *args, *sa, *sa1;
3716 int mstr_allocated, parlevel, *mstr, t;
3717 TokenString str;
3718 char *cstrval;
3719 CValue cval;
3720 CString cstr;
3722 /* if symbol is a macro, prepare substitution */
3724 /* special macros */
3725 if (tok == TOK___LINE__) {
3726 cval.i = file->line_num;
3727 tok_str_add2(tok_str, TOK_CINT, &cval);
3728 } else if (tok == TOK___FILE__) {
3729 cstrval = file->filename;
3730 goto add_cstr;
3731 tok_str_add2(tok_str, TOK_STR, &cval);
3732 } else if (tok == TOK___DATE__) {
3733 cstrval = "Jan 1 2002";
3734 goto add_cstr;
3735 } else if (tok == TOK___TIME__) {
3736 cstrval = "00:00:00";
3737 add_cstr:
3738 cstr_new(&cstr);
3739 cstr_cat(&cstr, cstrval);
3740 cstr_ccat(&cstr, '\0');
3741 cval.cstr = &cstr;
3742 tok_str_add2(tok_str, TOK_STR, &cval);
3743 cstr_free(&cstr);
3744 } else {
3745 mstr = (int *)s->c;
3746 mstr_allocated = 0;
3747 if (s->type.t == MACRO_FUNC) {
3748 /* NOTE: we do not use next_nomacro to avoid eating the
3749 next token. XXX: find better solution */
3750 if (macro_ptr) {
3751 t = *macro_ptr;
3752 } else {
3753 /* XXX: incorrect with comments */
3754 ch = file->buf_ptr[0];
3755 while (is_space(ch) || ch == '\n')
3756 cinp();
3757 t = ch;
3759 if (t != '(') /* no macro subst */
3760 return -1;
3762 /* argument macro */
3763 next_nomacro();
3764 next_nomacro();
3765 args = NULL;
3766 sa = s->next;
3767 /* NOTE: empty args are allowed, except if no args */
3768 for(;;) {
3769 /* handle '()' case */
3770 if (!args && tok == ')')
3771 break;
3772 if (!sa)
3773 error("macro '%s' used with too many args",
3774 get_tok_str(s->v, 0));
3775 tok_str_new(&str);
3776 parlevel = 0;
3777 /* NOTE: non zero sa->t indicates VA_ARGS */
3778 while ((parlevel > 0 ||
3779 (tok != ')' &&
3780 (tok != ',' || sa->type.t))) &&
3781 tok != -1) {
3782 if (tok == '(')
3783 parlevel++;
3784 else if (tok == ')')
3785 parlevel--;
3786 tok_str_add2(&str, tok, &tokc);
3787 next_nomacro();
3789 tok_str_add(&str, 0);
3790 sym_push2(&args, sa->v & ~SYM_FIELD, sa->type.t, (int)str.str);
3791 sa = sa->next;
3792 if (tok == ')') {
3793 /* special case for gcc var args: add an empty
3794 var arg argument if it is omitted */
3795 if (sa && sa->type.t && gnu_ext)
3796 continue;
3797 else
3798 break;
3800 if (tok != ',')
3801 expect(",");
3802 next_nomacro();
3804 if (sa) {
3805 error("macro '%s' used with too few args",
3806 get_tok_str(s->v, 0));
3809 /* now subst each arg */
3810 mstr = macro_arg_subst(nested_list, mstr, args);
3811 /* free memory */
3812 sa = args;
3813 while (sa) {
3814 sa1 = sa->prev;
3815 tok_str_free((int *)sa->c);
3816 tcc_free(sa);
3817 sa = sa1;
3819 mstr_allocated = 1;
3821 sym_push2(nested_list, s->v, 0, 0);
3822 macro_subst(tok_str, nested_list, mstr);
3823 /* pop nested defined symbol */
3824 sa1 = *nested_list;
3825 *nested_list = sa1->prev;
3826 tcc_free(sa1);
3827 if (mstr_allocated)
3828 tok_str_free(mstr);
3830 return 0;
3833 /* handle the '##' operator. Return NULL if no '##' seen. Otherwise
3834 return the resulting string (which must be freed). */
3835 static inline int *macro_twosharps(const int *macro_str)
3837 TokenSym *ts;
3838 const int *macro_ptr1, *start_macro_ptr, *ptr, *saved_macro_ptr;
3839 int t;
3840 const char *p1, *p2;
3841 CValue cval;
3842 TokenString macro_str1;
3843 CString cstr;
3845 start_macro_ptr = macro_str;
3846 /* we search the first '##' */
3847 for(;;) {
3848 macro_ptr1 = macro_str;
3849 TOK_GET(t, macro_str, cval);
3850 /* nothing more to do if end of string */
3851 if (t == 0)
3852 return NULL;
3853 if (*macro_str == TOK_TWOSHARPS)
3854 break;
3857 /* we saw '##', so we need more processing to handle it */
3858 cstr_new(&cstr);
3859 tok_str_new(&macro_str1);
3860 tok = t;
3861 tokc = cval;
3863 /* add all tokens seen so far */
3864 for(ptr = start_macro_ptr; ptr < macro_ptr1;) {
3865 TOK_GET(t, ptr, cval);
3866 tok_str_add2(&macro_str1, t, &cval);
3868 saved_macro_ptr = macro_ptr;
3869 /* XXX: get rid of the use of macro_ptr here */
3870 macro_ptr = (int *)macro_str;
3871 for(;;) {
3872 while (*macro_ptr == TOK_TWOSHARPS) {
3873 macro_ptr++;
3874 macro_ptr1 = macro_ptr;
3875 t = *macro_ptr;
3876 if (t) {
3877 TOK_GET(t, macro_ptr, cval);
3878 /* We concatenate the two tokens if we have an
3879 identifier or a preprocessing number */
3880 cstr_reset(&cstr);
3881 p1 = get_tok_str(tok, &tokc);
3882 cstr_cat(&cstr, p1);
3883 p2 = get_tok_str(t, &cval);
3884 cstr_cat(&cstr, p2);
3885 cstr_ccat(&cstr, '\0');
3887 if ((tok >= TOK_IDENT || tok == TOK_PPNUM) &&
3888 (t >= TOK_IDENT || t == TOK_PPNUM)) {
3889 if (tok == TOK_PPNUM) {
3890 /* if number, then create a number token */
3891 /* NOTE: no need to allocate because
3892 tok_str_add2() does it */
3893 tokc.cstr = &cstr;
3894 } else {
3895 /* if identifier, we must do a test to
3896 validate we have a correct identifier */
3897 if (t == TOK_PPNUM) {
3898 const char *p;
3899 int c;
3901 p = p2;
3902 for(;;) {
3903 c = *p;
3904 if (c == '\0')
3905 break;
3906 p++;
3907 if (!isnum(c) && !isid(c))
3908 goto error_pasting;
3911 ts = tok_alloc(cstr.data, strlen(cstr.data));
3912 tok = ts->tok; /* modify current token */
3914 } else {
3915 const char *str = cstr.data;
3916 const unsigned char *q;
3918 /* we look for a valid token */
3919 /* XXX: do more extensive checks */
3920 if (!strcmp(str, ">>=")) {
3921 tok = TOK_A_SAR;
3922 } else if (!strcmp(str, "<<=")) {
3923 tok = TOK_A_SHL;
3924 } else if (strlen(str) == 2) {
3925 /* search in two bytes table */
3926 q = tok_two_chars;
3927 for(;;) {
3928 if (!*q)
3929 goto error_pasting;
3930 if (q[0] == str[0] && q[1] == str[1])
3931 break;
3932 q += 3;
3934 tok = q[2];
3935 } else {
3936 error_pasting:
3937 /* NOTE: because get_tok_str use a static buffer,
3938 we must save it */
3939 cstr_reset(&cstr);
3940 p1 = get_tok_str(tok, &tokc);
3941 cstr_cat(&cstr, p1);
3942 cstr_ccat(&cstr, '\0');
3943 p2 = get_tok_str(t, &cval);
3944 warning("pasting \"%s\" and \"%s\" does not give a valid preprocessing token", cstr.data, p2);
3945 /* cannot merge tokens: just add them separately */
3946 tok_str_add2(&macro_str1, tok, &tokc);
3947 /* XXX: free associated memory ? */
3948 tok = t;
3949 tokc = cval;
3954 tok_str_add2(&macro_str1, tok, &tokc);
3955 next_nomacro();
3956 if (tok == 0)
3957 break;
3959 macro_ptr = (int *)saved_macro_ptr;
3960 cstr_free(&cstr);
3961 tok_str_add(&macro_str1, 0);
3962 return macro_str1.str;
3966 /* do macro substitution of macro_str and add result to
3967 (tok_str,tok_len). 'nested_list' is the list of all macros we got
3968 inside to avoid recursing. */
3969 static void macro_subst(TokenString *tok_str,
3970 Sym **nested_list, const int *macro_str)
3972 Sym *s;
3973 int *saved_macro_ptr, *macro_str1;
3974 const int *ptr;
3975 int t, ret;
3976 CValue cval;
3978 /* first scan for '##' operator handling */
3979 ptr = macro_str;
3980 macro_str1 = macro_twosharps(ptr);
3981 if (macro_str1)
3982 ptr = macro_str1;
3983 while (1) {
3984 TOK_GET(t, ptr, cval);
3985 if (t == 0)
3986 break;
3987 s = define_find(t);
3988 if (s != NULL) {
3989 /* if nested substitution, do nothing */
3990 if (sym_find2(*nested_list, t))
3991 goto no_subst;
3992 saved_macro_ptr = macro_ptr;
3993 macro_ptr = (int *)ptr;
3994 tok = t;
3995 ret = macro_subst_tok(tok_str, nested_list, s);
3996 ptr = (int *)macro_ptr;
3997 macro_ptr = saved_macro_ptr;
3998 if (ret != 0)
3999 goto no_subst;
4000 } else {
4001 no_subst:
4002 tok_str_add2(tok_str, t, &cval);
4005 if (macro_str1)
4006 tok_str_free(macro_str1);
4009 /* return next token with macro substitution */
4010 static void next(void)
4012 Sym *nested_list, *s;
4013 TokenString str;
4015 redo:
4016 next_nomacro();
4017 if (!macro_ptr) {
4018 /* if not reading from macro substituted string, then try
4019 to substitute macros */
4020 if (tok >= TOK_IDENT &&
4021 (parse_flags & PARSE_FLAG_PREPROCESS)) {
4022 s = define_find(tok);
4023 if (s) {
4024 /* we have a macro: we try to substitute */
4025 tok_str_new(&str);
4026 nested_list = NULL;
4027 if (macro_subst_tok(&str, &nested_list, s) == 0) {
4028 /* substitution done, NOTE: maybe empty */
4029 tok_str_add(&str, 0);
4030 macro_ptr = str.str;
4031 macro_ptr_allocated = str.str;
4032 goto redo;
4036 } else {
4037 if (tok == 0) {
4038 /* end of macro or end of unget buffer */
4039 if (unget_buffer_enabled) {
4040 macro_ptr = unget_saved_macro_ptr;
4041 unget_buffer_enabled = 0;
4042 } else {
4043 /* end of macro string: free it */
4044 tok_str_free(macro_ptr_allocated);
4045 macro_ptr = NULL;
4047 goto redo;
4051 /* convert preprocessor tokens into C tokens */
4052 if (tok == TOK_PPNUM &&
4053 (parse_flags & PARSE_FLAG_TOK_NUM)) {
4054 parse_number((char *)tokc.cstr->data);
4058 /* push back current token and set current token to 'last_tok'. Only
4059 identifier case handled for labels. */
4060 static inline void unget_tok(int last_tok)
4062 int i, n;
4063 int *q;
4064 unget_saved_macro_ptr = macro_ptr;
4065 unget_buffer_enabled = 1;
4066 q = unget_saved_buffer;
4067 macro_ptr = q;
4068 *q++ = tok;
4069 n = tok_ext_size(tok) - 1;
4070 for(i=0;i<n;i++)
4071 *q++ = tokc.tab[i];
4072 *q = 0; /* end of token string */
4073 tok = last_tok;
4077 void swap(int *p, int *q)
4079 int t;
4080 t = *p;
4081 *p = *q;
4082 *q = t;
4085 void vsetc(CType *type, int r, CValue *vc)
4087 int v;
4089 if (vtop >= vstack + VSTACK_SIZE)
4090 error("memory full");
4091 /* cannot let cpu flags if other instruction are generated. Also
4092 avoid leaving VT_JMP anywhere except on the top of the stack
4093 because it would complicate the code generator. */
4094 if (vtop >= vstack) {
4095 v = vtop->r & VT_VALMASK;
4096 if (v == VT_CMP || (v & ~1) == VT_JMP)
4097 gv(RC_INT);
4099 vtop++;
4100 vtop->type = *type;
4101 vtop->r = r;
4102 vtop->r2 = VT_CONST;
4103 vtop->c = *vc;
4106 /* push integer constant */
4107 void vpushi(int v)
4109 CValue cval;
4110 cval.i = v;
4111 vsetc(&int_type, VT_CONST, &cval);
4114 /* Return a static symbol pointing to a section */
4115 static Sym *get_sym_ref(CType *type, Section *sec,
4116 unsigned long offset, unsigned long size)
4118 int v;
4119 Sym *sym;
4121 v = anon_sym++;
4122 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
4123 sym->type.ref = type->ref;
4124 sym->r = VT_CONST | VT_SYM;
4125 put_extern_sym(sym, sec, offset, size);
4126 return sym;
4129 /* push a reference to a section offset by adding a dummy symbol */
4130 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
4132 CValue cval;
4134 cval.ul = 0;
4135 vsetc(type, VT_CONST | VT_SYM, &cval);
4136 vtop->sym = get_sym_ref(type, sec, offset, size);
4139 /* define a new external reference to a symbol 'v' of type 'u' */
4140 static Sym *external_global_sym(int v, CType *type, int r)
4142 Sym *s;
4144 s = sym_find(v);
4145 if (!s) {
4146 /* push forward reference */
4147 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
4148 s->type.ref = type->ref;
4149 s->r = r | VT_CONST | VT_SYM;
4151 return s;
4154 /* define a new external reference to a symbol 'v' of type 'u' */
4155 static Sym *external_sym(int v, CType *type, int r)
4157 Sym *s;
4159 s = sym_find(v);
4160 if (!s) {
4161 /* push forward reference */
4162 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
4163 s->type.t |= VT_EXTERN;
4164 } else {
4165 if (!is_compatible_types(&s->type, type))
4166 error("incompatible types for redefinition of '%s'",
4167 get_tok_str(v, NULL));
4169 return s;
4172 /* push a reference to global symbol v */
4173 static void vpush_global_sym(CType *type, int v)
4175 Sym *sym;
4176 CValue cval;
4178 sym = external_global_sym(v, type, 0);
4179 cval.ul = 0;
4180 vsetc(type, VT_CONST | VT_SYM, &cval);
4181 vtop->sym = sym;
4184 void vset(CType *type, int r, int v)
4186 CValue cval;
4188 cval.i = v;
4189 vsetc(type, r, &cval);
4192 void vseti(int r, int v)
4194 CType type;
4195 type.t = VT_INT;
4196 vset(&type, r, v);
4199 void vswap(void)
4201 SValue tmp;
4203 tmp = vtop[0];
4204 vtop[0] = vtop[-1];
4205 vtop[-1] = tmp;
4208 void vpushv(SValue *v)
4210 if (vtop >= vstack + VSTACK_SIZE)
4211 error("memory full");
4212 vtop++;
4213 *vtop = *v;
4216 void vdup(void)
4218 vpushv(vtop);
4221 /* save r to the memory stack, and mark it as being free */
4222 void save_reg(int r)
4224 int l, saved, size, align;
4225 SValue *p, sv;
4226 CType *type;
4228 /* modify all stack values */
4229 saved = 0;
4230 l = 0;
4231 for(p=vstack;p<=vtop;p++) {
4232 if ((p->r & VT_VALMASK) == r ||
4233 (p->r2 & VT_VALMASK) == r) {
4234 /* must save value on stack if not already done */
4235 if (!saved) {
4236 /* NOTE: must reload 'r' because r might be equal to r2 */
4237 r = p->r & VT_VALMASK;
4238 /* store register in the stack */
4239 type = &p->type;
4240 if ((p->r & VT_LVAL) ||
4241 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
4242 type = &int_type;
4243 size = type_size(type, &align);
4244 loc = (loc - size) & -align;
4245 sv.type.t = type->t;
4246 sv.r = VT_LOCAL | VT_LVAL;
4247 sv.c.ul = loc;
4248 store(r, &sv);
4249 #ifdef TCC_TARGET_I386
4250 /* x86 specific: need to pop fp register ST0 if saved */
4251 if (r == TREG_ST0) {
4252 o(0xd9dd); /* fstp %st(1) */
4254 #endif
4255 /* special long long case */
4256 if ((type->t & VT_BTYPE) == VT_LLONG) {
4257 sv.c.ul += 4;
4258 store(p->r2, &sv);
4260 l = loc;
4261 saved = 1;
4263 /* mark that stack entry as being saved on the stack */
4264 if (p->r & VT_LVAL) {
4265 /* also clear the bounded flag because the
4266 relocation address of the function was stored in
4267 p->c.ul */
4268 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
4269 } else {
4270 p->r = lvalue_type(p->type.t) | VT_LOCAL;
4272 p->r2 = VT_CONST;
4273 p->c.ul = l;
4278 /* find a free register of class 'rc'. If none, save one register */
4279 int get_reg(int rc)
4281 int r;
4282 SValue *p;
4284 /* find a free register */
4285 for(r=0;r<NB_REGS;r++) {
4286 if (reg_classes[r] & rc) {
4287 for(p=vstack;p<=vtop;p++) {
4288 if ((p->r & VT_VALMASK) == r ||
4289 (p->r2 & VT_VALMASK) == r)
4290 goto notfound;
4292 return r;
4294 notfound: ;
4297 /* no register left : free the first one on the stack (VERY
4298 IMPORTANT to start from the bottom to ensure that we don't
4299 spill registers used in gen_opi()) */
4300 for(p=vstack;p<=vtop;p++) {
4301 r = p->r & VT_VALMASK;
4302 if (r < VT_CONST && (reg_classes[r] & rc))
4303 goto save_found;
4304 /* also look at second register (if long long) */
4305 r = p->r2 & VT_VALMASK;
4306 if (r < VT_CONST && (reg_classes[r] & rc)) {
4307 save_found:
4308 save_reg(r);
4309 return r;
4312 /* Should never comes here */
4313 return -1;
4316 /* save registers up to (vtop - n) stack entry */
4317 void save_regs(int n)
4319 int r;
4320 SValue *p, *p1;
4321 p1 = vtop - n;
4322 for(p = vstack;p <= p1; p++) {
4323 r = p->r & VT_VALMASK;
4324 if (r < VT_CONST) {
4325 save_reg(r);
4330 /* move register 's' to 'r', and flush previous value of r to memory
4331 if needed */
4332 void move_reg(int r, int s)
4334 SValue sv;
4336 if (r != s) {
4337 save_reg(r);
4338 sv.type.t = VT_INT;
4339 sv.r = s;
4340 sv.c.ul = 0;
4341 load(r, &sv);
4345 /* get address of vtop (vtop MUST BE an lvalue) */
4346 void gaddrof(void)
4348 vtop->r &= ~VT_LVAL;
4349 /* tricky: if saved lvalue, then we can go back to lvalue */
4350 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
4351 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
4354 #ifdef CONFIG_TCC_BCHECK
4355 /* generate lvalue bound code */
4356 void gbound(void)
4358 int lval_type;
4359 CType type1;
4361 vtop->r &= ~VT_MUSTBOUND;
4362 /* if lvalue, then use checking code before dereferencing */
4363 if (vtop->r & VT_LVAL) {
4364 /* if not VT_BOUNDED value, then make one */
4365 if (!(vtop->r & VT_BOUNDED)) {
4366 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
4367 /* must save type because we must set it to int to get pointer */
4368 type1 = vtop->type;
4369 vtop->type.t = VT_INT;
4370 gaddrof();
4371 vpushi(0);
4372 gen_bounded_ptr_add();
4373 vtop->r |= lval_type;
4374 vtop->type = type1;
4376 /* then check for dereferencing */
4377 gen_bounded_ptr_deref();
4380 #endif
4382 /* store vtop a register belonging to class 'rc'. lvalues are
4383 converted to values. Cannot be used if cannot be converted to
4384 register value (such as structures). */
4385 int gv(int rc)
4387 int r, r2, rc2, bit_pos, bit_size, size, align, i;
4388 unsigned long long ll;
4390 /* NOTE: get_reg can modify vstack[] */
4391 if (vtop->type.t & VT_BITFIELD) {
4392 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4393 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4394 /* remove bit field info to avoid loops */
4395 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
4396 /* generate shifts */
4397 vpushi(32 - (bit_pos + bit_size));
4398 gen_op(TOK_SHL);
4399 vpushi(32 - bit_size);
4400 /* NOTE: transformed to SHR if unsigned */
4401 gen_op(TOK_SAR);
4402 r = gv(rc);
4403 } else {
4404 if (is_float(vtop->type.t) &&
4405 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4406 Sym *sym;
4407 int *ptr;
4408 unsigned long offset;
4410 /* XXX: unify with initializers handling ? */
4411 /* CPUs usually cannot use float constants, so we store them
4412 generically in data segment */
4413 size = type_size(&vtop->type, &align);
4414 offset = (data_section->data_offset + align - 1) & -align;
4415 data_section->data_offset = offset;
4416 /* XXX: not portable yet */
4417 ptr = section_ptr_add(data_section, size);
4418 size = size >> 2;
4419 for(i=0;i<size;i++)
4420 ptr[i] = vtop->c.tab[i];
4421 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
4422 vtop->r |= VT_LVAL | VT_SYM;
4423 vtop->sym = sym;
4424 vtop->c.ul = 0;
4426 #ifdef CONFIG_TCC_BCHECK
4427 if (vtop->r & VT_MUSTBOUND)
4428 gbound();
4429 #endif
4431 r = vtop->r & VT_VALMASK;
4432 /* need to reload if:
4433 - constant
4434 - lvalue (need to dereference pointer)
4435 - already a register, but not in the right class */
4436 if (r >= VT_CONST ||
4437 (vtop->r & VT_LVAL) ||
4438 !(reg_classes[r] & rc) ||
4439 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
4440 !(reg_classes[vtop->r2] & rc))) {
4441 r = get_reg(rc);
4442 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
4443 /* two register type load : expand to two words
4444 temporarily */
4445 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
4446 /* load constant */
4447 ll = vtop->c.ull;
4448 vtop->c.ui = ll; /* first word */
4449 load(r, vtop);
4450 vtop->r = r; /* save register value */
4451 vpushi(ll >> 32); /* second word */
4452 } else if (r >= VT_CONST ||
4453 (vtop->r & VT_LVAL)) {
4454 /* load from memory */
4455 load(r, vtop);
4456 vdup();
4457 vtop[-1].r = r; /* save register value */
4458 /* increment pointer to get second word */
4459 vtop->type.t = VT_INT;
4460 gaddrof();
4461 vpushi(4);
4462 gen_op('+');
4463 vtop->r |= VT_LVAL;
4464 } else {
4465 /* move registers */
4466 load(r, vtop);
4467 vdup();
4468 vtop[-1].r = r; /* save register value */
4469 vtop->r = vtop[-1].r2;
4471 /* allocate second register */
4472 rc2 = RC_INT;
4473 if (rc == RC_IRET)
4474 rc2 = RC_LRET;
4475 r2 = get_reg(rc2);
4476 load(r2, vtop);
4477 vpop();
4478 /* write second register */
4479 vtop->r2 = r2;
4480 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
4481 int t1, t;
4482 /* lvalue of scalar type : need to use lvalue type
4483 because of possible cast */
4484 t = vtop->type.t;
4485 t1 = t;
4486 /* compute memory access type */
4487 if (vtop->r & VT_LVAL_BYTE)
4488 t = VT_BYTE;
4489 else if (vtop->r & VT_LVAL_SHORT)
4490 t = VT_SHORT;
4491 if (vtop->r & VT_LVAL_UNSIGNED)
4492 t |= VT_UNSIGNED;
4493 vtop->type.t = t;
4494 load(r, vtop);
4495 /* restore wanted type */
4496 vtop->type.t = t1;
4497 } else {
4498 /* one register type load */
4499 load(r, vtop);
4502 vtop->r = r;
4504 return r;
4507 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
4508 void gv2(int rc1, int rc2)
4510 int v;
4512 /* generate more generic register first. But VT_JMP or VT_CMP
4513 values must be generated first in all cases to avoid possible
4514 reload errors */
4515 v = vtop[0].r & VT_VALMASK;
4516 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
4517 vswap();
4518 gv(rc1);
4519 vswap();
4520 gv(rc2);
4521 /* test if reload is needed for first register */
4522 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
4523 vswap();
4524 gv(rc1);
4525 vswap();
4527 } else {
4528 gv(rc2);
4529 vswap();
4530 gv(rc1);
4531 vswap();
4532 /* test if reload is needed for first register */
4533 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
4534 gv(rc2);
4539 /* expand long long on stack in two int registers */
4540 void lexpand(void)
4542 int u;
4544 u = vtop->type.t & VT_UNSIGNED;
4545 gv(RC_INT);
4546 vdup();
4547 vtop[0].r = vtop[-1].r2;
4548 vtop[0].r2 = VT_CONST;
4549 vtop[-1].r2 = VT_CONST;
4550 vtop[0].type.t = VT_INT | u;
4551 vtop[-1].type.t = VT_INT | u;
4554 /* build a long long from two ints */
4555 void lbuild(int t)
4557 gv2(RC_INT, RC_INT);
4558 vtop[-1].r2 = vtop[0].r;
4559 vtop[-1].type.t = t;
4560 vpop();
4563 /* rotate n first stack elements to the bottom */
4564 void vrotb(int n)
4566 int i;
4567 SValue tmp;
4569 tmp = vtop[-n + 1];
4570 for(i=-n+1;i!=0;i++)
4571 vtop[i] = vtop[i+1];
4572 vtop[0] = tmp;
4575 /* pop stack value */
4576 void vpop(void)
4578 int v;
4579 v = vtop->r & VT_VALMASK;
4580 #ifdef TCC_TARGET_I386
4581 /* for x86, we need to pop the FP stack */
4582 if (v == TREG_ST0 && !nocode_wanted) {
4583 o(0xd9dd); /* fstp %st(1) */
4584 } else
4585 #endif
4586 if (v == VT_JMP || v == VT_JMPI) {
4587 /* need to put correct jump if && or || without test */
4588 gsym(vtop->c.ul);
4590 vtop--;
4593 /* convert stack entry to register and duplicate its value in another
4594 register */
4595 void gv_dup(void)
4597 int rc, t, r, r1;
4598 SValue sv;
4600 t = vtop->type.t;
4601 if ((t & VT_BTYPE) == VT_LLONG) {
4602 lexpand();
4603 gv_dup();
4604 vswap();
4605 vrotb(3);
4606 gv_dup();
4607 vrotb(4);
4608 /* stack: H L L1 H1 */
4609 lbuild(t);
4610 vrotb(3);
4611 vrotb(3);
4612 vswap();
4613 lbuild(t);
4614 vswap();
4615 } else {
4616 /* duplicate value */
4617 rc = RC_INT;
4618 sv.type.t = VT_INT;
4619 if (is_float(t)) {
4620 rc = RC_FLOAT;
4621 sv.type.t = t;
4623 r = gv(rc);
4624 r1 = get_reg(rc);
4625 sv.r = r;
4626 sv.c.ul = 0;
4627 load(r1, &sv); /* move r to r1 */
4628 vdup();
4629 /* duplicates value */
4630 vtop->r = r1;
4634 /* generate CPU independent (unsigned) long long operations */
4635 void gen_opl(int op)
4637 int t, a, b, op1, c, i;
4638 int func;
4639 GFuncContext gf;
4640 SValue tmp;
4642 switch(op) {
4643 case '/':
4644 case TOK_PDIV:
4645 func = TOK___divdi3;
4646 goto gen_func;
4647 case TOK_UDIV:
4648 func = TOK___udivdi3;
4649 goto gen_func;
4650 case '%':
4651 func = TOK___moddi3;
4652 goto gen_func;
4653 case TOK_UMOD:
4654 func = TOK___umoddi3;
4655 gen_func:
4656 /* call generic long long function */
4657 gfunc_start(&gf, FUNC_CDECL);
4658 gfunc_param(&gf);
4659 gfunc_param(&gf);
4660 vpush_global_sym(&func_old_type, func);
4661 gfunc_call(&gf);
4662 vpushi(0);
4663 vtop->r = REG_IRET;
4664 vtop->r2 = REG_LRET;
4665 break;
4666 case '^':
4667 case '&':
4668 case '|':
4669 case '*':
4670 case '+':
4671 case '-':
4672 t = vtop->type.t;
4673 vswap();
4674 lexpand();
4675 vrotb(3);
4676 lexpand();
4677 /* stack: L1 H1 L2 H2 */
4678 tmp = vtop[0];
4679 vtop[0] = vtop[-3];
4680 vtop[-3] = tmp;
4681 tmp = vtop[-2];
4682 vtop[-2] = vtop[-3];
4683 vtop[-3] = tmp;
4684 vswap();
4685 /* stack: H1 H2 L1 L2 */
4686 if (op == '*') {
4687 vpushv(vtop - 1);
4688 vpushv(vtop - 1);
4689 gen_op(TOK_UMULL);
4690 lexpand();
4691 /* stack: H1 H2 L1 L2 ML MH */
4692 for(i=0;i<4;i++)
4693 vrotb(6);
4694 /* stack: ML MH H1 H2 L1 L2 */
4695 tmp = vtop[0];
4696 vtop[0] = vtop[-2];
4697 vtop[-2] = tmp;
4698 /* stack: ML MH H1 L2 H2 L1 */
4699 gen_op('*');
4700 vrotb(3);
4701 vrotb(3);
4702 gen_op('*');
4703 /* stack: ML MH M1 M2 */
4704 gen_op('+');
4705 gen_op('+');
4706 } else if (op == '+' || op == '-') {
4707 /* XXX: add non carry method too (for MIPS or alpha) */
4708 if (op == '+')
4709 op1 = TOK_ADDC1;
4710 else
4711 op1 = TOK_SUBC1;
4712 gen_op(op1);
4713 /* stack: H1 H2 (L1 op L2) */
4714 vrotb(3);
4715 vrotb(3);
4716 gen_op(op1 + 1); /* TOK_xxxC2 */
4717 } else {
4718 gen_op(op);
4719 /* stack: H1 H2 (L1 op L2) */
4720 vrotb(3);
4721 vrotb(3);
4722 /* stack: (L1 op L2) H1 H2 */
4723 gen_op(op);
4724 /* stack: (L1 op L2) (H1 op H2) */
4726 /* stack: L H */
4727 lbuild(t);
4728 break;
4729 case TOK_SAR:
4730 case TOK_SHR:
4731 case TOK_SHL:
4732 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4733 t = vtop[-1].type.t;
4734 vswap();
4735 lexpand();
4736 vrotb(3);
4737 /* stack: L H shift */
4738 c = (int)vtop->c.i;
4739 /* constant: simpler */
4740 /* NOTE: all comments are for SHL. the other cases are
4741 done by swaping words */
4742 vpop();
4743 if (op != TOK_SHL)
4744 vswap();
4745 if (c >= 32) {
4746 /* stack: L H */
4747 vpop();
4748 if (c > 32) {
4749 vpushi(c - 32);
4750 gen_op(op);
4752 if (op != TOK_SAR) {
4753 vpushi(0);
4754 } else {
4755 gv_dup();
4756 vpushi(31);
4757 gen_op(TOK_SAR);
4759 vswap();
4760 } else {
4761 vswap();
4762 gv_dup();
4763 /* stack: H L L */
4764 vpushi(c);
4765 gen_op(op);
4766 vswap();
4767 vpushi(32 - c);
4768 if (op == TOK_SHL)
4769 gen_op(TOK_SHR);
4770 else
4771 gen_op(TOK_SHL);
4772 vrotb(3);
4773 /* stack: L L H */
4774 vpushi(c);
4775 if (op == TOK_SHL)
4776 gen_op(TOK_SHL);
4777 else
4778 gen_op(TOK_SHR);
4779 gen_op('|');
4781 if (op != TOK_SHL)
4782 vswap();
4783 lbuild(t);
4784 } else {
4785 /* XXX: should provide a faster fallback on x86 ? */
4786 switch(op) {
4787 case TOK_SAR:
4788 func = TOK___sardi3;
4789 goto gen_func;
4790 case TOK_SHR:
4791 func = TOK___shrdi3;
4792 goto gen_func;
4793 case TOK_SHL:
4794 func = TOK___shldi3;
4795 goto gen_func;
4798 break;
4799 default:
4800 /* compare operations */
4801 t = vtop->type.t;
4802 vswap();
4803 lexpand();
4804 vrotb(3);
4805 lexpand();
4806 /* stack: L1 H1 L2 H2 */
4807 tmp = vtop[-1];
4808 vtop[-1] = vtop[-2];
4809 vtop[-2] = tmp;
4810 /* stack: L1 L2 H1 H2 */
4811 /* compare high */
4812 op1 = op;
4813 /* when values are equal, we need to compare low words. since
4814 the jump is inverted, we invert the test too. */
4815 if (op1 == TOK_LT)
4816 op1 = TOK_LE;
4817 else if (op1 == TOK_GT)
4818 op1 = TOK_GE;
4819 else if (op1 == TOK_ULT)
4820 op1 = TOK_ULE;
4821 else if (op1 == TOK_UGT)
4822 op1 = TOK_UGE;
4823 a = 0;
4824 b = 0;
4825 gen_op(op1);
4826 if (op1 != TOK_NE) {
4827 a = gtst(1, 0);
4829 if (op != TOK_EQ) {
4830 /* generate non equal test */
4831 /* XXX: NOT PORTABLE yet */
4832 if (a == 0) {
4833 b = gtst(0, 0);
4834 } else {
4835 #ifdef TCC_TARGET_I386
4836 b = psym(0x850f, 0);
4837 #else
4838 error("not implemented");
4839 #endif
4842 /* compare low. Always unsigned */
4843 op1 = op;
4844 if (op1 == TOK_LT)
4845 op1 = TOK_ULT;
4846 else if (op1 == TOK_LE)
4847 op1 = TOK_ULE;
4848 else if (op1 == TOK_GT)
4849 op1 = TOK_UGT;
4850 else if (op1 == TOK_GE)
4851 op1 = TOK_UGE;
4852 gen_op(op1);
4853 a = gtst(1, a);
4854 gsym(b);
4855 vseti(VT_JMPI, a);
4856 break;
4860 /* handle integer constant optimizations and various machine
4861 independent opt */
4862 void gen_opic(int op)
4864 int fc, c1, c2, n;
4865 SValue *v1, *v2;
4867 v1 = vtop - 1;
4868 v2 = vtop;
4869 /* currently, we cannot do computations with forward symbols */
4870 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4871 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4872 if (c1 && c2) {
4873 fc = v2->c.i;
4874 switch(op) {
4875 case '+': v1->c.i += fc; break;
4876 case '-': v1->c.i -= fc; break;
4877 case '&': v1->c.i &= fc; break;
4878 case '^': v1->c.i ^= fc; break;
4879 case '|': v1->c.i |= fc; break;
4880 case '*': v1->c.i *= fc; break;
4882 case TOK_PDIV:
4883 case '/':
4884 case '%':
4885 case TOK_UDIV:
4886 case TOK_UMOD:
4887 /* if division by zero, generate explicit division */
4888 if (fc == 0) {
4889 if (const_wanted)
4890 error("division by zero in constant");
4891 goto general_case;
4893 switch(op) {
4894 default: v1->c.i /= fc; break;
4895 case '%': v1->c.i %= fc; break;
4896 case TOK_UDIV: v1->c.i = (unsigned)v1->c.i / fc; break;
4897 case TOK_UMOD: v1->c.i = (unsigned)v1->c.i % fc; break;
4899 break;
4900 case TOK_SHL: v1->c.i <<= fc; break;
4901 case TOK_SHR: v1->c.i = (unsigned)v1->c.i >> fc; break;
4902 case TOK_SAR: v1->c.i >>= fc; break;
4903 /* tests */
4904 case TOK_ULT: v1->c.i = (unsigned)v1->c.i < (unsigned)fc; break;
4905 case TOK_UGE: v1->c.i = (unsigned)v1->c.i >= (unsigned)fc; break;
4906 case TOK_EQ: v1->c.i = v1->c.i == fc; break;
4907 case TOK_NE: v1->c.i = v1->c.i != fc; break;
4908 case TOK_ULE: v1->c.i = (unsigned)v1->c.i <= (unsigned)fc; break;
4909 case TOK_UGT: v1->c.i = (unsigned)v1->c.i > (unsigned)fc; break;
4910 case TOK_LT: v1->c.i = v1->c.i < fc; break;
4911 case TOK_GE: v1->c.i = v1->c.i >= fc; break;
4912 case TOK_LE: v1->c.i = v1->c.i <= fc; break;
4913 case TOK_GT: v1->c.i = v1->c.i > fc; break;
4914 /* logical */
4915 case TOK_LAND: v1->c.i = v1->c.i && fc; break;
4916 case TOK_LOR: v1->c.i = v1->c.i || fc; break;
4917 default:
4918 goto general_case;
4920 vtop--;
4921 } else {
4922 /* if commutative ops, put c2 as constant */
4923 if (c1 && (op == '+' || op == '&' || op == '^' ||
4924 op == '|' || op == '*')) {
4925 vswap();
4926 swap(&c1, &c2);
4928 fc = vtop->c.i;
4929 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
4930 op == TOK_PDIV) &&
4931 fc == 1) ||
4932 ((op == '+' || op == '-' || op == '|' || op == '^' ||
4933 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
4934 fc == 0) ||
4935 (op == '&' &&
4936 fc == -1))) {
4937 /* nothing to do */
4938 vtop--;
4939 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
4940 /* try to use shifts instead of muls or divs */
4941 if (fc > 0 && (fc & (fc - 1)) == 0) {
4942 n = -1;
4943 while (fc) {
4944 fc >>= 1;
4945 n++;
4947 vtop->c.i = n;
4948 if (op == '*')
4949 op = TOK_SHL;
4950 else if (op == TOK_PDIV)
4951 op = TOK_SAR;
4952 else
4953 op = TOK_SHR;
4955 goto general_case;
4956 } else if (c2 && (op == '+' || op == '-') &&
4957 (vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
4958 (VT_CONST | VT_SYM)) {
4959 /* symbol + constant case */
4960 if (op == '-')
4961 fc = -fc;
4962 vtop--;
4963 vtop->c.i += fc;
4964 } else {
4965 general_case:
4966 if (!nocode_wanted) {
4967 /* call low level op generator */
4968 gen_opi(op);
4969 } else {
4970 vtop--;
4976 /* generate a floating point operation with constant propagation */
4977 void gen_opif(int op)
4979 int c1, c2;
4980 SValue *v1, *v2;
4981 long double f1, f2;
4983 v1 = vtop - 1;
4984 v2 = vtop;
4985 /* currently, we cannot do computations with forward symbols */
4986 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4987 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4988 if (c1 && c2) {
4989 if (v1->type.t == VT_FLOAT) {
4990 f1 = v1->c.f;
4991 f2 = v2->c.f;
4992 } else if (v1->type.t == VT_DOUBLE) {
4993 f1 = v1->c.d;
4994 f2 = v2->c.d;
4995 } else {
4996 f1 = v1->c.ld;
4997 f2 = v2->c.ld;
5000 /* NOTE: we only do constant propagation if finite number (not
5001 NaN or infinity) (ANSI spec) */
5002 if (!ieee_finite(f1) || !ieee_finite(f2))
5003 goto general_case;
5005 switch(op) {
5006 case '+': f1 += f2; break;
5007 case '-': f1 -= f2; break;
5008 case '*': f1 *= f2; break;
5009 case '/':
5010 if (f2 == 0.0) {
5011 if (const_wanted)
5012 error("division by zero in constant");
5013 goto general_case;
5015 f1 /= f2;
5016 break;
5017 /* XXX: also handles tests ? */
5018 default:
5019 goto general_case;
5021 /* XXX: overflow test ? */
5022 if (v1->type.t == VT_FLOAT) {
5023 v1->c.f = f1;
5024 } else if (v1->type.t == VT_DOUBLE) {
5025 v1->c.d = f1;
5026 } else {
5027 v1->c.ld = f1;
5029 vtop--;
5030 } else {
5031 general_case:
5032 if (!nocode_wanted) {
5033 gen_opf(op);
5034 } else {
5035 vtop--;
5040 static int pointed_size(CType *type)
5042 int align;
5043 return type_size(pointed_type(type), &align);
5046 #if 0
5047 void check_pointer_types(SValue *p1, SValue *p2)
5049 char buf1[256], buf2[256];
5050 int t1, t2;
5051 t1 = p1->t;
5052 t2 = p2->t;
5053 if (!is_compatible_types(t1, t2)) {
5054 type_to_str(buf1, sizeof(buf1), t1, NULL);
5055 type_to_str(buf2, sizeof(buf2), t2, NULL);
5056 error("incompatible pointers '%s' and '%s'", buf1, buf2);
5059 #endif
5061 /* generic gen_op: handles types problems */
5062 void gen_op(int op)
5064 int u, t1, t2, bt1, bt2, t;
5065 CType type1;
5067 t1 = vtop[-1].type.t;
5068 t2 = vtop[0].type.t;
5069 bt1 = t1 & VT_BTYPE;
5070 bt2 = t2 & VT_BTYPE;
5072 if (bt1 == VT_PTR || bt2 == VT_PTR) {
5073 /* at least one operand is a pointer */
5074 /* relationnal op: must be both pointers */
5075 if (op >= TOK_ULT && op <= TOK_GT) {
5076 // check_pointer_types(vtop, vtop - 1);
5077 /* pointers are handled are unsigned */
5078 t = VT_INT | VT_UNSIGNED;
5079 goto std_op;
5081 /* if both pointers, then it must be the '-' op */
5082 if (bt1 == VT_PTR && bt2 == VT_PTR) {
5083 if (op != '-')
5084 error("cannot use pointers here");
5085 // check_pointer_types(vtop - 1, vtop);
5086 /* XXX: check that types are compatible */
5087 u = pointed_size(&vtop[-1].type);
5088 gen_opic(op);
5089 /* set to integer type */
5090 vtop->type.t = VT_INT;
5091 vpushi(u);
5092 gen_op(TOK_PDIV);
5093 } else {
5094 /* exactly one pointer : must be '+' or '-'. */
5095 if (op != '-' && op != '+')
5096 error("cannot use pointers here");
5097 /* Put pointer as first operand */
5098 if (bt2 == VT_PTR) {
5099 vswap();
5100 swap(&t1, &t2);
5102 type1 = vtop[-1].type;
5103 /* XXX: cast to int ? (long long case) */
5104 vpushi(pointed_size(&vtop[-1].type));
5105 gen_op('*');
5106 #ifdef CONFIG_TCC_BCHECK
5107 /* if evaluating constant expression, no code should be
5108 generated, so no bound check */
5109 if (do_bounds_check && !const_wanted) {
5110 /* if bounded pointers, we generate a special code to
5111 test bounds */
5112 if (op == '-') {
5113 vpushi(0);
5114 vswap();
5115 gen_op('-');
5117 gen_bounded_ptr_add();
5118 } else
5119 #endif
5121 gen_opic(op);
5123 /* put again type if gen_opic() swaped operands */
5124 vtop->type = type1;
5126 } else if (is_float(bt1) || is_float(bt2)) {
5127 /* compute bigger type and do implicit casts */
5128 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
5129 t = VT_LDOUBLE;
5130 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
5131 t = VT_DOUBLE;
5132 } else {
5133 t = VT_FLOAT;
5135 /* floats can only be used for a few operations */
5136 if (op != '+' && op != '-' && op != '*' && op != '/' &&
5137 (op < TOK_ULT || op > TOK_GT))
5138 error("invalid operands for binary operation");
5139 goto std_op;
5140 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
5141 /* cast to biggest op */
5142 t = VT_LLONG;
5143 /* convert to unsigned if it does not fit in a long long */
5144 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
5145 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
5146 t |= VT_UNSIGNED;
5147 goto std_op;
5148 } else {
5149 /* integer operations */
5150 t = VT_INT;
5151 /* convert to unsigned if it does not fit in an integer */
5152 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
5153 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
5154 t |= VT_UNSIGNED;
5155 std_op:
5156 /* XXX: currently, some unsigned operations are explicit, so
5157 we modify them here */
5158 if (t & VT_UNSIGNED) {
5159 if (op == TOK_SAR)
5160 op = TOK_SHR;
5161 else if (op == '/')
5162 op = TOK_UDIV;
5163 else if (op == '%')
5164 op = TOK_UMOD;
5165 else if (op == TOK_LT)
5166 op = TOK_ULT;
5167 else if (op == TOK_GT)
5168 op = TOK_UGT;
5169 else if (op == TOK_LE)
5170 op = TOK_ULE;
5171 else if (op == TOK_GE)
5172 op = TOK_UGE;
5174 vswap();
5175 type1.t = t;
5176 gen_cast(&type1);
5177 vswap();
5178 /* special case for shifts and long long: we keep the shift as
5179 an integer */
5180 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
5181 type1.t = VT_INT;
5182 gen_cast(&type1);
5183 if (is_float(t))
5184 gen_opif(op);
5185 else if ((t & VT_BTYPE) == VT_LLONG)
5186 gen_opl(op);
5187 else
5188 gen_opic(op);
5189 if (op >= TOK_ULT && op <= TOK_GT) {
5190 /* relationnal op: the result is an int */
5191 vtop->type.t = VT_INT;
5192 } else {
5193 vtop->type.t = t;
5198 /* generic itof for unsigned long long case */
5199 void gen_cvt_itof1(int t)
5201 GFuncContext gf;
5203 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
5204 (VT_LLONG | VT_UNSIGNED)) {
5206 gfunc_start(&gf, FUNC_CDECL);
5207 gfunc_param(&gf);
5208 if (t == VT_FLOAT)
5209 vpush_global_sym(&func_old_type, TOK___ulltof);
5210 else if (t == VT_DOUBLE)
5211 vpush_global_sym(&func_old_type, TOK___ulltod);
5212 else
5213 vpush_global_sym(&func_old_type, TOK___ulltold);
5214 gfunc_call(&gf);
5215 vpushi(0);
5216 vtop->r = REG_FRET;
5217 } else {
5218 gen_cvt_itof(t);
5222 /* generic ftoi for unsigned long long case */
5223 void gen_cvt_ftoi1(int t)
5225 GFuncContext gf;
5226 int st;
5228 if (t == (VT_LLONG | VT_UNSIGNED)) {
5229 /* not handled natively */
5230 gfunc_start(&gf, FUNC_CDECL);
5231 st = vtop->type.t & VT_BTYPE;
5232 gfunc_param(&gf);
5233 if (st == VT_FLOAT)
5234 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
5235 else if (st == VT_DOUBLE)
5236 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
5237 else
5238 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
5239 gfunc_call(&gf);
5240 vpushi(0);
5241 vtop->r = REG_IRET;
5242 vtop->r2 = REG_LRET;
5243 } else {
5244 gen_cvt_ftoi(t);
5248 /* force char or short cast */
5249 void force_charshort_cast(int t)
5251 int bits, dbt;
5252 dbt = t & VT_BTYPE;
5253 /* XXX: add optimization if lvalue : just change type and offset */
5254 if (dbt == VT_BYTE)
5255 bits = 8;
5256 else
5257 bits = 16;
5258 if (t & VT_UNSIGNED) {
5259 vpushi((1 << bits) - 1);
5260 gen_op('&');
5261 } else {
5262 bits = 32 - bits;
5263 vpushi(bits);
5264 gen_op(TOK_SHL);
5265 vpushi(bits);
5266 gen_op(TOK_SAR);
5270 /* cast 'vtop' to 'type' */
5271 static void gen_cast(CType *type)
5273 int sbt, dbt, sf, df, c;
5275 /* special delayed cast for char/short */
5276 /* XXX: in some cases (multiple cascaded casts), it may still
5277 be incorrect */
5278 if (vtop->r & VT_MUSTCAST) {
5279 vtop->r &= ~VT_MUSTCAST;
5280 force_charshort_cast(vtop->type.t);
5283 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
5284 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
5286 if (sbt != dbt && !nocode_wanted) {
5287 sf = is_float(sbt);
5288 df = is_float(dbt);
5289 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
5290 if (sf && df) {
5291 /* convert from fp to fp */
5292 if (c) {
5293 /* constant case: we can do it now */
5294 /* XXX: in ISOC, cannot do it if error in convert */
5295 if (dbt == VT_FLOAT && sbt == VT_DOUBLE)
5296 vtop->c.f = (float)vtop->c.d;
5297 else if (dbt == VT_FLOAT && sbt == VT_LDOUBLE)
5298 vtop->c.f = (float)vtop->c.ld;
5299 else if (dbt == VT_DOUBLE && sbt == VT_FLOAT)
5300 vtop->c.d = (double)vtop->c.f;
5301 else if (dbt == VT_DOUBLE && sbt == VT_LDOUBLE)
5302 vtop->c.d = (double)vtop->c.ld;
5303 else if (dbt == VT_LDOUBLE && sbt == VT_FLOAT)
5304 vtop->c.ld = (long double)vtop->c.f;
5305 else if (dbt == VT_LDOUBLE && sbt == VT_DOUBLE)
5306 vtop->c.ld = (long double)vtop->c.d;
5307 } else {
5308 /* non constant case: generate code */
5309 gen_cvt_ftof(dbt);
5311 } else if (df) {
5312 /* convert int to fp */
5313 if (c) {
5314 switch(sbt) {
5315 case VT_LLONG | VT_UNSIGNED:
5316 case VT_LLONG:
5317 /* XXX: add const cases for long long */
5318 goto do_itof;
5319 case VT_INT | VT_UNSIGNED:
5320 switch(dbt) {
5321 case VT_FLOAT: vtop->c.f = (float)vtop->c.ui; break;
5322 case VT_DOUBLE: vtop->c.d = (double)vtop->c.ui; break;
5323 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.ui; break;
5325 break;
5326 default:
5327 switch(dbt) {
5328 case VT_FLOAT: vtop->c.f = (float)vtop->c.i; break;
5329 case VT_DOUBLE: vtop->c.d = (double)vtop->c.i; break;
5330 case VT_LDOUBLE: vtop->c.ld = (long double)vtop->c.i; break;
5332 break;
5334 } else {
5335 do_itof:
5336 gen_cvt_itof1(dbt);
5338 } else if (sf) {
5339 /* convert fp to int */
5340 /* we handle char/short/etc... with generic code */
5341 if (dbt != (VT_INT | VT_UNSIGNED) &&
5342 dbt != (VT_LLONG | VT_UNSIGNED) &&
5343 dbt != VT_LLONG)
5344 dbt = VT_INT;
5345 if (c) {
5346 switch(dbt) {
5347 case VT_LLONG | VT_UNSIGNED:
5348 case VT_LLONG:
5349 /* XXX: add const cases for long long */
5350 goto do_ftoi;
5351 case VT_INT | VT_UNSIGNED:
5352 switch(sbt) {
5353 case VT_FLOAT: vtop->c.ui = (unsigned int)vtop->c.d; break;
5354 case VT_DOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5355 case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
5357 break;
5358 default:
5359 /* int case */
5360 switch(sbt) {
5361 case VT_FLOAT: vtop->c.i = (int)vtop->c.d; break;
5362 case VT_DOUBLE: vtop->c.i = (int)vtop->c.d; break;
5363 case VT_LDOUBLE: vtop->c.i = (int)vtop->c.d; break;
5365 break;
5367 } else {
5368 do_ftoi:
5369 gen_cvt_ftoi1(dbt);
5371 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
5372 /* additional cast for char/short/bool... */
5373 vtop->type.t = dbt;
5374 gen_cast(type);
5376 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
5377 if ((sbt & VT_BTYPE) != VT_LLONG) {
5378 /* scalar to long long */
5379 if (c) {
5380 if (sbt == (VT_INT | VT_UNSIGNED))
5381 vtop->c.ll = vtop->c.ui;
5382 else
5383 vtop->c.ll = vtop->c.i;
5384 } else {
5385 /* machine independent conversion */
5386 gv(RC_INT);
5387 /* generate high word */
5388 if (sbt == (VT_INT | VT_UNSIGNED)) {
5389 vpushi(0);
5390 gv(RC_INT);
5391 } else {
5392 gv_dup();
5393 vpushi(31);
5394 gen_op(TOK_SAR);
5396 /* patch second register */
5397 vtop[-1].r2 = vtop->r;
5398 vpop();
5401 } else if (dbt == VT_BOOL) {
5402 /* scalar to bool */
5403 vpushi(0);
5404 gen_op(TOK_NE);
5405 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
5406 (dbt & VT_BTYPE) == VT_SHORT) {
5407 force_charshort_cast(dbt);
5408 } else if ((dbt & VT_BTYPE) == VT_INT) {
5409 /* scalar to int */
5410 if (sbt == VT_LLONG) {
5411 /* from long long: just take low order word */
5412 lexpand();
5413 vpop();
5415 /* if lvalue and single word type, nothing to do because
5416 the lvalue already contains the real type size (see
5417 VT_LVAL_xxx constants) */
5420 vtop->type = *type;
5423 /* return type size. Put alignment at 'a' */
5424 static int type_size(CType *type, int *a)
5426 Sym *s;
5427 int bt;
5429 bt = type->t & VT_BTYPE;
5430 if (bt == VT_STRUCT) {
5431 /* struct/union */
5432 s = type->ref;
5433 *a = s->r;
5434 return s->c;
5435 } else if (bt == VT_PTR) {
5436 if (type->t & VT_ARRAY) {
5437 s = type->ref;
5438 return type_size(&s->type, a) * s->c;
5439 } else {
5440 *a = PTR_SIZE;
5441 return PTR_SIZE;
5443 } else if (bt == VT_LDOUBLE) {
5444 *a = LDOUBLE_ALIGN;
5445 return LDOUBLE_SIZE;
5446 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
5447 *a = 4; /* XXX: i386 specific */
5448 return 8;
5449 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
5450 *a = 4;
5451 return 4;
5452 } else if (bt == VT_SHORT) {
5453 *a = 2;
5454 return 2;
5455 } else {
5456 /* char, void, function, _Bool */
5457 *a = 1;
5458 return 1;
5462 /* return the pointed type of t */
5463 static inline CType *pointed_type(CType *type)
5465 return &type->ref->type;
5468 /* modify type so that its it is a pointer to type. */
5469 static void mk_pointer(CType *type)
5471 Sym *s;
5472 s = sym_push(SYM_FIELD, type, 0, -1);
5473 type->t = VT_PTR | (type->t & ~VT_TYPE);
5474 type->ref = s;
5477 static int is_compatible_types(CType *type1, CType *type2)
5479 Sym *s1, *s2;
5480 int bt1, bt2, t1, t2;
5482 t1 = type1->t & VT_TYPE;
5483 t2 = type2->t & VT_TYPE;
5484 bt1 = t1 & VT_BTYPE;
5485 bt2 = t2 & VT_BTYPE;
5486 if (bt1 == VT_PTR) {
5487 type1 = pointed_type(type1);
5488 /* if function, then convert implicitely to function pointer */
5489 if (bt2 != VT_FUNC) {
5490 if (bt2 != VT_PTR)
5491 return 0;
5492 type2 = pointed_type(type2);
5494 /* void matches everything */
5495 /* XXX: not fully compliant */
5496 if ((type1->t & VT_TYPE) == VT_VOID || (type2->t & VT_TYPE) == VT_VOID)
5497 return 1;
5498 return is_compatible_types(type1, type2);
5499 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
5500 return (type1->ref == type2->ref);
5501 } else if (bt1 == VT_FUNC) {
5502 if (bt2 != VT_FUNC)
5503 return 0;
5504 s1 = type1->ref;
5505 s2 = type2->ref;
5506 if (!is_compatible_types(&s1->type, &s2->type))
5507 return 0;
5508 /* XXX: not complete */
5509 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
5510 return 1;
5511 if (s1->c != s2->c)
5512 return 0;
5513 while (s1 != NULL) {
5514 if (s2 == NULL)
5515 return 0;
5516 if (!is_compatible_types(&s1->type, &s2->type))
5517 return 0;
5518 s1 = s1->next;
5519 s2 = s2->next;
5521 if (s2)
5522 return 0;
5523 return 1;
5524 } else {
5525 /* XXX: not complete */
5526 return 1;
5530 /* print a type. If 'varstr' is not NULL, then the variable is also
5531 printed in the type */
5532 /* XXX: union */
5533 /* XXX: add array and function pointers */
5534 void type_to_str(char *buf, int buf_size,
5535 CType *type, const char *varstr)
5537 int bt, v, t;
5538 Sym *s, *sa;
5539 char buf1[256];
5540 const char *tstr;
5542 t = type->t & VT_TYPE;
5543 bt = t & VT_BTYPE;
5544 buf[0] = '\0';
5545 if (t & VT_UNSIGNED)
5546 pstrcat(buf, buf_size, "unsigned ");
5547 switch(bt) {
5548 case VT_VOID:
5549 tstr = "void";
5550 goto add_tstr;
5551 case VT_BOOL:
5552 tstr = "_Bool";
5553 goto add_tstr;
5554 case VT_BYTE:
5555 tstr = "char";
5556 goto add_tstr;
5557 case VT_SHORT:
5558 tstr = "short";
5559 goto add_tstr;
5560 case VT_INT:
5561 tstr = "int";
5562 goto add_tstr;
5563 case VT_LONG:
5564 tstr = "long";
5565 goto add_tstr;
5566 case VT_LLONG:
5567 tstr = "long long";
5568 goto add_tstr;
5569 case VT_FLOAT:
5570 tstr = "float";
5571 goto add_tstr;
5572 case VT_DOUBLE:
5573 tstr = "double";
5574 goto add_tstr;
5575 case VT_LDOUBLE:
5576 tstr = "long double";
5577 add_tstr:
5578 pstrcat(buf, buf_size, tstr);
5579 break;
5580 case VT_ENUM:
5581 case VT_STRUCT:
5582 if (bt == VT_STRUCT)
5583 tstr = "struct ";
5584 else
5585 tstr = "enum ";
5586 pstrcat(buf, buf_size, tstr);
5587 v = type->ref->v & ~SYM_STRUCT;
5588 if (v >= SYM_FIRST_ANOM)
5589 pstrcat(buf, buf_size, "<anonymous>");
5590 else
5591 pstrcat(buf, buf_size, get_tok_str(v, NULL));
5592 break;
5593 case VT_FUNC:
5594 s = type->ref;
5595 type_to_str(buf, buf_size, &s->type, varstr);
5596 pstrcat(buf, buf_size, "(");
5597 sa = s->next;
5598 while (sa != NULL) {
5599 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
5600 pstrcat(buf, buf_size, buf1);
5601 sa = sa->next;
5602 if (sa)
5603 pstrcat(buf, buf_size, ", ");
5605 pstrcat(buf, buf_size, ")");
5606 goto no_var;
5607 case VT_PTR:
5608 s = type->ref;
5609 pstrcpy(buf1, sizeof(buf1), "*");
5610 if (varstr)
5611 pstrcat(buf1, sizeof(buf1), varstr);
5612 type_to_str(buf, buf_size, &s->type, buf1);
5613 goto no_var;
5615 if (varstr) {
5616 pstrcat(buf, buf_size, " ");
5617 pstrcat(buf, buf_size, varstr);
5619 no_var: ;
5622 /* verify type compatibility to store vtop in 'dt' type, and generate
5623 casts if needed. */
5624 static void gen_assign_cast(CType *dt)
5626 CType *st;
5627 char buf1[256], buf2[256];
5628 int dbt, sbt;
5630 st = &vtop->type; /* source type */
5631 dbt = dt->t & VT_BTYPE;
5632 sbt = st->t & VT_BTYPE;
5633 if (dbt == VT_PTR) {
5634 /* special cases for pointers */
5635 /* a function is implicitely a function pointer */
5636 if (sbt == VT_FUNC) {
5637 if (!is_compatible_types(pointed_type(dt), st))
5638 goto error;
5639 else
5640 goto type_ok;
5642 /* '0' can also be a pointer */
5643 if (sbt == VT_INT &&
5644 ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) &&
5645 vtop->c.i == 0)
5646 goto type_ok;
5647 /* accept implicit pointer to integer cast with warning */
5648 if (sbt == VT_BYTE || sbt == VT_SHORT ||
5649 sbt == VT_INT || sbt == VT_LLONG) {
5650 warning("assignment makes pointer from integer without a cast");
5651 goto type_ok;
5653 } else if (dbt == VT_BYTE || dbt == VT_SHORT ||
5654 dbt == VT_INT || dbt == VT_LLONG) {
5655 if (sbt == VT_PTR || sbt == VT_FUNC) {
5656 warning("assignment makes integer from pointer without a cast");
5657 goto type_ok;
5660 if (!is_compatible_types(dt, st)) {
5661 error:
5662 type_to_str(buf1, sizeof(buf1), st, NULL);
5663 type_to_str(buf2, sizeof(buf2), dt, NULL);
5664 error("cannot cast '%s' to '%s'", buf1, buf2);
5666 type_ok:
5667 gen_cast(dt);
5670 /* store vtop in lvalue pushed on stack */
5671 void vstore(void)
5673 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
5674 GFuncContext gf;
5676 ft = vtop[-1].type.t;
5677 sbt = vtop->type.t & VT_BTYPE;
5678 dbt = ft & VT_BTYPE;
5679 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
5680 (sbt == VT_INT && dbt == VT_SHORT)) {
5681 /* optimize char/short casts */
5682 delayed_cast = VT_MUSTCAST;
5683 vtop->type.t = ft & VT_TYPE;
5684 } else {
5685 delayed_cast = 0;
5686 gen_assign_cast(&vtop[-1].type);
5689 if (sbt == VT_STRUCT) {
5690 /* if structure, only generate pointer */
5691 /* structure assignment : generate memcpy */
5692 /* XXX: optimize if small size */
5693 if (!nocode_wanted) {
5694 vdup();
5695 gfunc_start(&gf, FUNC_CDECL);
5696 /* type size */
5697 size = type_size(&vtop->type, &align);
5698 vpushi(size);
5699 gfunc_param(&gf);
5700 /* source */
5701 vtop->type.t = VT_INT;
5702 gaddrof();
5703 gfunc_param(&gf);
5704 /* destination */
5705 vswap();
5706 vtop->type.t = VT_INT;
5707 gaddrof();
5708 gfunc_param(&gf);
5710 save_regs(0);
5711 vpush_global_sym(&func_old_type, TOK_memcpy);
5712 gfunc_call(&gf);
5713 } else {
5714 vswap();
5715 vpop();
5717 /* leave source on stack */
5718 } else if (ft & VT_BITFIELD) {
5719 /* bitfield store handling */
5720 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
5721 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5722 /* remove bit field info to avoid loops */
5723 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
5725 /* duplicate destination */
5726 vdup();
5727 vtop[-1] = vtop[-2];
5729 /* mask and shift source */
5730 vpushi((1 << bit_size) - 1);
5731 gen_op('&');
5732 vpushi(bit_pos);
5733 gen_op(TOK_SHL);
5734 /* load destination, mask and or with source */
5735 vswap();
5736 vpushi(~(((1 << bit_size) - 1) << bit_pos));
5737 gen_op('&');
5738 gen_op('|');
5739 /* store result */
5740 vstore();
5741 } else {
5742 #ifdef CONFIG_TCC_BCHECK
5743 /* bound check case */
5744 if (vtop[-1].r & VT_MUSTBOUND) {
5745 vswap();
5746 gbound();
5747 vswap();
5749 #endif
5750 if (!nocode_wanted) {
5751 rc = RC_INT;
5752 if (is_float(ft))
5753 rc = RC_FLOAT;
5754 r = gv(rc); /* generate value */
5755 /* if lvalue was saved on stack, must read it */
5756 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
5757 SValue sv;
5758 t = get_reg(RC_INT);
5759 sv.type.t = VT_INT;
5760 sv.r = VT_LOCAL | VT_LVAL;
5761 sv.c.ul = vtop[-1].c.ul;
5762 load(t, &sv);
5763 vtop[-1].r = t | VT_LVAL;
5765 store(r, vtop - 1);
5766 /* two word case handling : store second register at word + 4 */
5767 if ((ft & VT_BTYPE) == VT_LLONG) {
5768 vswap();
5769 /* convert to int to increment easily */
5770 vtop->type.t = VT_INT;
5771 gaddrof();
5772 vpushi(4);
5773 gen_op('+');
5774 vtop->r |= VT_LVAL;
5775 vswap();
5776 /* XXX: it works because r2 is spilled last ! */
5777 store(vtop->r2, vtop - 1);
5780 vswap();
5781 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5782 vtop->r |= delayed_cast;
5786 /* post defines POST/PRE add. c is the token ++ or -- */
5787 void inc(int post, int c)
5789 test_lvalue();
5790 vdup(); /* save lvalue */
5791 if (post) {
5792 gv_dup(); /* duplicate value */
5793 vrotb(3);
5794 vrotb(3);
5796 /* add constant */
5797 vpushi(c - TOK_MID);
5798 gen_op('+');
5799 vstore(); /* store value */
5800 if (post)
5801 vpop(); /* if post op, return saved value */
5804 /* Parse GNUC __attribute__ extension. Currently, the following
5805 extensions are recognized:
5806 - aligned(n) : set data/function alignment.
5807 - section(x) : generate data/code in this section.
5808 - unused : currently ignored, but may be used someday.
5810 static void parse_attribute(AttributeDef *ad)
5812 int t, n;
5814 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
5815 next();
5816 skip('(');
5817 skip('(');
5818 while (tok != ')') {
5819 if (tok < TOK_IDENT)
5820 expect("attribute name");
5821 t = tok;
5822 next();
5823 switch(t) {
5824 case TOK_SECTION1:
5825 case TOK_SECTION2:
5826 skip('(');
5827 if (tok != TOK_STR)
5828 expect("section name");
5829 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
5830 next();
5831 skip(')');
5832 break;
5833 case TOK_ALIGNED1:
5834 case TOK_ALIGNED2:
5835 if (tok == '(') {
5836 next();
5837 n = expr_const();
5838 if (n <= 0 || (n & (n - 1)) != 0)
5839 error("alignment must be a positive power of two");
5840 skip(')');
5841 } else {
5842 n = MAX_ALIGN;
5844 ad->aligned = n;
5845 break;
5846 case TOK_UNUSED1:
5847 case TOK_UNUSED2:
5848 /* currently, no need to handle it because tcc does not
5849 track unused objects */
5850 break;
5851 case TOK_NORETURN1:
5852 case TOK_NORETURN2:
5853 /* currently, no need to handle it because tcc does not
5854 track unused objects */
5855 break;
5856 case TOK_CDECL1:
5857 case TOK_CDECL2:
5858 case TOK_CDECL3:
5859 ad->func_call = FUNC_CDECL;
5860 break;
5861 case TOK_STDCALL1:
5862 case TOK_STDCALL2:
5863 case TOK_STDCALL3:
5864 ad->func_call = FUNC_STDCALL;
5865 break;
5866 default:
5867 // warning("'%s' attribute ignored", get_tok_str(t, NULL));
5868 /* skip parameters */
5869 /* XXX: skip parenthesis too */
5870 if (tok == '(') {
5871 next();
5872 while (tok != ')' && tok != -1)
5873 next();
5874 next();
5876 break;
5878 if (tok != ',')
5879 break;
5880 next();
5882 skip(')');
5883 skip(')');
5887 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
5888 static void struct_decl(CType *type, int u)
5890 int a, v, size, align, maxalign, c, offset;
5891 int bit_size, bit_pos, bsize, bt, lbit_pos;
5892 Sym *s, *ss, **ps;
5893 AttributeDef ad;
5894 CType type1, btype;
5896 a = tok; /* save decl type */
5897 next();
5898 if (tok != '{') {
5899 v = tok;
5900 next();
5901 /* struct already defined ? return it */
5902 if (v < TOK_IDENT)
5903 expect("struct/union/enum name");
5904 s = struct_find(v);
5905 if (s) {
5906 if (s->type.t != a)
5907 error("invalid type");
5908 goto do_decl;
5910 } else {
5911 v = anon_sym++;
5913 type1.t = a;
5914 s = sym_push(v | SYM_STRUCT, &type1, 0, 0);
5915 /* put struct/union/enum name in type */
5916 do_decl:
5917 type->t = u;
5918 type->ref = s;
5920 if (tok == '{') {
5921 next();
5922 if (s->c)
5923 error("struct/union/enum already defined");
5924 /* cannot be empty */
5925 c = 0;
5926 /* non empty enums are not allowed */
5927 if (a == TOK_ENUM) {
5928 for(;;) {
5929 v = tok;
5930 if (v < TOK_UIDENT)
5931 expect("identifier");
5932 next();
5933 if (tok == '=') {
5934 next();
5935 c = expr_const();
5937 /* enum symbols have static storage */
5938 ss = sym_push(v, &int_type, VT_CONST, c);
5939 ss->type.t |= VT_STATIC;
5940 if (tok != ',')
5941 break;
5942 next();
5943 c++;
5944 /* NOTE: we accept a trailing comma */
5945 if (tok == '}')
5946 break;
5948 skip('}');
5949 } else {
5950 maxalign = 1;
5951 ps = &s->next;
5952 bit_pos = 0;
5953 offset = 0;
5954 while (tok != '}') {
5955 parse_btype(&btype, &ad);
5956 while (1) {
5957 bit_size = -1;
5958 v = 0;
5959 type1 = btype;
5960 if (tok != ':') {
5961 type_decl(&type1, &ad, &v, TYPE_DIRECT);
5962 if ((type1.t & VT_BTYPE) == VT_FUNC ||
5963 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
5964 error("invalid type for '%s'",
5965 get_tok_str(v, NULL));
5967 if (tok == ':') {
5968 next();
5969 bit_size = expr_const();
5970 /* XXX: handle v = 0 case for messages */
5971 if (bit_size < 0)
5972 error("negative width in bit-field '%s'",
5973 get_tok_str(v, NULL));
5974 if (v && bit_size == 0)
5975 error("zero width for bit-field '%s'",
5976 get_tok_str(v, NULL));
5978 size = type_size(&type1, &align);
5979 lbit_pos = 0;
5980 if (bit_size >= 0) {
5981 bt = type1.t & VT_BTYPE;
5982 if (bt != VT_INT &&
5983 bt != VT_BYTE &&
5984 bt != VT_SHORT &&
5985 bt != VT_ENUM)
5986 error("bitfields must have scalar type");
5987 bsize = size * 8;
5988 if (bit_size > bsize) {
5989 error("width of '%s' exceeds its type",
5990 get_tok_str(v, NULL));
5991 } else if (bit_size == bsize) {
5992 /* no need for bit fields */
5993 bit_pos = 0;
5994 } else if (bit_size == 0) {
5995 /* XXX: what to do if only padding in a
5996 structure ? */
5997 /* zero size: means to pad */
5998 if (bit_pos > 0)
5999 bit_pos = bsize;
6000 } else {
6001 /* we do not have enough room ? */
6002 if ((bit_pos + bit_size) > bsize)
6003 bit_pos = 0;
6004 lbit_pos = bit_pos;
6005 /* XXX: handle LSB first */
6006 type1.t |= VT_BITFIELD |
6007 (bit_pos << VT_STRUCT_SHIFT) |
6008 (bit_size << (VT_STRUCT_SHIFT + 6));
6009 bit_pos += bit_size;
6011 } else {
6012 bit_pos = 0;
6014 if (v) {
6015 /* add new memory data only if starting
6016 bit field */
6017 if (lbit_pos == 0) {
6018 if (a == TOK_STRUCT) {
6019 c = (c + align - 1) & -align;
6020 offset = c;
6021 c += size;
6022 } else {
6023 offset = 0;
6024 if (size > c)
6025 c = size;
6027 if (align > maxalign)
6028 maxalign = align;
6030 #if 0
6031 printf("add field %s offset=%d",
6032 get_tok_str(v, NULL), offset);
6033 if (type1.t & VT_BITFIELD) {
6034 printf(" pos=%d size=%d",
6035 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
6036 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
6038 printf("\n");
6039 #endif
6040 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
6041 *ps = ss;
6042 ps = &ss->next;
6044 if (tok == ';' || tok == TOK_EOF)
6045 break;
6046 skip(',');
6048 skip(';');
6050 skip('}');
6051 /* store size and alignment */
6052 s->c = (c + maxalign - 1) & -maxalign;
6053 s->r = maxalign;
6058 /* return 0 if no type declaration. otherwise, return the basic type
6059 and skip it.
6061 static int parse_btype(CType *type, AttributeDef *ad)
6063 int t, u, type_found;
6064 Sym *s;
6065 CType type1;
6067 memset(ad, 0, sizeof(AttributeDef));
6068 type_found = 0;
6069 t = 0;
6070 while(1) {
6071 switch(tok) {
6072 case TOK_EXTENSION:
6073 /* currently, we really ignore extension */
6074 next();
6075 continue;
6077 /* basic types */
6078 case TOK_CHAR:
6079 u = VT_BYTE;
6080 basic_type:
6081 next();
6082 basic_type1:
6083 if ((t & VT_BTYPE) != 0)
6084 error("too many basic types");
6085 t |= u;
6086 break;
6087 case TOK_VOID:
6088 u = VT_VOID;
6089 goto basic_type;
6090 case TOK_SHORT:
6091 u = VT_SHORT;
6092 goto basic_type;
6093 case TOK_INT:
6094 next();
6095 break;
6096 case TOK_LONG:
6097 next();
6098 if ((t & VT_BTYPE) == VT_DOUBLE) {
6099 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6100 } else if ((t & VT_BTYPE) == VT_LONG) {
6101 t = (t & ~VT_BTYPE) | VT_LLONG;
6102 } else {
6103 u = VT_LONG;
6104 goto basic_type1;
6106 break;
6107 case TOK_BOOL:
6108 u = VT_BOOL;
6109 goto basic_type;
6110 case TOK_FLOAT:
6111 u = VT_FLOAT;
6112 goto basic_type;
6113 case TOK_DOUBLE:
6114 next();
6115 if ((t & VT_BTYPE) == VT_LONG) {
6116 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
6117 } else {
6118 u = VT_DOUBLE;
6119 goto basic_type1;
6121 break;
6122 case TOK_ENUM:
6123 struct_decl(&type1, VT_ENUM);
6124 basic_type2:
6125 u = type1.t;
6126 type->ref = type1.ref;
6127 goto basic_type1;
6128 case TOK_STRUCT:
6129 case TOK_UNION:
6130 struct_decl(&type1, VT_STRUCT);
6131 goto basic_type2;
6133 /* type modifiers */
6134 case TOK_CONST1:
6135 case TOK_CONST2:
6136 case TOK_CONST3:
6137 case TOK_VOLATILE1:
6138 case TOK_VOLATILE2:
6139 case TOK_VOLATILE3:
6140 case TOK_REGISTER:
6141 case TOK_SIGNED1:
6142 case TOK_SIGNED2:
6143 case TOK_SIGNED3:
6144 case TOK_AUTO:
6145 case TOK_RESTRICT1:
6146 case TOK_RESTRICT2:
6147 case TOK_RESTRICT3:
6148 next();
6149 break;
6150 case TOK_UNSIGNED:
6151 t |= VT_UNSIGNED;
6152 next();
6153 break;
6155 /* storage */
6156 case TOK_EXTERN:
6157 t |= VT_EXTERN;
6158 next();
6159 break;
6160 case TOK_STATIC:
6161 t |= VT_STATIC;
6162 next();
6163 break;
6164 case TOK_TYPEDEF:
6165 t |= VT_TYPEDEF;
6166 next();
6167 break;
6168 case TOK_INLINE1:
6169 case TOK_INLINE2:
6170 case TOK_INLINE3:
6171 t |= VT_INLINE;
6172 next();
6173 break;
6175 /* GNUC attribute */
6176 case TOK_ATTRIBUTE1:
6177 case TOK_ATTRIBUTE2:
6178 parse_attribute(ad);
6179 break;
6180 /* GNUC typeof */
6181 case TOK_TYPEOF1:
6182 case TOK_TYPEOF2:
6183 case TOK_TYPEOF3:
6184 next();
6185 parse_expr_type(&type1);
6186 goto basic_type2;
6187 default:
6188 s = sym_find(tok);
6189 if (!s || !(s->type.t & VT_TYPEDEF))
6190 goto the_end;
6191 t |= (s->type.t & ~VT_TYPEDEF);
6192 type->ref = s->type.ref;
6193 next();
6194 break;
6196 type_found = 1;
6198 the_end:
6199 /* long is never used as type */
6200 if ((t & VT_BTYPE) == VT_LONG)
6201 t = (t & ~VT_BTYPE) | VT_INT;
6202 type->t = t;
6203 return type_found;
6206 /* convert a function parameter type (array to pointer and function to
6207 function pointer) */
6208 static inline void convert_parameter_type(CType *pt)
6210 /* array must be transformed to pointer according to ANSI C */
6211 pt->t &= ~VT_ARRAY;
6212 if ((pt->t & VT_BTYPE) == VT_FUNC) {
6213 mk_pointer(pt);
6217 static void post_type(CType *type, AttributeDef *ad)
6219 int n, l, t1;
6220 Sym **plast, *s, *first;
6221 AttributeDef ad1;
6222 CType pt;
6224 if (tok == '(') {
6225 /* function declaration */
6226 next();
6227 l = 0;
6228 first = NULL;
6229 plast = &first;
6230 while (tok != ')') {
6231 /* read param name and compute offset */
6232 if (l != FUNC_OLD) {
6233 if (!parse_btype(&pt, &ad1)) {
6234 if (l) {
6235 error("invalid type");
6236 } else {
6237 l = FUNC_OLD;
6238 goto old_proto;
6241 l = FUNC_NEW;
6242 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
6243 break;
6244 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
6245 if ((pt.t & VT_BTYPE) == VT_VOID)
6246 error("parameter declared as void");
6247 } else {
6248 old_proto:
6249 n = tok;
6250 pt.t = VT_INT;
6251 next();
6253 convert_parameter_type(&pt);
6254 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
6255 *plast = s;
6256 plast = &s->next;
6257 if (tok == ',') {
6258 next();
6259 if (l == FUNC_NEW && tok == TOK_DOTS) {
6260 l = FUNC_ELLIPSIS;
6261 next();
6262 break;
6266 /* if no parameters, then old type prototype */
6267 if (l == 0)
6268 l = FUNC_OLD;
6269 skip(')');
6270 t1 = type->t & VT_STORAGE;
6271 type->t &= ~VT_STORAGE;
6272 post_type(type, ad);
6273 /* we push a anonymous symbol which will contain the function prototype */
6274 s = sym_push(SYM_FIELD, type, ad->func_call, l);
6275 s->next = first;
6276 type->t = t1 | VT_FUNC;
6277 type->ref = s;
6278 } else if (tok == '[') {
6279 /* array definition */
6280 next();
6281 n = -1;
6282 if (tok != ']') {
6283 n = expr_const();
6284 if (n < 0)
6285 error("invalid array size");
6287 skip(']');
6288 /* parse next post type */
6289 t1 = type->t & VT_STORAGE;
6290 type->t &= ~VT_STORAGE;
6291 post_type(type, ad);
6293 /* we push a anonymous symbol which will contain the array
6294 element type */
6295 s = sym_push(SYM_FIELD, type, 0, n);
6296 type->t = t1 | VT_ARRAY | VT_PTR;
6297 type->ref = s;
6301 /* Parse a type declaration (except basic type), and return the type
6302 in 'type'. 'td' is a bitmask indicating which kind of type decl is
6303 expected. 'type' should contain the basic type. 'ad' is the
6304 attribute definition of the basic type. It can be modified by
6305 type_decl().
6307 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
6309 Sym *s;
6310 CType type1, *type2;
6312 while (tok == '*') {
6313 next();
6314 redo:
6315 switch(tok) {
6316 case TOK_CONST1:
6317 case TOK_CONST2:
6318 case TOK_CONST3:
6319 case TOK_VOLATILE1:
6320 case TOK_VOLATILE2:
6321 case TOK_VOLATILE3:
6322 case TOK_RESTRICT1:
6323 case TOK_RESTRICT2:
6324 case TOK_RESTRICT3:
6325 next();
6326 goto redo;
6328 mk_pointer(type);
6331 /* XXX: clarify attribute handling */
6332 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6333 parse_attribute(ad);
6335 /* recursive type */
6336 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
6337 type1.t = 0; /* XXX: same as int */
6338 if (tok == '(') {
6339 next();
6340 /* XXX: this is not correct to modify 'ad' at this point, but
6341 the syntax is not clear */
6342 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6343 parse_attribute(ad);
6344 type_decl(&type1, ad, v, td);
6345 skip(')');
6346 } else {
6347 /* type identifier */
6348 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
6349 *v = tok;
6350 next();
6351 } else {
6352 if (!(td & TYPE_ABSTRACT))
6353 expect("identifier");
6354 *v = 0;
6357 post_type(type, ad);
6358 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
6359 parse_attribute(ad);
6360 if (!type1.t)
6361 return;
6362 /* append type at the end of type1 */
6363 type2 = &type1;
6364 for(;;) {
6365 s = type2->ref;
6366 type2 = &s->type;
6367 if (!type2->t) {
6368 *type2 = *type;
6369 break;
6372 *type = type1;
6375 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
6376 static int lvalue_type(int t)
6378 int bt, r;
6379 r = VT_LVAL;
6380 bt = t & VT_BTYPE;
6381 if (bt == VT_BYTE || bt == VT_BOOL)
6382 r |= VT_LVAL_BYTE;
6383 else if (bt == VT_SHORT)
6384 r |= VT_LVAL_SHORT;
6385 else
6386 return r;
6387 if (t & VT_UNSIGNED)
6388 r |= VT_LVAL_UNSIGNED;
6389 return r;
6392 /* indirection with full error checking and bound check */
6393 static void indir(void)
6395 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
6396 expect("pointer");
6397 if ((vtop->r & VT_LVAL) && !nocode_wanted)
6398 gv(RC_INT);
6399 vtop->type = *pointed_type(&vtop->type);
6400 /* an array is never an lvalue */
6401 if (!(vtop->type.t & VT_ARRAY)) {
6402 vtop->r |= lvalue_type(vtop->type.t);
6403 /* if bound checking, the referenced pointer must be checked */
6404 if (do_bounds_check)
6405 vtop->r |= VT_MUSTBOUND;
6409 /* pass a parameter to a function and do type checking and casting */
6410 void gfunc_param_typed(GFuncContext *gf, Sym *func, Sym *arg)
6412 int func_type;
6413 CType type;
6415 func_type = func->c;
6416 if (func_type == FUNC_OLD ||
6417 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
6418 /* default casting : only need to convert float to double */
6419 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
6420 type.t = VT_DOUBLE;
6421 gen_cast(&type);
6423 } else if (arg == NULL) {
6424 error("too many arguments to function");
6425 } else {
6426 gen_assign_cast(&arg->type);
6428 if (!nocode_wanted) {
6429 gfunc_param(gf);
6430 } else {
6431 vpop();
6435 /* parse an expression of the form '(type)' or '(expr)' and return its
6436 type */
6437 static void parse_expr_type(CType *type)
6439 int n;
6440 AttributeDef ad;
6442 skip('(');
6443 if (parse_btype(type, &ad)) {
6444 type_decl(type, &ad, &n, TYPE_ABSTRACT);
6445 } else {
6446 expr_type(type);
6448 skip(')');
6451 static void vpush_tokc(int t)
6453 CType type;
6454 type.t = t;
6455 vsetc(&type, VT_CONST, &tokc);
6458 static void unary(void)
6460 int n, t, align, size, r;
6461 CType type;
6462 Sym *s;
6463 GFuncContext gf;
6464 AttributeDef ad;
6466 /* XXX: GCC 2.95.3 does not generate a table although it should be
6467 better here */
6468 tok_next:
6469 switch(tok) {
6470 case TOK_EXTENSION:
6471 next();
6472 goto tok_next;
6473 case TOK_CINT:
6474 case TOK_CCHAR:
6475 case TOK_LCHAR:
6476 vpushi(tokc.i);
6477 next();
6478 break;
6479 case TOK_CUINT:
6480 vpush_tokc(VT_INT | VT_UNSIGNED);
6481 next();
6482 break;
6483 case TOK_CLLONG:
6484 vpush_tokc(VT_LLONG);
6485 next();
6486 break;
6487 case TOK_CULLONG:
6488 vpush_tokc(VT_LLONG | VT_UNSIGNED);
6489 next();
6490 break;
6491 case TOK_CFLOAT:
6492 vpush_tokc(VT_FLOAT);
6493 next();
6494 break;
6495 case TOK_CDOUBLE:
6496 vpush_tokc(VT_DOUBLE);
6497 next();
6498 break;
6499 case TOK_CLDOUBLE:
6500 vpush_tokc(VT_LDOUBLE);
6501 next();
6502 break;
6503 case TOK___FUNCTION__:
6504 if (!gnu_ext)
6505 goto tok_identifier;
6506 /* fall thru */
6507 case TOK___FUNC__:
6509 void *ptr;
6510 int len;
6511 /* special function name identifier */
6512 len = strlen(funcname) + 1;
6513 /* generate char[len] type */
6514 type.t = VT_BYTE;
6515 mk_pointer(&type);
6516 type.t |= VT_ARRAY;
6517 type.ref->c = len;
6518 vpush_ref(&type, data_section, data_section->data_offset, len);
6519 ptr = section_ptr_add(data_section, len);
6520 memcpy(ptr, funcname, len);
6521 next();
6523 break;
6524 case TOK_LSTR:
6525 t = VT_INT;
6526 goto str_init;
6527 case TOK_STR:
6528 /* string parsing */
6529 t = VT_BYTE;
6530 str_init:
6531 type.t = t;
6532 mk_pointer(&type);
6533 type.t |= VT_ARRAY;
6534 memset(&ad, 0, sizeof(AttributeDef));
6535 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
6536 break;
6537 case '(':
6538 next();
6539 /* cast ? */
6540 if (parse_btype(&type, &ad)) {
6541 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
6542 skip(')');
6543 /* check ISOC99 compound literal */
6544 if (tok == '{') {
6545 /* data is allocated locally by default */
6546 if (global_expr)
6547 r = VT_CONST;
6548 else
6549 r = VT_LOCAL;
6550 /* all except arrays are lvalues */
6551 if (!(type.t & VT_ARRAY))
6552 r |= lvalue_type(type.t);
6553 memset(&ad, 0, sizeof(AttributeDef));
6554 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
6555 } else {
6556 unary();
6557 gen_cast(&type);
6559 } else if (tok == '{') {
6560 /* save all registers */
6561 save_regs(0);
6562 /* statement expression : we do not accept break/continue
6563 inside as GCC does */
6564 block(NULL, NULL, NULL, NULL, 0, 1);
6565 skip(')');
6566 } else {
6567 gexpr();
6568 skip(')');
6570 break;
6571 case '*':
6572 next();
6573 unary();
6574 indir();
6575 break;
6576 case '&':
6577 next();
6578 unary();
6579 /* functions names must be treated as function pointers,
6580 except for unary '&' and sizeof. Since we consider that
6581 functions are not lvalues, we only have to handle it
6582 there and in function calls. */
6583 /* arrays can also be used although they are not lvalues */
6584 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
6585 !(vtop->type.t & VT_ARRAY))
6586 test_lvalue();
6587 mk_pointer(&vtop->type);
6588 gaddrof();
6589 break;
6590 case '!':
6591 next();
6592 unary();
6593 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST)
6594 vtop->c.i = !vtop->c.i;
6595 else if ((vtop->r & VT_VALMASK) == VT_CMP)
6596 vtop->c.i = vtop->c.i ^ 1;
6597 else
6598 vseti(VT_JMP, gtst(1, 0));
6599 break;
6600 case '~':
6601 next();
6602 unary();
6603 vpushi(-1);
6604 gen_op('^');
6605 break;
6606 case '+':
6607 next();
6608 /* in order to force cast, we add zero */
6609 unary();
6610 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
6611 error("pointer not accepted for unary plus");
6612 vpushi(0);
6613 gen_op('+');
6614 break;
6615 case TOK_SIZEOF:
6616 case TOK_ALIGNOF1:
6617 case TOK_ALIGNOF2:
6618 t = tok;
6619 next();
6620 if (tok == '(') {
6621 parse_expr_type(&type);
6622 } else {
6623 unary_type(&type);
6625 size = type_size(&type, &align);
6626 if (t == TOK_SIZEOF)
6627 vpushi(size);
6628 else
6629 vpushi(align);
6630 break;
6632 case TOK_INC:
6633 case TOK_DEC:
6634 t = tok;
6635 next();
6636 unary();
6637 inc(0, t);
6638 break;
6639 case '-':
6640 next();
6641 vpushi(0);
6642 unary();
6643 gen_op('-');
6644 break;
6645 case TOK_LAND:
6646 if (!gnu_ext)
6647 goto tok_identifier;
6648 next();
6649 /* allow to take the address of a label */
6650 if (tok < TOK_UIDENT)
6651 expect("label identifier");
6652 s = label_find(tok);
6653 if (!s) {
6654 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
6655 } else {
6656 if (s->r == LABEL_DECLARED)
6657 s->r = LABEL_FORWARD;
6659 if (!s->type.t) {
6660 s->type.t = VT_VOID;
6661 mk_pointer(&s->type);
6662 s->type.t |= VT_STATIC;
6664 vset(&s->type, VT_CONST | VT_SYM, 0);
6665 vtop->sym = s;
6666 next();
6667 break;
6668 default:
6669 tok_identifier:
6670 t = tok;
6671 next();
6672 if (t < TOK_UIDENT)
6673 expect("identifier");
6674 s = sym_find(t);
6675 if (!s) {
6676 if (tok != '(')
6677 error("'%s' undeclared", get_tok_str(t, NULL));
6678 /* for simple function calls, we tolerate undeclared
6679 external reference to int() function */
6680 s = external_global_sym(t, &func_old_type, 0);
6682 vset(&s->type, s->r, s->c);
6683 /* if forward reference, we must point to s */
6684 if (vtop->r & VT_SYM) {
6685 vtop->sym = s;
6686 vtop->c.ul = 0;
6688 break;
6691 /* post operations */
6692 while (1) {
6693 if (tok == TOK_INC || tok == TOK_DEC) {
6694 inc(1, tok);
6695 next();
6696 } else if (tok == '.' || tok == TOK_ARROW) {
6697 /* field */
6698 if (tok == TOK_ARROW)
6699 indir();
6700 test_lvalue();
6701 gaddrof();
6702 next();
6703 /* expect pointer on structure */
6704 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
6705 expect("struct or union");
6706 s = vtop->type.ref;
6707 /* find field */
6708 tok |= SYM_FIELD;
6709 while ((s = s->next) != NULL) {
6710 if (s->v == tok)
6711 break;
6713 if (!s)
6714 error("field not found");
6715 /* add field offset to pointer */
6716 vtop->type = char_pointer_type; /* change type to 'char *' */
6717 vpushi(s->c);
6718 gen_op('+');
6719 /* change type to field type, and set to lvalue */
6720 vtop->type = s->type;
6721 /* an array is never an lvalue */
6722 if (!(vtop->type.t & VT_ARRAY)) {
6723 vtop->r |= lvalue_type(vtop->type.t);
6724 /* if bound checking, the referenced pointer must be checked */
6725 if (do_bounds_check)
6726 vtop->r |= VT_MUSTBOUND;
6728 next();
6729 } else if (tok == '[') {
6730 next();
6731 gexpr();
6732 gen_op('+');
6733 indir();
6734 skip(']');
6735 } else if (tok == '(') {
6736 SValue ret;
6737 Sym *sa;
6739 /* function call */
6740 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
6741 /* pointer test (no array accepted) */
6742 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
6743 vtop->type = *pointed_type(&vtop->type);
6744 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
6745 goto error_func;
6746 } else {
6747 error_func:
6748 expect("function pointer");
6750 } else {
6751 vtop->r &= ~VT_LVAL; /* no lvalue */
6753 /* get return type */
6754 s = vtop->type.ref;
6755 if (!nocode_wanted) {
6756 save_regs(0); /* save used temporary registers */
6757 gfunc_start(&gf, s->r);
6759 next();
6760 sa = s->next; /* first parameter */
6761 #ifdef INVERT_FUNC_PARAMS
6763 int parlevel;
6764 Sym *args, *s1;
6765 ParseState saved_parse_state;
6766 TokenString str;
6768 /* read each argument and store it on a stack */
6769 args = NULL;
6770 if (tok != ')') {
6771 for(;;) {
6772 tok_str_new(&str);
6773 parlevel = 0;
6774 while ((parlevel > 0 || (tok != ')' && tok != ',')) &&
6775 tok != TOK_EOF) {
6776 if (tok == '(')
6777 parlevel++;
6778 else if (tok == ')')
6779 parlevel--;
6780 tok_str_add_tok(&str);
6781 next();
6783 tok_str_add(&str, -1); /* end of file added */
6784 tok_str_add(&str, 0);
6785 s1 = sym_push2(&args, 0, 0, (int)str.str);
6786 s1->next = sa; /* add reference to argument */
6787 if (sa)
6788 sa = sa->next;
6789 if (tok == ')')
6790 break;
6791 skip(',');
6795 /* now generate code in reverse order by reading the stack */
6796 save_parse_state(&saved_parse_state);
6797 while (args) {
6798 macro_ptr = (int *)args->c;
6799 next();
6800 expr_eq();
6801 if (tok != -1)
6802 expect("',' or ')'");
6803 gfunc_param_typed(&gf, s, args->next);
6804 s1 = args->prev;
6805 tok_str_free((int *)args->c);
6806 tcc_free(args);
6807 args = s1;
6809 restore_parse_state(&saved_parse_state);
6811 #endif
6812 /* compute first implicit argument if a structure is returned */
6813 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
6814 /* get some space for the returned structure */
6815 size = type_size(&s->type, &align);
6816 loc = (loc - size) & -align;
6817 ret.type = s->type;
6818 ret.r = VT_LOCAL | VT_LVAL;
6819 /* pass it as 'int' to avoid structure arg passing
6820 problems */
6821 vseti(VT_LOCAL, loc);
6822 ret.c = vtop->c;
6823 if (!nocode_wanted)
6824 gfunc_param(&gf);
6825 else
6826 vtop--;
6827 } else {
6828 ret.type = s->type;
6829 ret.r2 = VT_CONST;
6830 /* return in register */
6831 if (is_float(ret.type.t)) {
6832 ret.r = REG_FRET;
6833 } else {
6834 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
6835 ret.r2 = REG_LRET;
6836 ret.r = REG_IRET;
6838 ret.c.i = 0;
6840 #ifndef INVERT_FUNC_PARAMS
6841 if (tok != ')') {
6842 for(;;) {
6843 expr_eq();
6844 gfunc_param_typed(&gf, s, sa);
6845 if (sa)
6846 sa = sa->next;
6847 if (tok == ')')
6848 break;
6849 skip(',');
6852 #endif
6853 if (sa)
6854 error("too few arguments to function");
6855 skip(')');
6856 if (!nocode_wanted)
6857 gfunc_call(&gf);
6858 else
6859 vtop--;
6860 /* return value */
6861 vsetc(&ret.type, ret.r, &ret.c);
6862 vtop->r2 = ret.r2;
6863 } else {
6864 break;
6869 static void uneq(void)
6871 int t;
6873 unary();
6874 if (tok == '=' ||
6875 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
6876 tok == TOK_A_XOR || tok == TOK_A_OR ||
6877 tok == TOK_A_SHL || tok == TOK_A_SAR) {
6878 test_lvalue();
6879 t = tok;
6880 next();
6881 if (t == '=') {
6882 expr_eq();
6883 } else {
6884 vdup();
6885 expr_eq();
6886 gen_op(t & 0x7f);
6888 vstore();
6892 static void expr_prod(void)
6894 int t;
6896 uneq();
6897 while (tok == '*' || tok == '/' || tok == '%') {
6898 t = tok;
6899 next();
6900 uneq();
6901 gen_op(t);
6905 static void expr_sum(void)
6907 int t;
6909 expr_prod();
6910 while (tok == '+' || tok == '-') {
6911 t = tok;
6912 next();
6913 expr_prod();
6914 gen_op(t);
6918 static void expr_shift(void)
6920 int t;
6922 expr_sum();
6923 while (tok == TOK_SHL || tok == TOK_SAR) {
6924 t = tok;
6925 next();
6926 expr_sum();
6927 gen_op(t);
6931 static void expr_cmp(void)
6933 int t;
6935 expr_shift();
6936 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
6937 tok == TOK_ULT || tok == TOK_UGE) {
6938 t = tok;
6939 next();
6940 expr_shift();
6941 gen_op(t);
6945 static void expr_cmpeq(void)
6947 int t;
6949 expr_cmp();
6950 while (tok == TOK_EQ || tok == TOK_NE) {
6951 t = tok;
6952 next();
6953 expr_cmp();
6954 gen_op(t);
6958 static void expr_and(void)
6960 expr_cmpeq();
6961 while (tok == '&') {
6962 next();
6963 expr_cmpeq();
6964 gen_op('&');
6968 static void expr_xor(void)
6970 expr_and();
6971 while (tok == '^') {
6972 next();
6973 expr_and();
6974 gen_op('^');
6978 static void expr_or(void)
6980 expr_xor();
6981 while (tok == '|') {
6982 next();
6983 expr_xor();
6984 gen_op('|');
6988 /* XXX: fix this mess */
6989 static void expr_land_const(void)
6991 expr_or();
6992 while (tok == TOK_LAND) {
6993 next();
6994 expr_or();
6995 gen_op(TOK_LAND);
6999 /* XXX: fix this mess */
7000 static void expr_lor_const(void)
7002 expr_land_const();
7003 while (tok == TOK_LOR) {
7004 next();
7005 expr_land_const();
7006 gen_op(TOK_LOR);
7010 /* only used if non constant */
7011 static void expr_land(void)
7013 int t;
7015 expr_or();
7016 if (tok == TOK_LAND) {
7017 t = 0;
7018 for(;;) {
7019 t = gtst(1, t);
7020 if (tok != TOK_LAND) {
7021 vseti(VT_JMPI, t);
7022 break;
7024 next();
7025 expr_or();
7030 static void expr_lor(void)
7032 int t;
7034 expr_land();
7035 if (tok == TOK_LOR) {
7036 t = 0;
7037 for(;;) {
7038 t = gtst(0, t);
7039 if (tok != TOK_LOR) {
7040 vseti(VT_JMP, t);
7041 break;
7043 next();
7044 expr_land();
7049 /* XXX: better constant handling */
7050 static void expr_eq(void)
7052 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
7053 SValue sv;
7054 CType type, type1, type2;
7056 if (const_wanted) {
7057 int c1, c;
7058 expr_lor_const();
7059 if (tok == '?') {
7060 c = vtop->c.i;
7061 vpop();
7062 next();
7063 if (tok == ':' && gnu_ext) {
7064 c1 = c;
7065 } else {
7066 gexpr();
7067 c1 = vtop->c.i;
7068 vpop();
7070 skip(':');
7071 expr_eq();
7072 if (c)
7073 vtop->c.i = c1;
7075 } else {
7076 expr_lor();
7077 if (tok == '?') {
7078 next();
7079 if (vtop != vstack) {
7080 /* needed to avoid having different registers saved in
7081 each branch */
7082 if (is_float(vtop->type.t))
7083 rc = RC_FLOAT;
7084 else
7085 rc = RC_INT;
7086 gv(rc);
7087 save_regs(1);
7089 if (tok == ':' && gnu_ext) {
7090 gv_dup();
7091 tt = gtst(1, 0);
7092 } else {
7093 tt = gtst(1, 0);
7094 gexpr();
7096 type1 = vtop->type;
7097 sv = *vtop; /* save value to handle it later */
7098 vtop--; /* no vpop so that FP stack is not flushed */
7099 skip(':');
7100 u = gjmp(0);
7101 gsym(tt);
7102 expr_eq();
7103 type2 = vtop->type;
7105 t1 = type1.t;
7106 bt1 = t1 & VT_BTYPE;
7107 t2 = type2.t;
7108 bt2 = t2 & VT_BTYPE;
7109 /* cast operands to correct type according to ISOC rules */
7110 if (is_float(bt1) || is_float(bt2)) {
7111 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
7112 type.t = VT_LDOUBLE;
7113 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
7114 type.t = VT_DOUBLE;
7115 } else {
7116 type.t = VT_FLOAT;
7118 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
7119 /* cast to biggest op */
7120 type.t = VT_LLONG;
7121 /* convert to unsigned if it does not fit in a long long */
7122 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
7123 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
7124 type.t |= VT_UNSIGNED;
7125 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
7126 /* XXX: test pointer compatibility */
7127 type = type1;
7128 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
7129 /* XXX: test structure compatibility */
7130 type = type1;
7131 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
7132 /* NOTE: as an extension, we accept void on only one side */
7133 type.t = VT_VOID;
7134 } else {
7135 /* integer operations */
7136 type.t = VT_INT;
7137 /* convert to unsigned if it does not fit in an integer */
7138 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
7139 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
7140 type.t |= VT_UNSIGNED;
7143 /* now we convert second operand */
7144 gen_cast(&type);
7145 rc = RC_INT;
7146 if (is_float(type.t)) {
7147 rc = RC_FLOAT;
7148 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
7149 /* for long longs, we use fixed registers to avoid having
7150 to handle a complicated move */
7151 rc = RC_IRET;
7154 r2 = gv(rc);
7155 /* this is horrible, but we must also convert first
7156 operand */
7157 tt = gjmp(0);
7158 gsym(u);
7159 /* put again first value and cast it */
7160 *vtop = sv;
7161 gen_cast(&type);
7162 r1 = gv(rc);
7163 move_reg(r2, r1);
7164 vtop->r = r2;
7165 gsym(tt);
7170 static void gexpr(void)
7172 while (1) {
7173 expr_eq();
7174 if (tok != ',')
7175 break;
7176 vpop();
7177 next();
7181 /* parse an expression and return its type without any side effect. */
7182 static void expr_type(CType *type)
7184 int a;
7186 a = nocode_wanted;
7187 nocode_wanted = 1;
7188 gexpr();
7189 *type = vtop->type;
7190 vpop();
7191 nocode_wanted = a;
7194 /* parse a unary expression and return its type without any side
7195 effect. */
7196 static void unary_type(CType *type)
7198 int a;
7200 a = nocode_wanted;
7201 nocode_wanted = 1;
7202 unary();
7203 *type = vtop->type;
7204 vpop();
7205 nocode_wanted = a;
7208 /* parse a constant expression and return value in vtop. */
7209 static void expr_const1(void)
7211 int a;
7212 a = const_wanted;
7213 const_wanted = 1;
7214 expr_eq();
7215 const_wanted = a;
7218 /* parse an integer constant and return its value. */
7219 static int expr_const(void)
7221 int c;
7222 expr_const1();
7223 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
7224 expect("constant expression");
7225 c = vtop->c.i;
7226 vpop();
7227 return c;
7230 /* return the label token if current token is a label, otherwise
7231 return zero */
7232 static int is_label(void)
7234 int last_tok;
7236 /* fast test first */
7237 if (tok < TOK_UIDENT)
7238 return 0;
7239 /* no need to save tokc because tok is an identifier */
7240 last_tok = tok;
7241 next();
7242 if (tok == ':') {
7243 next();
7244 return last_tok;
7245 } else {
7246 unget_tok(last_tok);
7247 return 0;
7251 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
7252 int case_reg, int is_expr)
7254 int a, b, c, d;
7255 Sym *s;
7257 /* generate line number info */
7258 if (do_debug &&
7259 (last_line_num != file->line_num || last_ind != ind)) {
7260 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
7261 last_ind = ind;
7262 last_line_num = file->line_num;
7265 if (is_expr) {
7266 /* default return value is (void) */
7267 vpushi(0);
7268 vtop->type.t = VT_VOID;
7271 if (tok == TOK_IF) {
7272 /* if test */
7273 next();
7274 skip('(');
7275 gexpr();
7276 skip(')');
7277 a = gtst(1, 0);
7278 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7279 c = tok;
7280 if (c == TOK_ELSE) {
7281 next();
7282 d = gjmp(0);
7283 gsym(a);
7284 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7285 gsym(d); /* patch else jmp */
7286 } else
7287 gsym(a);
7288 } else if (tok == TOK_WHILE) {
7289 next();
7290 d = ind;
7291 skip('(');
7292 gexpr();
7293 skip(')');
7294 a = gtst(1, 0);
7295 b = 0;
7296 block(&a, &b, case_sym, def_sym, case_reg, 0);
7297 gjmp_addr(d);
7298 gsym(a);
7299 gsym_addr(b, d);
7300 } else if (tok == '{') {
7301 Sym *llabel;
7303 next();
7304 /* record local declaration stack position */
7305 s = local_stack;
7306 llabel = local_label_stack;
7307 /* handle local labels declarations */
7308 if (tok == TOK_LABEL) {
7309 next();
7310 for(;;) {
7311 if (tok < TOK_UIDENT)
7312 expect("label identifier");
7313 label_push(&local_label_stack, tok, LABEL_DECLARED);
7314 next();
7315 if (tok == ',') {
7316 next();
7317 } else {
7318 skip(';');
7319 break;
7323 while (tok != '}') {
7324 decl(VT_LOCAL);
7325 if (tok != '}') {
7326 if (is_expr)
7327 vpop();
7328 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7331 /* pop locally defined labels */
7332 label_pop(&local_label_stack, llabel);
7333 /* pop locally defined symbols */
7334 sym_pop(&local_stack, s);
7335 next();
7336 } else if (tok == TOK_RETURN) {
7337 next();
7338 if (tok != ';') {
7339 gexpr();
7340 gen_assign_cast(&func_vt);
7341 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
7342 CType type;
7343 /* if returning structure, must copy it to implicit
7344 first pointer arg location */
7345 type = func_vt;
7346 mk_pointer(&type);
7347 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
7348 indir();
7349 vswap();
7350 /* copy structure value to pointer */
7351 vstore();
7352 } else if (is_float(func_vt.t)) {
7353 gv(RC_FRET);
7354 } else {
7355 gv(RC_IRET);
7357 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
7359 skip(';');
7360 rsym = gjmp(rsym); /* jmp */
7361 } else if (tok == TOK_BREAK) {
7362 /* compute jump */
7363 if (!bsym)
7364 error("cannot break");
7365 *bsym = gjmp(*bsym);
7366 next();
7367 skip(';');
7368 } else if (tok == TOK_CONTINUE) {
7369 /* compute jump */
7370 if (!csym)
7371 error("cannot continue");
7372 *csym = gjmp(*csym);
7373 next();
7374 skip(';');
7375 } else if (tok == TOK_FOR) {
7376 int e;
7377 next();
7378 skip('(');
7379 if (tok != ';') {
7380 gexpr();
7381 vpop();
7383 skip(';');
7384 d = ind;
7385 c = ind;
7386 a = 0;
7387 b = 0;
7388 if (tok != ';') {
7389 gexpr();
7390 a = gtst(1, 0);
7392 skip(';');
7393 if (tok != ')') {
7394 e = gjmp(0);
7395 c = ind;
7396 gexpr();
7397 vpop();
7398 gjmp_addr(d);
7399 gsym(e);
7401 skip(')');
7402 block(&a, &b, case_sym, def_sym, case_reg, 0);
7403 gjmp_addr(c);
7404 gsym(a);
7405 gsym_addr(b, c);
7406 } else
7407 if (tok == TOK_DO) {
7408 next();
7409 a = 0;
7410 b = 0;
7411 d = ind;
7412 block(&a, &b, case_sym, def_sym, case_reg, 0);
7413 skip(TOK_WHILE);
7414 skip('(');
7415 gsym(b);
7416 gexpr();
7417 c = gtst(0, 0);
7418 gsym_addr(c, d);
7419 skip(')');
7420 gsym(a);
7421 skip(';');
7422 } else
7423 if (tok == TOK_SWITCH) {
7424 next();
7425 skip('(');
7426 gexpr();
7427 /* XXX: other types than integer */
7428 case_reg = gv(RC_INT);
7429 vpop();
7430 skip(')');
7431 a = 0;
7432 b = gjmp(0); /* jump to first case */
7433 c = 0;
7434 block(&a, csym, &b, &c, case_reg, 0);
7435 /* if no default, jmp after switch */
7436 if (c == 0)
7437 c = ind;
7438 /* default label */
7439 gsym_addr(b, c);
7440 /* break label */
7441 gsym(a);
7442 } else
7443 if (tok == TOK_CASE) {
7444 int v1, v2;
7445 if (!case_sym)
7446 expect("switch");
7447 next();
7448 v1 = expr_const();
7449 v2 = v1;
7450 if (gnu_ext && tok == TOK_DOTS) {
7451 next();
7452 v2 = expr_const();
7453 if (v2 < v1)
7454 warning("empty case range");
7456 /* since a case is like a label, we must skip it with a jmp */
7457 b = gjmp(0);
7458 gsym(*case_sym);
7459 vseti(case_reg, 0);
7460 vpushi(v1);
7461 if (v1 == v2) {
7462 gen_op(TOK_EQ);
7463 *case_sym = gtst(1, 0);
7464 } else {
7465 gen_op(TOK_GE);
7466 *case_sym = gtst(1, 0);
7467 vseti(case_reg, 0);
7468 vpushi(v2);
7469 gen_op(TOK_LE);
7470 *case_sym = gtst(1, *case_sym);
7472 gsym(b);
7473 skip(':');
7474 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7475 } else
7476 if (tok == TOK_DEFAULT) {
7477 next();
7478 skip(':');
7479 if (!def_sym)
7480 expect("switch");
7481 if (*def_sym)
7482 error("too many 'default'");
7483 *def_sym = ind;
7484 block(bsym, csym, case_sym, def_sym, case_reg, 0);
7485 } else
7486 if (tok == TOK_GOTO) {
7487 next();
7488 if (tok == '*' && gnu_ext) {
7489 /* computed goto */
7490 next();
7491 gexpr();
7492 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
7493 expect("pointer");
7494 ggoto();
7495 } else if (tok >= TOK_UIDENT) {
7496 s = label_find(tok);
7497 /* put forward definition if needed */
7498 if (!s) {
7499 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
7500 } else {
7501 if (s->r == LABEL_DECLARED)
7502 s->r = LABEL_FORWARD;
7504 /* label already defined */
7505 if (s->r & LABEL_FORWARD)
7506 s->next = (void *)gjmp((long)s->next);
7507 else
7508 gjmp_addr((long)s->next);
7509 next();
7510 } else {
7511 expect("label identifier");
7513 skip(';');
7514 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
7515 asm_instr();
7516 } else {
7517 b = is_label();
7518 if (b) {
7519 /* label case */
7520 s = label_find(b);
7521 if (s) {
7522 if (s->r == LABEL_DEFINED)
7523 error("duplicate label '%s'", get_tok_str(s->v, NULL));
7524 gsym((long)s->next);
7525 s->r = LABEL_DEFINED;
7526 } else {
7527 s = label_push(&global_label_stack, b, LABEL_DEFINED);
7529 s->next = (void *)ind;
7530 /* we accept this, but it is a mistake */
7531 if (tok == '}') {
7532 warning("deprecated use of label at end of compound statement");
7533 } else {
7534 if (is_expr)
7535 vpop();
7536 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
7538 } else {
7539 /* expression case */
7540 if (tok != ';') {
7541 if (is_expr) {
7542 vpop();
7543 gexpr();
7544 } else {
7545 gexpr();
7546 vpop();
7549 skip(';');
7554 /* t is the array or struct type. c is the array or struct
7555 address. cur_index/cur_field is the pointer to the current
7556 value. 'size_only' is true if only size info is needed (only used
7557 in arrays) */
7558 static void decl_designator(CType *type, Section *sec, unsigned long c,
7559 int *cur_index, Sym **cur_field,
7560 int size_only)
7562 Sym *s, *f;
7563 int notfirst, index, index_last, align, l, nb_elems, elem_size;
7564 CType type1;
7566 notfirst = 0;
7567 elem_size = 0;
7568 nb_elems = 1;
7569 if (gnu_ext && (l = is_label()) != 0)
7570 goto struct_field;
7571 while (tok == '[' || tok == '.') {
7572 if (tok == '[') {
7573 if (!(type->t & VT_ARRAY))
7574 expect("array type");
7575 s = type->ref;
7576 next();
7577 index = expr_const();
7578 if (index < 0 || (s->c >= 0 && index >= s->c))
7579 expect("invalid index");
7580 if (tok == TOK_DOTS && gnu_ext) {
7581 next();
7582 index_last = expr_const();
7583 if (index_last < 0 ||
7584 (s->c >= 0 && index_last >= s->c) ||
7585 index_last < index)
7586 expect("invalid index");
7587 } else {
7588 index_last = index;
7590 skip(']');
7591 if (!notfirst)
7592 *cur_index = index_last;
7593 type = pointed_type(type);
7594 elem_size = type_size(type, &align);
7595 c += index * elem_size;
7596 /* NOTE: we only support ranges for last designator */
7597 nb_elems = index_last - index + 1;
7598 if (nb_elems != 1) {
7599 notfirst = 1;
7600 break;
7602 } else {
7603 next();
7604 l = tok;
7605 next();
7606 struct_field:
7607 if ((type->t & VT_BTYPE) != VT_STRUCT)
7608 expect("struct/union type");
7609 s = type->ref;
7610 l |= SYM_FIELD;
7611 f = s->next;
7612 while (f) {
7613 if (f->v == l)
7614 break;
7615 f = f->next;
7617 if (!f)
7618 expect("field");
7619 if (!notfirst)
7620 *cur_field = f;
7621 /* XXX: fix this mess by using explicit storage field */
7622 type1 = f->type;
7623 type1.t |= (type->t & ~VT_TYPE);
7624 type = &type1;
7625 c += f->c;
7627 notfirst = 1;
7629 if (notfirst) {
7630 if (tok == '=') {
7631 next();
7632 } else {
7633 if (!gnu_ext)
7634 expect("=");
7636 } else {
7637 if (type->t & VT_ARRAY) {
7638 index = *cur_index;
7639 type = pointed_type(type);
7640 c += index * type_size(type, &align);
7641 } else {
7642 f = *cur_field;
7643 if (!f)
7644 error("too many field init");
7645 /* XXX: fix this mess by using explicit storage field */
7646 type1 = f->type;
7647 type1.t |= (type->t & ~VT_TYPE);
7648 type = &type1;
7649 c += f->c;
7652 decl_initializer(type, sec, c, 0, size_only);
7654 /* XXX: make it more general */
7655 if (!size_only && nb_elems > 1) {
7656 unsigned long c_end;
7657 uint8_t *src, *dst;
7658 int i;
7660 if (!sec)
7661 error("range init not supported yet for dynamic storage");
7662 c_end = c + nb_elems * elem_size;
7663 if (c_end > sec->data_allocated)
7664 section_realloc(sec, c_end);
7665 src = sec->data + c;
7666 dst = src;
7667 for(i = 1; i < nb_elems; i++) {
7668 dst += elem_size;
7669 memcpy(dst, src, elem_size);
7674 #define EXPR_VAL 0
7675 #define EXPR_CONST 1
7676 #define EXPR_ANY 2
7678 /* store a value or an expression directly in global data or in local array */
7679 static void init_putv(CType *type, Section *sec, unsigned long c,
7680 int v, int expr_type)
7682 int saved_global_expr, bt, bit_pos, bit_size;
7683 void *ptr;
7684 unsigned long long bit_mask;
7686 switch(expr_type) {
7687 case EXPR_VAL:
7688 vpushi(v);
7689 break;
7690 case EXPR_CONST:
7691 /* compound literals must be allocated globally in this case */
7692 saved_global_expr = global_expr;
7693 global_expr = 1;
7694 expr_const1();
7695 global_expr = saved_global_expr;
7696 /* NOTE: symbols are accepted */
7697 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
7698 error("initializer element is not constant");
7699 break;
7700 case EXPR_ANY:
7701 expr_eq();
7702 break;
7705 if (sec) {
7706 /* XXX: not portable */
7707 /* XXX: generate error if incorrect relocation */
7708 gen_assign_cast(type);
7709 bt = type->t & VT_BTYPE;
7710 ptr = sec->data + c;
7711 /* XXX: make code faster ? */
7712 if (!(type->t & VT_BITFIELD)) {
7713 bit_pos = 0;
7714 bit_size = 32;
7715 bit_mask = -1LL;
7716 } else {
7717 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
7718 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
7719 bit_mask = (1LL << bit_size) - 1;
7721 if ((vtop->r & VT_SYM) &&
7722 (bt == VT_BYTE ||
7723 bt == VT_SHORT ||
7724 bt == VT_DOUBLE ||
7725 bt == VT_LDOUBLE ||
7726 bt == VT_LLONG ||
7727 (bt == VT_INT && bit_size != 32)))
7728 error("initializer element is not computable at load time");
7729 switch(bt) {
7730 case VT_BYTE:
7731 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7732 break;
7733 case VT_SHORT:
7734 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7735 break;
7736 case VT_DOUBLE:
7737 *(double *)ptr = vtop->c.d;
7738 break;
7739 case VT_LDOUBLE:
7740 *(long double *)ptr = vtop->c.ld;
7741 break;
7742 case VT_LLONG:
7743 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
7744 break;
7745 default:
7746 if (vtop->r & VT_SYM) {
7747 greloc(sec, vtop->sym, c, R_DATA_32);
7749 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
7750 break;
7752 vtop--;
7753 } else {
7754 vset(type, VT_LOCAL, c);
7755 vswap();
7756 vstore();
7757 vpop();
7761 /* put zeros for variable based init */
7762 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
7764 GFuncContext gf;
7766 if (sec) {
7767 /* nothing to do because globals are already set to zero */
7768 } else {
7769 gfunc_start(&gf, FUNC_CDECL);
7770 vpushi(size);
7771 gfunc_param(&gf);
7772 vpushi(0);
7773 gfunc_param(&gf);
7774 vseti(VT_LOCAL, c);
7775 gfunc_param(&gf);
7776 vpush_global_sym(&func_old_type, TOK_memset);
7777 gfunc_call(&gf);
7781 /* 't' contains the type and storage info. 'c' is the offset of the
7782 object in section 'sec'. If 'sec' is NULL, it means stack based
7783 allocation. 'first' is true if array '{' must be read (multi
7784 dimension implicit array init handling). 'size_only' is true if
7785 size only evaluation is wanted (only for arrays). */
7786 static void decl_initializer(CType *type, Section *sec, unsigned long c,
7787 int first, int size_only)
7789 int index, array_length, n, no_oblock, nb, parlevel, i;
7790 int size1, align1, expr_type;
7791 Sym *s, *f;
7792 CType *t1;
7794 if (type->t & VT_ARRAY) {
7795 s = type->ref;
7796 n = s->c;
7797 array_length = 0;
7798 t1 = pointed_type(type);
7799 size1 = type_size(t1, &align1);
7801 no_oblock = 1;
7802 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
7803 tok == '{') {
7804 skip('{');
7805 no_oblock = 0;
7808 /* only parse strings here if correct type (otherwise: handle
7809 them as ((w)char *) expressions */
7810 if ((tok == TOK_LSTR &&
7811 (t1->t & VT_BTYPE) == VT_INT) ||
7812 (tok == TOK_STR &&
7813 (t1->t & VT_BTYPE) == VT_BYTE)) {
7814 while (tok == TOK_STR || tok == TOK_LSTR) {
7815 int cstr_len, ch;
7816 CString *cstr;
7818 cstr = tokc.cstr;
7819 /* compute maximum number of chars wanted */
7820 if (tok == TOK_STR)
7821 cstr_len = cstr->size;
7822 else
7823 cstr_len = cstr->size / sizeof(int);
7824 cstr_len--;
7825 nb = cstr_len;
7826 if (n >= 0 && nb > (n - array_length))
7827 nb = n - array_length;
7828 if (!size_only) {
7829 if (cstr_len > nb)
7830 warning("initializer-string for array is too long");
7831 /* in order to go faster for common case (char
7832 string in global variable, we handle it
7833 specifically */
7834 if (sec && tok == TOK_STR && size1 == 1) {
7835 memcpy(sec->data + c + array_length, cstr->data, nb);
7836 } else {
7837 for(i=0;i<nb;i++) {
7838 if (tok == TOK_STR)
7839 ch = ((unsigned char *)cstr->data)[i];
7840 else
7841 ch = ((int *)cstr->data)[i];
7842 init_putv(t1, sec, c + (array_length + i) * size1,
7843 ch, EXPR_VAL);
7847 array_length += nb;
7848 next();
7850 /* only add trailing zero if enough storage (no
7851 warning in this case since it is standard) */
7852 if (n < 0 || array_length < n) {
7853 if (!size_only) {
7854 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
7856 array_length++;
7858 } else {
7859 index = 0;
7860 while (tok != '}') {
7861 decl_designator(type, sec, c, &index, NULL, size_only);
7862 if (n >= 0 && index >= n)
7863 error("index too large");
7864 /* must put zero in holes (note that doing it that way
7865 ensures that it even works with designators) */
7866 if (!size_only && array_length < index) {
7867 init_putz(t1, sec, c + array_length * size1,
7868 (index - array_length) * size1);
7870 index++;
7871 if (index > array_length)
7872 array_length = index;
7873 /* special test for multi dimensional arrays (may not
7874 be strictly correct if designators are used at the
7875 same time) */
7876 if (index >= n && no_oblock)
7877 break;
7878 if (tok == '}')
7879 break;
7880 skip(',');
7883 if (!no_oblock)
7884 skip('}');
7885 /* put zeros at the end */
7886 if (!size_only && n >= 0 && array_length < n) {
7887 init_putz(t1, sec, c + array_length * size1,
7888 (n - array_length) * size1);
7890 /* patch type size if needed */
7891 if (n < 0)
7892 s->c = array_length;
7893 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
7894 (sec || !first || tok == '{')) {
7895 int par_count;
7897 /* NOTE: the previous test is a specific case for automatic
7898 struct/union init */
7899 /* XXX: union needs only one init */
7901 /* XXX: this test is incorrect for local initializers
7902 beginning with ( without {. It would be much more difficult
7903 to do it correctly (ideally, the expression parser should
7904 be used in all cases) */
7905 par_count = 0;
7906 if (tok == '(') {
7907 AttributeDef ad1;
7908 CType type1;
7909 next();
7910 while (tok == '(') {
7911 par_count++;
7912 next();
7914 if (!parse_btype(&type1, &ad1))
7915 expect("cast");
7916 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
7917 if (!is_compatible_types(type, &type1))
7918 error("invalid type for cast");
7919 skip(')');
7921 no_oblock = 1;
7922 if (first || tok == '{') {
7923 skip('{');
7924 no_oblock = 0;
7926 s = type->ref;
7927 f = s->next;
7928 array_length = 0;
7929 index = 0;
7930 n = s->c;
7931 while (tok != '}') {
7932 decl_designator(type, sec, c, NULL, &f, size_only);
7933 index = f->c;
7934 if (!size_only && array_length < index) {
7935 init_putz(type, sec, c + array_length,
7936 index - array_length);
7938 index = index + type_size(&f->type, &align1);
7939 if (index > array_length)
7940 array_length = index;
7941 f = f->next;
7942 if (no_oblock && f == NULL)
7943 break;
7944 if (tok == '}')
7945 break;
7946 skip(',');
7948 /* put zeros at the end */
7949 if (!size_only && array_length < n) {
7950 init_putz(type, sec, c + array_length,
7951 n - array_length);
7953 if (!no_oblock)
7954 skip('}');
7955 while (par_count) {
7956 skip(')');
7957 par_count--;
7959 } else if (tok == '{') {
7960 next();
7961 decl_initializer(type, sec, c, first, size_only);
7962 skip('}');
7963 } else if (size_only) {
7964 /* just skip expression */
7965 parlevel = 0;
7966 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
7967 tok != -1) {
7968 if (tok == '(')
7969 parlevel++;
7970 else if (tok == ')')
7971 parlevel--;
7972 next();
7974 } else {
7975 /* currently, we always use constant expression for globals
7976 (may change for scripting case) */
7977 expr_type = EXPR_CONST;
7978 if (!sec)
7979 expr_type = EXPR_ANY;
7980 init_putv(type, sec, c, 0, expr_type);
7984 /* parse an initializer for type 't' if 'has_init' is non zero, and
7985 allocate space in local or global data space ('r' is either
7986 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
7987 variable 'v' of scope 'scope' is declared before initializers are
7988 parsed. If 'v' is zero, then a reference to the new object is put
7989 in the value stack. If 'has_init' is 2, a special parsing is done
7990 to handle string constants. */
7991 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
7992 int has_init, int v, int scope)
7994 int size, align, addr, data_offset;
7995 int level;
7996 ParseState saved_parse_state;
7997 TokenString init_str;
7998 Section *sec;
8000 size = type_size(type, &align);
8001 /* If unknown size, we must evaluate it before
8002 evaluating initializers because
8003 initializers can generate global data too
8004 (e.g. string pointers or ISOC99 compound
8005 literals). It also simplifies local
8006 initializers handling */
8007 tok_str_new(&init_str);
8008 if (size < 0) {
8009 if (!has_init)
8010 error("unknown type size");
8011 /* get all init string */
8012 if (has_init == 2) {
8013 /* only get strings */
8014 while (tok == TOK_STR || tok == TOK_LSTR) {
8015 tok_str_add_tok(&init_str);
8016 next();
8018 } else {
8019 level = 0;
8020 while (level > 0 || (tok != ',' && tok != ';')) {
8021 if (tok < 0)
8022 error("unexpected end of file in initializer");
8023 tok_str_add_tok(&init_str);
8024 if (tok == '{')
8025 level++;
8026 else if (tok == '}') {
8027 if (level == 0)
8028 break;
8029 level--;
8031 next();
8034 tok_str_add(&init_str, -1);
8035 tok_str_add(&init_str, 0);
8037 /* compute size */
8038 save_parse_state(&saved_parse_state);
8040 macro_ptr = init_str.str;
8041 next();
8042 decl_initializer(type, NULL, 0, 1, 1);
8043 /* prepare second initializer parsing */
8044 macro_ptr = init_str.str;
8045 next();
8047 /* if still unknown size, error */
8048 size = type_size(type, &align);
8049 if (size < 0)
8050 error("unknown type size");
8052 /* take into account specified alignment if bigger */
8053 if (ad->aligned > align)
8054 align = ad->aligned;
8055 if ((r & VT_VALMASK) == VT_LOCAL) {
8056 sec = NULL;
8057 if (do_bounds_check && (type->t & VT_ARRAY))
8058 loc--;
8059 loc = (loc - size) & -align;
8060 addr = loc;
8061 /* handles bounds */
8062 /* XXX: currently, since we do only one pass, we cannot track
8063 '&' operators, so we add only arrays */
8064 if (do_bounds_check && (type->t & VT_ARRAY)) {
8065 unsigned long *bounds_ptr;
8066 /* add padding between regions */
8067 loc--;
8068 /* then add local bound info */
8069 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
8070 bounds_ptr[0] = addr;
8071 bounds_ptr[1] = size;
8073 if (v) {
8074 /* local variable */
8075 sym_push(v, type, r, addr);
8076 } else {
8077 /* push local reference */
8078 vset(type, r, addr);
8080 } else {
8081 Sym *sym;
8083 sym = NULL;
8084 if (v && scope == VT_CONST) {
8085 /* see if the symbol was already defined */
8086 sym = sym_find(v);
8087 if (sym) {
8088 if (!is_compatible_types(&sym->type, type))
8089 error("incompatible types for redefinition of '%s'",
8090 get_tok_str(v, NULL));
8091 if (sym->type.t & VT_EXTERN) {
8092 /* if the variable is extern, it was not allocated */
8093 sym->type.t &= ~VT_EXTERN;
8094 } else {
8095 /* we accept several definitions of the same
8096 global variable. this is tricky, because we
8097 must play with the SHN_COMMON type of the symbol */
8098 /* XXX: should check if the variable was already
8099 initialized. It is incorrect to initialized it
8100 twice */
8101 /* no init data, we won't add more to the symbol */
8102 if (!has_init)
8103 goto no_alloc;
8108 /* allocate symbol in corresponding section */
8109 sec = ad->section;
8110 if (!sec) {
8111 if (has_init)
8112 sec = data_section;
8114 if (sec) {
8115 data_offset = sec->data_offset;
8116 data_offset = (data_offset + align - 1) & -align;
8117 addr = data_offset;
8118 /* very important to increment global pointer at this time
8119 because initializers themselves can create new initializers */
8120 data_offset += size;
8121 /* add padding if bound check */
8122 if (do_bounds_check)
8123 data_offset++;
8124 sec->data_offset = data_offset;
8125 /* allocate section space to put the data */
8126 if (sec->sh_type != SHT_NOBITS &&
8127 data_offset > sec->data_allocated)
8128 section_realloc(sec, data_offset);
8129 } else {
8130 addr = 0; /* avoid warning */
8133 if (v) {
8134 if (scope == VT_CONST) {
8135 if (!sym)
8136 goto do_def;
8137 } else {
8138 do_def:
8139 sym = sym_push(v, type, r | VT_SYM, 0);
8141 /* update symbol definition */
8142 if (sec) {
8143 put_extern_sym(sym, sec, addr, size);
8144 } else {
8145 Elf32_Sym *esym;
8146 /* put a common area */
8147 put_extern_sym(sym, NULL, align, size);
8148 /* XXX: find a nicer way */
8149 esym = &((Elf32_Sym *)symtab_section->data)[sym->c];
8150 esym->st_shndx = SHN_COMMON;
8152 } else {
8153 CValue cval;
8155 /* push global reference */
8156 sym = get_sym_ref(type, sec, addr, size);
8157 cval.ul = 0;
8158 vsetc(type, VT_CONST | VT_SYM, &cval);
8159 vtop->sym = sym;
8162 /* handles bounds now because the symbol must be defined
8163 before for the relocation */
8164 if (do_bounds_check) {
8165 unsigned long *bounds_ptr;
8167 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
8168 /* then add global bound info */
8169 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
8170 bounds_ptr[0] = 0; /* relocated */
8171 bounds_ptr[1] = size;
8174 if (has_init) {
8175 decl_initializer(type, sec, addr, 1, 0);
8176 /* restore parse state if needed */
8177 if (init_str.str) {
8178 tok_str_free(init_str.str);
8179 restore_parse_state(&saved_parse_state);
8182 no_alloc: ;
8185 void put_func_debug(Sym *sym)
8187 char buf[512];
8189 /* stabs info */
8190 /* XXX: we put here a dummy type */
8191 snprintf(buf, sizeof(buf), "%s:%c1",
8192 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
8193 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
8194 cur_text_section, sym->c);
8195 last_ind = 0;
8196 last_line_num = 0;
8199 /* not finished : try to put some local vars in registers */
8200 //#define CONFIG_REG_VARS
8202 #ifdef CONFIG_REG_VARS
8203 void add_var_ref(int t)
8205 printf("%s:%d: &%s\n",
8206 file->filename, file->line_num,
8207 get_tok_str(t, NULL));
8210 /* first pass on a function with heuristic to extract variable usage
8211 and pointer references to local variables for register allocation */
8212 void analyse_function(void)
8214 int level, t;
8216 for(;;) {
8217 if (tok == -1)
8218 break;
8219 /* any symbol coming after '&' is considered as being a
8220 variable whose reference is taken. It is highly unaccurate
8221 but it is difficult to do better without a complete parse */
8222 if (tok == '&') {
8223 next();
8224 /* if '& number', then no need to examine next tokens */
8225 if (tok == TOK_CINT ||
8226 tok == TOK_CUINT ||
8227 tok == TOK_CLLONG ||
8228 tok == TOK_CULLONG) {
8229 continue;
8230 } else if (tok >= TOK_UIDENT) {
8231 /* if '& ident [' or '& ident ->', then ident address
8232 is not needed */
8233 t = tok;
8234 next();
8235 if (tok != '[' && tok != TOK_ARROW)
8236 add_var_ref(t);
8237 } else {
8238 level = 0;
8239 while (tok != '}' && tok != ';' &&
8240 !((tok == ',' || tok == ')') && level == 0)) {
8241 if (tok >= TOK_UIDENT) {
8242 add_var_ref(tok);
8243 } else if (tok == '(') {
8244 level++;
8245 } else if (tok == ')') {
8246 level--;
8248 next();
8251 } else {
8252 next();
8256 #endif
8258 /* parse an old style function declaration list */
8259 /* XXX: check multiple parameter */
8260 static void func_decl_list(Sym *func_sym)
8262 AttributeDef ad;
8263 int v;
8264 Sym *s;
8265 CType btype, type;
8267 /* parse each declaration */
8268 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
8269 if (!parse_btype(&btype, &ad))
8270 expect("declaration list");
8271 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8272 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8273 tok == ';') {
8274 /* we accept no variable after */
8275 } else {
8276 for(;;) {
8277 type = btype;
8278 type_decl(&type, &ad, &v, TYPE_DIRECT);
8279 /* find parameter in function parameter list */
8280 s = func_sym->next;
8281 while (s != NULL) {
8282 if ((s->v & ~SYM_FIELD) == v)
8283 goto found;
8284 s = s->next;
8286 error("declaration for parameter '%s' but no such parameter",
8287 get_tok_str(v, NULL));
8288 found:
8289 /* check that no storage specifier except 'register' was given */
8290 if (type.t & VT_STORAGE)
8291 error("storage class specified for '%s'", get_tok_str(v, NULL));
8292 convert_parameter_type(&type);
8293 /* we can add the type (NOTE: it could be local to the function) */
8294 s->type = type;
8295 /* accept other parameters */
8296 if (tok == ',')
8297 next();
8298 else
8299 break;
8302 skip(';');
8306 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
8307 static void decl(int l)
8309 int v, has_init, r;
8310 CType type, btype;
8311 Sym *sym;
8312 AttributeDef ad;
8314 while (1) {
8315 if (!parse_btype(&btype, &ad)) {
8316 /* skip redundant ';' */
8317 /* XXX: find more elegant solution */
8318 if (tok == ';') {
8319 next();
8320 continue;
8322 /* special test for old K&R protos without explicit int
8323 type. Only accepted when defining global data */
8324 if (l == VT_LOCAL || tok < TOK_DEFINE)
8325 break;
8326 btype.t = VT_INT;
8328 if (((btype.t & VT_BTYPE) == VT_ENUM ||
8329 (btype.t & VT_BTYPE) == VT_STRUCT) &&
8330 tok == ';') {
8331 /* we accept no variable after */
8332 next();
8333 continue;
8335 while (1) { /* iterate thru each declaration */
8336 type = btype;
8337 type_decl(&type, &ad, &v, TYPE_DIRECT);
8338 #if 0
8340 char buf[500];
8341 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
8342 printf("type = '%s'\n", buf);
8344 #endif
8345 if ((type.t & VT_BTYPE) == VT_FUNC) {
8346 /* if old style function prototype, we accept a
8347 declaration list */
8348 sym = type.ref;
8349 if (sym->c == FUNC_OLD)
8350 func_decl_list(sym);
8353 if (tok == '{') {
8354 #ifdef CONFIG_REG_VARS
8355 TokenString func_str;
8356 ParseState saved_parse_state;
8357 int block_level;
8358 #endif
8360 if (l == VT_LOCAL)
8361 error("cannot use local functions");
8362 if (!(type.t & VT_FUNC))
8363 expect("function definition");
8364 /* XXX: cannot do better now: convert extern line to static inline */
8365 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
8366 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
8368 #ifdef CONFIG_REG_VARS
8369 /* parse all function code and record it */
8371 tok_str_new(&func_str);
8373 block_level = 0;
8374 for(;;) {
8375 int t;
8376 if (tok == -1)
8377 error("unexpected end of file");
8378 tok_str_add_tok(&func_str);
8379 t = tok;
8380 next();
8381 if (t == '{') {
8382 block_level++;
8383 } else if (t == '}') {
8384 block_level--;
8385 if (block_level == 0)
8386 break;
8389 tok_str_add(&func_str, -1);
8390 tok_str_add(&func_str, 0);
8392 save_parse_state(&saved_parse_state);
8394 macro_ptr = func_str.str;
8395 next();
8396 analyse_function();
8397 #endif
8399 /* compute text section */
8400 cur_text_section = ad.section;
8401 if (!cur_text_section)
8402 cur_text_section = text_section;
8403 ind = cur_text_section->data_offset;
8404 funcname = get_tok_str(v, NULL);
8405 sym = sym_find(v);
8406 if (sym) {
8407 /* if symbol is already defined, then put complete type */
8408 sym->type = type;
8409 } else {
8410 /* put function symbol */
8411 sym = global_identifier_push(v, type.t, 0);
8412 sym->type.ref = type.ref;
8414 /* NOTE: we patch the symbol size later */
8415 put_extern_sym(sym, cur_text_section, ind, 0);
8416 func_ind = ind;
8417 sym->r = VT_SYM | VT_CONST;
8418 /* put debug symbol */
8419 if (do_debug)
8420 put_func_debug(sym);
8421 /* push a dummy symbol to enable local sym storage */
8422 sym_push2(&local_stack, SYM_FIELD, 0, 0);
8423 gfunc_prolog(&type);
8424 loc = 0;
8425 rsym = 0;
8426 #ifdef CONFIG_REG_VARS
8427 macro_ptr = func_str.str;
8428 next();
8429 #endif
8430 block(NULL, NULL, NULL, NULL, 0, 0);
8431 gsym(rsym);
8432 gfunc_epilog();
8433 cur_text_section->data_offset = ind;
8434 label_pop(&global_label_stack, NULL);
8435 sym_pop(&local_stack, NULL); /* reset local stack */
8436 /* end of function */
8437 /* patch symbol size */
8438 ((Elf32_Sym *)symtab_section->data)[sym->c].st_size =
8439 ind - func_ind;
8440 if (do_debug) {
8441 put_stabn(N_FUN, 0, 0, ind - func_ind);
8443 funcname = ""; /* for safety */
8444 func_vt.t = VT_VOID; /* for safety */
8445 ind = 0; /* for safety */
8447 #ifdef CONFIG_REG_VARS
8448 tok_str_free(func_str.str);
8449 restore_parse_state(&saved_parse_state);
8450 #endif
8451 break;
8452 } else {
8453 if (btype.t & VT_TYPEDEF) {
8454 /* save typedefed type */
8455 /* XXX: test storage specifiers ? */
8456 sym = sym_push(v, &type, 0, 0);
8457 sym->type.t |= VT_TYPEDEF;
8458 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
8459 /* external function definition */
8460 external_sym(v, &type, 0);
8461 } else {
8462 /* not lvalue if array */
8463 r = 0;
8464 if (!(type.t & VT_ARRAY))
8465 r |= lvalue_type(type.t);
8466 has_init = (tok == '=');
8467 if ((btype.t & VT_EXTERN) ||
8468 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
8469 !has_init && l == VT_CONST && type.ref->c < 0)) {
8470 /* external variable */
8471 /* NOTE: as GCC, uninitialized global static
8472 arrays of null size are considered as
8473 extern */
8474 external_sym(v, &type, r);
8475 } else {
8476 if (type.t & VT_STATIC)
8477 r |= VT_CONST;
8478 else
8479 r |= l;
8480 if (has_init)
8481 next();
8482 decl_initializer_alloc(&type, &ad, r,
8483 has_init, v, l);
8486 if (tok != ',') {
8487 skip(';');
8488 break;
8490 next();
8496 /* better than nothing, but needs extension to handle '-E' option
8497 correctly too */
8498 static void preprocess_init(TCCState *s1)
8500 s1->include_stack_ptr = s1->include_stack;
8501 /* XXX: move that before to avoid having to initialize
8502 file->ifdef_stack_ptr ? */
8503 s1->ifdef_stack_ptr = s1->ifdef_stack;
8504 file->ifdef_stack_ptr = s1->ifdef_stack_ptr;
8506 /* XXX: not ANSI compliant: bound checking says error */
8507 vtop = vstack - 1;
8510 /* compile the C file opened in 'file'. Return non zero if errors. */
8511 static int tcc_compile(TCCState *s1)
8513 Sym *define_start;
8514 char buf[512];
8515 volatile int section_sym;
8517 #ifdef INC_DEBUG
8518 printf("%s: **** new file\n", file->filename);
8519 #endif
8520 preprocess_init(s1);
8522 funcname = "";
8523 anon_sym = SYM_FIRST_ANOM;
8525 /* file info: full path + filename */
8526 section_sym = 0; /* avoid warning */
8527 if (do_debug) {
8528 section_sym = put_elf_sym(symtab_section, 0, 0,
8529 ELF32_ST_INFO(STB_LOCAL, STT_SECTION), 0,
8530 text_section->sh_num, NULL);
8531 getcwd(buf, sizeof(buf));
8532 pstrcat(buf, sizeof(buf), "/");
8533 put_stabs_r(buf, N_SO, 0, 0,
8534 text_section->data_offset, text_section, section_sym);
8535 put_stabs_r(file->filename, N_SO, 0, 0,
8536 text_section->data_offset, text_section, section_sym);
8538 /* an elf symbol of type STT_FILE must be put so that STB_LOCAL
8539 symbols can be safely used */
8540 put_elf_sym(symtab_section, 0, 0,
8541 ELF32_ST_INFO(STB_LOCAL, STT_FILE), 0,
8542 SHN_ABS, file->filename);
8544 /* define some often used types */
8545 int_type.t = VT_INT;
8547 char_pointer_type.t = VT_BYTE;
8548 mk_pointer(&char_pointer_type);
8550 func_old_type.t = VT_FUNC;
8551 func_old_type.ref = sym_push(SYM_FIELD, &int_type, FUNC_CDECL, FUNC_OLD);
8553 #if 0
8554 /* define 'void *alloca(unsigned int)' builtin function */
8556 Sym *s1;
8558 p = anon_sym++;
8559 sym = sym_push(p, mk_pointer(VT_VOID), FUNC_CDECL, FUNC_NEW);
8560 s1 = sym_push(SYM_FIELD, VT_UNSIGNED | VT_INT, 0, 0);
8561 s1->next = NULL;
8562 sym->next = s1;
8563 sym_push(TOK_alloca, VT_FUNC | (p << VT_STRUCT_SHIFT), VT_CONST, 0);
8565 #endif
8567 define_start = define_stack;
8569 if (setjmp(s1->error_jmp_buf) == 0) {
8570 s1->nb_errors = 0;
8571 s1->error_set_jmp_enabled = 1;
8573 ch = file->buf_ptr[0];
8574 tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
8575 parse_flags = PARSE_FLAG_PREPROCESS | PARSE_FLAG_TOK_NUM;
8576 next();
8577 decl(VT_CONST);
8578 if (tok != -1)
8579 expect("declaration");
8581 /* end of translation unit info */
8582 if (do_debug) {
8583 put_stabs_r(NULL, N_SO, 0, 0,
8584 text_section->data_offset, text_section, section_sym);
8587 s1->error_set_jmp_enabled = 0;
8589 /* reset define stack, but leave -Dsymbols (may be incorrect if
8590 they are undefined) */
8591 free_defines(define_start);
8593 sym_pop(&global_stack, NULL);
8595 return s1->nb_errors != 0 ? -1 : 0;
8598 #ifdef LIBTCC
8599 int tcc_compile_string(TCCState *s, const char *str)
8601 BufferedFile bf1, *bf = &bf1;
8602 int ret, len;
8603 char *buf;
8605 /* init file structure */
8606 bf->fd = -1;
8607 /* XXX: avoid copying */
8608 len = strlen(str);
8609 buf = tcc_malloc(len + 1);
8610 if (!buf)
8611 return -1;
8612 memcpy(buf, str, len);
8613 buf[len] = CH_EOB;
8614 bf->buf_ptr = buf;
8615 bf->buf_end = buf + len;
8616 pstrcpy(bf->filename, sizeof(bf->filename), "<string>");
8617 bf->line_num = 1;
8618 file = bf;
8620 ret = tcc_compile(s);
8622 tcc_free(buf);
8624 /* currently, no need to close */
8625 return ret;
8627 #endif
8629 /* define a preprocessor symbol. A value can also be provided with the '=' operator */
8630 void tcc_define_symbol(TCCState *s1, const char *sym, const char *value)
8632 BufferedFile bf1, *bf = &bf1;
8634 pstrcpy(bf->buffer, IO_BUF_SIZE, sym);
8635 pstrcat(bf->buffer, IO_BUF_SIZE, " ");
8636 /* default value */
8637 if (!value)
8638 value = "1";
8639 pstrcat(bf->buffer, IO_BUF_SIZE, value);
8641 /* init file structure */
8642 bf->fd = -1;
8643 bf->buf_ptr = bf->buffer;
8644 bf->buf_end = bf->buffer + strlen(bf->buffer);
8645 *bf->buf_end = CH_EOB;
8646 bf->filename[0] = '\0';
8647 bf->line_num = 1;
8648 file = bf;
8650 s1->include_stack_ptr = s1->include_stack;
8652 /* parse with define parser */
8653 ch = file->buf_ptr[0];
8654 next_nomacro();
8655 parse_define();
8656 file = NULL;
8659 /* undefine a preprocessor symbol */
8660 void tcc_undefine_symbol(TCCState *s1, const char *sym)
8662 TokenSym *ts;
8663 Sym *s;
8664 ts = tok_alloc(sym, strlen(sym));
8665 s = define_find(ts->tok);
8666 /* undefine symbol by putting an invalid name */
8667 if (s)
8668 define_undef(s);
8671 #ifdef CONFIG_TCC_ASM
8673 #include "i386-asm.c"
8674 #include "tccasm.c"
8676 #else
8677 static void asm_instr(void)
8679 error("inline asm() not supported");
8681 #endif
8683 #include "tccelf.c"
8685 /* print the position in the source file of PC value 'pc' by reading
8686 the stabs debug information */
8687 static void rt_printline(unsigned long wanted_pc)
8689 Stab_Sym *sym, *sym_end;
8690 char func_name[128], last_func_name[128];
8691 unsigned long func_addr, last_pc, pc;
8692 const char *incl_files[INCLUDE_STACK_SIZE];
8693 int incl_index, len, last_line_num, i;
8694 const char *str, *p;
8696 fprintf(stderr, "0x%08lx:", wanted_pc);
8698 func_name[0] = '\0';
8699 func_addr = 0;
8700 incl_index = 0;
8701 last_func_name[0] = '\0';
8702 last_pc = 0xffffffff;
8703 last_line_num = 1;
8704 sym = (Stab_Sym *)stab_section->data + 1;
8705 sym_end = (Stab_Sym *)(stab_section->data + stab_section->data_offset);
8706 while (sym < sym_end) {
8707 switch(sym->n_type) {
8708 /* function start or end */
8709 case N_FUN:
8710 if (sym->n_strx == 0) {
8711 /* we test if between last line and end of function */
8712 pc = sym->n_value + func_addr;
8713 if (wanted_pc >= last_pc && wanted_pc < pc)
8714 goto found;
8715 func_name[0] = '\0';
8716 func_addr = 0;
8717 } else {
8718 str = stabstr_section->data + sym->n_strx;
8719 p = strchr(str, ':');
8720 if (!p) {
8721 pstrcpy(func_name, sizeof(func_name), str);
8722 } else {
8723 len = p - str;
8724 if (len > sizeof(func_name) - 1)
8725 len = sizeof(func_name) - 1;
8726 memcpy(func_name, str, len);
8727 func_name[len] = '\0';
8729 func_addr = sym->n_value;
8731 break;
8732 /* line number info */
8733 case N_SLINE:
8734 pc = sym->n_value + func_addr;
8735 if (wanted_pc >= last_pc && wanted_pc < pc)
8736 goto found;
8737 last_pc = pc;
8738 last_line_num = sym->n_desc;
8739 /* XXX: slow! */
8740 strcpy(last_func_name, func_name);
8741 break;
8742 /* include files */
8743 case N_BINCL:
8744 str = stabstr_section->data + sym->n_strx;
8745 add_incl:
8746 if (incl_index < INCLUDE_STACK_SIZE) {
8747 incl_files[incl_index++] = str;
8749 break;
8750 case N_EINCL:
8751 if (incl_index > 1)
8752 incl_index--;
8753 break;
8754 case N_SO:
8755 if (sym->n_strx == 0) {
8756 incl_index = 0; /* end of translation unit */
8757 } else {
8758 str = stabstr_section->data + sym->n_strx;
8759 /* do not add path */
8760 len = strlen(str);
8761 if (len > 0 && str[len - 1] != '/')
8762 goto add_incl;
8764 break;
8766 sym++;
8769 /* second pass: we try symtab symbols (no line number info) */
8770 incl_index = 0;
8772 Elf32_Sym *sym, *sym_end;
8773 int type;
8775 sym_end = (Elf32_Sym *)(symtab_section->data + symtab_section->data_offset);
8776 for(sym = (Elf32_Sym *)symtab_section->data + 1;
8777 sym < sym_end;
8778 sym++) {
8779 type = ELF32_ST_TYPE(sym->st_info);
8780 if (type == STT_FUNC) {
8781 if (wanted_pc >= sym->st_value &&
8782 wanted_pc < sym->st_value + sym->st_size) {
8783 pstrcpy(last_func_name, sizeof(last_func_name),
8784 strtab_section->data + sym->st_name);
8785 goto found;
8790 /* did not find any info: */
8791 fprintf(stderr, " ???\n");
8792 return;
8793 found:
8794 if (last_func_name[0] != '\0') {
8795 fprintf(stderr, " %s()", last_func_name);
8797 if (incl_index > 0) {
8798 fprintf(stderr, " (%s:%d",
8799 incl_files[incl_index - 1], last_line_num);
8800 for(i = incl_index - 2; i >= 0; i--)
8801 fprintf(stderr, ", included from %s", incl_files[i]);
8802 fprintf(stderr, ")");
8804 fprintf(stderr, "\n");
8807 #ifndef WIN32
8809 #ifdef __i386__
8811 /* fix for glibc 2.1 */
8812 #ifndef REG_EIP
8813 #define REG_EIP EIP
8814 #define REG_EBP EBP
8815 #endif
8817 /* return the PC at frame level 'level'. Return non zero if not found */
8818 static int rt_get_caller_pc(unsigned long *paddr,
8819 ucontext_t *uc, int level)
8821 unsigned long fp;
8822 int i;
8824 if (level == 0) {
8825 #ifdef __FreeBSD__
8826 *paddr = uc->uc_mcontext.mc_eip;
8827 #else
8828 *paddr = uc->uc_mcontext.gregs[REG_EIP];
8829 #endif
8830 return 0;
8831 } else {
8832 #ifdef __FreeBSD__
8833 fp = uc->uc_mcontext.mc_ebp;
8834 #else
8835 fp = uc->uc_mcontext.gregs[REG_EBP];
8836 #endif
8837 for(i=1;i<level;i++) {
8838 /* XXX: check address validity with program info */
8839 if (fp <= 0x1000 || fp >= 0xc0000000)
8840 return -1;
8841 fp = ((unsigned long *)fp)[0];
8843 *paddr = ((unsigned long *)fp)[1];
8844 return 0;
8847 #else
8848 #error add arch specific rt_get_caller_pc()
8849 #endif
8851 /* emit a run time error at position 'pc' */
8852 void rt_error(ucontext_t *uc, const char *fmt, ...)
8854 va_list ap;
8855 unsigned long pc;
8856 int i;
8858 va_start(ap, fmt);
8859 fprintf(stderr, "Runtime error: ");
8860 vfprintf(stderr, fmt, ap);
8861 fprintf(stderr, "\n");
8862 for(i=0;i<num_callers;i++) {
8863 if (rt_get_caller_pc(&pc, uc, i) < 0)
8864 break;
8865 if (i == 0)
8866 fprintf(stderr, "at ");
8867 else
8868 fprintf(stderr, "by ");
8869 rt_printline(pc);
8871 exit(255);
8872 va_end(ap);
8875 /* signal handler for fatal errors */
8876 static void sig_error(int signum, siginfo_t *siginf, void *puc)
8878 ucontext_t *uc = puc;
8880 switch(signum) {
8881 case SIGFPE:
8882 switch(siginf->si_code) {
8883 case FPE_INTDIV:
8884 case FPE_FLTDIV:
8885 rt_error(uc, "division by zero");
8886 break;
8887 default:
8888 rt_error(uc, "floating point exception");
8889 break;
8891 break;
8892 case SIGBUS:
8893 case SIGSEGV:
8894 if (rt_bound_error_msg && *rt_bound_error_msg)
8895 rt_error(uc, *rt_bound_error_msg);
8896 else
8897 rt_error(uc, "dereferencing invalid pointer");
8898 break;
8899 case SIGILL:
8900 rt_error(uc, "illegal instruction");
8901 break;
8902 case SIGABRT:
8903 rt_error(uc, "abort() called");
8904 break;
8905 default:
8906 rt_error(uc, "caught signal %d", signum);
8907 break;
8909 exit(255);
8911 #endif
8913 /* do all relocations (needed before using tcc_get_symbol()) */
8914 int tcc_relocate(TCCState *s1)
8916 Section *s;
8917 int i;
8919 s1->nb_errors = 0;
8921 tcc_add_runtime(s1);
8923 relocate_common_syms();
8925 /* compute relocation address : section are relocated in place. We
8926 also alloc the bss space */
8927 for(i = 1; i < s1->nb_sections; i++) {
8928 s = s1->sections[i];
8929 if (s->sh_flags & SHF_ALLOC) {
8930 if (s->sh_type == SHT_NOBITS)
8931 s->data = tcc_mallocz(s->data_offset);
8932 s->sh_addr = (unsigned long)s->data;
8936 relocate_syms(s1, 1);
8938 if (s1->nb_errors != 0)
8939 return -1;
8941 /* relocate each section */
8942 for(i = 1; i < s1->nb_sections; i++) {
8943 s = s1->sections[i];
8944 if (s->reloc)
8945 relocate_section(s1, s);
8947 return 0;
8950 /* launch the compiled program with the given arguments */
8951 int tcc_run(TCCState *s1, int argc, char **argv)
8953 int (*prog_main)(int, char **);
8955 if (tcc_relocate(s1) < 0)
8956 return -1;
8958 prog_main = tcc_get_symbol(s1, "main");
8960 if (do_debug) {
8961 #ifdef WIN32
8962 error("debug mode currently not available for Windows");
8963 #else
8964 struct sigaction sigact;
8965 /* install TCC signal handlers to print debug info on fatal
8966 runtime errors */
8967 sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
8968 sigact.sa_sigaction = sig_error;
8969 sigemptyset(&sigact.sa_mask);
8970 sigaction(SIGFPE, &sigact, NULL);
8971 sigaction(SIGILL, &sigact, NULL);
8972 sigaction(SIGSEGV, &sigact, NULL);
8973 sigaction(SIGBUS, &sigact, NULL);
8974 sigaction(SIGABRT, &sigact, NULL);
8975 #endif
8978 #ifdef CONFIG_TCC_BCHECK
8979 if (do_bounds_check) {
8980 void (*bound_init)(void);
8982 /* set error function */
8983 rt_bound_error_msg = (void *)tcc_get_symbol(s1, "__bound_error_msg");
8985 /* XXX: use .init section so that it also work in binary ? */
8986 bound_init = (void *)tcc_get_symbol(s1, "__bound_init");
8987 bound_init();
8989 #endif
8990 return (*prog_main)(argc, argv);
8993 TCCState *tcc_new(void)
8995 const char *p, *r;
8996 TCCState *s;
8997 TokenSym *ts;
8998 int i, c;
9000 s = tcc_mallocz(sizeof(TCCState));
9001 if (!s)
9002 return NULL;
9003 tcc_state = s;
9004 s->output_type = TCC_OUTPUT_MEMORY;
9006 /* init isid table */
9007 for(i=0;i<256;i++)
9008 isidnum_table[i] = isid(i) || isnum(i);
9010 /* add all tokens */
9011 table_ident = NULL;
9012 memset(hash_ident, 0, TOK_HASH_SIZE * sizeof(TokenSym *));
9014 tok_ident = TOK_IDENT;
9015 p = tcc_keywords;
9016 while (*p) {
9017 r = p;
9018 for(;;) {
9019 c = *r++;
9020 if (c == '\0')
9021 break;
9023 ts = tok_alloc(p, r - p - 1);
9024 p = r;
9027 /* we add dummy defines for some special macros to speed up tests
9028 and to have working defined() */
9029 define_push(TOK___LINE__, MACRO_OBJ, NULL, NULL);
9030 define_push(TOK___FILE__, MACRO_OBJ, NULL, NULL);
9031 define_push(TOK___DATE__, MACRO_OBJ, NULL, NULL);
9032 define_push(TOK___TIME__, MACRO_OBJ, NULL, NULL);
9034 /* standard defines */
9035 tcc_define_symbol(s, "__STDC__", NULL);
9036 #if defined(TCC_TARGET_I386)
9037 tcc_define_symbol(s, "__i386__", NULL);
9038 #endif
9039 #if defined(linux)
9040 tcc_define_symbol(s, "__linux__", NULL);
9041 tcc_define_symbol(s, "linux", NULL);
9042 #endif
9043 /* tiny C specific defines */
9044 tcc_define_symbol(s, "__TINYC__", NULL);
9046 /* tiny C & gcc defines */
9047 tcc_define_symbol(s, "__SIZE_TYPE__", "unsigned int");
9048 tcc_define_symbol(s, "__PTRDIFF_TYPE__", "int");
9049 tcc_define_symbol(s, "__WCHAR_TYPE__", "int");
9051 /* default library paths */
9052 tcc_add_library_path(s, "/usr/local/lib");
9053 tcc_add_library_path(s, "/usr/lib");
9054 tcc_add_library_path(s, "/lib");
9056 /* no section zero */
9057 dynarray_add((void ***)&s->sections, &s->nb_sections, NULL);
9059 /* create standard sections */
9060 text_section = new_section(s, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
9061 data_section = new_section(s, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
9062 bss_section = new_section(s, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
9064 /* symbols are always generated for linking stage */
9065 symtab_section = new_symtab(s, ".symtab", SHT_SYMTAB, 0,
9066 ".strtab",
9067 ".hashtab", SHF_PRIVATE);
9068 strtab_section = symtab_section->link;
9070 /* private symbol table for dynamic symbols */
9071 s->dynsymtab_section = new_symtab(s, ".dynsymtab", SHT_SYMTAB, SHF_PRIVATE,
9072 ".dynstrtab",
9073 ".dynhashtab", SHF_PRIVATE);
9074 return s;
9077 void tcc_delete(TCCState *s1)
9079 int i, n;
9081 /* free -D defines */
9082 free_defines(NULL);
9084 /* free tokens */
9085 n = tok_ident - TOK_IDENT;
9086 for(i = 0; i < n; i++)
9087 tcc_free(table_ident[i]);
9088 tcc_free(table_ident);
9090 /* free all sections */
9092 free_section(symtab_section->hash);
9094 free_section(s1->dynsymtab_section->hash);
9095 free_section(s1->dynsymtab_section->link);
9096 free_section(s1->dynsymtab_section);
9098 for(i = 1; i < s1->nb_sections; i++)
9099 free_section(s1->sections[i]);
9100 tcc_free(s1->sections);
9102 /* free loaded dlls array */
9103 for(i = 0; i < s1->nb_loaded_dlls; i++)
9104 tcc_free(s1->loaded_dlls[i]);
9105 tcc_free(s1->loaded_dlls);
9107 /* library paths */
9108 for(i = 0; i < s1->nb_library_paths; i++)
9109 tcc_free(s1->library_paths[i]);
9110 tcc_free(s1->library_paths);
9112 /* cached includes */
9113 for(i = 0; i < s1->nb_cached_includes; i++)
9114 tcc_free(s1->cached_includes[i]);
9115 tcc_free(s1->cached_includes);
9117 for(i = 0; i < s1->nb_include_paths; i++)
9118 tcc_free(s1->include_paths[i]);
9119 tcc_free(s1->include_paths);
9121 for(i = 0; i < s1->nb_sysinclude_paths; i++)
9122 tcc_free(s1->sysinclude_paths[i]);
9123 tcc_free(s1->sysinclude_paths);
9125 tcc_free(s1);
9128 int tcc_add_include_path(TCCState *s1, const char *pathname)
9130 char *pathname1;
9132 pathname1 = tcc_strdup(pathname);
9133 dynarray_add((void ***)&s1->include_paths, &s1->nb_include_paths, pathname1);
9134 return 0;
9137 int tcc_add_sysinclude_path(TCCState *s1, const char *pathname)
9139 char *pathname1;
9141 pathname1 = tcc_strdup(pathname);
9142 dynarray_add((void ***)&s1->sysinclude_paths, &s1->nb_sysinclude_paths, pathname1);
9143 return 0;
9146 static int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
9148 const char *ext, *filename1;
9149 Elf32_Ehdr ehdr;
9150 int fd, ret;
9151 BufferedFile *saved_file;
9153 /* find source file type with extension */
9154 filename1 = strrchr(filename, '/');
9155 if (filename1)
9156 filename1++;
9157 else
9158 filename1 = filename;
9159 ext = strrchr(filename1, '.');
9160 if (ext)
9161 ext++;
9163 /* open the file */
9164 saved_file = file;
9165 file = tcc_open(s1, filename);
9166 if (!file) {
9167 if (flags & AFF_PRINT_ERROR) {
9168 error_noabort("file '%s' not found", filename);
9170 ret = -1;
9171 goto fail1;
9174 if (!ext || !strcmp(ext, "c")) {
9175 /* C file assumed */
9176 ret = tcc_compile(s1);
9177 } else
9178 #ifdef CONFIG_TCC_ASM
9179 if (!strcmp(ext, "S")) {
9180 /* preprocessed assembler */
9181 ret = tcc_assemble(s1, 1);
9182 } else if (!strcmp(ext, "s")) {
9183 /* non preprocessed assembler */
9184 ret = tcc_assemble(s1, 0);
9185 } else
9186 #endif
9188 fd = file->fd;
9189 /* assume executable format: auto guess file type */
9190 if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
9191 error_noabort("could not read header");
9192 goto fail;
9194 lseek(fd, 0, SEEK_SET);
9196 if (ehdr.e_ident[0] == ELFMAG0 &&
9197 ehdr.e_ident[1] == ELFMAG1 &&
9198 ehdr.e_ident[2] == ELFMAG2 &&
9199 ehdr.e_ident[3] == ELFMAG3) {
9200 file->line_num = 0; /* do not display line number if error */
9201 if (ehdr.e_type == ET_REL) {
9202 ret = tcc_load_object_file(s1, fd, 0);
9203 } else if (ehdr.e_type == ET_DYN) {
9204 ret = tcc_load_dll(s1, fd, filename,
9205 (flags & AFF_REFERENCED_DLL) != 0);
9206 } else {
9207 error_noabort("unrecognized ELF file");
9208 goto fail;
9210 } else if (memcmp((char *)&ehdr, ARMAG, 8) == 0) {
9211 file->line_num = 0; /* do not display line number if error */
9212 ret = tcc_load_archive(s1, fd);
9213 } else {
9214 /* as GNU ld, consider it is an ld script if not recognized */
9215 ret = tcc_load_ldscript(s1);
9216 if (ret < 0) {
9217 error_noabort("unrecognized file type");
9218 goto fail;
9222 the_end:
9223 tcc_close(file);
9224 fail1:
9225 file = saved_file;
9226 return ret;
9227 fail:
9228 ret = -1;
9229 goto the_end;
9232 int tcc_add_file(TCCState *s, const char *filename)
9234 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
9237 int tcc_add_library_path(TCCState *s, const char *pathname)
9239 char *pathname1;
9241 pathname1 = tcc_strdup(pathname);
9242 dynarray_add((void ***)&s->library_paths, &s->nb_library_paths, pathname1);
9243 return 0;
9246 /* find and load a dll. Return non zero if not found */
9247 /* XXX: add '-rpath' option support ? */
9248 static int tcc_add_dll(TCCState *s, const char *filename, int flags)
9250 char buf[1024];
9251 int i;
9253 for(i = 0; i < s->nb_library_paths; i++) {
9254 snprintf(buf, sizeof(buf), "%s/%s",
9255 s->library_paths[i], filename);
9256 if (tcc_add_file_internal(s, buf, flags) == 0)
9257 return 0;
9259 return -1;
9262 /* the library name is the same as the argument of the '-l' option */
9263 int tcc_add_library(TCCState *s, const char *libraryname)
9265 char buf[1024];
9266 int i;
9267 void *h;
9269 /* first we look for the dynamic library if not static linking */
9270 if (!s->static_link) {
9271 snprintf(buf, sizeof(buf), "lib%s.so", libraryname);
9272 /* if we output to memory, then we simply we dlopen(). */
9273 if (s->output_type == TCC_OUTPUT_MEMORY) {
9274 /* Since the libc is already loaded, we don't need to load it again */
9275 if (!strcmp(libraryname, "c"))
9276 return 0;
9277 h = dlopen(buf, RTLD_GLOBAL | RTLD_LAZY);
9278 if (h)
9279 return 0;
9280 } else {
9281 if (tcc_add_dll(s, buf, 0) == 0)
9282 return 0;
9286 /* then we look for the static library */
9287 for(i = 0; i < s->nb_library_paths; i++) {
9288 snprintf(buf, sizeof(buf), "%s/lib%s.a",
9289 s->library_paths[i], libraryname);
9290 if (tcc_add_file_internal(s, buf, 0) == 0)
9291 return 0;
9293 return -1;
9296 int tcc_add_symbol(TCCState *s, const char *name, unsigned long val)
9298 add_elf_sym(symtab_section, val, 0,
9299 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE),
9300 SHN_ABS, name);
9301 return 0;
9304 int tcc_set_output_type(TCCState *s, int output_type)
9306 char buf[1024];
9308 s->output_type = output_type;
9310 if (!s->nostdinc) {
9311 /* default include paths */
9312 /* XXX: reverse order needed if -isystem support */
9313 tcc_add_sysinclude_path(s, "/usr/local/include");
9314 tcc_add_sysinclude_path(s, "/usr/include");
9315 snprintf(buf, sizeof(buf), "%s/include", tcc_lib_path);
9316 tcc_add_sysinclude_path(s, buf);
9319 /* if bound checking, then add corresponding sections */
9320 #ifdef CONFIG_TCC_BCHECK
9321 if (do_bounds_check) {
9322 /* define symbol */
9323 tcc_define_symbol(s, "__BOUNDS_CHECKING_ON", NULL);
9324 /* create bounds sections */
9325 bounds_section = new_section(s, ".bounds",
9326 SHT_PROGBITS, SHF_ALLOC);
9327 lbounds_section = new_section(s, ".lbounds",
9328 SHT_PROGBITS, SHF_ALLOC);
9330 #endif
9332 /* add debug sections */
9333 if (do_debug) {
9334 /* stab symbols */
9335 stab_section = new_section(s, ".stab", SHT_PROGBITS, 0);
9336 stab_section->sh_entsize = sizeof(Stab_Sym);
9337 stabstr_section = new_section(s, ".stabstr", SHT_STRTAB, 0);
9338 put_elf_str(stabstr_section, "");
9339 stab_section->link = stabstr_section;
9340 /* put first entry */
9341 put_stabs("", 0, 0, 0, 0);
9344 /* add libc crt1/crti objects */
9345 if (output_type == TCC_OUTPUT_EXE ||
9346 output_type == TCC_OUTPUT_DLL) {
9347 if (output_type != TCC_OUTPUT_DLL)
9348 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crt1.o");
9349 tcc_add_file(s, CONFIG_TCC_CRT_PREFIX "/crti.o");
9351 return 0;
9354 #if !defined(LIBTCC)
9356 static int64_t getclock_us(void)
9358 #ifdef WIN32
9359 struct _timeb tb;
9360 _ftime(&tb);
9361 return (tb.time * 1000LL + tb.millitm) * 1000LL;
9362 #else
9363 struct timeval tv;
9364 gettimeofday(&tv, NULL);
9365 return tv.tv_sec * 1000000LL + tv.tv_usec;
9366 #endif
9369 void help(void)
9371 printf("tcc version 0.9.16 - Tiny C Compiler - Copyright (C) 2001, 2002 Fabrice Bellard\n"
9372 "usage: tcc [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
9373 " [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
9374 " [--] infile1 [infile2... --] [infile_args...]\n"
9375 "\n"
9376 "General options:\n"
9377 " -c compile only - generate an object file\n"
9378 " -o outfile set output filename\n"
9379 " -- allows multiples input files if no -o option given. Also\n"
9380 " separate input files from runtime arguments\n"
9381 " -Bdir set tcc internal library path\n"
9382 " -bench output compilation statistics\n"
9383 "Preprocessor options:\n"
9384 " -Idir add include path 'dir'\n"
9385 " -Dsym[=val] define 'sym' with value 'val'\n"
9386 " -Usym undefine 'sym'\n"
9387 "Linker options:\n"
9388 " -Ldir add library path 'dir'\n"
9389 " -llib link with dynamic or static library 'lib'\n"
9390 " -shared generate a shared library\n"
9391 " -static static linking\n"
9392 " -r relocatable output\n"
9393 "Debugger options:\n"
9394 " -g generate runtime debug info\n"
9395 #ifdef CONFIG_TCC_BCHECK
9396 " -b compile with built-in memory and bounds checker (implies -g)\n"
9397 #endif
9398 " -bt N show N callers in stack traces\n"
9402 #define TCC_OPTION_HAS_ARG 0x0001
9403 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
9405 typedef struct TCCOption {
9406 const char *name;
9407 uint16_t index;
9408 uint16_t flags;
9409 } TCCOption;
9411 enum {
9412 TCC_OPTION_HELP,
9413 TCC_OPTION_MARKER,
9414 TCC_OPTION_I,
9415 TCC_OPTION_D,
9416 TCC_OPTION_U,
9417 TCC_OPTION_L,
9418 TCC_OPTION_B,
9419 TCC_OPTION_l,
9420 TCC_OPTION_bench,
9421 TCC_OPTION_bt,
9422 TCC_OPTION_b,
9423 TCC_OPTION_g,
9424 TCC_OPTION_c,
9425 TCC_OPTION_static,
9426 TCC_OPTION_shared,
9427 TCC_OPTION_o,
9428 TCC_OPTION_r,
9429 TCC_OPTION_W,
9430 TCC_OPTION_O,
9431 TCC_OPTION_m,
9432 TCC_OPTION_f,
9433 TCC_OPTION_nostdinc,
9434 TCC_OPTION_print_search_dirs,
9435 TCC_OPTION_rdynamic,
9438 static const TCCOption tcc_options[] = {
9439 { "h", TCC_OPTION_HELP, 0 },
9440 { "?", TCC_OPTION_HELP, 0 },
9441 { "-", TCC_OPTION_MARKER, 0 },
9442 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
9443 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
9444 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
9445 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
9446 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
9447 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9448 { "bench", TCC_OPTION_bench, 0 },
9449 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
9450 #ifdef CONFIG_TCC_BCHECK
9451 { "b", TCC_OPTION_b, 0 },
9452 #endif
9453 { "g", TCC_OPTION_g, 0 },
9454 { "c", TCC_OPTION_c, 0 },
9455 { "static", TCC_OPTION_static, 0 },
9456 { "shared", TCC_OPTION_shared, 0 },
9457 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
9458 { "rdynamic", TCC_OPTION_rdynamic, 0 }, /* currently ignored */
9459 { "r", TCC_OPTION_r, 0 },
9460 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9461 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9462 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
9463 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
9464 { "nostdinc", TCC_OPTION_nostdinc, 0 },
9465 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
9466 { NULL },
9469 int main(int argc, char **argv)
9471 char *r;
9472 int optind, output_type, multiple_files, i, reloc_output;
9473 TCCState *s;
9474 char **files;
9475 int nb_files, nb_libraries, nb_objfiles, dminus, ret;
9476 char objfilename[1024];
9477 int64_t start_time = 0;
9478 const TCCOption *popt;
9479 const char *optarg, *p1, *r1, *outfile;
9480 int print_search_dirs;
9482 s = tcc_new();
9483 output_type = TCC_OUTPUT_MEMORY;
9485 optind = 1;
9486 outfile = NULL;
9487 multiple_files = 0;
9488 dminus = 0;
9489 files = NULL;
9490 nb_files = 0;
9491 nb_libraries = 0;
9492 reloc_output = 0;
9493 print_search_dirs = 0;
9494 while (1) {
9495 if (optind >= argc) {
9496 if (nb_files == 0 && !print_search_dirs)
9497 goto show_help;
9498 else
9499 break;
9501 r = argv[optind++];
9502 if (r[0] != '-') {
9503 /* add a new file */
9504 dynarray_add((void ***)&files, &nb_files, r);
9505 if (!multiple_files) {
9506 optind--;
9507 /* argv[0] will be this file */
9508 break;
9510 } else {
9511 /* find option in table (match only the first chars */
9512 popt = tcc_options;
9513 for(;;) {
9514 p1 = popt->name;
9515 if (p1 == NULL)
9516 error("invalid option -- '%s'", r);
9517 r1 = r + 1;
9518 for(;;) {
9519 if (*p1 == '\0')
9520 goto option_found;
9521 if (*r1 != *p1)
9522 break;
9523 p1++;
9524 r1++;
9526 popt++;
9528 option_found:
9529 if (popt->flags & TCC_OPTION_HAS_ARG) {
9530 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
9531 optarg = r1;
9532 } else {
9533 if (optind >= argc)
9534 error("argument to '%s' is missing", r);
9535 optarg = argv[optind++];
9537 } else {
9538 if (*r1 != '\0')
9539 goto show_help;
9540 optarg = NULL;
9543 switch(popt->index) {
9544 case TCC_OPTION_HELP:
9545 show_help:
9546 help();
9547 return 1;
9548 case TCC_OPTION_MARKER:
9549 /* '--' enables multiple files input and also ends several
9550 file input */
9551 if (dminus && multiple_files) {
9552 optind--; /* argv[0] will be '--' */
9553 goto end_parse;
9555 dminus = 1;
9556 multiple_files = 1;
9557 case TCC_OPTION_I:
9558 if (tcc_add_include_path(s, optarg) < 0)
9559 error("too many include paths");
9560 break;
9561 case TCC_OPTION_D:
9563 char *sym, *value;
9564 sym = (char *)optarg;
9565 value = strchr(sym, '=');
9566 if (value) {
9567 *value = '\0';
9568 value++;
9570 tcc_define_symbol(s, sym, value);
9572 break;
9573 case TCC_OPTION_U:
9574 tcc_undefine_symbol(s, optarg);
9575 break;
9576 case TCC_OPTION_L:
9577 tcc_add_library_path(s, optarg);
9578 break;
9579 case TCC_OPTION_B:
9580 /* set tcc utilities path (mainly for tcc development) */
9581 tcc_lib_path = optarg;
9582 break;
9583 case TCC_OPTION_l:
9584 dynarray_add((void ***)&files, &nb_files, r);
9585 nb_libraries++;
9586 break;
9587 case TCC_OPTION_bench:
9588 do_bench = 1;
9589 break;
9590 case TCC_OPTION_bt:
9591 num_callers = atoi(optarg);
9592 break;
9593 #ifdef CONFIG_TCC_BCHECK
9594 case TCC_OPTION_b:
9595 do_bounds_check = 1;
9596 do_debug = 1;
9597 break;
9598 #endif
9599 case TCC_OPTION_g:
9600 do_debug = 1;
9601 break;
9602 case TCC_OPTION_c:
9603 multiple_files = 1;
9604 output_type = TCC_OUTPUT_OBJ;
9605 break;
9606 case TCC_OPTION_static:
9607 s->static_link = 1;
9608 break;
9609 case TCC_OPTION_shared:
9610 output_type = TCC_OUTPUT_DLL;
9611 break;
9612 case TCC_OPTION_o:
9613 multiple_files = 1;
9614 outfile = optarg;
9615 break;
9616 case TCC_OPTION_r:
9617 /* generate a .o merging several output files */
9618 reloc_output = 1;
9619 output_type = TCC_OUTPUT_OBJ;
9620 break;
9621 case TCC_OPTION_nostdinc:
9622 s->nostdinc = 1;
9623 break;
9624 case TCC_OPTION_print_search_dirs:
9625 print_search_dirs = 1;
9626 break;
9627 default:
9628 break;
9632 end_parse:
9633 if (print_search_dirs) {
9634 /* enough for Linux kernel */
9635 printf("install: %s/\n", tcc_lib_path);
9636 return 0;
9639 nb_objfiles = nb_files - nb_libraries;
9641 /* if outfile provided without other options, we output an
9642 executable */
9643 if (outfile && output_type == TCC_OUTPUT_MEMORY)
9644 output_type = TCC_OUTPUT_EXE;
9646 /* check -c consistency : only single file handled. XXX: checks file type */
9647 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9648 /* accepts only a single input file */
9649 if (nb_objfiles != 1)
9650 error("cannot specify multiple files with -c");
9651 if (nb_libraries != 0)
9652 error("cannot specify libraries with -c");
9655 /* compute default outfile name */
9656 if (output_type != TCC_OUTPUT_MEMORY && !outfile) {
9657 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
9658 char *ext;
9659 /* add .o extension */
9660 pstrcpy(objfilename, sizeof(objfilename) - 1, files[0]);
9661 ext = strrchr(objfilename, '.');
9662 if (!ext)
9663 goto default_outfile;
9664 strcpy(ext + 1, "o");
9665 } else {
9666 default_outfile:
9667 pstrcpy(objfilename, sizeof(objfilename), "a.out");
9669 outfile = objfilename;
9672 if (do_bench) {
9673 start_time = getclock_us();
9676 tcc_set_output_type(s, output_type);
9678 /* compile or add each files or library */
9679 for(i = 0;i < nb_files; i++) {
9680 const char *filename;
9682 filename = files[i];
9683 if (filename[0] == '-') {
9684 if (tcc_add_library(s, filename + 2) < 0)
9685 error("cannot find %s", filename);
9686 } else {
9687 if (tcc_add_file(s, filename) < 0) {
9688 ret = 1;
9689 goto the_end;
9694 /* free all files */
9695 tcc_free(files);
9697 if (do_bench) {
9698 double total_time;
9699 total_time = (double)(getclock_us() - start_time) / 1000000.0;
9700 if (total_time < 0.001)
9701 total_time = 0.001;
9702 if (total_bytes < 1)
9703 total_bytes = 1;
9704 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
9705 tok_ident - TOK_IDENT, total_lines, total_bytes,
9706 total_time, (int)(total_lines / total_time),
9707 total_bytes / total_time / 1000000.0);
9710 if (s->output_type != TCC_OUTPUT_MEMORY) {
9711 tcc_output_file(s, outfile);
9712 ret = 0;
9713 } else {
9714 ret = tcc_run(s, argc - optind, argv + optind);
9716 the_end:
9717 /* XXX: cannot do it with bound checking because of the malloc hooks */
9718 if (!do_bounds_check)
9719 tcc_delete(s);
9721 #ifdef MEM_DEBUG
9722 if (do_bench) {
9723 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
9725 #endif
9726 return ret;
9729 #endif