Fix parsing of binary floating point number
[tinycc.git] / tccgen.c
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "tcc.h"
22
23 /********************************************************/
24 /* global variables */
25
26 /* loc : local variable index
27 ind : output code index
28 rsym: return symbol
29 anon_sym: anonymous symbol index
30 */
31 ST_DATA int rsym, anon_sym, ind, loc;
32
33 ST_DATA Section *text_section, *data_section, *bss_section; /* predefined sections */
34 ST_DATA Section *cur_text_section; /* current section where function code is generated */
35 #ifdef CONFIG_TCC_ASM
36 ST_DATA Section *last_text_section; /* to handle .previous asm directive */
37 #endif
38 #ifdef CONFIG_TCC_BCHECK
39 /* bound check related sections */
40 ST_DATA Section *bounds_section; /* contains global data bound description */
41 ST_DATA Section *lbounds_section; /* contains local data bound description */
42 #endif
43 /* symbol sections */
44 ST_DATA Section *symtab_section, *strtab_section;
45 /* debug sections */
46 ST_DATA Section *stab_section, *stabstr_section;
47 ST_DATA Sym *sym_free_first;
48 ST_DATA void **sym_pools;
49 ST_DATA int nb_sym_pools;
50
51 ST_DATA Sym *global_stack;
52 ST_DATA Sym *local_stack;
53 ST_DATA Sym *scope_stack_bottom;
54 ST_DATA Sym *define_stack;
55 ST_DATA Sym *global_label_stack;
56 ST_DATA Sym *local_label_stack;
57
58 ST_DATA int vla_sp_loc_tmp; /* vla_sp_loc is set to this when the value won't be needed later */
59 ST_DATA int vla_sp_root_loc; /* vla_sp_loc for SP before any VLAs were pushed */
60 ST_DATA int *vla_sp_loc; /* Pointer to variable holding location to store stack pointer on the stack when modifying stack pointer */
61 ST_DATA int vla_flags; /* VLA_* flags */
62
63 ST_DATA SValue __vstack[1+VSTACK_SIZE], *vtop;
64
65 ST_DATA int const_wanted; /* true if constant wanted */
66 ST_DATA int nocode_wanted; /* true if no code generation wanted for an expression */
67 ST_DATA int global_expr; /* true if compound literals must be allocated globally (used during initializers parsing */
68 ST_DATA CType func_vt; /* current function return type (used by return instruction) */
69 ST_DATA int func_var; /* true if current function is variadic (used by return instruction) */
70 ST_DATA int func_vc;
71 ST_DATA int last_line_num, last_ind, func_ind; /* debug last line number and pc */
72 ST_DATA char *funcname;
73
74 ST_DATA CType char_pointer_type, func_old_type, int_type, size_type;
75
76 /* ------------------------------------------------------------------------- */
77 static void gen_cast(CType *type);
78 static inline CType *pointed_type(CType *type);
79 static int is_compatible_types(CType *type1, CType *type2);
80 static int parse_btype(CType *type, AttributeDef *ad);
81 static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
82 static void parse_expr_type(CType *type);
83 static void decl_initializer(CType *type, Section *sec, unsigned long c, int first, int size_only);
84 static void block(int *bsym, int *csym, int *case_sym, int *def_sym, int case_reg, int is_expr);
85 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, int has_init, int v, char *asm_label, int scope);
86 static int decl0(int l, int is_for_loop_init);
87 static void expr_eq(void);
88 static void unary_type(CType *type);
89 static void vla_runtime_type_size(CType *type, int *a);
90 static void vla_sp_save(void);
91 static int is_compatible_parameter_types(CType *type1, CType *type2);
92 static void expr_type(CType *type);
93 ST_FUNC void vpush64(int ty, unsigned long long v);
94 ST_FUNC void vpush(CType *type);
95 ST_FUNC int gvtst(int inv, int t);
96 ST_FUNC int is_btype_size(int bt);
97
98 ST_INLN int is_float(int t)
99 {
100 int bt;
101 bt = t & VT_BTYPE;
102 return bt == VT_LDOUBLE || bt == VT_DOUBLE || bt == VT_FLOAT || bt == VT_QFLOAT;
103 }
104
105 /* we use our own 'finite' function to avoid potential problems with
106 non standard math libs */
107 /* XXX: endianness dependent */
108 ST_FUNC int ieee_finite(double d)
109 {
110 int p[4];
111 memcpy(p, &d, sizeof(double));
112 return ((unsigned)((p[1] | 0x800fffff) + 1)) >> 31;
113 }
114
115 ST_FUNC void test_lvalue(void)
116 {
117 if (!(vtop->r & VT_LVAL))
118 expect("lvalue");
119 }
120
121 /* ------------------------------------------------------------------------- */
122 /* symbol allocator */
123 static Sym *__sym_malloc(void)
124 {
125 Sym *sym_pool, *sym, *last_sym;
126 int i;
127
128 sym_pool = tcc_malloc(SYM_POOL_NB * sizeof(Sym));
129 dynarray_add(&sym_pools, &nb_sym_pools, sym_pool);
130
131 last_sym = sym_free_first;
132 sym = sym_pool;
133 for(i = 0; i < SYM_POOL_NB; i++) {
134 sym->next = last_sym;
135 last_sym = sym;
136 sym++;
137 }
138 sym_free_first = last_sym;
139 return last_sym;
140 }
141
142 static inline Sym *sym_malloc(void)
143 {
144 Sym *sym;
145 sym = sym_free_first;
146 if (!sym)
147 sym = __sym_malloc();
148 sym_free_first = sym->next;
149 return sym;
150 }
151
152 ST_INLN void sym_free(Sym *sym)
153 {
154 sym->next = sym_free_first;
155 tcc_free(sym->asm_label);
156 sym_free_first = sym;
157 }
158
159 /* push, without hashing */
160 ST_FUNC Sym *sym_push2(Sym **ps, int v, int t, long c)
161 {
162 Sym *s;
163 if (ps == &local_stack) {
164 for (s = *ps; s && s != scope_stack_bottom; s = s->prev)
165 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM && s->v == v)
166 tcc_error("incompatible types for redefinition of '%s'",
167 get_tok_str(v, NULL));
168 }
169 s = sym_malloc();
170 s->asm_label = NULL;
171 s->v = v;
172 s->type.t = t;
173 s->type.ref = NULL;
174 #ifdef _WIN64
175 s->d = NULL;
176 #endif
177 s->c = c;
178 s->next = NULL;
179 /* add in stack */
180 s->prev = *ps;
181 *ps = s;
182 return s;
183 }
184
185 /* find a symbol and return its associated structure. 's' is the top
186 of the symbol stack */
187 ST_FUNC Sym *sym_find2(Sym *s, int v)
188 {
189 while (s) {
190 if (s->v == v)
191 return s;
192 else if (s->v == -1)
193 return NULL;
194 s = s->prev;
195 }
196 return NULL;
197 }
198
199 /* structure lookup */
200 ST_INLN Sym *struct_find(int v)
201 {
202 v -= TOK_IDENT;
203 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
204 return NULL;
205 return table_ident[v]->sym_struct;
206 }
207
208 /* find an identifier */
209 ST_INLN Sym *sym_find(int v)
210 {
211 v -= TOK_IDENT;
212 if ((unsigned)v >= (unsigned)(tok_ident - TOK_IDENT))
213 return NULL;
214 return table_ident[v]->sym_identifier;
215 }
216
217 /* push a given symbol on the symbol stack */
218 ST_FUNC Sym *sym_push(int v, CType *type, int r, int c)
219 {
220 Sym *s, **ps;
221 TokenSym *ts;
222
223 if (local_stack)
224 ps = &local_stack;
225 else
226 ps = &global_stack;
227 s = sym_push2(ps, v, type->t, c);
228 s->type.ref = type->ref;
229 s->r = r;
230 /* don't record fields or anonymous symbols */
231 /* XXX: simplify */
232 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
233 /* record symbol in token array */
234 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
235 if (v & SYM_STRUCT)
236 ps = &ts->sym_struct;
237 else
238 ps = &ts->sym_identifier;
239 s->prev_tok = *ps;
240 *ps = s;
241 }
242 return s;
243 }
244
245 /* push a global identifier */
246 ST_FUNC Sym *global_identifier_push(int v, int t, int c)
247 {
248 Sym *s, **ps;
249 s = sym_push2(&global_stack, v, t, c);
250 /* don't record anonymous symbol */
251 if (v < SYM_FIRST_ANOM) {
252 ps = &table_ident[v - TOK_IDENT]->sym_identifier;
253 /* modify the top most local identifier, so that
254 sym_identifier will point to 's' when popped */
255 while (*ps != NULL)
256 ps = &(*ps)->prev_tok;
257 s->prev_tok = NULL;
258 *ps = s;
259 }
260 return s;
261 }
262
263 /* pop symbols until top reaches 'b' */
264 ST_FUNC void sym_pop(Sym **ptop, Sym *b)
265 {
266 Sym *s, *ss, **ps;
267 TokenSym *ts;
268 int v;
269
270 s = *ptop;
271 while(s != b) {
272 ss = s->prev;
273 v = s->v;
274 /* remove symbol in token array */
275 /* XXX: simplify */
276 if (!(v & SYM_FIELD) && (v & ~SYM_STRUCT) < SYM_FIRST_ANOM) {
277 ts = table_ident[(v & ~SYM_STRUCT) - TOK_IDENT];
278 if (v & SYM_STRUCT)
279 ps = &ts->sym_struct;
280 else
281 ps = &ts->sym_identifier;
282 *ps = s->prev_tok;
283 }
284 sym_free(s);
285 s = ss;
286 }
287 *ptop = b;
288 }
289
290 static void weaken_symbol(Sym *sym)
291 {
292 sym->type.t |= VT_WEAK;
293 if (sym->c > 0) {
294 int esym_type;
295 ElfW(Sym) *esym;
296
297 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
298 esym_type = ELFW(ST_TYPE)(esym->st_info);
299 esym->st_info = ELFW(ST_INFO)(STB_WEAK, esym_type);
300 }
301 }
302
303 static void apply_visibility(Sym *sym, CType *type)
304 {
305 int vis = sym->type.t & VT_VIS_MASK;
306 int vis2 = type->t & VT_VIS_MASK;
307 if (vis == (STV_DEFAULT << VT_VIS_SHIFT))
308 vis = vis2;
309 else if (vis2 == (STV_DEFAULT << VT_VIS_SHIFT))
310 ;
311 else
312 vis = (vis < vis2) ? vis : vis2;
313 sym->type.t &= ~VT_VIS_MASK;
314 sym->type.t |= vis;
315
316 if (sym->c > 0) {
317 ElfW(Sym) *esym;
318
319 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
320 vis >>= VT_VIS_SHIFT;
321 esym->st_other = (esym->st_other & ~ELFW(ST_VISIBILITY)(-1)) | vis;
322 }
323 }
324
325 /* ------------------------------------------------------------------------- */
326
327 ST_FUNC void swap(int *p, int *q)
328 {
329 int t;
330 t = *p;
331 *p = *q;
332 *q = t;
333 }
334
335 static void vsetc(CType *type, int r, CValue *vc)
336 {
337 int v;
338
339 if (vtop >= vstack + (VSTACK_SIZE - 1))
340 tcc_error("memory full (vstack)");
341 /* cannot let cpu flags if other instruction are generated. Also
342 avoid leaving VT_JMP anywhere except on the top of the stack
343 because it would complicate the code generator. */
344 if (vtop >= vstack) {
345 v = vtop->r & VT_VALMASK;
346 if (v == VT_CMP || (v & ~1) == VT_JMP)
347 gv(RC_INT);
348 }
349 vtop++;
350 vtop->type = *type;
351 vtop->r = r;
352 vtop->r2 = VT_CONST;
353 vtop->c = *vc;
354 }
355
356 /* push constant of type "type" with useless value */
357 ST_FUNC void vpush(CType *type)
358 {
359 CValue cval;
360 vsetc(type, VT_CONST, &cval);
361 }
362
363 /* push integer constant */
364 ST_FUNC void vpushi(int v)
365 {
366 CValue cval;
367 cval.i = v;
368 vsetc(&int_type, VT_CONST, &cval);
369 }
370
371 /* push a pointer sized constant */
372 static void vpushs(addr_t v)
373 {
374 CValue cval;
375 cval.ptr_offset = v;
376 vsetc(&size_type, VT_CONST, &cval);
377 }
378
379 /* push arbitrary 64bit constant */
380 ST_FUNC void vpush64(int ty, unsigned long long v)
381 {
382 CValue cval;
383 CType ctype;
384 ctype.t = ty;
385 ctype.ref = NULL;
386 cval.ull = v;
387 vsetc(&ctype, VT_CONST, &cval);
388 }
389
390 /* push long long constant */
391 static inline void vpushll(long long v)
392 {
393 vpush64(VT_LLONG, v);
394 }
395
396 /* push a symbol value of TYPE */
397 static inline void vpushsym(CType *type, Sym *sym)
398 {
399 CValue cval;
400 cval.ptr_offset = 0;
401 vsetc(type, VT_CONST | VT_SYM, &cval);
402 vtop->sym = sym;
403 }
404
405 /* Return a static symbol pointing to a section */
406 ST_FUNC Sym *get_sym_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
407 {
408 int v;
409 Sym *sym;
410
411 v = anon_sym++;
412 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
413 sym->type.ref = type->ref;
414 sym->r = VT_CONST | VT_SYM;
415 put_extern_sym(sym, sec, offset, size);
416 return sym;
417 }
418
419 /* push a reference to a section offset by adding a dummy symbol */
420 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
421 {
422 vpushsym(type, get_sym_ref(type, sec, offset, size));
423 }
424
425 /* define a new external reference to a symbol 'v' of type 'u' */
426 ST_FUNC Sym *external_global_sym(int v, CType *type, int r)
427 {
428 Sym *s;
429
430 s = sym_find(v);
431 if (!s) {
432 /* push forward reference */
433 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
434 s->type.ref = type->ref;
435 s->r = r | VT_CONST | VT_SYM;
436 }
437 return s;
438 }
439
440 /* define a new external reference to a symbol 'v' with alternate asm
441 name 'asm_label' of type 'u'. 'asm_label' is equal to NULL if there
442 is no alternate name (most cases) */
443 static Sym *external_sym(int v, CType *type, int r, char *asm_label)
444 {
445 Sym *s;
446
447 s = sym_find(v);
448 if (!s) {
449 /* push forward reference */
450 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
451 s->asm_label = asm_label;
452 s->type.t |= VT_EXTERN;
453 } else if (s->type.ref == func_old_type.ref) {
454 s->type.ref = type->ref;
455 s->r = r | VT_CONST | VT_SYM;
456 s->type.t |= VT_EXTERN;
457 } else if (!is_compatible_types(&s->type, type)) {
458 tcc_error("incompatible types for redefinition of '%s'",
459 get_tok_str(v, NULL));
460 }
461 /* Merge some storage attributes. */
462 if (type->t & VT_WEAK)
463 weaken_symbol(s);
464
465 if (type->t & VT_VIS_MASK)
466 apply_visibility(s, type);
467
468 return s;
469 }
470
471 /* push a reference to global symbol v */
472 ST_FUNC void vpush_global_sym(CType *type, int v)
473 {
474 vpushsym(type, external_global_sym(v, type, 0));
475 }
476
477 ST_FUNC void vset(CType *type, int r, int v)
478 {
479 CValue cval;
480
481 cval.i = v;
482 vsetc(type, r, &cval);
483 }
484
485 static void vseti(int r, int v)
486 {
487 CType type;
488 type.t = VT_INT;
489 type.ref = 0;
490 vset(&type, r, v);
491 }
492
493 ST_FUNC void vswap(void)
494 {
495 SValue tmp;
496 /* cannot let cpu flags if other instruction are generated. Also
497 avoid leaving VT_JMP anywhere except on the top of the stack
498 because it would complicate the code generator. */
499 if (vtop >= vstack) {
500 int v = vtop->r & VT_VALMASK;
501 if (v == VT_CMP || (v & ~1) == VT_JMP)
502 gv(RC_INT);
503 }
504 tmp = vtop[0];
505 vtop[0] = vtop[-1];
506 vtop[-1] = tmp;
507
508 /* XXX: +2% overall speed possible with optimized memswap
509 *
510 * memswap(&vtop[0], &vtop[1], sizeof *vtop);
511 */
512 }
513
514 ST_FUNC void vpushv(SValue *v)
515 {
516 if (vtop >= vstack + (VSTACK_SIZE - 1))
517 tcc_error("memory full (vstack)");
518 vtop++;
519 *vtop = *v;
520 }
521
522 static void vdup(void)
523 {
524 vpushv(vtop);
525 }
526
527 /* save r to the memory stack, and mark it as being free */
528 ST_FUNC void save_reg(int r)
529 {
530 int l, saved, size, align;
531 SValue *p, sv;
532 CType *type;
533
534 /* modify all stack values */
535 saved = 0;
536 l = 0;
537 for(p=vstack;p<=vtop;p++) {
538 if ((p->r & VT_VALMASK) == r ||
539 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
540 /* must save value on stack if not already done */
541 if (!saved) {
542 /* NOTE: must reload 'r' because r might be equal to r2 */
543 r = p->r & VT_VALMASK;
544 /* store register in the stack */
545 type = &p->type;
546 if ((p->r & VT_LVAL) ||
547 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
548 #ifdef TCC_TARGET_X86_64
549 type = &char_pointer_type;
550 #else
551 type = &int_type;
552 #endif
553 size = type_size(type, &align);
554 loc = (loc - size) & -align;
555 sv.type.t = type->t;
556 sv.r = VT_LOCAL | VT_LVAL;
557 sv.c.ul = loc;
558 store(r, &sv);
559 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
560 /* x86 specific: need to pop fp register ST0 if saved */
561 if (r == TREG_ST0) {
562 o(0xd8dd); /* fstp %st(0) */
563 }
564 #endif
565 #ifndef TCC_TARGET_X86_64
566 /* special long long case */
567 if ((type->t & VT_BTYPE) == VT_LLONG) {
568 sv.c.ul += 4;
569 store(p->r2, &sv);
570 }
571 #endif
572 l = loc;
573 saved = 1;
574 }
575 /* mark that stack entry as being saved on the stack */
576 if (p->r & VT_LVAL) {
577 /* also clear the bounded flag because the
578 relocation address of the function was stored in
579 p->c.ul */
580 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
581 } else {
582 p->r = lvalue_type(p->type.t) | VT_LOCAL;
583 }
584 p->r2 = VT_CONST;
585 p->c.ul = l;
586 }
587 }
588 }
589
590 #ifdef TCC_TARGET_ARM
591 /* find a register of class 'rc2' with at most one reference on stack.
592 * If none, call get_reg(rc) */
593 ST_FUNC int get_reg_ex(int rc, int rc2)
594 {
595 int r;
596 SValue *p;
597
598 for(r=0;r<NB_REGS;r++) {
599 if (reg_classes[r] & rc2) {
600 int n;
601 n=0;
602 for(p = vstack; p <= vtop; p++) {
603 if ((p->r & VT_VALMASK) == r ||
604 (p->r2 & VT_VALMASK) == r)
605 n++;
606 }
607 if (n <= 1)
608 return r;
609 }
610 }
611 return get_reg(rc);
612 }
613 #endif
614
615 /* find a free register of class 'rc'. If none, save one register */
616 ST_FUNC int get_reg(int rc)
617 {
618 int r;
619 SValue *p;
620
621 /* find a free register */
622 for(r=0;r<NB_REGS;r++) {
623 if (reg_classes[r] & rc) {
624 for(p=vstack;p<=vtop;p++) {
625 if ((p->r & VT_VALMASK) == r ||
626 (p->r2 & VT_VALMASK) == r)
627 goto notfound;
628 }
629 return r;
630 }
631 notfound: ;
632 }
633
634 /* no register left : free the first one on the stack (VERY
635 IMPORTANT to start from the bottom to ensure that we don't
636 spill registers used in gen_opi()) */
637 for(p=vstack;p<=vtop;p++) {
638 /* look at second register (if long long) */
639 r = p->r2 & VT_VALMASK;
640 if (r < VT_CONST && (reg_classes[r] & rc))
641 goto save_found;
642 r = p->r & VT_VALMASK;
643 if (r < VT_CONST && (reg_classes[r] & rc)) {
644 save_found:
645 save_reg(r);
646 return r;
647 }
648 }
649 /* Should never comes here */
650 return -1;
651 }
652
653 /* save registers up to (vtop - n) stack entry */
654 ST_FUNC void save_regs(int n)
655 {
656 int r;
657 SValue *p, *p1;
658 p1 = vtop - n;
659 for(p = vstack;p <= p1; p++) {
660 r = p->r & VT_VALMASK;
661 if (r < VT_CONST) {
662 save_reg(r);
663 }
664 }
665 }
666
667 /* move register 's' (of type 't') to 'r', and flush previous value of r to memory
668 if needed */
669 static void move_reg(int r, int s, int t)
670 {
671 SValue sv;
672
673 if (r != s) {
674 save_reg(r);
675 sv.type.t = t;
676 sv.type.ref = NULL;
677 sv.r = s;
678 sv.c.ul = 0;
679 load(r, &sv);
680 }
681 }
682
683 /* get address of vtop (vtop MUST BE an lvalue) */
684 static void gaddrof(void)
685 {
686 if (vtop->r & VT_REF)
687 gv(RC_INT);
688 vtop->r &= ~VT_LVAL;
689 /* tricky: if saved lvalue, then we can go back to lvalue */
690 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
691 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
692
693
694 }
695
696 #ifdef CONFIG_TCC_BCHECK
697 /* generate lvalue bound code */
698 static void gbound(void)
699 {
700 int lval_type;
701 CType type1;
702
703 vtop->r &= ~VT_MUSTBOUND;
704 /* if lvalue, then use checking code before dereferencing */
705 if (vtop->r & VT_LVAL) {
706 /* if not VT_BOUNDED value, then make one */
707 if (!(vtop->r & VT_BOUNDED)) {
708 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
709 /* must save type because we must set it to int to get pointer */
710 type1 = vtop->type;
711 vtop->type.t = VT_INT;
712 gaddrof();
713 vpushi(0);
714 gen_bounded_ptr_add();
715 vtop->r |= lval_type;
716 vtop->type = type1;
717 }
718 /* then check for dereferencing */
719 gen_bounded_ptr_deref();
720 }
721 }
722 #endif
723
724 /* store vtop a register belonging to class 'rc'. lvalues are
725 converted to values. Cannot be used if cannot be converted to
726 register value (such as structures). */
727 ST_FUNC int gv(int rc)
728 {
729 int r, bit_pos, bit_size, size, align, i;
730 int rc2;
731
732 /* NOTE: get_reg can modify vstack[] */
733 if (vtop->type.t & VT_BITFIELD) {
734 CType type;
735 int bits = 32;
736 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
737 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
738 /* remove bit field info to avoid loops */
739 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
740 /* cast to int to propagate signedness in following ops */
741 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
742 type.t = VT_LLONG;
743 bits = 64;
744 } else
745 type.t = VT_INT;
746 if((vtop->type.t & VT_UNSIGNED) ||
747 (vtop->type.t & VT_BTYPE) == VT_BOOL)
748 type.t |= VT_UNSIGNED;
749 gen_cast(&type);
750 /* generate shifts */
751 vpushi(bits - (bit_pos + bit_size));
752 gen_op(TOK_SHL);
753 vpushi(bits - bit_size);
754 /* NOTE: transformed to SHR if unsigned */
755 gen_op(TOK_SAR);
756 r = gv(rc);
757 } else {
758 if (is_float(vtop->type.t) &&
759 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
760 Sym *sym;
761 int *ptr;
762 unsigned long offset;
763 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
764 CValue check;
765 #endif
766
767 /* XXX: unify with initializers handling ? */
768 /* CPUs usually cannot use float constants, so we store them
769 generically in data segment */
770 size = type_size(&vtop->type, &align);
771 offset = (data_section->data_offset + align - 1) & -align;
772 data_section->data_offset = offset;
773 /* XXX: not portable yet */
774 #if defined(__i386__) || defined(__x86_64__)
775 /* Zero pad x87 tenbyte long doubles */
776 if (size == LDOUBLE_SIZE) {
777 vtop->c.tab[2] &= 0xffff;
778 #if LDOUBLE_SIZE == 16
779 vtop->c.tab[3] = 0;
780 #endif
781 }
782 #endif
783 ptr = section_ptr_add(data_section, size);
784 size = size >> 2;
785 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
786 check.d = 1;
787 if(check.tab[0])
788 for(i=0;i<size;i++)
789 ptr[i] = vtop->c.tab[size-1-i];
790 else
791 #endif
792 for(i=0;i<size;i++)
793 ptr[i] = vtop->c.tab[i];
794 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
795 vtop->r |= VT_LVAL | VT_SYM;
796 vtop->sym = sym;
797 vtop->c.ptr_offset = 0;
798 }
799 #ifdef CONFIG_TCC_BCHECK
800 if (vtop->r & VT_MUSTBOUND)
801 gbound();
802 #endif
803
804 r = vtop->r & VT_VALMASK;
805 rc2 = (rc & RC_FLOAT) ? RC_FLOAT : RC_INT;
806 if (rc == RC_IRET)
807 rc2 = RC_LRET;
808 #ifdef TCC_TARGET_X86_64
809 else if (rc == RC_FRET)
810 rc2 = RC_QRET;
811 #endif
812
813 /* need to reload if:
814 - constant
815 - lvalue (need to dereference pointer)
816 - already a register, but not in the right class */
817 if (r >= VT_CONST
818 || (vtop->r & VT_LVAL)
819 || !(reg_classes[r] & rc)
820 #ifdef TCC_TARGET_X86_64
821 || ((vtop->type.t & VT_BTYPE) == VT_QLONG && !(reg_classes[vtop->r2] & rc2))
822 || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT && !(reg_classes[vtop->r2] & rc2))
823 #else
824 || ((vtop->type.t & VT_BTYPE) == VT_LLONG && !(reg_classes[vtop->r2] & rc2))
825 #endif
826 )
827 {
828 r = get_reg(rc);
829 #ifdef TCC_TARGET_X86_64
830 if (((vtop->type.t & VT_BTYPE) == VT_QLONG) || ((vtop->type.t & VT_BTYPE) == VT_QFLOAT)) {
831 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
832 #else
833 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
834 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
835 unsigned long long ll;
836 #endif
837 int r2, original_type;
838 original_type = vtop->type.t;
839 /* two register type load : expand to two words
840 temporarily */
841 #ifndef TCC_TARGET_X86_64
842 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
843 /* load constant */
844 ll = vtop->c.ull;
845 vtop->c.ui = ll; /* first word */
846 load(r, vtop);
847 vtop->r = r; /* save register value */
848 vpushi(ll >> 32); /* second word */
849 } else
850 #endif
851 if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
852 (vtop->r & VT_LVAL)) {
853 /* We do not want to modifier the long long
854 pointer here, so the safest (and less
855 efficient) is to save all the other registers
856 in the stack. XXX: totally inefficient. */
857 save_regs(1);
858 /* load from memory */
859 vtop->type.t = load_type;
860 load(r, vtop);
861 vdup();
862 vtop[-1].r = r; /* save register value */
863 /* increment pointer to get second word */
864 vtop->type.t = addr_type;
865 gaddrof();
866 vpushi(load_size);
867 gen_op('+');
868 vtop->r |= VT_LVAL;
869 vtop->type.t = load_type;
870 } else {
871 /* move registers */
872 load(r, vtop);
873 vdup();
874 vtop[-1].r = r; /* save register value */
875 vtop->r = vtop[-1].r2;
876 }
877 /* Allocate second register. Here we rely on the fact that
878 get_reg() tries first to free r2 of an SValue. */
879 r2 = get_reg(rc2);
880 load(r2, vtop);
881 vpop();
882 /* write second register */
883 vtop->r2 = r2;
884 vtop->type.t = original_type;
885 } else if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
886 int t1, t;
887 /* lvalue of scalar type : need to use lvalue type
888 because of possible cast */
889 t = vtop->type.t;
890 t1 = t;
891 /* compute memory access type */
892 if (vtop->r & VT_REF)
893 #ifdef TCC_TARGET_X86_64
894 t = VT_PTR;
895 #else
896 t = VT_INT;
897 #endif
898 else if (vtop->r & VT_LVAL_BYTE)
899 t = VT_BYTE;
900 else if (vtop->r & VT_LVAL_SHORT)
901 t = VT_SHORT;
902 if (vtop->r & VT_LVAL_UNSIGNED)
903 t |= VT_UNSIGNED;
904 vtop->type.t = t;
905 load(r, vtop);
906 /* restore wanted type */
907 vtop->type.t = t1;
908 } else {
909 /* one register type load */
910 load(r, vtop);
911 }
912 }
913 vtop->r = r;
914 #ifdef TCC_TARGET_C67
915 /* uses register pairs for doubles */
916 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
917 vtop->r2 = r+1;
918 #endif
919 }
920 return r;
921 }
922
923 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
924 ST_FUNC void gv2(int rc1, int rc2)
925 {
926 int v;
927
928 /* generate more generic register first. But VT_JMP or VT_CMP
929 values must be generated first in all cases to avoid possible
930 reload errors */
931 v = vtop[0].r & VT_VALMASK;
932 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
933 vswap();
934 gv(rc1);
935 vswap();
936 gv(rc2);
937 /* test if reload is needed for first register */
938 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
939 vswap();
940 gv(rc1);
941 vswap();
942 }
943 } else {
944 gv(rc2);
945 vswap();
946 gv(rc1);
947 vswap();
948 /* test if reload is needed for first register */
949 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
950 gv(rc2);
951 }
952 }
953 }
954
955 /* wrapper around RC_FRET to return a register by type */
956 static int rc_fret(int t)
957 {
958 #ifdef TCC_TARGET_X86_64
959 if (t == VT_LDOUBLE) {
960 return RC_ST0;
961 }
962 #endif
963 return RC_FRET;
964 }
965
966 /* wrapper around REG_FRET to return a register by type */
967 static int reg_fret(int t)
968 {
969 #ifdef TCC_TARGET_X86_64
970 if (t == VT_LDOUBLE) {
971 return TREG_ST0;
972 }
973 #endif
974 return REG_FRET;
975 }
976
977 /* expand long long on stack in two int registers */
978 static void lexpand(void)
979 {
980 int u;
981
982 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
983 gv(RC_INT);
984 vdup();
985 vtop[0].r = vtop[-1].r2;
986 vtop[0].r2 = VT_CONST;
987 vtop[-1].r2 = VT_CONST;
988 vtop[0].type.t = VT_INT | u;
989 vtop[-1].type.t = VT_INT | u;
990 }
991
992 #ifdef TCC_TARGET_ARM
993 /* expand long long on stack */
994 ST_FUNC void lexpand_nr(void)
995 {
996 int u,v;
997
998 u = vtop->type.t & (VT_DEFSIGN | VT_UNSIGNED);
999 vdup();
1000 vtop->r2 = VT_CONST;
1001 vtop->type.t = VT_INT | u;
1002 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
1003 if (v == VT_CONST) {
1004 vtop[-1].c.ui = vtop->c.ull;
1005 vtop->c.ui = vtop->c.ull >> 32;
1006 vtop->r = VT_CONST;
1007 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1008 vtop->c.ui += 4;
1009 vtop->r = vtop[-1].r;
1010 } else if (v > VT_CONST) {
1011 vtop--;
1012 lexpand();
1013 } else
1014 vtop->r = vtop[-1].r2;
1015 vtop[-1].r2 = VT_CONST;
1016 vtop[-1].type.t = VT_INT | u;
1017 }
1018 #endif
1019
1020 /* build a long long from two ints */
1021 static void lbuild(int t)
1022 {
1023 gv2(RC_INT, RC_INT);
1024 vtop[-1].r2 = vtop[0].r;
1025 vtop[-1].type.t = t;
1026 vpop();
1027 }
1028
1029 /* rotate n first stack elements to the bottom
1030 I1 ... In -> I2 ... In I1 [top is right]
1031 */
1032 ST_FUNC void vrotb(int n)
1033 {
1034 int i;
1035 SValue tmp;
1036
1037 tmp = vtop[-n + 1];
1038 for(i=-n+1;i!=0;i++)
1039 vtop[i] = vtop[i+1];
1040 vtop[0] = tmp;
1041 }
1042
1043 /* rotate the n elements before entry e towards the top
1044 I1 ... In ... -> In I1 ... I(n-1) ... [top is right]
1045 */
1046 ST_FUNC void vrote(SValue *e, int n)
1047 {
1048 int i;
1049 SValue tmp;
1050
1051 tmp = *e;
1052 for(i = 0;i < n - 1; i++)
1053 e[-i] = e[-i - 1];
1054 e[-n + 1] = tmp;
1055 }
1056
1057 /* rotate n first stack elements to the top
1058 I1 ... In -> In I1 ... I(n-1) [top is right]
1059 */
1060 ST_FUNC void vrott(int n)
1061 {
1062 vrote(vtop, n);
1063 }
1064
1065 /* pop stack value */
1066 ST_FUNC void vpop(void)
1067 {
1068 int v;
1069 v = vtop->r & VT_VALMASK;
1070 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1071 /* for x86, we need to pop the FP stack */
1072 if (v == TREG_ST0 && !nocode_wanted) {
1073 o(0xd8dd); /* fstp %st(0) */
1074 } else
1075 #endif
1076 if (v == VT_JMP || v == VT_JMPI) {
1077 /* need to put correct jump if && or || without test */
1078 gsym(vtop->c.ul);
1079 }
1080 vtop--;
1081 }
1082
1083 /* convert stack entry to register and duplicate its value in another
1084 register */
1085 static void gv_dup(void)
1086 {
1087 int rc, t, r, r1;
1088 SValue sv;
1089
1090 t = vtop->type.t;
1091 if ((t & VT_BTYPE) == VT_LLONG) {
1092 lexpand();
1093 gv_dup();
1094 vswap();
1095 vrotb(3);
1096 gv_dup();
1097 vrotb(4);
1098 /* stack: H L L1 H1 */
1099 lbuild(t);
1100 vrotb(3);
1101 vrotb(3);
1102 vswap();
1103 lbuild(t);
1104 vswap();
1105 } else {
1106 /* duplicate value */
1107 rc = RC_INT;
1108 sv.type.t = VT_INT;
1109 if (is_float(t)) {
1110 rc = RC_FLOAT;
1111 #ifdef TCC_TARGET_X86_64
1112 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1113 rc = RC_ST0;
1114 }
1115 #endif
1116 sv.type.t = t;
1117 }
1118 r = gv(rc);
1119 r1 = get_reg(rc);
1120 sv.r = r;
1121 sv.c.ul = 0;
1122 load(r1, &sv); /* move r to r1 */
1123 vdup();
1124 /* duplicates value */
1125 if (r != r1)
1126 vtop->r = r1;
1127 }
1128 }
1129
1130 /* Generate value test
1131 *
1132 * Generate a test for any value (jump, comparison and integers) */
1133 ST_FUNC int gvtst(int inv, int t)
1134 {
1135 int v = vtop->r & VT_VALMASK;
1136 if (v != VT_CMP && v != VT_JMP && v != VT_JMPI) {
1137 vpushi(0);
1138 gen_op(TOK_NE);
1139 }
1140 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1141 /* constant jmp optimization */
1142 if ((vtop->c.i != 0) != inv)
1143 t = gjmp(t);
1144 vtop--;
1145 return t;
1146 }
1147 return gtst(inv, t);
1148 }
1149
1150 #ifndef TCC_TARGET_X86_64
1151 /* generate CPU independent (unsigned) long long operations */
1152 static void gen_opl(int op)
1153 {
1154 int t, a, b, op1, c, i;
1155 int func;
1156 unsigned short reg_iret = REG_IRET;
1157 unsigned short reg_lret = REG_LRET;
1158 SValue tmp;
1159
1160 switch(op) {
1161 case '/':
1162 case TOK_PDIV:
1163 func = TOK___divdi3;
1164 goto gen_func;
1165 case TOK_UDIV:
1166 func = TOK___udivdi3;
1167 goto gen_func;
1168 case '%':
1169 func = TOK___moddi3;
1170 goto gen_mod_func;
1171 case TOK_UMOD:
1172 func = TOK___umoddi3;
1173 gen_mod_func:
1174 #ifdef TCC_ARM_EABI
1175 reg_iret = TREG_R2;
1176 reg_lret = TREG_R3;
1177 #endif
1178 gen_func:
1179 /* call generic long long function */
1180 vpush_global_sym(&func_old_type, func);
1181 vrott(3);
1182 gfunc_call(2);
1183 vpushi(0);
1184 vtop->r = reg_iret;
1185 vtop->r2 = reg_lret;
1186 break;
1187 case '^':
1188 case '&':
1189 case '|':
1190 case '*':
1191 case '+':
1192 case '-':
1193 t = vtop->type.t;
1194 vswap();
1195 lexpand();
1196 vrotb(3);
1197 lexpand();
1198 /* stack: L1 H1 L2 H2 */
1199 tmp = vtop[0];
1200 vtop[0] = vtop[-3];
1201 vtop[-3] = tmp;
1202 tmp = vtop[-2];
1203 vtop[-2] = vtop[-3];
1204 vtop[-3] = tmp;
1205 vswap();
1206 /* stack: H1 H2 L1 L2 */
1207 if (op == '*') {
1208 vpushv(vtop - 1);
1209 vpushv(vtop - 1);
1210 gen_op(TOK_UMULL);
1211 lexpand();
1212 /* stack: H1 H2 L1 L2 ML MH */
1213 for(i=0;i<4;i++)
1214 vrotb(6);
1215 /* stack: ML MH H1 H2 L1 L2 */
1216 tmp = vtop[0];
1217 vtop[0] = vtop[-2];
1218 vtop[-2] = tmp;
1219 /* stack: ML MH H1 L2 H2 L1 */
1220 gen_op('*');
1221 vrotb(3);
1222 vrotb(3);
1223 gen_op('*');
1224 /* stack: ML MH M1 M2 */
1225 gen_op('+');
1226 gen_op('+');
1227 } else if (op == '+' || op == '-') {
1228 /* XXX: add non carry method too (for MIPS or alpha) */
1229 if (op == '+')
1230 op1 = TOK_ADDC1;
1231 else
1232 op1 = TOK_SUBC1;
1233 gen_op(op1);
1234 /* stack: H1 H2 (L1 op L2) */
1235 vrotb(3);
1236 vrotb(3);
1237 gen_op(op1 + 1); /* TOK_xxxC2 */
1238 } else {
1239 gen_op(op);
1240 /* stack: H1 H2 (L1 op L2) */
1241 vrotb(3);
1242 vrotb(3);
1243 /* stack: (L1 op L2) H1 H2 */
1244 gen_op(op);
1245 /* stack: (L1 op L2) (H1 op H2) */
1246 }
1247 /* stack: L H */
1248 lbuild(t);
1249 break;
1250 case TOK_SAR:
1251 case TOK_SHR:
1252 case TOK_SHL:
1253 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1254 t = vtop[-1].type.t;
1255 vswap();
1256 lexpand();
1257 vrotb(3);
1258 /* stack: L H shift */
1259 c = (int)vtop->c.i;
1260 /* constant: simpler */
1261 /* NOTE: all comments are for SHL. the other cases are
1262 done by swaping words */
1263 vpop();
1264 if (op != TOK_SHL)
1265 vswap();
1266 if (c >= 32) {
1267 /* stack: L H */
1268 vpop();
1269 if (c > 32) {
1270 vpushi(c - 32);
1271 gen_op(op);
1272 }
1273 if (op != TOK_SAR) {
1274 vpushi(0);
1275 } else {
1276 gv_dup();
1277 vpushi(31);
1278 gen_op(TOK_SAR);
1279 }
1280 vswap();
1281 } else {
1282 vswap();
1283 gv_dup();
1284 /* stack: H L L */
1285 vpushi(c);
1286 gen_op(op);
1287 vswap();
1288 vpushi(32 - c);
1289 if (op == TOK_SHL)
1290 gen_op(TOK_SHR);
1291 else
1292 gen_op(TOK_SHL);
1293 vrotb(3);
1294 /* stack: L L H */
1295 vpushi(c);
1296 if (op == TOK_SHL)
1297 gen_op(TOK_SHL);
1298 else
1299 gen_op(TOK_SHR);
1300 gen_op('|');
1301 }
1302 if (op != TOK_SHL)
1303 vswap();
1304 lbuild(t);
1305 } else {
1306 /* XXX: should provide a faster fallback on x86 ? */
1307 switch(op) {
1308 case TOK_SAR:
1309 func = TOK___ashrdi3;
1310 goto gen_func;
1311 case TOK_SHR:
1312 func = TOK___lshrdi3;
1313 goto gen_func;
1314 case TOK_SHL:
1315 func = TOK___ashldi3;
1316 goto gen_func;
1317 }
1318 }
1319 break;
1320 default:
1321 /* compare operations */
1322 t = vtop->type.t;
1323 vswap();
1324 lexpand();
1325 vrotb(3);
1326 lexpand();
1327 /* stack: L1 H1 L2 H2 */
1328 tmp = vtop[-1];
1329 vtop[-1] = vtop[-2];
1330 vtop[-2] = tmp;
1331 /* stack: L1 L2 H1 H2 */
1332 /* compare high */
1333 op1 = op;
1334 /* when values are equal, we need to compare low words. since
1335 the jump is inverted, we invert the test too. */
1336 if (op1 == TOK_LT)
1337 op1 = TOK_LE;
1338 else if (op1 == TOK_GT)
1339 op1 = TOK_GE;
1340 else if (op1 == TOK_ULT)
1341 op1 = TOK_ULE;
1342 else if (op1 == TOK_UGT)
1343 op1 = TOK_UGE;
1344 a = 0;
1345 b = 0;
1346 gen_op(op1);
1347 if (op1 != TOK_NE) {
1348 a = gvtst(1, 0);
1349 }
1350 if (op != TOK_EQ) {
1351 /* generate non equal test */
1352 /* XXX: NOT PORTABLE yet */
1353 if (a == 0) {
1354 b = gvtst(0, 0);
1355 } else {
1356 #if defined(TCC_TARGET_I386)
1357 b = psym(0x850f, 0);
1358 #elif defined(TCC_TARGET_ARM)
1359 b = ind;
1360 o(0x1A000000 | encbranch(ind, 0, 1));
1361 #elif defined(TCC_TARGET_C67)
1362 tcc_error("not implemented");
1363 #else
1364 #error not supported
1365 #endif
1366 }
1367 }
1368 /* compare low. Always unsigned */
1369 op1 = op;
1370 if (op1 == TOK_LT)
1371 op1 = TOK_ULT;
1372 else if (op1 == TOK_LE)
1373 op1 = TOK_ULE;
1374 else if (op1 == TOK_GT)
1375 op1 = TOK_UGT;
1376 else if (op1 == TOK_GE)
1377 op1 = TOK_UGE;
1378 gen_op(op1);
1379 a = gvtst(1, a);
1380 gsym(b);
1381 vseti(VT_JMPI, a);
1382 break;
1383 }
1384 }
1385 #endif
1386
1387 /* handle integer constant optimizations and various machine
1388 independent opt */
1389 static void gen_opic(int op)
1390 {
1391 int c1, c2, t1, t2, n;
1392 SValue *v1, *v2;
1393 long long l1, l2;
1394 typedef unsigned long long U;
1395
1396 v1 = vtop - 1;
1397 v2 = vtop;
1398 t1 = v1->type.t & VT_BTYPE;
1399 t2 = v2->type.t & VT_BTYPE;
1400
1401 if (t1 == VT_LLONG)
1402 l1 = v1->c.ll;
1403 else if (v1->type.t & VT_UNSIGNED)
1404 l1 = v1->c.ui;
1405 else
1406 l1 = v1->c.i;
1407
1408 if (t2 == VT_LLONG)
1409 l2 = v2->c.ll;
1410 else if (v2->type.t & VT_UNSIGNED)
1411 l2 = v2->c.ui;
1412 else
1413 l2 = v2->c.i;
1414
1415 /* currently, we cannot do computations with forward symbols */
1416 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1417 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1418 if (c1 && c2) {
1419 switch(op) {
1420 case '+': l1 += l2; break;
1421 case '-': l1 -= l2; break;
1422 case '&': l1 &= l2; break;
1423 case '^': l1 ^= l2; break;
1424 case '|': l1 |= l2; break;
1425 case '*': l1 *= l2; break;
1426
1427 case TOK_PDIV:
1428 case '/':
1429 case '%':
1430 case TOK_UDIV:
1431 case TOK_UMOD:
1432 /* if division by zero, generate explicit division */
1433 if (l2 == 0) {
1434 if (const_wanted)
1435 tcc_error("division by zero in constant");
1436 goto general_case;
1437 }
1438 switch(op) {
1439 default: l1 /= l2; break;
1440 case '%': l1 %= l2; break;
1441 case TOK_UDIV: l1 = (U)l1 / l2; break;
1442 case TOK_UMOD: l1 = (U)l1 % l2; break;
1443 }
1444 break;
1445 case TOK_SHL: l1 <<= l2; break;
1446 case TOK_SHR: l1 = (U)l1 >> l2; break;
1447 case TOK_SAR: l1 >>= l2; break;
1448 /* tests */
1449 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1450 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1451 case TOK_EQ: l1 = l1 == l2; break;
1452 case TOK_NE: l1 = l1 != l2; break;
1453 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1454 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1455 case TOK_LT: l1 = l1 < l2; break;
1456 case TOK_GE: l1 = l1 >= l2; break;
1457 case TOK_LE: l1 = l1 <= l2; break;
1458 case TOK_GT: l1 = l1 > l2; break;
1459 /* logical */
1460 case TOK_LAND: l1 = l1 && l2; break;
1461 case TOK_LOR: l1 = l1 || l2; break;
1462 default:
1463 goto general_case;
1464 }
1465 v1->c.ll = l1;
1466 vtop--;
1467 } else {
1468 /* if commutative ops, put c2 as constant */
1469 if (c1 && (op == '+' || op == '&' || op == '^' ||
1470 op == '|' || op == '*')) {
1471 vswap();
1472 c2 = c1; //c = c1, c1 = c2, c2 = c;
1473 l2 = l1; //l = l1, l1 = l2, l2 = l;
1474 }
1475 /* Filter out NOP operations like x*1, x-0, x&-1... */
1476 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1477 op == TOK_PDIV) &&
1478 l2 == 1) ||
1479 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1480 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1481 l2 == 0) ||
1482 (op == '&' &&
1483 l2 == -1))) {
1484 /* nothing to do */
1485 vtop--;
1486 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1487 /* try to use shifts instead of muls or divs */
1488 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1489 n = -1;
1490 while (l2) {
1491 l2 >>= 1;
1492 n++;
1493 }
1494 vtop->c.ll = n;
1495 if (op == '*')
1496 op = TOK_SHL;
1497 else if (op == TOK_PDIV)
1498 op = TOK_SAR;
1499 else
1500 op = TOK_SHR;
1501 }
1502 goto general_case;
1503 } else if (c2 && (op == '+' || op == '-') &&
1504 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM))
1505 || (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1506 /* symbol + constant case */
1507 if (op == '-')
1508 l2 = -l2;
1509 vtop--;
1510 vtop->c.ll += l2;
1511 } else {
1512 general_case:
1513 if (!nocode_wanted) {
1514 /* call low level op generator */
1515 if (t1 == VT_LLONG || t2 == VT_LLONG)
1516 gen_opl(op);
1517 else
1518 gen_opi(op);
1519 } else {
1520 vtop--;
1521 }
1522 }
1523 }
1524 }
1525
1526 /* generate a floating point operation with constant propagation */
1527 static void gen_opif(int op)
1528 {
1529 int c1, c2;
1530 SValue *v1, *v2;
1531 long double f1, f2;
1532
1533 v1 = vtop - 1;
1534 v2 = vtop;
1535 /* currently, we cannot do computations with forward symbols */
1536 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1537 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1538 if (c1 && c2) {
1539 if (v1->type.t == VT_FLOAT) {
1540 f1 = v1->c.f;
1541 f2 = v2->c.f;
1542 } else if (v1->type.t == VT_DOUBLE) {
1543 f1 = v1->c.d;
1544 f2 = v2->c.d;
1545 } else {
1546 f1 = v1->c.ld;
1547 f2 = v2->c.ld;
1548 }
1549
1550 /* NOTE: we only do constant propagation if finite number (not
1551 NaN or infinity) (ANSI spec) */
1552 if (!ieee_finite(f1) || !ieee_finite(f2))
1553 goto general_case;
1554
1555 switch(op) {
1556 case '+': f1 += f2; break;
1557 case '-': f1 -= f2; break;
1558 case '*': f1 *= f2; break;
1559 case '/':
1560 if (f2 == 0.0) {
1561 if (const_wanted)
1562 tcc_error("division by zero in constant");
1563 goto general_case;
1564 }
1565 f1 /= f2;
1566 break;
1567 /* XXX: also handles tests ? */
1568 default:
1569 goto general_case;
1570 }
1571 /* XXX: overflow test ? */
1572 if (v1->type.t == VT_FLOAT) {
1573 v1->c.f = f1;
1574 } else if (v1->type.t == VT_DOUBLE) {
1575 v1->c.d = f1;
1576 } else {
1577 v1->c.ld = f1;
1578 }
1579 vtop--;
1580 } else {
1581 general_case:
1582 if (!nocode_wanted) {
1583 gen_opf(op);
1584 } else {
1585 vtop--;
1586 }
1587 }
1588 }
1589
1590 static int pointed_size(CType *type)
1591 {
1592 int align;
1593 return type_size(pointed_type(type), &align);
1594 }
1595
1596 static void vla_runtime_pointed_size(CType *type)
1597 {
1598 int align;
1599 vla_runtime_type_size(pointed_type(type), &align);
1600 }
1601
1602 static inline int is_null_pointer(SValue *p)
1603 {
1604 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1605 return 0;
1606 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1607 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0) ||
1608 ((p->type.t & VT_BTYPE) == VT_PTR && p->c.ptr_offset == 0);
1609 }
1610
1611 static inline int is_integer_btype(int bt)
1612 {
1613 return (bt == VT_BYTE || bt == VT_SHORT ||
1614 bt == VT_INT || bt == VT_LLONG);
1615 }
1616
1617 /* check types for comparison or subtraction of pointers */
1618 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1619 {
1620 CType *type1, *type2, tmp_type1, tmp_type2;
1621 int bt1, bt2;
1622
1623 /* null pointers are accepted for all comparisons as gcc */
1624 if (is_null_pointer(p1) || is_null_pointer(p2))
1625 return;
1626 type1 = &p1->type;
1627 type2 = &p2->type;
1628 bt1 = type1->t & VT_BTYPE;
1629 bt2 = type2->t & VT_BTYPE;
1630 /* accept comparison between pointer and integer with a warning */
1631 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1632 if (op != TOK_LOR && op != TOK_LAND )
1633 tcc_warning("comparison between pointer and integer");
1634 return;
1635 }
1636
1637 /* both must be pointers or implicit function pointers */
1638 if (bt1 == VT_PTR) {
1639 type1 = pointed_type(type1);
1640 } else if (bt1 != VT_FUNC)
1641 goto invalid_operands;
1642
1643 if (bt2 == VT_PTR) {
1644 type2 = pointed_type(type2);
1645 } else if (bt2 != VT_FUNC) {
1646 invalid_operands:
1647 tcc_error("invalid operands to binary %s", get_tok_str(op, NULL));
1648 }
1649 if ((type1->t & VT_BTYPE) == VT_VOID ||
1650 (type2->t & VT_BTYPE) == VT_VOID)
1651 return;
1652 tmp_type1 = *type1;
1653 tmp_type2 = *type2;
1654 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1655 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1656 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1657 /* gcc-like error if '-' is used */
1658 if (op == '-')
1659 goto invalid_operands;
1660 else
1661 tcc_warning("comparison of distinct pointer types lacks a cast");
1662 }
1663 }
1664
1665 /* generic gen_op: handles types problems */
1666 ST_FUNC void gen_op(int op)
1667 {
1668 int u, t1, t2, bt1, bt2, t;
1669 CType type1;
1670
1671 t1 = vtop[-1].type.t;
1672 t2 = vtop[0].type.t;
1673 bt1 = t1 & VT_BTYPE;
1674 bt2 = t2 & VT_BTYPE;
1675
1676 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1677 /* at least one operand is a pointer */
1678 /* relationnal op: must be both pointers */
1679 if (op >= TOK_ULT && op <= TOK_LOR) {
1680 check_comparison_pointer_types(vtop - 1, vtop, op);
1681 /* pointers are handled are unsigned */
1682 #ifdef TCC_TARGET_X86_64
1683 t = VT_LLONG | VT_UNSIGNED;
1684 #else
1685 t = VT_INT | VT_UNSIGNED;
1686 #endif
1687 goto std_op;
1688 }
1689 /* if both pointers, then it must be the '-' op */
1690 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1691 if (op != '-')
1692 tcc_error("cannot use pointers here");
1693 check_comparison_pointer_types(vtop - 1, vtop, op);
1694 /* XXX: check that types are compatible */
1695 if (vtop[-1].type.t & VT_VLA) {
1696 vla_runtime_pointed_size(&vtop[-1].type);
1697 } else {
1698 vpushi(pointed_size(&vtop[-1].type));
1699 }
1700 vrott(3);
1701 gen_opic(op);
1702 /* set to integer type */
1703 #ifdef TCC_TARGET_X86_64
1704 vtop->type.t = VT_LLONG;
1705 #else
1706 vtop->type.t = VT_INT;
1707 #endif
1708 vswap();
1709 gen_op(TOK_PDIV);
1710 } else {
1711 /* exactly one pointer : must be '+' or '-'. */
1712 if (op != '-' && op != '+')
1713 tcc_error("cannot use pointers here");
1714 /* Put pointer as first operand */
1715 if (bt2 == VT_PTR) {
1716 vswap();
1717 swap(&t1, &t2);
1718 }
1719 type1 = vtop[-1].type;
1720 type1.t &= ~VT_ARRAY;
1721 if (vtop[-1].type.t & VT_VLA)
1722 vla_runtime_pointed_size(&vtop[-1].type);
1723 else {
1724 u = pointed_size(&vtop[-1].type);
1725 if (u < 0)
1726 tcc_error("unknown array element size");
1727 #ifdef TCC_TARGET_X86_64
1728 vpushll(u);
1729 #else
1730 /* XXX: cast to int ? (long long case) */
1731 vpushi(u);
1732 #endif
1733 }
1734 gen_op('*');
1735 #ifdef CONFIG_TCC_BCHECK
1736 /* if evaluating constant expression, no code should be
1737 generated, so no bound check */
1738 if (tcc_state->do_bounds_check && !const_wanted) {
1739 /* if bounded pointers, we generate a special code to
1740 test bounds */
1741 if (op == '-') {
1742 vpushi(0);
1743 vswap();
1744 gen_op('-');
1745 }
1746 gen_bounded_ptr_add();
1747 } else
1748 #endif
1749 {
1750 gen_opic(op);
1751 }
1752 /* put again type if gen_opic() swaped operands */
1753 vtop->type = type1;
1754 }
1755 } else if (is_float(bt1) || is_float(bt2)) {
1756 /* compute bigger type and do implicit casts */
1757 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1758 t = VT_LDOUBLE;
1759 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1760 t = VT_DOUBLE;
1761 } else {
1762 t = VT_FLOAT;
1763 }
1764 /* floats can only be used for a few operations */
1765 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1766 (op < TOK_ULT || op > TOK_GT))
1767 tcc_error("invalid operands for binary operation");
1768 goto std_op;
1769 } else if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL) {
1770 t = bt1 == VT_LLONG ? VT_LLONG : VT_INT;
1771 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (t | VT_UNSIGNED))
1772 t |= VT_UNSIGNED;
1773 goto std_op;
1774 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1775 /* cast to biggest op */
1776 t = VT_LLONG;
1777 /* convert to unsigned if it does not fit in a long long */
1778 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1779 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1780 t |= VT_UNSIGNED;
1781 goto std_op;
1782 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
1783 tcc_error("comparison of struct");
1784 } else {
1785 /* integer operations */
1786 t = VT_INT;
1787 /* convert to unsigned if it does not fit in an integer */
1788 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1789 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1790 t |= VT_UNSIGNED;
1791 std_op:
1792 /* XXX: currently, some unsigned operations are explicit, so
1793 we modify them here */
1794 if (t & VT_UNSIGNED) {
1795 if (op == TOK_SAR)
1796 op = TOK_SHR;
1797 else if (op == '/')
1798 op = TOK_UDIV;
1799 else if (op == '%')
1800 op = TOK_UMOD;
1801 else if (op == TOK_LT)
1802 op = TOK_ULT;
1803 else if (op == TOK_GT)
1804 op = TOK_UGT;
1805 else if (op == TOK_LE)
1806 op = TOK_ULE;
1807 else if (op == TOK_GE)
1808 op = TOK_UGE;
1809 }
1810 vswap();
1811 type1.t = t;
1812 gen_cast(&type1);
1813 vswap();
1814 /* special case for shifts and long long: we keep the shift as
1815 an integer */
1816 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1817 type1.t = VT_INT;
1818 gen_cast(&type1);
1819 if (is_float(t))
1820 gen_opif(op);
1821 else
1822 gen_opic(op);
1823 if (op >= TOK_ULT && op <= TOK_GT) {
1824 /* relationnal op: the result is an int */
1825 vtop->type.t = VT_INT;
1826 } else {
1827 vtop->type.t = t;
1828 }
1829 }
1830 }
1831
1832 #ifndef TCC_TARGET_ARM
1833 /* generic itof for unsigned long long case */
1834 static void gen_cvt_itof1(int t)
1835 {
1836 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1837 (VT_LLONG | VT_UNSIGNED)) {
1838
1839 if (t == VT_FLOAT)
1840 vpush_global_sym(&func_old_type, TOK___floatundisf);
1841 #if LDOUBLE_SIZE != 8
1842 else if (t == VT_LDOUBLE)
1843 vpush_global_sym(&func_old_type, TOK___floatundixf);
1844 #endif
1845 else
1846 vpush_global_sym(&func_old_type, TOK___floatundidf);
1847 vrott(2);
1848 gfunc_call(1);
1849 vpushi(0);
1850 vtop->r = reg_fret(t);
1851 } else {
1852 gen_cvt_itof(t);
1853 }
1854 }
1855 #endif
1856
1857 /* generic ftoi for unsigned long long case */
1858 static void gen_cvt_ftoi1(int t)
1859 {
1860 int st;
1861
1862 if (t == (VT_LLONG | VT_UNSIGNED)) {
1863 /* not handled natively */
1864 st = vtop->type.t & VT_BTYPE;
1865 if (st == VT_FLOAT)
1866 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1867 #if LDOUBLE_SIZE != 8
1868 else if (st == VT_LDOUBLE)
1869 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1870 #endif
1871 else
1872 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1873 vrott(2);
1874 gfunc_call(1);
1875 vpushi(0);
1876 vtop->r = REG_IRET;
1877 vtop->r2 = REG_LRET;
1878 } else {
1879 gen_cvt_ftoi(t);
1880 }
1881 }
1882
1883 /* force char or short cast */
1884 static void force_charshort_cast(int t)
1885 {
1886 int bits, dbt;
1887 dbt = t & VT_BTYPE;
1888 /* XXX: add optimization if lvalue : just change type and offset */
1889 if (dbt == VT_BYTE)
1890 bits = 8;
1891 else
1892 bits = 16;
1893 if (t & VT_UNSIGNED) {
1894 vpushi((1 << bits) - 1);
1895 gen_op('&');
1896 } else {
1897 bits = 32 - bits;
1898 vpushi(bits);
1899 gen_op(TOK_SHL);
1900 /* result must be signed or the SAR is converted to an SHL
1901 This was not the case when "t" was a signed short
1902 and the last value on the stack was an unsigned int */
1903 vtop->type.t &= ~VT_UNSIGNED;
1904 vpushi(bits);
1905 gen_op(TOK_SAR);
1906 }
1907 }
1908
1909 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1910 static void gen_cast(CType *type)
1911 {
1912 int sbt, dbt, sf, df, c, p;
1913
1914 /* special delayed cast for char/short */
1915 /* XXX: in some cases (multiple cascaded casts), it may still
1916 be incorrect */
1917 if (vtop->r & VT_MUSTCAST) {
1918 vtop->r &= ~VT_MUSTCAST;
1919 force_charshort_cast(vtop->type.t);
1920 }
1921
1922 /* bitfields first get cast to ints */
1923 if (vtop->type.t & VT_BITFIELD) {
1924 gv(RC_INT);
1925 }
1926
1927 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1928 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1929
1930 if (sbt != dbt) {
1931 sf = is_float(sbt);
1932 df = is_float(dbt);
1933 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1934 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1935 if (c) {
1936 /* constant case: we can do it now */
1937 /* XXX: in ISOC, cannot do it if error in convert */
1938 if (sbt == VT_FLOAT)
1939 vtop->c.ld = vtop->c.f;
1940 else if (sbt == VT_DOUBLE)
1941 vtop->c.ld = vtop->c.d;
1942
1943 if (df) {
1944 if ((sbt & VT_BTYPE) == VT_LLONG) {
1945 if (sbt & VT_UNSIGNED)
1946 vtop->c.ld = vtop->c.ull;
1947 else
1948 vtop->c.ld = vtop->c.ll;
1949 } else if(!sf) {
1950 if (sbt & VT_UNSIGNED)
1951 vtop->c.ld = vtop->c.ui;
1952 else
1953 vtop->c.ld = vtop->c.i;
1954 }
1955
1956 if (dbt == VT_FLOAT)
1957 vtop->c.f = (float)vtop->c.ld;
1958 else if (dbt == VT_DOUBLE)
1959 vtop->c.d = (double)vtop->c.ld;
1960 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1961 vtop->c.ull = (unsigned long long)vtop->c.ld;
1962 } else if (sf && dbt == VT_BOOL) {
1963 vtop->c.i = (vtop->c.ld != 0);
1964 } else {
1965 if(sf)
1966 vtop->c.ll = (long long)vtop->c.ld;
1967 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1968 vtop->c.ll = vtop->c.ull;
1969 else if (sbt & VT_UNSIGNED)
1970 vtop->c.ll = vtop->c.ui;
1971 #ifdef TCC_TARGET_X86_64
1972 else if (sbt == VT_PTR)
1973 ;
1974 #endif
1975 else if (sbt != VT_LLONG)
1976 vtop->c.ll = vtop->c.i;
1977
1978 if (dbt == (VT_LLONG|VT_UNSIGNED))
1979 vtop->c.ull = vtop->c.ll;
1980 else if (dbt == VT_BOOL)
1981 vtop->c.i = (vtop->c.ll != 0);
1982 #ifdef TCC_TARGET_X86_64
1983 else if (dbt == VT_PTR)
1984 ;
1985 #endif
1986 else if (dbt != VT_LLONG) {
1987 int s = 0;
1988 if ((dbt & VT_BTYPE) == VT_BYTE)
1989 s = 24;
1990 else if ((dbt & VT_BTYPE) == VT_SHORT)
1991 s = 16;
1992 if(dbt & VT_UNSIGNED)
1993 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1994 else
1995 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1996 }
1997 }
1998 } else if (p && dbt == VT_BOOL) {
1999 vtop->r = VT_CONST;
2000 vtop->c.i = 1;
2001 } else if (!nocode_wanted) {
2002 /* non constant case: generate code */
2003 if (sf && df) {
2004 /* convert from fp to fp */
2005 gen_cvt_ftof(dbt);
2006 } else if (df) {
2007 /* convert int to fp */
2008 gen_cvt_itof1(dbt);
2009 } else if (sf) {
2010 /* convert fp to int */
2011 if (dbt == VT_BOOL) {
2012 vpushi(0);
2013 gen_op(TOK_NE);
2014 } else {
2015 /* we handle char/short/etc... with generic code */
2016 if (dbt != (VT_INT | VT_UNSIGNED) &&
2017 dbt != (VT_LLONG | VT_UNSIGNED) &&
2018 dbt != VT_LLONG)
2019 dbt = VT_INT;
2020 gen_cvt_ftoi1(dbt);
2021 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
2022 /* additional cast for char/short... */
2023 vtop->type.t = dbt;
2024 gen_cast(type);
2025 }
2026 }
2027 #ifndef TCC_TARGET_X86_64
2028 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
2029 if ((sbt & VT_BTYPE) != VT_LLONG) {
2030 /* scalar to long long */
2031 /* machine independent conversion */
2032 gv(RC_INT);
2033 /* generate high word */
2034 if (sbt == (VT_INT | VT_UNSIGNED)) {
2035 vpushi(0);
2036 gv(RC_INT);
2037 } else {
2038 if (sbt == VT_PTR) {
2039 /* cast from pointer to int before we apply
2040 shift operation, which pointers don't support*/
2041 gen_cast(&int_type);
2042 }
2043 gv_dup();
2044 vpushi(31);
2045 gen_op(TOK_SAR);
2046 }
2047 /* patch second register */
2048 vtop[-1].r2 = vtop->r;
2049 vpop();
2050 }
2051 #else
2052 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2053 (dbt & VT_BTYPE) == VT_PTR ||
2054 (dbt & VT_BTYPE) == VT_FUNC) {
2055 if ((sbt & VT_BTYPE) != VT_LLONG &&
2056 (sbt & VT_BTYPE) != VT_PTR &&
2057 (sbt & VT_BTYPE) != VT_FUNC) {
2058 /* need to convert from 32bit to 64bit */
2059 int r = gv(RC_INT);
2060 if (sbt != (VT_INT | VT_UNSIGNED)) {
2061 /* x86_64 specific: movslq */
2062 o(0x6348);
2063 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2064 }
2065 }
2066 #endif
2067 } else if (dbt == VT_BOOL) {
2068 /* scalar to bool */
2069 vpushi(0);
2070 gen_op(TOK_NE);
2071 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2072 (dbt & VT_BTYPE) == VT_SHORT) {
2073 if (sbt == VT_PTR) {
2074 vtop->type.t = VT_INT;
2075 tcc_warning("nonportable conversion from pointer to char/short");
2076 }
2077 force_charshort_cast(dbt);
2078 } else if ((dbt & VT_BTYPE) == VT_INT) {
2079 /* scalar to int */
2080 if (sbt == VT_LLONG) {
2081 /* from long long: just take low order word */
2082 lexpand();
2083 vpop();
2084 }
2085 /* if lvalue and single word type, nothing to do because
2086 the lvalue already contains the real type size (see
2087 VT_LVAL_xxx constants) */
2088 }
2089 }
2090 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2091 /* if we are casting between pointer types,
2092 we must update the VT_LVAL_xxx size */
2093 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2094 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2095 }
2096 vtop->type = *type;
2097 }
2098
2099 /* return type size as known at compile time. Put alignment at 'a' */
2100 ST_FUNC int type_size(CType *type, int *a)
2101 {
2102 Sym *s;
2103 int bt;
2104
2105 bt = type->t & VT_BTYPE;
2106 if (bt == VT_STRUCT) {
2107 /* struct/union */
2108 s = type->ref;
2109 *a = s->r;
2110 return s->c;
2111 } else if (bt == VT_PTR) {
2112 if (type->t & VT_ARRAY) {
2113 int ts;
2114
2115 s = type->ref;
2116 ts = type_size(&s->type, a);
2117
2118 if (ts < 0 && s->c < 0)
2119 ts = -ts;
2120
2121 return ts * s->c;
2122 } else {
2123 *a = PTR_SIZE;
2124 return PTR_SIZE;
2125 }
2126 } else if (bt == VT_LDOUBLE) {
2127 *a = LDOUBLE_ALIGN;
2128 return LDOUBLE_SIZE;
2129 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2130 #ifdef TCC_TARGET_I386
2131 #ifdef TCC_TARGET_PE
2132 *a = 8;
2133 #else
2134 *a = 4;
2135 #endif
2136 #elif defined(TCC_TARGET_ARM)
2137 #ifdef TCC_ARM_EABI
2138 *a = 8;
2139 #else
2140 *a = 4;
2141 #endif
2142 #else
2143 *a = 8;
2144 #endif
2145 return 8;
2146 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2147 *a = 4;
2148 return 4;
2149 } else if (bt == VT_SHORT) {
2150 *a = 2;
2151 return 2;
2152 } else if (bt == VT_QLONG || bt == VT_QFLOAT) {
2153 *a = 8;
2154 return 16;
2155 } else {
2156 /* char, void, function, _Bool */
2157 *a = 1;
2158 return 1;
2159 }
2160 }
2161
2162 /* push type size as known at runtime time on top of value stack. Put
2163 alignment at 'a' */
2164 ST_FUNC void vla_runtime_type_size(CType *type, int *a)
2165 {
2166 if (type->t & VT_VLA) {
2167 vset(&int_type, VT_LOCAL|VT_LVAL, type->ref->c);
2168 } else {
2169 vpushi(type_size(type, a));
2170 }
2171 }
2172
2173 static void vla_sp_save(void) {
2174 if (!(vla_flags & VLA_SP_LOC_SET)) {
2175 *vla_sp_loc = (loc -= PTR_SIZE);
2176 vla_flags |= VLA_SP_LOC_SET;
2177 }
2178 if (!(vla_flags & VLA_SP_SAVED)) {
2179 gen_vla_sp_save(*vla_sp_loc);
2180 vla_flags |= VLA_SP_SAVED;
2181 }
2182 }
2183
2184 /* return the pointed type of t */
2185 static inline CType *pointed_type(CType *type)
2186 {
2187 return &type->ref->type;
2188 }
2189
2190 /* modify type so that its it is a pointer to type. */
2191 ST_FUNC void mk_pointer(CType *type)
2192 {
2193 Sym *s;
2194 s = sym_push(SYM_FIELD, type, 0, -1);
2195 type->t = VT_PTR | (type->t & ~VT_TYPE);
2196 type->ref = s;
2197 }
2198
2199 /* compare function types. OLD functions match any new functions */
2200 static int is_compatible_func(CType *type1, CType *type2)
2201 {
2202 Sym *s1, *s2;
2203
2204 s1 = type1->ref;
2205 s2 = type2->ref;
2206 if (!is_compatible_types(&s1->type, &s2->type))
2207 return 0;
2208 /* check func_call */
2209 if (s1->a.func_call != s2->a.func_call)
2210 return 0;
2211 /* XXX: not complete */
2212 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2213 return 1;
2214 if (s1->c != s2->c)
2215 return 0;
2216 while (s1 != NULL) {
2217 if (s2 == NULL)
2218 return 0;
2219 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2220 return 0;
2221 s1 = s1->next;
2222 s2 = s2->next;
2223 }
2224 if (s2)
2225 return 0;
2226 return 1;
2227 }
2228
2229 /* return true if type1 and type2 are the same. If unqualified is
2230 true, qualifiers on the types are ignored.
2231
2232 - enums are not checked as gcc __builtin_types_compatible_p ()
2233 */
2234 static int compare_types(CType *type1, CType *type2, int unqualified)
2235 {
2236 int bt1, t1, t2;
2237
2238 t1 = type1->t & VT_TYPE;
2239 t2 = type2->t & VT_TYPE;
2240 if (unqualified) {
2241 /* strip qualifiers before comparing */
2242 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2243 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2244 }
2245 /* Default Vs explicit signedness only matters for char */
2246 if ((t1 & VT_BTYPE) != VT_BYTE) {
2247 t1 &= ~VT_DEFSIGN;
2248 t2 &= ~VT_DEFSIGN;
2249 }
2250 /* XXX: bitfields ? */
2251 if (t1 != t2)
2252 return 0;
2253 /* test more complicated cases */
2254 bt1 = t1 & VT_BTYPE;
2255 if (bt1 == VT_PTR) {
2256 type1 = pointed_type(type1);
2257 type2 = pointed_type(type2);
2258 return is_compatible_types(type1, type2);
2259 } else if (bt1 == VT_STRUCT) {
2260 return (type1->ref == type2->ref);
2261 } else if (bt1 == VT_FUNC) {
2262 return is_compatible_func(type1, type2);
2263 } else {
2264 return 1;
2265 }
2266 }
2267
2268 /* return true if type1 and type2 are exactly the same (including
2269 qualifiers).
2270 */
2271 static int is_compatible_types(CType *type1, CType *type2)
2272 {
2273 return compare_types(type1,type2,0);
2274 }
2275
2276 /* return true if type1 and type2 are the same (ignoring qualifiers).
2277 */
2278 static int is_compatible_parameter_types(CType *type1, CType *type2)
2279 {
2280 return compare_types(type1,type2,1);
2281 }
2282
2283 /* print a type. If 'varstr' is not NULL, then the variable is also
2284 printed in the type */
2285 /* XXX: union */
2286 /* XXX: add array and function pointers */
2287 static void type_to_str(char *buf, int buf_size,
2288 CType *type, const char *varstr)
2289 {
2290 int bt, v, t;
2291 Sym *s, *sa;
2292 char buf1[256];
2293 const char *tstr;
2294
2295 t = type->t & VT_TYPE;
2296 bt = t & VT_BTYPE;
2297 buf[0] = '\0';
2298 if (t & VT_CONSTANT)
2299 pstrcat(buf, buf_size, "const ");
2300 if (t & VT_VOLATILE)
2301 pstrcat(buf, buf_size, "volatile ");
2302 if ((t & (VT_DEFSIGN | VT_UNSIGNED)) == (VT_DEFSIGN | VT_UNSIGNED))
2303 pstrcat(buf, buf_size, "unsigned ");
2304 else if (t & VT_DEFSIGN)
2305 pstrcat(buf, buf_size, "signed ");
2306 switch(bt) {
2307 case VT_VOID:
2308 tstr = "void";
2309 goto add_tstr;
2310 case VT_BOOL:
2311 tstr = "_Bool";
2312 goto add_tstr;
2313 case VT_BYTE:
2314 tstr = "char";
2315 goto add_tstr;
2316 case VT_SHORT:
2317 tstr = "short";
2318 goto add_tstr;
2319 case VT_INT:
2320 tstr = "int";
2321 goto add_tstr;
2322 case VT_LONG:
2323 tstr = "long";
2324 goto add_tstr;
2325 case VT_LLONG:
2326 tstr = "long long";
2327 goto add_tstr;
2328 case VT_FLOAT:
2329 tstr = "float";
2330 goto add_tstr;
2331 case VT_DOUBLE:
2332 tstr = "double";
2333 goto add_tstr;
2334 case VT_LDOUBLE:
2335 tstr = "long double";
2336 add_tstr:
2337 pstrcat(buf, buf_size, tstr);
2338 break;
2339 case VT_ENUM:
2340 case VT_STRUCT:
2341 if (bt == VT_STRUCT)
2342 tstr = "struct ";
2343 else
2344 tstr = "enum ";
2345 pstrcat(buf, buf_size, tstr);
2346 v = type->ref->v & ~SYM_STRUCT;
2347 if (v >= SYM_FIRST_ANOM)
2348 pstrcat(buf, buf_size, "<anonymous>");
2349 else
2350 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2351 break;
2352 case VT_FUNC:
2353 s = type->ref;
2354 type_to_str(buf, buf_size, &s->type, varstr);
2355 pstrcat(buf, buf_size, "(");
2356 sa = s->next;
2357 while (sa != NULL) {
2358 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2359 pstrcat(buf, buf_size, buf1);
2360 sa = sa->next;
2361 if (sa)
2362 pstrcat(buf, buf_size, ", ");
2363 }
2364 pstrcat(buf, buf_size, ")");
2365 goto no_var;
2366 case VT_PTR:
2367 s = type->ref;
2368 pstrcpy(buf1, sizeof(buf1), "*");
2369 if (varstr)
2370 pstrcat(buf1, sizeof(buf1), varstr);
2371 type_to_str(buf, buf_size, &s->type, buf1);
2372 goto no_var;
2373 }
2374 if (varstr) {
2375 pstrcat(buf, buf_size, " ");
2376 pstrcat(buf, buf_size, varstr);
2377 }
2378 no_var: ;
2379 }
2380
2381 /* verify type compatibility to store vtop in 'dt' type, and generate
2382 casts if needed. */
2383 static void gen_assign_cast(CType *dt)
2384 {
2385 CType *st, *type1, *type2, tmp_type1, tmp_type2;
2386 char buf1[256], buf2[256];
2387 int dbt, sbt;
2388
2389 st = &vtop->type; /* source type */
2390 dbt = dt->t & VT_BTYPE;
2391 sbt = st->t & VT_BTYPE;
2392 if (sbt == VT_VOID || dbt == VT_VOID)
2393 tcc_error("cannot cast from/to void");
2394 if (dt->t & VT_CONSTANT)
2395 tcc_warning("assignment of read-only location");
2396 switch(dbt) {
2397 case VT_PTR:
2398 /* special cases for pointers */
2399 /* '0' can also be a pointer */
2400 if (is_null_pointer(vtop))
2401 goto type_ok;
2402 /* accept implicit pointer to integer cast with warning */
2403 if (is_integer_btype(sbt)) {
2404 tcc_warning("assignment makes pointer from integer without a cast");
2405 goto type_ok;
2406 }
2407 type1 = pointed_type(dt);
2408 /* a function is implicitely a function pointer */
2409 if (sbt == VT_FUNC) {
2410 if ((type1->t & VT_BTYPE) != VT_VOID &&
2411 !is_compatible_types(pointed_type(dt), st))
2412 tcc_warning("assignment from incompatible pointer type");
2413 goto type_ok;
2414 }
2415 if (sbt != VT_PTR)
2416 goto error;
2417 type2 = pointed_type(st);
2418 if ((type1->t & VT_BTYPE) == VT_VOID ||
2419 (type2->t & VT_BTYPE) == VT_VOID) {
2420 /* void * can match anything */
2421 } else {
2422 /* exact type match, except for unsigned */
2423 tmp_type1 = *type1;
2424 tmp_type2 = *type2;
2425 tmp_type1.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2426 VT_VOLATILE);
2427 tmp_type2.t &= ~(VT_DEFSIGN | VT_UNSIGNED | VT_CONSTANT |
2428 VT_VOLATILE);
2429 if (!is_compatible_types(&tmp_type1, &tmp_type2))
2430 tcc_warning("assignment from incompatible pointer type");
2431 }
2432 /* check const and volatile */
2433 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
2434 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
2435 tcc_warning("assignment discards qualifiers from pointer target type");
2436 break;
2437 case VT_BYTE:
2438 case VT_SHORT:
2439 case VT_INT:
2440 case VT_LLONG:
2441 if (sbt == VT_PTR || sbt == VT_FUNC) {
2442 tcc_warning("assignment makes integer from pointer without a cast");
2443 }
2444 /* XXX: more tests */
2445 break;
2446 case VT_STRUCT:
2447 tmp_type1 = *dt;
2448 tmp_type2 = *st;
2449 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
2450 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
2451 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2452 error:
2453 type_to_str(buf1, sizeof(buf1), st, NULL);
2454 type_to_str(buf2, sizeof(buf2), dt, NULL);
2455 tcc_error("cannot cast '%s' to '%s'", buf1, buf2);
2456 }
2457 break;
2458 }
2459 type_ok:
2460 gen_cast(dt);
2461 }
2462
2463 /* store vtop in lvalue pushed on stack */
2464 ST_FUNC void vstore(void)
2465 {
2466 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2467
2468 ft = vtop[-1].type.t;
2469 sbt = vtop->type.t & VT_BTYPE;
2470 dbt = ft & VT_BTYPE;
2471 if ((((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2472 (sbt == VT_INT && dbt == VT_SHORT))
2473 && !(vtop->type.t & VT_BITFIELD)) {
2474 /* optimize char/short casts */
2475 delayed_cast = VT_MUSTCAST;
2476 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2477 /* XXX: factorize */
2478 if (ft & VT_CONSTANT)
2479 tcc_warning("assignment of read-only location");
2480 } else {
2481 delayed_cast = 0;
2482 if (!(ft & VT_BITFIELD))
2483 gen_assign_cast(&vtop[-1].type);
2484 }
2485
2486 if (sbt == VT_STRUCT) {
2487 /* if structure, only generate pointer */
2488 /* structure assignment : generate memcpy */
2489 /* XXX: optimize if small size */
2490 if (!nocode_wanted) {
2491 size = type_size(&vtop->type, &align);
2492
2493 /* destination */
2494 vswap();
2495 vtop->type.t = VT_PTR;
2496 gaddrof();
2497
2498 /* address of memcpy() */
2499 #ifdef TCC_ARM_EABI
2500 if(!(align & 7))
2501 vpush_global_sym(&func_old_type, TOK_memcpy8);
2502 else if(!(align & 3))
2503 vpush_global_sym(&func_old_type, TOK_memcpy4);
2504 else
2505 #endif
2506 vpush_global_sym(&func_old_type, TOK_memcpy);
2507
2508 vswap();
2509 /* source */
2510 vpushv(vtop - 2);
2511 vtop->type.t = VT_PTR;
2512 gaddrof();
2513 /* type size */
2514 vpushi(size);
2515 gfunc_call(3);
2516 } else {
2517 vswap();
2518 vpop();
2519 }
2520 /* leave source on stack */
2521 } else if (ft & VT_BITFIELD) {
2522 /* bitfield store handling */
2523
2524 /* save lvalue as expression result (example: s.b = s.a = n;) */
2525 vdup(), vtop[-1] = vtop[-2];
2526
2527 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2528 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2529 /* remove bit field info to avoid loops */
2530 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2531
2532 if((ft & VT_BTYPE) == VT_BOOL) {
2533 gen_cast(&vtop[-1].type);
2534 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2535 }
2536
2537 /* duplicate destination */
2538 vdup();
2539 vtop[-1] = vtop[-2];
2540
2541 /* mask and shift source */
2542 if((ft & VT_BTYPE) != VT_BOOL) {
2543 if((ft & VT_BTYPE) == VT_LLONG) {
2544 vpushll((1ULL << bit_size) - 1ULL);
2545 } else {
2546 vpushi((1 << bit_size) - 1);
2547 }
2548 gen_op('&');
2549 }
2550 vpushi(bit_pos);
2551 gen_op(TOK_SHL);
2552 /* load destination, mask and or with source */
2553 vswap();
2554 if((ft & VT_BTYPE) == VT_LLONG) {
2555 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2556 } else {
2557 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2558 }
2559 gen_op('&');
2560 gen_op('|');
2561 /* store result */
2562 vstore();
2563 /* ... and discard */
2564 vpop();
2565
2566 } else {
2567 #ifdef CONFIG_TCC_BCHECK
2568 /* bound check case */
2569 if (vtop[-1].r & VT_MUSTBOUND) {
2570 vswap();
2571 gbound();
2572 vswap();
2573 }
2574 #endif
2575 if (!nocode_wanted) {
2576 rc = RC_INT;
2577 if (is_float(ft)) {
2578 rc = RC_FLOAT;
2579 #ifdef TCC_TARGET_X86_64
2580 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2581 rc = RC_ST0;
2582 } else if ((ft & VT_BTYPE) == VT_QFLOAT) {
2583 rc = RC_FRET;
2584 }
2585 #endif
2586 }
2587 r = gv(rc); /* generate value */
2588 /* if lvalue was saved on stack, must read it */
2589 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2590 SValue sv;
2591 t = get_reg(RC_INT);
2592 #ifdef TCC_TARGET_X86_64
2593 sv.type.t = VT_PTR;
2594 #else
2595 sv.type.t = VT_INT;
2596 #endif
2597 sv.r = VT_LOCAL | VT_LVAL;
2598 sv.c.ul = vtop[-1].c.ul;
2599 load(t, &sv);
2600 vtop[-1].r = t | VT_LVAL;
2601 }
2602 /* two word case handling : store second register at word + 4 (or +8 for x86-64) */
2603 #ifdef TCC_TARGET_X86_64
2604 if (((ft & VT_BTYPE) == VT_QLONG) || ((ft & VT_BTYPE) == VT_QFLOAT)) {
2605 int addr_type = VT_LLONG, load_size = 8, load_type = ((vtop->type.t & VT_BTYPE) == VT_QLONG) ? VT_LLONG : VT_DOUBLE;
2606 #else
2607 if ((ft & VT_BTYPE) == VT_LLONG) {
2608 int addr_type = VT_INT, load_size = 4, load_type = VT_INT;
2609 #endif
2610 vtop[-1].type.t = load_type;
2611 store(r, vtop - 1);
2612 vswap();
2613 /* convert to int to increment easily */
2614 vtop->type.t = addr_type;
2615 gaddrof();
2616 vpushi(load_size);
2617 gen_op('+');
2618 vtop->r |= VT_LVAL;
2619 vswap();
2620 vtop[-1].type.t = load_type;
2621 /* XXX: it works because r2 is spilled last ! */
2622 store(vtop->r2, vtop - 1);
2623 } else {
2624 store(r, vtop - 1);
2625 }
2626 }
2627 vswap();
2628 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2629 vtop->r |= delayed_cast;
2630 }
2631 }
2632
2633 /* post defines POST/PRE add. c is the token ++ or -- */
2634 ST_FUNC void inc(int post, int c)
2635 {
2636 test_lvalue();
2637 vdup(); /* save lvalue */
2638 if (post) {
2639 gv_dup(); /* duplicate value */
2640 vrotb(3);
2641 vrotb(3);
2642 }
2643 /* add constant */
2644 vpushi(c - TOK_MID);
2645 gen_op('+');
2646 vstore(); /* store value */
2647 if (post)
2648 vpop(); /* if post op, return saved value */
2649 }
2650
2651 /* Parse GNUC __attribute__ extension. Currently, the following
2652 extensions are recognized:
2653 - aligned(n) : set data/function alignment.
2654 - packed : force data alignment to 1
2655 - section(x) : generate data/code in this section.
2656 - unused : currently ignored, but may be used someday.
2657 - regparm(n) : pass function parameters in registers (i386 only)
2658 */
2659 static void parse_attribute(AttributeDef *ad)
2660 {
2661 int t, n;
2662
2663 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2664 next();
2665 skip('(');
2666 skip('(');
2667 while (tok != ')') {
2668 if (tok < TOK_IDENT)
2669 expect("attribute name");
2670 t = tok;
2671 next();
2672 switch(t) {
2673 case TOK_SECTION1:
2674 case TOK_SECTION2:
2675 skip('(');
2676 if (tok != TOK_STR)
2677 expect("section name");
2678 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2679 next();
2680 skip(')');
2681 break;
2682 case TOK_ALIAS1:
2683 case TOK_ALIAS2:
2684 skip('(');
2685 if (tok != TOK_STR)
2686 expect("alias(\"target\")");
2687 ad->alias_target = /* save string as token, for later */
2688 tok_alloc((char*)tokc.cstr->data, tokc.cstr->size-1)->tok;
2689 next();
2690 skip(')');
2691 break;
2692 case TOK_VISIBILITY1:
2693 case TOK_VISIBILITY2:
2694 skip('(');
2695 if (tok != TOK_STR)
2696 expect("visibility(\"default|hidden|internal|protected\")");
2697 if (!strcmp (tokc.cstr->data, "default"))
2698 ad->a.visibility = STV_DEFAULT;
2699 else if (!strcmp (tokc.cstr->data, "hidden"))
2700 ad->a.visibility = STV_HIDDEN;
2701 else if (!strcmp (tokc.cstr->data, "internal"))
2702 ad->a.visibility = STV_INTERNAL;
2703 else if (!strcmp (tokc.cstr->data, "protected"))
2704 ad->a.visibility = STV_PROTECTED;
2705 else
2706 expect("visibility(\"default|hidden|internal|protected\")");
2707 next();
2708 skip(')');
2709 break;
2710 case TOK_ALIGNED1:
2711 case TOK_ALIGNED2:
2712 if (tok == '(') {
2713 next();
2714 n = expr_const();
2715 if (n <= 0 || (n & (n - 1)) != 0)
2716 tcc_error("alignment must be a positive power of two");
2717 skip(')');
2718 } else {
2719 n = MAX_ALIGN;
2720 }
2721 ad->a.aligned = n;
2722 break;
2723 case TOK_PACKED1:
2724 case TOK_PACKED2:
2725 ad->a.packed = 1;
2726 break;
2727 case TOK_WEAK1:
2728 case TOK_WEAK2:
2729 ad->a.weak = 1;
2730 break;
2731 case TOK_UNUSED1:
2732 case TOK_UNUSED2:
2733 /* currently, no need to handle it because tcc does not
2734 track unused objects */
2735 break;
2736 case TOK_NORETURN1:
2737 case TOK_NORETURN2:
2738 /* currently, no need to handle it because tcc does not
2739 track unused objects */
2740 break;
2741 case TOK_CDECL1:
2742 case TOK_CDECL2:
2743 case TOK_CDECL3:
2744 ad->a.func_call = FUNC_CDECL;
2745 break;
2746 case TOK_STDCALL1:
2747 case TOK_STDCALL2:
2748 case TOK_STDCALL3:
2749 ad->a.func_call = FUNC_STDCALL;
2750 break;
2751 #ifdef TCC_TARGET_I386
2752 case TOK_REGPARM1:
2753 case TOK_REGPARM2:
2754 skip('(');
2755 n = expr_const();
2756 if (n > 3)
2757 n = 3;
2758 else if (n < 0)
2759 n = 0;
2760 if (n > 0)
2761 ad->a.func_call = FUNC_FASTCALL1 + n - 1;
2762 skip(')');
2763 break;
2764 case TOK_FASTCALL1:
2765 case TOK_FASTCALL2:
2766 case TOK_FASTCALL3:
2767 ad->a.func_call = FUNC_FASTCALLW;
2768 break;
2769 #endif
2770 case TOK_MODE:
2771 skip('(');
2772 switch(tok) {
2773 case TOK_MODE_DI:
2774 ad->a.mode = VT_LLONG + 1;
2775 break;
2776 case TOK_MODE_HI:
2777 ad->a.mode = VT_SHORT + 1;
2778 break;
2779 case TOK_MODE_SI:
2780 ad->a.mode = VT_INT + 1;
2781 break;
2782 default:
2783 tcc_warning("__mode__(%s) not supported\n", get_tok_str(tok, NULL));
2784 break;
2785 }
2786 next();
2787 skip(')');
2788 break;
2789 case TOK_DLLEXPORT:
2790 ad->a.func_export = 1;
2791 break;
2792 case TOK_DLLIMPORT:
2793 ad->a.func_import = 1;
2794 break;
2795 default:
2796 if (tcc_state->warn_unsupported)
2797 tcc_warning("'%s' attribute ignored", get_tok_str(t, NULL));
2798 /* skip parameters */
2799 if (tok == '(') {
2800 int parenthesis = 0;
2801 do {
2802 if (tok == '(')
2803 parenthesis++;
2804 else if (tok == ')')
2805 parenthesis--;
2806 next();
2807 } while (parenthesis && tok != -1);
2808 }
2809 break;
2810 }
2811 if (tok != ',')
2812 break;
2813 next();
2814 }
2815 skip(')');
2816 skip(')');
2817 }
2818 }
2819
2820 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2821 static void struct_decl(CType *type, int u, int tdef)
2822 {
2823 int a, v, size, align, maxalign, c, offset, flexible;
2824 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2825 Sym *s, *ss, *ass, **ps;
2826 AttributeDef ad;
2827 CType type1, btype;
2828
2829 a = tok; /* save decl type */
2830 next();
2831 if (tok != '{') {
2832 v = tok;
2833 next();
2834 /* struct already defined ? return it */
2835 if (v < TOK_IDENT)
2836 expect("struct/union/enum name");
2837 s = struct_find(v);
2838 if (s) {
2839 if (s->type.t != a)
2840 tcc_error("invalid type");
2841 goto do_decl;
2842 } else if (tok >= TOK_IDENT && !tdef)
2843 tcc_error("unknown struct/union/enum");
2844 } else {
2845 v = anon_sym++;
2846 }
2847 type1.t = a;
2848 type1.ref = NULL;
2849 /* we put an undefined size for struct/union */
2850 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2851 s->r = 0; /* default alignment is zero as gcc */
2852 /* put struct/union/enum name in type */
2853 do_decl:
2854 type->t = u;
2855 type->ref = s;
2856
2857 if (tok == '{') {
2858 next();
2859 if (s->c != -1)
2860 tcc_error("struct/union/enum already defined");
2861 /* cannot be empty */
2862 c = 0;
2863 /* non empty enums are not allowed */
2864 if (a == TOK_ENUM) {
2865 for(;;) {
2866 v = tok;
2867 if (v < TOK_UIDENT)
2868 expect("identifier");
2869 ss = sym_find(v);
2870 if (ss && !local_stack)
2871 tcc_error("redefinition of enumerator '%s'",
2872 get_tok_str(v, NULL));
2873 next();
2874 if (tok == '=') {
2875 next();
2876 c = expr_const();
2877 }
2878 /* enum symbols have static storage */
2879 ss = sym_push(v, &int_type, VT_CONST, c);
2880 ss->type.t |= VT_STATIC;
2881 if (tok != ',')
2882 break;
2883 next();
2884 c++;
2885 /* NOTE: we accept a trailing comma */
2886 if (tok == '}')
2887 break;
2888 }
2889 s->c = type_size(&int_type, &align);
2890 skip('}');
2891 } else {
2892 maxalign = 1;
2893 ps = &s->next;
2894 prevbt = VT_INT;
2895 bit_pos = 0;
2896 offset = 0;
2897 flexible = 0;
2898 while (tok != '}') {
2899 parse_btype(&btype, &ad);
2900 while (1) {
2901 if (flexible)
2902 tcc_error("flexible array member '%s' not at the end of struct",
2903 get_tok_str(v, NULL));
2904 bit_size = -1;
2905 v = 0;
2906 type1 = btype;
2907 if (tok != ':') {
2908 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2909 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2910 expect("identifier");
2911 if (type_size(&type1, &align) < 0) {
2912 if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2913 flexible = 1;
2914 else
2915 tcc_error("field '%s' has incomplete type",
2916 get_tok_str(v, NULL));
2917 }
2918 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2919 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2920 tcc_error("invalid type for '%s'",
2921 get_tok_str(v, NULL));
2922 }
2923 if (tok == ':') {
2924 next();
2925 bit_size = expr_const();
2926 /* XXX: handle v = 0 case for messages */
2927 if (bit_size < 0)
2928 tcc_error("negative width in bit-field '%s'",
2929 get_tok_str(v, NULL));
2930 if (v && bit_size == 0)
2931 tcc_error("zero width for bit-field '%s'",
2932 get_tok_str(v, NULL));
2933 }
2934 size = type_size(&type1, &align);
2935 if (ad.a.aligned) {
2936 if (align < ad.a.aligned)
2937 align = ad.a.aligned;
2938 } else if (ad.a.packed) {
2939 align = 1;
2940 } else if (*tcc_state->pack_stack_ptr) {
2941 if (align > *tcc_state->pack_stack_ptr)
2942 align = *tcc_state->pack_stack_ptr;
2943 }
2944 lbit_pos = 0;
2945 if (bit_size >= 0) {
2946 bt = type1.t & VT_BTYPE;
2947 if (bt != VT_INT &&
2948 bt != VT_BYTE &&
2949 bt != VT_SHORT &&
2950 bt != VT_BOOL &&
2951 bt != VT_ENUM &&
2952 bt != VT_LLONG)
2953 tcc_error("bitfields must have scalar type");
2954 bsize = size * 8;
2955 if (bit_size > bsize) {
2956 tcc_error("width of '%s' exceeds its type",
2957 get_tok_str(v, NULL));
2958 } else if (bit_size == bsize) {
2959 /* no need for bit fields */
2960 bit_pos = 0;
2961 } else if (bit_size == 0) {
2962 /* XXX: what to do if only padding in a
2963 structure ? */
2964 /* zero size: means to pad */
2965 bit_pos = 0;
2966 } else {
2967 /* we do not have enough room ?
2968 did the type change?
2969 is it a union? */
2970 if ((bit_pos + bit_size) > bsize ||
2971 bt != prevbt || a == TOK_UNION)
2972 bit_pos = 0;
2973 lbit_pos = bit_pos;
2974 /* XXX: handle LSB first */
2975 type1.t |= VT_BITFIELD |
2976 (bit_pos << VT_STRUCT_SHIFT) |
2977 (bit_size << (VT_STRUCT_SHIFT + 6));
2978 bit_pos += bit_size;
2979 }
2980 prevbt = bt;
2981 } else {
2982 bit_pos = 0;
2983 }
2984 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2985 /* add new memory data only if starting
2986 bit field */
2987 if (lbit_pos == 0) {
2988 if (a == TOK_STRUCT) {
2989 c = (c + align - 1) & -align;
2990 offset = c;
2991 if (size > 0)
2992 c += size;
2993 } else {
2994 offset = 0;
2995 if (size > c)
2996 c = size;
2997 }
2998 if (align > maxalign)
2999 maxalign = align;
3000 }
3001 #if 0
3002 printf("add field %s offset=%d",
3003 get_tok_str(v, NULL), offset);
3004 if (type1.t & VT_BITFIELD) {
3005 printf(" pos=%d size=%d",
3006 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
3007 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
3008 }
3009 printf("\n");
3010 #endif
3011 }
3012 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
3013 ass = type1.ref;
3014 while ((ass = ass->next) != NULL) {
3015 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
3016 *ps = ss;
3017 ps = &ss->next;
3018 }
3019 } else if (v) {
3020 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
3021 *ps = ss;
3022 ps = &ss->next;
3023 }
3024 if (tok == ';' || tok == TOK_EOF)
3025 break;
3026 skip(',');
3027 }
3028 skip(';');
3029 }
3030 skip('}');
3031 /* store size and alignment */
3032 s->c = (c + maxalign - 1) & -maxalign;
3033 s->r = maxalign;
3034 }
3035 }
3036 }
3037
3038 /* return 1 if basic type is a type size (short, long, long long) */
3039 ST_FUNC int is_btype_size(int bt)
3040 {
3041 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
3042 }
3043
3044 /* return 0 if no type declaration. otherwise, return the basic type
3045 and skip it.
3046 */
3047 static int parse_btype(CType *type, AttributeDef *ad)
3048 {
3049 int t, u, bt_size, complete, type_found, typespec_found;
3050 Sym *s;
3051 CType type1;
3052
3053 memset(ad, 0, sizeof(AttributeDef));
3054 complete = 0;
3055 type_found = 0;
3056 typespec_found = 0;
3057 t = 0;
3058 while(1) {
3059 switch(tok) {
3060 case TOK_EXTENSION:
3061 /* currently, we really ignore extension */
3062 next();
3063 continue;
3064
3065 /* basic types */
3066 case TOK_CHAR:
3067 u = VT_BYTE;
3068 basic_type:
3069 next();
3070 basic_type1:
3071 if (complete)
3072 tcc_error("too many basic types");
3073 t |= u;
3074 bt_size = is_btype_size (u & VT_BTYPE);
3075 if (u == VT_INT || (!bt_size && !(t & VT_TYPEDEF)))
3076 complete = 1;
3077 typespec_found = 1;
3078 break;
3079 case TOK_VOID:
3080 u = VT_VOID;
3081 goto basic_type;
3082 case TOK_SHORT:
3083 u = VT_SHORT;
3084 goto basic_type;
3085 case TOK_INT:
3086 u = VT_INT;
3087 goto basic_type;
3088 case TOK_LONG:
3089 next();
3090 if ((t & VT_BTYPE) == VT_DOUBLE) {
3091 #ifndef TCC_TARGET_PE
3092 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3093 #endif
3094 } else if ((t & VT_BTYPE) == VT_LONG) {
3095 t = (t & ~VT_BTYPE) | VT_LLONG;
3096 } else {
3097 u = VT_LONG;
3098 goto basic_type1;
3099 }
3100 break;
3101 case TOK_BOOL:
3102 u = VT_BOOL;
3103 goto basic_type;
3104 case TOK_FLOAT:
3105 u = VT_FLOAT;
3106 goto basic_type;
3107 case TOK_DOUBLE:
3108 next();
3109 if ((t & VT_BTYPE) == VT_LONG) {
3110 #ifdef TCC_TARGET_PE
3111 t = (t & ~VT_BTYPE) | VT_DOUBLE;
3112 #else
3113 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3114 #endif
3115 } else {
3116 u = VT_DOUBLE;
3117 goto basic_type1;
3118 }
3119 break;
3120 case TOK_ENUM:
3121 struct_decl(&type1, VT_ENUM, t & VT_TYPEDEF);
3122 basic_type2:
3123 u = type1.t;
3124 type->ref = type1.ref;
3125 goto basic_type1;
3126 case TOK_STRUCT:
3127 case TOK_UNION:
3128 struct_decl(&type1, VT_STRUCT, t & VT_TYPEDEF);
3129 goto basic_type2;
3130
3131 /* type modifiers */
3132 case TOK_CONST1:
3133 case TOK_CONST2:
3134 case TOK_CONST3:
3135 t |= VT_CONSTANT;
3136 next();
3137 break;
3138 case TOK_VOLATILE1:
3139 case TOK_VOLATILE2:
3140 case TOK_VOLATILE3:
3141 t |= VT_VOLATILE;
3142 next();
3143 break;
3144 case TOK_SIGNED1:
3145 case TOK_SIGNED2:
3146 case TOK_SIGNED3:
3147 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == (VT_DEFSIGN|VT_UNSIGNED))
3148 tcc_error("signed and unsigned modifier");
3149 typespec_found = 1;
3150 t |= VT_DEFSIGN;
3151 next();
3152 break;
3153 case TOK_REGISTER:
3154 case TOK_AUTO:
3155 case TOK_RESTRICT1:
3156 case TOK_RESTRICT2:
3157 case TOK_RESTRICT3:
3158 next();
3159 break;
3160 case TOK_UNSIGNED:
3161 if ((t & (VT_DEFSIGN|VT_UNSIGNED)) == VT_DEFSIGN)
3162 tcc_error("signed and unsigned modifier");
3163 t |= VT_DEFSIGN | VT_UNSIGNED;
3164 next();
3165 typespec_found = 1;
3166 break;
3167
3168 /* storage */
3169 case TOK_EXTERN:
3170 t |= VT_EXTERN;
3171 next();
3172 break;
3173 case TOK_STATIC:
3174 t |= VT_STATIC;
3175 next();
3176 break;
3177 case TOK_TYPEDEF:
3178 t |= VT_TYPEDEF;
3179 next();
3180 break;
3181 case TOK_INLINE1:
3182 case TOK_INLINE2:
3183 case TOK_INLINE3:
3184 t |= VT_INLINE;
3185 next();
3186 break;
3187
3188 /* GNUC attribute */
3189 case TOK_ATTRIBUTE1:
3190 case TOK_ATTRIBUTE2:
3191 parse_attribute(ad);
3192 if (ad->a.mode) {
3193 u = ad->a.mode -1;
3194 t = (t & ~VT_BTYPE) | u;
3195 }
3196 break;
3197 /* GNUC typeof */
3198 case TOK_TYPEOF1:
3199 case TOK_TYPEOF2:
3200 case TOK_TYPEOF3:
3201 next();
3202 parse_expr_type(&type1);
3203 /* remove all storage modifiers except typedef */
3204 type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
3205 goto basic_type2;
3206 default:
3207 if (typespec_found)
3208 goto the_end;
3209 s = sym_find(tok);
3210 if (!s || !(s->type.t & VT_TYPEDEF))
3211 goto the_end;
3212 t |= (s->type.t & ~VT_TYPEDEF);
3213 type->ref = s->type.ref;
3214 if (s->r) {
3215 /* get attributes from typedef */
3216 if (0 == ad->a.aligned)
3217 ad->a.aligned = s->a.aligned;
3218 if (0 == ad->a.func_call)
3219 ad->a.func_call = s->a.func_call;
3220 ad->a.packed |= s->a.packed;
3221 }
3222 next();
3223 typespec_found = 1;
3224 break;
3225 }
3226 type_found = 1;
3227 }
3228 the_end:
3229 if (tcc_state->char_is_unsigned) {
3230 if ((t & (VT_DEFSIGN|VT_BTYPE)) == VT_BYTE)
3231 t |= VT_UNSIGNED;
3232 }
3233
3234 /* long is never used as type */
3235 if ((t & VT_BTYPE) == VT_LONG)
3236 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
3237 t = (t & ~VT_BTYPE) | VT_INT;
3238 #else
3239 t = (t & ~VT_BTYPE) | VT_LLONG;
3240 #endif
3241 type->t = t;
3242 return type_found;
3243 }
3244
3245 /* convert a function parameter type (array to pointer and function to
3246 function pointer) */
3247 static inline void convert_parameter_type(CType *pt)
3248 {
3249 /* remove const and volatile qualifiers (XXX: const could be used
3250 to indicate a const function parameter */
3251 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3252 /* array must be transformed to pointer according to ANSI C */
3253 pt->t &= ~VT_ARRAY;
3254 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3255 mk_pointer(pt);
3256 }
3257 }
3258
3259 ST_FUNC void parse_asm_str(CString *astr)
3260 {
3261 skip('(');
3262 /* read the string */
3263 if (tok != TOK_STR)
3264 expect("string constant");
3265 cstr_new(astr);
3266 while (tok == TOK_STR) {
3267 /* XXX: add \0 handling too ? */
3268 cstr_cat(astr, tokc.cstr->data);
3269 next();
3270 }
3271 cstr_ccat(astr, '\0');
3272 }
3273
3274 /* Parse an asm label and return the label
3275 * Don't forget to free the CString in the caller! */
3276 static void asm_label_instr(CString *astr)
3277 {
3278 next();
3279 parse_asm_str(astr);
3280 skip(')');
3281 #ifdef ASM_DEBUG
3282 printf("asm_alias: \"%s\"\n", (char *)astr->data);
3283 #endif
3284 }
3285
3286 static void post_type(CType *type, AttributeDef *ad)
3287 {
3288 int n, l, t1, arg_size, align;
3289 Sym **plast, *s, *first;
3290 AttributeDef ad1;
3291 CType pt;
3292
3293 if (tok == '(') {
3294 /* function declaration */
3295 next();
3296 l = 0;
3297 first = NULL;
3298 plast = &first;
3299 arg_size = 0;
3300 if (tok != ')') {
3301 for(;;) {
3302 /* read param name and compute offset */
3303 if (l != FUNC_OLD) {
3304 if (!parse_btype(&pt, &ad1)) {
3305 if (l) {
3306 tcc_error("invalid type");
3307 } else {
3308 l = FUNC_OLD;
3309 goto old_proto;
3310 }
3311 }
3312 l = FUNC_NEW;
3313 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3314 break;
3315 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3316 if ((pt.t & VT_BTYPE) == VT_VOID)
3317 tcc_error("parameter declared as void");
3318 arg_size += (type_size(&pt, &align) + PTR_SIZE - 1) / PTR_SIZE;
3319 } else {
3320 old_proto:
3321 n = tok;
3322 if (n < TOK_UIDENT)
3323 expect("identifier");
3324 pt.t = VT_INT;
3325 next();
3326 }
3327 convert_parameter_type(&pt);
3328 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3329 *plast = s;
3330 plast = &s->next;
3331 if (tok == ')')
3332 break;
3333 skip(',');
3334 if (l == FUNC_NEW && tok == TOK_DOTS) {
3335 l = FUNC_ELLIPSIS;
3336 next();
3337 break;
3338 }
3339 }
3340 }
3341 /* if no parameters, then old type prototype */
3342 if (l == 0)
3343 l = FUNC_OLD;
3344 skip(')');
3345 /* NOTE: const is ignored in returned type as it has a special
3346 meaning in gcc / C++ */
3347 type->t &= ~VT_CONSTANT;
3348 /* some ancient pre-K&R C allows a function to return an array
3349 and the array brackets to be put after the arguments, such
3350 that "int c()[]" means something like "int[] c()" */
3351 if (tok == '[') {
3352 next();
3353 skip(']'); /* only handle simple "[]" */
3354 type->t |= VT_PTR;
3355 }
3356 /* we push a anonymous symbol which will contain the function prototype */
3357 ad->a.func_args = arg_size;
3358 s = sym_push(SYM_FIELD, type, 0, l);
3359 s->a = ad->a;
3360 s->next = first;
3361 type->t = VT_FUNC;
3362 type->ref = s;
3363 } else if (tok == '[') {
3364 /* array definition */
3365 next();
3366 if (tok == TOK_RESTRICT1)
3367 next();
3368 n = -1;
3369 t1 = 0;
3370 if (tok != ']') {
3371 if (!local_stack || nocode_wanted)
3372 vpushi(expr_const());
3373 else gexpr();
3374 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3375 n = vtop->c.i;
3376 if (n < 0)
3377 tcc_error("invalid array size");
3378 } else {
3379 if (!is_integer_btype(vtop->type.t & VT_BTYPE))
3380 tcc_error("size of variable length array should be an integer");
3381 t1 = VT_VLA;
3382 }
3383 }
3384 skip(']');
3385 /* parse next post type */
3386 post_type(type, ad);
3387 if (type->t == VT_FUNC)
3388 tcc_error("declaration of an array of functions");
3389 t1 |= type->t & VT_VLA;
3390
3391 if (t1 & VT_VLA) {
3392 loc -= type_size(&int_type, &align);
3393 loc &= -align;
3394 n = loc;
3395
3396 vla_runtime_type_size(type, &align);
3397 gen_op('*');
3398 vset(&int_type, VT_LOCAL|VT_LVAL, loc);
3399 vswap();
3400 vstore();
3401 }
3402 if (n != -1)
3403 vpop();
3404
3405 /* we push an anonymous symbol which will contain the array
3406 element type */
3407 s = sym_push(SYM_FIELD, type, 0, n);
3408 type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
3409 type->ref = s;
3410 }
3411 }
3412
3413 /* Parse a type declaration (except basic type), and return the type
3414 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3415 expected. 'type' should contain the basic type. 'ad' is the
3416 attribute definition of the basic type. It can be modified by
3417 type_decl().
3418 */
3419 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3420 {
3421 Sym *s;
3422 CType type1, *type2;
3423 int qualifiers, storage;
3424
3425 while (tok == '*') {
3426 qualifiers = 0;
3427 redo:
3428 next();
3429 switch(tok) {
3430 case TOK_CONST1:
3431 case TOK_CONST2:
3432 case TOK_CONST3:
3433 qualifiers |= VT_CONSTANT;
3434 goto redo;
3435 case TOK_VOLATILE1:
3436 case TOK_VOLATILE2:
3437 case TOK_VOLATILE3:
3438 qualifiers |= VT_VOLATILE;
3439 goto redo;
3440 case TOK_RESTRICT1:
3441 case TOK_RESTRICT2:
3442 case TOK_RESTRICT3:
3443 goto redo;
3444 }
3445 mk_pointer(type);
3446 type->t |= qualifiers;
3447 }
3448
3449 /* XXX: clarify attribute handling */
3450 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3451 parse_attribute(ad);
3452
3453 /* recursive type */
3454 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3455 type1.t = 0; /* XXX: same as int */
3456 if (tok == '(') {
3457 next();
3458 /* XXX: this is not correct to modify 'ad' at this point, but
3459 the syntax is not clear */
3460 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3461 parse_attribute(ad);
3462 type_decl(&type1, ad, v, td);
3463 skip(')');
3464 } else {
3465 /* type identifier */
3466 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3467 *v = tok;
3468 next();
3469 } else {
3470 if (!(td & TYPE_ABSTRACT))
3471 expect("identifier");
3472 *v = 0;
3473 }
3474 }
3475 storage = type->t & VT_STORAGE;
3476 type->t &= ~VT_STORAGE;
3477 if (storage & VT_STATIC) {
3478 int saved_nocode_wanted = nocode_wanted;
3479 nocode_wanted = 1;
3480 post_type(type, ad);
3481 nocode_wanted = saved_nocode_wanted;
3482 } else
3483 post_type(type, ad);
3484 type->t |= storage;
3485 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3486 parse_attribute(ad);
3487
3488 if (!type1.t)
3489 return;
3490 /* append type at the end of type1 */
3491 type2 = &type1;
3492 for(;;) {
3493 s = type2->ref;
3494 type2 = &s->type;
3495 if (!type2->t) {
3496 *type2 = *type;
3497 break;
3498 }
3499 }
3500 *type = type1;
3501 }
3502
3503 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
3504 ST_FUNC int lvalue_type(int t)
3505 {
3506 int bt, r;
3507 r = VT_LVAL;
3508 bt = t & VT_BTYPE;
3509 if (bt == VT_BYTE || bt == VT_BOOL)
3510 r |= VT_LVAL_BYTE;
3511 else if (bt == VT_SHORT)
3512 r |= VT_LVAL_SHORT;
3513 else
3514 return r;
3515 if (t & VT_UNSIGNED)
3516 r |= VT_LVAL_UNSIGNED;
3517 return r;
3518 }
3519
3520 /* indirection with full error checking and bound check */
3521 ST_FUNC void indir(void)
3522 {
3523 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3524 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3525 return;
3526 expect("pointer");
3527 }
3528 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3529 gv(RC_INT);
3530 vtop->type = *pointed_type(&vtop->type);
3531 /* Arrays and functions are never lvalues */
3532 if (!(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_VLA)
3533 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3534 vtop->r |= lvalue_type(vtop->type.t);
3535 /* if bound checking, the referenced pointer must be checked */
3536 #ifdef CONFIG_TCC_BCHECK
3537 if (tcc_state->do_bounds_check)
3538 vtop->r |= VT_MUSTBOUND;
3539 #endif
3540 }
3541 }
3542
3543 /* pass a parameter to a function and do type checking and casting */
3544 static void gfunc_param_typed(Sym *func, Sym *arg)
3545 {
3546 int func_type;
3547 CType type;
3548
3549 func_type = func->c;
3550 if (func_type == FUNC_OLD ||
3551 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3552 /* default casting : only need to convert float to double */
3553 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3554 type.t = VT_DOUBLE;
3555 gen_cast(&type);
3556 } else if (vtop->type.t & VT_BITFIELD) {
3557 type.t = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
3558 gen_cast(&type);
3559 }
3560 } else if (arg == NULL) {
3561 tcc_error("too many arguments to function");
3562 } else {
3563 type = arg->type;
3564 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3565 gen_assign_cast(&type);
3566 }
3567 }
3568
3569 /* parse an expression of the form '(type)' or '(expr)' and return its
3570 type */
3571 static void parse_expr_type(CType *type)
3572 {
3573 int n;
3574 AttributeDef ad;
3575
3576 skip('(');
3577 if (parse_btype(type, &ad)) {
3578 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3579 } else {
3580 expr_type(type);
3581 }
3582 skip(')');
3583 }
3584
3585 static void parse_type(CType *type)
3586 {
3587 AttributeDef ad;
3588 int n;
3589
3590 if (!parse_btype(type, &ad)) {
3591 expect("type");
3592 }
3593 type_decl(type, &ad, &n, TYPE_ABSTRACT);
3594 }
3595
3596 static void vpush_tokc(int t)
3597 {
3598 CType type;
3599 type.t = t;
3600 type.ref = 0;
3601 vsetc(&type, VT_CONST, &tokc);
3602 }
3603
3604 ST_FUNC void unary(void)
3605 {
3606 int n, t, align, size, r, sizeof_caller;
3607 CType type;
3608 Sym *s;
3609 AttributeDef ad;
3610 static int in_sizeof = 0;
3611
3612 sizeof_caller = in_sizeof;
3613 in_sizeof = 0;
3614 /* XXX: GCC 2.95.3 does not generate a table although it should be
3615 better here */
3616 tok_next:
3617 switch(tok) {
3618 case TOK_EXTENSION:
3619 next();
3620 goto tok_next;
3621 case TOK_CINT:
3622 case TOK_CCHAR:
3623 case TOK_LCHAR:
3624 vpushi(tokc.i);
3625 next();
3626 break;
3627 case TOK_CUINT:
3628 vpush_tokc(VT_INT | VT_UNSIGNED);
3629 next();
3630 break;
3631 case TOK_CLLONG:
3632 vpush_tokc(VT_LLONG);
3633 next();
3634 break;
3635 case TOK_CULLONG:
3636 vpush_tokc(VT_LLONG | VT_UNSIGNED);
3637 next();
3638 break;
3639 case TOK_CFLOAT:
3640 vpush_tokc(VT_FLOAT);
3641 next();
3642 break;
3643 case TOK_CDOUBLE:
3644 vpush_tokc(VT_DOUBLE);
3645 next();
3646 break;
3647 case TOK_CLDOUBLE:
3648 vpush_tokc(VT_LDOUBLE);
3649 next();
3650 break;
3651 case TOK___FUNCTION__:
3652 if (!gnu_ext)
3653 goto tok_identifier;
3654 /* fall thru */
3655 case TOK___FUNC__:
3656 {
3657 void *ptr;
3658 int len;
3659 /* special function name identifier */
3660 len = strlen(funcname) + 1;
3661 /* generate char[len] type */
3662 type.t = VT_BYTE;
3663 mk_pointer(&type);
3664 type.t |= VT_ARRAY;
3665 type.ref->c = len;
3666 vpush_ref(&type, data_section, data_section->data_offset, len);
3667 ptr = section_ptr_add(data_section, len);
3668 memcpy(ptr, funcname, len);
3669 next();
3670 }
3671 break;
3672 case TOK_LSTR:
3673 #ifdef TCC_TARGET_PE
3674 t = VT_SHORT | VT_UNSIGNED;
3675 #else
3676 t = VT_INT;
3677 #endif
3678 goto str_init;
3679 case TOK_STR:
3680 /* string parsing */
3681 t = VT_BYTE;
3682 str_init:
3683 if (tcc_state->warn_write_strings)
3684 t |= VT_CONSTANT;
3685 type.t = t;
3686 mk_pointer(&type);
3687 type.t |= VT_ARRAY;
3688 memset(&ad, 0, sizeof(AttributeDef));
3689 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, NULL, 0);
3690 break;
3691 case '(':
3692 next();
3693 /* cast ? */
3694 if (parse_btype(&type, &ad)) {
3695 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3696 skip(')');
3697 /* check ISOC99 compound literal */
3698 if (tok == '{') {
3699 /* data is allocated locally by default */
3700 if (global_expr)
3701 r = VT_CONST;
3702 else
3703 r = VT_LOCAL;
3704 /* all except arrays are lvalues */
3705 if (!(type.t & VT_ARRAY))
3706 r |= lvalue_type(type.t);
3707 memset(&ad, 0, sizeof(AttributeDef));
3708 decl_initializer_alloc(&type, &ad, r, 1, 0, NULL, 0);
3709 } else {
3710 if (sizeof_caller) {
3711 vpush(&type);
3712 return;
3713 }
3714 unary();
3715 gen_cast(&type);
3716 }
3717 } else if (tok == '{') {
3718 /* save all registers */
3719 save_regs(0);
3720 /* statement expression : we do not accept break/continue
3721 inside as GCC does */
3722 block(NULL, NULL, NULL, NULL, 0, 1);
3723 skip(')');
3724 } else {
3725 gexpr();
3726 skip(')');
3727 }
3728 break;
3729 case '*':
3730 next();
3731 unary();
3732 indir();
3733 break;
3734 case '&':
3735 next();
3736 unary();
3737 /* functions names must be treated as function pointers,
3738 except for unary '&' and sizeof. Since we consider that
3739 functions are not lvalues, we only have to handle it
3740 there and in function calls. */
3741 /* arrays can also be used although they are not lvalues */
3742 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3743 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3744 test_lvalue();
3745 mk_pointer(&vtop->type);
3746 gaddrof();
3747 break;
3748 case '!':
3749 next();
3750 unary();
3751 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3752 CType boolean;
3753 boolean.t = VT_BOOL;
3754 gen_cast(&boolean);
3755 vtop->c.i = !vtop->c.i;
3756 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3757 vtop->c.i = vtop->c.i ^ 1;
3758 else {
3759 save_regs(1);
3760 vseti(VT_JMP, gvtst(1, 0));
3761 }
3762 break;
3763 case '~':
3764 next();
3765 unary();
3766 vpushi(-1);
3767 gen_op('^');
3768 break;
3769 case '+':
3770 next();
3771 unary();
3772 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3773 tcc_error("pointer not accepted for unary plus");
3774 /* In order to force cast, we add zero, except for floating point
3775 where we really need an noop (otherwise -0.0 will be transformed
3776 into +0.0). */
3777 if (!is_float(vtop->type.t)) {
3778 vpushi(0);
3779 gen_op('+');
3780 }
3781 break;
3782 case TOK_SIZEOF:
3783 case TOK_ALIGNOF1:
3784 case TOK_ALIGNOF2:
3785 t = tok;
3786 next();
3787 in_sizeof++;
3788 unary_type(&type); // Perform a in_sizeof = 0;
3789 size = type_size(&type, &align);
3790 if (t == TOK_SIZEOF) {
3791 if (!(type.t & VT_VLA)) {
3792 if (size < 0)
3793 tcc_error("sizeof applied to an incomplete type");
3794 vpushs(size);
3795 } else {
3796 vla_runtime_type_size(&type, &align);
3797 }
3798 } else {
3799 vpushs(align);
3800 }
3801 vtop->type.t |= VT_UNSIGNED;
3802 break;
3803
3804 case TOK_builtin_types_compatible_p:
3805 {
3806 CType type1, type2;
3807 next();
3808 skip('(');
3809 parse_type(&type1);
3810 skip(',');
3811 parse_type(&type2);
3812 skip(')');
3813 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3814 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3815 vpushi(is_compatible_types(&type1, &type2));
3816 }
3817 break;
3818 case TOK_builtin_constant_p:
3819 {
3820 int saved_nocode_wanted, res;
3821 next();
3822 skip('(');
3823 saved_nocode_wanted = nocode_wanted;
3824 nocode_wanted = 1;
3825 gexpr();
3826 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3827 vpop();
3828 nocode_wanted = saved_nocode_wanted;
3829 skip(')');
3830 vpushi(res);
3831 }
3832 break;
3833 case TOK_builtin_frame_address:
3834 {
3835 int level;
3836 CType type;
3837 next();
3838 skip('(');
3839 if (tok != TOK_CINT || tokc.i < 0) {
3840 tcc_error("__builtin_frame_address only takes positive integers");
3841 }
3842 level = tokc.i;
3843 next();
3844 skip(')');
3845 type.t = VT_VOID;
3846 mk_pointer(&type);
3847 vset(&type, VT_LOCAL, 0); /* local frame */
3848 while (level--) {
3849 mk_pointer(&vtop->type);
3850 indir(); /* -> parent frame */
3851 }
3852 }
3853 break;
3854 #ifdef TCC_TARGET_X86_64
3855 #ifdef TCC_TARGET_PE
3856 case TOK_builtin_va_start:
3857 {
3858 next();
3859 skip('(');
3860 expr_eq();
3861 skip(',');
3862 expr_eq();
3863 skip(')');
3864 if ((vtop->r & VT_VALMASK) != VT_LOCAL)
3865 tcc_error("__builtin_va_start expects a local variable");
3866 vtop->r &= ~(VT_LVAL | VT_REF);
3867 vtop->type = char_pointer_type;
3868 vstore();
3869 }
3870 break;
3871 #else
3872 case TOK_builtin_va_arg_types:
3873 {
3874 CType type;
3875 next();
3876 skip('(');
3877 parse_type(&type);
3878 skip(')');
3879 vpushi(classify_x86_64_va_arg(&type));
3880 }
3881 break;
3882 #endif
3883 #endif
3884 case TOK_INC:
3885 case TOK_DEC:
3886 t = tok;
3887 next();
3888 unary();
3889 inc(0, t);
3890 break;
3891 case '-':
3892 next();
3893 unary();
3894 t = vtop->type.t & VT_BTYPE;
3895 if (is_float(t)) {
3896 /* In IEEE negate(x) isn't subtract(0,x), but rather
3897 subtract(-0, x). */
3898 vpush(&vtop->type);
3899 if (t == VT_FLOAT)
3900 vtop->c.f = -0.0f;
3901 else if (t == VT_DOUBLE)
3902 vtop->c.d = -0.0;
3903 else
3904 vtop->c.ld = -0.0;
3905 } else
3906 vpushi(0);
3907 vswap();
3908 gen_op('-');
3909 break;
3910 case TOK_LAND:
3911 if (!gnu_ext)
3912 goto tok_identifier;
3913 next();
3914 /* allow to take the address of a label */
3915 if (tok < TOK_UIDENT)
3916 expect("label identifier");
3917 s = label_find(tok);
3918 if (!s) {
3919 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3920 } else {
3921 if (s->r == LABEL_DECLARED)
3922 s->r = LABEL_FORWARD;
3923 }
3924 if (!s->type.t) {
3925 s->type.t = VT_VOID;
3926 mk_pointer(&s->type);
3927 s->type.t |= VT_STATIC;
3928 }
3929 vpushsym(&s->type, s);
3930 next();
3931 break;
3932
3933 // special qnan , snan and infinity values
3934 case TOK___NAN__:
3935 vpush64(VT_DOUBLE, 0x7ff8000000000000ULL);
3936 next();
3937 break;
3938 case TOK___SNAN__:
3939 vpush64(VT_DOUBLE, 0x7ff0000000000001ULL);
3940 next();
3941 break;
3942 case TOK___INF__:
3943 vpush64(VT_DOUBLE, 0x7ff0000000000000ULL);
3944 next();
3945 break;
3946
3947 default:
3948 tok_identifier:
3949 t = tok;
3950 next();
3951 if (t < TOK_UIDENT)
3952 expect("identifier");
3953 s = sym_find(t);
3954 if (!s) {
3955 const char *name = get_tok_str(t, NULL);
3956 if (tok != '(')
3957 tcc_error("'%s' undeclared", name);
3958 /* for simple function calls, we tolerate undeclared
3959 external reference to int() function */
3960 if (tcc_state->warn_implicit_function_declaration
3961 #ifdef TCC_TARGET_PE
3962 /* people must be warned about using undeclared WINAPI functions
3963 (which usually start with uppercase letter) */
3964 || (name[0] >= 'A' && name[0] <= 'Z')
3965 #endif
3966 )
3967 tcc_warning("implicit declaration of function '%s'", name);
3968 s = external_global_sym(t, &func_old_type, 0);
3969 }
3970 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3971 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3972 /* if referencing an inline function, then we generate a
3973 symbol to it if not already done. It will have the
3974 effect to generate code for it at the end of the
3975 compilation unit. Inline function as always
3976 generated in the text section. */
3977 if (!s->c)
3978 put_extern_sym(s, text_section, 0, 0);
3979 r = VT_SYM | VT_CONST;
3980 } else {
3981 r = s->r;
3982 }
3983 vset(&s->type, r, s->c);
3984 /* if forward reference, we must point to s */
3985 if (vtop->r & VT_SYM) {
3986 vtop->sym = s;
3987 vtop->c.ptr_offset = 0;
3988 }
3989 break;
3990 }
3991
3992 /* post operations */
3993 while (1) {
3994 if (tok == TOK_INC || tok == TOK_DEC) {
3995 inc(1, tok);
3996 next();
3997 } else if (tok == '.' || tok == TOK_ARROW) {
3998 int qualifiers;
3999 /* field */
4000 if (tok == TOK_ARROW)
4001 indir();
4002 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
4003 test_lvalue();
4004 gaddrof();
4005 next();
4006 /* expect pointer on structure */
4007 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
4008 expect("struct or union");
4009 s = vtop->type.ref;
4010 /* find field */
4011 tok |= SYM_FIELD;
4012 while ((s = s->next) != NULL) {
4013 if (s->v == tok)
4014 break;
4015 }
4016 if (!s)
4017 tcc_error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
4018 /* add field offset to pointer */
4019 vtop->type = char_pointer_type; /* change type to 'char *' */
4020 vpushi(s->c);
4021 gen_op('+');
4022 /* change type to field type, and set to lvalue */
4023 vtop->type = s->type;
4024 vtop->type.t |= qualifiers;
4025 /* an array is never an lvalue */
4026 if (!(vtop->type.t & VT_ARRAY)) {
4027 vtop->r |= lvalue_type(vtop->type.t);
4028 #ifdef CONFIG_TCC_BCHECK
4029 /* if bound checking, the referenced pointer must be checked */
4030 if (tcc_state->do_bounds_check)
4031 vtop->r |= VT_MUSTBOUND;
4032 #endif
4033 }
4034 next();
4035 } else if (tok == '[') {
4036 next();
4037 gexpr();
4038 gen_op('+');
4039 indir();
4040 skip(']');
4041 } else if (tok == '(') {
4042 SValue ret;
4043 Sym *sa;
4044 int nb_args, ret_nregs, ret_align, variadic;
4045
4046 /* function call */
4047 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
4048 /* pointer test (no array accepted) */
4049 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
4050 vtop->type = *pointed_type(&vtop->type);
4051 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4052 goto error_func;
4053 } else {
4054 error_func:
4055 expect("function pointer");
4056 }
4057 } else {
4058 vtop->r &= ~VT_LVAL; /* no lvalue */
4059 }
4060 /* get return type */
4061 s = vtop->type.ref;
4062 next();
4063 sa = s->next; /* first parameter */
4064 nb_args = 0;
4065 ret.r2 = VT_CONST;
4066 /* compute first implicit argument if a structure is returned */
4067 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4068 variadic = (s->c == FUNC_ELLIPSIS);
4069 ret_nregs = gfunc_sret(&s->type, variadic, &ret.type,
4070 &ret_align);
4071 if (!ret_nregs) {
4072 /* get some space for the returned structure */
4073 size = type_size(&s->type, &align);
4074 loc = (loc - size) & -align;
4075 ret.type = s->type;
4076 ret.r = VT_LOCAL | VT_LVAL;
4077 /* pass it as 'int' to avoid structure arg passing
4078 problems */
4079 vseti(VT_LOCAL, loc);
4080 ret.c = vtop->c;
4081 nb_args++;
4082 }
4083 } else {
4084 ret_nregs = 1;
4085 ret.type = s->type;
4086 }
4087
4088 if (ret_nregs) {
4089 /* return in register */
4090 if (is_float(ret.type.t)) {
4091 ret.r = reg_fret(ret.type.t);
4092 #ifdef TCC_TARGET_X86_64
4093 if ((ret.type.t & VT_BTYPE) == VT_QFLOAT)
4094 ret.r2 = REG_QRET;
4095 #endif
4096 } else {
4097 #ifdef TCC_TARGET_X86_64
4098 if ((ret.type.t & VT_BTYPE) == VT_QLONG)
4099 #else
4100 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4101 #endif
4102 ret.r2 = REG_LRET;
4103 ret.r = REG_IRET;
4104 }
4105 ret.c.i = 0;
4106 }
4107 if (tok != ')') {
4108 for(;;) {
4109 expr_eq();
4110 gfunc_param_typed(s, sa);
4111 nb_args++;
4112 if (sa)
4113 sa = sa->next;
4114 if (tok == ')')
4115 break;
4116 skip(',');
4117 }
4118 }
4119 if (sa)
4120 tcc_error("too few arguments to function");
4121 skip(')');
4122 if (!nocode_wanted) {
4123 gfunc_call(nb_args);
4124 } else {
4125 vtop -= (nb_args + 1);
4126 }
4127
4128 /* return value */
4129 for (r = ret.r + ret_nregs + !ret_nregs; r-- > ret.r;) {
4130 vsetc(&ret.type, r, &ret.c);
4131 vtop->r2 = ret.r2; /* Loop only happens when r2 is VT_CONST */
4132 }
4133
4134 /* handle packed struct return */
4135 if (((s->type.t & VT_BTYPE) == VT_STRUCT) && ret_nregs) {
4136 int addr, offset;
4137
4138 size = type_size(&s->type, &align);
4139 loc = (loc - size) & -align;
4140 addr = loc;
4141 offset = 0;
4142 for (;;) {
4143 vset(&ret.type, VT_LOCAL | VT_LVAL, addr + offset);
4144 vswap();
4145 vstore();
4146 vtop--;
4147 if (--ret_nregs == 0)
4148 break;
4149 /* XXX: compatible with arm only: ret_align == register_size */
4150 offset += ret_align;
4151 }
4152 vset(&s->type, VT_LOCAL | VT_LVAL, addr);
4153 }
4154 } else {
4155 break;
4156 }
4157 }
4158 }
4159
4160 ST_FUNC void expr_prod(void)
4161 {
4162 int t;
4163
4164 unary();
4165 while (tok == '*' || tok == '/' || tok == '%') {
4166 t = tok;
4167 next();
4168 unary();
4169 gen_op(t);
4170 }
4171 }
4172
4173 ST_FUNC void expr_sum(void)
4174 {
4175 int t;
4176
4177 expr_prod();
4178 while (tok == '+' || tok == '-') {
4179 t = tok;
4180 next();
4181 expr_prod();
4182 gen_op(t);
4183 }
4184 }
4185
4186 static void expr_shift(void)
4187 {
4188 int t;
4189
4190 expr_sum();
4191 while (tok == TOK_SHL || tok == TOK_SAR) {
4192 t = tok;
4193 next();
4194 expr_sum();
4195 gen_op(t);
4196 }
4197 }
4198
4199 static void expr_cmp(void)
4200 {
4201 int t;
4202
4203 expr_shift();
4204 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4205 tok == TOK_ULT || tok == TOK_UGE) {
4206 t = tok;
4207 next();
4208 expr_shift();
4209 gen_op(t);
4210 }
4211 }
4212
4213 static void expr_cmpeq(void)
4214 {
4215 int t;
4216
4217 expr_cmp();
4218 while (tok == TOK_EQ || tok == TOK_NE) {
4219 t = tok;
4220 next();
4221 expr_cmp();
4222 gen_op(t);
4223 }
4224 }
4225
4226 static void expr_and(void)
4227 {
4228 expr_cmpeq();
4229 while (tok == '&') {
4230 next();
4231 expr_cmpeq();
4232 gen_op('&');
4233 }
4234 }
4235
4236 static void expr_xor(void)
4237 {
4238 expr_and();
4239 while (tok == '^') {
4240 next();
4241 expr_and();
4242 gen_op('^');
4243 }
4244 }
4245
4246 static void expr_or(void)
4247 {
4248 expr_xor();
4249 while (tok == '|') {
4250 next();
4251 expr_xor();
4252 gen_op('|');
4253 }
4254 }
4255
4256 /* XXX: fix this mess */
4257 static void expr_land_const(void)
4258 {
4259 expr_or();
4260 while (tok == TOK_LAND) {
4261 next();
4262 expr_or();
4263 gen_op(TOK_LAND);
4264 }
4265 }
4266
4267 /* XXX: fix this mess */
4268 static void expr_lor_const(void)
4269 {
4270 expr_land_const();
4271 while (tok == TOK_LOR) {
4272 next();
4273 expr_land_const();
4274 gen_op(TOK_LOR);
4275 }
4276 }
4277
4278 /* only used if non constant */
4279 static void expr_land(void)
4280 {
4281 int t;
4282
4283 expr_or();
4284 if (tok == TOK_LAND) {
4285 t = 0;
4286 save_regs(1);
4287 for(;;) {
4288 t = gvtst(1, t);
4289 if (tok != TOK_LAND) {
4290 vseti(VT_JMPI, t);
4291 break;
4292 }
4293 next();
4294 expr_or();
4295 }
4296 }
4297 }
4298
4299 static void expr_lor(void)
4300 {
4301 int t;
4302
4303 expr_land();
4304 if (tok == TOK_LOR) {
4305 t = 0;
4306 save_regs(1);
4307 for(;;) {
4308 t = gvtst(0, t);
4309 if (tok != TOK_LOR) {
4310 vseti(VT_JMP, t);
4311 break;
4312 }
4313 next();
4314 expr_land();
4315 }
4316 }
4317 }
4318
4319 /* XXX: better constant handling */
4320 static void expr_cond(void)
4321 {
4322 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4323 SValue sv;
4324 CType type, type1, type2;
4325
4326 if (const_wanted) {
4327 expr_lor_const();
4328 if (tok == '?') {
4329 CType boolean;
4330 int c;
4331 boolean.t = VT_BOOL;
4332 vdup();
4333 gen_cast(&boolean);
4334 c = vtop->c.i;
4335 vpop();
4336 next();
4337 if (tok != ':' || !gnu_ext) {
4338 vpop();
4339 gexpr();
4340 }
4341 if (!c)
4342 vpop();
4343 skip(':');
4344 expr_cond();
4345 if (c)
4346 vpop();
4347 }
4348 } else {
4349 expr_lor();
4350 if (tok == '?') {
4351 next();
4352 if (vtop != vstack) {
4353 /* needed to avoid having different registers saved in
4354 each branch */
4355 if (is_float(vtop->type.t)) {
4356 rc = RC_FLOAT;
4357 #ifdef TCC_TARGET_X86_64
4358 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4359 rc = RC_ST0;
4360 }
4361 #endif
4362 }
4363 else
4364 rc = RC_INT;
4365 gv(rc);
4366 save_regs(1);
4367 }
4368 if (tok == ':' && gnu_ext) {
4369 gv_dup();
4370 tt = gvtst(1, 0);
4371 } else {
4372 tt = gvtst(1, 0);
4373 gexpr();
4374 }
4375 type1 = vtop->type;
4376 sv = *vtop; /* save value to handle it later */
4377 vtop--; /* no vpop so that FP stack is not flushed */
4378 skip(':');
4379 u = gjmp(0);
4380 gsym(tt);
4381 expr_cond();
4382 type2 = vtop->type;
4383
4384 t1 = type1.t;
4385 bt1 = t1 & VT_BTYPE;
4386 t2 = type2.t;
4387 bt2 = t2 & VT_BTYPE;
4388 /* cast operands to correct type according to ISOC rules */
4389 if (is_float(bt1) || is_float(bt2)) {
4390 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4391 type.t = VT_LDOUBLE;
4392 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4393 type.t = VT_DOUBLE;
4394 } else {
4395 type.t = VT_FLOAT;
4396 }
4397 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4398 /* cast to biggest op */
4399 type.t = VT_LLONG;
4400 /* convert to unsigned if it does not fit in a long long */
4401 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4402 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4403 type.t |= VT_UNSIGNED;
4404 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4405 /* If one is a null ptr constant the result type
4406 is the other. */
4407 if (is_null_pointer (vtop))
4408 type = type1;
4409 else if (is_null_pointer (&sv))
4410 type = type2;
4411 /* XXX: test pointer compatibility, C99 has more elaborate
4412 rules here. */
4413 else
4414 type = type1;
4415 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4416 /* XXX: test function pointer compatibility */
4417 type = bt1 == VT_FUNC ? type1 : type2;
4418 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4419 /* XXX: test structure compatibility */
4420 type = bt1 == VT_STRUCT ? type1 : type2;
4421 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4422 /* NOTE: as an extension, we accept void on only one side */
4423 type.t = VT_VOID;
4424 } else {
4425 /* integer operations */
4426 type.t = VT_INT;
4427 /* convert to unsigned if it does not fit in an integer */
4428 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4429 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4430 type.t |= VT_UNSIGNED;
4431 }
4432
4433 /* now we convert second operand */
4434 gen_cast(&type);
4435 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4436 gaddrof();
4437 rc = RC_INT;
4438 if (is_float(type.t)) {
4439 rc = RC_FLOAT;
4440 #ifdef TCC_TARGET_X86_64
4441 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4442 rc = RC_ST0;
4443 }
4444 #endif
4445 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4446 /* for long longs, we use fixed registers to avoid having
4447 to handle a complicated move */
4448 rc = RC_IRET;
4449 }
4450
4451 r2 = gv(rc);
4452 /* this is horrible, but we must also convert first
4453 operand */
4454 tt = gjmp(0);
4455 gsym(u);
4456 /* put again first value and cast it */
4457 *vtop = sv;
4458 gen_cast(&type);
4459 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4460 gaddrof();
4461 r1 = gv(rc);
4462 move_reg(r2, r1, type.t);
4463 vtop->r = r2;
4464 gsym(tt);
4465 }
4466 }
4467 }
4468
4469 static void expr_eq(void)
4470 {
4471 int t;
4472
4473 expr_cond();
4474 if (tok == '=' ||
4475 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4476 tok == TOK_A_XOR || tok == TOK_A_OR ||
4477 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4478 test_lvalue();
4479 t = tok;
4480 next();
4481 if (t == '=') {
4482 expr_eq();
4483 } else {
4484 vdup();
4485 expr_eq();
4486 gen_op(t & 0x7f);
4487 }
4488 vstore();
4489 }
4490 }
4491
4492 ST_FUNC void gexpr(void)
4493 {
4494 while (1) {
4495 expr_eq();
4496 if (tok != ',')
4497 break;
4498 vpop();
4499 next();
4500 }
4501 }
4502
4503 /* parse an expression and return its type without any side effect. */
4504 static void expr_type(CType *type)
4505 {
4506 int saved_nocode_wanted;
4507
4508 saved_nocode_wanted = nocode_wanted;
4509 nocode_wanted = 1;
4510 gexpr();
4511 *type = vtop->type;
4512 vpop();
4513 nocode_wanted = saved_nocode_wanted;
4514 }
4515
4516 /* parse a unary expression and return its type without any side
4517 effect. */
4518 static void unary_type(CType *type)
4519 {
4520 int a;
4521
4522 a = nocode_wanted;
4523 nocode_wanted = 1;
4524 unary();
4525 *type = vtop->type;
4526 vpop();
4527 nocode_wanted = a;
4528 }
4529
4530 /* parse a constant expression and return value in vtop. */
4531 static void expr_const1(void)
4532 {
4533 int a;
4534 a = const_wanted;
4535 const_wanted = 1;
4536 expr_cond();
4537 const_wanted = a;
4538 }
4539
4540 /* parse an integer constant and return its value. */
4541 ST_FUNC int expr_const(void)
4542 {
4543 int c;
4544 expr_const1();
4545 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4546 expect("constant expression");
4547 c = vtop->c.i;
4548 vpop();
4549 return c;
4550 }
4551
4552 /* return the label token if current token is a label, otherwise
4553 return zero */
4554 static int is_label(void)
4555 {
4556 int last_tok;
4557
4558 /* fast test first */
4559 if (tok < TOK_UIDENT)
4560 return 0;
4561 /* no need to save tokc because tok is an identifier */
4562 last_tok = tok;
4563 next();
4564 if (tok == ':') {
4565 next();
4566 return last_tok;
4567 } else {
4568 unget_tok(last_tok);
4569 return 0;
4570 }
4571 }
4572
4573 static void label_or_decl(int l)
4574 {
4575 int last_tok;
4576
4577 /* fast test first */
4578 if (tok >= TOK_UIDENT)
4579 {
4580 /* no need to save tokc because tok is an identifier */
4581 last_tok = tok;
4582 next();
4583 if (tok == ':') {
4584 unget_tok(last_tok);
4585 return;
4586 }
4587 unget_tok(last_tok);
4588 }
4589 decl(l);
4590 }
4591
4592 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4593 int case_reg, int is_expr)
4594 {
4595 int a, b, c, d;
4596 Sym *s, *frame_bottom;
4597
4598 /* generate line number info */
4599 if (tcc_state->do_debug &&
4600 (last_line_num != file->line_num || last_ind != ind)) {
4601 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4602 last_ind = ind;
4603 last_line_num = file->line_num;
4604 }
4605
4606 if (is_expr) {
4607 /* default return value is (void) */
4608 vpushi(0);
4609 vtop->type.t = VT_VOID;
4610 }
4611
4612 if (tok == TOK_IF) {
4613 /* if test */
4614 next();
4615 skip('(');
4616 gexpr();
4617 skip(')');
4618 a = gvtst(1, 0);
4619 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4620 c = tok;
4621 if (c == TOK_ELSE) {
4622 next();
4623 d = gjmp(0);
4624 gsym(a);
4625 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4626 gsym(d); /* patch else jmp */
4627 } else
4628 gsym(a);
4629 } else if (tok == TOK_WHILE) {
4630 next();
4631 d = ind;
4632 skip('(');
4633 gexpr();
4634 skip(')');
4635 a = gvtst(1, 0);
4636 b = 0;
4637 block(&a, &b, case_sym, def_sym, case_reg, 0);
4638 gjmp_addr(d);
4639 gsym(a);
4640 gsym_addr(b, d);
4641 } else if (tok == '{') {
4642 Sym *llabel;
4643 int block_vla_sp_loc, *saved_vla_sp_loc, saved_vla_flags;
4644
4645 next();
4646 /* record local declaration stack position */
4647 s = local_stack;
4648 frame_bottom = sym_push2(&local_stack, SYM_FIELD, 0, 0);
4649 frame_bottom->next = scope_stack_bottom;
4650 scope_stack_bottom = frame_bottom;
4651 llabel = local_label_stack;
4652
4653 /* save VLA state */
4654 block_vla_sp_loc = *(saved_vla_sp_loc = vla_sp_loc);
4655 if (saved_vla_sp_loc != &vla_sp_root_loc)
4656 vla_sp_loc = &block_vla_sp_loc;
4657
4658 saved_vla_flags = vla_flags;
4659 vla_flags |= VLA_NEED_NEW_FRAME;
4660
4661 /* handle local labels declarations */
4662 if (tok == TOK_LABEL) {
4663 next();
4664 for(;;) {
4665 if (tok < TOK_UIDENT)
4666 expect("label identifier");
4667 label_push(&local_label_stack, tok, LABEL_DECLARED);
4668 next();
4669 if (tok == ',') {
4670 next();
4671 } else {
4672 skip(';');
4673 break;
4674 }
4675 }
4676 }
4677 while (tok != '}') {
4678 label_or_decl(VT_LOCAL);
4679 if (tok != '}') {
4680 if (is_expr)
4681 vpop();
4682 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4683 }
4684 }
4685 /* pop locally defined labels */
4686 label_pop(&local_label_stack, llabel);
4687 if(is_expr) {
4688 /* XXX: this solution makes only valgrind happy...
4689 triggered by gcc.c-torture/execute/20000917-1.c */
4690 Sym *p;
4691 switch(vtop->type.t & VT_BTYPE) {
4692 case VT_PTR:
4693 case VT_STRUCT:
4694 case VT_ENUM:
4695 case VT_FUNC:
4696 for(p=vtop->type.ref;p;p=p->prev)
4697 if(p->prev==s)
4698 tcc_error("unsupported expression type");
4699 }
4700 }
4701 /* pop locally defined symbols */
4702 scope_stack_bottom = scope_stack_bottom->next;
4703 sym_pop(&local_stack, s);
4704
4705 /* Pop VLA frames and restore stack pointer if required */
4706 if (saved_vla_sp_loc != &vla_sp_root_loc)
4707 *saved_vla_sp_loc = block_vla_sp_loc;
4708 if (vla_sp_loc != (saved_vla_sp_loc == &vla_sp_root_loc ? &vla_sp_root_loc : &block_vla_sp_loc)) {
4709 vla_sp_loc = saved_vla_sp_loc;
4710 gen_vla_sp_restore(*vla_sp_loc);
4711 }
4712 vla_flags = (vla_flags & ~VLA_SCOPE_FLAGS) | (saved_vla_flags & VLA_SCOPE_FLAGS);
4713
4714 next();
4715 } else if (tok == TOK_RETURN) {
4716 next();
4717 if (tok != ';') {
4718 gexpr();
4719 gen_assign_cast(&func_vt);
4720 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4721 CType type, ret_type;
4722 int ret_align, ret_nregs;
4723 ret_nregs = gfunc_sret(&func_vt, func_var, &ret_type,
4724</