2 *------------------------------------------------------------------
3 * node.c - the api generator's semantic back-end
5 * Copyright (c) 2004-2009 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
27 #include <vppinfra/vec.h>
28 #include <vppinfra/hash.h>
33 #define YYSTYPE void *
41 node_vft_t *the_vft[NODE_N_TYPES];
43 static int dont_output_version;
45 static char *fixed_name;
46 static char tmpbuf [MAXNAME];
47 static char *current_def_name;
48 static char *current_union_name;
49 static char *current_type_fmt;
50 static char *current_type_cast;
51 static char current_id;
52 static char current_is_complex;
53 static char *current_endianfun;
54 static char *current_type_name;
56 void indent_me(FILE *ofp)
60 for (i = 0; i < indent; i++)
64 char *uppercase (char *s)
70 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
71 if (*s >= 'a' && *s <= 'z')
72 *cp++ = *s++ - ('a' - 'A');
80 char *lowercase (char *s)
86 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
87 if (*s >= 'A' && *s <= 'Z')
88 *cp++ = *s++ + ('a' - 'A');
96 void primtype_recursive_print(node_t *this, i8 *fmt)
98 fputs((char *)fmt, stdout);
101 node_vft_t *vftp = the_vft[this->deeper->type];
102 vftp->print(this->deeper);
106 void primtype_recursive_generate(node_t *this, enum passid which, FILE *ofp,
107 i8 *type_name, i8 *type_fmt, i8 *type_cast)
111 current_type_name = (char *)type_name;
112 current_type_cast = (char *)type_cast;
116 fputs((char *)type_name, ofp);
121 current_type_fmt = (char *)type_fmt;
125 vftp = the_vft[this->type];
126 current_endianfun = vftp->endian_converter;
130 fputs("('", pythonfp);
131 fputs((char *)type_name, pythonfp);
132 fputs("', ", pythonfp);
136 fputs("[\"", jsonfp);
137 fputs((char *)type_name, jsonfp);
138 fputs("\", ", jsonfp);
142 fprintf(stderr, "primtype_recursive_generate: unimp pass %d\n", which);
147 vftp = the_vft[this->deeper->type];
148 vftp->generate(this->deeper, which, ofp);
152 void node_illegal_print (node_t *this)
154 fprintf(stderr, "node_illegal_print called\n");
158 void node_illegal_generate (node_t *this, enum passid notused, FILE *ofp)
160 fprintf(stderr, "node_illegal_generate called\n");
164 node_vft_t node_illegal_vft = {
166 node_illegal_generate,
170 void node_u8_print (node_t *this)
172 primtype_recursive_print(this, "u8 ");
175 void node_u8_generate (node_t *this, enum passid which, FILE *ofp)
177 primtype_recursive_generate(this, which, ofp, "u8", "%u", "(unsigned)");
180 node_vft_t node_u8_vft = {
186 void node_u16_print (node_t *this)
188 primtype_recursive_print(this, "u16 ");
191 void node_u16_generate (node_t *this, enum passid which, FILE *ofp)
193 primtype_recursive_generate(this, which, ofp, "u16", "%u", "(unsigned)");
196 node_vft_t node_u16_vft = {
199 "clib_net_to_host_u16"
202 void node_u32_print (node_t *this)
204 primtype_recursive_print(this, "u32 ");
207 void node_u32_generate (node_t *this, enum passid which, FILE *ofp)
209 primtype_recursive_generate(this, which, ofp, "u32", "%u", "(unsigned)");
212 node_vft_t node_u32_vft = {
215 "clib_net_to_host_u32",
218 void node_u64_print (node_t *this)
220 primtype_recursive_print(this, "u64 ");
223 void node_u64_generate (node_t *this, enum passid which, FILE *ofp)
225 primtype_recursive_generate(this, which, ofp, "u64", "%llu",
229 node_vft_t node_u64_vft = {
232 "clib_net_to_host_u64"
235 void node_i8_print (node_t *this)
237 primtype_recursive_print(this, "i8 ");
240 void node_i8_generate (node_t *this, enum passid which, FILE *ofp)
242 primtype_recursive_generate(this, which, ofp, "i8", "%d", "(int)");
245 node_vft_t node_i8_vft = {
251 void node_i16_print (node_t *this)
253 primtype_recursive_print(this, "i16 ");
256 void node_i16_generate (node_t *this, enum passid which, FILE *ofp)
258 primtype_recursive_generate(this, which, ofp, "i16", "%d", "(int)");
261 node_vft_t node_i16_vft = {
264 "clib_net_to_host_u16"
267 void node_i32_print (node_t *this)
269 primtype_recursive_print(this, "i32 ");
272 void node_i32_generate (node_t *this, enum passid which, FILE *ofp)
274 primtype_recursive_generate(this, which, ofp, "i32", "%ld", "(long)");
277 node_vft_t node_i32_vft = {
280 "clib_net_to_host_u32"
283 void node_i64_print (node_t *this)
285 primtype_recursive_print(this, "i64 ");
288 void node_i64_generate (node_t *this, enum passid which, FILE *ofp)
290 primtype_recursive_generate(this, which, ofp, "i64", "%lld",
294 node_vft_t node_i64_vft = {
297 "clib_net_to_host_u64"
300 void node_f64_print (node_t *this)
302 primtype_recursive_print(this, "f64 ");
305 void node_f64_generate (node_t *this, enum passid which, FILE *ofp)
307 primtype_recursive_generate(this, which, ofp, "f64", "%.2f",
311 node_vft_t node_f64_vft = {
314 " ", /* FP numbers are sent in host byte order */
318 void node_packed_print (node_t *this)
320 primtype_recursive_print (this, "packed ");
323 void node_packed_generate (node_t *this, enum passid which, FILE *ofp)
325 primtype_recursive_generate(this, which, ofp, "PACKED", "", "");
328 node_vft_t node_packed_vft = {
330 node_packed_generate,
334 void node_define_print (node_t *this)
336 fprintf(stdout, "define %s {\n", CDATA0);
338 node_vft_t *vftp = the_vft[this->deeper->type];
339 fprintf(stdout, " ");
340 vftp->print(this->deeper);
342 fprintf(stdout, "};\n");
345 void node_define_generate (node_t *this, enum passid which, FILE *fp)
351 fprintf(fp, "typedef VL_API_PACKED(struct _vl_api_%s {\n", CDATA0);
352 child = this->deeper;
355 node_vft_t *vftp = the_vft[child->type];
357 vftp->generate(child, which, fp);
361 fprintf(fp, "}) vl_api_%s_t;\n\n", CDATA0);
366 child = this->deeper;
368 node_vft_t *vftp = the_vft[child->type];
369 vftp->generate(child, which, fp);
375 fprintf(fp, "('%s',\n", CDATA0);
376 child = this->deeper;
379 node_vft_t *vftp = the_vft[child->type];
381 vftp->generate(child, which, fp);
385 fprintf(fp, "),\n\n");
389 fprintf(fp, "[\"%s\",\n", CDATA0);
390 child = this->deeper;
393 node_vft_t *vftp = the_vft[child->type];
395 vftp->generate(child, which, fp);
400 fprintf (fp, "{\"crc\" : \"0x%08x\"}\n", (u32)(u64)CDATA3);
407 fprintf(stderr, "node_define_generate: unimp pass %d\n", which);
412 node_vft_t node_define_vft = {
414 node_define_generate,
418 void node_union_print (node_t *this)
420 primtype_recursive_print (this, "union ");
423 void node_union_generate (node_t *this, enum passid which, FILE *fp)
431 fprintf(fp, "u8 _%s_which;\n", CDATA0);
433 fprintf(fp, "union _%s {\n", CDATA0);
434 child = this->deeper;
438 node_vft_t *vftp = the_vft[child->type];
440 vftp->generate(child, which, fp);
445 fprintf(fp, "} %s;\n", CDATA0);
450 uelem = this->deeper;
453 fprintf(fp, "switch(a->_%s_which) {\n",
456 current_union_name = CDATA0;
458 /* Walk the list of objects in this union */
460 node_vft_t *vftp = the_vft[uelem->type];
463 fprintf(fp, "case %d:\n", case_id);
466 /* Drill down on each element */
467 vftp->generate(uelem, which, fp);
469 fprintf(fp, "break;\n");
472 current_union_name = 0;
475 fprintf(fp, "default:\n");
478 if (which == PRINTFUN_PASS) {
480 "vl_print(handle, \"WARNING: _%s_which not set.\\n\");\n",
484 fprintf(fp, "break;\n");
491 fprintf(stderr, "node_union_generate: unimp pass %d\n", which);
497 node_vft_t node_union_vft = {
503 void node_scalar_print (node_t *this)
505 fprintf(stdout, "%s", CDATA0);
506 primtype_recursive_print (this, "");
509 void node_scalar_generate (node_t *this, enum passid which, FILE *fp)
511 char *union_prefix = "";
513 if (current_union_name) {
514 sprintf(tmpbuf, "%s.", current_union_name);
515 union_prefix = tmpbuf;
520 fprintf(fp, "%s;\n", CDATA0);
525 if (current_is_complex) {
526 fprintf(fp, "vl_api_%s_t_print(a->%s%s, handle);\n",
527 current_type_name, union_prefix, CDATA0);
529 if (!strcmp(current_type_fmt, "uword")) {
531 "vl_print(handle, \"%s%s: \" _uword_fmt \"\\n\", %s a->%s%s);\n",
532 union_prefix, CDATA0, "(_uword_cast)",
533 union_prefix, CDATA0);
536 "vl_print(handle, \"%s%s: %s\\n\", %s a->%s%s);\n",
537 union_prefix, CDATA0,
538 current_type_fmt, current_type_cast,
539 union_prefix, CDATA0);
546 if (current_is_complex) {
547 fprintf(fp, "vl_api%s_t_endian(a->%s%s);\n",
548 current_type_name, union_prefix, CDATA0);
550 /* Current_endianfun == NULL means e.g. it's a u8... */
551 if (current_endianfun) {
552 fprintf(fp, "a->%s%s = %s(a->%s%s);\n", union_prefix,
553 CDATA0, current_endianfun,
554 union_prefix, CDATA0);
556 fprintf(fp, "/* a->%s%s = a->%s%s (no-op) */\n",
557 union_prefix, CDATA0,
558 union_prefix, CDATA0);
563 fprintf(fp, "'%s'),\n", CDATA0);
567 fprintf(fp, "\"%s\"]", CDATA0);
571 fprintf(stderr, "node_scalar_generate: unimp pass %d\n", which);
574 fprintf(stderr, "broken recursion in node_scalar_generate\n");
579 node_vft_t node_scalar_vft = {
581 node_scalar_generate,
585 void node_vector_print (node_t *this)
587 primtype_recursive_print (this, "vector ");
590 void node_vector_generate (node_t *this, enum passid which, FILE *fp)
592 char *union_prefix = "";
594 if (current_union_name) {
595 sprintf(tmpbuf, "%s.", current_union_name);
596 union_prefix = tmpbuf;
601 fprintf(fp, "%s[%d];\n", CDATA0, IDATA1);
605 /* Don't bother about "u8 data [0];" et al. */
613 fprintf(fp, "int _i;\n");
615 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
619 if (current_is_complex) {
620 fprintf(fp, "vl_print(handle, \"%s%s[%%d]: ",
621 union_prefix, CDATA0);
623 "vl_print_%s (handle, a->%s%s[_i]);\n",
624 CDATA0, union_prefix, CDATA0);
627 "vl_print(handle, \"%s%s[%%d]: %s\\n\", _i, a->%s%s[_i]);\n",
628 union_prefix, CDATA0,
630 union_prefix, CDATA0);
641 /* Don't bother about "u8 data [0];" et al. */
644 /* If this is a simple endian swap, but the endian swap method is a no-op,
645 * then indicate this is a no-op in a comment.
647 if (!current_is_complex && current_endianfun == NULL) {
649 fprintf(fp, "/* a->%s%s[0..%d] = a->%s%s[0..%d] (no-op) */\n",
650 union_prefix, CDATA0, IDATA1 - 1,
651 union_prefix, CDATA0, IDATA1 - 1);
659 fprintf(fp, "int _i;\n");
661 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
665 if (current_is_complex) {
667 "vl_api_%s_t_endian (a->%s%s[_i]);\n",
668 current_type_name, union_prefix, CDATA0);
671 "a->%s%s[_i] = %s(a->%s%s[_i]);\n",
672 union_prefix, CDATA0,
674 union_prefix, CDATA0);
684 if (CDATA2 != 0) { // variable length vector
685 fprintf(fp, "'%s', '%d', '%s'),\n", CDATA0, IDATA1, CDATA2);
687 fprintf(fp, "'%s', '%d'),\n", CDATA0, IDATA1);
692 if (CDATA2 != 0) { /* variable length vector */
693 fprintf(fp, "\"%s\", %d, \"%s\"]", CDATA0, IDATA1, CDATA2);
695 fprintf(fp, "\"%s\", %d]", CDATA0, IDATA1);
700 fprintf(stderr, "node_vector_generate: unimp pass %d\n", which);
703 fprintf(stderr, "broken recursion in node_vector_generate\n");
707 node_vft_t node_vector_vft = {
709 node_vector_generate,
713 void node_complex_print (node_t *this)
715 primtype_recursive_print (this, "complex ");
718 void node_complex_generate (node_t *this, enum passid which, FILE *fp)
722 char *member_name = "broken!";
723 char *union_prefix = "";
725 if (current_union_name) {
726 sprintf(tmpbuf, "%s.", current_union_name);
727 union_prefix = tmpbuf;
730 current_is_complex++;
734 fprintf(fp, "%s ", CDATA0);
735 deeper = this->deeper;
737 vftp = the_vft[deeper->type];
738 vftp->generate(deeper, which, fp);
743 deeper = this->deeper;
745 if (deeper->type == NODE_SCALAR ||
746 deeper->type == NODE_VECTOR) {
747 member_name = deeper->data[0];
750 deeper = deeper->deeper;
753 fprintf(fp, "vl_print(handle, \"%s%s ----- \\n\");\n",
754 union_prefix, member_name);
756 fprintf(fp, "%s_print(&a->%s%s, handle);\n",
757 CDATA0, union_prefix, member_name);
759 fprintf(fp, "vl_print(handle, \"%s%s ----- END \\n\");\n",
760 union_prefix, member_name);
764 deeper = this->deeper;
766 if (deeper->type == NODE_SCALAR ||
767 deeper->type == NODE_VECTOR) {
768 member_name = deeper->data[0];
771 deeper = deeper->deeper;
775 fprintf(fp, "%s_endian(&a->%s%s);\n",
776 CDATA0, union_prefix, member_name);
779 fprintf(fp, "('%s',", CDATA0);
780 deeper = this->deeper;
782 vftp = the_vft[deeper->type];
783 vftp->generate(deeper, which, fp);
788 fprintf(fp, "[\"%s\", ", CDATA0);
789 deeper = this->deeper;
791 vftp = the_vft[deeper->type];
792 vftp->generate(deeper, which, fp);
797 fprintf(stderr, "node_complex_generate unimp pass %d...\n", which);
800 current_is_complex--;
803 node_vft_t node_complex_vft = {
805 node_complex_generate,
809 void node_noversion_print (node_t *this)
811 primtype_recursive_print (this, "noversion ");
814 void node_noversion_generate (node_t *this, enum passid which, FILE *ofp)
816 fprintf(stderr, "node_noversion_generate called...\n");
819 node_vft_t node_noversion_vft = {
820 node_noversion_print,
821 node_noversion_generate,
825 void node_uword_print (node_t *this)
827 primtype_recursive_print(this, "uword ");
830 void node_uword_generate (node_t *this, enum passid which, FILE *ofp)
832 primtype_recursive_generate(this, which, ofp, "uword", "uword", "");
835 node_vft_t node_uword_vft = {
838 "clib_net_to_host_uword",
841 node_vft_t *the_vft[NODE_N_TYPES] = {
862 void *make_node (enum node_subclass type)
866 rv = (node_t *) malloc (sizeof (*rv));
868 fprintf (stderr, "fatal: make_node out of memory\n");
871 bzero (rv, sizeof (*rv));
873 return ((void *) rv);
876 YYSTYPE deeper (YYSTYPE arg1, YYSTYPE arg2)
878 node_t *np1 = (node_t *) arg1;
879 node_t *np2 = (node_t *) arg2;
884 while (hook_point->deeper)
885 hook_point = hook_point->deeper;
887 hook_point->deeper = np2;
891 YYSTYPE addpeer (YYSTYPE arg1, YYSTYPE arg2)
893 node_t *np1 = (node_t *) arg1;
894 node_t *np2 = (node_t *) arg2;
899 while (hook_point->peer)
900 hook_point = hook_point->peer;
902 hook_point->peer = np2;
907 * add_slist (stmt_list, stmt)
910 YYSTYPE add_slist (YYSTYPE a1, YYSTYPE a2)
913 return (addpeer(a1, a2));
921 * add_define (char *name, defn_list);
923 YYSTYPE add_define (YYSTYPE a1, YYSTYPE a2)
927 np = make_node(NODE_DEFINE);
929 np->data[3] = (void *) message_crc;
930 deeper((YYSTYPE)np, a2);
931 return ((YYSTYPE) np);
935 * add_defbody (defn_list, new_defn)
937 YYSTYPE add_defbody (YYSTYPE a1, YYSTYPE a2)
939 return (addpeer(a1, a2));
943 * add_primtype ([packed], primitive type, instance)
946 YYSTYPE add_primtype (YYSTYPE a1, YYSTYPE a2, YYSTYPE a3)
948 /* Hook instance to type node */
957 * add_complex(char *type_name, instance)
960 YYSTYPE add_complex (YYSTYPE a1, YYSTYPE a2)
964 np = make_node(NODE_COMPLEX);
965 np->data[0] = (void *) a1;
967 deeper((YYSTYPE)np, a2);
968 return ((YYSTYPE) np);
972 * add_union(char *type_name, definition)
975 YYSTYPE add_union (YYSTYPE a1, YYSTYPE a2)
979 np = make_node(NODE_UNION);
980 np->data[0] = (void *) a1;
982 deeper((YYSTYPE)np, a2);
983 return ((YYSTYPE) np);
988 * add_vector_vbl (node_t *variable, YYSTYPE size)
991 YYSTYPE add_vector_vbl (YYSTYPE a1, YYSTYPE a2)
995 np = make_node(NODE_VECTOR);
996 np->data[0] = (void *) a1;
997 np->data[1] = (void *) a2;
998 return ((YYSTYPE) np);
1002 * add_vector_vbl (char *vector_name, char *vector_length_var)
1005 YYSTYPE add_variable_length_vector_vbl (YYSTYPE vector_name, YYSTYPE vector_length_var)
1009 np = make_node(NODE_VECTOR);
1010 np->data[0] = (void *) vector_name;
1011 np->data[1] = (void *) 0; // vector size used for vpe.api.h generation (array of length zero)
1012 np->data[2] = (void *) vector_length_var; // name of the variable that stores vector length
1013 return ((YYSTYPE) np);
1017 * add_scalar_vbl (char *name)
1019 YYSTYPE add_scalar_vbl (YYSTYPE a1)
1023 np = make_node(NODE_SCALAR);
1024 np->data[0] = (void *) a1;
1025 return ((YYSTYPE) np);
1029 * set_flags (int flags, msg(=0?))
1031 YYSTYPE set_flags(YYSTYPE a1, YYSTYPE a2)
1040 flags = (int)(uword) a1;
1048 YYSTYPE suppress_version (void)
1050 dont_output_version = 1;
1054 void dump(node_t *np)
1059 vftp = the_vft[np->type];
1065 char *fixup_input_filename(void)
1069 cp = (char *)input_filename;
1076 while (cp > input_filename && *cp != '/')
1081 strncpy (tmpbuf, cp, sizeof(tmpbuf)-1);
1090 while (cp > tmpbuf && *cp != '.')
1096 return (sxerox(tmpbuf));
1099 void generate_top_boilerplate(FILE *fp)
1102 char *datestring = ctime(&starttime);
1103 fixed_name = fixup_input_filename();
1107 fprintf (fp, "/*\n");
1108 fprintf (fp, " * VLIB API definitions %s\n", datestring);
1109 fprintf (fp, " * Input file: %s\n", input_filename);
1110 fprintf (fp, " * Automatically generated: please edit the input file ");
1111 fprintf (fp, "NOT this file!\n");
1112 fprintf (fp, " */\n\n");
1113 fprintf (fp, "#if defined(vl_msg_id)||defined(vl_union_id)||");
1114 fprintf (fp, "defined(vl_printfun) \\\n ||defined(vl_endianfun)||");
1115 fprintf (fp, " defined(vl_api_version)||defined(vl_typedefs) \\\n");
1116 fprintf (fp, " ||defined(vl_msg_name)||defined(vl_msg_name_crc_list)\n");
1117 fprintf (fp, "/* ok, something was selected */\n");
1118 fprintf (fp, "#else\n");
1119 fprintf (fp, "#warning no content included from %s\n", input_filename);
1120 fprintf (fp, "#endif\n\n");
1121 fprintf (fp, "#define VL_API_PACKED(x) x __attribute__ ((packed))\n\n");
1124 void generate_bottom_boilerplate(FILE *fp)
1127 fprintf (fp, "\n#ifdef vl_api_version\n");
1129 if (dont_output_version) {
1130 fprintf (fp, "/* WARNING: API FILE VERSION CHECK DISABLED */\n");
1134 fprintf (fp, "vl_api_version(%s, 0x%08x)\n\n",
1135 fixed_name, (unsigned int)input_crc);
1136 fprintf (fp, "#endif\n\n");
1139 void generate_msg_ids(YYSTYPE a1, FILE *fp)
1141 node_t *np = (node_t *)a1;
1143 fprintf (fp, "\n/****** Message ID / handler enum ******/\n\n");
1144 fprintf (fp, "#ifdef vl_msg_id\n");
1147 if (np->type == NODE_DEFINE) {
1148 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1149 fprintf (fp, "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n",
1150 uppercase(np->data[0]), (i8 *)np->data[0]);
1152 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1157 fprintf (fp, "#endif\n");
1161 void generate_msg_names(YYSTYPE a1, FILE *fp)
1163 node_t *np = (node_t *)a1;
1165 fprintf (fp, "\n/****** Message names ******/\n\n");
1167 fprintf (fp, "#ifdef vl_msg_name\n");
1170 if (np->type == NODE_DEFINE) {
1171 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1172 fprintf (fp, "vl_msg_name(vl_api_%s_t, %d)\n",
1174 (np->flags & NODE_FLAG_DONT_TRACE ? 0 : 1));
1176 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1181 fprintf (fp, "#endif\n\n");
1184 void generate_msg_name_crc_list (YYSTYPE a1, FILE *fp)
1186 node_t *np = (node_t *)a1;
1187 char *unique_suffix, *cp;
1189 unique_suffix = sxerox(fixed_name);
1192 while (*cp && (*cp != '.'))
1197 fprintf (fp, "\n/****** Message name, crc list ******/\n\n");
1199 fprintf (fp, "#ifdef vl_msg_name_crc_list\n");
1200 fprintf (fp, "#define foreach_vl_msg_name_crc_%s ", unique_suffix);
1203 if (np->type == NODE_DEFINE) {
1204 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1205 fprintf (fp, "\\\n_(VL_API_%s, %s, %08x) ",
1206 uppercase (np->data[0]), (i8 *) np->data[0],
1207 (u32)(u64)np->data[3]);
1212 fprintf (fp, "\n#endif\n\n");
1213 free (unique_suffix);
1216 void generate_typedefs(YYSTYPE a1, FILE *fp)
1218 node_t *np = (node_t *)a1;
1221 fprintf(fp, "\n/****** Typedefs *****/\n\n");
1222 fprintf(fp, "#ifdef vl_typedefs\n\n");
1224 /* Walk the top-level node-list */
1226 if (np->type == NODE_DEFINE) {
1227 /* Yeah, this is pedantic */
1228 vftp = the_vft[np->type];
1229 vftp->generate(np, TYPEDEF_PASS, fp);
1233 fprintf(fp, "#endif /* vl_typedefs */\n\n");
1236 void union_walk_one_defn(node_t *np, FILE *fp)
1241 /* Walk the list of typed objects in this msg def */
1243 if (np->type == NODE_UNION) {
1244 current_union_name = np->data[0];
1247 /* Walk the list of objects in this union */
1249 vblp = uelem->deeper;
1250 /* Drill down on each element, find the variable name */
1252 if (vblp->type == NODE_SCALAR ||
1253 vblp->type == NODE_VECTOR ||
1254 vblp->type == NODE_COMPLEX) {
1255 fprintf(ofp, "#define %s_",
1256 uppercase(current_def_name));
1257 fprintf(ofp, "%s_", uppercase(current_union_name));
1258 fprintf(ofp, "%s %d\n",uppercase(vblp->data[0]),
1263 vblp = vblp->deeper;
1265 uelem = uelem->peer;
1267 current_union_name = 0;
1274 void generate_uniondefs(YYSTYPE a1, FILE *fp)
1276 node_t *np = (node_t *)a1;
1278 fprintf(fp, "/****** Discriminated Union Definitions *****/\n\n");
1279 fprintf(fp, "#ifdef vl_union_id\n\n");
1281 /* Walk the top-level node-list */
1283 if (np->type == NODE_DEFINE) {
1285 current_def_name = np->data[0];
1286 union_walk_one_defn(np->deeper, fp);
1290 fprintf(fp, "\n#endif /* vl_union_id */\n\n");
1293 void generate_printfun(YYSTYPE a1, FILE *fp)
1295 node_t *np = (node_t *)a1;
1298 fprintf(fp, "/****** Print functions *****/\n\n");
1299 fprintf(fp, "#ifdef vl_printfun\n\n");
1301 fprintf(fp, "#ifdef LP64\n");
1302 fputs ("#define _uword_fmt \"%lld\"\n", fp);
1303 fputs ("#define _uword_cast (long long)\n", fp);
1304 fprintf(fp, "#else\n");
1305 fputs("#define _uword_fmt \"%ld\"\n", fp);
1306 fputs ("#define _uword_cast long\n", fp);
1307 fprintf(fp, "#endif\n\n");
1309 /* Walk the top-level node-list */
1311 if (np->type == NODE_DEFINE) {
1312 if (!(np->flags & NODE_FLAG_MANUAL_PRINT)) {
1314 "static inline void *vl_api_%s_t_print (vl_api_%s_t *a,",
1315 (i8 *)np->data[0], (i8 *) np->data[0]);
1316 fprintf(fp, "void *handle)\n{\n");
1317 /* output the message name */
1319 " vl_print(handle, \"vl_api_%s_t:\\n\");\n",
1323 /* Yeah, this is pedantic */
1324 vftp = the_vft[np->type];
1325 vftp->generate(np, PRINTFUN_PASS, fp);
1326 fprintf(fp, " return handle;\n");
1327 fprintf(fp, "}\n\n");
1330 fprintf(fp, "/***** manual: vl_api_%s_t_print *****/\n\n",
1331 (i8 *) np->data[0]);
1336 fprintf(fp, "#endif /* vl_printfun */\n\n");
1339 void generate_endianfun(YYSTYPE a1, FILE *fp)
1341 node_t *np = (node_t *)a1;
1344 fprintf(fp, "\n/****** Endian swap functions *****/\n\n");
1345 fprintf(fp, "#ifdef vl_endianfun\n\n");
1346 fprintf(fp, "#undef clib_net_to_host_uword\n");
1347 fprintf(fp, "#ifdef LP64\n");
1348 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u64\n");
1349 fprintf(fp, "#else\n");
1350 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u32\n");
1351 fprintf(fp, "#endif\n\n");
1353 /* Walk the top-level node-list */
1355 if (np->type == NODE_DEFINE) {
1356 if (!(np->flags & NODE_FLAG_MANUAL_ENDIAN)) {
1358 "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)\n{\n",
1359 (i8 *) np->data[0], (i8 *) np->data[0]);
1361 /* Yeah, this is pedantic */
1362 vftp = the_vft[np->type];
1363 vftp->generate(np, ENDIANFUN_PASS, fp);
1364 fprintf(fp, "}\n\n");
1367 fprintf(fp, "/***** manual: vl_api_%s_t_endian *****/\n\n",
1368 (i8 *) np->data[0]);
1373 fprintf(fp, "#endif /* vl_endianfun */\n\n");
1376 void add_msg_ids(YYSTYPE a1)
1378 node_t *np = (node_t *)a1;
1382 /* Walk the top-level node-list */
1384 if (np->type == NODE_DEFINE) {
1385 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1386 /* add the parse tree for "u16 _vl_msg_id" */
1387 new_u16 = make_node(NODE_U16);
1388 new_u16->peer = np->deeper;
1389 np->deeper = new_u16;
1390 new_vbl = make_node(NODE_SCALAR);
1391 new_vbl->data[0] = sxerox("_vl_msg_id");
1392 new_u16->deeper = new_vbl;
1399 void generate_python_msg_definitions(YYSTYPE a1, FILE *fp)
1401 node_t *np = (node_t *)a1;
1403 fprintf (fp, "messages = [\n");
1404 /* Walk the top-level node-list */
1406 if (np->type == NODE_DEFINE && !(np->flags & NODE_FLAG_TYPEONLY)) {
1407 /* Yeah, this is pedantic */
1408 vftp = the_vft[np->type];
1409 vftp->generate(np, PYTHON_PASS, fp);
1413 fprintf (fp, "\n]\n");
1417 is_typeonly_check(node_t *np, bool typeonly)
1419 bool is_typeonly = (np->flags & NODE_FLAG_TYPEONLY);
1420 return (is_typeonly == typeonly);
1424 generate_json_definitions(YYSTYPE a1, FILE *fp, bool typeonly)
1426 node_t *np = (node_t *)a1;
1430 fprintf (fp, "\"types\" : [\n");
1432 fprintf (fp, "\"messages\" : [\n");
1434 /* Walk the top-level node-list */
1438 if (np->type == NODE_DEFINE && is_typeonly_check(np, typeonly)) {
1439 /* Yeah, this is pedantic */
1440 vftp = the_vft[np->type];
1442 vftp->generate(np, JSON_PASS, fp);
1447 np->type == NODE_DEFINE && is_typeonly_check(np, typeonly))
1448 fprintf (fp, ",\n");
1457 void generate_python_typeonly_definitions(YYSTYPE a1, FILE *fp)
1459 node_t *np = (node_t *)a1;
1461 fprintf (fp, "types = [\n");
1462 /* Walk the top-level node-list */
1464 if (np->type == NODE_DEFINE && (np->flags & NODE_FLAG_TYPEONLY)) {
1465 vftp = the_vft[np->type];
1466 vftp->generate(np, PYTHON_PASS, fp);
1470 fprintf (fp, "\n]\n");
1473 void generate_python(YYSTYPE a1, FILE *fp)
1475 generate_python_typeonly_definitions(a1, fp);
1476 generate_python_msg_definitions(a1, fp);
1481 fprintf (fp, "vl_api_version = 0x%08x\n\n", (unsigned int)input_crc);
1484 void generate_json(YYSTYPE a1, FILE *fp)
1486 fprintf (fp, "{\n");
1488 generate_json_definitions(a1, fp, true);
1489 fprintf (fp, ",\n");
1490 generate_json_definitions(a1, fp, false);
1495 fprintf (fp, ",\n\"vl_api_version\" :\"0x%08x\"\n",
1496 (unsigned int)input_crc);
1497 fprintf (fp, "}\n");
1500 void generate(YYSTYPE a1)
1509 generate_top_boilerplate(ofp);
1511 generate_msg_ids(a1, ofp);
1512 generate_msg_names(a1, ofp);
1513 generate_msg_name_crc_list(a1, ofp);
1514 generate_typedefs(a1, ofp);
1515 generate_uniondefs(a1, ofp);
1516 generate_printfun(a1, ofp);
1517 generate_endianfun(a1, ofp);
1519 generate_bottom_boilerplate(ofp);
1522 generate_python(a1, pythonfp);
1525 generate_json(a1, jsonfp);