blob: 4460fd5f933a6d0694bba0f26a6c0f61795c2d74 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 *------------------------------------------------------------------
3 * node.c - the api generator's semantic back-end
4 *
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:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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 *------------------------------------------------------------------
18 */
19
20#include <stdio.h>
Dave Barach557d1282016-11-10 14:22:49 -050021#include <stdbool.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070022#include <ctype.h>
23#include <time.h>
24#include <string.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <vppinfra/vec.h>
28#include <vppinfra/hash.h>
29
30#include "lex.h"
31#include "node.h"
32
33#define YYSTYPE void *
34
35FILE *ofp;
Ole Troan6855f6c2016-04-09 03:16:30 +020036FILE *pythonfp;
Dave Barach557d1282016-11-10 14:22:49 -050037FILE *jsonfp;
Ed Warnickecb9cada2015-12-08 15:45:58 -070038time_t starttime;
39char *vlib_app_name;
40char *input_filename;
41node_vft_t *the_vft[NODE_N_TYPES];
42static int indent;
43static int dont_output_version;
44int dump_tree;
45static char *fixed_name;
46static char tmpbuf [MAXNAME];
47static char *current_def_name;
48static char *current_union_name;
49static char *current_type_fmt;
50static char *current_type_cast;
51static char current_id;
52static char current_is_complex;
53static char *current_endianfun;
54static char *current_type_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
56void indent_me(FILE *ofp)
57{
58 int i;
59
60 for (i = 0; i < indent; i++)
61 putc(' ', ofp);
62}
63
64char *uppercase (char *s)
65{
66 char *cp;
67
68 cp = tmpbuf;
69
70 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
71 if (*s >= 'a' && *s <= 'z')
72 *cp++ = *s++ - ('a' - 'A');
73 else
74 *cp++ = *s++;
75 }
76 *cp = 0;
77 return(tmpbuf);
78}
79
80char *lowercase (char *s)
81{
82 char *cp;
83
84 cp = tmpbuf;
85
86 while (*s && (cp < tmpbuf + (sizeof(tmpbuf)-1))) {
87 if (*s >= 'A' && *s <= 'Z')
88 *cp++ = *s++ + ('a' - 'A');
89 else
90 *cp++ = *s++;
91 }
92 *cp = 0;
93 return(tmpbuf);
94}
95
Ed Warnickecb9cada2015-12-08 15:45:58 -070096void primtype_recursive_print(node_t *this, i8 *fmt)
97{
98 fputs((char *)fmt, stdout);
99
100 if (this->deeper) {
101 node_vft_t *vftp = the_vft[this->deeper->type];
102 vftp->print(this->deeper);
103 }
104}
105
106void primtype_recursive_generate(node_t *this, enum passid which, FILE *ofp,
107 i8 *type_name, i8 *type_fmt, i8 *type_cast)
108{
109 node_vft_t *vftp;
110
111 current_type_name = (char *)type_name;
112 current_type_cast = (char *)type_cast;
113
114 switch(which) {
115 case TYPEDEF_PASS:
116 fputs((char *)type_name, ofp);
117 fputs(" ", ofp);
118 break;
119
120 case PRINTFUN_PASS:
121 current_type_fmt = (char *)type_fmt;
122 break;
123
124 case ENDIANFUN_PASS:
125 vftp = the_vft[this->type];
126 current_endianfun = vftp->endian_converter;
127 break;
128
Ole Troan6855f6c2016-04-09 03:16:30 +0200129 case PYTHON_PASS:
130 fputs("('", pythonfp);
131 fputs((char *)type_name, pythonfp);
132 fputs("', ", pythonfp);
133 break;
134
Dave Barach557d1282016-11-10 14:22:49 -0500135 case JSON_PASS:
136 fputs("[\"", jsonfp);
137 fputs((char *)type_name, jsonfp);
138 fputs("\", ", jsonfp);
139 break;
140
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 default:
142 fprintf(stderr, "primtype_recursive_generate: unimp pass %d\n", which);
143 break;
144 }
145
146 if (this->deeper) {
147 vftp = the_vft[this->deeper->type];
148 vftp->generate(this->deeper, which, ofp);
149 }
150}
151
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152void node_illegal_print (node_t *this)
153{
154 fprintf(stderr, "node_illegal_print called\n");
155 exit(0);
156}
157
158void node_illegal_generate (node_t *this, enum passid notused, FILE *ofp)
159{
160 fprintf(stderr, "node_illegal_generate called\n");
161 exit(0);
162}
163
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164node_vft_t node_illegal_vft = {
165 node_illegal_print,
166 node_illegal_generate,
Marek Gradzki948b95a2016-07-20 14:53:48 +0200167 "illegal"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168};
169
170void node_u8_print (node_t *this)
171{
172 primtype_recursive_print(this, "u8 ");
173}
174
175void node_u8_generate (node_t *this, enum passid which, FILE *ofp)
176{
177 primtype_recursive_generate(this, which, ofp, "u8", "%u", "(unsigned)");
178}
179
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180node_vft_t node_u8_vft = {
181 node_u8_print,
182 node_u8_generate,
Chris Luke1642e392016-07-22 09:40:19 -0400183 NULL
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184};
185
186void node_u16_print (node_t *this)
187{
188 primtype_recursive_print(this, "u16 ");
189}
190
191void node_u16_generate (node_t *this, enum passid which, FILE *ofp)
192{
193 primtype_recursive_generate(this, which, ofp, "u16", "%u", "(unsigned)");
194}
195
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196node_vft_t node_u16_vft = {
197 node_u16_print,
198 node_u16_generate,
Marek Gradzki948b95a2016-07-20 14:53:48 +0200199 "clib_net_to_host_u16"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200};
201
202void node_u32_print (node_t *this)
203{
204 primtype_recursive_print(this, "u32 ");
205}
206
207void node_u32_generate (node_t *this, enum passid which, FILE *ofp)
208{
209 primtype_recursive_generate(this, which, ofp, "u32", "%u", "(unsigned)");
210}
211
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212node_vft_t node_u32_vft = {
213 node_u32_print,
214 node_u32_generate,
215 "clib_net_to_host_u32",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216};
217
218void node_u64_print (node_t *this)
219{
220 primtype_recursive_print(this, "u64 ");
221}
222
223void node_u64_generate (node_t *this, enum passid which, FILE *ofp)
224{
225 primtype_recursive_generate(this, which, ofp, "u64", "%llu",
226 "(long long)");
227}
228
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229node_vft_t node_u64_vft = {
230 node_u64_print,
231 node_u64_generate,
Marek Gradzki948b95a2016-07-20 14:53:48 +0200232 "clib_net_to_host_u64"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233};
234
235void node_i8_print (node_t *this)
236{
237 primtype_recursive_print(this, "i8 ");
238}
239
240void node_i8_generate (node_t *this, enum passid which, FILE *ofp)
241{
242 primtype_recursive_generate(this, which, ofp, "i8", "%d", "(int)");
243}
244
245node_vft_t node_i8_vft = {
246 node_i8_print,
247 node_i8_generate,
Marek Gradzki948b95a2016-07-20 14:53:48 +0200248 ""
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249};
250
251void node_i16_print (node_t *this)
252{
253 primtype_recursive_print(this, "i16 ");
254}
255
256void node_i16_generate (node_t *this, enum passid which, FILE *ofp)
257{
258 primtype_recursive_generate(this, which, ofp, "i16", "%d", "(int)");
259}
260
261node_vft_t node_i16_vft = {
262 node_i16_print,
263 node_i16_generate,
Marek Gradzki948b95a2016-07-20 14:53:48 +0200264 "clib_net_to_host_u16"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265};
266
267void node_i32_print (node_t *this)
268{
269 primtype_recursive_print(this, "i32 ");
270}
271
272void node_i32_generate (node_t *this, enum passid which, FILE *ofp)
273{
274 primtype_recursive_generate(this, which, ofp, "i32", "%ld", "(long)");
275}
276
277node_vft_t node_i32_vft = {
278 node_i32_print,
279 node_i32_generate,
Marek Gradzki948b95a2016-07-20 14:53:48 +0200280 "clib_net_to_host_u32"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281};
282
283void node_i64_print (node_t *this)
284{
285 primtype_recursive_print(this, "i64 ");
286}
287
288void node_i64_generate (node_t *this, enum passid which, FILE *ofp)
289{
290 primtype_recursive_generate(this, which, ofp, "i64", "%lld",
291 "(long long)");
292}
293
294node_vft_t node_i64_vft = {
295 node_i64_print,
296 node_i64_generate,
Marek Gradzki948b95a2016-07-20 14:53:48 +0200297 "clib_net_to_host_u64"
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298};
299
300void node_f64_print (node_t *this)
301{
302 primtype_recursive_print(this, "f64 ");
303}
304
305void node_f64_generate (node_t *this, enum passid which, FILE *ofp)
306{
307 primtype_recursive_generate(this, which, ofp, "f64", "%.2f",
308 "(double)");
309}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310
311node_vft_t node_f64_vft = {
312 node_f64_print,
313 node_f64_generate,
314 " ", /* FP numbers are sent in host byte order */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315};
316
317
318void node_packed_print (node_t *this)
319{
320 primtype_recursive_print (this, "packed ");
321}
322
323void node_packed_generate (node_t *this, enum passid which, FILE *ofp)
324{
325 primtype_recursive_generate(this, which, ofp, "PACKED", "", "");
326}
327
328node_vft_t node_packed_vft = {
329 node_packed_print,
330 node_packed_generate,
331 0,
332};
333
334void node_define_print (node_t *this)
335{
336 fprintf(stdout, "define %s {\n", CDATA0);
337 if (this->deeper) {
338 node_vft_t *vftp = the_vft[this->deeper->type];
339 fprintf(stdout, " ");
340 vftp->print(this->deeper);
341 }
342 fprintf(stdout, "};\n");
343}
344
345void node_define_generate (node_t *this, enum passid which, FILE *fp)
346{
347 node_t *child, *save_child;
348
349 switch(which) {
350 case TYPEDEF_PASS:
351 fprintf(fp, "typedef VL_API_PACKED(struct _vl_api_%s {\n", CDATA0);
352 child = this->deeper;
353 indent += 4;
354 while (child) {
355 node_vft_t *vftp = the_vft[child->type];
356 indent_me(fp);
357 vftp->generate(child, which, fp);
358 child = child->peer;
359 }
360 indent -= 4;
361 fprintf(fp, "}) vl_api_%s_t;\n\n", CDATA0);
362 break;
363
364 case ENDIANFUN_PASS:
365 case PRINTFUN_PASS:
366 child = this->deeper;
367 while (child) {
368 node_vft_t *vftp = the_vft[child->type];
369 vftp->generate(child, which, fp);
370 child = child->peer;
371 }
372 break;
373
Ole Troan6855f6c2016-04-09 03:16:30 +0200374 case PYTHON_PASS:
375 fprintf(fp, "('%s',\n", CDATA0);
376 child = this->deeper;
377 indent += 4;
378 while (child) {
379 node_vft_t *vftp = the_vft[child->type];
380 indent_me(fp);
381 vftp->generate(child, which, fp);
382 child = child->peer;
383 }
384 indent -= 4;
385 fprintf(fp, "),\n\n");
386 break;
387
Dave Barach557d1282016-11-10 14:22:49 -0500388 case JSON_PASS:
389 fprintf(fp, "[\"%s\",\n", CDATA0);
390 child = this->deeper;
391 indent += 4;
392 while (child) {
393 node_vft_t *vftp = the_vft[child->type];
394 indent_me(fp);
395 vftp->generate(child, which, fp);
396 child = child->peer;
397 fprintf(fp, ",\n");
398 }
399 indent_me(fp);
400 fprintf (fp, "{\"crc\" : \"0x%08x\"}\n", (u32)(u64)CDATA3);
401 indent -= 4;
402 indent_me(fp);
403 fprintf(fp, "]");
404 break;
405
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 default:
407 fprintf(stderr, "node_define_generate: unimp pass %d\n", which);
408 break;
409 }
410}
411
412node_vft_t node_define_vft = {
413 node_define_print,
414 node_define_generate,
415 0,
416};
417
418void node_union_print (node_t *this)
419{
420 primtype_recursive_print (this, "union ");
421}
422
423void node_union_generate (node_t *this, enum passid which, FILE *fp)
424{
425 node_t *child;
426 node_t *uelem;
427 int case_id=1;
428
429 switch(which) {
430 case TYPEDEF_PASS:
431 fprintf(fp, "u8 _%s_which;\n", CDATA0);
432 indent_me(fp);
433 fprintf(fp, "union _%s {\n", CDATA0);
434 child = this->deeper;
435 indent += 4;
436
437 while (child) {
438 node_vft_t *vftp = the_vft[child->type];
439 indent_me(fp);
440 vftp->generate(child, which, fp);
441 child = child->peer;
442 }
443 indent -= 4;
444 indent_me(fp);
445 fprintf(fp, "} %s;\n", CDATA0);
446 break;
447
448 case PRINTFUN_PASS:
449 case ENDIANFUN_PASS:
450 uelem = this->deeper;
451
452 indent_me(fp);
453 fprintf(fp, "switch(a->_%s_which) {\n",
454 CDATA0);
455 indent += 4;
456 current_union_name = CDATA0;
457
458 /* Walk the list of objects in this union */
459 while (uelem) {
460 node_vft_t *vftp = the_vft[uelem->type];
461 indent -= 4;
462 indent_me(fp);
463 fprintf(fp, "case %d:\n", case_id);
464 case_id++;
465 indent += 4;
466 /* Drill down on each element */
467 vftp->generate(uelem, which, fp);
468 indent_me(fp);
469 fprintf(fp, "break;\n");
470 uelem = uelem->peer;
471 }
472 current_union_name = 0;
473 indent -= 4;
474 indent_me(fp);
475 fprintf(fp, "default:\n");
476 indent += 4;
477 indent_me(fp);
478 if (which == PRINTFUN_PASS) {
479 fprintf(fp,
480 "vl_print(handle, \"WARNING: _%s_which not set.\\n\");\n",
481 CDATA0);
482 }
483 indent_me(fp);
484 fprintf(fp, "break;\n");
485 indent -= 4;
486 indent_me(fp);
487 fprintf(fp, "}\n");
488 break;
489
490 default:
491 fprintf(stderr, "node_union_generate: unimp pass %d\n", which);
492 break;
493 }
494}
495
496
497node_vft_t node_union_vft = {
498 node_union_print,
499 node_union_generate,
500 0,
501};
502
503void node_scalar_print (node_t *this)
504{
505 fprintf(stdout, "%s", CDATA0);
506 primtype_recursive_print (this, "");
507}
508
509void node_scalar_generate (node_t *this, enum passid which, FILE *fp)
510{
511 char *union_prefix = "";
512
513 if (current_union_name) {
514 sprintf(tmpbuf, "%s.", current_union_name);
515 union_prefix = tmpbuf;
516 }
517
518 switch(which) {
519 case TYPEDEF_PASS:
520 fprintf(fp, "%s;\n", CDATA0);
521 break;
522
523 case PRINTFUN_PASS:
524 indent_me(fp);
525 if (current_is_complex) {
526 fprintf(fp, "vl_api_%s_t_print(a->%s%s, handle);\n",
527 current_type_name, union_prefix, CDATA0);
528 } else {
529 if (!strcmp(current_type_fmt, "uword")) {
530 fprintf(fp,
531 "vl_print(handle, \"%s%s: \" _uword_fmt \"\\n\", %s a->%s%s);\n",
532 union_prefix, CDATA0, "(_uword_cast)",
533 union_prefix, CDATA0);
534 } else {
535 fprintf(fp,
536 "vl_print(handle, \"%s%s: %s\\n\", %s a->%s%s);\n",
537 union_prefix, CDATA0,
538 current_type_fmt, current_type_cast,
539 union_prefix, CDATA0);
540 }
541 }
542 break;
543
544 case ENDIANFUN_PASS:
545 indent_me(fp);
546 if (current_is_complex) {
547 fprintf(fp, "vl_api%s_t_endian(a->%s%s);\n",
548 current_type_name, union_prefix, CDATA0);
549 } else {
550 /* Current_endianfun == NULL means e.g. it's a u8... */
551 if (current_endianfun) {
552 fprintf(fp, "a->%s%s = %s(a->%s%s);\n", union_prefix,
553 CDATA0, current_endianfun,
554 union_prefix, CDATA0);
555 } else {
Chris Luke1642e392016-07-22 09:40:19 -0400556 fprintf(fp, "/* a->%s%s = a->%s%s (no-op) */\n",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 union_prefix, CDATA0,
558 union_prefix, CDATA0);
559 }
560 }
561 break;
Ole Troan6855f6c2016-04-09 03:16:30 +0200562 case PYTHON_PASS:
563 fprintf(fp, "'%s'),\n", CDATA0);
564 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Dave Barach557d1282016-11-10 14:22:49 -0500566 case JSON_PASS:
567 fprintf(fp, "\"%s\"]", CDATA0);
568 break;
569
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 default:
571 fprintf(stderr, "node_scalar_generate: unimp pass %d\n", which);
572 }
573 if (this->deeper) {
574 fprintf(stderr, "broken recursion in node_scalar_generate\n");
575 }
576}
577
578
579node_vft_t node_scalar_vft = {
580 node_scalar_print,
581 node_scalar_generate,
582 0,
583};
584
585void node_vector_print (node_t *this)
586{
587 primtype_recursive_print (this, "vector ");
588}
589
590void node_vector_generate (node_t *this, enum passid which, FILE *fp)
591{
592 char *union_prefix = "";
593
594 if (current_union_name) {
595 sprintf(tmpbuf, "%s.", current_union_name);
596 union_prefix = tmpbuf;
597 }
598
599 switch(which) {
600 case TYPEDEF_PASS:
601 fprintf(fp, "%s[%d];\n", CDATA0, IDATA1);
602 break;
603
604 case PRINTFUN_PASS:
605 /* Don't bother about "u8 data [0];" et al. */
606 if (IDATA1 == 0)
607 break;
608
609 indent_me(fp);
610 fprintf(fp, "{\n");
611 indent += 4;
612 indent_me(fp);
613 fprintf(fp, "int _i;\n");
614 indent_me(fp);
615 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
616 IDATA1);
617 indent += 4;
618 indent_me(fp);
619 if (current_is_complex) {
620 fprintf(fp, "vl_print(handle, \"%s%s[%%d]: ",
621 union_prefix, CDATA0);
622 fprintf(fp,
623 "vl_print_%s (handle, a->%s%s[_i]);\n",
624 CDATA0, union_prefix, CDATA0);
625 } else {
626 fprintf(fp,
627 "vl_print(handle, \"%s%s[%%d]: %s\\n\", _i, a->%s%s[_i]);\n",
628 union_prefix, CDATA0,
629 current_type_fmt,
630 union_prefix, CDATA0);
631 }
632 indent -= 4;
633 indent_me(fp);
634 fprintf(fp, "}\n");
635 indent -= 4;
636 indent_me(fp);
637 fprintf(fp, "}\n");
638 break;
639
640 case ENDIANFUN_PASS:
641 /* Don't bother about "u8 data [0];" et al. */
642 if (IDATA1 == 0)
643 break;
Chris Luke1642e392016-07-22 09:40:19 -0400644 /* If this is a simple endian swap, but the endian swap method is a no-op,
645 * then indicate this is a no-op in a comment.
646 */
647 if (!current_is_complex && current_endianfun == NULL) {
648 indent_me(fp);
649 fprintf(fp, "/* a->%s%s[0..%d] = a->%s%s[0..%d] (no-op) */\n",
650 union_prefix, CDATA0, IDATA1 - 1,
651 union_prefix, CDATA0, IDATA1 - 1);
652 break;
653 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654
655 indent_me(fp);
656 fprintf(fp, "{\n");
657 indent += 4;
658 indent_me(fp);
659 fprintf(fp, "int _i;\n");
660 indent_me(fp);
661 fprintf(fp, "for (_i = 0; _i < %d; _i++) {\n",
662 IDATA1);
663 indent += 4;
664 indent_me(fp);
665 if (current_is_complex) {
666 fprintf(fp,
667 "vl_api_%s_t_endian (a->%s%s[_i]);\n",
668 current_type_name, union_prefix, CDATA0);
669 } else {
670 fprintf(fp,
671 "a->%s%s[_i] = %s(a->%s%s[_i]);\n",
672 union_prefix, CDATA0,
673 current_endianfun,
674 union_prefix, CDATA0);
675 }
676 indent -= 4;
677 indent_me(fp);
678 fprintf(fp, "}\n");
679 indent -= 4;
680 indent_me(fp);
681 fprintf(fp, "}\n");
682 break;
Ole Troan6855f6c2016-04-09 03:16:30 +0200683 case PYTHON_PASS:
Marek Gradzkifa42e252016-06-15 16:38:33 +0200684 if (CDATA2 != 0) { // variable length vector
685 fprintf(fp, "'%s', '%d', '%s'),\n", CDATA0, IDATA1, CDATA2);
686 } else {
687 fprintf(fp, "'%s', '%d'),\n", CDATA0, IDATA1);
688 }
Ole Troan6855f6c2016-04-09 03:16:30 +0200689 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
Dave Barach557d1282016-11-10 14:22:49 -0500691 case JSON_PASS:
692 if (CDATA2 != 0) { /* variable length vector */
693 fprintf(fp, "\"%s\", %d, \"%s\"]", CDATA0, IDATA1, CDATA2);
694 } else {
695 fprintf(fp, "\"%s\", %d]", CDATA0, IDATA1);
696 }
697 break;
698
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 default:
700 fprintf(stderr, "node_vector_generate: unimp pass %d\n", which);
701 }
702 if (this->deeper) {
703 fprintf(stderr, "broken recursion in node_vector_generate\n");
704 }
705}
706
707node_vft_t node_vector_vft = {
708 node_vector_print,
709 node_vector_generate,
710 0,
711};
712
713void node_complex_print (node_t *this)
714{
715 primtype_recursive_print (this, "complex ");
716}
717
718void node_complex_generate (node_t *this, enum passid which, FILE *fp)
719{
720 node_t *deeper;
721 node_vft_t *vftp;
722 char *member_name = "broken!";
723 char *union_prefix = "";
724
725 if (current_union_name) {
726 sprintf(tmpbuf, "%s.", current_union_name);
727 union_prefix = tmpbuf;
728 }
729
730 current_is_complex++;
731
732 switch(which) {
733 case TYPEDEF_PASS:
734 fprintf(fp, "%s ", CDATA0);
735 deeper = this->deeper;
736 if (deeper) {
737 vftp = the_vft[deeper->type];
738 vftp->generate(deeper, which, fp);
739 }
740 break;
741
742 case PRINTFUN_PASS:
743 deeper = this->deeper;
744 while (deeper) {
745 if (deeper->type == NODE_SCALAR ||
746 deeper->type == NODE_VECTOR) {
747 member_name = deeper->data[0];
748 break;
749 }
750 deeper = deeper->deeper;
751 }
752 indent_me(fp);
753 fprintf(fp, "vl_print(handle, \"%s%s ----- \\n\");\n",
754 union_prefix, member_name);
755 indent_me(fp);
756 fprintf(fp, "%s_print(&a->%s%s, handle);\n",
757 CDATA0, union_prefix, member_name);
758 indent_me(fp);
759 fprintf(fp, "vl_print(handle, \"%s%s ----- END \\n\");\n",
760 union_prefix, member_name);
761 break;
762
763 case ENDIANFUN_PASS:
764 deeper = this->deeper;
765 while (deeper) {
766 if (deeper->type == NODE_SCALAR ||
767 deeper->type == NODE_VECTOR) {
768 member_name = deeper->data[0];
769 break;
770 }
771 deeper = deeper->deeper;
772 }
773
774 indent_me(fp);
775 fprintf(fp, "%s_endian(&a->%s%s);\n",
776 CDATA0, union_prefix, member_name);
777 break;
Ole Troan6855f6c2016-04-09 03:16:30 +0200778 case PYTHON_PASS:
779 fprintf(fp, "('%s',", CDATA0);
780 deeper = this->deeper;
781 if (deeper) {
782 vftp = the_vft[deeper->type];
783 vftp->generate(deeper, which, fp);
784 }
785 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786
Dave Barach557d1282016-11-10 14:22:49 -0500787 case JSON_PASS:
788 fprintf(fp, "[\"%s\", ", CDATA0);
789 deeper = this->deeper;
790 if (deeper) {
791 vftp = the_vft[deeper->type];
792 vftp->generate(deeper, which, fp);
793 }
794 break;
795
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 default:
797 fprintf(stderr, "node_complex_generate unimp pass %d...\n", which);
798 break;
799 }
800 current_is_complex--;
801}
802
803node_vft_t node_complex_vft = {
804 node_complex_print,
805 node_complex_generate,
806 0,
807};
808
809void node_noversion_print (node_t *this)
810{
811 primtype_recursive_print (this, "noversion ");
812}
813
814void node_noversion_generate (node_t *this, enum passid which, FILE *ofp)
815{
816 fprintf(stderr, "node_noversion_generate called...\n");
817}
818
819node_vft_t node_noversion_vft = {
820 node_noversion_print,
821 node_noversion_generate,
822 0,
823};
824
825void node_uword_print (node_t *this)
826{
827 primtype_recursive_print(this, "uword ");
828}
829
830void node_uword_generate (node_t *this, enum passid which, FILE *ofp)
831{
832 primtype_recursive_generate(this, which, ofp, "uword", "uword", "");
833}
834
835node_vft_t node_uword_vft = {
836 node_uword_print,
837 node_uword_generate,
838 "clib_net_to_host_uword",
839};
840
841node_vft_t *the_vft[NODE_N_TYPES] = {
842 &node_illegal_vft,
843 &node_u8_vft,
844 &node_u16_vft,
845 &node_u32_vft,
846 &node_u64_vft,
847 &node_i8_vft,
848 &node_i16_vft,
849 &node_i32_vft,
850 &node_i64_vft,
851 &node_f64_vft,
852 &node_packed_vft,
853 &node_define_vft,
854 &node_union_vft,
855 &node_scalar_vft,
856 &node_vector_vft,
857 &node_complex_vft,
858 &node_noversion_vft,
859 &node_uword_vft,
860};
861
862void *make_node (enum node_subclass type)
863{
864 node_t *rv;
865
866 rv = (node_t *) malloc (sizeof (*rv));
867 if (rv == 0) {
868 fprintf (stderr, "fatal: make_node out of memory\n");
869 exit (1);
870 }
871 bzero (rv, sizeof (*rv));
872 rv->type = type;
873 return ((void *) rv);
874}
875
876YYSTYPE deeper (YYSTYPE arg1, YYSTYPE arg2)
877{
878 node_t *np1 = (node_t *) arg1;
879 node_t *np2 = (node_t *) arg2;
880 node_t *hook_point;
881
882 hook_point = np1;
883
884 while (hook_point->deeper)
885 hook_point = hook_point->deeper;
886
887 hook_point->deeper = np2;
888 return (arg1);
889}
890
891YYSTYPE addpeer (YYSTYPE arg1, YYSTYPE arg2)
892{
893 node_t *np1 = (node_t *) arg1;
894 node_t *np2 = (node_t *) arg2;
895 node_t *hook_point;
896
897 hook_point = np1;
898
899 while (hook_point->peer)
900 hook_point = hook_point->peer;
901
902 hook_point->peer = np2;
903 return (arg1);
904}
905
906/*
907 * add_slist (stmt_list, stmt)
908 */
909
910YYSTYPE add_slist (YYSTYPE a1, YYSTYPE a2)
911{
912 if (a1 && a2)
913 return (addpeer(a1, a2));
914 else if(a1)
915 return(a1);
916 else
917 return(a2);
918}
919
920/*
921 * add_define (char *name, defn_list);
922 */
923YYSTYPE add_define (YYSTYPE a1, YYSTYPE a2)
924{
925 node_t *np;
926
927 np = make_node(NODE_DEFINE);
928 np->data[0] = a1;
Dave Barach557d1282016-11-10 14:22:49 -0500929 np->data[3] = (void *) message_crc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 deeper((YYSTYPE)np, a2);
931 return ((YYSTYPE) np);
932}
933
934/*
935 * add_defbody (defn_list, new_defn)
936 */
937YYSTYPE add_defbody (YYSTYPE a1, YYSTYPE a2)
938{
939 return (addpeer(a1, a2));
940}
941
942/*
943 * add_primtype ([packed], primitive type, instance)
944 */
945
946YYSTYPE add_primtype (YYSTYPE a1, YYSTYPE a2, YYSTYPE a3)
947{
948 node_t *np1;
949
950 np1 = (node_t *)a1;
951
952 /* Hook instance to type node */
953 deeper (a1, a2);
954 if (a3) {
955 deeper(a1, a3);
956 }
957 return (a1);
958}
959
960/*
961 * add_complex(char *type_name, instance)
962 */
963
964YYSTYPE add_complex (YYSTYPE a1, YYSTYPE a2)
965{
966 node_t *np;
967
968 np = make_node(NODE_COMPLEX);
969 np->data[0] = (void *) a1;
970
971 deeper((YYSTYPE)np, a2);
972 return ((YYSTYPE) np);
973}
974
975/*
976 * add_union(char *type_name, definition)
977 */
978
979YYSTYPE add_union (YYSTYPE a1, YYSTYPE a2)
980{
981 node_t *np;
982
983 np = make_node(NODE_UNION);
984 np->data[0] = (void *) a1;
985
986 deeper((YYSTYPE)np, a2);
987 return ((YYSTYPE) np);
988}
989
990
991/*
992 * add_vector_vbl (node_t *variable, YYSTYPE size)
993 */
994
995YYSTYPE add_vector_vbl (YYSTYPE a1, YYSTYPE a2)
996{
997 node_t *np;
998
999 np = make_node(NODE_VECTOR);
1000 np->data[0] = (void *) a1;
1001 np->data[1] = (void *) a2;
1002 return ((YYSTYPE) np);
1003}
1004
1005/*
Marek Gradzkifa42e252016-06-15 16:38:33 +02001006 * add_vector_vbl (char *vector_name, char *vector_length_var)
1007 */
1008
1009YYSTYPE add_variable_length_vector_vbl (YYSTYPE vector_name, YYSTYPE vector_length_var)
1010{
1011 node_t *np;
1012
1013 np = make_node(NODE_VECTOR);
1014 np->data[0] = (void *) vector_name;
1015 np->data[1] = (void *) 0; // vector size used for vpe.api.h generation (array of length zero)
1016 np->data[2] = (void *) vector_length_var; // name of the variable that stores vector length
1017 return ((YYSTYPE) np);
1018}
1019
1020/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 * add_scalar_vbl (char *name)
1022 */
1023YYSTYPE add_scalar_vbl (YYSTYPE a1)
1024{
1025 node_t *np;
1026
1027 np = make_node(NODE_SCALAR);
1028 np->data[0] = (void *) a1;
1029 return ((YYSTYPE) np);
1030}
1031
1032/*
1033 * set_flags (int flags, msg(=0?))
1034 */
1035YYSTYPE set_flags(YYSTYPE a1, YYSTYPE a2)
1036{
1037 node_t *np;
1038 int flags;
1039
1040 np = (node_t *)a2;
1041 if (!np)
1042 return(0);
1043
1044 flags = (int)(uword) a1;
1045
1046 np->flags |= flags;
1047 return (a2);
1048}
1049/*
1050 * suppress_version
1051 */
1052YYSTYPE suppress_version (void)
1053{
1054 dont_output_version = 1;
1055 return (0);
1056}
1057
1058void dump(node_t *np)
1059{
1060 node_vft_t *vftp;
1061
1062 while (np) {
1063 vftp = the_vft[np->type];
1064 vftp->print(np);
1065 np = np->peer;
1066 }
1067}
1068
1069char *fixup_input_filename(void)
1070{
1071 char *cp;
1072
1073 cp = (char *)input_filename;
1074
1075 while (*cp)
1076 cp++;
1077
1078 cp--;
1079
1080 while (cp > input_filename && *cp != '/')
1081 cp--;
1082 if (*cp == '/')
1083 cp++;
1084
Dave Barachf9c231e2016-08-05 10:10:18 -04001085 strncpy (tmpbuf, cp, sizeof(tmpbuf)-1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086
1087 cp = tmpbuf;
1088
1089 while (*cp)
1090 cp++;
1091
1092 cp--;
1093
1094 while (cp > tmpbuf && *cp != '.')
1095 cp--;
1096
1097 if (*cp == '.')
1098 *cp = 0;
1099
1100 return (sxerox(tmpbuf));
1101}
1102
1103void generate_top_boilerplate(FILE *fp)
1104
1105{
1106 char *datestring = ctime(&starttime);
1107 fixed_name = fixup_input_filename();
1108
1109 datestring[24] = 0;
1110
1111 fprintf (fp, "/*\n");
1112 fprintf (fp, " * VLIB API definitions %s\n", datestring);
1113 fprintf (fp, " * Input file: %s\n", input_filename);
1114 fprintf (fp, " * Automatically generated: please edit the input file ");
1115 fprintf (fp, "NOT this file!\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 fprintf (fp, " */\n\n");
1117 fprintf (fp, "#if defined(vl_msg_id)||defined(vl_union_id)||");
1118 fprintf (fp, "defined(vl_printfun) \\\n ||defined(vl_endianfun)||");
1119 fprintf (fp, " defined(vl_api_version)||defined(vl_typedefs) \\\n");
Dave Barach557d1282016-11-10 14:22:49 -05001120 fprintf (fp, " ||defined(vl_msg_name)||defined(vl_msg_name_crc_list)\n");
Ed Warnickecb9cada2015-12-08 15:45:58 -07001121 fprintf (fp, "/* ok, something was selected */\n");
1122 fprintf (fp, "#else\n");
1123 fprintf (fp, "#warning no content included from %s\n", input_filename);
1124 fprintf (fp, "#endif\n\n");
1125 fprintf (fp, "#define VL_API_PACKED(x) x __attribute__ ((packed))\n\n");
1126}
1127
1128void generate_bottom_boilerplate(FILE *fp)
1129
1130{
1131 fprintf (fp, "\n#ifdef vl_api_version\n");
1132
1133 if (dont_output_version) {
1134 fprintf (fp, "/* WARNING: API FILE VERSION CHECK DISABLED */\n");
1135 input_crc = 0;
1136 }
1137
1138 fprintf (fp, "vl_api_version(%s, 0x%08x)\n\n",
1139 fixed_name, (unsigned int)input_crc);
1140 fprintf (fp, "#endif\n\n");
1141}
1142
1143void generate_msg_ids(YYSTYPE a1, FILE *fp)
1144{
1145 node_t *np = (node_t *)a1;
1146
1147 fprintf (fp, "\n/****** Message ID / handler enum ******/\n\n");
1148 fprintf (fp, "#ifdef vl_msg_id\n");
1149
1150 while (np) {
1151 if (np->type == NODE_DEFINE) {
1152 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1153 fprintf (fp, "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n",
1154 uppercase(np->data[0]), (i8 *)np->data[0]);
1155 } else {
1156 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1157 }
1158 }
1159 np = np->peer;
1160 }
1161 fprintf (fp, "#endif\n");
1162
1163}
1164
1165void generate_msg_names(YYSTYPE a1, FILE *fp)
1166{
1167 node_t *np = (node_t *)a1;
1168
1169 fprintf (fp, "\n/****** Message names ******/\n\n");
1170
1171 fprintf (fp, "#ifdef vl_msg_name\n");
1172
1173 while (np) {
1174 if (np->type == NODE_DEFINE) {
1175 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1176 fprintf (fp, "vl_msg_name(vl_api_%s_t, %d)\n",
1177 (i8 *) np->data[0],
1178 (np->flags & NODE_FLAG_DONT_TRACE ? 0 : 1));
1179 } else {
1180 fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]);
1181 }
1182 }
1183 np = np->peer;
1184 }
1185 fprintf (fp, "#endif\n\n");
1186}
1187
Dave Barach557d1282016-11-10 14:22:49 -05001188void generate_msg_name_crc_list (YYSTYPE a1, FILE *fp)
1189{
1190 node_t *np = (node_t *)a1;
1191 char *unique_suffix, *cp;
1192
1193 unique_suffix = sxerox(fixed_name);
1194
1195 cp = unique_suffix;
1196 while (*cp && (*cp != '.'))
1197 cp++;
1198 if (*cp == '.')
1199 *cp = 0;
1200
1201 fprintf (fp, "\n/****** Message name, crc list ******/\n\n");
1202
1203 fprintf (fp, "#ifdef vl_msg_name_crc_list\n");
1204 fprintf (fp, "#define foreach_vl_msg_name_crc_%s ", unique_suffix);
1205
1206 while (np) {
1207 if (np->type == NODE_DEFINE) {
1208 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
1209 fprintf (fp, "\\\n_(VL_API_%s, %s, %08x) ",
1210 uppercase (np->data[0]), (i8 *) np->data[0],
1211 (u32)(u64)np->data[3]);
1212 }
1213 }
1214 np = np->peer;
1215 }
1216 fprintf (fp, "\n#endif\n\n");
1217}
1218
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219void generate_typedefs(YYSTYPE a1, FILE *fp)
1220{
1221 node_t *np = (node_t *)a1;
1222 node_vft_t *vftp;
1223
1224 fprintf(fp, "\n/****** Typedefs *****/\n\n");
1225 fprintf(fp, "#ifdef vl_typedefs\n\n");
1226
1227 /* Walk the top-level node-list */
1228 while (np) {
1229 if (np->type == NODE_DEFINE) {
1230 /* Yeah, this is pedantic */
1231 vftp = the_vft[np->type];
1232 vftp->generate(np, TYPEDEF_PASS, fp);
1233 }
1234 np = np->peer;
1235 }
1236 fprintf(fp, "#endif /* vl_typedefs */\n\n");
1237}
1238
1239void union_walk_one_defn(node_t *np, FILE *fp)
1240{
1241 node_t *vblp;
1242 node_t *uelem;
1243
1244 /* Walk the list of typed objects in this msg def */
1245 while (np) {
1246 if (np->type == NODE_UNION) {
1247 current_union_name = np->data[0];
1248 uelem = np->deeper;
1249
1250 /* Walk the list of objects in this union */
1251 while (uelem) {
1252 vblp = uelem->deeper;
1253 /* Drill down on each element, find the variable name */
1254 while(vblp) {
1255 if (vblp->type == NODE_SCALAR ||
1256 vblp->type == NODE_VECTOR ||
1257 vblp->type == NODE_COMPLEX) {
1258 fprintf(ofp, "#define %s_",
1259 uppercase(current_def_name));
1260 fprintf(ofp, "%s_", uppercase(current_union_name));
1261 fprintf(ofp, "%s %d\n",uppercase(vblp->data[0]),
1262 current_id);
1263 current_id++;
1264 break;
1265 }
1266 vblp = vblp->deeper;
1267 }
1268 uelem = uelem->peer;
1269 }
1270 current_union_name = 0;
1271 current_id = 1;
1272 }
1273 np = np->peer;
1274 }
1275}
1276
1277void generate_uniondefs(YYSTYPE a1, FILE *fp)
1278{
1279 node_t *np = (node_t *)a1;
1280
1281 fprintf(fp, "/****** Discriminated Union Definitions *****/\n\n");
1282 fprintf(fp, "#ifdef vl_union_id\n\n");
1283
1284 /* Walk the top-level node-list */
1285 while (np) {
1286 if (np->type == NODE_DEFINE) {
1287 current_id = 1;
1288 current_def_name = np->data[0];
1289 union_walk_one_defn(np->deeper, fp);
1290 }
1291 np = np->peer;
1292 }
1293 fprintf(fp, "\n#endif /* vl_union_id */\n\n");
1294}
1295
1296void generate_printfun(YYSTYPE a1, FILE *fp)
1297{
1298 node_t *np = (node_t *)a1;
1299 node_vft_t *vftp;
1300
1301 fprintf(fp, "/****** Print functions *****/\n\n");
1302 fprintf(fp, "#ifdef vl_printfun\n\n");
1303
1304 fprintf(fp, "#ifdef LP64\n");
1305 fputs ("#define _uword_fmt \"%lld\"\n", fp);
1306 fputs ("#define _uword_cast (long long)\n", fp);
1307 fprintf(fp, "#else\n");
1308 fputs("#define _uword_fmt \"%ld\"\n", fp);
1309 fputs ("#define _uword_cast long\n", fp);
1310 fprintf(fp, "#endif\n\n");
1311
1312 /* Walk the top-level node-list */
1313 while (np) {
1314 if (np->type == NODE_DEFINE) {
1315 if (!(np->flags & NODE_FLAG_MANUAL_PRINT)) {
1316 fprintf(fp,
1317 "static inline void *vl_api_%s_t_print (vl_api_%s_t *a,",
1318 (i8 *)np->data[0], (i8 *) np->data[0]);
1319 fprintf(fp, "void *handle)\n{\n");
1320 /* output the message name */
1321 fprintf(fp,
1322 " vl_print(handle, \"vl_api_%s_t:\\n\");\n",
1323 (i8 *)np->data[0]);
1324
1325 indent += 4;
1326 /* Yeah, this is pedantic */
1327 vftp = the_vft[np->type];
1328 vftp->generate(np, PRINTFUN_PASS, fp);
1329 fprintf(fp, " return handle;\n");
1330 fprintf(fp, "}\n\n");
1331 indent -= 4;
1332 } else {
1333 fprintf(fp, "/***** manual: vl_api_%s_t_print *****/\n\n",
1334 (i8 *) np->data[0]);
1335 }
1336 }
1337 np = np->peer;
1338 }
1339 fprintf(fp, "#endif /* vl_printfun */\n\n");
1340}
1341
1342void generate_endianfun(YYSTYPE a1, FILE *fp)
1343{
1344 node_t *np = (node_t *)a1;
1345 node_vft_t *vftp;
1346
1347 fprintf(fp, "\n/****** Endian swap functions *****/\n\n");
1348 fprintf(fp, "#ifdef vl_endianfun\n\n");
1349 fprintf(fp, "#undef clib_net_to_host_uword\n");
1350 fprintf(fp, "#ifdef LP64\n");
1351 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u64\n");
1352 fprintf(fp, "#else\n");
1353 fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u32\n");
1354 fprintf(fp, "#endif\n\n");
1355
1356 /* Walk the top-level node-list */
1357 while (np) {
1358 if (np->type == NODE_DEFINE) {
1359 if (!(np->flags & NODE_FLAG_MANUAL_ENDIAN)) {
1360 fprintf(fp,
1361 "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)\n{\n",
1362 (i8 *) np->data[0], (i8 *) np->data[0]);
1363 indent += 4;
1364 /* Yeah, this is pedantic */
1365 vftp = the_vft[np->type];
1366 vftp->generate(np, ENDIANFUN_PASS, fp);
1367 fprintf(fp, "}\n\n");
1368 indent -= 4;
1369 } else {
1370 fprintf(fp, "/***** manual: vl_api_%s_t_endian *****/\n\n",
1371 (i8 *) np->data[0]);
1372 }
1373 }
1374 np = np->peer;
1375 }
1376 fprintf(fp, "#endif /* vl_endianfun */\n\n");
1377}
1378
1379void add_msg_ids(YYSTYPE a1)
1380{
1381 node_t *np = (node_t *)a1;
1382 node_t *new_u16;
1383 node_t *new_vbl;
1384
1385 /* Walk the top-level node-list */
1386 while (np) {
1387 if (np->type == NODE_DEFINE) {
1388 if (!(np->flags & NODE_FLAG_TYPEONLY)) {
Ole Troan6855f6c2016-04-09 03:16:30 +02001389 /* add the parse tree for "u16 _vl_msg_id" */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390 new_u16 = make_node(NODE_U16);
1391 new_u16->peer = np->deeper;
1392 np->deeper = new_u16;
1393 new_vbl = make_node(NODE_SCALAR);
1394 new_vbl->data[0] = sxerox("_vl_msg_id");
1395 new_u16->deeper = new_vbl;
Ole Troan6855f6c2016-04-09 03:16:30 +02001396 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001397 }
1398 np = np->peer;
1399 }
1400}
1401
Marek Gradzki101759c2016-09-29 13:20:52 +02001402void generate_python_msg_definitions(YYSTYPE a1, FILE *fp)
Ole Troan6855f6c2016-04-09 03:16:30 +02001403{
Marek Gradzki101759c2016-09-29 13:20:52 +02001404 node_t *np = (node_t *)a1;
1405 node_vft_t *vftp;
1406 fprintf (fp, "messages = [\n");
1407 /* Walk the top-level node-list */
1408 while (np) {
1409 if (np->type == NODE_DEFINE && !(np->flags & NODE_FLAG_TYPEONLY)) {
1410 /* Yeah, this is pedantic */
1411 vftp = the_vft[np->type];
1412 vftp->generate(np, PYTHON_PASS, fp);
1413 }
1414 np = np->peer;
Ole Troan6855f6c2016-04-09 03:16:30 +02001415 }
Marek Gradzki101759c2016-09-29 13:20:52 +02001416 fprintf (fp, "\n]\n");
1417}
Ole Troan5f9dcff2016-08-01 04:59:13 +02001418
Dave Barach557d1282016-11-10 14:22:49 -05001419static bool
1420is_typeonly_check(node_t *np, bool typeonly)
1421{
1422 bool is_typeonly = (np->flags & NODE_FLAG_TYPEONLY);
1423 return (is_typeonly == typeonly);
1424}
1425
1426static void
1427generate_json_definitions(YYSTYPE a1, FILE *fp, bool typeonly)
1428{
1429 node_t *np = (node_t *)a1;
1430 node_vft_t *vftp;
1431 indent_me(fp);
1432 if (typeonly)
1433 fprintf (fp, "\"types\" : [\n");
1434 else
1435 fprintf (fp, "\"messages\" : [\n");
1436
1437 /* Walk the top-level node-list */
1438 bool comma = false;
1439 indent += 4;
1440 while (np) {
1441 if (np->type == NODE_DEFINE && is_typeonly_check(np, typeonly)) {
1442 /* Yeah, this is pedantic */
1443 vftp = the_vft[np->type];
1444 indent_me(fp);
1445 vftp->generate(np, JSON_PASS, fp);
1446 comma = true;
1447 }
1448 np = np->peer;
1449 if (comma && np &&
1450 np->type == NODE_DEFINE && is_typeonly_check(np, typeonly))
1451 fprintf (fp, ",\n");
1452
1453 }
1454 indent -= 4;
1455 fprintf (fp, "\n");
1456 indent_me(fp);
1457 fprintf(fp, "]");
1458}
1459
Marek Gradzki101759c2016-09-29 13:20:52 +02001460void generate_python_typeonly_definitions(YYSTYPE a1, FILE *fp)
1461{
1462 node_t *np = (node_t *)a1;
1463 node_vft_t *vftp;
1464 fprintf (fp, "types = [\n");
1465 /* Walk the top-level node-list */
1466 while (np) {
1467 if (np->type == NODE_DEFINE && (np->flags & NODE_FLAG_TYPEONLY)) {
1468 vftp = the_vft[np->type];
1469 vftp->generate(np, PYTHON_PASS, fp);
1470 }
1471 np = np->peer;
1472 }
1473 fprintf (fp, "\n]\n");
1474}
1475
1476void generate_python(YYSTYPE a1, FILE *fp)
1477{
1478 generate_python_typeonly_definitions(a1, fp);
1479 generate_python_msg_definitions(a1, fp);
1480
1481 /*
1482 * API CRC signature
1483 */
1484 fprintf (fp, "vl_api_version = 0x%08x\n\n", (unsigned int)input_crc);
Ole Troan6855f6c2016-04-09 03:16:30 +02001485}
1486
Dave Barach557d1282016-11-10 14:22:49 -05001487void generate_json(YYSTYPE a1, FILE *fp)
1488{
1489 fprintf (fp, "{\n");
1490 indent += 4;
1491 generate_json_definitions(a1, fp, true);
1492 fprintf (fp, ",\n");
1493 generate_json_definitions(a1, fp, false);
1494
1495 /*
1496 * API CRC signature
1497 */
1498 fprintf (fp, ",\n\"vl_api_version\" :\"0x%08x\"\n",
1499 (unsigned int)input_crc);
1500 fprintf (fp, "}\n");
1501}
1502
Ed Warnickecb9cada2015-12-08 15:45:58 -07001503void generate(YYSTYPE a1)
1504{
1505 if (dump_tree) {
1506 dump((node_t *)a1);
1507 }
1508
1509 add_msg_ids(a1);
1510
1511 if (ofp) {
1512 generate_top_boilerplate(ofp);
1513
1514 generate_msg_ids(a1, ofp);
1515 generate_msg_names(a1, ofp);
Dave Barach557d1282016-11-10 14:22:49 -05001516 generate_msg_name_crc_list(a1, ofp);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001517 generate_typedefs(a1, ofp);
1518 generate_uniondefs(a1, ofp);
1519 generate_printfun(a1, ofp);
1520 generate_endianfun(a1, ofp);
1521
1522 generate_bottom_boilerplate(ofp);
1523 }
Ole Troan6855f6c2016-04-09 03:16:30 +02001524 if (pythonfp) {
1525 generate_python(a1, pythonfp);
1526 }
Dave Barach557d1282016-11-10 14:22:49 -05001527 if (jsonfp) {
1528 generate_json(a1, jsonfp);
1529 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001530}