summaryrefslogtreecommitdiffstats
path: root/src/vnet/format_fns.h
blob: b88cb53f233453a0273069f728ca6e92a9f2e927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef included_format_fns_h
#define included_format_fns_h

#include <vnet/ip/format.h>
#include <vnet/ip/ip_format_fns.h>
#include <vnet/ethernet/ethernet_format_fns.h>

#endif
> 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
/* 
 *------------------------------------------------------------------
 * node.c - the api generator's semantic back-end
 *
 * Copyright (c) 2004-2009 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <vppinfra/vec.h>
#include <vppinfra/hash.h>

#include "lex.h"
#include "node.h"

#define YYSTYPE void *

FILE *ofp;
FILE *javafp;
FILE *jnifp;
char *java_class;
time_t starttime;
char *vlib_app_name;
char *input_filename;
node_vft_t *the_vft[NODE_N_TYPES];
static int indent;
static int dont_output_version;
int dump_tree;
static char *fixed_name;
static char tmpbuf [MAXNAME];
static char *current_def_name;
static char *current_union_name;
static char *current_type_fmt;
static char *current_type_cast;
static char current_id;
static char current_is_complex;
static char *current_endianfun;
static char *current_type_name;
static int current_java_parameter_number;
static int current_java_emitted_parameter;
static int current_java_parameter_need_comma_space;
void *current_java_methodfun;
void *current_java_jnifun;

void indent_me(FILE *ofp)
{
    int i;

    for (i = 0; i < indent; i++)
        putc(' ', ofp);
}

char *uppercase (char *s)
{
    char *cp;

    cp = tmpbuf;

    while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
        if (*s >= 'a' && *s <= 'z')
            *cp++ = *s++ - ('a' - 'A');
        else
            *cp++ = *s++;
    }
    *cp = 0;
    return(tmpbuf);
}

char *lowercase (char *s)
{
    char *cp;

    cp = tmpbuf;

    while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
        if (*s >= 'A' && *s <= 'Z')
            *cp++ = *s++ + ('a' - 'A');
        else
            *cp++ = *s++;
    }
    *cp = 0;
    return(tmpbuf);
}

/* 
 * javah maps foo_bar to foo_1bar for whatever freakin' reason
 * So, adjust java names accordingly.
 */
char *java_name_mangle (void * name_arg)
{
    char * name = name_arg;
    static u8 * s;
    int i;

    vec_reset_length (s);

    s = format (s, "%s%c", name, 0);

    for (i = 0; i < vec_len(s); i++)
        if (s[i] == '_') {
            vec_delete (s, 1, i);
            if (s[i] >= 'a' && s[i] <= 'z')
                s[i] -= ('a' - 'A');
        }
    vec_add1 (s, 0);
    
    return ((char *) s);
}

void primtype_recursive_print(node_t *this, i8 *fmt)
{
    fputs((char *)fmt, stdout);

    if (this->deeper) {
        node_vft_t *vftp = the_vft[this->deeper->type];
        vftp->print(this->deeper);
    }
}

void primtype_recursive_generate(node_t *this, enum passid which, FILE *ofp,
                                 i8 *type_name, i8 *type_fmt, i8 *type_cast)
{
    node_vft_t *vftp;

    current_type_name = (char *)type_name;
    current_type_cast = (char *)type_cast;

    switch(which) {
    case TYPEDEF_PASS:
        fputs((char *)type_name, ofp);
        fputs(" ", ofp);
        break;

    case PRINTFUN_PASS:
        current_type_fmt = (char *)type_fmt;
        break;

    case ENDIANFUN_PASS:
        vftp = the_vft[this->type];
        current_endianfun = vftp->endian_converter;
        break;

    case JAVA_METHOD_PASS:
        vftp = the_vft[this->type];
        current_java_methodfun = vftp->java_method_function;
        break;

    default:
        fprintf(stderr, "primtype_recursive_generate: unimp pass %d\n", which);
        break;
    }

    if (this->deeper) {
        vftp = the_vft[this->deeper->type];
        vftp->generate(this->deeper, which, ofp);
    }
}

void primtype_java_method (node_t * this, enum passid which, FILE *ofp, 
                           char *java_type_name)
{
    node_t * deeper;

    deeper = this->deeper;

    /* We'll take care of _msg_id, client_index, and context ourselves */
    if (current_java_parameter_number++ < 3) {
        if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
            return;
    }

    if (deeper->type == NODE_SCALAR)
        fprintf (ofp, "%s %s", java_type_name, 
                 java_name_mangle(deeper->data[0]));
    else
        fprintf (ofp, "%s [] %s", java_type_name, 
                 java_name_mangle(deeper->data[0]));

    current_java_emitted_parameter = 1;
}

void primtype_java_parameter (node_t * this, enum passid which, FILE *ofp, 
                              char *java_type_name)
{
    node_t * deeper;

    deeper = this->deeper;

    /* We'll take care of _msg_id, client_index, and context ourselves */
    if (current_java_parameter_number++ < 3) {
        if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
            return;
    }
    if (current_java_parameter_need_comma_space) {
        current_java_parameter_need_comma_space = 0;
        fputs (", ", ofp);
    }

    if (deeper->type == NODE_SCALAR)
        fprintf (ofp, "%s %s", java_type_name, (char *)(deeper->data[0]));
    else
        fprintf (ofp, "%sArray %s", java_type_name, (char *)(deeper->data[0]));

    current_java_emitted_parameter = 1;
}

void primtype_java_setup (node_t * this, enum passid which, FILE *ofp, 
                          char *java_type_name, char *array_element_name)
{
    node_t * deeper;

    deeper = this->deeper;

    /* We'll take care of _msg_id, client_index, and context ourselves */
    if (current_java_parameter_number++ < 3) {
        if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
            return;
    }

    if (deeper->type == NODE_VECTOR) {
        indent_me(ofp);
        fprintf (ofp, 
                 "%s * %sP = (*env)->Get%sArrayElements (env, %s, NULL);\n",
                 java_type_name, (char *)(deeper->data[0]),
                 array_element_name, (char *)(deeper->data[0]));
    }
                 
    current_java_emitted_parameter = 1;
}

void primtype_java_code (node_t * this, enum passid which, FILE *ofp, 
                         char *java_type_name, char * swapper)
{
    node_t * deeper;
    char * s;

    deeper = this->deeper;

    /* We'll take care of _msg_id, client_index, and context ourselves */
    if (current_java_parameter_number++ < 3) {
        if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
            return;
    }

    indent_me(ofp);

    s = (char *)(deeper->data[0]);

    if (swapper == 0) {
        if (deeper->type == NODE_VECTOR)
            fprintf (ofp, "memcpy (mp->%s, %sP, sizeof (mp->%s));\n",
                     s, s, s);
        else
            fprintf (ofp, "mp->%s = %s;\n", s, s);
    } else {
        if (deeper->type == NODE_VECTOR) {
            fprintf(ofp, "{\n");
            indent += 4;
            indent_me(ofp);
            fprintf(ofp, "int _i;\n");
            indent_me(ofp);
            fprintf(ofp, "for (_i = 0; _i < %d; _i++) {\n", 
                    (int)(u64)(deeper->data[1]));
            indent += 4;
            indent_me(ofp);
            fprintf(ofp, "mp->%s[_i] = %s(%sP[_i]);\n",
                    s, swapper, s);
            indent -= 4;
            indent_me(ofp);
            fprintf(ofp, "}\n");
            indent -= 4;
            indent_me(ofp);
            fprintf(ofp, "}\n");
        } else {
            fprintf (ofp, "mp->%s = %s(%s);\n", s, swapper, s);
        }
    }

    current_java_emitted_parameter = 1;
}

void primtype_java_teardown (node_t * this, enum passid which, FILE *ofp, 
                             char * array_element_name)
{
    node_t * deeper;

    deeper = this->deeper;

    /* We'll take care of _msg_id, client_index, and context ourselves */
    if (current_java_parameter_number++ < 3) {
        if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
            return;
        else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
            return;
    }

    if (deeper->type == NODE_VECTOR) {
        indent_me(ofp);
        fprintf (ofp, 
                 "(*env)->Release%sArrayElements (env, %s, %sP, 0);\n",
                 array_element_name, 
                 (char *)(deeper->data[0]), 
                 (char *)(deeper->data[0]));
    }

    current_java_emitted_parameter = 1;
}

void node_illegal_print (node_t *this)
{
    fprintf(stderr, "node_illegal_print called\n");
    exit(0);
}

void node_illegal_generate (node_t *this, enum passid notused, FILE *ofp)
{
    fprintf(stderr, "node_illegal_generate called\n");
    exit(0);
}

void node_illegal_java_method (node_t *this, enum passid notused, FILE *ofp)
{
    fprintf(stderr, "node_illegal_java_method called\n");
    exit(0);
}

void node_illegal_java_jni (node_t *this, enum passid notused, FILE *ofp)
{
    fprintf(stderr, "node_illegal_java_jni called\n");
    exit(0);
}

node_vft_t node_illegal_vft = {
    node_illegal_print,
    node_illegal_generate,
    "illegal",
    node_illegal_java_method,
    node_illegal_java_jni,
};

void node_u8_print (node_t *this)
{
    primtype_recursive_print(this, "u8 ");
}

void node_u8_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "u8", "%u", "(unsigned)");
}

void node_u8_java_method (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_method (this, which, ofp, "byte");
}

void node_u8_java_parameter (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_parameter (this, which, ofp, "jbyte");
}

void node_u8_java_setup (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_setup (this, which, ofp, "jbyte", "Byte");
}

void node_u8_java_code (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_code (this, which, ofp, "jbyte", 0 /* swapper */);
}

void node_u8_java_teardown (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_teardown (this, which, ofp, "Byte");
}

node_vft_t node_u8_vft = {
    node_u8_print,
    node_u8_generate,
    "", 
    node_u8_java_method,
    node_u8_java_parameter,
    node_u8_java_setup,
    node_u8_java_code,
    node_u8_java_teardown,
};

void node_u16_print (node_t *this)
{
    primtype_recursive_print(this, "u16 ");
}

void node_u16_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "u16", "%u", "(unsigned)");
}

void node_u16_java_method (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_method (this, which, ofp, "short");
}

void node_u16_java_parameter (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_parameter (this, which, ofp, "jshort");
}

void node_u16_java_setup (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_setup (this, which, ofp, "jshort", "Short");
}

void node_u16_java_code (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_code (this, which, ofp, "jshort", "clib_host_to_net_u16");
}

void node_u16_java_teardown (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_teardown (this, which, ofp, "Short");
}

node_vft_t node_u16_vft = {
    node_u16_print,
    node_u16_generate,
    "clib_net_to_host_u16",
    node_u16_java_method,
    node_u16_java_parameter,
    node_u16_java_setup,
    node_u16_java_code,
    node_u16_java_teardown,
};

void node_u32_print (node_t *this)
{
    primtype_recursive_print(this, "u32 ");
}

void node_u32_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "u32", "%u", "(unsigned)");
}

void node_u32_java_method (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_method (this, which, ofp, "int");
}

void node_u32_java_parameter (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_parameter (this, which, ofp, "jint");
}

void node_u32_java_setup (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_setup (this, which, ofp, "jint", "Int");
}

void node_u32_java_code (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_code (this, which, ofp, "jint", "clib_host_to_net_u32");
}

void node_u32_java_teardown (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_teardown (this, which, ofp, "Int");
}

node_vft_t node_u32_vft = {
    node_u32_print,
    node_u32_generate,
    "clib_net_to_host_u32",
    node_u32_java_method,
    node_u32_java_parameter,
    node_u32_java_setup,
    node_u32_java_code,
    node_u32_java_teardown,
};

void node_u64_print (node_t *this)
{
    primtype_recursive_print(this, "u64 ");
}

void node_u64_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "u64", "%llu", 
                                "(long long)");
}

void node_u64_java_method (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_method (this, which, ofp, "long");
}

void node_u64_java_parameter (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_parameter (this, which, ofp, "jlong");
}

void node_u64_java_setup (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_setup (this, which, ofp, "jlong", "Long");
}

void node_u64_java_code (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_code (this, which, ofp, "jlong", "clib_host_to_net_u64");
}

void node_u64_java_teardown (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_teardown (this, which, ofp, "Long");
}

node_vft_t node_u64_vft = {
    node_u64_print,
    node_u64_generate,
    "clib_net_to_host_u64",
    node_u64_java_method,
    node_u64_java_parameter,
    node_u64_java_setup,
    node_u64_java_code,
    node_u64_java_teardown,
};

void node_i8_print (node_t *this)
{
    primtype_recursive_print(this, "i8 ");
}

void node_i8_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "i8", "%d", "(int)");
}

node_vft_t node_i8_vft = {
    node_i8_print,
    node_i8_generate,
    "",
    node_u8_java_method,
    node_u8_java_parameter,
    node_u8_java_setup,
    node_u8_java_code,
    node_u8_java_teardown,
};

void node_i16_print (node_t *this)
{
    primtype_recursive_print(this, "i16 ");
}

void node_i16_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "i16", "%d", "(int)");
}

node_vft_t node_i16_vft = {
    node_i16_print,
    node_i16_generate,
    "clib_net_to_host_u16",
    node_u16_java_method,
    node_u16_java_parameter,
    node_u16_java_setup,
    node_u16_java_code,
    node_u16_java_teardown,
};

void node_i32_print (node_t *this)
{
    primtype_recursive_print(this, "i32 ");
}

void node_i32_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "i32", "%ld", "(long)");
}

node_vft_t node_i32_vft = {
    node_i32_print,
    node_i32_generate,
    "clib_net_to_host_u32",
    node_u32_java_method,
    node_u32_java_parameter,
    node_u32_java_setup,
    node_u32_java_code,
    node_u32_java_teardown,
};

void node_i64_print (node_t *this)
{
    primtype_recursive_print(this, "i64 ");
}

void node_i64_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "i64", "%lld", 
                                "(long long)");
}

node_vft_t node_i64_vft = {
    node_i64_print,
    node_i64_generate,
    "clib_net_to_host_u64",
    node_u64_java_method,
    node_u64_java_parameter,
    node_u64_java_setup,
    node_u64_java_code,
    node_u64_java_teardown,
};

void node_f64_print (node_t *this)
{
    primtype_recursive_print(this, "f64 ");
}

void node_f64_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "f64", "%.2f", 
                                "(double)");
}
void node_f64_java_method (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_method (this, which, ofp, "double");
}

void node_f64_java_parameter (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_parameter (this, which, ofp, "jdouble");
}

void node_f64_java_setup (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_setup (this, which, ofp, "jdouble", "Double");
}

void node_f64_java_code (node_t *this, enum passid which, FILE *ofp)
{
    /* 
     * Current API code doesn't try to endian-swap doubles
     * FP formats aren't portable yadda yadda yadda
     */
    primtype_java_code (this, which, ofp, "jdouble", 0 /* $$$ */);
}

void node_f64_java_teardown (node_t *this, enum passid which, FILE *ofp)
{
    primtype_java_teardown (this, which, ofp, "Double");
}

node_vft_t node_f64_vft = {
    node_f64_print,
    node_f64_generate,
    " ",                        /* FP numbers are sent in host byte order */
    node_f64_java_method,
    node_f64_java_parameter,
    node_f64_java_setup,
    node_f64_java_code,
    node_f64_java_teardown,
};


void node_packed_print (node_t *this)
{
    primtype_recursive_print (this, "packed ");
}

void node_packed_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "PACKED", "", "");
}

node_vft_t node_packed_vft = {
    node_packed_print,
    node_packed_generate,
    0,
};

void node_define_print (node_t *this)
{
    fprintf(stdout, "define %s {\n", CDATA0);
    if (this->deeper) {
        node_vft_t *vftp = the_vft[this->deeper->type];
        fprintf(stdout, "    ");
        vftp->print(this->deeper);
    }
    fprintf(stdout, "};\n");
}

void node_define_generate (node_t *this, enum passid which, FILE *fp)
{
    node_t *child, *save_child;

    switch(which) {
    case TYPEDEF_PASS:
        fprintf(fp, "typedef VL_API_PACKED(struct _vl_api_%s {\n", CDATA0);
        child = this->deeper;
        indent += 4;
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            indent_me(fp);
            vftp->generate(child, which, fp);
            child = child->peer;
        }
        indent -= 4;
        fprintf(fp, "}) vl_api_%s_t;\n\n", CDATA0);
        break;

    case ENDIANFUN_PASS:
    case PRINTFUN_PASS:
        child = this->deeper;
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            vftp->generate(child, which, fp);
            child = child->peer;
        }
        break;

    case JAVA_METHOD_PASS:
        indent += 4;
        indent_me(fp);
        fprintf (fp, "public native int %s (", 
                 java_name_mangle(CDATA0));
        child = this->deeper;
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            current_java_emitted_parameter = 0;
            vftp->java_method_function(child, which, fp);
            child = child->peer;
            if (child && current_java_emitted_parameter)
                fputs (", ", fp);
        }
        fprintf (fp, ");\n");
        indent -= 4;
        break;

    case JAVA_JNI_PASS:
        /* Generate function prototype */
        fprintf (fp, "JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_%s_%s\n", 
                 java_class, java_name_mangle(CDATA0));

        fprintf (fp, "(JNIEnv * env, jobject obj");
        current_java_parameter_need_comma_space = 1;
        child = this->deeper;
        save_child = child;
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            current_java_emitted_parameter = 0;
            vftp->java_jni_parameter(child, which, fp);
            child = child->peer;
            if (child && current_java_emitted_parameter)
                fputs (", ", fp);
        }
        fprintf (fp, ")\n{\n");
        indent += 4;

        /* define the api message pointer */
        indent_me(fp);
        fprintf (fp, "vppjni_main_t *jm = &vppjni_main;\n");
        indent_me(fp);
        fprintf (fp, "vl_api_%s_t * mp;\n", current_def_name);
        indent_me(fp);
        fprintf (fp, "u32 my_context_id;\n");
        indent_me(fp);
        fprintf (fp, "int rv;\n");

        indent_me(fp);
        fprintf (fp, "rv = vppjni_sanity_check (jm);\n");
        indent_me(fp);
        fprintf (fp, "if (rv) return rv;\n");

        indent_me(fp);
        fprintf (fp, "my_context_id = vppjni_get_context_id (jm);\n");

        /* Generate array setups, if any */
        child = save_child;
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            current_java_parameter_number = 0;
            current_java_emitted_parameter = 0;
            vftp->java_jni_setup(child, which, fp);
            child = child->peer;
        }

        /* Setup the API message */
        indent_me(fp);
        fprintf (fp, "M(%s, %s);\n", uppercase(current_def_name),
                 current_def_name);
        indent_me(fp);
        fprintf (fp, "mp->context = clib_host_to_net_u32 (my_context_id);\n");
        /* $$$ Set up context hash table or some such... */

        /* Generate code */
        child = save_child;
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            current_java_parameter_number = 0;
            current_java_emitted_parameter = 0;
            vftp->java_jni_code(child, which, fp);
            child = child->peer;
        }

        /* Generate array teardowns */
        child = save_child;
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            current_java_parameter_number = 0;
            current_java_emitted_parameter = 0;
            vftp->java_jni_teardown(child, which, fp);
            child = child->peer;
        }

        /* Send the message, return context_id */
        indent_me (fp);
        fprintf (fp, "S;\n");
        indent_me (fp);
        fprintf (fp, "return my_context_id;\n");
        
        indent -= 4;
        fprintf (fp, "}\n\n");
        break;

    default:
        fprintf(stderr, "node_define_generate: unimp pass %d\n", which);
        break;
    }
}

node_vft_t node_define_vft = {
    node_define_print,
    node_define_generate,
    0,
};

void node_union_print (node_t *this)
{
    primtype_recursive_print (this, "union ");
}

void node_union_generate (node_t *this, enum passid which, FILE *fp)
{
    node_t *child;
    node_t *uelem;
    int case_id=1;

    switch(which) {
    case TYPEDEF_PASS:
        fprintf(fp, "u8 _%s_which;\n", CDATA0);
        indent_me(fp);
        fprintf(fp, "union _%s {\n", CDATA0);
        child = this->deeper;
        indent += 4;
    
        while (child) {
            node_vft_t *vftp = the_vft[child->type];
            indent_me(fp);
            vftp->generate(child, which, fp);
            child = child->peer;
        }
        indent -= 4;
        indent_me(fp);
        fprintf(fp, "} %s;\n", CDATA0);
        break;

    case PRINTFUN_PASS:
    case ENDIANFUN_PASS:
        uelem = this->deeper;
        
        indent_me(fp);
        fprintf(fp, "switch(a->_%s_which) {\n",
                CDATA0);
        indent += 4;
        current_union_name = CDATA0;

        /* Walk the list of objects in this union */
        while (uelem) {
            node_vft_t *vftp = the_vft[uelem->type];
            indent -= 4;
            indent_me(fp);
            fprintf(fp, "case %d:\n", case_id);
            case_id++;
            indent += 4;
            /* Drill down on each element */
            vftp->generate(uelem, which, fp);
            indent_me(fp);
            fprintf(fp, "break;\n");
            uelem = uelem->peer;
        }
        current_union_name = 0;
        indent -= 4;
        indent_me(fp);
        fprintf(fp, "default:\n");
        indent += 4;
        indent_me(fp);                 
        if (which == PRINTFUN_PASS) {
            fprintf(fp, 
                    "vl_print(handle, \"WARNING: _%s_which not set.\\n\");\n",
                    CDATA0);
        }
        indent_me(fp);
        fprintf(fp, "break;\n");
        indent -= 4;
        indent_me(fp);
        fprintf(fp, "}\n");
        break;

    default:
        fprintf(stderr, "node_union_generate: unimp pass %d\n", which);
        break;
    }
}


node_vft_t node_union_vft = {
    node_union_print,
    node_union_generate,
    0,
};

void node_scalar_print (node_t *this)
{
    fprintf(stdout, "%s", CDATA0);
    primtype_recursive_print (this, "");
}

void node_scalar_generate (node_t *this, enum passid which, FILE *fp)
{
    char *union_prefix = "";

    if (current_union_name) {
        sprintf(tmpbuf, "%s.", current_union_name);
        union_prefix = tmpbuf;
    }

    switch(which) {
    case TYPEDEF_PASS:
        fprintf(fp, "%s;\n", CDATA0);
        break;

    case PRINTFUN_PASS:
        indent_me(fp);
        if (current_is_complex) {
            fprintf(fp, "vl_api_%s_t_print(a->%s%s, handle);\n", 
                    current_type_name, union_prefix, CDATA0);
        } else {
            if (!strcmp(current_type_fmt, "uword")) {
                fprintf(fp, 
           "vl_print(handle, \"%s%s: \" _uword_fmt \"\\n\", %s a->%s%s);\n", 
                        union_prefix, CDATA0, "(_uword_cast)",
                        union_prefix, CDATA0);
            } else {
                fprintf(fp, 
                        "vl_print(handle, \"%s%s: %s\\n\", %s a->%s%s);\n", 
                        union_prefix, CDATA0, 
                        current_type_fmt, current_type_cast,
                        union_prefix, CDATA0);
            }
        }
        break;

    case ENDIANFUN_PASS:
        indent_me(fp);
        if (current_is_complex) {
            fprintf(fp, "vl_api%s_t_endian(a->%s%s);\n", 
                    current_type_name, union_prefix, CDATA0);
        } else {
            /* Current_endianfun == NULL means e.g. it's a u8... */
            if (current_endianfun) {
                fprintf(fp, "a->%s%s = %s(a->%s%s);\n", union_prefix,
                        CDATA0, current_endianfun, 
                        union_prefix, CDATA0);
            } else {
                fprintf(fp, "/* a->%s%s = a->%s%s */\n",
                        union_prefix, CDATA0, 
                        union_prefix, CDATA0);
            }
        }
        break;

    default:
        fprintf(stderr, "node_scalar_generate: unimp pass %d\n", which);
    }
    if (this->deeper) {
        fprintf(stderr, "broken recursion in node_scalar_generate\n");
    }
}


node_vft_t node_scalar_vft = {
    node_scalar_print,
    node_scalar_generate,
    0,
};

void node_vector_print (node_t *this)
{
    primtype_recursive_print (this, "vector ");
}

void node_vector_generate (node_t *this, enum passid which, FILE *fp)
{
    char *union_prefix = "";

    if (current_union_name) {
        sprintf(tmpbuf, "%s.", current_union_name);
        union_prefix = tmpbuf;
    }

    switch(which) {
    case TYPEDEF_PASS:
        fprintf(fp, "%s[%d];\n", CDATA0, IDATA1);
        break;

    case PRINTFUN_PASS:
        /* Don't bother about "u8 data [0];" et al. */
        if (IDATA1 == 0)
            break;

        indent_me(fp);
        fprintf(fp, "{\n");
        indent += 4;
        indent_me(fp);
        fprintf(fp, "int _i;\n");
        indent_me(fp);
        fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n", 
                IDATA1);
        indent += 4;
        indent_me(fp);
        if (current_is_complex) {
            fprintf(fp, "vl_print(handle, \"%s%s[%%d]: ",
                    union_prefix, CDATA0);
            fprintf(fp, 
                    "vl_print_%s (handle, a->%s%s[_i]);\n", 
                    CDATA0, union_prefix, CDATA0);
        } else {
            fprintf(fp, 
         "vl_print(handle, \"%s%s[%%d]: %s\\n\", _i, a->%s%s[_i]);\n",
                    union_prefix, CDATA0, 
                    current_type_fmt, 
                    union_prefix, CDATA0);
        }
        indent -= 4;
        indent_me(fp);
        fprintf(fp, "}\n");
        indent -= 4;
        indent_me(fp);
        fprintf(fp, "}\n");
        break;

    case ENDIANFUN_PASS:
        /* Don't bother about "u8 data [0];" et al. */
        if (IDATA1 == 0)
            break;

        indent_me(fp);
        fprintf(fp, "{\n");
        indent += 4;
        indent_me(fp);
        fprintf(fp, "int _i;\n");
        indent_me(fp);
        fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n", 
                IDATA1);
        indent += 4;
        indent_me(fp);
        if (current_is_complex) {
            fprintf(fp, 
                    "vl_api_%s_t_endian (a->%s%s[_i]);\n", 
                    current_type_name, union_prefix, CDATA0);
        } else {
            fprintf(fp, 
                    "a->%s%s[_i] = %s(a->%s%s[_i]);\n", 
                    union_prefix, CDATA0, 
                    current_endianfun, 
                    union_prefix, CDATA0);
        }
        indent -= 4;
        indent_me(fp);
        fprintf(fp, "}\n");
        indent -= 4;
        indent_me(fp);
        fprintf(fp, "}\n");
        break;

    default:
        fprintf(stderr, "node_vector_generate: unimp pass %d\n", which);
    }
    if (this->deeper) {
        fprintf(stderr, "broken recursion in node_vector_generate\n");
    }
}

node_vft_t node_vector_vft = {
    node_vector_print,
    node_vector_generate,
    0,
};

void node_complex_print (node_t *this)
{
    primtype_recursive_print (this, "complex ");
}

void node_complex_generate (node_t *this, enum passid which, FILE *fp)
{
    node_t *deeper;
    node_vft_t *vftp;
    char *member_name = "broken!";
    char *union_prefix = "";

    if (current_union_name) {
        sprintf(tmpbuf, "%s.", current_union_name);
        union_prefix = tmpbuf;
    }

    current_is_complex++;
    
    switch(which) {
    case TYPEDEF_PASS:
        fprintf(fp, "%s ", CDATA0);
        deeper = this->deeper;
        if (deeper) {
            vftp = the_vft[deeper->type];
            vftp->generate(deeper, which, fp);
        }
        break;

    case PRINTFUN_PASS:
        deeper = this->deeper;
        while (deeper) {
            if (deeper->type == NODE_SCALAR ||
                deeper->type == NODE_VECTOR) {
                member_name = deeper->data[0];
                break;
            }
            deeper = deeper->deeper;
        }
        indent_me(fp);
        fprintf(fp, "vl_print(handle, \"%s%s ----- \\n\");\n", 
                union_prefix, member_name);
        indent_me(fp);
        fprintf(fp, "%s_print(&a->%s%s, handle);\n", 
                CDATA0, union_prefix, member_name);
        indent_me(fp);
        fprintf(fp, "vl_print(handle, \"%s%s ----- END \\n\");\n", 
                union_prefix, member_name);
        break;

    case ENDIANFUN_PASS:
        deeper = this->deeper;
        while (deeper) {
            if (deeper->type == NODE_SCALAR ||
                deeper->type == NODE_VECTOR) {
                member_name = deeper->data[0];
                break;
            }
            deeper = deeper->deeper;
        }

        indent_me(fp);
        fprintf(fp, "%s_endian(&a->%s%s);\n", 
                CDATA0, union_prefix, member_name);
        break;

    default:
        fprintf(stderr, "node_complex_generate unimp pass %d...\n", which);
        break;
    }
    current_is_complex--;
}

node_vft_t node_complex_vft = {
    node_complex_print,
    node_complex_generate,
    0,
};

void node_noversion_print (node_t *this)
{
    primtype_recursive_print (this, "noversion ");
}

void node_noversion_generate (node_t *this, enum passid which, FILE *ofp)
{
    fprintf(stderr, "node_noversion_generate called...\n");
}

node_vft_t node_noversion_vft = {
    node_noversion_print,
    node_noversion_generate,
    0,
};

void node_uword_print (node_t *this)
{
    primtype_recursive_print(this, "uword ");
}

void node_uword_generate (node_t *this, enum passid which, FILE *ofp)
{
    primtype_recursive_generate(this, which, ofp, "uword", "uword", "");
}

node_vft_t node_uword_vft = {
    node_uword_print,
    node_uword_generate,
    "clib_net_to_host_uword",
};

node_vft_t *the_vft[NODE_N_TYPES] = {
    &node_illegal_vft,
    &node_u8_vft,
    &node_u16_vft,
    &node_u32_vft,
    &node_u64_vft,
    &node_i8_vft,
    &node_i16_vft,
    &node_i32_vft,
    &node_i64_vft,
    &node_f64_vft,
    &node_packed_vft,
    &node_define_vft,
    &node_union_vft,
    &node_scalar_vft,
    &node_vector_vft,
    &node_complex_vft,
    &node_noversion_vft,
    &node_uword_vft,
};

void *make_node (enum node_subclass type)
{
    node_t *rv;

    rv = (node_t *) malloc (sizeof (*rv));
    if (rv == 0) {
        fprintf (stderr, "fatal: make_node out of memory\n");
        exit (1);
    }
    bzero (rv, sizeof (*rv));
    rv->type = type;
    return ((void *) rv);
}

YYSTYPE deeper (YYSTYPE arg1, YYSTYPE arg2)
{
    node_t *np1 = (node_t *) arg1;
    node_t *np2 = (node_t *) arg2;
    node_t *hook_point;
    
    hook_point = np1;

    while (hook_point->deeper)
        hook_point = hook_point->deeper;

    hook_point->deeper = np2;
    return (arg1);
}

YYSTYPE addpeer (YYSTYPE arg1, YYSTYPE arg2)
{
    node_t *np1 = (node_t *) arg1;
    node_t *np2 = (node_t *) arg2;
    node_t *hook_point;
    
    hook_point = np1;

    while (hook_point->peer)
        hook_point = hook_point->peer;

    hook_point->peer = np2;
    return (arg1);
}

/*
 * add_slist (stmt_list, stmt)
 */

YYSTYPE add_slist (YYSTYPE a1, YYSTYPE a2)
{
    if (a1 && a2)
        return (addpeer(a1, a2));
    else if(a1)
        return(a1);
    else 
        return(a2);
}

/*
 * add_define (char *name, defn_list);
 */
YYSTYPE add_define (YYSTYPE a1, YYSTYPE a2)
{
    node_t *np;

    np = make_node(NODE_DEFINE);
    np->data[0] = a1;
    deeper((YYSTYPE)np, a2);
    return ((YYSTYPE) np);
}

/*
 * add_defbody (defn_list, new_defn)
 */
YYSTYPE add_defbody (YYSTYPE a1, YYSTYPE a2)
{
    return (addpeer(a1, a2));
}

/*
 * add_primtype ([packed], primitive type, instance)
 */ 

YYSTYPE add_primtype (YYSTYPE a1, YYSTYPE a2, YYSTYPE a3)
{
    node_t *np1;

    np1 = (node_t *)a1;
    
    /* Hook instance to type node */
    deeper (a1, a2);
    if (a3) {
        deeper(a1, a3);
    }
    return (a1);
}

/*
 * add_complex(char *type_name, instance)
 */

YYSTYPE add_complex (YYSTYPE a1, YYSTYPE a2)
{
    node_t *np;

    np = make_node(NODE_COMPLEX);
    np->data[0] = (void *) a1;

    deeper((YYSTYPE)np, a2);
    return ((YYSTYPE) np);
}

/*
 * add_union(char *type_name, definition)
 */

YYSTYPE add_union (YYSTYPE a1, YYSTYPE a2)
{
    node_t *np;

    np = make_node(NODE_UNION);
    np->data[0] = (void *) a1;

    deeper((YYSTYPE)np, a2);
    return ((YYSTYPE) np);
}


/*
 * add_vector_vbl (node_t *variable, YYSTYPE size)
 */

YYSTYPE add_vector_vbl (YYSTYPE a1, YYSTYPE a2)
{
    node_t *np;

    np = make_node(NODE_VECTOR);
    np->data[0] = (void *) a1;
    np->data[1] = (void *) a2;
    return ((YYSTYPE) np);
}

/*
 * add_scalar_vbl (char *name)
 */
YYSTYPE add_scalar_vbl (YYSTYPE a1)
{
    node_t *np;

    np = make_node(NODE_SCALAR);
    np->data[0] = (void *) a1;
    return ((YYSTYPE) np);
}

/*
 * set_flags (int flags, msg(=0?))
 */ 
YYSTYPE set_flags(YYSTYPE a1, YYSTYPE a2)
{
    node_t *np;
    int flags;

    np = (node_t *)a2;
    if (!np)
        return(0);

    flags = (int)(uword) a1;

    np->flags |= flags;
    return (a2);
}
/*
 * suppress_version
 */
YYSTYPE suppress_version (void)
{
    dont_output_version = 1;
    return (0);
}

void dump(node_t *np)
{
    node_vft_t *vftp;

    while (np) {
        vftp = the_vft[np->type];
        vftp->print(np);
        np = np->peer;
    }
}

char *fixup_input_filename(void)
{
    char *cp;

    cp = (char *)input_filename;

    while (*cp)
        cp++;

    cp--;

    while (cp > input_filename && *cp != '/')
        cp--;
    if (*cp == '/')
        cp++;

    strcpy (tmpbuf, cp);

    cp = tmpbuf;

    while (*cp)
        cp++;

    cp--;

    while (cp > tmpbuf && *cp != '.')
        cp--;
    
    if (*cp == '.')
        *cp = 0;

    return (sxerox(tmpbuf));
}

void generate_top_boilerplate(FILE *fp)

{
    char *datestring = ctime(&starttime);
    fixed_name = fixup_input_filename();

    datestring[24] = 0;

    fprintf (fp, "/*\n");
    fprintf (fp, " * VLIB API definitions %s\n", datestring);
    fprintf (fp, " * Input file: %s\n", input_filename);
    fprintf (fp, " * Automatically generated: please edit the input file ");
    fprintf (fp, "NOT this file!\n");

    /* Moron Acme trigger workaround */
    fprintf (fp, " * %syright (c) %s by Cisco Systems, Inc.\n", "Cop", 
             &datestring[20]);
    fprintf (fp, " */\n\n");
    fprintf (fp, "#if defined(vl_msg_id)||defined(vl_union_id)||");
    fprintf (fp, "defined(vl_printfun) \\\n ||defined(vl_endianfun)||");
    fprintf (fp, " defined(vl_api_version)||defined(vl_typedefs) \\\n");
    fprintf (fp, " ||defined(vl_msg_name)\n");
    fprintf (fp, "/* ok, something was selected */\n");
    fprintf (fp, "#else\n");
    fprintf (fp, "#warning no content included from %s\n", input_filename);
    fprintf (fp, "#endif\n\n");
    fprintf (fp, "#define VL_API_PACKED(x) x __attribute__ ((packed))\n\n");
}

void generate_bottom_boilerplate(FILE *fp)

{
    fprintf (fp, "\n#ifdef vl_api_version\n");

    if (dont_output_version) {
        fprintf (fp, "/* WARNING: API FILE VERSION CHECK DISABLED */\n");
        input_crc = 0;
    }

    fprintf (fp, "vl_api_version(%s, 0x%08x)\n\n", 
             fixed_name, (unsigned int)input_crc);
    fprintf (fp, "#endif\n\n");
}

void generate_msg_ids(YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;

    fprintf (fp, "\n/****** Message ID / handler enum ******/\n\n");
    fprintf (fp, "#ifdef vl_msg_id\n");

    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & NODE_FLAG_TYPEONLY)) {
                fprintf (fp, "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n", 
                         uppercase(np->data[0]), (i8 *)np->data[0]);
            } else {
                fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
            }
        }
        np = np->peer;
    }
    fprintf (fp, "#endif\n");

}

void generate_msg_names(YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;

    fprintf (fp, "\n/****** Message names ******/\n\n");

    fprintf (fp, "#ifdef vl_msg_name\n");

    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & NODE_FLAG_TYPEONLY)) {
                fprintf (fp, "vl_msg_name(vl_api_%s_t, %d)\n",
                         (i8 *) np->data[0], 
                         (np->flags & NODE_FLAG_DONT_TRACE ? 0 : 1));
            } else {
                fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
            }
        }
        np = np->peer;
    }
    fprintf (fp, "#endif\n\n");
}

void generate_typedefs(YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;
    node_vft_t *vftp;

    fprintf(fp, "\n/****** Typedefs *****/\n\n");
    fprintf(fp, "#ifdef vl_typedefs\n\n");

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            /* Yeah, this is pedantic */
            vftp = the_vft[np->type];
            vftp->generate(np, TYPEDEF_PASS, fp);
        }
        np = np->peer;
    }
    fprintf(fp, "#endif /* vl_typedefs */\n\n");
}

void union_walk_one_defn(node_t *np, FILE *fp)
{
    node_t *vblp;
    node_t *uelem;

    /* Walk the list of typed objects in this msg def */
    while (np) {
        if (np->type == NODE_UNION) {
            current_union_name = np->data[0];
            uelem = np->deeper;

            /* Walk the list of objects in this union */
            while (uelem) {
                vblp = uelem->deeper;
                /* Drill down on each element, find the variable name */
                while(vblp) {
                    if (vblp->type == NODE_SCALAR ||
                        vblp->type == NODE_VECTOR ||
                        vblp->type == NODE_COMPLEX) {
                        fprintf(ofp, "#define %s_", 
                                uppercase(current_def_name));
                        fprintf(ofp, "%s_", uppercase(current_union_name));
                        fprintf(ofp, "%s %d\n",uppercase(vblp->data[0]),
                                current_id);
                        current_id++;
                        break;
                    }
                    vblp = vblp->deeper;
                }
                uelem = uelem->peer;
            }
            current_union_name = 0;
            current_id = 1;
        }
        np = np->peer;
    }
}

void generate_uniondefs(YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;

    fprintf(fp, "/****** Discriminated Union Definitions *****/\n\n");
    fprintf(fp, "#ifdef vl_union_id\n\n");

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            current_id = 1;
            current_def_name = np->data[0];
            union_walk_one_defn(np->deeper, fp);
        }
        np = np->peer;
    }
    fprintf(fp, "\n#endif /* vl_union_id */\n\n");
}

void generate_printfun(YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;
    node_vft_t *vftp;

    fprintf(fp, "/****** Print functions *****/\n\n");
    fprintf(fp, "#ifdef vl_printfun\n\n");

    fprintf(fp, "#ifdef LP64\n");
    fputs ("#define _uword_fmt \"%lld\"\n", fp);
    fputs ("#define _uword_cast (long long)\n", fp);
    fprintf(fp, "#else\n");
    fputs("#define _uword_fmt \"%ld\"\n", fp);
    fputs ("#define _uword_cast long\n", fp);
    fprintf(fp, "#endif\n\n");

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & NODE_FLAG_MANUAL_PRINT)) {
                fprintf(fp, 
       "static inline void *vl_api_%s_t_print (vl_api_%s_t *a,",
                        (i8 *)np->data[0], (i8 *) np->data[0]);
                fprintf(fp, "void *handle)\n{\n");
                /* output the message name */
                fprintf(fp, 
                    "    vl_print(handle, \"vl_api_%s_t:\\n\");\n",
                        (i8 *)np->data[0]);

                indent += 4;
                /* Yeah, this is pedantic */
                vftp = the_vft[np->type];
                vftp->generate(np, PRINTFUN_PASS, fp);
                fprintf(fp, "    return handle;\n");
                fprintf(fp, "}\n\n");
                indent -= 4;
            } else {
                fprintf(fp, "/***** manual: vl_api_%s_t_print  *****/\n\n",
                        (i8 *) np->data[0]);
            }
        }
        np = np->peer;
    }
    fprintf(fp, "#endif /* vl_printfun */\n\n");
}

void generate_endianfun(YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;
    node_vft_t *vftp;

    fprintf(fp, "\n/****** Endian swap functions *****/\n\n");
    fprintf(fp, "#ifdef vl_endianfun\n\n");
    fprintf(fp, "#undef clib_net_to_host_uword\n");
    fprintf(fp, "#ifdef LP64\n");
    fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u64\n");
    fprintf(fp, "#else\n");
    fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u32\n");
    fprintf(fp, "#endif\n\n");

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & NODE_FLAG_MANUAL_ENDIAN)) {
                fprintf(fp, 
               "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)\n{\n",
                        (i8 *) np->data[0], (i8 *) np->data[0]);
                indent += 4;
                /* Yeah, this is pedantic */
                vftp = the_vft[np->type];
                vftp->generate(np, ENDIANFUN_PASS, fp);
                fprintf(fp, "}\n\n");
                indent -= 4;
            } else {
                fprintf(fp, "/***** manual: vl_api_%s_t_endian  *****/\n\n",
                        (i8 *) np->data[0]);
            }
        }
        np = np->peer;
    }
    fprintf(fp, "#endif /* vl_endianfun */\n\n");
}

void add_msg_ids(YYSTYPE a1)
{
    node_t *np = (node_t *)a1;
    node_t *new_u16;
    node_t *new_vbl;

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & NODE_FLAG_TYPEONLY)) {
                /* add the parse tree for "u16 _vl_msg_id" */
                new_u16 = make_node(NODE_U16);
                new_u16->peer = np->deeper;
                np->deeper = new_u16;
                new_vbl = make_node(NODE_SCALAR);
                new_vbl->data[0] = sxerox("_vl_msg_id");
                new_u16->deeper = new_vbl;
            }
        }
        np = np->peer;
    }
}

void generate_java_top_boilerplate(FILE *fp)

{
    char *datestring = ctime(&starttime);
    fixed_name = fixup_input_filename();

    datestring[24] = 0;

    fprintf (fp, "/*\n");
    fprintf (fp, " * VLIB API java binding %s\n", datestring);
    fprintf (fp, " * Input file: %s\n", input_filename);
    fprintf (fp, " * Automatically generated: please edit the input file ");
    fprintf (fp, "NOT this file!\n");
    fprintf (fp, " */\n\n");

    fprintf (fp, "package org.openvpp.vppjapi;\n\n");
    fprintf (fp, "import org.openvpp.vppjapi.vppConn;\n\n");
    fprintf (fp, "public class %s extends vppConn {\n\n",
             java_class);
}

void generate_java_bottom_boilerplate(FILE *fp)
{
    fprintf (fp, "}\n");
}


void generate_java_class_definition (YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;
    node_vft_t *vftp;

    fprintf(fp, "/****** API methods *****/\n\n");

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
                /* Suppress messages named "xyz_reply" */
                char * cp = (char *) np->data[0];
                while (*cp)
                    cp++;
                cp -= 6;
                if (strncmp (cp, "_reply", 6)) {
                    current_java_parameter_number = 0;
                    vftp = the_vft[np->type];
                    vftp->generate(np, JAVA_METHOD_PASS, fp);
                }
            }
        }
        np = np->peer;
    }

    fprintf(fp, "\n/****** end of API methods *****/\n");
}

void generate_jni_reply_handler_list (YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;
    node_vft_t *vftp;

    fprintf (fp, "#define foreach_api_reply_handler \\\n");

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
                /* emit messages named "xyz_reply" */
                char * cp = (char *) np->data[0];
                while (*cp)
                    cp++;
                cp -= 6;
                if (!strncmp (cp, "_reply", 6)) {
                    fprintf (fp, "_(%s, %s) \\\n", 
                             uppercase(np->data[0]), (char *)(np->data[0]));
                }
            }
        }
        np = np->peer;
    }

    fprintf (fp, "\n\n");
}

char * m_macro_boilerplate =     
"#define M(T,t)                                      \\\n"
"do {                                                \\\n"
"    api_result_ready = 0;                           \\\n"
"    mp = vl_msg_api_alloc(sizeof(*mp));             \\\n"
"    memset (mp, 0, sizeof (*mp));                   \\\n"
"    mp->_vl_msg_id = ntohs (VL_API_##T);            \\\n"
"    mp->client_index = api_main.my_client_index;    \\\n"
"} while(0);\n\n"
"#define M2(T,t,n)                                   \\\n"
"do {                                                \\\n"
"    api_result_ready = 0;                           \\\n"
"    mp = vl_msg_api_alloc(sizeof(*mp)+(n));         \\\n"
"    memset (mp, 0, sizeof (*mp));                   \\\n"
"    mp->_vl_msg_id = ntohs (VL_API_##T);            \\\n"
"    mp->client_index = api_main.my_client_index;    \\\n"
"} while(0);\n\n";

char * s_macro_boilerplate = 
"#define S (vl_msg_api_send_shmem (api_main.shmem_hdr->vl_input_queue, \\\n"
"(u8 *)&mp));\n\n";

char * w_macro_boilerplate = 
"#define W                                               \\\n"
"do {                                                    \\\n"
"    timeout = clib_time_now (&clib_time) + 1.0;         \\\n"
"                                                        \\\n"
"    while (clib_time_now (&clib_time) < timeout) {      \\\n"
"        if (api_result_ready == 1) {                    \\\n"
"            return ((jint) api_result);                 \\\n"    
"        }                                               \\\n"
"    }                                                   \\\n"
"    return -99;                                         \\\n"   
"} while(0);\n\n";

void generate_jni_top_boilerplate(FILE *fp)

{
    char *datestring = ctime(&starttime);
    fixed_name = fixup_input_filename();

    datestring[24] = 0;

    fprintf (fp, "/*\n");
    fprintf (fp, " * VLIB Java native code %s\n", datestring);
    fprintf (fp, " * Input file: %s\n", input_filename);
    fprintf (fp, " * Automatically generated: please edit the input file ");
    fprintf (fp, "NOT this file!\n");
    fprintf (fp, " */\n\n");

    fprintf (fp, "#include <japi/vppjni.h>\n");

    fprintf (fp, 
             "#define vl_api_version(n,v) static u32 %s_api_version %s = v;\n",
	     vlib_app_name, "__attribute__((unused))");
    fprintf (fp, "#include <api/%s.api.h>\n", vlib_app_name);
    fprintf (fp, "#undef vl_api_version\n\n");

    fprintf (fp, "#include <japi/org_openvpp_vppjapi_vppConn.h>\n");
    fprintf (fp, "#include <japi/org_openvpp_vppjapi_%s.h>\n\n", java_class);

    fprintf (fp, "#include <api/%s_msg_enum.h>\n", vlib_app_name);
    fprintf (fp, "#define vl_typedefs /* define message structures */\n");
    fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
    fprintf (fp, "#undef vl_typedefs\n\n");

    fprintf (fp, "#define vl_endianfun \n");
    fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
    fprintf (fp, "#undef vl_endianfun\n\n");

    fprintf (fp, "#define vl_print(handle, ...)\n");
    fprintf (fp, "#define vl_printfun\n");
    fprintf (fp, "#include <api/%s_all_api_h.h>\n", vlib_app_name);
    fprintf (fp, "#undef vl_printfun\n\n");
}

void generate_jni_code (YYSTYPE a1, FILE *fp)
{
    node_t *np = (node_t *)a1;
    node_vft_t *vftp;

    /* Walk the top-level node-list */
    while (np) {
        if (np->type == NODE_DEFINE) {
            if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
                /* Suppress messages named "xyz_reply" */
                char * cp = (char *) np->data[0];
                while (*cp)
                    cp++;
                cp -= 6;
                if (strncmp (cp, "_reply", 6)) {
                    current_def_name = np->data[0];
                    current_java_parameter_number = 0;
                    vftp = the_vft[np->type];
                    vftp->generate(np, JAVA_JNI_PASS, fp);
                }
            }
        }
        np = np->peer;
    }
}

char *hookup_boilerplate = 
"void vl_msg_reply_handler_hookup (void)\n"
"{\n"
"#define _(N,n)	\\\n"
"    vl_msg_api_set_handlers (VL_API_##N, #n, \\\n"
"        vl_api_generic_reply_handler, \\\n"
"        vl_noop_handler, \\\n"
"        vl_api_##n##_t_endian, \\\n"
"        vl_api_##n##_t_print, \\\n"
"        sizeof(vl_api_##n##_t), 1); \n"
"    foreach_api_reply_handler;\n"
"#undef _\n\n"
"}\n\n";
    
void generate_jni_bottom_boilerplate(FILE *fp)
{
    fputs (hookup_boilerplate, fp);
}

void generate(YYSTYPE a1)
{
    if (dump_tree) {
        dump((node_t *)a1);
    }

    add_msg_ids(a1);

    if (ofp) {
        generate_top_boilerplate(ofp);

        generate_msg_ids(a1, ofp);
        generate_msg_names(a1, ofp);
        generate_typedefs(a1, ofp);
        generate_uniondefs(a1, ofp);
        generate_printfun(a1, ofp);
        generate_endianfun(a1, ofp);
        
        generate_bottom_boilerplate(ofp);
    }

    if (javafp) {
        generate_java_top_boilerplate(javafp);
        generate_java_class_definition(a1, javafp);
        generate_java_bottom_boilerplate(javafp);
    }
    if (jnifp) {
        generate_jni_top_boilerplate(jnifp);
        generate_jni_reply_handler_list (a1, jnifp);
        generate_jni_code(a1, jnifp);
        generate_jni_bottom_boilerplate(jnifp);
    }
}