broadcom/genxml: Introduce a V3D packet/struct decoder.
[mesa.git] / src / broadcom / cle / v3d_decoder.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 * Copyright © 2017 Broadcom
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <expat.h>
31 #include <inttypes.h>
32 #include <zlib.h>
33
34 #include <util/macros.h>
35 #include <util/ralloc.h>
36
37 #include "v3d_decoder.h"
38 #include "v3d_packet_helpers.h"
39 #include "v3d_xml.h"
40
41 struct v3d_spec {
42 uint32_t ver;
43
44 int ncommands;
45 struct v3d_group *commands[256];
46 int nstructs;
47 struct v3d_group *structs[256];
48 int nregisters;
49 struct v3d_group *registers[256];
50 int nenums;
51 struct v3d_enum *enums[256];
52 };
53
54 struct location {
55 const char *filename;
56 int line_number;
57 };
58
59 struct parser_context {
60 XML_Parser parser;
61 int foo;
62 struct location loc;
63
64 struct v3d_group *group;
65 struct v3d_enum *enoom;
66
67 int nvalues;
68 struct v3d_value *values[256];
69
70 struct v3d_spec *spec;
71 };
72
73 const char *
74 v3d_group_get_name(struct v3d_group *group)
75 {
76 return group->name;
77 }
78
79 uint8_t
80 v3d_group_get_opcode(struct v3d_group *group)
81 {
82 return group->opcode;
83 }
84
85 struct v3d_group *
86 v3d_spec_find_struct(struct v3d_spec *spec, const char *name)
87 {
88 for (int i = 0; i < spec->nstructs; i++)
89 if (strcmp(spec->structs[i]->name, name) == 0)
90 return spec->structs[i];
91
92 return NULL;
93 }
94
95 struct v3d_group *
96 v3d_spec_find_register(struct v3d_spec *spec, uint32_t offset)
97 {
98 for (int i = 0; i < spec->nregisters; i++)
99 if (spec->registers[i]->register_offset == offset)
100 return spec->registers[i];
101
102 return NULL;
103 }
104
105 struct v3d_group *
106 v3d_spec_find_register_by_name(struct v3d_spec *spec, const char *name)
107 {
108 for (int i = 0; i < spec->nregisters; i++) {
109 if (strcmp(spec->registers[i]->name, name) == 0)
110 return spec->registers[i];
111 }
112
113 return NULL;
114 }
115
116 struct v3d_enum *
117 v3d_spec_find_enum(struct v3d_spec *spec, const char *name)
118 {
119 for (int i = 0; i < spec->nenums; i++)
120 if (strcmp(spec->enums[i]->name, name) == 0)
121 return spec->enums[i];
122
123 return NULL;
124 }
125
126 static void __attribute__((noreturn))
127 fail(struct location *loc, const char *msg, ...)
128 {
129 va_list ap;
130
131 va_start(ap, msg);
132 fprintf(stderr, "%s:%d: error: ",
133 loc->filename, loc->line_number);
134 vfprintf(stderr, msg, ap);
135 fprintf(stderr, "\n");
136 va_end(ap);
137 exit(EXIT_FAILURE);
138 }
139
140 static void *
141 fail_on_null(void *p)
142 {
143 if (p == NULL) {
144 fprintf(stderr, "aubinator: out of memory\n");
145 exit(EXIT_FAILURE);
146 }
147
148 return p;
149 }
150
151 static char *
152 xstrdup(const char *s)
153 {
154 return fail_on_null(strdup(s));
155 }
156
157 static void *
158 zalloc(size_t s)
159 {
160 return calloc(s, 1);
161 }
162
163 static void *
164 xzalloc(size_t s)
165 {
166 return fail_on_null(zalloc(s));
167 }
168
169 /* We allow fields to have either a bit index, or append "b" for a byte index.
170 */
171 static bool
172 is_byte_offset(const char *value)
173 {
174 return value[strlen(value) - 1] == 'b';
175 }
176
177 static void
178 get_group_offset_count(const char **atts, uint32_t *offset, uint32_t *count,
179 uint32_t *size, bool *variable)
180 {
181 char *p;
182 int i;
183
184 for (i = 0; atts[i]; i += 2) {
185 if (strcmp(atts[i], "count") == 0) {
186 *count = strtoul(atts[i + 1], &p, 0);
187 if (*count == 0)
188 *variable = true;
189 } else if (strcmp(atts[i], "start") == 0) {
190 *offset = strtoul(atts[i + 1], &p, 0);
191 } else if (strcmp(atts[i], "size") == 0) {
192 *size = strtoul(atts[i + 1], &p, 0);
193 }
194 }
195 return;
196 }
197
198 static struct v3d_group *
199 create_group(struct parser_context *ctx,
200 const char *name,
201 const char **atts,
202 struct v3d_group *parent)
203 {
204 struct v3d_group *group;
205
206 group = xzalloc(sizeof(*group));
207 if (name)
208 group->name = xstrdup(name);
209
210 group->spec = ctx->spec;
211 group->group_offset = 0;
212 group->group_count = 0;
213 group->variable = false;
214
215 if (parent) {
216 group->parent = parent;
217 get_group_offset_count(atts,
218 &group->group_offset,
219 &group->group_count,
220 &group->group_size,
221 &group->variable);
222 }
223
224 return group;
225 }
226
227 static struct v3d_enum *
228 create_enum(struct parser_context *ctx, const char *name, const char **atts)
229 {
230 struct v3d_enum *e;
231
232 e = xzalloc(sizeof(*e));
233 if (name)
234 e->name = xstrdup(name);
235
236 e->nvalues = 0;
237
238 return e;
239 }
240
241 static void
242 get_register_offset(const char **atts, uint32_t *offset)
243 {
244 char *p;
245 int i;
246
247 for (i = 0; atts[i]; i += 2) {
248 if (strcmp(atts[i], "num") == 0)
249 *offset = strtoul(atts[i + 1], &p, 0);
250 }
251 return;
252 }
253
254 static void
255 get_start_end_pos(int *start, int *end)
256 {
257 /* start value has to be mod with 32 as we need the relative
258 * start position in the first DWord. For the end position, add
259 * the length of the field to the start position to get the
260 * relative postion in the 64 bit address.
261 */
262 if (*end - *start > 32) {
263 int len = *end - *start;
264 *start = *start % 32;
265 *end = *start + len;
266 } else {
267 *start = *start % 32;
268 *end = *end % 32;
269 }
270
271 return;
272 }
273
274 static inline uint64_t
275 mask(int start, int end)
276 {
277 uint64_t v;
278
279 v = ~0ULL >> (63 - end + start);
280
281 return v << start;
282 }
283
284 static inline uint64_t
285 field(uint64_t value, int start, int end)
286 {
287 get_start_end_pos(&start, &end);
288 return (value & mask(start, end)) >> (start);
289 }
290
291 static inline uint64_t
292 field_address(uint64_t value, int start, int end)
293 {
294 /* no need to right shift for address/offset */
295 get_start_end_pos(&start, &end);
296 return (value & mask(start, end));
297 }
298
299 static struct v3d_type
300 string_to_type(struct parser_context *ctx, const char *s)
301 {
302 int i, f;
303 struct v3d_group *g;
304 struct v3d_enum *e;
305
306 if (strcmp(s, "int") == 0)
307 return (struct v3d_type) { .kind = V3D_TYPE_INT };
308 else if (strcmp(s, "uint") == 0)
309 return (struct v3d_type) { .kind = V3D_TYPE_UINT };
310 else if (strcmp(s, "bool") == 0)
311 return (struct v3d_type) { .kind = V3D_TYPE_BOOL };
312 else if (strcmp(s, "float") == 0)
313 return (struct v3d_type) { .kind = V3D_TYPE_FLOAT };
314 else if (strcmp(s, "address") == 0)
315 return (struct v3d_type) { .kind = V3D_TYPE_ADDRESS };
316 else if (strcmp(s, "offset") == 0)
317 return (struct v3d_type) { .kind = V3D_TYPE_OFFSET };
318 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
319 return (struct v3d_type) { .kind = V3D_TYPE_UFIXED, .i = i, .f = f };
320 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
321 return (struct v3d_type) { .kind = V3D_TYPE_SFIXED, .i = i, .f = f };
322 else if (g = v3d_spec_find_struct(ctx->spec, s), g != NULL)
323 return (struct v3d_type) { .kind = V3D_TYPE_STRUCT, .v3d_struct = g };
324 else if (e = v3d_spec_find_enum(ctx->spec, s), e != NULL)
325 return (struct v3d_type) { .kind = V3D_TYPE_ENUM, .v3d_enum = e };
326 else if (strcmp(s, "mbo") == 0)
327 return (struct v3d_type) { .kind = V3D_TYPE_MBO };
328 else
329 fail(&ctx->loc, "invalid type: %s", s);
330 }
331
332 static struct v3d_field *
333 create_field(struct parser_context *ctx, const char **atts)
334 {
335 struct v3d_field *field;
336 char *p;
337 int i;
338 uint32_t size = 0;
339
340 field = xzalloc(sizeof(*field));
341
342 for (i = 0; atts[i]; i += 2) {
343 if (strcmp(atts[i], "name") == 0)
344 field->name = xstrdup(atts[i + 1]);
345 else if (strcmp(atts[i], "start") == 0) {
346 field->start = strtoul(atts[i + 1], &p, 0);
347 if (is_byte_offset(atts[i + 1]))
348 field->start *= 8;
349 } else if (strcmp(atts[i], "end") == 0) {
350 field->end = strtoul(atts[i + 1], &p, 0) - 1;
351 if (is_byte_offset(atts[i + 1]))
352 field->end *= 8;
353 } else if (strcmp(atts[i], "size") == 0) {
354 size = strtoul(atts[i + 1], &p, 0);
355 if (is_byte_offset(atts[i + 1]))
356 size *= 8;
357 } else if (strcmp(atts[i], "type") == 0)
358 field->type = string_to_type(ctx, atts[i + 1]);
359 else if (strcmp(atts[i], "default") == 0 &&
360 field->start >= 16 && field->end <= 31) {
361 field->has_default = true;
362 field->default_value = strtoul(atts[i + 1], &p, 0);
363 }
364 }
365
366 if (size)
367 field->end = field->start + size - 1;
368
369 return field;
370 }
371
372 static struct v3d_value *
373 create_value(struct parser_context *ctx, const char **atts)
374 {
375 struct v3d_value *value = xzalloc(sizeof(*value));
376
377 for (int i = 0; atts[i]; i += 2) {
378 if (strcmp(atts[i], "name") == 0)
379 value->name = xstrdup(atts[i + 1]);
380 else if (strcmp(atts[i], "value") == 0)
381 value->value = strtoul(atts[i + 1], NULL, 0);
382 }
383
384 return value;
385 }
386
387 static void
388 create_and_append_field(struct parser_context *ctx,
389 const char **atts)
390 {
391 if (ctx->group->nfields == ctx->group->fields_size) {
392 ctx->group->fields_size = MAX2(ctx->group->fields_size * 2, 2);
393 ctx->group->fields =
394 (struct v3d_field **) realloc(ctx->group->fields,
395 sizeof(ctx->group->fields[0]) *
396 ctx->group->fields_size);
397 }
398
399 ctx->group->fields[ctx->group->nfields++] = create_field(ctx, atts);
400 }
401
402 static void
403 set_group_opcode(struct v3d_group *group, const char **atts)
404 {
405 char *p;
406 int i;
407
408 for (i = 0; atts[i]; i += 2) {
409 if (strcmp(atts[i], "code") == 0)
410 group->opcode = strtoul(atts[i + 1], &p, 0);
411 }
412 return;
413 }
414
415 static void
416 start_element(void *data, const char *element_name, const char **atts)
417 {
418 struct parser_context *ctx = data;
419 int i;
420 const char *name = NULL;
421 const char *ver = NULL;
422
423 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
424
425 for (i = 0; atts[i]; i += 2) {
426 if (strcmp(atts[i], "name") == 0)
427 name = atts[i + 1];
428 else if (strcmp(atts[i], "gen") == 0)
429 ver = atts[i + 1];
430 }
431
432 if (strcmp(element_name, "vcxml") == 0) {
433 if (ver == NULL)
434 fail(&ctx->loc, "no ver given");
435
436 int major, minor;
437 int n = sscanf(ver, "%d.%d", &major, &minor);
438 if (n == 0)
439 fail(&ctx->loc, "invalid ver given: %s", ver);
440 if (n == 1)
441 minor = 0;
442
443 ctx->spec->ver = major * 10 + minor;
444 } else if (strcmp(element_name, "packet") == 0 ||
445 strcmp(element_name, "struct") == 0) {
446 ctx->group = create_group(ctx, name, atts, NULL);
447
448 if (strcmp(element_name, "packet") == 0)
449 set_group_opcode(ctx->group, atts);
450 } else if (strcmp(element_name, "register") == 0) {
451 ctx->group = create_group(ctx, name, atts, NULL);
452 get_register_offset(atts, &ctx->group->register_offset);
453 } else if (strcmp(element_name, "group") == 0) {
454 struct v3d_group *previous_group = ctx->group;
455 while (previous_group->next)
456 previous_group = previous_group->next;
457
458 struct v3d_group *group = create_group(ctx, "", atts,
459 ctx->group);
460 previous_group->next = group;
461 ctx->group = group;
462 } else if (strcmp(element_name, "field") == 0) {
463 create_and_append_field(ctx, atts);
464 } else if (strcmp(element_name, "enum") == 0) {
465 ctx->enoom = create_enum(ctx, name, atts);
466 } else if (strcmp(element_name, "value") == 0) {
467 ctx->values[ctx->nvalues++] = create_value(ctx, atts);
468 assert(ctx->nvalues < ARRAY_SIZE(ctx->values));
469 }
470
471 }
472
473 static void
474 end_element(void *data, const char *name)
475 {
476 struct parser_context *ctx = data;
477 struct v3d_spec *spec = ctx->spec;
478
479 if (strcmp(name, "packet") == 0 ||
480 strcmp(name, "struct") == 0 ||
481 strcmp(name, "register") == 0) {
482 struct v3d_group *group = ctx->group;
483
484 ctx->group = ctx->group->parent;
485
486 if (strcmp(name, "packet") == 0) {
487 spec->commands[spec->ncommands++] = group;
488
489 /* V3D packet XML has the packet contents with offsets
490 * starting from the first bit after the opcode, to
491 * match the spec. Shift the fields up now.
492 */
493 for (int i = 0; i < group->nfields; i++) {
494 group->fields[i]->start += 8;
495 group->fields[i]->end += 8;
496 }
497 }
498 else if (strcmp(name, "struct") == 0)
499 spec->structs[spec->nstructs++] = group;
500 else if (strcmp(name, "register") == 0)
501 spec->registers[spec->nregisters++] = group;
502
503 assert(spec->ncommands < ARRAY_SIZE(spec->commands));
504 assert(spec->nstructs < ARRAY_SIZE(spec->structs));
505 assert(spec->nregisters < ARRAY_SIZE(spec->registers));
506 } else if (strcmp(name, "group") == 0) {
507 ctx->group = ctx->group->parent;
508 } else if (strcmp(name, "field") == 0) {
509 assert(ctx->group->nfields > 0);
510 struct v3d_field *field = ctx->group->fields[ctx->group->nfields - 1];
511 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
512 field->inline_enum.values = xzalloc(size);
513 field->inline_enum.nvalues = ctx->nvalues;
514 memcpy(field->inline_enum.values, ctx->values, size);
515 ctx->nvalues = 0;
516 } else if (strcmp(name, "enum") == 0) {
517 struct v3d_enum *e = ctx->enoom;
518 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
519 e->values = xzalloc(size);
520 e->nvalues = ctx->nvalues;
521 memcpy(e->values, ctx->values, size);
522 ctx->nvalues = 0;
523 ctx->enoom = NULL;
524 spec->enums[spec->nenums++] = e;
525 }
526 }
527
528 static void
529 character_data(void *data, const XML_Char *s, int len)
530 {
531 }
532
533 static uint32_t zlib_inflate(const void *compressed_data,
534 uint32_t compressed_len,
535 void **out_ptr)
536 {
537 struct z_stream_s zstream;
538 void *out;
539
540 memset(&zstream, 0, sizeof(zstream));
541
542 zstream.next_in = (unsigned char *)compressed_data;
543 zstream.avail_in = compressed_len;
544
545 if (inflateInit(&zstream) != Z_OK)
546 return 0;
547
548 out = malloc(4096);
549 zstream.next_out = out;
550 zstream.avail_out = 4096;
551
552 do {
553 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
554 case Z_STREAM_END:
555 goto end;
556 case Z_OK:
557 break;
558 default:
559 inflateEnd(&zstream);
560 return 0;
561 }
562
563 if (zstream.avail_out)
564 break;
565
566 out = realloc(out, 2*zstream.total_out);
567 if (out == NULL) {
568 inflateEnd(&zstream);
569 return 0;
570 }
571
572 zstream.next_out = (unsigned char *)out + zstream.total_out;
573 zstream.avail_out = zstream.total_out;
574 } while (1);
575 end:
576 inflateEnd(&zstream);
577 *out_ptr = out;
578 return zstream.total_out;
579 }
580
581 struct v3d_spec *
582 v3d_spec_load(const struct v3d_device_info *devinfo)
583 {
584 struct parser_context ctx;
585 void *buf;
586 uint8_t *text_data = NULL;
587 uint32_t text_offset = 0, text_length = 0, total_length;
588
589 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
590 if (genxml_files_table[i].gen_10 == devinfo->ver) {
591 text_offset = genxml_files_table[i].offset;
592 text_length = genxml_files_table[i].length;
593 break;
594 }
595 }
596
597 if (text_length == 0) {
598 fprintf(stderr, "unable to find gen (%u) data\n", devinfo->ver);
599 return NULL;
600 }
601
602 memset(&ctx, 0, sizeof ctx);
603 ctx.parser = XML_ParserCreate(NULL);
604 XML_SetUserData(ctx.parser, &ctx);
605 if (ctx.parser == NULL) {
606 fprintf(stderr, "failed to create parser\n");
607 return NULL;
608 }
609
610 XML_SetElementHandler(ctx.parser, start_element, end_element);
611 XML_SetCharacterDataHandler(ctx.parser, character_data);
612
613 ctx.spec = xzalloc(sizeof(*ctx.spec));
614
615 total_length = zlib_inflate(compress_genxmls,
616 sizeof(compress_genxmls),
617 (void **) &text_data);
618 assert(text_offset + text_length <= total_length);
619
620 buf = XML_GetBuffer(ctx.parser, text_length);
621 memcpy(buf, &text_data[text_offset], text_length);
622
623 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
624 fprintf(stderr,
625 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
626 XML_GetCurrentLineNumber(ctx.parser),
627 XML_GetCurrentColumnNumber(ctx.parser),
628 XML_GetCurrentByteIndex(ctx.parser), text_length,
629 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
630 XML_ParserFree(ctx.parser);
631 free(text_data);
632 return NULL;
633 }
634
635 XML_ParserFree(ctx.parser);
636 free(text_data);
637
638 return ctx.spec;
639 }
640
641 struct v3d_group *
642 v3d_spec_find_instruction(struct v3d_spec *spec, const uint8_t *p)
643 {
644 for (int i = 0; i < spec->ncommands; i++) {
645 uint8_t opcode = *p;
646 if (opcode == spec->commands[i]->opcode)
647 return spec->commands[i];
648 }
649
650 return NULL;
651 }
652
653 /** Returns the size of a V3D packet. */
654 int
655 v3d_group_get_length(struct v3d_group *group)
656 {
657 int last_bit = 0;
658 for (int i = 0; i < group->nfields; i++) {
659 struct v3d_field *field = group->fields[i];
660
661 last_bit = MAX2(last_bit, field->end);
662 }
663 return last_bit / 8 + 1;
664 }
665
666 void
667 v3d_field_iterator_init(struct v3d_field_iterator *iter,
668 struct v3d_group *group,
669 const uint8_t *p,
670 bool print_colors)
671 {
672 memset(iter, 0, sizeof(*iter));
673
674 iter->group = group;
675 iter->p = p;
676 iter->print_colors = print_colors;
677 }
678
679 static const char *
680 v3d_get_enum_name(struct v3d_enum *e, uint64_t value)
681 {
682 for (int i = 0; i < e->nvalues; i++) {
683 if (e->values[i]->value == value) {
684 return e->values[i]->name;
685 }
686 }
687 return NULL;
688 }
689
690 static bool
691 iter_more_fields(const struct v3d_field_iterator *iter)
692 {
693 return iter->field_iter < iter->group->nfields;
694 }
695
696 static uint32_t
697 iter_group_offset_bits(const struct v3d_field_iterator *iter,
698 uint32_t group_iter)
699 {
700 return iter->group->group_offset + (group_iter *
701 iter->group->group_size);
702 }
703
704 static bool
705 iter_more_groups(const struct v3d_field_iterator *iter)
706 {
707 if (iter->group->variable) {
708 return iter_group_offset_bits(iter, iter->group_iter + 1) <
709 (v3d_group_get_length(iter->group) * 8);
710 } else {
711 return (iter->group_iter + 1) < iter->group->group_count ||
712 iter->group->next != NULL;
713 }
714 }
715
716 static void
717 iter_advance_group(struct v3d_field_iterator *iter)
718 {
719 if (iter->group->variable)
720 iter->group_iter++;
721 else {
722 if ((iter->group_iter + 1) < iter->group->group_count) {
723 iter->group_iter++;
724 } else {
725 iter->group = iter->group->next;
726 iter->group_iter = 0;
727 }
728 }
729
730 iter->field_iter = 0;
731 }
732
733 static bool
734 iter_advance_field(struct v3d_field_iterator *iter)
735 {
736 while (!iter_more_fields(iter)) {
737 if (!iter_more_groups(iter))
738 return false;
739
740 iter_advance_group(iter);
741 }
742
743 iter->field = iter->group->fields[iter->field_iter++];
744 if (iter->field->name)
745 strncpy(iter->name, iter->field->name, sizeof(iter->name));
746 else
747 memset(iter->name, 0, sizeof(iter->name));
748 iter->offset = iter_group_offset_bits(iter, iter->group_iter) / 8 +
749 iter->field->start / 8;
750 iter->struct_desc = NULL;
751
752 return true;
753 }
754
755 bool
756 v3d_field_iterator_next(struct v3d_field_iterator *iter)
757 {
758 if (!iter_advance_field(iter))
759 return false;
760
761 const char *enum_name = NULL;
762
763 int s = iter->field->start;
764 int e = iter->field->end;
765
766 switch (iter->field->type.kind) {
767 case V3D_TYPE_UNKNOWN:
768 case V3D_TYPE_INT: {
769 uint32_t value = __gen_unpack_sint(iter->p, s, e);
770 snprintf(iter->value, sizeof(iter->value), "%d", value);
771 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
772 break;
773 }
774 case V3D_TYPE_UINT: {
775 uint32_t value = __gen_unpack_uint(iter->p, s, e);
776 snprintf(iter->value, sizeof(iter->value), "%u", value);
777 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
778 break;
779 }
780 case V3D_TYPE_BOOL: {
781 const char *true_string =
782 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
783 snprintf(iter->value, sizeof(iter->value), "%s",
784 __gen_unpack_uint(iter->p, s, e) ?
785 true_string : "false");
786 break;
787 }
788 case V3D_TYPE_FLOAT:
789 snprintf(iter->value, sizeof(iter->value), "%f",
790 __gen_unpack_float(iter->p, s, e));
791 break;
792 case V3D_TYPE_ADDRESS:
793 case V3D_TYPE_OFFSET:
794 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64,
795 __gen_unpack_uint(iter->p, s, e));
796 break;
797 case V3D_TYPE_STRUCT:
798 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
799 iter->field->type.v3d_struct->name);
800 iter->struct_desc =
801 v3d_spec_find_struct(iter->group->spec,
802 iter->field->type.v3d_struct->name);
803 break;
804 case V3D_TYPE_SFIXED:
805 snprintf(iter->value, sizeof(iter->value), "%f",
806 __gen_unpack_sfixed(iter->p, s, e,
807 iter->field->type.f));
808 break;
809 case V3D_TYPE_UFIXED:
810 snprintf(iter->value, sizeof(iter->value), "%f",
811 __gen_unpack_ufixed(iter->p, s, e,
812 iter->field->type.f));
813 break;
814 case V3D_TYPE_MBO:
815 break;
816 case V3D_TYPE_ENUM: {
817 uint32_t value = __gen_unpack_uint(iter->p, s, e);
818 snprintf(iter->value, sizeof(iter->value), "%d", value);
819 enum_name = v3d_get_enum_name(iter->field->type.v3d_enum, value);
820 break;
821 }
822 }
823
824 if (strlen(iter->group->name) == 0) {
825 int length = strlen(iter->name);
826 snprintf(iter->name + length, sizeof(iter->name) - length,
827 "[%i]", iter->group_iter);
828 }
829
830 if (enum_name) {
831 int length = strlen(iter->value);
832 snprintf(iter->value + length, sizeof(iter->value) - length,
833 " (%s)", enum_name);
834 }
835
836 return true;
837 }
838
839 void
840 v3d_print_group(FILE *outfile, struct v3d_group *group,
841 uint64_t offset, const uint8_t *p, bool color)
842 {
843 struct v3d_field_iterator iter;
844
845 v3d_field_iterator_init(&iter, group, p, color);
846 while (v3d_field_iterator_next(&iter)) {
847 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
848 if (iter.struct_desc) {
849 uint64_t struct_offset = offset + iter.offset;
850 v3d_print_group(outfile, iter.struct_desc,
851 struct_offset,
852 &p[iter.offset], color);
853 }
854 }
855 }