spu.c 50.5 KB
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
/* spu -- A program to make lots of random C/C++ code.
   Copyright (C) 1993, 1994, 2000 Free Software Foundation, Inc.
   Contributed by Cygnus Support.  Written by Stan Shebs.

This file is part of SPU.

SPU is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* This is a random program generator. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

char *version_string = "0.5";

/* These values are the builtin defaults, mainly useful for testing
   purposes, or if the user is uninterested in setting a value.  */

#define DEFAULT_NUM_FILES 5

#define DEFAULT_NUM_HEADER_FILES 1

#define DEFAULT_NUM_MACROS 10

#define DEFAULT_NUM_LIB_MACROS 30

#define DEFAULT_MAX_MACRO_ARGS 5

#define DEFAULT_NUM_ENUMS 10

#define DEFAULT_NUM_LIB_ENUMS 30

#define DEFAULT_NUM_ENUMERATORS 10

#define DEFAULT_NUM_STRUCTS 10

#define DEFAULT_NUM_LIB_STRUCTS 30

#define DEFAULT_NUM_FIELDS 20

#define DEFAULT_NUM_CLASSES 10

#define DEFAULT_NUM_LIB_CLASSES 30

#define DEFAULT_NUM_METHODS 20

#define DEFAULT_NUM_FUNCTIONS 100

#define DEFAULT_NUM_LIB_FUNCTIONS 300

#define DEFAULT_MAX_FUNCTION_ARGS 8

#define DEFAULT_FUNCTION_LENGTH 20

#define DEFAULT_FUNCTION_DEPTH 3

#define DEFAULT_LIB_PERCENT 10

/* Generic hash table.  */

struct hash_entry
{
  char *val;
  struct hash_entry *next;
};

struct hash_table
{
  struct hash_entry *entries[253];
  int numadds;
};

enum decl_types {
  d_nothing,
  d_macro,
  d_enum,
  d_struct,
  d_class,
  d_function
};

enum {
  t_nothing = 0,
  t_void = 1,
  t_int = 2,
  t_short = 3,
  t_char = 4,
  t_first_user = 100,
  t_char_ptr = 1000004
};

char *typenames[] = { "?", "void", "int", "short", "char" };

struct macro_desc
{
  int id;
  char *name;
  int numargs;
  char **args;
  int use;
};

struct enumerator_desc
{
  char *name;
};

struct enum_desc
{
  int id;
  char *name;
  int num_enumerators;
  struct enumerator_desc *enumerators;
  int use;
};

struct field_desc
{
  int type;
  char *name;
};

struct struct_desc
{
  int id;
  char *name;
  int numfields;
  struct field_desc *fields;
  int use;
};

/* (should add unions as type of struct) */

struct class_desc
{
  int id;
  char *name;
  int numfields;
  struct field_desc *fields;
  int nummethods;
  struct function_desc *methods;
  int use;
};

struct type_desc
{
  char *name;
};

struct arg_desc
{
  int type;
  char *name;
};

struct function_desc
{
  int id;
  char *name;
  int return_type;
  int numargs;
  struct arg_desc *args;
  struct class_desc *class;
  int use;
};

struct file_desc
{
  char *name;
};

struct decl_entry {
  enum decl_types type;
  union {
    struct macro_desc *macro_d;
    struct enum_desc *enum_d;
    struct struct_desc *struct_d;
    struct class_desc *class_d;
    struct function_desc *function_d;
  } decl;
  int seq;
  int order;
};

struct decl_table {
  int size;
  int nextentry;
  struct decl_entry *entries;
};

/* Function declarations.  */

void display_usage (void);

int hash_string (char *str);

struct hash_entry *get_hash_entry (void);

char *add_to_hash_table (char *buf, struct hash_table *table);

char *get_from_hash_table (char *buf, struct hash_table *table);

void init_xrandom (int seed);

int xrandom (int n);

int probability (int prob);

char *copy_string (char *str);

char *xmalloc (int n);

char *gen_unique_global_name (char *root, int upcase);

void gen_random_global_name (char *root, char *namebuf);

char *gen_random_local_name (int n, char **others);

void create_macros (void);

void create_macro (struct macro_desc *macrodesc);

char *gen_new_macro_name (void);

void create_enums (void);

void create_enum (struct enum_desc *enumdesc);

char *gen_random_enumerator_name (void);

void create_structs (void);

void create_struct (struct struct_desc *structdesc, int lib);

char *gen_random_field_name (int n);

void create_classes (void);

void create_class (struct class_desc *classdesc, int lib);

void create_functions (void);

void create_function (struct function_desc *fndesc, int lib);

void write_header_file (int n);

void write_lib_header_file (void);

void write_source_file (int n);

void write_lib_source_file (void);

void write_macro (FILE *fp, struct macro_desc *macrodesc);

void write_enum (FILE *fp, struct enum_desc *enumdesc);

void write_struct (FILE *fp, struct struct_desc *structdesc);

void write_class (FILE *fp, struct class_desc *classdesc);

void write_function_decl (FILE *fp, struct function_desc *fndesc);

void write_function (FILE *fp, struct function_desc *fndesc);

void write_lib_function (FILE *fp, int n);

void write_statement (FILE *fp, int depth, int max_depth);

void write_expression (FILE *fp, int rslttype, int depth, int max_depth,
		       int exclude_id);

void write_description_block (FILE *fp);

void write_makefile (void);

/* Global variables.  */

/* The possible languages. */

enum languages { knr, c, cpp, objc };

enum languages language = c;

/* Filename extensions to use with each language type.  */

char *extensions[] = { "c", "c", "cc", "m" };

/* Names for each language.  */

char *lang_names[] = { "K&R C", "standard C", "standard C++", "Objective-C" };

int num_files = DEFAULT_NUM_FILES;

int num_header_files = DEFAULT_NUM_HEADER_FILES;

char *file_base_name = "file";

int num_macros = DEFAULT_NUM_MACROS;

int num_lib_macros = DEFAULT_NUM_LIB_MACROS;

int num_enums = DEFAULT_NUM_ENUMS;

int num_lib_enums = DEFAULT_NUM_LIB_ENUMS;

int num_enumerators = DEFAULT_NUM_ENUMERATORS;

int num_structs = DEFAULT_NUM_STRUCTS;

int num_lib_structs = DEFAULT_NUM_LIB_STRUCTS;

int num_fields = DEFAULT_NUM_FIELDS;

int num_classes = DEFAULT_NUM_CLASSES;

int num_lib_classes = DEFAULT_NUM_LIB_CLASSES;

int num_methods = DEFAULT_NUM_METHODS;

int num_functions = DEFAULT_NUM_FUNCTIONS;

int num_lib_functions = DEFAULT_NUM_LIB_FUNCTIONS;

int max_function_args = DEFAULT_MAX_FUNCTION_ARGS;

int function_length = DEFAULT_FUNCTION_LENGTH;

int function_depth = DEFAULT_FUNCTION_DEPTH;

/* Percentage of library constructs that will be referenced.  */

int lib_percent = DEFAULT_LIB_PERCENT;

int randomize_order = 1;

int num_functions_per_file;

/* The amount of commenting in the source.  */

int commenting = 0;

/* Hash table for globally visible symbols.  */

struct hash_table *global_hash_table;

/* The seed for the random number generator.  */

int seed = -1;

int next_id = 1;

/* Space to record info about generated constructs.  */

struct macro_desc *macros;

struct macro_desc *lib_macros;

struct enum_desc *enums;

struct enum_desc *lib_enums;

struct struct_desc *structs;

struct struct_desc *lib_structs;

struct class_desc *classes;

struct class_desc *lib_classes;

struct function_desc *functions;

struct function_desc *lib_functions;

struct decl_table order;

struct decl_table lib_order;

int num_computer_terms;

/* Likely words to appear in names of things.  These must never be
   valid C/C++ keywords, since they may appear by themselves in some
   contexts.  */

char *computerese[] = {
  "add", "all", "alloc", "allocate", "area", "array", "at",
  "bogus", "buf", "buff", "buffer", "by", "btree",
  "ch", "chr", "clean", "cleanup", "count", "create", "cull",
  "data", "del", "delete_", "depth", "desc", "dest", "discard", "dismiss",
  "dma", "done", "dst",
  "fill", "find", "fn", "for_",
  "gc", "go", "goto_", "grok", "gronk", "group", "grovel",
  "hack", "hacked", "have", "heap",
  "in", "ind", "index", "ini", "init", "initial", "inside",
  "lab", "label", "last", "len", "length", "line", "lis", "list", "lose",
  "make", "mark", "mod", "modify", "more",
  "name", "nest", "nesting", "new_", "next", "node", "null", "num", "number",
  "part", "partial",
  "query", "queue",
  "ob", "obj", "object", "of",
  "pc", "pnt", "point", "pop", "pos", "position", "push",
  "raw", "recalc", "rect", "rectangle", "rel", "relative", "ret", "rslt",
  "remove", "reset", "rmv",
  "see", "set", "shape", "stack", "str", "string",
  "tab", "table", "tbl", "tag", "tree",
  "undel", "undo", "unmark", "use",
  "vary", "vec", "vect", "vector", "virt", "virtual_",
  "win", "wind", "window", "word",
  "zbuf",
  NULL
};

/* Return a word that commonly appears in programs. */

char *
random_computer_word (void)
{
  if (num_computer_terms == 0)
    {
      int i;

      for (i = 0; computerese[i] != NULL; ++i)
	;
      num_computer_terms = i;
    }
  return computerese[xrandom (num_computer_terms)];
}

int
main (int argc, char **argv)
{
  int i, num;
  char *arg;

  
  /* Parse all the arguments. */
  /* (should check on numeric values) */
  for (i = 1; i < argc; ++i)
    {
      arg = argv[i];
      if (strcmp(arg, "--basename") == 0)
	{
	  file_base_name = copy_string(argv[++i]);
	}
      else if (strcmp(arg, "--classes") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_classes = num;
	}
      else if (strcmp(arg, "--comments") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  commenting = num;
	}
      else if (strcmp(arg, "--enums") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_enums = num;
	}
      else if (strcmp(arg, "--enumerators") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_enumerators = num;
	}
      else if (strcmp(arg, "--fields") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_fields = num;
	}
      else if (strcmp(arg, "--files") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_files = num;
	}
      else if (strcmp(arg, "--functions") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_functions = num;
	}
      else if (strcmp(arg, "--function-length") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  function_length = num;
	}
      else if (strcmp(arg, "--function-depth") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  function_depth = num;
	}
      else if (strcmp(arg, "--header-files") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_header_files = num;
	}
      else if (strcmp(arg, "--help") == 0)
	{
	  display_usage ();
	  exit (0);
	}
      else if (strcmp(arg, "--language") == 0)
	{
	  if (strcmp (argv[i+1], "c") == 0)
	    language = c;
	  else if (strcmp (argv[i+1], "c++") == 0)
	    language = cpp;
	  else if (strcmp (argv[i+1], "knr") == 0)
	    language = knr;
	  else if (strcmp (argv[i+1], "objc") == 0)
	    language = objc;
	  ++i;
	}
      else if (strcmp(arg, "--lib-classes") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_lib_classes = num;
	}
      else if (strcmp(arg, "--lib-enums") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_lib_enums = num;
	}
      else if (strcmp(arg, "--lib-functions") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_lib_functions = num;
	}
      else if (strcmp(arg, "--lib-macros") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_lib_macros = num;
	}
      else if (strcmp(arg, "--lib-structs") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_lib_structs = num;
	}
      else if (strcmp(arg, "--macros") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_macros = num;
	}
      else if (strcmp(arg, "--methods") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_methods = num;
	}
      else if (strcmp(arg, "--seed") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  seed = num;
	}
      else if (strcmp(arg, "--structs") == 0)
	{
	  num = strtol (argv[++i], NULL, 10);
	  num_structs = num;
	}
      else if (strcmp(arg, "--version") == 0)
	{
	  fprintf (stderr, "SPU program generator version %s\n",
		   version_string);
	  exit (0);
	}
      else
	{
	  fprintf (stderr, "Usage: \"%s\" not valid, ignored\n", arg);
	  display_usage ();
	}
    }
  if (language != cpp)
    num_classes = num_lib_classes = 0;
  init_xrandom (seed);
  /* Round up the number of functions so each file gets the same
     number. */
  num_functions_per_file = (num_functions + num_files - 1) / num_files;
  num_functions = num_functions_per_file * num_files;
  /* Create the definitions of objects internally. */
  order.size =
    num_macros + num_enums + num_structs + num_classes + num_functions;
  order.nextentry = 0;
  order.entries =
    (struct decl_entry *) xmalloc (order.size * sizeof(struct decl_entry));
  lib_order.size = num_lib_macros + num_lib_enums + num_lib_structs + num_lib_classes + num_lib_functions;
  lib_order.nextentry = 0;
  lib_order.entries =
    (struct decl_entry *) xmalloc (lib_order.size * sizeof(struct decl_entry));
  create_macros ();
  create_enums ();
  create_structs ();
  create_functions ();
  create_classes ();
  /* Write out a bunch of files. */
  printf ("Writing %d header files...\n", num_header_files);
  for (i = 0; i < num_header_files; ++i)
    write_header_file (i);
  write_lib_header_file ();
  write_lib_source_file ();
  printf ("Writing %d files...\n", num_files);
  for (i = 0; i < num_files; ++i)
    write_source_file (i);
  /* Write out a makefile. */
  write_makefile ();
  /* Succeed if we actually wrote out a whole program correctly. */
  exit (0);
}

void
display_usage (void)
{
  fprintf (stderr, "Usage: spu [ ... options ... ]\n");
  fprintf (stderr, "\t--basename str (default \"%s\")\n", "file");
  fprintf (stderr, "\t--classes n (default %d)\n", DEFAULT_NUM_CLASSES);
  fprintf (stderr, "\t--comments n\n");
  fprintf (stderr, "\t--enums n (default %d)\n", DEFAULT_NUM_ENUMS);
  fprintf (stderr, "\t--enumerators n (default %d)\n", DEFAULT_NUM_ENUMERATORS);
  fprintf (stderr, "\t--fields n (default %d)\n", DEFAULT_NUM_FIELDS);
  fprintf (stderr, "\t--files n (default %d)\n", DEFAULT_NUM_FILES);
  fprintf (stderr, "\t--functions n (default %d)\n", DEFAULT_NUM_FUNCTIONS);
  fprintf (stderr, "\t--function-length n (default %d)\n", DEFAULT_FUNCTION_LENGTH);
  fprintf (stderr, "\t--function-depth n (default %d)\n", DEFAULT_FUNCTION_DEPTH);
  fprintf (stderr, "\t--header-files n (default %d)\n", DEFAULT_NUM_HEADER_FILES);
  fprintf (stderr, "\t--help\n");
  fprintf (stderr, "\t--language c|cpp|knr|objc (default c)\n");
  fprintf (stderr, "\t--lib-classes n (default %d)\n", DEFAULT_NUM_LIB_CLASSES);
  fprintf (stderr, "\t--lib-enums n (default %d)\n", DEFAULT_NUM_LIB_ENUMS);
  fprintf (stderr, "\t--lib-functions n (default %d)\n", DEFAULT_NUM_LIB_FUNCTIONS);
  fprintf (stderr, "\t--lib-macros n (default %d)\n", DEFAULT_NUM_LIB_MACROS);
  fprintf (stderr, "\t--lib-structs n (default %d)\n", DEFAULT_NUM_LIB_STRUCTS);
  fprintf (stderr, "\t--macros n (default %d)\n", DEFAULT_NUM_MACROS);
  fprintf (stderr, "\t--methods n (default %d)\n", DEFAULT_NUM_METHODS);
  fprintf (stderr, "\t--seed n\n");
  fprintf (stderr, "\t--structs n (default %d)\n", DEFAULT_NUM_STRUCTS);
  fprintf (stderr, "\t--version\n");
}

int
random_type (int libonly)
{
  int i, n;

  switch (xrandom (6))
    {
    case 0:
      return t_short;
    case 1:
      return t_char;
    case 2:
      return t_char_ptr;
    case 3:
      for (i = 0; i < 1000; ++i)
	{
	  n = xrandom (num_lib_structs);
	  if (lib_structs[n].id > 0 && lib_structs[n].use)
	    return t_first_user + lib_structs[n].id;
	}
      return t_int;
    case 4:
      if (libonly)
	return t_int;
      for (i = 0; i < 1000; ++i)
	{
	  n = xrandom (num_structs);
	  if (structs[n].id > 0)
	    return t_first_user + structs[n].id;
	}
      return t_int;
    default:
      return t_int;
    }
}

/* Given a numbered type, return its name.  */

char *
name_from_type (int n)
{
  int i;
  char tmpbuf[100];

  if (n < t_first_user)
    {
      return typenames[n];
    }
  else if (n >= 1000000 && (n - 1000000) < t_first_user)
    {
      sprintf (tmpbuf, "%s *", typenames[n - 1000000]);
      return copy_string (tmpbuf);
    }
  else
    {
      for (i = 0; i < num_structs; ++i)
	{
	  if (structs[i].id == (n - t_first_user))
	    {
	      sprintf (tmpbuf, "struct %s *", structs[i].name);
	      return copy_string (tmpbuf);
	    }
	}
      for (i = 0; i < num_lib_structs; ++i)
	{
	  if (lib_structs[i].id == (n - t_first_user))
	    {
	      sprintf (tmpbuf, "struct %s *", lib_structs[i].name);
	      return copy_string (tmpbuf);
	    }
	}
    }
  return "?type?";
}

void
add_decl_to_table (enum decl_types type, void *desc, struct decl_table *table)
{
  int n = table->nextentry++;

  table->entries[n].type = type;
  switch (type)
    {
    case d_macro:
      table->entries[n].decl.macro_d = (struct macro_desc *) desc;
      break;
    case d_enum:
      table->entries[n].decl.enum_d = (struct enum_desc *) desc;
      break;
    case d_struct:
      table->entries[n].decl.struct_d = (struct struct_desc *) desc;
      break;
    case d_class:
      table->entries[n].decl.class_d = (struct class_desc *) desc;
      break;
    case d_function:
      table->entries[n].decl.function_d = (struct function_desc *) desc;
      break;
    default:
      fprintf (stderr, "Unknown decl type %d in add_decl_to_table\n", type);
      break;
    }
  table->entries[n].seq = n;
  if (randomize_order)
    table->entries[n].order = xrandom (10000);
}

/* Create basic definitions of macros.  Negative number of arguments
   means a macros that looks like a constant instead of a function.  */

void
create_macros (void)
{
  int i;

  printf ("Creating %d macros...\n", num_macros);
  macros =
    (struct macro_desc *) xmalloc (num_macros * sizeof(struct macro_desc));
  for (i = 0; i < num_macros; ++i)
    {
      create_macro (&(macros[i]));
      add_decl_to_table (d_macro, &(macros[i]), &order);
    }

  /* It's important to create library macros second, so that their ids
     are higher than those for program macros.  This is so that
     library macro bodies are excluded from referencing any program
     macros (because of the exclude_id bits in write_expression).  */

  printf ("Creating %d library macros...\n", num_lib_macros);
  lib_macros =
    (struct macro_desc *) xmalloc (num_lib_macros * sizeof(struct macro_desc));
  for (i = 0; i < num_lib_macros; ++i)
    {
      create_macro (&(lib_macros[i]));
      if (!probability(lib_percent))
	lib_macros[i].use = 0;
      add_decl_to_table (d_macro, &(lib_macros[i]), &lib_order);
    }
}

void
create_macro (struct macro_desc *macrodesc)
{
  int j, numargs;

  macrodesc->id = next_id++;
  macrodesc->name = gen_new_macro_name ();
  numargs = xrandom (DEFAULT_MAX_MACRO_ARGS + 1);
  --numargs;
  if (numargs > 0)
    {
      macrodesc->args = (char **) xmalloc (numargs * sizeof(char *));
      for (j = 0; j < numargs; ++j)
	{
	  macrodesc->args[j] = gen_random_local_name (j, NULL);
	}
    }
  macrodesc->numargs = numargs;
  macrodesc->use = 1;
}

/* Generate a macro name.  */

char *
gen_new_macro_name (void)
{
  return gen_unique_global_name ("M", (xrandom (3) > 0));
}

/* Create definitions of the desired number of enums.  */

void
create_enums (void)
{
  int i;

  printf ("Creating %d enums...\n", num_enums);
  enums =
    (struct enum_desc *) xmalloc (num_enums * sizeof(struct enum_desc));
  for (i = 0; i < num_enums; ++i)
    {
      create_enum (&(enums[i]));
      add_decl_to_table (d_enum, &(enums[i]), &order);
    }
  printf ("Creating %d library enums...\n", num_lib_enums);
  lib_enums =
    (struct enum_desc *) xmalloc (num_lib_enums * sizeof(struct enum_desc));
  for (i = 0; i < num_lib_enums; ++i)
    {
      create_enum (&(lib_enums[i]));
      if (!probability(lib_percent))
	lib_enums[i].use = 0;
      add_decl_to_table (d_enum, &(lib_enums[i]), &lib_order);
    }
}

void
create_enum (struct enum_desc *enumdesc)
{
  int j, num;

  enumdesc->id = next_id++;
  /* Let some enums be anonymous. */
  if (xrandom (100) < 50)
    enumdesc->name = gen_unique_global_name (NULL, 0);
  num = num_enumerators / 2 + xrandom (num_enumerators);
  if (num <= 0)
    num = 1;
  enumdesc->enumerators =
    (struct enumerator_desc *) xmalloc (num * sizeof(struct enumerator_desc));
  for (j = 0; j < num; ++j)
    {
      enumdesc->enumerators[j].name = gen_random_enumerator_name ();
    }
  enumdesc->num_enumerators = j;
  enumdesc->use = 1;
}

/* Generate a unique enumerator within an enum.  */

char *
gen_random_enumerator_name (void)
{
  return gen_unique_global_name ("enum", 0);
}

/* Create definitions of the desired number of structures.  */

void
create_structs (void)
{
  int i;

  /* Do the library structs first, so that program structs may use
     them in their definitions.  */
  printf ("Creating %d library structs...\n", num_lib_structs);
  lib_structs =
    (struct struct_desc *) xmalloc (num_lib_structs * sizeof(struct struct_desc));
  for (i = 0; i < num_lib_structs; ++i)
    {
      create_struct (&(lib_structs[i]), 1);
      if (!probability(lib_percent))
	lib_structs[i].use = 0;
      add_decl_to_table (d_struct, &(lib_structs[i]), &lib_order);
    }

  printf ("Creating %d structs...\n", num_structs);
  structs =
    (struct struct_desc *) xmalloc (num_structs * sizeof(struct struct_desc));
  for (i = 0; i < num_structs; ++i)
    {
      create_struct (&(structs[i]), 0);
      add_decl_to_table (d_struct, &(structs[i]), &order);
    }
}

void
create_struct (struct struct_desc *structdesc, int lib)
{
  int j, numf;

  structdesc->id = next_id++;
  structdesc->name = gen_unique_global_name (NULL, 0);
  numf = xrandom (num_fields) + 1;
  structdesc->fields =
    (struct field_desc *) xmalloc (numf * sizeof(struct field_desc));
  for (j = 0; j < numf; ++j)
    {
      structdesc->fields[j].type = random_type (lib);
      structdesc->fields[j].name = gen_random_field_name (j);
    }
  structdesc->numfields = numf;
  structdesc->use = 1;
}

char *
gen_random_field_name (int n)
{
  char namebuf[100];

  /* (should have more variety) */
  sprintf (namebuf, "field%d", n);
  return copy_string (namebuf);
}

/* Create definitions of the desired number of classures.  */

void
create_classes (void)
{
  int i;

  /* Do the library classes first, so that program classes may use
     them in their definitions.  */
  printf ("Creating %d library classes...\n", num_lib_classes);
  lib_classes =
    (struct class_desc *) xmalloc (num_lib_classes * sizeof(struct class_desc));
  for (i = 0; i < num_lib_classes; ++i)
    {
      create_class (&(lib_classes[i]), 1);
      if (!probability(lib_percent))
	lib_classes[i].use = 0;
      add_decl_to_table (d_class, &(lib_classes[i]), &lib_order);
    }

  printf ("Creating %d classes...\n", num_classes);
  classes =
    (struct class_desc *) xmalloc (num_classes * sizeof(struct class_desc));
  for (i = 0; i < num_classes; ++i)
    {
      create_class (&(classes[i]), 0);
      add_decl_to_table (d_class, &(classes[i]), &order);
    }
}

void
create_class (struct class_desc *classdesc, int lib)
{
  int j, numf, numm;

  classdesc->id = next_id++;
  classdesc->name = gen_unique_global_name (NULL, 0);
  numf = xrandom (num_fields) + 1;
  classdesc->fields =
    (struct field_desc *) xmalloc (numf * sizeof(struct field_desc));
  for (j = 0; j < numf; ++j)
    {
      classdesc->fields[j].type = random_type (lib);
      classdesc->fields[j].name = gen_random_field_name (j);
    }
  classdesc->numfields = numf;
  numm = xrandom (num_methods + 1);
  classdesc->methods =
    (struct function_desc *) xmalloc (numm * sizeof(struct function_desc));
  for (j = 0; j < numm; ++j)
    {
      create_function (&(classdesc->methods[j]), lib);
      classdesc->methods[j].class = classdesc;
    }
  classdesc->nummethods = numm;
  classdesc->use = 1;
}

/* Create a number of functions with random numbers and types of
   arguments. */

void
create_functions (void)
{
  int i;

  printf ("Creating %d functions...\n", num_functions);
  functions =
    (struct function_desc *) xmalloc (num_functions * sizeof(struct function_desc));

  /* Generate the main program, as the first function.  */
  functions[0].id = next_id++;
  functions[0].name = "main";
  functions[0].return_type = t_int;
  functions[0].numargs = 0;
  functions[0].use = 1;
  add_decl_to_table (d_function, &(functions[0]), &order);

  /* Generate all the other functions.  */
  for (i = 1; i < num_functions; ++i)
    {
      create_function (&(functions[i]), 0);
      add_decl_to_table (d_function, &(functions[i]), &order);
#if 0 /* use forward decls for now instead */
      {
	int j, type;
	struct function_desc *fndesc = &(functions[i]);
      
	for (j = 0; j < fndesc->numargs; ++j)
	  {
	    type = fndesc->args[i].type;
	    if (type >= t_first_user)
	      {
		/* (should find arg types and increase fndesc->order) */
	      }
	  }
      }
#endif
    }

  printf ("Creating %d library functions...\n", num_lib_functions);
  lib_functions =
    (struct function_desc *) xmalloc (num_lib_functions * sizeof(struct function_desc));
  for (i = 0; i < num_lib_functions; ++i)
    {
      create_function (&(lib_functions[i]), 1);
      /* Mark some functions as not to be referenced from the program.  */
      if (!probability(lib_percent))
	lib_functions[i].use = 0;
      add_decl_to_table (d_function, &(lib_functions[i]), &lib_order);
    }
}

/* Generate the details of a single function.  */

void
create_function (struct function_desc *fndesc, int lib)
{
  int j, range, numargs;

  fndesc->id = next_id++;
  fndesc->name = gen_unique_global_name ("fn", 0);
  fndesc->return_type = ((xrandom (4) == 0) ? t_void : random_type (lib));
  /* Choose the number of arguments, preferring shorter argument lists
     by using a simple binomial distribution that is "folded" in the
     middle so zero-arg functions are the most common.  */
  range = 2 * (max_function_args + 1);
  numargs = 0;
  for (j = 0; j < 6; ++j)
    numargs += xrandom (range + 1);
  if (j > 0)
    numargs /= j;
  /* Shift distribution so 0 is in the middle.  */
  numargs -= max_function_args;
  /* Fold negative values over to positive side.  */
  if (numargs < 0)
    numargs = -numargs;
  if (numargs > max_function_args)
    numargs = max_function_args;
  if (numargs > 0)
    {
      fndesc->args =
	(struct arg_desc *) xmalloc (numargs * sizeof(struct arg_desc));
      for (j = 0; j < numargs; ++j)
	{
	  fndesc->args[j].type = random_type (lib);
	  fndesc->args[j].name = gen_random_local_name (j, NULL);
	}
    }
  fndesc->numargs = numargs;
  fndesc->class = NULL;
  fndesc->use = 1;
}

int
compare_entries(const void *x1, const void *x2)
{
  struct decl_entry *e1 = (struct decl_entry *) x1;
  struct decl_entry *e2 = (struct decl_entry *) x2;

  if (e1->order != e2->order)
    return (e1->order - e2->order);
  /* Randomized order may have pairs of matching numbers, so use this
     as fallback.  */
  return (e1->seq - e2->seq);
}

void
write_header_file (int n)
{
  int i;
  char tmpbuf[100];
  FILE *fp;

  sprintf (tmpbuf, "%s%d.h", file_base_name, n);
  fp = fopen (tmpbuf, "w");
  if (fp == NULL)
    return;
  write_description_block (fp);
  if (commenting > 0)
    fprintf (fp, "/* header */\n");
  /* Ensure that structure decls exist before functions mentioning them.  */
  if (randomize_order)
    {
      fprintf (fp, "/* forward decls */\n");
      for (i = 0; i < num_structs; ++i)
	fprintf (fp, "struct %s;\n", structs[i].name);
      for (i = 0; i < num_classes; ++i)
	fprintf (fp, "class %s;\n", classes[i].name);
      fprintf (fp, "\n");
    }
  qsort(order.entries, order.size, sizeof(struct decl_entry),
	compare_entries);
  for (i = 0; i < order.size; ++i)
    {
      switch (order.entries[i].type)
	{
	case d_macro:
	  write_macro (fp, order.entries[i].decl.macro_d);
	  break;
	case d_enum:
	  write_enum (fp, order.entries[i].decl.enum_d);
	  break;
	case d_struct:
	  write_struct (fp, order.entries[i].decl.struct_d);
	  break;
	case d_class:
	  write_class (fp, order.entries[i].decl.class_d);
	  break;
	case d_function:
	  write_function_decl (fp, order.entries[i].decl.function_d);
	  break;
	default:
	  fprintf (stderr, "Unknown decl type %d in write_header_file\n",
		   order.entries[i].type);
	  break;
	}
    }
  fclose (fp);
}

void
write_lib_header_file (void)
{
  int i;
  char tmpbuf[100];
  FILE *fp;

  sprintf (tmpbuf, "%slib.h", file_base_name);
  fp = fopen (tmpbuf, "w");
  if (fp == NULL)
    return;
  if (commenting > 0)
    fprintf (fp, "/* library header */\n");
  write_description_block (fp);
  /* Ensure that structure decls exist before functions mentioning them.  */
  if (randomize_order)
    {
      fprintf (fp, "/* forward decls */\n");
      for (i = 0; i < num_lib_structs; ++i)
	fprintf (fp, "struct %s;\n", lib_structs[i].name);
      for (i = 0; i < num_lib_classes; ++i)
	fprintf (fp, "class %s;\n", lib_classes[i].name);
      fprintf (fp, "\n");
    }
  qsort(lib_order.entries, lib_order.size, sizeof(struct decl_entry),
	compare_entries);
  for (i = 0; i < lib_order.size; ++i)
    {
      switch (lib_order.entries[i].type)
	{
	case d_macro:
	  write_macro (fp, lib_order.entries[i].decl.macro_d);
	  break;
	case d_enum:
	  write_enum (fp, lib_order.entries[i].decl.enum_d);
	  break;
	case d_struct:
	  write_struct (fp, lib_order.entries[i].decl.struct_d);
	  break;
	case d_class:
	  write_class (fp, lib_order.entries[i].decl.class_d);
	  break;
	case d_function:
	  write_function_decl (fp, lib_order.entries[i].decl.function_d);
	  break;
	default:
	  fprintf (stderr, "Unknown decl type %d in write_header_file\n",
		   lib_order.entries[i].type);
	  break;
	}
    }
  fclose (fp);
}

void
write_macro (FILE *fp, struct macro_desc *macrodesc)
{
  int j;

  fprintf (fp, "\n#define %s", macrodesc->name);
  /* Negative # arguments indicates an argumentless macro instead of
     one with zero arguments. */
  if (macrodesc->numargs >= 0)
    {
      fprintf (fp, "(");
      for (j = 0; j < macrodesc->numargs; ++j)
	{
	  if (j > 0)
	    fprintf (fp, ",");
	  fprintf (fp, "%s", macrodesc->args[j]);
	}
      fprintf (fp, ")");
    }
  /* Generate a macro body. */
  fprintf (fp, " (");
  switch (xrandom(4))
    {
    case 0:
      write_expression (fp, t_int, 0, 2, macrodesc->id);
      break;
    case 1:
      /* A very common expansion for macros.  */
      fprintf (fp, "0");
      break;
    case 2:
      /* Likewise.  */
      fprintf (fp, "1");
      break;
    default:
      fprintf (fp, "%d", xrandom (100));
      break;
    }
  fprintf (fp, ")");
  fprintf (fp, "\n\n");
}

/* Write out the definition of a enum. */

void
write_enum (FILE *fp, struct enum_desc *enumdesc)
{
  int j;

  fprintf (fp, "\nenum");
  if (enumdesc->name)
    fprintf (fp, " %s", enumdesc->name);
  fprintf (fp, " {");
  for (j = 0; j < enumdesc->num_enumerators; ++j)
    {
      if (j > 0)
	fprintf (fp, ",");
      fprintf (fp, "\n  %s", enumdesc->enumerators[j].name);
    }
  fprintf (fp, "\n};\n\n");
}

/* Write out the definition of a structure. */

void
write_struct (FILE *fp, struct struct_desc *structdesc)
{
  int j;

  fprintf (fp, "\nstruct %s {\n", structdesc->name);
  for (j = 0; j < structdesc->numfields; ++j)
    {
      fprintf (fp, "  %s %s;\n",
	       name_from_type (structdesc->fields[j].type),
	       structdesc->fields[j].name);
    }
  fprintf (fp, "};\n\n");
}

/* Write out the definition of a classure. */

void
write_class (FILE *fp, struct class_desc *classdesc)
{
  int j;

  fprintf (fp, "\nclass %s {\n", classdesc->name);
  fprintf (fp, "public:\n");
  for (j = 0; j < classdesc->numfields; ++j)
    {
      fprintf (fp, "  %s %s;\n",
	       name_from_type (classdesc->fields[j].type),
	       classdesc->fields[j].name);
    }
  for (j = 0; j < classdesc->nummethods; ++j)
    {
      write_function (fp, &(classdesc->methods[j]));
    }
  fprintf (fp, "};\n\n");
}

void
write_function_decl (FILE *fp, struct function_desc *fndesc)
{
  int i;

  fprintf (fp, "extern %s %s (",
	   name_from_type (fndesc->return_type), fndesc->name);
  if (language != knr)
    {
      for (i = 0; i < fndesc->numargs; ++i)
	{
	  fprintf (fp, "%s %s",
		   name_from_type (fndesc->args[i].type),
		   fndesc->args[i].name);
	  if (i + 1 < fndesc->numargs)
	    fprintf (fp, ", ");
	}
    }
  fprintf (fp, ");\n");
}

/* Write a complete source file. */

void
write_source_file (int n)
{
  char tmpbuf[100];
  int j;
  FILE *fp;

  sprintf (tmpbuf, "%s%d.%s",
	   file_base_name, n, extensions[language]);
  fp = fopen (tmpbuf, "w");
  if (fp == NULL)
    return;
  write_description_block (fp);
  if (1 /*num_lib_header_files*/ > 0)
    {
      for (j = 0; j < 1 /*num_header_files*/; ++j)
	{
	  fprintf (fp, "#include \"%slib.h\"\n", file_base_name);
	}
      fprintf (fp, "\n");
    }
  if (num_header_files > 0)
    {
      for (j = 0; j < num_header_files; ++j)
	{
	  fprintf (fp, "#include \"%s%d.h\"\n", file_base_name, j);
	}
      fprintf (fp, "\n");
    }

  if (n == 0)
    printf ("  (Each file contains %d functions)\n",
	    num_functions_per_file);

  for (j = 0; j < num_functions_per_file; ++j)
    {
      write_function (fp, &(functions[n * num_functions_per_file + j]));
    }
  fclose (fp);
}

/* (should add option to define methods separately) */

void
write_function (FILE *fp, struct function_desc *fndesc)
{
  int i, k;

  if (fndesc->class)
    fprintf (fp, "  ");
  fprintf (fp, "%s", name_from_type (fndesc->return_type));
  fprintf (fp, (fndesc->class ? " " : "\n"));
  fprintf (fp, "%s (", fndesc->name);
  for (i = 0; i < fndesc->numargs; ++i)
    {
      if (language != knr)
	{
	  fprintf (fp, "%s ", name_from_type (fndesc->args[i].type));
	}
      fprintf (fp, "%s", fndesc->args[i].name);
      if (i + 1 < fndesc->numargs)
	fprintf (fp, ", ");
    }
  fprintf(fp, ")");
  fprintf (fp, (fndesc->class ? " " : "\n"));
  if (language == knr)
    {
      for (i = 0; i < fndesc->numargs; ++i)
	{
	  fprintf (fp, "%s %s;\n",
		   name_from_type (fndesc->args[i].type),
		   fndesc->args[i].name);
	}
    }
  fprintf(fp, "{");
  fprintf (fp, (fndesc->class ? " " : "\n"));
  if (fndesc->class)
    {
      /* (should generate something for the method sometimes) */
    }
  else
    {
      /* Generate a plausible function body by writing a number of
	 statements. */
      for (k = 0; k < function_length; ++k)
	{
	  write_statement (fp, 1, function_depth - 1 + xrandom (3));
	}
    }
  /* Write a return statement if appropriate.  */
  if (fndesc->return_type != t_void)
    {
      fprintf (fp, "  return 0;");
      fprintf (fp, (fndesc->class ? " " : "\n"));
    }
  fprintf (fp, "}");
  if (fndesc->class)
    fprintf (fp, ";");
  fprintf (fp, "\n");
}

/* Write "library source", which really just means empty function
   bodies, done so the program will link.  */

void
write_lib_source_file (void)
{
  char tmpbuf[100];
  int j;
  FILE *fp;

  sprintf (tmpbuf, "%slib.%s", file_base_name, extensions[language]);
  fp = fopen (tmpbuf, "w");
  if (fp == NULL)
    return;
  write_description_block (fp);
  if (1 /*num_lib_header_files*/ > 0)
    {
      for (j = 0; j < 1 /*num_lib_header_files*/; ++j)
	{
	  fprintf (fp, "#include \"%slib.h\"\n", file_base_name);
	}
      fprintf (fp, "\n");
    }

  for (j = 0; j < num_lib_functions; ++j)
    {
      write_lib_function (fp, j);
    }
  fclose (fp);
}

/* Generate empty bodies for library function definitions.  */

void
write_lib_function (FILE *fp, int n)
{
  int i;

  fprintf (fp, "%s\n%s (",
	   name_from_type (lib_functions[n].return_type),
	   lib_functions[n].name);
  for (i = 0; i < lib_functions[n].numargs; ++i)
    {
      if (language != knr)
	{
	  fprintf (fp, "%s ", name_from_type (lib_functions[n].args[i].type));
	}
      fprintf (fp, "%s", lib_functions[n].args[i].name);
      if (i + 1 < lib_functions[n].numargs)
	fprintf (fp, ", ");
    }
  fprintf (fp, ")");
  if (!lib_functions[n].use)
    fprintf (fp, " /* unused */");
  fprintf (fp, "\n");
  if (language == knr)
    {
      for (i = 0; i < lib_functions[n].numargs; ++i)
	{
	  fprintf (fp, "%s %s;\n",
		   name_from_type (lib_functions[n].args[i].type),
		   lib_functions[n].args[i].name);
	}
    }
  fprintf (fp, "{\n");
  if (lib_functions[n].return_type != t_void)
    fprintf (fp, "  return 0;\n");
  fprintf (fp, "}\n\n");
}

void
write_statement (FILE *fp, int depth, int max_depth)
{
  int i;

  for (i = 0; i < depth; ++i)
    fprintf (fp, "  ");
  /* Always do non-recursive statements if going too deep. */
  if (depth >= max_depth)
    {
      write_expression (fp, t_void, 0, xrandom(4) + 1, 0);
      fprintf (fp, ";\n");
      return;
    }
  switch (xrandom(2))
    {
    case 0:
      write_expression (fp, t_void, 0, xrandom(4) + 1, 0);
      fprintf (fp, ";");
      break;
    case 1:
      fprintf (fp, "if (");
      write_expression (fp, t_int, 0, xrandom(2) + 1, 0);
      fprintf (fp, ") {\n");
      write_statement(fp, depth + 1, max_depth);
      for (i = 0; i < depth; ++i)
	fprintf (fp, "  ");
      fprintf (fp, "}");
      break;
    }
  fprintf(fp, "\n");
}

/* Write a single expression. */

void cast_integer_type (FILE *fp, int type);
struct function_desc *find_function (int rslttype,
				     struct function_desc *fns, int numfns);
struct macro_desc *find_macro (int rslttype,
			       struct macro_desc *macs, int nummacros,
			       int exclude_id);

void
write_expression (FILE *fp, int rslttype, int depth, int max_depth,
		  int exclude_id)
{
  int n, n2, j;
  struct macro_desc *macrodesc;
  struct function_desc *fndesc;

  /* Always do non-recursive statements if going too deep. */
  if (depth >= max_depth)
    {
      switch (xrandom(10))
	{
	case 7:
	  cast_integer_type (fp, rslttype);
	  fprintf (fp, "%d", xrandom (1000));
	  break;
	default:
	  cast_integer_type (fp, rslttype);
	  fprintf (fp, "%d", xrandom (127));
	  break;
	}
      return;
    }
  switch (xrandom(10))
    {
    case 0:
      fndesc = find_function (rslttype, lib_functions, num_lib_functions);
      if (fndesc == NULL)
	{
	  cast_integer_type (fp, rslttype);
	  fprintf (fp, "%d", xrandom (100));
	  return;
	}
      fprintf (fp, "%s (", fndesc->name);
      for (j = 0; j < fndesc->numargs; ++j)
	{
	  if (j > 0)
	    fprintf (fp, ", ");
	  write_expression (fp, fndesc->args[j].type, depth + 1,
			    max_depth - 1, exclude_id);
	}
      fprintf(fp, ")");
      break;
    case 7:
      fndesc = find_function (rslttype, functions, num_functions);
      if (fndesc == NULL)
	{
	  cast_integer_type (fp, rslttype);
	  fprintf (fp, "%d", xrandom (100));
	  return;
	}
      fprintf (fp, "%s (", fndesc->name);
      for (j = 0; j < fndesc->numargs; ++j)
	{
	  if (j > 0)
	    fprintf (fp, ", ");
	  write_expression (fp, fndesc->args[j].type, depth + 1,
			    max_depth - 1, exclude_id);
	}
      fprintf(fp, ")");
      break;
    case 1:
    case 6:
      macrodesc = find_macro (t_int, lib_macros, num_lib_macros, exclude_id);
      if (macrodesc == NULL)
	{
	  cast_integer_type (fp, rslttype);
	  fprintf (fp, "%d", xrandom (100));
	  return;
	}
      cast_integer_type (fp, rslttype);
      fprintf (fp, "%s", macrodesc->name);
      if (macrodesc->numargs >= 0)
	{
	  fprintf (fp, " (");
	  for (j = 0; j < macrodesc->numargs; ++j)
	    {
	      if (j > 0)
		fprintf (fp, ", ");
	      write_expression (fp, t_int, depth + 1, max_depth - 1,
				exclude_id);
	    }
	  fprintf (fp, ")");
	}
      break;
    case 8:
      macrodesc = find_macro (t_int, macros, num_macros, exclude_id);
      if (macrodesc == NULL)
	{
	  cast_integer_type (fp, rslttype);
	  fprintf (fp, "%d", xrandom (100));
	  return;
	}
      cast_integer_type (fp, rslttype);
      fprintf (fp, "%s", macrodesc->name);
      if (macrodesc->numargs >= 0)
	{
	  fprintf (fp, " (");
	  for (j = 0; j < macrodesc->numargs; ++j)
	    {
	      if (j > 0)
		fprintf (fp, ", ");
	      write_expression (fp, t_int, depth + 1, max_depth - 1,
				exclude_id);
	    }
	  fprintf (fp, ")");
	}
      break;
    case 2:
      cast_integer_type (fp, rslttype);
      fprintf (fp, "(");
      write_expression (fp, t_int, depth + 1, max_depth, exclude_id);
      fprintf (fp, " + ");
      write_expression (fp, t_int, depth + 1, max_depth, exclude_id);
      fprintf (fp, ")");
      break;
    case 3:
      cast_integer_type (fp, rslttype);
      fprintf (fp, "(");
      write_expression (fp, t_int, depth + 1, max_depth, exclude_id);
      fprintf (fp, " - ");
      write_expression (fp, t_int, depth + 1, max_depth, exclude_id);
      fprintf (fp, ")");
      break;
    case 4:
      cast_integer_type (fp, rslttype);
      fprintf (fp, "(");
      write_expression (fp, t_int, depth + 1, max_depth, exclude_id);
      fprintf (fp, " * ");
      write_expression (fp, t_int, depth + 1, max_depth, exclude_id);
      fprintf (fp, ")");
      break;
    case 5:
      cast_integer_type (fp, rslttype);
      n = xrandom (num_enums);
      n2 = xrandom (enums[n].num_enumerators);
      fprintf (fp, "%s", enums[n].enumerators[n2].name);
      break;
    default:
      cast_integer_type (fp, rslttype);
      fprintf (fp, "%d", xrandom (127));
      break;
    }
}

void
cast_integer_type (FILE *fp, int type)
{
  if (type == t_void || type == t_int)
    return;
  fprintf (fp, "(%s) ", name_from_type (type));
}

struct macro_desc *
find_macro (int rslttype, struct macro_desc *macs, int nummacros,
	    int exclude_id)
{
  int j, n;

  for (j = 0; j < 1000; ++j)
    {
      n = xrandom (nummacros);
      if (macs[n].use && macs[n].id > exclude_id)
	return &(macs[n]);
    }
  return NULL;
}

/* Find a function that has the right return type.  */

struct function_desc *
find_function (int rslttype, struct function_desc *fns, int numfns)
{
  int j, n;

  /* Try several times, but eventually give up.  */
  for (j = 0; j < 1000; ++j)
    {
      n = xrandom (numfns);
      if (fns[n].use && fns[n].return_type == rslttype)
	return &(fns[n]);
    }
  return NULL;
}

/* Write out a comment block that lists all the parameters used to
   generate this program.  */

void
write_description_block (FILE *fp)
{
  extern unsigned long initial_randstate;

  fprintf (fp, "/* A fine software product by SPU %s.  */\n",
	   version_string);
  fprintf (fp, "/* Written in %s. */\n", lang_names[language]);
  fprintf (fp, "/* Program: %d macros, %d enums, %d structs, %d classes, %d functions, */\n",
	   num_macros, num_enums, num_structs, num_classes, num_functions);
  fprintf (fp, "/* divided into %d source file(s) and %d header(s).  */\n",
	   num_files, num_header_files);
  fprintf (fp, "/* Library: %d macros, %d enums, %d structs, %d classes, %d functions. */\n",
	   num_lib_macros, num_lib_enums, num_lib_structs, num_lib_classes,
	   num_lib_functions);
  fprintf (fp, "/* Enumerators per enum range from %d to %d.  */\n",
	   num_enumerators / 2, num_enumerators + 1);
  fprintf (fp, "/* Fields per struct/class range from %d to %d.  */\n",
	   1, num_fields);
  if (num_classes > 0 || num_lib_classes > 0)
    fprintf (fp, "/* Methods per class range from %d to %d.  */\n",
	     0, num_methods);
  fprintf (fp, "/* Function length is %d statements, expression depth is %d.  */\n",
	   function_length, function_depth);
  fprintf (fp, "/* Random seed is %d.  */\n", (int) initial_randstate);
  fprintf (fp, "\n");
}

/* Write out a makefile that will compile the program just generated. */

void
write_makefile (void)
{
  char tmpbuf[100];
  int i, j;
  FILE *fp;

  sprintf (tmpbuf, "%s.mk", file_base_name);
  fp = fopen (tmpbuf, "w");
  if (fp)
    {
      /* Name the compiler so we can change it easily.  */
      fprintf (fp, "CC = cc\n\n");
      /* Write dependencies and action line for the executable.  */
      fprintf (fp, "%s.out:	", file_base_name);
      for (i = 0; i < num_files; ++i)
	fprintf (fp, " %s%d.o", file_base_name, i);
      fprintf (fp, " %slib.o", file_base_name);
      fprintf (fp, "\n");
      fprintf (fp, "\t$(CC) -o %s.out", file_base_name);
      for (i = 0; i < num_files; ++i)
	fprintf (fp, " %s%d.o", file_base_name, i);
      fprintf (fp, " %slib.o", file_base_name);
      fprintf (fp, "\n\n");
      /* Write dependencies for individual files. */
      for (i = 0; i < num_files; ++i)
	{
	  fprintf (fp, " %s%d.o:	%s%d.%s",
		   file_base_name, i, file_base_name, i,
		   extensions[language]);
	  for (j = 0; j < num_header_files; ++j)
	    fprintf (fp, " %s%d.h", file_base_name, j);
	  fprintf (fp, "\n");
	  fprintf (fp, "\t$(CC) -c %s%d.%s\n",
		   file_base_name, i, extensions[language]);
	}

      /* Library stuff.  */
      fprintf (fp, "\nlib:	%slib.o\n\n", file_base_name);
      fprintf (fp, " %slib.o:	%slib.%s %slib.h",
	       file_base_name, file_base_name, extensions[language],
	       file_base_name);
      fprintf (fp, "\n");
      fprintf (fp, "\t$(CC) -c %slib.%s\n",
	       file_base_name, extensions[language]);
      fclose (fp);
    }
}

/* Utility/general functions. */

/* Generate a name that is guaranteed to be a valid C/C++ identifier.  */

char *
gen_unique_global_name (char *root, int upcase)
{
  int i, j;
  char *str, namebuf[100];

  if (global_hash_table == NULL)
    {
      global_hash_table =
	(struct hash_table *) xmalloc (sizeof (struct hash_table));
    }
  str = NULL;
  /* Keep trying until we get a unique name. */
  for (i = 0; i < 10000; ++i)
    {
      gen_random_global_name (root, namebuf);
      if (upcase)
	{
	  for (j = 0; namebuf[j] != '\0'; ++j)
	    namebuf[j] = toupper (namebuf[j]);
	}
      if (get_from_hash_table (namebuf, global_hash_table) == NULL)
	{
	  str = add_to_hash_table (namebuf, global_hash_table);
	  break;
	}
    }
  if (str == NULL)
    {
      fprintf (stderr, "Can't get a unique name!\n");
      exit(1);
    }
  return str;
}

/* Synthesize a random name that is a valid C/C++ identifier and that
   "looks like" something one might see in a program.  */

void
gen_random_global_name (char *root, char *namebuf)
{
  char smallbuf[100];
  int n, i, len;

  namebuf[0] = '\0';
  switch (xrandom (4))
    {
    case 0:
      namebuf[0] = 'a' + xrandom (26);
      namebuf[1] = '\0';
      break;
    case 1:
      /* Convert a random number into a string, maybe with some
	 underscores thrown in for flavor.  */
      n = xrandom (10000);
      i = 0;
      while (n > 0)
	{
	  if (xrandom (6) == 0)
	    namebuf[i++] = '_';
	  namebuf[i++] = 'a' + (n % 26);
	  n /= 26;
	}
      namebuf[i] = '\0';
      break;
    default:
      strcat (namebuf, random_computer_word ());
      break;
    }
  if (root != NULL)
    {
      strcat (namebuf, "_");
      strcat (namebuf, root);
    }
  switch (xrandom (5))
    {
    case 0:
      strcat (namebuf, "_");
      len = strlen (namebuf);
      namebuf[len] = 'a' + xrandom (26);
      namebuf[len + 1] = '\0';
      break;
    case 1:
      strcat (namebuf, "_");
      sprintf (smallbuf, "%d", xrandom (10000));
      strcat (namebuf, smallbuf);
      break;
    case 2:
      n = xrandom (10000);
      len = strlen (namebuf);
      i = len;
      while (n > 0)
	{
	  if (xrandom (6) == 0)
	    namebuf[i++] = '_';
	  namebuf[i++] = 'a' + (n % 26);
	  n /= 26;
	}
      namebuf[i] = '\0';
      break;
    default:
      strcat (namebuf, "_");
      strcat (namebuf, random_computer_word ());
      break;
    }
  if (xrandom (5) == 0)
    {
      strcat (namebuf, "_");
      sprintf (smallbuf, "%d", xrandom (10000));
      strcat (namebuf, smallbuf);
    }
#if 0 /* enable to study random name distribution */
  printf ("Try %s\n", namebuf);
#endif
}

/* Generate a local variable name. */

char *
gen_random_local_name (int numothers, char **others)
{
  char namebuf[100];

  sprintf (namebuf, "%s%d",
	   (xrandom (2) == 0 ? random_computer_word () : "arg"),
	   numothers + 1);
  return copy_string (namebuf);
}

/* Generic hash table code. */

int
hash_string (char *str)
{
  int i, rslt;

  rslt = 0;
  for (i = 0; str[i] != '\0'; ++i)
    {
      rslt ^= str[i];
      rslt %= 253;
    }
  return rslt;
}

struct hash_entry *
get_hash_entry (void)
{
  return (struct hash_entry *) xmalloc (sizeof (struct hash_entry));
}

char *
add_to_hash_table (char *buf, struct hash_table *table)
{
  int hash;
  struct hash_entry *ent, *lastent;

  if (buf == NULL)
    buf = "";
  ++(table->numadds);
  hash = hash_string (buf);
  if (table->entries[hash] == NULL)
    {
      table->entries[hash] = get_hash_entry ();
      table->entries[hash]->val = copy_string (buf);
      return table->entries[hash]->val;
    }
  else
    {
      for (ent = table->entries[hash]; ent != NULL; ent = ent->next)
	{
	  if (ent->val == NULL)
	    return "null!?!";
	  if (strcmp (buf, ent->val) == 0)
	    return ent->val;
	  lastent = ent;
	}
      if (lastent != NULL)
	{
	  lastent->next = get_hash_entry ();
	  lastent->next->val = copy_string (buf);
	  return lastent->next->val;
	}
    }
  /* should never happen */
  return "?!hash!?";
}

char *
get_from_hash_table (char *buf, struct hash_table *table)
{
  int hash;
  struct hash_entry *ent, *lastent;

  if (buf == NULL)
    buf = "";
  hash = hash_string (buf);
  if (table->entries[hash] == NULL)
    {
      return NULL;
    }
  else
    {
      for (ent = table->entries[hash]; ent != NULL; ent = ent->next)
	{
	  if (ent->val == NULL)
	    return "null!?!";
	  if (strcmp (buf, ent->val) == 0)
	    return ent->val;
	  lastent = ent;
	}
      if (lastent != NULL)
	{
	  return NULL;
	}
    }
  /* should never happen */
  return "?!hash!?";
}

/* Random number handling is important but terrible/nonexistent in
   some systems.  Do it ourselves.  Also, this will give repeatable
   results across multiple platforms, which is important if this is
   being used to generate test cases. */

/* The random state *must* be at least 32 bits.  */

unsigned long initial_randstate = 0;

unsigned long randstate = 0;

/* Seed can come from elsewhere, for repeatability.  Otherwise, it comes
   from the current time, scaled down to where 32-bit arithmetic won't
   overflow.  */

void
init_xrandom (int seed)
{
  time_t tm;
    	
  if (seed > 0)
    {
      /* If the random state is already set, changes are somewhat
	 suspicious.  */
      if (randstate > 0)
	{
	  fprintf (stderr, "Randstate being changed from %lu to %d\n",
		   randstate, seed);
	}
      randstate = seed;
    }
  else
    {
      time (&tm);
      randstate = tm;
    }
  /* Whatever its source, put the randstate into known range (0 - 199999).  */
  randstate = abs (randstate);
  randstate %= 200000;
  /* This is kept around for the sake of error reporting. */
  initial_randstate = randstate;
}

/* Numbers lifted from Numerical Recipes, p. 198.  */
/* Arithmetic must be 32-bit.  */

int
xrandom (int m)
{
  int rslt;

  randstate = (9301 * randstate + 49297) % 233280;
  rslt = (m * randstate) / 233280;
#if 0 /* enable to study random number distribution */
  printf ("# %lu -> %d\n", randstate, rslt);
#endif
  return rslt;
}

/* Percentage probability, with bounds checking. */

int
probability(int prob)
{
  if (prob <= 0)
    return 0;
  if (prob >= 100)
    return 1;
  return (xrandom (100) < prob);
}

char *
xmalloc (int amt)
{
  char *value = (char *) malloc (amt);

  if (value == NULL)
    {
      /* This is pretty serious, have to get out quickly.  */
      fprintf (stderr, "Memory exhausted!!\n");
      exit (1);
    }
  /* Save callers from having to clear things themselves.  */
  memset (value, 0, amt);
  return value;
}

/* Copy a string to newly-allocated space.  The new space is never
   freed.  */

char *
copy_string (char *str)
{
  int len = strlen (str);
  char *rslt;
  
  rslt = xmalloc (len + 1);
  strcpy (rslt, str);
  return rslt;
}