Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 1 | /* |
| 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> |
| 21 | #include <stdbool.h> |
| 22 | #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 | |
| 35 | FILE *ofp; |
| 36 | FILE *pythonfp; |
| 37 | FILE *jsonfp; |
| 38 | time_t starttime; |
| 39 | char *vlib_app_name; |
| 40 | char *input_filename; |
| 41 | node_vft_t *the_vft[NODE_N_TYPES]; |
| 42 | static int indent; |
| 43 | static int dont_output_version; |
| 44 | int dump_tree; |
| 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 | |
| 56 | void indent_me(FILE *ofp) |
| 57 | { |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < indent; i++) |
| 61 | putc(' ', ofp); |
| 62 | } |
| 63 | |
| 64 | char *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 | |
| 80 | char *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 | |
| 96 | void 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 | |
| 106 | void 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 | |
| 129 | case PYTHON_PASS: |
| 130 | fputs("('", pythonfp); |
| 131 | fputs((char *)type_name, pythonfp); |
| 132 | fputs("', ", pythonfp); |
| 133 | break; |
| 134 | |
| 135 | case JSON_PASS: |
| 136 | fputs("[\"", jsonfp); |
| 137 | fputs((char *)type_name, jsonfp); |
| 138 | fputs("\", ", jsonfp); |
| 139 | break; |
| 140 | |
| 141 | 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 | |
| 152 | void node_illegal_print (node_t *this) |
| 153 | { |
| 154 | fprintf(stderr, "node_illegal_print called\n"); |
| 155 | exit(0); |
| 156 | } |
| 157 | |
| 158 | void 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 | |
| 164 | node_vft_t node_illegal_vft = { |
| 165 | node_illegal_print, |
| 166 | node_illegal_generate, |
| 167 | "illegal" |
| 168 | }; |
| 169 | |
| 170 | void node_u8_print (node_t *this) |
| 171 | { |
| 172 | primtype_recursive_print(this, "u8 "); |
| 173 | } |
| 174 | |
| 175 | void node_u8_generate (node_t *this, enum passid which, FILE *ofp) |
| 176 | { |
| 177 | primtype_recursive_generate(this, which, ofp, "u8", "%u", "(unsigned)"); |
| 178 | } |
| 179 | |
| 180 | node_vft_t node_u8_vft = { |
| 181 | node_u8_print, |
| 182 | node_u8_generate, |
| 183 | NULL |
| 184 | }; |
| 185 | |
| 186 | void node_u16_print (node_t *this) |
| 187 | { |
| 188 | primtype_recursive_print(this, "u16 "); |
| 189 | } |
| 190 | |
| 191 | void node_u16_generate (node_t *this, enum passid which, FILE *ofp) |
| 192 | { |
| 193 | primtype_recursive_generate(this, which, ofp, "u16", "%u", "(unsigned)"); |
| 194 | } |
| 195 | |
| 196 | node_vft_t node_u16_vft = { |
| 197 | node_u16_print, |
| 198 | node_u16_generate, |
| 199 | "clib_net_to_host_u16" |
| 200 | }; |
| 201 | |
| 202 | void node_u32_print (node_t *this) |
| 203 | { |
| 204 | primtype_recursive_print(this, "u32 "); |
| 205 | } |
| 206 | |
| 207 | void node_u32_generate (node_t *this, enum passid which, FILE *ofp) |
| 208 | { |
| 209 | primtype_recursive_generate(this, which, ofp, "u32", "%u", "(unsigned)"); |
| 210 | } |
| 211 | |
| 212 | node_vft_t node_u32_vft = { |
| 213 | node_u32_print, |
| 214 | node_u32_generate, |
| 215 | "clib_net_to_host_u32", |
| 216 | }; |
| 217 | |
| 218 | void node_u64_print (node_t *this) |
| 219 | { |
| 220 | primtype_recursive_print(this, "u64 "); |
| 221 | } |
| 222 | |
| 223 | void 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 | |
| 229 | node_vft_t node_u64_vft = { |
| 230 | node_u64_print, |
| 231 | node_u64_generate, |
| 232 | "clib_net_to_host_u64" |
| 233 | }; |
| 234 | |
| 235 | void node_i8_print (node_t *this) |
| 236 | { |
| 237 | primtype_recursive_print(this, "i8 "); |
| 238 | } |
| 239 | |
| 240 | void node_i8_generate (node_t *this, enum passid which, FILE *ofp) |
| 241 | { |
| 242 | primtype_recursive_generate(this, which, ofp, "i8", "%d", "(int)"); |
| 243 | } |
| 244 | |
| 245 | node_vft_t node_i8_vft = { |
| 246 | node_i8_print, |
| 247 | node_i8_generate, |
| 248 | "" |
| 249 | }; |
| 250 | |
| 251 | void node_i16_print (node_t *this) |
| 252 | { |
| 253 | primtype_recursive_print(this, "i16 "); |
| 254 | } |
| 255 | |
| 256 | void node_i16_generate (node_t *this, enum passid which, FILE *ofp) |
| 257 | { |
| 258 | primtype_recursive_generate(this, which, ofp, "i16", "%d", "(int)"); |
| 259 | } |
| 260 | |
| 261 | node_vft_t node_i16_vft = { |
| 262 | node_i16_print, |
| 263 | node_i16_generate, |
| 264 | "clib_net_to_host_u16" |
| 265 | }; |
| 266 | |
| 267 | void node_i32_print (node_t *this) |
| 268 | { |
| 269 | primtype_recursive_print(this, "i32 "); |
| 270 | } |
| 271 | |
| 272 | void node_i32_generate (node_t *this, enum passid which, FILE *ofp) |
| 273 | { |
| 274 | primtype_recursive_generate(this, which, ofp, "i32", "%ld", "(long)"); |
| 275 | } |
| 276 | |
| 277 | node_vft_t node_i32_vft = { |
| 278 | node_i32_print, |
| 279 | node_i32_generate, |
| 280 | "clib_net_to_host_u32" |
| 281 | }; |
| 282 | |
| 283 | void node_i64_print (node_t *this) |
| 284 | { |
| 285 | primtype_recursive_print(this, "i64 "); |
| 286 | } |
| 287 | |
| 288 | void 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 | |
| 294 | node_vft_t node_i64_vft = { |
| 295 | node_i64_print, |
| 296 | node_i64_generate, |
| 297 | "clib_net_to_host_u64" |
| 298 | }; |
| 299 | |
| 300 | void node_f64_print (node_t *this) |
| 301 | { |
| 302 | primtype_recursive_print(this, "f64 "); |
| 303 | } |
| 304 | |
| 305 | void node_f64_generate (node_t *this, enum passid which, FILE *ofp) |
| 306 | { |
| 307 | primtype_recursive_generate(this, which, ofp, "f64", "%.2f", |
| 308 | "(double)"); |
| 309 | } |
| 310 | |
| 311 | node_vft_t node_f64_vft = { |
| 312 | node_f64_print, |
| 313 | node_f64_generate, |
| 314 | " ", /* FP numbers are sent in host byte order */ |
| 315 | }; |
| 316 | |
| 317 | |
| 318 | void node_packed_print (node_t *this) |
| 319 | { |
| 320 | primtype_recursive_print (this, "packed "); |
| 321 | } |
| 322 | |
| 323 | void node_packed_generate (node_t *this, enum passid which, FILE *ofp) |
| 324 | { |
| 325 | primtype_recursive_generate(this, which, ofp, "PACKED", "", ""); |
| 326 | } |
| 327 | |
| 328 | node_vft_t node_packed_vft = { |
| 329 | node_packed_print, |
| 330 | node_packed_generate, |
| 331 | 0, |
| 332 | }; |
| 333 | |
| 334 | void 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 | |
| 345 | void node_define_generate (node_t *this, enum passid which, FILE *fp) |
| 346 | { |
| 347 | node_t *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 | |
| 374 | 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 | |
| 388 | 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 | |
| 406 | default: |
| 407 | fprintf(stderr, "node_define_generate: unimp pass %d\n", which); |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | node_vft_t node_define_vft = { |
| 413 | node_define_print, |
| 414 | node_define_generate, |
| 415 | 0, |
| 416 | }; |
| 417 | |
| 418 | void node_union_print (node_t *this) |
| 419 | { |
| 420 | primtype_recursive_print (this, "union "); |
| 421 | } |
| 422 | |
| 423 | void 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 | |
| 497 | node_vft_t node_union_vft = { |
| 498 | node_union_print, |
| 499 | node_union_generate, |
| 500 | 0, |
| 501 | }; |
| 502 | |
| 503 | void node_scalar_print (node_t *this) |
| 504 | { |
| 505 | fprintf(stdout, "%s", CDATA0); |
| 506 | primtype_recursive_print (this, ""); |
| 507 | } |
| 508 | |
| 509 | void 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 { |
| 556 | fprintf(fp, "/* a->%s%s = a->%s%s (no-op) */\n", |
| 557 | union_prefix, CDATA0, |
| 558 | union_prefix, CDATA0); |
| 559 | } |
| 560 | } |
| 561 | break; |
| 562 | case PYTHON_PASS: |
| 563 | fprintf(fp, "'%s'),\n", CDATA0); |
| 564 | break; |
| 565 | |
| 566 | case JSON_PASS: |
| 567 | fprintf(fp, "\"%s\"]", CDATA0); |
| 568 | break; |
| 569 | |
| 570 | 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 | |
| 579 | node_vft_t node_scalar_vft = { |
| 580 | node_scalar_print, |
| 581 | node_scalar_generate, |
| 582 | 0, |
| 583 | }; |
| 584 | |
| 585 | void node_vector_print (node_t *this) |
| 586 | { |
| 587 | primtype_recursive_print (this, "vector "); |
| 588 | } |
| 589 | |
| 590 | void 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; |
| 644 | /* If this is a simple endian swap, but the endian swap method is a no-op, |
| 645 | * then indicate this is a no-op in a comment. |
| 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 | } |
| 654 | |
| 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; |
| 683 | case PYTHON_PASS: |
| 684 | 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 | } |
| 689 | break; |
| 690 | |
| 691 | 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 | |
| 699 | 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 | |
| 707 | node_vft_t node_vector_vft = { |
| 708 | node_vector_print, |
| 709 | node_vector_generate, |
| 710 | 0, |
| 711 | }; |
| 712 | |
| 713 | void node_complex_print (node_t *this) |
| 714 | { |
| 715 | primtype_recursive_print (this, "complex "); |
| 716 | } |
| 717 | |
| 718 | void 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); |
Dave Barach | 05472b6 | 2017-02-27 09:25:39 -0500 | [diff] [blame] | 756 | |
Dave Barach | a80ab84 | 2017-03-02 07:38:52 -0500 | [diff] [blame] | 757 | if (deeper && deeper->type == NODE_VECTOR) |
Dave Barach | 05472b6 | 2017-02-27 09:25:39 -0500 | [diff] [blame] | 758 | fprintf(fp, "%s_print(a->%s%s, handle);\n", |
| 759 | CDATA0, union_prefix, member_name); |
| 760 | else |
| 761 | fprintf(fp, "%s_print(&a->%s%s, handle);\n", |
| 762 | CDATA0, union_prefix, member_name); |
| 763 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 764 | indent_me(fp); |
| 765 | fprintf(fp, "vl_print(handle, \"%s%s ----- END \\n\");\n", |
| 766 | union_prefix, member_name); |
| 767 | break; |
| 768 | |
| 769 | case ENDIANFUN_PASS: |
| 770 | deeper = this->deeper; |
| 771 | while (deeper) { |
| 772 | if (deeper->type == NODE_SCALAR || |
| 773 | deeper->type == NODE_VECTOR) { |
| 774 | member_name = deeper->data[0]; |
| 775 | break; |
| 776 | } |
| 777 | deeper = deeper->deeper; |
| 778 | } |
| 779 | |
| 780 | indent_me(fp); |
Dave Barach | a80ab84 | 2017-03-02 07:38:52 -0500 | [diff] [blame] | 781 | if (deeper && deeper->type == NODE_VECTOR) |
Dave Barach | 05472b6 | 2017-02-27 09:25:39 -0500 | [diff] [blame] | 782 | fprintf(fp, "%s_endian(a->%s%s);\n", |
| 783 | CDATA0, union_prefix, member_name); |
| 784 | else |
| 785 | fprintf(fp, "%s_endian(&a->%s%s);\n", |
| 786 | CDATA0, union_prefix, member_name); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 787 | break; |
| 788 | case PYTHON_PASS: |
| 789 | fprintf(fp, "('%s',", CDATA0); |
| 790 | deeper = this->deeper; |
| 791 | if (deeper) { |
| 792 | vftp = the_vft[deeper->type]; |
| 793 | vftp->generate(deeper, which, fp); |
| 794 | } |
| 795 | break; |
| 796 | |
| 797 | case JSON_PASS: |
| 798 | fprintf(fp, "[\"%s\", ", CDATA0); |
| 799 | deeper = this->deeper; |
| 800 | if (deeper) { |
| 801 | vftp = the_vft[deeper->type]; |
| 802 | vftp->generate(deeper, which, fp); |
| 803 | } |
| 804 | break; |
| 805 | |
| 806 | default: |
| 807 | fprintf(stderr, "node_complex_generate unimp pass %d...\n", which); |
| 808 | break; |
| 809 | } |
| 810 | current_is_complex--; |
| 811 | } |
| 812 | |
| 813 | node_vft_t node_complex_vft = { |
| 814 | node_complex_print, |
| 815 | node_complex_generate, |
| 816 | 0, |
| 817 | }; |
| 818 | |
| 819 | void node_noversion_print (node_t *this) |
| 820 | { |
| 821 | primtype_recursive_print (this, "noversion "); |
| 822 | } |
| 823 | |
| 824 | void node_noversion_generate (node_t *this, enum passid which, FILE *ofp) |
| 825 | { |
| 826 | fprintf(stderr, "node_noversion_generate called...\n"); |
| 827 | } |
| 828 | |
| 829 | node_vft_t node_noversion_vft = { |
| 830 | node_noversion_print, |
| 831 | node_noversion_generate, |
| 832 | 0, |
| 833 | }; |
| 834 | |
| 835 | void node_uword_print (node_t *this) |
| 836 | { |
| 837 | primtype_recursive_print(this, "uword "); |
| 838 | } |
| 839 | |
| 840 | void node_uword_generate (node_t *this, enum passid which, FILE *ofp) |
| 841 | { |
| 842 | primtype_recursive_generate(this, which, ofp, "uword", "uword", ""); |
| 843 | } |
| 844 | |
| 845 | node_vft_t node_uword_vft = { |
| 846 | node_uword_print, |
| 847 | node_uword_generate, |
| 848 | "clib_net_to_host_uword", |
| 849 | }; |
| 850 | |
| 851 | node_vft_t *the_vft[NODE_N_TYPES] = { |
| 852 | &node_illegal_vft, |
| 853 | &node_u8_vft, |
| 854 | &node_u16_vft, |
| 855 | &node_u32_vft, |
| 856 | &node_u64_vft, |
| 857 | &node_i8_vft, |
| 858 | &node_i16_vft, |
| 859 | &node_i32_vft, |
| 860 | &node_i64_vft, |
| 861 | &node_f64_vft, |
| 862 | &node_packed_vft, |
| 863 | &node_define_vft, |
| 864 | &node_union_vft, |
| 865 | &node_scalar_vft, |
| 866 | &node_vector_vft, |
| 867 | &node_complex_vft, |
| 868 | &node_noversion_vft, |
| 869 | &node_uword_vft, |
| 870 | }; |
| 871 | |
| 872 | void *make_node (enum node_subclass type) |
| 873 | { |
| 874 | node_t *rv; |
| 875 | |
| 876 | rv = (node_t *) malloc (sizeof (*rv)); |
| 877 | if (rv == 0) { |
| 878 | fprintf (stderr, "fatal: make_node out of memory\n"); |
| 879 | exit (1); |
| 880 | } |
| 881 | bzero (rv, sizeof (*rv)); |
| 882 | rv->type = type; |
| 883 | return ((void *) rv); |
| 884 | } |
| 885 | |
| 886 | YYSTYPE deeper (YYSTYPE arg1, YYSTYPE arg2) |
| 887 | { |
| 888 | node_t *np1 = (node_t *) arg1; |
| 889 | node_t *np2 = (node_t *) arg2; |
| 890 | node_t *hook_point; |
| 891 | |
| 892 | hook_point = np1; |
| 893 | |
| 894 | while (hook_point->deeper) |
| 895 | hook_point = hook_point->deeper; |
| 896 | |
| 897 | hook_point->deeper = np2; |
| 898 | return (arg1); |
| 899 | } |
| 900 | |
| 901 | YYSTYPE addpeer (YYSTYPE arg1, YYSTYPE arg2) |
| 902 | { |
| 903 | node_t *np1 = (node_t *) arg1; |
| 904 | node_t *np2 = (node_t *) arg2; |
| 905 | node_t *hook_point; |
| 906 | |
| 907 | hook_point = np1; |
| 908 | |
| 909 | while (hook_point->peer) |
| 910 | hook_point = hook_point->peer; |
| 911 | |
| 912 | hook_point->peer = np2; |
| 913 | return (arg1); |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * add_slist (stmt_list, stmt) |
| 918 | */ |
| 919 | |
| 920 | YYSTYPE add_slist (YYSTYPE a1, YYSTYPE a2) |
| 921 | { |
| 922 | if (a1 && a2) |
| 923 | return (addpeer(a1, a2)); |
| 924 | else if(a1) |
| 925 | return(a1); |
| 926 | else |
| 927 | return(a2); |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * add_define (char *name, defn_list); |
| 932 | */ |
| 933 | YYSTYPE add_define (YYSTYPE a1, YYSTYPE a2) |
| 934 | { |
| 935 | node_t *np; |
| 936 | |
| 937 | np = make_node(NODE_DEFINE); |
| 938 | np->data[0] = a1; |
| 939 | np->data[3] = (void *) message_crc; |
| 940 | deeper((YYSTYPE)np, a2); |
| 941 | return ((YYSTYPE) np); |
| 942 | } |
| 943 | |
| 944 | /* |
| 945 | * add_defbody (defn_list, new_defn) |
| 946 | */ |
| 947 | YYSTYPE add_defbody (YYSTYPE a1, YYSTYPE a2) |
| 948 | { |
| 949 | return (addpeer(a1, a2)); |
| 950 | } |
| 951 | |
| 952 | /* |
| 953 | * add_primtype ([packed], primitive type, instance) |
| 954 | */ |
| 955 | |
| 956 | YYSTYPE add_primtype (YYSTYPE a1, YYSTYPE a2, YYSTYPE a3) |
| 957 | { |
| 958 | /* Hook instance to type node */ |
| 959 | deeper (a1, a2); |
| 960 | if (a3) { |
| 961 | deeper(a1, a3); |
| 962 | } |
| 963 | return (a1); |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * add_complex(char *type_name, instance) |
| 968 | */ |
| 969 | |
| 970 | YYSTYPE add_complex (YYSTYPE a1, YYSTYPE a2) |
| 971 | { |
| 972 | node_t *np; |
| 973 | |
| 974 | np = make_node(NODE_COMPLEX); |
| 975 | np->data[0] = (void *) a1; |
| 976 | |
| 977 | deeper((YYSTYPE)np, a2); |
| 978 | return ((YYSTYPE) np); |
| 979 | } |
| 980 | |
| 981 | /* |
| 982 | * add_union(char *type_name, definition) |
| 983 | */ |
| 984 | |
| 985 | YYSTYPE add_union (YYSTYPE a1, YYSTYPE a2) |
| 986 | { |
| 987 | node_t *np; |
| 988 | |
| 989 | np = make_node(NODE_UNION); |
| 990 | np->data[0] = (void *) a1; |
| 991 | |
| 992 | deeper((YYSTYPE)np, a2); |
| 993 | return ((YYSTYPE) np); |
| 994 | } |
| 995 | |
| 996 | |
| 997 | /* |
| 998 | * add_vector_vbl (node_t *variable, YYSTYPE size) |
| 999 | */ |
| 1000 | |
| 1001 | YYSTYPE add_vector_vbl (YYSTYPE a1, YYSTYPE a2) |
| 1002 | { |
| 1003 | node_t *np; |
| 1004 | |
| 1005 | np = make_node(NODE_VECTOR); |
| 1006 | np->data[0] = (void *) a1; |
| 1007 | np->data[1] = (void *) a2; |
| 1008 | return ((YYSTYPE) np); |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * add_vector_vbl (char *vector_name, char *vector_length_var) |
| 1013 | */ |
| 1014 | |
| 1015 | YYSTYPE add_variable_length_vector_vbl (YYSTYPE vector_name, YYSTYPE vector_length_var) |
| 1016 | { |
| 1017 | node_t *np; |
| 1018 | |
| 1019 | np = make_node(NODE_VECTOR); |
| 1020 | np->data[0] = (void *) vector_name; |
| 1021 | np->data[1] = (void *) 0; // vector size used for vpe.api.h generation (array of length zero) |
| 1022 | np->data[2] = (void *) vector_length_var; // name of the variable that stores vector length |
| 1023 | return ((YYSTYPE) np); |
| 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * add_scalar_vbl (char *name) |
| 1028 | */ |
| 1029 | YYSTYPE add_scalar_vbl (YYSTYPE a1) |
| 1030 | { |
| 1031 | node_t *np; |
| 1032 | |
| 1033 | np = make_node(NODE_SCALAR); |
| 1034 | np->data[0] = (void *) a1; |
| 1035 | return ((YYSTYPE) np); |
| 1036 | } |
| 1037 | |
| 1038 | /* |
| 1039 | * set_flags (int flags, msg(=0?)) |
| 1040 | */ |
| 1041 | YYSTYPE set_flags(YYSTYPE a1, YYSTYPE a2) |
| 1042 | { |
| 1043 | node_t *np; |
| 1044 | int flags; |
| 1045 | |
| 1046 | np = (node_t *)a2; |
| 1047 | if (!np) |
| 1048 | return(0); |
| 1049 | |
| 1050 | flags = (int)(uword) a1; |
| 1051 | |
| 1052 | np->flags |= flags; |
Dave Barach | 11b8dbf | 2017-04-24 10:46:54 -0400 | [diff] [blame] | 1053 | |
| 1054 | /* Generate a foo_reply_t right here */ |
| 1055 | if (flags & NODE_FLAG_AUTOREPLY) |
| 1056 | autoreply(np); |
| 1057 | |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 1058 | return (a2); |
| 1059 | } |
| 1060 | /* |
| 1061 | * suppress_version |
| 1062 | */ |
| 1063 | YYSTYPE suppress_version (void) |
| 1064 | { |
| 1065 | dont_output_version = 1; |
| 1066 | return (0); |
| 1067 | } |
| 1068 | |
| 1069 | void dump(node_t *np) |
| 1070 | { |
| 1071 | node_vft_t *vftp; |
| 1072 | |
| 1073 | while (np) { |
| 1074 | vftp = the_vft[np->type]; |
| 1075 | vftp->print(np); |
| 1076 | np = np->peer; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | char *fixup_input_filename(void) |
| 1081 | { |
| 1082 | char *cp; |
| 1083 | |
| 1084 | cp = (char *)input_filename; |
| 1085 | |
| 1086 | while (*cp) |
| 1087 | cp++; |
| 1088 | |
| 1089 | cp--; |
| 1090 | |
| 1091 | while (cp > input_filename && *cp != '/') |
| 1092 | cp--; |
| 1093 | if (*cp == '/') |
| 1094 | cp++; |
| 1095 | |
| 1096 | strncpy (tmpbuf, cp, sizeof(tmpbuf)-1); |
| 1097 | |
| 1098 | cp = tmpbuf; |
| 1099 | |
| 1100 | while (*cp) |
| 1101 | cp++; |
| 1102 | |
| 1103 | cp--; |
| 1104 | |
| 1105 | while (cp > tmpbuf && *cp != '.') |
| 1106 | cp--; |
| 1107 | |
| 1108 | if (*cp == '.') |
| 1109 | *cp = 0; |
| 1110 | |
| 1111 | return (sxerox(tmpbuf)); |
| 1112 | } |
| 1113 | |
| 1114 | void generate_top_boilerplate(FILE *fp) |
| 1115 | |
| 1116 | { |
| 1117 | char *datestring = ctime(&starttime); |
| 1118 | fixed_name = fixup_input_filename(); |
| 1119 | |
| 1120 | datestring[24] = 0; |
| 1121 | |
| 1122 | fprintf (fp, "/*\n"); |
| 1123 | fprintf (fp, " * VLIB API definitions %s\n", datestring); |
| 1124 | fprintf (fp, " * Input file: %s\n", input_filename); |
| 1125 | fprintf (fp, " * Automatically generated: please edit the input file "); |
| 1126 | fprintf (fp, "NOT this file!\n"); |
| 1127 | fprintf (fp, " */\n\n"); |
| 1128 | fprintf (fp, "#if defined(vl_msg_id)||defined(vl_union_id)||"); |
| 1129 | fprintf (fp, "defined(vl_printfun) \\\n ||defined(vl_endianfun)||"); |
| 1130 | fprintf (fp, " defined(vl_api_version)||defined(vl_typedefs) \\\n"); |
| 1131 | fprintf (fp, " ||defined(vl_msg_name)||defined(vl_msg_name_crc_list)\n"); |
| 1132 | fprintf (fp, "/* ok, something was selected */\n"); |
| 1133 | fprintf (fp, "#else\n"); |
| 1134 | fprintf (fp, "#warning no content included from %s\n", input_filename); |
| 1135 | fprintf (fp, "#endif\n\n"); |
| 1136 | fprintf (fp, "#define VL_API_PACKED(x) x __attribute__ ((packed))\n\n"); |
| 1137 | } |
| 1138 | |
| 1139 | void generate_bottom_boilerplate(FILE *fp) |
| 1140 | |
| 1141 | { |
| 1142 | fprintf (fp, "\n#ifdef vl_api_version\n"); |
| 1143 | |
| 1144 | if (dont_output_version) { |
| 1145 | fprintf (fp, "/* WARNING: API FILE VERSION CHECK DISABLED */\n"); |
| 1146 | input_crc = 0; |
| 1147 | } |
| 1148 | |
| 1149 | fprintf (fp, "vl_api_version(%s, 0x%08x)\n\n", |
| 1150 | fixed_name, (unsigned int)input_crc); |
| 1151 | fprintf (fp, "#endif\n\n"); |
| 1152 | } |
| 1153 | |
| 1154 | void generate_msg_ids(YYSTYPE a1, FILE *fp) |
| 1155 | { |
| 1156 | node_t *np = (node_t *)a1; |
| 1157 | |
| 1158 | fprintf (fp, "\n/****** Message ID / handler enum ******/\n\n"); |
| 1159 | fprintf (fp, "#ifdef vl_msg_id\n"); |
| 1160 | |
| 1161 | while (np) { |
| 1162 | if (np->type == NODE_DEFINE) { |
| 1163 | if (!(np->flags & NODE_FLAG_TYPEONLY)) { |
| 1164 | fprintf (fp, "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n", |
| 1165 | uppercase(np->data[0]), (i8 *)np->data[0]); |
| 1166 | } else { |
| 1167 | fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]); |
| 1168 | } |
| 1169 | } |
| 1170 | np = np->peer; |
| 1171 | } |
| 1172 | fprintf (fp, "#endif\n"); |
| 1173 | |
| 1174 | } |
| 1175 | |
| 1176 | void generate_msg_names(YYSTYPE a1, FILE *fp) |
| 1177 | { |
| 1178 | node_t *np = (node_t *)a1; |
| 1179 | |
| 1180 | fprintf (fp, "\n/****** Message names ******/\n\n"); |
| 1181 | |
| 1182 | fprintf (fp, "#ifdef vl_msg_name\n"); |
| 1183 | |
| 1184 | while (np) { |
| 1185 | if (np->type == NODE_DEFINE) { |
| 1186 | if (!(np->flags & NODE_FLAG_TYPEONLY)) { |
| 1187 | fprintf (fp, "vl_msg_name(vl_api_%s_t, %d)\n", |
| 1188 | (i8 *) np->data[0], |
| 1189 | (np->flags & NODE_FLAG_DONT_TRACE ? 0 : 1)); |
| 1190 | } else { |
| 1191 | fprintf (fp, "/* typeonly: %s */\n", (i8 *)np->data[0]); |
| 1192 | } |
| 1193 | } |
| 1194 | np = np->peer; |
| 1195 | } |
| 1196 | fprintf (fp, "#endif\n\n"); |
| 1197 | } |
| 1198 | |
| 1199 | void generate_msg_name_crc_list (YYSTYPE a1, FILE *fp) |
| 1200 | { |
| 1201 | node_t *np = (node_t *)a1; |
| 1202 | char *unique_suffix, *cp; |
| 1203 | |
| 1204 | unique_suffix = sxerox(fixed_name); |
| 1205 | |
| 1206 | cp = unique_suffix; |
| 1207 | while (*cp && (*cp != '.')) |
| 1208 | cp++; |
| 1209 | if (*cp == '.') |
| 1210 | *cp = 0; |
| 1211 | |
| 1212 | fprintf (fp, "\n/****** Message name, crc list ******/\n\n"); |
| 1213 | |
| 1214 | fprintf (fp, "#ifdef vl_msg_name_crc_list\n"); |
| 1215 | fprintf (fp, "#define foreach_vl_msg_name_crc_%s ", unique_suffix); |
| 1216 | |
| 1217 | while (np) { |
| 1218 | if (np->type == NODE_DEFINE) { |
| 1219 | if (!(np->flags & NODE_FLAG_TYPEONLY)) { |
| 1220 | fprintf (fp, "\\\n_(VL_API_%s, %s, %08x) ", |
| 1221 | uppercase (np->data[0]), (i8 *) np->data[0], |
| 1222 | (u32)(u64)np->data[3]); |
| 1223 | } |
| 1224 | } |
| 1225 | np = np->peer; |
| 1226 | } |
| 1227 | fprintf (fp, "\n#endif\n\n"); |
| 1228 | free (unique_suffix); |
| 1229 | } |
| 1230 | |
| 1231 | void generate_typedefs(YYSTYPE a1, FILE *fp) |
| 1232 | { |
| 1233 | node_t *np = (node_t *)a1; |
| 1234 | node_vft_t *vftp; |
| 1235 | |
| 1236 | fprintf(fp, "\n/****** Typedefs *****/\n\n"); |
| 1237 | fprintf(fp, "#ifdef vl_typedefs\n\n"); |
| 1238 | |
| 1239 | /* Walk the top-level node-list */ |
| 1240 | while (np) { |
| 1241 | if (np->type == NODE_DEFINE) { |
| 1242 | /* Yeah, this is pedantic */ |
| 1243 | vftp = the_vft[np->type]; |
| 1244 | vftp->generate(np, TYPEDEF_PASS, fp); |
| 1245 | } |
| 1246 | np = np->peer; |
| 1247 | } |
| 1248 | fprintf(fp, "#endif /* vl_typedefs */\n\n"); |
| 1249 | } |
| 1250 | |
| 1251 | void union_walk_one_defn(node_t *np, FILE *fp) |
| 1252 | { |
| 1253 | node_t *vblp; |
| 1254 | node_t *uelem; |
| 1255 | |
| 1256 | /* Walk the list of typed objects in this msg def */ |
| 1257 | while (np) { |
| 1258 | if (np->type == NODE_UNION) { |
| 1259 | current_union_name = np->data[0]; |
| 1260 | uelem = np->deeper; |
| 1261 | |
| 1262 | /* Walk the list of objects in this union */ |
| 1263 | while (uelem) { |
| 1264 | vblp = uelem->deeper; |
| 1265 | /* Drill down on each element, find the variable name */ |
| 1266 | while(vblp) { |
| 1267 | if (vblp->type == NODE_SCALAR || |
| 1268 | vblp->type == NODE_VECTOR || |
| 1269 | vblp->type == NODE_COMPLEX) { |
| 1270 | fprintf(ofp, "#define %s_", |
| 1271 | uppercase(current_def_name)); |
| 1272 | fprintf(ofp, "%s_", uppercase(current_union_name)); |
| 1273 | fprintf(ofp, "%s %d\n",uppercase(vblp->data[0]), |
| 1274 | current_id); |
| 1275 | current_id++; |
| 1276 | break; |
| 1277 | } |
| 1278 | vblp = vblp->deeper; |
| 1279 | } |
| 1280 | uelem = uelem->peer; |
| 1281 | } |
| 1282 | current_union_name = 0; |
| 1283 | current_id = 1; |
| 1284 | } |
| 1285 | np = np->peer; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | void generate_uniondefs(YYSTYPE a1, FILE *fp) |
| 1290 | { |
| 1291 | node_t *np = (node_t *)a1; |
| 1292 | |
| 1293 | fprintf(fp, "/****** Discriminated Union Definitions *****/\n\n"); |
| 1294 | fprintf(fp, "#ifdef vl_union_id\n\n"); |
| 1295 | |
| 1296 | /* Walk the top-level node-list */ |
| 1297 | while (np) { |
| 1298 | if (np->type == NODE_DEFINE) { |
| 1299 | current_id = 1; |
| 1300 | current_def_name = np->data[0]; |
| 1301 | union_walk_one_defn(np->deeper, fp); |
| 1302 | } |
| 1303 | np = np->peer; |
| 1304 | } |
| 1305 | fprintf(fp, "\n#endif /* vl_union_id */\n\n"); |
| 1306 | } |
| 1307 | |
| 1308 | void generate_printfun(YYSTYPE a1, FILE *fp) |
| 1309 | { |
| 1310 | node_t *np = (node_t *)a1; |
| 1311 | node_vft_t *vftp; |
| 1312 | |
| 1313 | fprintf(fp, "/****** Print functions *****/\n\n"); |
| 1314 | fprintf(fp, "#ifdef vl_printfun\n\n"); |
| 1315 | |
| 1316 | fprintf(fp, "#ifdef LP64\n"); |
| 1317 | fputs ("#define _uword_fmt \"%lld\"\n", fp); |
| 1318 | fputs ("#define _uword_cast (long long)\n", fp); |
| 1319 | fprintf(fp, "#else\n"); |
| 1320 | fputs("#define _uword_fmt \"%ld\"\n", fp); |
| 1321 | fputs ("#define _uword_cast long\n", fp); |
| 1322 | fprintf(fp, "#endif\n\n"); |
| 1323 | |
| 1324 | /* Walk the top-level node-list */ |
| 1325 | while (np) { |
| 1326 | if (np->type == NODE_DEFINE) { |
| 1327 | if (!(np->flags & NODE_FLAG_MANUAL_PRINT)) { |
| 1328 | fprintf(fp, |
| 1329 | "static inline void *vl_api_%s_t_print (vl_api_%s_t *a,", |
| 1330 | (i8 *)np->data[0], (i8 *) np->data[0]); |
| 1331 | fprintf(fp, "void *handle)\n{\n"); |
| 1332 | /* output the message name */ |
| 1333 | fprintf(fp, |
| 1334 | " vl_print(handle, \"vl_api_%s_t:\\n\");\n", |
| 1335 | (i8 *)np->data[0]); |
| 1336 | |
| 1337 | indent += 4; |
| 1338 | /* Yeah, this is pedantic */ |
| 1339 | vftp = the_vft[np->type]; |
| 1340 | vftp->generate(np, PRINTFUN_PASS, fp); |
| 1341 | fprintf(fp, " return handle;\n"); |
| 1342 | fprintf(fp, "}\n\n"); |
| 1343 | indent -= 4; |
| 1344 | } else { |
| 1345 | fprintf(fp, "/***** manual: vl_api_%s_t_print *****/\n\n", |
| 1346 | (i8 *) np->data[0]); |
| 1347 | } |
| 1348 | } |
| 1349 | np = np->peer; |
| 1350 | } |
| 1351 | fprintf(fp, "#endif /* vl_printfun */\n\n"); |
| 1352 | } |
| 1353 | |
| 1354 | void generate_endianfun(YYSTYPE a1, FILE *fp) |
| 1355 | { |
| 1356 | node_t *np = (node_t *)a1; |
| 1357 | node_vft_t *vftp; |
| 1358 | |
| 1359 | fprintf(fp, "\n/****** Endian swap functions *****/\n\n"); |
| 1360 | fprintf(fp, "#ifdef vl_endianfun\n\n"); |
| 1361 | fprintf(fp, "#undef clib_net_to_host_uword\n"); |
| 1362 | fprintf(fp, "#ifdef LP64\n"); |
| 1363 | fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u64\n"); |
| 1364 | fprintf(fp, "#else\n"); |
| 1365 | fprintf(fp, "#define clib_net_to_host_uword clib_net_to_host_u32\n"); |
| 1366 | fprintf(fp, "#endif\n\n"); |
| 1367 | |
| 1368 | /* Walk the top-level node-list */ |
| 1369 | while (np) { |
| 1370 | if (np->type == NODE_DEFINE) { |
| 1371 | if (!(np->flags & NODE_FLAG_MANUAL_ENDIAN)) { |
| 1372 | fprintf(fp, |
| 1373 | "static inline void vl_api_%s_t_endian (vl_api_%s_t *a)\n{\n", |
| 1374 | (i8 *) np->data[0], (i8 *) np->data[0]); |
| 1375 | indent += 4; |
| 1376 | /* Yeah, this is pedantic */ |
| 1377 | vftp = the_vft[np->type]; |
| 1378 | vftp->generate(np, ENDIANFUN_PASS, fp); |
| 1379 | fprintf(fp, "}\n\n"); |
| 1380 | indent -= 4; |
| 1381 | } else { |
| 1382 | fprintf(fp, "/***** manual: vl_api_%s_t_endian *****/\n\n", |
| 1383 | (i8 *) np->data[0]); |
| 1384 | } |
| 1385 | } |
| 1386 | np = np->peer; |
| 1387 | } |
| 1388 | fprintf(fp, "#endif /* vl_endianfun */\n\n"); |
| 1389 | } |
| 1390 | |
| 1391 | void add_msg_ids(YYSTYPE a1) |
| 1392 | { |
| 1393 | node_t *np = (node_t *)a1; |
| 1394 | node_t *new_u16; |
| 1395 | node_t *new_vbl; |
| 1396 | |
| 1397 | /* Walk the top-level node-list */ |
| 1398 | while (np) { |
| 1399 | if (np->type == NODE_DEFINE) { |
| 1400 | if (!(np->flags & NODE_FLAG_TYPEONLY)) { |
| 1401 | /* add the parse tree for "u16 _vl_msg_id" */ |
| 1402 | new_u16 = make_node(NODE_U16); |
| 1403 | new_u16->peer = np->deeper; |
| 1404 | np->deeper = new_u16; |
| 1405 | new_vbl = make_node(NODE_SCALAR); |
| 1406 | new_vbl->data[0] = sxerox("_vl_msg_id"); |
| 1407 | new_u16->deeper = new_vbl; |
| 1408 | } |
| 1409 | } |
| 1410 | np = np->peer; |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | void generate_python_msg_definitions(YYSTYPE a1, FILE *fp) |
| 1415 | { |
| 1416 | node_t *np = (node_t *)a1; |
| 1417 | node_vft_t *vftp; |
| 1418 | fprintf (fp, "messages = [\n"); |
| 1419 | /* Walk the top-level node-list */ |
| 1420 | while (np) { |
| 1421 | if (np->type == NODE_DEFINE && !(np->flags & NODE_FLAG_TYPEONLY)) { |
| 1422 | /* Yeah, this is pedantic */ |
| 1423 | vftp = the_vft[np->type]; |
| 1424 | vftp->generate(np, PYTHON_PASS, fp); |
| 1425 | } |
| 1426 | np = np->peer; |
| 1427 | } |
| 1428 | fprintf (fp, "\n]\n"); |
| 1429 | } |
| 1430 | |
| 1431 | static bool |
| 1432 | is_typeonly_check(node_t *np, bool typeonly) |
| 1433 | { |
| 1434 | bool is_typeonly = (np->flags & NODE_FLAG_TYPEONLY); |
| 1435 | return (is_typeonly == typeonly); |
| 1436 | } |
| 1437 | |
| 1438 | static void |
| 1439 | generate_json_definitions(YYSTYPE a1, FILE *fp, bool typeonly) |
| 1440 | { |
| 1441 | node_t *np = (node_t *)a1; |
| 1442 | node_vft_t *vftp; |
| 1443 | indent_me(fp); |
| 1444 | if (typeonly) |
| 1445 | fprintf (fp, "\"types\" : [\n"); |
| 1446 | else |
| 1447 | fprintf (fp, "\"messages\" : [\n"); |
| 1448 | |
| 1449 | /* Walk the top-level node-list */ |
| 1450 | bool comma = false; |
| 1451 | indent += 4; |
| 1452 | while (np) { |
| 1453 | if (np->type == NODE_DEFINE && is_typeonly_check(np, typeonly)) { |
| 1454 | /* Yeah, this is pedantic */ |
| 1455 | vftp = the_vft[np->type]; |
| 1456 | indent_me(fp); |
| 1457 | vftp->generate(np, JSON_PASS, fp); |
| 1458 | comma = true; |
| 1459 | } |
| 1460 | np = np->peer; |
| 1461 | if (comma && np && |
| 1462 | np->type == NODE_DEFINE && is_typeonly_check(np, typeonly)) |
| 1463 | fprintf (fp, ",\n"); |
| 1464 | |
| 1465 | } |
| 1466 | indent -= 4; |
| 1467 | fprintf (fp, "\n"); |
| 1468 | indent_me(fp); |
| 1469 | fprintf(fp, "]"); |
| 1470 | } |
| 1471 | |
| 1472 | void generate_python_typeonly_definitions(YYSTYPE a1, FILE *fp) |
| 1473 | { |
| 1474 | node_t *np = (node_t *)a1; |
| 1475 | node_vft_t *vftp; |
| 1476 | fprintf (fp, "types = [\n"); |
| 1477 | /* Walk the top-level node-list */ |
| 1478 | while (np) { |
| 1479 | if (np->type == NODE_DEFINE && (np->flags & NODE_FLAG_TYPEONLY)) { |
| 1480 | vftp = the_vft[np->type]; |
| 1481 | vftp->generate(np, PYTHON_PASS, fp); |
| 1482 | } |
| 1483 | np = np->peer; |
| 1484 | } |
| 1485 | fprintf (fp, "\n]\n"); |
| 1486 | } |
| 1487 | |
| 1488 | void generate_python(YYSTYPE a1, FILE *fp) |
| 1489 | { |
| 1490 | generate_python_typeonly_definitions(a1, fp); |
| 1491 | generate_python_msg_definitions(a1, fp); |
| 1492 | |
| 1493 | /* |
| 1494 | * API CRC signature |
| 1495 | */ |
| 1496 | fprintf (fp, "vl_api_version = 0x%08x\n\n", (unsigned int)input_crc); |
| 1497 | } |
| 1498 | |
| 1499 | void generate_json(YYSTYPE a1, FILE *fp) |
| 1500 | { |
| 1501 | fprintf (fp, "{\n"); |
| 1502 | indent += 4; |
| 1503 | generate_json_definitions(a1, fp, true); |
| 1504 | fprintf (fp, ",\n"); |
| 1505 | generate_json_definitions(a1, fp, false); |
| 1506 | |
| 1507 | /* |
| 1508 | * API CRC signature |
| 1509 | */ |
| 1510 | fprintf (fp, ",\n\"vl_api_version\" :\"0x%08x\"\n", |
| 1511 | (unsigned int)input_crc); |
| 1512 | fprintf (fp, "}\n"); |
| 1513 | } |
| 1514 | |
| 1515 | void generate(YYSTYPE a1) |
| 1516 | { |
| 1517 | if (dump_tree) { |
| 1518 | dump((node_t *)a1); |
| 1519 | } |
| 1520 | |
| 1521 | add_msg_ids(a1); |
| 1522 | |
| 1523 | if (ofp) { |
| 1524 | generate_top_boilerplate(ofp); |
| 1525 | |
| 1526 | generate_msg_ids(a1, ofp); |
| 1527 | generate_msg_names(a1, ofp); |
| 1528 | generate_msg_name_crc_list(a1, ofp); |
| 1529 | generate_typedefs(a1, ofp); |
| 1530 | generate_uniondefs(a1, ofp); |
| 1531 | generate_printfun(a1, ofp); |
| 1532 | generate_endianfun(a1, ofp); |
| 1533 | |
| 1534 | generate_bottom_boilerplate(ofp); |
| 1535 | } |
| 1536 | if (pythonfp) { |
| 1537 | generate_python(a1, pythonfp); |
| 1538 | } |
| 1539 | if (jsonfp) { |
| 1540 | generate_json(a1, jsonfp); |
| 1541 | } |
| 1542 | } |