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 *------------------------------------------------------------------
26 #include <vppinfra/vec.h>
27 #include <vppinfra/hash.h>
32 #define YYSTYPE void *
42 node_vft_t *the_vft[NODE_N_TYPES];
44 static int dont_output_version;
46 static char *fixed_name;
47 static char tmpbuf [MAXNAME];
48 static char *current_def_name;
49 static char *current_union_name;
50 static char *current_type_fmt;
51 static char *current_type_cast;
52 static char current_id;
53 static char current_is_complex;
54 static char *current_endianfun;
55 static char *current_type_name;
56 static int current_java_parameter_number;
57 static int current_java_emitted_parameter;
58 static int current_java_parameter_need_comma_space;
59 void *current_java_methodfun;
60 void *current_java_jnifun;
62 void indent_me(FILE *ofp)
66 for (i = 0; i < indent; i++)
70 char *uppercase (char *s)
76 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
77 if (*s >= 'a' && *s <= 'z')
78 *cp++ = *s++ - ('a' - 'A');
86 char *lowercase (char *s)
92 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
93 if (*s >= 'A' && *s <= 'Z')
94 *cp++ = *s++ + ('a' - 'A');
103 * javah maps foo_bar to foo_1bar for whatever freakin' reason
104 * So, adjust java names accordingly.
106 char *java_name_mangle (void * name_arg)
108 char * name = name_arg;
112 vec_reset_length (s);
114 s = format (s, "%s%c", name, 0);
116 for (i = 0; i < vec_len(s); i++)
118 vec_delete (s, 1, i);
119 if (s[i] >= 'a' && s[i] <= 'z')
127 void primtype_recursive_print(node_t *this, i8 *fmt)
129 fputs((char *)fmt, stdout);
132 node_vft_t *vftp = the_vft[this->deeper->type];
133 vftp->print(this->deeper);
137 void primtype_recursive_generate(node_t *this, enum passid which, FILE *ofp,
138 i8 *type_name, i8 *type_fmt, i8 *type_cast)
142 current_type_name = (char *)type_name;
143 current_type_cast = (char *)type_cast;
147 fputs((char *)type_name, ofp);
152 current_type_fmt = (char *)type_fmt;
156 vftp = the_vft[this->type];
157 current_endianfun = vftp->endian_converter;
160 case JAVA_METHOD_PASS:
161 vftp = the_vft[this->type];
162 current_java_methodfun = vftp->java_method_function;
166 fputs("('", pythonfp);
167 fputs((char *)type_name, pythonfp);
168 fputs("', ", pythonfp);
172 fprintf(stderr, "primtype_recursive_generate: unimp pass %d\n", which);
177 vftp = the_vft[this->deeper->type];
178 vftp->generate(this->deeper, which, ofp);
182 static int hidden_from_java(const node_t * deeper)
184 if (current_java_parameter_number++ < 3) {
185 if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
187 else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
189 else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
196 void primtype_java_method (node_t * this, enum passid which, FILE *ofp,
197 char *java_type_name)
201 deeper = this->deeper;
203 /* We'll take care of _msg_id, client_index, and context ourselves */
204 if (hidden_from_java(deeper)) {
208 if (deeper->type == NODE_SCALAR)
209 fprintf (ofp, "%s %s", java_type_name,
210 java_name_mangle(deeper->data[0]));
212 fprintf (ofp, "%s [] %s", java_type_name,
213 java_name_mangle(deeper->data[0]));
215 current_java_emitted_parameter = 1;
218 void primtype_java_parameter (node_t * this, enum passid which, FILE *ofp,
219 char *java_type_name)
223 deeper = this->deeper;
225 /* We'll take care of _msg_id, client_index, and context ourselves */
226 if (hidden_from_java(deeper)) {
229 if (current_java_parameter_need_comma_space) {
230 current_java_parameter_need_comma_space = 0;
234 if (deeper->type == NODE_SCALAR)
235 fprintf (ofp, "%s %s", java_type_name, (char *)(deeper->data[0]));
237 fprintf (ofp, "%sArray %s", java_type_name, (char *)(deeper->data[0]));
239 current_java_emitted_parameter = 1;
242 void primtype_java_setup (node_t * this, enum passid which, FILE *ofp,
243 char *java_type_name, char *array_element_name)
247 deeper = this->deeper;
249 /* We'll take care of _msg_id, client_index, and context ourselves */
250 if (hidden_from_java(deeper)) {
254 if (deeper->type == NODE_VECTOR) {
257 "%s * %sP = (*env)->Get%sArrayElements (env, %s, NULL);\n",
258 java_type_name, (char *)(deeper->data[0]),
259 array_element_name, (char *)(deeper->data[0]));
262 current_java_emitted_parameter = 1;
265 void primtype_java_code (node_t * this, enum passid which, FILE *ofp,
266 char *java_type_name, char * swapper)
271 deeper = this->deeper;
273 /* We'll take care of _msg_id, client_index, and context ourselves */
274 if (hidden_from_java(deeper)) {
280 s = (char *)(deeper->data[0]);
283 if (deeper->type == NODE_VECTOR)
284 fprintf (ofp, "memcpy (mp->%s, %sP, sizeof (mp->%s));\n",
287 fprintf (ofp, "mp->%s = %s;\n", s, s);
289 if (deeper->type == NODE_VECTOR) {
293 fprintf(ofp, "int _i;\n");
295 fprintf(ofp, "for (_i = 0; _i < %lu; _i++) {\n",
296 (u64)(deeper->data[1]));
299 fprintf(ofp, "mp->%s[_i] = %s(%sP[_i]);\n",
308 fprintf (ofp, "mp->%s = %s(%s);\n", s, swapper, s);
312 current_java_emitted_parameter = 1;
315 void primtype_java_teardown (node_t * this, enum passid which, FILE *ofp,
316 char * array_element_name)
320 deeper = this->deeper;
322 /* We'll take care of _msg_id, client_index, and context ourselves */
323 if (hidden_from_java(deeper)) {
327 if (deeper->type == NODE_VECTOR) {
330 "(*env)->Release%sArrayElements (env, %s, %sP, 0);\n",
332 (char *)(deeper->data[0]),
333 (char *)(deeper->data[0]));
336 current_java_emitted_parameter = 1;
339 void node_illegal_print (node_t *this)
341 fprintf(stderr, "node_illegal_print called\n");
345 void node_illegal_generate (node_t *this, enum passid notused, FILE *ofp)
347 fprintf(stderr, "node_illegal_generate called\n");
351 void node_illegal_java_method (node_t *this, enum passid notused, FILE *ofp)
353 fprintf(stderr, "node_illegal_java_method called\n");
357 void node_illegal_java_jni (node_t *this, enum passid notused, FILE *ofp)
359 fprintf(stderr, "node_illegal_java_jni called\n");
363 node_vft_t node_illegal_vft = {
365 node_illegal_generate,
367 node_illegal_java_method,
368 node_illegal_java_jni,
371 void node_u8_print (node_t *this)
373 primtype_recursive_print(this, "u8 ");
376 void node_u8_generate (node_t *this, enum passid which, FILE *ofp)
378 primtype_recursive_generate(this, which, ofp, "u8", "%u", "(unsigned)");
381 void node_u8_java_method (node_t *this, enum passid which, FILE *ofp)
383 primtype_java_method (this, which, ofp, "byte");
386 void node_u8_java_parameter (node_t *this, enum passid which, FILE *ofp)
388 primtype_java_parameter (this, which, ofp, "jbyte");
391 void node_u8_java_setup (node_t *this, enum passid which, FILE *ofp)
393 primtype_java_setup (this, which, ofp, "jbyte", "Byte");
396 void node_u8_java_code (node_t *this, enum passid which, FILE *ofp)
398 primtype_java_code (this, which, ofp, "jbyte", 0 /* swapper */);
401 void node_u8_java_teardown (node_t *this, enum passid which, FILE *ofp)
403 primtype_java_teardown (this, which, ofp, "Byte");
406 node_vft_t node_u8_vft = {
411 node_u8_java_parameter,
414 node_u8_java_teardown,
417 void node_u16_print (node_t *this)
419 primtype_recursive_print(this, "u16 ");
422 void node_u16_generate (node_t *this, enum passid which, FILE *ofp)
424 primtype_recursive_generate(this, which, ofp, "u16", "%u", "(unsigned)");
427 void node_u16_java_method (node_t *this, enum passid which, FILE *ofp)
429 primtype_java_method (this, which, ofp, "short");
432 void node_u16_java_parameter (node_t *this, enum passid which, FILE *ofp)
434 primtype_java_parameter (this, which, ofp, "jshort");
437 void node_u16_java_setup (node_t *this, enum passid which, FILE *ofp)
439 primtype_java_setup (this, which, ofp, "jshort", "Short");
442 void node_u16_java_code (node_t *this, enum passid which, FILE *ofp)
444 primtype_java_code (this, which, ofp, "jshort", "clib_host_to_net_u16");
447 void node_u16_java_teardown (node_t *this, enum passid which, FILE *ofp)
449 primtype_java_teardown (this, which, ofp, "Short");
452 node_vft_t node_u16_vft = {
455 "clib_net_to_host_u16",
456 node_u16_java_method,
457 node_u16_java_parameter,
460 node_u16_java_teardown,
463 void node_u32_print (node_t *this)
465 primtype_recursive_print(this, "u32 ");
468 void node_u32_generate (node_t *this, enum passid which, FILE *ofp)
470 primtype_recursive_generate(this, which, ofp, "u32", "%u", "(unsigned)");
473 void node_u32_java_method (node_t *this, enum passid which, FILE *ofp)
475 primtype_java_method (this, which, ofp, "int");
478 void node_u32_java_parameter (node_t *this, enum passid which, FILE *ofp)
480 primtype_java_parameter (this, which, ofp, "jint");
483 void node_u32_java_setup (node_t *this, enum passid which, FILE *ofp)
485 primtype_java_setup (this, which, ofp, "jint", "Int");
488 void node_u32_java_code (node_t *this, enum passid which, FILE *ofp)
490 primtype_java_code (this, which, ofp, "jint", "clib_host_to_net_u32");
493 void node_u32_java_teardown (node_t *this, enum passid which, FILE *ofp)
495 primtype_java_teardown (this, which, ofp, "Int");
498 node_vft_t node_u32_vft = {
501 "clib_net_to_host_u32",
502 node_u32_java_method,
503 node_u32_java_parameter,
506 node_u32_java_teardown,
509 void node_u64_print (node_t *this)
511 primtype_recursive_print(this, "u64 ");
514 void node_u64_generate (node_t *this, enum passid which, FILE *ofp)
516 primtype_recursive_generate(this, which, ofp, "u64", "%llu",
520 void node_u64_java_method (node_t *this, enum passid which, FILE *ofp)
522 primtype_java_method (this, which, ofp, "long");
525 void node_u64_java_parameter (node_t *this, enum passid which, FILE *ofp)
527 primtype_java_parameter (this, which, ofp, "jlong");
530 void node_u64_java_setup (node_t *this, enum passid which, FILE *ofp)
532 primtype_java_setup (this, which, ofp, "jlong", "Long");
535 void node_u64_java_code (node_t *this, enum passid which, FILE *ofp)
537 primtype_java_code (this, which, ofp, "jlong", "clib_host_to_net_u64");
540 void node_u64_java_teardown (node_t *this, enum passid which, FILE *ofp)
542 primtype_java_teardown (this, which, ofp, "Long");
545 node_vft_t node_u64_vft = {
548 "clib_net_to_host_u64",
549 node_u64_java_method,
550 node_u64_java_parameter,
553 node_u64_java_teardown,
556 void node_i8_print (node_t *this)
558 primtype_recursive_print(this, "i8 ");
561 void node_i8_generate (node_t *this, enum passid which, FILE *ofp)
563 primtype_recursive_generate(this, which, ofp, "i8", "%d", "(int)");
566 node_vft_t node_i8_vft = {
571 node_u8_java_parameter,
574 node_u8_java_teardown,
577 void node_i16_print (node_t *this)
579 primtype_recursive_print(this, "i16 ");
582 void node_i16_generate (node_t *this, enum passid which, FILE *ofp)
584 primtype_recursive_generate(this, which, ofp, "i16", "%d", "(int)");
587 node_vft_t node_i16_vft = {
590 "clib_net_to_host_u16",
591 node_u16_java_method,
592 node_u16_java_parameter,
595 node_u16_java_teardown,
598 void node_i32_print (node_t *this)
600 primtype_recursive_print(this, "i32 ");
603 void node_i32_generate (node_t *this, enum passid which, FILE *ofp)
605 primtype_recursive_generate(this, which, ofp, "i32", "%ld", "(long)");
608 node_vft_t node_i32_vft = {
611 "clib_net_to_host_u32",
612 node_u32_java_method,
613 node_u32_java_parameter,
616 node_u32_java_teardown,
619 void node_i64_print (node_t *this)
621 primtype_recursive_print(this, "i64 ");
624 void node_i64_generate (node_t *this, enum passid which, FILE *ofp)
626 primtype_recursive_generate(this, which, ofp, "i64", "%lld",
630 node_vft_t node_i64_vft = {
633 "clib_net_to_host_u64",
634 node_u64_java_method,
635 node_u64_java_parameter,
638 node_u64_java_teardown,
641 void node_f64_print (node_t *this)
643 primtype_recursive_print(this, "f64 ");
646 void node_f64_generate (node_t *this, enum passid which, FILE *ofp)
648 primtype_recursive_generate(this, which, ofp, "f64", "%.2f",
651 void node_f64_java_method (node_t *this, enum passid which, FILE *ofp)
653 primtype_java_method (this, which, ofp, "double");
656 void node_f64_java_parameter (node_t *this, enum passid which, FILE *ofp)
658 primtype_java_parameter (this, which, ofp, "jdouble");
661 void node_f64_java_setup (node_t *this, enum passid which, FILE *ofp)
663 primtype_java_setup (this, which, ofp, "jdouble", "Double");
666 void node_f64_java_code (node_t *this, enum passid which, FILE *ofp)
669 * Current API code doesn't try to endian-swap doubles
670 * FP formats aren't portable yadda yadda yadda
672 primtype_java_code (this, which, ofp, "jdouble", 0 /* $$$ */);
675 void node_f64_java_teardown (node_t *this, enum passid which, FILE *ofp)
677 primtype_java_teardown (this, which, ofp, "Double");
680 node_vft_t node_f64_vft = {
683 " ", /* FP numbers are sent in host byte order */
684 node_f64_java_method,
685 node_f64_java_parameter,
688 node_f64_java_teardown,
692 void node_packed_print (node_t *this)
694 primtype_recursive_print (this, "packed ");
697 void node_packed_generate (node_t *this, enum passid which, FILE *ofp)
699 primtype_recursive_generate(this, which, ofp, "PACKED", "", "");
702 node_vft_t node_packed_vft = {
704 node_packed_generate,
708 void node_define_print (node_t *this)
710 fprintf(stdout, "define %s {\n", CDATA0);
712 node_vft_t *vftp = the_vft[this->deeper->type];
713 fprintf(stdout, " ");
714 vftp->print(this->deeper);
716 fprintf(stdout, "};\n");
719 static void emit_java_arg_declaration(node_t *child, FILE *fp) {
720 current_java_parameter_number = 0;
722 node_vft_t *vftp = the_vft[child->type];
723 current_java_emitted_parameter = 0;
724 vftp->java_method_function(child, JAVA_METHOD_PASS, fp);
726 if (child && current_java_emitted_parameter)
731 void node_define_generate (node_t *this, enum passid which, FILE *fp)
733 node_t *child, *save_child;
737 fprintf(fp, "typedef VL_API_PACKED(struct _vl_api_%s {\n", CDATA0);
738 child = this->deeper;
741 node_vft_t *vftp = the_vft[child->type];
743 vftp->generate(child, which, fp);
747 fprintf(fp, "}) vl_api_%s_t;\n\n", CDATA0);
752 child = this->deeper;
754 node_vft_t *vftp = the_vft[child->type];
755 vftp->generate(child, which, fp);
760 case JAVA_METHOD_PASS:
764 /* Generate private native declaration */
765 fprintf (fp, "private static native int %s0(", java_name_mangle(CDATA0));
766 emit_java_arg_declaration(this->deeper, fp);
769 /* Generate public Java method */
771 fprintf (fp, "public final int %s(", java_name_mangle(CDATA0));
772 emit_java_arg_declaration(this->deeper, fp);
777 fputs ("checkConnected();\n", fp);
779 fprintf (fp, "return %s.%s0(", java_class, java_name_mangle(CDATA0));
781 child = this->deeper;
782 current_java_parameter_number = 0;
783 while (child && hidden_from_java(child->deeper)) {
787 fputs(java_name_mangle((char *)(child->deeper->data[0])), fp);
801 /* Generate function prototype */
802 fprintf (fp, "JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_%s_%s0\n",
803 java_class, java_name_mangle(CDATA0));
805 fprintf (fp, "(JNIEnv * env, jclass clazz");
806 current_java_parameter_need_comma_space = 1;
807 child = this->deeper;
810 node_vft_t *vftp = the_vft[child->type];
811 current_java_emitted_parameter = 0;
812 vftp->java_jni_parameter(child, which, fp);
814 if (child && current_java_emitted_parameter)
817 fprintf (fp, ")\n{\n");
820 /* define the api message pointer */
822 fprintf (fp, "vppjni_main_t *jm = &vppjni_main;\n");
824 fprintf (fp, "vl_api_%s_t * mp;\n", current_def_name);
826 fprintf (fp, "u32 my_context_id;\n");
828 fprintf (fp, "int rv;\n");
831 fprintf (fp, "rv = vppjni_sanity_check (jm);\n");
833 fprintf (fp, "if (rv) return rv;\n");
836 fprintf (fp, "my_context_id = vppjni_get_context_id (jm);\n");
838 /* Generate array setups, if any */
841 node_vft_t *vftp = the_vft[child->type];
842 current_java_parameter_number = 0;
843 current_java_emitted_parameter = 0;
844 vftp->java_jni_setup(child, which, fp);
848 /* Setup the API message */
850 fprintf (fp, "M(%s, %s);\n", uppercase(current_def_name),
853 fprintf (fp, "mp->context = clib_host_to_net_u32 (my_context_id);\n");
854 /* $$$ Set up context hash table or some such... */
859 node_vft_t *vftp = the_vft[child->type];
860 current_java_parameter_number = 0;
861 current_java_emitted_parameter = 0;
862 vftp->java_jni_code(child, which, fp);
866 /* Generate array teardowns */
869 node_vft_t *vftp = the_vft[child->type];
870 current_java_parameter_number = 0;
871 current_java_emitted_parameter = 0;
872 vftp->java_jni_teardown(child, which, fp);
876 /* Send the message, return context_id */
878 fprintf (fp, "S;\n");
880 fprintf (fp, "return my_context_id;\n");
883 fprintf (fp, "}\n\n");
887 fprintf(fp, "('%s',\n", CDATA0);
888 child = this->deeper;
891 node_vft_t *vftp = the_vft[child->type];
893 vftp->generate(child, which, fp);
897 fprintf(fp, "),\n\n");
901 fprintf(stderr, "node_define_generate: unimp pass %d\n", which);
906 node_vft_t node_define_vft = {
908 node_define_generate,
912 void node_union_print (node_t *this)
914 primtype_recursive_print (this, "union ");
917 void node_union_generate (node_t *this, enum passid which, FILE *fp)
925 fprintf(fp, "u8 _%s_which;\n", CDATA0);
927 fprintf(fp, "union _%s {\n", CDATA0);
928 child = this->deeper;
932 node_vft_t *vftp = the_vft[child->type];
934 vftp->generate(child, which, fp);
939 fprintf(fp, "} %s;\n", CDATA0);
944 uelem = this->deeper;
947 fprintf(fp, "switch(a->_%s_which) {\n",
950 current_union_name = CDATA0;
952 /* Walk the list of objects in this union */
954 node_vft_t *vftp = the_vft[uelem->type];
957 fprintf(fp, "case %d:\n", case_id);
960 /* Drill down on each element */
961 vftp->generate(uelem, which, fp);
963 fprintf(fp, "break;\n");
966 current_union_name = 0;
969 fprintf(fp, "default:\n");
972 if (which == PRINTFUN_PASS) {
974 "vl_print(handle, \"WARNING: _%s_which not set.\\n\");\n",
978 fprintf(fp, "break;\n");
985 fprintf(stderr, "node_union_generate: unimp pass %d\n", which);
991 node_vft_t node_union_vft = {
997 void node_scalar_print (node_t *this)
999 fprintf(stdout, "%s", CDATA0);
1000 primtype_recursive_print (this, "");
1003 void node_scalar_generate (node_t *this, enum passid which, FILE *fp)
1005 char *union_prefix = "";
1007 if (current_union_name) {
1008 sprintf(tmpbuf, "%s.", current_union_name);
1009 union_prefix = tmpbuf;
1014 fprintf(fp, "%s;\n", CDATA0);
1019 if (current_is_complex) {
1020 fprintf(fp, "vl_api_%s_t_print(a->%s%s, handle);\n",
1021 current_type_name, union_prefix, CDATA0);
1023 if (!strcmp(current_type_fmt, "uword")) {
1025 "vl_print(handle, \"%s%s: \" _uword_fmt \"\\n\", %s a->%s%s);\n",
1026 union_prefix, CDATA0, "(_uword_cast)",
1027 union_prefix, CDATA0);
1030 "vl_print(handle, \"%s%s: %s\\n\", %s a->%s%s);\n",
1031 union_prefix, CDATA0,
1032 current_type_fmt, current_type_cast,
1033 union_prefix, CDATA0);
1038 case ENDIANFUN_PASS:
1040 if (current_is_complex) {
1041 fprintf(fp, "vl_api%s_t_endian(a->%s%s);\n",
1042 current_type_name, union_prefix, CDATA0);
1044 /* Current_endianfun == NULL means e.g. it's a u8... */
1045 if (current_endianfun) {
1046 fprintf(fp, "a->%s%s = %s(a->%s%s);\n", union_prefix,
1047 CDATA0, current_endianfun,
1048 union_prefix, CDATA0);
1050 fprintf(fp, "/* a->%s%s = a->%s%s */\n",
1051 union_prefix, CDATA0,
1052 union_prefix, CDATA0);
1057 fprintf(fp, "'%s'),\n", CDATA0);
1061 fprintf(stderr, "node_scalar_generate: unimp pass %d\n", which);
1064 fprintf(stderr, "broken recursion in node_scalar_generate\n");
1069 node_vft_t node_scalar_vft = {
1071 node_scalar_generate,
1075 void node_vector_print (node_t *this)
1077 primtype_recursive_print (this, "vector ");
1080 void node_vector_generate (node_t *this, enum passid which, FILE *fp)
1082 char *union_prefix = "";
1084 if (current_union_name) {
1085 sprintf(tmpbuf, "%s.", current_union_name);
1086 union_prefix = tmpbuf;
1091 fprintf(fp, "%s[%d];\n", CDATA0, IDATA1);
1095 /* Don't bother about "u8 data [0];" et al. */
1103 fprintf(fp, "int _i;\n");
1105 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
1109 if (current_is_complex) {
1110 fprintf(fp, "vl_print(handle, \"%s%s[%%d]: ",
1111 union_prefix, CDATA0);
1113 "vl_print_%s (handle, a->%s%s[_i]);\n",
1114 CDATA0, union_prefix, CDATA0);
1117 "vl_print(handle, \"%s%s[%%d]: %s\\n\", _i, a->%s%s[_i]);\n",
1118 union_prefix, CDATA0,
1120 union_prefix, CDATA0);
1130 case ENDIANFUN_PASS:
1131 /* Don't bother about "u8 data [0];" et al. */
1139 fprintf(fp, "int _i;\n");
1141 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
1145 if (current_is_complex) {
1147 "vl_api_%s_t_endian (a->%s%s[_i]);\n",
1148 current_type_name, union_prefix, CDATA0);
1151 "a->%s%s[_i] = %s(a->%s%s[_i]);\n",
1152 union_prefix, CDATA0,
1154 union_prefix, CDATA0);
1164 fprintf(fp, "'%s', '%d'),\n", CDATA0, IDATA1);
1168 fprintf(stderr, "node_vector_generate: unimp pass %d\n", which);
1171 fprintf(stderr, "broken recursion in node_vector_generate\n");
1175 node_vft_t node_vector_vft = {
1177 node_vector_generate,
1181 void node_complex_print (node_t *this)
1183 primtype_recursive_print (this, "complex ");
1186 void node_complex_generate (node_t *this, enum passid which, FILE *fp)
1190 char *member_name = "broken!";
1191 char *union_prefix = "";
1193 if (current_union_name) {
1194 sprintf(tmpbuf, "%s.", current_union_name);
1195 union_prefix = tmpbuf;
1198 current_is_complex++;
1202 fprintf(fp, "%s ", CDATA0);
1203 deeper = this->deeper;
1205 vftp = the_vft[deeper->type];
1206 vftp->generate(deeper, which, fp);
1211 deeper = this->deeper;
1213 if (deeper->type == NODE_SCALAR ||
1214 deeper->type == NODE_VECTOR) {
1215 member_name = deeper->data[0];
1218 deeper = deeper->deeper;
1221 fprintf(fp, "vl_print(handle, \"%s%s ----- \\n\");\n",
1222 union_prefix, member_name);
1224 fprintf(fp, "%s_print(&a->%s%s, handle);\n",
1225 CDATA0, union_prefix, member_name);
1227 fprintf(fp, "vl_print(handle, \"%s%s ----- END \\n\");\n",
1228 union_prefix, member_name);
1231 case ENDIANFUN_PASS:
1232 deeper = this->deeper;
1234 if (deeper->type == NODE_SCALAR ||
1235 deeper->type == NODE_VECTOR) {
1236 member_name = deeper->data[0];
1239 deeper = deeper->deeper;
1243 fprintf(fp, "%s_endian(&a->%s%s);\n",
1244 CDATA0, union_prefix, member_name);
1247 fprintf(fp, "('%s',", CDATA0);
1248 deeper = this->deeper;
1250 vftp = the_vft[deeper->type];
1251 vftp->generate(deeper, which, fp);
1256 fprintf(stderr, "node_complex_generate unimp pass %d...\n", which);
1259 current_is_complex--;
1262 node_vft_t node_complex_vft = {
1264 node_complex_generate,
1268 void node_noversion_print (node_t *this)
1270 primtype_recursive_print (this, "noversion ");
1273 void node_noversion_generate (node_t *this, enum passid which, FILE *ofp)
1275 fprintf(stderr, "node_noversion_generate called...\n");
1278 node_vft_t node_noversion_vft = {
1279 node_noversion_print,
1280 node_noversion_generate,
1284 void node_uword_print (node_t *this)
1286 primtype_recursive_print(this, "uword ");
1289 void node_uword_generate (node_t *this, enum passid which, FILE *ofp)
1291 primtype_recursive_generate(this, which, ofp, "uword", "uword", "");
1294 node_vft_t node_uword_vft = {
1296 node_uword_generate,
1297 "clib_net_to_host_uword",
1300 node_vft_t *the_vft[NODE_N_TYPES] = {
1317 &node_noversion_vft,
1321 void *make_node (enum node_subclass type)
1325 rv = (node_t *) malloc (sizeof (*rv));
1327 fprintf (stderr, "fatal: make_node out of memory\n");
1330 bzero (rv, sizeof (*rv));
1332 return ((void *) rv);
1335 YYSTYPE deeper (YYSTYPE arg1, YYSTYPE arg2)
1337 node_t *np1 = (node_t *) arg1;
1338 node_t *np2 = (node_t *) arg2;
1343 while (hook_point->deeper)
1344 hook_point = hook_point->deeper;
1346 hook_point->deeper = np2;
1350 YYSTYPE addpeer (YYSTYPE arg1, YYSTYPE arg2)
1352 node_t *np1 = (node_t *) arg1;
1353 node_t *np2 = (node_t *) arg2;
1358 while (hook_point->peer)
1359 hook_point = hook_point->peer;
1361 hook_point->peer = np2;
1366 * add_slist (stmt_list, stmt)
1369 YYSTYPE add_slist (YYSTYPE a1, YYSTYPE a2)
1372 return (addpeer(a1, a2));
1380 * add_define (char *name, defn_list);
1382 YYSTYPE add_define (YYSTYPE a1, YYSTYPE a2)
1386 np = make_node(NODE_DEFINE);
1388 deeper((YYSTYPE)np, a2);
1389 return ((YYSTYPE) np);
1393 * add_defbody (defn_list, new_defn)
1395 YYSTYPE add_defbody (YYSTYPE a1, YYSTYPE a2)
1397 return (addpeer(a1, a2));
1401 * add_primtype ([packed], primitive type, instance)
1404 YYSTYPE add_primtype (YYSTYPE a1, YYSTYPE a2, YYSTYPE a3)
1410 /* Hook instance to type node */
1419 * add_complex(char *type_name, instance)
1422 YYSTYPE add_complex (YYSTYPE a1, YYSTYPE a2)
1426 np = make_node(NODE_COMPLEX);
1427 np->data[0] = (void *) a1;
1429 deeper((YYSTYPE)np, a2);
1430 return ((YYSTYPE) np);
1434 * add_union(char *type_name, definition)
1437 YYSTYPE add_union (YYSTYPE a1, YYSTYPE a2)
1441 np = make_node(NODE_UNION);
1442 np->data[0] = (void *) a1;
1444 deeper((YYSTYPE)np, a2);
1445 return ((YYSTYPE) np);
1450 * add_vector_vbl (node_t *variable, YYSTYPE size)
1453 YYSTYPE add_vector_vbl (YYSTYPE a1, YYSTYPE a2)
1457 np = make_node(NODE_VECTOR);
1458 np->data[0] = (void *) a1;
1459 np->data[1] = (void *) a2;
1460 return ((YYSTYPE) np);
1464 * add_scalar_vbl (char *name)
1466 YYSTYPE add_scalar_vbl (YYSTYPE a1)
1470 np = make_node(NODE_SCALAR);
1471 np->data[0] = (void *) a1;
1472 return ((YYSTYPE) np);
1476 * set_flags (int flags, msg(=0?))
1478 YYSTYPE set_flags(YYSTYPE a1, YYSTYPE a2)
1487 flags = (int)(uword) a1;
1495 YYSTYPE suppress_version (void)
1497 dont_output_version = 1;
1501 void dump(node_t *np)
1506 vftp = the_vft[np->type];
1512 char *fixup_input_filename(void)
1516 cp = (char *)input_filename;
1523 while (cp > input_filename && *cp != '/')
1528 strcpy (tmpbuf, cp);
1537 while (cp > tmpbuf && *cp != '.')
1543 return (sxerox(tmpbuf));
1546 void generate_top_boilerplate(FILE *fp)
1549 char *datestring = ctime(&starttime);
1550 fixed_name = fixup_input_filename();
1554 fprintf (fp, "/*\n");
1555 fprintf (fp, " * VLIB API definitions %s\n", datestring);
1556 fprintf (fp, " * Input file: %s\n", input_filename);
1557 fprintf (fp, " * Automatically generated: please edit the input file ");
1558 fprintf (fp, "NOT this file!\n");
1560 /* Moron Acme trigger workaround */
1561 fprintf (fp, " * %syright (c) %s by Cisco Systems, Inc.\n", "Cop",
1563 fprintf (fp, " */\n\n");
1564 fprintf (fp, "#if defined(vl_msg_id)||defined(vl_union_id)||");
1565 fprintf (fp, "defined(vl_printfun) \\\n ||defined(vl_endianfun)||");
1566 fprintf (fp, " defined(vl_api_version)||defined(vl_typedefs) \\\n");
1567 fprintf (fp, " ||defined(vl_msg_name)\n");
1568 fprintf (fp, "/* ok, something was selected */\n");
1569 fprintf (fp, "#else\n");
1570 fprintf (fp, "#warning no content included from %s\n", input_filename);
1571 fprintf (fp, "#endif\n\n");
1572 fprintf (fp, "#define VL_API_PACKED(x) x __attribute__ ((packed))\n\n");
1575 void generate_bottom_boilerplate(FILE *fp)
1578 fprintf (fp, "\n#ifdef vl_api_version\n");
1580 if (dont_output_version) {
1581 fprintf (fp, "/* WARNING: API FILE VERSION CHECK DISABLED */\n");
1585 fprintf (fp, "vl_api_version(%s, 0x%08x)\n\n",
1586 fixed_name, (unsigned int)input_crc);
1587 fprintf (fp, "#endif\n\n");
1590 void generate_msg_ids(YYSTYPE a1, FILE *fp)
1592 node_t *np = (node_t *)a1;
1594 fprintf (fp, "\n/****** Message ID / handler enum ******/\n\n");
1595 fprintf (fp, "#ifdef vl_msg_id\n");
1598 if (np->type == NODE_DEFINE) {
1599 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1600 fprintf (fp, "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n",
1601 uppercase(np->data[0]), (i8 *)np->data[0]);
1603 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1608 fprintf (fp, "#endif\n");
1612 void generate_msg_names(YYSTYPE a1, FILE *fp)
1614 node_t *np = (node_t *)a1;
1616 fprintf (fp, "\n/****** Message names ******/\n\n");
1618 fprintf (fp, "#ifdef vl_msg_name\n");
1621 if (np->type == NODE_DEFINE) {
1622 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1623 fprintf (fp, "vl_msg_name(vl_api_%s_t, %d)\n",
1625 (np->flags & NODE_FLAG_DONT_TRACE ? 0 : 1));
1627 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1632 fprintf (fp, "#endif\n\n");
1635 void generate_typedefs(YYSTYPE a1, FILE *fp)
1637 node_t *np = (node_t *)a1;
1640 fprintf(fp, "\n/****** Typedefs *****/\n\n");
1641 fprintf(fp, "#ifdef vl_typedefs\n\n");
1643 /* Walk the top-level node-list */
1645 if (np->type == NODE_DEFINE) {
1646 /* Yeah, this is pedantic */
1647 vftp = the_vft[np->type];
1648 vftp->generate(np, TYPEDEF_PASS, fp);
1652 fprintf(fp, "#endif /* vl_typedefs */\n\n");
1655 void union_walk_one_defn(node_t *np, FILE *fp)
1660 /* Walk the list of typed objects in this msg def */
1662 if (np->type == NODE_UNION) {
1663 current_union_name = np->data[0];
1666 /* Walk the list of objects in this union */
1668 vblp = uelem->deeper;
1669 /* Drill down on each element, find the variable name */
1671 if (vblp->type == NODE_SCALAR ||
1672 vblp->type == NODE_VECTOR ||
1673 vblp->type == NODE_COMPLEX) {
1674 fprintf(ofp, "#define %s_",
1675 uppercase(current_def_name));
1676 fprintf(ofp, "%s_", uppercase(current_union_name));
1677 fprintf(ofp, "%s %d\n",uppercase(vblp->data[0]),
1682 vblp = vblp->deeper;
1684 uelem = uelem->peer;
1686 current_union_name = 0;
1693 void generate_uniondefs(YYSTYPE a1, FILE *fp)
1695 node_t *np = (node_t *)a1;
1697 fprintf(fp, "/****** Discriminated Union Definitions *****/\n\n");
1698 fprintf(fp, "#ifdef vl_union_id\n\n");
1700 /* Walk the top-level node-list */
1702 if (np->type == NODE_DEFINE) {
1704 current_def_name = np->data[0];
1705 union_walk_one_defn(np->deeper, fp);
1709 fprintf(fp, "\n#endif /* vl_union_id */\n\n");
1712 void generate_printfun(YYSTYPE a1, FILE *fp)
1714 node_t *np = (node_t *)a1;
1717 fprintf(fp, "/****** Print functions *****/\n\n");
1718 fprintf(fp, "#ifdef vl_printfun\n\n");
1720 fprintf(fp, "#ifdef LP64\n");
1721 fputs ("#define _uword_fmt \"%lld\"\n", fp);
1722 fputs ("#define _uword_cast (long long)\n", fp);
1723 fprintf(fp, "#else\n");
1724 fputs("#define _uword_fmt \"%ld\"\n", fp);
1725 fputs ("#define _uword_cast long\n", fp);
1726 fprintf(fp, "#endif\n\n");
1728 /* Walk the top-level node-list */
1730 if (np->type == NODE_DEFINE) {
1731 if (!(np->flags & NODE_FLAG_MANUAL_PRINT)) {
1733 "static inline void *vl_api_%s_t_print (vl_api_%s_t *a,",
1734 (i8 *)np->data[0], (i8 *) np->data[0]);
1735 fprintf(fp, "void *handle)\n{\n");
1736 /* output the message name */
1738 " vl_print(handle, \"vl_api_%s_t:\\n\");\n",
1742 /* Yeah, this is pedantic */
1743 vftp = the_vft[np->type];
1744 vftp->generate(np, PRINTFUN_PASS, fp);
1745 fprintf(fp, " return handle;\n");
1746 fprintf(fp, "}\n\n");
1749 fprintf(fp, "/***** manual: vl_api_%s_t_print *****/\n\n",
1750 (i8 *) np->data[0]);
1755 fprintf(fp, "#endif /* vl_printfun */\n\n");
1758 void generate_endianfun(YYSTYPE a1, FILE *fp)
1760 node_t *np = (node_t *)a1;
1763 fprintf(fp, "\n/****** Endian swap functions *****/\n\n");
1764 fprintf(fp, "#ifdef vl_endianfun\n\n");
1765 fprintf(fp, "#undef clib_net_to_host_uword\n");
1766 fprintf(fp, "#ifdef LP64\n");
1767 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u64\n");
1768 fprintf(fp, "#else\n");
1769 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u32\n");
1770 fprintf(fp, "#endif\n\n");
1772 /* Walk the top-level node-list */
1774 if (np->type == NODE_DEFINE) {
1775 if (!(np->flags & NODE_FLAG_MANUAL_ENDIAN)) {
1777 "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)\n{\n",
1778 (i8 *) np->data[0], (i8 *) np->data[0]);
1780 /* Yeah, this is pedantic */
1781 vftp = the_vft[np->type];
1782 vftp->generate(np, ENDIANFUN_PASS, fp);
1783 fprintf(fp, "}\n\n");
1786 fprintf(fp, "/***** manual: vl_api_%s_t_endian *****/\n\n",
1787 (i8 *) np->data[0]);
1792 fprintf(fp, "#endif /* vl_endianfun */\n\n");
1795 void add_msg_ids(YYSTYPE a1)
1797 node_t *np = (node_t *)a1;
1801 /* Walk the top-level node-list */
1803 if (np->type == NODE_DEFINE) {
1804 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1805 /* add the parse tree for "u16 _vl_msg_id" */
1806 new_u16 = make_node(NODE_U16);
1807 new_u16->peer = np->deeper;
1808 np->deeper = new_u16;
1809 new_vbl = make_node(NODE_SCALAR);
1810 new_vbl->data[0] = sxerox("_vl_msg_id");
1811 new_u16->deeper = new_vbl;
1818 void generate_java_top_boilerplate(FILE *fp)
1821 char *datestring = ctime(&starttime);
1822 fixed_name = fixup_input_filename();
1826 fprintf (fp, "/*\n");
1827 fprintf (fp, " * VLIB API java binding %s\n", datestring);
1828 fprintf (fp, " * Input file: %s\n", input_filename);
1829 fprintf (fp, " * Automatically generated: please edit the input file ");
1830 fprintf (fp, "NOT this file!\n");
1831 fprintf (fp, " */\n\n");
1833 fprintf (fp, "package org.openvpp.vppjapi;\n\n");
1834 fprintf (fp, "import java.io.IOException;\n\n");
1835 fprintf (fp, "public class %s extends vppConn {\n",
1837 fprintf (fp, " public %s(String clientName) throws IOException {\n", java_class);
1838 fprintf (fp, " super(clientName);\n");
1839 fprintf (fp, " }\n\n");
1842 void generate_java_bottom_boilerplate(FILE *fp)
1844 fprintf (fp, "}\n");
1848 void generate_java_class_definition (YYSTYPE a1, FILE *fp)
1850 node_t *np = (node_t *)a1;
1853 fprintf(fp, "/****** API methods *****/\n\n");
1855 /* Walk the top-level node-list */
1857 if (np->type == NODE_DEFINE) {
1858 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1859 /* Suppress messages named "xyz_reply" */
1860 char * cp = (char *) np->data[0];
1864 if (strncmp (cp, "_reply", 6)) {
1865 current_java_parameter_number = 0;
1866 vftp = the_vft[np->type];
1867 vftp->generate(np, JAVA_METHOD_PASS, fp);
1874 fprintf(fp, "\n/****** end of API methods *****/\n");
1877 void generate_jni_reply_handler_list (YYSTYPE a1, FILE *fp)
1879 node_t *np = (node_t *)a1;
1882 fprintf (fp, "#define foreach_api_reply_handler \\\n");
1884 /* Walk the top-level node-list */
1886 if (np->type == NODE_DEFINE) {
1887 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1888 /* emit messages named "xyz_reply" */
1889 char * cp = (char *) np->data[0];
1893 if (!strncmp (cp, "_reply", 6)) {
1894 fprintf (fp, "_(%s, %s) \\\n",
1895 uppercase(np->data[0]), (char *)(np->data[0]));
1902 fprintf (fp, "\n\n");
1905 char * m_macro_boilerplate =
1906 "#define M(T,t) \\\n"
1908 " api_result_ready = 0; \\\n"
1909 " mp = vl_msg_api_alloc(sizeof(*mp)); \\\n"
1910 " memset (mp, 0, sizeof (*mp)); \\\n"
1911 " mp->_vl_msg_id = ntohs (VL_API_##T); \\\n"
1912 " mp->client_index = api_main.my_client_index; \\\n"
1914 "#define M2(T,t,n) \\\n"
1916 " api_result_ready = 0; \\\n"
1917 " mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \\\n"
1918 " memset (mp, 0, sizeof (*mp)); \\\n"
1919 " mp->_vl_msg_id = ntohs (VL_API_##T); \\\n"
1920 " mp->client_index = api_main.my_client_index; \\\n"
1923 char * s_macro_boilerplate =
1924 "#define S (vl_msg_api_send_shmem (api_main.shmem_hdr->vl_input_queue, \\\n"
1927 char * w_macro_boilerplate =
1930 " timeout = clib_time_now (&clib_time) + 1.0; \\\n"
1932 " while (clib_time_now (&clib_time) < timeout) { \\\n"
1933 " if (api_result_ready == 1) { \\\n"
1934 " return ((jint) api_result); \\\n"
1940 void generate_jni_top_boilerplate(FILE *fp)
1943 char *datestring = ctime(&starttime);
1944 fixed_name = fixup_input_filename();
1948 fprintf (fp, "/*\n");
1949 fprintf (fp, " * VLIB Java native code %s\n", datestring);
1950 fprintf (fp, " * Input file: %s\n", input_filename);
1951 fprintf (fp, " * Automatically generated: please edit the input file ");
1952 fprintf (fp, "NOT this file!\n");
1953 fprintf (fp, " */\n\n");
1955 fprintf (fp, "#include <japi/vppjni.h>\n");
1958 "#define vl_api_version(n,v) static u32 %s_api_version %s = v;\n",
1959 vlib_app_name, "__attribute__((unused))");
1960 fprintf (fp, "#include <api/%s.api.h>\n", vlib_app_name);
1961 fprintf (fp, "#undef vl_api_version\n\n");
1963 fprintf (fp, "#include <japi/org_openvpp_vppjapi_vppConn.h>\n");
1964 fprintf (fp, "#include <japi/org_openvpp_vppjapi_%s.h>\n\n", java_class);
1966 fprintf (fp, "#include <api/%s_msg_enum.h>\n", vlib_app_name);
1967 fprintf (fp, "#define vl_typedefs /* define message structures */\n");
1968 fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
1969 fprintf (fp, "#undef vl_typedefs\n\n");
1971 fprintf (fp, "#define vl_endianfun \n");
1972 fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
1973 fprintf (fp, "#undef vl_endianfun\n\n");
1975 fprintf (fp, "#define vl_print(handle, ...)\n");
1976 fprintf (fp, "#define vl_printfun\n");
1977 fprintf (fp, "#include <api/%s_all_api_h.h>\n", vlib_app_name);
1978 fprintf (fp, "#undef vl_printfun\n\n");
1981 void generate_jni_code (YYSTYPE a1, FILE *fp)
1983 node_t *np = (node_t *)a1;
1986 /* Walk the top-level node-list */
1988 if (np->type == NODE_DEFINE) {
1989 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1990 /* Suppress messages named "xyz_reply" */
1991 char * cp = (char *) np->data[0];
1995 if (strncmp (cp, "_reply", 6)) {
1996 current_def_name = np->data[0];
1997 current_java_parameter_number = 0;
1998 vftp = the_vft[np->type];
1999 vftp->generate(np, JAVA_JNI_PASS, fp);
2007 char *hookup_boilerplate =
2008 "void vl_msg_reply_handler_hookup (void)\n"
2010 "#define _(N,n) \\\n"
2011 " vl_msg_api_set_handlers (VL_API_##N, #n, \\\n"
2012 " vl_api_generic_reply_handler, \\\n"
2013 " vl_noop_handler, \\\n"
2014 " vl_api_##n##_t_endian, \\\n"
2015 " vl_api_##n##_t_print, \\\n"
2016 " sizeof(vl_api_##n##_t), 1); \n"
2017 " foreach_api_reply_handler;\n"
2021 void generate_jni_bottom_boilerplate(FILE *fp)
2023 fputs (hookup_boilerplate, fp);
2026 void generate_python (YYSTYPE a1, FILE *fp)
2028 node_t *np = (node_t *)a1;
2030 fprintf (fp, "vppapidef = [\n");
2031 /* Walk the top-level node-list */
2033 if (np->type == NODE_DEFINE && !(np->flags & NODE_FLAG_TYPEONLY)) {
2034 /* Yeah, this is pedantic */
2035 vftp = the_vft[np->type];
2036 vftp->generate(np, PYTHON_PASS, fp);
2040 fprintf (fp, "\n]\n");
2043 void generate(YYSTYPE a1)
2052 generate_top_boilerplate(ofp);
2054 generate_msg_ids(a1, ofp);
2055 generate_msg_names(a1, ofp);
2056 generate_typedefs(a1, ofp);
2057 generate_uniondefs(a1, ofp);
2058 generate_printfun(a1, ofp);
2059 generate_endianfun(a1, ofp);
2061 generate_bottom_boilerplate(ofp);
2065 generate_java_top_boilerplate(javafp);
2066 generate_java_class_definition(a1, javafp);
2067 generate_java_bottom_boilerplate(javafp);
2070 generate_jni_top_boilerplate(jnifp);
2071 generate_jni_reply_handler_list (a1, jnifp);
2072 generate_jni_code(a1, jnifp);
2073 generate_jni_bottom_boilerplate(jnifp);
2076 generate_python(a1, pythonfp);