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