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 static int hidden_from_java(const node_t * deeper)
177 if (current_java_parameter_number++ < 3) {
178 if (!strncmp ((char *)(deeper->data[0]), "client_index", 12))
180 else if (!strncmp ((char *)(deeper->data[0]), "context", 7))
182 else if (!strncmp ((char *)(deeper->data[0]), "_vl_msg_id", 10))
189 void primtype_java_method (node_t * this, enum passid which, FILE *ofp,
190 char *java_type_name)
194 deeper = this->deeper;
196 /* We'll take care of _msg_id, client_index, and context ourselves */
197 if (hidden_from_java(deeper)) {
201 if (deeper->type == NODE_SCALAR)
202 fprintf (ofp, "%s %s", java_type_name,
203 java_name_mangle(deeper->data[0]));
205 fprintf (ofp, "%s [] %s", java_type_name,
206 java_name_mangle(deeper->data[0]));
208 current_java_emitted_parameter = 1;
211 void primtype_java_parameter (node_t * this, enum passid which, FILE *ofp,
212 char *java_type_name)
216 deeper = this->deeper;
218 /* We'll take care of _msg_id, client_index, and context ourselves */
219 if (hidden_from_java(deeper)) {
222 if (current_java_parameter_need_comma_space) {
223 current_java_parameter_need_comma_space = 0;
227 if (deeper->type == NODE_SCALAR)
228 fprintf (ofp, "%s %s", java_type_name, (char *)(deeper->data[0]));
230 fprintf (ofp, "%sArray %s", java_type_name, (char *)(deeper->data[0]));
232 current_java_emitted_parameter = 1;
235 void primtype_java_setup (node_t * this, enum passid which, FILE *ofp,
236 char *java_type_name, char *array_element_name)
240 deeper = this->deeper;
242 /* We'll take care of _msg_id, client_index, and context ourselves */
243 if (hidden_from_java(deeper)) {
247 if (deeper->type == NODE_VECTOR) {
250 "%s * %sP = (*env)->Get%sArrayElements (env, %s, NULL);\n",
251 java_type_name, (char *)(deeper->data[0]),
252 array_element_name, (char *)(deeper->data[0]));
255 current_java_emitted_parameter = 1;
258 void primtype_java_code (node_t * this, enum passid which, FILE *ofp,
259 char *java_type_name, char * swapper)
264 deeper = this->deeper;
266 /* We'll take care of _msg_id, client_index, and context ourselves */
267 if (hidden_from_java(deeper)) {
273 s = (char *)(deeper->data[0]);
276 if (deeper->type == NODE_VECTOR)
277 fprintf (ofp, "memcpy (mp->%s, %sP, sizeof (mp->%s));\n",
280 fprintf (ofp, "mp->%s = %s;\n", s, s);
282 if (deeper->type == NODE_VECTOR) {
286 fprintf(ofp, "int _i;\n");
288 fprintf(ofp, "for (_i = 0; _i < %d; _i++) {\n",
289 (int)(u64)(deeper->data[1]));
292 fprintf(ofp, "mp->%s[_i] = %s(%sP[_i]);\n",
301 fprintf (ofp, "mp->%s = %s(%s);\n", s, swapper, s);
305 current_java_emitted_parameter = 1;
308 void primtype_java_teardown (node_t * this, enum passid which, FILE *ofp,
309 char * array_element_name)
313 deeper = this->deeper;
315 /* We'll take care of _msg_id, client_index, and context ourselves */
316 if (hidden_from_java(deeper)) {
320 if (deeper->type == NODE_VECTOR) {
323 "(*env)->Release%sArrayElements (env, %s, %sP, 0);\n",
325 (char *)(deeper->data[0]),
326 (char *)(deeper->data[0]));
329 current_java_emitted_parameter = 1;
332 void node_illegal_print (node_t *this)
334 fprintf(stderr, "node_illegal_print called\n");
338 void node_illegal_generate (node_t *this, enum passid notused, FILE *ofp)
340 fprintf(stderr, "node_illegal_generate called\n");
344 void node_illegal_java_method (node_t *this, enum passid notused, FILE *ofp)
346 fprintf(stderr, "node_illegal_java_method called\n");
350 void node_illegal_java_jni (node_t *this, enum passid notused, FILE *ofp)
352 fprintf(stderr, "node_illegal_java_jni called\n");
356 node_vft_t node_illegal_vft = {
358 node_illegal_generate,
360 node_illegal_java_method,
361 node_illegal_java_jni,
364 void node_u8_print (node_t *this)
366 primtype_recursive_print(this, "u8 ");
369 void node_u8_generate (node_t *this, enum passid which, FILE *ofp)
371 primtype_recursive_generate(this, which, ofp, "u8", "%u", "(unsigned)");
374 void node_u8_java_method (node_t *this, enum passid which, FILE *ofp)
376 primtype_java_method (this, which, ofp, "byte");
379 void node_u8_java_parameter (node_t *this, enum passid which, FILE *ofp)
381 primtype_java_parameter (this, which, ofp, "jbyte");
384 void node_u8_java_setup (node_t *this, enum passid which, FILE *ofp)
386 primtype_java_setup (this, which, ofp, "jbyte", "Byte");
389 void node_u8_java_code (node_t *this, enum passid which, FILE *ofp)
391 primtype_java_code (this, which, ofp, "jbyte", 0 /* swapper */);
394 void node_u8_java_teardown (node_t *this, enum passid which, FILE *ofp)
396 primtype_java_teardown (this, which, ofp, "Byte");
399 node_vft_t node_u8_vft = {
404 node_u8_java_parameter,
407 node_u8_java_teardown,
410 void node_u16_print (node_t *this)
412 primtype_recursive_print(this, "u16 ");
415 void node_u16_generate (node_t *this, enum passid which, FILE *ofp)
417 primtype_recursive_generate(this, which, ofp, "u16", "%u", "(unsigned)");
420 void node_u16_java_method (node_t *this, enum passid which, FILE *ofp)
422 primtype_java_method (this, which, ofp, "short");
425 void node_u16_java_parameter (node_t *this, enum passid which, FILE *ofp)
427 primtype_java_parameter (this, which, ofp, "jshort");
430 void node_u16_java_setup (node_t *this, enum passid which, FILE *ofp)
432 primtype_java_setup (this, which, ofp, "jshort", "Short");
435 void node_u16_java_code (node_t *this, enum passid which, FILE *ofp)
437 primtype_java_code (this, which, ofp, "jshort", "clib_host_to_net_u16");
440 void node_u16_java_teardown (node_t *this, enum passid which, FILE *ofp)
442 primtype_java_teardown (this, which, ofp, "Short");
445 node_vft_t node_u16_vft = {
448 "clib_net_to_host_u16",
449 node_u16_java_method,
450 node_u16_java_parameter,
453 node_u16_java_teardown,
456 void node_u32_print (node_t *this)
458 primtype_recursive_print(this, "u32 ");
461 void node_u32_generate (node_t *this, enum passid which, FILE *ofp)
463 primtype_recursive_generate(this, which, ofp, "u32", "%u", "(unsigned)");
466 void node_u32_java_method (node_t *this, enum passid which, FILE *ofp)
468 primtype_java_method (this, which, ofp, "int");
471 void node_u32_java_parameter (node_t *this, enum passid which, FILE *ofp)
473 primtype_java_parameter (this, which, ofp, "jint");
476 void node_u32_java_setup (node_t *this, enum passid which, FILE *ofp)
478 primtype_java_setup (this, which, ofp, "jint", "Int");
481 void node_u32_java_code (node_t *this, enum passid which, FILE *ofp)
483 primtype_java_code (this, which, ofp, "jint", "clib_host_to_net_u32");
486 void node_u32_java_teardown (node_t *this, enum passid which, FILE *ofp)
488 primtype_java_teardown (this, which, ofp, "Int");
491 node_vft_t node_u32_vft = {
494 "clib_net_to_host_u32",
495 node_u32_java_method,
496 node_u32_java_parameter,
499 node_u32_java_teardown,
502 void node_u64_print (node_t *this)
504 primtype_recursive_print(this, "u64 ");
507 void node_u64_generate (node_t *this, enum passid which, FILE *ofp)
509 primtype_recursive_generate(this, which, ofp, "u64", "%llu",
513 void node_u64_java_method (node_t *this, enum passid which, FILE *ofp)
515 primtype_java_method (this, which, ofp, "long");
518 void node_u64_java_parameter (node_t *this, enum passid which, FILE *ofp)
520 primtype_java_parameter (this, which, ofp, "jlong");
523 void node_u64_java_setup (node_t *this, enum passid which, FILE *ofp)
525 primtype_java_setup (this, which, ofp, "jlong", "Long");
528 void node_u64_java_code (node_t *this, enum passid which, FILE *ofp)
530 primtype_java_code (this, which, ofp, "jlong", "clib_host_to_net_u64");
533 void node_u64_java_teardown (node_t *this, enum passid which, FILE *ofp)
535 primtype_java_teardown (this, which, ofp, "Long");
538 node_vft_t node_u64_vft = {
541 "clib_net_to_host_u64",
542 node_u64_java_method,
543 node_u64_java_parameter,
546 node_u64_java_teardown,
549 void node_i8_print (node_t *this)
551 primtype_recursive_print(this, "i8 ");
554 void node_i8_generate (node_t *this, enum passid which, FILE *ofp)
556 primtype_recursive_generate(this, which, ofp, "i8", "%d", "(int)");
559 node_vft_t node_i8_vft = {
564 node_u8_java_parameter,
567 node_u8_java_teardown,
570 void node_i16_print (node_t *this)
572 primtype_recursive_print(this, "i16 ");
575 void node_i16_generate (node_t *this, enum passid which, FILE *ofp)
577 primtype_recursive_generate(this, which, ofp, "i16", "%d", "(int)");
580 node_vft_t node_i16_vft = {
583 "clib_net_to_host_u16",
584 node_u16_java_method,
585 node_u16_java_parameter,
588 node_u16_java_teardown,
591 void node_i32_print (node_t *this)
593 primtype_recursive_print(this, "i32 ");
596 void node_i32_generate (node_t *this, enum passid which, FILE *ofp)
598 primtype_recursive_generate(this, which, ofp, "i32", "%ld", "(long)");
601 node_vft_t node_i32_vft = {
604 "clib_net_to_host_u32",
605 node_u32_java_method,
606 node_u32_java_parameter,
609 node_u32_java_teardown,
612 void node_i64_print (node_t *this)
614 primtype_recursive_print(this, "i64 ");
617 void node_i64_generate (node_t *this, enum passid which, FILE *ofp)
619 primtype_recursive_generate(this, which, ofp, "i64", "%lld",
623 node_vft_t node_i64_vft = {
626 "clib_net_to_host_u64",
627 node_u64_java_method,
628 node_u64_java_parameter,
631 node_u64_java_teardown,
634 void node_f64_print (node_t *this)
636 primtype_recursive_print(this, "f64 ");
639 void node_f64_generate (node_t *this, enum passid which, FILE *ofp)
641 primtype_recursive_generate(this, which, ofp, "f64", "%.2f",
644 void node_f64_java_method (node_t *this, enum passid which, FILE *ofp)
646 primtype_java_method (this, which, ofp, "double");
649 void node_f64_java_parameter (node_t *this, enum passid which, FILE *ofp)
651 primtype_java_parameter (this, which, ofp, "jdouble");
654 void node_f64_java_setup (node_t *this, enum passid which, FILE *ofp)
656 primtype_java_setup (this, which, ofp, "jdouble", "Double");
659 void node_f64_java_code (node_t *this, enum passid which, FILE *ofp)
662 * Current API code doesn't try to endian-swap doubles
663 * FP formats aren't portable yadda yadda yadda
665 primtype_java_code (this, which, ofp, "jdouble", 0 /* $$$ */);
668 void node_f64_java_teardown (node_t *this, enum passid which, FILE *ofp)
670 primtype_java_teardown (this, which, ofp, "Double");
673 node_vft_t node_f64_vft = {
676 " ", /* FP numbers are sent in host byte order */
677 node_f64_java_method,
678 node_f64_java_parameter,
681 node_f64_java_teardown,
685 void node_packed_print (node_t *this)
687 primtype_recursive_print (this, "packed ");
690 void node_packed_generate (node_t *this, enum passid which, FILE *ofp)
692 primtype_recursive_generate(this, which, ofp, "PACKED", "", "");
695 node_vft_t node_packed_vft = {
697 node_packed_generate,
701 void node_define_print (node_t *this)
703 fprintf(stdout, "define %s {\n", CDATA0);
705 node_vft_t *vftp = the_vft[this->deeper->type];
706 fprintf(stdout, " ");
707 vftp->print(this->deeper);
709 fprintf(stdout, "};\n");
712 static void emit_java_arg_declaration(node_t *child, FILE *fp) {
713 current_java_parameter_number = 0;
715 node_vft_t *vftp = the_vft[child->type];
716 current_java_emitted_parameter = 0;
717 vftp->java_method_function(child, JAVA_METHOD_PASS, fp);
719 if (child && current_java_emitted_parameter)
724 void node_define_generate (node_t *this, enum passid which, FILE *fp)
726 node_t *child, *save_child;
730 fprintf(fp, "typedef VL_API_PACKED(struct _vl_api_%s {\n", CDATA0);
731 child = this->deeper;
734 node_vft_t *vftp = the_vft[child->type];
736 vftp->generate(child, which, fp);
740 fprintf(fp, "}) vl_api_%s_t;\n\n", CDATA0);
745 child = this->deeper;
747 node_vft_t *vftp = the_vft[child->type];
748 vftp->generate(child, which, fp);
753 case JAVA_METHOD_PASS:
757 /* Generate private native declaration */
758 fprintf (fp, "private static native int %s0(", java_name_mangle(CDATA0));
759 emit_java_arg_declaration(this->deeper, fp);
762 /* Generate public Java method */
764 fprintf (fp, "public final int %s(", java_name_mangle(CDATA0));
765 emit_java_arg_declaration(this->deeper, fp);
770 fputs ("checkConnected();\n", fp);
772 fprintf (fp, "return %s.%s0(", java_class, java_name_mangle(CDATA0));
774 child = this->deeper;
775 current_java_parameter_number = 0;
776 while (child && hidden_from_java(child->deeper)) {
780 fputs(java_name_mangle((char *)(child->deeper->data[0])), fp);
794 /* Generate function prototype */
795 fprintf (fp, "JNIEXPORT jint JNICALL Java_org_openvpp_vppjapi_%s_%s0\n",
796 java_class, java_name_mangle(CDATA0));
798 fprintf (fp, "(JNIEnv * env, jclass clazz");
799 current_java_parameter_need_comma_space = 1;
800 child = this->deeper;
803 node_vft_t *vftp = the_vft[child->type];
804 current_java_emitted_parameter = 0;
805 vftp->java_jni_parameter(child, which, fp);
807 if (child && current_java_emitted_parameter)
810 fprintf (fp, ")\n{\n");
813 /* define the api message pointer */
815 fprintf (fp, "vppjni_main_t *jm = &vppjni_main;\n");
817 fprintf (fp, "vl_api_%s_t * mp;\n", current_def_name);
819 fprintf (fp, "u32 my_context_id;\n");
821 fprintf (fp, "int rv;\n");
824 fprintf (fp, "rv = vppjni_sanity_check (jm);\n");
826 fprintf (fp, "if (rv) return rv;\n");
829 fprintf (fp, "my_context_id = vppjni_get_context_id (jm);\n");
831 /* Generate array setups, if any */
834 node_vft_t *vftp = the_vft[child->type];
835 current_java_parameter_number = 0;
836 current_java_emitted_parameter = 0;
837 vftp->java_jni_setup(child, which, fp);
841 /* Setup the API message */
843 fprintf (fp, "M(%s, %s);\n", uppercase(current_def_name),
846 fprintf (fp, "mp->context = clib_host_to_net_u32 (my_context_id);\n");
847 /* $$$ Set up context hash table or some such... */
852 node_vft_t *vftp = the_vft[child->type];
853 current_java_parameter_number = 0;
854 current_java_emitted_parameter = 0;
855 vftp->java_jni_code(child, which, fp);
859 /* Generate array teardowns */
862 node_vft_t *vftp = the_vft[child->type];
863 current_java_parameter_number = 0;
864 current_java_emitted_parameter = 0;
865 vftp->java_jni_teardown(child, which, fp);
869 /* Send the message, return context_id */
871 fprintf (fp, "S;\n");
873 fprintf (fp, "return my_context_id;\n");
876 fprintf (fp, "}\n\n");
880 fprintf(stderr, "node_define_generate: unimp pass %d\n", which);
885 node_vft_t node_define_vft = {
887 node_define_generate,
891 void node_union_print (node_t *this)
893 primtype_recursive_print (this, "union ");
896 void node_union_generate (node_t *this, enum passid which, FILE *fp)
904 fprintf(fp, "u8 _%s_which;\n", CDATA0);
906 fprintf(fp, "union _%s {\n", CDATA0);
907 child = this->deeper;
911 node_vft_t *vftp = the_vft[child->type];
913 vftp->generate(child, which, fp);
918 fprintf(fp, "} %s;\n", CDATA0);
923 uelem = this->deeper;
926 fprintf(fp, "switch(a->_%s_which) {\n",
929 current_union_name = CDATA0;
931 /* Walk the list of objects in this union */
933 node_vft_t *vftp = the_vft[uelem->type];
936 fprintf(fp, "case %d:\n", case_id);
939 /* Drill down on each element */
940 vftp->generate(uelem, which, fp);
942 fprintf(fp, "break;\n");
945 current_union_name = 0;
948 fprintf(fp, "default:\n");
951 if (which == PRINTFUN_PASS) {
953 "vl_print(handle, \"WARNING: _%s_which not set.\\n\");\n",
957 fprintf(fp, "break;\n");
964 fprintf(stderr, "node_union_generate: unimp pass %d\n", which);
970 node_vft_t node_union_vft = {
976 void node_scalar_print (node_t *this)
978 fprintf(stdout, "%s", CDATA0);
979 primtype_recursive_print (this, "");
982 void node_scalar_generate (node_t *this, enum passid which, FILE *fp)
984 char *union_prefix = "";
986 if (current_union_name) {
987 sprintf(tmpbuf, "%s.", current_union_name);
988 union_prefix = tmpbuf;
993 fprintf(fp, "%s;\n", CDATA0);
998 if (current_is_complex) {
999 fprintf(fp, "vl_api_%s_t_print(a->%s%s, handle);\n",
1000 current_type_name, union_prefix, CDATA0);
1002 if (!strcmp(current_type_fmt, "uword")) {
1004 "vl_print(handle, \"%s%s: \" _uword_fmt \"\\n\", %s a->%s%s);\n",
1005 union_prefix, CDATA0, "(_uword_cast)",
1006 union_prefix, CDATA0);
1009 "vl_print(handle, \"%s%s: %s\\n\", %s a->%s%s);\n",
1010 union_prefix, CDATA0,
1011 current_type_fmt, current_type_cast,
1012 union_prefix, CDATA0);
1017 case ENDIANFUN_PASS:
1019 if (current_is_complex) {
1020 fprintf(fp, "vl_api%s_t_endian(a->%s%s);\n",
1021 current_type_name, union_prefix, CDATA0);
1023 /* Current_endianfun == NULL means e.g. it's a u8... */
1024 if (current_endianfun) {
1025 fprintf(fp, "a->%s%s = %s(a->%s%s);\n", union_prefix,
1026 CDATA0, current_endianfun,
1027 union_prefix, CDATA0);
1029 fprintf(fp, "/* a->%s%s = a->%s%s */\n",
1030 union_prefix, CDATA0,
1031 union_prefix, CDATA0);
1037 fprintf(stderr, "node_scalar_generate: unimp pass %d\n", which);
1040 fprintf(stderr, "broken recursion in node_scalar_generate\n");
1045 node_vft_t node_scalar_vft = {
1047 node_scalar_generate,
1051 void node_vector_print (node_t *this)
1053 primtype_recursive_print (this, "vector ");
1056 void node_vector_generate (node_t *this, enum passid which, FILE *fp)
1058 char *union_prefix = "";
1060 if (current_union_name) {
1061 sprintf(tmpbuf, "%s.", current_union_name);
1062 union_prefix = tmpbuf;
1067 fprintf(fp, "%s[%d];\n", CDATA0, IDATA1);
1071 /* Don't bother about "u8 data [0];" et al. */
1079 fprintf(fp, "int _i;\n");
1081 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
1085 if (current_is_complex) {
1086 fprintf(fp, "vl_print(handle, \"%s%s[%%d]: ",
1087 union_prefix, CDATA0);
1089 "vl_print_%s (handle, a->%s%s[_i]);\n",
1090 CDATA0, union_prefix, CDATA0);
1093 "vl_print(handle, \"%s%s[%%d]: %s\\n\", _i, a->%s%s[_i]);\n",
1094 union_prefix, CDATA0,
1096 union_prefix, CDATA0);
1106 case ENDIANFUN_PASS:
1107 /* Don't bother about "u8 data [0];" et al. */
1115 fprintf(fp, "int _i;\n");
1117 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
1121 if (current_is_complex) {
1123 "vl_api_%s_t_endian (a->%s%s[_i]);\n",
1124 current_type_name, union_prefix, CDATA0);
1127 "a->%s%s[_i] = %s(a->%s%s[_i]);\n",
1128 union_prefix, CDATA0,
1130 union_prefix, CDATA0);
1141 fprintf(stderr, "node_vector_generate: unimp pass %d\n", which);
1144 fprintf(stderr, "broken recursion in node_vector_generate\n");
1148 node_vft_t node_vector_vft = {
1150 node_vector_generate,
1154 void node_complex_print (node_t *this)
1156 primtype_recursive_print (this, "complex ");
1159 void node_complex_generate (node_t *this, enum passid which, FILE *fp)
1163 char *member_name = "broken!";
1164 char *union_prefix = "";
1166 if (current_union_name) {
1167 sprintf(tmpbuf, "%s.", current_union_name);
1168 union_prefix = tmpbuf;
1171 current_is_complex++;
1175 fprintf(fp, "%s ", CDATA0);
1176 deeper = this->deeper;
1178 vftp = the_vft[deeper->type];
1179 vftp->generate(deeper, which, fp);
1184 deeper = this->deeper;
1186 if (deeper->type == NODE_SCALAR ||
1187 deeper->type == NODE_VECTOR) {
1188 member_name = deeper->data[0];
1191 deeper = deeper->deeper;
1194 fprintf(fp, "vl_print(handle, \"%s%s ----- \\n\");\n",
1195 union_prefix, member_name);
1197 fprintf(fp, "%s_print(&a->%s%s, handle);\n",
1198 CDATA0, union_prefix, member_name);
1200 fprintf(fp, "vl_print(handle, \"%s%s ----- END \\n\");\n",
1201 union_prefix, member_name);
1204 case ENDIANFUN_PASS:
1205 deeper = this->deeper;
1207 if (deeper->type == NODE_SCALAR ||
1208 deeper->type == NODE_VECTOR) {
1209 member_name = deeper->data[0];
1212 deeper = deeper->deeper;
1216 fprintf(fp, "%s_endian(&a->%s%s);\n",
1217 CDATA0, union_prefix, member_name);
1221 fprintf(stderr, "node_complex_generate unimp pass %d...\n", which);
1224 current_is_complex--;
1227 node_vft_t node_complex_vft = {
1229 node_complex_generate,
1233 void node_noversion_print (node_t *this)
1235 primtype_recursive_print (this, "noversion ");
1238 void node_noversion_generate (node_t *this, enum passid which, FILE *ofp)
1240 fprintf(stderr, "node_noversion_generate called...\n");
1243 node_vft_t node_noversion_vft = {
1244 node_noversion_print,
1245 node_noversion_generate,
1249 void node_uword_print (node_t *this)
1251 primtype_recursive_print(this, "uword ");
1254 void node_uword_generate (node_t *this, enum passid which, FILE *ofp)
1256 primtype_recursive_generate(this, which, ofp, "uword", "uword", "");
1259 node_vft_t node_uword_vft = {
1261 node_uword_generate,
1262 "clib_net_to_host_uword",
1265 node_vft_t *the_vft[NODE_N_TYPES] = {
1282 &node_noversion_vft,
1286 void *make_node (enum node_subclass type)
1290 rv = (node_t *) malloc (sizeof (*rv));
1292 fprintf (stderr, "fatal: make_node out of memory\n");
1295 bzero (rv, sizeof (*rv));
1297 return ((void *) rv);
1300 YYSTYPE deeper (YYSTYPE arg1, YYSTYPE arg2)
1302 node_t *np1 = (node_t *) arg1;
1303 node_t *np2 = (node_t *) arg2;
1308 while (hook_point->deeper)
1309 hook_point = hook_point->deeper;
1311 hook_point->deeper = np2;
1315 YYSTYPE addpeer (YYSTYPE arg1, YYSTYPE arg2)
1317 node_t *np1 = (node_t *) arg1;
1318 node_t *np2 = (node_t *) arg2;
1323 while (hook_point->peer)
1324 hook_point = hook_point->peer;
1326 hook_point->peer = np2;
1331 * add_slist (stmt_list, stmt)
1334 YYSTYPE add_slist (YYSTYPE a1, YYSTYPE a2)
1337 return (addpeer(a1, a2));
1345 * add_define (char *name, defn_list);
1347 YYSTYPE add_define (YYSTYPE a1, YYSTYPE a2)
1351 np = make_node(NODE_DEFINE);
1353 deeper((YYSTYPE)np, a2);
1354 return ((YYSTYPE) np);
1358 * add_defbody (defn_list, new_defn)
1360 YYSTYPE add_defbody (YYSTYPE a1, YYSTYPE a2)
1362 return (addpeer(a1, a2));
1366 * add_primtype ([packed], primitive type, instance)
1369 YYSTYPE add_primtype (YYSTYPE a1, YYSTYPE a2, YYSTYPE a3)
1375 /* Hook instance to type node */
1384 * add_complex(char *type_name, instance)
1387 YYSTYPE add_complex (YYSTYPE a1, YYSTYPE a2)
1391 np = make_node(NODE_COMPLEX);
1392 np->data[0] = (void *) a1;
1394 deeper((YYSTYPE)np, a2);
1395 return ((YYSTYPE) np);
1399 * add_union(char *type_name, definition)
1402 YYSTYPE add_union (YYSTYPE a1, YYSTYPE a2)
1406 np = make_node(NODE_UNION);
1407 np->data[0] = (void *) a1;
1409 deeper((YYSTYPE)np, a2);
1410 return ((YYSTYPE) np);
1415 * add_vector_vbl (node_t *variable, YYSTYPE size)
1418 YYSTYPE add_vector_vbl (YYSTYPE a1, YYSTYPE a2)
1422 np = make_node(NODE_VECTOR);
1423 np->data[0] = (void *) a1;
1424 np->data[1] = (void *) a2;
1425 return ((YYSTYPE) np);
1429 * add_scalar_vbl (char *name)
1431 YYSTYPE add_scalar_vbl (YYSTYPE a1)
1435 np = make_node(NODE_SCALAR);
1436 np->data[0] = (void *) a1;
1437 return ((YYSTYPE) np);
1441 * set_flags (int flags, msg(=0?))
1443 YYSTYPE set_flags(YYSTYPE a1, YYSTYPE a2)
1452 flags = (int)(uword) a1;
1460 YYSTYPE suppress_version (void)
1462 dont_output_version = 1;
1466 void dump(node_t *np)
1471 vftp = the_vft[np->type];
1477 char *fixup_input_filename(void)
1481 cp = (char *)input_filename;
1488 while (cp > input_filename && *cp != '/')
1493 strcpy (tmpbuf, cp);
1502 while (cp > tmpbuf && *cp != '.')
1508 return (sxerox(tmpbuf));
1511 void generate_top_boilerplate(FILE *fp)
1514 char *datestring = ctime(&starttime);
1515 fixed_name = fixup_input_filename();
1519 fprintf (fp, "/*\n");
1520 fprintf (fp, " * VLIB API definitions %s\n", datestring);
1521 fprintf (fp, " * Input file: %s\n", input_filename);
1522 fprintf (fp, " * Automatically generated: please edit the input file ");
1523 fprintf (fp, "NOT this file!\n");
1525 /* Moron Acme trigger workaround */
1526 fprintf (fp, " * %syright (c) %s by Cisco Systems, Inc.\n", "Cop",
1528 fprintf (fp, " */\n\n");
1529 fprintf (fp, "#if defined(vl_msg_id)||defined(vl_union_id)||");
1530 fprintf (fp, "defined(vl_printfun) \\\n ||defined(vl_endianfun)||");
1531 fprintf (fp, " defined(vl_api_version)||defined(vl_typedefs) \\\n");
1532 fprintf (fp, " ||defined(vl_msg_name)\n");
1533 fprintf (fp, "/* ok, something was selected */\n");
1534 fprintf (fp, "#else\n");
1535 fprintf (fp, "#warning no content included from %s\n", input_filename);
1536 fprintf (fp, "#endif\n\n");
1537 fprintf (fp, "#define VL_API_PACKED(x) x __attribute__ ((packed))\n\n");
1540 void generate_bottom_boilerplate(FILE *fp)
1543 fprintf (fp, "\n#ifdef vl_api_version\n");
1545 if (dont_output_version) {
1546 fprintf (fp, "/* WARNING: API FILE VERSION CHECK DISABLED */\n");
1550 fprintf (fp, "vl_api_version(%s, 0x%08x)\n\n",
1551 fixed_name, (unsigned int)input_crc);
1552 fprintf (fp, "#endif\n\n");
1555 void generate_msg_ids(YYSTYPE a1, FILE *fp)
1557 node_t *np = (node_t *)a1;
1559 fprintf (fp, "\n/****** Message ID / handler enum ******/\n\n");
1560 fprintf (fp, "#ifdef vl_msg_id\n");
1563 if (np->type == NODE_DEFINE) {
1564 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1565 fprintf (fp, "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n",
1566 uppercase(np->data[0]), (i8 *)np->data[0]);
1568 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1573 fprintf (fp, "#endif\n");
1577 void generate_msg_names(YYSTYPE a1, FILE *fp)
1579 node_t *np = (node_t *)a1;
1581 fprintf (fp, "\n/****** Message names ******/\n\n");
1583 fprintf (fp, "#ifdef vl_msg_name\n");
1586 if (np->type == NODE_DEFINE) {
1587 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1588 fprintf (fp, "vl_msg_name(vl_api_%s_t, %d)\n",
1590 (np->flags & NODE_FLAG_DONT_TRACE ? 0 : 1));
1592 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1597 fprintf (fp, "#endif\n\n");
1600 void generate_typedefs(YYSTYPE a1, FILE *fp)
1602 node_t *np = (node_t *)a1;
1605 fprintf(fp, "\n/****** Typedefs *****/\n\n");
1606 fprintf(fp, "#ifdef vl_typedefs\n\n");
1608 /* Walk the top-level node-list */
1610 if (np->type == NODE_DEFINE) {
1611 /* Yeah, this is pedantic */
1612 vftp = the_vft[np->type];
1613 vftp->generate(np, TYPEDEF_PASS, fp);
1617 fprintf(fp, "#endif /* vl_typedefs */\n\n");
1620 void union_walk_one_defn(node_t *np, FILE *fp)
1625 /* Walk the list of typed objects in this msg def */
1627 if (np->type == NODE_UNION) {
1628 current_union_name = np->data[0];
1631 /* Walk the list of objects in this union */
1633 vblp = uelem->deeper;
1634 /* Drill down on each element, find the variable name */
1636 if (vblp->type == NODE_SCALAR ||
1637 vblp->type == NODE_VECTOR ||
1638 vblp->type == NODE_COMPLEX) {
1639 fprintf(ofp, "#define %s_",
1640 uppercase(current_def_name));
1641 fprintf(ofp, "%s_", uppercase(current_union_name));
1642 fprintf(ofp, "%s %d\n",uppercase(vblp->data[0]),
1647 vblp = vblp->deeper;
1649 uelem = uelem->peer;
1651 current_union_name = 0;
1658 void generate_uniondefs(YYSTYPE a1, FILE *fp)
1660 node_t *np = (node_t *)a1;
1662 fprintf(fp, "/****** Discriminated Union Definitions *****/\n\n");
1663 fprintf(fp, "#ifdef vl_union_id\n\n");
1665 /* Walk the top-level node-list */
1667 if (np->type == NODE_DEFINE) {
1669 current_def_name = np->data[0];
1670 union_walk_one_defn(np->deeper, fp);
1674 fprintf(fp, "\n#endif /* vl_union_id */\n\n");
1677 void generate_printfun(YYSTYPE a1, FILE *fp)
1679 node_t *np = (node_t *)a1;
1682 fprintf(fp, "/****** Print functions *****/\n\n");
1683 fprintf(fp, "#ifdef vl_printfun\n\n");
1685 fprintf(fp, "#ifdef LP64\n");
1686 fputs ("#define _uword_fmt \"%lld\"\n", fp);
1687 fputs ("#define _uword_cast (long long)\n", fp);
1688 fprintf(fp, "#else\n");
1689 fputs("#define _uword_fmt \"%ld\"\n", fp);
1690 fputs ("#define _uword_cast long\n", fp);
1691 fprintf(fp, "#endif\n\n");
1693 /* Walk the top-level node-list */
1695 if (np->type == NODE_DEFINE) {
1696 if (!(np->flags & NODE_FLAG_MANUAL_PRINT)) {
1698 "static inline void *vl_api_%s_t_print (vl_api_%s_t *a,",
1699 (i8 *)np->data[0], (i8 *) np->data[0]);
1700 fprintf(fp, "void *handle)\n{\n");
1701 /* output the message name */
1703 " vl_print(handle, \"vl_api_%s_t:\\n\");\n",
1707 /* Yeah, this is pedantic */
1708 vftp = the_vft[np->type];
1709 vftp->generate(np, PRINTFUN_PASS, fp);
1710 fprintf(fp, " return handle;\n");
1711 fprintf(fp, "}\n\n");
1714 fprintf(fp, "/***** manual: vl_api_%s_t_print *****/\n\n",
1715 (i8 *) np->data[0]);
1720 fprintf(fp, "#endif /* vl_printfun */\n\n");
1723 void generate_endianfun(YYSTYPE a1, FILE *fp)
1725 node_t *np = (node_t *)a1;
1728 fprintf(fp, "\n/****** Endian swap functions *****/\n\n");
1729 fprintf(fp, "#ifdef vl_endianfun\n\n");
1730 fprintf(fp, "#undef clib_net_to_host_uword\n");
1731 fprintf(fp, "#ifdef LP64\n");
1732 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u64\n");
1733 fprintf(fp, "#else\n");
1734 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u32\n");
1735 fprintf(fp, "#endif\n\n");
1737 /* Walk the top-level node-list */
1739 if (np->type == NODE_DEFINE) {
1740 if (!(np->flags & NODE_FLAG_MANUAL_ENDIAN)) {
1742 "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)\n{\n",
1743 (i8 *) np->data[0], (i8 *) np->data[0]);
1745 /* Yeah, this is pedantic */
1746 vftp = the_vft[np->type];
1747 vftp->generate(np, ENDIANFUN_PASS, fp);
1748 fprintf(fp, "}\n\n");
1751 fprintf(fp, "/***** manual: vl_api_%s_t_endian *****/\n\n",
1752 (i8 *) np->data[0]);
1757 fprintf(fp, "#endif /* vl_endianfun */\n\n");
1760 void add_msg_ids(YYSTYPE a1)
1762 node_t *np = (node_t *)a1;
1766 /* Walk the top-level node-list */
1768 if (np->type == NODE_DEFINE) {
1769 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1770 /* add the parse tree for "u16 _vl_msg_id" */
1771 new_u16 = make_node(NODE_U16);
1772 new_u16->peer = np->deeper;
1773 np->deeper = new_u16;
1774 new_vbl = make_node(NODE_SCALAR);
1775 new_vbl->data[0] = sxerox("_vl_msg_id");
1776 new_u16->deeper = new_vbl;
1783 void generate_java_top_boilerplate(FILE *fp)
1786 char *datestring = ctime(&starttime);
1787 fixed_name = fixup_input_filename();
1791 fprintf (fp, "/*\n");
1792 fprintf (fp, " * VLIB API java binding %s\n", datestring);
1793 fprintf (fp, " * Input file: %s\n", input_filename);
1794 fprintf (fp, " * Automatically generated: please edit the input file ");
1795 fprintf (fp, "NOT this file!\n");
1796 fprintf (fp, " */\n\n");
1798 fprintf (fp, "package org.openvpp.vppjapi;\n\n");
1799 fprintf (fp, "import java.io.IOException;\n\n");
1800 fprintf (fp, "public class %s extends vppConn {\n",
1802 fprintf (fp, " public %s(String clientName) throws IOException {\n", java_class);
1803 fprintf (fp, " super(clientName);\n");
1804 fprintf (fp, " }\n\n");
1807 void generate_java_bottom_boilerplate(FILE *fp)
1809 fprintf (fp, "}\n");
1813 void generate_java_class_definition (YYSTYPE a1, FILE *fp)
1815 node_t *np = (node_t *)a1;
1818 fprintf(fp, "/****** API methods *****/\n\n");
1820 /* Walk the top-level node-list */
1822 if (np->type == NODE_DEFINE) {
1823 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1824 /* Suppress messages named "xyz_reply" */
1825 char * cp = (char *) np->data[0];
1829 if (strncmp (cp, "_reply", 6)) {
1830 current_java_parameter_number = 0;
1831 vftp = the_vft[np->type];
1832 vftp->generate(np, JAVA_METHOD_PASS, fp);
1839 fprintf(fp, "\n/****** end of API methods *****/\n");
1842 void generate_jni_reply_handler_list (YYSTYPE a1, FILE *fp)
1844 node_t *np = (node_t *)a1;
1847 fprintf (fp, "#define foreach_api_reply_handler \\\n");
1849 /* Walk the top-level node-list */
1851 if (np->type == NODE_DEFINE) {
1852 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1853 /* emit messages named "xyz_reply" */
1854 char * cp = (char *) np->data[0];
1858 if (!strncmp (cp, "_reply", 6)) {
1859 fprintf (fp, "_(%s, %s) \\\n",
1860 uppercase(np->data[0]), (char *)(np->data[0]));
1867 fprintf (fp, "\n\n");
1870 char * m_macro_boilerplate =
1871 "#define M(T,t) \\\n"
1873 " api_result_ready = 0; \\\n"
1874 " mp = vl_msg_api_alloc(sizeof(*mp)); \\\n"
1875 " memset (mp, 0, sizeof (*mp)); \\\n"
1876 " mp->_vl_msg_id = ntohs (VL_API_##T); \\\n"
1877 " mp->client_index = api_main.my_client_index; \\\n"
1879 "#define M2(T,t,n) \\\n"
1881 " api_result_ready = 0; \\\n"
1882 " mp = vl_msg_api_alloc(sizeof(*mp)+(n)); \\\n"
1883 " memset (mp, 0, sizeof (*mp)); \\\n"
1884 " mp->_vl_msg_id = ntohs (VL_API_##T); \\\n"
1885 " mp->client_index = api_main.my_client_index; \\\n"
1888 char * s_macro_boilerplate =
1889 "#define S (vl_msg_api_send_shmem (api_main.shmem_hdr->vl_input_queue, \\\n"
1892 char * w_macro_boilerplate =
1895 " timeout = clib_time_now (&clib_time) + 1.0; \\\n"
1897 " while (clib_time_now (&clib_time) < timeout) { \\\n"
1898 " if (api_result_ready == 1) { \\\n"
1899 " return ((jint) api_result); \\\n"
1905 void generate_jni_top_boilerplate(FILE *fp)
1908 char *datestring = ctime(&starttime);
1909 fixed_name = fixup_input_filename();
1913 fprintf (fp, "/*\n");
1914 fprintf (fp, " * VLIB Java native code %s\n", datestring);
1915 fprintf (fp, " * Input file: %s\n", input_filename);
1916 fprintf (fp, " * Automatically generated: please edit the input file ");
1917 fprintf (fp, "NOT this file!\n");
1918 fprintf (fp, " */\n\n");
1920 fprintf (fp, "#include <japi/vppjni.h>\n");
1923 "#define vl_api_version(n,v) static u32 %s_api_version %s = v;\n",
1924 vlib_app_name, "__attribute__((unused))");
1925 fprintf (fp, "#include <api/%s.api.h>\n", vlib_app_name);
1926 fprintf (fp, "#undef vl_api_version\n\n");
1928 fprintf (fp, "#include <japi/org_openvpp_vppjapi_vppConn.h>\n");
1929 fprintf (fp, "#include <japi/org_openvpp_vppjapi_%s.h>\n\n", java_class);
1931 fprintf (fp, "#include <api/%s_msg_enum.h>\n", vlib_app_name);
1932 fprintf (fp, "#define vl_typedefs /* define message structures */\n");
1933 fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
1934 fprintf (fp, "#undef vl_typedefs\n\n");
1936 fprintf (fp, "#define vl_endianfun \n");
1937 fprintf (fp, "#include <api/%s_all_api_h.h> \n", vlib_app_name);
1938 fprintf (fp, "#undef vl_endianfun\n\n");
1940 fprintf (fp, "#define vl_print(handle, ...)\n");
1941 fprintf (fp, "#define vl_printfun\n");
1942 fprintf (fp, "#include <api/%s_all_api_h.h>\n", vlib_app_name);
1943 fprintf (fp, "#undef vl_printfun\n\n");
1946 void generate_jni_code (YYSTYPE a1, FILE *fp)
1948 node_t *np = (node_t *)a1;
1951 /* Walk the top-level node-list */
1953 if (np->type == NODE_DEFINE) {
1954 if (!(np->flags & (NODE_FLAG_MANUAL_JAVA | NODE_FLAG_TYPEONLY))) {
1955 /* Suppress messages named "xyz_reply" */
1956 char * cp = (char *) np->data[0];
1960 if (strncmp (cp, "_reply", 6)) {
1961 current_def_name = np->data[0];
1962 current_java_parameter_number = 0;
1963 vftp = the_vft[np->type];
1964 vftp->generate(np, JAVA_JNI_PASS, fp);
1972 char *hookup_boilerplate =
1973 "void vl_msg_reply_handler_hookup (void)\n"
1975 "#define _(N,n) \\\n"
1976 " vl_msg_api_set_handlers (VL_API_##N, #n, \\\n"
1977 " vl_api_generic_reply_handler, \\\n"
1978 " vl_noop_handler, \\\n"
1979 " vl_api_##n##_t_endian, \\\n"
1980 " vl_api_##n##_t_print, \\\n"
1981 " sizeof(vl_api_##n##_t), 1); \n"
1982 " foreach_api_reply_handler;\n"
1986 void generate_jni_bottom_boilerplate(FILE *fp)
1988 fputs (hookup_boilerplate, fp);
1991 void generate(YYSTYPE a1)
2000 generate_top_boilerplate(ofp);
2002 generate_msg_ids(a1, ofp);
2003 generate_msg_names(a1, ofp);
2004 generate_typedefs(a1, ofp);
2005 generate_uniondefs(a1, ofp);
2006 generate_printfun(a1, ofp);
2007 generate_endianfun(a1, ofp);
2009 generate_bottom_boilerplate(ofp);
2013 generate_java_top_boilerplate(javafp);
2014 generate_java_class_definition(a1, javafp);
2015 generate_java_bottom_boilerplate(javafp);
2018 generate_jni_top_boilerplate(jnifp);
2019 generate_jni_reply_handler_list (a1, jnifp);
2020 generate_jni_code(a1, jnifp);
2021 generate_jni_bottom_boilerplate(jnifp);