Added missing file.
[tinycc/k1w1.git] / tccgen.c
blobe258f3a069c7562c6628656a0f274615c9c39cdf
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
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.
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.
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
20 #include "tcc.h"
21 #include "tccpp.h"
23 void swap(int *p, int *q)
25 int t;
26 t = *p;
27 *p = *q;
28 *q = t;
31 void vsetc(CType *type, int r, CValue *vc)
33 int v;
35 if (vtop >= vstack + (VSTACK_SIZE - 1))
36 error("memory full");
37 /* cannot let cpu flags if other instruction are generated. Also
38 avoid leaving VT_JMP anywhere except on the top of the stack
39 because it would complicate the code generator. */
40 if (vtop >= vstack) {
41 v = vtop->r & VT_VALMASK;
42 if (v == VT_CMP || (v & ~1) == VT_JMP)
43 gv(RC_INT);
45 vtop++;
46 vtop->type = *type;
47 vtop->r = r;
48 vtop->r2 = VT_CONST;
49 vtop->c = *vc;
52 /* push integer constant */
53 void vpushi(int v)
55 CValue cval;
56 cval.i = v;
57 vsetc(&int_type, VT_CONST, &cval);
60 /* push long long constant */
61 void vpushll(long long v)
63 CValue cval;
64 CType ctype;
65 ctype.t = VT_LLONG;
66 cval.ull = v;
67 vsetc(&ctype, VT_CONST, &cval);
70 /* Return a static symbol pointing to a section */
71 Sym *get_sym_ref(CType *type, Section *sec,
72 unsigned long offset, unsigned long size)
74 int v;
75 Sym *sym;
77 v = anon_sym++;
78 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
79 sym->type.ref = type->ref;
80 sym->r = VT_CONST | VT_SYM;
81 put_extern_sym(sym, sec, offset, size);
82 return sym;
85 /* push a reference to a section offset by adding a dummy symbol */
86 static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
88 CValue cval;
90 cval.ul = 0;
91 vsetc(type, VT_CONST | VT_SYM, &cval);
92 vtop->sym = get_sym_ref(type, sec, offset, size);
95 /* define a new external reference to a symbol 'v' of type 'u' */
96 Sym *external_global_sym(int v, CType *type, int r)
98 Sym *s;
100 s = sym_find(v);
101 if (!s) {
102 /* push forward reference */
103 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
104 s->type.ref = type->ref;
105 s->r = r | VT_CONST | VT_SYM;
107 return s;
110 /* define a new external reference to a symbol 'v' of type 'u' */
111 static Sym *external_sym(int v, CType *type, int r)
113 Sym *s;
115 s = sym_find(v);
116 if (!s) {
117 /* push forward reference */
118 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
119 s->type.t |= VT_EXTERN;
120 } else if (s->type.ref == func_old_type.ref) {
121 s->type.ref = type->ref;
122 s->r = r | VT_CONST | VT_SYM;
123 s->type.t |= VT_EXTERN;
124 } else if (!is_compatible_types(&s->type, type)) {
125 error("incompatible types for redefinition of '%s'",
126 get_tok_str(v, NULL));
128 return s;
131 /* push a reference to global symbol v */
132 static void vpush_global_sym(CType *type, int v)
134 Sym *sym;
135 CValue cval;
137 sym = external_global_sym(v, type, 0);
138 cval.ul = 0;
139 vsetc(type, VT_CONST | VT_SYM, &cval);
140 vtop->sym = sym;
143 void vset(CType *type, int r, int v)
145 CValue cval;
147 cval.i = v;
148 vsetc(type, r, &cval);
151 void vseti(int r, int v)
153 CType type;
154 type.t = VT_INT;
155 vset(&type, r, v);
158 void vswap(void)
160 SValue tmp;
162 tmp = vtop[0];
163 vtop[0] = vtop[-1];
164 vtop[-1] = tmp;
167 void vpushv(SValue *v)
169 if (vtop >= vstack + (VSTACK_SIZE - 1))
170 error("memory full");
171 vtop++;
172 *vtop = *v;
175 void vdup(void)
177 vpushv(vtop);
180 /* save r to the memory stack, and mark it as being free */
181 void save_reg(int r)
183 int l, saved, size, align;
184 SValue *p, sv;
185 CType *type;
187 /* modify all stack values */
188 saved = 0;
189 l = 0;
190 for(p=vstack;p<=vtop;p++) {
191 if ((p->r & VT_VALMASK) == r ||
192 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
193 /* must save value on stack if not already done */
194 if (!saved) {
195 /* NOTE: must reload 'r' because r might be equal to r2 */
196 r = p->r & VT_VALMASK;
197 /* store register in the stack */
198 type = &p->type;
199 if ((p->r & VT_LVAL) ||
200 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
201 #ifdef TCC_TARGET_X86_64
202 type = &char_pointer_type;
203 #else
204 type = &int_type;
205 #endif
206 size = type_size(type, &align);
207 loc = (loc - size) & -align;
208 sv.type.t = type->t;
209 sv.r = VT_LOCAL | VT_LVAL;
210 sv.c.ul = loc;
211 store(r, &sv);
212 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
213 /* x86 specific: need to pop fp register ST0 if saved */
214 if (r == TREG_ST0) {
215 o(0xd8dd); /* fstp %st(0) */
217 #endif
218 #ifndef TCC_TARGET_X86_64
219 /* special long long case */
220 if ((type->t & VT_BTYPE) == VT_LLONG) {
221 sv.c.ul += 4;
222 store(p->r2, &sv);
224 #endif
225 l = loc;
226 saved = 1;
228 /* mark that stack entry as being saved on the stack */
229 if (p->r & VT_LVAL) {
230 /* also clear the bounded flag because the
231 relocation address of the function was stored in
232 p->c.ul */
233 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
234 } else {
235 p->r = lvalue_type(p->type.t) | VT_LOCAL;
237 p->r2 = VT_CONST;
238 p->c.ul = l;
243 /* find a register of class 'rc2' with at most one reference on stack.
244 * If none, call get_reg(rc) */
245 int get_reg_ex(int rc, int rc2)
247 int r;
248 SValue *p;
250 for(r=0;r<NB_REGS;r++) {
251 if (reg_classes[r] & rc2) {
252 int n;
253 n=0;
254 for(p = vstack; p <= vtop; p++) {
255 if ((p->r & VT_VALMASK) == r ||
256 (p->r2 & VT_VALMASK) == r)
257 n++;
259 if (n <= 1)
260 return r;
263 return get_reg(rc);
266 /* find a free register of class 'rc'. If none, save one register */
267 int get_reg(int rc)
269 int r;
270 SValue *p;
272 /* find a free register */
273 for(r=0;r<NB_REGS;r++) {
274 if (reg_classes[r] & rc) {
275 for(p=vstack;p<=vtop;p++) {
276 if ((p->r & VT_VALMASK) == r ||
277 (p->r2 & VT_VALMASK) == r)
278 goto notfound;
280 return r;
282 notfound: ;
285 /* no register left : free the first one on the stack (VERY
286 IMPORTANT to start from the bottom to ensure that we don't
287 spill registers used in gen_opi()) */
288 for(p=vstack;p<=vtop;p++) {
289 r = p->r & VT_VALMASK;
290 if (r < VT_CONST && (reg_classes[r] & rc))
291 goto save_found;
292 /* also look at second register (if long long) */
293 r = p->r2 & VT_VALMASK;
294 if (r < VT_CONST && (reg_classes[r] & rc)) {
295 save_found:
296 save_reg(r);
297 return r;
300 /* Should never comes here */
301 return -1;
304 /* save registers up to (vtop - n) stack entry */
305 void save_regs(int n)
307 int r;
308 SValue *p, *p1;
309 p1 = vtop - n;
310 for(p = vstack;p <= p1; p++) {
311 r = p->r & VT_VALMASK;
312 if (r < VT_CONST) {
313 save_reg(r);
318 /* move register 's' to 'r', and flush previous value of r to memory
319 if needed */
320 void move_reg(int r, int s)
322 SValue sv;
324 if (r != s) {
325 save_reg(r);
326 sv.type.t = VT_INT;
327 sv.r = s;
328 sv.c.ul = 0;
329 load(r, &sv);
333 /* get address of vtop (vtop MUST BE an lvalue) */
334 void gaddrof(void)
336 vtop->r &= ~VT_LVAL;
337 /* tricky: if saved lvalue, then we can go back to lvalue */
338 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
339 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
342 #ifdef CONFIG_TCC_BCHECK
343 /* generate lvalue bound code */
344 void gbound(void)
346 int lval_type;
347 CType type1;
349 vtop->r &= ~VT_MUSTBOUND;
350 /* if lvalue, then use checking code before dereferencing */
351 if (vtop->r & VT_LVAL) {
352 /* if not VT_BOUNDED value, then make one */
353 if (!(vtop->r & VT_BOUNDED)) {
354 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
355 /* must save type because we must set it to int to get pointer */
356 type1 = vtop->type;
357 vtop->type.t = VT_INT;
358 gaddrof();
359 vpushi(0);
360 gen_bounded_ptr_add();
361 vtop->r |= lval_type;
362 vtop->type = type1;
364 /* then check for dereferencing */
365 gen_bounded_ptr_deref();
368 #endif
370 /* store vtop a register belonging to class 'rc'. lvalues are
371 converted to values. Cannot be used if cannot be converted to
372 register value (such as structures). */
373 int gv(int rc)
375 int r, rc2, bit_pos, bit_size, size, align, i;
377 /* NOTE: get_reg can modify vstack[] */
378 if (vtop->type.t & VT_BITFIELD) {
379 CType type;
380 int bits = 32;
381 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
382 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
383 /* remove bit field info to avoid loops */
384 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
385 /* cast to int to propagate signedness in following ops */
386 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
387 type.t = VT_LLONG;
388 bits = 64;
389 } else
390 type.t = VT_INT;
391 if((vtop->type.t & VT_UNSIGNED) ||
392 (vtop->type.t & VT_BTYPE) == VT_BOOL)
393 type.t |= VT_UNSIGNED;
394 gen_cast(&type);
395 /* generate shifts */
396 vpushi(bits - (bit_pos + bit_size));
397 gen_op(TOK_SHL);
398 vpushi(bits - bit_size);
399 /* NOTE: transformed to SHR if unsigned */
400 gen_op(TOK_SAR);
401 r = gv(rc);
402 } else {
403 if (is_float(vtop->type.t) &&
404 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
405 Sym *sym;
406 int *ptr;
407 unsigned long offset;
408 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
409 CValue check;
410 #endif
412 /* XXX: unify with initializers handling ? */
413 /* CPUs usually cannot use float constants, so we store them
414 generically in data segment */
415 size = type_size(&vtop->type, &align);
416 offset = (data_section->data_offset + align - 1) & -align;
417 data_section->data_offset = offset;
418 /* XXX: not portable yet */
419 #if defined(__i386__) || defined(__x86_64__)
420 /* Zero pad x87 tenbyte long doubles */
421 if (size == LDOUBLE_SIZE)
422 vtop->c.tab[2] &= 0xffff;
423 #endif
424 ptr = section_ptr_add(data_section, size);
425 size = size >> 2;
426 #if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
427 check.d = 1;
428 if(check.tab[0])
429 for(i=0;i<size;i++)
430 ptr[i] = vtop->c.tab[size-1-i];
431 else
432 #endif
433 for(i=0;i<size;i++)
434 ptr[i] = vtop->c.tab[i];
435 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
436 vtop->r |= VT_LVAL | VT_SYM;
437 vtop->sym = sym;
438 vtop->c.ul = 0;
440 #ifdef CONFIG_TCC_BCHECK
441 if (vtop->r & VT_MUSTBOUND)
442 gbound();
443 #endif
445 r = vtop->r & VT_VALMASK;
446 rc2 = RC_INT;
447 if (rc == RC_IRET)
448 rc2 = RC_LRET;
449 /* need to reload if:
450 - constant
451 - lvalue (need to dereference pointer)
452 - already a register, but not in the right class */
453 if (r >= VT_CONST ||
454 (vtop->r & VT_LVAL) ||
455 !(reg_classes[r] & rc) ||
456 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
457 !(reg_classes[vtop->r2] & rc2))) {
458 r = get_reg(rc);
459 #ifndef TCC_TARGET_X86_64
460 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
461 int r2;
462 unsigned long long ll;
463 /* two register type load : expand to two words
464 temporarily */
465 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
466 /* load constant */
467 ll = vtop->c.ull;
468 vtop->c.ui = ll; /* first word */
469 load(r, vtop);
470 vtop->r = r; /* save register value */
471 vpushi(ll >> 32); /* second word */
472 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
473 (vtop->r & VT_LVAL)) {
474 /* We do not want to modifier the long long
475 pointer here, so the safest (and less
476 efficient) is to save all the other registers
477 in the stack. XXX: totally inefficient. */
478 save_regs(1);
479 /* load from memory */
480 load(r, vtop);
481 vdup();
482 vtop[-1].r = r; /* save register value */
483 /* increment pointer to get second word */
484 vtop->type.t = VT_INT;
485 gaddrof();
486 vpushi(4);
487 gen_op('+');
488 vtop->r |= VT_LVAL;
489 } else {
490 /* move registers */
491 load(r, vtop);
492 vdup();
493 vtop[-1].r = r; /* save register value */
494 vtop->r = vtop[-1].r2;
496 /* allocate second register */
497 r2 = get_reg(rc2);
498 load(r2, vtop);
499 vpop();
500 /* write second register */
501 vtop->r2 = r2;
502 } else
503 #endif
504 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
505 int t1, t;
506 /* lvalue of scalar type : need to use lvalue type
507 because of possible cast */
508 t = vtop->type.t;
509 t1 = t;
510 /* compute memory access type */
511 if (vtop->r & VT_LVAL_BYTE)
512 t = VT_BYTE;
513 else if (vtop->r & VT_LVAL_SHORT)
514 t = VT_SHORT;
515 if (vtop->r & VT_LVAL_UNSIGNED)
516 t |= VT_UNSIGNED;
517 vtop->type.t = t;
518 load(r, vtop);
519 /* restore wanted type */
520 vtop->type.t = t1;
521 } else {
522 /* one register type load */
523 load(r, vtop);
526 vtop->r = r;
527 #ifdef TCC_TARGET_C67
528 /* uses register pairs for doubles */
529 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
530 vtop->r2 = r+1;
531 #endif
533 return r;
536 /* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
537 void gv2(int rc1, int rc2)
539 int v;
541 /* generate more generic register first. But VT_JMP or VT_CMP
542 values must be generated first in all cases to avoid possible
543 reload errors */
544 v = vtop[0].r & VT_VALMASK;
545 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
546 vswap();
547 gv(rc1);
548 vswap();
549 gv(rc2);
550 /* test if reload is needed for first register */
551 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
552 vswap();
553 gv(rc1);
554 vswap();
556 } else {
557 gv(rc2);
558 vswap();
559 gv(rc1);
560 vswap();
561 /* test if reload is needed for first register */
562 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
563 gv(rc2);
568 /* wrapper around RC_FRET to return a register by type */
569 int rc_fret(int t)
571 #ifdef TCC_TARGET_X86_64
572 if (t == VT_LDOUBLE) {
573 return RC_ST0;
575 #endif
576 return RC_FRET;
579 /* wrapper around REG_FRET to return a register by type */
580 int reg_fret(int t)
582 #ifdef TCC_TARGET_X86_64
583 if (t == VT_LDOUBLE) {
584 return TREG_ST0;
586 #endif
587 return REG_FRET;
590 /* expand long long on stack in two int registers */
591 void lexpand(void)
593 int u;
595 u = vtop->type.t & VT_UNSIGNED;
596 gv(RC_INT);
597 vdup();
598 vtop[0].r = vtop[-1].r2;
599 vtop[0].r2 = VT_CONST;
600 vtop[-1].r2 = VT_CONST;
601 vtop[0].type.t = VT_INT | u;
602 vtop[-1].type.t = VT_INT | u;
605 #ifdef TCC_TARGET_ARM
606 /* expand long long on stack */
607 void lexpand_nr(void)
609 int u,v;
611 u = vtop->type.t & VT_UNSIGNED;
612 vdup();
613 vtop->r2 = VT_CONST;
614 vtop->type.t = VT_INT | u;
615 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
616 if (v == VT_CONST) {
617 vtop[-1].c.ui = vtop->c.ull;
618 vtop->c.ui = vtop->c.ull >> 32;
619 vtop->r = VT_CONST;
620 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
621 vtop->c.ui += 4;
622 vtop->r = vtop[-1].r;
623 } else if (v > VT_CONST) {
624 vtop--;
625 lexpand();
626 } else
627 vtop->r = vtop[-1].r2;
628 vtop[-1].r2 = VT_CONST;
629 vtop[-1].type.t = VT_INT | u;
631 #endif
633 /* build a long long from two ints */
634 void lbuild(int t)
636 gv2(RC_INT, RC_INT);
637 vtop[-1].r2 = vtop[0].r;
638 vtop[-1].type.t = t;
639 vpop();
642 /* rotate n first stack elements to the bottom
643 I1 ... In -> I2 ... In I1 [top is right]
645 void vrotb(int n)
647 int i;
648 SValue tmp;
650 tmp = vtop[-n + 1];
651 for(i=-n+1;i!=0;i++)
652 vtop[i] = vtop[i+1];
653 vtop[0] = tmp;
656 /* rotate n first stack elements to the top
657 I1 ... In -> In I1 ... I(n-1) [top is right]
659 void vrott(int n)
661 int i;
662 SValue tmp;
664 tmp = vtop[0];
665 for(i = 0;i < n - 1; i++)
666 vtop[-i] = vtop[-i - 1];
667 vtop[-n + 1] = tmp;
670 #ifdef TCC_TARGET_ARM
671 /* like vrott but in other direction
672 In ... I1 -> I(n-1) ... I1 In [top is right]
674 void vnrott(int n)
676 int i;
677 SValue tmp;
679 tmp = vtop[-n + 1];
680 for(i = n - 1; i > 0; i--)
681 vtop[-i] = vtop[-i + 1];
682 vtop[0] = tmp;
684 #endif
686 /* pop stack value */
687 void vpop(void)
689 int v;
690 v = vtop->r & VT_VALMASK;
691 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
692 /* for x86, we need to pop the FP stack */
693 if (v == TREG_ST0 && !nocode_wanted) {
694 o(0xd8dd); /* fstp %st(0) */
695 } else
696 #endif
697 if (v == VT_JMP || v == VT_JMPI) {
698 /* need to put correct jump if && or || without test */
699 gsym(vtop->c.ul);
701 vtop--;
704 /* convert stack entry to register and duplicate its value in another
705 register */
706 void gv_dup(void)
708 int rc, t, r, r1;
709 SValue sv;
711 t = vtop->type.t;
712 if ((t & VT_BTYPE) == VT_LLONG) {
713 lexpand();
714 gv_dup();
715 vswap();
716 vrotb(3);
717 gv_dup();
718 vrotb(4);
719 /* stack: H L L1 H1 */
720 lbuild(t);
721 vrotb(3);
722 vrotb(3);
723 vswap();
724 lbuild(t);
725 vswap();
726 } else {
727 /* duplicate value */
728 rc = RC_INT;
729 sv.type.t = VT_INT;
730 if (is_float(t)) {
731 rc = RC_FLOAT;
732 #ifdef TCC_TARGET_X86_64
733 if ((t & VT_BTYPE) == VT_LDOUBLE) {
734 rc = RC_ST0;
736 #endif
737 sv.type.t = t;
739 r = gv(rc);
740 r1 = get_reg(rc);
741 sv.r = r;
742 sv.c.ul = 0;
743 load(r1, &sv); /* move r to r1 */
744 vdup();
745 /* duplicates value */
746 if (r != r1)
747 vtop->r = r1;
751 #ifndef TCC_TARGET_X86_64
752 /* generate CPU independent (unsigned) long long operations */
753 void gen_opl(int op)
755 int t, a, b, op1, c, i;
756 int func;
757 unsigned short reg_iret = REG_IRET;
758 unsigned short reg_lret = REG_LRET;
759 SValue tmp;
761 switch(op) {
762 case '/':
763 case TOK_PDIV:
764 func = TOK___divdi3;
765 goto gen_func;
766 case TOK_UDIV:
767 func = TOK___udivdi3;
768 goto gen_func;
769 case '%':
770 func = TOK___moddi3;
771 goto gen_mod_func;
772 case TOK_UMOD:
773 func = TOK___umoddi3;
774 gen_mod_func:
775 #ifdef TCC_ARM_EABI
776 reg_iret = TREG_R2;
777 reg_lret = TREG_R3;
778 #endif
779 gen_func:
780 /* call generic long long function */
781 vpush_global_sym(&func_old_type, func);
782 vrott(3);
783 gfunc_call(2);
784 vpushi(0);
785 vtop->r = reg_iret;
786 vtop->r2 = reg_lret;
787 break;
788 case '^':
789 case '&':
790 case '|':
791 case '*':
792 case '+':
793 case '-':
794 t = vtop->type.t;
795 vswap();
796 lexpand();
797 vrotb(3);
798 lexpand();
799 /* stack: L1 H1 L2 H2 */
800 tmp = vtop[0];
801 vtop[0] = vtop[-3];
802 vtop[-3] = tmp;
803 tmp = vtop[-2];
804 vtop[-2] = vtop[-3];
805 vtop[-3] = tmp;
806 vswap();
807 /* stack: H1 H2 L1 L2 */
808 if (op == '*') {
809 vpushv(vtop - 1);
810 vpushv(vtop - 1);
811 gen_op(TOK_UMULL);
812 lexpand();
813 /* stack: H1 H2 L1 L2 ML MH */
814 for(i=0;i<4;i++)
815 vrotb(6);
816 /* stack: ML MH H1 H2 L1 L2 */
817 tmp = vtop[0];
818 vtop[0] = vtop[-2];
819 vtop[-2] = tmp;
820 /* stack: ML MH H1 L2 H2 L1 */
821 gen_op('*');
822 vrotb(3);
823 vrotb(3);
824 gen_op('*');
825 /* stack: ML MH M1 M2 */
826 gen_op('+');
827 gen_op('+');
828 } else if (op == '+' || op == '-') {
829 /* XXX: add non carry method too (for MIPS or alpha) */
830 if (op == '+')
831 op1 = TOK_ADDC1;
832 else
833 op1 = TOK_SUBC1;
834 gen_op(op1);
835 /* stack: H1 H2 (L1 op L2) */
836 vrotb(3);
837 vrotb(3);
838 gen_op(op1 + 1); /* TOK_xxxC2 */
839 } else {
840 gen_op(op);
841 /* stack: H1 H2 (L1 op L2) */
842 vrotb(3);
843 vrotb(3);
844 /* stack: (L1 op L2) H1 H2 */
845 gen_op(op);
846 /* stack: (L1 op L2) (H1 op H2) */
848 /* stack: L H */
849 lbuild(t);
850 break;
851 case TOK_SAR:
852 case TOK_SHR:
853 case TOK_SHL:
854 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
855 t = vtop[-1].type.t;
856 vswap();
857 lexpand();
858 vrotb(3);
859 /* stack: L H shift */
860 c = (int)vtop->c.i;
861 /* constant: simpler */
862 /* NOTE: all comments are for SHL. the other cases are
863 done by swaping words */
864 vpop();
865 if (op != TOK_SHL)
866 vswap();
867 if (c >= 32) {
868 /* stack: L H */
869 vpop();
870 if (c > 32) {
871 vpushi(c - 32);
872 gen_op(op);
874 if (op != TOK_SAR) {
875 vpushi(0);
876 } else {
877 gv_dup();
878 vpushi(31);
879 gen_op(TOK_SAR);
881 vswap();
882 } else {
883 vswap();
884 gv_dup();
885 /* stack: H L L */
886 vpushi(c);
887 gen_op(op);
888 vswap();
889 vpushi(32 - c);
890 if (op == TOK_SHL)
891 gen_op(TOK_SHR);
892 else
893 gen_op(TOK_SHL);
894 vrotb(3);
895 /* stack: L L H */
896 vpushi(c);
897 if (op == TOK_SHL)
898 gen_op(TOK_SHL);
899 else
900 gen_op(TOK_SHR);
901 gen_op('|');
903 if (op != TOK_SHL)
904 vswap();
905 lbuild(t);
906 } else {
907 /* XXX: should provide a faster fallback on x86 ? */
908 switch(op) {
909 case TOK_SAR:
910 func = TOK___ashrdi3;
911 goto gen_func;
912 case TOK_SHR:
913 func = TOK___lshrdi3;
914 goto gen_func;
915 case TOK_SHL:
916 func = TOK___ashldi3;
917 goto gen_func;
920 break;
921 default:
922 /* compare operations */
923 t = vtop->type.t;
924 vswap();
925 lexpand();
926 vrotb(3);
927 lexpand();
928 /* stack: L1 H1 L2 H2 */
929 tmp = vtop[-1];
930 vtop[-1] = vtop[-2];
931 vtop[-2] = tmp;
932 /* stack: L1 L2 H1 H2 */
933 /* compare high */
934 op1 = op;
935 /* when values are equal, we need to compare low words. since
936 the jump is inverted, we invert the test too. */
937 if (op1 == TOK_LT)
938 op1 = TOK_LE;
939 else if (op1 == TOK_GT)
940 op1 = TOK_GE;
941 else if (op1 == TOK_ULT)
942 op1 = TOK_ULE;
943 else if (op1 == TOK_UGT)
944 op1 = TOK_UGE;
945 a = 0;
946 b = 0;
947 gen_op(op1);
948 if (op1 != TOK_NE) {
949 a = gtst(1, 0);
951 if (op != TOK_EQ) {
952 /* generate non equal test */
953 /* XXX: NOT PORTABLE yet */
954 if (a == 0) {
955 b = gtst(0, 0);
956 } else {
957 #if defined(TCC_TARGET_I386)
958 b = psym(0x850f, 0);
959 #elif defined(TCC_TARGET_ARM)
960 b = ind;
961 o(0x1A000000 | encbranch(ind, 0, 1));
962 #elif defined(TCC_TARGET_C67)
963 error("not implemented");
964 #else
965 #error not supported
966 #endif
969 /* compare low. Always unsigned */
970 op1 = op;
971 if (op1 == TOK_LT)
972 op1 = TOK_ULT;
973 else if (op1 == TOK_LE)
974 op1 = TOK_ULE;
975 else if (op1 == TOK_GT)
976 op1 = TOK_UGT;
977 else if (op1 == TOK_GE)
978 op1 = TOK_UGE;
979 gen_op(op1);
980 a = gtst(1, a);
981 gsym(b);
982 vseti(VT_JMPI, a);
983 break;
986 #endif
988 /* handle integer constant optimizations and various machine
989 independent opt */
990 void gen_opic(int op)
992 int c1, c2, t1, t2, n;
993 SValue *v1, *v2;
994 long long l1, l2;
995 typedef unsigned long long U;
997 v1 = vtop - 1;
998 v2 = vtop;
999 t1 = v1->type.t & VT_BTYPE;
1000 t2 = v2->type.t & VT_BTYPE;
1002 if (t1 == VT_LLONG)
1003 l1 = v1->c.ll;
1004 else if (v1->type.t & VT_UNSIGNED)
1005 l1 = v1->c.ui;
1006 else
1007 l1 = v1->c.i;
1009 if (t2 == VT_LLONG)
1010 l2 = v2->c.ll;
1011 else if (v2->type.t & VT_UNSIGNED)
1012 l2 = v2->c.ui;
1013 else
1014 l2 = v2->c.i;
1016 /* currently, we cannot do computations with forward symbols */
1017 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1018 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1019 if (c1 && c2) {
1020 switch(op) {
1021 case '+': l1 += l2; break;
1022 case '-': l1 -= l2; break;
1023 case '&': l1 &= l2; break;
1024 case '^': l1 ^= l2; break;
1025 case '|': l1 |= l2; break;
1026 case '*': l1 *= l2; break;
1028 case TOK_PDIV:
1029 case '/':
1030 case '%':
1031 case TOK_UDIV:
1032 case TOK_UMOD:
1033 /* if division by zero, generate explicit division */
1034 if (l2 == 0) {
1035 if (const_wanted)
1036 error("division by zero in constant");
1037 goto general_case;
1039 switch(op) {
1040 default: l1 /= l2; break;
1041 case '%': l1 %= l2; break;
1042 case TOK_UDIV: l1 = (U)l1 / l2; break;
1043 case TOK_UMOD: l1 = (U)l1 % l2; break;
1045 break;
1046 case TOK_SHL: l1 <<= l2; break;
1047 case TOK_SHR: l1 = (U)l1 >> l2; break;
1048 case TOK_SAR: l1 >>= l2; break;
1049 /* tests */
1050 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
1051 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
1052 case TOK_EQ: l1 = l1 == l2; break;
1053 case TOK_NE: l1 = l1 != l2; break;
1054 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
1055 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
1056 case TOK_LT: l1 = l1 < l2; break;
1057 case TOK_GE: l1 = l1 >= l2; break;
1058 case TOK_LE: l1 = l1 <= l2; break;
1059 case TOK_GT: l1 = l1 > l2; break;
1060 /* logical */
1061 case TOK_LAND: l1 = l1 && l2; break;
1062 case TOK_LOR: l1 = l1 || l2; break;
1063 default:
1064 goto general_case;
1066 v1->c.ll = l1;
1067 vtop--;
1068 } else {
1069 /* if commutative ops, put c2 as constant */
1070 if (c1 && (op == '+' || op == '&' || op == '^' ||
1071 op == '|' || op == '*')) {
1072 vswap();
1073 c2 = c1; //c = c1, c1 = c2, c2 = c;
1074 l2 = l1; //l = l1, l1 = l2, l2 = l;
1076 /* Filter out NOP operations like x*1, x-0, x&-1... */
1077 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
1078 op == TOK_PDIV) &&
1079 l2 == 1) ||
1080 ((op == '+' || op == '-' || op == '|' || op == '^' ||
1081 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
1082 l2 == 0) ||
1083 (op == '&' &&
1084 l2 == -1))) {
1085 /* nothing to do */
1086 vtop--;
1087 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
1088 /* try to use shifts instead of muls or divs */
1089 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
1090 n = -1;
1091 while (l2) {
1092 l2 >>= 1;
1093 n++;
1095 vtop->c.ll = n;
1096 if (op == '*')
1097 op = TOK_SHL;
1098 else if (op == TOK_PDIV)
1099 op = TOK_SAR;
1100 else
1101 op = TOK_SHR;
1103 goto general_case;
1104 } else if (c2 && (op == '+' || op == '-') &&
1105 (((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM)
1106 && !(vtop[-1].sym->type.t & VT_IMPORT))
1108 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
1109 /* symbol + constant case */
1110 if (op == '-')
1111 l2 = -l2;
1112 vtop--;
1113 vtop->c.ll += l2;
1114 } else {
1115 general_case:
1116 if (!nocode_wanted) {
1117 /* call low level op generator */
1118 if (t1 == VT_LLONG || t2 == VT_LLONG)
1119 gen_opl(op);
1120 else
1121 gen_opi(op);
1122 } else {
1123 vtop--;
1129 /* generate a floating point operation with constant propagation */
1130 void gen_opif(int op)
1132 int c1, c2;
1133 SValue *v1, *v2;
1134 long double f1, f2;
1136 v1 = vtop - 1;
1137 v2 = vtop;
1138 /* currently, we cannot do computations with forward symbols */
1139 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1140 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1141 if (c1 && c2) {
1142 if (v1->type.t == VT_FLOAT) {
1143 f1 = v1->c.f;
1144 f2 = v2->c.f;
1145 } else if (v1->type.t == VT_DOUBLE) {
1146 f1 = v1->c.d;
1147 f2 = v2->c.d;
1148 } else {
1149 f1 = v1->c.ld;
1150 f2 = v2->c.ld;
1153 /* NOTE: we only do constant propagation if finite number (not
1154 NaN or infinity) (ANSI spec) */
1155 if (!ieee_finite(f1) || !ieee_finite(f2))
1156 goto general_case;
1158 switch(op) {
1159 case '+': f1 += f2; break;
1160 case '-': f1 -= f2; break;
1161 case '*': f1 *= f2; break;
1162 case '/':
1163 if (f2 == 0.0) {
1164 if (const_wanted)
1165 error("division by zero in constant");
1166 goto general_case;
1168 f1 /= f2;
1169 break;
1170 /* XXX: also handles tests ? */
1171 default:
1172 goto general_case;
1174 /* XXX: overflow test ? */
1175 if (v1->type.t == VT_FLOAT) {
1176 v1->c.f = f1;
1177 } else if (v1->type.t == VT_DOUBLE) {
1178 v1->c.d = f1;
1179 } else {
1180 v1->c.ld = f1;
1182 vtop--;
1183 } else {
1184 general_case:
1185 if (!nocode_wanted) {
1186 gen_opf(op);
1187 } else {
1188 vtop--;
1193 static int pointed_size(CType *type)
1195 int align;
1196 return type_size(pointed_type(type), &align);
1199 static inline int is_null_pointer(SValue *p)
1201 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
1202 return 0;
1203 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
1204 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
1207 static inline int is_integer_btype(int bt)
1209 return (bt == VT_BYTE || bt == VT_SHORT ||
1210 bt == VT_INT || bt == VT_LLONG);
1213 /* check types for comparison or substraction of pointers */
1214 static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
1216 CType *type1, *type2, tmp_type1, tmp_type2;
1217 int bt1, bt2;
1219 /* null pointers are accepted for all comparisons as gcc */
1220 if (is_null_pointer(p1) || is_null_pointer(p2))
1221 return;
1222 type1 = &p1->type;
1223 type2 = &p2->type;
1224 bt1 = type1->t & VT_BTYPE;
1225 bt2 = type2->t & VT_BTYPE;
1226 /* accept comparison between pointer and integer with a warning */
1227 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
1228 if (op != TOK_LOR && op != TOK_LAND )
1229 warning("comparison between pointer and integer");
1230 return;
1233 /* both must be pointers or implicit function pointers */
1234 if (bt1 == VT_PTR) {
1235 type1 = pointed_type(type1);
1236 } else if (bt1 != VT_FUNC)
1237 goto invalid_operands;
1239 if (bt2 == VT_PTR) {
1240 type2 = pointed_type(type2);
1241 } else if (bt2 != VT_FUNC) {
1242 invalid_operands:
1243 error("invalid operands to binary %s", get_tok_str(op, NULL));
1245 if ((type1->t & VT_BTYPE) == VT_VOID ||
1246 (type2->t & VT_BTYPE) == VT_VOID)
1247 return;
1248 tmp_type1 = *type1;
1249 tmp_type2 = *type2;
1250 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1251 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1252 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1253 /* gcc-like error if '-' is used */
1254 if (op == '-')
1255 goto invalid_operands;
1256 else
1257 warning("comparison of distinct pointer types lacks a cast");
1261 /* generic gen_op: handles types problems */
1262 void gen_op(int op)
1264 int u, t1, t2, bt1, bt2, t;
1265 CType type1;
1267 t1 = vtop[-1].type.t;
1268 t2 = vtop[0].type.t;
1269 bt1 = t1 & VT_BTYPE;
1270 bt2 = t2 & VT_BTYPE;
1272 if (bt1 == VT_PTR || bt2 == VT_PTR) {
1273 /* at least one operand is a pointer */
1274 /* relationnal op: must be both pointers */
1275 if (op >= TOK_ULT && op <= TOK_LOR) {
1276 check_comparison_pointer_types(vtop - 1, vtop, op);
1277 /* pointers are handled are unsigned */
1278 #ifdef TCC_TARGET_X86_64
1279 t = VT_LLONG | VT_UNSIGNED;
1280 #else
1281 t = VT_INT | VT_UNSIGNED;
1282 #endif
1283 goto std_op;
1285 /* if both pointers, then it must be the '-' op */
1286 if (bt1 == VT_PTR && bt2 == VT_PTR) {
1287 if (op != '-')
1288 error("cannot use pointers here");
1289 check_comparison_pointer_types(vtop - 1, vtop, op);
1290 /* XXX: check that types are compatible */
1291 u = pointed_size(&vtop[-1].type);
1292 gen_opic(op);
1293 /* set to integer type */
1294 #ifdef TCC_TARGET_X86_64
1295 vtop->type.t = VT_LLONG;
1296 #else
1297 vtop->type.t = VT_INT;
1298 #endif
1299 vpushi(u);
1300 gen_op(TOK_PDIV);
1301 } else {
1302 /* exactly one pointer : must be '+' or '-'. */
1303 if (op != '-' && op != '+')
1304 error("cannot use pointers here");
1305 /* Put pointer as first operand */
1306 if (bt2 == VT_PTR) {
1307 vswap();
1308 swap(&t1, &t2);
1310 type1 = vtop[-1].type;
1311 type1.t &= ~VT_ARRAY;
1312 #ifdef TCC_TARGET_X86_64
1313 vpushll(pointed_size(&vtop[-1].type));
1314 #else
1315 /* XXX: cast to int ? (long long case) */
1316 vpushi(pointed_size(&vtop[-1].type));
1317 #endif
1318 gen_op('*');
1319 #ifdef CONFIG_TCC_BCHECK
1320 /* if evaluating constant expression, no code should be
1321 generated, so no bound check */
1322 if (tcc_state->do_bounds_check && !const_wanted) {
1323 /* if bounded pointers, we generate a special code to
1324 test bounds */
1325 if (op == '-') {
1326 vpushi(0);
1327 vswap();
1328 gen_op('-');
1330 gen_bounded_ptr_add();
1331 } else
1332 #endif
1334 gen_opic(op);
1336 /* put again type if gen_opic() swaped operands */
1337 vtop->type = type1;
1339 } else if (is_float(bt1) || is_float(bt2)) {
1340 /* compute bigger type and do implicit casts */
1341 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
1342 t = VT_LDOUBLE;
1343 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
1344 t = VT_DOUBLE;
1345 } else {
1346 t = VT_FLOAT;
1348 /* floats can only be used for a few operations */
1349 if (op != '+' && op != '-' && op != '*' && op != '/' &&
1350 (op < TOK_ULT || op > TOK_GT))
1351 error("invalid operands for binary operation");
1352 goto std_op;
1353 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
1354 /* cast to biggest op */
1355 t = VT_LLONG;
1356 /* convert to unsigned if it does not fit in a long long */
1357 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
1358 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
1359 t |= VT_UNSIGNED;
1360 goto std_op;
1361 } else {
1362 /* integer operations */
1363 t = VT_INT;
1364 /* convert to unsigned if it does not fit in an integer */
1365 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
1366 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
1367 t |= VT_UNSIGNED;
1368 std_op:
1369 /* XXX: currently, some unsigned operations are explicit, so
1370 we modify them here */
1371 if (t & VT_UNSIGNED) {
1372 if (op == TOK_SAR)
1373 op = TOK_SHR;
1374 else if (op == '/')
1375 op = TOK_UDIV;
1376 else if (op == '%')
1377 op = TOK_UMOD;
1378 else if (op == TOK_LT)
1379 op = TOK_ULT;
1380 else if (op == TOK_GT)
1381 op = TOK_UGT;
1382 else if (op == TOK_LE)
1383 op = TOK_ULE;
1384 else if (op == TOK_GE)
1385 op = TOK_UGE;
1387 vswap();
1388 type1.t = t;
1389 gen_cast(&type1);
1390 vswap();
1391 /* special case for shifts and long long: we keep the shift as
1392 an integer */
1393 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
1394 type1.t = VT_INT;
1395 gen_cast(&type1);
1396 if (is_float(t))
1397 gen_opif(op);
1398 else
1399 gen_opic(op);
1400 if (op >= TOK_ULT && op <= TOK_GT) {
1401 /* relationnal op: the result is an int */
1402 vtop->type.t = VT_INT;
1403 } else {
1404 vtop->type.t = t;
1409 #ifndef TCC_TARGET_ARM
1410 /* generic itof for unsigned long long case */
1411 void gen_cvt_itof1(int t)
1413 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
1414 (VT_LLONG | VT_UNSIGNED)) {
1416 if (t == VT_FLOAT)
1417 vpush_global_sym(&func_old_type, TOK___floatundisf);
1418 #if LDOUBLE_SIZE != 8
1419 else if (t == VT_LDOUBLE)
1420 vpush_global_sym(&func_old_type, TOK___floatundixf);
1421 #endif
1422 else
1423 vpush_global_sym(&func_old_type, TOK___floatundidf);
1424 vrott(2);
1425 gfunc_call(1);
1426 vpushi(0);
1427 vtop->r = reg_fret(t);
1428 } else {
1429 gen_cvt_itof(t);
1432 #endif
1434 /* generic ftoi for unsigned long long case */
1435 void gen_cvt_ftoi1(int t)
1437 int st;
1439 if (t == (VT_LLONG | VT_UNSIGNED)) {
1440 /* not handled natively */
1441 st = vtop->type.t & VT_BTYPE;
1442 if (st == VT_FLOAT)
1443 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
1444 #if LDOUBLE_SIZE != 8
1445 else if (st == VT_LDOUBLE)
1446 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
1447 #endif
1448 else
1449 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
1450 vrott(2);
1451 gfunc_call(1);
1452 vpushi(0);
1453 vtop->r = REG_IRET;
1454 vtop->r2 = REG_LRET;
1455 } else {
1456 gen_cvt_ftoi(t);
1460 /* force char or short cast */
1461 void force_charshort_cast(int t)
1463 int bits, dbt;
1464 dbt = t & VT_BTYPE;
1465 /* XXX: add optimization if lvalue : just change type and offset */
1466 if (dbt == VT_BYTE)
1467 bits = 8;
1468 else
1469 bits = 16;
1470 if (t & VT_UNSIGNED) {
1471 vpushi((1 << bits) - 1);
1472 gen_op('&');
1473 } else {
1474 bits = 32 - bits;
1475 vpushi(bits);
1476 gen_op(TOK_SHL);
1477 /* result must be signed or the SAR is converted to an SHL
1478 This was not the case when "t" was a signed short
1479 and the last value on the stack was an unsigned int */
1480 vtop->type.t &= ~VT_UNSIGNED;
1481 vpushi(bits);
1482 gen_op(TOK_SAR);
1486 /* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
1487 static void gen_cast(CType *type)
1489 int sbt, dbt, sf, df, c, p;
1491 /* special delayed cast for char/short */
1492 /* XXX: in some cases (multiple cascaded casts), it may still
1493 be incorrect */
1494 if (vtop->r & VT_MUSTCAST) {
1495 vtop->r &= ~VT_MUSTCAST;
1496 force_charshort_cast(vtop->type.t);
1499 /* bitfields first get cast to ints */
1500 if (vtop->type.t & VT_BITFIELD) {
1501 gv(RC_INT);
1504 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
1505 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
1507 if (sbt != dbt) {
1508 sf = is_float(sbt);
1509 df = is_float(dbt);
1510 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
1511 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
1512 if (c) {
1513 /* constant case: we can do it now */
1514 /* XXX: in ISOC, cannot do it if error in convert */
1515 if (sbt == VT_FLOAT)
1516 vtop->c.ld = vtop->c.f;
1517 else if (sbt == VT_DOUBLE)
1518 vtop->c.ld = vtop->c.d;
1520 if (df) {
1521 if ((sbt & VT_BTYPE) == VT_LLONG) {
1522 if (sbt & VT_UNSIGNED)
1523 vtop->c.ld = vtop->c.ull;
1524 else
1525 vtop->c.ld = vtop->c.ll;
1526 } else if(!sf) {
1527 if (sbt & VT_UNSIGNED)
1528 vtop->c.ld = vtop->c.ui;
1529 else
1530 vtop->c.ld = vtop->c.i;
1533 if (dbt == VT_FLOAT)
1534 vtop->c.f = (float)vtop->c.ld;
1535 else if (dbt == VT_DOUBLE)
1536 vtop->c.d = (double)vtop->c.ld;
1537 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
1538 vtop->c.ull = (unsigned long long)vtop->c.ld;
1539 } else if (sf && dbt == VT_BOOL) {
1540 vtop->c.i = (vtop->c.ld != 0);
1541 } else {
1542 if(sf)
1543 vtop->c.ll = (long long)vtop->c.ld;
1544 else if (sbt == (VT_LLONG|VT_UNSIGNED))
1545 vtop->c.ll = vtop->c.ull;
1546 else if (sbt & VT_UNSIGNED)
1547 vtop->c.ll = vtop->c.ui;
1548 else if (sbt != VT_LLONG)
1549 vtop->c.ll = vtop->c.i;
1551 if (dbt == (VT_LLONG|VT_UNSIGNED))
1552 vtop->c.ull = vtop->c.ll;
1553 else if (dbt == VT_BOOL)
1554 vtop->c.i = (vtop->c.ll != 0);
1555 else if (dbt != VT_LLONG) {
1556 int s = 0;
1557 if ((dbt & VT_BTYPE) == VT_BYTE)
1558 s = 24;
1559 else if ((dbt & VT_BTYPE) == VT_SHORT)
1560 s = 16;
1562 if(dbt & VT_UNSIGNED)
1563 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
1564 else
1565 vtop->c.i = ((int)vtop->c.ll << s) >> s;
1568 } else if (p && dbt == VT_BOOL) {
1569 vtop->r = VT_CONST;
1570 vtop->c.i = 1;
1571 } else if (!nocode_wanted) {
1572 /* non constant case: generate code */
1573 if (sf && df) {
1574 /* convert from fp to fp */
1575 gen_cvt_ftof(dbt);
1576 } else if (df) {
1577 /* convert int to fp */
1578 gen_cvt_itof1(dbt);
1579 } else if (sf) {
1580 /* convert fp to int */
1581 if (dbt == VT_BOOL) {
1582 vpushi(0);
1583 gen_op(TOK_NE);
1584 } else {
1585 /* we handle char/short/etc... with generic code */
1586 if (dbt != (VT_INT | VT_UNSIGNED) &&
1587 dbt != (VT_LLONG | VT_UNSIGNED) &&
1588 dbt != VT_LLONG)
1589 dbt = VT_INT;
1590 gen_cvt_ftoi1(dbt);
1591 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
1592 /* additional cast for char/short... */
1593 vtop->type.t = dbt;
1594 gen_cast(type);
1597 #ifndef TCC_TARGET_X86_64
1598 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
1599 if ((sbt & VT_BTYPE) != VT_LLONG) {
1600 /* scalar to long long */
1601 /* machine independent conversion */
1602 gv(RC_INT);
1603 /* generate high word */
1604 if (sbt == (VT_INT | VT_UNSIGNED)) {
1605 vpushi(0);
1606 gv(RC_INT);
1607 } else {
1608 if (sbt == VT_PTR) {
1609 /* cast from pointer to int before we apply
1610 shift operation, which pointers don't support*/
1611 gen_cast(&int_type);
1613 gv_dup();
1614 vpushi(31);
1615 gen_op(TOK_SAR);
1617 /* patch second register */
1618 vtop[-1].r2 = vtop->r;
1619 vpop();
1621 #else
1622 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
1623 (dbt & VT_BTYPE) == VT_PTR) {
1624 /* XXX: not sure if this is perfect... need more tests */
1625 if ((sbt & VT_BTYPE) != VT_LLONG) {
1626 int r = gv(RC_INT);
1627 if (sbt != (VT_INT | VT_UNSIGNED) &&
1628 sbt != VT_PTR && sbt != VT_FUNC) {
1629 /* x86_64 specific: movslq */
1630 o(0x6348);
1631 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
1634 #endif
1635 } else if (dbt == VT_BOOL) {
1636 /* scalar to bool */
1637 vpushi(0);
1638 gen_op(TOK_NE);
1639 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
1640 (dbt & VT_BTYPE) == VT_SHORT) {
1641 if (sbt == VT_PTR) {
1642 vtop->type.t = VT_INT;
1643 warning("nonportable conversion from pointer to char/short");
1645 force_charshort_cast(dbt);
1646 } else if ((dbt & VT_BTYPE) == VT_INT) {
1647 /* scalar to int */
1648 if (sbt == VT_LLONG) {
1649 /* from long long: just take low order word */
1650 lexpand();
1651 vpop();
1653 /* if lvalue and single word type, nothing to do because
1654 the lvalue already contains the real type size (see
1655 VT_LVAL_xxx constants) */
1658 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
1659 /* if we are casting between pointer types,
1660 we must update the VT_LVAL_xxx size */
1661 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
1662 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
1664 vtop->type = *type;
1667 /* return type size. Put alignment at 'a' */
1668 int type_size(CType *type, int *a)
1670 Sym *s;
1671 int bt;
1673 bt = type->t & VT_BTYPE;
1674 if (bt == VT_STRUCT) {
1675 /* struct/union */
1676 s = type->ref;
1677 *a = s->r;
1678 return s->c;
1679 } else if (bt == VT_PTR) {
1680 if (type->t & VT_ARRAY) {
1681 int ts;
1683 s = type->ref;
1684 ts = type_size(&s->type, a);
1686 if (ts < 0 && s->c < 0)
1687 ts = -ts;
1689 return ts * s->c;
1690 } else {
1691 *a = PTR_SIZE;
1692 return PTR_SIZE;
1694 } else if (bt == VT_LDOUBLE) {
1695 *a = LDOUBLE_ALIGN;
1696 return LDOUBLE_SIZE;
1697 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
1698 #ifdef TCC_TARGET_I386
1699 #ifdef TCC_TARGET_PE
1700 *a = 8;
1701 #else
1702 *a = 4;
1703 #endif
1704 #elif defined(TCC_TARGET_ARM)
1705 #ifdef TCC_ARM_EABI
1706 *a = 8;
1707 #else
1708 *a = 4;
1709 #endif
1710 #else
1711 *a = 8;
1712 #endif
1713 return 8;
1714 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
1715 *a = 4;
1716 return 4;
1717 } else if (bt == VT_SHORT) {
1718 *a = 2;
1719 return 2;
1720 } else {
1721 /* char, void, function, _Bool */
1722 *a = 1;
1723 return 1;
1727 /* return the pointed type of t */
1728 static inline CType *pointed_type(CType *type)
1730 return &type->ref->type;
1733 /* modify type so that its it is a pointer to type. */
1734 void mk_pointer(CType *type)
1736 Sym *s;
1737 s = sym_push(SYM_FIELD, type, 0, -1);
1738 type->t = VT_PTR | (type->t & ~VT_TYPE);
1739 type->ref = s;
1742 /* compare function types. OLD functions match any new functions */
1743 static int is_compatible_func(CType *type1, CType *type2)
1745 Sym *s1, *s2;
1747 s1 = type1->ref;
1748 s2 = type2->ref;
1749 if (!is_compatible_types(&s1->type, &s2->type))
1750 return 0;
1751 /* check func_call */
1752 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
1753 return 0;
1754 /* XXX: not complete */
1755 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
1756 return 1;
1757 if (s1->c != s2->c)
1758 return 0;
1759 while (s1 != NULL) {
1760 if (s2 == NULL)
1761 return 0;
1762 if (!is_compatible_parameter_types(&s1->type, &s2->type))
1763 return 0;
1764 s1 = s1->next;
1765 s2 = s2->next;
1767 if (s2)
1768 return 0;
1769 return 1;
1772 /* return true if type1 and type2 are the same. If unqualified is
1773 true, qualifiers on the types are ignored.
1775 - enums are not checked as gcc __builtin_types_compatible_p ()
1777 static int compare_types(CType *type1, CType *type2, int unqualified)
1779 int bt1, t1, t2;
1781 t1 = type1->t & VT_TYPE;
1782 t2 = type2->t & VT_TYPE;
1783 if (unqualified) {
1784 /* strip qualifiers before comparing */
1785 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
1786 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
1788 /* XXX: bitfields ? */
1789 if (t1 != t2)
1790 return 0;
1791 /* test more complicated cases */
1792 bt1 = t1 & VT_BTYPE;
1793 if (bt1 == VT_PTR) {
1794 type1 = pointed_type(type1);
1795 type2 = pointed_type(type2);
1796 return is_compatible_types(type1, type2);
1797 } else if (bt1 == VT_STRUCT) {
1798 return (type1->ref == type2->ref);
1799 } else if (bt1 == VT_FUNC) {
1800 return is_compatible_func(type1, type2);
1801 } else {
1802 return 1;
1806 /* return true if type1 and type2 are exactly the same (including
1807 qualifiers).
1809 static int is_compatible_types(CType *type1, CType *type2)
1811 return compare_types(type1,type2,0);
1814 /* return true if type1 and type2 are the same (ignoring qualifiers).
1816 static int is_compatible_parameter_types(CType *type1, CType *type2)
1818 return compare_types(type1,type2,1);
1821 /* print a type. If 'varstr' is not NULL, then the variable is also
1822 printed in the type */
1823 /* XXX: union */
1824 /* XXX: add array and function pointers */
1825 void type_to_str(char *buf, int buf_size,
1826 CType *type, const char *varstr)
1828 int bt, v, t;
1829 Sym *s, *sa;
1830 char buf1[256];
1831 const char *tstr;
1833 t = type->t & VT_TYPE;
1834 bt = t & VT_BTYPE;
1835 buf[0] = '\0';
1836 if (t & VT_CONSTANT)
1837 pstrcat(buf, buf_size, "const ");
1838 if (t & VT_VOLATILE)
1839 pstrcat(buf, buf_size, "volatile ");
1840 if (t & VT_UNSIGNED)
1841 pstrcat(buf, buf_size, "unsigned ");
1842 switch(bt) {
1843 case VT_VOID:
1844 tstr = "void";
1845 goto add_tstr;
1846 case VT_BOOL:
1847 tstr = "_Bool";
1848 goto add_tstr;
1849 case VT_BYTE:
1850 tstr = "char";
1851 goto add_tstr;
1852 case VT_SHORT:
1853 tstr = "short";
1854 goto add_tstr;
1855 case VT_INT:
1856 tstr = "int";
1857 goto add_tstr;
1858 case VT_LONG:
1859 tstr = "long";
1860 goto add_tstr;
1861 case VT_LLONG:
1862 tstr = "long long";
1863 goto add_tstr;
1864 case VT_FLOAT:
1865 tstr = "float";
1866 goto add_tstr;
1867 case VT_DOUBLE:
1868 tstr = "double";
1869 goto add_tstr;
1870 case VT_LDOUBLE:
1871 tstr = "long double";
1872 add_tstr:
1873 pstrcat(buf, buf_size, tstr);
1874 break;
1875 case VT_ENUM:
1876 case VT_STRUCT:
1877 if (bt == VT_STRUCT)
1878 tstr = "struct ";
1879 else
1880 tstr = "enum ";
1881 pstrcat(buf, buf_size, tstr);
1882 v = type->ref->v & ~SYM_STRUCT;
1883 if (v >= SYM_FIRST_ANOM)
1884 pstrcat(buf, buf_size, "<anonymous>");
1885 else
1886 pstrcat(buf, buf_size, get_tok_str(v, NULL));
1887 break;
1888 case VT_FUNC:
1889 s = type->ref;
1890 type_to_str(buf, buf_size, &s->type, varstr);
1891 pstrcat(buf, buf_size, "(");
1892 sa = s->next;
1893 while (sa != NULL) {
1894 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
1895 pstrcat(buf, buf_size, buf1);
1896 sa = sa->next;
1897 if (sa)
1898 pstrcat(buf, buf_size, ", ");
1900 pstrcat(buf, buf_size, ")");
1901 goto no_var;
1902 case VT_PTR:
1903 s = type->ref;
1904 pstrcpy(buf1, sizeof(buf1), "*");
1905 if (varstr)
1906 pstrcat(buf1, sizeof(buf1), varstr);
1907 type_to_str(buf, buf_size, &s->type, buf1);
1908 goto no_var;
1910 if (varstr) {
1911 pstrcat(buf, buf_size, " ");
1912 pstrcat(buf, buf_size, varstr);
1914 no_var: ;
1917 /* verify type compatibility to store vtop in 'dt' type, and generate
1918 casts if needed. */
1919 static void gen_assign_cast(CType *dt)
1921 CType *st, *type1, *type2, tmp_type1, tmp_type2;
1922 char buf1[256], buf2[256];
1923 int dbt, sbt;
1925 st = &vtop->type; /* source type */
1926 dbt = dt->t & VT_BTYPE;
1927 sbt = st->t & VT_BTYPE;
1928 if (dt->t & VT_CONSTANT)
1929 warning("assignment of read-only location");
1930 switch(dbt) {
1931 case VT_PTR:
1932 /* special cases for pointers */
1933 /* '0' can also be a pointer */
1934 if (is_null_pointer(vtop))
1935 goto type_ok;
1936 /* accept implicit pointer to integer cast with warning */
1937 if (is_integer_btype(sbt)) {
1938 warning("assignment makes pointer from integer without a cast");
1939 goto type_ok;
1941 type1 = pointed_type(dt);
1942 /* a function is implicitely a function pointer */
1943 if (sbt == VT_FUNC) {
1944 if ((type1->t & VT_BTYPE) != VT_VOID &&
1945 !is_compatible_types(pointed_type(dt), st))
1946 warning("assignment from incompatible pointer type");
1947 goto type_ok;
1949 if (sbt != VT_PTR)
1950 goto error;
1951 type2 = pointed_type(st);
1952 if ((type1->t & VT_BTYPE) == VT_VOID ||
1953 (type2->t & VT_BTYPE) == VT_VOID) {
1954 /* void * can match anything */
1955 } else {
1956 /* exact type match, except for unsigned */
1957 tmp_type1 = *type1;
1958 tmp_type2 = *type2;
1959 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1960 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
1961 if (!is_compatible_types(&tmp_type1, &tmp_type2))
1962 warning("assignment from incompatible pointer type");
1964 /* check const and volatile */
1965 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
1966 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
1967 warning("assignment discards qualifiers from pointer target type");
1968 break;
1969 case VT_BYTE:
1970 case VT_SHORT:
1971 case VT_INT:
1972 case VT_LLONG:
1973 if (sbt == VT_PTR || sbt == VT_FUNC) {
1974 warning("assignment makes integer from pointer without a cast");
1976 /* XXX: more tests */
1977 break;
1978 case VT_STRUCT:
1979 tmp_type1 = *dt;
1980 tmp_type2 = *st;
1981 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
1982 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
1983 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
1984 error:
1985 type_to_str(buf1, sizeof(buf1), st, NULL);
1986 type_to_str(buf2, sizeof(buf2), dt, NULL);
1987 error("cannot cast '%s' to '%s'", buf1, buf2);
1989 break;
1991 type_ok:
1992 gen_cast(dt);
1995 /* store vtop in lvalue pushed on stack */
1996 void vstore(void)
1998 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
2000 ft = vtop[-1].type.t;
2001 sbt = vtop->type.t & VT_BTYPE;
2002 dbt = ft & VT_BTYPE;
2003 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
2004 (sbt == VT_INT && dbt == VT_SHORT)) {
2005 /* optimize char/short casts */
2006 delayed_cast = VT_MUSTCAST;
2007 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
2008 /* XXX: factorize */
2009 if (ft & VT_CONSTANT)
2010 warning("assignment of read-only location");
2011 } else {
2012 delayed_cast = 0;
2013 if (!(ft & VT_BITFIELD))
2014 gen_assign_cast(&vtop[-1].type);
2017 if (sbt == VT_STRUCT) {
2018 /* if structure, only generate pointer */
2019 /* structure assignment : generate memcpy */
2020 /* XXX: optimize if small size */
2021 if (!nocode_wanted) {
2022 size = type_size(&vtop->type, &align);
2024 /* destination */
2025 vswap();
2026 vtop->type.t = VT_PTR;
2027 gaddrof();
2029 /* address of memcpy() */
2030 #ifdef TCC_ARM_EABI
2031 if(!(align & 7))
2032 vpush_global_sym(&func_old_type, TOK_memcpy8);
2033 else if(!(align & 3))
2034 vpush_global_sym(&func_old_type, TOK_memcpy4);
2035 else
2036 #endif
2037 vpush_global_sym(&func_old_type, TOK_memcpy);
2039 vswap();
2040 /* source */
2041 vpushv(vtop - 2);
2042 vtop->type.t = VT_PTR;
2043 gaddrof();
2044 /* type size */
2045 vpushi(size);
2046 gfunc_call(3);
2047 } else {
2048 vswap();
2049 vpop();
2051 /* leave source on stack */
2052 } else if (ft & VT_BITFIELD) {
2053 /* bitfield store handling */
2054 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
2055 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
2056 /* remove bit field info to avoid loops */
2057 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
2059 /* duplicate source into other register */
2060 gv_dup();
2061 vswap();
2062 vrott(3);
2064 if((ft & VT_BTYPE) == VT_BOOL) {
2065 gen_cast(&vtop[-1].type);
2066 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
2069 /* duplicate destination */
2070 vdup();
2071 vtop[-1] = vtop[-2];
2073 /* mask and shift source */
2074 if((ft & VT_BTYPE) != VT_BOOL) {
2075 if((ft & VT_BTYPE) == VT_LLONG) {
2076 vpushll((1ULL << bit_size) - 1ULL);
2077 } else {
2078 vpushi((1 << bit_size) - 1);
2080 gen_op('&');
2082 vpushi(bit_pos);
2083 gen_op(TOK_SHL);
2084 /* load destination, mask and or with source */
2085 vswap();
2086 if((ft & VT_BTYPE) == VT_LLONG) {
2087 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
2088 } else {
2089 vpushi(~(((1 << bit_size) - 1) << bit_pos));
2091 gen_op('&');
2092 gen_op('|');
2093 /* store result */
2094 vstore();
2096 /* pop off shifted source from "duplicate source..." above */
2097 vpop();
2099 } else {
2100 #ifdef CONFIG_TCC_BCHECK
2101 /* bound check case */
2102 if (vtop[-1].r & VT_MUSTBOUND) {
2103 vswap();
2104 gbound();
2105 vswap();
2107 #endif
2108 if (!nocode_wanted) {
2109 rc = RC_INT;
2110 if (is_float(ft)) {
2111 rc = RC_FLOAT;
2112 #ifdef TCC_TARGET_X86_64
2113 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
2114 rc = RC_ST0;
2116 #endif
2118 r = gv(rc); /* generate value */
2119 /* if lvalue was saved on stack, must read it */
2120 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
2121 SValue sv;
2122 t = get_reg(RC_INT);
2123 #ifdef TCC_TARGET_X86_64
2124 sv.type.t = VT_PTR;
2125 #else
2126 sv.type.t = VT_INT;
2127 #endif
2128 sv.r = VT_LOCAL | VT_LVAL;
2129 sv.c.ul = vtop[-1].c.ul;
2130 load(t, &sv);
2131 vtop[-1].r = t | VT_LVAL;
2133 store(r, vtop - 1);
2134 #ifndef TCC_TARGET_X86_64
2135 /* two word case handling : store second register at word + 4 */
2136 if ((ft & VT_BTYPE) == VT_LLONG) {
2137 vswap();
2138 /* convert to int to increment easily */
2139 vtop->type.t = VT_INT;
2140 gaddrof();
2141 vpushi(4);
2142 gen_op('+');
2143 vtop->r |= VT_LVAL;
2144 vswap();
2145 /* XXX: it works because r2 is spilled last ! */
2146 store(vtop->r2, vtop - 1);
2148 #endif
2150 vswap();
2151 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
2152 vtop->r |= delayed_cast;
2156 /* post defines POST/PRE add. c is the token ++ or -- */
2157 void inc(int post, int c)
2159 test_lvalue();
2160 vdup(); /* save lvalue */
2161 if (post) {
2162 gv_dup(); /* duplicate value */
2163 vrotb(3);
2164 vrotb(3);
2166 /* add constant */
2167 vpushi(c - TOK_MID);
2168 gen_op('+');
2169 vstore(); /* store value */
2170 if (post)
2171 vpop(); /* if post op, return saved value */
2174 /* Parse GNUC __attribute__ extension. Currently, the following
2175 extensions are recognized:
2176 - aligned(n) : set data/function alignment.
2177 - packed : force data alignment to 1
2178 - section(x) : generate data/code in this section.
2179 - unused : currently ignored, but may be used someday.
2180 - regparm(n) : pass function parameters in registers (i386 only)
2182 static void parse_attribute(AttributeDef *ad)
2184 int t, n;
2186 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
2187 next();
2188 skip('(');
2189 skip('(');
2190 while (tok != ')') {
2191 if (tok < TOK_IDENT)
2192 expect("attribute name");
2193 t = tok;
2194 next();
2195 switch(t) {
2196 case TOK_SECTION1:
2197 case TOK_SECTION2:
2198 skip('(');
2199 if (tok != TOK_STR)
2200 expect("section name");
2201 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
2202 next();
2203 skip(')');
2204 break;
2205 case TOK_ALIGNED1:
2206 case TOK_ALIGNED2:
2207 if (tok == '(') {
2208 next();
2209 n = expr_const();
2210 if (n <= 0 || (n & (n - 1)) != 0)
2211 error("alignment must be a positive power of two");
2212 skip(')');
2213 } else {
2214 n = MAX_ALIGN;
2216 ad->aligned = n;
2217 break;
2218 case TOK_PACKED1:
2219 case TOK_PACKED2:
2220 ad->packed = 1;
2221 break;
2222 case TOK_UNUSED1:
2223 case TOK_UNUSED2:
2224 /* currently, no need to handle it because tcc does not
2225 track unused objects */
2226 break;
2227 case TOK_NORETURN1:
2228 case TOK_NORETURN2:
2229 /* currently, no need to handle it because tcc does not
2230 track unused objects */
2231 break;
2232 case TOK_CDECL1:
2233 case TOK_CDECL2:
2234 case TOK_CDECL3:
2235 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
2236 break;
2237 case TOK_STDCALL1:
2238 case TOK_STDCALL2:
2239 case TOK_STDCALL3:
2240 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
2241 break;
2242 #ifdef TCC_TARGET_I386
2243 case TOK_REGPARM1:
2244 case TOK_REGPARM2:
2245 skip('(');
2246 n = expr_const();
2247 if (n > 3)
2248 n = 3;
2249 else if (n < 0)
2250 n = 0;
2251 if (n > 0)
2252 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
2253 skip(')');
2254 break;
2255 case TOK_FASTCALL1:
2256 case TOK_FASTCALL2:
2257 case TOK_FASTCALL3:
2258 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
2259 break;
2260 #endif
2261 case TOK_DLLEXPORT:
2262 FUNC_EXPORT(ad->func_attr) = 1;
2263 break;
2264 case TOK_DLLIMPORT:
2265 FUNC_IMPORT(ad->func_attr) = 1;
2266 break;
2267 default:
2268 if (tcc_state->warn_unsupported)
2269 warning("'%s' attribute ignored", get_tok_str(t, NULL));
2270 /* skip parameters */
2271 if (tok == '(') {
2272 int parenthesis = 0;
2273 do {
2274 if (tok == '(')
2275 parenthesis++;
2276 else if (tok == ')')
2277 parenthesis--;
2278 next();
2279 } while (parenthesis && tok != -1);
2281 break;
2283 if (tok != ',')
2284 break;
2285 next();
2287 skip(')');
2288 skip(')');
2292 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
2293 static void struct_decl(CType *type, int u)
2295 int a, v, size, align, maxalign, c, offset;
2296 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
2297 Sym *s, *ss, *ass, **ps;
2298 AttributeDef ad;
2299 CType type1, btype;
2301 a = tok; /* save decl type */
2302 next();
2303 if (tok != '{') {
2304 v = tok;
2305 next();
2306 /* struct already defined ? return it */
2307 if (v < TOK_IDENT)
2308 expect("struct/union/enum name");
2309 s = struct_find(v);
2310 if (s) {
2311 if (s->type.t != a)
2312 error("invalid type");
2313 goto do_decl;
2315 } else {
2316 v = anon_sym++;
2318 type1.t = a;
2319 /* we put an undefined size for struct/union */
2320 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
2321 s->r = 0; /* default alignment is zero as gcc */
2322 /* put struct/union/enum name in type */
2323 do_decl:
2324 type->t = u;
2325 type->ref = s;
2327 if (tok == '{') {
2328 next();
2329 if (s->c != -1)
2330 error("struct/union/enum already defined");
2331 /* cannot be empty */
2332 c = 0;
2333 /* non empty enums are not allowed */
2334 if (a == TOK_ENUM) {
2335 for(;;) {
2336 v = tok;
2337 if (v < TOK_UIDENT)
2338 expect("identifier");
2339 next();
2340 if (tok == '=') {
2341 next();
2342 c = expr_const();
2344 /* enum symbols have static storage */
2345 ss = sym_push(v, &int_type, VT_CONST, c);
2346 ss->type.t |= VT_STATIC;
2347 if (tok != ',')
2348 break;
2349 next();
2350 c++;
2351 /* NOTE: we accept a trailing comma */
2352 if (tok == '}')
2353 break;
2355 skip('}');
2356 } else {
2357 maxalign = 1;
2358 ps = &s->next;
2359 prevbt = VT_INT;
2360 bit_pos = 0;
2361 offset = 0;
2362 while (tok != '}') {
2363 parse_btype(&btype, &ad);
2364 while (1) {
2365 bit_size = -1;
2366 v = 0;
2367 type1 = btype;
2368 if (tok != ':') {
2369 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
2370 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
2371 expect("identifier");
2372 if ((type1.t & VT_BTYPE) == VT_FUNC ||
2373 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
2374 error("invalid type for '%s'",
2375 get_tok_str(v, NULL));
2377 if (tok == ':') {
2378 next();
2379 bit_size = expr_const();
2380 /* XXX: handle v = 0 case for messages */
2381 if (bit_size < 0)
2382 error("negative width in bit-field '%s'",
2383 get_tok_str(v, NULL));
2384 if (v && bit_size == 0)
2385 error("zero width for bit-field '%s'",
2386 get_tok_str(v, NULL));
2388 size = type_size(&type1, &align);
2389 if (ad.aligned) {
2390 if (align < ad.aligned)
2391 align = ad.aligned;
2392 } else if (ad.packed) {
2393 align = 1;
2394 } else if (*tcc_state->pack_stack_ptr) {
2395 if (align > *tcc_state->pack_stack_ptr)
2396 align = *tcc_state->pack_stack_ptr;
2398 lbit_pos = 0;
2399 if (bit_size >= 0) {
2400 bt = type1.t & VT_BTYPE;
2401 if (bt != VT_INT &&
2402 bt != VT_BYTE &&
2403 bt != VT_SHORT &&
2404 bt != VT_BOOL &&
2405 bt != VT_ENUM &&
2406 bt != VT_LLONG)
2407 error("bitfields must have scalar type");
2408 bsize = size * 8;
2409 if (bit_size > bsize) {
2410 error("width of '%s' exceeds its type",
2411 get_tok_str(v, NULL));
2412 } else if (bit_size == bsize) {
2413 /* no need for bit fields */
2414 bit_pos = 0;
2415 } else if (bit_size == 0) {
2416 /* XXX: what to do if only padding in a
2417 structure ? */
2418 /* zero size: means to pad */
2419 bit_pos = 0;
2420 } else {
2421 /* we do not have enough room ?
2422 did the type change?
2423 is it a union? */
2424 if ((bit_pos + bit_size) > bsize ||
2425 bt != prevbt || a == TOK_UNION)
2426 bit_pos = 0;
2427 lbit_pos = bit_pos;
2428 /* XXX: handle LSB first */
2429 type1.t |= VT_BITFIELD |
2430 (bit_pos << VT_STRUCT_SHIFT) |
2431 (bit_size << (VT_STRUCT_SHIFT + 6));
2432 bit_pos += bit_size;
2434 prevbt = bt;
2435 } else {
2436 bit_pos = 0;
2438 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
2439 /* add new memory data only if starting
2440 bit field */
2441 if (lbit_pos == 0) {
2442 if (a == TOK_STRUCT) {
2443 c = (c + align - 1) & -align;
2444 offset = c;
2445 if (size > 0)
2446 c += size;
2447 } else {
2448 offset = 0;
2449 if (size > c)
2450 c = size;
2452 if (align > maxalign)
2453 maxalign = align;
2455 #if 0
2456 printf("add field %s offset=%d",
2457 get_tok_str(v, NULL), offset);
2458 if (type1.t & VT_BITFIELD) {
2459 printf(" pos=%d size=%d",
2460 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
2461 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
2463 printf("\n");
2464 #endif
2466 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
2467 ass = type1.ref;
2468 while ((ass = ass->next) != NULL) {
2469 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
2470 *ps = ss;
2471 ps = &ss->next;
2473 } else if (v) {
2474 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
2475 *ps = ss;
2476 ps = &ss->next;
2478 if (tok == ';' || tok == TOK_EOF)
2479 break;
2480 skip(',');
2482 skip(';');
2484 skip('}');
2485 /* store size and alignment */
2486 s->c = (c + maxalign - 1) & -maxalign;
2487 s->r = maxalign;
2492 /* return 0 if no type declaration. otherwise, return the basic type
2493 and skip it.
2495 static int parse_btype(CType *type, AttributeDef *ad)
2497 int t, u, type_found, typespec_found, typedef_found;
2498 Sym *s;
2499 CType type1;
2501 memset(ad, 0, sizeof(AttributeDef));
2502 type_found = 0;
2503 typespec_found = 0;
2504 typedef_found = 0;
2505 t = 0;
2506 while(1) {
2507 switch(tok) {
2508 case TOK_EXTENSION:
2509 /* currently, we really ignore extension */
2510 next();
2511 continue;
2513 /* basic types */
2514 case TOK_CHAR:
2515 u = VT_BYTE;
2516 basic_type:
2517 next();
2518 basic_type1:
2519 if ((t & VT_BTYPE) != 0)
2520 error("too many basic types");
2521 t |= u;
2522 typespec_found = 1;
2523 break;
2524 case TOK_VOID:
2525 u = VT_VOID;
2526 goto basic_type;
2527 case TOK_SHORT:
2528 u = VT_SHORT;
2529 goto basic_type;
2530 case TOK_INT:
2531 next();
2532 typespec_found = 1;
2533 break;
2534 case TOK_LONG:
2535 next();
2536 if ((t & VT_BTYPE) == VT_DOUBLE) {
2537 #ifndef TCC_TARGET_PE
2538 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2539 #endif
2540 } else if ((t & VT_BTYPE) == VT_LONG) {
2541 t = (t & ~VT_BTYPE) | VT_LLONG;
2542 } else {
2543 u = VT_LONG;
2544 goto basic_type1;
2546 break;
2547 case TOK_BOOL:
2548 u = VT_BOOL;
2549 goto basic_type;
2550 case TOK_FLOAT:
2551 u = VT_FLOAT;
2552 goto basic_type;
2553 case TOK_DOUBLE:
2554 next();
2555 if ((t & VT_BTYPE) == VT_LONG) {
2556 #ifdef TCC_TARGET_PE
2557 t = (t & ~VT_BTYPE) | VT_DOUBLE;
2558 #else
2559 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
2560 #endif
2561 } else {
2562 u = VT_DOUBLE;
2563 goto basic_type1;
2565 break;
2566 case TOK_ENUM:
2567 struct_decl(&type1, VT_ENUM);
2568 basic_type2:
2569 u = type1.t;
2570 type->ref = type1.ref;
2571 goto basic_type1;
2572 case TOK_STRUCT:
2573 case TOK_UNION:
2574 struct_decl(&type1, VT_STRUCT);
2575 goto basic_type2;
2577 /* type modifiers */
2578 case TOK_CONST1:
2579 case TOK_CONST2:
2580 case TOK_CONST3:
2581 t |= VT_CONSTANT;
2582 next();
2583 break;
2584 case TOK_VOLATILE1:
2585 case TOK_VOLATILE2:
2586 case TOK_VOLATILE3:
2587 t |= VT_VOLATILE;
2588 next();
2589 break;
2590 case TOK_SIGNED1:
2591 case TOK_SIGNED2:
2592 case TOK_SIGNED3:
2593 typespec_found = 1;
2594 t |= VT_SIGNED;
2595 next();
2596 break;
2597 case TOK_REGISTER:
2598 case TOK_AUTO:
2599 case TOK_RESTRICT1:
2600 case TOK_RESTRICT2:
2601 case TOK_RESTRICT3:
2602 next();
2603 break;
2604 case TOK_UNSIGNED:
2605 t |= VT_UNSIGNED;
2606 next();
2607 typespec_found = 1;
2608 break;
2610 /* storage */
2611 case TOK_EXTERN:
2612 t |= VT_EXTERN;
2613 next();
2614 break;
2615 case TOK_STATIC:
2616 t |= VT_STATIC;
2617 next();
2618 break;
2619 case TOK_TYPEDEF:
2620 t |= VT_TYPEDEF;
2621 next();
2622 break;
2623 case TOK_INLINE1:
2624 case TOK_INLINE2:
2625 case TOK_INLINE3:
2626 t |= VT_INLINE;
2627 next();
2628 break;
2630 /* GNUC attribute */
2631 case TOK_ATTRIBUTE1:
2632 case TOK_ATTRIBUTE2:
2633 parse_attribute(ad);
2634 break;
2635 /* GNUC typeof */
2636 case TOK_TYPEOF1:
2637 case TOK_TYPEOF2:
2638 case TOK_TYPEOF3:
2639 next();
2640 parse_expr_type(&type1);
2641 goto basic_type2;
2642 default:
2643 if (typespec_found || typedef_found)
2644 goto the_end;
2645 s = sym_find(tok);
2646 if (!s || !(s->type.t & VT_TYPEDEF))
2647 goto the_end;
2648 typedef_found = 1;
2649 t |= (s->type.t & ~VT_TYPEDEF);
2650 type->ref = s->type.ref;
2651 next();
2652 typespec_found = 1;
2653 break;
2655 type_found = 1;
2657 the_end:
2658 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
2659 error("signed and unsigned modifier");
2660 if (tcc_state->char_is_unsigned) {
2661 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
2662 t |= VT_UNSIGNED;
2664 t &= ~VT_SIGNED;
2666 /* long is never used as type */
2667 if ((t & VT_BTYPE) == VT_LONG)
2668 #if !defined TCC_TARGET_X86_64 || defined TCC_TARGET_PE
2669 t = (t & ~VT_BTYPE) | VT_INT;
2670 #else
2671 t = (t & ~VT_BTYPE) | VT_LLONG;
2672 #endif
2673 type->t = t;
2674 return type_found;
2677 /* convert a function parameter type (array to pointer and function to
2678 function pointer) */
2679 static inline void convert_parameter_type(CType *pt)
2681 /* remove const and volatile qualifiers (XXX: const could be used
2682 to indicate a const function parameter */
2683 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
2684 /* array must be transformed to pointer according to ANSI C */
2685 pt->t &= ~VT_ARRAY;
2686 if ((pt->t & VT_BTYPE) == VT_FUNC) {
2687 mk_pointer(pt);
2691 static void post_type(CType *type, AttributeDef *ad)
2693 int n, l, t1, arg_size, align;
2694 Sym **plast, *s, *first;
2695 AttributeDef ad1;
2696 CType pt;
2698 if (tok == '(') {
2699 /* function declaration */
2700 next();
2701 l = 0;
2702 first = NULL;
2703 plast = &first;
2704 arg_size = 0;
2705 if (tok != ')') {
2706 for(;;) {
2707 /* read param name and compute offset */
2708 if (l != FUNC_OLD) {
2709 if (!parse_btype(&pt, &ad1)) {
2710 if (l) {
2711 error("invalid type");
2712 } else {
2713 l = FUNC_OLD;
2714 goto old_proto;
2717 l = FUNC_NEW;
2718 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
2719 break;
2720 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
2721 if ((pt.t & VT_BTYPE) == VT_VOID)
2722 error("parameter declared as void");
2723 arg_size += (type_size(&pt, &align) + 3) & ~3;
2724 } else {
2725 old_proto:
2726 n = tok;
2727 if (n < TOK_UIDENT)
2728 expect("identifier");
2729 pt.t = VT_INT;
2730 next();
2732 convert_parameter_type(&pt);
2733 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
2734 *plast = s;
2735 plast = &s->next;
2736 if (tok == ')')
2737 break;
2738 skip(',');
2739 if (l == FUNC_NEW && tok == TOK_DOTS) {
2740 l = FUNC_ELLIPSIS;
2741 next();
2742 break;
2746 /* if no parameters, then old type prototype */
2747 if (l == 0)
2748 l = FUNC_OLD;
2749 skip(')');
2750 t1 = type->t & VT_STORAGE;
2751 /* NOTE: const is ignored in returned type as it has a special
2752 meaning in gcc / C++ */
2753 type->t &= ~(VT_STORAGE | VT_CONSTANT);
2754 post_type(type, ad);
2755 /* we push a anonymous symbol which will contain the function prototype */
2756 FUNC_ARGS(ad->func_attr) = arg_size;
2757 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
2758 s->next = first;
2759 type->t = t1 | VT_FUNC;
2760 type->ref = s;
2761 } else if (tok == '[') {
2762 /* array definition */
2763 next();
2764 if (tok == TOK_RESTRICT1)
2765 next();
2766 n = -1;
2767 if (tok != ']') {
2768 n = expr_const();
2769 if (n < 0)
2770 error("invalid array size");
2772 skip(']');
2773 /* parse next post type */
2774 t1 = type->t & VT_STORAGE;
2775 type->t &= ~VT_STORAGE;
2776 post_type(type, ad);
2778 /* we push a anonymous symbol which will contain the array
2779 element type */
2780 s = sym_push(SYM_FIELD, type, 0, n);
2781 type->t = t1 | VT_ARRAY | VT_PTR;
2782 type->ref = s;
2786 /* Parse a type declaration (except basic type), and return the type
2787 in 'type'. 'td' is a bitmask indicating which kind of type decl is
2788 expected. 'type' should contain the basic type. 'ad' is the
2789 attribute definition of the basic type. It can be modified by
2790 type_decl().
2792 static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
2794 Sym *s;
2795 CType type1, *type2;
2796 int qualifiers;
2798 while (tok == '*') {
2799 qualifiers = 0;
2800 redo:
2801 next();
2802 switch(tok) {
2803 case TOK_CONST1:
2804 case TOK_CONST2:
2805 case TOK_CONST3:
2806 qualifiers |= VT_CONSTANT;
2807 goto redo;
2808 case TOK_VOLATILE1:
2809 case TOK_VOLATILE2:
2810 case TOK_VOLATILE3:
2811 qualifiers |= VT_VOLATILE;
2812 goto redo;
2813 case TOK_RESTRICT1:
2814 case TOK_RESTRICT2:
2815 case TOK_RESTRICT3:
2816 goto redo;
2818 mk_pointer(type);
2819 type->t |= qualifiers;
2822 /* XXX: clarify attribute handling */
2823 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2824 parse_attribute(ad);
2826 /* recursive type */
2827 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
2828 type1.t = 0; /* XXX: same as int */
2829 if (tok == '(') {
2830 next();
2831 /* XXX: this is not correct to modify 'ad' at this point, but
2832 the syntax is not clear */
2833 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2834 parse_attribute(ad);
2835 type_decl(&type1, ad, v, td);
2836 skip(')');
2837 } else {
2838 /* type identifier */
2839 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
2840 *v = tok;
2841 next();
2842 } else {
2843 if (!(td & TYPE_ABSTRACT))
2844 expect("identifier");
2845 *v = 0;
2848 post_type(type, ad);
2849 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
2850 parse_attribute(ad);
2851 if (!type1.t)
2852 return;
2853 /* append type at the end of type1 */
2854 type2 = &type1;
2855 for(;;) {
2856 s = type2->ref;
2857 type2 = &s->type;
2858 if (!type2->t) {
2859 *type2 = *type;
2860 break;
2863 *type = type1;
2866 /* compute the lvalue VT_LVAL_xxx needed to match type t. */
2867 int lvalue_type(int t)
2869 int bt, r;
2870 r = VT_LVAL;
2871 bt = t & VT_BTYPE;
2872 if (bt == VT_BYTE || bt == VT_BOOL)
2873 r |= VT_LVAL_BYTE;
2874 else if (bt == VT_SHORT)
2875 r |= VT_LVAL_SHORT;
2876 else
2877 return r;
2878 if (t & VT_UNSIGNED)
2879 r |= VT_LVAL_UNSIGNED;
2880 return r;
2883 /* indirection with full error checking and bound check */
2884 static void indir(void)
2886 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
2887 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
2888 return;
2889 expect("pointer");
2891 if ((vtop->r & VT_LVAL) && !nocode_wanted)
2892 gv(RC_INT);
2893 vtop->type = *pointed_type(&vtop->type);
2894 /* Arrays and functions are never lvalues */
2895 if (!(vtop->type.t & VT_ARRAY)
2896 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
2897 vtop->r |= lvalue_type(vtop->type.t);
2898 /* if bound checking, the referenced pointer must be checked */
2899 if (tcc_state->do_bounds_check)
2900 vtop->r |= VT_MUSTBOUND;
2904 /* pass a parameter to a function and do type checking and casting */
2905 static void gfunc_param_typed(Sym *func, Sym *arg)
2907 int func_type;
2908 CType type;
2910 func_type = func->c;
2911 if (func_type == FUNC_OLD ||
2912 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
2913 /* default casting : only need to convert float to double */
2914 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
2915 type.t = VT_DOUBLE;
2916 gen_cast(&type);
2918 } else if (arg == NULL) {
2919 error("too many arguments to function");
2920 } else {
2921 type = arg->type;
2922 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
2923 gen_assign_cast(&type);
2927 /* parse an expression of the form '(type)' or '(expr)' and return its
2928 type */
2929 static void parse_expr_type(CType *type)
2931 int n;
2932 AttributeDef ad;
2934 skip('(');
2935 if (parse_btype(type, &ad)) {
2936 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2937 } else {
2938 expr_type(type);
2940 skip(')');
2943 static void parse_type(CType *type)
2945 AttributeDef ad;
2946 int n;
2948 if (!parse_btype(type, &ad)) {
2949 expect("type");
2951 type_decl(type, &ad, &n, TYPE_ABSTRACT);
2954 static void vpush_tokc(int t)
2956 CType type;
2957 type.t = t;
2958 vsetc(&type, VT_CONST, &tokc);
2961 static void unary(void)
2963 int n, t, align, size, r;
2964 CType type;
2965 Sym *s;
2966 AttributeDef ad;
2968 /* XXX: GCC 2.95.3 does not generate a table although it should be
2969 better here */
2970 tok_next:
2971 switch(tok) {
2972 case TOK_EXTENSION:
2973 next();
2974 goto tok_next;
2975 case TOK_CINT:
2976 case TOK_CCHAR:
2977 case TOK_LCHAR:
2978 vpushi(tokc.i);
2979 next();
2980 break;
2981 case TOK_CUINT:
2982 vpush_tokc(VT_INT | VT_UNSIGNED);
2983 next();
2984 break;
2985 case TOK_CLLONG:
2986 vpush_tokc(VT_LLONG);
2987 next();
2988 break;
2989 case TOK_CULLONG:
2990 vpush_tokc(VT_LLONG | VT_UNSIGNED);
2991 next();
2992 break;
2993 case TOK_CFLOAT:
2994 vpush_tokc(VT_FLOAT);
2995 next();
2996 break;
2997 case TOK_CDOUBLE:
2998 vpush_tokc(VT_DOUBLE);
2999 next();
3000 break;
3001 case TOK_CLDOUBLE:
3002 vpush_tokc(VT_LDOUBLE);
3003 next();
3004 break;
3005 case TOK___FUNCTION__:
3006 if (!gnu_ext)
3007 goto tok_identifier;
3008 /* fall thru */
3009 case TOK___FUNC__:
3011 void *ptr;
3012 int len;
3013 /* special function name identifier */
3014 len = strlen(funcname) + 1;
3015 /* generate char[len] type */
3016 type.t = VT_BYTE;
3017 mk_pointer(&type);
3018 type.t |= VT_ARRAY;
3019 type.ref->c = len;
3020 vpush_ref(&type, data_section, data_section->data_offset, len);
3021 ptr = section_ptr_add(data_section, len);
3022 memcpy(ptr, funcname, len);
3023 next();
3025 break;
3026 case TOK_LSTR:
3027 #ifdef TCC_TARGET_PE
3028 t = VT_SHORT | VT_UNSIGNED;
3029 #else
3030 t = VT_INT;
3031 #endif
3032 goto str_init;
3033 case TOK_STR:
3034 /* string parsing */
3035 t = VT_BYTE;
3036 str_init:
3037 if (tcc_state->warn_write_strings)
3038 t |= VT_CONSTANT;
3039 type.t = t;
3040 mk_pointer(&type);
3041 type.t |= VT_ARRAY;
3042 memset(&ad, 0, sizeof(AttributeDef));
3043 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
3044 break;
3045 case '(':
3046 next();
3047 /* cast ? */
3048 if (parse_btype(&type, &ad)) {
3049 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
3050 skip(')');
3051 /* check ISOC99 compound literal */
3052 if (tok == '{') {
3053 /* data is allocated locally by default */
3054 if (global_expr)
3055 r = VT_CONST;
3056 else
3057 r = VT_LOCAL;
3058 /* all except arrays are lvalues */
3059 if (!(type.t & VT_ARRAY))
3060 r |= lvalue_type(type.t);
3061 memset(&ad, 0, sizeof(AttributeDef));
3062 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
3063 } else {
3064 unary();
3065 gen_cast(&type);
3067 } else if (tok == '{') {
3068 /* save all registers */
3069 save_regs(0);
3070 /* statement expression : we do not accept break/continue
3071 inside as GCC does */
3072 block(NULL, NULL, NULL, NULL, 0, 1);
3073 skip(')');
3074 } else {
3075 gexpr();
3076 skip(')');
3078 break;
3079 case '*':
3080 next();
3081 unary();
3082 indir();
3083 break;
3084 case '&':
3085 next();
3086 unary();
3087 /* functions names must be treated as function pointers,
3088 except for unary '&' and sizeof. Since we consider that
3089 functions are not lvalues, we only have to handle it
3090 there and in function calls. */
3091 /* arrays can also be used although they are not lvalues */
3092 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
3093 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
3094 test_lvalue();
3095 mk_pointer(&vtop->type);
3096 gaddrof();
3097 break;
3098 case '!':
3099 next();
3100 unary();
3101 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
3102 CType boolean;
3103 boolean.t = VT_BOOL;
3104 gen_cast(&boolean);
3105 vtop->c.i = !vtop->c.i;
3106 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
3107 vtop->c.i = vtop->c.i ^ 1;
3108 else {
3109 save_regs(1);
3110 vseti(VT_JMP, gtst(1, 0));
3112 break;
3113 case '~':
3114 next();
3115 unary();
3116 vpushi(-1);
3117 gen_op('^');
3118 break;
3119 case '+':
3120 next();
3121 /* in order to force cast, we add zero */
3122 unary();
3123 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
3124 error("pointer not accepted for unary plus");
3125 vpushi(0);
3126 gen_op('+');
3127 break;
3128 case TOK_SIZEOF:
3129 case TOK_ALIGNOF1:
3130 case TOK_ALIGNOF2:
3131 t = tok;
3132 next();
3133 if (tok == '(') {
3134 parse_expr_type(&type);
3135 } else {
3136 unary_type(&type);
3138 size = type_size(&type, &align);
3139 if (t == TOK_SIZEOF) {
3140 if (size < 0)
3141 error("sizeof applied to an incomplete type");
3142 vpushi(size);
3143 } else {
3144 vpushi(align);
3146 vtop->type.t |= VT_UNSIGNED;
3147 break;
3149 case TOK_builtin_types_compatible_p:
3151 CType type1, type2;
3152 next();
3153 skip('(');
3154 parse_type(&type1);
3155 skip(',');
3156 parse_type(&type2);
3157 skip(')');
3158 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3159 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3160 vpushi(is_compatible_types(&type1, &type2));
3162 break;
3163 case TOK_builtin_constant_p:
3165 int saved_nocode_wanted, res;
3166 next();
3167 skip('(');
3168 saved_nocode_wanted = nocode_wanted;
3169 nocode_wanted = 1;
3170 gexpr();
3171 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
3172 vpop();
3173 nocode_wanted = saved_nocode_wanted;
3174 skip(')');
3175 vpushi(res);
3177 break;
3178 case TOK_builtin_frame_address:
3180 CType type;
3181 next();
3182 skip('(');
3183 if (tok != TOK_CINT) {
3184 error("__builtin_frame_address only takes integers");
3186 if (tokc.i != 0) {
3187 error("TCC only supports __builtin_frame_address(0)");
3189 next();
3190 skip(')');
3191 type.t = VT_VOID;
3192 mk_pointer(&type);
3193 vset(&type, VT_LOCAL, 0);
3195 break;
3196 #ifdef TCC_TARGET_X86_64
3197 case TOK_builtin_malloc:
3198 tok = TOK_malloc;
3199 goto tok_identifier;
3200 case TOK_builtin_free:
3201 tok = TOK_free;
3202 goto tok_identifier;
3203 #endif
3204 case TOK_INC:
3205 case TOK_DEC:
3206 t = tok;
3207 next();
3208 unary();
3209 inc(0, t);
3210 break;
3211 case '-':
3212 next();
3213 vpushi(0);
3214 unary();
3215 gen_op('-');
3216 break;
3217 case TOK_LAND:
3218 if (!gnu_ext)
3219 goto tok_identifier;
3220 next();
3221 /* allow to take the address of a label */
3222 if (tok < TOK_UIDENT)
3223 expect("label identifier");
3224 s = label_find(tok);
3225 if (!s) {
3226 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
3227 } else {
3228 if (s->r == LABEL_DECLARED)
3229 s->r = LABEL_FORWARD;
3231 if (!s->type.t) {
3232 s->type.t = VT_VOID;
3233 mk_pointer(&s->type);
3234 s->type.t |= VT_STATIC;
3236 vset(&s->type, VT_CONST | VT_SYM, 0);
3237 vtop->sym = s;
3238 next();
3239 break;
3240 default:
3241 tok_identifier:
3242 t = tok;
3243 next();
3244 if (t < TOK_UIDENT)
3245 expect("identifier");
3246 s = sym_find(t);
3247 if (!s) {
3248 if (tok != '(')
3249 error("'%s' undeclared", get_tok_str(t, NULL));
3250 /* for simple function calls, we tolerate undeclared
3251 external reference to int() function */
3252 if (tcc_state->warn_implicit_function_declaration)
3253 warning("implicit declaration of function '%s'",
3254 get_tok_str(t, NULL));
3255 s = external_global_sym(t, &func_old_type, 0);
3257 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
3258 (VT_STATIC | VT_INLINE | VT_FUNC)) {
3259 /* if referencing an inline function, then we generate a
3260 symbol to it if not already done. It will have the
3261 effect to generate code for it at the end of the
3262 compilation unit. Inline function as always
3263 generated in the text section. */
3264 if (!s->c)
3265 put_extern_sym(s, text_section, 0, 0);
3266 r = VT_SYM | VT_CONST;
3267 } else {
3268 r = s->r;
3270 vset(&s->type, r, s->c);
3271 /* if forward reference, we must point to s */
3272 if (vtop->r & VT_SYM) {
3273 vtop->sym = s;
3274 vtop->c.ul = 0;
3276 break;
3279 /* post operations */
3280 while (1) {
3281 if (tok == TOK_INC || tok == TOK_DEC) {
3282 inc(1, tok);
3283 next();
3284 } else if (tok == '.' || tok == TOK_ARROW) {
3285 int qualifiers;
3286 /* field */
3287 if (tok == TOK_ARROW)
3288 indir();
3289 qualifiers = vtop->type.t & (VT_CONSTANT | VT_VOLATILE);
3290 test_lvalue();
3291 gaddrof();
3292 next();
3293 /* expect pointer on structure */
3294 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
3295 expect("struct or union");
3296 s = vtop->type.ref;
3297 /* find field */
3298 tok |= SYM_FIELD;
3299 while ((s = s->next) != NULL) {
3300 if (s->v == tok)
3301 break;
3303 if (!s)
3304 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
3305 /* add field offset to pointer */
3306 vtop->type = char_pointer_type; /* change type to 'char *' */
3307 vpushi(s->c);
3308 gen_op('+');
3309 /* change type to field type, and set to lvalue */
3310 vtop->type = s->type;
3311 vtop->type.t |= qualifiers;
3312 /* an array is never an lvalue */
3313 if (!(vtop->type.t & VT_ARRAY)) {
3314 vtop->r |= lvalue_type(vtop->type.t);
3315 /* if bound checking, the referenced pointer must be checked */
3316 if (tcc_state->do_bounds_check)
3317 vtop->r |= VT_MUSTBOUND;
3319 next();
3320 } else if (tok == '[') {
3321 next();
3322 gexpr();
3323 gen_op('+');
3324 indir();
3325 skip(']');
3326 } else if (tok == '(') {
3327 SValue ret;
3328 Sym *sa;
3329 int nb_args;
3331 /* function call */
3332 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
3333 /* pointer test (no array accepted) */
3334 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
3335 vtop->type = *pointed_type(&vtop->type);
3336 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
3337 goto error_func;
3338 } else {
3339 error_func:
3340 expect("function pointer");
3342 } else {
3343 vtop->r &= ~VT_LVAL; /* no lvalue */
3345 /* get return type */
3346 s = vtop->type.ref;
3347 next();
3348 sa = s->next; /* first parameter */
3349 nb_args = 0;
3350 ret.r2 = VT_CONST;
3351 /* compute first implicit argument if a structure is returned */
3352 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
3353 /* get some space for the returned structure */
3354 size = type_size(&s->type, &align);
3355 loc = (loc - size) & -align;
3356 ret.type = s->type;
3357 ret.r = VT_LOCAL | VT_LVAL;
3358 /* pass it as 'int' to avoid structure arg passing
3359 problems */
3360 vseti(VT_LOCAL, loc);
3361 ret.c = vtop->c;
3362 nb_args++;
3363 } else {
3364 ret.type = s->type;
3365 /* return in register */
3366 if (is_float(ret.type.t)) {
3367 ret.r = reg_fret(ret.type.t);
3368 } else {
3369 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
3370 ret.r2 = REG_LRET;
3371 ret.r = REG_IRET;
3373 ret.c.i = 0;
3375 if (tok != ')') {
3376 for(;;) {
3377 expr_eq();
3378 gfunc_param_typed(s, sa);
3379 nb_args++;
3380 if (sa)
3381 sa = sa->next;
3382 if (tok == ')')
3383 break;
3384 skip(',');
3387 if (sa)
3388 error("too few arguments to function");
3389 skip(')');
3390 if (!nocode_wanted) {
3391 gfunc_call(nb_args);
3392 } else {
3393 vtop -= (nb_args + 1);
3395 /* return value */
3396 vsetc(&ret.type, ret.r, &ret.c);
3397 vtop->r2 = ret.r2;
3398 } else {
3399 break;
3404 static void uneq(void)
3406 int t;
3408 unary();
3409 if (tok == '=' ||
3410 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
3411 tok == TOK_A_XOR || tok == TOK_A_OR ||
3412 tok == TOK_A_SHL || tok == TOK_A_SAR) {
3413 test_lvalue();
3414 t = tok;
3415 next();
3416 if (t == '=') {
3417 expr_eq();
3418 } else {
3419 vdup();
3420 expr_eq();
3421 gen_op(t & 0x7f);
3423 vstore();
3427 static void expr_prod(void)
3429 int t;
3431 uneq();
3432 while (tok == '*' || tok == '/' || tok == '%') {
3433 t = tok;
3434 next();
3435 uneq();
3436 gen_op(t);
3440 static void expr_sum(void)
3442 int t;
3444 expr_prod();
3445 while (tok == '+' || tok == '-') {
3446 t = tok;
3447 next();
3448 expr_prod();
3449 gen_op(t);
3453 static void expr_shift(void)
3455 int t;
3457 expr_sum();
3458 while (tok == TOK_SHL || tok == TOK_SAR) {
3459 t = tok;
3460 next();
3461 expr_sum();
3462 gen_op(t);
3466 static void expr_cmp(void)
3468 int t;
3470 expr_shift();
3471 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
3472 tok == TOK_ULT || tok == TOK_UGE) {
3473 t = tok;
3474 next();
3475 expr_shift();
3476 gen_op(t);
3480 static void expr_cmpeq(void)
3482 int t;
3484 expr_cmp();
3485 while (tok == TOK_EQ || tok == TOK_NE) {
3486 t = tok;
3487 next();
3488 expr_cmp();
3489 gen_op(t);
3493 static void expr_and(void)
3495 expr_cmpeq();
3496 while (tok == '&') {
3497 next();
3498 expr_cmpeq();
3499 gen_op('&');
3503 static void expr_xor(void)
3505 expr_and();
3506 while (tok == '^') {
3507 next();
3508 expr_and();
3509 gen_op('^');
3513 static void expr_or(void)
3515 expr_xor();
3516 while (tok == '|') {
3517 next();
3518 expr_xor();
3519 gen_op('|');
3523 /* XXX: fix this mess */
3524 static void expr_land_const(void)
3526 expr_or();
3527 while (tok == TOK_LAND) {
3528 next();
3529 expr_or();
3530 gen_op(TOK_LAND);
3534 /* XXX: fix this mess */
3535 static void expr_lor_const(void)
3537 expr_land_const();
3538 while (tok == TOK_LOR) {
3539 next();
3540 expr_land_const();
3541 gen_op(TOK_LOR);
3545 /* only used if non constant */
3546 static void expr_land(void)
3548 int t;
3550 expr_or();
3551 if (tok == TOK_LAND) {
3552 t = 0;
3553 save_regs(1);
3554 for(;;) {
3555 t = gtst(1, t);
3556 if (tok != TOK_LAND) {
3557 vseti(VT_JMPI, t);
3558 break;
3560 next();
3561 expr_or();
3566 static void expr_lor(void)
3568 int t;
3570 expr_land();
3571 if (tok == TOK_LOR) {
3572 t = 0;
3573 save_regs(1);
3574 for(;;) {
3575 t = gtst(0, t);
3576 if (tok != TOK_LOR) {
3577 vseti(VT_JMP, t);
3578 break;
3580 next();
3581 expr_land();
3586 /* XXX: better constant handling */
3587 static void expr_eq(void)
3589 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
3590 SValue sv;
3591 CType type, type1, type2;
3593 if (const_wanted) {
3594 expr_lor_const();
3595 if (tok == '?') {
3596 CType boolean;
3597 int c;
3598 boolean.t = VT_BOOL;
3599 vdup();
3600 gen_cast(&boolean);
3601 c = vtop->c.i;
3602 vpop();
3603 next();
3604 if (tok != ':' || !gnu_ext) {
3605 vpop();
3606 gexpr();
3608 if (!c)
3609 vpop();
3610 skip(':');
3611 expr_eq();
3612 if (c)
3613 vpop();
3615 } else {
3616 expr_lor();
3617 if (tok == '?') {
3618 next();
3619 if (vtop != vstack) {
3620 /* needed to avoid having different registers saved in
3621 each branch */
3622 if (is_float(vtop->type.t)) {
3623 rc = RC_FLOAT;
3624 #ifdef TCC_TARGET_X86_64
3625 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
3626 rc = RC_ST0;
3628 #endif
3630 else
3631 rc = RC_INT;
3632 gv(rc);
3633 save_regs(1);
3635 if (tok == ':' && gnu_ext) {
3636 gv_dup();
3637 tt = gtst(1, 0);
3638 } else {
3639 tt = gtst(1, 0);
3640 gexpr();
3642 type1 = vtop->type;
3643 sv = *vtop; /* save value to handle it later */
3644 vtop--; /* no vpop so that FP stack is not flushed */
3645 skip(':');
3646 u = gjmp(0);
3647 gsym(tt);
3648 expr_eq();
3649 type2 = vtop->type;
3651 t1 = type1.t;
3652 bt1 = t1 & VT_BTYPE;
3653 t2 = type2.t;
3654 bt2 = t2 & VT_BTYPE;
3655 /* cast operands to correct type according to ISOC rules */
3656 if (is_float(bt1) || is_float(bt2)) {
3657 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
3658 type.t = VT_LDOUBLE;
3659 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
3660 type.t = VT_DOUBLE;
3661 } else {
3662 type.t = VT_FLOAT;
3664 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
3665 /* cast to biggest op */
3666 type.t = VT_LLONG;
3667 /* convert to unsigned if it does not fit in a long long */
3668 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
3669 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
3670 type.t |= VT_UNSIGNED;
3671 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
3672 /* XXX: test pointer compatibility */
3673 type = type1;
3674 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
3675 /* XXX: test function pointer compatibility */
3676 type = type1;
3677 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
3678 /* XXX: test structure compatibility */
3679 type = type1;
3680 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
3681 /* NOTE: as an extension, we accept void on only one side */
3682 type.t = VT_VOID;
3683 } else {
3684 /* integer operations */
3685 type.t = VT_INT;
3686 /* convert to unsigned if it does not fit in an integer */
3687 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
3688 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
3689 type.t |= VT_UNSIGNED;
3692 /* now we convert second operand */
3693 gen_cast(&type);
3694 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3695 gaddrof();
3696 rc = RC_INT;
3697 if (is_float(type.t)) {
3698 rc = RC_FLOAT;
3699 #ifdef TCC_TARGET_X86_64
3700 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
3701 rc = RC_ST0;
3703 #endif
3704 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
3705 /* for long longs, we use fixed registers to avoid having
3706 to handle a complicated move */
3707 rc = RC_IRET;
3710 r2 = gv(rc);
3711 /* this is horrible, but we must also convert first
3712 operand */
3713 tt = gjmp(0);
3714 gsym(u);
3715 /* put again first value and cast it */
3716 *vtop = sv;
3717 gen_cast(&type);
3718 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
3719 gaddrof();
3720 r1 = gv(rc);
3721 move_reg(r2, r1);
3722 vtop->r = r2;
3723 gsym(tt);
3728 void gexpr(void)
3730 while (1) {
3731 expr_eq();
3732 if (tok != ',')
3733 break;
3734 vpop();
3735 next();
3739 /* parse an expression and return its type without any side effect. */
3740 static void expr_type(CType *type)
3742 int saved_nocode_wanted;
3744 saved_nocode_wanted = nocode_wanted;
3745 nocode_wanted = 1;
3746 gexpr();
3747 *type = vtop->type;
3748 vpop();
3749 nocode_wanted = saved_nocode_wanted;
3752 /* parse a unary expression and return its type without any side
3753 effect. */
3754 static void unary_type(CType *type)
3756 int a;
3758 a = nocode_wanted;
3759 nocode_wanted = 1;
3760 unary();
3761 *type = vtop->type;
3762 vpop();
3763 nocode_wanted = a;
3766 /* parse a constant expression and return value in vtop. */
3767 static void expr_const1(void)
3769 int a;
3770 a = const_wanted;
3771 const_wanted = 1;
3772 expr_eq();
3773 const_wanted = a;
3776 /* parse an integer constant and return its value. */
3777 int expr_const(void)
3779 int c;
3780 expr_const1();
3781 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
3782 expect("constant expression");
3783 c = vtop->c.i;
3784 vpop();
3785 return c;
3788 /* return the label token if current token is a label, otherwise
3789 return zero */
3790 static int is_label(void)
3792 int last_tok;
3794 /* fast test first */
3795 if (tok < TOK_UIDENT)
3796 return 0;
3797 /* no need to save tokc because tok is an identifier */
3798 last_tok = tok;
3799 next();
3800 if (tok == ':') {
3801 next();
3802 return last_tok;
3803 } else {
3804 unget_tok(last_tok);
3805 return 0;
3809 static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
3810 int case_reg, int is_expr)
3812 int a, b, c, d;
3813 Sym *s;
3815 /* generate line number info */
3816 if (tcc_state->do_debug &&
3817 (last_line_num != file->line_num || last_ind != ind)) {
3818 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
3819 last_ind = ind;
3820 last_line_num = file->line_num;
3823 if (is_expr) {
3824 /* default return value is (void) */
3825 vpushi(0);
3826 vtop->type.t = VT_VOID;
3829 if (tok == TOK_IF) {
3830 /* if test */
3831 next();
3832 skip('(');
3833 gexpr();
3834 skip(')');
3835 a = gtst(1, 0);
3836 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3837 c = tok;
3838 if (c == TOK_ELSE) {
3839 next();
3840 d = gjmp(0);
3841 gsym(a);
3842 block(bsym, csym, case_sym, def_sym, case_reg, 0);
3843 gsym(d); /* patch else jmp */
3844 } else
3845 gsym(a);
3846 } else if (tok == TOK_WHILE) {
3847 next();
3848 d = ind;
3849 skip('(');
3850 gexpr();
3851 skip(')');
3852 a = gtst(1, 0);
3853 b = 0;
3854 block(&a, &b, case_sym, def_sym, case_reg, 0);
3855 gjmp_addr(d);
3856 gsym(a);
3857 gsym_addr(b, d);
3858 } else if (tok == '{') {
3859 Sym *llabel;
3861 next();
3862 /* record local declaration stack position */
3863 s = local_stack;
3864 llabel = local_label_stack;
3865 /* handle local labels declarations */
3866 if (tok == TOK_LABEL) {
3867 next();
3868 for(;;) {
3869 if (tok < TOK_UIDENT)
3870 expect("label identifier");
3871 label_push(&local_label_stack, tok, LABEL_DECLARED);
3872 next();
3873 if (tok == ',') {
3874 next();
3875 } else {
3876 skip(';');
3877 break;
3881 while (tok != '}') {
3882 decl(VT_LOCAL);
3883 if (tok != '}') {
3884 if (is_expr)
3885 vpop();
3886 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
3889 /* pop locally defined labels */
3890 label_pop(&local_label_stack, llabel);
3891 /* pop locally defined symbols */
3892 if(is_expr) {
3893 /* XXX: this solution makes only valgrind happy...
3894 triggered by gcc.c-torture/execute/20000917-1.c */
3895 Sym *p;
3896 switch(vtop->type.t & VT_BTYPE) {
3897 case VT_PTR:
3898 case VT_STRUCT:
3899 case VT_ENUM:
3900 case VT_FUNC:
3901 for(p=vtop->type.ref;p;p=p->prev)
3902 if(p->prev==s)
3903 error("unsupported expression type");
3906 sym_pop(&local_stack, s);
3907 next();
3908 } else if (tok == TOK_RETURN) {
3909 next();
3910 if (tok != ';') {
3911 gexpr();
3912 gen_assign_cast(&func_vt);
3913 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
3914 CType type;
3915 /* if returning structure, must copy it to implicit
3916 first pointer arg location */
3917 #ifdef TCC_ARM_EABI
3918 int align, size;
3919 size = type_size(&func_vt,&align);
3920 if(size <= 4)
3922 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
3923 && (align & 3))
3925 int addr;
3926 loc = (loc - size) & -4;
3927 addr = loc;
3928 type = func_vt;
3929 vset(&type, VT_LOCAL | VT_LVAL, addr);
3930 vswap();
3931 vstore();
3932 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
3934 vtop->type = int_type;
3935 gv(RC_IRET);
3936 } else {
3937 #endif
3938 type = func_vt;
3939 mk_pointer(&type);
3940 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
3941 indir();
3942 vswap();
3943 /* copy structure value to pointer */
3944 vstore();
3945 #ifdef TCC_ARM_EABI
3947 #endif
3948 } else if (is_float(func_vt.t)) {
3949 gv(rc_fret(func_vt.t));
3950 } else {
3951 gv(RC_IRET);
3953 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3955 skip(';');
3956 rsym = gjmp(rsym); /* jmp */
3957 } else if (tok == TOK_BREAK) {
3958 /* compute jump */
3959 if (!bsym)
3960 error("cannot break");
3961 *bsym = gjmp(*bsym);
3962 next();
3963 skip(';');
3964 } else if (tok == TOK_CONTINUE) {
3965 /* compute jump */
3966 if (!csym)
3967 error("cannot continue");
3968 *csym = gjmp(*csym);
3969 next();
3970 skip(';');
3971 } else if (tok == TOK_FOR) {
3972 int e;
3973 next();
3974 skip('(');
3975 if (tok != ';') {
3976 gexpr();
3977 vpop();
3979 skip(';');
3980 d = ind;
3981 c = ind;
3982 a = 0;
3983 b = 0;
3984 if (tok != ';') {
3985 gexpr();
3986 a = gtst(1, 0);
3988 skip(';');
3989 if (tok != ')') {
3990 e = gjmp(0);
3991 c = ind;
3992 gexpr();
3993 vpop();
3994 gjmp_addr(d);
3995 gsym(e);
3997 skip(')');
3998 block(&a, &b, case_sym, def_sym, case_reg, 0);
3999 gjmp_addr(c);
4000 gsym(a);
4001 gsym_addr(b, c);
4002 } else
4003 if (tok == TOK_DO) {
4004 next();
4005 a = 0;
4006 b = 0;
4007 d = ind;
4008 block(&a, &b, case_sym, def_sym, case_reg, 0);
4009 skip(TOK_WHILE);
4010 skip('(');
4011 gsym(b);
4012 gexpr();
4013 c = gtst(0, 0);
4014 gsym_addr(c, d);
4015 skip(')');
4016 gsym(a);
4017 skip(';');
4018 } else
4019 if (tok == TOK_SWITCH) {
4020 next();
4021 skip('(');
4022 gexpr();
4023 /* XXX: other types than integer */
4024 case_reg = gv(RC_INT);
4025 vpop();
4026 skip(')');
4027 a = 0;
4028 b = gjmp(0); /* jump to first case */
4029 c = 0;
4030 block(&a, csym, &b, &c, case_reg, 0);
4031 /* if no default, jmp after switch */
4032 if (c == 0)
4033 c = ind;
4034 /* default label */
4035 gsym_addr(b, c);
4036 /* break label */
4037 gsym(a);
4038 } else
4039 if (tok == TOK_CASE) {
4040 int v1, v2;
4041 if (!case_sym)
4042 expect("switch");
4043 next();
4044 v1 = expr_const();
4045 v2 = v1;
4046 if (gnu_ext && tok == TOK_DOTS) {
4047 next();
4048 v2 = expr_const();
4049 if (v2 < v1)
4050 warning("empty case range");
4052 /* since a case is like a label, we must skip it with a jmp */
4053 b = gjmp(0);
4054 gsym(*case_sym);
4055 vseti(case_reg, 0);
4056 vpushi(v1);
4057 if (v1 == v2) {
4058 gen_op(TOK_EQ);
4059 *case_sym = gtst(1, 0);
4060 } else {
4061 gen_op(TOK_GE);
4062 *case_sym = gtst(1, 0);
4063 vseti(case_reg, 0);
4064 vpushi(v2);
4065 gen_op(TOK_LE);
4066 *case_sym = gtst(1, *case_sym);
4068 gsym(b);
4069 skip(':');
4070 is_expr = 0;
4071 goto block_after_label;
4072 } else
4073 if (tok == TOK_DEFAULT) {
4074 next();
4075 skip(':');
4076 if (!def_sym)
4077 expect("switch");
4078 if (*def_sym)
4079 error("too many 'default'");
4080 *def_sym = ind;
4081 is_expr = 0;
4082 goto block_after_label;
4083 } else
4084 if (tok == TOK_GOTO) {
4085 next();
4086 if (tok == '*' && gnu_ext) {
4087 /* computed goto */
4088 next();
4089 gexpr();
4090 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
4091 expect("pointer");
4092 ggoto();
4093 } else if (tok >= TOK_UIDENT) {
4094 s = label_find(tok);
4095 /* put forward definition if needed */
4096 if (!s) {
4097 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4098 } else {
4099 if (s->r == LABEL_DECLARED)
4100 s->r = LABEL_FORWARD;
4102 /* label already defined */
4103 if (s->r & LABEL_FORWARD)
4104 s->jnext = gjmp(s->jnext);
4105 else
4106 gjmp_addr(s->jnext);
4107 next();
4108 } else {
4109 expect("label identifier");
4111 skip(';');
4112 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
4113 asm_instr();
4114 } else {
4115 b = is_label();
4116 if (b) {
4117 /* label case */
4118 s = label_find(b);
4119 if (s) {
4120 if (s->r == LABEL_DEFINED)
4121 error("duplicate label '%s'", get_tok_str(s->v, NULL));
4122 gsym(s->jnext);
4123 s->r = LABEL_DEFINED;
4124 } else {
4125 s = label_push(&global_label_stack, b, LABEL_DEFINED);
4127 s->jnext = ind;
4128 /* we accept this, but it is a mistake */
4129 block_after_label:
4130 if (tok == '}') {
4131 warning("deprecated use of label at end of compound statement");
4132 } else {
4133 if (is_expr)
4134 vpop();
4135 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4137 } else {
4138 /* expression case */
4139 if (tok != ';') {
4140 if (is_expr) {
4141 vpop();
4142 gexpr();
4143 } else {
4144 gexpr();
4145 vpop();
4148 skip(';');
4153 /* t is the array or struct type. c is the array or struct
4154 address. cur_index/cur_field is the pointer to the current
4155 value. 'size_only' is true if only size info is needed (only used
4156 in arrays) */
4157 static void decl_designator(CType *type, Section *sec, unsigned long c,
4158 int *cur_index, Sym **cur_field,
4159 int size_only)
4161 Sym *s, *f;
4162 int notfirst, index, index_last, align, l, nb_elems, elem_size;
4163 CType type1;
4165 notfirst = 0;
4166 elem_size = 0;
4167 nb_elems = 1;
4168 if (gnu_ext && (l = is_label()) != 0)
4169 goto struct_field;
4170 while (tok == '[' || tok == '.') {
4171 if (tok == '[') {
4172 if (!(type->t & VT_ARRAY))
4173 expect("array type");
4174 s = type->ref;
4175 next();
4176 index = expr_const();
4177 if (index < 0 || (s->c >= 0 && index >= s->c))
4178 expect("invalid index");
4179 if (tok == TOK_DOTS && gnu_ext) {
4180 next();
4181 index_last = expr_const();
4182 if (index_last < 0 ||
4183 (s->c >= 0 && index_last >= s->c) ||
4184 index_last < index)
4185 expect("invalid index");
4186 } else {
4187 index_last = index;
4189 skip(']');
4190 if (!notfirst)
4191 *cur_index = index_last;
4192 type = pointed_type(type);
4193 elem_size = type_size(type, &align);
4194 c += index * elem_size;
4195 /* NOTE: we only support ranges for last designator */
4196 nb_elems = index_last - index + 1;
4197 if (nb_elems != 1) {
4198 notfirst = 1;
4199 break;
4201 } else {
4202 next();
4203 l = tok;
4204 next();
4205 struct_field:
4206 if ((type->t & VT_BTYPE) != VT_STRUCT)
4207 expect("struct/union type");
4208 s = type->ref;
4209 l |= SYM_FIELD;
4210 f = s->next;
4211 while (f) {
4212 if (f->v == l)
4213 break;
4214 f = f->next;
4216 if (!f)
4217 expect("field");
4218 if (!notfirst)
4219 *cur_field = f;
4220 /* XXX: fix this mess by using explicit storage field */
4221 type1 = f->type;
4222 type1.t |= (type->t & ~VT_TYPE);
4223 type = &type1;
4224 c += f->c;
4226 notfirst = 1;
4228 if (notfirst) {
4229 if (tok == '=') {
4230 next();
4231 } else {
4232 if (!gnu_ext)
4233 expect("=");
4235 } else {
4236 if (type->t & VT_ARRAY) {
4237 index = *cur_index;
4238 type = pointed_type(type);
4239 c += index * type_size(type, &align);
4240 } else {
4241 f = *cur_field;
4242 if (!f)
4243 error("too many field init");
4244 /* XXX: fix this mess by using explicit storage field */
4245 type1 = f->type;
4246 type1.t |= (type->t & ~VT_TYPE);
4247 type = &type1;
4248 c += f->c;
4251 decl_initializer(type, sec, c, 0, size_only);
4253 /* XXX: make it more general */
4254 if (!size_only && nb_elems > 1) {
4255 unsigned long c_end;
4256 uint8_t *src, *dst;
4257 int i;
4259 if (!sec)
4260 error("range init not supported yet for dynamic storage");
4261 c_end = c + nb_elems * elem_size;
4262 if (c_end > sec->data_allocated)
4263 section_realloc(sec, c_end);
4264 src = sec->data + c;
4265 dst = src;
4266 for(i = 1; i < nb_elems; i++) {
4267 dst += elem_size;
4268 memcpy(dst, src, elem_size);
4273 #define EXPR_VAL 0
4274 #define EXPR_CONST 1
4275 #define EXPR_ANY 2
4277 /* store a value or an expression directly in global data or in local array */
4278 static void init_putv(CType *type, Section *sec, unsigned long c,
4279 int v, int expr_type)
4281 int saved_global_expr, bt, bit_pos, bit_size;
4282 void *ptr;
4283 unsigned long long bit_mask;
4284 CType dtype;
4286 switch(expr_type) {
4287 case EXPR_VAL:
4288 vpushi(v);
4289 break;
4290 case EXPR_CONST:
4291 /* compound literals must be allocated globally in this case */
4292 saved_global_expr = global_expr;
4293 global_expr = 1;
4294 expr_const1();
4295 global_expr = saved_global_expr;
4296 /* NOTE: symbols are accepted */
4297 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
4298 error("initializer element is not constant");
4299 break;
4300 case EXPR_ANY:
4301 expr_eq();
4302 break;
4305 dtype = *type;
4306 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
4308 if (sec) {
4309 /* XXX: not portable */
4310 /* XXX: generate error if incorrect relocation */
4311 gen_assign_cast(&dtype);
4312 bt = type->t & VT_BTYPE;
4313 /* we'll write at most 12 bytes */
4314 if (c + 12 > sec->data_allocated) {
4315 section_realloc(sec, c + 12);
4317 ptr = sec->data + c;
4318 /* XXX: make code faster ? */
4319 if (!(type->t & VT_BITFIELD)) {
4320 bit_pos = 0;
4321 bit_size = 32;
4322 bit_mask = -1LL;
4323 } else {
4324 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4325 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
4326 bit_mask = (1LL << bit_size) - 1;
4328 if ((vtop->r & VT_SYM) &&
4329 (bt == VT_BYTE ||
4330 bt == VT_SHORT ||
4331 bt == VT_DOUBLE ||
4332 bt == VT_LDOUBLE ||
4333 bt == VT_LLONG ||
4334 (bt == VT_INT && bit_size != 32)))
4335 error("initializer element is not computable at load time");
4336 switch(bt) {
4337 case VT_BOOL:
4338 vtop->c.i = (vtop->c.i != 0);
4339 case VT_BYTE:
4340 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4341 break;
4342 case VT_SHORT:
4343 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4344 break;
4345 case VT_DOUBLE:
4346 *(double *)ptr = vtop->c.d;
4347 break;
4348 case VT_LDOUBLE:
4349 *(long double *)ptr = vtop->c.ld;
4350 break;
4351 case VT_LLONG:
4352 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
4353 break;
4354 default:
4355 if (vtop->r & VT_SYM) {
4356 greloc(sec, vtop->sym, c, R_DATA_PTR);
4358 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
4359 break;
4361 vtop--;
4362 } else {
4363 vset(&dtype, VT_LOCAL|VT_LVAL, c);
4364 vswap();
4365 vstore();
4366 vpop();
4370 /* put zeros for variable based init */
4371 static void init_putz(CType *t, Section *sec, unsigned long c, int size)
4373 if (sec) {
4374 /* nothing to do because globals are already set to zero */
4375 } else {
4376 vpush_global_sym(&func_old_type, TOK_memset);
4377 vseti(VT_LOCAL, c);
4378 vpushi(0);
4379 vpushi(size);
4380 gfunc_call(3);
4384 /* 't' contains the type and storage info. 'c' is the offset of the
4385 object in section 'sec'. If 'sec' is NULL, it means stack based
4386 allocation. 'first' is true if array '{' must be read (multi
4387 dimension implicit array init handling). 'size_only' is true if
4388 size only evaluation is wanted (only for arrays). */
4389 static void decl_initializer(CType *type, Section *sec, unsigned long c,
4390 int first, int size_only)
4392 int index, array_length, n, no_oblock, nb, parlevel, i;
4393 int size1, align1, expr_type;
4394 Sym *s, *f;
4395 CType *t1;
4397 if (type->t & VT_ARRAY) {
4398 s = type->ref;
4399 n = s->c;
4400 array_length = 0;
4401 t1 = pointed_type(type);
4402 size1 = type_size(t1, &align1);
4404 no_oblock = 1;
4405 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
4406 tok == '{') {
4407 skip('{');
4408 no_oblock = 0;
4411 /* only parse strings here if correct type (otherwise: handle
4412 them as ((w)char *) expressions */
4413 if ((tok == TOK_LSTR &&
4414 #ifdef TCC_TARGET_PE
4415 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
4416 #else
4417 (t1->t & VT_BTYPE) == VT_INT
4418 #endif
4419 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
4420 while (tok == TOK_STR || tok == TOK_LSTR) {
4421 int cstr_len, ch;
4422 CString *cstr;
4424 cstr = tokc.cstr;
4425 /* compute maximum number of chars wanted */
4426 if (tok == TOK_STR)
4427 cstr_len = cstr->size;
4428 else
4429 cstr_len = cstr->size / sizeof(nwchar_t);
4430 cstr_len--;
4431 nb = cstr_len;
4432 if (n >= 0 && nb > (n - array_length))
4433 nb = n - array_length;
4434 if (!size_only) {
4435 if (cstr_len > nb)
4436 warning("initializer-string for array is too long");
4437 /* in order to go faster for common case (char
4438 string in global variable, we handle it
4439 specifically */
4440 if (sec && tok == TOK_STR && size1 == 1) {
4441 memcpy(sec->data + c + array_length, cstr->data, nb);
4442 } else {
4443 for(i=0;i<nb;i++) {
4444 if (tok == TOK_STR)
4445 ch = ((unsigned char *)cstr->data)[i];
4446 else
4447 ch = ((nwchar_t *)cstr->data)[i];
4448 init_putv(t1, sec, c + (array_length + i) * size1,
4449 ch, EXPR_VAL);
4453 array_length += nb;
4454 next();
4456 /* only add trailing zero if enough storage (no
4457 warning in this case since it is standard) */
4458 if (n < 0 || array_length < n) {
4459 if (!size_only) {
4460 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
4462 array_length++;
4464 } else {
4465 index = 0;
4466 while (tok != '}') {
4467 decl_designator(type, sec, c, &index, NULL, size_only);
4468 if (n >= 0 && index >= n)
4469 error("index too large");
4470 /* must put zero in holes (note that doing it that way
4471 ensures that it even works with designators) */
4472 if (!size_only && array_length < index) {
4473 init_putz(t1, sec, c + array_length * size1,
4474 (index - array_length) * size1);
4476 index++;
4477 if (index > array_length)
4478 array_length = index;
4479 /* special test for multi dimensional arrays (may not
4480 be strictly correct if designators are used at the
4481 same time) */
4482 if (index >= n && no_oblock)
4483 break;
4484 if (tok == '}')
4485 break;
4486 skip(',');
4489 if (!no_oblock)
4490 skip('}');
4491 /* put zeros at the end */
4492 if (!size_only && n >= 0 && array_length < n) {
4493 init_putz(t1, sec, c + array_length * size1,
4494 (n - array_length) * size1);
4496 /* patch type size if needed */
4497 if (n < 0)
4498 s->c = array_length;
4499 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
4500 (sec || !first || tok == '{')) {
4501 int par_count;
4503 /* NOTE: the previous test is a specific case for automatic
4504 struct/union init */
4505 /* XXX: union needs only one init */
4507 /* XXX: this test is incorrect for local initializers
4508 beginning with ( without {. It would be much more difficult
4509 to do it correctly (ideally, the expression parser should
4510 be used in all cases) */
4511 par_count = 0;
4512 if (tok == '(') {
4513 AttributeDef ad1;
4514 CType type1;
4515 next();
4516 while (tok == '(') {
4517 par_count++;
4518 next();
4520 if (!parse_btype(&type1, &ad1))
4521 expect("cast");
4522 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4523 #if 0
4524 if (!is_assignable_types(type, &type1))
4525 error("invalid type for cast");
4526 #endif
4527 skip(')');
4529 no_oblock = 1;
4530 if (first || tok == '{') {
4531 skip('{');
4532 no_oblock = 0;
4534 s = type->ref;
4535 f = s->next;
4536 array_length = 0;
4537 index = 0;
4538 n = s->c;
4539 while (tok != '}') {
4540 decl_designator(type, sec, c, NULL, &f, size_only);
4541 index = f->c;
4542 if (!size_only && array_length < index) {
4543 init_putz(type, sec, c + array_length,
4544 index - array_length);
4546 index = index + type_size(&f->type, &align1);
4547 if (index > array_length)
4548 array_length = index;
4550 /* gr: skip fields from same union - ugly. */
4551 while (f->next) {
4552 ///printf("index: %2d %08x -- %2d %08x\n", f->c, f->type.t, f->next->c, f->next->type.t);
4553 /* test for same offset */
4554 if (f->next->c != f->c)
4555 break;
4556 /* if yes, test for bitfield shift */
4557 if ((f->type.t & VT_BITFIELD) && (f->next->type.t & VT_BITFIELD)) {
4558 int bit_pos_1 = (f->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4559 int bit_pos_2 = (f->next->type.t >> VT_STRUCT_SHIFT) & 0x3f;
4560 //printf("bitfield %d %d\n", bit_pos_1, bit_pos_2);
4561 if (bit_pos_1 != bit_pos_2)
4562 break;
4564 f = f->next;
4567 f = f->next;
4568 if (no_oblock && f == NULL)
4569 break;
4570 if (tok == '}')
4571 break;
4572 skip(',');
4574 /* put zeros at the end */
4575 if (!size_only && array_length < n) {
4576 init_putz(type, sec, c + array_length,
4577 n - array_length);
4579 if (!no_oblock)
4580 skip('}');
4581 while (par_count) {
4582 skip(')');
4583 par_count--;
4585 } else if (tok == '{') {
4586 next();
4587 decl_initializer(type, sec, c, first, size_only);
4588 skip('}');
4589 } else if (size_only) {
4590 /* just skip expression */
4591 parlevel = 0;
4592 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
4593 tok != -1) {
4594 if (tok == '(')
4595 parlevel++;
4596 else if (tok == ')')
4597 parlevel--;
4598 next();
4600 } else {
4601 /* currently, we always use constant expression for globals
4602 (may change for scripting case) */
4603 expr_type = EXPR_CONST;
4604 if (!sec)
4605 expr_type = EXPR_ANY;
4606 init_putv(type, sec, c, 0, expr_type);
4610 /* parse an initializer for type 't' if 'has_init' is non zero, and
4611 allocate space in local or global data space ('r' is either
4612 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
4613 variable 'v' of scope 'scope' is declared before initializers are
4614 parsed. If 'v' is zero, then a reference to the new object is put
4615 in the value stack. If 'has_init' is 2, a special parsing is done
4616 to handle string constants. */
4617 static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
4618 int has_init, int v, int scope)
4620 int size, align, addr, data_offset;
4621 int level;
4622 ParseState saved_parse_state = {0};
4623 TokenString init_str;
4624 Section *sec;
4626 size = type_size(type, &align);
4627 /* If unknown size, we must evaluate it before
4628 evaluating initializers because
4629 initializers can generate global data too
4630 (e.g. string pointers or ISOC99 compound
4631 literals). It also simplifies local
4632 initializers handling */
4633 tok_str_new(&init_str);
4634 if (size < 0) {
4635 if (!has_init)
4636 error("unknown type size");
4637 /* get all init string */
4638 if (has_init == 2) {
4639 /* only get strings */
4640 while (tok == TOK_STR || tok == TOK_LSTR) {
4641 tok_str_add_tok(&init_str);
4642 next();
4644 } else {
4645 level = 0;
4646 while (level > 0 || (tok != ',' && tok != ';')) {
4647 if (tok < 0)
4648 error("unexpected end of file in initializer");
4649 tok_str_add_tok(&init_str);
4650 if (tok == '{')
4651 level++;
4652 else if (tok == '}') {
4653 level--;
4654 if (level <= 0) {
4655 next();
4656 break;
4659 next();
4662 tok_str_add(&init_str, -1);
4663 tok_str_add(&init_str, 0);
4665 /* compute size */
4666 save_parse_state(&saved_parse_state);
4668 macro_ptr = init_str.str;
4669 next();
4670 decl_initializer(type, NULL, 0, 1, 1);
4671 /* prepare second initializer parsing */
4672 macro_ptr = init_str.str;
4673 next();
4675 /* if still unknown size, error */
4676 size = type_size(type, &align);
4677 if (size < 0)
4678 error("unknown type size");
4680 /* take into account specified alignment if bigger */
4681 if (ad->aligned) {
4682 if (ad->aligned > align)
4683 align = ad->aligned;
4684 } else if (ad->packed) {
4685 align = 1;
4687 if ((r & VT_VALMASK) == VT_LOCAL) {
4688 sec = NULL;
4689 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY))
4690 loc--;
4691 loc = (loc - size) & -align;
4692 addr = loc;
4693 /* handles bounds */
4694 /* XXX: currently, since we do only one pass, we cannot track
4695 '&' operators, so we add only arrays */
4696 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
4697 unsigned long *bounds_ptr;
4698 /* add padding between regions */
4699 loc--;
4700 /* then add local bound info */
4701 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
4702 bounds_ptr[0] = addr;
4703 bounds_ptr[1] = size;
4705 if (v) {
4706 /* local variable */
4707 sym_push(v, type, r, addr);
4708 } else {
4709 /* push local reference */
4710 vset(type, r, addr);
4712 } else {
4713 Sym *sym;
4715 sym = NULL;
4716 if (v && scope == VT_CONST) {
4717 /* see if the symbol was already defined */
4718 sym = sym_find(v);
4719 if (sym) {
4720 if (!is_compatible_types(&sym->type, type))
4721 error("incompatible types for redefinition of '%s'",
4722 get_tok_str(v, NULL));
4723 if (sym->type.t & VT_EXTERN) {
4724 /* if the variable is extern, it was not allocated */
4725 sym->type.t &= ~VT_EXTERN;
4726 /* set array size if it was ommited in extern
4727 declaration */
4728 if ((sym->type.t & VT_ARRAY) &&
4729 sym->type.ref->c < 0 &&
4730 type->ref->c >= 0)
4731 sym->type.ref->c = type->ref->c;
4732 } else {
4733 /* we accept several definitions of the same
4734 global variable. this is tricky, because we
4735 must play with the SHN_COMMON type of the symbol */
4736 /* XXX: should check if the variable was already
4737 initialized. It is incorrect to initialized it
4738 twice */
4739 /* no init data, we won't add more to the symbol */
4740 if (!has_init)
4741 goto no_alloc;
4746 /* allocate symbol in corresponding section */
4747 sec = ad->section;
4748 if (!sec) {
4749 if (has_init)
4750 sec = data_section;
4751 else if (tcc_state->nocommon)
4752 sec = bss_section;
4754 if (sec) {
4755 data_offset = sec->data_offset;
4756 data_offset = (data_offset + align - 1) & -align;
4757 addr = data_offset;
4758 /* very important to increment global pointer at this time
4759 because initializers themselves can create new initializers */
4760 data_offset += size;
4761 /* add padding if bound check */
4762 if (tcc_state->do_bounds_check)
4763 data_offset++;
4764 sec->data_offset = data_offset;
4765 /* allocate section space to put the data */
4766 if (sec->sh_type != SHT_NOBITS &&
4767 data_offset > sec->data_allocated)
4768 section_realloc(sec, data_offset);
4769 /* align section if needed */
4770 if (align > sec->sh_addralign)
4771 sec->sh_addralign = align;
4772 } else {
4773 addr = 0; /* avoid warning */
4776 if (v) {
4777 if (scope != VT_CONST || !sym) {
4778 sym = sym_push(v, type, r | VT_SYM, 0);
4780 /* update symbol definition */
4781 if (sec) {
4782 put_extern_sym(sym, sec, addr, size);
4783 } else {
4784 ElfW(Sym) *esym;
4785 /* put a common area */
4786 put_extern_sym(sym, NULL, align, size);
4787 /* XXX: find a nicer way */
4788 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
4789 esym->st_shndx = SHN_COMMON;
4791 } else {
4792 CValue cval;
4794 /* push global reference */
4795 sym = get_sym_ref(type, sec, addr, size);
4796 cval.ul = 0;
4797 vsetc(type, VT_CONST | VT_SYM, &cval);
4798 vtop->sym = sym;
4801 /* handles bounds now because the symbol must be defined
4802 before for the relocation */
4803 if (tcc_state->do_bounds_check) {
4804 unsigned long *bounds_ptr;
4806 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_PTR);
4807 /* then add global bound info */
4808 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
4809 bounds_ptr[0] = 0; /* relocated */
4810 bounds_ptr[1] = size;
4813 if (has_init) {
4814 decl_initializer(type, sec, addr, 1, 0);
4815 /* restore parse state if needed */
4816 if (init_str.str) {
4817 tok_str_free(init_str.str);
4818 restore_parse_state(&saved_parse_state);
4821 no_alloc: ;
4824 void put_func_debug(Sym *sym)
4826 char buf[512];
4828 /* stabs info */
4829 /* XXX: we put here a dummy type */
4830 snprintf(buf, sizeof(buf), "%s:%c1",
4831 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
4832 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
4833 cur_text_section, sym->c);
4834 /* //gr gdb wants a line at the function */
4835 put_stabn(N_SLINE, 0, file->line_num, 0);
4836 last_ind = 0;
4837 last_line_num = 0;
4840 /* parse an old style function declaration list */
4841 /* XXX: check multiple parameter */
4842 static void func_decl_list(Sym *func_sym)
4844 AttributeDef ad;
4845 int v;
4846 Sym *s;
4847 CType btype, type;
4849 /* parse each declaration */
4850 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
4851 if (!parse_btype(&btype, &ad))
4852 expect("declaration list");
4853 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4854 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4855 tok == ';') {
4856 /* we accept no variable after */
4857 } else {
4858 for(;;) {
4859 type = btype;
4860 type_decl(&type, &ad, &v, TYPE_DIRECT);
4861 /* find parameter in function parameter list */
4862 s = func_sym->next;
4863 while (s != NULL) {
4864 if ((s->v & ~SYM_FIELD) == v)
4865 goto found;
4866 s = s->next;
4868 error("declaration for parameter '%s' but no such parameter",
4869 get_tok_str(v, NULL));
4870 found:
4871 /* check that no storage specifier except 'register' was given */
4872 if (type.t & VT_STORAGE)
4873 error("storage class specified for '%s'", get_tok_str(v, NULL));
4874 convert_parameter_type(&type);
4875 /* we can add the type (NOTE: it could be local to the function) */
4876 s->type = type;
4877 /* accept other parameters */
4878 if (tok == ',')
4879 next();
4880 else
4881 break;
4884 skip(';');
4888 /* parse a function defined by symbol 'sym' and generate its code in
4889 'cur_text_section' */
4890 static void gen_function(Sym *sym)
4892 int saved_nocode_wanted = nocode_wanted;
4893 nocode_wanted = 0;
4894 ind = cur_text_section->data_offset;
4895 /* NOTE: we patch the symbol size later */
4896 put_extern_sym(sym, cur_text_section, ind, 0);
4897 funcname = get_tok_str(sym->v, NULL);
4898 func_ind = ind;
4899 /* put debug symbol */
4900 if (tcc_state->do_debug)
4901 put_func_debug(sym);
4902 /* push a dummy symbol to enable local sym storage */
4903 sym_push2(&local_stack, SYM_FIELD, 0, 0);
4904 gfunc_prolog(&sym->type);
4905 rsym = 0;
4906 block(NULL, NULL, NULL, NULL, 0, 0);
4907 gsym(rsym);
4908 gfunc_epilog();
4909 cur_text_section->data_offset = ind;
4910 label_pop(&global_label_stack, NULL);
4911 sym_pop(&local_stack, NULL); /* reset local stack */
4912 /* end of function */
4913 /* patch symbol size */
4914 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
4915 ind - func_ind;
4916 if (tcc_state->do_debug) {
4917 put_stabn(N_FUN, 0, 0, ind - func_ind);
4919 /* It's better to crash than to generate wrong code */
4920 cur_text_section = NULL;
4921 funcname = ""; /* for safety */
4922 func_vt.t = VT_VOID; /* for safety */
4923 ind = 0; /* for safety */
4924 nocode_wanted = saved_nocode_wanted;
4927 void gen_inline_functions(void)
4929 Sym *sym;
4930 int *str, inline_generated, i;
4931 struct InlineFunc *fn;
4933 /* iterate while inline function are referenced */
4934 for(;;) {
4935 inline_generated = 0;
4936 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4937 fn = tcc_state->inline_fns[i];
4938 sym = fn->sym;
4939 if (sym && sym->c) {
4940 /* the function was used: generate its code and
4941 convert it to a normal function */
4942 str = fn->token_str;
4943 fn->sym = NULL;
4944 if (file)
4945 strcpy(file->filename, fn->filename);
4946 sym->r = VT_SYM | VT_CONST;
4947 sym->type.t &= ~VT_INLINE;
4949 macro_ptr = str;
4950 next();
4951 cur_text_section = text_section;
4952 gen_function(sym);
4953 macro_ptr = NULL; /* fail safe */
4955 inline_generated = 1;
4958 if (!inline_generated)
4959 break;
4961 for (i = 0; i < tcc_state->nb_inline_fns; ++i) {
4962 fn = tcc_state->inline_fns[i];
4963 str = fn->token_str;
4964 tok_str_free(str);
4966 dynarray_reset(&tcc_state->inline_fns, &tcc_state->nb_inline_fns);
4969 /* 'l' is VT_LOCAL or VT_CONST to define default storage type */
4970 void decl(int l)
4972 int v, has_init, r;
4973 CType type, btype;
4974 Sym *sym;
4975 AttributeDef ad;
4977 while (1) {
4978 if (!parse_btype(&btype, &ad)) {
4979 /* skip redundant ';' */
4980 /* XXX: find more elegant solution */
4981 if (tok == ';') {
4982 next();
4983 continue;
4985 if (l == VT_CONST &&
4986 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
4987 /* global asm block */
4988 asm_global_instr();
4989 continue;
4991 /* special test for old K&R protos without explicit int
4992 type. Only accepted when defining global data */
4993 if (l == VT_LOCAL || tok < TOK_DEFINE)
4994 break;
4995 btype.t = VT_INT;
4997 if (((btype.t & VT_BTYPE) == VT_ENUM ||
4998 (btype.t & VT_BTYPE) == VT_STRUCT) &&
4999 tok == ';') {
5000 /* we accept no variable after */
5001 next();
5002 continue;
5004 while (1) { /* iterate thru each declaration */
5005 type = btype;
5006 type_decl(&type, &ad, &v, TYPE_DIRECT);
5007 #if 0
5009 char buf[500];
5010 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
5011 printf("type = '%s'\n", buf);
5013 #endif
5014 if ((type.t & VT_BTYPE) == VT_FUNC) {
5015 /* if old style function prototype, we accept a
5016 declaration list */
5017 sym = type.ref;
5018 if (sym->c == FUNC_OLD)
5019 func_decl_list(sym);
5022 if (tok == '{') {
5023 if (l == VT_LOCAL)
5024 error("cannot use local functions");
5025 if ((type.t & VT_BTYPE) != VT_FUNC)
5026 expect("function definition");
5028 /* reject abstract declarators in function definition */
5029 sym = type.ref;
5030 while ((sym = sym->next) != NULL)
5031 if (!(sym->v & ~SYM_FIELD))
5032 expect("identifier");
5034 /* XXX: cannot do better now: convert extern line to static inline */
5035 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
5036 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5038 sym = sym_find(v);
5039 if (sym) {
5040 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
5041 goto func_error1;
5043 r = sym->type.ref->r;
5044 /* use func_call from prototype if not defined */
5045 if (FUNC_CALL(r) != FUNC_CDECL
5046 && FUNC_CALL(type.ref->r) == FUNC_CDECL)
5047 FUNC_CALL(type.ref->r) = FUNC_CALL(r);
5049 /* use export from prototype */
5050 if (FUNC_EXPORT(r))
5051 FUNC_EXPORT(type.ref->r) = 1;
5053 /* use static from prototype */
5054 if (sym->type.t & VT_STATIC)
5055 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
5057 if (!is_compatible_types(&sym->type, &type)) {
5058 func_error1:
5059 error("incompatible types for redefinition of '%s'",
5060 get_tok_str(v, NULL));
5062 /* if symbol is already defined, then put complete type */
5063 sym->type = type;
5064 } else {
5065 /* put function symbol */
5066 sym = global_identifier_push(v, type.t, 0);
5067 sym->type.ref = type.ref;
5070 /* static inline functions are just recorded as a kind
5071 of macro. Their code will be emitted at the end of
5072 the compilation unit only if they are used */
5073 if ((type.t & (VT_INLINE | VT_STATIC)) ==
5074 (VT_INLINE | VT_STATIC)) {
5075 TokenString func_str;
5076 int block_level;
5077 struct InlineFunc *fn;
5078 const char *filename;
5080 tok_str_new(&func_str);
5082 block_level = 0;
5083 for(;;) {
5084 int t;
5085 if (tok == TOK_EOF)
5086 error("unexpected end of file");
5087 tok_str_add_tok(&func_str);
5088 t = tok;
5089 next();
5090 if (t == '{') {
5091 block_level++;
5092 } else if (t == '}') {
5093 block_level--;
5094 if (block_level == 0)
5095 break;
5098 tok_str_add(&func_str, -1);
5099 tok_str_add(&func_str, 0);
5100 filename = file ? file->filename : "";
5101 fn = tcc_malloc(sizeof *fn + strlen(filename));
5102 strcpy(fn->filename, filename);
5103 fn->sym = sym;
5104 fn->token_str = func_str.str;
5105 dynarray_add((void ***)&tcc_state->inline_fns, &tcc_state->nb_inline_fns, fn);
5107 } else {
5108 /* compute text section */
5109 cur_text_section = ad.section;
5110 if (!cur_text_section)
5111 cur_text_section = text_section;
5112 sym->r = VT_SYM | VT_CONST;
5113 gen_function(sym);
5115 break;
5116 } else {
5117 if (btype.t & VT_TYPEDEF) {
5118 /* save typedefed type */
5119 /* XXX: test storage specifiers ? */
5120 sym = sym_push(v, &type, 0, 0);
5121 sym->type.t |= VT_TYPEDEF;
5122 } else if ((type.t & VT_BTYPE) == VT_FUNC) {
5123 /* external function definition */
5124 /* specific case for func_call attribute */
5125 if (ad.func_attr)
5126 type.ref->r = ad.func_attr;
5127 external_sym(v, &type, 0);
5128 } else {
5129 /* not lvalue if array */
5130 r = 0;
5131 if (!(type.t & VT_ARRAY))
5132 r |= lvalue_type(type.t);
5133 has_init = (tok == '=');
5134 if ((btype.t & VT_EXTERN) ||
5135 ((type.t & VT_ARRAY) && (type.t & VT_STATIC) &&
5136 !has_init && l == VT_CONST && type.ref->c < 0)) {
5137 /* external variable */
5138 /* NOTE: as GCC, uninitialized global static
5139 arrays of null size are considered as
5140 extern */
5141 #ifdef TCC_TARGET_PE
5142 if (FUNC_IMPORT(ad.func_attr))
5143 type.t |= VT_IMPORT;
5144 #endif
5145 external_sym(v, &type, r);
5146 } else {
5147 type.t |= (btype.t & VT_STATIC); /* Retain "static". */
5148 if (type.t & VT_STATIC)
5149 r |= VT_CONST;
5150 else
5151 r |= l;
5152 if (has_init)
5153 next();
5154 decl_initializer_alloc(&type, &ad, r,
5155 has_init, v, l);
5158 if (tok != ',') {
5159 skip(';');
5160 break;
5162 next();