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 *
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;
55 static int current_java_parameter_number;
56 static int current_java_emitted_parameter;
57 static int current_java_parameter_need_comma_space;
58 void *current_java_methodfun;
59 void *current_java_jnifun;
61 void indent_me(FILE *ofp)
65 for (i = 0; i < indent; i++)
69 char *uppercase (char *s)
75 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
76 if (*s >= 'a' && *s <= 'z')
77 *cp++ = *s++ - ('a' - 'A');
85 char *lowercase (char *s)
91 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
92 if (*s >= 'A' && *s <= 'Z')
93 *cp++ = *s++ + ('a' - 'A');
102 * javah maps foo_bar to foo_1bar for whatever freakin' reason
103 * So, adjust java names accordingly.
105 char *java_name_mangle (void * name_arg)
107 char * name = name_arg;
111 vec_reset_length (s);
113 s = format (s, "%s%c", name, 0);
115 for (i = 0; i < vec_len(s); i++)
117 vec_delete (s, 1, i);
118 if (s[i] >= 'a' && s[i] <= 'z')
126 void primtype_recursive_print(node_t *this, i8 *fmt)
128 fputs((char *)fmt, stdout);
131 node_vft_t *vftp = the_vft[this->deeper->type];
132 vftp->print(this->deeper);
136 void primtype_recursive_generate(node_t *this, enum passid which, FILE *ofp,
137 i8 *type_name, i8 *type_fmt, i8 *type_cast)
141 current_type_name = (char *)type_name;
142 current_type_cast = (char *)type_cast;
146 fputs((char *)type_name, ofp);
151 current_type_fmt = (char *)type_fmt;
155 vftp = the_vft[this->type];
156 current_endianfun = vftp->endian_converter;
159 case JAVA_METHOD_PASS:
160 vftp = the_vft[this->type];
161 current_java_methodfun = vftp->java_method_function;
165 fprintf(stderr, "primtype_recursive_generate: unimp pass %d\n", which);
170 vftp = the_vft[this->deeper->type];
171 vftp->generate(this->deeper, which, ofp);
175 void primtype_java_method (node_t * this, enum passid which, FILE *ofp,
176 char *java_type_name)
180 deeper = this->deeper;
182 /* We'll take care of _msg_id, client_index, and context ourselves */
183 if (current_java_parameter_number++ < 3) {
184 if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
186 else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
188 else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
192 if (deeper->type == NODE_SCALAR)
193 fprintf (ofp, "%s %s", java_type_name,
194 java_name_mangle(deeper->data[0]));
196 fprintf (ofp, "%s [] %s", java_type_name,
197 java_name_mangle(deeper->data[0]));
199 current_java_emitted_parameter = 1;
202 void primtype_java_parameter (node_t * this, enum passid which, FILE *ofp,
203 char *java_type_name)
207 deeper = this->deeper;
209 /* We'll take care of _msg_id, client_index, and context ourselves */
210 if (current_java_parameter_number++ < 3) {
211 if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
213 else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
215 else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
218 if (current_java_parameter_need_comma_space) {
219 current_java_parameter_need_comma_space = 0;
223 if (deeper->type == NODE_SCALAR)
224 fprintf (ofp, "%s %s", java_type_name, (char *)(deeper->data[0]));
226 fprintf (ofp, "%sArray %s", java_type_name, (char *)(deeper->data[0]));
228 current_java_emitted_parameter = 1;
231 void primtype_java_setup (node_t * this, enum passid which, FILE *ofp,
232 char *java_type_name, char *array_element_name)
236 deeper = this->deeper;
238 /* We'll take care of _msg_id, client_index, and context ourselves */
239 if (current_java_parameter_number++ < 3) {
240 if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
242 else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
244 else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
248 if (deeper->type == NODE_VECTOR) {
251 "%s * %sP = (*env)->Get%sArrayElements (env, %s, NULL);\n",
252 java_type_name, (char *)(deeper->data[0]),
253 array_element_name, (char *)(deeper->data[0]));
256 current_java_emitted_parameter = 1;
259 void primtype_java_code (node_t * this, enum passid which, FILE *ofp,
260 char *java_type_name, char * swapper)
265 deeper = this->deeper;
267 /* We'll take care of _msg_id, client_index, and context ourselves */
268 if (current_java_parameter_number++ < 3) {
269 if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
271 else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
273 else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
279 s = (char *)(deeper->data[0]);
282 if (deeper->type == NODE_VECTOR)
283 fprintf (ofp, "memcpy (mp->%s, %sP, sizeof (mp->%s));\n",
286 fprintf (ofp, "mp->%s = %s;\n", s, s);
288 if (deeper->type == NODE_VECTOR) {
292 fprintf(ofp, "int _i;\n");
294 fprintf(ofp, "for (_i = 0; _i < %d; _i++) {\n",
295 (int)(u64)(deeper->data[1]));
298 fprintf(ofp, "mp->%s[_i] = %s(%sP[_i]);\n",
307 fprintf (ofp, "mp->%s = %s(%s);\n", s, swapper, s);
311 current_java_emitted_parameter = 1;
314 void primtype_java_teardown (node_t * this, enum passid which, FILE *ofp,
315 char * array_element_name)
319 deeper = this->deeper;
321 /* We'll take care of _msg_id, client_index, and context ourselves */
322 if (current_java_parameter_number++ < 3) {
323 if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
325 else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
327 else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
331 if (deeper->type == NODE_VECTOR) {
334 "(*env)->Release%sArrayElements (env, %s, %sP, 0);\n",
336 (char *)(deeper->data[0]),
337 (char *)(deeper->data[0]));
340 current_java_emitted_parameter = 1;
343 void node_illegal_print (node_t *this)
345 fprintf(stderr, "node_illegal_print called\n");
349 void node_illegal_generate (node_t *this, enum passid notused, FILE *ofp)
351 fprintf(stderr, "node_illegal_generate called\n");
355 void node_illegal_java_method (node_t *this, enum passid notused, FILE *ofp)
357 fprintf(stderr, "node_illegal_java_method called\n");
361 void node_illegal_java_jni (node_t *this, enum passid notused, FILE *ofp)
363 fprintf(stderr, "node_illegal_java_jni called\n");
367 node_vft_t node_illegal_vft = {
369 node_illegal_generate,
371 node_illegal_java_method,
372 node_illegal_java_jni,
375 void node_u8_print (node_t *this)
377 primtype_recursive_print(this, "u8 ");
380 void node_u8_generate (node_t *this, enum passid which, FILE *ofp)
382 primtype_recursive_generate(this, which, ofp, "u8", "%u", "(unsigned)");
385 void node_u8_java_method (node_t *this, enum passid which, FILE *ofp)
387 primtype_java_method (this, which, ofp, "byte");
390 void node_u8_java_parameter (node_t *this, enum passid which, FILE *ofp)
392 primtype_java_parameter (this, which, ofp, "jbyte");
395 void node_u8_java_setup (node_t *this, enum passid which, FILE *ofp)
397 primtype_java_setup (this, which, ofp, "jbyte", "Byte");
400 void node_u8_java_code (node_t *this, enum passid which, FILE *ofp)
402 primtype_java_code (this, which, ofp, "jbyte", 0 /* swapper */);
405 void node_u8_java_teardown (node_t *this, enum passid which, FILE *ofp)
407 primtype_java_teardown (this, which, ofp, "Byte");
410 node_vft_t node_u8_vft = {
415 node_u8_java_parameter,
418 node_u8_java_teardown,
421 void node_u16_print (node_t *this)
423 primtype_recursive_print(this, "u16 ");
426 void node_u16_generate (node_t *this, enum passid which, FILE *ofp)
428 primtype_recursive_generate(this, which, ofp, "u16", "%u", "(unsigned)");
431 void node_u16_java_method (node_t *this, enum passid which, FILE *ofp)
433 primtype_java_method (this, which, ofp, "short");
436 void node_u16_java_parameter (node_t *this, enum passid which, FILE *ofp)
438 primtype_java_parameter (this, which, ofp, "jshort");
441 void node_u16_java_setup (node_t *this, enum passid which, FILE *ofp)
443 primtype_java_setup (this, which, ofp, "jshort", "Short");
446 void node_u16_java_code (node_t *this, enum passid which, FILE *ofp)
448 primtype_java_code (this, which, ofp, "jshort", "clib_host_to_net_u16");
451 void node_u16_java_teardown (node_t *this, enum passid which, FILE *ofp)
453 primtype_java_teardown (this, which, ofp, "Short");
456 node_vft_t node_u16_vft = {
459 "clib_net_to_host_u16",
460 node_u16_java_method,
461 node_u16_java_parameter,
464 node_u16_java_teardown,
467 void node_u32_print (node_t *this)
469 primtype_recursive_print(this, "u32 ");
472 void node_u32_generate (node_t *this, enum passid which, FILE *ofp)
474 primtype_recursive_generate(this, which, ofp, "u32", "%u", "(unsigned)");
477 void node_u32_java_method (node_t *this, enum passid which, FILE *ofp)
479 primtype_java_method (this, which, ofp, "int");
482 void node_u32_java_parameter (node_t *this, enum passid which, FILE *ofp)
484 primtype_java_parameter (this, which, ofp, "jint");
487 void node_u32_java_setup (node_t *this, enum passid which, FILE *ofp)
489 primtype_java_setup (this, which, ofp, "jint", "Int");
492 void node_u32_java_code (node_t *this, enum passid which, FILE *ofp)
494 primtype_java_code (this, which, ofp, "jint", "clib_host_to_net_u32");
497 void node_u32_java_teardown (node_t *this, enum passid which, FILE *ofp)
499 primtype_java_teardown (this, which, ofp, "Int");
502 node_vft_t node_u32_vft = {
505 "clib_net_to_host_u32",
506 node_u32_java_method,
507 node_u32_java_parameter,
510 node_u32_java_teardown,
513 void node_u64_print (node_t *this)
515 primtype_recursive_print(this, "u64 ");
518 void node_u64_generate (node_t *this, enum passid which, FILE *ofp)
520 primtype_recursive_generate(this, which, ofp, "u64", "%llu",
524 void node_u64_java_method (node_t *this, enum passid which, FILE *ofp)
526 primtype_java_method (this, which, ofp, "long");
529 void node_u64_java_parameter (node_t *this, enum passid which, FILE *ofp)
531 primtype_java_parameter (this, which, ofp, "jlong");
534 void node_u64_java_setup (node_t *this, enum passid which, FILE *ofp)
536 primtype_java_setup (this, which, ofp, "jlong", "Long");
539 void node_u64_java_code (node_t *this, enum passid which, FILE *ofp)
541 primtype_java_code (this, which, ofp, "jlong", "clib_host_to_net_u64");
544 void node_u64_java_teardown (node_t *this, enum passid which, FILE *ofp)
546 primtype_java_teardown (this, which, ofp, "Long");
549 node_vft_t node_u64_vft = {
552 "clib_net_to_host_u64",
553 node_u64_java_method,
554 node_u64_java_parameter,
557 node_u64_java_teardown,
560 void node_i8_print (node_t *this)
562 primtype_recursive_print(this, "i8 ");
565 void node_i8_generate (node_t *this, enum passid which, FILE *ofp)
567 primtype_recursive_generate(this, which, ofp, "i8", "%d", "(int)");
570 node_vft_t node_i8_vft = {
575 node_u8_java_parameter,
578 node_u8_java_teardown,
581 void node_i16_print (node_t *this)
583 primtype_recursive_print(this, "i16 ");
586 void node_i16_generate (node_t *this, enum passid which, FILE *ofp)
588 primtype_recursive_generate(this, which, ofp, "i16", "%d", "(int)");
591 node_vft_t node_i16_vft = {
594 "clib_net_to_host_u16",
595 node_u16_java_method,
596 node_u16_java_parameter,
599 node_u16_java_teardown,
602 void node_i32_print (node_t *this)
604 primtype_recursive_print(this, "i32 ");
607 void node_i32_generate (node_t *this, enum passid which, FILE *ofp)
609 primtype_recursive_generate(this, which, ofp, "i32", "%ld", "(long)");
612 node_vft_t node_i32_vft = {
615 "clib_net_to_host_u32",
616 node_u32_java_method,
617 node_u32_java_parameter,
620 node_u32_java_teardown,
623 void node_i64_print (node_t *this)
625 primtype_recursive_print(this, "i64 ");
628 void node_i64_generate (node_t *this, enum passid which, FILE *ofp)
630 primtype_recursive_generate(this, which, ofp, "i64", "%lld",
634 node_vft_t node_i64_vft = {
637 "clib_net_to_host_u64",
638 node_u64_java_method,
639 node_u64_java_parameter,
642 node_u64_java_teardown,
645 void node_f64_print (node_t *this)
647 primtype_recursive_print(this, "f64 ");
650 void node_f64_generate (node_t *this, enum passid which, FILE *ofp)
652 primtype_recursive_generate(this, which, ofp, "f64", "%.2f",
655 void node_f64_java_method (node_t *this, enum passid which, FILE *ofp)
657 primtype_java_method (this, which, ofp, "double");
660 void node_f64_java_parameter (node_t *this, enum passid which, FILE *ofp)
662 primtype_java_parameter (this, which, ofp, "jdouble");
665 void node_f64_java_setup (node_t *this, enum passid which, FILE *ofp)
667 primtype_java_setup (this, which, ofp, "jdouble", "Double");
670 void node_f64_java_code (node_t *this, enum passid which, FILE *ofp)
673 * Current API code doesn't try to endian-swap doubles
674 * FP formats aren't portable yadda yadda yadda
676 primtype_java_code (this, which, ofp, "jdouble", 0 /* $$$ */);
679 void node_f64_java_teardown (node_t *this, enum passid which, FILE *ofp)
681 primtype_java_teardown (this, which, ofp, "Double");
684 node_vft_t node_f64_vft = {
687 " ", /* FP numbers are sent in host byte order */
688 node_f64_java_method,
689 node_f64_java_parameter,
692 node_f64_java_teardown,
696 void node_packed_print (node_t *this)
698 primtype_recursive_print (this, "packed ");
701 void node_packed_generate (node_t *this, enum passid which, FILE *ofp)
703 primtype_recursive_generate(this, which, ofp, "PACKED", "", "");
706 node_vft_t node_packed_vft = {
708 node_packed_generate,
712 void node_define_print (node_t *this)
714 fprintf(stdout, "define %s {\n", CDATA0);
716 node_vft_t *vftp = the_vft[this->deeper->type];
717 fprintf(stdout, " ");
718 vftp->print(this->deeper);
720 fprintf(stdout, "};\n");
723 void node_define_generate (node_t *this, enum passid which, FILE *fp)
725 node_t *child, *save_child;
729 fprintf(fp, "typedef VL_API_PACKED(struct _vl_api_%s {\n", CDATA0);
730 child = this->deeper;
733 node_vft_t *vftp = the_vft[child->type];
735 vftp->generate(child, which, fp);
739 fprintf(fp, "}) vl_api_%s_t;\n\n", CDATA0);
744 child = this->deeper;
746 node_vft_t *vftp = the_vft[child->type];
747 vftp->generate(child, which, fp);
752 case JAVA_METHOD_PASS:
755 fprintf (fp, "public native int %s (",
756 java_name_mangle(CDATA0));
757 child = this->deeper;
759 node_vft_t *vftp = the_vft[child->type];
760 current_java_emitted_parameter = 0;
761 vftp->java_method_function(child, which, fp);
763 if (child && current_java_emitted_parameter)
766 fprintf (fp, ");\n");
771 /* Generate function prototype */
772 fprintf (fp, "JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_%s_%s\n",
773 java_class, java_name_mangle(CDATA0));
775 fprintf (fp, "(JNIEnv * env, jobject obj");
776 current_java_parameter_need_comma_space = 1;
777 child = this->deeper;
780 node_vft_t *vftp = the_vft[child->type];
781 current_java_emitted_parameter = 0;
782 vftp->java_jni_parameter(child, which, fp);
784 if (child && current_java_emitted_parameter)
787 fprintf (fp, ")\n{\n");
790 /* define the api message pointer */
792 fprintf (fp, "vppjni_main_t *jm = &vppjni_main;\n");
794 fprintf (fp, "vl_api_%s_t * mp;\n", current_def_name);
796 fprintf (fp, "u32 my_context_id;\n");
798 fprintf (fp, "int rv;\n");
801 fprintf (fp, "rv = vppjni_sanity_check (jm);\n");
803 fprintf (fp, "if (rv) return rv;\n");
806 fprintf (fp, "my_context_id = vppjni_get_context_id (jm);\n");
808 /* Generate array setups, if any */
811 node_vft_t *vftp = the_vft[child->type];
812 current_java_parameter_number = 0;
813 current_java_emitted_parameter = 0;
814 vftp->java_jni_setup(child, which, fp);
818 /* Setup the API message */
820 fprintf (fp, "M(%s, %s);\n", uppercase(current_def_name),
823 fprintf (fp, "mp->context = clib_host_to_net_u32 (my_context_id);\n");
824 /* $$$ Set up context hash table or some such... */
829 node_vft_t *vftp = the_vft[child->type];
830 current_java_parameter_number = 0;
831 current_java_emitted_parameter = 0;
832 vftp->java_jni_code(child, which, fp);
836 /* Generate array teardowns */
839 node_vft_t *vftp = the_vft[child->type];
840 current_java_parameter_number = 0;
841 current_java_emitted_parameter = 0;
842 vftp->java_jni_teardown(child, which, fp);
846 /* Send the message, return context_id */
848 fprintf (fp, "S;\n");
850 fprintf (fp, "return my_context_id;\n");
853 fprintf (fp, "}\n\n");
857 fprintf(stderr, "node_define_generate: unimp pass %d\n", which);
862 node_vft_t node_define_vft = {
864 node_define_generate,
868 void node_union_print (node_t *this)
870 primtype_recursive_print (this, "union ");
873 void node_union_generate (node_t *this, enum passid which, FILE *fp)
881 fprintf(fp, "u8 _%s_which;\n", CDATA0);
883 fprintf(fp, "union _%s {\n", CDATA0);
884 child = this->deeper;
888 node_vft_t *vftp = the_vft[child->type];
890 vftp->generate(child, which, fp);
895 fprintf(fp, "} %s;\n", CDATA0);
900 uelem = this->deeper;
903 fprintf(fp, "switch(a->_%s_which) {\n",
906 current_union_name = CDATA0;
908 /* Walk the list of objects in this union */
910 node_vft_t *vftp = the_vft[uelem->type];
913 fprintf(fp, "case %d:\n", case_id);
916 /* Drill down on each element */
917 vftp->generate(uelem, which, fp);
919 fprintf(fp, "break;\n");
922 current_union_name = 0;
925 fprintf(fp, "default:\n");
928 if (which == PRINTFUN_PASS) {
930 "vl_print(handle, \"WARNING: _%s_which not set.\\n\");\n",
934 fprintf(fp, "break;\n");
941 fprintf(stderr, "node_union_generate: unimp pass %d\n", which);
947 node_vft_t node_union_vft = {
953 void node_scalar_print (node_t *this)
955 fprintf(stdout, "%s", CDATA0);
956 primtype_recursive_print (this, "");
959 void node_scalar_generate (node_t *this, enum passid which, FILE *fp)
961 char *union_prefix = "";
963 if (current_union_name) {
964 sprintf(tmpbuf, "%s.", current_union_name);
965 union_prefix = tmpbuf;
970 fprintf(fp, "%s;\n", CDATA0);
975 if (current_is_complex) {
976 fprintf(fp, "vl_api_%s_t_print(a->%s%s, handle);\n",
977 current_type_name, union_prefix, CDATA0);
979 if (!strcmp(current_type_fmt, "uword")) {
981 "vl_print(handle, \"%s%s: \" _uword_fmt \"\\n\", %s a->%s%s);\n",
982 union_prefix, CDATA0, "(_uword_cast)",
983 union_prefix, CDATA0);
986 "vl_print(handle, \"%s%s: %s\\n\", %s a->%s%s);\n",
987 union_prefix, CDATA0,
988 current_type_fmt, current_type_cast,
989 union_prefix, CDATA0);
996 if (current_is_complex) {
997 fprintf(fp, "vl_api%s_t_endian(a->%s%s);\n",
998 current_type_name, union_prefix, CDATA0);
1000 /* Current_endianfun == NULL means e.g. it's a u8... */
1001 if (current_endianfun) {
1002 fprintf(fp, "a->%s%s = %s(a->%s%s);\n", union_prefix,
1003 CDATA0, current_endianfun,
1004 union_prefix, CDATA0);
1006 fprintf(fp, "/* a->%s%s = a->%s%s */\n",
1007 union_prefix, CDATA0,
1008 union_prefix, CDATA0);
1014 fprintf(stderr, "node_scalar_generate: unimp pass %d\n", which);
1017 fprintf(stderr, "broken recursion in node_scalar_generate\n");
1022 node_vft_t node_scalar_vft = {
1024 node_scalar_generate,
1028 void node_vector_print (node_t *this)
1030 primtype_recursive_print (this, "vector ");
1033 void node_vector_generate (node_t *this, enum passid which, FILE *fp)
1035 char *union_prefix = "";
1037 if (current_union_name) {
1038 sprintf(tmpbuf, "%s.", current_union_name);
1039 union_prefix = tmpbuf;
1044 fprintf(fp, "%s[%d];\n", CDATA0, IDATA1);
1048 /* Don't bother about "u8 data [0];" et al. */
1056 fprintf(fp, "int _i;\n");
1058 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
1062 if (current_is_complex) {
1063 fprintf(fp, "vl_print(handle, \"%s%s[%%d]: ",
1064 union_prefix, CDATA0);
1066 "vl_print_%s (handle, a->%s%s[_i]);\n",
1067 CDATA0, union_prefix, CDATA0);
1070 "vl_print(handle, \"%s%s[%%d]: %s\\n\", _i, a->%s%s[_i]);\n",
1071 union_prefix, CDATA0,
1073 union_prefix, CDATA0);
1083 case ENDIANFUN_PASS:
1084 /* Don't bother about "u8 data [0];" et al. */
1092 fprintf(fp, "int _i;\n");
1094 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
1098 if (current_is_complex) {
1100 "vl_api_%s_t_endian (a->%s%s[_i]);\n",
1101 current_type_name, union_prefix, CDATA0);
1104 "a->%s%s[_i] = %s(a->%s%s[_i]);\n",
1105 union_prefix, CDATA0,
1107 union_prefix, CDATA0);
1118 fprintf(stderr, "node_vector_generate: unimp pass %d\n", which);
1121 fprintf(stderr, "broken recursion in node_vector_generate\n");
1125 node_vft_t node_vector_vft = {
1127 node_vector_generate,
1131 void node_complex_print (node_t *this)
1133 primtype_recursive_print (this, "complex ");
1136 void node_complex_generate (node_t *this, enum passid which, FILE *fp)
1140 char *member_name = "broken!";
1141 char *union_prefix = "";
1143 if (current_union_name) {
1144 sprintf(tmpbuf, "%s.", current_union_name);
1145 union_prefix = tmpbuf;
1148 current_is_complex++;
1152 fprintf(fp, "%s ", CDATA0);
1153 deeper = this->deeper;
1155 vftp = the_vft[deeper->type];
1156 vftp->generate(deeper, which, fp);
1161 deeper = this->deeper;
1163 if (deeper->type == NODE_SCALAR ||
1164 deeper->type == NODE_VECTOR) {
1165 member_name = deeper->data[0];
1168 deeper = deeper->deeper;
1171 fprintf(fp, "vl_print(handle, \"%s%s ----- \\n\");\n",
1172 union_prefix, member_name);
1174 fprintf(fp, "%s_print(&a->%s%s, handle);\n",
1175 CDATA0, union_prefix, member_name);
1177 fprintf(fp, "vl_print(handle, \"%s%s ----- END \\n\");\n",
1178 union_prefix, member_name);
1181 case ENDIANFUN_PASS:
1182 deeper = this->deeper;
1184 if (deeper->type == NODE_SCALAR ||
1185 deeper->type == NODE_VECTOR) {
1186 member_name = deeper->data[0];
1189 deeper = deeper->deeper;
1193 fprintf(fp, "%s_endian(&a->%s%s);\n",
1194 CDATA0, union_prefix, member_name);
1198 fprintf(stderr, "node_complex_generate unimp pass %d...\n", which);
1201 current_is_complex--;
1204 node_vft_t node_complex_vft = {
1206 node_complex_generate,
1210 void node_noversion_print (node_t *this)
1212 primtype_recursive_print (this, "noversion ");
1215 void node_noversion_generate (node_t *this, enum passid which, FILE *ofp)
1217 fprintf(stderr, "node_noversion_generate called...\n");
1220 node_vft_t node_noversion_vft = {
1221 node_noversion_print,
1222 node_noversion_generate,
1226 void node_uword_print (node_t *this)
1228 primtype_recursive_print(this, "uword ");
1231 void node_uword_generate (node_t *this, enum passid which, FILE *ofp)
1233 primtype_recursive_generate(this, which, ofp, "uword", "uword", "");
1236 node_vft_t node_uword_vft = {
1238 node_uword_generate,
1239 "clib_net_to_host_uword",
1242 node_vft_t *the_vft[NODE_N_TYPES] = {
1259 &node_noversion_vft,
1263 void *make_node (enum node_subclass type)
1267 rv = (node_t *) malloc (sizeof (*rv));
1269 fprintf (stderr, "fatal: make_node out of memory\n");
1272 bzero (rv, sizeof (*rv));
1274 return ((void *) rv);
1277 YYSTYPE deeper (YYSTYPE arg1, YYSTYPE arg2)
1279 node_t *np1 = (node_t *) arg1;
1280 node_t *np2 = (node_t *) arg2;
1285 while (hook_point->deeper)
1286 hook_point = hook_point->deeper;
1288 hook_point->deeper = np2;
1292 YYSTYPE addpeer (YYSTYPE arg1, YYSTYPE arg2)
1294 node_t *np1 = (node_t *) arg1;
1295 node_t *np2 = (node_t *) arg2;
1300 while (hook_point->peer)
1301 hook_point = hook_point->peer;
1303 hook_point->peer = np2;
1308 * add_slist (stmt_list, stmt)
1311 YYSTYPE add_slist (YYSTYPE a1, YYSTYPE a2)
1314 return (addpeer(a1, a2));
1322 * add_define (char *name, defn_list);
1324 YYSTYPE add_define (YYSTYPE a1, YYSTYPE a2)
1328 np = make_node(NODE_DEFINE);
1330 deeper((YYSTYPE)np, a2);
1331 return ((YYSTYPE) np);
1335 * add_defbody (defn_list, new_defn)
1337 YYSTYPE add_defbody (YYSTYPE a1, YYSTYPE a2)
1339 return (addpeer(a1, a2));
1343 * add_primtype ([packed], primitive type, instance)
1346 YYSTYPE add_primtype (YYSTYPE a1, YYSTYPE a2, YYSTYPE a3)
1352 /* Hook instance to type node */
1361 * add_complex(char *type_name, instance)
1364 YYSTYPE add_complex (YYSTYPE a1, YYSTYPE a2)
1368 np = make_node(NODE_COMPLEX);
1369 np->data[0] = (void *) a1;
1371 deeper((YYSTYPE)np, a2);
1372 return ((YYSTYPE) np);
1376 * add_union(char *type_name, definition)
1379 YYSTYPE add_union (YYSTYPE a1, YYSTYPE a2)
1383 np = make_node(NODE_UNION);
1384 np->data[0] = (void *) a1;
1386 deeper((YYSTYPE)np, a2);
1387 return ((YYSTYPE) np);
1392 * add_vector_vbl (node_t *variable, YYSTYPE size)
1395 YYSTYPE add_vector_vbl (YYSTYPE a1, YYSTYPE a2)
1399 np = make_node(NODE_VECTOR);
1400 np->data[0] = (void *) a1;
1401 np->data[1] = (void *) a2;
1402 return ((YYSTYPE) np);
1406 * add_scalar_vbl (char *name)
1408 YYSTYPE add_scalar_vbl (YYSTYPE a1)
1412 np = make_node(NODE_SCALAR);
1413 np->data[0] = (void *) a1;
1414 return ((YYSTYPE) np);
1418 * set_flags (int flags, msg(=0?))
1420 YYSTYPE set_flags(YYSTYPE a1, YYSTYPE a2)
1429 flags = (int)(uword) a1;
1437 YYSTYPE suppress_version (void)
1439 dont_output_version = 1;
1443 void dump(node_t *np)
1448 vftp = the_vft[np->type];
1454 char *fixup_input_filename(void)
1458 cp = (char *)input_filename;
1465 while (cp > input_filename && *cp != '/')
1470 strcpy (tmpbuf, cp);
1479 while (cp > tmpbuf && *cp != '.')
1485 return (sxerox(tmpbuf));
1488 void generate_top_boilerplate(FILE *fp)
1491 char *datestring = ctime(&starttime);
1492 fixed_name = fixup_input_filename();
1496 fprintf (fp, "/*\n");
1497 fprintf (fp, " * VLIB API definitions %s\n", datestring);
1498 fprintf (fp, " * Input file: %s\n", input_filename);
1499 fprintf (fp, " * Automatically generated: please edit the input file ");
1500 fprintf (fp, "NOT this file!\n");
1502 /* Moron Acme trigger workaround */
1503 fprintf (fp, " * %syright (c) %s by Cisco Systems, Inc.\n", "Cop",
1505 fprintf (fp, " */\n\n");
1506 fprintf (fp, "#if defined(vl_msg_id)||defined(vl_union_id)||");
1507 fprintf (fp, "defined(vl_printfun) \\\n ||defined(vl_endianfun)||");
1508 fprintf (fp, " defined(vl_api_version)||defined(vl_typedefs) \\\n");
1509 fprintf (fp, " ||defined(vl_msg_name)\n");
1510 fprintf (fp, "/* ok, something was selected */\n");
1511 fprintf (fp, "#else\n");
1512 fprintf (fp, "#warning no content included from %s\n", input_filename);
1513 fprintf (fp, "#endif\n\n");
1514 fprintf (fp, "#define VL_API_PACKED(x) x __attribute__ ((packed))\n\n");
1517 void generate_bottom_boilerplate(FILE *fp)
1520 fprintf (fp, "\n#ifdef vl_api_version\n");
1522 if (dont_output_version) {
1523 fprintf (fp, "/* WARNING: API FILE VERSION CHECK DISABLED */\n");
1527 fprintf (fp, "vl_api_version(%s, 0x%08x)\n\n",
1528 fixed_name, (unsigned int)input_crc);
1529 fprintf (fp, "#endif\n\n");
1532 void generate_msg_ids(YYSTYPE a1, FILE *fp)
1534 node_t *np = (node_t *)a1;
1536 fprintf (fp, "\n/****** Message ID / handler enum ******/\n\n");
1537 fprintf (fp, "#ifdef vl_msg_id\n");
1540 if (np->type == NODE_DEFINE) {
1541 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1542 fprintf (fp, "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n",
1543 uppercase(np->data[0]), (i8 *)np->data[0]);
1545 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1550 fprintf (fp, "#endif\n");
1554 void generate_msg_names(YYSTYPE a1, FILE *fp)
1556 node_t *np = (node_t *)a1;
1558 fprintf (fp, "\n/****** Message names ******/\n\n");
1560 fprintf (fp, "#ifdef vl_msg_name\n");
1563 if (np->type == NODE_DEFINE) {
1564 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1565 fprintf (fp, "vl_msg_name(vl_api_%s_t, %d)\n",
1567 (np->flags & NODE_FLAG_DONT_TRACE ? 0 : 1));
1569 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1574 fprintf (fp, "#endif\n\n");
1577 void generate_typedefs(YYSTYPE a1, FILE *fp)
1579 node_t *np = (node_t *)a1;
1582 fprintf(fp, "\n/****** Typedefs *****/\n\n");
1583 fprintf(fp, "#ifdef vl_typedefs\n\n");
1585 /* Walk the top-level node-list */
1587 if (np->type == NODE_DEFINE) {
1588 /* Yeah, this is pedantic */
1589 vftp = the_vft[np->type];
1590 vftp->generate(np, TYPEDEF_PASS, fp);
1594 fprintf(fp, "#endif /* vl_typedefs */\n\n");
1597 void union_walk_one_defn(node_t *np, FILE *fp)
1602 /* Walk the list of typed objects in this msg def */
1604 if (np->type == NODE_UNION) {
1605 current_union_name = np->data[0];
1608 /* Walk the list of objects in this union */
1610 vblp = uelem->deeper;
1611 /* Drill down on each element, find the variable name */
1613 if (vblp->type == NODE_SCALAR ||
1614 vblp->type == NODE_VECTOR ||
1615 vblp->type == NODE_COMPLEX) {
1616 fprintf(ofp, "#define %s_",
1617 uppercase(current_def_name));
1618 fprintf(ofp, "%s_", uppercase(current_union_name));
1619 fprintf(ofp, "%s %d\n",uppercase(vblp->data[0]),
1624 vblp = vblp->deeper;
1626 uelem = uelem->peer;
1628 current_union_name = 0;
1635 void generate_uniondefs(YYSTYPE a1, FILE *fp)
1637 node_t *np = (node_t *)a1;
1639 fprintf(fp, "/****** Discriminated Union Definitions *****/\n\n");
1640 fprintf(fp, "#ifdef vl_union_id\n\n");
1642 /* Walk the top-level node-list */
1644 if (np->type == NODE_DEFINE) {
1646 current_def_name = np->data[0];
1647 union_walk_one_defn(np->deeper, fp);
1651 fprintf(fp, "\n#endif /* vl_union_id */\n\n");
1654 void generate_printfun(YYSTYPE a1, FILE *fp)
1656 node_t *np = (node_t *)a1;
1659 fprintf(fp, "/****** Print functions *****/\n\n");
1660 fprintf(fp, "#ifdef vl_printfun\n\n");
1662 fprintf(fp, "#ifdef LP64\n");
1663 fputs ("#define _uword_fmt \"%lld\"\n", fp);
1664 fputs ("#define _uword_cast (long long)\n", fp);
1665 fprintf(fp, "#else\n");
1666 fputs("#define _uword_fmt \"%ld\"\n", fp);
1667 fputs ("#define _uword_cast long\n", fp);
1668 fprintf(fp, "#endif\n\n");
1670 /* Walk the top-level node-list */
1672 if (np->type == NODE_DEFINE) {
1673 if (!(np->flags & NODE_FLAG_MANUAL_PRINT)) {
1675 "static inline void *vl_api_%s_t_print (vl_api_%s_t *a,",
1676 (i8 *)np->data[0], (i8 *) np->data[0]);
1677 fprintf(fp, "void *handle)\n{\n");
1678 /* output the message name */
1680 " vl_print(handle, \"vl_api_%s_t:\\n\");\n",
1684 /* Yeah, this is pedantic */
1685 vftp = the_vft[np->type];
1686 vftp->generate(np, PRINTFUN_PASS, fp);
1687 fprintf(fp, " return handle;\n");
1688 fprintf(fp, "}\n\n");
1691 fprintf(fp, "/***** manual: vl_api_%s_t_print *****/\n\n",
1692 (i8 *) np->data[0]);
1697 fprintf(fp, "#endif /* vl_printfun */\n\n");
1700 void generate_endianfun(YYSTYPE a1, FILE *fp)
1702 node_t *np = (node_t *)a1;
1705 fprintf(fp, "\n/****** Endian swap functions *****/\n\n");
1706 fprintf(fp, "#ifdef vl_endianfun\n\n");
1707 fprintf(fp, "#undef clib_net_to_host_uword\n");
1708 fprintf(fp, "#ifdef LP64\n");
1709 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u64\n");
1710 fprintf(fp, "#else\n");
1711 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u32\n");
1712 fprintf(fp, "#endif\n\n");
1714 /* Walk the top-level node-list */
1716 if (np->type == NODE_DEFINE) {
1717 if (!(np->flags & NODE_FLAG_MANUAL_ENDIAN)) {
1719 "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)\n{\n",
1720 (i8 *) np->data[0], (i8 *) np->data[0]);
1722 /* Yeah, this is pedantic */
1723 vftp = the_vft[np->type];
1724 vftp->generate(np, ENDIANFUN_PASS, fp);
1725 fprintf(fp, "}\n\n");
1728 fprintf(fp, "/***** manual: vl_api_%s_t_endian *****/\n\n",
1729 (i8 *) np->data[0]);
1734 fprintf(fp, "#endif /* vl_endianfun */\n\n");
1737 void add_msg_ids(YYSTYPE a1)
1739 node_t *np = (node_t *)a1;
1743 /* Walk the top-level node-list */
1745 if (np->type == NODE_DEFINE) {
1746 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1747 /* add the parse tree for "u16 _vl_msg_id" */
1748 new_u16 = make_node(NODE_U16);
1749 new_u16->peer = np->deeper;
1750 np->deeper = new_u16;
1751 new_vbl = make_node(NODE_SCALAR);
1752 new_vbl->data[0] = sxerox("_vl_msg_id");
1753 new_u16->deeper = new_vbl;
1760 void generate_java_top_boilerplate(FILE *fp)
1763 char *datestring = ctime(&starttime);
1764 fixed_name = fixup_input_filename();
1768 fprintf (fp, "/*\n");
1769 fprintf (fp, " * VLIB API java binding %s\n", datestring);
1770 fprintf (fp, " * Input file: %s\n", input_filename);
1771 fprintf (fp, " * Automatically generated: please edit the input file ");
1772 fprintf (fp, "NOT this file!\n");
1773 fprintf (fp, " */\n\n");
1775 fprintf (fp, "package org.openvpp.vppjapi;\n\n");
1776 fprintf (fp, "import org.openvpp.vppjapi.vppConn;\n\n");
1777 fprintf (fp, "public class %s extends vppConn {\n\n",
1781 void generate_java_bottom_boilerplate(FILE *fp)
1783 fprintf (fp, "}\n");
1787 void generate_java_class_definition (YYSTYPE a1, FILE *fp)
1789 node_t *np = (node_t *)a1;
1792 fprintf(fp, "/****** API methods *****/\n\n");
1794 /* Walk the top-level node-list */
1796 if (np->type == NODE_DEFINE) {
1797 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1798 /* Suppress messages named "xyz_reply" */
1799 char * cp = (char *) np->data[0];
1803 if (strncmp (cp, "_reply", 6)) {
1804 current_java_parameter_number = 0;
1805 vftp = the_vft[np->type];
1806 vftp->generate(np, JAVA_METHOD_PASS, fp);
1813 fprintf(fp, "\n/****** end of API methods *****/\n");
1816 void generate_jni_reply_handler_list (YYSTYPE a1, FILE *fp)
1818 node_t *np = (node_t *)a1;
1821 fprintf (fp, "#define foreach_api_reply_handler \\\n");
1823 /* Walk the top-level node-list */
1825 if (np->type == NODE_DEFINE) {
1826 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1827 /* emit messages named "xyz_reply" */
1828 char * cp = (char *) np->data[0];
1832 if (!strncmp (cp, "_reply", 6)) {
1833 fprintf (fp, "_(%s, %s) \\\n",
1834 uppercase(np->data[0]), (char *)(np->data[0]));
1841 fprintf (fp, "\n\n");
1844 char * m_macro_boilerplate =
1845 "#define M(T,t) \\\n"
1847 " api_result_ready = 0; \\\n"
1848 " mp = vl_msg_api_alloc(sizeof(*mp)); \\\n"
1849 " memset (mp, 0, sizeof (*mp)); \\\n"
1850 " mp->_vl_msg_id = ntohs (VL_API_##T); \\\n"
1851 " mp->client_index = api_main.my_client_index; \\\n"
1853 "#define M2(T,t,n) \\\n"
1855 " api_result_ready = 0; \\\n"
1856 " mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \\\n"
1857 " memset (mp, 0, sizeof (*mp)); \\\n"
1858 " mp->_vl_msg_id = ntohs (VL_API_##T); \\\n"
1859 " mp->client_index = api_main.my_client_index; \\\n"
1862 char * s_macro_boilerplate =
1863 "#define S (vl_msg_api_send_shmem (api_main.shmem_hdr->vl_input_queue, \\\n"
1866 char * w_macro_boilerplate =
1869 " timeout = clib_time_now (&clib_time) + 1.0; \\\n"
1871 " while (clib_time_now (&clib_time) < timeout) { \\\n"
1872 " if (api_result_ready == 1) { \\\n"
1873 " return ((jint) api_result); \\\n"
1879 void generate_jni_top_boilerplate(FILE *fp)
1882 char *datestring = ctime(&starttime);
1883 fixed_name = fixup_input_filename();
1887 fprintf (fp, "/*\n");
1888 fprintf (fp, " * VLIB Java native code %s\n", datestring);
1889 fprintf (fp, " * Input file: %s\n", input_filename);
1890 fprintf (fp, " * Automatically generated: please edit the input file ");
1891 fprintf (fp, "NOT this file!\n");
1892 fprintf (fp, " */\n\n");
1894 fprintf (fp, "#include <japi/vppjni.h>\n");
1897 "#define vl_api_version(n,v) static u32 %s_api_version %s = v;\n",
1898 vlib_app_name, "__attribute__((unused))");
1899 fprintf (fp, "#include <api/%s.api.h>\n", vlib_app_name);
1900 fprintf (fp, "#undef vl_api_version\n\n");
1902 fprintf (fp, "#include <japi/org_openvpp_vppjapi_vppConn.h>\n");
1903 fprintf (fp, "#include <japi/org_openvpp_vppjapi_%s.h>\n\n", java_class);
1905 fprintf (fp, "#include <api/%s_msg_enum.h>\n", vlib_app_name);
1906 fprintf (fp, "#define vl_typedefs /* define message structures */\n");
1907 fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
1908 fprintf (fp, "#undef vl_typedefs\n\n");
1910 fprintf (fp, "#define vl_endianfun \n");
1911 fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
1912 fprintf (fp, "#undef vl_endianfun\n\n");
1914 fprintf (fp, "#define vl_print(handle, ...)\n");
1915 fprintf (fp, "#define vl_printfun\n");
1916 fprintf (fp, "#include <api/%s_all_api_h.h>\n", vlib_app_name);
1917 fprintf (fp, "#undef vl_printfun\n\n");
1920 void generate_jni_code (YYSTYPE a1, FILE *fp)
1922 node_t *np = (node_t *)a1;
1925 /* Walk the top-level node-list */
1927 if (np->type == NODE_DEFINE) {
1928 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1929 /* Suppress messages named "xyz_reply" */
1930 char * cp = (char *) np->data[0];
1934 if (strncmp (cp, "_reply", 6)) {
1935 current_def_name = np->data[0];
1936 current_java_parameter_number = 0;
1937 vftp = the_vft[np->type];
1938 vftp->generate(np, JAVA_JNI_PASS, fp);
1946 char *hookup_boilerplate =
1947 "void vl_msg_reply_handler_hookup (void)\n"
1949 "#define _(N,n) \\\n"
1950 " vl_msg_api_set_handlers (VL_API_##N, #n, \\\n"
1951 " vl_api_generic_reply_handler, \\\n"
1952 " vl_noop_handler, \\\n"
1953 " vl_api_##n##_t_endian, \\\n"
1954 " vl_api_##n##_t_print, \\\n"
1955 " sizeof(vl_api_##n##_t), 1); \n"
1956 " foreach_api_reply_handler;\n"
1960 void generate_jni_bottom_boilerplate(FILE *fp)
1962 fputs (hookup_boilerplate, fp);
1965 void generate(YYSTYPE a1)
1974 generate_top_boilerplate(ofp);
1976 generate_msg_ids(a1, ofp);
1977 generate_msg_names(a1, ofp);
1978 generate_typedefs(a1, ofp);
1979 generate_uniondefs(a1, ofp);
1980 generate_printfun(a1, ofp);
1981 generate_endianfun(a1, ofp);
1983 generate_bottom_boilerplate(ofp);
1987 generate_java_top_boilerplate(javafp);
1988 generate_java_class_definition(a1, javafp);
1989 generate_java_bottom_boilerplate(javafp);
1992 generate_jni_top_boilerplate(jnifp);
1993 generate_jni_reply_handler_list (a1, jnifp);
1994 generate_jni_code(a1, jnifp);
1995 generate_jni_bottom_boilerplate(jnifp);