/usr/include/freehdl/std-vhdl-types.hh is in libfreehdl0-dev 0.0.8-2.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #ifndef FREEHDL_STD_VHDL_TYPES_H
#define FREEHDL_STD_VHDL_TYPES_H
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <freehdl/std-memory.hh>
#include <freehdl/kernel-error.hh>
#include <freehdl/kernel-acl.hh>
using namespace std;
typedef long long int lint;
const int BUFFER_STREAM_SIZE_INCREMENT = 1024;
// replaces ostrstream in order to speedup simulation. Furhter, this
// stream can also handle raw binary data, i.e., data which may
// contain 0 as an element!
class buffer_stream
{
public:
char* buffer;
char* end_of_buffer;
char* pos_cursor;
void resize()
{
int size = end_of_buffer - buffer;
int fill_count = pos_cursor - buffer;
buffer = (char*)realloc(buffer, size + BUFFER_STREAM_SIZE_INCREMENT);
end_of_buffer = buffer + size + BUFFER_STREAM_SIZE_INCREMENT;
pos_cursor = buffer + fill_count;
}
//constructor
buffer_stream(){
buffer = end_of_buffer = pos_cursor = NULL;
resize();
buffer [0] = '\0';
}
//desconstructor
~buffer_stream()
{
if (buffer != NULL)
free (buffer);
}
// Write binary data to stream. Note that after writing binary data
// to the stream the buffer usually is NOT ended with 0 !
buffer_stream& binary_write(const void *src, const int len) {
while (pos_cursor + len >= end_of_buffer)
resize();
memcpy(pos_cursor, src, len);
pos_cursor += len;
return *this;
}
// output operator
buffer_stream& operator<<(const char* c) {
const int len = strlen(c);
if (pos_cursor + len >= end_of_buffer)
resize();
strcpy(pos_cursor, c);
pos_cursor += len;
return *this;
}
buffer_stream& operator<<(const char c) {
if (c != '\0') {
if (pos_cursor + 2 >= end_of_buffer)
resize();
*(pos_cursor++) = c;
}
*pos_cursor = '\0';
return *this;
}
buffer_stream& operator<<(const int i) {
// An int converted to decimal will never have more than 12
// characters
char ibuffer[13];
char *cursor = ibuffer + 12;
int l;
*(cursor--) = '\0';
if (i > 0) {
l = i;
while (l > 0) {
int new_l = l / 10;
int diff = l - (new_l << 3) - (new_l << 1);
*(cursor--) = (char)diff + '0';
l = new_l;
}
} else if (i < 0) {
l = -i;
while (l > 0) {
int new_l = l / 10;
int diff = l - (new_l << 3) - (new_l << 1);
*(cursor--) = (char)diff + '0';
l = new_l;
}
*(cursor--) = '-';
} else
*(cursor--) = '0';
if (pos_cursor + 30 >= end_of_buffer)
resize();
strcpy(pos_cursor, cursor+1);
pos_cursor += ibuffer + 12 - cursor - 1;
return *this;
}
buffer_stream& operator<<(const lint i) {
// An lint converted to decimal will never have more than 30
// characters
char ibuffer[31];
char *cursor = ibuffer + 30;
lint l;
*(cursor--) = '\0';
if (i > 0) {
l = i;
while (l > 0) {
lint new_l = l / 10;
lint diff = l - (new_l << 3) - (new_l << 1);
*(cursor--) = (char)diff + '0';
l = new_l;
}
} else if (i < 0) {
l = -i;
while (l > 0) {
lint new_l = l / 10;
lint diff = l - (new_l << 3) - (new_l << 1);
*(cursor--) = (char)diff + '0';
l = new_l;
}
*(cursor--) = '-';
} else
*(cursor--) = '0';
if (pos_cursor + 30 >= end_of_buffer)
resize();
strcpy(pos_cursor, cursor+1);
pos_cursor += ibuffer + 30 - cursor - 1;
return *this;
}
buffer_stream& operator<<(string& s) {
return *this << s.c_str();
}
buffer_stream& operator<<(const double i) {
char ibuffer[40];
sprintf(ibuffer,"%e",i);
return *this << ibuffer;
}
int str_len() {
return (int)(pos_cursor - buffer);
}
void init(char* pos_cursor){
if(pos_cursor == end_of_buffer)
buffer =(char*)realloc((void*)buffer, 1024);
}
//clean function
void clean(){ pos_cursor = buffer; };
void reinit() {
if (buffer != NULL)
free (buffer);
end_of_buffer = buffer = pos_cursor = NULL;
resize();
}
char *str() { return buffer; }
};
/* *************************************************************
* Type-Ids for the different VHDL type groups
* ************************************************************* */
typedef unsigned char type_id;
#define INTEGER 1
#define ENUM 2
#define FLOAT 3
#define PHYSICAL 4
#define RECORD 5
#define ARRAY 6
#define ACCESS 7
#define VHDLFILE 8
inline bool scalar(type_id id)
{
switch (id) {
case RECORD:
case ARRAY:
return false;
default:
return true;
}
}
/* *************************************************************
* Typedefs
* ************************************************************* */
typedef long long int lint;
typedef unsigned char uchar;
/* *************************************************************
* Logartihmus dualis of the sizes of VHDL types (in Bytes)
* ************************************************************* */
#define INTEGER_SIZE sizeof(integer)
#define ENUM_SIZE sizeof(enumeration)
#define FLOAT_SIZE sizeof(floatingpoint)
#define PHYSICAL_SIZE sizeof(physical)
/* HACK: see below */
#define RECORD_SIZE (2*sizeof(void*))
/* HACK: an array instance actually consists of two pointers (not
* including memory for info and data instances). We cannot use
* sizeof(array_base) here as array_base will be defined at the end of
* this file while ARRAY_SIZE is used before! */
#define ARRAY_SIZE (2*sizeof(void*))
#define ACCESS_SIZE sizeof(void*)
#define VHDLFILE_SIZE sizeof(iostream*)
#define INTEGER_SIZE_LD 32
/* *************************************************************
* All VHDL integer types are actually C ints
* ************************************************************* */
typedef int integer;
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(integer *d) { return INTEGER; }
/* Actually does nothing */
inline void cleanup(integer *d) { };
/* mod operator */
inline integer op_mod(const integer a, const integer b) {
register int tmp = a % b;
return ((b^tmp) < 0)? tmp + b : tmp;
}
/* power operator */
inline integer op_power(const integer a, const integer b) {
return (integer)pow((double)a, (double)b);
}
/* absolute operator */
inline integer op_abs (const integer a) { return abs (a); }
/* *************************************************************
* All VHDL access type are actually void pointer
* ************************************************************* */
typedef void * vhdlaccess;
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(vhdlaccess *d) { return ACCESS; }
/* Actually does nothing */
inline void cleanup(vhdlaccess *d) { };
/* *************************************************************
* All VHDL enumeration types are actually C chars
* ************************************************************* */
typedef unsigned char enumeration;
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(enumeration *d) { return ENUM; }
/* Actually does nothing */
inline void cleanup(enumeration *d) { };
/* *************************************************************
* All VHDL floating point types are actually C doubles
* ************************************************************* */
typedef double floatingpoint;
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(floatingpoint *d) { return FLOAT; }
/* Actually does nothing */
inline void cleanup(floatingpoint *d) { };
/* power operator */
inline floatingpoint op_power(const floatingpoint a, const integer b) {
return pow(a, (double)b);
}
/* absolute operator */
inline floatingpoint op_abs (const floatingpoint a) {
return (floatingpoint)fabs (a);
}
/* *************************************************************
* All VHDL physical types are actually long long ints (64 bit)
* ************************************************************* */
typedef lint physical;
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(physical *d) { return PHYSICAL; }
/* Actually does nothing */
inline void cleanup(physical *d) { };
/* absolute operator */
inline physical op_abs (const physical a) {
return (a >= 0)? a : -a;
}
/* *************************************************************
* VHDL file types
* ************************************************************* */
struct vhdlfile {
bool do_close; // Is true if the stream must be closed manually
istream *in_stream;
ostream *out_stream;
vhdlfile() { in_stream = NULL; out_stream = NULL; do_close = false; }
~vhdlfile() {
if (do_close) return;
if (in_stream != NULL) delete in_stream;
if (out_stream != NULL) delete out_stream;
in_stream = NULL;
out_stream = NULL;
}
};
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(vhdlfile *d) { return VHDLFILE; }
/* Actually does nothing */
inline void cleanup(vhdlfile *d) { };
/* *************************************************************
* Some functions for composite types
* ************************************************************* */
/* Function to call the cleanup operator of the appropriate array
instance */
template<class A>
void cleanup(A *p) { p->cleanup_instance(); }
/* *************************************************************
* Some operators
* ************************************************************* */
/* not operator */
inline enumeration op_not(const enumeration a) {
return a ? 0 : 1;
}
/* nand operator */
inline enumeration op_nand(const enumeration a, const enumeration b) {
return op_not(a & b);
}
/* xor operator */
inline enumeration op_xor(const enumeration a, const enumeration b) {
return a ^ b;
}
/* xnor operator */
inline enumeration op_xnor(const enumeration a, const enumeration b) {
return op_not(op_xor(a, b));
}
/* nor operator */
inline enumeration op_nor(const enumeration a, const enumeration b) {
return a | b ? 0 : 1;
}
/* *************************************************************
* Some functions of global interest
* ************************************************************* */
/* Returns true if value is in between left and right */
template<class T>
inline bool check_bounds(const T &left, const T &right, const T &value) {
if (left <= right)
return left <= value && value <= right;
else
return left >= value && value >= right;
}
#if !(!defined __GNUC__ || __GNUC__ != 2)
template<class T>
inline T min(const T old_value, const T new_value) {
return new_value < old_value? new_value : old_value;
}
template<class T>
inline T max(const T old_value, const T new_value) {
return new_value > old_value? new_value : old_value;
}
#endif
/* Determine length of range */
template<class T>
inline T
range_to_length(T le, range_direction r, T ri)
{
if (r == to)
return le <= ri? (ri - le + (T)1) : (T)0;
else
return ri <= le? (le - ri + (T)1) : (T)0;
}
template<class T>
inline T
up_range_to_length(T le, T ri)
{
return le <= ri? (ri - le + (T)1) : (T)0;
}
template<class T>
inline T
down_range_to_length(T le, T ri)
{
return ri <= le? (le - ri + (T)1) : (T)0;
}
/* Rotate left value by count bits. Should be mapped to the
* corresponding rotate left machine instruction by the C++ compiler! */
inline unsigned int
rotate_left(unsigned int value, int count)
{
return (value << count) | (value >> (sizeof(unsigned int) * 8 - count));
}
/* This function is used to select an appropriate alternative of an
* VHDL case statement where the selection expression is an array of
* characters */
inline int
do_case_select(const int size, const int key_count, const unsigned int key,
const unsigned int* const sel, const unsigned int* const keys,
const int* const key_to_lit, const enumeration** const lit_tab,
const int* const actions)
{
// Try to find a key in the key table which matches the key value
// calculated from the selection expression
for (int i = 0; i < key_count; i++)
if (key == keys[i]) {
// If a key was found then we use the index number of the key to
// access a table which stores for each key a list of pointer to
// array literals. Note that all these array literals are
// associated with the *same* key.
i = key_to_lit[i];
do {
// Compare the current selection array litaral with all those
// literals associated with the specific key (note that there
// may be more than one literal). If a match is found then use
// the index number to access an action table which stores the
// corresponding action number. Return the action number.
if (!memcmp(sel, lit_tab[i], size * sizeof(enumeration)))
return actions[i];
} while (lit_tab[++i] != 0);
break;
}
// If no match was found then return the default action number
return -1;
}
/* Returns address of object passed over as a parameter */
template<class T>
inline const void *
const_pointer(const T &a) { return &a; }
/* Casts void pointer to type T and derefs it */
template<class T>
inline T
deref(void *p) { return *(T*)p; }
/* *************************************************************
* Definitions required for signal resolution
* ************************************************************* */
// resolver_handler_p is a pointer type to a function which will be
// called by the kernel ro perform signal reolution. The first
// parameter points to a object which shall take the result of a
// resolving operation while the second parameter points to array
// containing the driver values to be resolved. The last parameter
// points to the unconstrained array_info.
class array_base;
class array_info;
class driver_info;
typedef void (*resolver_handler_p)(void*, driver_info*, array_base*, array_info*);
/* *************************************************************
* The type info interface class is te base class for all
* derived type info classes
* ************************************************************* */
class type_info_interface {
public:
const type_id id;
const uchar size; /* size of an object that corresponds with the
* type_info_instance */
bool resolved; /* set to true if the type is resolved. Note that the
* type_infos do NOT store any further information to
* support the resolving mechanism as these infos are
* used by the kernel only. Instead, the kernel
* collects and stores all necessary information. */
type_info_interface(const type_id i, const uchar s) :
id(i), size(s), resolved(false) {};
virtual ~type_info_interface() {};
// Returns true if the current info instance describes a scalar type
inline bool scalar();
// Prints value of instance (src is pointing to) into a char
// buffer and returns a pointer to the buffer. Note, the buffer
// is static! Hence, each call to str uses the SAME buffer!
char *str(const void *src);
// Copy the content of src to dest. Note can be used
// for scalar VHDL objects only!
inline void *fast_copy(void *dest, const void *src);
// Compare the content of src and dest. Note: can be used
// for scalar VHDL objects only!
inline bool fast_compare(const void *dest, const void *src);
// Assigns the content of src and dest. Note: can be used
// for scalar VHDL objects only! Returns true if the new
// value differs from the old one.
inline bool fast_assign(void *dest, const void *src);
// Gets left and right bounds of an enumeration, integer or array
// type and returns 0. Returns -1 if type is no enumeration, integer
// or array type.
int get_bounds(int &left, range_direction &range, int &right);
// Create a new object. Memory is allocated by malloc
virtual void *create() = 0;
// Clone a object. Memory is allocated by malloc
virtual void *clone(const void *src) = 0;
// Copy the content of src to dest
virtual void *copy(void *dest, const void *src) = 0;
// Initiliazes an instance
virtual void init(void *src) = 0;
// Frees additional memory occupied by an object.
virtual void clear(void *src) {};
// Compare src1 with src2. Returns true if they are equal
virtual bool compare(const void *src1, const void *src2) = 0;
// Copy the content of src to dest. Returns true if the new
// value differs from the old one
virtual bool assign(void *dest, const void *src) = 0;
// Removes an object (frees memory)
virtual void remove(void *src) = 0;
// This method does nothing for scalar types. For non-scalar types
// it assumes that src points to allocated memory that contains
// garbage. Hence, the method does *not* try to read the conent
// stored in *src.
void reset (void *src);
// Returns the address of an appropriate subelement of the current
// object. acl is the "path" to the subelement. Note that element
// returns the address of the left element in case of a "range"
// (slice) is applied onto an array.
virtual void *element(void *src, acl *a) { return src; }
// Returns the address of an appropriate subelement of
// the current object. i is the index number of the subelment.
void *element(void *src, int i);
// Convert acl to index start and end. Returns start value
int acl_to_index(acl *a, int &start, int &end) const;
// Convert acl to index start
int acl_to_index(acl *a) const;
// Get info instance of scalar element with index i
type_info_interface *get_info(int i) const;
// Return info instance of element determined by acl a. src is an
// optional pointer to an object.
type_info_interface *get_info(void *src, acl *a) const;
// Returns the number of scalar elements of the current object
virtual int element_count() { return 1; }
// Prints the content of src into an string stream
virtual void print(buffer_stream &str, const void *src, int mode) = 0;
// Reads a value from a string and stores it into *dest. Further, in
// case of an error the function returns a pointer to the character
// which caused the failure or NULL otherwise.
virtual const char *read(void *dest, const char *str) = 0;
// Prints the content of src into an string stream in VCD format
virtual void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure) = 0;
// Prints value into binary stream. Note that only the raw data but
// no type info objects are written! The method returns the number
// of bytes written to the stream.
int binary_print(buffer_stream &str, const void *src);
// Reads value from memory (str) and puts the values into dest. The
// sequence of the data values must be the same as generated by
// binary_print. Note that only the raw data but no type info objects
// are read! Hence, the dest pointer must point to valid object of the
// appropriate size! Returns the number of bytes read or -1 if the
// read operation was not successfull.
int binary_read(void *dest, void *src);
// Usualy increments/decrements reference counter of type_info
// instances. However, as these instances are static for all
// but composite types both function actually do noting. Note
// that the corresponding methods are overloaded by the array_info
// and record_info classes.
virtual void add_ref() { };
virtual void remove_ref() { };
// Add a resolver handler (required by the kernel to perform signal
// resolution) to a type. Returns a reference to the current info
// instance. handler points to a function which is used by the
// kernel to perform the resolution mechanism. ainfo points to a
// corresponding array_info instance describing the driver
// array. The last parameter stated whether the resolver function
// must not be called for a single source (ideal = true).
type_info_interface &add_resolver(resolver_handler_p handler, type_info_interface *ainfo, bool ideal);
// Register a type
void register_type(const char *sln, const char *ln, const char *n, void *sr);
};
/******************************************************
* Some definitions which are used by the kernel only
******************************************************/
#ifdef KERNEL // This is only required when the kernel is compiled
// Constants to control printing of values via the
// type_info_interface::print method
#define VHDL_PRINT_MODE 0
#define CDFG_PRINT_MODE 1
// Copy the content of src to dest. Note, this method can be used for
// scalar VHDL objects only!
inline void *
type_info_interface::fast_copy(void *dest, const void *src)
{
switch (id) {
case ENUM:
*(enumeration*)dest = *(enumeration*)src;
break;
case INTEGER:
*(integer*)dest = *(integer*)src;
break;
case FLOAT:
case PHYSICAL:
*(physical*)dest = *(physical*)src;
break;
}
return dest;
}
// Compare the content of src and dest. Note: can be used
// for scalar VHDL objects only!
inline bool
type_info_interface::fast_compare(const void *dest, const void *src)
{
switch (id) {
case ENUM:
return *(enumeration*)dest == *(enumeration*)src;
break;
case INTEGER:
return *(integer*)dest == *(integer*)src;
break;
case FLOAT:
case PHYSICAL:
return *(physical*)dest == *(physical*)src;
break;
}
return false;
}
// Assigns the content of src and dest. Note: can be used
// for scalar VHDL objects only! Returns true if the new
// value differs from the old one
inline bool
type_info_interface::fast_assign(void *dest, const void *src)
{
switch (id) {
case ENUM:
{
enumeration src_val = *(enumeration*)src, dest_val = *(enumeration*)dest;
if (src_val != dest_val) {
*(enumeration*)dest = src_val;
return true;
} else return false;
}
case INTEGER:
{
integer src_val = *(integer*)src, dest_val = *(integer*)dest;
if (src_val != dest_val) {
*(integer*)dest = src_val;
return true;
} else return false;
}
case FLOAT:
case PHYSICAL:
{
physical src_val = *(physical*)src, dest_val = *(physical*)dest;
if (src_val != dest_val) {
*(physical*)dest = src_val;
return true;
} else return false;
}
}
return false;
}
inline bool
type_info_interface::scalar()
{
switch (id) {
case RECORD:
case ARRAY:
return false;
default:
return true;
}
}
// Returns whether info is or includes array types which are not
// constrained. Returns true if no such type or element type was
// found.
bool
is_constrained(type_info_interface *info);
// Create a type instance based on a reference info rinfo and an acl
// a. The acl stores all information which are not included within the
// reference info. E.g., if the reference info is an unconstrained
// array then the acl stores the actual bounds of the array. Note that
// information which can be derived from the reference info is NOT
// stored in the acl!
type_info_interface *
setup_type_info_interface(type_info_interface *rinfo, pacl a);
/******************************************************
* End of internal kernel definitions
******************************************************/
#endif
/* *************************************************************
* Integer info base class
* ************************************************************* */
class integer_info_base : public type_info_interface {
public:
integer left_bound, right_bound, low_bound, high_bound;
integer_info_base();
integer_info_base(const integer le, const integer ri,
const integer lo, const integer hi);
integer_info_base &set(integer_info_base *src);
integer_info_base &set(const integer le, const integer ri,
const integer lo, const integer hi);
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
bool compare(const void *src1, const void *src2);
bool assign(void *dest, const void *src);
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode);
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure);
const char *read(void *dest, const char *str);
integer check(integer value) {
if (value >= low_bound && value <= high_bound)
return value;
else
error(ERROR_SCALAR_OUT_OF_BOUNDS, this, &value);
return 0;
}
};
/* *************************************************************
* Access info base class
* ************************************************************* */
class access_info_base : public type_info_interface {
public:
type_info_interface *designated_type_info;
access_info_base();
access_info_base(type_info_interface *d_info);
access_info_base &set(type_info_interface *d_info);
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
bool compare(const void *src1, const void *src2);
bool assign(void *dest, const void *src);
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode);
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure) {};
const char *read(void *dest, const char *str);
};
/* *************************************************************
* File info base class
* ************************************************************* */
class vhdlfile_info_base : public type_info_interface {
public:
type_info_interface *type_info;
vhdlfile_info_base();
vhdlfile_info_base(type_info_interface *f_info);
vhdlfile_info_base &set(type_info_interface *f_info);
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
bool compare(const void *src1, const void *src2);
bool assign(void *dest, const void *src);
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode) {};
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure) {};
const char *read(void *dest, const char *str);
};
/* *************************************************************
* Float info base class
* ************************************************************* */
class float_info_base : public type_info_interface {
public:
floatingpoint left_bound, right_bound, low_bound, high_bound;
float_info_base();
float_info_base(const floatingpoint le, const floatingpoint ri,
const floatingpoint lo, const floatingpoint hi);
float_info_base &set(const floatingpoint le, const floatingpoint ri,
const floatingpoint lo, const floatingpoint hi);
float_info_base &set(float_info_base *src);
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
bool compare(const void *src1, const void *src2);
bool assign(void *dest, const void *src);
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode);
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure);
const char *read(void *dest, const char *str);
floatingpoint check(floatingpoint value) {
if (value >= low_bound && value <= high_bound)
return value;
else
error(ERROR_SCALAR_OUT_OF_BOUNDS, this, &value);
return 0;
}
};
/* *************************************************************
* Enum info base class
* ************************************************************* */
class enum_info_base : public type_info_interface {
public:
int left_bound, right_bound, length;
const char **values;
enum_info_base();
enum_info_base(const int le, const int ri, const char **val);
enum_info_base &set(const int le, const int ri, const char **val);
enum_info_base &set(enum_info_base *src);
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
bool compare(const void *src1, const void *src2);
bool assign(void *dest, const void *src);
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode);
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure);
const char *read(void *dest, const char *str);
enumeration check(integer value) {
if (value >= left_bound && value <= right_bound)
return value;
else
error(ERROR_SCALAR_OUT_OF_BOUNDS, this, &value);
return 0;
}
};
/* *************************************************************
* Physical info base class
* ************************************************************* */
class physical_info_base : public type_info_interface {
public:
physical left_bound, right_bound, low_bound, high_bound;
const char **units;
const physical *scale;
int unit_count;
physical_info_base();
physical_info_base(const physical le, const physical ri,
const physical lo, const physical hi,
const char **un, const physical *sc, int uc);
physical_info_base &set(const physical le, const physical ri,
const physical lo, const physical hi,
const char **un, const physical *sc, int uc);
physical_info_base &set(physical_info_base *src);
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
bool compare(const void *src1, const void *src2);
bool assign(void *dest, const void *src);
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode);
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure) ;
const char *read(void *dest, const char *str);
physical check(physical value) {
if (value >= low_bound && value <= high_bound)
return value;
else
error(ERROR_SCALAR_OUT_OF_BOUNDS, this, &value);
return 0;
}
};
/* *************************************************************
* Record info base class
* ************************************************************* */
class record_info : public type_info_interface {
public:
int record_size;
int data_size;
type_info_interface **element_types;
void *(*element_addr) (void*, int);
const char **element_names;
/* Counts the number of instances which are currently using this
* record_info object. Hence, several record instances may
* share a single record_info instance! */
int ref_counter;
/* Memory management for record_info instances. */
void *operator new(size_t s) {
return (record_info*)internal_dynamic_alloc(sizeof(record_info));
}
void operator delete(void *a) {
internal_dynamic_remove(a, sizeof(record_info));
}
record_info () : type_info_interface(RECORD, RECORD_SIZE) {
record_size = -1; data_size = -1; element_types = NULL; element_addr = NULL; element_names = NULL;
}
record_info (int rs, int ds, const char **en, void *(*ea)(void*, int), int rc);
record_info &set(int rs, int ds, const char **en, void *(*ea)(void*, int), int rc) {
record_size = rs;
data_size = ds;
element_names = en;
element_addr = ea;
element_types = (type_info_interface**)internal_dynamic_alloc(record_size * sizeof(type_info_interface*));
memset(element_types, 0, record_size * sizeof(type_info_interface*));
ref_counter = rc;
return *this;
}
/* Initialize record info from other record info */
record_info &set (record_info *info, int rc)
{
record_size = info->record_size;
data_size = info->data_size;
element_types = info->element_types;
element_addr = info->element_addr;
element_names = info->element_names;
ref_counter = rc;
return *this;
}
/* Set info pointer of index "index" to eti */
record_info &set(int index, type_info_interface *eti) {
element_types[index] = eti;
eti->add_ref();
return *this;
}
~record_info () {
/* Do not destruct permanent structs at all. Global objects can
not be handled reliably since their destructors will be run in
no particular order and the type_info_interface structs
referenced by element_types could have been destroyed before
running this destructor, for example. */
if (ref_counter < 0)
return;
if (element_types)
{
for (int i = 0; i < record_size; i++)
if (element_types[i] != NULL)
element_types[i]->remove_ref();
internal_dynamic_remove (element_types,
record_size * sizeof(type_info_interface*));
}
}
/* Returns the number of scalar elements of the current type */
int element_count() {
int count = 0;
for (int i = 0; i < record_size; i++)
count += element_types[i]->element_count();
return count;
}
/* Returns the address of an appropriate element of the current
* object. acl is the "path" to the element */
void *element(void *src, acl *a);
/* This method must be called by each array instance which references
* the current info instance */
void add_ref() {
if (ref_counter >= 0) ref_counter++;
}
/* If rec_counter == 0 then no object uses this info instance and
* it is removed. Note, there are also static info instances which
* are not removed even when they are not in use. To create a
* static info instance, array_info is called with parameter -1. */
void remove_ref() {
if (ref_counter > 0) {
ref_counter--;
if (ref_counter == 0)
delete this;
}
}
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
void clear(void *src);
bool compare(const void *src1, const void *src2) { return false; };
bool assign(void *dest, const void *src) { return false; };
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode);
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure) ;
const char *read(void *dest, const char *str);
};
/* *************************************************************
* Array info class
* ************************************************************* */
class array_info : public type_info_interface {
public:
/* Dimension info */
range_direction index_direction; /* = "to" if index is of enumeration type */
int left_bound, right_bound;
int length; /* = -1 if bounds are actually not set (unconstrained array) */
/* Index type info */
type_info_interface *index_type;
/* Element info */
type_info_interface *element_type;
/* Counts the number of array instances which are currently using
* this array_info_base object to define their bounds. Hence, several
* array instances may share a single array_info_base instance! */
int ref_counter;
/* Methods */
/* Memory management for array_info instances. */
void *operator new(size_t s) {
return (array_info*)internal_dynamic_alloc(sizeof(array_info));
}
void operator delete(void *a) {
internal_dynamic_remove(a, sizeof(array_info));
}
/* Default constructor */
array_info() : type_info_interface(ARRAY, ARRAY_SIZE)
{
length = -1;
element_type = index_type = NULL;
ref_counter = 0;
}
/* Destructor */
~array_info() {
/* See ~record_info for additional general comments about
destructors of global objects. */
if (ref_counter < 0)
return;
if (element_type)
element_type->remove_ref();
if (index_type)
index_type->remove_ref();
}
/* Constructor. If rc is set to -1 then the array_info instance
* will not be removed */
array_info &set(type_info_interface *et, type_info_interface *it, int rc);
array_info(type_info_interface *et, type_info_interface *it, int rc);
/* Constructor to create an info instance for an array subtype */
/* Set the element info object and the range of the array */
array_info &set(type_info_interface *et, type_info_interface *it, int le, range_direction r, int ri, int rc = 1) {
index_direction = r;
left_bound = le;
right_bound = ri;
length = (ri - le) * (r == to? 1 : -1);
length = length < 0? 0 : length + 1;
ref_counter = rc;
index_type = it;
index_type->add_ref();
element_type = et;
element_type->add_ref();
return *this;
}
array_info(type_info_interface *et, type_info_interface *it, int le, range_direction r, int ri, int rc);
/* Constructor to create an info instance where the left bound is
* determined by base and the right bound is derived from len */
array_info &set(type_info_interface *et, type_info_interface *it, int len, int rc);
array_info(type_info_interface *et, type_info_interface *it, int len, int rc);
/* Set the element info object */
void set(type_info_interface *et) { element_type = et; element_type->add_ref(); }
/* This method must be called by each array instance which references
* the current info instance */
void add_ref() {
if (ref_counter >= 0) ref_counter++;
}
/* If rec_counter == 0 then no object uses this info instance and
* it is removed. Note, there are also static info instances which
* are not removed even when they are not in use. To create a
* static info instance, array_info is called with parameter -1. */
void remove_ref() {
if (ref_counter > 0) {
ref_counter--;
if (ref_counter == 0)
delete this;
}
}
// Check an array index value
int check(int value) {
if (index_direction == to) {
if (value >= left_bound && value <= right_bound)
return value;
} else
if (value <= left_bound && value >= right_bound)
return value;
error(ERROR_ARRAY_INDEX, this, &value);
}
/* The methods named "???_match" are used to test the bounds of the
* current array_info instance. All methods return a pointer to the
* current array_info instance. */
/* Methods to test whether an array_info instances has bounds and
* direction as defined by a reference array_info instance */
array_info *exact_match(type_info_interface *test_info);
/* Methods to test whether an array_info instances has bounds le and
* ri and direction r */
array_info *exact_match(int le, range_direction r, int ri);
/* Methods to test whether an array_info instances has length
* len */
array_info *length_match(int len);
/* Returns the number of scalar elements of the current type */
int element_count() { return length * element_type->element_count(); }
/* Returns the address of an appropriate subelement of
* the current object. acl is the "path" to the subelement */
void *element(void *src, acl *a);
void *create();
void *clone(const void *src);
void *copy(void *dest, const void *src);
void init(void *src);
void clear(void *src);
bool compare(const void *src1, const void *src2) { return false; };
bool assign(void *dest, const void *src) { return false; };
void remove(void *src);
void print(buffer_stream &str, const void *src, int mode);
void vcd_print(buffer_stream &str, const void *src,char* translation_table, bool pure);
const char *read(void *dest, const char *str);
};
// Function to cast an type_info_interface pointer to an array_info
// pointer
inline array_info *parray_info(type_info_interface *t) { return (array_info*)t; }
/* *************************************************************
* The type classes
* ************************************************************* */
/* *************************************************************
* Record type class
* ************************************************************* */
/* Record base class */
class record_base {
public:
record_info *info; /* Points to the object which stores information
* about the current record */
char *data; /* Pointer to the actual data of the record */
record_base() { };
record_base(const record_base &a);
record_base(record_info *i) { info = i; i->add_ref(); }
void set_info(record_info *i) {
if (info != NULL) info->remove_ref();
info = i;
i->add_ref();
}
static int size() { return RECORD_SIZE; }
};
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(record_base *d) { return RECORD; }
/* E is a structure containing the actual record data */
template<class E>
class record_type : public record_base {
public:
typedef E E_type; /* declare a local type name which is equivalent
* to the actual record type */
/* Method to access record value */
E &value() { return *(E*)data; }
/* Method to access record value */
const E &value() const { return *(E*)data; }
inline void cleanup_instance();
record_type() {};
/* init initiliazes an record. The record elements are set to their
* corresponding default values. */
record_type &init(type_info_interface *rinfo) {
info = (record_info*)rinfo;
info->add_ref();
data = (char*)internal_dynamic_alloc(sizeof(E));
this->value().init(rinfo);
return *this;
}
/* "p" points to an record instance. The record elements are copied
* from "p". */
record_type &init(type_info_interface *rinfo, const void *p) {
info = (record_info*)rinfo;
info->add_ref();
data = (char*)internal_dynamic_alloc(sizeof(E));
this->value().init(rinfo, p);
return *this;
}
record_type(const record_type &r) : record_base() { init(r.info, (void*)&r); }
inline ~record_type() { cleanup_instance(); }
/* Methods used to build record aggregats */
inline record_type &aggregate_set(int index, const void *element_value);
inline record_type &aggregate_copy(int index, int copy_index);
record_type &operator=(const record_type &record) {
this->value() = record.value();
return *this;
}
// Note that only the relational operators == is directly defined in
// the class as it is valid for all array classes (!= is defined by
// combining ! and ==).
bool operator==(const record_type &record) { return record.value() == this->value(); }
};
/* Method to cleanup a record instance, i.e. memory allocated by the
* instance is freed */
template<class E>
inline void record_type<E>::cleanup_instance()
{
if (info != NULL)
{
record_info &rinfo = *info;
for (int i = 0; i < rinfo.record_size; i++) {
rinfo.element_types[i]->clear ( (*rinfo.element_addr) (data, i));
rinfo.element_types[i]->remove_ref();
}
rinfo.remove_ref();
internal_dynamic_remove(data, rinfo.data_size);
}
}
template<class E>
inline record_type<E> &record_type<E>::aggregate_set(int index, const void *element_value)
{
record_info &rinfo = *info;
rinfo.element_types[index]->copy( (*rinfo.element_addr) (data, index), element_value);
return *this;
}
template<class E>
inline record_type<E> &record_type<E>::aggregate_copy(int index, int copy_index)
{
record_info &rinfo = *info;
rinfo.element_types[index]->copy( (*rinfo.element_addr) (data, index),
(*rinfo.element_addr) (data, copy_index));
return *this;
}
/* *************************************************************
* Array type class
* ************************************************************* */
class array_base {
public:
array_info *info; /* Points to the object which stores information
* about the current array */
char *data; /* Pointer to the actual data of the array */
array_base() { };
array_base(const array_base &a);
array_base(array_info *i) { info = i; i->add_ref(); }
void set_info(array_info *i) {
if (info != NULL) info->remove_ref();
info = i;
i->add_ref();
}
array_base &operator=(const array_base &a);
static int size() { return ARRAY_SIZE; }
};
/* Always returns the id of the type. The parameter is actually not
* needed but used to select the appropriate function id. */
inline type_id id(array_base *d) { return ARRAY; }
/* E is the array element type of the _unconstrained_ array range
* type. */
template<class E>
class array_type : public array_base {
public:
typedef E E_type; /* declare a local type name which is
* equivalent to the element type */
inline void cleanup_instance();
array_type() {};
/* init initiliazes an array. The array elements are set to their
* corresponding default values. */
array_type &init(type_info_interface *ainfo);
/* All elements of the array are initialized to "elem_init_value" */
array_type &init(type_info_interface *ainfo, const E &elem_init_value);
/* "p" points to an array instance. The array elements are copied
* from "p". */
array_type &init(type_info_interface *ainfo, const void *p);
array_type &init(const void *p) { init(((array_base*)p)->info, p); return *this; }
array_type(const array_type &a) : array_base() { init(a.info, (void*)&a); }
array_type(array_info *ainfo, const E *iarray);
array_type(array_info *ainfo, const E &val);
inline ~array_type() { cleanup_instance(); }
/* Methods used to build array aggregats */
inline array_type &aggregate_set(int left, range_direction direction, int right, const E &value);
inline array_type &aggregate_copy(int left, range_direction direction, int right, int copy_index);
E &operator[](const int i) {
int index = (info->index_direction == to? i - info->left_bound : info->left_bound - i);
if (index < 0 || index >= info->length)
error(ERROR_ARRAY_INDEX);
return ((E*)data)[index];
}
const E &operator[](const int i) const {
int index = (info->index_direction == to? i - info->left_bound : info->left_bound - i);
if (index < 0 || index >= info->length)
error(ERROR_ARRAY_INDEX);
return ((E*)data)[index];
}
E &operator()(const int i) { return ((E*)data)[i]; }
const E &operator()(const int i) const { return ((E*)data)[i]; }
array_type &operator=(const array_type &array);
// Note that only the relational operators == is directly defined in
// the class as it is valid for all array classes (!= is defined by
// combining ! and ==).
bool operator==(const array_type &array) const;
};
/* Method to cleanup an array instance, i.e. memory allocated by the
instance is freed */
template<class E>
void array_type<E>::cleanup_instance()
{
if (data != NULL)
{
/* Note, scalar(...) can be evaluated at compile time. */
if (!scalar(id((E*)NULL)))
/* If the element type is not scalar then execute
* cleanup() for each element in the array */
for (int i = 0; i < info->length; i++)
cleanup(&((E*)data)[i]);
if (data != NULL)
internal_dynamic_remove(data, sizeof(E) * info->length); /* Remove data memory */
}
if (info != NULL)
info->remove_ref(); /* Unlink from the info instance */
}
/* The concat operator */
template<class A, class E>
A concat(array_info *ainfo, const A &a1, const A &a2)
{
int length = ainfo->length;
// If both arrays are null arrays then return the result is the
// right operand
if (ainfo->length == 0)
return A(a2);
// Create new array
A new_array;
new_array.info = ainfo;
new_array.info->add_ref();
/* Allocate memory for the data */
const int mem_size = length * ainfo->element_type->size();
new_array.data = (char*)internal_dynamic_alloc(mem_size);
/* Note, scalar(...) can be evaluated at compile time. */
type_info_interface *etype = ainfo->element_type;
if (!scalar(id((E*)NULL))) {
/* If the element type is not scalar then init the memory and
* execute init(...) for each element in the array */
memset(new_array.data, 0, mem_size);
for (int i = 0; i < length; i++)
etype->init(&((E*)new_array.data)[i]);
}
/* Copy data */
int length1 = a1.info->length1;
int length2 = a2.info->length2;
int i;
for (i = 0; i < length1; i++)
((E*)new_array.data)[i] = ((E*)a1.data)[i];
for (; i < length; i++)
((E*)new_array.data)[i] = ((E*)a2.data)[i];
return new_array;
}
/* Another version of the concat operator */
template<class A, class E>
A concat(const A &a1, const A &a2)
{
int length = a1.info->length + a2.info->length;
// If both arrays are null arrays then return the result is the
// right operand
if (length == 0)
return A(a2);
// Create new array
A new_array;
array_info &ref_ainfo = a1.info->length != 0? *a1.info : *a2.info;
new_array.info = new array_info(ref_ainfo.element_type, ref_ainfo.index_type, 0);
array_info &new_info = *new_array.info;
new_array.info->add_ref();
int right;
ref_ainfo.index_type->get_bounds(new_info.left_bound, new_info.index_direction, right);
if (new_info.index_direction == to) {
new_info.right_bound = new_info.left_bound + length - 1;
if (new_info.right_bound > right) error(ERROR_ARRAY_INDEX_OUT_OF_BOUNDS);
} else {
new_info.right_bound = new_info.left_bound - length + 1;
if (new_info.right_bound < right) error(ERROR_ARRAY_INDEX_OUT_OF_BOUNDS);
}
new_info.length = length;
/* Allocate memory for the data */
const int mem_size = length * sizeof(E);
new_array.data = (char*)internal_dynamic_alloc(mem_size);
/* Note, scalar(...) can be evaluated at compile time. */
type_info_interface *etype = new_array.info->element_type;
if (!scalar(id((E*)NULL))) {
/* If the element type is not scalar then execute init(...) for
* each element in the array after setting the memory to 0 */
memset(new_array.data, 0, mem_size);
for (int i = 0; i < length; i++)
etype->init(&((E*)new_array.data)[i]);
}
/* Copy data */
int length1 = a1.info->length;
int i;
for (i = 0; i < length1; i++)
((E*)new_array.data)[i] = ((E*)a1.data)[i];
for (int j = 0; i < length; i++, j++)
((E*)new_array.data)[i] = ((E*)a2.data)[j];
return new_array;
}
template<class E>
array_type<E>::array_type (array_info *ainfo, const E *iarray)
{
/* Create a new array_info instance */
info = ainfo;
info->add_ref();
/* Allocate memory for the data */
int length = info->length;
const int mem_size = length * sizeof(E);
data = (char*)internal_dynamic_alloc(mem_size);
/* Note, scalar(...) can be evaluated at compile time. */
type_info_interface *etype = info->element_type;
if (!scalar(id((E*)NULL))) {
/* If the element type is not scalar then execute init(...) for
* each element in the array after initialing the memory to 0 */
memset(data, 0, mem_size);
for (int i = 0; i < length; i++)
etype->init(&((E*)data)[i]);
}
/* Copy data */
for (int i = 0; i < length; i++)
((E*)data)[i] = iarray[i];
}
template<class E>
array_type<E>::array_type (array_info *ainfo, const E &val)
{
/* Create a new array_info instance */
info = ainfo;
info->add_ref();
int length = info->length;
/* Allocate memory for the data */
const int mem_size = length * sizeof(E);
data = (char*)internal_dynamic_alloc(mem_size);
/* Note, scalar(...) can be evaluated at compile time. */
type_info_interface *etype = info->element_type;
if (!scalar(id((E*)NULL))) {
/* If the element type is not scalar then execute init(...) for
* each element in the array after initialing the memory to 0 */
memset(data, 0, mem_size);
for (int i = 0; i < length; i++) {
etype->init(&((E*)data)[i]);
((E*)data)[i] = val;
}
} else {
/* Each element of the array is set to val */
const E val_copy = val; // Important for speed optimization
for (int i = 0; i < length; i++)
((E*)data)[i] = val_copy;
}
}
template<class E>
array_type<E> &array_type<E>::operator=(const array_type<E> &array)
{
int length = info->length;
/* Check whether array bounds are compatible */
if (info != array.info && length != array.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Copy the data part of the arrays */
if (scalar(id((E*)NULL))) {
/* If the element type is not scalar then execute init(...) for
* each element in the array after initialing the memory to 0 */
const int mem_size = length * sizeof(E);
memcpy(data, array.data, mem_size);
} else
for (int i = 0; i < length; i++)
((E*)data)[i] = ((E*)array.data)[i];
return *this;
}
template<class E>
bool array_type<E>::operator==(const array_type<E> &array) const
{
const int length = info->length;
if (length != array.info->length) return false;
/* Compare the data part of the arrays */
for (int i = 0; i < length; i++)
if ((((E*)data)[i] != ((E*)array.data)[i])) return false;
return true;
}
template<class E>
array_type<E> &array_type<E>::init(type_info_interface *ainfo, const void *p)
{
info = (array_info*)ainfo;
info->add_ref();
int length = info->length;
type_info_interface *etype = info->element_type;
/* Allocate memory for the data */
const int mem_size = length * sizeof(E);
data = (char*)internal_dynamic_alloc(mem_size);
E *src_data = (E*)((array_base*)p)->data;
/* Next, analyze element type of array */
switch (id((E*)NULL)) {
case ARRAY:
{
/* If the element type is not scalar then init memory to 0 */
memset(data, 0, mem_size);
/* Copy each element of the array */
for (int i = 0; i < length; i++) {
array_base &dest = (array_base&)((E*)data)[i];
((array_info*)etype)->init (&dest);
etype->copy(&dest, &src_data[i]);
}
break;
}
case RECORD:
{
/* If the element type is not scalar then init memory to 0 */
memset(data, 0, mem_size);
/* Copy each element of the array */
for (int i = 0; i < length; i++) {
record_base &dest = (record_base&)((E*)data)[i];
((record_info*)etype)->init (&dest);
etype->copy(&dest, &src_data[i]);
}
break;
}
default:
{
/* The element is a scalar simply copy the entire data region */
memcpy(data, src_data, mem_size);
break;
}
}
return *this;
}
template<class E>
array_type<E> &array_type<E>::init(type_info_interface *ainfo)
{
info = (array_info*)ainfo;
info->add_ref();
int length = info->length;
type_info_interface *etype = info->element_type;
/* Allocate memory for the data */
const int mem_size = length * sizeof(E);
data = (char*)internal_dynamic_alloc(mem_size);
if (scalar(id((E*)NULL))) {
/* If the element type is scalar then create a single copy of the
* element and use this copy to initialize the other array
* elements */
E elem_init_val;
etype->init(&elem_init_val);
for (int i = 0; i < length; i++)
((E*)data)[i] = elem_init_val;
} else {
/* If the element type is not scalar then init memory to 0 */
memset(data, 0, mem_size);
/* Initialize each element of the array */
for (int i = 0; i < length; i++)
etype->init(&((E*)data)[i]);
}
return *this;
}
template<class E>
array_type<E> &array_type<E>::init(type_info_interface *ainfo, const E &elem_init_value)
{
info = (array_info*)ainfo;
info->add_ref();
int length = info->length;
/* Allocate memory for the data */
const int mem_size = length * sizeof(E);
data = (char*)internal_dynamic_alloc(mem_size);
/* Init data part */
if (scalar(id((E*)NULL))) {
// Create a local copy of the element value (important for speed
// optimizations)
const E elem_init_value_copy = elem_init_value;
for (int i = 0; i < length; i++)
((E*)data)[i] = elem_init_value_copy;
} else {
/* If the element type is not scalar then init memory to 0 */
memset(data, 0, mem_size);
type_info_interface *etype = info->element_type;
for (int i = 0; i < length; i++) {
etype->init(&((E*)data)[i]);
((E*)data)[i] = elem_init_value;
}
}
return *this;
}
/* Method to set array elements left to/downto right to value */
template<class E>
inline array_type<E> &array_type<E>::aggregate_set(int left, range_direction direction, int right, const E &value)
{
if (direction == downto) {
int tmp = left;
left = right;
right = tmp;
}
for (int i = left; i <= right; i++)
(*this)[i] = value;
return *this;
}
/* Method to copys array element with index copy_index to array
* elements left to/downto right to value */
template<class E>
inline array_type<E> &array_type<E>::aggregate_copy(int left, range_direction direction, int right, int copy_index)
{
if (direction == downto) {
int tmp = left;
left = right;
right = tmp;
}
E &value = ((E*)data)[copy_index];
for (int i = left; i <= right; i++)
(*this)[i] = value;
return *this;
}
/* *************************************************************
* The relational operators <,<=, >, >= are not defined for each array
* type. Hence, a separate set of template functions are defined here
* which define operator < (op_array_lt) and operator <=
* (op_array_le). The remaining relational operators are defined by
* combining < and <= with operator ! (this is done online when the
* C++ code is generated by the VHDL compiler).
* ************************************************************* */
template<class A, class B>
bool op_array_lt(const A &a1, const B &a2)
{
const int length1 = a1.info->length;
const int length2 = a2.info->length;
const int min_length = min(length1, length2);
/* Compare the data part of the arrays */
for (int i = 0; i < min_length; i++)
if (a1(i) != a2(i)) return a1(i) < a2(i);
return length1 < length2;
}
template<class A, class B>
bool op_array_le(const A &a1, const B &a2)
{
const int length1 = a1.info->length;
const int length2 = a2.info->length;
const int min_length = min(length1, length2);
/* Compare the data part of the arrays */
for (int i = 0; i < min_length; i++)
if (a1(i) != a2(i)) return a1(i) < a2(i);
return length1 <= length2;
}
/* *************************************************************
* The logical operators not, and, or, nor, xor, xnor are not defined
* for each array type. Hence, a separate set of template functions
* are defined.
* ************************************************************* */
template<class A>
A op_array_not (const A &a)
{
A result(a);
const int length = a.info->length;
/* Compare the data part of the arrays */
for (int i = 0; i < length; i++)
result(i) = op_not(a(i));
return result;
}
template<class A, class B>
A op_array_and (const A &a1, const B &a2)
{
A result(a1);
const int length = a1.info->length;
/* Check whether array bounds are compatible */
if (a1.info != a2.info && length != a2.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Handle data part of the arrays */
for (int i = 0; i < length; i++)
result(i) = a1(i) & a2(i);
return result;
}
template<class A, class B>
A op_array_or (const A &a1, const B &a2)
{
A result(a1);
const int length = a1.info->length;
/* Check whether array bounds are compatible */
if (a1.info != a2.info && length != a2.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Handle data part of the arrays */
for (int i = 0; i < length; i++)
result(i) = a1(i) | a2(i);
return result;
}
template<class A, class B>
A op_array_nand (const A &a1, const B &a2)
{
A result(a1);
const int length = a1.info->length;
/* Check whether array bounds are compatible */
if (a1.info != a2.info && length != a2.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Handle data part of the arrays */
for (int i = 0; i < length; i++)
result(i) = op_nand(a1(i), a2(i));
return result;
}
template<class A, class B>
A op_array_nor (const A &a1, const B &a2)
{
A result(a1);
const int length = a1.info->length;
/* Check whether array bounds are compatible */
if (a1.info != a2.info && length != a2.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Handle the data part of the arrays */
for (int i = 0; i < length; i++)
result(i) = op_nor(a1(i), a2(i));
return result;
}
template<class A, class B>
A op_array_xor (const A &a1, const B &a2)
{
A result(a1);
const int length = a1.info->length;
/* Check whether array bounds are compatible */
if (a1.info != a2.info && length != a2.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Handle data part of the arrays */
for (int i = 0; i < length; i++)
result(i) = op_xor(a1(i), a2(i));
return result;
}
template<class A, class B>
A op_array_xnor (const A &a1, const B &a2)
{
A result(a1);
const int length = a1.info->length;
/* Check whether array bounds are compatible */
if (a1.info != a2.info && length != a2.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Handle data part of the arrays */
for (int i = 0; i < length; i++)
result(i) = op_xnor(a1(i), a2(i));
return result;
}
template<class A>
A op_array_sll (const A &a, integer j)
{
typedef typename A::E_type E;
const int length = a.info->length;
if (length <= 0) return a;
const int shift = abs (j) % length;
/* Create result vector. Note that result is initialized with all
* elements set to element_type'left */
A result;
result.init (a.info);
if (shift == 0) return a;
else if (j > 0)
/* Handle data part of the arrays */
for (int i = 0; i < length - shift; i++)
((E*)result.data) [i] = ((E*)a.data) [i + shift];
else
for (int i = length - 1; i >= shift; i--)
((E*)result.data) [i] = ((E*)a.data) [i - shift];
return result;
}
template<class A>
A op_array_srl (const A &a, integer j)
{
return op_array_sll<A> (a, -j);
}
template<class A>
A op_array_sla (const A &a, integer j)
{
typedef typename A::E_type E;
const int length = a.info->length;
if (length <= 0) return a;
const int shift = abs (j) % length;
/* Create result vector. Note that result is initialized with all
* elements set to a[a'right] */
A result;
result.init (a.info, ((E*)a.data) [j > 0? length - 1 : 0]);
if (shift == 0) return a;
else if (j > 0)
/* Handle data part of the arrays */
for (int i = 0; i < length - shift; i++)
((E*)result.data) [i] = ((E*)a.data) [i + shift];
else
for (int i = length - 1; i >= shift; i--)
((E*)result.data) [i] = ((E*)a.data) [i - shift];
return result;
}
template<class A>
A op_array_sra(const A &a, integer j)
{
return op_array_sla (a, -j);
}
template<class A>
A op_array_rol (const A &a, integer j)
{
typedef typename A::E_type E;
const int length = a.info->length;
if (length <= 0) return a;
const int shift = abs (j) % length;
/* Create result vector */
A result;
result.init(a.info);
if (shift == 0) return a;
else if (j > 0)
/* Handle data part of the arrays */
for (int i = 0; i < length; i++)
((E*)result.data) [i] =
((E*)a.data) [i + shift - (i + shift >= length? length : 0)];
else
for (int i = 0; i < length; i++)
((E*)result.data) [i] =
((E*)a.data) [i - shift + (i - shift < 0? length : 0)];
return result;
}
template<class A>
A op_array_ror (const A &a, integer j)
{
return op_array_rol<A> (a, -j);
}
/* *************************************************************
* Array alias type class
* ************************************************************* */
/* array_alias class is a array class derived from an array_type class
* (T). This special class is used to build array alias instances of
* an array object. Basically, the alias class adds some new
* constructors and a destructor. The constructors do not create a new
* data array but reuse an existing array. Hence, the destructor does
* not deallocate the data part of the array!
*
* Note that T must be an array_type class! */
template<class T>
class array_alias : public T {
public:
array_alias(): T () {
array_base &array = *(array_base*)this;
array.info = NULL;
array.data = NULL;
}
array_alias(type_info_interface *et, type_info_interface *it,
int le, range_direction dir, int ri, int rc, void *iarray) : T () {
array_base &array = *(array_base*)this;
/* Create a new array_info instance */
array.info = new array_info(et, it, le, dir, ri, rc);
array.data = (char*)iarray;
}
/* Note that here the bounds of the alias are taken from the
* source array and NOT from the array base! */
array_alias(array_info *base, const array_base &abase) : T () {
array_base &array = *(array_base*)this;
const array_info &ainfo = *abase.info;
array.info = new array_info(base->element_type, base->index_type, ainfo.left_bound,
ainfo.index_direction, ainfo.right_bound, 1);
array.data = abase.data;
}
array_alias(array_info *ainfo, const void *iarray) : T () {
array_base &array = *(array_base*)this;
array.info = ainfo;
array.info->add_ref();
array.data = (char*)iarray;
}
array_alias &set(array_info *ainfo, void *iarray) {
array_base &array = *(array_base*)this;
if (array.info != NULL)
array.info->remove_ref();
array.info = ainfo;
array.info->add_ref();
array.data = (char*)iarray;
return *this;
}
~array_alias() {
/* Note that the data array is NOT deallocated as the memory has
* been allocated by another array_type instance */
array_base &array = *(array_base*)this;
array.data = NULL; /* Set pointer to NULL so that the destructor of
* class T will NOT remove the memory! */
}
array_alias &operator=(const T &a);
array_alias &operator=(const array_alias &a) { *this = (T&)a; return *this; }
};
template<class T>
array_alias<T> &array_alias<T>::operator=(const T &a)
{
array_base &array = *(array_base*)this;
int length = array.info->length;
typedef typename T::E_type E;
/* Check whether array bounds are compatible */
if (array.info != a.info && length != a.info->length)
error(ERROR_INCOMPATIBLE_ARRAYS);
/* Check whether the arrays overlap */
const int mem_size = length * sizeof(E);
if (abs((long)a.data - (long)array.data) >= mem_size) {
/* Ok, arrays do NOT overlap! */
/* Copy the data part of the arrays */
if (scalar(id((E*)NULL)))
/* If the element type is not scalar then execute init(...) for
* each element in the array after initialing the memory to 0 */
memcpy(array.data, a.data, mem_size);
else
for (int i = 0; i < length; i++)
((E*)array.data)[i] = ((E*)a.data)[i];
} else {
/* Attention: arrays overlap! */
/* Copy the data part of the arrays */
if (scalar(id((E*)NULL)))
/* If the element type is not scalar then execute init(...) for
* each element in the array after initialing the memory to
* 0. Note that twe are using memmove instead of memcpy here
* because the memory ranges of source and destination
* overlap. */
memmove(array.data, a.data, mem_size);
else {
/* Check out whether the lower border of the source array is
* located within the destination array as this affects the
* iteration direction. */
if (a.data >= array.data)
for (int i = 0; i < length; i++)
((E*)array.data)[i] = ((E*)a.data)[i];
else
for (int i = length - 1; i >= 0; i--)
((E*)array.data)[i] = ((E*)a.data)[i];
}
}
return *this;
}
/* *************************************************************
* Functions to implement VHDL file IO
* ************************************************************* */
enumeration file_eof(vhdlfile &file);
void file_close(vhdlfile &file);
void file_open(vhdlfile &file, const array_type<enumeration> &name, enumeration kind);
void file_open(enumeration &status, const vhdlfile &file, array_type<enumeration> &name, enumeration kind);
void file_read_scalar(vhdlfile &file, void *value_p, int size);
void file_read_array(vhdlfile &file, void *value_p);
void file_read_record(vhdlfile &file, void *value_p);
void file_read_array(vhdlfile &file, void *value_p, integer &length);
void file_write_scalar(vhdlfile &file, const void *value_p, int size);
void file_write_array(vhdlfile &file, const void *value_p);
void file_write_record(vhdlfile &file, const void *value_p);
/******************************************************
* Some definitions which are used by the kernel only
******************************************************/
#ifdef KERNEL // This is only required when the kernel is compiled
#include <freehdl/kernel-db.hh>
// Create a database key type for type_info_interface so that entries
// can be associated to it.
define_db_key_type (type_info_interface*, type_info_interface_p);
#endif
#endif
|