1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
|
/*
* sparse/compile-i386.c
*
* Copyright (C) 2003 Transmeta Corp.
* 2003 Linus Torvalds
* Copyright 2003 Jeff Garzik
*
* Licensed under the Open Software License version 1.1
*
* x86 backend
*
* TODO list:
* in general, any non-32bit SYM_BASETYPE is unlikely to work.
* complex initializers
* bitfields
* global struct/union variables
* addressing structures, and members of structures (as opposed to
* scalars) on the stack. Requires smarter stack frame allocation.
* labels / goto
* any function argument that isn't 32 bits (or promoted to such)
* inline asm
* floating point
*
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include "lib.h"
#include "allocate.h"
#include "token.h"
#include "parse.h"
#include "symbol.h"
#include "scope.h"
#include "expression.h"
#include "target.h"
#include "compile.h"
#include "bitmap.h"
struct textbuf {
unsigned int len; /* does NOT include terminating null */
char *text;
struct textbuf *next;
struct textbuf *prev;
};
struct loop_stack {
int continue_lbl;
int loop_bottom_lbl;
struct loop_stack *next;
};
struct atom;
struct storage;
DECLARE_PTR_LIST(str_list, struct atom);
DECLARE_PTR_LIST(atom_list, struct atom);
DECLARE_PTR_LIST(storage_list, struct storage);
struct function {
int stack_size;
int pseudo_nr;
struct storage_list *pseudo_list;
struct atom_list *atom_list;
struct str_list *str_list;
struct loop_stack *loop_stack;
struct symbol **argv;
unsigned int argc;
int ret_target;
};
enum storage_type {
STOR_PSEUDO, /* variable stored on the stack */
STOR_ARG, /* function argument */
STOR_SYM, /* a symbol we can directly ref in the asm */
STOR_REG, /* scratch register */
STOR_VALUE, /* integer constant */
STOR_LABEL, /* label / jump target */
STOR_LABELSYM, /* label generated from symbol's pointer value */
};
struct reg_info {
const char *name;
struct storage *contains;
const unsigned char aliases[12];
#define own_regno aliases[0]
};
struct storage {
enum storage_type type;
unsigned long flags;
/* STOR_REG */
struct reg_info *reg;
struct symbol *ctype;
union {
/* STOR_PSEUDO */
struct {
int pseudo;
int offset;
int size;
};
/* STOR_ARG */
struct {
int idx;
};
/* STOR_SYM */
struct {
struct symbol *sym;
};
/* STOR_VALUE */
struct {
long long value;
};
/* STOR_LABEL */
struct {
int label;
};
/* STOR_LABELSYM */
struct {
struct symbol *labelsym;
};
};
};
enum {
STOR_LABEL_VAL = (1 << 0),
STOR_WANTS_FREE = (1 << 1),
};
struct symbol_private {
struct storage *addr;
};
enum atom_type {
ATOM_TEXT,
ATOM_INSN,
ATOM_CSTR,
};
struct atom {
enum atom_type type;
union {
/* stuff for text */
struct {
char *text;
unsigned int text_len; /* w/o terminating null */
};
/* stuff for insns */
struct {
char insn[32];
char comment[40];
struct storage *op1;
struct storage *op2;
};
/* stuff for C strings */
struct {
struct string *string;
int label;
};
};
};
static struct function *current_func = NULL;
static struct textbuf *unit_post_text = NULL;
static const char *current_section;
static void emit_comment(const char * fmt, ...) FORMAT_ATTR(1);
static void emit_move(struct storage *src, struct storage *dest,
struct symbol *ctype, const char *comment);
static int type_is_signed(struct symbol *sym);
static struct storage *x86_address_gen(struct expression *expr);
static struct storage *x86_symbol_expr(struct symbol *sym);
static void x86_symbol(struct symbol *sym);
static struct storage *x86_statement(struct statement *stmt);
static struct storage *x86_expression(struct expression *expr);
enum registers {
NOREG,
AL, DL, CL, BL, AH, DH, CH, BH, // 8-bit
AX, DX, CX, BX, SI, DI, BP, SP, // 16-bit
EAX, EDX, ECX, EBX, ESI, EDI, EBP, ESP, // 32-bit
EAX_EDX, ECX_EBX, ESI_EDI, // 64-bit
};
/* This works on regno's, reg_info's and hardreg_storage's */
#define byte_reg(reg) ((reg) - 16)
#define highbyte_reg(reg) ((reg)-12)
#define word_reg(reg) ((reg)-8)
#define REGINFO(nr, str, conflicts...) [nr] = { .name = str, .aliases = { nr , conflicts } }
static struct reg_info reg_info_table[] = {
REGINFO( AL, "%al", AX, EAX, EAX_EDX),
REGINFO( DL, "%dl", DX, EDX, EAX_EDX),
REGINFO( CL, "%cl", CX, ECX, ECX_EBX),
REGINFO( BL, "%bl", BX, EBX, ECX_EBX),
REGINFO( AH, "%ah", AX, EAX, EAX_EDX),
REGINFO( DH, "%dh", DX, EDX, EAX_EDX),
REGINFO( CH, "%ch", CX, ECX, ECX_EBX),
REGINFO( BH, "%bh", BX, EBX, ECX_EBX),
REGINFO( AX, "%ax", AL, AH, EAX, EAX_EDX),
REGINFO( DX, "%dx", DL, DH, EDX, EAX_EDX),
REGINFO( CX, "%cx", CL, CH, ECX, ECX_EBX),
REGINFO( BX, "%bx", BL, BH, EBX, ECX_EBX),
REGINFO( SI, "%si", ESI, ESI_EDI),
REGINFO( DI, "%di", EDI, ESI_EDI),
REGINFO( BP, "%bp", EBP),
REGINFO( SP, "%sp", ESP),
REGINFO(EAX, "%eax", AL, AH, AX, EAX_EDX),
REGINFO(EDX, "%edx", DL, DH, DX, EAX_EDX),
REGINFO(ECX, "%ecx", CL, CH, CX, ECX_EBX),
REGINFO(EBX, "%ebx", BL, BH, BX, ECX_EBX),
REGINFO(ESI, "%esi", SI, ESI_EDI),
REGINFO(EDI, "%edi", DI, ESI_EDI),
REGINFO(EBP, "%ebp", BP),
REGINFO(ESP, "%esp", SP),
REGINFO(EAX_EDX, "%eax:%edx", AL, AH, AX, EAX, DL, DH, DX, EDX),
REGINFO(ECX_EBX, "%ecx:%ebx", CL, CH, CX, ECX, BL, BH, BX, EBX),
REGINFO(ESI_EDI, "%esi:%edi", SI, ESI, DI, EDI),
};
#define REGSTORAGE(nr) [nr] = { .type = STOR_REG, .reg = reg_info_table + (nr) }
static struct storage hardreg_storage_table[] = {
REGSTORAGE(AL), REGSTORAGE(DL), REGSTORAGE(CL), REGSTORAGE(BL),
REGSTORAGE(AH), REGSTORAGE(DH), REGSTORAGE(CH), REGSTORAGE(BH),
REGSTORAGE(AX), REGSTORAGE(DX), REGSTORAGE(CX), REGSTORAGE(BX),
REGSTORAGE(SI), REGSTORAGE(DI), REGSTORAGE(BP), REGSTORAGE(SP),
REGSTORAGE(EAX), REGSTORAGE(EDX), REGSTORAGE(ECX), REGSTORAGE(EBX),
REGSTORAGE(ESI), REGSTORAGE(EDI), REGSTORAGE(EBP), REGSTORAGE(ESP),
REGSTORAGE(EAX_EDX), REGSTORAGE(ECX_EBX), REGSTORAGE(ESI_EDI),
};
#define REG_EAX (&hardreg_storage_table[EAX])
#define REG_ECX (&hardreg_storage_table[ECX])
#define REG_EDX (&hardreg_storage_table[EDX])
#define REG_ESP (&hardreg_storage_table[ESP])
#define REG_DL (&hardreg_storage_table[DL])
#define REG_DX (&hardreg_storage_table[DX])
#define REG_AL (&hardreg_storage_table[AL])
#define REG_AX (&hardreg_storage_table[AX])
static DECLARE_BITMAP(regs_in_use, 256);
static inline struct storage * reginfo_reg(struct reg_info *info)
{
return hardreg_storage_table + info->own_regno;
}
static struct storage * get_hardreg(struct storage *reg, int clear)
{
struct reg_info *info = reg->reg;
const unsigned char *aliases;
int regno;
aliases = info->aliases;
while ((regno = *aliases++) != NOREG) {
if (test_bit(regno, regs_in_use))
goto busy;
if (clear)
reg_info_table[regno].contains = NULL;
}
set_bit(info->own_regno, regs_in_use);
return reg;
busy:
fprintf(stderr, "register %s is busy\n", info->name);
if (regno + reg_info_table != info)
fprintf(stderr, " conflicts with %s\n", reg_info_table[regno].name);
exit(1);
}
static void put_reg(struct storage *reg)
{
struct reg_info *info = reg->reg;
int regno = info->own_regno;
if (test_and_clear_bit(regno, regs_in_use))
return;
fprintf(stderr, "freeing already free'd register %s\n", reg_info_table[regno].name);
}
struct regclass {
const char *name;
const unsigned char regs[30];
};
static struct regclass regclass_8 = { "8-bit", { AL, DL, CL, BL, AH, DH, CH, BH }};
static struct regclass regclass_16 = { "16-bit", { AX, DX, CX, BX, SI, DI, BP }};
static struct regclass regclass_32 = { "32-bit", { EAX, EDX, ECX, EBX, ESI, EDI, EBP }};
static struct regclass regclass_64 = { "64-bit", { EAX_EDX, ECX_EBX, ESI_EDI }};
static struct regclass regclass_32_8 = { "32-bit bytes", { EAX, EDX, ECX, EBX }};
static struct regclass *get_regclass_bits(int bits)
{
switch (bits) {
case 8: return ®class_8;
case 16: return ®class_16;
case 64: return ®class_64;
default: return ®class_32;
}
}
static struct regclass *get_regclass(struct expression *expr)
{
return get_regclass_bits(expr->ctype->bit_size);
}
static int register_busy(int regno)
{
if (!test_bit(regno, regs_in_use)) {
struct reg_info *info = reg_info_table + regno;
const unsigned char *regs = info->aliases+1;
while ((regno = *regs) != NOREG) {
regs++;
if (test_bit(regno, regs_in_use))
goto busy;
}
return 0;
}
busy:
return 1;
}
static struct storage *get_reg(struct regclass *class)
{
const unsigned char *regs = class->regs;
int regno;
while ((regno = *regs) != NOREG) {
regs++;
if (register_busy(regno))
continue;
return get_hardreg(hardreg_storage_table + regno, 1);
}
fprintf(stderr, "Ran out of %s registers\n", class->name);
exit(1);
}
static struct storage *get_reg_value(struct storage *value, struct regclass *class)
{
struct reg_info *info;
struct storage *reg;
/* Do we already have it somewhere */
info = value->reg;
if (info && info->contains == value) {
emit_comment("already have register %s", info->name);
return get_hardreg(hardreg_storage_table + info->own_regno, 0);
}
reg = get_reg(class);
emit_move(value, reg, value->ctype, "reload register");
info = reg->reg;
info->contains = value;
value->reg = info;
return reg;
}
static struct storage *temp_from_bits(unsigned int bit_size)
{
return get_reg(get_regclass_bits(bit_size));
}
static inline unsigned int pseudo_offset(struct storage *s)
{
if (s->type != STOR_PSEUDO)
return 123456; /* intentionally bogus value */
return s->offset;
}
static inline unsigned int arg_offset(struct storage *s)
{
if (s->type != STOR_ARG)
return 123456; /* intentionally bogus value */
/* FIXME: this is wrong wrong wrong */
return current_func->stack_size + ((1 + s->idx) * 4);
}
static const char *pretty_offset(int ofs)
{
static char esp_buf[64];
if (ofs)
sprintf(esp_buf, "%d(%%esp)", ofs);
else
strcpy(esp_buf, "(%esp)");
return esp_buf;
}
static void stor_sym_init(struct symbol *sym)
{
struct storage *stor;
struct symbol_private *priv;
priv = calloc(1, sizeof(*priv) + sizeof(*stor));
if (!priv)
die("OOM in stor_sym_init");
stor = (struct storage *) (priv + 1);
priv->addr = stor;
stor->type = STOR_SYM;
stor->sym = sym;
}
static const char *stor_op_name(struct storage *s)
{
static char name[32];
switch (s->type) {
case STOR_PSEUDO:
strcpy(name, pretty_offset((int) pseudo_offset(s)));
break;
case STOR_ARG:
strcpy(name, pretty_offset((int) arg_offset(s)));
break;
case STOR_SYM:
strcpy(name, show_ident(s->sym->ident));
break;
case STOR_REG:
strcpy(name, s->reg->name);
break;
case STOR_VALUE:
sprintf(name, "$%Ld", s->value);
break;
case STOR_LABEL:
sprintf(name, "%s.L%d", s->flags & STOR_LABEL_VAL ? "$" : "",
s->label);
break;
case STOR_LABELSYM:
sprintf(name, "%s.LS%p", s->flags & STOR_LABEL_VAL ? "$" : "",
s->labelsym);
break;
}
return name;
}
static struct atom *new_atom(enum atom_type type)
{
struct atom *atom;
atom = calloc(1, sizeof(*atom)); /* TODO: chunked alloc */
if (!atom)
die("nuclear OOM");
atom->type = type;
return atom;
}
static inline void push_cstring(struct function *f, struct string *str,
int label)
{
struct atom *atom;
atom = new_atom(ATOM_CSTR);
atom->string = str;
atom->label = label;
add_ptr_list(&f->str_list, atom); /* note: _not_ atom_list */
}
static inline void push_atom(struct function *f, struct atom *atom)
{
add_ptr_list(&f->atom_list, atom);
}
static void push_text_atom(struct function *f, const char *text)
{
struct atom *atom = new_atom(ATOM_TEXT);
atom->text = strdup(text);
atom->text_len = strlen(text);
push_atom(f, atom);
}
static struct storage *new_storage(enum storage_type type)
{
struct storage *stor;
stor = calloc(1, sizeof(*stor));
if (!stor)
die("OOM in new_storage");
stor->type = type;
return stor;
}
static struct storage *stack_alloc(int n_bytes)
{
struct function *f = current_func;
struct storage *stor;
assert(f != NULL);
stor = new_storage(STOR_PSEUDO);
stor->type = STOR_PSEUDO;
stor->pseudo = f->pseudo_nr;
stor->offset = f->stack_size; /* FIXME: stack req. natural align */
stor->size = n_bytes;
f->stack_size += n_bytes;
f->pseudo_nr++;
add_ptr_list(&f->pseudo_list, stor);
return stor;
}
static struct storage *new_labelsym(struct symbol *sym)
{
struct storage *stor;
stor = new_storage(STOR_LABELSYM);
if (stor) {
stor->flags |= STOR_WANTS_FREE;
stor->labelsym = sym;
}
return stor;
}
static struct storage *new_val(long long value)
{
struct storage *stor;
stor = new_storage(STOR_VALUE);
if (stor) {
stor->flags |= STOR_WANTS_FREE;
stor->value = value;
}
return stor;
}
static int new_label(void)
{
static int label = 0;
return ++label;
}
static void textbuf_push(struct textbuf **buf_p, const char *text)
{
struct textbuf *tmp, *list = *buf_p;
unsigned int text_len = strlen(text);
unsigned int alloc_len = text_len + 1 + sizeof(*list);
tmp = calloc(1, alloc_len);
if (!tmp)
die("OOM on textbuf alloc");
tmp->text = ((void *) tmp) + sizeof(*tmp);
memcpy(tmp->text, text, text_len + 1);
tmp->len = text_len;
/* add to end of list */
if (!list) {
list = tmp;
tmp->prev = tmp;
} else {
tmp->prev = list->prev;
tmp->prev->next = tmp;
list->prev = tmp;
}
tmp->next = list;
*buf_p = list;
}
static void textbuf_emit(struct textbuf **buf_p)
{
struct textbuf *tmp, *list = *buf_p;
while (list) {
tmp = list;
if (tmp->next == tmp)
list = NULL;
else {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
list = tmp->next;
}
fputs(tmp->text, stdout);
free(tmp);
}
*buf_p = list;
}
static void insn(const char *insn, struct storage *op1, struct storage *op2,
const char *comment_in)
{
struct function *f = current_func;
struct atom *atom = new_atom(ATOM_INSN);
assert(insn != NULL);
strcpy(atom->insn, insn);
if (comment_in && (*comment_in))
strncpy(atom->comment, comment_in,
sizeof(atom->comment) - 1);
atom->op1 = op1;
atom->op2 = op2;
push_atom(f, atom);
}
static void emit_comment(const char *fmt, ...)
{
struct function *f = current_func;
static char tmpbuf[100] = "\t# ";
va_list args;
int i;
va_start(args, fmt);
i = vsnprintf(tmpbuf+3, sizeof(tmpbuf)-4, fmt, args);
va_end(args);
tmpbuf[i+3] = '\n';
tmpbuf[i+4] = '\0';
push_text_atom(f, tmpbuf);
}
static void emit_label (int label, const char *comment)
{
struct function *f = current_func;
char s[64];
if (!comment)
sprintf(s, ".L%d:\n", label);
else
sprintf(s, ".L%d:\t\t\t\t\t# %s\n", label, comment);
push_text_atom(f, s);
}
static void emit_labelsym (struct symbol *sym, const char *comment)
{
struct function *f = current_func;
char s[64];
if (!comment)
sprintf(s, ".LS%p:\n", sym);
else
sprintf(s, ".LS%p:\t\t\t\t# %s\n", sym, comment);
push_text_atom(f, s);
}
void emit_unit_begin(const char *basename)
{
printf("\t.file\t\"%s\"\n", basename);
}
void emit_unit_end(void)
{
textbuf_emit(&unit_post_text);
printf("\t.ident\t\"sparse silly x86 backend (built %s)\"\n", __DATE__);
}
/* conditionally switch sections */
static void emit_section(const char *s)
{
if (s == current_section)
return;
if (current_section && (!strcmp(s, current_section)))
return;
printf("\t%s\n", s);
current_section = s;
}
static void emit_insn_atom(struct function *f, struct atom *atom)
{
char s[128];
char comment[64];
struct storage *op1 = atom->op1;
struct storage *op2 = atom->op2;
if (atom->comment[0])
sprintf(comment, "\t\t# %s", atom->comment);
else
comment[0] = 0;
if (atom->op2) {
char tmp[16];
strcpy(tmp, stor_op_name(op1));
sprintf(s, "\t%s\t%s, %s%s\n",
atom->insn, tmp, stor_op_name(op2), comment);
} else if (atom->op1)
sprintf(s, "\t%s\t%s%s%s\n",
atom->insn, stor_op_name(op1),
comment[0] ? "\t" : "", comment);
else
sprintf(s, "\t%s\t%s%s\n",
atom->insn,
comment[0] ? "\t\t" : "", comment);
write(STDOUT_FILENO, s, strlen(s));
}
static void emit_atom_list(struct function *f)
{
struct atom *atom;
FOR_EACH_PTR(f->atom_list, atom) {
switch (atom->type) {
case ATOM_TEXT: {
ssize_t rc = write(STDOUT_FILENO, atom->text,
atom->text_len);
(void) rc; /* FIXME */
break;
}
case ATOM_INSN:
emit_insn_atom(f, atom);
break;
case ATOM_CSTR:
assert(0);
break;
}
} END_FOR_EACH_PTR(atom);
}
static void emit_string_list(struct function *f)
{
struct atom *atom;
emit_section(".section\t.rodata");
FOR_EACH_PTR(f->str_list, atom) {
/* FIXME: escape " in string */
printf(".L%d:\n", atom->label);
printf("\t.string\t%s\n", show_string(atom->string));
free(atom);
} END_FOR_EACH_PTR(atom);
}
static void func_cleanup(struct function *f)
{
struct storage *stor;
struct atom *atom;
FOR_EACH_PTR(f->pseudo_list, stor) {
free(stor);
} END_FOR_EACH_PTR(stor);
FOR_EACH_PTR(f->atom_list, atom) {
if ((atom->type == ATOM_TEXT) && (atom->text))
free(atom->text);
if (atom->op1 && (atom->op1->flags & STOR_WANTS_FREE))
free(atom->op1);
if (atom->op2 && (atom->op2->flags & STOR_WANTS_FREE))
free(atom->op2);
free(atom);
} END_FOR_EACH_PTR(atom);
free_ptr_list(&f->pseudo_list);
free(f);
}
/* function prologue */
static void emit_func_pre(struct symbol *sym)
{
struct function *f;
struct symbol *arg;
unsigned int i, argc = 0, alloc_len;
unsigned char *mem;
struct symbol_private *privbase;
struct storage *storage_base;
struct symbol *base_type = sym->ctype.base_type;
FOR_EACH_PTR(base_type->arguments, arg) {
argc++;
} END_FOR_EACH_PTR(arg);
alloc_len =
sizeof(*f) +
(argc * sizeof(struct symbol *)) +
(argc * sizeof(struct symbol_private)) +
(argc * sizeof(struct storage));
mem = calloc(1, alloc_len);
if (!mem)
die("OOM on func info");
f = (struct function *) mem;
mem += sizeof(*f);
f->argv = (struct symbol **) mem;
mem += (argc * sizeof(struct symbol *));
privbase = (struct symbol_private *) mem;
mem += (argc * sizeof(struct symbol_private));
storage_base = (struct storage *) mem;
f->argc = argc;
f->ret_target = new_label();
i = 0;
FOR_EACH_PTR(base_type->arguments, arg) {
f->argv[i] = arg;
arg->aux = &privbase[i];
storage_base[i].type = STOR_ARG;
storage_base[i].idx = i;
privbase[i].addr = &storage_base[i];
i++;
} END_FOR_EACH_PTR(arg);
assert(current_func == NULL);
current_func = f;
}
/* function epilogue */
static void emit_func_post(struct symbol *sym)
{
const char *name = show_ident(sym->ident);
struct function *f = current_func;
int stack_size = f->stack_size;
if (f->str_list)
emit_string_list(f);
/* function prologue */
emit_section(".text");
if ((sym->ctype.modifiers & MOD_STATIC) == 0)
printf(".globl %s\n", name);
printf("\t.type\t%s, @function\n", name);
printf("%s:\n", name);
if (stack_size) {
char pseudo_const[16];
sprintf(pseudo_const, "$%d", stack_size);
printf("\tsubl\t%s, %%esp\n", pseudo_const);
}
/* function epilogue */
/* jump target for 'return' statements */
emit_label(f->ret_target, NULL);
if (stack_size) {
struct storage *val;
val = new_storage(STOR_VALUE);
val->value = (long long) (stack_size);
val->flags = STOR_WANTS_FREE;
insn("addl", val, REG_ESP, NULL);
}
insn("ret", NULL, NULL, NULL);
/* output everything to stdout */
fflush(stdout); /* paranoia; needed? */
emit_atom_list(f);
/* function footer */
name = show_ident(sym->ident);
printf("\t.size\t%s, .-%s\n", name, name);
func_cleanup(f);
current_func = NULL;
}
/* emit object (a.k.a. variable, a.k.a. data) prologue */
static void emit_object_pre(const char *name, unsigned long modifiers,
unsigned long alignment, unsigned int byte_size)
{
if ((modifiers & MOD_STATIC) == 0)
printf(".globl %s\n", name);
emit_section(".data");
if (alignment)
printf("\t.align %lu\n", alignment);
printf("\t.type\t%s, @object\n", name);
printf("\t.size\t%s, %d\n", name, byte_size);
printf("%s:\n", name);
}
/* emit value (only) for an initializer scalar */
static void emit_scalar(struct expression *expr, unsigned int bit_size)
{
const char *type;
long long ll;
assert(expr->type == EXPR_VALUE);
if (expr->value == 0ULL) {
printf("\t.zero\t%d\n", bit_size / 8);
return;
}
ll = (long long) expr->value;
switch (bit_size) {
case 8: type = "byte"; ll = (char) ll; break;
case 16: type = "value"; ll = (short) ll; break;
case 32: type = "long"; ll = (int) ll; break;
case 64: type = "quad"; break;
default: type = NULL; break;
}
assert(type != NULL);
printf("\t.%s\t%Ld\n", type, ll);
}
static void emit_global_noinit(const char *name, unsigned long modifiers,
unsigned long alignment, unsigned int byte_size)
{
char s[64];
if (modifiers & MOD_STATIC) {
sprintf(s, "\t.local\t%s\n", name);
textbuf_push(&unit_post_text, s);
}
if (alignment)
sprintf(s, "\t.comm\t%s,%d,%lu\n", name, byte_size, alignment);
else
sprintf(s, "\t.comm\t%s,%d\n", name, byte_size);
textbuf_push(&unit_post_text, s);
}
static int ea_current, ea_last;
static void emit_initializer(struct symbol *sym,
struct expression *expr)
{
int distance = ea_current - ea_last - 1;
if (distance > 0)
printf("\t.zero\t%d\n", (sym->bit_size / 8) * distance);
if (expr->type == EXPR_VALUE) {
struct symbol *base_type = sym->ctype.base_type;
assert(base_type != NULL);
emit_scalar(expr, sym->bit_size / get_expression_value(base_type->array_size));
return;
}
if (expr->type != EXPR_INITIALIZER)
return;
assert(0); /* FIXME */
}
static int sort_array_cmp(const struct expression *a,
const struct expression *b)
{
int a_ofs = 0, b_ofs = 0;
if (a->type == EXPR_POS)
a_ofs = (int) a->init_offset;
if (b->type == EXPR_POS)
b_ofs = (int) b->init_offset;
return a_ofs - b_ofs;
}
/* move to front-end? */
static void sort_array(struct expression *expr)
{
struct expression *entry, **list;
unsigned int elem, sorted, i;
elem = 0;
FOR_EACH_PTR(expr->expr_list, entry) {
elem++;
} END_FOR_EACH_PTR(entry);
if (!elem)
return;
list = malloc(sizeof(entry) * elem);
if (!list)
die("OOM in sort_array");
/* this code is no doubt evil and ignores EXPR_INDEX possibly
* to its detriment and other nasty things. improvements
* welcome.
*/
i = 0;
sorted = 0;
FOR_EACH_PTR(expr->expr_list, entry) {
if ((entry->type == EXPR_POS) || (entry->type == EXPR_VALUE)) {
/* add entry to list[], in sorted order */
if (sorted == 0) {
list[0] = entry;
sorted = 1;
} else {
for (i = 0; i < sorted; i++)
if (sort_array_cmp(entry, list[i]) <= 0)
break;
/* If inserting into the middle of list[]
* instead of appending, we memmove.
* This is ugly, but thankfully
* uncommon. Input data with tons of
* entries very rarely have explicit
* offsets. convert to qsort eventually...
*/
if (i != sorted)
memmove(&list[i + 1], &list[i],
(sorted - i) * sizeof(entry));
list[i] = entry;
sorted++;
}
}
} END_FOR_EACH_PTR(entry);
i = 0;
FOR_EACH_PTR(expr->expr_list, entry) {
if ((entry->type == EXPR_POS) || (entry->type == EXPR_VALUE))
*THIS_ADDRESS(entry) = list[i++];
} END_FOR_EACH_PTR(entry);
}
static void emit_array(struct symbol *sym)
{
struct symbol *base_type = sym->ctype.base_type;
struct expression *expr = sym->initializer;
struct expression *entry;
assert(base_type != NULL);
stor_sym_init(sym);
ea_last = -1;
emit_object_pre(show_ident(sym->ident), sym->ctype.modifiers,
sym->ctype.alignment,
sym->bit_size / 8);
sort_array(expr);
FOR_EACH_PTR(expr->expr_list, entry) {
if (entry->type == EXPR_VALUE) {
ea_current = 0;
emit_initializer(sym, entry);
ea_last = ea_current;
} else if (entry->type == EXPR_POS) {
ea_current =
entry->init_offset / (base_type->bit_size / 8);
emit_initializer(sym, entry->init_expr);
ea_last = ea_current;
}
} END_FOR_EACH_PTR(entry);
}
void emit_one_symbol(struct symbol *sym)
{
x86_symbol(sym);
}
static void emit_copy(struct storage *dest, struct storage *src,
struct symbol *ctype)
{
struct storage *reg = NULL;
unsigned int bit_size;
/* FIXME: Bitfield copy! */
bit_size = src->size * 8;
if (!bit_size)
bit_size = 32;
if ((src->type == STOR_ARG) && (bit_size < 32))
bit_size = 32;
reg = temp_from_bits(bit_size);
emit_move(src, reg, ctype, "begin copy ..");
bit_size = dest->size * 8;
if (!bit_size)
bit_size = 32;
if ((dest->type == STOR_ARG) && (bit_size < 32))
bit_size = 32;
emit_move(reg, dest, ctype, ".... end copy");
put_reg(reg);
}
static void emit_store(struct expression *dest_expr, struct storage *dest,
struct storage *src, int bits)
{
/* FIXME: Bitfield store! */
printf("\tst.%d\t\tv%d,[v%d]\n", bits, src->pseudo, dest->pseudo);
}
static void emit_scalar_noinit(struct symbol *sym)
{
emit_global_noinit(show_ident(sym->ident),
sym->ctype.modifiers, sym->ctype.alignment,
sym->bit_size / 8);
stor_sym_init(sym);
}
static void emit_array_noinit(struct symbol *sym)
{
emit_global_noinit(show_ident(sym->ident),
sym->ctype.modifiers, sym->ctype.alignment,
get_expression_value(sym->array_size) * (sym->bit_size / 8));
stor_sym_init(sym);
}
static const char *opbits(const char *insn, unsigned int bits)
{
static char opbits_str[32];
char c;
switch (bits) {
case 8: c = 'b'; break;
case 16: c = 'w'; break;
case 32: c = 'l'; break;
case 64: c = 'q'; break;
default: abort(); break;
}
sprintf(opbits_str, "%s%c", insn, c);
return opbits_str;
}
static void emit_move(struct storage *src, struct storage *dest,
struct symbol *ctype, const char *comment)
{
unsigned int bits;
unsigned int is_signed;
unsigned int is_dest = (src->type == STOR_REG);
const char *opname;
if (ctype) {
bits = ctype->bit_size;
is_signed = type_is_signed(ctype);
} else {
bits = 32;
is_signed = 0;
}
/*
* Are we moving from a register to a register?
* Make the new reg to be the "cache".
*/
if ((dest->type == STOR_REG) && (src->type == STOR_REG)) {
struct storage *backing;
reg_reg_move:
if (dest == src)
return;
backing = src->reg->contains;
if (backing) {
/* Is it still valid? */
if (backing->reg != src->reg)
backing = NULL;
else
backing->reg = dest->reg;
}
dest->reg->contains = backing;
insn("mov", src, dest, NULL);
return;
}
/*
* Are we moving to a register from a non-reg?
*
* See if we have the non-reg source already cached
* in a register..
*/
if (dest->type == STOR_REG) {
if (src->reg) {
struct reg_info *info = src->reg;
if (info->contains == src) {
src = reginfo_reg(info);
goto reg_reg_move;
}
}
dest->reg->contains = src;
src->reg = dest->reg;
}
if (src->type == STOR_REG) {
/* We could just mark the register dirty here and do lazy store.. */
src->reg->contains = dest;
dest->reg = src->reg;
}
if ((bits == 8) || (bits == 16)) {
if (is_dest)
opname = "mov";
else
opname = is_signed ? "movsx" : "movzx";
} else
opname = "mov";
insn(opbits(opname, bits), src, dest, comment);
}
static struct storage *emit_compare(struct expression *expr)
{
struct storage *left = x86_expression(expr->left);
struct storage *right = x86_expression(expr->right);
struct storage *reg1, *reg2;
struct storage *new, *val;
const char *opname = NULL;
unsigned int right_bits = expr->right->ctype->bit_size;
switch(expr->op) {
case '<': opname = "setl"; break;
case '>': opname = "setg"; break;
case SPECIAL_LTE:
opname = "setle"; break;
case SPECIAL_GTE:
opname = "setge"; break;
case SPECIAL_EQUAL: opname = "sete"; break;
case SPECIAL_NOTEQUAL: opname = "setne"; break;
case SPECIAL_UNSIGNED_LT:
opname = "setb"; break;
case SPECIAL_UNSIGNED_GT:
opname = "seta"; break;
case SPECIAL_UNSIGNED_LTE:
opname = "setb"; break;
case SPECIAL_UNSIGNED_GTE:
opname = "setae"; break;
default:
assert(0);
break;
}
/* init EDX to 0 */
val = new_storage(STOR_VALUE);
val->flags = STOR_WANTS_FREE;
reg1 = get_reg(®class_32_8);
emit_move(val, reg1, NULL, NULL);
/* move op1 into EAX */
reg2 = get_reg_value(left, get_regclass(expr->left));
/* perform comparison, RHS (op1, right) and LHS (op2, EAX) */
insn(opbits("cmp", right_bits), right, reg2, NULL);
put_reg(reg2);
/* store result of operation, 0 or 1, in DL using SETcc */
insn(opname, byte_reg(reg1), NULL, NULL);
/* finally, store the result (DL) in a new pseudo / stack slot */
new = stack_alloc(4);
emit_move(reg1, new, NULL, "end EXPR_COMPARE");
put_reg(reg1);
return new;
}
static struct storage *emit_value(struct expression *expr)
{
#if 0 /* old and slow way */
struct storage *new = stack_alloc(4);
struct storage *val;
val = new_storage(STOR_VALUE);
val->value = (long long) expr->value;
val->flags = STOR_WANTS_FREE;
insn("movl", val, new, NULL);
return new;
#else
struct storage *val;
val = new_storage(STOR_VALUE);
val->value = (long long) expr->value;
return val; /* FIXME: memory leak */
#endif
}
static struct storage *emit_divide(struct expression *expr, struct storage *left, struct storage *right)
{
struct storage *eax_edx;
struct storage *reg, *new;
struct storage *val = new_storage(STOR_VALUE);
emit_comment("begin DIVIDE");
eax_edx = get_hardreg(hardreg_storage_table + EAX_EDX, 1);
/* init EDX to 0 */
val = new_storage(STOR_VALUE);
val->flags = STOR_WANTS_FREE;
emit_move(val, REG_EDX, NULL, NULL);
new = stack_alloc(expr->ctype->bit_size / 8);
/* EAX is dividend */
emit_move(left, REG_EAX, NULL, NULL);
reg = get_reg_value(right, ®class_32);
/* perform binop */
insn("div", reg, REG_EAX, NULL);
put_reg(reg);
reg = REG_EAX;
if (expr->op == '%')
reg = REG_EDX;
emit_move(reg, new, NULL, NULL);
put_reg(eax_edx);
emit_comment("end DIVIDE");
return new;
}
static struct storage *emit_binop(struct expression *expr)
{
struct storage *left = x86_expression(expr->left);
struct storage *right = x86_expression(expr->right);
struct storage *new;
struct storage *dest, *src;
const char *opname = NULL;
const char *suffix = NULL;
char opstr[16];
int is_signed;
/* Divides have special register constraints */
if ((expr->op == '/') || (expr->op == '%'))
return emit_divide(expr, left, right);
is_signed = type_is_signed(expr->ctype);
switch (expr->op) {
case '+':
opname = "add";
break;
case '-':
opname = "sub";
break;
case '&':
opname = "and";
break;
case '|':
opname = "or";
break;
case '^':
opname = "xor";
break;
case SPECIAL_LEFTSHIFT:
opname = "shl";
break;
case SPECIAL_RIGHTSHIFT:
if (is_signed)
opname = "sar";
else
opname = "shr";
break;
case '*':
if (is_signed)
opname = "imul";
else
opname = "mul";
break;
case SPECIAL_LOGICAL_AND:
warning(expr->pos, "bogus bitwise and for logical op (should use '2*setne + and' or something)");
opname = "and";
break;
case SPECIAL_LOGICAL_OR:
warning(expr->pos, "bogus bitwise or for logical op (should use 'or + setne' or something)");
opname = "or";
break;
default:
error_die(expr->pos, "unhandled binop '%s'\n", show_special(expr->op));
break;
}
dest = get_reg_value(right, ®class_32);
src = get_reg_value(left, ®class_32);
switch (expr->ctype->bit_size) {
case 8:
suffix = "b";
break;
case 16:
suffix = "w";
break;
case 32:
suffix = "l";
break;
case 64:
suffix = "q"; /* FIXME */
break;
default:
assert(0);
break;
}
snprintf(opstr, sizeof(opstr), "%s%s", opname, suffix);
/* perform binop */
insn(opstr, src, dest, NULL);
put_reg(src);
/* store result in new pseudo / stack slot */
new = stack_alloc(expr->ctype->bit_size / 8);
emit_move(dest, new, NULL, "end EXPR_BINOP");
put_reg(dest);
return new;
}
static int emit_conditional_test(struct storage *val)
{
struct storage *reg;
struct storage *target_val;
int target_false;
/* load result into EAX */
emit_comment("begin if/conditional");
reg = get_reg_value(val, ®class_32);
/* compare result with zero */
insn("test", reg, reg, NULL);
put_reg(reg);
/* create conditional-failed label to jump to */
target_false = new_label();
target_val = new_storage(STOR_LABEL);
target_val->label = target_false;
target_val->flags = STOR_WANTS_FREE;
insn("jz", target_val, NULL, NULL);
return target_false;
}
static int emit_conditional_end(int target_false)
{
struct storage *cond_end_st;
int cond_end;
/* finished generating code for if-true statement.
* add a jump-to-end jump to avoid falling through
* to the if-false statement code.
*/
cond_end = new_label();
cond_end_st = new_storage(STOR_LABEL);
cond_end_st->label = cond_end;
cond_end_st->flags = STOR_WANTS_FREE;
insn("jmp", cond_end_st, NULL, NULL);
/* if we have both if-true and if-false statements,
* the failed-conditional case will fall through to here
*/
emit_label(target_false, NULL);
return cond_end;
}
static void emit_if_conditional(struct statement *stmt)
{
struct storage *val;
int cond_end;
/* emit test portion of conditional */
val = x86_expression(stmt->if_conditional);
cond_end = emit_conditional_test(val);
/* emit if-true statement */
x86_statement(stmt->if_true);
/* emit if-false statement, if present */
if (stmt->if_false) {
cond_end = emit_conditional_end(cond_end);
x86_statement(stmt->if_false);
}
/* end of conditional; jump target for if-true branch */
emit_label(cond_end, "end if");
}
static struct storage *emit_inc_dec(struct expression *expr, int postop)
{
struct storage *addr = x86_address_gen(expr->unop);
struct storage *retval;
char opname[16];
strcpy(opname, opbits(expr->op == SPECIAL_INCREMENT ? "inc" : "dec",
expr->ctype->bit_size));
if (postop) {
struct storage *new = stack_alloc(4);
emit_copy(new, addr, expr->unop->ctype);
retval = new;
} else
retval = addr;
insn(opname, addr, NULL, NULL);
return retval;
}
static struct storage *emit_postop(struct expression *expr)
{
return emit_inc_dec(expr, 1);
}
static struct storage *emit_return_stmt(struct statement *stmt)
{
struct function *f = current_func;
struct expression *expr = stmt->ret_value;
struct storage *val = NULL, *jmplbl;
if (expr && expr->ctype) {
val = x86_expression(expr);
assert(val != NULL);
emit_move(val, REG_EAX, expr->ctype, "return");
}
jmplbl = new_storage(STOR_LABEL);
jmplbl->flags |= STOR_WANTS_FREE;
jmplbl->label = f->ret_target;
insn("jmp", jmplbl, NULL, NULL);
return val;
}
static struct storage *emit_conditional_expr(struct expression *expr)
{
struct storage *cond, *true = NULL, *false = NULL;
struct storage *new = stack_alloc(expr->ctype->bit_size / 8);
int target_false, cond_end;
/* evaluate conditional */
cond = x86_expression(expr->conditional);
target_false = emit_conditional_test(cond);
/* handle if-true part of the expression */
true = x86_expression(expr->cond_true);
emit_copy(new, true, expr->ctype);
cond_end = emit_conditional_end(target_false);
/* handle if-false part of the expression */
false = x86_expression(expr->cond_false);
emit_copy(new, false, expr->ctype);
/* end of conditional; jump target for if-true branch */
emit_label(cond_end, "end conditional");
return new;
}
static struct storage *emit_select_expr(struct expression *expr)
{
struct storage *cond = x86_expression(expr->conditional);
struct storage *true = x86_expression(expr->cond_true);
struct storage *false = x86_expression(expr->cond_false);
struct storage *reg_cond, *reg_true, *reg_false;
struct storage *new = stack_alloc(4);
emit_comment("begin SELECT");
reg_cond = get_reg_value(cond, get_regclass(expr->conditional));
reg_true = get_reg_value(true, get_regclass(expr));
reg_false = get_reg_value(false, get_regclass(expr));
/*
* Do the actual select: check the conditional for zero,
* move false over true if zero
*/
insn("test", reg_cond, reg_cond, NULL);
insn("cmovz", reg_false, reg_true, NULL);
/* Store it back */
emit_move(reg_true, new, expr->ctype, NULL);
put_reg(reg_cond);
put_reg(reg_true);
put_reg(reg_false);
emit_comment("end SELECT");
return new;
}
static struct storage *emit_symbol_expr_init(struct symbol *sym)
{
struct expression *expr = sym->initializer;
struct symbol_private *priv = sym->aux;
if (priv == NULL) {
priv = calloc(1, sizeof(*priv));
sym->aux = priv;
if (expr == NULL) {
struct storage *new = stack_alloc(4);
fprintf(stderr, "FIXME! no value for symbol %s. creating pseudo %d (stack offset %d)\n",
show_ident(sym->ident),
new->pseudo, new->pseudo * 4);
priv->addr = new;
} else {
priv->addr = x86_expression(expr);
}
}
return priv->addr;
}
static struct storage *emit_string_expr(struct expression *expr)
{
struct function *f = current_func;
int label = new_label();
struct storage *new;
push_cstring(f, expr->string, label);
new = new_storage(STOR_LABEL);
new->label = label;
new->flags = STOR_LABEL_VAL | STOR_WANTS_FREE;
return new;
}
static struct storage *emit_cast_expr(struct expression *expr)
{
struct symbol *old_type, *new_type;
struct storage *op = x86_expression(expr->cast_expression);
int oldbits, newbits;
struct storage *new;
old_type = expr->cast_expression->ctype;
new_type = expr->cast_type;
oldbits = old_type->bit_size;
newbits = new_type->bit_size;
if (oldbits >= newbits)
return op;
emit_move(op, REG_EAX, old_type, "begin cast ..");
new = stack_alloc(newbits / 8);
emit_move(REG_EAX, new, new_type, ".... end cast");
return new;
}
static struct storage *emit_regular_preop(struct expression *expr)
{
struct storage *target = x86_expression(expr->unop);
struct storage *val, *new = stack_alloc(4);
const char *opname = NULL;
switch (expr->op) {
case '!':
val = new_storage(STOR_VALUE);
val->flags = STOR_WANTS_FREE;
emit_move(val, REG_EDX, NULL, NULL);
emit_move(target, REG_EAX, expr->unop->ctype, NULL);
insn("test", REG_EAX, REG_EAX, NULL);
insn("setz", REG_DL, NULL, NULL);
emit_move(REG_EDX, new, expr->unop->ctype, NULL);
break;
case '~':
opname = "not";
case '-':
if (!opname)
opname = "neg";
emit_move(target, REG_EAX, expr->unop->ctype, NULL);
insn(opname, REG_EAX, NULL, NULL);
emit_move(REG_EAX, new, expr->unop->ctype, NULL);
break;
default:
assert(0);
break;
}
return new;
}
static void emit_case_statement(struct statement *stmt)
{
emit_labelsym(stmt->case_label, NULL);
x86_statement(stmt->case_statement);
}
static void emit_switch_statement(struct statement *stmt)
{
struct storage *val = x86_expression(stmt->switch_expression);
struct symbol *sym, *default_sym = NULL;
struct storage *labelsym, *label;
int switch_end = 0;
emit_move(val, REG_EAX, stmt->switch_expression->ctype, "begin case");
/*
* This is where a _real_ back-end would go through the
* cases to decide whether to use a lookup table or a
* series of comparisons etc
*/
FOR_EACH_PTR(stmt->switch_case->symbol_list, sym) {
struct statement *case_stmt = sym->stmt;
struct expression *expr = case_stmt->case_expression;
struct expression *to = case_stmt->case_to;
/* default: */
if (!expr)
default_sym = sym;
/* case NNN: */
else {
struct storage *case_val = new_val(expr->value);
assert (expr->type == EXPR_VALUE);
insn("cmpl", case_val, REG_EAX, NULL);
if (!to) {
labelsym = new_labelsym(sym);
insn("je", labelsym, NULL, NULL);
} else {
int next_test;
label = new_storage(STOR_LABEL);
label->flags |= STOR_WANTS_FREE;
label->label = next_test = new_label();
/* FIXME: signed/unsigned */
insn("jl", label, NULL, NULL);
case_val = new_val(to->value);
insn("cmpl", case_val, REG_EAX, NULL);
/* TODO: implement and use refcounting... */
label = new_storage(STOR_LABEL);
label->flags |= STOR_WANTS_FREE;
label->label = next_test;
/* FIXME: signed/unsigned */
insn("jg", label, NULL, NULL);
labelsym = new_labelsym(sym);
insn("jmp", labelsym, NULL, NULL);
emit_label(next_test, NULL);
}
}
} END_FOR_EACH_PTR(sym);
if (default_sym) {
labelsym = new_labelsym(default_sym);
insn("jmp", labelsym, NULL, "default");
} else {
label = new_storage(STOR_LABEL);
label->flags |= STOR_WANTS_FREE;
label->label = switch_end = new_label();
insn("jmp", label, NULL, "goto end of switch");
}
x86_statement(stmt->switch_statement);
if (stmt->switch_break->used)
emit_labelsym(stmt->switch_break, NULL);
if (switch_end)
emit_label(switch_end, NULL);
}
static void x86_struct_member(struct symbol *sym)
{
printf("\t%s:%d:%ld at offset %ld.%d", show_ident(sym->ident), sym->bit_size, sym->ctype.alignment, sym->offset, sym->bit_offset);
printf("\n");
}
static void x86_symbol(struct symbol *sym)
{
struct symbol *type;
if (!sym)
return;
type = sym->ctype.base_type;
if (!type)
return;
/*
* Show actual implementation information
*/
switch (type->type) {
case SYM_ARRAY:
if (sym->initializer)
emit_array(sym);
else
emit_array_noinit(sym);
break;
case SYM_BASETYPE:
if (sym->initializer) {
emit_object_pre(show_ident(sym->ident),
sym->ctype.modifiers,
sym->ctype.alignment,
sym->bit_size / 8);
emit_scalar(sym->initializer, sym->bit_size);
stor_sym_init(sym);
} else
emit_scalar_noinit(sym);
break;
case SYM_STRUCT:
case SYM_UNION: {
struct symbol *member;
printf(" {\n");
FOR_EACH_PTR(type->symbol_list, member) {
x86_struct_member(member);
} END_FOR_EACH_PTR(member);
printf("}\n");
break;
}
case SYM_FN: {
struct statement *stmt = type->stmt;
if (stmt) {
emit_func_pre(sym);
x86_statement(stmt);
emit_func_post(sym);
}
break;
}
default:
break;
}
if (sym->initializer && (type->type != SYM_BASETYPE) &&
(type->type != SYM_ARRAY)) {
printf(" = \n");
x86_expression(sym->initializer);
}
}
static void x86_symbol_init(struct symbol *sym);
static void x86_symbol_decl(struct symbol_list *syms)
{
struct symbol *sym;
FOR_EACH_PTR(syms, sym) {
x86_symbol_init(sym);
} END_FOR_EACH_PTR(sym);
}
static void loopstk_push(int cont_lbl, int loop_bottom_lbl)
{
struct function *f = current_func;
struct loop_stack *ls;
ls = malloc(sizeof(*ls));
ls->continue_lbl = cont_lbl;
ls->loop_bottom_lbl = loop_bottom_lbl;
ls->next = f->loop_stack;
f->loop_stack = ls;
}
static void loopstk_pop(void)
{
struct function *f = current_func;
struct loop_stack *ls;
assert(f->loop_stack != NULL);
ls = f->loop_stack;
f->loop_stack = f->loop_stack->next;
free(ls);
}
static int loopstk_break(void)
{
return current_func->loop_stack->loop_bottom_lbl;
}
static int loopstk_continue(void)
{
return current_func->loop_stack->continue_lbl;
}
static void emit_loop(struct statement *stmt)
{
struct statement *pre_statement = stmt->iterator_pre_statement;
struct expression *pre_condition = stmt->iterator_pre_condition;
struct statement *statement = stmt->iterator_statement;
struct statement *post_statement = stmt->iterator_post_statement;
struct expression *post_condition = stmt->iterator_post_condition;
int loop_top = 0, loop_bottom, loop_continue;
int have_bottom = 0;
struct storage *val;
loop_bottom = new_label();
loop_continue = new_label();
loopstk_push(loop_continue, loop_bottom);
x86_symbol_decl(stmt->iterator_syms);
x86_statement(pre_statement);
if (pre_condition) {
if (pre_condition->type == EXPR_VALUE) {
if (!pre_condition->value) {
struct storage *lbv;
lbv = new_storage(STOR_LABEL);
lbv->label = loop_bottom;
lbv->flags = STOR_WANTS_FREE;
insn("jmp", lbv, NULL, "go to loop bottom");
have_bottom = 1;
}
} else {
struct storage *lbv = new_storage(STOR_LABEL);
lbv->label = loop_bottom;
lbv->flags = STOR_WANTS_FREE;
have_bottom = 1;
val = x86_expression(pre_condition);
emit_move(val, REG_EAX, NULL, "loop pre condition");
insn("test", REG_EAX, REG_EAX, NULL);
insn("jz", lbv, NULL, NULL);
}
}
if (!post_condition || post_condition->type != EXPR_VALUE || post_condition->value) {
loop_top = new_label();
emit_label(loop_top, "loop top");
}
x86_statement(statement);
if (stmt->iterator_continue->used)
emit_label(loop_continue, "'continue' iterator");
x86_statement(post_statement);
if (!post_condition) {
struct storage *lbv = new_storage(STOR_LABEL);
lbv->label = loop_top;
lbv->flags = STOR_WANTS_FREE;
insn("jmp", lbv, NULL, "go to loop top");
} else if (post_condition->type == EXPR_VALUE) {
if (post_condition->value) {
struct storage *lbv = new_storage(STOR_LABEL);
lbv->label = loop_top;
lbv->flags = STOR_WANTS_FREE;
insn("jmp", lbv, NULL, "go to loop top");
}
} else {
struct storage *lbv = new_storage(STOR_LABEL);
lbv->label = loop_top;
lbv->flags = STOR_WANTS_FREE;
val = x86_expression(post_condition);
emit_move(val, REG_EAX, NULL, "loop post condition");
insn("test", REG_EAX, REG_EAX, NULL);
insn("jnz", lbv, NULL, NULL);
}
if (have_bottom || stmt->iterator_break->used)
emit_label(loop_bottom, "loop bottom");
loopstk_pop();
}
/*
* Print out a statement
*/
static struct storage *x86_statement(struct statement *stmt)
{
if (!stmt)
return NULL;
switch (stmt->type) {
default:
return NULL;
case STMT_RETURN:
return emit_return_stmt(stmt);
case STMT_DECLARATION:
x86_symbol_decl(stmt->declaration);
break;
case STMT_COMPOUND: {
struct statement *s;
struct storage *last = NULL;
FOR_EACH_PTR(stmt->stmts, s) {
last = x86_statement(s);
} END_FOR_EACH_PTR(s);
return last;
}
case STMT_EXPRESSION:
return x86_expression(stmt->expression);
case STMT_IF:
emit_if_conditional(stmt);
return NULL;
case STMT_CASE:
emit_case_statement(stmt);
break;
case STMT_SWITCH:
emit_switch_statement(stmt);
break;
case STMT_ITERATOR:
emit_loop(stmt);
break;
case STMT_NONE:
break;
case STMT_LABEL:
printf(".L%p:\n", stmt->label_identifier);
x86_statement(stmt->label_statement);
break;
case STMT_GOTO:
if (stmt->goto_expression) {
struct storage *val = x86_expression(stmt->goto_expression);
printf("\tgoto *v%d\n", val->pseudo);
} else if (!strcmp("break", show_ident(stmt->goto_label->ident))) {
struct storage *lbv = new_storage(STOR_LABEL);
lbv->label = loopstk_break();
lbv->flags = STOR_WANTS_FREE;
insn("jmp", lbv, NULL, "'break'; go to loop bottom");
} else if (!strcmp("continue", show_ident(stmt->goto_label->ident))) {
struct storage *lbv = new_storage(STOR_LABEL);
lbv->label = loopstk_continue();
lbv->flags = STOR_WANTS_FREE;
insn("jmp", lbv, NULL, "'continue'; go to loop top");
} else {
struct storage *labelsym = new_labelsym(stmt->goto_label);
insn("jmp", labelsym, NULL, NULL);
}
break;
case STMT_ASM:
printf("\tasm( .... )\n");
break;
}
return NULL;
}
static struct storage *x86_call_expression(struct expression *expr)
{
struct function *f = current_func;
struct symbol *direct;
struct expression *arg, *fn;
struct storage *retval, *fncall;
int framesize;
char s[64];
if (!expr->ctype) {
warning(expr->pos, "\tcall with no type!");
return NULL;
}
framesize = 0;
FOR_EACH_PTR_REVERSE(expr->args, arg) {
struct storage *new = x86_expression(arg);
int size = arg->ctype->bit_size;
/*
* FIXME: i386 SysV ABI dictates that values
* smaller than 32 bits should be placed onto
* the stack as 32-bit objects. We should not
* blindly do a 32-bit push on objects smaller
* than 32 bits.
*/
if (size < 32)
size = 32;
insn("pushl", new, NULL,
!framesize ? "begin function call" : NULL);
framesize += size >> 3;
} END_FOR_EACH_PTR_REVERSE(arg);
fn = expr->fn;
/* Remove dereference, if any */
direct = NULL;
if (fn->type == EXPR_PREOP) {
if (fn->unop->type == EXPR_SYMBOL) {
struct symbol *sym = fn->unop->symbol;
if (sym->ctype.base_type->type == SYM_FN)
direct = sym;
}
}
if (direct) {
struct storage *direct_stor = new_storage(STOR_SYM);
direct_stor->flags |= STOR_WANTS_FREE;
direct_stor->sym = direct;
insn("call", direct_stor, NULL, NULL);
} else {
fncall = x86_expression(fn);
emit_move(fncall, REG_EAX, fn->ctype, NULL);
strcpy(s, "\tcall\t*%eax\n");
push_text_atom(f, s);
}
/* FIXME: pay attention to BITS_IN_POINTER */
if (framesize) {
struct storage *val = new_storage(STOR_VALUE);
val->value = (long long) framesize;
val->flags = STOR_WANTS_FREE;
insn("addl", val, REG_ESP, NULL);
}
retval = stack_alloc(4);
emit_move(REG_EAX, retval, NULL, "end function call");
return retval;
}
static struct storage *x86_address_gen(struct expression *expr)
{
struct function *f = current_func;
struct storage *addr;
struct storage *new;
char s[32];
addr = x86_expression(expr->unop);
if (expr->unop->type == EXPR_SYMBOL)
return addr;
emit_move(addr, REG_EAX, NULL, "begin deref ..");
/* FIXME: operand size */
strcpy(s, "\tmovl\t(%eax), %ecx\n");
push_text_atom(f, s);
new = stack_alloc(4);
emit_move(REG_ECX, new, NULL, ".... end deref");
return new;
}
static struct storage *x86_assignment(struct expression *expr)
{
struct expression *target = expr->left;
struct storage *val, *addr;
if (!expr->ctype)
return NULL;
val = x86_expression(expr->right);
addr = x86_address_gen(target);
switch (val->type) {
/* copy, where both operands are memory */
case STOR_PSEUDO:
case STOR_ARG:
emit_copy(addr, val, expr->ctype);
break;
/* copy, one or zero operands are memory */
case STOR_REG:
case STOR_SYM:
case STOR_VALUE:
case STOR_LABEL:
emit_move(val, addr, expr->left->ctype, NULL);
break;
case STOR_LABELSYM:
assert(0);
break;
}
return val;
}
static int x86_initialization(struct symbol *sym, struct expression *expr)
{
struct storage *val, *addr;
int bits;
if (!expr->ctype)
return 0;
bits = expr->ctype->bit_size;
val = x86_expression(expr);
addr = x86_symbol_expr(sym);
// FIXME! The "target" expression is for bitfield store information.
// Leave it NULL, which works fine.
emit_store(NULL, addr, val, bits);
return 0;
}
static struct storage *x86_access(struct expression *expr)
{
return x86_address_gen(expr);
}
static struct storage *x86_preop(struct expression *expr)
{
/*
* '*' is an lvalue access, and is fundamentally different
* from an arithmetic operation. Maybe it should have an
* expression type of its own..
*/
if (expr->op == '*')
return x86_access(expr);
if (expr->op == SPECIAL_INCREMENT || expr->op == SPECIAL_DECREMENT)
return emit_inc_dec(expr, 0);
return emit_regular_preop(expr);
}
static struct storage *x86_symbol_expr(struct symbol *sym)
{
struct storage *new = stack_alloc(4);
if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_EXTERN | MOD_STATIC)) {
printf("\tmovi.%d\t\tv%d,$%s\n", bits_in_pointer, new->pseudo, show_ident(sym->ident));
return new;
}
if (sym->ctype.modifiers & MOD_ADDRESSABLE) {
printf("\taddi.%d\t\tv%d,vFP,$%lld\n", bits_in_pointer, new->pseudo, sym->value);
return new;
}
printf("\taddi.%d\t\tv%d,vFP,$offsetof(%s:%p)\n", bits_in_pointer, new->pseudo, show_ident(sym->ident), sym);
return new;
}
static void x86_symbol_init(struct symbol *sym)
{
struct symbol_private *priv = sym->aux;
struct expression *expr = sym->initializer;
struct storage *new;
if (expr)
new = x86_expression(expr);
else
new = stack_alloc(sym->bit_size / 8);
if (!priv) {
priv = calloc(1, sizeof(*priv));
sym->aux = priv;
/* FIXME: leak! we don't free... */
/* (well, we don't free symbols either) */
}
priv->addr = new;
}
static int type_is_signed(struct symbol *sym)
{
if (sym->type == SYM_NODE)
sym = sym->ctype.base_type;
if (sym->type == SYM_PTR)
return 0;
return !(sym->ctype.modifiers & MOD_UNSIGNED);
}
static struct storage *x86_label_expr(struct expression *expr)
{
struct storage *new = stack_alloc(4);
printf("\tmovi.%d\t\tv%d,.L%p\n", bits_in_pointer, new->pseudo, expr->label_symbol);
return new;
}
static struct storage *x86_statement_expr(struct expression *expr)
{
return x86_statement(expr->statement);
}
static int x86_position_expr(struct expression *expr, struct symbol *base)
{
struct storage *new = x86_expression(expr->init_expr);
struct symbol *ctype = expr->init_expr->ctype;
printf("\tinsert v%d at [%d:%d] of %s\n", new->pseudo,
expr->init_offset, ctype->bit_offset,
show_ident(base->ident));
return 0;
}
static void x86_initializer_expr(struct expression *expr, struct symbol *ctype)
{
struct expression *entry;
FOR_EACH_PTR(expr->expr_list, entry) {
// Nested initializers have their positions already
// recursively calculated - just output them too
if (entry->type == EXPR_INITIALIZER) {
x86_initializer_expr(entry, ctype);
continue;
}
// Ignore initializer indexes and identifiers - the
// evaluator has taken them into account
if (entry->type == EXPR_IDENTIFIER || entry->type == EXPR_INDEX)
continue;
if (entry->type == EXPR_POS) {
x86_position_expr(entry, ctype);
continue;
}
x86_initialization(ctype, entry);
} END_FOR_EACH_PTR(entry);
}
/*
* Print out an expression. Return the pseudo that contains the
* variable.
*/
static struct storage *x86_expression(struct expression *expr)
{
if (!expr)
return NULL;
if (!expr->ctype) {
struct position *pos = &expr->pos;
printf("\tno type at %s:%d:%d\n",
stream_name(pos->stream),
pos->line, pos->pos);
return NULL;
}
switch (expr->type) {
default:
return NULL;
case EXPR_CALL:
return x86_call_expression(expr);
case EXPR_ASSIGNMENT:
return x86_assignment(expr);
case EXPR_COMPARE:
return emit_compare(expr);
case EXPR_BINOP:
case EXPR_COMMA:
case EXPR_LOGICAL:
return emit_binop(expr);
case EXPR_PREOP:
return x86_preop(expr);
case EXPR_POSTOP:
return emit_postop(expr);
case EXPR_SYMBOL:
return emit_symbol_expr_init(expr->symbol);
case EXPR_DEREF:
case EXPR_SIZEOF:
case EXPR_ALIGNOF:
warning(expr->pos, "invalid expression after evaluation");
return NULL;
case EXPR_CAST:
case EXPR_FORCE_CAST:
case EXPR_IMPLIED_CAST:
return emit_cast_expr(expr);
case EXPR_VALUE:
return emit_value(expr);
case EXPR_STRING:
return emit_string_expr(expr);
case EXPR_INITIALIZER:
x86_initializer_expr(expr, expr->ctype);
return NULL;
case EXPR_SELECT:
return emit_select_expr(expr);
case EXPR_CONDITIONAL:
return emit_conditional_expr(expr);
case EXPR_STATEMENT:
return x86_statement_expr(expr);
case EXPR_LABEL:
return x86_label_expr(expr);
// None of these should exist as direct expressions: they are only
// valid as sub-expressions of initializers.
case EXPR_POS:
warning(expr->pos, "unable to show plain initializer position expression");
return NULL;
case EXPR_IDENTIFIER:
warning(expr->pos, "unable to show identifier expression");
return NULL;
case EXPR_INDEX:
warning(expr->pos, "unable to show index expression");
return NULL;
case EXPR_TYPE:
warning(expr->pos, "unable to show type expression");
return NULL;
case EXPR_FVALUE:
warning(expr->pos, "floating point support is not implemented");
return NULL;
}
return NULL;
}
|