History

move parser/generator to tccgen.c

grischka committed 17 years ago

Commit 0d1ed741 parent 805990b2 files · +5103 −5102Browse files

Unified · Split

2 files
  1. tcc.c +1 −5102
  2. tccgen.c +5102 −0
tcc.c+1−5102
@@ -1107,5108 +1107,7 @@ void tcc_close(BufferedFile *bf) · 1106 unmodified lines
1107}1107}
1108 1108
1109#include "tccpp.c"1109#include "tccpp.c"
1110
1111void swap(int *p, int *q)
1112{
1113 int t;
1114 t = *p;
1115 *p = *q;
1116 *q = t;
1117}
1118
1119void vsetc(CType *type, int r, CValue *vc)
1120{
1121 int v;
1122
1123 if (vtop >= vstack + (VSTACK_SIZE - 1))
1124 error("memory full");
1125 /* cannot let cpu flags if other instruction are generated. Also
1126 avoid leaving VT_JMP anywhere except on the top of the stack
1127 because it would complicate the code generator. */
1128 if (vtop >= vstack) {
1129 v = vtop->r & VT_VALMASK;
1130 if (v == VT_CMP || (v & ~1) == VT_JMP)
1131 gv(RC_INT);
1132 }
1133 vtop++;
1134 vtop->type = *type;
1135 vtop->r = r;
1136 vtop->r2 = VT_CONST;
1137 vtop->c = *vc;
1138}
1139
1140/* push integer constant */
1141void vpushi(int v)
1142{
1143 CValue cval;
1144 cval.i = v;
1145 vsetc(&int_type, VT_CONST, &cval);
1146}
1147
1148/* push long long constant */
1149void vpushll(long long v)
1150{
1151 CValue cval;
1152 CType ctype;
1153 ctype.t = VT_LLONG;
1154 cval.ull = v;
1155 vsetc(&ctype, VT_CONST, &cval);
1156}
1157
1158/* Return a static symbol pointing to a section */
1159static Sym *get_sym_ref(CType *type, Section *sec,
1160 unsigned long offset, unsigned long size)
1161{
1162 int v;
1163 Sym *sym;
1164
1165 v = anon_sym++;
1166 sym = global_identifier_push(v, type->t | VT_STATIC, 0);
1167 sym->type.ref = type->ref;
1168 sym->r = VT_CONST | VT_SYM;
1169 put_extern_sym(sym, sec, offset, size);
1170 return sym;
1171}
1172
1173/* push a reference to a section offset by adding a dummy symbol */
1174static void vpush_ref(CType *type, Section *sec, unsigned long offset, unsigned long size)
1175{
1176 CValue cval;
1177
1178 cval.ul = 0;
1179 vsetc(type, VT_CONST | VT_SYM, &cval);
1180 vtop->sym = get_sym_ref(type, sec, offset, size);
1181}
1182
1183/* define a new external reference to a symbol 'v' of type 'u' */
1184static Sym *external_global_sym(int v, CType *type, int r)
1185{
1186 Sym *s;
1187
1188 s = sym_find(v);
1189 if (!s) {
1190 /* push forward reference */
1191 s = global_identifier_push(v, type->t | VT_EXTERN, 0);
1192 s->type.ref = type->ref;
1193 s->r = r | VT_CONST | VT_SYM;
1194 }
1195 return s;
1196}
1197
1198/* define a new external reference to a symbol 'v' of type 'u' */
1199static Sym *external_sym(int v, CType *type, int r)
1200{
1201 Sym *s;
1202
1203 s = sym_find(v);
1204 if (!s) {
1205 /* push forward reference */
1206 s = sym_push(v, type, r | VT_CONST | VT_SYM, 0);
1207 s->type.t |= VT_EXTERN;
1208 } else {
1209 if (!is_compatible_types(&s->type, type))
1210 error("incompatible types for redefinition of '%s'",
1211 get_tok_str(v, NULL));
1212 }
1213 return s;
1214}
1215
1216/* push a reference to global symbol v */
1217static void vpush_global_sym(CType *type, int v)
1218{
1219 Sym *sym;
1220 CValue cval;
1221
1222 sym = external_global_sym(v, type, 0);
1223 cval.ul = 0;
1224 vsetc(type, VT_CONST | VT_SYM, &cval);
1225 vtop->sym = sym;
1226}
1227
1228void vset(CType *type, int r, int v)
1229{
1230 CValue cval;
1231
1232 cval.i = v;
1233 vsetc(type, r, &cval);
1234}
1235
1236void vseti(int r, int v)
1237{
1238 CType type;
1239 type.t = VT_INT;
1240 vset(&type, r, v);
1241}
1242
1243void vswap(void)
1244{
1245 SValue tmp;
1246
1247 tmp = vtop[0];
1248 vtop[0] = vtop[-1];
1249 vtop[-1] = tmp;
1250}
1251
1252void vpushv(SValue *v)
1253{
1254 if (vtop >= vstack + (VSTACK_SIZE - 1))
1255 error("memory full");
1256 vtop++;
1257 *vtop = *v;
1258}
1259
1260void vdup(void)
1261{
1262 vpushv(vtop);
1263}
1264
1265/* save r to the memory stack, and mark it as being free */
1266void save_reg(int r)
1267{
1268 int l, saved, size, align;
1269 SValue *p, sv;
1270 CType *type;
1271
1272 /* modify all stack values */
1273 saved = 0;
1274 l = 0;
1275 for(p=vstack;p<=vtop;p++) {
1276 if ((p->r & VT_VALMASK) == r ||
1277 ((p->type.t & VT_BTYPE) == VT_LLONG && (p->r2 & VT_VALMASK) == r)) {
1278 /* must save value on stack if not already done */
1279 if (!saved) {
1280 /* NOTE: must reload 'r' because r might be equal to r2 */
1281 r = p->r & VT_VALMASK;
1282 /* store register in the stack */
1283 type = &p->type;
1284 if ((p->r & VT_LVAL) ||
1285 (!is_float(type->t) && (type->t & VT_BTYPE) != VT_LLONG))
1286#ifdef TCC_TARGET_X86_64
1287 type = &char_pointer_type;
1288#else
1289 type = &int_type;
1290#endif
1291 size = type_size(type, &align);
1292 loc = (loc - size) & -align;
1293 sv.type.t = type->t;
1294 sv.r = VT_LOCAL | VT_LVAL;
1295 sv.c.ul = loc;
1296 store(r, &sv);
1297#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1298 /* x86 specific: need to pop fp register ST0 if saved */
1299 if (r == TREG_ST0) {
1300 o(0xd9dd); /* fstp %st(1) */
1301 }
1302#endif
1303#ifndef TCC_TARGET_X86_64
1304 /* special long long case */
1305 if ((type->t & VT_BTYPE) == VT_LLONG) {
1306 sv.c.ul += 4;
1307 store(p->r2, &sv);
1308 }
1309#endif
1310 l = loc;
1311 saved = 1;
1312 }
1313 /* mark that stack entry as being saved on the stack */
1314 if (p->r & VT_LVAL) {
1315 /* also clear the bounded flag because the
1316 relocation address of the function was stored in
1317 p->c.ul */
1318 p->r = (p->r & ~(VT_VALMASK | VT_BOUNDED)) | VT_LLOCAL;
1319 } else {
1320 p->r = lvalue_type(p->type.t) | VT_LOCAL;
1321 }
1322 p->r2 = VT_CONST;
1323 p->c.ul = l;
1324 }
1325 }
1326}
1327
1328/* find a register of class 'rc2' with at most one reference on stack.
1329 * If none, call get_reg(rc) */
1330int get_reg_ex(int rc, int rc2)
1331{
1332 int r;
1333 SValue *p;
1334
1335 for(r=0;r<NB_REGS;r++) {
1336 if (reg_classes[r] & rc2) {
1337 int n;
1338 n=0;
1339 for(p = vstack; p <= vtop; p++) {
1340 if ((p->r & VT_VALMASK) == r ||
1341 (p->r2 & VT_VALMASK) == r)
1342 n++;
1343 }
1344 if (n <= 1)
1345 return r;
1346 }
1347 }
1348 return get_reg(rc);
1349}
1350
1351/* find a free register of class 'rc'. If none, save one register */
1352int get_reg(int rc)
1353{
1354 int r;
1355 SValue *p;
1356
1357 /* find a free register */
1358 for(r=0;r<NB_REGS;r++) {
1359 if (reg_classes[r] & rc) {
1360 for(p=vstack;p<=vtop;p++) {
1361 if ((p->r & VT_VALMASK) == r ||
1362 (p->r2 & VT_VALMASK) == r)
1363 goto notfound;
1364 }
1365 return r;
1366 }
1367 notfound: ;
1368 }
1369
1370 /* no register left : free the first one on the stack (VERY
1371 IMPORTANT to start from the bottom to ensure that we don't
1372 spill registers used in gen_opi()) */
1373 for(p=vstack;p<=vtop;p++) {
1374 r = p->r & VT_VALMASK;
1375 if (r < VT_CONST && (reg_classes[r] & rc))
1376 goto save_found;
1377 /* also look at second register (if long long) */
1378 r = p->r2 & VT_VALMASK;
1379 if (r < VT_CONST && (reg_classes[r] & rc)) {
1380 save_found:
1381 save_reg(r);
1382 return r;
1383 }
1384 }
1385 /* Should never comes here */
1386 return -1;
1387}
1388
1389/* save registers up to (vtop - n) stack entry */
1390void save_regs(int n)
1391{
1392 int r;
1393 SValue *p, *p1;
1394 p1 = vtop - n;
1395 for(p = vstack;p <= p1; p++) {
1396 r = p->r & VT_VALMASK;
1397 if (r < VT_CONST) {
1398 save_reg(r);
1399 }
1400 }
1401}
1402
1403/* move register 's' to 'r', and flush previous value of r to memory
1404 if needed */
1405void move_reg(int r, int s)
1406{
1407 SValue sv;
1408
1409 if (r != s) {
1410 save_reg(r);
1411 sv.type.t = VT_INT;
1412 sv.r = s;
1413 sv.c.ul = 0;
1414 load(r, &sv);
1415 }
1416}
1417
1418/* get address of vtop (vtop MUST BE an lvalue) */
1419void gaddrof(void)
1420{
1421 vtop->r &= ~VT_LVAL;
1422 /* tricky: if saved lvalue, then we can go back to lvalue */
1423 if ((vtop->r & VT_VALMASK) == VT_LLOCAL)
1424 vtop->r = (vtop->r & ~(VT_VALMASK | VT_LVAL_TYPE)) | VT_LOCAL | VT_LVAL;
1425}
1426
1427#ifdef CONFIG_TCC_BCHECK
1428/* generate lvalue bound code */
1429void gbound(void)
1430{
1431 int lval_type;
1432 CType type1;
1433
1434 vtop->r &= ~VT_MUSTBOUND;
1435 /* if lvalue, then use checking code before dereferencing */
1436 if (vtop->r & VT_LVAL) {
1437 /* if not VT_BOUNDED value, then make one */
1438 if (!(vtop->r & VT_BOUNDED)) {
1439 lval_type = vtop->r & (VT_LVAL_TYPE | VT_LVAL);
1440 /* must save type because we must set it to int to get pointer */
1441 type1 = vtop->type;
1442 vtop->type.t = VT_INT;
1443 gaddrof();
1444 vpushi(0);
1445 gen_bounded_ptr_add();
1446 vtop->r |= lval_type;
1447 vtop->type = type1;
1448 }
1449 /* then check for dereferencing */
1450 gen_bounded_ptr_deref();
1451 }
1452}
1453#endif
1454
1455/* store vtop a register belonging to class 'rc'. lvalues are
1456 converted to values. Cannot be used if cannot be converted to
1457 register value (such as structures). */
1458int gv(int rc)
1459{
1460 int r, rc2, bit_pos, bit_size, size, align, i;
1461
1462 /* NOTE: get_reg can modify vstack[] */
1463 if (vtop->type.t & VT_BITFIELD) {
1464 CType type;
1465 int bits = 32;
1466 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
1467 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
1468 /* remove bit field info to avoid loops */
1469 vtop->type.t &= ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
1470 /* cast to int to propagate signedness in following ops */
1471 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1472 type.t = VT_LLONG;
1473 bits = 64;
1474 } else
1475 type.t = VT_INT;
1476 if((vtop->type.t & VT_UNSIGNED) ||
1477 (vtop->type.t & VT_BTYPE) == VT_BOOL)
1478 type.t |= VT_UNSIGNED;
1479 gen_cast(&type);
1480 /* generate shifts */
1481 vpushi(bits - (bit_pos + bit_size));
1482 gen_op(TOK_SHL);
1483 vpushi(bits - bit_size);
1484 /* NOTE: transformed to SHR if unsigned */
1485 gen_op(TOK_SAR);
1486 r = gv(rc);
1487 } else {
1488 if (is_float(vtop->type.t) &&
1489 (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1490 Sym *sym;
1491 int *ptr;
1492 unsigned long offset;
1493#if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1494 CValue check;
1495#endif
1496
1497 /* XXX: unify with initializers handling ? */
1498 /* CPUs usually cannot use float constants, so we store them
1499 generically in data segment */
1500 size = type_size(&vtop->type, &align);
1501 offset = (data_section->data_offset + align - 1) & -align;
1502 data_section->data_offset = offset;
1503 /* XXX: not portable yet */
1504#if defined(__i386__) || defined(__x86_64__)
1505 /* Zero pad x87 tenbyte long doubles */
1506 if (size == LDOUBLE_SIZE)
1507 vtop->c.tab[2] &= 0xffff;
1508#endif
1509 ptr = section_ptr_add(data_section, size);
1510 size = size >> 2;
1511#if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
1512 check.d = 1;
1513 if(check.tab[0])
1514 for(i=0;i<size;i++)
1515 ptr[i] = vtop->c.tab[size-1-i];
1516 else
1517#endif
1518 for(i=0;i<size;i++)
1519 ptr[i] = vtop->c.tab[i];
1520 sym = get_sym_ref(&vtop->type, data_section, offset, size << 2);
1521 vtop->r |= VT_LVAL | VT_SYM;
1522 vtop->sym = sym;
1523 vtop->c.ul = 0;
1524 }
1525#ifdef CONFIG_TCC_BCHECK
1526 if (vtop->r & VT_MUSTBOUND)
1527 gbound();
1528#endif
1529
1530 r = vtop->r & VT_VALMASK;
1531 rc2 = RC_INT;
1532 if (rc == RC_IRET)
1533 rc2 = RC_LRET;
1534 /* need to reload if:
1535 - constant
1536 - lvalue (need to dereference pointer)
1537 - already a register, but not in the right class */
1538 if (r >= VT_CONST ||
1539 (vtop->r & VT_LVAL) ||
1540 !(reg_classes[r] & rc) ||
1541 ((vtop->type.t & VT_BTYPE) == VT_LLONG &&
1542 !(reg_classes[vtop->r2] & rc2))) {
1543 r = get_reg(rc);
1544#ifndef TCC_TARGET_X86_64
1545 if ((vtop->type.t & VT_BTYPE) == VT_LLONG) {
1546 int r2;
1547 unsigned long long ll;
1548 /* two register type load : expand to two words
1549 temporarily */
1550 if ((vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST) {
1551 /* load constant */
1552 ll = vtop->c.ull;
1553 vtop->c.ui = ll; /* first word */
1554 load(r, vtop);
1555 vtop->r = r; /* save register value */
1556 vpushi(ll >> 32); /* second word */
1557 } else if (r >= VT_CONST || /* XXX: test to VT_CONST incorrect ? */
1558 (vtop->r & VT_LVAL)) {
1559 /* We do not want to modifier the long long
1560 pointer here, so the safest (and less
1561 efficient) is to save all the other registers
1562 in the stack. XXX: totally inefficient. */
1563 save_regs(1);
1564 /* load from memory */
1565 load(r, vtop);
1566 vdup();
1567 vtop[-1].r = r; /* save register value */
1568 /* increment pointer to get second word */
1569 vtop->type.t = VT_INT;
1570 gaddrof();
1571 vpushi(4);
1572 gen_op('+');
1573 vtop->r |= VT_LVAL;
1574 } else {
1575 /* move registers */
1576 load(r, vtop);
1577 vdup();
1578 vtop[-1].r = r; /* save register value */
1579 vtop->r = vtop[-1].r2;
1580 }
1581 /* allocate second register */
1582 r2 = get_reg(rc2);
1583 load(r2, vtop);
1584 vpop();
1585 /* write second register */
1586 vtop->r2 = r2;
1587 } else
1588#endif
1589 if ((vtop->r & VT_LVAL) && !is_float(vtop->type.t)) {
1590 int t1, t;
1591 /* lvalue of scalar type : need to use lvalue type
1592 because of possible cast */
1593 t = vtop->type.t;
1594 t1 = t;
1595 /* compute memory access type */
1596 if (vtop->r & VT_LVAL_BYTE)
1597 t = VT_BYTE;
1598 else if (vtop->r & VT_LVAL_SHORT)
1599 t = VT_SHORT;
1600 if (vtop->r & VT_LVAL_UNSIGNED)
1601 t |= VT_UNSIGNED;
1602 vtop->type.t = t;
1603 load(r, vtop);
1604 /* restore wanted type */
1605 vtop->type.t = t1;
1606 } else {
1607 /* one register type load */
1608 load(r, vtop);
1609 }
1610 }
1611 vtop->r = r;
1612#ifdef TCC_TARGET_C67
1613 /* uses register pairs for doubles */
1614 if ((vtop->type.t & VT_BTYPE) == VT_DOUBLE)
1615 vtop->r2 = r+1;
1616#endif
1617 }
1618 return r;
1619}
1620
1621/* generate vtop[-1] and vtop[0] in resp. classes rc1 and rc2 */
1622void gv2(int rc1, int rc2)
1623{
1624 int v;
1625
1626 /* generate more generic register first. But VT_JMP or VT_CMP
1627 values must be generated first in all cases to avoid possible
1628 reload errors */
1629 v = vtop[0].r & VT_VALMASK;
1630 if (v != VT_CMP && (v & ~1) != VT_JMP && rc1 <= rc2) {
1631 vswap();
1632 gv(rc1);
1633 vswap();
1634 gv(rc2);
1635 /* test if reload is needed for first register */
1636 if ((vtop[-1].r & VT_VALMASK) >= VT_CONST) {
1637 vswap();
1638 gv(rc1);
1639 vswap();
1640 }
1641 } else {
1642 gv(rc2);
1643 vswap();
1644 gv(rc1);
1645 vswap();
1646 /* test if reload is needed for first register */
1647 if ((vtop[0].r & VT_VALMASK) >= VT_CONST) {
1648 gv(rc2);
1649 }
1650 }
1651}
1652
1653/* wrapper around RC_FRET to return a register by type */
1654int rc_fret(int t)
1655{
1656#ifdef TCC_TARGET_X86_64
1657 if (t == VT_LDOUBLE) {
1658 return RC_ST0;
1659 }
1660#endif
1661 return RC_FRET;
1662}
1663
1664/* wrapper around REG_FRET to return a register by type */
1665int reg_fret(int t)
1666{
1667#ifdef TCC_TARGET_X86_64
1668 if (t == VT_LDOUBLE) {
1669 return TREG_ST0;
1670 }
1671#endif
1672 return REG_FRET;
1673}
1674
1675/* expand long long on stack in two int registers */
1676void lexpand(void)
1677{
1678 int u;
1679
1680 u = vtop->type.t & VT_UNSIGNED;
1681 gv(RC_INT);
1682 vdup();
1683 vtop[0].r = vtop[-1].r2;
1684 vtop[0].r2 = VT_CONST;
1685 vtop[-1].r2 = VT_CONST;
1686 vtop[0].type.t = VT_INT | u;
1687 vtop[-1].type.t = VT_INT | u;
1688}
1689
1690#ifdef TCC_TARGET_ARM
1691/* expand long long on stack */
1692void lexpand_nr(void)
1693{
1694 int u,v;
1695
1696 u = vtop->type.t & VT_UNSIGNED;
1697 vdup();
1698 vtop->r2 = VT_CONST;
1699 vtop->type.t = VT_INT | u;
1700 v=vtop[-1].r & (VT_VALMASK | VT_LVAL);
1701 if (v == VT_CONST) {
1702 vtop[-1].c.ui = vtop->c.ull;
1703 vtop->c.ui = vtop->c.ull >> 32;
1704 vtop->r = VT_CONST;
1705 } else if (v == (VT_LVAL|VT_CONST) || v == (VT_LVAL|VT_LOCAL)) {
1706 vtop->c.ui += 4;
1707 vtop->r = vtop[-1].r;
1708 } else if (v > VT_CONST) {
1709 vtop--;
1710 lexpand();
1711 } else
1712 vtop->r = vtop[-1].r2;
1713 vtop[-1].r2 = VT_CONST;
1714 vtop[-1].type.t = VT_INT | u;
1715}
1716#endif
1717
1718/* build a long long from two ints */
1719void lbuild(int t)
1720{
1721 gv2(RC_INT, RC_INT);
1722 vtop[-1].r2 = vtop[0].r;
1723 vtop[-1].type.t = t;
1724 vpop();
1725}
1726
1727/* rotate n first stack elements to the bottom
1728 I1 ... In -> I2 ... In I1 [top is right]
1729*/
1730void vrotb(int n)
1731{
1732 int i;
1733 SValue tmp;
1734
1735 tmp = vtop[-n + 1];
1736 for(i=-n+1;i!=0;i++)
1737 vtop[i] = vtop[i+1];
1738 vtop[0] = tmp;
1739}
1740
1741/* rotate n first stack elements to the top
1742 I1 ... In -> In I1 ... I(n-1) [top is right]
1743 */
1744void vrott(int n)
1745{
1746 int i;
1747 SValue tmp;
1748
1749 tmp = vtop[0];
1750 for(i = 0;i < n - 1; i++)
1751 vtop[-i] = vtop[-i - 1];
1752 vtop[-n + 1] = tmp;
1753}
1754
1755#ifdef TCC_TARGET_ARM
1756/* like vrott but in other direction
1757 In ... I1 -> I(n-1) ... I1 In [top is right]
1758 */
1759void vnrott(int n)
1760{
1761 int i;
1762 SValue tmp;
1763
1764 tmp = vtop[-n + 1];
1765 for(i = n - 1; i > 0; i--)
1766 vtop[-i] = vtop[-i + 1];
1767 vtop[0] = tmp;
1768}
1769#endif
1770
1771/* pop stack value */
1772void vpop(void)
1773{
1774 int v;
1775 v = vtop->r & VT_VALMASK;
1776#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
1777 /* for x86, we need to pop the FP stack */
1778 if (v == TREG_ST0 && !nocode_wanted) {
1779 o(0xd9dd); /* fstp %st(1) */
1780 } else
1781#endif
1782 if (v == VT_JMP || v == VT_JMPI) {
1783 /* need to put correct jump if && or || without test */
1784 gsym(vtop->c.ul);
1785 }
1786 vtop--;
1787}
1788
1789/* convert stack entry to register and duplicate its value in another
1790 register */
1791void gv_dup(void)
1792{
1793 int rc, t, r, r1;
1794 SValue sv;
1795
1796 t = vtop->type.t;
1797 if ((t & VT_BTYPE) == VT_LLONG) {
1798 lexpand();
1799 gv_dup();
1800 vswap();
1801 vrotb(3);
1802 gv_dup();
1803 vrotb(4);
1804 /* stack: H L L1 H1 */
1805 lbuild(t);
1806 vrotb(3);
1807 vrotb(3);
1808 vswap();
1809 lbuild(t);
1810 vswap();
1811 } else {
1812 /* duplicate value */
1813 rc = RC_INT;
1814 sv.type.t = VT_INT;
1815 if (is_float(t)) {
1816 rc = RC_FLOAT;
1817#ifdef TCC_TARGET_X86_64
1818 if ((t & VT_BTYPE) == VT_LDOUBLE) {
1819 rc = RC_ST0;
1820 }
1821#endif
1822 sv.type.t = t;
1823 }
1824 r = gv(rc);
1825 r1 = get_reg(rc);
1826 sv.r = r;
1827 sv.c.ul = 0;
1828 load(r1, &sv); /* move r to r1 */
1829 vdup();
1830 /* duplicates value */
1831 vtop->r = r1;
1832 }
1833}
1834
1835#ifndef TCC_TARGET_X86_64
1836/* generate CPU independent (unsigned) long long operations */
1837void gen_opl(int op)
1838{
1839 int t, a, b, op1, c, i;
1840 int func;
1841 unsigned short reg_iret = REG_IRET;
1842 unsigned short reg_lret = REG_LRET;
1843 SValue tmp;
1844
1845 switch(op) {
1846 case '/':
1847 case TOK_PDIV:
1848 func = TOK___divdi3;
1849 goto gen_func;
1850 case TOK_UDIV:
1851 func = TOK___udivdi3;
1852 goto gen_func;
1853 case '%':
1854 func = TOK___moddi3;
1855 goto gen_mod_func;
1856 case TOK_UMOD:
1857 func = TOK___umoddi3;
1858 gen_mod_func:
1859#ifdef TCC_ARM_EABI
1860 reg_iret = TREG_R2;
1861 reg_lret = TREG_R3;
1862#endif
1863 gen_func:
1864 /* call generic long long function */
1865 vpush_global_sym(&func_old_type, func);
1866 vrott(3);
1867 gfunc_call(2);
1868 vpushi(0);
1869 vtop->r = reg_iret;
1870 vtop->r2 = reg_lret;
1871 break;
1872 case '^':
1873 case '&':
1874 case '|':
1875 case '*':
1876 case '+':
1877 case '-':
1878 t = vtop->type.t;
1879 vswap();
1880 lexpand();
1881 vrotb(3);
1882 lexpand();
1883 /* stack: L1 H1 L2 H2 */
1884 tmp = vtop[0];
1885 vtop[0] = vtop[-3];
1886 vtop[-3] = tmp;
1887 tmp = vtop[-2];
1888 vtop[-2] = vtop[-3];
1889 vtop[-3] = tmp;
1890 vswap();
1891 /* stack: H1 H2 L1 L2 */
1892 if (op == '*') {
1893 vpushv(vtop - 1);
1894 vpushv(vtop - 1);
1895 gen_op(TOK_UMULL);
1896 lexpand();
1897 /* stack: H1 H2 L1 L2 ML MH */
1898 for(i=0;i<4;i++)
1899 vrotb(6);
1900 /* stack: ML MH H1 H2 L1 L2 */
1901 tmp = vtop[0];
1902 vtop[0] = vtop[-2];
1903 vtop[-2] = tmp;
1904 /* stack: ML MH H1 L2 H2 L1 */
1905 gen_op('*');
1906 vrotb(3);
1907 vrotb(3);
1908 gen_op('*');
1909 /* stack: ML MH M1 M2 */
1910 gen_op('+');
1911 gen_op('+');
1912 } else if (op == '+' || op == '-') {
1913 /* XXX: add non carry method too (for MIPS or alpha) */
1914 if (op == '+')
1915 op1 = TOK_ADDC1;
1916 else
1917 op1 = TOK_SUBC1;
1918 gen_op(op1);
1919 /* stack: H1 H2 (L1 op L2) */
1920 vrotb(3);
1921 vrotb(3);
1922 gen_op(op1 + 1); /* TOK_xxxC2 */
1923 } else {
1924 gen_op(op);
1925 /* stack: H1 H2 (L1 op L2) */
1926 vrotb(3);
1927 vrotb(3);
1928 /* stack: (L1 op L2) H1 H2 */
1929 gen_op(op);
1930 /* stack: (L1 op L2) (H1 op H2) */
1931 }
1932 /* stack: L H */
1933 lbuild(t);
1934 break;
1935 case TOK_SAR:
1936 case TOK_SHR:
1937 case TOK_SHL:
1938 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
1939 t = vtop[-1].type.t;
1940 vswap();
1941 lexpand();
1942 vrotb(3);
1943 /* stack: L H shift */
1944 c = (int)vtop->c.i;
1945 /* constant: simpler */
1946 /* NOTE: all comments are for SHL. the other cases are
1947 done by swaping words */
1948 vpop();
1949 if (op != TOK_SHL)
1950 vswap();
1951 if (c >= 32) {
1952 /* stack: L H */
1953 vpop();
1954 if (c > 32) {
1955 vpushi(c - 32);
1956 gen_op(op);
1957 }
1958 if (op != TOK_SAR) {
1959 vpushi(0);
1960 } else {
1961 gv_dup();
1962 vpushi(31);
1963 gen_op(TOK_SAR);
1964 }
1965 vswap();
1966 } else {
1967 vswap();
1968 gv_dup();
1969 /* stack: H L L */
1970 vpushi(c);
1971 gen_op(op);
1972 vswap();
1973 vpushi(32 - c);
1974 if (op == TOK_SHL)
1975 gen_op(TOK_SHR);
1976 else
1977 gen_op(TOK_SHL);
1978 vrotb(3);
1979 /* stack: L L H */
1980 vpushi(c);
1981 if (op == TOK_SHL)
1982 gen_op(TOK_SHL);
1983 else
1984 gen_op(TOK_SHR);
1985 gen_op('|');
1986 }
1987 if (op != TOK_SHL)
1988 vswap();
1989 lbuild(t);
1990 } else {
1991 /* XXX: should provide a faster fallback on x86 ? */
1992 switch(op) {
1993 case TOK_SAR:
1994 func = TOK___ashrdi3;
1995 goto gen_func;
1996 case TOK_SHR:
1997 func = TOK___lshrdi3;
1998 goto gen_func;
1999 case TOK_SHL:
2000 func = TOK___ashldi3;
2001 goto gen_func;
2002 }
2003 }
2004 break;
2005 default:
2006 /* compare operations */
2007 t = vtop->type.t;
2008 vswap();
2009 lexpand();
2010 vrotb(3);
2011 lexpand();
2012 /* stack: L1 H1 L2 H2 */
2013 tmp = vtop[-1];
2014 vtop[-1] = vtop[-2];
2015 vtop[-2] = tmp;
2016 /* stack: L1 L2 H1 H2 */
2017 /* compare high */
2018 op1 = op;
2019 /* when values are equal, we need to compare low words. since
2020 the jump is inverted, we invert the test too. */
2021 if (op1 == TOK_LT)
2022 op1 = TOK_LE;
2023 else if (op1 == TOK_GT)
2024 op1 = TOK_GE;
2025 else if (op1 == TOK_ULT)
2026 op1 = TOK_ULE;
2027 else if (op1 == TOK_UGT)
2028 op1 = TOK_UGE;
2029 a = 0;
2030 b = 0;
2031 gen_op(op1);
2032 if (op1 != TOK_NE) {
2033 a = gtst(1, 0);
2034 }
2035 if (op != TOK_EQ) {
2036 /* generate non equal test */
2037 /* XXX: NOT PORTABLE yet */
2038 if (a == 0) {
2039 b = gtst(0, 0);
2040 } else {
2041#if defined(TCC_TARGET_I386)
2042 b = psym(0x850f, 0);
2043#elif defined(TCC_TARGET_ARM)
2044 b = ind;
2045 o(0x1A000000 | encbranch(ind, 0, 1));
2046#elif defined(TCC_TARGET_C67)
2047 error("not implemented");
2048#else
2049#error not supported
2050#endif
2051 }
2052 }
2053 /* compare low. Always unsigned */
2054 op1 = op;
2055 if (op1 == TOK_LT)
2056 op1 = TOK_ULT;
2057 else if (op1 == TOK_LE)
2058 op1 = TOK_ULE;
2059 else if (op1 == TOK_GT)
2060 op1 = TOK_UGT;
2061 else if (op1 == TOK_GE)
2062 op1 = TOK_UGE;
2063 gen_op(op1);
2064 a = gtst(1, a);
2065 gsym(b);
2066 vseti(VT_JMPI, a);
2067 break;
2068 }
2069}
2070#endif
2071
2072/* handle integer constant optimizations and various machine
2073 independent opt */
2074void gen_opic(int op)
2075{
2076 int c1, c2, t1, t2, n;
2077 SValue *v1, *v2;
2078 long long l1, l2;
2079 typedef unsigned long long U;
2080
2081 v1 = vtop - 1;
2082 v2 = vtop;
2083 t1 = v1->type.t & VT_BTYPE;
2084 t2 = v2->type.t & VT_BTYPE;
2085
2086 if (t1 == VT_LLONG)
2087 l1 = v1->c.ll;
2088 else if (v1->type.t & VT_UNSIGNED)
2089 l1 = v1->c.ui;
2090 else
2091 l1 = v1->c.i;
2092
2093 if (t2 == VT_LLONG)
2094 l2 = v2->c.ll;
2095 else if (v2->type.t & VT_UNSIGNED)
2096 l2 = v2->c.ui;
2097 else
2098 l2 = v2->c.i;
2099
2100 /* currently, we cannot do computations with forward symbols */
2101 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2102 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2103 if (c1 && c2) {
2104 switch(op) {
2105 case '+': l1 += l2; break;
2106 case '-': l1 -= l2; break;
2107 case '&': l1 &= l2; break;
2108 case '^': l1 ^= l2; break;
2109 case '|': l1 |= l2; break;
2110 case '*': l1 *= l2; break;
2111
2112 case TOK_PDIV:
2113 case '/':
2114 case '%':
2115 case TOK_UDIV:
2116 case TOK_UMOD:
2117 /* if division by zero, generate explicit division */
2118 if (l2 == 0) {
2119 if (const_wanted)
2120 error("division by zero in constant");
2121 goto general_case;
2122 }
2123 switch(op) {
2124 default: l1 /= l2; break;
2125 case '%': l1 %= l2; break;
2126 case TOK_UDIV: l1 = (U)l1 / l2; break;
2127 case TOK_UMOD: l1 = (U)l1 % l2; break;
2128 }
2129 break;
2130 case TOK_SHL: l1 <<= l2; break;
2131 case TOK_SHR: l1 = (U)l1 >> l2; break;
2132 case TOK_SAR: l1 >>= l2; break;
2133 /* tests */
2134 case TOK_ULT: l1 = (U)l1 < (U)l2; break;
2135 case TOK_UGE: l1 = (U)l1 >= (U)l2; break;
2136 case TOK_EQ: l1 = l1 == l2; break;
2137 case TOK_NE: l1 = l1 != l2; break;
2138 case TOK_ULE: l1 = (U)l1 <= (U)l2; break;
2139 case TOK_UGT: l1 = (U)l1 > (U)l2; break;
2140 case TOK_LT: l1 = l1 < l2; break;
2141 case TOK_GE: l1 = l1 >= l2; break;
2142 case TOK_LE: l1 = l1 <= l2; break;
2143 case TOK_GT: l1 = l1 > l2; break;
2144 /* logical */
2145 case TOK_LAND: l1 = l1 && l2; break;
2146 case TOK_LOR: l1 = l1 || l2; break;
2147 default:
2148 goto general_case;
2149 }
2150 v1->c.ll = l1;
2151 vtop--;
2152 } else {
2153 /* if commutative ops, put c2 as constant */
2154 if (c1 && (op == '+' || op == '&' || op == '^' ||
2155 op == '|' || op == '*')) {
2156 vswap();
2157 c2 = c1; //c = c1, c1 = c2, c2 = c;
2158 l2 = l1; //l = l1, l1 = l2, l2 = l;
2159 }
2160 /* Filter out NOP operations like x*1, x-0, x&-1... */
2161 if (c2 && (((op == '*' || op == '/' || op == TOK_UDIV ||
2162 op == TOK_PDIV) &&
2163 l2 == 1) ||
2164 ((op == '+' || op == '-' || op == '|' || op == '^' ||
2165 op == TOK_SHL || op == TOK_SHR || op == TOK_SAR) &&
2166 l2 == 0) ||
2167 (op == '&' &&
2168 l2 == -1))) {
2169 /* nothing to do */
2170 vtop--;
2171 } else if (c2 && (op == '*' || op == TOK_PDIV || op == TOK_UDIV)) {
2172 /* try to use shifts instead of muls or divs */
2173 if (l2 > 0 && (l2 & (l2 - 1)) == 0) {
2174 n = -1;
2175 while (l2) {
2176 l2 >>= 1;
2177 n++;
2178 }
2179 vtop->c.ll = n;
2180 if (op == '*')
2181 op = TOK_SHL;
2182 else if (op == TOK_PDIV)
2183 op = TOK_SAR;
2184 else
2185 op = TOK_SHR;
2186 }
2187 goto general_case;
2188 } else if (c2 && (op == '+' || op == '-') &&
2189 ((vtop[-1].r & (VT_VALMASK | VT_LVAL | VT_SYM)) ==
2190 (VT_CONST | VT_SYM) ||
2191 (vtop[-1].r & (VT_VALMASK | VT_LVAL)) == VT_LOCAL)) {
2192 /* symbol + constant case */
2193 if (op == '-')
2194 l2 = -l2;
2195 vtop--;
2196 vtop->c.ll += l2;
2197 } else {
2198 general_case:
2199 if (!nocode_wanted) {
2200 /* call low level op generator */
2201 if (t1 == VT_LLONG || t2 == VT_LLONG)
2202 gen_opl(op);
2203 else
2204 gen_opi(op);
2205 } else {
2206 vtop--;
2207 }
2208 }
2209 }
2210}
2211
2212/* generate a floating point operation with constant propagation */
2213void gen_opif(int op)
2214{
2215 int c1, c2;
2216 SValue *v1, *v2;
2217 long double f1, f2;
2218
2219 v1 = vtop - 1;
2220 v2 = vtop;
2221 /* currently, we cannot do computations with forward symbols */
2222 c1 = (v1->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2223 c2 = (v2->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2224 if (c1 && c2) {
2225 if (v1->type.t == VT_FLOAT) {
2226 f1 = v1->c.f;
2227 f2 = v2->c.f;
2228 } else if (v1->type.t == VT_DOUBLE) {
2229 f1 = v1->c.d;
2230 f2 = v2->c.d;
2231 } else {
2232 f1 = v1->c.ld;
2233 f2 = v2->c.ld;
2234 }
2235
2236 /* NOTE: we only do constant propagation if finite number (not
2237 NaN or infinity) (ANSI spec) */
2238 if (!ieee_finite(f1) || !ieee_finite(f2))
2239 goto general_case;
2240
2241 switch(op) {
2242 case '+': f1 += f2; break;
2243 case '-': f1 -= f2; break;
2244 case '*': f1 *= f2; break;
2245 case '/':
2246 if (f2 == 0.0) {
2247 if (const_wanted)
2248 error("division by zero in constant");
2249 goto general_case;
2250 }
2251 f1 /= f2;
2252 break;
2253 /* XXX: also handles tests ? */
2254 default:
2255 goto general_case;
2256 }
2257 /* XXX: overflow test ? */
2258 if (v1->type.t == VT_FLOAT) {
2259 v1->c.f = f1;
2260 } else if (v1->type.t == VT_DOUBLE) {
2261 v1->c.d = f1;
2262 } else {
2263 v1->c.ld = f1;
2264 }
2265 vtop--;
2266 } else {
2267 general_case:
2268 if (!nocode_wanted) {
2269 gen_opf(op);
2270 } else {
2271 vtop--;
2272 }
2273 }
2274}
2275
2276static int pointed_size(CType *type)
2277{
2278 int align;
2279 return type_size(pointed_type(type), &align);
2280}
2281
2282static inline int is_null_pointer(SValue *p)
2283{
2284 if ((p->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
2285 return 0;
2286 return ((p->type.t & VT_BTYPE) == VT_INT && p->c.i == 0) ||
2287 ((p->type.t & VT_BTYPE) == VT_LLONG && p->c.ll == 0);
2288}
2289
2290static inline int is_integer_btype(int bt)
2291{
2292 return (bt == VT_BYTE || bt == VT_SHORT ||
2293 bt == VT_INT || bt == VT_LLONG);
2294}
2295
2296/* check types for comparison or substraction of pointers */
2297static void check_comparison_pointer_types(SValue *p1, SValue *p2, int op)
2298{
2299 CType *type1, *type2, tmp_type1, tmp_type2;
2300 int bt1, bt2;
2301
2302 /* null pointers are accepted for all comparisons as gcc */
2303 if (is_null_pointer(p1) || is_null_pointer(p2))
2304 return;
2305 type1 = &p1->type;
2306 type2 = &p2->type;
2307 bt1 = type1->t & VT_BTYPE;
2308 bt2 = type2->t & VT_BTYPE;
2309 /* accept comparison between pointer and integer with a warning */
2310 if ((is_integer_btype(bt1) || is_integer_btype(bt2)) && op != '-') {
2311 if (op != TOK_LOR && op != TOK_LAND )
2312 warning("comparison between pointer and integer");
2313 return;
2314 }
2315
2316 /* both must be pointers or implicit function pointers */
2317 if (bt1 == VT_PTR) {
2318 type1 = pointed_type(type1);
2319 } else if (bt1 != VT_FUNC)
2320 goto invalid_operands;
2321
2322 if (bt2 == VT_PTR) {
2323 type2 = pointed_type(type2);
2324 } else if (bt2 != VT_FUNC) {
2325 invalid_operands:
2326 error("invalid operands to binary %s", get_tok_str(op, NULL));
2327 }
2328 if ((type1->t & VT_BTYPE) == VT_VOID ||
2329 (type2->t & VT_BTYPE) == VT_VOID)
2330 return;
2331 tmp_type1 = *type1;
2332 tmp_type2 = *type2;
2333 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2334 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
2335 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
2336 /* gcc-like error if '-' is used */
2337 if (op == '-')
2338 goto invalid_operands;
2339 else
2340 warning("comparison of distinct pointer types lacks a cast");
2341 }
2342}
2343
2344/* generic gen_op: handles types problems */
2345void gen_op(int op)
2346{
2347 int u, t1, t2, bt1, bt2, t;
2348 CType type1;
2349
2350 t1 = vtop[-1].type.t;
2351 t2 = vtop[0].type.t;
2352 bt1 = t1 & VT_BTYPE;
2353 bt2 = t2 & VT_BTYPE;
2354
2355 if (bt1 == VT_PTR || bt2 == VT_PTR) {
2356 /* at least one operand is a pointer */
2357 /* relationnal op: must be both pointers */
2358 if (op >= TOK_ULT && op <= TOK_LOR) {
2359 check_comparison_pointer_types(vtop - 1, vtop, op);
2360 /* pointers are handled are unsigned */
2361#ifdef TCC_TARGET_X86_64
2362 t = VT_LLONG | VT_UNSIGNED;
2363#else
2364 t = VT_INT | VT_UNSIGNED;
2365#endif
2366 goto std_op;
2367 }
2368 /* if both pointers, then it must be the '-' op */
2369 if (bt1 == VT_PTR && bt2 == VT_PTR) {
2370 if (op != '-')
2371 error("cannot use pointers here");
2372 check_comparison_pointer_types(vtop - 1, vtop, op);
2373 /* XXX: check that types are compatible */
2374 u = pointed_size(&vtop[-1].type);
2375 gen_opic(op);
2376 /* set to integer type */
2377#ifdef TCC_TARGET_X86_64
2378 vtop->type.t = VT_LLONG;
2379#else
2380 vtop->type.t = VT_INT;
2381#endif
2382 vpushi(u);
2383 gen_op(TOK_PDIV);
2384 } else {
2385 /* exactly one pointer : must be '+' or '-'. */
2386 if (op != '-' && op != '+')
2387 error("cannot use pointers here");
2388 /* Put pointer as first operand */
2389 if (bt2 == VT_PTR) {
2390 vswap();
2391 swap(&t1, &t2);
2392 }
2393 type1 = vtop[-1].type;
2394#ifdef TCC_TARGET_X86_64
2395 vpushll(pointed_size(&vtop[-1].type));
2396#else
2397 /* XXX: cast to int ? (long long case) */
2398 vpushi(pointed_size(&vtop[-1].type));
2399#endif
2400 gen_op('*');
2401#ifdef CONFIG_TCC_BCHECK
2402 /* if evaluating constant expression, no code should be
2403 generated, so no bound check */
2404 if (do_bounds_check && !const_wanted) {
2405 /* if bounded pointers, we generate a special code to
2406 test bounds */
2407 if (op == '-') {
2408 vpushi(0);
2409 vswap();
2410 gen_op('-');
2411 }
2412 gen_bounded_ptr_add();
2413 } else
2414#endif
2415 {
2416 gen_opic(op);
2417 }
2418 /* put again type if gen_opic() swaped operands */
2419 vtop->type = type1;
2420 }
2421 } else if (is_float(bt1) || is_float(bt2)) {
2422 /* compute bigger type and do implicit casts */
2423 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
2424 t = VT_LDOUBLE;
2425 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
2426 t = VT_DOUBLE;
2427 } else {
2428 t = VT_FLOAT;
2429 }
2430 /* floats can only be used for a few operations */
2431 if (op != '+' && op != '-' && op != '*' && op != '/' &&
2432 (op < TOK_ULT || op > TOK_GT))
2433 error("invalid operands for binary operation");
2434 goto std_op;
2435 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
2436 /* cast to biggest op */
2437 t = VT_LLONG;
2438 /* convert to unsigned if it does not fit in a long long */
2439 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
2440 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
2441 t |= VT_UNSIGNED;
2442 goto std_op;
2443 } else {
2444 /* integer operations */
2445 t = VT_INT;
2446 /* convert to unsigned if it does not fit in an integer */
2447 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
2448 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
2449 t |= VT_UNSIGNED;
2450 std_op:
2451 /* XXX: currently, some unsigned operations are explicit, so
2452 we modify them here */
2453 if (t & VT_UNSIGNED) {
2454 if (op == TOK_SAR)
2455 op = TOK_SHR;
2456 else if (op == '/')
2457 op = TOK_UDIV;
2458 else if (op == '%')
2459 op = TOK_UMOD;
2460 else if (op == TOK_LT)
2461 op = TOK_ULT;
2462 else if (op == TOK_GT)
2463 op = TOK_UGT;
2464 else if (op == TOK_LE)
2465 op = TOK_ULE;
2466 else if (op == TOK_GE)
2467 op = TOK_UGE;
2468 }
2469 vswap();
2470 type1.t = t;
2471 gen_cast(&type1);
2472 vswap();
2473 /* special case for shifts and long long: we keep the shift as
2474 an integer */
2475 if (op == TOK_SHR || op == TOK_SAR || op == TOK_SHL)
2476 type1.t = VT_INT;
2477 gen_cast(&type1);
2478 if (is_float(t))
2479 gen_opif(op);
2480 else
2481 gen_opic(op);
2482 if (op >= TOK_ULT && op <= TOK_GT) {
2483 /* relationnal op: the result is an int */
2484 vtop->type.t = VT_INT;
2485 } else {
2486 vtop->type.t = t;
2487 }
2488 }
2489}
2490
2491#ifndef TCC_TARGET_ARM
2492/* generic itof for unsigned long long case */
2493void gen_cvt_itof1(int t)
2494{
2495 if ((vtop->type.t & (VT_BTYPE | VT_UNSIGNED)) ==
2496 (VT_LLONG | VT_UNSIGNED)) {
2497
2498 if (t == VT_FLOAT)
2499 vpush_global_sym(&func_old_type, TOK___floatundisf);
2500#if LDOUBLE_SIZE != 8
2501 else if (t == VT_LDOUBLE)
2502 vpush_global_sym(&func_old_type, TOK___floatundixf);
2503#endif
2504 else
2505 vpush_global_sym(&func_old_type, TOK___floatundidf);
2506 vrott(2);
2507 gfunc_call(1);
2508 vpushi(0);
2509 vtop->r = reg_fret(t);
2510 } else {
2511 gen_cvt_itof(t);
2512 }
2513}
2514#endif
2515
2516/* generic ftoi for unsigned long long case */
2517void gen_cvt_ftoi1(int t)
2518{
2519 int st;
2520
2521 if (t == (VT_LLONG | VT_UNSIGNED)) {
2522 /* not handled natively */
2523 st = vtop->type.t & VT_BTYPE;
2524 if (st == VT_FLOAT)
2525 vpush_global_sym(&func_old_type, TOK___fixunssfdi);
2526#if LDOUBLE_SIZE != 8
2527 else if (st == VT_LDOUBLE)
2528 vpush_global_sym(&func_old_type, TOK___fixunsxfdi);
2529#endif
2530 else
2531 vpush_global_sym(&func_old_type, TOK___fixunsdfdi);
2532 vrott(2);
2533 gfunc_call(1);
2534 vpushi(0);
2535 vtop->r = REG_IRET;
2536 vtop->r2 = REG_LRET;
2537 } else {
2538 gen_cvt_ftoi(t);
2539 }
2540}
2541
2542/* force char or short cast */
2543void force_charshort_cast(int t)
2544{
2545 int bits, dbt;
2546 dbt = t & VT_BTYPE;
2547 /* XXX: add optimization if lvalue : just change type and offset */
2548 if (dbt == VT_BYTE)
2549 bits = 8;
2550 else
2551 bits = 16;
2552 if (t & VT_UNSIGNED) {
2553 vpushi((1 << bits) - 1);
2554 gen_op('&');
2555 } else {
2556 bits = 32 - bits;
2557 vpushi(bits);
2558 gen_op(TOK_SHL);
2559 /* result must be signed or the SAR is converted to an SHL
2560 This was not the case when "t" was a signed short
2561 and the last value on the stack was an unsigned int */
2562 vtop->type.t &= ~VT_UNSIGNED;
2563 vpushi(bits);
2564 gen_op(TOK_SAR);
2565 }
2566}
2567
2568/* cast 'vtop' to 'type'. Casting to bitfields is forbidden. */
2569static void gen_cast(CType *type)
2570{
2571 int sbt, dbt, sf, df, c, p;
2572
2573 /* special delayed cast for char/short */
2574 /* XXX: in some cases (multiple cascaded casts), it may still
2575 be incorrect */
2576 if (vtop->r & VT_MUSTCAST) {
2577 vtop->r &= ~VT_MUSTCAST;
2578 force_charshort_cast(vtop->type.t);
2579 }
2580
2581 /* bitfields first get cast to ints */
2582 if (vtop->type.t & VT_BITFIELD) {
2583 gv(RC_INT);
2584 }
2585
2586 dbt = type->t & (VT_BTYPE | VT_UNSIGNED);
2587 sbt = vtop->type.t & (VT_BTYPE | VT_UNSIGNED);
2588
2589 if (sbt != dbt) {
2590 sf = is_float(sbt);
2591 df = is_float(dbt);
2592 c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
2593 p = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == (VT_CONST | VT_SYM);
2594 if (c) {
2595 /* constant case: we can do it now */
2596 /* XXX: in ISOC, cannot do it if error in convert */
2597 if (sbt == VT_FLOAT)
2598 vtop->c.ld = vtop->c.f;
2599 else if (sbt == VT_DOUBLE)
2600 vtop->c.ld = vtop->c.d;
2601
2602 if (df) {
2603 if ((sbt & VT_BTYPE) == VT_LLONG) {
2604 if (sbt & VT_UNSIGNED)
2605 vtop->c.ld = vtop->c.ull;
2606 else
2607 vtop->c.ld = vtop->c.ll;
2608 } else if(!sf) {
2609 if (sbt & VT_UNSIGNED)
2610 vtop->c.ld = vtop->c.ui;
2611 else
2612 vtop->c.ld = vtop->c.i;
2613 }
2614
2615 if (dbt == VT_FLOAT)
2616 vtop->c.f = (float)vtop->c.ld;
2617 else if (dbt == VT_DOUBLE)
2618 vtop->c.d = (double)vtop->c.ld;
2619 } else if (sf && dbt == (VT_LLONG|VT_UNSIGNED)) {
2620 vtop->c.ull = (unsigned long long)vtop->c.ld;
2621 } else if (sf && dbt == VT_BOOL) {
2622 vtop->c.i = (vtop->c.ld != 0);
2623 } else {
2624 if(sf)
2625 vtop->c.ll = (long long)vtop->c.ld;
2626 else if (sbt == (VT_LLONG|VT_UNSIGNED))
2627 vtop->c.ll = vtop->c.ull;
2628 else if (sbt & VT_UNSIGNED)
2629 vtop->c.ll = vtop->c.ui;
2630 else if (sbt != VT_LLONG)
2631 vtop->c.ll = vtop->c.i;
2632
2633 if (dbt == (VT_LLONG|VT_UNSIGNED))
2634 vtop->c.ull = vtop->c.ll;
2635 else if (dbt == VT_BOOL)
2636 vtop->c.i = (vtop->c.ll != 0);
2637 else if (dbt != VT_LLONG) {
2638 int s = 0;
2639 if ((dbt & VT_BTYPE) == VT_BYTE)
2640 s = 24;
2641 else if ((dbt & VT_BTYPE) == VT_SHORT)
2642 s = 16;
2643
2644 if(dbt & VT_UNSIGNED)
2645 vtop->c.ui = ((unsigned int)vtop->c.ll << s) >> s;
2646 else
2647 vtop->c.i = ((int)vtop->c.ll << s) >> s;
2648 }
2649 }
2650 } else if (p && dbt == VT_BOOL) {
2651 vtop->r = VT_CONST;
2652 vtop->c.i = 1;
2653 } else if (!nocode_wanted) {
2654 /* non constant case: generate code */
2655 if (sf && df) {
2656 /* convert from fp to fp */
2657 gen_cvt_ftof(dbt);
2658 } else if (df) {
2659 /* convert int to fp */
2660 gen_cvt_itof1(dbt);
2661 } else if (sf) {
2662 /* convert fp to int */
2663 if (dbt == VT_BOOL) {
2664 vpushi(0);
2665 gen_op(TOK_NE);
2666 } else {
2667 /* we handle char/short/etc... with generic code */
2668 if (dbt != (VT_INT | VT_UNSIGNED) &&
2669 dbt != (VT_LLONG | VT_UNSIGNED) &&
2670 dbt != VT_LLONG)
2671 dbt = VT_INT;
2672 gen_cvt_ftoi1(dbt);
2673 if (dbt == VT_INT && (type->t & (VT_BTYPE | VT_UNSIGNED)) != dbt) {
2674 /* additional cast for char/short... */
2675 vtop->type.t = dbt;
2676 gen_cast(type);
2677 }
2678 }
2679#ifndef TCC_TARGET_X86_64
2680 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
2681 if ((sbt & VT_BTYPE) != VT_LLONG) {
2682 /* scalar to long long */
2683 /* machine independent conversion */
2684 gv(RC_INT);
2685 /* generate high word */
2686 if (sbt == (VT_INT | VT_UNSIGNED)) {
2687 vpushi(0);
2688 gv(RC_INT);
2689 } else {
2690 if (sbt == VT_PTR) {
2691 /* cast from pointer to int before we apply
2692 shift operation, which pointers don't support*/
2693 gen_cast(&int_type);
2694 }
2695 gv_dup();
2696 vpushi(31);
2697 gen_op(TOK_SAR);
2698 }
2699 /* patch second register */
2700 vtop[-1].r2 = vtop->r;
2701 vpop();
2702 }
2703#else
2704 } else if ((dbt & VT_BTYPE) == VT_LLONG ||
2705 (dbt & VT_BTYPE) == VT_PTR) {
2706 /* XXX: not sure if this is perfect... need more tests */
2707 if ((sbt & VT_BTYPE) != VT_LLONG) {
2708 int r = gv(RC_INT);
2709 if (sbt != (VT_INT | VT_UNSIGNED) &&
2710 sbt != VT_PTR && sbt != VT_FUNC) {
2711 /* x86_64 specific: movslq */
2712 o(0x6348);
2713 o(0xc0 + (REG_VALUE(r) << 3) + REG_VALUE(r));
2714 }
2715 }
2716#endif
2717 } else if (dbt == VT_BOOL) {
2718 /* scalar to bool */
2719 vpushi(0);
2720 gen_op(TOK_NE);
2721 } else if ((dbt & VT_BTYPE) == VT_BYTE ||
2722 (dbt & VT_BTYPE) == VT_SHORT) {
2723 if (sbt == VT_PTR) {
2724 vtop->type.t = VT_INT;
2725 warning("nonportable conversion from pointer to char/short");
2726 }
2727 force_charshort_cast(dbt);
2728 } else if ((dbt & VT_BTYPE) == VT_INT) {
2729 /* scalar to int */
2730 if (sbt == VT_LLONG) {
2731 /* from long long: just take low order word */
2732 lexpand();
2733 vpop();
2734 }
2735 /* if lvalue and single word type, nothing to do because
2736 the lvalue already contains the real type size (see
2737 VT_LVAL_xxx constants) */
2738 }
2739 }
2740 } else if ((dbt & VT_BTYPE) == VT_PTR && !(vtop->r & VT_LVAL)) {
2741 /* if we are casting between pointer types,
2742 we must update the VT_LVAL_xxx size */
2743 vtop->r = (vtop->r & ~VT_LVAL_TYPE)
2744 | (lvalue_type(type->ref->type.t) & VT_LVAL_TYPE);
2745 }
2746 vtop->type = *type;
2747}
2748
2749/* return type size. Put alignment at 'a' */
2750static int type_size(CType *type, int *a)
2751{
2752 Sym *s;
2753 int bt;
2754
2755 bt = type->t & VT_BTYPE;
2756 if (bt == VT_STRUCT) {
2757 /* struct/union */
2758 s = type->ref;
2759 *a = s->r;
2760 return s->c;
2761 } else if (bt == VT_PTR) {
2762 if (type->t & VT_ARRAY) {
2763 int ts;
2764
2765 s = type->ref;
2766 ts = type_size(&s->type, a);
2767
2768 if (ts < 0 && s->c < 0)
2769 ts = -ts;
2770
2771 return ts * s->c;
2772 } else {
2773 *a = PTR_SIZE;
2774 return PTR_SIZE;
2775 }
2776 } else if (bt == VT_LDOUBLE) {
2777 *a = LDOUBLE_ALIGN;
2778 return LDOUBLE_SIZE;
2779 } else if (bt == VT_DOUBLE || bt == VT_LLONG) {
2780#ifdef TCC_TARGET_I386
2781#ifdef TCC_TARGET_PE
2782 *a = 8;
2783#else
2784 *a = 4;
2785#endif
2786#elif defined(TCC_TARGET_ARM)
2787#ifdef TCC_ARM_EABI
2788 *a = 8;
2789#else
2790 *a = 4;
2791#endif
2792#else
2793 *a = 8;
2794#endif
2795 return 8;
2796 } else if (bt == VT_INT || bt == VT_ENUM || bt == VT_FLOAT) {
2797 *a = 4;
2798 return 4;
2799 } else if (bt == VT_SHORT) {
2800 *a = 2;
2801 return 2;
2802 } else {
2803 /* char, void, function, _Bool */
2804 *a = 1;
2805 return 1;
2806 }
2807}
2808
2809/* return the pointed type of t */
2810static inline CType *pointed_type(CType *type)
2811{
2812 return &type->ref->type;
2813}
2814
2815/* modify type so that its it is a pointer to type. */
2816static void mk_pointer(CType *type)
2817{
2818 Sym *s;
2819 s = sym_push(SYM_FIELD, type, 0, -1);
2820 type->t = VT_PTR | (type->t & ~VT_TYPE);
2821 type->ref = s;
2822}
2823
2824/* compare function types. OLD functions match any new functions */
2825static int is_compatible_func(CType *type1, CType *type2)
2826{
2827 Sym *s1, *s2;
2828
2829 s1 = type1->ref;
2830 s2 = type2->ref;
2831 if (!is_compatible_types(&s1->type, &s2->type))
2832 return 0;
2833 /* check func_call */
2834 if (FUNC_CALL(s1->r) != FUNC_CALL(s2->r))
2835 return 0;
2836 /* XXX: not complete */
2837 if (s1->c == FUNC_OLD || s2->c == FUNC_OLD)
2838 return 1;
2839 if (s1->c != s2->c)
2840 return 0;
2841 while (s1 != NULL) {
2842 if (s2 == NULL)
2843 return 0;
2844 if (!is_compatible_parameter_types(&s1->type, &s2->type))
2845 return 0;
2846 s1 = s1->next;
2847 s2 = s2->next;
2848 }
2849 if (s2)
2850 return 0;
2851 return 1;
2852}
2853
2854/* return true if type1 and type2 are the same. If unqualified is
2855 true, qualifiers on the types are ignored.
2856
2857 - enums are not checked as gcc __builtin_types_compatible_p ()
2858 */
2859static int compare_types(CType *type1, CType *type2, int unqualified)
2860{
2861 int bt1, t1, t2;
2862
2863 t1 = type1->t & VT_TYPE;
2864 t2 = type2->t & VT_TYPE;
2865 if (unqualified) {
2866 /* strip qualifiers before comparing */
2867 t1 &= ~(VT_CONSTANT | VT_VOLATILE);
2868 t2 &= ~(VT_CONSTANT | VT_VOLATILE);
2869 }
2870 /* XXX: bitfields ? */
2871 if (t1 != t2)
2872 return 0;
2873 /* test more complicated cases */
2874 bt1 = t1 & VT_BTYPE;
2875 if (bt1 == VT_PTR) {
2876 type1 = pointed_type(type1);
2877 type2 = pointed_type(type2);
2878 return is_compatible_types(type1, type2);
2879 } else if (bt1 == VT_STRUCT) {
2880 return (type1->ref == type2->ref);
2881 } else if (bt1 == VT_FUNC) {
2882 return is_compatible_func(type1, type2);
2883 } else {
2884 return 1;
2885 }
2886}
2887
2888/* return true if type1 and type2 are exactly the same (including
2889 qualifiers).
2890*/
2891static int is_compatible_types(CType *type1, CType *type2)
2892{
2893 return compare_types(type1,type2,0);
2894}
2895
2896/* return true if type1 and type2 are the same (ignoring qualifiers).
2897*/
2898static int is_compatible_parameter_types(CType *type1, CType *type2)
2899{
2900 return compare_types(type1,type2,1);
2901}
2902
2903/* print a type. If 'varstr' is not NULL, then the variable is also
2904 printed in the type */
2905/* XXX: union */
2906/* XXX: add array and function pointers */
2907void type_to_str(char *buf, int buf_size,
2908 CType *type, const char *varstr)
2909{
2910 int bt, v, t;
2911 Sym *s, *sa;
2912 char buf1[256];
2913 const char *tstr;
2914
2915 t = type->t & VT_TYPE;
2916 bt = t & VT_BTYPE;
2917 buf[0] = '\0';
2918 if (t & VT_CONSTANT)
2919 pstrcat(buf, buf_size, "const ");
2920 if (t & VT_VOLATILE)
2921 pstrcat(buf, buf_size, "volatile ");
2922 if (t & VT_UNSIGNED)
2923 pstrcat(buf, buf_size, "unsigned ");
2924 switch(bt) {
2925 case VT_VOID:
2926 tstr = "void";
2927 goto add_tstr;
2928 case VT_BOOL:
2929 tstr = "_Bool";
2930 goto add_tstr;
2931 case VT_BYTE:
2932 tstr = "char";
2933 goto add_tstr;
2934 case VT_SHORT:
2935 tstr = "short";
2936 goto add_tstr;
2937 case VT_INT:
2938 tstr = "int";
2939 goto add_tstr;
2940 case VT_LONG:
2941 tstr = "long";
2942 goto add_tstr;
2943 case VT_LLONG:
2944 tstr = "long long";
2945 goto add_tstr;
2946 case VT_FLOAT:
2947 tstr = "float";
2948 goto add_tstr;
2949 case VT_DOUBLE:
2950 tstr = "double";
2951 goto add_tstr;
2952 case VT_LDOUBLE:
2953 tstr = "long double";
2954 add_tstr:
2955 pstrcat(buf, buf_size, tstr);
2956 break;
2957 case VT_ENUM:
2958 case VT_STRUCT:
2959 if (bt == VT_STRUCT)
2960 tstr = "struct ";
2961 else
2962 tstr = "enum ";
2963 pstrcat(buf, buf_size, tstr);
2964 v = type->ref->v & ~SYM_STRUCT;
2965 if (v >= SYM_FIRST_ANOM)
2966 pstrcat(buf, buf_size, "<anonymous>");
2967 else
2968 pstrcat(buf, buf_size, get_tok_str(v, NULL));
2969 break;
2970 case VT_FUNC:
2971 s = type->ref;
2972 type_to_str(buf, buf_size, &s->type, varstr);
2973 pstrcat(buf, buf_size, "(");
2974 sa = s->next;
2975 while (sa != NULL) {
2976 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2977 pstrcat(buf, buf_size, buf1);
2978 sa = sa->next;
2979 if (sa)
2980 pstrcat(buf, buf_size, ", ");
2981 }
2982 pstrcat(buf, buf_size, ")");
2983 goto no_var;
2984 case VT_PTR:
2985 s = type->ref;
2986 pstrcpy(buf1, sizeof(buf1), "*");
2987 if (varstr)
2988 pstrcat(buf1, sizeof(buf1), varstr);
2989 type_to_str(buf, buf_size, &s->type, buf1);
2990 goto no_var;
2991 }
2992 if (varstr) {
2993 pstrcat(buf, buf_size, " ");
2994 pstrcat(buf, buf_size, varstr);
2995 }
2996 no_var: ;
2997}
2998
2999/* verify type compatibility to store vtop in 'dt' type, and generate
3000 casts if needed. */
3001static void gen_assign_cast(CType *dt)
3002{
3003 CType *st, *type1, *type2, tmp_type1, tmp_type2;
3004 char buf1[256], buf2[256];
3005 int dbt, sbt;
3006
3007 st = &vtop->type; /* source type */
3008 dbt = dt->t & VT_BTYPE;
3009 sbt = st->t & VT_BTYPE;
3010 if (dt->t & VT_CONSTANT)
3011 warning("assignment of read-only location");
3012 switch(dbt) {
3013 case VT_PTR:
3014 /* special cases for pointers */
3015 /* '0' can also be a pointer */
3016 if (is_null_pointer(vtop))
3017 goto type_ok;
3018 /* accept implicit pointer to integer cast with warning */
3019 if (is_integer_btype(sbt)) {
3020 warning("assignment makes pointer from integer without a cast");
3021 goto type_ok;
3022 }
3023 type1 = pointed_type(dt);
3024 /* a function is implicitely a function pointer */
3025 if (sbt == VT_FUNC) {
3026 if ((type1->t & VT_BTYPE) != VT_VOID &&
3027 !is_compatible_types(pointed_type(dt), st))
3028 goto error;
3029 else
3030 goto type_ok;
3031 }
3032 if (sbt != VT_PTR)
3033 goto error;
3034 type2 = pointed_type(st);
3035 if ((type1->t & VT_BTYPE) == VT_VOID ||
3036 (type2->t & VT_BTYPE) == VT_VOID) {
3037 /* void * can match anything */
3038 } else {
3039 /* exact type match, except for unsigned */
3040 tmp_type1 = *type1;
3041 tmp_type2 = *type2;
3042 tmp_type1.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
3043 tmp_type2.t &= ~(VT_UNSIGNED | VT_CONSTANT | VT_VOLATILE);
3044 if (!is_compatible_types(&tmp_type1, &tmp_type2))
3045 warning("assignment from incompatible pointer type");
3046 }
3047 /* check const and volatile */
3048 if ((!(type1->t & VT_CONSTANT) && (type2->t & VT_CONSTANT)) ||
3049 (!(type1->t & VT_VOLATILE) && (type2->t & VT_VOLATILE)))
3050 warning("assignment discards qualifiers from pointer target type");
3051 break;
3052 case VT_BYTE:
3053 case VT_SHORT:
3054 case VT_INT:
3055 case VT_LLONG:
3056 if (sbt == VT_PTR || sbt == VT_FUNC) {
3057 warning("assignment makes integer from pointer without a cast");
3058 }
3059 /* XXX: more tests */
3060 break;
3061 case VT_STRUCT:
3062 tmp_type1 = *dt;
3063 tmp_type2 = *st;
3064 tmp_type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
3065 tmp_type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
3066 if (!is_compatible_types(&tmp_type1, &tmp_type2)) {
3067 error:
3068 type_to_str(buf1, sizeof(buf1), st, NULL);
3069 type_to_str(buf2, sizeof(buf2), dt, NULL);
3070 error("cannot cast '%s' to '%s'", buf1, buf2);
3071 }
3072 break;
3073 }
3074 type_ok:
3075 gen_cast(dt);
3076}
3077
3078/* store vtop in lvalue pushed on stack */
3079void vstore(void)
3080{
3081 int sbt, dbt, ft, r, t, size, align, bit_size, bit_pos, rc, delayed_cast;
3082
3083 ft = vtop[-1].type.t;
3084 sbt = vtop->type.t & VT_BTYPE;
3085 dbt = ft & VT_BTYPE;
3086 if (((sbt == VT_INT || sbt == VT_SHORT) && dbt == VT_BYTE) ||
3087 (sbt == VT_INT && dbt == VT_SHORT)) {
3088 /* optimize char/short casts */
3089 delayed_cast = VT_MUSTCAST;
3090 vtop->type.t = ft & (VT_TYPE & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT)));
3091 /* XXX: factorize */
3092 if (ft & VT_CONSTANT)
3093 warning("assignment of read-only location");
3094 } else {
3095 delayed_cast = 0;
3096 if (!(ft & VT_BITFIELD))
3097 gen_assign_cast(&vtop[-1].type);
3098 }
3099
3100 if (sbt == VT_STRUCT) {
3101 /* if structure, only generate pointer */
3102 /* structure assignment : generate memcpy */
3103 /* XXX: optimize if small size */
3104 if (!nocode_wanted) {
3105 size = type_size(&vtop->type, &align);
3106
3107#ifdef TCC_ARM_EABI
3108 if(!(align & 7))
3109 vpush_global_sym(&func_old_type, TOK_memcpy8);
3110 else if(!(align & 3))
3111 vpush_global_sym(&func_old_type, TOK_memcpy4);
3112 else
3113#endif
3114 vpush_global_sym(&func_old_type, TOK_memcpy);
3115
3116 /* destination */
3117 vpushv(vtop - 2);
3118 vtop->type.t = VT_PTR;
3119 gaddrof();
3120 /* source */
3121 vpushv(vtop - 2);
3122 vtop->type.t = VT_PTR;
3123 gaddrof();
3124 /* type size */
3125 vpushi(size);
3126 gfunc_call(3);
3127
3128 vswap();
3129 vpop();
3130 } else {
3131 vswap();
3132 vpop();
3133 }
3134 /* leave source on stack */
3135 } else if (ft & VT_BITFIELD) {
3136 /* bitfield store handling */
3137 bit_pos = (ft >> VT_STRUCT_SHIFT) & 0x3f;
3138 bit_size = (ft >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
3139 /* remove bit field info to avoid loops */
3140 vtop[-1].type.t = ft & ~(VT_BITFIELD | (-1 << VT_STRUCT_SHIFT));
3141
3142 /* duplicate source into other register */
3143 gv_dup();
3144 vswap();
3145 vrott(3);
3146
3147 if((ft & VT_BTYPE) == VT_BOOL) {
3148 gen_cast(&vtop[-1].type);
3149 vtop[-1].type.t = (vtop[-1].type.t & ~VT_BTYPE) | (VT_BYTE | VT_UNSIGNED);
3150 }
3151
3152 /* duplicate destination */
3153 vdup();
3154 vtop[-1] = vtop[-2];
3155
3156 /* mask and shift source */
3157 if((ft & VT_BTYPE) != VT_BOOL) {
3158 if((ft & VT_BTYPE) == VT_LLONG) {
3159 vpushll((1ULL << bit_size) - 1ULL);
3160 } else {
3161 vpushi((1 << bit_size) - 1);
3162 }
3163 gen_op('&');
3164 }
3165 vpushi(bit_pos);
3166 gen_op(TOK_SHL);
3167 /* load destination, mask and or with source */
3168 vswap();
3169 if((ft & VT_BTYPE) == VT_LLONG) {
3170 vpushll(~(((1ULL << bit_size) - 1ULL) << bit_pos));
3171 } else {
3172 vpushi(~(((1 << bit_size) - 1) << bit_pos));
3173 }
3174 gen_op('&');
3175 gen_op('|');
3176 /* store result */
3177 vstore();
3178
3179 /* pop off shifted source from "duplicate source..." above */
3180 vpop();
3181
3182 } else {
3183#ifdef CONFIG_TCC_BCHECK
3184 /* bound check case */
3185 if (vtop[-1].r & VT_MUSTBOUND) {
3186 vswap();
3187 gbound();
3188 vswap();
3189 }
3190#endif
3191 if (!nocode_wanted) {
3192 rc = RC_INT;
3193 if (is_float(ft)) {
3194 rc = RC_FLOAT;
3195#ifdef TCC_TARGET_X86_64
3196 if ((ft & VT_BTYPE) == VT_LDOUBLE) {
3197 rc = RC_ST0;
3198 }
3199#endif
3200 }
3201 r = gv(rc); /* generate value */
3202 /* if lvalue was saved on stack, must read it */
3203 if ((vtop[-1].r & VT_VALMASK) == VT_LLOCAL) {
3204 SValue sv;
3205 t = get_reg(RC_INT);
3206#ifdef TCC_TARGET_X86_64
3207 sv.type.t = VT_PTR;
3208#else
3209 sv.type.t = VT_INT;
3210#endif
3211 sv.r = VT_LOCAL | VT_LVAL;
3212 sv.c.ul = vtop[-1].c.ul;
3213 load(t, &sv);
3214 vtop[-1].r = t | VT_LVAL;
3215 }
3216 store(r, vtop - 1);
3217#ifndef TCC_TARGET_X86_64
3218 /* two word case handling : store second register at word + 4 */
3219 if ((ft & VT_BTYPE) == VT_LLONG) {
3220 vswap();
3221 /* convert to int to increment easily */
3222 vtop->type.t = VT_INT;
3223 gaddrof();
3224 vpushi(4);
3225 gen_op('+');
3226 vtop->r |= VT_LVAL;
3227 vswap();
3228 /* XXX: it works because r2 is spilled last ! */
3229 store(vtop->r2, vtop - 1);
3230 }
3231#endif
3232 }
3233 vswap();
3234 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
3235 vtop->r |= delayed_cast;
3236 }
3237}
3238
3239/* post defines POST/PRE add. c is the token ++ or -- */
3240void inc(int post, int c)
3241{
3242 test_lvalue();
3243 vdup(); /* save lvalue */
3244 if (post) {
3245 gv_dup(); /* duplicate value */
3246 vrotb(3);
3247 vrotb(3);
3248 }
3249 /* add constant */
3250 vpushi(c - TOK_MID);
3251 gen_op('+');
3252 vstore(); /* store value */
3253 if (post)
3254 vpop(); /* if post op, return saved value */
3255}
3256
3257/* Parse GNUC __attribute__ extension. Currently, the following
3258 extensions are recognized:
3259 - aligned(n) : set data/function alignment.
3260 - packed : force data alignment to 1
3261 - section(x) : generate data/code in this section.
3262 - unused : currently ignored, but may be used someday.
3263 - regparm(n) : pass function parameters in registers (i386 only)
3264 */
3265static void parse_attribute(AttributeDef *ad)
3266{
3267 int t, n;
3268
3269 while (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2) {
3270 next();
3271 skip('(');
3272 skip('(');
3273 while (tok != ')') {
3274 if (tok < TOK_IDENT)
3275 expect("attribute name");
3276 t = tok;
3277 next();
3278 switch(t) {
3279 case TOK_SECTION1:
3280 case TOK_SECTION2:
3281 skip('(');
3282 if (tok != TOK_STR)
3283 expect("section name");
3284 ad->section = find_section(tcc_state, (char *)tokc.cstr->data);
3285 next();
3286 skip(')');
3287 break;
3288 case TOK_ALIGNED1:
3289 case TOK_ALIGNED2:
3290 if (tok == '(') {
3291 next();
3292 n = expr_const();
3293 if (n <= 0 || (n & (n - 1)) != 0)
3294 error("alignment must be a positive power of two");
3295 skip(')');
3296 } else {
3297 n = MAX_ALIGN;
3298 }
3299 ad->aligned = n;
3300 break;
3301 case TOK_PACKED1:
3302 case TOK_PACKED2:
3303 ad->packed = 1;
3304 break;
3305 case TOK_UNUSED1:
3306 case TOK_UNUSED2:
3307 /* currently, no need to handle it because tcc does not
3308 track unused objects */
3309 break;
3310 case TOK_NORETURN1:
3311 case TOK_NORETURN2:
3312 /* currently, no need to handle it because tcc does not
3313 track unused objects */
3314 break;
3315 case TOK_CDECL1:
3316 case TOK_CDECL2:
3317 case TOK_CDECL3:
3318 FUNC_CALL(ad->func_attr) = FUNC_CDECL;
3319 break;
3320 case TOK_STDCALL1:
3321 case TOK_STDCALL2:
3322 case TOK_STDCALL3:
3323 FUNC_CALL(ad->func_attr) = FUNC_STDCALL;
3324 break;
3325#ifdef TCC_TARGET_I386
3326 case TOK_REGPARM1:
3327 case TOK_REGPARM2:
3328 skip('(');
3329 n = expr_const();
3330 if (n > 3)
3331 n = 3;
3332 else if (n < 0)
3333 n = 0;
3334 if (n > 0)
3335 FUNC_CALL(ad->func_attr) = FUNC_FASTCALL1 + n - 1;
3336 skip(')');
3337 break;
3338 case TOK_FASTCALL1:
3339 case TOK_FASTCALL2:
3340 case TOK_FASTCALL3:
3341 FUNC_CALL(ad->func_attr) = FUNC_FASTCALLW;
3342 break;
3343#endif
3344 case TOK_DLLEXPORT:
3345 FUNC_EXPORT(ad->func_attr) = 1;
3346 break;
3347 default:
3348 if (tcc_state->warn_unsupported)
3349 warning("'%s' attribute ignored", get_tok_str(t, NULL));
3350 /* skip parameters */
3351 if (tok == '(') {
3352 int parenthesis = 0;
3353 do {
3354 if (tok == '(')
3355 parenthesis++;
3356 else if (tok == ')')
3357 parenthesis--;
3358 next();
3359 } while (parenthesis && tok != -1);
3360 }
3361 break;
3362 }
3363 if (tok != ',')
3364 break;
3365 next();
3366 }
3367 skip(')');
3368 skip(')');
3369 }
3370}
3371
3372/* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
3373static void struct_decl(CType *type, int u)
3374{
3375 int a, v, size, align, maxalign, c, offset;
3376 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
3377 Sym *s, *ss, *ass, **ps;
3378 AttributeDef ad;
3379 CType type1, btype;
3380
3381 a = tok; /* save decl type */
3382 next();
3383 if (tok != '{') {
3384 v = tok;
3385 next();
3386 /* struct already defined ? return it */
3387 if (v < TOK_IDENT)
3388 expect("struct/union/enum name");
3389 s = struct_find(v);
3390 if (s) {
3391 if (s->type.t != a)
3392 error("invalid type");
3393 goto do_decl;
3394 }
3395 } else {
3396 v = anon_sym++;
3397 }
3398 type1.t = a;
3399 /* we put an undefined size for struct/union */
3400 s = sym_push(v | SYM_STRUCT, &type1, 0, -1);
3401 s->r = 0; /* default alignment is zero as gcc */
3402 /* put struct/union/enum name in type */
3403 do_decl:
3404 type->t = u;
3405 type->ref = s;
3406
3407 if (tok == '{') {
3408 next();
3409 if (s->c != -1)
3410 error("struct/union/enum already defined");
3411 /* cannot be empty */
3412 c = 0;
3413 /* non empty enums are not allowed */
3414 if (a == TOK_ENUM) {
3415 for(;;) {
3416 v = tok;
3417 if (v < TOK_UIDENT)
3418 expect("identifier");
3419 next();
3420 if (tok == '=') {
3421 next();
3422 c = expr_const();
3423 }
3424 /* enum symbols have static storage */
3425 ss = sym_push(v, &int_type, VT_CONST, c);
3426 ss->type.t |= VT_STATIC;
3427 if (tok != ',')
3428 break;
3429 next();
3430 c++;
3431 /* NOTE: we accept a trailing comma */
3432 if (tok == '}')
3433 break;
3434 }
3435 skip('}');
3436 } else {
3437 maxalign = 1;
3438 ps = &s->next;
3439 prevbt = VT_INT;
3440 bit_pos = 0;
3441 offset = 0;
3442 while (tok != '}') {
3443 parse_btype(&btype, &ad);
3444 while (1) {
3445 bit_size = -1;
3446 v = 0;
3447 type1 = btype;
3448 if (tok != ':') {
3449 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
3450 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
3451 expect("identifier");
3452 if ((type1.t & VT_BTYPE) == VT_FUNC ||
3453 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
3454 error("invalid type for '%s'",
3455 get_tok_str(v, NULL));
3456 }
3457 if (tok == ':') {
3458 next();
3459 bit_size = expr_const();
3460 /* XXX: handle v = 0 case for messages */
3461 if (bit_size < 0)
3462 error("negative width in bit-field '%s'",
3463 get_tok_str(v, NULL));
3464 if (v && bit_size == 0)
3465 error("zero width for bit-field '%s'",
3466 get_tok_str(v, NULL));
3467 }
3468 size = type_size(&type1, &align);
3469 if (ad.aligned) {
3470 if (align < ad.aligned)
3471 align = ad.aligned;
3472 } else if (ad.packed) {
3473 align = 1;
3474 } else if (*tcc_state->pack_stack_ptr) {
3475 if (align > *tcc_state->pack_stack_ptr)
3476 align = *tcc_state->pack_stack_ptr;
3477 }
3478 lbit_pos = 0;
3479 if (bit_size >= 0) {
3480 bt = type1.t & VT_BTYPE;
3481 if (bt != VT_INT &&
3482 bt != VT_BYTE &&
3483 bt != VT_SHORT &&
3484 bt != VT_BOOL &&
3485 bt != VT_ENUM &&
3486 bt != VT_LLONG)
3487 error("bitfields must have scalar type");
3488 bsize = size * 8;
3489 if (bit_size > bsize) {
3490 error("width of '%s' exceeds its type",
3491 get_tok_str(v, NULL));
3492 } else if (bit_size == bsize) {
3493 /* no need for bit fields */
3494 bit_pos = 0;
3495 } else if (bit_size == 0) {
3496 /* XXX: what to do if only padding in a
3497 structure ? */
3498 /* zero size: means to pad */
3499 bit_pos = 0;
3500 } else {
3501 /* we do not have enough room ?
3502 did the type change?
3503 is it a union? */
3504 if ((bit_pos + bit_size) > bsize ||
3505 bt != prevbt || a == TOK_UNION)
3506 bit_pos = 0;
3507 lbit_pos = bit_pos;
3508 /* XXX: handle LSB first */
3509 type1.t |= VT_BITFIELD |
3510 (bit_pos << VT_STRUCT_SHIFT) |
3511 (bit_size << (VT_STRUCT_SHIFT + 6));
3512 bit_pos += bit_size;
3513 }
3514 prevbt = bt;
3515 } else {
3516 bit_pos = 0;
3517 }
3518 if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
3519 /* add new memory data only if starting
3520 bit field */
3521 if (lbit_pos == 0) {
3522 if (a == TOK_STRUCT) {
3523 c = (c + align - 1) & -align;
3524 offset = c;
3525 if (size > 0)
3526 c += size;
3527 } else {
3528 offset = 0;
3529 if (size > c)
3530 c = size;
3531 }
3532 if (align > maxalign)
3533 maxalign = align;
3534 }
3535#if 0
3536 printf("add field %s offset=%d",
3537 get_tok_str(v, NULL), offset);
3538 if (type1.t & VT_BITFIELD) {
3539 printf(" pos=%d size=%d",
3540 (type1.t >> VT_STRUCT_SHIFT) & 0x3f,
3541 (type1.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f);
3542 }
3543 printf("\n");
3544#endif
3545 }
3546 if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
3547 ass = type1.ref;
3548 while ((ass = ass->next) != NULL) {
3549 ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
3550 *ps = ss;
3551 ps = &ss->next;
3552 }
3553 } else if (v) {
3554 ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
3555 *ps = ss;
3556 ps = &ss->next;
3557 }
3558 if (tok == ';' || tok == TOK_EOF)
3559 break;
3560 skip(',');
3561 }
3562 skip(';');
3563 }
3564 skip('}');
3565 /* store size and alignment */
3566 s->c = (c + maxalign - 1) & -maxalign;
3567 s->r = maxalign;
3568 }
3569 }
3570}
3571
3572/* return 0 if no type declaration. otherwise, return the basic type
3573 and skip it.
3574 */
3575static int parse_btype(CType *type, AttributeDef *ad)
3576{
3577 int t, u, type_found, typespec_found, typedef_found;
3578 Sym *s;
3579 CType type1;
3580
3581 memset(ad, 0, sizeof(AttributeDef));
3582 type_found = 0;
3583 typespec_found = 0;
3584 typedef_found = 0;
3585 t = 0;
3586 while(1) {
3587 switch(tok) {
3588 case TOK_EXTENSION:
3589 /* currently, we really ignore extension */
3590 next();
3591 continue;
3592
3593 /* basic types */
3594 case TOK_CHAR:
3595 u = VT_BYTE;
3596 basic_type:
3597 next();
3598 basic_type1:
3599 if ((t & VT_BTYPE) != 0)
3600 error("too many basic types");
3601 t |= u;
3602 typespec_found = 1;
3603 break;
3604 case TOK_VOID:
3605 u = VT_VOID;
3606 goto basic_type;
3607 case TOK_SHORT:
3608 u = VT_SHORT;
3609 goto basic_type;
3610 case TOK_INT:
3611 next();
3612 typespec_found = 1;
3613 break;
3614 case TOK_LONG:
3615 next();
3616 if ((t & VT_BTYPE) == VT_DOUBLE) {
3617 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3618 } else if ((t & VT_BTYPE) == VT_LONG) {
3619 t = (t & ~VT_BTYPE) | VT_LLONG;
3620 } else {
3621 u = VT_LONG;
3622 goto basic_type1;
3623 }
3624 break;
3625 case TOK_BOOL:
3626 u = VT_BOOL;
3627 goto basic_type;
3628 case TOK_FLOAT:
3629 u = VT_FLOAT;
3630 goto basic_type;
3631 case TOK_DOUBLE:
3632 next();
3633 if ((t & VT_BTYPE) == VT_LONG) {
3634 t = (t & ~VT_BTYPE) | VT_LDOUBLE;
3635 } else {
3636 u = VT_DOUBLE;
3637 goto basic_type1;
3638 }
3639 break;
3640 case TOK_ENUM:
3641 struct_decl(&type1, VT_ENUM);
3642 basic_type2:
3643 u = type1.t;
3644 type->ref = type1.ref;
3645 goto basic_type1;
3646 case TOK_STRUCT:
3647 case TOK_UNION:
3648 struct_decl(&type1, VT_STRUCT);
3649 goto basic_type2;
3650
3651 /* type modifiers */
3652 case TOK_CONST1:
3653 case TOK_CONST2:
3654 case TOK_CONST3:
3655 t |= VT_CONSTANT;
3656 next();
3657 break;
3658 case TOK_VOLATILE1:
3659 case TOK_VOLATILE2:
3660 case TOK_VOLATILE3:
3661 t |= VT_VOLATILE;
3662 next();
3663 break;
3664 case TOK_SIGNED1:
3665 case TOK_SIGNED2:
3666 case TOK_SIGNED3:
3667 typespec_found = 1;
3668 t |= VT_SIGNED;
3669 next();
3670 break;
3671 case TOK_REGISTER:
3672 case TOK_AUTO:
3673 case TOK_RESTRICT1:
3674 case TOK_RESTRICT2:
3675 case TOK_RESTRICT3:
3676 next();
3677 break;
3678 case TOK_UNSIGNED:
3679 t |= VT_UNSIGNED;
3680 next();
3681 typespec_found = 1;
3682 break;
3683
3684 /* storage */
3685 case TOK_EXTERN:
3686 t |= VT_EXTERN;
3687 next();
3688 break;
3689 case TOK_STATIC:
3690 t |= VT_STATIC;
3691 next();
3692 break;
3693 case TOK_TYPEDEF:
3694 t |= VT_TYPEDEF;
3695 next();
3696 break;
3697 case TOK_INLINE1:
3698 case TOK_INLINE2:
3699 case TOK_INLINE3:
3700 t |= VT_INLINE;
3701 next();
3702 break;
3703
3704 /* GNUC attribute */
3705 case TOK_ATTRIBUTE1:
3706 case TOK_ATTRIBUTE2:
3707 parse_attribute(ad);
3708 break;
3709 /* GNUC typeof */
3710 case TOK_TYPEOF1:
3711 case TOK_TYPEOF2:
3712 case TOK_TYPEOF3:
3713 next();
3714 parse_expr_type(&type1);
3715 goto basic_type2;
3716 default:
3717 if (typespec_found || typedef_found)
3718 goto the_end;
3719 s = sym_find(tok);
3720 if (!s || !(s->type.t & VT_TYPEDEF))
3721 goto the_end;
3722 typedef_found = 1;
3723 t |= (s->type.t & ~VT_TYPEDEF);
3724 type->ref = s->type.ref;
3725 next();
3726 typespec_found = 1;
3727 break;
3728 }
3729 type_found = 1;
3730 }
3731the_end:
3732 if ((t & (VT_SIGNED|VT_UNSIGNED)) == (VT_SIGNED|VT_UNSIGNED))
3733 error("signed and unsigned modifier");
3734 if (tcc_state->char_is_unsigned) {
3735 if ((t & (VT_SIGNED|VT_UNSIGNED|VT_BTYPE)) == VT_BYTE)
3736 t |= VT_UNSIGNED;
3737 }
3738 t &= ~VT_SIGNED;
3739
3740 /* long is never used as type */
3741 if ((t & VT_BTYPE) == VT_LONG)
3742#ifndef TCC_TARGET_X86_64
3743 t = (t & ~VT_BTYPE) | VT_INT;
3744#else
3745 t = (t & ~VT_BTYPE) | VT_LLONG;
3746#endif
3747 type->t = t;
3748 return type_found;
3749}
3750
3751/* convert a function parameter type (array to pointer and function to
3752 function pointer) */
3753static inline void convert_parameter_type(CType *pt)
3754{
3755 /* remove const and volatile qualifiers (XXX: const could be used
3756 to indicate a const function parameter */
3757 pt->t &= ~(VT_CONSTANT | VT_VOLATILE);
3758 /* array must be transformed to pointer according to ANSI C */
3759 pt->t &= ~VT_ARRAY;
3760 if ((pt->t & VT_BTYPE) == VT_FUNC) {
3761 mk_pointer(pt);
3762 }
3763}
3764
3765static void post_type(CType *type, AttributeDef *ad)
3766{
3767 int n, l, t1, arg_size, align;
3768 Sym **plast, *s, *first;
3769 AttributeDef ad1;
3770 CType pt;
3771
3772 if (tok == '(') {
3773 /* function declaration */
3774 next();
3775 l = 0;
3776 first = NULL;
3777 plast = &first;
3778 arg_size = 0;
3779 if (tok != ')') {
3780 for(;;) {
3781 /* read param name and compute offset */
3782 if (l != FUNC_OLD) {
3783 if (!parse_btype(&pt, &ad1)) {
3784 if (l) {
3785 error("invalid type");
3786 } else {
3787 l = FUNC_OLD;
3788 goto old_proto;
3789 }
3790 }
3791 l = FUNC_NEW;
3792 if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
3793 break;
3794 type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
3795 if ((pt.t & VT_BTYPE) == VT_VOID)
3796 error("parameter declared as void");
3797 arg_size += (type_size(&pt, &align) + 3) & ~3;
3798 } else {
3799 old_proto:
3800 n = tok;
3801 if (n < TOK_UIDENT)
3802 expect("identifier");
3803 pt.t = VT_INT;
3804 next();
3805 }
3806 convert_parameter_type(&pt);
3807 s = sym_push(n | SYM_FIELD, &pt, 0, 0);
3808 *plast = s;
3809 plast = &s->next;
3810 if (tok == ')')
3811 break;
3812 skip(',');
3813 if (l == FUNC_NEW && tok == TOK_DOTS) {
3814 l = FUNC_ELLIPSIS;
3815 next();
3816 break;
3817 }
3818 }
3819 }
3820 /* if no parameters, then old type prototype */
3821 if (l == 0)
3822 l = FUNC_OLD;
3823 skip(')');
3824 t1 = type->t & VT_STORAGE;
3825 /* NOTE: const is ignored in returned type as it has a special
3826 meaning in gcc / C++ */
3827 type->t &= ~(VT_STORAGE | VT_CONSTANT);
3828 post_type(type, ad);
3829 /* we push a anonymous symbol which will contain the function prototype */
3830 FUNC_ARGS(ad->func_attr) = arg_size;
3831 s = sym_push(SYM_FIELD, type, ad->func_attr, l);
3832 s->next = first;
3833 type->t = t1 | VT_FUNC;
3834 type->ref = s;
3835 } else if (tok == '[') {
3836 /* array definition */
3837 next();
3838 if (tok == TOK_RESTRICT1)
3839 next();
3840 n = -1;
3841 if (tok != ']') {
3842 n = expr_const();
3843 if (n < 0)
3844 error("invalid array size");
3845 }
3846 skip(']');
3847 /* parse next post type */
3848 t1 = type->t & VT_STORAGE;
3849 type->t &= ~VT_STORAGE;
3850 post_type(type, ad);
3851
3852 /* we push a anonymous symbol which will contain the array
3853 element type */
3854 s = sym_push(SYM_FIELD, type, 0, n);
3855 type->t = t1 | VT_ARRAY | VT_PTR;
3856 type->ref = s;
3857 }
3858}
3859
3860/* Parse a type declaration (except basic type), and return the type
3861 in 'type'. 'td' is a bitmask indicating which kind of type decl is
3862 expected. 'type' should contain the basic type. 'ad' is the
3863 attribute definition of the basic type. It can be modified by
3864 type_decl().
3865 */
3866static void type_decl(CType *type, AttributeDef *ad, int *v, int td)
3867{
3868 Sym *s;
3869 CType type1, *type2;
3870 int qualifiers;
3871
3872 while (tok == '*') {
3873 qualifiers = 0;
3874 redo:
3875 next();
3876 switch(tok) {
3877 case TOK_CONST1:
3878 case TOK_CONST2:
3879 case TOK_CONST3:
3880 qualifiers |= VT_CONSTANT;
3881 goto redo;
3882 case TOK_VOLATILE1:
3883 case TOK_VOLATILE2:
3884 case TOK_VOLATILE3:
3885 qualifiers |= VT_VOLATILE;
3886 goto redo;
3887 case TOK_RESTRICT1:
3888 case TOK_RESTRICT2:
3889 case TOK_RESTRICT3:
3890 goto redo;
3891 }
3892 mk_pointer(type);
3893 type->t |= qualifiers;
3894 }
3895
3896 /* XXX: clarify attribute handling */
3897 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3898 parse_attribute(ad);
3899
3900 /* recursive type */
3901 /* XXX: incorrect if abstract type for functions (e.g. 'int ()') */
3902 type1.t = 0; /* XXX: same as int */
3903 if (tok == '(') {
3904 next();
3905 /* XXX: this is not correct to modify 'ad' at this point, but
3906 the syntax is not clear */
3907 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3908 parse_attribute(ad);
3909 type_decl(&type1, ad, v, td);
3910 skip(')');
3911 } else {
3912 /* type identifier */
3913 if (tok >= TOK_IDENT && (td & TYPE_DIRECT)) {
3914 *v = tok;
3915 next();
3916 } else {
3917 if (!(td & TYPE_ABSTRACT))
3918 expect("identifier");
3919 *v = 0;
3920 }
3921 }
3922 post_type(type, ad);
3923 if (tok == TOK_ATTRIBUTE1 || tok == TOK_ATTRIBUTE2)
3924 parse_attribute(ad);
3925 if (!type1.t)
3926 return;
3927 /* append type at the end of type1 */
3928 type2 = &type1;
3929 for(;;) {
3930 s = type2->ref;
3931 type2 = &s->type;
3932 if (!type2->t) {
3933 *type2 = *type;
3934 break;
3935 }
3936 }
3937 *type = type1;
3938}
3939
3940/* compute the lvalue VT_LVAL_xxx needed to match type t. */
3941static int lvalue_type(int t)
3942{
3943 int bt, r;
3944 r = VT_LVAL;
3945 bt = t & VT_BTYPE;
3946 if (bt == VT_BYTE || bt == VT_BOOL)
3947 r |= VT_LVAL_BYTE;
3948 else if (bt == VT_SHORT)
3949 r |= VT_LVAL_SHORT;
3950 else
3951 return r;
3952 if (t & VT_UNSIGNED)
3953 r |= VT_LVAL_UNSIGNED;
3954 return r;
3955}
3956
3957/* indirection with full error checking and bound check */
3958static void indir(void)
3959{
3960 if ((vtop->type.t & VT_BTYPE) != VT_PTR) {
3961 if ((vtop->type.t & VT_BTYPE) == VT_FUNC)
3962 return;
3963 expect("pointer");
3964 }
3965 if ((vtop->r & VT_LVAL) && !nocode_wanted)
3966 gv(RC_INT);
3967 vtop->type = *pointed_type(&vtop->type);
3968 /* Arrays and functions are never lvalues */
3969 if (!(vtop->type.t & VT_ARRAY)
3970 && (vtop->type.t & VT_BTYPE) != VT_FUNC) {
3971 vtop->r |= lvalue_type(vtop->type.t);
3972 /* if bound checking, the referenced pointer must be checked */
3973 if (do_bounds_check)
3974 vtop->r |= VT_MUSTBOUND;
3975 }
3976}
3977
3978/* pass a parameter to a function and do type checking and casting */
3979static void gfunc_param_typed(Sym *func, Sym *arg)
3980{
3981 int func_type;
3982 CType type;
3983
3984 func_type = func->c;
3985 if (func_type == FUNC_OLD ||
3986 (func_type == FUNC_ELLIPSIS && arg == NULL)) {
3987 /* default casting : only need to convert float to double */
3988 if ((vtop->type.t & VT_BTYPE) == VT_FLOAT) {
3989 type.t = VT_DOUBLE;
3990 gen_cast(&type);
3991 }
3992 } else if (arg == NULL) {
3993 error("too many arguments to function");
3994 } else {
3995 type = arg->type;
3996 type.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
3997 gen_assign_cast(&type);
3998 }
3999}
4000
4001/* parse an expression of the form '(type)' or '(expr)' and return its
4002 type */
4003static void parse_expr_type(CType *type)
4004{
4005 int n;
4006 AttributeDef ad;
4007
4008 skip('(');
4009 if (parse_btype(type, &ad)) {
4010 type_decl(type, &ad, &n, TYPE_ABSTRACT);
4011 } else {
4012 expr_type(type);
4013 }
4014 skip(')');
4015}
4016
4017static void parse_type(CType *type)
4018{
4019 AttributeDef ad;
4020 int n;
4021
4022 if (!parse_btype(type, &ad)) {
4023 expect("type");
4024 }
4025 type_decl(type, &ad, &n, TYPE_ABSTRACT);
4026}
4027
4028static void vpush_tokc(int t)
4029{
4030 CType type;
4031 type.t = t;
4032 vsetc(&type, VT_CONST, &tokc);
4033}
4034
4035static void unary(void)
4036{
4037 int n, t, align, size, r;
4038 CType type;
4039 Sym *s;
4040 AttributeDef ad;
4041
4042 /* XXX: GCC 2.95.3 does not generate a table although it should be
4043 better here */
4044 tok_next:
4045 switch(tok) {
4046 case TOK_EXTENSION:
4047 next();
4048 goto tok_next;
4049 case TOK_CINT:
4050 case TOK_CCHAR:
4051 case TOK_LCHAR:
4052 vpushi(tokc.i);
4053 next();
4054 break;
4055 case TOK_CUINT:
4056 vpush_tokc(VT_INT | VT_UNSIGNED);
4057 next();
4058 break;
4059 case TOK_CLLONG:
4060 vpush_tokc(VT_LLONG);
4061 next();
4062 break;
4063 case TOK_CULLONG:
4064 vpush_tokc(VT_LLONG | VT_UNSIGNED);
4065 next();
4066 break;
4067 case TOK_CFLOAT:
4068 vpush_tokc(VT_FLOAT);
4069 next();
4070 break;
4071 case TOK_CDOUBLE:
4072 vpush_tokc(VT_DOUBLE);
4073 next();
4074 break;
4075 case TOK_CLDOUBLE:
4076 vpush_tokc(VT_LDOUBLE);
4077 next();
4078 break;
4079 case TOK___FUNCTION__:
4080 if (!gnu_ext)
4081 goto tok_identifier;
4082 /* fall thru */
4083 case TOK___FUNC__:
4084 {
4085 void *ptr;
4086 int len;
4087 /* special function name identifier */
4088 len = strlen(funcname) + 1;
4089 /* generate char[len] type */
4090 type.t = VT_BYTE;
4091 mk_pointer(&type);
4092 type.t |= VT_ARRAY;
4093 type.ref->c = len;
4094 vpush_ref(&type, data_section, data_section->data_offset, len);
4095 ptr = section_ptr_add(data_section, len);
4096 memcpy(ptr, funcname, len);
4097 next();
4098 }
4099 break;
4100 case TOK_LSTR:
4101#ifdef TCC_TARGET_PE
4102 t = VT_SHORT | VT_UNSIGNED;
4103#else
4104 t = VT_INT;
4105#endif
4106 goto str_init;
4107 case TOK_STR:
4108 /* string parsing */
4109 t = VT_BYTE;
4110 str_init:
4111 if (tcc_state->warn_write_strings)
4112 t |= VT_CONSTANT;
4113 type.t = t;
4114 mk_pointer(&type);
4115 type.t |= VT_ARRAY;
4116 memset(&ad, 0, sizeof(AttributeDef));
4117 decl_initializer_alloc(&type, &ad, VT_CONST, 2, 0, 0);
4118 break;
4119 case '(':
4120 next();
4121 /* cast ? */
4122 if (parse_btype(&type, &ad)) {
4123 type_decl(&type, &ad, &n, TYPE_ABSTRACT);
4124 skip(')');
4125 /* check ISOC99 compound literal */
4126 if (tok == '{') {
4127 /* data is allocated locally by default */
4128 if (global_expr)
4129 r = VT_CONST;
4130 else
4131 r = VT_LOCAL;
4132 /* all except arrays are lvalues */
4133 if (!(type.t & VT_ARRAY))
4134 r |= lvalue_type(type.t);
4135 memset(&ad, 0, sizeof(AttributeDef));
4136 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
4137 } else {
4138 unary();
4139 gen_cast(&type);
4140 }
4141 } else if (tok == '{') {
4142 /* save all registers */
4143 save_regs(0);
4144 /* statement expression : we do not accept break/continue
4145 inside as GCC does */
4146 block(NULL, NULL, NULL, NULL, 0, 1);
4147 skip(')');
4148 } else {
4149 gexpr();
4150 skip(')');
4151 }
4152 break;
4153 case '*':
4154 next();
4155 unary();
4156 indir();
4157 break;
4158 case '&':
4159 next();
4160 unary();
4161 /* functions names must be treated as function pointers,
4162 except for unary '&' and sizeof. Since we consider that
4163 functions are not lvalues, we only have to handle it
4164 there and in function calls. */
4165 /* arrays can also be used although they are not lvalues */
4166 if ((vtop->type.t & VT_BTYPE) != VT_FUNC &&
4167 !(vtop->type.t & VT_ARRAY) && !(vtop->type.t & VT_LLOCAL))
4168 test_lvalue();
4169 mk_pointer(&vtop->type);
4170 gaddrof();
4171 break;
4172 case '!':
4173 next();
4174 unary();
4175 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST) {
4176 CType boolean;
4177 boolean.t = VT_BOOL;
4178 gen_cast(&boolean);
4179 vtop->c.i = !vtop->c.i;
4180 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
4181 vtop->c.i = vtop->c.i ^ 1;
4182 else {
4183 save_regs(1);
4184 vseti(VT_JMP, gtst(1, 0));
4185 }
4186 break;
4187 case '~':
4188 next();
4189 unary();
4190 vpushi(-1);
4191 gen_op('^');
4192 break;
4193 case '+':
4194 next();
4195 /* in order to force cast, we add zero */
4196 unary();
4197 if ((vtop->type.t & VT_BTYPE) == VT_PTR)
4198 error("pointer not accepted for unary plus");
4199 vpushi(0);
4200 gen_op('+');
4201 break;
4202 case TOK_SIZEOF:
4203 case TOK_ALIGNOF1:
4204 case TOK_ALIGNOF2:
4205 t = tok;
4206 next();
4207 if (tok == '(') {
4208 parse_expr_type(&type);
4209 } else {
4210 unary_type(&type);
4211 }
4212 size = type_size(&type, &align);
4213 if (t == TOK_SIZEOF) {
4214 if (size < 0)
4215 error("sizeof applied to an incomplete type");
4216 vpushi(size);
4217 } else {
4218 vpushi(align);
4219 }
4220 vtop->type.t |= VT_UNSIGNED;
4221 break;
4222
4223 case TOK_builtin_types_compatible_p:
4224 {
4225 CType type1, type2;
4226 next();
4227 skip('(');
4228 parse_type(&type1);
4229 skip(',');
4230 parse_type(&type2);
4231 skip(')');
4232 type1.t &= ~(VT_CONSTANT | VT_VOLATILE);
4233 type2.t &= ~(VT_CONSTANT | VT_VOLATILE);
4234 vpushi(is_compatible_types(&type1, &type2));
4235 }
4236 break;
4237 case TOK_builtin_constant_p:
4238 {
4239 int saved_nocode_wanted, res;
4240 next();
4241 skip('(');
4242 saved_nocode_wanted = nocode_wanted;
4243 nocode_wanted = 1;
4244 gexpr();
4245 res = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
4246 vpop();
4247 nocode_wanted = saved_nocode_wanted;
4248 skip(')');
4249 vpushi(res);
4250 }
4251 break;
4252 case TOK_builtin_frame_address:
4253 {
4254 CType type;
4255 next();
4256 skip('(');
4257 if (tok != TOK_CINT) {
4258 error("__builtin_frame_address only takes integers");
4259 }
4260 if (tokc.i != 0) {
4261 error("TCC only supports __builtin_frame_address(0)");
4262 }
4263 next();
4264 skip(')');
4265 type.t = VT_VOID;
4266 mk_pointer(&type);
4267 vset(&type, VT_LOCAL, 0);
4268 }
4269 break;
4270#ifdef TCC_TARGET_X86_64
4271 case TOK_builtin_malloc:
4272 tok = TOK_malloc;
4273 goto tok_identifier;
4274 case TOK_builtin_free:
4275 tok = TOK_free;
4276 goto tok_identifier;
4277#endif
4278 case TOK_INC:
4279 case TOK_DEC:
4280 t = tok;
4281 next();
4282 unary();
4283 inc(0, t);
4284 break;
4285 case '-':
4286 next();
4287 vpushi(0);
4288 unary();
4289 gen_op('-');
4290 break;
4291 case TOK_LAND:
4292 if (!gnu_ext)
4293 goto tok_identifier;
4294 next();
4295 /* allow to take the address of a label */
4296 if (tok < TOK_UIDENT)
4297 expect("label identifier");
4298 s = label_find(tok);
4299 if (!s) {
4300 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
4301 } else {
4302 if (s->r == LABEL_DECLARED)
4303 s->r = LABEL_FORWARD;
4304 }
4305 if (!s->type.t) {
4306 s->type.t = VT_VOID;
4307 mk_pointer(&s->type);
4308 s->type.t |= VT_STATIC;
4309 }
4310 vset(&s->type, VT_CONST | VT_SYM, 0);
4311 vtop->sym = s;
4312 next();
4313 break;
4314 default:
4315 tok_identifier:
4316 t = tok;
4317 next();
4318 if (t < TOK_UIDENT)
4319 expect("identifier");
4320 s = sym_find(t);
4321 if (!s) {
4322 if (tok != '(')
4323 error("'%s' undeclared", get_tok_str(t, NULL));
4324 /* for simple function calls, we tolerate undeclared
4325 external reference to int() function */
4326 if (tcc_state->warn_implicit_function_declaration)
4327 warning("implicit declaration of function '%s'",
4328 get_tok_str(t, NULL));
4329 s = external_global_sym(t, &func_old_type, 0);
4330 }
4331 if ((s->type.t & (VT_STATIC | VT_INLINE | VT_BTYPE)) ==
4332 (VT_STATIC | VT_INLINE | VT_FUNC)) {
4333 /* if referencing an inline function, then we generate a
4334 symbol to it if not already done. It will have the
4335 effect to generate code for it at the end of the
4336 compilation unit. Inline function as always
4337 generated in the text section. */
4338 if (!s->c)
4339 put_extern_sym(s, text_section, 0, 0);
4340 r = VT_SYM | VT_CONST;
4341 } else {
4342 r = s->r;
4343 }
4344 vset(&s->type, r, s->c);
4345 /* if forward reference, we must point to s */
4346 if (vtop->r & VT_SYM) {
4347 vtop->sym = s;
4348 vtop->c.ul = 0;
4349 }
4350 break;
4351 }
4352
4353 /* post operations */
4354 while (1) {
4355 if (tok == TOK_INC || tok == TOK_DEC) {
4356 inc(1, tok);
4357 next();
4358 } else if (tok == '.' || tok == TOK_ARROW) {
4359 /* field */
4360 if (tok == TOK_ARROW)
4361 indir();
4362 test_lvalue();
4363 gaddrof();
4364 next();
4365 /* expect pointer on structure */
4366 if ((vtop->type.t & VT_BTYPE) != VT_STRUCT)
4367 expect("struct or union");
4368 s = vtop->type.ref;
4369 /* find field */
4370 tok |= SYM_FIELD;
4371 while ((s = s->next) != NULL) {
4372 if (s->v == tok)
4373 break;
4374 }
4375 if (!s)
4376 error("field not found: %s", get_tok_str(tok & ~SYM_FIELD, NULL));
4377 /* add field offset to pointer */
4378 vtop->type = char_pointer_type; /* change type to 'char *' */
4379 vpushi(s->c);
4380 gen_op('+');
4381 /* change type to field type, and set to lvalue */
4382 vtop->type = s->type;
4383 /* an array is never an lvalue */
4384 if (!(vtop->type.t & VT_ARRAY)) {
4385 vtop->r |= lvalue_type(vtop->type.t);
4386 /* if bound checking, the referenced pointer must be checked */
4387 if (do_bounds_check)
4388 vtop->r |= VT_MUSTBOUND;
4389 }
4390 next();
4391 } else if (tok == '[') {
4392 next();
4393 gexpr();
4394 gen_op('+');
4395 indir();
4396 skip(']');
4397 } else if (tok == '(') {
4398 SValue ret;
4399 Sym *sa;
4400 int nb_args;
4401
4402 /* function call */
4403 if ((vtop->type.t & VT_BTYPE) != VT_FUNC) {
4404 /* pointer test (no array accepted) */
4405 if ((vtop->type.t & (VT_BTYPE | VT_ARRAY)) == VT_PTR) {
4406 vtop->type = *pointed_type(&vtop->type);
4407 if ((vtop->type.t & VT_BTYPE) != VT_FUNC)
4408 goto error_func;
4409 } else {
4410 error_func:
4411 expect("function pointer");
4412 }
4413 } else {
4414 vtop->r &= ~VT_LVAL; /* no lvalue */
4415 }
4416 /* get return type */
4417 s = vtop->type.ref;
4418 next();
4419 sa = s->next; /* first parameter */
4420 nb_args = 0;
4421 ret.r2 = VT_CONST;
4422 /* compute first implicit argument if a structure is returned */
4423 if ((s->type.t & VT_BTYPE) == VT_STRUCT) {
4424 /* get some space for the returned structure */
4425 size = type_size(&s->type, &align);
4426 loc = (loc - size) & -align;
4427 ret.type = s->type;
4428 ret.r = VT_LOCAL | VT_LVAL;
4429 /* pass it as 'int' to avoid structure arg passing
4430 problems */
4431 vseti(VT_LOCAL, loc);
4432 ret.c = vtop->c;
4433 nb_args++;
4434 } else {
4435 ret.type = s->type;
4436 /* return in register */
4437 if (is_float(ret.type.t)) {
4438 ret.r = reg_fret(ret.type.t);
4439 } else {
4440 if ((ret.type.t & VT_BTYPE) == VT_LLONG)
4441 ret.r2 = REG_LRET;
4442 ret.r = REG_IRET;
4443 }
4444 ret.c.i = 0;
4445 }
4446 if (tok != ')') {
4447 for(;;) {
4448 expr_eq();
4449 gfunc_param_typed(s, sa);
4450 nb_args++;
4451 if (sa)
4452 sa = sa->next;
4453 if (tok == ')')
4454 break;
4455 skip(',');
4456 }
4457 }
4458 if (sa)
4459 error("too few arguments to function");
4460 skip(')');
4461 if (!nocode_wanted) {
4462 gfunc_call(nb_args);
4463 } else {
4464 vtop -= (nb_args + 1);
4465 }
4466 /* return value */
4467 vsetc(&ret.type, ret.r, &ret.c);
4468 vtop->r2 = ret.r2;
4469 } else {
4470 break;
4471 }
4472 }
4473}
4474
4475static void uneq(void)
4476{
4477 int t;
4478
4479 unary();
4480 if (tok == '=' ||
4481 (tok >= TOK_A_MOD && tok <= TOK_A_DIV) ||
4482 tok == TOK_A_XOR || tok == TOK_A_OR ||
4483 tok == TOK_A_SHL || tok == TOK_A_SAR) {
4484 test_lvalue();
4485 t = tok;
4486 next();
4487 if (t == '=') {
4488 expr_eq();
4489 } else {
4490 vdup();
4491 expr_eq();
4492 gen_op(t & 0x7f);
4493 }
4494 vstore();
4495 }
4496}
4497
4498static void expr_prod(void)
4499{
4500 int t;
4501
4502 uneq();
4503 while (tok == '*' || tok == '/' || tok == '%') {
4504 t = tok;
4505 next();
4506 uneq();
4507 gen_op(t);
4508 }
4509}
4510
4511static void expr_sum(void)
4512{
4513 int t;
4514
4515 expr_prod();
4516 while (tok == '+' || tok == '-') {
4517 t = tok;
4518 next();
4519 expr_prod();
4520 gen_op(t);
4521 }
4522}
4523
4524static void expr_shift(void)
4525{
4526 int t;
4527
4528 expr_sum();
4529 while (tok == TOK_SHL || tok == TOK_SAR) {
4530 t = tok;
4531 next();
4532 expr_sum();
4533 gen_op(t);
4534 }
4535}
4536
4537static void expr_cmp(void)
4538{
4539 int t;
4540
4541 expr_shift();
4542 while ((tok >= TOK_ULE && tok <= TOK_GT) ||
4543 tok == TOK_ULT || tok == TOK_UGE) {
4544 t = tok;
4545 next();
4546 expr_shift();
4547 gen_op(t);
4548 }
4549}
4550
4551static void expr_cmpeq(void)
4552{
4553 int t;
4554
4555 expr_cmp();
4556 while (tok == TOK_EQ || tok == TOK_NE) {
4557 t = tok;
4558 next();
4559 expr_cmp();
4560 gen_op(t);
4561 }
4562}
4563
4564static void expr_and(void)
4565{
4566 expr_cmpeq();
4567 while (tok == '&') {
4568 next();
4569 expr_cmpeq();
4570 gen_op('&');
4571 }
4572}
4573
4574static void expr_xor(void)
4575{
4576 expr_and();
4577 while (tok == '^') {
4578 next();
4579 expr_and();
4580 gen_op('^');
4581 }
4582}
4583
4584static void expr_or(void)
4585{
4586 expr_xor();
4587 while (tok == '|') {
4588 next();
4589 expr_xor();
4590 gen_op('|');
4591 }
4592}
4593
4594/* XXX: fix this mess */
4595static void expr_land_const(void)
4596{
4597 expr_or();
4598 while (tok == TOK_LAND) {
4599 next();
4600 expr_or();
4601 gen_op(TOK_LAND);
4602 }
4603}
4604
4605/* XXX: fix this mess */
4606static void expr_lor_const(void)
4607{
4608 expr_land_const();
4609 while (tok == TOK_LOR) {
4610 next();
4611 expr_land_const();
4612 gen_op(TOK_LOR);
4613 }
4614}
4615
4616/* only used if non constant */
4617static void expr_land(void)
4618{
4619 int t;
4620
4621 expr_or();
4622 if (tok == TOK_LAND) {
4623 t = 0;
4624 save_regs(1);
4625 for(;;) {
4626 t = gtst(1, t);
4627 if (tok != TOK_LAND) {
4628 vseti(VT_JMPI, t);
4629 break;
4630 }
4631 next();
4632 expr_or();
4633 }
4634 }
4635}
4636
4637static void expr_lor(void)
4638{
4639 int t;
4640
4641 expr_land();
4642 if (tok == TOK_LOR) {
4643 t = 0;
4644 save_regs(1);
4645 for(;;) {
4646 t = gtst(0, t);
4647 if (tok != TOK_LOR) {
4648 vseti(VT_JMP, t);
4649 break;
4650 }
4651 next();
4652 expr_land();
4653 }
4654 }
4655}
4656
4657/* XXX: better constant handling */
4658static void expr_eq(void)
4659{
4660 int tt, u, r1, r2, rc, t1, t2, bt1, bt2;
4661 SValue sv;
4662 CType type, type1, type2;
4663
4664 if (const_wanted) {
4665 expr_lor_const();
4666 if (tok == '?') {
4667 CType boolean;
4668 int c;
4669 boolean.t = VT_BOOL;
4670 vdup();
4671 gen_cast(&boolean);
4672 c = vtop->c.i;
4673 vpop();
4674 next();
4675 if (tok != ':' || !gnu_ext) {
4676 vpop();
4677 gexpr();
4678 }
4679 if (!c)
4680 vpop();
4681 skip(':');
4682 expr_eq();
4683 if (c)
4684 vpop();
4685 }
4686 } else {
4687 expr_lor();
4688 if (tok == '?') {
4689 next();
4690 if (vtop != vstack) {
4691 /* needed to avoid having different registers saved in
4692 each branch */
4693 if (is_float(vtop->type.t)) {
4694 rc = RC_FLOAT;
4695#ifdef TCC_TARGET_X86_64
4696 if ((vtop->type.t & VT_BTYPE) == VT_LDOUBLE) {
4697 rc = RC_ST0;
4698 }
4699#endif
4700 }
4701 else
4702 rc = RC_INT;
4703 gv(rc);
4704 save_regs(1);
4705 }
4706 if (tok == ':' && gnu_ext) {
4707 gv_dup();
4708 tt = gtst(1, 0);
4709 } else {
4710 tt = gtst(1, 0);
4711 gexpr();
4712 }
4713 type1 = vtop->type;
4714 sv = *vtop; /* save value to handle it later */
4715 vtop--; /* no vpop so that FP stack is not flushed */
4716 skip(':');
4717 u = gjmp(0);
4718 gsym(tt);
4719 expr_eq();
4720 type2 = vtop->type;
4721
4722 t1 = type1.t;
4723 bt1 = t1 & VT_BTYPE;
4724 t2 = type2.t;
4725 bt2 = t2 & VT_BTYPE;
4726 /* cast operands to correct type according to ISOC rules */
4727 if (is_float(bt1) || is_float(bt2)) {
4728 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
4729 type.t = VT_LDOUBLE;
4730 } else if (bt1 == VT_DOUBLE || bt2 == VT_DOUBLE) {
4731 type.t = VT_DOUBLE;
4732 } else {
4733 type.t = VT_FLOAT;
4734 }
4735 } else if (bt1 == VT_LLONG || bt2 == VT_LLONG) {
4736 /* cast to biggest op */
4737 type.t = VT_LLONG;
4738 /* convert to unsigned if it does not fit in a long long */
4739 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED) ||
4740 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_LLONG | VT_UNSIGNED))
4741 type.t |= VT_UNSIGNED;
4742 } else if (bt1 == VT_PTR || bt2 == VT_PTR) {
4743 /* XXX: test pointer compatibility */
4744 type = type1;
4745 } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
4746 /* XXX: test function pointer compatibility */
4747 type = type1;
4748 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
4749 /* XXX: test structure compatibility */
4750 type = type1;
4751 } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
4752 /* NOTE: as an extension, we accept void on only one side */
4753 type.t = VT_VOID;
4754 } else {
4755 /* integer operations */
4756 type.t = VT_INT;
4757 /* convert to unsigned if it does not fit in an integer */
4758 if ((t1 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED) ||
4759 (t2 & (VT_BTYPE | VT_UNSIGNED)) == (VT_INT | VT_UNSIGNED))
4760 type.t |= VT_UNSIGNED;
4761 }
4762
4763 /* now we convert second operand */
4764 gen_cast(&type);
4765 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4766 gaddrof();
4767 rc = RC_INT;
4768 if (is_float(type.t)) {
4769 rc = RC_FLOAT;
4770#ifdef TCC_TARGET_X86_64
4771 if ((type.t & VT_BTYPE) == VT_LDOUBLE) {
4772 rc = RC_ST0;
4773 }
4774#endif
4775 } else if ((type.t & VT_BTYPE) == VT_LLONG) {
4776 /* for long longs, we use fixed registers to avoid having
4777 to handle a complicated move */
4778 rc = RC_IRET;
4779 }
4780
4781 r2 = gv(rc);
4782 /* this is horrible, but we must also convert first
4783 operand */
4784 tt = gjmp(0);
4785 gsym(u);
4786 /* put again first value and cast it */
4787 *vtop = sv;
4788 gen_cast(&type);
4789 if (VT_STRUCT == (vtop->type.t & VT_BTYPE))
4790 gaddrof();
4791 r1 = gv(rc);
4792 move_reg(r2, r1);
4793 vtop->r = r2;
4794 gsym(tt);
4795 }
4796 }
4797}
4798
4799static void gexpr(void)
4800{
4801 while (1) {
4802 expr_eq();
4803 if (tok != ',')
4804 break;
4805 vpop();
4806 next();
4807 }
4808}
4809
4810/* parse an expression and return its type without any side effect. */
4811static void expr_type(CType *type)
4812{
4813 int saved_nocode_wanted;
4814
4815 saved_nocode_wanted = nocode_wanted;
4816 nocode_wanted = 1;
4817 gexpr();
4818 *type = vtop->type;
4819 vpop();
4820 nocode_wanted = saved_nocode_wanted;
4821}
4822
4823/* parse a unary expression and return its type without any side
4824 effect. */
4825static void unary_type(CType *type)
4826{
4827 int a;
4828
4829 a = nocode_wanted;
4830 nocode_wanted = 1;
4831 unary();
4832 *type = vtop->type;
4833 vpop();
4834 nocode_wanted = a;
4835}
4836
4837/* parse a constant expression and return value in vtop. */
4838static void expr_const1(void)
4839{
4840 int a;
4841 a = const_wanted;
4842 const_wanted = 1;
4843 expr_eq();
4844 const_wanted = a;
4845}
4846
4847/* parse an integer constant and return its value. */
4848static int expr_const(void)
4849{
4850 int c;
4851 expr_const1();
4852 if ((vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) != VT_CONST)
4853 expect("constant expression");
4854 c = vtop->c.i;
4855 vpop();
4856 return c;
4857}
4858
4859/* return the label token if current token is a label, otherwise
4860 return zero */
4861static int is_label(void)
4862{
4863 int last_tok;
4864
4865 /* fast test first */
4866 if (tok < TOK_UIDENT)
4867 return 0;
4868 /* no need to save tokc because tok is an identifier */
4869 last_tok = tok;
4870 next();
4871 if (tok == ':') {
4872 next();
4873 return last_tok;
4874 } else {
4875 unget_tok(last_tok);
4876 return 0;
4877 }
4878}
4879
4880static void block(int *bsym, int *csym, int *case_sym, int *def_sym,
4881 int case_reg, int is_expr)
4882{
4883 int a, b, c, d;
4884 Sym *s;
4885
4886 /* generate line number info */
4887 if (do_debug &&
4888 (last_line_num != file->line_num || last_ind != ind)) {
4889 put_stabn(N_SLINE, 0, file->line_num, ind - func_ind);
4890 last_ind = ind;
4891 last_line_num = file->line_num;
4892 }
4893
4894 if (is_expr) {
4895 /* default return value is (void) */
4896 vpushi(0);
4897 vtop->type.t = VT_VOID;
4898 }
4899
4900 if (tok == TOK_IF) {
4901 /* if test */
4902 next();
4903 skip('(');
4904 gexpr();
4905 skip(')');
4906 a = gtst(1, 0);
4907 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4908 c = tok;
4909 if (c == TOK_ELSE) {
4910 next();
4911 d = gjmp(0);
4912 gsym(a);
4913 block(bsym, csym, case_sym, def_sym, case_reg, 0);
4914 gsym(d); /* patch else jmp */
4915 } else
4916 gsym(a);
4917 } else if (tok == TOK_WHILE) {
4918 next();
4919 d = ind;
4920 skip('(');
4921 gexpr();
4922 skip(')');
4923 a = gtst(1, 0);
4924 b = 0;
4925 block(&a, &b, case_sym, def_sym, case_reg, 0);
4926 gjmp_addr(d);
4927 gsym(a);
4928 gsym_addr(b, d);
4929 } else if (tok == '{') {
4930 Sym *llabel;
4931
4932 next();
4933 /* record local declaration stack position */
4934 s = local_stack;
4935 llabel = local_label_stack;
4936 /* handle local labels declarations */
4937 if (tok == TOK_LABEL) {
4938 next();
4939 for(;;) {
4940 if (tok < TOK_UIDENT)
4941 expect("label identifier");
4942 label_push(&local_label_stack, tok, LABEL_DECLARED);
4943 next();
4944 if (tok == ',') {
4945 next();
4946 } else {
4947 skip(';');
4948 break;
4949 }
4950 }
4951 }
4952 while (tok != '}') {
4953 decl(VT_LOCAL);
4954 if (tok != '}') {
4955 if (is_expr)
4956 vpop();
4957 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
4958 }
4959 }
4960 /* pop locally defined labels */
4961 label_pop(&local_label_stack, llabel);
4962 /* pop locally defined symbols */
4963 if(is_expr) {
4964 /* XXX: this solution makes only valgrind happy...
4965 triggered by gcc.c-torture/execute/20000917-1.c */
4966 Sym *p;
4967 switch(vtop->type.t & VT_BTYPE) {
4968 case VT_PTR:
4969 case VT_STRUCT:
4970 case VT_ENUM:
4971 case VT_FUNC:
4972 for(p=vtop->type.ref;p;p=p->prev)
4973 if(p->prev==s)
4974 error("unsupported expression type");
4975 }
4976 }
4977 sym_pop(&local_stack, s);
4978 next();
4979 } else if (tok == TOK_RETURN) {
4980 next();
4981 if (tok != ';') {
4982 gexpr();
4983 gen_assign_cast(&func_vt);
4984 if ((func_vt.t & VT_BTYPE) == VT_STRUCT) {
4985 CType type;
4986 /* if returning structure, must copy it to implicit
4987 first pointer arg location */
4988#ifdef TCC_ARM_EABI
4989 int align, size;
4990 size = type_size(&func_vt,&align);
4991 if(size <= 4)
4992 {
4993 if((vtop->r != (VT_LOCAL | VT_LVAL) || (vtop->c.i & 3))
4994 && (align & 3))
4995 {
4996 int addr;
4997 loc = (loc - size) & -4;
4998 addr = loc;
4999 type = func_vt;
5000 vset(&type, VT_LOCAL | VT_LVAL, addr);
5001 vswap();
5002 vstore();
5003 vset(&int_type, VT_LOCAL | VT_LVAL, addr);
5004 }
5005 vtop->type = int_type;
5006 gv(RC_IRET);
5007 } else {
5008#endif
5009 type = func_vt;
5010 mk_pointer(&type);
5011 vset(&type, VT_LOCAL | VT_LVAL, func_vc);
5012 indir();
5013 vswap();
5014 /* copy structure value to pointer */
5015 vstore();
5016#ifdef TCC_ARM_EABI
5017 }
5018#endif
5019 } else if (is_float(func_vt.t)) {
5020 gv(rc_fret(func_vt.t));
5021 } else {
5022 gv(RC_IRET);
5023 }
5024 vtop--; /* NOT vpop() because on x86 it would flush the fp stack */
5025 }
5026 skip(';');
5027 rsym = gjmp(rsym); /* jmp */
5028 } else if (tok == TOK_BREAK) {
5029 /* compute jump */
5030 if (!bsym)
5031 error("cannot break");
5032 *bsym = gjmp(*bsym);
5033 next();
5034 skip(';');
5035 } else if (tok == TOK_CONTINUE) {
5036 /* compute jump */
5037 if (!csym)
5038 error("cannot continue");
5039 *csym = gjmp(*csym);
5040 next();
5041 skip(';');
5042 } else if (tok == TOK_FOR) {
5043 int e;
5044 next();
5045 skip('(');
5046 if (tok != ';') {
5047 gexpr();
5048 vpop();
5049 }
5050 skip(';');
5051 d = ind;
5052 c = ind;
5053 a = 0;
5054 b = 0;
5055 if (tok != ';') {
5056 gexpr();
5057 a = gtst(1, 0);
5058 }
5059 skip(';');
5060 if (tok != ')') {
5061 e = gjmp(0);
5062 c = ind;
5063 gexpr();
5064 vpop();
5065 gjmp_addr(d);
5066 gsym(e);
5067 }
5068 skip(')');
5069 block(&a, &b, case_sym, def_sym, case_reg, 0);
5070 gjmp_addr(c);
5071 gsym(a);
5072 gsym_addr(b, c);
5073 } else
5074 if (tok == TOK_DO) {
5075 next();
5076 a = 0;
5077 b = 0;
5078 d = ind;
5079 block(&a, &b, case_sym, def_sym, case_reg, 0);
5080 skip(TOK_WHILE);
5081 skip('(');
5082 gsym(b);
5083 gexpr();
5084 c = gtst(0, 0);
5085 gsym_addr(c, d);
5086 skip(')');
5087 gsym(a);
5088 skip(';');
5089 } else
5090 if (tok == TOK_SWITCH) {
5091 next();
5092 skip('(');
5093 gexpr();
5094 /* XXX: other types than integer */
5095 case_reg = gv(RC_INT);
5096 vpop();
5097 skip(')');
5098 a = 0;
5099 b = gjmp(0); /* jump to first case */
5100 c = 0;
5101 block(&a, csym, &b, &c, case_reg, 0);
5102 /* if no default, jmp after switch */
5103 if (c == 0)
5104 c = ind;
5105 /* default label */
5106 gsym_addr(b, c);
5107 /* break label */
5108 gsym(a);
5109 } else
5110 if (tok == TOK_CASE) {
5111 int v1, v2;
5112 if (!case_sym)
5113 expect("switch");
5114 next();
5115 v1 = expr_const();
5116 v2 = v1;
5117 if (gnu_ext && tok == TOK_DOTS) {
5118 next();
5119 v2 = expr_const();
5120 if (v2 < v1)
5121 warning("empty case range");
5122 }
5123 /* since a case is like a label, we must skip it with a jmp */
5124 b = gjmp(0);
5125 gsym(*case_sym);
5126 vseti(case_reg, 0);
5127 vpushi(v1);
5128 if (v1 == v2) {
5129 gen_op(TOK_EQ);
5130 *case_sym = gtst(1, 0);
5131 } else {
5132 gen_op(TOK_GE);
5133 *case_sym = gtst(1, 0);
5134 vseti(case_reg, 0);
5135 vpushi(v2);
5136 gen_op(TOK_LE);
5137 *case_sym = gtst(1, *case_sym);
5138 }
5139 gsym(b);
5140 skip(':');
5141 is_expr = 0;
5142 goto block_after_label;
5143 } else
5144 if (tok == TOK_DEFAULT) {
5145 next();
5146 skip(':');
5147 if (!def_sym)
5148 expect("switch");
5149 if (*def_sym)
5150 error("too many 'default'");
5151 *def_sym = ind;
5152 is_expr = 0;
5153 goto block_after_label;
5154 } else
5155 if (tok == TOK_GOTO) {
5156 next();
5157 if (tok == '*' && gnu_ext) {
5158 /* computed goto */
5159 next();
5160 gexpr();
5161 if ((vtop->type.t & VT_BTYPE) != VT_PTR)
5162 expect("pointer");
5163 ggoto();
5164 } else if (tok >= TOK_UIDENT) {
5165 s = label_find(tok);
5166 /* put forward definition if needed */
5167 if (!s) {
5168 s = label_push(&global_label_stack, tok, LABEL_FORWARD);
5169 } else {
5170 if (s->r == LABEL_DECLARED)
5171 s->r = LABEL_FORWARD;
5172 }
5173 /* label already defined */
5174 if (s->r & LABEL_FORWARD)
5175 s->next = (void *)gjmp((long)s->next);
5176 else
5177 gjmp_addr((long)s->next);
5178 next();
5179 } else {
5180 expect("label identifier");
5181 }
5182 skip(';');
5183 } else if (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3) {
5184 asm_instr();
5185 } else {
5186 b = is_label();
5187 if (b) {
5188 /* label case */
5189 s = label_find(b);
5190 if (s) {
5191 if (s->r == LABEL_DEFINED)
5192 error("duplicate label '%s'", get_tok_str(s->v, NULL));
5193 gsym((long)s->next);
5194 s->r = LABEL_DEFINED;
5195 } else {
5196 s = label_push(&global_label_stack, b, LABEL_DEFINED);
5197 }
5198 s->next = (void *)ind;
5199 /* we accept this, but it is a mistake */
5200 block_after_label:
5201 if (tok == '}') {
5202 warning("deprecated use of label at end of compound statement");
5203 } else {
5204 if (is_expr)
5205 vpop();
5206 block(bsym, csym, case_sym, def_sym, case_reg, is_expr);
5207 }
5208 } else {
5209 /* expression case */
5210 if (tok != ';') {
5211 if (is_expr) {
5212 vpop();
5213 gexpr();
5214 } else {
5215 gexpr();
5216 vpop();
5217 }
5218 }
5219 skip(';');
5220 }
5221 }
5222}
5223
5224/* t is the array or struct type. c is the array or struct
5225 address. cur_index/cur_field is the pointer to the current
5226 value. 'size_only' is true if only size info is needed (only used
5227 in arrays) */
5228static void decl_designator(CType *type, Section *sec, unsigned long c,
5229 int *cur_index, Sym **cur_field,
5230 int size_only)
5231{
5232 Sym *s, *f;
5233 int notfirst, index, index_last, align, l, nb_elems, elem_size;
5234 CType type1;
5235
5236 notfirst = 0;
5237 elem_size = 0;
5238 nb_elems = 1;
5239 if (gnu_ext && (l = is_label()) != 0)
5240 goto struct_field;
5241 while (tok == '[' || tok == '.') {
5242 if (tok == '[') {
5243 if (!(type->t & VT_ARRAY))
5244 expect("array type");
5245 s = type->ref;
5246 next();
5247 index = expr_const();
5248 if (index < 0 || (s->c >= 0 && index >= s->c))
5249 expect("invalid index");
5250 if (tok == TOK_DOTS && gnu_ext) {
5251 next();
5252 index_last = expr_const();
5253 if (index_last < 0 ||
5254 (s->c >= 0 && index_last >= s->c) ||
5255 index_last < index)
5256 expect("invalid index");
5257 } else {
5258 index_last = index;
5259 }
5260 skip(']');
5261 if (!notfirst)
5262 *cur_index = index_last;
5263 type = pointed_type(type);
5264 elem_size = type_size(type, &align);
5265 c += index * elem_size;
5266 /* NOTE: we only support ranges for last designator */
5267 nb_elems = index_last - index + 1;
5268 if (nb_elems != 1) {
5269 notfirst = 1;
5270 break;
5271 }
5272 } else {
5273 next();
5274 l = tok;
5275 next();
5276 struct_field:
5277 if ((type->t & VT_BTYPE) != VT_STRUCT)
5278 expect("struct/union type");
5279 s = type->ref;
5280 l |= SYM_FIELD;
5281 f = s->next;
5282 while (f) {
5283 if (f->v == l)
5284 break;
5285 f = f->next;
5286 }
5287 if (!f)
5288 expect("field");
5289 if (!notfirst)
5290 *cur_field = f;
5291 /* XXX: fix this mess by using explicit storage field */
5292 type1 = f->type;
5293 type1.t |= (type->t & ~VT_TYPE);
5294 type = &type1;
5295 c += f->c;
5296 }
5297 notfirst = 1;
5298 }
5299 if (notfirst) {
5300 if (tok == '=') {
5301 next();
5302 } else {
5303 if (!gnu_ext)
5304 expect("=");
5305 }
5306 } else {
5307 if (type->t & VT_ARRAY) {
5308 index = *cur_index;
5309 type = pointed_type(type);
5310 c += index * type_size(type, &align);
5311 } else {
5312 f = *cur_field;
5313 if (!f)
5314 error("too many field init");
5315 /* XXX: fix this mess by using explicit storage field */
5316 type1 = f->type;
5317 type1.t |= (type->t & ~VT_TYPE);
5318 type = &type1;
5319 c += f->c;
5320 }
5321 }
5322 decl_initializer(type, sec, c, 0, size_only);
5323
5324 /* XXX: make it more general */
5325 if (!size_only && nb_elems > 1) {
5326 unsigned long c_end;
5327 uint8_t *src, *dst;
5328 int i;
5329
5330 if (!sec)
5331 error("range init not supported yet for dynamic storage");
5332 c_end = c + nb_elems * elem_size;
5333 if (c_end > sec->data_allocated)
5334 section_realloc(sec, c_end);
5335 src = sec->data + c;
5336 dst = src;
5337 for(i = 1; i < nb_elems; i++) {
5338 dst += elem_size;
5339 memcpy(dst, src, elem_size);
5340 }
5341 }
5342}
5343
5344#define EXPR_VAL 0
5345#define EXPR_CONST 1
5346#define EXPR_ANY 2
5347
5348/* store a value or an expression directly in global data or in local array */
5349static void init_putv(CType *type, Section *sec, unsigned long c,
5350 int v, int expr_type)
5351{
5352 int saved_global_expr, bt, bit_pos, bit_size;
5353 void *ptr;
5354 unsigned long long bit_mask;
5355 CType dtype;
5356
5357 switch(expr_type) {
5358 case EXPR_VAL:
5359 vpushi(v);
5360 break;
5361 case EXPR_CONST:
5362 /* compound literals must be allocated globally in this case */
5363 saved_global_expr = global_expr;
5364 global_expr = 1;
5365 expr_const1();
5366 global_expr = saved_global_expr;
5367 /* NOTE: symbols are accepted */
5368 if ((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST)
5369 error("initializer element is not constant");
5370 break;
5371 case EXPR_ANY:
5372 expr_eq();
5373 break;
5374 }
5375
5376 dtype = *type;
5377 dtype.t &= ~VT_CONSTANT; /* need to do that to avoid false warning */
5378
5379 if (sec) {
5380 /* XXX: not portable */
5381 /* XXX: generate error if incorrect relocation */
5382 gen_assign_cast(&dtype);
5383 bt = type->t & VT_BTYPE;
5384 /* we'll write at most 12 bytes */
5385 if (c + 12 > sec->data_allocated) {
5386 section_realloc(sec, c + 12);
5387 }
5388 ptr = sec->data + c;
5389 /* XXX: make code faster ? */
5390 if (!(type->t & VT_BITFIELD)) {
5391 bit_pos = 0;
5392 bit_size = 32;
5393 bit_mask = -1LL;
5394 } else {
5395 bit_pos = (vtop->type.t >> VT_STRUCT_SHIFT) & 0x3f;
5396 bit_size = (vtop->type.t >> (VT_STRUCT_SHIFT + 6)) & 0x3f;
5397 bit_mask = (1LL << bit_size) - 1;
5398 }
5399 if ((vtop->r & VT_SYM) &&
5400 (bt == VT_BYTE ||
5401 bt == VT_SHORT ||
5402 bt == VT_DOUBLE ||
5403 bt == VT_LDOUBLE ||
5404 bt == VT_LLONG ||
5405 (bt == VT_INT && bit_size != 32)))
5406 error("initializer element is not computable at load time");
5407 switch(bt) {
5408 case VT_BOOL:
5409 vtop->c.i = (vtop->c.i != 0);
5410 case VT_BYTE:
5411 *(char *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5412 break;
5413 case VT_SHORT:
5414 *(short *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5415 break;
5416 case VT_DOUBLE:
5417 *(double *)ptr = vtop->c.d;
5418 break;
5419 case VT_LDOUBLE:
5420 *(long double *)ptr = vtop->c.ld;
5421 break;
5422 case VT_LLONG:
5423 *(long long *)ptr |= (vtop->c.ll & bit_mask) << bit_pos;
5424 break;
5425 default:
5426 if (vtop->r & VT_SYM) {
5427 greloc(sec, vtop->sym, c, R_DATA_32);
5428 }
5429 *(int *)ptr |= (vtop->c.i & bit_mask) << bit_pos;
5430 break;
5431 }
5432 vtop--;
5433 } else {
5434 vset(&dtype, VT_LOCAL|VT_LVAL, c);
5435 vswap();
5436 vstore();
5437 vpop();
5438 }
5439}
5440
5441/* put zeros for variable based init */
5442static void init_putz(CType *t, Section *sec, unsigned long c, int size)
5443{
5444 if (sec) {
5445 /* nothing to do because globals are already set to zero */
5446 } else {
5447 vpush_global_sym(&func_old_type, TOK_memset);
5448 vseti(VT_LOCAL, c);
5449 vpushi(0);
5450 vpushi(size);
5451 gfunc_call(3);
5452 }
5453}
5454
5455/* 't' contains the type and storage info. 'c' is the offset of the
5456 object in section 'sec'. If 'sec' is NULL, it means stack based
5457 allocation. 'first' is true if array '{' must be read (multi
5458 dimension implicit array init handling). 'size_only' is true if
5459 size only evaluation is wanted (only for arrays). */
5460static void decl_initializer(CType *type, Section *sec, unsigned long c,
5461 int first, int size_only)
5462{
5463 int index, array_length, n, no_oblock, nb, parlevel, i;
5464 int size1, align1, expr_type;
5465 Sym *s, *f;
5466 CType *t1;
5467
5468 if (type->t & VT_ARRAY) {
5469 s = type->ref;
5470 n = s->c;
5471 array_length = 0;
5472 t1 = pointed_type(type);
5473 size1 = type_size(t1, &align1);
5474
5475 no_oblock = 1;
5476 if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
5477 tok == '{') {
5478 skip('{');
5479 no_oblock = 0;
5480 }
5481
5482 /* only parse strings here if correct type (otherwise: handle
5483 them as ((w)char *) expressions */
5484 if ((tok == TOK_LSTR &&
5485#ifdef TCC_TARGET_PE
5486 (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
5487#else
5488 (t1->t & VT_BTYPE) == VT_INT
5489#endif
5490 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
5491 while (tok == TOK_STR || tok == TOK_LSTR) {
5492 int cstr_len, ch;
5493 CString *cstr;
5494
5495 cstr = tokc.cstr;
5496 /* compute maximum number of chars wanted */
5497 if (tok == TOK_STR)
5498 cstr_len = cstr->size;
5499 else
5500 cstr_len = cstr->size / sizeof(nwchar_t);
5501 cstr_len--;
5502 nb = cstr_len;
5503 if (n >= 0 && nb > (n - array_length))
5504 nb = n - array_length;
5505 if (!size_only) {
5506 if (cstr_len > nb)
5507 warning("initializer-string for array is too long");
5508 /* in order to go faster for common case (char
5509 string in global variable, we handle it
5510 specifically */
5511 if (sec && tok == TOK_STR && size1 == 1) {
5512 memcpy(sec->data + c + array_length, cstr->data, nb);
5513 } else {
5514 for(i=0;i<nb;i++) {
5515 if (tok == TOK_STR)
5516 ch = ((unsigned char *)cstr->data)[i];
5517 else
5518 ch = ((nwchar_t *)cstr->data)[i];
5519 init_putv(t1, sec, c + (array_length + i) * size1,
5520 ch, EXPR_VAL);
5521 }
5522 }
5523 }
5524 array_length += nb;
5525 next();
5526 }
5527 /* only add trailing zero if enough storage (no
5528 warning in this case since it is standard) */
5529 if (n < 0 || array_length < n) {
5530 if (!size_only) {
5531 init_putv(t1, sec, c + (array_length * size1), 0, EXPR_VAL);
5532 }
5533 array_length++;
5534 }
5535 } else {
5536 index = 0;
5537 while (tok != '}') {
5538 decl_designator(type, sec, c, &index, NULL, size_only);
5539 if (n >= 0 && index >= n)
5540 error("index too large");
5541 /* must put zero in holes (note that doing it that way
5542 ensures that it even works with designators) */
5543 if (!size_only && array_length < index) {
5544 init_putz(t1, sec, c + array_length * size1,
5545 (index - array_length) * size1);
5546 }
5547 index++;
5548 if (index > array_length)
5549 array_length = index;
5550 /* special test for multi dimensional arrays (may not
5551 be strictly correct if designators are used at the
5552 same time) */
5553 if (index >= n && no_oblock)
5554 break;
5555 if (tok == '}')
5556 break;
5557 skip(',');
5558 }
5559 }
5560 if (!no_oblock)
5561 skip('}');
5562 /* put zeros at the end */
5563 if (!size_only && n >= 0 && array_length < n) {
5564 init_putz(t1, sec, c + array_length * size1,
5565 (n - array_length) * size1);
5566 }
5567 /* patch type size if needed */
5568 if (n < 0)
5569 s->c = array_length;
5570 } else if ((type->t & VT_BTYPE) == VT_STRUCT &&
5571 (sec || !first || tok == '{')) {
5572 int par_count;
5573
5574 /* NOTE: the previous test is a specific case for automatic
5575 struct/union init */
5576 /* XXX: union needs only one init */
5577
5578 /* XXX: this test is incorrect for local initializers
5579 beginning with ( without {. It would be much more difficult
5580 to do it correctly (ideally, the expression parser should
5581 be used in all cases) */
5582 par_count = 0;
5583 if (tok == '(') {
5584 AttributeDef ad1;
5585 CType type1;
5586 next();
5587 while (tok == '(') {
5588 par_count++;
5589 next();
5590 }
5591 if (!parse_btype(&type1, &ad1))
5592 expect("cast");
5593 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
5594#if 0
5595 if (!is_assignable_types(type, &type1))
5596 error("invalid type for cast");
5597#endif
5598 skip(')');
5599 }
5600 no_oblock = 1;
5601 if (first || tok == '{') {
5602 skip('{');
5603 no_oblock = 0;
5604 }
5605 s = type->ref;
5606 f = s->next;
5607 array_length = 0;
5608 index = 0;
5609 n = s->c;
5610 while (tok != '}') {
5611 decl_designator(type, sec, c, NULL, &f, size_only);
5612 index = f->c;
5613 if (!size_only && array_length < index) {
5614 init_putz(type, sec, c + array_length,
5615 index - array_length);
5616 }
5617 index = index + type_size(&f->type, &align1);
5618 if (index > array_length)
5619 array_length = index;
5620 f = f->next;
5621 if (no_oblock && f == NULL)
5622 break;
5623 if (tok == '}')
5624 break;
5625 skip(',');
5626 }
5627 /* put zeros at the end */
5628 if (!size_only && array_length < n) {
5629 init_putz(type, sec, c + array_length,
5630 n - array_length);
5631 }
5632 if (!no_oblock)
5633 skip('}');
5634 while (par_count) {
5635 skip(')');
5636 par_count--;
5637 }
5638 } else if (tok == '{') {
5639 next();
5640 decl_initializer(type, sec, c, first, size_only);
5641 skip('}');
5642 } else if (size_only) {
5643 /* just skip expression */
5644 parlevel = 0;
5645 while ((parlevel > 0 || (tok != '}' && tok != ',')) &&
5646 tok != -1) {
5647 if (tok == '(')
5648 parlevel++;
5649 else if (tok == ')')
5650 parlevel--;
5651 next();
5652 }
5653 } else {
5654 /* currently, we always use constant expression for globals
5655 (may change for scripting case) */
5656 expr_type = EXPR_CONST;
5657 if (!sec)
5658 expr_type = EXPR_ANY;
5659 init_putv(type, sec, c, 0, expr_type);
5660 }
5661}
5662
5663/* parse an initializer for type 't' if 'has_init' is non zero, and
5664 allocate space in local or global data space ('r' is either
5665 VT_LOCAL or VT_CONST). If 'v' is non zero, then an associated
5666 variable 'v' of scope 'scope' is declared before initializers are
5667 parsed. If 'v' is zero, then a reference to the new object is put
5668 in the value stack. If 'has_init' is 2, a special parsing is done
5669 to handle string constants. */
5670static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
5671 int has_init, int v, int scope)
5672{
5673 int size, align, addr, data_offset;
5674 int level;
5675 ParseState saved_parse_state = {0};
5676 TokenString init_str;
5677 Section *sec;
5678
5679 size = type_size(type, &align);
5680 /* If unknown size, we must evaluate it before
5681 evaluating initializers because
5682 initializers can generate global data too
5683 (e.g. string pointers or ISOC99 compound
5684 literals). It also simplifies local
5685 initializers handling */
5686 tok_str_new(&init_str);
5687 if (size < 0) {
5688 if (!has_init)
5689 error("unknown type size");
5690 /* get all init string */
5691 if (has_init == 2) {
5692 /* only get strings */
5693 while (tok == TOK_STR || tok == TOK_LSTR) {
5694 tok_str_add_tok(&init_str);
5695 next();
5696 }
5697 } else {
5698 level = 0;
5699 while (level > 0 || (tok != ',' && tok != ';')) {
5700 if (tok < 0)
5701 error("unexpected end of file in initializer");
5702 tok_str_add_tok(&init_str);
5703 if (tok == '{')
5704 level++;
5705 else if (tok == '}') {
5706 level--;
5707 if (level <= 0) {
5708 next();
5709 break;
5710 }
5711 }
5712 next();
5713 }
5714 }
5715 tok_str_add(&init_str, -1);
5716 tok_str_add(&init_str, 0);
5717
5718 /* compute size */
5719 save_parse_state(&saved_parse_state);
5720
5721 macro_ptr = init_str.str;
5722 next();
5723 decl_initializer(type, NULL, 0, 1, 1);
5724 /* prepare second initializer parsing */
5725 macro_ptr = init_str.str;
5726 next();
5727
5728 /* if still unknown size, error */
5729 size = type_size(type, &align);
5730 if (size < 0)
5731 error("unknown type size");
5732 }
5733 /* take into account specified alignment if bigger */
5734 if (ad->aligned) {
5735 if (ad->aligned > align)
5736 align = ad->aligned;
5737 } else if (ad->packed) {
5738 align = 1;
5739 }
5740 if ((r & VT_VALMASK) == VT_LOCAL) {
5741 sec = NULL;
5742 if (do_bounds_check && (type->t & VT_ARRAY))
5743 loc--;
5744 loc = (loc - size) & -align;
5745 addr = loc;
5746 /* handles bounds */
5747 /* XXX: currently, since we do only one pass, we cannot track
5748 '&' operators, so we add only arrays */
5749 if (do_bounds_check && (type->t & VT_ARRAY)) {
5750 unsigned long *bounds_ptr;
5751 /* add padding between regions */
5752 loc--;
5753 /* then add local bound info */
5754 bounds_ptr = section_ptr_add(lbounds_section, 2 * sizeof(unsigned long));
5755 bounds_ptr[0] = addr;
5756 bounds_ptr[1] = size;
5757 }
5758 if (v) {
5759 /* local variable */
5760 sym_push(v, type, r, addr);
5761 } else {
5762 /* push local reference */
5763 vset(type, r, addr);
5764 }
5765 } else {
5766 Sym *sym;
5767
5768 sym = NULL;
5769 if (v && scope == VT_CONST) {
5770 /* see if the symbol was already defined */
5771 sym = sym_find(v);
5772 if (sym) {
5773 if (!is_compatible_types(&sym->type, type))
5774 error("incompatible types for redefinition of '%s'",
5775 get_tok_str(v, NULL));
5776 if (sym->type.t & VT_EXTERN) {
5777 /* if the variable is extern, it was not allocated */
5778 sym->type.t &= ~VT_EXTERN;
5779 /* set array size if it was ommited in extern
5780 declaration */
5781 if ((sym->type.t & VT_ARRAY) &&
5782 sym->type.ref->c < 0 &&
5783 type->ref->c >= 0)
5784 sym->type.ref->c = type->ref->c;
5785 } else {
5786 /* we accept several definitions of the same
5787 global variable. this is tricky, because we
5788 must play with the SHN_COMMON type of the symbol */
5789 /* XXX: should check if the variable was already
5790 initialized. It is incorrect to initialized it
5791 twice */
5792 /* no init data, we won't add more to the symbol */
5793 if (!has_init)
5794 goto no_alloc;
5795 }
5796 }
5797 }
5798
5799 /* allocate symbol in corresponding section */
5800 sec = ad->section;
5801 if (!sec) {
5802 if (has_init)
5803 sec = data_section;
5804 else if (tcc_state->nocommon)
5805 sec = bss_section;
5806 }
5807 if (sec) {
5808 data_offset = sec->data_offset;
5809 data_offset = (data_offset + align - 1) & -align;
5810 addr = data_offset;
5811 /* very important to increment global pointer at this time
5812 because initializers themselves can create new initializers */
5813 data_offset += size;
5814 /* add padding if bound check */
5815 if (do_bounds_check)
5816 data_offset++;
5817 sec->data_offset = data_offset;
5818 /* allocate section space to put the data */
5819 if (sec->sh_type != SHT_NOBITS &&
5820 data_offset > sec->data_allocated)
5821 section_realloc(sec, data_offset);
5822 /* align section if needed */
5823 if (align > sec->sh_addralign)
5824 sec->sh_addralign = align;
5825 } else {
5826 addr = 0; /* avoid warning */
5827 }
5828
5829 if (v) {
5830 if (scope != VT_CONST || !sym) {
5831 sym = sym_push(v, type, r | VT_SYM, 0);
5832 }
5833 /* update symbol definition */
5834 if (sec) {
5835 put_extern_sym(sym, sec, addr, size);
5836 } else {
5837 ElfW(Sym) *esym;
5838 /* put a common area */
5839 put_extern_sym(sym, NULL, align, size);
5840 /* XXX: find a nicer way */
5841 esym = &((ElfW(Sym) *)symtab_section->data)[sym->c];
5842 esym->st_shndx = SHN_COMMON;
5843 }
5844 } else {
5845 CValue cval;
5846
5847 /* push global reference */
5848 sym = get_sym_ref(type, sec, addr, size);
5849 cval.ul = 0;
5850 vsetc(type, VT_CONST | VT_SYM, &cval);
5851 vtop->sym = sym;
5852 }
5853
5854 /* handles bounds now because the symbol must be defined
5855 before for the relocation */
5856 if (do_bounds_check) {
5857 unsigned long *bounds_ptr;
5858
5859 greloc(bounds_section, sym, bounds_section->data_offset, R_DATA_32);
5860 /* then add global bound info */
5861 bounds_ptr = section_ptr_add(bounds_section, 2 * sizeof(long));
5862 bounds_ptr[0] = 0; /* relocated */
5863 bounds_ptr[1] = size;
5864 }
5865 }
5866 if (has_init) {
5867 decl_initializer(type, sec, addr, 1, 0);
5868 /* restore parse state if needed */
5869 if (init_str.str) {
5870 tok_str_free(init_str.str);
5871 restore_parse_state(&saved_parse_state);
5872 }
5873 }
5874 no_alloc: ;
5875}
5876
5877void put_func_debug(Sym *sym)
5878{
5879 char buf[512];
5880
5881 /* stabs info */
5882 /* XXX: we put here a dummy type */
5883 snprintf(buf, sizeof(buf), "%s:%c1",
5884 funcname, sym->type.t & VT_STATIC ? 'f' : 'F');
5885 put_stabs_r(buf, N_FUN, 0, file->line_num, 0,
5886 cur_text_section, sym->c);
5887 /* //gr gdb wants a line at the function */
5888 put_stabn(N_SLINE, 0, file->line_num, 0);
5889 last_ind = 0;
5890 last_line_num = 0;
5891}
5892
5893/* parse an old style function declaration list */
5894/* XXX: check multiple parameter */
5895static void func_decl_list(Sym *func_sym)
5896{
5897 AttributeDef ad;
5898 int v;
5899 Sym *s;
5900 CType btype, type;
5901
5902 /* parse each declaration */
5903 while (tok != '{' && tok != ';' && tok != ',' && tok != TOK_EOF) {
5904 if (!parse_btype(&btype, &ad))
5905 expect("declaration list");
5906 if (((btype.t & VT_BTYPE) == VT_ENUM ||
5907 (btype.t & VT_BTYPE) == VT_STRUCT) &&
5908 tok == ';') {
5909 /* we accept no variable after */
5910 } else {
5911 for(;;) {
5912 type = btype;
5913 type_decl(&type, &ad, &v, TYPE_DIRECT);
5914 /* find parameter in function parameter list */
5915 s = func_sym->next;
5916 while (s != NULL) {
5917 if ((s->v & ~SYM_FIELD) == v)
5918 goto found;
5919 s = s->next;
5920 }
5921 error("declaration for parameter '%s' but no such parameter",
5922 get_tok_str(v, NULL));
5923 found:
5924 /* check that no storage specifier except 'register' was given */
5925 if (type.t & VT_STORAGE)
5926 error("storage class specified for '%s'", get_tok_str(v, NULL));
5927 convert_parameter_type(&type);
5928 /* we can add the type (NOTE: it could be local to the function) */
5929 s->type = type;
5930 /* accept other parameters */
5931 if (tok == ',')
5932 next();
5933 else
5934 break;
5935 }
5936 }
5937 skip(';');
5938 }
5939}
5940
5941/* parse a function defined by symbol 'sym' and generate its code in
5942 'cur_text_section' */
5943static void gen_function(Sym *sym)
5944{
5945 int saved_nocode_wanted = nocode_wanted;
5946 nocode_wanted = 0;
5947 ind = cur_text_section->data_offset;
5948 /* NOTE: we patch the symbol size later */
5949 put_extern_sym(sym, cur_text_section, ind, 0);
5950 funcname = get_tok_str(sym->v, NULL);
5951 func_ind = ind;
5952 /* put debug symbol */
5953 if (do_debug)
5954 put_func_debug(sym);
5955 /* push a dummy symbol to enable local sym storage */
5956 sym_push2(&local_stack, SYM_FIELD, 0, 0);
5957 gfunc_prolog(&sym->type);
5958 rsym = 0;
5959 block(NULL, NULL, NULL, NULL, 0, 0);
5960 gsym(rsym);
5961 gfunc_epilog();
5962 cur_text_section->data_offset = ind;
5963 label_pop(&global_label_stack, NULL);
5964 sym_pop(&local_stack, NULL); /* reset local stack */
5965 /* end of function */
5966 /* patch symbol size */
5967 ((ElfW(Sym) *)symtab_section->data)[sym->c].st_size =
5968 ind - func_ind;
5969 if (do_debug) {
5970 put_stabn(N_FUN, 0, 0, ind - func_ind);
5971 }
5972 /* It's better to crash than to generate wrong code */
5973 cur_text_section = NULL;
5974 funcname = ""; /* for safety */
5975 func_vt.t = VT_VOID; /* for safety */
5976 ind = 0; /* for safety */
5977 nocode_wanted = saved_nocode_wanted;
5978}
5979
5980static void gen_inline_functions(void)
5981{
5982 Sym *sym;
5983 CType *type;
5984 int *str, inline_generated;
5985
5986 /* iterate while inline function are referenced */
5987 for(;;) {
5988 inline_generated = 0;
5989 for(sym = global_stack; sym != NULL; sym = sym->prev) {
5990 type = &sym->type;
5991 if (((type->t & VT_BTYPE) == VT_FUNC) &&
5992 (type->t & (VT_STATIC | VT_INLINE)) ==
5993 (VT_STATIC | VT_INLINE) &&
5994 sym->c != 0) {
5995 /* the function was used: generate its code and
5996 convert it to a normal function */
5997 str = INLINE_DEF(sym->r);
5998 sym->r = VT_SYM | VT_CONST;
5999 sym->type.t &= ~VT_INLINE;
6000
6001 macro_ptr = str;
6002 next();
6003 cur_text_section = text_section;
6004 gen_function(sym);
6005 macro_ptr = NULL; /* fail safe */
6006
6007 tok_str_free(str);
6008 inline_generated = 1;
6009 }
6010 }
6011 if (!inline_generated)
6012 break;
6013 }
6014
6015 /* free all remaining inline function tokens */
6016 for(sym = global_stack; sym != NULL; sym = sym->prev) {
6017 type = &sym->type;
6018 if (((type->t & VT_BTYPE) == VT_FUNC) &&
6019 (type->t & (VT_STATIC | VT_INLINE)) ==
6020 (VT_STATIC | VT_INLINE)) {
6021 //gr printf("sym %d %s\n", sym->r, get_tok_str(sym->v, NULL));
6022 if (sym->r == (VT_SYM | VT_CONST)) //gr beware!
6023 continue;
6024 str = INLINE_DEF(sym->r);
6025 tok_str_free(str);
6026 sym->r = 0; /* fail safe */
6027 }
6028 }
6029}
6030
6031/* 'l' is VT_LOCAL or VT_CONST to define default storage type */
6032static void decl(int l)
6033{
6034 int v, has_init, r;
6035 CType type, btype;
6036 Sym *sym;
6037 AttributeDef ad;
6038
6039 while (1) {
6040 if (!parse_btype(&btype, &ad)) {
6041 /* skip redundant ';' */
6042 /* XXX: find more elegant solution */
6043 if (tok == ';') {
6044 next();
6045 continue;
6046 }
6047 if (l == VT_CONST &&
6048 (tok == TOK_ASM1 || tok == TOK_ASM2 || tok == TOK_ASM3)) {
6049 /* global asm block */
6050 asm_global_instr();
6051 continue;
6052 }
6053 /* special test for old K&R protos without explicit int
6054 type. Only accepted when defining global data */
6055 if (l == VT_LOCAL || tok < TOK_DEFINE)
6056 break;
6057 btype.t = VT_INT;
6058 }
6059 if (((btype.t & VT_BTYPE) == VT_ENUM ||
6060 (btype.t & VT_BTYPE) == VT_STRUCT) &&
6061 tok == ';') {
6062 /* we accept no variable after */
6063 next();
6064 continue;
6065 }
6066 while (1) { /* iterate thru each declaration */
6067 type = btype;
6068 type_decl(&type, &ad, &v, TYPE_DIRECT);
6069#if 0
6070 {
6071 char buf[500];
6072 type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
6073 printf("type = '%s'\n", buf);
6074 }
6075#endif
6076 if ((type.t & VT_BTYPE) == VT_FUNC) {
6077 /* if old style function prototype, we accept a
6078 declaration list */
6079 sym = type.ref;
6080 if (sym->c == FUNC_OLD)
6081 func_decl_list(sym);
6082 }
6083
6084 if (tok == '{') {
6085 if (l == VT_LOCAL)
6086 error("cannot use local functions");
6087 if ((type.t & VT_BTYPE) != VT_FUNC)
6088 expect("function definition");
6089
6090 /* reject abstract declarators in function definition */
6091 sym = type.ref;
6092 while ((sym = sym->next) != NULL)
6093 if (!(sym->v & ~SYM_FIELD))
6094 expect("identifier");
6095
6096 /* XXX: cannot do better now: convert extern line to static inline */
6097 if ((type.t & (VT_EXTERN | VT_INLINE)) == (VT_EXTERN | VT_INLINE))
6098 type.t = (type.t & ~VT_EXTERN) | VT_STATIC;
6099
6100 sym = sym_find(v);
6101 if (sym) {
6102 if ((sym->type.t & VT_BTYPE) != VT_FUNC)
6103 goto func_error1;
6104 /* specific case: if not func_call defined, we put
6105 the one of the prototype */

110 more lines aren’t shown here.

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

106 more lines aren’t shown here.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t